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