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