2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
24 * Authors: Dave Airlie
28 #include <linux/power_supply.h>
29 #include <linux/kthread.h>
30 #include <linux/module.h>
31 #include <linux/console.h>
32 #include <linux/slab.h>
33 #include <linux/iommu.h>
34 #include <linux/pci.h>
35 #include <linux/devcoredump.h>
36 #include <generated/utsrelease.h>
37 #include <linux/pci-p2pdma.h>
38 #include <linux/apple-gmux.h>
40 #include <drm/drm_aperture.h>
41 #include <drm/drm_atomic_helper.h>
42 #include <drm/drm_crtc_helper.h>
43 #include <drm/drm_fb_helper.h>
44 #include <drm/drm_probe_helper.h>
45 #include <drm/amdgpu_drm.h>
46 #include <linux/vgaarb.h>
47 #include <linux/vga_switcheroo.h>
48 #include <linux/efi.h>
50 #include "amdgpu_trace.h"
51 #include "amdgpu_i2c.h"
53 #include "amdgpu_atombios.h"
54 #include "amdgpu_atomfirmware.h"
56 #ifdef CONFIG_DRM_AMDGPU_SI
59 #ifdef CONFIG_DRM_AMDGPU_CIK
65 #include "bif/bif_4_1_d.h"
66 #include <linux/firmware.h>
67 #include "amdgpu_vf_error.h"
69 #include "amdgpu_amdkfd.h"
70 #include "amdgpu_pm.h"
72 #include "amdgpu_xgmi.h"
73 #include "amdgpu_ras.h"
74 #include "amdgpu_pmu.h"
75 #include "amdgpu_fru_eeprom.h"
76 #include "amdgpu_reset.h"
78 #include <linux/suspend.h>
79 #include <drm/task_barrier.h>
80 #include <linux/pm_runtime.h>
82 #include <drm/drm_drv.h>
84 MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
85 MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
86 MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
87 MODULE_FIRMWARE("amdgpu/picasso_gpu_info.bin");
88 MODULE_FIRMWARE("amdgpu/raven2_gpu_info.bin");
89 MODULE_FIRMWARE("amdgpu/arcturus_gpu_info.bin");
90 MODULE_FIRMWARE("amdgpu/navi12_gpu_info.bin");
92 #define AMDGPU_RESUME_MS 2000
93 #define AMDGPU_MAX_RETRY_LIMIT 2
94 #define AMDGPU_RETRY_SRIOV_RESET(r) ((r) == -EBUSY || (r) == -ETIMEDOUT || (r) == -EINVAL)
96 static const struct drm_driver amdgpu_kms_driver;
98 const char *amdgpu_asic_name[] = {
140 * DOC: pcie_replay_count
142 * The amdgpu driver provides a sysfs API for reporting the total number
143 * of PCIe replays (NAKs)
144 * The file pcie_replay_count is used for this and returns the total
145 * number of replays as a sum of the NAKs generated and NAKs received
148 static ssize_t amdgpu_device_get_pcie_replay_count(struct device *dev,
149 struct device_attribute *attr, char *buf)
151 struct drm_device *ddev = dev_get_drvdata(dev);
152 struct amdgpu_device *adev = drm_to_adev(ddev);
153 uint64_t cnt = amdgpu_asic_get_pcie_replay_count(adev);
155 return sysfs_emit(buf, "%llu\n", cnt);
158 static DEVICE_ATTR(pcie_replay_count, S_IRUGO,
159 amdgpu_device_get_pcie_replay_count, NULL);
161 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev);
166 * The amdgpu driver provides a sysfs API for reporting the product name
168 * The file product_name is used for this and returns the product name
169 * as returned from the FRU.
170 * NOTE: This is only available for certain server cards
173 static ssize_t amdgpu_device_get_product_name(struct device *dev,
174 struct device_attribute *attr, char *buf)
176 struct drm_device *ddev = dev_get_drvdata(dev);
177 struct amdgpu_device *adev = drm_to_adev(ddev);
179 return sysfs_emit(buf, "%s\n", adev->product_name);
182 static DEVICE_ATTR(product_name, S_IRUGO,
183 amdgpu_device_get_product_name, NULL);
186 * DOC: product_number
188 * The amdgpu driver provides a sysfs API for reporting the part number
190 * The file product_number is used for this and returns the part number
191 * as returned from the FRU.
192 * NOTE: This is only available for certain server cards
195 static ssize_t amdgpu_device_get_product_number(struct device *dev,
196 struct device_attribute *attr, char *buf)
198 struct drm_device *ddev = dev_get_drvdata(dev);
199 struct amdgpu_device *adev = drm_to_adev(ddev);
201 return sysfs_emit(buf, "%s\n", adev->product_number);
204 static DEVICE_ATTR(product_number, S_IRUGO,
205 amdgpu_device_get_product_number, NULL);
210 * The amdgpu driver provides a sysfs API for reporting the serial number
212 * The file serial_number is used for this and returns the serial number
213 * as returned from the FRU.
214 * NOTE: This is only available for certain server cards
217 static ssize_t amdgpu_device_get_serial_number(struct device *dev,
218 struct device_attribute *attr, char *buf)
220 struct drm_device *ddev = dev_get_drvdata(dev);
221 struct amdgpu_device *adev = drm_to_adev(ddev);
223 return sysfs_emit(buf, "%s\n", adev->serial);
226 static DEVICE_ATTR(serial_number, S_IRUGO,
227 amdgpu_device_get_serial_number, NULL);
230 * amdgpu_device_supports_px - Is the device a dGPU with ATPX power control
232 * @dev: drm_device pointer
234 * Returns true if the device is a dGPU with ATPX power control,
235 * otherwise return false.
237 bool amdgpu_device_supports_px(struct drm_device *dev)
239 struct amdgpu_device *adev = drm_to_adev(dev);
241 if ((adev->flags & AMD_IS_PX) && !amdgpu_is_atpx_hybrid())
247 * amdgpu_device_supports_boco - Is the device a dGPU with ACPI power resources
249 * @dev: drm_device pointer
251 * Returns true if the device is a dGPU with ACPI power control,
252 * otherwise return false.
254 bool amdgpu_device_supports_boco(struct drm_device *dev)
256 struct amdgpu_device *adev = drm_to_adev(dev);
259 ((adev->flags & AMD_IS_PX) && amdgpu_is_atpx_hybrid()))
265 * amdgpu_device_supports_baco - Does the device support BACO
267 * @dev: drm_device pointer
269 * Returns true if the device supporte BACO,
270 * otherwise return false.
272 bool amdgpu_device_supports_baco(struct drm_device *dev)
274 struct amdgpu_device *adev = drm_to_adev(dev);
276 return amdgpu_asic_supports_baco(adev);
280 * amdgpu_device_supports_smart_shift - Is the device dGPU with
281 * smart shift support
283 * @dev: drm_device pointer
285 * Returns true if the device is a dGPU with Smart Shift support,
286 * otherwise returns false.
288 bool amdgpu_device_supports_smart_shift(struct drm_device *dev)
290 return (amdgpu_device_supports_boco(dev) &&
291 amdgpu_acpi_is_power_shift_control_supported());
295 * VRAM access helper functions
299 * amdgpu_device_mm_access - access vram by MM_INDEX/MM_DATA
301 * @adev: amdgpu_device pointer
302 * @pos: offset of the buffer in vram
303 * @buf: virtual address of the buffer in system memory
304 * @size: read/write size, sizeof(@buf) must > @size
305 * @write: true - write to vram, otherwise - read from vram
307 void amdgpu_device_mm_access(struct amdgpu_device *adev, loff_t pos,
308 void *buf, size_t size, bool write)
311 uint32_t hi = ~0, tmp = 0;
312 uint32_t *data = buf;
316 if (!drm_dev_enter(adev_to_drm(adev), &idx))
319 BUG_ON(!IS_ALIGNED(pos, 4) || !IS_ALIGNED(size, 4));
321 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
322 for (last = pos + size; pos < last; pos += 4) {
325 WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)pos) | 0x80000000);
327 WREG32_NO_KIQ(mmMM_INDEX_HI, tmp);
331 WREG32_NO_KIQ(mmMM_DATA, *data++);
333 *data++ = RREG32_NO_KIQ(mmMM_DATA);
336 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
341 * amdgpu_device_aper_access - access vram by vram aperature
343 * @adev: amdgpu_device pointer
344 * @pos: offset of the buffer in vram
345 * @buf: virtual address of the buffer in system memory
346 * @size: read/write size, sizeof(@buf) must > @size
347 * @write: true - write to vram, otherwise - read from vram
349 * The return value means how many bytes have been transferred.
351 size_t amdgpu_device_aper_access(struct amdgpu_device *adev, loff_t pos,
352 void *buf, size_t size, bool write)
359 if (!adev->mman.aper_base_kaddr)
362 last = min(pos + size, adev->gmc.visible_vram_size);
364 addr = adev->mman.aper_base_kaddr + pos;
368 memcpy_toio(addr, buf, count);
370 amdgpu_device_flush_hdp(adev, NULL);
372 amdgpu_device_invalidate_hdp(adev, NULL);
374 memcpy_fromio(buf, addr, count);
386 * amdgpu_device_vram_access - read/write a buffer in vram
388 * @adev: amdgpu_device pointer
389 * @pos: offset of the buffer in vram
390 * @buf: virtual address of the buffer in system memory
391 * @size: read/write size, sizeof(@buf) must > @size
392 * @write: true - write to vram, otherwise - read from vram
394 void amdgpu_device_vram_access(struct amdgpu_device *adev, loff_t pos,
395 void *buf, size_t size, bool write)
399 /* try to using vram apreature to access vram first */
400 count = amdgpu_device_aper_access(adev, pos, buf, size, write);
403 /* using MM to access rest vram */
406 amdgpu_device_mm_access(adev, pos, buf, size, write);
411 * register access helper functions.
414 /* Check if hw access should be skipped because of hotplug or device error */
415 bool amdgpu_device_skip_hw_access(struct amdgpu_device *adev)
417 if (adev->no_hw_access)
420 #ifdef CONFIG_LOCKDEP
422 * This is a bit complicated to understand, so worth a comment. What we assert
423 * here is that the GPU reset is not running on another thread in parallel.
425 * For this we trylock the read side of the reset semaphore, if that succeeds
426 * we know that the reset is not running in paralell.
428 * If the trylock fails we assert that we are either already holding the read
429 * side of the lock or are the reset thread itself and hold the write side of
433 if (down_read_trylock(&adev->reset_domain->sem))
434 up_read(&adev->reset_domain->sem);
436 lockdep_assert_held(&adev->reset_domain->sem);
443 * amdgpu_device_rreg - read a memory mapped IO or indirect register
445 * @adev: amdgpu_device pointer
446 * @reg: dword aligned register offset
447 * @acc_flags: access flags which require special behavior
449 * Returns the 32 bit value from the offset specified.
451 uint32_t amdgpu_device_rreg(struct amdgpu_device *adev,
452 uint32_t reg, uint32_t acc_flags)
456 if (amdgpu_device_skip_hw_access(adev))
459 if ((reg * 4) < adev->rmmio_size) {
460 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
461 amdgpu_sriov_runtime(adev) &&
462 down_read_trylock(&adev->reset_domain->sem)) {
463 ret = amdgpu_kiq_rreg(adev, reg);
464 up_read(&adev->reset_domain->sem);
466 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
469 ret = adev->pcie_rreg(adev, reg * 4);
472 trace_amdgpu_device_rreg(adev->pdev->device, reg, ret);
478 * MMIO register read with bytes helper functions
479 * @offset:bytes offset from MMIO start
484 * amdgpu_mm_rreg8 - read a memory mapped IO register
486 * @adev: amdgpu_device pointer
487 * @offset: byte aligned register offset
489 * Returns the 8 bit value from the offset specified.
491 uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset)
493 if (amdgpu_device_skip_hw_access(adev))
496 if (offset < adev->rmmio_size)
497 return (readb(adev->rmmio + offset));
502 * MMIO register write with bytes helper functions
503 * @offset:bytes offset from MMIO start
504 * @value: the value want to be written to the register
508 * amdgpu_mm_wreg8 - read a memory mapped IO register
510 * @adev: amdgpu_device pointer
511 * @offset: byte aligned register offset
512 * @value: 8 bit value to write
514 * Writes the value specified to the offset specified.
516 void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value)
518 if (amdgpu_device_skip_hw_access(adev))
521 if (offset < adev->rmmio_size)
522 writeb(value, adev->rmmio + offset);
528 * amdgpu_device_wreg - write to a memory mapped IO or indirect register
530 * @adev: amdgpu_device pointer
531 * @reg: dword aligned register offset
532 * @v: 32 bit value to write to the register
533 * @acc_flags: access flags which require special behavior
535 * Writes the value specified to the offset specified.
537 void amdgpu_device_wreg(struct amdgpu_device *adev,
538 uint32_t reg, uint32_t v,
541 if (amdgpu_device_skip_hw_access(adev))
544 if ((reg * 4) < adev->rmmio_size) {
545 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
546 amdgpu_sriov_runtime(adev) &&
547 down_read_trylock(&adev->reset_domain->sem)) {
548 amdgpu_kiq_wreg(adev, reg, v);
549 up_read(&adev->reset_domain->sem);
551 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
554 adev->pcie_wreg(adev, reg * 4, v);
557 trace_amdgpu_device_wreg(adev->pdev->device, reg, v);
561 * amdgpu_mm_wreg_mmio_rlc - write register either with direct/indirect mmio or with RLC path if in range
563 * @adev: amdgpu_device pointer
564 * @reg: mmio/rlc register
567 * this function is invoked only for the debugfs register access
569 void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
570 uint32_t reg, uint32_t v)
572 if (amdgpu_device_skip_hw_access(adev))
575 if (amdgpu_sriov_fullaccess(adev) &&
576 adev->gfx.rlc.funcs &&
577 adev->gfx.rlc.funcs->is_rlcg_access_range) {
578 if (adev->gfx.rlc.funcs->is_rlcg_access_range(adev, reg))
579 return amdgpu_sriov_wreg(adev, reg, v, 0, 0);
580 } else if ((reg * 4) >= adev->rmmio_size) {
581 adev->pcie_wreg(adev, reg * 4, v);
583 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
588 * amdgpu_mm_rdoorbell - read a doorbell dword
590 * @adev: amdgpu_device pointer
591 * @index: doorbell index
593 * Returns the value in the doorbell aperture at the
594 * requested doorbell index (CIK).
596 u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
598 if (amdgpu_device_skip_hw_access(adev))
601 if (index < adev->doorbell.num_doorbells) {
602 return readl(adev->doorbell.ptr + index);
604 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
610 * amdgpu_mm_wdoorbell - write a doorbell dword
612 * @adev: amdgpu_device pointer
613 * @index: doorbell index
616 * Writes @v to the doorbell aperture at the
617 * requested doorbell index (CIK).
619 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
621 if (amdgpu_device_skip_hw_access(adev))
624 if (index < adev->doorbell.num_doorbells) {
625 writel(v, adev->doorbell.ptr + index);
627 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
632 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
634 * @adev: amdgpu_device pointer
635 * @index: doorbell index
637 * Returns the value in the doorbell aperture at the
638 * requested doorbell index (VEGA10+).
640 u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
642 if (amdgpu_device_skip_hw_access(adev))
645 if (index < adev->doorbell.num_doorbells) {
646 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
648 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
654 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
656 * @adev: amdgpu_device pointer
657 * @index: doorbell index
660 * Writes @v to the doorbell aperture at the
661 * requested doorbell index (VEGA10+).
663 void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
665 if (amdgpu_device_skip_hw_access(adev))
668 if (index < adev->doorbell.num_doorbells) {
669 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
671 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
676 * amdgpu_device_indirect_rreg - read an indirect register
678 * @adev: amdgpu_device pointer
679 * @pcie_index: mmio register offset
680 * @pcie_data: mmio register offset
681 * @reg_addr: indirect register address to read from
683 * Returns the value of indirect register @reg_addr
685 u32 amdgpu_device_indirect_rreg(struct amdgpu_device *adev,
686 u32 pcie_index, u32 pcie_data,
691 void __iomem *pcie_index_offset;
692 void __iomem *pcie_data_offset;
694 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
695 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
696 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
698 writel(reg_addr, pcie_index_offset);
699 readl(pcie_index_offset);
700 r = readl(pcie_data_offset);
701 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
707 * amdgpu_device_indirect_rreg64 - read a 64bits indirect register
709 * @adev: amdgpu_device pointer
710 * @pcie_index: mmio register offset
711 * @pcie_data: mmio register offset
712 * @reg_addr: indirect register address to read from
714 * Returns the value of indirect register @reg_addr
716 u64 amdgpu_device_indirect_rreg64(struct amdgpu_device *adev,
717 u32 pcie_index, u32 pcie_data,
722 void __iomem *pcie_index_offset;
723 void __iomem *pcie_data_offset;
725 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
726 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
727 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
729 /* read low 32 bits */
730 writel(reg_addr, pcie_index_offset);
731 readl(pcie_index_offset);
732 r = readl(pcie_data_offset);
733 /* read high 32 bits */
734 writel(reg_addr + 4, pcie_index_offset);
735 readl(pcie_index_offset);
736 r |= ((u64)readl(pcie_data_offset) << 32);
737 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
743 * amdgpu_device_indirect_wreg - write an indirect register address
745 * @adev: amdgpu_device pointer
746 * @pcie_index: mmio register offset
747 * @pcie_data: mmio register offset
748 * @reg_addr: indirect register offset
749 * @reg_data: indirect register data
752 void amdgpu_device_indirect_wreg(struct amdgpu_device *adev,
753 u32 pcie_index, u32 pcie_data,
754 u32 reg_addr, u32 reg_data)
757 void __iomem *pcie_index_offset;
758 void __iomem *pcie_data_offset;
760 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
761 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
762 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
764 writel(reg_addr, pcie_index_offset);
765 readl(pcie_index_offset);
766 writel(reg_data, pcie_data_offset);
767 readl(pcie_data_offset);
768 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
772 * amdgpu_device_indirect_wreg64 - write a 64bits indirect register address
774 * @adev: amdgpu_device pointer
775 * @pcie_index: mmio register offset
776 * @pcie_data: mmio register offset
777 * @reg_addr: indirect register offset
778 * @reg_data: indirect register data
781 void amdgpu_device_indirect_wreg64(struct amdgpu_device *adev,
782 u32 pcie_index, u32 pcie_data,
783 u32 reg_addr, u64 reg_data)
786 void __iomem *pcie_index_offset;
787 void __iomem *pcie_data_offset;
789 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
790 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
791 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
793 /* write low 32 bits */
794 writel(reg_addr, pcie_index_offset);
795 readl(pcie_index_offset);
796 writel((u32)(reg_data & 0xffffffffULL), pcie_data_offset);
797 readl(pcie_data_offset);
798 /* write high 32 bits */
799 writel(reg_addr + 4, pcie_index_offset);
800 readl(pcie_index_offset);
801 writel((u32)(reg_data >> 32), pcie_data_offset);
802 readl(pcie_data_offset);
803 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
807 * amdgpu_invalid_rreg - dummy reg read function
809 * @adev: amdgpu_device pointer
810 * @reg: offset of register
812 * Dummy register read function. Used for register blocks
813 * that certain asics don't have (all asics).
814 * Returns the value in the register.
816 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
818 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
824 * amdgpu_invalid_wreg - dummy reg write function
826 * @adev: amdgpu_device pointer
827 * @reg: offset of register
828 * @v: value to write to the register
830 * Dummy register read function. Used for register blocks
831 * that certain asics don't have (all asics).
833 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
835 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
841 * amdgpu_invalid_rreg64 - dummy 64 bit reg read function
843 * @adev: amdgpu_device pointer
844 * @reg: offset of register
846 * Dummy register read function. Used for register blocks
847 * that certain asics don't have (all asics).
848 * Returns the value in the register.
850 static uint64_t amdgpu_invalid_rreg64(struct amdgpu_device *adev, uint32_t reg)
852 DRM_ERROR("Invalid callback to read 64 bit register 0x%04X\n", reg);
858 * amdgpu_invalid_wreg64 - dummy reg write function
860 * @adev: amdgpu_device pointer
861 * @reg: offset of register
862 * @v: value to write to the register
864 * Dummy register read function. Used for register blocks
865 * that certain asics don't have (all asics).
867 static void amdgpu_invalid_wreg64(struct amdgpu_device *adev, uint32_t reg, uint64_t v)
869 DRM_ERROR("Invalid callback to write 64 bit register 0x%04X with 0x%08llX\n",
875 * amdgpu_block_invalid_rreg - dummy reg read function
877 * @adev: amdgpu_device pointer
878 * @block: offset of instance
879 * @reg: offset of register
881 * Dummy register read function. Used for register blocks
882 * that certain asics don't have (all asics).
883 * Returns the value in the register.
885 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
886 uint32_t block, uint32_t reg)
888 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
895 * amdgpu_block_invalid_wreg - dummy reg write function
897 * @adev: amdgpu_device pointer
898 * @block: offset of instance
899 * @reg: offset of register
900 * @v: value to write to the register
902 * Dummy register read function. Used for register blocks
903 * that certain asics don't have (all asics).
905 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
907 uint32_t reg, uint32_t v)
909 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
915 * amdgpu_device_asic_init - Wrapper for atom asic_init
917 * @adev: amdgpu_device pointer
919 * Does any asic specific work and then calls atom asic init.
921 static int amdgpu_device_asic_init(struct amdgpu_device *adev)
923 amdgpu_asic_pre_asic_init(adev);
925 if (adev->ip_versions[GC_HWIP][0] >= IP_VERSION(11, 0, 0))
926 return amdgpu_atomfirmware_asic_init(adev, true);
928 return amdgpu_atom_asic_init(adev->mode_info.atom_context);
932 * amdgpu_device_mem_scratch_init - allocate the VRAM scratch page
934 * @adev: amdgpu_device pointer
936 * Allocates a scratch page of VRAM for use by various things in the
939 static int amdgpu_device_mem_scratch_init(struct amdgpu_device *adev)
941 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE, PAGE_SIZE,
942 AMDGPU_GEM_DOMAIN_VRAM |
943 AMDGPU_GEM_DOMAIN_GTT,
944 &adev->mem_scratch.robj,
945 &adev->mem_scratch.gpu_addr,
946 (void **)&adev->mem_scratch.ptr);
950 * amdgpu_device_mem_scratch_fini - Free the VRAM scratch page
952 * @adev: amdgpu_device pointer
954 * Frees the VRAM scratch page.
956 static void amdgpu_device_mem_scratch_fini(struct amdgpu_device *adev)
958 amdgpu_bo_free_kernel(&adev->mem_scratch.robj, NULL, NULL);
962 * amdgpu_device_program_register_sequence - program an array of registers.
964 * @adev: amdgpu_device pointer
965 * @registers: pointer to the register array
966 * @array_size: size of the register array
968 * Programs an array or registers with and and or masks.
969 * This is a helper for setting golden registers.
971 void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
972 const u32 *registers,
973 const u32 array_size)
975 u32 tmp, reg, and_mask, or_mask;
981 for (i = 0; i < array_size; i +=3) {
982 reg = registers[i + 0];
983 and_mask = registers[i + 1];
984 or_mask = registers[i + 2];
986 if (and_mask == 0xffffffff) {
991 if (adev->family >= AMDGPU_FAMILY_AI)
992 tmp |= (or_mask & and_mask);
1001 * amdgpu_device_pci_config_reset - reset the GPU
1003 * @adev: amdgpu_device pointer
1005 * Resets the GPU using the pci config reset sequence.
1006 * Only applicable to asics prior to vega10.
1008 void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
1010 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
1014 * amdgpu_device_pci_reset - reset the GPU using generic PCI means
1016 * @adev: amdgpu_device pointer
1018 * Resets the GPU using generic pci reset interfaces (FLR, SBR, etc.).
1020 int amdgpu_device_pci_reset(struct amdgpu_device *adev)
1022 return pci_reset_function(adev->pdev);
1026 * GPU doorbell aperture helpers function.
1029 * amdgpu_device_doorbell_init - Init doorbell driver information.
1031 * @adev: amdgpu_device pointer
1033 * Init doorbell driver information (CIK)
1034 * Returns 0 on success, error on failure.
1036 static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
1039 /* No doorbell on SI hardware generation */
1040 if (adev->asic_type < CHIP_BONAIRE) {
1041 adev->doorbell.base = 0;
1042 adev->doorbell.size = 0;
1043 adev->doorbell.num_doorbells = 0;
1044 adev->doorbell.ptr = NULL;
1048 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
1051 amdgpu_asic_init_doorbell_index(adev);
1053 /* doorbell bar mapping */
1054 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
1055 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
1057 if (adev->enable_mes) {
1058 adev->doorbell.num_doorbells =
1059 adev->doorbell.size / sizeof(u32);
1061 adev->doorbell.num_doorbells =
1062 min_t(u32, adev->doorbell.size / sizeof(u32),
1063 adev->doorbell_index.max_assignment+1);
1064 if (adev->doorbell.num_doorbells == 0)
1067 /* For Vega, reserve and map two pages on doorbell BAR since SDMA
1068 * paging queue doorbell use the second page. The
1069 * AMDGPU_DOORBELL64_MAX_ASSIGNMENT definition assumes all the
1070 * doorbells are in the first page. So with paging queue enabled,
1071 * the max num_doorbells should + 1 page (0x400 in dword)
1073 if (adev->asic_type >= CHIP_VEGA10)
1074 adev->doorbell.num_doorbells += 0x400;
1077 adev->doorbell.ptr = ioremap(adev->doorbell.base,
1078 adev->doorbell.num_doorbells *
1080 if (adev->doorbell.ptr == NULL)
1087 * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
1089 * @adev: amdgpu_device pointer
1091 * Tear down doorbell driver information (CIK)
1093 static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
1095 iounmap(adev->doorbell.ptr);
1096 adev->doorbell.ptr = NULL;
1102 * amdgpu_device_wb_*()
1103 * Writeback is the method by which the GPU updates special pages in memory
1104 * with the status of certain GPU events (fences, ring pointers,etc.).
1108 * amdgpu_device_wb_fini - Disable Writeback and free memory
1110 * @adev: amdgpu_device pointer
1112 * Disables Writeback and frees the Writeback memory (all asics).
1113 * Used at driver shutdown.
1115 static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
1117 if (adev->wb.wb_obj) {
1118 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
1120 (void **)&adev->wb.wb);
1121 adev->wb.wb_obj = NULL;
1126 * amdgpu_device_wb_init - Init Writeback driver info and allocate memory
1128 * @adev: amdgpu_device pointer
1130 * Initializes writeback and allocates writeback memory (all asics).
1131 * Used at driver startup.
1132 * Returns 0 on success or an -error on failure.
1134 static int amdgpu_device_wb_init(struct amdgpu_device *adev)
1138 if (adev->wb.wb_obj == NULL) {
1139 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
1140 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
1141 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
1142 &adev->wb.wb_obj, &adev->wb.gpu_addr,
1143 (void **)&adev->wb.wb);
1145 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
1149 adev->wb.num_wb = AMDGPU_MAX_WB;
1150 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
1152 /* clear wb memory */
1153 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
1160 * amdgpu_device_wb_get - Allocate a wb entry
1162 * @adev: amdgpu_device pointer
1165 * Allocate a wb slot for use by the driver (all asics).
1166 * Returns 0 on success or -EINVAL on failure.
1168 int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
1170 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
1172 if (offset < adev->wb.num_wb) {
1173 __set_bit(offset, adev->wb.used);
1174 *wb = offset << 3; /* convert to dw offset */
1182 * amdgpu_device_wb_free - Free a wb entry
1184 * @adev: amdgpu_device pointer
1187 * Free a wb slot allocated for use by the driver (all asics)
1189 void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
1192 if (wb < adev->wb.num_wb)
1193 __clear_bit(wb, adev->wb.used);
1197 * amdgpu_device_resize_fb_bar - try to resize FB BAR
1199 * @adev: amdgpu_device pointer
1201 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
1202 * to fail, but if any of the BARs is not accessible after the size we abort
1203 * driver loading by returning -ENODEV.
1205 int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
1207 int rbar_size = pci_rebar_bytes_to_size(adev->gmc.real_vram_size);
1208 struct pci_bus *root;
1209 struct resource *res;
1215 if (amdgpu_sriov_vf(adev))
1218 /* skip if the bios has already enabled large BAR */
1219 if (adev->gmc.real_vram_size &&
1220 (pci_resource_len(adev->pdev, 0) >= adev->gmc.real_vram_size))
1223 /* Check if the root BUS has 64bit memory resources */
1224 root = adev->pdev->bus;
1225 while (root->parent)
1226 root = root->parent;
1228 pci_bus_for_each_resource(root, res, i) {
1229 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
1230 res->start > 0x100000000ull)
1234 /* Trying to resize is pointless without a root hub window above 4GB */
1238 /* Limit the BAR size to what is available */
1239 rbar_size = min(fls(pci_rebar_get_possible_sizes(adev->pdev, 0)) - 1,
1242 /* Disable memory decoding while we change the BAR addresses and size */
1243 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
1244 pci_write_config_word(adev->pdev, PCI_COMMAND,
1245 cmd & ~PCI_COMMAND_MEMORY);
1247 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
1248 amdgpu_device_doorbell_fini(adev);
1249 if (adev->asic_type >= CHIP_BONAIRE)
1250 pci_release_resource(adev->pdev, 2);
1252 pci_release_resource(adev->pdev, 0);
1254 r = pci_resize_resource(adev->pdev, 0, rbar_size);
1256 DRM_INFO("Not enough PCI address space for a large BAR.");
1257 else if (r && r != -ENOTSUPP)
1258 DRM_ERROR("Problem resizing BAR0 (%d).", r);
1260 pci_assign_unassigned_bus_resources(adev->pdev->bus);
1262 /* When the doorbell or fb BAR isn't available we have no chance of
1265 r = amdgpu_device_doorbell_init(adev);
1266 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
1269 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
1275 * GPU helpers function.
1278 * amdgpu_device_need_post - check if the hw need post or not
1280 * @adev: amdgpu_device pointer
1282 * Check if the asic has been initialized (all asics) at driver startup
1283 * or post is needed if hw reset is performed.
1284 * Returns true if need or false if not.
1286 bool amdgpu_device_need_post(struct amdgpu_device *adev)
1290 if (amdgpu_sriov_vf(adev))
1293 if (amdgpu_passthrough(adev)) {
1294 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
1295 * some old smc fw still need driver do vPost otherwise gpu hang, while
1296 * those smc fw version above 22.15 doesn't have this flaw, so we force
1297 * vpost executed for smc version below 22.15
1299 if (adev->asic_type == CHIP_FIJI) {
1302 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
1303 /* force vPost if error occured */
1307 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
1308 if (fw_ver < 0x00160e00)
1313 /* Don't post if we need to reset whole hive on init */
1314 if (adev->gmc.xgmi.pending_reset)
1317 if (adev->has_hw_reset) {
1318 adev->has_hw_reset = false;
1322 /* bios scratch used on CIK+ */
1323 if (adev->asic_type >= CHIP_BONAIRE)
1324 return amdgpu_atombios_scratch_need_asic_init(adev);
1326 /* check MEM_SIZE for older asics */
1327 reg = amdgpu_asic_get_config_memsize(adev);
1329 if ((reg != 0) && (reg != 0xffffffff))
1336 * amdgpu_device_should_use_aspm - check if the device should program ASPM
1338 * @adev: amdgpu_device pointer
1340 * Confirm whether the module parameter and pcie bridge agree that ASPM should
1341 * be set for this device.
1343 * Returns true if it should be used or false if not.
1345 bool amdgpu_device_should_use_aspm(struct amdgpu_device *adev)
1347 switch (amdgpu_aspm) {
1357 return pcie_aspm_enabled(adev->pdev);
1360 /* if we get transitioned to only one device, take VGA back */
1362 * amdgpu_device_vga_set_decode - enable/disable vga decode
1364 * @pdev: PCI device pointer
1365 * @state: enable/disable vga decode
1367 * Enable/disable vga decode (all asics).
1368 * Returns VGA resource flags.
1370 static unsigned int amdgpu_device_vga_set_decode(struct pci_dev *pdev,
1373 struct amdgpu_device *adev = drm_to_adev(pci_get_drvdata(pdev));
1374 amdgpu_asic_set_vga_state(adev, state);
1376 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1377 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1379 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1383 * amdgpu_device_check_block_size - validate the vm block size
1385 * @adev: amdgpu_device pointer
1387 * Validates the vm block size specified via module parameter.
1388 * The vm block size defines number of bits in page table versus page directory,
1389 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1390 * page table and the remaining bits are in the page directory.
1392 static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
1394 /* defines number of bits in page table versus page directory,
1395 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1396 * page table and the remaining bits are in the page directory */
1397 if (amdgpu_vm_block_size == -1)
1400 if (amdgpu_vm_block_size < 9) {
1401 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1402 amdgpu_vm_block_size);
1403 amdgpu_vm_block_size = -1;
1408 * amdgpu_device_check_vm_size - validate the vm size
1410 * @adev: amdgpu_device pointer
1412 * Validates the vm size in GB specified via module parameter.
1413 * The VM size is the size of the GPU virtual memory space in GB.
1415 static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
1417 /* no need to check the default value */
1418 if (amdgpu_vm_size == -1)
1421 if (amdgpu_vm_size < 1) {
1422 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1424 amdgpu_vm_size = -1;
1428 static void amdgpu_device_check_smu_prv_buffer_size(struct amdgpu_device *adev)
1431 bool is_os_64 = (sizeof(void *) == 8);
1432 uint64_t total_memory;
1433 uint64_t dram_size_seven_GB = 0x1B8000000;
1434 uint64_t dram_size_three_GB = 0xB8000000;
1436 if (amdgpu_smu_memory_pool_size == 0)
1440 DRM_WARN("Not 64-bit OS, feature not supported\n");
1444 total_memory = (uint64_t)si.totalram * si.mem_unit;
1446 if ((amdgpu_smu_memory_pool_size == 1) ||
1447 (amdgpu_smu_memory_pool_size == 2)) {
1448 if (total_memory < dram_size_three_GB)
1450 } else if ((amdgpu_smu_memory_pool_size == 4) ||
1451 (amdgpu_smu_memory_pool_size == 8)) {
1452 if (total_memory < dram_size_seven_GB)
1455 DRM_WARN("Smu memory pool size not supported\n");
1458 adev->pm.smu_prv_buffer_size = amdgpu_smu_memory_pool_size << 28;
1463 DRM_WARN("No enough system memory\n");
1465 adev->pm.smu_prv_buffer_size = 0;
1468 static int amdgpu_device_init_apu_flags(struct amdgpu_device *adev)
1470 if (!(adev->flags & AMD_IS_APU) ||
1471 adev->asic_type < CHIP_RAVEN)
1474 switch (adev->asic_type) {
1476 if (adev->pdev->device == 0x15dd)
1477 adev->apu_flags |= AMD_APU_IS_RAVEN;
1478 if (adev->pdev->device == 0x15d8)
1479 adev->apu_flags |= AMD_APU_IS_PICASSO;
1482 if ((adev->pdev->device == 0x1636) ||
1483 (adev->pdev->device == 0x164c))
1484 adev->apu_flags |= AMD_APU_IS_RENOIR;
1486 adev->apu_flags |= AMD_APU_IS_GREEN_SARDINE;
1489 adev->apu_flags |= AMD_APU_IS_VANGOGH;
1491 case CHIP_YELLOW_CARP:
1493 case CHIP_CYAN_SKILLFISH:
1494 if ((adev->pdev->device == 0x13FE) ||
1495 (adev->pdev->device == 0x143F))
1496 adev->apu_flags |= AMD_APU_IS_CYAN_SKILLFISH2;
1506 * amdgpu_device_check_arguments - validate module params
1508 * @adev: amdgpu_device pointer
1510 * Validates certain module parameters and updates
1511 * the associated values used by the driver (all asics).
1513 static int amdgpu_device_check_arguments(struct amdgpu_device *adev)
1515 if (amdgpu_sched_jobs < 4) {
1516 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1518 amdgpu_sched_jobs = 4;
1519 } else if (!is_power_of_2(amdgpu_sched_jobs)){
1520 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1522 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1525 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
1526 /* gart size must be greater or equal to 32M */
1527 dev_warn(adev->dev, "gart size (%d) too small\n",
1529 amdgpu_gart_size = -1;
1532 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
1533 /* gtt size must be greater or equal to 32M */
1534 dev_warn(adev->dev, "gtt size (%d) too small\n",
1536 amdgpu_gtt_size = -1;
1539 /* valid range is between 4 and 9 inclusive */
1540 if (amdgpu_vm_fragment_size != -1 &&
1541 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1542 dev_warn(adev->dev, "valid range is between 4 and 9\n");
1543 amdgpu_vm_fragment_size = -1;
1546 if (amdgpu_sched_hw_submission < 2) {
1547 dev_warn(adev->dev, "sched hw submission jobs (%d) must be at least 2\n",
1548 amdgpu_sched_hw_submission);
1549 amdgpu_sched_hw_submission = 2;
1550 } else if (!is_power_of_2(amdgpu_sched_hw_submission)) {
1551 dev_warn(adev->dev, "sched hw submission jobs (%d) must be a power of 2\n",
1552 amdgpu_sched_hw_submission);
1553 amdgpu_sched_hw_submission = roundup_pow_of_two(amdgpu_sched_hw_submission);
1556 if (amdgpu_reset_method < -1 || amdgpu_reset_method > 4) {
1557 dev_warn(adev->dev, "invalid option for reset method, reverting to default\n");
1558 amdgpu_reset_method = -1;
1561 amdgpu_device_check_smu_prv_buffer_size(adev);
1563 amdgpu_device_check_vm_size(adev);
1565 amdgpu_device_check_block_size(adev);
1567 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
1573 * amdgpu_switcheroo_set_state - set switcheroo state
1575 * @pdev: pci dev pointer
1576 * @state: vga_switcheroo state
1578 * Callback for the switcheroo driver. Suspends or resumes
1579 * the asics before or after it is powered up using ACPI methods.
1581 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev,
1582 enum vga_switcheroo_state state)
1584 struct drm_device *dev = pci_get_drvdata(pdev);
1587 if (amdgpu_device_supports_px(dev) && state == VGA_SWITCHEROO_OFF)
1590 if (state == VGA_SWITCHEROO_ON) {
1591 pr_info("switched on\n");
1592 /* don't suspend or resume card normally */
1593 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1595 pci_set_power_state(pdev, PCI_D0);
1596 amdgpu_device_load_pci_state(pdev);
1597 r = pci_enable_device(pdev);
1599 DRM_WARN("pci_enable_device failed (%d)\n", r);
1600 amdgpu_device_resume(dev, true);
1602 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1604 pr_info("switched off\n");
1605 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1606 amdgpu_device_suspend(dev, true);
1607 amdgpu_device_cache_pci_state(pdev);
1608 /* Shut down the device */
1609 pci_disable_device(pdev);
1610 pci_set_power_state(pdev, PCI_D3cold);
1611 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1616 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1618 * @pdev: pci dev pointer
1620 * Callback for the switcheroo driver. Check of the switcheroo
1621 * state can be changed.
1622 * Returns true if the state can be changed, false if not.
1624 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1626 struct drm_device *dev = pci_get_drvdata(pdev);
1629 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1630 * locking inversion with the driver load path. And the access here is
1631 * completely racy anyway. So don't bother with locking for now.
1633 return atomic_read(&dev->open_count) == 0;
1636 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1637 .set_gpu_state = amdgpu_switcheroo_set_state,
1639 .can_switch = amdgpu_switcheroo_can_switch,
1643 * amdgpu_device_ip_set_clockgating_state - set the CG state
1645 * @dev: amdgpu_device pointer
1646 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1647 * @state: clockgating state (gate or ungate)
1649 * Sets the requested clockgating state for all instances of
1650 * the hardware IP specified.
1651 * Returns the error code from the last instance.
1653 int amdgpu_device_ip_set_clockgating_state(void *dev,
1654 enum amd_ip_block_type block_type,
1655 enum amd_clockgating_state state)
1657 struct amdgpu_device *adev = dev;
1660 for (i = 0; i < adev->num_ip_blocks; i++) {
1661 if (!adev->ip_blocks[i].status.valid)
1663 if (adev->ip_blocks[i].version->type != block_type)
1665 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1667 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1668 (void *)adev, state);
1670 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1671 adev->ip_blocks[i].version->funcs->name, r);
1677 * amdgpu_device_ip_set_powergating_state - set the PG state
1679 * @dev: amdgpu_device pointer
1680 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1681 * @state: powergating state (gate or ungate)
1683 * Sets the requested powergating state for all instances of
1684 * the hardware IP specified.
1685 * Returns the error code from the last instance.
1687 int amdgpu_device_ip_set_powergating_state(void *dev,
1688 enum amd_ip_block_type block_type,
1689 enum amd_powergating_state state)
1691 struct amdgpu_device *adev = dev;
1694 for (i = 0; i < adev->num_ip_blocks; i++) {
1695 if (!adev->ip_blocks[i].status.valid)
1697 if (adev->ip_blocks[i].version->type != block_type)
1699 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1701 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1702 (void *)adev, state);
1704 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1705 adev->ip_blocks[i].version->funcs->name, r);
1711 * amdgpu_device_ip_get_clockgating_state - get the CG state
1713 * @adev: amdgpu_device pointer
1714 * @flags: clockgating feature flags
1716 * Walks the list of IPs on the device and updates the clockgating
1717 * flags for each IP.
1718 * Updates @flags with the feature flags for each hardware IP where
1719 * clockgating is enabled.
1721 void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
1726 for (i = 0; i < adev->num_ip_blocks; i++) {
1727 if (!adev->ip_blocks[i].status.valid)
1729 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1730 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1735 * amdgpu_device_ip_wait_for_idle - wait for idle
1737 * @adev: amdgpu_device pointer
1738 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1740 * Waits for the request hardware IP to be idle.
1741 * Returns 0 for success or a negative error code on failure.
1743 int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1744 enum amd_ip_block_type block_type)
1748 for (i = 0; i < adev->num_ip_blocks; i++) {
1749 if (!adev->ip_blocks[i].status.valid)
1751 if (adev->ip_blocks[i].version->type == block_type) {
1752 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
1763 * amdgpu_device_ip_is_idle - is the hardware IP idle
1765 * @adev: amdgpu_device pointer
1766 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1768 * Check if the hardware IP is idle or not.
1769 * Returns true if it the IP is idle, false if not.
1771 bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1772 enum amd_ip_block_type block_type)
1776 for (i = 0; i < adev->num_ip_blocks; i++) {
1777 if (!adev->ip_blocks[i].status.valid)
1779 if (adev->ip_blocks[i].version->type == block_type)
1780 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
1787 * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1789 * @adev: amdgpu_device pointer
1790 * @type: Type of hardware IP (SMU, GFX, UVD, etc.)
1792 * Returns a pointer to the hardware IP block structure
1793 * if it exists for the asic, otherwise NULL.
1795 struct amdgpu_ip_block *
1796 amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1797 enum amd_ip_block_type type)
1801 for (i = 0; i < adev->num_ip_blocks; i++)
1802 if (adev->ip_blocks[i].version->type == type)
1803 return &adev->ip_blocks[i];
1809 * amdgpu_device_ip_block_version_cmp
1811 * @adev: amdgpu_device pointer
1812 * @type: enum amd_ip_block_type
1813 * @major: major version
1814 * @minor: minor version
1816 * return 0 if equal or greater
1817 * return 1 if smaller or the ip_block doesn't exist
1819 int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1820 enum amd_ip_block_type type,
1821 u32 major, u32 minor)
1823 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
1825 if (ip_block && ((ip_block->version->major > major) ||
1826 ((ip_block->version->major == major) &&
1827 (ip_block->version->minor >= minor))))
1834 * amdgpu_device_ip_block_add
1836 * @adev: amdgpu_device pointer
1837 * @ip_block_version: pointer to the IP to add
1839 * Adds the IP block driver information to the collection of IPs
1842 int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1843 const struct amdgpu_ip_block_version *ip_block_version)
1845 if (!ip_block_version)
1848 switch (ip_block_version->type) {
1849 case AMD_IP_BLOCK_TYPE_VCN:
1850 if (adev->harvest_ip_mask & AMD_HARVEST_IP_VCN_MASK)
1853 case AMD_IP_BLOCK_TYPE_JPEG:
1854 if (adev->harvest_ip_mask & AMD_HARVEST_IP_JPEG_MASK)
1861 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
1862 ip_block_version->funcs->name);
1864 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1870 * amdgpu_device_enable_virtual_display - enable virtual display feature
1872 * @adev: amdgpu_device pointer
1874 * Enabled the virtual display feature if the user has enabled it via
1875 * the module parameter virtual_display. This feature provides a virtual
1876 * display hardware on headless boards or in virtualized environments.
1877 * This function parses and validates the configuration string specified by
1878 * the user and configues the virtual display configuration (number of
1879 * virtual connectors, crtcs, etc.) specified.
1881 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
1883 adev->enable_virtual_display = false;
1885 if (amdgpu_virtual_display) {
1886 const char *pci_address_name = pci_name(adev->pdev);
1887 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
1889 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1890 pciaddstr_tmp = pciaddstr;
1891 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1892 pciaddname = strsep(&pciaddname_tmp, ",");
1893 if (!strcmp("all", pciaddname)
1894 || !strcmp(pci_address_name, pciaddname)) {
1898 adev->enable_virtual_display = true;
1901 res = kstrtol(pciaddname_tmp, 10,
1909 adev->mode_info.num_crtc = num_crtc;
1911 adev->mode_info.num_crtc = 1;
1917 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1918 amdgpu_virtual_display, pci_address_name,
1919 adev->enable_virtual_display, adev->mode_info.num_crtc);
1925 void amdgpu_device_set_sriov_virtual_display(struct amdgpu_device *adev)
1927 if (amdgpu_sriov_vf(adev) && !adev->enable_virtual_display) {
1928 adev->mode_info.num_crtc = 1;
1929 adev->enable_virtual_display = true;
1930 DRM_INFO("virtual_display:%d, num_crtc:%d\n",
1931 adev->enable_virtual_display, adev->mode_info.num_crtc);
1936 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
1938 * @adev: amdgpu_device pointer
1940 * Parses the asic configuration parameters specified in the gpu info
1941 * firmware and makes them availale to the driver for use in configuring
1943 * Returns 0 on success, -EINVAL on failure.
1945 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1947 const char *chip_name;
1950 const struct gpu_info_firmware_header_v1_0 *hdr;
1952 adev->firmware.gpu_info_fw = NULL;
1954 if (adev->mman.discovery_bin) {
1956 * FIXME: The bounding box is still needed by Navi12, so
1957 * temporarily read it from gpu_info firmware. Should be dropped
1958 * when DAL no longer needs it.
1960 if (adev->asic_type != CHIP_NAVI12)
1964 switch (adev->asic_type) {
1968 chip_name = "vega10";
1971 chip_name = "vega12";
1974 if (adev->apu_flags & AMD_APU_IS_RAVEN2)
1975 chip_name = "raven2";
1976 else if (adev->apu_flags & AMD_APU_IS_PICASSO)
1977 chip_name = "picasso";
1979 chip_name = "raven";
1982 chip_name = "arcturus";
1985 chip_name = "navi12";
1989 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
1990 err = amdgpu_ucode_request(adev, &adev->firmware.gpu_info_fw, fw_name);
1993 "Failed to get gpu_info firmware \"%s\"\n",
1998 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
1999 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
2001 switch (hdr->version_major) {
2004 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
2005 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
2006 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2009 * Should be droped when DAL no longer needs it.
2011 if (adev->asic_type == CHIP_NAVI12)
2012 goto parse_soc_bounding_box;
2014 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
2015 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
2016 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
2017 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
2018 adev->gfx.config.max_texture_channel_caches =
2019 le32_to_cpu(gpu_info_fw->gc_num_tccs);
2020 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
2021 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
2022 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
2023 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
2024 adev->gfx.config.double_offchip_lds_buf =
2025 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
2026 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
2027 adev->gfx.cu_info.max_waves_per_simd =
2028 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
2029 adev->gfx.cu_info.max_scratch_slots_per_cu =
2030 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
2031 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
2032 if (hdr->version_minor >= 1) {
2033 const struct gpu_info_firmware_v1_1 *gpu_info_fw =
2034 (const struct gpu_info_firmware_v1_1 *)(adev->firmware.gpu_info_fw->data +
2035 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2036 adev->gfx.config.num_sc_per_sh =
2037 le32_to_cpu(gpu_info_fw->num_sc_per_sh);
2038 adev->gfx.config.num_packer_per_sc =
2039 le32_to_cpu(gpu_info_fw->num_packer_per_sc);
2042 parse_soc_bounding_box:
2044 * soc bounding box info is not integrated in disocovery table,
2045 * we always need to parse it from gpu info firmware if needed.
2047 if (hdr->version_minor == 2) {
2048 const struct gpu_info_firmware_v1_2 *gpu_info_fw =
2049 (const struct gpu_info_firmware_v1_2 *)(adev->firmware.gpu_info_fw->data +
2050 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2051 adev->dm.soc_bounding_box = &gpu_info_fw->soc_bounding_box;
2057 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
2066 * amdgpu_device_ip_early_init - run early init for hardware IPs
2068 * @adev: amdgpu_device pointer
2070 * Early initialization pass for hardware IPs. The hardware IPs that make
2071 * up each asic are discovered each IP's early_init callback is run. This
2072 * is the first stage in initializing the asic.
2073 * Returns 0 on success, negative error code on failure.
2075 static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
2077 struct drm_device *dev = adev_to_drm(adev);
2078 struct pci_dev *parent;
2082 amdgpu_device_enable_virtual_display(adev);
2084 if (amdgpu_sriov_vf(adev)) {
2085 r = amdgpu_virt_request_full_gpu(adev, true);
2090 switch (adev->asic_type) {
2091 #ifdef CONFIG_DRM_AMDGPU_SI
2097 adev->family = AMDGPU_FAMILY_SI;
2098 r = si_set_ip_blocks(adev);
2103 #ifdef CONFIG_DRM_AMDGPU_CIK
2109 if (adev->flags & AMD_IS_APU)
2110 adev->family = AMDGPU_FAMILY_KV;
2112 adev->family = AMDGPU_FAMILY_CI;
2114 r = cik_set_ip_blocks(adev);
2122 case CHIP_POLARIS10:
2123 case CHIP_POLARIS11:
2124 case CHIP_POLARIS12:
2128 if (adev->flags & AMD_IS_APU)
2129 adev->family = AMDGPU_FAMILY_CZ;
2131 adev->family = AMDGPU_FAMILY_VI;
2133 r = vi_set_ip_blocks(adev);
2138 r = amdgpu_discovery_set_ip_blocks(adev);
2144 if (amdgpu_has_atpx() &&
2145 (amdgpu_is_atpx_hybrid() ||
2146 amdgpu_has_atpx_dgpu_power_cntl()) &&
2147 ((adev->flags & AMD_IS_APU) == 0) &&
2148 !pci_is_thunderbolt_attached(to_pci_dev(dev->dev)))
2149 adev->flags |= AMD_IS_PX;
2151 if (!(adev->flags & AMD_IS_APU)) {
2152 parent = pci_upstream_bridge(adev->pdev);
2153 adev->has_pr3 = parent ? pci_pr3_present(parent) : false;
2156 amdgpu_amdkfd_device_probe(adev);
2158 adev->pm.pp_feature = amdgpu_pp_feature_mask;
2159 if (amdgpu_sriov_vf(adev) || sched_policy == KFD_SCHED_POLICY_NO_HWS)
2160 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
2161 if (amdgpu_sriov_vf(adev) && adev->asic_type == CHIP_SIENNA_CICHLID)
2162 adev->pm.pp_feature &= ~PP_OVERDRIVE_MASK;
2165 for (i = 0; i < adev->num_ip_blocks; i++) {
2166 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
2167 DRM_ERROR("disabled ip block: %d <%s>\n",
2168 i, adev->ip_blocks[i].version->funcs->name);
2169 adev->ip_blocks[i].status.valid = false;
2171 if (adev->ip_blocks[i].version->funcs->early_init) {
2172 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
2174 adev->ip_blocks[i].status.valid = false;
2176 DRM_ERROR("early_init of IP block <%s> failed %d\n",
2177 adev->ip_blocks[i].version->funcs->name, r);
2180 adev->ip_blocks[i].status.valid = true;
2183 adev->ip_blocks[i].status.valid = true;
2186 /* get the vbios after the asic_funcs are set up */
2187 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) {
2188 r = amdgpu_device_parse_gpu_info_fw(adev);
2193 if (!amdgpu_get_bios(adev))
2196 r = amdgpu_atombios_init(adev);
2198 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
2199 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
2203 /*get pf2vf msg info at it's earliest time*/
2204 if (amdgpu_sriov_vf(adev))
2205 amdgpu_virt_init_data_exchange(adev);
2212 adev->cg_flags &= amdgpu_cg_mask;
2213 adev->pg_flags &= amdgpu_pg_mask;
2218 static int amdgpu_device_ip_hw_init_phase1(struct amdgpu_device *adev)
2222 for (i = 0; i < adev->num_ip_blocks; i++) {
2223 if (!adev->ip_blocks[i].status.sw)
2225 if (adev->ip_blocks[i].status.hw)
2227 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2228 (amdgpu_sriov_vf(adev) && (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)) ||
2229 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
2230 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2232 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2233 adev->ip_blocks[i].version->funcs->name, r);
2236 adev->ip_blocks[i].status.hw = true;
2243 static int amdgpu_device_ip_hw_init_phase2(struct amdgpu_device *adev)
2247 for (i = 0; i < adev->num_ip_blocks; i++) {
2248 if (!adev->ip_blocks[i].status.sw)
2250 if (adev->ip_blocks[i].status.hw)
2252 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2254 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2255 adev->ip_blocks[i].version->funcs->name, r);
2258 adev->ip_blocks[i].status.hw = true;
2264 static int amdgpu_device_fw_loading(struct amdgpu_device *adev)
2268 uint32_t smu_version;
2270 if (adev->asic_type >= CHIP_VEGA10) {
2271 for (i = 0; i < adev->num_ip_blocks; i++) {
2272 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_PSP)
2275 if (!adev->ip_blocks[i].status.sw)
2278 /* no need to do the fw loading again if already done*/
2279 if (adev->ip_blocks[i].status.hw == true)
2282 if (amdgpu_in_reset(adev) || adev->in_suspend) {
2283 r = adev->ip_blocks[i].version->funcs->resume(adev);
2285 DRM_ERROR("resume of IP block <%s> failed %d\n",
2286 adev->ip_blocks[i].version->funcs->name, r);
2290 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2292 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2293 adev->ip_blocks[i].version->funcs->name, r);
2298 adev->ip_blocks[i].status.hw = true;
2303 if (!amdgpu_sriov_vf(adev) || adev->asic_type == CHIP_TONGA)
2304 r = amdgpu_pm_load_smu_firmware(adev, &smu_version);
2309 static int amdgpu_device_init_schedulers(struct amdgpu_device *adev)
2314 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2315 struct amdgpu_ring *ring = adev->rings[i];
2317 /* No need to setup the GPU scheduler for rings that don't need it */
2318 if (!ring || ring->no_scheduler)
2321 switch (ring->funcs->type) {
2322 case AMDGPU_RING_TYPE_GFX:
2323 timeout = adev->gfx_timeout;
2325 case AMDGPU_RING_TYPE_COMPUTE:
2326 timeout = adev->compute_timeout;
2328 case AMDGPU_RING_TYPE_SDMA:
2329 timeout = adev->sdma_timeout;
2332 timeout = adev->video_timeout;
2336 r = drm_sched_init(&ring->sched, &amdgpu_sched_ops,
2337 ring->num_hw_submission, amdgpu_job_hang_limit,
2338 timeout, adev->reset_domain->wq,
2339 ring->sched_score, ring->name,
2342 DRM_ERROR("Failed to create scheduler on ring %s.\n",
2353 * amdgpu_device_ip_init - run init for hardware IPs
2355 * @adev: amdgpu_device pointer
2357 * Main initialization pass for hardware IPs. The list of all the hardware
2358 * IPs that make up the asic is walked and the sw_init and hw_init callbacks
2359 * are run. sw_init initializes the software state associated with each IP
2360 * and hw_init initializes the hardware associated with each IP.
2361 * Returns 0 on success, negative error code on failure.
2363 static int amdgpu_device_ip_init(struct amdgpu_device *adev)
2367 r = amdgpu_ras_init(adev);
2371 for (i = 0; i < adev->num_ip_blocks; i++) {
2372 if (!adev->ip_blocks[i].status.valid)
2374 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
2376 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
2377 adev->ip_blocks[i].version->funcs->name, r);
2380 adev->ip_blocks[i].status.sw = true;
2382 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) {
2383 /* need to do common hw init early so everything is set up for gmc */
2384 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
2386 DRM_ERROR("hw_init %d failed %d\n", i, r);
2389 adev->ip_blocks[i].status.hw = true;
2390 } else if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
2391 /* need to do gmc hw init early so we can allocate gpu mem */
2392 /* Try to reserve bad pages early */
2393 if (amdgpu_sriov_vf(adev))
2394 amdgpu_virt_exchange_data(adev);
2396 r = amdgpu_device_mem_scratch_init(adev);
2398 DRM_ERROR("amdgpu_mem_scratch_init failed %d\n", r);
2401 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
2403 DRM_ERROR("hw_init %d failed %d\n", i, r);
2406 r = amdgpu_device_wb_init(adev);
2408 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
2411 adev->ip_blocks[i].status.hw = true;
2413 /* right after GMC hw init, we create CSA */
2415 r = amdgpu_allocate_static_csa(adev, &adev->virt.csa_obj,
2416 AMDGPU_GEM_DOMAIN_VRAM |
2417 AMDGPU_GEM_DOMAIN_GTT,
2420 DRM_ERROR("allocate CSA failed %d\n", r);
2427 if (amdgpu_sriov_vf(adev))
2428 amdgpu_virt_init_data_exchange(adev);
2430 r = amdgpu_ib_pool_init(adev);
2432 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
2433 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
2437 r = amdgpu_ucode_create_bo(adev); /* create ucode bo when sw_init complete*/
2441 r = amdgpu_device_ip_hw_init_phase1(adev);
2445 r = amdgpu_device_fw_loading(adev);
2449 r = amdgpu_device_ip_hw_init_phase2(adev);
2454 * retired pages will be loaded from eeprom and reserved here,
2455 * it should be called after amdgpu_device_ip_hw_init_phase2 since
2456 * for some ASICs the RAS EEPROM code relies on SMU fully functioning
2457 * for I2C communication which only true at this point.
2459 * amdgpu_ras_recovery_init may fail, but the upper only cares the
2460 * failure from bad gpu situation and stop amdgpu init process
2461 * accordingly. For other failed cases, it will still release all
2462 * the resource and print error message, rather than returning one
2463 * negative value to upper level.
2465 * Note: theoretically, this should be called before all vram allocations
2466 * to protect retired page from abusing
2468 r = amdgpu_ras_recovery_init(adev);
2473 * In case of XGMI grab extra reference for reset domain for this device
2475 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2476 if (amdgpu_xgmi_add_device(adev) == 0) {
2477 if (!amdgpu_sriov_vf(adev)) {
2478 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
2480 if (WARN_ON(!hive)) {
2485 if (!hive->reset_domain ||
2486 !amdgpu_reset_get_reset_domain(hive->reset_domain)) {
2488 amdgpu_put_xgmi_hive(hive);
2492 /* Drop the early temporary reset domain we created for device */
2493 amdgpu_reset_put_reset_domain(adev->reset_domain);
2494 adev->reset_domain = hive->reset_domain;
2495 amdgpu_put_xgmi_hive(hive);
2500 r = amdgpu_device_init_schedulers(adev);
2504 /* Don't init kfd if whole hive need to be reset during init */
2505 if (!adev->gmc.xgmi.pending_reset)
2506 amdgpu_amdkfd_device_init(adev);
2508 amdgpu_fru_get_product_info(adev);
2511 if (amdgpu_sriov_vf(adev))
2512 amdgpu_virt_release_full_gpu(adev, true);
2518 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
2520 * @adev: amdgpu_device pointer
2522 * Writes a reset magic value to the gart pointer in VRAM. The driver calls
2523 * this function before a GPU reset. If the value is retained after a
2524 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
2526 static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
2528 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
2532 * amdgpu_device_check_vram_lost - check if vram is valid
2534 * @adev: amdgpu_device pointer
2536 * Checks the reset magic value written to the gart pointer in VRAM.
2537 * The driver calls this after a GPU reset to see if the contents of
2538 * VRAM is lost or now.
2539 * returns true if vram is lost, false if not.
2541 static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
2543 if (memcmp(adev->gart.ptr, adev->reset_magic,
2544 AMDGPU_RESET_MAGIC_NUM))
2547 if (!amdgpu_in_reset(adev))
2551 * For all ASICs with baco/mode1 reset, the VRAM is
2552 * always assumed to be lost.
2554 switch (amdgpu_asic_reset_method(adev)) {
2555 case AMD_RESET_METHOD_BACO:
2556 case AMD_RESET_METHOD_MODE1:
2564 * amdgpu_device_set_cg_state - set clockgating for amdgpu device
2566 * @adev: amdgpu_device pointer
2567 * @state: clockgating state (gate or ungate)
2569 * The list of all the hardware IPs that make up the asic is walked and the
2570 * set_clockgating_state callbacks are run.
2571 * Late initialization pass enabling clockgating for hardware IPs.
2572 * Fini or suspend, pass disabling clockgating for hardware IPs.
2573 * Returns 0 on success, negative error code on failure.
2576 int amdgpu_device_set_cg_state(struct amdgpu_device *adev,
2577 enum amd_clockgating_state state)
2581 if (amdgpu_emu_mode == 1)
2584 for (j = 0; j < adev->num_ip_blocks; j++) {
2585 i = state == AMD_CG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
2586 if (!adev->ip_blocks[i].status.late_initialized)
2588 /* skip CG for GFX, SDMA on S0ix */
2589 if (adev->in_s0ix &&
2590 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX ||
2591 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SDMA))
2593 /* skip CG for VCE/UVD, it's handled specially */
2594 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
2595 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
2596 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
2597 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
2598 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
2599 /* enable clockgating to save power */
2600 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
2603 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
2604 adev->ip_blocks[i].version->funcs->name, r);
2613 int amdgpu_device_set_pg_state(struct amdgpu_device *adev,
2614 enum amd_powergating_state state)
2618 if (amdgpu_emu_mode == 1)
2621 for (j = 0; j < adev->num_ip_blocks; j++) {
2622 i = state == AMD_PG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
2623 if (!adev->ip_blocks[i].status.late_initialized)
2625 /* skip PG for GFX, SDMA on S0ix */
2626 if (adev->in_s0ix &&
2627 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX ||
2628 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SDMA))
2630 /* skip CG for VCE/UVD, it's handled specially */
2631 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
2632 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
2633 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
2634 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
2635 adev->ip_blocks[i].version->funcs->set_powergating_state) {
2636 /* enable powergating to save power */
2637 r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
2640 DRM_ERROR("set_powergating_state(gate) of IP block <%s> failed %d\n",
2641 adev->ip_blocks[i].version->funcs->name, r);
2649 static int amdgpu_device_enable_mgpu_fan_boost(void)
2651 struct amdgpu_gpu_instance *gpu_ins;
2652 struct amdgpu_device *adev;
2655 mutex_lock(&mgpu_info.mutex);
2658 * MGPU fan boost feature should be enabled
2659 * only when there are two or more dGPUs in
2662 if (mgpu_info.num_dgpu < 2)
2665 for (i = 0; i < mgpu_info.num_dgpu; i++) {
2666 gpu_ins = &(mgpu_info.gpu_ins[i]);
2667 adev = gpu_ins->adev;
2668 if (!(adev->flags & AMD_IS_APU) &&
2669 !gpu_ins->mgpu_fan_enabled) {
2670 ret = amdgpu_dpm_enable_mgpu_fan_boost(adev);
2674 gpu_ins->mgpu_fan_enabled = 1;
2679 mutex_unlock(&mgpu_info.mutex);
2685 * amdgpu_device_ip_late_init - run late init for hardware IPs
2687 * @adev: amdgpu_device pointer
2689 * Late initialization pass for hardware IPs. The list of all the hardware
2690 * IPs that make up the asic is walked and the late_init callbacks are run.
2691 * late_init covers any special initialization that an IP requires
2692 * after all of the have been initialized or something that needs to happen
2693 * late in the init process.
2694 * Returns 0 on success, negative error code on failure.
2696 static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
2698 struct amdgpu_gpu_instance *gpu_instance;
2701 for (i = 0; i < adev->num_ip_blocks; i++) {
2702 if (!adev->ip_blocks[i].status.hw)
2704 if (adev->ip_blocks[i].version->funcs->late_init) {
2705 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
2707 DRM_ERROR("late_init of IP block <%s> failed %d\n",
2708 adev->ip_blocks[i].version->funcs->name, r);
2712 adev->ip_blocks[i].status.late_initialized = true;
2715 r = amdgpu_ras_late_init(adev);
2717 DRM_ERROR("amdgpu_ras_late_init failed %d", r);
2721 amdgpu_ras_set_error_query_ready(adev, true);
2723 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE);
2724 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_GATE);
2726 amdgpu_device_fill_reset_magic(adev);
2728 r = amdgpu_device_enable_mgpu_fan_boost();
2730 DRM_ERROR("enable mgpu fan boost failed (%d).\n", r);
2732 /* For passthrough configuration on arcturus and aldebaran, enable special handling SBR */
2733 if (amdgpu_passthrough(adev) && ((adev->asic_type == CHIP_ARCTURUS && adev->gmc.xgmi.num_physical_nodes > 1)||
2734 adev->asic_type == CHIP_ALDEBARAN ))
2735 amdgpu_dpm_handle_passthrough_sbr(adev, true);
2737 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2738 mutex_lock(&mgpu_info.mutex);
2741 * Reset device p-state to low as this was booted with high.
2743 * This should be performed only after all devices from the same
2744 * hive get initialized.
2746 * However, it's unknown how many device in the hive in advance.
2747 * As this is counted one by one during devices initializations.
2749 * So, we wait for all XGMI interlinked devices initialized.
2750 * This may bring some delays as those devices may come from
2751 * different hives. But that should be OK.
2753 if (mgpu_info.num_dgpu == adev->gmc.xgmi.num_physical_nodes) {
2754 for (i = 0; i < mgpu_info.num_gpu; i++) {
2755 gpu_instance = &(mgpu_info.gpu_ins[i]);
2756 if (gpu_instance->adev->flags & AMD_IS_APU)
2759 r = amdgpu_xgmi_set_pstate(gpu_instance->adev,
2760 AMDGPU_XGMI_PSTATE_MIN);
2762 DRM_ERROR("pstate setting failed (%d).\n", r);
2768 mutex_unlock(&mgpu_info.mutex);
2775 * amdgpu_device_smu_fini_early - smu hw_fini wrapper
2777 * @adev: amdgpu_device pointer
2779 * For ASICs need to disable SMC first
2781 static void amdgpu_device_smu_fini_early(struct amdgpu_device *adev)
2785 if (adev->ip_versions[GC_HWIP][0] > IP_VERSION(9, 0, 0))
2788 for (i = 0; i < adev->num_ip_blocks; i++) {
2789 if (!adev->ip_blocks[i].status.hw)
2791 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
2792 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2793 /* XXX handle errors */
2795 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2796 adev->ip_blocks[i].version->funcs->name, r);
2798 adev->ip_blocks[i].status.hw = false;
2804 static int amdgpu_device_ip_fini_early(struct amdgpu_device *adev)
2808 for (i = 0; i < adev->num_ip_blocks; i++) {
2809 if (!adev->ip_blocks[i].version->funcs->early_fini)
2812 r = adev->ip_blocks[i].version->funcs->early_fini((void *)adev);
2814 DRM_DEBUG("early_fini of IP block <%s> failed %d\n",
2815 adev->ip_blocks[i].version->funcs->name, r);
2819 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2820 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2822 amdgpu_amdkfd_suspend(adev, false);
2824 /* Workaroud for ASICs need to disable SMC first */
2825 amdgpu_device_smu_fini_early(adev);
2827 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2828 if (!adev->ip_blocks[i].status.hw)
2831 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2832 /* XXX handle errors */
2834 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2835 adev->ip_blocks[i].version->funcs->name, r);
2838 adev->ip_blocks[i].status.hw = false;
2841 if (amdgpu_sriov_vf(adev)) {
2842 if (amdgpu_virt_release_full_gpu(adev, false))
2843 DRM_ERROR("failed to release exclusive mode on fini\n");
2850 * amdgpu_device_ip_fini - run fini for hardware IPs
2852 * @adev: amdgpu_device pointer
2854 * Main teardown pass for hardware IPs. The list of all the hardware
2855 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
2856 * are run. hw_fini tears down the hardware associated with each IP
2857 * and sw_fini tears down any software state associated with each IP.
2858 * Returns 0 on success, negative error code on failure.
2860 static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
2864 if (amdgpu_sriov_vf(adev) && adev->virt.ras_init_done)
2865 amdgpu_virt_release_ras_err_handler_data(adev);
2867 if (adev->gmc.xgmi.num_physical_nodes > 1)
2868 amdgpu_xgmi_remove_device(adev);
2870 amdgpu_amdkfd_device_fini_sw(adev);
2872 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2873 if (!adev->ip_blocks[i].status.sw)
2876 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
2877 amdgpu_ucode_free_bo(adev);
2878 amdgpu_free_static_csa(&adev->virt.csa_obj);
2879 amdgpu_device_wb_fini(adev);
2880 amdgpu_device_mem_scratch_fini(adev);
2881 amdgpu_ib_pool_fini(adev);
2884 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
2885 /* XXX handle errors */
2887 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
2888 adev->ip_blocks[i].version->funcs->name, r);
2890 adev->ip_blocks[i].status.sw = false;
2891 adev->ip_blocks[i].status.valid = false;
2894 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2895 if (!adev->ip_blocks[i].status.late_initialized)
2897 if (adev->ip_blocks[i].version->funcs->late_fini)
2898 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
2899 adev->ip_blocks[i].status.late_initialized = false;
2902 amdgpu_ras_fini(adev);
2908 * amdgpu_device_delayed_init_work_handler - work handler for IB tests
2910 * @work: work_struct.
2912 static void amdgpu_device_delayed_init_work_handler(struct work_struct *work)
2914 struct amdgpu_device *adev =
2915 container_of(work, struct amdgpu_device, delayed_init_work.work);
2918 r = amdgpu_ib_ring_tests(adev);
2920 DRM_ERROR("ib ring test failed (%d).\n", r);
2923 static void amdgpu_device_delay_enable_gfx_off(struct work_struct *work)
2925 struct amdgpu_device *adev =
2926 container_of(work, struct amdgpu_device, gfx.gfx_off_delay_work.work);
2928 WARN_ON_ONCE(adev->gfx.gfx_off_state);
2929 WARN_ON_ONCE(adev->gfx.gfx_off_req_count);
2931 if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true))
2932 adev->gfx.gfx_off_state = true;
2936 * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1)
2938 * @adev: amdgpu_device pointer
2940 * Main suspend function for hardware IPs. The list of all the hardware
2941 * IPs that make up the asic is walked, clockgating is disabled and the
2942 * suspend callbacks are run. suspend puts the hardware and software state
2943 * in each IP into a state suitable for suspend.
2944 * Returns 0 on success, negative error code on failure.
2946 static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev)
2950 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2951 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2954 * Per PMFW team's suggestion, driver needs to handle gfxoff
2955 * and df cstate features disablement for gpu reset(e.g. Mode1Reset)
2956 * scenario. Add the missing df cstate disablement here.
2958 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_DISALLOW))
2959 dev_warn(adev->dev, "Failed to disallow df cstate");
2961 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2962 if (!adev->ip_blocks[i].status.valid)
2965 /* displays are handled separately */
2966 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_DCE)
2969 /* XXX handle errors */
2970 r = adev->ip_blocks[i].version->funcs->suspend(adev);
2971 /* XXX handle errors */
2973 DRM_ERROR("suspend of IP block <%s> failed %d\n",
2974 adev->ip_blocks[i].version->funcs->name, r);
2978 adev->ip_blocks[i].status.hw = false;
2985 * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2)
2987 * @adev: amdgpu_device pointer
2989 * Main suspend function for hardware IPs. The list of all the hardware
2990 * IPs that make up the asic is walked, clockgating is disabled and the
2991 * suspend callbacks are run. suspend puts the hardware and software state
2992 * in each IP into a state suitable for suspend.
2993 * Returns 0 on success, negative error code on failure.
2995 static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev)
3000 amdgpu_dpm_gfx_state_change(adev, sGpuChangeState_D3Entry);
3002 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
3003 if (!adev->ip_blocks[i].status.valid)
3005 /* displays are handled in phase1 */
3006 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)
3008 /* PSP lost connection when err_event_athub occurs */
3009 if (amdgpu_ras_intr_triggered() &&
3010 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
3011 adev->ip_blocks[i].status.hw = false;
3015 /* skip unnecessary suspend if we do not initialize them yet */
3016 if (adev->gmc.xgmi.pending_reset &&
3017 !(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3018 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC ||
3019 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3020 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)) {
3021 adev->ip_blocks[i].status.hw = false;
3025 /* skip suspend of gfx/mes and psp for S0ix
3026 * gfx is in gfxoff state, so on resume it will exit gfxoff just
3027 * like at runtime. PSP is also part of the always on hardware
3028 * so no need to suspend it.
3030 if (adev->in_s0ix &&
3031 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP ||
3032 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX ||
3033 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_MES))
3036 /* SDMA 5.x+ is part of GFX power domain so it's covered by GFXOFF */
3037 if (adev->in_s0ix &&
3038 (adev->ip_versions[SDMA0_HWIP][0] >= IP_VERSION(5, 0, 0)) &&
3039 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SDMA))
3042 /* Once swPSP provides the IMU, RLC FW binaries to TOS during cold-boot.
3043 * These are in TMR, hence are expected to be reused by PSP-TOS to reload
3044 * from this location and RLC Autoload automatically also gets loaded
3045 * from here based on PMFW -> PSP message during re-init sequence.
3046 * Therefore, the psp suspend & resume should be skipped to avoid destroy
3047 * the TMR and reload FWs again for IMU enabled APU ASICs.
3049 if (amdgpu_in_reset(adev) &&
3050 (adev->flags & AMD_IS_APU) && adev->gfx.imu.funcs &&
3051 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)
3054 /* XXX handle errors */
3055 r = adev->ip_blocks[i].version->funcs->suspend(adev);
3056 /* XXX handle errors */
3058 DRM_ERROR("suspend of IP block <%s> failed %d\n",
3059 adev->ip_blocks[i].version->funcs->name, r);
3061 adev->ip_blocks[i].status.hw = false;
3062 /* handle putting the SMC in the appropriate state */
3063 if(!amdgpu_sriov_vf(adev)){
3064 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
3065 r = amdgpu_dpm_set_mp1_state(adev, adev->mp1_state);
3067 DRM_ERROR("SMC failed to set mp1 state %d, %d\n",
3068 adev->mp1_state, r);
3079 * amdgpu_device_ip_suspend - run suspend for hardware IPs
3081 * @adev: amdgpu_device pointer
3083 * Main suspend function for hardware IPs. The list of all the hardware
3084 * IPs that make up the asic is walked, clockgating is disabled and the
3085 * suspend callbacks are run. suspend puts the hardware and software state
3086 * in each IP into a state suitable for suspend.
3087 * Returns 0 on success, negative error code on failure.
3089 int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
3093 if (amdgpu_sriov_vf(adev)) {
3094 amdgpu_virt_fini_data_exchange(adev);
3095 amdgpu_virt_request_full_gpu(adev, false);
3098 r = amdgpu_device_ip_suspend_phase1(adev);
3101 r = amdgpu_device_ip_suspend_phase2(adev);
3103 if (amdgpu_sriov_vf(adev))
3104 amdgpu_virt_release_full_gpu(adev, false);
3109 static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
3113 static enum amd_ip_block_type ip_order[] = {
3114 AMD_IP_BLOCK_TYPE_COMMON,
3115 AMD_IP_BLOCK_TYPE_GMC,
3116 AMD_IP_BLOCK_TYPE_PSP,
3117 AMD_IP_BLOCK_TYPE_IH,
3120 for (i = 0; i < adev->num_ip_blocks; i++) {
3122 struct amdgpu_ip_block *block;
3124 block = &adev->ip_blocks[i];
3125 block->status.hw = false;
3127 for (j = 0; j < ARRAY_SIZE(ip_order); j++) {
3129 if (block->version->type != ip_order[j] ||
3130 !block->status.valid)
3133 r = block->version->funcs->hw_init(adev);
3134 DRM_INFO("RE-INIT-early: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
3137 block->status.hw = true;
3144 static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
3148 static enum amd_ip_block_type ip_order[] = {
3149 AMD_IP_BLOCK_TYPE_SMC,
3150 AMD_IP_BLOCK_TYPE_DCE,
3151 AMD_IP_BLOCK_TYPE_GFX,
3152 AMD_IP_BLOCK_TYPE_SDMA,
3153 AMD_IP_BLOCK_TYPE_UVD,
3154 AMD_IP_BLOCK_TYPE_VCE,
3155 AMD_IP_BLOCK_TYPE_VCN
3158 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
3160 struct amdgpu_ip_block *block;
3162 for (j = 0; j < adev->num_ip_blocks; j++) {
3163 block = &adev->ip_blocks[j];
3165 if (block->version->type != ip_order[i] ||
3166 !block->status.valid ||
3170 if (block->version->type == AMD_IP_BLOCK_TYPE_SMC)
3171 r = block->version->funcs->resume(adev);
3173 r = block->version->funcs->hw_init(adev);
3175 DRM_INFO("RE-INIT-late: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
3178 block->status.hw = true;
3186 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
3188 * @adev: amdgpu_device pointer
3190 * First resume function for hardware IPs. The list of all the hardware
3191 * IPs that make up the asic is walked and the resume callbacks are run for
3192 * COMMON, GMC, and IH. resume puts the hardware into a functional state
3193 * after a suspend and updates the software state as necessary. This
3194 * function is also used for restoring the GPU after a GPU reset.
3195 * Returns 0 on success, negative error code on failure.
3197 static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
3201 for (i = 0; i < adev->num_ip_blocks; i++) {
3202 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
3204 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3205 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3206 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3207 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP && amdgpu_sriov_vf(adev))) {
3209 r = adev->ip_blocks[i].version->funcs->resume(adev);
3211 DRM_ERROR("resume of IP block <%s> failed %d\n",
3212 adev->ip_blocks[i].version->funcs->name, r);
3215 adev->ip_blocks[i].status.hw = true;
3223 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
3225 * @adev: amdgpu_device pointer
3227 * First resume function for hardware IPs. The list of all the hardware
3228 * IPs that make up the asic is walked and the resume callbacks are run for
3229 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
3230 * functional state after a suspend and updates the software state as
3231 * necessary. This function is also used for restoring the GPU after a GPU
3233 * Returns 0 on success, negative error code on failure.
3235 static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
3239 for (i = 0; i < adev->num_ip_blocks; i++) {
3240 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
3242 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3243 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3244 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3245 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)
3247 r = adev->ip_blocks[i].version->funcs->resume(adev);
3249 DRM_ERROR("resume of IP block <%s> failed %d\n",
3250 adev->ip_blocks[i].version->funcs->name, r);
3253 adev->ip_blocks[i].status.hw = true;
3260 * amdgpu_device_ip_resume - run resume for hardware IPs
3262 * @adev: amdgpu_device pointer
3264 * Main resume function for hardware IPs. The hardware IPs
3265 * are split into two resume functions because they are
3266 * are also used in in recovering from a GPU reset and some additional
3267 * steps need to be take between them. In this case (S3/S4) they are
3269 * Returns 0 on success, negative error code on failure.
3271 static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
3275 r = amdgpu_amdkfd_resume_iommu(adev);
3279 r = amdgpu_device_ip_resume_phase1(adev);
3283 r = amdgpu_device_fw_loading(adev);
3287 r = amdgpu_device_ip_resume_phase2(adev);
3293 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
3295 * @adev: amdgpu_device pointer
3297 * Query the VBIOS data tables to determine if the board supports SR-IOV.
3299 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
3301 if (amdgpu_sriov_vf(adev)) {
3302 if (adev->is_atom_fw) {
3303 if (amdgpu_atomfirmware_gpu_virtualization_supported(adev))
3304 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
3306 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
3307 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
3310 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
3311 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
3316 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
3318 * @asic_type: AMD asic type
3320 * Check if there is DC (new modesetting infrastructre) support for an asic.
3321 * returns true if DC has support, false if not.
3323 bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
3325 switch (asic_type) {
3326 #ifdef CONFIG_DRM_AMDGPU_SI
3330 /* chips with no display hardware */
3332 #if defined(CONFIG_DRM_AMD_DC)
3338 * We have systems in the wild with these ASICs that require
3339 * LVDS and VGA support which is not supported with DC.
3341 * Fallback to the non-DC driver here by default so as not to
3342 * cause regressions.
3344 #if defined(CONFIG_DRM_AMD_DC_SI)
3345 return amdgpu_dc > 0;
3354 * We have systems in the wild with these ASICs that require
3355 * VGA support which is not supported with DC.
3357 * Fallback to the non-DC driver here by default so as not to
3358 * cause regressions.
3360 return amdgpu_dc > 0;
3362 return amdgpu_dc != 0;
3366 DRM_INFO_ONCE("Display Core has been requested via kernel parameter "
3367 "but isn't supported by ASIC, ignoring\n");
3374 * amdgpu_device_has_dc_support - check if dc is supported
3376 * @adev: amdgpu_device pointer
3378 * Returns true for supported, false for not supported
3380 bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
3382 if (adev->enable_virtual_display ||
3383 (adev->harvest_ip_mask & AMD_HARVEST_IP_DMU_MASK))
3386 return amdgpu_device_asic_has_dc_support(adev->asic_type);
3389 static void amdgpu_device_xgmi_reset_func(struct work_struct *__work)
3391 struct amdgpu_device *adev =
3392 container_of(__work, struct amdgpu_device, xgmi_reset_work);
3393 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
3395 /* It's a bug to not have a hive within this function */
3400 * Use task barrier to synchronize all xgmi reset works across the
3401 * hive. task_barrier_enter and task_barrier_exit will block
3402 * until all the threads running the xgmi reset works reach
3403 * those points. task_barrier_full will do both blocks.
3405 if (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) {
3407 task_barrier_enter(&hive->tb);
3408 adev->asic_reset_res = amdgpu_device_baco_enter(adev_to_drm(adev));
3410 if (adev->asic_reset_res)
3413 task_barrier_exit(&hive->tb);
3414 adev->asic_reset_res = amdgpu_device_baco_exit(adev_to_drm(adev));
3416 if (adev->asic_reset_res)
3419 if (adev->mmhub.ras && adev->mmhub.ras->ras_block.hw_ops &&
3420 adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count)
3421 adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count(adev);
3424 task_barrier_full(&hive->tb);
3425 adev->asic_reset_res = amdgpu_asic_reset(adev);
3429 if (adev->asic_reset_res)
3430 DRM_WARN("ASIC reset failed with error, %d for drm dev, %s",
3431 adev->asic_reset_res, adev_to_drm(adev)->unique);
3432 amdgpu_put_xgmi_hive(hive);
3435 static int amdgpu_device_get_job_timeout_settings(struct amdgpu_device *adev)
3437 char *input = amdgpu_lockup_timeout;
3438 char *timeout_setting = NULL;
3444 * By default timeout for non compute jobs is 10000
3445 * and 60000 for compute jobs.
3446 * In SR-IOV or passthrough mode, timeout for compute
3447 * jobs are 60000 by default.
3449 adev->gfx_timeout = msecs_to_jiffies(10000);
3450 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
3451 if (amdgpu_sriov_vf(adev))
3452 adev->compute_timeout = amdgpu_sriov_is_pp_one_vf(adev) ?
3453 msecs_to_jiffies(60000) : msecs_to_jiffies(10000);
3455 adev->compute_timeout = msecs_to_jiffies(60000);
3457 if (strnlen(input, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
3458 while ((timeout_setting = strsep(&input, ",")) &&
3459 strnlen(timeout_setting, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
3460 ret = kstrtol(timeout_setting, 0, &timeout);
3467 } else if (timeout < 0) {
3468 timeout = MAX_SCHEDULE_TIMEOUT;
3469 dev_warn(adev->dev, "lockup timeout disabled");
3470 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
3472 timeout = msecs_to_jiffies(timeout);
3477 adev->gfx_timeout = timeout;
3480 adev->compute_timeout = timeout;
3483 adev->sdma_timeout = timeout;
3486 adev->video_timeout = timeout;
3493 * There is only one value specified and
3494 * it should apply to all non-compute jobs.
3497 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
3498 if (amdgpu_sriov_vf(adev) || amdgpu_passthrough(adev))
3499 adev->compute_timeout = adev->gfx_timeout;
3507 * amdgpu_device_check_iommu_direct_map - check if RAM direct mapped to GPU
3509 * @adev: amdgpu_device pointer
3511 * RAM direct mapped to GPU if IOMMU is not enabled or is pass through mode
3513 static void amdgpu_device_check_iommu_direct_map(struct amdgpu_device *adev)
3515 struct iommu_domain *domain;
3517 domain = iommu_get_domain_for_dev(adev->dev);
3518 if (!domain || domain->type == IOMMU_DOMAIN_IDENTITY)
3519 adev->ram_is_direct_mapped = true;
3522 static const struct attribute *amdgpu_dev_attributes[] = {
3523 &dev_attr_product_name.attr,
3524 &dev_attr_product_number.attr,
3525 &dev_attr_serial_number.attr,
3526 &dev_attr_pcie_replay_count.attr,
3531 * amdgpu_device_init - initialize the driver
3533 * @adev: amdgpu_device pointer
3534 * @flags: driver flags
3536 * Initializes the driver info and hw (all asics).
3537 * Returns 0 for success or an error on failure.
3538 * Called at driver startup.
3540 int amdgpu_device_init(struct amdgpu_device *adev,
3543 struct drm_device *ddev = adev_to_drm(adev);
3544 struct pci_dev *pdev = adev->pdev;
3549 adev->shutdown = false;
3550 adev->flags = flags;
3552 if (amdgpu_force_asic_type >= 0 && amdgpu_force_asic_type < CHIP_LAST)
3553 adev->asic_type = amdgpu_force_asic_type;
3555 adev->asic_type = flags & AMD_ASIC_MASK;
3557 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
3558 if (amdgpu_emu_mode == 1)
3559 adev->usec_timeout *= 10;
3560 adev->gmc.gart_size = 512 * 1024 * 1024;
3561 adev->accel_working = false;
3562 adev->num_rings = 0;
3563 RCU_INIT_POINTER(adev->gang_submit, dma_fence_get_stub());
3564 adev->mman.buffer_funcs = NULL;
3565 adev->mman.buffer_funcs_ring = NULL;
3566 adev->vm_manager.vm_pte_funcs = NULL;
3567 adev->vm_manager.vm_pte_num_scheds = 0;
3568 adev->gmc.gmc_funcs = NULL;
3569 adev->harvest_ip_mask = 0x0;
3570 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
3571 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
3573 adev->smc_rreg = &amdgpu_invalid_rreg;
3574 adev->smc_wreg = &amdgpu_invalid_wreg;
3575 adev->pcie_rreg = &amdgpu_invalid_rreg;
3576 adev->pcie_wreg = &amdgpu_invalid_wreg;
3577 adev->pciep_rreg = &amdgpu_invalid_rreg;
3578 adev->pciep_wreg = &amdgpu_invalid_wreg;
3579 adev->pcie_rreg64 = &amdgpu_invalid_rreg64;
3580 adev->pcie_wreg64 = &amdgpu_invalid_wreg64;
3581 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
3582 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
3583 adev->didt_rreg = &amdgpu_invalid_rreg;
3584 adev->didt_wreg = &amdgpu_invalid_wreg;
3585 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
3586 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
3587 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
3588 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
3590 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
3591 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
3592 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
3594 /* mutex initialization are all done here so we
3595 * can recall function without having locking issues */
3596 mutex_init(&adev->firmware.mutex);
3597 mutex_init(&adev->pm.mutex);
3598 mutex_init(&adev->gfx.gpu_clock_mutex);
3599 mutex_init(&adev->srbm_mutex);
3600 mutex_init(&adev->gfx.pipe_reserve_mutex);
3601 mutex_init(&adev->gfx.gfx_off_mutex);
3602 mutex_init(&adev->grbm_idx_mutex);
3603 mutex_init(&adev->mn_lock);
3604 mutex_init(&adev->virt.vf_errors.lock);
3605 hash_init(adev->mn_hash);
3606 mutex_init(&adev->psp.mutex);
3607 mutex_init(&adev->notifier_lock);
3608 mutex_init(&adev->pm.stable_pstate_ctx_lock);
3609 mutex_init(&adev->benchmark_mutex);
3611 amdgpu_device_init_apu_flags(adev);
3613 r = amdgpu_device_check_arguments(adev);
3617 spin_lock_init(&adev->mmio_idx_lock);
3618 spin_lock_init(&adev->smc_idx_lock);
3619 spin_lock_init(&adev->pcie_idx_lock);
3620 spin_lock_init(&adev->uvd_ctx_idx_lock);
3621 spin_lock_init(&adev->didt_idx_lock);
3622 spin_lock_init(&adev->gc_cac_idx_lock);
3623 spin_lock_init(&adev->se_cac_idx_lock);
3624 spin_lock_init(&adev->audio_endpt_idx_lock);
3625 spin_lock_init(&adev->mm_stats.lock);
3627 INIT_LIST_HEAD(&adev->shadow_list);
3628 mutex_init(&adev->shadow_list_lock);
3630 INIT_LIST_HEAD(&adev->reset_list);
3632 INIT_LIST_HEAD(&adev->ras_list);
3634 INIT_DELAYED_WORK(&adev->delayed_init_work,
3635 amdgpu_device_delayed_init_work_handler);
3636 INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work,
3637 amdgpu_device_delay_enable_gfx_off);
3639 INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
3641 adev->gfx.gfx_off_req_count = 1;
3642 adev->gfx.gfx_off_residency = 0;
3643 adev->gfx.gfx_off_entrycount = 0;
3644 adev->pm.ac_power = power_supply_is_system_supplied() > 0;
3646 atomic_set(&adev->throttling_logging_enabled, 1);
3648 * If throttling continues, logging will be performed every minute
3649 * to avoid log flooding. "-1" is subtracted since the thermal
3650 * throttling interrupt comes every second. Thus, the total logging
3651 * interval is 59 seconds(retelimited printk interval) + 1(waiting
3652 * for throttling interrupt) = 60 seconds.
3654 ratelimit_state_init(&adev->throttling_logging_rs, (60 - 1) * HZ, 1);
3655 ratelimit_set_flags(&adev->throttling_logging_rs, RATELIMIT_MSG_ON_RELEASE);
3657 /* Registers mapping */
3658 /* TODO: block userspace mapping of io register */
3659 if (adev->asic_type >= CHIP_BONAIRE) {
3660 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
3661 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
3663 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
3664 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
3667 for (i = 0; i < AMD_IP_BLOCK_TYPE_NUM; i++)
3668 atomic_set(&adev->pm.pwr_state[i], POWER_STATE_UNKNOWN);
3670 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
3671 if (adev->rmmio == NULL) {
3674 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
3675 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
3677 amdgpu_device_get_pcie_info(adev);
3680 DRM_INFO("MCBP is enabled\n");
3683 * Reset domain needs to be present early, before XGMI hive discovered
3684 * (if any) and intitialized to use reset sem and in_gpu reset flag
3685 * early on during init and before calling to RREG32.
3687 adev->reset_domain = amdgpu_reset_create_reset_domain(SINGLE_DEVICE, "amdgpu-reset-dev");
3688 if (!adev->reset_domain)
3691 /* detect hw virtualization here */
3692 amdgpu_detect_virtualization(adev);
3694 r = amdgpu_device_get_job_timeout_settings(adev);
3696 dev_err(adev->dev, "invalid lockup_timeout parameter syntax\n");
3700 /* early init functions */
3701 r = amdgpu_device_ip_early_init(adev);
3705 /* Get rid of things like offb */
3706 r = drm_aperture_remove_conflicting_pci_framebuffers(adev->pdev, &amdgpu_kms_driver);
3710 /* Enable TMZ based on IP_VERSION */
3711 amdgpu_gmc_tmz_set(adev);
3713 amdgpu_gmc_noretry_set(adev);
3714 /* Need to get xgmi info early to decide the reset behavior*/
3715 if (adev->gmc.xgmi.supported) {
3716 r = adev->gfxhub.funcs->get_xgmi_info(adev);
3721 /* enable PCIE atomic ops */
3722 if (amdgpu_sriov_vf(adev))
3723 adev->have_atomics_support = ((struct amd_sriov_msg_pf2vf_info *)
3724 adev->virt.fw_reserve.p_pf2vf)->pcie_atomic_ops_support_flags ==
3725 (PCI_EXP_DEVCAP2_ATOMIC_COMP32 | PCI_EXP_DEVCAP2_ATOMIC_COMP64);
3727 adev->have_atomics_support =
3728 !pci_enable_atomic_ops_to_root(adev->pdev,
3729 PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
3730 PCI_EXP_DEVCAP2_ATOMIC_COMP64);
3731 if (!adev->have_atomics_support)
3732 dev_info(adev->dev, "PCIE atomic ops is not supported\n");
3734 /* doorbell bar mapping and doorbell index init*/
3735 amdgpu_device_doorbell_init(adev);
3737 if (amdgpu_emu_mode == 1) {
3738 /* post the asic on emulation mode */
3739 emu_soc_asic_init(adev);
3740 goto fence_driver_init;
3743 amdgpu_reset_init(adev);
3745 /* detect if we are with an SRIOV vbios */
3746 amdgpu_device_detect_sriov_bios(adev);
3748 /* check if we need to reset the asic
3749 * E.g., driver was not cleanly unloaded previously, etc.
3751 if (!amdgpu_sriov_vf(adev) && amdgpu_asic_need_reset_on_init(adev)) {
3752 if (adev->gmc.xgmi.num_physical_nodes) {
3753 dev_info(adev->dev, "Pending hive reset.\n");
3754 adev->gmc.xgmi.pending_reset = true;
3755 /* Only need to init necessary block for SMU to handle the reset */
3756 for (i = 0; i < adev->num_ip_blocks; i++) {
3757 if (!adev->ip_blocks[i].status.valid)
3759 if (!(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3760 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3761 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3762 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC)) {
3763 DRM_DEBUG("IP %s disabled for hw_init.\n",
3764 adev->ip_blocks[i].version->funcs->name);
3765 adev->ip_blocks[i].status.hw = true;
3769 r = amdgpu_asic_reset(adev);
3771 dev_err(adev->dev, "asic reset on init failed\n");
3777 /* Post card if necessary */
3778 if (amdgpu_device_need_post(adev)) {
3780 dev_err(adev->dev, "no vBIOS found\n");
3784 DRM_INFO("GPU posting now...\n");
3785 r = amdgpu_device_asic_init(adev);
3787 dev_err(adev->dev, "gpu post error!\n");
3792 if (adev->is_atom_fw) {
3793 /* Initialize clocks */
3794 r = amdgpu_atomfirmware_get_clock_info(adev);
3796 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
3797 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3801 /* Initialize clocks */
3802 r = amdgpu_atombios_get_clock_info(adev);
3804 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
3805 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3808 /* init i2c buses */
3809 if (!amdgpu_device_has_dc_support(adev))
3810 amdgpu_atombios_i2c_init(adev);
3815 r = amdgpu_fence_driver_sw_init(adev);
3817 dev_err(adev->dev, "amdgpu_fence_driver_sw_init failed\n");
3818 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
3822 /* init the mode config */
3823 drm_mode_config_init(adev_to_drm(adev));
3825 r = amdgpu_device_ip_init(adev);
3827 /* failed in exclusive mode due to timeout */
3828 if (amdgpu_sriov_vf(adev) &&
3829 !amdgpu_sriov_runtime(adev) &&
3830 amdgpu_virt_mmio_blocked(adev) &&
3831 !amdgpu_virt_wait_reset(adev)) {
3832 dev_err(adev->dev, "VF exclusive mode timeout\n");
3833 /* Don't send request since VF is inactive. */
3834 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
3835 adev->virt.ops = NULL;
3837 goto release_ras_con;
3839 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
3840 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
3841 goto release_ras_con;
3844 amdgpu_fence_driver_hw_init(adev);
3847 "SE %d, SH per SE %d, CU per SH %d, active_cu_number %d\n",
3848 adev->gfx.config.max_shader_engines,
3849 adev->gfx.config.max_sh_per_se,
3850 adev->gfx.config.max_cu_per_sh,
3851 adev->gfx.cu_info.number);
3853 adev->accel_working = true;
3855 amdgpu_vm_check_compute_bug(adev);
3857 /* Initialize the buffer migration limit. */
3858 if (amdgpu_moverate >= 0)
3859 max_MBps = amdgpu_moverate;
3861 max_MBps = 8; /* Allow 8 MB/s. */
3862 /* Get a log2 for easy divisions. */
3863 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
3865 r = amdgpu_pm_sysfs_init(adev);
3867 DRM_ERROR("registering pm sysfs failed (%d).\n", r);
3869 r = amdgpu_ucode_sysfs_init(adev);
3871 adev->ucode_sysfs_en = false;
3872 DRM_ERROR("Creating firmware sysfs failed (%d).\n", r);
3874 adev->ucode_sysfs_en = true;
3876 r = amdgpu_psp_sysfs_init(adev);
3878 adev->psp_sysfs_en = false;
3879 if (!amdgpu_sriov_vf(adev))
3880 DRM_ERROR("Creating psp sysfs failed\n");
3882 adev->psp_sysfs_en = true;
3885 * Register gpu instance before amdgpu_device_enable_mgpu_fan_boost.
3886 * Otherwise the mgpu fan boost feature will be skipped due to the
3887 * gpu instance is counted less.
3889 amdgpu_register_gpu_instance(adev);
3891 /* enable clockgating, etc. after ib tests, etc. since some blocks require
3892 * explicit gating rather than handling it automatically.
3894 if (!adev->gmc.xgmi.pending_reset) {
3895 r = amdgpu_device_ip_late_init(adev);
3897 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
3898 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
3899 goto release_ras_con;
3902 amdgpu_ras_resume(adev);
3903 queue_delayed_work(system_wq, &adev->delayed_init_work,
3904 msecs_to_jiffies(AMDGPU_RESUME_MS));
3907 if (amdgpu_sriov_vf(adev))
3908 flush_delayed_work(&adev->delayed_init_work);
3910 r = sysfs_create_files(&adev->dev->kobj, amdgpu_dev_attributes);
3912 dev_err(adev->dev, "Could not create amdgpu device attr\n");
3914 if (IS_ENABLED(CONFIG_PERF_EVENTS))
3915 r = amdgpu_pmu_init(adev);
3917 dev_err(adev->dev, "amdgpu_pmu_init failed\n");
3919 /* Have stored pci confspace at hand for restore in sudden PCI error */
3920 if (amdgpu_device_cache_pci_state(adev->pdev))
3921 pci_restore_state(pdev);
3923 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
3924 /* this will fail for cards that aren't VGA class devices, just
3926 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
3927 vga_client_register(adev->pdev, amdgpu_device_vga_set_decode);
3929 px = amdgpu_device_supports_px(ddev);
3931 if (px || (!pci_is_thunderbolt_attached(adev->pdev) &&
3932 apple_gmux_detect(NULL, NULL)))
3933 vga_switcheroo_register_client(adev->pdev,
3934 &amdgpu_switcheroo_ops, px);
3937 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
3939 if (adev->gmc.xgmi.pending_reset)
3940 queue_delayed_work(system_wq, &mgpu_info.delayed_reset_work,
3941 msecs_to_jiffies(AMDGPU_RESUME_MS));
3943 amdgpu_device_check_iommu_direct_map(adev);
3948 amdgpu_release_ras_context(adev);
3951 amdgpu_vf_error_trans_all(adev);
3956 static void amdgpu_device_unmap_mmio(struct amdgpu_device *adev)
3959 /* Clear all CPU mappings pointing to this device */
3960 unmap_mapping_range(adev->ddev.anon_inode->i_mapping, 0, 0, 1);
3962 /* Unmap all mapped bars - Doorbell, registers and VRAM */
3963 amdgpu_device_doorbell_fini(adev);
3965 iounmap(adev->rmmio);
3967 if (adev->mman.aper_base_kaddr)
3968 iounmap(adev->mman.aper_base_kaddr);
3969 adev->mman.aper_base_kaddr = NULL;
3971 /* Memory manager related */
3972 if (!adev->gmc.xgmi.connected_to_cpu) {
3973 arch_phys_wc_del(adev->gmc.vram_mtrr);
3974 arch_io_free_memtype_wc(adev->gmc.aper_base, adev->gmc.aper_size);
3979 * amdgpu_device_fini_hw - tear down the driver
3981 * @adev: amdgpu_device pointer
3983 * Tear down the driver info (all asics).
3984 * Called at driver shutdown.
3986 void amdgpu_device_fini_hw(struct amdgpu_device *adev)
3988 dev_info(adev->dev, "amdgpu: finishing device.\n");
3989 flush_delayed_work(&adev->delayed_init_work);
3990 adev->shutdown = true;
3992 /* make sure IB test finished before entering exclusive mode
3993 * to avoid preemption on IB test
3995 if (amdgpu_sriov_vf(adev)) {
3996 amdgpu_virt_request_full_gpu(adev, false);
3997 amdgpu_virt_fini_data_exchange(adev);
4000 /* disable all interrupts */
4001 amdgpu_irq_disable_all(adev);
4002 if (adev->mode_info.mode_config_initialized){
4003 if (!drm_drv_uses_atomic_modeset(adev_to_drm(adev)))
4004 drm_helper_force_disable_all(adev_to_drm(adev));
4006 drm_atomic_helper_shutdown(adev_to_drm(adev));
4008 amdgpu_fence_driver_hw_fini(adev);
4010 if (adev->mman.initialized)
4011 drain_workqueue(adev->mman.bdev.wq);
4013 if (adev->pm.sysfs_initialized)
4014 amdgpu_pm_sysfs_fini(adev);
4015 if (adev->ucode_sysfs_en)
4016 amdgpu_ucode_sysfs_fini(adev);
4017 if (adev->psp_sysfs_en)
4018 amdgpu_psp_sysfs_fini(adev);
4019 sysfs_remove_files(&adev->dev->kobj, amdgpu_dev_attributes);
4021 /* disable ras feature must before hw fini */
4022 amdgpu_ras_pre_fini(adev);
4024 amdgpu_device_ip_fini_early(adev);
4026 amdgpu_irq_fini_hw(adev);
4028 if (adev->mman.initialized)
4029 ttm_device_clear_dma_mappings(&adev->mman.bdev);
4031 amdgpu_gart_dummy_page_fini(adev);
4033 if (drm_dev_is_unplugged(adev_to_drm(adev)))
4034 amdgpu_device_unmap_mmio(adev);
4038 void amdgpu_device_fini_sw(struct amdgpu_device *adev)
4043 amdgpu_fence_driver_sw_fini(adev);
4044 amdgpu_device_ip_fini(adev);
4045 amdgpu_ucode_release(&adev->firmware.gpu_info_fw);
4046 adev->accel_working = false;
4047 dma_fence_put(rcu_dereference_protected(adev->gang_submit, true));
4049 amdgpu_reset_fini(adev);
4051 /* free i2c buses */
4052 if (!amdgpu_device_has_dc_support(adev))
4053 amdgpu_i2c_fini(adev);
4055 if (amdgpu_emu_mode != 1)
4056 amdgpu_atombios_fini(adev);
4061 px = amdgpu_device_supports_px(adev_to_drm(adev));
4063 if (px || (!pci_is_thunderbolt_attached(adev->pdev) &&
4064 apple_gmux_detect(NULL, NULL)))
4065 vga_switcheroo_unregister_client(adev->pdev);
4068 vga_switcheroo_fini_domain_pm_ops(adev->dev);
4070 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
4071 vga_client_unregister(adev->pdev);
4073 if (drm_dev_enter(adev_to_drm(adev), &idx)) {
4075 iounmap(adev->rmmio);
4077 amdgpu_device_doorbell_fini(adev);
4081 if (IS_ENABLED(CONFIG_PERF_EVENTS))
4082 amdgpu_pmu_fini(adev);
4083 if (adev->mman.discovery_bin)
4084 amdgpu_discovery_fini(adev);
4086 amdgpu_reset_put_reset_domain(adev->reset_domain);
4087 adev->reset_domain = NULL;
4089 kfree(adev->pci_state);
4094 * amdgpu_device_evict_resources - evict device resources
4095 * @adev: amdgpu device object
4097 * Evicts all ttm device resources(vram BOs, gart table) from the lru list
4098 * of the vram memory type. Mainly used for evicting device resources
4102 static int amdgpu_device_evict_resources(struct amdgpu_device *adev)
4106 /* No need to evict vram on APUs for suspend to ram or s2idle */
4107 if ((adev->in_s3 || adev->in_s0ix) && (adev->flags & AMD_IS_APU))
4110 ret = amdgpu_ttm_evict_resources(adev, TTM_PL_VRAM);
4112 DRM_WARN("evicting device resources failed\n");
4120 * amdgpu_device_suspend - initiate device suspend
4122 * @dev: drm dev pointer
4123 * @fbcon : notify the fbdev of suspend
4125 * Puts the hw in the suspend state (all asics).
4126 * Returns 0 for success or an error on failure.
4127 * Called at driver suspend.
4129 int amdgpu_device_suspend(struct drm_device *dev, bool fbcon)
4131 struct amdgpu_device *adev = drm_to_adev(dev);
4134 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
4137 adev->in_suspend = true;
4139 /* Evict the majority of BOs before grabbing the full access */
4140 r = amdgpu_device_evict_resources(adev);
4144 if (amdgpu_sriov_vf(adev)) {
4145 amdgpu_virt_fini_data_exchange(adev);
4146 r = amdgpu_virt_request_full_gpu(adev, false);
4151 if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DEV_D3))
4152 DRM_WARN("smart shift update failed\n");
4154 drm_kms_helper_poll_disable(dev);
4157 drm_fb_helper_set_suspend_unlocked(adev_to_drm(adev)->fb_helper, true);
4159 cancel_delayed_work_sync(&adev->delayed_init_work);
4161 amdgpu_ras_suspend(adev);
4163 amdgpu_device_ip_suspend_phase1(adev);
4166 amdgpu_amdkfd_suspend(adev, adev->in_runpm);
4168 r = amdgpu_device_evict_resources(adev);
4172 amdgpu_fence_driver_hw_fini(adev);
4174 amdgpu_device_ip_suspend_phase2(adev);
4176 if (amdgpu_sriov_vf(adev))
4177 amdgpu_virt_release_full_gpu(adev, false);
4183 * amdgpu_device_resume - initiate device resume
4185 * @dev: drm dev pointer
4186 * @fbcon : notify the fbdev of resume
4188 * Bring the hw back to operating state (all asics).
4189 * Returns 0 for success or an error on failure.
4190 * Called at driver resume.
4192 int amdgpu_device_resume(struct drm_device *dev, bool fbcon)
4194 struct amdgpu_device *adev = drm_to_adev(dev);
4197 if (amdgpu_sriov_vf(adev)) {
4198 r = amdgpu_virt_request_full_gpu(adev, true);
4203 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
4207 amdgpu_dpm_gfx_state_change(adev, sGpuChangeState_D0Entry);
4210 if (amdgpu_device_need_post(adev)) {
4211 r = amdgpu_device_asic_init(adev);
4213 dev_err(adev->dev, "amdgpu asic init failed\n");
4216 r = amdgpu_device_ip_resume(adev);
4219 dev_err(adev->dev, "amdgpu_device_ip_resume failed (%d).\n", r);
4222 amdgpu_fence_driver_hw_init(adev);
4224 r = amdgpu_device_ip_late_init(adev);
4228 queue_delayed_work(system_wq, &adev->delayed_init_work,
4229 msecs_to_jiffies(AMDGPU_RESUME_MS));
4231 if (!adev->in_s0ix) {
4232 r = amdgpu_amdkfd_resume(adev, adev->in_runpm);
4238 if (amdgpu_sriov_vf(adev)) {
4239 amdgpu_virt_init_data_exchange(adev);
4240 amdgpu_virt_release_full_gpu(adev, true);
4246 /* Make sure IB tests flushed */
4247 flush_delayed_work(&adev->delayed_init_work);
4250 drm_fb_helper_set_suspend_unlocked(adev_to_drm(adev)->fb_helper, false);
4252 drm_kms_helper_poll_enable(dev);
4254 amdgpu_ras_resume(adev);
4256 if (adev->mode_info.num_crtc) {
4258 * Most of the connector probing functions try to acquire runtime pm
4259 * refs to ensure that the GPU is powered on when connector polling is
4260 * performed. Since we're calling this from a runtime PM callback,
4261 * trying to acquire rpm refs will cause us to deadlock.
4263 * Since we're guaranteed to be holding the rpm lock, it's safe to
4264 * temporarily disable the rpm helpers so this doesn't deadlock us.
4267 dev->dev->power.disable_depth++;
4269 if (!adev->dc_enabled)
4270 drm_helper_hpd_irq_event(dev);
4272 drm_kms_helper_hotplug_event(dev);
4274 dev->dev->power.disable_depth--;
4277 adev->in_suspend = false;
4279 if (adev->enable_mes)
4280 amdgpu_mes_self_test(adev);
4282 if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DEV_D0))
4283 DRM_WARN("smart shift update failed\n");
4289 * amdgpu_device_ip_check_soft_reset - did soft reset succeed
4291 * @adev: amdgpu_device pointer
4293 * The list of all the hardware IPs that make up the asic is walked and
4294 * the check_soft_reset callbacks are run. check_soft_reset determines
4295 * if the asic is still hung or not.
4296 * Returns true if any of the IPs are still in a hung state, false if not.
4298 static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
4301 bool asic_hang = false;
4303 if (amdgpu_sriov_vf(adev))
4306 if (amdgpu_asic_need_full_reset(adev))
4309 for (i = 0; i < adev->num_ip_blocks; i++) {
4310 if (!adev->ip_blocks[i].status.valid)
4312 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
4313 adev->ip_blocks[i].status.hang =
4314 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
4315 if (adev->ip_blocks[i].status.hang) {
4316 dev_info(adev->dev, "IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
4324 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
4326 * @adev: amdgpu_device pointer
4328 * The list of all the hardware IPs that make up the asic is walked and the
4329 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
4330 * handles any IP specific hardware or software state changes that are
4331 * necessary for a soft reset to succeed.
4332 * Returns 0 on success, negative error code on failure.
4334 static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
4338 for (i = 0; i < adev->num_ip_blocks; i++) {
4339 if (!adev->ip_blocks[i].status.valid)
4341 if (adev->ip_blocks[i].status.hang &&
4342 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
4343 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
4353 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
4355 * @adev: amdgpu_device pointer
4357 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
4358 * reset is necessary to recover.
4359 * Returns true if a full asic reset is required, false if not.
4361 static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
4365 if (amdgpu_asic_need_full_reset(adev))
4368 for (i = 0; i < adev->num_ip_blocks; i++) {
4369 if (!adev->ip_blocks[i].status.valid)
4371 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
4372 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
4373 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
4374 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
4375 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
4376 if (adev->ip_blocks[i].status.hang) {
4377 dev_info(adev->dev, "Some block need full reset!\n");
4386 * amdgpu_device_ip_soft_reset - do a soft reset
4388 * @adev: amdgpu_device pointer
4390 * The list of all the hardware IPs that make up the asic is walked and the
4391 * soft_reset callbacks are run if the block is hung. soft_reset handles any
4392 * IP specific hardware or software state changes that are necessary to soft
4394 * Returns 0 on success, negative error code on failure.
4396 static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
4400 for (i = 0; i < adev->num_ip_blocks; i++) {
4401 if (!adev->ip_blocks[i].status.valid)
4403 if (adev->ip_blocks[i].status.hang &&
4404 adev->ip_blocks[i].version->funcs->soft_reset) {
4405 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
4415 * amdgpu_device_ip_post_soft_reset - clean up from soft reset
4417 * @adev: amdgpu_device pointer
4419 * The list of all the hardware IPs that make up the asic is walked and the
4420 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
4421 * handles any IP specific hardware or software state changes that are
4422 * necessary after the IP has been soft reset.
4423 * Returns 0 on success, negative error code on failure.
4425 static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
4429 for (i = 0; i < adev->num_ip_blocks; i++) {
4430 if (!adev->ip_blocks[i].status.valid)
4432 if (adev->ip_blocks[i].status.hang &&
4433 adev->ip_blocks[i].version->funcs->post_soft_reset)
4434 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
4443 * amdgpu_device_recover_vram - Recover some VRAM contents
4445 * @adev: amdgpu_device pointer
4447 * Restores the contents of VRAM buffers from the shadows in GTT. Used to
4448 * restore things like GPUVM page tables after a GPU reset where
4449 * the contents of VRAM might be lost.
4452 * 0 on success, negative error code on failure.
4454 static int amdgpu_device_recover_vram(struct amdgpu_device *adev)
4456 struct dma_fence *fence = NULL, *next = NULL;
4457 struct amdgpu_bo *shadow;
4458 struct amdgpu_bo_vm *vmbo;
4461 if (amdgpu_sriov_runtime(adev))
4462 tmo = msecs_to_jiffies(8000);
4464 tmo = msecs_to_jiffies(100);
4466 dev_info(adev->dev, "recover vram bo from shadow start\n");
4467 mutex_lock(&adev->shadow_list_lock);
4468 list_for_each_entry(vmbo, &adev->shadow_list, shadow_list) {
4470 /* No need to recover an evicted BO */
4471 if (shadow->tbo.resource->mem_type != TTM_PL_TT ||
4472 shadow->tbo.resource->start == AMDGPU_BO_INVALID_OFFSET ||
4473 shadow->parent->tbo.resource->mem_type != TTM_PL_VRAM)
4476 r = amdgpu_bo_restore_shadow(shadow, &next);
4481 tmo = dma_fence_wait_timeout(fence, false, tmo);
4482 dma_fence_put(fence);
4487 } else if (tmo < 0) {
4495 mutex_unlock(&adev->shadow_list_lock);
4498 tmo = dma_fence_wait_timeout(fence, false, tmo);
4499 dma_fence_put(fence);
4501 if (r < 0 || tmo <= 0) {
4502 dev_err(adev->dev, "recover vram bo from shadow failed, r is %ld, tmo is %ld\n", r, tmo);
4506 dev_info(adev->dev, "recover vram bo from shadow done\n");
4512 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
4514 * @adev: amdgpu_device pointer
4515 * @from_hypervisor: request from hypervisor
4517 * do VF FLR and reinitialize Asic
4518 * return 0 means succeeded otherwise failed
4520 static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
4521 bool from_hypervisor)
4524 struct amdgpu_hive_info *hive = NULL;
4525 int retry_limit = 0;
4528 amdgpu_amdkfd_pre_reset(adev);
4530 if (from_hypervisor)
4531 r = amdgpu_virt_request_full_gpu(adev, true);
4533 r = amdgpu_virt_reset_gpu(adev);
4537 /* Resume IP prior to SMC */
4538 r = amdgpu_device_ip_reinit_early_sriov(adev);
4542 amdgpu_virt_init_data_exchange(adev);
4544 r = amdgpu_device_fw_loading(adev);
4548 /* now we are okay to resume SMC/CP/SDMA */
4549 r = amdgpu_device_ip_reinit_late_sriov(adev);
4553 hive = amdgpu_get_xgmi_hive(adev);
4554 /* Update PSP FW topology after reset */
4555 if (hive && adev->gmc.xgmi.num_physical_nodes > 1)
4556 r = amdgpu_xgmi_update_topology(hive, adev);
4559 amdgpu_put_xgmi_hive(hive);
4562 amdgpu_irq_gpu_reset_resume_helper(adev);
4563 r = amdgpu_ib_ring_tests(adev);
4565 amdgpu_amdkfd_post_reset(adev);
4569 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
4570 amdgpu_inc_vram_lost(adev);
4571 r = amdgpu_device_recover_vram(adev);
4573 amdgpu_virt_release_full_gpu(adev, true);
4575 if (AMDGPU_RETRY_SRIOV_RESET(r)) {
4576 if (retry_limit < AMDGPU_MAX_RETRY_LIMIT) {
4580 DRM_ERROR("GPU reset retry is beyond the retry limit\n");
4587 * amdgpu_device_has_job_running - check if there is any job in mirror list
4589 * @adev: amdgpu_device pointer
4591 * check if there is any job in mirror list
4593 bool amdgpu_device_has_job_running(struct amdgpu_device *adev)
4596 struct drm_sched_job *job;
4598 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4599 struct amdgpu_ring *ring = adev->rings[i];
4601 if (!ring || !ring->sched.thread)
4604 spin_lock(&ring->sched.job_list_lock);
4605 job = list_first_entry_or_null(&ring->sched.pending_list,
4606 struct drm_sched_job, list);
4607 spin_unlock(&ring->sched.job_list_lock);
4615 * amdgpu_device_should_recover_gpu - check if we should try GPU recovery
4617 * @adev: amdgpu_device pointer
4619 * Check amdgpu_gpu_recovery and SRIOV status to see if we should try to recover
4622 bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev)
4625 if (amdgpu_gpu_recovery == 0)
4628 /* Skip soft reset check in fatal error mode */
4629 if (!amdgpu_ras_is_poison_mode_supported(adev))
4632 if (amdgpu_sriov_vf(adev))
4635 if (amdgpu_gpu_recovery == -1) {
4636 switch (adev->asic_type) {
4637 #ifdef CONFIG_DRM_AMDGPU_SI
4644 #ifdef CONFIG_DRM_AMDGPU_CIK
4651 case CHIP_CYAN_SKILLFISH:
4661 dev_info(adev->dev, "GPU recovery disabled.\n");
4665 int amdgpu_device_mode1_reset(struct amdgpu_device *adev)
4670 amdgpu_atombios_scratch_regs_engine_hung(adev, true);
4672 dev_info(adev->dev, "GPU mode1 reset\n");
4675 pci_clear_master(adev->pdev);
4677 amdgpu_device_cache_pci_state(adev->pdev);
4679 if (amdgpu_dpm_is_mode1_reset_supported(adev)) {
4680 dev_info(adev->dev, "GPU smu mode1 reset\n");
4681 ret = amdgpu_dpm_mode1_reset(adev);
4683 dev_info(adev->dev, "GPU psp mode1 reset\n");
4684 ret = psp_gpu_reset(adev);
4688 dev_err(adev->dev, "GPU mode1 reset failed\n");
4690 amdgpu_device_load_pci_state(adev->pdev);
4692 /* wait for asic to come out of reset */
4693 for (i = 0; i < adev->usec_timeout; i++) {
4694 u32 memsize = adev->nbio.funcs->get_memsize(adev);
4696 if (memsize != 0xffffffff)
4701 amdgpu_atombios_scratch_regs_engine_hung(adev, false);
4705 int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
4706 struct amdgpu_reset_context *reset_context)
4709 struct amdgpu_job *job = NULL;
4710 bool need_full_reset =
4711 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4713 if (reset_context->reset_req_dev == adev)
4714 job = reset_context->job;
4716 if (amdgpu_sriov_vf(adev)) {
4717 /* stop the data exchange thread */
4718 amdgpu_virt_fini_data_exchange(adev);
4721 amdgpu_fence_driver_isr_toggle(adev, true);
4723 /* block all schedulers and reset given job's ring */
4724 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4725 struct amdgpu_ring *ring = adev->rings[i];
4727 if (!ring || !ring->sched.thread)
4730 /*clear job fence from fence drv to avoid force_completion
4731 *leave NULL and vm flush fence in fence drv */
4732 amdgpu_fence_driver_clear_job_fences(ring);
4734 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
4735 amdgpu_fence_driver_force_completion(ring);
4738 amdgpu_fence_driver_isr_toggle(adev, false);
4741 drm_sched_increase_karma(&job->base);
4743 r = amdgpu_reset_prepare_hwcontext(adev, reset_context);
4744 /* If reset handler not implemented, continue; otherwise return */
4750 /* Don't suspend on bare metal if we are not going to HW reset the ASIC */
4751 if (!amdgpu_sriov_vf(adev)) {
4753 if (!need_full_reset)
4754 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
4756 if (!need_full_reset && amdgpu_gpu_recovery &&
4757 amdgpu_device_ip_check_soft_reset(adev)) {
4758 amdgpu_device_ip_pre_soft_reset(adev);
4759 r = amdgpu_device_ip_soft_reset(adev);
4760 amdgpu_device_ip_post_soft_reset(adev);
4761 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
4762 dev_info(adev->dev, "soft reset failed, will fallback to full reset!\n");
4763 need_full_reset = true;
4767 if (need_full_reset)
4768 r = amdgpu_device_ip_suspend(adev);
4769 if (need_full_reset)
4770 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4772 clear_bit(AMDGPU_NEED_FULL_RESET,
4773 &reset_context->flags);
4779 static int amdgpu_reset_reg_dumps(struct amdgpu_device *adev)
4783 lockdep_assert_held(&adev->reset_domain->sem);
4785 for (i = 0; i < adev->num_regs; i++) {
4786 adev->reset_dump_reg_value[i] = RREG32(adev->reset_dump_reg_list[i]);
4787 trace_amdgpu_reset_reg_dumps(adev->reset_dump_reg_list[i],
4788 adev->reset_dump_reg_value[i]);
4794 #ifdef CONFIG_DEV_COREDUMP
4795 static ssize_t amdgpu_devcoredump_read(char *buffer, loff_t offset,
4796 size_t count, void *data, size_t datalen)
4798 struct drm_printer p;
4799 struct amdgpu_device *adev = data;
4800 struct drm_print_iterator iter;
4805 iter.start = offset;
4806 iter.remain = count;
4808 p = drm_coredump_printer(&iter);
4810 drm_printf(&p, "**** AMDGPU Device Coredump ****\n");
4811 drm_printf(&p, "kernel: " UTS_RELEASE "\n");
4812 drm_printf(&p, "module: " KBUILD_MODNAME "\n");
4813 drm_printf(&p, "time: %lld.%09ld\n", adev->reset_time.tv_sec, adev->reset_time.tv_nsec);
4814 if (adev->reset_task_info.pid)
4815 drm_printf(&p, "process_name: %s PID: %d\n",
4816 adev->reset_task_info.process_name,
4817 adev->reset_task_info.pid);
4819 if (adev->reset_vram_lost)
4820 drm_printf(&p, "VRAM is lost due to GPU reset!\n");
4821 if (adev->num_regs) {
4822 drm_printf(&p, "AMDGPU register dumps:\nOffset: Value:\n");
4824 for (i = 0; i < adev->num_regs; i++)
4825 drm_printf(&p, "0x%08x: 0x%08x\n",
4826 adev->reset_dump_reg_list[i],
4827 adev->reset_dump_reg_value[i]);
4830 return count - iter.remain;
4833 static void amdgpu_devcoredump_free(void *data)
4837 static void amdgpu_reset_capture_coredumpm(struct amdgpu_device *adev)
4839 struct drm_device *dev = adev_to_drm(adev);
4841 ktime_get_ts64(&adev->reset_time);
4842 dev_coredumpm(dev->dev, THIS_MODULE, adev, 0, GFP_KERNEL,
4843 amdgpu_devcoredump_read, amdgpu_devcoredump_free);
4847 int amdgpu_do_asic_reset(struct list_head *device_list_handle,
4848 struct amdgpu_reset_context *reset_context)
4850 struct amdgpu_device *tmp_adev = NULL;
4851 bool need_full_reset, skip_hw_reset, vram_lost = false;
4853 bool gpu_reset_for_dev_remove = 0;
4855 /* Try reset handler method first */
4856 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
4858 amdgpu_reset_reg_dumps(tmp_adev);
4860 reset_context->reset_device_list = device_list_handle;
4861 r = amdgpu_reset_perform_reset(tmp_adev, reset_context);
4862 /* If reset handler not implemented, continue; otherwise return */
4868 /* Reset handler not implemented, use the default method */
4870 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4871 skip_hw_reset = test_bit(AMDGPU_SKIP_HW_RESET, &reset_context->flags);
4873 gpu_reset_for_dev_remove =
4874 test_bit(AMDGPU_RESET_FOR_DEVICE_REMOVE, &reset_context->flags) &&
4875 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4878 * ASIC reset has to be done on all XGMI hive nodes ASAP
4879 * to allow proper links negotiation in FW (within 1 sec)
4881 if (!skip_hw_reset && need_full_reset) {
4882 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4883 /* For XGMI run all resets in parallel to speed up the process */
4884 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
4885 tmp_adev->gmc.xgmi.pending_reset = false;
4886 if (!queue_work(system_unbound_wq, &tmp_adev->xgmi_reset_work))
4889 r = amdgpu_asic_reset(tmp_adev);
4892 dev_err(tmp_adev->dev, "ASIC reset failed with error, %d for drm dev, %s",
4893 r, adev_to_drm(tmp_adev)->unique);
4898 /* For XGMI wait for all resets to complete before proceed */
4900 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4901 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
4902 flush_work(&tmp_adev->xgmi_reset_work);
4903 r = tmp_adev->asic_reset_res;
4911 if (!r && amdgpu_ras_intr_triggered()) {
4912 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4913 if (tmp_adev->mmhub.ras && tmp_adev->mmhub.ras->ras_block.hw_ops &&
4914 tmp_adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count)
4915 tmp_adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count(tmp_adev);
4918 amdgpu_ras_intr_cleared();
4921 /* Since the mode1 reset affects base ip blocks, the
4922 * phase1 ip blocks need to be resumed. Otherwise there
4923 * will be a BIOS signature error and the psp bootloader
4924 * can't load kdb on the next amdgpu install.
4926 if (gpu_reset_for_dev_remove) {
4927 list_for_each_entry(tmp_adev, device_list_handle, reset_list)
4928 amdgpu_device_ip_resume_phase1(tmp_adev);
4933 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4934 if (need_full_reset) {
4936 r = amdgpu_device_asic_init(tmp_adev);
4938 dev_warn(tmp_adev->dev, "asic atom init failed!");
4940 dev_info(tmp_adev->dev, "GPU reset succeeded, trying to resume\n");
4941 r = amdgpu_amdkfd_resume_iommu(tmp_adev);
4945 r = amdgpu_device_ip_resume_phase1(tmp_adev);
4949 vram_lost = amdgpu_device_check_vram_lost(tmp_adev);
4950 #ifdef CONFIG_DEV_COREDUMP
4951 tmp_adev->reset_vram_lost = vram_lost;
4952 memset(&tmp_adev->reset_task_info, 0,
4953 sizeof(tmp_adev->reset_task_info));
4954 if (reset_context->job && reset_context->job->vm)
4955 tmp_adev->reset_task_info =
4956 reset_context->job->vm->task_info;
4957 amdgpu_reset_capture_coredumpm(tmp_adev);
4960 DRM_INFO("VRAM is lost due to GPU reset!\n");
4961 amdgpu_inc_vram_lost(tmp_adev);
4964 r = amdgpu_device_fw_loading(tmp_adev);
4968 r = amdgpu_device_ip_resume_phase2(tmp_adev);
4973 amdgpu_device_fill_reset_magic(tmp_adev);
4976 * Add this ASIC as tracked as reset was already
4977 * complete successfully.
4979 amdgpu_register_gpu_instance(tmp_adev);
4981 if (!reset_context->hive &&
4982 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
4983 amdgpu_xgmi_add_device(tmp_adev);
4985 r = amdgpu_device_ip_late_init(tmp_adev);
4989 drm_fb_helper_set_suspend_unlocked(adev_to_drm(tmp_adev)->fb_helper, false);
4992 * The GPU enters bad state once faulty pages
4993 * by ECC has reached the threshold, and ras
4994 * recovery is scheduled next. So add one check
4995 * here to break recovery if it indeed exceeds
4996 * bad page threshold, and remind user to
4997 * retire this GPU or setting one bigger
4998 * bad_page_threshold value to fix this once
4999 * probing driver again.
5001 if (!amdgpu_ras_eeprom_check_err_threshold(tmp_adev)) {
5003 amdgpu_ras_resume(tmp_adev);
5009 /* Update PSP FW topology after reset */
5010 if (reset_context->hive &&
5011 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
5012 r = amdgpu_xgmi_update_topology(
5013 reset_context->hive, tmp_adev);
5019 amdgpu_irq_gpu_reset_resume_helper(tmp_adev);
5020 r = amdgpu_ib_ring_tests(tmp_adev);
5022 dev_err(tmp_adev->dev, "ib ring test failed (%d).\n", r);
5023 need_full_reset = true;
5030 r = amdgpu_device_recover_vram(tmp_adev);
5032 tmp_adev->asic_reset_res = r;
5036 if (need_full_reset)
5037 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
5039 clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
5043 static void amdgpu_device_set_mp1_state(struct amdgpu_device *adev)
5046 switch (amdgpu_asic_reset_method(adev)) {
5047 case AMD_RESET_METHOD_MODE1:
5048 adev->mp1_state = PP_MP1_STATE_SHUTDOWN;
5050 case AMD_RESET_METHOD_MODE2:
5051 adev->mp1_state = PP_MP1_STATE_RESET;
5054 adev->mp1_state = PP_MP1_STATE_NONE;
5059 static void amdgpu_device_unset_mp1_state(struct amdgpu_device *adev)
5061 amdgpu_vf_error_trans_all(adev);
5062 adev->mp1_state = PP_MP1_STATE_NONE;
5065 static void amdgpu_device_resume_display_audio(struct amdgpu_device *adev)
5067 struct pci_dev *p = NULL;
5069 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
5070 adev->pdev->bus->number, 1);
5072 pm_runtime_enable(&(p->dev));
5073 pm_runtime_resume(&(p->dev));
5079 static int amdgpu_device_suspend_display_audio(struct amdgpu_device *adev)
5081 enum amd_reset_method reset_method;
5082 struct pci_dev *p = NULL;
5086 * For now, only BACO and mode1 reset are confirmed
5087 * to suffer the audio issue without proper suspended.
5089 reset_method = amdgpu_asic_reset_method(adev);
5090 if ((reset_method != AMD_RESET_METHOD_BACO) &&
5091 (reset_method != AMD_RESET_METHOD_MODE1))
5094 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
5095 adev->pdev->bus->number, 1);
5099 expires = pm_runtime_autosuspend_expiration(&(p->dev));
5102 * If we cannot get the audio device autosuspend delay,
5103 * a fixed 4S interval will be used. Considering 3S is
5104 * the audio controller default autosuspend delay setting.
5105 * 4S used here is guaranteed to cover that.
5107 expires = ktime_get_mono_fast_ns() + NSEC_PER_SEC * 4ULL;
5109 while (!pm_runtime_status_suspended(&(p->dev))) {
5110 if (!pm_runtime_suspend(&(p->dev)))
5113 if (expires < ktime_get_mono_fast_ns()) {
5114 dev_warn(adev->dev, "failed to suspend display audio\n");
5116 /* TODO: abort the succeeding gpu reset? */
5121 pm_runtime_disable(&(p->dev));
5127 static inline void amdgpu_device_stop_pending_resets(struct amdgpu_device *adev)
5129 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
5131 #if defined(CONFIG_DEBUG_FS)
5132 if (!amdgpu_sriov_vf(adev))
5133 cancel_work(&adev->reset_work);
5137 cancel_work(&adev->kfd.reset_work);
5139 if (amdgpu_sriov_vf(adev))
5140 cancel_work(&adev->virt.flr_work);
5142 if (con && adev->ras_enabled)
5143 cancel_work(&con->recovery_work);
5148 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
5150 * @adev: amdgpu_device pointer
5151 * @job: which job trigger hang
5153 * Attempt to reset the GPU if it has hung (all asics).
5154 * Attempt to do soft-reset or full-reset and reinitialize Asic
5155 * Returns 0 for success or an error on failure.
5158 int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
5159 struct amdgpu_job *job,
5160 struct amdgpu_reset_context *reset_context)
5162 struct list_head device_list, *device_list_handle = NULL;
5163 bool job_signaled = false;
5164 struct amdgpu_hive_info *hive = NULL;
5165 struct amdgpu_device *tmp_adev = NULL;
5167 bool need_emergency_restart = false;
5168 bool audio_suspended = false;
5169 bool gpu_reset_for_dev_remove = false;
5171 gpu_reset_for_dev_remove =
5172 test_bit(AMDGPU_RESET_FOR_DEVICE_REMOVE, &reset_context->flags) &&
5173 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
5176 * Special case: RAS triggered and full reset isn't supported
5178 need_emergency_restart = amdgpu_ras_need_emergency_restart(adev);
5181 * Flush RAM to disk so that after reboot
5182 * the user can read log and see why the system rebooted.
5184 if (need_emergency_restart && amdgpu_ras_get_context(adev)->reboot) {
5185 DRM_WARN("Emergency reboot.");
5188 emergency_restart();
5191 dev_info(adev->dev, "GPU %s begin!\n",
5192 need_emergency_restart ? "jobs stop":"reset");
5194 if (!amdgpu_sriov_vf(adev))
5195 hive = amdgpu_get_xgmi_hive(adev);
5197 mutex_lock(&hive->hive_lock);
5199 reset_context->job = job;
5200 reset_context->hive = hive;
5202 * Build list of devices to reset.
5203 * In case we are in XGMI hive mode, resort the device list
5204 * to put adev in the 1st position.
5206 INIT_LIST_HEAD(&device_list);
5207 if (!amdgpu_sriov_vf(adev) && (adev->gmc.xgmi.num_physical_nodes > 1)) {
5208 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
5209 list_add_tail(&tmp_adev->reset_list, &device_list);
5210 if (gpu_reset_for_dev_remove && adev->shutdown)
5211 tmp_adev->shutdown = true;
5213 if (!list_is_first(&adev->reset_list, &device_list))
5214 list_rotate_to_front(&adev->reset_list, &device_list);
5215 device_list_handle = &device_list;
5217 list_add_tail(&adev->reset_list, &device_list);
5218 device_list_handle = &device_list;
5221 /* We need to lock reset domain only once both for XGMI and single device */
5222 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
5224 amdgpu_device_lock_reset_domain(tmp_adev->reset_domain);
5226 /* block all schedulers and reset given job's ring */
5227 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5229 amdgpu_device_set_mp1_state(tmp_adev);
5232 * Try to put the audio codec into suspend state
5233 * before gpu reset started.
5235 * Due to the power domain of the graphics device
5236 * is shared with AZ power domain. Without this,
5237 * we may change the audio hardware from behind
5238 * the audio driver's back. That will trigger
5239 * some audio codec errors.
5241 if (!amdgpu_device_suspend_display_audio(tmp_adev))
5242 audio_suspended = true;
5244 amdgpu_ras_set_error_query_ready(tmp_adev, false);
5246 cancel_delayed_work_sync(&tmp_adev->delayed_init_work);
5248 if (!amdgpu_sriov_vf(tmp_adev))
5249 amdgpu_amdkfd_pre_reset(tmp_adev);
5252 * Mark these ASICs to be reseted as untracked first
5253 * And add them back after reset completed
5255 amdgpu_unregister_gpu_instance(tmp_adev);
5257 drm_fb_helper_set_suspend_unlocked(adev_to_drm(tmp_adev)->fb_helper, true);
5259 /* disable ras on ALL IPs */
5260 if (!need_emergency_restart &&
5261 amdgpu_device_ip_need_full_reset(tmp_adev))
5262 amdgpu_ras_suspend(tmp_adev);
5264 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5265 struct amdgpu_ring *ring = tmp_adev->rings[i];
5267 if (!ring || !ring->sched.thread)
5270 drm_sched_stop(&ring->sched, job ? &job->base : NULL);
5272 if (need_emergency_restart)
5273 amdgpu_job_stop_all_jobs_on_sched(&ring->sched);
5275 atomic_inc(&tmp_adev->gpu_reset_counter);
5278 if (need_emergency_restart)
5279 goto skip_sched_resume;
5282 * Must check guilty signal here since after this point all old
5283 * HW fences are force signaled.
5285 * job->base holds a reference to parent fence
5287 if (job && dma_fence_is_signaled(&job->hw_fence)) {
5288 job_signaled = true;
5289 dev_info(adev->dev, "Guilty job already signaled, skipping HW reset");
5293 retry: /* Rest of adevs pre asic reset from XGMI hive. */
5294 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5295 if (gpu_reset_for_dev_remove) {
5296 /* Workaroud for ASICs need to disable SMC first */
5297 amdgpu_device_smu_fini_early(tmp_adev);
5299 r = amdgpu_device_pre_asic_reset(tmp_adev, reset_context);
5300 /*TODO Should we stop ?*/
5302 dev_err(tmp_adev->dev, "GPU pre asic reset failed with err, %d for drm dev, %s ",
5303 r, adev_to_drm(tmp_adev)->unique);
5304 tmp_adev->asic_reset_res = r;
5308 * Drop all pending non scheduler resets. Scheduler resets
5309 * were already dropped during drm_sched_stop
5311 amdgpu_device_stop_pending_resets(tmp_adev);
5314 /* Actual ASIC resets if needed.*/
5315 /* Host driver will handle XGMI hive reset for SRIOV */
5316 if (amdgpu_sriov_vf(adev)) {
5317 r = amdgpu_device_reset_sriov(adev, job ? false : true);
5319 adev->asic_reset_res = r;
5321 /* Aldebaran supports ras in SRIOV, so need resume ras during reset */
5322 if (adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 2))
5323 amdgpu_ras_resume(adev);
5325 r = amdgpu_do_asic_reset(device_list_handle, reset_context);
5326 if (r && r == -EAGAIN)
5329 if (!r && gpu_reset_for_dev_remove)
5335 /* Post ASIC reset for all devs .*/
5336 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5338 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5339 struct amdgpu_ring *ring = tmp_adev->rings[i];
5341 if (!ring || !ring->sched.thread)
5344 drm_sched_start(&ring->sched, true);
5347 if (adev->enable_mes && adev->ip_versions[GC_HWIP][0] != IP_VERSION(11, 0, 3))
5348 amdgpu_mes_self_test(tmp_adev);
5350 if (!drm_drv_uses_atomic_modeset(adev_to_drm(tmp_adev)) && !job_signaled) {
5351 drm_helper_resume_force_mode(adev_to_drm(tmp_adev));
5354 if (tmp_adev->asic_reset_res)
5355 r = tmp_adev->asic_reset_res;
5357 tmp_adev->asic_reset_res = 0;
5360 /* bad news, how to tell it to userspace ? */
5361 dev_info(tmp_adev->dev, "GPU reset(%d) failed\n", atomic_read(&tmp_adev->gpu_reset_counter));
5362 amdgpu_vf_error_put(tmp_adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
5364 dev_info(tmp_adev->dev, "GPU reset(%d) succeeded!\n", atomic_read(&tmp_adev->gpu_reset_counter));
5365 if (amdgpu_acpi_smart_shift_update(adev_to_drm(tmp_adev), AMDGPU_SS_DEV_D0))
5366 DRM_WARN("smart shift update failed\n");
5371 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5372 /* unlock kfd: SRIOV would do it separately */
5373 if (!need_emergency_restart && !amdgpu_sriov_vf(tmp_adev))
5374 amdgpu_amdkfd_post_reset(tmp_adev);
5376 /* kfd_post_reset will do nothing if kfd device is not initialized,
5377 * need to bring up kfd here if it's not be initialized before
5379 if (!adev->kfd.init_complete)
5380 amdgpu_amdkfd_device_init(adev);
5382 if (audio_suspended)
5383 amdgpu_device_resume_display_audio(tmp_adev);
5385 amdgpu_device_unset_mp1_state(tmp_adev);
5387 amdgpu_ras_set_error_query_ready(tmp_adev, true);
5391 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
5393 amdgpu_device_unlock_reset_domain(tmp_adev->reset_domain);
5396 mutex_unlock(&hive->hive_lock);
5397 amdgpu_put_xgmi_hive(hive);
5401 dev_info(adev->dev, "GPU reset end with ret = %d\n", r);
5403 atomic_set(&adev->reset_domain->reset_res, r);
5408 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
5410 * @adev: amdgpu_device pointer
5412 * Fetchs and stores in the driver the PCIE capabilities (gen speed
5413 * and lanes) of the slot the device is in. Handles APUs and
5414 * virtualized environments where PCIE config space may not be available.
5416 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
5418 struct pci_dev *pdev;
5419 enum pci_bus_speed speed_cap, platform_speed_cap;
5420 enum pcie_link_width platform_link_width;
5422 if (amdgpu_pcie_gen_cap)
5423 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
5425 if (amdgpu_pcie_lane_cap)
5426 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
5428 /* covers APUs as well */
5429 if (pci_is_root_bus(adev->pdev->bus)) {
5430 if (adev->pm.pcie_gen_mask == 0)
5431 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
5432 if (adev->pm.pcie_mlw_mask == 0)
5433 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
5437 if (adev->pm.pcie_gen_mask && adev->pm.pcie_mlw_mask)
5440 pcie_bandwidth_available(adev->pdev, NULL,
5441 &platform_speed_cap, &platform_link_width);
5443 if (adev->pm.pcie_gen_mask == 0) {
5446 speed_cap = pcie_get_speed_cap(pdev);
5447 if (speed_cap == PCI_SPEED_UNKNOWN) {
5448 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5449 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5450 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
5452 if (speed_cap == PCIE_SPEED_32_0GT)
5453 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5454 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5455 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5456 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4 |
5457 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN5);
5458 else if (speed_cap == PCIE_SPEED_16_0GT)
5459 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5460 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5461 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5462 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4);
5463 else if (speed_cap == PCIE_SPEED_8_0GT)
5464 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5465 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5466 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
5467 else if (speed_cap == PCIE_SPEED_5_0GT)
5468 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5469 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2);
5471 adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1;
5474 if (platform_speed_cap == PCI_SPEED_UNKNOWN) {
5475 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5476 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
5478 if (platform_speed_cap == PCIE_SPEED_32_0GT)
5479 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5480 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5481 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5482 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4 |
5483 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN5);
5484 else if (platform_speed_cap == PCIE_SPEED_16_0GT)
5485 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5486 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5487 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5488 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4);
5489 else if (platform_speed_cap == PCIE_SPEED_8_0GT)
5490 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5491 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5492 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3);
5493 else if (platform_speed_cap == PCIE_SPEED_5_0GT)
5494 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5495 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
5497 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
5501 if (adev->pm.pcie_mlw_mask == 0) {
5502 if (platform_link_width == PCIE_LNK_WIDTH_UNKNOWN) {
5503 adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK;
5505 switch (platform_link_width) {
5507 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
5508 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5509 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5510 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5511 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5512 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5513 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5516 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5517 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5518 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5519 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5520 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5521 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5524 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5525 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5526 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5527 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5528 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5531 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5532 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5533 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5534 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5537 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5538 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5539 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5542 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5543 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5546 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
5556 * amdgpu_device_is_peer_accessible - Check peer access through PCIe BAR
5558 * @adev: amdgpu_device pointer
5559 * @peer_adev: amdgpu_device pointer for peer device trying to access @adev
5561 * Return true if @peer_adev can access (DMA) @adev through the PCIe
5562 * BAR, i.e. @adev is "large BAR" and the BAR matches the DMA mask of
5565 bool amdgpu_device_is_peer_accessible(struct amdgpu_device *adev,
5566 struct amdgpu_device *peer_adev)
5568 #ifdef CONFIG_HSA_AMD_P2P
5569 uint64_t address_mask = peer_adev->dev->dma_mask ?
5570 ~*peer_adev->dev->dma_mask : ~((1ULL << 32) - 1);
5571 resource_size_t aper_limit =
5572 adev->gmc.aper_base + adev->gmc.aper_size - 1;
5574 !adev->gmc.xgmi.connected_to_cpu &&
5575 !(pci_p2pdma_distance(adev->pdev, peer_adev->dev, false) < 0);
5577 return pcie_p2p && p2p_access && (adev->gmc.visible_vram_size &&
5578 adev->gmc.real_vram_size == adev->gmc.visible_vram_size &&
5579 !(adev->gmc.aper_base & address_mask ||
5580 aper_limit & address_mask));
5586 int amdgpu_device_baco_enter(struct drm_device *dev)
5588 struct amdgpu_device *adev = drm_to_adev(dev);
5589 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
5591 if (!amdgpu_device_supports_baco(dev))
5594 if (ras && adev->ras_enabled &&
5595 adev->nbio.funcs->enable_doorbell_interrupt)
5596 adev->nbio.funcs->enable_doorbell_interrupt(adev, false);
5598 return amdgpu_dpm_baco_enter(adev);
5601 int amdgpu_device_baco_exit(struct drm_device *dev)
5603 struct amdgpu_device *adev = drm_to_adev(dev);
5604 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
5607 if (!amdgpu_device_supports_baco(dev))
5610 ret = amdgpu_dpm_baco_exit(adev);
5614 if (ras && adev->ras_enabled &&
5615 adev->nbio.funcs->enable_doorbell_interrupt)
5616 adev->nbio.funcs->enable_doorbell_interrupt(adev, true);
5618 if (amdgpu_passthrough(adev) &&
5619 adev->nbio.funcs->clear_doorbell_interrupt)
5620 adev->nbio.funcs->clear_doorbell_interrupt(adev);
5626 * amdgpu_pci_error_detected - Called when a PCI error is detected.
5627 * @pdev: PCI device struct
5628 * @state: PCI channel state
5630 * Description: Called when a PCI error is detected.
5632 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT.
5634 pci_ers_result_t amdgpu_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
5636 struct drm_device *dev = pci_get_drvdata(pdev);
5637 struct amdgpu_device *adev = drm_to_adev(dev);
5640 DRM_INFO("PCI error: detected callback, state(%d)!!\n", state);
5642 if (adev->gmc.xgmi.num_physical_nodes > 1) {
5643 DRM_WARN("No support for XGMI hive yet...");
5644 return PCI_ERS_RESULT_DISCONNECT;
5647 adev->pci_channel_state = state;
5650 case pci_channel_io_normal:
5651 return PCI_ERS_RESULT_CAN_RECOVER;
5652 /* Fatal error, prepare for slot reset */
5653 case pci_channel_io_frozen:
5655 * Locking adev->reset_domain->sem will prevent any external access
5656 * to GPU during PCI error recovery
5658 amdgpu_device_lock_reset_domain(adev->reset_domain);
5659 amdgpu_device_set_mp1_state(adev);
5662 * Block any work scheduling as we do for regular GPU reset
5663 * for the duration of the recovery
5665 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5666 struct amdgpu_ring *ring = adev->rings[i];
5668 if (!ring || !ring->sched.thread)
5671 drm_sched_stop(&ring->sched, NULL);
5673 atomic_inc(&adev->gpu_reset_counter);
5674 return PCI_ERS_RESULT_NEED_RESET;
5675 case pci_channel_io_perm_failure:
5676 /* Permanent error, prepare for device removal */
5677 return PCI_ERS_RESULT_DISCONNECT;
5680 return PCI_ERS_RESULT_NEED_RESET;
5684 * amdgpu_pci_mmio_enabled - Enable MMIO and dump debug registers
5685 * @pdev: pointer to PCI device
5687 pci_ers_result_t amdgpu_pci_mmio_enabled(struct pci_dev *pdev)
5690 DRM_INFO("PCI error: mmio enabled callback!!\n");
5692 /* TODO - dump whatever for debugging purposes */
5694 /* This called only if amdgpu_pci_error_detected returns
5695 * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still
5696 * works, no need to reset slot.
5699 return PCI_ERS_RESULT_RECOVERED;
5703 * amdgpu_pci_slot_reset - Called when PCI slot has been reset.
5704 * @pdev: PCI device struct
5706 * Description: This routine is called by the pci error recovery
5707 * code after the PCI slot has been reset, just before we
5708 * should resume normal operations.
5710 pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev)
5712 struct drm_device *dev = pci_get_drvdata(pdev);
5713 struct amdgpu_device *adev = drm_to_adev(dev);
5715 struct amdgpu_reset_context reset_context;
5717 struct list_head device_list;
5719 DRM_INFO("PCI error: slot reset callback!!\n");
5721 memset(&reset_context, 0, sizeof(reset_context));
5723 INIT_LIST_HEAD(&device_list);
5724 list_add_tail(&adev->reset_list, &device_list);
5726 /* wait for asic to come out of reset */
5729 /* Restore PCI confspace */
5730 amdgpu_device_load_pci_state(pdev);
5732 /* confirm ASIC came out of reset */
5733 for (i = 0; i < adev->usec_timeout; i++) {
5734 memsize = amdgpu_asic_get_config_memsize(adev);
5736 if (memsize != 0xffffffff)
5740 if (memsize == 0xffffffff) {
5745 reset_context.method = AMD_RESET_METHOD_NONE;
5746 reset_context.reset_req_dev = adev;
5747 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
5748 set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
5750 adev->no_hw_access = true;
5751 r = amdgpu_device_pre_asic_reset(adev, &reset_context);
5752 adev->no_hw_access = false;
5756 r = amdgpu_do_asic_reset(&device_list, &reset_context);
5760 if (amdgpu_device_cache_pci_state(adev->pdev))
5761 pci_restore_state(adev->pdev);
5763 DRM_INFO("PCIe error recovery succeeded\n");
5765 DRM_ERROR("PCIe error recovery failed, err:%d", r);
5766 amdgpu_device_unset_mp1_state(adev);
5767 amdgpu_device_unlock_reset_domain(adev->reset_domain);
5770 return r ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
5774 * amdgpu_pci_resume() - resume normal ops after PCI reset
5775 * @pdev: pointer to PCI device
5777 * Called when the error recovery driver tells us that its
5778 * OK to resume normal operation.
5780 void amdgpu_pci_resume(struct pci_dev *pdev)
5782 struct drm_device *dev = pci_get_drvdata(pdev);
5783 struct amdgpu_device *adev = drm_to_adev(dev);
5787 DRM_INFO("PCI error: resume callback!!\n");
5789 /* Only continue execution for the case of pci_channel_io_frozen */
5790 if (adev->pci_channel_state != pci_channel_io_frozen)
5793 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5794 struct amdgpu_ring *ring = adev->rings[i];
5796 if (!ring || !ring->sched.thread)
5799 drm_sched_start(&ring->sched, true);
5802 amdgpu_device_unset_mp1_state(adev);
5803 amdgpu_device_unlock_reset_domain(adev->reset_domain);
5806 bool amdgpu_device_cache_pci_state(struct pci_dev *pdev)
5808 struct drm_device *dev = pci_get_drvdata(pdev);
5809 struct amdgpu_device *adev = drm_to_adev(dev);
5812 r = pci_save_state(pdev);
5814 kfree(adev->pci_state);
5816 adev->pci_state = pci_store_saved_state(pdev);
5818 if (!adev->pci_state) {
5819 DRM_ERROR("Failed to store PCI saved state");
5823 DRM_WARN("Failed to save PCI state, err:%d\n", r);
5830 bool amdgpu_device_load_pci_state(struct pci_dev *pdev)
5832 struct drm_device *dev = pci_get_drvdata(pdev);
5833 struct amdgpu_device *adev = drm_to_adev(dev);
5836 if (!adev->pci_state)
5839 r = pci_load_saved_state(pdev, adev->pci_state);
5842 pci_restore_state(pdev);
5844 DRM_WARN("Failed to load PCI state, err:%d\n", r);
5851 void amdgpu_device_flush_hdp(struct amdgpu_device *adev,
5852 struct amdgpu_ring *ring)
5854 #ifdef CONFIG_X86_64
5855 if ((adev->flags & AMD_IS_APU) && !amdgpu_passthrough(adev))
5858 if (adev->gmc.xgmi.connected_to_cpu)
5861 if (ring && ring->funcs->emit_hdp_flush)
5862 amdgpu_ring_emit_hdp_flush(ring);
5864 amdgpu_asic_flush_hdp(adev, ring);
5867 void amdgpu_device_invalidate_hdp(struct amdgpu_device *adev,
5868 struct amdgpu_ring *ring)
5870 #ifdef CONFIG_X86_64
5871 if ((adev->flags & AMD_IS_APU) && !amdgpu_passthrough(adev))
5874 if (adev->gmc.xgmi.connected_to_cpu)
5877 amdgpu_asic_invalidate_hdp(adev, ring);
5880 int amdgpu_in_reset(struct amdgpu_device *adev)
5882 return atomic_read(&adev->reset_domain->in_gpu_reset);
5886 * amdgpu_device_halt() - bring hardware to some kind of halt state
5888 * @adev: amdgpu_device pointer
5890 * Bring hardware to some kind of halt state so that no one can touch it
5891 * any more. It will help to maintain error context when error occurred.
5892 * Compare to a simple hang, the system will keep stable at least for SSH
5893 * access. Then it should be trivial to inspect the hardware state and
5894 * see what's going on. Implemented as following:
5896 * 1. drm_dev_unplug() makes device inaccessible to user space(IOCTLs, etc),
5897 * clears all CPU mappings to device, disallows remappings through page faults
5898 * 2. amdgpu_irq_disable_all() disables all interrupts
5899 * 3. amdgpu_fence_driver_hw_fini() signals all HW fences
5900 * 4. set adev->no_hw_access to avoid potential crashes after setp 5
5901 * 5. amdgpu_device_unmap_mmio() clears all MMIO mappings
5902 * 6. pci_disable_device() and pci_wait_for_pending_transaction()
5903 * flush any in flight DMA operations
5905 void amdgpu_device_halt(struct amdgpu_device *adev)
5907 struct pci_dev *pdev = adev->pdev;
5908 struct drm_device *ddev = adev_to_drm(adev);
5910 drm_dev_unplug(ddev);
5912 amdgpu_irq_disable_all(adev);
5914 amdgpu_fence_driver_hw_fini(adev);
5916 adev->no_hw_access = true;
5918 amdgpu_device_unmap_mmio(adev);
5920 pci_disable_device(pdev);
5921 pci_wait_for_pending_transaction(pdev);
5924 u32 amdgpu_device_pcie_port_rreg(struct amdgpu_device *adev,
5927 unsigned long flags, address, data;
5930 address = adev->nbio.funcs->get_pcie_port_index_offset(adev);
5931 data = adev->nbio.funcs->get_pcie_port_data_offset(adev);
5933 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
5934 WREG32(address, reg * 4);
5935 (void)RREG32(address);
5937 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
5941 void amdgpu_device_pcie_port_wreg(struct amdgpu_device *adev,
5944 unsigned long flags, address, data;
5946 address = adev->nbio.funcs->get_pcie_port_index_offset(adev);
5947 data = adev->nbio.funcs->get_pcie_port_data_offset(adev);
5949 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
5950 WREG32(address, reg * 4);
5951 (void)RREG32(address);
5954 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
5958 * amdgpu_device_switch_gang - switch to a new gang
5959 * @adev: amdgpu_device pointer
5960 * @gang: the gang to switch to
5962 * Try to switch to a new gang.
5963 * Returns: NULL if we switched to the new gang or a reference to the current
5966 struct dma_fence *amdgpu_device_switch_gang(struct amdgpu_device *adev,
5967 struct dma_fence *gang)
5969 struct dma_fence *old = NULL;
5974 old = dma_fence_get_rcu_safe(&adev->gang_submit);
5980 if (!dma_fence_is_signaled(old))
5983 } while (cmpxchg((struct dma_fence __force **)&adev->gang_submit,
5990 bool amdgpu_device_has_display_hardware(struct amdgpu_device *adev)
5992 switch (adev->asic_type) {
5993 #ifdef CONFIG_DRM_AMDGPU_SI
5997 /* chips with no display hardware */
5999 #ifdef CONFIG_DRM_AMDGPU_SI
6005 #ifdef CONFIG_DRM_AMDGPU_CIK
6014 case CHIP_POLARIS10:
6015 case CHIP_POLARIS11:
6016 case CHIP_POLARIS12:
6020 /* chips with display hardware */
6024 if (!adev->ip_versions[DCE_HWIP][0] ||
6025 (adev->harvest_ip_mask & AMD_HARVEST_IP_DMU_MASK))