1 /* 2 * Copyright 2018 Advanced Micro Devices, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 * 23 */ 24 #include <linux/debugfs.h> 25 #include <linux/list.h> 26 #include <linux/module.h> 27 #include <linux/uaccess.h> 28 #include <linux/reboot.h> 29 #include <linux/syscalls.h> 30 #include <linux/pm_runtime.h> 31 32 #include "amdgpu.h" 33 #include "amdgpu_ras.h" 34 #include "amdgpu_atomfirmware.h" 35 #include "amdgpu_xgmi.h" 36 #include "ivsrcid/nbio/irqsrcs_nbif_7_4.h" 37 #include "atom.h" 38 39 static const char *RAS_FS_NAME = "ras"; 40 41 const char *ras_error_string[] = { 42 "none", 43 "parity", 44 "single_correctable", 45 "multi_uncorrectable", 46 "poison", 47 }; 48 49 const char *ras_block_string[] = { 50 "umc", 51 "sdma", 52 "gfx", 53 "mmhub", 54 "athub", 55 "pcie_bif", 56 "hdp", 57 "xgmi_wafl", 58 "df", 59 "smn", 60 "sem", 61 "mp0", 62 "mp1", 63 "fuse", 64 }; 65 66 #define ras_err_str(i) (ras_error_string[ffs(i)]) 67 68 #define RAS_DEFAULT_FLAGS (AMDGPU_RAS_FLAG_INIT_BY_VBIOS) 69 70 /* inject address is 52 bits */ 71 #define RAS_UMC_INJECT_ADDR_LIMIT (0x1ULL << 52) 72 73 /* typical ECC bad page rate is 1 bad page per 100MB VRAM */ 74 #define RAS_BAD_PAGE_COVER (100 * 1024 * 1024ULL) 75 76 enum amdgpu_ras_retire_page_reservation { 77 AMDGPU_RAS_RETIRE_PAGE_RESERVED, 78 AMDGPU_RAS_RETIRE_PAGE_PENDING, 79 AMDGPU_RAS_RETIRE_PAGE_FAULT, 80 }; 81 82 atomic_t amdgpu_ras_in_intr = ATOMIC_INIT(0); 83 84 static bool amdgpu_ras_check_bad_page_unlock(struct amdgpu_ras *con, 85 uint64_t addr); 86 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev, 87 uint64_t addr); 88 89 void amdgpu_ras_set_error_query_ready(struct amdgpu_device *adev, bool ready) 90 { 91 if (adev && amdgpu_ras_get_context(adev)) 92 amdgpu_ras_get_context(adev)->error_query_ready = ready; 93 } 94 95 static bool amdgpu_ras_get_error_query_ready(struct amdgpu_device *adev) 96 { 97 if (adev && amdgpu_ras_get_context(adev)) 98 return amdgpu_ras_get_context(adev)->error_query_ready; 99 100 return false; 101 } 102 103 static int amdgpu_reserve_page_direct(struct amdgpu_device *adev, uint64_t address) 104 { 105 struct ras_err_data err_data = {0, 0, 0, NULL}; 106 struct eeprom_table_record err_rec; 107 108 if ((address >= adev->gmc.mc_vram_size) || 109 (address >= RAS_UMC_INJECT_ADDR_LIMIT)) { 110 dev_warn(adev->dev, 111 "RAS WARN: input address 0x%llx is invalid.\n", 112 address); 113 return -EINVAL; 114 } 115 116 if (amdgpu_ras_check_bad_page(adev, address)) { 117 dev_warn(adev->dev, 118 "RAS WARN: 0x%llx has already been marked as bad page!\n", 119 address); 120 return 0; 121 } 122 123 memset(&err_rec, 0x0, sizeof(struct eeprom_table_record)); 124 125 err_rec.address = address; 126 err_rec.retired_page = address >> AMDGPU_GPU_PAGE_SHIFT; 127 err_rec.ts = (uint64_t)ktime_get_real_seconds(); 128 err_rec.err_type = AMDGPU_RAS_EEPROM_ERR_NON_RECOVERABLE; 129 130 err_data.err_addr = &err_rec; 131 err_data.err_addr_cnt = 1; 132 133 if (amdgpu_bad_page_threshold != 0) { 134 amdgpu_ras_add_bad_pages(adev, err_data.err_addr, 135 err_data.err_addr_cnt); 136 amdgpu_ras_save_bad_pages(adev); 137 } 138 139 dev_warn(adev->dev, "WARNING: THIS IS ONLY FOR TEST PURPOSES AND WILL CORRUPT RAS EEPROM\n"); 140 dev_warn(adev->dev, "Clear EEPROM:\n"); 141 dev_warn(adev->dev, " echo 1 > /sys/kernel/debug/dri/0/ras/ras_eeprom_reset\n"); 142 143 return 0; 144 } 145 146 #ifdef __linux__ 147 148 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf, 149 size_t size, loff_t *pos) 150 { 151 struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private; 152 struct ras_query_if info = { 153 .head = obj->head, 154 }; 155 ssize_t s; 156 char val[128]; 157 158 if (amdgpu_ras_query_error_status(obj->adev, &info)) 159 return -EINVAL; 160 161 s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n", 162 "ue", info.ue_count, 163 "ce", info.ce_count); 164 if (*pos >= s) 165 return 0; 166 167 s -= *pos; 168 s = min_t(u64, s, size); 169 170 171 if (copy_to_user(buf, &val[*pos], s)) 172 return -EINVAL; 173 174 *pos += s; 175 176 return s; 177 } 178 179 static const struct file_operations amdgpu_ras_debugfs_ops = { 180 .owner = THIS_MODULE, 181 .read = amdgpu_ras_debugfs_read, 182 .write = NULL, 183 .llseek = default_llseek 184 }; 185 186 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id) 187 { 188 int i; 189 190 for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) { 191 *block_id = i; 192 if (strcmp(name, ras_block_str(i)) == 0) 193 return 0; 194 } 195 return -EINVAL; 196 } 197 198 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f, 199 const char __user *buf, size_t size, 200 loff_t *pos, struct ras_debug_if *data) 201 { 202 ssize_t s = min_t(u64, 64, size); 203 char str[65]; 204 char block_name[33]; 205 char err[9] = "ue"; 206 int op = -1; 207 int block_id; 208 uint32_t sub_block; 209 u64 address, value; 210 211 if (*pos) 212 return -EINVAL; 213 *pos = size; 214 215 memset(str, 0, sizeof(str)); 216 memset(data, 0, sizeof(*data)); 217 218 if (copy_from_user(str, buf, s)) 219 return -EINVAL; 220 221 if (sscanf(str, "disable %32s", block_name) == 1) 222 op = 0; 223 else if (sscanf(str, "enable %32s %8s", block_name, err) == 2) 224 op = 1; 225 else if (sscanf(str, "inject %32s %8s", block_name, err) == 2) 226 op = 2; 227 else if (strstr(str, "retire_page") != NULL) 228 op = 3; 229 else if (str[0] && str[1] && str[2] && str[3]) 230 /* ascii string, but commands are not matched. */ 231 return -EINVAL; 232 233 if (op != -1) { 234 if (op == 3) { 235 if (sscanf(str, "%*s 0x%llx", &address) != 1 && 236 sscanf(str, "%*s %llu", &address) != 1) 237 return -EINVAL; 238 239 data->op = op; 240 data->inject.address = address; 241 242 return 0; 243 } 244 245 if (amdgpu_ras_find_block_id_by_name(block_name, &block_id)) 246 return -EINVAL; 247 248 data->head.block = block_id; 249 /* only ue and ce errors are supported */ 250 if (!memcmp("ue", err, 2)) 251 data->head.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE; 252 else if (!memcmp("ce", err, 2)) 253 data->head.type = AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE; 254 else 255 return -EINVAL; 256 257 data->op = op; 258 259 if (op == 2) { 260 if (sscanf(str, "%*s %*s %*s 0x%x 0x%llx 0x%llx", 261 &sub_block, &address, &value) != 3 && 262 sscanf(str, "%*s %*s %*s %u %llu %llu", 263 &sub_block, &address, &value) != 3) 264 return -EINVAL; 265 data->head.sub_block_index = sub_block; 266 data->inject.address = address; 267 data->inject.value = value; 268 } 269 } else { 270 if (size < sizeof(*data)) 271 return -EINVAL; 272 273 if (copy_from_user(data, buf, sizeof(*data))) 274 return -EINVAL; 275 } 276 277 return 0; 278 } 279 280 /** 281 * DOC: AMDGPU RAS debugfs control interface 282 * 283 * The control interface accepts struct ras_debug_if which has two members. 284 * 285 * First member: ras_debug_if::head or ras_debug_if::inject. 286 * 287 * head is used to indicate which IP block will be under control. 288 * 289 * head has four members, they are block, type, sub_block_index, name. 290 * block: which IP will be under control. 291 * type: what kind of error will be enabled/disabled/injected. 292 * sub_block_index: some IPs have subcomponets. say, GFX, sDMA. 293 * name: the name of IP. 294 * 295 * inject has two more members than head, they are address, value. 296 * As their names indicate, inject operation will write the 297 * value to the address. 298 * 299 * The second member: struct ras_debug_if::op. 300 * It has three kinds of operations. 301 * 302 * - 0: disable RAS on the block. Take ::head as its data. 303 * - 1: enable RAS on the block. Take ::head as its data. 304 * - 2: inject errors on the block. Take ::inject as its data. 305 * 306 * How to use the interface? 307 * 308 * In a program 309 * 310 * Copy the struct ras_debug_if in your code and initialize it. 311 * Write the struct to the control interface. 312 * 313 * From shell 314 * 315 * .. code-block:: bash 316 * 317 * echo "disable <block>" > /sys/kernel/debug/dri/<N>/ras/ras_ctrl 318 * echo "enable <block> <error>" > /sys/kernel/debug/dri/<N>/ras/ras_ctrl 319 * echo "inject <block> <error> <sub-block> <address> <value> > /sys/kernel/debug/dri/<N>/ras/ras_ctrl 320 * 321 * Where N, is the card which you want to affect. 322 * 323 * "disable" requires only the block. 324 * "enable" requires the block and error type. 325 * "inject" requires the block, error type, address, and value. 326 * 327 * The block is one of: umc, sdma, gfx, etc. 328 * see ras_block_string[] for details 329 * 330 * The error type is one of: ue, ce, where, 331 * ue is multi-uncorrectable 332 * ce is single-correctable 333 * 334 * The sub-block is a the sub-block index, pass 0 if there is no sub-block. 335 * The address and value are hexadecimal numbers, leading 0x is optional. 336 * 337 * For instance, 338 * 339 * .. code-block:: bash 340 * 341 * echo inject umc ue 0x0 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl 342 * echo inject umc ce 0 0 0 > /sys/kernel/debug/dri/0/ras/ras_ctrl 343 * echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl 344 * 345 * How to check the result of the operation? 346 * 347 * To check disable/enable, see "ras" features at, 348 * /sys/class/drm/card[0/1/2...]/device/ras/features 349 * 350 * To check inject, see the corresponding error count at, 351 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx|sdma|umc|...]_err_count 352 * 353 * .. note:: 354 * Operations are only allowed on blocks which are supported. 355 * Check the "ras" mask at /sys/module/amdgpu/parameters/ras_mask 356 * to see which blocks support RAS on a particular asic. 357 * 358 */ 359 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f, 360 const char __user *buf, 361 size_t size, loff_t *pos) 362 { 363 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private; 364 struct ras_debug_if data; 365 int ret = 0; 366 367 if (!amdgpu_ras_get_error_query_ready(adev)) { 368 dev_warn(adev->dev, "RAS WARN: error injection " 369 "currently inaccessible\n"); 370 return size; 371 } 372 373 ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data); 374 if (ret) 375 return ret; 376 377 if (data.op == 3) { 378 ret = amdgpu_reserve_page_direct(adev, data.inject.address); 379 if (!ret) 380 return size; 381 else 382 return ret; 383 } 384 385 if (!amdgpu_ras_is_supported(adev, data.head.block)) 386 return -EINVAL; 387 388 switch (data.op) { 389 case 0: 390 ret = amdgpu_ras_feature_enable(adev, &data.head, 0); 391 break; 392 case 1: 393 ret = amdgpu_ras_feature_enable(adev, &data.head, 1); 394 break; 395 case 2: 396 if ((data.inject.address >= adev->gmc.mc_vram_size) || 397 (data.inject.address >= RAS_UMC_INJECT_ADDR_LIMIT)) { 398 dev_warn(adev->dev, "RAS WARN: input address " 399 "0x%llx is invalid.", 400 data.inject.address); 401 ret = -EINVAL; 402 break; 403 } 404 405 /* umc ce/ue error injection for a bad page is not allowed */ 406 if ((data.head.block == AMDGPU_RAS_BLOCK__UMC) && 407 amdgpu_ras_check_bad_page(adev, data.inject.address)) { 408 dev_warn(adev->dev, "RAS WARN: inject: 0x%llx has " 409 "already been marked as bad!\n", 410 data.inject.address); 411 break; 412 } 413 414 /* data.inject.address is offset instead of absolute gpu address */ 415 ret = amdgpu_ras_error_inject(adev, &data.inject); 416 break; 417 default: 418 ret = -EINVAL; 419 break; 420 } 421 422 if (ret) 423 return -EINVAL; 424 425 return size; 426 } 427 428 /** 429 * DOC: AMDGPU RAS debugfs EEPROM table reset interface 430 * 431 * Some boards contain an EEPROM which is used to persistently store a list of 432 * bad pages which experiences ECC errors in vram. This interface provides 433 * a way to reset the EEPROM, e.g., after testing error injection. 434 * 435 * Usage: 436 * 437 * .. code-block:: bash 438 * 439 * echo 1 > ../ras/ras_eeprom_reset 440 * 441 * will reset EEPROM table to 0 entries. 442 * 443 */ 444 static ssize_t amdgpu_ras_debugfs_eeprom_write(struct file *f, 445 const char __user *buf, 446 size_t size, loff_t *pos) 447 { 448 struct amdgpu_device *adev = 449 (struct amdgpu_device *)file_inode(f)->i_private; 450 int ret; 451 452 ret = amdgpu_ras_eeprom_reset_table( 453 &(amdgpu_ras_get_context(adev)->eeprom_control)); 454 455 if (!ret) { 456 /* Something was written to EEPROM. 457 */ 458 amdgpu_ras_get_context(adev)->flags = RAS_DEFAULT_FLAGS; 459 return size; 460 } else { 461 return ret; 462 } 463 } 464 465 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = { 466 .owner = THIS_MODULE, 467 .read = NULL, 468 .write = amdgpu_ras_debugfs_ctrl_write, 469 .llseek = default_llseek 470 }; 471 472 static const struct file_operations amdgpu_ras_debugfs_eeprom_ops = { 473 .owner = THIS_MODULE, 474 .read = NULL, 475 .write = amdgpu_ras_debugfs_eeprom_write, 476 .llseek = default_llseek 477 }; 478 479 /** 480 * DOC: AMDGPU RAS sysfs Error Count Interface 481 * 482 * It allows the user to read the error count for each IP block on the gpu through 483 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count 484 * 485 * It outputs the multiple lines which report the uncorrected (ue) and corrected 486 * (ce) error counts. 487 * 488 * The format of one line is below, 489 * 490 * [ce|ue]: count 491 * 492 * Example: 493 * 494 * .. code-block:: bash 495 * 496 * ue: 0 497 * ce: 1 498 * 499 */ 500 static ssize_t amdgpu_ras_sysfs_read(struct device *dev, 501 struct device_attribute *attr, char *buf) 502 { 503 struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr); 504 struct ras_query_if info = { 505 .head = obj->head, 506 }; 507 508 if (!amdgpu_ras_get_error_query_ready(obj->adev)) 509 return sysfs_emit(buf, "Query currently inaccessible\n"); 510 511 if (amdgpu_ras_query_error_status(obj->adev, &info)) 512 return -EINVAL; 513 514 515 if (obj->adev->asic_type == CHIP_ALDEBARAN) { 516 if (amdgpu_ras_reset_error_status(obj->adev, info.head.block)) 517 DRM_WARN("Failed to reset error counter and error status"); 518 } 519 520 return sysfs_emit(buf, "%s: %lu\n%s: %lu\n", "ue", info.ue_count, 521 "ce", info.ce_count); 522 } 523 524 #endif /* __linux__ */ 525 526 /* obj begin */ 527 528 #define get_obj(obj) do { (obj)->use++; } while (0) 529 #define alive_obj(obj) ((obj)->use) 530 531 static inline void put_obj(struct ras_manager *obj) 532 { 533 if (obj && (--obj->use == 0)) 534 list_del(&obj->node); 535 if (obj && (obj->use < 0)) 536 DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", ras_block_str(obj->head.block)); 537 } 538 539 /* make one obj and return it. */ 540 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev, 541 struct ras_common_if *head) 542 { 543 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 544 struct ras_manager *obj; 545 546 if (!adev->ras_enabled || !con) 547 return NULL; 548 549 if (head->block >= AMDGPU_RAS_BLOCK_COUNT) 550 return NULL; 551 552 obj = &con->objs[head->block]; 553 /* already exist. return obj? */ 554 if (alive_obj(obj)) 555 return NULL; 556 557 obj->head = *head; 558 obj->adev = adev; 559 list_add(&obj->node, &con->head); 560 get_obj(obj); 561 562 return obj; 563 } 564 565 /* return an obj equal to head, or the first when head is NULL */ 566 struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev, 567 struct ras_common_if *head) 568 { 569 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 570 struct ras_manager *obj; 571 int i; 572 573 if (!adev->ras_enabled || !con) 574 return NULL; 575 576 if (head) { 577 if (head->block >= AMDGPU_RAS_BLOCK_COUNT) 578 return NULL; 579 580 obj = &con->objs[head->block]; 581 582 if (alive_obj(obj)) { 583 WARN_ON(head->block != obj->head.block); 584 return obj; 585 } 586 } else { 587 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) { 588 obj = &con->objs[i]; 589 if (alive_obj(obj)) { 590 WARN_ON(i != obj->head.block); 591 return obj; 592 } 593 } 594 } 595 596 return NULL; 597 } 598 /* obj end */ 599 600 /* feature ctl begin */ 601 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev, 602 struct ras_common_if *head) 603 { 604 return adev->ras_hw_enabled & BIT(head->block); 605 } 606 607 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev, 608 struct ras_common_if *head) 609 { 610 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 611 612 return con->features & BIT(head->block); 613 } 614 615 /* 616 * if obj is not created, then create one. 617 * set feature enable flag. 618 */ 619 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev, 620 struct ras_common_if *head, int enable) 621 { 622 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 623 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head); 624 625 /* If hardware does not support ras, then do not create obj. 626 * But if hardware support ras, we can create the obj. 627 * Ras framework checks con->hw_supported to see if it need do 628 * corresponding initialization. 629 * IP checks con->support to see if it need disable ras. 630 */ 631 if (!amdgpu_ras_is_feature_allowed(adev, head)) 632 return 0; 633 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head))) 634 return 0; 635 636 if (enable) { 637 if (!obj) { 638 obj = amdgpu_ras_create_obj(adev, head); 639 if (!obj) 640 return -EINVAL; 641 } else { 642 /* In case we create obj somewhere else */ 643 get_obj(obj); 644 } 645 con->features |= BIT(head->block); 646 } else { 647 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) { 648 con->features &= ~BIT(head->block); 649 put_obj(obj); 650 } 651 } 652 653 return 0; 654 } 655 656 /* wrapper of psp_ras_enable_features */ 657 int amdgpu_ras_feature_enable(struct amdgpu_device *adev, 658 struct ras_common_if *head, bool enable) 659 { 660 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 661 union ta_ras_cmd_input *info; 662 int ret; 663 664 if (!con) 665 return -EINVAL; 666 667 info = kzalloc(sizeof(union ta_ras_cmd_input), GFP_KERNEL); 668 if (!info) 669 return -ENOMEM; 670 671 if (!enable) { 672 info->disable_features = (struct ta_ras_disable_features_input) { 673 .block_id = amdgpu_ras_block_to_ta(head->block), 674 .error_type = amdgpu_ras_error_to_ta(head->type), 675 }; 676 } else { 677 info->enable_features = (struct ta_ras_enable_features_input) { 678 .block_id = amdgpu_ras_block_to_ta(head->block), 679 .error_type = amdgpu_ras_error_to_ta(head->type), 680 }; 681 } 682 683 /* Do not enable if it is not allowed. */ 684 WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head)); 685 /* Are we alerady in that state we are going to set? */ 686 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head))) { 687 ret = 0; 688 goto out; 689 } 690 691 if (!amdgpu_ras_intr_triggered()) { 692 ret = psp_ras_enable_features(&adev->psp, info, enable); 693 if (ret) { 694 dev_err(adev->dev, "ras %s %s failed %d\n", 695 enable ? "enable":"disable", 696 ras_block_str(head->block), 697 ret); 698 goto out; 699 } 700 } 701 702 /* setup the obj */ 703 __amdgpu_ras_feature_enable(adev, head, enable); 704 ret = 0; 705 out: 706 kfree(info); 707 return ret; 708 } 709 710 /* Only used in device probe stage and called only once. */ 711 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev, 712 struct ras_common_if *head, bool enable) 713 { 714 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 715 int ret; 716 717 if (!con) 718 return -EINVAL; 719 720 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) { 721 if (enable) { 722 /* There is no harm to issue a ras TA cmd regardless of 723 * the currecnt ras state. 724 * If current state == target state, it will do nothing 725 * But sometimes it requests driver to reset and repost 726 * with error code -EAGAIN. 727 */ 728 ret = amdgpu_ras_feature_enable(adev, head, 1); 729 /* With old ras TA, we might fail to enable ras. 730 * Log it and just setup the object. 731 * TODO need remove this WA in the future. 732 */ 733 if (ret == -EINVAL) { 734 ret = __amdgpu_ras_feature_enable(adev, head, 1); 735 if (!ret) 736 dev_info(adev->dev, 737 "RAS INFO: %s setup object\n", 738 ras_block_str(head->block)); 739 } 740 } else { 741 /* setup the object then issue a ras TA disable cmd.*/ 742 ret = __amdgpu_ras_feature_enable(adev, head, 1); 743 if (ret) 744 return ret; 745 746 /* gfx block ras dsiable cmd must send to ras-ta */ 747 if (head->block == AMDGPU_RAS_BLOCK__GFX) 748 con->features |= BIT(head->block); 749 750 ret = amdgpu_ras_feature_enable(adev, head, 0); 751 752 /* clean gfx block ras features flag */ 753 if (adev->ras_enabled && head->block == AMDGPU_RAS_BLOCK__GFX) 754 con->features &= ~BIT(head->block); 755 } 756 } else 757 ret = amdgpu_ras_feature_enable(adev, head, enable); 758 759 return ret; 760 } 761 762 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev, 763 bool bypass) 764 { 765 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 766 struct ras_manager *obj, *tmp; 767 768 list_for_each_entry_safe(obj, tmp, &con->head, node) { 769 /* bypass psp. 770 * aka just release the obj and corresponding flags 771 */ 772 if (bypass) { 773 if (__amdgpu_ras_feature_enable(adev, &obj->head, 0)) 774 break; 775 } else { 776 if (amdgpu_ras_feature_enable(adev, &obj->head, 0)) 777 break; 778 } 779 } 780 781 return con->features; 782 } 783 784 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev, 785 bool bypass) 786 { 787 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 788 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT; 789 int i; 790 const enum amdgpu_ras_error_type default_ras_type = 791 AMDGPU_RAS_ERROR__NONE; 792 793 for (i = 0; i < ras_block_count; i++) { 794 struct ras_common_if head = { 795 .block = i, 796 .type = default_ras_type, 797 .sub_block_index = 0, 798 }; 799 if (bypass) { 800 /* 801 * bypass psp. vbios enable ras for us. 802 * so just create the obj 803 */ 804 if (__amdgpu_ras_feature_enable(adev, &head, 1)) 805 break; 806 } else { 807 if (amdgpu_ras_feature_enable(adev, &head, 1)) 808 break; 809 } 810 } 811 812 return con->features; 813 } 814 /* feature ctl end */ 815 816 /* query/inject/cure begin */ 817 int amdgpu_ras_query_error_status(struct amdgpu_device *adev, 818 struct ras_query_if *info) 819 { 820 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head); 821 struct ras_err_data err_data = {0, 0, 0, NULL}; 822 int i; 823 824 if (!obj) 825 return -EINVAL; 826 827 switch (info->head.block) { 828 case AMDGPU_RAS_BLOCK__UMC: 829 if (adev->umc.ras_funcs && 830 adev->umc.ras_funcs->query_ras_error_count) 831 adev->umc.ras_funcs->query_ras_error_count(adev, &err_data); 832 /* umc query_ras_error_address is also responsible for clearing 833 * error status 834 */ 835 if (adev->umc.ras_funcs && 836 adev->umc.ras_funcs->query_ras_error_address) 837 adev->umc.ras_funcs->query_ras_error_address(adev, &err_data); 838 break; 839 case AMDGPU_RAS_BLOCK__SDMA: 840 if (adev->sdma.funcs->query_ras_error_count) { 841 for (i = 0; i < adev->sdma.num_instances; i++) 842 adev->sdma.funcs->query_ras_error_count(adev, i, 843 &err_data); 844 } 845 break; 846 case AMDGPU_RAS_BLOCK__GFX: 847 if (adev->gfx.ras_funcs && 848 adev->gfx.ras_funcs->query_ras_error_count) 849 adev->gfx.ras_funcs->query_ras_error_count(adev, &err_data); 850 851 if (adev->gfx.ras_funcs && 852 adev->gfx.ras_funcs->query_ras_error_status) 853 adev->gfx.ras_funcs->query_ras_error_status(adev); 854 break; 855 case AMDGPU_RAS_BLOCK__MMHUB: 856 if (adev->mmhub.ras_funcs && 857 adev->mmhub.ras_funcs->query_ras_error_count) 858 adev->mmhub.ras_funcs->query_ras_error_count(adev, &err_data); 859 860 if (adev->mmhub.ras_funcs && 861 adev->mmhub.ras_funcs->query_ras_error_status) 862 adev->mmhub.ras_funcs->query_ras_error_status(adev); 863 break; 864 case AMDGPU_RAS_BLOCK__PCIE_BIF: 865 if (adev->nbio.ras_funcs && 866 adev->nbio.ras_funcs->query_ras_error_count) 867 adev->nbio.ras_funcs->query_ras_error_count(adev, &err_data); 868 break; 869 case AMDGPU_RAS_BLOCK__XGMI_WAFL: 870 if (adev->gmc.xgmi.ras_funcs && 871 adev->gmc.xgmi.ras_funcs->query_ras_error_count) 872 adev->gmc.xgmi.ras_funcs->query_ras_error_count(adev, &err_data); 873 break; 874 case AMDGPU_RAS_BLOCK__HDP: 875 if (adev->hdp.ras_funcs && 876 adev->hdp.ras_funcs->query_ras_error_count) 877 adev->hdp.ras_funcs->query_ras_error_count(adev, &err_data); 878 break; 879 default: 880 break; 881 } 882 883 obj->err_data.ue_count += err_data.ue_count; 884 obj->err_data.ce_count += err_data.ce_count; 885 886 info->ue_count = obj->err_data.ue_count; 887 info->ce_count = obj->err_data.ce_count; 888 889 if (err_data.ce_count) { 890 if (adev->smuio.funcs && 891 adev->smuio.funcs->get_socket_id && 892 adev->smuio.funcs->get_die_id) { 893 dev_info(adev->dev, "socket: %d, die: %d " 894 "%ld correctable hardware errors " 895 "detected in %s block, no user " 896 "action is needed.\n", 897 adev->smuio.funcs->get_socket_id(adev), 898 adev->smuio.funcs->get_die_id(adev), 899 obj->err_data.ce_count, 900 ras_block_str(info->head.block)); 901 } else { 902 dev_info(adev->dev, "%ld correctable hardware errors " 903 "detected in %s block, no user " 904 "action is needed.\n", 905 obj->err_data.ce_count, 906 ras_block_str(info->head.block)); 907 } 908 } 909 if (err_data.ue_count) { 910 if (adev->smuio.funcs && 911 adev->smuio.funcs->get_socket_id && 912 adev->smuio.funcs->get_die_id) { 913 dev_info(adev->dev, "socket: %d, die: %d " 914 "%ld uncorrectable hardware errors " 915 "detected in %s block\n", 916 adev->smuio.funcs->get_socket_id(adev), 917 adev->smuio.funcs->get_die_id(adev), 918 obj->err_data.ue_count, 919 ras_block_str(info->head.block)); 920 } else { 921 dev_info(adev->dev, "%ld uncorrectable hardware errors " 922 "detected in %s block\n", 923 obj->err_data.ue_count, 924 ras_block_str(info->head.block)); 925 } 926 } 927 928 return 0; 929 } 930 931 int amdgpu_ras_reset_error_status(struct amdgpu_device *adev, 932 enum amdgpu_ras_block block) 933 { 934 if (!amdgpu_ras_is_supported(adev, block)) 935 return -EINVAL; 936 937 switch (block) { 938 case AMDGPU_RAS_BLOCK__GFX: 939 if (adev->gfx.ras_funcs && 940 adev->gfx.ras_funcs->reset_ras_error_count) 941 adev->gfx.ras_funcs->reset_ras_error_count(adev); 942 943 if (adev->gfx.ras_funcs && 944 adev->gfx.ras_funcs->reset_ras_error_status) 945 adev->gfx.ras_funcs->reset_ras_error_status(adev); 946 break; 947 case AMDGPU_RAS_BLOCK__MMHUB: 948 if (adev->mmhub.ras_funcs && 949 adev->mmhub.ras_funcs->reset_ras_error_count) 950 adev->mmhub.ras_funcs->reset_ras_error_count(adev); 951 952 if (adev->mmhub.ras_funcs && 953 adev->mmhub.ras_funcs->reset_ras_error_status) 954 adev->mmhub.ras_funcs->reset_ras_error_status(adev); 955 break; 956 case AMDGPU_RAS_BLOCK__SDMA: 957 if (adev->sdma.funcs->reset_ras_error_count) 958 adev->sdma.funcs->reset_ras_error_count(adev); 959 break; 960 case AMDGPU_RAS_BLOCK__HDP: 961 if (adev->hdp.ras_funcs && 962 adev->hdp.ras_funcs->reset_ras_error_count) 963 adev->hdp.ras_funcs->reset_ras_error_count(adev); 964 break; 965 default: 966 break; 967 } 968 969 return 0; 970 } 971 972 /* Trigger XGMI/WAFL error */ 973 static int amdgpu_ras_error_inject_xgmi(struct amdgpu_device *adev, 974 struct ta_ras_trigger_error_input *block_info) 975 { 976 int ret; 977 978 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_DISALLOW)) 979 dev_warn(adev->dev, "Failed to disallow df cstate"); 980 981 if (amdgpu_dpm_allow_xgmi_power_down(adev, false)) 982 dev_warn(adev->dev, "Failed to disallow XGMI power down"); 983 984 ret = psp_ras_trigger_error(&adev->psp, block_info); 985 986 if (amdgpu_ras_intr_triggered()) 987 return ret; 988 989 if (amdgpu_dpm_allow_xgmi_power_down(adev, true)) 990 dev_warn(adev->dev, "Failed to allow XGMI power down"); 991 992 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_ALLOW)) 993 dev_warn(adev->dev, "Failed to allow df cstate"); 994 995 return ret; 996 } 997 998 /* wrapper of psp_ras_trigger_error */ 999 int amdgpu_ras_error_inject(struct amdgpu_device *adev, 1000 struct ras_inject_if *info) 1001 { 1002 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head); 1003 struct ta_ras_trigger_error_input block_info = { 1004 .block_id = amdgpu_ras_block_to_ta(info->head.block), 1005 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type), 1006 .sub_block_index = info->head.sub_block_index, 1007 .address = info->address, 1008 .value = info->value, 1009 }; 1010 int ret = 0; 1011 1012 if (!obj) 1013 return -EINVAL; 1014 1015 /* Calculate XGMI relative offset */ 1016 if (adev->gmc.xgmi.num_physical_nodes > 1) { 1017 block_info.address = 1018 amdgpu_xgmi_get_relative_phy_addr(adev, 1019 block_info.address); 1020 } 1021 1022 switch (info->head.block) { 1023 case AMDGPU_RAS_BLOCK__GFX: 1024 if (adev->gfx.ras_funcs && 1025 adev->gfx.ras_funcs->ras_error_inject) 1026 ret = adev->gfx.ras_funcs->ras_error_inject(adev, info); 1027 else 1028 ret = -EINVAL; 1029 break; 1030 case AMDGPU_RAS_BLOCK__UMC: 1031 case AMDGPU_RAS_BLOCK__SDMA: 1032 case AMDGPU_RAS_BLOCK__MMHUB: 1033 case AMDGPU_RAS_BLOCK__PCIE_BIF: 1034 ret = psp_ras_trigger_error(&adev->psp, &block_info); 1035 break; 1036 case AMDGPU_RAS_BLOCK__XGMI_WAFL: 1037 ret = amdgpu_ras_error_inject_xgmi(adev, &block_info); 1038 break; 1039 default: 1040 dev_info(adev->dev, "%s error injection is not supported yet\n", 1041 ras_block_str(info->head.block)); 1042 ret = -EINVAL; 1043 } 1044 1045 if (ret) 1046 dev_err(adev->dev, "ras inject %s failed %d\n", 1047 ras_block_str(info->head.block), ret); 1048 1049 return ret; 1050 } 1051 1052 /** 1053 * amdgpu_ras_query_error_count -- Get error counts of all IPs 1054 * adev: pointer to AMD GPU device 1055 * ce_count: pointer to an integer to be set to the count of correctible errors. 1056 * ue_count: pointer to an integer to be set to the count of uncorrectible 1057 * errors. 1058 * 1059 * If set, @ce_count or @ue_count, count and return the corresponding 1060 * error counts in those integer pointers. Return 0 if the device 1061 * supports RAS. Return -EOPNOTSUPP if the device doesn't support RAS. 1062 */ 1063 int amdgpu_ras_query_error_count(struct amdgpu_device *adev, 1064 unsigned long *ce_count, 1065 unsigned long *ue_count) 1066 { 1067 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1068 struct ras_manager *obj; 1069 unsigned long ce, ue; 1070 1071 if (!adev->ras_enabled || !con) 1072 return -EOPNOTSUPP; 1073 1074 /* Don't count since no reporting. 1075 */ 1076 if (!ce_count && !ue_count) 1077 return 0; 1078 1079 ce = 0; 1080 ue = 0; 1081 list_for_each_entry(obj, &con->head, node) { 1082 struct ras_query_if info = { 1083 .head = obj->head, 1084 }; 1085 int res; 1086 1087 res = amdgpu_ras_query_error_status(adev, &info); 1088 if (res) 1089 return res; 1090 1091 ce += info.ce_count; 1092 ue += info.ue_count; 1093 } 1094 1095 if (ce_count) 1096 *ce_count = ce; 1097 1098 if (ue_count) 1099 *ue_count = ue; 1100 1101 return 0; 1102 } 1103 /* query/inject/cure end */ 1104 1105 #ifdef __linux__ 1106 1107 /* sysfs begin */ 1108 1109 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev, 1110 struct ras_badpage **bps, unsigned int *count); 1111 1112 static char *amdgpu_ras_badpage_flags_str(unsigned int flags) 1113 { 1114 switch (flags) { 1115 case AMDGPU_RAS_RETIRE_PAGE_RESERVED: 1116 return "R"; 1117 case AMDGPU_RAS_RETIRE_PAGE_PENDING: 1118 return "P"; 1119 case AMDGPU_RAS_RETIRE_PAGE_FAULT: 1120 default: 1121 return "F"; 1122 } 1123 } 1124 1125 /** 1126 * DOC: AMDGPU RAS sysfs gpu_vram_bad_pages Interface 1127 * 1128 * It allows user to read the bad pages of vram on the gpu through 1129 * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages 1130 * 1131 * It outputs multiple lines, and each line stands for one gpu page. 1132 * 1133 * The format of one line is below, 1134 * gpu pfn : gpu page size : flags 1135 * 1136 * gpu pfn and gpu page size are printed in hex format. 1137 * flags can be one of below character, 1138 * 1139 * R: reserved, this gpu page is reserved and not able to use. 1140 * 1141 * P: pending for reserve, this gpu page is marked as bad, will be reserved 1142 * in next window of page_reserve. 1143 * 1144 * F: unable to reserve. this gpu page can't be reserved due to some reasons. 1145 * 1146 * Examples: 1147 * 1148 * .. code-block:: bash 1149 * 1150 * 0x00000001 : 0x00001000 : R 1151 * 0x00000002 : 0x00001000 : P 1152 * 1153 */ 1154 1155 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f, 1156 struct kobject *kobj, struct bin_attribute *attr, 1157 char *buf, loff_t ppos, size_t count) 1158 { 1159 struct amdgpu_ras *con = 1160 container_of(attr, struct amdgpu_ras, badpages_attr); 1161 struct amdgpu_device *adev = con->adev; 1162 const unsigned int element_size = 1163 sizeof("0xabcdabcd : 0x12345678 : R\n") - 1; 1164 unsigned int start = div64_ul(ppos + element_size - 1, element_size); 1165 unsigned int end = div64_ul(ppos + count - 1, element_size); 1166 ssize_t s = 0; 1167 struct ras_badpage *bps = NULL; 1168 unsigned int bps_count = 0; 1169 1170 memset(buf, 0, count); 1171 1172 if (amdgpu_ras_badpages_read(adev, &bps, &bps_count)) 1173 return 0; 1174 1175 for (; start < end && start < bps_count; start++) 1176 s += scnprintf(&buf[s], element_size + 1, 1177 "0x%08x : 0x%08x : %1s\n", 1178 bps[start].bp, 1179 bps[start].size, 1180 amdgpu_ras_badpage_flags_str(bps[start].flags)); 1181 1182 kfree(bps); 1183 1184 return s; 1185 } 1186 1187 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev, 1188 struct device_attribute *attr, char *buf) 1189 { 1190 struct amdgpu_ras *con = 1191 container_of(attr, struct amdgpu_ras, features_attr); 1192 1193 return scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features); 1194 } 1195 1196 static void amdgpu_ras_sysfs_remove_bad_page_node(struct amdgpu_device *adev) 1197 { 1198 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1199 1200 sysfs_remove_file_from_group(&adev->dev->kobj, 1201 &con->badpages_attr.attr, 1202 RAS_FS_NAME); 1203 } 1204 1205 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev) 1206 { 1207 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1208 struct attribute *attrs[] = { 1209 &con->features_attr.attr, 1210 NULL 1211 }; 1212 struct attribute_group group = { 1213 .name = RAS_FS_NAME, 1214 .attrs = attrs, 1215 }; 1216 1217 sysfs_remove_group(&adev->dev->kobj, &group); 1218 1219 return 0; 1220 } 1221 1222 #endif /* __linux__ */ 1223 1224 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev, 1225 struct ras_fs_if *head) 1226 { 1227 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head); 1228 1229 if (!obj || obj->attr_inuse) 1230 return -EINVAL; 1231 1232 STUB(); 1233 return -ENOSYS; 1234 #ifdef notyet 1235 get_obj(obj); 1236 1237 memcpy(obj->fs_data.sysfs_name, 1238 head->sysfs_name, 1239 sizeof(obj->fs_data.sysfs_name)); 1240 1241 obj->sysfs_attr = (struct device_attribute){ 1242 .attr = { 1243 .name = obj->fs_data.sysfs_name, 1244 .mode = S_IRUGO, 1245 }, 1246 .show = amdgpu_ras_sysfs_read, 1247 }; 1248 sysfs_attr_init(&obj->sysfs_attr.attr); 1249 1250 if (sysfs_add_file_to_group(&adev->dev->kobj, 1251 &obj->sysfs_attr.attr, 1252 RAS_FS_NAME)) { 1253 put_obj(obj); 1254 return -EINVAL; 1255 } 1256 1257 obj->attr_inuse = 1; 1258 1259 return 0; 1260 #endif 1261 } 1262 1263 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev, 1264 struct ras_common_if *head) 1265 { 1266 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head); 1267 1268 if (!obj || !obj->attr_inuse) 1269 return -EINVAL; 1270 1271 sysfs_remove_file_from_group(&adev->dev->kobj, 1272 &obj->sysfs_attr.attr, 1273 RAS_FS_NAME); 1274 obj->attr_inuse = 0; 1275 put_obj(obj); 1276 1277 return 0; 1278 } 1279 1280 #ifdef __linux__ 1281 1282 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev) 1283 { 1284 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1285 struct ras_manager *obj, *tmp; 1286 1287 list_for_each_entry_safe(obj, tmp, &con->head, node) { 1288 amdgpu_ras_sysfs_remove(adev, &obj->head); 1289 } 1290 1291 if (amdgpu_bad_page_threshold != 0) 1292 amdgpu_ras_sysfs_remove_bad_page_node(adev); 1293 1294 amdgpu_ras_sysfs_remove_feature_node(adev); 1295 1296 return 0; 1297 } 1298 /* sysfs end */ 1299 1300 /** 1301 * DOC: AMDGPU RAS Reboot Behavior for Unrecoverable Errors 1302 * 1303 * Normally when there is an uncorrectable error, the driver will reset 1304 * the GPU to recover. However, in the event of an unrecoverable error, 1305 * the driver provides an interface to reboot the system automatically 1306 * in that event. 1307 * 1308 * The following file in debugfs provides that interface: 1309 * /sys/kernel/debug/dri/[0/1/2...]/ras/auto_reboot 1310 * 1311 * Usage: 1312 * 1313 * .. code-block:: bash 1314 * 1315 * echo true > .../ras/auto_reboot 1316 * 1317 */ 1318 /* debugfs begin */ 1319 static struct dentry *amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev) 1320 { 1321 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1322 struct drm_minor *minor = adev_to_drm(adev)->primary; 1323 struct dentry *dir; 1324 1325 dir = debugfs_create_dir(RAS_FS_NAME, minor->debugfs_root); 1326 debugfs_create_file("ras_ctrl", S_IWUGO | S_IRUGO, dir, adev, 1327 &amdgpu_ras_debugfs_ctrl_ops); 1328 debugfs_create_file("ras_eeprom_reset", S_IWUGO | S_IRUGO, dir, adev, 1329 &amdgpu_ras_debugfs_eeprom_ops); 1330 debugfs_create_u32("bad_page_cnt_threshold", 0444, dir, 1331 &con->bad_page_cnt_threshold); 1332 debugfs_create_x32("ras_hw_enabled", 0444, dir, &adev->ras_hw_enabled); 1333 debugfs_create_x32("ras_enabled", 0444, dir, &adev->ras_enabled); 1334 debugfs_create_file("ras_eeprom_size", S_IRUGO, dir, adev, 1335 &amdgpu_ras_debugfs_eeprom_size_ops); 1336 con->de_ras_eeprom_table = debugfs_create_file("ras_eeprom_table", 1337 S_IRUGO, dir, adev, 1338 &amdgpu_ras_debugfs_eeprom_table_ops); 1339 amdgpu_ras_debugfs_set_ret_size(&con->eeprom_control); 1340 1341 /* 1342 * After one uncorrectable error happens, usually GPU recovery will 1343 * be scheduled. But due to the known problem in GPU recovery failing 1344 * to bring GPU back, below interface provides one direct way to 1345 * user to reboot system automatically in such case within 1346 * ERREVENT_ATHUB_INTERRUPT generated. Normal GPU recovery routine 1347 * will never be called. 1348 */ 1349 debugfs_create_bool("auto_reboot", S_IWUGO | S_IRUGO, dir, &con->reboot); 1350 1351 /* 1352 * User could set this not to clean up hardware's error count register 1353 * of RAS IPs during ras recovery. 1354 */ 1355 debugfs_create_bool("disable_ras_err_cnt_harvest", 0644, dir, 1356 &con->disable_ras_err_cnt_harvest); 1357 return dir; 1358 } 1359 1360 static void amdgpu_ras_debugfs_create(struct amdgpu_device *adev, 1361 struct ras_fs_if *head, 1362 struct dentry *dir) 1363 { 1364 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head); 1365 1366 if (!obj || !dir) 1367 return; 1368 1369 get_obj(obj); 1370 1371 memcpy(obj->fs_data.debugfs_name, 1372 head->debugfs_name, 1373 sizeof(obj->fs_data.debugfs_name)); 1374 1375 debugfs_create_file(obj->fs_data.debugfs_name, S_IWUGO | S_IRUGO, dir, 1376 obj, &amdgpu_ras_debugfs_ops); 1377 } 1378 1379 void amdgpu_ras_debugfs_create_all(struct amdgpu_device *adev) 1380 { 1381 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1382 struct dentry *dir; 1383 struct ras_manager *obj; 1384 struct ras_fs_if fs_info; 1385 1386 /* 1387 * it won't be called in resume path, no need to check 1388 * suspend and gpu reset status 1389 */ 1390 if (!IS_ENABLED(CONFIG_DEBUG_FS) || !con) 1391 return; 1392 1393 dir = amdgpu_ras_debugfs_create_ctrl_node(adev); 1394 1395 list_for_each_entry(obj, &con->head, node) { 1396 if (amdgpu_ras_is_supported(adev, obj->head.block) && 1397 (obj->attr_inuse == 1)) { 1398 sprintf(fs_info.debugfs_name, "%s_err_inject", 1399 ras_block_str(obj->head.block)); 1400 fs_info.head = obj->head; 1401 amdgpu_ras_debugfs_create(adev, &fs_info, dir); 1402 } 1403 } 1404 } 1405 1406 /* debugfs end */ 1407 1408 /* ras fs */ 1409 static BIN_ATTR(gpu_vram_bad_pages, S_IRUGO, 1410 amdgpu_ras_sysfs_badpages_read, NULL, 0); 1411 #endif /* __linux__ */ 1412 static DEVICE_ATTR(features, S_IRUGO, 1413 amdgpu_ras_sysfs_features_read, NULL); 1414 static int amdgpu_ras_fs_init(struct amdgpu_device *adev) 1415 { 1416 #ifdef __linux__ 1417 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1418 struct attribute_group group = { 1419 .name = RAS_FS_NAME, 1420 }; 1421 struct attribute *attrs[] = { 1422 &con->features_attr.attr, 1423 NULL 1424 }; 1425 struct bin_attribute *bin_attrs[] = { 1426 NULL, 1427 NULL, 1428 }; 1429 int r; 1430 1431 /* add features entry */ 1432 con->features_attr = dev_attr_features; 1433 group.attrs = attrs; 1434 sysfs_attr_init(attrs[0]); 1435 1436 if (amdgpu_bad_page_threshold != 0) { 1437 /* add bad_page_features entry */ 1438 bin_attr_gpu_vram_bad_pages.private = NULL; 1439 con->badpages_attr = bin_attr_gpu_vram_bad_pages; 1440 bin_attrs[0] = &con->badpages_attr; 1441 group.bin_attrs = bin_attrs; 1442 sysfs_bin_attr_init(bin_attrs[0]); 1443 } 1444 1445 r = sysfs_create_group(&adev->dev->kobj, &group); 1446 if (r) 1447 dev_err(adev->dev, "Failed to create RAS sysfs group!"); 1448 #endif 1449 1450 return 0; 1451 } 1452 1453 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev) 1454 { 1455 #ifdef __linux__ 1456 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1457 struct ras_manager *con_obj, *ip_obj, *tmp; 1458 1459 if (IS_ENABLED(CONFIG_DEBUG_FS)) { 1460 list_for_each_entry_safe(con_obj, tmp, &con->head, node) { 1461 ip_obj = amdgpu_ras_find_obj(adev, &con_obj->head); 1462 if (ip_obj) 1463 put_obj(ip_obj); 1464 } 1465 } 1466 1467 amdgpu_ras_sysfs_remove_all(adev); 1468 #endif 1469 return 0; 1470 } 1471 /* ras fs end */ 1472 1473 /* ih begin */ 1474 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj) 1475 { 1476 struct ras_ih_data *data = &obj->ih_data; 1477 struct amdgpu_iv_entry entry; 1478 int ret; 1479 struct ras_err_data err_data = {0, 0, 0, NULL}; 1480 1481 while (data->rptr != data->wptr) { 1482 rmb(); 1483 memcpy(&entry, &data->ring[data->rptr], 1484 data->element_size); 1485 1486 wmb(); 1487 data->rptr = (data->aligned_element_size + 1488 data->rptr) % data->ring_size; 1489 1490 /* Let IP handle its data, maybe we need get the output 1491 * from the callback to udpate the error type/count, etc 1492 */ 1493 if (data->cb) { 1494 ret = data->cb(obj->adev, &err_data, &entry); 1495 /* ue will trigger an interrupt, and in that case 1496 * we need do a reset to recovery the whole system. 1497 * But leave IP do that recovery, here we just dispatch 1498 * the error. 1499 */ 1500 if (ret == AMDGPU_RAS_SUCCESS) { 1501 /* these counts could be left as 0 if 1502 * some blocks do not count error number 1503 */ 1504 obj->err_data.ue_count += err_data.ue_count; 1505 obj->err_data.ce_count += err_data.ce_count; 1506 } 1507 } 1508 } 1509 } 1510 1511 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work) 1512 { 1513 struct ras_ih_data *data = 1514 container_of(work, struct ras_ih_data, ih_work); 1515 struct ras_manager *obj = 1516 container_of(data, struct ras_manager, ih_data); 1517 1518 amdgpu_ras_interrupt_handler(obj); 1519 } 1520 1521 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev, 1522 struct ras_dispatch_if *info) 1523 { 1524 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head); 1525 struct ras_ih_data *data = &obj->ih_data; 1526 1527 if (!obj) 1528 return -EINVAL; 1529 1530 if (data->inuse == 0) 1531 return 0; 1532 1533 /* Might be overflow... */ 1534 memcpy(&data->ring[data->wptr], info->entry, 1535 data->element_size); 1536 1537 wmb(); 1538 data->wptr = (data->aligned_element_size + 1539 data->wptr) % data->ring_size; 1540 1541 schedule_work(&data->ih_work); 1542 1543 return 0; 1544 } 1545 1546 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev, 1547 struct ras_ih_if *info) 1548 { 1549 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head); 1550 struct ras_ih_data *data; 1551 1552 if (!obj) 1553 return -EINVAL; 1554 1555 data = &obj->ih_data; 1556 if (data->inuse == 0) 1557 return 0; 1558 1559 cancel_work_sync(&data->ih_work); 1560 1561 kfree(data->ring); 1562 memset(data, 0, sizeof(*data)); 1563 put_obj(obj); 1564 1565 return 0; 1566 } 1567 1568 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev, 1569 struct ras_ih_if *info) 1570 { 1571 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head); 1572 struct ras_ih_data *data; 1573 1574 if (!obj) { 1575 /* in case we registe the IH before enable ras feature */ 1576 obj = amdgpu_ras_create_obj(adev, &info->head); 1577 if (!obj) 1578 return -EINVAL; 1579 } else 1580 get_obj(obj); 1581 1582 data = &obj->ih_data; 1583 /* add the callback.etc */ 1584 *data = (struct ras_ih_data) { 1585 .inuse = 0, 1586 .cb = info->cb, 1587 .element_size = sizeof(struct amdgpu_iv_entry), 1588 .rptr = 0, 1589 .wptr = 0, 1590 }; 1591 1592 INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler); 1593 1594 data->aligned_element_size = roundup2(data->element_size, 8); 1595 /* the ring can store 64 iv entries. */ 1596 data->ring_size = 64 * data->aligned_element_size; 1597 data->ring = kmalloc(data->ring_size, GFP_KERNEL); 1598 if (!data->ring) { 1599 put_obj(obj); 1600 return -ENOMEM; 1601 } 1602 1603 /* IH is ready */ 1604 data->inuse = 1; 1605 1606 return 0; 1607 } 1608 1609 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev) 1610 { 1611 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1612 struct ras_manager *obj, *tmp; 1613 1614 list_for_each_entry_safe(obj, tmp, &con->head, node) { 1615 struct ras_ih_if info = { 1616 .head = obj->head, 1617 }; 1618 amdgpu_ras_interrupt_remove_handler(adev, &info); 1619 } 1620 1621 return 0; 1622 } 1623 /* ih end */ 1624 1625 /* traversal all IPs except NBIO to query error counter */ 1626 static void amdgpu_ras_log_on_err_counter(struct amdgpu_device *adev) 1627 { 1628 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1629 struct ras_manager *obj; 1630 1631 if (!adev->ras_enabled || !con) 1632 return; 1633 1634 list_for_each_entry(obj, &con->head, node) { 1635 struct ras_query_if info = { 1636 .head = obj->head, 1637 }; 1638 1639 /* 1640 * PCIE_BIF IP has one different isr by ras controller 1641 * interrupt, the specific ras counter query will be 1642 * done in that isr. So skip such block from common 1643 * sync flood interrupt isr calling. 1644 */ 1645 if (info.head.block == AMDGPU_RAS_BLOCK__PCIE_BIF) 1646 continue; 1647 1648 amdgpu_ras_query_error_status(adev, &info); 1649 } 1650 } 1651 1652 /* Parse RdRspStatus and WrRspStatus */ 1653 static void amdgpu_ras_error_status_query(struct amdgpu_device *adev, 1654 struct ras_query_if *info) 1655 { 1656 /* 1657 * Only two block need to query read/write 1658 * RspStatus at current state 1659 */ 1660 switch (info->head.block) { 1661 case AMDGPU_RAS_BLOCK__GFX: 1662 if (adev->gfx.ras_funcs && 1663 adev->gfx.ras_funcs->query_ras_error_status) 1664 adev->gfx.ras_funcs->query_ras_error_status(adev); 1665 break; 1666 case AMDGPU_RAS_BLOCK__MMHUB: 1667 if (adev->mmhub.ras_funcs && 1668 adev->mmhub.ras_funcs->query_ras_error_status) 1669 adev->mmhub.ras_funcs->query_ras_error_status(adev); 1670 break; 1671 default: 1672 break; 1673 } 1674 } 1675 1676 static void amdgpu_ras_query_err_status(struct amdgpu_device *adev) 1677 { 1678 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1679 struct ras_manager *obj; 1680 1681 if (!adev->ras_enabled || !con) 1682 return; 1683 1684 list_for_each_entry(obj, &con->head, node) { 1685 struct ras_query_if info = { 1686 .head = obj->head, 1687 }; 1688 1689 amdgpu_ras_error_status_query(adev, &info); 1690 } 1691 } 1692 1693 /* recovery begin */ 1694 1695 /* return 0 on success. 1696 * caller need free bps. 1697 */ 1698 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev, 1699 struct ras_badpage **bps, unsigned int *count) 1700 { 1701 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1702 struct ras_err_handler_data *data; 1703 int i = 0; 1704 int ret = 0, status; 1705 1706 if (!con || !con->eh_data || !bps || !count) 1707 return -EINVAL; 1708 1709 mutex_lock(&con->recovery_lock); 1710 data = con->eh_data; 1711 if (!data || data->count == 0) { 1712 *bps = NULL; 1713 ret = -EINVAL; 1714 goto out; 1715 } 1716 1717 *bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL); 1718 if (!*bps) { 1719 ret = -ENOMEM; 1720 goto out; 1721 } 1722 1723 for (; i < data->count; i++) { 1724 (*bps)[i] = (struct ras_badpage){ 1725 .bp = data->bps[i].retired_page, 1726 .size = AMDGPU_GPU_PAGE_SIZE, 1727 .flags = AMDGPU_RAS_RETIRE_PAGE_RESERVED, 1728 }; 1729 status = amdgpu_vram_mgr_query_page_status( 1730 ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM), 1731 data->bps[i].retired_page); 1732 if (status == -EBUSY) 1733 (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_PENDING; 1734 else if (status == -ENOENT) 1735 (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_FAULT; 1736 } 1737 1738 *count = data->count; 1739 out: 1740 mutex_unlock(&con->recovery_lock); 1741 return ret; 1742 } 1743 1744 static void amdgpu_ras_do_recovery(struct work_struct *work) 1745 { 1746 struct amdgpu_ras *ras = 1747 container_of(work, struct amdgpu_ras, recovery_work); 1748 struct amdgpu_device *remote_adev = NULL; 1749 struct amdgpu_device *adev = ras->adev; 1750 struct list_head device_list, *device_list_handle = NULL; 1751 1752 if (!ras->disable_ras_err_cnt_harvest) { 1753 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev); 1754 1755 /* Build list of devices to query RAS related errors */ 1756 if (hive && adev->gmc.xgmi.num_physical_nodes > 1) { 1757 device_list_handle = &hive->device_list; 1758 } else { 1759 INIT_LIST_HEAD(&device_list); 1760 list_add_tail(&adev->gmc.xgmi.head, &device_list); 1761 device_list_handle = &device_list; 1762 } 1763 1764 list_for_each_entry(remote_adev, 1765 device_list_handle, gmc.xgmi.head) { 1766 amdgpu_ras_query_err_status(remote_adev); 1767 amdgpu_ras_log_on_err_counter(remote_adev); 1768 } 1769 1770 amdgpu_put_xgmi_hive(hive); 1771 } 1772 1773 if (amdgpu_device_should_recover_gpu(ras->adev)) 1774 amdgpu_device_gpu_recover(ras->adev, NULL); 1775 atomic_set(&ras->in_recovery, 0); 1776 } 1777 1778 /* alloc/realloc bps array */ 1779 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev, 1780 struct ras_err_handler_data *data, int pages) 1781 { 1782 unsigned int old_space = data->count + data->space_left; 1783 unsigned int new_space = old_space + pages; 1784 unsigned int align_space = roundup2(new_space, 512); 1785 void *bps = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL); 1786 1787 if (!bps) { 1788 kfree(bps); 1789 return -ENOMEM; 1790 } 1791 1792 if (data->bps) { 1793 memcpy(bps, data->bps, 1794 data->count * sizeof(*data->bps)); 1795 kfree(data->bps); 1796 } 1797 1798 data->bps = bps; 1799 data->space_left += align_space - old_space; 1800 return 0; 1801 } 1802 1803 /* it deal with vram only. */ 1804 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev, 1805 struct eeprom_table_record *bps, int pages) 1806 { 1807 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1808 struct ras_err_handler_data *data; 1809 int ret = 0; 1810 uint32_t i; 1811 1812 if (!con || !con->eh_data || !bps || pages <= 0) 1813 return 0; 1814 1815 mutex_lock(&con->recovery_lock); 1816 data = con->eh_data; 1817 if (!data) 1818 goto out; 1819 1820 for (i = 0; i < pages; i++) { 1821 if (amdgpu_ras_check_bad_page_unlock(con, 1822 bps[i].retired_page << AMDGPU_GPU_PAGE_SHIFT)) 1823 continue; 1824 1825 if (!data->space_left && 1826 amdgpu_ras_realloc_eh_data_space(adev, data, 256)) { 1827 ret = -ENOMEM; 1828 goto out; 1829 } 1830 1831 amdgpu_vram_mgr_reserve_range( 1832 ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM), 1833 bps[i].retired_page << AMDGPU_GPU_PAGE_SHIFT, 1834 AMDGPU_GPU_PAGE_SIZE); 1835 1836 memcpy(&data->bps[data->count], &bps[i], sizeof(*data->bps)); 1837 data->count++; 1838 data->space_left--; 1839 } 1840 out: 1841 mutex_unlock(&con->recovery_lock); 1842 1843 return ret; 1844 } 1845 1846 /* 1847 * write error record array to eeprom, the function should be 1848 * protected by recovery_lock 1849 */ 1850 int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev) 1851 { 1852 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1853 struct ras_err_handler_data *data; 1854 struct amdgpu_ras_eeprom_control *control; 1855 int save_count; 1856 1857 if (!con || !con->eh_data) 1858 return 0; 1859 1860 control = &con->eeprom_control; 1861 data = con->eh_data; 1862 save_count = data->count - control->ras_num_recs; 1863 /* only new entries are saved */ 1864 if (save_count > 0) { 1865 if (amdgpu_ras_eeprom_append(control, 1866 &data->bps[control->ras_num_recs], 1867 save_count)) { 1868 dev_err(adev->dev, "Failed to save EEPROM table data!"); 1869 return -EIO; 1870 } 1871 1872 dev_info(adev->dev, "Saved %d pages to EEPROM table.\n", save_count); 1873 } 1874 1875 return 0; 1876 } 1877 1878 /* 1879 * read error record array in eeprom and reserve enough space for 1880 * storing new bad pages 1881 */ 1882 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev) 1883 { 1884 struct amdgpu_ras_eeprom_control *control = 1885 &adev->psp.ras_context.ras->eeprom_control; 1886 struct eeprom_table_record *bps; 1887 int ret; 1888 1889 /* no bad page record, skip eeprom access */ 1890 if (control->ras_num_recs == 0 || amdgpu_bad_page_threshold == 0) 1891 return 0; 1892 1893 bps = kcalloc(control->ras_num_recs, sizeof(*bps), GFP_KERNEL); 1894 if (!bps) 1895 return -ENOMEM; 1896 1897 ret = amdgpu_ras_eeprom_read(control, bps, control->ras_num_recs); 1898 if (ret) 1899 dev_err(adev->dev, "Failed to load EEPROM table records!"); 1900 else 1901 ret = amdgpu_ras_add_bad_pages(adev, bps, control->ras_num_recs); 1902 1903 kfree(bps); 1904 return ret; 1905 } 1906 1907 static bool amdgpu_ras_check_bad_page_unlock(struct amdgpu_ras *con, 1908 uint64_t addr) 1909 { 1910 struct ras_err_handler_data *data = con->eh_data; 1911 int i; 1912 1913 addr >>= AMDGPU_GPU_PAGE_SHIFT; 1914 for (i = 0; i < data->count; i++) 1915 if (addr == data->bps[i].retired_page) 1916 return true; 1917 1918 return false; 1919 } 1920 1921 /* 1922 * check if an address belongs to bad page 1923 * 1924 * Note: this check is only for umc block 1925 */ 1926 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev, 1927 uint64_t addr) 1928 { 1929 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1930 bool ret = false; 1931 1932 if (!con || !con->eh_data) 1933 return ret; 1934 1935 mutex_lock(&con->recovery_lock); 1936 ret = amdgpu_ras_check_bad_page_unlock(con, addr); 1937 mutex_unlock(&con->recovery_lock); 1938 return ret; 1939 } 1940 1941 static void amdgpu_ras_validate_threshold(struct amdgpu_device *adev, 1942 uint32_t max_count) 1943 { 1944 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1945 1946 /* 1947 * Justification of value bad_page_cnt_threshold in ras structure 1948 * 1949 * Generally, -1 <= amdgpu_bad_page_threshold <= max record length 1950 * in eeprom, and introduce two scenarios accordingly. 1951 * 1952 * Bad page retirement enablement: 1953 * - If amdgpu_bad_page_threshold = -1, 1954 * bad_page_cnt_threshold = typical value by formula. 1955 * 1956 * - When the value from user is 0 < amdgpu_bad_page_threshold < 1957 * max record length in eeprom, use it directly. 1958 * 1959 * Bad page retirement disablement: 1960 * - If amdgpu_bad_page_threshold = 0, bad page retirement 1961 * functionality is disabled, and bad_page_cnt_threshold will 1962 * take no effect. 1963 */ 1964 1965 if (amdgpu_bad_page_threshold < 0) { 1966 u64 val = adev->gmc.mc_vram_size; 1967 1968 do_div(val, RAS_BAD_PAGE_COVER); 1969 con->bad_page_cnt_threshold = min(lower_32_bits(val), 1970 max_count); 1971 } else { 1972 con->bad_page_cnt_threshold = min_t(int, max_count, 1973 amdgpu_bad_page_threshold); 1974 } 1975 } 1976 1977 int amdgpu_ras_recovery_init(struct amdgpu_device *adev) 1978 { 1979 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1980 struct ras_err_handler_data **data; 1981 u32 max_eeprom_records_count = 0; 1982 bool exc_err_limit = false; 1983 int ret; 1984 1985 if (!con) 1986 return 0; 1987 1988 /* Allow access to RAS EEPROM via debugfs, when the ASIC 1989 * supports RAS and debugfs is enabled, but when 1990 * adev->ras_enabled is unset, i.e. when "ras_enable" 1991 * module parameter is set to 0. 1992 */ 1993 con->adev = adev; 1994 1995 if (!adev->ras_enabled) 1996 return 0; 1997 1998 data = &con->eh_data; 1999 *data = kmalloc(sizeof(**data), GFP_KERNEL | __GFP_ZERO); 2000 if (!*data) { 2001 ret = -ENOMEM; 2002 goto out; 2003 } 2004 2005 rw_init(&con->recovery_lock, "rasrec"); 2006 INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery); 2007 atomic_set(&con->in_recovery, 0); 2008 2009 max_eeprom_records_count = amdgpu_ras_eeprom_max_record_count(); 2010 amdgpu_ras_validate_threshold(adev, max_eeprom_records_count); 2011 2012 /* Todo: During test the SMU might fail to read the eeprom through I2C 2013 * when the GPU is pending on XGMI reset during probe time 2014 * (Mostly after second bus reset), skip it now 2015 */ 2016 if (adev->gmc.xgmi.pending_reset) 2017 return 0; 2018 ret = amdgpu_ras_eeprom_init(&con->eeprom_control, &exc_err_limit); 2019 /* 2020 * This calling fails when exc_err_limit is true or 2021 * ret != 0. 2022 */ 2023 if (exc_err_limit || ret) 2024 goto free; 2025 2026 if (con->eeprom_control.ras_num_recs) { 2027 ret = amdgpu_ras_load_bad_pages(adev); 2028 if (ret) 2029 goto free; 2030 2031 if (adev->smu.ppt_funcs && adev->smu.ppt_funcs->send_hbm_bad_pages_num) 2032 adev->smu.ppt_funcs->send_hbm_bad_pages_num(&adev->smu, con->eeprom_control.ras_num_recs); 2033 } 2034 2035 return 0; 2036 2037 free: 2038 kfree((*data)->bps); 2039 kfree(*data); 2040 con->eh_data = NULL; 2041 out: 2042 dev_warn(adev->dev, "Failed to initialize ras recovery! (%d)\n", ret); 2043 2044 /* 2045 * Except error threshold exceeding case, other failure cases in this 2046 * function would not fail amdgpu driver init. 2047 */ 2048 if (!exc_err_limit) 2049 ret = 0; 2050 else 2051 ret = -EINVAL; 2052 2053 return ret; 2054 } 2055 2056 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev) 2057 { 2058 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 2059 struct ras_err_handler_data *data = con->eh_data; 2060 2061 /* recovery_init failed to init it, fini is useless */ 2062 if (!data) 2063 return 0; 2064 2065 cancel_work_sync(&con->recovery_work); 2066 2067 mutex_lock(&con->recovery_lock); 2068 con->eh_data = NULL; 2069 kfree(data->bps); 2070 kfree(data); 2071 mutex_unlock(&con->recovery_lock); 2072 2073 return 0; 2074 } 2075 /* recovery end */ 2076 2077 /* return 0 if ras will reset gpu and repost.*/ 2078 int amdgpu_ras_request_reset_on_boot(struct amdgpu_device *adev, 2079 unsigned int block) 2080 { 2081 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev); 2082 2083 if (!ras) 2084 return -EINVAL; 2085 2086 ras->flags |= AMDGPU_RAS_FLAG_INIT_NEED_RESET; 2087 return 0; 2088 } 2089 2090 static bool amdgpu_ras_asic_supported(struct amdgpu_device *adev) 2091 { 2092 return adev->asic_type == CHIP_VEGA10 || 2093 adev->asic_type == CHIP_VEGA20 || 2094 adev->asic_type == CHIP_ARCTURUS || 2095 adev->asic_type == CHIP_ALDEBARAN || 2096 adev->asic_type == CHIP_SIENNA_CICHLID; 2097 } 2098 2099 /* 2100 * this is workaround for vega20 workstation sku, 2101 * force enable gfx ras, ignore vbios gfx ras flag 2102 * due to GC EDC can not write 2103 */ 2104 static void amdgpu_ras_get_quirks(struct amdgpu_device *adev) 2105 { 2106 struct atom_context *ctx = adev->mode_info.atom_context; 2107 2108 if (!ctx) 2109 return; 2110 2111 #ifdef notyet 2112 if (strnstr(ctx->vbios_version, "D16406", 2113 sizeof(ctx->vbios_version)) || 2114 strnstr(ctx->vbios_version, "D36002", 2115 sizeof(ctx->vbios_version))) 2116 adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__GFX); 2117 #endif 2118 } 2119 2120 /* 2121 * check hardware's ras ability which will be saved in hw_supported. 2122 * if hardware does not support ras, we can skip some ras initializtion and 2123 * forbid some ras operations from IP. 2124 * if software itself, say boot parameter, limit the ras ability. We still 2125 * need allow IP do some limited operations, like disable. In such case, 2126 * we have to initialize ras as normal. but need check if operation is 2127 * allowed or not in each function. 2128 */ 2129 static void amdgpu_ras_check_supported(struct amdgpu_device *adev) 2130 { 2131 adev->ras_hw_enabled = adev->ras_enabled = 0; 2132 2133 if (amdgpu_sriov_vf(adev) || !adev->is_atom_fw || 2134 !amdgpu_ras_asic_supported(adev)) 2135 return; 2136 2137 if (!adev->gmc.xgmi.connected_to_cpu) { 2138 if (amdgpu_atomfirmware_mem_ecc_supported(adev)) { 2139 dev_info(adev->dev, "MEM ECC is active.\n"); 2140 adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__UMC | 2141 1 << AMDGPU_RAS_BLOCK__DF); 2142 } else { 2143 dev_info(adev->dev, "MEM ECC is not presented.\n"); 2144 } 2145 2146 if (amdgpu_atomfirmware_sram_ecc_supported(adev)) { 2147 dev_info(adev->dev, "SRAM ECC is active.\n"); 2148 adev->ras_hw_enabled |= ~(1 << AMDGPU_RAS_BLOCK__UMC | 2149 1 << AMDGPU_RAS_BLOCK__DF); 2150 } else { 2151 dev_info(adev->dev, "SRAM ECC is not presented.\n"); 2152 } 2153 } else { 2154 /* driver only manages a few IP blocks RAS feature 2155 * when GPU is connected cpu through XGMI */ 2156 adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__GFX | 2157 1 << AMDGPU_RAS_BLOCK__SDMA | 2158 1 << AMDGPU_RAS_BLOCK__MMHUB); 2159 } 2160 2161 amdgpu_ras_get_quirks(adev); 2162 2163 /* hw_supported needs to be aligned with RAS block mask. */ 2164 adev->ras_hw_enabled &= AMDGPU_RAS_BLOCK_MASK; 2165 2166 adev->ras_enabled = amdgpu_ras_enable == 0 ? 0 : 2167 adev->ras_hw_enabled & amdgpu_ras_mask; 2168 } 2169 2170 static void amdgpu_ras_counte_dw(struct work_struct *work) 2171 { 2172 struct amdgpu_ras *con = container_of(work, struct amdgpu_ras, 2173 ras_counte_delay_work.work); 2174 struct amdgpu_device *adev = con->adev; 2175 struct drm_device *dev = adev_to_drm(adev); 2176 unsigned long ce_count, ue_count; 2177 int res; 2178 2179 res = pm_runtime_get_sync(dev->dev); 2180 if (res < 0) 2181 goto Out; 2182 2183 /* Cache new values. 2184 */ 2185 if (amdgpu_ras_query_error_count(adev, &ce_count, &ue_count) == 0) { 2186 atomic_set(&con->ras_ce_count, ce_count); 2187 atomic_set(&con->ras_ue_count, ue_count); 2188 } 2189 2190 pm_runtime_mark_last_busy(dev->dev); 2191 Out: 2192 pm_runtime_put_autosuspend(dev->dev); 2193 } 2194 2195 int amdgpu_ras_init(struct amdgpu_device *adev) 2196 { 2197 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 2198 int r; 2199 2200 if (con) 2201 return 0; 2202 2203 con = kmalloc(sizeof(struct amdgpu_ras) + 2204 sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT, 2205 GFP_KERNEL|__GFP_ZERO); 2206 if (!con) 2207 return -ENOMEM; 2208 2209 con->adev = adev; 2210 INIT_DELAYED_WORK(&con->ras_counte_delay_work, amdgpu_ras_counte_dw); 2211 atomic_set(&con->ras_ce_count, 0); 2212 atomic_set(&con->ras_ue_count, 0); 2213 2214 con->objs = (struct ras_manager *)(con + 1); 2215 2216 amdgpu_ras_set_context(adev, con); 2217 2218 amdgpu_ras_check_supported(adev); 2219 2220 if (!adev->ras_enabled || adev->asic_type == CHIP_VEGA10) { 2221 /* set gfx block ras context feature for VEGA20 Gaming 2222 * send ras disable cmd to ras ta during ras late init. 2223 */ 2224 if (!adev->ras_enabled && adev->asic_type == CHIP_VEGA20) { 2225 con->features |= BIT(AMDGPU_RAS_BLOCK__GFX); 2226 2227 return 0; 2228 } 2229 2230 r = 0; 2231 goto release_con; 2232 } 2233 2234 con->features = 0; 2235 INIT_LIST_HEAD(&con->head); 2236 /* Might need get this flag from vbios. */ 2237 con->flags = RAS_DEFAULT_FLAGS; 2238 2239 /* initialize nbio ras function ahead of any other 2240 * ras functions so hardware fatal error interrupt 2241 * can be enabled as early as possible */ 2242 switch (adev->asic_type) { 2243 case CHIP_VEGA20: 2244 case CHIP_ARCTURUS: 2245 case CHIP_ALDEBARAN: 2246 if (!adev->gmc.xgmi.connected_to_cpu) 2247 adev->nbio.ras_funcs = &nbio_v7_4_ras_funcs; 2248 break; 2249 default: 2250 /* nbio ras is not available */ 2251 break; 2252 } 2253 2254 if (adev->nbio.ras_funcs && 2255 adev->nbio.ras_funcs->init_ras_controller_interrupt) { 2256 r = adev->nbio.ras_funcs->init_ras_controller_interrupt(adev); 2257 if (r) 2258 goto release_con; 2259 } 2260 2261 if (adev->nbio.ras_funcs && 2262 adev->nbio.ras_funcs->init_ras_err_event_athub_interrupt) { 2263 r = adev->nbio.ras_funcs->init_ras_err_event_athub_interrupt(adev); 2264 if (r) 2265 goto release_con; 2266 } 2267 2268 if (amdgpu_ras_fs_init(adev)) { 2269 r = -EINVAL; 2270 goto release_con; 2271 } 2272 2273 dev_info(adev->dev, "RAS INFO: ras initialized successfully, " 2274 "hardware ability[%x] ras_mask[%x]\n", 2275 adev->ras_hw_enabled, adev->ras_enabled); 2276 2277 return 0; 2278 release_con: 2279 amdgpu_ras_set_context(adev, NULL); 2280 kfree(con); 2281 2282 return r; 2283 } 2284 2285 int amdgpu_persistent_edc_harvesting_supported(struct amdgpu_device *adev) 2286 { 2287 if (adev->gmc.xgmi.connected_to_cpu) 2288 return 1; 2289 return 0; 2290 } 2291 2292 static int amdgpu_persistent_edc_harvesting(struct amdgpu_device *adev, 2293 struct ras_common_if *ras_block) 2294 { 2295 struct ras_query_if info = { 2296 .head = *ras_block, 2297 }; 2298 2299 if (!amdgpu_persistent_edc_harvesting_supported(adev)) 2300 return 0; 2301 2302 if (amdgpu_ras_query_error_status(adev, &info) != 0) 2303 DRM_WARN("RAS init harvest failure"); 2304 2305 if (amdgpu_ras_reset_error_status(adev, ras_block->block) != 0) 2306 DRM_WARN("RAS init harvest reset failure"); 2307 2308 return 0; 2309 } 2310 2311 /* helper function to handle common stuff in ip late init phase */ 2312 int amdgpu_ras_late_init(struct amdgpu_device *adev, 2313 struct ras_common_if *ras_block, 2314 struct ras_fs_if *fs_info, 2315 struct ras_ih_if *ih_info) 2316 { 2317 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 2318 unsigned long ue_count, ce_count; 2319 int r; 2320 2321 /* disable RAS feature per IP block if it is not supported */ 2322 if (!amdgpu_ras_is_supported(adev, ras_block->block)) { 2323 amdgpu_ras_feature_enable_on_boot(adev, ras_block, 0); 2324 return 0; 2325 } 2326 2327 r = amdgpu_ras_feature_enable_on_boot(adev, ras_block, 1); 2328 if (r) { 2329 if (r == -EAGAIN) { 2330 /* request gpu reset. will run again */ 2331 amdgpu_ras_request_reset_on_boot(adev, 2332 ras_block->block); 2333 return 0; 2334 } else if (adev->in_suspend || amdgpu_in_reset(adev)) { 2335 /* in resume phase, if fail to enable ras, 2336 * clean up all ras fs nodes, and disable ras */ 2337 goto cleanup; 2338 } else 2339 return r; 2340 } 2341 2342 /* check for errors on warm reset edc persisant supported ASIC */ 2343 amdgpu_persistent_edc_harvesting(adev, ras_block); 2344 2345 /* in resume phase, no need to create ras fs node */ 2346 if (adev->in_suspend || amdgpu_in_reset(adev)) 2347 return 0; 2348 2349 if (ih_info->cb) { 2350 r = amdgpu_ras_interrupt_add_handler(adev, ih_info); 2351 if (r) 2352 goto interrupt; 2353 } 2354 2355 r = amdgpu_ras_sysfs_create(adev, fs_info); 2356 if (r) 2357 goto sysfs; 2358 2359 /* Those are the cached values at init. 2360 */ 2361 if (amdgpu_ras_query_error_count(adev, &ce_count, &ue_count) == 0) { 2362 atomic_set(&con->ras_ce_count, ce_count); 2363 atomic_set(&con->ras_ue_count, ue_count); 2364 } 2365 2366 return 0; 2367 cleanup: 2368 amdgpu_ras_sysfs_remove(adev, ras_block); 2369 sysfs: 2370 if (ih_info->cb) 2371 amdgpu_ras_interrupt_remove_handler(adev, ih_info); 2372 interrupt: 2373 amdgpu_ras_feature_enable(adev, ras_block, 0); 2374 return r; 2375 } 2376 2377 /* helper function to remove ras fs node and interrupt handler */ 2378 void amdgpu_ras_late_fini(struct amdgpu_device *adev, 2379 struct ras_common_if *ras_block, 2380 struct ras_ih_if *ih_info) 2381 { 2382 if (!ras_block || !ih_info) 2383 return; 2384 2385 amdgpu_ras_sysfs_remove(adev, ras_block); 2386 if (ih_info->cb) 2387 amdgpu_ras_interrupt_remove_handler(adev, ih_info); 2388 amdgpu_ras_feature_enable(adev, ras_block, 0); 2389 } 2390 2391 /* do some init work after IP late init as dependence. 2392 * and it runs in resume/gpu reset/booting up cases. 2393 */ 2394 void amdgpu_ras_resume(struct amdgpu_device *adev) 2395 { 2396 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 2397 struct ras_manager *obj, *tmp; 2398 2399 if (!adev->ras_enabled || !con) { 2400 /* clean ras context for VEGA20 Gaming after send ras disable cmd */ 2401 amdgpu_release_ras_context(adev); 2402 2403 return; 2404 } 2405 2406 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) { 2407 /* Set up all other IPs which are not implemented. There is a 2408 * tricky thing that IP's actual ras error type should be 2409 * MULTI_UNCORRECTABLE, but as driver does not handle it, so 2410 * ERROR_NONE make sense anyway. 2411 */ 2412 amdgpu_ras_enable_all_features(adev, 1); 2413 2414 /* We enable ras on all hw_supported block, but as boot 2415 * parameter might disable some of them and one or more IP has 2416 * not implemented yet. So we disable them on behalf. 2417 */ 2418 list_for_each_entry_safe(obj, tmp, &con->head, node) { 2419 if (!amdgpu_ras_is_supported(adev, obj->head.block)) { 2420 amdgpu_ras_feature_enable(adev, &obj->head, 0); 2421 /* there should be no any reference. */ 2422 WARN_ON(alive_obj(obj)); 2423 } 2424 } 2425 } 2426 2427 if (con->flags & AMDGPU_RAS_FLAG_INIT_NEED_RESET) { 2428 con->flags &= ~AMDGPU_RAS_FLAG_INIT_NEED_RESET; 2429 /* setup ras obj state as disabled. 2430 * for init_by_vbios case. 2431 * if we want to enable ras, just enable it in a normal way. 2432 * If we want do disable it, need setup ras obj as enabled, 2433 * then issue another TA disable cmd. 2434 * See feature_enable_on_boot 2435 */ 2436 amdgpu_ras_disable_all_features(adev, 1); 2437 amdgpu_ras_reset_gpu(adev); 2438 } 2439 } 2440 2441 void amdgpu_ras_suspend(struct amdgpu_device *adev) 2442 { 2443 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 2444 2445 if (!adev->ras_enabled || !con) 2446 return; 2447 2448 amdgpu_ras_disable_all_features(adev, 0); 2449 /* Make sure all ras objects are disabled. */ 2450 if (con->features) 2451 amdgpu_ras_disable_all_features(adev, 1); 2452 } 2453 2454 /* do some fini work before IP fini as dependence */ 2455 int amdgpu_ras_pre_fini(struct amdgpu_device *adev) 2456 { 2457 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 2458 2459 if (!adev->ras_enabled || !con) 2460 return 0; 2461 2462 2463 /* Need disable ras on all IPs here before ip [hw/sw]fini */ 2464 amdgpu_ras_disable_all_features(adev, 0); 2465 amdgpu_ras_recovery_fini(adev); 2466 return 0; 2467 } 2468 2469 int amdgpu_ras_fini(struct amdgpu_device *adev) 2470 { 2471 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 2472 2473 if (!adev->ras_enabled || !con) 2474 return 0; 2475 2476 amdgpu_ras_fs_fini(adev); 2477 amdgpu_ras_interrupt_remove_all(adev); 2478 2479 WARN(con->features, "Feature mask is not cleared"); 2480 2481 if (con->features) 2482 amdgpu_ras_disable_all_features(adev, 1); 2483 2484 cancel_delayed_work_sync(&con->ras_counte_delay_work); 2485 2486 amdgpu_ras_set_context(adev, NULL); 2487 kfree(con); 2488 2489 return 0; 2490 } 2491 2492 void amdgpu_ras_global_ras_isr(struct amdgpu_device *adev) 2493 { 2494 amdgpu_ras_check_supported(adev); 2495 if (!adev->ras_hw_enabled) 2496 return; 2497 2498 if (atomic_cmpxchg(&amdgpu_ras_in_intr, 0, 1) == 0) { 2499 dev_info(adev->dev, "uncorrectable hardware error" 2500 "(ERREVENT_ATHUB_INTERRUPT) detected!\n"); 2501 2502 amdgpu_ras_reset_gpu(adev); 2503 } 2504 } 2505 2506 bool amdgpu_ras_need_emergency_restart(struct amdgpu_device *adev) 2507 { 2508 if (adev->asic_type == CHIP_VEGA20 && 2509 adev->pm.fw_version <= 0x283400) { 2510 return !(amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) && 2511 amdgpu_ras_intr_triggered(); 2512 } 2513 2514 return false; 2515 } 2516 2517 void amdgpu_release_ras_context(struct amdgpu_device *adev) 2518 { 2519 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 2520 2521 if (!con) 2522 return; 2523 2524 if (!adev->ras_enabled && con->features & BIT(AMDGPU_RAS_BLOCK__GFX)) { 2525 con->features &= ~BIT(AMDGPU_RAS_BLOCK__GFX); 2526 amdgpu_ras_set_context(adev, NULL); 2527 kfree(con); 2528 } 2529 } 2530