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