1 /* 2 * Copyright 2008 Advanced Micro Devices, Inc. 3 * Copyright 2008 Red Hat Inc. 4 * Copyright 2009 Jerome Glisse. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 * 24 * Authors: Dave Airlie 25 * Alex Deucher 26 * Jerome Glisse 27 */ 28 29 #include <linux/aperture.h> 30 #include <linux/power_supply.h> 31 #include <linux/kthread.h> 32 #include <linux/module.h> 33 #include <linux/console.h> 34 #include <linux/slab.h> 35 #include <linux/iommu.h> 36 #include <linux/pci.h> 37 #include <linux/pci-p2pdma.h> 38 #include <linux/apple-gmux.h> 39 #include <linux/nospec.h> 40 41 #include <drm/drm_atomic_helper.h> 42 #include <drm/drm_client_event.h> 43 #include <drm/drm_crtc_helper.h> 44 #include <drm/drm_probe_helper.h> 45 #include <drm/amdgpu_drm.h> 46 #include <linux/device.h> 47 #include <linux/vgaarb.h> 48 #include <linux/vga_switcheroo.h> 49 #include <linux/efi.h> 50 #include "amdgpu.h" 51 #include "amdgpu_trace.h" 52 #include "amdgpu_i2c.h" 53 #include "atom.h" 54 #include "amdgpu_atombios.h" 55 #include "amdgpu_atomfirmware.h" 56 #include "amd_pcie.h" 57 #ifdef CONFIG_DRM_AMDGPU_SI 58 #include "si.h" 59 #endif 60 #ifdef CONFIG_DRM_AMDGPU_CIK 61 #include "cik.h" 62 #endif 63 #include "vi.h" 64 #include "soc15.h" 65 #include "nv.h" 66 #include "bif/bif_4_1_d.h" 67 #include <linux/firmware.h> 68 #include "amdgpu_vf_error.h" 69 70 #include "amdgpu_amdkfd.h" 71 #include "amdgpu_pm.h" 72 73 #include "amdgpu_xgmi.h" 74 #include "amdgpu_ras.h" 75 #include "amdgpu_wb.h" 76 #include "amdgpu_ras_mgr.h" 77 #include "amdgpu_pmu.h" 78 #include "amdgpu_smu.h" 79 #include "amdgpu_fru_eeprom.h" 80 #include "amdgpu_reset.h" 81 #include "amdgpu_virt.h" 82 #include "amdgpu_dev_coredump.h" 83 84 #include <linux/suspend.h> 85 #include <drm/task_barrier.h> 86 #include <linux/pm_runtime.h> 87 88 #include <drm/drm_drv.h> 89 90 #if IS_ENABLED(CONFIG_X86) 91 #include <asm/intel-family.h> 92 #include <asm/cpu_device_id.h> 93 #endif 94 95 MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin"); 96 MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin"); 97 MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin"); 98 MODULE_FIRMWARE("amdgpu/picasso_gpu_info.bin"); 99 MODULE_FIRMWARE("amdgpu/raven2_gpu_info.bin"); 100 MODULE_FIRMWARE("amdgpu/arcturus_gpu_info.bin"); 101 MODULE_FIRMWARE("amdgpu/navi12_gpu_info.bin"); 102 MODULE_FIRMWARE("amdgpu/cyan_skillfish_gpu_info.bin"); 103 104 #define AMDGPU_RESUME_MS 2000 105 #define AMDGPU_MAX_RETRY_LIMIT 2 106 #define AMDGPU_RETRY_SRIOV_RESET(r) ((r) == -EBUSY || (r) == -ETIMEDOUT || (r) == -EINVAL) 107 #define AMDGPU_PCIE_INDEX_FALLBACK (0x38 >> 2) 108 #define AMDGPU_PCIE_INDEX_HI_FALLBACK (0x44 >> 2) 109 #define AMDGPU_PCIE_DATA_FALLBACK (0x3C >> 2) 110 111 #define AMDGPU_VBIOS_SKIP (1U << 0) 112 #define AMDGPU_VBIOS_OPTIONAL (1U << 1) 113 114 static const struct drm_driver amdgpu_kms_driver; 115 116 const char *amdgpu_asic_name[] = { 117 "TAHITI", 118 "PITCAIRN", 119 "VERDE", 120 "OLAND", 121 "HAINAN", 122 "BONAIRE", 123 "KAVERI", 124 "KABINI", 125 "HAWAII", 126 "MULLINS", 127 "TOPAZ", 128 "TONGA", 129 "FIJI", 130 "CARRIZO", 131 "STONEY", 132 "POLARIS10", 133 "POLARIS11", 134 "POLARIS12", 135 "VEGAM", 136 "VEGA10", 137 "VEGA12", 138 "VEGA20", 139 "RAVEN", 140 "ARCTURUS", 141 "RENOIR", 142 "ALDEBARAN", 143 "NAVI10", 144 "CYAN_SKILLFISH", 145 "NAVI14", 146 "NAVI12", 147 "SIENNA_CICHLID", 148 "NAVY_FLOUNDER", 149 "VANGOGH", 150 "DIMGREY_CAVEFISH", 151 "BEIGE_GOBY", 152 "YELLOW_CARP", 153 "IP DISCOVERY", 154 "LAST", 155 }; 156 157 #define AMDGPU_IP_BLK_MASK_ALL GENMASK(AMD_IP_BLOCK_TYPE_NUM - 1, 0) 158 /* 159 * Default init level where all blocks are expected to be initialized. This is 160 * the level of initialization expected by default and also after a full reset 161 * of the device. 162 */ 163 struct amdgpu_init_level amdgpu_init_default = { 164 .level = AMDGPU_INIT_LEVEL_DEFAULT, 165 .hwini_ip_block_mask = AMDGPU_IP_BLK_MASK_ALL, 166 }; 167 168 struct amdgpu_init_level amdgpu_init_recovery = { 169 .level = AMDGPU_INIT_LEVEL_RESET_RECOVERY, 170 .hwini_ip_block_mask = AMDGPU_IP_BLK_MASK_ALL, 171 }; 172 173 /* 174 * Minimal blocks needed to be initialized before a XGMI hive can be reset. This 175 * is used for cases like reset on initialization where the entire hive needs to 176 * be reset before first use. 177 */ 178 struct amdgpu_init_level amdgpu_init_minimal_xgmi = { 179 .level = AMDGPU_INIT_LEVEL_MINIMAL_XGMI, 180 .hwini_ip_block_mask = 181 BIT(AMD_IP_BLOCK_TYPE_GMC) | BIT(AMD_IP_BLOCK_TYPE_SMC) | 182 BIT(AMD_IP_BLOCK_TYPE_COMMON) | BIT(AMD_IP_BLOCK_TYPE_IH) | 183 BIT(AMD_IP_BLOCK_TYPE_PSP) 184 }; 185 186 static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev); 187 static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev); 188 static int amdgpu_device_ip_resume_phase3(struct amdgpu_device *adev); 189 190 static void amdgpu_device_load_switch_state(struct amdgpu_device *adev); 191 192 static inline bool amdgpu_ip_member_of_hwini(struct amdgpu_device *adev, 193 enum amd_ip_block_type block) 194 { 195 return (adev->init_lvl->hwini_ip_block_mask & (1U << block)) != 0; 196 } 197 198 void amdgpu_set_init_level(struct amdgpu_device *adev, 199 enum amdgpu_init_lvl_id lvl) 200 { 201 switch (lvl) { 202 case AMDGPU_INIT_LEVEL_MINIMAL_XGMI: 203 adev->init_lvl = &amdgpu_init_minimal_xgmi; 204 break; 205 case AMDGPU_INIT_LEVEL_RESET_RECOVERY: 206 adev->init_lvl = &amdgpu_init_recovery; 207 break; 208 case AMDGPU_INIT_LEVEL_DEFAULT: 209 fallthrough; 210 default: 211 adev->init_lvl = &amdgpu_init_default; 212 break; 213 } 214 } 215 216 static inline void amdgpu_device_stop_pending_resets(struct amdgpu_device *adev); 217 static int amdgpu_device_pm_notifier(struct notifier_block *nb, unsigned long mode, 218 void *data); 219 220 /** 221 * DOC: pcie_replay_count 222 * 223 * The amdgpu driver provides a sysfs API for reporting the total number 224 * of PCIe replays (NAKs). 225 * The file pcie_replay_count is used for this and returns the total 226 * number of replays as a sum of the NAKs generated and NAKs received. 227 */ 228 229 static ssize_t amdgpu_device_get_pcie_replay_count(struct device *dev, 230 struct device_attribute *attr, char *buf) 231 { 232 struct drm_device *ddev = dev_get_drvdata(dev); 233 struct amdgpu_device *adev = drm_to_adev(ddev); 234 uint64_t cnt = amdgpu_asic_get_pcie_replay_count(adev); 235 236 return sysfs_emit(buf, "%llu\n", cnt); 237 } 238 239 static DEVICE_ATTR(pcie_replay_count, 0444, 240 amdgpu_device_get_pcie_replay_count, NULL); 241 242 static int amdgpu_device_attr_sysfs_init(struct amdgpu_device *adev) 243 { 244 int ret = 0; 245 246 if (amdgpu_nbio_is_replay_cnt_supported(adev)) 247 ret = sysfs_create_file(&adev->dev->kobj, 248 &dev_attr_pcie_replay_count.attr); 249 250 return ret; 251 } 252 253 static void amdgpu_device_attr_sysfs_fini(struct amdgpu_device *adev) 254 { 255 if (amdgpu_nbio_is_replay_cnt_supported(adev)) 256 sysfs_remove_file(&adev->dev->kobj, 257 &dev_attr_pcie_replay_count.attr); 258 } 259 260 static ssize_t amdgpu_sysfs_reg_state_get(struct file *f, struct kobject *kobj, 261 const struct bin_attribute *attr, char *buf, 262 loff_t ppos, size_t count) 263 { 264 struct device *dev = kobj_to_dev(kobj); 265 struct drm_device *ddev = dev_get_drvdata(dev); 266 struct amdgpu_device *adev = drm_to_adev(ddev); 267 ssize_t bytes_read; 268 269 switch (ppos) { 270 case AMDGPU_SYS_REG_STATE_XGMI: 271 bytes_read = amdgpu_asic_get_reg_state( 272 adev, AMDGPU_REG_STATE_TYPE_XGMI, buf, count); 273 break; 274 case AMDGPU_SYS_REG_STATE_WAFL: 275 bytes_read = amdgpu_asic_get_reg_state( 276 adev, AMDGPU_REG_STATE_TYPE_WAFL, buf, count); 277 break; 278 case AMDGPU_SYS_REG_STATE_PCIE: 279 bytes_read = amdgpu_asic_get_reg_state( 280 adev, AMDGPU_REG_STATE_TYPE_PCIE, buf, count); 281 break; 282 case AMDGPU_SYS_REG_STATE_USR: 283 bytes_read = amdgpu_asic_get_reg_state( 284 adev, AMDGPU_REG_STATE_TYPE_USR, buf, count); 285 break; 286 case AMDGPU_SYS_REG_STATE_USR_1: 287 bytes_read = amdgpu_asic_get_reg_state( 288 adev, AMDGPU_REG_STATE_TYPE_USR_1, buf, count); 289 break; 290 default: 291 return -EINVAL; 292 } 293 294 return bytes_read; 295 } 296 297 static const BIN_ATTR(reg_state, 0444, amdgpu_sysfs_reg_state_get, NULL, 298 AMDGPU_SYS_REG_STATE_END); 299 300 int amdgpu_reg_state_sysfs_init(struct amdgpu_device *adev) 301 { 302 int ret; 303 304 if (!amdgpu_asic_get_reg_state_supported(adev)) 305 return 0; 306 307 ret = sysfs_create_bin_file(&adev->dev->kobj, &bin_attr_reg_state); 308 309 return ret; 310 } 311 312 void amdgpu_reg_state_sysfs_fini(struct amdgpu_device *adev) 313 { 314 if (!amdgpu_asic_get_reg_state_supported(adev)) 315 return; 316 sysfs_remove_bin_file(&adev->dev->kobj, &bin_attr_reg_state); 317 } 318 319 /** 320 * DOC: board_info 321 * 322 * The amdgpu driver provides a sysfs API for giving board related information. 323 * It provides the form factor information in the format 324 * 325 * type : form factor 326 * 327 * Possible form factor values 328 * 329 * - "cem" - PCIE CEM card 330 * - "oam" - Open Compute Accelerator Module 331 * - "unknown" - Not known 332 * 333 */ 334 335 static ssize_t amdgpu_device_get_board_info(struct device *dev, 336 struct device_attribute *attr, 337 char *buf) 338 { 339 struct drm_device *ddev = dev_get_drvdata(dev); 340 struct amdgpu_device *adev = drm_to_adev(ddev); 341 enum amdgpu_pkg_type pkg_type = AMDGPU_PKG_TYPE_CEM; 342 const char *pkg; 343 344 if (adev->smuio.funcs && adev->smuio.funcs->get_pkg_type) 345 pkg_type = adev->smuio.funcs->get_pkg_type(adev); 346 347 switch (pkg_type) { 348 case AMDGPU_PKG_TYPE_CEM: 349 pkg = "cem"; 350 break; 351 case AMDGPU_PKG_TYPE_OAM: 352 pkg = "oam"; 353 break; 354 default: 355 pkg = "unknown"; 356 break; 357 } 358 359 return sysfs_emit(buf, "%s : %s\n", "type", pkg); 360 } 361 362 static DEVICE_ATTR(board_info, 0444, amdgpu_device_get_board_info, NULL); 363 364 static struct attribute *amdgpu_board_attrs[] = { 365 &dev_attr_board_info.attr, 366 NULL, 367 }; 368 369 static umode_t amdgpu_board_attrs_is_visible(struct kobject *kobj, 370 struct attribute *attr, int n) 371 { 372 struct device *dev = kobj_to_dev(kobj); 373 struct drm_device *ddev = dev_get_drvdata(dev); 374 struct amdgpu_device *adev = drm_to_adev(ddev); 375 376 if (adev->flags & AMD_IS_APU) 377 return 0; 378 379 return attr->mode; 380 } 381 382 static const struct attribute_group amdgpu_board_attrs_group = { 383 .attrs = amdgpu_board_attrs, 384 .is_visible = amdgpu_board_attrs_is_visible 385 }; 386 387 /** 388 * DOC: uma/carveout_options 389 * 390 * This is a read-only file that lists all available UMA allocation 391 * options and their corresponding indices. Example output:: 392 * 393 * $ cat uma/carveout_options 394 * 0: Minimum (512 MB) 395 * 1: (1 GB) 396 * 2: (2 GB) 397 * 3: (4 GB) 398 * 4: (6 GB) 399 * 5: (8 GB) 400 * 6: (12 GB) 401 * 7: Medium (16 GB) 402 * 8: (24 GB) 403 * 9: High (32 GB) 404 */ 405 static ssize_t carveout_options_show(struct device *dev, 406 struct device_attribute *attr, 407 char *buf) 408 { 409 struct drm_device *ddev = dev_get_drvdata(dev); 410 struct amdgpu_device *adev = drm_to_adev(ddev); 411 struct amdgpu_uma_carveout_info *uma_info = &adev->uma_info; 412 uint32_t memory_carved; 413 ssize_t size = 0; 414 415 if (!uma_info || !uma_info->num_entries) 416 return -ENODEV; 417 418 for (int i = 0; i < uma_info->num_entries; i++) { 419 memory_carved = uma_info->entries[i].memory_carved_mb; 420 if (memory_carved >= SZ_1G/SZ_1M) { 421 size += sysfs_emit_at(buf, size, "%d: %s (%u GB)\n", 422 i, 423 uma_info->entries[i].name, 424 memory_carved >> 10); 425 } else { 426 size += sysfs_emit_at(buf, size, "%d: %s (%u MB)\n", 427 i, 428 uma_info->entries[i].name, 429 memory_carved); 430 } 431 } 432 433 return size; 434 } 435 static DEVICE_ATTR_RO(carveout_options); 436 437 /** 438 * DOC: uma/carveout 439 * 440 * This file is both readable and writable. When read, it shows the 441 * index of the current setting. Writing a valid index to this file 442 * allows users to change the UMA carveout size to the selected option 443 * on the next boot. 444 * 445 * The available options and their corresponding indices can be read 446 * from the uma/carveout_options file. 447 */ 448 static ssize_t carveout_show(struct device *dev, 449 struct device_attribute *attr, 450 char *buf) 451 { 452 struct drm_device *ddev = dev_get_drvdata(dev); 453 struct amdgpu_device *adev = drm_to_adev(ddev); 454 455 return sysfs_emit(buf, "%u\n", adev->uma_info.uma_option_index); 456 } 457 458 static ssize_t carveout_store(struct device *dev, 459 struct device_attribute *attr, 460 const char *buf, size_t count) 461 { 462 struct drm_device *ddev = dev_get_drvdata(dev); 463 struct amdgpu_device *adev = drm_to_adev(ddev); 464 struct amdgpu_uma_carveout_info *uma_info = &adev->uma_info; 465 struct amdgpu_uma_carveout_option *opt; 466 unsigned long val; 467 uint8_t flags; 468 int r; 469 470 r = kstrtoul(buf, 10, &val); 471 if (r) 472 return r; 473 474 if (val >= uma_info->num_entries) 475 return -EINVAL; 476 477 val = array_index_nospec(val, uma_info->num_entries); 478 opt = &uma_info->entries[val]; 479 480 if (!(opt->flags & AMDGPU_UMA_FLAG_AUTO) && 481 !(opt->flags & AMDGPU_UMA_FLAG_CUSTOM)) { 482 drm_err_once(ddev, "Option %lu not supported due to lack of Custom/Auto flag", val); 483 return -EINVAL; 484 } 485 486 flags = opt->flags; 487 flags &= ~((flags & AMDGPU_UMA_FLAG_AUTO) >> 1); 488 489 guard(mutex)(&uma_info->update_lock); 490 491 r = amdgpu_acpi_set_uma_allocation_size(adev, val, flags); 492 if (r) 493 return r; 494 495 uma_info->uma_option_index = val; 496 497 return count; 498 } 499 static DEVICE_ATTR_RW(carveout); 500 501 static struct attribute *amdgpu_uma_attrs[] = { 502 &dev_attr_carveout.attr, 503 &dev_attr_carveout_options.attr, 504 NULL 505 }; 506 507 const struct attribute_group amdgpu_uma_attr_group = { 508 .name = "uma", 509 .attrs = amdgpu_uma_attrs 510 }; 511 512 static void amdgpu_uma_sysfs_init(struct amdgpu_device *adev) 513 { 514 int rc; 515 516 if (!(adev->flags & AMD_IS_APU)) 517 return; 518 519 if (!amdgpu_acpi_is_set_uma_allocation_size_supported()) 520 return; 521 522 rc = amdgpu_atomfirmware_get_uma_carveout_info(adev, &adev->uma_info); 523 if (rc) { 524 drm_dbg(adev_to_drm(adev), 525 "Failed to parse UMA carveout info from VBIOS: %d\n", rc); 526 goto out_info; 527 } 528 529 mutex_init(&adev->uma_info.update_lock); 530 531 rc = devm_device_add_group(adev->dev, &amdgpu_uma_attr_group); 532 if (rc) { 533 drm_dbg(adev_to_drm(adev), "Failed to add UMA carveout sysfs interfaces %d\n", rc); 534 goto out_attr; 535 } 536 537 return; 538 539 out_attr: 540 mutex_destroy(&adev->uma_info.update_lock); 541 out_info: 542 return; 543 } 544 545 static void amdgpu_uma_sysfs_fini(struct amdgpu_device *adev) 546 { 547 struct amdgpu_uma_carveout_info *uma_info = &adev->uma_info; 548 549 if (!amdgpu_acpi_is_set_uma_allocation_size_supported()) 550 return; 551 552 mutex_destroy(&uma_info->update_lock); 553 uma_info->num_entries = 0; 554 } 555 556 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev); 557 558 /** 559 * amdgpu_device_supports_px - Is the device a dGPU with ATPX power control 560 * 561 * @adev: amdgpu device pointer 562 * 563 * Returns true if the device is a dGPU with ATPX power control, 564 * otherwise return false. 565 */ 566 bool amdgpu_device_supports_px(struct amdgpu_device *adev) 567 { 568 if ((adev->flags & AMD_IS_PX) && !amdgpu_is_atpx_hybrid()) 569 return true; 570 return false; 571 } 572 573 /** 574 * amdgpu_device_supports_boco - Is the device a dGPU with ACPI power resources 575 * 576 * @adev: amdgpu device pointer 577 * 578 * Returns true if the device is a dGPU with ACPI power control, 579 * otherwise return false. 580 */ 581 bool amdgpu_device_supports_boco(struct amdgpu_device *adev) 582 { 583 if (!IS_ENABLED(CONFIG_HOTPLUG_PCI_PCIE)) 584 return false; 585 586 if (adev->has_pr3 || 587 ((adev->flags & AMD_IS_PX) && amdgpu_is_atpx_hybrid())) 588 return true; 589 return false; 590 } 591 592 /** 593 * amdgpu_device_supports_baco - Does the device support BACO 594 * 595 * @adev: amdgpu device pointer 596 * 597 * Return: 598 * 1 if the device supports BACO; 599 * 3 if the device supports MACO (only works if BACO is supported) 600 * otherwise return 0. 601 */ 602 int amdgpu_device_supports_baco(struct amdgpu_device *adev) 603 { 604 return amdgpu_asic_supports_baco(adev); 605 } 606 607 void amdgpu_device_detect_runtime_pm_mode(struct amdgpu_device *adev) 608 { 609 int bamaco_support; 610 611 adev->pm.rpm_mode = AMDGPU_RUNPM_NONE; 612 bamaco_support = amdgpu_device_supports_baco(adev); 613 614 switch (amdgpu_runtime_pm) { 615 case 2: 616 if (bamaco_support & MACO_SUPPORT) { 617 adev->pm.rpm_mode = AMDGPU_RUNPM_BAMACO; 618 dev_info(adev->dev, "Forcing BAMACO for runtime pm\n"); 619 } else if (bamaco_support == BACO_SUPPORT) { 620 adev->pm.rpm_mode = AMDGPU_RUNPM_BACO; 621 dev_info(adev->dev, "Requested mode BAMACO not available,fallback to use BACO\n"); 622 } 623 break; 624 case 1: 625 if (bamaco_support & BACO_SUPPORT) { 626 adev->pm.rpm_mode = AMDGPU_RUNPM_BACO; 627 dev_info(adev->dev, "Forcing BACO for runtime pm\n"); 628 } 629 break; 630 case -1: 631 case -2: 632 if (amdgpu_device_supports_px(adev)) { 633 /* enable PX as runtime mode */ 634 adev->pm.rpm_mode = AMDGPU_RUNPM_PX; 635 dev_info(adev->dev, "Using ATPX for runtime pm\n"); 636 } else if (amdgpu_device_supports_boco(adev)) { 637 /* enable boco as runtime mode */ 638 adev->pm.rpm_mode = AMDGPU_RUNPM_BOCO; 639 dev_info(adev->dev, "Using BOCO for runtime pm\n"); 640 } else { 641 if (!bamaco_support) 642 goto no_runtime_pm; 643 644 switch (adev->asic_type) { 645 case CHIP_VEGA20: 646 case CHIP_ARCTURUS: 647 /* BACO are not supported on vega20 and arctrus */ 648 break; 649 case CHIP_VEGA10: 650 /* enable BACO as runpm mode if noretry=0 */ 651 if (!adev->gmc.noretry && !amdgpu_passthrough(adev)) 652 adev->pm.rpm_mode = AMDGPU_RUNPM_BACO; 653 break; 654 default: 655 /* enable BACO as runpm mode on CI+ */ 656 if (!amdgpu_passthrough(adev)) 657 adev->pm.rpm_mode = AMDGPU_RUNPM_BACO; 658 break; 659 } 660 661 if (adev->pm.rpm_mode == AMDGPU_RUNPM_BACO) { 662 if (bamaco_support & MACO_SUPPORT) { 663 adev->pm.rpm_mode = AMDGPU_RUNPM_BAMACO; 664 dev_info(adev->dev, "Using BAMACO for runtime pm\n"); 665 } else { 666 dev_info(adev->dev, "Using BACO for runtime pm\n"); 667 } 668 } 669 } 670 break; 671 case 0: 672 dev_info(adev->dev, "runtime pm is manually disabled\n"); 673 break; 674 default: 675 break; 676 } 677 678 no_runtime_pm: 679 if (adev->pm.rpm_mode == AMDGPU_RUNPM_NONE) 680 dev_info(adev->dev, "Runtime PM not available\n"); 681 } 682 /** 683 * amdgpu_device_supports_smart_shift - Is the device dGPU with 684 * smart shift support 685 * 686 * @adev: amdgpu device pointer 687 * 688 * Returns true if the device is a dGPU with Smart Shift support, 689 * otherwise returns false. 690 */ 691 bool amdgpu_device_supports_smart_shift(struct amdgpu_device *adev) 692 { 693 return (amdgpu_device_supports_boco(adev) && 694 amdgpu_acpi_is_power_shift_control_supported()); 695 } 696 697 /* 698 * VRAM access helper functions 699 */ 700 701 /** 702 * amdgpu_device_mm_access - access vram by MM_INDEX/MM_DATA 703 * 704 * @adev: amdgpu_device pointer 705 * @pos: offset of the buffer in vram 706 * @buf: virtual address of the buffer in system memory 707 * @size: read/write size, sizeof(@buf) must > @size 708 * @write: true - write to vram, otherwise - read from vram 709 */ 710 void amdgpu_device_mm_access(struct amdgpu_device *adev, loff_t pos, 711 void *buf, size_t size, bool write) 712 { 713 unsigned long flags; 714 uint32_t hi = ~0, tmp = 0; 715 uint32_t *data = buf; 716 uint64_t last; 717 int idx; 718 719 if (!drm_dev_enter(adev_to_drm(adev), &idx)) 720 return; 721 722 if (!IS_ALIGNED(pos, 4) || !IS_ALIGNED(size, 4)) { 723 dev_err(adev->dev, "unaligned pos/size (pos=0x%llx, size=0x%zx)\n", 724 pos, size); 725 drm_dev_exit(idx); 726 return; 727 } 728 729 spin_lock_irqsave(&adev->mmio_idx_lock, flags); 730 for (last = pos + size; pos < last; pos += 4) { 731 tmp = pos >> 31; 732 733 WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)pos) | 0x80000000); 734 if (tmp != hi) { 735 WREG32_NO_KIQ(mmMM_INDEX_HI, tmp); 736 hi = tmp; 737 } 738 if (write) 739 WREG32_NO_KIQ(mmMM_DATA, *data++); 740 else 741 *data++ = RREG32_NO_KIQ(mmMM_DATA); 742 } 743 744 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags); 745 drm_dev_exit(idx); 746 } 747 748 #ifdef CONFIG_64BIT 749 /* 750 * During early SR-IOV VF init, host-provided init data can live in FB before 751 * the normal VRAM aperture mapping is ready. Use a temporary BAR0 mapping for 752 * reads only, and verify it matches the VRAM aperture when aperture information 753 * is already available. 754 */ 755 static int amdgpu_device_read_fb_via_bar0(struct amdgpu_device *adev, 756 u64 offset, void *buf, size_t size) 757 { 758 resource_size_t aper_base, aper_size, bar_start, bar_size, map_base; 759 void __iomem *vram; 760 size_t map_offset, map_size; 761 unsigned long flags; 762 u64 end; 763 764 if (!buf || !size) 765 return -EINVAL; 766 767 flags = pci_resource_flags(adev->pdev, 0); 768 if ((flags & IORESOURCE_UNSET) || !(flags & IORESOURCE_MEM)) 769 return -EINVAL; 770 771 bar_start = pci_resource_start(adev->pdev, 0); 772 bar_size = pci_resource_len(adev->pdev, 0); 773 if (!bar_size) 774 return -ENODEV; 775 776 aper_base = adev->gmc.aper_base; 777 aper_size = adev->gmc.visible_vram_size ? adev->gmc.visible_vram_size : 778 adev->gmc.aper_size; 779 780 if (aper_base || aper_size) { 781 if (aper_base != bar_start || aper_size > bar_size) 782 return -EINVAL; 783 } else { 784 aper_base = bar_start; 785 aper_size = bar_size; 786 } 787 788 if (check_add_overflow(offset, size, &end) || end > aper_size) 789 return -EINVAL; 790 791 map_offset = offset_in_page(offset); 792 map_base = aper_base + (offset & PAGE_MASK); 793 map_size = PAGE_ALIGN(map_offset + size); 794 795 vram = ioremap_wc(map_base, map_size); 796 if (!vram) 797 return -ENOMEM; 798 799 memcpy_fromio(buf, (u8 __iomem *)vram + map_offset, size); 800 iounmap(vram); 801 802 return 0; 803 } 804 #endif 805 806 /** 807 * amdgpu_device_aper_access - access vram by vram aperture 808 * 809 * @adev: amdgpu_device pointer 810 * @pos: offset of the buffer in vram 811 * @buf: virtual address of the buffer in system memory 812 * @size: read/write size, sizeof(@buf) must > @size 813 * @write: true - write to vram, otherwise - read from vram 814 * 815 * The return value means how many bytes have been transferred. 816 */ 817 size_t amdgpu_device_aper_access(struct amdgpu_device *adev, loff_t pos, 818 void *buf, size_t size, bool write) 819 { 820 #ifdef CONFIG_64BIT 821 void __iomem *addr; 822 size_t count = 0; 823 uint64_t last; 824 825 if (!adev->mman.aper_base_kaddr) { 826 /* Writes still require the regular aperture/MM path. */ 827 if (!write && !amdgpu_device_read_fb_via_bar0(adev, pos, buf, size)) 828 return size; 829 return 0; 830 } 831 832 last = min(pos + size, adev->gmc.visible_vram_size); 833 if (last > pos) { 834 addr = adev->mman.aper_base_kaddr + pos; 835 count = last - pos; 836 837 if (write) { 838 memcpy_toio(addr, buf, count); 839 /* Make sure HDP write cache flush happens without any reordering 840 * after the system memory contents are sent over PCIe device 841 */ 842 mb(); 843 amdgpu_device_flush_hdp(adev, NULL); 844 } else { 845 amdgpu_device_invalidate_hdp(adev, NULL); 846 /* Make sure HDP read cache is invalidated before issuing a read 847 * to the PCIe device 848 */ 849 mb(); 850 memcpy_fromio(buf, addr, count); 851 } 852 853 } 854 855 return count; 856 #else 857 return 0; 858 #endif 859 } 860 861 /** 862 * amdgpu_device_vram_access - read/write a buffer in vram 863 * 864 * @adev: amdgpu_device pointer 865 * @pos: offset of the buffer in vram 866 * @buf: virtual address of the buffer in system memory 867 * @size: read/write size, sizeof(@buf) must > @size 868 * @write: true - write to vram, otherwise - read from vram 869 */ 870 void amdgpu_device_vram_access(struct amdgpu_device *adev, loff_t pos, 871 void *buf, size_t size, bool write) 872 { 873 size_t count; 874 875 /* try to using vram apreature to access vram first */ 876 count = amdgpu_device_aper_access(adev, pos, buf, size, write); 877 size -= count; 878 if (size) { 879 /* using MM to access rest vram */ 880 pos += count; 881 buf += count; 882 amdgpu_device_mm_access(adev, pos, buf, size, write); 883 } 884 } 885 886 /* 887 * register access helper functions. 888 */ 889 890 /* Check if hw access should be skipped because of hotplug or device error */ 891 bool amdgpu_device_skip_hw_access(struct amdgpu_device *adev) 892 { 893 if (adev->no_hw_access) 894 return true; 895 896 #ifdef CONFIG_LOCKDEP 897 /* 898 * This is a bit complicated to understand, so worth a comment. What we assert 899 * here is that the GPU reset is not running on another thread in parallel. 900 * 901 * For this we trylock the read side of the reset semaphore, if that succeeds 902 * we know that the reset is not running in parallel. 903 * 904 * If the trylock fails we assert that we are either already holding the read 905 * side of the lock or are the reset thread itself and hold the write side of 906 * the lock. 907 */ 908 if (in_task()) { 909 if (down_read_trylock(&adev->reset_domain->sem)) 910 up_read(&adev->reset_domain->sem); 911 else 912 lockdep_assert_held(&adev->reset_domain->sem); 913 } 914 #endif 915 return false; 916 } 917 918 /** 919 * amdgpu_device_get_rev_id - query device rev_id 920 * 921 * @adev: amdgpu_device pointer 922 * 923 * Return device rev_id 924 */ 925 u32 amdgpu_device_get_rev_id(struct amdgpu_device *adev) 926 { 927 return adev->nbio.funcs->get_rev_id(adev); 928 } 929 930 static uint32_t amdgpu_device_get_vbios_flags(struct amdgpu_device *adev) 931 { 932 if (hweight32(adev->aid_mask) && (adev->flags & AMD_IS_APU)) 933 return AMDGPU_VBIOS_SKIP; 934 935 if (hweight32(adev->aid_mask) && amdgpu_passthrough(adev)) 936 return AMDGPU_VBIOS_OPTIONAL; 937 938 return 0; 939 } 940 941 /** 942 * amdgpu_device_asic_init - Wrapper for atom asic_init 943 * 944 * @adev: amdgpu_device pointer 945 * 946 * Does any asic specific work and then calls atom asic init. 947 */ 948 static int amdgpu_device_asic_init(struct amdgpu_device *adev) 949 { 950 uint32_t flags; 951 bool optional; 952 int ret; 953 954 amdgpu_asic_pre_asic_init(adev); 955 flags = amdgpu_device_get_vbios_flags(adev); 956 optional = !!(flags & (AMDGPU_VBIOS_OPTIONAL | AMDGPU_VBIOS_SKIP)); 957 958 if (amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 3) || 959 amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 4) || 960 amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 5, 0) || 961 amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(11, 0, 0)) { 962 amdgpu_psp_wait_for_bootloader(adev); 963 if (optional && !adev->bios) 964 return 0; 965 966 ret = amdgpu_atomfirmware_asic_init(adev, true); 967 return ret; 968 } else { 969 if (optional && !adev->bios) 970 return 0; 971 972 return amdgpu_atom_asic_init(adev->mode_info.atom_context); 973 } 974 975 return 0; 976 } 977 978 /** 979 * amdgpu_device_mem_scratch_init - allocate the VRAM scratch page 980 * 981 * @adev: amdgpu_device pointer 982 * 983 * Allocates a scratch page of VRAM for use by various things in the 984 * driver. 985 */ 986 static int amdgpu_device_mem_scratch_init(struct amdgpu_device *adev) 987 { 988 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE, PAGE_SIZE, 989 AMDGPU_GEM_DOMAIN_VRAM | 990 AMDGPU_GEM_DOMAIN_GTT, 991 &adev->mem_scratch.robj, 992 &adev->mem_scratch.gpu_addr, 993 (void **)&adev->mem_scratch.ptr); 994 } 995 996 /** 997 * amdgpu_device_mem_scratch_fini - Free the VRAM scratch page 998 * 999 * @adev: amdgpu_device pointer 1000 * 1001 * Frees the VRAM scratch page. 1002 */ 1003 static void amdgpu_device_mem_scratch_fini(struct amdgpu_device *adev) 1004 { 1005 amdgpu_bo_free_kernel(&adev->mem_scratch.robj, NULL, NULL); 1006 } 1007 1008 /** 1009 * amdgpu_device_program_register_sequence - program an array of registers. 1010 * 1011 * @adev: amdgpu_device pointer 1012 * @registers: pointer to the register array 1013 * @array_size: size of the register array 1014 * 1015 * Programs an array or registers with and or masks. 1016 * This is a helper for setting golden registers. 1017 */ 1018 void amdgpu_device_program_register_sequence(struct amdgpu_device *adev, 1019 const u32 *registers, 1020 const u32 array_size) 1021 { 1022 u32 tmp, reg, and_mask, or_mask; 1023 int i; 1024 1025 if (array_size % 3) 1026 return; 1027 1028 for (i = 0; i < array_size; i += 3) { 1029 reg = registers[i + 0]; 1030 and_mask = registers[i + 1]; 1031 or_mask = registers[i + 2]; 1032 1033 if (and_mask == 0xffffffff) { 1034 tmp = or_mask; 1035 } else { 1036 tmp = RREG32(reg); 1037 tmp &= ~and_mask; 1038 if (adev->family >= AMDGPU_FAMILY_AI) 1039 tmp |= (or_mask & and_mask); 1040 else 1041 tmp |= or_mask; 1042 } 1043 WREG32(reg, tmp); 1044 } 1045 } 1046 1047 /** 1048 * amdgpu_device_pci_config_reset - reset the GPU 1049 * 1050 * @adev: amdgpu_device pointer 1051 * 1052 * Resets the GPU using the pci config reset sequence. 1053 * Only applicable to asics prior to vega10. 1054 */ 1055 void amdgpu_device_pci_config_reset(struct amdgpu_device *adev) 1056 { 1057 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA); 1058 } 1059 1060 /** 1061 * amdgpu_device_pci_reset - reset the GPU using generic PCI means 1062 * 1063 * @adev: amdgpu_device pointer 1064 * 1065 * Resets the GPU using generic pci reset interfaces (FLR, SBR, etc.). 1066 */ 1067 int amdgpu_device_pci_reset(struct amdgpu_device *adev) 1068 { 1069 return pci_reset_function(adev->pdev); 1070 } 1071 1072 /** 1073 * amdgpu_device_resize_fb_bar - try to resize FB BAR 1074 * 1075 * @adev: amdgpu_device pointer 1076 * 1077 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not 1078 * to fail, but if any of the BARs is not accessible after the size we abort 1079 * driver loading by returning -ENODEV. 1080 */ 1081 int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev) 1082 { 1083 int rbar_size = pci_rebar_bytes_to_size(adev->gmc.real_vram_size); 1084 struct pci_bus *root; 1085 struct resource *res; 1086 int max_size, r; 1087 unsigned int i; 1088 u16 cmd; 1089 1090 if (!IS_ENABLED(CONFIG_PHYS_ADDR_T_64BIT)) 1091 return 0; 1092 1093 /* Bypass for VF */ 1094 if (amdgpu_sriov_vf(adev)) 1095 return 0; 1096 1097 if (!amdgpu_rebar) 1098 return 0; 1099 1100 /* resizing on Dell G5 SE platforms causes problems with runtime pm */ 1101 if ((amdgpu_runtime_pm != 0) && 1102 adev->pdev->vendor == PCI_VENDOR_ID_ATI && 1103 adev->pdev->device == 0x731f && 1104 adev->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL) 1105 return 0; 1106 1107 /* PCI_EXT_CAP_ID_VNDR extended capability is located at 0x100 */ 1108 if (!pci_find_ext_capability(adev->pdev, PCI_EXT_CAP_ID_VNDR)) 1109 dev_warn( 1110 adev->dev, 1111 "System can't access extended configuration space, please check!!\n"); 1112 1113 /* skip if the bios has already enabled large BAR */ 1114 if (adev->gmc.real_vram_size && 1115 (pci_resource_len(adev->pdev, 0) >= adev->gmc.real_vram_size)) 1116 return 0; 1117 1118 /* Check if the root BUS has 64bit memory resources */ 1119 root = adev->pdev->bus; 1120 while (root->parent) 1121 root = root->parent; 1122 1123 pci_bus_for_each_resource(root, res, i) { 1124 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) && 1125 res->start > 0x100000000ull) 1126 break; 1127 } 1128 1129 /* Trying to resize is pointless without a root hub window above 4GB */ 1130 if (!res) 1131 return 0; 1132 1133 /* Limit the BAR size to what is available */ 1134 max_size = pci_rebar_get_max_size(adev->pdev, 0); 1135 if (max_size < 0) 1136 return 0; 1137 rbar_size = min(max_size, rbar_size); 1138 1139 /* Disable memory decoding while we change the BAR addresses and size */ 1140 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd); 1141 pci_write_config_word(adev->pdev, PCI_COMMAND, 1142 cmd & ~PCI_COMMAND_MEMORY); 1143 1144 /* Tear down doorbell as resizing will release BARs */ 1145 amdgpu_doorbell_fini(adev); 1146 1147 r = pci_resize_resource(adev->pdev, 0, rbar_size, 1148 (adev->asic_type >= CHIP_BONAIRE) ? 1 << 5 1149 : 1 << 2); 1150 if (r == -ENOSPC) 1151 dev_info(adev->dev, 1152 "Not enough PCI address space for a large BAR."); 1153 else if (r && r != -ENOTSUPP) 1154 dev_err(adev->dev, "Problem resizing BAR0 (%d).", r); 1155 1156 /* When the doorbell or fb BAR isn't available we have no chance of 1157 * using the device. 1158 */ 1159 r = amdgpu_doorbell_init(adev); 1160 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET)) 1161 return -ENODEV; 1162 1163 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd); 1164 1165 return 0; 1166 } 1167 1168 /* 1169 * GPU helpers function. 1170 */ 1171 /** 1172 * amdgpu_device_need_post - check if the hw need post or not 1173 * 1174 * @adev: amdgpu_device pointer 1175 * 1176 * Check if the asic has been initialized (all asics) at driver startup 1177 * or post is needed if hw reset is performed. 1178 * Returns true if need or false if not. 1179 */ 1180 bool amdgpu_device_need_post(struct amdgpu_device *adev) 1181 { 1182 uint32_t reg, flags; 1183 1184 if (amdgpu_sriov_vf(adev)) 1185 return false; 1186 1187 flags = amdgpu_device_get_vbios_flags(adev); 1188 if (flags & AMDGPU_VBIOS_SKIP) 1189 return false; 1190 if ((flags & AMDGPU_VBIOS_OPTIONAL) && !adev->bios) 1191 return false; 1192 1193 if (amdgpu_passthrough(adev)) { 1194 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot 1195 * some old smc fw still need driver do vPost otherwise gpu hang, while 1196 * those smc fw version above 22.15 doesn't have this flaw, so we force 1197 * vpost executed for smc version below 22.15 1198 */ 1199 if (adev->asic_type == CHIP_FIJI) { 1200 int err; 1201 uint32_t fw_ver; 1202 1203 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev); 1204 /* force vPost if error occurred */ 1205 if (err) 1206 return true; 1207 1208 fw_ver = *((uint32_t *)adev->pm.fw->data + 69); 1209 release_firmware(adev->pm.fw); 1210 if (fw_ver < 0x00160e00) 1211 return true; 1212 } 1213 } 1214 1215 /* Don't post if we need to reset whole hive on init */ 1216 if (adev->init_lvl->level == AMDGPU_INIT_LEVEL_MINIMAL_XGMI) 1217 return false; 1218 1219 if (adev->has_hw_reset) { 1220 adev->has_hw_reset = false; 1221 return true; 1222 } 1223 1224 /* bios scratch used on CIK+ */ 1225 if (adev->asic_type >= CHIP_BONAIRE) 1226 return amdgpu_atombios_scratch_need_asic_init(adev); 1227 1228 /* check MEM_SIZE for older asics */ 1229 reg = amdgpu_asic_get_config_memsize(adev); 1230 1231 if ((reg != 0) && (reg != 0xffffffff)) 1232 return false; 1233 1234 return true; 1235 } 1236 1237 /* 1238 * Check whether seamless boot is supported. 1239 * 1240 * So far we only support seamless boot on DCE 3.0 or later. 1241 * If users report that it works on older ASICS as well, we may 1242 * loosen this. 1243 */ 1244 bool amdgpu_device_seamless_boot_supported(struct amdgpu_device *adev) 1245 { 1246 switch (amdgpu_seamless) { 1247 case -1: 1248 break; 1249 case 1: 1250 return true; 1251 case 0: 1252 return false; 1253 default: 1254 dev_err(adev->dev, "Invalid value for amdgpu.seamless: %d\n", 1255 amdgpu_seamless); 1256 return false; 1257 } 1258 1259 if (!(adev->flags & AMD_IS_APU)) 1260 return false; 1261 1262 if (adev->mman.keep_stolen_vga_memory) 1263 return false; 1264 1265 return amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(3, 0, 0); 1266 } 1267 1268 #if IS_ENABLED(CONFIG_X86) 1269 static const struct x86_cpu_id amdgpu_pcie_dynamic_switching_quirks[] = { 1270 /* 1271 * Intel hosts such as Rocket Lake, Alder Lake, Raptor Lake and Sapphire Rapids 1272 * don't support dynamic speed switching. Until we have confirmation from Intel 1273 * that a specific host supports it, it's safer that we keep it disabled for all. 1274 * 1275 * https://edc.intel.com/content/www/us/en/design/products/platforms/details/raptor-lake-s/13th-generation-core-processors-datasheet-volume-1-of-2/005/pci-express-support/ 1276 * https://gitlab.freedesktop.org/drm/amd/-/issues/2663 1277 */ 1278 X86_MATCH_VENDOR_FAM(INTEL, X86_FAMILY_ANY, NULL), 1279 /* 1280 * AMD Ryzen Pinnacle Ridge (Zen+, family 0x17 model 0x08) CPUs don't 1281 * support PCIe dynamic speed switching. 1282 * https://gitlab.freedesktop.org/drm/amd/-/work_items/5436 1283 */ 1284 X86_MATCH_VENDOR_FAM_MODEL(AMD, 0x17, 0x08, NULL), 1285 {} 1286 }; 1287 1288 static bool amdgpu_device_pcie_dynamic_switching_supported(struct amdgpu_device *adev) 1289 { 1290 /* eGPU change speeds based on USB4 fabric conditions */ 1291 if (dev_is_removable(adev->dev)) 1292 return true; 1293 1294 /* Hosts have problems with dynamic speed switching */ 1295 if (x86_match_cpu(amdgpu_pcie_dynamic_switching_quirks)) 1296 return false; 1297 1298 return true; 1299 } 1300 #else 1301 static inline bool amdgpu_device_pcie_dynamic_switching_supported(struct amdgpu_device *adev) 1302 { 1303 return true; 1304 } 1305 #endif 1306 1307 static bool amdgpu_device_aspm_support_quirk(struct amdgpu_device *adev) 1308 { 1309 /* Enabling ASPM causes randoms hangs on Tahiti and Oland on Zen4. 1310 * It's unclear if this is a platform-specific or GPU-specific issue. 1311 * Disable ASPM on SI for the time being. 1312 */ 1313 if (adev->family == AMDGPU_FAMILY_SI || 1314 (!(adev->pm.pp_feature & PP_PCIE_DPM_MASK) && adev->family == AMDGPU_FAMILY_VI)) 1315 return true; 1316 1317 #if IS_ENABLED(CONFIG_X86) 1318 struct cpuinfo_x86 *c = &cpu_data(0); 1319 1320 if (c->x86_vendor == X86_VENDOR_INTEL) { 1321 switch (c->x86_model) { 1322 case VFM_MODEL(INTEL_ALDERLAKE): 1323 case VFM_MODEL(INTEL_ALDERLAKE_L): 1324 case VFM_MODEL(INTEL_RAPTORLAKE): 1325 case VFM_MODEL(INTEL_RAPTORLAKE_P): 1326 case VFM_MODEL(INTEL_RAPTORLAKE_S): 1327 case VFM_MODEL(INTEL_TIGERLAKE): 1328 case VFM_MODEL(INTEL_TIGERLAKE_L): 1329 return true; 1330 default: 1331 return false; 1332 } 1333 } else { 1334 return false; 1335 } 1336 #else 1337 return false; 1338 #endif 1339 } 1340 1341 /** 1342 * amdgpu_device_should_use_aspm - check if the device should program ASPM 1343 * 1344 * @adev: amdgpu_device pointer 1345 * 1346 * Confirm whether the module parameter and pcie bridge agree that ASPM should 1347 * be set for this device. 1348 * 1349 * Returns true if it should be used or false if not. 1350 */ 1351 bool amdgpu_device_should_use_aspm(struct amdgpu_device *adev) 1352 { 1353 switch (amdgpu_aspm) { 1354 case -1: 1355 break; 1356 case 0: 1357 return false; 1358 case 1: 1359 return true; 1360 default: 1361 return false; 1362 } 1363 if (adev->flags & AMD_IS_APU) 1364 return false; 1365 if (amdgpu_device_aspm_support_quirk(adev)) 1366 return false; 1367 return pcie_aspm_enabled(adev->pdev); 1368 } 1369 1370 /* if we get transitioned to only one device, take VGA back */ 1371 /** 1372 * amdgpu_device_vga_set_decode - enable/disable vga decode 1373 * 1374 * @pdev: PCI device pointer 1375 * @state: enable/disable vga decode 1376 * 1377 * Enable/disable vga decode (all asics). 1378 * Returns VGA resource flags. 1379 */ 1380 static unsigned int amdgpu_device_vga_set_decode(struct pci_dev *pdev, 1381 bool state) 1382 { 1383 struct amdgpu_device *adev = drm_to_adev(pci_get_drvdata(pdev)); 1384 1385 amdgpu_asic_set_vga_state(adev, state); 1386 if (state) 1387 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM | 1388 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; 1389 else 1390 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; 1391 } 1392 1393 /** 1394 * amdgpu_device_check_block_size - validate the vm block size 1395 * 1396 * @adev: amdgpu_device pointer 1397 * 1398 * Validates the vm block size specified via module parameter. 1399 * The vm block size defines number of bits in page table versus page directory, 1400 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the 1401 * page table and the remaining bits are in the page directory. 1402 */ 1403 static void amdgpu_device_check_block_size(struct amdgpu_device *adev) 1404 { 1405 /* defines number of bits in page table versus page directory, 1406 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the 1407 * page table and the remaining bits are in the page directory 1408 */ 1409 if (amdgpu_vm_block_size == -1) 1410 return; 1411 1412 if (amdgpu_vm_block_size < 9) { 1413 dev_warn(adev->dev, "VM page table size (%d) too small\n", 1414 amdgpu_vm_block_size); 1415 amdgpu_vm_block_size = -1; 1416 } 1417 } 1418 1419 /** 1420 * amdgpu_device_check_vm_size - validate the vm size 1421 * 1422 * @adev: amdgpu_device pointer 1423 * 1424 * Validates the vm size in GB specified via module parameter. 1425 * The VM size is the size of the GPU virtual memory space in GB. 1426 */ 1427 static void amdgpu_device_check_vm_size(struct amdgpu_device *adev) 1428 { 1429 /* no need to check the default value */ 1430 if (amdgpu_vm_size == -1) 1431 return; 1432 1433 if (amdgpu_vm_size < 1) { 1434 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n", 1435 amdgpu_vm_size); 1436 amdgpu_vm_size = -1; 1437 } 1438 } 1439 1440 static void amdgpu_device_check_smu_prv_buffer_size(struct amdgpu_device *adev) 1441 { 1442 struct sysinfo si; 1443 bool is_os_64 = (sizeof(void *) == 8); 1444 uint64_t total_memory; 1445 uint64_t dram_size_seven_GB = 0x1B8000000; 1446 uint64_t dram_size_three_GB = 0xB8000000; 1447 1448 if (amdgpu_smu_memory_pool_size == 0) 1449 return; 1450 1451 if (!is_os_64) { 1452 dev_warn(adev->dev, "Not 64-bit OS, feature not supported\n"); 1453 goto def_value; 1454 } 1455 si_meminfo(&si); 1456 total_memory = (uint64_t)si.totalram * si.mem_unit; 1457 1458 if ((amdgpu_smu_memory_pool_size == 1) || 1459 (amdgpu_smu_memory_pool_size == 2)) { 1460 if (total_memory < dram_size_three_GB) 1461 goto def_value1; 1462 } else if ((amdgpu_smu_memory_pool_size == 4) || 1463 (amdgpu_smu_memory_pool_size == 8)) { 1464 if (total_memory < dram_size_seven_GB) 1465 goto def_value1; 1466 } else { 1467 dev_warn(adev->dev, "Smu memory pool size not supported\n"); 1468 goto def_value; 1469 } 1470 adev->pm.smu_prv_buffer_size = amdgpu_smu_memory_pool_size << 28; 1471 1472 return; 1473 1474 def_value1: 1475 dev_warn(adev->dev, "No enough system memory\n"); 1476 def_value: 1477 adev->pm.smu_prv_buffer_size = 0; 1478 } 1479 1480 static int amdgpu_device_init_apu_flags(struct amdgpu_device *adev) 1481 { 1482 if (!(adev->flags & AMD_IS_APU) || 1483 adev->asic_type < CHIP_RAVEN) 1484 return 0; 1485 1486 switch (adev->asic_type) { 1487 case CHIP_RAVEN: 1488 if (adev->pdev->device == 0x15dd) 1489 adev->apu_flags |= AMD_APU_IS_RAVEN; 1490 if (adev->pdev->device == 0x15d8) 1491 adev->apu_flags |= AMD_APU_IS_PICASSO; 1492 break; 1493 case CHIP_RENOIR: 1494 if ((adev->pdev->device == 0x1636) || 1495 (adev->pdev->device == 0x164c)) 1496 adev->apu_flags |= AMD_APU_IS_RENOIR; 1497 else 1498 adev->apu_flags |= AMD_APU_IS_GREEN_SARDINE; 1499 break; 1500 case CHIP_VANGOGH: 1501 adev->apu_flags |= AMD_APU_IS_VANGOGH; 1502 break; 1503 case CHIP_YELLOW_CARP: 1504 break; 1505 case CHIP_CYAN_SKILLFISH: 1506 if ((adev->pdev->device == 0x13FE) || 1507 (adev->pdev->device == 0x143F)) 1508 adev->apu_flags |= AMD_APU_IS_CYAN_SKILLFISH2; 1509 break; 1510 default: 1511 break; 1512 } 1513 1514 return 0; 1515 } 1516 1517 /** 1518 * amdgpu_device_check_arguments - validate module params 1519 * 1520 * @adev: amdgpu_device pointer 1521 * 1522 * Validates certain module parameters and updates 1523 * the associated values used by the driver (all asics). 1524 */ 1525 static int amdgpu_device_check_arguments(struct amdgpu_device *adev) 1526 { 1527 int i; 1528 1529 if (amdgpu_sched_jobs < 4) { 1530 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n", 1531 amdgpu_sched_jobs); 1532 amdgpu_sched_jobs = 4; 1533 } else if (!is_power_of_2(amdgpu_sched_jobs)) { 1534 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n", 1535 amdgpu_sched_jobs); 1536 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs); 1537 } 1538 1539 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) { 1540 /* gart size must be greater or equal to 32M */ 1541 dev_warn(adev->dev, "gart size (%d) too small\n", 1542 amdgpu_gart_size); 1543 amdgpu_gart_size = -1; 1544 } 1545 1546 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) { 1547 /* gtt size must be greater or equal to 32M */ 1548 dev_warn(adev->dev, "gtt size (%d) too small\n", 1549 amdgpu_gtt_size); 1550 amdgpu_gtt_size = -1; 1551 } 1552 1553 /* valid range is between 4 and 9 inclusive */ 1554 if (amdgpu_vm_fragment_size != -1 && 1555 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) { 1556 dev_warn(adev->dev, "valid range is between 4 and 9\n"); 1557 amdgpu_vm_fragment_size = -1; 1558 } 1559 1560 if (amdgpu_sched_hw_submission < 2) { 1561 dev_warn(adev->dev, "sched hw submission jobs (%d) must be at least 2\n", 1562 amdgpu_sched_hw_submission); 1563 amdgpu_sched_hw_submission = 2; 1564 } else if (!is_power_of_2(amdgpu_sched_hw_submission)) { 1565 dev_warn(adev->dev, "sched hw submission jobs (%d) must be a power of 2\n", 1566 amdgpu_sched_hw_submission); 1567 amdgpu_sched_hw_submission = roundup_pow_of_two(amdgpu_sched_hw_submission); 1568 } 1569 1570 if (amdgpu_reset_method < -1 || amdgpu_reset_method > 4) { 1571 dev_warn(adev->dev, "invalid option for reset method, reverting to default\n"); 1572 amdgpu_reset_method = -1; 1573 } 1574 1575 amdgpu_device_check_smu_prv_buffer_size(adev); 1576 1577 amdgpu_device_check_vm_size(adev); 1578 1579 amdgpu_device_check_block_size(adev); 1580 1581 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type); 1582 1583 for (i = 0; i < MAX_XCP; i++) { 1584 switch (amdgpu_enforce_isolation) { 1585 case -1: 1586 case 0: 1587 default: 1588 /* disable */ 1589 adev->enforce_isolation[i] = AMDGPU_ENFORCE_ISOLATION_DISABLE; 1590 break; 1591 case 1: 1592 /* enable */ 1593 adev->enforce_isolation[i] = 1594 AMDGPU_ENFORCE_ISOLATION_ENABLE; 1595 break; 1596 case 2: 1597 /* enable legacy mode */ 1598 adev->enforce_isolation[i] = 1599 AMDGPU_ENFORCE_ISOLATION_ENABLE_LEGACY; 1600 break; 1601 case 3: 1602 /* enable only process isolation without submitting cleaner shader */ 1603 adev->enforce_isolation[i] = 1604 AMDGPU_ENFORCE_ISOLATION_NO_CLEANER_SHADER; 1605 break; 1606 } 1607 } 1608 1609 return 0; 1610 } 1611 1612 /** 1613 * amdgpu_switcheroo_set_state - set switcheroo state 1614 * 1615 * @pdev: pci dev pointer 1616 * @state: vga_switcheroo state 1617 * 1618 * Callback for the switcheroo driver. Suspends or resumes 1619 * the asics before or after it is powered up using ACPI methods. 1620 */ 1621 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, 1622 enum vga_switcheroo_state state) 1623 { 1624 struct drm_device *dev = pci_get_drvdata(pdev); 1625 int r; 1626 1627 if (amdgpu_device_supports_px(drm_to_adev(dev)) && 1628 state == VGA_SWITCHEROO_OFF) 1629 return; 1630 1631 if (state == VGA_SWITCHEROO_ON) { 1632 pr_info("switched on\n"); 1633 /* don't suspend or resume card normally */ 1634 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING; 1635 1636 pci_set_power_state(pdev, PCI_D0); 1637 amdgpu_device_load_pci_state(pdev); 1638 r = pci_enable_device(pdev); 1639 if (r) 1640 dev_warn(&pdev->dev, "pci_enable_device failed (%d)\n", 1641 r); 1642 amdgpu_device_resume(dev, true); 1643 1644 dev->switch_power_state = DRM_SWITCH_POWER_ON; 1645 } else { 1646 dev_info(&pdev->dev, "switched off\n"); 1647 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING; 1648 amdgpu_device_prepare(dev); 1649 amdgpu_device_suspend(dev, true); 1650 amdgpu_device_cache_pci_state(pdev); 1651 /* Shut down the device */ 1652 pci_disable_device(pdev); 1653 pci_set_power_state(pdev, PCI_D3cold); 1654 dev->switch_power_state = DRM_SWITCH_POWER_OFF; 1655 } 1656 } 1657 1658 /** 1659 * amdgpu_switcheroo_can_switch - see if switcheroo state can change 1660 * 1661 * @pdev: pci dev pointer 1662 * 1663 * Callback for the switcheroo driver. Check of the switcheroo 1664 * state can be changed. 1665 * Returns true if the state can be changed, false if not. 1666 */ 1667 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev) 1668 { 1669 struct drm_device *dev = pci_get_drvdata(pdev); 1670 1671 /* 1672 * FIXME: open_count is protected by drm_global_mutex but that would lead to 1673 * locking inversion with the driver load path. And the access here is 1674 * completely racy anyway. So don't bother with locking for now. 1675 */ 1676 return atomic_read(&dev->open_count) == 0; 1677 } 1678 1679 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = { 1680 .set_gpu_state = amdgpu_switcheroo_set_state, 1681 .reprobe = NULL, 1682 .can_switch = amdgpu_switcheroo_can_switch, 1683 }; 1684 1685 /** 1686 * amdgpu_device_enable_virtual_display - enable virtual display feature 1687 * 1688 * @adev: amdgpu_device pointer 1689 * 1690 * Enabled the virtual display feature if the user has enabled it via 1691 * the module parameter virtual_display. This feature provides a virtual 1692 * display hardware on headless boards or in virtualized environments. 1693 * This function parses and validates the configuration string specified by 1694 * the user and configures the virtual display configuration (number of 1695 * virtual connectors, crtcs, etc.) specified. 1696 */ 1697 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev) 1698 { 1699 adev->enable_virtual_display = false; 1700 1701 if (amdgpu_virtual_display) { 1702 const char *pci_address_name = pci_name(adev->pdev); 1703 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname; 1704 1705 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL); 1706 pciaddstr_tmp = pciaddstr; 1707 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) { 1708 pciaddname = strsep(&pciaddname_tmp, ","); 1709 if (!strcmp("all", pciaddname) 1710 || !strcmp(pci_address_name, pciaddname)) { 1711 long num_crtc; 1712 int res = -1; 1713 1714 adev->enable_virtual_display = true; 1715 1716 if (pciaddname_tmp) 1717 res = kstrtol(pciaddname_tmp, 10, 1718 &num_crtc); 1719 1720 if (!res) { 1721 if (num_crtc < 1) 1722 num_crtc = 1; 1723 if (num_crtc > 6) 1724 num_crtc = 6; 1725 adev->mode_info.num_crtc = num_crtc; 1726 } else { 1727 adev->mode_info.num_crtc = 1; 1728 } 1729 break; 1730 } 1731 } 1732 1733 dev_info( 1734 adev->dev, 1735 "virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n", 1736 amdgpu_virtual_display, pci_address_name, 1737 adev->enable_virtual_display, adev->mode_info.num_crtc); 1738 1739 kfree(pciaddstr); 1740 } 1741 } 1742 1743 void amdgpu_device_set_sriov_virtual_display(struct amdgpu_device *adev) 1744 { 1745 if (amdgpu_sriov_vf(adev) && !adev->enable_virtual_display) { 1746 adev->mode_info.num_crtc = 1; 1747 adev->enable_virtual_display = true; 1748 dev_info(adev->dev, "virtual_display:%d, num_crtc:%d\n", 1749 adev->enable_virtual_display, 1750 adev->mode_info.num_crtc); 1751 } 1752 } 1753 1754 /** 1755 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware 1756 * 1757 * @adev: amdgpu_device pointer 1758 * 1759 * Parses the asic configuration parameters specified in the gpu info 1760 * firmware and makes them available to the driver for use in configuring 1761 * the asic. 1762 * Returns 0 on success, -EINVAL on failure. 1763 */ 1764 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev) 1765 { 1766 const char *chip_name; 1767 int err; 1768 const struct gpu_info_firmware_header_v1_0 *hdr; 1769 1770 adev->firmware.gpu_info_fw = NULL; 1771 1772 switch (adev->asic_type) { 1773 default: 1774 return 0; 1775 case CHIP_VEGA10: 1776 chip_name = "vega10"; 1777 break; 1778 case CHIP_VEGA12: 1779 chip_name = "vega12"; 1780 break; 1781 case CHIP_RAVEN: 1782 if (adev->apu_flags & AMD_APU_IS_RAVEN2) 1783 chip_name = "raven2"; 1784 else if (adev->apu_flags & AMD_APU_IS_PICASSO) 1785 chip_name = "picasso"; 1786 else 1787 chip_name = "raven"; 1788 break; 1789 case CHIP_ARCTURUS: 1790 chip_name = "arcturus"; 1791 break; 1792 case CHIP_NAVI12: 1793 if (adev->discovery.bin) 1794 return 0; 1795 chip_name = "navi12"; 1796 break; 1797 case CHIP_CYAN_SKILLFISH: 1798 if (adev->discovery.bin) 1799 return 0; 1800 chip_name = "cyan_skillfish"; 1801 break; 1802 } 1803 1804 err = amdgpu_ucode_request(adev, &adev->firmware.gpu_info_fw, 1805 AMDGPU_UCODE_OPTIONAL, 1806 "amdgpu/%s_gpu_info.bin", chip_name); 1807 if (err) { 1808 dev_err(adev->dev, 1809 "Failed to get gpu_info firmware \"%s_gpu_info.bin\"\n", 1810 chip_name); 1811 goto out; 1812 } 1813 1814 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data; 1815 amdgpu_ucode_print_gpu_info_hdr(&hdr->header); 1816 1817 switch (hdr->version_major) { 1818 case 1: 1819 { 1820 const struct gpu_info_firmware_v1_0 *gpu_info_fw = 1821 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data + 1822 le32_to_cpu(hdr->header.ucode_array_offset_bytes)); 1823 1824 /* 1825 * Should be dropped when DAL no longer needs it. 1826 */ 1827 if (adev->asic_type == CHIP_NAVI12) 1828 goto parse_soc_bounding_box; 1829 1830 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se); 1831 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh); 1832 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se); 1833 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se); 1834 adev->gfx.config.max_texture_channel_caches = 1835 le32_to_cpu(gpu_info_fw->gc_num_tccs); 1836 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs); 1837 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds); 1838 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth); 1839 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth); 1840 adev->gfx.config.double_offchip_lds_buf = 1841 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer); 1842 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size); 1843 adev->gfx.cu_info.max_waves_per_simd = 1844 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd); 1845 adev->gfx.cu_info.max_scratch_slots_per_cu = 1846 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu); 1847 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size); 1848 if (hdr->version_minor >= 1) { 1849 const struct gpu_info_firmware_v1_1 *gpu_info_fw = 1850 (const struct gpu_info_firmware_v1_1 *)(adev->firmware.gpu_info_fw->data + 1851 le32_to_cpu(hdr->header.ucode_array_offset_bytes)); 1852 adev->gfx.config.num_sc_per_sh = 1853 le32_to_cpu(gpu_info_fw->num_sc_per_sh); 1854 adev->gfx.config.num_packer_per_sc = 1855 le32_to_cpu(gpu_info_fw->num_packer_per_sc); 1856 } 1857 1858 parse_soc_bounding_box: 1859 /* 1860 * soc bounding box info is not integrated in disocovery table, 1861 * we always need to parse it from gpu info firmware if needed. 1862 */ 1863 if (hdr->version_minor == 2) { 1864 const struct gpu_info_firmware_v1_2 *gpu_info_fw = 1865 (const struct gpu_info_firmware_v1_2 *)(adev->firmware.gpu_info_fw->data + 1866 le32_to_cpu(hdr->header.ucode_array_offset_bytes)); 1867 adev->dm.soc_bounding_box = &gpu_info_fw->soc_bounding_box; 1868 } 1869 break; 1870 } 1871 default: 1872 dev_err(adev->dev, 1873 "Unsupported gpu_info table %d\n", hdr->header.ucode_version); 1874 err = -EINVAL; 1875 goto out; 1876 } 1877 out: 1878 return err; 1879 } 1880 1881 static void amdgpu_uid_init(struct amdgpu_device *adev) 1882 { 1883 /* Initialize the UID for the device */ 1884 adev->uid_info = kzalloc_obj(struct amdgpu_uid); 1885 if (!adev->uid_info) { 1886 dev_warn(adev->dev, "Failed to allocate memory for UID\n"); 1887 return; 1888 } 1889 adev->uid_info->adev = adev; 1890 } 1891 1892 static void amdgpu_uid_fini(struct amdgpu_device *adev) 1893 { 1894 /* Free the UID memory */ 1895 kfree(adev->uid_info); 1896 adev->uid_info = NULL; 1897 } 1898 1899 static struct pci_dev *amdgpu_device_find_parent(struct amdgpu_device *adev) 1900 { 1901 struct pci_dev *parent = adev->pdev; 1902 1903 /* skip upstream/downstream switches internal to dGPU */ 1904 while ((parent = pci_upstream_bridge(parent))) { 1905 if (parent->vendor == PCI_VENDOR_ID_ATI) 1906 continue; 1907 break; 1908 } 1909 1910 return parent; 1911 } 1912 1913 /** 1914 * amdgpu_device_ip_early_init - run early init for hardware IPs 1915 * 1916 * @adev: amdgpu_device pointer 1917 * 1918 * Early initialization pass for hardware IPs. The hardware IPs that make 1919 * up each asic are discovered each IP's early_init callback is run. This 1920 * is the first stage in initializing the asic. 1921 * Returns 0 on success, negative error code on failure. 1922 */ 1923 static int amdgpu_device_ip_early_init(struct amdgpu_device *adev) 1924 { 1925 struct amdgpu_ip_block *ip_block; 1926 struct pci_dev *parent; 1927 bool total, skip_bios, early_full_gpu_access = false; 1928 uint32_t bios_flags; 1929 int i, r; 1930 1931 amdgpu_device_enable_virtual_display(adev); 1932 1933 if (amdgpu_sriov_vf(adev)) { 1934 /* 1935 * Legacy hosts do not provide init data before early init, so 1936 * keep the original early full GPU access request for them. Newer 1937 * hosts publish the init data through VF FB, which lets us defer 1938 * full GPU access until after non-GPU early init work is done. 1939 */ 1940 early_full_gpu_access = (adev->virt.req_init_data_ver == 0); 1941 if (early_full_gpu_access) { 1942 r = amdgpu_virt_request_full_gpu(adev, true); 1943 if (r) 1944 return r; 1945 } 1946 1947 r = amdgpu_virt_init_critical_region(adev); 1948 if (r) 1949 return r; 1950 } 1951 1952 switch (adev->asic_type) { 1953 #ifdef CONFIG_DRM_AMDGPU_SI 1954 case CHIP_VERDE: 1955 case CHIP_TAHITI: 1956 case CHIP_PITCAIRN: 1957 case CHIP_OLAND: 1958 case CHIP_HAINAN: 1959 adev->family = AMDGPU_FAMILY_SI; 1960 r = si_set_ip_blocks(adev); 1961 if (r) 1962 return r; 1963 break; 1964 #endif 1965 #ifdef CONFIG_DRM_AMDGPU_CIK 1966 case CHIP_BONAIRE: 1967 case CHIP_HAWAII: 1968 case CHIP_KAVERI: 1969 case CHIP_KABINI: 1970 case CHIP_MULLINS: 1971 if (adev->flags & AMD_IS_APU) 1972 adev->family = AMDGPU_FAMILY_KV; 1973 else 1974 adev->family = AMDGPU_FAMILY_CI; 1975 1976 r = cik_set_ip_blocks(adev); 1977 if (r) 1978 return r; 1979 break; 1980 #endif 1981 case CHIP_TOPAZ: 1982 case CHIP_TONGA: 1983 case CHIP_FIJI: 1984 case CHIP_POLARIS10: 1985 case CHIP_POLARIS11: 1986 case CHIP_POLARIS12: 1987 case CHIP_VEGAM: 1988 case CHIP_CARRIZO: 1989 case CHIP_STONEY: 1990 if (adev->flags & AMD_IS_APU) 1991 adev->family = AMDGPU_FAMILY_CZ; 1992 else 1993 adev->family = AMDGPU_FAMILY_VI; 1994 1995 r = vi_set_ip_blocks(adev); 1996 if (r) 1997 return r; 1998 break; 1999 default: 2000 r = amdgpu_discovery_set_ip_blocks(adev); 2001 if (r) { 2002 adev->num_ip_blocks = 0; 2003 return r; 2004 } 2005 break; 2006 } 2007 2008 /* Check for IP version 9.4.3 with A0 hardware */ 2009 if (amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 3) && 2010 !amdgpu_device_get_rev_id(adev)) { 2011 dev_err(adev->dev, "Unsupported A0 hardware\n"); 2012 return -ENODEV; /* device unsupported - no device error */ 2013 } 2014 2015 if (amdgpu_has_atpx() && 2016 (amdgpu_is_atpx_hybrid() || 2017 amdgpu_has_atpx_dgpu_power_cntl()) && 2018 ((adev->flags & AMD_IS_APU) == 0) && 2019 !dev_is_removable(&adev->pdev->dev)) 2020 adev->flags |= AMD_IS_PX; 2021 2022 if (!(adev->flags & AMD_IS_APU)) { 2023 parent = amdgpu_device_find_parent(adev); 2024 adev->has_pr3 = parent ? pci_pr3_present(parent) : false; 2025 } 2026 2027 adev->pm.pp_feature = amdgpu_pp_feature_mask; 2028 if (amdgpu_sriov_vf(adev) || sched_policy == KFD_SCHED_POLICY_NO_HWS) 2029 adev->pm.pp_feature &= ~PP_GFXOFF_MASK; 2030 if (amdgpu_sriov_vf(adev) && adev->asic_type == CHIP_SIENNA_CICHLID) 2031 adev->pm.pp_feature &= ~PP_OVERDRIVE_MASK; 2032 if (!amdgpu_device_pcie_dynamic_switching_supported(adev)) 2033 adev->pm.pp_feature &= ~PP_PCIE_DPM_MASK; 2034 2035 adev->virt.is_xgmi_node_migrate_enabled = false; 2036 if (amdgpu_sriov_vf(adev)) { 2037 adev->virt.is_xgmi_node_migrate_enabled = 2038 amdgpu_ip_version((adev), GC_HWIP, 0) == IP_VERSION(9, 4, 4); 2039 } 2040 2041 total = true; 2042 for (i = 0; i < adev->num_ip_blocks; i++) { 2043 ip_block = &adev->ip_blocks[i]; 2044 2045 if ((amdgpu_ip_block_mask & (1 << i)) == 0) { 2046 dev_warn(adev->dev, "disabled ip block: %d <%s>\n", i, 2047 adev->ip_blocks[i].version->funcs->name); 2048 adev->ip_blocks[i].status.valid = false; 2049 } else if (ip_block->version->funcs->early_init) { 2050 r = ip_block->version->funcs->early_init(ip_block); 2051 if (r == -ENOENT) { 2052 adev->ip_blocks[i].status.valid = false; 2053 } else if (r) { 2054 dev_err(adev->dev, 2055 "early_init of IP block <%s> failed %d\n", 2056 adev->ip_blocks[i].version->funcs->name, 2057 r); 2058 total = false; 2059 } else { 2060 adev->ip_blocks[i].status.valid = true; 2061 } 2062 } else { 2063 adev->ip_blocks[i].status.valid = true; 2064 } 2065 /* get the vbios after the asic_funcs are set up */ 2066 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) { 2067 r = amdgpu_device_parse_gpu_info_fw(adev); 2068 if (r) 2069 return r; 2070 2071 bios_flags = amdgpu_device_get_vbios_flags(adev); 2072 skip_bios = !!(bios_flags & AMDGPU_VBIOS_SKIP); 2073 /* Read BIOS */ 2074 if (!skip_bios) { 2075 bool optional = 2076 !!(bios_flags & AMDGPU_VBIOS_OPTIONAL); 2077 if (!amdgpu_get_bios(adev) && !optional) 2078 return -EINVAL; 2079 2080 if (optional && !adev->bios) 2081 dev_info( 2082 adev->dev, 2083 "VBIOS image optional, proceeding without VBIOS image"); 2084 2085 if (adev->bios) { 2086 r = amdgpu_atombios_init(adev); 2087 if (r) { 2088 dev_err(adev->dev, 2089 "amdgpu_atombios_init failed\n"); 2090 amdgpu_vf_error_put( 2091 adev, 2092 AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 2093 0, 0); 2094 return r; 2095 } 2096 } 2097 } 2098 2099 /*get pf2vf msg info at it's earliest time*/ 2100 if (amdgpu_sriov_vf(adev)) 2101 amdgpu_virt_init_data_exchange(adev); 2102 2103 } 2104 } 2105 if (!total) 2106 return -ENODEV; 2107 2108 /* Request full GPU access only for the remaining SR-IOV init work. */ 2109 if (amdgpu_sriov_vf(adev) && !early_full_gpu_access) { 2110 r = amdgpu_virt_request_full_gpu(adev, true); 2111 if (r) 2112 return r; 2113 } 2114 2115 if (adev->gmc.xgmi.supported) 2116 amdgpu_xgmi_early_init(adev); 2117 2118 if (amdgpu_is_multi_aid(adev)) 2119 amdgpu_uid_init(adev); 2120 ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX); 2121 if (ip_block->status.valid != false) 2122 amdgpu_amdkfd_device_probe(adev); 2123 2124 adev->cg_flags &= amdgpu_cg_mask; 2125 adev->pg_flags &= amdgpu_pg_mask; 2126 2127 amdgpu_smu_early_init(adev); 2128 2129 return 0; 2130 } 2131 2132 static int amdgpu_device_ip_hw_init_phase1(struct amdgpu_device *adev) 2133 { 2134 int i, r; 2135 2136 for (i = 0; i < adev->num_ip_blocks; i++) { 2137 if (!adev->ip_blocks[i].status.sw) 2138 continue; 2139 if (adev->ip_blocks[i].status.hw) 2140 continue; 2141 if (!amdgpu_ip_member_of_hwini( 2142 adev, adev->ip_blocks[i].version->type)) 2143 continue; 2144 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON || 2145 (amdgpu_sriov_vf(adev) && (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)) || 2146 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) { 2147 r = adev->ip_blocks[i].version->funcs->hw_init(&adev->ip_blocks[i]); 2148 if (r) { 2149 dev_err(adev->dev, 2150 "hw_init of IP block <%s> failed %d\n", 2151 adev->ip_blocks[i].version->funcs->name, 2152 r); 2153 return r; 2154 } 2155 adev->ip_blocks[i].status.hw = true; 2156 } 2157 } 2158 2159 return 0; 2160 } 2161 2162 static int amdgpu_device_ip_hw_init_phase2(struct amdgpu_device *adev) 2163 { 2164 int i, r; 2165 2166 for (i = 0; i < adev->num_ip_blocks; i++) { 2167 if (!adev->ip_blocks[i].status.sw) 2168 continue; 2169 if (adev->ip_blocks[i].status.hw) 2170 continue; 2171 if (!amdgpu_ip_member_of_hwini( 2172 adev, adev->ip_blocks[i].version->type)) 2173 continue; 2174 r = adev->ip_blocks[i].version->funcs->hw_init(&adev->ip_blocks[i]); 2175 if (r) { 2176 dev_err(adev->dev, 2177 "hw_init of IP block <%s> failed %d\n", 2178 adev->ip_blocks[i].version->funcs->name, r); 2179 return r; 2180 } 2181 adev->ip_blocks[i].status.hw = true; 2182 } 2183 2184 return 0; 2185 } 2186 2187 static int amdgpu_device_fw_loading(struct amdgpu_device *adev) 2188 { 2189 int r = 0; 2190 int i; 2191 uint32_t smu_version; 2192 2193 if (adev->asic_type >= CHIP_VEGA10) { 2194 for (i = 0; i < adev->num_ip_blocks; i++) { 2195 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_PSP) 2196 continue; 2197 2198 if (!amdgpu_ip_member_of_hwini(adev, 2199 AMD_IP_BLOCK_TYPE_PSP)) 2200 break; 2201 2202 if (!adev->ip_blocks[i].status.sw) 2203 continue; 2204 2205 /* no need to do the fw loading again if already done*/ 2206 if (adev->ip_blocks[i].status.hw == true) 2207 break; 2208 2209 if (amdgpu_in_reset(adev) || adev->in_suspend) { 2210 r = amdgpu_ip_block_resume(&adev->ip_blocks[i]); 2211 if (r) 2212 return r; 2213 } else { 2214 r = adev->ip_blocks[i].version->funcs->hw_init(&adev->ip_blocks[i]); 2215 if (r) { 2216 dev_err(adev->dev, 2217 "hw_init of IP block <%s> failed %d\n", 2218 adev->ip_blocks[i] 2219 .version->funcs->name, 2220 r); 2221 return r; 2222 } 2223 adev->ip_blocks[i].status.hw = true; 2224 } 2225 break; 2226 } 2227 } 2228 2229 if (!amdgpu_sriov_vf(adev) || adev->asic_type == CHIP_TONGA) 2230 r = amdgpu_pm_load_smu_firmware(adev, &smu_version); 2231 2232 return r; 2233 } 2234 2235 static int amdgpu_device_init_schedulers(struct amdgpu_device *adev) 2236 { 2237 struct drm_sched_init_args args = { 2238 .ops = &amdgpu_sched_ops, 2239 .timeout_wq = adev->reset_domain->wq, 2240 .dev = adev->dev, 2241 }; 2242 long timeout; 2243 int r, i; 2244 2245 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 2246 struct amdgpu_ring *ring = adev->rings[i]; 2247 2248 /* No need to setup the GPU scheduler for rings that don't need it */ 2249 if (!ring || ring->no_scheduler) 2250 continue; 2251 2252 switch (ring->funcs->type) { 2253 case AMDGPU_RING_TYPE_GFX: 2254 timeout = adev->gfx_timeout; 2255 break; 2256 case AMDGPU_RING_TYPE_COMPUTE: 2257 timeout = adev->compute_timeout; 2258 break; 2259 case AMDGPU_RING_TYPE_SDMA: 2260 timeout = adev->sdma_timeout; 2261 break; 2262 default: 2263 timeout = adev->video_timeout; 2264 break; 2265 } 2266 2267 args.timeout = timeout; 2268 args.credit_limit = ring->num_hw_submission; 2269 args.score = ring->sched_score; 2270 args.name = ring->name; 2271 2272 r = drm_sched_init(&ring->sched, &args); 2273 if (r) { 2274 dev_err(adev->dev, 2275 "Failed to create scheduler on ring %s.\n", 2276 ring->name); 2277 return r; 2278 } 2279 r = amdgpu_uvd_entity_init(adev, ring); 2280 if (r) { 2281 dev_err(adev->dev, 2282 "Failed to create UVD scheduling entity on ring %s.\n", 2283 ring->name); 2284 return r; 2285 } 2286 r = amdgpu_vce_entity_init(adev, ring); 2287 if (r) { 2288 dev_err(adev->dev, 2289 "Failed to create VCE scheduling entity on ring %s.\n", 2290 ring->name); 2291 return r; 2292 } 2293 } 2294 2295 if (adev->xcp_mgr) 2296 amdgpu_xcp_update_partition_sched_list(adev); 2297 2298 return 0; 2299 } 2300 2301 2302 /** 2303 * amdgpu_device_ip_init - run init for hardware IPs 2304 * 2305 * @adev: amdgpu_device pointer 2306 * 2307 * Main initialization pass for hardware IPs. The list of all the hardware 2308 * IPs that make up the asic is walked and the sw_init and hw_init callbacks 2309 * are run. sw_init initializes the software state associated with each IP 2310 * and hw_init initializes the hardware associated with each IP. 2311 * Returns 0 on success, negative error code on failure. 2312 */ 2313 static int amdgpu_device_ip_init(struct amdgpu_device *adev) 2314 { 2315 bool init_badpage; 2316 int i, r; 2317 2318 r = amdgpu_ras_init(adev); 2319 if (r) 2320 return r; 2321 2322 for (i = 0; i < adev->num_ip_blocks; i++) { 2323 if (!adev->ip_blocks[i].status.valid) 2324 continue; 2325 if (adev->ip_blocks[i].version->funcs->sw_init) { 2326 r = adev->ip_blocks[i].version->funcs->sw_init(&adev->ip_blocks[i]); 2327 if (r) { 2328 dev_err(adev->dev, 2329 "sw_init of IP block <%s> failed %d\n", 2330 adev->ip_blocks[i].version->funcs->name, 2331 r); 2332 goto init_failed; 2333 } 2334 } 2335 adev->ip_blocks[i].status.sw = true; 2336 2337 if (!amdgpu_ip_member_of_hwini( 2338 adev, adev->ip_blocks[i].version->type)) 2339 continue; 2340 2341 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) { 2342 /* need to do common hw init early so everything is set up for gmc */ 2343 r = adev->ip_blocks[i].version->funcs->hw_init(&adev->ip_blocks[i]); 2344 if (r) { 2345 dev_err(adev->dev, "hw_init %d failed %d\n", i, 2346 r); 2347 goto init_failed; 2348 } 2349 adev->ip_blocks[i].status.hw = true; 2350 } else if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) { 2351 /* need to do gmc hw init early so we can allocate gpu mem */ 2352 /* Try to reserve bad pages early */ 2353 if (amdgpu_sriov_vf(adev)) 2354 amdgpu_virt_exchange_data(adev); 2355 2356 r = amdgpu_device_mem_scratch_init(adev); 2357 if (r) { 2358 dev_err(adev->dev, 2359 "amdgpu_mem_scratch_init failed %d\n", 2360 r); 2361 goto init_failed; 2362 } 2363 r = adev->ip_blocks[i].version->funcs->hw_init(&adev->ip_blocks[i]); 2364 if (r) { 2365 dev_err(adev->dev, "hw_init %d failed %d\n", i, 2366 r); 2367 goto init_failed; 2368 } 2369 r = amdgpu_wb_init(adev); 2370 if (r) { 2371 dev_err(adev->dev, 2372 "amdgpu_wb_init failed %d\n", r); 2373 goto init_failed; 2374 } 2375 adev->ip_blocks[i].status.hw = true; 2376 2377 /* right after GMC hw init, we create CSA */ 2378 if (adev->gfx.mcbp) { 2379 r = amdgpu_allocate_static_csa(adev, &adev->virt.csa_obj, 2380 AMDGPU_GEM_DOMAIN_VRAM | 2381 AMDGPU_GEM_DOMAIN_GTT, 2382 AMDGPU_CSA_SIZE); 2383 if (r) { 2384 dev_err(adev->dev, 2385 "allocate CSA failed %d\n", r); 2386 goto init_failed; 2387 } 2388 } 2389 2390 r = amdgpu_seq64_init(adev); 2391 if (r) { 2392 dev_err(adev->dev, "allocate seq64 failed %d\n", 2393 r); 2394 goto init_failed; 2395 } 2396 } 2397 } 2398 2399 if (amdgpu_sriov_vf(adev)) 2400 amdgpu_virt_init_data_exchange(adev); 2401 2402 r = amdgpu_ib_pool_init(adev); 2403 if (r) { 2404 dev_err(adev->dev, "IB initialization failed (%d).\n", r); 2405 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r); 2406 goto init_failed; 2407 } 2408 2409 r = amdgpu_ucode_create_bo(adev); /* create ucode bo when sw_init complete*/ 2410 if (r) 2411 goto init_failed; 2412 2413 r = amdgpu_device_ip_hw_init_phase1(adev); 2414 if (r) 2415 goto init_failed; 2416 2417 r = amdgpu_device_fw_loading(adev); 2418 if (r) 2419 goto init_failed; 2420 2421 r = amdgpu_device_ip_hw_init_phase2(adev); 2422 if (r) 2423 goto init_failed; 2424 2425 /* 2426 * retired pages will be loaded from eeprom and reserved here, 2427 * it should be called after amdgpu_device_ip_hw_init_phase2 since 2428 * for some ASICs the RAS EEPROM code relies on SMU fully functioning 2429 * for I2C communication which only true at this point. 2430 * 2431 * amdgpu_ras_recovery_init may fail, but the upper only cares the 2432 * failure from bad gpu situation and stop amdgpu init process 2433 * accordingly. For other failed cases, it will still release all 2434 * the resource and print error message, rather than returning one 2435 * negative value to upper level. 2436 * 2437 * Note: theoretically, this should be called before all vram allocations 2438 * to protect retired page from abusing 2439 */ 2440 init_badpage = (adev->init_lvl->level != AMDGPU_INIT_LEVEL_MINIMAL_XGMI); 2441 r = amdgpu_ras_recovery_init(adev, init_badpage); 2442 if (r) 2443 goto init_failed; 2444 2445 /** 2446 * In case of XGMI grab extra reference for reset domain for this device 2447 */ 2448 if (adev->gmc.xgmi.num_physical_nodes > 1) { 2449 if (amdgpu_xgmi_add_device(adev) == 0) { 2450 if (!amdgpu_sriov_vf(adev)) { 2451 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev); 2452 2453 if (WARN_ON(!hive)) { 2454 r = -ENOENT; 2455 goto init_failed; 2456 } 2457 2458 if (!hive->reset_domain || 2459 !amdgpu_reset_get_reset_domain(hive->reset_domain)) { 2460 r = -ENOENT; 2461 amdgpu_put_xgmi_hive(hive); 2462 goto init_failed; 2463 } 2464 2465 /* Drop the early temporary reset domain we created for device */ 2466 amdgpu_reset_put_reset_domain(adev->reset_domain); 2467 adev->reset_domain = hive->reset_domain; 2468 amdgpu_put_xgmi_hive(hive); 2469 } 2470 } 2471 } 2472 2473 r = amdgpu_device_init_schedulers(adev); 2474 if (r) 2475 goto init_failed; 2476 2477 amdgpu_ttm_enable_buffer_funcs(adev); 2478 2479 /* Don't init kfd if whole hive need to be reset during init */ 2480 if (adev->init_lvl->level != AMDGPU_INIT_LEVEL_MINIMAL_XGMI) { 2481 amdgpu_amdkfd_device_init(adev); 2482 } 2483 2484 amdgpu_fru_get_product_info(adev); 2485 2486 r = amdgpu_cper_init(adev); 2487 2488 init_failed: 2489 2490 return r; 2491 } 2492 2493 /** 2494 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer 2495 * 2496 * @adev: amdgpu_device pointer 2497 * 2498 * Writes a reset magic value to the gart pointer in VRAM. The driver calls 2499 * this function before a GPU reset. If the value is retained after a 2500 * GPU reset, VRAM has not been lost. Some GPU resets may destroy VRAM contents. 2501 */ 2502 static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev) 2503 { 2504 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM); 2505 } 2506 2507 /** 2508 * amdgpu_device_check_vram_lost - check if vram is valid 2509 * 2510 * @adev: amdgpu_device pointer 2511 * 2512 * Checks the reset magic value written to the gart pointer in VRAM. 2513 * The driver calls this after a GPU reset to see if the contents of 2514 * VRAM is lost or now. 2515 * returns true if vram is lost, false if not. 2516 */ 2517 static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev) 2518 { 2519 if (memcmp(adev->gart.ptr, adev->reset_magic, 2520 AMDGPU_RESET_MAGIC_NUM)) 2521 return true; 2522 2523 if (!amdgpu_in_reset(adev)) 2524 return false; 2525 2526 /* 2527 * For all ASICs with baco/mode1 reset, the VRAM is 2528 * always assumed to be lost. 2529 */ 2530 switch (amdgpu_asic_reset_method(adev)) { 2531 case AMD_RESET_METHOD_LEGACY: 2532 case AMD_RESET_METHOD_LINK: 2533 case AMD_RESET_METHOD_BACO: 2534 case AMD_RESET_METHOD_MODE1: 2535 return true; 2536 default: 2537 return false; 2538 } 2539 } 2540 2541 /** 2542 * amdgpu_device_set_cg_state - set clockgating for amdgpu device 2543 * 2544 * @adev: amdgpu_device pointer 2545 * @state: clockgating state (gate or ungate) 2546 * 2547 * The list of all the hardware IPs that make up the asic is walked and the 2548 * set_clockgating_state callbacks are run. 2549 * Late initialization pass enabling clockgating for hardware IPs. 2550 * Fini or suspend, pass disabling clockgating for hardware IPs. 2551 * Returns 0 on success, negative error code on failure. 2552 */ 2553 2554 int amdgpu_device_set_cg_state(struct amdgpu_device *adev, 2555 enum amd_clockgating_state state) 2556 { 2557 int i, j, r; 2558 2559 if (amdgpu_emu_mode == 1) 2560 return 0; 2561 2562 for (j = 0; j < adev->num_ip_blocks; j++) { 2563 i = state == AMD_CG_STATE_GATE ? j : adev->num_ip_blocks - j - 1; 2564 if (!adev->ip_blocks[i].status.late_initialized) 2565 continue; 2566 if (!adev->ip_blocks[i].version) 2567 continue; 2568 /* skip CG for GFX, SDMA on S0ix */ 2569 if (adev->in_s0ix && 2570 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX || 2571 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SDMA)) 2572 continue; 2573 /* skip CG for VCE/UVD, it's handled specially */ 2574 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD && 2575 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE && 2576 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN && 2577 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG && 2578 adev->ip_blocks[i].version->funcs->set_clockgating_state) { 2579 /* enable clockgating to save power */ 2580 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(&adev->ip_blocks[i], 2581 state); 2582 if (r) { 2583 dev_err(adev->dev, 2584 "set_clockgating_state(gate) of IP block <%s> failed %d\n", 2585 adev->ip_blocks[i].version->funcs->name, 2586 r); 2587 return r; 2588 } 2589 } 2590 } 2591 2592 return 0; 2593 } 2594 2595 int amdgpu_device_set_pg_state(struct amdgpu_device *adev, 2596 enum amd_powergating_state state) 2597 { 2598 int i, j, r; 2599 2600 if (amdgpu_emu_mode == 1) 2601 return 0; 2602 2603 for (j = 0; j < adev->num_ip_blocks; j++) { 2604 i = state == AMD_PG_STATE_GATE ? j : adev->num_ip_blocks - j - 1; 2605 if (!adev->ip_blocks[i].status.late_initialized) 2606 continue; 2607 if (!adev->ip_blocks[i].version) 2608 continue; 2609 /* skip PG for GFX, SDMA on S0ix */ 2610 if (adev->in_s0ix && 2611 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX || 2612 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SDMA)) 2613 continue; 2614 /* skip CG for VCE/UVD, it's handled specially */ 2615 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD && 2616 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE && 2617 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN && 2618 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG && 2619 adev->ip_blocks[i].version->funcs->set_powergating_state) { 2620 /* enable powergating to save power */ 2621 r = adev->ip_blocks[i].version->funcs->set_powergating_state(&adev->ip_blocks[i], 2622 state); 2623 if (r) { 2624 dev_err(adev->dev, 2625 "set_powergating_state(gate) of IP block <%s> failed %d\n", 2626 adev->ip_blocks[i].version->funcs->name, 2627 r); 2628 return r; 2629 } 2630 } 2631 } 2632 return 0; 2633 } 2634 2635 static int amdgpu_device_enable_mgpu_fan_boost(void) 2636 { 2637 struct amdgpu_gpu_instance *gpu_ins; 2638 struct amdgpu_device *adev; 2639 int i, ret = 0; 2640 2641 mutex_lock(&mgpu_info.mutex); 2642 2643 /* 2644 * MGPU fan boost feature should be enabled 2645 * only when there are two or more dGPUs in 2646 * the system 2647 */ 2648 if (mgpu_info.num_dgpu < 2) 2649 goto out; 2650 2651 for (i = 0; i < mgpu_info.num_dgpu; i++) { 2652 gpu_ins = &(mgpu_info.gpu_ins[i]); 2653 adev = gpu_ins->adev; 2654 if (!(adev->flags & AMD_IS_APU || amdgpu_sriov_multi_vf_mode(adev)) && 2655 !gpu_ins->mgpu_fan_enabled) { 2656 ret = amdgpu_dpm_enable_mgpu_fan_boost(adev); 2657 if (ret) 2658 break; 2659 2660 gpu_ins->mgpu_fan_enabled = 1; 2661 } 2662 } 2663 2664 out: 2665 mutex_unlock(&mgpu_info.mutex); 2666 2667 return ret; 2668 } 2669 2670 /** 2671 * amdgpu_device_ip_late_init - run late init for hardware IPs 2672 * 2673 * @adev: amdgpu_device pointer 2674 * 2675 * Late initialization pass for hardware IPs. The list of all the hardware 2676 * IPs that make up the asic is walked and the late_init callbacks are run. 2677 * late_init covers any special initialization that an IP requires 2678 * after all of the have been initialized or something that needs to happen 2679 * late in the init process. 2680 * Returns 0 on success, negative error code on failure. 2681 */ 2682 static int amdgpu_device_ip_late_init(struct amdgpu_device *adev) 2683 { 2684 struct amdgpu_gpu_instance *gpu_instance; 2685 int i = 0, r; 2686 2687 for (i = 0; i < adev->num_ip_blocks; i++) { 2688 if (!adev->ip_blocks[i].status.hw) 2689 continue; 2690 if (adev->ip_blocks[i].version->funcs->late_init) { 2691 r = adev->ip_blocks[i].version->funcs->late_init(&adev->ip_blocks[i]); 2692 if (r) { 2693 dev_err(adev->dev, 2694 "late_init of IP block <%s> failed %d\n", 2695 adev->ip_blocks[i].version->funcs->name, 2696 r); 2697 return r; 2698 } 2699 } 2700 adev->ip_blocks[i].status.late_initialized = true; 2701 } 2702 2703 r = amdgpu_ras_late_init(adev); 2704 if (r) { 2705 dev_err(adev->dev, "amdgpu_ras_late_init failed %d", r); 2706 return r; 2707 } 2708 2709 if (!amdgpu_reset_in_recovery(adev)) 2710 amdgpu_ras_set_error_query_ready(adev, true); 2711 2712 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE); 2713 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_GATE); 2714 2715 amdgpu_device_fill_reset_magic(adev); 2716 2717 r = amdgpu_device_enable_mgpu_fan_boost(); 2718 if (r) 2719 dev_err(adev->dev, "enable mgpu fan boost failed (%d).\n", r); 2720 2721 /* For passthrough configuration on arcturus and aldebaran, enable special handling SBR */ 2722 if (amdgpu_passthrough(adev) && 2723 ((adev->asic_type == CHIP_ARCTURUS && adev->gmc.xgmi.num_physical_nodes > 1) || 2724 adev->asic_type == CHIP_ALDEBARAN)) 2725 amdgpu_dpm_handle_passthrough_sbr(adev, true); 2726 2727 if (adev->gmc.xgmi.num_physical_nodes > 1) { 2728 mutex_lock(&mgpu_info.mutex); 2729 2730 /* 2731 * Reset device p-state to low as this was booted with high. 2732 * 2733 * This should be performed only after all devices from the same 2734 * hive get initialized. 2735 * 2736 * However, it's unknown how many device in the hive in advance. 2737 * As this is counted one by one during devices initializations. 2738 * 2739 * So, we wait for all XGMI interlinked devices initialized. 2740 * This may bring some delays as those devices may come from 2741 * different hives. But that should be OK. 2742 */ 2743 if (mgpu_info.num_dgpu == adev->gmc.xgmi.num_physical_nodes) { 2744 for (i = 0; i < mgpu_info.num_gpu; i++) { 2745 gpu_instance = &(mgpu_info.gpu_ins[i]); 2746 if (gpu_instance->adev->flags & AMD_IS_APU) 2747 continue; 2748 2749 r = amdgpu_xgmi_set_pstate(gpu_instance->adev, 2750 AMDGPU_XGMI_PSTATE_MIN); 2751 if (r) { 2752 dev_err(adev->dev, 2753 "pstate setting failed (%d).\n", 2754 r); 2755 break; 2756 } 2757 } 2758 } 2759 2760 mutex_unlock(&mgpu_info.mutex); 2761 } 2762 2763 return 0; 2764 } 2765 2766 static void amdgpu_ip_block_hw_fini(struct amdgpu_ip_block *ip_block) 2767 { 2768 struct amdgpu_device *adev = ip_block->adev; 2769 int r; 2770 2771 if (!ip_block->version->funcs->hw_fini) { 2772 dev_err(adev->dev, "hw_fini of IP block <%s> not defined\n", 2773 ip_block->version->funcs->name); 2774 } else { 2775 r = ip_block->version->funcs->hw_fini(ip_block); 2776 /* XXX handle errors */ 2777 if (r) { 2778 dev_dbg(adev->dev, 2779 "hw_fini of IP block <%s> failed %d\n", 2780 ip_block->version->funcs->name, r); 2781 } 2782 } 2783 2784 ip_block->status.hw = false; 2785 } 2786 2787 /** 2788 * amdgpu_device_smu_fini_early - smu hw_fini wrapper 2789 * 2790 * @adev: amdgpu_device pointer 2791 * 2792 * For ASICs need to disable SMC first 2793 */ 2794 static void amdgpu_device_smu_fini_early(struct amdgpu_device *adev) 2795 { 2796 int i; 2797 2798 if (amdgpu_ip_version(adev, GC_HWIP, 0) > IP_VERSION(9, 0, 0)) 2799 return; 2800 2801 for (i = 0; i < adev->num_ip_blocks; i++) { 2802 if (!adev->ip_blocks[i].status.hw) 2803 continue; 2804 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) { 2805 amdgpu_ip_block_hw_fini(&adev->ip_blocks[i]); 2806 break; 2807 } 2808 } 2809 } 2810 2811 static int amdgpu_device_ip_fini_early(struct amdgpu_device *adev) 2812 { 2813 int i, r; 2814 2815 for (i = 0; i < adev->num_ip_blocks; i++) { 2816 if (!adev->ip_blocks[i].version) 2817 continue; 2818 if (!adev->ip_blocks[i].version->funcs->early_fini) 2819 continue; 2820 2821 r = adev->ip_blocks[i].version->funcs->early_fini(&adev->ip_blocks[i]); 2822 if (r) { 2823 dev_dbg(adev->dev, 2824 "early_fini of IP block <%s> failed %d\n", 2825 adev->ip_blocks[i].version->funcs->name, r); 2826 } 2827 } 2828 2829 amdgpu_amdkfd_suspend(adev, true); 2830 amdgpu_amdkfd_teardown_processes(adev); 2831 amdgpu_userq_suspend(adev); 2832 2833 /* Workaround for ASICs need to disable SMC first */ 2834 amdgpu_device_smu_fini_early(adev); 2835 2836 for (i = adev->num_ip_blocks - 1; i >= 0; i--) { 2837 if (!adev->ip_blocks[i].status.hw) 2838 continue; 2839 2840 amdgpu_ip_block_hw_fini(&adev->ip_blocks[i]); 2841 } 2842 2843 if (amdgpu_sriov_vf(adev)) { 2844 if (amdgpu_virt_release_full_gpu(adev, false)) 2845 dev_err(adev->dev, 2846 "failed to release exclusive mode on fini\n"); 2847 } 2848 2849 /* 2850 * Driver reload on the APU can fail due to firmware validation because 2851 * the PSP is always running, as it is shared across the whole SoC. 2852 * This same issue does not occur on dGPU because it has a mechanism 2853 * that checks whether the PSP is running. A solution for those issues 2854 * in the APU is to trigger a GPU reset, but this should be done during 2855 * the unload phase to avoid adding boot latency and screen flicker. 2856 * GFX V11 has GC block as default off IP. Every time AMDGPU driver sends 2857 * a request to PMFW to unload MP1, PMFW will put GC in reset and power down 2858 * the voltage. Hence, skipping reset for APUs with GFX V11 or later. 2859 */ 2860 if ((adev->flags & AMD_IS_APU) && !adev->gmc.is_app_apu && 2861 amdgpu_ip_version(adev, GC_HWIP, 0) < IP_VERSION(11, 0, 0)) { 2862 r = amdgpu_asic_reset(adev); 2863 if (r) 2864 dev_err(adev->dev, "asic reset on %s failed\n", __func__); 2865 } 2866 2867 return 0; 2868 } 2869 2870 /** 2871 * amdgpu_device_ip_fini - run fini for hardware IPs 2872 * 2873 * @adev: amdgpu_device pointer 2874 * 2875 * Main teardown pass for hardware IPs. The list of all the hardware 2876 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks 2877 * are run. hw_fini tears down the hardware associated with each IP 2878 * and sw_fini tears down any software state associated with each IP. 2879 * Returns 0 on success, negative error code on failure. 2880 */ 2881 static int amdgpu_device_ip_fini(struct amdgpu_device *adev) 2882 { 2883 int i, r; 2884 2885 amdgpu_cper_fini(adev); 2886 2887 if (amdgpu_sriov_vf(adev) && adev->virt.ras_init_done) 2888 amdgpu_virt_release_ras_err_handler_data(adev); 2889 2890 if (adev->gmc.xgmi.num_physical_nodes > 1) 2891 amdgpu_xgmi_remove_device(adev); 2892 2893 amdgpu_amdkfd_device_fini_sw(adev); 2894 2895 for (i = adev->num_ip_blocks - 1; i >= 0; i--) { 2896 if (!adev->ip_blocks[i].status.sw) 2897 continue; 2898 2899 if (!adev->ip_blocks[i].version) 2900 continue; 2901 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) { 2902 amdgpu_ucode_free_bo(adev); 2903 amdgpu_free_static_csa(&adev->virt.csa_obj); 2904 amdgpu_wb_fini(adev); 2905 amdgpu_device_mem_scratch_fini(adev); 2906 amdgpu_ib_pool_fini(adev); 2907 amdgpu_seq64_fini(adev); 2908 amdgpu_doorbell_fini(adev); 2909 } 2910 if (adev->ip_blocks[i].version->funcs->sw_fini) { 2911 r = adev->ip_blocks[i].version->funcs->sw_fini(&adev->ip_blocks[i]); 2912 /* XXX handle errors */ 2913 if (r) { 2914 dev_dbg(adev->dev, 2915 "sw_fini of IP block <%s> failed %d\n", 2916 adev->ip_blocks[i].version->funcs->name, 2917 r); 2918 } 2919 } 2920 adev->ip_blocks[i].status.sw = false; 2921 adev->ip_blocks[i].status.valid = false; 2922 } 2923 2924 for (i = adev->num_ip_blocks - 1; i >= 0; i--) { 2925 if (!adev->ip_blocks[i].status.late_initialized) 2926 continue; 2927 if (!adev->ip_blocks[i].version) 2928 continue; 2929 if (adev->ip_blocks[i].version->funcs->late_fini) 2930 adev->ip_blocks[i].version->funcs->late_fini(&adev->ip_blocks[i]); 2931 adev->ip_blocks[i].status.late_initialized = false; 2932 } 2933 2934 amdgpu_ras_fini(adev); 2935 amdgpu_uid_fini(adev); 2936 2937 return 0; 2938 } 2939 2940 /** 2941 * amdgpu_device_delayed_init_work_handler - work handler for IB tests 2942 * 2943 * @work: work_struct. 2944 */ 2945 static void amdgpu_device_delayed_init_work_handler(struct work_struct *work) 2946 { 2947 struct amdgpu_device *adev = 2948 container_of(work, struct amdgpu_device, delayed_init_work.work); 2949 int r; 2950 2951 r = amdgpu_ib_ring_tests(adev); 2952 if (r) 2953 dev_err(adev->dev, "ib ring test failed (%d).\n", r); 2954 } 2955 2956 static void amdgpu_device_delay_enable_gfx_off(struct work_struct *work) 2957 { 2958 struct amdgpu_device *adev = 2959 container_of(work, struct amdgpu_device, gfx.gfx_off_delay_work.work); 2960 2961 WARN_ON_ONCE(adev->gfx.gfx_off_state); 2962 WARN_ON_ONCE(adev->gfx.gfx_off_req_count); 2963 2964 if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true, 0)) 2965 adev->gfx.gfx_off_state = true; 2966 } 2967 2968 /** 2969 * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1) 2970 * 2971 * @adev: amdgpu_device pointer 2972 * 2973 * Main suspend function for hardware IPs. The list of all the hardware 2974 * IPs that make up the asic is walked, clockgating is disabled and the 2975 * suspend callbacks are run. suspend puts the hardware and software state 2976 * in each IP into a state suitable for suspend. 2977 * Returns 0 on success, negative error code on failure. 2978 */ 2979 static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev) 2980 { 2981 int i, r, rec; 2982 2983 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE); 2984 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE); 2985 2986 /* 2987 * Per PMFW team's suggestion, driver needs to handle gfxoff 2988 * and df cstate features disablement for gpu reset(e.g. Mode1Reset) 2989 * scenario. Add the missing df cstate disablement here. 2990 */ 2991 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_DISALLOW)) 2992 dev_warn(adev->dev, "Failed to disallow df cstate"); 2993 2994 for (i = adev->num_ip_blocks - 1; i >= 0; i--) { 2995 if (!adev->ip_blocks[i].status.valid) 2996 continue; 2997 2998 /* displays are handled separately */ 2999 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_DCE) 3000 continue; 3001 3002 r = amdgpu_ip_block_suspend(&adev->ip_blocks[i]); 3003 if (r) 3004 goto unwind; 3005 } 3006 3007 return 0; 3008 unwind: 3009 rec = amdgpu_device_ip_resume_phase3(adev); 3010 if (rec) 3011 dev_err(adev->dev, 3012 "amdgpu_device_ip_resume_phase3 failed during unwind: %d\n", 3013 rec); 3014 3015 amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_ALLOW); 3016 3017 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_GATE); 3018 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE); 3019 3020 return r; 3021 } 3022 3023 /** 3024 * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2) 3025 * 3026 * @adev: amdgpu_device pointer 3027 * 3028 * Main suspend function for hardware IPs. The list of all the hardware 3029 * IPs that make up the asic is walked, clockgating is disabled and the 3030 * suspend callbacks are run. suspend puts the hardware and software state 3031 * in each IP into a state suitable for suspend. 3032 * Returns 0 on success, negative error code on failure. 3033 */ 3034 static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev) 3035 { 3036 int i, r, rec; 3037 3038 if (adev->in_s0ix) 3039 amdgpu_dpm_gfx_state_change(adev, sGpuChangeState_D3Entry); 3040 3041 for (i = adev->num_ip_blocks - 1; i >= 0; i--) { 3042 if (!adev->ip_blocks[i].status.valid || !adev->ip_blocks[i].status.hw) 3043 continue; 3044 /* displays are handled in phase1 */ 3045 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) 3046 continue; 3047 /* PSP lost connection when err_event_athub occurs */ 3048 if (amdgpu_ras_intr_triggered() && 3049 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) { 3050 adev->ip_blocks[i].status.hw = false; 3051 continue; 3052 } 3053 3054 /* skip unnecessary suspend if we do not initialize them yet */ 3055 if (!amdgpu_ip_member_of_hwini( 3056 adev, adev->ip_blocks[i].version->type)) 3057 continue; 3058 3059 /* Since we skip suspend for S0i3, we need to cancel the delayed 3060 * idle work here as the suspend callback never gets called. 3061 */ 3062 if (adev->in_s0ix && 3063 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX && 3064 amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(10, 0, 0)) 3065 cancel_delayed_work_sync(&adev->gfx.idle_work); 3066 /* skip suspend of gfx/mes and psp for S0ix 3067 * gfx is in gfxoff state, so on resume it will exit gfxoff just 3068 * like at runtime. PSP is also part of the always on hardware 3069 * so no need to suspend it. 3070 */ 3071 if (adev->in_s0ix && 3072 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP || 3073 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX || 3074 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_MES)) 3075 continue; 3076 3077 /* SDMA 5.x+ is part of GFX power domain so it's covered by GFXOFF */ 3078 if (adev->in_s0ix && 3079 (amdgpu_ip_version(adev, SDMA0_HWIP, 0) >= 3080 IP_VERSION(5, 0, 0)) && 3081 (adev->ip_blocks[i].version->type == 3082 AMD_IP_BLOCK_TYPE_SDMA)) 3083 continue; 3084 3085 /* Once swPSP provides the IMU, RLC FW binaries to TOS during cold-boot. 3086 * These are in TMR, hence are expected to be reused by PSP-TOS to reload 3087 * from this location and RLC Autoload automatically also gets loaded 3088 * from here based on PMFW -> PSP message during re-init sequence. 3089 * Therefore, the psp suspend & resume should be skipped to avoid destroy 3090 * the TMR and reload FWs again for IMU enabled APU ASICs. 3091 */ 3092 if (amdgpu_in_reset(adev) && 3093 (adev->flags & AMD_IS_APU) && adev->gfx.imu.funcs && 3094 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) 3095 continue; 3096 3097 r = amdgpu_ip_block_suspend(&adev->ip_blocks[i]); 3098 if (r) 3099 goto unwind; 3100 3101 /* handle putting the SMC in the appropriate state */ 3102 if (!amdgpu_sriov_vf(adev)) { 3103 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) { 3104 r = amdgpu_dpm_set_mp1_state(adev, adev->mp1_state); 3105 if (r) { 3106 dev_err(adev->dev, 3107 "SMC failed to set mp1 state %d, %d\n", 3108 adev->mp1_state, r); 3109 goto unwind; 3110 } 3111 } 3112 } 3113 } 3114 3115 return 0; 3116 unwind: 3117 /* suspend phase 2 = resume phase 1 + resume phase 2 */ 3118 rec = amdgpu_device_ip_resume_phase1(adev); 3119 if (rec) { 3120 dev_err(adev->dev, 3121 "amdgpu_device_ip_resume_phase1 failed during unwind: %d\n", 3122 rec); 3123 return r; 3124 } 3125 3126 rec = amdgpu_device_fw_loading(adev); 3127 if (rec) { 3128 dev_err(adev->dev, 3129 "amdgpu_device_fw_loading failed during unwind: %d\n", 3130 rec); 3131 return r; 3132 } 3133 3134 rec = amdgpu_device_ip_resume_phase2(adev); 3135 if (rec) { 3136 dev_err(adev->dev, 3137 "amdgpu_device_ip_resume_phase2 failed during unwind: %d\n", 3138 rec); 3139 return r; 3140 } 3141 3142 return r; 3143 } 3144 3145 /** 3146 * amdgpu_device_ip_suspend - run suspend for hardware IPs 3147 * 3148 * @adev: amdgpu_device pointer 3149 * 3150 * Main suspend function for hardware IPs. The list of all the hardware 3151 * IPs that make up the asic is walked, clockgating is disabled and the 3152 * suspend callbacks are run. suspend puts the hardware and software state 3153 * in each IP into a state suitable for suspend. 3154 * Returns 0 on success, negative error code on failure. 3155 */ 3156 static int amdgpu_device_ip_suspend(struct amdgpu_device *adev) 3157 { 3158 int r; 3159 3160 if (amdgpu_sriov_vf(adev)) { 3161 amdgpu_virt_fini_data_exchange(adev); 3162 amdgpu_virt_request_full_gpu(adev, false); 3163 } 3164 3165 amdgpu_ttm_disable_buffer_funcs(adev); 3166 3167 r = amdgpu_device_ip_suspend_phase1(adev); 3168 if (r) 3169 return r; 3170 r = amdgpu_device_ip_suspend_phase2(adev); 3171 3172 if (amdgpu_sriov_vf(adev)) 3173 amdgpu_virt_release_full_gpu(adev, false); 3174 3175 return r; 3176 } 3177 3178 static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev) 3179 { 3180 int i, r; 3181 3182 static enum amd_ip_block_type ip_order[] = { 3183 AMD_IP_BLOCK_TYPE_COMMON, 3184 AMD_IP_BLOCK_TYPE_GMC, 3185 AMD_IP_BLOCK_TYPE_PSP, 3186 AMD_IP_BLOCK_TYPE_IH, 3187 }; 3188 3189 for (i = 0; i < adev->num_ip_blocks; i++) { 3190 int j; 3191 struct amdgpu_ip_block *block; 3192 3193 block = &adev->ip_blocks[i]; 3194 block->status.hw = false; 3195 3196 for (j = 0; j < ARRAY_SIZE(ip_order); j++) { 3197 3198 if (block->version->type != ip_order[j] || 3199 !block->status.valid) 3200 continue; 3201 3202 r = block->version->funcs->hw_init(&adev->ip_blocks[i]); 3203 if (r) { 3204 dev_err(adev->dev, "RE-INIT-early: %s failed\n", 3205 block->version->funcs->name); 3206 return r; 3207 } 3208 block->status.hw = true; 3209 } 3210 } 3211 3212 return 0; 3213 } 3214 3215 static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev) 3216 { 3217 struct amdgpu_ip_block *block; 3218 int i, r = 0; 3219 3220 static enum amd_ip_block_type ip_order[] = { 3221 AMD_IP_BLOCK_TYPE_SMC, 3222 AMD_IP_BLOCK_TYPE_DCE, 3223 AMD_IP_BLOCK_TYPE_GFX, 3224 AMD_IP_BLOCK_TYPE_SDMA, 3225 AMD_IP_BLOCK_TYPE_MES, 3226 AMD_IP_BLOCK_TYPE_UVD, 3227 AMD_IP_BLOCK_TYPE_VCE, 3228 AMD_IP_BLOCK_TYPE_VCN, 3229 AMD_IP_BLOCK_TYPE_JPEG 3230 }; 3231 3232 for (i = 0; i < ARRAY_SIZE(ip_order); i++) { 3233 block = amdgpu_device_ip_get_ip_block(adev, ip_order[i]); 3234 3235 if (!block) 3236 continue; 3237 3238 if (block->status.valid && !block->status.hw) { 3239 if (block->version->type == AMD_IP_BLOCK_TYPE_SMC) { 3240 r = amdgpu_ip_block_resume(block); 3241 } else { 3242 r = block->version->funcs->hw_init(block); 3243 } 3244 3245 if (r) { 3246 dev_err(adev->dev, "RE-INIT-late: %s failed\n", 3247 block->version->funcs->name); 3248 break; 3249 } 3250 block->status.hw = true; 3251 } 3252 } 3253 3254 return r; 3255 } 3256 3257 /** 3258 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs 3259 * 3260 * @adev: amdgpu_device pointer 3261 * 3262 * First resume function for hardware IPs. The list of all the hardware 3263 * IPs that make up the asic is walked and the resume callbacks are run for 3264 * COMMON, GMC, and IH. resume puts the hardware into a functional state 3265 * after a suspend and updates the software state as necessary. This 3266 * function is also used for restoring the GPU after a GPU reset. 3267 * Returns 0 on success, negative error code on failure. 3268 */ 3269 static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev) 3270 { 3271 int i, r; 3272 3273 for (i = 0; i < adev->num_ip_blocks; i++) { 3274 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw) 3275 continue; 3276 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON || 3277 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC || 3278 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH || 3279 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP && amdgpu_sriov_vf(adev))) { 3280 3281 r = amdgpu_ip_block_resume(&adev->ip_blocks[i]); 3282 if (r) 3283 return r; 3284 } 3285 } 3286 3287 return 0; 3288 } 3289 3290 /** 3291 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs 3292 * 3293 * @adev: amdgpu_device pointer 3294 * 3295 * Second resume function for hardware IPs. The list of all the hardware 3296 * IPs that make up the asic is walked and the resume callbacks are run for 3297 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a 3298 * functional state after a suspend and updates the software state as 3299 * necessary. This function is also used for restoring the GPU after a GPU 3300 * reset. 3301 * Returns 0 on success, negative error code on failure. 3302 */ 3303 static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev) 3304 { 3305 int i, r; 3306 3307 for (i = 0; i < adev->num_ip_blocks; i++) { 3308 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw) 3309 continue; 3310 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON || 3311 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC || 3312 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH || 3313 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE || 3314 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) 3315 continue; 3316 r = amdgpu_ip_block_resume(&adev->ip_blocks[i]); 3317 if (r) 3318 return r; 3319 } 3320 3321 return 0; 3322 } 3323 3324 /** 3325 * amdgpu_device_ip_resume_phase3 - run resume for hardware IPs 3326 * 3327 * @adev: amdgpu_device pointer 3328 * 3329 * Third resume function for hardware IPs. The list of all the hardware 3330 * IPs that make up the asic is walked and the resume callbacks are run for 3331 * all DCE. resume puts the hardware into a functional state after a suspend 3332 * and updates the software state as necessary. This function is also used 3333 * for restoring the GPU after a GPU reset. 3334 * 3335 * Returns 0 on success, negative error code on failure. 3336 */ 3337 static int amdgpu_device_ip_resume_phase3(struct amdgpu_device *adev) 3338 { 3339 int i, r; 3340 3341 for (i = 0; i < adev->num_ip_blocks; i++) { 3342 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw) 3343 continue; 3344 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) { 3345 r = amdgpu_ip_block_resume(&adev->ip_blocks[i]); 3346 if (r) 3347 return r; 3348 } 3349 } 3350 3351 return 0; 3352 } 3353 3354 /** 3355 * amdgpu_device_ip_resume - run resume for hardware IPs 3356 * 3357 * @adev: amdgpu_device pointer 3358 * 3359 * Main resume function for hardware IPs. The hardware IPs 3360 * are split into two resume functions because they are 3361 * also used in recovering from a GPU reset and some additional 3362 * steps need to be take between them. In this case (S3/S4) they are 3363 * run sequentially. 3364 * Returns 0 on success, negative error code on failure. 3365 */ 3366 static int amdgpu_device_ip_resume(struct amdgpu_device *adev) 3367 { 3368 int r; 3369 3370 r = amdgpu_device_ip_resume_phase1(adev); 3371 if (r) 3372 return r; 3373 3374 r = amdgpu_device_fw_loading(adev); 3375 if (r) 3376 return r; 3377 3378 r = amdgpu_device_ip_resume_phase2(adev); 3379 3380 amdgpu_ttm_enable_buffer_funcs(adev); 3381 3382 if (r) 3383 return r; 3384 3385 amdgpu_fence_driver_hw_init(adev); 3386 3387 r = amdgpu_device_ip_resume_phase3(adev); 3388 3389 return r; 3390 } 3391 3392 /** 3393 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV 3394 * 3395 * @adev: amdgpu_device pointer 3396 * 3397 * Query the VBIOS data tables to determine if the board supports SR-IOV. 3398 */ 3399 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev) 3400 { 3401 if (amdgpu_sriov_vf(adev)) { 3402 if (adev->is_atom_fw) { 3403 if (amdgpu_atomfirmware_gpu_virtualization_supported(adev)) 3404 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS; 3405 } else { 3406 if (amdgpu_atombios_has_gpu_virtualization_table(adev)) 3407 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS; 3408 } 3409 3410 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS)) 3411 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0); 3412 } 3413 } 3414 3415 /** 3416 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic 3417 * 3418 * @pdev : pci device context 3419 * @asic_type: AMD asic type 3420 * 3421 * Check if there is DC (new modesetting infrastructre) support for an asic. 3422 * returns true if DC has support, false if not. 3423 */ 3424 bool amdgpu_device_asic_has_dc_support(struct pci_dev *pdev, 3425 enum amd_asic_type asic_type) 3426 { 3427 switch (asic_type) { 3428 #ifdef CONFIG_DRM_AMDGPU_SI 3429 case CHIP_HAINAN: 3430 #endif 3431 case CHIP_TOPAZ: 3432 /* chips with no display hardware */ 3433 return false; 3434 #if defined(CONFIG_DRM_AMD_DC) 3435 case CHIP_TAHITI: 3436 case CHIP_PITCAIRN: 3437 case CHIP_VERDE: 3438 case CHIP_OLAND: 3439 return amdgpu_dc != 0 && IS_ENABLED(CONFIG_DRM_AMD_DC_SI); 3440 default: 3441 return amdgpu_dc != 0; 3442 #else 3443 default: 3444 if (amdgpu_dc > 0) 3445 dev_info_once( 3446 &pdev->dev, 3447 "Display Core has been requested via kernel parameter but isn't supported by ASIC, ignoring\n"); 3448 return false; 3449 #endif 3450 } 3451 } 3452 3453 /** 3454 * amdgpu_device_has_dc_support - check if dc is supported 3455 * 3456 * @adev: amdgpu_device pointer 3457 * 3458 * Returns true for supported, false for not supported 3459 */ 3460 bool amdgpu_device_has_dc_support(struct amdgpu_device *adev) 3461 { 3462 if (adev->enable_virtual_display || 3463 (adev->harvest_ip_mask & AMD_HARVEST_IP_DMU_MASK)) 3464 return false; 3465 3466 return amdgpu_device_asic_has_dc_support(adev->pdev, adev->asic_type); 3467 } 3468 3469 static void amdgpu_device_xgmi_reset_func(struct work_struct *__work) 3470 { 3471 struct amdgpu_device *adev = 3472 container_of(__work, struct amdgpu_device, xgmi_reset_work); 3473 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev); 3474 3475 /* It's a bug to not have a hive within this function */ 3476 if (WARN_ON(!hive)) 3477 return; 3478 3479 /* 3480 * Use task barrier to synchronize all xgmi reset works across the 3481 * hive. task_barrier_enter and task_barrier_exit will block 3482 * until all the threads running the xgmi reset works reach 3483 * those points. task_barrier_full will do both blocks. 3484 */ 3485 if (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) { 3486 3487 task_barrier_enter(&hive->tb); 3488 adev->asic_reset_res = amdgpu_device_baco_enter(adev); 3489 3490 if (adev->asic_reset_res) 3491 goto fail; 3492 3493 task_barrier_exit(&hive->tb); 3494 adev->asic_reset_res = amdgpu_device_baco_exit(adev); 3495 3496 if (adev->asic_reset_res) 3497 goto fail; 3498 3499 amdgpu_ras_reset_error_count(adev, AMDGPU_RAS_BLOCK__MMHUB); 3500 } else { 3501 3502 task_barrier_full(&hive->tb); 3503 adev->asic_reset_res = amdgpu_asic_reset(adev); 3504 } 3505 3506 fail: 3507 if (adev->asic_reset_res) 3508 dev_warn(adev->dev, 3509 "ASIC reset failed with error, %d for drm dev, %s", 3510 adev->asic_reset_res, adev_to_drm(adev)->unique); 3511 amdgpu_put_xgmi_hive(hive); 3512 } 3513 3514 static int amdgpu_device_get_job_timeout_settings(struct amdgpu_device *adev) 3515 { 3516 char buf[AMDGPU_MAX_TIMEOUT_PARAM_LENGTH]; 3517 char *input = buf; 3518 char *timeout_setting = NULL; 3519 int index = 0; 3520 long timeout; 3521 int ret = 0; 3522 3523 /* By default timeout for all queues is 2 sec */ 3524 adev->gfx_timeout = adev->compute_timeout = adev->sdma_timeout = 3525 adev->video_timeout = msecs_to_jiffies(2000); 3526 3527 if (!strnlen(amdgpu_lockup_timeout, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) 3528 return 0; 3529 3530 /* 3531 * strsep() destructively modifies its input by replacing delimiters 3532 * with '\0'. Use a stack copy so the global module parameter buffer 3533 * remains intact for multi-GPU systems where this function is called 3534 * once per device. 3535 */ 3536 strscpy(buf, amdgpu_lockup_timeout, sizeof(buf)); 3537 3538 while ((timeout_setting = strsep(&input, ",")) && 3539 strnlen(timeout_setting, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) { 3540 ret = kstrtol(timeout_setting, 0, &timeout); 3541 if (ret) 3542 return ret; 3543 3544 if (timeout == 0) { 3545 index++; 3546 continue; 3547 } else if (timeout < 0) { 3548 timeout = MAX_SCHEDULE_TIMEOUT; 3549 dev_warn(adev->dev, "lockup timeout disabled"); 3550 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK); 3551 } else { 3552 timeout = msecs_to_jiffies(timeout); 3553 } 3554 3555 switch (index++) { 3556 case 0: 3557 adev->gfx_timeout = timeout; 3558 break; 3559 case 1: 3560 adev->compute_timeout = timeout; 3561 break; 3562 case 2: 3563 adev->sdma_timeout = timeout; 3564 break; 3565 case 3: 3566 adev->video_timeout = timeout; 3567 break; 3568 default: 3569 break; 3570 } 3571 } 3572 3573 /* When only one value specified apply it to all queues. */ 3574 if (index == 1) 3575 adev->gfx_timeout = adev->compute_timeout = adev->sdma_timeout = 3576 adev->video_timeout = timeout; 3577 3578 return ret; 3579 } 3580 3581 /** 3582 * amdgpu_device_check_iommu_direct_map - check if RAM direct mapped to GPU 3583 * 3584 * @adev: amdgpu_device pointer 3585 * 3586 * RAM direct mapped to GPU if IOMMU is not enabled or is pass through mode 3587 */ 3588 static void amdgpu_device_check_iommu_direct_map(struct amdgpu_device *adev) 3589 { 3590 struct iommu_domain *domain; 3591 3592 domain = iommu_get_domain_for_dev(adev->dev); 3593 if (!domain || domain->type == IOMMU_DOMAIN_IDENTITY) 3594 adev->ram_is_direct_mapped = true; 3595 } 3596 3597 #if defined(CONFIG_HSA_AMD_P2P) 3598 /** 3599 * amdgpu_device_check_iommu_remap - Check if DMA remapping is enabled. 3600 * 3601 * @adev: amdgpu_device pointer 3602 * 3603 * return if IOMMU remapping bar address 3604 */ 3605 static bool amdgpu_device_check_iommu_remap(struct amdgpu_device *adev) 3606 { 3607 struct iommu_domain *domain; 3608 3609 domain = iommu_get_domain_for_dev(adev->dev); 3610 if (domain && (domain->type == IOMMU_DOMAIN_DMA || 3611 domain->type == IOMMU_DOMAIN_DMA_FQ)) 3612 return true; 3613 3614 return false; 3615 } 3616 #endif 3617 3618 static void amdgpu_device_set_mcbp(struct amdgpu_device *adev) 3619 { 3620 if (amdgpu_mcbp == 1) 3621 adev->gfx.mcbp = true; 3622 else if (amdgpu_mcbp == 0) 3623 adev->gfx.mcbp = false; 3624 3625 if (amdgpu_sriov_vf(adev)) 3626 adev->gfx.mcbp = true; 3627 3628 if (adev->gfx.mcbp) 3629 dev_info(adev->dev, "MCBP is enabled\n"); 3630 } 3631 3632 static int amdgpu_device_sys_interface_init(struct amdgpu_device *adev) 3633 { 3634 int r; 3635 3636 r = amdgpu_atombios_sysfs_init(adev); 3637 if (r) 3638 drm_err(&adev->ddev, 3639 "registering atombios sysfs failed (%d).\n", r); 3640 3641 r = amdgpu_pm_sysfs_init(adev); 3642 if (r) 3643 dev_err(adev->dev, "registering pm sysfs failed (%d).\n", r); 3644 3645 r = amdgpu_ucode_sysfs_init(adev); 3646 if (r) { 3647 adev->ucode_sysfs_en = false; 3648 dev_err(adev->dev, "Creating firmware sysfs failed (%d).\n", r); 3649 } else 3650 adev->ucode_sysfs_en = true; 3651 3652 r = amdgpu_device_attr_sysfs_init(adev); 3653 if (r) 3654 dev_err(adev->dev, "Could not create amdgpu device attr\n"); 3655 3656 r = devm_device_add_group(adev->dev, &amdgpu_board_attrs_group); 3657 if (r) 3658 dev_err(adev->dev, 3659 "Could not create amdgpu board attributes\n"); 3660 3661 amdgpu_fru_sysfs_init(adev); 3662 amdgpu_reg_state_sysfs_init(adev); 3663 amdgpu_xcp_sysfs_init(adev); 3664 amdgpu_uma_sysfs_init(adev); 3665 amdgpu_ptl_sysfs_init(adev); 3666 3667 return r; 3668 } 3669 3670 static void amdgpu_device_sys_interface_fini(struct amdgpu_device *adev) 3671 { 3672 if (adev->pm.sysfs_initialized) 3673 amdgpu_pm_sysfs_fini(adev); 3674 if (adev->ucode_sysfs_en) 3675 amdgpu_ucode_sysfs_fini(adev); 3676 3677 amdgpu_discovery_sysfs_fini(adev); 3678 amdgpu_preempt_mgr_sysfs_fini(adev); 3679 3680 amdgpu_device_attr_sysfs_fini(adev); 3681 amdgpu_fru_sysfs_fini(adev); 3682 3683 amdgpu_reg_state_sysfs_fini(adev); 3684 amdgpu_xcp_sysfs_fini(adev); 3685 amdgpu_uma_sysfs_fini(adev); 3686 amdgpu_ptl_sysfs_fini(adev); 3687 } 3688 3689 /** 3690 * amdgpu_device_init - initialize the driver 3691 * 3692 * @adev: amdgpu_device pointer 3693 * @flags: driver flags 3694 * 3695 * Initializes the driver info and hw (all asics). 3696 * Returns 0 for success or an error on failure. 3697 * Called at driver startup. 3698 */ 3699 int amdgpu_device_init(struct amdgpu_device *adev, 3700 uint32_t flags) 3701 { 3702 struct pci_dev *pdev = adev->pdev; 3703 int r, i; 3704 bool px = false; 3705 u32 max_MBps; 3706 int tmp; 3707 3708 adev->shutdown = false; 3709 adev->flags = flags; 3710 3711 if (amdgpu_force_asic_type >= 0 && amdgpu_force_asic_type < CHIP_LAST) 3712 adev->asic_type = amdgpu_force_asic_type; 3713 else 3714 adev->asic_type = flags & AMD_ASIC_MASK; 3715 3716 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT; 3717 if (amdgpu_emu_mode == 1) 3718 adev->usec_timeout *= 10; 3719 adev->gmc.gart_size = 512 * 1024 * 1024; 3720 adev->accel_working = false; 3721 adev->num_rings = 0; 3722 RCU_INIT_POINTER(adev->gang_submit, dma_fence_get_stub()); 3723 adev->mman.buffer_funcs = NULL; 3724 adev->mman.num_buffer_funcs_scheds = 0; 3725 adev->vm_manager.vm_pte_funcs = NULL; 3726 adev->vm_manager.vm_pte_num_scheds = 0; 3727 adev->gmc.gmc_funcs = NULL; 3728 adev->harvest_ip_mask = 0x0; 3729 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS); 3730 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES); 3731 3732 amdgpu_reg_access_init(adev); 3733 3734 dev_info( 3735 adev->dev, 3736 "initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n", 3737 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device, 3738 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision); 3739 3740 /* mutex initialization are all done here so we 3741 * can recall function without having locking issues 3742 */ 3743 mutex_init(&adev->firmware.mutex); 3744 mutex_init(&adev->pm.mutex); 3745 mutex_init(&adev->gfx.gpu_clock_mutex); 3746 mutex_init(&adev->srbm_mutex); 3747 mutex_init(&adev->gfx.pipe_reserve_mutex); 3748 mutex_init(&adev->gfx.gfx_off_mutex); 3749 mutex_init(&adev->gfx.partition_mutex); 3750 mutex_init(&adev->grbm_idx_mutex); 3751 mutex_init(&adev->mn_lock); 3752 mutex_init(&adev->virt.vf_errors.lock); 3753 hash_init(adev->mn_hash); 3754 mutex_init(&adev->psp.mutex); 3755 mutex_init(&adev->psp.ptl.mutex); 3756 mutex_init(&adev->notifier_lock); 3757 mutex_init(&adev->pm.stable_pstate_ctx_lock); 3758 mutex_init(&adev->benchmark_mutex); 3759 mutex_init(&adev->gfx.reset_sem_mutex); 3760 3761 /* Associate locks with lockdep classes for ordering validation */ 3762 amdgpu_lockdep_set_class(adev); 3763 /* Initialize the mutex for cleaner shader isolation between GFX and compute processes */ 3764 mutex_init(&adev->enforce_isolation_mutex); 3765 for (i = 0; i < MAX_XCP; ++i) { 3766 adev->isolation[i].spearhead = dma_fence_get_stub(); 3767 amdgpu_sync_create(&adev->isolation[i].active); 3768 amdgpu_sync_create(&adev->isolation[i].prev); 3769 } 3770 mutex_init(&adev->gfx.userq_sch_mutex); 3771 mutex_init(&adev->gfx.workload_profile_mutex); 3772 mutex_init(&adev->vcn.workload_profile_mutex); 3773 3774 spin_lock_init(&adev->irq.lock); 3775 3776 amdgpu_early_init_rlc_reg_funcs(adev); 3777 amdgpu_device_init_apu_flags(adev); 3778 3779 r = amdgpu_device_check_arguments(adev); 3780 if (r) 3781 return r; 3782 3783 spin_lock_init(&adev->mmio_idx_lock); 3784 spin_lock_init(&adev->mm_stats.lock); 3785 spin_lock_init(&adev->virt.rlcg_reg_lock); 3786 spin_lock_init(&adev->wb.lock); 3787 3788 INIT_LIST_HEAD(&adev->reset_list); 3789 3790 INIT_LIST_HEAD(&adev->ras_list); 3791 3792 INIT_LIST_HEAD(&adev->pm.od_kobj_list); 3793 3794 xa_init_flags(&adev->userq_doorbell_xa, XA_FLAGS_LOCK_IRQ); 3795 3796 INIT_DELAYED_WORK(&adev->delayed_init_work, 3797 amdgpu_device_delayed_init_work_handler); 3798 INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work, 3799 amdgpu_device_delay_enable_gfx_off); 3800 /* 3801 * Initialize the enforce_isolation work structures for each XCP 3802 * partition. This work handler is responsible for enforcing shader 3803 * isolation on AMD GPUs. It counts the number of emitted fences for 3804 * each GFX and compute ring. If there are any fences, it schedules 3805 * the `enforce_isolation_work` to be run after a delay. If there are 3806 * no fences, it signals the Kernel Fusion Driver (KFD) to resume the 3807 * runqueue. 3808 */ 3809 for (i = 0; i < MAX_XCP; i++) { 3810 INIT_DELAYED_WORK(&adev->gfx.enforce_isolation[i].work, 3811 amdgpu_gfx_enforce_isolation_handler); 3812 adev->gfx.enforce_isolation[i].adev = adev; 3813 adev->gfx.enforce_isolation[i].xcp_id = i; 3814 } 3815 3816 INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func); 3817 3818 amdgpu_coredump_init(adev); 3819 3820 adev->gfx.gfx_off_req_count = 1; 3821 adev->gfx.gfx_off_residency = 0; 3822 adev->gfx.gfx_off_entrycount = 0; 3823 adev->pm.ac_power = power_supply_is_system_supplied() > 0; 3824 3825 atomic_set(&adev->throttling_logging_enabled, 1); 3826 /* 3827 * If throttling continues, logging will be performed every minute 3828 * to avoid log flooding. "-1" is subtracted since the thermal 3829 * throttling interrupt comes every second. Thus, the total logging 3830 * interval is 59 seconds(retelimited printk interval) + 1(waiting 3831 * for throttling interrupt) = 60 seconds. 3832 */ 3833 ratelimit_state_init(&adev->throttling_logging_rs, (60 - 1) * HZ, 1); 3834 3835 ratelimit_set_flags(&adev->throttling_logging_rs, RATELIMIT_MSG_ON_RELEASE); 3836 3837 /* Registers mapping */ 3838 /* TODO: block userspace mapping of io register */ 3839 if (adev->asic_type >= CHIP_BONAIRE) { 3840 adev->rmmio_base = pci_resource_start(adev->pdev, 5); 3841 adev->rmmio_size = pci_resource_len(adev->pdev, 5); 3842 } else { 3843 adev->rmmio_base = pci_resource_start(adev->pdev, 2); 3844 adev->rmmio_size = pci_resource_len(adev->pdev, 2); 3845 } 3846 3847 for (i = 0; i < AMD_IP_BLOCK_TYPE_NUM; i++) 3848 atomic_set(&adev->pm.pwr_state[i], POWER_STATE_UNKNOWN); 3849 3850 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size); 3851 if (!adev->rmmio) 3852 return -ENOMEM; 3853 3854 dev_info(adev->dev, "register mmio base: 0x%08X\n", 3855 (uint32_t)adev->rmmio_base); 3856 dev_info(adev->dev, "register mmio size: %u\n", 3857 (unsigned int)adev->rmmio_size); 3858 3859 /* 3860 * Reset domain needs to be present early, before XGMI hive discovered 3861 * (if any) and initialized to use reset sem and in_gpu reset flag 3862 * early on during init and before calling to RREG32. 3863 */ 3864 adev->reset_domain = amdgpu_reset_create_reset_domain(SINGLE_DEVICE, "amdgpu-reset-dev"); 3865 if (!adev->reset_domain) 3866 return -ENOMEM; 3867 3868 /* detect hw virtualization here */ 3869 amdgpu_virt_init(adev); 3870 3871 amdgpu_device_get_pcie_info(adev); 3872 3873 r = amdgpu_device_get_job_timeout_settings(adev); 3874 if (r) { 3875 dev_err(adev->dev, "invalid lockup_timeout parameter syntax\n"); 3876 return r; 3877 } 3878 3879 amdgpu_device_set_mcbp(adev); 3880 3881 /* 3882 * By default, use default mode where all blocks are expected to be 3883 * initialized. At present a 'swinit' of blocks is required to be 3884 * completed before the need for a different level is detected. 3885 */ 3886 amdgpu_set_init_level(adev, AMDGPU_INIT_LEVEL_DEFAULT); 3887 3888 amdgpu_device_check_iommu_direct_map(adev); 3889 3890 /* early init functions */ 3891 r = amdgpu_device_ip_early_init(adev); 3892 if (r) 3893 return r; 3894 3895 /* 3896 * No need to remove conflicting FBs for non-display class devices. 3897 * This prevents the sysfb from being freed accidently. 3898 */ 3899 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA || 3900 (pdev->class >> 8) == PCI_CLASS_DISPLAY_OTHER) { 3901 /* Get rid of things like offb */ 3902 r = aperture_remove_conflicting_pci_devices(adev->pdev, amdgpu_kms_driver.name); 3903 if (r) 3904 return r; 3905 } 3906 3907 /* Enable TMZ based on IP_VERSION */ 3908 amdgpu_gmc_tmz_set(adev); 3909 3910 if (amdgpu_sriov_vf(adev) && 3911 amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(10, 3, 0)) 3912 /* VF MMIO access (except mailbox range) from CPU 3913 * will be blocked during sriov runtime 3914 */ 3915 adev->virt.caps |= AMDGPU_VF_MMIO_ACCESS_PROTECT; 3916 3917 amdgpu_gmc_noretry_set(adev); 3918 /* Need to get xgmi info early to decide the reset behavior*/ 3919 if (adev->gmc.xgmi.supported) { 3920 if (adev->gfxhub.funcs && 3921 adev->gfxhub.funcs->get_xgmi_info) { 3922 r = adev->gfxhub.funcs->get_xgmi_info(adev); 3923 if (r) 3924 return r; 3925 } 3926 } 3927 3928 if (adev->gmc.xgmi.connected_to_cpu) { 3929 if (adev->mmhub.funcs && 3930 adev->mmhub.funcs->get_xgmi_info) { 3931 r = adev->mmhub.funcs->get_xgmi_info(adev); 3932 if (r) 3933 return r; 3934 } 3935 } 3936 3937 /* enable PCIE atomic ops */ 3938 if (amdgpu_sriov_vf(adev)) { 3939 if (adev->virt.fw_reserve.p_pf2vf) 3940 adev->have_atomics_support = ((struct amd_sriov_msg_pf2vf_info *) 3941 adev->virt.fw_reserve.p_pf2vf)->pcie_atomic_ops_support_flags == 3942 (PCI_EXP_DEVCAP2_ATOMIC_COMP32 | PCI_EXP_DEVCAP2_ATOMIC_COMP64); 3943 /* APUs w/ gfx9 onwards doesn't reply on PCIe atomics, rather it is a 3944 * internal path natively support atomics, set have_atomics_support to true. 3945 */ 3946 } else if ((adev->flags & AMD_IS_APU && 3947 amdgpu_ip_version(adev, GC_HWIP, 0) > IP_VERSION(9, 0, 0)) || 3948 (adev->gmc.xgmi.connected_to_cpu && 3949 amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(12, 1, 0))) { 3950 adev->have_atomics_support = true; 3951 } else { 3952 adev->have_atomics_support = 3953 !pci_enable_atomic_ops_to_root(adev->pdev, 3954 PCI_EXP_DEVCAP2_ATOMIC_COMP32 | 3955 PCI_EXP_DEVCAP2_ATOMIC_COMP64); 3956 } 3957 3958 if (!adev->have_atomics_support) 3959 dev_info(adev->dev, "PCIE atomic ops is not supported\n"); 3960 3961 /* doorbell bar mapping and doorbell index init*/ 3962 amdgpu_doorbell_init(adev); 3963 3964 if (amdgpu_emu_mode == 1) { 3965 /* post the asic on emulation mode */ 3966 emu_soc_asic_init(adev); 3967 goto fence_driver_init; 3968 } 3969 3970 amdgpu_reset_init(adev); 3971 3972 /* detect if we are with an SRIOV vbios */ 3973 if (adev->bios) 3974 amdgpu_device_detect_sriov_bios(adev); 3975 3976 /* check if we need to reset the asic 3977 * E.g., driver was not cleanly unloaded previously, etc. 3978 */ 3979 if (!amdgpu_sriov_vf(adev) && amdgpu_asic_need_reset_on_init(adev)) { 3980 if (adev->gmc.xgmi.num_physical_nodes) { 3981 dev_info(adev->dev, "Pending hive reset.\n"); 3982 amdgpu_set_init_level(adev, 3983 AMDGPU_INIT_LEVEL_MINIMAL_XGMI); 3984 } else { 3985 tmp = amdgpu_reset_method; 3986 /* It should do a default reset when loading or reloading the driver, 3987 * regardless of the module parameter reset_method. 3988 */ 3989 amdgpu_reset_method = AMD_RESET_METHOD_NONE; 3990 r = amdgpu_asic_reset(adev); 3991 amdgpu_reset_method = tmp; 3992 } 3993 3994 if (r) { 3995 dev_err(adev->dev, "asic reset on init failed\n"); 3996 goto failed; 3997 } 3998 } 3999 4000 /* Post card if necessary */ 4001 if (amdgpu_device_need_post(adev)) { 4002 if (!adev->bios) { 4003 dev_err(adev->dev, "no vBIOS found\n"); 4004 r = -EINVAL; 4005 goto failed; 4006 } 4007 dev_info(adev->dev, "GPU posting now...\n"); 4008 r = amdgpu_device_asic_init(adev); 4009 if (r) { 4010 dev_err(adev->dev, "gpu post error!\n"); 4011 goto failed; 4012 } 4013 } 4014 4015 if (adev->bios) { 4016 if (adev->is_atom_fw) { 4017 /* Initialize clocks */ 4018 r = amdgpu_atomfirmware_get_clock_info(adev); 4019 if (r) { 4020 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n"); 4021 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0); 4022 goto failed; 4023 } 4024 } else { 4025 /* Initialize clocks */ 4026 r = amdgpu_atombios_get_clock_info(adev); 4027 if (r) { 4028 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n"); 4029 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0); 4030 goto failed; 4031 } 4032 /* init i2c buses */ 4033 amdgpu_i2c_init(adev); 4034 } 4035 } 4036 4037 fence_driver_init: 4038 /* Fence driver */ 4039 r = amdgpu_fence_driver_sw_init(adev); 4040 if (r) { 4041 dev_err(adev->dev, "amdgpu_fence_driver_sw_init failed\n"); 4042 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0); 4043 goto failed; 4044 } 4045 4046 /* init the mode config */ 4047 drm_mode_config_init(adev_to_drm(adev)); 4048 4049 r = amdgpu_device_ip_init(adev); 4050 if (r) { 4051 dev_err(adev->dev, "amdgpu_device_ip_init failed\n"); 4052 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0); 4053 goto release_ras_con; 4054 } 4055 4056 amdgpu_fence_driver_hw_init(adev); 4057 4058 dev_info(adev->dev, 4059 "SE %d, SH per SE %d, CU per SH %d, active_cu_number %d\n", 4060 adev->gfx.config.max_shader_engines, 4061 adev->gfx.config.max_sh_per_se, 4062 adev->gfx.config.max_cu_per_sh, 4063 adev->gfx.cu_info.number); 4064 4065 adev->accel_working = true; 4066 4067 amdgpu_vm_check_compute_bug(adev); 4068 4069 /* Initialize the buffer migration limit. */ 4070 if (amdgpu_moverate >= 0) 4071 max_MBps = amdgpu_moverate; 4072 else 4073 max_MBps = 8; /* Allow 8 MB/s. */ 4074 /* Get a log2 for easy divisions. */ 4075 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps)); 4076 4077 /* 4078 * Register gpu instance before amdgpu_device_enable_mgpu_fan_boost. 4079 * Otherwise the mgpu fan boost feature will be skipped due to the 4080 * gpu instance is counted less. 4081 */ 4082 amdgpu_register_gpu_instance(adev); 4083 4084 /* enable clockgating, etc. after ib tests, etc. since some blocks require 4085 * explicit gating rather than handling it automatically. 4086 */ 4087 if (adev->init_lvl->level != AMDGPU_INIT_LEVEL_MINIMAL_XGMI) { 4088 r = amdgpu_device_ip_late_init(adev); 4089 if (r) { 4090 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n"); 4091 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r); 4092 goto release_ras_con; 4093 } 4094 /* must succeed. */ 4095 amdgpu_ras_resume(adev); 4096 queue_delayed_work(system_dfl_wq, &adev->delayed_init_work, 4097 msecs_to_jiffies(AMDGPU_RESUME_MS)); 4098 } 4099 4100 if (amdgpu_sriov_vf(adev)) { 4101 amdgpu_virt_release_full_gpu(adev, true); 4102 flush_delayed_work(&adev->delayed_init_work); 4103 } 4104 4105 /* Don't init kfd if whole hive need to be reset during init */ 4106 if (adev->init_lvl->level != AMDGPU_INIT_LEVEL_MINIMAL_XGMI) { 4107 kgd2kfd_init_zone_device(adev); 4108 kfd_update_svm_support_properties(adev); 4109 } 4110 4111 if (adev->init_lvl->level == AMDGPU_INIT_LEVEL_MINIMAL_XGMI) 4112 amdgpu_xgmi_reset_on_init(adev); 4113 4114 /* 4115 * Place those sysfs registering after `late_init`. As some of those 4116 * operations performed in `late_init` might affect the sysfs 4117 * interfaces creating. 4118 */ 4119 r = amdgpu_device_sys_interface_init(adev); 4120 4121 if (IS_ENABLED(CONFIG_PERF_EVENTS)) 4122 r = amdgpu_pmu_init(adev); 4123 if (r) 4124 dev_err(adev->dev, "amdgpu_pmu_init failed\n"); 4125 4126 /* Have stored pci confspace at hand for restore in sudden PCI error */ 4127 if (amdgpu_device_cache_pci_state(adev->pdev)) 4128 pci_restore_state(pdev); 4129 4130 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */ 4131 /* this will fail for cards that aren't VGA class devices, just 4132 * ignore it 4133 */ 4134 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) 4135 vga_client_register(adev->pdev, amdgpu_device_vga_set_decode); 4136 4137 px = amdgpu_device_supports_px(adev); 4138 4139 if (px || (!dev_is_removable(&adev->pdev->dev) && 4140 apple_gmux_detect(NULL, NULL))) 4141 vga_switcheroo_register_client(adev->pdev, 4142 &amdgpu_switcheroo_ops, px); 4143 4144 if (px) 4145 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain); 4146 4147 adev->pm_nb.notifier_call = amdgpu_device_pm_notifier; 4148 r = register_pm_notifier(&adev->pm_nb); 4149 if (r) 4150 goto failed; 4151 4152 return 0; 4153 4154 release_ras_con: 4155 if (amdgpu_sriov_vf(adev)) 4156 amdgpu_virt_release_full_gpu(adev, true); 4157 4158 /* failed in exclusive mode due to timeout */ 4159 if (amdgpu_sriov_vf(adev) && 4160 !amdgpu_sriov_runtime(adev) && 4161 amdgpu_virt_mmio_blocked(adev) && 4162 !amdgpu_virt_wait_reset(adev)) { 4163 dev_err(adev->dev, "VF exclusive mode timeout\n"); 4164 /* Don't send request since VF is inactive. */ 4165 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME; 4166 adev->virt.ops = NULL; 4167 r = -EAGAIN; 4168 } 4169 amdgpu_release_ras_context(adev); 4170 4171 failed: 4172 amdgpu_vf_error_trans_all(adev); 4173 4174 return r; 4175 } 4176 4177 static void amdgpu_device_unmap_mmio(struct amdgpu_device *adev) 4178 { 4179 4180 /* Clear all CPU mappings pointing to this device */ 4181 unmap_mapping_range(adev->ddev.anon_inode->i_mapping, 0, 0, 1); 4182 4183 /* Unmap all mapped bars - Doorbell, registers and VRAM */ 4184 amdgpu_doorbell_fini(adev); 4185 4186 iounmap(adev->rmmio); 4187 adev->rmmio = NULL; 4188 adev->mman.aper_base_kaddr = NULL; 4189 4190 /* Memory manager related */ 4191 if (!adev->gmc.xgmi.connected_to_cpu && !adev->gmc.is_app_apu) { 4192 arch_phys_wc_del(adev->gmc.vram_mtrr); 4193 arch_io_free_memtype_wc(adev->gmc.aper_base, adev->gmc.aper_size); 4194 } 4195 } 4196 4197 /** 4198 * amdgpu_device_fini_hw - tear down the driver 4199 * 4200 * @adev: amdgpu_device pointer 4201 * 4202 * Tear down the driver info (all asics). 4203 * Called at driver shutdown. 4204 */ 4205 void amdgpu_device_fini_hw(struct amdgpu_device *adev) 4206 { 4207 dev_info(adev->dev, "finishing device.\n"); 4208 flush_delayed_work(&adev->delayed_init_work); 4209 4210 if (adev->mman.initialized) 4211 drain_workqueue(adev->mman.bdev.wq); 4212 4213 adev->shutdown = true; 4214 4215 unregister_pm_notifier(&adev->pm_nb); 4216 4217 /* make sure IB test finished before entering exclusive mode 4218 * to avoid preemption on IB test 4219 */ 4220 if (amdgpu_sriov_vf(adev)) { 4221 amdgpu_virt_request_full_gpu(adev, false); 4222 amdgpu_virt_fini_data_exchange(adev); 4223 } 4224 4225 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE); 4226 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE); 4227 4228 /* disable all interrupts */ 4229 amdgpu_irq_disable_all(adev); 4230 if (adev->mode_info.mode_config_initialized) { 4231 if (!drm_drv_uses_atomic_modeset(adev_to_drm(adev))) 4232 drm_helper_force_disable_all(adev_to_drm(adev)); 4233 else 4234 drm_atomic_helper_shutdown(adev_to_drm(adev)); 4235 } 4236 amdgpu_fence_driver_hw_fini(adev); 4237 4238 amdgpu_device_sys_interface_fini(adev); 4239 4240 /* disable ras feature must before hw fini */ 4241 amdgpu_ras_pre_fini(adev); 4242 4243 amdgpu_ttm_disable_buffer_funcs(adev); 4244 4245 /* 4246 * device went through surprise hotplug; we need to destroy topology 4247 * before ip_fini_early to prevent kfd locking refcount issues by calling 4248 * amdgpu_amdkfd_suspend() 4249 */ 4250 if (pci_dev_is_disconnected(adev->pdev)) 4251 amdgpu_amdkfd_device_fini_sw(adev); 4252 4253 amdgpu_coredump_fini(adev); 4254 amdgpu_device_ip_fini_early(adev); 4255 4256 amdgpu_irq_fini_hw(adev); 4257 4258 if (adev->mman.initialized) 4259 ttm_device_clear_dma_mappings(&adev->mman.bdev); 4260 4261 amdgpu_gart_dummy_page_fini(adev); 4262 4263 if (pci_dev_is_disconnected(adev->pdev)) 4264 amdgpu_device_unmap_mmio(adev); 4265 4266 } 4267 4268 void amdgpu_device_fini_sw(struct amdgpu_device *adev) 4269 { 4270 int i, idx; 4271 bool px; 4272 4273 amdgpu_device_ip_fini(adev); 4274 amdgpu_fence_driver_sw_fini(adev); 4275 amdgpu_ucode_release(&adev->firmware.gpu_info_fw); 4276 adev->accel_working = false; 4277 dma_fence_put(rcu_dereference_protected(adev->gang_submit, true)); 4278 for (i = 0; i < MAX_XCP; ++i) { 4279 dma_fence_put(adev->isolation[i].spearhead); 4280 amdgpu_sync_free(&adev->isolation[i].active); 4281 amdgpu_sync_free(&adev->isolation[i].prev); 4282 } 4283 4284 amdgpu_reset_fini(adev); 4285 4286 /* free i2c buses */ 4287 amdgpu_i2c_fini(adev); 4288 4289 if (adev->bios) { 4290 if (amdgpu_emu_mode != 1) 4291 amdgpu_atombios_fini(adev); 4292 amdgpu_bios_release(adev); 4293 } 4294 4295 kfree(adev->fru_info); 4296 adev->fru_info = NULL; 4297 4298 kfree(adev->xcp_mgr); 4299 adev->xcp_mgr = NULL; 4300 4301 px = amdgpu_device_supports_px(adev); 4302 4303 if (px || (!dev_is_removable(&adev->pdev->dev) && 4304 apple_gmux_detect(NULL, NULL))) 4305 vga_switcheroo_unregister_client(adev->pdev); 4306 4307 if (px) 4308 vga_switcheroo_fini_domain_pm_ops(adev->dev); 4309 4310 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) 4311 vga_client_unregister(adev->pdev); 4312 4313 if (drm_dev_enter(adev_to_drm(adev), &idx)) { 4314 4315 iounmap(adev->rmmio); 4316 adev->rmmio = NULL; 4317 drm_dev_exit(idx); 4318 } 4319 4320 if (IS_ENABLED(CONFIG_PERF_EVENTS)) 4321 amdgpu_pmu_fini(adev); 4322 if (adev->discovery.bin) 4323 amdgpu_discovery_fini(adev); 4324 4325 amdgpu_reset_put_reset_domain(adev->reset_domain); 4326 adev->reset_domain = NULL; 4327 4328 kfree(adev->pci_state); 4329 kfree(adev->pcie_reset_ctx.swds_pcistate); 4330 kfree(adev->pcie_reset_ctx.swus_pcistate); 4331 } 4332 4333 /** 4334 * amdgpu_device_evict_resources - evict device resources 4335 * @adev: amdgpu device object 4336 * 4337 * Evicts all ttm device resources(vram BOs, gart table) from the lru list 4338 * of the vram memory type. Mainly used for evicting device resources 4339 * at suspend time. 4340 * 4341 */ 4342 static int amdgpu_device_evict_resources(struct amdgpu_device *adev) 4343 { 4344 int ret; 4345 4346 /* No need to evict vram on APUs unless going to S4 */ 4347 if (!adev->in_s4 && (adev->flags & AMD_IS_APU)) 4348 return 0; 4349 4350 /* No need to evict when going to S5 through S4 callbacks */ 4351 if (system_state == SYSTEM_POWER_OFF) 4352 return 0; 4353 4354 ret = amdgpu_ttm_evict_resources(adev, TTM_PL_VRAM); 4355 if (ret) { 4356 dev_warn(adev->dev, "evicting device resources failed\n"); 4357 return ret; 4358 } 4359 4360 if (adev->in_s4) { 4361 ret = ttm_device_prepare_hibernation(&adev->mman.bdev); 4362 if (ret) 4363 dev_err(adev->dev, "prepare hibernation failed, %d\n", ret); 4364 } 4365 return ret; 4366 } 4367 4368 /* 4369 * Suspend & resume. 4370 */ 4371 /** 4372 * amdgpu_device_pm_notifier - Notification block for Suspend/Hibernate events 4373 * @nb: notifier block 4374 * @mode: suspend mode 4375 * @data: data 4376 * 4377 * This function is called when the system is about to suspend or hibernate. 4378 * It is used to set the appropriate flags so that eviction can be optimized 4379 * in the pm prepare callback. 4380 */ 4381 static int amdgpu_device_pm_notifier(struct notifier_block *nb, unsigned long mode, 4382 void *data) 4383 { 4384 struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, pm_nb); 4385 4386 switch (mode) { 4387 case PM_HIBERNATION_PREPARE: 4388 adev->in_s4 = true; 4389 break; 4390 case PM_POST_HIBERNATION: 4391 adev->in_s4 = false; 4392 break; 4393 } 4394 4395 return NOTIFY_DONE; 4396 } 4397 4398 /** 4399 * amdgpu_device_prepare - prepare for device suspend 4400 * 4401 * @dev: drm dev pointer 4402 * 4403 * Prepare to put the hw in the suspend state (all asics). 4404 * Returns 0 for success or an error on failure. 4405 * Called at driver suspend. 4406 */ 4407 int amdgpu_device_prepare(struct drm_device *dev) 4408 { 4409 struct amdgpu_device *adev = drm_to_adev(dev); 4410 int i, r; 4411 4412 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) 4413 return 0; 4414 4415 /* Evict the majority of BOs before starting suspend sequence */ 4416 r = amdgpu_device_evict_resources(adev); 4417 if (r) 4418 return r; 4419 4420 flush_delayed_work(&adev->gfx.gfx_off_delay_work); 4421 4422 for (i = 0; i < adev->num_ip_blocks; i++) { 4423 if (!adev->ip_blocks[i].status.valid) 4424 continue; 4425 if (!adev->ip_blocks[i].version->funcs->prepare_suspend) 4426 continue; 4427 r = adev->ip_blocks[i].version->funcs->prepare_suspend(&adev->ip_blocks[i]); 4428 if (r) 4429 return r; 4430 } 4431 4432 return 0; 4433 } 4434 4435 /** 4436 * amdgpu_device_complete - complete power state transition 4437 * 4438 * @dev: drm dev pointer 4439 * 4440 * Undo the changes from amdgpu_device_prepare. This will be 4441 * called on all resume transitions, including those that failed. 4442 */ 4443 void amdgpu_device_complete(struct drm_device *dev) 4444 { 4445 struct amdgpu_device *adev = drm_to_adev(dev); 4446 int i; 4447 4448 for (i = 0; i < adev->num_ip_blocks; i++) { 4449 if (!adev->ip_blocks[i].status.valid) 4450 continue; 4451 if (!adev->ip_blocks[i].version->funcs->complete) 4452 continue; 4453 adev->ip_blocks[i].version->funcs->complete(&adev->ip_blocks[i]); 4454 } 4455 } 4456 4457 /** 4458 * amdgpu_device_suspend - initiate device suspend 4459 * 4460 * @dev: drm dev pointer 4461 * @notify_clients: notify in-kernel DRM clients 4462 * 4463 * Puts the hw in the suspend state (all asics). 4464 * Returns 0 for success or an error on failure. 4465 * Called at driver suspend. 4466 */ 4467 int amdgpu_device_suspend(struct drm_device *dev, bool notify_clients) 4468 { 4469 struct amdgpu_device *adev = drm_to_adev(dev); 4470 int r, rec; 4471 4472 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) 4473 return 0; 4474 4475 adev->in_suspend = true; 4476 4477 if (amdgpu_sriov_vf(adev)) { 4478 if (!adev->in_runpm) 4479 amdgpu_amdkfd_suspend_process(adev); 4480 amdgpu_virt_fini_data_exchange(adev); 4481 r = amdgpu_virt_request_full_gpu(adev, false); 4482 if (r) 4483 return r; 4484 } 4485 4486 r = amdgpu_acpi_smart_shift_update(adev, AMDGPU_SS_DEV_D3); 4487 if (r) 4488 goto unwind_sriov; 4489 4490 if (notify_clients) 4491 drm_client_dev_suspend(adev_to_drm(adev)); 4492 4493 cancel_delayed_work_sync(&adev->delayed_init_work); 4494 4495 amdgpu_ras_suspend(adev); 4496 4497 r = amdgpu_device_ip_suspend_phase1(adev); 4498 if (r) 4499 goto unwind_smartshift; 4500 4501 amdgpu_amdkfd_suspend(adev, !amdgpu_sriov_vf(adev) && !adev->in_runpm); 4502 r = amdgpu_userq_suspend(adev); 4503 if (r) 4504 goto unwind_ip_phase1; 4505 4506 r = amdgpu_device_evict_resources(adev); 4507 if (r) 4508 goto unwind_userq; 4509 4510 amdgpu_ttm_disable_buffer_funcs(adev); 4511 4512 amdgpu_fence_driver_hw_fini(adev); 4513 4514 r = amdgpu_device_ip_suspend_phase2(adev); 4515 if (r) 4516 goto unwind_evict; 4517 4518 if (amdgpu_sriov_vf(adev)) 4519 amdgpu_virt_release_full_gpu(adev, false); 4520 4521 return 0; 4522 4523 unwind_evict: 4524 amdgpu_ttm_enable_buffer_funcs(adev); 4525 amdgpu_fence_driver_hw_init(adev); 4526 4527 unwind_userq: 4528 rec = amdgpu_userq_resume(adev); 4529 if (rec) { 4530 dev_warn(adev->dev, "failed to re-initialize user queues: %d\n", rec); 4531 return r; 4532 } 4533 rec = amdgpu_amdkfd_resume(adev, !amdgpu_sriov_vf(adev) && !adev->in_runpm); 4534 if (rec) { 4535 dev_warn(adev->dev, "failed to re-initialize kfd: %d\n", rec); 4536 return r; 4537 } 4538 4539 unwind_ip_phase1: 4540 /* suspend phase 1 = resume phase 3 */ 4541 rec = amdgpu_device_ip_resume_phase3(adev); 4542 if (rec) { 4543 dev_warn(adev->dev, "failed to re-initialize IPs phase1: %d\n", rec); 4544 return r; 4545 } 4546 4547 unwind_smartshift: 4548 rec = amdgpu_acpi_smart_shift_update(adev, AMDGPU_SS_DEV_D0); 4549 if (rec) { 4550 dev_warn(adev->dev, "failed to re-update smart shift: %d\n", rec); 4551 return r; 4552 } 4553 4554 if (notify_clients) 4555 drm_client_dev_resume(adev_to_drm(adev)); 4556 4557 amdgpu_ras_resume(adev); 4558 4559 unwind_sriov: 4560 if (amdgpu_sriov_vf(adev)) { 4561 rec = amdgpu_virt_request_full_gpu(adev, true); 4562 if (rec) { 4563 dev_warn(adev->dev, "failed to reinitialize sriov: %d\n", rec); 4564 return r; 4565 } 4566 } 4567 4568 adev->in_suspend = adev->in_s0ix = adev->in_s3 = false; 4569 4570 return r; 4571 } 4572 4573 static inline int amdgpu_virt_resume(struct amdgpu_device *adev) 4574 { 4575 int r; 4576 unsigned int prev_physical_node_id = adev->gmc.xgmi.physical_node_id; 4577 4578 /* During VM resume, QEMU programming of VF MSIX table (register GFXMSIX_VECT0_ADDR_LO) 4579 * may not work. The access could be blocked by nBIF protection as VF isn't in 4580 * exclusive access mode. Exclusive access is enabled now, disable/enable MSIX 4581 * so that QEMU reprograms MSIX table. 4582 */ 4583 amdgpu_restore_msix(adev); 4584 4585 r = adev->gfxhub.funcs->get_xgmi_info(adev); 4586 if (r) 4587 return r; 4588 4589 dev_info(adev->dev, "xgmi node, old id %d, new id %d\n", 4590 prev_physical_node_id, adev->gmc.xgmi.physical_node_id); 4591 4592 adev->vm_manager.vram_base_offset = adev->gfxhub.funcs->get_mc_fb_offset(adev); 4593 adev->vm_manager.vram_base_offset += 4594 adev->gmc.xgmi.physical_node_id * adev->gmc.xgmi.node_segment_size; 4595 4596 return 0; 4597 } 4598 4599 /** 4600 * amdgpu_device_resume - initiate device resume 4601 * 4602 * @dev: drm dev pointer 4603 * @notify_clients: notify in-kernel DRM clients 4604 * 4605 * Bring the hw back to operating state (all asics). 4606 * Returns 0 for success or an error on failure. 4607 * Called at driver resume. 4608 */ 4609 int amdgpu_device_resume(struct drm_device *dev, bool notify_clients) 4610 { 4611 struct amdgpu_device *adev = drm_to_adev(dev); 4612 int r = 0; 4613 4614 if (amdgpu_sriov_vf(adev)) { 4615 r = amdgpu_virt_request_full_gpu(adev, true); 4616 if (r) 4617 return r; 4618 } 4619 4620 if (amdgpu_virt_xgmi_migrate_enabled(adev)) { 4621 r = amdgpu_virt_resume(adev); 4622 if (r) 4623 goto exit; 4624 } 4625 4626 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) 4627 return 0; 4628 4629 if (adev->in_s0ix) 4630 amdgpu_dpm_gfx_state_change(adev, sGpuChangeState_D0Entry); 4631 4632 /* post card */ 4633 if (amdgpu_device_need_post(adev)) { 4634 r = amdgpu_device_asic_init(adev); 4635 if (r) 4636 dev_err(adev->dev, "amdgpu asic init failed\n"); 4637 } 4638 4639 r = amdgpu_device_ip_resume(adev); 4640 4641 if (r) { 4642 dev_err(adev->dev, "amdgpu_device_ip_resume failed (%d).\n", r); 4643 goto exit; 4644 } 4645 4646 r = amdgpu_amdkfd_resume(adev, !amdgpu_sriov_vf(adev) && !adev->in_runpm); 4647 if (r) 4648 goto exit; 4649 4650 r = amdgpu_userq_resume(adev); 4651 if (r) 4652 goto exit; 4653 4654 r = amdgpu_device_ip_late_init(adev); 4655 if (r) 4656 goto exit; 4657 4658 queue_delayed_work(system_dfl_wq, &adev->delayed_init_work, 4659 msecs_to_jiffies(AMDGPU_RESUME_MS)); 4660 exit: 4661 if (amdgpu_sriov_vf(adev)) { 4662 amdgpu_virt_init_data_exchange(adev); 4663 amdgpu_virt_release_full_gpu(adev, true); 4664 4665 if (!r && !adev->in_runpm) 4666 r = amdgpu_amdkfd_resume_process(adev); 4667 } 4668 4669 if (r) 4670 return r; 4671 4672 /* Make sure IB tests flushed */ 4673 flush_delayed_work(&adev->delayed_init_work); 4674 4675 if (notify_clients) 4676 drm_client_dev_resume(adev_to_drm(adev)); 4677 4678 amdgpu_ras_resume(adev); 4679 4680 if (adev->mode_info.num_crtc) { 4681 /* 4682 * Most of the connector probing functions try to acquire runtime pm 4683 * refs to ensure that the GPU is powered on when connector polling is 4684 * performed. Since we're calling this from a runtime PM callback, 4685 * trying to acquire rpm refs will cause us to deadlock. 4686 * 4687 * Since we're guaranteed to be holding the rpm lock, it's safe to 4688 * temporarily disable the rpm helpers so this doesn't deadlock us. 4689 */ 4690 #ifdef CONFIG_PM 4691 dev->dev->power.disable_depth++; 4692 #endif 4693 if (!adev->dc_enabled) 4694 drm_helper_hpd_irq_event(dev); 4695 else 4696 drm_kms_helper_hotplug_event(dev); 4697 #ifdef CONFIG_PM 4698 dev->dev->power.disable_depth--; 4699 #endif 4700 } 4701 4702 amdgpu_vram_mgr_clear_reset_blocks(adev); 4703 adev->in_suspend = false; 4704 4705 if (amdgpu_acpi_smart_shift_update(adev, AMDGPU_SS_DEV_D0)) 4706 dev_warn(adev->dev, "smart shift update failed\n"); 4707 4708 return 0; 4709 } 4710 4711 /** 4712 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf 4713 * 4714 * @adev: amdgpu_device pointer 4715 * @reset_context: amdgpu reset context pointer 4716 * 4717 * do VF FLR and reinitialize Asic 4718 * return 0 means succeeded otherwise failed 4719 */ 4720 static int amdgpu_device_reset_sriov(struct amdgpu_device *adev, 4721 struct amdgpu_reset_context *reset_context) 4722 { 4723 int r; 4724 struct amdgpu_hive_info *hive = NULL; 4725 4726 if (test_bit(AMDGPU_HOST_FLR, &reset_context->flags)) { 4727 if (!amdgpu_ras_get_fed_status(adev)) 4728 amdgpu_virt_ready_to_reset(adev); 4729 amdgpu_virt_wait_reset(adev); 4730 clear_bit(AMDGPU_HOST_FLR, &reset_context->flags); 4731 r = amdgpu_virt_request_full_gpu(adev, true); 4732 } else { 4733 r = amdgpu_virt_reset_gpu(adev); 4734 } 4735 if (r) 4736 return r; 4737 4738 amdgpu_ras_clear_err_state(adev); 4739 amdgpu_irq_gpu_reset_resume_helper(adev); 4740 4741 /* some sw clean up VF needs to do before recover */ 4742 amdgpu_virt_post_reset(adev); 4743 4744 /* Resume IP prior to SMC */ 4745 r = amdgpu_device_ip_reinit_early_sriov(adev); 4746 if (r) 4747 return r; 4748 4749 amdgpu_virt_init_data_exchange(adev); 4750 4751 r = amdgpu_device_fw_loading(adev); 4752 if (r) 4753 return r; 4754 4755 /* now we are okay to resume SMC/CP/SDMA */ 4756 r = amdgpu_device_ip_reinit_late_sriov(adev); 4757 if (r) 4758 return r; 4759 4760 hive = amdgpu_get_xgmi_hive(adev); 4761 /* Update PSP FW topology after reset */ 4762 if (hive && adev->gmc.xgmi.num_physical_nodes > 1) 4763 r = amdgpu_xgmi_update_topology(hive, adev); 4764 if (hive) 4765 amdgpu_put_xgmi_hive(hive); 4766 if (r) 4767 return r; 4768 4769 r = amdgpu_ib_ring_tests(adev); 4770 if (r) 4771 return r; 4772 4773 if (adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) 4774 amdgpu_inc_vram_lost(adev); 4775 4776 /* need to be called during full access so we can't do it later like 4777 * bare-metal does. 4778 */ 4779 amdgpu_amdkfd_post_reset(adev); 4780 amdgpu_virt_release_full_gpu(adev, true); 4781 4782 /* Aldebaran and gfx_11_0_3 support ras in SRIOV, so need resume ras during reset */ 4783 if (amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 2) || 4784 amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 3) || 4785 amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 4) || 4786 amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 5, 0) || 4787 amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(11, 0, 3)) 4788 amdgpu_ras_resume(adev); 4789 4790 amdgpu_virt_ras_telemetry_post_reset(adev); 4791 4792 return 0; 4793 } 4794 4795 /** 4796 * amdgpu_device_has_job_running - check if there is any unfinished job 4797 * 4798 * @adev: amdgpu_device pointer 4799 * 4800 * check if there is any job running on the device when guest driver receives 4801 * FLR notification from host driver. If there are still jobs running, then 4802 * the guest driver will not respond the FLR reset. Instead, let the job hit 4803 * the timeout and guest driver then issue the reset request. 4804 */ 4805 bool amdgpu_device_has_job_running(struct amdgpu_device *adev) 4806 { 4807 int i; 4808 4809 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 4810 struct amdgpu_ring *ring = adev->rings[i]; 4811 4812 if (!amdgpu_ring_sched_ready(ring)) 4813 continue; 4814 4815 if (amdgpu_fence_count_emitted(ring)) 4816 return true; 4817 } 4818 return false; 4819 } 4820 4821 /** 4822 * amdgpu_device_should_recover_gpu - check if we should try GPU recovery 4823 * 4824 * @adev: amdgpu_device pointer 4825 * 4826 * Check amdgpu_gpu_recovery and SRIOV status to see if we should try to recover 4827 * a hung GPU. 4828 */ 4829 bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev) 4830 { 4831 4832 if (amdgpu_gpu_recovery == 0) 4833 goto disabled; 4834 4835 /* Skip soft reset check in fatal error mode */ 4836 if (!amdgpu_ras_is_poison_mode_supported(adev)) 4837 return true; 4838 4839 if (amdgpu_sriov_vf(adev)) 4840 return true; 4841 4842 if (amdgpu_gpu_recovery == -1) { 4843 switch (adev->asic_type) { 4844 #ifdef CONFIG_DRM_AMDGPU_SI 4845 case CHIP_VERDE: 4846 case CHIP_TAHITI: 4847 case CHIP_PITCAIRN: 4848 case CHIP_OLAND: 4849 case CHIP_HAINAN: 4850 #endif 4851 #ifdef CONFIG_DRM_AMDGPU_CIK 4852 case CHIP_KAVERI: 4853 case CHIP_KABINI: 4854 case CHIP_MULLINS: 4855 #endif 4856 case CHIP_CARRIZO: 4857 case CHIP_STONEY: 4858 case CHIP_CYAN_SKILLFISH: 4859 goto disabled; 4860 default: 4861 break; 4862 } 4863 } 4864 4865 return true; 4866 4867 disabled: 4868 dev_info(adev->dev, "GPU recovery disabled.\n"); 4869 return false; 4870 } 4871 4872 int amdgpu_device_mode1_reset(struct amdgpu_device *adev) 4873 { 4874 u32 i; 4875 int ret = 0; 4876 4877 if (adev->bios) 4878 amdgpu_atombios_scratch_regs_engine_hung(adev, true); 4879 4880 dev_info(adev->dev, "GPU mode1 reset\n"); 4881 4882 /* Cache the state before bus master disable. The saved config space 4883 * values are used in other cases like restore after mode-2 reset. 4884 */ 4885 amdgpu_device_cache_pci_state(adev->pdev); 4886 4887 /* disable BM */ 4888 pci_clear_master(adev->pdev); 4889 4890 if (amdgpu_dpm_is_mode1_reset_supported(adev)) { 4891 dev_info(adev->dev, "GPU smu mode1 reset\n"); 4892 ret = amdgpu_dpm_mode1_reset(adev); 4893 } else { 4894 dev_info(adev->dev, "GPU psp mode1 reset\n"); 4895 ret = psp_gpu_reset(adev); 4896 } 4897 4898 if (ret) 4899 goto mode1_reset_failed; 4900 4901 /* enable mmio access after mode 1 reset completed */ 4902 adev->no_hw_access = false; 4903 4904 /* ensure no_hw_access is updated before we access hw */ 4905 smp_mb(); 4906 4907 amdgpu_device_load_pci_state(adev->pdev); 4908 ret = amdgpu_psp_wait_for_bootloader(adev); 4909 if (ret) 4910 goto mode1_reset_failed; 4911 4912 /* wait for asic to come out of reset */ 4913 for (i = 0; i < adev->usec_timeout; i++) { 4914 u32 memsize = adev->nbio.funcs->get_memsize(adev); 4915 4916 if (memsize != 0xffffffff) 4917 break; 4918 udelay(1); 4919 } 4920 4921 if (i >= adev->usec_timeout) { 4922 ret = -ETIMEDOUT; 4923 goto mode1_reset_failed; 4924 } 4925 4926 if (adev->bios) 4927 amdgpu_atombios_scratch_regs_engine_hung(adev, false); 4928 4929 return 0; 4930 4931 mode1_reset_failed: 4932 dev_err(adev->dev, "GPU mode1 reset failed\n"); 4933 return ret; 4934 } 4935 4936 int amdgpu_device_link_reset(struct amdgpu_device *adev) 4937 { 4938 int ret = 0; 4939 4940 dev_info(adev->dev, "GPU link reset\n"); 4941 4942 if (!amdgpu_reset_in_dpc(adev)) 4943 ret = amdgpu_dpm_link_reset(adev); 4944 4945 if (ret) 4946 goto link_reset_failed; 4947 4948 ret = amdgpu_psp_wait_for_bootloader(adev); 4949 if (ret) 4950 goto link_reset_failed; 4951 4952 return 0; 4953 4954 link_reset_failed: 4955 dev_err(adev->dev, "GPU link reset failed\n"); 4956 return ret; 4957 } 4958 4959 int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev, 4960 struct amdgpu_reset_context *reset_context) 4961 { 4962 struct amdgpu_job *job = NULL; 4963 struct dma_fence *fence = NULL; 4964 struct amdgpu_device *tmp_adev = reset_context->reset_req_dev; 4965 bool need_full_reset = 4966 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags); 4967 int i, r; 4968 4969 if (reset_context->reset_req_dev == adev) 4970 job = reset_context->job; 4971 4972 if (amdgpu_sriov_vf(adev)) 4973 amdgpu_virt_pre_reset(adev); 4974 4975 amdgpu_fence_driver_isr_toggle(adev, true); 4976 4977 if (job) 4978 fence = &job->hw_fence->base; 4979 4980 /* block all schedulers and reset given job's ring */ 4981 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 4982 struct amdgpu_ring *ring = adev->rings[i]; 4983 4984 if (!amdgpu_ring_sched_ready(ring)) 4985 continue; 4986 4987 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */ 4988 amdgpu_fence_driver_force_completion(ring, fence); 4989 } 4990 4991 amdgpu_fence_driver_isr_toggle(adev, false); 4992 4993 r = amdgpu_reset_prepare_hwcontext(adev, reset_context); 4994 /* If reset handler not implemented, continue; otherwise return */ 4995 if (r == -EOPNOTSUPP) 4996 r = 0; 4997 else 4998 return r; 4999 5000 /* Don't suspend on bare metal if we are not going to HW reset the ASIC */ 5001 if (!amdgpu_sriov_vf(adev)) { 5002 need_full_reset = true; 5003 5004 if (!test_bit(AMDGPU_SKIP_COREDUMP, &reset_context->flags)) { 5005 dev_info(tmp_adev->dev, "Dumping IP State\n"); 5006 /* Trigger ip dump before we reset the asic */ 5007 for (i = 0; i < tmp_adev->num_ip_blocks; i++) 5008 if (tmp_adev->ip_blocks[i].version->funcs->dump_ip_state) 5009 tmp_adev->ip_blocks[i].version->funcs 5010 ->dump_ip_state((void *)&tmp_adev->ip_blocks[i]); 5011 dev_info(tmp_adev->dev, "Dumping IP State Completed\n"); 5012 } 5013 5014 if (need_full_reset) 5015 r = amdgpu_device_ip_suspend(adev); 5016 if (need_full_reset) 5017 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags); 5018 else 5019 clear_bit(AMDGPU_NEED_FULL_RESET, 5020 &reset_context->flags); 5021 } 5022 5023 return r; 5024 } 5025 5026 int amdgpu_device_reinit_after_reset(struct amdgpu_reset_context *reset_context) 5027 { 5028 struct list_head *device_list_handle; 5029 bool full_reset, vram_lost = false; 5030 struct amdgpu_device *tmp_adev; 5031 int r, init_level; 5032 5033 device_list_handle = reset_context->reset_device_list; 5034 5035 if (!device_list_handle) 5036 return -EINVAL; 5037 5038 full_reset = test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags); 5039 5040 /** 5041 * If it's reset on init, it's default init level, otherwise keep level 5042 * as recovery level. 5043 */ 5044 if (reset_context->method == AMD_RESET_METHOD_ON_INIT) 5045 init_level = AMDGPU_INIT_LEVEL_DEFAULT; 5046 else 5047 init_level = AMDGPU_INIT_LEVEL_RESET_RECOVERY; 5048 5049 r = 0; 5050 list_for_each_entry(tmp_adev, device_list_handle, reset_list) { 5051 amdgpu_set_init_level(tmp_adev, init_level); 5052 if (full_reset) { 5053 /* post card */ 5054 amdgpu_reset_set_dpc_status(tmp_adev, false); 5055 amdgpu_ras_clear_err_state(tmp_adev); 5056 r = amdgpu_device_asic_init(tmp_adev); 5057 if (r) { 5058 dev_warn(tmp_adev->dev, "asic atom init failed!"); 5059 } else { 5060 dev_info(tmp_adev->dev, "GPU reset succeeded, trying to resume\n"); 5061 5062 r = amdgpu_device_ip_resume_phase1(tmp_adev); 5063 if (r) 5064 goto out; 5065 5066 vram_lost = amdgpu_device_check_vram_lost(tmp_adev); 5067 5068 if (!test_bit(AMDGPU_SKIP_COREDUMP, &reset_context->flags)) 5069 amdgpu_coredump(tmp_adev, false, vram_lost, reset_context->job); 5070 5071 if (vram_lost) { 5072 dev_info( 5073 tmp_adev->dev, 5074 "VRAM is lost due to GPU reset!\n"); 5075 amdgpu_inc_vram_lost(tmp_adev); 5076 } 5077 5078 r = amdgpu_device_fw_loading(tmp_adev); 5079 if (r) 5080 return r; 5081 5082 r = amdgpu_xcp_restore_partition_mode( 5083 tmp_adev->xcp_mgr); 5084 if (r) 5085 goto out; 5086 5087 r = amdgpu_device_ip_resume_phase2(tmp_adev); 5088 if (r) 5089 goto out; 5090 5091 amdgpu_ttm_enable_buffer_funcs(tmp_adev); 5092 5093 r = amdgpu_device_ip_resume_phase3(tmp_adev); 5094 if (r) 5095 goto out; 5096 5097 if (vram_lost) 5098 amdgpu_device_fill_reset_magic(tmp_adev); 5099 5100 /* 5101 * Add this ASIC as tracked as reset was already 5102 * complete successfully. 5103 */ 5104 amdgpu_register_gpu_instance(tmp_adev); 5105 5106 if (!reset_context->hive && 5107 tmp_adev->gmc.xgmi.num_physical_nodes > 1) 5108 amdgpu_xgmi_add_device(tmp_adev); 5109 5110 r = amdgpu_device_ip_late_init(tmp_adev); 5111 if (r) 5112 goto out; 5113 5114 r = amdgpu_userq_post_reset(tmp_adev, vram_lost); 5115 if (r) 5116 goto out; 5117 5118 drm_client_dev_resume(adev_to_drm(tmp_adev)); 5119 5120 /* 5121 * The GPU enters bad state once faulty pages 5122 * by ECC has reached the threshold, and ras 5123 * recovery is scheduled next. So add one check 5124 * here to break recovery if it indeed exceeds 5125 * bad page threshold, and remind user to 5126 * retire this GPU or setting one bigger 5127 * bad_page_threshold value to fix this once 5128 * probing driver again. 5129 */ 5130 if (!amdgpu_ras_is_rma(tmp_adev)) { 5131 /* must succeed. */ 5132 amdgpu_ras_resume(tmp_adev); 5133 } else { 5134 r = -EINVAL; 5135 goto out; 5136 } 5137 5138 /* Update PSP FW topology after reset */ 5139 if (reset_context->hive && 5140 tmp_adev->gmc.xgmi.num_physical_nodes > 1) 5141 r = amdgpu_xgmi_update_topology( 5142 reset_context->hive, tmp_adev); 5143 } 5144 } 5145 5146 out: 5147 if (!r) { 5148 /* IP init is complete now, set level as default */ 5149 amdgpu_set_init_level(tmp_adev, 5150 AMDGPU_INIT_LEVEL_DEFAULT); 5151 amdgpu_irq_gpu_reset_resume_helper(tmp_adev); 5152 r = amdgpu_ib_ring_tests(tmp_adev); 5153 if (r) { 5154 dev_err(tmp_adev->dev, "ib ring test failed (%d).\n", r); 5155 r = -EAGAIN; 5156 goto end; 5157 } 5158 } 5159 5160 if (r) 5161 tmp_adev->asic_reset_res = r; 5162 } 5163 5164 end: 5165 return r; 5166 } 5167 5168 int amdgpu_do_asic_reset(struct list_head *device_list_handle, 5169 struct amdgpu_reset_context *reset_context) 5170 { 5171 struct amdgpu_device *tmp_adev = NULL; 5172 bool need_full_reset, skip_hw_reset; 5173 int r = 0; 5174 5175 /* Try reset handler method first */ 5176 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device, 5177 reset_list); 5178 5179 reset_context->reset_device_list = device_list_handle; 5180 r = amdgpu_reset_perform_reset(tmp_adev, reset_context); 5181 /* If reset handler not implemented, continue; otherwise return */ 5182 if (r == -EOPNOTSUPP) 5183 r = 0; 5184 else 5185 return r; 5186 5187 /* Reset handler not implemented, use the default method */ 5188 need_full_reset = 5189 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags); 5190 skip_hw_reset = test_bit(AMDGPU_SKIP_HW_RESET, &reset_context->flags); 5191 5192 /* 5193 * ASIC reset has to be done on all XGMI hive nodes ASAP 5194 * to allow proper links negotiation in FW (within 1 sec) 5195 */ 5196 if (!skip_hw_reset && need_full_reset) { 5197 list_for_each_entry(tmp_adev, device_list_handle, reset_list) { 5198 /* For XGMI run all resets in parallel to speed up the process */ 5199 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) { 5200 if (!queue_work(system_dfl_wq, 5201 &tmp_adev->xgmi_reset_work)) 5202 r = -EALREADY; 5203 } else 5204 r = amdgpu_asic_reset(tmp_adev); 5205 5206 if (r) { 5207 dev_err(tmp_adev->dev, 5208 "ASIC reset failed with error, %d for drm dev, %s", 5209 r, adev_to_drm(tmp_adev)->unique); 5210 goto out; 5211 } 5212 } 5213 5214 /* For XGMI wait for all resets to complete before proceed */ 5215 if (!r) { 5216 list_for_each_entry(tmp_adev, device_list_handle, 5217 reset_list) { 5218 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) { 5219 flush_work(&tmp_adev->xgmi_reset_work); 5220 r = tmp_adev->asic_reset_res; 5221 if (r) 5222 break; 5223 } 5224 } 5225 } 5226 } 5227 5228 if (!r && amdgpu_ras_intr_triggered()) { 5229 list_for_each_entry(tmp_adev, device_list_handle, reset_list) { 5230 amdgpu_ras_reset_error_count(tmp_adev, 5231 AMDGPU_RAS_BLOCK__MMHUB); 5232 } 5233 5234 amdgpu_ras_intr_cleared(); 5235 } 5236 5237 r = amdgpu_device_reinit_after_reset(reset_context); 5238 if (r == -EAGAIN) 5239 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags); 5240 else 5241 clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags); 5242 5243 out: 5244 return r; 5245 } 5246 5247 static void amdgpu_device_set_mp1_state(struct amdgpu_device *adev) 5248 { 5249 5250 switch (amdgpu_asic_reset_method(adev)) { 5251 case AMD_RESET_METHOD_MODE1: 5252 case AMD_RESET_METHOD_LINK: 5253 adev->mp1_state = PP_MP1_STATE_SHUTDOWN; 5254 break; 5255 case AMD_RESET_METHOD_MODE2: 5256 adev->mp1_state = PP_MP1_STATE_RESET; 5257 break; 5258 default: 5259 adev->mp1_state = PP_MP1_STATE_NONE; 5260 break; 5261 } 5262 } 5263 5264 static void amdgpu_device_unset_mp1_state(struct amdgpu_device *adev) 5265 { 5266 amdgpu_vf_error_trans_all(adev); 5267 adev->mp1_state = PP_MP1_STATE_NONE; 5268 } 5269 5270 static void amdgpu_device_resume_display_audio(struct amdgpu_device *adev) 5271 { 5272 struct pci_dev *p = NULL; 5273 5274 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus), 5275 adev->pdev->bus->number, 1); 5276 if (p) { 5277 pm_runtime_enable(&(p->dev)); 5278 pm_runtime_resume(&(p->dev)); 5279 } 5280 5281 pci_dev_put(p); 5282 } 5283 5284 static int amdgpu_device_suspend_display_audio(struct amdgpu_device *adev) 5285 { 5286 enum amd_reset_method reset_method; 5287 struct pci_dev *p = NULL; 5288 u64 expires; 5289 5290 /* 5291 * For now, only BACO and mode1 reset are confirmed 5292 * to suffer the audio issue without proper suspended. 5293 */ 5294 reset_method = amdgpu_asic_reset_method(adev); 5295 if ((reset_method != AMD_RESET_METHOD_BACO) && 5296 (reset_method != AMD_RESET_METHOD_MODE1)) 5297 return -EINVAL; 5298 5299 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus), 5300 adev->pdev->bus->number, 1); 5301 if (!p) 5302 return -ENODEV; 5303 5304 expires = pm_runtime_autosuspend_expiration(&(p->dev)); 5305 if (!expires) 5306 /* 5307 * If we cannot get the audio device autosuspend delay, 5308 * a fixed 4S interval will be used. Considering 3S is 5309 * the audio controller default autosuspend delay setting. 5310 * 4S used here is guaranteed to cover that. 5311 */ 5312 expires = ktime_get_mono_fast_ns() + NSEC_PER_SEC * 4ULL; 5313 5314 while (!pm_runtime_status_suspended(&(p->dev))) { 5315 if (!pm_runtime_suspend(&(p->dev))) 5316 break; 5317 5318 if (expires < ktime_get_mono_fast_ns()) { 5319 dev_warn(adev->dev, "failed to suspend display audio\n"); 5320 pci_dev_put(p); 5321 /* TODO: abort the succeeding gpu reset? */ 5322 return -ETIMEDOUT; 5323 } 5324 } 5325 5326 pm_runtime_disable(&(p->dev)); 5327 5328 pci_dev_put(p); 5329 return 0; 5330 } 5331 5332 static inline void amdgpu_device_stop_pending_resets(struct amdgpu_device *adev) 5333 { 5334 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 5335 5336 #if defined(CONFIG_DEBUG_FS) 5337 if (!amdgpu_sriov_vf(adev)) 5338 cancel_work(&adev->reset_work); 5339 #endif 5340 amdgpu_userq_mgr_cancel_reset_work(adev); 5341 5342 if (adev->kfd.dev) 5343 cancel_work(&adev->kfd.reset_work); 5344 5345 if (amdgpu_sriov_vf(adev)) 5346 cancel_work(&adev->virt.flr_work); 5347 5348 if (con && adev->ras_enabled) 5349 cancel_work(&con->recovery_work); 5350 5351 } 5352 5353 static int amdgpu_device_health_check(struct list_head *device_list_handle) 5354 { 5355 struct amdgpu_device *tmp_adev; 5356 int ret = 0; 5357 5358 list_for_each_entry(tmp_adev, device_list_handle, reset_list) { 5359 ret |= amdgpu_device_bus_status_check(tmp_adev); 5360 } 5361 5362 return ret; 5363 } 5364 5365 static void amdgpu_device_recovery_prepare(struct amdgpu_device *adev, 5366 struct list_head *device_list, 5367 struct amdgpu_hive_info *hive) 5368 { 5369 struct amdgpu_device *tmp_adev = NULL; 5370 5371 /* 5372 * Build list of devices to reset. 5373 * In case we are in XGMI hive mode, resort the device list 5374 * to put adev in the 1st position. 5375 */ 5376 if (!amdgpu_sriov_vf(adev) && (adev->gmc.xgmi.num_physical_nodes > 1) && hive) { 5377 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) { 5378 list_add_tail(&tmp_adev->reset_list, device_list); 5379 if (adev->shutdown) 5380 tmp_adev->shutdown = true; 5381 } 5382 if (!list_is_first(&adev->reset_list, device_list)) 5383 list_rotate_to_front(&adev->reset_list, device_list); 5384 } else { 5385 list_add_tail(&adev->reset_list, device_list); 5386 } 5387 } 5388 5389 static void amdgpu_device_recovery_get_reset_lock(struct amdgpu_device *adev, 5390 struct list_head *device_list) 5391 { 5392 struct amdgpu_device *tmp_adev = NULL; 5393 5394 if (list_empty(device_list)) 5395 return; 5396 tmp_adev = 5397 list_first_entry(device_list, struct amdgpu_device, reset_list); 5398 amdgpu_device_lock_reset_domain(tmp_adev->reset_domain); 5399 } 5400 5401 static void amdgpu_device_recovery_put_reset_lock(struct amdgpu_device *adev, 5402 struct list_head *device_list) 5403 { 5404 struct amdgpu_device *tmp_adev = NULL; 5405 5406 if (list_empty(device_list)) 5407 return; 5408 tmp_adev = 5409 list_first_entry(device_list, struct amdgpu_device, reset_list); 5410 amdgpu_device_unlock_reset_domain(tmp_adev->reset_domain); 5411 } 5412 5413 static void amdgpu_device_halt_activities(struct amdgpu_device *adev, 5414 struct amdgpu_job *job, 5415 struct amdgpu_reset_context *reset_context, 5416 struct list_head *device_list, 5417 struct amdgpu_hive_info *hive, 5418 bool need_emergency_restart) 5419 { 5420 struct amdgpu_device *tmp_adev = NULL; 5421 int i; 5422 5423 /* block all schedulers and reset given job's ring */ 5424 list_for_each_entry(tmp_adev, device_list, reset_list) { 5425 amdgpu_device_set_mp1_state(tmp_adev); 5426 5427 /* 5428 * Try to put the audio codec into suspend state 5429 * before gpu reset started. 5430 * 5431 * Due to the power domain of the graphics device 5432 * is shared with AZ power domain. Without this, 5433 * we may change the audio hardware from behind 5434 * the audio driver's back. That will trigger 5435 * some audio codec errors. 5436 */ 5437 if (!amdgpu_device_suspend_display_audio(tmp_adev)) 5438 tmp_adev->pcie_reset_ctx.audio_suspended = true; 5439 5440 amdgpu_ras_set_error_query_ready(tmp_adev, false); 5441 5442 cancel_delayed_work_sync(&tmp_adev->delayed_init_work); 5443 5444 amdgpu_amdkfd_pre_reset(tmp_adev, reset_context); 5445 5446 /* 5447 * Mark these ASICs to be reset as untracked first 5448 * And add them back after reset completed 5449 */ 5450 amdgpu_unregister_gpu_instance(tmp_adev); 5451 5452 drm_client_dev_suspend(adev_to_drm(tmp_adev)); 5453 5454 /* disable ras on ALL IPs */ 5455 if (!need_emergency_restart && !amdgpu_reset_in_dpc(adev)) 5456 amdgpu_ras_suspend(tmp_adev); 5457 5458 amdgpu_userq_pre_reset(tmp_adev); 5459 5460 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 5461 struct amdgpu_ring *ring = tmp_adev->rings[i]; 5462 5463 if (!amdgpu_ring_sched_ready(ring)) 5464 continue; 5465 5466 drm_sched_wqueue_stop(&ring->sched); 5467 5468 if (need_emergency_restart) 5469 amdgpu_job_stop_all_jobs_on_sched(&ring->sched); 5470 } 5471 atomic_inc(&tmp_adev->gpu_reset_counter); 5472 } 5473 } 5474 5475 static int amdgpu_device_asic_reset(struct amdgpu_device *adev, 5476 struct list_head *device_list, 5477 struct amdgpu_reset_context *reset_context) 5478 { 5479 struct amdgpu_device *tmp_adev = NULL; 5480 int retry_limit = AMDGPU_MAX_RETRY_LIMIT; 5481 int r = 0; 5482 5483 retry: /* Rest of adevs pre asic reset from XGMI hive. */ 5484 list_for_each_entry(tmp_adev, device_list, reset_list) { 5485 r = amdgpu_device_pre_asic_reset(tmp_adev, reset_context); 5486 /*TODO Should we stop ?*/ 5487 if (r) { 5488 dev_err(tmp_adev->dev, "GPU pre asic reset failed with err, %d for drm dev, %s ", 5489 r, adev_to_drm(tmp_adev)->unique); 5490 tmp_adev->asic_reset_res = r; 5491 } 5492 } 5493 5494 /* Actual ASIC resets if needed.*/ 5495 /* Host driver will handle XGMI hive reset for SRIOV */ 5496 if (amdgpu_sriov_vf(adev)) { 5497 5498 /* Bail out of reset early */ 5499 if (amdgpu_ras_is_rma(adev)) 5500 return -ENODEV; 5501 5502 if (amdgpu_ras_get_fed_status(adev) || amdgpu_virt_rcvd_ras_interrupt(adev)) { 5503 dev_dbg(adev->dev, "Detected RAS error, wait for FLR completion\n"); 5504 amdgpu_ras_set_fed(adev, true); 5505 set_bit(AMDGPU_HOST_FLR, &reset_context->flags); 5506 } 5507 5508 r = amdgpu_device_reset_sriov(adev, reset_context); 5509 if (AMDGPU_RETRY_SRIOV_RESET(r) && (retry_limit--) > 0) { 5510 amdgpu_virt_release_full_gpu(adev, true); 5511 goto retry; 5512 } 5513 if (r) 5514 adev->asic_reset_res = r; 5515 } else { 5516 r = amdgpu_do_asic_reset(device_list, reset_context); 5517 if (r && r == -EAGAIN) 5518 goto retry; 5519 } 5520 5521 list_for_each_entry(tmp_adev, device_list, reset_list) { 5522 /* 5523 * Drop any pending non scheduler resets queued before reset is done. 5524 * Any reset scheduled after this point would be valid. Scheduler resets 5525 * were already dropped during drm_sched_stop and no new ones can come 5526 * in before drm_sched_start. 5527 */ 5528 amdgpu_device_stop_pending_resets(tmp_adev); 5529 } 5530 5531 return r; 5532 } 5533 5534 static int amdgpu_device_sched_resume(struct list_head *device_list, 5535 struct amdgpu_reset_context *reset_context, 5536 bool job_signaled) 5537 { 5538 struct amdgpu_device *tmp_adev = NULL; 5539 int i, r = 0; 5540 5541 /* Post ASIC reset for all devs .*/ 5542 list_for_each_entry(tmp_adev, device_list, reset_list) { 5543 5544 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 5545 struct amdgpu_ring *ring = tmp_adev->rings[i]; 5546 5547 if (!amdgpu_ring_sched_ready(ring)) 5548 continue; 5549 5550 drm_sched_wqueue_start(&ring->sched); 5551 } 5552 5553 if (!drm_drv_uses_atomic_modeset(adev_to_drm(tmp_adev)) && !job_signaled) 5554 drm_helper_resume_force_mode(adev_to_drm(tmp_adev)); 5555 5556 if (tmp_adev->asic_reset_res) { 5557 /* bad news, how to tell it to userspace ? 5558 * for ras error, we should report GPU bad status instead of 5559 * reset failure 5560 */ 5561 if (reset_context->src != AMDGPU_RESET_SRC_RAS || 5562 !amdgpu_ras_eeprom_check_err_threshold(tmp_adev)) 5563 dev_info( 5564 tmp_adev->dev, 5565 "GPU reset(%d) failed with error %d\n", 5566 atomic_read( 5567 &tmp_adev->gpu_reset_counter), 5568 tmp_adev->asic_reset_res); 5569 amdgpu_vf_error_put(tmp_adev, 5570 AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, 5571 tmp_adev->asic_reset_res); 5572 if (!r) 5573 r = tmp_adev->asic_reset_res; 5574 tmp_adev->asic_reset_res = 0; 5575 } else { 5576 dev_info(tmp_adev->dev, "GPU reset(%d) succeeded!\n", 5577 atomic_read(&tmp_adev->gpu_reset_counter)); 5578 if (amdgpu_acpi_smart_shift_update(tmp_adev, 5579 AMDGPU_SS_DEV_D0)) 5580 dev_warn(tmp_adev->dev, 5581 "smart shift update failed\n"); 5582 } 5583 } 5584 5585 return r; 5586 } 5587 5588 static void amdgpu_device_gpu_resume(struct amdgpu_device *adev, 5589 struct list_head *device_list, 5590 bool need_emergency_restart) 5591 { 5592 struct amdgpu_device *tmp_adev = NULL; 5593 5594 list_for_each_entry(tmp_adev, device_list, reset_list) { 5595 /* unlock kfd: SRIOV would do it separately */ 5596 if (!need_emergency_restart && !amdgpu_sriov_vf(tmp_adev)) 5597 amdgpu_amdkfd_post_reset(tmp_adev); 5598 5599 /* kfd_post_reset will do nothing if kfd device is not initialized, 5600 * need to bring up kfd here if it's not be initialized before 5601 */ 5602 if (!adev->kfd.init_complete) 5603 amdgpu_amdkfd_device_init(adev); 5604 5605 if (tmp_adev->pcie_reset_ctx.audio_suspended) 5606 amdgpu_device_resume_display_audio(tmp_adev); 5607 5608 amdgpu_device_unset_mp1_state(tmp_adev); 5609 5610 amdgpu_ras_set_error_query_ready(tmp_adev, true); 5611 5612 } 5613 } 5614 5615 5616 /** 5617 * amdgpu_device_gpu_recover - reset the asic and recover scheduler 5618 * 5619 * @adev: amdgpu_device pointer 5620 * @job: which job trigger hang 5621 * @reset_context: amdgpu reset context pointer 5622 * 5623 * Attempt to reset the GPU if it has hung (all asics). 5624 * Attempt to do soft-reset or full-reset and reinitialize Asic 5625 * Returns 0 for success or an error on failure. 5626 */ 5627 5628 int amdgpu_device_gpu_recover(struct amdgpu_device *adev, 5629 struct amdgpu_job *job, 5630 struct amdgpu_reset_context *reset_context) 5631 { 5632 struct list_head device_list; 5633 bool job_signaled = false; 5634 struct amdgpu_hive_info *hive = NULL; 5635 int r = 0; 5636 bool need_emergency_restart = false; 5637 /* save the pasid here as the job may be freed before the end of the reset */ 5638 int pasid = job ? job->pasid : -EINVAL; 5639 5640 /* 5641 * If it reaches here because of hang/timeout and a RAS error is 5642 * detected at the same time, let RAS recovery take care of it. 5643 */ 5644 if (amdgpu_ras_is_err_state(adev, AMDGPU_RAS_BLOCK__ANY) && 5645 !amdgpu_sriov_vf(adev) && 5646 reset_context->src != AMDGPU_RESET_SRC_RAS) { 5647 dev_dbg(adev->dev, 5648 "Gpu recovery from source: %d yielding to RAS error recovery handling", 5649 reset_context->src); 5650 return 0; 5651 } 5652 5653 /* 5654 * Special case: RAS triggered and full reset isn't supported 5655 */ 5656 need_emergency_restart = amdgpu_ras_need_emergency_restart(adev); 5657 5658 /* 5659 * Flush RAM to disk so that after reboot 5660 * the user can read log and see why the system rebooted. 5661 */ 5662 if (need_emergency_restart && amdgpu_ras_get_context(adev) && 5663 amdgpu_ras_get_context(adev)->reboot) { 5664 dev_warn(adev->dev, "Emergency reboot."); 5665 5666 ksys_sync_helper(); 5667 emergency_restart(); 5668 } 5669 5670 dev_info(adev->dev, "GPU %s begin!. Source: %d\n", 5671 need_emergency_restart ? "jobs stop" : "reset", 5672 reset_context->src); 5673 5674 if (!amdgpu_sriov_vf(adev)) 5675 hive = amdgpu_get_xgmi_hive(adev); 5676 if (hive) 5677 mutex_lock(&hive->hive_lock); 5678 5679 reset_context->job = job; 5680 reset_context->hive = hive; 5681 INIT_LIST_HEAD(&device_list); 5682 5683 amdgpu_device_recovery_prepare(adev, &device_list, hive); 5684 5685 if (!amdgpu_sriov_vf(adev)) { 5686 r = amdgpu_device_health_check(&device_list); 5687 if (r) 5688 goto end_reset; 5689 } 5690 5691 /* Cannot be called after locking reset domain */ 5692 amdgpu_ras_pre_reset(adev, &device_list); 5693 5694 /* We need to lock reset domain only once both for XGMI and single device */ 5695 amdgpu_device_recovery_get_reset_lock(adev, &device_list); 5696 5697 /* unmap all the mappings of doorbell and framebuffer to prevent user space from 5698 * accessing them 5699 */ 5700 unmap_mapping_range(adev->ddev.anon_inode->i_mapping, 0, 0, 1); 5701 amdgpu_amdkfd_clear_kfd_mapping(adev); 5702 5703 amdgpu_device_halt_activities(adev, job, reset_context, &device_list, 5704 hive, need_emergency_restart); 5705 if (need_emergency_restart) 5706 goto skip_sched_resume; 5707 /* 5708 * Must check guilty signal here since after this point all old 5709 * HW fences are force signaled. 5710 * 5711 * job->base holds a reference to parent fence 5712 */ 5713 if (job && (dma_fence_get_status(&job->hw_fence->base) > 0)) { 5714 job_signaled = true; 5715 dev_info(adev->dev, "Guilty job already signaled, skipping HW reset"); 5716 goto skip_hw_reset; 5717 } 5718 5719 r = amdgpu_device_asic_reset(adev, &device_list, reset_context); 5720 if (r) 5721 goto reset_unlock; 5722 skip_hw_reset: 5723 r = amdgpu_device_sched_resume(&device_list, reset_context, job_signaled); 5724 if (r) 5725 goto reset_unlock; 5726 skip_sched_resume: 5727 amdgpu_device_gpu_resume(adev, &device_list, need_emergency_restart); 5728 reset_unlock: 5729 amdgpu_device_recovery_put_reset_lock(adev, &device_list); 5730 amdgpu_ras_post_reset(adev, &device_list); 5731 end_reset: 5732 if (hive) { 5733 mutex_unlock(&hive->hive_lock); 5734 amdgpu_put_xgmi_hive(hive); 5735 } 5736 5737 if (r) 5738 dev_info(adev->dev, "GPU reset end with ret = %d\n", r); 5739 5740 atomic_set(&adev->reset_domain->reset_res, r); 5741 5742 if (!r) { 5743 struct amdgpu_task_info *ti = NULL; 5744 5745 /* 5746 * The job may already be freed at this point via the sched tdr workqueue so 5747 * use the cached pasid. 5748 */ 5749 if (pasid >= 0) 5750 ti = amdgpu_vm_get_task_info_pasid(adev, pasid); 5751 5752 drm_dev_wedged_event(adev_to_drm(adev), DRM_WEDGE_RECOVERY_NONE, 5753 ti ? &ti->task : NULL); 5754 5755 amdgpu_vm_put_task_info(ti); 5756 } 5757 5758 return r; 5759 } 5760 5761 /** 5762 * amdgpu_device_partner_bandwidth - find the bandwidth of appropriate partner 5763 * 5764 * @adev: amdgpu_device pointer 5765 * @speed: pointer to the speed of the link 5766 * @width: pointer to the width of the link 5767 * 5768 * Evaluate the hierarchy to find the speed and bandwidth capabilities of the 5769 * first physical partner to an AMD dGPU. 5770 * This will exclude any virtual switches and links. 5771 */ 5772 static void amdgpu_device_partner_bandwidth(struct amdgpu_device *adev, 5773 enum pci_bus_speed *speed, 5774 enum pcie_link_width *width) 5775 { 5776 if (!speed || !width) 5777 return; 5778 5779 *speed = PCI_SPEED_UNKNOWN; 5780 *width = PCIE_LNK_WIDTH_UNKNOWN; 5781 5782 if (amdgpu_device_pcie_dynamic_switching_supported(adev)) { 5783 struct pci_dev *parent = amdgpu_device_find_parent(adev); 5784 5785 if (parent) { 5786 *speed = pcie_get_speed_cap(parent); 5787 *width = pcie_get_width_cap(parent); 5788 } 5789 } else { 5790 /* use the current speeds rather than max if switching is not supported */ 5791 pcie_bandwidth_available(adev->pdev, NULL, speed, width); 5792 } 5793 } 5794 5795 /** 5796 * amdgpu_device_gpu_bandwidth - find the bandwidth of the GPU 5797 * 5798 * @adev: amdgpu_device pointer 5799 * @speed: pointer to the speed of the link 5800 * @width: pointer to the width of the link 5801 * 5802 * Evaluate the hierarchy to find the speed and bandwidth capabilities of the 5803 * AMD dGPU which may be a virtual upstream bridge. 5804 */ 5805 static void amdgpu_device_gpu_bandwidth(struct amdgpu_device *adev, 5806 enum pci_bus_speed *speed, 5807 enum pcie_link_width *width) 5808 { 5809 struct pci_dev *parent = adev->pdev; 5810 5811 if (!speed || !width) 5812 return; 5813 5814 /* use the device itself */ 5815 *speed = pcie_get_speed_cap(adev->pdev); 5816 *width = pcie_get_width_cap(adev->pdev); 5817 5818 /* use the link outside the device */ 5819 parent = amdgpu_device_find_parent(adev); 5820 if (parent) { 5821 *speed = pcie_get_speed_cap(parent); 5822 *width = pcie_get_width_cap(parent); 5823 } 5824 } 5825 5826 /** 5827 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot 5828 * 5829 * @adev: amdgpu_device pointer 5830 * 5831 * Fetches and stores in the driver the PCIE capabilities (gen speed 5832 * and lanes) of the slot the device is in. Handles APUs and 5833 * virtualized environments where PCIE config space may not be available. 5834 */ 5835 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev) 5836 { 5837 enum pci_bus_speed speed_cap, platform_speed_cap; 5838 enum pcie_link_width platform_link_width, link_width; 5839 5840 if (amdgpu_pcie_gen_cap) 5841 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap; 5842 5843 if (amdgpu_pcie_lane_cap) 5844 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap; 5845 5846 /* covers APUs as well */ 5847 if (pci_is_root_bus(adev->pdev->bus) && !amdgpu_passthrough(adev)) { 5848 if (adev->pm.pcie_gen_mask == 0) 5849 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK; 5850 if (adev->pm.pcie_mlw_mask == 0) 5851 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK; 5852 return; 5853 } 5854 5855 if (adev->pm.pcie_gen_mask && adev->pm.pcie_mlw_mask) 5856 return; 5857 5858 amdgpu_device_partner_bandwidth(adev, &platform_speed_cap, 5859 &platform_link_width); 5860 amdgpu_device_gpu_bandwidth(adev, &speed_cap, &link_width); 5861 5862 if (adev->pm.pcie_gen_mask == 0) { 5863 /* asic caps */ 5864 if (speed_cap == PCI_SPEED_UNKNOWN) { 5865 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 | 5866 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 | 5867 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3); 5868 } else { 5869 if (speed_cap == PCIE_SPEED_32_0GT) 5870 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 | 5871 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 | 5872 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 | 5873 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4 | 5874 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN5); 5875 else if (speed_cap == PCIE_SPEED_16_0GT) 5876 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 | 5877 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 | 5878 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 | 5879 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4); 5880 else if (speed_cap == PCIE_SPEED_8_0GT) 5881 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 | 5882 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 | 5883 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3); 5884 else if (speed_cap == PCIE_SPEED_5_0GT) 5885 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 | 5886 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2); 5887 else 5888 adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1; 5889 } 5890 /* platform caps */ 5891 if (platform_speed_cap == PCI_SPEED_UNKNOWN) { 5892 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 | 5893 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2); 5894 } else { 5895 if (platform_speed_cap == PCIE_SPEED_32_0GT) 5896 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 | 5897 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 | 5898 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 | 5899 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4 | 5900 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN5); 5901 else if (platform_speed_cap == PCIE_SPEED_16_0GT) 5902 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 | 5903 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 | 5904 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 | 5905 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4); 5906 else if (platform_speed_cap == PCIE_SPEED_8_0GT) 5907 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 | 5908 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 | 5909 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3); 5910 else if (platform_speed_cap == PCIE_SPEED_5_0GT) 5911 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 | 5912 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2); 5913 else 5914 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1; 5915 5916 } 5917 } 5918 if (adev->pm.pcie_mlw_mask == 0) { 5919 /* asic caps */ 5920 if (link_width == PCIE_LNK_WIDTH_UNKNOWN) { 5921 adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_ASIC_PCIE_MLW_MASK; 5922 } else { 5923 switch (link_width) { 5924 case PCIE_LNK_X32: 5925 adev->pm.pcie_mlw_mask |= (CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X32 | 5926 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X16 | 5927 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X12 | 5928 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X8 | 5929 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X4 | 5930 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X2 | 5931 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X1); 5932 break; 5933 case PCIE_LNK_X16: 5934 adev->pm.pcie_mlw_mask |= (CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X16 | 5935 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X12 | 5936 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X8 | 5937 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X4 | 5938 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X2 | 5939 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X1); 5940 break; 5941 case PCIE_LNK_X12: 5942 adev->pm.pcie_mlw_mask |= (CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X12 | 5943 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X8 | 5944 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X4 | 5945 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X2 | 5946 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X1); 5947 break; 5948 case PCIE_LNK_X8: 5949 adev->pm.pcie_mlw_mask |= (CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X8 | 5950 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X4 | 5951 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X2 | 5952 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X1); 5953 break; 5954 case PCIE_LNK_X4: 5955 adev->pm.pcie_mlw_mask |= (CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X4 | 5956 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X2 | 5957 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X1); 5958 break; 5959 case PCIE_LNK_X2: 5960 adev->pm.pcie_mlw_mask |= (CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X2 | 5961 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X1); 5962 break; 5963 case PCIE_LNK_X1: 5964 adev->pm.pcie_mlw_mask |= CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X1; 5965 break; 5966 default: 5967 break; 5968 } 5969 } 5970 /* platform caps */ 5971 if (platform_link_width == PCIE_LNK_WIDTH_UNKNOWN) { 5972 adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK; 5973 } else { 5974 switch (platform_link_width) { 5975 case PCIE_LNK_X32: 5976 adev->pm.pcie_mlw_mask |= (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 | 5977 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 | 5978 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 | 5979 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 | 5980 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 | 5981 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | 5982 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); 5983 break; 5984 case PCIE_LNK_X16: 5985 adev->pm.pcie_mlw_mask |= (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 | 5986 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 | 5987 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 | 5988 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 | 5989 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | 5990 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); 5991 break; 5992 case PCIE_LNK_X12: 5993 adev->pm.pcie_mlw_mask |= (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 | 5994 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 | 5995 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 | 5996 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | 5997 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); 5998 break; 5999 case PCIE_LNK_X8: 6000 adev->pm.pcie_mlw_mask |= (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 | 6001 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 | 6002 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | 6003 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); 6004 break; 6005 case PCIE_LNK_X4: 6006 adev->pm.pcie_mlw_mask |= (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 | 6007 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | 6008 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); 6009 break; 6010 case PCIE_LNK_X2: 6011 adev->pm.pcie_mlw_mask |= (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | 6012 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); 6013 break; 6014 case PCIE_LNK_X1: 6015 adev->pm.pcie_mlw_mask |= CAIL_PCIE_LINK_WIDTH_SUPPORT_X1; 6016 break; 6017 default: 6018 break; 6019 } 6020 } 6021 } 6022 } 6023 6024 /** 6025 * amdgpu_device_is_peer_accessible - Check peer access through PCIe BAR 6026 * 6027 * @adev: amdgpu_device pointer 6028 * @peer_adev: amdgpu_device pointer for peer device trying to access @adev 6029 * 6030 * Return true if @peer_adev can access (DMA) @adev through the PCIe 6031 * BAR, i.e. @adev is "large BAR" and the BAR matches the DMA mask of 6032 * @peer_adev. 6033 */ 6034 bool amdgpu_device_is_peer_accessible(struct amdgpu_device *adev, 6035 struct amdgpu_device *peer_adev) 6036 { 6037 #ifdef CONFIG_HSA_AMD_P2P 6038 bool p2p_access = 6039 !adev->gmc.xgmi.connected_to_cpu && 6040 !(pci_p2pdma_distance(adev->pdev, peer_adev->dev, false) < 0); 6041 if (!p2p_access) 6042 dev_info(adev->dev, "PCIe P2P access from peer device %s is not supported by the chipset\n", 6043 pci_name(peer_adev->pdev)); 6044 6045 bool is_large_bar = adev->gmc.visible_vram_size && 6046 adev->gmc.real_vram_size == adev->gmc.visible_vram_size; 6047 bool p2p_addressable = amdgpu_device_check_iommu_remap(peer_adev); 6048 6049 if (!p2p_addressable) { 6050 uint64_t address_mask = peer_adev->dev->dma_mask ? 6051 ~*peer_adev->dev->dma_mask : ~((1ULL << 32) - 1); 6052 resource_size_t aper_limit = 6053 adev->gmc.aper_base + adev->gmc.aper_size - 1; 6054 6055 p2p_addressable = !(adev->gmc.aper_base & address_mask || 6056 aper_limit & address_mask); 6057 } 6058 return pcie_p2p && is_large_bar && p2p_access && p2p_addressable; 6059 #else 6060 return false; 6061 #endif 6062 } 6063 6064 int amdgpu_device_baco_enter(struct amdgpu_device *adev) 6065 { 6066 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev); 6067 6068 if (!amdgpu_device_supports_baco(adev)) 6069 return -ENOTSUPP; 6070 6071 if (ras && adev->ras_enabled && 6072 adev->nbio.funcs->enable_doorbell_interrupt) 6073 adev->nbio.funcs->enable_doorbell_interrupt(adev, false); 6074 6075 return amdgpu_dpm_baco_enter(adev); 6076 } 6077 6078 int amdgpu_device_baco_exit(struct amdgpu_device *adev) 6079 { 6080 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev); 6081 int ret = 0; 6082 6083 if (!amdgpu_device_supports_baco(adev)) 6084 return -ENOTSUPP; 6085 6086 ret = amdgpu_dpm_baco_exit(adev); 6087 if (ret) 6088 return ret; 6089 6090 if (ras && adev->ras_enabled && 6091 adev->nbio.funcs->enable_doorbell_interrupt) 6092 adev->nbio.funcs->enable_doorbell_interrupt(adev, true); 6093 6094 if (amdgpu_passthrough(adev) && adev->nbio.funcs && 6095 adev->nbio.funcs->clear_doorbell_interrupt) 6096 adev->nbio.funcs->clear_doorbell_interrupt(adev); 6097 6098 return 0; 6099 } 6100 6101 /** 6102 * amdgpu_pci_error_detected - Called when a PCI error is detected. 6103 * @pdev: PCI device struct 6104 * @state: PCI channel state 6105 * 6106 * Description: Called when a PCI error is detected. 6107 * 6108 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT. 6109 */ 6110 pci_ers_result_t amdgpu_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state) 6111 { 6112 struct drm_device *dev = pci_get_drvdata(pdev); 6113 struct amdgpu_device *adev = drm_to_adev(dev); 6114 struct amdgpu_hive_info *hive __free(xgmi_put_hive) = 6115 amdgpu_get_xgmi_hive(adev); 6116 struct amdgpu_reset_context reset_context; 6117 struct list_head device_list; 6118 6119 dev_info(adev->dev, "PCI error: detected callback!!\n"); 6120 6121 adev->pci_channel_state = state; 6122 6123 switch (state) { 6124 case pci_channel_io_normal: 6125 dev_info(adev->dev, "pci_channel_io_normal: state(%d)!!\n", state); 6126 return PCI_ERS_RESULT_CAN_RECOVER; 6127 case pci_channel_io_frozen: 6128 /* Fatal error, prepare for slot reset */ 6129 dev_info(adev->dev, "pci_channel_io_frozen: state(%d)!!\n", state); 6130 if (hive) { 6131 /* Hive devices should be able to support FW based 6132 * link reset on other devices, if not return. 6133 */ 6134 if (!amdgpu_dpm_is_link_reset_supported(adev)) { 6135 dev_warn(adev->dev, 6136 "No support for XGMI hive yet...\n"); 6137 return PCI_ERS_RESULT_DISCONNECT; 6138 } 6139 /* Set dpc status only if device is part of hive 6140 * Non-hive devices should be able to recover after 6141 * link reset. 6142 */ 6143 amdgpu_reset_set_dpc_status(adev, true); 6144 6145 mutex_lock(&hive->hive_lock); 6146 } else { 6147 if (amdgpu_device_bus_status_check(adev)) 6148 amdgpu_reset_set_dpc_status(adev, true); 6149 } 6150 memset(&reset_context, 0, sizeof(reset_context)); 6151 INIT_LIST_HEAD(&device_list); 6152 6153 amdgpu_device_recovery_prepare(adev, &device_list, hive); 6154 amdgpu_device_recovery_get_reset_lock(adev, &device_list); 6155 amdgpu_device_halt_activities(adev, NULL, &reset_context, &device_list, 6156 hive, false); 6157 if (hive) 6158 mutex_unlock(&hive->hive_lock); 6159 return PCI_ERS_RESULT_NEED_RESET; 6160 case pci_channel_io_perm_failure: 6161 /* Permanent error, prepare for device removal */ 6162 dev_info(adev->dev, "pci_channel_io_perm_failure: state(%d)!!\n", state); 6163 return PCI_ERS_RESULT_DISCONNECT; 6164 } 6165 6166 return PCI_ERS_RESULT_NEED_RESET; 6167 } 6168 6169 /** 6170 * amdgpu_pci_mmio_enabled - Enable MMIO and dump debug registers 6171 * @pdev: pointer to PCI device 6172 */ 6173 pci_ers_result_t amdgpu_pci_mmio_enabled(struct pci_dev *pdev) 6174 { 6175 struct drm_device *dev = pci_get_drvdata(pdev); 6176 struct amdgpu_device *adev = drm_to_adev(dev); 6177 6178 dev_info(adev->dev, "PCI error: mmio enabled callback!!\n"); 6179 6180 /* TODO - dump whatever for debugging purposes */ 6181 6182 /* This called only if amdgpu_pci_error_detected returns 6183 * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still 6184 * works, no need to reset slot. 6185 */ 6186 6187 return PCI_ERS_RESULT_RECOVERED; 6188 } 6189 6190 /** 6191 * amdgpu_pci_slot_reset - Called when PCI slot has been reset. 6192 * @pdev: PCI device struct 6193 * 6194 * Description: This routine is called by the pci error recovery 6195 * code after the PCI slot has been reset, just before we 6196 * should resume normal operations. 6197 */ 6198 pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev) 6199 { 6200 struct drm_device *dev = pci_get_drvdata(pdev); 6201 struct amdgpu_device *adev = drm_to_adev(dev); 6202 struct amdgpu_reset_context reset_context; 6203 struct amdgpu_device *tmp_adev; 6204 struct amdgpu_hive_info *hive; 6205 struct list_head device_list; 6206 struct pci_dev *link_dev; 6207 int r = 0, i, timeout; 6208 u32 memsize; 6209 u16 status; 6210 6211 dev_info(adev->dev, "PCI error: slot reset callback!!\n"); 6212 6213 memset(&reset_context, 0, sizeof(reset_context)); 6214 INIT_LIST_HEAD(&device_list); 6215 hive = amdgpu_get_xgmi_hive(adev); 6216 if (hive) { 6217 mutex_lock(&hive->hive_lock); 6218 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) 6219 list_add_tail(&tmp_adev->reset_list, &device_list); 6220 } else { 6221 list_add_tail(&adev->reset_list, &device_list); 6222 } 6223 6224 if (adev->pcie_reset_ctx.swus) 6225 link_dev = adev->pcie_reset_ctx.swus; 6226 else 6227 link_dev = adev->pdev; 6228 /* wait for asic to come out of reset, timeout = 10s */ 6229 timeout = 10000; 6230 do { 6231 usleep_range(10000, 10500); 6232 r = pci_read_config_word(link_dev, PCI_VENDOR_ID, &status); 6233 timeout -= 10; 6234 } while (timeout > 0 && (status != PCI_VENDOR_ID_ATI) && 6235 (status != PCI_VENDOR_ID_AMD)); 6236 6237 if ((status != PCI_VENDOR_ID_ATI) && (status != PCI_VENDOR_ID_AMD)) { 6238 r = -ETIME; 6239 goto out; 6240 } 6241 6242 amdgpu_device_load_switch_state(adev); 6243 /* Restore PCI confspace */ 6244 amdgpu_device_load_pci_state(pdev); 6245 6246 /* confirm ASIC came out of reset */ 6247 for (i = 0; i < adev->usec_timeout; i++) { 6248 memsize = amdgpu_asic_get_config_memsize(adev); 6249 6250 if (memsize != 0xffffffff) 6251 break; 6252 udelay(1); 6253 } 6254 if (memsize == 0xffffffff) { 6255 r = -ETIME; 6256 goto out; 6257 } 6258 6259 reset_context.method = AMD_RESET_METHOD_NONE; 6260 reset_context.reset_req_dev = adev; 6261 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags); 6262 set_bit(AMDGPU_SKIP_COREDUMP, &reset_context.flags); 6263 6264 if (hive) { 6265 reset_context.hive = hive; 6266 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) 6267 tmp_adev->pcie_reset_ctx.in_link_reset = true; 6268 } else { 6269 adev->pcie_reset_ctx.in_link_reset = true; 6270 set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags); 6271 } 6272 6273 r = amdgpu_device_asic_reset(adev, &device_list, &reset_context); 6274 out: 6275 if (!r) { 6276 if (amdgpu_device_cache_pci_state(adev->pdev)) 6277 pci_restore_state(adev->pdev); 6278 dev_info(adev->dev, "PCIe error recovery succeeded\n"); 6279 } else { 6280 dev_err(adev->dev, "PCIe error recovery failed, err:%d\n", r); 6281 if (hive) { 6282 list_for_each_entry(tmp_adev, &device_list, reset_list) 6283 amdgpu_device_unset_mp1_state(tmp_adev); 6284 } 6285 amdgpu_device_recovery_put_reset_lock(adev, &device_list); 6286 } 6287 6288 if (hive) { 6289 mutex_unlock(&hive->hive_lock); 6290 amdgpu_put_xgmi_hive(hive); 6291 } 6292 6293 return r ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED; 6294 } 6295 6296 /** 6297 * amdgpu_pci_resume() - resume normal ops after PCI reset 6298 * @pdev: pointer to PCI device 6299 * 6300 * Called when the error recovery driver tells us that its 6301 * OK to resume normal operation. 6302 */ 6303 void amdgpu_pci_resume(struct pci_dev *pdev) 6304 { 6305 struct drm_device *dev = pci_get_drvdata(pdev); 6306 struct amdgpu_device *adev = drm_to_adev(dev); 6307 struct list_head device_list; 6308 struct amdgpu_hive_info *hive = NULL; 6309 struct amdgpu_device *tmp_adev = NULL; 6310 6311 dev_info(adev->dev, "PCI error: resume callback!!\n"); 6312 6313 /* Only continue execution for the case of pci_channel_io_frozen */ 6314 if (adev->pci_channel_state != pci_channel_io_frozen) 6315 return; 6316 6317 INIT_LIST_HEAD(&device_list); 6318 6319 hive = amdgpu_get_xgmi_hive(adev); 6320 if (hive) { 6321 mutex_lock(&hive->hive_lock); 6322 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) { 6323 tmp_adev->pcie_reset_ctx.in_link_reset = false; 6324 list_add_tail(&tmp_adev->reset_list, &device_list); 6325 } 6326 } else { 6327 adev->pcie_reset_ctx.in_link_reset = false; 6328 list_add_tail(&adev->reset_list, &device_list); 6329 } 6330 amdgpu_device_sched_resume(&device_list, NULL, NULL); 6331 amdgpu_device_gpu_resume(adev, &device_list, false); 6332 amdgpu_device_recovery_put_reset_lock(adev, &device_list); 6333 6334 if (hive) { 6335 mutex_unlock(&hive->hive_lock); 6336 amdgpu_put_xgmi_hive(hive); 6337 } 6338 } 6339 6340 static void amdgpu_device_cache_switch_state(struct amdgpu_device *adev) 6341 { 6342 struct pci_dev *swus, *swds; 6343 int r; 6344 6345 swds = pci_upstream_bridge(adev->pdev); 6346 if (!swds || swds->vendor != PCI_VENDOR_ID_ATI || 6347 pci_pcie_type(swds) != PCI_EXP_TYPE_DOWNSTREAM) 6348 return; 6349 swus = pci_upstream_bridge(swds); 6350 if (!swus || 6351 (swus->vendor != PCI_VENDOR_ID_ATI && 6352 swus->vendor != PCI_VENDOR_ID_AMD) || 6353 pci_pcie_type(swus) != PCI_EXP_TYPE_UPSTREAM) 6354 return; 6355 6356 /* If already saved, return */ 6357 if (adev->pcie_reset_ctx.swus) 6358 return; 6359 /* Upstream bridge is ATI, assume it's SWUS/DS architecture */ 6360 r = pci_save_state(swds); 6361 if (r) 6362 return; 6363 adev->pcie_reset_ctx.swds_pcistate = pci_store_saved_state(swds); 6364 6365 r = pci_save_state(swus); 6366 if (r) 6367 return; 6368 adev->pcie_reset_ctx.swus_pcistate = pci_store_saved_state(swus); 6369 6370 adev->pcie_reset_ctx.swus = swus; 6371 } 6372 6373 static void amdgpu_device_load_switch_state(struct amdgpu_device *adev) 6374 { 6375 struct pci_dev *pdev; 6376 int r; 6377 6378 if (!adev->pcie_reset_ctx.swds_pcistate || 6379 !adev->pcie_reset_ctx.swus_pcistate) 6380 return; 6381 6382 pdev = adev->pcie_reset_ctx.swus; 6383 r = pci_load_saved_state(pdev, adev->pcie_reset_ctx.swus_pcistate); 6384 if (!r) { 6385 pci_restore_state(pdev); 6386 } else { 6387 dev_warn(adev->dev, "Failed to load SWUS state, err:%d\n", r); 6388 return; 6389 } 6390 6391 pdev = pci_upstream_bridge(adev->pdev); 6392 r = pci_load_saved_state(pdev, adev->pcie_reset_ctx.swds_pcistate); 6393 if (!r) 6394 pci_restore_state(pdev); 6395 else 6396 dev_warn(adev->dev, "Failed to load SWDS state, err:%d\n", r); 6397 } 6398 6399 bool amdgpu_device_cache_pci_state(struct pci_dev *pdev) 6400 { 6401 struct drm_device *dev = pci_get_drvdata(pdev); 6402 struct amdgpu_device *adev = drm_to_adev(dev); 6403 int r; 6404 6405 if (amdgpu_sriov_vf(adev)) 6406 return false; 6407 6408 r = pci_save_state(pdev); 6409 if (!r) { 6410 kfree(adev->pci_state); 6411 6412 adev->pci_state = pci_store_saved_state(pdev); 6413 6414 if (!adev->pci_state) { 6415 dev_err(adev->dev, "Failed to store PCI saved state"); 6416 return false; 6417 } 6418 } else { 6419 dev_warn(adev->dev, "Failed to save PCI state, err:%d\n", r); 6420 return false; 6421 } 6422 6423 amdgpu_device_cache_switch_state(adev); 6424 6425 return true; 6426 } 6427 6428 bool amdgpu_device_load_pci_state(struct pci_dev *pdev) 6429 { 6430 struct drm_device *dev = pci_get_drvdata(pdev); 6431 struct amdgpu_device *adev = drm_to_adev(dev); 6432 int r; 6433 6434 if (!adev->pci_state) 6435 return false; 6436 6437 r = pci_load_saved_state(pdev, adev->pci_state); 6438 6439 if (!r) { 6440 pci_restore_state(pdev); 6441 } else { 6442 dev_warn(adev->dev, "Failed to load PCI state, err:%d\n", r); 6443 return false; 6444 } 6445 6446 return true; 6447 } 6448 6449 void amdgpu_device_flush_hdp(struct amdgpu_device *adev, 6450 struct amdgpu_ring *ring) 6451 { 6452 #ifdef CONFIG_X86_64 6453 if ((adev->flags & AMD_IS_APU) && !amdgpu_passthrough(adev)) 6454 return; 6455 #endif 6456 if (adev->gmc.xgmi.connected_to_cpu) 6457 return; 6458 6459 if (ring && ring->funcs->emit_hdp_flush) { 6460 amdgpu_ring_emit_hdp_flush(ring); 6461 return; 6462 } 6463 6464 if (!ring && amdgpu_sriov_runtime(adev)) { 6465 if (!amdgpu_kiq_hdp_flush(adev)) 6466 return; 6467 } 6468 6469 amdgpu_hdp_flush(adev, ring); 6470 } 6471 6472 void amdgpu_device_invalidate_hdp(struct amdgpu_device *adev, 6473 struct amdgpu_ring *ring) 6474 { 6475 #ifdef CONFIG_X86_64 6476 if ((adev->flags & AMD_IS_APU) && !amdgpu_passthrough(adev)) 6477 return; 6478 #endif 6479 if (adev->gmc.xgmi.connected_to_cpu) 6480 return; 6481 6482 amdgpu_hdp_invalidate(adev, ring); 6483 } 6484 6485 int amdgpu_in_reset(struct amdgpu_device *adev) 6486 { 6487 return atomic_read(&adev->reset_domain->in_gpu_reset); 6488 } 6489 6490 /** 6491 * amdgpu_device_halt() - bring hardware to some kind of halt state 6492 * 6493 * @adev: amdgpu_device pointer 6494 * 6495 * Bring hardware to some kind of halt state so that no one can touch it 6496 * any more. It will help to maintain error context when error occurred. 6497 * Compare to a simple hang, the system will keep stable at least for SSH 6498 * access. Then it should be trivial to inspect the hardware state and 6499 * see what's going on. Implemented as following: 6500 * 6501 * 1. drm_dev_unplug() makes device inaccessible to user space(IOCTLs, etc), 6502 * clears all CPU mappings to device, disallows remappings through page faults 6503 * 2. amdgpu_irq_disable_all() disables all interrupts 6504 * 3. amdgpu_fence_driver_hw_fini() signals all HW fences 6505 * 4. set adev->no_hw_access to avoid potential crashes after setp 5 6506 * 5. amdgpu_device_unmap_mmio() clears all MMIO mappings 6507 * 6. pci_disable_device() and pci_wait_for_pending_transaction() 6508 * flush any in flight DMA operations 6509 */ 6510 void amdgpu_device_halt(struct amdgpu_device *adev) 6511 { 6512 struct pci_dev *pdev = adev->pdev; 6513 struct drm_device *ddev = adev_to_drm(adev); 6514 6515 amdgpu_xcp_dev_unplug(adev); 6516 drm_dev_unplug(ddev); 6517 6518 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE); 6519 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE); 6520 6521 amdgpu_irq_disable_all(adev); 6522 6523 amdgpu_fence_driver_hw_fini(adev); 6524 6525 adev->no_hw_access = true; 6526 6527 amdgpu_device_unmap_mmio(adev); 6528 6529 pci_disable_device(pdev); 6530 pci_wait_for_pending_transaction(pdev); 6531 } 6532 6533 /** 6534 * amdgpu_device_get_gang - return a reference to the current gang 6535 * @adev: amdgpu_device pointer 6536 * 6537 * Returns: A new reference to the current gang leader. 6538 */ 6539 struct dma_fence *amdgpu_device_get_gang(struct amdgpu_device *adev) 6540 { 6541 struct dma_fence *fence; 6542 6543 rcu_read_lock(); 6544 fence = dma_fence_get_rcu_safe(&adev->gang_submit); 6545 rcu_read_unlock(); 6546 return fence; 6547 } 6548 6549 /** 6550 * amdgpu_device_switch_gang - switch to a new gang 6551 * @adev: amdgpu_device pointer 6552 * @gang: the gang to switch to 6553 * 6554 * Try to switch to a new gang. 6555 * Returns: NULL if we switched to the new gang or a reference to the current 6556 * gang leader. 6557 */ 6558 struct dma_fence *amdgpu_device_switch_gang(struct amdgpu_device *adev, 6559 struct dma_fence *gang) 6560 { 6561 struct dma_fence *old = NULL; 6562 6563 dma_fence_get(gang); 6564 do { 6565 dma_fence_put(old); 6566 old = amdgpu_device_get_gang(adev); 6567 if (old == gang) 6568 break; 6569 6570 if (!dma_fence_is_signaled(old)) { 6571 dma_fence_put(gang); 6572 return old; 6573 } 6574 6575 } while (cmpxchg((struct dma_fence __force **)&adev->gang_submit, 6576 old, gang) != old); 6577 6578 /* 6579 * Drop it once for the exchanged reference in adev and once for the 6580 * thread local reference acquired in amdgpu_device_get_gang(). 6581 */ 6582 dma_fence_put(old); 6583 dma_fence_put(old); 6584 return NULL; 6585 } 6586 6587 /** 6588 * amdgpu_device_enforce_isolation - enforce HW isolation 6589 * @adev: the amdgpu device pointer 6590 * @ring: the HW ring the job is supposed to run on 6591 * @job: the job which is about to be pushed to the HW ring 6592 * 6593 * Makes sure that only one client at a time can use the GFX block. 6594 * Returns: The dependency to wait on before the job can be pushed to the HW. 6595 * The function is called multiple times until NULL is returned. 6596 */ 6597 struct dma_fence *amdgpu_device_enforce_isolation(struct amdgpu_device *adev, 6598 struct amdgpu_ring *ring, 6599 struct amdgpu_job *job) 6600 { 6601 struct amdgpu_isolation *isolation = &adev->isolation[ring->xcp_id]; 6602 struct drm_sched_fence *f = job->base.s_fence; 6603 struct dma_fence *dep; 6604 void *owner; 6605 int r; 6606 6607 /* 6608 * For now enforce isolation only for the GFX block since we only need 6609 * the cleaner shader on those rings. 6610 */ 6611 if (ring->funcs->type != AMDGPU_RING_TYPE_GFX && 6612 ring->funcs->type != AMDGPU_RING_TYPE_COMPUTE) 6613 return NULL; 6614 6615 /* 6616 * All submissions where enforce isolation is false are handled as if 6617 * they come from a single client. Use ~0l as the owner to distinct it 6618 * from kernel submissions where the owner is NULL. 6619 */ 6620 owner = job->enforce_isolation ? f->owner : (void *)~0l; 6621 6622 mutex_lock(&adev->enforce_isolation_mutex); 6623 6624 /* 6625 * The "spearhead" submission is the first one which changes the 6626 * ownership to its client. We always need to wait for it to be 6627 * pushed to the HW before proceeding with anything. 6628 */ 6629 if (&f->scheduled != isolation->spearhead && 6630 !dma_fence_is_signaled(isolation->spearhead)) { 6631 dep = isolation->spearhead; 6632 goto out_grab_ref; 6633 } 6634 6635 if (isolation->owner != owner) { 6636 6637 /* 6638 * Wait for any gang to be assembled before switching to a 6639 * different owner or otherwise we could deadlock the 6640 * submissions. 6641 */ 6642 if (!job->gang_submit) { 6643 dep = amdgpu_device_get_gang(adev); 6644 if (!dma_fence_is_signaled(dep)) 6645 goto out_return_dep; 6646 dma_fence_put(dep); 6647 } 6648 6649 dma_fence_put(isolation->spearhead); 6650 isolation->spearhead = dma_fence_get(&f->scheduled); 6651 amdgpu_sync_move(&isolation->active, &isolation->prev); 6652 trace_amdgpu_isolation(isolation->owner, owner); 6653 isolation->owner = owner; 6654 } 6655 6656 /* 6657 * Specifying the ring here helps to pipeline submissions even when 6658 * isolation is enabled. If that is not desired for testing NULL can be 6659 * used instead of the ring to enforce a CPU round trip while switching 6660 * between clients. 6661 */ 6662 dep = amdgpu_sync_peek_fence(&isolation->prev, ring); 6663 r = amdgpu_sync_fence(&isolation->active, &f->finished, GFP_NOWAIT); 6664 if (r) 6665 dev_warn(adev->dev, "OOM tracking isolation\n"); 6666 6667 out_grab_ref: 6668 dma_fence_get(dep); 6669 out_return_dep: 6670 mutex_unlock(&adev->enforce_isolation_mutex); 6671 return dep; 6672 } 6673 6674 bool amdgpu_device_has_display_hardware(struct amdgpu_device *adev) 6675 { 6676 switch (adev->asic_type) { 6677 #ifdef CONFIG_DRM_AMDGPU_SI 6678 case CHIP_HAINAN: 6679 #endif 6680 case CHIP_TOPAZ: 6681 /* chips with no display hardware */ 6682 return false; 6683 #ifdef CONFIG_DRM_AMDGPU_SI 6684 case CHIP_TAHITI: 6685 case CHIP_PITCAIRN: 6686 case CHIP_VERDE: 6687 case CHIP_OLAND: 6688 #endif 6689 #ifdef CONFIG_DRM_AMDGPU_CIK 6690 case CHIP_BONAIRE: 6691 case CHIP_HAWAII: 6692 case CHIP_KAVERI: 6693 case CHIP_KABINI: 6694 case CHIP_MULLINS: 6695 #endif 6696 case CHIP_TONGA: 6697 case CHIP_FIJI: 6698 case CHIP_POLARIS10: 6699 case CHIP_POLARIS11: 6700 case CHIP_POLARIS12: 6701 case CHIP_VEGAM: 6702 case CHIP_CARRIZO: 6703 case CHIP_STONEY: 6704 /* chips with display hardware */ 6705 return true; 6706 default: 6707 /* IP discovery */ 6708 if (!amdgpu_ip_version(adev, DCE_HWIP, 0) || 6709 (adev->harvest_ip_mask & AMD_HARVEST_IP_DMU_MASK)) 6710 return false; 6711 return true; 6712 } 6713 } 6714 6715 ssize_t amdgpu_get_soft_full_reset_mask(struct amdgpu_ring *ring) 6716 { 6717 ssize_t size = 0; 6718 6719 if (!ring || !ring->adev) 6720 return size; 6721 6722 if (amdgpu_device_should_recover_gpu(ring->adev)) 6723 size |= AMDGPU_RESET_TYPE_FULL; 6724 6725 if (unlikely(!ring->adev->debug_disable_soft_recovery) && 6726 !amdgpu_sriov_vf(ring->adev) && ring->funcs->soft_recovery) 6727 size |= AMDGPU_RESET_TYPE_SOFT_RECOVERY; 6728 6729 return size; 6730 } 6731 6732 ssize_t amdgpu_show_reset_mask(char *buf, uint32_t supported_reset) 6733 { 6734 ssize_t size = 0; 6735 6736 if (supported_reset == 0) { 6737 size += sysfs_emit_at(buf, size, "unsupported"); 6738 size += sysfs_emit_at(buf, size, "\n"); 6739 return size; 6740 6741 } 6742 6743 if (supported_reset & AMDGPU_RESET_TYPE_SOFT_RECOVERY) 6744 size += sysfs_emit_at(buf, size, "soft_recovery "); 6745 6746 if (supported_reset & AMDGPU_RESET_TYPE_PER_QUEUE) 6747 size += sysfs_emit_at(buf, size, "queue "); 6748 6749 if (supported_reset & AMDGPU_RESET_TYPE_PER_PIPE) 6750 size += sysfs_emit_at(buf, size, "pipe "); 6751 6752 if (supported_reset & AMDGPU_RESET_TYPE_FULL) 6753 size += sysfs_emit_at(buf, size, "full "); 6754 6755 size += sysfs_emit_at(buf, size, "\n"); 6756 return size; 6757 } 6758 6759 void amdgpu_device_set_uid(struct amdgpu_uid *uid_info, 6760 enum amdgpu_uid_type type, uint8_t inst, 6761 uint64_t uid) 6762 { 6763 if (!uid_info) 6764 return; 6765 6766 if (type >= AMDGPU_UID_TYPE_MAX) { 6767 dev_err_once(uid_info->adev->dev, "Invalid UID type %d\n", 6768 type); 6769 return; 6770 } 6771 6772 if (inst >= AMDGPU_UID_INST_MAX) { 6773 dev_err_once(uid_info->adev->dev, "Invalid UID instance %d\n", 6774 inst); 6775 return; 6776 } 6777 6778 if (uid_info->uid[type][inst] != 0) { 6779 dev_warn_once( 6780 uid_info->adev->dev, 6781 "Overwriting existing UID %llu for type %d instance %d\n", 6782 uid_info->uid[type][inst], type, inst); 6783 } 6784 6785 uid_info->uid[type][inst] = uid; 6786 } 6787 6788 u64 amdgpu_device_get_uid(struct amdgpu_uid *uid_info, 6789 enum amdgpu_uid_type type, uint8_t inst) 6790 { 6791 if (!uid_info) 6792 return 0; 6793 6794 if (type >= AMDGPU_UID_TYPE_MAX) { 6795 dev_err_once(uid_info->adev->dev, "Invalid UID type %d\n", 6796 type); 6797 return 0; 6798 } 6799 6800 if (inst >= AMDGPU_UID_INST_MAX) { 6801 dev_err_once(uid_info->adev->dev, "Invalid UID instance %d\n", 6802 inst); 6803 return 0; 6804 } 6805 6806 return uid_info->uid[type][inst]; 6807 } 6808