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