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 MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
75 MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
76 MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
77 MODULE_FIRMWARE("amdgpu/picasso_gpu_info.bin");
78 MODULE_FIRMWARE("amdgpu/raven2_gpu_info.bin");
79 MODULE_FIRMWARE("amdgpu/arcturus_gpu_info.bin");
80 MODULE_FIRMWARE("amdgpu/renoir_gpu_info.bin");
81 MODULE_FIRMWARE("amdgpu/navi10_gpu_info.bin");
82 MODULE_FIRMWARE("amdgpu/navi14_gpu_info.bin");
83 MODULE_FIRMWARE("amdgpu/navi12_gpu_info.bin");
84 MODULE_FIRMWARE("amdgpu/vangogh_gpu_info.bin");
86 #define AMDGPU_RESUME_MS 2000
88 const char *amdgpu_asic_name[] = {
126 * DOC: pcie_replay_count
128 * The amdgpu driver provides a sysfs API for reporting the total number
129 * of PCIe replays (NAKs)
130 * The file pcie_replay_count is used for this and returns the total
131 * number of replays as a sum of the NAKs generated and NAKs received
134 static ssize_t amdgpu_device_get_pcie_replay_count(struct device *dev,
135 struct device_attribute *attr, char *buf)
137 struct drm_device *ddev = dev_get_drvdata(dev);
138 struct amdgpu_device *adev = drm_to_adev(ddev);
139 uint64_t cnt = amdgpu_asic_get_pcie_replay_count(adev);
141 return sysfs_emit(buf, "%llu\n", cnt);
144 static DEVICE_ATTR(pcie_replay_count, S_IRUGO,
145 amdgpu_device_get_pcie_replay_count, NULL);
147 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev);
152 * The amdgpu driver provides a sysfs API for reporting the product name
154 * The file serial_number is used for this and returns the product name
155 * as returned from the FRU.
156 * NOTE: This is only available for certain server cards
159 static ssize_t amdgpu_device_get_product_name(struct device *dev,
160 struct device_attribute *attr, char *buf)
162 struct drm_device *ddev = dev_get_drvdata(dev);
163 struct amdgpu_device *adev = drm_to_adev(ddev);
165 return sysfs_emit(buf, "%s\n", adev->product_name);
168 static DEVICE_ATTR(product_name, S_IRUGO,
169 amdgpu_device_get_product_name, NULL);
172 * DOC: product_number
174 * The amdgpu driver provides a sysfs API for reporting the part number
176 * The file serial_number is used for this and returns the part number
177 * as returned from the FRU.
178 * NOTE: This is only available for certain server cards
181 static ssize_t amdgpu_device_get_product_number(struct device *dev,
182 struct device_attribute *attr, char *buf)
184 struct drm_device *ddev = dev_get_drvdata(dev);
185 struct amdgpu_device *adev = drm_to_adev(ddev);
187 return sysfs_emit(buf, "%s\n", adev->product_number);
190 static DEVICE_ATTR(product_number, S_IRUGO,
191 amdgpu_device_get_product_number, NULL);
196 * The amdgpu driver provides a sysfs API for reporting the serial number
198 * The file serial_number is used for this and returns the serial number
199 * as returned from the FRU.
200 * NOTE: This is only available for certain server cards
203 static ssize_t amdgpu_device_get_serial_number(struct device *dev,
204 struct device_attribute *attr, char *buf)
206 struct drm_device *ddev = dev_get_drvdata(dev);
207 struct amdgpu_device *adev = drm_to_adev(ddev);
209 return sysfs_emit(buf, "%s\n", adev->serial);
212 static DEVICE_ATTR(serial_number, S_IRUGO,
213 amdgpu_device_get_serial_number, NULL);
216 * amdgpu_device_supports_px - Is the device a dGPU with ATPX power control
218 * @dev: drm_device pointer
220 * Returns true if the device is a dGPU with ATPX power control,
221 * otherwise return false.
223 bool amdgpu_device_supports_px(struct drm_device *dev)
225 struct amdgpu_device *adev = drm_to_adev(dev);
227 if ((adev->flags & AMD_IS_PX) && !amdgpu_is_atpx_hybrid())
233 * amdgpu_device_supports_boco - Is the device a dGPU with ACPI power resources
235 * @dev: drm_device pointer
237 * Returns true if the device is a dGPU with ACPI power control,
238 * otherwise return false.
240 bool amdgpu_device_supports_boco(struct drm_device *dev)
242 struct amdgpu_device *adev = drm_to_adev(dev);
245 ((adev->flags & AMD_IS_PX) && amdgpu_is_atpx_hybrid()))
251 * amdgpu_device_supports_baco - Does the device support BACO
253 * @dev: drm_device pointer
255 * Returns true if the device supporte BACO,
256 * otherwise return false.
258 bool amdgpu_device_supports_baco(struct drm_device *dev)
260 struct amdgpu_device *adev = drm_to_adev(dev);
262 return amdgpu_asic_supports_baco(adev);
266 * VRAM access helper functions
270 * amdgpu_device_vram_access - read/write a buffer in vram
272 * @adev: amdgpu_device pointer
273 * @pos: offset of the buffer in vram
274 * @buf: virtual address of the buffer in system memory
275 * @size: read/write size, sizeof(@buf) must > @size
276 * @write: true - write to vram, otherwise - read from vram
278 void amdgpu_device_vram_access(struct amdgpu_device *adev, loff_t pos,
279 uint32_t *buf, size_t size, bool write)
287 last = min(pos + size, adev->gmc.visible_vram_size);
289 void __iomem *addr = adev->mman.aper_base_kaddr + pos;
290 size_t count = last - pos;
293 memcpy_toio(addr, buf, count);
295 amdgpu_asic_flush_hdp(adev, NULL);
297 amdgpu_asic_invalidate_hdp(adev, NULL);
299 memcpy_fromio(buf, addr, count);
311 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
312 for (last = pos + size; pos < last; pos += 4) {
313 uint32_t tmp = pos >> 31;
315 WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)pos) | 0x80000000);
317 WREG32_NO_KIQ(mmMM_INDEX_HI, tmp);
321 WREG32_NO_KIQ(mmMM_DATA, *buf++);
323 *buf++ = RREG32_NO_KIQ(mmMM_DATA);
325 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
329 * register access helper functions.
332 /* Check if hw access should be skipped because of hotplug or device error */
333 bool amdgpu_device_skip_hw_access(struct amdgpu_device *adev)
335 if (adev->in_pci_err_recovery)
338 #ifdef CONFIG_LOCKDEP
340 * This is a bit complicated to understand, so worth a comment. What we assert
341 * here is that the GPU reset is not running on another thread in parallel.
343 * For this we trylock the read side of the reset semaphore, if that succeeds
344 * we know that the reset is not running in paralell.
346 * If the trylock fails we assert that we are either already holding the read
347 * side of the lock or are the reset thread itself and hold the write side of
351 if (down_read_trylock(&adev->reset_sem))
352 up_read(&adev->reset_sem);
354 lockdep_assert_held(&adev->reset_sem);
361 * amdgpu_device_rreg - read a memory mapped IO or indirect register
363 * @adev: amdgpu_device pointer
364 * @reg: dword aligned register offset
365 * @acc_flags: access flags which require special behavior
367 * Returns the 32 bit value from the offset specified.
369 uint32_t amdgpu_device_rreg(struct amdgpu_device *adev,
370 uint32_t reg, uint32_t acc_flags)
374 if (amdgpu_device_skip_hw_access(adev))
377 if ((reg * 4) < adev->rmmio_size) {
378 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
379 amdgpu_sriov_runtime(adev) &&
380 down_read_trylock(&adev->reset_sem)) {
381 ret = amdgpu_kiq_rreg(adev, reg);
382 up_read(&adev->reset_sem);
384 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
387 ret = adev->pcie_rreg(adev, reg * 4);
390 trace_amdgpu_device_rreg(adev->pdev->device, reg, ret);
396 * MMIO register read with bytes helper functions
397 * @offset:bytes offset from MMIO start
402 * amdgpu_mm_rreg8 - read a memory mapped IO register
404 * @adev: amdgpu_device pointer
405 * @offset: byte aligned register offset
407 * Returns the 8 bit value from the offset specified.
409 uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset)
411 if (amdgpu_device_skip_hw_access(adev))
414 if (offset < adev->rmmio_size)
415 return (readb(adev->rmmio + offset));
420 * MMIO register write with bytes helper functions
421 * @offset:bytes offset from MMIO start
422 * @value: the value want to be written to the register
426 * amdgpu_mm_wreg8 - read a memory mapped IO register
428 * @adev: amdgpu_device pointer
429 * @offset: byte aligned register offset
430 * @value: 8 bit value to write
432 * Writes the value specified to the offset specified.
434 void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value)
436 if (amdgpu_device_skip_hw_access(adev))
439 if (offset < adev->rmmio_size)
440 writeb(value, adev->rmmio + offset);
446 * amdgpu_device_wreg - write to a memory mapped IO or indirect register
448 * @adev: amdgpu_device pointer
449 * @reg: dword aligned register offset
450 * @v: 32 bit value to write to the register
451 * @acc_flags: access flags which require special behavior
453 * Writes the value specified to the offset specified.
455 void amdgpu_device_wreg(struct amdgpu_device *adev,
456 uint32_t reg, uint32_t v,
459 if (amdgpu_device_skip_hw_access(adev))
462 if ((reg * 4) < adev->rmmio_size) {
463 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
464 amdgpu_sriov_runtime(adev) &&
465 down_read_trylock(&adev->reset_sem)) {
466 amdgpu_kiq_wreg(adev, reg, v);
467 up_read(&adev->reset_sem);
469 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
472 adev->pcie_wreg(adev, reg * 4, v);
475 trace_amdgpu_device_wreg(adev->pdev->device, reg, v);
479 * amdgpu_mm_wreg_mmio_rlc - write register either with mmio or with RLC path if in range
481 * this function is invoked only the debugfs register access
483 void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
484 uint32_t reg, uint32_t v)
486 if (amdgpu_device_skip_hw_access(adev))
489 if (amdgpu_sriov_fullaccess(adev) &&
490 adev->gfx.rlc.funcs &&
491 adev->gfx.rlc.funcs->is_rlcg_access_range) {
492 if (adev->gfx.rlc.funcs->is_rlcg_access_range(adev, reg))
493 return adev->gfx.rlc.funcs->rlcg_wreg(adev, reg, v);
495 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
500 * amdgpu_mm_rdoorbell - read a doorbell dword
502 * @adev: amdgpu_device pointer
503 * @index: doorbell index
505 * Returns the value in the doorbell aperture at the
506 * requested doorbell index (CIK).
508 u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
510 if (amdgpu_device_skip_hw_access(adev))
513 if (index < adev->doorbell.num_doorbells) {
514 return readl(adev->doorbell.ptr + index);
516 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
522 * amdgpu_mm_wdoorbell - write a doorbell dword
524 * @adev: amdgpu_device pointer
525 * @index: doorbell index
528 * Writes @v to the doorbell aperture at the
529 * requested doorbell index (CIK).
531 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
533 if (amdgpu_device_skip_hw_access(adev))
536 if (index < adev->doorbell.num_doorbells) {
537 writel(v, adev->doorbell.ptr + index);
539 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
544 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
546 * @adev: amdgpu_device pointer
547 * @index: doorbell index
549 * Returns the value in the doorbell aperture at the
550 * requested doorbell index (VEGA10+).
552 u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
554 if (amdgpu_device_skip_hw_access(adev))
557 if (index < adev->doorbell.num_doorbells) {
558 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
560 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
566 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
568 * @adev: amdgpu_device pointer
569 * @index: doorbell index
572 * Writes @v to the doorbell aperture at the
573 * requested doorbell index (VEGA10+).
575 void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
577 if (amdgpu_device_skip_hw_access(adev))
580 if (index < adev->doorbell.num_doorbells) {
581 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
583 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
588 * amdgpu_device_indirect_rreg - read an indirect register
590 * @adev: amdgpu_device pointer
591 * @pcie_index: mmio register offset
592 * @pcie_data: mmio register offset
593 * @reg_addr: indirect register address to read from
595 * Returns the value of indirect register @reg_addr
597 u32 amdgpu_device_indirect_rreg(struct amdgpu_device *adev,
598 u32 pcie_index, u32 pcie_data,
603 void __iomem *pcie_index_offset;
604 void __iomem *pcie_data_offset;
606 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
607 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
608 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
610 writel(reg_addr, pcie_index_offset);
611 readl(pcie_index_offset);
612 r = readl(pcie_data_offset);
613 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
619 * amdgpu_device_indirect_rreg64 - read a 64bits indirect register
621 * @adev: amdgpu_device pointer
622 * @pcie_index: mmio register offset
623 * @pcie_data: mmio register offset
624 * @reg_addr: indirect register address to read from
626 * Returns the value of indirect register @reg_addr
628 u64 amdgpu_device_indirect_rreg64(struct amdgpu_device *adev,
629 u32 pcie_index, u32 pcie_data,
634 void __iomem *pcie_index_offset;
635 void __iomem *pcie_data_offset;
637 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
638 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
639 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
641 /* read low 32 bits */
642 writel(reg_addr, pcie_index_offset);
643 readl(pcie_index_offset);
644 r = readl(pcie_data_offset);
645 /* read high 32 bits */
646 writel(reg_addr + 4, pcie_index_offset);
647 readl(pcie_index_offset);
648 r |= ((u64)readl(pcie_data_offset) << 32);
649 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
655 * amdgpu_device_indirect_wreg - write an indirect register address
657 * @adev: amdgpu_device pointer
658 * @pcie_index: mmio register offset
659 * @pcie_data: mmio register offset
660 * @reg_addr: indirect register offset
661 * @reg_data: indirect register data
664 void amdgpu_device_indirect_wreg(struct amdgpu_device *adev,
665 u32 pcie_index, u32 pcie_data,
666 u32 reg_addr, u32 reg_data)
669 void __iomem *pcie_index_offset;
670 void __iomem *pcie_data_offset;
672 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
673 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
674 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
676 writel(reg_addr, pcie_index_offset);
677 readl(pcie_index_offset);
678 writel(reg_data, pcie_data_offset);
679 readl(pcie_data_offset);
680 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
684 * amdgpu_device_indirect_wreg64 - write a 64bits indirect register address
686 * @adev: amdgpu_device pointer
687 * @pcie_index: mmio register offset
688 * @pcie_data: mmio register offset
689 * @reg_addr: indirect register offset
690 * @reg_data: indirect register data
693 void amdgpu_device_indirect_wreg64(struct amdgpu_device *adev,
694 u32 pcie_index, u32 pcie_data,
695 u32 reg_addr, u64 reg_data)
698 void __iomem *pcie_index_offset;
699 void __iomem *pcie_data_offset;
701 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
702 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
703 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
705 /* write low 32 bits */
706 writel(reg_addr, pcie_index_offset);
707 readl(pcie_index_offset);
708 writel((u32)(reg_data & 0xffffffffULL), pcie_data_offset);
709 readl(pcie_data_offset);
710 /* write high 32 bits */
711 writel(reg_addr + 4, pcie_index_offset);
712 readl(pcie_index_offset);
713 writel((u32)(reg_data >> 32), pcie_data_offset);
714 readl(pcie_data_offset);
715 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
719 * amdgpu_invalid_rreg - dummy reg read function
721 * @adev: amdgpu_device pointer
722 * @reg: offset of register
724 * Dummy register read function. Used for register blocks
725 * that certain asics don't have (all asics).
726 * Returns the value in the register.
728 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
730 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
736 * amdgpu_invalid_wreg - dummy reg write function
738 * @adev: amdgpu_device pointer
739 * @reg: offset of register
740 * @v: value to write to the register
742 * Dummy register read function. Used for register blocks
743 * that certain asics don't have (all asics).
745 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
747 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
753 * amdgpu_invalid_rreg64 - dummy 64 bit reg read function
755 * @adev: amdgpu_device pointer
756 * @reg: offset of register
758 * Dummy register read function. Used for register blocks
759 * that certain asics don't have (all asics).
760 * Returns the value in the register.
762 static uint64_t amdgpu_invalid_rreg64(struct amdgpu_device *adev, uint32_t reg)
764 DRM_ERROR("Invalid callback to read 64 bit register 0x%04X\n", reg);
770 * amdgpu_invalid_wreg64 - dummy reg write function
772 * @adev: amdgpu_device pointer
773 * @reg: offset of register
774 * @v: value to write to the register
776 * Dummy register read function. Used for register blocks
777 * that certain asics don't have (all asics).
779 static void amdgpu_invalid_wreg64(struct amdgpu_device *adev, uint32_t reg, uint64_t v)
781 DRM_ERROR("Invalid callback to write 64 bit register 0x%04X with 0x%08llX\n",
787 * amdgpu_block_invalid_rreg - dummy reg read function
789 * @adev: amdgpu_device pointer
790 * @block: offset of instance
791 * @reg: offset of register
793 * Dummy register read function. Used for register blocks
794 * that certain asics don't have (all asics).
795 * Returns the value in the register.
797 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
798 uint32_t block, uint32_t reg)
800 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
807 * amdgpu_block_invalid_wreg - dummy reg write function
809 * @adev: amdgpu_device pointer
810 * @block: offset of instance
811 * @reg: offset of register
812 * @v: value to write to the register
814 * Dummy register read function. Used for register blocks
815 * that certain asics don't have (all asics).
817 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
819 uint32_t reg, uint32_t v)
821 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
827 * amdgpu_device_asic_init - Wrapper for atom asic_init
829 * @adev: amdgpu_device pointer
831 * Does any asic specific work and then calls atom asic init.
833 static int amdgpu_device_asic_init(struct amdgpu_device *adev)
835 amdgpu_asic_pre_asic_init(adev);
837 return amdgpu_atom_asic_init(adev->mode_info.atom_context);
841 * amdgpu_device_vram_scratch_init - allocate the VRAM scratch page
843 * @adev: amdgpu_device pointer
845 * Allocates a scratch page of VRAM for use by various things in the
848 static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev)
850 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
851 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
852 &adev->vram_scratch.robj,
853 &adev->vram_scratch.gpu_addr,
854 (void **)&adev->vram_scratch.ptr);
858 * amdgpu_device_vram_scratch_fini - Free the VRAM scratch page
860 * @adev: amdgpu_device pointer
862 * Frees the VRAM scratch page.
864 static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev)
866 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
870 * amdgpu_device_program_register_sequence - program an array of registers.
872 * @adev: amdgpu_device pointer
873 * @registers: pointer to the register array
874 * @array_size: size of the register array
876 * Programs an array or registers with and and or masks.
877 * This is a helper for setting golden registers.
879 void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
880 const u32 *registers,
881 const u32 array_size)
883 u32 tmp, reg, and_mask, or_mask;
889 for (i = 0; i < array_size; i +=3) {
890 reg = registers[i + 0];
891 and_mask = registers[i + 1];
892 or_mask = registers[i + 2];
894 if (and_mask == 0xffffffff) {
899 if (adev->family >= AMDGPU_FAMILY_AI)
900 tmp |= (or_mask & and_mask);
909 * amdgpu_device_pci_config_reset - reset the GPU
911 * @adev: amdgpu_device pointer
913 * Resets the GPU using the pci config reset sequence.
914 * Only applicable to asics prior to vega10.
916 void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
918 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
922 * amdgpu_device_pci_reset - reset the GPU using generic PCI means
924 * @adev: amdgpu_device pointer
926 * Resets the GPU using generic pci reset interfaces (FLR, SBR, etc.).
928 int amdgpu_device_pci_reset(struct amdgpu_device *adev)
930 return pci_reset_function(adev->pdev);
934 * GPU doorbell aperture helpers function.
937 * amdgpu_device_doorbell_init - Init doorbell driver information.
939 * @adev: amdgpu_device pointer
941 * Init doorbell driver information (CIK)
942 * Returns 0 on success, error on failure.
944 static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
947 /* No doorbell on SI hardware generation */
948 if (adev->asic_type < CHIP_BONAIRE) {
949 adev->doorbell.base = 0;
950 adev->doorbell.size = 0;
951 adev->doorbell.num_doorbells = 0;
952 adev->doorbell.ptr = NULL;
956 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
959 amdgpu_asic_init_doorbell_index(adev);
961 /* doorbell bar mapping */
962 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
963 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
965 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
966 adev->doorbell_index.max_assignment+1);
967 if (adev->doorbell.num_doorbells == 0)
970 /* For Vega, reserve and map two pages on doorbell BAR since SDMA
971 * paging queue doorbell use the second page. The
972 * AMDGPU_DOORBELL64_MAX_ASSIGNMENT definition assumes all the
973 * doorbells are in the first page. So with paging queue enabled,
974 * the max num_doorbells should + 1 page (0x400 in dword)
976 if (adev->asic_type >= CHIP_VEGA10)
977 adev->doorbell.num_doorbells += 0x400;
979 adev->doorbell.ptr = ioremap(adev->doorbell.base,
980 adev->doorbell.num_doorbells *
982 if (adev->doorbell.ptr == NULL)
989 * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
991 * @adev: amdgpu_device pointer
993 * Tear down doorbell driver information (CIK)
995 static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
997 iounmap(adev->doorbell.ptr);
998 adev->doorbell.ptr = NULL;
1004 * amdgpu_device_wb_*()
1005 * Writeback is the method by which the GPU updates special pages in memory
1006 * with the status of certain GPU events (fences, ring pointers,etc.).
1010 * amdgpu_device_wb_fini - Disable Writeback and free memory
1012 * @adev: amdgpu_device pointer
1014 * Disables Writeback and frees the Writeback memory (all asics).
1015 * Used at driver shutdown.
1017 static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
1019 if (adev->wb.wb_obj) {
1020 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
1022 (void **)&adev->wb.wb);
1023 adev->wb.wb_obj = NULL;
1028 * amdgpu_device_wb_init- Init Writeback driver info and allocate memory
1030 * @adev: amdgpu_device pointer
1032 * Initializes writeback and allocates writeback memory (all asics).
1033 * Used at driver startup.
1034 * Returns 0 on success or an -error on failure.
1036 static int amdgpu_device_wb_init(struct amdgpu_device *adev)
1040 if (adev->wb.wb_obj == NULL) {
1041 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
1042 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
1043 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
1044 &adev->wb.wb_obj, &adev->wb.gpu_addr,
1045 (void **)&adev->wb.wb);
1047 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
1051 adev->wb.num_wb = AMDGPU_MAX_WB;
1052 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
1054 /* clear wb memory */
1055 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
1062 * amdgpu_device_wb_get - Allocate a wb entry
1064 * @adev: amdgpu_device pointer
1067 * Allocate a wb slot for use by the driver (all asics).
1068 * Returns 0 on success or -EINVAL on failure.
1070 int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
1072 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
1074 if (offset < adev->wb.num_wb) {
1075 __set_bit(offset, adev->wb.used);
1076 *wb = offset << 3; /* convert to dw offset */
1084 * amdgpu_device_wb_free - Free a wb entry
1086 * @adev: amdgpu_device pointer
1089 * Free a wb slot allocated for use by the driver (all asics)
1091 void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
1094 if (wb < adev->wb.num_wb)
1095 __clear_bit(wb, adev->wb.used);
1099 * amdgpu_device_resize_fb_bar - try to resize FB BAR
1101 * @adev: amdgpu_device pointer
1103 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
1104 * to fail, but if any of the BARs is not accessible after the size we abort
1105 * driver loading by returning -ENODEV.
1107 int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
1109 int rbar_size = pci_rebar_bytes_to_size(adev->gmc.real_vram_size);
1110 struct pci_bus *root;
1111 struct resource *res;
1117 if (amdgpu_sriov_vf(adev))
1120 /* skip if the bios has already enabled large BAR */
1121 if (adev->gmc.real_vram_size &&
1122 (pci_resource_len(adev->pdev, 0) >= adev->gmc.real_vram_size))
1125 /* Check if the root BUS has 64bit memory resources */
1126 root = adev->pdev->bus;
1127 while (root->parent)
1128 root = root->parent;
1130 pci_bus_for_each_resource(root, res, i) {
1131 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
1132 res->start > 0x100000000ull)
1136 /* Trying to resize is pointless without a root hub window above 4GB */
1140 /* Limit the BAR size to what is available */
1141 rbar_size = min(fls(pci_rebar_get_possible_sizes(adev->pdev, 0)) - 1,
1144 /* Disable memory decoding while we change the BAR addresses and size */
1145 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
1146 pci_write_config_word(adev->pdev, PCI_COMMAND,
1147 cmd & ~PCI_COMMAND_MEMORY);
1149 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
1150 amdgpu_device_doorbell_fini(adev);
1151 if (adev->asic_type >= CHIP_BONAIRE)
1152 pci_release_resource(adev->pdev, 2);
1154 pci_release_resource(adev->pdev, 0);
1156 r = pci_resize_resource(adev->pdev, 0, rbar_size);
1158 DRM_INFO("Not enough PCI address space for a large BAR.");
1159 else if (r && r != -ENOTSUPP)
1160 DRM_ERROR("Problem resizing BAR0 (%d).", r);
1162 pci_assign_unassigned_bus_resources(adev->pdev->bus);
1164 /* When the doorbell or fb BAR isn't available we have no chance of
1167 r = amdgpu_device_doorbell_init(adev);
1168 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
1171 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
1177 * GPU helpers function.
1180 * amdgpu_device_need_post - check if the hw need post or not
1182 * @adev: amdgpu_device pointer
1184 * Check if the asic has been initialized (all asics) at driver startup
1185 * or post is needed if hw reset is performed.
1186 * Returns true if need or false if not.
1188 bool amdgpu_device_need_post(struct amdgpu_device *adev)
1192 if (amdgpu_sriov_vf(adev))
1195 if (amdgpu_passthrough(adev)) {
1196 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
1197 * some old smc fw still need driver do vPost otherwise gpu hang, while
1198 * those smc fw version above 22.15 doesn't have this flaw, so we force
1199 * vpost executed for smc version below 22.15
1201 if (adev->asic_type == CHIP_FIJI) {
1204 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
1205 /* force vPost if error occured */
1209 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
1210 if (fw_ver < 0x00160e00)
1215 /* Don't post if we need to reset whole hive on init */
1216 if (adev->gmc.xgmi.pending_reset)
1219 if (adev->has_hw_reset) {
1220 adev->has_hw_reset = false;
1224 /* bios scratch used on CIK+ */
1225 if (adev->asic_type >= CHIP_BONAIRE)
1226 return amdgpu_atombios_scratch_need_asic_init(adev);
1228 /* check MEM_SIZE for older asics */
1229 reg = amdgpu_asic_get_config_memsize(adev);
1231 if ((reg != 0) && (reg != 0xffffffff))
1237 /* if we get transitioned to only one device, take VGA back */
1239 * amdgpu_device_vga_set_decode - enable/disable vga decode
1241 * @cookie: amdgpu_device pointer
1242 * @state: enable/disable vga decode
1244 * Enable/disable vga decode (all asics).
1245 * Returns VGA resource flags.
1247 static unsigned int amdgpu_device_vga_set_decode(void *cookie, bool state)
1249 struct amdgpu_device *adev = cookie;
1250 amdgpu_asic_set_vga_state(adev, state);
1252 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1253 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1255 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1259 * amdgpu_device_check_block_size - validate the vm block size
1261 * @adev: amdgpu_device pointer
1263 * Validates the vm block size specified via module parameter.
1264 * The vm block size defines number of bits in page table versus page directory,
1265 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1266 * page table and the remaining bits are in the page directory.
1268 static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
1270 /* defines number of bits in page table versus page directory,
1271 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1272 * page table and the remaining bits are in the page directory */
1273 if (amdgpu_vm_block_size == -1)
1276 if (amdgpu_vm_block_size < 9) {
1277 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1278 amdgpu_vm_block_size);
1279 amdgpu_vm_block_size = -1;
1284 * amdgpu_device_check_vm_size - validate the vm size
1286 * @adev: amdgpu_device pointer
1288 * Validates the vm size in GB specified via module parameter.
1289 * The VM size is the size of the GPU virtual memory space in GB.
1291 static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
1293 /* no need to check the default value */
1294 if (amdgpu_vm_size == -1)
1297 if (amdgpu_vm_size < 1) {
1298 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1300 amdgpu_vm_size = -1;
1304 static void amdgpu_device_check_smu_prv_buffer_size(struct amdgpu_device *adev)
1307 bool is_os_64 = (sizeof(void *) == 8);
1308 uint64_t total_memory;
1309 uint64_t dram_size_seven_GB = 0x1B8000000;
1310 uint64_t dram_size_three_GB = 0xB8000000;
1312 if (amdgpu_smu_memory_pool_size == 0)
1316 DRM_WARN("Not 64-bit OS, feature not supported\n");
1320 total_memory = (uint64_t)si.totalram * si.mem_unit;
1322 if ((amdgpu_smu_memory_pool_size == 1) ||
1323 (amdgpu_smu_memory_pool_size == 2)) {
1324 if (total_memory < dram_size_three_GB)
1326 } else if ((amdgpu_smu_memory_pool_size == 4) ||
1327 (amdgpu_smu_memory_pool_size == 8)) {
1328 if (total_memory < dram_size_seven_GB)
1331 DRM_WARN("Smu memory pool size not supported\n");
1334 adev->pm.smu_prv_buffer_size = amdgpu_smu_memory_pool_size << 28;
1339 DRM_WARN("No enough system memory\n");
1341 adev->pm.smu_prv_buffer_size = 0;
1345 * amdgpu_device_check_arguments - validate module params
1347 * @adev: amdgpu_device pointer
1349 * Validates certain module parameters and updates
1350 * the associated values used by the driver (all asics).
1352 static int amdgpu_device_check_arguments(struct amdgpu_device *adev)
1354 if (amdgpu_sched_jobs < 4) {
1355 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1357 amdgpu_sched_jobs = 4;
1358 } else if (!is_power_of_2(amdgpu_sched_jobs)){
1359 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1361 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1364 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
1365 /* gart size must be greater or equal to 32M */
1366 dev_warn(adev->dev, "gart size (%d) too small\n",
1368 amdgpu_gart_size = -1;
1371 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
1372 /* gtt size must be greater or equal to 32M */
1373 dev_warn(adev->dev, "gtt size (%d) too small\n",
1375 amdgpu_gtt_size = -1;
1378 /* valid range is between 4 and 9 inclusive */
1379 if (amdgpu_vm_fragment_size != -1 &&
1380 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1381 dev_warn(adev->dev, "valid range is between 4 and 9\n");
1382 amdgpu_vm_fragment_size = -1;
1385 if (amdgpu_sched_hw_submission < 2) {
1386 dev_warn(adev->dev, "sched hw submission jobs (%d) must be at least 2\n",
1387 amdgpu_sched_hw_submission);
1388 amdgpu_sched_hw_submission = 2;
1389 } else if (!is_power_of_2(amdgpu_sched_hw_submission)) {
1390 dev_warn(adev->dev, "sched hw submission jobs (%d) must be a power of 2\n",
1391 amdgpu_sched_hw_submission);
1392 amdgpu_sched_hw_submission = roundup_pow_of_two(amdgpu_sched_hw_submission);
1395 amdgpu_device_check_smu_prv_buffer_size(adev);
1397 amdgpu_device_check_vm_size(adev);
1399 amdgpu_device_check_block_size(adev);
1401 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
1403 amdgpu_gmc_tmz_set(adev);
1405 amdgpu_gmc_noretry_set(adev);
1411 * amdgpu_switcheroo_set_state - set switcheroo state
1413 * @pdev: pci dev pointer
1414 * @state: vga_switcheroo state
1416 * Callback for the switcheroo driver. Suspends or resumes the
1417 * the asics before or after it is powered up using ACPI methods.
1419 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev,
1420 enum vga_switcheroo_state state)
1422 struct drm_device *dev = pci_get_drvdata(pdev);
1425 if (amdgpu_device_supports_px(dev) && state == VGA_SWITCHEROO_OFF)
1428 if (state == VGA_SWITCHEROO_ON) {
1429 pr_info("switched on\n");
1430 /* don't suspend or resume card normally */
1431 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1433 pci_set_power_state(pdev, PCI_D0);
1434 amdgpu_device_load_pci_state(pdev);
1435 r = pci_enable_device(pdev);
1437 DRM_WARN("pci_enable_device failed (%d)\n", r);
1438 amdgpu_device_resume(dev, true);
1440 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1442 pr_info("switched off\n");
1443 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1444 amdgpu_device_suspend(dev, true);
1445 amdgpu_device_cache_pci_state(pdev);
1446 /* Shut down the device */
1447 pci_disable_device(pdev);
1448 pci_set_power_state(pdev, PCI_D3cold);
1449 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1454 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1456 * @pdev: pci dev pointer
1458 * Callback for the switcheroo driver. Check of the switcheroo
1459 * state can be changed.
1460 * Returns true if the state can be changed, false if not.
1462 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1464 struct drm_device *dev = pci_get_drvdata(pdev);
1467 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1468 * locking inversion with the driver load path. And the access here is
1469 * completely racy anyway. So don't bother with locking for now.
1471 return atomic_read(&dev->open_count) == 0;
1474 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1475 .set_gpu_state = amdgpu_switcheroo_set_state,
1477 .can_switch = amdgpu_switcheroo_can_switch,
1481 * amdgpu_device_ip_set_clockgating_state - set the CG state
1483 * @dev: amdgpu_device pointer
1484 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1485 * @state: clockgating state (gate or ungate)
1487 * Sets the requested clockgating state for all instances of
1488 * the hardware IP specified.
1489 * Returns the error code from the last instance.
1491 int amdgpu_device_ip_set_clockgating_state(void *dev,
1492 enum amd_ip_block_type block_type,
1493 enum amd_clockgating_state state)
1495 struct amdgpu_device *adev = dev;
1498 for (i = 0; i < adev->num_ip_blocks; i++) {
1499 if (!adev->ip_blocks[i].status.valid)
1501 if (adev->ip_blocks[i].version->type != block_type)
1503 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1505 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1506 (void *)adev, state);
1508 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1509 adev->ip_blocks[i].version->funcs->name, r);
1515 * amdgpu_device_ip_set_powergating_state - set the PG state
1517 * @dev: amdgpu_device pointer
1518 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1519 * @state: powergating state (gate or ungate)
1521 * Sets the requested powergating state for all instances of
1522 * the hardware IP specified.
1523 * Returns the error code from the last instance.
1525 int amdgpu_device_ip_set_powergating_state(void *dev,
1526 enum amd_ip_block_type block_type,
1527 enum amd_powergating_state state)
1529 struct amdgpu_device *adev = dev;
1532 for (i = 0; i < adev->num_ip_blocks; i++) {
1533 if (!adev->ip_blocks[i].status.valid)
1535 if (adev->ip_blocks[i].version->type != block_type)
1537 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1539 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1540 (void *)adev, state);
1542 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1543 adev->ip_blocks[i].version->funcs->name, r);
1549 * amdgpu_device_ip_get_clockgating_state - get the CG state
1551 * @adev: amdgpu_device pointer
1552 * @flags: clockgating feature flags
1554 * Walks the list of IPs on the device and updates the clockgating
1555 * flags for each IP.
1556 * Updates @flags with the feature flags for each hardware IP where
1557 * clockgating is enabled.
1559 void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
1564 for (i = 0; i < adev->num_ip_blocks; i++) {
1565 if (!adev->ip_blocks[i].status.valid)
1567 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1568 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1573 * amdgpu_device_ip_wait_for_idle - wait for idle
1575 * @adev: amdgpu_device pointer
1576 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1578 * Waits for the request hardware IP to be idle.
1579 * Returns 0 for success or a negative error code on failure.
1581 int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1582 enum amd_ip_block_type block_type)
1586 for (i = 0; i < adev->num_ip_blocks; i++) {
1587 if (!adev->ip_blocks[i].status.valid)
1589 if (adev->ip_blocks[i].version->type == block_type) {
1590 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
1601 * amdgpu_device_ip_is_idle - is the hardware IP idle
1603 * @adev: amdgpu_device pointer
1604 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1606 * Check if the hardware IP is idle or not.
1607 * Returns true if it the IP is idle, false if not.
1609 bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1610 enum amd_ip_block_type block_type)
1614 for (i = 0; i < adev->num_ip_blocks; i++) {
1615 if (!adev->ip_blocks[i].status.valid)
1617 if (adev->ip_blocks[i].version->type == block_type)
1618 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
1625 * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1627 * @adev: amdgpu_device pointer
1628 * @type: Type of hardware IP (SMU, GFX, UVD, etc.)
1630 * Returns a pointer to the hardware IP block structure
1631 * if it exists for the asic, otherwise NULL.
1633 struct amdgpu_ip_block *
1634 amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1635 enum amd_ip_block_type type)
1639 for (i = 0; i < adev->num_ip_blocks; i++)
1640 if (adev->ip_blocks[i].version->type == type)
1641 return &adev->ip_blocks[i];
1647 * amdgpu_device_ip_block_version_cmp
1649 * @adev: amdgpu_device pointer
1650 * @type: enum amd_ip_block_type
1651 * @major: major version
1652 * @minor: minor version
1654 * return 0 if equal or greater
1655 * return 1 if smaller or the ip_block doesn't exist
1657 int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1658 enum amd_ip_block_type type,
1659 u32 major, u32 minor)
1661 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
1663 if (ip_block && ((ip_block->version->major > major) ||
1664 ((ip_block->version->major == major) &&
1665 (ip_block->version->minor >= minor))))
1672 * amdgpu_device_ip_block_add
1674 * @adev: amdgpu_device pointer
1675 * @ip_block_version: pointer to the IP to add
1677 * Adds the IP block driver information to the collection of IPs
1680 int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1681 const struct amdgpu_ip_block_version *ip_block_version)
1683 if (!ip_block_version)
1686 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
1687 ip_block_version->funcs->name);
1689 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1695 * amdgpu_device_enable_virtual_display - enable virtual display feature
1697 * @adev: amdgpu_device pointer
1699 * Enabled the virtual display feature if the user has enabled it via
1700 * the module parameter virtual_display. This feature provides a virtual
1701 * display hardware on headless boards or in virtualized environments.
1702 * This function parses and validates the configuration string specified by
1703 * the user and configues the virtual display configuration (number of
1704 * virtual connectors, crtcs, etc.) specified.
1706 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
1708 adev->enable_virtual_display = false;
1710 if (amdgpu_virtual_display) {
1711 const char *pci_address_name = pci_name(adev->pdev);
1712 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
1714 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1715 pciaddstr_tmp = pciaddstr;
1716 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1717 pciaddname = strsep(&pciaddname_tmp, ",");
1718 if (!strcmp("all", pciaddname)
1719 || !strcmp(pci_address_name, pciaddname)) {
1723 adev->enable_virtual_display = true;
1726 res = kstrtol(pciaddname_tmp, 10,
1734 adev->mode_info.num_crtc = num_crtc;
1736 adev->mode_info.num_crtc = 1;
1742 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1743 amdgpu_virtual_display, pci_address_name,
1744 adev->enable_virtual_display, adev->mode_info.num_crtc);
1751 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
1753 * @adev: amdgpu_device pointer
1755 * Parses the asic configuration parameters specified in the gpu info
1756 * firmware and makes them availale to the driver for use in configuring
1758 * Returns 0 on success, -EINVAL on failure.
1760 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1762 const char *chip_name;
1765 const struct gpu_info_firmware_header_v1_0 *hdr;
1767 adev->firmware.gpu_info_fw = NULL;
1769 if (adev->mman.discovery_bin) {
1770 amdgpu_discovery_get_gfx_info(adev);
1773 * FIXME: The bounding box is still needed by Navi12, so
1774 * temporarily read it from gpu_info firmware. Should be droped
1775 * when DAL no longer needs it.
1777 if (adev->asic_type != CHIP_NAVI12)
1781 switch (adev->asic_type) {
1782 #ifdef CONFIG_DRM_AMDGPU_SI
1789 #ifdef CONFIG_DRM_AMDGPU_CIK
1799 case CHIP_POLARIS10:
1800 case CHIP_POLARIS11:
1801 case CHIP_POLARIS12:
1806 case CHIP_ALDEBARAN:
1807 case CHIP_SIENNA_CICHLID:
1808 case CHIP_NAVY_FLOUNDER:
1809 case CHIP_DIMGREY_CAVEFISH:
1813 chip_name = "vega10";
1816 chip_name = "vega12";
1819 if (adev->apu_flags & AMD_APU_IS_RAVEN2)
1820 chip_name = "raven2";
1821 else if (adev->apu_flags & AMD_APU_IS_PICASSO)
1822 chip_name = "picasso";
1824 chip_name = "raven";
1827 chip_name = "arcturus";
1830 if (adev->apu_flags & AMD_APU_IS_RENOIR)
1831 chip_name = "renoir";
1833 chip_name = "green_sardine";
1836 chip_name = "navi10";
1839 chip_name = "navi14";
1842 chip_name = "navi12";
1845 chip_name = "vangogh";
1849 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
1850 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
1853 "Failed to load gpu_info firmware \"%s\"\n",
1857 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
1860 "Failed to validate gpu_info firmware \"%s\"\n",
1865 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
1866 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1868 switch (hdr->version_major) {
1871 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
1872 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
1873 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1876 * Should be droped when DAL no longer needs it.
1878 if (adev->asic_type == CHIP_NAVI12)
1879 goto parse_soc_bounding_box;
1881 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1882 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1883 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1884 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
1885 adev->gfx.config.max_texture_channel_caches =
1886 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1887 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1888 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1889 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1890 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
1891 adev->gfx.config.double_offchip_lds_buf =
1892 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1893 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
1894 adev->gfx.cu_info.max_waves_per_simd =
1895 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1896 adev->gfx.cu_info.max_scratch_slots_per_cu =
1897 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1898 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
1899 if (hdr->version_minor >= 1) {
1900 const struct gpu_info_firmware_v1_1 *gpu_info_fw =
1901 (const struct gpu_info_firmware_v1_1 *)(adev->firmware.gpu_info_fw->data +
1902 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1903 adev->gfx.config.num_sc_per_sh =
1904 le32_to_cpu(gpu_info_fw->num_sc_per_sh);
1905 adev->gfx.config.num_packer_per_sc =
1906 le32_to_cpu(gpu_info_fw->num_packer_per_sc);
1909 parse_soc_bounding_box:
1911 * soc bounding box info is not integrated in disocovery table,
1912 * we always need to parse it from gpu info firmware if needed.
1914 if (hdr->version_minor == 2) {
1915 const struct gpu_info_firmware_v1_2 *gpu_info_fw =
1916 (const struct gpu_info_firmware_v1_2 *)(adev->firmware.gpu_info_fw->data +
1917 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1918 adev->dm.soc_bounding_box = &gpu_info_fw->soc_bounding_box;
1924 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1933 * amdgpu_device_ip_early_init - run early init for hardware IPs
1935 * @adev: amdgpu_device pointer
1937 * Early initialization pass for hardware IPs. The hardware IPs that make
1938 * up each asic are discovered each IP's early_init callback is run. This
1939 * is the first stage in initializing the asic.
1940 * Returns 0 on success, negative error code on failure.
1942 static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
1946 amdgpu_device_enable_virtual_display(adev);
1948 if (amdgpu_sriov_vf(adev)) {
1949 r = amdgpu_virt_request_full_gpu(adev, true);
1954 switch (adev->asic_type) {
1955 #ifdef CONFIG_DRM_AMDGPU_SI
1961 adev->family = AMDGPU_FAMILY_SI;
1962 r = si_set_ip_blocks(adev);
1967 #ifdef CONFIG_DRM_AMDGPU_CIK
1973 if (adev->flags & AMD_IS_APU)
1974 adev->family = AMDGPU_FAMILY_KV;
1976 adev->family = AMDGPU_FAMILY_CI;
1978 r = cik_set_ip_blocks(adev);
1986 case CHIP_POLARIS10:
1987 case CHIP_POLARIS11:
1988 case CHIP_POLARIS12:
1992 if (adev->flags & AMD_IS_APU)
1993 adev->family = AMDGPU_FAMILY_CZ;
1995 adev->family = AMDGPU_FAMILY_VI;
1997 r = vi_set_ip_blocks(adev);
2007 case CHIP_ALDEBARAN:
2008 if (adev->flags & AMD_IS_APU)
2009 adev->family = AMDGPU_FAMILY_RV;
2011 adev->family = AMDGPU_FAMILY_AI;
2013 r = soc15_set_ip_blocks(adev);
2020 case CHIP_SIENNA_CICHLID:
2021 case CHIP_NAVY_FLOUNDER:
2022 case CHIP_DIMGREY_CAVEFISH:
2024 if (adev->asic_type == CHIP_VANGOGH)
2025 adev->family = AMDGPU_FAMILY_VGH;
2027 adev->family = AMDGPU_FAMILY_NV;
2029 r = nv_set_ip_blocks(adev);
2034 /* FIXME: not supported yet */
2038 amdgpu_amdkfd_device_probe(adev);
2040 adev->pm.pp_feature = amdgpu_pp_feature_mask;
2041 if (amdgpu_sriov_vf(adev) || sched_policy == KFD_SCHED_POLICY_NO_HWS)
2042 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
2043 if (amdgpu_sriov_vf(adev) && adev->asic_type == CHIP_SIENNA_CICHLID)
2044 adev->pm.pp_feature &= ~PP_OVERDRIVE_MASK;
2046 for (i = 0; i < adev->num_ip_blocks; i++) {
2047 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
2048 DRM_ERROR("disabled ip block: %d <%s>\n",
2049 i, adev->ip_blocks[i].version->funcs->name);
2050 adev->ip_blocks[i].status.valid = false;
2052 if (adev->ip_blocks[i].version->funcs->early_init) {
2053 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
2055 adev->ip_blocks[i].status.valid = false;
2057 DRM_ERROR("early_init of IP block <%s> failed %d\n",
2058 adev->ip_blocks[i].version->funcs->name, r);
2061 adev->ip_blocks[i].status.valid = true;
2064 adev->ip_blocks[i].status.valid = true;
2067 /* get the vbios after the asic_funcs are set up */
2068 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) {
2069 r = amdgpu_device_parse_gpu_info_fw(adev);
2074 if (!amdgpu_get_bios(adev))
2077 r = amdgpu_atombios_init(adev);
2079 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
2080 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
2086 adev->cg_flags &= amdgpu_cg_mask;
2087 adev->pg_flags &= amdgpu_pg_mask;
2092 static int amdgpu_device_ip_hw_init_phase1(struct amdgpu_device *adev)
2096 for (i = 0; i < adev->num_ip_blocks; i++) {
2097 if (!adev->ip_blocks[i].status.sw)
2099 if (adev->ip_blocks[i].status.hw)
2101 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2102 (amdgpu_sriov_vf(adev) && (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)) ||
2103 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
2104 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2106 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2107 adev->ip_blocks[i].version->funcs->name, r);
2110 adev->ip_blocks[i].status.hw = true;
2117 static int amdgpu_device_ip_hw_init_phase2(struct amdgpu_device *adev)
2121 for (i = 0; i < adev->num_ip_blocks; i++) {
2122 if (!adev->ip_blocks[i].status.sw)
2124 if (adev->ip_blocks[i].status.hw)
2126 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2128 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2129 adev->ip_blocks[i].version->funcs->name, r);
2132 adev->ip_blocks[i].status.hw = true;
2138 static int amdgpu_device_fw_loading(struct amdgpu_device *adev)
2142 uint32_t smu_version;
2144 if (adev->asic_type >= CHIP_VEGA10) {
2145 for (i = 0; i < adev->num_ip_blocks; i++) {
2146 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_PSP)
2149 if (!adev->ip_blocks[i].status.sw)
2152 /* no need to do the fw loading again if already done*/
2153 if (adev->ip_blocks[i].status.hw == true)
2156 if (amdgpu_in_reset(adev) || adev->in_suspend) {
2157 r = adev->ip_blocks[i].version->funcs->resume(adev);
2159 DRM_ERROR("resume of IP block <%s> failed %d\n",
2160 adev->ip_blocks[i].version->funcs->name, r);
2164 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2166 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2167 adev->ip_blocks[i].version->funcs->name, r);
2172 adev->ip_blocks[i].status.hw = true;
2177 if (!amdgpu_sriov_vf(adev) || adev->asic_type == CHIP_TONGA)
2178 r = amdgpu_pm_load_smu_firmware(adev, &smu_version);
2184 * amdgpu_device_ip_init - run init for hardware IPs
2186 * @adev: amdgpu_device pointer
2188 * Main initialization pass for hardware IPs. The list of all the hardware
2189 * IPs that make up the asic is walked and the sw_init and hw_init callbacks
2190 * are run. sw_init initializes the software state associated with each IP
2191 * and hw_init initializes the hardware associated with each IP.
2192 * Returns 0 on success, negative error code on failure.
2194 static int amdgpu_device_ip_init(struct amdgpu_device *adev)
2198 r = amdgpu_ras_init(adev);
2202 for (i = 0; i < adev->num_ip_blocks; i++) {
2203 if (!adev->ip_blocks[i].status.valid)
2205 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
2207 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
2208 adev->ip_blocks[i].version->funcs->name, r);
2211 adev->ip_blocks[i].status.sw = true;
2213 /* need to do gmc hw init early so we can allocate gpu mem */
2214 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
2215 r = amdgpu_device_vram_scratch_init(adev);
2217 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
2220 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
2222 DRM_ERROR("hw_init %d failed %d\n", i, r);
2225 r = amdgpu_device_wb_init(adev);
2227 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
2230 adev->ip_blocks[i].status.hw = true;
2232 /* right after GMC hw init, we create CSA */
2233 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) {
2234 r = amdgpu_allocate_static_csa(adev, &adev->virt.csa_obj,
2235 AMDGPU_GEM_DOMAIN_VRAM,
2238 DRM_ERROR("allocate CSA failed %d\n", r);
2245 if (amdgpu_sriov_vf(adev))
2246 amdgpu_virt_init_data_exchange(adev);
2248 r = amdgpu_ib_pool_init(adev);
2250 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
2251 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
2255 r = amdgpu_ucode_create_bo(adev); /* create ucode bo when sw_init complete*/
2259 r = amdgpu_device_ip_hw_init_phase1(adev);
2263 r = amdgpu_device_fw_loading(adev);
2267 r = amdgpu_device_ip_hw_init_phase2(adev);
2272 * retired pages will be loaded from eeprom and reserved here,
2273 * it should be called after amdgpu_device_ip_hw_init_phase2 since
2274 * for some ASICs the RAS EEPROM code relies on SMU fully functioning
2275 * for I2C communication which only true at this point.
2277 * amdgpu_ras_recovery_init may fail, but the upper only cares the
2278 * failure from bad gpu situation and stop amdgpu init process
2279 * accordingly. For other failed cases, it will still release all
2280 * the resource and print error message, rather than returning one
2281 * negative value to upper level.
2283 * Note: theoretically, this should be called before all vram allocations
2284 * to protect retired page from abusing
2286 r = amdgpu_ras_recovery_init(adev);
2290 if (adev->gmc.xgmi.num_physical_nodes > 1)
2291 amdgpu_xgmi_add_device(adev);
2293 /* Don't init kfd if whole hive need to be reset during init */
2294 if (!adev->gmc.xgmi.pending_reset)
2295 amdgpu_amdkfd_device_init(adev);
2297 amdgpu_fru_get_product_info(adev);
2300 if (amdgpu_sriov_vf(adev))
2301 amdgpu_virt_release_full_gpu(adev, true);
2307 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
2309 * @adev: amdgpu_device pointer
2311 * Writes a reset magic value to the gart pointer in VRAM. The driver calls
2312 * this function before a GPU reset. If the value is retained after a
2313 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
2315 static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
2317 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
2321 * amdgpu_device_check_vram_lost - check if vram is valid
2323 * @adev: amdgpu_device pointer
2325 * Checks the reset magic value written to the gart pointer in VRAM.
2326 * The driver calls this after a GPU reset to see if the contents of
2327 * VRAM is lost or now.
2328 * returns true if vram is lost, false if not.
2330 static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
2332 if (memcmp(adev->gart.ptr, adev->reset_magic,
2333 AMDGPU_RESET_MAGIC_NUM))
2336 if (!amdgpu_in_reset(adev))
2340 * For all ASICs with baco/mode1 reset, the VRAM is
2341 * always assumed to be lost.
2343 switch (amdgpu_asic_reset_method(adev)) {
2344 case AMD_RESET_METHOD_BACO:
2345 case AMD_RESET_METHOD_MODE1:
2353 * amdgpu_device_set_cg_state - set clockgating for amdgpu device
2355 * @adev: amdgpu_device pointer
2356 * @state: clockgating state (gate or ungate)
2358 * The list of all the hardware IPs that make up the asic is walked and the
2359 * set_clockgating_state callbacks are run.
2360 * Late initialization pass enabling clockgating for hardware IPs.
2361 * Fini or suspend, pass disabling clockgating for hardware IPs.
2362 * Returns 0 on success, negative error code on failure.
2365 static int amdgpu_device_set_cg_state(struct amdgpu_device *adev,
2366 enum amd_clockgating_state state)
2370 if (amdgpu_emu_mode == 1)
2373 for (j = 0; j < adev->num_ip_blocks; j++) {
2374 i = state == AMD_CG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
2375 if (!adev->ip_blocks[i].status.late_initialized)
2377 /* skip CG for GFX on S0ix */
2378 if (adev->in_s0ix &&
2379 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX)
2381 /* skip CG for VCE/UVD, it's handled specially */
2382 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
2383 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
2384 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
2385 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
2386 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
2387 /* enable clockgating to save power */
2388 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
2391 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
2392 adev->ip_blocks[i].version->funcs->name, r);
2401 static int amdgpu_device_set_pg_state(struct amdgpu_device *adev, enum amd_powergating_state state)
2405 if (amdgpu_emu_mode == 1)
2408 for (j = 0; j < adev->num_ip_blocks; j++) {
2409 i = state == AMD_PG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
2410 if (!adev->ip_blocks[i].status.late_initialized)
2412 /* skip PG for GFX on S0ix */
2413 if (adev->in_s0ix &&
2414 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX)
2416 /* skip CG for VCE/UVD, it's handled specially */
2417 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
2418 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
2419 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
2420 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
2421 adev->ip_blocks[i].version->funcs->set_powergating_state) {
2422 /* enable powergating to save power */
2423 r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
2426 DRM_ERROR("set_powergating_state(gate) of IP block <%s> failed %d\n",
2427 adev->ip_blocks[i].version->funcs->name, r);
2435 static int amdgpu_device_enable_mgpu_fan_boost(void)
2437 struct amdgpu_gpu_instance *gpu_ins;
2438 struct amdgpu_device *adev;
2441 mutex_lock(&mgpu_info.mutex);
2444 * MGPU fan boost feature should be enabled
2445 * only when there are two or more dGPUs in
2448 if (mgpu_info.num_dgpu < 2)
2451 for (i = 0; i < mgpu_info.num_dgpu; i++) {
2452 gpu_ins = &(mgpu_info.gpu_ins[i]);
2453 adev = gpu_ins->adev;
2454 if (!(adev->flags & AMD_IS_APU) &&
2455 !gpu_ins->mgpu_fan_enabled) {
2456 ret = amdgpu_dpm_enable_mgpu_fan_boost(adev);
2460 gpu_ins->mgpu_fan_enabled = 1;
2465 mutex_unlock(&mgpu_info.mutex);
2471 * amdgpu_device_ip_late_init - run late init for hardware IPs
2473 * @adev: amdgpu_device pointer
2475 * Late initialization pass for hardware IPs. The list of all the hardware
2476 * IPs that make up the asic is walked and the late_init callbacks are run.
2477 * late_init covers any special initialization that an IP requires
2478 * after all of the have been initialized or something that needs to happen
2479 * late in the init process.
2480 * Returns 0 on success, negative error code on failure.
2482 static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
2484 struct amdgpu_gpu_instance *gpu_instance;
2487 for (i = 0; i < adev->num_ip_blocks; i++) {
2488 if (!adev->ip_blocks[i].status.hw)
2490 if (adev->ip_blocks[i].version->funcs->late_init) {
2491 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
2493 DRM_ERROR("late_init of IP block <%s> failed %d\n",
2494 adev->ip_blocks[i].version->funcs->name, r);
2498 adev->ip_blocks[i].status.late_initialized = true;
2501 amdgpu_ras_set_error_query_ready(adev, true);
2503 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE);
2504 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_GATE);
2506 amdgpu_device_fill_reset_magic(adev);
2508 r = amdgpu_device_enable_mgpu_fan_boost();
2510 DRM_ERROR("enable mgpu fan boost failed (%d).\n", r);
2512 /* For XGMI + passthrough configuration on arcturus, enable light SBR */
2513 if (adev->asic_type == CHIP_ARCTURUS &&
2514 amdgpu_passthrough(adev) &&
2515 adev->gmc.xgmi.num_physical_nodes > 1)
2516 smu_set_light_sbr(&adev->smu, true);
2518 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2519 mutex_lock(&mgpu_info.mutex);
2522 * Reset device p-state to low as this was booted with high.
2524 * This should be performed only after all devices from the same
2525 * hive get initialized.
2527 * However, it's unknown how many device in the hive in advance.
2528 * As this is counted one by one during devices initializations.
2530 * So, we wait for all XGMI interlinked devices initialized.
2531 * This may bring some delays as those devices may come from
2532 * different hives. But that should be OK.
2534 if (mgpu_info.num_dgpu == adev->gmc.xgmi.num_physical_nodes) {
2535 for (i = 0; i < mgpu_info.num_gpu; i++) {
2536 gpu_instance = &(mgpu_info.gpu_ins[i]);
2537 if (gpu_instance->adev->flags & AMD_IS_APU)
2540 r = amdgpu_xgmi_set_pstate(gpu_instance->adev,
2541 AMDGPU_XGMI_PSTATE_MIN);
2543 DRM_ERROR("pstate setting failed (%d).\n", r);
2549 mutex_unlock(&mgpu_info.mutex);
2556 * amdgpu_device_ip_fini - run fini for hardware IPs
2558 * @adev: amdgpu_device pointer
2560 * Main teardown pass for hardware IPs. The list of all the hardware
2561 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
2562 * are run. hw_fini tears down the hardware associated with each IP
2563 * and sw_fini tears down any software state associated with each IP.
2564 * Returns 0 on success, negative error code on failure.
2566 static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
2570 if (amdgpu_sriov_vf(adev) && adev->virt.ras_init_done)
2571 amdgpu_virt_release_ras_err_handler_data(adev);
2573 amdgpu_ras_pre_fini(adev);
2575 if (adev->gmc.xgmi.num_physical_nodes > 1)
2576 amdgpu_xgmi_remove_device(adev);
2578 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2579 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2581 amdgpu_amdkfd_device_fini(adev);
2583 /* need to disable SMC first */
2584 for (i = 0; i < adev->num_ip_blocks; i++) {
2585 if (!adev->ip_blocks[i].status.hw)
2587 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
2588 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2589 /* XXX handle errors */
2591 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2592 adev->ip_blocks[i].version->funcs->name, r);
2594 adev->ip_blocks[i].status.hw = false;
2599 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2600 if (!adev->ip_blocks[i].status.hw)
2603 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2604 /* XXX handle errors */
2606 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2607 adev->ip_blocks[i].version->funcs->name, r);
2610 adev->ip_blocks[i].status.hw = false;
2614 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2615 if (!adev->ip_blocks[i].status.sw)
2618 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
2619 amdgpu_ucode_free_bo(adev);
2620 amdgpu_free_static_csa(&adev->virt.csa_obj);
2621 amdgpu_device_wb_fini(adev);
2622 amdgpu_device_vram_scratch_fini(adev);
2623 amdgpu_ib_pool_fini(adev);
2626 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
2627 /* XXX handle errors */
2629 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
2630 adev->ip_blocks[i].version->funcs->name, r);
2632 adev->ip_blocks[i].status.sw = false;
2633 adev->ip_blocks[i].status.valid = false;
2636 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2637 if (!adev->ip_blocks[i].status.late_initialized)
2639 if (adev->ip_blocks[i].version->funcs->late_fini)
2640 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
2641 adev->ip_blocks[i].status.late_initialized = false;
2644 amdgpu_ras_fini(adev);
2646 if (amdgpu_sriov_vf(adev))
2647 if (amdgpu_virt_release_full_gpu(adev, false))
2648 DRM_ERROR("failed to release exclusive mode on fini\n");
2654 * amdgpu_device_delayed_init_work_handler - work handler for IB tests
2656 * @work: work_struct.
2658 static void amdgpu_device_delayed_init_work_handler(struct work_struct *work)
2660 struct amdgpu_device *adev =
2661 container_of(work, struct amdgpu_device, delayed_init_work.work);
2664 r = amdgpu_ib_ring_tests(adev);
2666 DRM_ERROR("ib ring test failed (%d).\n", r);
2669 static void amdgpu_device_delay_enable_gfx_off(struct work_struct *work)
2671 struct amdgpu_device *adev =
2672 container_of(work, struct amdgpu_device, gfx.gfx_off_delay_work.work);
2674 mutex_lock(&adev->gfx.gfx_off_mutex);
2675 if (!adev->gfx.gfx_off_state && !adev->gfx.gfx_off_req_count) {
2676 if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true))
2677 adev->gfx.gfx_off_state = true;
2679 mutex_unlock(&adev->gfx.gfx_off_mutex);
2683 * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1)
2685 * @adev: amdgpu_device pointer
2687 * Main suspend function for hardware IPs. The list of all the hardware
2688 * IPs that make up the asic is walked, clockgating is disabled and the
2689 * suspend callbacks are run. suspend puts the hardware and software state
2690 * in each IP into a state suitable for suspend.
2691 * Returns 0 on success, negative error code on failure.
2693 static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev)
2697 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2698 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2700 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2701 if (!adev->ip_blocks[i].status.valid)
2704 /* displays are handled separately */
2705 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_DCE)
2708 /* XXX handle errors */
2709 r = adev->ip_blocks[i].version->funcs->suspend(adev);
2710 /* XXX handle errors */
2712 DRM_ERROR("suspend of IP block <%s> failed %d\n",
2713 adev->ip_blocks[i].version->funcs->name, r);
2717 adev->ip_blocks[i].status.hw = false;
2724 * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2)
2726 * @adev: amdgpu_device pointer
2728 * Main suspend function for hardware IPs. The list of all the hardware
2729 * IPs that make up the asic is walked, clockgating is disabled and the
2730 * suspend callbacks are run. suspend puts the hardware and software state
2731 * in each IP into a state suitable for suspend.
2732 * Returns 0 on success, negative error code on failure.
2734 static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev)
2739 amdgpu_gfx_state_change_set(adev, sGpuChangeState_D3Entry);
2741 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2742 if (!adev->ip_blocks[i].status.valid)
2744 /* displays are handled in phase1 */
2745 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)
2747 /* PSP lost connection when err_event_athub occurs */
2748 if (amdgpu_ras_intr_triggered() &&
2749 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
2750 adev->ip_blocks[i].status.hw = false;
2754 /* skip unnecessary suspend if we do not initialize them yet */
2755 if (adev->gmc.xgmi.pending_reset &&
2756 !(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2757 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC ||
2758 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2759 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)) {
2760 adev->ip_blocks[i].status.hw = false;
2764 /* skip suspend of gfx and psp for S0ix
2765 * gfx is in gfxoff state, so on resume it will exit gfxoff just
2766 * like at runtime. PSP is also part of the always on hardware
2767 * so no need to suspend it.
2769 if (adev->in_s0ix &&
2770 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP ||
2771 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX))
2774 /* XXX handle errors */
2775 r = adev->ip_blocks[i].version->funcs->suspend(adev);
2776 /* XXX handle errors */
2778 DRM_ERROR("suspend of IP block <%s> failed %d\n",
2779 adev->ip_blocks[i].version->funcs->name, r);
2781 adev->ip_blocks[i].status.hw = false;
2782 /* handle putting the SMC in the appropriate state */
2783 if(!amdgpu_sriov_vf(adev)){
2784 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
2785 r = amdgpu_dpm_set_mp1_state(adev, adev->mp1_state);
2787 DRM_ERROR("SMC failed to set mp1 state %d, %d\n",
2788 adev->mp1_state, r);
2799 * amdgpu_device_ip_suspend - run suspend for hardware IPs
2801 * @adev: amdgpu_device pointer
2803 * Main suspend function for hardware IPs. The list of all the hardware
2804 * IPs that make up the asic is walked, clockgating is disabled and the
2805 * suspend callbacks are run. suspend puts the hardware and software state
2806 * in each IP into a state suitable for suspend.
2807 * Returns 0 on success, negative error code on failure.
2809 int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
2813 if (amdgpu_sriov_vf(adev)) {
2814 amdgpu_virt_fini_data_exchange(adev);
2815 amdgpu_virt_request_full_gpu(adev, false);
2818 r = amdgpu_device_ip_suspend_phase1(adev);
2821 r = amdgpu_device_ip_suspend_phase2(adev);
2823 if (amdgpu_sriov_vf(adev))
2824 amdgpu_virt_release_full_gpu(adev, false);
2829 static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
2833 static enum amd_ip_block_type ip_order[] = {
2834 AMD_IP_BLOCK_TYPE_GMC,
2835 AMD_IP_BLOCK_TYPE_COMMON,
2836 AMD_IP_BLOCK_TYPE_PSP,
2837 AMD_IP_BLOCK_TYPE_IH,
2840 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
2842 struct amdgpu_ip_block *block;
2844 block = &adev->ip_blocks[i];
2845 block->status.hw = false;
2847 for (j = 0; j < ARRAY_SIZE(ip_order); j++) {
2849 if (block->version->type != ip_order[j] ||
2850 !block->status.valid)
2853 r = block->version->funcs->hw_init(adev);
2854 DRM_INFO("RE-INIT-early: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
2857 block->status.hw = true;
2864 static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
2868 static enum amd_ip_block_type ip_order[] = {
2869 AMD_IP_BLOCK_TYPE_SMC,
2870 AMD_IP_BLOCK_TYPE_DCE,
2871 AMD_IP_BLOCK_TYPE_GFX,
2872 AMD_IP_BLOCK_TYPE_SDMA,
2873 AMD_IP_BLOCK_TYPE_UVD,
2874 AMD_IP_BLOCK_TYPE_VCE,
2875 AMD_IP_BLOCK_TYPE_VCN
2878 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
2880 struct amdgpu_ip_block *block;
2882 for (j = 0; j < adev->num_ip_blocks; j++) {
2883 block = &adev->ip_blocks[j];
2885 if (block->version->type != ip_order[i] ||
2886 !block->status.valid ||
2890 if (block->version->type == AMD_IP_BLOCK_TYPE_SMC)
2891 r = block->version->funcs->resume(adev);
2893 r = block->version->funcs->hw_init(adev);
2895 DRM_INFO("RE-INIT-late: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
2898 block->status.hw = true;
2906 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
2908 * @adev: amdgpu_device pointer
2910 * First resume function for hardware IPs. The list of all the hardware
2911 * IPs that make up the asic is walked and the resume callbacks are run for
2912 * COMMON, GMC, and IH. resume puts the hardware into a functional state
2913 * after a suspend and updates the software state as necessary. This
2914 * function is also used for restoring the GPU after a GPU reset.
2915 * Returns 0 on success, negative error code on failure.
2917 static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
2921 for (i = 0; i < adev->num_ip_blocks; i++) {
2922 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
2924 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2925 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2926 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
2928 r = adev->ip_blocks[i].version->funcs->resume(adev);
2930 DRM_ERROR("resume of IP block <%s> failed %d\n",
2931 adev->ip_blocks[i].version->funcs->name, r);
2934 adev->ip_blocks[i].status.hw = true;
2942 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
2944 * @adev: amdgpu_device pointer
2946 * First resume function for hardware IPs. The list of all the hardware
2947 * IPs that make up the asic is walked and the resume callbacks are run for
2948 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
2949 * functional state after a suspend and updates the software state as
2950 * necessary. This function is also used for restoring the GPU after a GPU
2952 * Returns 0 on success, negative error code on failure.
2954 static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
2958 for (i = 0; i < adev->num_ip_blocks; i++) {
2959 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
2961 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2962 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2963 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
2964 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)
2966 r = adev->ip_blocks[i].version->funcs->resume(adev);
2968 DRM_ERROR("resume of IP block <%s> failed %d\n",
2969 adev->ip_blocks[i].version->funcs->name, r);
2972 adev->ip_blocks[i].status.hw = true;
2979 * amdgpu_device_ip_resume - run resume for hardware IPs
2981 * @adev: amdgpu_device pointer
2983 * Main resume function for hardware IPs. The hardware IPs
2984 * are split into two resume functions because they are
2985 * are also used in in recovering from a GPU reset and some additional
2986 * steps need to be take between them. In this case (S3/S4) they are
2988 * Returns 0 on success, negative error code on failure.
2990 static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
2994 r = amdgpu_device_ip_resume_phase1(adev);
2998 r = amdgpu_device_fw_loading(adev);
3002 r = amdgpu_device_ip_resume_phase2(adev);
3008 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
3010 * @adev: amdgpu_device pointer
3012 * Query the VBIOS data tables to determine if the board supports SR-IOV.
3014 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
3016 if (amdgpu_sriov_vf(adev)) {
3017 if (adev->is_atom_fw) {
3018 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
3019 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
3021 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
3022 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
3025 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
3026 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
3031 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
3033 * @asic_type: AMD asic type
3035 * Check if there is DC (new modesetting infrastructre) support for an asic.
3036 * returns true if DC has support, false if not.
3038 bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
3040 switch (asic_type) {
3041 #if defined(CONFIG_DRM_AMD_DC)
3042 #if defined(CONFIG_DRM_AMD_DC_SI)
3053 * We have systems in the wild with these ASICs that require
3054 * LVDS and VGA support which is not supported with DC.
3056 * Fallback to the non-DC driver here by default so as not to
3057 * cause regressions.
3059 return amdgpu_dc > 0;
3063 case CHIP_POLARIS10:
3064 case CHIP_POLARIS11:
3065 case CHIP_POLARIS12:
3072 #if defined(CONFIG_DRM_AMD_DC_DCN)
3078 case CHIP_SIENNA_CICHLID:
3079 case CHIP_NAVY_FLOUNDER:
3080 case CHIP_DIMGREY_CAVEFISH:
3083 return amdgpu_dc != 0;
3087 DRM_INFO_ONCE("Display Core has been requested via kernel parameter "
3088 "but isn't supported by ASIC, ignoring\n");
3094 * amdgpu_device_has_dc_support - check if dc is supported
3096 * @adev: amdgpu_device pointer
3098 * Returns true for supported, false for not supported
3100 bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
3102 if (amdgpu_sriov_vf(adev) || adev->enable_virtual_display)
3105 return amdgpu_device_asic_has_dc_support(adev->asic_type);
3109 static void amdgpu_device_xgmi_reset_func(struct work_struct *__work)
3111 struct amdgpu_device *adev =
3112 container_of(__work, struct amdgpu_device, xgmi_reset_work);
3113 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
3115 /* It's a bug to not have a hive within this function */
3120 * Use task barrier to synchronize all xgmi reset works across the
3121 * hive. task_barrier_enter and task_barrier_exit will block
3122 * until all the threads running the xgmi reset works reach
3123 * those points. task_barrier_full will do both blocks.
3125 if (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) {
3127 task_barrier_enter(&hive->tb);
3128 adev->asic_reset_res = amdgpu_device_baco_enter(adev_to_drm(adev));
3130 if (adev->asic_reset_res)
3133 task_barrier_exit(&hive->tb);
3134 adev->asic_reset_res = amdgpu_device_baco_exit(adev_to_drm(adev));
3136 if (adev->asic_reset_res)
3139 if (adev->mmhub.funcs && adev->mmhub.funcs->reset_ras_error_count)
3140 adev->mmhub.funcs->reset_ras_error_count(adev);
3143 task_barrier_full(&hive->tb);
3144 adev->asic_reset_res = amdgpu_asic_reset(adev);
3148 if (adev->asic_reset_res)
3149 DRM_WARN("ASIC reset failed with error, %d for drm dev, %s",
3150 adev->asic_reset_res, adev_to_drm(adev)->unique);
3151 amdgpu_put_xgmi_hive(hive);
3154 static int amdgpu_device_get_job_timeout_settings(struct amdgpu_device *adev)
3156 char *input = amdgpu_lockup_timeout;
3157 char *timeout_setting = NULL;
3163 * By default timeout for non compute jobs is 10000.
3164 * And there is no timeout enforced on compute jobs.
3165 * In SR-IOV or passthrough mode, timeout for compute
3166 * jobs are 60000 by default.
3168 adev->gfx_timeout = msecs_to_jiffies(10000);
3169 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
3170 if (amdgpu_sriov_vf(adev))
3171 adev->compute_timeout = amdgpu_sriov_is_pp_one_vf(adev) ?
3172 msecs_to_jiffies(60000) : msecs_to_jiffies(10000);
3173 else if (amdgpu_passthrough(adev))
3174 adev->compute_timeout = msecs_to_jiffies(60000);
3176 adev->compute_timeout = MAX_SCHEDULE_TIMEOUT;
3178 if (strnlen(input, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
3179 while ((timeout_setting = strsep(&input, ",")) &&
3180 strnlen(timeout_setting, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
3181 ret = kstrtol(timeout_setting, 0, &timeout);
3188 } else if (timeout < 0) {
3189 timeout = MAX_SCHEDULE_TIMEOUT;
3191 timeout = msecs_to_jiffies(timeout);
3196 adev->gfx_timeout = timeout;
3199 adev->compute_timeout = timeout;
3202 adev->sdma_timeout = timeout;
3205 adev->video_timeout = timeout;
3212 * There is only one value specified and
3213 * it should apply to all non-compute jobs.
3216 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
3217 if (amdgpu_sriov_vf(adev) || amdgpu_passthrough(adev))
3218 adev->compute_timeout = adev->gfx_timeout;
3225 static const struct attribute *amdgpu_dev_attributes[] = {
3226 &dev_attr_product_name.attr,
3227 &dev_attr_product_number.attr,
3228 &dev_attr_serial_number.attr,
3229 &dev_attr_pcie_replay_count.attr,
3235 * amdgpu_device_init - initialize the driver
3237 * @adev: amdgpu_device pointer
3238 * @flags: driver flags
3240 * Initializes the driver info and hw (all asics).
3241 * Returns 0 for success or an error on failure.
3242 * Called at driver startup.
3244 int amdgpu_device_init(struct amdgpu_device *adev,
3247 struct drm_device *ddev = adev_to_drm(adev);
3248 struct pci_dev *pdev = adev->pdev;
3253 adev->shutdown = false;
3254 adev->flags = flags;
3256 if (amdgpu_force_asic_type >= 0 && amdgpu_force_asic_type < CHIP_LAST)
3257 adev->asic_type = amdgpu_force_asic_type;
3259 adev->asic_type = flags & AMD_ASIC_MASK;
3261 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
3262 if (amdgpu_emu_mode == 1)
3263 adev->usec_timeout *= 10;
3264 adev->gmc.gart_size = 512 * 1024 * 1024;
3265 adev->accel_working = false;
3266 adev->num_rings = 0;
3267 adev->mman.buffer_funcs = NULL;
3268 adev->mman.buffer_funcs_ring = NULL;
3269 adev->vm_manager.vm_pte_funcs = NULL;
3270 adev->vm_manager.vm_pte_num_scheds = 0;
3271 adev->gmc.gmc_funcs = NULL;
3272 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
3273 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
3275 adev->smc_rreg = &amdgpu_invalid_rreg;
3276 adev->smc_wreg = &amdgpu_invalid_wreg;
3277 adev->pcie_rreg = &amdgpu_invalid_rreg;
3278 adev->pcie_wreg = &amdgpu_invalid_wreg;
3279 adev->pciep_rreg = &amdgpu_invalid_rreg;
3280 adev->pciep_wreg = &amdgpu_invalid_wreg;
3281 adev->pcie_rreg64 = &amdgpu_invalid_rreg64;
3282 adev->pcie_wreg64 = &amdgpu_invalid_wreg64;
3283 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
3284 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
3285 adev->didt_rreg = &amdgpu_invalid_rreg;
3286 adev->didt_wreg = &amdgpu_invalid_wreg;
3287 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
3288 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
3289 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
3290 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
3292 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
3293 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
3294 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
3296 /* mutex initialization are all done here so we
3297 * can recall function without having locking issues */
3298 mutex_init(&adev->firmware.mutex);
3299 mutex_init(&adev->pm.mutex);
3300 mutex_init(&adev->gfx.gpu_clock_mutex);
3301 mutex_init(&adev->srbm_mutex);
3302 mutex_init(&adev->gfx.pipe_reserve_mutex);
3303 mutex_init(&adev->gfx.gfx_off_mutex);
3304 mutex_init(&adev->grbm_idx_mutex);
3305 mutex_init(&adev->mn_lock);
3306 mutex_init(&adev->virt.vf_errors.lock);
3307 hash_init(adev->mn_hash);
3308 atomic_set(&adev->in_gpu_reset, 0);
3309 init_rwsem(&adev->reset_sem);
3310 mutex_init(&adev->psp.mutex);
3311 mutex_init(&adev->notifier_lock);
3313 r = amdgpu_device_check_arguments(adev);
3317 spin_lock_init(&adev->mmio_idx_lock);
3318 spin_lock_init(&adev->smc_idx_lock);
3319 spin_lock_init(&adev->pcie_idx_lock);
3320 spin_lock_init(&adev->uvd_ctx_idx_lock);
3321 spin_lock_init(&adev->didt_idx_lock);
3322 spin_lock_init(&adev->gc_cac_idx_lock);
3323 spin_lock_init(&adev->se_cac_idx_lock);
3324 spin_lock_init(&adev->audio_endpt_idx_lock);
3325 spin_lock_init(&adev->mm_stats.lock);
3327 INIT_LIST_HEAD(&adev->shadow_list);
3328 mutex_init(&adev->shadow_list_lock);
3330 INIT_LIST_HEAD(&adev->reset_list);
3332 INIT_DELAYED_WORK(&adev->delayed_init_work,
3333 amdgpu_device_delayed_init_work_handler);
3334 INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work,
3335 amdgpu_device_delay_enable_gfx_off);
3337 INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
3339 adev->gfx.gfx_off_req_count = 1;
3340 adev->pm.ac_power = power_supply_is_system_supplied() > 0;
3342 atomic_set(&adev->throttling_logging_enabled, 1);
3344 * If throttling continues, logging will be performed every minute
3345 * to avoid log flooding. "-1" is subtracted since the thermal
3346 * throttling interrupt comes every second. Thus, the total logging
3347 * interval is 59 seconds(retelimited printk interval) + 1(waiting
3348 * for throttling interrupt) = 60 seconds.
3350 ratelimit_state_init(&adev->throttling_logging_rs, (60 - 1) * HZ, 1);
3351 ratelimit_set_flags(&adev->throttling_logging_rs, RATELIMIT_MSG_ON_RELEASE);
3353 /* Registers mapping */
3354 /* TODO: block userspace mapping of io register */
3355 if (adev->asic_type >= CHIP_BONAIRE) {
3356 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
3357 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
3359 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
3360 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
3363 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
3364 if (adev->rmmio == NULL) {
3367 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
3368 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
3370 /* enable PCIE atomic ops */
3371 r = pci_enable_atomic_ops_to_root(adev->pdev,
3372 PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
3373 PCI_EXP_DEVCAP2_ATOMIC_COMP64);
3375 adev->have_atomics_support = false;
3376 DRM_INFO("PCIE atomic ops is not supported\n");
3378 adev->have_atomics_support = true;
3381 amdgpu_device_get_pcie_info(adev);
3384 DRM_INFO("MCBP is enabled\n");
3386 if (amdgpu_mes && adev->asic_type >= CHIP_NAVI10)
3387 adev->enable_mes = true;
3389 /* detect hw virtualization here */
3390 amdgpu_detect_virtualization(adev);
3392 r = amdgpu_device_get_job_timeout_settings(adev);
3394 dev_err(adev->dev, "invalid lockup_timeout parameter syntax\n");
3398 /* early init functions */
3399 r = amdgpu_device_ip_early_init(adev);
3403 /* doorbell bar mapping and doorbell index init*/
3404 amdgpu_device_doorbell_init(adev);
3406 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
3407 /* this will fail for cards that aren't VGA class devices, just
3409 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
3410 vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
3412 if (amdgpu_device_supports_px(ddev)) {
3414 vga_switcheroo_register_client(adev->pdev,
3415 &amdgpu_switcheroo_ops, px);
3416 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
3419 if (amdgpu_emu_mode == 1) {
3420 /* post the asic on emulation mode */
3421 emu_soc_asic_init(adev);
3422 goto fence_driver_init;
3425 amdgpu_reset_init(adev);
3427 /* detect if we are with an SRIOV vbios */
3428 amdgpu_device_detect_sriov_bios(adev);
3430 /* check if we need to reset the asic
3431 * E.g., driver was not cleanly unloaded previously, etc.
3433 if (!amdgpu_sriov_vf(adev) && amdgpu_asic_need_reset_on_init(adev)) {
3434 if (adev->gmc.xgmi.num_physical_nodes) {
3435 dev_info(adev->dev, "Pending hive reset.\n");
3436 adev->gmc.xgmi.pending_reset = true;
3437 /* Only need to init necessary block for SMU to handle the reset */
3438 for (i = 0; i < adev->num_ip_blocks; i++) {
3439 if (!adev->ip_blocks[i].status.valid)
3441 if (!(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3442 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3443 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3444 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC)) {
3445 DRM_DEBUG("IP %s disabled for hw_init.\n",
3446 adev->ip_blocks[i].version->funcs->name);
3447 adev->ip_blocks[i].status.hw = true;
3451 r = amdgpu_asic_reset(adev);
3453 dev_err(adev->dev, "asic reset on init failed\n");
3459 pci_enable_pcie_error_reporting(adev->pdev);
3461 /* Post card if necessary */
3462 if (amdgpu_device_need_post(adev)) {
3464 dev_err(adev->dev, "no vBIOS found\n");
3468 DRM_INFO("GPU posting now...\n");
3469 r = amdgpu_device_asic_init(adev);
3471 dev_err(adev->dev, "gpu post error!\n");
3476 if (adev->is_atom_fw) {
3477 /* Initialize clocks */
3478 r = amdgpu_atomfirmware_get_clock_info(adev);
3480 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
3481 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3485 /* Initialize clocks */
3486 r = amdgpu_atombios_get_clock_info(adev);
3488 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
3489 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3492 /* init i2c buses */
3493 if (!amdgpu_device_has_dc_support(adev))
3494 amdgpu_atombios_i2c_init(adev);
3499 r = amdgpu_fence_driver_init(adev);
3501 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
3502 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
3506 /* init the mode config */
3507 drm_mode_config_init(adev_to_drm(adev));
3509 r = amdgpu_device_ip_init(adev);
3511 /* failed in exclusive mode due to timeout */
3512 if (amdgpu_sriov_vf(adev) &&
3513 !amdgpu_sriov_runtime(adev) &&
3514 amdgpu_virt_mmio_blocked(adev) &&
3515 !amdgpu_virt_wait_reset(adev)) {
3516 dev_err(adev->dev, "VF exclusive mode timeout\n");
3517 /* Don't send request since VF is inactive. */
3518 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
3519 adev->virt.ops = NULL;
3521 goto release_ras_con;
3523 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
3524 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
3525 goto release_ras_con;
3529 "SE %d, SH per SE %d, CU per SH %d, active_cu_number %d\n",
3530 adev->gfx.config.max_shader_engines,
3531 adev->gfx.config.max_sh_per_se,
3532 adev->gfx.config.max_cu_per_sh,
3533 adev->gfx.cu_info.number);
3535 adev->accel_working = true;
3537 amdgpu_vm_check_compute_bug(adev);
3539 /* Initialize the buffer migration limit. */
3540 if (amdgpu_moverate >= 0)
3541 max_MBps = amdgpu_moverate;
3543 max_MBps = 8; /* Allow 8 MB/s. */
3544 /* Get a log2 for easy divisions. */
3545 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
3547 amdgpu_fbdev_init(adev);
3549 r = amdgpu_pm_sysfs_init(adev);
3551 adev->pm_sysfs_en = false;
3552 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
3554 adev->pm_sysfs_en = true;
3556 r = amdgpu_ucode_sysfs_init(adev);
3558 adev->ucode_sysfs_en = false;
3559 DRM_ERROR("Creating firmware sysfs failed (%d).\n", r);
3561 adev->ucode_sysfs_en = true;
3563 if ((amdgpu_testing & 1)) {
3564 if (adev->accel_working)
3565 amdgpu_test_moves(adev);
3567 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
3569 if (amdgpu_benchmarking) {
3570 if (adev->accel_working)
3571 amdgpu_benchmark(adev, amdgpu_benchmarking);
3573 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
3577 * Register gpu instance before amdgpu_device_enable_mgpu_fan_boost.
3578 * Otherwise the mgpu fan boost feature will be skipped due to the
3579 * gpu instance is counted less.
3581 amdgpu_register_gpu_instance(adev);
3583 /* enable clockgating, etc. after ib tests, etc. since some blocks require
3584 * explicit gating rather than handling it automatically.
3586 if (!adev->gmc.xgmi.pending_reset) {
3587 r = amdgpu_device_ip_late_init(adev);
3589 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
3590 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
3591 goto release_ras_con;
3594 amdgpu_ras_resume(adev);
3595 queue_delayed_work(system_wq, &adev->delayed_init_work,
3596 msecs_to_jiffies(AMDGPU_RESUME_MS));
3599 if (amdgpu_sriov_vf(adev))
3600 flush_delayed_work(&adev->delayed_init_work);
3602 r = sysfs_create_files(&adev->dev->kobj, amdgpu_dev_attributes);
3604 dev_err(adev->dev, "Could not create amdgpu device attr\n");
3606 if (IS_ENABLED(CONFIG_PERF_EVENTS))
3607 r = amdgpu_pmu_init(adev);
3609 dev_err(adev->dev, "amdgpu_pmu_init failed\n");
3611 /* Have stored pci confspace at hand for restore in sudden PCI error */
3612 if (amdgpu_device_cache_pci_state(adev->pdev))
3613 pci_restore_state(pdev);
3615 if (adev->gmc.xgmi.pending_reset)
3616 queue_delayed_work(system_wq, &mgpu_info.delayed_reset_work,
3617 msecs_to_jiffies(AMDGPU_RESUME_MS));
3622 amdgpu_release_ras_context(adev);
3625 amdgpu_vf_error_trans_all(adev);
3627 vga_switcheroo_fini_domain_pm_ops(adev->dev);
3630 iounmap(adev->rmmio);
3637 * amdgpu_device_fini - tear down the driver
3639 * @adev: amdgpu_device pointer
3641 * Tear down the driver info (all asics).
3642 * Called at driver shutdown.
3644 void amdgpu_device_fini(struct amdgpu_device *adev)
3646 dev_info(adev->dev, "amdgpu: finishing device.\n");
3647 flush_delayed_work(&adev->delayed_init_work);
3648 ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
3649 adev->shutdown = true;
3651 kfree(adev->pci_state);
3653 /* make sure IB test finished before entering exclusive mode
3654 * to avoid preemption on IB test
3656 if (amdgpu_sriov_vf(adev)) {
3657 amdgpu_virt_request_full_gpu(adev, false);
3658 amdgpu_virt_fini_data_exchange(adev);
3661 /* disable all interrupts */
3662 amdgpu_irq_disable_all(adev);
3663 if (adev->mode_info.mode_config_initialized){
3664 if (!amdgpu_device_has_dc_support(adev))
3665 drm_helper_force_disable_all(adev_to_drm(adev));
3667 drm_atomic_helper_shutdown(adev_to_drm(adev));
3669 amdgpu_fence_driver_fini(adev);
3670 if (adev->pm_sysfs_en)
3671 amdgpu_pm_sysfs_fini(adev);
3672 amdgpu_fbdev_fini(adev);
3673 amdgpu_device_ip_fini(adev);
3674 release_firmware(adev->firmware.gpu_info_fw);
3675 adev->firmware.gpu_info_fw = NULL;
3676 adev->accel_working = false;
3678 amdgpu_reset_fini(adev);
3680 /* free i2c buses */
3681 if (!amdgpu_device_has_dc_support(adev))
3682 amdgpu_i2c_fini(adev);
3684 if (amdgpu_emu_mode != 1)
3685 amdgpu_atombios_fini(adev);
3689 if (amdgpu_device_supports_px(adev_to_drm(adev))) {
3690 vga_switcheroo_unregister_client(adev->pdev);
3691 vga_switcheroo_fini_domain_pm_ops(adev->dev);
3693 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
3694 vga_client_register(adev->pdev, NULL, NULL, NULL);
3695 iounmap(adev->rmmio);
3697 amdgpu_device_doorbell_fini(adev);
3699 if (adev->ucode_sysfs_en)
3700 amdgpu_ucode_sysfs_fini(adev);
3702 sysfs_remove_files(&adev->dev->kobj, amdgpu_dev_attributes);
3703 if (IS_ENABLED(CONFIG_PERF_EVENTS))
3704 amdgpu_pmu_fini(adev);
3705 if (adev->mman.discovery_bin)
3706 amdgpu_discovery_fini(adev);
3714 * amdgpu_device_suspend - initiate device suspend
3716 * @dev: drm dev pointer
3717 * @fbcon : notify the fbdev of suspend
3719 * Puts the hw in the suspend state (all asics).
3720 * Returns 0 for success or an error on failure.
3721 * Called at driver suspend.
3723 int amdgpu_device_suspend(struct drm_device *dev, bool fbcon)
3725 struct amdgpu_device *adev = drm_to_adev(dev);
3728 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
3731 adev->in_suspend = true;
3732 drm_kms_helper_poll_disable(dev);
3735 amdgpu_fbdev_set_suspend(adev, 1);
3737 cancel_delayed_work_sync(&adev->delayed_init_work);
3739 amdgpu_ras_suspend(adev);
3741 r = amdgpu_device_ip_suspend_phase1(adev);
3744 amdgpu_amdkfd_suspend(adev, adev->in_runpm);
3746 /* evict vram memory */
3747 amdgpu_bo_evict_vram(adev);
3749 amdgpu_fence_driver_suspend(adev);
3751 r = amdgpu_device_ip_suspend_phase2(adev);
3752 /* evict remaining vram memory
3753 * This second call to evict vram is to evict the gart page table
3756 amdgpu_bo_evict_vram(adev);
3762 * amdgpu_device_resume - initiate device resume
3764 * @dev: drm dev pointer
3765 * @fbcon : notify the fbdev of resume
3767 * Bring the hw back to operating state (all asics).
3768 * Returns 0 for success or an error on failure.
3769 * Called at driver resume.
3771 int amdgpu_device_resume(struct drm_device *dev, bool fbcon)
3773 struct amdgpu_device *adev = drm_to_adev(dev);
3776 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
3780 amdgpu_gfx_state_change_set(adev, sGpuChangeState_D0Entry);
3783 if (amdgpu_device_need_post(adev)) {
3784 r = amdgpu_device_asic_init(adev);
3786 dev_err(adev->dev, "amdgpu asic init failed\n");
3789 r = amdgpu_device_ip_resume(adev);
3791 dev_err(adev->dev, "amdgpu_device_ip_resume failed (%d).\n", r);
3794 amdgpu_fence_driver_resume(adev);
3797 r = amdgpu_device_ip_late_init(adev);
3801 queue_delayed_work(system_wq, &adev->delayed_init_work,
3802 msecs_to_jiffies(AMDGPU_RESUME_MS));
3804 if (!adev->in_s0ix) {
3805 r = amdgpu_amdkfd_resume(adev, adev->in_runpm);
3810 /* Make sure IB tests flushed */
3811 flush_delayed_work(&adev->delayed_init_work);
3814 amdgpu_fbdev_set_suspend(adev, 0);
3816 drm_kms_helper_poll_enable(dev);
3818 amdgpu_ras_resume(adev);
3821 * Most of the connector probing functions try to acquire runtime pm
3822 * refs to ensure that the GPU is powered on when connector polling is
3823 * performed. Since we're calling this from a runtime PM callback,
3824 * trying to acquire rpm refs will cause us to deadlock.
3826 * Since we're guaranteed to be holding the rpm lock, it's safe to
3827 * temporarily disable the rpm helpers so this doesn't deadlock us.
3830 dev->dev->power.disable_depth++;
3832 if (!amdgpu_device_has_dc_support(adev))
3833 drm_helper_hpd_irq_event(dev);
3835 drm_kms_helper_hotplug_event(dev);
3837 dev->dev->power.disable_depth--;
3839 adev->in_suspend = false;
3845 * amdgpu_device_ip_check_soft_reset - did soft reset succeed
3847 * @adev: amdgpu_device pointer
3849 * The list of all the hardware IPs that make up the asic is walked and
3850 * the check_soft_reset callbacks are run. check_soft_reset determines
3851 * if the asic is still hung or not.
3852 * Returns true if any of the IPs are still in a hung state, false if not.
3854 static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
3857 bool asic_hang = false;
3859 if (amdgpu_sriov_vf(adev))
3862 if (amdgpu_asic_need_full_reset(adev))
3865 for (i = 0; i < adev->num_ip_blocks; i++) {
3866 if (!adev->ip_blocks[i].status.valid)
3868 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
3869 adev->ip_blocks[i].status.hang =
3870 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
3871 if (adev->ip_blocks[i].status.hang) {
3872 dev_info(adev->dev, "IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
3880 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
3882 * @adev: amdgpu_device pointer
3884 * The list of all the hardware IPs that make up the asic is walked and the
3885 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
3886 * handles any IP specific hardware or software state changes that are
3887 * necessary for a soft reset to succeed.
3888 * Returns 0 on success, negative error code on failure.
3890 static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
3894 for (i = 0; i < adev->num_ip_blocks; i++) {
3895 if (!adev->ip_blocks[i].status.valid)
3897 if (adev->ip_blocks[i].status.hang &&
3898 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
3899 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
3909 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
3911 * @adev: amdgpu_device pointer
3913 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
3914 * reset is necessary to recover.
3915 * Returns true if a full asic reset is required, false if not.
3917 static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
3921 if (amdgpu_asic_need_full_reset(adev))
3924 for (i = 0; i < adev->num_ip_blocks; i++) {
3925 if (!adev->ip_blocks[i].status.valid)
3927 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
3928 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
3929 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
3930 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
3931 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
3932 if (adev->ip_blocks[i].status.hang) {
3933 dev_info(adev->dev, "Some block need full reset!\n");
3942 * amdgpu_device_ip_soft_reset - do a soft reset
3944 * @adev: amdgpu_device pointer
3946 * The list of all the hardware IPs that make up the asic is walked and the
3947 * soft_reset callbacks are run if the block is hung. soft_reset handles any
3948 * IP specific hardware or software state changes that are necessary to soft
3950 * Returns 0 on success, negative error code on failure.
3952 static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
3956 for (i = 0; i < adev->num_ip_blocks; i++) {
3957 if (!adev->ip_blocks[i].status.valid)
3959 if (adev->ip_blocks[i].status.hang &&
3960 adev->ip_blocks[i].version->funcs->soft_reset) {
3961 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
3971 * amdgpu_device_ip_post_soft_reset - clean up from soft reset
3973 * @adev: amdgpu_device pointer
3975 * The list of all the hardware IPs that make up the asic is walked and the
3976 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
3977 * handles any IP specific hardware or software state changes that are
3978 * necessary after the IP has been soft reset.
3979 * Returns 0 on success, negative error code on failure.
3981 static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
3985 for (i = 0; i < adev->num_ip_blocks; i++) {
3986 if (!adev->ip_blocks[i].status.valid)
3988 if (adev->ip_blocks[i].status.hang &&
3989 adev->ip_blocks[i].version->funcs->post_soft_reset)
3990 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
3999 * amdgpu_device_recover_vram - Recover some VRAM contents
4001 * @adev: amdgpu_device pointer
4003 * Restores the contents of VRAM buffers from the shadows in GTT. Used to
4004 * restore things like GPUVM page tables after a GPU reset where
4005 * the contents of VRAM might be lost.
4008 * 0 on success, negative error code on failure.
4010 static int amdgpu_device_recover_vram(struct amdgpu_device *adev)
4012 struct dma_fence *fence = NULL, *next = NULL;
4013 struct amdgpu_bo *shadow;
4016 if (amdgpu_sriov_runtime(adev))
4017 tmo = msecs_to_jiffies(8000);
4019 tmo = msecs_to_jiffies(100);
4021 dev_info(adev->dev, "recover vram bo from shadow start\n");
4022 mutex_lock(&adev->shadow_list_lock);
4023 list_for_each_entry(shadow, &adev->shadow_list, shadow_list) {
4025 /* No need to recover an evicted BO */
4026 if (shadow->tbo.mem.mem_type != TTM_PL_TT ||
4027 shadow->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET ||
4028 shadow->parent->tbo.mem.mem_type != TTM_PL_VRAM)
4031 r = amdgpu_bo_restore_shadow(shadow, &next);
4036 tmo = dma_fence_wait_timeout(fence, false, tmo);
4037 dma_fence_put(fence);
4042 } else if (tmo < 0) {
4050 mutex_unlock(&adev->shadow_list_lock);
4053 tmo = dma_fence_wait_timeout(fence, false, tmo);
4054 dma_fence_put(fence);
4056 if (r < 0 || tmo <= 0) {
4057 dev_err(adev->dev, "recover vram bo from shadow failed, r is %ld, tmo is %ld\n", r, tmo);
4061 dev_info(adev->dev, "recover vram bo from shadow done\n");
4067 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
4069 * @adev: amdgpu_device pointer
4070 * @from_hypervisor: request from hypervisor
4072 * do VF FLR and reinitialize Asic
4073 * return 0 means succeeded otherwise failed
4075 static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
4076 bool from_hypervisor)
4080 if (from_hypervisor)
4081 r = amdgpu_virt_request_full_gpu(adev, true);
4083 r = amdgpu_virt_reset_gpu(adev);
4087 amdgpu_amdkfd_pre_reset(adev);
4089 /* Resume IP prior to SMC */
4090 r = amdgpu_device_ip_reinit_early_sriov(adev);
4094 amdgpu_virt_init_data_exchange(adev);
4095 /* we need recover gart prior to run SMC/CP/SDMA resume */
4096 amdgpu_gtt_mgr_recover(ttm_manager_type(&adev->mman.bdev, TTM_PL_TT));
4098 r = amdgpu_device_fw_loading(adev);
4102 /* now we are okay to resume SMC/CP/SDMA */
4103 r = amdgpu_device_ip_reinit_late_sriov(adev);
4107 amdgpu_irq_gpu_reset_resume_helper(adev);
4108 r = amdgpu_ib_ring_tests(adev);
4109 amdgpu_amdkfd_post_reset(adev);
4112 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
4113 amdgpu_inc_vram_lost(adev);
4114 r = amdgpu_device_recover_vram(adev);
4116 amdgpu_virt_release_full_gpu(adev, true);
4122 * amdgpu_device_has_job_running - check if there is any job in mirror list
4124 * @adev: amdgpu_device pointer
4126 * check if there is any job in mirror list
4128 bool amdgpu_device_has_job_running(struct amdgpu_device *adev)
4131 struct drm_sched_job *job;
4133 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4134 struct amdgpu_ring *ring = adev->rings[i];
4136 if (!ring || !ring->sched.thread)
4139 spin_lock(&ring->sched.job_list_lock);
4140 job = list_first_entry_or_null(&ring->sched.pending_list,
4141 struct drm_sched_job, list);
4142 spin_unlock(&ring->sched.job_list_lock);
4150 * amdgpu_device_should_recover_gpu - check if we should try GPU recovery
4152 * @adev: amdgpu_device pointer
4154 * Check amdgpu_gpu_recovery and SRIOV status to see if we should try to recover
4157 bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev)
4159 if (!amdgpu_device_ip_check_soft_reset(adev)) {
4160 dev_info(adev->dev, "Timeout, but no hardware hang detected.\n");
4164 if (amdgpu_gpu_recovery == 0)
4167 if (amdgpu_sriov_vf(adev))
4170 if (amdgpu_gpu_recovery == -1) {
4171 switch (adev->asic_type) {
4177 case CHIP_POLARIS10:
4178 case CHIP_POLARIS11:
4179 case CHIP_POLARIS12:
4190 case CHIP_SIENNA_CICHLID:
4191 case CHIP_NAVY_FLOUNDER:
4192 case CHIP_DIMGREY_CAVEFISH:
4203 dev_info(adev->dev, "GPU recovery disabled.\n");
4207 int amdgpu_device_mode1_reset(struct amdgpu_device *adev)
4212 amdgpu_atombios_scratch_regs_engine_hung(adev, true);
4214 dev_info(adev->dev, "GPU mode1 reset\n");
4217 pci_clear_master(adev->pdev);
4219 amdgpu_device_cache_pci_state(adev->pdev);
4221 if (amdgpu_dpm_is_mode1_reset_supported(adev)) {
4222 dev_info(adev->dev, "GPU smu mode1 reset\n");
4223 ret = amdgpu_dpm_mode1_reset(adev);
4225 dev_info(adev->dev, "GPU psp mode1 reset\n");
4226 ret = psp_gpu_reset(adev);
4230 dev_err(adev->dev, "GPU mode1 reset failed\n");
4232 amdgpu_device_load_pci_state(adev->pdev);
4234 /* wait for asic to come out of reset */
4235 for (i = 0; i < adev->usec_timeout; i++) {
4236 u32 memsize = adev->nbio.funcs->get_memsize(adev);
4238 if (memsize != 0xffffffff)
4243 amdgpu_atombios_scratch_regs_engine_hung(adev, false);
4247 int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
4248 struct amdgpu_reset_context *reset_context)
4251 struct amdgpu_job *job = NULL;
4252 bool need_full_reset =
4253 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4255 if (reset_context->reset_req_dev == adev)
4256 job = reset_context->job;
4258 /* no need to dump if device is not in good state during probe period */
4259 if (!adev->gmc.xgmi.pending_reset)
4260 amdgpu_debugfs_wait_dump(adev);
4262 if (amdgpu_sriov_vf(adev)) {
4263 /* stop the data exchange thread */
4264 amdgpu_virt_fini_data_exchange(adev);
4267 /* block all schedulers and reset given job's ring */
4268 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4269 struct amdgpu_ring *ring = adev->rings[i];
4271 if (!ring || !ring->sched.thread)
4274 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
4275 amdgpu_fence_driver_force_completion(ring);
4279 drm_sched_increase_karma(&job->base);
4281 r = amdgpu_reset_prepare_hwcontext(adev, reset_context);
4285 /* Don't suspend on bare metal if we are not going to HW reset the ASIC */
4286 if (!amdgpu_sriov_vf(adev)) {
4288 if (!need_full_reset)
4289 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
4291 if (!need_full_reset) {
4292 amdgpu_device_ip_pre_soft_reset(adev);
4293 r = amdgpu_device_ip_soft_reset(adev);
4294 amdgpu_device_ip_post_soft_reset(adev);
4295 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
4296 dev_info(adev->dev, "soft reset failed, will fallback to full reset!\n");
4297 need_full_reset = true;
4301 if (need_full_reset)
4302 r = amdgpu_device_ip_suspend(adev);
4303 if (need_full_reset)
4304 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4306 clear_bit(AMDGPU_NEED_FULL_RESET,
4307 &reset_context->flags);
4313 int amdgpu_do_asic_reset(struct list_head *device_list_handle,
4314 struct amdgpu_reset_context *reset_context)
4316 struct amdgpu_device *tmp_adev = NULL;
4317 bool need_full_reset, skip_hw_reset, vram_lost = false;
4320 /* Try reset handler method first */
4321 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
4323 r = amdgpu_reset_perform_reset(tmp_adev, reset_context);
4328 /* Reset handler not implemented, use the default method */
4330 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4331 skip_hw_reset = test_bit(AMDGPU_SKIP_HW_RESET, &reset_context->flags);
4334 * ASIC reset has to be done on all XGMI hive nodes ASAP
4335 * to allow proper links negotiation in FW (within 1 sec)
4337 if (!skip_hw_reset && need_full_reset) {
4338 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4339 /* For XGMI run all resets in parallel to speed up the process */
4340 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
4341 tmp_adev->gmc.xgmi.pending_reset = false;
4342 if (!queue_work(system_unbound_wq, &tmp_adev->xgmi_reset_work))
4345 r = amdgpu_asic_reset(tmp_adev);
4348 dev_err(tmp_adev->dev, "ASIC reset failed with error, %d for drm dev, %s",
4349 r, adev_to_drm(tmp_adev)->unique);
4354 /* For XGMI wait for all resets to complete before proceed */
4356 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4357 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
4358 flush_work(&tmp_adev->xgmi_reset_work);
4359 r = tmp_adev->asic_reset_res;
4367 if (!r && amdgpu_ras_intr_triggered()) {
4368 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4369 if (tmp_adev->mmhub.funcs &&
4370 tmp_adev->mmhub.funcs->reset_ras_error_count)
4371 tmp_adev->mmhub.funcs->reset_ras_error_count(tmp_adev);
4374 amdgpu_ras_intr_cleared();
4377 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4378 if (need_full_reset) {
4380 r = amdgpu_device_asic_init(tmp_adev);
4382 dev_warn(tmp_adev->dev, "asic atom init failed!");
4384 dev_info(tmp_adev->dev, "GPU reset succeeded, trying to resume\n");
4385 r = amdgpu_device_ip_resume_phase1(tmp_adev);
4389 vram_lost = amdgpu_device_check_vram_lost(tmp_adev);
4391 DRM_INFO("VRAM is lost due to GPU reset!\n");
4392 amdgpu_inc_vram_lost(tmp_adev);
4395 r = amdgpu_gtt_mgr_recover(ttm_manager_type(&tmp_adev->mman.bdev, TTM_PL_TT));
4399 r = amdgpu_device_fw_loading(tmp_adev);
4403 r = amdgpu_device_ip_resume_phase2(tmp_adev);
4408 amdgpu_device_fill_reset_magic(tmp_adev);
4411 * Add this ASIC as tracked as reset was already
4412 * complete successfully.
4414 amdgpu_register_gpu_instance(tmp_adev);
4416 if (!reset_context->hive &&
4417 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
4418 amdgpu_xgmi_add_device(tmp_adev);
4420 r = amdgpu_device_ip_late_init(tmp_adev);
4424 amdgpu_fbdev_set_suspend(tmp_adev, 0);
4427 * The GPU enters bad state once faulty pages
4428 * by ECC has reached the threshold, and ras
4429 * recovery is scheduled next. So add one check
4430 * here to break recovery if it indeed exceeds
4431 * bad page threshold, and remind user to
4432 * retire this GPU or setting one bigger
4433 * bad_page_threshold value to fix this once
4434 * probing driver again.
4436 if (!amdgpu_ras_eeprom_check_err_threshold(tmp_adev)) {
4438 amdgpu_ras_resume(tmp_adev);
4444 /* Update PSP FW topology after reset */
4445 if (reset_context->hive &&
4446 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
4447 r = amdgpu_xgmi_update_topology(
4448 reset_context->hive, tmp_adev);
4454 amdgpu_irq_gpu_reset_resume_helper(tmp_adev);
4455 r = amdgpu_ib_ring_tests(tmp_adev);
4457 dev_err(tmp_adev->dev, "ib ring test failed (%d).\n", r);
4458 r = amdgpu_device_ip_suspend(tmp_adev);
4459 need_full_reset = true;
4466 r = amdgpu_device_recover_vram(tmp_adev);
4468 tmp_adev->asic_reset_res = r;
4472 if (need_full_reset)
4473 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4475 clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4479 static bool amdgpu_device_lock_adev(struct amdgpu_device *adev,
4480 struct amdgpu_hive_info *hive)
4482 if (atomic_cmpxchg(&adev->in_gpu_reset, 0, 1) != 0)
4486 down_write_nest_lock(&adev->reset_sem, &hive->hive_lock);
4488 down_write(&adev->reset_sem);
4491 switch (amdgpu_asic_reset_method(adev)) {
4492 case AMD_RESET_METHOD_MODE1:
4493 adev->mp1_state = PP_MP1_STATE_SHUTDOWN;
4495 case AMD_RESET_METHOD_MODE2:
4496 adev->mp1_state = PP_MP1_STATE_RESET;
4499 adev->mp1_state = PP_MP1_STATE_NONE;
4506 static void amdgpu_device_unlock_adev(struct amdgpu_device *adev)
4508 amdgpu_vf_error_trans_all(adev);
4509 adev->mp1_state = PP_MP1_STATE_NONE;
4510 atomic_set(&adev->in_gpu_reset, 0);
4511 up_write(&adev->reset_sem);
4515 * to lockup a list of amdgpu devices in a hive safely, if not a hive
4516 * with multiple nodes, it will be similar as amdgpu_device_lock_adev.
4518 * unlock won't require roll back.
4520 static int amdgpu_device_lock_hive_adev(struct amdgpu_device *adev, struct amdgpu_hive_info *hive)
4522 struct amdgpu_device *tmp_adev = NULL;
4524 if (adev->gmc.xgmi.num_physical_nodes > 1) {
4526 dev_err(adev->dev, "Hive is NULL while device has multiple xgmi nodes");
4529 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
4530 if (!amdgpu_device_lock_adev(tmp_adev, hive))
4533 } else if (!amdgpu_device_lock_adev(adev, hive))
4538 if (!list_is_first(&tmp_adev->gmc.xgmi.head, &hive->device_list)) {
4540 * if the lockup iteration break in the middle of a hive,
4541 * it may means there may has a race issue,
4542 * or a hive device locked up independently.
4543 * we may be in trouble and may not, so will try to roll back
4544 * the lock and give out a warnning.
4546 dev_warn(tmp_adev->dev, "Hive lock iteration broke in the middle. Rolling back to unlock");
4547 list_for_each_entry_continue_reverse(tmp_adev, &hive->device_list, gmc.xgmi.head) {
4548 amdgpu_device_unlock_adev(tmp_adev);
4554 static void amdgpu_device_resume_display_audio(struct amdgpu_device *adev)
4556 struct pci_dev *p = NULL;
4558 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
4559 adev->pdev->bus->number, 1);
4561 pm_runtime_enable(&(p->dev));
4562 pm_runtime_resume(&(p->dev));
4566 static int amdgpu_device_suspend_display_audio(struct amdgpu_device *adev)
4568 enum amd_reset_method reset_method;
4569 struct pci_dev *p = NULL;
4573 * For now, only BACO and mode1 reset are confirmed
4574 * to suffer the audio issue without proper suspended.
4576 reset_method = amdgpu_asic_reset_method(adev);
4577 if ((reset_method != AMD_RESET_METHOD_BACO) &&
4578 (reset_method != AMD_RESET_METHOD_MODE1))
4581 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
4582 adev->pdev->bus->number, 1);
4586 expires = pm_runtime_autosuspend_expiration(&(p->dev));
4589 * If we cannot get the audio device autosuspend delay,
4590 * a fixed 4S interval will be used. Considering 3S is
4591 * the audio controller default autosuspend delay setting.
4592 * 4S used here is guaranteed to cover that.
4594 expires = ktime_get_mono_fast_ns() + NSEC_PER_SEC * 4ULL;
4596 while (!pm_runtime_status_suspended(&(p->dev))) {
4597 if (!pm_runtime_suspend(&(p->dev)))
4600 if (expires < ktime_get_mono_fast_ns()) {
4601 dev_warn(adev->dev, "failed to suspend display audio\n");
4602 /* TODO: abort the succeeding gpu reset? */
4607 pm_runtime_disable(&(p->dev));
4612 void amdgpu_device_recheck_guilty_jobs(
4613 struct amdgpu_device *adev, struct list_head *device_list_handle,
4614 struct amdgpu_reset_context *reset_context)
4618 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4619 struct amdgpu_ring *ring = adev->rings[i];
4621 struct drm_sched_job *s_job;
4623 if (!ring || !ring->sched.thread)
4626 s_job = list_first_entry_or_null(&ring->sched.pending_list,
4627 struct drm_sched_job, list);
4631 /* clear job's guilty and depend the folowing step to decide the real one */
4632 drm_sched_reset_karma(s_job);
4633 drm_sched_resubmit_jobs_ext(&ring->sched, 1);
4635 ret = dma_fence_wait_timeout(s_job->s_fence->parent, false, ring->sched.timeout);
4636 if (ret == 0) { /* timeout */
4637 DRM_ERROR("Found the real bad job! ring:%s, job_id:%llx\n",
4638 ring->sched.name, s_job->id);
4641 drm_sched_increase_karma(s_job);
4644 if (amdgpu_sriov_vf(adev)) {
4645 amdgpu_virt_fini_data_exchange(adev);
4646 r = amdgpu_device_reset_sriov(adev, false);
4648 adev->asic_reset_res = r;
4650 clear_bit(AMDGPU_SKIP_HW_RESET,
4651 &reset_context->flags);
4652 r = amdgpu_do_asic_reset(device_list_handle,
4654 if (r && r == -EAGAIN)
4659 * add reset counter so that the following
4660 * resubmitted job could flush vmid
4662 atomic_inc(&adev->gpu_reset_counter);
4666 /* got the hw fence, signal finished fence */
4667 atomic_dec(ring->sched.score);
4668 dma_fence_get(&s_job->s_fence->finished);
4669 dma_fence_signal(&s_job->s_fence->finished);
4670 dma_fence_put(&s_job->s_fence->finished);
4672 /* remove node from list and free the job */
4673 spin_lock(&ring->sched.job_list_lock);
4674 list_del_init(&s_job->list);
4675 spin_unlock(&ring->sched.job_list_lock);
4676 ring->sched.ops->free_job(s_job);
4681 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
4683 * @adev: amdgpu_device pointer
4684 * @job: which job trigger hang
4686 * Attempt to reset the GPU if it has hung (all asics).
4687 * Attempt to do soft-reset or full-reset and reinitialize Asic
4688 * Returns 0 for success or an error on failure.
4691 int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
4692 struct amdgpu_job *job)
4694 struct list_head device_list, *device_list_handle = NULL;
4695 bool job_signaled = false;
4696 struct amdgpu_hive_info *hive = NULL;
4697 struct amdgpu_device *tmp_adev = NULL;
4699 bool need_emergency_restart = false;
4700 bool audio_suspended = false;
4701 int tmp_vram_lost_counter;
4702 struct amdgpu_reset_context reset_context;
4704 memset(&reset_context, 0, sizeof(reset_context));
4707 * Special case: RAS triggered and full reset isn't supported
4709 need_emergency_restart = amdgpu_ras_need_emergency_restart(adev);
4712 * Flush RAM to disk so that after reboot
4713 * the user can read log and see why the system rebooted.
4715 if (need_emergency_restart && amdgpu_ras_get_context(adev)->reboot) {
4716 DRM_WARN("Emergency reboot.");
4719 emergency_restart();
4722 dev_info(adev->dev, "GPU %s begin!\n",
4723 need_emergency_restart ? "jobs stop":"reset");
4726 * Here we trylock to avoid chain of resets executing from
4727 * either trigger by jobs on different adevs in XGMI hive or jobs on
4728 * different schedulers for same device while this TO handler is running.
4729 * We always reset all schedulers for device and all devices for XGMI
4730 * hive so that should take care of them too.
4732 hive = amdgpu_get_xgmi_hive(adev);
4734 if (atomic_cmpxchg(&hive->in_reset, 0, 1) != 0) {
4735 DRM_INFO("Bailing on TDR for s_job:%llx, hive: %llx as another already in progress",
4736 job ? job->base.id : -1, hive->hive_id);
4737 amdgpu_put_xgmi_hive(hive);
4739 drm_sched_increase_karma(&job->base);
4742 mutex_lock(&hive->hive_lock);
4745 reset_context.method = AMD_RESET_METHOD_NONE;
4746 reset_context.reset_req_dev = adev;
4747 reset_context.job = job;
4748 reset_context.hive = hive;
4749 clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
4752 * lock the device before we try to operate the linked list
4753 * if didn't get the device lock, don't touch the linked list since
4754 * others may iterating it.
4756 r = amdgpu_device_lock_hive_adev(adev, hive);
4758 dev_info(adev->dev, "Bailing on TDR for s_job:%llx, as another already in progress",
4759 job ? job->base.id : -1);
4761 /* even we skipped this reset, still need to set the job to guilty */
4763 drm_sched_increase_karma(&job->base);
4768 * Build list of devices to reset.
4769 * In case we are in XGMI hive mode, resort the device list
4770 * to put adev in the 1st position.
4772 INIT_LIST_HEAD(&device_list);
4773 if (adev->gmc.xgmi.num_physical_nodes > 1) {
4774 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head)
4775 list_add_tail(&tmp_adev->reset_list, &device_list);
4776 if (!list_is_first(&adev->reset_list, &device_list))
4777 list_rotate_to_front(&adev->reset_list, &device_list);
4778 device_list_handle = &device_list;
4780 list_add_tail(&adev->reset_list, &device_list);
4781 device_list_handle = &device_list;
4784 /* block all schedulers and reset given job's ring */
4785 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4787 * Try to put the audio codec into suspend state
4788 * before gpu reset started.
4790 * Due to the power domain of the graphics device
4791 * is shared with AZ power domain. Without this,
4792 * we may change the audio hardware from behind
4793 * the audio driver's back. That will trigger
4794 * some audio codec errors.
4796 if (!amdgpu_device_suspend_display_audio(tmp_adev))
4797 audio_suspended = true;
4799 amdgpu_ras_set_error_query_ready(tmp_adev, false);
4801 cancel_delayed_work_sync(&tmp_adev->delayed_init_work);
4803 if (!amdgpu_sriov_vf(tmp_adev))
4804 amdgpu_amdkfd_pre_reset(tmp_adev);
4807 * Mark these ASICs to be reseted as untracked first
4808 * And add them back after reset completed
4810 amdgpu_unregister_gpu_instance(tmp_adev);
4812 amdgpu_fbdev_set_suspend(tmp_adev, 1);
4814 /* disable ras on ALL IPs */
4815 if (!need_emergency_restart &&
4816 amdgpu_device_ip_need_full_reset(tmp_adev))
4817 amdgpu_ras_suspend(tmp_adev);
4819 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4820 struct amdgpu_ring *ring = tmp_adev->rings[i];
4822 if (!ring || !ring->sched.thread)
4825 drm_sched_stop(&ring->sched, job ? &job->base : NULL);
4827 if (need_emergency_restart)
4828 amdgpu_job_stop_all_jobs_on_sched(&ring->sched);
4830 atomic_inc(&tmp_adev->gpu_reset_counter);
4833 if (need_emergency_restart)
4834 goto skip_sched_resume;
4837 * Must check guilty signal here since after this point all old
4838 * HW fences are force signaled.
4840 * job->base holds a reference to parent fence
4842 if (job && job->base.s_fence->parent &&
4843 dma_fence_is_signaled(job->base.s_fence->parent)) {
4844 job_signaled = true;
4845 dev_info(adev->dev, "Guilty job already signaled, skipping HW reset");
4849 retry: /* Rest of adevs pre asic reset from XGMI hive. */
4850 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4851 r = amdgpu_device_pre_asic_reset(tmp_adev, &reset_context);
4852 /*TODO Should we stop ?*/
4854 dev_err(tmp_adev->dev, "GPU pre asic reset failed with err, %d for drm dev, %s ",
4855 r, adev_to_drm(tmp_adev)->unique);
4856 tmp_adev->asic_reset_res = r;
4860 tmp_vram_lost_counter = atomic_read(&((adev)->vram_lost_counter));
4861 /* Actual ASIC resets if needed.*/
4862 /* TODO Implement XGMI hive reset logic for SRIOV */
4863 if (amdgpu_sriov_vf(adev)) {
4864 r = amdgpu_device_reset_sriov(adev, job ? false : true);
4866 adev->asic_reset_res = r;
4868 r = amdgpu_do_asic_reset(device_list_handle, &reset_context);
4869 if (r && r == -EAGAIN)
4875 /* Post ASIC reset for all devs .*/
4876 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4879 * Sometimes a later bad compute job can block a good gfx job as gfx
4880 * and compute ring share internal GC HW mutually. We add an additional
4881 * guilty jobs recheck step to find the real guilty job, it synchronously
4882 * submits and pends for the first job being signaled. If it gets timeout,
4883 * we identify it as a real guilty job.
4885 if (amdgpu_gpu_recovery == 2 &&
4886 !(tmp_vram_lost_counter < atomic_read(&adev->vram_lost_counter)))
4887 amdgpu_device_recheck_guilty_jobs(
4888 tmp_adev, device_list_handle, &reset_context);
4890 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4891 struct amdgpu_ring *ring = tmp_adev->rings[i];
4893 if (!ring || !ring->sched.thread)
4896 /* No point to resubmit jobs if we didn't HW reset*/
4897 if (!tmp_adev->asic_reset_res && !job_signaled)
4898 drm_sched_resubmit_jobs(&ring->sched);
4900 drm_sched_start(&ring->sched, !tmp_adev->asic_reset_res);
4903 if (!amdgpu_device_has_dc_support(tmp_adev) && !job_signaled) {
4904 drm_helper_resume_force_mode(adev_to_drm(tmp_adev));
4907 tmp_adev->asic_reset_res = 0;
4910 /* bad news, how to tell it to userspace ? */
4911 dev_info(tmp_adev->dev, "GPU reset(%d) failed\n", atomic_read(&tmp_adev->gpu_reset_counter));
4912 amdgpu_vf_error_put(tmp_adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
4914 dev_info(tmp_adev->dev, "GPU reset(%d) succeeded!\n", atomic_read(&tmp_adev->gpu_reset_counter));
4919 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4920 /* unlock kfd: SRIOV would do it separately */
4921 if (!need_emergency_restart && !amdgpu_sriov_vf(tmp_adev))
4922 amdgpu_amdkfd_post_reset(tmp_adev);
4924 /* kfd_post_reset will do nothing if kfd device is not initialized,
4925 * need to bring up kfd here if it's not be initialized before
4927 if (!adev->kfd.init_complete)
4928 amdgpu_amdkfd_device_init(adev);
4930 if (audio_suspended)
4931 amdgpu_device_resume_display_audio(tmp_adev);
4932 amdgpu_device_unlock_adev(tmp_adev);
4937 atomic_set(&hive->in_reset, 0);
4938 mutex_unlock(&hive->hive_lock);
4939 amdgpu_put_xgmi_hive(hive);
4942 if (r && r != -EAGAIN)
4943 dev_info(adev->dev, "GPU reset end with ret = %d\n", r);
4948 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
4950 * @adev: amdgpu_device pointer
4952 * Fetchs and stores in the driver the PCIE capabilities (gen speed
4953 * and lanes) of the slot the device is in. Handles APUs and
4954 * virtualized environments where PCIE config space may not be available.
4956 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
4958 struct pci_dev *pdev;
4959 enum pci_bus_speed speed_cap, platform_speed_cap;
4960 enum pcie_link_width platform_link_width;
4962 if (amdgpu_pcie_gen_cap)
4963 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
4965 if (amdgpu_pcie_lane_cap)
4966 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
4968 /* covers APUs as well */
4969 if (pci_is_root_bus(adev->pdev->bus)) {
4970 if (adev->pm.pcie_gen_mask == 0)
4971 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
4972 if (adev->pm.pcie_mlw_mask == 0)
4973 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
4977 if (adev->pm.pcie_gen_mask && adev->pm.pcie_mlw_mask)
4980 pcie_bandwidth_available(adev->pdev, NULL,
4981 &platform_speed_cap, &platform_link_width);
4983 if (adev->pm.pcie_gen_mask == 0) {
4986 speed_cap = pcie_get_speed_cap(pdev);
4987 if (speed_cap == PCI_SPEED_UNKNOWN) {
4988 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4989 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
4990 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
4992 if (speed_cap == PCIE_SPEED_32_0GT)
4993 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4994 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
4995 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
4996 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4 |
4997 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN5);
4998 else if (speed_cap == PCIE_SPEED_16_0GT)
4999 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5000 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5001 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5002 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4);
5003 else if (speed_cap == PCIE_SPEED_8_0GT)
5004 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5005 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5006 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
5007 else if (speed_cap == PCIE_SPEED_5_0GT)
5008 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5009 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2);
5011 adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1;
5014 if (platform_speed_cap == PCI_SPEED_UNKNOWN) {
5015 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5016 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
5018 if (platform_speed_cap == PCIE_SPEED_32_0GT)
5019 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5020 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5021 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5022 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4 |
5023 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN5);
5024 else if (platform_speed_cap == PCIE_SPEED_16_0GT)
5025 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5026 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5027 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5028 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4);
5029 else if (platform_speed_cap == PCIE_SPEED_8_0GT)
5030 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5031 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5032 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3);
5033 else if (platform_speed_cap == PCIE_SPEED_5_0GT)
5034 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5035 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
5037 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
5041 if (adev->pm.pcie_mlw_mask == 0) {
5042 if (platform_link_width == PCIE_LNK_WIDTH_UNKNOWN) {
5043 adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK;
5045 switch (platform_link_width) {
5047 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
5048 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5049 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5050 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5051 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5052 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5053 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5056 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5057 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5058 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5059 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5060 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5061 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5064 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5065 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5066 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5067 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5068 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5071 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5072 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5073 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5074 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5077 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5078 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5079 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5082 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5083 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5086 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
5095 int amdgpu_device_baco_enter(struct drm_device *dev)
5097 struct amdgpu_device *adev = drm_to_adev(dev);
5098 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
5100 if (!amdgpu_device_supports_baco(adev_to_drm(adev)))
5103 if (ras && ras->supported && adev->nbio.funcs->enable_doorbell_interrupt)
5104 adev->nbio.funcs->enable_doorbell_interrupt(adev, false);
5106 return amdgpu_dpm_baco_enter(adev);
5109 int amdgpu_device_baco_exit(struct drm_device *dev)
5111 struct amdgpu_device *adev = drm_to_adev(dev);
5112 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
5115 if (!amdgpu_device_supports_baco(adev_to_drm(adev)))
5118 ret = amdgpu_dpm_baco_exit(adev);
5122 if (ras && ras->supported && adev->nbio.funcs->enable_doorbell_interrupt)
5123 adev->nbio.funcs->enable_doorbell_interrupt(adev, true);
5128 static void amdgpu_cancel_all_tdr(struct amdgpu_device *adev)
5132 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5133 struct amdgpu_ring *ring = adev->rings[i];
5135 if (!ring || !ring->sched.thread)
5138 cancel_delayed_work_sync(&ring->sched.work_tdr);
5143 * amdgpu_pci_error_detected - Called when a PCI error is detected.
5144 * @pdev: PCI device struct
5145 * @state: PCI channel state
5147 * Description: Called when a PCI error is detected.
5149 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT.
5151 pci_ers_result_t amdgpu_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
5153 struct drm_device *dev = pci_get_drvdata(pdev);
5154 struct amdgpu_device *adev = drm_to_adev(dev);
5157 DRM_INFO("PCI error: detected callback, state(%d)!!\n", state);
5159 if (adev->gmc.xgmi.num_physical_nodes > 1) {
5160 DRM_WARN("No support for XGMI hive yet...");
5161 return PCI_ERS_RESULT_DISCONNECT;
5165 case pci_channel_io_normal:
5166 return PCI_ERS_RESULT_CAN_RECOVER;
5167 /* Fatal error, prepare for slot reset */
5168 case pci_channel_io_frozen:
5170 * Cancel and wait for all TDRs in progress if failing to
5171 * set adev->in_gpu_reset in amdgpu_device_lock_adev
5173 * Locking adev->reset_sem will prevent any external access
5174 * to GPU during PCI error recovery
5176 while (!amdgpu_device_lock_adev(adev, NULL))
5177 amdgpu_cancel_all_tdr(adev);
5180 * Block any work scheduling as we do for regular GPU reset
5181 * for the duration of the recovery
5183 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5184 struct amdgpu_ring *ring = adev->rings[i];
5186 if (!ring || !ring->sched.thread)
5189 drm_sched_stop(&ring->sched, NULL);
5191 atomic_inc(&adev->gpu_reset_counter);
5192 return PCI_ERS_RESULT_NEED_RESET;
5193 case pci_channel_io_perm_failure:
5194 /* Permanent error, prepare for device removal */
5195 return PCI_ERS_RESULT_DISCONNECT;
5198 return PCI_ERS_RESULT_NEED_RESET;
5202 * amdgpu_pci_mmio_enabled - Enable MMIO and dump debug registers
5203 * @pdev: pointer to PCI device
5205 pci_ers_result_t amdgpu_pci_mmio_enabled(struct pci_dev *pdev)
5208 DRM_INFO("PCI error: mmio enabled callback!!\n");
5210 /* TODO - dump whatever for debugging purposes */
5212 /* This called only if amdgpu_pci_error_detected returns
5213 * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still
5214 * works, no need to reset slot.
5217 return PCI_ERS_RESULT_RECOVERED;
5221 * amdgpu_pci_slot_reset - Called when PCI slot has been reset.
5222 * @pdev: PCI device struct
5224 * Description: This routine is called by the pci error recovery
5225 * code after the PCI slot has been reset, just before we
5226 * should resume normal operations.
5228 pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev)
5230 struct drm_device *dev = pci_get_drvdata(pdev);
5231 struct amdgpu_device *adev = drm_to_adev(dev);
5233 struct amdgpu_reset_context reset_context;
5235 struct list_head device_list;
5237 DRM_INFO("PCI error: slot reset callback!!\n");
5239 memset(&reset_context, 0, sizeof(reset_context));
5241 INIT_LIST_HEAD(&device_list);
5242 list_add_tail(&adev->reset_list, &device_list);
5244 /* wait for asic to come out of reset */
5247 /* Restore PCI confspace */
5248 amdgpu_device_load_pci_state(pdev);
5250 /* confirm ASIC came out of reset */
5251 for (i = 0; i < adev->usec_timeout; i++) {
5252 memsize = amdgpu_asic_get_config_memsize(adev);
5254 if (memsize != 0xffffffff)
5258 if (memsize == 0xffffffff) {
5263 reset_context.method = AMD_RESET_METHOD_NONE;
5264 reset_context.reset_req_dev = adev;
5265 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
5266 set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
5268 adev->in_pci_err_recovery = true;
5269 r = amdgpu_device_pre_asic_reset(adev, &reset_context);
5270 adev->in_pci_err_recovery = false;
5274 r = amdgpu_do_asic_reset(&device_list, &reset_context);
5278 if (amdgpu_device_cache_pci_state(adev->pdev))
5279 pci_restore_state(adev->pdev);
5281 DRM_INFO("PCIe error recovery succeeded\n");
5283 DRM_ERROR("PCIe error recovery failed, err:%d", r);
5284 amdgpu_device_unlock_adev(adev);
5287 return r ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
5291 * amdgpu_pci_resume() - resume normal ops after PCI reset
5292 * @pdev: pointer to PCI device
5294 * Called when the error recovery driver tells us that its
5295 * OK to resume normal operation.
5297 void amdgpu_pci_resume(struct pci_dev *pdev)
5299 struct drm_device *dev = pci_get_drvdata(pdev);
5300 struct amdgpu_device *adev = drm_to_adev(dev);
5304 DRM_INFO("PCI error: resume callback!!\n");
5306 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5307 struct amdgpu_ring *ring = adev->rings[i];
5309 if (!ring || !ring->sched.thread)
5313 drm_sched_resubmit_jobs(&ring->sched);
5314 drm_sched_start(&ring->sched, true);
5317 amdgpu_device_unlock_adev(adev);
5320 bool amdgpu_device_cache_pci_state(struct pci_dev *pdev)
5322 struct drm_device *dev = pci_get_drvdata(pdev);
5323 struct amdgpu_device *adev = drm_to_adev(dev);
5326 r = pci_save_state(pdev);
5328 kfree(adev->pci_state);
5330 adev->pci_state = pci_store_saved_state(pdev);
5332 if (!adev->pci_state) {
5333 DRM_ERROR("Failed to store PCI saved state");
5337 DRM_WARN("Failed to save PCI state, err:%d\n", r);
5344 bool amdgpu_device_load_pci_state(struct pci_dev *pdev)
5346 struct drm_device *dev = pci_get_drvdata(pdev);
5347 struct amdgpu_device *adev = drm_to_adev(dev);
5350 if (!adev->pci_state)
5353 r = pci_load_saved_state(pdev, adev->pci_state);
5356 pci_restore_state(pdev);
5358 DRM_WARN("Failed to load PCI state, err:%d\n", r);