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