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