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>
34 #include <drm/drm_atomic_helper.h>
35 #include <drm/drm_probe_helper.h>
36 #include <drm/amdgpu_drm.h>
37 #include <linux/vgaarb.h>
38 #include <linux/vga_switcheroo.h>
39 #include <linux/efi.h>
41 #include "amdgpu_trace.h"
42 #include "amdgpu_i2c.h"
44 #include "amdgpu_atombios.h"
45 #include "amdgpu_atomfirmware.h"
47 #ifdef CONFIG_DRM_AMDGPU_SI
50 #ifdef CONFIG_DRM_AMDGPU_CIK
56 #include "bif/bif_4_1_d.h"
57 #include <linux/pci.h>
58 #include <linux/firmware.h>
59 #include "amdgpu_vf_error.h"
61 #include "amdgpu_amdkfd.h"
62 #include "amdgpu_pm.h"
64 #include "amdgpu_xgmi.h"
65 #include "amdgpu_ras.h"
66 #include "amdgpu_pmu.h"
67 #include "amdgpu_fru_eeprom.h"
68 #include "amdgpu_reset.h"
70 #include <linux/suspend.h>
71 #include <drm/task_barrier.h>
72 #include <linux/pm_runtime.h>
74 #include <drm/drm_drv.h>
76 MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
77 MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
78 MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
79 MODULE_FIRMWARE("amdgpu/picasso_gpu_info.bin");
80 MODULE_FIRMWARE("amdgpu/raven2_gpu_info.bin");
81 MODULE_FIRMWARE("amdgpu/arcturus_gpu_info.bin");
82 MODULE_FIRMWARE("amdgpu/renoir_gpu_info.bin");
83 MODULE_FIRMWARE("amdgpu/navi10_gpu_info.bin");
84 MODULE_FIRMWARE("amdgpu/navi14_gpu_info.bin");
85 MODULE_FIRMWARE("amdgpu/navi12_gpu_info.bin");
86 MODULE_FIRMWARE("amdgpu/vangogh_gpu_info.bin");
88 #define AMDGPU_RESUME_MS 2000
90 const char *amdgpu_asic_name[] = {
128 * DOC: pcie_replay_count
130 * The amdgpu driver provides a sysfs API for reporting the total number
131 * of PCIe replays (NAKs)
132 * The file pcie_replay_count is used for this and returns the total
133 * number of replays as a sum of the NAKs generated and NAKs received
136 static ssize_t amdgpu_device_get_pcie_replay_count(struct device *dev,
137 struct device_attribute *attr, char *buf)
139 struct drm_device *ddev = dev_get_drvdata(dev);
140 struct amdgpu_device *adev = drm_to_adev(ddev);
141 uint64_t cnt = amdgpu_asic_get_pcie_replay_count(adev);
143 return sysfs_emit(buf, "%llu\n", cnt);
146 static DEVICE_ATTR(pcie_replay_count, S_IRUGO,
147 amdgpu_device_get_pcie_replay_count, NULL);
149 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev);
154 * The amdgpu driver provides a sysfs API for reporting the product name
156 * The file serial_number is used for this and returns the product name
157 * as returned from the FRU.
158 * NOTE: This is only available for certain server cards
161 static ssize_t amdgpu_device_get_product_name(struct device *dev,
162 struct device_attribute *attr, char *buf)
164 struct drm_device *ddev = dev_get_drvdata(dev);
165 struct amdgpu_device *adev = drm_to_adev(ddev);
167 return sysfs_emit(buf, "%s\n", adev->product_name);
170 static DEVICE_ATTR(product_name, S_IRUGO,
171 amdgpu_device_get_product_name, NULL);
174 * DOC: product_number
176 * The amdgpu driver provides a sysfs API for reporting the part number
178 * The file serial_number is used for this and returns the part number
179 * as returned from the FRU.
180 * NOTE: This is only available for certain server cards
183 static ssize_t amdgpu_device_get_product_number(struct device *dev,
184 struct device_attribute *attr, char *buf)
186 struct drm_device *ddev = dev_get_drvdata(dev);
187 struct amdgpu_device *adev = drm_to_adev(ddev);
189 return sysfs_emit(buf, "%s\n", adev->product_number);
192 static DEVICE_ATTR(product_number, S_IRUGO,
193 amdgpu_device_get_product_number, NULL);
198 * The amdgpu driver provides a sysfs API for reporting the serial number
200 * The file serial_number is used for this and returns the serial number
201 * as returned from the FRU.
202 * NOTE: This is only available for certain server cards
205 static ssize_t amdgpu_device_get_serial_number(struct device *dev,
206 struct device_attribute *attr, char *buf)
208 struct drm_device *ddev = dev_get_drvdata(dev);
209 struct amdgpu_device *adev = drm_to_adev(ddev);
211 return sysfs_emit(buf, "%s\n", adev->serial);
214 static DEVICE_ATTR(serial_number, S_IRUGO,
215 amdgpu_device_get_serial_number, NULL);
218 * amdgpu_device_supports_px - Is the device a dGPU with ATPX power control
220 * @dev: drm_device pointer
222 * Returns true if the device is a dGPU with ATPX power control,
223 * otherwise return false.
225 bool amdgpu_device_supports_px(struct drm_device *dev)
227 struct amdgpu_device *adev = drm_to_adev(dev);
229 if ((adev->flags & AMD_IS_PX) && !amdgpu_is_atpx_hybrid())
235 * amdgpu_device_supports_boco - Is the device a dGPU with ACPI power resources
237 * @dev: drm_device pointer
239 * Returns true if the device is a dGPU with ACPI power control,
240 * otherwise return false.
242 bool amdgpu_device_supports_boco(struct drm_device *dev)
244 struct amdgpu_device *adev = drm_to_adev(dev);
247 ((adev->flags & AMD_IS_PX) && amdgpu_is_atpx_hybrid()))
253 * amdgpu_device_supports_baco - Does the device support BACO
255 * @dev: drm_device pointer
257 * Returns true if the device supporte BACO,
258 * otherwise return false.
260 bool amdgpu_device_supports_baco(struct drm_device *dev)
262 struct amdgpu_device *adev = drm_to_adev(dev);
264 return amdgpu_asic_supports_baco(adev);
268 * VRAM access helper functions
272 * amdgpu_device_vram_access - read/write a buffer in vram
274 * @adev: amdgpu_device pointer
275 * @pos: offset of the buffer in vram
276 * @buf: virtual address of the buffer in system memory
277 * @size: read/write size, sizeof(@buf) must > @size
278 * @write: true - write to vram, otherwise - read from vram
280 void amdgpu_device_vram_access(struct amdgpu_device *adev, loff_t pos,
281 uint32_t *buf, size_t size, bool write)
288 if (!drm_dev_enter(&adev->ddev, &idx))
292 last = min(pos + size, adev->gmc.visible_vram_size);
294 void __iomem *addr = adev->mman.aper_base_kaddr + pos;
295 size_t count = last - pos;
298 memcpy_toio(addr, buf, count);
300 amdgpu_asic_flush_hdp(adev, NULL);
302 amdgpu_asic_invalidate_hdp(adev, NULL);
304 memcpy_fromio(buf, addr, count);
316 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
317 for (last = pos + size; pos < last; pos += 4) {
318 uint32_t tmp = pos >> 31;
320 WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)pos) | 0x80000000);
322 WREG32_NO_KIQ(mmMM_INDEX_HI, tmp);
326 WREG32_NO_KIQ(mmMM_DATA, *buf++);
328 *buf++ = RREG32_NO_KIQ(mmMM_DATA);
330 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
337 * register access helper functions.
340 /* Check if hw access should be skipped because of hotplug or device error */
341 bool amdgpu_device_skip_hw_access(struct amdgpu_device *adev)
343 if (adev->in_pci_err_recovery)
346 #ifdef CONFIG_LOCKDEP
348 * This is a bit complicated to understand, so worth a comment. What we assert
349 * here is that the GPU reset is not running on another thread in parallel.
351 * For this we trylock the read side of the reset semaphore, if that succeeds
352 * we know that the reset is not running in paralell.
354 * If the trylock fails we assert that we are either already holding the read
355 * side of the lock or are the reset thread itself and hold the write side of
359 if (down_read_trylock(&adev->reset_sem))
360 up_read(&adev->reset_sem);
362 lockdep_assert_held(&adev->reset_sem);
369 * amdgpu_device_rreg - read a memory mapped IO or indirect register
371 * @adev: amdgpu_device pointer
372 * @reg: dword aligned register offset
373 * @acc_flags: access flags which require special behavior
375 * Returns the 32 bit value from the offset specified.
377 uint32_t amdgpu_device_rreg(struct amdgpu_device *adev,
378 uint32_t reg, uint32_t acc_flags)
382 if (amdgpu_device_skip_hw_access(adev))
385 if ((reg * 4) < adev->rmmio_size) {
386 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
387 amdgpu_sriov_runtime(adev) &&
388 down_read_trylock(&adev->reset_sem)) {
389 ret = amdgpu_kiq_rreg(adev, reg);
390 up_read(&adev->reset_sem);
392 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
395 ret = adev->pcie_rreg(adev, reg * 4);
398 trace_amdgpu_device_rreg(adev->pdev->device, reg, ret);
404 * MMIO register read with bytes helper functions
405 * @offset:bytes offset from MMIO start
410 * amdgpu_mm_rreg8 - read a memory mapped IO register
412 * @adev: amdgpu_device pointer
413 * @offset: byte aligned register offset
415 * Returns the 8 bit value from the offset specified.
417 uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset)
419 if (amdgpu_device_skip_hw_access(adev))
422 if (offset < adev->rmmio_size)
423 return (readb(adev->rmmio + offset));
428 * MMIO register write with bytes helper functions
429 * @offset:bytes offset from MMIO start
430 * @value: the value want to be written to the register
434 * amdgpu_mm_wreg8 - read a memory mapped IO register
436 * @adev: amdgpu_device pointer
437 * @offset: byte aligned register offset
438 * @value: 8 bit value to write
440 * Writes the value specified to the offset specified.
442 void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value)
444 if (amdgpu_device_skip_hw_access(adev))
447 if (offset < adev->rmmio_size)
448 writeb(value, adev->rmmio + offset);
454 * amdgpu_device_wreg - write to a memory mapped IO or indirect register
456 * @adev: amdgpu_device pointer
457 * @reg: dword aligned register offset
458 * @v: 32 bit value to write to the register
459 * @acc_flags: access flags which require special behavior
461 * Writes the value specified to the offset specified.
463 void amdgpu_device_wreg(struct amdgpu_device *adev,
464 uint32_t reg, uint32_t v,
467 if (amdgpu_device_skip_hw_access(adev))
470 if ((reg * 4) < adev->rmmio_size) {
471 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
472 amdgpu_sriov_runtime(adev) &&
473 down_read_trylock(&adev->reset_sem)) {
474 amdgpu_kiq_wreg(adev, reg, v);
475 up_read(&adev->reset_sem);
477 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
480 adev->pcie_wreg(adev, reg * 4, v);
483 trace_amdgpu_device_wreg(adev->pdev->device, reg, v);
487 * amdgpu_mm_wreg_mmio_rlc - write register either with mmio or with RLC path if in range
489 * this function is invoked only the debugfs register access
491 void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
492 uint32_t reg, uint32_t v)
494 if (amdgpu_device_skip_hw_access(adev))
497 if (amdgpu_sriov_fullaccess(adev) &&
498 adev->gfx.rlc.funcs &&
499 adev->gfx.rlc.funcs->is_rlcg_access_range) {
500 if (adev->gfx.rlc.funcs->is_rlcg_access_range(adev, reg))
501 return adev->gfx.rlc.funcs->rlcg_wreg(adev, reg, v, 0);
503 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
508 * amdgpu_mm_rdoorbell - read a doorbell dword
510 * @adev: amdgpu_device pointer
511 * @index: doorbell index
513 * Returns the value in the doorbell aperture at the
514 * requested doorbell index (CIK).
516 u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
518 if (amdgpu_device_skip_hw_access(adev))
521 if (index < adev->doorbell.num_doorbells) {
522 return readl(adev->doorbell.ptr + index);
524 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
530 * amdgpu_mm_wdoorbell - write a doorbell dword
532 * @adev: amdgpu_device pointer
533 * @index: doorbell index
536 * Writes @v to the doorbell aperture at the
537 * requested doorbell index (CIK).
539 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
541 if (amdgpu_device_skip_hw_access(adev))
544 if (index < adev->doorbell.num_doorbells) {
545 writel(v, adev->doorbell.ptr + index);
547 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
552 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
554 * @adev: amdgpu_device pointer
555 * @index: doorbell index
557 * Returns the value in the doorbell aperture at the
558 * requested doorbell index (VEGA10+).
560 u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
562 if (amdgpu_device_skip_hw_access(adev))
565 if (index < adev->doorbell.num_doorbells) {
566 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
568 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
574 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
576 * @adev: amdgpu_device pointer
577 * @index: doorbell index
580 * Writes @v to the doorbell aperture at the
581 * requested doorbell index (VEGA10+).
583 void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
585 if (amdgpu_device_skip_hw_access(adev))
588 if (index < adev->doorbell.num_doorbells) {
589 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
591 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
596 * amdgpu_device_indirect_rreg - read an indirect register
598 * @adev: amdgpu_device pointer
599 * @pcie_index: mmio register offset
600 * @pcie_data: mmio register offset
601 * @reg_addr: indirect register address to read from
603 * Returns the value of indirect register @reg_addr
605 u32 amdgpu_device_indirect_rreg(struct amdgpu_device *adev,
606 u32 pcie_index, u32 pcie_data,
611 void __iomem *pcie_index_offset;
612 void __iomem *pcie_data_offset;
614 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
615 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
616 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
618 writel(reg_addr, pcie_index_offset);
619 readl(pcie_index_offset);
620 r = readl(pcie_data_offset);
621 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
627 * amdgpu_device_indirect_rreg64 - read a 64bits indirect register
629 * @adev: amdgpu_device pointer
630 * @pcie_index: mmio register offset
631 * @pcie_data: mmio register offset
632 * @reg_addr: indirect register address to read from
634 * Returns the value of indirect register @reg_addr
636 u64 amdgpu_device_indirect_rreg64(struct amdgpu_device *adev,
637 u32 pcie_index, u32 pcie_data,
642 void __iomem *pcie_index_offset;
643 void __iomem *pcie_data_offset;
645 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
646 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
647 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
649 /* read low 32 bits */
650 writel(reg_addr, pcie_index_offset);
651 readl(pcie_index_offset);
652 r = readl(pcie_data_offset);
653 /* read high 32 bits */
654 writel(reg_addr + 4, pcie_index_offset);
655 readl(pcie_index_offset);
656 r |= ((u64)readl(pcie_data_offset) << 32);
657 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
663 * amdgpu_device_indirect_wreg - write an indirect register address
665 * @adev: amdgpu_device pointer
666 * @pcie_index: mmio register offset
667 * @pcie_data: mmio register offset
668 * @reg_addr: indirect register offset
669 * @reg_data: indirect register data
672 void amdgpu_device_indirect_wreg(struct amdgpu_device *adev,
673 u32 pcie_index, u32 pcie_data,
674 u32 reg_addr, u32 reg_data)
677 void __iomem *pcie_index_offset;
678 void __iomem *pcie_data_offset;
680 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
681 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
682 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
684 writel(reg_addr, pcie_index_offset);
685 readl(pcie_index_offset);
686 writel(reg_data, pcie_data_offset);
687 readl(pcie_data_offset);
688 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
692 * amdgpu_device_indirect_wreg64 - write a 64bits indirect register address
694 * @adev: amdgpu_device pointer
695 * @pcie_index: mmio register offset
696 * @pcie_data: mmio register offset
697 * @reg_addr: indirect register offset
698 * @reg_data: indirect register data
701 void amdgpu_device_indirect_wreg64(struct amdgpu_device *adev,
702 u32 pcie_index, u32 pcie_data,
703 u32 reg_addr, u64 reg_data)
706 void __iomem *pcie_index_offset;
707 void __iomem *pcie_data_offset;
709 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
710 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
711 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
713 /* write low 32 bits */
714 writel(reg_addr, pcie_index_offset);
715 readl(pcie_index_offset);
716 writel((u32)(reg_data & 0xffffffffULL), pcie_data_offset);
717 readl(pcie_data_offset);
718 /* write high 32 bits */
719 writel(reg_addr + 4, pcie_index_offset);
720 readl(pcie_index_offset);
721 writel((u32)(reg_data >> 32), pcie_data_offset);
722 readl(pcie_data_offset);
723 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
727 * amdgpu_invalid_rreg - dummy reg read function
729 * @adev: amdgpu_device pointer
730 * @reg: offset of register
732 * Dummy register read function. Used for register blocks
733 * that certain asics don't have (all asics).
734 * Returns the value in the register.
736 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
738 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
744 * amdgpu_invalid_wreg - dummy reg write function
746 * @adev: amdgpu_device pointer
747 * @reg: offset of register
748 * @v: value to write to the register
750 * Dummy register read function. Used for register blocks
751 * that certain asics don't have (all asics).
753 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
755 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
761 * amdgpu_invalid_rreg64 - dummy 64 bit reg read function
763 * @adev: amdgpu_device pointer
764 * @reg: offset of register
766 * Dummy register read function. Used for register blocks
767 * that certain asics don't have (all asics).
768 * Returns the value in the register.
770 static uint64_t amdgpu_invalid_rreg64(struct amdgpu_device *adev, uint32_t reg)
772 DRM_ERROR("Invalid callback to read 64 bit register 0x%04X\n", reg);
778 * amdgpu_invalid_wreg64 - dummy reg write function
780 * @adev: amdgpu_device pointer
781 * @reg: offset of register
782 * @v: value to write to the register
784 * Dummy register read function. Used for register blocks
785 * that certain asics don't have (all asics).
787 static void amdgpu_invalid_wreg64(struct amdgpu_device *adev, uint32_t reg, uint64_t v)
789 DRM_ERROR("Invalid callback to write 64 bit register 0x%04X with 0x%08llX\n",
795 * amdgpu_block_invalid_rreg - dummy reg read function
797 * @adev: amdgpu_device pointer
798 * @block: offset of instance
799 * @reg: offset of register
801 * Dummy register read function. Used for register blocks
802 * that certain asics don't have (all asics).
803 * Returns the value in the register.
805 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
806 uint32_t block, uint32_t reg)
808 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
815 * amdgpu_block_invalid_wreg - dummy reg write function
817 * @adev: amdgpu_device pointer
818 * @block: offset of instance
819 * @reg: offset of register
820 * @v: value to write to the register
822 * Dummy register read function. Used for register blocks
823 * that certain asics don't have (all asics).
825 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
827 uint32_t reg, uint32_t v)
829 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
835 * amdgpu_device_asic_init - Wrapper for atom asic_init
837 * @adev: amdgpu_device pointer
839 * Does any asic specific work and then calls atom asic init.
841 static int amdgpu_device_asic_init(struct amdgpu_device *adev)
843 amdgpu_asic_pre_asic_init(adev);
845 return amdgpu_atom_asic_init(adev->mode_info.atom_context);
849 * amdgpu_device_vram_scratch_init - allocate the VRAM scratch page
851 * @adev: amdgpu_device pointer
853 * Allocates a scratch page of VRAM for use by various things in the
856 static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev)
858 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
859 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
860 &adev->vram_scratch.robj,
861 &adev->vram_scratch.gpu_addr,
862 (void **)&adev->vram_scratch.ptr);
866 * amdgpu_device_vram_scratch_fini - Free the VRAM scratch page
868 * @adev: amdgpu_device pointer
870 * Frees the VRAM scratch page.
872 static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev)
874 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
878 * amdgpu_device_program_register_sequence - program an array of registers.
880 * @adev: amdgpu_device pointer
881 * @registers: pointer to the register array
882 * @array_size: size of the register array
884 * Programs an array or registers with and and or masks.
885 * This is a helper for setting golden registers.
887 void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
888 const u32 *registers,
889 const u32 array_size)
891 u32 tmp, reg, and_mask, or_mask;
897 for (i = 0; i < array_size; i +=3) {
898 reg = registers[i + 0];
899 and_mask = registers[i + 1];
900 or_mask = registers[i + 2];
902 if (and_mask == 0xffffffff) {
907 if (adev->family >= AMDGPU_FAMILY_AI)
908 tmp |= (or_mask & and_mask);
917 * amdgpu_device_pci_config_reset - reset the GPU
919 * @adev: amdgpu_device pointer
921 * Resets the GPU using the pci config reset sequence.
922 * Only applicable to asics prior to vega10.
924 void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
926 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
930 * amdgpu_device_pci_reset - reset the GPU using generic PCI means
932 * @adev: amdgpu_device pointer
934 * Resets the GPU using generic pci reset interfaces (FLR, SBR, etc.).
936 int amdgpu_device_pci_reset(struct amdgpu_device *adev)
938 return pci_reset_function(adev->pdev);
942 * GPU doorbell aperture helpers function.
945 * amdgpu_device_doorbell_init - Init doorbell driver information.
947 * @adev: amdgpu_device pointer
949 * Init doorbell driver information (CIK)
950 * Returns 0 on success, error on failure.
952 static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
955 /* No doorbell on SI hardware generation */
956 if (adev->asic_type < CHIP_BONAIRE) {
957 adev->doorbell.base = 0;
958 adev->doorbell.size = 0;
959 adev->doorbell.num_doorbells = 0;
960 adev->doorbell.ptr = NULL;
964 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
967 amdgpu_asic_init_doorbell_index(adev);
969 /* doorbell bar mapping */
970 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
971 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
973 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
974 adev->doorbell_index.max_assignment+1);
975 if (adev->doorbell.num_doorbells == 0)
978 /* For Vega, reserve and map two pages on doorbell BAR since SDMA
979 * paging queue doorbell use the second page. The
980 * AMDGPU_DOORBELL64_MAX_ASSIGNMENT definition assumes all the
981 * doorbells are in the first page. So with paging queue enabled,
982 * the max num_doorbells should + 1 page (0x400 in dword)
984 if (adev->asic_type >= CHIP_VEGA10)
985 adev->doorbell.num_doorbells += 0x400;
987 adev->doorbell.ptr = ioremap(adev->doorbell.base,
988 adev->doorbell.num_doorbells *
990 if (adev->doorbell.ptr == NULL)
997 * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
999 * @adev: amdgpu_device pointer
1001 * Tear down doorbell driver information (CIK)
1003 static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
1005 iounmap(adev->doorbell.ptr);
1006 adev->doorbell.ptr = NULL;
1012 * amdgpu_device_wb_*()
1013 * Writeback is the method by which the GPU updates special pages in memory
1014 * with the status of certain GPU events (fences, ring pointers,etc.).
1018 * amdgpu_device_wb_fini - Disable Writeback and free memory
1020 * @adev: amdgpu_device pointer
1022 * Disables Writeback and frees the Writeback memory (all asics).
1023 * Used at driver shutdown.
1025 static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
1027 if (adev->wb.wb_obj) {
1028 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
1030 (void **)&adev->wb.wb);
1031 adev->wb.wb_obj = NULL;
1036 * amdgpu_device_wb_init- Init Writeback driver info and allocate memory
1038 * @adev: amdgpu_device pointer
1040 * Initializes writeback and allocates writeback memory (all asics).
1041 * Used at driver startup.
1042 * Returns 0 on success or an -error on failure.
1044 static int amdgpu_device_wb_init(struct amdgpu_device *adev)
1048 if (adev->wb.wb_obj == NULL) {
1049 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
1050 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
1051 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
1052 &adev->wb.wb_obj, &adev->wb.gpu_addr,
1053 (void **)&adev->wb.wb);
1055 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
1059 adev->wb.num_wb = AMDGPU_MAX_WB;
1060 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
1062 /* clear wb memory */
1063 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
1070 * amdgpu_device_wb_get - Allocate a wb entry
1072 * @adev: amdgpu_device pointer
1075 * Allocate a wb slot for use by the driver (all asics).
1076 * Returns 0 on success or -EINVAL on failure.
1078 int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
1080 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
1082 if (offset < adev->wb.num_wb) {
1083 __set_bit(offset, adev->wb.used);
1084 *wb = offset << 3; /* convert to dw offset */
1092 * amdgpu_device_wb_free - Free a wb entry
1094 * @adev: amdgpu_device pointer
1097 * Free a wb slot allocated for use by the driver (all asics)
1099 void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
1102 if (wb < adev->wb.num_wb)
1103 __clear_bit(wb, adev->wb.used);
1107 * amdgpu_device_resize_fb_bar - try to resize FB BAR
1109 * @adev: amdgpu_device pointer
1111 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
1112 * to fail, but if any of the BARs is not accessible after the size we abort
1113 * driver loading by returning -ENODEV.
1115 int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
1117 int rbar_size = pci_rebar_bytes_to_size(adev->gmc.real_vram_size);
1118 struct pci_bus *root;
1119 struct resource *res;
1125 if (amdgpu_sriov_vf(adev))
1128 /* skip if the bios has already enabled large BAR */
1129 if (adev->gmc.real_vram_size &&
1130 (pci_resource_len(adev->pdev, 0) >= adev->gmc.real_vram_size))
1133 /* Check if the root BUS has 64bit memory resources */
1134 root = adev->pdev->bus;
1135 while (root->parent)
1136 root = root->parent;
1138 pci_bus_for_each_resource(root, res, i) {
1139 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
1140 res->start > 0x100000000ull)
1144 /* Trying to resize is pointless without a root hub window above 4GB */
1148 /* Limit the BAR size to what is available */
1149 rbar_size = min(fls(pci_rebar_get_possible_sizes(adev->pdev, 0)) - 1,
1152 /* Disable memory decoding while we change the BAR addresses and size */
1153 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
1154 pci_write_config_word(adev->pdev, PCI_COMMAND,
1155 cmd & ~PCI_COMMAND_MEMORY);
1157 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
1158 amdgpu_device_doorbell_fini(adev);
1159 if (adev->asic_type >= CHIP_BONAIRE)
1160 pci_release_resource(adev->pdev, 2);
1162 pci_release_resource(adev->pdev, 0);
1164 r = pci_resize_resource(adev->pdev, 0, rbar_size);
1166 DRM_INFO("Not enough PCI address space for a large BAR.");
1167 else if (r && r != -ENOTSUPP)
1168 DRM_ERROR("Problem resizing BAR0 (%d).", r);
1170 pci_assign_unassigned_bus_resources(adev->pdev->bus);
1172 /* When the doorbell or fb BAR isn't available we have no chance of
1175 r = amdgpu_device_doorbell_init(adev);
1176 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
1179 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
1185 * GPU helpers function.
1188 * amdgpu_device_need_post - check if the hw need post or not
1190 * @adev: amdgpu_device pointer
1192 * Check if the asic has been initialized (all asics) at driver startup
1193 * or post is needed if hw reset is performed.
1194 * Returns true if need or false if not.
1196 bool amdgpu_device_need_post(struct amdgpu_device *adev)
1200 if (amdgpu_sriov_vf(adev))
1203 if (amdgpu_passthrough(adev)) {
1204 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
1205 * some old smc fw still need driver do vPost otherwise gpu hang, while
1206 * those smc fw version above 22.15 doesn't have this flaw, so we force
1207 * vpost executed for smc version below 22.15
1209 if (adev->asic_type == CHIP_FIJI) {
1212 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
1213 /* force vPost if error occured */
1217 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
1218 if (fw_ver < 0x00160e00)
1223 /* Don't post if we need to reset whole hive on init */
1224 if (adev->gmc.xgmi.pending_reset)
1227 if (adev->has_hw_reset) {
1228 adev->has_hw_reset = false;
1232 /* bios scratch used on CIK+ */
1233 if (adev->asic_type >= CHIP_BONAIRE)
1234 return amdgpu_atombios_scratch_need_asic_init(adev);
1236 /* check MEM_SIZE for older asics */
1237 reg = amdgpu_asic_get_config_memsize(adev);
1239 if ((reg != 0) && (reg != 0xffffffff))
1245 /* if we get transitioned to only one device, take VGA back */
1247 * amdgpu_device_vga_set_decode - enable/disable vga decode
1249 * @cookie: amdgpu_device pointer
1250 * @state: enable/disable vga decode
1252 * Enable/disable vga decode (all asics).
1253 * Returns VGA resource flags.
1255 static unsigned int amdgpu_device_vga_set_decode(void *cookie, bool state)
1257 struct amdgpu_device *adev = cookie;
1258 amdgpu_asic_set_vga_state(adev, state);
1260 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1261 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1263 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1267 * amdgpu_device_check_block_size - validate the vm block size
1269 * @adev: amdgpu_device pointer
1271 * Validates the vm block size specified via module parameter.
1272 * The vm block size defines number of bits in page table versus page directory,
1273 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1274 * page table and the remaining bits are in the page directory.
1276 static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
1278 /* defines number of bits in page table versus page directory,
1279 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1280 * page table and the remaining bits are in the page directory */
1281 if (amdgpu_vm_block_size == -1)
1284 if (amdgpu_vm_block_size < 9) {
1285 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1286 amdgpu_vm_block_size);
1287 amdgpu_vm_block_size = -1;
1292 * amdgpu_device_check_vm_size - validate the vm size
1294 * @adev: amdgpu_device pointer
1296 * Validates the vm size in GB specified via module parameter.
1297 * The VM size is the size of the GPU virtual memory space in GB.
1299 static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
1301 /* no need to check the default value */
1302 if (amdgpu_vm_size == -1)
1305 if (amdgpu_vm_size < 1) {
1306 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1308 amdgpu_vm_size = -1;
1312 static void amdgpu_device_check_smu_prv_buffer_size(struct amdgpu_device *adev)
1315 bool is_os_64 = (sizeof(void *) == 8);
1316 uint64_t total_memory;
1317 uint64_t dram_size_seven_GB = 0x1B8000000;
1318 uint64_t dram_size_three_GB = 0xB8000000;
1320 if (amdgpu_smu_memory_pool_size == 0)
1324 DRM_WARN("Not 64-bit OS, feature not supported\n");
1328 total_memory = (uint64_t)si.totalram * si.mem_unit;
1330 if ((amdgpu_smu_memory_pool_size == 1) ||
1331 (amdgpu_smu_memory_pool_size == 2)) {
1332 if (total_memory < dram_size_three_GB)
1334 } else if ((amdgpu_smu_memory_pool_size == 4) ||
1335 (amdgpu_smu_memory_pool_size == 8)) {
1336 if (total_memory < dram_size_seven_GB)
1339 DRM_WARN("Smu memory pool size not supported\n");
1342 adev->pm.smu_prv_buffer_size = amdgpu_smu_memory_pool_size << 28;
1347 DRM_WARN("No enough system memory\n");
1349 adev->pm.smu_prv_buffer_size = 0;
1353 * amdgpu_device_check_arguments - validate module params
1355 * @adev: amdgpu_device pointer
1357 * Validates certain module parameters and updates
1358 * the associated values used by the driver (all asics).
1360 static int amdgpu_device_check_arguments(struct amdgpu_device *adev)
1362 if (amdgpu_sched_jobs < 4) {
1363 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1365 amdgpu_sched_jobs = 4;
1366 } else if (!is_power_of_2(amdgpu_sched_jobs)){
1367 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1369 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1372 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
1373 /* gart size must be greater or equal to 32M */
1374 dev_warn(adev->dev, "gart size (%d) too small\n",
1376 amdgpu_gart_size = -1;
1379 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
1380 /* gtt size must be greater or equal to 32M */
1381 dev_warn(adev->dev, "gtt size (%d) too small\n",
1383 amdgpu_gtt_size = -1;
1386 /* valid range is between 4 and 9 inclusive */
1387 if (amdgpu_vm_fragment_size != -1 &&
1388 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1389 dev_warn(adev->dev, "valid range is between 4 and 9\n");
1390 amdgpu_vm_fragment_size = -1;
1393 if (amdgpu_sched_hw_submission < 2) {
1394 dev_warn(adev->dev, "sched hw submission jobs (%d) must be at least 2\n",
1395 amdgpu_sched_hw_submission);
1396 amdgpu_sched_hw_submission = 2;
1397 } else if (!is_power_of_2(amdgpu_sched_hw_submission)) {
1398 dev_warn(adev->dev, "sched hw submission jobs (%d) must be a power of 2\n",
1399 amdgpu_sched_hw_submission);
1400 amdgpu_sched_hw_submission = roundup_pow_of_two(amdgpu_sched_hw_submission);
1403 amdgpu_device_check_smu_prv_buffer_size(adev);
1405 amdgpu_device_check_vm_size(adev);
1407 amdgpu_device_check_block_size(adev);
1409 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
1411 amdgpu_gmc_tmz_set(adev);
1413 amdgpu_gmc_noretry_set(adev);
1419 * amdgpu_switcheroo_set_state - set switcheroo state
1421 * @pdev: pci dev pointer
1422 * @state: vga_switcheroo state
1424 * Callback for the switcheroo driver. Suspends or resumes the
1425 * the asics before or after it is powered up using ACPI methods.
1427 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev,
1428 enum vga_switcheroo_state state)
1430 struct drm_device *dev = pci_get_drvdata(pdev);
1433 if (amdgpu_device_supports_px(dev) && state == VGA_SWITCHEROO_OFF)
1436 if (state == VGA_SWITCHEROO_ON) {
1437 pr_info("switched on\n");
1438 /* don't suspend or resume card normally */
1439 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1441 pci_set_power_state(pdev, PCI_D0);
1442 amdgpu_device_load_pci_state(pdev);
1443 r = pci_enable_device(pdev);
1445 DRM_WARN("pci_enable_device failed (%d)\n", r);
1446 amdgpu_device_resume(dev, true);
1448 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1450 pr_info("switched off\n");
1451 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1452 amdgpu_device_suspend(dev, true);
1453 amdgpu_device_cache_pci_state(pdev);
1454 /* Shut down the device */
1455 pci_disable_device(pdev);
1456 pci_set_power_state(pdev, PCI_D3cold);
1457 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1462 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1464 * @pdev: pci dev pointer
1466 * Callback for the switcheroo driver. Check of the switcheroo
1467 * state can be changed.
1468 * Returns true if the state can be changed, false if not.
1470 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1472 struct drm_device *dev = pci_get_drvdata(pdev);
1475 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1476 * locking inversion with the driver load path. And the access here is
1477 * completely racy anyway. So don't bother with locking for now.
1479 return atomic_read(&dev->open_count) == 0;
1482 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1483 .set_gpu_state = amdgpu_switcheroo_set_state,
1485 .can_switch = amdgpu_switcheroo_can_switch,
1489 * amdgpu_device_ip_set_clockgating_state - set the CG state
1491 * @dev: amdgpu_device pointer
1492 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1493 * @state: clockgating state (gate or ungate)
1495 * Sets the requested clockgating state for all instances of
1496 * the hardware IP specified.
1497 * Returns the error code from the last instance.
1499 int amdgpu_device_ip_set_clockgating_state(void *dev,
1500 enum amd_ip_block_type block_type,
1501 enum amd_clockgating_state state)
1503 struct amdgpu_device *adev = dev;
1506 for (i = 0; i < adev->num_ip_blocks; i++) {
1507 if (!adev->ip_blocks[i].status.valid)
1509 if (adev->ip_blocks[i].version->type != block_type)
1511 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1513 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1514 (void *)adev, state);
1516 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1517 adev->ip_blocks[i].version->funcs->name, r);
1523 * amdgpu_device_ip_set_powergating_state - set the PG state
1525 * @dev: amdgpu_device pointer
1526 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1527 * @state: powergating state (gate or ungate)
1529 * Sets the requested powergating state for all instances of
1530 * the hardware IP specified.
1531 * Returns the error code from the last instance.
1533 int amdgpu_device_ip_set_powergating_state(void *dev,
1534 enum amd_ip_block_type block_type,
1535 enum amd_powergating_state state)
1537 struct amdgpu_device *adev = dev;
1540 for (i = 0; i < adev->num_ip_blocks; i++) {
1541 if (!adev->ip_blocks[i].status.valid)
1543 if (adev->ip_blocks[i].version->type != block_type)
1545 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1547 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1548 (void *)adev, state);
1550 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1551 adev->ip_blocks[i].version->funcs->name, r);
1557 * amdgpu_device_ip_get_clockgating_state - get the CG state
1559 * @adev: amdgpu_device pointer
1560 * @flags: clockgating feature flags
1562 * Walks the list of IPs on the device and updates the clockgating
1563 * flags for each IP.
1564 * Updates @flags with the feature flags for each hardware IP where
1565 * clockgating is enabled.
1567 void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
1572 for (i = 0; i < adev->num_ip_blocks; i++) {
1573 if (!adev->ip_blocks[i].status.valid)
1575 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1576 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1581 * amdgpu_device_ip_wait_for_idle - wait for idle
1583 * @adev: amdgpu_device pointer
1584 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1586 * Waits for the request hardware IP to be idle.
1587 * Returns 0 for success or a negative error code on failure.
1589 int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1590 enum amd_ip_block_type block_type)
1594 for (i = 0; i < adev->num_ip_blocks; i++) {
1595 if (!adev->ip_blocks[i].status.valid)
1597 if (adev->ip_blocks[i].version->type == block_type) {
1598 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
1609 * amdgpu_device_ip_is_idle - is the hardware IP idle
1611 * @adev: amdgpu_device pointer
1612 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1614 * Check if the hardware IP is idle or not.
1615 * Returns true if it the IP is idle, false if not.
1617 bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1618 enum amd_ip_block_type block_type)
1622 for (i = 0; i < adev->num_ip_blocks; i++) {
1623 if (!adev->ip_blocks[i].status.valid)
1625 if (adev->ip_blocks[i].version->type == block_type)
1626 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
1633 * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1635 * @adev: amdgpu_device pointer
1636 * @type: Type of hardware IP (SMU, GFX, UVD, etc.)
1638 * Returns a pointer to the hardware IP block structure
1639 * if it exists for the asic, otherwise NULL.
1641 struct amdgpu_ip_block *
1642 amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1643 enum amd_ip_block_type type)
1647 for (i = 0; i < adev->num_ip_blocks; i++)
1648 if (adev->ip_blocks[i].version->type == type)
1649 return &adev->ip_blocks[i];
1655 * amdgpu_device_ip_block_version_cmp
1657 * @adev: amdgpu_device pointer
1658 * @type: enum amd_ip_block_type
1659 * @major: major version
1660 * @minor: minor version
1662 * return 0 if equal or greater
1663 * return 1 if smaller or the ip_block doesn't exist
1665 int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1666 enum amd_ip_block_type type,
1667 u32 major, u32 minor)
1669 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
1671 if (ip_block && ((ip_block->version->major > major) ||
1672 ((ip_block->version->major == major) &&
1673 (ip_block->version->minor >= minor))))
1680 * amdgpu_device_ip_block_add
1682 * @adev: amdgpu_device pointer
1683 * @ip_block_version: pointer to the IP to add
1685 * Adds the IP block driver information to the collection of IPs
1688 int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1689 const struct amdgpu_ip_block_version *ip_block_version)
1691 if (!ip_block_version)
1694 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
1695 ip_block_version->funcs->name);
1697 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1703 * amdgpu_device_enable_virtual_display - enable virtual display feature
1705 * @adev: amdgpu_device pointer
1707 * Enabled the virtual display feature if the user has enabled it via
1708 * the module parameter virtual_display. This feature provides a virtual
1709 * display hardware on headless boards or in virtualized environments.
1710 * This function parses and validates the configuration string specified by
1711 * the user and configues the virtual display configuration (number of
1712 * virtual connectors, crtcs, etc.) specified.
1714 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
1716 adev->enable_virtual_display = false;
1718 if (amdgpu_virtual_display) {
1719 const char *pci_address_name = pci_name(adev->pdev);
1720 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
1722 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1723 pciaddstr_tmp = pciaddstr;
1724 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1725 pciaddname = strsep(&pciaddname_tmp, ",");
1726 if (!strcmp("all", pciaddname)
1727 || !strcmp(pci_address_name, pciaddname)) {
1731 adev->enable_virtual_display = true;
1734 res = kstrtol(pciaddname_tmp, 10,
1742 adev->mode_info.num_crtc = num_crtc;
1744 adev->mode_info.num_crtc = 1;
1750 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1751 amdgpu_virtual_display, pci_address_name,
1752 adev->enable_virtual_display, adev->mode_info.num_crtc);
1759 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
1761 * @adev: amdgpu_device pointer
1763 * Parses the asic configuration parameters specified in the gpu info
1764 * firmware and makes them availale to the driver for use in configuring
1766 * Returns 0 on success, -EINVAL on failure.
1768 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1770 const char *chip_name;
1773 const struct gpu_info_firmware_header_v1_0 *hdr;
1775 adev->firmware.gpu_info_fw = NULL;
1777 if (adev->mman.discovery_bin) {
1778 amdgpu_discovery_get_gfx_info(adev);
1781 * FIXME: The bounding box is still needed by Navi12, so
1782 * temporarily read it from gpu_info firmware. Should be droped
1783 * when DAL no longer needs it.
1785 if (adev->asic_type != CHIP_NAVI12)
1789 switch (adev->asic_type) {
1790 #ifdef CONFIG_DRM_AMDGPU_SI
1797 #ifdef CONFIG_DRM_AMDGPU_CIK
1807 case CHIP_POLARIS10:
1808 case CHIP_POLARIS11:
1809 case CHIP_POLARIS12:
1814 case CHIP_ALDEBARAN:
1815 case CHIP_SIENNA_CICHLID:
1816 case CHIP_NAVY_FLOUNDER:
1817 case CHIP_DIMGREY_CAVEFISH:
1821 chip_name = "vega10";
1824 chip_name = "vega12";
1827 if (adev->apu_flags & AMD_APU_IS_RAVEN2)
1828 chip_name = "raven2";
1829 else if (adev->apu_flags & AMD_APU_IS_PICASSO)
1830 chip_name = "picasso";
1832 chip_name = "raven";
1835 chip_name = "arcturus";
1838 if (adev->apu_flags & AMD_APU_IS_RENOIR)
1839 chip_name = "renoir";
1841 chip_name = "green_sardine";
1844 chip_name = "navi10";
1847 chip_name = "navi14";
1850 chip_name = "navi12";
1853 chip_name = "vangogh";
1857 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
1858 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
1861 "Failed to load gpu_info firmware \"%s\"\n",
1865 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
1868 "Failed to validate gpu_info firmware \"%s\"\n",
1873 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
1874 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1876 switch (hdr->version_major) {
1879 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
1880 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
1881 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1884 * Should be droped when DAL no longer needs it.
1886 if (adev->asic_type == CHIP_NAVI12)
1887 goto parse_soc_bounding_box;
1889 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1890 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1891 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1892 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
1893 adev->gfx.config.max_texture_channel_caches =
1894 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1895 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1896 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1897 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1898 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
1899 adev->gfx.config.double_offchip_lds_buf =
1900 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1901 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
1902 adev->gfx.cu_info.max_waves_per_simd =
1903 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1904 adev->gfx.cu_info.max_scratch_slots_per_cu =
1905 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1906 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
1907 if (hdr->version_minor >= 1) {
1908 const struct gpu_info_firmware_v1_1 *gpu_info_fw =
1909 (const struct gpu_info_firmware_v1_1 *)(adev->firmware.gpu_info_fw->data +
1910 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1911 adev->gfx.config.num_sc_per_sh =
1912 le32_to_cpu(gpu_info_fw->num_sc_per_sh);
1913 adev->gfx.config.num_packer_per_sc =
1914 le32_to_cpu(gpu_info_fw->num_packer_per_sc);
1917 parse_soc_bounding_box:
1919 * soc bounding box info is not integrated in disocovery table,
1920 * we always need to parse it from gpu info firmware if needed.
1922 if (hdr->version_minor == 2) {
1923 const struct gpu_info_firmware_v1_2 *gpu_info_fw =
1924 (const struct gpu_info_firmware_v1_2 *)(adev->firmware.gpu_info_fw->data +
1925 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1926 adev->dm.soc_bounding_box = &gpu_info_fw->soc_bounding_box;
1932 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1941 * amdgpu_device_ip_early_init - run early init for hardware IPs
1943 * @adev: amdgpu_device pointer
1945 * Early initialization pass for hardware IPs. The hardware IPs that make
1946 * up each asic are discovered each IP's early_init callback is run. This
1947 * is the first stage in initializing the asic.
1948 * Returns 0 on success, negative error code on failure.
1950 static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
1954 amdgpu_device_enable_virtual_display(adev);
1956 if (amdgpu_sriov_vf(adev)) {
1957 r = amdgpu_virt_request_full_gpu(adev, true);
1962 switch (adev->asic_type) {
1963 #ifdef CONFIG_DRM_AMDGPU_SI
1969 adev->family = AMDGPU_FAMILY_SI;
1970 r = si_set_ip_blocks(adev);
1975 #ifdef CONFIG_DRM_AMDGPU_CIK
1981 if (adev->flags & AMD_IS_APU)
1982 adev->family = AMDGPU_FAMILY_KV;
1984 adev->family = AMDGPU_FAMILY_CI;
1986 r = cik_set_ip_blocks(adev);
1994 case CHIP_POLARIS10:
1995 case CHIP_POLARIS11:
1996 case CHIP_POLARIS12:
2000 if (adev->flags & AMD_IS_APU)
2001 adev->family = AMDGPU_FAMILY_CZ;
2003 adev->family = AMDGPU_FAMILY_VI;
2005 r = vi_set_ip_blocks(adev);
2015 case CHIP_ALDEBARAN:
2016 if (adev->flags & AMD_IS_APU)
2017 adev->family = AMDGPU_FAMILY_RV;
2019 adev->family = AMDGPU_FAMILY_AI;
2021 r = soc15_set_ip_blocks(adev);
2028 case CHIP_SIENNA_CICHLID:
2029 case CHIP_NAVY_FLOUNDER:
2030 case CHIP_DIMGREY_CAVEFISH:
2032 if (adev->asic_type == CHIP_VANGOGH)
2033 adev->family = AMDGPU_FAMILY_VGH;
2035 adev->family = AMDGPU_FAMILY_NV;
2037 r = nv_set_ip_blocks(adev);
2042 /* FIXME: not supported yet */
2046 amdgpu_amdkfd_device_probe(adev);
2048 adev->pm.pp_feature = amdgpu_pp_feature_mask;
2049 if (amdgpu_sriov_vf(adev) || sched_policy == KFD_SCHED_POLICY_NO_HWS)
2050 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
2051 if (amdgpu_sriov_vf(adev) && adev->asic_type == CHIP_SIENNA_CICHLID)
2052 adev->pm.pp_feature &= ~PP_OVERDRIVE_MASK;
2054 for (i = 0; i < adev->num_ip_blocks; i++) {
2055 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
2056 DRM_ERROR("disabled ip block: %d <%s>\n",
2057 i, adev->ip_blocks[i].version->funcs->name);
2058 adev->ip_blocks[i].status.valid = false;
2060 if (adev->ip_blocks[i].version->funcs->early_init) {
2061 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
2063 adev->ip_blocks[i].status.valid = false;
2065 DRM_ERROR("early_init of IP block <%s> failed %d\n",
2066 adev->ip_blocks[i].version->funcs->name, r);
2069 adev->ip_blocks[i].status.valid = true;
2072 adev->ip_blocks[i].status.valid = true;
2075 /* get the vbios after the asic_funcs are set up */
2076 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) {
2077 r = amdgpu_device_parse_gpu_info_fw(adev);
2082 if (!amdgpu_get_bios(adev))
2085 r = amdgpu_atombios_init(adev);
2087 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
2088 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
2092 /*get pf2vf msg info at it's earliest time*/
2093 if (amdgpu_sriov_vf(adev))
2094 amdgpu_virt_init_data_exchange(adev);
2099 adev->cg_flags &= amdgpu_cg_mask;
2100 adev->pg_flags &= amdgpu_pg_mask;
2105 static int amdgpu_device_ip_hw_init_phase1(struct amdgpu_device *adev)
2109 for (i = 0; i < adev->num_ip_blocks; i++) {
2110 if (!adev->ip_blocks[i].status.sw)
2112 if (adev->ip_blocks[i].status.hw)
2114 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2115 (amdgpu_sriov_vf(adev) && (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)) ||
2116 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
2117 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2119 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2120 adev->ip_blocks[i].version->funcs->name, r);
2123 adev->ip_blocks[i].status.hw = true;
2130 static int amdgpu_device_ip_hw_init_phase2(struct amdgpu_device *adev)
2134 for (i = 0; i < adev->num_ip_blocks; i++) {
2135 if (!adev->ip_blocks[i].status.sw)
2137 if (adev->ip_blocks[i].status.hw)
2139 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2141 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2142 adev->ip_blocks[i].version->funcs->name, r);
2145 adev->ip_blocks[i].status.hw = true;
2151 static int amdgpu_device_fw_loading(struct amdgpu_device *adev)
2155 uint32_t smu_version;
2157 if (adev->asic_type >= CHIP_VEGA10) {
2158 for (i = 0; i < adev->num_ip_blocks; i++) {
2159 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_PSP)
2162 if (!adev->ip_blocks[i].status.sw)
2165 /* no need to do the fw loading again if already done*/
2166 if (adev->ip_blocks[i].status.hw == true)
2169 if (amdgpu_in_reset(adev) || adev->in_suspend) {
2170 r = adev->ip_blocks[i].version->funcs->resume(adev);
2172 DRM_ERROR("resume of IP block <%s> failed %d\n",
2173 adev->ip_blocks[i].version->funcs->name, r);
2177 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2179 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2180 adev->ip_blocks[i].version->funcs->name, r);
2185 adev->ip_blocks[i].status.hw = true;
2190 if (!amdgpu_sriov_vf(adev) || adev->asic_type == CHIP_TONGA)
2191 r = amdgpu_pm_load_smu_firmware(adev, &smu_version);
2197 * amdgpu_device_ip_init - run init for hardware IPs
2199 * @adev: amdgpu_device pointer
2201 * Main initialization pass for hardware IPs. The list of all the hardware
2202 * IPs that make up the asic is walked and the sw_init and hw_init callbacks
2203 * are run. sw_init initializes the software state associated with each IP
2204 * and hw_init initializes the hardware associated with each IP.
2205 * Returns 0 on success, negative error code on failure.
2207 static int amdgpu_device_ip_init(struct amdgpu_device *adev)
2211 r = amdgpu_ras_init(adev);
2215 for (i = 0; i < adev->num_ip_blocks; i++) {
2216 if (!adev->ip_blocks[i].status.valid)
2218 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
2220 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
2221 adev->ip_blocks[i].version->funcs->name, r);
2224 adev->ip_blocks[i].status.sw = true;
2226 /* need to do gmc hw init early so we can allocate gpu mem */
2227 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
2228 r = amdgpu_device_vram_scratch_init(adev);
2230 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
2233 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
2235 DRM_ERROR("hw_init %d failed %d\n", i, r);
2238 r = amdgpu_device_wb_init(adev);
2240 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
2243 adev->ip_blocks[i].status.hw = true;
2245 /* right after GMC hw init, we create CSA */
2246 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) {
2247 r = amdgpu_allocate_static_csa(adev, &adev->virt.csa_obj,
2248 AMDGPU_GEM_DOMAIN_VRAM,
2251 DRM_ERROR("allocate CSA failed %d\n", r);
2258 if (amdgpu_sriov_vf(adev))
2259 amdgpu_virt_init_data_exchange(adev);
2261 r = amdgpu_ib_pool_init(adev);
2263 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
2264 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
2268 r = amdgpu_ucode_create_bo(adev); /* create ucode bo when sw_init complete*/
2272 r = amdgpu_device_ip_hw_init_phase1(adev);
2276 r = amdgpu_device_fw_loading(adev);
2280 r = amdgpu_device_ip_hw_init_phase2(adev);
2285 * retired pages will be loaded from eeprom and reserved here,
2286 * it should be called after amdgpu_device_ip_hw_init_phase2 since
2287 * for some ASICs the RAS EEPROM code relies on SMU fully functioning
2288 * for I2C communication which only true at this point.
2290 * amdgpu_ras_recovery_init may fail, but the upper only cares the
2291 * failure from bad gpu situation and stop amdgpu init process
2292 * accordingly. For other failed cases, it will still release all
2293 * the resource and print error message, rather than returning one
2294 * negative value to upper level.
2296 * Note: theoretically, this should be called before all vram allocations
2297 * to protect retired page from abusing
2299 r = amdgpu_ras_recovery_init(adev);
2303 if (adev->gmc.xgmi.num_physical_nodes > 1)
2304 amdgpu_xgmi_add_device(adev);
2306 /* Don't init kfd if whole hive need to be reset during init */
2307 if (!adev->gmc.xgmi.pending_reset)
2308 amdgpu_amdkfd_device_init(adev);
2310 amdgpu_fru_get_product_info(adev);
2313 if (amdgpu_sriov_vf(adev))
2314 amdgpu_virt_release_full_gpu(adev, true);
2320 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
2322 * @adev: amdgpu_device pointer
2324 * Writes a reset magic value to the gart pointer in VRAM. The driver calls
2325 * this function before a GPU reset. If the value is retained after a
2326 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
2328 static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
2330 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
2334 * amdgpu_device_check_vram_lost - check if vram is valid
2336 * @adev: amdgpu_device pointer
2338 * Checks the reset magic value written to the gart pointer in VRAM.
2339 * The driver calls this after a GPU reset to see if the contents of
2340 * VRAM is lost or now.
2341 * returns true if vram is lost, false if not.
2343 static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
2345 if (memcmp(adev->gart.ptr, adev->reset_magic,
2346 AMDGPU_RESET_MAGIC_NUM))
2349 if (!amdgpu_in_reset(adev))
2353 * For all ASICs with baco/mode1 reset, the VRAM is
2354 * always assumed to be lost.
2356 switch (amdgpu_asic_reset_method(adev)) {
2357 case AMD_RESET_METHOD_BACO:
2358 case AMD_RESET_METHOD_MODE1:
2366 * amdgpu_device_set_cg_state - set clockgating for amdgpu device
2368 * @adev: amdgpu_device pointer
2369 * @state: clockgating state (gate or ungate)
2371 * The list of all the hardware IPs that make up the asic is walked and the
2372 * set_clockgating_state callbacks are run.
2373 * Late initialization pass enabling clockgating for hardware IPs.
2374 * Fini or suspend, pass disabling clockgating for hardware IPs.
2375 * Returns 0 on success, negative error code on failure.
2378 int amdgpu_device_set_cg_state(struct amdgpu_device *adev,
2379 enum amd_clockgating_state state)
2383 if (amdgpu_emu_mode == 1)
2386 for (j = 0; j < adev->num_ip_blocks; j++) {
2387 i = state == AMD_CG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
2388 if (!adev->ip_blocks[i].status.late_initialized)
2390 /* skip CG for GFX on S0ix */
2391 if (adev->in_s0ix &&
2392 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX)
2394 /* skip CG for VCE/UVD, it's handled specially */
2395 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
2396 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
2397 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
2398 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
2399 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
2400 /* enable clockgating to save power */
2401 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
2404 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
2405 adev->ip_blocks[i].version->funcs->name, r);
2414 int amdgpu_device_set_pg_state(struct amdgpu_device *adev,
2415 enum amd_powergating_state state)
2419 if (amdgpu_emu_mode == 1)
2422 for (j = 0; j < adev->num_ip_blocks; j++) {
2423 i = state == AMD_PG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
2424 if (!adev->ip_blocks[i].status.late_initialized)
2426 /* skip PG for GFX on S0ix */
2427 if (adev->in_s0ix &&
2428 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX)
2430 /* skip CG for VCE/UVD, it's handled specially */
2431 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
2432 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
2433 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
2434 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
2435 adev->ip_blocks[i].version->funcs->set_powergating_state) {
2436 /* enable powergating to save power */
2437 r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
2440 DRM_ERROR("set_powergating_state(gate) of IP block <%s> failed %d\n",
2441 adev->ip_blocks[i].version->funcs->name, r);
2449 static int amdgpu_device_enable_mgpu_fan_boost(void)
2451 struct amdgpu_gpu_instance *gpu_ins;
2452 struct amdgpu_device *adev;
2455 mutex_lock(&mgpu_info.mutex);
2458 * MGPU fan boost feature should be enabled
2459 * only when there are two or more dGPUs in
2462 if (mgpu_info.num_dgpu < 2)
2465 for (i = 0; i < mgpu_info.num_dgpu; i++) {
2466 gpu_ins = &(mgpu_info.gpu_ins[i]);
2467 adev = gpu_ins->adev;
2468 if (!(adev->flags & AMD_IS_APU) &&
2469 !gpu_ins->mgpu_fan_enabled) {
2470 ret = amdgpu_dpm_enable_mgpu_fan_boost(adev);
2474 gpu_ins->mgpu_fan_enabled = 1;
2479 mutex_unlock(&mgpu_info.mutex);
2485 * amdgpu_device_ip_late_init - run late init for hardware IPs
2487 * @adev: amdgpu_device pointer
2489 * Late initialization pass for hardware IPs. The list of all the hardware
2490 * IPs that make up the asic is walked and the late_init callbacks are run.
2491 * late_init covers any special initialization that an IP requires
2492 * after all of the have been initialized or something that needs to happen
2493 * late in the init process.
2494 * Returns 0 on success, negative error code on failure.
2496 static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
2498 struct amdgpu_gpu_instance *gpu_instance;
2501 for (i = 0; i < adev->num_ip_blocks; i++) {
2502 if (!adev->ip_blocks[i].status.hw)
2504 if (adev->ip_blocks[i].version->funcs->late_init) {
2505 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
2507 DRM_ERROR("late_init of IP block <%s> failed %d\n",
2508 adev->ip_blocks[i].version->funcs->name, r);
2512 adev->ip_blocks[i].status.late_initialized = true;
2515 amdgpu_ras_set_error_query_ready(adev, true);
2517 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE);
2518 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_GATE);
2520 amdgpu_device_fill_reset_magic(adev);
2522 r = amdgpu_device_enable_mgpu_fan_boost();
2524 DRM_ERROR("enable mgpu fan boost failed (%d).\n", r);
2526 /* For XGMI + passthrough configuration on arcturus, enable light SBR */
2527 if (adev->asic_type == CHIP_ARCTURUS &&
2528 amdgpu_passthrough(adev) &&
2529 adev->gmc.xgmi.num_physical_nodes > 1)
2530 smu_set_light_sbr(&adev->smu, true);
2532 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2533 mutex_lock(&mgpu_info.mutex);
2536 * Reset device p-state to low as this was booted with high.
2538 * This should be performed only after all devices from the same
2539 * hive get initialized.
2541 * However, it's unknown how many device in the hive in advance.
2542 * As this is counted one by one during devices initializations.
2544 * So, we wait for all XGMI interlinked devices initialized.
2545 * This may bring some delays as those devices may come from
2546 * different hives. But that should be OK.
2548 if (mgpu_info.num_dgpu == adev->gmc.xgmi.num_physical_nodes) {
2549 for (i = 0; i < mgpu_info.num_gpu; i++) {
2550 gpu_instance = &(mgpu_info.gpu_ins[i]);
2551 if (gpu_instance->adev->flags & AMD_IS_APU)
2554 r = amdgpu_xgmi_set_pstate(gpu_instance->adev,
2555 AMDGPU_XGMI_PSTATE_MIN);
2557 DRM_ERROR("pstate setting failed (%d).\n", r);
2563 mutex_unlock(&mgpu_info.mutex);
2569 static int amdgpu_device_ip_fini_early(struct amdgpu_device *adev)
2573 for (i = 0; i < adev->num_ip_blocks; i++) {
2574 if (!adev->ip_blocks[i].version->funcs->early_fini)
2577 r = adev->ip_blocks[i].version->funcs->early_fini((void *)adev);
2579 DRM_DEBUG("early_fini of IP block <%s> failed %d\n",
2580 adev->ip_blocks[i].version->funcs->name, r);
2584 amdgpu_amdkfd_suspend(adev, false);
2586 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2587 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2589 /* need to disable SMC first */
2590 for (i = 0; i < adev->num_ip_blocks; i++) {
2591 if (!adev->ip_blocks[i].status.hw)
2593 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
2594 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2595 /* XXX handle errors */
2597 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2598 adev->ip_blocks[i].version->funcs->name, r);
2600 adev->ip_blocks[i].status.hw = false;
2605 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2606 if (!adev->ip_blocks[i].status.hw)
2609 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2610 /* XXX handle errors */
2612 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2613 adev->ip_blocks[i].version->funcs->name, r);
2616 adev->ip_blocks[i].status.hw = false;
2623 * amdgpu_device_ip_fini - run fini for hardware IPs
2625 * @adev: amdgpu_device pointer
2627 * Main teardown pass for hardware IPs. The list of all the hardware
2628 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
2629 * are run. hw_fini tears down the hardware associated with each IP
2630 * and sw_fini tears down any software state associated with each IP.
2631 * Returns 0 on success, negative error code on failure.
2633 static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
2637 if (amdgpu_sriov_vf(adev) && adev->virt.ras_init_done)
2638 amdgpu_virt_release_ras_err_handler_data(adev);
2640 amdgpu_ras_pre_fini(adev);
2642 if (adev->gmc.xgmi.num_physical_nodes > 1)
2643 amdgpu_xgmi_remove_device(adev);
2645 amdgpu_amdkfd_device_fini_sw(adev);
2647 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2648 if (!adev->ip_blocks[i].status.sw)
2651 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
2652 amdgpu_ucode_free_bo(adev);
2653 amdgpu_free_static_csa(&adev->virt.csa_obj);
2654 amdgpu_device_wb_fini(adev);
2655 amdgpu_device_vram_scratch_fini(adev);
2656 amdgpu_ib_pool_fini(adev);
2659 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
2660 /* XXX handle errors */
2662 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
2663 adev->ip_blocks[i].version->funcs->name, r);
2665 adev->ip_blocks[i].status.sw = false;
2666 adev->ip_blocks[i].status.valid = false;
2669 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2670 if (!adev->ip_blocks[i].status.late_initialized)
2672 if (adev->ip_blocks[i].version->funcs->late_fini)
2673 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
2674 adev->ip_blocks[i].status.late_initialized = false;
2677 amdgpu_ras_fini(adev);
2679 if (amdgpu_sriov_vf(adev))
2680 if (amdgpu_virt_release_full_gpu(adev, false))
2681 DRM_ERROR("failed to release exclusive mode on fini\n");
2687 * amdgpu_device_delayed_init_work_handler - work handler for IB tests
2689 * @work: work_struct.
2691 static void amdgpu_device_delayed_init_work_handler(struct work_struct *work)
2693 struct amdgpu_device *adev =
2694 container_of(work, struct amdgpu_device, delayed_init_work.work);
2697 r = amdgpu_ib_ring_tests(adev);
2699 DRM_ERROR("ib ring test failed (%d).\n", r);
2702 static void amdgpu_device_delay_enable_gfx_off(struct work_struct *work)
2704 struct amdgpu_device *adev =
2705 container_of(work, struct amdgpu_device, gfx.gfx_off_delay_work.work);
2707 mutex_lock(&adev->gfx.gfx_off_mutex);
2708 if (!adev->gfx.gfx_off_state && !adev->gfx.gfx_off_req_count) {
2709 if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true))
2710 adev->gfx.gfx_off_state = true;
2712 mutex_unlock(&adev->gfx.gfx_off_mutex);
2716 * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1)
2718 * @adev: amdgpu_device pointer
2720 * Main suspend function for hardware IPs. The list of all the hardware
2721 * IPs that make up the asic is walked, clockgating is disabled and the
2722 * suspend callbacks are run. suspend puts the hardware and software state
2723 * in each IP into a state suitable for suspend.
2724 * Returns 0 on success, negative error code on failure.
2726 static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev)
2730 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2731 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2733 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2734 if (!adev->ip_blocks[i].status.valid)
2737 /* displays are handled separately */
2738 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_DCE)
2741 /* XXX handle errors */
2742 r = adev->ip_blocks[i].version->funcs->suspend(adev);
2743 /* XXX handle errors */
2745 DRM_ERROR("suspend of IP block <%s> failed %d\n",
2746 adev->ip_blocks[i].version->funcs->name, r);
2750 adev->ip_blocks[i].status.hw = false;
2757 * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2)
2759 * @adev: amdgpu_device pointer
2761 * Main suspend function for hardware IPs. The list of all the hardware
2762 * IPs that make up the asic is walked, clockgating is disabled and the
2763 * suspend callbacks are run. suspend puts the hardware and software state
2764 * in each IP into a state suitable for suspend.
2765 * Returns 0 on success, negative error code on failure.
2767 static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev)
2772 amdgpu_gfx_state_change_set(adev, sGpuChangeState_D3Entry);
2774 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2775 if (!adev->ip_blocks[i].status.valid)
2777 /* displays are handled in phase1 */
2778 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)
2780 /* PSP lost connection when err_event_athub occurs */
2781 if (amdgpu_ras_intr_triggered() &&
2782 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
2783 adev->ip_blocks[i].status.hw = false;
2787 /* skip unnecessary suspend if we do not initialize them yet */
2788 if (adev->gmc.xgmi.pending_reset &&
2789 !(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2790 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC ||
2791 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2792 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)) {
2793 adev->ip_blocks[i].status.hw = false;
2797 /* skip suspend of gfx and psp for S0ix
2798 * gfx is in gfxoff state, so on resume it will exit gfxoff just
2799 * like at runtime. PSP is also part of the always on hardware
2800 * so no need to suspend it.
2802 if (adev->in_s0ix &&
2803 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP ||
2804 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX))
2807 /* XXX handle errors */
2808 r = adev->ip_blocks[i].version->funcs->suspend(adev);
2809 /* XXX handle errors */
2811 DRM_ERROR("suspend of IP block <%s> failed %d\n",
2812 adev->ip_blocks[i].version->funcs->name, r);
2814 adev->ip_blocks[i].status.hw = false;
2815 /* handle putting the SMC in the appropriate state */
2816 if(!amdgpu_sriov_vf(adev)){
2817 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
2818 r = amdgpu_dpm_set_mp1_state(adev, adev->mp1_state);
2820 DRM_ERROR("SMC failed to set mp1 state %d, %d\n",
2821 adev->mp1_state, r);
2832 * amdgpu_device_ip_suspend - run suspend for hardware IPs
2834 * @adev: amdgpu_device pointer
2836 * Main suspend function for hardware IPs. The list of all the hardware
2837 * IPs that make up the asic is walked, clockgating is disabled and the
2838 * suspend callbacks are run. suspend puts the hardware and software state
2839 * in each IP into a state suitable for suspend.
2840 * Returns 0 on success, negative error code on failure.
2842 int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
2846 if (amdgpu_sriov_vf(adev)) {
2847 amdgpu_virt_fini_data_exchange(adev);
2848 amdgpu_virt_request_full_gpu(adev, false);
2851 r = amdgpu_device_ip_suspend_phase1(adev);
2854 r = amdgpu_device_ip_suspend_phase2(adev);
2856 if (amdgpu_sriov_vf(adev))
2857 amdgpu_virt_release_full_gpu(adev, false);
2862 static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
2866 static enum amd_ip_block_type ip_order[] = {
2867 AMD_IP_BLOCK_TYPE_GMC,
2868 AMD_IP_BLOCK_TYPE_COMMON,
2869 AMD_IP_BLOCK_TYPE_PSP,
2870 AMD_IP_BLOCK_TYPE_IH,
2873 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
2875 struct amdgpu_ip_block *block;
2877 block = &adev->ip_blocks[i];
2878 block->status.hw = false;
2880 for (j = 0; j < ARRAY_SIZE(ip_order); j++) {
2882 if (block->version->type != ip_order[j] ||
2883 !block->status.valid)
2886 r = block->version->funcs->hw_init(adev);
2887 DRM_INFO("RE-INIT-early: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
2890 block->status.hw = true;
2897 static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
2901 static enum amd_ip_block_type ip_order[] = {
2902 AMD_IP_BLOCK_TYPE_SMC,
2903 AMD_IP_BLOCK_TYPE_DCE,
2904 AMD_IP_BLOCK_TYPE_GFX,
2905 AMD_IP_BLOCK_TYPE_SDMA,
2906 AMD_IP_BLOCK_TYPE_UVD,
2907 AMD_IP_BLOCK_TYPE_VCE,
2908 AMD_IP_BLOCK_TYPE_VCN
2911 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
2913 struct amdgpu_ip_block *block;
2915 for (j = 0; j < adev->num_ip_blocks; j++) {
2916 block = &adev->ip_blocks[j];
2918 if (block->version->type != ip_order[i] ||
2919 !block->status.valid ||
2923 if (block->version->type == AMD_IP_BLOCK_TYPE_SMC)
2924 r = block->version->funcs->resume(adev);
2926 r = block->version->funcs->hw_init(adev);
2928 DRM_INFO("RE-INIT-late: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
2931 block->status.hw = true;
2939 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
2941 * @adev: amdgpu_device pointer
2943 * First resume function for hardware IPs. The list of all the hardware
2944 * IPs that make up the asic is walked and the resume callbacks are run for
2945 * COMMON, GMC, and IH. resume puts the hardware into a functional state
2946 * after a suspend and updates the software state as necessary. This
2947 * function is also used for restoring the GPU after a GPU reset.
2948 * Returns 0 on success, negative error code on failure.
2950 static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
2954 for (i = 0; i < adev->num_ip_blocks; i++) {
2955 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
2957 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2958 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2959 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
2961 r = adev->ip_blocks[i].version->funcs->resume(adev);
2963 DRM_ERROR("resume of IP block <%s> failed %d\n",
2964 adev->ip_blocks[i].version->funcs->name, r);
2967 adev->ip_blocks[i].status.hw = true;
2975 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
2977 * @adev: amdgpu_device pointer
2979 * First resume function for hardware IPs. The list of all the hardware
2980 * IPs that make up the asic is walked and the resume callbacks are run for
2981 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
2982 * functional state after a suspend and updates the software state as
2983 * necessary. This function is also used for restoring the GPU after a GPU
2985 * Returns 0 on success, negative error code on failure.
2987 static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
2991 for (i = 0; i < adev->num_ip_blocks; i++) {
2992 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
2994 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2995 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2996 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
2997 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)
2999 r = adev->ip_blocks[i].version->funcs->resume(adev);
3001 DRM_ERROR("resume of IP block <%s> failed %d\n",
3002 adev->ip_blocks[i].version->funcs->name, r);
3005 adev->ip_blocks[i].status.hw = true;
3012 * amdgpu_device_ip_resume - run resume for hardware IPs
3014 * @adev: amdgpu_device pointer
3016 * Main resume function for hardware IPs. The hardware IPs
3017 * are split into two resume functions because they are
3018 * are also used in in recovering from a GPU reset and some additional
3019 * steps need to be take between them. In this case (S3/S4) they are
3021 * Returns 0 on success, negative error code on failure.
3023 static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
3027 r = amdgpu_device_ip_resume_phase1(adev);
3031 r = amdgpu_device_fw_loading(adev);
3035 r = amdgpu_device_ip_resume_phase2(adev);
3041 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
3043 * @adev: amdgpu_device pointer
3045 * Query the VBIOS data tables to determine if the board supports SR-IOV.
3047 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
3049 if (amdgpu_sriov_vf(adev)) {
3050 if (adev->is_atom_fw) {
3051 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
3052 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
3054 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
3055 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
3058 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
3059 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
3064 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
3066 * @asic_type: AMD asic type
3068 * Check if there is DC (new modesetting infrastructre) support for an asic.
3069 * returns true if DC has support, false if not.
3071 bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
3073 switch (asic_type) {
3074 #if defined(CONFIG_DRM_AMD_DC)
3075 #if defined(CONFIG_DRM_AMD_DC_SI)
3086 * We have systems in the wild with these ASICs that require
3087 * LVDS and VGA support which is not supported with DC.
3089 * Fallback to the non-DC driver here by default so as not to
3090 * cause regressions.
3092 return amdgpu_dc > 0;
3096 case CHIP_POLARIS10:
3097 case CHIP_POLARIS11:
3098 case CHIP_POLARIS12:
3105 #if defined(CONFIG_DRM_AMD_DC_DCN)
3111 case CHIP_SIENNA_CICHLID:
3112 case CHIP_NAVY_FLOUNDER:
3113 case CHIP_DIMGREY_CAVEFISH:
3116 return amdgpu_dc != 0;
3120 DRM_INFO_ONCE("Display Core has been requested via kernel parameter "
3121 "but isn't supported by ASIC, ignoring\n");
3127 * amdgpu_device_has_dc_support - check if dc is supported
3129 * @adev: amdgpu_device pointer
3131 * Returns true for supported, false for not supported
3133 bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
3135 if (amdgpu_sriov_vf(adev) || adev->enable_virtual_display)
3138 return amdgpu_device_asic_has_dc_support(adev->asic_type);
3142 static void amdgpu_device_xgmi_reset_func(struct work_struct *__work)
3144 struct amdgpu_device *adev =
3145 container_of(__work, struct amdgpu_device, xgmi_reset_work);
3146 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
3148 /* It's a bug to not have a hive within this function */
3153 * Use task barrier to synchronize all xgmi reset works across the
3154 * hive. task_barrier_enter and task_barrier_exit will block
3155 * until all the threads running the xgmi reset works reach
3156 * those points. task_barrier_full will do both blocks.
3158 if (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) {
3160 task_barrier_enter(&hive->tb);
3161 adev->asic_reset_res = amdgpu_device_baco_enter(adev_to_drm(adev));
3163 if (adev->asic_reset_res)
3166 task_barrier_exit(&hive->tb);
3167 adev->asic_reset_res = amdgpu_device_baco_exit(adev_to_drm(adev));
3169 if (adev->asic_reset_res)
3172 if (adev->mmhub.ras_funcs &&
3173 adev->mmhub.ras_funcs->reset_ras_error_count)
3174 adev->mmhub.ras_funcs->reset_ras_error_count(adev);
3177 task_barrier_full(&hive->tb);
3178 adev->asic_reset_res = amdgpu_asic_reset(adev);
3182 if (adev->asic_reset_res)
3183 DRM_WARN("ASIC reset failed with error, %d for drm dev, %s",
3184 adev->asic_reset_res, adev_to_drm(adev)->unique);
3185 amdgpu_put_xgmi_hive(hive);
3188 static int amdgpu_device_get_job_timeout_settings(struct amdgpu_device *adev)
3190 char *input = amdgpu_lockup_timeout;
3191 char *timeout_setting = NULL;
3197 * By default timeout for non compute jobs is 10000.
3198 * And there is no timeout enforced on compute jobs.
3199 * In SR-IOV or passthrough mode, timeout for compute
3200 * jobs are 60000 by default.
3202 adev->gfx_timeout = msecs_to_jiffies(10000);
3203 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
3204 if (amdgpu_sriov_vf(adev))
3205 adev->compute_timeout = amdgpu_sriov_is_pp_one_vf(adev) ?
3206 msecs_to_jiffies(60000) : msecs_to_jiffies(10000);
3207 else if (amdgpu_passthrough(adev))
3208 adev->compute_timeout = msecs_to_jiffies(60000);
3210 adev->compute_timeout = MAX_SCHEDULE_TIMEOUT;
3212 if (strnlen(input, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
3213 while ((timeout_setting = strsep(&input, ",")) &&
3214 strnlen(timeout_setting, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
3215 ret = kstrtol(timeout_setting, 0, &timeout);
3222 } else if (timeout < 0) {
3223 timeout = MAX_SCHEDULE_TIMEOUT;
3225 timeout = msecs_to_jiffies(timeout);
3230 adev->gfx_timeout = timeout;
3233 adev->compute_timeout = timeout;
3236 adev->sdma_timeout = timeout;
3239 adev->video_timeout = timeout;
3246 * There is only one value specified and
3247 * it should apply to all non-compute jobs.
3250 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
3251 if (amdgpu_sriov_vf(adev) || amdgpu_passthrough(adev))
3252 adev->compute_timeout = adev->gfx_timeout;
3259 static const struct attribute *amdgpu_dev_attributes[] = {
3260 &dev_attr_product_name.attr,
3261 &dev_attr_product_number.attr,
3262 &dev_attr_serial_number.attr,
3263 &dev_attr_pcie_replay_count.attr,
3268 * amdgpu_device_init - initialize the driver
3270 * @adev: amdgpu_device pointer
3271 * @flags: driver flags
3273 * Initializes the driver info and hw (all asics).
3274 * Returns 0 for success or an error on failure.
3275 * Called at driver startup.
3277 int amdgpu_device_init(struct amdgpu_device *adev,
3280 struct drm_device *ddev = adev_to_drm(adev);
3281 struct pci_dev *pdev = adev->pdev;
3286 adev->shutdown = false;
3287 adev->flags = flags;
3289 if (amdgpu_force_asic_type >= 0 && amdgpu_force_asic_type < CHIP_LAST)
3290 adev->asic_type = amdgpu_force_asic_type;
3292 adev->asic_type = flags & AMD_ASIC_MASK;
3294 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
3295 if (amdgpu_emu_mode == 1)
3296 adev->usec_timeout *= 10;
3297 adev->gmc.gart_size = 512 * 1024 * 1024;
3298 adev->accel_working = false;
3299 adev->num_rings = 0;
3300 adev->mman.buffer_funcs = NULL;
3301 adev->mman.buffer_funcs_ring = NULL;
3302 adev->vm_manager.vm_pte_funcs = NULL;
3303 adev->vm_manager.vm_pte_num_scheds = 0;
3304 adev->gmc.gmc_funcs = NULL;
3305 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
3306 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
3308 adev->smc_rreg = &amdgpu_invalid_rreg;
3309 adev->smc_wreg = &amdgpu_invalid_wreg;
3310 adev->pcie_rreg = &amdgpu_invalid_rreg;
3311 adev->pcie_wreg = &amdgpu_invalid_wreg;
3312 adev->pciep_rreg = &amdgpu_invalid_rreg;
3313 adev->pciep_wreg = &amdgpu_invalid_wreg;
3314 adev->pcie_rreg64 = &amdgpu_invalid_rreg64;
3315 adev->pcie_wreg64 = &amdgpu_invalid_wreg64;
3316 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
3317 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
3318 adev->didt_rreg = &amdgpu_invalid_rreg;
3319 adev->didt_wreg = &amdgpu_invalid_wreg;
3320 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
3321 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
3322 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
3323 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
3325 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
3326 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
3327 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
3329 /* mutex initialization are all done here so we
3330 * can recall function without having locking issues */
3331 mutex_init(&adev->firmware.mutex);
3332 mutex_init(&adev->pm.mutex);
3333 mutex_init(&adev->gfx.gpu_clock_mutex);
3334 mutex_init(&adev->srbm_mutex);
3335 mutex_init(&adev->gfx.pipe_reserve_mutex);
3336 mutex_init(&adev->gfx.gfx_off_mutex);
3337 mutex_init(&adev->grbm_idx_mutex);
3338 mutex_init(&adev->mn_lock);
3339 mutex_init(&adev->virt.vf_errors.lock);
3340 hash_init(adev->mn_hash);
3341 atomic_set(&adev->in_gpu_reset, 0);
3342 init_rwsem(&adev->reset_sem);
3343 mutex_init(&adev->psp.mutex);
3344 mutex_init(&adev->notifier_lock);
3346 r = amdgpu_device_check_arguments(adev);
3350 spin_lock_init(&adev->mmio_idx_lock);
3351 spin_lock_init(&adev->smc_idx_lock);
3352 spin_lock_init(&adev->pcie_idx_lock);
3353 spin_lock_init(&adev->uvd_ctx_idx_lock);
3354 spin_lock_init(&adev->didt_idx_lock);
3355 spin_lock_init(&adev->gc_cac_idx_lock);
3356 spin_lock_init(&adev->se_cac_idx_lock);
3357 spin_lock_init(&adev->audio_endpt_idx_lock);
3358 spin_lock_init(&adev->mm_stats.lock);
3360 INIT_LIST_HEAD(&adev->shadow_list);
3361 mutex_init(&adev->shadow_list_lock);
3363 INIT_LIST_HEAD(&adev->reset_list);
3365 INIT_DELAYED_WORK(&adev->delayed_init_work,
3366 amdgpu_device_delayed_init_work_handler);
3367 INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work,
3368 amdgpu_device_delay_enable_gfx_off);
3370 INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
3372 adev->gfx.gfx_off_req_count = 1;
3373 adev->pm.ac_power = power_supply_is_system_supplied() > 0;
3375 atomic_set(&adev->throttling_logging_enabled, 1);
3377 * If throttling continues, logging will be performed every minute
3378 * to avoid log flooding. "-1" is subtracted since the thermal
3379 * throttling interrupt comes every second. Thus, the total logging
3380 * interval is 59 seconds(retelimited printk interval) + 1(waiting
3381 * for throttling interrupt) = 60 seconds.
3383 ratelimit_state_init(&adev->throttling_logging_rs, (60 - 1) * HZ, 1);
3384 ratelimit_set_flags(&adev->throttling_logging_rs, RATELIMIT_MSG_ON_RELEASE);
3386 /* Registers mapping */
3387 /* TODO: block userspace mapping of io register */
3388 if (adev->asic_type >= CHIP_BONAIRE) {
3389 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
3390 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
3392 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
3393 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
3396 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
3397 if (adev->rmmio == NULL) {
3400 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
3401 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
3403 /* enable PCIE atomic ops */
3404 r = pci_enable_atomic_ops_to_root(adev->pdev,
3405 PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
3406 PCI_EXP_DEVCAP2_ATOMIC_COMP64);
3408 adev->have_atomics_support = false;
3409 DRM_INFO("PCIE atomic ops is not supported\n");
3411 adev->have_atomics_support = true;
3414 amdgpu_device_get_pcie_info(adev);
3417 DRM_INFO("MCBP is enabled\n");
3419 if (amdgpu_mes && adev->asic_type >= CHIP_NAVI10)
3420 adev->enable_mes = true;
3422 /* detect hw virtualization here */
3423 amdgpu_detect_virtualization(adev);
3425 r = amdgpu_device_get_job_timeout_settings(adev);
3427 dev_err(adev->dev, "invalid lockup_timeout parameter syntax\n");
3431 /* early init functions */
3432 r = amdgpu_device_ip_early_init(adev);
3436 /* doorbell bar mapping and doorbell index init*/
3437 amdgpu_device_doorbell_init(adev);
3439 if (amdgpu_emu_mode == 1) {
3440 /* post the asic on emulation mode */
3441 emu_soc_asic_init(adev);
3442 goto fence_driver_init;
3445 amdgpu_reset_init(adev);
3447 /* detect if we are with an SRIOV vbios */
3448 amdgpu_device_detect_sriov_bios(adev);
3450 /* check if we need to reset the asic
3451 * E.g., driver was not cleanly unloaded previously, etc.
3453 if (!amdgpu_sriov_vf(adev) && amdgpu_asic_need_reset_on_init(adev)) {
3454 if (adev->gmc.xgmi.num_physical_nodes) {
3455 dev_info(adev->dev, "Pending hive reset.\n");
3456 adev->gmc.xgmi.pending_reset = true;
3457 /* Only need to init necessary block for SMU to handle the reset */
3458 for (i = 0; i < adev->num_ip_blocks; i++) {
3459 if (!adev->ip_blocks[i].status.valid)
3461 if (!(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3462 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3463 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3464 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC)) {
3465 DRM_DEBUG("IP %s disabled for hw_init.\n",
3466 adev->ip_blocks[i].version->funcs->name);
3467 adev->ip_blocks[i].status.hw = true;
3471 r = amdgpu_asic_reset(adev);
3473 dev_err(adev->dev, "asic reset on init failed\n");
3479 pci_enable_pcie_error_reporting(adev->pdev);
3481 /* Post card if necessary */
3482 if (amdgpu_device_need_post(adev)) {
3484 dev_err(adev->dev, "no vBIOS found\n");
3488 DRM_INFO("GPU posting now...\n");
3489 r = amdgpu_device_asic_init(adev);
3491 dev_err(adev->dev, "gpu post error!\n");
3496 if (adev->is_atom_fw) {
3497 /* Initialize clocks */
3498 r = amdgpu_atomfirmware_get_clock_info(adev);
3500 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
3501 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3505 /* Initialize clocks */
3506 r = amdgpu_atombios_get_clock_info(adev);
3508 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
3509 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3512 /* init i2c buses */
3513 if (!amdgpu_device_has_dc_support(adev))
3514 amdgpu_atombios_i2c_init(adev);
3519 r = amdgpu_fence_driver_init(adev);
3521 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
3522 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
3526 /* init the mode config */
3527 drm_mode_config_init(adev_to_drm(adev));
3529 r = amdgpu_device_ip_init(adev);
3531 /* failed in exclusive mode due to timeout */
3532 if (amdgpu_sriov_vf(adev) &&
3533 !amdgpu_sriov_runtime(adev) &&
3534 amdgpu_virt_mmio_blocked(adev) &&
3535 !amdgpu_virt_wait_reset(adev)) {
3536 dev_err(adev->dev, "VF exclusive mode timeout\n");
3537 /* Don't send request since VF is inactive. */
3538 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
3539 adev->virt.ops = NULL;
3541 goto release_ras_con;
3543 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
3544 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
3545 goto release_ras_con;
3549 "SE %d, SH per SE %d, CU per SH %d, active_cu_number %d\n",
3550 adev->gfx.config.max_shader_engines,
3551 adev->gfx.config.max_sh_per_se,
3552 adev->gfx.config.max_cu_per_sh,
3553 adev->gfx.cu_info.number);
3555 adev->accel_working = true;
3557 amdgpu_vm_check_compute_bug(adev);
3559 /* Initialize the buffer migration limit. */
3560 if (amdgpu_moverate >= 0)
3561 max_MBps = amdgpu_moverate;
3563 max_MBps = 8; /* Allow 8 MB/s. */
3564 /* Get a log2 for easy divisions. */
3565 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
3567 amdgpu_fbdev_init(adev);
3569 r = amdgpu_pm_sysfs_init(adev);
3571 adev->pm_sysfs_en = false;
3572 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
3574 adev->pm_sysfs_en = true;
3576 r = amdgpu_ucode_sysfs_init(adev);
3578 adev->ucode_sysfs_en = false;
3579 DRM_ERROR("Creating firmware sysfs failed (%d).\n", r);
3581 adev->ucode_sysfs_en = true;
3583 if ((amdgpu_testing & 1)) {
3584 if (adev->accel_working)
3585 amdgpu_test_moves(adev);
3587 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
3589 if (amdgpu_benchmarking) {
3590 if (adev->accel_working)
3591 amdgpu_benchmark(adev, amdgpu_benchmarking);
3593 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
3597 * Register gpu instance before amdgpu_device_enable_mgpu_fan_boost.
3598 * Otherwise the mgpu fan boost feature will be skipped due to the
3599 * gpu instance is counted less.
3601 amdgpu_register_gpu_instance(adev);
3603 /* enable clockgating, etc. after ib tests, etc. since some blocks require
3604 * explicit gating rather than handling it automatically.
3606 if (!adev->gmc.xgmi.pending_reset) {
3607 r = amdgpu_device_ip_late_init(adev);
3609 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
3610 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
3611 goto release_ras_con;
3614 amdgpu_ras_resume(adev);
3615 queue_delayed_work(system_wq, &adev->delayed_init_work,
3616 msecs_to_jiffies(AMDGPU_RESUME_MS));
3619 if (amdgpu_sriov_vf(adev))
3620 flush_delayed_work(&adev->delayed_init_work);
3622 r = sysfs_create_files(&adev->dev->kobj, amdgpu_dev_attributes);
3624 dev_err(adev->dev, "Could not create amdgpu device attr\n");
3626 if (IS_ENABLED(CONFIG_PERF_EVENTS))
3627 r = amdgpu_pmu_init(adev);
3629 dev_err(adev->dev, "amdgpu_pmu_init failed\n");
3631 /* Have stored pci confspace at hand for restore in sudden PCI error */
3632 if (amdgpu_device_cache_pci_state(adev->pdev))
3633 pci_restore_state(pdev);
3635 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
3636 /* this will fail for cards that aren't VGA class devices, just
3638 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
3639 vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
3641 if (amdgpu_device_supports_px(ddev)) {
3643 vga_switcheroo_register_client(adev->pdev,
3644 &amdgpu_switcheroo_ops, px);
3645 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
3648 if (adev->gmc.xgmi.pending_reset)
3649 queue_delayed_work(system_wq, &mgpu_info.delayed_reset_work,
3650 msecs_to_jiffies(AMDGPU_RESUME_MS));
3655 amdgpu_release_ras_context(adev);
3658 amdgpu_vf_error_trans_all(adev);
3661 iounmap(adev->rmmio);
3668 * amdgpu_device_fini - tear down the driver
3670 * @adev: amdgpu_device pointer
3672 * Tear down the driver info (all asics).
3673 * Called at driver shutdown.
3675 void amdgpu_device_fini_hw(struct amdgpu_device *adev)
3677 dev_info(adev->dev, "amdgpu: finishing device.\n");
3678 flush_delayed_work(&adev->delayed_init_work);
3679 ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
3680 adev->shutdown = true;
3682 /* make sure IB test finished before entering exclusive mode
3683 * to avoid preemption on IB test
3685 if (amdgpu_sriov_vf(adev)) {
3686 amdgpu_virt_request_full_gpu(adev, false);
3687 amdgpu_virt_fini_data_exchange(adev);
3690 /* disable all interrupts */
3691 amdgpu_irq_disable_all(adev);
3692 if (adev->mode_info.mode_config_initialized){
3693 if (!amdgpu_device_has_dc_support(adev))
3694 drm_helper_force_disable_all(adev_to_drm(adev));
3696 drm_atomic_helper_shutdown(adev_to_drm(adev));
3698 amdgpu_fence_driver_fini_hw(adev);
3700 if (adev->pm_sysfs_en)
3701 amdgpu_pm_sysfs_fini(adev);
3702 if (adev->ucode_sysfs_en)
3703 amdgpu_ucode_sysfs_fini(adev);
3704 sysfs_remove_files(&adev->dev->kobj, amdgpu_dev_attributes);
3706 amdgpu_fbdev_fini(adev);
3708 amdgpu_irq_fini_hw(adev);
3710 amdgpu_device_ip_fini_early(adev);
3712 amdgpu_gart_dummy_page_fini(adev);
3715 void amdgpu_device_fini_sw(struct amdgpu_device *adev)
3717 amdgpu_device_ip_fini(adev);
3718 amdgpu_fence_driver_fini_sw(adev);
3719 release_firmware(adev->firmware.gpu_info_fw);
3720 adev->firmware.gpu_info_fw = NULL;
3721 adev->accel_working = false;
3723 amdgpu_reset_fini(adev);
3725 /* free i2c buses */
3726 if (!amdgpu_device_has_dc_support(adev))
3727 amdgpu_i2c_fini(adev);
3729 if (amdgpu_emu_mode != 1)
3730 amdgpu_atombios_fini(adev);
3734 if (amdgpu_device_supports_px(adev_to_drm(adev))) {
3735 vga_switcheroo_unregister_client(adev->pdev);
3736 vga_switcheroo_fini_domain_pm_ops(adev->dev);
3738 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
3739 vga_client_register(adev->pdev, NULL, NULL, NULL);
3740 iounmap(adev->rmmio);
3742 amdgpu_device_doorbell_fini(adev);
3744 if (IS_ENABLED(CONFIG_PERF_EVENTS))
3745 amdgpu_pmu_fini(adev);
3746 if (adev->mman.discovery_bin)
3747 amdgpu_discovery_fini(adev);
3749 kfree(adev->pci_state);
3758 * amdgpu_device_suspend - initiate device suspend
3760 * @dev: drm dev pointer
3761 * @fbcon : notify the fbdev of suspend
3763 * Puts the hw in the suspend state (all asics).
3764 * Returns 0 for success or an error on failure.
3765 * Called at driver suspend.
3767 int amdgpu_device_suspend(struct drm_device *dev, bool fbcon)
3769 struct amdgpu_device *adev = drm_to_adev(dev);
3772 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
3775 adev->in_suspend = true;
3776 drm_kms_helper_poll_disable(dev);
3779 amdgpu_fbdev_set_suspend(adev, 1);
3781 cancel_delayed_work_sync(&adev->delayed_init_work);
3783 amdgpu_ras_suspend(adev);
3785 r = amdgpu_device_ip_suspend_phase1(adev);
3788 amdgpu_amdkfd_suspend(adev, adev->in_runpm);
3790 /* evict vram memory */
3791 amdgpu_bo_evict_vram(adev);
3793 amdgpu_fence_driver_suspend(adev);
3795 r = amdgpu_device_ip_suspend_phase2(adev);
3796 /* evict remaining vram memory
3797 * This second call to evict vram is to evict the gart page table
3800 amdgpu_bo_evict_vram(adev);
3806 * amdgpu_device_resume - initiate device resume
3808 * @dev: drm dev pointer
3809 * @fbcon : notify the fbdev of resume
3811 * Bring the hw back to operating state (all asics).
3812 * Returns 0 for success or an error on failure.
3813 * Called at driver resume.
3815 int amdgpu_device_resume(struct drm_device *dev, bool fbcon)
3817 struct amdgpu_device *adev = drm_to_adev(dev);
3820 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
3824 amdgpu_gfx_state_change_set(adev, sGpuChangeState_D0Entry);
3827 if (amdgpu_device_need_post(adev)) {
3828 r = amdgpu_device_asic_init(adev);
3830 dev_err(adev->dev, "amdgpu asic init failed\n");
3833 r = amdgpu_device_ip_resume(adev);
3835 dev_err(adev->dev, "amdgpu_device_ip_resume failed (%d).\n", r);
3838 amdgpu_fence_driver_resume(adev);
3841 r = amdgpu_device_ip_late_init(adev);
3845 queue_delayed_work(system_wq, &adev->delayed_init_work,
3846 msecs_to_jiffies(AMDGPU_RESUME_MS));
3848 if (!adev->in_s0ix) {
3849 r = amdgpu_amdkfd_resume(adev, adev->in_runpm);
3854 /* Make sure IB tests flushed */
3855 flush_delayed_work(&adev->delayed_init_work);
3858 amdgpu_fbdev_set_suspend(adev, 0);
3860 drm_kms_helper_poll_enable(dev);
3862 amdgpu_ras_resume(adev);
3865 * Most of the connector probing functions try to acquire runtime pm
3866 * refs to ensure that the GPU is powered on when connector polling is
3867 * performed. Since we're calling this from a runtime PM callback,
3868 * trying to acquire rpm refs will cause us to deadlock.
3870 * Since we're guaranteed to be holding the rpm lock, it's safe to
3871 * temporarily disable the rpm helpers so this doesn't deadlock us.
3874 dev->dev->power.disable_depth++;
3876 if (!amdgpu_device_has_dc_support(adev))
3877 drm_helper_hpd_irq_event(dev);
3879 drm_kms_helper_hotplug_event(dev);
3881 dev->dev->power.disable_depth--;
3883 adev->in_suspend = false;
3889 * amdgpu_device_ip_check_soft_reset - did soft reset succeed
3891 * @adev: amdgpu_device pointer
3893 * The list of all the hardware IPs that make up the asic is walked and
3894 * the check_soft_reset callbacks are run. check_soft_reset determines
3895 * if the asic is still hung or not.
3896 * Returns true if any of the IPs are still in a hung state, false if not.
3898 static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
3901 bool asic_hang = false;
3903 if (amdgpu_sriov_vf(adev))
3906 if (amdgpu_asic_need_full_reset(adev))
3909 for (i = 0; i < adev->num_ip_blocks; i++) {
3910 if (!adev->ip_blocks[i].status.valid)
3912 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
3913 adev->ip_blocks[i].status.hang =
3914 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
3915 if (adev->ip_blocks[i].status.hang) {
3916 dev_info(adev->dev, "IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
3924 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
3926 * @adev: amdgpu_device pointer
3928 * The list of all the hardware IPs that make up the asic is walked and the
3929 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
3930 * handles any IP specific hardware or software state changes that are
3931 * necessary for a soft reset to succeed.
3932 * Returns 0 on success, negative error code on failure.
3934 static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
3938 for (i = 0; i < adev->num_ip_blocks; i++) {
3939 if (!adev->ip_blocks[i].status.valid)
3941 if (adev->ip_blocks[i].status.hang &&
3942 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
3943 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
3953 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
3955 * @adev: amdgpu_device pointer
3957 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
3958 * reset is necessary to recover.
3959 * Returns true if a full asic reset is required, false if not.
3961 static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
3965 if (amdgpu_asic_need_full_reset(adev))
3968 for (i = 0; i < adev->num_ip_blocks; i++) {
3969 if (!adev->ip_blocks[i].status.valid)
3971 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
3972 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
3973 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
3974 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
3975 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
3976 if (adev->ip_blocks[i].status.hang) {
3977 dev_info(adev->dev, "Some block need full reset!\n");
3986 * amdgpu_device_ip_soft_reset - do a soft reset
3988 * @adev: amdgpu_device pointer
3990 * The list of all the hardware IPs that make up the asic is walked and the
3991 * soft_reset callbacks are run if the block is hung. soft_reset handles any
3992 * IP specific hardware or software state changes that are necessary to soft
3994 * Returns 0 on success, negative error code on failure.
3996 static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
4000 for (i = 0; i < adev->num_ip_blocks; i++) {
4001 if (!adev->ip_blocks[i].status.valid)
4003 if (adev->ip_blocks[i].status.hang &&
4004 adev->ip_blocks[i].version->funcs->soft_reset) {
4005 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
4015 * amdgpu_device_ip_post_soft_reset - clean up from soft reset
4017 * @adev: amdgpu_device pointer
4019 * The list of all the hardware IPs that make up the asic is walked and the
4020 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
4021 * handles any IP specific hardware or software state changes that are
4022 * necessary after the IP has been soft reset.
4023 * Returns 0 on success, negative error code on failure.
4025 static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
4029 for (i = 0; i < adev->num_ip_blocks; i++) {
4030 if (!adev->ip_blocks[i].status.valid)
4032 if (adev->ip_blocks[i].status.hang &&
4033 adev->ip_blocks[i].version->funcs->post_soft_reset)
4034 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
4043 * amdgpu_device_recover_vram - Recover some VRAM contents
4045 * @adev: amdgpu_device pointer
4047 * Restores the contents of VRAM buffers from the shadows in GTT. Used to
4048 * restore things like GPUVM page tables after a GPU reset where
4049 * the contents of VRAM might be lost.
4052 * 0 on success, negative error code on failure.
4054 static int amdgpu_device_recover_vram(struct amdgpu_device *adev)
4056 struct dma_fence *fence = NULL, *next = NULL;
4057 struct amdgpu_bo *shadow;
4060 if (amdgpu_sriov_runtime(adev))
4061 tmo = msecs_to_jiffies(8000);
4063 tmo = msecs_to_jiffies(100);
4065 dev_info(adev->dev, "recover vram bo from shadow start\n");
4066 mutex_lock(&adev->shadow_list_lock);
4067 list_for_each_entry(shadow, &adev->shadow_list, shadow_list) {
4069 /* No need to recover an evicted BO */
4070 if (shadow->tbo.mem.mem_type != TTM_PL_TT ||
4071 shadow->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET ||
4072 shadow->parent->tbo.mem.mem_type != TTM_PL_VRAM)
4075 r = amdgpu_bo_restore_shadow(shadow, &next);
4080 tmo = dma_fence_wait_timeout(fence, false, tmo);
4081 dma_fence_put(fence);
4086 } else if (tmo < 0) {
4094 mutex_unlock(&adev->shadow_list_lock);
4097 tmo = dma_fence_wait_timeout(fence, false, tmo);
4098 dma_fence_put(fence);
4100 if (r < 0 || tmo <= 0) {
4101 dev_err(adev->dev, "recover vram bo from shadow failed, r is %ld, tmo is %ld\n", r, tmo);
4105 dev_info(adev->dev, "recover vram bo from shadow done\n");
4111 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
4113 * @adev: amdgpu_device pointer
4114 * @from_hypervisor: request from hypervisor
4116 * do VF FLR and reinitialize Asic
4117 * return 0 means succeeded otherwise failed
4119 static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
4120 bool from_hypervisor)
4124 if (from_hypervisor)
4125 r = amdgpu_virt_request_full_gpu(adev, true);
4127 r = amdgpu_virt_reset_gpu(adev);
4131 amdgpu_amdkfd_pre_reset(adev);
4133 /* Resume IP prior to SMC */
4134 r = amdgpu_device_ip_reinit_early_sriov(adev);
4138 amdgpu_virt_init_data_exchange(adev);
4139 /* we need recover gart prior to run SMC/CP/SDMA resume */
4140 amdgpu_gtt_mgr_recover(ttm_manager_type(&adev->mman.bdev, TTM_PL_TT));
4142 r = amdgpu_device_fw_loading(adev);
4146 /* now we are okay to resume SMC/CP/SDMA */
4147 r = amdgpu_device_ip_reinit_late_sriov(adev);
4151 amdgpu_irq_gpu_reset_resume_helper(adev);
4152 r = amdgpu_ib_ring_tests(adev);
4153 amdgpu_amdkfd_post_reset(adev);
4156 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
4157 amdgpu_inc_vram_lost(adev);
4158 r = amdgpu_device_recover_vram(adev);
4160 amdgpu_virt_release_full_gpu(adev, true);
4166 * amdgpu_device_has_job_running - check if there is any job in mirror list
4168 * @adev: amdgpu_device pointer
4170 * check if there is any job in mirror list
4172 bool amdgpu_device_has_job_running(struct amdgpu_device *adev)
4175 struct drm_sched_job *job;
4177 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4178 struct amdgpu_ring *ring = adev->rings[i];
4180 if (!ring || !ring->sched.thread)
4183 spin_lock(&ring->sched.job_list_lock);
4184 job = list_first_entry_or_null(&ring->sched.pending_list,
4185 struct drm_sched_job, list);
4186 spin_unlock(&ring->sched.job_list_lock);
4194 * amdgpu_device_should_recover_gpu - check if we should try GPU recovery
4196 * @adev: amdgpu_device pointer
4198 * Check amdgpu_gpu_recovery and SRIOV status to see if we should try to recover
4201 bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev)
4203 if (!amdgpu_device_ip_check_soft_reset(adev)) {
4204 dev_info(adev->dev, "Timeout, but no hardware hang detected.\n");
4208 if (amdgpu_gpu_recovery == 0)
4211 if (amdgpu_sriov_vf(adev))
4214 if (amdgpu_gpu_recovery == -1) {
4215 switch (adev->asic_type) {
4221 case CHIP_POLARIS10:
4222 case CHIP_POLARIS11:
4223 case CHIP_POLARIS12:
4234 case CHIP_SIENNA_CICHLID:
4235 case CHIP_NAVY_FLOUNDER:
4236 case CHIP_DIMGREY_CAVEFISH:
4238 case CHIP_ALDEBARAN:
4248 dev_info(adev->dev, "GPU recovery disabled.\n");
4252 int amdgpu_device_mode1_reset(struct amdgpu_device *adev)
4257 amdgpu_atombios_scratch_regs_engine_hung(adev, true);
4259 dev_info(adev->dev, "GPU mode1 reset\n");
4262 pci_clear_master(adev->pdev);
4264 amdgpu_device_cache_pci_state(adev->pdev);
4266 if (amdgpu_dpm_is_mode1_reset_supported(adev)) {
4267 dev_info(adev->dev, "GPU smu mode1 reset\n");
4268 ret = amdgpu_dpm_mode1_reset(adev);
4270 dev_info(adev->dev, "GPU psp mode1 reset\n");
4271 ret = psp_gpu_reset(adev);
4275 dev_err(adev->dev, "GPU mode1 reset failed\n");
4277 amdgpu_device_load_pci_state(adev->pdev);
4279 /* wait for asic to come out of reset */
4280 for (i = 0; i < adev->usec_timeout; i++) {
4281 u32 memsize = adev->nbio.funcs->get_memsize(adev);
4283 if (memsize != 0xffffffff)
4288 amdgpu_atombios_scratch_regs_engine_hung(adev, false);
4292 int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
4293 struct amdgpu_reset_context *reset_context)
4296 struct amdgpu_job *job = NULL;
4297 bool need_full_reset =
4298 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4300 if (reset_context->reset_req_dev == adev)
4301 job = reset_context->job;
4303 /* no need to dump if device is not in good state during probe period */
4304 if (!adev->gmc.xgmi.pending_reset)
4305 amdgpu_debugfs_wait_dump(adev);
4307 if (amdgpu_sriov_vf(adev)) {
4308 /* stop the data exchange thread */
4309 amdgpu_virt_fini_data_exchange(adev);
4312 /* block all schedulers and reset given job's ring */
4313 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4314 struct amdgpu_ring *ring = adev->rings[i];
4316 if (!ring || !ring->sched.thread)
4319 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
4320 amdgpu_fence_driver_force_completion(ring);
4324 drm_sched_increase_karma(&job->base);
4326 r = amdgpu_reset_prepare_hwcontext(adev, reset_context);
4327 /* If reset handler not implemented, continue; otherwise return */
4333 /* Don't suspend on bare metal if we are not going to HW reset the ASIC */
4334 if (!amdgpu_sriov_vf(adev)) {
4336 if (!need_full_reset)
4337 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
4339 if (!need_full_reset) {
4340 amdgpu_device_ip_pre_soft_reset(adev);
4341 r = amdgpu_device_ip_soft_reset(adev);
4342 amdgpu_device_ip_post_soft_reset(adev);
4343 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
4344 dev_info(adev->dev, "soft reset failed, will fallback to full reset!\n");
4345 need_full_reset = true;
4349 if (need_full_reset)
4350 r = amdgpu_device_ip_suspend(adev);
4351 if (need_full_reset)
4352 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4354 clear_bit(AMDGPU_NEED_FULL_RESET,
4355 &reset_context->flags);
4361 int amdgpu_do_asic_reset(struct list_head *device_list_handle,
4362 struct amdgpu_reset_context *reset_context)
4364 struct amdgpu_device *tmp_adev = NULL;
4365 bool need_full_reset, skip_hw_reset, vram_lost = false;
4368 /* Try reset handler method first */
4369 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
4371 r = amdgpu_reset_perform_reset(tmp_adev, reset_context);
4372 /* If reset handler not implemented, continue; otherwise return */
4378 /* Reset handler not implemented, use the default method */
4380 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4381 skip_hw_reset = test_bit(AMDGPU_SKIP_HW_RESET, &reset_context->flags);
4384 * ASIC reset has to be done on all XGMI hive nodes ASAP
4385 * to allow proper links negotiation in FW (within 1 sec)
4387 if (!skip_hw_reset && need_full_reset) {
4388 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4389 /* For XGMI run all resets in parallel to speed up the process */
4390 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
4391 tmp_adev->gmc.xgmi.pending_reset = false;
4392 if (!queue_work(system_unbound_wq, &tmp_adev->xgmi_reset_work))
4395 r = amdgpu_asic_reset(tmp_adev);
4398 dev_err(tmp_adev->dev, "ASIC reset failed with error, %d for drm dev, %s",
4399 r, adev_to_drm(tmp_adev)->unique);
4404 /* For XGMI wait for all resets to complete before proceed */
4406 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4407 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
4408 flush_work(&tmp_adev->xgmi_reset_work);
4409 r = tmp_adev->asic_reset_res;
4417 if (!r && amdgpu_ras_intr_triggered()) {
4418 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4419 if (tmp_adev->mmhub.ras_funcs &&
4420 tmp_adev->mmhub.ras_funcs->reset_ras_error_count)
4421 tmp_adev->mmhub.ras_funcs->reset_ras_error_count(tmp_adev);
4424 amdgpu_ras_intr_cleared();
4427 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4428 if (need_full_reset) {
4430 r = amdgpu_device_asic_init(tmp_adev);
4432 dev_warn(tmp_adev->dev, "asic atom init failed!");
4434 dev_info(tmp_adev->dev, "GPU reset succeeded, trying to resume\n");
4435 r = amdgpu_device_ip_resume_phase1(tmp_adev);
4439 vram_lost = amdgpu_device_check_vram_lost(tmp_adev);
4441 DRM_INFO("VRAM is lost due to GPU reset!\n");
4442 amdgpu_inc_vram_lost(tmp_adev);
4445 r = amdgpu_gtt_mgr_recover(ttm_manager_type(&tmp_adev->mman.bdev, TTM_PL_TT));
4449 r = amdgpu_device_fw_loading(tmp_adev);
4453 r = amdgpu_device_ip_resume_phase2(tmp_adev);
4458 amdgpu_device_fill_reset_magic(tmp_adev);
4461 * Add this ASIC as tracked as reset was already
4462 * complete successfully.
4464 amdgpu_register_gpu_instance(tmp_adev);
4466 if (!reset_context->hive &&
4467 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
4468 amdgpu_xgmi_add_device(tmp_adev);
4470 r = amdgpu_device_ip_late_init(tmp_adev);
4474 amdgpu_fbdev_set_suspend(tmp_adev, 0);
4477 * The GPU enters bad state once faulty pages
4478 * by ECC has reached the threshold, and ras
4479 * recovery is scheduled next. So add one check
4480 * here to break recovery if it indeed exceeds
4481 * bad page threshold, and remind user to
4482 * retire this GPU or setting one bigger
4483 * bad_page_threshold value to fix this once
4484 * probing driver again.
4486 if (!amdgpu_ras_eeprom_check_err_threshold(tmp_adev)) {
4488 amdgpu_ras_resume(tmp_adev);
4494 /* Update PSP FW topology after reset */
4495 if (reset_context->hive &&
4496 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
4497 r = amdgpu_xgmi_update_topology(
4498 reset_context->hive, tmp_adev);
4504 amdgpu_irq_gpu_reset_resume_helper(tmp_adev);
4505 r = amdgpu_ib_ring_tests(tmp_adev);
4507 dev_err(tmp_adev->dev, "ib ring test failed (%d).\n", r);
4508 r = amdgpu_device_ip_suspend(tmp_adev);
4509 need_full_reset = true;
4516 r = amdgpu_device_recover_vram(tmp_adev);
4518 tmp_adev->asic_reset_res = r;
4522 if (need_full_reset)
4523 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4525 clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4529 static bool amdgpu_device_lock_adev(struct amdgpu_device *adev,
4530 struct amdgpu_hive_info *hive)
4532 if (atomic_cmpxchg(&adev->in_gpu_reset, 0, 1) != 0)
4536 down_write_nest_lock(&adev->reset_sem, &hive->hive_lock);
4538 down_write(&adev->reset_sem);
4541 switch (amdgpu_asic_reset_method(adev)) {
4542 case AMD_RESET_METHOD_MODE1:
4543 adev->mp1_state = PP_MP1_STATE_SHUTDOWN;
4545 case AMD_RESET_METHOD_MODE2:
4546 adev->mp1_state = PP_MP1_STATE_RESET;
4549 adev->mp1_state = PP_MP1_STATE_NONE;
4556 static void amdgpu_device_unlock_adev(struct amdgpu_device *adev)
4558 amdgpu_vf_error_trans_all(adev);
4559 adev->mp1_state = PP_MP1_STATE_NONE;
4560 atomic_set(&adev->in_gpu_reset, 0);
4561 up_write(&adev->reset_sem);
4565 * to lockup a list of amdgpu devices in a hive safely, if not a hive
4566 * with multiple nodes, it will be similar as amdgpu_device_lock_adev.
4568 * unlock won't require roll back.
4570 static int amdgpu_device_lock_hive_adev(struct amdgpu_device *adev, struct amdgpu_hive_info *hive)
4572 struct amdgpu_device *tmp_adev = NULL;
4574 if (adev->gmc.xgmi.num_physical_nodes > 1) {
4576 dev_err(adev->dev, "Hive is NULL while device has multiple xgmi nodes");
4579 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
4580 if (!amdgpu_device_lock_adev(tmp_adev, hive))
4583 } else if (!amdgpu_device_lock_adev(adev, hive))
4588 if (!list_is_first(&tmp_adev->gmc.xgmi.head, &hive->device_list)) {
4590 * if the lockup iteration break in the middle of a hive,
4591 * it may means there may has a race issue,
4592 * or a hive device locked up independently.
4593 * we may be in trouble and may not, so will try to roll back
4594 * the lock and give out a warnning.
4596 dev_warn(tmp_adev->dev, "Hive lock iteration broke in the middle. Rolling back to unlock");
4597 list_for_each_entry_continue_reverse(tmp_adev, &hive->device_list, gmc.xgmi.head) {
4598 amdgpu_device_unlock_adev(tmp_adev);
4604 static void amdgpu_device_resume_display_audio(struct amdgpu_device *adev)
4606 struct pci_dev *p = NULL;
4608 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
4609 adev->pdev->bus->number, 1);
4611 pm_runtime_enable(&(p->dev));
4612 pm_runtime_resume(&(p->dev));
4616 static int amdgpu_device_suspend_display_audio(struct amdgpu_device *adev)
4618 enum amd_reset_method reset_method;
4619 struct pci_dev *p = NULL;
4623 * For now, only BACO and mode1 reset are confirmed
4624 * to suffer the audio issue without proper suspended.
4626 reset_method = amdgpu_asic_reset_method(adev);
4627 if ((reset_method != AMD_RESET_METHOD_BACO) &&
4628 (reset_method != AMD_RESET_METHOD_MODE1))
4631 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
4632 adev->pdev->bus->number, 1);
4636 expires = pm_runtime_autosuspend_expiration(&(p->dev));
4639 * If we cannot get the audio device autosuspend delay,
4640 * a fixed 4S interval will be used. Considering 3S is
4641 * the audio controller default autosuspend delay setting.
4642 * 4S used here is guaranteed to cover that.
4644 expires = ktime_get_mono_fast_ns() + NSEC_PER_SEC * 4ULL;
4646 while (!pm_runtime_status_suspended(&(p->dev))) {
4647 if (!pm_runtime_suspend(&(p->dev)))
4650 if (expires < ktime_get_mono_fast_ns()) {
4651 dev_warn(adev->dev, "failed to suspend display audio\n");
4652 /* TODO: abort the succeeding gpu reset? */
4657 pm_runtime_disable(&(p->dev));
4662 void amdgpu_device_recheck_guilty_jobs(
4663 struct amdgpu_device *adev, struct list_head *device_list_handle,
4664 struct amdgpu_reset_context *reset_context)
4668 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4669 struct amdgpu_ring *ring = adev->rings[i];
4671 struct drm_sched_job *s_job;
4673 if (!ring || !ring->sched.thread)
4676 s_job = list_first_entry_or_null(&ring->sched.pending_list,
4677 struct drm_sched_job, list);
4681 /* clear job's guilty and depend the folowing step to decide the real one */
4682 drm_sched_reset_karma(s_job);
4683 drm_sched_resubmit_jobs_ext(&ring->sched, 1);
4685 ret = dma_fence_wait_timeout(s_job->s_fence->parent, false, ring->sched.timeout);
4686 if (ret == 0) { /* timeout */
4687 DRM_ERROR("Found the real bad job! ring:%s, job_id:%llx\n",
4688 ring->sched.name, s_job->id);
4691 drm_sched_increase_karma(s_job);
4694 if (amdgpu_sriov_vf(adev)) {
4695 amdgpu_virt_fini_data_exchange(adev);
4696 r = amdgpu_device_reset_sriov(adev, false);
4698 adev->asic_reset_res = r;
4700 clear_bit(AMDGPU_SKIP_HW_RESET,
4701 &reset_context->flags);
4702 r = amdgpu_do_asic_reset(device_list_handle,
4704 if (r && r == -EAGAIN)
4709 * add reset counter so that the following
4710 * resubmitted job could flush vmid
4712 atomic_inc(&adev->gpu_reset_counter);
4716 /* got the hw fence, signal finished fence */
4717 atomic_dec(ring->sched.score);
4718 dma_fence_get(&s_job->s_fence->finished);
4719 dma_fence_signal(&s_job->s_fence->finished);
4720 dma_fence_put(&s_job->s_fence->finished);
4722 /* remove node from list and free the job */
4723 spin_lock(&ring->sched.job_list_lock);
4724 list_del_init(&s_job->list);
4725 spin_unlock(&ring->sched.job_list_lock);
4726 ring->sched.ops->free_job(s_job);
4731 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
4733 * @adev: amdgpu_device pointer
4734 * @job: which job trigger hang
4736 * Attempt to reset the GPU if it has hung (all asics).
4737 * Attempt to do soft-reset or full-reset and reinitialize Asic
4738 * Returns 0 for success or an error on failure.
4741 int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
4742 struct amdgpu_job *job)
4744 struct list_head device_list, *device_list_handle = NULL;
4745 bool job_signaled = false;
4746 struct amdgpu_hive_info *hive = NULL;
4747 struct amdgpu_device *tmp_adev = NULL;
4749 bool need_emergency_restart = false;
4750 bool audio_suspended = false;
4751 int tmp_vram_lost_counter;
4752 struct amdgpu_reset_context reset_context;
4754 memset(&reset_context, 0, sizeof(reset_context));
4757 * Special case: RAS triggered and full reset isn't supported
4759 need_emergency_restart = amdgpu_ras_need_emergency_restart(adev);
4762 * Flush RAM to disk so that after reboot
4763 * the user can read log and see why the system rebooted.
4765 if (need_emergency_restart && amdgpu_ras_get_context(adev)->reboot) {
4766 DRM_WARN("Emergency reboot.");
4769 emergency_restart();
4772 dev_info(adev->dev, "GPU %s begin!\n",
4773 need_emergency_restart ? "jobs stop":"reset");
4776 * Here we trylock to avoid chain of resets executing from
4777 * either trigger by jobs on different adevs in XGMI hive or jobs on
4778 * different schedulers for same device while this TO handler is running.
4779 * We always reset all schedulers for device and all devices for XGMI
4780 * hive so that should take care of them too.
4782 hive = amdgpu_get_xgmi_hive(adev);
4784 if (atomic_cmpxchg(&hive->in_reset, 0, 1) != 0) {
4785 DRM_INFO("Bailing on TDR for s_job:%llx, hive: %llx as another already in progress",
4786 job ? job->base.id : -1, hive->hive_id);
4787 amdgpu_put_xgmi_hive(hive);
4789 drm_sched_increase_karma(&job->base);
4792 mutex_lock(&hive->hive_lock);
4795 reset_context.method = AMD_RESET_METHOD_NONE;
4796 reset_context.reset_req_dev = adev;
4797 reset_context.job = job;
4798 reset_context.hive = hive;
4799 clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
4802 * lock the device before we try to operate the linked list
4803 * if didn't get the device lock, don't touch the linked list since
4804 * others may iterating it.
4806 r = amdgpu_device_lock_hive_adev(adev, hive);
4808 dev_info(adev->dev, "Bailing on TDR for s_job:%llx, as another already in progress",
4809 job ? job->base.id : -1);
4811 /* even we skipped this reset, still need to set the job to guilty */
4813 drm_sched_increase_karma(&job->base);
4818 * Build list of devices to reset.
4819 * In case we are in XGMI hive mode, resort the device list
4820 * to put adev in the 1st position.
4822 INIT_LIST_HEAD(&device_list);
4823 if (adev->gmc.xgmi.num_physical_nodes > 1) {
4824 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head)
4825 list_add_tail(&tmp_adev->reset_list, &device_list);
4826 if (!list_is_first(&adev->reset_list, &device_list))
4827 list_rotate_to_front(&adev->reset_list, &device_list);
4828 device_list_handle = &device_list;
4830 list_add_tail(&adev->reset_list, &device_list);
4831 device_list_handle = &device_list;
4834 /* block all schedulers and reset given job's ring */
4835 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4837 * Try to put the audio codec into suspend state
4838 * before gpu reset started.
4840 * Due to the power domain of the graphics device
4841 * is shared with AZ power domain. Without this,
4842 * we may change the audio hardware from behind
4843 * the audio driver's back. That will trigger
4844 * some audio codec errors.
4846 if (!amdgpu_device_suspend_display_audio(tmp_adev))
4847 audio_suspended = true;
4849 amdgpu_ras_set_error_query_ready(tmp_adev, false);
4851 cancel_delayed_work_sync(&tmp_adev->delayed_init_work);
4853 if (!amdgpu_sriov_vf(tmp_adev))
4854 amdgpu_amdkfd_pre_reset(tmp_adev);
4857 * Mark these ASICs to be reseted as untracked first
4858 * And add them back after reset completed
4860 amdgpu_unregister_gpu_instance(tmp_adev);
4862 amdgpu_fbdev_set_suspend(tmp_adev, 1);
4864 /* disable ras on ALL IPs */
4865 if (!need_emergency_restart &&
4866 amdgpu_device_ip_need_full_reset(tmp_adev))
4867 amdgpu_ras_suspend(tmp_adev);
4869 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4870 struct amdgpu_ring *ring = tmp_adev->rings[i];
4872 if (!ring || !ring->sched.thread)
4875 drm_sched_stop(&ring->sched, job ? &job->base : NULL);
4877 if (need_emergency_restart)
4878 amdgpu_job_stop_all_jobs_on_sched(&ring->sched);
4880 atomic_inc(&tmp_adev->gpu_reset_counter);
4883 if (need_emergency_restart)
4884 goto skip_sched_resume;
4887 * Must check guilty signal here since after this point all old
4888 * HW fences are force signaled.
4890 * job->base holds a reference to parent fence
4892 if (job && job->base.s_fence->parent &&
4893 dma_fence_is_signaled(job->base.s_fence->parent)) {
4894 job_signaled = true;
4895 dev_info(adev->dev, "Guilty job already signaled, skipping HW reset");
4899 retry: /* Rest of adevs pre asic reset from XGMI hive. */
4900 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4901 r = amdgpu_device_pre_asic_reset(tmp_adev, &reset_context);
4902 /*TODO Should we stop ?*/
4904 dev_err(tmp_adev->dev, "GPU pre asic reset failed with err, %d for drm dev, %s ",
4905 r, adev_to_drm(tmp_adev)->unique);
4906 tmp_adev->asic_reset_res = r;
4910 tmp_vram_lost_counter = atomic_read(&((adev)->vram_lost_counter));
4911 /* Actual ASIC resets if needed.*/
4912 /* TODO Implement XGMI hive reset logic for SRIOV */
4913 if (amdgpu_sriov_vf(adev)) {
4914 r = amdgpu_device_reset_sriov(adev, job ? false : true);
4916 adev->asic_reset_res = r;
4918 r = amdgpu_do_asic_reset(device_list_handle, &reset_context);
4919 if (r && r == -EAGAIN)
4925 /* Post ASIC reset for all devs .*/
4926 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4929 * Sometimes a later bad compute job can block a good gfx job as gfx
4930 * and compute ring share internal GC HW mutually. We add an additional
4931 * guilty jobs recheck step to find the real guilty job, it synchronously
4932 * submits and pends for the first job being signaled. If it gets timeout,
4933 * we identify it as a real guilty job.
4935 if (amdgpu_gpu_recovery == 2 &&
4936 !(tmp_vram_lost_counter < atomic_read(&adev->vram_lost_counter)))
4937 amdgpu_device_recheck_guilty_jobs(
4938 tmp_adev, device_list_handle, &reset_context);
4940 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4941 struct amdgpu_ring *ring = tmp_adev->rings[i];
4943 if (!ring || !ring->sched.thread)
4946 /* No point to resubmit jobs if we didn't HW reset*/
4947 if (!tmp_adev->asic_reset_res && !job_signaled)
4948 drm_sched_resubmit_jobs(&ring->sched);
4950 drm_sched_start(&ring->sched, !tmp_adev->asic_reset_res);
4953 if (!amdgpu_device_has_dc_support(tmp_adev) && !job_signaled) {
4954 drm_helper_resume_force_mode(adev_to_drm(tmp_adev));
4957 tmp_adev->asic_reset_res = 0;
4960 /* bad news, how to tell it to userspace ? */
4961 dev_info(tmp_adev->dev, "GPU reset(%d) failed\n", atomic_read(&tmp_adev->gpu_reset_counter));
4962 amdgpu_vf_error_put(tmp_adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
4964 dev_info(tmp_adev->dev, "GPU reset(%d) succeeded!\n", atomic_read(&tmp_adev->gpu_reset_counter));
4969 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4970 /* unlock kfd: SRIOV would do it separately */
4971 if (!need_emergency_restart && !amdgpu_sriov_vf(tmp_adev))
4972 amdgpu_amdkfd_post_reset(tmp_adev);
4974 /* kfd_post_reset will do nothing if kfd device is not initialized,
4975 * need to bring up kfd here if it's not be initialized before
4977 if (!adev->kfd.init_complete)
4978 amdgpu_amdkfd_device_init(adev);
4980 if (audio_suspended)
4981 amdgpu_device_resume_display_audio(tmp_adev);
4982 amdgpu_device_unlock_adev(tmp_adev);
4987 atomic_set(&hive->in_reset, 0);
4988 mutex_unlock(&hive->hive_lock);
4989 amdgpu_put_xgmi_hive(hive);
4992 if (r && r != -EAGAIN)
4993 dev_info(adev->dev, "GPU reset end with ret = %d\n", r);
4998 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
5000 * @adev: amdgpu_device pointer
5002 * Fetchs and stores in the driver the PCIE capabilities (gen speed
5003 * and lanes) of the slot the device is in. Handles APUs and
5004 * virtualized environments where PCIE config space may not be available.
5006 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
5008 struct pci_dev *pdev;
5009 enum pci_bus_speed speed_cap, platform_speed_cap;
5010 enum pcie_link_width platform_link_width;
5012 if (amdgpu_pcie_gen_cap)
5013 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
5015 if (amdgpu_pcie_lane_cap)
5016 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
5018 /* covers APUs as well */
5019 if (pci_is_root_bus(adev->pdev->bus)) {
5020 if (adev->pm.pcie_gen_mask == 0)
5021 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
5022 if (adev->pm.pcie_mlw_mask == 0)
5023 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
5027 if (adev->pm.pcie_gen_mask && adev->pm.pcie_mlw_mask)
5030 pcie_bandwidth_available(adev->pdev, NULL,
5031 &platform_speed_cap, &platform_link_width);
5033 if (adev->pm.pcie_gen_mask == 0) {
5036 speed_cap = pcie_get_speed_cap(pdev);
5037 if (speed_cap == PCI_SPEED_UNKNOWN) {
5038 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5039 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5040 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
5042 if (speed_cap == PCIE_SPEED_32_0GT)
5043 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5044 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5045 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5046 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4 |
5047 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN5);
5048 else if (speed_cap == PCIE_SPEED_16_0GT)
5049 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5050 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5051 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5052 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4);
5053 else if (speed_cap == PCIE_SPEED_8_0GT)
5054 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5055 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5056 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
5057 else if (speed_cap == PCIE_SPEED_5_0GT)
5058 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5059 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2);
5061 adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1;
5064 if (platform_speed_cap == PCI_SPEED_UNKNOWN) {
5065 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5066 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
5068 if (platform_speed_cap == PCIE_SPEED_32_0GT)
5069 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5070 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5071 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5072 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4 |
5073 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN5);
5074 else if (platform_speed_cap == PCIE_SPEED_16_0GT)
5075 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5076 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5077 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5078 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4);
5079 else if (platform_speed_cap == PCIE_SPEED_8_0GT)
5080 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5081 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5082 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3);
5083 else if (platform_speed_cap == PCIE_SPEED_5_0GT)
5084 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5085 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
5087 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
5091 if (adev->pm.pcie_mlw_mask == 0) {
5092 if (platform_link_width == PCIE_LNK_WIDTH_UNKNOWN) {
5093 adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK;
5095 switch (platform_link_width) {
5097 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
5098 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5099 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5100 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5101 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5102 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5103 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5106 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5107 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5108 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5109 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5110 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5111 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5114 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5115 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5116 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5117 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5118 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5121 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5122 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5123 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5124 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5127 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5128 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5129 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5132 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5133 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5136 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
5145 int amdgpu_device_baco_enter(struct drm_device *dev)
5147 struct amdgpu_device *adev = drm_to_adev(dev);
5148 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
5150 if (!amdgpu_device_supports_baco(adev_to_drm(adev)))
5153 if (ras && ras->supported && adev->nbio.funcs->enable_doorbell_interrupt)
5154 adev->nbio.funcs->enable_doorbell_interrupt(adev, false);
5156 return amdgpu_dpm_baco_enter(adev);
5159 int amdgpu_device_baco_exit(struct drm_device *dev)
5161 struct amdgpu_device *adev = drm_to_adev(dev);
5162 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
5165 if (!amdgpu_device_supports_baco(adev_to_drm(adev)))
5168 ret = amdgpu_dpm_baco_exit(adev);
5172 if (ras && ras->supported && adev->nbio.funcs->enable_doorbell_interrupt)
5173 adev->nbio.funcs->enable_doorbell_interrupt(adev, true);
5178 static void amdgpu_cancel_all_tdr(struct amdgpu_device *adev)
5182 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5183 struct amdgpu_ring *ring = adev->rings[i];
5185 if (!ring || !ring->sched.thread)
5188 cancel_delayed_work_sync(&ring->sched.work_tdr);
5193 * amdgpu_pci_error_detected - Called when a PCI error is detected.
5194 * @pdev: PCI device struct
5195 * @state: PCI channel state
5197 * Description: Called when a PCI error is detected.
5199 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT.
5201 pci_ers_result_t amdgpu_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
5203 struct drm_device *dev = pci_get_drvdata(pdev);
5204 struct amdgpu_device *adev = drm_to_adev(dev);
5207 DRM_INFO("PCI error: detected callback, state(%d)!!\n", state);
5209 if (adev->gmc.xgmi.num_physical_nodes > 1) {
5210 DRM_WARN("No support for XGMI hive yet...");
5211 return PCI_ERS_RESULT_DISCONNECT;
5215 case pci_channel_io_normal:
5216 return PCI_ERS_RESULT_CAN_RECOVER;
5217 /* Fatal error, prepare for slot reset */
5218 case pci_channel_io_frozen:
5220 * Cancel and wait for all TDRs in progress if failing to
5221 * set adev->in_gpu_reset in amdgpu_device_lock_adev
5223 * Locking adev->reset_sem will prevent any external access
5224 * to GPU during PCI error recovery
5226 while (!amdgpu_device_lock_adev(adev, NULL))
5227 amdgpu_cancel_all_tdr(adev);
5230 * Block any work scheduling as we do for regular GPU reset
5231 * for the duration of the recovery
5233 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5234 struct amdgpu_ring *ring = adev->rings[i];
5236 if (!ring || !ring->sched.thread)
5239 drm_sched_stop(&ring->sched, NULL);
5241 atomic_inc(&adev->gpu_reset_counter);
5242 return PCI_ERS_RESULT_NEED_RESET;
5243 case pci_channel_io_perm_failure:
5244 /* Permanent error, prepare for device removal */
5245 return PCI_ERS_RESULT_DISCONNECT;
5248 return PCI_ERS_RESULT_NEED_RESET;
5252 * amdgpu_pci_mmio_enabled - Enable MMIO and dump debug registers
5253 * @pdev: pointer to PCI device
5255 pci_ers_result_t amdgpu_pci_mmio_enabled(struct pci_dev *pdev)
5258 DRM_INFO("PCI error: mmio enabled callback!!\n");
5260 /* TODO - dump whatever for debugging purposes */
5262 /* This called only if amdgpu_pci_error_detected returns
5263 * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still
5264 * works, no need to reset slot.
5267 return PCI_ERS_RESULT_RECOVERED;
5271 * amdgpu_pci_slot_reset - Called when PCI slot has been reset.
5272 * @pdev: PCI device struct
5274 * Description: This routine is called by the pci error recovery
5275 * code after the PCI slot has been reset, just before we
5276 * should resume normal operations.
5278 pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev)
5280 struct drm_device *dev = pci_get_drvdata(pdev);
5281 struct amdgpu_device *adev = drm_to_adev(dev);
5283 struct amdgpu_reset_context reset_context;
5285 struct list_head device_list;
5287 DRM_INFO("PCI error: slot reset callback!!\n");
5289 memset(&reset_context, 0, sizeof(reset_context));
5291 INIT_LIST_HEAD(&device_list);
5292 list_add_tail(&adev->reset_list, &device_list);
5294 /* wait for asic to come out of reset */
5297 /* Restore PCI confspace */
5298 amdgpu_device_load_pci_state(pdev);
5300 /* confirm ASIC came out of reset */
5301 for (i = 0; i < adev->usec_timeout; i++) {
5302 memsize = amdgpu_asic_get_config_memsize(adev);
5304 if (memsize != 0xffffffff)
5308 if (memsize == 0xffffffff) {
5313 reset_context.method = AMD_RESET_METHOD_NONE;
5314 reset_context.reset_req_dev = adev;
5315 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
5316 set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
5318 adev->in_pci_err_recovery = true;
5319 r = amdgpu_device_pre_asic_reset(adev, &reset_context);
5320 adev->in_pci_err_recovery = false;
5324 r = amdgpu_do_asic_reset(&device_list, &reset_context);
5328 if (amdgpu_device_cache_pci_state(adev->pdev))
5329 pci_restore_state(adev->pdev);
5331 DRM_INFO("PCIe error recovery succeeded\n");
5333 DRM_ERROR("PCIe error recovery failed, err:%d", r);
5334 amdgpu_device_unlock_adev(adev);
5337 return r ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
5341 * amdgpu_pci_resume() - resume normal ops after PCI reset
5342 * @pdev: pointer to PCI device
5344 * Called when the error recovery driver tells us that its
5345 * OK to resume normal operation.
5347 void amdgpu_pci_resume(struct pci_dev *pdev)
5349 struct drm_device *dev = pci_get_drvdata(pdev);
5350 struct amdgpu_device *adev = drm_to_adev(dev);
5354 DRM_INFO("PCI error: resume callback!!\n");
5356 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5357 struct amdgpu_ring *ring = adev->rings[i];
5359 if (!ring || !ring->sched.thread)
5363 drm_sched_resubmit_jobs(&ring->sched);
5364 drm_sched_start(&ring->sched, true);
5367 amdgpu_device_unlock_adev(adev);
5370 bool amdgpu_device_cache_pci_state(struct pci_dev *pdev)
5372 struct drm_device *dev = pci_get_drvdata(pdev);
5373 struct amdgpu_device *adev = drm_to_adev(dev);
5376 r = pci_save_state(pdev);
5378 kfree(adev->pci_state);
5380 adev->pci_state = pci_store_saved_state(pdev);
5382 if (!adev->pci_state) {
5383 DRM_ERROR("Failed to store PCI saved state");
5387 DRM_WARN("Failed to save PCI state, err:%d\n", r);
5394 bool amdgpu_device_load_pci_state(struct pci_dev *pdev)
5396 struct drm_device *dev = pci_get_drvdata(pdev);
5397 struct amdgpu_device *adev = drm_to_adev(dev);
5400 if (!adev->pci_state)
5403 r = pci_load_saved_state(pdev, adev->pci_state);
5406 pci_restore_state(pdev);
5408 DRM_WARN("Failed to load PCI state, err:%d\n", r);