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>
39 #include <drm/drm_aperture.h>
40 #include <drm/drm_atomic_helper.h>
41 #include <drm/drm_probe_helper.h>
42 #include <drm/amdgpu_drm.h>
43 #include <linux/vgaarb.h>
44 #include <linux/vga_switcheroo.h>
45 #include <linux/efi.h>
47 #include "amdgpu_trace.h"
48 #include "amdgpu_i2c.h"
50 #include "amdgpu_atombios.h"
51 #include "amdgpu_atomfirmware.h"
53 #ifdef CONFIG_DRM_AMDGPU_SI
56 #ifdef CONFIG_DRM_AMDGPU_CIK
62 #include "bif/bif_4_1_d.h"
63 #include <linux/firmware.h>
64 #include "amdgpu_vf_error.h"
66 #include "amdgpu_amdkfd.h"
67 #include "amdgpu_pm.h"
69 #include "amdgpu_xgmi.h"
70 #include "amdgpu_ras.h"
71 #include "amdgpu_pmu.h"
72 #include "amdgpu_fru_eeprom.h"
73 #include "amdgpu_reset.h"
75 #include <linux/suspend.h>
76 #include <drm/task_barrier.h>
77 #include <linux/pm_runtime.h>
79 #include <drm/drm_drv.h>
81 MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
82 MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
83 MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
84 MODULE_FIRMWARE("amdgpu/picasso_gpu_info.bin");
85 MODULE_FIRMWARE("amdgpu/raven2_gpu_info.bin");
86 MODULE_FIRMWARE("amdgpu/arcturus_gpu_info.bin");
87 MODULE_FIRMWARE("amdgpu/navi12_gpu_info.bin");
89 #define AMDGPU_RESUME_MS 2000
90 #define AMDGPU_MAX_RETRY_LIMIT 2
91 #define AMDGPU_RETRY_SRIOV_RESET(r) ((r) == -EBUSY || (r) == -ETIMEDOUT || (r) == -EINVAL)
93 static const struct drm_driver amdgpu_kms_driver;
95 const char *amdgpu_asic_name[] = {
137 * DOC: pcie_replay_count
139 * The amdgpu driver provides a sysfs API for reporting the total number
140 * of PCIe replays (NAKs)
141 * The file pcie_replay_count is used for this and returns the total
142 * number of replays as a sum of the NAKs generated and NAKs received
145 static ssize_t amdgpu_device_get_pcie_replay_count(struct device *dev,
146 struct device_attribute *attr, char *buf)
148 struct drm_device *ddev = dev_get_drvdata(dev);
149 struct amdgpu_device *adev = drm_to_adev(ddev);
150 uint64_t cnt = amdgpu_asic_get_pcie_replay_count(adev);
152 return sysfs_emit(buf, "%llu\n", cnt);
155 static DEVICE_ATTR(pcie_replay_count, S_IRUGO,
156 amdgpu_device_get_pcie_replay_count, NULL);
158 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev);
163 * The amdgpu driver provides a sysfs API for reporting the product name
165 * The file serial_number is used for this and returns the product name
166 * as returned from the FRU.
167 * NOTE: This is only available for certain server cards
170 static ssize_t amdgpu_device_get_product_name(struct device *dev,
171 struct device_attribute *attr, char *buf)
173 struct drm_device *ddev = dev_get_drvdata(dev);
174 struct amdgpu_device *adev = drm_to_adev(ddev);
176 return sysfs_emit(buf, "%s\n", adev->product_name);
179 static DEVICE_ATTR(product_name, S_IRUGO,
180 amdgpu_device_get_product_name, NULL);
183 * DOC: product_number
185 * The amdgpu driver provides a sysfs API for reporting the part number
187 * The file serial_number is used for this and returns the part number
188 * as returned from the FRU.
189 * NOTE: This is only available for certain server cards
192 static ssize_t amdgpu_device_get_product_number(struct device *dev,
193 struct device_attribute *attr, char *buf)
195 struct drm_device *ddev = dev_get_drvdata(dev);
196 struct amdgpu_device *adev = drm_to_adev(ddev);
198 return sysfs_emit(buf, "%s\n", adev->product_number);
201 static DEVICE_ATTR(product_number, S_IRUGO,
202 amdgpu_device_get_product_number, NULL);
207 * The amdgpu driver provides a sysfs API for reporting the serial number
209 * The file serial_number is used for this and returns the serial number
210 * as returned from the FRU.
211 * NOTE: This is only available for certain server cards
214 static ssize_t amdgpu_device_get_serial_number(struct device *dev,
215 struct device_attribute *attr, char *buf)
217 struct drm_device *ddev = dev_get_drvdata(dev);
218 struct amdgpu_device *adev = drm_to_adev(ddev);
220 return sysfs_emit(buf, "%s\n", adev->serial);
223 static DEVICE_ATTR(serial_number, S_IRUGO,
224 amdgpu_device_get_serial_number, NULL);
227 * amdgpu_device_supports_px - Is the device a dGPU with ATPX power control
229 * @dev: drm_device pointer
231 * Returns true if the device is a dGPU with ATPX power control,
232 * otherwise return false.
234 bool amdgpu_device_supports_px(struct drm_device *dev)
236 struct amdgpu_device *adev = drm_to_adev(dev);
238 if ((adev->flags & AMD_IS_PX) && !amdgpu_is_atpx_hybrid())
244 * amdgpu_device_supports_boco - Is the device a dGPU with ACPI power resources
246 * @dev: drm_device pointer
248 * Returns true if the device is a dGPU with ACPI power control,
249 * otherwise return false.
251 bool amdgpu_device_supports_boco(struct drm_device *dev)
253 struct amdgpu_device *adev = drm_to_adev(dev);
256 ((adev->flags & AMD_IS_PX) && amdgpu_is_atpx_hybrid()))
262 * amdgpu_device_supports_baco - Does the device support BACO
264 * @dev: drm_device pointer
266 * Returns true if the device supporte BACO,
267 * otherwise return false.
269 bool amdgpu_device_supports_baco(struct drm_device *dev)
271 struct amdgpu_device *adev = drm_to_adev(dev);
273 return amdgpu_asic_supports_baco(adev);
277 * amdgpu_device_supports_smart_shift - Is the device dGPU with
278 * smart shift support
280 * @dev: drm_device pointer
282 * Returns true if the device is a dGPU with Smart Shift support,
283 * otherwise returns false.
285 bool amdgpu_device_supports_smart_shift(struct drm_device *dev)
287 return (amdgpu_device_supports_boco(dev) &&
288 amdgpu_acpi_is_power_shift_control_supported());
292 * VRAM access helper functions
296 * amdgpu_device_mm_access - access vram by MM_INDEX/MM_DATA
298 * @adev: amdgpu_device pointer
299 * @pos: offset of the buffer in vram
300 * @buf: virtual address of the buffer in system memory
301 * @size: read/write size, sizeof(@buf) must > @size
302 * @write: true - write to vram, otherwise - read from vram
304 void amdgpu_device_mm_access(struct amdgpu_device *adev, loff_t pos,
305 void *buf, size_t size, bool write)
308 uint32_t hi = ~0, tmp = 0;
309 uint32_t *data = buf;
313 if (!drm_dev_enter(adev_to_drm(adev), &idx))
316 BUG_ON(!IS_ALIGNED(pos, 4) || !IS_ALIGNED(size, 4));
318 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
319 for (last = pos + size; pos < last; pos += 4) {
322 WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)pos) | 0x80000000);
324 WREG32_NO_KIQ(mmMM_INDEX_HI, tmp);
328 WREG32_NO_KIQ(mmMM_DATA, *data++);
330 *data++ = RREG32_NO_KIQ(mmMM_DATA);
333 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
338 * amdgpu_device_aper_access - access vram by vram aperature
340 * @adev: amdgpu_device pointer
341 * @pos: offset of the buffer in vram
342 * @buf: virtual address of the buffer in system memory
343 * @size: read/write size, sizeof(@buf) must > @size
344 * @write: true - write to vram, otherwise - read from vram
346 * The return value means how many bytes have been transferred.
348 size_t amdgpu_device_aper_access(struct amdgpu_device *adev, loff_t pos,
349 void *buf, size_t size, bool write)
356 if (!adev->mman.aper_base_kaddr)
359 last = min(pos + size, adev->gmc.visible_vram_size);
361 addr = adev->mman.aper_base_kaddr + pos;
365 memcpy_toio(addr, buf, count);
367 amdgpu_device_flush_hdp(adev, NULL);
369 amdgpu_device_invalidate_hdp(adev, NULL);
371 memcpy_fromio(buf, addr, count);
383 * amdgpu_device_vram_access - read/write a buffer in vram
385 * @adev: amdgpu_device pointer
386 * @pos: offset of the buffer in vram
387 * @buf: virtual address of the buffer in system memory
388 * @size: read/write size, sizeof(@buf) must > @size
389 * @write: true - write to vram, otherwise - read from vram
391 void amdgpu_device_vram_access(struct amdgpu_device *adev, loff_t pos,
392 void *buf, size_t size, bool write)
396 /* try to using vram apreature to access vram first */
397 count = amdgpu_device_aper_access(adev, pos, buf, size, write);
400 /* using MM to access rest vram */
403 amdgpu_device_mm_access(adev, pos, buf, size, write);
408 * register access helper functions.
411 /* Check if hw access should be skipped because of hotplug or device error */
412 bool amdgpu_device_skip_hw_access(struct amdgpu_device *adev)
414 if (adev->no_hw_access)
417 #ifdef CONFIG_LOCKDEP
419 * This is a bit complicated to understand, so worth a comment. What we assert
420 * here is that the GPU reset is not running on another thread in parallel.
422 * For this we trylock the read side of the reset semaphore, if that succeeds
423 * we know that the reset is not running in paralell.
425 * If the trylock fails we assert that we are either already holding the read
426 * side of the lock or are the reset thread itself and hold the write side of
430 if (down_read_trylock(&adev->reset_domain->sem))
431 up_read(&adev->reset_domain->sem);
433 lockdep_assert_held(&adev->reset_domain->sem);
440 * amdgpu_device_rreg - read a memory mapped IO or indirect register
442 * @adev: amdgpu_device pointer
443 * @reg: dword aligned register offset
444 * @acc_flags: access flags which require special behavior
446 * Returns the 32 bit value from the offset specified.
448 uint32_t amdgpu_device_rreg(struct amdgpu_device *adev,
449 uint32_t reg, uint32_t acc_flags)
453 if (amdgpu_device_skip_hw_access(adev))
456 if ((reg * 4) < adev->rmmio_size) {
457 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
458 amdgpu_sriov_runtime(adev) &&
459 down_read_trylock(&adev->reset_domain->sem)) {
460 ret = amdgpu_kiq_rreg(adev, reg);
461 up_read(&adev->reset_domain->sem);
463 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
466 ret = adev->pcie_rreg(adev, reg * 4);
469 trace_amdgpu_device_rreg(adev->pdev->device, reg, ret);
475 * MMIO register read with bytes helper functions
476 * @offset:bytes offset from MMIO start
481 * amdgpu_mm_rreg8 - read a memory mapped IO register
483 * @adev: amdgpu_device pointer
484 * @offset: byte aligned register offset
486 * Returns the 8 bit value from the offset specified.
488 uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset)
490 if (amdgpu_device_skip_hw_access(adev))
493 if (offset < adev->rmmio_size)
494 return (readb(adev->rmmio + offset));
499 * MMIO register write with bytes helper functions
500 * @offset:bytes offset from MMIO start
501 * @value: the value want to be written to the register
505 * amdgpu_mm_wreg8 - read a memory mapped IO register
507 * @adev: amdgpu_device pointer
508 * @offset: byte aligned register offset
509 * @value: 8 bit value to write
511 * Writes the value specified to the offset specified.
513 void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value)
515 if (amdgpu_device_skip_hw_access(adev))
518 if (offset < adev->rmmio_size)
519 writeb(value, adev->rmmio + offset);
525 * amdgpu_device_wreg - write to a memory mapped IO or indirect register
527 * @adev: amdgpu_device pointer
528 * @reg: dword aligned register offset
529 * @v: 32 bit value to write to the register
530 * @acc_flags: access flags which require special behavior
532 * Writes the value specified to the offset specified.
534 void amdgpu_device_wreg(struct amdgpu_device *adev,
535 uint32_t reg, uint32_t v,
538 if (amdgpu_device_skip_hw_access(adev))
541 if ((reg * 4) < adev->rmmio_size) {
542 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
543 amdgpu_sriov_runtime(adev) &&
544 down_read_trylock(&adev->reset_domain->sem)) {
545 amdgpu_kiq_wreg(adev, reg, v);
546 up_read(&adev->reset_domain->sem);
548 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
551 adev->pcie_wreg(adev, reg * 4, v);
554 trace_amdgpu_device_wreg(adev->pdev->device, reg, v);
558 * amdgpu_mm_wreg_mmio_rlc - write register either with direct/indirect mmio or with RLC path if in range
560 * @adev: amdgpu_device pointer
561 * @reg: mmio/rlc register
564 * this function is invoked only for the debugfs register access
566 void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
567 uint32_t reg, uint32_t v)
569 if (amdgpu_device_skip_hw_access(adev))
572 if (amdgpu_sriov_fullaccess(adev) &&
573 adev->gfx.rlc.funcs &&
574 adev->gfx.rlc.funcs->is_rlcg_access_range) {
575 if (adev->gfx.rlc.funcs->is_rlcg_access_range(adev, reg))
576 return amdgpu_sriov_wreg(adev, reg, v, 0, 0);
577 } else if ((reg * 4) >= adev->rmmio_size) {
578 adev->pcie_wreg(adev, reg * 4, v);
580 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
585 * amdgpu_mm_rdoorbell - read a doorbell dword
587 * @adev: amdgpu_device pointer
588 * @index: doorbell index
590 * Returns the value in the doorbell aperture at the
591 * requested doorbell index (CIK).
593 u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
595 if (amdgpu_device_skip_hw_access(adev))
598 if (index < adev->doorbell.num_doorbells) {
599 return readl(adev->doorbell.ptr + index);
601 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
607 * amdgpu_mm_wdoorbell - write a doorbell dword
609 * @adev: amdgpu_device pointer
610 * @index: doorbell index
613 * Writes @v to the doorbell aperture at the
614 * requested doorbell index (CIK).
616 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
618 if (amdgpu_device_skip_hw_access(adev))
621 if (index < adev->doorbell.num_doorbells) {
622 writel(v, adev->doorbell.ptr + index);
624 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
629 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
631 * @adev: amdgpu_device pointer
632 * @index: doorbell index
634 * Returns the value in the doorbell aperture at the
635 * requested doorbell index (VEGA10+).
637 u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
639 if (amdgpu_device_skip_hw_access(adev))
642 if (index < adev->doorbell.num_doorbells) {
643 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
645 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
651 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
653 * @adev: amdgpu_device pointer
654 * @index: doorbell index
657 * Writes @v to the doorbell aperture at the
658 * requested doorbell index (VEGA10+).
660 void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
662 if (amdgpu_device_skip_hw_access(adev))
665 if (index < adev->doorbell.num_doorbells) {
666 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
668 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
673 * amdgpu_device_indirect_rreg - read an indirect register
675 * @adev: amdgpu_device pointer
676 * @pcie_index: mmio register offset
677 * @pcie_data: mmio register offset
678 * @reg_addr: indirect register address to read from
680 * Returns the value of indirect register @reg_addr
682 u32 amdgpu_device_indirect_rreg(struct amdgpu_device *adev,
683 u32 pcie_index, u32 pcie_data,
688 void __iomem *pcie_index_offset;
689 void __iomem *pcie_data_offset;
691 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
692 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
693 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
695 writel(reg_addr, pcie_index_offset);
696 readl(pcie_index_offset);
697 r = readl(pcie_data_offset);
698 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
704 * amdgpu_device_indirect_rreg64 - read a 64bits indirect register
706 * @adev: amdgpu_device pointer
707 * @pcie_index: mmio register offset
708 * @pcie_data: mmio register offset
709 * @reg_addr: indirect register address to read from
711 * Returns the value of indirect register @reg_addr
713 u64 amdgpu_device_indirect_rreg64(struct amdgpu_device *adev,
714 u32 pcie_index, u32 pcie_data,
719 void __iomem *pcie_index_offset;
720 void __iomem *pcie_data_offset;
722 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
723 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
724 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
726 /* read low 32 bits */
727 writel(reg_addr, pcie_index_offset);
728 readl(pcie_index_offset);
729 r = readl(pcie_data_offset);
730 /* read high 32 bits */
731 writel(reg_addr + 4, pcie_index_offset);
732 readl(pcie_index_offset);
733 r |= ((u64)readl(pcie_data_offset) << 32);
734 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
740 * amdgpu_device_indirect_wreg - write an indirect register address
742 * @adev: amdgpu_device pointer
743 * @pcie_index: mmio register offset
744 * @pcie_data: mmio register offset
745 * @reg_addr: indirect register offset
746 * @reg_data: indirect register data
749 void amdgpu_device_indirect_wreg(struct amdgpu_device *adev,
750 u32 pcie_index, u32 pcie_data,
751 u32 reg_addr, u32 reg_data)
754 void __iomem *pcie_index_offset;
755 void __iomem *pcie_data_offset;
757 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
758 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
759 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
761 writel(reg_addr, pcie_index_offset);
762 readl(pcie_index_offset);
763 writel(reg_data, pcie_data_offset);
764 readl(pcie_data_offset);
765 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
769 * amdgpu_device_indirect_wreg64 - write a 64bits indirect register address
771 * @adev: amdgpu_device pointer
772 * @pcie_index: mmio register offset
773 * @pcie_data: mmio register offset
774 * @reg_addr: indirect register offset
775 * @reg_data: indirect register data
778 void amdgpu_device_indirect_wreg64(struct amdgpu_device *adev,
779 u32 pcie_index, u32 pcie_data,
780 u32 reg_addr, u64 reg_data)
783 void __iomem *pcie_index_offset;
784 void __iomem *pcie_data_offset;
786 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
787 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
788 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
790 /* write low 32 bits */
791 writel(reg_addr, pcie_index_offset);
792 readl(pcie_index_offset);
793 writel((u32)(reg_data & 0xffffffffULL), pcie_data_offset);
794 readl(pcie_data_offset);
795 /* write high 32 bits */
796 writel(reg_addr + 4, pcie_index_offset);
797 readl(pcie_index_offset);
798 writel((u32)(reg_data >> 32), pcie_data_offset);
799 readl(pcie_data_offset);
800 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
804 * amdgpu_invalid_rreg - dummy reg read function
806 * @adev: amdgpu_device pointer
807 * @reg: offset of register
809 * Dummy register read function. Used for register blocks
810 * that certain asics don't have (all asics).
811 * Returns the value in the register.
813 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
815 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
821 * amdgpu_invalid_wreg - dummy reg write function
823 * @adev: amdgpu_device pointer
824 * @reg: offset of register
825 * @v: value to write to the register
827 * Dummy register read function. Used for register blocks
828 * that certain asics don't have (all asics).
830 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
832 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
838 * amdgpu_invalid_rreg64 - dummy 64 bit reg read function
840 * @adev: amdgpu_device pointer
841 * @reg: offset of register
843 * Dummy register read function. Used for register blocks
844 * that certain asics don't have (all asics).
845 * Returns the value in the register.
847 static uint64_t amdgpu_invalid_rreg64(struct amdgpu_device *adev, uint32_t reg)
849 DRM_ERROR("Invalid callback to read 64 bit register 0x%04X\n", reg);
855 * amdgpu_invalid_wreg64 - dummy reg write function
857 * @adev: amdgpu_device pointer
858 * @reg: offset of register
859 * @v: value to write to the register
861 * Dummy register read function. Used for register blocks
862 * that certain asics don't have (all asics).
864 static void amdgpu_invalid_wreg64(struct amdgpu_device *adev, uint32_t reg, uint64_t v)
866 DRM_ERROR("Invalid callback to write 64 bit register 0x%04X with 0x%08llX\n",
872 * amdgpu_block_invalid_rreg - dummy reg read function
874 * @adev: amdgpu_device pointer
875 * @block: offset of instance
876 * @reg: offset of register
878 * Dummy register read function. Used for register blocks
879 * that certain asics don't have (all asics).
880 * Returns the value in the register.
882 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
883 uint32_t block, uint32_t reg)
885 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
892 * amdgpu_block_invalid_wreg - dummy reg write function
894 * @adev: amdgpu_device pointer
895 * @block: offset of instance
896 * @reg: offset of register
897 * @v: value to write to the register
899 * Dummy register read function. Used for register blocks
900 * that certain asics don't have (all asics).
902 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
904 uint32_t reg, uint32_t v)
906 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
912 * amdgpu_device_asic_init - Wrapper for atom asic_init
914 * @adev: amdgpu_device pointer
916 * Does any asic specific work and then calls atom asic init.
918 static int amdgpu_device_asic_init(struct amdgpu_device *adev)
920 amdgpu_asic_pre_asic_init(adev);
922 if (adev->ip_versions[GC_HWIP][0] >= IP_VERSION(11, 0, 0))
923 return amdgpu_atomfirmware_asic_init(adev, true);
925 return amdgpu_atom_asic_init(adev->mode_info.atom_context);
929 * amdgpu_device_vram_scratch_init - allocate the VRAM scratch page
931 * @adev: amdgpu_device pointer
933 * Allocates a scratch page of VRAM for use by various things in the
936 static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev)
938 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
939 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
940 &adev->vram_scratch.robj,
941 &adev->vram_scratch.gpu_addr,
942 (void **)&adev->vram_scratch.ptr);
946 * amdgpu_device_vram_scratch_fini - Free the VRAM scratch page
948 * @adev: amdgpu_device pointer
950 * Frees the VRAM scratch page.
952 static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev)
954 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
958 * amdgpu_device_program_register_sequence - program an array of registers.
960 * @adev: amdgpu_device pointer
961 * @registers: pointer to the register array
962 * @array_size: size of the register array
964 * Programs an array or registers with and and or masks.
965 * This is a helper for setting golden registers.
967 void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
968 const u32 *registers,
969 const u32 array_size)
971 u32 tmp, reg, and_mask, or_mask;
977 for (i = 0; i < array_size; i +=3) {
978 reg = registers[i + 0];
979 and_mask = registers[i + 1];
980 or_mask = registers[i + 2];
982 if (and_mask == 0xffffffff) {
987 if (adev->family >= AMDGPU_FAMILY_AI)
988 tmp |= (or_mask & and_mask);
997 * amdgpu_device_pci_config_reset - reset the GPU
999 * @adev: amdgpu_device pointer
1001 * Resets the GPU using the pci config reset sequence.
1002 * Only applicable to asics prior to vega10.
1004 void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
1006 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
1010 * amdgpu_device_pci_reset - reset the GPU using generic PCI means
1012 * @adev: amdgpu_device pointer
1014 * Resets the GPU using generic pci reset interfaces (FLR, SBR, etc.).
1016 int amdgpu_device_pci_reset(struct amdgpu_device *adev)
1018 return pci_reset_function(adev->pdev);
1022 * GPU doorbell aperture helpers function.
1025 * amdgpu_device_doorbell_init - Init doorbell driver information.
1027 * @adev: amdgpu_device pointer
1029 * Init doorbell driver information (CIK)
1030 * Returns 0 on success, error on failure.
1032 static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
1035 /* No doorbell on SI hardware generation */
1036 if (adev->asic_type < CHIP_BONAIRE) {
1037 adev->doorbell.base = 0;
1038 adev->doorbell.size = 0;
1039 adev->doorbell.num_doorbells = 0;
1040 adev->doorbell.ptr = NULL;
1044 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
1047 amdgpu_asic_init_doorbell_index(adev);
1049 /* doorbell bar mapping */
1050 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
1051 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
1053 if (adev->enable_mes) {
1054 adev->doorbell.num_doorbells =
1055 adev->doorbell.size / sizeof(u32);
1057 adev->doorbell.num_doorbells =
1058 min_t(u32, adev->doorbell.size / sizeof(u32),
1059 adev->doorbell_index.max_assignment+1);
1060 if (adev->doorbell.num_doorbells == 0)
1063 /* For Vega, reserve and map two pages on doorbell BAR since SDMA
1064 * paging queue doorbell use the second page. The
1065 * AMDGPU_DOORBELL64_MAX_ASSIGNMENT definition assumes all the
1066 * doorbells are in the first page. So with paging queue enabled,
1067 * the max num_doorbells should + 1 page (0x400 in dword)
1069 if (adev->asic_type >= CHIP_VEGA10)
1070 adev->doorbell.num_doorbells += 0x400;
1073 adev->doorbell.ptr = ioremap(adev->doorbell.base,
1074 adev->doorbell.num_doorbells *
1076 if (adev->doorbell.ptr == NULL)
1083 * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
1085 * @adev: amdgpu_device pointer
1087 * Tear down doorbell driver information (CIK)
1089 static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
1091 iounmap(adev->doorbell.ptr);
1092 adev->doorbell.ptr = NULL;
1098 * amdgpu_device_wb_*()
1099 * Writeback is the method by which the GPU updates special pages in memory
1100 * with the status of certain GPU events (fences, ring pointers,etc.).
1104 * amdgpu_device_wb_fini - Disable Writeback and free memory
1106 * @adev: amdgpu_device pointer
1108 * Disables Writeback and frees the Writeback memory (all asics).
1109 * Used at driver shutdown.
1111 static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
1113 if (adev->wb.wb_obj) {
1114 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
1116 (void **)&adev->wb.wb);
1117 adev->wb.wb_obj = NULL;
1122 * amdgpu_device_wb_init - Init Writeback driver info and allocate memory
1124 * @adev: amdgpu_device pointer
1126 * Initializes writeback and allocates writeback memory (all asics).
1127 * Used at driver startup.
1128 * Returns 0 on success or an -error on failure.
1130 static int amdgpu_device_wb_init(struct amdgpu_device *adev)
1134 if (adev->wb.wb_obj == NULL) {
1135 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
1136 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
1137 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
1138 &adev->wb.wb_obj, &adev->wb.gpu_addr,
1139 (void **)&adev->wb.wb);
1141 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
1145 adev->wb.num_wb = AMDGPU_MAX_WB;
1146 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
1148 /* clear wb memory */
1149 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
1156 * amdgpu_device_wb_get - Allocate a wb entry
1158 * @adev: amdgpu_device pointer
1161 * Allocate a wb slot for use by the driver (all asics).
1162 * Returns 0 on success or -EINVAL on failure.
1164 int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
1166 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
1168 if (offset < adev->wb.num_wb) {
1169 __set_bit(offset, adev->wb.used);
1170 *wb = offset << 3; /* convert to dw offset */
1178 * amdgpu_device_wb_free - Free a wb entry
1180 * @adev: amdgpu_device pointer
1183 * Free a wb slot allocated for use by the driver (all asics)
1185 void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
1188 if (wb < adev->wb.num_wb)
1189 __clear_bit(wb, adev->wb.used);
1193 * amdgpu_device_resize_fb_bar - try to resize FB BAR
1195 * @adev: amdgpu_device pointer
1197 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
1198 * to fail, but if any of the BARs is not accessible after the size we abort
1199 * driver loading by returning -ENODEV.
1201 int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
1203 int rbar_size = pci_rebar_bytes_to_size(adev->gmc.real_vram_size);
1204 struct pci_bus *root;
1205 struct resource *res;
1211 if (amdgpu_sriov_vf(adev))
1214 /* skip if the bios has already enabled large BAR */
1215 if (adev->gmc.real_vram_size &&
1216 (pci_resource_len(adev->pdev, 0) >= adev->gmc.real_vram_size))
1219 /* Check if the root BUS has 64bit memory resources */
1220 root = adev->pdev->bus;
1221 while (root->parent)
1222 root = root->parent;
1224 pci_bus_for_each_resource(root, res, i) {
1225 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
1226 res->start > 0x100000000ull)
1230 /* Trying to resize is pointless without a root hub window above 4GB */
1234 /* Limit the BAR size to what is available */
1235 rbar_size = min(fls(pci_rebar_get_possible_sizes(adev->pdev, 0)) - 1,
1238 /* Disable memory decoding while we change the BAR addresses and size */
1239 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
1240 pci_write_config_word(adev->pdev, PCI_COMMAND,
1241 cmd & ~PCI_COMMAND_MEMORY);
1243 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
1244 amdgpu_device_doorbell_fini(adev);
1245 if (adev->asic_type >= CHIP_BONAIRE)
1246 pci_release_resource(adev->pdev, 2);
1248 pci_release_resource(adev->pdev, 0);
1250 r = pci_resize_resource(adev->pdev, 0, rbar_size);
1252 DRM_INFO("Not enough PCI address space for a large BAR.");
1253 else if (r && r != -ENOTSUPP)
1254 DRM_ERROR("Problem resizing BAR0 (%d).", r);
1256 pci_assign_unassigned_bus_resources(adev->pdev->bus);
1258 /* When the doorbell or fb BAR isn't available we have no chance of
1261 r = amdgpu_device_doorbell_init(adev);
1262 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
1265 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
1271 * GPU helpers function.
1274 * amdgpu_device_need_post - check if the hw need post or not
1276 * @adev: amdgpu_device pointer
1278 * Check if the asic has been initialized (all asics) at driver startup
1279 * or post is needed if hw reset is performed.
1280 * Returns true if need or false if not.
1282 bool amdgpu_device_need_post(struct amdgpu_device *adev)
1286 if (amdgpu_sriov_vf(adev))
1289 if (amdgpu_passthrough(adev)) {
1290 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
1291 * some old smc fw still need driver do vPost otherwise gpu hang, while
1292 * those smc fw version above 22.15 doesn't have this flaw, so we force
1293 * vpost executed for smc version below 22.15
1295 if (adev->asic_type == CHIP_FIJI) {
1298 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
1299 /* force vPost if error occured */
1303 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
1304 if (fw_ver < 0x00160e00)
1309 /* Don't post if we need to reset whole hive on init */
1310 if (adev->gmc.xgmi.pending_reset)
1313 if (adev->has_hw_reset) {
1314 adev->has_hw_reset = false;
1318 /* bios scratch used on CIK+ */
1319 if (adev->asic_type >= CHIP_BONAIRE)
1320 return amdgpu_atombios_scratch_need_asic_init(adev);
1322 /* check MEM_SIZE for older asics */
1323 reg = amdgpu_asic_get_config_memsize(adev);
1325 if ((reg != 0) && (reg != 0xffffffff))
1332 * amdgpu_device_should_use_aspm - check if the device should program ASPM
1334 * @adev: amdgpu_device pointer
1336 * Confirm whether the module parameter and pcie bridge agree that ASPM should
1337 * be set for this device.
1339 * Returns true if it should be used or false if not.
1341 bool amdgpu_device_should_use_aspm(struct amdgpu_device *adev)
1343 switch (amdgpu_aspm) {
1353 return pcie_aspm_enabled(adev->pdev);
1356 /* if we get transitioned to only one device, take VGA back */
1358 * amdgpu_device_vga_set_decode - enable/disable vga decode
1360 * @pdev: PCI device pointer
1361 * @state: enable/disable vga decode
1363 * Enable/disable vga decode (all asics).
1364 * Returns VGA resource flags.
1366 static unsigned int amdgpu_device_vga_set_decode(struct pci_dev *pdev,
1369 struct amdgpu_device *adev = drm_to_adev(pci_get_drvdata(pdev));
1370 amdgpu_asic_set_vga_state(adev, state);
1372 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1373 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1375 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1379 * amdgpu_device_check_block_size - validate the vm block size
1381 * @adev: amdgpu_device pointer
1383 * Validates the vm block size specified via module parameter.
1384 * The vm block size defines number of bits in page table versus page directory,
1385 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1386 * page table and the remaining bits are in the page directory.
1388 static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
1390 /* defines number of bits in page table versus page directory,
1391 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1392 * page table and the remaining bits are in the page directory */
1393 if (amdgpu_vm_block_size == -1)
1396 if (amdgpu_vm_block_size < 9) {
1397 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1398 amdgpu_vm_block_size);
1399 amdgpu_vm_block_size = -1;
1404 * amdgpu_device_check_vm_size - validate the vm size
1406 * @adev: amdgpu_device pointer
1408 * Validates the vm size in GB specified via module parameter.
1409 * The VM size is the size of the GPU virtual memory space in GB.
1411 static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
1413 /* no need to check the default value */
1414 if (amdgpu_vm_size == -1)
1417 if (amdgpu_vm_size < 1) {
1418 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1420 amdgpu_vm_size = -1;
1424 static void amdgpu_device_check_smu_prv_buffer_size(struct amdgpu_device *adev)
1427 bool is_os_64 = (sizeof(void *) == 8);
1428 uint64_t total_memory;
1429 uint64_t dram_size_seven_GB = 0x1B8000000;
1430 uint64_t dram_size_three_GB = 0xB8000000;
1432 if (amdgpu_smu_memory_pool_size == 0)
1436 DRM_WARN("Not 64-bit OS, feature not supported\n");
1440 total_memory = (uint64_t)si.totalram * si.mem_unit;
1442 if ((amdgpu_smu_memory_pool_size == 1) ||
1443 (amdgpu_smu_memory_pool_size == 2)) {
1444 if (total_memory < dram_size_three_GB)
1446 } else if ((amdgpu_smu_memory_pool_size == 4) ||
1447 (amdgpu_smu_memory_pool_size == 8)) {
1448 if (total_memory < dram_size_seven_GB)
1451 DRM_WARN("Smu memory pool size not supported\n");
1454 adev->pm.smu_prv_buffer_size = amdgpu_smu_memory_pool_size << 28;
1459 DRM_WARN("No enough system memory\n");
1461 adev->pm.smu_prv_buffer_size = 0;
1464 static int amdgpu_device_init_apu_flags(struct amdgpu_device *adev)
1466 if (!(adev->flags & AMD_IS_APU) ||
1467 adev->asic_type < CHIP_RAVEN)
1470 switch (adev->asic_type) {
1472 if (adev->pdev->device == 0x15dd)
1473 adev->apu_flags |= AMD_APU_IS_RAVEN;
1474 if (adev->pdev->device == 0x15d8)
1475 adev->apu_flags |= AMD_APU_IS_PICASSO;
1478 if ((adev->pdev->device == 0x1636) ||
1479 (adev->pdev->device == 0x164c))
1480 adev->apu_flags |= AMD_APU_IS_RENOIR;
1482 adev->apu_flags |= AMD_APU_IS_GREEN_SARDINE;
1485 adev->apu_flags |= AMD_APU_IS_VANGOGH;
1487 case CHIP_YELLOW_CARP:
1489 case CHIP_CYAN_SKILLFISH:
1490 if ((adev->pdev->device == 0x13FE) ||
1491 (adev->pdev->device == 0x143F))
1492 adev->apu_flags |= AMD_APU_IS_CYAN_SKILLFISH2;
1502 * amdgpu_device_check_arguments - validate module params
1504 * @adev: amdgpu_device pointer
1506 * Validates certain module parameters and updates
1507 * the associated values used by the driver (all asics).
1509 static int amdgpu_device_check_arguments(struct amdgpu_device *adev)
1511 if (amdgpu_sched_jobs < 4) {
1512 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1514 amdgpu_sched_jobs = 4;
1515 } else if (!is_power_of_2(amdgpu_sched_jobs)){
1516 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1518 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1521 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
1522 /* gart size must be greater or equal to 32M */
1523 dev_warn(adev->dev, "gart size (%d) too small\n",
1525 amdgpu_gart_size = -1;
1528 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
1529 /* gtt size must be greater or equal to 32M */
1530 dev_warn(adev->dev, "gtt size (%d) too small\n",
1532 amdgpu_gtt_size = -1;
1535 /* valid range is between 4 and 9 inclusive */
1536 if (amdgpu_vm_fragment_size != -1 &&
1537 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1538 dev_warn(adev->dev, "valid range is between 4 and 9\n");
1539 amdgpu_vm_fragment_size = -1;
1542 if (amdgpu_sched_hw_submission < 2) {
1543 dev_warn(adev->dev, "sched hw submission jobs (%d) must be at least 2\n",
1544 amdgpu_sched_hw_submission);
1545 amdgpu_sched_hw_submission = 2;
1546 } else if (!is_power_of_2(amdgpu_sched_hw_submission)) {
1547 dev_warn(adev->dev, "sched hw submission jobs (%d) must be a power of 2\n",
1548 amdgpu_sched_hw_submission);
1549 amdgpu_sched_hw_submission = roundup_pow_of_two(amdgpu_sched_hw_submission);
1552 if (amdgpu_reset_method < -1 || amdgpu_reset_method > 4) {
1553 dev_warn(adev->dev, "invalid option for reset method, reverting to default\n");
1554 amdgpu_reset_method = -1;
1557 amdgpu_device_check_smu_prv_buffer_size(adev);
1559 amdgpu_device_check_vm_size(adev);
1561 amdgpu_device_check_block_size(adev);
1563 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
1569 * amdgpu_switcheroo_set_state - set switcheroo state
1571 * @pdev: pci dev pointer
1572 * @state: vga_switcheroo state
1574 * Callback for the switcheroo driver. Suspends or resumes the
1575 * the asics before or after it is powered up using ACPI methods.
1577 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev,
1578 enum vga_switcheroo_state state)
1580 struct drm_device *dev = pci_get_drvdata(pdev);
1583 if (amdgpu_device_supports_px(dev) && state == VGA_SWITCHEROO_OFF)
1586 if (state == VGA_SWITCHEROO_ON) {
1587 pr_info("switched on\n");
1588 /* don't suspend or resume card normally */
1589 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1591 pci_set_power_state(pdev, PCI_D0);
1592 amdgpu_device_load_pci_state(pdev);
1593 r = pci_enable_device(pdev);
1595 DRM_WARN("pci_enable_device failed (%d)\n", r);
1596 amdgpu_device_resume(dev, true);
1598 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1600 pr_info("switched off\n");
1601 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1602 amdgpu_device_suspend(dev, true);
1603 amdgpu_device_cache_pci_state(pdev);
1604 /* Shut down the device */
1605 pci_disable_device(pdev);
1606 pci_set_power_state(pdev, PCI_D3cold);
1607 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1612 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1614 * @pdev: pci dev pointer
1616 * Callback for the switcheroo driver. Check of the switcheroo
1617 * state can be changed.
1618 * Returns true if the state can be changed, false if not.
1620 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1622 struct drm_device *dev = pci_get_drvdata(pdev);
1625 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1626 * locking inversion with the driver load path. And the access here is
1627 * completely racy anyway. So don't bother with locking for now.
1629 return atomic_read(&dev->open_count) == 0;
1632 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1633 .set_gpu_state = amdgpu_switcheroo_set_state,
1635 .can_switch = amdgpu_switcheroo_can_switch,
1639 * amdgpu_device_ip_set_clockgating_state - set the CG state
1641 * @dev: amdgpu_device pointer
1642 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1643 * @state: clockgating state (gate or ungate)
1645 * Sets the requested clockgating state for all instances of
1646 * the hardware IP specified.
1647 * Returns the error code from the last instance.
1649 int amdgpu_device_ip_set_clockgating_state(void *dev,
1650 enum amd_ip_block_type block_type,
1651 enum amd_clockgating_state state)
1653 struct amdgpu_device *adev = dev;
1656 for (i = 0; i < adev->num_ip_blocks; i++) {
1657 if (!adev->ip_blocks[i].status.valid)
1659 if (adev->ip_blocks[i].version->type != block_type)
1661 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1663 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1664 (void *)adev, state);
1666 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1667 adev->ip_blocks[i].version->funcs->name, r);
1673 * amdgpu_device_ip_set_powergating_state - set the PG state
1675 * @dev: amdgpu_device pointer
1676 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1677 * @state: powergating state (gate or ungate)
1679 * Sets the requested powergating state for all instances of
1680 * the hardware IP specified.
1681 * Returns the error code from the last instance.
1683 int amdgpu_device_ip_set_powergating_state(void *dev,
1684 enum amd_ip_block_type block_type,
1685 enum amd_powergating_state state)
1687 struct amdgpu_device *adev = dev;
1690 for (i = 0; i < adev->num_ip_blocks; i++) {
1691 if (!adev->ip_blocks[i].status.valid)
1693 if (adev->ip_blocks[i].version->type != block_type)
1695 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1697 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1698 (void *)adev, state);
1700 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1701 adev->ip_blocks[i].version->funcs->name, r);
1707 * amdgpu_device_ip_get_clockgating_state - get the CG state
1709 * @adev: amdgpu_device pointer
1710 * @flags: clockgating feature flags
1712 * Walks the list of IPs on the device and updates the clockgating
1713 * flags for each IP.
1714 * Updates @flags with the feature flags for each hardware IP where
1715 * clockgating is enabled.
1717 void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
1722 for (i = 0; i < adev->num_ip_blocks; i++) {
1723 if (!adev->ip_blocks[i].status.valid)
1725 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1726 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1731 * amdgpu_device_ip_wait_for_idle - wait for idle
1733 * @adev: amdgpu_device pointer
1734 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1736 * Waits for the request hardware IP to be idle.
1737 * Returns 0 for success or a negative error code on failure.
1739 int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1740 enum amd_ip_block_type block_type)
1744 for (i = 0; i < adev->num_ip_blocks; i++) {
1745 if (!adev->ip_blocks[i].status.valid)
1747 if (adev->ip_blocks[i].version->type == block_type) {
1748 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
1759 * amdgpu_device_ip_is_idle - is the hardware IP idle
1761 * @adev: amdgpu_device pointer
1762 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1764 * Check if the hardware IP is idle or not.
1765 * Returns true if it the IP is idle, false if not.
1767 bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1768 enum amd_ip_block_type block_type)
1772 for (i = 0; i < adev->num_ip_blocks; i++) {
1773 if (!adev->ip_blocks[i].status.valid)
1775 if (adev->ip_blocks[i].version->type == block_type)
1776 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
1783 * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1785 * @adev: amdgpu_device pointer
1786 * @type: Type of hardware IP (SMU, GFX, UVD, etc.)
1788 * Returns a pointer to the hardware IP block structure
1789 * if it exists for the asic, otherwise NULL.
1791 struct amdgpu_ip_block *
1792 amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1793 enum amd_ip_block_type type)
1797 for (i = 0; i < adev->num_ip_blocks; i++)
1798 if (adev->ip_blocks[i].version->type == type)
1799 return &adev->ip_blocks[i];
1805 * amdgpu_device_ip_block_version_cmp
1807 * @adev: amdgpu_device pointer
1808 * @type: enum amd_ip_block_type
1809 * @major: major version
1810 * @minor: minor version
1812 * return 0 if equal or greater
1813 * return 1 if smaller or the ip_block doesn't exist
1815 int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1816 enum amd_ip_block_type type,
1817 u32 major, u32 minor)
1819 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
1821 if (ip_block && ((ip_block->version->major > major) ||
1822 ((ip_block->version->major == major) &&
1823 (ip_block->version->minor >= minor))))
1830 * amdgpu_device_ip_block_add
1832 * @adev: amdgpu_device pointer
1833 * @ip_block_version: pointer to the IP to add
1835 * Adds the IP block driver information to the collection of IPs
1838 int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1839 const struct amdgpu_ip_block_version *ip_block_version)
1841 if (!ip_block_version)
1844 switch (ip_block_version->type) {
1845 case AMD_IP_BLOCK_TYPE_VCN:
1846 if (adev->harvest_ip_mask & AMD_HARVEST_IP_VCN_MASK)
1849 case AMD_IP_BLOCK_TYPE_JPEG:
1850 if (adev->harvest_ip_mask & AMD_HARVEST_IP_JPEG_MASK)
1857 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
1858 ip_block_version->funcs->name);
1860 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1866 * amdgpu_device_enable_virtual_display - enable virtual display feature
1868 * @adev: amdgpu_device pointer
1870 * Enabled the virtual display feature if the user has enabled it via
1871 * the module parameter virtual_display. This feature provides a virtual
1872 * display hardware on headless boards or in virtualized environments.
1873 * This function parses and validates the configuration string specified by
1874 * the user and configues the virtual display configuration (number of
1875 * virtual connectors, crtcs, etc.) specified.
1877 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
1879 adev->enable_virtual_display = false;
1881 if (amdgpu_virtual_display) {
1882 const char *pci_address_name = pci_name(adev->pdev);
1883 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
1885 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1886 pciaddstr_tmp = pciaddstr;
1887 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1888 pciaddname = strsep(&pciaddname_tmp, ",");
1889 if (!strcmp("all", pciaddname)
1890 || !strcmp(pci_address_name, pciaddname)) {
1894 adev->enable_virtual_display = true;
1897 res = kstrtol(pciaddname_tmp, 10,
1905 adev->mode_info.num_crtc = num_crtc;
1907 adev->mode_info.num_crtc = 1;
1913 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1914 amdgpu_virtual_display, pci_address_name,
1915 adev->enable_virtual_display, adev->mode_info.num_crtc);
1922 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
1924 * @adev: amdgpu_device pointer
1926 * Parses the asic configuration parameters specified in the gpu info
1927 * firmware and makes them availale to the driver for use in configuring
1929 * Returns 0 on success, -EINVAL on failure.
1931 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1933 const char *chip_name;
1936 const struct gpu_info_firmware_header_v1_0 *hdr;
1938 adev->firmware.gpu_info_fw = NULL;
1940 if (adev->mman.discovery_bin) {
1942 * FIXME: The bounding box is still needed by Navi12, so
1943 * temporarily read it from gpu_info firmware. Should be dropped
1944 * when DAL no longer needs it.
1946 if (adev->asic_type != CHIP_NAVI12)
1950 switch (adev->asic_type) {
1954 chip_name = "vega10";
1957 chip_name = "vega12";
1960 if (adev->apu_flags & AMD_APU_IS_RAVEN2)
1961 chip_name = "raven2";
1962 else if (adev->apu_flags & AMD_APU_IS_PICASSO)
1963 chip_name = "picasso";
1965 chip_name = "raven";
1968 chip_name = "arcturus";
1971 chip_name = "navi12";
1975 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
1976 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
1979 "Failed to load gpu_info firmware \"%s\"\n",
1983 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
1986 "Failed to validate gpu_info firmware \"%s\"\n",
1991 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
1992 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1994 switch (hdr->version_major) {
1997 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
1998 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
1999 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2002 * Should be droped when DAL no longer needs it.
2004 if (adev->asic_type == CHIP_NAVI12)
2005 goto parse_soc_bounding_box;
2007 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
2008 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
2009 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
2010 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
2011 adev->gfx.config.max_texture_channel_caches =
2012 le32_to_cpu(gpu_info_fw->gc_num_tccs);
2013 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
2014 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
2015 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
2016 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
2017 adev->gfx.config.double_offchip_lds_buf =
2018 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
2019 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
2020 adev->gfx.cu_info.max_waves_per_simd =
2021 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
2022 adev->gfx.cu_info.max_scratch_slots_per_cu =
2023 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
2024 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
2025 if (hdr->version_minor >= 1) {
2026 const struct gpu_info_firmware_v1_1 *gpu_info_fw =
2027 (const struct gpu_info_firmware_v1_1 *)(adev->firmware.gpu_info_fw->data +
2028 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2029 adev->gfx.config.num_sc_per_sh =
2030 le32_to_cpu(gpu_info_fw->num_sc_per_sh);
2031 adev->gfx.config.num_packer_per_sc =
2032 le32_to_cpu(gpu_info_fw->num_packer_per_sc);
2035 parse_soc_bounding_box:
2037 * soc bounding box info is not integrated in disocovery table,
2038 * we always need to parse it from gpu info firmware if needed.
2040 if (hdr->version_minor == 2) {
2041 const struct gpu_info_firmware_v1_2 *gpu_info_fw =
2042 (const struct gpu_info_firmware_v1_2 *)(adev->firmware.gpu_info_fw->data +
2043 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2044 adev->dm.soc_bounding_box = &gpu_info_fw->soc_bounding_box;
2050 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
2059 * amdgpu_device_ip_early_init - run early init for hardware IPs
2061 * @adev: amdgpu_device pointer
2063 * Early initialization pass for hardware IPs. The hardware IPs that make
2064 * up each asic are discovered each IP's early_init callback is run. This
2065 * is the first stage in initializing the asic.
2066 * Returns 0 on success, negative error code on failure.
2068 static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
2070 struct drm_device *dev = adev_to_drm(adev);
2071 struct pci_dev *parent;
2074 amdgpu_device_enable_virtual_display(adev);
2076 if (amdgpu_sriov_vf(adev)) {
2077 r = amdgpu_virt_request_full_gpu(adev, true);
2082 switch (adev->asic_type) {
2083 #ifdef CONFIG_DRM_AMDGPU_SI
2089 adev->family = AMDGPU_FAMILY_SI;
2090 r = si_set_ip_blocks(adev);
2095 #ifdef CONFIG_DRM_AMDGPU_CIK
2101 if (adev->flags & AMD_IS_APU)
2102 adev->family = AMDGPU_FAMILY_KV;
2104 adev->family = AMDGPU_FAMILY_CI;
2106 r = cik_set_ip_blocks(adev);
2114 case CHIP_POLARIS10:
2115 case CHIP_POLARIS11:
2116 case CHIP_POLARIS12:
2120 if (adev->flags & AMD_IS_APU)
2121 adev->family = AMDGPU_FAMILY_CZ;
2123 adev->family = AMDGPU_FAMILY_VI;
2125 r = vi_set_ip_blocks(adev);
2130 r = amdgpu_discovery_set_ip_blocks(adev);
2136 if (amdgpu_has_atpx() &&
2137 (amdgpu_is_atpx_hybrid() ||
2138 amdgpu_has_atpx_dgpu_power_cntl()) &&
2139 ((adev->flags & AMD_IS_APU) == 0) &&
2140 !pci_is_thunderbolt_attached(to_pci_dev(dev->dev)))
2141 adev->flags |= AMD_IS_PX;
2143 if (!(adev->flags & AMD_IS_APU)) {
2144 parent = pci_upstream_bridge(adev->pdev);
2145 adev->has_pr3 = parent ? pci_pr3_present(parent) : false;
2148 amdgpu_amdkfd_device_probe(adev);
2150 adev->pm.pp_feature = amdgpu_pp_feature_mask;
2151 if (amdgpu_sriov_vf(adev) || sched_policy == KFD_SCHED_POLICY_NO_HWS)
2152 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
2153 if (amdgpu_sriov_vf(adev) && adev->asic_type == CHIP_SIENNA_CICHLID)
2154 adev->pm.pp_feature &= ~PP_OVERDRIVE_MASK;
2156 for (i = 0; i < adev->num_ip_blocks; i++) {
2157 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
2158 DRM_ERROR("disabled ip block: %d <%s>\n",
2159 i, adev->ip_blocks[i].version->funcs->name);
2160 adev->ip_blocks[i].status.valid = false;
2162 if (adev->ip_blocks[i].version->funcs->early_init) {
2163 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
2165 adev->ip_blocks[i].status.valid = false;
2167 DRM_ERROR("early_init of IP block <%s> failed %d\n",
2168 adev->ip_blocks[i].version->funcs->name, r);
2171 adev->ip_blocks[i].status.valid = true;
2174 adev->ip_blocks[i].status.valid = true;
2177 /* get the vbios after the asic_funcs are set up */
2178 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) {
2179 r = amdgpu_device_parse_gpu_info_fw(adev);
2184 if (!amdgpu_get_bios(adev))
2187 r = amdgpu_atombios_init(adev);
2189 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
2190 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
2194 /*get pf2vf msg info at it's earliest time*/
2195 if (amdgpu_sriov_vf(adev))
2196 amdgpu_virt_init_data_exchange(adev);
2201 adev->cg_flags &= amdgpu_cg_mask;
2202 adev->pg_flags &= amdgpu_pg_mask;
2207 static int amdgpu_device_ip_hw_init_phase1(struct amdgpu_device *adev)
2211 for (i = 0; i < adev->num_ip_blocks; i++) {
2212 if (!adev->ip_blocks[i].status.sw)
2214 if (adev->ip_blocks[i].status.hw)
2216 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2217 (amdgpu_sriov_vf(adev) && (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)) ||
2218 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
2219 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2221 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2222 adev->ip_blocks[i].version->funcs->name, r);
2225 adev->ip_blocks[i].status.hw = true;
2232 static int amdgpu_device_ip_hw_init_phase2(struct amdgpu_device *adev)
2236 for (i = 0; i < adev->num_ip_blocks; i++) {
2237 if (!adev->ip_blocks[i].status.sw)
2239 if (adev->ip_blocks[i].status.hw)
2241 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2243 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2244 adev->ip_blocks[i].version->funcs->name, r);
2247 adev->ip_blocks[i].status.hw = true;
2253 static int amdgpu_device_fw_loading(struct amdgpu_device *adev)
2257 uint32_t smu_version;
2259 if (adev->asic_type >= CHIP_VEGA10) {
2260 for (i = 0; i < adev->num_ip_blocks; i++) {
2261 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_PSP)
2264 if (!adev->ip_blocks[i].status.sw)
2267 /* no need to do the fw loading again if already done*/
2268 if (adev->ip_blocks[i].status.hw == true)
2271 if (amdgpu_in_reset(adev) || adev->in_suspend) {
2272 r = adev->ip_blocks[i].version->funcs->resume(adev);
2274 DRM_ERROR("resume of IP block <%s> failed %d\n",
2275 adev->ip_blocks[i].version->funcs->name, r);
2279 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2281 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2282 adev->ip_blocks[i].version->funcs->name, r);
2287 adev->ip_blocks[i].status.hw = true;
2292 if (!amdgpu_sriov_vf(adev) || adev->asic_type == CHIP_TONGA)
2293 r = amdgpu_pm_load_smu_firmware(adev, &smu_version);
2298 static int amdgpu_device_init_schedulers(struct amdgpu_device *adev)
2303 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2304 struct amdgpu_ring *ring = adev->rings[i];
2306 /* No need to setup the GPU scheduler for rings that don't need it */
2307 if (!ring || ring->no_scheduler)
2310 switch (ring->funcs->type) {
2311 case AMDGPU_RING_TYPE_GFX:
2312 timeout = adev->gfx_timeout;
2314 case AMDGPU_RING_TYPE_COMPUTE:
2315 timeout = adev->compute_timeout;
2317 case AMDGPU_RING_TYPE_SDMA:
2318 timeout = adev->sdma_timeout;
2321 timeout = adev->video_timeout;
2325 r = drm_sched_init(&ring->sched, &amdgpu_sched_ops,
2326 ring->num_hw_submission, amdgpu_job_hang_limit,
2327 timeout, adev->reset_domain->wq,
2328 ring->sched_score, ring->name,
2331 DRM_ERROR("Failed to create scheduler on ring %s.\n",
2342 * amdgpu_device_ip_init - run init for hardware IPs
2344 * @adev: amdgpu_device pointer
2346 * Main initialization pass for hardware IPs. The list of all the hardware
2347 * IPs that make up the asic is walked and the sw_init and hw_init callbacks
2348 * are run. sw_init initializes the software state associated with each IP
2349 * and hw_init initializes the hardware associated with each IP.
2350 * Returns 0 on success, negative error code on failure.
2352 static int amdgpu_device_ip_init(struct amdgpu_device *adev)
2356 r = amdgpu_ras_init(adev);
2360 for (i = 0; i < adev->num_ip_blocks; i++) {
2361 if (!adev->ip_blocks[i].status.valid)
2363 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
2365 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
2366 adev->ip_blocks[i].version->funcs->name, r);
2369 adev->ip_blocks[i].status.sw = true;
2371 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) {
2372 /* need to do common hw init early so everything is set up for gmc */
2373 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
2375 DRM_ERROR("hw_init %d failed %d\n", i, r);
2378 adev->ip_blocks[i].status.hw = true;
2379 } else if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
2380 /* need to do gmc hw init early so we can allocate gpu mem */
2381 /* Try to reserve bad pages early */
2382 if (amdgpu_sriov_vf(adev))
2383 amdgpu_virt_exchange_data(adev);
2385 r = amdgpu_device_vram_scratch_init(adev);
2387 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
2390 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
2392 DRM_ERROR("hw_init %d failed %d\n", i, r);
2395 r = amdgpu_device_wb_init(adev);
2397 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
2400 adev->ip_blocks[i].status.hw = true;
2402 /* right after GMC hw init, we create CSA */
2403 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) {
2404 r = amdgpu_allocate_static_csa(adev, &adev->virt.csa_obj,
2405 AMDGPU_GEM_DOMAIN_VRAM,
2408 DRM_ERROR("allocate CSA failed %d\n", r);
2415 if (amdgpu_sriov_vf(adev))
2416 amdgpu_virt_init_data_exchange(adev);
2418 r = amdgpu_ib_pool_init(adev);
2420 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
2421 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
2425 r = amdgpu_ucode_create_bo(adev); /* create ucode bo when sw_init complete*/
2429 r = amdgpu_device_ip_hw_init_phase1(adev);
2433 r = amdgpu_device_fw_loading(adev);
2437 r = amdgpu_device_ip_hw_init_phase2(adev);
2442 * retired pages will be loaded from eeprom and reserved here,
2443 * it should be called after amdgpu_device_ip_hw_init_phase2 since
2444 * for some ASICs the RAS EEPROM code relies on SMU fully functioning
2445 * for I2C communication which only true at this point.
2447 * amdgpu_ras_recovery_init may fail, but the upper only cares the
2448 * failure from bad gpu situation and stop amdgpu init process
2449 * accordingly. For other failed cases, it will still release all
2450 * the resource and print error message, rather than returning one
2451 * negative value to upper level.
2453 * Note: theoretically, this should be called before all vram allocations
2454 * to protect retired page from abusing
2456 r = amdgpu_ras_recovery_init(adev);
2461 * In case of XGMI grab extra reference for reset domain for this device
2463 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2464 if (amdgpu_xgmi_add_device(adev) == 0) {
2465 if (!amdgpu_sriov_vf(adev)) {
2466 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
2468 if (WARN_ON(!hive)) {
2473 if (!hive->reset_domain ||
2474 !amdgpu_reset_get_reset_domain(hive->reset_domain)) {
2476 amdgpu_put_xgmi_hive(hive);
2480 /* Drop the early temporary reset domain we created for device */
2481 amdgpu_reset_put_reset_domain(adev->reset_domain);
2482 adev->reset_domain = hive->reset_domain;
2483 amdgpu_put_xgmi_hive(hive);
2488 r = amdgpu_device_init_schedulers(adev);
2492 /* Don't init kfd if whole hive need to be reset during init */
2493 if (!adev->gmc.xgmi.pending_reset)
2494 amdgpu_amdkfd_device_init(adev);
2496 amdgpu_fru_get_product_info(adev);
2499 if (amdgpu_sriov_vf(adev))
2500 amdgpu_virt_release_full_gpu(adev, true);
2506 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
2508 * @adev: amdgpu_device pointer
2510 * Writes a reset magic value to the gart pointer in VRAM. The driver calls
2511 * this function before a GPU reset. If the value is retained after a
2512 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
2514 static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
2516 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
2520 * amdgpu_device_check_vram_lost - check if vram is valid
2522 * @adev: amdgpu_device pointer
2524 * Checks the reset magic value written to the gart pointer in VRAM.
2525 * The driver calls this after a GPU reset to see if the contents of
2526 * VRAM is lost or now.
2527 * returns true if vram is lost, false if not.
2529 static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
2531 if (memcmp(adev->gart.ptr, adev->reset_magic,
2532 AMDGPU_RESET_MAGIC_NUM))
2535 if (!amdgpu_in_reset(adev))
2539 * For all ASICs with baco/mode1 reset, the VRAM is
2540 * always assumed to be lost.
2542 switch (amdgpu_asic_reset_method(adev)) {
2543 case AMD_RESET_METHOD_BACO:
2544 case AMD_RESET_METHOD_MODE1:
2552 * amdgpu_device_set_cg_state - set clockgating for amdgpu device
2554 * @adev: amdgpu_device pointer
2555 * @state: clockgating state (gate or ungate)
2557 * The list of all the hardware IPs that make up the asic is walked and the
2558 * set_clockgating_state callbacks are run.
2559 * Late initialization pass enabling clockgating for hardware IPs.
2560 * Fini or suspend, pass disabling clockgating for hardware IPs.
2561 * Returns 0 on success, negative error code on failure.
2564 int amdgpu_device_set_cg_state(struct amdgpu_device *adev,
2565 enum amd_clockgating_state state)
2569 if (amdgpu_emu_mode == 1)
2572 for (j = 0; j < adev->num_ip_blocks; j++) {
2573 i = state == AMD_CG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
2574 if (!adev->ip_blocks[i].status.late_initialized)
2576 /* skip CG for GFX on S0ix */
2577 if (adev->in_s0ix &&
2578 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX)
2580 /* skip CG for VCE/UVD, it's handled specially */
2581 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
2582 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
2583 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
2584 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
2585 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
2586 /* enable clockgating to save power */
2587 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
2590 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
2591 adev->ip_blocks[i].version->funcs->name, r);
2600 int amdgpu_device_set_pg_state(struct amdgpu_device *adev,
2601 enum amd_powergating_state state)
2605 if (amdgpu_emu_mode == 1)
2608 for (j = 0; j < adev->num_ip_blocks; j++) {
2609 i = state == AMD_PG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
2610 if (!adev->ip_blocks[i].status.late_initialized)
2612 /* skip PG for GFX on S0ix */
2613 if (adev->in_s0ix &&
2614 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX)
2616 /* skip CG for VCE/UVD, it's handled specially */
2617 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
2618 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
2619 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
2620 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
2621 adev->ip_blocks[i].version->funcs->set_powergating_state) {
2622 /* enable powergating to save power */
2623 r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
2626 DRM_ERROR("set_powergating_state(gate) of IP block <%s> failed %d\n",
2627 adev->ip_blocks[i].version->funcs->name, r);
2635 static int amdgpu_device_enable_mgpu_fan_boost(void)
2637 struct amdgpu_gpu_instance *gpu_ins;
2638 struct amdgpu_device *adev;
2641 mutex_lock(&mgpu_info.mutex);
2644 * MGPU fan boost feature should be enabled
2645 * only when there are two or more dGPUs in
2648 if (mgpu_info.num_dgpu < 2)
2651 for (i = 0; i < mgpu_info.num_dgpu; i++) {
2652 gpu_ins = &(mgpu_info.gpu_ins[i]);
2653 adev = gpu_ins->adev;
2654 if (!(adev->flags & AMD_IS_APU) &&
2655 !gpu_ins->mgpu_fan_enabled) {
2656 ret = amdgpu_dpm_enable_mgpu_fan_boost(adev);
2660 gpu_ins->mgpu_fan_enabled = 1;
2665 mutex_unlock(&mgpu_info.mutex);
2671 * amdgpu_device_ip_late_init - run late init for hardware IPs
2673 * @adev: amdgpu_device pointer
2675 * Late initialization pass for hardware IPs. The list of all the hardware
2676 * IPs that make up the asic is walked and the late_init callbacks are run.
2677 * late_init covers any special initialization that an IP requires
2678 * after all of the have been initialized or something that needs to happen
2679 * late in the init process.
2680 * Returns 0 on success, negative error code on failure.
2682 static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
2684 struct amdgpu_gpu_instance *gpu_instance;
2687 for (i = 0; i < adev->num_ip_blocks; i++) {
2688 if (!adev->ip_blocks[i].status.hw)
2690 if (adev->ip_blocks[i].version->funcs->late_init) {
2691 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
2693 DRM_ERROR("late_init of IP block <%s> failed %d\n",
2694 adev->ip_blocks[i].version->funcs->name, r);
2698 adev->ip_blocks[i].status.late_initialized = true;
2701 r = amdgpu_ras_late_init(adev);
2703 DRM_ERROR("amdgpu_ras_late_init failed %d", r);
2707 amdgpu_ras_set_error_query_ready(adev, true);
2709 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE);
2710 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_GATE);
2712 amdgpu_device_fill_reset_magic(adev);
2714 r = amdgpu_device_enable_mgpu_fan_boost();
2716 DRM_ERROR("enable mgpu fan boost failed (%d).\n", r);
2718 /* For passthrough configuration on arcturus and aldebaran, enable special handling SBR */
2719 if (amdgpu_passthrough(adev) && ((adev->asic_type == CHIP_ARCTURUS && adev->gmc.xgmi.num_physical_nodes > 1)||
2720 adev->asic_type == CHIP_ALDEBARAN ))
2721 amdgpu_dpm_handle_passthrough_sbr(adev, true);
2723 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2724 mutex_lock(&mgpu_info.mutex);
2727 * Reset device p-state to low as this was booted with high.
2729 * This should be performed only after all devices from the same
2730 * hive get initialized.
2732 * However, it's unknown how many device in the hive in advance.
2733 * As this is counted one by one during devices initializations.
2735 * So, we wait for all XGMI interlinked devices initialized.
2736 * This may bring some delays as those devices may come from
2737 * different hives. But that should be OK.
2739 if (mgpu_info.num_dgpu == adev->gmc.xgmi.num_physical_nodes) {
2740 for (i = 0; i < mgpu_info.num_gpu; i++) {
2741 gpu_instance = &(mgpu_info.gpu_ins[i]);
2742 if (gpu_instance->adev->flags & AMD_IS_APU)
2745 r = amdgpu_xgmi_set_pstate(gpu_instance->adev,
2746 AMDGPU_XGMI_PSTATE_MIN);
2748 DRM_ERROR("pstate setting failed (%d).\n", r);
2754 mutex_unlock(&mgpu_info.mutex);
2761 * amdgpu_device_smu_fini_early - smu hw_fini wrapper
2763 * @adev: amdgpu_device pointer
2765 * For ASICs need to disable SMC first
2767 static void amdgpu_device_smu_fini_early(struct amdgpu_device *adev)
2771 if (adev->ip_versions[GC_HWIP][0] > IP_VERSION(9, 0, 0))
2774 for (i = 0; i < adev->num_ip_blocks; i++) {
2775 if (!adev->ip_blocks[i].status.hw)
2777 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
2778 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2779 /* XXX handle errors */
2781 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2782 adev->ip_blocks[i].version->funcs->name, r);
2784 adev->ip_blocks[i].status.hw = false;
2790 static int amdgpu_device_ip_fini_early(struct amdgpu_device *adev)
2794 for (i = 0; i < adev->num_ip_blocks; i++) {
2795 if (!adev->ip_blocks[i].version->funcs->early_fini)
2798 r = adev->ip_blocks[i].version->funcs->early_fini((void *)adev);
2800 DRM_DEBUG("early_fini of IP block <%s> failed %d\n",
2801 adev->ip_blocks[i].version->funcs->name, r);
2805 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2806 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2808 amdgpu_amdkfd_suspend(adev, false);
2810 /* Workaroud for ASICs need to disable SMC first */
2811 amdgpu_device_smu_fini_early(adev);
2813 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2814 if (!adev->ip_blocks[i].status.hw)
2817 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2818 /* XXX handle errors */
2820 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2821 adev->ip_blocks[i].version->funcs->name, r);
2824 adev->ip_blocks[i].status.hw = false;
2827 if (amdgpu_sriov_vf(adev)) {
2828 if (amdgpu_virt_release_full_gpu(adev, false))
2829 DRM_ERROR("failed to release exclusive mode on fini\n");
2836 * amdgpu_device_ip_fini - run fini for hardware IPs
2838 * @adev: amdgpu_device pointer
2840 * Main teardown pass for hardware IPs. The list of all the hardware
2841 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
2842 * are run. hw_fini tears down the hardware associated with each IP
2843 * and sw_fini tears down any software state associated with each IP.
2844 * Returns 0 on success, negative error code on failure.
2846 static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
2850 if (amdgpu_sriov_vf(adev) && adev->virt.ras_init_done)
2851 amdgpu_virt_release_ras_err_handler_data(adev);
2853 if (adev->gmc.xgmi.num_physical_nodes > 1)
2854 amdgpu_xgmi_remove_device(adev);
2856 amdgpu_amdkfd_device_fini_sw(adev);
2858 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2859 if (!adev->ip_blocks[i].status.sw)
2862 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
2863 amdgpu_ucode_free_bo(adev);
2864 amdgpu_free_static_csa(&adev->virt.csa_obj);
2865 amdgpu_device_wb_fini(adev);
2866 amdgpu_device_vram_scratch_fini(adev);
2867 amdgpu_ib_pool_fini(adev);
2870 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
2871 /* XXX handle errors */
2873 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
2874 adev->ip_blocks[i].version->funcs->name, r);
2876 adev->ip_blocks[i].status.sw = false;
2877 adev->ip_blocks[i].status.valid = false;
2880 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2881 if (!adev->ip_blocks[i].status.late_initialized)
2883 if (adev->ip_blocks[i].version->funcs->late_fini)
2884 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
2885 adev->ip_blocks[i].status.late_initialized = false;
2888 amdgpu_ras_fini(adev);
2894 * amdgpu_device_delayed_init_work_handler - work handler for IB tests
2896 * @work: work_struct.
2898 static void amdgpu_device_delayed_init_work_handler(struct work_struct *work)
2900 struct amdgpu_device *adev =
2901 container_of(work, struct amdgpu_device, delayed_init_work.work);
2904 r = amdgpu_ib_ring_tests(adev);
2906 DRM_ERROR("ib ring test failed (%d).\n", r);
2909 static void amdgpu_device_delay_enable_gfx_off(struct work_struct *work)
2911 struct amdgpu_device *adev =
2912 container_of(work, struct amdgpu_device, gfx.gfx_off_delay_work.work);
2914 WARN_ON_ONCE(adev->gfx.gfx_off_state);
2915 WARN_ON_ONCE(adev->gfx.gfx_off_req_count);
2917 if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true))
2918 adev->gfx.gfx_off_state = true;
2922 * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1)
2924 * @adev: amdgpu_device pointer
2926 * Main suspend function for hardware IPs. The list of all the hardware
2927 * IPs that make up the asic is walked, clockgating is disabled and the
2928 * suspend callbacks are run. suspend puts the hardware and software state
2929 * in each IP into a state suitable for suspend.
2930 * Returns 0 on success, negative error code on failure.
2932 static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev)
2936 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2937 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2940 * Per PMFW team's suggestion, driver needs to handle gfxoff
2941 * and df cstate features disablement for gpu reset(e.g. Mode1Reset)
2942 * scenario. Add the missing df cstate disablement here.
2944 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_DISALLOW))
2945 dev_warn(adev->dev, "Failed to disallow df cstate");
2947 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2948 if (!adev->ip_blocks[i].status.valid)
2951 /* displays are handled separately */
2952 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_DCE)
2955 /* XXX handle errors */
2956 r = adev->ip_blocks[i].version->funcs->suspend(adev);
2957 /* XXX handle errors */
2959 DRM_ERROR("suspend of IP block <%s> failed %d\n",
2960 adev->ip_blocks[i].version->funcs->name, r);
2964 adev->ip_blocks[i].status.hw = false;
2971 * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2)
2973 * @adev: amdgpu_device pointer
2975 * Main suspend function for hardware IPs. The list of all the hardware
2976 * IPs that make up the asic is walked, clockgating is disabled and the
2977 * suspend callbacks are run. suspend puts the hardware and software state
2978 * in each IP into a state suitable for suspend.
2979 * Returns 0 on success, negative error code on failure.
2981 static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev)
2986 amdgpu_dpm_gfx_state_change(adev, sGpuChangeState_D3Entry);
2988 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2989 if (!adev->ip_blocks[i].status.valid)
2991 /* displays are handled in phase1 */
2992 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)
2994 /* PSP lost connection when err_event_athub occurs */
2995 if (amdgpu_ras_intr_triggered() &&
2996 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
2997 adev->ip_blocks[i].status.hw = false;
3001 /* skip unnecessary suspend if we do not initialize them yet */
3002 if (adev->gmc.xgmi.pending_reset &&
3003 !(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3004 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC ||
3005 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3006 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)) {
3007 adev->ip_blocks[i].status.hw = false;
3011 /* skip suspend of gfx/mes and psp for S0ix
3012 * gfx is in gfxoff state, so on resume it will exit gfxoff just
3013 * like at runtime. PSP is also part of the always on hardware
3014 * so no need to suspend it.
3016 if (adev->in_s0ix &&
3017 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP ||
3018 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX ||
3019 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_MES))
3022 /* XXX handle errors */
3023 r = adev->ip_blocks[i].version->funcs->suspend(adev);
3024 /* XXX handle errors */
3026 DRM_ERROR("suspend of IP block <%s> failed %d\n",
3027 adev->ip_blocks[i].version->funcs->name, r);
3029 adev->ip_blocks[i].status.hw = false;
3030 /* handle putting the SMC in the appropriate state */
3031 if(!amdgpu_sriov_vf(adev)){
3032 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
3033 r = amdgpu_dpm_set_mp1_state(adev, adev->mp1_state);
3035 DRM_ERROR("SMC failed to set mp1 state %d, %d\n",
3036 adev->mp1_state, r);
3047 * amdgpu_device_ip_suspend - run suspend for hardware IPs
3049 * @adev: amdgpu_device pointer
3051 * Main suspend function for hardware IPs. The list of all the hardware
3052 * IPs that make up the asic is walked, clockgating is disabled and the
3053 * suspend callbacks are run. suspend puts the hardware and software state
3054 * in each IP into a state suitable for suspend.
3055 * Returns 0 on success, negative error code on failure.
3057 int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
3061 if (amdgpu_sriov_vf(adev)) {
3062 amdgpu_virt_fini_data_exchange(adev);
3063 amdgpu_virt_request_full_gpu(adev, false);
3066 r = amdgpu_device_ip_suspend_phase1(adev);
3069 r = amdgpu_device_ip_suspend_phase2(adev);
3071 if (amdgpu_sriov_vf(adev))
3072 amdgpu_virt_release_full_gpu(adev, false);
3077 static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
3081 static enum amd_ip_block_type ip_order[] = {
3082 AMD_IP_BLOCK_TYPE_COMMON,
3083 AMD_IP_BLOCK_TYPE_GMC,
3084 AMD_IP_BLOCK_TYPE_PSP,
3085 AMD_IP_BLOCK_TYPE_IH,
3088 for (i = 0; i < adev->num_ip_blocks; i++) {
3090 struct amdgpu_ip_block *block;
3092 block = &adev->ip_blocks[i];
3093 block->status.hw = false;
3095 for (j = 0; j < ARRAY_SIZE(ip_order); j++) {
3097 if (block->version->type != ip_order[j] ||
3098 !block->status.valid)
3101 r = block->version->funcs->hw_init(adev);
3102 DRM_INFO("RE-INIT-early: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
3105 block->status.hw = true;
3112 static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
3116 static enum amd_ip_block_type ip_order[] = {
3117 AMD_IP_BLOCK_TYPE_SMC,
3118 AMD_IP_BLOCK_TYPE_DCE,
3119 AMD_IP_BLOCK_TYPE_GFX,
3120 AMD_IP_BLOCK_TYPE_SDMA,
3121 AMD_IP_BLOCK_TYPE_UVD,
3122 AMD_IP_BLOCK_TYPE_VCE,
3123 AMD_IP_BLOCK_TYPE_VCN
3126 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
3128 struct amdgpu_ip_block *block;
3130 for (j = 0; j < adev->num_ip_blocks; j++) {
3131 block = &adev->ip_blocks[j];
3133 if (block->version->type != ip_order[i] ||
3134 !block->status.valid ||
3138 if (block->version->type == AMD_IP_BLOCK_TYPE_SMC)
3139 r = block->version->funcs->resume(adev);
3141 r = block->version->funcs->hw_init(adev);
3143 DRM_INFO("RE-INIT-late: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
3146 block->status.hw = true;
3154 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
3156 * @adev: amdgpu_device pointer
3158 * First resume function for hardware IPs. The list of all the hardware
3159 * IPs that make up the asic is walked and the resume callbacks are run for
3160 * COMMON, GMC, and IH. resume puts the hardware into a functional state
3161 * after a suspend and updates the software state as necessary. This
3162 * function is also used for restoring the GPU after a GPU reset.
3163 * Returns 0 on success, negative error code on failure.
3165 static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
3169 for (i = 0; i < adev->num_ip_blocks; i++) {
3170 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
3172 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3173 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3174 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3175 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP && amdgpu_sriov_vf(adev))) {
3177 r = adev->ip_blocks[i].version->funcs->resume(adev);
3179 DRM_ERROR("resume of IP block <%s> failed %d\n",
3180 adev->ip_blocks[i].version->funcs->name, r);
3183 adev->ip_blocks[i].status.hw = true;
3191 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
3193 * @adev: amdgpu_device pointer
3195 * First resume function for hardware IPs. The list of all the hardware
3196 * IPs that make up the asic is walked and the resume callbacks are run for
3197 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
3198 * functional state after a suspend and updates the software state as
3199 * necessary. This function is also used for restoring the GPU after a GPU
3201 * Returns 0 on success, negative error code on failure.
3203 static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
3207 for (i = 0; i < adev->num_ip_blocks; i++) {
3208 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
3210 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3211 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3212 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3213 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)
3215 r = adev->ip_blocks[i].version->funcs->resume(adev);
3217 DRM_ERROR("resume of IP block <%s> failed %d\n",
3218 adev->ip_blocks[i].version->funcs->name, r);
3221 adev->ip_blocks[i].status.hw = true;
3223 if (adev->in_s0ix && adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
3224 /* disable gfxoff for IP resume. The gfxoff will be re-enabled in
3225 * amdgpu_device_resume() after IP resume.
3227 amdgpu_gfx_off_ctrl(adev, false);
3228 DRM_DEBUG("will disable gfxoff for re-initializing other blocks\n");
3237 * amdgpu_device_ip_resume - run resume for hardware IPs
3239 * @adev: amdgpu_device pointer
3241 * Main resume function for hardware IPs. The hardware IPs
3242 * are split into two resume functions because they are
3243 * are also used in in recovering from a GPU reset and some additional
3244 * steps need to be take between them. In this case (S3/S4) they are
3246 * Returns 0 on success, negative error code on failure.
3248 static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
3252 r = amdgpu_amdkfd_resume_iommu(adev);
3256 r = amdgpu_device_ip_resume_phase1(adev);
3260 r = amdgpu_device_fw_loading(adev);
3264 r = amdgpu_device_ip_resume_phase2(adev);
3270 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
3272 * @adev: amdgpu_device pointer
3274 * Query the VBIOS data tables to determine if the board supports SR-IOV.
3276 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
3278 if (amdgpu_sriov_vf(adev)) {
3279 if (adev->is_atom_fw) {
3280 if (amdgpu_atomfirmware_gpu_virtualization_supported(adev))
3281 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
3283 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
3284 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
3287 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
3288 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
3293 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
3295 * @asic_type: AMD asic type
3297 * Check if there is DC (new modesetting infrastructre) support for an asic.
3298 * returns true if DC has support, false if not.
3300 bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
3302 switch (asic_type) {
3303 #ifdef CONFIG_DRM_AMDGPU_SI
3307 /* chips with no display hardware */
3309 #if defined(CONFIG_DRM_AMD_DC)
3315 * We have systems in the wild with these ASICs that require
3316 * LVDS and VGA support which is not supported with DC.
3318 * Fallback to the non-DC driver here by default so as not to
3319 * cause regressions.
3321 #if defined(CONFIG_DRM_AMD_DC_SI)
3322 return amdgpu_dc > 0;
3331 * We have systems in the wild with these ASICs that require
3332 * VGA support which is not supported with DC.
3334 * Fallback to the non-DC driver here by default so as not to
3335 * cause regressions.
3337 return amdgpu_dc > 0;
3339 return amdgpu_dc != 0;
3343 DRM_INFO_ONCE("Display Core has been requested via kernel parameter "
3344 "but isn't supported by ASIC, ignoring\n");
3351 * amdgpu_device_has_dc_support - check if dc is supported
3353 * @adev: amdgpu_device pointer
3355 * Returns true for supported, false for not supported
3357 bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
3359 if (amdgpu_sriov_vf(adev) ||
3360 adev->enable_virtual_display ||
3361 (adev->harvest_ip_mask & AMD_HARVEST_IP_DMU_MASK))
3364 return amdgpu_device_asic_has_dc_support(adev->asic_type);
3367 static void amdgpu_device_xgmi_reset_func(struct work_struct *__work)
3369 struct amdgpu_device *adev =
3370 container_of(__work, struct amdgpu_device, xgmi_reset_work);
3371 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
3373 /* It's a bug to not have a hive within this function */
3378 * Use task barrier to synchronize all xgmi reset works across the
3379 * hive. task_barrier_enter and task_barrier_exit will block
3380 * until all the threads running the xgmi reset works reach
3381 * those points. task_barrier_full will do both blocks.
3383 if (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) {
3385 task_barrier_enter(&hive->tb);
3386 adev->asic_reset_res = amdgpu_device_baco_enter(adev_to_drm(adev));
3388 if (adev->asic_reset_res)
3391 task_barrier_exit(&hive->tb);
3392 adev->asic_reset_res = amdgpu_device_baco_exit(adev_to_drm(adev));
3394 if (adev->asic_reset_res)
3397 if (adev->mmhub.ras && adev->mmhub.ras->ras_block.hw_ops &&
3398 adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count)
3399 adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count(adev);
3402 task_barrier_full(&hive->tb);
3403 adev->asic_reset_res = amdgpu_asic_reset(adev);
3407 if (adev->asic_reset_res)
3408 DRM_WARN("ASIC reset failed with error, %d for drm dev, %s",
3409 adev->asic_reset_res, adev_to_drm(adev)->unique);
3410 amdgpu_put_xgmi_hive(hive);
3413 static int amdgpu_device_get_job_timeout_settings(struct amdgpu_device *adev)
3415 char *input = amdgpu_lockup_timeout;
3416 char *timeout_setting = NULL;
3422 * By default timeout for non compute jobs is 10000
3423 * and 60000 for compute jobs.
3424 * In SR-IOV or passthrough mode, timeout for compute
3425 * jobs are 60000 by default.
3427 adev->gfx_timeout = msecs_to_jiffies(10000);
3428 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
3429 if (amdgpu_sriov_vf(adev))
3430 adev->compute_timeout = amdgpu_sriov_is_pp_one_vf(adev) ?
3431 msecs_to_jiffies(60000) : msecs_to_jiffies(10000);
3433 adev->compute_timeout = msecs_to_jiffies(60000);
3435 if (strnlen(input, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
3436 while ((timeout_setting = strsep(&input, ",")) &&
3437 strnlen(timeout_setting, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
3438 ret = kstrtol(timeout_setting, 0, &timeout);
3445 } else if (timeout < 0) {
3446 timeout = MAX_SCHEDULE_TIMEOUT;
3447 dev_warn(adev->dev, "lockup timeout disabled");
3448 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
3450 timeout = msecs_to_jiffies(timeout);
3455 adev->gfx_timeout = timeout;
3458 adev->compute_timeout = timeout;
3461 adev->sdma_timeout = timeout;
3464 adev->video_timeout = timeout;
3471 * There is only one value specified and
3472 * it should apply to all non-compute jobs.
3475 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
3476 if (amdgpu_sriov_vf(adev) || amdgpu_passthrough(adev))
3477 adev->compute_timeout = adev->gfx_timeout;
3485 * amdgpu_device_check_iommu_direct_map - check if RAM direct mapped to GPU
3487 * @adev: amdgpu_device pointer
3489 * RAM direct mapped to GPU if IOMMU is not enabled or is pass through mode
3491 static void amdgpu_device_check_iommu_direct_map(struct amdgpu_device *adev)
3493 struct iommu_domain *domain;
3495 domain = iommu_get_domain_for_dev(adev->dev);
3496 if (!domain || domain->type == IOMMU_DOMAIN_IDENTITY)
3497 adev->ram_is_direct_mapped = true;
3500 static const struct attribute *amdgpu_dev_attributes[] = {
3501 &dev_attr_product_name.attr,
3502 &dev_attr_product_number.attr,
3503 &dev_attr_serial_number.attr,
3504 &dev_attr_pcie_replay_count.attr,
3509 * amdgpu_device_init - initialize the driver
3511 * @adev: amdgpu_device pointer
3512 * @flags: driver flags
3514 * Initializes the driver info and hw (all asics).
3515 * Returns 0 for success or an error on failure.
3516 * Called at driver startup.
3518 int amdgpu_device_init(struct amdgpu_device *adev,
3521 struct drm_device *ddev = adev_to_drm(adev);
3522 struct pci_dev *pdev = adev->pdev;
3527 adev->shutdown = false;
3528 adev->flags = flags;
3530 if (amdgpu_force_asic_type >= 0 && amdgpu_force_asic_type < CHIP_LAST)
3531 adev->asic_type = amdgpu_force_asic_type;
3533 adev->asic_type = flags & AMD_ASIC_MASK;
3535 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
3536 if (amdgpu_emu_mode == 1)
3537 adev->usec_timeout *= 10;
3538 adev->gmc.gart_size = 512 * 1024 * 1024;
3539 adev->accel_working = false;
3540 adev->num_rings = 0;
3541 RCU_INIT_POINTER(adev->gang_submit, dma_fence_get_stub());
3542 adev->mman.buffer_funcs = NULL;
3543 adev->mman.buffer_funcs_ring = NULL;
3544 adev->vm_manager.vm_pte_funcs = NULL;
3545 adev->vm_manager.vm_pte_num_scheds = 0;
3546 adev->gmc.gmc_funcs = NULL;
3547 adev->harvest_ip_mask = 0x0;
3548 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
3549 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
3551 adev->smc_rreg = &amdgpu_invalid_rreg;
3552 adev->smc_wreg = &amdgpu_invalid_wreg;
3553 adev->pcie_rreg = &amdgpu_invalid_rreg;
3554 adev->pcie_wreg = &amdgpu_invalid_wreg;
3555 adev->pciep_rreg = &amdgpu_invalid_rreg;
3556 adev->pciep_wreg = &amdgpu_invalid_wreg;
3557 adev->pcie_rreg64 = &amdgpu_invalid_rreg64;
3558 adev->pcie_wreg64 = &amdgpu_invalid_wreg64;
3559 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
3560 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
3561 adev->didt_rreg = &amdgpu_invalid_rreg;
3562 adev->didt_wreg = &amdgpu_invalid_wreg;
3563 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
3564 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
3565 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
3566 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
3568 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
3569 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
3570 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
3572 /* mutex initialization are all done here so we
3573 * can recall function without having locking issues */
3574 mutex_init(&adev->firmware.mutex);
3575 mutex_init(&adev->pm.mutex);
3576 mutex_init(&adev->gfx.gpu_clock_mutex);
3577 mutex_init(&adev->srbm_mutex);
3578 mutex_init(&adev->gfx.pipe_reserve_mutex);
3579 mutex_init(&adev->gfx.gfx_off_mutex);
3580 mutex_init(&adev->grbm_idx_mutex);
3581 mutex_init(&adev->mn_lock);
3582 mutex_init(&adev->virt.vf_errors.lock);
3583 hash_init(adev->mn_hash);
3584 mutex_init(&adev->psp.mutex);
3585 mutex_init(&adev->notifier_lock);
3586 mutex_init(&adev->pm.stable_pstate_ctx_lock);
3587 mutex_init(&adev->benchmark_mutex);
3589 amdgpu_device_init_apu_flags(adev);
3591 r = amdgpu_device_check_arguments(adev);
3595 spin_lock_init(&adev->mmio_idx_lock);
3596 spin_lock_init(&adev->smc_idx_lock);
3597 spin_lock_init(&adev->pcie_idx_lock);
3598 spin_lock_init(&adev->uvd_ctx_idx_lock);
3599 spin_lock_init(&adev->didt_idx_lock);
3600 spin_lock_init(&adev->gc_cac_idx_lock);
3601 spin_lock_init(&adev->se_cac_idx_lock);
3602 spin_lock_init(&adev->audio_endpt_idx_lock);
3603 spin_lock_init(&adev->mm_stats.lock);
3605 INIT_LIST_HEAD(&adev->shadow_list);
3606 mutex_init(&adev->shadow_list_lock);
3608 INIT_LIST_HEAD(&adev->reset_list);
3610 INIT_LIST_HEAD(&adev->ras_list);
3612 INIT_DELAYED_WORK(&adev->delayed_init_work,
3613 amdgpu_device_delayed_init_work_handler);
3614 INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work,
3615 amdgpu_device_delay_enable_gfx_off);
3617 INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
3619 adev->gfx.gfx_off_req_count = 1;
3620 adev->gfx.gfx_off_residency = 0;
3621 adev->gfx.gfx_off_entrycount = 0;
3622 adev->pm.ac_power = power_supply_is_system_supplied() > 0;
3624 atomic_set(&adev->throttling_logging_enabled, 1);
3626 * If throttling continues, logging will be performed every minute
3627 * to avoid log flooding. "-1" is subtracted since the thermal
3628 * throttling interrupt comes every second. Thus, the total logging
3629 * interval is 59 seconds(retelimited printk interval) + 1(waiting
3630 * for throttling interrupt) = 60 seconds.
3632 ratelimit_state_init(&adev->throttling_logging_rs, (60 - 1) * HZ, 1);
3633 ratelimit_set_flags(&adev->throttling_logging_rs, RATELIMIT_MSG_ON_RELEASE);
3635 /* Registers mapping */
3636 /* TODO: block userspace mapping of io register */
3637 if (adev->asic_type >= CHIP_BONAIRE) {
3638 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
3639 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
3641 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
3642 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
3645 for (i = 0; i < AMD_IP_BLOCK_TYPE_NUM; i++)
3646 atomic_set(&adev->pm.pwr_state[i], POWER_STATE_UNKNOWN);
3648 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
3649 if (adev->rmmio == NULL) {
3652 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
3653 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
3655 amdgpu_device_get_pcie_info(adev);
3658 DRM_INFO("MCBP is enabled\n");
3661 * Reset domain needs to be present early, before XGMI hive discovered
3662 * (if any) and intitialized to use reset sem and in_gpu reset flag
3663 * early on during init and before calling to RREG32.
3665 adev->reset_domain = amdgpu_reset_create_reset_domain(SINGLE_DEVICE, "amdgpu-reset-dev");
3666 if (!adev->reset_domain)
3669 /* detect hw virtualization here */
3670 amdgpu_detect_virtualization(adev);
3672 r = amdgpu_device_get_job_timeout_settings(adev);
3674 dev_err(adev->dev, "invalid lockup_timeout parameter syntax\n");
3678 /* early init functions */
3679 r = amdgpu_device_ip_early_init(adev);
3683 /* Get rid of things like offb */
3684 r = drm_aperture_remove_conflicting_pci_framebuffers(adev->pdev, &amdgpu_kms_driver);
3688 /* Enable TMZ based on IP_VERSION */
3689 amdgpu_gmc_tmz_set(adev);
3691 amdgpu_gmc_noretry_set(adev);
3692 /* Need to get xgmi info early to decide the reset behavior*/
3693 if (adev->gmc.xgmi.supported) {
3694 r = adev->gfxhub.funcs->get_xgmi_info(adev);
3699 /* enable PCIE atomic ops */
3700 if (amdgpu_sriov_vf(adev))
3701 adev->have_atomics_support = ((struct amd_sriov_msg_pf2vf_info *)
3702 adev->virt.fw_reserve.p_pf2vf)->pcie_atomic_ops_support_flags ==
3703 (PCI_EXP_DEVCAP2_ATOMIC_COMP32 | PCI_EXP_DEVCAP2_ATOMIC_COMP64);
3705 adev->have_atomics_support =
3706 !pci_enable_atomic_ops_to_root(adev->pdev,
3707 PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
3708 PCI_EXP_DEVCAP2_ATOMIC_COMP64);
3709 if (!adev->have_atomics_support)
3710 dev_info(adev->dev, "PCIE atomic ops is not supported\n");
3712 /* doorbell bar mapping and doorbell index init*/
3713 amdgpu_device_doorbell_init(adev);
3715 if (amdgpu_emu_mode == 1) {
3716 /* post the asic on emulation mode */
3717 emu_soc_asic_init(adev);
3718 goto fence_driver_init;
3721 amdgpu_reset_init(adev);
3723 /* detect if we are with an SRIOV vbios */
3724 amdgpu_device_detect_sriov_bios(adev);
3726 /* check if we need to reset the asic
3727 * E.g., driver was not cleanly unloaded previously, etc.
3729 if (!amdgpu_sriov_vf(adev) && amdgpu_asic_need_reset_on_init(adev)) {
3730 if (adev->gmc.xgmi.num_physical_nodes) {
3731 dev_info(adev->dev, "Pending hive reset.\n");
3732 adev->gmc.xgmi.pending_reset = true;
3733 /* Only need to init necessary block for SMU to handle the reset */
3734 for (i = 0; i < adev->num_ip_blocks; i++) {
3735 if (!adev->ip_blocks[i].status.valid)
3737 if (!(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3738 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3739 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3740 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC)) {
3741 DRM_DEBUG("IP %s disabled for hw_init.\n",
3742 adev->ip_blocks[i].version->funcs->name);
3743 adev->ip_blocks[i].status.hw = true;
3747 r = amdgpu_asic_reset(adev);
3749 dev_err(adev->dev, "asic reset on init failed\n");
3755 pci_enable_pcie_error_reporting(adev->pdev);
3757 /* Post card if necessary */
3758 if (amdgpu_device_need_post(adev)) {
3760 dev_err(adev->dev, "no vBIOS found\n");
3764 DRM_INFO("GPU posting now...\n");
3765 r = amdgpu_device_asic_init(adev);
3767 dev_err(adev->dev, "gpu post error!\n");
3772 if (adev->is_atom_fw) {
3773 /* Initialize clocks */
3774 r = amdgpu_atomfirmware_get_clock_info(adev);
3776 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
3777 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3781 /* Initialize clocks */
3782 r = amdgpu_atombios_get_clock_info(adev);
3784 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
3785 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3788 /* init i2c buses */
3789 if (!amdgpu_device_has_dc_support(adev))
3790 amdgpu_atombios_i2c_init(adev);
3795 r = amdgpu_fence_driver_sw_init(adev);
3797 dev_err(adev->dev, "amdgpu_fence_driver_sw_init failed\n");
3798 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
3802 /* init the mode config */
3803 drm_mode_config_init(adev_to_drm(adev));
3805 r = amdgpu_device_ip_init(adev);
3807 /* failed in exclusive mode due to timeout */
3808 if (amdgpu_sriov_vf(adev) &&
3809 !amdgpu_sriov_runtime(adev) &&
3810 amdgpu_virt_mmio_blocked(adev) &&
3811 !amdgpu_virt_wait_reset(adev)) {
3812 dev_err(adev->dev, "VF exclusive mode timeout\n");
3813 /* Don't send request since VF is inactive. */
3814 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
3815 adev->virt.ops = NULL;
3817 goto release_ras_con;
3819 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
3820 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
3821 goto release_ras_con;
3824 amdgpu_fence_driver_hw_init(adev);
3827 "SE %d, SH per SE %d, CU per SH %d, active_cu_number %d\n",
3828 adev->gfx.config.max_shader_engines,
3829 adev->gfx.config.max_sh_per_se,
3830 adev->gfx.config.max_cu_per_sh,
3831 adev->gfx.cu_info.number);
3833 adev->accel_working = true;
3835 amdgpu_vm_check_compute_bug(adev);
3837 /* Initialize the buffer migration limit. */
3838 if (amdgpu_moverate >= 0)
3839 max_MBps = amdgpu_moverate;
3841 max_MBps = 8; /* Allow 8 MB/s. */
3842 /* Get a log2 for easy divisions. */
3843 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
3845 r = amdgpu_pm_sysfs_init(adev);
3847 adev->pm_sysfs_en = false;
3848 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
3850 adev->pm_sysfs_en = true;
3852 r = amdgpu_ucode_sysfs_init(adev);
3854 adev->ucode_sysfs_en = false;
3855 DRM_ERROR("Creating firmware sysfs failed (%d).\n", r);
3857 adev->ucode_sysfs_en = true;
3859 r = amdgpu_psp_sysfs_init(adev);
3861 adev->psp_sysfs_en = false;
3862 if (!amdgpu_sriov_vf(adev))
3863 DRM_ERROR("Creating psp sysfs failed\n");
3865 adev->psp_sysfs_en = true;
3868 * Register gpu instance before amdgpu_device_enable_mgpu_fan_boost.
3869 * Otherwise the mgpu fan boost feature will be skipped due to the
3870 * gpu instance is counted less.
3872 amdgpu_register_gpu_instance(adev);
3874 /* enable clockgating, etc. after ib tests, etc. since some blocks require
3875 * explicit gating rather than handling it automatically.
3877 if (!adev->gmc.xgmi.pending_reset) {
3878 r = amdgpu_device_ip_late_init(adev);
3880 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
3881 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
3882 goto release_ras_con;
3885 amdgpu_ras_resume(adev);
3886 queue_delayed_work(system_wq, &adev->delayed_init_work,
3887 msecs_to_jiffies(AMDGPU_RESUME_MS));
3890 if (amdgpu_sriov_vf(adev))
3891 flush_delayed_work(&adev->delayed_init_work);
3893 r = sysfs_create_files(&adev->dev->kobj, amdgpu_dev_attributes);
3895 dev_err(adev->dev, "Could not create amdgpu device attr\n");
3897 if (IS_ENABLED(CONFIG_PERF_EVENTS))
3898 r = amdgpu_pmu_init(adev);
3900 dev_err(adev->dev, "amdgpu_pmu_init failed\n");
3902 /* Have stored pci confspace at hand for restore in sudden PCI error */
3903 if (amdgpu_device_cache_pci_state(adev->pdev))
3904 pci_restore_state(pdev);
3906 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
3907 /* this will fail for cards that aren't VGA class devices, just
3909 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
3910 vga_client_register(adev->pdev, amdgpu_device_vga_set_decode);
3912 if (amdgpu_device_supports_px(ddev)) {
3914 vga_switcheroo_register_client(adev->pdev,
3915 &amdgpu_switcheroo_ops, px);
3916 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
3919 if (adev->gmc.xgmi.pending_reset)
3920 queue_delayed_work(system_wq, &mgpu_info.delayed_reset_work,
3921 msecs_to_jiffies(AMDGPU_RESUME_MS));
3923 amdgpu_device_check_iommu_direct_map(adev);
3928 amdgpu_release_ras_context(adev);
3931 amdgpu_vf_error_trans_all(adev);
3936 static void amdgpu_device_unmap_mmio(struct amdgpu_device *adev)
3939 /* Clear all CPU mappings pointing to this device */
3940 unmap_mapping_range(adev->ddev.anon_inode->i_mapping, 0, 0, 1);
3942 /* Unmap all mapped bars - Doorbell, registers and VRAM */
3943 amdgpu_device_doorbell_fini(adev);
3945 iounmap(adev->rmmio);
3947 if (adev->mman.aper_base_kaddr)
3948 iounmap(adev->mman.aper_base_kaddr);
3949 adev->mman.aper_base_kaddr = NULL;
3951 /* Memory manager related */
3952 if (!adev->gmc.xgmi.connected_to_cpu) {
3953 arch_phys_wc_del(adev->gmc.vram_mtrr);
3954 arch_io_free_memtype_wc(adev->gmc.aper_base, adev->gmc.aper_size);
3959 * amdgpu_device_fini_hw - tear down the driver
3961 * @adev: amdgpu_device pointer
3963 * Tear down the driver info (all asics).
3964 * Called at driver shutdown.
3966 void amdgpu_device_fini_hw(struct amdgpu_device *adev)
3968 dev_info(adev->dev, "amdgpu: finishing device.\n");
3969 flush_delayed_work(&adev->delayed_init_work);
3970 adev->shutdown = true;
3972 /* make sure IB test finished before entering exclusive mode
3973 * to avoid preemption on IB test
3975 if (amdgpu_sriov_vf(adev)) {
3976 amdgpu_virt_request_full_gpu(adev, false);
3977 amdgpu_virt_fini_data_exchange(adev);
3980 /* disable all interrupts */
3981 amdgpu_irq_disable_all(adev);
3982 if (adev->mode_info.mode_config_initialized){
3983 if (!drm_drv_uses_atomic_modeset(adev_to_drm(adev)))
3984 drm_helper_force_disable_all(adev_to_drm(adev));
3986 drm_atomic_helper_shutdown(adev_to_drm(adev));
3988 amdgpu_fence_driver_hw_fini(adev);
3990 if (adev->mman.initialized) {
3991 flush_delayed_work(&adev->mman.bdev.wq);
3992 ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
3995 if (adev->pm_sysfs_en)
3996 amdgpu_pm_sysfs_fini(adev);
3997 if (adev->ucode_sysfs_en)
3998 amdgpu_ucode_sysfs_fini(adev);
3999 if (adev->psp_sysfs_en)
4000 amdgpu_psp_sysfs_fini(adev);
4001 sysfs_remove_files(&adev->dev->kobj, amdgpu_dev_attributes);
4003 /* disable ras feature must before hw fini */
4004 amdgpu_ras_pre_fini(adev);
4006 amdgpu_device_ip_fini_early(adev);
4008 amdgpu_irq_fini_hw(adev);
4010 if (adev->mman.initialized)
4011 ttm_device_clear_dma_mappings(&adev->mman.bdev);
4013 amdgpu_gart_dummy_page_fini(adev);
4015 amdgpu_device_unmap_mmio(adev);
4019 void amdgpu_device_fini_sw(struct amdgpu_device *adev)
4023 amdgpu_fence_driver_sw_fini(adev);
4024 amdgpu_device_ip_fini(adev);
4025 release_firmware(adev->firmware.gpu_info_fw);
4026 adev->firmware.gpu_info_fw = NULL;
4027 adev->accel_working = false;
4028 dma_fence_put(rcu_dereference_protected(adev->gang_submit, true));
4030 amdgpu_reset_fini(adev);
4032 /* free i2c buses */
4033 if (!amdgpu_device_has_dc_support(adev))
4034 amdgpu_i2c_fini(adev);
4036 if (amdgpu_emu_mode != 1)
4037 amdgpu_atombios_fini(adev);
4041 if (amdgpu_device_supports_px(adev_to_drm(adev))) {
4042 vga_switcheroo_unregister_client(adev->pdev);
4043 vga_switcheroo_fini_domain_pm_ops(adev->dev);
4045 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
4046 vga_client_unregister(adev->pdev);
4048 if (drm_dev_enter(adev_to_drm(adev), &idx)) {
4050 iounmap(adev->rmmio);
4052 amdgpu_device_doorbell_fini(adev);
4056 if (IS_ENABLED(CONFIG_PERF_EVENTS))
4057 amdgpu_pmu_fini(adev);
4058 if (adev->mman.discovery_bin)
4059 amdgpu_discovery_fini(adev);
4061 amdgpu_reset_put_reset_domain(adev->reset_domain);
4062 adev->reset_domain = NULL;
4064 kfree(adev->pci_state);
4069 * amdgpu_device_evict_resources - evict device resources
4070 * @adev: amdgpu device object
4072 * Evicts all ttm device resources(vram BOs, gart table) from the lru list
4073 * of the vram memory type. Mainly used for evicting device resources
4077 static int amdgpu_device_evict_resources(struct amdgpu_device *adev)
4081 /* No need to evict vram on APUs for suspend to ram or s2idle */
4082 if ((adev->in_s3 || adev->in_s0ix) && (adev->flags & AMD_IS_APU))
4085 ret = amdgpu_ttm_evict_resources(adev, TTM_PL_VRAM);
4087 DRM_WARN("evicting device resources failed\n");
4095 * amdgpu_device_suspend - initiate device suspend
4097 * @dev: drm dev pointer
4098 * @fbcon : notify the fbdev of suspend
4100 * Puts the hw in the suspend state (all asics).
4101 * Returns 0 for success or an error on failure.
4102 * Called at driver suspend.
4104 int amdgpu_device_suspend(struct drm_device *dev, bool fbcon)
4106 struct amdgpu_device *adev = drm_to_adev(dev);
4109 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
4112 adev->in_suspend = true;
4114 if (amdgpu_sriov_vf(adev)) {
4115 amdgpu_virt_fini_data_exchange(adev);
4116 r = amdgpu_virt_request_full_gpu(adev, false);
4121 if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DEV_D3))
4122 DRM_WARN("smart shift update failed\n");
4124 drm_kms_helper_poll_disable(dev);
4127 drm_fb_helper_set_suspend_unlocked(adev_to_drm(adev)->fb_helper, true);
4129 cancel_delayed_work_sync(&adev->delayed_init_work);
4131 amdgpu_ras_suspend(adev);
4133 amdgpu_device_ip_suspend_phase1(adev);
4136 amdgpu_amdkfd_suspend(adev, adev->in_runpm);
4138 r = amdgpu_device_evict_resources(adev);
4142 amdgpu_fence_driver_hw_fini(adev);
4144 amdgpu_device_ip_suspend_phase2(adev);
4146 if (amdgpu_sriov_vf(adev))
4147 amdgpu_virt_release_full_gpu(adev, false);
4153 * amdgpu_device_resume - initiate device resume
4155 * @dev: drm dev pointer
4156 * @fbcon : notify the fbdev of resume
4158 * Bring the hw back to operating state (all asics).
4159 * Returns 0 for success or an error on failure.
4160 * Called at driver resume.
4162 int amdgpu_device_resume(struct drm_device *dev, bool fbcon)
4164 struct amdgpu_device *adev = drm_to_adev(dev);
4167 if (amdgpu_sriov_vf(adev)) {
4168 r = amdgpu_virt_request_full_gpu(adev, true);
4173 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
4177 amdgpu_dpm_gfx_state_change(adev, sGpuChangeState_D0Entry);
4180 if (amdgpu_device_need_post(adev)) {
4181 r = amdgpu_device_asic_init(adev);
4183 dev_err(adev->dev, "amdgpu asic init failed\n");
4186 r = amdgpu_device_ip_resume(adev);
4188 /* no matter what r is, always need to properly release full GPU */
4189 if (amdgpu_sriov_vf(adev)) {
4190 amdgpu_virt_init_data_exchange(adev);
4191 amdgpu_virt_release_full_gpu(adev, true);
4195 dev_err(adev->dev, "amdgpu_device_ip_resume failed (%d).\n", r);
4198 amdgpu_fence_driver_hw_init(adev);
4200 r = amdgpu_device_ip_late_init(adev);
4204 queue_delayed_work(system_wq, &adev->delayed_init_work,
4205 msecs_to_jiffies(AMDGPU_RESUME_MS));
4207 if (!adev->in_s0ix) {
4208 r = amdgpu_amdkfd_resume(adev, adev->in_runpm);
4213 /* Make sure IB tests flushed */
4214 flush_delayed_work(&adev->delayed_init_work);
4216 if (adev->in_s0ix) {
4217 /* re-enable gfxoff after IP resume. This re-enables gfxoff after
4218 * it was disabled for IP resume in amdgpu_device_ip_resume_phase2().
4220 amdgpu_gfx_off_ctrl(adev, true);
4221 DRM_DEBUG("will enable gfxoff for the mission mode\n");
4224 drm_fb_helper_set_suspend_unlocked(adev_to_drm(adev)->fb_helper, false);
4226 drm_kms_helper_poll_enable(dev);
4228 amdgpu_ras_resume(adev);
4231 * Most of the connector probing functions try to acquire runtime pm
4232 * refs to ensure that the GPU is powered on when connector polling is
4233 * performed. Since we're calling this from a runtime PM callback,
4234 * trying to acquire rpm refs will cause us to deadlock.
4236 * Since we're guaranteed to be holding the rpm lock, it's safe to
4237 * temporarily disable the rpm helpers so this doesn't deadlock us.
4240 dev->dev->power.disable_depth++;
4242 if (!amdgpu_device_has_dc_support(adev))
4243 drm_helper_hpd_irq_event(dev);
4245 drm_kms_helper_hotplug_event(dev);
4247 dev->dev->power.disable_depth--;
4249 adev->in_suspend = false;
4251 if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DEV_D0))
4252 DRM_WARN("smart shift update failed\n");
4258 * amdgpu_device_ip_check_soft_reset - did soft reset succeed
4260 * @adev: amdgpu_device pointer
4262 * The list of all the hardware IPs that make up the asic is walked and
4263 * the check_soft_reset callbacks are run. check_soft_reset determines
4264 * if the asic is still hung or not.
4265 * Returns true if any of the IPs are still in a hung state, false if not.
4267 static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
4270 bool asic_hang = false;
4272 if (amdgpu_sriov_vf(adev))
4275 if (amdgpu_asic_need_full_reset(adev))
4278 for (i = 0; i < adev->num_ip_blocks; i++) {
4279 if (!adev->ip_blocks[i].status.valid)
4281 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
4282 adev->ip_blocks[i].status.hang =
4283 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
4284 if (adev->ip_blocks[i].status.hang) {
4285 dev_info(adev->dev, "IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
4293 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
4295 * @adev: amdgpu_device pointer
4297 * The list of all the hardware IPs that make up the asic is walked and the
4298 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
4299 * handles any IP specific hardware or software state changes that are
4300 * necessary for a soft reset to succeed.
4301 * Returns 0 on success, negative error code on failure.
4303 static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
4307 for (i = 0; i < adev->num_ip_blocks; i++) {
4308 if (!adev->ip_blocks[i].status.valid)
4310 if (adev->ip_blocks[i].status.hang &&
4311 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
4312 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
4322 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
4324 * @adev: amdgpu_device pointer
4326 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
4327 * reset is necessary to recover.
4328 * Returns true if a full asic reset is required, false if not.
4330 static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
4334 if (amdgpu_asic_need_full_reset(adev))
4337 for (i = 0; i < adev->num_ip_blocks; i++) {
4338 if (!adev->ip_blocks[i].status.valid)
4340 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
4341 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
4342 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
4343 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
4344 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
4345 if (adev->ip_blocks[i].status.hang) {
4346 dev_info(adev->dev, "Some block need full reset!\n");
4355 * amdgpu_device_ip_soft_reset - do a soft reset
4357 * @adev: amdgpu_device pointer
4359 * The list of all the hardware IPs that make up the asic is walked and the
4360 * soft_reset callbacks are run if the block is hung. soft_reset handles any
4361 * IP specific hardware or software state changes that are necessary to soft
4363 * Returns 0 on success, negative error code on failure.
4365 static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
4369 for (i = 0; i < adev->num_ip_blocks; i++) {
4370 if (!adev->ip_blocks[i].status.valid)
4372 if (adev->ip_blocks[i].status.hang &&
4373 adev->ip_blocks[i].version->funcs->soft_reset) {
4374 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
4384 * amdgpu_device_ip_post_soft_reset - clean up from soft reset
4386 * @adev: amdgpu_device pointer
4388 * The list of all the hardware IPs that make up the asic is walked and the
4389 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
4390 * handles any IP specific hardware or software state changes that are
4391 * necessary after the IP has been soft reset.
4392 * Returns 0 on success, negative error code on failure.
4394 static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
4398 for (i = 0; i < adev->num_ip_blocks; i++) {
4399 if (!adev->ip_blocks[i].status.valid)
4401 if (adev->ip_blocks[i].status.hang &&
4402 adev->ip_blocks[i].version->funcs->post_soft_reset)
4403 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
4412 * amdgpu_device_recover_vram - Recover some VRAM contents
4414 * @adev: amdgpu_device pointer
4416 * Restores the contents of VRAM buffers from the shadows in GTT. Used to
4417 * restore things like GPUVM page tables after a GPU reset where
4418 * the contents of VRAM might be lost.
4421 * 0 on success, negative error code on failure.
4423 static int amdgpu_device_recover_vram(struct amdgpu_device *adev)
4425 struct dma_fence *fence = NULL, *next = NULL;
4426 struct amdgpu_bo *shadow;
4427 struct amdgpu_bo_vm *vmbo;
4430 if (amdgpu_sriov_runtime(adev))
4431 tmo = msecs_to_jiffies(8000);
4433 tmo = msecs_to_jiffies(100);
4435 dev_info(adev->dev, "recover vram bo from shadow start\n");
4436 mutex_lock(&adev->shadow_list_lock);
4437 list_for_each_entry(vmbo, &adev->shadow_list, shadow_list) {
4439 /* No need to recover an evicted BO */
4440 if (shadow->tbo.resource->mem_type != TTM_PL_TT ||
4441 shadow->tbo.resource->start == AMDGPU_BO_INVALID_OFFSET ||
4442 shadow->parent->tbo.resource->mem_type != TTM_PL_VRAM)
4445 r = amdgpu_bo_restore_shadow(shadow, &next);
4450 tmo = dma_fence_wait_timeout(fence, false, tmo);
4451 dma_fence_put(fence);
4456 } else if (tmo < 0) {
4464 mutex_unlock(&adev->shadow_list_lock);
4467 tmo = dma_fence_wait_timeout(fence, false, tmo);
4468 dma_fence_put(fence);
4470 if (r < 0 || tmo <= 0) {
4471 dev_err(adev->dev, "recover vram bo from shadow failed, r is %ld, tmo is %ld\n", r, tmo);
4475 dev_info(adev->dev, "recover vram bo from shadow done\n");
4481 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
4483 * @adev: amdgpu_device pointer
4484 * @from_hypervisor: request from hypervisor
4486 * do VF FLR and reinitialize Asic
4487 * return 0 means succeeded otherwise failed
4489 static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
4490 bool from_hypervisor)
4493 struct amdgpu_hive_info *hive = NULL;
4494 int retry_limit = 0;
4497 amdgpu_amdkfd_pre_reset(adev);
4499 if (from_hypervisor)
4500 r = amdgpu_virt_request_full_gpu(adev, true);
4502 r = amdgpu_virt_reset_gpu(adev);
4506 /* Resume IP prior to SMC */
4507 r = amdgpu_device_ip_reinit_early_sriov(adev);
4511 amdgpu_virt_init_data_exchange(adev);
4513 r = amdgpu_device_fw_loading(adev);
4517 /* now we are okay to resume SMC/CP/SDMA */
4518 r = amdgpu_device_ip_reinit_late_sriov(adev);
4522 hive = amdgpu_get_xgmi_hive(adev);
4523 /* Update PSP FW topology after reset */
4524 if (hive && adev->gmc.xgmi.num_physical_nodes > 1)
4525 r = amdgpu_xgmi_update_topology(hive, adev);
4528 amdgpu_put_xgmi_hive(hive);
4531 amdgpu_irq_gpu_reset_resume_helper(adev);
4532 r = amdgpu_ib_ring_tests(adev);
4534 amdgpu_amdkfd_post_reset(adev);
4538 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
4539 amdgpu_inc_vram_lost(adev);
4540 r = amdgpu_device_recover_vram(adev);
4542 amdgpu_virt_release_full_gpu(adev, true);
4544 if (AMDGPU_RETRY_SRIOV_RESET(r)) {
4545 if (retry_limit < AMDGPU_MAX_RETRY_LIMIT) {
4549 DRM_ERROR("GPU reset retry is beyond the retry limit\n");
4556 * amdgpu_device_has_job_running - check if there is any job in mirror list
4558 * @adev: amdgpu_device pointer
4560 * check if there is any job in mirror list
4562 bool amdgpu_device_has_job_running(struct amdgpu_device *adev)
4565 struct drm_sched_job *job;
4567 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4568 struct amdgpu_ring *ring = adev->rings[i];
4570 if (!ring || !ring->sched.thread)
4573 spin_lock(&ring->sched.job_list_lock);
4574 job = list_first_entry_or_null(&ring->sched.pending_list,
4575 struct drm_sched_job, list);
4576 spin_unlock(&ring->sched.job_list_lock);
4584 * amdgpu_device_should_recover_gpu - check if we should try GPU recovery
4586 * @adev: amdgpu_device pointer
4588 * Check amdgpu_gpu_recovery and SRIOV status to see if we should try to recover
4591 bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev)
4594 if (amdgpu_gpu_recovery == 0)
4597 if (!amdgpu_device_ip_check_soft_reset(adev)) {
4598 dev_info(adev->dev,"Timeout, but no hardware hang detected.\n");
4602 if (amdgpu_sriov_vf(adev))
4605 if (amdgpu_gpu_recovery == -1) {
4606 switch (adev->asic_type) {
4607 #ifdef CONFIG_DRM_AMDGPU_SI
4614 #ifdef CONFIG_DRM_AMDGPU_CIK
4621 case CHIP_CYAN_SKILLFISH:
4631 dev_info(adev->dev, "GPU recovery disabled.\n");
4635 int amdgpu_device_mode1_reset(struct amdgpu_device *adev)
4640 amdgpu_atombios_scratch_regs_engine_hung(adev, true);
4642 dev_info(adev->dev, "GPU mode1 reset\n");
4645 pci_clear_master(adev->pdev);
4647 amdgpu_device_cache_pci_state(adev->pdev);
4649 if (amdgpu_dpm_is_mode1_reset_supported(adev)) {
4650 dev_info(adev->dev, "GPU smu mode1 reset\n");
4651 ret = amdgpu_dpm_mode1_reset(adev);
4653 dev_info(adev->dev, "GPU psp mode1 reset\n");
4654 ret = psp_gpu_reset(adev);
4658 dev_err(adev->dev, "GPU mode1 reset failed\n");
4660 amdgpu_device_load_pci_state(adev->pdev);
4662 /* wait for asic to come out of reset */
4663 for (i = 0; i < adev->usec_timeout; i++) {
4664 u32 memsize = adev->nbio.funcs->get_memsize(adev);
4666 if (memsize != 0xffffffff)
4671 amdgpu_atombios_scratch_regs_engine_hung(adev, false);
4675 int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
4676 struct amdgpu_reset_context *reset_context)
4679 struct amdgpu_job *job = NULL;
4680 bool need_full_reset =
4681 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4683 if (reset_context->reset_req_dev == adev)
4684 job = reset_context->job;
4686 if (amdgpu_sriov_vf(adev)) {
4687 /* stop the data exchange thread */
4688 amdgpu_virt_fini_data_exchange(adev);
4691 amdgpu_fence_driver_isr_toggle(adev, true);
4693 /* block all schedulers and reset given job's ring */
4694 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4695 struct amdgpu_ring *ring = adev->rings[i];
4697 if (!ring || !ring->sched.thread)
4700 /*clear job fence from fence drv to avoid force_completion
4701 *leave NULL and vm flush fence in fence drv */
4702 amdgpu_fence_driver_clear_job_fences(ring);
4704 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
4705 amdgpu_fence_driver_force_completion(ring);
4708 amdgpu_fence_driver_isr_toggle(adev, false);
4711 drm_sched_increase_karma(&job->base);
4713 r = amdgpu_reset_prepare_hwcontext(adev, reset_context);
4714 /* If reset handler not implemented, continue; otherwise return */
4720 /* Don't suspend on bare metal if we are not going to HW reset the ASIC */
4721 if (!amdgpu_sriov_vf(adev)) {
4723 if (!need_full_reset)
4724 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
4726 if (!need_full_reset && amdgpu_gpu_recovery) {
4727 amdgpu_device_ip_pre_soft_reset(adev);
4728 r = amdgpu_device_ip_soft_reset(adev);
4729 amdgpu_device_ip_post_soft_reset(adev);
4730 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
4731 dev_info(adev->dev, "soft reset failed, will fallback to full reset!\n");
4732 need_full_reset = true;
4736 if (need_full_reset)
4737 r = amdgpu_device_ip_suspend(adev);
4738 if (need_full_reset)
4739 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4741 clear_bit(AMDGPU_NEED_FULL_RESET,
4742 &reset_context->flags);
4748 static int amdgpu_reset_reg_dumps(struct amdgpu_device *adev)
4752 lockdep_assert_held(&adev->reset_domain->sem);
4754 for (i = 0; i < adev->num_regs; i++) {
4755 adev->reset_dump_reg_value[i] = RREG32(adev->reset_dump_reg_list[i]);
4756 trace_amdgpu_reset_reg_dumps(adev->reset_dump_reg_list[i],
4757 adev->reset_dump_reg_value[i]);
4763 #ifdef CONFIG_DEV_COREDUMP
4764 static ssize_t amdgpu_devcoredump_read(char *buffer, loff_t offset,
4765 size_t count, void *data, size_t datalen)
4767 struct drm_printer p;
4768 struct amdgpu_device *adev = data;
4769 struct drm_print_iterator iter;
4774 iter.start = offset;
4775 iter.remain = count;
4777 p = drm_coredump_printer(&iter);
4779 drm_printf(&p, "**** AMDGPU Device Coredump ****\n");
4780 drm_printf(&p, "kernel: " UTS_RELEASE "\n");
4781 drm_printf(&p, "module: " KBUILD_MODNAME "\n");
4782 drm_printf(&p, "time: %lld.%09ld\n", adev->reset_time.tv_sec, adev->reset_time.tv_nsec);
4783 if (adev->reset_task_info.pid)
4784 drm_printf(&p, "process_name: %s PID: %d\n",
4785 adev->reset_task_info.process_name,
4786 adev->reset_task_info.pid);
4788 if (adev->reset_vram_lost)
4789 drm_printf(&p, "VRAM is lost due to GPU reset!\n");
4790 if (adev->num_regs) {
4791 drm_printf(&p, "AMDGPU register dumps:\nOffset: Value:\n");
4793 for (i = 0; i < adev->num_regs; i++)
4794 drm_printf(&p, "0x%08x: 0x%08x\n",
4795 adev->reset_dump_reg_list[i],
4796 adev->reset_dump_reg_value[i]);
4799 return count - iter.remain;
4802 static void amdgpu_devcoredump_free(void *data)
4806 static void amdgpu_reset_capture_coredumpm(struct amdgpu_device *adev)
4808 struct drm_device *dev = adev_to_drm(adev);
4810 ktime_get_ts64(&adev->reset_time);
4811 dev_coredumpm(dev->dev, THIS_MODULE, adev, 0, GFP_KERNEL,
4812 amdgpu_devcoredump_read, amdgpu_devcoredump_free);
4816 int amdgpu_do_asic_reset(struct list_head *device_list_handle,
4817 struct amdgpu_reset_context *reset_context)
4819 struct amdgpu_device *tmp_adev = NULL;
4820 bool need_full_reset, skip_hw_reset, vram_lost = false;
4822 bool gpu_reset_for_dev_remove = 0;
4824 /* Try reset handler method first */
4825 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
4827 amdgpu_reset_reg_dumps(tmp_adev);
4829 reset_context->reset_device_list = device_list_handle;
4830 r = amdgpu_reset_perform_reset(tmp_adev, reset_context);
4831 /* If reset handler not implemented, continue; otherwise return */
4837 /* Reset handler not implemented, use the default method */
4839 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4840 skip_hw_reset = test_bit(AMDGPU_SKIP_HW_RESET, &reset_context->flags);
4842 gpu_reset_for_dev_remove =
4843 test_bit(AMDGPU_RESET_FOR_DEVICE_REMOVE, &reset_context->flags) &&
4844 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4847 * ASIC reset has to be done on all XGMI hive nodes ASAP
4848 * to allow proper links negotiation in FW (within 1 sec)
4850 if (!skip_hw_reset && need_full_reset) {
4851 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4852 /* For XGMI run all resets in parallel to speed up the process */
4853 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
4854 tmp_adev->gmc.xgmi.pending_reset = false;
4855 if (!queue_work(system_unbound_wq, &tmp_adev->xgmi_reset_work))
4858 r = amdgpu_asic_reset(tmp_adev);
4861 dev_err(tmp_adev->dev, "ASIC reset failed with error, %d for drm dev, %s",
4862 r, adev_to_drm(tmp_adev)->unique);
4867 /* For XGMI wait for all resets to complete before proceed */
4869 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4870 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
4871 flush_work(&tmp_adev->xgmi_reset_work);
4872 r = tmp_adev->asic_reset_res;
4880 if (!r && amdgpu_ras_intr_triggered()) {
4881 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4882 if (tmp_adev->mmhub.ras && tmp_adev->mmhub.ras->ras_block.hw_ops &&
4883 tmp_adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count)
4884 tmp_adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count(tmp_adev);
4887 amdgpu_ras_intr_cleared();
4890 /* Since the mode1 reset affects base ip blocks, the
4891 * phase1 ip blocks need to be resumed. Otherwise there
4892 * will be a BIOS signature error and the psp bootloader
4893 * can't load kdb on the next amdgpu install.
4895 if (gpu_reset_for_dev_remove) {
4896 list_for_each_entry(tmp_adev, device_list_handle, reset_list)
4897 amdgpu_device_ip_resume_phase1(tmp_adev);
4902 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4903 if (need_full_reset) {
4905 r = amdgpu_device_asic_init(tmp_adev);
4907 dev_warn(tmp_adev->dev, "asic atom init failed!");
4909 dev_info(tmp_adev->dev, "GPU reset succeeded, trying to resume\n");
4910 r = amdgpu_amdkfd_resume_iommu(tmp_adev);
4914 r = amdgpu_device_ip_resume_phase1(tmp_adev);
4918 vram_lost = amdgpu_device_check_vram_lost(tmp_adev);
4919 #ifdef CONFIG_DEV_COREDUMP
4920 tmp_adev->reset_vram_lost = vram_lost;
4921 memset(&tmp_adev->reset_task_info, 0,
4922 sizeof(tmp_adev->reset_task_info));
4923 if (reset_context->job && reset_context->job->vm)
4924 tmp_adev->reset_task_info =
4925 reset_context->job->vm->task_info;
4926 amdgpu_reset_capture_coredumpm(tmp_adev);
4929 DRM_INFO("VRAM is lost due to GPU reset!\n");
4930 amdgpu_inc_vram_lost(tmp_adev);
4933 r = amdgpu_device_fw_loading(tmp_adev);
4937 r = amdgpu_device_ip_resume_phase2(tmp_adev);
4942 amdgpu_device_fill_reset_magic(tmp_adev);
4945 * Add this ASIC as tracked as reset was already
4946 * complete successfully.
4948 amdgpu_register_gpu_instance(tmp_adev);
4950 if (!reset_context->hive &&
4951 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
4952 amdgpu_xgmi_add_device(tmp_adev);
4954 r = amdgpu_device_ip_late_init(tmp_adev);
4958 drm_fb_helper_set_suspend_unlocked(adev_to_drm(tmp_adev)->fb_helper, false);
4961 * The GPU enters bad state once faulty pages
4962 * by ECC has reached the threshold, and ras
4963 * recovery is scheduled next. So add one check
4964 * here to break recovery if it indeed exceeds
4965 * bad page threshold, and remind user to
4966 * retire this GPU or setting one bigger
4967 * bad_page_threshold value to fix this once
4968 * probing driver again.
4970 if (!amdgpu_ras_eeprom_check_err_threshold(tmp_adev)) {
4972 amdgpu_ras_resume(tmp_adev);
4978 /* Update PSP FW topology after reset */
4979 if (reset_context->hive &&
4980 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
4981 r = amdgpu_xgmi_update_topology(
4982 reset_context->hive, tmp_adev);
4988 amdgpu_irq_gpu_reset_resume_helper(tmp_adev);
4989 r = amdgpu_ib_ring_tests(tmp_adev);
4991 dev_err(tmp_adev->dev, "ib ring test failed (%d).\n", r);
4992 need_full_reset = true;
4999 r = amdgpu_device_recover_vram(tmp_adev);
5001 tmp_adev->asic_reset_res = r;
5005 if (need_full_reset)
5006 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
5008 clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
5012 static void amdgpu_device_set_mp1_state(struct amdgpu_device *adev)
5015 switch (amdgpu_asic_reset_method(adev)) {
5016 case AMD_RESET_METHOD_MODE1:
5017 adev->mp1_state = PP_MP1_STATE_SHUTDOWN;
5019 case AMD_RESET_METHOD_MODE2:
5020 adev->mp1_state = PP_MP1_STATE_RESET;
5023 adev->mp1_state = PP_MP1_STATE_NONE;
5028 static void amdgpu_device_unset_mp1_state(struct amdgpu_device *adev)
5030 amdgpu_vf_error_trans_all(adev);
5031 adev->mp1_state = PP_MP1_STATE_NONE;
5034 static void amdgpu_device_resume_display_audio(struct amdgpu_device *adev)
5036 struct pci_dev *p = NULL;
5038 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
5039 adev->pdev->bus->number, 1);
5041 pm_runtime_enable(&(p->dev));
5042 pm_runtime_resume(&(p->dev));
5048 static int amdgpu_device_suspend_display_audio(struct amdgpu_device *adev)
5050 enum amd_reset_method reset_method;
5051 struct pci_dev *p = NULL;
5055 * For now, only BACO and mode1 reset are confirmed
5056 * to suffer the audio issue without proper suspended.
5058 reset_method = amdgpu_asic_reset_method(adev);
5059 if ((reset_method != AMD_RESET_METHOD_BACO) &&
5060 (reset_method != AMD_RESET_METHOD_MODE1))
5063 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
5064 adev->pdev->bus->number, 1);
5068 expires = pm_runtime_autosuspend_expiration(&(p->dev));
5071 * If we cannot get the audio device autosuspend delay,
5072 * a fixed 4S interval will be used. Considering 3S is
5073 * the audio controller default autosuspend delay setting.
5074 * 4S used here is guaranteed to cover that.
5076 expires = ktime_get_mono_fast_ns() + NSEC_PER_SEC * 4ULL;
5078 while (!pm_runtime_status_suspended(&(p->dev))) {
5079 if (!pm_runtime_suspend(&(p->dev)))
5082 if (expires < ktime_get_mono_fast_ns()) {
5083 dev_warn(adev->dev, "failed to suspend display audio\n");
5085 /* TODO: abort the succeeding gpu reset? */
5090 pm_runtime_disable(&(p->dev));
5096 static void amdgpu_device_recheck_guilty_jobs(
5097 struct amdgpu_device *adev, struct list_head *device_list_handle,
5098 struct amdgpu_reset_context *reset_context)
5102 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5103 struct amdgpu_ring *ring = adev->rings[i];
5105 struct drm_sched_job *s_job;
5107 if (!ring || !ring->sched.thread)
5110 s_job = list_first_entry_or_null(&ring->sched.pending_list,
5111 struct drm_sched_job, list);
5115 /* clear job's guilty and depend the folowing step to decide the real one */
5116 drm_sched_reset_karma(s_job);
5117 drm_sched_resubmit_jobs_ext(&ring->sched, 1);
5119 if (!s_job->s_fence->parent) {
5120 DRM_WARN("Failed to get a HW fence for job!");
5124 ret = dma_fence_wait_timeout(s_job->s_fence->parent, false, ring->sched.timeout);
5125 if (ret == 0) { /* timeout */
5126 DRM_ERROR("Found the real bad job! ring:%s, job_id:%llx\n",
5127 ring->sched.name, s_job->id);
5130 amdgpu_fence_driver_isr_toggle(adev, true);
5132 /* Clear this failed job from fence array */
5133 amdgpu_fence_driver_clear_job_fences(ring);
5135 amdgpu_fence_driver_isr_toggle(adev, false);
5137 /* Since the job won't signal and we go for
5138 * another resubmit drop this parent pointer
5140 dma_fence_put(s_job->s_fence->parent);
5141 s_job->s_fence->parent = NULL;
5144 drm_sched_increase_karma(s_job);
5145 amdgpu_reset_prepare_hwcontext(adev, reset_context);
5148 if (amdgpu_sriov_vf(adev)) {
5149 amdgpu_virt_fini_data_exchange(adev);
5150 r = amdgpu_device_reset_sriov(adev, false);
5152 adev->asic_reset_res = r;
5154 clear_bit(AMDGPU_SKIP_HW_RESET,
5155 &reset_context->flags);
5156 r = amdgpu_do_asic_reset(device_list_handle,
5158 if (r && r == -EAGAIN)
5163 * add reset counter so that the following
5164 * resubmitted job could flush vmid
5166 atomic_inc(&adev->gpu_reset_counter);
5170 /* got the hw fence, signal finished fence */
5171 atomic_dec(ring->sched.score);
5172 dma_fence_get(&s_job->s_fence->finished);
5173 dma_fence_signal(&s_job->s_fence->finished);
5174 dma_fence_put(&s_job->s_fence->finished);
5176 /* remove node from list and free the job */
5177 spin_lock(&ring->sched.job_list_lock);
5178 list_del_init(&s_job->list);
5179 spin_unlock(&ring->sched.job_list_lock);
5180 ring->sched.ops->free_job(s_job);
5184 static inline void amdgpu_device_stop_pending_resets(struct amdgpu_device *adev)
5186 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
5188 #if defined(CONFIG_DEBUG_FS)
5189 if (!amdgpu_sriov_vf(adev))
5190 cancel_work(&adev->reset_work);
5194 cancel_work(&adev->kfd.reset_work);
5196 if (amdgpu_sriov_vf(adev))
5197 cancel_work(&adev->virt.flr_work);
5199 if (con && adev->ras_enabled)
5200 cancel_work(&con->recovery_work);
5206 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
5208 * @adev: amdgpu_device pointer
5209 * @job: which job trigger hang
5211 * Attempt to reset the GPU if it has hung (all asics).
5212 * Attempt to do soft-reset or full-reset and reinitialize Asic
5213 * Returns 0 for success or an error on failure.
5216 int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
5217 struct amdgpu_job *job,
5218 struct amdgpu_reset_context *reset_context)
5220 struct list_head device_list, *device_list_handle = NULL;
5221 bool job_signaled = false;
5222 struct amdgpu_hive_info *hive = NULL;
5223 struct amdgpu_device *tmp_adev = NULL;
5225 bool need_emergency_restart = false;
5226 bool audio_suspended = false;
5227 int tmp_vram_lost_counter;
5228 bool gpu_reset_for_dev_remove = false;
5230 gpu_reset_for_dev_remove =
5231 test_bit(AMDGPU_RESET_FOR_DEVICE_REMOVE, &reset_context->flags) &&
5232 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
5235 * Special case: RAS triggered and full reset isn't supported
5237 need_emergency_restart = amdgpu_ras_need_emergency_restart(adev);
5240 * Flush RAM to disk so that after reboot
5241 * the user can read log and see why the system rebooted.
5243 if (need_emergency_restart && amdgpu_ras_get_context(adev)->reboot) {
5244 DRM_WARN("Emergency reboot.");
5247 emergency_restart();
5250 dev_info(adev->dev, "GPU %s begin!\n",
5251 need_emergency_restart ? "jobs stop":"reset");
5253 if (!amdgpu_sriov_vf(adev))
5254 hive = amdgpu_get_xgmi_hive(adev);
5256 mutex_lock(&hive->hive_lock);
5258 reset_context->job = job;
5259 reset_context->hive = hive;
5261 * Build list of devices to reset.
5262 * In case we are in XGMI hive mode, resort the device list
5263 * to put adev in the 1st position.
5265 INIT_LIST_HEAD(&device_list);
5266 if (!amdgpu_sriov_vf(adev) && (adev->gmc.xgmi.num_physical_nodes > 1)) {
5267 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
5268 list_add_tail(&tmp_adev->reset_list, &device_list);
5269 if (gpu_reset_for_dev_remove && adev->shutdown)
5270 tmp_adev->shutdown = true;
5272 if (!list_is_first(&adev->reset_list, &device_list))
5273 list_rotate_to_front(&adev->reset_list, &device_list);
5274 device_list_handle = &device_list;
5276 list_add_tail(&adev->reset_list, &device_list);
5277 device_list_handle = &device_list;
5280 /* We need to lock reset domain only once both for XGMI and single device */
5281 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
5283 amdgpu_device_lock_reset_domain(tmp_adev->reset_domain);
5285 /* block all schedulers and reset given job's ring */
5286 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5288 amdgpu_device_set_mp1_state(tmp_adev);
5291 * Try to put the audio codec into suspend state
5292 * before gpu reset started.
5294 * Due to the power domain of the graphics device
5295 * is shared with AZ power domain. Without this,
5296 * we may change the audio hardware from behind
5297 * the audio driver's back. That will trigger
5298 * some audio codec errors.
5300 if (!amdgpu_device_suspend_display_audio(tmp_adev))
5301 audio_suspended = true;
5303 amdgpu_ras_set_error_query_ready(tmp_adev, false);
5305 cancel_delayed_work_sync(&tmp_adev->delayed_init_work);
5307 if (!amdgpu_sriov_vf(tmp_adev))
5308 amdgpu_amdkfd_pre_reset(tmp_adev);
5311 * Mark these ASICs to be reseted as untracked first
5312 * And add them back after reset completed
5314 amdgpu_unregister_gpu_instance(tmp_adev);
5316 drm_fb_helper_set_suspend_unlocked(adev_to_drm(tmp_adev)->fb_helper, true);
5318 /* disable ras on ALL IPs */
5319 if (!need_emergency_restart &&
5320 amdgpu_device_ip_need_full_reset(tmp_adev))
5321 amdgpu_ras_suspend(tmp_adev);
5323 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5324 struct amdgpu_ring *ring = tmp_adev->rings[i];
5326 if (!ring || !ring->sched.thread)
5329 drm_sched_stop(&ring->sched, job ? &job->base : NULL);
5331 if (need_emergency_restart)
5332 amdgpu_job_stop_all_jobs_on_sched(&ring->sched);
5334 atomic_inc(&tmp_adev->gpu_reset_counter);
5337 if (need_emergency_restart)
5338 goto skip_sched_resume;
5341 * Must check guilty signal here since after this point all old
5342 * HW fences are force signaled.
5344 * job->base holds a reference to parent fence
5346 if (job && dma_fence_is_signaled(&job->hw_fence)) {
5347 job_signaled = true;
5348 dev_info(adev->dev, "Guilty job already signaled, skipping HW reset");
5352 retry: /* Rest of adevs pre asic reset from XGMI hive. */
5353 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5354 if (gpu_reset_for_dev_remove) {
5355 /* Workaroud for ASICs need to disable SMC first */
5356 amdgpu_device_smu_fini_early(tmp_adev);
5358 r = amdgpu_device_pre_asic_reset(tmp_adev, reset_context);
5359 /*TODO Should we stop ?*/
5361 dev_err(tmp_adev->dev, "GPU pre asic reset failed with err, %d for drm dev, %s ",
5362 r, adev_to_drm(tmp_adev)->unique);
5363 tmp_adev->asic_reset_res = r;
5367 * Drop all pending non scheduler resets. Scheduler resets
5368 * were already dropped during drm_sched_stop
5370 amdgpu_device_stop_pending_resets(tmp_adev);
5373 tmp_vram_lost_counter = atomic_read(&((adev)->vram_lost_counter));
5374 /* Actual ASIC resets if needed.*/
5375 /* Host driver will handle XGMI hive reset for SRIOV */
5376 if (amdgpu_sriov_vf(adev)) {
5377 r = amdgpu_device_reset_sriov(adev, job ? false : true);
5379 adev->asic_reset_res = r;
5381 /* Aldebaran supports ras in SRIOV, so need resume ras during reset */
5382 if (adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 2))
5383 amdgpu_ras_resume(adev);
5385 r = amdgpu_do_asic_reset(device_list_handle, reset_context);
5386 if (r && r == -EAGAIN)
5389 if (!r && gpu_reset_for_dev_remove)
5395 /* Post ASIC reset for all devs .*/
5396 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5399 * Sometimes a later bad compute job can block a good gfx job as gfx
5400 * and compute ring share internal GC HW mutually. We add an additional
5401 * guilty jobs recheck step to find the real guilty job, it synchronously
5402 * submits and pends for the first job being signaled. If it gets timeout,
5403 * we identify it as a real guilty job.
5405 if (amdgpu_gpu_recovery == 2 &&
5406 !(tmp_vram_lost_counter < atomic_read(&adev->vram_lost_counter)))
5407 amdgpu_device_recheck_guilty_jobs(
5408 tmp_adev, device_list_handle, reset_context);
5410 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5411 struct amdgpu_ring *ring = tmp_adev->rings[i];
5413 if (!ring || !ring->sched.thread)
5416 /* No point to resubmit jobs if we didn't HW reset*/
5417 if (!tmp_adev->asic_reset_res && !job_signaled)
5418 drm_sched_resubmit_jobs(&ring->sched);
5420 drm_sched_start(&ring->sched, !tmp_adev->asic_reset_res);
5423 if (adev->enable_mes && adev->ip_versions[GC_HWIP][0] != IP_VERSION(11, 0, 3))
5424 amdgpu_mes_self_test(tmp_adev);
5426 if (!drm_drv_uses_atomic_modeset(adev_to_drm(tmp_adev)) && !job_signaled) {
5427 drm_helper_resume_force_mode(adev_to_drm(tmp_adev));
5430 if (tmp_adev->asic_reset_res)
5431 r = tmp_adev->asic_reset_res;
5433 tmp_adev->asic_reset_res = 0;
5436 /* bad news, how to tell it to userspace ? */
5437 dev_info(tmp_adev->dev, "GPU reset(%d) failed\n", atomic_read(&tmp_adev->gpu_reset_counter));
5438 amdgpu_vf_error_put(tmp_adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
5440 dev_info(tmp_adev->dev, "GPU reset(%d) succeeded!\n", atomic_read(&tmp_adev->gpu_reset_counter));
5441 if (amdgpu_acpi_smart_shift_update(adev_to_drm(tmp_adev), AMDGPU_SS_DEV_D0))
5442 DRM_WARN("smart shift update failed\n");
5447 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5448 /* unlock kfd: SRIOV would do it separately */
5449 if (!need_emergency_restart && !amdgpu_sriov_vf(tmp_adev))
5450 amdgpu_amdkfd_post_reset(tmp_adev);
5452 /* kfd_post_reset will do nothing if kfd device is not initialized,
5453 * need to bring up kfd here if it's not be initialized before
5455 if (!adev->kfd.init_complete)
5456 amdgpu_amdkfd_device_init(adev);
5458 if (audio_suspended)
5459 amdgpu_device_resume_display_audio(tmp_adev);
5461 amdgpu_device_unset_mp1_state(tmp_adev);
5465 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
5467 amdgpu_device_unlock_reset_domain(tmp_adev->reset_domain);
5470 mutex_unlock(&hive->hive_lock);
5471 amdgpu_put_xgmi_hive(hive);
5475 dev_info(adev->dev, "GPU reset end with ret = %d\n", r);
5477 atomic_set(&adev->reset_domain->reset_res, r);
5482 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
5484 * @adev: amdgpu_device pointer
5486 * Fetchs and stores in the driver the PCIE capabilities (gen speed
5487 * and lanes) of the slot the device is in. Handles APUs and
5488 * virtualized environments where PCIE config space may not be available.
5490 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
5492 struct pci_dev *pdev;
5493 enum pci_bus_speed speed_cap, platform_speed_cap;
5494 enum pcie_link_width platform_link_width;
5496 if (amdgpu_pcie_gen_cap)
5497 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
5499 if (amdgpu_pcie_lane_cap)
5500 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
5502 /* covers APUs as well */
5503 if (pci_is_root_bus(adev->pdev->bus)) {
5504 if (adev->pm.pcie_gen_mask == 0)
5505 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
5506 if (adev->pm.pcie_mlw_mask == 0)
5507 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
5511 if (adev->pm.pcie_gen_mask && adev->pm.pcie_mlw_mask)
5514 pcie_bandwidth_available(adev->pdev, NULL,
5515 &platform_speed_cap, &platform_link_width);
5517 if (adev->pm.pcie_gen_mask == 0) {
5520 speed_cap = pcie_get_speed_cap(pdev);
5521 if (speed_cap == PCI_SPEED_UNKNOWN) {
5522 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5523 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5524 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
5526 if (speed_cap == PCIE_SPEED_32_0GT)
5527 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5528 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5529 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5530 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4 |
5531 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN5);
5532 else if (speed_cap == PCIE_SPEED_16_0GT)
5533 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5534 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5535 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5536 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4);
5537 else if (speed_cap == PCIE_SPEED_8_0GT)
5538 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5539 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5540 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
5541 else if (speed_cap == PCIE_SPEED_5_0GT)
5542 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5543 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2);
5545 adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1;
5548 if (platform_speed_cap == PCI_SPEED_UNKNOWN) {
5549 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5550 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
5552 if (platform_speed_cap == PCIE_SPEED_32_0GT)
5553 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5554 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5555 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5556 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4 |
5557 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN5);
5558 else if (platform_speed_cap == PCIE_SPEED_16_0GT)
5559 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5560 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5561 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5562 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4);
5563 else if (platform_speed_cap == PCIE_SPEED_8_0GT)
5564 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5565 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5566 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3);
5567 else if (platform_speed_cap == PCIE_SPEED_5_0GT)
5568 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5569 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
5571 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
5575 if (adev->pm.pcie_mlw_mask == 0) {
5576 if (platform_link_width == PCIE_LNK_WIDTH_UNKNOWN) {
5577 adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK;
5579 switch (platform_link_width) {
5581 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
5582 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5583 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5584 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5585 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5586 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5587 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5590 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5591 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5592 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5593 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5594 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5595 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5598 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5599 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5600 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5601 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5602 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5605 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5606 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5607 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5608 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5611 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5612 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5613 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5616 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5617 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5620 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
5630 * amdgpu_device_is_peer_accessible - Check peer access through PCIe BAR
5632 * @adev: amdgpu_device pointer
5633 * @peer_adev: amdgpu_device pointer for peer device trying to access @adev
5635 * Return true if @peer_adev can access (DMA) @adev through the PCIe
5636 * BAR, i.e. @adev is "large BAR" and the BAR matches the DMA mask of
5639 bool amdgpu_device_is_peer_accessible(struct amdgpu_device *adev,
5640 struct amdgpu_device *peer_adev)
5642 #ifdef CONFIG_HSA_AMD_P2P
5643 uint64_t address_mask = peer_adev->dev->dma_mask ?
5644 ~*peer_adev->dev->dma_mask : ~((1ULL << 32) - 1);
5645 resource_size_t aper_limit =
5646 adev->gmc.aper_base + adev->gmc.aper_size - 1;
5648 !adev->gmc.xgmi.connected_to_cpu &&
5649 !(pci_p2pdma_distance(adev->pdev, peer_adev->dev, false) < 0);
5651 return pcie_p2p && p2p_access && (adev->gmc.visible_vram_size &&
5652 adev->gmc.real_vram_size == adev->gmc.visible_vram_size &&
5653 !(adev->gmc.aper_base & address_mask ||
5654 aper_limit & address_mask));
5660 int amdgpu_device_baco_enter(struct drm_device *dev)
5662 struct amdgpu_device *adev = drm_to_adev(dev);
5663 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
5665 if (!amdgpu_device_supports_baco(adev_to_drm(adev)))
5668 if (ras && adev->ras_enabled &&
5669 adev->nbio.funcs->enable_doorbell_interrupt)
5670 adev->nbio.funcs->enable_doorbell_interrupt(adev, false);
5672 return amdgpu_dpm_baco_enter(adev);
5675 int amdgpu_device_baco_exit(struct drm_device *dev)
5677 struct amdgpu_device *adev = drm_to_adev(dev);
5678 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
5681 if (!amdgpu_device_supports_baco(adev_to_drm(adev)))
5684 ret = amdgpu_dpm_baco_exit(adev);
5688 if (ras && adev->ras_enabled &&
5689 adev->nbio.funcs->enable_doorbell_interrupt)
5690 adev->nbio.funcs->enable_doorbell_interrupt(adev, true);
5692 if (amdgpu_passthrough(adev) &&
5693 adev->nbio.funcs->clear_doorbell_interrupt)
5694 adev->nbio.funcs->clear_doorbell_interrupt(adev);
5700 * amdgpu_pci_error_detected - Called when a PCI error is detected.
5701 * @pdev: PCI device struct
5702 * @state: PCI channel state
5704 * Description: Called when a PCI error is detected.
5706 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT.
5708 pci_ers_result_t amdgpu_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
5710 struct drm_device *dev = pci_get_drvdata(pdev);
5711 struct amdgpu_device *adev = drm_to_adev(dev);
5714 DRM_INFO("PCI error: detected callback, state(%d)!!\n", state);
5716 if (adev->gmc.xgmi.num_physical_nodes > 1) {
5717 DRM_WARN("No support for XGMI hive yet...");
5718 return PCI_ERS_RESULT_DISCONNECT;
5721 adev->pci_channel_state = state;
5724 case pci_channel_io_normal:
5725 return PCI_ERS_RESULT_CAN_RECOVER;
5726 /* Fatal error, prepare for slot reset */
5727 case pci_channel_io_frozen:
5729 * Locking adev->reset_domain->sem will prevent any external access
5730 * to GPU during PCI error recovery
5732 amdgpu_device_lock_reset_domain(adev->reset_domain);
5733 amdgpu_device_set_mp1_state(adev);
5736 * Block any work scheduling as we do for regular GPU reset
5737 * for the duration of the recovery
5739 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5740 struct amdgpu_ring *ring = adev->rings[i];
5742 if (!ring || !ring->sched.thread)
5745 drm_sched_stop(&ring->sched, NULL);
5747 atomic_inc(&adev->gpu_reset_counter);
5748 return PCI_ERS_RESULT_NEED_RESET;
5749 case pci_channel_io_perm_failure:
5750 /* Permanent error, prepare for device removal */
5751 return PCI_ERS_RESULT_DISCONNECT;
5754 return PCI_ERS_RESULT_NEED_RESET;
5758 * amdgpu_pci_mmio_enabled - Enable MMIO and dump debug registers
5759 * @pdev: pointer to PCI device
5761 pci_ers_result_t amdgpu_pci_mmio_enabled(struct pci_dev *pdev)
5764 DRM_INFO("PCI error: mmio enabled callback!!\n");
5766 /* TODO - dump whatever for debugging purposes */
5768 /* This called only if amdgpu_pci_error_detected returns
5769 * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still
5770 * works, no need to reset slot.
5773 return PCI_ERS_RESULT_RECOVERED;
5777 * amdgpu_pci_slot_reset - Called when PCI slot has been reset.
5778 * @pdev: PCI device struct
5780 * Description: This routine is called by the pci error recovery
5781 * code after the PCI slot has been reset, just before we
5782 * should resume normal operations.
5784 pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev)
5786 struct drm_device *dev = pci_get_drvdata(pdev);
5787 struct amdgpu_device *adev = drm_to_adev(dev);
5789 struct amdgpu_reset_context reset_context;
5791 struct list_head device_list;
5793 DRM_INFO("PCI error: slot reset callback!!\n");
5795 memset(&reset_context, 0, sizeof(reset_context));
5797 INIT_LIST_HEAD(&device_list);
5798 list_add_tail(&adev->reset_list, &device_list);
5800 /* wait for asic to come out of reset */
5803 /* Restore PCI confspace */
5804 amdgpu_device_load_pci_state(pdev);
5806 /* confirm ASIC came out of reset */
5807 for (i = 0; i < adev->usec_timeout; i++) {
5808 memsize = amdgpu_asic_get_config_memsize(adev);
5810 if (memsize != 0xffffffff)
5814 if (memsize == 0xffffffff) {
5819 reset_context.method = AMD_RESET_METHOD_NONE;
5820 reset_context.reset_req_dev = adev;
5821 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
5822 set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
5824 adev->no_hw_access = true;
5825 r = amdgpu_device_pre_asic_reset(adev, &reset_context);
5826 adev->no_hw_access = false;
5830 r = amdgpu_do_asic_reset(&device_list, &reset_context);
5834 if (amdgpu_device_cache_pci_state(adev->pdev))
5835 pci_restore_state(adev->pdev);
5837 DRM_INFO("PCIe error recovery succeeded\n");
5839 DRM_ERROR("PCIe error recovery failed, err:%d", r);
5840 amdgpu_device_unset_mp1_state(adev);
5841 amdgpu_device_unlock_reset_domain(adev->reset_domain);
5844 return r ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
5848 * amdgpu_pci_resume() - resume normal ops after PCI reset
5849 * @pdev: pointer to PCI device
5851 * Called when the error recovery driver tells us that its
5852 * OK to resume normal operation.
5854 void amdgpu_pci_resume(struct pci_dev *pdev)
5856 struct drm_device *dev = pci_get_drvdata(pdev);
5857 struct amdgpu_device *adev = drm_to_adev(dev);
5861 DRM_INFO("PCI error: resume callback!!\n");
5863 /* Only continue execution for the case of pci_channel_io_frozen */
5864 if (adev->pci_channel_state != pci_channel_io_frozen)
5867 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5868 struct amdgpu_ring *ring = adev->rings[i];
5870 if (!ring || !ring->sched.thread)
5874 drm_sched_resubmit_jobs(&ring->sched);
5875 drm_sched_start(&ring->sched, true);
5878 amdgpu_device_unset_mp1_state(adev);
5879 amdgpu_device_unlock_reset_domain(adev->reset_domain);
5882 bool amdgpu_device_cache_pci_state(struct pci_dev *pdev)
5884 struct drm_device *dev = pci_get_drvdata(pdev);
5885 struct amdgpu_device *adev = drm_to_adev(dev);
5888 r = pci_save_state(pdev);
5890 kfree(adev->pci_state);
5892 adev->pci_state = pci_store_saved_state(pdev);
5894 if (!adev->pci_state) {
5895 DRM_ERROR("Failed to store PCI saved state");
5899 DRM_WARN("Failed to save PCI state, err:%d\n", r);
5906 bool amdgpu_device_load_pci_state(struct pci_dev *pdev)
5908 struct drm_device *dev = pci_get_drvdata(pdev);
5909 struct amdgpu_device *adev = drm_to_adev(dev);
5912 if (!adev->pci_state)
5915 r = pci_load_saved_state(pdev, adev->pci_state);
5918 pci_restore_state(pdev);
5920 DRM_WARN("Failed to load PCI state, err:%d\n", r);
5927 void amdgpu_device_flush_hdp(struct amdgpu_device *adev,
5928 struct amdgpu_ring *ring)
5930 #ifdef CONFIG_X86_64
5931 if ((adev->flags & AMD_IS_APU) && !amdgpu_passthrough(adev))
5934 if (adev->gmc.xgmi.connected_to_cpu)
5937 if (ring && ring->funcs->emit_hdp_flush)
5938 amdgpu_ring_emit_hdp_flush(ring);
5940 amdgpu_asic_flush_hdp(adev, ring);
5943 void amdgpu_device_invalidate_hdp(struct amdgpu_device *adev,
5944 struct amdgpu_ring *ring)
5946 #ifdef CONFIG_X86_64
5947 if ((adev->flags & AMD_IS_APU) && !amdgpu_passthrough(adev))
5950 if (adev->gmc.xgmi.connected_to_cpu)
5953 amdgpu_asic_invalidate_hdp(adev, ring);
5956 int amdgpu_in_reset(struct amdgpu_device *adev)
5958 return atomic_read(&adev->reset_domain->in_gpu_reset);
5962 * amdgpu_device_halt() - bring hardware to some kind of halt state
5964 * @adev: amdgpu_device pointer
5966 * Bring hardware to some kind of halt state so that no one can touch it
5967 * any more. It will help to maintain error context when error occurred.
5968 * Compare to a simple hang, the system will keep stable at least for SSH
5969 * access. Then it should be trivial to inspect the hardware state and
5970 * see what's going on. Implemented as following:
5972 * 1. drm_dev_unplug() makes device inaccessible to user space(IOCTLs, etc),
5973 * clears all CPU mappings to device, disallows remappings through page faults
5974 * 2. amdgpu_irq_disable_all() disables all interrupts
5975 * 3. amdgpu_fence_driver_hw_fini() signals all HW fences
5976 * 4. set adev->no_hw_access to avoid potential crashes after setp 5
5977 * 5. amdgpu_device_unmap_mmio() clears all MMIO mappings
5978 * 6. pci_disable_device() and pci_wait_for_pending_transaction()
5979 * flush any in flight DMA operations
5981 void amdgpu_device_halt(struct amdgpu_device *adev)
5983 struct pci_dev *pdev = adev->pdev;
5984 struct drm_device *ddev = adev_to_drm(adev);
5986 drm_dev_unplug(ddev);
5988 amdgpu_irq_disable_all(adev);
5990 amdgpu_fence_driver_hw_fini(adev);
5992 adev->no_hw_access = true;
5994 amdgpu_device_unmap_mmio(adev);
5996 pci_disable_device(pdev);
5997 pci_wait_for_pending_transaction(pdev);
6000 u32 amdgpu_device_pcie_port_rreg(struct amdgpu_device *adev,
6003 unsigned long flags, address, data;
6006 address = adev->nbio.funcs->get_pcie_port_index_offset(adev);
6007 data = adev->nbio.funcs->get_pcie_port_data_offset(adev);
6009 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
6010 WREG32(address, reg * 4);
6011 (void)RREG32(address);
6013 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
6017 void amdgpu_device_pcie_port_wreg(struct amdgpu_device *adev,
6020 unsigned long flags, address, data;
6022 address = adev->nbio.funcs->get_pcie_port_index_offset(adev);
6023 data = adev->nbio.funcs->get_pcie_port_data_offset(adev);
6025 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
6026 WREG32(address, reg * 4);
6027 (void)RREG32(address);
6030 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
6034 * amdgpu_device_switch_gang - switch to a new gang
6035 * @adev: amdgpu_device pointer
6036 * @gang: the gang to switch to
6038 * Try to switch to a new gang.
6039 * Returns: NULL if we switched to the new gang or a reference to the current
6042 struct dma_fence *amdgpu_device_switch_gang(struct amdgpu_device *adev,
6043 struct dma_fence *gang)
6045 struct dma_fence *old = NULL;
6050 old = dma_fence_get_rcu_safe(&adev->gang_submit);
6056 if (!dma_fence_is_signaled(old))
6059 } while (cmpxchg((struct dma_fence __force **)&adev->gang_submit,
6066 bool amdgpu_device_has_display_hardware(struct amdgpu_device *adev)
6068 switch (adev->asic_type) {
6069 #ifdef CONFIG_DRM_AMDGPU_SI
6073 /* chips with no display hardware */
6075 #ifdef CONFIG_DRM_AMDGPU_SI
6081 #ifdef CONFIG_DRM_AMDGPU_CIK
6090 case CHIP_POLARIS10:
6091 case CHIP_POLARIS11:
6092 case CHIP_POLARIS12:
6096 /* chips with display hardware */
6100 if (!adev->ip_versions[DCE_HWIP][0] ||
6101 (adev->harvest_ip_mask & AMD_HARVEST_IP_DMU_MASK))