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