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