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