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