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