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>
36 #include <drm/drm_atomic_helper.h>
37 #include <drm/drm_probe_helper.h>
38 #include <drm/amdgpu_drm.h>
39 #include <linux/vgaarb.h>
40 #include <linux/vga_switcheroo.h>
41 #include <linux/efi.h>
43 #include "amdgpu_trace.h"
44 #include "amdgpu_i2c.h"
46 #include "amdgpu_atombios.h"
47 #include "amdgpu_atomfirmware.h"
49 #ifdef CONFIG_DRM_AMDGPU_SI
52 #ifdef CONFIG_DRM_AMDGPU_CIK
58 #include "bif/bif_4_1_d.h"
59 #include <linux/firmware.h>
60 #include "amdgpu_vf_error.h"
62 #include "amdgpu_amdkfd.h"
63 #include "amdgpu_pm.h"
65 #include "amdgpu_xgmi.h"
66 #include "amdgpu_ras.h"
67 #include "amdgpu_pmu.h"
68 #include "amdgpu_fru_eeprom.h"
69 #include "amdgpu_reset.h"
71 #include <linux/suspend.h>
72 #include <drm/task_barrier.h>
73 #include <linux/pm_runtime.h>
75 #include <drm/drm_drv.h>
77 MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
78 MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
79 MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
80 MODULE_FIRMWARE("amdgpu/picasso_gpu_info.bin");
81 MODULE_FIRMWARE("amdgpu/raven2_gpu_info.bin");
82 MODULE_FIRMWARE("amdgpu/arcturus_gpu_info.bin");
83 MODULE_FIRMWARE("amdgpu/renoir_gpu_info.bin");
84 MODULE_FIRMWARE("amdgpu/navi10_gpu_info.bin");
85 MODULE_FIRMWARE("amdgpu/navi14_gpu_info.bin");
86 MODULE_FIRMWARE("amdgpu/navi12_gpu_info.bin");
87 MODULE_FIRMWARE("amdgpu/vangogh_gpu_info.bin");
88 MODULE_FIRMWARE("amdgpu/yellow_carp_gpu_info.bin");
90 #define AMDGPU_RESUME_MS 2000
91 #define AMDGPU_MAX_RETRY_LIMIT 2
92 #define AMDGPU_RETRY_SRIOV_RESET(r) ((r) == -EBUSY || (r) == -ETIMEDOUT || (r) == -EINVAL)
94 const char *amdgpu_asic_name[] = {
136 * DOC: pcie_replay_count
138 * The amdgpu driver provides a sysfs API for reporting the total number
139 * of PCIe replays (NAKs)
140 * The file pcie_replay_count is used for this and returns the total
141 * number of replays as a sum of the NAKs generated and NAKs received
144 static ssize_t amdgpu_device_get_pcie_replay_count(struct device *dev,
145 struct device_attribute *attr, char *buf)
147 struct drm_device *ddev = dev_get_drvdata(dev);
148 struct amdgpu_device *adev = drm_to_adev(ddev);
149 uint64_t cnt = amdgpu_asic_get_pcie_replay_count(adev);
151 return sysfs_emit(buf, "%llu\n", cnt);
154 static DEVICE_ATTR(pcie_replay_count, S_IRUGO,
155 amdgpu_device_get_pcie_replay_count, NULL);
157 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev);
162 * The amdgpu driver provides a sysfs API for reporting the product name
164 * The file serial_number is used for this and returns the product name
165 * as returned from the FRU.
166 * NOTE: This is only available for certain server cards
169 static ssize_t amdgpu_device_get_product_name(struct device *dev,
170 struct device_attribute *attr, char *buf)
172 struct drm_device *ddev = dev_get_drvdata(dev);
173 struct amdgpu_device *adev = drm_to_adev(ddev);
175 return sysfs_emit(buf, "%s\n", adev->product_name);
178 static DEVICE_ATTR(product_name, S_IRUGO,
179 amdgpu_device_get_product_name, NULL);
182 * DOC: product_number
184 * The amdgpu driver provides a sysfs API for reporting the part number
186 * The file serial_number is used for this and returns the part number
187 * as returned from the FRU.
188 * NOTE: This is only available for certain server cards
191 static ssize_t amdgpu_device_get_product_number(struct device *dev,
192 struct device_attribute *attr, char *buf)
194 struct drm_device *ddev = dev_get_drvdata(dev);
195 struct amdgpu_device *adev = drm_to_adev(ddev);
197 return sysfs_emit(buf, "%s\n", adev->product_number);
200 static DEVICE_ATTR(product_number, S_IRUGO,
201 amdgpu_device_get_product_number, NULL);
206 * The amdgpu driver provides a sysfs API for reporting the serial number
208 * The file serial_number is used for this and returns the serial number
209 * as returned from the FRU.
210 * NOTE: This is only available for certain server cards
213 static ssize_t amdgpu_device_get_serial_number(struct device *dev,
214 struct device_attribute *attr, char *buf)
216 struct drm_device *ddev = dev_get_drvdata(dev);
217 struct amdgpu_device *adev = drm_to_adev(ddev);
219 return sysfs_emit(buf, "%s\n", adev->serial);
222 static DEVICE_ATTR(serial_number, S_IRUGO,
223 amdgpu_device_get_serial_number, NULL);
226 * amdgpu_device_supports_px - Is the device a dGPU with ATPX power control
228 * @dev: drm_device pointer
230 * Returns true if the device is a dGPU with ATPX power control,
231 * otherwise return false.
233 bool amdgpu_device_supports_px(struct drm_device *dev)
235 struct amdgpu_device *adev = drm_to_adev(dev);
237 if ((adev->flags & AMD_IS_PX) && !amdgpu_is_atpx_hybrid())
243 * amdgpu_device_supports_boco - Is the device a dGPU with ACPI power resources
245 * @dev: drm_device pointer
247 * Returns true if the device is a dGPU with ACPI power control,
248 * otherwise return false.
250 bool amdgpu_device_supports_boco(struct drm_device *dev)
252 struct amdgpu_device *adev = drm_to_adev(dev);
255 ((adev->flags & AMD_IS_PX) && amdgpu_is_atpx_hybrid()))
261 * amdgpu_device_supports_baco - Does the device support BACO
263 * @dev: drm_device pointer
265 * Returns true if the device supporte BACO,
266 * otherwise return false.
268 bool amdgpu_device_supports_baco(struct drm_device *dev)
270 struct amdgpu_device *adev = drm_to_adev(dev);
272 return amdgpu_asic_supports_baco(adev);
276 * amdgpu_device_supports_smart_shift - Is the device dGPU with
277 * smart shift support
279 * @dev: drm_device pointer
281 * Returns true if the device is a dGPU with Smart Shift support,
282 * otherwise returns false.
284 bool amdgpu_device_supports_smart_shift(struct drm_device *dev)
286 return (amdgpu_device_supports_boco(dev) &&
287 amdgpu_acpi_is_power_shift_control_supported());
291 * VRAM access helper functions
295 * amdgpu_device_mm_access - access vram by MM_INDEX/MM_DATA
297 * @adev: amdgpu_device pointer
298 * @pos: offset of the buffer in vram
299 * @buf: virtual address of the buffer in system memory
300 * @size: read/write size, sizeof(@buf) must > @size
301 * @write: true - write to vram, otherwise - read from vram
303 void amdgpu_device_mm_access(struct amdgpu_device *adev, loff_t pos,
304 void *buf, size_t size, bool write)
307 uint32_t hi = ~0, tmp = 0;
308 uint32_t *data = buf;
312 if (!drm_dev_enter(adev_to_drm(adev), &idx))
315 BUG_ON(!IS_ALIGNED(pos, 4) || !IS_ALIGNED(size, 4));
317 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
318 for (last = pos + size; pos < last; pos += 4) {
321 WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)pos) | 0x80000000);
323 WREG32_NO_KIQ(mmMM_INDEX_HI, tmp);
327 WREG32_NO_KIQ(mmMM_DATA, *data++);
329 *data++ = RREG32_NO_KIQ(mmMM_DATA);
332 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
337 * amdgpu_device_aper_access - access vram by vram aperature
339 * @adev: amdgpu_device pointer
340 * @pos: offset of the buffer in vram
341 * @buf: virtual address of the buffer in system memory
342 * @size: read/write size, sizeof(@buf) must > @size
343 * @write: true - write to vram, otherwise - read from vram
345 * The return value means how many bytes have been transferred.
347 size_t amdgpu_device_aper_access(struct amdgpu_device *adev, loff_t pos,
348 void *buf, size_t size, bool write)
355 if (!adev->mman.aper_base_kaddr)
358 last = min(pos + size, adev->gmc.visible_vram_size);
360 addr = adev->mman.aper_base_kaddr + pos;
364 memcpy_toio(addr, buf, count);
366 amdgpu_device_flush_hdp(adev, NULL);
368 amdgpu_device_invalidate_hdp(adev, NULL);
370 memcpy_fromio(buf, addr, count);
382 * amdgpu_device_vram_access - read/write a buffer in vram
384 * @adev: amdgpu_device pointer
385 * @pos: offset of the buffer in vram
386 * @buf: virtual address of the buffer in system memory
387 * @size: read/write size, sizeof(@buf) must > @size
388 * @write: true - write to vram, otherwise - read from vram
390 void amdgpu_device_vram_access(struct amdgpu_device *adev, loff_t pos,
391 void *buf, size_t size, bool write)
395 /* try to using vram apreature to access vram first */
396 count = amdgpu_device_aper_access(adev, pos, buf, size, write);
399 /* using MM to access rest vram */
402 amdgpu_device_mm_access(adev, pos, buf, size, write);
407 * register access helper functions.
410 /* Check if hw access should be skipped because of hotplug or device error */
411 bool amdgpu_device_skip_hw_access(struct amdgpu_device *adev)
413 if (adev->no_hw_access)
416 #ifdef CONFIG_LOCKDEP
418 * This is a bit complicated to understand, so worth a comment. What we assert
419 * here is that the GPU reset is not running on another thread in parallel.
421 * For this we trylock the read side of the reset semaphore, if that succeeds
422 * we know that the reset is not running in paralell.
424 * If the trylock fails we assert that we are either already holding the read
425 * side of the lock or are the reset thread itself and hold the write side of
429 if (down_read_trylock(&adev->reset_domain->sem))
430 up_read(&adev->reset_domain->sem);
432 lockdep_assert_held(&adev->reset_domain->sem);
439 * amdgpu_device_rreg - read a memory mapped IO or indirect register
441 * @adev: amdgpu_device pointer
442 * @reg: dword aligned register offset
443 * @acc_flags: access flags which require special behavior
445 * Returns the 32 bit value from the offset specified.
447 uint32_t amdgpu_device_rreg(struct amdgpu_device *adev,
448 uint32_t reg, uint32_t acc_flags)
452 if (amdgpu_device_skip_hw_access(adev))
455 if ((reg * 4) < adev->rmmio_size) {
456 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
457 amdgpu_sriov_runtime(adev) &&
458 down_read_trylock(&adev->reset_domain->sem)) {
459 ret = amdgpu_kiq_rreg(adev, reg);
460 up_read(&adev->reset_domain->sem);
462 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
465 ret = adev->pcie_rreg(adev, reg * 4);
468 trace_amdgpu_device_rreg(adev->pdev->device, reg, ret);
474 * MMIO register read with bytes helper functions
475 * @offset:bytes offset from MMIO start
480 * amdgpu_mm_rreg8 - read a memory mapped IO register
482 * @adev: amdgpu_device pointer
483 * @offset: byte aligned register offset
485 * Returns the 8 bit value from the offset specified.
487 uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset)
489 if (amdgpu_device_skip_hw_access(adev))
492 if (offset < adev->rmmio_size)
493 return (readb(adev->rmmio + offset));
498 * MMIO register write with bytes helper functions
499 * @offset:bytes offset from MMIO start
500 * @value: the value want to be written to the register
504 * amdgpu_mm_wreg8 - read a memory mapped IO register
506 * @adev: amdgpu_device pointer
507 * @offset: byte aligned register offset
508 * @value: 8 bit value to write
510 * Writes the value specified to the offset specified.
512 void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value)
514 if (amdgpu_device_skip_hw_access(adev))
517 if (offset < adev->rmmio_size)
518 writeb(value, adev->rmmio + offset);
524 * amdgpu_device_wreg - write to a memory mapped IO or indirect register
526 * @adev: amdgpu_device pointer
527 * @reg: dword aligned register offset
528 * @v: 32 bit value to write to the register
529 * @acc_flags: access flags which require special behavior
531 * Writes the value specified to the offset specified.
533 void amdgpu_device_wreg(struct amdgpu_device *adev,
534 uint32_t reg, uint32_t v,
537 if (amdgpu_device_skip_hw_access(adev))
540 if ((reg * 4) < adev->rmmio_size) {
541 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
542 amdgpu_sriov_runtime(adev) &&
543 down_read_trylock(&adev->reset_domain->sem)) {
544 amdgpu_kiq_wreg(adev, reg, v);
545 up_read(&adev->reset_domain->sem);
547 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
550 adev->pcie_wreg(adev, reg * 4, v);
553 trace_amdgpu_device_wreg(adev->pdev->device, reg, v);
557 * amdgpu_mm_wreg_mmio_rlc - write register either with direct/indirect mmio or with RLC path if in range
559 * @adev: amdgpu_device pointer
560 * @reg: mmio/rlc register
563 * this function is invoked only for the debugfs register access
565 void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
566 uint32_t reg, uint32_t v)
568 if (amdgpu_device_skip_hw_access(adev))
571 if (amdgpu_sriov_fullaccess(adev) &&
572 adev->gfx.rlc.funcs &&
573 adev->gfx.rlc.funcs->is_rlcg_access_range) {
574 if (adev->gfx.rlc.funcs->is_rlcg_access_range(adev, reg))
575 return amdgpu_sriov_wreg(adev, reg, v, 0, 0);
576 } else if ((reg * 4) >= adev->rmmio_size) {
577 adev->pcie_wreg(adev, reg * 4, v);
579 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
584 * amdgpu_mm_rdoorbell - read a doorbell dword
586 * @adev: amdgpu_device pointer
587 * @index: doorbell index
589 * Returns the value in the doorbell aperture at the
590 * requested doorbell index (CIK).
592 u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
594 if (amdgpu_device_skip_hw_access(adev))
597 if (index < adev->doorbell.num_doorbells) {
598 return readl(adev->doorbell.ptr + index);
600 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
606 * amdgpu_mm_wdoorbell - write a doorbell dword
608 * @adev: amdgpu_device pointer
609 * @index: doorbell index
612 * Writes @v to the doorbell aperture at the
613 * requested doorbell index (CIK).
615 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
617 if (amdgpu_device_skip_hw_access(adev))
620 if (index < adev->doorbell.num_doorbells) {
621 writel(v, adev->doorbell.ptr + index);
623 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
628 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
630 * @adev: amdgpu_device pointer
631 * @index: doorbell index
633 * Returns the value in the doorbell aperture at the
634 * requested doorbell index (VEGA10+).
636 u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
638 if (amdgpu_device_skip_hw_access(adev))
641 if (index < adev->doorbell.num_doorbells) {
642 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
644 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
650 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
652 * @adev: amdgpu_device pointer
653 * @index: doorbell index
656 * Writes @v to the doorbell aperture at the
657 * requested doorbell index (VEGA10+).
659 void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
661 if (amdgpu_device_skip_hw_access(adev))
664 if (index < adev->doorbell.num_doorbells) {
665 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
667 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
672 * amdgpu_device_indirect_rreg - read an indirect register
674 * @adev: amdgpu_device pointer
675 * @pcie_index: mmio register offset
676 * @pcie_data: mmio register offset
677 * @reg_addr: indirect register address to read from
679 * Returns the value of indirect register @reg_addr
681 u32 amdgpu_device_indirect_rreg(struct amdgpu_device *adev,
682 u32 pcie_index, u32 pcie_data,
687 void __iomem *pcie_index_offset;
688 void __iomem *pcie_data_offset;
690 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
691 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
692 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
694 writel(reg_addr, pcie_index_offset);
695 readl(pcie_index_offset);
696 r = readl(pcie_data_offset);
697 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
703 * amdgpu_device_indirect_rreg64 - read a 64bits indirect register
705 * @adev: amdgpu_device pointer
706 * @pcie_index: mmio register offset
707 * @pcie_data: mmio register offset
708 * @reg_addr: indirect register address to read from
710 * Returns the value of indirect register @reg_addr
712 u64 amdgpu_device_indirect_rreg64(struct amdgpu_device *adev,
713 u32 pcie_index, u32 pcie_data,
718 void __iomem *pcie_index_offset;
719 void __iomem *pcie_data_offset;
721 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
722 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
723 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
725 /* read low 32 bits */
726 writel(reg_addr, pcie_index_offset);
727 readl(pcie_index_offset);
728 r = readl(pcie_data_offset);
729 /* read high 32 bits */
730 writel(reg_addr + 4, pcie_index_offset);
731 readl(pcie_index_offset);
732 r |= ((u64)readl(pcie_data_offset) << 32);
733 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
739 * amdgpu_device_indirect_wreg - write an indirect register address
741 * @adev: amdgpu_device pointer
742 * @pcie_index: mmio register offset
743 * @pcie_data: mmio register offset
744 * @reg_addr: indirect register offset
745 * @reg_data: indirect register data
748 void amdgpu_device_indirect_wreg(struct amdgpu_device *adev,
749 u32 pcie_index, u32 pcie_data,
750 u32 reg_addr, u32 reg_data)
753 void __iomem *pcie_index_offset;
754 void __iomem *pcie_data_offset;
756 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
757 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
758 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
760 writel(reg_addr, pcie_index_offset);
761 readl(pcie_index_offset);
762 writel(reg_data, pcie_data_offset);
763 readl(pcie_data_offset);
764 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
768 * amdgpu_device_indirect_wreg64 - write a 64bits indirect register address
770 * @adev: amdgpu_device pointer
771 * @pcie_index: mmio register offset
772 * @pcie_data: mmio register offset
773 * @reg_addr: indirect register offset
774 * @reg_data: indirect register data
777 void amdgpu_device_indirect_wreg64(struct amdgpu_device *adev,
778 u32 pcie_index, u32 pcie_data,
779 u32 reg_addr, u64 reg_data)
782 void __iomem *pcie_index_offset;
783 void __iomem *pcie_data_offset;
785 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
786 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
787 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
789 /* write low 32 bits */
790 writel(reg_addr, pcie_index_offset);
791 readl(pcie_index_offset);
792 writel((u32)(reg_data & 0xffffffffULL), pcie_data_offset);
793 readl(pcie_data_offset);
794 /* write high 32 bits */
795 writel(reg_addr + 4, pcie_index_offset);
796 readl(pcie_index_offset);
797 writel((u32)(reg_data >> 32), pcie_data_offset);
798 readl(pcie_data_offset);
799 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
803 * amdgpu_invalid_rreg - dummy reg read function
805 * @adev: amdgpu_device pointer
806 * @reg: offset of register
808 * Dummy register read function. Used for register blocks
809 * that certain asics don't have (all asics).
810 * Returns the value in the register.
812 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
814 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
820 * amdgpu_invalid_wreg - dummy reg write function
822 * @adev: amdgpu_device pointer
823 * @reg: offset of register
824 * @v: value to write to the register
826 * Dummy register read function. Used for register blocks
827 * that certain asics don't have (all asics).
829 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
831 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
837 * amdgpu_invalid_rreg64 - dummy 64 bit reg read function
839 * @adev: amdgpu_device pointer
840 * @reg: offset of register
842 * Dummy register read function. Used for register blocks
843 * that certain asics don't have (all asics).
844 * Returns the value in the register.
846 static uint64_t amdgpu_invalid_rreg64(struct amdgpu_device *adev, uint32_t reg)
848 DRM_ERROR("Invalid callback to read 64 bit register 0x%04X\n", reg);
854 * amdgpu_invalid_wreg64 - dummy reg write function
856 * @adev: amdgpu_device pointer
857 * @reg: offset of register
858 * @v: value to write to the register
860 * Dummy register read function. Used for register blocks
861 * that certain asics don't have (all asics).
863 static void amdgpu_invalid_wreg64(struct amdgpu_device *adev, uint32_t reg, uint64_t v)
865 DRM_ERROR("Invalid callback to write 64 bit register 0x%04X with 0x%08llX\n",
871 * amdgpu_block_invalid_rreg - dummy reg read function
873 * @adev: amdgpu_device pointer
874 * @block: offset of instance
875 * @reg: offset of register
877 * Dummy register read function. Used for register blocks
878 * that certain asics don't have (all asics).
879 * Returns the value in the register.
881 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
882 uint32_t block, uint32_t reg)
884 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
891 * amdgpu_block_invalid_wreg - dummy reg write function
893 * @adev: amdgpu_device pointer
894 * @block: offset of instance
895 * @reg: offset of register
896 * @v: value to write to the register
898 * Dummy register read function. Used for register blocks
899 * that certain asics don't have (all asics).
901 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
903 uint32_t reg, uint32_t v)
905 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
911 * amdgpu_device_asic_init - Wrapper for atom asic_init
913 * @adev: amdgpu_device pointer
915 * Does any asic specific work and then calls atom asic init.
917 static int amdgpu_device_asic_init(struct amdgpu_device *adev)
919 amdgpu_asic_pre_asic_init(adev);
921 return amdgpu_atom_asic_init(adev->mode_info.atom_context);
925 * amdgpu_device_vram_scratch_init - allocate the VRAM scratch page
927 * @adev: amdgpu_device pointer
929 * Allocates a scratch page of VRAM for use by various things in the
932 static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev)
934 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
935 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
936 &adev->vram_scratch.robj,
937 &adev->vram_scratch.gpu_addr,
938 (void **)&adev->vram_scratch.ptr);
942 * amdgpu_device_vram_scratch_fini - Free the VRAM scratch page
944 * @adev: amdgpu_device pointer
946 * Frees the VRAM scratch page.
948 static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev)
950 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
954 * amdgpu_device_program_register_sequence - program an array of registers.
956 * @adev: amdgpu_device pointer
957 * @registers: pointer to the register array
958 * @array_size: size of the register array
960 * Programs an array or registers with and and or masks.
961 * This is a helper for setting golden registers.
963 void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
964 const u32 *registers,
965 const u32 array_size)
967 u32 tmp, reg, and_mask, or_mask;
973 for (i = 0; i < array_size; i +=3) {
974 reg = registers[i + 0];
975 and_mask = registers[i + 1];
976 or_mask = registers[i + 2];
978 if (and_mask == 0xffffffff) {
983 if (adev->family >= AMDGPU_FAMILY_AI)
984 tmp |= (or_mask & and_mask);
993 * amdgpu_device_pci_config_reset - reset the GPU
995 * @adev: amdgpu_device pointer
997 * Resets the GPU using the pci config reset sequence.
998 * Only applicable to asics prior to vega10.
1000 void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
1002 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
1006 * amdgpu_device_pci_reset - reset the GPU using generic PCI means
1008 * @adev: amdgpu_device pointer
1010 * Resets the GPU using generic pci reset interfaces (FLR, SBR, etc.).
1012 int amdgpu_device_pci_reset(struct amdgpu_device *adev)
1014 return pci_reset_function(adev->pdev);
1018 * GPU doorbell aperture helpers function.
1021 * amdgpu_device_doorbell_init - Init doorbell driver information.
1023 * @adev: amdgpu_device pointer
1025 * Init doorbell driver information (CIK)
1026 * Returns 0 on success, error on failure.
1028 static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
1031 /* No doorbell on SI hardware generation */
1032 if (adev->asic_type < CHIP_BONAIRE) {
1033 adev->doorbell.base = 0;
1034 adev->doorbell.size = 0;
1035 adev->doorbell.num_doorbells = 0;
1036 adev->doorbell.ptr = NULL;
1040 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
1043 amdgpu_asic_init_doorbell_index(adev);
1045 /* doorbell bar mapping */
1046 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
1047 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
1049 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
1050 adev->doorbell_index.max_assignment+1);
1051 if (adev->doorbell.num_doorbells == 0)
1054 /* For Vega, reserve and map two pages on doorbell BAR since SDMA
1055 * paging queue doorbell use the second page. The
1056 * AMDGPU_DOORBELL64_MAX_ASSIGNMENT definition assumes all the
1057 * doorbells are in the first page. So with paging queue enabled,
1058 * the max num_doorbells should + 1 page (0x400 in dword)
1060 if (adev->asic_type >= CHIP_VEGA10)
1061 adev->doorbell.num_doorbells += 0x400;
1063 adev->doorbell.ptr = ioremap(adev->doorbell.base,
1064 adev->doorbell.num_doorbells *
1066 if (adev->doorbell.ptr == NULL)
1073 * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
1075 * @adev: amdgpu_device pointer
1077 * Tear down doorbell driver information (CIK)
1079 static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
1081 iounmap(adev->doorbell.ptr);
1082 adev->doorbell.ptr = NULL;
1088 * amdgpu_device_wb_*()
1089 * Writeback is the method by which the GPU updates special pages in memory
1090 * with the status of certain GPU events (fences, ring pointers,etc.).
1094 * amdgpu_device_wb_fini - Disable Writeback and free memory
1096 * @adev: amdgpu_device pointer
1098 * Disables Writeback and frees the Writeback memory (all asics).
1099 * Used at driver shutdown.
1101 static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
1103 if (adev->wb.wb_obj) {
1104 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
1106 (void **)&adev->wb.wb);
1107 adev->wb.wb_obj = NULL;
1112 * amdgpu_device_wb_init - Init Writeback driver info and allocate memory
1114 * @adev: amdgpu_device pointer
1116 * Initializes writeback and allocates writeback memory (all asics).
1117 * Used at driver startup.
1118 * Returns 0 on success or an -error on failure.
1120 static int amdgpu_device_wb_init(struct amdgpu_device *adev)
1124 if (adev->wb.wb_obj == NULL) {
1125 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
1126 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
1127 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
1128 &adev->wb.wb_obj, &adev->wb.gpu_addr,
1129 (void **)&adev->wb.wb);
1131 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
1135 adev->wb.num_wb = AMDGPU_MAX_WB;
1136 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
1138 /* clear wb memory */
1139 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
1146 * amdgpu_device_wb_get - Allocate a wb entry
1148 * @adev: amdgpu_device pointer
1151 * Allocate a wb slot for use by the driver (all asics).
1152 * Returns 0 on success or -EINVAL on failure.
1154 int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
1156 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
1158 if (offset < adev->wb.num_wb) {
1159 __set_bit(offset, adev->wb.used);
1160 *wb = offset << 3; /* convert to dw offset */
1168 * amdgpu_device_wb_free - Free a wb entry
1170 * @adev: amdgpu_device pointer
1173 * Free a wb slot allocated for use by the driver (all asics)
1175 void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
1178 if (wb < adev->wb.num_wb)
1179 __clear_bit(wb, adev->wb.used);
1183 * amdgpu_device_resize_fb_bar - try to resize FB BAR
1185 * @adev: amdgpu_device pointer
1187 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
1188 * to fail, but if any of the BARs is not accessible after the size we abort
1189 * driver loading by returning -ENODEV.
1191 int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
1193 int rbar_size = pci_rebar_bytes_to_size(adev->gmc.real_vram_size);
1194 struct pci_bus *root;
1195 struct resource *res;
1201 if (amdgpu_sriov_vf(adev))
1204 /* skip if the bios has already enabled large BAR */
1205 if (adev->gmc.real_vram_size &&
1206 (pci_resource_len(adev->pdev, 0) >= adev->gmc.real_vram_size))
1209 /* Check if the root BUS has 64bit memory resources */
1210 root = adev->pdev->bus;
1211 while (root->parent)
1212 root = root->parent;
1214 pci_bus_for_each_resource(root, res, i) {
1215 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
1216 res->start > 0x100000000ull)
1220 /* Trying to resize is pointless without a root hub window above 4GB */
1224 /* Limit the BAR size to what is available */
1225 rbar_size = min(fls(pci_rebar_get_possible_sizes(adev->pdev, 0)) - 1,
1228 /* Disable memory decoding while we change the BAR addresses and size */
1229 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
1230 pci_write_config_word(adev->pdev, PCI_COMMAND,
1231 cmd & ~PCI_COMMAND_MEMORY);
1233 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
1234 amdgpu_device_doorbell_fini(adev);
1235 if (adev->asic_type >= CHIP_BONAIRE)
1236 pci_release_resource(adev->pdev, 2);
1238 pci_release_resource(adev->pdev, 0);
1240 r = pci_resize_resource(adev->pdev, 0, rbar_size);
1242 DRM_INFO("Not enough PCI address space for a large BAR.");
1243 else if (r && r != -ENOTSUPP)
1244 DRM_ERROR("Problem resizing BAR0 (%d).", r);
1246 pci_assign_unassigned_bus_resources(adev->pdev->bus);
1248 /* When the doorbell or fb BAR isn't available we have no chance of
1251 r = amdgpu_device_doorbell_init(adev);
1252 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
1255 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
1261 * GPU helpers function.
1264 * amdgpu_device_need_post - check if the hw need post or not
1266 * @adev: amdgpu_device pointer
1268 * Check if the asic has been initialized (all asics) at driver startup
1269 * or post is needed if hw reset is performed.
1270 * Returns true if need or false if not.
1272 bool amdgpu_device_need_post(struct amdgpu_device *adev)
1276 if (amdgpu_sriov_vf(adev))
1279 if (amdgpu_passthrough(adev)) {
1280 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
1281 * some old smc fw still need driver do vPost otherwise gpu hang, while
1282 * those smc fw version above 22.15 doesn't have this flaw, so we force
1283 * vpost executed for smc version below 22.15
1285 if (adev->asic_type == CHIP_FIJI) {
1288 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
1289 /* force vPost if error occured */
1293 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
1294 if (fw_ver < 0x00160e00)
1299 /* Don't post if we need to reset whole hive on init */
1300 if (adev->gmc.xgmi.pending_reset)
1303 if (adev->has_hw_reset) {
1304 adev->has_hw_reset = false;
1308 /* bios scratch used on CIK+ */
1309 if (adev->asic_type >= CHIP_BONAIRE)
1310 return amdgpu_atombios_scratch_need_asic_init(adev);
1312 /* check MEM_SIZE for older asics */
1313 reg = amdgpu_asic_get_config_memsize(adev);
1315 if ((reg != 0) && (reg != 0xffffffff))
1322 * amdgpu_device_should_use_aspm - check if the device should program ASPM
1324 * @adev: amdgpu_device pointer
1326 * Confirm whether the module parameter and pcie bridge agree that ASPM should
1327 * be set for this device.
1329 * Returns true if it should be used or false if not.
1331 bool amdgpu_device_should_use_aspm(struct amdgpu_device *adev)
1333 switch (amdgpu_aspm) {
1343 return pcie_aspm_enabled(adev->pdev);
1346 /* if we get transitioned to only one device, take VGA back */
1348 * amdgpu_device_vga_set_decode - enable/disable vga decode
1350 * @pdev: PCI device pointer
1351 * @state: enable/disable vga decode
1353 * Enable/disable vga decode (all asics).
1354 * Returns VGA resource flags.
1356 static unsigned int amdgpu_device_vga_set_decode(struct pci_dev *pdev,
1359 struct amdgpu_device *adev = drm_to_adev(pci_get_drvdata(pdev));
1360 amdgpu_asic_set_vga_state(adev, state);
1362 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1363 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1365 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1369 * amdgpu_device_check_block_size - validate the vm block size
1371 * @adev: amdgpu_device pointer
1373 * Validates the vm block size specified via module parameter.
1374 * The vm block size defines number of bits in page table versus page directory,
1375 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1376 * page table and the remaining bits are in the page directory.
1378 static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
1380 /* defines number of bits in page table versus page directory,
1381 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1382 * page table and the remaining bits are in the page directory */
1383 if (amdgpu_vm_block_size == -1)
1386 if (amdgpu_vm_block_size < 9) {
1387 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1388 amdgpu_vm_block_size);
1389 amdgpu_vm_block_size = -1;
1394 * amdgpu_device_check_vm_size - validate the vm size
1396 * @adev: amdgpu_device pointer
1398 * Validates the vm size in GB specified via module parameter.
1399 * The VM size is the size of the GPU virtual memory space in GB.
1401 static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
1403 /* no need to check the default value */
1404 if (amdgpu_vm_size == -1)
1407 if (amdgpu_vm_size < 1) {
1408 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1410 amdgpu_vm_size = -1;
1414 static void amdgpu_device_check_smu_prv_buffer_size(struct amdgpu_device *adev)
1417 bool is_os_64 = (sizeof(void *) == 8);
1418 uint64_t total_memory;
1419 uint64_t dram_size_seven_GB = 0x1B8000000;
1420 uint64_t dram_size_three_GB = 0xB8000000;
1422 if (amdgpu_smu_memory_pool_size == 0)
1426 DRM_WARN("Not 64-bit OS, feature not supported\n");
1430 total_memory = (uint64_t)si.totalram * si.mem_unit;
1432 if ((amdgpu_smu_memory_pool_size == 1) ||
1433 (amdgpu_smu_memory_pool_size == 2)) {
1434 if (total_memory < dram_size_three_GB)
1436 } else if ((amdgpu_smu_memory_pool_size == 4) ||
1437 (amdgpu_smu_memory_pool_size == 8)) {
1438 if (total_memory < dram_size_seven_GB)
1441 DRM_WARN("Smu memory pool size not supported\n");
1444 adev->pm.smu_prv_buffer_size = amdgpu_smu_memory_pool_size << 28;
1449 DRM_WARN("No enough system memory\n");
1451 adev->pm.smu_prv_buffer_size = 0;
1454 static int amdgpu_device_init_apu_flags(struct amdgpu_device *adev)
1456 if (!(adev->flags & AMD_IS_APU) ||
1457 adev->asic_type < CHIP_RAVEN)
1460 switch (adev->asic_type) {
1462 if (adev->pdev->device == 0x15dd)
1463 adev->apu_flags |= AMD_APU_IS_RAVEN;
1464 if (adev->pdev->device == 0x15d8)
1465 adev->apu_flags |= AMD_APU_IS_PICASSO;
1468 if ((adev->pdev->device == 0x1636) ||
1469 (adev->pdev->device == 0x164c))
1470 adev->apu_flags |= AMD_APU_IS_RENOIR;
1472 adev->apu_flags |= AMD_APU_IS_GREEN_SARDINE;
1475 adev->apu_flags |= AMD_APU_IS_VANGOGH;
1477 case CHIP_YELLOW_CARP:
1479 case CHIP_CYAN_SKILLFISH:
1480 if ((adev->pdev->device == 0x13FE) ||
1481 (adev->pdev->device == 0x143F))
1482 adev->apu_flags |= AMD_APU_IS_CYAN_SKILLFISH2;
1492 * amdgpu_device_check_arguments - validate module params
1494 * @adev: amdgpu_device pointer
1496 * Validates certain module parameters and updates
1497 * the associated values used by the driver (all asics).
1499 static int amdgpu_device_check_arguments(struct amdgpu_device *adev)
1501 if (amdgpu_sched_jobs < 4) {
1502 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1504 amdgpu_sched_jobs = 4;
1505 } else if (!is_power_of_2(amdgpu_sched_jobs)){
1506 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1508 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1511 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
1512 /* gart size must be greater or equal to 32M */
1513 dev_warn(adev->dev, "gart size (%d) too small\n",
1515 amdgpu_gart_size = -1;
1518 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
1519 /* gtt size must be greater or equal to 32M */
1520 dev_warn(adev->dev, "gtt size (%d) too small\n",
1522 amdgpu_gtt_size = -1;
1525 /* valid range is between 4 and 9 inclusive */
1526 if (amdgpu_vm_fragment_size != -1 &&
1527 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1528 dev_warn(adev->dev, "valid range is between 4 and 9\n");
1529 amdgpu_vm_fragment_size = -1;
1532 if (amdgpu_sched_hw_submission < 2) {
1533 dev_warn(adev->dev, "sched hw submission jobs (%d) must be at least 2\n",
1534 amdgpu_sched_hw_submission);
1535 amdgpu_sched_hw_submission = 2;
1536 } else if (!is_power_of_2(amdgpu_sched_hw_submission)) {
1537 dev_warn(adev->dev, "sched hw submission jobs (%d) must be a power of 2\n",
1538 amdgpu_sched_hw_submission);
1539 amdgpu_sched_hw_submission = roundup_pow_of_two(amdgpu_sched_hw_submission);
1542 amdgpu_device_check_smu_prv_buffer_size(adev);
1544 amdgpu_device_check_vm_size(adev);
1546 amdgpu_device_check_block_size(adev);
1548 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
1550 amdgpu_gmc_tmz_set(adev);
1552 amdgpu_gmc_noretry_set(adev);
1558 * amdgpu_switcheroo_set_state - set switcheroo state
1560 * @pdev: pci dev pointer
1561 * @state: vga_switcheroo state
1563 * Callback for the switcheroo driver. Suspends or resumes the
1564 * the asics before or after it is powered up using ACPI methods.
1566 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev,
1567 enum vga_switcheroo_state state)
1569 struct drm_device *dev = pci_get_drvdata(pdev);
1572 if (amdgpu_device_supports_px(dev) && state == VGA_SWITCHEROO_OFF)
1575 if (state == VGA_SWITCHEROO_ON) {
1576 pr_info("switched on\n");
1577 /* don't suspend or resume card normally */
1578 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1580 pci_set_power_state(pdev, PCI_D0);
1581 amdgpu_device_load_pci_state(pdev);
1582 r = pci_enable_device(pdev);
1584 DRM_WARN("pci_enable_device failed (%d)\n", r);
1585 amdgpu_device_resume(dev, true);
1587 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1589 pr_info("switched off\n");
1590 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1591 amdgpu_device_suspend(dev, true);
1592 amdgpu_device_cache_pci_state(pdev);
1593 /* Shut down the device */
1594 pci_disable_device(pdev);
1595 pci_set_power_state(pdev, PCI_D3cold);
1596 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1601 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1603 * @pdev: pci dev pointer
1605 * Callback for the switcheroo driver. Check of the switcheroo
1606 * state can be changed.
1607 * Returns true if the state can be changed, false if not.
1609 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1611 struct drm_device *dev = pci_get_drvdata(pdev);
1614 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1615 * locking inversion with the driver load path. And the access here is
1616 * completely racy anyway. So don't bother with locking for now.
1618 return atomic_read(&dev->open_count) == 0;
1621 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1622 .set_gpu_state = amdgpu_switcheroo_set_state,
1624 .can_switch = amdgpu_switcheroo_can_switch,
1628 * amdgpu_device_ip_set_clockgating_state - set the CG state
1630 * @dev: amdgpu_device pointer
1631 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1632 * @state: clockgating state (gate or ungate)
1634 * Sets the requested clockgating state for all instances of
1635 * the hardware IP specified.
1636 * Returns the error code from the last instance.
1638 int amdgpu_device_ip_set_clockgating_state(void *dev,
1639 enum amd_ip_block_type block_type,
1640 enum amd_clockgating_state state)
1642 struct amdgpu_device *adev = dev;
1645 for (i = 0; i < adev->num_ip_blocks; i++) {
1646 if (!adev->ip_blocks[i].status.valid)
1648 if (adev->ip_blocks[i].version->type != block_type)
1650 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1652 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1653 (void *)adev, state);
1655 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1656 adev->ip_blocks[i].version->funcs->name, r);
1662 * amdgpu_device_ip_set_powergating_state - set the PG state
1664 * @dev: amdgpu_device pointer
1665 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1666 * @state: powergating state (gate or ungate)
1668 * Sets the requested powergating state for all instances of
1669 * the hardware IP specified.
1670 * Returns the error code from the last instance.
1672 int amdgpu_device_ip_set_powergating_state(void *dev,
1673 enum amd_ip_block_type block_type,
1674 enum amd_powergating_state state)
1676 struct amdgpu_device *adev = dev;
1679 for (i = 0; i < adev->num_ip_blocks; i++) {
1680 if (!adev->ip_blocks[i].status.valid)
1682 if (adev->ip_blocks[i].version->type != block_type)
1684 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1686 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1687 (void *)adev, state);
1689 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1690 adev->ip_blocks[i].version->funcs->name, r);
1696 * amdgpu_device_ip_get_clockgating_state - get the CG state
1698 * @adev: amdgpu_device pointer
1699 * @flags: clockgating feature flags
1701 * Walks the list of IPs on the device and updates the clockgating
1702 * flags for each IP.
1703 * Updates @flags with the feature flags for each hardware IP where
1704 * clockgating is enabled.
1706 void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
1711 for (i = 0; i < adev->num_ip_blocks; i++) {
1712 if (!adev->ip_blocks[i].status.valid)
1714 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1715 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1720 * amdgpu_device_ip_wait_for_idle - wait for idle
1722 * @adev: amdgpu_device pointer
1723 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1725 * Waits for the request hardware IP to be idle.
1726 * Returns 0 for success or a negative error code on failure.
1728 int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1729 enum amd_ip_block_type block_type)
1733 for (i = 0; i < adev->num_ip_blocks; i++) {
1734 if (!adev->ip_blocks[i].status.valid)
1736 if (adev->ip_blocks[i].version->type == block_type) {
1737 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
1748 * amdgpu_device_ip_is_idle - is the hardware IP idle
1750 * @adev: amdgpu_device pointer
1751 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1753 * Check if the hardware IP is idle or not.
1754 * Returns true if it the IP is idle, false if not.
1756 bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1757 enum amd_ip_block_type block_type)
1761 for (i = 0; i < adev->num_ip_blocks; i++) {
1762 if (!adev->ip_blocks[i].status.valid)
1764 if (adev->ip_blocks[i].version->type == block_type)
1765 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
1772 * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1774 * @adev: amdgpu_device pointer
1775 * @type: Type of hardware IP (SMU, GFX, UVD, etc.)
1777 * Returns a pointer to the hardware IP block structure
1778 * if it exists for the asic, otherwise NULL.
1780 struct amdgpu_ip_block *
1781 amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1782 enum amd_ip_block_type type)
1786 for (i = 0; i < adev->num_ip_blocks; i++)
1787 if (adev->ip_blocks[i].version->type == type)
1788 return &adev->ip_blocks[i];
1794 * amdgpu_device_ip_block_version_cmp
1796 * @adev: amdgpu_device pointer
1797 * @type: enum amd_ip_block_type
1798 * @major: major version
1799 * @minor: minor version
1801 * return 0 if equal or greater
1802 * return 1 if smaller or the ip_block doesn't exist
1804 int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1805 enum amd_ip_block_type type,
1806 u32 major, u32 minor)
1808 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
1810 if (ip_block && ((ip_block->version->major > major) ||
1811 ((ip_block->version->major == major) &&
1812 (ip_block->version->minor >= minor))))
1819 * amdgpu_device_ip_block_add
1821 * @adev: amdgpu_device pointer
1822 * @ip_block_version: pointer to the IP to add
1824 * Adds the IP block driver information to the collection of IPs
1827 int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1828 const struct amdgpu_ip_block_version *ip_block_version)
1830 if (!ip_block_version)
1833 switch (ip_block_version->type) {
1834 case AMD_IP_BLOCK_TYPE_VCN:
1835 if (adev->harvest_ip_mask & AMD_HARVEST_IP_VCN_MASK)
1838 case AMD_IP_BLOCK_TYPE_JPEG:
1839 if (adev->harvest_ip_mask & AMD_HARVEST_IP_JPEG_MASK)
1846 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
1847 ip_block_version->funcs->name);
1849 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1855 * amdgpu_device_enable_virtual_display - enable virtual display feature
1857 * @adev: amdgpu_device pointer
1859 * Enabled the virtual display feature if the user has enabled it via
1860 * the module parameter virtual_display. This feature provides a virtual
1861 * display hardware on headless boards or in virtualized environments.
1862 * This function parses and validates the configuration string specified by
1863 * the user and configues the virtual display configuration (number of
1864 * virtual connectors, crtcs, etc.) specified.
1866 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
1868 adev->enable_virtual_display = false;
1870 if (amdgpu_virtual_display) {
1871 const char *pci_address_name = pci_name(adev->pdev);
1872 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
1874 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1875 pciaddstr_tmp = pciaddstr;
1876 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1877 pciaddname = strsep(&pciaddname_tmp, ",");
1878 if (!strcmp("all", pciaddname)
1879 || !strcmp(pci_address_name, pciaddname)) {
1883 adev->enable_virtual_display = true;
1886 res = kstrtol(pciaddname_tmp, 10,
1894 adev->mode_info.num_crtc = num_crtc;
1896 adev->mode_info.num_crtc = 1;
1902 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1903 amdgpu_virtual_display, pci_address_name,
1904 adev->enable_virtual_display, adev->mode_info.num_crtc);
1911 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
1913 * @adev: amdgpu_device pointer
1915 * Parses the asic configuration parameters specified in the gpu info
1916 * firmware and makes them availale to the driver for use in configuring
1918 * Returns 0 on success, -EINVAL on failure.
1920 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1922 const char *chip_name;
1925 const struct gpu_info_firmware_header_v1_0 *hdr;
1927 adev->firmware.gpu_info_fw = NULL;
1929 if (adev->mman.discovery_bin) {
1930 amdgpu_discovery_get_gfx_info(adev);
1933 * FIXME: The bounding box is still needed by Navi12, so
1934 * temporarily read it from gpu_info firmware. Should be droped
1935 * when DAL no longer needs it.
1937 if (adev->asic_type != CHIP_NAVI12)
1941 switch (adev->asic_type) {
1942 #ifdef CONFIG_DRM_AMDGPU_SI
1949 #ifdef CONFIG_DRM_AMDGPU_CIK
1959 case CHIP_POLARIS10:
1960 case CHIP_POLARIS11:
1961 case CHIP_POLARIS12:
1966 case CHIP_ALDEBARAN:
1967 case CHIP_SIENNA_CICHLID:
1968 case CHIP_NAVY_FLOUNDER:
1969 case CHIP_DIMGREY_CAVEFISH:
1970 case CHIP_BEIGE_GOBY:
1974 chip_name = "vega10";
1977 chip_name = "vega12";
1980 if (adev->apu_flags & AMD_APU_IS_RAVEN2)
1981 chip_name = "raven2";
1982 else if (adev->apu_flags & AMD_APU_IS_PICASSO)
1983 chip_name = "picasso";
1985 chip_name = "raven";
1988 chip_name = "arcturus";
1991 if (adev->apu_flags & AMD_APU_IS_RENOIR)
1992 chip_name = "renoir";
1994 chip_name = "green_sardine";
1997 chip_name = "navi10";
2000 chip_name = "navi14";
2003 chip_name = "navi12";
2006 chip_name = "vangogh";
2008 case CHIP_YELLOW_CARP:
2009 chip_name = "yellow_carp";
2013 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
2014 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
2017 "Failed to load gpu_info firmware \"%s\"\n",
2021 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
2024 "Failed to validate gpu_info firmware \"%s\"\n",
2029 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
2030 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
2032 switch (hdr->version_major) {
2035 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
2036 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
2037 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2040 * Should be droped when DAL no longer needs it.
2042 if (adev->asic_type == CHIP_NAVI12)
2043 goto parse_soc_bounding_box;
2045 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
2046 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
2047 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
2048 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
2049 adev->gfx.config.max_texture_channel_caches =
2050 le32_to_cpu(gpu_info_fw->gc_num_tccs);
2051 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
2052 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
2053 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
2054 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
2055 adev->gfx.config.double_offchip_lds_buf =
2056 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
2057 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
2058 adev->gfx.cu_info.max_waves_per_simd =
2059 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
2060 adev->gfx.cu_info.max_scratch_slots_per_cu =
2061 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
2062 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
2063 if (hdr->version_minor >= 1) {
2064 const struct gpu_info_firmware_v1_1 *gpu_info_fw =
2065 (const struct gpu_info_firmware_v1_1 *)(adev->firmware.gpu_info_fw->data +
2066 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2067 adev->gfx.config.num_sc_per_sh =
2068 le32_to_cpu(gpu_info_fw->num_sc_per_sh);
2069 adev->gfx.config.num_packer_per_sc =
2070 le32_to_cpu(gpu_info_fw->num_packer_per_sc);
2073 parse_soc_bounding_box:
2075 * soc bounding box info is not integrated in disocovery table,
2076 * we always need to parse it from gpu info firmware if needed.
2078 if (hdr->version_minor == 2) {
2079 const struct gpu_info_firmware_v1_2 *gpu_info_fw =
2080 (const struct gpu_info_firmware_v1_2 *)(adev->firmware.gpu_info_fw->data +
2081 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2082 adev->dm.soc_bounding_box = &gpu_info_fw->soc_bounding_box;
2088 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
2097 * amdgpu_device_ip_early_init - run early init for hardware IPs
2099 * @adev: amdgpu_device pointer
2101 * Early initialization pass for hardware IPs. The hardware IPs that make
2102 * up each asic are discovered each IP's early_init callback is run. This
2103 * is the first stage in initializing the asic.
2104 * Returns 0 on success, negative error code on failure.
2106 static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
2108 struct drm_device *dev = adev_to_drm(adev);
2109 struct pci_dev *parent;
2112 amdgpu_device_enable_virtual_display(adev);
2114 if (amdgpu_sriov_vf(adev)) {
2115 r = amdgpu_virt_request_full_gpu(adev, true);
2120 switch (adev->asic_type) {
2121 #ifdef CONFIG_DRM_AMDGPU_SI
2127 adev->family = AMDGPU_FAMILY_SI;
2128 r = si_set_ip_blocks(adev);
2133 #ifdef CONFIG_DRM_AMDGPU_CIK
2139 if (adev->flags & AMD_IS_APU)
2140 adev->family = AMDGPU_FAMILY_KV;
2142 adev->family = AMDGPU_FAMILY_CI;
2144 r = cik_set_ip_blocks(adev);
2152 case CHIP_POLARIS10:
2153 case CHIP_POLARIS11:
2154 case CHIP_POLARIS12:
2158 if (adev->flags & AMD_IS_APU)
2159 adev->family = AMDGPU_FAMILY_CZ;
2161 adev->family = AMDGPU_FAMILY_VI;
2163 r = vi_set_ip_blocks(adev);
2168 r = amdgpu_discovery_set_ip_blocks(adev);
2174 if (amdgpu_has_atpx() &&
2175 (amdgpu_is_atpx_hybrid() ||
2176 amdgpu_has_atpx_dgpu_power_cntl()) &&
2177 ((adev->flags & AMD_IS_APU) == 0) &&
2178 !pci_is_thunderbolt_attached(to_pci_dev(dev->dev)))
2179 adev->flags |= AMD_IS_PX;
2181 parent = pci_upstream_bridge(adev->pdev);
2182 adev->has_pr3 = parent ? pci_pr3_present(parent) : false;
2184 amdgpu_amdkfd_device_probe(adev);
2186 adev->pm.pp_feature = amdgpu_pp_feature_mask;
2187 if (amdgpu_sriov_vf(adev) || sched_policy == KFD_SCHED_POLICY_NO_HWS)
2188 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
2189 if (amdgpu_sriov_vf(adev) && adev->asic_type == CHIP_SIENNA_CICHLID)
2190 adev->pm.pp_feature &= ~PP_OVERDRIVE_MASK;
2192 for (i = 0; i < adev->num_ip_blocks; i++) {
2193 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
2194 DRM_ERROR("disabled ip block: %d <%s>\n",
2195 i, adev->ip_blocks[i].version->funcs->name);
2196 adev->ip_blocks[i].status.valid = false;
2198 if (adev->ip_blocks[i].version->funcs->early_init) {
2199 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
2201 adev->ip_blocks[i].status.valid = false;
2203 DRM_ERROR("early_init of IP block <%s> failed %d\n",
2204 adev->ip_blocks[i].version->funcs->name, r);
2207 adev->ip_blocks[i].status.valid = true;
2210 adev->ip_blocks[i].status.valid = true;
2213 /* get the vbios after the asic_funcs are set up */
2214 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) {
2215 r = amdgpu_device_parse_gpu_info_fw(adev);
2220 if (!amdgpu_get_bios(adev))
2223 r = amdgpu_atombios_init(adev);
2225 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
2226 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
2230 /*get pf2vf msg info at it's earliest time*/
2231 if (amdgpu_sriov_vf(adev))
2232 amdgpu_virt_init_data_exchange(adev);
2237 adev->cg_flags &= amdgpu_cg_mask;
2238 adev->pg_flags &= amdgpu_pg_mask;
2243 static int amdgpu_device_ip_hw_init_phase1(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 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2253 (amdgpu_sriov_vf(adev) && (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)) ||
2254 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
2255 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2257 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2258 adev->ip_blocks[i].version->funcs->name, r);
2261 adev->ip_blocks[i].status.hw = true;
2268 static int amdgpu_device_ip_hw_init_phase2(struct amdgpu_device *adev)
2272 for (i = 0; i < adev->num_ip_blocks; i++) {
2273 if (!adev->ip_blocks[i].status.sw)
2275 if (adev->ip_blocks[i].status.hw)
2277 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2279 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2280 adev->ip_blocks[i].version->funcs->name, r);
2283 adev->ip_blocks[i].status.hw = true;
2289 static int amdgpu_device_fw_loading(struct amdgpu_device *adev)
2293 uint32_t smu_version;
2295 if (adev->asic_type >= CHIP_VEGA10) {
2296 for (i = 0; i < adev->num_ip_blocks; i++) {
2297 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_PSP)
2300 if (!adev->ip_blocks[i].status.sw)
2303 /* no need to do the fw loading again if already done*/
2304 if (adev->ip_blocks[i].status.hw == true)
2307 if (amdgpu_in_reset(adev) || adev->in_suspend) {
2308 r = adev->ip_blocks[i].version->funcs->resume(adev);
2310 DRM_ERROR("resume of IP block <%s> failed %d\n",
2311 adev->ip_blocks[i].version->funcs->name, r);
2315 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2317 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2318 adev->ip_blocks[i].version->funcs->name, r);
2323 adev->ip_blocks[i].status.hw = true;
2328 if (!amdgpu_sriov_vf(adev) || adev->asic_type == CHIP_TONGA)
2329 r = amdgpu_pm_load_smu_firmware(adev, &smu_version);
2334 static int amdgpu_device_init_schedulers(struct amdgpu_device *adev)
2339 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2340 struct amdgpu_ring *ring = adev->rings[i];
2342 /* No need to setup the GPU scheduler for rings that don't need it */
2343 if (!ring || ring->no_scheduler)
2346 switch (ring->funcs->type) {
2347 case AMDGPU_RING_TYPE_GFX:
2348 timeout = adev->gfx_timeout;
2350 case AMDGPU_RING_TYPE_COMPUTE:
2351 timeout = adev->compute_timeout;
2353 case AMDGPU_RING_TYPE_SDMA:
2354 timeout = adev->sdma_timeout;
2357 timeout = adev->video_timeout;
2361 r = drm_sched_init(&ring->sched, &amdgpu_sched_ops,
2362 ring->num_hw_submission, amdgpu_job_hang_limit,
2363 timeout, adev->reset_domain->wq,
2364 ring->sched_score, ring->name,
2367 DRM_ERROR("Failed to create scheduler on ring %s.\n",
2378 * amdgpu_device_ip_init - run init for hardware IPs
2380 * @adev: amdgpu_device pointer
2382 * Main initialization pass for hardware IPs. The list of all the hardware
2383 * IPs that make up the asic is walked and the sw_init and hw_init callbacks
2384 * are run. sw_init initializes the software state associated with each IP
2385 * and hw_init initializes the hardware associated with each IP.
2386 * Returns 0 on success, negative error code on failure.
2388 static int amdgpu_device_ip_init(struct amdgpu_device *adev)
2392 r = amdgpu_ras_init(adev);
2396 for (i = 0; i < adev->num_ip_blocks; i++) {
2397 if (!adev->ip_blocks[i].status.valid)
2399 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
2401 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
2402 adev->ip_blocks[i].version->funcs->name, r);
2405 adev->ip_blocks[i].status.sw = true;
2407 /* need to do gmc hw init early so we can allocate gpu mem */
2408 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
2409 /* Try to reserve bad pages early */
2410 if (amdgpu_sriov_vf(adev))
2411 amdgpu_virt_exchange_data(adev);
2413 r = amdgpu_device_vram_scratch_init(adev);
2415 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
2418 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
2420 DRM_ERROR("hw_init %d failed %d\n", i, r);
2423 r = amdgpu_device_wb_init(adev);
2425 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
2428 adev->ip_blocks[i].status.hw = true;
2430 /* right after GMC hw init, we create CSA */
2431 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) {
2432 r = amdgpu_allocate_static_csa(adev, &adev->virt.csa_obj,
2433 AMDGPU_GEM_DOMAIN_VRAM,
2436 DRM_ERROR("allocate CSA failed %d\n", r);
2443 if (amdgpu_sriov_vf(adev))
2444 amdgpu_virt_init_data_exchange(adev);
2446 r = amdgpu_ib_pool_init(adev);
2448 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
2449 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
2453 r = amdgpu_ucode_create_bo(adev); /* create ucode bo when sw_init complete*/
2457 r = amdgpu_device_ip_hw_init_phase1(adev);
2461 r = amdgpu_device_fw_loading(adev);
2465 r = amdgpu_device_ip_hw_init_phase2(adev);
2470 * retired pages will be loaded from eeprom and reserved here,
2471 * it should be called after amdgpu_device_ip_hw_init_phase2 since
2472 * for some ASICs the RAS EEPROM code relies on SMU fully functioning
2473 * for I2C communication which only true at this point.
2475 * amdgpu_ras_recovery_init may fail, but the upper only cares the
2476 * failure from bad gpu situation and stop amdgpu init process
2477 * accordingly. For other failed cases, it will still release all
2478 * the resource and print error message, rather than returning one
2479 * negative value to upper level.
2481 * Note: theoretically, this should be called before all vram allocations
2482 * to protect retired page from abusing
2484 r = amdgpu_ras_recovery_init(adev);
2489 * In case of XGMI grab extra reference for reset domain for this device
2491 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2492 if (amdgpu_xgmi_add_device(adev) == 0) {
2493 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
2495 if (!hive->reset_domain ||
2496 !amdgpu_reset_get_reset_domain(hive->reset_domain)) {
2501 /* Drop the early temporary reset domain we created for device */
2502 amdgpu_reset_put_reset_domain(adev->reset_domain);
2503 adev->reset_domain = hive->reset_domain;
2507 r = amdgpu_device_init_schedulers(adev);
2511 /* Don't init kfd if whole hive need to be reset during init */
2512 if (!adev->gmc.xgmi.pending_reset)
2513 amdgpu_amdkfd_device_init(adev);
2515 amdgpu_fru_get_product_info(adev);
2518 if (amdgpu_sriov_vf(adev))
2519 amdgpu_virt_release_full_gpu(adev, true);
2525 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
2527 * @adev: amdgpu_device pointer
2529 * Writes a reset magic value to the gart pointer in VRAM. The driver calls
2530 * this function before a GPU reset. If the value is retained after a
2531 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
2533 static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
2535 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
2539 * amdgpu_device_check_vram_lost - check if vram is valid
2541 * @adev: amdgpu_device pointer
2543 * Checks the reset magic value written to the gart pointer in VRAM.
2544 * The driver calls this after a GPU reset to see if the contents of
2545 * VRAM is lost or now.
2546 * returns true if vram is lost, false if not.
2548 static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
2550 if (memcmp(adev->gart.ptr, adev->reset_magic,
2551 AMDGPU_RESET_MAGIC_NUM))
2554 if (!amdgpu_in_reset(adev))
2558 * For all ASICs with baco/mode1 reset, the VRAM is
2559 * always assumed to be lost.
2561 switch (amdgpu_asic_reset_method(adev)) {
2562 case AMD_RESET_METHOD_BACO:
2563 case AMD_RESET_METHOD_MODE1:
2571 * amdgpu_device_set_cg_state - set clockgating for amdgpu device
2573 * @adev: amdgpu_device pointer
2574 * @state: clockgating state (gate or ungate)
2576 * The list of all the hardware IPs that make up the asic is walked and the
2577 * set_clockgating_state callbacks are run.
2578 * Late initialization pass enabling clockgating for hardware IPs.
2579 * Fini or suspend, pass disabling clockgating for hardware IPs.
2580 * Returns 0 on success, negative error code on failure.
2583 int amdgpu_device_set_cg_state(struct amdgpu_device *adev,
2584 enum amd_clockgating_state state)
2588 if (amdgpu_emu_mode == 1)
2591 for (j = 0; j < adev->num_ip_blocks; j++) {
2592 i = state == AMD_CG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
2593 if (!adev->ip_blocks[i].status.late_initialized)
2595 /* skip CG for GFX on S0ix */
2596 if (adev->in_s0ix &&
2597 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX)
2599 /* skip CG for VCE/UVD, it's handled specially */
2600 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
2601 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
2602 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
2603 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
2604 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
2605 /* enable clockgating to save power */
2606 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
2609 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
2610 adev->ip_blocks[i].version->funcs->name, r);
2619 int amdgpu_device_set_pg_state(struct amdgpu_device *adev,
2620 enum amd_powergating_state state)
2624 if (amdgpu_emu_mode == 1)
2627 for (j = 0; j < adev->num_ip_blocks; j++) {
2628 i = state == AMD_PG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
2629 if (!adev->ip_blocks[i].status.late_initialized)
2631 /* skip PG for GFX on S0ix */
2632 if (adev->in_s0ix &&
2633 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX)
2635 /* skip CG for VCE/UVD, it's handled specially */
2636 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
2637 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
2638 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
2639 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
2640 adev->ip_blocks[i].version->funcs->set_powergating_state) {
2641 /* enable powergating to save power */
2642 r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
2645 DRM_ERROR("set_powergating_state(gate) of IP block <%s> failed %d\n",
2646 adev->ip_blocks[i].version->funcs->name, r);
2654 static int amdgpu_device_enable_mgpu_fan_boost(void)
2656 struct amdgpu_gpu_instance *gpu_ins;
2657 struct amdgpu_device *adev;
2660 mutex_lock(&mgpu_info.mutex);
2663 * MGPU fan boost feature should be enabled
2664 * only when there are two or more dGPUs in
2667 if (mgpu_info.num_dgpu < 2)
2670 for (i = 0; i < mgpu_info.num_dgpu; i++) {
2671 gpu_ins = &(mgpu_info.gpu_ins[i]);
2672 adev = gpu_ins->adev;
2673 if (!(adev->flags & AMD_IS_APU) &&
2674 !gpu_ins->mgpu_fan_enabled) {
2675 ret = amdgpu_dpm_enable_mgpu_fan_boost(adev);
2679 gpu_ins->mgpu_fan_enabled = 1;
2684 mutex_unlock(&mgpu_info.mutex);
2690 * amdgpu_device_ip_late_init - run late init for hardware IPs
2692 * @adev: amdgpu_device pointer
2694 * Late initialization pass for hardware IPs. The list of all the hardware
2695 * IPs that make up the asic is walked and the late_init callbacks are run.
2696 * late_init covers any special initialization that an IP requires
2697 * after all of the have been initialized or something that needs to happen
2698 * late in the init process.
2699 * Returns 0 on success, negative error code on failure.
2701 static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
2703 struct amdgpu_gpu_instance *gpu_instance;
2706 for (i = 0; i < adev->num_ip_blocks; i++) {
2707 if (!adev->ip_blocks[i].status.hw)
2709 if (adev->ip_blocks[i].version->funcs->late_init) {
2710 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
2712 DRM_ERROR("late_init of IP block <%s> failed %d\n",
2713 adev->ip_blocks[i].version->funcs->name, r);
2717 adev->ip_blocks[i].status.late_initialized = true;
2720 r = amdgpu_ras_late_init(adev);
2722 DRM_ERROR("amdgpu_ras_late_init failed %d", r);
2726 amdgpu_ras_set_error_query_ready(adev, true);
2728 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE);
2729 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_GATE);
2731 amdgpu_device_fill_reset_magic(adev);
2733 r = amdgpu_device_enable_mgpu_fan_boost();
2735 DRM_ERROR("enable mgpu fan boost failed (%d).\n", r);
2737 /* For passthrough configuration on arcturus and aldebaran, enable special handling SBR */
2738 if (amdgpu_passthrough(adev) && ((adev->asic_type == CHIP_ARCTURUS && adev->gmc.xgmi.num_physical_nodes > 1)||
2739 adev->asic_type == CHIP_ALDEBARAN ))
2740 amdgpu_dpm_handle_passthrough_sbr(adev, true);
2742 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2743 mutex_lock(&mgpu_info.mutex);
2746 * Reset device p-state to low as this was booted with high.
2748 * This should be performed only after all devices from the same
2749 * hive get initialized.
2751 * However, it's unknown how many device in the hive in advance.
2752 * As this is counted one by one during devices initializations.
2754 * So, we wait for all XGMI interlinked devices initialized.
2755 * This may bring some delays as those devices may come from
2756 * different hives. But that should be OK.
2758 if (mgpu_info.num_dgpu == adev->gmc.xgmi.num_physical_nodes) {
2759 for (i = 0; i < mgpu_info.num_gpu; i++) {
2760 gpu_instance = &(mgpu_info.gpu_ins[i]);
2761 if (gpu_instance->adev->flags & AMD_IS_APU)
2764 r = amdgpu_xgmi_set_pstate(gpu_instance->adev,
2765 AMDGPU_XGMI_PSTATE_MIN);
2767 DRM_ERROR("pstate setting failed (%d).\n", r);
2773 mutex_unlock(&mgpu_info.mutex);
2780 * amdgpu_device_smu_fini_early - smu hw_fini wrapper
2782 * @adev: amdgpu_device pointer
2784 * For ASICs need to disable SMC first
2786 static void amdgpu_device_smu_fini_early(struct amdgpu_device *adev)
2790 if (adev->ip_versions[GC_HWIP][0] > IP_VERSION(9, 0, 0))
2793 for (i = 0; i < adev->num_ip_blocks; i++) {
2794 if (!adev->ip_blocks[i].status.hw)
2796 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
2797 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2798 /* XXX handle errors */
2800 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2801 adev->ip_blocks[i].version->funcs->name, r);
2803 adev->ip_blocks[i].status.hw = false;
2809 static int amdgpu_device_ip_fini_early(struct amdgpu_device *adev)
2813 for (i = 0; i < adev->num_ip_blocks; i++) {
2814 if (!adev->ip_blocks[i].version->funcs->early_fini)
2817 r = adev->ip_blocks[i].version->funcs->early_fini((void *)adev);
2819 DRM_DEBUG("early_fini of IP block <%s> failed %d\n",
2820 adev->ip_blocks[i].version->funcs->name, r);
2824 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2825 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2827 amdgpu_amdkfd_suspend(adev, false);
2829 /* Workaroud for ASICs need to disable SMC first */
2830 amdgpu_device_smu_fini_early(adev);
2832 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2833 if (!adev->ip_blocks[i].status.hw)
2836 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2837 /* XXX handle errors */
2839 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2840 adev->ip_blocks[i].version->funcs->name, r);
2843 adev->ip_blocks[i].status.hw = false;
2846 if (amdgpu_sriov_vf(adev)) {
2847 if (amdgpu_virt_release_full_gpu(adev, false))
2848 DRM_ERROR("failed to release exclusive mode on fini\n");
2855 * amdgpu_device_ip_fini - run fini for hardware IPs
2857 * @adev: amdgpu_device pointer
2859 * Main teardown pass for hardware IPs. The list of all the hardware
2860 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
2861 * are run. hw_fini tears down the hardware associated with each IP
2862 * and sw_fini tears down any software state associated with each IP.
2863 * Returns 0 on success, negative error code on failure.
2865 static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
2869 if (amdgpu_sriov_vf(adev) && adev->virt.ras_init_done)
2870 amdgpu_virt_release_ras_err_handler_data(adev);
2872 if (adev->gmc.xgmi.num_physical_nodes > 1)
2873 amdgpu_xgmi_remove_device(adev);
2875 amdgpu_amdkfd_device_fini_sw(adev);
2877 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2878 if (!adev->ip_blocks[i].status.sw)
2881 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
2882 amdgpu_ucode_free_bo(adev);
2883 amdgpu_free_static_csa(&adev->virt.csa_obj);
2884 amdgpu_device_wb_fini(adev);
2885 amdgpu_device_vram_scratch_fini(adev);
2886 amdgpu_ib_pool_fini(adev);
2889 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
2890 /* XXX handle errors */
2892 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
2893 adev->ip_blocks[i].version->funcs->name, r);
2895 adev->ip_blocks[i].status.sw = false;
2896 adev->ip_blocks[i].status.valid = false;
2899 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2900 if (!adev->ip_blocks[i].status.late_initialized)
2902 if (adev->ip_blocks[i].version->funcs->late_fini)
2903 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
2904 adev->ip_blocks[i].status.late_initialized = false;
2907 amdgpu_ras_fini(adev);
2913 * amdgpu_device_delayed_init_work_handler - work handler for IB tests
2915 * @work: work_struct.
2917 static void amdgpu_device_delayed_init_work_handler(struct work_struct *work)
2919 struct amdgpu_device *adev =
2920 container_of(work, struct amdgpu_device, delayed_init_work.work);
2923 r = amdgpu_ib_ring_tests(adev);
2925 DRM_ERROR("ib ring test failed (%d).\n", r);
2928 static void amdgpu_device_delay_enable_gfx_off(struct work_struct *work)
2930 struct amdgpu_device *adev =
2931 container_of(work, struct amdgpu_device, gfx.gfx_off_delay_work.work);
2933 WARN_ON_ONCE(adev->gfx.gfx_off_state);
2934 WARN_ON_ONCE(adev->gfx.gfx_off_req_count);
2936 if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true))
2937 adev->gfx.gfx_off_state = true;
2941 * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1)
2943 * @adev: amdgpu_device pointer
2945 * Main suspend function for hardware IPs. The list of all the hardware
2946 * IPs that make up the asic is walked, clockgating is disabled and the
2947 * suspend callbacks are run. suspend puts the hardware and software state
2948 * in each IP into a state suitable for suspend.
2949 * Returns 0 on success, negative error code on failure.
2951 static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev)
2955 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2956 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2958 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2959 if (!adev->ip_blocks[i].status.valid)
2962 /* displays are handled separately */
2963 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_DCE)
2966 /* XXX handle errors */
2967 r = adev->ip_blocks[i].version->funcs->suspend(adev);
2968 /* XXX handle errors */
2970 DRM_ERROR("suspend of IP block <%s> failed %d\n",
2971 adev->ip_blocks[i].version->funcs->name, r);
2975 adev->ip_blocks[i].status.hw = false;
2982 * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2)
2984 * @adev: amdgpu_device pointer
2986 * Main suspend function for hardware IPs. The list of all the hardware
2987 * IPs that make up the asic is walked, clockgating is disabled and the
2988 * suspend callbacks are run. suspend puts the hardware and software state
2989 * in each IP into a state suitable for suspend.
2990 * Returns 0 on success, negative error code on failure.
2992 static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev)
2997 amdgpu_dpm_gfx_state_change(adev, sGpuChangeState_D3Entry);
2999 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
3000 if (!adev->ip_blocks[i].status.valid)
3002 /* displays are handled in phase1 */
3003 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)
3005 /* PSP lost connection when err_event_athub occurs */
3006 if (amdgpu_ras_intr_triggered() &&
3007 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
3008 adev->ip_blocks[i].status.hw = false;
3012 /* skip unnecessary suspend if we do not initialize them yet */
3013 if (adev->gmc.xgmi.pending_reset &&
3014 !(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3015 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC ||
3016 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3017 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)) {
3018 adev->ip_blocks[i].status.hw = false;
3022 /* skip suspend of gfx and psp for S0ix
3023 * gfx is in gfxoff state, so on resume it will exit gfxoff just
3024 * like at runtime. PSP is also part of the always on hardware
3025 * so no need to suspend it.
3027 if (adev->in_s0ix &&
3028 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP ||
3029 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX))
3032 /* XXX handle errors */
3033 r = adev->ip_blocks[i].version->funcs->suspend(adev);
3034 /* XXX handle errors */
3036 DRM_ERROR("suspend of IP block <%s> failed %d\n",
3037 adev->ip_blocks[i].version->funcs->name, r);
3039 adev->ip_blocks[i].status.hw = false;
3040 /* handle putting the SMC in the appropriate state */
3041 if(!amdgpu_sriov_vf(adev)){
3042 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
3043 r = amdgpu_dpm_set_mp1_state(adev, adev->mp1_state);
3045 DRM_ERROR("SMC failed to set mp1 state %d, %d\n",
3046 adev->mp1_state, r);
3057 * amdgpu_device_ip_suspend - run suspend for hardware IPs
3059 * @adev: amdgpu_device pointer
3061 * Main suspend function for hardware IPs. The list of all the hardware
3062 * IPs that make up the asic is walked, clockgating is disabled and the
3063 * suspend callbacks are run. suspend puts the hardware and software state
3064 * in each IP into a state suitable for suspend.
3065 * Returns 0 on success, negative error code on failure.
3067 int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
3071 if (amdgpu_sriov_vf(adev)) {
3072 amdgpu_virt_fini_data_exchange(adev);
3073 amdgpu_virt_request_full_gpu(adev, false);
3076 r = amdgpu_device_ip_suspend_phase1(adev);
3079 r = amdgpu_device_ip_suspend_phase2(adev);
3081 if (amdgpu_sriov_vf(adev))
3082 amdgpu_virt_release_full_gpu(adev, false);
3087 static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
3091 static enum amd_ip_block_type ip_order[] = {
3092 AMD_IP_BLOCK_TYPE_GMC,
3093 AMD_IP_BLOCK_TYPE_COMMON,
3094 AMD_IP_BLOCK_TYPE_PSP,
3095 AMD_IP_BLOCK_TYPE_IH,
3098 for (i = 0; i < adev->num_ip_blocks; i++) {
3100 struct amdgpu_ip_block *block;
3102 block = &adev->ip_blocks[i];
3103 block->status.hw = false;
3105 for (j = 0; j < ARRAY_SIZE(ip_order); j++) {
3107 if (block->version->type != ip_order[j] ||
3108 !block->status.valid)
3111 r = block->version->funcs->hw_init(adev);
3112 DRM_INFO("RE-INIT-early: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
3115 block->status.hw = true;
3122 static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
3126 static enum amd_ip_block_type ip_order[] = {
3127 AMD_IP_BLOCK_TYPE_SMC,
3128 AMD_IP_BLOCK_TYPE_DCE,
3129 AMD_IP_BLOCK_TYPE_GFX,
3130 AMD_IP_BLOCK_TYPE_SDMA,
3131 AMD_IP_BLOCK_TYPE_UVD,
3132 AMD_IP_BLOCK_TYPE_VCE,
3133 AMD_IP_BLOCK_TYPE_VCN
3136 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
3138 struct amdgpu_ip_block *block;
3140 for (j = 0; j < adev->num_ip_blocks; j++) {
3141 block = &adev->ip_blocks[j];
3143 if (block->version->type != ip_order[i] ||
3144 !block->status.valid ||
3148 if (block->version->type == AMD_IP_BLOCK_TYPE_SMC)
3149 r = block->version->funcs->resume(adev);
3151 r = block->version->funcs->hw_init(adev);
3153 DRM_INFO("RE-INIT-late: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
3156 block->status.hw = true;
3164 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
3166 * @adev: amdgpu_device pointer
3168 * First resume function for hardware IPs. The list of all the hardware
3169 * IPs that make up the asic is walked and the resume callbacks are run for
3170 * COMMON, GMC, and IH. resume puts the hardware into a functional state
3171 * after a suspend and updates the software state as necessary. This
3172 * function is also used for restoring the GPU after a GPU reset.
3173 * Returns 0 on success, negative error code on failure.
3175 static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
3179 for (i = 0; i < adev->num_ip_blocks; i++) {
3180 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
3182 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3183 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3184 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
3186 r = adev->ip_blocks[i].version->funcs->resume(adev);
3188 DRM_ERROR("resume of IP block <%s> failed %d\n",
3189 adev->ip_blocks[i].version->funcs->name, r);
3192 adev->ip_blocks[i].status.hw = true;
3200 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
3202 * @adev: amdgpu_device pointer
3204 * First resume function for hardware IPs. The list of all the hardware
3205 * IPs that make up the asic is walked and the resume callbacks are run for
3206 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
3207 * functional state after a suspend and updates the software state as
3208 * necessary. This function is also used for restoring the GPU after a GPU
3210 * Returns 0 on success, negative error code on failure.
3212 static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
3216 for (i = 0; i < adev->num_ip_blocks; i++) {
3217 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
3219 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3220 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3221 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3222 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)
3224 r = adev->ip_blocks[i].version->funcs->resume(adev);
3226 DRM_ERROR("resume of IP block <%s> failed %d\n",
3227 adev->ip_blocks[i].version->funcs->name, r);
3230 adev->ip_blocks[i].status.hw = true;
3237 * amdgpu_device_ip_resume - run resume for hardware IPs
3239 * @adev: amdgpu_device pointer
3241 * Main resume function for hardware IPs. The hardware IPs
3242 * are split into two resume functions because they are
3243 * are also used in in recovering from a GPU reset and some additional
3244 * steps need to be take between them. In this case (S3/S4) they are
3246 * Returns 0 on success, negative error code on failure.
3248 static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
3252 r = amdgpu_amdkfd_resume_iommu(adev);
3256 r = amdgpu_device_ip_resume_phase1(adev);
3260 r = amdgpu_device_fw_loading(adev);
3264 r = amdgpu_device_ip_resume_phase2(adev);
3270 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
3272 * @adev: amdgpu_device pointer
3274 * Query the VBIOS data tables to determine if the board supports SR-IOV.
3276 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
3278 if (amdgpu_sriov_vf(adev)) {
3279 if (adev->is_atom_fw) {
3280 if (amdgpu_atomfirmware_gpu_virtualization_supported(adev))
3281 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
3283 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
3284 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
3287 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
3288 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
3293 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
3295 * @asic_type: AMD asic type
3297 * Check if there is DC (new modesetting infrastructre) support for an asic.
3298 * returns true if DC has support, false if not.
3300 bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
3302 switch (asic_type) {
3303 #ifdef CONFIG_DRM_AMDGPU_SI
3307 /* chips with no display hardware */
3309 #if defined(CONFIG_DRM_AMD_DC)
3315 * We have systems in the wild with these ASICs that require
3316 * LVDS and VGA support which is not supported with DC.
3318 * Fallback to the non-DC driver here by default so as not to
3319 * cause regressions.
3321 #if defined(CONFIG_DRM_AMD_DC_SI)
3322 return amdgpu_dc > 0;
3331 * We have systems in the wild with these ASICs that require
3332 * LVDS and VGA support which is not supported with DC.
3334 * Fallback to the non-DC driver here by default so as not to
3335 * cause regressions.
3337 return amdgpu_dc > 0;
3341 case CHIP_POLARIS10:
3342 case CHIP_POLARIS11:
3343 case CHIP_POLARIS12:
3350 #if defined(CONFIG_DRM_AMD_DC_DCN)
3356 case CHIP_CYAN_SKILLFISH:
3357 case CHIP_SIENNA_CICHLID:
3358 case CHIP_NAVY_FLOUNDER:
3359 case CHIP_DIMGREY_CAVEFISH:
3360 case CHIP_BEIGE_GOBY:
3362 case CHIP_YELLOW_CARP:
3365 return amdgpu_dc != 0;
3369 DRM_INFO_ONCE("Display Core has been requested via kernel parameter "
3370 "but isn't supported by ASIC, ignoring\n");
3377 * amdgpu_device_has_dc_support - check if dc is supported
3379 * @adev: amdgpu_device pointer
3381 * Returns true for supported, false for not supported
3383 bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
3385 if (amdgpu_sriov_vf(adev) ||
3386 adev->enable_virtual_display ||
3387 (adev->harvest_ip_mask & AMD_HARVEST_IP_DMU_MASK))
3390 return amdgpu_device_asic_has_dc_support(adev->asic_type);
3393 static void amdgpu_device_xgmi_reset_func(struct work_struct *__work)
3395 struct amdgpu_device *adev =
3396 container_of(__work, struct amdgpu_device, xgmi_reset_work);
3397 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
3399 /* It's a bug to not have a hive within this function */
3404 * Use task barrier to synchronize all xgmi reset works across the
3405 * hive. task_barrier_enter and task_barrier_exit will block
3406 * until all the threads running the xgmi reset works reach
3407 * those points. task_barrier_full will do both blocks.
3409 if (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) {
3411 task_barrier_enter(&hive->tb);
3412 adev->asic_reset_res = amdgpu_device_baco_enter(adev_to_drm(adev));
3414 if (adev->asic_reset_res)
3417 task_barrier_exit(&hive->tb);
3418 adev->asic_reset_res = amdgpu_device_baco_exit(adev_to_drm(adev));
3420 if (adev->asic_reset_res)
3423 if (adev->mmhub.ras && adev->mmhub.ras->ras_block.hw_ops &&
3424 adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count)
3425 adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count(adev);
3428 task_barrier_full(&hive->tb);
3429 adev->asic_reset_res = amdgpu_asic_reset(adev);
3433 if (adev->asic_reset_res)
3434 DRM_WARN("ASIC reset failed with error, %d for drm dev, %s",
3435 adev->asic_reset_res, adev_to_drm(adev)->unique);
3436 amdgpu_put_xgmi_hive(hive);
3439 static int amdgpu_device_get_job_timeout_settings(struct amdgpu_device *adev)
3441 char *input = amdgpu_lockup_timeout;
3442 char *timeout_setting = NULL;
3448 * By default timeout for non compute jobs is 10000
3449 * and 60000 for compute jobs.
3450 * In SR-IOV or passthrough mode, timeout for compute
3451 * jobs are 60000 by default.
3453 adev->gfx_timeout = msecs_to_jiffies(10000);
3454 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
3455 if (amdgpu_sriov_vf(adev))
3456 adev->compute_timeout = amdgpu_sriov_is_pp_one_vf(adev) ?
3457 msecs_to_jiffies(60000) : msecs_to_jiffies(10000);
3459 adev->compute_timeout = msecs_to_jiffies(60000);
3461 if (strnlen(input, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
3462 while ((timeout_setting = strsep(&input, ",")) &&
3463 strnlen(timeout_setting, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
3464 ret = kstrtol(timeout_setting, 0, &timeout);
3471 } else if (timeout < 0) {
3472 timeout = MAX_SCHEDULE_TIMEOUT;
3473 dev_warn(adev->dev, "lockup timeout disabled");
3474 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
3476 timeout = msecs_to_jiffies(timeout);
3481 adev->gfx_timeout = timeout;
3484 adev->compute_timeout = timeout;
3487 adev->sdma_timeout = timeout;
3490 adev->video_timeout = timeout;
3497 * There is only one value specified and
3498 * it should apply to all non-compute jobs.
3501 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
3502 if (amdgpu_sriov_vf(adev) || amdgpu_passthrough(adev))
3503 adev->compute_timeout = adev->gfx_timeout;
3511 * amdgpu_device_check_iommu_direct_map - check if RAM direct mapped to GPU
3513 * @adev: amdgpu_device pointer
3515 * RAM direct mapped to GPU if IOMMU is not enabled or is pass through mode
3517 static void amdgpu_device_check_iommu_direct_map(struct amdgpu_device *adev)
3519 struct iommu_domain *domain;
3521 domain = iommu_get_domain_for_dev(adev->dev);
3522 if (!domain || domain->type == IOMMU_DOMAIN_IDENTITY)
3523 adev->ram_is_direct_mapped = true;
3526 static const struct attribute *amdgpu_dev_attributes[] = {
3527 &dev_attr_product_name.attr,
3528 &dev_attr_product_number.attr,
3529 &dev_attr_serial_number.attr,
3530 &dev_attr_pcie_replay_count.attr,
3535 * amdgpu_device_init - initialize the driver
3537 * @adev: amdgpu_device pointer
3538 * @flags: driver flags
3540 * Initializes the driver info and hw (all asics).
3541 * Returns 0 for success or an error on failure.
3542 * Called at driver startup.
3544 int amdgpu_device_init(struct amdgpu_device *adev,
3547 struct drm_device *ddev = adev_to_drm(adev);
3548 struct pci_dev *pdev = adev->pdev;
3553 adev->shutdown = false;
3554 adev->flags = flags;
3556 if (amdgpu_force_asic_type >= 0 && amdgpu_force_asic_type < CHIP_LAST)
3557 adev->asic_type = amdgpu_force_asic_type;
3559 adev->asic_type = flags & AMD_ASIC_MASK;
3561 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
3562 if (amdgpu_emu_mode == 1)
3563 adev->usec_timeout *= 10;
3564 adev->gmc.gart_size = 512 * 1024 * 1024;
3565 adev->accel_working = false;
3566 adev->num_rings = 0;
3567 adev->mman.buffer_funcs = NULL;
3568 adev->mman.buffer_funcs_ring = NULL;
3569 adev->vm_manager.vm_pte_funcs = NULL;
3570 adev->vm_manager.vm_pte_num_scheds = 0;
3571 adev->gmc.gmc_funcs = NULL;
3572 adev->harvest_ip_mask = 0x0;
3573 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
3574 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
3576 adev->smc_rreg = &amdgpu_invalid_rreg;
3577 adev->smc_wreg = &amdgpu_invalid_wreg;
3578 adev->pcie_rreg = &amdgpu_invalid_rreg;
3579 adev->pcie_wreg = &amdgpu_invalid_wreg;
3580 adev->pciep_rreg = &amdgpu_invalid_rreg;
3581 adev->pciep_wreg = &amdgpu_invalid_wreg;
3582 adev->pcie_rreg64 = &amdgpu_invalid_rreg64;
3583 adev->pcie_wreg64 = &amdgpu_invalid_wreg64;
3584 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
3585 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
3586 adev->didt_rreg = &amdgpu_invalid_rreg;
3587 adev->didt_wreg = &amdgpu_invalid_wreg;
3588 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
3589 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
3590 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
3591 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
3593 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
3594 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
3595 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
3597 /* mutex initialization are all done here so we
3598 * can recall function without having locking issues */
3599 mutex_init(&adev->firmware.mutex);
3600 mutex_init(&adev->pm.mutex);
3601 mutex_init(&adev->gfx.gpu_clock_mutex);
3602 mutex_init(&adev->srbm_mutex);
3603 mutex_init(&adev->gfx.pipe_reserve_mutex);
3604 mutex_init(&adev->gfx.gfx_off_mutex);
3605 mutex_init(&adev->grbm_idx_mutex);
3606 mutex_init(&adev->mn_lock);
3607 mutex_init(&adev->virt.vf_errors.lock);
3608 hash_init(adev->mn_hash);
3609 mutex_init(&adev->psp.mutex);
3610 mutex_init(&adev->notifier_lock);
3611 mutex_init(&adev->pm.stable_pstate_ctx_lock);
3613 amdgpu_device_init_apu_flags(adev);
3615 r = amdgpu_device_check_arguments(adev);
3619 spin_lock_init(&adev->mmio_idx_lock);
3620 spin_lock_init(&adev->smc_idx_lock);
3621 spin_lock_init(&adev->pcie_idx_lock);
3622 spin_lock_init(&adev->uvd_ctx_idx_lock);
3623 spin_lock_init(&adev->didt_idx_lock);
3624 spin_lock_init(&adev->gc_cac_idx_lock);
3625 spin_lock_init(&adev->se_cac_idx_lock);
3626 spin_lock_init(&adev->audio_endpt_idx_lock);
3627 spin_lock_init(&adev->mm_stats.lock);
3629 INIT_LIST_HEAD(&adev->shadow_list);
3630 mutex_init(&adev->shadow_list_lock);
3632 INIT_LIST_HEAD(&adev->reset_list);
3634 INIT_LIST_HEAD(&adev->ras_list);
3636 INIT_DELAYED_WORK(&adev->delayed_init_work,
3637 amdgpu_device_delayed_init_work_handler);
3638 INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work,
3639 amdgpu_device_delay_enable_gfx_off);
3641 INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
3643 adev->gfx.gfx_off_req_count = 1;
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");
3682 if (amdgpu_mes && adev->asic_type >= CHIP_NAVI10)
3683 adev->enable_mes = true;
3685 /* detect hw virtualization here */
3686 amdgpu_detect_virtualization(adev);
3688 r = amdgpu_device_get_job_timeout_settings(adev);
3690 dev_err(adev->dev, "invalid lockup_timeout parameter syntax\n");
3695 * Reset domain needs to be present early, before XGMI hive discovered
3696 * (if any) and intitialized to use reset sem and in_gpu reset flag
3697 * early on during init.
3699 adev->reset_domain = amdgpu_reset_create_reset_domain(SINGLE_DEVICE ,"amdgpu-reset-dev");
3700 if (!adev->reset_domain)
3703 /* early init functions */
3704 r = amdgpu_device_ip_early_init(adev);
3708 /* Need to get xgmi info early to decide the reset behavior*/
3709 if (adev->gmc.xgmi.supported) {
3710 r = adev->gfxhub.funcs->get_xgmi_info(adev);
3715 /* enable PCIE atomic ops */
3716 if (amdgpu_sriov_vf(adev))
3717 adev->have_atomics_support = ((struct amd_sriov_msg_pf2vf_info *)
3718 adev->virt.fw_reserve.p_pf2vf)->pcie_atomic_ops_enabled_flags ==
3719 (PCI_EXP_DEVCAP2_ATOMIC_COMP32 | PCI_EXP_DEVCAP2_ATOMIC_COMP64);
3721 adev->have_atomics_support =
3722 !pci_enable_atomic_ops_to_root(adev->pdev,
3723 PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
3724 PCI_EXP_DEVCAP2_ATOMIC_COMP64);
3725 if (!adev->have_atomics_support)
3726 dev_info(adev->dev, "PCIE atomic ops is not supported\n");
3728 /* doorbell bar mapping and doorbell index init*/
3729 amdgpu_device_doorbell_init(adev);
3731 if (amdgpu_emu_mode == 1) {
3732 /* post the asic on emulation mode */
3733 emu_soc_asic_init(adev);
3734 goto fence_driver_init;
3737 amdgpu_reset_init(adev);
3739 /* detect if we are with an SRIOV vbios */
3740 amdgpu_device_detect_sriov_bios(adev);
3742 /* check if we need to reset the asic
3743 * E.g., driver was not cleanly unloaded previously, etc.
3745 if (!amdgpu_sriov_vf(adev) && amdgpu_asic_need_reset_on_init(adev)) {
3746 if (adev->gmc.xgmi.num_physical_nodes) {
3747 dev_info(adev->dev, "Pending hive reset.\n");
3748 adev->gmc.xgmi.pending_reset = true;
3749 /* Only need to init necessary block for SMU to handle the reset */
3750 for (i = 0; i < adev->num_ip_blocks; i++) {
3751 if (!adev->ip_blocks[i].status.valid)
3753 if (!(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3754 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3755 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3756 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC)) {
3757 DRM_DEBUG("IP %s disabled for hw_init.\n",
3758 adev->ip_blocks[i].version->funcs->name);
3759 adev->ip_blocks[i].status.hw = true;
3763 r = amdgpu_asic_reset(adev);
3765 dev_err(adev->dev, "asic reset on init failed\n");
3771 pci_enable_pcie_error_reporting(adev->pdev);
3773 /* Post card if necessary */
3774 if (amdgpu_device_need_post(adev)) {
3776 dev_err(adev->dev, "no vBIOS found\n");
3780 DRM_INFO("GPU posting now...\n");
3781 r = amdgpu_device_asic_init(adev);
3783 dev_err(adev->dev, "gpu post error!\n");
3788 if (adev->is_atom_fw) {
3789 /* Initialize clocks */
3790 r = amdgpu_atomfirmware_get_clock_info(adev);
3792 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
3793 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3797 /* Initialize clocks */
3798 r = amdgpu_atombios_get_clock_info(adev);
3800 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
3801 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3804 /* init i2c buses */
3805 if (!amdgpu_device_has_dc_support(adev))
3806 amdgpu_atombios_i2c_init(adev);
3811 r = amdgpu_fence_driver_sw_init(adev);
3813 dev_err(adev->dev, "amdgpu_fence_driver_sw_init failed\n");
3814 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
3818 /* init the mode config */
3819 drm_mode_config_init(adev_to_drm(adev));
3821 r = amdgpu_device_ip_init(adev);
3823 /* failed in exclusive mode due to timeout */
3824 if (amdgpu_sriov_vf(adev) &&
3825 !amdgpu_sriov_runtime(adev) &&
3826 amdgpu_virt_mmio_blocked(adev) &&
3827 !amdgpu_virt_wait_reset(adev)) {
3828 dev_err(adev->dev, "VF exclusive mode timeout\n");
3829 /* Don't send request since VF is inactive. */
3830 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
3831 adev->virt.ops = NULL;
3833 goto release_ras_con;
3835 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
3836 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
3837 goto release_ras_con;
3840 amdgpu_fence_driver_hw_init(adev);
3843 "SE %d, SH per SE %d, CU per SH %d, active_cu_number %d\n",
3844 adev->gfx.config.max_shader_engines,
3845 adev->gfx.config.max_sh_per_se,
3846 adev->gfx.config.max_cu_per_sh,
3847 adev->gfx.cu_info.number);
3849 adev->accel_working = true;
3851 amdgpu_vm_check_compute_bug(adev);
3853 /* Initialize the buffer migration limit. */
3854 if (amdgpu_moverate >= 0)
3855 max_MBps = amdgpu_moverate;
3857 max_MBps = 8; /* Allow 8 MB/s. */
3858 /* Get a log2 for easy divisions. */
3859 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
3861 r = amdgpu_pm_sysfs_init(adev);
3863 adev->pm_sysfs_en = false;
3864 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
3866 adev->pm_sysfs_en = true;
3868 r = amdgpu_ucode_sysfs_init(adev);
3870 adev->ucode_sysfs_en = false;
3871 DRM_ERROR("Creating firmware sysfs failed (%d).\n", r);
3873 adev->ucode_sysfs_en = true;
3875 if ((amdgpu_testing & 1)) {
3876 if (adev->accel_working)
3877 amdgpu_test_moves(adev);
3879 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
3881 if (amdgpu_benchmarking) {
3882 if (adev->accel_working)
3883 amdgpu_benchmark(adev, amdgpu_benchmarking);
3885 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
3889 * Register gpu instance before amdgpu_device_enable_mgpu_fan_boost.
3890 * Otherwise the mgpu fan boost feature will be skipped due to the
3891 * gpu instance is counted less.
3893 amdgpu_register_gpu_instance(adev);
3895 /* enable clockgating, etc. after ib tests, etc. since some blocks require
3896 * explicit gating rather than handling it automatically.
3898 if (!adev->gmc.xgmi.pending_reset) {
3899 r = amdgpu_device_ip_late_init(adev);
3901 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
3902 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
3903 goto release_ras_con;
3906 amdgpu_ras_resume(adev);
3907 queue_delayed_work(system_wq, &adev->delayed_init_work,
3908 msecs_to_jiffies(AMDGPU_RESUME_MS));
3911 if (amdgpu_sriov_vf(adev))
3912 flush_delayed_work(&adev->delayed_init_work);
3914 r = sysfs_create_files(&adev->dev->kobj, amdgpu_dev_attributes);
3916 dev_err(adev->dev, "Could not create amdgpu device attr\n");
3918 if (IS_ENABLED(CONFIG_PERF_EVENTS))
3919 r = amdgpu_pmu_init(adev);
3921 dev_err(adev->dev, "amdgpu_pmu_init failed\n");
3923 /* Have stored pci confspace at hand for restore in sudden PCI error */
3924 if (amdgpu_device_cache_pci_state(adev->pdev))
3925 pci_restore_state(pdev);
3927 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
3928 /* this will fail for cards that aren't VGA class devices, just
3930 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
3931 vga_client_register(adev->pdev, amdgpu_device_vga_set_decode);
3933 if (amdgpu_device_supports_px(ddev)) {
3935 vga_switcheroo_register_client(adev->pdev,
3936 &amdgpu_switcheroo_ops, px);
3937 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
3940 if (adev->gmc.xgmi.pending_reset)
3941 queue_delayed_work(system_wq, &mgpu_info.delayed_reset_work,
3942 msecs_to_jiffies(AMDGPU_RESUME_MS));
3944 amdgpu_device_check_iommu_direct_map(adev);
3949 amdgpu_release_ras_context(adev);
3952 amdgpu_vf_error_trans_all(adev);
3957 static void amdgpu_device_unmap_mmio(struct amdgpu_device *adev)
3960 /* Clear all CPU mappings pointing to this device */
3961 unmap_mapping_range(adev->ddev.anon_inode->i_mapping, 0, 0, 1);
3963 /* Unmap all mapped bars - Doorbell, registers and VRAM */
3964 amdgpu_device_doorbell_fini(adev);
3966 iounmap(adev->rmmio);
3968 if (adev->mman.aper_base_kaddr)
3969 iounmap(adev->mman.aper_base_kaddr);
3970 adev->mman.aper_base_kaddr = NULL;
3972 /* Memory manager related */
3973 if (!adev->gmc.xgmi.connected_to_cpu) {
3974 arch_phys_wc_del(adev->gmc.vram_mtrr);
3975 arch_io_free_memtype_wc(adev->gmc.aper_base, adev->gmc.aper_size);
3980 * amdgpu_device_fini_hw - tear down the driver
3982 * @adev: amdgpu_device pointer
3984 * Tear down the driver info (all asics).
3985 * Called at driver shutdown.
3987 void amdgpu_device_fini_hw(struct amdgpu_device *adev)
3989 dev_info(adev->dev, "amdgpu: finishing device.\n");
3990 flush_delayed_work(&adev->delayed_init_work);
3991 if (adev->mman.initialized) {
3992 flush_delayed_work(&adev->mman.bdev.wq);
3993 ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
3995 adev->shutdown = true;
3997 /* make sure IB test finished before entering exclusive mode
3998 * to avoid preemption on IB test
4000 if (amdgpu_sriov_vf(adev)) {
4001 amdgpu_virt_request_full_gpu(adev, false);
4002 amdgpu_virt_fini_data_exchange(adev);
4005 /* disable all interrupts */
4006 amdgpu_irq_disable_all(adev);
4007 if (adev->mode_info.mode_config_initialized){
4008 if (!drm_drv_uses_atomic_modeset(adev_to_drm(adev)))
4009 drm_helper_force_disable_all(adev_to_drm(adev));
4011 drm_atomic_helper_shutdown(adev_to_drm(adev));
4013 amdgpu_fence_driver_hw_fini(adev);
4015 if (adev->pm_sysfs_en)
4016 amdgpu_pm_sysfs_fini(adev);
4017 if (adev->ucode_sysfs_en)
4018 amdgpu_ucode_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)
4042 amdgpu_fence_driver_sw_fini(adev);
4043 amdgpu_device_ip_fini(adev);
4044 release_firmware(adev->firmware.gpu_info_fw);
4045 adev->firmware.gpu_info_fw = NULL;
4046 adev->accel_working = false;
4048 amdgpu_reset_fini(adev);
4050 /* free i2c buses */
4051 if (!amdgpu_device_has_dc_support(adev))
4052 amdgpu_i2c_fini(adev);
4054 if (amdgpu_emu_mode != 1)
4055 amdgpu_atombios_fini(adev);
4059 if (amdgpu_device_supports_px(adev_to_drm(adev))) {
4060 vga_switcheroo_unregister_client(adev->pdev);
4061 vga_switcheroo_fini_domain_pm_ops(adev->dev);
4063 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
4064 vga_client_unregister(adev->pdev);
4066 if (drm_dev_enter(adev_to_drm(adev), &idx)) {
4068 iounmap(adev->rmmio);
4070 amdgpu_device_doorbell_fini(adev);
4074 if (IS_ENABLED(CONFIG_PERF_EVENTS))
4075 amdgpu_pmu_fini(adev);
4076 if (adev->mman.discovery_bin)
4077 amdgpu_discovery_fini(adev);
4079 amdgpu_reset_put_reset_domain(adev->reset_domain);
4080 adev->reset_domain = NULL;
4082 kfree(adev->pci_state);
4087 * amdgpu_device_evict_resources - evict device resources
4088 * @adev: amdgpu device object
4090 * Evicts all ttm device resources(vram BOs, gart table) from the lru list
4091 * of the vram memory type. Mainly used for evicting device resources
4095 static void amdgpu_device_evict_resources(struct amdgpu_device *adev)
4097 /* No need to evict vram on APUs for suspend to ram or s2idle */
4098 if ((adev->in_s3 || adev->in_s0ix) && (adev->flags & AMD_IS_APU))
4101 if (amdgpu_ttm_evict_resources(adev, TTM_PL_VRAM))
4102 DRM_WARN("evicting device resources failed\n");
4110 * amdgpu_device_suspend - initiate device suspend
4112 * @dev: drm dev pointer
4113 * @fbcon : notify the fbdev of suspend
4115 * Puts the hw in the suspend state (all asics).
4116 * Returns 0 for success or an error on failure.
4117 * Called at driver suspend.
4119 int amdgpu_device_suspend(struct drm_device *dev, bool fbcon)
4121 struct amdgpu_device *adev = drm_to_adev(dev);
4123 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
4126 adev->in_suspend = true;
4128 if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DEV_D3))
4129 DRM_WARN("smart shift update failed\n");
4131 drm_kms_helper_poll_disable(dev);
4134 drm_fb_helper_set_suspend_unlocked(adev_to_drm(adev)->fb_helper, true);
4136 cancel_delayed_work_sync(&adev->delayed_init_work);
4138 amdgpu_ras_suspend(adev);
4140 amdgpu_device_ip_suspend_phase1(adev);
4143 amdgpu_amdkfd_suspend(adev, adev->in_runpm);
4145 amdgpu_device_evict_resources(adev);
4147 amdgpu_fence_driver_hw_fini(adev);
4149 amdgpu_device_ip_suspend_phase2(adev);
4155 * amdgpu_device_resume - initiate device resume
4157 * @dev: drm dev pointer
4158 * @fbcon : notify the fbdev of resume
4160 * Bring the hw back to operating state (all asics).
4161 * Returns 0 for success or an error on failure.
4162 * Called at driver resume.
4164 int amdgpu_device_resume(struct drm_device *dev, bool fbcon)
4166 struct amdgpu_device *adev = drm_to_adev(dev);
4169 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
4173 amdgpu_dpm_gfx_state_change(adev, sGpuChangeState_D0Entry);
4176 if (amdgpu_device_need_post(adev)) {
4177 r = amdgpu_device_asic_init(adev);
4179 dev_err(adev->dev, "amdgpu asic init failed\n");
4182 r = amdgpu_device_ip_resume(adev);
4184 dev_err(adev->dev, "amdgpu_device_ip_resume failed (%d).\n", r);
4187 amdgpu_fence_driver_hw_init(adev);
4189 r = amdgpu_device_ip_late_init(adev);
4193 queue_delayed_work(system_wq, &adev->delayed_init_work,
4194 msecs_to_jiffies(AMDGPU_RESUME_MS));
4196 if (!adev->in_s0ix) {
4197 r = amdgpu_amdkfd_resume(adev, adev->in_runpm);
4202 /* Make sure IB tests flushed */
4203 flush_delayed_work(&adev->delayed_init_work);
4206 drm_fb_helper_set_suspend_unlocked(adev_to_drm(adev)->fb_helper, false);
4208 drm_kms_helper_poll_enable(dev);
4210 amdgpu_ras_resume(adev);
4213 * Most of the connector probing functions try to acquire runtime pm
4214 * refs to ensure that the GPU is powered on when connector polling is
4215 * performed. Since we're calling this from a runtime PM callback,
4216 * trying to acquire rpm refs will cause us to deadlock.
4218 * Since we're guaranteed to be holding the rpm lock, it's safe to
4219 * temporarily disable the rpm helpers so this doesn't deadlock us.
4222 dev->dev->power.disable_depth++;
4224 if (!amdgpu_device_has_dc_support(adev))
4225 drm_helper_hpd_irq_event(dev);
4227 drm_kms_helper_hotplug_event(dev);
4229 dev->dev->power.disable_depth--;
4231 adev->in_suspend = false;
4233 if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DEV_D0))
4234 DRM_WARN("smart shift update failed\n");
4240 * amdgpu_device_ip_check_soft_reset - did soft reset succeed
4242 * @adev: amdgpu_device pointer
4244 * The list of all the hardware IPs that make up the asic is walked and
4245 * the check_soft_reset callbacks are run. check_soft_reset determines
4246 * if the asic is still hung or not.
4247 * Returns true if any of the IPs are still in a hung state, false if not.
4249 static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
4252 bool asic_hang = false;
4254 if (amdgpu_sriov_vf(adev))
4257 if (amdgpu_asic_need_full_reset(adev))
4260 for (i = 0; i < adev->num_ip_blocks; i++) {
4261 if (!adev->ip_blocks[i].status.valid)
4263 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
4264 adev->ip_blocks[i].status.hang =
4265 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
4266 if (adev->ip_blocks[i].status.hang) {
4267 dev_info(adev->dev, "IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
4275 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
4277 * @adev: amdgpu_device pointer
4279 * The list of all the hardware IPs that make up the asic is walked and the
4280 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
4281 * handles any IP specific hardware or software state changes that are
4282 * necessary for a soft reset to succeed.
4283 * Returns 0 on success, negative error code on failure.
4285 static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
4289 for (i = 0; i < adev->num_ip_blocks; i++) {
4290 if (!adev->ip_blocks[i].status.valid)
4292 if (adev->ip_blocks[i].status.hang &&
4293 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
4294 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
4304 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
4306 * @adev: amdgpu_device pointer
4308 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
4309 * reset is necessary to recover.
4310 * Returns true if a full asic reset is required, false if not.
4312 static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
4316 if (amdgpu_asic_need_full_reset(adev))
4319 for (i = 0; i < adev->num_ip_blocks; i++) {
4320 if (!adev->ip_blocks[i].status.valid)
4322 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
4323 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
4324 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
4325 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
4326 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
4327 if (adev->ip_blocks[i].status.hang) {
4328 dev_info(adev->dev, "Some block need full reset!\n");
4337 * amdgpu_device_ip_soft_reset - do a soft reset
4339 * @adev: amdgpu_device pointer
4341 * The list of all the hardware IPs that make up the asic is walked and the
4342 * soft_reset callbacks are run if the block is hung. soft_reset handles any
4343 * IP specific hardware or software state changes that are necessary to soft
4345 * Returns 0 on success, negative error code on failure.
4347 static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
4351 for (i = 0; i < adev->num_ip_blocks; i++) {
4352 if (!adev->ip_blocks[i].status.valid)
4354 if (adev->ip_blocks[i].status.hang &&
4355 adev->ip_blocks[i].version->funcs->soft_reset) {
4356 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
4366 * amdgpu_device_ip_post_soft_reset - clean up from soft reset
4368 * @adev: amdgpu_device pointer
4370 * The list of all the hardware IPs that make up the asic is walked and the
4371 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
4372 * handles any IP specific hardware or software state changes that are
4373 * necessary after the IP has been soft reset.
4374 * Returns 0 on success, negative error code on failure.
4376 static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
4380 for (i = 0; i < adev->num_ip_blocks; i++) {
4381 if (!adev->ip_blocks[i].status.valid)
4383 if (adev->ip_blocks[i].status.hang &&
4384 adev->ip_blocks[i].version->funcs->post_soft_reset)
4385 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
4394 * amdgpu_device_recover_vram - Recover some VRAM contents
4396 * @adev: amdgpu_device pointer
4398 * Restores the contents of VRAM buffers from the shadows in GTT. Used to
4399 * restore things like GPUVM page tables after a GPU reset where
4400 * the contents of VRAM might be lost.
4403 * 0 on success, negative error code on failure.
4405 static int amdgpu_device_recover_vram(struct amdgpu_device *adev)
4407 struct dma_fence *fence = NULL, *next = NULL;
4408 struct amdgpu_bo *shadow;
4409 struct amdgpu_bo_vm *vmbo;
4412 if (amdgpu_sriov_runtime(adev))
4413 tmo = msecs_to_jiffies(8000);
4415 tmo = msecs_to_jiffies(100);
4417 dev_info(adev->dev, "recover vram bo from shadow start\n");
4418 mutex_lock(&adev->shadow_list_lock);
4419 list_for_each_entry(vmbo, &adev->shadow_list, shadow_list) {
4421 /* No need to recover an evicted BO */
4422 if (shadow->tbo.resource->mem_type != TTM_PL_TT ||
4423 shadow->tbo.resource->start == AMDGPU_BO_INVALID_OFFSET ||
4424 shadow->parent->tbo.resource->mem_type != TTM_PL_VRAM)
4427 r = amdgpu_bo_restore_shadow(shadow, &next);
4432 tmo = dma_fence_wait_timeout(fence, false, tmo);
4433 dma_fence_put(fence);
4438 } else if (tmo < 0) {
4446 mutex_unlock(&adev->shadow_list_lock);
4449 tmo = dma_fence_wait_timeout(fence, false, tmo);
4450 dma_fence_put(fence);
4452 if (r < 0 || tmo <= 0) {
4453 dev_err(adev->dev, "recover vram bo from shadow failed, r is %ld, tmo is %ld\n", r, tmo);
4457 dev_info(adev->dev, "recover vram bo from shadow done\n");
4463 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
4465 * @adev: amdgpu_device pointer
4466 * @from_hypervisor: request from hypervisor
4468 * do VF FLR and reinitialize Asic
4469 * return 0 means succeeded otherwise failed
4471 static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
4472 bool from_hypervisor)
4475 struct amdgpu_hive_info *hive = NULL;
4476 int retry_limit = 0;
4479 amdgpu_amdkfd_pre_reset(adev);
4481 amdgpu_amdkfd_pre_reset(adev);
4483 if (from_hypervisor)
4484 r = amdgpu_virt_request_full_gpu(adev, true);
4486 r = amdgpu_virt_reset_gpu(adev);
4490 /* Resume IP prior to SMC */
4491 r = amdgpu_device_ip_reinit_early_sriov(adev);
4495 amdgpu_virt_init_data_exchange(adev);
4497 r = amdgpu_device_fw_loading(adev);
4501 /* now we are okay to resume SMC/CP/SDMA */
4502 r = amdgpu_device_ip_reinit_late_sriov(adev);
4506 hive = amdgpu_get_xgmi_hive(adev);
4507 /* Update PSP FW topology after reset */
4508 if (hive && adev->gmc.xgmi.num_physical_nodes > 1)
4509 r = amdgpu_xgmi_update_topology(hive, adev);
4512 amdgpu_put_xgmi_hive(hive);
4515 amdgpu_irq_gpu_reset_resume_helper(adev);
4516 r = amdgpu_ib_ring_tests(adev);
4517 amdgpu_amdkfd_post_reset(adev);
4521 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
4522 amdgpu_inc_vram_lost(adev);
4523 r = amdgpu_device_recover_vram(adev);
4525 amdgpu_virt_release_full_gpu(adev, true);
4527 if (AMDGPU_RETRY_SRIOV_RESET(r)) {
4528 if (retry_limit < AMDGPU_MAX_RETRY_LIMIT) {
4532 DRM_ERROR("GPU reset retry is beyond the retry limit\n");
4539 * amdgpu_device_has_job_running - check if there is any job in mirror list
4541 * @adev: amdgpu_device pointer
4543 * check if there is any job in mirror list
4545 bool amdgpu_device_has_job_running(struct amdgpu_device *adev)
4548 struct drm_sched_job *job;
4550 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4551 struct amdgpu_ring *ring = adev->rings[i];
4553 if (!ring || !ring->sched.thread)
4556 spin_lock(&ring->sched.job_list_lock);
4557 job = list_first_entry_or_null(&ring->sched.pending_list,
4558 struct drm_sched_job, list);
4559 spin_unlock(&ring->sched.job_list_lock);
4567 * amdgpu_device_should_recover_gpu - check if we should try GPU recovery
4569 * @adev: amdgpu_device pointer
4571 * Check amdgpu_gpu_recovery and SRIOV status to see if we should try to recover
4574 bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev)
4576 if (!amdgpu_device_ip_check_soft_reset(adev)) {
4577 dev_info(adev->dev, "Timeout, but no hardware hang detected.\n");
4581 if (amdgpu_gpu_recovery == 0)
4584 if (amdgpu_sriov_vf(adev))
4587 if (amdgpu_gpu_recovery == -1) {
4588 switch (adev->asic_type) {
4589 #ifdef CONFIG_DRM_AMDGPU_SI
4596 #ifdef CONFIG_DRM_AMDGPU_CIK
4603 case CHIP_CYAN_SKILLFISH:
4613 dev_info(adev->dev, "GPU recovery disabled.\n");
4617 int amdgpu_device_mode1_reset(struct amdgpu_device *adev)
4622 amdgpu_atombios_scratch_regs_engine_hung(adev, true);
4624 dev_info(adev->dev, "GPU mode1 reset\n");
4627 pci_clear_master(adev->pdev);
4629 amdgpu_device_cache_pci_state(adev->pdev);
4631 if (amdgpu_dpm_is_mode1_reset_supported(adev)) {
4632 dev_info(adev->dev, "GPU smu mode1 reset\n");
4633 ret = amdgpu_dpm_mode1_reset(adev);
4635 dev_info(adev->dev, "GPU psp mode1 reset\n");
4636 ret = psp_gpu_reset(adev);
4640 dev_err(adev->dev, "GPU mode1 reset failed\n");
4642 amdgpu_device_load_pci_state(adev->pdev);
4644 /* wait for asic to come out of reset */
4645 for (i = 0; i < adev->usec_timeout; i++) {
4646 u32 memsize = adev->nbio.funcs->get_memsize(adev);
4648 if (memsize != 0xffffffff)
4653 amdgpu_atombios_scratch_regs_engine_hung(adev, false);
4657 int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
4658 struct amdgpu_reset_context *reset_context)
4661 struct amdgpu_job *job = NULL;
4662 bool need_full_reset =
4663 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4665 if (reset_context->reset_req_dev == adev)
4666 job = reset_context->job;
4668 if (amdgpu_sriov_vf(adev)) {
4669 /* stop the data exchange thread */
4670 amdgpu_virt_fini_data_exchange(adev);
4673 /* block all schedulers and reset given job's ring */
4674 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4675 struct amdgpu_ring *ring = adev->rings[i];
4677 if (!ring || !ring->sched.thread)
4680 /*clear job fence from fence drv to avoid force_completion
4681 *leave NULL and vm flush fence in fence drv */
4682 amdgpu_fence_driver_clear_job_fences(ring);
4684 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
4685 amdgpu_fence_driver_force_completion(ring);
4689 drm_sched_increase_karma(&job->base);
4691 r = amdgpu_reset_prepare_hwcontext(adev, reset_context);
4692 /* If reset handler not implemented, continue; otherwise return */
4698 /* Don't suspend on bare metal if we are not going to HW reset the ASIC */
4699 if (!amdgpu_sriov_vf(adev)) {
4701 if (!need_full_reset)
4702 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
4704 if (!need_full_reset) {
4705 amdgpu_device_ip_pre_soft_reset(adev);
4706 r = amdgpu_device_ip_soft_reset(adev);
4707 amdgpu_device_ip_post_soft_reset(adev);
4708 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
4709 dev_info(adev->dev, "soft reset failed, will fallback to full reset!\n");
4710 need_full_reset = true;
4714 if (need_full_reset)
4715 r = amdgpu_device_ip_suspend(adev);
4716 if (need_full_reset)
4717 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4719 clear_bit(AMDGPU_NEED_FULL_RESET,
4720 &reset_context->flags);
4726 int amdgpu_do_asic_reset(struct list_head *device_list_handle,
4727 struct amdgpu_reset_context *reset_context)
4729 struct amdgpu_device *tmp_adev = NULL;
4730 bool need_full_reset, skip_hw_reset, vram_lost = false;
4733 /* Try reset handler method first */
4734 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
4736 r = amdgpu_reset_perform_reset(tmp_adev, reset_context);
4737 /* If reset handler not implemented, continue; otherwise return */
4743 /* Reset handler not implemented, use the default method */
4745 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4746 skip_hw_reset = test_bit(AMDGPU_SKIP_HW_RESET, &reset_context->flags);
4749 * ASIC reset has to be done on all XGMI hive nodes ASAP
4750 * to allow proper links negotiation in FW (within 1 sec)
4752 if (!skip_hw_reset && need_full_reset) {
4753 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4754 /* For XGMI run all resets in parallel to speed up the process */
4755 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
4756 tmp_adev->gmc.xgmi.pending_reset = false;
4757 if (!queue_work(system_unbound_wq, &tmp_adev->xgmi_reset_work))
4760 r = amdgpu_asic_reset(tmp_adev);
4763 dev_err(tmp_adev->dev, "ASIC reset failed with error, %d for drm dev, %s",
4764 r, adev_to_drm(tmp_adev)->unique);
4769 /* For XGMI wait for all resets to complete before proceed */
4771 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4772 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
4773 flush_work(&tmp_adev->xgmi_reset_work);
4774 r = tmp_adev->asic_reset_res;
4782 if (!r && amdgpu_ras_intr_triggered()) {
4783 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4784 if (tmp_adev->mmhub.ras && tmp_adev->mmhub.ras->ras_block.hw_ops &&
4785 tmp_adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count)
4786 tmp_adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count(tmp_adev);
4789 amdgpu_ras_intr_cleared();
4792 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4793 if (need_full_reset) {
4795 r = amdgpu_device_asic_init(tmp_adev);
4797 dev_warn(tmp_adev->dev, "asic atom init failed!");
4799 dev_info(tmp_adev->dev, "GPU reset succeeded, trying to resume\n");
4800 r = amdgpu_amdkfd_resume_iommu(tmp_adev);
4804 r = amdgpu_device_ip_resume_phase1(tmp_adev);
4808 vram_lost = amdgpu_device_check_vram_lost(tmp_adev);
4810 DRM_INFO("VRAM is lost due to GPU reset!\n");
4811 amdgpu_inc_vram_lost(tmp_adev);
4814 r = amdgpu_device_fw_loading(tmp_adev);
4818 r = amdgpu_device_ip_resume_phase2(tmp_adev);
4823 amdgpu_device_fill_reset_magic(tmp_adev);
4826 * Add this ASIC as tracked as reset was already
4827 * complete successfully.
4829 amdgpu_register_gpu_instance(tmp_adev);
4831 if (!reset_context->hive &&
4832 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
4833 amdgpu_xgmi_add_device(tmp_adev);
4835 r = amdgpu_device_ip_late_init(tmp_adev);
4839 drm_fb_helper_set_suspend_unlocked(adev_to_drm(tmp_adev)->fb_helper, false);
4842 * The GPU enters bad state once faulty pages
4843 * by ECC has reached the threshold, and ras
4844 * recovery is scheduled next. So add one check
4845 * here to break recovery if it indeed exceeds
4846 * bad page threshold, and remind user to
4847 * retire this GPU or setting one bigger
4848 * bad_page_threshold value to fix this once
4849 * probing driver again.
4851 if (!amdgpu_ras_eeprom_check_err_threshold(tmp_adev)) {
4853 amdgpu_ras_resume(tmp_adev);
4859 /* Update PSP FW topology after reset */
4860 if (reset_context->hive &&
4861 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
4862 r = amdgpu_xgmi_update_topology(
4863 reset_context->hive, tmp_adev);
4869 amdgpu_irq_gpu_reset_resume_helper(tmp_adev);
4870 r = amdgpu_ib_ring_tests(tmp_adev);
4872 dev_err(tmp_adev->dev, "ib ring test failed (%d).\n", r);
4873 need_full_reset = true;
4880 r = amdgpu_device_recover_vram(tmp_adev);
4882 tmp_adev->asic_reset_res = r;
4886 if (need_full_reset)
4887 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4889 clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4893 static void amdgpu_device_set_mp1_state(struct amdgpu_device *adev)
4896 switch (amdgpu_asic_reset_method(adev)) {
4897 case AMD_RESET_METHOD_MODE1:
4898 adev->mp1_state = PP_MP1_STATE_SHUTDOWN;
4900 case AMD_RESET_METHOD_MODE2:
4901 adev->mp1_state = PP_MP1_STATE_RESET;
4904 adev->mp1_state = PP_MP1_STATE_NONE;
4909 static void amdgpu_device_unset_mp1_state(struct amdgpu_device *adev)
4911 amdgpu_vf_error_trans_all(adev);
4912 adev->mp1_state = PP_MP1_STATE_NONE;
4915 static void amdgpu_device_resume_display_audio(struct amdgpu_device *adev)
4917 struct pci_dev *p = NULL;
4919 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
4920 adev->pdev->bus->number, 1);
4922 pm_runtime_enable(&(p->dev));
4923 pm_runtime_resume(&(p->dev));
4927 static int amdgpu_device_suspend_display_audio(struct amdgpu_device *adev)
4929 enum amd_reset_method reset_method;
4930 struct pci_dev *p = NULL;
4934 * For now, only BACO and mode1 reset are confirmed
4935 * to suffer the audio issue without proper suspended.
4937 reset_method = amdgpu_asic_reset_method(adev);
4938 if ((reset_method != AMD_RESET_METHOD_BACO) &&
4939 (reset_method != AMD_RESET_METHOD_MODE1))
4942 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
4943 adev->pdev->bus->number, 1);
4947 expires = pm_runtime_autosuspend_expiration(&(p->dev));
4950 * If we cannot get the audio device autosuspend delay,
4951 * a fixed 4S interval will be used. Considering 3S is
4952 * the audio controller default autosuspend delay setting.
4953 * 4S used here is guaranteed to cover that.
4955 expires = ktime_get_mono_fast_ns() + NSEC_PER_SEC * 4ULL;
4957 while (!pm_runtime_status_suspended(&(p->dev))) {
4958 if (!pm_runtime_suspend(&(p->dev)))
4961 if (expires < ktime_get_mono_fast_ns()) {
4962 dev_warn(adev->dev, "failed to suspend display audio\n");
4963 /* TODO: abort the succeeding gpu reset? */
4968 pm_runtime_disable(&(p->dev));
4973 static void amdgpu_device_recheck_guilty_jobs(
4974 struct amdgpu_device *adev, struct list_head *device_list_handle,
4975 struct amdgpu_reset_context *reset_context)
4979 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4980 struct amdgpu_ring *ring = adev->rings[i];
4982 struct drm_sched_job *s_job;
4984 if (!ring || !ring->sched.thread)
4987 s_job = list_first_entry_or_null(&ring->sched.pending_list,
4988 struct drm_sched_job, list);
4992 /* clear job's guilty and depend the folowing step to decide the real one */
4993 drm_sched_reset_karma(s_job);
4994 /* for the real bad job, it will be resubmitted twice, adding a dma_fence_get
4995 * to make sure fence is balanced */
4996 dma_fence_get(s_job->s_fence->parent);
4997 drm_sched_resubmit_jobs_ext(&ring->sched, 1);
4999 ret = dma_fence_wait_timeout(s_job->s_fence->parent, false, ring->sched.timeout);
5000 if (ret == 0) { /* timeout */
5001 DRM_ERROR("Found the real bad job! ring:%s, job_id:%llx\n",
5002 ring->sched.name, s_job->id);
5005 drm_sched_increase_karma(s_job);
5008 if (amdgpu_sriov_vf(adev)) {
5009 amdgpu_virt_fini_data_exchange(adev);
5010 r = amdgpu_device_reset_sriov(adev, false);
5012 adev->asic_reset_res = r;
5014 clear_bit(AMDGPU_SKIP_HW_RESET,
5015 &reset_context->flags);
5016 r = amdgpu_do_asic_reset(device_list_handle,
5018 if (r && r == -EAGAIN)
5023 * add reset counter so that the following
5024 * resubmitted job could flush vmid
5026 atomic_inc(&adev->gpu_reset_counter);
5030 /* got the hw fence, signal finished fence */
5031 atomic_dec(ring->sched.score);
5032 dma_fence_put(s_job->s_fence->parent);
5033 dma_fence_get(&s_job->s_fence->finished);
5034 dma_fence_signal(&s_job->s_fence->finished);
5035 dma_fence_put(&s_job->s_fence->finished);
5037 /* remove node from list and free the job */
5038 spin_lock(&ring->sched.job_list_lock);
5039 list_del_init(&s_job->list);
5040 spin_unlock(&ring->sched.job_list_lock);
5041 ring->sched.ops->free_job(s_job);
5046 * amdgpu_device_gpu_recover_imp - reset the asic and recover scheduler
5048 * @adev: amdgpu_device pointer
5049 * @job: which job trigger hang
5051 * Attempt to reset the GPU if it has hung (all asics).
5052 * Attempt to do soft-reset or full-reset and reinitialize Asic
5053 * Returns 0 for success or an error on failure.
5056 int amdgpu_device_gpu_recover_imp(struct amdgpu_device *adev,
5057 struct amdgpu_job *job)
5059 struct list_head device_list, *device_list_handle = NULL;
5060 bool job_signaled = false;
5061 struct amdgpu_hive_info *hive = NULL;
5062 struct amdgpu_device *tmp_adev = NULL;
5064 bool need_emergency_restart = false;
5065 bool audio_suspended = false;
5066 int tmp_vram_lost_counter;
5067 struct amdgpu_reset_context reset_context;
5069 memset(&reset_context, 0, sizeof(reset_context));
5072 * Special case: RAS triggered and full reset isn't supported
5074 need_emergency_restart = amdgpu_ras_need_emergency_restart(adev);
5077 * Flush RAM to disk so that after reboot
5078 * the user can read log and see why the system rebooted.
5080 if (need_emergency_restart && amdgpu_ras_get_context(adev)->reboot) {
5081 DRM_WARN("Emergency reboot.");
5084 emergency_restart();
5087 dev_info(adev->dev, "GPU %s begin!\n",
5088 need_emergency_restart ? "jobs stop":"reset");
5090 if (!amdgpu_sriov_vf(adev))
5091 hive = amdgpu_get_xgmi_hive(adev);
5093 mutex_lock(&hive->hive_lock);
5095 reset_context.method = AMD_RESET_METHOD_NONE;
5096 reset_context.reset_req_dev = adev;
5097 reset_context.job = job;
5098 reset_context.hive = hive;
5099 clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
5102 * Build list of devices to reset.
5103 * In case we are in XGMI hive mode, resort the device list
5104 * to put adev in the 1st position.
5106 INIT_LIST_HEAD(&device_list);
5107 if (!amdgpu_sriov_vf(adev) && (adev->gmc.xgmi.num_physical_nodes > 1)) {
5108 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head)
5109 list_add_tail(&tmp_adev->reset_list, &device_list);
5110 if (!list_is_first(&adev->reset_list, &device_list))
5111 list_rotate_to_front(&adev->reset_list, &device_list);
5112 device_list_handle = &device_list;
5114 list_add_tail(&adev->reset_list, &device_list);
5115 device_list_handle = &device_list;
5118 /* We need to lock reset domain only once both for XGMI and single device */
5119 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
5121 amdgpu_device_lock_reset_domain(tmp_adev->reset_domain);
5123 /* block all schedulers and reset given job's ring */
5124 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5126 amdgpu_device_set_mp1_state(tmp_adev);
5129 * Try to put the audio codec into suspend state
5130 * before gpu reset started.
5132 * Due to the power domain of the graphics device
5133 * is shared with AZ power domain. Without this,
5134 * we may change the audio hardware from behind
5135 * the audio driver's back. That will trigger
5136 * some audio codec errors.
5138 if (!amdgpu_device_suspend_display_audio(tmp_adev))
5139 audio_suspended = true;
5141 amdgpu_ras_set_error_query_ready(tmp_adev, false);
5143 cancel_delayed_work_sync(&tmp_adev->delayed_init_work);
5145 if (!amdgpu_sriov_vf(tmp_adev))
5146 amdgpu_amdkfd_pre_reset(tmp_adev);
5149 * Mark these ASICs to be reseted as untracked first
5150 * And add them back after reset completed
5152 amdgpu_unregister_gpu_instance(tmp_adev);
5154 drm_fb_helper_set_suspend_unlocked(adev_to_drm(adev)->fb_helper, true);
5156 /* disable ras on ALL IPs */
5157 if (!need_emergency_restart &&
5158 amdgpu_device_ip_need_full_reset(tmp_adev))
5159 amdgpu_ras_suspend(tmp_adev);
5161 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5162 struct amdgpu_ring *ring = tmp_adev->rings[i];
5164 if (!ring || !ring->sched.thread)
5167 drm_sched_stop(&ring->sched, job ? &job->base : NULL);
5169 if (need_emergency_restart)
5170 amdgpu_job_stop_all_jobs_on_sched(&ring->sched);
5172 atomic_inc(&tmp_adev->gpu_reset_counter);
5175 if (need_emergency_restart)
5176 goto skip_sched_resume;
5179 * Must check guilty signal here since after this point all old
5180 * HW fences are force signaled.
5182 * job->base holds a reference to parent fence
5184 if (job && job->base.s_fence->parent &&
5185 dma_fence_is_signaled(job->base.s_fence->parent)) {
5186 job_signaled = true;
5187 dev_info(adev->dev, "Guilty job already signaled, skipping HW reset");
5191 retry: /* Rest of adevs pre asic reset from XGMI hive. */
5192 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5193 r = amdgpu_device_pre_asic_reset(tmp_adev, &reset_context);
5194 /*TODO Should we stop ?*/
5196 dev_err(tmp_adev->dev, "GPU pre asic reset failed with err, %d for drm dev, %s ",
5197 r, adev_to_drm(tmp_adev)->unique);
5198 tmp_adev->asic_reset_res = r;
5202 tmp_vram_lost_counter = atomic_read(&((adev)->vram_lost_counter));
5203 /* Actual ASIC resets if needed.*/
5204 /* Host driver will handle XGMI hive reset for SRIOV */
5205 if (amdgpu_sriov_vf(adev)) {
5206 r = amdgpu_device_reset_sriov(adev, job ? false : true);
5208 adev->asic_reset_res = r;
5210 r = amdgpu_do_asic_reset(device_list_handle, &reset_context);
5211 if (r && r == -EAGAIN)
5217 /* Post ASIC reset for all devs .*/
5218 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5221 * Sometimes a later bad compute job can block a good gfx job as gfx
5222 * and compute ring share internal GC HW mutually. We add an additional
5223 * guilty jobs recheck step to find the real guilty job, it synchronously
5224 * submits and pends for the first job being signaled. If it gets timeout,
5225 * we identify it as a real guilty job.
5227 if (amdgpu_gpu_recovery == 2 &&
5228 !(tmp_vram_lost_counter < atomic_read(&adev->vram_lost_counter)))
5229 amdgpu_device_recheck_guilty_jobs(
5230 tmp_adev, device_list_handle, &reset_context);
5232 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5233 struct amdgpu_ring *ring = tmp_adev->rings[i];
5235 if (!ring || !ring->sched.thread)
5238 /* No point to resubmit jobs if we didn't HW reset*/
5239 if (!tmp_adev->asic_reset_res && !job_signaled)
5240 drm_sched_resubmit_jobs(&ring->sched);
5242 drm_sched_start(&ring->sched, !tmp_adev->asic_reset_res);
5245 if (!drm_drv_uses_atomic_modeset(adev_to_drm(tmp_adev)) && !job_signaled) {
5246 drm_helper_resume_force_mode(adev_to_drm(tmp_adev));
5249 if (tmp_adev->asic_reset_res)
5250 r = tmp_adev->asic_reset_res;
5252 tmp_adev->asic_reset_res = 0;
5255 /* bad news, how to tell it to userspace ? */
5256 dev_info(tmp_adev->dev, "GPU reset(%d) failed\n", atomic_read(&tmp_adev->gpu_reset_counter));
5257 amdgpu_vf_error_put(tmp_adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
5259 dev_info(tmp_adev->dev, "GPU reset(%d) succeeded!\n", atomic_read(&tmp_adev->gpu_reset_counter));
5260 if (amdgpu_acpi_smart_shift_update(adev_to_drm(tmp_adev), AMDGPU_SS_DEV_D0))
5261 DRM_WARN("smart shift update failed\n");
5266 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5267 /* unlock kfd: SRIOV would do it separately */
5268 if (!need_emergency_restart && !amdgpu_sriov_vf(tmp_adev))
5269 amdgpu_amdkfd_post_reset(tmp_adev);
5271 /* kfd_post_reset will do nothing if kfd device is not initialized,
5272 * need to bring up kfd here if it's not be initialized before
5274 if (!adev->kfd.init_complete)
5275 amdgpu_amdkfd_device_init(adev);
5277 if (audio_suspended)
5278 amdgpu_device_resume_display_audio(tmp_adev);
5280 amdgpu_device_unset_mp1_state(tmp_adev);
5283 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
5285 amdgpu_device_unlock_reset_domain(tmp_adev->reset_domain);
5288 mutex_unlock(&hive->hive_lock);
5289 amdgpu_put_xgmi_hive(hive);
5293 dev_info(adev->dev, "GPU reset end with ret = %d\n", r);
5297 struct amdgpu_recover_work_struct {
5298 struct work_struct base;
5299 struct amdgpu_device *adev;
5300 struct amdgpu_job *job;
5304 static void amdgpu_device_queue_gpu_recover_work(struct work_struct *work)
5306 struct amdgpu_recover_work_struct *recover_work = container_of(work, struct amdgpu_recover_work_struct, base);
5308 recover_work->ret = amdgpu_device_gpu_recover_imp(recover_work->adev, recover_work->job);
5311 * Serialize gpu recover into reset domain single threaded wq
5313 int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
5314 struct amdgpu_job *job)
5316 struct amdgpu_recover_work_struct work = {.adev = adev, .job = job};
5318 INIT_WORK(&work.base, amdgpu_device_queue_gpu_recover_work);
5320 if (!amdgpu_reset_domain_schedule(adev->reset_domain, &work.base))
5323 flush_work(&work.base);
5329 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
5331 * @adev: amdgpu_device pointer
5333 * Fetchs and stores in the driver the PCIE capabilities (gen speed
5334 * and lanes) of the slot the device is in. Handles APUs and
5335 * virtualized environments where PCIE config space may not be available.
5337 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
5339 struct pci_dev *pdev;
5340 enum pci_bus_speed speed_cap, platform_speed_cap;
5341 enum pcie_link_width platform_link_width;
5343 if (amdgpu_pcie_gen_cap)
5344 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
5346 if (amdgpu_pcie_lane_cap)
5347 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
5349 /* covers APUs as well */
5350 if (pci_is_root_bus(adev->pdev->bus)) {
5351 if (adev->pm.pcie_gen_mask == 0)
5352 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
5353 if (adev->pm.pcie_mlw_mask == 0)
5354 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
5358 if (adev->pm.pcie_gen_mask && adev->pm.pcie_mlw_mask)
5361 pcie_bandwidth_available(adev->pdev, NULL,
5362 &platform_speed_cap, &platform_link_width);
5364 if (adev->pm.pcie_gen_mask == 0) {
5367 speed_cap = pcie_get_speed_cap(pdev);
5368 if (speed_cap == PCI_SPEED_UNKNOWN) {
5369 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5370 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5371 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
5373 if (speed_cap == PCIE_SPEED_32_0GT)
5374 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5375 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5376 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5377 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4 |
5378 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN5);
5379 else if (speed_cap == PCIE_SPEED_16_0GT)
5380 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5381 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5382 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5383 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4);
5384 else if (speed_cap == PCIE_SPEED_8_0GT)
5385 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5386 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5387 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
5388 else if (speed_cap == PCIE_SPEED_5_0GT)
5389 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5390 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2);
5392 adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1;
5395 if (platform_speed_cap == PCI_SPEED_UNKNOWN) {
5396 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5397 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
5399 if (platform_speed_cap == PCIE_SPEED_32_0GT)
5400 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5401 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5402 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5403 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4 |
5404 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN5);
5405 else if (platform_speed_cap == PCIE_SPEED_16_0GT)
5406 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5407 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5408 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5409 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4);
5410 else if (platform_speed_cap == PCIE_SPEED_8_0GT)
5411 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5412 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5413 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3);
5414 else if (platform_speed_cap == PCIE_SPEED_5_0GT)
5415 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5416 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
5418 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
5422 if (adev->pm.pcie_mlw_mask == 0) {
5423 if (platform_link_width == PCIE_LNK_WIDTH_UNKNOWN) {
5424 adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK;
5426 switch (platform_link_width) {
5428 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
5429 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5430 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5431 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5432 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5433 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5434 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5437 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5438 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5439 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5440 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5441 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5442 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5445 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5446 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5447 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5448 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5449 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5452 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5453 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5454 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5455 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5458 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5459 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5460 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5463 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5464 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5467 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
5476 int amdgpu_device_baco_enter(struct drm_device *dev)
5478 struct amdgpu_device *adev = drm_to_adev(dev);
5479 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
5481 if (!amdgpu_device_supports_baco(adev_to_drm(adev)))
5484 if (ras && adev->ras_enabled &&
5485 adev->nbio.funcs->enable_doorbell_interrupt)
5486 adev->nbio.funcs->enable_doorbell_interrupt(adev, false);
5488 return amdgpu_dpm_baco_enter(adev);
5491 int amdgpu_device_baco_exit(struct drm_device *dev)
5493 struct amdgpu_device *adev = drm_to_adev(dev);
5494 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
5497 if (!amdgpu_device_supports_baco(adev_to_drm(adev)))
5500 ret = amdgpu_dpm_baco_exit(adev);
5504 if (ras && adev->ras_enabled &&
5505 adev->nbio.funcs->enable_doorbell_interrupt)
5506 adev->nbio.funcs->enable_doorbell_interrupt(adev, true);
5508 if (amdgpu_passthrough(adev) &&
5509 adev->nbio.funcs->clear_doorbell_interrupt)
5510 adev->nbio.funcs->clear_doorbell_interrupt(adev);
5516 * amdgpu_pci_error_detected - Called when a PCI error is detected.
5517 * @pdev: PCI device struct
5518 * @state: PCI channel state
5520 * Description: Called when a PCI error is detected.
5522 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT.
5524 pci_ers_result_t amdgpu_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
5526 struct drm_device *dev = pci_get_drvdata(pdev);
5527 struct amdgpu_device *adev = drm_to_adev(dev);
5530 DRM_INFO("PCI error: detected callback, state(%d)!!\n", state);
5532 if (adev->gmc.xgmi.num_physical_nodes > 1) {
5533 DRM_WARN("No support for XGMI hive yet...");
5534 return PCI_ERS_RESULT_DISCONNECT;
5537 adev->pci_channel_state = state;
5540 case pci_channel_io_normal:
5541 return PCI_ERS_RESULT_CAN_RECOVER;
5542 /* Fatal error, prepare for slot reset */
5543 case pci_channel_io_frozen:
5545 * Locking adev->reset_domain->sem will prevent any external access
5546 * to GPU during PCI error recovery
5548 amdgpu_device_lock_reset_domain(adev->reset_domain);
5549 amdgpu_device_set_mp1_state(adev);
5552 * Block any work scheduling as we do for regular GPU reset
5553 * for the duration of the recovery
5555 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5556 struct amdgpu_ring *ring = adev->rings[i];
5558 if (!ring || !ring->sched.thread)
5561 drm_sched_stop(&ring->sched, NULL);
5563 atomic_inc(&adev->gpu_reset_counter);
5564 return PCI_ERS_RESULT_NEED_RESET;
5565 case pci_channel_io_perm_failure:
5566 /* Permanent error, prepare for device removal */
5567 return PCI_ERS_RESULT_DISCONNECT;
5570 return PCI_ERS_RESULT_NEED_RESET;
5574 * amdgpu_pci_mmio_enabled - Enable MMIO and dump debug registers
5575 * @pdev: pointer to PCI device
5577 pci_ers_result_t amdgpu_pci_mmio_enabled(struct pci_dev *pdev)
5580 DRM_INFO("PCI error: mmio enabled callback!!\n");
5582 /* TODO - dump whatever for debugging purposes */
5584 /* This called only if amdgpu_pci_error_detected returns
5585 * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still
5586 * works, no need to reset slot.
5589 return PCI_ERS_RESULT_RECOVERED;
5593 * amdgpu_pci_slot_reset - Called when PCI slot has been reset.
5594 * @pdev: PCI device struct
5596 * Description: This routine is called by the pci error recovery
5597 * code after the PCI slot has been reset, just before we
5598 * should resume normal operations.
5600 pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev)
5602 struct drm_device *dev = pci_get_drvdata(pdev);
5603 struct amdgpu_device *adev = drm_to_adev(dev);
5605 struct amdgpu_reset_context reset_context;
5607 struct list_head device_list;
5609 DRM_INFO("PCI error: slot reset callback!!\n");
5611 memset(&reset_context, 0, sizeof(reset_context));
5613 INIT_LIST_HEAD(&device_list);
5614 list_add_tail(&adev->reset_list, &device_list);
5616 /* wait for asic to come out of reset */
5619 /* Restore PCI confspace */
5620 amdgpu_device_load_pci_state(pdev);
5622 /* confirm ASIC came out of reset */
5623 for (i = 0; i < adev->usec_timeout; i++) {
5624 memsize = amdgpu_asic_get_config_memsize(adev);
5626 if (memsize != 0xffffffff)
5630 if (memsize == 0xffffffff) {
5635 reset_context.method = AMD_RESET_METHOD_NONE;
5636 reset_context.reset_req_dev = adev;
5637 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
5638 set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
5640 adev->no_hw_access = true;
5641 r = amdgpu_device_pre_asic_reset(adev, &reset_context);
5642 adev->no_hw_access = false;
5646 r = amdgpu_do_asic_reset(&device_list, &reset_context);
5650 if (amdgpu_device_cache_pci_state(adev->pdev))
5651 pci_restore_state(adev->pdev);
5653 DRM_INFO("PCIe error recovery succeeded\n");
5655 DRM_ERROR("PCIe error recovery failed, err:%d", r);
5656 amdgpu_device_unset_mp1_state(adev);
5657 amdgpu_device_unlock_reset_domain(adev->reset_domain);
5660 return r ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
5664 * amdgpu_pci_resume() - resume normal ops after PCI reset
5665 * @pdev: pointer to PCI device
5667 * Called when the error recovery driver tells us that its
5668 * OK to resume normal operation.
5670 void amdgpu_pci_resume(struct pci_dev *pdev)
5672 struct drm_device *dev = pci_get_drvdata(pdev);
5673 struct amdgpu_device *adev = drm_to_adev(dev);
5677 DRM_INFO("PCI error: resume callback!!\n");
5679 /* Only continue execution for the case of pci_channel_io_frozen */
5680 if (adev->pci_channel_state != pci_channel_io_frozen)
5683 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5684 struct amdgpu_ring *ring = adev->rings[i];
5686 if (!ring || !ring->sched.thread)
5690 drm_sched_resubmit_jobs(&ring->sched);
5691 drm_sched_start(&ring->sched, true);
5694 amdgpu_device_unset_mp1_state(adev);
5695 amdgpu_device_unlock_reset_domain(adev->reset_domain);
5698 bool amdgpu_device_cache_pci_state(struct pci_dev *pdev)
5700 struct drm_device *dev = pci_get_drvdata(pdev);
5701 struct amdgpu_device *adev = drm_to_adev(dev);
5704 r = pci_save_state(pdev);
5706 kfree(adev->pci_state);
5708 adev->pci_state = pci_store_saved_state(pdev);
5710 if (!adev->pci_state) {
5711 DRM_ERROR("Failed to store PCI saved state");
5715 DRM_WARN("Failed to save PCI state, err:%d\n", r);
5722 bool amdgpu_device_load_pci_state(struct pci_dev *pdev)
5724 struct drm_device *dev = pci_get_drvdata(pdev);
5725 struct amdgpu_device *adev = drm_to_adev(dev);
5728 if (!adev->pci_state)
5731 r = pci_load_saved_state(pdev, adev->pci_state);
5734 pci_restore_state(pdev);
5736 DRM_WARN("Failed to load PCI state, err:%d\n", r);
5743 void amdgpu_device_flush_hdp(struct amdgpu_device *adev,
5744 struct amdgpu_ring *ring)
5746 #ifdef CONFIG_X86_64
5747 if (adev->flags & AMD_IS_APU)
5750 if (adev->gmc.xgmi.connected_to_cpu)
5753 if (ring && ring->funcs->emit_hdp_flush)
5754 amdgpu_ring_emit_hdp_flush(ring);
5756 amdgpu_asic_flush_hdp(adev, ring);
5759 void amdgpu_device_invalidate_hdp(struct amdgpu_device *adev,
5760 struct amdgpu_ring *ring)
5762 #ifdef CONFIG_X86_64
5763 if (adev->flags & AMD_IS_APU)
5766 if (adev->gmc.xgmi.connected_to_cpu)
5769 amdgpu_asic_invalidate_hdp(adev, ring);
5772 int amdgpu_in_reset(struct amdgpu_device *adev)
5774 return atomic_read(&adev->reset_domain->in_gpu_reset);
5778 * amdgpu_device_halt() - bring hardware to some kind of halt state
5780 * @adev: amdgpu_device pointer
5782 * Bring hardware to some kind of halt state so that no one can touch it
5783 * any more. It will help to maintain error context when error occurred.
5784 * Compare to a simple hang, the system will keep stable at least for SSH
5785 * access. Then it should be trivial to inspect the hardware state and
5786 * see what's going on. Implemented as following:
5788 * 1. drm_dev_unplug() makes device inaccessible to user space(IOCTLs, etc),
5789 * clears all CPU mappings to device, disallows remappings through page faults
5790 * 2. amdgpu_irq_disable_all() disables all interrupts
5791 * 3. amdgpu_fence_driver_hw_fini() signals all HW fences
5792 * 4. set adev->no_hw_access to avoid potential crashes after setp 5
5793 * 5. amdgpu_device_unmap_mmio() clears all MMIO mappings
5794 * 6. pci_disable_device() and pci_wait_for_pending_transaction()
5795 * flush any in flight DMA operations
5797 void amdgpu_device_halt(struct amdgpu_device *adev)
5799 struct pci_dev *pdev = adev->pdev;
5800 struct drm_device *ddev = adev_to_drm(adev);
5802 drm_dev_unplug(ddev);
5804 amdgpu_irq_disable_all(adev);
5806 amdgpu_fence_driver_hw_fini(adev);
5808 adev->no_hw_access = true;
5810 amdgpu_device_unmap_mmio(adev);
5812 pci_disable_device(pdev);
5813 pci_wait_for_pending_transaction(pdev);
5816 u32 amdgpu_device_pcie_port_rreg(struct amdgpu_device *adev,
5819 unsigned long flags, address, data;
5822 address = adev->nbio.funcs->get_pcie_port_index_offset(adev);
5823 data = adev->nbio.funcs->get_pcie_port_data_offset(adev);
5825 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
5826 WREG32(address, reg * 4);
5827 (void)RREG32(address);
5829 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
5833 void amdgpu_device_pcie_port_wreg(struct amdgpu_device *adev,
5836 unsigned long flags, address, data;
5838 address = adev->nbio.funcs->get_pcie_port_index_offset(adev);
5839 data = adev->nbio.funcs->get_pcie_port_data_offset(adev);
5841 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
5842 WREG32(address, reg * 4);
5843 (void)RREG32(address);
5846 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);