2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 * Authors: Dave Airlie
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>
40 #include <drm/drm_aperture.h>
41 #include <drm/drm_atomic_helper.h>
42 #include <drm/drm_crtc_helper.h>
43 #include <drm/drm_fb_helper.h>
44 #include <drm/drm_probe_helper.h>
45 #include <drm/amdgpu_drm.h>
46 #include <linux/vgaarb.h>
47 #include <linux/vga_switcheroo.h>
48 #include <linux/efi.h>
50 #include "amdgpu_trace.h"
51 #include "amdgpu_i2c.h"
53 #include "amdgpu_atombios.h"
54 #include "amdgpu_atomfirmware.h"
56 #ifdef CONFIG_DRM_AMDGPU_SI
59 #ifdef CONFIG_DRM_AMDGPU_CIK
65 #include "bif/bif_4_1_d.h"
66 #include <linux/firmware.h>
67 #include "amdgpu_vf_error.h"
69 #include "amdgpu_amdkfd.h"
70 #include "amdgpu_pm.h"
72 #include "amdgpu_xgmi.h"
73 #include "amdgpu_ras.h"
74 #include "amdgpu_pmu.h"
75 #include "amdgpu_fru_eeprom.h"
76 #include "amdgpu_reset.h"
78 #include <linux/suspend.h>
79 #include <drm/task_barrier.h>
80 #include <linux/pm_runtime.h>
82 #include <drm/drm_drv.h>
84 #if IS_ENABLED(CONFIG_X86)
85 #include <asm/intel-family.h>
88 MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
89 MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
90 MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
91 MODULE_FIRMWARE("amdgpu/picasso_gpu_info.bin");
92 MODULE_FIRMWARE("amdgpu/raven2_gpu_info.bin");
93 MODULE_FIRMWARE("amdgpu/arcturus_gpu_info.bin");
94 MODULE_FIRMWARE("amdgpu/navi12_gpu_info.bin");
96 #define AMDGPU_RESUME_MS 2000
97 #define AMDGPU_MAX_RETRY_LIMIT 2
98 #define AMDGPU_RETRY_SRIOV_RESET(r) ((r) == -EBUSY || (r) == -ETIMEDOUT || (r) == -EINVAL)
100 static const struct drm_driver amdgpu_kms_driver;
102 const char *amdgpu_asic_name[] = {
144 * DOC: pcie_replay_count
146 * The amdgpu driver provides a sysfs API for reporting the total number
147 * of PCIe replays (NAKs)
148 * The file pcie_replay_count is used for this and returns the total
149 * number of replays as a sum of the NAKs generated and NAKs received
152 static ssize_t amdgpu_device_get_pcie_replay_count(struct device *dev,
153 struct device_attribute *attr, char *buf)
155 struct drm_device *ddev = dev_get_drvdata(dev);
156 struct amdgpu_device *adev = drm_to_adev(ddev);
157 uint64_t cnt = amdgpu_asic_get_pcie_replay_count(adev);
159 return sysfs_emit(buf, "%llu\n", cnt);
162 static DEVICE_ATTR(pcie_replay_count, S_IRUGO,
163 amdgpu_device_get_pcie_replay_count, NULL);
165 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev);
170 * The amdgpu driver provides a sysfs API for reporting the product name
172 * The file product_name is used for this and returns the product name
173 * as returned from the FRU.
174 * NOTE: This is only available for certain server cards
177 static ssize_t amdgpu_device_get_product_name(struct device *dev,
178 struct device_attribute *attr, char *buf)
180 struct drm_device *ddev = dev_get_drvdata(dev);
181 struct amdgpu_device *adev = drm_to_adev(ddev);
183 return sysfs_emit(buf, "%s\n", adev->product_name);
186 static DEVICE_ATTR(product_name, S_IRUGO,
187 amdgpu_device_get_product_name, NULL);
190 * DOC: product_number
192 * The amdgpu driver provides a sysfs API for reporting the part number
194 * The file product_number is used for this and returns the part number
195 * as returned from the FRU.
196 * NOTE: This is only available for certain server cards
199 static ssize_t amdgpu_device_get_product_number(struct device *dev,
200 struct device_attribute *attr, char *buf)
202 struct drm_device *ddev = dev_get_drvdata(dev);
203 struct amdgpu_device *adev = drm_to_adev(ddev);
205 return sysfs_emit(buf, "%s\n", adev->product_number);
208 static DEVICE_ATTR(product_number, S_IRUGO,
209 amdgpu_device_get_product_number, NULL);
214 * The amdgpu driver provides a sysfs API for reporting the serial number
216 * The file serial_number is used for this and returns the serial number
217 * as returned from the FRU.
218 * NOTE: This is only available for certain server cards
221 static ssize_t amdgpu_device_get_serial_number(struct device *dev,
222 struct device_attribute *attr, char *buf)
224 struct drm_device *ddev = dev_get_drvdata(dev);
225 struct amdgpu_device *adev = drm_to_adev(ddev);
227 return sysfs_emit(buf, "%s\n", adev->serial);
230 static DEVICE_ATTR(serial_number, S_IRUGO,
231 amdgpu_device_get_serial_number, NULL);
234 * amdgpu_device_supports_px - Is the device a dGPU with ATPX power control
236 * @dev: drm_device pointer
238 * Returns true if the device is a dGPU with ATPX power control,
239 * otherwise return false.
241 bool amdgpu_device_supports_px(struct drm_device *dev)
243 struct amdgpu_device *adev = drm_to_adev(dev);
245 if ((adev->flags & AMD_IS_PX) && !amdgpu_is_atpx_hybrid())
251 * amdgpu_device_supports_boco - Is the device a dGPU with ACPI power resources
253 * @dev: drm_device pointer
255 * Returns true if the device is a dGPU with ACPI power control,
256 * otherwise return false.
258 bool amdgpu_device_supports_boco(struct drm_device *dev)
260 struct amdgpu_device *adev = drm_to_adev(dev);
263 ((adev->flags & AMD_IS_PX) && amdgpu_is_atpx_hybrid()))
269 * amdgpu_device_supports_baco - Does the device support BACO
271 * @dev: drm_device pointer
273 * Returns true if the device supporte BACO,
274 * otherwise return false.
276 bool amdgpu_device_supports_baco(struct drm_device *dev)
278 struct amdgpu_device *adev = drm_to_adev(dev);
280 return amdgpu_asic_supports_baco(adev);
284 * amdgpu_device_supports_smart_shift - Is the device dGPU with
285 * smart shift support
287 * @dev: drm_device pointer
289 * Returns true if the device is a dGPU with Smart Shift support,
290 * otherwise returns false.
292 bool amdgpu_device_supports_smart_shift(struct drm_device *dev)
294 return (amdgpu_device_supports_boco(dev) &&
295 amdgpu_acpi_is_power_shift_control_supported());
299 * VRAM access helper functions
303 * amdgpu_device_mm_access - access vram by MM_INDEX/MM_DATA
305 * @adev: amdgpu_device pointer
306 * @pos: offset of the buffer in vram
307 * @buf: virtual address of the buffer in system memory
308 * @size: read/write size, sizeof(@buf) must > @size
309 * @write: true - write to vram, otherwise - read from vram
311 void amdgpu_device_mm_access(struct amdgpu_device *adev, loff_t pos,
312 void *buf, size_t size, bool write)
315 uint32_t hi = ~0, tmp = 0;
316 uint32_t *data = buf;
320 if (!drm_dev_enter(adev_to_drm(adev), &idx))
323 BUG_ON(!IS_ALIGNED(pos, 4) || !IS_ALIGNED(size, 4));
325 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
326 for (last = pos + size; pos < last; pos += 4) {
329 WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)pos) | 0x80000000);
331 WREG32_NO_KIQ(mmMM_INDEX_HI, tmp);
335 WREG32_NO_KIQ(mmMM_DATA, *data++);
337 *data++ = RREG32_NO_KIQ(mmMM_DATA);
340 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
345 * amdgpu_device_aper_access - access vram by vram aperature
347 * @adev: amdgpu_device pointer
348 * @pos: offset of the buffer in vram
349 * @buf: virtual address of the buffer in system memory
350 * @size: read/write size, sizeof(@buf) must > @size
351 * @write: true - write to vram, otherwise - read from vram
353 * The return value means how many bytes have been transferred.
355 size_t amdgpu_device_aper_access(struct amdgpu_device *adev, loff_t pos,
356 void *buf, size_t size, bool write)
363 if (!adev->mman.aper_base_kaddr)
366 last = min(pos + size, adev->gmc.visible_vram_size);
368 addr = adev->mman.aper_base_kaddr + pos;
372 memcpy_toio(addr, buf, count);
374 amdgpu_device_flush_hdp(adev, NULL);
376 amdgpu_device_invalidate_hdp(adev, NULL);
378 memcpy_fromio(buf, addr, count);
390 * amdgpu_device_vram_access - read/write a buffer in vram
392 * @adev: amdgpu_device pointer
393 * @pos: offset of the buffer in vram
394 * @buf: virtual address of the buffer in system memory
395 * @size: read/write size, sizeof(@buf) must > @size
396 * @write: true - write to vram, otherwise - read from vram
398 void amdgpu_device_vram_access(struct amdgpu_device *adev, loff_t pos,
399 void *buf, size_t size, bool write)
403 /* try to using vram apreature to access vram first */
404 count = amdgpu_device_aper_access(adev, pos, buf, size, write);
407 /* using MM to access rest vram */
410 amdgpu_device_mm_access(adev, pos, buf, size, write);
415 * register access helper functions.
418 /* Check if hw access should be skipped because of hotplug or device error */
419 bool amdgpu_device_skip_hw_access(struct amdgpu_device *adev)
421 if (adev->no_hw_access)
424 #ifdef CONFIG_LOCKDEP
426 * This is a bit complicated to understand, so worth a comment. What we assert
427 * here is that the GPU reset is not running on another thread in parallel.
429 * For this we trylock the read side of the reset semaphore, if that succeeds
430 * we know that the reset is not running in paralell.
432 * If the trylock fails we assert that we are either already holding the read
433 * side of the lock or are the reset thread itself and hold the write side of
437 if (down_read_trylock(&adev->reset_domain->sem))
438 up_read(&adev->reset_domain->sem);
440 lockdep_assert_held(&adev->reset_domain->sem);
447 * amdgpu_device_rreg - read a memory mapped IO or indirect register
449 * @adev: amdgpu_device pointer
450 * @reg: dword aligned register offset
451 * @acc_flags: access flags which require special behavior
453 * Returns the 32 bit value from the offset specified.
455 uint32_t amdgpu_device_rreg(struct amdgpu_device *adev,
456 uint32_t reg, uint32_t acc_flags)
460 if (amdgpu_device_skip_hw_access(adev))
463 if ((reg * 4) < adev->rmmio_size) {
464 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
465 amdgpu_sriov_runtime(adev) &&
466 down_read_trylock(&adev->reset_domain->sem)) {
467 ret = amdgpu_kiq_rreg(adev, reg);
468 up_read(&adev->reset_domain->sem);
470 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
473 ret = adev->pcie_rreg(adev, reg * 4);
476 trace_amdgpu_device_rreg(adev->pdev->device, reg, ret);
482 * MMIO register read with bytes helper functions
483 * @offset:bytes offset from MMIO start
488 * amdgpu_mm_rreg8 - read a memory mapped IO register
490 * @adev: amdgpu_device pointer
491 * @offset: byte aligned register offset
493 * Returns the 8 bit value from the offset specified.
495 uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset)
497 if (amdgpu_device_skip_hw_access(adev))
500 if (offset < adev->rmmio_size)
501 return (readb(adev->rmmio + offset));
506 * MMIO register write with bytes helper functions
507 * @offset:bytes offset from MMIO start
508 * @value: the value want to be written to the register
512 * amdgpu_mm_wreg8 - read a memory mapped IO register
514 * @adev: amdgpu_device pointer
515 * @offset: byte aligned register offset
516 * @value: 8 bit value to write
518 * Writes the value specified to the offset specified.
520 void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value)
522 if (amdgpu_device_skip_hw_access(adev))
525 if (offset < adev->rmmio_size)
526 writeb(value, adev->rmmio + offset);
532 * amdgpu_device_wreg - write to a memory mapped IO or indirect register
534 * @adev: amdgpu_device pointer
535 * @reg: dword aligned register offset
536 * @v: 32 bit value to write to the register
537 * @acc_flags: access flags which require special behavior
539 * Writes the value specified to the offset specified.
541 void amdgpu_device_wreg(struct amdgpu_device *adev,
542 uint32_t reg, uint32_t v,
545 if (amdgpu_device_skip_hw_access(adev))
548 if ((reg * 4) < adev->rmmio_size) {
549 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
550 amdgpu_sriov_runtime(adev) &&
551 down_read_trylock(&adev->reset_domain->sem)) {
552 amdgpu_kiq_wreg(adev, reg, v);
553 up_read(&adev->reset_domain->sem);
555 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
558 adev->pcie_wreg(adev, reg * 4, v);
561 trace_amdgpu_device_wreg(adev->pdev->device, reg, v);
565 * amdgpu_mm_wreg_mmio_rlc - write register either with direct/indirect mmio or with RLC path if in range
567 * @adev: amdgpu_device pointer
568 * @reg: mmio/rlc register
571 * this function is invoked only for the debugfs register access
573 void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
574 uint32_t reg, uint32_t v)
576 if (amdgpu_device_skip_hw_access(adev))
579 if (amdgpu_sriov_fullaccess(adev) &&
580 adev->gfx.rlc.funcs &&
581 adev->gfx.rlc.funcs->is_rlcg_access_range) {
582 if (adev->gfx.rlc.funcs->is_rlcg_access_range(adev, reg))
583 return amdgpu_sriov_wreg(adev, reg, v, 0, 0);
584 } else if ((reg * 4) >= adev->rmmio_size) {
585 adev->pcie_wreg(adev, reg * 4, v);
587 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
592 * amdgpu_mm_rdoorbell - read a doorbell dword
594 * @adev: amdgpu_device pointer
595 * @index: doorbell index
597 * Returns the value in the doorbell aperture at the
598 * requested doorbell index (CIK).
600 u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
602 if (amdgpu_device_skip_hw_access(adev))
605 if (index < adev->doorbell.num_kernel_doorbells) {
606 return readl(adev->doorbell.ptr + index);
608 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
614 * amdgpu_mm_wdoorbell - write a doorbell dword
616 * @adev: amdgpu_device pointer
617 * @index: doorbell index
620 * Writes @v to the doorbell aperture at the
621 * requested doorbell index (CIK).
623 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
625 if (amdgpu_device_skip_hw_access(adev))
628 if (index < adev->doorbell.num_kernel_doorbells) {
629 writel(v, adev->doorbell.ptr + index);
631 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
636 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
638 * @adev: amdgpu_device pointer
639 * @index: doorbell index
641 * Returns the value in the doorbell aperture at the
642 * requested doorbell index (VEGA10+).
644 u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
646 if (amdgpu_device_skip_hw_access(adev))
649 if (index < adev->doorbell.num_kernel_doorbells) {
650 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
652 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
658 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
660 * @adev: amdgpu_device pointer
661 * @index: doorbell index
664 * Writes @v to the doorbell aperture at the
665 * requested doorbell index (VEGA10+).
667 void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
669 if (amdgpu_device_skip_hw_access(adev))
672 if (index < adev->doorbell.num_kernel_doorbells) {
673 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
675 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
680 * amdgpu_device_indirect_rreg - read an indirect register
682 * @adev: amdgpu_device pointer
683 * @reg_addr: indirect register address to read from
685 * Returns the value of indirect register @reg_addr
687 u32 amdgpu_device_indirect_rreg(struct amdgpu_device *adev,
690 unsigned long flags, pcie_index, pcie_data;
691 void __iomem *pcie_index_offset;
692 void __iomem *pcie_data_offset;
695 pcie_index = adev->nbio.funcs->get_pcie_index_offset(adev);
696 pcie_data = adev->nbio.funcs->get_pcie_data_offset(adev);
698 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
699 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
700 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
702 writel(reg_addr, pcie_index_offset);
703 readl(pcie_index_offset);
704 r = readl(pcie_data_offset);
705 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
710 u32 amdgpu_device_indirect_rreg_ext(struct amdgpu_device *adev,
713 unsigned long flags, pcie_index, pcie_index_hi, pcie_data;
715 void __iomem *pcie_index_offset;
716 void __iomem *pcie_index_hi_offset;
717 void __iomem *pcie_data_offset;
719 pcie_index = adev->nbio.funcs->get_pcie_index_offset(adev);
720 pcie_data = adev->nbio.funcs->get_pcie_data_offset(adev);
721 if (adev->nbio.funcs->get_pcie_index_hi_offset)
722 pcie_index_hi = adev->nbio.funcs->get_pcie_index_hi_offset(adev);
726 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
727 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
728 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
729 if (pcie_index_hi != 0)
730 pcie_index_hi_offset = (void __iomem *)adev->rmmio +
733 writel(reg_addr, pcie_index_offset);
734 readl(pcie_index_offset);
735 if (pcie_index_hi != 0) {
736 writel((reg_addr >> 32) & 0xff, pcie_index_hi_offset);
737 readl(pcie_index_hi_offset);
739 r = readl(pcie_data_offset);
741 /* clear the high bits */
742 if (pcie_index_hi != 0) {
743 writel(0, pcie_index_hi_offset);
744 readl(pcie_index_hi_offset);
747 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
753 * amdgpu_device_indirect_rreg64 - read a 64bits indirect register
755 * @adev: amdgpu_device pointer
756 * @reg_addr: indirect register address to read from
758 * Returns the value of indirect register @reg_addr
760 u64 amdgpu_device_indirect_rreg64(struct amdgpu_device *adev,
763 unsigned long flags, pcie_index, pcie_data;
764 void __iomem *pcie_index_offset;
765 void __iomem *pcie_data_offset;
768 pcie_index = adev->nbio.funcs->get_pcie_index_offset(adev);
769 pcie_data = adev->nbio.funcs->get_pcie_data_offset(adev);
771 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
772 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
773 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
775 /* read low 32 bits */
776 writel(reg_addr, pcie_index_offset);
777 readl(pcie_index_offset);
778 r = readl(pcie_data_offset);
779 /* read high 32 bits */
780 writel(reg_addr + 4, pcie_index_offset);
781 readl(pcie_index_offset);
782 r |= ((u64)readl(pcie_data_offset) << 32);
783 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
789 * amdgpu_device_indirect_wreg - write an indirect register address
791 * @adev: amdgpu_device pointer
792 * @reg_addr: indirect register offset
793 * @reg_data: indirect register data
796 void amdgpu_device_indirect_wreg(struct amdgpu_device *adev,
797 u32 reg_addr, u32 reg_data)
799 unsigned long flags, pcie_index, pcie_data;
800 void __iomem *pcie_index_offset;
801 void __iomem *pcie_data_offset;
803 pcie_index = adev->nbio.funcs->get_pcie_index_offset(adev);
804 pcie_data = adev->nbio.funcs->get_pcie_data_offset(adev);
806 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
807 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
808 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
810 writel(reg_addr, pcie_index_offset);
811 readl(pcie_index_offset);
812 writel(reg_data, pcie_data_offset);
813 readl(pcie_data_offset);
814 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
817 void amdgpu_device_indirect_wreg_ext(struct amdgpu_device *adev,
818 u64 reg_addr, u32 reg_data)
820 unsigned long flags, pcie_index, pcie_index_hi, pcie_data;
821 void __iomem *pcie_index_offset;
822 void __iomem *pcie_index_hi_offset;
823 void __iomem *pcie_data_offset;
825 pcie_index = adev->nbio.funcs->get_pcie_index_offset(adev);
826 pcie_data = adev->nbio.funcs->get_pcie_data_offset(adev);
827 if (adev->nbio.funcs->get_pcie_index_hi_offset)
828 pcie_index_hi = adev->nbio.funcs->get_pcie_index_hi_offset(adev);
832 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
833 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
834 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
835 if (pcie_index_hi != 0)
836 pcie_index_hi_offset = (void __iomem *)adev->rmmio +
839 writel(reg_addr, pcie_index_offset);
840 readl(pcie_index_offset);
841 if (pcie_index_hi != 0) {
842 writel((reg_addr >> 32) & 0xff, pcie_index_hi_offset);
843 readl(pcie_index_hi_offset);
845 writel(reg_data, pcie_data_offset);
846 readl(pcie_data_offset);
848 /* clear the high bits */
849 if (pcie_index_hi != 0) {
850 writel(0, pcie_index_hi_offset);
851 readl(pcie_index_hi_offset);
854 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
858 * amdgpu_device_indirect_wreg64 - write a 64bits indirect register address
860 * @adev: amdgpu_device pointer
861 * @reg_addr: indirect register offset
862 * @reg_data: indirect register data
865 void amdgpu_device_indirect_wreg64(struct amdgpu_device *adev,
866 u32 reg_addr, u64 reg_data)
868 unsigned long flags, pcie_index, pcie_data;
869 void __iomem *pcie_index_offset;
870 void __iomem *pcie_data_offset;
872 pcie_index = adev->nbio.funcs->get_pcie_index_offset(adev);
873 pcie_data = adev->nbio.funcs->get_pcie_data_offset(adev);
875 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
876 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
877 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
879 /* write low 32 bits */
880 writel(reg_addr, pcie_index_offset);
881 readl(pcie_index_offset);
882 writel((u32)(reg_data & 0xffffffffULL), pcie_data_offset);
883 readl(pcie_data_offset);
884 /* write high 32 bits */
885 writel(reg_addr + 4, pcie_index_offset);
886 readl(pcie_index_offset);
887 writel((u32)(reg_data >> 32), pcie_data_offset);
888 readl(pcie_data_offset);
889 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
893 * amdgpu_device_get_rev_id - query device rev_id
895 * @adev: amdgpu_device pointer
897 * Return device rev_id
899 u32 amdgpu_device_get_rev_id(struct amdgpu_device *adev)
901 return adev->nbio.funcs->get_rev_id(adev);
905 * amdgpu_invalid_rreg - dummy reg read function
907 * @adev: amdgpu_device pointer
908 * @reg: offset of register
910 * Dummy register read function. Used for register blocks
911 * that certain asics don't have (all asics).
912 * Returns the value in the register.
914 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
916 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
921 static uint32_t amdgpu_invalid_rreg_ext(struct amdgpu_device *adev, uint64_t reg)
923 DRM_ERROR("Invalid callback to read register 0x%llX\n", reg);
929 * amdgpu_invalid_wreg - dummy reg write function
931 * @adev: amdgpu_device pointer
932 * @reg: offset of register
933 * @v: value to write to the register
935 * Dummy register read function. Used for register blocks
936 * that certain asics don't have (all asics).
938 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
940 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
945 static void amdgpu_invalid_wreg_ext(struct amdgpu_device *adev, uint64_t reg, uint32_t v)
947 DRM_ERROR("Invalid callback to write register 0x%llX with 0x%08X\n",
953 * amdgpu_invalid_rreg64 - dummy 64 bit reg read function
955 * @adev: amdgpu_device pointer
956 * @reg: offset of register
958 * Dummy register read function. Used for register blocks
959 * that certain asics don't have (all asics).
960 * Returns the value in the register.
962 static uint64_t amdgpu_invalid_rreg64(struct amdgpu_device *adev, uint32_t reg)
964 DRM_ERROR("Invalid callback to read 64 bit register 0x%04X\n", reg);
970 * amdgpu_invalid_wreg64 - dummy reg write function
972 * @adev: amdgpu_device pointer
973 * @reg: offset of register
974 * @v: value to write to the register
976 * Dummy register read function. Used for register blocks
977 * that certain asics don't have (all asics).
979 static void amdgpu_invalid_wreg64(struct amdgpu_device *adev, uint32_t reg, uint64_t v)
981 DRM_ERROR("Invalid callback to write 64 bit register 0x%04X with 0x%08llX\n",
987 * amdgpu_block_invalid_rreg - dummy reg read function
989 * @adev: amdgpu_device pointer
990 * @block: offset of instance
991 * @reg: offset of register
993 * Dummy register read function. Used for register blocks
994 * that certain asics don't have (all asics).
995 * Returns the value in the register.
997 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
998 uint32_t block, uint32_t reg)
1000 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
1007 * amdgpu_block_invalid_wreg - dummy reg write function
1009 * @adev: amdgpu_device pointer
1010 * @block: offset of instance
1011 * @reg: offset of register
1012 * @v: value to write to the register
1014 * Dummy register read function. Used for register blocks
1015 * that certain asics don't have (all asics).
1017 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
1019 uint32_t reg, uint32_t v)
1021 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
1027 * amdgpu_device_asic_init - Wrapper for atom asic_init
1029 * @adev: amdgpu_device pointer
1031 * Does any asic specific work and then calls atom asic init.
1033 static int amdgpu_device_asic_init(struct amdgpu_device *adev)
1035 amdgpu_asic_pre_asic_init(adev);
1037 if (adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 3) ||
1038 adev->ip_versions[GC_HWIP][0] >= IP_VERSION(11, 0, 0))
1039 return amdgpu_atomfirmware_asic_init(adev, true);
1041 return amdgpu_atom_asic_init(adev->mode_info.atom_context);
1045 * amdgpu_device_mem_scratch_init - allocate the VRAM scratch page
1047 * @adev: amdgpu_device pointer
1049 * Allocates a scratch page of VRAM for use by various things in the
1052 static int amdgpu_device_mem_scratch_init(struct amdgpu_device *adev)
1054 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE, PAGE_SIZE,
1055 AMDGPU_GEM_DOMAIN_VRAM |
1056 AMDGPU_GEM_DOMAIN_GTT,
1057 &adev->mem_scratch.robj,
1058 &adev->mem_scratch.gpu_addr,
1059 (void **)&adev->mem_scratch.ptr);
1063 * amdgpu_device_mem_scratch_fini - Free the VRAM scratch page
1065 * @adev: amdgpu_device pointer
1067 * Frees the VRAM scratch page.
1069 static void amdgpu_device_mem_scratch_fini(struct amdgpu_device *adev)
1071 amdgpu_bo_free_kernel(&adev->mem_scratch.robj, NULL, NULL);
1075 * amdgpu_device_program_register_sequence - program an array of registers.
1077 * @adev: amdgpu_device pointer
1078 * @registers: pointer to the register array
1079 * @array_size: size of the register array
1081 * Programs an array or registers with and and or masks.
1082 * This is a helper for setting golden registers.
1084 void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
1085 const u32 *registers,
1086 const u32 array_size)
1088 u32 tmp, reg, and_mask, or_mask;
1094 for (i = 0; i < array_size; i += 3) {
1095 reg = registers[i + 0];
1096 and_mask = registers[i + 1];
1097 or_mask = registers[i + 2];
1099 if (and_mask == 0xffffffff) {
1104 if (adev->family >= AMDGPU_FAMILY_AI)
1105 tmp |= (or_mask & and_mask);
1114 * amdgpu_device_pci_config_reset - reset the GPU
1116 * @adev: amdgpu_device pointer
1118 * Resets the GPU using the pci config reset sequence.
1119 * Only applicable to asics prior to vega10.
1121 void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
1123 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
1127 * amdgpu_device_pci_reset - reset the GPU using generic PCI means
1129 * @adev: amdgpu_device pointer
1131 * Resets the GPU using generic pci reset interfaces (FLR, SBR, etc.).
1133 int amdgpu_device_pci_reset(struct amdgpu_device *adev)
1135 return pci_reset_function(adev->pdev);
1139 * GPU doorbell aperture helpers function.
1142 * amdgpu_device_doorbell_init - Init doorbell driver information.
1144 * @adev: amdgpu_device pointer
1146 * Init doorbell driver information (CIK)
1147 * Returns 0 on success, error on failure.
1149 static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
1152 /* No doorbell on SI hardware generation */
1153 if (adev->asic_type < CHIP_BONAIRE) {
1154 adev->doorbell.base = 0;
1155 adev->doorbell.size = 0;
1156 adev->doorbell.num_kernel_doorbells = 0;
1157 adev->doorbell.ptr = NULL;
1161 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
1164 amdgpu_asic_init_doorbell_index(adev);
1166 /* doorbell bar mapping */
1167 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
1168 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
1170 if (adev->enable_mes) {
1171 adev->doorbell.num_kernel_doorbells =
1172 adev->doorbell.size / sizeof(u32);
1174 adev->doorbell.num_kernel_doorbells =
1175 min_t(u32, adev->doorbell.size / sizeof(u32),
1176 adev->doorbell_index.max_assignment+1);
1177 if (adev->doorbell.num_kernel_doorbells == 0)
1180 /* For Vega, reserve and map two pages on doorbell BAR since SDMA
1181 * paging queue doorbell use the second page. The
1182 * AMDGPU_DOORBELL64_MAX_ASSIGNMENT definition assumes all the
1183 * doorbells are in the first page. So with paging queue enabled,
1184 * the max num_kernel_doorbells should + 1 page (0x400 in dword)
1186 if (adev->ip_versions[SDMA0_HWIP][0] >= IP_VERSION(4, 0, 0) &&
1187 adev->ip_versions[SDMA0_HWIP][0] < IP_VERSION(4, 2, 0))
1188 adev->doorbell.num_kernel_doorbells += 0x400;
1191 adev->doorbell.ptr = ioremap(adev->doorbell.base,
1192 adev->doorbell.num_kernel_doorbells *
1194 if (adev->doorbell.ptr == NULL)
1201 * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
1203 * @adev: amdgpu_device pointer
1205 * Tear down doorbell driver information (CIK)
1207 static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
1209 iounmap(adev->doorbell.ptr);
1210 adev->doorbell.ptr = NULL;
1216 * amdgpu_device_wb_*()
1217 * Writeback is the method by which the GPU updates special pages in memory
1218 * with the status of certain GPU events (fences, ring pointers,etc.).
1222 * amdgpu_device_wb_fini - Disable Writeback and free memory
1224 * @adev: amdgpu_device pointer
1226 * Disables Writeback and frees the Writeback memory (all asics).
1227 * Used at driver shutdown.
1229 static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
1231 if (adev->wb.wb_obj) {
1232 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
1234 (void **)&adev->wb.wb);
1235 adev->wb.wb_obj = NULL;
1240 * amdgpu_device_wb_init - Init Writeback driver info and allocate memory
1242 * @adev: amdgpu_device pointer
1244 * Initializes writeback and allocates writeback memory (all asics).
1245 * Used at driver startup.
1246 * Returns 0 on success or an -error on failure.
1248 static int amdgpu_device_wb_init(struct amdgpu_device *adev)
1252 if (adev->wb.wb_obj == NULL) {
1253 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
1254 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
1255 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
1256 &adev->wb.wb_obj, &adev->wb.gpu_addr,
1257 (void **)&adev->wb.wb);
1259 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
1263 adev->wb.num_wb = AMDGPU_MAX_WB;
1264 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
1266 /* clear wb memory */
1267 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
1274 * amdgpu_device_wb_get - Allocate a wb entry
1276 * @adev: amdgpu_device pointer
1279 * Allocate a wb slot for use by the driver (all asics).
1280 * Returns 0 on success or -EINVAL on failure.
1282 int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
1284 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
1286 if (offset < adev->wb.num_wb) {
1287 __set_bit(offset, adev->wb.used);
1288 *wb = offset << 3; /* convert to dw offset */
1296 * amdgpu_device_wb_free - Free a wb entry
1298 * @adev: amdgpu_device pointer
1301 * Free a wb slot allocated for use by the driver (all asics)
1303 void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
1306 if (wb < adev->wb.num_wb)
1307 __clear_bit(wb, adev->wb.used);
1311 * amdgpu_device_resize_fb_bar - try to resize FB BAR
1313 * @adev: amdgpu_device pointer
1315 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
1316 * to fail, but if any of the BARs is not accessible after the size we abort
1317 * driver loading by returning -ENODEV.
1319 int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
1321 int rbar_size = pci_rebar_bytes_to_size(adev->gmc.real_vram_size);
1322 struct pci_bus *root;
1323 struct resource *res;
1329 if (amdgpu_sriov_vf(adev))
1332 /* skip if the bios has already enabled large BAR */
1333 if (adev->gmc.real_vram_size &&
1334 (pci_resource_len(adev->pdev, 0) >= adev->gmc.real_vram_size))
1337 /* Check if the root BUS has 64bit memory resources */
1338 root = adev->pdev->bus;
1339 while (root->parent)
1340 root = root->parent;
1342 pci_bus_for_each_resource(root, res, i) {
1343 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
1344 res->start > 0x100000000ull)
1348 /* Trying to resize is pointless without a root hub window above 4GB */
1352 /* Limit the BAR size to what is available */
1353 rbar_size = min(fls(pci_rebar_get_possible_sizes(adev->pdev, 0)) - 1,
1356 /* Disable memory decoding while we change the BAR addresses and size */
1357 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
1358 pci_write_config_word(adev->pdev, PCI_COMMAND,
1359 cmd & ~PCI_COMMAND_MEMORY);
1361 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
1362 amdgpu_device_doorbell_fini(adev);
1363 if (adev->asic_type >= CHIP_BONAIRE)
1364 pci_release_resource(adev->pdev, 2);
1366 pci_release_resource(adev->pdev, 0);
1368 r = pci_resize_resource(adev->pdev, 0, rbar_size);
1370 DRM_INFO("Not enough PCI address space for a large BAR.");
1371 else if (r && r != -ENOTSUPP)
1372 DRM_ERROR("Problem resizing BAR0 (%d).", r);
1374 pci_assign_unassigned_bus_resources(adev->pdev->bus);
1376 /* When the doorbell or fb BAR isn't available we have no chance of
1379 r = amdgpu_device_doorbell_init(adev);
1380 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
1383 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
1388 static bool amdgpu_device_read_bios(struct amdgpu_device *adev)
1390 if (hweight32(adev->aid_mask) && (adev->flags & AMD_IS_APU)) {
1398 * GPU helpers function.
1401 * amdgpu_device_need_post - check if the hw need post or not
1403 * @adev: amdgpu_device pointer
1405 * Check if the asic has been initialized (all asics) at driver startup
1406 * or post is needed if hw reset is performed.
1407 * Returns true if need or false if not.
1409 bool amdgpu_device_need_post(struct amdgpu_device *adev)
1413 if (amdgpu_sriov_vf(adev))
1416 if (!amdgpu_device_read_bios(adev))
1419 if (amdgpu_passthrough(adev)) {
1420 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
1421 * some old smc fw still need driver do vPost otherwise gpu hang, while
1422 * those smc fw version above 22.15 doesn't have this flaw, so we force
1423 * vpost executed for smc version below 22.15
1425 if (adev->asic_type == CHIP_FIJI) {
1428 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
1429 /* force vPost if error occured */
1433 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
1434 if (fw_ver < 0x00160e00)
1439 /* Don't post if we need to reset whole hive on init */
1440 if (adev->gmc.xgmi.pending_reset)
1443 if (adev->has_hw_reset) {
1444 adev->has_hw_reset = false;
1448 /* bios scratch used on CIK+ */
1449 if (adev->asic_type >= CHIP_BONAIRE)
1450 return amdgpu_atombios_scratch_need_asic_init(adev);
1452 /* check MEM_SIZE for older asics */
1453 reg = amdgpu_asic_get_config_memsize(adev);
1455 if ((reg != 0) && (reg != 0xffffffff))
1462 * amdgpu_device_should_use_aspm - check if the device should program ASPM
1464 * @adev: amdgpu_device pointer
1466 * Confirm whether the module parameter and pcie bridge agree that ASPM should
1467 * be set for this device.
1469 * Returns true if it should be used or false if not.
1471 bool amdgpu_device_should_use_aspm(struct amdgpu_device *adev)
1473 switch (amdgpu_aspm) {
1483 return pcie_aspm_enabled(adev->pdev);
1486 bool amdgpu_device_aspm_support_quirk(void)
1488 #if IS_ENABLED(CONFIG_X86)
1489 struct cpuinfo_x86 *c = &cpu_data(0);
1491 return !(c->x86 == 6 && c->x86_model == INTEL_FAM6_ALDERLAKE);
1497 /* if we get transitioned to only one device, take VGA back */
1499 * amdgpu_device_vga_set_decode - enable/disable vga decode
1501 * @pdev: PCI device pointer
1502 * @state: enable/disable vga decode
1504 * Enable/disable vga decode (all asics).
1505 * Returns VGA resource flags.
1507 static unsigned int amdgpu_device_vga_set_decode(struct pci_dev *pdev,
1510 struct amdgpu_device *adev = drm_to_adev(pci_get_drvdata(pdev));
1511 amdgpu_asic_set_vga_state(adev, state);
1513 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1514 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1516 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1520 * amdgpu_device_check_block_size - validate the vm block size
1522 * @adev: amdgpu_device pointer
1524 * Validates the vm block size specified via module parameter.
1525 * The vm block size defines number of bits in page table versus page directory,
1526 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1527 * page table and the remaining bits are in the page directory.
1529 static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
1531 /* defines number of bits in page table versus page directory,
1532 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1533 * page table and the remaining bits are in the page directory */
1534 if (amdgpu_vm_block_size == -1)
1537 if (amdgpu_vm_block_size < 9) {
1538 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1539 amdgpu_vm_block_size);
1540 amdgpu_vm_block_size = -1;
1545 * amdgpu_device_check_vm_size - validate the vm size
1547 * @adev: amdgpu_device pointer
1549 * Validates the vm size in GB specified via module parameter.
1550 * The VM size is the size of the GPU virtual memory space in GB.
1552 static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
1554 /* no need to check the default value */
1555 if (amdgpu_vm_size == -1)
1558 if (amdgpu_vm_size < 1) {
1559 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1561 amdgpu_vm_size = -1;
1565 static void amdgpu_device_check_smu_prv_buffer_size(struct amdgpu_device *adev)
1568 bool is_os_64 = (sizeof(void *) == 8);
1569 uint64_t total_memory;
1570 uint64_t dram_size_seven_GB = 0x1B8000000;
1571 uint64_t dram_size_three_GB = 0xB8000000;
1573 if (amdgpu_smu_memory_pool_size == 0)
1577 DRM_WARN("Not 64-bit OS, feature not supported\n");
1581 total_memory = (uint64_t)si.totalram * si.mem_unit;
1583 if ((amdgpu_smu_memory_pool_size == 1) ||
1584 (amdgpu_smu_memory_pool_size == 2)) {
1585 if (total_memory < dram_size_three_GB)
1587 } else if ((amdgpu_smu_memory_pool_size == 4) ||
1588 (amdgpu_smu_memory_pool_size == 8)) {
1589 if (total_memory < dram_size_seven_GB)
1592 DRM_WARN("Smu memory pool size not supported\n");
1595 adev->pm.smu_prv_buffer_size = amdgpu_smu_memory_pool_size << 28;
1600 DRM_WARN("No enough system memory\n");
1602 adev->pm.smu_prv_buffer_size = 0;
1605 static int amdgpu_device_init_apu_flags(struct amdgpu_device *adev)
1607 if (!(adev->flags & AMD_IS_APU) ||
1608 adev->asic_type < CHIP_RAVEN)
1611 switch (adev->asic_type) {
1613 if (adev->pdev->device == 0x15dd)
1614 adev->apu_flags |= AMD_APU_IS_RAVEN;
1615 if (adev->pdev->device == 0x15d8)
1616 adev->apu_flags |= AMD_APU_IS_PICASSO;
1619 if ((adev->pdev->device == 0x1636) ||
1620 (adev->pdev->device == 0x164c))
1621 adev->apu_flags |= AMD_APU_IS_RENOIR;
1623 adev->apu_flags |= AMD_APU_IS_GREEN_SARDINE;
1626 adev->apu_flags |= AMD_APU_IS_VANGOGH;
1628 case CHIP_YELLOW_CARP:
1630 case CHIP_CYAN_SKILLFISH:
1631 if ((adev->pdev->device == 0x13FE) ||
1632 (adev->pdev->device == 0x143F))
1633 adev->apu_flags |= AMD_APU_IS_CYAN_SKILLFISH2;
1643 * amdgpu_device_check_arguments - validate module params
1645 * @adev: amdgpu_device pointer
1647 * Validates certain module parameters and updates
1648 * the associated values used by the driver (all asics).
1650 static int amdgpu_device_check_arguments(struct amdgpu_device *adev)
1652 if (amdgpu_sched_jobs < 4) {
1653 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1655 amdgpu_sched_jobs = 4;
1656 } else if (!is_power_of_2(amdgpu_sched_jobs)) {
1657 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1659 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1662 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
1663 /* gart size must be greater or equal to 32M */
1664 dev_warn(adev->dev, "gart size (%d) too small\n",
1666 amdgpu_gart_size = -1;
1669 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
1670 /* gtt size must be greater or equal to 32M */
1671 dev_warn(adev->dev, "gtt size (%d) too small\n",
1673 amdgpu_gtt_size = -1;
1676 /* valid range is between 4 and 9 inclusive */
1677 if (amdgpu_vm_fragment_size != -1 &&
1678 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1679 dev_warn(adev->dev, "valid range is between 4 and 9\n");
1680 amdgpu_vm_fragment_size = -1;
1683 if (amdgpu_sched_hw_submission < 2) {
1684 dev_warn(adev->dev, "sched hw submission jobs (%d) must be at least 2\n",
1685 amdgpu_sched_hw_submission);
1686 amdgpu_sched_hw_submission = 2;
1687 } else if (!is_power_of_2(amdgpu_sched_hw_submission)) {
1688 dev_warn(adev->dev, "sched hw submission jobs (%d) must be a power of 2\n",
1689 amdgpu_sched_hw_submission);
1690 amdgpu_sched_hw_submission = roundup_pow_of_two(amdgpu_sched_hw_submission);
1693 if (amdgpu_reset_method < -1 || amdgpu_reset_method > 4) {
1694 dev_warn(adev->dev, "invalid option for reset method, reverting to default\n");
1695 amdgpu_reset_method = -1;
1698 amdgpu_device_check_smu_prv_buffer_size(adev);
1700 amdgpu_device_check_vm_size(adev);
1702 amdgpu_device_check_block_size(adev);
1704 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
1710 * amdgpu_switcheroo_set_state - set switcheroo state
1712 * @pdev: pci dev pointer
1713 * @state: vga_switcheroo state
1715 * Callback for the switcheroo driver. Suspends or resumes
1716 * the asics before or after it is powered up using ACPI methods.
1718 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev,
1719 enum vga_switcheroo_state state)
1721 struct drm_device *dev = pci_get_drvdata(pdev);
1724 if (amdgpu_device_supports_px(dev) && state == VGA_SWITCHEROO_OFF)
1727 if (state == VGA_SWITCHEROO_ON) {
1728 pr_info("switched on\n");
1729 /* don't suspend or resume card normally */
1730 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1732 pci_set_power_state(pdev, PCI_D0);
1733 amdgpu_device_load_pci_state(pdev);
1734 r = pci_enable_device(pdev);
1736 DRM_WARN("pci_enable_device failed (%d)\n", r);
1737 amdgpu_device_resume(dev, true);
1739 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1741 pr_info("switched off\n");
1742 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1743 amdgpu_device_suspend(dev, true);
1744 amdgpu_device_cache_pci_state(pdev);
1745 /* Shut down the device */
1746 pci_disable_device(pdev);
1747 pci_set_power_state(pdev, PCI_D3cold);
1748 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1753 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1755 * @pdev: pci dev pointer
1757 * Callback for the switcheroo driver. Check of the switcheroo
1758 * state can be changed.
1759 * Returns true if the state can be changed, false if not.
1761 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1763 struct drm_device *dev = pci_get_drvdata(pdev);
1766 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1767 * locking inversion with the driver load path. And the access here is
1768 * completely racy anyway. So don't bother with locking for now.
1770 return atomic_read(&dev->open_count) == 0;
1773 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1774 .set_gpu_state = amdgpu_switcheroo_set_state,
1776 .can_switch = amdgpu_switcheroo_can_switch,
1780 * amdgpu_device_ip_set_clockgating_state - set the CG state
1782 * @dev: amdgpu_device pointer
1783 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1784 * @state: clockgating state (gate or ungate)
1786 * Sets the requested clockgating state for all instances of
1787 * the hardware IP specified.
1788 * Returns the error code from the last instance.
1790 int amdgpu_device_ip_set_clockgating_state(void *dev,
1791 enum amd_ip_block_type block_type,
1792 enum amd_clockgating_state state)
1794 struct amdgpu_device *adev = dev;
1797 for (i = 0; i < adev->num_ip_blocks; i++) {
1798 if (!adev->ip_blocks[i].status.valid)
1800 if (adev->ip_blocks[i].version->type != block_type)
1802 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1804 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1805 (void *)adev, state);
1807 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1808 adev->ip_blocks[i].version->funcs->name, r);
1814 * amdgpu_device_ip_set_powergating_state - set the PG state
1816 * @dev: amdgpu_device pointer
1817 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1818 * @state: powergating state (gate or ungate)
1820 * Sets the requested powergating state for all instances of
1821 * the hardware IP specified.
1822 * Returns the error code from the last instance.
1824 int amdgpu_device_ip_set_powergating_state(void *dev,
1825 enum amd_ip_block_type block_type,
1826 enum amd_powergating_state state)
1828 struct amdgpu_device *adev = dev;
1831 for (i = 0; i < adev->num_ip_blocks; i++) {
1832 if (!adev->ip_blocks[i].status.valid)
1834 if (adev->ip_blocks[i].version->type != block_type)
1836 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1838 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1839 (void *)adev, state);
1841 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1842 adev->ip_blocks[i].version->funcs->name, r);
1848 * amdgpu_device_ip_get_clockgating_state - get the CG state
1850 * @adev: amdgpu_device pointer
1851 * @flags: clockgating feature flags
1853 * Walks the list of IPs on the device and updates the clockgating
1854 * flags for each IP.
1855 * Updates @flags with the feature flags for each hardware IP where
1856 * clockgating is enabled.
1858 void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
1863 for (i = 0; i < adev->num_ip_blocks; i++) {
1864 if (!adev->ip_blocks[i].status.valid)
1866 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1867 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1872 * amdgpu_device_ip_wait_for_idle - wait for idle
1874 * @adev: amdgpu_device pointer
1875 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1877 * Waits for the request hardware IP to be idle.
1878 * Returns 0 for success or a negative error code on failure.
1880 int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1881 enum amd_ip_block_type block_type)
1885 for (i = 0; i < adev->num_ip_blocks; i++) {
1886 if (!adev->ip_blocks[i].status.valid)
1888 if (adev->ip_blocks[i].version->type == block_type) {
1889 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
1900 * amdgpu_device_ip_is_idle - is the hardware IP idle
1902 * @adev: amdgpu_device pointer
1903 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1905 * Check if the hardware IP is idle or not.
1906 * Returns true if it the IP is idle, false if not.
1908 bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1909 enum amd_ip_block_type block_type)
1913 for (i = 0; i < adev->num_ip_blocks; i++) {
1914 if (!adev->ip_blocks[i].status.valid)
1916 if (adev->ip_blocks[i].version->type == block_type)
1917 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
1924 * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1926 * @adev: amdgpu_device pointer
1927 * @type: Type of hardware IP (SMU, GFX, UVD, etc.)
1929 * Returns a pointer to the hardware IP block structure
1930 * if it exists for the asic, otherwise NULL.
1932 struct amdgpu_ip_block *
1933 amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1934 enum amd_ip_block_type type)
1938 for (i = 0; i < adev->num_ip_blocks; i++)
1939 if (adev->ip_blocks[i].version->type == type)
1940 return &adev->ip_blocks[i];
1946 * amdgpu_device_ip_block_version_cmp
1948 * @adev: amdgpu_device pointer
1949 * @type: enum amd_ip_block_type
1950 * @major: major version
1951 * @minor: minor version
1953 * return 0 if equal or greater
1954 * return 1 if smaller or the ip_block doesn't exist
1956 int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1957 enum amd_ip_block_type type,
1958 u32 major, u32 minor)
1960 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
1962 if (ip_block && ((ip_block->version->major > major) ||
1963 ((ip_block->version->major == major) &&
1964 (ip_block->version->minor >= minor))))
1971 * amdgpu_device_ip_block_add
1973 * @adev: amdgpu_device pointer
1974 * @ip_block_version: pointer to the IP to add
1976 * Adds the IP block driver information to the collection of IPs
1979 int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1980 const struct amdgpu_ip_block_version *ip_block_version)
1982 if (!ip_block_version)
1985 switch (ip_block_version->type) {
1986 case AMD_IP_BLOCK_TYPE_VCN:
1987 if (adev->harvest_ip_mask & AMD_HARVEST_IP_VCN_MASK)
1990 case AMD_IP_BLOCK_TYPE_JPEG:
1991 if (adev->harvest_ip_mask & AMD_HARVEST_IP_JPEG_MASK)
1998 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
1999 ip_block_version->funcs->name);
2001 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
2007 * amdgpu_device_enable_virtual_display - enable virtual display feature
2009 * @adev: amdgpu_device pointer
2011 * Enabled the virtual display feature if the user has enabled it via
2012 * the module parameter virtual_display. This feature provides a virtual
2013 * display hardware on headless boards or in virtualized environments.
2014 * This function parses and validates the configuration string specified by
2015 * the user and configues the virtual display configuration (number of
2016 * virtual connectors, crtcs, etc.) specified.
2018 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
2020 adev->enable_virtual_display = false;
2022 if (amdgpu_virtual_display) {
2023 const char *pci_address_name = pci_name(adev->pdev);
2024 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
2026 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
2027 pciaddstr_tmp = pciaddstr;
2028 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
2029 pciaddname = strsep(&pciaddname_tmp, ",");
2030 if (!strcmp("all", pciaddname)
2031 || !strcmp(pci_address_name, pciaddname)) {
2035 adev->enable_virtual_display = true;
2038 res = kstrtol(pciaddname_tmp, 10,
2046 adev->mode_info.num_crtc = num_crtc;
2048 adev->mode_info.num_crtc = 1;
2054 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
2055 amdgpu_virtual_display, pci_address_name,
2056 adev->enable_virtual_display, adev->mode_info.num_crtc);
2062 void amdgpu_device_set_sriov_virtual_display(struct amdgpu_device *adev)
2064 if (amdgpu_sriov_vf(adev) && !adev->enable_virtual_display) {
2065 adev->mode_info.num_crtc = 1;
2066 adev->enable_virtual_display = true;
2067 DRM_INFO("virtual_display:%d, num_crtc:%d\n",
2068 adev->enable_virtual_display, adev->mode_info.num_crtc);
2073 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
2075 * @adev: amdgpu_device pointer
2077 * Parses the asic configuration parameters specified in the gpu info
2078 * firmware and makes them availale to the driver for use in configuring
2080 * Returns 0 on success, -EINVAL on failure.
2082 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
2084 const char *chip_name;
2087 const struct gpu_info_firmware_header_v1_0 *hdr;
2089 adev->firmware.gpu_info_fw = NULL;
2091 if (adev->mman.discovery_bin) {
2093 * FIXME: The bounding box is still needed by Navi12, so
2094 * temporarily read it from gpu_info firmware. Should be dropped
2095 * when DAL no longer needs it.
2097 if (adev->asic_type != CHIP_NAVI12)
2101 switch (adev->asic_type) {
2105 chip_name = "vega10";
2108 chip_name = "vega12";
2111 if (adev->apu_flags & AMD_APU_IS_RAVEN2)
2112 chip_name = "raven2";
2113 else if (adev->apu_flags & AMD_APU_IS_PICASSO)
2114 chip_name = "picasso";
2116 chip_name = "raven";
2119 chip_name = "arcturus";
2122 chip_name = "navi12";
2126 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
2127 err = amdgpu_ucode_request(adev, &adev->firmware.gpu_info_fw, fw_name);
2130 "Failed to get gpu_info firmware \"%s\"\n",
2135 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
2136 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
2138 switch (hdr->version_major) {
2141 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
2142 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
2143 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2146 * Should be droped when DAL no longer needs it.
2148 if (adev->asic_type == CHIP_NAVI12)
2149 goto parse_soc_bounding_box;
2151 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
2152 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
2153 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
2154 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
2155 adev->gfx.config.max_texture_channel_caches =
2156 le32_to_cpu(gpu_info_fw->gc_num_tccs);
2157 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
2158 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
2159 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
2160 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
2161 adev->gfx.config.double_offchip_lds_buf =
2162 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
2163 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
2164 adev->gfx.cu_info.max_waves_per_simd =
2165 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
2166 adev->gfx.cu_info.max_scratch_slots_per_cu =
2167 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
2168 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
2169 if (hdr->version_minor >= 1) {
2170 const struct gpu_info_firmware_v1_1 *gpu_info_fw =
2171 (const struct gpu_info_firmware_v1_1 *)(adev->firmware.gpu_info_fw->data +
2172 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2173 adev->gfx.config.num_sc_per_sh =
2174 le32_to_cpu(gpu_info_fw->num_sc_per_sh);
2175 adev->gfx.config.num_packer_per_sc =
2176 le32_to_cpu(gpu_info_fw->num_packer_per_sc);
2179 parse_soc_bounding_box:
2181 * soc bounding box info is not integrated in disocovery table,
2182 * we always need to parse it from gpu info firmware if needed.
2184 if (hdr->version_minor == 2) {
2185 const struct gpu_info_firmware_v1_2 *gpu_info_fw =
2186 (const struct gpu_info_firmware_v1_2 *)(adev->firmware.gpu_info_fw->data +
2187 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2188 adev->dm.soc_bounding_box = &gpu_info_fw->soc_bounding_box;
2194 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
2203 * amdgpu_device_ip_early_init - run early init for hardware IPs
2205 * @adev: amdgpu_device pointer
2207 * Early initialization pass for hardware IPs. The hardware IPs that make
2208 * up each asic are discovered each IP's early_init callback is run. This
2209 * is the first stage in initializing the asic.
2210 * Returns 0 on success, negative error code on failure.
2212 static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
2214 struct drm_device *dev = adev_to_drm(adev);
2215 struct pci_dev *parent;
2219 amdgpu_device_enable_virtual_display(adev);
2221 if (amdgpu_sriov_vf(adev)) {
2222 r = amdgpu_virt_request_full_gpu(adev, true);
2227 switch (adev->asic_type) {
2228 #ifdef CONFIG_DRM_AMDGPU_SI
2234 adev->family = AMDGPU_FAMILY_SI;
2235 r = si_set_ip_blocks(adev);
2240 #ifdef CONFIG_DRM_AMDGPU_CIK
2246 if (adev->flags & AMD_IS_APU)
2247 adev->family = AMDGPU_FAMILY_KV;
2249 adev->family = AMDGPU_FAMILY_CI;
2251 r = cik_set_ip_blocks(adev);
2259 case CHIP_POLARIS10:
2260 case CHIP_POLARIS11:
2261 case CHIP_POLARIS12:
2265 if (adev->flags & AMD_IS_APU)
2266 adev->family = AMDGPU_FAMILY_CZ;
2268 adev->family = AMDGPU_FAMILY_VI;
2270 r = vi_set_ip_blocks(adev);
2275 r = amdgpu_discovery_set_ip_blocks(adev);
2281 if (amdgpu_has_atpx() &&
2282 (amdgpu_is_atpx_hybrid() ||
2283 amdgpu_has_atpx_dgpu_power_cntl()) &&
2284 ((adev->flags & AMD_IS_APU) == 0) &&
2285 !pci_is_thunderbolt_attached(to_pci_dev(dev->dev)))
2286 adev->flags |= AMD_IS_PX;
2288 if (!(adev->flags & AMD_IS_APU)) {
2289 parent = pci_upstream_bridge(adev->pdev);
2290 adev->has_pr3 = parent ? pci_pr3_present(parent) : false;
2294 adev->pm.pp_feature = amdgpu_pp_feature_mask;
2295 if (amdgpu_sriov_vf(adev) || sched_policy == KFD_SCHED_POLICY_NO_HWS)
2296 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
2297 if (amdgpu_sriov_vf(adev) && adev->asic_type == CHIP_SIENNA_CICHLID)
2298 adev->pm.pp_feature &= ~PP_OVERDRIVE_MASK;
2301 for (i = 0; i < adev->num_ip_blocks; i++) {
2302 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
2303 DRM_WARN("disabled ip block: %d <%s>\n",
2304 i, adev->ip_blocks[i].version->funcs->name);
2305 adev->ip_blocks[i].status.valid = false;
2307 if (adev->ip_blocks[i].version->funcs->early_init) {
2308 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
2310 adev->ip_blocks[i].status.valid = false;
2312 DRM_ERROR("early_init of IP block <%s> failed %d\n",
2313 adev->ip_blocks[i].version->funcs->name, r);
2316 adev->ip_blocks[i].status.valid = true;
2319 adev->ip_blocks[i].status.valid = true;
2322 /* get the vbios after the asic_funcs are set up */
2323 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) {
2324 r = amdgpu_device_parse_gpu_info_fw(adev);
2329 if (amdgpu_device_read_bios(adev)) {
2330 if (!amdgpu_get_bios(adev))
2333 r = amdgpu_atombios_init(adev);
2335 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
2336 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
2341 /*get pf2vf msg info at it's earliest time*/
2342 if (amdgpu_sriov_vf(adev))
2343 amdgpu_virt_init_data_exchange(adev);
2350 amdgpu_amdkfd_device_probe(adev);
2351 adev->cg_flags &= amdgpu_cg_mask;
2352 adev->pg_flags &= amdgpu_pg_mask;
2357 static int amdgpu_device_ip_hw_init_phase1(struct amdgpu_device *adev)
2361 for (i = 0; i < adev->num_ip_blocks; i++) {
2362 if (!adev->ip_blocks[i].status.sw)
2364 if (adev->ip_blocks[i].status.hw)
2366 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2367 (amdgpu_sriov_vf(adev) && (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)) ||
2368 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
2369 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2371 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2372 adev->ip_blocks[i].version->funcs->name, r);
2375 adev->ip_blocks[i].status.hw = true;
2382 static int amdgpu_device_ip_hw_init_phase2(struct amdgpu_device *adev)
2386 for (i = 0; i < adev->num_ip_blocks; i++) {
2387 if (!adev->ip_blocks[i].status.sw)
2389 if (adev->ip_blocks[i].status.hw)
2391 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2393 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2394 adev->ip_blocks[i].version->funcs->name, r);
2397 adev->ip_blocks[i].status.hw = true;
2403 static int amdgpu_device_fw_loading(struct amdgpu_device *adev)
2407 uint32_t smu_version;
2409 if (adev->asic_type >= CHIP_VEGA10) {
2410 for (i = 0; i < adev->num_ip_blocks; i++) {
2411 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_PSP)
2414 if (!adev->ip_blocks[i].status.sw)
2417 /* no need to do the fw loading again if already done*/
2418 if (adev->ip_blocks[i].status.hw == true)
2421 if (amdgpu_in_reset(adev) || adev->in_suspend) {
2422 r = adev->ip_blocks[i].version->funcs->resume(adev);
2424 DRM_ERROR("resume of IP block <%s> failed %d\n",
2425 adev->ip_blocks[i].version->funcs->name, r);
2429 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2431 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2432 adev->ip_blocks[i].version->funcs->name, r);
2437 adev->ip_blocks[i].status.hw = true;
2442 if (!amdgpu_sriov_vf(adev) || adev->asic_type == CHIP_TONGA)
2443 r = amdgpu_pm_load_smu_firmware(adev, &smu_version);
2448 static int amdgpu_device_init_schedulers(struct amdgpu_device *adev)
2453 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2454 struct amdgpu_ring *ring = adev->rings[i];
2456 /* No need to setup the GPU scheduler for rings that don't need it */
2457 if (!ring || ring->no_scheduler)
2460 switch (ring->funcs->type) {
2461 case AMDGPU_RING_TYPE_GFX:
2462 timeout = adev->gfx_timeout;
2464 case AMDGPU_RING_TYPE_COMPUTE:
2465 timeout = adev->compute_timeout;
2467 case AMDGPU_RING_TYPE_SDMA:
2468 timeout = adev->sdma_timeout;
2471 timeout = adev->video_timeout;
2475 r = drm_sched_init(&ring->sched, &amdgpu_sched_ops,
2476 ring->num_hw_submission, 0,
2477 timeout, adev->reset_domain->wq,
2478 ring->sched_score, ring->name,
2481 DRM_ERROR("Failed to create scheduler on ring %s.\n",
2487 amdgpu_xcp_update_partition_sched_list(adev);
2494 * amdgpu_device_ip_init - run init for hardware IPs
2496 * @adev: amdgpu_device pointer
2498 * Main initialization pass for hardware IPs. The list of all the hardware
2499 * IPs that make up the asic is walked and the sw_init and hw_init callbacks
2500 * are run. sw_init initializes the software state associated with each IP
2501 * and hw_init initializes the hardware associated with each IP.
2502 * Returns 0 on success, negative error code on failure.
2504 static int amdgpu_device_ip_init(struct amdgpu_device *adev)
2508 r = amdgpu_ras_init(adev);
2512 for (i = 0; i < adev->num_ip_blocks; i++) {
2513 if (!adev->ip_blocks[i].status.valid)
2515 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
2517 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
2518 adev->ip_blocks[i].version->funcs->name, r);
2521 adev->ip_blocks[i].status.sw = true;
2523 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) {
2524 /* need to do common hw init early so everything is set up for gmc */
2525 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
2527 DRM_ERROR("hw_init %d failed %d\n", i, r);
2530 adev->ip_blocks[i].status.hw = true;
2531 } else if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
2532 /* need to do gmc hw init early so we can allocate gpu mem */
2533 /* Try to reserve bad pages early */
2534 if (amdgpu_sriov_vf(adev))
2535 amdgpu_virt_exchange_data(adev);
2537 r = amdgpu_device_mem_scratch_init(adev);
2539 DRM_ERROR("amdgpu_mem_scratch_init failed %d\n", r);
2542 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
2544 DRM_ERROR("hw_init %d failed %d\n", i, r);
2547 r = amdgpu_device_wb_init(adev);
2549 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
2552 adev->ip_blocks[i].status.hw = true;
2554 /* right after GMC hw init, we create CSA */
2556 r = amdgpu_allocate_static_csa(adev, &adev->virt.csa_obj,
2557 AMDGPU_GEM_DOMAIN_VRAM |
2558 AMDGPU_GEM_DOMAIN_GTT,
2561 DRM_ERROR("allocate CSA failed %d\n", r);
2568 if (amdgpu_sriov_vf(adev))
2569 amdgpu_virt_init_data_exchange(adev);
2571 r = amdgpu_ib_pool_init(adev);
2573 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
2574 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
2578 r = amdgpu_ucode_create_bo(adev); /* create ucode bo when sw_init complete*/
2582 r = amdgpu_device_ip_hw_init_phase1(adev);
2586 r = amdgpu_device_fw_loading(adev);
2590 r = amdgpu_device_ip_hw_init_phase2(adev);
2595 * retired pages will be loaded from eeprom and reserved here,
2596 * it should be called after amdgpu_device_ip_hw_init_phase2 since
2597 * for some ASICs the RAS EEPROM code relies on SMU fully functioning
2598 * for I2C communication which only true at this point.
2600 * amdgpu_ras_recovery_init may fail, but the upper only cares the
2601 * failure from bad gpu situation and stop amdgpu init process
2602 * accordingly. For other failed cases, it will still release all
2603 * the resource and print error message, rather than returning one
2604 * negative value to upper level.
2606 * Note: theoretically, this should be called before all vram allocations
2607 * to protect retired page from abusing
2609 r = amdgpu_ras_recovery_init(adev);
2614 * In case of XGMI grab extra reference for reset domain for this device
2616 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2617 if (amdgpu_xgmi_add_device(adev) == 0) {
2618 if (!amdgpu_sriov_vf(adev)) {
2619 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
2621 if (WARN_ON(!hive)) {
2626 if (!hive->reset_domain ||
2627 !amdgpu_reset_get_reset_domain(hive->reset_domain)) {
2629 amdgpu_put_xgmi_hive(hive);
2633 /* Drop the early temporary reset domain we created for device */
2634 amdgpu_reset_put_reset_domain(adev->reset_domain);
2635 adev->reset_domain = hive->reset_domain;
2636 amdgpu_put_xgmi_hive(hive);
2641 r = amdgpu_device_init_schedulers(adev);
2645 /* Don't init kfd if whole hive need to be reset during init */
2646 if (!adev->gmc.xgmi.pending_reset) {
2647 kgd2kfd_init_zone_device(adev);
2648 amdgpu_amdkfd_device_init(adev);
2651 amdgpu_fru_get_product_info(adev);
2659 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
2661 * @adev: amdgpu_device pointer
2663 * Writes a reset magic value to the gart pointer in VRAM. The driver calls
2664 * this function before a GPU reset. If the value is retained after a
2665 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
2667 static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
2669 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
2673 * amdgpu_device_check_vram_lost - check if vram is valid
2675 * @adev: amdgpu_device pointer
2677 * Checks the reset magic value written to the gart pointer in VRAM.
2678 * The driver calls this after a GPU reset to see if the contents of
2679 * VRAM is lost or now.
2680 * returns true if vram is lost, false if not.
2682 static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
2684 if (memcmp(adev->gart.ptr, adev->reset_magic,
2685 AMDGPU_RESET_MAGIC_NUM))
2688 if (!amdgpu_in_reset(adev))
2692 * For all ASICs with baco/mode1 reset, the VRAM is
2693 * always assumed to be lost.
2695 switch (amdgpu_asic_reset_method(adev)) {
2696 case AMD_RESET_METHOD_BACO:
2697 case AMD_RESET_METHOD_MODE1:
2705 * amdgpu_device_set_cg_state - set clockgating for amdgpu device
2707 * @adev: amdgpu_device pointer
2708 * @state: clockgating state (gate or ungate)
2710 * The list of all the hardware IPs that make up the asic is walked and the
2711 * set_clockgating_state callbacks are run.
2712 * Late initialization pass enabling clockgating for hardware IPs.
2713 * Fini or suspend, pass disabling clockgating for hardware IPs.
2714 * Returns 0 on success, negative error code on failure.
2717 int amdgpu_device_set_cg_state(struct amdgpu_device *adev,
2718 enum amd_clockgating_state state)
2722 if (amdgpu_emu_mode == 1)
2725 for (j = 0; j < adev->num_ip_blocks; j++) {
2726 i = state == AMD_CG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
2727 if (!adev->ip_blocks[i].status.late_initialized)
2729 /* skip CG for GFX, SDMA on S0ix */
2730 if (adev->in_s0ix &&
2731 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX ||
2732 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SDMA))
2734 /* skip CG for VCE/UVD, it's handled specially */
2735 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
2736 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
2737 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
2738 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
2739 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
2740 /* enable clockgating to save power */
2741 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
2744 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
2745 adev->ip_blocks[i].version->funcs->name, r);
2754 int amdgpu_device_set_pg_state(struct amdgpu_device *adev,
2755 enum amd_powergating_state state)
2759 if (amdgpu_emu_mode == 1)
2762 for (j = 0; j < adev->num_ip_blocks; j++) {
2763 i = state == AMD_PG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
2764 if (!adev->ip_blocks[i].status.late_initialized)
2766 /* skip PG for GFX, SDMA on S0ix */
2767 if (adev->in_s0ix &&
2768 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX ||
2769 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SDMA))
2771 /* skip CG for VCE/UVD, it's handled specially */
2772 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
2773 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
2774 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
2775 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
2776 adev->ip_blocks[i].version->funcs->set_powergating_state) {
2777 /* enable powergating to save power */
2778 r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
2781 DRM_ERROR("set_powergating_state(gate) of IP block <%s> failed %d\n",
2782 adev->ip_blocks[i].version->funcs->name, r);
2790 static int amdgpu_device_enable_mgpu_fan_boost(void)
2792 struct amdgpu_gpu_instance *gpu_ins;
2793 struct amdgpu_device *adev;
2796 mutex_lock(&mgpu_info.mutex);
2799 * MGPU fan boost feature should be enabled
2800 * only when there are two or more dGPUs in
2803 if (mgpu_info.num_dgpu < 2)
2806 for (i = 0; i < mgpu_info.num_dgpu; i++) {
2807 gpu_ins = &(mgpu_info.gpu_ins[i]);
2808 adev = gpu_ins->adev;
2809 if (!(adev->flags & AMD_IS_APU) &&
2810 !gpu_ins->mgpu_fan_enabled) {
2811 ret = amdgpu_dpm_enable_mgpu_fan_boost(adev);
2815 gpu_ins->mgpu_fan_enabled = 1;
2820 mutex_unlock(&mgpu_info.mutex);
2826 * amdgpu_device_ip_late_init - run late init for hardware IPs
2828 * @adev: amdgpu_device pointer
2830 * Late initialization pass for hardware IPs. The list of all the hardware
2831 * IPs that make up the asic is walked and the late_init callbacks are run.
2832 * late_init covers any special initialization that an IP requires
2833 * after all of the have been initialized or something that needs to happen
2834 * late in the init process.
2835 * Returns 0 on success, negative error code on failure.
2837 static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
2839 struct amdgpu_gpu_instance *gpu_instance;
2842 for (i = 0; i < adev->num_ip_blocks; i++) {
2843 if (!adev->ip_blocks[i].status.hw)
2845 if (adev->ip_blocks[i].version->funcs->late_init) {
2846 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
2848 DRM_ERROR("late_init of IP block <%s> failed %d\n",
2849 adev->ip_blocks[i].version->funcs->name, r);
2853 adev->ip_blocks[i].status.late_initialized = true;
2856 r = amdgpu_ras_late_init(adev);
2858 DRM_ERROR("amdgpu_ras_late_init failed %d", r);
2862 amdgpu_ras_set_error_query_ready(adev, true);
2864 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE);
2865 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_GATE);
2867 amdgpu_device_fill_reset_magic(adev);
2869 r = amdgpu_device_enable_mgpu_fan_boost();
2871 DRM_ERROR("enable mgpu fan boost failed (%d).\n", r);
2873 /* For passthrough configuration on arcturus and aldebaran, enable special handling SBR */
2874 if (amdgpu_passthrough(adev) &&
2875 ((adev->asic_type == CHIP_ARCTURUS && adev->gmc.xgmi.num_physical_nodes > 1) ||
2876 adev->asic_type == CHIP_ALDEBARAN))
2877 amdgpu_dpm_handle_passthrough_sbr(adev, true);
2879 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2880 mutex_lock(&mgpu_info.mutex);
2883 * Reset device p-state to low as this was booted with high.
2885 * This should be performed only after all devices from the same
2886 * hive get initialized.
2888 * However, it's unknown how many device in the hive in advance.
2889 * As this is counted one by one during devices initializations.
2891 * So, we wait for all XGMI interlinked devices initialized.
2892 * This may bring some delays as those devices may come from
2893 * different hives. But that should be OK.
2895 if (mgpu_info.num_dgpu == adev->gmc.xgmi.num_physical_nodes) {
2896 for (i = 0; i < mgpu_info.num_gpu; i++) {
2897 gpu_instance = &(mgpu_info.gpu_ins[i]);
2898 if (gpu_instance->adev->flags & AMD_IS_APU)
2901 r = amdgpu_xgmi_set_pstate(gpu_instance->adev,
2902 AMDGPU_XGMI_PSTATE_MIN);
2904 DRM_ERROR("pstate setting failed (%d).\n", r);
2910 mutex_unlock(&mgpu_info.mutex);
2917 * amdgpu_device_smu_fini_early - smu hw_fini wrapper
2919 * @adev: amdgpu_device pointer
2921 * For ASICs need to disable SMC first
2923 static void amdgpu_device_smu_fini_early(struct amdgpu_device *adev)
2927 if (adev->ip_versions[GC_HWIP][0] > IP_VERSION(9, 0, 0))
2930 for (i = 0; i < adev->num_ip_blocks; i++) {
2931 if (!adev->ip_blocks[i].status.hw)
2933 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
2934 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2935 /* XXX handle errors */
2937 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2938 adev->ip_blocks[i].version->funcs->name, r);
2940 adev->ip_blocks[i].status.hw = false;
2946 static int amdgpu_device_ip_fini_early(struct amdgpu_device *adev)
2950 for (i = 0; i < adev->num_ip_blocks; i++) {
2951 if (!adev->ip_blocks[i].version->funcs->early_fini)
2954 r = adev->ip_blocks[i].version->funcs->early_fini((void *)adev);
2956 DRM_DEBUG("early_fini of IP block <%s> failed %d\n",
2957 adev->ip_blocks[i].version->funcs->name, r);
2961 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2962 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2964 amdgpu_amdkfd_suspend(adev, false);
2966 /* Workaroud for ASICs need to disable SMC first */
2967 amdgpu_device_smu_fini_early(adev);
2969 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2970 if (!adev->ip_blocks[i].status.hw)
2973 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2974 /* XXX handle errors */
2976 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2977 adev->ip_blocks[i].version->funcs->name, r);
2980 adev->ip_blocks[i].status.hw = false;
2983 if (amdgpu_sriov_vf(adev)) {
2984 if (amdgpu_virt_release_full_gpu(adev, false))
2985 DRM_ERROR("failed to release exclusive mode on fini\n");
2992 * amdgpu_device_ip_fini - run fini for hardware IPs
2994 * @adev: amdgpu_device pointer
2996 * Main teardown pass for hardware IPs. The list of all the hardware
2997 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
2998 * are run. hw_fini tears down the hardware associated with each IP
2999 * and sw_fini tears down any software state associated with each IP.
3000 * Returns 0 on success, negative error code on failure.
3002 static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
3006 if (amdgpu_sriov_vf(adev) && adev->virt.ras_init_done)
3007 amdgpu_virt_release_ras_err_handler_data(adev);
3009 if (adev->gmc.xgmi.num_physical_nodes > 1)
3010 amdgpu_xgmi_remove_device(adev);
3012 amdgpu_amdkfd_device_fini_sw(adev);
3014 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
3015 if (!adev->ip_blocks[i].status.sw)
3018 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
3019 amdgpu_ucode_free_bo(adev);
3020 amdgpu_free_static_csa(&adev->virt.csa_obj);
3021 amdgpu_device_wb_fini(adev);
3022 amdgpu_device_mem_scratch_fini(adev);
3023 amdgpu_ib_pool_fini(adev);
3026 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
3027 /* XXX handle errors */
3029 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
3030 adev->ip_blocks[i].version->funcs->name, r);
3032 adev->ip_blocks[i].status.sw = false;
3033 adev->ip_blocks[i].status.valid = false;
3036 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
3037 if (!adev->ip_blocks[i].status.late_initialized)
3039 if (adev->ip_blocks[i].version->funcs->late_fini)
3040 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
3041 adev->ip_blocks[i].status.late_initialized = false;
3044 amdgpu_ras_fini(adev);
3050 * amdgpu_device_delayed_init_work_handler - work handler for IB tests
3052 * @work: work_struct.
3054 static void amdgpu_device_delayed_init_work_handler(struct work_struct *work)
3056 struct amdgpu_device *adev =
3057 container_of(work, struct amdgpu_device, delayed_init_work.work);
3060 r = amdgpu_ib_ring_tests(adev);
3062 DRM_ERROR("ib ring test failed (%d).\n", r);
3065 static void amdgpu_device_delay_enable_gfx_off(struct work_struct *work)
3067 struct amdgpu_device *adev =
3068 container_of(work, struct amdgpu_device, gfx.gfx_off_delay_work.work);
3070 WARN_ON_ONCE(adev->gfx.gfx_off_state);
3071 WARN_ON_ONCE(adev->gfx.gfx_off_req_count);
3073 if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true))
3074 adev->gfx.gfx_off_state = true;
3078 * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1)
3080 * @adev: amdgpu_device pointer
3082 * Main suspend function for hardware IPs. The list of all the hardware
3083 * IPs that make up the asic is walked, clockgating is disabled and the
3084 * suspend callbacks are run. suspend puts the hardware and software state
3085 * in each IP into a state suitable for suspend.
3086 * Returns 0 on success, negative error code on failure.
3088 static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev)
3092 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
3093 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
3096 * Per PMFW team's suggestion, driver needs to handle gfxoff
3097 * and df cstate features disablement for gpu reset(e.g. Mode1Reset)
3098 * scenario. Add the missing df cstate disablement here.
3100 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_DISALLOW))
3101 dev_warn(adev->dev, "Failed to disallow df cstate");
3103 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
3104 if (!adev->ip_blocks[i].status.valid)
3107 /* displays are handled separately */
3108 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_DCE)
3111 /* XXX handle errors */
3112 r = adev->ip_blocks[i].version->funcs->suspend(adev);
3113 /* XXX handle errors */
3115 DRM_ERROR("suspend of IP block <%s> failed %d\n",
3116 adev->ip_blocks[i].version->funcs->name, r);
3120 adev->ip_blocks[i].status.hw = false;
3127 * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2)
3129 * @adev: amdgpu_device pointer
3131 * Main suspend function for hardware IPs. The list of all the hardware
3132 * IPs that make up the asic is walked, clockgating is disabled and the
3133 * suspend callbacks are run. suspend puts the hardware and software state
3134 * in each IP into a state suitable for suspend.
3135 * Returns 0 on success, negative error code on failure.
3137 static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev)
3142 amdgpu_dpm_gfx_state_change(adev, sGpuChangeState_D3Entry);
3144 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
3145 if (!adev->ip_blocks[i].status.valid)
3147 /* displays are handled in phase1 */
3148 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)
3150 /* PSP lost connection when err_event_athub occurs */
3151 if (amdgpu_ras_intr_triggered() &&
3152 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
3153 adev->ip_blocks[i].status.hw = false;
3157 /* skip unnecessary suspend if we do not initialize them yet */
3158 if (adev->gmc.xgmi.pending_reset &&
3159 !(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3160 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC ||
3161 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3162 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)) {
3163 adev->ip_blocks[i].status.hw = false;
3167 /* skip suspend of gfx/mes and psp for S0ix
3168 * gfx is in gfxoff state, so on resume it will exit gfxoff just
3169 * like at runtime. PSP is also part of the always on hardware
3170 * so no need to suspend it.
3172 if (adev->in_s0ix &&
3173 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP ||
3174 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX ||
3175 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_MES))
3178 /* SDMA 5.x+ is part of GFX power domain so it's covered by GFXOFF */
3179 if (adev->in_s0ix &&
3180 (adev->ip_versions[SDMA0_HWIP][0] >= IP_VERSION(5, 0, 0)) &&
3181 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SDMA))
3184 /* Once swPSP provides the IMU, RLC FW binaries to TOS during cold-boot.
3185 * These are in TMR, hence are expected to be reused by PSP-TOS to reload
3186 * from this location and RLC Autoload automatically also gets loaded
3187 * from here based on PMFW -> PSP message during re-init sequence.
3188 * Therefore, the psp suspend & resume should be skipped to avoid destroy
3189 * the TMR and reload FWs again for IMU enabled APU ASICs.
3191 if (amdgpu_in_reset(adev) &&
3192 (adev->flags & AMD_IS_APU) && adev->gfx.imu.funcs &&
3193 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)
3196 /* XXX handle errors */
3197 r = adev->ip_blocks[i].version->funcs->suspend(adev);
3198 /* XXX handle errors */
3200 DRM_ERROR("suspend of IP block <%s> failed %d\n",
3201 adev->ip_blocks[i].version->funcs->name, r);
3203 adev->ip_blocks[i].status.hw = false;
3204 /* handle putting the SMC in the appropriate state */
3205 if (!amdgpu_sriov_vf(adev)) {
3206 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
3207 r = amdgpu_dpm_set_mp1_state(adev, adev->mp1_state);
3209 DRM_ERROR("SMC failed to set mp1 state %d, %d\n",
3210 adev->mp1_state, r);
3221 * amdgpu_device_ip_suspend - run suspend for hardware IPs
3223 * @adev: amdgpu_device pointer
3225 * Main suspend function for hardware IPs. The list of all the hardware
3226 * IPs that make up the asic is walked, clockgating is disabled and the
3227 * suspend callbacks are run. suspend puts the hardware and software state
3228 * in each IP into a state suitable for suspend.
3229 * Returns 0 on success, negative error code on failure.
3231 int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
3235 if (amdgpu_sriov_vf(adev)) {
3236 amdgpu_virt_fini_data_exchange(adev);
3237 amdgpu_virt_request_full_gpu(adev, false);
3240 r = amdgpu_device_ip_suspend_phase1(adev);
3243 r = amdgpu_device_ip_suspend_phase2(adev);
3245 if (amdgpu_sriov_vf(adev))
3246 amdgpu_virt_release_full_gpu(adev, false);
3251 static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
3255 static enum amd_ip_block_type ip_order[] = {
3256 AMD_IP_BLOCK_TYPE_COMMON,
3257 AMD_IP_BLOCK_TYPE_GMC,
3258 AMD_IP_BLOCK_TYPE_PSP,
3259 AMD_IP_BLOCK_TYPE_IH,
3262 for (i = 0; i < adev->num_ip_blocks; i++) {
3264 struct amdgpu_ip_block *block;
3266 block = &adev->ip_blocks[i];
3267 block->status.hw = false;
3269 for (j = 0; j < ARRAY_SIZE(ip_order); j++) {
3271 if (block->version->type != ip_order[j] ||
3272 !block->status.valid)
3275 r = block->version->funcs->hw_init(adev);
3276 DRM_INFO("RE-INIT-early: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
3279 block->status.hw = true;
3286 static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
3290 static enum amd_ip_block_type ip_order[] = {
3291 AMD_IP_BLOCK_TYPE_SMC,
3292 AMD_IP_BLOCK_TYPE_DCE,
3293 AMD_IP_BLOCK_TYPE_GFX,
3294 AMD_IP_BLOCK_TYPE_SDMA,
3295 AMD_IP_BLOCK_TYPE_MES,
3296 AMD_IP_BLOCK_TYPE_UVD,
3297 AMD_IP_BLOCK_TYPE_VCE,
3298 AMD_IP_BLOCK_TYPE_VCN,
3299 AMD_IP_BLOCK_TYPE_JPEG
3302 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
3304 struct amdgpu_ip_block *block;
3306 for (j = 0; j < adev->num_ip_blocks; j++) {
3307 block = &adev->ip_blocks[j];
3309 if (block->version->type != ip_order[i] ||
3310 !block->status.valid ||
3314 if (block->version->type == AMD_IP_BLOCK_TYPE_SMC)
3315 r = block->version->funcs->resume(adev);
3317 r = block->version->funcs->hw_init(adev);
3319 DRM_INFO("RE-INIT-late: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
3322 block->status.hw = true;
3330 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
3332 * @adev: amdgpu_device pointer
3334 * First resume function for hardware IPs. The list of all the hardware
3335 * IPs that make up the asic is walked and the resume callbacks are run for
3336 * COMMON, GMC, and IH. resume puts the hardware into a functional state
3337 * after a suspend and updates the software state as necessary. This
3338 * function is also used for restoring the GPU after a GPU reset.
3339 * Returns 0 on success, negative error code on failure.
3341 static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
3345 for (i = 0; i < adev->num_ip_blocks; i++) {
3346 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
3348 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3349 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3350 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3351 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP && amdgpu_sriov_vf(adev))) {
3353 r = adev->ip_blocks[i].version->funcs->resume(adev);
3355 DRM_ERROR("resume of IP block <%s> failed %d\n",
3356 adev->ip_blocks[i].version->funcs->name, r);
3359 adev->ip_blocks[i].status.hw = true;
3367 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
3369 * @adev: amdgpu_device pointer
3371 * First resume function for hardware IPs. The list of all the hardware
3372 * IPs that make up the asic is walked and the resume callbacks are run for
3373 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
3374 * functional state after a suspend and updates the software state as
3375 * necessary. This function is also used for restoring the GPU after a GPU
3377 * Returns 0 on success, negative error code on failure.
3379 static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
3383 for (i = 0; i < adev->num_ip_blocks; i++) {
3384 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
3386 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3387 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3388 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3389 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)
3391 r = adev->ip_blocks[i].version->funcs->resume(adev);
3393 DRM_ERROR("resume of IP block <%s> failed %d\n",
3394 adev->ip_blocks[i].version->funcs->name, r);
3397 adev->ip_blocks[i].status.hw = true;
3404 * amdgpu_device_ip_resume - run resume for hardware IPs
3406 * @adev: amdgpu_device pointer
3408 * Main resume function for hardware IPs. The hardware IPs
3409 * are split into two resume functions because they are
3410 * are also used in in recovering from a GPU reset and some additional
3411 * steps need to be take between them. In this case (S3/S4) they are
3413 * Returns 0 on success, negative error code on failure.
3415 static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
3419 if (!adev->in_s0ix) {
3420 r = amdgpu_amdkfd_resume_iommu(adev);
3425 r = amdgpu_device_ip_resume_phase1(adev);
3429 r = amdgpu_device_fw_loading(adev);
3433 r = amdgpu_device_ip_resume_phase2(adev);
3439 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
3441 * @adev: amdgpu_device pointer
3443 * Query the VBIOS data tables to determine if the board supports SR-IOV.
3445 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
3447 if (amdgpu_sriov_vf(adev)) {
3448 if (adev->is_atom_fw) {
3449 if (amdgpu_atomfirmware_gpu_virtualization_supported(adev))
3450 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
3452 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
3453 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
3456 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
3457 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
3462 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
3464 * @asic_type: AMD asic type
3466 * Check if there is DC (new modesetting infrastructre) support for an asic.
3467 * returns true if DC has support, false if not.
3469 bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
3471 switch (asic_type) {
3472 #ifdef CONFIG_DRM_AMDGPU_SI
3476 /* chips with no display hardware */
3478 #if defined(CONFIG_DRM_AMD_DC)
3484 * We have systems in the wild with these ASICs that require
3485 * LVDS and VGA support which is not supported with DC.
3487 * Fallback to the non-DC driver here by default so as not to
3488 * cause regressions.
3490 #if defined(CONFIG_DRM_AMD_DC_SI)
3491 return amdgpu_dc > 0;
3500 * We have systems in the wild with these ASICs that require
3501 * VGA support which is not supported with DC.
3503 * Fallback to the non-DC driver here by default so as not to
3504 * cause regressions.
3506 return amdgpu_dc > 0;
3508 return amdgpu_dc != 0;
3512 DRM_INFO_ONCE("Display Core has been requested via kernel parameter "
3513 "but isn't supported by ASIC, ignoring\n");
3520 * amdgpu_device_has_dc_support - check if dc is supported
3522 * @adev: amdgpu_device pointer
3524 * Returns true for supported, false for not supported
3526 bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
3528 if (adev->enable_virtual_display ||
3529 (adev->harvest_ip_mask & AMD_HARVEST_IP_DMU_MASK))
3532 return amdgpu_device_asic_has_dc_support(adev->asic_type);
3535 static void amdgpu_device_xgmi_reset_func(struct work_struct *__work)
3537 struct amdgpu_device *adev =
3538 container_of(__work, struct amdgpu_device, xgmi_reset_work);
3539 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
3541 /* It's a bug to not have a hive within this function */
3546 * Use task barrier to synchronize all xgmi reset works across the
3547 * hive. task_barrier_enter and task_barrier_exit will block
3548 * until all the threads running the xgmi reset works reach
3549 * those points. task_barrier_full will do both blocks.
3551 if (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) {
3553 task_barrier_enter(&hive->tb);
3554 adev->asic_reset_res = amdgpu_device_baco_enter(adev_to_drm(adev));
3556 if (adev->asic_reset_res)
3559 task_barrier_exit(&hive->tb);
3560 adev->asic_reset_res = amdgpu_device_baco_exit(adev_to_drm(adev));
3562 if (adev->asic_reset_res)
3565 if (adev->mmhub.ras && adev->mmhub.ras->ras_block.hw_ops &&
3566 adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count)
3567 adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count(adev);
3570 task_barrier_full(&hive->tb);
3571 adev->asic_reset_res = amdgpu_asic_reset(adev);
3575 if (adev->asic_reset_res)
3576 DRM_WARN("ASIC reset failed with error, %d for drm dev, %s",
3577 adev->asic_reset_res, adev_to_drm(adev)->unique);
3578 amdgpu_put_xgmi_hive(hive);
3581 static int amdgpu_device_get_job_timeout_settings(struct amdgpu_device *adev)
3583 char *input = amdgpu_lockup_timeout;
3584 char *timeout_setting = NULL;
3590 * By default timeout for non compute jobs is 10000
3591 * and 60000 for compute jobs.
3592 * In SR-IOV or passthrough mode, timeout for compute
3593 * jobs are 60000 by default.
3595 adev->gfx_timeout = msecs_to_jiffies(10000);
3596 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
3597 if (amdgpu_sriov_vf(adev))
3598 adev->compute_timeout = amdgpu_sriov_is_pp_one_vf(adev) ?
3599 msecs_to_jiffies(60000) : msecs_to_jiffies(10000);
3601 adev->compute_timeout = msecs_to_jiffies(60000);
3603 if (strnlen(input, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
3604 while ((timeout_setting = strsep(&input, ",")) &&
3605 strnlen(timeout_setting, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
3606 ret = kstrtol(timeout_setting, 0, &timeout);
3613 } else if (timeout < 0) {
3614 timeout = MAX_SCHEDULE_TIMEOUT;
3615 dev_warn(adev->dev, "lockup timeout disabled");
3616 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
3618 timeout = msecs_to_jiffies(timeout);
3623 adev->gfx_timeout = timeout;
3626 adev->compute_timeout = timeout;
3629 adev->sdma_timeout = timeout;
3632 adev->video_timeout = timeout;
3639 * There is only one value specified and
3640 * it should apply to all non-compute jobs.
3643 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
3644 if (amdgpu_sriov_vf(adev) || amdgpu_passthrough(adev))
3645 adev->compute_timeout = adev->gfx_timeout;
3653 * amdgpu_device_check_iommu_direct_map - check if RAM direct mapped to GPU
3655 * @adev: amdgpu_device pointer
3657 * RAM direct mapped to GPU if IOMMU is not enabled or is pass through mode
3659 static void amdgpu_device_check_iommu_direct_map(struct amdgpu_device *adev)
3661 struct iommu_domain *domain;
3663 domain = iommu_get_domain_for_dev(adev->dev);
3664 if (!domain || domain->type == IOMMU_DOMAIN_IDENTITY)
3665 adev->ram_is_direct_mapped = true;
3668 static const struct attribute *amdgpu_dev_attributes[] = {
3669 &dev_attr_product_name.attr,
3670 &dev_attr_product_number.attr,
3671 &dev_attr_serial_number.attr,
3672 &dev_attr_pcie_replay_count.attr,
3677 * amdgpu_device_init - initialize the driver
3679 * @adev: amdgpu_device pointer
3680 * @flags: driver flags
3682 * Initializes the driver info and hw (all asics).
3683 * Returns 0 for success or an error on failure.
3684 * Called at driver startup.
3686 int amdgpu_device_init(struct amdgpu_device *adev,
3689 struct drm_device *ddev = adev_to_drm(adev);
3690 struct pci_dev *pdev = adev->pdev;
3696 adev->shutdown = false;
3697 adev->flags = flags;
3699 if (amdgpu_force_asic_type >= 0 && amdgpu_force_asic_type < CHIP_LAST)
3700 adev->asic_type = amdgpu_force_asic_type;
3702 adev->asic_type = flags & AMD_ASIC_MASK;
3704 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
3705 if (amdgpu_emu_mode == 1)
3706 adev->usec_timeout *= 10;
3707 adev->gmc.gart_size = 512 * 1024 * 1024;
3708 adev->accel_working = false;
3709 adev->num_rings = 0;
3710 RCU_INIT_POINTER(adev->gang_submit, dma_fence_get_stub());
3711 adev->mman.buffer_funcs = NULL;
3712 adev->mman.buffer_funcs_ring = NULL;
3713 adev->vm_manager.vm_pte_funcs = NULL;
3714 adev->vm_manager.vm_pte_num_scheds = 0;
3715 adev->gmc.gmc_funcs = NULL;
3716 adev->harvest_ip_mask = 0x0;
3717 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
3718 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
3720 adev->smc_rreg = &amdgpu_invalid_rreg;
3721 adev->smc_wreg = &amdgpu_invalid_wreg;
3722 adev->pcie_rreg = &amdgpu_invalid_rreg;
3723 adev->pcie_wreg = &amdgpu_invalid_wreg;
3724 adev->pcie_rreg_ext = &amdgpu_invalid_rreg_ext;
3725 adev->pcie_wreg_ext = &amdgpu_invalid_wreg_ext;
3726 adev->pciep_rreg = &amdgpu_invalid_rreg;
3727 adev->pciep_wreg = &amdgpu_invalid_wreg;
3728 adev->pcie_rreg64 = &amdgpu_invalid_rreg64;
3729 adev->pcie_wreg64 = &amdgpu_invalid_wreg64;
3730 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
3731 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
3732 adev->didt_rreg = &amdgpu_invalid_rreg;
3733 adev->didt_wreg = &amdgpu_invalid_wreg;
3734 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
3735 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
3736 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
3737 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
3739 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
3740 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
3741 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
3743 /* mutex initialization are all done here so we
3744 * can recall function without having locking issues */
3745 mutex_init(&adev->firmware.mutex);
3746 mutex_init(&adev->pm.mutex);
3747 mutex_init(&adev->gfx.gpu_clock_mutex);
3748 mutex_init(&adev->srbm_mutex);
3749 mutex_init(&adev->gfx.pipe_reserve_mutex);
3750 mutex_init(&adev->gfx.gfx_off_mutex);
3751 mutex_init(&adev->gfx.partition_mutex);
3752 mutex_init(&adev->grbm_idx_mutex);
3753 mutex_init(&adev->mn_lock);
3754 mutex_init(&adev->virt.vf_errors.lock);
3755 hash_init(adev->mn_hash);
3756 mutex_init(&adev->psp.mutex);
3757 mutex_init(&adev->notifier_lock);
3758 mutex_init(&adev->pm.stable_pstate_ctx_lock);
3759 mutex_init(&adev->benchmark_mutex);
3761 amdgpu_device_init_apu_flags(adev);
3763 r = amdgpu_device_check_arguments(adev);
3767 spin_lock_init(&adev->mmio_idx_lock);
3768 spin_lock_init(&adev->smc_idx_lock);
3769 spin_lock_init(&adev->pcie_idx_lock);
3770 spin_lock_init(&adev->uvd_ctx_idx_lock);
3771 spin_lock_init(&adev->didt_idx_lock);
3772 spin_lock_init(&adev->gc_cac_idx_lock);
3773 spin_lock_init(&adev->se_cac_idx_lock);
3774 spin_lock_init(&adev->audio_endpt_idx_lock);
3775 spin_lock_init(&adev->mm_stats.lock);
3777 INIT_LIST_HEAD(&adev->shadow_list);
3778 mutex_init(&adev->shadow_list_lock);
3780 INIT_LIST_HEAD(&adev->reset_list);
3782 INIT_LIST_HEAD(&adev->ras_list);
3784 INIT_DELAYED_WORK(&adev->delayed_init_work,
3785 amdgpu_device_delayed_init_work_handler);
3786 INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work,
3787 amdgpu_device_delay_enable_gfx_off);
3789 INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
3791 adev->gfx.gfx_off_req_count = 1;
3792 adev->gfx.gfx_off_residency = 0;
3793 adev->gfx.gfx_off_entrycount = 0;
3794 adev->pm.ac_power = power_supply_is_system_supplied() > 0;
3796 atomic_set(&adev->throttling_logging_enabled, 1);
3798 * If throttling continues, logging will be performed every minute
3799 * to avoid log flooding. "-1" is subtracted since the thermal
3800 * throttling interrupt comes every second. Thus, the total logging
3801 * interval is 59 seconds(retelimited printk interval) + 1(waiting
3802 * for throttling interrupt) = 60 seconds.
3804 ratelimit_state_init(&adev->throttling_logging_rs, (60 - 1) * HZ, 1);
3805 ratelimit_set_flags(&adev->throttling_logging_rs, RATELIMIT_MSG_ON_RELEASE);
3807 /* Registers mapping */
3808 /* TODO: block userspace mapping of io register */
3809 if (adev->asic_type >= CHIP_BONAIRE) {
3810 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
3811 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
3813 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
3814 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
3817 for (i = 0; i < AMD_IP_BLOCK_TYPE_NUM; i++)
3818 atomic_set(&adev->pm.pwr_state[i], POWER_STATE_UNKNOWN);
3820 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
3821 if (adev->rmmio == NULL) {
3824 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
3825 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
3828 DRM_INFO("MCBP is enabled\n");
3831 * Reset domain needs to be present early, before XGMI hive discovered
3832 * (if any) and intitialized to use reset sem and in_gpu reset flag
3833 * early on during init and before calling to RREG32.
3835 adev->reset_domain = amdgpu_reset_create_reset_domain(SINGLE_DEVICE, "amdgpu-reset-dev");
3836 if (!adev->reset_domain)
3839 /* detect hw virtualization here */
3840 amdgpu_detect_virtualization(adev);
3842 amdgpu_device_get_pcie_info(adev);
3844 r = amdgpu_device_get_job_timeout_settings(adev);
3846 dev_err(adev->dev, "invalid lockup_timeout parameter syntax\n");
3850 /* early init functions */
3851 r = amdgpu_device_ip_early_init(adev);
3855 /* Get rid of things like offb */
3856 r = drm_aperture_remove_conflicting_pci_framebuffers(adev->pdev, &amdgpu_kms_driver);
3860 /* Enable TMZ based on IP_VERSION */
3861 amdgpu_gmc_tmz_set(adev);
3863 amdgpu_gmc_noretry_set(adev);
3864 /* Need to get xgmi info early to decide the reset behavior*/
3865 if (adev->gmc.xgmi.supported) {
3866 r = adev->gfxhub.funcs->get_xgmi_info(adev);
3871 /* enable PCIE atomic ops */
3872 if (amdgpu_sriov_vf(adev)) {
3873 if (adev->virt.fw_reserve.p_pf2vf)
3874 adev->have_atomics_support = ((struct amd_sriov_msg_pf2vf_info *)
3875 adev->virt.fw_reserve.p_pf2vf)->pcie_atomic_ops_support_flags ==
3876 (PCI_EXP_DEVCAP2_ATOMIC_COMP32 | PCI_EXP_DEVCAP2_ATOMIC_COMP64);
3877 /* APUs w/ gfx9 onwards doesn't reply on PCIe atomics, rather it is a
3878 * internal path natively support atomics, set have_atomics_support to true.
3880 } else if ((adev->flags & AMD_IS_APU) &&
3881 (adev->ip_versions[GC_HWIP][0] > IP_VERSION(9, 0, 0))) {
3882 adev->have_atomics_support = true;
3884 adev->have_atomics_support =
3885 !pci_enable_atomic_ops_to_root(adev->pdev,
3886 PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
3887 PCI_EXP_DEVCAP2_ATOMIC_COMP64);
3890 if (!adev->have_atomics_support)
3891 dev_info(adev->dev, "PCIE atomic ops is not supported\n");
3893 /* doorbell bar mapping and doorbell index init*/
3894 amdgpu_device_doorbell_init(adev);
3896 if (amdgpu_emu_mode == 1) {
3897 /* post the asic on emulation mode */
3898 emu_soc_asic_init(adev);
3899 goto fence_driver_init;
3902 amdgpu_reset_init(adev);
3904 /* detect if we are with an SRIOV vbios */
3906 amdgpu_device_detect_sriov_bios(adev);
3908 /* check if we need to reset the asic
3909 * E.g., driver was not cleanly unloaded previously, etc.
3911 if (!amdgpu_sriov_vf(adev) && amdgpu_asic_need_reset_on_init(adev)) {
3912 if (adev->gmc.xgmi.num_physical_nodes) {
3913 dev_info(adev->dev, "Pending hive reset.\n");
3914 adev->gmc.xgmi.pending_reset = true;
3915 /* Only need to init necessary block for SMU to handle the reset */
3916 for (i = 0; i < adev->num_ip_blocks; i++) {
3917 if (!adev->ip_blocks[i].status.valid)
3919 if (!(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3920 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3921 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3922 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC)) {
3923 DRM_DEBUG("IP %s disabled for hw_init.\n",
3924 adev->ip_blocks[i].version->funcs->name);
3925 adev->ip_blocks[i].status.hw = true;
3929 tmp = amdgpu_reset_method;
3930 /* It should do a default reset when loading or reloading the driver,
3931 * regardless of the module parameter reset_method.
3933 amdgpu_reset_method = AMD_RESET_METHOD_NONE;
3934 r = amdgpu_asic_reset(adev);
3935 amdgpu_reset_method = tmp;
3937 dev_err(adev->dev, "asic reset on init failed\n");
3943 /* Post card if necessary */
3944 if (amdgpu_device_need_post(adev)) {
3946 dev_err(adev->dev, "no vBIOS found\n");
3950 DRM_INFO("GPU posting now...\n");
3951 r = amdgpu_device_asic_init(adev);
3953 dev_err(adev->dev, "gpu post error!\n");
3959 if (adev->is_atom_fw) {
3960 /* Initialize clocks */
3961 r = amdgpu_atomfirmware_get_clock_info(adev);
3963 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
3964 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3968 /* Initialize clocks */
3969 r = amdgpu_atombios_get_clock_info(adev);
3971 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
3972 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3975 /* init i2c buses */
3976 if (!amdgpu_device_has_dc_support(adev))
3977 amdgpu_atombios_i2c_init(adev);
3983 r = amdgpu_fence_driver_sw_init(adev);
3985 dev_err(adev->dev, "amdgpu_fence_driver_sw_init failed\n");
3986 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
3990 /* init the mode config */
3991 drm_mode_config_init(adev_to_drm(adev));
3993 r = amdgpu_device_ip_init(adev);
3995 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
3996 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
3997 goto release_ras_con;
4000 amdgpu_fence_driver_hw_init(adev);
4003 "SE %d, SH per SE %d, CU per SH %d, active_cu_number %d\n",
4004 adev->gfx.config.max_shader_engines,
4005 adev->gfx.config.max_sh_per_se,
4006 adev->gfx.config.max_cu_per_sh,
4007 adev->gfx.cu_info.number);
4009 adev->accel_working = true;
4011 amdgpu_vm_check_compute_bug(adev);
4013 /* Initialize the buffer migration limit. */
4014 if (amdgpu_moverate >= 0)
4015 max_MBps = amdgpu_moverate;
4017 max_MBps = 8; /* Allow 8 MB/s. */
4018 /* Get a log2 for easy divisions. */
4019 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
4021 r = amdgpu_pm_sysfs_init(adev);
4023 DRM_ERROR("registering pm sysfs failed (%d).\n", r);
4025 r = amdgpu_ucode_sysfs_init(adev);
4027 adev->ucode_sysfs_en = false;
4028 DRM_ERROR("Creating firmware sysfs failed (%d).\n", r);
4030 adev->ucode_sysfs_en = true;
4032 r = amdgpu_psp_sysfs_init(adev);
4034 adev->psp_sysfs_en = false;
4035 if (!amdgpu_sriov_vf(adev))
4036 DRM_ERROR("Creating psp sysfs failed\n");
4038 adev->psp_sysfs_en = true;
4041 * Register gpu instance before amdgpu_device_enable_mgpu_fan_boost.
4042 * Otherwise the mgpu fan boost feature will be skipped due to the
4043 * gpu instance is counted less.
4045 amdgpu_register_gpu_instance(adev);
4047 /* enable clockgating, etc. after ib tests, etc. since some blocks require
4048 * explicit gating rather than handling it automatically.
4050 if (!adev->gmc.xgmi.pending_reset) {
4051 r = amdgpu_device_ip_late_init(adev);
4053 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
4054 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
4055 goto release_ras_con;
4058 amdgpu_ras_resume(adev);
4059 queue_delayed_work(system_wq, &adev->delayed_init_work,
4060 msecs_to_jiffies(AMDGPU_RESUME_MS));
4063 if (amdgpu_sriov_vf(adev)) {
4064 amdgpu_virt_release_full_gpu(adev, true);
4065 flush_delayed_work(&adev->delayed_init_work);
4068 r = sysfs_create_files(&adev->dev->kobj, amdgpu_dev_attributes);
4070 dev_err(adev->dev, "Could not create amdgpu device attr\n");
4072 if (IS_ENABLED(CONFIG_PERF_EVENTS))
4073 r = amdgpu_pmu_init(adev);
4075 dev_err(adev->dev, "amdgpu_pmu_init failed\n");
4077 /* Have stored pci confspace at hand for restore in sudden PCI error */
4078 if (amdgpu_device_cache_pci_state(adev->pdev))
4079 pci_restore_state(pdev);
4081 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
4082 /* this will fail for cards that aren't VGA class devices, just
4084 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
4085 vga_client_register(adev->pdev, amdgpu_device_vga_set_decode);
4087 px = amdgpu_device_supports_px(ddev);
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);
4095 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
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));
4101 amdgpu_device_check_iommu_direct_map(adev);
4106 if (amdgpu_sriov_vf(adev))
4107 amdgpu_virt_release_full_gpu(adev, true);
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;
4120 amdgpu_release_ras_context(adev);
4123 amdgpu_vf_error_trans_all(adev);
4128 static void amdgpu_device_unmap_mmio(struct amdgpu_device *adev)
4131 /* Clear all CPU mappings pointing to this device */
4132 unmap_mapping_range(adev->ddev.anon_inode->i_mapping, 0, 0, 1);
4134 /* Unmap all mapped bars - Doorbell, registers and VRAM */
4135 amdgpu_device_doorbell_fini(adev);
4137 iounmap(adev->rmmio);
4139 if (adev->mman.aper_base_kaddr)
4140 iounmap(adev->mman.aper_base_kaddr);
4141 adev->mman.aper_base_kaddr = NULL;
4143 /* Memory manager related */
4144 if (!adev->gmc.xgmi.connected_to_cpu && !adev->gmc.is_app_apu) {
4145 arch_phys_wc_del(adev->gmc.vram_mtrr);
4146 arch_io_free_memtype_wc(adev->gmc.aper_base, adev->gmc.aper_size);
4151 * amdgpu_device_fini_hw - tear down the driver
4153 * @adev: amdgpu_device pointer
4155 * Tear down the driver info (all asics).
4156 * Called at driver shutdown.
4158 void amdgpu_device_fini_hw(struct amdgpu_device *adev)
4160 dev_info(adev->dev, "amdgpu: finishing device.\n");
4161 flush_delayed_work(&adev->delayed_init_work);
4162 adev->shutdown = true;
4164 /* make sure IB test finished before entering exclusive mode
4165 * to avoid preemption on IB test
4167 if (amdgpu_sriov_vf(adev)) {
4168 amdgpu_virt_request_full_gpu(adev, false);
4169 amdgpu_virt_fini_data_exchange(adev);
4172 /* disable all interrupts */
4173 amdgpu_irq_disable_all(adev);
4174 if (adev->mode_info.mode_config_initialized) {
4175 if (!drm_drv_uses_atomic_modeset(adev_to_drm(adev)))
4176 drm_helper_force_disable_all(adev_to_drm(adev));
4178 drm_atomic_helper_shutdown(adev_to_drm(adev));
4180 amdgpu_fence_driver_hw_fini(adev);
4182 if (adev->mman.initialized)
4183 drain_workqueue(adev->mman.bdev.wq);
4185 if (adev->pm.sysfs_initialized)
4186 amdgpu_pm_sysfs_fini(adev);
4187 if (adev->ucode_sysfs_en)
4188 amdgpu_ucode_sysfs_fini(adev);
4189 if (adev->psp_sysfs_en)
4190 amdgpu_psp_sysfs_fini(adev);
4191 sysfs_remove_files(&adev->dev->kobj, amdgpu_dev_attributes);
4193 /* disable ras feature must before hw fini */
4194 amdgpu_ras_pre_fini(adev);
4196 amdgpu_device_ip_fini_early(adev);
4198 amdgpu_irq_fini_hw(adev);
4200 if (adev->mman.initialized)
4201 ttm_device_clear_dma_mappings(&adev->mman.bdev);
4203 amdgpu_gart_dummy_page_fini(adev);
4205 if (drm_dev_is_unplugged(adev_to_drm(adev)))
4206 amdgpu_device_unmap_mmio(adev);
4210 void amdgpu_device_fini_sw(struct amdgpu_device *adev)
4215 amdgpu_fence_driver_sw_fini(adev);
4216 amdgpu_device_ip_fini(adev);
4217 amdgpu_ucode_release(&adev->firmware.gpu_info_fw);
4218 adev->accel_working = false;
4219 dma_fence_put(rcu_dereference_protected(adev->gang_submit, true));
4221 amdgpu_reset_fini(adev);
4223 /* free i2c buses */
4224 if (!amdgpu_device_has_dc_support(adev))
4225 amdgpu_i2c_fini(adev);
4227 if (amdgpu_emu_mode != 1)
4228 amdgpu_atombios_fini(adev);
4233 px = amdgpu_device_supports_px(adev_to_drm(adev));
4235 if (px || (!pci_is_thunderbolt_attached(adev->pdev) &&
4236 apple_gmux_detect(NULL, NULL)))
4237 vga_switcheroo_unregister_client(adev->pdev);
4240 vga_switcheroo_fini_domain_pm_ops(adev->dev);
4242 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
4243 vga_client_unregister(adev->pdev);
4245 if (drm_dev_enter(adev_to_drm(adev), &idx)) {
4247 iounmap(adev->rmmio);
4249 amdgpu_device_doorbell_fini(adev);
4253 if (IS_ENABLED(CONFIG_PERF_EVENTS))
4254 amdgpu_pmu_fini(adev);
4255 if (adev->mman.discovery_bin)
4256 amdgpu_discovery_fini(adev);
4258 amdgpu_reset_put_reset_domain(adev->reset_domain);
4259 adev->reset_domain = NULL;
4261 kfree(adev->pci_state);
4266 * amdgpu_device_evict_resources - evict device resources
4267 * @adev: amdgpu device object
4269 * Evicts all ttm device resources(vram BOs, gart table) from the lru list
4270 * of the vram memory type. Mainly used for evicting device resources
4274 static int amdgpu_device_evict_resources(struct amdgpu_device *adev)
4278 /* No need to evict vram on APUs for suspend to ram or s2idle */
4279 if ((adev->in_s3 || adev->in_s0ix) && (adev->flags & AMD_IS_APU))
4282 ret = amdgpu_ttm_evict_resources(adev, TTM_PL_VRAM);
4284 DRM_WARN("evicting device resources failed\n");
4292 * amdgpu_device_suspend - initiate device suspend
4294 * @dev: drm dev pointer
4295 * @fbcon : notify the fbdev of suspend
4297 * Puts the hw in the suspend state (all asics).
4298 * Returns 0 for success or an error on failure.
4299 * Called at driver suspend.
4301 int amdgpu_device_suspend(struct drm_device *dev, bool fbcon)
4303 struct amdgpu_device *adev = drm_to_adev(dev);
4306 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
4309 adev->in_suspend = true;
4311 /* Evict the majority of BOs before grabbing the full access */
4312 r = amdgpu_device_evict_resources(adev);
4316 if (amdgpu_sriov_vf(adev)) {
4317 amdgpu_virt_fini_data_exchange(adev);
4318 r = amdgpu_virt_request_full_gpu(adev, false);
4323 if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DEV_D3))
4324 DRM_WARN("smart shift update failed\n");
4327 drm_fb_helper_set_suspend_unlocked(adev_to_drm(adev)->fb_helper, true);
4329 cancel_delayed_work_sync(&adev->delayed_init_work);
4331 amdgpu_ras_suspend(adev);
4333 amdgpu_device_ip_suspend_phase1(adev);
4336 amdgpu_amdkfd_suspend(adev, adev->in_runpm);
4338 r = amdgpu_device_evict_resources(adev);
4342 amdgpu_fence_driver_hw_fini(adev);
4344 amdgpu_device_ip_suspend_phase2(adev);
4346 if (amdgpu_sriov_vf(adev))
4347 amdgpu_virt_release_full_gpu(adev, false);
4353 * amdgpu_device_resume - initiate device resume
4355 * @dev: drm dev pointer
4356 * @fbcon : notify the fbdev of resume
4358 * Bring the hw back to operating state (all asics).
4359 * Returns 0 for success or an error on failure.
4360 * Called at driver resume.
4362 int amdgpu_device_resume(struct drm_device *dev, bool fbcon)
4364 struct amdgpu_device *adev = drm_to_adev(dev);
4367 if (amdgpu_sriov_vf(adev)) {
4368 r = amdgpu_virt_request_full_gpu(adev, true);
4373 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
4377 amdgpu_dpm_gfx_state_change(adev, sGpuChangeState_D0Entry);
4380 if (amdgpu_device_need_post(adev)) {
4381 r = amdgpu_device_asic_init(adev);
4383 dev_err(adev->dev, "amdgpu asic init failed\n");
4386 r = amdgpu_device_ip_resume(adev);
4389 dev_err(adev->dev, "amdgpu_device_ip_resume failed (%d).\n", r);
4392 amdgpu_fence_driver_hw_init(adev);
4394 r = amdgpu_device_ip_late_init(adev);
4398 queue_delayed_work(system_wq, &adev->delayed_init_work,
4399 msecs_to_jiffies(AMDGPU_RESUME_MS));
4401 if (!adev->in_s0ix) {
4402 r = amdgpu_amdkfd_resume(adev, adev->in_runpm);
4408 if (amdgpu_sriov_vf(adev)) {
4409 amdgpu_virt_init_data_exchange(adev);
4410 amdgpu_virt_release_full_gpu(adev, true);
4416 /* Make sure IB tests flushed */
4417 flush_delayed_work(&adev->delayed_init_work);
4420 drm_fb_helper_set_suspend_unlocked(adev_to_drm(adev)->fb_helper, false);
4422 amdgpu_ras_resume(adev);
4424 if (adev->mode_info.num_crtc) {
4426 * Most of the connector probing functions try to acquire runtime pm
4427 * refs to ensure that the GPU is powered on when connector polling is
4428 * performed. Since we're calling this from a runtime PM callback,
4429 * trying to acquire rpm refs will cause us to deadlock.
4431 * Since we're guaranteed to be holding the rpm lock, it's safe to
4432 * temporarily disable the rpm helpers so this doesn't deadlock us.
4435 dev->dev->power.disable_depth++;
4437 if (!adev->dc_enabled)
4438 drm_helper_hpd_irq_event(dev);
4440 drm_kms_helper_hotplug_event(dev);
4442 dev->dev->power.disable_depth--;
4445 adev->in_suspend = false;
4447 if (adev->enable_mes)
4448 amdgpu_mes_self_test(adev);
4450 if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DEV_D0))
4451 DRM_WARN("smart shift update failed\n");
4457 * amdgpu_device_ip_check_soft_reset - did soft reset succeed
4459 * @adev: amdgpu_device pointer
4461 * The list of all the hardware IPs that make up the asic is walked and
4462 * the check_soft_reset callbacks are run. check_soft_reset determines
4463 * if the asic is still hung or not.
4464 * Returns true if any of the IPs are still in a hung state, false if not.
4466 static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
4469 bool asic_hang = false;
4471 if (amdgpu_sriov_vf(adev))
4474 if (amdgpu_asic_need_full_reset(adev))
4477 for (i = 0; i < adev->num_ip_blocks; i++) {
4478 if (!adev->ip_blocks[i].status.valid)
4480 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
4481 adev->ip_blocks[i].status.hang =
4482 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
4483 if (adev->ip_blocks[i].status.hang) {
4484 dev_info(adev->dev, "IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
4492 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
4494 * @adev: amdgpu_device pointer
4496 * The list of all the hardware IPs that make up the asic is walked and the
4497 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
4498 * handles any IP specific hardware or software state changes that are
4499 * necessary for a soft reset to succeed.
4500 * Returns 0 on success, negative error code on failure.
4502 static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
4506 for (i = 0; i < adev->num_ip_blocks; i++) {
4507 if (!adev->ip_blocks[i].status.valid)
4509 if (adev->ip_blocks[i].status.hang &&
4510 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
4511 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
4521 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
4523 * @adev: amdgpu_device pointer
4525 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
4526 * reset is necessary to recover.
4527 * Returns true if a full asic reset is required, false if not.
4529 static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
4533 if (amdgpu_asic_need_full_reset(adev))
4536 for (i = 0; i < adev->num_ip_blocks; i++) {
4537 if (!adev->ip_blocks[i].status.valid)
4539 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
4540 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
4541 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
4542 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
4543 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
4544 if (adev->ip_blocks[i].status.hang) {
4545 dev_info(adev->dev, "Some block need full reset!\n");
4554 * amdgpu_device_ip_soft_reset - do a soft reset
4556 * @adev: amdgpu_device pointer
4558 * The list of all the hardware IPs that make up the asic is walked and the
4559 * soft_reset callbacks are run if the block is hung. soft_reset handles any
4560 * IP specific hardware or software state changes that are necessary to soft
4562 * Returns 0 on success, negative error code on failure.
4564 static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
4568 for (i = 0; i < adev->num_ip_blocks; i++) {
4569 if (!adev->ip_blocks[i].status.valid)
4571 if (adev->ip_blocks[i].status.hang &&
4572 adev->ip_blocks[i].version->funcs->soft_reset) {
4573 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
4583 * amdgpu_device_ip_post_soft_reset - clean up from soft reset
4585 * @adev: amdgpu_device pointer
4587 * The list of all the hardware IPs that make up the asic is walked and the
4588 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
4589 * handles any IP specific hardware or software state changes that are
4590 * necessary after the IP has been soft reset.
4591 * Returns 0 on success, negative error code on failure.
4593 static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
4597 for (i = 0; i < adev->num_ip_blocks; i++) {
4598 if (!adev->ip_blocks[i].status.valid)
4600 if (adev->ip_blocks[i].status.hang &&
4601 adev->ip_blocks[i].version->funcs->post_soft_reset)
4602 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
4611 * amdgpu_device_recover_vram - Recover some VRAM contents
4613 * @adev: amdgpu_device pointer
4615 * Restores the contents of VRAM buffers from the shadows in GTT. Used to
4616 * restore things like GPUVM page tables after a GPU reset where
4617 * the contents of VRAM might be lost.
4620 * 0 on success, negative error code on failure.
4622 static int amdgpu_device_recover_vram(struct amdgpu_device *adev)
4624 struct dma_fence *fence = NULL, *next = NULL;
4625 struct amdgpu_bo *shadow;
4626 struct amdgpu_bo_vm *vmbo;
4629 if (amdgpu_sriov_runtime(adev))
4630 tmo = msecs_to_jiffies(8000);
4632 tmo = msecs_to_jiffies(100);
4634 dev_info(adev->dev, "recover vram bo from shadow start\n");
4635 mutex_lock(&adev->shadow_list_lock);
4636 list_for_each_entry(vmbo, &adev->shadow_list, shadow_list) {
4637 /* If vm is compute context or adev is APU, shadow will be NULL */
4640 shadow = vmbo->shadow;
4642 /* No need to recover an evicted BO */
4643 if (shadow->tbo.resource->mem_type != TTM_PL_TT ||
4644 shadow->tbo.resource->start == AMDGPU_BO_INVALID_OFFSET ||
4645 shadow->parent->tbo.resource->mem_type != TTM_PL_VRAM)
4648 r = amdgpu_bo_restore_shadow(shadow, &next);
4653 tmo = dma_fence_wait_timeout(fence, false, tmo);
4654 dma_fence_put(fence);
4659 } else if (tmo < 0) {
4667 mutex_unlock(&adev->shadow_list_lock);
4670 tmo = dma_fence_wait_timeout(fence, false, tmo);
4671 dma_fence_put(fence);
4673 if (r < 0 || tmo <= 0) {
4674 dev_err(adev->dev, "recover vram bo from shadow failed, r is %ld, tmo is %ld\n", r, tmo);
4678 dev_info(adev->dev, "recover vram bo from shadow done\n");
4684 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
4686 * @adev: amdgpu_device pointer
4687 * @from_hypervisor: request from hypervisor
4689 * do VF FLR and reinitialize Asic
4690 * return 0 means succeeded otherwise failed
4692 static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
4693 bool from_hypervisor)
4696 struct amdgpu_hive_info *hive = NULL;
4697 int retry_limit = 0;
4700 amdgpu_amdkfd_pre_reset(adev);
4702 if (from_hypervisor)
4703 r = amdgpu_virt_request_full_gpu(adev, true);
4705 r = amdgpu_virt_reset_gpu(adev);
4709 /* Resume IP prior to SMC */
4710 r = amdgpu_device_ip_reinit_early_sriov(adev);
4714 amdgpu_virt_init_data_exchange(adev);
4716 r = amdgpu_device_fw_loading(adev);
4720 /* now we are okay to resume SMC/CP/SDMA */
4721 r = amdgpu_device_ip_reinit_late_sriov(adev);
4725 hive = amdgpu_get_xgmi_hive(adev);
4726 /* Update PSP FW topology after reset */
4727 if (hive && adev->gmc.xgmi.num_physical_nodes > 1)
4728 r = amdgpu_xgmi_update_topology(hive, adev);
4731 amdgpu_put_xgmi_hive(hive);
4734 amdgpu_irq_gpu_reset_resume_helper(adev);
4735 r = amdgpu_ib_ring_tests(adev);
4737 amdgpu_amdkfd_post_reset(adev);
4741 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
4742 amdgpu_inc_vram_lost(adev);
4743 r = amdgpu_device_recover_vram(adev);
4745 amdgpu_virt_release_full_gpu(adev, true);
4747 if (AMDGPU_RETRY_SRIOV_RESET(r)) {
4748 if (retry_limit < AMDGPU_MAX_RETRY_LIMIT) {
4752 DRM_ERROR("GPU reset retry is beyond the retry limit\n");
4759 * amdgpu_device_has_job_running - check if there is any job in mirror list
4761 * @adev: amdgpu_device pointer
4763 * check if there is any job in mirror list
4765 bool amdgpu_device_has_job_running(struct amdgpu_device *adev)
4768 struct drm_sched_job *job;
4770 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4771 struct amdgpu_ring *ring = adev->rings[i];
4773 if (!ring || !ring->sched.thread)
4776 spin_lock(&ring->sched.job_list_lock);
4777 job = list_first_entry_or_null(&ring->sched.pending_list,
4778 struct drm_sched_job, list);
4779 spin_unlock(&ring->sched.job_list_lock);
4787 * amdgpu_device_should_recover_gpu - check if we should try GPU recovery
4789 * @adev: amdgpu_device pointer
4791 * Check amdgpu_gpu_recovery and SRIOV status to see if we should try to recover
4794 bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev)
4797 if (amdgpu_gpu_recovery == 0)
4800 /* Skip soft reset check in fatal error mode */
4801 if (!amdgpu_ras_is_poison_mode_supported(adev))
4804 if (amdgpu_sriov_vf(adev))
4807 if (amdgpu_gpu_recovery == -1) {
4808 switch (adev->asic_type) {
4809 #ifdef CONFIG_DRM_AMDGPU_SI
4816 #ifdef CONFIG_DRM_AMDGPU_CIK
4823 case CHIP_CYAN_SKILLFISH:
4833 dev_info(adev->dev, "GPU recovery disabled.\n");
4837 int amdgpu_device_mode1_reset(struct amdgpu_device *adev)
4842 amdgpu_atombios_scratch_regs_engine_hung(adev, true);
4844 dev_info(adev->dev, "GPU mode1 reset\n");
4847 pci_clear_master(adev->pdev);
4849 amdgpu_device_cache_pci_state(adev->pdev);
4851 if (amdgpu_dpm_is_mode1_reset_supported(adev)) {
4852 dev_info(adev->dev, "GPU smu mode1 reset\n");
4853 ret = amdgpu_dpm_mode1_reset(adev);
4855 dev_info(adev->dev, "GPU psp mode1 reset\n");
4856 ret = psp_gpu_reset(adev);
4860 dev_err(adev->dev, "GPU mode1 reset failed\n");
4862 amdgpu_device_load_pci_state(adev->pdev);
4864 /* wait for asic to come out of reset */
4865 for (i = 0; i < adev->usec_timeout; i++) {
4866 u32 memsize = adev->nbio.funcs->get_memsize(adev);
4868 if (memsize != 0xffffffff)
4873 amdgpu_atombios_scratch_regs_engine_hung(adev, false);
4877 int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
4878 struct amdgpu_reset_context *reset_context)
4881 struct amdgpu_job *job = NULL;
4882 bool need_full_reset =
4883 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4885 if (reset_context->reset_req_dev == adev)
4886 job = reset_context->job;
4888 if (amdgpu_sriov_vf(adev)) {
4889 /* stop the data exchange thread */
4890 amdgpu_virt_fini_data_exchange(adev);
4893 amdgpu_fence_driver_isr_toggle(adev, true);
4895 /* block all schedulers and reset given job's ring */
4896 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4897 struct amdgpu_ring *ring = adev->rings[i];
4899 if (!ring || !ring->sched.thread)
4902 /*clear job fence from fence drv to avoid force_completion
4903 *leave NULL and vm flush fence in fence drv */
4904 amdgpu_fence_driver_clear_job_fences(ring);
4906 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
4907 amdgpu_fence_driver_force_completion(ring);
4910 amdgpu_fence_driver_isr_toggle(adev, false);
4913 drm_sched_increase_karma(&job->base);
4915 r = amdgpu_reset_prepare_hwcontext(adev, reset_context);
4916 /* If reset handler not implemented, continue; otherwise return */
4922 /* Don't suspend on bare metal if we are not going to HW reset the ASIC */
4923 if (!amdgpu_sriov_vf(adev)) {
4925 if (!need_full_reset)
4926 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
4928 if (!need_full_reset && amdgpu_gpu_recovery &&
4929 amdgpu_device_ip_check_soft_reset(adev)) {
4930 amdgpu_device_ip_pre_soft_reset(adev);
4931 r = amdgpu_device_ip_soft_reset(adev);
4932 amdgpu_device_ip_post_soft_reset(adev);
4933 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
4934 dev_info(adev->dev, "soft reset failed, will fallback to full reset!\n");
4935 need_full_reset = true;
4939 if (need_full_reset)
4940 r = amdgpu_device_ip_suspend(adev);
4941 if (need_full_reset)
4942 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4944 clear_bit(AMDGPU_NEED_FULL_RESET,
4945 &reset_context->flags);
4951 static int amdgpu_reset_reg_dumps(struct amdgpu_device *adev)
4955 lockdep_assert_held(&adev->reset_domain->sem);
4957 for (i = 0; i < adev->num_regs; i++) {
4958 adev->reset_dump_reg_value[i] = RREG32(adev->reset_dump_reg_list[i]);
4959 trace_amdgpu_reset_reg_dumps(adev->reset_dump_reg_list[i],
4960 adev->reset_dump_reg_value[i]);
4966 #ifdef CONFIG_DEV_COREDUMP
4967 static ssize_t amdgpu_devcoredump_read(char *buffer, loff_t offset,
4968 size_t count, void *data, size_t datalen)
4970 struct drm_printer p;
4971 struct amdgpu_device *adev = data;
4972 struct drm_print_iterator iter;
4977 iter.start = offset;
4978 iter.remain = count;
4980 p = drm_coredump_printer(&iter);
4982 drm_printf(&p, "**** AMDGPU Device Coredump ****\n");
4983 drm_printf(&p, "kernel: " UTS_RELEASE "\n");
4984 drm_printf(&p, "module: " KBUILD_MODNAME "\n");
4985 drm_printf(&p, "time: %lld.%09ld\n", adev->reset_time.tv_sec, adev->reset_time.tv_nsec);
4986 if (adev->reset_task_info.pid)
4987 drm_printf(&p, "process_name: %s PID: %d\n",
4988 adev->reset_task_info.process_name,
4989 adev->reset_task_info.pid);
4991 if (adev->reset_vram_lost)
4992 drm_printf(&p, "VRAM is lost due to GPU reset!\n");
4993 if (adev->num_regs) {
4994 drm_printf(&p, "AMDGPU register dumps:\nOffset: Value:\n");
4996 for (i = 0; i < adev->num_regs; i++)
4997 drm_printf(&p, "0x%08x: 0x%08x\n",
4998 adev->reset_dump_reg_list[i],
4999 adev->reset_dump_reg_value[i]);
5002 return count - iter.remain;
5005 static void amdgpu_devcoredump_free(void *data)
5009 static void amdgpu_reset_capture_coredumpm(struct amdgpu_device *adev)
5011 struct drm_device *dev = adev_to_drm(adev);
5013 ktime_get_ts64(&adev->reset_time);
5014 dev_coredumpm(dev->dev, THIS_MODULE, adev, 0, GFP_KERNEL,
5015 amdgpu_devcoredump_read, amdgpu_devcoredump_free);
5019 int amdgpu_do_asic_reset(struct list_head *device_list_handle,
5020 struct amdgpu_reset_context *reset_context)
5022 struct amdgpu_device *tmp_adev = NULL;
5023 bool need_full_reset, skip_hw_reset, vram_lost = false;
5025 bool gpu_reset_for_dev_remove = 0;
5027 /* Try reset handler method first */
5028 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
5030 amdgpu_reset_reg_dumps(tmp_adev);
5032 reset_context->reset_device_list = device_list_handle;
5033 r = amdgpu_reset_perform_reset(tmp_adev, reset_context);
5034 /* If reset handler not implemented, continue; otherwise return */
5040 /* Reset handler not implemented, use the default method */
5042 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
5043 skip_hw_reset = test_bit(AMDGPU_SKIP_HW_RESET, &reset_context->flags);
5045 gpu_reset_for_dev_remove =
5046 test_bit(AMDGPU_RESET_FOR_DEVICE_REMOVE, &reset_context->flags) &&
5047 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
5050 * ASIC reset has to be done on all XGMI hive nodes ASAP
5051 * to allow proper links negotiation in FW (within 1 sec)
5053 if (!skip_hw_reset && need_full_reset) {
5054 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5055 /* For XGMI run all resets in parallel to speed up the process */
5056 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
5057 tmp_adev->gmc.xgmi.pending_reset = false;
5058 if (!queue_work(system_unbound_wq, &tmp_adev->xgmi_reset_work))
5061 r = amdgpu_asic_reset(tmp_adev);
5064 dev_err(tmp_adev->dev, "ASIC reset failed with error, %d for drm dev, %s",
5065 r, adev_to_drm(tmp_adev)->unique);
5070 /* For XGMI wait for all resets to complete before proceed */
5072 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5073 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
5074 flush_work(&tmp_adev->xgmi_reset_work);
5075 r = tmp_adev->asic_reset_res;
5083 if (!r && amdgpu_ras_intr_triggered()) {
5084 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5085 if (tmp_adev->mmhub.ras && tmp_adev->mmhub.ras->ras_block.hw_ops &&
5086 tmp_adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count)
5087 tmp_adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count(tmp_adev);
5090 amdgpu_ras_intr_cleared();
5093 /* Since the mode1 reset affects base ip blocks, the
5094 * phase1 ip blocks need to be resumed. Otherwise there
5095 * will be a BIOS signature error and the psp bootloader
5096 * can't load kdb on the next amdgpu install.
5098 if (gpu_reset_for_dev_remove) {
5099 list_for_each_entry(tmp_adev, device_list_handle, reset_list)
5100 amdgpu_device_ip_resume_phase1(tmp_adev);
5105 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5106 if (need_full_reset) {
5108 r = amdgpu_device_asic_init(tmp_adev);
5110 dev_warn(tmp_adev->dev, "asic atom init failed!");
5112 dev_info(tmp_adev->dev, "GPU reset succeeded, trying to resume\n");
5113 r = amdgpu_amdkfd_resume_iommu(tmp_adev);
5117 r = amdgpu_device_ip_resume_phase1(tmp_adev);
5121 vram_lost = amdgpu_device_check_vram_lost(tmp_adev);
5122 #ifdef CONFIG_DEV_COREDUMP
5123 tmp_adev->reset_vram_lost = vram_lost;
5124 memset(&tmp_adev->reset_task_info, 0,
5125 sizeof(tmp_adev->reset_task_info));
5126 if (reset_context->job && reset_context->job->vm)
5127 tmp_adev->reset_task_info =
5128 reset_context->job->vm->task_info;
5129 amdgpu_reset_capture_coredumpm(tmp_adev);
5132 DRM_INFO("VRAM is lost due to GPU reset!\n");
5133 amdgpu_inc_vram_lost(tmp_adev);
5136 r = amdgpu_device_fw_loading(tmp_adev);
5140 r = amdgpu_device_ip_resume_phase2(tmp_adev);
5145 amdgpu_device_fill_reset_magic(tmp_adev);
5148 * Add this ASIC as tracked as reset was already
5149 * complete successfully.
5151 amdgpu_register_gpu_instance(tmp_adev);
5153 if (!reset_context->hive &&
5154 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
5155 amdgpu_xgmi_add_device(tmp_adev);
5157 r = amdgpu_device_ip_late_init(tmp_adev);
5161 drm_fb_helper_set_suspend_unlocked(adev_to_drm(tmp_adev)->fb_helper, false);
5164 * The GPU enters bad state once faulty pages
5165 * by ECC has reached the threshold, and ras
5166 * recovery is scheduled next. So add one check
5167 * here to break recovery if it indeed exceeds
5168 * bad page threshold, and remind user to
5169 * retire this GPU or setting one bigger
5170 * bad_page_threshold value to fix this once
5171 * probing driver again.
5173 if (!amdgpu_ras_eeprom_check_err_threshold(tmp_adev)) {
5175 amdgpu_ras_resume(tmp_adev);
5181 /* Update PSP FW topology after reset */
5182 if (reset_context->hive &&
5183 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
5184 r = amdgpu_xgmi_update_topology(
5185 reset_context->hive, tmp_adev);
5191 amdgpu_irq_gpu_reset_resume_helper(tmp_adev);
5192 r = amdgpu_ib_ring_tests(tmp_adev);
5194 dev_err(tmp_adev->dev, "ib ring test failed (%d).\n", r);
5195 need_full_reset = true;
5202 r = amdgpu_device_recover_vram(tmp_adev);
5204 tmp_adev->asic_reset_res = r;
5208 if (need_full_reset)
5209 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
5211 clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
5215 static void amdgpu_device_set_mp1_state(struct amdgpu_device *adev)
5218 switch (amdgpu_asic_reset_method(adev)) {
5219 case AMD_RESET_METHOD_MODE1:
5220 adev->mp1_state = PP_MP1_STATE_SHUTDOWN;
5222 case AMD_RESET_METHOD_MODE2:
5223 adev->mp1_state = PP_MP1_STATE_RESET;
5226 adev->mp1_state = PP_MP1_STATE_NONE;
5231 static void amdgpu_device_unset_mp1_state(struct amdgpu_device *adev)
5233 amdgpu_vf_error_trans_all(adev);
5234 adev->mp1_state = PP_MP1_STATE_NONE;
5237 static void amdgpu_device_resume_display_audio(struct amdgpu_device *adev)
5239 struct pci_dev *p = NULL;
5241 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
5242 adev->pdev->bus->number, 1);
5244 pm_runtime_enable(&(p->dev));
5245 pm_runtime_resume(&(p->dev));
5251 static int amdgpu_device_suspend_display_audio(struct amdgpu_device *adev)
5253 enum amd_reset_method reset_method;
5254 struct pci_dev *p = NULL;
5258 * For now, only BACO and mode1 reset are confirmed
5259 * to suffer the audio issue without proper suspended.
5261 reset_method = amdgpu_asic_reset_method(adev);
5262 if ((reset_method != AMD_RESET_METHOD_BACO) &&
5263 (reset_method != AMD_RESET_METHOD_MODE1))
5266 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
5267 adev->pdev->bus->number, 1);
5271 expires = pm_runtime_autosuspend_expiration(&(p->dev));
5274 * If we cannot get the audio device autosuspend delay,
5275 * a fixed 4S interval will be used. Considering 3S is
5276 * the audio controller default autosuspend delay setting.
5277 * 4S used here is guaranteed to cover that.
5279 expires = ktime_get_mono_fast_ns() + NSEC_PER_SEC * 4ULL;
5281 while (!pm_runtime_status_suspended(&(p->dev))) {
5282 if (!pm_runtime_suspend(&(p->dev)))
5285 if (expires < ktime_get_mono_fast_ns()) {
5286 dev_warn(adev->dev, "failed to suspend display audio\n");
5288 /* TODO: abort the succeeding gpu reset? */
5293 pm_runtime_disable(&(p->dev));
5299 static inline void amdgpu_device_stop_pending_resets(struct amdgpu_device *adev)
5301 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
5303 #if defined(CONFIG_DEBUG_FS)
5304 if (!amdgpu_sriov_vf(adev))
5305 cancel_work(&adev->reset_work);
5309 cancel_work(&adev->kfd.reset_work);
5311 if (amdgpu_sriov_vf(adev))
5312 cancel_work(&adev->virt.flr_work);
5314 if (con && adev->ras_enabled)
5315 cancel_work(&con->recovery_work);
5320 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
5322 * @adev: amdgpu_device pointer
5323 * @job: which job trigger hang
5324 * @reset_context: amdgpu reset context pointer
5326 * Attempt to reset the GPU if it has hung (all asics).
5327 * Attempt to do soft-reset or full-reset and reinitialize Asic
5328 * Returns 0 for success or an error on failure.
5331 int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
5332 struct amdgpu_job *job,
5333 struct amdgpu_reset_context *reset_context)
5335 struct list_head device_list, *device_list_handle = NULL;
5336 bool job_signaled = false;
5337 struct amdgpu_hive_info *hive = NULL;
5338 struct amdgpu_device *tmp_adev = NULL;
5340 bool need_emergency_restart = false;
5341 bool audio_suspended = false;
5342 bool gpu_reset_for_dev_remove = false;
5344 gpu_reset_for_dev_remove =
5345 test_bit(AMDGPU_RESET_FOR_DEVICE_REMOVE, &reset_context->flags) &&
5346 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
5349 * Special case: RAS triggered and full reset isn't supported
5351 need_emergency_restart = amdgpu_ras_need_emergency_restart(adev);
5354 * Flush RAM to disk so that after reboot
5355 * the user can read log and see why the system rebooted.
5357 if (need_emergency_restart && amdgpu_ras_get_context(adev)->reboot) {
5358 DRM_WARN("Emergency reboot.");
5361 emergency_restart();
5364 dev_info(adev->dev, "GPU %s begin!\n",
5365 need_emergency_restart ? "jobs stop":"reset");
5367 if (!amdgpu_sriov_vf(adev))
5368 hive = amdgpu_get_xgmi_hive(adev);
5370 mutex_lock(&hive->hive_lock);
5372 reset_context->job = job;
5373 reset_context->hive = hive;
5375 * Build list of devices to reset.
5376 * In case we are in XGMI hive mode, resort the device list
5377 * to put adev in the 1st position.
5379 INIT_LIST_HEAD(&device_list);
5380 if (!amdgpu_sriov_vf(adev) && (adev->gmc.xgmi.num_physical_nodes > 1)) {
5381 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
5382 list_add_tail(&tmp_adev->reset_list, &device_list);
5383 if (gpu_reset_for_dev_remove && adev->shutdown)
5384 tmp_adev->shutdown = true;
5386 if (!list_is_first(&adev->reset_list, &device_list))
5387 list_rotate_to_front(&adev->reset_list, &device_list);
5388 device_list_handle = &device_list;
5390 list_add_tail(&adev->reset_list, &device_list);
5391 device_list_handle = &device_list;
5394 /* We need to lock reset domain only once both for XGMI and single device */
5395 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
5397 amdgpu_device_lock_reset_domain(tmp_adev->reset_domain);
5399 /* block all schedulers and reset given job's ring */
5400 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5402 amdgpu_device_set_mp1_state(tmp_adev);
5405 * Try to put the audio codec into suspend state
5406 * before gpu reset started.
5408 * Due to the power domain of the graphics device
5409 * is shared with AZ power domain. Without this,
5410 * we may change the audio hardware from behind
5411 * the audio driver's back. That will trigger
5412 * some audio codec errors.
5414 if (!amdgpu_device_suspend_display_audio(tmp_adev))
5415 audio_suspended = true;
5417 amdgpu_ras_set_error_query_ready(tmp_adev, false);
5419 cancel_delayed_work_sync(&tmp_adev->delayed_init_work);
5421 if (!amdgpu_sriov_vf(tmp_adev))
5422 amdgpu_amdkfd_pre_reset(tmp_adev);
5425 * Mark these ASICs to be reseted as untracked first
5426 * And add them back after reset completed
5428 amdgpu_unregister_gpu_instance(tmp_adev);
5430 drm_fb_helper_set_suspend_unlocked(adev_to_drm(tmp_adev)->fb_helper, true);
5432 /* disable ras on ALL IPs */
5433 if (!need_emergency_restart &&
5434 amdgpu_device_ip_need_full_reset(tmp_adev))
5435 amdgpu_ras_suspend(tmp_adev);
5437 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5438 struct amdgpu_ring *ring = tmp_adev->rings[i];
5440 if (!ring || !ring->sched.thread)
5443 drm_sched_stop(&ring->sched, job ? &job->base : NULL);
5445 if (need_emergency_restart)
5446 amdgpu_job_stop_all_jobs_on_sched(&ring->sched);
5448 atomic_inc(&tmp_adev->gpu_reset_counter);
5451 if (need_emergency_restart)
5452 goto skip_sched_resume;
5455 * Must check guilty signal here since after this point all old
5456 * HW fences are force signaled.
5458 * job->base holds a reference to parent fence
5460 if (job && dma_fence_is_signaled(&job->hw_fence)) {
5461 job_signaled = true;
5462 dev_info(adev->dev, "Guilty job already signaled, skipping HW reset");
5466 retry: /* Rest of adevs pre asic reset from XGMI hive. */
5467 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5468 if (gpu_reset_for_dev_remove) {
5469 /* Workaroud for ASICs need to disable SMC first */
5470 amdgpu_device_smu_fini_early(tmp_adev);
5472 r = amdgpu_device_pre_asic_reset(tmp_adev, reset_context);
5473 /*TODO Should we stop ?*/
5475 dev_err(tmp_adev->dev, "GPU pre asic reset failed with err, %d for drm dev, %s ",
5476 r, adev_to_drm(tmp_adev)->unique);
5477 tmp_adev->asic_reset_res = r;
5481 * Drop all pending non scheduler resets. Scheduler resets
5482 * were already dropped during drm_sched_stop
5484 amdgpu_device_stop_pending_resets(tmp_adev);
5487 /* Actual ASIC resets if needed.*/
5488 /* Host driver will handle XGMI hive reset for SRIOV */
5489 if (amdgpu_sriov_vf(adev)) {
5490 r = amdgpu_device_reset_sriov(adev, job ? false : true);
5492 adev->asic_reset_res = r;
5494 /* Aldebaran and gfx_11_0_3 support ras in SRIOV, so need resume ras during reset */
5495 if (adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 2) ||
5496 adev->ip_versions[GC_HWIP][0] == IP_VERSION(11, 0, 3))
5497 amdgpu_ras_resume(adev);
5499 r = amdgpu_do_asic_reset(device_list_handle, reset_context);
5500 if (r && r == -EAGAIN)
5503 if (!r && gpu_reset_for_dev_remove)
5509 /* Post ASIC reset for all devs .*/
5510 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5512 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5513 struct amdgpu_ring *ring = tmp_adev->rings[i];
5515 if (!ring || !ring->sched.thread)
5518 drm_sched_start(&ring->sched, true);
5521 if (adev->enable_mes && adev->ip_versions[GC_HWIP][0] != IP_VERSION(11, 0, 3))
5522 amdgpu_mes_self_test(tmp_adev);
5524 if (!drm_drv_uses_atomic_modeset(adev_to_drm(tmp_adev)) && !job_signaled) {
5525 drm_helper_resume_force_mode(adev_to_drm(tmp_adev));
5528 if (tmp_adev->asic_reset_res)
5529 r = tmp_adev->asic_reset_res;
5531 tmp_adev->asic_reset_res = 0;
5534 /* bad news, how to tell it to userspace ? */
5535 dev_info(tmp_adev->dev, "GPU reset(%d) failed\n", atomic_read(&tmp_adev->gpu_reset_counter));
5536 amdgpu_vf_error_put(tmp_adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
5538 dev_info(tmp_adev->dev, "GPU reset(%d) succeeded!\n", atomic_read(&tmp_adev->gpu_reset_counter));
5539 if (amdgpu_acpi_smart_shift_update(adev_to_drm(tmp_adev), AMDGPU_SS_DEV_D0))
5540 DRM_WARN("smart shift update failed\n");
5545 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5546 /* unlock kfd: SRIOV would do it separately */
5547 if (!need_emergency_restart && !amdgpu_sriov_vf(tmp_adev))
5548 amdgpu_amdkfd_post_reset(tmp_adev);
5550 /* kfd_post_reset will do nothing if kfd device is not initialized,
5551 * need to bring up kfd here if it's not be initialized before
5553 if (!adev->kfd.init_complete)
5554 amdgpu_amdkfd_device_init(adev);
5556 if (audio_suspended)
5557 amdgpu_device_resume_display_audio(tmp_adev);
5559 amdgpu_device_unset_mp1_state(tmp_adev);
5561 amdgpu_ras_set_error_query_ready(tmp_adev, true);
5565 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
5567 amdgpu_device_unlock_reset_domain(tmp_adev->reset_domain);
5570 mutex_unlock(&hive->hive_lock);
5571 amdgpu_put_xgmi_hive(hive);
5575 dev_info(adev->dev, "GPU reset end with ret = %d\n", r);
5577 atomic_set(&adev->reset_domain->reset_res, r);
5582 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
5584 * @adev: amdgpu_device pointer
5586 * Fetchs and stores in the driver the PCIE capabilities (gen speed
5587 * and lanes) of the slot the device is in. Handles APUs and
5588 * virtualized environments where PCIE config space may not be available.
5590 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
5592 struct pci_dev *pdev;
5593 enum pci_bus_speed speed_cap, platform_speed_cap;
5594 enum pcie_link_width platform_link_width;
5596 if (amdgpu_pcie_gen_cap)
5597 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
5599 if (amdgpu_pcie_lane_cap)
5600 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
5602 /* covers APUs as well */
5603 if (pci_is_root_bus(adev->pdev->bus) && !amdgpu_passthrough(adev)) {
5604 if (adev->pm.pcie_gen_mask == 0)
5605 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
5606 if (adev->pm.pcie_mlw_mask == 0)
5607 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
5611 if (adev->pm.pcie_gen_mask && adev->pm.pcie_mlw_mask)
5614 pcie_bandwidth_available(adev->pdev, NULL,
5615 &platform_speed_cap, &platform_link_width);
5617 if (adev->pm.pcie_gen_mask == 0) {
5620 speed_cap = pcie_get_speed_cap(pdev);
5621 if (speed_cap == PCI_SPEED_UNKNOWN) {
5622 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5623 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5624 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
5626 if (speed_cap == PCIE_SPEED_32_0GT)
5627 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5628 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5629 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5630 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4 |
5631 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN5);
5632 else if (speed_cap == PCIE_SPEED_16_0GT)
5633 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5634 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5635 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5636 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4);
5637 else if (speed_cap == PCIE_SPEED_8_0GT)
5638 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5639 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5640 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
5641 else if (speed_cap == PCIE_SPEED_5_0GT)
5642 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5643 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2);
5645 adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1;
5648 if (platform_speed_cap == PCI_SPEED_UNKNOWN) {
5649 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5650 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
5652 if (platform_speed_cap == PCIE_SPEED_32_0GT)
5653 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5654 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5655 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5656 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4 |
5657 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN5);
5658 else if (platform_speed_cap == PCIE_SPEED_16_0GT)
5659 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5660 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5661 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5662 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4);
5663 else if (platform_speed_cap == PCIE_SPEED_8_0GT)
5664 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5665 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5666 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3);
5667 else if (platform_speed_cap == PCIE_SPEED_5_0GT)
5668 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5669 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
5671 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
5675 if (adev->pm.pcie_mlw_mask == 0) {
5676 if (platform_link_width == PCIE_LNK_WIDTH_UNKNOWN) {
5677 adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK;
5679 switch (platform_link_width) {
5681 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
5682 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5683 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5684 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5685 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5686 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5687 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5690 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5691 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5692 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5693 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5694 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5695 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5698 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5699 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5700 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5701 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5702 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5705 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5706 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5707 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5708 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5711 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5712 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5713 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5716 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5717 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5720 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
5730 * amdgpu_device_is_peer_accessible - Check peer access through PCIe BAR
5732 * @adev: amdgpu_device pointer
5733 * @peer_adev: amdgpu_device pointer for peer device trying to access @adev
5735 * Return true if @peer_adev can access (DMA) @adev through the PCIe
5736 * BAR, i.e. @adev is "large BAR" and the BAR matches the DMA mask of
5739 bool amdgpu_device_is_peer_accessible(struct amdgpu_device *adev,
5740 struct amdgpu_device *peer_adev)
5742 #ifdef CONFIG_HSA_AMD_P2P
5743 uint64_t address_mask = peer_adev->dev->dma_mask ?
5744 ~*peer_adev->dev->dma_mask : ~((1ULL << 32) - 1);
5745 resource_size_t aper_limit =
5746 adev->gmc.aper_base + adev->gmc.aper_size - 1;
5748 !adev->gmc.xgmi.connected_to_cpu &&
5749 !(pci_p2pdma_distance(adev->pdev, peer_adev->dev, false) < 0);
5751 return pcie_p2p && p2p_access && (adev->gmc.visible_vram_size &&
5752 adev->gmc.real_vram_size == adev->gmc.visible_vram_size &&
5753 !(adev->gmc.aper_base & address_mask ||
5754 aper_limit & address_mask));
5760 int amdgpu_device_baco_enter(struct drm_device *dev)
5762 struct amdgpu_device *adev = drm_to_adev(dev);
5763 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
5765 if (!amdgpu_device_supports_baco(dev))
5768 if (ras && adev->ras_enabled &&
5769 adev->nbio.funcs->enable_doorbell_interrupt)
5770 adev->nbio.funcs->enable_doorbell_interrupt(adev, false);
5772 return amdgpu_dpm_baco_enter(adev);
5775 int amdgpu_device_baco_exit(struct drm_device *dev)
5777 struct amdgpu_device *adev = drm_to_adev(dev);
5778 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
5781 if (!amdgpu_device_supports_baco(dev))
5784 ret = amdgpu_dpm_baco_exit(adev);
5788 if (ras && adev->ras_enabled &&
5789 adev->nbio.funcs->enable_doorbell_interrupt)
5790 adev->nbio.funcs->enable_doorbell_interrupt(adev, true);
5792 if (amdgpu_passthrough(adev) &&
5793 adev->nbio.funcs->clear_doorbell_interrupt)
5794 adev->nbio.funcs->clear_doorbell_interrupt(adev);
5800 * amdgpu_pci_error_detected - Called when a PCI error is detected.
5801 * @pdev: PCI device struct
5802 * @state: PCI channel state
5804 * Description: Called when a PCI error is detected.
5806 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT.
5808 pci_ers_result_t amdgpu_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
5810 struct drm_device *dev = pci_get_drvdata(pdev);
5811 struct amdgpu_device *adev = drm_to_adev(dev);
5814 DRM_INFO("PCI error: detected callback, state(%d)!!\n", state);
5816 if (adev->gmc.xgmi.num_physical_nodes > 1) {
5817 DRM_WARN("No support for XGMI hive yet...");
5818 return PCI_ERS_RESULT_DISCONNECT;
5821 adev->pci_channel_state = state;
5824 case pci_channel_io_normal:
5825 return PCI_ERS_RESULT_CAN_RECOVER;
5826 /* Fatal error, prepare for slot reset */
5827 case pci_channel_io_frozen:
5829 * Locking adev->reset_domain->sem will prevent any external access
5830 * to GPU during PCI error recovery
5832 amdgpu_device_lock_reset_domain(adev->reset_domain);
5833 amdgpu_device_set_mp1_state(adev);
5836 * Block any work scheduling as we do for regular GPU reset
5837 * for the duration of the recovery
5839 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5840 struct amdgpu_ring *ring = adev->rings[i];
5842 if (!ring || !ring->sched.thread)
5845 drm_sched_stop(&ring->sched, NULL);
5847 atomic_inc(&adev->gpu_reset_counter);
5848 return PCI_ERS_RESULT_NEED_RESET;
5849 case pci_channel_io_perm_failure:
5850 /* Permanent error, prepare for device removal */
5851 return PCI_ERS_RESULT_DISCONNECT;
5854 return PCI_ERS_RESULT_NEED_RESET;
5858 * amdgpu_pci_mmio_enabled - Enable MMIO and dump debug registers
5859 * @pdev: pointer to PCI device
5861 pci_ers_result_t amdgpu_pci_mmio_enabled(struct pci_dev *pdev)
5864 DRM_INFO("PCI error: mmio enabled callback!!\n");
5866 /* TODO - dump whatever for debugging purposes */
5868 /* This called only if amdgpu_pci_error_detected returns
5869 * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still
5870 * works, no need to reset slot.
5873 return PCI_ERS_RESULT_RECOVERED;
5877 * amdgpu_pci_slot_reset - Called when PCI slot has been reset.
5878 * @pdev: PCI device struct
5880 * Description: This routine is called by the pci error recovery
5881 * code after the PCI slot has been reset, just before we
5882 * should resume normal operations.
5884 pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev)
5886 struct drm_device *dev = pci_get_drvdata(pdev);
5887 struct amdgpu_device *adev = drm_to_adev(dev);
5889 struct amdgpu_reset_context reset_context;
5891 struct list_head device_list;
5893 DRM_INFO("PCI error: slot reset callback!!\n");
5895 memset(&reset_context, 0, sizeof(reset_context));
5897 INIT_LIST_HEAD(&device_list);
5898 list_add_tail(&adev->reset_list, &device_list);
5900 /* wait for asic to come out of reset */
5903 /* Restore PCI confspace */
5904 amdgpu_device_load_pci_state(pdev);
5906 /* confirm ASIC came out of reset */
5907 for (i = 0; i < adev->usec_timeout; i++) {
5908 memsize = amdgpu_asic_get_config_memsize(adev);
5910 if (memsize != 0xffffffff)
5914 if (memsize == 0xffffffff) {
5919 reset_context.method = AMD_RESET_METHOD_NONE;
5920 reset_context.reset_req_dev = adev;
5921 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
5922 set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
5924 adev->no_hw_access = true;
5925 r = amdgpu_device_pre_asic_reset(adev, &reset_context);
5926 adev->no_hw_access = false;
5930 r = amdgpu_do_asic_reset(&device_list, &reset_context);
5934 if (amdgpu_device_cache_pci_state(adev->pdev))
5935 pci_restore_state(adev->pdev);
5937 DRM_INFO("PCIe error recovery succeeded\n");
5939 DRM_ERROR("PCIe error recovery failed, err:%d", r);
5940 amdgpu_device_unset_mp1_state(adev);
5941 amdgpu_device_unlock_reset_domain(adev->reset_domain);
5944 return r ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
5948 * amdgpu_pci_resume() - resume normal ops after PCI reset
5949 * @pdev: pointer to PCI device
5951 * Called when the error recovery driver tells us that its
5952 * OK to resume normal operation.
5954 void amdgpu_pci_resume(struct pci_dev *pdev)
5956 struct drm_device *dev = pci_get_drvdata(pdev);
5957 struct amdgpu_device *adev = drm_to_adev(dev);
5961 DRM_INFO("PCI error: resume callback!!\n");
5963 /* Only continue execution for the case of pci_channel_io_frozen */
5964 if (adev->pci_channel_state != pci_channel_io_frozen)
5967 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5968 struct amdgpu_ring *ring = adev->rings[i];
5970 if (!ring || !ring->sched.thread)
5973 drm_sched_start(&ring->sched, true);
5976 amdgpu_device_unset_mp1_state(adev);
5977 amdgpu_device_unlock_reset_domain(adev->reset_domain);
5980 bool amdgpu_device_cache_pci_state(struct pci_dev *pdev)
5982 struct drm_device *dev = pci_get_drvdata(pdev);
5983 struct amdgpu_device *adev = drm_to_adev(dev);
5986 r = pci_save_state(pdev);
5988 kfree(adev->pci_state);
5990 adev->pci_state = pci_store_saved_state(pdev);
5992 if (!adev->pci_state) {
5993 DRM_ERROR("Failed to store PCI saved state");
5997 DRM_WARN("Failed to save PCI state, err:%d\n", r);
6004 bool amdgpu_device_load_pci_state(struct pci_dev *pdev)
6006 struct drm_device *dev = pci_get_drvdata(pdev);
6007 struct amdgpu_device *adev = drm_to_adev(dev);
6010 if (!adev->pci_state)
6013 r = pci_load_saved_state(pdev, adev->pci_state);
6016 pci_restore_state(pdev);
6018 DRM_WARN("Failed to load PCI state, err:%d\n", r);
6025 void amdgpu_device_flush_hdp(struct amdgpu_device *adev,
6026 struct amdgpu_ring *ring)
6028 #ifdef CONFIG_X86_64
6029 if ((adev->flags & AMD_IS_APU) && !amdgpu_passthrough(adev))
6032 if (adev->gmc.xgmi.connected_to_cpu)
6035 if (ring && ring->funcs->emit_hdp_flush)
6036 amdgpu_ring_emit_hdp_flush(ring);
6038 amdgpu_asic_flush_hdp(adev, ring);
6041 void amdgpu_device_invalidate_hdp(struct amdgpu_device *adev,
6042 struct amdgpu_ring *ring)
6044 #ifdef CONFIG_X86_64
6045 if ((adev->flags & AMD_IS_APU) && !amdgpu_passthrough(adev))
6048 if (adev->gmc.xgmi.connected_to_cpu)
6051 amdgpu_asic_invalidate_hdp(adev, ring);
6054 int amdgpu_in_reset(struct amdgpu_device *adev)
6056 return atomic_read(&adev->reset_domain->in_gpu_reset);
6060 * amdgpu_device_halt() - bring hardware to some kind of halt state
6062 * @adev: amdgpu_device pointer
6064 * Bring hardware to some kind of halt state so that no one can touch it
6065 * any more. It will help to maintain error context when error occurred.
6066 * Compare to a simple hang, the system will keep stable at least for SSH
6067 * access. Then it should be trivial to inspect the hardware state and
6068 * see what's going on. Implemented as following:
6070 * 1. drm_dev_unplug() makes device inaccessible to user space(IOCTLs, etc),
6071 * clears all CPU mappings to device, disallows remappings through page faults
6072 * 2. amdgpu_irq_disable_all() disables all interrupts
6073 * 3. amdgpu_fence_driver_hw_fini() signals all HW fences
6074 * 4. set adev->no_hw_access to avoid potential crashes after setp 5
6075 * 5. amdgpu_device_unmap_mmio() clears all MMIO mappings
6076 * 6. pci_disable_device() and pci_wait_for_pending_transaction()
6077 * flush any in flight DMA operations
6079 void amdgpu_device_halt(struct amdgpu_device *adev)
6081 struct pci_dev *pdev = adev->pdev;
6082 struct drm_device *ddev = adev_to_drm(adev);
6084 amdgpu_xcp_dev_unplug(adev);
6085 drm_dev_unplug(ddev);
6087 amdgpu_irq_disable_all(adev);
6089 amdgpu_fence_driver_hw_fini(adev);
6091 adev->no_hw_access = true;
6093 amdgpu_device_unmap_mmio(adev);
6095 pci_disable_device(pdev);
6096 pci_wait_for_pending_transaction(pdev);
6099 u32 amdgpu_device_pcie_port_rreg(struct amdgpu_device *adev,
6102 unsigned long flags, address, data;
6105 address = adev->nbio.funcs->get_pcie_port_index_offset(adev);
6106 data = adev->nbio.funcs->get_pcie_port_data_offset(adev);
6108 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
6109 WREG32(address, reg * 4);
6110 (void)RREG32(address);
6112 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
6116 void amdgpu_device_pcie_port_wreg(struct amdgpu_device *adev,
6119 unsigned long flags, address, data;
6121 address = adev->nbio.funcs->get_pcie_port_index_offset(adev);
6122 data = adev->nbio.funcs->get_pcie_port_data_offset(adev);
6124 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
6125 WREG32(address, reg * 4);
6126 (void)RREG32(address);
6129 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
6133 * amdgpu_device_switch_gang - switch to a new gang
6134 * @adev: amdgpu_device pointer
6135 * @gang: the gang to switch to
6137 * Try to switch to a new gang.
6138 * Returns: NULL if we switched to the new gang or a reference to the current
6141 struct dma_fence *amdgpu_device_switch_gang(struct amdgpu_device *adev,
6142 struct dma_fence *gang)
6144 struct dma_fence *old = NULL;
6149 old = dma_fence_get_rcu_safe(&adev->gang_submit);
6155 if (!dma_fence_is_signaled(old))
6158 } while (cmpxchg((struct dma_fence __force **)&adev->gang_submit,
6165 bool amdgpu_device_has_display_hardware(struct amdgpu_device *adev)
6167 switch (adev->asic_type) {
6168 #ifdef CONFIG_DRM_AMDGPU_SI
6172 /* chips with no display hardware */
6174 #ifdef CONFIG_DRM_AMDGPU_SI
6180 #ifdef CONFIG_DRM_AMDGPU_CIK
6189 case CHIP_POLARIS10:
6190 case CHIP_POLARIS11:
6191 case CHIP_POLARIS12:
6195 /* chips with display hardware */
6199 if (!adev->ip_versions[DCE_HWIP][0] ||
6200 (adev->harvest_ip_mask & AMD_HARVEST_IP_DMU_MASK))
6206 uint32_t amdgpu_device_wait_on_rreg(struct amdgpu_device *adev,
6207 uint32_t inst, uint32_t reg_addr, char reg_name[],
6208 uint32_t expected_value, uint32_t mask)
6212 uint32_t tmp_ = RREG32(reg_addr);
6213 uint32_t loop = adev->usec_timeout;
6215 while ((tmp_ & (mask)) != (expected_value)) {
6217 loop = adev->usec_timeout;
6221 tmp_ = RREG32(reg_addr);
6224 DRM_WARN("Register(%d) [%s] failed to reach value 0x%08x != 0x%08xn",
6225 inst, reg_name, (uint32_t)expected_value,
6226 (uint32_t)(tmp_ & (mask)));