1 /* 2 * Copyright 2018 Advanced Micro Devices, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 * 23 */ 24 #include <linux/debugfs.h> 25 #include <linux/list.h> 26 #include <linux/module.h> 27 #include <linux/uaccess.h> 28 #include <linux/reboot.h> 29 #include <linux/syscalls.h> 30 31 #include "amdgpu.h" 32 #include "amdgpu_ras.h" 33 #include "amdgpu_atomfirmware.h" 34 #include "amdgpu_xgmi.h" 35 #include "ivsrcid/nbio/irqsrcs_nbif_7_4.h" 36 37 const char *ras_error_string[] = { 38 "none", 39 "parity", 40 "single_correctable", 41 "multi_uncorrectable", 42 "poison", 43 }; 44 45 const char *ras_block_string[] = { 46 "umc", 47 "sdma", 48 "gfx", 49 "mmhub", 50 "athub", 51 "pcie_bif", 52 "hdp", 53 "xgmi_wafl", 54 "df", 55 "smn", 56 "sem", 57 "mp0", 58 "mp1", 59 "fuse", 60 }; 61 62 #define ras_err_str(i) (ras_error_string[ffs(i)]) 63 #define ras_block_str(i) (ras_block_string[i]) 64 65 #define AMDGPU_RAS_FLAG_INIT_BY_VBIOS 1 66 #define AMDGPU_RAS_FLAG_INIT_NEED_RESET 2 67 #define RAS_DEFAULT_FLAGS (AMDGPU_RAS_FLAG_INIT_BY_VBIOS) 68 69 /* inject address is 52 bits */ 70 #define RAS_UMC_INJECT_ADDR_LIMIT (0x1ULL << 52) 71 72 enum amdgpu_ras_retire_page_reservation { 73 AMDGPU_RAS_RETIRE_PAGE_RESERVED, 74 AMDGPU_RAS_RETIRE_PAGE_PENDING, 75 AMDGPU_RAS_RETIRE_PAGE_FAULT, 76 }; 77 78 atomic_t amdgpu_ras_in_intr = ATOMIC_INIT(0); 79 80 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev, 81 uint64_t addr); 82 83 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf, 84 size_t size, loff_t *pos) 85 { 86 struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private; 87 struct ras_query_if info = { 88 .head = obj->head, 89 }; 90 ssize_t s; 91 char val[128]; 92 93 if (amdgpu_ras_error_query(obj->adev, &info)) 94 return -EINVAL; 95 96 s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n", 97 "ue", info.ue_count, 98 "ce", info.ce_count); 99 if (*pos >= s) 100 return 0; 101 102 s -= *pos; 103 s = min_t(u64, s, size); 104 105 106 if (copy_to_user(buf, &val[*pos], s)) 107 return -EINVAL; 108 109 *pos += s; 110 111 return s; 112 } 113 114 static const struct file_operations amdgpu_ras_debugfs_ops = { 115 .owner = THIS_MODULE, 116 .read = amdgpu_ras_debugfs_read, 117 .write = NULL, 118 .llseek = default_llseek 119 }; 120 121 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id) 122 { 123 int i; 124 125 for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) { 126 *block_id = i; 127 if (strcmp(name, ras_block_str(i)) == 0) 128 return 0; 129 } 130 return -EINVAL; 131 } 132 133 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f, 134 const char __user *buf, size_t size, 135 loff_t *pos, struct ras_debug_if *data) 136 { 137 ssize_t s = min_t(u64, 64, size); 138 char str[65]; 139 char block_name[33]; 140 char err[9] = "ue"; 141 int op = -1; 142 int block_id; 143 uint32_t sub_block; 144 u64 address, value; 145 146 if (*pos) 147 return -EINVAL; 148 *pos = size; 149 150 memset(str, 0, sizeof(str)); 151 memset(data, 0, sizeof(*data)); 152 153 if (copy_from_user(str, buf, s)) 154 return -EINVAL; 155 156 if (sscanf(str, "disable %32s", block_name) == 1) 157 op = 0; 158 else if (sscanf(str, "enable %32s %8s", block_name, err) == 2) 159 op = 1; 160 else if (sscanf(str, "inject %32s %8s", block_name, err) == 2) 161 op = 2; 162 else if (str[0] && str[1] && str[2] && str[3]) 163 /* ascii string, but commands are not matched. */ 164 return -EINVAL; 165 166 if (op != -1) { 167 if (amdgpu_ras_find_block_id_by_name(block_name, &block_id)) 168 return -EINVAL; 169 170 data->head.block = block_id; 171 /* only ue and ce errors are supported */ 172 if (!memcmp("ue", err, 2)) 173 data->head.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE; 174 else if (!memcmp("ce", err, 2)) 175 data->head.type = AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE; 176 else 177 return -EINVAL; 178 179 data->op = op; 180 181 if (op == 2) { 182 if (sscanf(str, "%*s %*s %*s %u %llu %llu", 183 &sub_block, &address, &value) != 3) 184 if (sscanf(str, "%*s %*s %*s 0x%x 0x%llx 0x%llx", 185 &sub_block, &address, &value) != 3) 186 return -EINVAL; 187 data->head.sub_block_index = sub_block; 188 data->inject.address = address; 189 data->inject.value = value; 190 } 191 } else { 192 if (size < sizeof(*data)) 193 return -EINVAL; 194 195 if (copy_from_user(data, buf, sizeof(*data))) 196 return -EINVAL; 197 } 198 199 return 0; 200 } 201 202 /** 203 * DOC: AMDGPU RAS debugfs control interface 204 * 205 * It accepts struct ras_debug_if who has two members. 206 * 207 * First member: ras_debug_if::head or ras_debug_if::inject. 208 * 209 * head is used to indicate which IP block will be under control. 210 * 211 * head has four members, they are block, type, sub_block_index, name. 212 * block: which IP will be under control. 213 * type: what kind of error will be enabled/disabled/injected. 214 * sub_block_index: some IPs have subcomponets. say, GFX, sDMA. 215 * name: the name of IP. 216 * 217 * inject has two more members than head, they are address, value. 218 * As their names indicate, inject operation will write the 219 * value to the address. 220 * 221 * The second member: struct ras_debug_if::op. 222 * It has three kinds of operations. 223 * 224 * - 0: disable RAS on the block. Take ::head as its data. 225 * - 1: enable RAS on the block. Take ::head as its data. 226 * - 2: inject errors on the block. Take ::inject as its data. 227 * 228 * How to use the interface? 229 * 230 * Programs 231 * 232 * Copy the struct ras_debug_if in your codes and initialize it. 233 * Write the struct to the control node. 234 * 235 * Shells 236 * 237 * .. code-block:: bash 238 * 239 * echo op block [error [sub_block address value]] > .../ras/ras_ctrl 240 * 241 * Parameters: 242 * 243 * op: disable, enable, inject 244 * disable: only block is needed 245 * enable: block and error are needed 246 * inject: error, address, value are needed 247 * block: umc, sdma, gfx, ......... 248 * see ras_block_string[] for details 249 * error: ue, ce 250 * ue: multi_uncorrectable 251 * ce: single_correctable 252 * sub_block: 253 * sub block index, pass 0 if there is no sub block 254 * 255 * here are some examples for bash commands: 256 * 257 * .. code-block:: bash 258 * 259 * echo inject umc ue 0x0 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl 260 * echo inject umc ce 0 0 0 > /sys/kernel/debug/dri/0/ras/ras_ctrl 261 * echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl 262 * 263 * How to check the result? 264 * 265 * For disable/enable, please check ras features at 266 * /sys/class/drm/card[0/1/2...]/device/ras/features 267 * 268 * For inject, please check corresponding err count at 269 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count 270 * 271 * .. note:: 272 * Operations are only allowed on blocks which are supported. 273 * Please check ras mask at /sys/module/amdgpu/parameters/ras_mask 274 * to see which blocks support RAS on a particular asic. 275 * 276 */ 277 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f, const char __user *buf, 278 size_t size, loff_t *pos) 279 { 280 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private; 281 struct ras_debug_if data; 282 int ret = 0; 283 284 ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data); 285 if (ret) 286 return -EINVAL; 287 288 if (!amdgpu_ras_is_supported(adev, data.head.block)) 289 return -EINVAL; 290 291 switch (data.op) { 292 case 0: 293 ret = amdgpu_ras_feature_enable(adev, &data.head, 0); 294 break; 295 case 1: 296 ret = amdgpu_ras_feature_enable(adev, &data.head, 1); 297 break; 298 case 2: 299 if ((data.inject.address >= adev->gmc.mc_vram_size) || 300 (data.inject.address >= RAS_UMC_INJECT_ADDR_LIMIT)) { 301 ret = -EINVAL; 302 break; 303 } 304 305 /* umc ce/ue error injection for a bad page is not allowed */ 306 if ((data.head.block == AMDGPU_RAS_BLOCK__UMC) && 307 amdgpu_ras_check_bad_page(adev, data.inject.address)) { 308 DRM_WARN("RAS WARN: 0x%llx has been marked as bad before error injection!\n", 309 data.inject.address); 310 break; 311 } 312 313 /* data.inject.address is offset instead of absolute gpu address */ 314 ret = amdgpu_ras_error_inject(adev, &data.inject); 315 break; 316 default: 317 ret = -EINVAL; 318 break; 319 } 320 321 if (ret) 322 return -EINVAL; 323 324 return size; 325 } 326 327 /** 328 * DOC: AMDGPU RAS debugfs EEPROM table reset interface 329 * 330 * Some boards contain an EEPROM which is used to persistently store a list of 331 * bad pages which experiences ECC errors in vram. This interface provides 332 * a way to reset the EEPROM, e.g., after testing error injection. 333 * 334 * Usage: 335 * 336 * .. code-block:: bash 337 * 338 * echo 1 > ../ras/ras_eeprom_reset 339 * 340 * will reset EEPROM table to 0 entries. 341 * 342 */ 343 static ssize_t amdgpu_ras_debugfs_eeprom_write(struct file *f, const char __user *buf, 344 size_t size, loff_t *pos) 345 { 346 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private; 347 int ret; 348 349 ret = amdgpu_ras_eeprom_reset_table(&adev->psp.ras.ras->eeprom_control); 350 351 return ret == 1 ? size : -EIO; 352 } 353 354 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = { 355 .owner = THIS_MODULE, 356 .read = NULL, 357 .write = amdgpu_ras_debugfs_ctrl_write, 358 .llseek = default_llseek 359 }; 360 361 static const struct file_operations amdgpu_ras_debugfs_eeprom_ops = { 362 .owner = THIS_MODULE, 363 .read = NULL, 364 .write = amdgpu_ras_debugfs_eeprom_write, 365 .llseek = default_llseek 366 }; 367 368 /** 369 * DOC: AMDGPU RAS sysfs Error Count Interface 370 * 371 * It allows the user to read the error count for each IP block on the gpu through 372 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count 373 * 374 * It outputs the multiple lines which report the uncorrected (ue) and corrected 375 * (ce) error counts. 376 * 377 * The format of one line is below, 378 * 379 * [ce|ue]: count 380 * 381 * Example: 382 * 383 * .. code-block:: bash 384 * 385 * ue: 0 386 * ce: 1 387 * 388 */ 389 static ssize_t amdgpu_ras_sysfs_read(struct device *dev, 390 struct device_attribute *attr, char *buf) 391 { 392 struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr); 393 struct ras_query_if info = { 394 .head = obj->head, 395 }; 396 397 if (amdgpu_ras_error_query(obj->adev, &info)) 398 return -EINVAL; 399 400 return snprintf(buf, PAGE_SIZE, "%s: %lu\n%s: %lu\n", 401 "ue", info.ue_count, 402 "ce", info.ce_count); 403 } 404 405 /* obj begin */ 406 407 #define get_obj(obj) do { (obj)->use++; } while (0) 408 #define alive_obj(obj) ((obj)->use) 409 410 static inline void put_obj(struct ras_manager *obj) 411 { 412 if (obj && --obj->use == 0) 413 list_del(&obj->node); 414 if (obj && obj->use < 0) { 415 DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", obj->head.name); 416 } 417 } 418 419 /* make one obj and return it. */ 420 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev, 421 struct ras_common_if *head) 422 { 423 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 424 struct ras_manager *obj; 425 426 if (!con) 427 return NULL; 428 429 if (head->block >= AMDGPU_RAS_BLOCK_COUNT) 430 return NULL; 431 432 obj = &con->objs[head->block]; 433 /* already exist. return obj? */ 434 if (alive_obj(obj)) 435 return NULL; 436 437 obj->head = *head; 438 obj->adev = adev; 439 list_add(&obj->node, &con->head); 440 get_obj(obj); 441 442 return obj; 443 } 444 445 /* return an obj equal to head, or the first when head is NULL */ 446 struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev, 447 struct ras_common_if *head) 448 { 449 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 450 struct ras_manager *obj; 451 int i; 452 453 if (!con) 454 return NULL; 455 456 if (head) { 457 if (head->block >= AMDGPU_RAS_BLOCK_COUNT) 458 return NULL; 459 460 obj = &con->objs[head->block]; 461 462 if (alive_obj(obj)) { 463 WARN_ON(head->block != obj->head.block); 464 return obj; 465 } 466 } else { 467 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) { 468 obj = &con->objs[i]; 469 if (alive_obj(obj)) { 470 WARN_ON(i != obj->head.block); 471 return obj; 472 } 473 } 474 } 475 476 return NULL; 477 } 478 /* obj end */ 479 480 /* feature ctl begin */ 481 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev, 482 struct ras_common_if *head) 483 { 484 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 485 486 return con->hw_supported & BIT(head->block); 487 } 488 489 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev, 490 struct ras_common_if *head) 491 { 492 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 493 494 return con->features & BIT(head->block); 495 } 496 497 /* 498 * if obj is not created, then create one. 499 * set feature enable flag. 500 */ 501 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev, 502 struct ras_common_if *head, int enable) 503 { 504 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 505 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head); 506 507 /* If hardware does not support ras, then do not create obj. 508 * But if hardware support ras, we can create the obj. 509 * Ras framework checks con->hw_supported to see if it need do 510 * corresponding initialization. 511 * IP checks con->support to see if it need disable ras. 512 */ 513 if (!amdgpu_ras_is_feature_allowed(adev, head)) 514 return 0; 515 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head))) 516 return 0; 517 518 if (enable) { 519 if (!obj) { 520 obj = amdgpu_ras_create_obj(adev, head); 521 if (!obj) 522 return -EINVAL; 523 } else { 524 /* In case we create obj somewhere else */ 525 get_obj(obj); 526 } 527 con->features |= BIT(head->block); 528 } else { 529 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) { 530 con->features &= ~BIT(head->block); 531 put_obj(obj); 532 } 533 } 534 535 return 0; 536 } 537 538 /* wrapper of psp_ras_enable_features */ 539 int amdgpu_ras_feature_enable(struct amdgpu_device *adev, 540 struct ras_common_if *head, bool enable) 541 { 542 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 543 union ta_ras_cmd_input info; 544 int ret; 545 546 if (!con) 547 return -EINVAL; 548 549 if (!enable) { 550 info.disable_features = (struct ta_ras_disable_features_input) { 551 .block_id = amdgpu_ras_block_to_ta(head->block), 552 .error_type = amdgpu_ras_error_to_ta(head->type), 553 }; 554 } else { 555 info.enable_features = (struct ta_ras_enable_features_input) { 556 .block_id = amdgpu_ras_block_to_ta(head->block), 557 .error_type = amdgpu_ras_error_to_ta(head->type), 558 }; 559 } 560 561 /* Do not enable if it is not allowed. */ 562 WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head)); 563 /* Are we alerady in that state we are going to set? */ 564 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head))) 565 return 0; 566 567 if (!amdgpu_ras_intr_triggered()) { 568 ret = psp_ras_enable_features(&adev->psp, &info, enable); 569 if (ret) { 570 DRM_ERROR("RAS ERROR: %s %s feature failed ret %d\n", 571 enable ? "enable":"disable", 572 ras_block_str(head->block), 573 ret); 574 if (ret == TA_RAS_STATUS__RESET_NEEDED) 575 return -EAGAIN; 576 return -EINVAL; 577 } 578 } 579 580 /* setup the obj */ 581 __amdgpu_ras_feature_enable(adev, head, enable); 582 583 return 0; 584 } 585 586 /* Only used in device probe stage and called only once. */ 587 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev, 588 struct ras_common_if *head, bool enable) 589 { 590 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 591 int ret; 592 593 if (!con) 594 return -EINVAL; 595 596 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) { 597 if (enable) { 598 /* There is no harm to issue a ras TA cmd regardless of 599 * the currecnt ras state. 600 * If current state == target state, it will do nothing 601 * But sometimes it requests driver to reset and repost 602 * with error code -EAGAIN. 603 */ 604 ret = amdgpu_ras_feature_enable(adev, head, 1); 605 /* With old ras TA, we might fail to enable ras. 606 * Log it and just setup the object. 607 * TODO need remove this WA in the future. 608 */ 609 if (ret == -EINVAL) { 610 ret = __amdgpu_ras_feature_enable(adev, head, 1); 611 if (!ret) 612 DRM_INFO("RAS INFO: %s setup object\n", 613 ras_block_str(head->block)); 614 } 615 } else { 616 /* setup the object then issue a ras TA disable cmd.*/ 617 ret = __amdgpu_ras_feature_enable(adev, head, 1); 618 if (ret) 619 return ret; 620 621 ret = amdgpu_ras_feature_enable(adev, head, 0); 622 } 623 } else 624 ret = amdgpu_ras_feature_enable(adev, head, enable); 625 626 return ret; 627 } 628 629 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev, 630 bool bypass) 631 { 632 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 633 struct ras_manager *obj, *tmp; 634 635 list_for_each_entry_safe(obj, tmp, &con->head, node) { 636 /* bypass psp. 637 * aka just release the obj and corresponding flags 638 */ 639 if (bypass) { 640 if (__amdgpu_ras_feature_enable(adev, &obj->head, 0)) 641 break; 642 } else { 643 if (amdgpu_ras_feature_enable(adev, &obj->head, 0)) 644 break; 645 } 646 } 647 648 return con->features; 649 } 650 651 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev, 652 bool bypass) 653 { 654 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 655 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT; 656 int i; 657 const enum amdgpu_ras_error_type default_ras_type = 658 AMDGPU_RAS_ERROR__NONE; 659 660 for (i = 0; i < ras_block_count; i++) { 661 struct ras_common_if head = { 662 .block = i, 663 .type = default_ras_type, 664 .sub_block_index = 0, 665 }; 666 strcpy(head.name, ras_block_str(i)); 667 if (bypass) { 668 /* 669 * bypass psp. vbios enable ras for us. 670 * so just create the obj 671 */ 672 if (__amdgpu_ras_feature_enable(adev, &head, 1)) 673 break; 674 } else { 675 if (amdgpu_ras_feature_enable(adev, &head, 1)) 676 break; 677 } 678 } 679 680 return con->features; 681 } 682 /* feature ctl end */ 683 684 /* query/inject/cure begin */ 685 int amdgpu_ras_error_query(struct amdgpu_device *adev, 686 struct ras_query_if *info) 687 { 688 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head); 689 struct ras_err_data err_data = {0, 0, 0, NULL}; 690 int i; 691 692 if (!obj) 693 return -EINVAL; 694 695 switch (info->head.block) { 696 case AMDGPU_RAS_BLOCK__UMC: 697 if (adev->umc.funcs->query_ras_error_count) 698 adev->umc.funcs->query_ras_error_count(adev, &err_data); 699 /* umc query_ras_error_address is also responsible for clearing 700 * error status 701 */ 702 if (adev->umc.funcs->query_ras_error_address) 703 adev->umc.funcs->query_ras_error_address(adev, &err_data); 704 break; 705 case AMDGPU_RAS_BLOCK__SDMA: 706 if (adev->sdma.funcs->query_ras_error_count) { 707 for (i = 0; i < adev->sdma.num_instances; i++) 708 adev->sdma.funcs->query_ras_error_count(adev, i, 709 &err_data); 710 } 711 break; 712 case AMDGPU_RAS_BLOCK__GFX: 713 if (adev->gfx.funcs->query_ras_error_count) 714 adev->gfx.funcs->query_ras_error_count(adev, &err_data); 715 break; 716 case AMDGPU_RAS_BLOCK__MMHUB: 717 if (adev->mmhub.funcs->query_ras_error_count) 718 adev->mmhub.funcs->query_ras_error_count(adev, &err_data); 719 break; 720 case AMDGPU_RAS_BLOCK__PCIE_BIF: 721 if (adev->nbio.funcs->query_ras_error_count) 722 adev->nbio.funcs->query_ras_error_count(adev, &err_data); 723 break; 724 case AMDGPU_RAS_BLOCK__XGMI_WAFL: 725 amdgpu_xgmi_query_ras_error_count(adev, &err_data); 726 break; 727 default: 728 break; 729 } 730 731 obj->err_data.ue_count += err_data.ue_count; 732 obj->err_data.ce_count += err_data.ce_count; 733 734 info->ue_count = obj->err_data.ue_count; 735 info->ce_count = obj->err_data.ce_count; 736 737 if (err_data.ce_count) { 738 dev_info(adev->dev, "%ld correctable errors detected in %s block\n", 739 obj->err_data.ce_count, ras_block_str(info->head.block)); 740 } 741 if (err_data.ue_count) { 742 dev_info(adev->dev, "%ld uncorrectable errors detected in %s block\n", 743 obj->err_data.ue_count, ras_block_str(info->head.block)); 744 } 745 746 return 0; 747 } 748 749 /* wrapper of psp_ras_trigger_error */ 750 int amdgpu_ras_error_inject(struct amdgpu_device *adev, 751 struct ras_inject_if *info) 752 { 753 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head); 754 struct ta_ras_trigger_error_input block_info = { 755 .block_id = amdgpu_ras_block_to_ta(info->head.block), 756 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type), 757 .sub_block_index = info->head.sub_block_index, 758 .address = info->address, 759 .value = info->value, 760 }; 761 int ret = 0; 762 763 if (!obj) 764 return -EINVAL; 765 766 /* Calculate XGMI relative offset */ 767 if (adev->gmc.xgmi.num_physical_nodes > 1) { 768 block_info.address = 769 amdgpu_xgmi_get_relative_phy_addr(adev, 770 block_info.address); 771 } 772 773 switch (info->head.block) { 774 case AMDGPU_RAS_BLOCK__GFX: 775 if (adev->gfx.funcs->ras_error_inject) 776 ret = adev->gfx.funcs->ras_error_inject(adev, info); 777 else 778 ret = -EINVAL; 779 break; 780 case AMDGPU_RAS_BLOCK__UMC: 781 case AMDGPU_RAS_BLOCK__MMHUB: 782 case AMDGPU_RAS_BLOCK__XGMI_WAFL: 783 case AMDGPU_RAS_BLOCK__PCIE_BIF: 784 ret = psp_ras_trigger_error(&adev->psp, &block_info); 785 break; 786 default: 787 DRM_INFO("%s error injection is not supported yet\n", 788 ras_block_str(info->head.block)); 789 ret = -EINVAL; 790 } 791 792 if (ret) 793 DRM_ERROR("RAS ERROR: inject %s error failed ret %d\n", 794 ras_block_str(info->head.block), 795 ret); 796 797 return ret; 798 } 799 800 int amdgpu_ras_error_cure(struct amdgpu_device *adev, 801 struct ras_cure_if *info) 802 { 803 /* psp fw has no cure interface for now. */ 804 return 0; 805 } 806 807 /* get the total error counts on all IPs */ 808 unsigned long amdgpu_ras_query_error_count(struct amdgpu_device *adev, 809 bool is_ce) 810 { 811 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 812 struct ras_manager *obj; 813 struct ras_err_data data = {0, 0}; 814 815 if (!con) 816 return 0; 817 818 list_for_each_entry(obj, &con->head, node) { 819 struct ras_query_if info = { 820 .head = obj->head, 821 }; 822 823 if (amdgpu_ras_error_query(adev, &info)) 824 return 0; 825 826 data.ce_count += info.ce_count; 827 data.ue_count += info.ue_count; 828 } 829 830 return is_ce ? data.ce_count : data.ue_count; 831 } 832 /* query/inject/cure end */ 833 834 835 /* sysfs begin */ 836 837 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev, 838 struct ras_badpage **bps, unsigned int *count); 839 840 static char *amdgpu_ras_badpage_flags_str(unsigned int flags) 841 { 842 switch (flags) { 843 case AMDGPU_RAS_RETIRE_PAGE_RESERVED: 844 return "R"; 845 case AMDGPU_RAS_RETIRE_PAGE_PENDING: 846 return "P"; 847 case AMDGPU_RAS_RETIRE_PAGE_FAULT: 848 default: 849 return "F"; 850 }; 851 } 852 853 /** 854 * DOC: AMDGPU RAS sysfs gpu_vram_bad_pages Interface 855 * 856 * It allows user to read the bad pages of vram on the gpu through 857 * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages 858 * 859 * It outputs multiple lines, and each line stands for one gpu page. 860 * 861 * The format of one line is below, 862 * gpu pfn : gpu page size : flags 863 * 864 * gpu pfn and gpu page size are printed in hex format. 865 * flags can be one of below character, 866 * 867 * R: reserved, this gpu page is reserved and not able to use. 868 * 869 * P: pending for reserve, this gpu page is marked as bad, will be reserved 870 * in next window of page_reserve. 871 * 872 * F: unable to reserve. this gpu page can't be reserved due to some reasons. 873 * 874 * Examples: 875 * 876 * .. code-block:: bash 877 * 878 * 0x00000001 : 0x00001000 : R 879 * 0x00000002 : 0x00001000 : P 880 * 881 */ 882 883 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f, 884 struct kobject *kobj, struct bin_attribute *attr, 885 char *buf, loff_t ppos, size_t count) 886 { 887 struct amdgpu_ras *con = 888 container_of(attr, struct amdgpu_ras, badpages_attr); 889 struct amdgpu_device *adev = con->adev; 890 const unsigned int element_size = 891 sizeof("0xabcdabcd : 0x12345678 : R\n") - 1; 892 unsigned int start = div64_ul(ppos + element_size - 1, element_size); 893 unsigned int end = div64_ul(ppos + count - 1, element_size); 894 ssize_t s = 0; 895 struct ras_badpage *bps = NULL; 896 unsigned int bps_count = 0; 897 898 memset(buf, 0, count); 899 900 if (amdgpu_ras_badpages_read(adev, &bps, &bps_count)) 901 return 0; 902 903 for (; start < end && start < bps_count; start++) 904 s += scnprintf(&buf[s], element_size + 1, 905 "0x%08x : 0x%08x : %1s\n", 906 bps[start].bp, 907 bps[start].size, 908 amdgpu_ras_badpage_flags_str(bps[start].flags)); 909 910 kfree(bps); 911 912 return s; 913 } 914 915 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev, 916 struct device_attribute *attr, char *buf) 917 { 918 struct amdgpu_ras *con = 919 container_of(attr, struct amdgpu_ras, features_attr); 920 921 return scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features); 922 } 923 924 static int amdgpu_ras_sysfs_create_feature_node(struct amdgpu_device *adev) 925 { 926 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 927 struct attribute *attrs[] = { 928 &con->features_attr.attr, 929 NULL 930 }; 931 struct bin_attribute *bin_attrs[] = { 932 &con->badpages_attr, 933 NULL 934 }; 935 struct attribute_group group = { 936 .name = "ras", 937 .attrs = attrs, 938 .bin_attrs = bin_attrs, 939 }; 940 941 con->features_attr = (struct device_attribute) { 942 .attr = { 943 .name = "features", 944 .mode = S_IRUGO, 945 }, 946 .show = amdgpu_ras_sysfs_features_read, 947 }; 948 949 con->badpages_attr = (struct bin_attribute) { 950 .attr = { 951 .name = "gpu_vram_bad_pages", 952 .mode = S_IRUGO, 953 }, 954 .size = 0, 955 .private = NULL, 956 .read = amdgpu_ras_sysfs_badpages_read, 957 }; 958 959 sysfs_attr_init(attrs[0]); 960 sysfs_bin_attr_init(bin_attrs[0]); 961 962 return sysfs_create_group(&adev->dev->kobj, &group); 963 } 964 965 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev) 966 { 967 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 968 struct attribute *attrs[] = { 969 &con->features_attr.attr, 970 NULL 971 }; 972 struct bin_attribute *bin_attrs[] = { 973 &con->badpages_attr, 974 NULL 975 }; 976 struct attribute_group group = { 977 .name = "ras", 978 .attrs = attrs, 979 .bin_attrs = bin_attrs, 980 }; 981 982 sysfs_remove_group(&adev->dev->kobj, &group); 983 984 return 0; 985 } 986 987 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev, 988 struct ras_fs_if *head) 989 { 990 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head); 991 992 if (!obj || obj->attr_inuse) 993 return -EINVAL; 994 995 get_obj(obj); 996 997 memcpy(obj->fs_data.sysfs_name, 998 head->sysfs_name, 999 sizeof(obj->fs_data.sysfs_name)); 1000 1001 obj->sysfs_attr = (struct device_attribute){ 1002 .attr = { 1003 .name = obj->fs_data.sysfs_name, 1004 .mode = S_IRUGO, 1005 }, 1006 .show = amdgpu_ras_sysfs_read, 1007 }; 1008 sysfs_attr_init(&obj->sysfs_attr.attr); 1009 1010 if (sysfs_add_file_to_group(&adev->dev->kobj, 1011 &obj->sysfs_attr.attr, 1012 "ras")) { 1013 put_obj(obj); 1014 return -EINVAL; 1015 } 1016 1017 obj->attr_inuse = 1; 1018 1019 return 0; 1020 } 1021 1022 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev, 1023 struct ras_common_if *head) 1024 { 1025 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head); 1026 1027 if (!obj || !obj->attr_inuse) 1028 return -EINVAL; 1029 1030 sysfs_remove_file_from_group(&adev->dev->kobj, 1031 &obj->sysfs_attr.attr, 1032 "ras"); 1033 obj->attr_inuse = 0; 1034 put_obj(obj); 1035 1036 return 0; 1037 } 1038 1039 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev) 1040 { 1041 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1042 struct ras_manager *obj, *tmp; 1043 1044 list_for_each_entry_safe(obj, tmp, &con->head, node) { 1045 amdgpu_ras_sysfs_remove(adev, &obj->head); 1046 } 1047 1048 amdgpu_ras_sysfs_remove_feature_node(adev); 1049 1050 return 0; 1051 } 1052 /* sysfs end */ 1053 1054 /** 1055 * DOC: AMDGPU RAS Reboot Behavior for Unrecoverable Errors 1056 * 1057 * Normally when there is an uncorrectable error, the driver will reset 1058 * the GPU to recover. However, in the event of an unrecoverable error, 1059 * the driver provides an interface to reboot the system automatically 1060 * in that event. 1061 * 1062 * The following file in debugfs provides that interface: 1063 * /sys/kernel/debug/dri/[0/1/2...]/ras/auto_reboot 1064 * 1065 * Usage: 1066 * 1067 * .. code-block:: bash 1068 * 1069 * echo true > .../ras/auto_reboot 1070 * 1071 */ 1072 /* debugfs begin */ 1073 static void amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev) 1074 { 1075 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1076 struct drm_minor *minor = adev->ddev->primary; 1077 1078 con->dir = debugfs_create_dir("ras", minor->debugfs_root); 1079 debugfs_create_file("ras_ctrl", S_IWUGO | S_IRUGO, con->dir, 1080 adev, &amdgpu_ras_debugfs_ctrl_ops); 1081 debugfs_create_file("ras_eeprom_reset", S_IWUGO | S_IRUGO, con->dir, 1082 adev, &amdgpu_ras_debugfs_eeprom_ops); 1083 1084 /* 1085 * After one uncorrectable error happens, usually GPU recovery will 1086 * be scheduled. But due to the known problem in GPU recovery failing 1087 * to bring GPU back, below interface provides one direct way to 1088 * user to reboot system automatically in such case within 1089 * ERREVENT_ATHUB_INTERRUPT generated. Normal GPU recovery routine 1090 * will never be called. 1091 */ 1092 debugfs_create_bool("auto_reboot", S_IWUGO | S_IRUGO, con->dir, 1093 &con->reboot); 1094 } 1095 1096 void amdgpu_ras_debugfs_create(struct amdgpu_device *adev, 1097 struct ras_fs_if *head) 1098 { 1099 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1100 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head); 1101 1102 if (!obj || obj->ent) 1103 return; 1104 1105 get_obj(obj); 1106 1107 memcpy(obj->fs_data.debugfs_name, 1108 head->debugfs_name, 1109 sizeof(obj->fs_data.debugfs_name)); 1110 1111 obj->ent = debugfs_create_file(obj->fs_data.debugfs_name, 1112 S_IWUGO | S_IRUGO, con->dir, obj, 1113 &amdgpu_ras_debugfs_ops); 1114 } 1115 1116 void amdgpu_ras_debugfs_create_all(struct amdgpu_device *adev) 1117 { 1118 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1119 struct ras_manager *obj; 1120 struct ras_fs_if fs_info; 1121 1122 /* 1123 * it won't be called in resume path, no need to check 1124 * suspend and gpu reset status 1125 */ 1126 if (!con) 1127 return; 1128 1129 amdgpu_ras_debugfs_create_ctrl_node(adev); 1130 1131 list_for_each_entry(obj, &con->head, node) { 1132 if (amdgpu_ras_is_supported(adev, obj->head.block) && 1133 (obj->attr_inuse == 1)) { 1134 sprintf(fs_info.debugfs_name, "%s_err_inject", 1135 ras_block_str(obj->head.block)); 1136 fs_info.head = obj->head; 1137 amdgpu_ras_debugfs_create(adev, &fs_info); 1138 } 1139 } 1140 } 1141 1142 void amdgpu_ras_debugfs_remove(struct amdgpu_device *adev, 1143 struct ras_common_if *head) 1144 { 1145 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head); 1146 1147 if (!obj || !obj->ent) 1148 return; 1149 1150 debugfs_remove(obj->ent); 1151 obj->ent = NULL; 1152 put_obj(obj); 1153 } 1154 1155 static void amdgpu_ras_debugfs_remove_all(struct amdgpu_device *adev) 1156 { 1157 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1158 struct ras_manager *obj, *tmp; 1159 1160 list_for_each_entry_safe(obj, tmp, &con->head, node) { 1161 amdgpu_ras_debugfs_remove(adev, &obj->head); 1162 } 1163 1164 debugfs_remove_recursive(con->dir); 1165 con->dir = NULL; 1166 } 1167 /* debugfs end */ 1168 1169 /* ras fs */ 1170 1171 static int amdgpu_ras_fs_init(struct amdgpu_device *adev) 1172 { 1173 amdgpu_ras_sysfs_create_feature_node(adev); 1174 1175 return 0; 1176 } 1177 1178 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev) 1179 { 1180 amdgpu_ras_debugfs_remove_all(adev); 1181 amdgpu_ras_sysfs_remove_all(adev); 1182 return 0; 1183 } 1184 /* ras fs end */ 1185 1186 /* ih begin */ 1187 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj) 1188 { 1189 struct ras_ih_data *data = &obj->ih_data; 1190 struct amdgpu_iv_entry entry; 1191 int ret; 1192 struct ras_err_data err_data = {0, 0, 0, NULL}; 1193 1194 while (data->rptr != data->wptr) { 1195 rmb(); 1196 memcpy(&entry, &data->ring[data->rptr], 1197 data->element_size); 1198 1199 wmb(); 1200 data->rptr = (data->aligned_element_size + 1201 data->rptr) % data->ring_size; 1202 1203 /* Let IP handle its data, maybe we need get the output 1204 * from the callback to udpate the error type/count, etc 1205 */ 1206 if (data->cb) { 1207 ret = data->cb(obj->adev, &err_data, &entry); 1208 /* ue will trigger an interrupt, and in that case 1209 * we need do a reset to recovery the whole system. 1210 * But leave IP do that recovery, here we just dispatch 1211 * the error. 1212 */ 1213 if (ret == AMDGPU_RAS_SUCCESS) { 1214 /* these counts could be left as 0 if 1215 * some blocks do not count error number 1216 */ 1217 obj->err_data.ue_count += err_data.ue_count; 1218 obj->err_data.ce_count += err_data.ce_count; 1219 } 1220 } 1221 } 1222 } 1223 1224 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work) 1225 { 1226 struct ras_ih_data *data = 1227 container_of(work, struct ras_ih_data, ih_work); 1228 struct ras_manager *obj = 1229 container_of(data, struct ras_manager, ih_data); 1230 1231 amdgpu_ras_interrupt_handler(obj); 1232 } 1233 1234 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev, 1235 struct ras_dispatch_if *info) 1236 { 1237 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head); 1238 struct ras_ih_data *data = &obj->ih_data; 1239 1240 if (!obj) 1241 return -EINVAL; 1242 1243 if (data->inuse == 0) 1244 return 0; 1245 1246 /* Might be overflow... */ 1247 memcpy(&data->ring[data->wptr], info->entry, 1248 data->element_size); 1249 1250 wmb(); 1251 data->wptr = (data->aligned_element_size + 1252 data->wptr) % data->ring_size; 1253 1254 schedule_work(&data->ih_work); 1255 1256 return 0; 1257 } 1258 1259 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev, 1260 struct ras_ih_if *info) 1261 { 1262 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head); 1263 struct ras_ih_data *data; 1264 1265 if (!obj) 1266 return -EINVAL; 1267 1268 data = &obj->ih_data; 1269 if (data->inuse == 0) 1270 return 0; 1271 1272 cancel_work_sync(&data->ih_work); 1273 1274 kfree(data->ring); 1275 memset(data, 0, sizeof(*data)); 1276 put_obj(obj); 1277 1278 return 0; 1279 } 1280 1281 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev, 1282 struct ras_ih_if *info) 1283 { 1284 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head); 1285 struct ras_ih_data *data; 1286 1287 if (!obj) { 1288 /* in case we registe the IH before enable ras feature */ 1289 obj = amdgpu_ras_create_obj(adev, &info->head); 1290 if (!obj) 1291 return -EINVAL; 1292 } else 1293 get_obj(obj); 1294 1295 data = &obj->ih_data; 1296 /* add the callback.etc */ 1297 *data = (struct ras_ih_data) { 1298 .inuse = 0, 1299 .cb = info->cb, 1300 .element_size = sizeof(struct amdgpu_iv_entry), 1301 .rptr = 0, 1302 .wptr = 0, 1303 }; 1304 1305 INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler); 1306 1307 data->aligned_element_size = ALIGN(data->element_size, 8); 1308 /* the ring can store 64 iv entries. */ 1309 data->ring_size = 64 * data->aligned_element_size; 1310 data->ring = kmalloc(data->ring_size, GFP_KERNEL); 1311 if (!data->ring) { 1312 put_obj(obj); 1313 return -ENOMEM; 1314 } 1315 1316 /* IH is ready */ 1317 data->inuse = 1; 1318 1319 return 0; 1320 } 1321 1322 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev) 1323 { 1324 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1325 struct ras_manager *obj, *tmp; 1326 1327 list_for_each_entry_safe(obj, tmp, &con->head, node) { 1328 struct ras_ih_if info = { 1329 .head = obj->head, 1330 }; 1331 amdgpu_ras_interrupt_remove_handler(adev, &info); 1332 } 1333 1334 return 0; 1335 } 1336 /* ih end */ 1337 1338 /* traversal all IPs except NBIO to query error counter */ 1339 static void amdgpu_ras_log_on_err_counter(struct amdgpu_device *adev) 1340 { 1341 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1342 struct ras_manager *obj; 1343 1344 if (!con) 1345 return; 1346 1347 list_for_each_entry(obj, &con->head, node) { 1348 struct ras_query_if info = { 1349 .head = obj->head, 1350 }; 1351 1352 /* 1353 * PCIE_BIF IP has one different isr by ras controller 1354 * interrupt, the specific ras counter query will be 1355 * done in that isr. So skip such block from common 1356 * sync flood interrupt isr calling. 1357 */ 1358 if (info.head.block == AMDGPU_RAS_BLOCK__PCIE_BIF) 1359 continue; 1360 1361 amdgpu_ras_error_query(adev, &info); 1362 } 1363 } 1364 1365 /* recovery begin */ 1366 1367 /* return 0 on success. 1368 * caller need free bps. 1369 */ 1370 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev, 1371 struct ras_badpage **bps, unsigned int *count) 1372 { 1373 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1374 struct ras_err_handler_data *data; 1375 int i = 0; 1376 int ret = 0; 1377 1378 if (!con || !con->eh_data || !bps || !count) 1379 return -EINVAL; 1380 1381 mutex_lock(&con->recovery_lock); 1382 data = con->eh_data; 1383 if (!data || data->count == 0) { 1384 *bps = NULL; 1385 ret = -EINVAL; 1386 goto out; 1387 } 1388 1389 *bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL); 1390 if (!*bps) { 1391 ret = -ENOMEM; 1392 goto out; 1393 } 1394 1395 for (; i < data->count; i++) { 1396 (*bps)[i] = (struct ras_badpage){ 1397 .bp = data->bps[i].retired_page, 1398 .size = AMDGPU_GPU_PAGE_SIZE, 1399 .flags = AMDGPU_RAS_RETIRE_PAGE_RESERVED, 1400 }; 1401 1402 if (data->last_reserved <= i) 1403 (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_PENDING; 1404 else if (data->bps_bo[i] == NULL) 1405 (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_FAULT; 1406 } 1407 1408 *count = data->count; 1409 out: 1410 mutex_unlock(&con->recovery_lock); 1411 return ret; 1412 } 1413 1414 static void amdgpu_ras_do_recovery(struct work_struct *work) 1415 { 1416 struct amdgpu_ras *ras = 1417 container_of(work, struct amdgpu_ras, recovery_work); 1418 1419 /* 1420 * Query and print non zero error counter per IP block for 1421 * awareness before recovering GPU. 1422 */ 1423 amdgpu_ras_log_on_err_counter(ras->adev); 1424 1425 if (amdgpu_device_should_recover_gpu(ras->adev)) 1426 amdgpu_device_gpu_recover(ras->adev, 0); 1427 atomic_set(&ras->in_recovery, 0); 1428 } 1429 1430 /* alloc/realloc bps array */ 1431 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev, 1432 struct ras_err_handler_data *data, int pages) 1433 { 1434 unsigned int old_space = data->count + data->space_left; 1435 unsigned int new_space = old_space + pages; 1436 unsigned int align_space = ALIGN(new_space, 512); 1437 void *bps = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL); 1438 struct amdgpu_bo **bps_bo = 1439 kmalloc(align_space * sizeof(*data->bps_bo), GFP_KERNEL); 1440 1441 if (!bps || !bps_bo) { 1442 kfree(bps); 1443 kfree(bps_bo); 1444 return -ENOMEM; 1445 } 1446 1447 if (data->bps) { 1448 memcpy(bps, data->bps, 1449 data->count * sizeof(*data->bps)); 1450 kfree(data->bps); 1451 } 1452 if (data->bps_bo) { 1453 memcpy(bps_bo, data->bps_bo, 1454 data->count * sizeof(*data->bps_bo)); 1455 kfree(data->bps_bo); 1456 } 1457 1458 data->bps = bps; 1459 data->bps_bo = bps_bo; 1460 data->space_left += align_space - old_space; 1461 return 0; 1462 } 1463 1464 /* it deal with vram only. */ 1465 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev, 1466 struct eeprom_table_record *bps, int pages) 1467 { 1468 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1469 struct ras_err_handler_data *data; 1470 int ret = 0; 1471 1472 if (!con || !con->eh_data || !bps || pages <= 0) 1473 return 0; 1474 1475 mutex_lock(&con->recovery_lock); 1476 data = con->eh_data; 1477 if (!data) 1478 goto out; 1479 1480 if (data->space_left <= pages) 1481 if (amdgpu_ras_realloc_eh_data_space(adev, data, pages)) { 1482 ret = -ENOMEM; 1483 goto out; 1484 } 1485 1486 memcpy(&data->bps[data->count], bps, pages * sizeof(*data->bps)); 1487 data->count += pages; 1488 data->space_left -= pages; 1489 1490 out: 1491 mutex_unlock(&con->recovery_lock); 1492 1493 return ret; 1494 } 1495 1496 /* 1497 * write error record array to eeprom, the function should be 1498 * protected by recovery_lock 1499 */ 1500 static int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev) 1501 { 1502 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1503 struct ras_err_handler_data *data; 1504 struct amdgpu_ras_eeprom_control *control; 1505 int save_count; 1506 1507 if (!con || !con->eh_data) 1508 return 0; 1509 1510 control = &con->eeprom_control; 1511 data = con->eh_data; 1512 save_count = data->count - control->num_recs; 1513 /* only new entries are saved */ 1514 if (save_count > 0) 1515 if (amdgpu_ras_eeprom_process_recods(control, 1516 &data->bps[control->num_recs], 1517 true, 1518 save_count)) { 1519 DRM_ERROR("Failed to save EEPROM table data!"); 1520 return -EIO; 1521 } 1522 1523 return 0; 1524 } 1525 1526 /* 1527 * read error record array in eeprom and reserve enough space for 1528 * storing new bad pages 1529 */ 1530 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev) 1531 { 1532 struct amdgpu_ras_eeprom_control *control = 1533 &adev->psp.ras.ras->eeprom_control; 1534 struct eeprom_table_record *bps = NULL; 1535 int ret = 0; 1536 1537 /* no bad page record, skip eeprom access */ 1538 if (!control->num_recs) 1539 return ret; 1540 1541 bps = kcalloc(control->num_recs, sizeof(*bps), GFP_KERNEL); 1542 if (!bps) 1543 return -ENOMEM; 1544 1545 if (amdgpu_ras_eeprom_process_recods(control, bps, false, 1546 control->num_recs)) { 1547 DRM_ERROR("Failed to load EEPROM table records!"); 1548 ret = -EIO; 1549 goto out; 1550 } 1551 1552 ret = amdgpu_ras_add_bad_pages(adev, bps, control->num_recs); 1553 1554 out: 1555 kfree(bps); 1556 return ret; 1557 } 1558 1559 /* 1560 * check if an address belongs to bad page 1561 * 1562 * Note: this check is only for umc block 1563 */ 1564 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev, 1565 uint64_t addr) 1566 { 1567 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1568 struct ras_err_handler_data *data; 1569 int i; 1570 bool ret = false; 1571 1572 if (!con || !con->eh_data) 1573 return ret; 1574 1575 mutex_lock(&con->recovery_lock); 1576 data = con->eh_data; 1577 if (!data) 1578 goto out; 1579 1580 addr >>= AMDGPU_GPU_PAGE_SHIFT; 1581 for (i = 0; i < data->count; i++) 1582 if (addr == data->bps[i].retired_page) { 1583 ret = true; 1584 goto out; 1585 } 1586 1587 out: 1588 mutex_unlock(&con->recovery_lock); 1589 return ret; 1590 } 1591 1592 /* called in gpu recovery/init */ 1593 int amdgpu_ras_reserve_bad_pages(struct amdgpu_device *adev) 1594 { 1595 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1596 struct ras_err_handler_data *data; 1597 uint64_t bp; 1598 struct amdgpu_bo *bo = NULL; 1599 int i, ret = 0; 1600 1601 if (!con || !con->eh_data) 1602 return 0; 1603 1604 mutex_lock(&con->recovery_lock); 1605 data = con->eh_data; 1606 if (!data) 1607 goto out; 1608 /* reserve vram at driver post stage. */ 1609 for (i = data->last_reserved; i < data->count; i++) { 1610 bp = data->bps[i].retired_page; 1611 1612 /* There are two cases of reserve error should be ignored: 1613 * 1) a ras bad page has been allocated (used by someone); 1614 * 2) a ras bad page has been reserved (duplicate error injection 1615 * for one page); 1616 */ 1617 if (amdgpu_bo_create_kernel_at(adev, bp << AMDGPU_GPU_PAGE_SHIFT, 1618 AMDGPU_GPU_PAGE_SIZE, 1619 AMDGPU_GEM_DOMAIN_VRAM, 1620 &bo, NULL)) 1621 DRM_WARN("RAS WARN: reserve vram for retired page %llx fail\n", bp); 1622 1623 data->bps_bo[i] = bo; 1624 data->last_reserved = i + 1; 1625 bo = NULL; 1626 } 1627 1628 /* continue to save bad pages to eeprom even reesrve_vram fails */ 1629 ret = amdgpu_ras_save_bad_pages(adev); 1630 out: 1631 mutex_unlock(&con->recovery_lock); 1632 return ret; 1633 } 1634 1635 /* called when driver unload */ 1636 static int amdgpu_ras_release_bad_pages(struct amdgpu_device *adev) 1637 { 1638 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1639 struct ras_err_handler_data *data; 1640 struct amdgpu_bo *bo; 1641 int i; 1642 1643 if (!con || !con->eh_data) 1644 return 0; 1645 1646 mutex_lock(&con->recovery_lock); 1647 data = con->eh_data; 1648 if (!data) 1649 goto out; 1650 1651 for (i = data->last_reserved - 1; i >= 0; i--) { 1652 bo = data->bps_bo[i]; 1653 1654 amdgpu_bo_free_kernel(&bo, NULL, NULL); 1655 1656 data->bps_bo[i] = bo; 1657 data->last_reserved = i; 1658 } 1659 out: 1660 mutex_unlock(&con->recovery_lock); 1661 return 0; 1662 } 1663 1664 int amdgpu_ras_recovery_init(struct amdgpu_device *adev) 1665 { 1666 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1667 struct ras_err_handler_data **data; 1668 int ret; 1669 1670 if (con) 1671 data = &con->eh_data; 1672 else 1673 return 0; 1674 1675 *data = kmalloc(sizeof(**data), GFP_KERNEL | __GFP_ZERO); 1676 if (!*data) { 1677 ret = -ENOMEM; 1678 goto out; 1679 } 1680 1681 mutex_init(&con->recovery_lock); 1682 INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery); 1683 atomic_set(&con->in_recovery, 0); 1684 con->adev = adev; 1685 1686 ret = amdgpu_ras_eeprom_init(&con->eeprom_control); 1687 if (ret) 1688 goto free; 1689 1690 if (con->eeprom_control.num_recs) { 1691 ret = amdgpu_ras_load_bad_pages(adev); 1692 if (ret) 1693 goto free; 1694 ret = amdgpu_ras_reserve_bad_pages(adev); 1695 if (ret) 1696 goto release; 1697 } 1698 1699 return 0; 1700 1701 release: 1702 amdgpu_ras_release_bad_pages(adev); 1703 free: 1704 kfree((*data)->bps); 1705 kfree((*data)->bps_bo); 1706 kfree(*data); 1707 con->eh_data = NULL; 1708 out: 1709 DRM_WARN("Failed to initialize ras recovery!\n"); 1710 1711 return ret; 1712 } 1713 1714 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev) 1715 { 1716 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1717 struct ras_err_handler_data *data = con->eh_data; 1718 1719 /* recovery_init failed to init it, fini is useless */ 1720 if (!data) 1721 return 0; 1722 1723 cancel_work_sync(&con->recovery_work); 1724 amdgpu_ras_release_bad_pages(adev); 1725 1726 mutex_lock(&con->recovery_lock); 1727 con->eh_data = NULL; 1728 kfree(data->bps); 1729 kfree(data->bps_bo); 1730 kfree(data); 1731 mutex_unlock(&con->recovery_lock); 1732 1733 return 0; 1734 } 1735 /* recovery end */ 1736 1737 /* return 0 if ras will reset gpu and repost.*/ 1738 int amdgpu_ras_request_reset_on_boot(struct amdgpu_device *adev, 1739 unsigned int block) 1740 { 1741 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev); 1742 1743 if (!ras) 1744 return -EINVAL; 1745 1746 ras->flags |= AMDGPU_RAS_FLAG_INIT_NEED_RESET; 1747 return 0; 1748 } 1749 1750 /* 1751 * check hardware's ras ability which will be saved in hw_supported. 1752 * if hardware does not support ras, we can skip some ras initializtion and 1753 * forbid some ras operations from IP. 1754 * if software itself, say boot parameter, limit the ras ability. We still 1755 * need allow IP do some limited operations, like disable. In such case, 1756 * we have to initialize ras as normal. but need check if operation is 1757 * allowed or not in each function. 1758 */ 1759 static void amdgpu_ras_check_supported(struct amdgpu_device *adev, 1760 uint32_t *hw_supported, uint32_t *supported) 1761 { 1762 *hw_supported = 0; 1763 *supported = 0; 1764 1765 if (amdgpu_sriov_vf(adev) || !adev->is_atom_fw || 1766 (adev->asic_type != CHIP_VEGA20 && 1767 adev->asic_type != CHIP_ARCTURUS)) 1768 return; 1769 1770 if (amdgpu_atomfirmware_mem_ecc_supported(adev)) { 1771 DRM_INFO("HBM ECC is active.\n"); 1772 *hw_supported |= (1 << AMDGPU_RAS_BLOCK__UMC | 1773 1 << AMDGPU_RAS_BLOCK__DF); 1774 } else 1775 DRM_INFO("HBM ECC is not presented.\n"); 1776 1777 if (amdgpu_atomfirmware_sram_ecc_supported(adev)) { 1778 DRM_INFO("SRAM ECC is active.\n"); 1779 *hw_supported |= ~(1 << AMDGPU_RAS_BLOCK__UMC | 1780 1 << AMDGPU_RAS_BLOCK__DF); 1781 } else 1782 DRM_INFO("SRAM ECC is not presented.\n"); 1783 1784 /* hw_supported needs to be aligned with RAS block mask. */ 1785 *hw_supported &= AMDGPU_RAS_BLOCK_MASK; 1786 1787 *supported = amdgpu_ras_enable == 0 ? 1788 0 : *hw_supported & amdgpu_ras_mask; 1789 } 1790 1791 int amdgpu_ras_init(struct amdgpu_device *adev) 1792 { 1793 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1794 int r; 1795 1796 if (con) 1797 return 0; 1798 1799 con = kmalloc(sizeof(struct amdgpu_ras) + 1800 sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT, 1801 GFP_KERNEL|__GFP_ZERO); 1802 if (!con) 1803 return -ENOMEM; 1804 1805 con->objs = (struct ras_manager *)(con + 1); 1806 1807 amdgpu_ras_set_context(adev, con); 1808 1809 amdgpu_ras_check_supported(adev, &con->hw_supported, 1810 &con->supported); 1811 if (!con->hw_supported) { 1812 amdgpu_ras_set_context(adev, NULL); 1813 kfree(con); 1814 return 0; 1815 } 1816 1817 con->features = 0; 1818 INIT_LIST_HEAD(&con->head); 1819 /* Might need get this flag from vbios. */ 1820 con->flags = RAS_DEFAULT_FLAGS; 1821 1822 if (adev->nbio.funcs->init_ras_controller_interrupt) { 1823 r = adev->nbio.funcs->init_ras_controller_interrupt(adev); 1824 if (r) 1825 return r; 1826 } 1827 1828 if (adev->nbio.funcs->init_ras_err_event_athub_interrupt) { 1829 r = adev->nbio.funcs->init_ras_err_event_athub_interrupt(adev); 1830 if (r) 1831 return r; 1832 } 1833 1834 amdgpu_ras_mask &= AMDGPU_RAS_BLOCK_MASK; 1835 1836 if (amdgpu_ras_fs_init(adev)) 1837 goto fs_out; 1838 1839 DRM_INFO("RAS INFO: ras initialized successfully, " 1840 "hardware ability[%x] ras_mask[%x]\n", 1841 con->hw_supported, con->supported); 1842 return 0; 1843 fs_out: 1844 amdgpu_ras_set_context(adev, NULL); 1845 kfree(con); 1846 1847 return -EINVAL; 1848 } 1849 1850 /* helper function to handle common stuff in ip late init phase */ 1851 int amdgpu_ras_late_init(struct amdgpu_device *adev, 1852 struct ras_common_if *ras_block, 1853 struct ras_fs_if *fs_info, 1854 struct ras_ih_if *ih_info) 1855 { 1856 int r; 1857 1858 /* disable RAS feature per IP block if it is not supported */ 1859 if (!amdgpu_ras_is_supported(adev, ras_block->block)) { 1860 amdgpu_ras_feature_enable_on_boot(adev, ras_block, 0); 1861 return 0; 1862 } 1863 1864 r = amdgpu_ras_feature_enable_on_boot(adev, ras_block, 1); 1865 if (r) { 1866 if (r == -EAGAIN) { 1867 /* request gpu reset. will run again */ 1868 amdgpu_ras_request_reset_on_boot(adev, 1869 ras_block->block); 1870 return 0; 1871 } else if (adev->in_suspend || adev->in_gpu_reset) { 1872 /* in resume phase, if fail to enable ras, 1873 * clean up all ras fs nodes, and disable ras */ 1874 goto cleanup; 1875 } else 1876 return r; 1877 } 1878 1879 /* in resume phase, no need to create ras fs node */ 1880 if (adev->in_suspend || adev->in_gpu_reset) 1881 return 0; 1882 1883 if (ih_info->cb) { 1884 r = amdgpu_ras_interrupt_add_handler(adev, ih_info); 1885 if (r) 1886 goto interrupt; 1887 } 1888 1889 r = amdgpu_ras_sysfs_create(adev, fs_info); 1890 if (r) 1891 goto sysfs; 1892 1893 return 0; 1894 cleanup: 1895 amdgpu_ras_sysfs_remove(adev, ras_block); 1896 sysfs: 1897 if (ih_info->cb) 1898 amdgpu_ras_interrupt_remove_handler(adev, ih_info); 1899 interrupt: 1900 amdgpu_ras_feature_enable(adev, ras_block, 0); 1901 return r; 1902 } 1903 1904 /* helper function to remove ras fs node and interrupt handler */ 1905 void amdgpu_ras_late_fini(struct amdgpu_device *adev, 1906 struct ras_common_if *ras_block, 1907 struct ras_ih_if *ih_info) 1908 { 1909 if (!ras_block || !ih_info) 1910 return; 1911 1912 amdgpu_ras_sysfs_remove(adev, ras_block); 1913 if (ih_info->cb) 1914 amdgpu_ras_interrupt_remove_handler(adev, ih_info); 1915 amdgpu_ras_feature_enable(adev, ras_block, 0); 1916 } 1917 1918 /* do some init work after IP late init as dependence. 1919 * and it runs in resume/gpu reset/booting up cases. 1920 */ 1921 void amdgpu_ras_resume(struct amdgpu_device *adev) 1922 { 1923 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1924 struct ras_manager *obj, *tmp; 1925 1926 if (!con) 1927 return; 1928 1929 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) { 1930 /* Set up all other IPs which are not implemented. There is a 1931 * tricky thing that IP's actual ras error type should be 1932 * MULTI_UNCORRECTABLE, but as driver does not handle it, so 1933 * ERROR_NONE make sense anyway. 1934 */ 1935 amdgpu_ras_enable_all_features(adev, 1); 1936 1937 /* We enable ras on all hw_supported block, but as boot 1938 * parameter might disable some of them and one or more IP has 1939 * not implemented yet. So we disable them on behalf. 1940 */ 1941 list_for_each_entry_safe(obj, tmp, &con->head, node) { 1942 if (!amdgpu_ras_is_supported(adev, obj->head.block)) { 1943 amdgpu_ras_feature_enable(adev, &obj->head, 0); 1944 /* there should be no any reference. */ 1945 WARN_ON(alive_obj(obj)); 1946 } 1947 } 1948 } 1949 1950 if (con->flags & AMDGPU_RAS_FLAG_INIT_NEED_RESET) { 1951 con->flags &= ~AMDGPU_RAS_FLAG_INIT_NEED_RESET; 1952 /* setup ras obj state as disabled. 1953 * for init_by_vbios case. 1954 * if we want to enable ras, just enable it in a normal way. 1955 * If we want do disable it, need setup ras obj as enabled, 1956 * then issue another TA disable cmd. 1957 * See feature_enable_on_boot 1958 */ 1959 amdgpu_ras_disable_all_features(adev, 1); 1960 amdgpu_ras_reset_gpu(adev); 1961 } 1962 } 1963 1964 void amdgpu_ras_suspend(struct amdgpu_device *adev) 1965 { 1966 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1967 1968 if (!con) 1969 return; 1970 1971 amdgpu_ras_disable_all_features(adev, 0); 1972 /* Make sure all ras objects are disabled. */ 1973 if (con->features) 1974 amdgpu_ras_disable_all_features(adev, 1); 1975 } 1976 1977 /* do some fini work before IP fini as dependence */ 1978 int amdgpu_ras_pre_fini(struct amdgpu_device *adev) 1979 { 1980 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1981 1982 if (!con) 1983 return 0; 1984 1985 /* Need disable ras on all IPs here before ip [hw/sw]fini */ 1986 amdgpu_ras_disable_all_features(adev, 0); 1987 amdgpu_ras_recovery_fini(adev); 1988 return 0; 1989 } 1990 1991 int amdgpu_ras_fini(struct amdgpu_device *adev) 1992 { 1993 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 1994 1995 if (!con) 1996 return 0; 1997 1998 amdgpu_ras_fs_fini(adev); 1999 amdgpu_ras_interrupt_remove_all(adev); 2000 2001 WARN(con->features, "Feature mask is not cleared"); 2002 2003 if (con->features) 2004 amdgpu_ras_disable_all_features(adev, 1); 2005 2006 amdgpu_ras_set_context(adev, NULL); 2007 kfree(con); 2008 2009 return 0; 2010 } 2011 2012 void amdgpu_ras_global_ras_isr(struct amdgpu_device *adev) 2013 { 2014 uint32_t hw_supported, supported; 2015 2016 amdgpu_ras_check_supported(adev, &hw_supported, &supported); 2017 if (!hw_supported) 2018 return; 2019 2020 if (atomic_cmpxchg(&amdgpu_ras_in_intr, 0, 1) == 0) { 2021 DRM_WARN("RAS event of type ERREVENT_ATHUB_INTERRUPT detected!\n"); 2022 2023 amdgpu_ras_reset_gpu(adev); 2024 } 2025 } 2026