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 #include <linux/list_sort.h> 32 33 #include "amdgpu.h" 34 #include "amdgpu_ras.h" 35 #include "amdgpu_atomfirmware.h" 36 #include "amdgpu_xgmi.h" 37 #include "ivsrcid/nbio/irqsrcs_nbif_7_4.h" 38 #include "nbio_v4_3.h" 39 #include "nbif_v6_3_1.h" 40 #include "nbio_v7_9.h" 41 #include "atom.h" 42 #include "amdgpu_reset.h" 43 #include "amdgpu_psp.h" 44 #include "amdgpu_ras_mgr.h" 45 46 #ifdef CONFIG_X86_MCE_AMD 47 #include <asm/mce.h> 48 49 static bool notifier_registered; 50 #endif 51 static const char *RAS_FS_NAME = "ras"; 52 53 const char *ras_error_string[] = { 54 "none", 55 "parity", 56 "single_correctable", 57 "multi_uncorrectable", 58 "poison", 59 }; 60 61 const char *ras_block_string[] = { 62 "umc", 63 "sdma", 64 "gfx", 65 "mmhub", 66 "athub", 67 "pcie_bif", 68 "hdp", 69 "xgmi_wafl", 70 "df", 71 "smn", 72 "sem", 73 "mp0", 74 "mp1", 75 "fuse", 76 "mca", 77 "vcn", 78 "jpeg", 79 "ih", 80 "mpio", 81 "mmsch", 82 }; 83 84 const char *ras_mca_block_string[] = { 85 "mca_mp0", 86 "mca_mp1", 87 "mca_mpio", 88 "mca_iohc", 89 }; 90 91 struct amdgpu_ras_block_list { 92 /* ras block link */ 93 struct list_head node; 94 95 struct amdgpu_ras_block_object *ras_obj; 96 }; 97 98 const char *get_ras_block_str(struct ras_common_if *ras_block) 99 { 100 if (!ras_block) 101 return "NULL"; 102 103 if (ras_block->block >= AMDGPU_RAS_BLOCK_COUNT || 104 ras_block->block >= ARRAY_SIZE(ras_block_string)) 105 return "OUT OF RANGE"; 106 107 if (ras_block->block == AMDGPU_RAS_BLOCK__MCA) 108 return ras_mca_block_string[ras_block->sub_block_index]; 109 110 return ras_block_string[ras_block->block]; 111 } 112 113 #define ras_block_str(_BLOCK_) \ 114 (((_BLOCK_) < ARRAY_SIZE(ras_block_string)) ? ras_block_string[_BLOCK_] : "Out Of Range") 115 116 #define ras_err_str(i) (ras_error_string[ffs(i)]) 117 118 #define RAS_DEFAULT_FLAGS (AMDGPU_RAS_FLAG_INIT_BY_VBIOS) 119 120 /* inject address is 52 bits */ 121 #define RAS_UMC_INJECT_ADDR_LIMIT (0x1ULL << 52) 122 123 /* typical ECC bad page rate is 1 bad page per 100MB VRAM */ 124 #define RAS_BAD_PAGE_COVER (100 * 1024 * 1024ULL) 125 126 #define MAX_UMC_POISON_POLLING_TIME_ASYNC 10 127 128 #define AMDGPU_RAS_RETIRE_PAGE_INTERVAL 100 //ms 129 130 #define MAX_FLUSH_RETIRE_DWORK_TIMES 100 131 132 #define BYPASS_ALLOCATED_ADDRESS 0x0 133 #define BYPASS_INITIALIZATION_ADDRESS 0x1 134 135 enum amdgpu_ras_retire_page_reservation { 136 AMDGPU_RAS_RETIRE_PAGE_RESERVED, 137 AMDGPU_RAS_RETIRE_PAGE_PENDING, 138 AMDGPU_RAS_RETIRE_PAGE_FAULT, 139 }; 140 141 atomic_t amdgpu_ras_in_intr = ATOMIC_INIT(0); 142 143 static int amdgpu_ras_check_bad_page_unlock(struct amdgpu_ras *con, 144 uint64_t addr); 145 static int amdgpu_ras_check_bad_page(struct amdgpu_device *adev, 146 uint64_t addr); 147 148 static void amdgpu_ras_critical_region_init(struct amdgpu_device *adev); 149 static void amdgpu_ras_critical_region_fini(struct amdgpu_device *adev); 150 151 #ifdef CONFIG_X86_MCE_AMD 152 static void amdgpu_register_bad_pages_mca_notifier(struct amdgpu_device *adev); 153 static void 154 amdgpu_unregister_bad_pages_mca_notifier(struct amdgpu_device *adev); 155 struct mce_notifier_adev_list { 156 struct amdgpu_device *devs[MAX_GPU_INSTANCE]; 157 int num_gpu; 158 }; 159 static struct mce_notifier_adev_list mce_adev_list; 160 #endif 161 162 void amdgpu_ras_set_error_query_ready(struct amdgpu_device *adev, bool ready) 163 { 164 if (adev && amdgpu_ras_get_context(adev)) 165 amdgpu_ras_get_context(adev)->error_query_ready = ready; 166 } 167 168 static bool amdgpu_ras_get_error_query_ready(struct amdgpu_device *adev) 169 { 170 if (adev && amdgpu_ras_get_context(adev)) 171 return amdgpu_ras_get_context(adev)->error_query_ready; 172 173 return false; 174 } 175 176 static int amdgpu_reserve_page_direct(struct amdgpu_device *adev, uint64_t address) 177 { 178 struct ras_err_data err_data; 179 struct eeprom_table_record err_rec; 180 int ret; 181 182 ret = amdgpu_ras_check_bad_page(adev, address); 183 if (ret == -EINVAL) { 184 dev_warn(adev->dev, 185 "RAS WARN: input address 0x%llx is invalid.\n", 186 address); 187 return -EINVAL; 188 } else if (ret == 1) { 189 dev_warn(adev->dev, 190 "RAS WARN: 0x%llx has already been marked as bad page!\n", 191 address); 192 return 0; 193 } 194 195 ret = amdgpu_ras_error_data_init(&err_data); 196 if (ret) 197 return ret; 198 199 memset(&err_rec, 0x0, sizeof(struct eeprom_table_record)); 200 err_data.err_addr = &err_rec; 201 amdgpu_umc_fill_error_record(&err_data, address, address, 0, 0); 202 203 if (amdgpu_bad_page_threshold != 0) { 204 amdgpu_ras_add_bad_pages(adev, err_data.err_addr, 205 err_data.err_addr_cnt, false); 206 amdgpu_ras_save_bad_pages(adev, NULL); 207 } 208 209 amdgpu_ras_error_data_fini(&err_data); 210 211 dev_warn(adev->dev, "WARNING: THIS IS ONLY FOR TEST PURPOSES AND WILL CORRUPT RAS EEPROM\n"); 212 dev_warn(adev->dev, "Clear EEPROM:\n"); 213 dev_warn(adev->dev, " echo 1 > /sys/kernel/debug/dri/0/ras/ras_eeprom_reset\n"); 214 215 return 0; 216 } 217 218 static int amdgpu_check_address_validity(struct amdgpu_device *adev, 219 uint64_t address, uint64_t flags) 220 { 221 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 222 struct amdgpu_vram_block_info blk_info; 223 uint64_t page_pfns[32] = {0}; 224 int i, ret, count; 225 bool hit = false; 226 227 if (amdgpu_ip_version(adev, UMC_HWIP, 0) < IP_VERSION(12, 0, 0)) 228 return 0; 229 230 if (amdgpu_sriov_vf(adev)) { 231 if (amdgpu_virt_check_vf_critical_region(adev, address, &hit)) 232 return -EPERM; 233 return hit ? -EACCES : 0; 234 } 235 236 if ((address >= adev->gmc.mc_vram_size) || 237 (address >= RAS_UMC_INJECT_ADDR_LIMIT)) 238 return -EFAULT; 239 240 count = amdgpu_umc_lookup_bad_pages_in_a_row(adev, 241 address, page_pfns, ARRAY_SIZE(page_pfns)); 242 if (count <= 0) 243 return -EPERM; 244 245 for (i = 0; i < count; i++) { 246 memset(&blk_info, 0, sizeof(blk_info)); 247 ret = amdgpu_vram_mgr_query_address_block_info(&adev->mman.vram_mgr, 248 page_pfns[i] << AMDGPU_GPU_PAGE_SHIFT, &blk_info); 249 if (!ret) { 250 /* The input address that needs to be checked is allocated by 251 * current calling process, so it is necessary to exclude 252 * the calling process. 253 */ 254 if ((flags == BYPASS_ALLOCATED_ADDRESS) && 255 ((blk_info.task.pid != task_pid_nr(current)) || 256 strncmp(blk_info.task.comm, current->comm, TASK_COMM_LEN))) 257 return -EACCES; 258 else if ((flags == BYPASS_INITIALIZATION_ADDRESS) && 259 (blk_info.task.pid == con->init_task_pid) && 260 !strncmp(blk_info.task.comm, con->init_task_comm, TASK_COMM_LEN)) 261 return -EACCES; 262 } 263 } 264 265 return 0; 266 } 267 268 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf, 269 size_t size, loff_t *pos) 270 { 271 struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private; 272 struct ras_query_if info = { 273 .head = obj->head, 274 }; 275 ssize_t s; 276 char val[128]; 277 278 if (amdgpu_ras_query_error_status(obj->adev, &info)) 279 return -EINVAL; 280 281 /* Hardware counter will be reset automatically after the query on Vega20 and Arcturus */ 282 if (amdgpu_ip_version(obj->adev, MP0_HWIP, 0) != IP_VERSION(11, 0, 2) && 283 amdgpu_ip_version(obj->adev, MP0_HWIP, 0) != IP_VERSION(11, 0, 4)) { 284 if (amdgpu_ras_reset_error_status(obj->adev, info.head.block)) 285 dev_warn(obj->adev->dev, "Failed to reset error counter and error status"); 286 } 287 288 s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n", 289 "ue", info.ue_count, 290 "ce", info.ce_count); 291 if (*pos >= s) 292 return 0; 293 294 s -= *pos; 295 s = min_t(u64, s, size); 296 297 298 if (copy_to_user(buf, &val[*pos], s)) 299 return -EINVAL; 300 301 *pos += s; 302 303 return s; 304 } 305 306 static const struct file_operations amdgpu_ras_debugfs_ops = { 307 .owner = THIS_MODULE, 308 .read = amdgpu_ras_debugfs_read, 309 .write = NULL, 310 .llseek = default_llseek 311 }; 312 313 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id) 314 { 315 int i; 316 317 for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) { 318 *block_id = i; 319 if (strcmp(name, ras_block_string[i]) == 0) 320 return 0; 321 } 322 return -EINVAL; 323 } 324 325 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f, 326 const char __user *buf, size_t size, 327 loff_t *pos, struct ras_debug_if *data) 328 { 329 ssize_t s = min_t(u64, 64, size); 330 char str[65]; 331 char block_name[33]; 332 char err[9] = "ue"; 333 int op = -1; 334 int block_id; 335 uint32_t sub_block; 336 u64 address, value; 337 /* default value is 0 if the mask is not set by user */ 338 u32 instance_mask = 0; 339 340 if (*pos) 341 return -EINVAL; 342 *pos = size; 343 344 memset(str, 0, sizeof(str)); 345 memset(data, 0, sizeof(*data)); 346 347 if (copy_from_user(str, buf, s)) 348 return -EINVAL; 349 350 if (sscanf(str, "disable %32s", block_name) == 1) 351 op = 0; 352 else if (sscanf(str, "enable %32s %8s", block_name, err) == 2) 353 op = 1; 354 else if (sscanf(str, "inject %32s %8s", block_name, err) == 2) 355 op = 2; 356 else if (strstr(str, "retire_page") != NULL) 357 op = 3; 358 else if (strstr(str, "check_address") != NULL) 359 op = 4; 360 else if (str[0] && str[1] && str[2] && str[3]) 361 /* ascii string, but commands are not matched. */ 362 return -EINVAL; 363 364 if (op != -1) { 365 if (op == 3) { 366 if (sscanf(str, "%*s 0x%llx", &address) != 1 && 367 sscanf(str, "%*s %llu", &address) != 1) 368 return -EINVAL; 369 370 data->op = op; 371 data->inject.address = address; 372 373 return 0; 374 } else if (op == 4) { 375 if (sscanf(str, "%*s 0x%llx 0x%llx", &address, &value) != 2 && 376 sscanf(str, "%*s %llu %llu", &address, &value) != 2) 377 return -EINVAL; 378 379 data->op = op; 380 data->inject.address = address; 381 data->inject.value = value; 382 return 0; 383 } 384 385 if (amdgpu_ras_find_block_id_by_name(block_name, &block_id)) 386 return -EINVAL; 387 388 data->head.block = block_id; 389 /* only ue, ce and poison errors are supported */ 390 if (!memcmp("ue", err, 2)) 391 data->head.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE; 392 else if (!memcmp("ce", err, 2)) 393 data->head.type = AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE; 394 else if (!memcmp("poison", err, 6)) 395 data->head.type = AMDGPU_RAS_ERROR__POISON; 396 else 397 return -EINVAL; 398 399 data->op = op; 400 401 if (op == 2) { 402 if (sscanf(str, "%*s %*s %*s 0x%x 0x%llx 0x%llx 0x%x", 403 &sub_block, &address, &value, &instance_mask) != 4 && 404 sscanf(str, "%*s %*s %*s %u %llu %llu %u", 405 &sub_block, &address, &value, &instance_mask) != 4 && 406 sscanf(str, "%*s %*s %*s 0x%x 0x%llx 0x%llx", 407 &sub_block, &address, &value) != 3 && 408 sscanf(str, "%*s %*s %*s %u %llu %llu", 409 &sub_block, &address, &value) != 3) 410 return -EINVAL; 411 data->head.sub_block_index = sub_block; 412 data->inject.address = address; 413 data->inject.value = value; 414 data->inject.instance_mask = instance_mask; 415 } 416 } else { 417 if (size < sizeof(*data)) 418 return -EINVAL; 419 420 if (copy_from_user(data, buf, sizeof(*data))) 421 return -EINVAL; 422 } 423 424 return 0; 425 } 426 427 static void amdgpu_ras_instance_mask_check(struct amdgpu_device *adev, 428 struct ras_debug_if *data) 429 { 430 int num_xcc = adev->gfx.xcc_mask ? NUM_XCC(adev->gfx.xcc_mask) : 1; 431 uint32_t mask, inst_mask = data->inject.instance_mask; 432 433 /* no need to set instance mask if there is only one instance */ 434 if (num_xcc <= 1 && inst_mask) { 435 data->inject.instance_mask = 0; 436 dev_dbg(adev->dev, 437 "RAS inject mask(0x%x) isn't supported and force it to 0.\n", 438 inst_mask); 439 440 return; 441 } 442 443 switch (data->head.block) { 444 case AMDGPU_RAS_BLOCK__GFX: 445 mask = GENMASK(num_xcc - 1, 0); 446 break; 447 case AMDGPU_RAS_BLOCK__SDMA: 448 mask = GENMASK(adev->sdma.num_instances - 1, 0); 449 break; 450 case AMDGPU_RAS_BLOCK__VCN: 451 case AMDGPU_RAS_BLOCK__JPEG: 452 mask = GENMASK(adev->vcn.num_vcn_inst - 1, 0); 453 break; 454 default: 455 mask = inst_mask; 456 break; 457 } 458 459 /* remove invalid bits in instance mask */ 460 data->inject.instance_mask &= mask; 461 if (inst_mask != data->inject.instance_mask) 462 dev_dbg(adev->dev, 463 "Adjust RAS inject mask 0x%x to 0x%x\n", 464 inst_mask, data->inject.instance_mask); 465 } 466 467 /** 468 * DOC: AMDGPU RAS debugfs control interface 469 * 470 * The control interface accepts struct ras_debug_if which has two members. 471 * 472 * First member: ras_debug_if::head or ras_debug_if::inject. 473 * 474 * head is used to indicate which IP block will be under control. 475 * 476 * head has four members, they are block, type, sub_block_index, name. 477 * block: which IP will be under control. 478 * type: what kind of error will be enabled/disabled/injected. 479 * sub_block_index: some IPs have subcomponets. say, GFX, sDMA. 480 * name: the name of IP. 481 * 482 * inject has three more members than head, they are address, value and mask. 483 * As their names indicate, inject operation will write the 484 * value to the address. 485 * 486 * The second member: struct ras_debug_if::op. 487 * It has three kinds of operations. 488 * 489 * - 0: disable RAS on the block. Take ::head as its data. 490 * - 1: enable RAS on the block. Take ::head as its data. 491 * - 2: inject errors on the block. Take ::inject as its data. 492 * 493 * How to use the interface? 494 * 495 * In a program 496 * 497 * Copy the struct ras_debug_if in your code and initialize it. 498 * Write the struct to the control interface. 499 * 500 * From shell 501 * 502 * .. code-block:: bash 503 * 504 * echo "disable <block>" > /sys/kernel/debug/dri/<N>/ras/ras_ctrl 505 * echo "enable <block> <error>" > /sys/kernel/debug/dri/<N>/ras/ras_ctrl 506 * echo "inject <block> <error> <sub-block> <address> <value> <mask>" > /sys/kernel/debug/dri/<N>/ras/ras_ctrl 507 * 508 * Where N, is the card which you want to affect. 509 * 510 * "disable" requires only the block. 511 * "enable" requires the block and error type. 512 * "inject" requires the block, error type, address, and value. 513 * 514 * The block is one of: umc, sdma, gfx, etc. 515 * see ras_block_string[] for details 516 * 517 * The error type is one of: ue, ce and poison where, 518 * ue is multi-uncorrectable 519 * ce is single-correctable 520 * poison is poison 521 * 522 * The sub-block is a the sub-block index, pass 0 if there is no sub-block. 523 * The address and value are hexadecimal numbers, leading 0x is optional. 524 * The mask means instance mask, is optional, default value is 0x1. 525 * 526 * For instance, 527 * 528 * .. code-block:: bash 529 * 530 * echo inject umc ue 0x0 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl 531 * echo inject umc ce 0 0 0 3 > /sys/kernel/debug/dri/0/ras/ras_ctrl 532 * echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl 533 * 534 * How to check the result of the operation? 535 * 536 * To check disable/enable, see "ras" features at, 537 * /sys/class/drm/card[0/1/2...]/device/ras/features 538 * 539 * To check inject, see the corresponding error count at, 540 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx|sdma|umc|...]_err_count 541 * 542 * .. note:: 543 * Operations are only allowed on blocks which are supported. 544 * Check the "ras" mask at /sys/module/amdgpu/parameters/ras_mask 545 * to see which blocks support RAS on a particular asic. 546 * 547 */ 548 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f, 549 const char __user *buf, 550 size_t size, loff_t *pos) 551 { 552 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private; 553 struct ras_debug_if data; 554 int ret = 0; 555 556 if (!amdgpu_ras_get_error_query_ready(adev)) { 557 dev_warn(adev->dev, "RAS WARN: error injection " 558 "currently inaccessible\n"); 559 return size; 560 } 561 562 ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data); 563 if (ret) 564 return ret; 565 566 if (data.op == 3) { 567 ret = amdgpu_reserve_page_direct(adev, data.inject.address); 568 if (!ret) 569 return size; 570 else 571 return ret; 572 } else if (data.op == 4) { 573 ret = amdgpu_check_address_validity(adev, data.inject.address, data.inject.value); 574 return ret ? ret : size; 575 } 576 577 if (!amdgpu_ras_is_supported(adev, data.head.block)) 578 return -EINVAL; 579 580 switch (data.op) { 581 case 0: 582 ret = amdgpu_ras_feature_enable(adev, &data.head, 0); 583 break; 584 case 1: 585 ret = amdgpu_ras_feature_enable(adev, &data.head, 1); 586 break; 587 case 2: 588 /* umc ce/ue error injection for a bad page is not allowed */ 589 if (data.head.block == AMDGPU_RAS_BLOCK__UMC) 590 ret = amdgpu_ras_check_bad_page(adev, data.inject.address); 591 if (ret == -EINVAL) { 592 dev_warn(adev->dev, "RAS WARN: input address 0x%llx is invalid.", 593 data.inject.address); 594 break; 595 } else if (ret == 1) { 596 dev_warn(adev->dev, "RAS WARN: inject: 0x%llx has already been marked as bad!\n", 597 data.inject.address); 598 break; 599 } 600 601 amdgpu_ras_instance_mask_check(adev, &data); 602 603 /* data.inject.address is offset instead of absolute gpu address */ 604 ret = amdgpu_ras_error_inject(adev, &data.inject); 605 break; 606 default: 607 ret = -EINVAL; 608 break; 609 } 610 611 if (ret) 612 return ret; 613 614 return size; 615 } 616 617 static int amdgpu_uniras_clear_badpages_info(struct amdgpu_device *adev); 618 619 /** 620 * DOC: AMDGPU RAS debugfs EEPROM table reset interface 621 * 622 * Some boards contain an EEPROM which is used to persistently store a list of 623 * bad pages which experiences ECC errors in vram. This interface provides 624 * a way to reset the EEPROM, e.g., after testing error injection. 625 * 626 * Usage: 627 * 628 * .. code-block:: bash 629 * 630 * echo 1 > ../ras/ras_eeprom_reset 631 * 632 * will reset EEPROM table to 0 entries. 633 * 634 */ 635 static ssize_t amdgpu_ras_debugfs_eeprom_write(struct file *f, 636 const char __user *buf, 637 size_t size, loff_t *pos) 638 { 639 struct amdgpu_device *adev = 640 (struct amdgpu_device *)file_inode(f)->i_private; 641 int ret; 642 643 if (amdgpu_uniras_enabled(adev)) { 644 ret = amdgpu_uniras_clear_badpages_info(adev); 645 return ret ? ret : size; 646 } 647 648 ret = amdgpu_ras_eeprom_reset_table( 649 &(amdgpu_ras_get_context(adev)->eeprom_control)); 650 651 if (!ret) { 652 /* Something was written to EEPROM. 653 */ 654 amdgpu_ras_get_context(adev)->flags = RAS_DEFAULT_FLAGS; 655 return size; 656 } else { 657 return ret; 658 } 659 } 660 661 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = { 662 .owner = THIS_MODULE, 663 .read = NULL, 664 .write = amdgpu_ras_debugfs_ctrl_write, 665 .llseek = default_llseek 666 }; 667 668 static const struct file_operations amdgpu_ras_debugfs_eeprom_ops = { 669 .owner = THIS_MODULE, 670 .read = NULL, 671 .write = amdgpu_ras_debugfs_eeprom_write, 672 .llseek = default_llseek 673 }; 674 675 /** 676 * DOC: AMDGPU RAS sysfs Error Count Interface 677 * 678 * It allows the user to read the error count for each IP block on the gpu through 679 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count 680 * 681 * It outputs the multiple lines which report the uncorrected (ue) and corrected 682 * (ce) error counts. 683 * 684 * The format of one line is below, 685 * 686 * [ce|ue]: count 687 * 688 * Example: 689 * 690 * .. code-block:: bash 691 * 692 * ue: 0 693 * ce: 1 694 * 695 */ 696 static ssize_t amdgpu_ras_sysfs_read(struct device *dev, 697 struct device_attribute *attr, char *buf) 698 { 699 struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr); 700 struct ras_query_if info = { 701 .head = obj->head, 702 }; 703 704 if (!amdgpu_ras_get_error_query_ready(obj->adev)) 705 return sysfs_emit(buf, "Query currently inaccessible\n"); 706 707 if (amdgpu_ras_query_error_status(obj->adev, &info)) 708 return -EINVAL; 709 710 if (amdgpu_ip_version(obj->adev, MP0_HWIP, 0) != IP_VERSION(11, 0, 2) && 711 amdgpu_ip_version(obj->adev, MP0_HWIP, 0) != IP_VERSION(11, 0, 4)) { 712 if (amdgpu_ras_reset_error_status(obj->adev, info.head.block)) 713 dev_warn(obj->adev->dev, "Failed to reset error counter and error status"); 714 } 715 716 if (info.head.block == AMDGPU_RAS_BLOCK__UMC) 717 return sysfs_emit(buf, "%s: %lu\n%s: %lu\n%s: %lu\n", "ue", info.ue_count, 718 "ce", info.ce_count, "de", info.de_count); 719 else 720 return sysfs_emit(buf, "%s: %lu\n%s: %lu\n", "ue", info.ue_count, 721 "ce", info.ce_count); 722 } 723 724 /* obj begin */ 725 726 #define get_obj(obj) do { (obj)->use++; } while (0) 727 #define alive_obj(obj) ((obj)->use) 728 729 static inline void put_obj(struct ras_manager *obj) 730 { 731 if (obj && (--obj->use == 0)) { 732 list_del(&obj->node); 733 amdgpu_ras_error_data_fini(&obj->err_data); 734 } 735 736 if (obj && (obj->use < 0)) 737 DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", get_ras_block_str(&obj->head)); 738 } 739 740 /* make one obj and return it. */ 741 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev, 742 struct ras_common_if *head) 743 { 744 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 745 struct ras_manager *obj; 746 747 if (!adev->ras_enabled || !con) 748 return NULL; 749 750 if (head->block >= AMDGPU_RAS_BLOCK_COUNT) 751 return NULL; 752 753 if (head->block == AMDGPU_RAS_BLOCK__MCA) { 754 if (head->sub_block_index >= AMDGPU_RAS_MCA_BLOCK__LAST) 755 return NULL; 756 757 obj = &con->objs[AMDGPU_RAS_BLOCK__LAST + head->sub_block_index]; 758 } else 759 obj = &con->objs[head->block]; 760 761 /* already exist. return obj? */ 762 if (alive_obj(obj)) 763 return NULL; 764 765 if (amdgpu_ras_error_data_init(&obj->err_data)) 766 return NULL; 767 768 obj->head = *head; 769 obj->adev = adev; 770 list_add(&obj->node, &con->head); 771 get_obj(obj); 772 773 return obj; 774 } 775 776 /* return an obj equal to head, or the first when head is NULL */ 777 struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev, 778 struct ras_common_if *head) 779 { 780 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 781 struct ras_manager *obj; 782 int i; 783 784 if (!adev->ras_enabled || !con) 785 return NULL; 786 787 if (head) { 788 if (head->block >= AMDGPU_RAS_BLOCK_COUNT) 789 return NULL; 790 791 if (head->block == AMDGPU_RAS_BLOCK__MCA) { 792 if (head->sub_block_index >= AMDGPU_RAS_MCA_BLOCK__LAST) 793 return NULL; 794 795 obj = &con->objs[AMDGPU_RAS_BLOCK__LAST + head->sub_block_index]; 796 } else 797 obj = &con->objs[head->block]; 798 799 if (alive_obj(obj)) 800 return obj; 801 } else { 802 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT + AMDGPU_RAS_MCA_BLOCK_COUNT; i++) { 803 obj = &con->objs[i]; 804 if (alive_obj(obj)) 805 return obj; 806 } 807 } 808 809 return NULL; 810 } 811 /* obj end */ 812 813 /* feature ctl begin */ 814 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev, 815 struct ras_common_if *head) 816 { 817 return adev->ras_hw_enabled & BIT(head->block); 818 } 819 820 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev, 821 struct ras_common_if *head) 822 { 823 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 824 825 return con->features & BIT(head->block); 826 } 827 828 /* 829 * if obj is not created, then create one. 830 * set feature enable flag. 831 */ 832 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev, 833 struct ras_common_if *head, int enable) 834 { 835 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 836 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head); 837 838 /* If hardware does not support ras, then do not create obj. 839 * But if hardware support ras, we can create the obj. 840 * Ras framework checks con->hw_supported to see if it need do 841 * corresponding initialization. 842 * IP checks con->support to see if it need disable ras. 843 */ 844 if (!amdgpu_ras_is_feature_allowed(adev, head)) 845 return 0; 846 847 if (enable) { 848 if (!obj) { 849 obj = amdgpu_ras_create_obj(adev, head); 850 if (!obj) 851 return -EINVAL; 852 } else { 853 /* In case we create obj somewhere else */ 854 get_obj(obj); 855 } 856 con->features |= BIT(head->block); 857 } else { 858 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) { 859 con->features &= ~BIT(head->block); 860 put_obj(obj); 861 } 862 } 863 864 return 0; 865 } 866 867 /* wrapper of psp_ras_enable_features */ 868 int amdgpu_ras_feature_enable(struct amdgpu_device *adev, 869 struct ras_common_if *head, bool enable) 870 { 871 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 872 union ta_ras_cmd_input *info; 873 int ret; 874 875 if (!con) 876 return -EINVAL; 877 878 /* For non-gfx ip, do not enable ras feature if it is not allowed */ 879 /* For gfx ip, regardless of feature support status, */ 880 /* Force issue enable or disable ras feature commands */ 881 if (head->block != AMDGPU_RAS_BLOCK__GFX && 882 !amdgpu_ras_is_feature_allowed(adev, head)) 883 return 0; 884 885 /* Only enable gfx ras feature from host side */ 886 if (head->block == AMDGPU_RAS_BLOCK__GFX && 887 !amdgpu_sriov_vf(adev) && 888 !amdgpu_ras_intr_triggered()) { 889 info = kzalloc(sizeof(union ta_ras_cmd_input), GFP_KERNEL); 890 if (!info) 891 return -ENOMEM; 892 893 if (!enable) { 894 info->disable_features = (struct ta_ras_disable_features_input) { 895 .block_id = amdgpu_ras_block_to_ta(head->block), 896 .error_type = amdgpu_ras_error_to_ta(head->type), 897 }; 898 } else { 899 info->enable_features = (struct ta_ras_enable_features_input) { 900 .block_id = amdgpu_ras_block_to_ta(head->block), 901 .error_type = amdgpu_ras_error_to_ta(head->type), 902 }; 903 } 904 905 ret = psp_ras_enable_features(&adev->psp, info, enable); 906 if (ret) { 907 dev_err(adev->dev, "ras %s %s failed poison:%d ret:%d\n", 908 enable ? "enable":"disable", 909 get_ras_block_str(head), 910 amdgpu_ras_is_poison_mode_supported(adev), ret); 911 kfree(info); 912 return ret; 913 } 914 915 kfree(info); 916 } 917 918 /* setup the obj */ 919 __amdgpu_ras_feature_enable(adev, head, enable); 920 921 return 0; 922 } 923 924 /* Only used in device probe stage and called only once. */ 925 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev, 926 struct ras_common_if *head, bool enable) 927 { 928 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 929 int ret; 930 931 if (!con) 932 return -EINVAL; 933 934 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) { 935 if (enable) { 936 /* There is no harm to issue a ras TA cmd regardless of 937 * the currecnt ras state. 938 * If current state == target state, it will do nothing 939 * But sometimes it requests driver to reset and repost 940 * with error code -EAGAIN. 941 */ 942 ret = amdgpu_ras_feature_enable(adev, head, 1); 943 /* With old ras TA, we might fail to enable ras. 944 * Log it and just setup the object. 945 * TODO need remove this WA in the future. 946 */ 947 if (ret == -EINVAL) { 948 ret = __amdgpu_ras_feature_enable(adev, head, 1); 949 if (!ret) 950 dev_info(adev->dev, 951 "RAS INFO: %s setup object\n", 952 get_ras_block_str(head)); 953 } 954 } else { 955 /* setup the object then issue a ras TA disable cmd.*/ 956 ret = __amdgpu_ras_feature_enable(adev, head, 1); 957 if (ret) 958 return ret; 959 960 /* gfx block ras disable cmd must send to ras-ta */ 961 if (head->block == AMDGPU_RAS_BLOCK__GFX) 962 con->features |= BIT(head->block); 963 964 ret = amdgpu_ras_feature_enable(adev, head, 0); 965 966 /* clean gfx block ras features flag */ 967 if (adev->ras_enabled && head->block == AMDGPU_RAS_BLOCK__GFX) 968 con->features &= ~BIT(head->block); 969 } 970 } else 971 ret = amdgpu_ras_feature_enable(adev, head, enable); 972 973 return ret; 974 } 975 976 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev, 977 bool bypass) 978 { 979 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 980 struct ras_manager *obj, *tmp; 981 982 list_for_each_entry_safe(obj, tmp, &con->head, node) { 983 /* bypass psp. 984 * aka just release the obj and corresponding flags 985 */ 986 if (bypass) { 987 if (__amdgpu_ras_feature_enable(adev, &obj->head, 0)) 988 break; 989 } else { 990 if (amdgpu_ras_feature_enable(adev, &obj->head, 0)) 991 break; 992 } 993 } 994 995 return con->features; 996 } 997 998 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev, 999 bool bypass) 1000 { 1001 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1002 int i; 1003 const enum amdgpu_ras_error_type default_ras_type = AMDGPU_RAS_ERROR__NONE; 1004 1005 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) { 1006 struct ras_common_if head = { 1007 .block = i, 1008 .type = default_ras_type, 1009 .sub_block_index = 0, 1010 }; 1011 1012 if (i == AMDGPU_RAS_BLOCK__MCA) 1013 continue; 1014 1015 if (bypass) { 1016 /* 1017 * bypass psp. vbios enable ras for us. 1018 * so just create the obj 1019 */ 1020 if (__amdgpu_ras_feature_enable(adev, &head, 1)) 1021 break; 1022 } else { 1023 if (amdgpu_ras_feature_enable(adev, &head, 1)) 1024 break; 1025 } 1026 } 1027 1028 for (i = 0; i < AMDGPU_RAS_MCA_BLOCK_COUNT; i++) { 1029 struct ras_common_if head = { 1030 .block = AMDGPU_RAS_BLOCK__MCA, 1031 .type = default_ras_type, 1032 .sub_block_index = i, 1033 }; 1034 1035 if (bypass) { 1036 /* 1037 * bypass psp. vbios enable ras for us. 1038 * so just create the obj 1039 */ 1040 if (__amdgpu_ras_feature_enable(adev, &head, 1)) 1041 break; 1042 } else { 1043 if (amdgpu_ras_feature_enable(adev, &head, 1)) 1044 break; 1045 } 1046 } 1047 1048 return con->features; 1049 } 1050 /* feature ctl end */ 1051 1052 static int amdgpu_ras_block_match_default(struct amdgpu_ras_block_object *block_obj, 1053 enum amdgpu_ras_block block) 1054 { 1055 if (!block_obj) 1056 return -EINVAL; 1057 1058 if (block_obj->ras_comm.block == block) 1059 return 0; 1060 1061 return -EINVAL; 1062 } 1063 1064 static struct amdgpu_ras_block_object *amdgpu_ras_get_ras_block(struct amdgpu_device *adev, 1065 enum amdgpu_ras_block block, uint32_t sub_block_index) 1066 { 1067 struct amdgpu_ras_block_list *node, *tmp; 1068 struct amdgpu_ras_block_object *obj; 1069 1070 if (block >= AMDGPU_RAS_BLOCK__LAST) 1071 return NULL; 1072 1073 list_for_each_entry_safe(node, tmp, &adev->ras_list, node) { 1074 if (!node->ras_obj) { 1075 dev_warn(adev->dev, "Warning: abnormal ras list node.\n"); 1076 continue; 1077 } 1078 1079 obj = node->ras_obj; 1080 if (obj->ras_block_match) { 1081 if (obj->ras_block_match(obj, block, sub_block_index) == 0) 1082 return obj; 1083 } else { 1084 if (amdgpu_ras_block_match_default(obj, block) == 0) 1085 return obj; 1086 } 1087 } 1088 1089 return NULL; 1090 } 1091 1092 static void amdgpu_ras_get_ecc_info(struct amdgpu_device *adev, struct ras_err_data *err_data) 1093 { 1094 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev); 1095 int ret = 0; 1096 1097 /* 1098 * choosing right query method according to 1099 * whether smu support query error information 1100 */ 1101 ret = amdgpu_dpm_get_ecc_info(adev, (void *)&(ras->umc_ecc)); 1102 if (ret == -EOPNOTSUPP) { 1103 if (adev->umc.ras && adev->umc.ras->ras_block.hw_ops && 1104 adev->umc.ras->ras_block.hw_ops->query_ras_error_count) 1105 adev->umc.ras->ras_block.hw_ops->query_ras_error_count(adev, err_data); 1106 1107 /* umc query_ras_error_address is also responsible for clearing 1108 * error status 1109 */ 1110 if (adev->umc.ras && adev->umc.ras->ras_block.hw_ops && 1111 adev->umc.ras->ras_block.hw_ops->query_ras_error_address) 1112 adev->umc.ras->ras_block.hw_ops->query_ras_error_address(adev, err_data); 1113 } else if (!ret) { 1114 if (adev->umc.ras && 1115 adev->umc.ras->ecc_info_query_ras_error_count) 1116 adev->umc.ras->ecc_info_query_ras_error_count(adev, err_data); 1117 1118 if (adev->umc.ras && 1119 adev->umc.ras->ecc_info_query_ras_error_address) 1120 adev->umc.ras->ecc_info_query_ras_error_address(adev, err_data); 1121 } 1122 } 1123 1124 static void amdgpu_ras_error_print_error_data(struct amdgpu_device *adev, 1125 struct ras_manager *ras_mgr, 1126 struct ras_err_data *err_data, 1127 struct ras_query_context *qctx, 1128 const char *blk_name, 1129 bool is_ue, 1130 bool is_de) 1131 { 1132 struct amdgpu_smuio_mcm_config_info *mcm_info; 1133 struct ras_err_node *err_node; 1134 struct ras_err_info *err_info; 1135 u64 event_id = qctx->evid.event_id; 1136 1137 if (is_ue) { 1138 for_each_ras_error(err_node, err_data) { 1139 err_info = &err_node->err_info; 1140 mcm_info = &err_info->mcm_info; 1141 if (err_info->ue_count) { 1142 RAS_EVENT_LOG(adev, event_id, "socket: %d, die: %d, " 1143 "%lld new uncorrectable hardware errors detected in %s block\n", 1144 mcm_info->socket_id, 1145 mcm_info->die_id, 1146 err_info->ue_count, 1147 blk_name); 1148 } 1149 } 1150 1151 for_each_ras_error(err_node, &ras_mgr->err_data) { 1152 err_info = &err_node->err_info; 1153 mcm_info = &err_info->mcm_info; 1154 RAS_EVENT_LOG(adev, event_id, "socket: %d, die: %d, " 1155 "%lld uncorrectable hardware errors detected in total in %s block\n", 1156 mcm_info->socket_id, mcm_info->die_id, err_info->ue_count, blk_name); 1157 } 1158 1159 } else { 1160 if (is_de) { 1161 for_each_ras_error(err_node, err_data) { 1162 err_info = &err_node->err_info; 1163 mcm_info = &err_info->mcm_info; 1164 if (err_info->de_count) { 1165 RAS_EVENT_LOG(adev, event_id, "socket: %d, die: %d, " 1166 "%lld new deferred hardware errors detected in %s block\n", 1167 mcm_info->socket_id, 1168 mcm_info->die_id, 1169 err_info->de_count, 1170 blk_name); 1171 } 1172 } 1173 1174 for_each_ras_error(err_node, &ras_mgr->err_data) { 1175 err_info = &err_node->err_info; 1176 mcm_info = &err_info->mcm_info; 1177 RAS_EVENT_LOG(adev, event_id, "socket: %d, die: %d, " 1178 "%lld deferred hardware errors detected in total in %s block\n", 1179 mcm_info->socket_id, mcm_info->die_id, 1180 err_info->de_count, blk_name); 1181 } 1182 } else { 1183 if (adev->debug_disable_ce_logs) 1184 return; 1185 1186 for_each_ras_error(err_node, err_data) { 1187 err_info = &err_node->err_info; 1188 mcm_info = &err_info->mcm_info; 1189 if (err_info->ce_count) { 1190 RAS_EVENT_LOG(adev, event_id, "socket: %d, die: %d, " 1191 "%lld new correctable hardware errors detected in %s block\n", 1192 mcm_info->socket_id, 1193 mcm_info->die_id, 1194 err_info->ce_count, 1195 blk_name); 1196 } 1197 } 1198 1199 for_each_ras_error(err_node, &ras_mgr->err_data) { 1200 err_info = &err_node->err_info; 1201 mcm_info = &err_info->mcm_info; 1202 RAS_EVENT_LOG(adev, event_id, "socket: %d, die: %d, " 1203 "%lld correctable hardware errors detected in total in %s block\n", 1204 mcm_info->socket_id, mcm_info->die_id, 1205 err_info->ce_count, blk_name); 1206 } 1207 } 1208 } 1209 } 1210 1211 static inline bool err_data_has_source_info(struct ras_err_data *data) 1212 { 1213 return !list_empty(&data->err_node_list); 1214 } 1215 1216 static void amdgpu_ras_error_generate_report(struct amdgpu_device *adev, 1217 struct ras_query_if *query_if, 1218 struct ras_err_data *err_data, 1219 struct ras_query_context *qctx) 1220 { 1221 struct ras_manager *ras_mgr = amdgpu_ras_find_obj(adev, &query_if->head); 1222 const char *blk_name = get_ras_block_str(&query_if->head); 1223 u64 event_id = qctx->evid.event_id; 1224 1225 if (err_data->ce_count) { 1226 if (err_data_has_source_info(err_data)) { 1227 amdgpu_ras_error_print_error_data(adev, ras_mgr, err_data, qctx, 1228 blk_name, false, false); 1229 } else if (!adev->aid_mask && 1230 adev->smuio.funcs && 1231 adev->smuio.funcs->get_socket_id && 1232 adev->smuio.funcs->get_die_id) { 1233 RAS_EVENT_LOG(adev, event_id, "socket: %d, die: %d " 1234 "%ld correctable hardware errors " 1235 "detected in %s block\n", 1236 adev->smuio.funcs->get_socket_id(adev), 1237 adev->smuio.funcs->get_die_id(adev), 1238 ras_mgr->err_data.ce_count, 1239 blk_name); 1240 } else { 1241 RAS_EVENT_LOG(adev, event_id, "%ld correctable hardware errors " 1242 "detected in %s block\n", 1243 ras_mgr->err_data.ce_count, 1244 blk_name); 1245 } 1246 } 1247 1248 if (err_data->ue_count) { 1249 if (err_data_has_source_info(err_data)) { 1250 amdgpu_ras_error_print_error_data(adev, ras_mgr, err_data, qctx, 1251 blk_name, true, false); 1252 } else if (!adev->aid_mask && 1253 adev->smuio.funcs && 1254 adev->smuio.funcs->get_socket_id && 1255 adev->smuio.funcs->get_die_id) { 1256 RAS_EVENT_LOG(adev, event_id, "socket: %d, die: %d " 1257 "%ld uncorrectable hardware errors " 1258 "detected in %s block\n", 1259 adev->smuio.funcs->get_socket_id(adev), 1260 adev->smuio.funcs->get_die_id(adev), 1261 ras_mgr->err_data.ue_count, 1262 blk_name); 1263 } else { 1264 RAS_EVENT_LOG(adev, event_id, "%ld uncorrectable hardware errors " 1265 "detected in %s block\n", 1266 ras_mgr->err_data.ue_count, 1267 blk_name); 1268 } 1269 } 1270 1271 if (err_data->de_count) { 1272 if (err_data_has_source_info(err_data)) { 1273 amdgpu_ras_error_print_error_data(adev, ras_mgr, err_data, qctx, 1274 blk_name, false, true); 1275 } else if (!adev->aid_mask && 1276 adev->smuio.funcs && 1277 adev->smuio.funcs->get_socket_id && 1278 adev->smuio.funcs->get_die_id) { 1279 RAS_EVENT_LOG(adev, event_id, "socket: %d, die: %d " 1280 "%ld deferred hardware errors " 1281 "detected in %s block\n", 1282 adev->smuio.funcs->get_socket_id(adev), 1283 adev->smuio.funcs->get_die_id(adev), 1284 ras_mgr->err_data.de_count, 1285 blk_name); 1286 } else { 1287 RAS_EVENT_LOG(adev, event_id, "%ld deferred hardware errors " 1288 "detected in %s block\n", 1289 ras_mgr->err_data.de_count, 1290 blk_name); 1291 } 1292 } 1293 } 1294 1295 static void amdgpu_ras_virt_error_generate_report(struct amdgpu_device *adev, 1296 struct ras_query_if *query_if, 1297 struct ras_err_data *err_data, 1298 struct ras_query_context *qctx) 1299 { 1300 unsigned long new_ue, new_ce, new_de; 1301 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &query_if->head); 1302 const char *blk_name = get_ras_block_str(&query_if->head); 1303 u64 event_id = qctx->evid.event_id; 1304 1305 new_ce = err_data->ce_count - obj->err_data.ce_count; 1306 new_ue = err_data->ue_count - obj->err_data.ue_count; 1307 new_de = err_data->de_count - obj->err_data.de_count; 1308 1309 if (new_ce) { 1310 RAS_EVENT_LOG(adev, event_id, "%lu correctable hardware errors " 1311 "detected in %s block\n", 1312 new_ce, 1313 blk_name); 1314 } 1315 1316 if (new_ue) { 1317 RAS_EVENT_LOG(adev, event_id, "%lu uncorrectable hardware errors " 1318 "detected in %s block\n", 1319 new_ue, 1320 blk_name); 1321 } 1322 1323 if (new_de) { 1324 RAS_EVENT_LOG(adev, event_id, "%lu deferred hardware errors " 1325 "detected in %s block\n", 1326 new_de, 1327 blk_name); 1328 } 1329 } 1330 1331 static void amdgpu_rasmgr_error_data_statistic_update(struct ras_manager *obj, struct ras_err_data *err_data) 1332 { 1333 struct ras_err_node *err_node; 1334 struct ras_err_info *err_info; 1335 1336 if (err_data_has_source_info(err_data)) { 1337 for_each_ras_error(err_node, err_data) { 1338 err_info = &err_node->err_info; 1339 amdgpu_ras_error_statistic_de_count(&obj->err_data, 1340 &err_info->mcm_info, err_info->de_count); 1341 amdgpu_ras_error_statistic_ce_count(&obj->err_data, 1342 &err_info->mcm_info, err_info->ce_count); 1343 amdgpu_ras_error_statistic_ue_count(&obj->err_data, 1344 &err_info->mcm_info, err_info->ue_count); 1345 } 1346 } else { 1347 /* for legacy asic path which doesn't has error source info */ 1348 obj->err_data.ue_count += err_data->ue_count; 1349 obj->err_data.ce_count += err_data->ce_count; 1350 obj->err_data.de_count += err_data->de_count; 1351 } 1352 } 1353 1354 static void amdgpu_ras_mgr_virt_error_data_statistics_update(struct ras_manager *obj, 1355 struct ras_err_data *err_data) 1356 { 1357 /* Host reports absolute counts */ 1358 obj->err_data.ue_count = err_data->ue_count; 1359 obj->err_data.ce_count = err_data->ce_count; 1360 obj->err_data.de_count = err_data->de_count; 1361 } 1362 1363 static struct ras_manager *get_ras_manager(struct amdgpu_device *adev, enum amdgpu_ras_block blk) 1364 { 1365 struct ras_common_if head; 1366 1367 memset(&head, 0, sizeof(head)); 1368 head.block = blk; 1369 1370 return amdgpu_ras_find_obj(adev, &head); 1371 } 1372 1373 int amdgpu_ras_bind_aca(struct amdgpu_device *adev, enum amdgpu_ras_block blk, 1374 const struct aca_info *aca_info, void *data) 1375 { 1376 struct ras_manager *obj; 1377 1378 /* in resume phase, no need to create aca fs node */ 1379 if (adev->in_suspend || amdgpu_reset_in_recovery(adev)) 1380 return 0; 1381 1382 obj = get_ras_manager(adev, blk); 1383 if (!obj) 1384 return -EINVAL; 1385 1386 return amdgpu_aca_add_handle(adev, &obj->aca_handle, ras_block_str(blk), aca_info, data); 1387 } 1388 1389 int amdgpu_ras_unbind_aca(struct amdgpu_device *adev, enum amdgpu_ras_block blk) 1390 { 1391 struct ras_manager *obj; 1392 1393 obj = get_ras_manager(adev, blk); 1394 if (!obj) 1395 return -EINVAL; 1396 1397 amdgpu_aca_remove_handle(&obj->aca_handle); 1398 1399 return 0; 1400 } 1401 1402 static int amdgpu_aca_log_ras_error_data(struct amdgpu_device *adev, enum amdgpu_ras_block blk, 1403 enum aca_error_type type, struct ras_err_data *err_data, 1404 struct ras_query_context *qctx) 1405 { 1406 struct ras_manager *obj; 1407 1408 obj = get_ras_manager(adev, blk); 1409 if (!obj) 1410 return -EINVAL; 1411 1412 return amdgpu_aca_get_error_data(adev, &obj->aca_handle, type, err_data, qctx); 1413 } 1414 1415 ssize_t amdgpu_ras_aca_sysfs_read(struct device *dev, struct device_attribute *attr, 1416 struct aca_handle *handle, char *buf, void *data) 1417 { 1418 struct ras_manager *obj = container_of(handle, struct ras_manager, aca_handle); 1419 struct ras_query_if info = { 1420 .head = obj->head, 1421 }; 1422 1423 if (!amdgpu_ras_get_error_query_ready(obj->adev)) 1424 return sysfs_emit(buf, "Query currently inaccessible\n"); 1425 1426 if (amdgpu_ras_query_error_status(obj->adev, &info)) 1427 return -EINVAL; 1428 1429 return sysfs_emit(buf, "%s: %lu\n%s: %lu\n%s: %lu\n", "ue", info.ue_count, 1430 "ce", info.ce_count, "de", info.de_count); 1431 } 1432 1433 static int amdgpu_ras_query_error_status_helper(struct amdgpu_device *adev, 1434 struct ras_query_if *info, 1435 struct ras_err_data *err_data, 1436 struct ras_query_context *qctx, 1437 unsigned int error_query_mode) 1438 { 1439 enum amdgpu_ras_block blk = info ? info->head.block : AMDGPU_RAS_BLOCK_COUNT; 1440 struct amdgpu_ras_block_object *block_obj = NULL; 1441 int ret; 1442 1443 if (blk == AMDGPU_RAS_BLOCK_COUNT) 1444 return -EINVAL; 1445 1446 if (error_query_mode == AMDGPU_RAS_INVALID_ERROR_QUERY) 1447 return -EINVAL; 1448 1449 if (error_query_mode == AMDGPU_RAS_VIRT_ERROR_COUNT_QUERY) { 1450 return amdgpu_virt_req_ras_err_count(adev, blk, err_data); 1451 } else if (error_query_mode == AMDGPU_RAS_DIRECT_ERROR_QUERY) { 1452 if (info->head.block == AMDGPU_RAS_BLOCK__UMC) { 1453 amdgpu_ras_get_ecc_info(adev, err_data); 1454 } else { 1455 block_obj = amdgpu_ras_get_ras_block(adev, info->head.block, 0); 1456 if (!block_obj || !block_obj->hw_ops) { 1457 dev_dbg_once(adev->dev, "%s doesn't config RAS function\n", 1458 get_ras_block_str(&info->head)); 1459 return -EINVAL; 1460 } 1461 1462 if (block_obj->hw_ops->query_ras_error_count) 1463 block_obj->hw_ops->query_ras_error_count(adev, err_data); 1464 1465 if ((info->head.block == AMDGPU_RAS_BLOCK__SDMA) || 1466 (info->head.block == AMDGPU_RAS_BLOCK__GFX) || 1467 (info->head.block == AMDGPU_RAS_BLOCK__MMHUB)) { 1468 if (block_obj->hw_ops->query_ras_error_status) 1469 block_obj->hw_ops->query_ras_error_status(adev); 1470 } 1471 } 1472 } else { 1473 if (amdgpu_aca_is_enabled(adev)) { 1474 ret = amdgpu_aca_log_ras_error_data(adev, blk, ACA_ERROR_TYPE_UE, err_data, qctx); 1475 if (ret) 1476 return ret; 1477 1478 ret = amdgpu_aca_log_ras_error_data(adev, blk, ACA_ERROR_TYPE_CE, err_data, qctx); 1479 if (ret) 1480 return ret; 1481 1482 ret = amdgpu_aca_log_ras_error_data(adev, blk, ACA_ERROR_TYPE_DEFERRED, err_data, qctx); 1483 if (ret) 1484 return ret; 1485 } else { 1486 /* FIXME: add code to check return value later */ 1487 amdgpu_mca_smu_log_ras_error(adev, blk, AMDGPU_MCA_ERROR_TYPE_UE, err_data, qctx); 1488 amdgpu_mca_smu_log_ras_error(adev, blk, AMDGPU_MCA_ERROR_TYPE_CE, err_data, qctx); 1489 } 1490 } 1491 1492 return 0; 1493 } 1494 1495 /* query/inject/cure begin */ 1496 static int amdgpu_ras_query_error_status_with_event(struct amdgpu_device *adev, 1497 struct ras_query_if *info, 1498 enum ras_event_type type) 1499 { 1500 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head); 1501 struct ras_err_data err_data; 1502 struct ras_query_context qctx; 1503 unsigned int error_query_mode; 1504 int ret; 1505 1506 if (!obj) 1507 return -EINVAL; 1508 1509 ret = amdgpu_ras_error_data_init(&err_data); 1510 if (ret) 1511 return ret; 1512 1513 if (!amdgpu_ras_get_error_query_mode(adev, &error_query_mode)) 1514 return -EINVAL; 1515 1516 memset(&qctx, 0, sizeof(qctx)); 1517 qctx.evid.type = type; 1518 qctx.evid.event_id = amdgpu_ras_acquire_event_id(adev, type); 1519 1520 if (!down_read_trylock(&adev->reset_domain->sem)) { 1521 ret = -EIO; 1522 goto out_fini_err_data; 1523 } 1524 1525 ret = amdgpu_ras_query_error_status_helper(adev, info, 1526 &err_data, 1527 &qctx, 1528 error_query_mode); 1529 up_read(&adev->reset_domain->sem); 1530 if (ret) 1531 goto out_fini_err_data; 1532 1533 if (error_query_mode != AMDGPU_RAS_VIRT_ERROR_COUNT_QUERY) { 1534 amdgpu_rasmgr_error_data_statistic_update(obj, &err_data); 1535 amdgpu_ras_error_generate_report(adev, info, &err_data, &qctx); 1536 } else { 1537 /* Host provides absolute error counts. First generate the report 1538 * using the previous VF internal count against new host count. 1539 * Then Update VF internal count. 1540 */ 1541 amdgpu_ras_virt_error_generate_report(adev, info, &err_data, &qctx); 1542 amdgpu_ras_mgr_virt_error_data_statistics_update(obj, &err_data); 1543 } 1544 1545 info->ue_count = obj->err_data.ue_count; 1546 info->ce_count = obj->err_data.ce_count; 1547 info->de_count = obj->err_data.de_count; 1548 1549 out_fini_err_data: 1550 amdgpu_ras_error_data_fini(&err_data); 1551 1552 return ret; 1553 } 1554 1555 static int amdgpu_uniras_clear_badpages_info(struct amdgpu_device *adev) 1556 { 1557 struct ras_cmd_dev_handle req = {0}; 1558 int ret; 1559 1560 ret = amdgpu_ras_mgr_handle_ras_cmd(adev, RAS_CMD__CLEAR_BAD_PAGE_INFO, 1561 &req, sizeof(req), NULL, 0); 1562 if (ret) { 1563 dev_err(adev->dev, "Failed to clear bad pages info, ret: %d\n", ret); 1564 return ret; 1565 } 1566 1567 return 0; 1568 } 1569 1570 static int amdgpu_uniras_query_block_ecc(struct amdgpu_device *adev, 1571 struct ras_query_if *info) 1572 { 1573 struct ras_cmd_block_ecc_info_req req = {0}; 1574 struct ras_cmd_block_ecc_info_rsp rsp = {0}; 1575 int ret; 1576 1577 if (!info) 1578 return -EINVAL; 1579 1580 req.block_id = info->head.block; 1581 req.subblock_id = info->head.sub_block_index; 1582 1583 ret = amdgpu_ras_mgr_handle_ras_cmd(adev, RAS_CMD__GET_BLOCK_ECC_STATUS, 1584 &req, sizeof(req), &rsp, sizeof(rsp)); 1585 if (!ret) { 1586 info->ce_count = rsp.ce_count; 1587 info->ue_count = rsp.ue_count; 1588 info->de_count = rsp.de_count; 1589 } 1590 1591 return ret; 1592 } 1593 1594 int amdgpu_ras_query_error_status(struct amdgpu_device *adev, struct ras_query_if *info) 1595 { 1596 if (amdgpu_uniras_enabled(adev)) 1597 return amdgpu_uniras_query_block_ecc(adev, info); 1598 else 1599 return amdgpu_ras_query_error_status_with_event(adev, info, RAS_EVENT_TYPE_INVALID); 1600 } 1601 1602 int amdgpu_ras_reset_error_count(struct amdgpu_device *adev, 1603 enum amdgpu_ras_block block) 1604 { 1605 struct amdgpu_ras_block_object *block_obj = amdgpu_ras_get_ras_block(adev, block, 0); 1606 const struct amdgpu_mca_smu_funcs *mca_funcs = adev->mca.mca_funcs; 1607 const struct aca_smu_funcs *smu_funcs = adev->aca.smu_funcs; 1608 1609 if (!block_obj || !block_obj->hw_ops) { 1610 dev_dbg_once(adev->dev, "%s doesn't config RAS function\n", 1611 ras_block_str(block)); 1612 return -EOPNOTSUPP; 1613 } 1614 1615 if (!amdgpu_ras_is_supported(adev, block) || 1616 !amdgpu_ras_get_aca_debug_mode(adev)) 1617 return -EOPNOTSUPP; 1618 1619 if (amdgpu_sriov_vf(adev)) 1620 return -EOPNOTSUPP; 1621 1622 /* skip ras error reset in gpu reset */ 1623 if ((amdgpu_in_reset(adev) || amdgpu_ras_in_recovery(adev)) && 1624 ((smu_funcs && smu_funcs->set_debug_mode) || 1625 (mca_funcs && mca_funcs->mca_set_debug_mode))) 1626 return -EOPNOTSUPP; 1627 1628 if (block_obj->hw_ops->reset_ras_error_count) 1629 block_obj->hw_ops->reset_ras_error_count(adev); 1630 1631 return 0; 1632 } 1633 1634 int amdgpu_ras_reset_error_status(struct amdgpu_device *adev, 1635 enum amdgpu_ras_block block) 1636 { 1637 struct amdgpu_ras_block_object *block_obj = amdgpu_ras_get_ras_block(adev, block, 0); 1638 1639 if (amdgpu_ras_reset_error_count(adev, block) == -EOPNOTSUPP) 1640 return 0; 1641 1642 if ((block == AMDGPU_RAS_BLOCK__GFX) || 1643 (block == AMDGPU_RAS_BLOCK__MMHUB)) { 1644 if (block_obj->hw_ops->reset_ras_error_status) 1645 block_obj->hw_ops->reset_ras_error_status(adev); 1646 } 1647 1648 return 0; 1649 } 1650 1651 static int amdgpu_uniras_error_inject(struct amdgpu_device *adev, 1652 struct ras_inject_if *info) 1653 { 1654 struct ras_cmd_inject_error_req inject_req; 1655 struct ras_cmd_inject_error_rsp rsp; 1656 1657 if (!info) 1658 return -EINVAL; 1659 1660 memset(&inject_req, 0, sizeof(inject_req)); 1661 inject_req.block_id = info->head.block; 1662 inject_req.subblock_id = info->head.sub_block_index; 1663 inject_req.address = info->address; 1664 inject_req.error_type = info->head.type; 1665 inject_req.instance_mask = info->instance_mask; 1666 inject_req.method = info->value; 1667 1668 return amdgpu_ras_mgr_handle_ras_cmd(adev, RAS_CMD__INJECT_ERROR, 1669 &inject_req, sizeof(inject_req), &rsp, sizeof(rsp)); 1670 } 1671 1672 /* wrapper of psp_ras_trigger_error */ 1673 int amdgpu_ras_error_inject(struct amdgpu_device *adev, 1674 struct ras_inject_if *info) 1675 { 1676 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head); 1677 struct ta_ras_trigger_error_input block_info = { 1678 .block_id = amdgpu_ras_block_to_ta(info->head.block), 1679 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type), 1680 .sub_block_index = info->head.sub_block_index, 1681 .address = info->address, 1682 .value = info->value, 1683 }; 1684 int ret = -EINVAL; 1685 struct amdgpu_ras_block_object *block_obj = amdgpu_ras_get_ras_block(adev, 1686 info->head.block, 1687 info->head.sub_block_index); 1688 1689 if (amdgpu_uniras_enabled(adev)) 1690 return amdgpu_uniras_error_inject(adev, info); 1691 1692 /* inject on guest isn't allowed, return success directly */ 1693 if (amdgpu_sriov_vf(adev)) 1694 return 0; 1695 1696 if (!obj) 1697 return -EINVAL; 1698 1699 if (!block_obj || !block_obj->hw_ops) { 1700 dev_dbg_once(adev->dev, "%s doesn't config RAS function\n", 1701 get_ras_block_str(&info->head)); 1702 return -EINVAL; 1703 } 1704 1705 /* Calculate XGMI relative offset */ 1706 if (adev->gmc.xgmi.num_physical_nodes > 1 && 1707 info->head.block != AMDGPU_RAS_BLOCK__GFX) { 1708 block_info.address = 1709 amdgpu_xgmi_get_relative_phy_addr(adev, 1710 block_info.address); 1711 } 1712 1713 if (block_obj->hw_ops->ras_error_inject) { 1714 if (info->head.block == AMDGPU_RAS_BLOCK__GFX) 1715 ret = block_obj->hw_ops->ras_error_inject(adev, info, info->instance_mask); 1716 else /* Special ras_error_inject is defined (e.g: xgmi) */ 1717 ret = block_obj->hw_ops->ras_error_inject(adev, &block_info, 1718 info->instance_mask); 1719 } else { 1720 /* default path */ 1721 ret = psp_ras_trigger_error(&adev->psp, &block_info, info->instance_mask); 1722 } 1723 1724 if (ret) 1725 dev_err(adev->dev, "ras inject %s failed %d\n", 1726 get_ras_block_str(&info->head), ret); 1727 1728 return ret; 1729 } 1730 1731 /** 1732 * amdgpu_ras_query_error_count_helper -- Get error counter for specific IP 1733 * @adev: pointer to AMD GPU device 1734 * @ce_count: pointer to an integer to be set to the count of correctible errors. 1735 * @ue_count: pointer to an integer to be set to the count of uncorrectible errors. 1736 * @query_info: pointer to ras_query_if 1737 * 1738 * Return 0 for query success or do nothing, otherwise return an error 1739 * on failures 1740 */ 1741 static int amdgpu_ras_query_error_count_helper(struct amdgpu_device *adev, 1742 unsigned long *ce_count, 1743 unsigned long *ue_count, 1744 struct ras_query_if *query_info) 1745 { 1746 int ret; 1747 1748 if (!query_info) 1749 /* do nothing if query_info is not specified */ 1750 return 0; 1751 1752 ret = amdgpu_ras_query_error_status(adev, query_info); 1753 if (ret) 1754 return ret; 1755 1756 *ce_count += query_info->ce_count; 1757 *ue_count += query_info->ue_count; 1758 1759 /* some hardware/IP supports read to clear 1760 * no need to explictly reset the err status after the query call */ 1761 if (amdgpu_ip_version(adev, MP0_HWIP, 0) != IP_VERSION(11, 0, 2) && 1762 amdgpu_ip_version(adev, MP0_HWIP, 0) != IP_VERSION(11, 0, 4)) { 1763 if (amdgpu_ras_reset_error_status(adev, query_info->head.block)) 1764 dev_warn(adev->dev, 1765 "Failed to reset error counter and error status\n"); 1766 } 1767 1768 return 0; 1769 } 1770 1771 /** 1772 * amdgpu_ras_query_error_count -- Get error counts of all IPs or specific IP 1773 * @adev: pointer to AMD GPU device 1774 * @ce_count: pointer to an integer to be set to the count of correctible errors. 1775 * @ue_count: pointer to an integer to be set to the count of uncorrectible 1776 * errors. 1777 * @query_info: pointer to ras_query_if if the query request is only for 1778 * specific ip block; if info is NULL, then the qurey request is for 1779 * all the ip blocks that support query ras error counters/status 1780 * 1781 * If set, @ce_count or @ue_count, count and return the corresponding 1782 * error counts in those integer pointers. Return 0 if the device 1783 * supports RAS. Return -EOPNOTSUPP if the device doesn't support RAS. 1784 */ 1785 int amdgpu_ras_query_error_count(struct amdgpu_device *adev, 1786 unsigned long *ce_count, 1787 unsigned long *ue_count, 1788 struct ras_query_if *query_info) 1789 { 1790 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1791 struct ras_manager *obj; 1792 unsigned long ce, ue; 1793 int ret; 1794 1795 if (!adev->ras_enabled || !con) 1796 return -EOPNOTSUPP; 1797 1798 /* Don't count since no reporting. 1799 */ 1800 if (!ce_count && !ue_count) 1801 return 0; 1802 1803 ce = 0; 1804 ue = 0; 1805 if (!query_info) { 1806 /* query all the ip blocks that support ras query interface */ 1807 list_for_each_entry(obj, &con->head, node) { 1808 struct ras_query_if info = { 1809 .head = obj->head, 1810 }; 1811 1812 ret = amdgpu_ras_query_error_count_helper(adev, &ce, &ue, &info); 1813 } 1814 } else { 1815 /* query specific ip block */ 1816 ret = amdgpu_ras_query_error_count_helper(adev, &ce, &ue, query_info); 1817 } 1818 1819 if (ret) 1820 return ret; 1821 1822 if (ce_count) 1823 *ce_count = ce; 1824 1825 if (ue_count) 1826 *ue_count = ue; 1827 1828 return 0; 1829 } 1830 /* query/inject/cure end */ 1831 1832 1833 /* sysfs begin */ 1834 1835 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev, 1836 struct ras_badpage *bps, uint32_t count, uint32_t start); 1837 static int amdgpu_uniras_badpages_read(struct amdgpu_device *adev, 1838 struct ras_badpage *bps, uint32_t count, uint32_t start); 1839 1840 static char *amdgpu_ras_badpage_flags_str(unsigned int flags) 1841 { 1842 switch (flags) { 1843 case AMDGPU_RAS_RETIRE_PAGE_RESERVED: 1844 return "R"; 1845 case AMDGPU_RAS_RETIRE_PAGE_PENDING: 1846 return "P"; 1847 case AMDGPU_RAS_RETIRE_PAGE_FAULT: 1848 default: 1849 return "F"; 1850 } 1851 } 1852 1853 /** 1854 * DOC: AMDGPU RAS sysfs gpu_vram_bad_pages Interface 1855 * 1856 * It allows user to read the bad pages of vram on the gpu through 1857 * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages 1858 * 1859 * It outputs multiple lines, and each line stands for one gpu page. 1860 * 1861 * The format of one line is below, 1862 * gpu pfn : gpu page size : flags 1863 * 1864 * gpu pfn and gpu page size are printed in hex format. 1865 * flags can be one of below character, 1866 * 1867 * R: reserved, this gpu page is reserved and not able to use. 1868 * 1869 * P: pending for reserve, this gpu page is marked as bad, will be reserved 1870 * in next window of page_reserve. 1871 * 1872 * F: unable to reserve. this gpu page can't be reserved due to some reasons. 1873 * 1874 * Examples: 1875 * 1876 * .. code-block:: bash 1877 * 1878 * 0x00000001 : 0x00001000 : R 1879 * 0x00000002 : 0x00001000 : P 1880 * 1881 */ 1882 1883 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f, 1884 struct kobject *kobj, const struct bin_attribute *attr, 1885 char *buf, loff_t ppos, size_t count) 1886 { 1887 struct amdgpu_ras *con = 1888 container_of(attr, struct amdgpu_ras, badpages_attr); 1889 struct amdgpu_device *adev = con->adev; 1890 const unsigned int element_size = 1891 sizeof("0xabcdabcd : 0x12345678 : R\n") - 1; 1892 unsigned int start = div64_ul(ppos + element_size - 1, element_size); 1893 unsigned int end = div64_ul(ppos + count - 1, element_size); 1894 ssize_t s = 0; 1895 struct ras_badpage *bps = NULL; 1896 int bps_count = 0, i, status; 1897 uint64_t address; 1898 1899 memset(buf, 0, count); 1900 1901 bps_count = end - start; 1902 bps = kmalloc_array(bps_count, sizeof(*bps), GFP_KERNEL); 1903 if (!bps) 1904 return 0; 1905 1906 memset(bps, 0, sizeof(*bps) * bps_count); 1907 1908 if (amdgpu_uniras_enabled(adev)) 1909 bps_count = amdgpu_uniras_badpages_read(adev, bps, bps_count, start); 1910 else 1911 bps_count = amdgpu_ras_badpages_read(adev, bps, bps_count, start); 1912 1913 if (bps_count <= 0) { 1914 kfree(bps); 1915 return 0; 1916 } 1917 1918 for (i = 0; i < bps_count; i++) { 1919 address = ((uint64_t)bps[i].bp) << AMDGPU_GPU_PAGE_SHIFT; 1920 if (amdgpu_ras_check_critical_address(adev, address)) 1921 continue; 1922 1923 bps[i].size = AMDGPU_GPU_PAGE_SIZE; 1924 1925 status = amdgpu_vram_mgr_query_page_status(&adev->mman.vram_mgr, 1926 address); 1927 if (status == -EBUSY) 1928 bps[i].flags = AMDGPU_RAS_RETIRE_PAGE_PENDING; 1929 else if (status == -ENOENT) 1930 bps[i].flags = AMDGPU_RAS_RETIRE_PAGE_FAULT; 1931 else 1932 bps[i].flags = AMDGPU_RAS_RETIRE_PAGE_RESERVED; 1933 1934 s += scnprintf(&buf[s], element_size + 1, 1935 "0x%08x : 0x%08x : %1s\n", 1936 bps[i].bp, 1937 bps[i].size, 1938 amdgpu_ras_badpage_flags_str(bps[i].flags)); 1939 } 1940 1941 kfree(bps); 1942 1943 return s; 1944 } 1945 1946 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev, 1947 struct device_attribute *attr, char *buf) 1948 { 1949 struct amdgpu_ras *con = 1950 container_of(attr, struct amdgpu_ras, features_attr); 1951 1952 return sysfs_emit(buf, "feature mask: 0x%x\n", con->features); 1953 } 1954 1955 static bool amdgpu_ras_get_version_info(struct amdgpu_device *adev, u32 *major, 1956 u32 *minor, u32 *rev) 1957 { 1958 int i; 1959 1960 if (!adev || !major || !minor || !rev || !amdgpu_uniras_enabled(adev)) 1961 return false; 1962 1963 for (i = 0; i < adev->num_ip_blocks; i++) { 1964 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_RAS) { 1965 *major = adev->ip_blocks[i].version->major; 1966 *minor = adev->ip_blocks[i].version->minor; 1967 *rev = adev->ip_blocks[i].version->rev; 1968 return true; 1969 } 1970 } 1971 1972 return false; 1973 } 1974 1975 static ssize_t amdgpu_ras_sysfs_version_show(struct device *dev, 1976 struct device_attribute *attr, char *buf) 1977 { 1978 struct amdgpu_ras *con = 1979 container_of(attr, struct amdgpu_ras, version_attr); 1980 u32 major, minor, rev; 1981 ssize_t size = 0; 1982 1983 size += sysfs_emit_at(buf, size, "table version: 0x%x\n", 1984 con->eeprom_control.tbl_hdr.version); 1985 1986 if (amdgpu_ras_get_version_info(con->adev, &major, &minor, &rev)) 1987 size += sysfs_emit_at(buf, size, "ras version: %u.%u.%u\n", 1988 major, minor, rev); 1989 1990 return size; 1991 } 1992 1993 static ssize_t amdgpu_ras_sysfs_schema_show(struct device *dev, 1994 struct device_attribute *attr, char *buf) 1995 { 1996 struct amdgpu_ras *con = 1997 container_of(attr, struct amdgpu_ras, schema_attr); 1998 return sysfs_emit(buf, "schema: 0x%x\n", con->schema); 1999 } 2000 2001 static struct { 2002 enum ras_event_type type; 2003 const char *name; 2004 } dump_event[] = { 2005 {RAS_EVENT_TYPE_FATAL, "Fatal Error"}, 2006 {RAS_EVENT_TYPE_POISON_CREATION, "Poison Creation"}, 2007 {RAS_EVENT_TYPE_POISON_CONSUMPTION, "Poison Consumption"}, 2008 }; 2009 2010 static ssize_t amdgpu_ras_sysfs_event_state_show(struct device *dev, 2011 struct device_attribute *attr, char *buf) 2012 { 2013 struct amdgpu_ras *con = 2014 container_of(attr, struct amdgpu_ras, event_state_attr); 2015 struct ras_event_manager *event_mgr = con->event_mgr; 2016 struct ras_event_state *event_state; 2017 int i, size = 0; 2018 2019 if (!event_mgr) 2020 return -EINVAL; 2021 2022 size += sysfs_emit_at(buf, size, "current seqno: %llu\n", atomic64_read(&event_mgr->seqno)); 2023 for (i = 0; i < ARRAY_SIZE(dump_event); i++) { 2024 event_state = &event_mgr->event_state[dump_event[i].type]; 2025 size += sysfs_emit_at(buf, size, "%s: count:%llu, last_seqno:%llu\n", 2026 dump_event[i].name, 2027 atomic64_read(&event_state->count), 2028 event_state->last_seqno); 2029 } 2030 2031 return (ssize_t)size; 2032 } 2033 2034 static void amdgpu_ras_sysfs_remove_bad_page_node(struct amdgpu_device *adev) 2035 { 2036 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 2037 2038 if (adev->dev->kobj.sd) 2039 sysfs_remove_file_from_group(&adev->dev->kobj, 2040 &con->badpages_attr.attr, 2041 RAS_FS_NAME); 2042 } 2043 2044 static int amdgpu_ras_sysfs_remove_dev_attr_node(struct amdgpu_device *adev) 2045 { 2046 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 2047 struct attribute *attrs[] = { 2048 &con->features_attr.attr, 2049 &con->version_attr.attr, 2050 &con->schema_attr.attr, 2051 &con->event_state_attr.attr, 2052 NULL 2053 }; 2054 struct attribute_group group = { 2055 .name = RAS_FS_NAME, 2056 .attrs = attrs, 2057 }; 2058 2059 if (adev->dev->kobj.sd) 2060 sysfs_remove_group(&adev->dev->kobj, &group); 2061 2062 return 0; 2063 } 2064 2065 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev, 2066 struct ras_common_if *head) 2067 { 2068 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head); 2069 2070 if (amdgpu_aca_is_enabled(adev)) 2071 return 0; 2072 2073 if (!obj || obj->attr_inuse) 2074 return -EINVAL; 2075 2076 if (amdgpu_sriov_vf(adev) && !amdgpu_virt_ras_telemetry_block_en(adev, head->block)) 2077 return 0; 2078 2079 get_obj(obj); 2080 2081 snprintf(obj->fs_data.sysfs_name, sizeof(obj->fs_data.sysfs_name), 2082 "%s_err_count", head->name); 2083 2084 obj->sysfs_attr = (struct device_attribute){ 2085 .attr = { 2086 .name = obj->fs_data.sysfs_name, 2087 .mode = S_IRUGO, 2088 }, 2089 .show = amdgpu_ras_sysfs_read, 2090 }; 2091 sysfs_attr_init(&obj->sysfs_attr.attr); 2092 2093 if (sysfs_add_file_to_group(&adev->dev->kobj, 2094 &obj->sysfs_attr.attr, 2095 RAS_FS_NAME)) { 2096 put_obj(obj); 2097 return -EINVAL; 2098 } 2099 2100 obj->attr_inuse = 1; 2101 2102 return 0; 2103 } 2104 2105 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev, 2106 struct ras_common_if *head) 2107 { 2108 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head); 2109 2110 if (amdgpu_aca_is_enabled(adev)) 2111 return 0; 2112 2113 if (!obj || !obj->attr_inuse) 2114 return -EINVAL; 2115 2116 if (adev->dev->kobj.sd) 2117 sysfs_remove_file_from_group(&adev->dev->kobj, 2118 &obj->sysfs_attr.attr, 2119 RAS_FS_NAME); 2120 obj->attr_inuse = 0; 2121 put_obj(obj); 2122 2123 return 0; 2124 } 2125 2126 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev) 2127 { 2128 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 2129 struct ras_manager *obj, *tmp; 2130 2131 list_for_each_entry_safe(obj, tmp, &con->head, node) { 2132 amdgpu_ras_sysfs_remove(adev, &obj->head); 2133 } 2134 2135 if (amdgpu_bad_page_threshold != 0) 2136 amdgpu_ras_sysfs_remove_bad_page_node(adev); 2137 2138 amdgpu_ras_sysfs_remove_dev_attr_node(adev); 2139 2140 return 0; 2141 } 2142 /* sysfs end */ 2143 2144 /** 2145 * DOC: AMDGPU RAS Reboot Behavior for Unrecoverable Errors 2146 * 2147 * Normally when there is an uncorrectable error, the driver will reset 2148 * the GPU to recover. However, in the event of an unrecoverable error, 2149 * the driver provides an interface to reboot the system automatically 2150 * in that event. 2151 * 2152 * The following file in debugfs provides that interface: 2153 * /sys/kernel/debug/dri/[0/1/2...]/ras/auto_reboot 2154 * 2155 * Usage: 2156 * 2157 * .. code-block:: bash 2158 * 2159 * echo true > .../ras/auto_reboot 2160 * 2161 */ 2162 /* debugfs begin */ 2163 static struct dentry *amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev) 2164 { 2165 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 2166 struct amdgpu_ras_eeprom_control *eeprom = &con->eeprom_control; 2167 struct drm_minor *minor = adev_to_drm(adev)->primary; 2168 struct dentry *dir; 2169 2170 dir = debugfs_create_dir(RAS_FS_NAME, minor->debugfs_root); 2171 debugfs_create_file("ras_ctrl", S_IWUGO | S_IRUGO, dir, adev, 2172 &amdgpu_ras_debugfs_ctrl_ops); 2173 debugfs_create_file("ras_eeprom_reset", S_IWUGO | S_IRUGO, dir, adev, 2174 &amdgpu_ras_debugfs_eeprom_ops); 2175 debugfs_create_u32("bad_page_cnt_threshold", 0444, dir, 2176 &con->bad_page_cnt_threshold); 2177 debugfs_create_u32("ras_num_recs", 0444, dir, &eeprom->ras_num_recs); 2178 debugfs_create_x32("ras_hw_enabled", 0444, dir, &adev->ras_hw_enabled); 2179 debugfs_create_x32("ras_enabled", 0444, dir, &adev->ras_enabled); 2180 debugfs_create_file("ras_eeprom_size", S_IRUGO, dir, adev, 2181 &amdgpu_ras_debugfs_eeprom_size_ops); 2182 con->de_ras_eeprom_table = debugfs_create_file("ras_eeprom_table", 2183 S_IRUGO, dir, adev, 2184 &amdgpu_ras_debugfs_eeprom_table_ops); 2185 amdgpu_ras_debugfs_set_ret_size(&con->eeprom_control); 2186 2187 /* 2188 * After one uncorrectable error happens, usually GPU recovery will 2189 * be scheduled. But due to the known problem in GPU recovery failing 2190 * to bring GPU back, below interface provides one direct way to 2191 * user to reboot system automatically in such case within 2192 * ERREVENT_ATHUB_INTERRUPT generated. Normal GPU recovery routine 2193 * will never be called. 2194 */ 2195 debugfs_create_bool("auto_reboot", S_IWUGO | S_IRUGO, dir, &con->reboot); 2196 2197 /* 2198 * User could set this not to clean up hardware's error count register 2199 * of RAS IPs during ras recovery. 2200 */ 2201 debugfs_create_bool("disable_ras_err_cnt_harvest", 0644, dir, 2202 &con->disable_ras_err_cnt_harvest); 2203 return dir; 2204 } 2205 2206 static void amdgpu_ras_debugfs_create(struct amdgpu_device *adev, 2207 struct ras_fs_if *head, 2208 struct dentry *dir) 2209 { 2210 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head); 2211 2212 if (!obj || !dir) 2213 return; 2214 2215 get_obj(obj); 2216 2217 memcpy(obj->fs_data.debugfs_name, 2218 head->debugfs_name, 2219 sizeof(obj->fs_data.debugfs_name)); 2220 2221 debugfs_create_file(obj->fs_data.debugfs_name, S_IWUGO | S_IRUGO, dir, 2222 obj, &amdgpu_ras_debugfs_ops); 2223 } 2224 2225 static bool amdgpu_ras_aca_is_supported(struct amdgpu_device *adev) 2226 { 2227 bool ret; 2228 2229 switch (amdgpu_ip_version(adev, MP0_HWIP, 0)) { 2230 case IP_VERSION(13, 0, 6): 2231 case IP_VERSION(13, 0, 12): 2232 case IP_VERSION(13, 0, 14): 2233 ret = true; 2234 break; 2235 default: 2236 ret = false; 2237 break; 2238 } 2239 2240 return ret; 2241 } 2242 2243 void amdgpu_ras_debugfs_create_all(struct amdgpu_device *adev) 2244 { 2245 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 2246 struct dentry *dir; 2247 struct ras_manager *obj; 2248 struct ras_fs_if fs_info; 2249 2250 /* 2251 * it won't be called in resume path, no need to check 2252 * suspend and gpu reset status 2253 */ 2254 if (!IS_ENABLED(CONFIG_DEBUG_FS) || !con) 2255 return; 2256 2257 dir = amdgpu_ras_debugfs_create_ctrl_node(adev); 2258 2259 list_for_each_entry(obj, &con->head, node) { 2260 if (amdgpu_ras_is_supported(adev, obj->head.block) && 2261 (obj->attr_inuse == 1)) { 2262 sprintf(fs_info.debugfs_name, "%s_err_inject", 2263 get_ras_block_str(&obj->head)); 2264 fs_info.head = obj->head; 2265 amdgpu_ras_debugfs_create(adev, &fs_info, dir); 2266 } 2267 } 2268 2269 if (amdgpu_ras_aca_is_supported(adev)) { 2270 if (amdgpu_aca_is_enabled(adev)) 2271 amdgpu_aca_smu_debugfs_init(adev, dir); 2272 else 2273 amdgpu_mca_smu_debugfs_init(adev, dir); 2274 } 2275 } 2276 2277 /* debugfs end */ 2278 2279 /* ras fs */ 2280 static const BIN_ATTR(gpu_vram_bad_pages, S_IRUGO, 2281 amdgpu_ras_sysfs_badpages_read, NULL, 0); 2282 static DEVICE_ATTR(features, S_IRUGO, 2283 amdgpu_ras_sysfs_features_read, NULL); 2284 static DEVICE_ATTR(version, 0444, 2285 amdgpu_ras_sysfs_version_show, NULL); 2286 static DEVICE_ATTR(schema, 0444, 2287 amdgpu_ras_sysfs_schema_show, NULL); 2288 static DEVICE_ATTR(event_state, 0444, 2289 amdgpu_ras_sysfs_event_state_show, NULL); 2290 static int amdgpu_ras_fs_init(struct amdgpu_device *adev) 2291 { 2292 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 2293 struct attribute_group group = { 2294 .name = RAS_FS_NAME, 2295 }; 2296 struct attribute *attrs[] = { 2297 &con->features_attr.attr, 2298 &con->version_attr.attr, 2299 &con->schema_attr.attr, 2300 &con->event_state_attr.attr, 2301 NULL 2302 }; 2303 const struct bin_attribute *bin_attrs[] = { 2304 NULL, 2305 NULL, 2306 }; 2307 int r; 2308 2309 group.attrs = attrs; 2310 2311 /* add features entry */ 2312 con->features_attr = dev_attr_features; 2313 sysfs_attr_init(attrs[0]); 2314 2315 /* add version entry */ 2316 con->version_attr = dev_attr_version; 2317 sysfs_attr_init(attrs[1]); 2318 2319 /* add schema entry */ 2320 con->schema_attr = dev_attr_schema; 2321 sysfs_attr_init(attrs[2]); 2322 2323 /* add event_state entry */ 2324 con->event_state_attr = dev_attr_event_state; 2325 sysfs_attr_init(attrs[3]); 2326 2327 if (amdgpu_bad_page_threshold != 0) { 2328 /* add bad_page_features entry */ 2329 con->badpages_attr = bin_attr_gpu_vram_bad_pages; 2330 sysfs_bin_attr_init(&con->badpages_attr); 2331 bin_attrs[0] = &con->badpages_attr; 2332 group.bin_attrs = bin_attrs; 2333 } 2334 2335 r = sysfs_create_group(&adev->dev->kobj, &group); 2336 if (r) 2337 dev_err(adev->dev, "Failed to create RAS sysfs group!"); 2338 2339 return 0; 2340 } 2341 2342 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev) 2343 { 2344 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 2345 struct ras_manager *con_obj, *ip_obj, *tmp; 2346 2347 if (IS_ENABLED(CONFIG_DEBUG_FS)) { 2348 list_for_each_entry_safe(con_obj, tmp, &con->head, node) { 2349 ip_obj = amdgpu_ras_find_obj(adev, &con_obj->head); 2350 if (ip_obj) 2351 put_obj(ip_obj); 2352 } 2353 } 2354 2355 amdgpu_ras_sysfs_remove_all(adev); 2356 return 0; 2357 } 2358 /* ras fs end */ 2359 2360 /* ih begin */ 2361 2362 /* For the hardware that cannot enable bif ring for both ras_controller_irq 2363 * and ras_err_evnet_athub_irq ih cookies, the driver has to poll status 2364 * register to check whether the interrupt is triggered or not, and properly 2365 * ack the interrupt if it is there 2366 */ 2367 void amdgpu_ras_interrupt_fatal_error_handler(struct amdgpu_device *adev) 2368 { 2369 /* Fatal error events are handled on host side */ 2370 if (amdgpu_sriov_vf(adev)) 2371 return; 2372 /* 2373 * If the current interrupt is caused by a non-fatal RAS error, skip 2374 * check for fatal error. For fatal errors, FED status of all devices 2375 * in XGMI hive gets set when the first device gets fatal error 2376 * interrupt. The error gets propagated to other devices as well, so 2377 * make sure to ack the interrupt regardless of FED status. 2378 */ 2379 if (!amdgpu_ras_get_fed_status(adev) && 2380 amdgpu_ras_is_err_state(adev, AMDGPU_RAS_BLOCK__ANY)) 2381 return; 2382 2383 if (amdgpu_uniras_enabled(adev)) { 2384 amdgpu_ras_mgr_handle_fatal_interrupt(adev, NULL); 2385 return; 2386 } 2387 2388 if (adev->nbio.ras && 2389 adev->nbio.ras->handle_ras_controller_intr_no_bifring) 2390 adev->nbio.ras->handle_ras_controller_intr_no_bifring(adev); 2391 2392 if (adev->nbio.ras && 2393 adev->nbio.ras->handle_ras_err_event_athub_intr_no_bifring) 2394 adev->nbio.ras->handle_ras_err_event_athub_intr_no_bifring(adev); 2395 } 2396 2397 static void amdgpu_ras_interrupt_poison_consumption_handler(struct ras_manager *obj, 2398 struct amdgpu_iv_entry *entry) 2399 { 2400 bool poison_stat = false; 2401 struct amdgpu_device *adev = obj->adev; 2402 struct amdgpu_ras_block_object *block_obj = 2403 amdgpu_ras_get_ras_block(adev, obj->head.block, 0); 2404 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 2405 enum ras_event_type type = RAS_EVENT_TYPE_POISON_CONSUMPTION; 2406 u64 event_id; 2407 int ret; 2408 2409 if (!block_obj || !con) 2410 return; 2411 2412 ret = amdgpu_ras_mark_ras_event(adev, type); 2413 if (ret) 2414 return; 2415 2416 amdgpu_ras_set_err_poison(adev, block_obj->ras_comm.block); 2417 /* both query_poison_status and handle_poison_consumption are optional, 2418 * but at least one of them should be implemented if we need poison 2419 * consumption handler 2420 */ 2421 if (block_obj->hw_ops && block_obj->hw_ops->query_poison_status) { 2422 poison_stat = block_obj->hw_ops->query_poison_status(adev); 2423 if (!poison_stat) { 2424 /* Not poison consumption interrupt, no need to handle it */ 2425 dev_info(adev->dev, "No RAS poison status in %s poison IH.\n", 2426 block_obj->ras_comm.name); 2427 2428 return; 2429 } 2430 } 2431 2432 amdgpu_umc_poison_handler(adev, obj->head.block, 0); 2433 2434 if (block_obj->hw_ops && block_obj->hw_ops->handle_poison_consumption) 2435 poison_stat = block_obj->hw_ops->handle_poison_consumption(adev); 2436 2437 /* gpu reset is fallback for failed and default cases. 2438 * For RMA case, amdgpu_umc_poison_handler will handle gpu reset. 2439 */ 2440 if (poison_stat && !amdgpu_ras_is_rma(adev)) { 2441 event_id = amdgpu_ras_acquire_event_id(adev, type); 2442 RAS_EVENT_LOG(adev, event_id, 2443 "GPU reset for %s RAS poison consumption is issued!\n", 2444 block_obj->ras_comm.name); 2445 amdgpu_ras_reset_gpu(adev); 2446 } 2447 2448 if (!poison_stat) 2449 amdgpu_gfx_poison_consumption_handler(adev, entry); 2450 } 2451 2452 static void amdgpu_ras_interrupt_poison_creation_handler(struct ras_manager *obj, 2453 struct amdgpu_iv_entry *entry) 2454 { 2455 struct amdgpu_device *adev = obj->adev; 2456 enum ras_event_type type = RAS_EVENT_TYPE_POISON_CREATION; 2457 u64 event_id; 2458 int ret; 2459 2460 ret = amdgpu_ras_mark_ras_event(adev, type); 2461 if (ret) 2462 return; 2463 2464 event_id = amdgpu_ras_acquire_event_id(adev, type); 2465 RAS_EVENT_LOG(adev, event_id, "Poison is created\n"); 2466 2467 if (amdgpu_ip_version(obj->adev, UMC_HWIP, 0) >= IP_VERSION(12, 0, 0)) { 2468 struct amdgpu_ras *con = amdgpu_ras_get_context(obj->adev); 2469 2470 atomic_inc(&con->page_retirement_req_cnt); 2471 atomic_inc(&con->poison_creation_count); 2472 2473 wake_up(&con->page_retirement_wq); 2474 } 2475 } 2476 2477 static void amdgpu_ras_interrupt_umc_handler(struct ras_manager *obj, 2478 struct amdgpu_iv_entry *entry) 2479 { 2480 struct ras_ih_data *data = &obj->ih_data; 2481 struct ras_err_data err_data; 2482 int ret; 2483 2484 if (!data->cb) 2485 return; 2486 2487 ret = amdgpu_ras_error_data_init(&err_data); 2488 if (ret) 2489 return; 2490 2491 /* Let IP handle its data, maybe we need get the output 2492 * from the callback to update the error type/count, etc 2493 */ 2494 amdgpu_ras_set_fed(obj->adev, true); 2495 ret = data->cb(obj->adev, &err_data, entry); 2496 /* ue will trigger an interrupt, and in that case 2497 * we need do a reset to recovery the whole system. 2498 * But leave IP do that recovery, here we just dispatch 2499 * the error. 2500 */ 2501 if (ret == AMDGPU_RAS_SUCCESS) { 2502 /* these counts could be left as 0 if 2503 * some blocks do not count error number 2504 */ 2505 obj->err_data.ue_count += err_data.ue_count; 2506 obj->err_data.ce_count += err_data.ce_count; 2507 obj->err_data.de_count += err_data.de_count; 2508 } 2509 2510 amdgpu_ras_error_data_fini(&err_data); 2511 } 2512 2513 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj) 2514 { 2515 struct ras_ih_data *data = &obj->ih_data; 2516 struct amdgpu_iv_entry entry; 2517 2518 while (data->rptr != data->wptr) { 2519 rmb(); 2520 memcpy(&entry, &data->ring[data->rptr], 2521 data->element_size); 2522 2523 wmb(); 2524 data->rptr = (data->aligned_element_size + 2525 data->rptr) % data->ring_size; 2526 2527 if (amdgpu_ras_is_poison_mode_supported(obj->adev)) { 2528 if (obj->head.block == AMDGPU_RAS_BLOCK__UMC) 2529 amdgpu_ras_interrupt_poison_creation_handler(obj, &entry); 2530 else 2531 amdgpu_ras_interrupt_poison_consumption_handler(obj, &entry); 2532 } else { 2533 if (obj->head.block == AMDGPU_RAS_BLOCK__UMC) 2534 amdgpu_ras_interrupt_umc_handler(obj, &entry); 2535 else 2536 dev_warn(obj->adev->dev, 2537 "No RAS interrupt handler for non-UMC block with poison disabled.\n"); 2538 } 2539 } 2540 } 2541 2542 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work) 2543 { 2544 struct ras_ih_data *data = 2545 container_of(work, struct ras_ih_data, ih_work); 2546 struct ras_manager *obj = 2547 container_of(data, struct ras_manager, ih_data); 2548 2549 amdgpu_ras_interrupt_handler(obj); 2550 } 2551 2552 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev, 2553 struct ras_dispatch_if *info) 2554 { 2555 struct ras_manager *obj; 2556 struct ras_ih_data *data; 2557 2558 if (amdgpu_uniras_enabled(adev)) { 2559 struct ras_ih_info ih_info; 2560 2561 memset(&ih_info, 0, sizeof(ih_info)); 2562 ih_info.block = info->head.block; 2563 memcpy(&ih_info.iv_entry, info->entry, sizeof(struct amdgpu_iv_entry)); 2564 2565 return amdgpu_ras_mgr_handle_controller_interrupt(adev, &ih_info); 2566 } 2567 2568 obj = amdgpu_ras_find_obj(adev, &info->head); 2569 if (!obj) 2570 return -EINVAL; 2571 2572 data = &obj->ih_data; 2573 2574 if (data->inuse == 0) 2575 return 0; 2576 2577 /* Might be overflow... */ 2578 memcpy(&data->ring[data->wptr], info->entry, 2579 data->element_size); 2580 2581 wmb(); 2582 data->wptr = (data->aligned_element_size + 2583 data->wptr) % data->ring_size; 2584 2585 schedule_work(&data->ih_work); 2586 2587 return 0; 2588 } 2589 2590 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev, 2591 struct ras_common_if *head) 2592 { 2593 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head); 2594 struct ras_ih_data *data; 2595 2596 if (!obj) 2597 return -EINVAL; 2598 2599 data = &obj->ih_data; 2600 if (data->inuse == 0) 2601 return 0; 2602 2603 cancel_work_sync(&data->ih_work); 2604 2605 kfree(data->ring); 2606 memset(data, 0, sizeof(*data)); 2607 put_obj(obj); 2608 2609 return 0; 2610 } 2611 2612 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev, 2613 struct ras_common_if *head) 2614 { 2615 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head); 2616 struct ras_ih_data *data; 2617 struct amdgpu_ras_block_object *ras_obj; 2618 2619 if (!obj) { 2620 /* in case we registe the IH before enable ras feature */ 2621 obj = amdgpu_ras_create_obj(adev, head); 2622 if (!obj) 2623 return -EINVAL; 2624 } else 2625 get_obj(obj); 2626 2627 ras_obj = container_of(head, struct amdgpu_ras_block_object, ras_comm); 2628 2629 data = &obj->ih_data; 2630 /* add the callback.etc */ 2631 *data = (struct ras_ih_data) { 2632 .inuse = 0, 2633 .cb = ras_obj->ras_cb, 2634 .element_size = sizeof(struct amdgpu_iv_entry), 2635 .rptr = 0, 2636 .wptr = 0, 2637 }; 2638 2639 INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler); 2640 2641 data->aligned_element_size = ALIGN(data->element_size, 8); 2642 /* the ring can store 64 iv entries. */ 2643 data->ring_size = 64 * data->aligned_element_size; 2644 data->ring = kmalloc(data->ring_size, GFP_KERNEL); 2645 if (!data->ring) { 2646 put_obj(obj); 2647 return -ENOMEM; 2648 } 2649 2650 /* IH is ready */ 2651 data->inuse = 1; 2652 2653 return 0; 2654 } 2655 2656 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev) 2657 { 2658 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 2659 struct ras_manager *obj, *tmp; 2660 2661 list_for_each_entry_safe(obj, tmp, &con->head, node) { 2662 amdgpu_ras_interrupt_remove_handler(adev, &obj->head); 2663 } 2664 2665 return 0; 2666 } 2667 /* ih end */ 2668 2669 /* traversal all IPs except NBIO to query error counter */ 2670 static void amdgpu_ras_log_on_err_counter(struct amdgpu_device *adev, enum ras_event_type type) 2671 { 2672 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 2673 struct ras_manager *obj; 2674 2675 if (!adev->ras_enabled || !con) 2676 return; 2677 2678 list_for_each_entry(obj, &con->head, node) { 2679 struct ras_query_if info = { 2680 .head = obj->head, 2681 }; 2682 2683 /* 2684 * PCIE_BIF IP has one different isr by ras controller 2685 * interrupt, the specific ras counter query will be 2686 * done in that isr. So skip such block from common 2687 * sync flood interrupt isr calling. 2688 */ 2689 if (info.head.block == AMDGPU_RAS_BLOCK__PCIE_BIF) 2690 continue; 2691 2692 /* 2693 * this is a workaround for aldebaran, skip send msg to 2694 * smu to get ecc_info table due to smu handle get ecc 2695 * info table failed temporarily. 2696 * should be removed until smu fix handle ecc_info table. 2697 */ 2698 if ((info.head.block == AMDGPU_RAS_BLOCK__UMC) && 2699 (amdgpu_ip_version(adev, MP1_HWIP, 0) == 2700 IP_VERSION(13, 0, 2))) 2701 continue; 2702 2703 amdgpu_ras_query_error_status_with_event(adev, &info, type); 2704 2705 if (amdgpu_ip_version(adev, MP0_HWIP, 0) != 2706 IP_VERSION(11, 0, 2) && 2707 amdgpu_ip_version(adev, MP0_HWIP, 0) != 2708 IP_VERSION(11, 0, 4) && 2709 amdgpu_ip_version(adev, MP0_HWIP, 0) != 2710 IP_VERSION(13, 0, 0)) { 2711 if (amdgpu_ras_reset_error_status(adev, info.head.block)) 2712 dev_warn(adev->dev, "Failed to reset error counter and error status"); 2713 } 2714 } 2715 } 2716 2717 /* Parse RdRspStatus and WrRspStatus */ 2718 static void amdgpu_ras_error_status_query(struct amdgpu_device *adev, 2719 struct ras_query_if *info) 2720 { 2721 struct amdgpu_ras_block_object *block_obj; 2722 /* 2723 * Only two block need to query read/write 2724 * RspStatus at current state 2725 */ 2726 if ((info->head.block != AMDGPU_RAS_BLOCK__GFX) && 2727 (info->head.block != AMDGPU_RAS_BLOCK__MMHUB)) 2728 return; 2729 2730 block_obj = amdgpu_ras_get_ras_block(adev, 2731 info->head.block, 2732 info->head.sub_block_index); 2733 2734 if (!block_obj || !block_obj->hw_ops) { 2735 dev_dbg_once(adev->dev, "%s doesn't config RAS function\n", 2736 get_ras_block_str(&info->head)); 2737 return; 2738 } 2739 2740 if (block_obj->hw_ops->query_ras_error_status) 2741 block_obj->hw_ops->query_ras_error_status(adev); 2742 2743 } 2744 2745 static void amdgpu_ras_query_err_status(struct amdgpu_device *adev) 2746 { 2747 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 2748 struct ras_manager *obj; 2749 2750 if (!adev->ras_enabled || !con) 2751 return; 2752 2753 list_for_each_entry(obj, &con->head, node) { 2754 struct ras_query_if info = { 2755 .head = obj->head, 2756 }; 2757 2758 amdgpu_ras_error_status_query(adev, &info); 2759 } 2760 } 2761 2762 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev, 2763 struct ras_badpage *bps, uint32_t count, uint32_t start) 2764 { 2765 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 2766 struct ras_err_handler_data *data; 2767 int r = 0; 2768 uint32_t i; 2769 2770 if (!con || !con->eh_data || !bps || !count) 2771 return -EINVAL; 2772 2773 mutex_lock(&con->recovery_lock); 2774 data = con->eh_data; 2775 if (start < data->count) { 2776 for (i = start; i < data->count; i++) { 2777 if (!data->bps[i].ts) 2778 continue; 2779 2780 bps[r].bp = data->bps[i].retired_page; 2781 r++; 2782 if (r >= count) 2783 break; 2784 } 2785 } 2786 mutex_unlock(&con->recovery_lock); 2787 2788 return r; 2789 } 2790 2791 static int amdgpu_uniras_badpages_read(struct amdgpu_device *adev, 2792 struct ras_badpage *bps, uint32_t count, uint32_t start) 2793 { 2794 struct ras_cmd_bad_pages_info_req cmd_input; 2795 struct ras_cmd_bad_pages_info_rsp *output; 2796 uint32_t group, start_group, end_group; 2797 uint32_t pos, pos_in_group; 2798 int r = 0, i; 2799 2800 if (!bps || !count) 2801 return -EINVAL; 2802 2803 output = kmalloc(sizeof(*output), GFP_KERNEL); 2804 if (!output) 2805 return -ENOMEM; 2806 2807 memset(&cmd_input, 0, sizeof(cmd_input)); 2808 2809 start_group = start / RAS_CMD_MAX_BAD_PAGES_PER_GROUP; 2810 end_group = (start + count + RAS_CMD_MAX_BAD_PAGES_PER_GROUP - 1) / 2811 RAS_CMD_MAX_BAD_PAGES_PER_GROUP; 2812 2813 pos = start; 2814 for (group = start_group; group < end_group; group++) { 2815 memset(output, 0, sizeof(*output)); 2816 cmd_input.group_index = group; 2817 if (amdgpu_ras_mgr_handle_ras_cmd(adev, RAS_CMD__GET_BAD_PAGES, 2818 &cmd_input, sizeof(cmd_input), output, sizeof(*output))) 2819 goto out; 2820 2821 if (pos >= output->bp_total_cnt) 2822 goto out; 2823 2824 pos_in_group = pos - group * RAS_CMD_MAX_BAD_PAGES_PER_GROUP; 2825 for (i = pos_in_group; i < output->bp_in_group; i++, pos++) { 2826 if (!output->records[i].ts) 2827 continue; 2828 2829 bps[r].bp = output->records[i].retired_page; 2830 r++; 2831 if (r >= count) 2832 goto out; 2833 } 2834 } 2835 2836 out: 2837 kfree(output); 2838 return r; 2839 } 2840 2841 static void amdgpu_ras_set_fed_all(struct amdgpu_device *adev, 2842 struct amdgpu_hive_info *hive, bool status) 2843 { 2844 struct amdgpu_device *tmp_adev; 2845 2846 if (hive) { 2847 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) 2848 amdgpu_ras_set_fed(tmp_adev, status); 2849 } else { 2850 amdgpu_ras_set_fed(adev, status); 2851 } 2852 } 2853 2854 bool amdgpu_ras_in_recovery(struct amdgpu_device *adev) 2855 { 2856 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev); 2857 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev); 2858 int hive_ras_recovery = 0; 2859 2860 if (hive) { 2861 hive_ras_recovery = atomic_read(&hive->ras_recovery); 2862 amdgpu_put_xgmi_hive(hive); 2863 } 2864 2865 if (ras && (atomic_read(&ras->in_recovery) || hive_ras_recovery)) 2866 return true; 2867 2868 return false; 2869 } 2870 2871 static enum ras_event_type amdgpu_ras_get_fatal_error_event(struct amdgpu_device *adev) 2872 { 2873 if (amdgpu_ras_intr_triggered()) 2874 return RAS_EVENT_TYPE_FATAL; 2875 else 2876 return RAS_EVENT_TYPE_POISON_CONSUMPTION; 2877 } 2878 2879 static void amdgpu_ras_do_recovery(struct work_struct *work) 2880 { 2881 struct amdgpu_ras *ras = 2882 container_of(work, struct amdgpu_ras, recovery_work); 2883 struct amdgpu_device *remote_adev = NULL; 2884 struct amdgpu_device *adev = ras->adev; 2885 struct list_head device_list, *device_list_handle = NULL; 2886 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev); 2887 unsigned int error_query_mode; 2888 enum ras_event_type type; 2889 2890 if (hive) { 2891 atomic_set(&hive->ras_recovery, 1); 2892 2893 /* If any device which is part of the hive received RAS fatal 2894 * error interrupt, set fatal error status on all. This 2895 * condition will need a recovery, and flag will be cleared 2896 * as part of recovery. 2897 */ 2898 list_for_each_entry(remote_adev, &hive->device_list, 2899 gmc.xgmi.head) 2900 if (amdgpu_ras_get_fed_status(remote_adev)) { 2901 amdgpu_ras_set_fed_all(adev, hive, true); 2902 break; 2903 } 2904 } 2905 if (!ras->disable_ras_err_cnt_harvest) { 2906 2907 /* Build list of devices to query RAS related errors */ 2908 if (hive && adev->gmc.xgmi.num_physical_nodes > 1) { 2909 device_list_handle = &hive->device_list; 2910 } else { 2911 INIT_LIST_HEAD(&device_list); 2912 list_add_tail(&adev->gmc.xgmi.head, &device_list); 2913 device_list_handle = &device_list; 2914 } 2915 2916 if (amdgpu_ras_get_error_query_mode(adev, &error_query_mode)) { 2917 if (error_query_mode == AMDGPU_RAS_FIRMWARE_ERROR_QUERY) { 2918 /* wait 500ms to ensure pmfw polling mca bank info done */ 2919 msleep(500); 2920 } 2921 } 2922 2923 type = amdgpu_ras_get_fatal_error_event(adev); 2924 list_for_each_entry(remote_adev, 2925 device_list_handle, gmc.xgmi.head) { 2926 if (amdgpu_uniras_enabled(remote_adev)) { 2927 amdgpu_ras_mgr_update_ras_ecc(remote_adev); 2928 } else { 2929 amdgpu_ras_query_err_status(remote_adev); 2930 amdgpu_ras_log_on_err_counter(remote_adev, type); 2931 } 2932 } 2933 2934 } 2935 2936 if (amdgpu_device_should_recover_gpu(ras->adev)) { 2937 struct amdgpu_reset_context reset_context; 2938 memset(&reset_context, 0, sizeof(reset_context)); 2939 2940 reset_context.method = AMD_RESET_METHOD_NONE; 2941 reset_context.reset_req_dev = adev; 2942 reset_context.src = AMDGPU_RESET_SRC_RAS; 2943 set_bit(AMDGPU_SKIP_COREDUMP, &reset_context.flags); 2944 2945 /* Perform full reset in fatal error mode */ 2946 if (!amdgpu_ras_is_poison_mode_supported(ras->adev)) 2947 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags); 2948 else { 2949 clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags); 2950 2951 if (ras->gpu_reset_flags & AMDGPU_RAS_GPU_RESET_MODE2_RESET) { 2952 ras->gpu_reset_flags &= ~AMDGPU_RAS_GPU_RESET_MODE2_RESET; 2953 reset_context.method = AMD_RESET_METHOD_MODE2; 2954 } 2955 2956 /* Fatal error occurs in poison mode, mode1 reset is used to 2957 * recover gpu. 2958 */ 2959 if (ras->gpu_reset_flags & AMDGPU_RAS_GPU_RESET_MODE1_RESET) { 2960 ras->gpu_reset_flags &= ~AMDGPU_RAS_GPU_RESET_MODE1_RESET; 2961 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags); 2962 2963 psp_fatal_error_recovery_quirk(&adev->psp); 2964 } 2965 } 2966 2967 amdgpu_device_gpu_recover(ras->adev, NULL, &reset_context); 2968 } 2969 atomic_set(&ras->in_recovery, 0); 2970 if (hive) { 2971 atomic_set(&hive->ras_recovery, 0); 2972 amdgpu_put_xgmi_hive(hive); 2973 } 2974 } 2975 2976 /* alloc/realloc bps array */ 2977 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev, 2978 struct ras_err_handler_data *data, int pages) 2979 { 2980 unsigned int old_space = data->count + data->space_left; 2981 unsigned int new_space = old_space + pages; 2982 unsigned int align_space = ALIGN(new_space, 512); 2983 void *bps = kmalloc_array(align_space, sizeof(*data->bps), GFP_KERNEL); 2984 2985 if (!bps) { 2986 return -ENOMEM; 2987 } 2988 2989 if (data->bps) { 2990 memcpy(bps, data->bps, 2991 data->count * sizeof(*data->bps)); 2992 kfree(data->bps); 2993 } 2994 2995 data->bps = bps; 2996 data->space_left += align_space - old_space; 2997 return 0; 2998 } 2999 3000 static int amdgpu_ras_mca2pa_by_idx(struct amdgpu_device *adev, 3001 struct eeprom_table_record *bps, 3002 struct ras_err_data *err_data) 3003 { 3004 struct ta_ras_query_address_input addr_in; 3005 uint32_t socket = 0; 3006 int ret = 0; 3007 3008 if (adev->smuio.funcs && adev->smuio.funcs->get_socket_id) 3009 socket = adev->smuio.funcs->get_socket_id(adev); 3010 3011 /* reinit err_data */ 3012 err_data->err_addr_cnt = 0; 3013 err_data->err_addr_len = adev->umc.retire_unit; 3014 3015 memset(&addr_in, 0, sizeof(addr_in)); 3016 addr_in.ma.err_addr = bps->address; 3017 addr_in.ma.socket_id = socket; 3018 addr_in.ma.ch_inst = bps->mem_channel; 3019 if (!amdgpu_ras_smu_eeprom_supported(adev)) { 3020 /* tell RAS TA the node instance is not used */ 3021 addr_in.ma.node_inst = TA_RAS_INV_NODE; 3022 } else { 3023 addr_in.ma.umc_inst = bps->mcumc_id; 3024 addr_in.ma.node_inst = bps->cu; 3025 } 3026 3027 if (adev->umc.ras && adev->umc.ras->convert_ras_err_addr) 3028 ret = adev->umc.ras->convert_ras_err_addr(adev, err_data, 3029 &addr_in, NULL, false); 3030 3031 return ret; 3032 } 3033 3034 static int amdgpu_ras_mca2pa(struct amdgpu_device *adev, 3035 struct eeprom_table_record *bps, 3036 struct ras_err_data *err_data) 3037 { 3038 struct ta_ras_query_address_input addr_in; 3039 uint32_t die_id, socket = 0; 3040 3041 if (adev->smuio.funcs && adev->smuio.funcs->get_socket_id) 3042 socket = adev->smuio.funcs->get_socket_id(adev); 3043 3044 /* although die id is gotten from PA in nps1 mode, the id is 3045 * fitable for any nps mode 3046 */ 3047 if (adev->umc.ras && adev->umc.ras->get_die_id_from_pa) 3048 die_id = adev->umc.ras->get_die_id_from_pa(adev, bps->address, 3049 bps->retired_page << AMDGPU_GPU_PAGE_SHIFT); 3050 else 3051 return -EINVAL; 3052 3053 /* reinit err_data */ 3054 err_data->err_addr_cnt = 0; 3055 err_data->err_addr_len = adev->umc.retire_unit; 3056 3057 memset(&addr_in, 0, sizeof(addr_in)); 3058 addr_in.ma.err_addr = bps->address; 3059 addr_in.ma.ch_inst = bps->mem_channel; 3060 addr_in.ma.umc_inst = bps->mcumc_id; 3061 addr_in.ma.node_inst = die_id; 3062 addr_in.ma.socket_id = socket; 3063 3064 if (adev->umc.ras && adev->umc.ras->convert_ras_err_addr) 3065 return adev->umc.ras->convert_ras_err_addr(adev, err_data, 3066 &addr_in, NULL, false); 3067 else 3068 return -EINVAL; 3069 } 3070 3071 static int __amdgpu_ras_restore_bad_pages(struct amdgpu_device *adev, 3072 struct eeprom_table_record *bps, int count) 3073 { 3074 int j; 3075 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 3076 struct ras_err_handler_data *data = con->eh_data; 3077 3078 for (j = 0; j < count; j++) { 3079 if (!data->space_left && 3080 amdgpu_ras_realloc_eh_data_space(adev, data, 256)) { 3081 return -ENOMEM; 3082 } 3083 3084 if (amdgpu_ras_check_bad_page_unlock(con, 3085 bps[j].retired_page << AMDGPU_GPU_PAGE_SHIFT)) { 3086 data->count++; 3087 data->space_left--; 3088 continue; 3089 } 3090 3091 amdgpu_ras_reserve_page(adev, bps[j].retired_page); 3092 3093 memcpy(&data->bps[data->count], &(bps[j]), 3094 sizeof(struct eeprom_table_record)); 3095 data->count++; 3096 data->space_left--; 3097 con->bad_page_num++; 3098 } 3099 3100 return 0; 3101 } 3102 3103 static int __amdgpu_ras_convert_rec_array_from_rom(struct amdgpu_device *adev, 3104 struct eeprom_table_record *bps, struct ras_err_data *err_data, 3105 enum amdgpu_memory_partition nps) 3106 { 3107 int i = 0; 3108 enum amdgpu_memory_partition save_nps; 3109 3110 save_nps = (bps[0].retired_page >> UMC_NPS_SHIFT) & UMC_NPS_MASK; 3111 3112 /*old asics just have pa in eeprom*/ 3113 if (IP_VERSION_MAJ(amdgpu_ip_version(adev, UMC_HWIP, 0)) < 12) { 3114 memcpy(err_data->err_addr, bps, 3115 sizeof(struct eeprom_table_record) * adev->umc.retire_unit); 3116 goto out; 3117 } 3118 3119 for (i = 0; i < adev->umc.retire_unit; i++) 3120 bps[i].retired_page &= ~(UMC_NPS_MASK << UMC_NPS_SHIFT); 3121 3122 if (save_nps) { 3123 if (save_nps == nps) { 3124 if (amdgpu_umc_pages_in_a_row(adev, err_data, 3125 bps[0].retired_page << AMDGPU_GPU_PAGE_SHIFT)) 3126 return -EINVAL; 3127 for (i = 0; i < adev->umc.retire_unit; i++) { 3128 err_data->err_addr[i].address = bps[0].address; 3129 err_data->err_addr[i].mem_channel = bps[0].mem_channel; 3130 err_data->err_addr[i].bank = bps[0].bank; 3131 err_data->err_addr[i].err_type = bps[0].err_type; 3132 err_data->err_addr[i].mcumc_id = bps[0].mcumc_id; 3133 } 3134 } else { 3135 if (amdgpu_ras_mca2pa_by_idx(adev, &bps[0], err_data)) 3136 return -EINVAL; 3137 } 3138 } else { 3139 if (bps[0].address == 0) { 3140 /* for specific old eeprom data, mca address is not stored, 3141 * calc it from pa 3142 */ 3143 if (amdgpu_umc_pa2mca(adev, bps[0].retired_page << AMDGPU_GPU_PAGE_SHIFT, 3144 &(bps[0].address), AMDGPU_NPS1_PARTITION_MODE)) 3145 return -EINVAL; 3146 } 3147 3148 if (amdgpu_ras_mca2pa(adev, &bps[0], err_data)) { 3149 if (nps == AMDGPU_NPS1_PARTITION_MODE) 3150 memcpy(err_data->err_addr, bps, 3151 sizeof(struct eeprom_table_record) * adev->umc.retire_unit); 3152 else 3153 return -EOPNOTSUPP; 3154 } 3155 } 3156 3157 out: 3158 return __amdgpu_ras_restore_bad_pages(adev, err_data->err_addr, adev->umc.retire_unit); 3159 } 3160 3161 static int __amdgpu_ras_convert_rec_from_rom(struct amdgpu_device *adev, 3162 struct eeprom_table_record *bps, struct ras_err_data *err_data, 3163 enum amdgpu_memory_partition nps) 3164 { 3165 int i = 0; 3166 enum amdgpu_memory_partition save_nps; 3167 3168 if (!amdgpu_ras_smu_eeprom_supported(adev)) { 3169 save_nps = (bps->retired_page >> UMC_NPS_SHIFT) & UMC_NPS_MASK; 3170 bps->retired_page &= ~(UMC_NPS_MASK << UMC_NPS_SHIFT); 3171 } else { 3172 /* if pmfw manages eeprom, save_nps is not stored on eeprom, 3173 * we should always convert mca address into physical address, 3174 * make save_nps different from nps 3175 */ 3176 save_nps = nps + 1; 3177 } 3178 3179 if (save_nps == nps) { 3180 if (amdgpu_umc_pages_in_a_row(adev, err_data, 3181 bps->retired_page << AMDGPU_GPU_PAGE_SHIFT)) 3182 return -EINVAL; 3183 for (i = 0; i < adev->umc.retire_unit; i++) { 3184 err_data->err_addr[i].address = bps->address; 3185 err_data->err_addr[i].mem_channel = bps->mem_channel; 3186 err_data->err_addr[i].bank = bps->bank; 3187 err_data->err_addr[i].err_type = bps->err_type; 3188 err_data->err_addr[i].mcumc_id = bps->mcumc_id; 3189 } 3190 } else { 3191 if (bps->address) { 3192 if (amdgpu_ras_mca2pa_by_idx(adev, bps, err_data)) 3193 return -EINVAL; 3194 } else { 3195 /* for specific old eeprom data, mca address is not stored, 3196 * calc it from pa 3197 */ 3198 if (amdgpu_umc_pa2mca(adev, bps->retired_page << AMDGPU_GPU_PAGE_SHIFT, 3199 &(bps->address), AMDGPU_NPS1_PARTITION_MODE)) 3200 return -EINVAL; 3201 3202 if (amdgpu_ras_mca2pa(adev, bps, err_data)) 3203 return -EOPNOTSUPP; 3204 } 3205 } 3206 3207 return __amdgpu_ras_restore_bad_pages(adev, err_data->err_addr, 3208 adev->umc.retire_unit); 3209 } 3210 3211 /* it deal with vram only. */ 3212 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev, 3213 struct eeprom_table_record *bps, int pages, bool from_rom) 3214 { 3215 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 3216 struct ras_err_data err_data; 3217 struct amdgpu_ras_eeprom_control *control = 3218 &adev->psp.ras_context.ras->eeprom_control; 3219 enum amdgpu_memory_partition nps = AMDGPU_NPS1_PARTITION_MODE; 3220 int ret = 0; 3221 uint32_t i = 0; 3222 3223 if (!con || !con->eh_data || !bps || pages <= 0) 3224 return 0; 3225 3226 if (from_rom) { 3227 err_data.err_addr = 3228 kcalloc(adev->umc.retire_unit, 3229 sizeof(struct eeprom_table_record), GFP_KERNEL); 3230 if (!err_data.err_addr) { 3231 dev_warn(adev->dev, "Failed to alloc UMC error address record in mca2pa conversion!\n"); 3232 return -ENOMEM; 3233 } 3234 3235 if (adev->gmc.gmc_funcs->query_mem_partition_mode) 3236 nps = adev->gmc.gmc_funcs->query_mem_partition_mode(adev); 3237 } 3238 3239 mutex_lock(&con->recovery_lock); 3240 3241 if (from_rom) { 3242 /* there is no pa recs in V3, so skip pa recs processing */ 3243 if ((control->tbl_hdr.version < RAS_TABLE_VER_V3) && 3244 !amdgpu_ras_smu_eeprom_supported(adev)) { 3245 for (i = 0; i < pages; i++) { 3246 if (control->ras_num_recs - i >= adev->umc.retire_unit) { 3247 if ((bps[i].address == bps[i + 1].address) && 3248 (bps[i].mem_channel == bps[i + 1].mem_channel)) { 3249 /* deal with retire_unit records a time */ 3250 ret = __amdgpu_ras_convert_rec_array_from_rom(adev, 3251 &bps[i], &err_data, nps); 3252 i += (adev->umc.retire_unit - 1); 3253 } else { 3254 break; 3255 } 3256 } else { 3257 break; 3258 } 3259 } 3260 } 3261 for (; i < pages; i++) { 3262 ret = __amdgpu_ras_convert_rec_from_rom(adev, 3263 &bps[i], &err_data, nps); 3264 } 3265 3266 con->eh_data->count_saved = con->eh_data->count; 3267 } else { 3268 ret = __amdgpu_ras_restore_bad_pages(adev, bps, pages); 3269 } 3270 3271 if (from_rom) 3272 kfree(err_data.err_addr); 3273 mutex_unlock(&con->recovery_lock); 3274 3275 return ret; 3276 } 3277 3278 /* 3279 * write error record array to eeprom, the function should be 3280 * protected by recovery_lock 3281 * new_cnt: new added UE count, excluding reserved bad pages, can be NULL 3282 */ 3283 int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev, 3284 unsigned long *new_cnt) 3285 { 3286 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 3287 struct ras_err_handler_data *data; 3288 struct amdgpu_ras_eeprom_control *control; 3289 int save_count, unit_num, i; 3290 3291 if (!con || !con->eh_data) { 3292 if (new_cnt) 3293 *new_cnt = 0; 3294 3295 return 0; 3296 } 3297 3298 if (!con->eeprom_control.is_eeprom_valid) { 3299 dev_warn(adev->dev, 3300 "Failed to save EEPROM table data because of EEPROM data corruption!"); 3301 if (new_cnt) 3302 *new_cnt = 0; 3303 3304 return 0; 3305 } 3306 3307 mutex_lock(&con->recovery_lock); 3308 control = &con->eeprom_control; 3309 data = con->eh_data; 3310 if (amdgpu_ras_smu_eeprom_supported(adev)) 3311 unit_num = control->ras_num_recs - 3312 control->ras_num_recs_old; 3313 else 3314 unit_num = data->count / adev->umc.retire_unit - 3315 control->ras_num_recs; 3316 3317 save_count = con->bad_page_num - control->ras_num_bad_pages; 3318 mutex_unlock(&con->recovery_lock); 3319 3320 if (new_cnt) 3321 *new_cnt = unit_num; 3322 3323 /* only new entries are saved */ 3324 if (unit_num && save_count) { 3325 /*old asics only save pa to eeprom like before*/ 3326 if (IP_VERSION_MAJ(amdgpu_ip_version(adev, UMC_HWIP, 0)) < 12) { 3327 if (amdgpu_ras_eeprom_append(control, 3328 &data->bps[data->count_saved], unit_num)) { 3329 dev_err(adev->dev, "Failed to save EEPROM table data!"); 3330 return -EIO; 3331 } 3332 } else { 3333 for (i = 0; i < unit_num; i++) { 3334 if (amdgpu_ras_eeprom_append(control, 3335 &data->bps[data->count_saved + 3336 i * adev->umc.retire_unit], 1)) { 3337 dev_err(adev->dev, "Failed to save EEPROM table data!"); 3338 return -EIO; 3339 } 3340 } 3341 } 3342 3343 dev_info(adev->dev, "Saved %d pages to EEPROM table.\n", save_count); 3344 data->count_saved = data->count; 3345 } 3346 3347 return 0; 3348 } 3349 3350 /* 3351 * read error record array in eeprom and reserve enough space for 3352 * storing new bad pages 3353 */ 3354 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev) 3355 { 3356 struct amdgpu_ras_eeprom_control *control = 3357 &adev->psp.ras_context.ras->eeprom_control; 3358 struct eeprom_table_record *bps; 3359 int ret, i = 0; 3360 3361 /* no bad page record, skip eeprom access */ 3362 if (control->ras_num_recs == 0 || amdgpu_bad_page_threshold == 0) 3363 return 0; 3364 3365 bps = kcalloc(control->ras_num_recs, sizeof(*bps), GFP_KERNEL); 3366 if (!bps) 3367 return -ENOMEM; 3368 3369 ret = amdgpu_ras_eeprom_read(control, bps, control->ras_num_recs); 3370 if (ret) { 3371 dev_err(adev->dev, "Failed to load EEPROM table records!"); 3372 } else { 3373 if (adev->umc.ras && adev->umc.ras->convert_ras_err_addr) { 3374 /*In V3, there is no pa recs, and some cases(when address==0) may be parsed 3375 as pa recs, so add verion check to avoid it. 3376 */ 3377 if ((control->tbl_hdr.version < RAS_TABLE_VER_V3) && 3378 !amdgpu_ras_smu_eeprom_supported(adev)) { 3379 for (i = 0; i < control->ras_num_recs; i++) { 3380 if ((control->ras_num_recs - i) >= adev->umc.retire_unit) { 3381 if ((bps[i].address == bps[i + 1].address) && 3382 (bps[i].mem_channel == bps[i + 1].mem_channel)) { 3383 control->ras_num_pa_recs += adev->umc.retire_unit; 3384 i += (adev->umc.retire_unit - 1); 3385 } else { 3386 control->ras_num_mca_recs += 3387 (control->ras_num_recs - i); 3388 break; 3389 } 3390 } else { 3391 control->ras_num_mca_recs += (control->ras_num_recs - i); 3392 break; 3393 } 3394 } 3395 } else { 3396 control->ras_num_mca_recs = control->ras_num_recs; 3397 } 3398 } 3399 3400 ret = amdgpu_ras_add_bad_pages(adev, bps, control->ras_num_recs, true); 3401 if (ret) 3402 goto out; 3403 3404 ret = amdgpu_ras_eeprom_check(control); 3405 if (ret) 3406 goto out; 3407 3408 /* HW not usable */ 3409 if (amdgpu_ras_is_rma(adev)) 3410 ret = -EHWPOISON; 3411 } 3412 3413 out: 3414 kfree(bps); 3415 return ret; 3416 } 3417 3418 static int amdgpu_ras_check_bad_page_unlock(struct amdgpu_ras *con, 3419 uint64_t addr) 3420 { 3421 struct ras_err_handler_data *data = con->eh_data; 3422 struct amdgpu_device *adev = con->adev; 3423 int i; 3424 3425 if ((addr >= adev->gmc.mc_vram_size && 3426 adev->gmc.mc_vram_size) || 3427 (addr >= RAS_UMC_INJECT_ADDR_LIMIT)) 3428 return -EINVAL; 3429 3430 addr >>= AMDGPU_GPU_PAGE_SHIFT; 3431 for (i = 0; i < data->count; i++) 3432 if (addr == data->bps[i].retired_page) 3433 return 1; 3434 3435 return 0; 3436 } 3437 3438 /* 3439 * check if an address belongs to bad page 3440 * 3441 * Note: this check is only for umc block 3442 */ 3443 static int amdgpu_ras_check_bad_page(struct amdgpu_device *adev, 3444 uint64_t addr) 3445 { 3446 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 3447 int ret = 0; 3448 3449 if (!con || !con->eh_data) 3450 return ret; 3451 3452 mutex_lock(&con->recovery_lock); 3453 ret = amdgpu_ras_check_bad_page_unlock(con, addr); 3454 mutex_unlock(&con->recovery_lock); 3455 return ret; 3456 } 3457 3458 static void amdgpu_ras_validate_threshold(struct amdgpu_device *adev, 3459 uint32_t max_count) 3460 { 3461 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 3462 3463 /* 3464 * amdgpu_bad_page_threshold is used to config 3465 * the threshold for the number of bad pages. 3466 * -1: Threshold is set to default value 3467 * Driver will issue a warning message when threshold is reached 3468 * and continue runtime services. 3469 * 0: Disable bad page retirement 3470 * Driver will not retire bad pages 3471 * which is intended for debugging purpose. 3472 * -2: Threshold is determined by a formula 3473 * that assumes 1 bad page per 100M of local memory. 3474 * Driver will continue runtime services when threhold is reached. 3475 * 0 < threshold < max number of bad page records in EEPROM, 3476 * A user-defined threshold is set 3477 * Driver will halt runtime services when this custom threshold is reached. 3478 */ 3479 if (amdgpu_bad_page_threshold == -2) { 3480 u64 val = adev->gmc.mc_vram_size; 3481 3482 do_div(val, RAS_BAD_PAGE_COVER); 3483 con->bad_page_cnt_threshold = min(lower_32_bits(val), 3484 max_count); 3485 } else if (amdgpu_bad_page_threshold == -1) { 3486 con->bad_page_cnt_threshold = ((con->reserved_pages_in_bytes) >> 21) << 4; 3487 } else { 3488 con->bad_page_cnt_threshold = min_t(int, max_count, 3489 amdgpu_bad_page_threshold); 3490 } 3491 } 3492 3493 int amdgpu_ras_put_poison_req(struct amdgpu_device *adev, 3494 enum amdgpu_ras_block block, uint16_t pasid, 3495 pasid_notify pasid_fn, void *data, uint32_t reset) 3496 { 3497 int ret = 0; 3498 struct ras_poison_msg poison_msg; 3499 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 3500 3501 memset(&poison_msg, 0, sizeof(poison_msg)); 3502 poison_msg.block = block; 3503 poison_msg.pasid = pasid; 3504 poison_msg.reset = reset; 3505 poison_msg.pasid_fn = pasid_fn; 3506 poison_msg.data = data; 3507 3508 ret = kfifo_put(&con->poison_fifo, poison_msg); 3509 if (!ret) { 3510 dev_err(adev->dev, "Poison message fifo is full!\n"); 3511 return -ENOSPC; 3512 } 3513 3514 return 0; 3515 } 3516 3517 static int amdgpu_ras_get_poison_req(struct amdgpu_device *adev, 3518 struct ras_poison_msg *poison_msg) 3519 { 3520 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 3521 3522 return kfifo_get(&con->poison_fifo, poison_msg); 3523 } 3524 3525 static void amdgpu_ras_ecc_log_init(struct ras_ecc_log_info *ecc_log) 3526 { 3527 mutex_init(&ecc_log->lock); 3528 3529 INIT_RADIX_TREE(&ecc_log->de_page_tree, GFP_KERNEL); 3530 ecc_log->de_queried_count = 0; 3531 ecc_log->consumption_q_count = 0; 3532 } 3533 3534 static void amdgpu_ras_ecc_log_fini(struct ras_ecc_log_info *ecc_log) 3535 { 3536 struct radix_tree_iter iter; 3537 void __rcu **slot; 3538 struct ras_ecc_err *ecc_err; 3539 3540 mutex_lock(&ecc_log->lock); 3541 radix_tree_for_each_slot(slot, &ecc_log->de_page_tree, &iter, 0) { 3542 ecc_err = radix_tree_deref_slot(slot); 3543 kfree(ecc_err->err_pages.pfn); 3544 kfree(ecc_err); 3545 radix_tree_iter_delete(&ecc_log->de_page_tree, &iter, slot); 3546 } 3547 mutex_unlock(&ecc_log->lock); 3548 3549 mutex_destroy(&ecc_log->lock); 3550 ecc_log->de_queried_count = 0; 3551 ecc_log->consumption_q_count = 0; 3552 } 3553 3554 static bool amdgpu_ras_schedule_retirement_dwork(struct amdgpu_ras *con, 3555 uint32_t delayed_ms) 3556 { 3557 int ret; 3558 3559 mutex_lock(&con->umc_ecc_log.lock); 3560 ret = radix_tree_tagged(&con->umc_ecc_log.de_page_tree, 3561 UMC_ECC_NEW_DETECTED_TAG); 3562 mutex_unlock(&con->umc_ecc_log.lock); 3563 3564 if (ret) 3565 schedule_delayed_work(&con->page_retirement_dwork, 3566 msecs_to_jiffies(delayed_ms)); 3567 3568 return ret ? true : false; 3569 } 3570 3571 static void amdgpu_ras_do_page_retirement(struct work_struct *work) 3572 { 3573 struct amdgpu_ras *con = container_of(work, struct amdgpu_ras, 3574 page_retirement_dwork.work); 3575 struct amdgpu_device *adev = con->adev; 3576 struct ras_err_data err_data; 3577 3578 /* If gpu reset is ongoing, delay retiring the bad pages */ 3579 if (amdgpu_in_reset(adev) || amdgpu_ras_in_recovery(adev)) { 3580 amdgpu_ras_schedule_retirement_dwork(con, 3581 AMDGPU_RAS_RETIRE_PAGE_INTERVAL * 3); 3582 return; 3583 } 3584 3585 amdgpu_ras_error_data_init(&err_data); 3586 3587 amdgpu_umc_handle_bad_pages(adev, &err_data); 3588 3589 amdgpu_ras_error_data_fini(&err_data); 3590 3591 amdgpu_ras_schedule_retirement_dwork(con, 3592 AMDGPU_RAS_RETIRE_PAGE_INTERVAL); 3593 } 3594 3595 static int amdgpu_ras_poison_creation_handler(struct amdgpu_device *adev, 3596 uint32_t poison_creation_count) 3597 { 3598 int ret = 0; 3599 struct ras_ecc_log_info *ecc_log; 3600 struct ras_query_if info; 3601 u32 timeout = MAX_UMC_POISON_POLLING_TIME_ASYNC; 3602 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev); 3603 u64 de_queried_count; 3604 u64 consumption_q_count; 3605 enum ras_event_type type = RAS_EVENT_TYPE_POISON_CREATION; 3606 3607 memset(&info, 0, sizeof(info)); 3608 info.head.block = AMDGPU_RAS_BLOCK__UMC; 3609 3610 ecc_log = &ras->umc_ecc_log; 3611 ecc_log->de_queried_count = 0; 3612 ecc_log->consumption_q_count = 0; 3613 3614 do { 3615 ret = amdgpu_ras_query_error_status_with_event(adev, &info, type); 3616 if (ret) 3617 return ret; 3618 3619 de_queried_count = ecc_log->de_queried_count; 3620 consumption_q_count = ecc_log->consumption_q_count; 3621 3622 if (de_queried_count && consumption_q_count) 3623 break; 3624 3625 msleep(100); 3626 } while (--timeout); 3627 3628 if (de_queried_count) 3629 schedule_delayed_work(&ras->page_retirement_dwork, 0); 3630 3631 if (amdgpu_ras_is_rma(adev) && atomic_cmpxchg(&ras->rma_in_recovery, 0, 1) == 0) 3632 amdgpu_ras_reset_gpu(adev); 3633 3634 return 0; 3635 } 3636 3637 static void amdgpu_ras_clear_poison_fifo(struct amdgpu_device *adev) 3638 { 3639 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 3640 struct ras_poison_msg msg; 3641 int ret; 3642 3643 do { 3644 ret = kfifo_get(&con->poison_fifo, &msg); 3645 } while (ret); 3646 } 3647 3648 static int amdgpu_ras_poison_consumption_handler(struct amdgpu_device *adev, 3649 uint32_t msg_count, uint32_t *gpu_reset) 3650 { 3651 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 3652 uint32_t reset_flags = 0, reset = 0; 3653 struct ras_poison_msg msg; 3654 int ret, i; 3655 3656 kgd2kfd_set_sram_ecc_flag(adev->kfd.dev); 3657 3658 for (i = 0; i < msg_count; i++) { 3659 ret = amdgpu_ras_get_poison_req(adev, &msg); 3660 if (!ret) 3661 continue; 3662 3663 if (msg.pasid_fn) 3664 msg.pasid_fn(adev, msg.pasid, msg.data); 3665 3666 reset_flags |= msg.reset; 3667 } 3668 3669 /* 3670 * Try to ensure poison creation handler is completed first 3671 * to set rma if bad page exceed threshold. 3672 */ 3673 flush_delayed_work(&con->page_retirement_dwork); 3674 3675 /* for RMA, amdgpu_ras_poison_creation_handler will trigger gpu reset */ 3676 if (reset_flags && !amdgpu_ras_is_rma(adev)) { 3677 if (reset_flags & AMDGPU_RAS_GPU_RESET_MODE1_RESET) 3678 reset = AMDGPU_RAS_GPU_RESET_MODE1_RESET; 3679 else if (reset_flags & AMDGPU_RAS_GPU_RESET_MODE2_RESET) 3680 reset = AMDGPU_RAS_GPU_RESET_MODE2_RESET; 3681 else 3682 reset = reset_flags; 3683 3684 con->gpu_reset_flags |= reset; 3685 amdgpu_ras_reset_gpu(adev); 3686 3687 *gpu_reset = reset; 3688 3689 /* Wait for gpu recovery to complete */ 3690 flush_work(&con->recovery_work); 3691 } 3692 3693 return 0; 3694 } 3695 3696 static int amdgpu_ras_page_retirement_thread(void *param) 3697 { 3698 struct amdgpu_device *adev = (struct amdgpu_device *)param; 3699 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 3700 uint32_t poison_creation_count, msg_count; 3701 uint32_t gpu_reset; 3702 int ret; 3703 3704 while (!kthread_should_stop()) { 3705 3706 wait_event_interruptible(con->page_retirement_wq, 3707 kthread_should_stop() || 3708 atomic_read(&con->page_retirement_req_cnt)); 3709 3710 if (kthread_should_stop()) 3711 break; 3712 3713 mutex_lock(&con->poison_lock); 3714 gpu_reset = 0; 3715 3716 do { 3717 poison_creation_count = atomic_read(&con->poison_creation_count); 3718 ret = amdgpu_ras_poison_creation_handler(adev, poison_creation_count); 3719 if (ret == -EIO) 3720 break; 3721 3722 if (poison_creation_count) { 3723 atomic_sub(poison_creation_count, &con->poison_creation_count); 3724 atomic_sub(poison_creation_count, &con->page_retirement_req_cnt); 3725 } 3726 } while (atomic_read(&con->poison_creation_count) && 3727 !atomic_read(&con->poison_consumption_count)); 3728 3729 if (ret != -EIO) { 3730 msg_count = kfifo_len(&con->poison_fifo); 3731 if (msg_count) { 3732 ret = amdgpu_ras_poison_consumption_handler(adev, 3733 msg_count, &gpu_reset); 3734 if ((ret != -EIO) && 3735 (gpu_reset != AMDGPU_RAS_GPU_RESET_MODE1_RESET)) 3736 atomic_sub(msg_count, &con->page_retirement_req_cnt); 3737 } 3738 } 3739 3740 if ((ret == -EIO) || (gpu_reset == AMDGPU_RAS_GPU_RESET_MODE1_RESET)) { 3741 /* gpu mode-1 reset is ongoing or just completed ras mode-1 reset */ 3742 /* Clear poison creation request */ 3743 atomic_set(&con->poison_creation_count, 0); 3744 atomic_set(&con->poison_consumption_count, 0); 3745 3746 /* Clear poison fifo */ 3747 amdgpu_ras_clear_poison_fifo(adev); 3748 3749 /* Clear all poison requests */ 3750 atomic_set(&con->page_retirement_req_cnt, 0); 3751 3752 if (ret == -EIO) { 3753 /* Wait for mode-1 reset to complete */ 3754 down_read(&adev->reset_domain->sem); 3755 up_read(&adev->reset_domain->sem); 3756 } 3757 3758 /* Wake up work to save bad pages to eeprom */ 3759 schedule_delayed_work(&con->page_retirement_dwork, 0); 3760 } else if (gpu_reset) { 3761 /* gpu just completed mode-2 reset or other reset */ 3762 /* Clear poison consumption messages cached in fifo */ 3763 msg_count = kfifo_len(&con->poison_fifo); 3764 if (msg_count) { 3765 amdgpu_ras_clear_poison_fifo(adev); 3766 atomic_sub(msg_count, &con->page_retirement_req_cnt); 3767 } 3768 3769 atomic_set(&con->poison_consumption_count, 0); 3770 3771 /* Wake up work to save bad pages to eeprom */ 3772 schedule_delayed_work(&con->page_retirement_dwork, 0); 3773 } 3774 mutex_unlock(&con->poison_lock); 3775 } 3776 3777 return 0; 3778 } 3779 3780 int amdgpu_ras_init_badpage_info(struct amdgpu_device *adev) 3781 { 3782 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 3783 struct amdgpu_ras_eeprom_control *control; 3784 int ret; 3785 3786 if (!con || amdgpu_sriov_vf(adev)) 3787 return 0; 3788 3789 if (amdgpu_uniras_enabled(adev)) 3790 return 0; 3791 3792 control = &con->eeprom_control; 3793 con->ras_smu_drv = amdgpu_dpm_get_ras_smu_driver(adev); 3794 3795 ret = amdgpu_ras_eeprom_init(control); 3796 control->is_eeprom_valid = !ret; 3797 3798 if (!adev->umc.ras || !adev->umc.ras->convert_ras_err_addr) 3799 control->ras_num_pa_recs = control->ras_num_recs; 3800 3801 if (adev->umc.ras && 3802 adev->umc.ras->get_retire_flip_bits) 3803 adev->umc.ras->get_retire_flip_bits(adev); 3804 3805 if (control->ras_num_recs && control->is_eeprom_valid) { 3806 ret = amdgpu_ras_load_bad_pages(adev); 3807 if (ret) { 3808 control->is_eeprom_valid = false; 3809 return 0; 3810 } 3811 3812 amdgpu_dpm_send_hbm_bad_pages_num( 3813 adev, control->ras_num_bad_pages); 3814 3815 if (con->update_channel_flag == true) { 3816 amdgpu_dpm_send_hbm_bad_channel_flag( 3817 adev, control->bad_channel_bitmap); 3818 con->update_channel_flag = false; 3819 } 3820 3821 /* The format action is only applied to new ASICs */ 3822 if (IP_VERSION_MAJ(amdgpu_ip_version(adev, UMC_HWIP, 0)) >= 12 && 3823 control->tbl_hdr.version < RAS_TABLE_VER_V3) 3824 if (!amdgpu_ras_eeprom_reset_table(control)) 3825 if (amdgpu_ras_save_bad_pages(adev, NULL)) 3826 dev_warn(adev->dev, "Failed to format RAS EEPROM data in V3 version!\n"); 3827 } 3828 3829 return 0; 3830 } 3831 3832 int amdgpu_ras_recovery_init(struct amdgpu_device *adev, bool init_bp_info) 3833 { 3834 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 3835 struct ras_err_handler_data **data; 3836 u32 max_eeprom_records_count = 0; 3837 int ret; 3838 3839 if (!con || amdgpu_sriov_vf(adev)) 3840 return 0; 3841 3842 /* Allow access to RAS EEPROM via debugfs, when the ASIC 3843 * supports RAS and debugfs is enabled, but when 3844 * adev->ras_enabled is unset, i.e. when "ras_enable" 3845 * module parameter is set to 0. 3846 */ 3847 con->adev = adev; 3848 3849 if (!adev->ras_enabled) 3850 return 0; 3851 3852 data = &con->eh_data; 3853 *data = kzalloc(sizeof(**data), GFP_KERNEL); 3854 if (!*data) { 3855 ret = -ENOMEM; 3856 goto out; 3857 } 3858 3859 mutex_init(&con->recovery_lock); 3860 mutex_init(&con->poison_lock); 3861 INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery); 3862 atomic_set(&con->in_recovery, 0); 3863 atomic_set(&con->rma_in_recovery, 0); 3864 con->eeprom_control.bad_channel_bitmap = 0; 3865 3866 max_eeprom_records_count = amdgpu_ras_eeprom_max_record_count(&con->eeprom_control); 3867 amdgpu_ras_validate_threshold(adev, max_eeprom_records_count); 3868 3869 if (init_bp_info) { 3870 ret = amdgpu_ras_init_badpage_info(adev); 3871 if (ret) 3872 goto free; 3873 } 3874 3875 mutex_init(&con->page_rsv_lock); 3876 INIT_KFIFO(con->poison_fifo); 3877 mutex_init(&con->page_retirement_lock); 3878 init_waitqueue_head(&con->page_retirement_wq); 3879 atomic_set(&con->page_retirement_req_cnt, 0); 3880 atomic_set(&con->poison_creation_count, 0); 3881 atomic_set(&con->poison_consumption_count, 0); 3882 con->page_retirement_thread = 3883 kthread_run(amdgpu_ras_page_retirement_thread, adev, "umc_page_retirement"); 3884 if (IS_ERR(con->page_retirement_thread)) { 3885 con->page_retirement_thread = NULL; 3886 dev_warn(adev->dev, "Failed to create umc_page_retirement thread!!!\n"); 3887 } 3888 3889 INIT_DELAYED_WORK(&con->page_retirement_dwork, amdgpu_ras_do_page_retirement); 3890 amdgpu_ras_ecc_log_init(&con->umc_ecc_log); 3891 #ifdef CONFIG_X86_MCE_AMD 3892 if ((adev->asic_type == CHIP_ALDEBARAN) && 3893 (adev->gmc.xgmi.connected_to_cpu)) 3894 amdgpu_register_bad_pages_mca_notifier(adev); 3895 #endif 3896 return 0; 3897 3898 free: 3899 kfree((*data)->bps); 3900 kfree(*data); 3901 con->eh_data = NULL; 3902 out: 3903 dev_warn(adev->dev, "Failed to initialize ras recovery! (%d)\n", ret); 3904 3905 /* 3906 * Except error threshold exceeding case, other failure cases in this 3907 * function would not fail amdgpu driver init. 3908 */ 3909 if (!amdgpu_ras_is_rma(adev)) 3910 ret = 0; 3911 else 3912 ret = -EINVAL; 3913 3914 return ret; 3915 } 3916 3917 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev) 3918 { 3919 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 3920 struct ras_err_handler_data *data = con->eh_data; 3921 int max_flush_timeout = MAX_FLUSH_RETIRE_DWORK_TIMES; 3922 bool ret; 3923 3924 /* recovery_init failed to init it, fini is useless */ 3925 if (!data) 3926 return 0; 3927 3928 /* Save all cached bad pages to eeprom */ 3929 do { 3930 flush_delayed_work(&con->page_retirement_dwork); 3931 ret = amdgpu_ras_schedule_retirement_dwork(con, 0); 3932 } while (ret && max_flush_timeout--); 3933 3934 if (con->page_retirement_thread) 3935 kthread_stop(con->page_retirement_thread); 3936 3937 atomic_set(&con->page_retirement_req_cnt, 0); 3938 atomic_set(&con->poison_creation_count, 0); 3939 3940 mutex_destroy(&con->page_rsv_lock); 3941 3942 cancel_work_sync(&con->recovery_work); 3943 3944 cancel_delayed_work_sync(&con->page_retirement_dwork); 3945 3946 amdgpu_ras_ecc_log_fini(&con->umc_ecc_log); 3947 3948 mutex_lock(&con->recovery_lock); 3949 con->eh_data = NULL; 3950 kfree(data->bps); 3951 kfree(data); 3952 mutex_unlock(&con->recovery_lock); 3953 3954 amdgpu_ras_critical_region_init(adev); 3955 #ifdef CONFIG_X86_MCE_AMD 3956 amdgpu_unregister_bad_pages_mca_notifier(adev); 3957 #endif 3958 return 0; 3959 } 3960 /* recovery end */ 3961 3962 static bool amdgpu_ras_asic_supported(struct amdgpu_device *adev) 3963 { 3964 if (amdgpu_sriov_vf(adev)) { 3965 switch (amdgpu_ip_version(adev, MP0_HWIP, 0)) { 3966 case IP_VERSION(13, 0, 2): 3967 case IP_VERSION(13, 0, 6): 3968 case IP_VERSION(13, 0, 12): 3969 case IP_VERSION(13, 0, 14): 3970 return true; 3971 default: 3972 return false; 3973 } 3974 } 3975 3976 if (adev->asic_type == CHIP_IP_DISCOVERY) { 3977 switch (amdgpu_ip_version(adev, MP0_HWIP, 0)) { 3978 case IP_VERSION(13, 0, 0): 3979 case IP_VERSION(13, 0, 6): 3980 case IP_VERSION(13, 0, 10): 3981 case IP_VERSION(13, 0, 12): 3982 case IP_VERSION(13, 0, 14): 3983 case IP_VERSION(14, 0, 3): 3984 return true; 3985 default: 3986 return false; 3987 } 3988 } 3989 3990 return adev->asic_type == CHIP_VEGA10 || 3991 adev->asic_type == CHIP_VEGA20 || 3992 adev->asic_type == CHIP_ARCTURUS || 3993 adev->asic_type == CHIP_ALDEBARAN || 3994 adev->asic_type == CHIP_SIENNA_CICHLID; 3995 } 3996 3997 /* 3998 * this is workaround for vega20 workstation sku, 3999 * force enable gfx ras, ignore vbios gfx ras flag 4000 * due to GC EDC can not write 4001 */ 4002 static void amdgpu_ras_get_quirks(struct amdgpu_device *adev) 4003 { 4004 struct atom_context *ctx = adev->mode_info.atom_context; 4005 4006 if (!ctx) 4007 return; 4008 4009 if (strnstr(ctx->vbios_pn, "D16406", 4010 sizeof(ctx->vbios_pn)) || 4011 strnstr(ctx->vbios_pn, "D36002", 4012 sizeof(ctx->vbios_pn))) 4013 adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__GFX); 4014 } 4015 4016 /* Query ras capablity via atomfirmware interface */ 4017 static void amdgpu_ras_query_ras_capablity_from_vbios(struct amdgpu_device *adev) 4018 { 4019 /* mem_ecc cap */ 4020 if (amdgpu_atomfirmware_mem_ecc_supported(adev)) { 4021 dev_info(adev->dev, "MEM ECC is active.\n"); 4022 adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__UMC | 4023 1 << AMDGPU_RAS_BLOCK__DF); 4024 } else { 4025 dev_info(adev->dev, "MEM ECC is not presented.\n"); 4026 } 4027 4028 /* sram_ecc cap */ 4029 if (amdgpu_atomfirmware_sram_ecc_supported(adev)) { 4030 dev_info(adev->dev, "SRAM ECC is active.\n"); 4031 if (!amdgpu_sriov_vf(adev)) 4032 adev->ras_hw_enabled |= ~(1 << AMDGPU_RAS_BLOCK__UMC | 4033 1 << AMDGPU_RAS_BLOCK__DF); 4034 else 4035 adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__PCIE_BIF | 4036 1 << AMDGPU_RAS_BLOCK__SDMA | 4037 1 << AMDGPU_RAS_BLOCK__GFX); 4038 4039 /* 4040 * VCN/JPEG RAS can be supported on both bare metal and 4041 * SRIOV environment 4042 */ 4043 if (amdgpu_ip_version(adev, VCN_HWIP, 0) == IP_VERSION(2, 6, 0) || 4044 amdgpu_ip_version(adev, VCN_HWIP, 0) == IP_VERSION(4, 0, 0) || 4045 amdgpu_ip_version(adev, VCN_HWIP, 0) == IP_VERSION(4, 0, 3) || 4046 amdgpu_ip_version(adev, VCN_HWIP, 0) == IP_VERSION(5, 0, 1)) 4047 adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__VCN | 4048 1 << AMDGPU_RAS_BLOCK__JPEG); 4049 else 4050 adev->ras_hw_enabled &= ~(1 << AMDGPU_RAS_BLOCK__VCN | 4051 1 << AMDGPU_RAS_BLOCK__JPEG); 4052 4053 /* 4054 * XGMI RAS is not supported if xgmi num physical nodes 4055 * is zero 4056 */ 4057 if (!adev->gmc.xgmi.num_physical_nodes) 4058 adev->ras_hw_enabled &= ~(1 << AMDGPU_RAS_BLOCK__XGMI_WAFL); 4059 } else { 4060 dev_info(adev->dev, "SRAM ECC is not presented.\n"); 4061 } 4062 } 4063 4064 /* Query poison mode from umc/df IP callbacks */ 4065 static void amdgpu_ras_query_poison_mode(struct amdgpu_device *adev) 4066 { 4067 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 4068 bool df_poison, umc_poison; 4069 4070 /* poison setting is useless on SRIOV guest */ 4071 if (amdgpu_sriov_vf(adev) || !con) 4072 return; 4073 4074 /* Init poison supported flag, the default value is false */ 4075 if (adev->gmc.xgmi.connected_to_cpu || 4076 adev->gmc.is_app_apu) { 4077 /* enabled by default when GPU is connected to CPU */ 4078 con->poison_supported = true; 4079 } else if (adev->df.funcs && 4080 adev->df.funcs->query_ras_poison_mode && 4081 adev->umc.ras && 4082 adev->umc.ras->query_ras_poison_mode) { 4083 df_poison = 4084 adev->df.funcs->query_ras_poison_mode(adev); 4085 umc_poison = 4086 adev->umc.ras->query_ras_poison_mode(adev); 4087 4088 /* Only poison is set in both DF and UMC, we can support it */ 4089 if (df_poison && umc_poison) 4090 con->poison_supported = true; 4091 else if (df_poison != umc_poison) 4092 dev_warn(adev->dev, 4093 "Poison setting is inconsistent in DF/UMC(%d:%d)!\n", 4094 df_poison, umc_poison); 4095 } 4096 } 4097 4098 /* 4099 * check hardware's ras ability which will be saved in hw_supported. 4100 * if hardware does not support ras, we can skip some ras initializtion and 4101 * forbid some ras operations from IP. 4102 * if software itself, say boot parameter, limit the ras ability. We still 4103 * need allow IP do some limited operations, like disable. In such case, 4104 * we have to initialize ras as normal. but need check if operation is 4105 * allowed or not in each function. 4106 */ 4107 static void amdgpu_ras_check_supported(struct amdgpu_device *adev) 4108 { 4109 adev->ras_hw_enabled = adev->ras_enabled = 0; 4110 4111 if (!amdgpu_ras_asic_supported(adev)) 4112 return; 4113 4114 if (amdgpu_sriov_vf(adev)) { 4115 if (amdgpu_virt_get_ras_capability(adev)) 4116 goto init_ras_enabled_flag; 4117 } 4118 4119 /* query ras capability from psp */ 4120 if (amdgpu_psp_get_ras_capability(&adev->psp)) 4121 goto init_ras_enabled_flag; 4122 4123 /* query ras capablity from bios */ 4124 if (!adev->gmc.xgmi.connected_to_cpu && !adev->gmc.is_app_apu) { 4125 amdgpu_ras_query_ras_capablity_from_vbios(adev); 4126 } else { 4127 /* driver only manages a few IP blocks RAS feature 4128 * when GPU is connected cpu through XGMI */ 4129 adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__GFX | 4130 1 << AMDGPU_RAS_BLOCK__SDMA | 4131 1 << AMDGPU_RAS_BLOCK__MMHUB); 4132 } 4133 4134 /* apply asic specific settings (vega20 only for now) */ 4135 amdgpu_ras_get_quirks(adev); 4136 4137 /* query poison mode from umc/df ip callback */ 4138 amdgpu_ras_query_poison_mode(adev); 4139 4140 init_ras_enabled_flag: 4141 /* hw_supported needs to be aligned with RAS block mask. */ 4142 adev->ras_hw_enabled &= AMDGPU_RAS_BLOCK_MASK; 4143 4144 adev->ras_enabled = amdgpu_ras_enable == 0 ? 0 : 4145 adev->ras_hw_enabled & amdgpu_ras_mask; 4146 4147 /* aca is disabled by default except for psp v13_0_6/v13_0_12/v13_0_14 */ 4148 if (!amdgpu_sriov_vf(adev)) { 4149 adev->aca.is_enabled = 4150 (amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(13, 0, 6) || 4151 amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(13, 0, 12) || 4152 amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(13, 0, 14)); 4153 } 4154 4155 /* bad page feature is not applicable to specific app platform */ 4156 if (adev->gmc.is_app_apu && 4157 amdgpu_ip_version(adev, UMC_HWIP, 0) == IP_VERSION(12, 0, 0)) 4158 amdgpu_bad_page_threshold = 0; 4159 } 4160 4161 static void amdgpu_ras_counte_dw(struct work_struct *work) 4162 { 4163 struct amdgpu_ras *con = container_of(work, struct amdgpu_ras, 4164 ras_counte_delay_work.work); 4165 struct amdgpu_device *adev = con->adev; 4166 struct drm_device *dev = adev_to_drm(adev); 4167 unsigned long ce_count, ue_count; 4168 int res; 4169 4170 res = pm_runtime_get_sync(dev->dev); 4171 if (res < 0) 4172 goto Out; 4173 4174 /* Cache new values. 4175 */ 4176 if (amdgpu_ras_query_error_count(adev, &ce_count, &ue_count, NULL) == 0) { 4177 atomic_set(&con->ras_ce_count, ce_count); 4178 atomic_set(&con->ras_ue_count, ue_count); 4179 } 4180 4181 Out: 4182 pm_runtime_put_autosuspend(dev->dev); 4183 } 4184 4185 static int amdgpu_get_ras_schema(struct amdgpu_device *adev) 4186 { 4187 return amdgpu_ras_is_poison_mode_supported(adev) ? AMDGPU_RAS_ERROR__POISON : 0 | 4188 AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE | 4189 AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE | 4190 AMDGPU_RAS_ERROR__PARITY; 4191 } 4192 4193 static void ras_event_mgr_init(struct ras_event_manager *mgr) 4194 { 4195 struct ras_event_state *event_state; 4196 int i; 4197 4198 memset(mgr, 0, sizeof(*mgr)); 4199 atomic64_set(&mgr->seqno, 0); 4200 4201 for (i = 0; i < ARRAY_SIZE(mgr->event_state); i++) { 4202 event_state = &mgr->event_state[i]; 4203 event_state->last_seqno = RAS_EVENT_INVALID_ID; 4204 atomic64_set(&event_state->count, 0); 4205 } 4206 } 4207 4208 static void amdgpu_ras_event_mgr_init(struct amdgpu_device *adev) 4209 { 4210 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev); 4211 struct amdgpu_hive_info *hive; 4212 4213 if (!ras) 4214 return; 4215 4216 hive = amdgpu_get_xgmi_hive(adev); 4217 ras->event_mgr = hive ? &hive->event_mgr : &ras->__event_mgr; 4218 4219 /* init event manager with node 0 on xgmi system */ 4220 if (!amdgpu_reset_in_recovery(adev)) { 4221 if (!hive || adev->gmc.xgmi.node_id == 0) 4222 ras_event_mgr_init(ras->event_mgr); 4223 } 4224 4225 if (hive) 4226 amdgpu_put_xgmi_hive(hive); 4227 } 4228 4229 static void amdgpu_ras_init_reserved_vram_size(struct amdgpu_device *adev) 4230 { 4231 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 4232 4233 if (!con || (adev->flags & AMD_IS_APU)) 4234 return; 4235 4236 switch (amdgpu_ip_version(adev, MP0_HWIP, 0)) { 4237 case IP_VERSION(13, 0, 2): 4238 case IP_VERSION(13, 0, 6): 4239 case IP_VERSION(13, 0, 12): 4240 con->reserved_pages_in_bytes = AMDGPU_RAS_RESERVED_VRAM_SIZE_DEFAULT; 4241 break; 4242 case IP_VERSION(13, 0, 14): 4243 con->reserved_pages_in_bytes = (AMDGPU_RAS_RESERVED_VRAM_SIZE_DEFAULT << 1); 4244 break; 4245 default: 4246 break; 4247 } 4248 } 4249 4250 int amdgpu_ras_init(struct amdgpu_device *adev) 4251 { 4252 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 4253 int r; 4254 4255 if (con) 4256 return 0; 4257 4258 con = kzalloc(sizeof(*con) + 4259 sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT + 4260 sizeof(struct ras_manager) * AMDGPU_RAS_MCA_BLOCK_COUNT, 4261 GFP_KERNEL); 4262 if (!con) 4263 return -ENOMEM; 4264 4265 con->adev = adev; 4266 INIT_DELAYED_WORK(&con->ras_counte_delay_work, amdgpu_ras_counte_dw); 4267 atomic_set(&con->ras_ce_count, 0); 4268 atomic_set(&con->ras_ue_count, 0); 4269 4270 con->objs = (struct ras_manager *)(con + 1); 4271 4272 amdgpu_ras_set_context(adev, con); 4273 4274 amdgpu_ras_check_supported(adev); 4275 4276 if (!adev->ras_enabled || adev->asic_type == CHIP_VEGA10) { 4277 /* set gfx block ras context feature for VEGA20 Gaming 4278 * send ras disable cmd to ras ta during ras late init. 4279 */ 4280 if (!adev->ras_enabled && adev->asic_type == CHIP_VEGA20) { 4281 con->features |= BIT(AMDGPU_RAS_BLOCK__GFX); 4282 4283 return 0; 4284 } 4285 4286 r = 0; 4287 goto release_con; 4288 } 4289 4290 con->update_channel_flag = false; 4291 con->features = 0; 4292 con->schema = 0; 4293 INIT_LIST_HEAD(&con->head); 4294 /* Might need get this flag from vbios. */ 4295 con->flags = RAS_DEFAULT_FLAGS; 4296 4297 /* initialize nbio ras function ahead of any other 4298 * ras functions so hardware fatal error interrupt 4299 * can be enabled as early as possible */ 4300 switch (amdgpu_ip_version(adev, NBIO_HWIP, 0)) { 4301 case IP_VERSION(7, 4, 0): 4302 case IP_VERSION(7, 4, 1): 4303 case IP_VERSION(7, 4, 4): 4304 if (!adev->gmc.xgmi.connected_to_cpu) 4305 adev->nbio.ras = &nbio_v7_4_ras; 4306 break; 4307 case IP_VERSION(4, 3, 0): 4308 if (adev->ras_hw_enabled & (1 << AMDGPU_RAS_BLOCK__DF)) 4309 /* unlike other generation of nbio ras, 4310 * nbio v4_3 only support fatal error interrupt 4311 * to inform software that DF is freezed due to 4312 * system fatal error event. driver should not 4313 * enable nbio ras in such case. Instead, 4314 * check DF RAS */ 4315 adev->nbio.ras = &nbio_v4_3_ras; 4316 break; 4317 case IP_VERSION(6, 3, 1): 4318 if (adev->ras_hw_enabled & (1 << AMDGPU_RAS_BLOCK__DF)) 4319 /* unlike other generation of nbio ras, 4320 * nbif v6_3_1 only support fatal error interrupt 4321 * to inform software that DF is freezed due to 4322 * system fatal error event. driver should not 4323 * enable nbio ras in such case. Instead, 4324 * check DF RAS 4325 */ 4326 adev->nbio.ras = &nbif_v6_3_1_ras; 4327 break; 4328 case IP_VERSION(7, 9, 0): 4329 case IP_VERSION(7, 9, 1): 4330 if (!adev->gmc.is_app_apu) 4331 adev->nbio.ras = &nbio_v7_9_ras; 4332 break; 4333 default: 4334 /* nbio ras is not available */ 4335 break; 4336 } 4337 4338 /* nbio ras block needs to be enabled ahead of other ras blocks 4339 * to handle fatal error */ 4340 r = amdgpu_nbio_ras_sw_init(adev); 4341 if (r) 4342 return r; 4343 4344 if (adev->nbio.ras && 4345 adev->nbio.ras->init_ras_controller_interrupt) { 4346 r = adev->nbio.ras->init_ras_controller_interrupt(adev); 4347 if (r) 4348 goto release_con; 4349 } 4350 4351 if (adev->nbio.ras && 4352 adev->nbio.ras->init_ras_err_event_athub_interrupt) { 4353 r = adev->nbio.ras->init_ras_err_event_athub_interrupt(adev); 4354 if (r) 4355 goto release_con; 4356 } 4357 4358 /* Packed socket_id to ras feature mask bits[31:29] */ 4359 if (adev->smuio.funcs && 4360 adev->smuio.funcs->get_socket_id) 4361 con->features |= ((adev->smuio.funcs->get_socket_id(adev)) << 4362 AMDGPU_RAS_FEATURES_SOCKETID_SHIFT); 4363 4364 /* Get RAS schema for particular SOC */ 4365 con->schema = amdgpu_get_ras_schema(adev); 4366 4367 amdgpu_ras_init_reserved_vram_size(adev); 4368 4369 if (amdgpu_ras_fs_init(adev)) { 4370 r = -EINVAL; 4371 goto release_con; 4372 } 4373 4374 if (amdgpu_ras_aca_is_supported(adev)) { 4375 if (amdgpu_aca_is_enabled(adev)) 4376 r = amdgpu_aca_init(adev); 4377 else 4378 r = amdgpu_mca_init(adev); 4379 if (r) 4380 goto release_con; 4381 } 4382 4383 con->init_task_pid = task_pid_nr(current); 4384 get_task_comm(con->init_task_comm, current); 4385 4386 mutex_init(&con->critical_region_lock); 4387 INIT_LIST_HEAD(&con->critical_region_head); 4388 4389 dev_info(adev->dev, "RAS INFO: ras initialized successfully, " 4390 "hardware ability[%x] ras_mask[%x]\n", 4391 adev->ras_hw_enabled, adev->ras_enabled); 4392 4393 return 0; 4394 release_con: 4395 amdgpu_ras_set_context(adev, NULL); 4396 kfree(con); 4397 4398 return r; 4399 } 4400 4401 int amdgpu_persistent_edc_harvesting_supported(struct amdgpu_device *adev) 4402 { 4403 if (adev->gmc.xgmi.connected_to_cpu || 4404 adev->gmc.is_app_apu) 4405 return 1; 4406 return 0; 4407 } 4408 4409 static int amdgpu_persistent_edc_harvesting(struct amdgpu_device *adev, 4410 struct ras_common_if *ras_block) 4411 { 4412 struct ras_query_if info = { 4413 .head = *ras_block, 4414 }; 4415 4416 if (!amdgpu_persistent_edc_harvesting_supported(adev)) 4417 return 0; 4418 4419 if (amdgpu_ras_query_error_status(adev, &info) != 0) 4420 DRM_WARN("RAS init harvest failure"); 4421 4422 if (amdgpu_ras_reset_error_status(adev, ras_block->block) != 0) 4423 DRM_WARN("RAS init harvest reset failure"); 4424 4425 return 0; 4426 } 4427 4428 bool amdgpu_ras_is_poison_mode_supported(struct amdgpu_device *adev) 4429 { 4430 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 4431 4432 if (!con) 4433 return false; 4434 4435 return con->poison_supported; 4436 } 4437 4438 /* helper function to handle common stuff in ip late init phase */ 4439 int amdgpu_ras_block_late_init(struct amdgpu_device *adev, 4440 struct ras_common_if *ras_block) 4441 { 4442 struct amdgpu_ras_block_object *ras_obj = NULL; 4443 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 4444 struct ras_query_if *query_info; 4445 unsigned long ue_count, ce_count; 4446 int r; 4447 4448 /* disable RAS feature per IP block if it is not supported */ 4449 if (!amdgpu_ras_is_supported(adev, ras_block->block)) { 4450 amdgpu_ras_feature_enable_on_boot(adev, ras_block, 0); 4451 return 0; 4452 } 4453 4454 r = amdgpu_ras_feature_enable_on_boot(adev, ras_block, 1); 4455 if (r) { 4456 if (adev->in_suspend || amdgpu_reset_in_recovery(adev)) { 4457 /* in resume phase, if fail to enable ras, 4458 * clean up all ras fs nodes, and disable ras */ 4459 goto cleanup; 4460 } else 4461 return r; 4462 } 4463 4464 /* check for errors on warm reset edc persisant supported ASIC */ 4465 amdgpu_persistent_edc_harvesting(adev, ras_block); 4466 4467 /* in resume phase, no need to create ras fs node */ 4468 if (adev->in_suspend || amdgpu_reset_in_recovery(adev)) 4469 return 0; 4470 4471 ras_obj = container_of(ras_block, struct amdgpu_ras_block_object, ras_comm); 4472 if (ras_obj->ras_cb || (ras_obj->hw_ops && 4473 (ras_obj->hw_ops->query_poison_status || 4474 ras_obj->hw_ops->handle_poison_consumption))) { 4475 r = amdgpu_ras_interrupt_add_handler(adev, ras_block); 4476 if (r) 4477 goto cleanup; 4478 } 4479 4480 if (ras_obj->hw_ops && 4481 (ras_obj->hw_ops->query_ras_error_count || 4482 ras_obj->hw_ops->query_ras_error_status)) { 4483 r = amdgpu_ras_sysfs_create(adev, ras_block); 4484 if (r) 4485 goto interrupt; 4486 4487 /* Those are the cached values at init. 4488 */ 4489 query_info = kzalloc(sizeof(*query_info), GFP_KERNEL); 4490 if (!query_info) 4491 return -ENOMEM; 4492 memcpy(&query_info->head, ras_block, sizeof(struct ras_common_if)); 4493 4494 if (amdgpu_ras_query_error_count(adev, &ce_count, &ue_count, query_info) == 0) { 4495 atomic_set(&con->ras_ce_count, ce_count); 4496 atomic_set(&con->ras_ue_count, ue_count); 4497 } 4498 4499 kfree(query_info); 4500 } 4501 4502 return 0; 4503 4504 interrupt: 4505 if (ras_obj->ras_cb) 4506 amdgpu_ras_interrupt_remove_handler(adev, ras_block); 4507 cleanup: 4508 amdgpu_ras_feature_enable(adev, ras_block, 0); 4509 return r; 4510 } 4511 4512 static int amdgpu_ras_block_late_init_default(struct amdgpu_device *adev, 4513 struct ras_common_if *ras_block) 4514 { 4515 return amdgpu_ras_block_late_init(adev, ras_block); 4516 } 4517 4518 /* helper function to remove ras fs node and interrupt handler */ 4519 void amdgpu_ras_block_late_fini(struct amdgpu_device *adev, 4520 struct ras_common_if *ras_block) 4521 { 4522 struct amdgpu_ras_block_object *ras_obj; 4523 if (!ras_block) 4524 return; 4525 4526 amdgpu_ras_sysfs_remove(adev, ras_block); 4527 4528 ras_obj = container_of(ras_block, struct amdgpu_ras_block_object, ras_comm); 4529 if (ras_obj->ras_cb) 4530 amdgpu_ras_interrupt_remove_handler(adev, ras_block); 4531 } 4532 4533 static void amdgpu_ras_block_late_fini_default(struct amdgpu_device *adev, 4534 struct ras_common_if *ras_block) 4535 { 4536 return amdgpu_ras_block_late_fini(adev, ras_block); 4537 } 4538 4539 /* do some init work after IP late init as dependence. 4540 * and it runs in resume/gpu reset/booting up cases. 4541 */ 4542 void amdgpu_ras_resume(struct amdgpu_device *adev) 4543 { 4544 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 4545 struct ras_manager *obj, *tmp; 4546 4547 if (!adev->ras_enabled || !con) { 4548 /* clean ras context for VEGA20 Gaming after send ras disable cmd */ 4549 amdgpu_release_ras_context(adev); 4550 4551 return; 4552 } 4553 4554 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) { 4555 /* Set up all other IPs which are not implemented. There is a 4556 * tricky thing that IP's actual ras error type should be 4557 * MULTI_UNCORRECTABLE, but as driver does not handle it, so 4558 * ERROR_NONE make sense anyway. 4559 */ 4560 amdgpu_ras_enable_all_features(adev, 1); 4561 4562 /* We enable ras on all hw_supported block, but as boot 4563 * parameter might disable some of them and one or more IP has 4564 * not implemented yet. So we disable them on behalf. 4565 */ 4566 list_for_each_entry_safe(obj, tmp, &con->head, node) { 4567 if (!amdgpu_ras_is_supported(adev, obj->head.block)) { 4568 amdgpu_ras_feature_enable(adev, &obj->head, 0); 4569 /* there should be no any reference. */ 4570 WARN_ON(alive_obj(obj)); 4571 } 4572 } 4573 } 4574 } 4575 4576 void amdgpu_ras_suspend(struct amdgpu_device *adev) 4577 { 4578 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 4579 4580 if (!adev->ras_enabled || !con) 4581 return; 4582 4583 amdgpu_ras_disable_all_features(adev, 0); 4584 /* Make sure all ras objects are disabled. */ 4585 if (AMDGPU_RAS_GET_FEATURES(con->features)) 4586 amdgpu_ras_disable_all_features(adev, 1); 4587 } 4588 4589 int amdgpu_ras_late_init(struct amdgpu_device *adev) 4590 { 4591 struct amdgpu_ras_block_list *node, *tmp; 4592 struct amdgpu_ras_block_object *obj; 4593 int r; 4594 4595 amdgpu_ras_event_mgr_init(adev); 4596 4597 if (amdgpu_ras_aca_is_supported(adev)) { 4598 if (amdgpu_reset_in_recovery(adev)) { 4599 if (amdgpu_aca_is_enabled(adev)) 4600 r = amdgpu_aca_reset(adev); 4601 else 4602 r = amdgpu_mca_reset(adev); 4603 if (r) 4604 return r; 4605 } 4606 4607 if (!amdgpu_sriov_vf(adev)) { 4608 if (amdgpu_aca_is_enabled(adev)) 4609 amdgpu_ras_set_aca_debug_mode(adev, false); 4610 else 4611 amdgpu_ras_set_mca_debug_mode(adev, false); 4612 } 4613 } 4614 4615 /* Guest side doesn't need init ras feature */ 4616 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_ras_telemetry_en(adev)) 4617 return 0; 4618 4619 list_for_each_entry_safe(node, tmp, &adev->ras_list, node) { 4620 obj = node->ras_obj; 4621 if (!obj) { 4622 dev_warn(adev->dev, "Warning: abnormal ras list node.\n"); 4623 continue; 4624 } 4625 4626 if (!amdgpu_ras_is_supported(adev, obj->ras_comm.block)) 4627 continue; 4628 4629 if (obj->ras_late_init) { 4630 r = obj->ras_late_init(adev, &obj->ras_comm); 4631 if (r) { 4632 dev_err(adev->dev, "%s failed to execute ras_late_init! ret:%d\n", 4633 obj->ras_comm.name, r); 4634 return r; 4635 } 4636 } else 4637 amdgpu_ras_block_late_init_default(adev, &obj->ras_comm); 4638 } 4639 4640 return 0; 4641 } 4642 4643 /* do some fini work before IP fini as dependence */ 4644 int amdgpu_ras_pre_fini(struct amdgpu_device *adev) 4645 { 4646 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 4647 4648 if (!adev->ras_enabled || !con) 4649 return 0; 4650 4651 4652 /* Need disable ras on all IPs here before ip [hw/sw]fini */ 4653 if (AMDGPU_RAS_GET_FEATURES(con->features)) 4654 amdgpu_ras_disable_all_features(adev, 0); 4655 amdgpu_ras_recovery_fini(adev); 4656 return 0; 4657 } 4658 4659 int amdgpu_ras_fini(struct amdgpu_device *adev) 4660 { 4661 struct amdgpu_ras_block_list *ras_node, *tmp; 4662 struct amdgpu_ras_block_object *obj = NULL; 4663 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 4664 4665 if (!adev->ras_enabled || !con) 4666 return 0; 4667 4668 amdgpu_ras_critical_region_fini(adev); 4669 mutex_destroy(&con->critical_region_lock); 4670 4671 list_for_each_entry_safe(ras_node, tmp, &adev->ras_list, node) { 4672 if (ras_node->ras_obj) { 4673 obj = ras_node->ras_obj; 4674 if (amdgpu_ras_is_supported(adev, obj->ras_comm.block) && 4675 obj->ras_fini) 4676 obj->ras_fini(adev, &obj->ras_comm); 4677 else 4678 amdgpu_ras_block_late_fini_default(adev, &obj->ras_comm); 4679 } 4680 4681 /* Clear ras blocks from ras_list and free ras block list node */ 4682 list_del(&ras_node->node); 4683 kfree(ras_node); 4684 } 4685 4686 amdgpu_ras_fs_fini(adev); 4687 amdgpu_ras_interrupt_remove_all(adev); 4688 4689 if (amdgpu_ras_aca_is_supported(adev)) { 4690 if (amdgpu_aca_is_enabled(adev)) 4691 amdgpu_aca_fini(adev); 4692 else 4693 amdgpu_mca_fini(adev); 4694 } 4695 4696 WARN(AMDGPU_RAS_GET_FEATURES(con->features), "Feature mask is not cleared"); 4697 4698 if (AMDGPU_RAS_GET_FEATURES(con->features)) 4699 amdgpu_ras_disable_all_features(adev, 0); 4700 4701 cancel_delayed_work_sync(&con->ras_counte_delay_work); 4702 4703 amdgpu_ras_set_context(adev, NULL); 4704 kfree(con); 4705 4706 return 0; 4707 } 4708 4709 bool amdgpu_ras_get_fed_status(struct amdgpu_device *adev) 4710 { 4711 struct amdgpu_ras *ras; 4712 4713 ras = amdgpu_ras_get_context(adev); 4714 if (!ras) 4715 return false; 4716 4717 return test_bit(AMDGPU_RAS_BLOCK__LAST, &ras->ras_err_state); 4718 } 4719 4720 void amdgpu_ras_set_fed(struct amdgpu_device *adev, bool status) 4721 { 4722 struct amdgpu_ras *ras; 4723 4724 ras = amdgpu_ras_get_context(adev); 4725 if (ras) { 4726 if (status) 4727 set_bit(AMDGPU_RAS_BLOCK__LAST, &ras->ras_err_state); 4728 else 4729 clear_bit(AMDGPU_RAS_BLOCK__LAST, &ras->ras_err_state); 4730 } 4731 } 4732 4733 void amdgpu_ras_clear_err_state(struct amdgpu_device *adev) 4734 { 4735 struct amdgpu_ras *ras; 4736 4737 ras = amdgpu_ras_get_context(adev); 4738 if (ras) { 4739 ras->ras_err_state = 0; 4740 ras->gpu_reset_flags = 0; 4741 } 4742 } 4743 4744 void amdgpu_ras_set_err_poison(struct amdgpu_device *adev, 4745 enum amdgpu_ras_block block) 4746 { 4747 struct amdgpu_ras *ras; 4748 4749 ras = amdgpu_ras_get_context(adev); 4750 if (ras) 4751 set_bit(block, &ras->ras_err_state); 4752 } 4753 4754 bool amdgpu_ras_is_err_state(struct amdgpu_device *adev, int block) 4755 { 4756 struct amdgpu_ras *ras; 4757 4758 ras = amdgpu_ras_get_context(adev); 4759 if (ras) { 4760 if (block == AMDGPU_RAS_BLOCK__ANY) 4761 return (ras->ras_err_state != 0); 4762 else 4763 return test_bit(block, &ras->ras_err_state) || 4764 test_bit(AMDGPU_RAS_BLOCK__LAST, 4765 &ras->ras_err_state); 4766 } 4767 4768 return false; 4769 } 4770 4771 static struct ras_event_manager *__get_ras_event_mgr(struct amdgpu_device *adev) 4772 { 4773 struct amdgpu_ras *ras; 4774 4775 ras = amdgpu_ras_get_context(adev); 4776 if (!ras) 4777 return NULL; 4778 4779 return ras->event_mgr; 4780 } 4781 4782 int amdgpu_ras_mark_ras_event_caller(struct amdgpu_device *adev, enum ras_event_type type, 4783 const void *caller) 4784 { 4785 struct ras_event_manager *event_mgr; 4786 struct ras_event_state *event_state; 4787 int ret = 0; 4788 4789 if (amdgpu_uniras_enabled(adev)) 4790 return 0; 4791 4792 if (type >= RAS_EVENT_TYPE_COUNT) { 4793 ret = -EINVAL; 4794 goto out; 4795 } 4796 4797 event_mgr = __get_ras_event_mgr(adev); 4798 if (!event_mgr) { 4799 ret = -EINVAL; 4800 goto out; 4801 } 4802 4803 event_state = &event_mgr->event_state[type]; 4804 event_state->last_seqno = atomic64_inc_return(&event_mgr->seqno); 4805 atomic64_inc(&event_state->count); 4806 4807 out: 4808 if (ret && caller) 4809 dev_warn(adev->dev, "failed mark ras event (%d) in %ps, ret:%d\n", 4810 (int)type, caller, ret); 4811 4812 return ret; 4813 } 4814 4815 u64 amdgpu_ras_acquire_event_id(struct amdgpu_device *adev, enum ras_event_type type) 4816 { 4817 struct ras_event_manager *event_mgr; 4818 u64 id; 4819 4820 if (type >= RAS_EVENT_TYPE_COUNT) 4821 return RAS_EVENT_INVALID_ID; 4822 4823 switch (type) { 4824 case RAS_EVENT_TYPE_FATAL: 4825 case RAS_EVENT_TYPE_POISON_CREATION: 4826 case RAS_EVENT_TYPE_POISON_CONSUMPTION: 4827 event_mgr = __get_ras_event_mgr(adev); 4828 if (!event_mgr) 4829 return RAS_EVENT_INVALID_ID; 4830 4831 id = event_mgr->event_state[type].last_seqno; 4832 break; 4833 case RAS_EVENT_TYPE_INVALID: 4834 default: 4835 id = RAS_EVENT_INVALID_ID; 4836 break; 4837 } 4838 4839 return id; 4840 } 4841 4842 int amdgpu_ras_global_ras_isr(struct amdgpu_device *adev) 4843 { 4844 if (atomic_cmpxchg(&amdgpu_ras_in_intr, 0, 1) == 0) { 4845 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev); 4846 enum ras_event_type type = RAS_EVENT_TYPE_FATAL; 4847 u64 event_id = RAS_EVENT_INVALID_ID; 4848 4849 if (amdgpu_uniras_enabled(adev)) 4850 return 0; 4851 4852 if (!amdgpu_ras_mark_ras_event(adev, type)) 4853 event_id = amdgpu_ras_acquire_event_id(adev, type); 4854 4855 RAS_EVENT_LOG(adev, event_id, "uncorrectable hardware error" 4856 "(ERREVENT_ATHUB_INTERRUPT) detected!\n"); 4857 4858 amdgpu_ras_set_fed(adev, true); 4859 ras->gpu_reset_flags |= AMDGPU_RAS_GPU_RESET_MODE1_RESET; 4860 amdgpu_ras_reset_gpu(adev); 4861 } 4862 4863 return -EBUSY; 4864 } 4865 4866 bool amdgpu_ras_need_emergency_restart(struct amdgpu_device *adev) 4867 { 4868 if (adev->asic_type == CHIP_VEGA20 && 4869 adev->pm.fw_version <= 0x283400) { 4870 return !(amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) && 4871 amdgpu_ras_intr_triggered(); 4872 } 4873 4874 return false; 4875 } 4876 4877 void amdgpu_release_ras_context(struct amdgpu_device *adev) 4878 { 4879 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 4880 4881 if (!con) 4882 return; 4883 4884 if (!adev->ras_enabled && con->features & BIT(AMDGPU_RAS_BLOCK__GFX)) { 4885 con->features &= ~BIT(AMDGPU_RAS_BLOCK__GFX); 4886 amdgpu_ras_set_context(adev, NULL); 4887 kfree(con); 4888 } 4889 } 4890 4891 #ifdef CONFIG_X86_MCE_AMD 4892 static struct amdgpu_device *find_adev(uint32_t node_id) 4893 { 4894 int i; 4895 struct amdgpu_device *adev = NULL; 4896 4897 for (i = 0; i < mce_adev_list.num_gpu; i++) { 4898 adev = mce_adev_list.devs[i]; 4899 4900 if (adev && adev->gmc.xgmi.connected_to_cpu && 4901 adev->gmc.xgmi.physical_node_id == node_id) 4902 break; 4903 adev = NULL; 4904 } 4905 4906 return adev; 4907 } 4908 4909 #define GET_MCA_IPID_GPUID(m) (((m) >> 44) & 0xF) 4910 #define GET_UMC_INST(m) (((m) >> 21) & 0x7) 4911 #define GET_CHAN_INDEX(m) ((((m) >> 12) & 0x3) | (((m) >> 18) & 0x4)) 4912 #define GPU_ID_OFFSET 8 4913 4914 static int amdgpu_bad_page_notifier(struct notifier_block *nb, 4915 unsigned long val, void *data) 4916 { 4917 struct mce *m = (struct mce *)data; 4918 struct amdgpu_device *adev = NULL; 4919 uint32_t gpu_id = 0; 4920 uint32_t umc_inst = 0, ch_inst = 0; 4921 4922 /* 4923 * If the error was generated in UMC_V2, which belongs to GPU UMCs, 4924 * and error occurred in DramECC (Extended error code = 0) then only 4925 * process the error, else bail out. 4926 */ 4927 if (!m || !((smca_get_bank_type(m->extcpu, m->bank) == SMCA_UMC_V2) && 4928 (XEC(m->status, 0x3f) == 0x0))) 4929 return NOTIFY_DONE; 4930 4931 /* 4932 * If it is correctable error, return. 4933 */ 4934 if (mce_is_correctable(m)) 4935 return NOTIFY_OK; 4936 4937 /* 4938 * GPU Id is offset by GPU_ID_OFFSET in MCA_IPID_UMC register. 4939 */ 4940 gpu_id = GET_MCA_IPID_GPUID(m->ipid) - GPU_ID_OFFSET; 4941 4942 adev = find_adev(gpu_id); 4943 if (!adev) { 4944 DRM_WARN("%s: Unable to find adev for gpu_id: %d\n", __func__, 4945 gpu_id); 4946 return NOTIFY_DONE; 4947 } 4948 4949 /* 4950 * If it is uncorrectable error, then find out UMC instance and 4951 * channel index. 4952 */ 4953 umc_inst = GET_UMC_INST(m->ipid); 4954 ch_inst = GET_CHAN_INDEX(m->ipid); 4955 4956 dev_info(adev->dev, "Uncorrectable error detected in UMC inst: %d, chan_idx: %d", 4957 umc_inst, ch_inst); 4958 4959 if (!amdgpu_umc_page_retirement_mca(adev, m->addr, ch_inst, umc_inst)) 4960 return NOTIFY_OK; 4961 else 4962 return NOTIFY_DONE; 4963 } 4964 4965 static struct notifier_block amdgpu_bad_page_nb = { 4966 .notifier_call = amdgpu_bad_page_notifier, 4967 .priority = MCE_PRIO_UC, 4968 }; 4969 4970 static void amdgpu_register_bad_pages_mca_notifier(struct amdgpu_device *adev) 4971 { 4972 /* 4973 * Add the adev to the mce_adev_list. 4974 * During mode2 reset, amdgpu device is temporarily 4975 * removed from the mgpu_info list which can cause 4976 * page retirement to fail. 4977 * Use this list instead of mgpu_info to find the amdgpu 4978 * device on which the UMC error was reported. 4979 */ 4980 mce_adev_list.devs[mce_adev_list.num_gpu++] = adev; 4981 4982 /* 4983 * Register the x86 notifier only once 4984 * with MCE subsystem. 4985 */ 4986 if (notifier_registered == false) { 4987 mce_register_decode_chain(&amdgpu_bad_page_nb); 4988 notifier_registered = true; 4989 } 4990 } 4991 static void amdgpu_unregister_bad_pages_mca_notifier(struct amdgpu_device *adev) 4992 { 4993 int i, j; 4994 4995 if (!notifier_registered && !mce_adev_list.num_gpu) 4996 return; 4997 for (i = 0, j = 0; i < mce_adev_list.num_gpu; i++) { 4998 if (mce_adev_list.devs[i] == adev) 4999 mce_adev_list.devs[i] = NULL; 5000 if (!mce_adev_list.devs[i]) 5001 ++j; 5002 } 5003 5004 if (j == mce_adev_list.num_gpu) { 5005 mce_adev_list.num_gpu = 0; 5006 /* Unregister x86 notifier with MCE subsystem. */ 5007 if (notifier_registered) { 5008 mce_unregister_decode_chain(&amdgpu_bad_page_nb); 5009 notifier_registered = false; 5010 } 5011 } 5012 } 5013 #endif 5014 5015 struct amdgpu_ras *amdgpu_ras_get_context(struct amdgpu_device *adev) 5016 { 5017 if (!adev) 5018 return NULL; 5019 5020 return adev->psp.ras_context.ras; 5021 } 5022 5023 int amdgpu_ras_set_context(struct amdgpu_device *adev, struct amdgpu_ras *ras_con) 5024 { 5025 if (!adev) 5026 return -EINVAL; 5027 5028 adev->psp.ras_context.ras = ras_con; 5029 return 0; 5030 } 5031 5032 /* check if ras is supported on block, say, sdma, gfx */ 5033 int amdgpu_ras_is_supported(struct amdgpu_device *adev, 5034 unsigned int block) 5035 { 5036 int ret = 0; 5037 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev); 5038 5039 if (block >= AMDGPU_RAS_BLOCK_COUNT) 5040 return 0; 5041 5042 ret = ras && (adev->ras_enabled & (1 << block)); 5043 5044 /* For the special asic with mem ecc enabled but sram ecc 5045 * not enabled, even if the ras block is not supported on 5046 * .ras_enabled, if the asic supports poison mode and the 5047 * ras block has ras configuration, it can be considered 5048 * that the ras block supports ras function. 5049 */ 5050 if (!ret && 5051 (block == AMDGPU_RAS_BLOCK__GFX || 5052 block == AMDGPU_RAS_BLOCK__SDMA || 5053 block == AMDGPU_RAS_BLOCK__VCN || 5054 block == AMDGPU_RAS_BLOCK__JPEG) && 5055 (amdgpu_ras_mask & (1 << block)) && 5056 amdgpu_ras_is_poison_mode_supported(adev) && 5057 amdgpu_ras_get_ras_block(adev, block, 0)) 5058 ret = 1; 5059 5060 return ret; 5061 } 5062 5063 int amdgpu_ras_reset_gpu(struct amdgpu_device *adev) 5064 { 5065 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev); 5066 5067 /* mode1 is the only selection for RMA status */ 5068 if (amdgpu_ras_is_rma(adev)) { 5069 ras->gpu_reset_flags = 0; 5070 ras->gpu_reset_flags |= AMDGPU_RAS_GPU_RESET_MODE1_RESET; 5071 } 5072 5073 if (atomic_cmpxchg(&ras->in_recovery, 0, 1) == 0) { 5074 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev); 5075 int hive_ras_recovery = 0; 5076 5077 if (hive) { 5078 hive_ras_recovery = atomic_read(&hive->ras_recovery); 5079 amdgpu_put_xgmi_hive(hive); 5080 } 5081 /* In the case of multiple GPUs, after a GPU has started 5082 * resetting all GPUs on hive, other GPUs do not need to 5083 * trigger GPU reset again. 5084 */ 5085 if (!hive_ras_recovery) 5086 amdgpu_reset_domain_schedule(ras->adev->reset_domain, &ras->recovery_work); 5087 else 5088 atomic_set(&ras->in_recovery, 0); 5089 } else { 5090 flush_work(&ras->recovery_work); 5091 amdgpu_reset_domain_schedule(ras->adev->reset_domain, &ras->recovery_work); 5092 } 5093 5094 return 0; 5095 } 5096 5097 int amdgpu_ras_set_mca_debug_mode(struct amdgpu_device *adev, bool enable) 5098 { 5099 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 5100 int ret = 0; 5101 5102 if (con) { 5103 ret = amdgpu_mca_smu_set_debug_mode(adev, enable); 5104 if (!ret) 5105 con->is_aca_debug_mode = enable; 5106 } 5107 5108 return ret; 5109 } 5110 5111 int amdgpu_ras_set_aca_debug_mode(struct amdgpu_device *adev, bool enable) 5112 { 5113 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 5114 int ret = 0; 5115 5116 if (con) { 5117 if (amdgpu_aca_is_enabled(adev)) 5118 ret = amdgpu_aca_smu_set_debug_mode(adev, enable); 5119 else 5120 ret = amdgpu_mca_smu_set_debug_mode(adev, enable); 5121 if (!ret) 5122 con->is_aca_debug_mode = enable; 5123 } 5124 5125 return ret; 5126 } 5127 5128 bool amdgpu_ras_get_aca_debug_mode(struct amdgpu_device *adev) 5129 { 5130 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 5131 const struct aca_smu_funcs *smu_funcs = adev->aca.smu_funcs; 5132 const struct amdgpu_mca_smu_funcs *mca_funcs = adev->mca.mca_funcs; 5133 5134 if (!con) 5135 return false; 5136 5137 if ((amdgpu_aca_is_enabled(adev) && smu_funcs && smu_funcs->set_debug_mode) || 5138 (!amdgpu_aca_is_enabled(adev) && mca_funcs && mca_funcs->mca_set_debug_mode)) 5139 return con->is_aca_debug_mode; 5140 else 5141 return true; 5142 } 5143 5144 bool amdgpu_ras_get_error_query_mode(struct amdgpu_device *adev, 5145 unsigned int *error_query_mode) 5146 { 5147 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 5148 const struct amdgpu_mca_smu_funcs *mca_funcs = adev->mca.mca_funcs; 5149 const struct aca_smu_funcs *smu_funcs = adev->aca.smu_funcs; 5150 5151 if (!con) { 5152 *error_query_mode = AMDGPU_RAS_INVALID_ERROR_QUERY; 5153 return false; 5154 } 5155 5156 if (amdgpu_sriov_vf(adev)) { 5157 *error_query_mode = AMDGPU_RAS_VIRT_ERROR_COUNT_QUERY; 5158 } else if ((smu_funcs && smu_funcs->set_debug_mode) || (mca_funcs && mca_funcs->mca_set_debug_mode)) { 5159 *error_query_mode = 5160 (con->is_aca_debug_mode) ? AMDGPU_RAS_DIRECT_ERROR_QUERY : AMDGPU_RAS_FIRMWARE_ERROR_QUERY; 5161 } else { 5162 *error_query_mode = AMDGPU_RAS_DIRECT_ERROR_QUERY; 5163 } 5164 5165 return true; 5166 } 5167 5168 /* Register each ip ras block into amdgpu ras */ 5169 int amdgpu_ras_register_ras_block(struct amdgpu_device *adev, 5170 struct amdgpu_ras_block_object *ras_block_obj) 5171 { 5172 struct amdgpu_ras_block_list *ras_node; 5173 if (!adev || !ras_block_obj) 5174 return -EINVAL; 5175 5176 ras_node = kzalloc(sizeof(*ras_node), GFP_KERNEL); 5177 if (!ras_node) 5178 return -ENOMEM; 5179 5180 INIT_LIST_HEAD(&ras_node->node); 5181 ras_node->ras_obj = ras_block_obj; 5182 list_add_tail(&ras_node->node, &adev->ras_list); 5183 5184 return 0; 5185 } 5186 5187 void amdgpu_ras_get_error_type_name(uint32_t err_type, char *err_type_name) 5188 { 5189 if (!err_type_name) 5190 return; 5191 5192 switch (err_type) { 5193 case AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE: 5194 sprintf(err_type_name, "correctable"); 5195 break; 5196 case AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE: 5197 sprintf(err_type_name, "uncorrectable"); 5198 break; 5199 default: 5200 sprintf(err_type_name, "unknown"); 5201 break; 5202 } 5203 } 5204 5205 bool amdgpu_ras_inst_get_memory_id_field(struct amdgpu_device *adev, 5206 const struct amdgpu_ras_err_status_reg_entry *reg_entry, 5207 uint32_t instance, 5208 uint32_t *memory_id) 5209 { 5210 uint32_t err_status_lo_data, err_status_lo_offset; 5211 5212 if (!reg_entry) 5213 return false; 5214 5215 err_status_lo_offset = 5216 AMDGPU_RAS_REG_ENTRY_OFFSET(reg_entry->hwip, instance, 5217 reg_entry->seg_lo, reg_entry->reg_lo); 5218 err_status_lo_data = RREG32(err_status_lo_offset); 5219 5220 if ((reg_entry->flags & AMDGPU_RAS_ERR_STATUS_VALID) && 5221 !REG_GET_FIELD(err_status_lo_data, ERR_STATUS_LO, ERR_STATUS_VALID_FLAG)) 5222 return false; 5223 5224 *memory_id = REG_GET_FIELD(err_status_lo_data, ERR_STATUS_LO, MEMORY_ID); 5225 5226 return true; 5227 } 5228 5229 bool amdgpu_ras_inst_get_err_cnt_field(struct amdgpu_device *adev, 5230 const struct amdgpu_ras_err_status_reg_entry *reg_entry, 5231 uint32_t instance, 5232 unsigned long *err_cnt) 5233 { 5234 uint32_t err_status_hi_data, err_status_hi_offset; 5235 5236 if (!reg_entry) 5237 return false; 5238 5239 err_status_hi_offset = 5240 AMDGPU_RAS_REG_ENTRY_OFFSET(reg_entry->hwip, instance, 5241 reg_entry->seg_hi, reg_entry->reg_hi); 5242 err_status_hi_data = RREG32(err_status_hi_offset); 5243 5244 if ((reg_entry->flags & AMDGPU_RAS_ERR_INFO_VALID) && 5245 !REG_GET_FIELD(err_status_hi_data, ERR_STATUS_HI, ERR_INFO_VALID_FLAG)) 5246 /* keep the check here in case we need to refer to the result later */ 5247 dev_dbg(adev->dev, "Invalid err_info field\n"); 5248 5249 /* read err count */ 5250 *err_cnt = REG_GET_FIELD(err_status_hi_data, ERR_STATUS, ERR_CNT); 5251 5252 return true; 5253 } 5254 5255 void amdgpu_ras_inst_query_ras_error_count(struct amdgpu_device *adev, 5256 const struct amdgpu_ras_err_status_reg_entry *reg_list, 5257 uint32_t reg_list_size, 5258 const struct amdgpu_ras_memory_id_entry *mem_list, 5259 uint32_t mem_list_size, 5260 uint32_t instance, 5261 uint32_t err_type, 5262 unsigned long *err_count) 5263 { 5264 uint32_t memory_id; 5265 unsigned long err_cnt; 5266 char err_type_name[16]; 5267 uint32_t i, j; 5268 5269 for (i = 0; i < reg_list_size; i++) { 5270 /* query memory_id from err_status_lo */ 5271 if (!amdgpu_ras_inst_get_memory_id_field(adev, ®_list[i], 5272 instance, &memory_id)) 5273 continue; 5274 5275 /* query err_cnt from err_status_hi */ 5276 if (!amdgpu_ras_inst_get_err_cnt_field(adev, ®_list[i], 5277 instance, &err_cnt) || 5278 !err_cnt) 5279 continue; 5280 5281 *err_count += err_cnt; 5282 5283 /* log the errors */ 5284 amdgpu_ras_get_error_type_name(err_type, err_type_name); 5285 if (!mem_list) { 5286 /* memory_list is not supported */ 5287 dev_info(adev->dev, 5288 "%ld %s hardware errors detected in %s, instance: %d, memory_id: %d\n", 5289 err_cnt, err_type_name, 5290 reg_list[i].block_name, 5291 instance, memory_id); 5292 } else { 5293 for (j = 0; j < mem_list_size; j++) { 5294 if (memory_id == mem_list[j].memory_id) { 5295 dev_info(adev->dev, 5296 "%ld %s hardware errors detected in %s, instance: %d, memory block: %s\n", 5297 err_cnt, err_type_name, 5298 reg_list[i].block_name, 5299 instance, mem_list[j].name); 5300 break; 5301 } 5302 } 5303 } 5304 } 5305 } 5306 5307 void amdgpu_ras_inst_reset_ras_error_count(struct amdgpu_device *adev, 5308 const struct amdgpu_ras_err_status_reg_entry *reg_list, 5309 uint32_t reg_list_size, 5310 uint32_t instance) 5311 { 5312 uint32_t err_status_lo_offset, err_status_hi_offset; 5313 uint32_t i; 5314 5315 for (i = 0; i < reg_list_size; i++) { 5316 err_status_lo_offset = 5317 AMDGPU_RAS_REG_ENTRY_OFFSET(reg_list[i].hwip, instance, 5318 reg_list[i].seg_lo, reg_list[i].reg_lo); 5319 err_status_hi_offset = 5320 AMDGPU_RAS_REG_ENTRY_OFFSET(reg_list[i].hwip, instance, 5321 reg_list[i].seg_hi, reg_list[i].reg_hi); 5322 WREG32(err_status_lo_offset, 0); 5323 WREG32(err_status_hi_offset, 0); 5324 } 5325 } 5326 5327 int amdgpu_ras_error_data_init(struct ras_err_data *err_data) 5328 { 5329 memset(err_data, 0, sizeof(*err_data)); 5330 5331 INIT_LIST_HEAD(&err_data->err_node_list); 5332 5333 return 0; 5334 } 5335 5336 static void amdgpu_ras_error_node_release(struct ras_err_node *err_node) 5337 { 5338 if (!err_node) 5339 return; 5340 5341 list_del(&err_node->node); 5342 kvfree(err_node); 5343 } 5344 5345 void amdgpu_ras_error_data_fini(struct ras_err_data *err_data) 5346 { 5347 struct ras_err_node *err_node, *tmp; 5348 5349 list_for_each_entry_safe(err_node, tmp, &err_data->err_node_list, node) 5350 amdgpu_ras_error_node_release(err_node); 5351 } 5352 5353 static struct ras_err_node *amdgpu_ras_error_find_node_by_id(struct ras_err_data *err_data, 5354 struct amdgpu_smuio_mcm_config_info *mcm_info) 5355 { 5356 struct ras_err_node *err_node; 5357 struct amdgpu_smuio_mcm_config_info *ref_id; 5358 5359 if (!err_data || !mcm_info) 5360 return NULL; 5361 5362 for_each_ras_error(err_node, err_data) { 5363 ref_id = &err_node->err_info.mcm_info; 5364 5365 if (mcm_info->socket_id == ref_id->socket_id && 5366 mcm_info->die_id == ref_id->die_id) 5367 return err_node; 5368 } 5369 5370 return NULL; 5371 } 5372 5373 static struct ras_err_node *amdgpu_ras_error_node_new(void) 5374 { 5375 struct ras_err_node *err_node; 5376 5377 err_node = kvzalloc(sizeof(*err_node), GFP_KERNEL); 5378 if (!err_node) 5379 return NULL; 5380 5381 INIT_LIST_HEAD(&err_node->node); 5382 5383 return err_node; 5384 } 5385 5386 static int ras_err_info_cmp(void *priv, const struct list_head *a, const struct list_head *b) 5387 { 5388 struct ras_err_node *nodea = container_of(a, struct ras_err_node, node); 5389 struct ras_err_node *nodeb = container_of(b, struct ras_err_node, node); 5390 struct amdgpu_smuio_mcm_config_info *infoa = &nodea->err_info.mcm_info; 5391 struct amdgpu_smuio_mcm_config_info *infob = &nodeb->err_info.mcm_info; 5392 5393 if (unlikely(infoa->socket_id != infob->socket_id)) 5394 return infoa->socket_id - infob->socket_id; 5395 else 5396 return infoa->die_id - infob->die_id; 5397 5398 return 0; 5399 } 5400 5401 static struct ras_err_info *amdgpu_ras_error_get_info(struct ras_err_data *err_data, 5402 struct amdgpu_smuio_mcm_config_info *mcm_info) 5403 { 5404 struct ras_err_node *err_node; 5405 5406 err_node = amdgpu_ras_error_find_node_by_id(err_data, mcm_info); 5407 if (err_node) 5408 return &err_node->err_info; 5409 5410 err_node = amdgpu_ras_error_node_new(); 5411 if (!err_node) 5412 return NULL; 5413 5414 memcpy(&err_node->err_info.mcm_info, mcm_info, sizeof(*mcm_info)); 5415 5416 err_data->err_list_count++; 5417 list_add_tail(&err_node->node, &err_data->err_node_list); 5418 list_sort(NULL, &err_data->err_node_list, ras_err_info_cmp); 5419 5420 return &err_node->err_info; 5421 } 5422 5423 int amdgpu_ras_error_statistic_ue_count(struct ras_err_data *err_data, 5424 struct amdgpu_smuio_mcm_config_info *mcm_info, 5425 u64 count) 5426 { 5427 struct ras_err_info *err_info; 5428 5429 if (!err_data || !mcm_info) 5430 return -EINVAL; 5431 5432 if (!count) 5433 return 0; 5434 5435 err_info = amdgpu_ras_error_get_info(err_data, mcm_info); 5436 if (!err_info) 5437 return -EINVAL; 5438 5439 err_info->ue_count += count; 5440 err_data->ue_count += count; 5441 5442 return 0; 5443 } 5444 5445 int amdgpu_ras_error_statistic_ce_count(struct ras_err_data *err_data, 5446 struct amdgpu_smuio_mcm_config_info *mcm_info, 5447 u64 count) 5448 { 5449 struct ras_err_info *err_info; 5450 5451 if (!err_data || !mcm_info) 5452 return -EINVAL; 5453 5454 if (!count) 5455 return 0; 5456 5457 err_info = amdgpu_ras_error_get_info(err_data, mcm_info); 5458 if (!err_info) 5459 return -EINVAL; 5460 5461 err_info->ce_count += count; 5462 err_data->ce_count += count; 5463 5464 return 0; 5465 } 5466 5467 int amdgpu_ras_error_statistic_de_count(struct ras_err_data *err_data, 5468 struct amdgpu_smuio_mcm_config_info *mcm_info, 5469 u64 count) 5470 { 5471 struct ras_err_info *err_info; 5472 5473 if (!err_data || !mcm_info) 5474 return -EINVAL; 5475 5476 if (!count) 5477 return 0; 5478 5479 err_info = amdgpu_ras_error_get_info(err_data, mcm_info); 5480 if (!err_info) 5481 return -EINVAL; 5482 5483 err_info->de_count += count; 5484 err_data->de_count += count; 5485 5486 return 0; 5487 } 5488 5489 #define mmMP0_SMN_C2PMSG_92 0x1609C 5490 #define mmMP0_SMN_C2PMSG_126 0x160BE 5491 static void amdgpu_ras_boot_time_error_reporting(struct amdgpu_device *adev, 5492 u32 instance) 5493 { 5494 u32 socket_id, aid_id, hbm_id; 5495 u32 fw_status; 5496 u32 boot_error; 5497 u64 reg_addr; 5498 5499 /* The pattern for smn addressing in other SOC could be different from 5500 * the one for aqua_vanjaram. We should revisit the code if the pattern 5501 * is changed. In such case, replace the aqua_vanjaram implementation 5502 * with more common helper */ 5503 reg_addr = (mmMP0_SMN_C2PMSG_92 << 2) + 5504 aqua_vanjaram_encode_ext_smn_addressing(instance); 5505 fw_status = amdgpu_device_indirect_rreg_ext(adev, reg_addr); 5506 5507 reg_addr = (mmMP0_SMN_C2PMSG_126 << 2) + 5508 aqua_vanjaram_encode_ext_smn_addressing(instance); 5509 boot_error = amdgpu_device_indirect_rreg_ext(adev, reg_addr); 5510 5511 socket_id = AMDGPU_RAS_GPU_ERR_SOCKET_ID(boot_error); 5512 aid_id = AMDGPU_RAS_GPU_ERR_AID_ID(boot_error); 5513 hbm_id = ((1 == AMDGPU_RAS_GPU_ERR_HBM_ID(boot_error)) ? 0 : 1); 5514 5515 if (AMDGPU_RAS_GPU_ERR_MEM_TRAINING(boot_error)) 5516 dev_info(adev->dev, 5517 "socket: %d, aid: %d, hbm: %d, fw_status: 0x%x, memory training failed\n", 5518 socket_id, aid_id, hbm_id, fw_status); 5519 5520 if (AMDGPU_RAS_GPU_ERR_FW_LOAD(boot_error)) 5521 dev_info(adev->dev, 5522 "socket: %d, aid: %d, fw_status: 0x%x, firmware load failed at boot time\n", 5523 socket_id, aid_id, fw_status); 5524 5525 if (AMDGPU_RAS_GPU_ERR_WAFL_LINK_TRAINING(boot_error)) 5526 dev_info(adev->dev, 5527 "socket: %d, aid: %d, fw_status: 0x%x, wafl link training failed\n", 5528 socket_id, aid_id, fw_status); 5529 5530 if (AMDGPU_RAS_GPU_ERR_XGMI_LINK_TRAINING(boot_error)) 5531 dev_info(adev->dev, 5532 "socket: %d, aid: %d, fw_status: 0x%x, xgmi link training failed\n", 5533 socket_id, aid_id, fw_status); 5534 5535 if (AMDGPU_RAS_GPU_ERR_USR_CP_LINK_TRAINING(boot_error)) 5536 dev_info(adev->dev, 5537 "socket: %d, aid: %d, fw_status: 0x%x, usr cp link training failed\n", 5538 socket_id, aid_id, fw_status); 5539 5540 if (AMDGPU_RAS_GPU_ERR_USR_DP_LINK_TRAINING(boot_error)) 5541 dev_info(adev->dev, 5542 "socket: %d, aid: %d, fw_status: 0x%x, usr dp link training failed\n", 5543 socket_id, aid_id, fw_status); 5544 5545 if (AMDGPU_RAS_GPU_ERR_HBM_MEM_TEST(boot_error)) 5546 dev_info(adev->dev, 5547 "socket: %d, aid: %d, hbm: %d, fw_status: 0x%x, hbm memory test failed\n", 5548 socket_id, aid_id, hbm_id, fw_status); 5549 5550 if (AMDGPU_RAS_GPU_ERR_HBM_BIST_TEST(boot_error)) 5551 dev_info(adev->dev, 5552 "socket: %d, aid: %d, hbm: %d, fw_status: 0x%x, hbm bist test failed\n", 5553 socket_id, aid_id, hbm_id, fw_status); 5554 5555 if (AMDGPU_RAS_GPU_ERR_DATA_ABORT(boot_error)) 5556 dev_info(adev->dev, 5557 "socket: %d, aid: %d, fw_status: 0x%x, data abort exception\n", 5558 socket_id, aid_id, fw_status); 5559 5560 if (AMDGPU_RAS_GPU_ERR_GENERIC(boot_error)) 5561 dev_info(adev->dev, 5562 "socket: %d, aid: %d, fw_status: 0x%x, Boot Controller Generic Error\n", 5563 socket_id, aid_id, fw_status); 5564 } 5565 5566 static bool amdgpu_ras_boot_error_detected(struct amdgpu_device *adev, 5567 u32 instance) 5568 { 5569 u64 reg_addr; 5570 u32 reg_data; 5571 int retry_loop; 5572 5573 reg_addr = (mmMP0_SMN_C2PMSG_92 << 2) + 5574 aqua_vanjaram_encode_ext_smn_addressing(instance); 5575 5576 for (retry_loop = 0; retry_loop < AMDGPU_RAS_BOOT_STATUS_POLLING_LIMIT; retry_loop++) { 5577 reg_data = amdgpu_device_indirect_rreg_ext(adev, reg_addr); 5578 if ((reg_data & AMDGPU_RAS_BOOT_STATUS_MASK) == AMDGPU_RAS_BOOT_STEADY_STATUS) 5579 return false; 5580 else 5581 msleep(1); 5582 } 5583 5584 return true; 5585 } 5586 5587 void amdgpu_ras_query_boot_status(struct amdgpu_device *adev, u32 num_instances) 5588 { 5589 u32 i; 5590 5591 for (i = 0; i < num_instances; i++) { 5592 if (amdgpu_ras_boot_error_detected(adev, i)) 5593 amdgpu_ras_boot_time_error_reporting(adev, i); 5594 } 5595 } 5596 5597 int amdgpu_ras_reserve_page(struct amdgpu_device *adev, uint64_t pfn) 5598 { 5599 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 5600 struct amdgpu_vram_mgr *mgr = &adev->mman.vram_mgr; 5601 uint64_t start = pfn << AMDGPU_GPU_PAGE_SHIFT; 5602 int ret = 0; 5603 5604 if (amdgpu_ras_check_critical_address(adev, start)) 5605 return 0; 5606 5607 mutex_lock(&con->page_rsv_lock); 5608 ret = amdgpu_vram_mgr_query_page_status(mgr, start); 5609 if (ret == -ENOENT) 5610 ret = amdgpu_vram_mgr_reserve_range(mgr, start, AMDGPU_GPU_PAGE_SIZE); 5611 mutex_unlock(&con->page_rsv_lock); 5612 5613 return ret; 5614 } 5615 5616 void amdgpu_ras_event_log_print(struct amdgpu_device *adev, u64 event_id, 5617 const char *fmt, ...) 5618 { 5619 struct va_format vaf; 5620 va_list args; 5621 5622 va_start(args, fmt); 5623 vaf.fmt = fmt; 5624 vaf.va = &args; 5625 5626 if (RAS_EVENT_ID_IS_VALID(event_id)) 5627 dev_printk(KERN_INFO, adev->dev, "{%llu}%pV", event_id, &vaf); 5628 else 5629 dev_printk(KERN_INFO, adev->dev, "%pV", &vaf); 5630 5631 va_end(args); 5632 } 5633 5634 bool amdgpu_ras_is_rma(struct amdgpu_device *adev) 5635 { 5636 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 5637 5638 if (amdgpu_uniras_enabled(adev)) 5639 return amdgpu_ras_mgr_is_rma(adev); 5640 5641 if (!con) 5642 return false; 5643 5644 return con->is_rma; 5645 } 5646 5647 int amdgpu_ras_add_critical_region(struct amdgpu_device *adev, 5648 struct amdgpu_bo *bo) 5649 { 5650 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 5651 struct amdgpu_vram_mgr_resource *vres; 5652 struct ras_critical_region *region; 5653 struct drm_buddy_block *block; 5654 int ret = 0; 5655 5656 if (!bo || !bo->tbo.resource) 5657 return -EINVAL; 5658 5659 vres = to_amdgpu_vram_mgr_resource(bo->tbo.resource); 5660 5661 mutex_lock(&con->critical_region_lock); 5662 5663 /* Check if the bo had been recorded */ 5664 list_for_each_entry(region, &con->critical_region_head, node) 5665 if (region->bo == bo) 5666 goto out; 5667 5668 /* Record new critical amdgpu bo */ 5669 list_for_each_entry(block, &vres->blocks, link) { 5670 region = kzalloc(sizeof(*region), GFP_KERNEL); 5671 if (!region) { 5672 ret = -ENOMEM; 5673 goto out; 5674 } 5675 region->bo = bo; 5676 region->start = amdgpu_vram_mgr_block_start(block); 5677 region->size = amdgpu_vram_mgr_block_size(block); 5678 list_add_tail(®ion->node, &con->critical_region_head); 5679 } 5680 5681 out: 5682 mutex_unlock(&con->critical_region_lock); 5683 5684 return ret; 5685 } 5686 5687 static void amdgpu_ras_critical_region_init(struct amdgpu_device *adev) 5688 { 5689 amdgpu_ras_add_critical_region(adev, adev->mman.fw_reserved_memory); 5690 } 5691 5692 static void amdgpu_ras_critical_region_fini(struct amdgpu_device *adev) 5693 { 5694 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 5695 struct ras_critical_region *region, *tmp; 5696 5697 mutex_lock(&con->critical_region_lock); 5698 list_for_each_entry_safe(region, tmp, &con->critical_region_head, node) { 5699 list_del(®ion->node); 5700 kfree(region); 5701 } 5702 mutex_unlock(&con->critical_region_lock); 5703 } 5704 5705 bool amdgpu_ras_check_critical_address(struct amdgpu_device *adev, uint64_t addr) 5706 { 5707 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 5708 struct ras_critical_region *region; 5709 bool ret = false; 5710 5711 mutex_lock(&con->critical_region_lock); 5712 list_for_each_entry(region, &con->critical_region_head, node) { 5713 if ((region->start <= addr) && 5714 (addr < (region->start + region->size))) { 5715 ret = true; 5716 break; 5717 } 5718 } 5719 mutex_unlock(&con->critical_region_lock); 5720 5721 return ret; 5722 } 5723 5724 void amdgpu_ras_pre_reset(struct amdgpu_device *adev, 5725 struct list_head *device_list) 5726 { 5727 struct amdgpu_device *tmp_adev = NULL; 5728 5729 list_for_each_entry(tmp_adev, device_list, reset_list) { 5730 if (amdgpu_uniras_enabled(tmp_adev)) 5731 amdgpu_ras_mgr_pre_reset(tmp_adev); 5732 } 5733 } 5734 5735 void amdgpu_ras_post_reset(struct amdgpu_device *adev, 5736 struct list_head *device_list) 5737 { 5738 struct amdgpu_device *tmp_adev = NULL; 5739 5740 list_for_each_entry(tmp_adev, device_list, reset_list) { 5741 if (amdgpu_uniras_enabled(tmp_adev)) 5742 amdgpu_ras_mgr_post_reset(tmp_adev); 5743 } 5744 } 5745