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