2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
24 * Authors: Dave Airlie
28 #include <linux/power_supply.h>
29 #include <linux/kthread.h>
30 #include <linux/module.h>
31 #include <linux/console.h>
32 #include <linux/slab.h>
33 #include <linux/iommu.h>
34 #include <linux/pci.h>
35 #include <linux/devcoredump.h>
36 #include <generated/utsrelease.h>
37 #include <linux/pci-p2pdma.h>
38 #include <linux/apple-gmux.h>
40 #include <drm/drm_aperture.h>
41 #include <drm/drm_atomic_helper.h>
42 #include <drm/drm_crtc_helper.h>
43 #include <drm/drm_fb_helper.h>
44 #include <drm/drm_probe_helper.h>
45 #include <drm/amdgpu_drm.h>
46 #include <linux/vgaarb.h>
47 #include <linux/vga_switcheroo.h>
48 #include <linux/efi.h>
50 #include "amdgpu_trace.h"
51 #include "amdgpu_i2c.h"
53 #include "amdgpu_atombios.h"
54 #include "amdgpu_atomfirmware.h"
56 #ifdef CONFIG_DRM_AMDGPU_SI
59 #ifdef CONFIG_DRM_AMDGPU_CIK
65 #include "bif/bif_4_1_d.h"
66 #include <linux/firmware.h>
67 #include "amdgpu_vf_error.h"
69 #include "amdgpu_amdkfd.h"
70 #include "amdgpu_pm.h"
72 #include "amdgpu_xgmi.h"
73 #include "amdgpu_ras.h"
74 #include "amdgpu_pmu.h"
75 #include "amdgpu_fru_eeprom.h"
76 #include "amdgpu_reset.h"
78 #include <linux/suspend.h>
79 #include <drm/task_barrier.h>
80 #include <linux/pm_runtime.h>
82 #include <drm/drm_drv.h>
84 #if IS_ENABLED(CONFIG_X86)
85 #include <asm/intel-family.h>
88 MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
89 MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
90 MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
91 MODULE_FIRMWARE("amdgpu/picasso_gpu_info.bin");
92 MODULE_FIRMWARE("amdgpu/raven2_gpu_info.bin");
93 MODULE_FIRMWARE("amdgpu/arcturus_gpu_info.bin");
94 MODULE_FIRMWARE("amdgpu/navi12_gpu_info.bin");
96 #define AMDGPU_RESUME_MS 2000
97 #define AMDGPU_MAX_RETRY_LIMIT 2
98 #define AMDGPU_RETRY_SRIOV_RESET(r) ((r) == -EBUSY || (r) == -ETIMEDOUT || (r) == -EINVAL)
100 static const struct drm_driver amdgpu_kms_driver;
102 const char *amdgpu_asic_name[] = {
144 * DOC: pcie_replay_count
146 * The amdgpu driver provides a sysfs API for reporting the total number
147 * of PCIe replays (NAKs)
148 * The file pcie_replay_count is used for this and returns the total
149 * number of replays as a sum of the NAKs generated and NAKs received
152 static ssize_t amdgpu_device_get_pcie_replay_count(struct device *dev,
153 struct device_attribute *attr, char *buf)
155 struct drm_device *ddev = dev_get_drvdata(dev);
156 struct amdgpu_device *adev = drm_to_adev(ddev);
157 uint64_t cnt = amdgpu_asic_get_pcie_replay_count(adev);
159 return sysfs_emit(buf, "%llu\n", cnt);
162 static DEVICE_ATTR(pcie_replay_count, 0444,
163 amdgpu_device_get_pcie_replay_count, NULL);
165 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev);
169 * amdgpu_device_supports_px - Is the device a dGPU with ATPX power control
171 * @dev: drm_device pointer
173 * Returns true if the device is a dGPU with ATPX power control,
174 * otherwise return false.
176 bool amdgpu_device_supports_px(struct drm_device *dev)
178 struct amdgpu_device *adev = drm_to_adev(dev);
180 if ((adev->flags & AMD_IS_PX) && !amdgpu_is_atpx_hybrid())
186 * amdgpu_device_supports_boco - Is the device a dGPU with ACPI power resources
188 * @dev: drm_device pointer
190 * Returns true if the device is a dGPU with ACPI power control,
191 * otherwise return false.
193 bool amdgpu_device_supports_boco(struct drm_device *dev)
195 struct amdgpu_device *adev = drm_to_adev(dev);
198 ((adev->flags & AMD_IS_PX) && amdgpu_is_atpx_hybrid()))
204 * amdgpu_device_supports_baco - Does the device support BACO
206 * @dev: drm_device pointer
208 * Returns true if the device supporte BACO,
209 * otherwise return false.
211 bool amdgpu_device_supports_baco(struct drm_device *dev)
213 struct amdgpu_device *adev = drm_to_adev(dev);
215 return amdgpu_asic_supports_baco(adev);
219 * amdgpu_device_supports_smart_shift - Is the device dGPU with
220 * smart shift support
222 * @dev: drm_device pointer
224 * Returns true if the device is a dGPU with Smart Shift support,
225 * otherwise returns false.
227 bool amdgpu_device_supports_smart_shift(struct drm_device *dev)
229 return (amdgpu_device_supports_boco(dev) &&
230 amdgpu_acpi_is_power_shift_control_supported());
234 * VRAM access helper functions
238 * amdgpu_device_mm_access - access vram by MM_INDEX/MM_DATA
240 * @adev: amdgpu_device pointer
241 * @pos: offset of the buffer in vram
242 * @buf: virtual address of the buffer in system memory
243 * @size: read/write size, sizeof(@buf) must > @size
244 * @write: true - write to vram, otherwise - read from vram
246 void amdgpu_device_mm_access(struct amdgpu_device *adev, loff_t pos,
247 void *buf, size_t size, bool write)
250 uint32_t hi = ~0, tmp = 0;
251 uint32_t *data = buf;
255 if (!drm_dev_enter(adev_to_drm(adev), &idx))
258 BUG_ON(!IS_ALIGNED(pos, 4) || !IS_ALIGNED(size, 4));
260 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
261 for (last = pos + size; pos < last; pos += 4) {
264 WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)pos) | 0x80000000);
266 WREG32_NO_KIQ(mmMM_INDEX_HI, tmp);
270 WREG32_NO_KIQ(mmMM_DATA, *data++);
272 *data++ = RREG32_NO_KIQ(mmMM_DATA);
275 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
280 * amdgpu_device_aper_access - access vram by vram aperature
282 * @adev: amdgpu_device pointer
283 * @pos: offset of the buffer in vram
284 * @buf: virtual address of the buffer in system memory
285 * @size: read/write size, sizeof(@buf) must > @size
286 * @write: true - write to vram, otherwise - read from vram
288 * The return value means how many bytes have been transferred.
290 size_t amdgpu_device_aper_access(struct amdgpu_device *adev, loff_t pos,
291 void *buf, size_t size, bool write)
298 if (!adev->mman.aper_base_kaddr)
301 last = min(pos + size, adev->gmc.visible_vram_size);
303 addr = adev->mman.aper_base_kaddr + pos;
307 memcpy_toio(addr, buf, count);
308 /* Make sure HDP write cache flush happens without any reordering
309 * after the system memory contents are sent over PCIe device
312 amdgpu_device_flush_hdp(adev, NULL);
314 amdgpu_device_invalidate_hdp(adev, NULL);
315 /* Make sure HDP read cache is invalidated before issuing a read
319 memcpy_fromio(buf, addr, count);
331 * amdgpu_device_vram_access - read/write a buffer in vram
333 * @adev: amdgpu_device pointer
334 * @pos: offset of the buffer in vram
335 * @buf: virtual address of the buffer in system memory
336 * @size: read/write size, sizeof(@buf) must > @size
337 * @write: true - write to vram, otherwise - read from vram
339 void amdgpu_device_vram_access(struct amdgpu_device *adev, loff_t pos,
340 void *buf, size_t size, bool write)
344 /* try to using vram apreature to access vram first */
345 count = amdgpu_device_aper_access(adev, pos, buf, size, write);
348 /* using MM to access rest vram */
351 amdgpu_device_mm_access(adev, pos, buf, size, write);
356 * register access helper functions.
359 /* Check if hw access should be skipped because of hotplug or device error */
360 bool amdgpu_device_skip_hw_access(struct amdgpu_device *adev)
362 if (adev->no_hw_access)
365 #ifdef CONFIG_LOCKDEP
367 * This is a bit complicated to understand, so worth a comment. What we assert
368 * here is that the GPU reset is not running on another thread in parallel.
370 * For this we trylock the read side of the reset semaphore, if that succeeds
371 * we know that the reset is not running in paralell.
373 * If the trylock fails we assert that we are either already holding the read
374 * side of the lock or are the reset thread itself and hold the write side of
378 if (down_read_trylock(&adev->reset_domain->sem))
379 up_read(&adev->reset_domain->sem);
381 lockdep_assert_held(&adev->reset_domain->sem);
388 * amdgpu_device_rreg - read a memory mapped IO or indirect register
390 * @adev: amdgpu_device pointer
391 * @reg: dword aligned register offset
392 * @acc_flags: access flags which require special behavior
394 * Returns the 32 bit value from the offset specified.
396 uint32_t amdgpu_device_rreg(struct amdgpu_device *adev,
397 uint32_t reg, uint32_t acc_flags)
401 if (amdgpu_device_skip_hw_access(adev))
404 if ((reg * 4) < adev->rmmio_size) {
405 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
406 amdgpu_sriov_runtime(adev) &&
407 down_read_trylock(&adev->reset_domain->sem)) {
408 ret = amdgpu_kiq_rreg(adev, reg);
409 up_read(&adev->reset_domain->sem);
411 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
414 ret = adev->pcie_rreg(adev, reg * 4);
417 trace_amdgpu_device_rreg(adev->pdev->device, reg, ret);
423 * MMIO register read with bytes helper functions
424 * @offset:bytes offset from MMIO start
428 * amdgpu_mm_rreg8 - read a memory mapped IO register
430 * @adev: amdgpu_device pointer
431 * @offset: byte aligned register offset
433 * Returns the 8 bit value from the offset specified.
435 uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset)
437 if (amdgpu_device_skip_hw_access(adev))
440 if (offset < adev->rmmio_size)
441 return (readb(adev->rmmio + offset));
446 * MMIO register write with bytes helper functions
447 * @offset:bytes offset from MMIO start
448 * @value: the value want to be written to the register
452 * amdgpu_mm_wreg8 - read a memory mapped IO register
454 * @adev: amdgpu_device pointer
455 * @offset: byte aligned register offset
456 * @value: 8 bit value to write
458 * Writes the value specified to the offset specified.
460 void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value)
462 if (amdgpu_device_skip_hw_access(adev))
465 if (offset < adev->rmmio_size)
466 writeb(value, adev->rmmio + offset);
472 * amdgpu_device_wreg - write to a memory mapped IO or indirect register
474 * @adev: amdgpu_device pointer
475 * @reg: dword aligned register offset
476 * @v: 32 bit value to write to the register
477 * @acc_flags: access flags which require special behavior
479 * Writes the value specified to the offset specified.
481 void amdgpu_device_wreg(struct amdgpu_device *adev,
482 uint32_t reg, uint32_t v,
485 if (amdgpu_device_skip_hw_access(adev))
488 if ((reg * 4) < adev->rmmio_size) {
489 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
490 amdgpu_sriov_runtime(adev) &&
491 down_read_trylock(&adev->reset_domain->sem)) {
492 amdgpu_kiq_wreg(adev, reg, v);
493 up_read(&adev->reset_domain->sem);
495 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
498 adev->pcie_wreg(adev, reg * 4, v);
501 trace_amdgpu_device_wreg(adev->pdev->device, reg, v);
505 * amdgpu_mm_wreg_mmio_rlc - write register either with direct/indirect mmio or with RLC path if in range
507 * @adev: amdgpu_device pointer
508 * @reg: mmio/rlc register
510 * @xcc_id: xcc accelerated compute core id
512 * this function is invoked only for the debugfs register access
514 void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
515 uint32_t reg, uint32_t v,
518 if (amdgpu_device_skip_hw_access(adev))
521 if (amdgpu_sriov_fullaccess(adev) &&
522 adev->gfx.rlc.funcs &&
523 adev->gfx.rlc.funcs->is_rlcg_access_range) {
524 if (adev->gfx.rlc.funcs->is_rlcg_access_range(adev, reg))
525 return amdgpu_sriov_wreg(adev, reg, v, 0, 0, xcc_id);
526 } else if ((reg * 4) >= adev->rmmio_size) {
527 adev->pcie_wreg(adev, reg * 4, v);
529 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
534 * amdgpu_device_indirect_rreg - read an indirect register
536 * @adev: amdgpu_device pointer
537 * @reg_addr: indirect register address to read from
539 * Returns the value of indirect register @reg_addr
541 u32 amdgpu_device_indirect_rreg(struct amdgpu_device *adev,
544 unsigned long flags, pcie_index, pcie_data;
545 void __iomem *pcie_index_offset;
546 void __iomem *pcie_data_offset;
549 pcie_index = adev->nbio.funcs->get_pcie_index_offset(adev);
550 pcie_data = adev->nbio.funcs->get_pcie_data_offset(adev);
552 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
553 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
554 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
556 writel(reg_addr, pcie_index_offset);
557 readl(pcie_index_offset);
558 r = readl(pcie_data_offset);
559 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
564 u32 amdgpu_device_indirect_rreg_ext(struct amdgpu_device *adev,
567 unsigned long flags, pcie_index, pcie_index_hi, pcie_data;
569 void __iomem *pcie_index_offset;
570 void __iomem *pcie_index_hi_offset;
571 void __iomem *pcie_data_offset;
573 pcie_index = adev->nbio.funcs->get_pcie_index_offset(adev);
574 pcie_data = adev->nbio.funcs->get_pcie_data_offset(adev);
575 if ((reg_addr >> 32) && (adev->nbio.funcs->get_pcie_index_hi_offset))
576 pcie_index_hi = adev->nbio.funcs->get_pcie_index_hi_offset(adev);
580 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
581 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
582 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
583 if (pcie_index_hi != 0)
584 pcie_index_hi_offset = (void __iomem *)adev->rmmio +
587 writel(reg_addr, pcie_index_offset);
588 readl(pcie_index_offset);
589 if (pcie_index_hi != 0) {
590 writel((reg_addr >> 32) & 0xff, pcie_index_hi_offset);
591 readl(pcie_index_hi_offset);
593 r = readl(pcie_data_offset);
595 /* clear the high bits */
596 if (pcie_index_hi != 0) {
597 writel(0, pcie_index_hi_offset);
598 readl(pcie_index_hi_offset);
601 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
607 * amdgpu_device_indirect_rreg64 - read a 64bits indirect register
609 * @adev: amdgpu_device pointer
610 * @reg_addr: indirect register address to read from
612 * Returns the value of indirect register @reg_addr
614 u64 amdgpu_device_indirect_rreg64(struct amdgpu_device *adev,
617 unsigned long flags, pcie_index, pcie_data;
618 void __iomem *pcie_index_offset;
619 void __iomem *pcie_data_offset;
622 pcie_index = adev->nbio.funcs->get_pcie_index_offset(adev);
623 pcie_data = adev->nbio.funcs->get_pcie_data_offset(adev);
625 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
626 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
627 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
629 /* read low 32 bits */
630 writel(reg_addr, pcie_index_offset);
631 readl(pcie_index_offset);
632 r = readl(pcie_data_offset);
633 /* read high 32 bits */
634 writel(reg_addr + 4, pcie_index_offset);
635 readl(pcie_index_offset);
636 r |= ((u64)readl(pcie_data_offset) << 32);
637 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
642 u64 amdgpu_device_indirect_rreg64_ext(struct amdgpu_device *adev,
645 unsigned long flags, pcie_index, pcie_data;
646 unsigned long pcie_index_hi = 0;
647 void __iomem *pcie_index_offset;
648 void __iomem *pcie_index_hi_offset;
649 void __iomem *pcie_data_offset;
652 pcie_index = adev->nbio.funcs->get_pcie_index_offset(adev);
653 pcie_data = adev->nbio.funcs->get_pcie_data_offset(adev);
654 if ((reg_addr >> 32) && (adev->nbio.funcs->get_pcie_index_hi_offset))
655 pcie_index_hi = adev->nbio.funcs->get_pcie_index_hi_offset(adev);
657 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
658 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
659 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
660 if (pcie_index_hi != 0)
661 pcie_index_hi_offset = (void __iomem *)adev->rmmio +
664 /* read low 32 bits */
665 writel(reg_addr, pcie_index_offset);
666 readl(pcie_index_offset);
667 if (pcie_index_hi != 0) {
668 writel((reg_addr >> 32) & 0xff, pcie_index_hi_offset);
669 readl(pcie_index_hi_offset);
671 r = readl(pcie_data_offset);
672 /* read high 32 bits */
673 writel(reg_addr + 4, pcie_index_offset);
674 readl(pcie_index_offset);
675 if (pcie_index_hi != 0) {
676 writel((reg_addr >> 32) & 0xff, pcie_index_hi_offset);
677 readl(pcie_index_hi_offset);
679 r |= ((u64)readl(pcie_data_offset) << 32);
681 /* clear the high bits */
682 if (pcie_index_hi != 0) {
683 writel(0, pcie_index_hi_offset);
684 readl(pcie_index_hi_offset);
687 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
693 * amdgpu_device_indirect_wreg - write an indirect register address
695 * @adev: amdgpu_device pointer
696 * @reg_addr: indirect register offset
697 * @reg_data: indirect register data
700 void amdgpu_device_indirect_wreg(struct amdgpu_device *adev,
701 u32 reg_addr, u32 reg_data)
703 unsigned long flags, pcie_index, pcie_data;
704 void __iomem *pcie_index_offset;
705 void __iomem *pcie_data_offset;
707 pcie_index = adev->nbio.funcs->get_pcie_index_offset(adev);
708 pcie_data = adev->nbio.funcs->get_pcie_data_offset(adev);
710 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
711 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
712 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
714 writel(reg_addr, pcie_index_offset);
715 readl(pcie_index_offset);
716 writel(reg_data, pcie_data_offset);
717 readl(pcie_data_offset);
718 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
721 void amdgpu_device_indirect_wreg_ext(struct amdgpu_device *adev,
722 u64 reg_addr, u32 reg_data)
724 unsigned long flags, pcie_index, pcie_index_hi, pcie_data;
725 void __iomem *pcie_index_offset;
726 void __iomem *pcie_index_hi_offset;
727 void __iomem *pcie_data_offset;
729 pcie_index = adev->nbio.funcs->get_pcie_index_offset(adev);
730 pcie_data = adev->nbio.funcs->get_pcie_data_offset(adev);
731 if ((reg_addr >> 32) && (adev->nbio.funcs->get_pcie_index_hi_offset))
732 pcie_index_hi = adev->nbio.funcs->get_pcie_index_hi_offset(adev);
736 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
737 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
738 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
739 if (pcie_index_hi != 0)
740 pcie_index_hi_offset = (void __iomem *)adev->rmmio +
743 writel(reg_addr, pcie_index_offset);
744 readl(pcie_index_offset);
745 if (pcie_index_hi != 0) {
746 writel((reg_addr >> 32) & 0xff, pcie_index_hi_offset);
747 readl(pcie_index_hi_offset);
749 writel(reg_data, pcie_data_offset);
750 readl(pcie_data_offset);
752 /* clear the high bits */
753 if (pcie_index_hi != 0) {
754 writel(0, pcie_index_hi_offset);
755 readl(pcie_index_hi_offset);
758 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
762 * amdgpu_device_indirect_wreg64 - write a 64bits indirect register address
764 * @adev: amdgpu_device pointer
765 * @reg_addr: indirect register offset
766 * @reg_data: indirect register data
769 void amdgpu_device_indirect_wreg64(struct amdgpu_device *adev,
770 u32 reg_addr, u64 reg_data)
772 unsigned long flags, pcie_index, pcie_data;
773 void __iomem *pcie_index_offset;
774 void __iomem *pcie_data_offset;
776 pcie_index = adev->nbio.funcs->get_pcie_index_offset(adev);
777 pcie_data = adev->nbio.funcs->get_pcie_data_offset(adev);
779 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
780 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
781 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
783 /* write low 32 bits */
784 writel(reg_addr, pcie_index_offset);
785 readl(pcie_index_offset);
786 writel((u32)(reg_data & 0xffffffffULL), pcie_data_offset);
787 readl(pcie_data_offset);
788 /* write high 32 bits */
789 writel(reg_addr + 4, pcie_index_offset);
790 readl(pcie_index_offset);
791 writel((u32)(reg_data >> 32), pcie_data_offset);
792 readl(pcie_data_offset);
793 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
796 void amdgpu_device_indirect_wreg64_ext(struct amdgpu_device *adev,
797 u64 reg_addr, u64 reg_data)
799 unsigned long flags, pcie_index, pcie_data;
800 unsigned long pcie_index_hi = 0;
801 void __iomem *pcie_index_offset;
802 void __iomem *pcie_index_hi_offset;
803 void __iomem *pcie_data_offset;
805 pcie_index = adev->nbio.funcs->get_pcie_index_offset(adev);
806 pcie_data = adev->nbio.funcs->get_pcie_data_offset(adev);
807 if ((reg_addr >> 32) && (adev->nbio.funcs->get_pcie_index_hi_offset))
808 pcie_index_hi = adev->nbio.funcs->get_pcie_index_hi_offset(adev);
810 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
811 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
812 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
813 if (pcie_index_hi != 0)
814 pcie_index_hi_offset = (void __iomem *)adev->rmmio +
817 /* write low 32 bits */
818 writel(reg_addr, pcie_index_offset);
819 readl(pcie_index_offset);
820 if (pcie_index_hi != 0) {
821 writel((reg_addr >> 32) & 0xff, pcie_index_hi_offset);
822 readl(pcie_index_hi_offset);
824 writel((u32)(reg_data & 0xffffffffULL), pcie_data_offset);
825 readl(pcie_data_offset);
826 /* write high 32 bits */
827 writel(reg_addr + 4, pcie_index_offset);
828 readl(pcie_index_offset);
829 if (pcie_index_hi != 0) {
830 writel((reg_addr >> 32) & 0xff, pcie_index_hi_offset);
831 readl(pcie_index_hi_offset);
833 writel((u32)(reg_data >> 32), pcie_data_offset);
834 readl(pcie_data_offset);
836 /* clear the high bits */
837 if (pcie_index_hi != 0) {
838 writel(0, pcie_index_hi_offset);
839 readl(pcie_index_hi_offset);
842 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
846 * amdgpu_device_get_rev_id - query device rev_id
848 * @adev: amdgpu_device pointer
850 * Return device rev_id
852 u32 amdgpu_device_get_rev_id(struct amdgpu_device *adev)
854 return adev->nbio.funcs->get_rev_id(adev);
858 * amdgpu_invalid_rreg - dummy reg read function
860 * @adev: amdgpu_device pointer
861 * @reg: offset of register
863 * Dummy register read function. Used for register blocks
864 * that certain asics don't have (all asics).
865 * Returns the value in the register.
867 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
869 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
874 static uint32_t amdgpu_invalid_rreg_ext(struct amdgpu_device *adev, uint64_t reg)
876 DRM_ERROR("Invalid callback to read register 0x%llX\n", reg);
882 * amdgpu_invalid_wreg - dummy reg write function
884 * @adev: amdgpu_device pointer
885 * @reg: offset of register
886 * @v: value to write to the register
888 * Dummy register read function. Used for register blocks
889 * that certain asics don't have (all asics).
891 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
893 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
898 static void amdgpu_invalid_wreg_ext(struct amdgpu_device *adev, uint64_t reg, uint32_t v)
900 DRM_ERROR("Invalid callback to write register 0x%llX with 0x%08X\n",
906 * amdgpu_invalid_rreg64 - dummy 64 bit reg read function
908 * @adev: amdgpu_device pointer
909 * @reg: offset of register
911 * Dummy register read function. Used for register blocks
912 * that certain asics don't have (all asics).
913 * Returns the value in the register.
915 static uint64_t amdgpu_invalid_rreg64(struct amdgpu_device *adev, uint32_t reg)
917 DRM_ERROR("Invalid callback to read 64 bit register 0x%04X\n", reg);
922 static uint64_t amdgpu_invalid_rreg64_ext(struct amdgpu_device *adev, uint64_t reg)
924 DRM_ERROR("Invalid callback to read register 0x%llX\n", reg);
930 * amdgpu_invalid_wreg64 - dummy reg write function
932 * @adev: amdgpu_device pointer
933 * @reg: offset of register
934 * @v: value to write to the register
936 * Dummy register read function. Used for register blocks
937 * that certain asics don't have (all asics).
939 static void amdgpu_invalid_wreg64(struct amdgpu_device *adev, uint32_t reg, uint64_t v)
941 DRM_ERROR("Invalid callback to write 64 bit register 0x%04X with 0x%08llX\n",
946 static void amdgpu_invalid_wreg64_ext(struct amdgpu_device *adev, uint64_t reg, uint64_t v)
948 DRM_ERROR("Invalid callback to write 64 bit register 0x%llX with 0x%08llX\n",
954 * amdgpu_block_invalid_rreg - dummy reg read function
956 * @adev: amdgpu_device pointer
957 * @block: offset of instance
958 * @reg: offset of register
960 * Dummy register read function. Used for register blocks
961 * that certain asics don't have (all asics).
962 * Returns the value in the register.
964 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
965 uint32_t block, uint32_t reg)
967 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
974 * amdgpu_block_invalid_wreg - dummy reg write function
976 * @adev: amdgpu_device pointer
977 * @block: offset of instance
978 * @reg: offset of register
979 * @v: value to write to the register
981 * Dummy register read function. Used for register blocks
982 * that certain asics don't have (all asics).
984 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
986 uint32_t reg, uint32_t v)
988 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
994 * amdgpu_device_asic_init - Wrapper for atom asic_init
996 * @adev: amdgpu_device pointer
998 * Does any asic specific work and then calls atom asic init.
1000 static int amdgpu_device_asic_init(struct amdgpu_device *adev)
1004 amdgpu_asic_pre_asic_init(adev);
1006 if (adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 3) ||
1007 adev->ip_versions[GC_HWIP][0] >= IP_VERSION(11, 0, 0)) {
1008 amdgpu_psp_wait_for_bootloader(adev);
1009 ret = amdgpu_atomfirmware_asic_init(adev, true);
1012 return amdgpu_atom_asic_init(adev->mode_info.atom_context);
1019 * amdgpu_device_mem_scratch_init - allocate the VRAM scratch page
1021 * @adev: amdgpu_device pointer
1023 * Allocates a scratch page of VRAM for use by various things in the
1026 static int amdgpu_device_mem_scratch_init(struct amdgpu_device *adev)
1028 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE, PAGE_SIZE,
1029 AMDGPU_GEM_DOMAIN_VRAM |
1030 AMDGPU_GEM_DOMAIN_GTT,
1031 &adev->mem_scratch.robj,
1032 &adev->mem_scratch.gpu_addr,
1033 (void **)&adev->mem_scratch.ptr);
1037 * amdgpu_device_mem_scratch_fini - Free the VRAM scratch page
1039 * @adev: amdgpu_device pointer
1041 * Frees the VRAM scratch page.
1043 static void amdgpu_device_mem_scratch_fini(struct amdgpu_device *adev)
1045 amdgpu_bo_free_kernel(&adev->mem_scratch.robj, NULL, NULL);
1049 * amdgpu_device_program_register_sequence - program an array of registers.
1051 * @adev: amdgpu_device pointer
1052 * @registers: pointer to the register array
1053 * @array_size: size of the register array
1055 * Programs an array or registers with and or masks.
1056 * This is a helper for setting golden registers.
1058 void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
1059 const u32 *registers,
1060 const u32 array_size)
1062 u32 tmp, reg, and_mask, or_mask;
1068 for (i = 0; i < array_size; i += 3) {
1069 reg = registers[i + 0];
1070 and_mask = registers[i + 1];
1071 or_mask = registers[i + 2];
1073 if (and_mask == 0xffffffff) {
1078 if (adev->family >= AMDGPU_FAMILY_AI)
1079 tmp |= (or_mask & and_mask);
1088 * amdgpu_device_pci_config_reset - reset the GPU
1090 * @adev: amdgpu_device pointer
1092 * Resets the GPU using the pci config reset sequence.
1093 * Only applicable to asics prior to vega10.
1095 void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
1097 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
1101 * amdgpu_device_pci_reset - reset the GPU using generic PCI means
1103 * @adev: amdgpu_device pointer
1105 * Resets the GPU using generic pci reset interfaces (FLR, SBR, etc.).
1107 int amdgpu_device_pci_reset(struct amdgpu_device *adev)
1109 return pci_reset_function(adev->pdev);
1113 * amdgpu_device_wb_*()
1114 * Writeback is the method by which the GPU updates special pages in memory
1115 * with the status of certain GPU events (fences, ring pointers,etc.).
1119 * amdgpu_device_wb_fini - Disable Writeback and free memory
1121 * @adev: amdgpu_device pointer
1123 * Disables Writeback and frees the Writeback memory (all asics).
1124 * Used at driver shutdown.
1126 static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
1128 if (adev->wb.wb_obj) {
1129 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
1131 (void **)&adev->wb.wb);
1132 adev->wb.wb_obj = NULL;
1137 * amdgpu_device_wb_init - Init Writeback driver info and allocate memory
1139 * @adev: amdgpu_device pointer
1141 * Initializes writeback and allocates writeback memory (all asics).
1142 * Used at driver startup.
1143 * Returns 0 on success or an -error on failure.
1145 static int amdgpu_device_wb_init(struct amdgpu_device *adev)
1149 if (adev->wb.wb_obj == NULL) {
1150 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
1151 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
1152 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
1153 &adev->wb.wb_obj, &adev->wb.gpu_addr,
1154 (void **)&adev->wb.wb);
1156 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
1160 adev->wb.num_wb = AMDGPU_MAX_WB;
1161 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
1163 /* clear wb memory */
1164 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
1171 * amdgpu_device_wb_get - Allocate a wb entry
1173 * @adev: amdgpu_device pointer
1176 * Allocate a wb slot for use by the driver (all asics).
1177 * Returns 0 on success or -EINVAL on failure.
1179 int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
1181 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
1183 if (offset < adev->wb.num_wb) {
1184 __set_bit(offset, adev->wb.used);
1185 *wb = offset << 3; /* convert to dw offset */
1193 * amdgpu_device_wb_free - Free a wb entry
1195 * @adev: amdgpu_device pointer
1198 * Free a wb slot allocated for use by the driver (all asics)
1200 void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
1203 if (wb < adev->wb.num_wb)
1204 __clear_bit(wb, adev->wb.used);
1208 * amdgpu_device_resize_fb_bar - try to resize FB BAR
1210 * @adev: amdgpu_device pointer
1212 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
1213 * to fail, but if any of the BARs is not accessible after the size we abort
1214 * driver loading by returning -ENODEV.
1216 int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
1218 int rbar_size = pci_rebar_bytes_to_size(adev->gmc.real_vram_size);
1219 struct pci_bus *root;
1220 struct resource *res;
1225 if (!IS_ENABLED(CONFIG_PHYS_ADDR_T_64BIT))
1229 if (amdgpu_sriov_vf(adev))
1232 /* skip if the bios has already enabled large BAR */
1233 if (adev->gmc.real_vram_size &&
1234 (pci_resource_len(adev->pdev, 0) >= adev->gmc.real_vram_size))
1237 /* Check if the root BUS has 64bit memory resources */
1238 root = adev->pdev->bus;
1239 while (root->parent)
1240 root = root->parent;
1242 pci_bus_for_each_resource(root, res, i) {
1243 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
1244 res->start > 0x100000000ull)
1248 /* Trying to resize is pointless without a root hub window above 4GB */
1252 /* Limit the BAR size to what is available */
1253 rbar_size = min(fls(pci_rebar_get_possible_sizes(adev->pdev, 0)) - 1,
1256 /* Disable memory decoding while we change the BAR addresses and size */
1257 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
1258 pci_write_config_word(adev->pdev, PCI_COMMAND,
1259 cmd & ~PCI_COMMAND_MEMORY);
1261 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
1262 amdgpu_doorbell_fini(adev);
1263 if (adev->asic_type >= CHIP_BONAIRE)
1264 pci_release_resource(adev->pdev, 2);
1266 pci_release_resource(adev->pdev, 0);
1268 r = pci_resize_resource(adev->pdev, 0, rbar_size);
1270 DRM_INFO("Not enough PCI address space for a large BAR.");
1271 else if (r && r != -ENOTSUPP)
1272 DRM_ERROR("Problem resizing BAR0 (%d).", r);
1274 pci_assign_unassigned_bus_resources(adev->pdev->bus);
1276 /* When the doorbell or fb BAR isn't available we have no chance of
1279 r = amdgpu_doorbell_init(adev);
1280 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
1283 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
1288 static bool amdgpu_device_read_bios(struct amdgpu_device *adev)
1290 if (hweight32(adev->aid_mask) && (adev->flags & AMD_IS_APU))
1297 * GPU helpers function.
1300 * amdgpu_device_need_post - check if the hw need post or not
1302 * @adev: amdgpu_device pointer
1304 * Check if the asic has been initialized (all asics) at driver startup
1305 * or post is needed if hw reset is performed.
1306 * Returns true if need or false if not.
1308 bool amdgpu_device_need_post(struct amdgpu_device *adev)
1312 if (amdgpu_sriov_vf(adev))
1315 if (!amdgpu_device_read_bios(adev))
1318 if (amdgpu_passthrough(adev)) {
1319 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
1320 * some old smc fw still need driver do vPost otherwise gpu hang, while
1321 * those smc fw version above 22.15 doesn't have this flaw, so we force
1322 * vpost executed for smc version below 22.15
1324 if (adev->asic_type == CHIP_FIJI) {
1328 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
1329 /* force vPost if error occured */
1333 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
1334 if (fw_ver < 0x00160e00)
1339 /* Don't post if we need to reset whole hive on init */
1340 if (adev->gmc.xgmi.pending_reset)
1343 if (adev->has_hw_reset) {
1344 adev->has_hw_reset = false;
1348 /* bios scratch used on CIK+ */
1349 if (adev->asic_type >= CHIP_BONAIRE)
1350 return amdgpu_atombios_scratch_need_asic_init(adev);
1352 /* check MEM_SIZE for older asics */
1353 reg = amdgpu_asic_get_config_memsize(adev);
1355 if ((reg != 0) && (reg != 0xffffffff))
1362 * On APUs with >= 64GB white flickering has been observed w/ SG enabled.
1363 * Disable S/G on such systems until we have a proper fix.
1364 * https://gitlab.freedesktop.org/drm/amd/-/issues/2354
1365 * https://gitlab.freedesktop.org/drm/amd/-/issues/2735
1367 bool amdgpu_sg_display_supported(struct amdgpu_device *adev)
1369 switch (amdgpu_sg_display) {
1379 if ((totalram_pages() << (PAGE_SHIFT - 10)) +
1380 (adev->gmc.real_vram_size / 1024) >= 64000000) {
1381 DRM_WARN("Disabling S/G due to >=64GB RAM\n");
1388 * Intel hosts such as Raptor Lake and Sapphire Rapids don't support dynamic
1389 * speed switching. Until we have confirmation from Intel that a specific host
1390 * supports it, it's safer that we keep it disabled for all.
1392 * https://edc.intel.com/content/www/us/en/design/products/platforms/details/raptor-lake-s/13th-generation-core-processors-datasheet-volume-1-of-2/005/pci-express-support/
1393 * https://gitlab.freedesktop.org/drm/amd/-/issues/2663
1395 bool amdgpu_device_pcie_dynamic_switching_supported(void)
1397 #if IS_ENABLED(CONFIG_X86)
1398 struct cpuinfo_x86 *c = &cpu_data(0);
1400 if (c->x86_vendor == X86_VENDOR_INTEL)
1407 * amdgpu_device_should_use_aspm - check if the device should program ASPM
1409 * @adev: amdgpu_device pointer
1411 * Confirm whether the module parameter and pcie bridge agree that ASPM should
1412 * be set for this device.
1414 * Returns true if it should be used or false if not.
1416 bool amdgpu_device_should_use_aspm(struct amdgpu_device *adev)
1418 switch (amdgpu_aspm) {
1428 return pcie_aspm_enabled(adev->pdev);
1431 bool amdgpu_device_aspm_support_quirk(void)
1433 #if IS_ENABLED(CONFIG_X86)
1434 struct cpuinfo_x86 *c = &cpu_data(0);
1436 return !(c->x86 == 6 && c->x86_model == INTEL_FAM6_ALDERLAKE);
1442 /* if we get transitioned to only one device, take VGA back */
1444 * amdgpu_device_vga_set_decode - enable/disable vga decode
1446 * @pdev: PCI device pointer
1447 * @state: enable/disable vga decode
1449 * Enable/disable vga decode (all asics).
1450 * Returns VGA resource flags.
1452 static unsigned int amdgpu_device_vga_set_decode(struct pci_dev *pdev,
1455 struct amdgpu_device *adev = drm_to_adev(pci_get_drvdata(pdev));
1457 amdgpu_asic_set_vga_state(adev, state);
1459 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1460 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1462 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1466 * amdgpu_device_check_block_size - validate the vm block size
1468 * @adev: amdgpu_device pointer
1470 * Validates the vm block size specified via module parameter.
1471 * The vm block size defines number of bits in page table versus page directory,
1472 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1473 * page table and the remaining bits are in the page directory.
1475 static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
1477 /* defines number of bits in page table versus page directory,
1478 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1479 * page table and the remaining bits are in the page directory
1481 if (amdgpu_vm_block_size == -1)
1484 if (amdgpu_vm_block_size < 9) {
1485 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1486 amdgpu_vm_block_size);
1487 amdgpu_vm_block_size = -1;
1492 * amdgpu_device_check_vm_size - validate the vm size
1494 * @adev: amdgpu_device pointer
1496 * Validates the vm size in GB specified via module parameter.
1497 * The VM size is the size of the GPU virtual memory space in GB.
1499 static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
1501 /* no need to check the default value */
1502 if (amdgpu_vm_size == -1)
1505 if (amdgpu_vm_size < 1) {
1506 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1508 amdgpu_vm_size = -1;
1512 static void amdgpu_device_check_smu_prv_buffer_size(struct amdgpu_device *adev)
1515 bool is_os_64 = (sizeof(void *) == 8);
1516 uint64_t total_memory;
1517 uint64_t dram_size_seven_GB = 0x1B8000000;
1518 uint64_t dram_size_three_GB = 0xB8000000;
1520 if (amdgpu_smu_memory_pool_size == 0)
1524 DRM_WARN("Not 64-bit OS, feature not supported\n");
1528 total_memory = (uint64_t)si.totalram * si.mem_unit;
1530 if ((amdgpu_smu_memory_pool_size == 1) ||
1531 (amdgpu_smu_memory_pool_size == 2)) {
1532 if (total_memory < dram_size_three_GB)
1534 } else if ((amdgpu_smu_memory_pool_size == 4) ||
1535 (amdgpu_smu_memory_pool_size == 8)) {
1536 if (total_memory < dram_size_seven_GB)
1539 DRM_WARN("Smu memory pool size not supported\n");
1542 adev->pm.smu_prv_buffer_size = amdgpu_smu_memory_pool_size << 28;
1547 DRM_WARN("No enough system memory\n");
1549 adev->pm.smu_prv_buffer_size = 0;
1552 static int amdgpu_device_init_apu_flags(struct amdgpu_device *adev)
1554 if (!(adev->flags & AMD_IS_APU) ||
1555 adev->asic_type < CHIP_RAVEN)
1558 switch (adev->asic_type) {
1560 if (adev->pdev->device == 0x15dd)
1561 adev->apu_flags |= AMD_APU_IS_RAVEN;
1562 if (adev->pdev->device == 0x15d8)
1563 adev->apu_flags |= AMD_APU_IS_PICASSO;
1566 if ((adev->pdev->device == 0x1636) ||
1567 (adev->pdev->device == 0x164c))
1568 adev->apu_flags |= AMD_APU_IS_RENOIR;
1570 adev->apu_flags |= AMD_APU_IS_GREEN_SARDINE;
1573 adev->apu_flags |= AMD_APU_IS_VANGOGH;
1575 case CHIP_YELLOW_CARP:
1577 case CHIP_CYAN_SKILLFISH:
1578 if ((adev->pdev->device == 0x13FE) ||
1579 (adev->pdev->device == 0x143F))
1580 adev->apu_flags |= AMD_APU_IS_CYAN_SKILLFISH2;
1590 * amdgpu_device_check_arguments - validate module params
1592 * @adev: amdgpu_device pointer
1594 * Validates certain module parameters and updates
1595 * the associated values used by the driver (all asics).
1597 static int amdgpu_device_check_arguments(struct amdgpu_device *adev)
1599 if (amdgpu_sched_jobs < 4) {
1600 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1602 amdgpu_sched_jobs = 4;
1603 } else if (!is_power_of_2(amdgpu_sched_jobs)) {
1604 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1606 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1609 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
1610 /* gart size must be greater or equal to 32M */
1611 dev_warn(adev->dev, "gart size (%d) too small\n",
1613 amdgpu_gart_size = -1;
1616 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
1617 /* gtt size must be greater or equal to 32M */
1618 dev_warn(adev->dev, "gtt size (%d) too small\n",
1620 amdgpu_gtt_size = -1;
1623 /* valid range is between 4 and 9 inclusive */
1624 if (amdgpu_vm_fragment_size != -1 &&
1625 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1626 dev_warn(adev->dev, "valid range is between 4 and 9\n");
1627 amdgpu_vm_fragment_size = -1;
1630 if (amdgpu_sched_hw_submission < 2) {
1631 dev_warn(adev->dev, "sched hw submission jobs (%d) must be at least 2\n",
1632 amdgpu_sched_hw_submission);
1633 amdgpu_sched_hw_submission = 2;
1634 } else if (!is_power_of_2(amdgpu_sched_hw_submission)) {
1635 dev_warn(adev->dev, "sched hw submission jobs (%d) must be a power of 2\n",
1636 amdgpu_sched_hw_submission);
1637 amdgpu_sched_hw_submission = roundup_pow_of_two(amdgpu_sched_hw_submission);
1640 if (amdgpu_reset_method < -1 || amdgpu_reset_method > 4) {
1641 dev_warn(adev->dev, "invalid option for reset method, reverting to default\n");
1642 amdgpu_reset_method = -1;
1645 amdgpu_device_check_smu_prv_buffer_size(adev);
1647 amdgpu_device_check_vm_size(adev);
1649 amdgpu_device_check_block_size(adev);
1651 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
1657 * amdgpu_switcheroo_set_state - set switcheroo state
1659 * @pdev: pci dev pointer
1660 * @state: vga_switcheroo state
1662 * Callback for the switcheroo driver. Suspends or resumes
1663 * the asics before or after it is powered up using ACPI methods.
1665 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev,
1666 enum vga_switcheroo_state state)
1668 struct drm_device *dev = pci_get_drvdata(pdev);
1671 if (amdgpu_device_supports_px(dev) && state == VGA_SWITCHEROO_OFF)
1674 if (state == VGA_SWITCHEROO_ON) {
1675 pr_info("switched on\n");
1676 /* don't suspend or resume card normally */
1677 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1679 pci_set_power_state(pdev, PCI_D0);
1680 amdgpu_device_load_pci_state(pdev);
1681 r = pci_enable_device(pdev);
1683 DRM_WARN("pci_enable_device failed (%d)\n", r);
1684 amdgpu_device_resume(dev, true);
1686 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1688 pr_info("switched off\n");
1689 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1690 amdgpu_device_suspend(dev, true);
1691 amdgpu_device_cache_pci_state(pdev);
1692 /* Shut down the device */
1693 pci_disable_device(pdev);
1694 pci_set_power_state(pdev, PCI_D3cold);
1695 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1700 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1702 * @pdev: pci dev pointer
1704 * Callback for the switcheroo driver. Check of the switcheroo
1705 * state can be changed.
1706 * Returns true if the state can be changed, false if not.
1708 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1710 struct drm_device *dev = pci_get_drvdata(pdev);
1713 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1714 * locking inversion with the driver load path. And the access here is
1715 * completely racy anyway. So don't bother with locking for now.
1717 return atomic_read(&dev->open_count) == 0;
1720 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1721 .set_gpu_state = amdgpu_switcheroo_set_state,
1723 .can_switch = amdgpu_switcheroo_can_switch,
1727 * amdgpu_device_ip_set_clockgating_state - set the CG state
1729 * @dev: amdgpu_device pointer
1730 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1731 * @state: clockgating state (gate or ungate)
1733 * Sets the requested clockgating state for all instances of
1734 * the hardware IP specified.
1735 * Returns the error code from the last instance.
1737 int amdgpu_device_ip_set_clockgating_state(void *dev,
1738 enum amd_ip_block_type block_type,
1739 enum amd_clockgating_state state)
1741 struct amdgpu_device *adev = dev;
1744 for (i = 0; i < adev->num_ip_blocks; i++) {
1745 if (!adev->ip_blocks[i].status.valid)
1747 if (adev->ip_blocks[i].version->type != block_type)
1749 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1751 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1752 (void *)adev, state);
1754 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1755 adev->ip_blocks[i].version->funcs->name, r);
1761 * amdgpu_device_ip_set_powergating_state - set the PG state
1763 * @dev: amdgpu_device pointer
1764 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1765 * @state: powergating state (gate or ungate)
1767 * Sets the requested powergating state for all instances of
1768 * the hardware IP specified.
1769 * Returns the error code from the last instance.
1771 int amdgpu_device_ip_set_powergating_state(void *dev,
1772 enum amd_ip_block_type block_type,
1773 enum amd_powergating_state state)
1775 struct amdgpu_device *adev = dev;
1778 for (i = 0; i < adev->num_ip_blocks; i++) {
1779 if (!adev->ip_blocks[i].status.valid)
1781 if (adev->ip_blocks[i].version->type != block_type)
1783 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1785 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1786 (void *)adev, state);
1788 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1789 adev->ip_blocks[i].version->funcs->name, r);
1795 * amdgpu_device_ip_get_clockgating_state - get the CG state
1797 * @adev: amdgpu_device pointer
1798 * @flags: clockgating feature flags
1800 * Walks the list of IPs on the device and updates the clockgating
1801 * flags for each IP.
1802 * Updates @flags with the feature flags for each hardware IP where
1803 * clockgating is enabled.
1805 void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
1810 for (i = 0; i < adev->num_ip_blocks; i++) {
1811 if (!adev->ip_blocks[i].status.valid)
1813 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1814 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1819 * amdgpu_device_ip_wait_for_idle - wait for idle
1821 * @adev: amdgpu_device pointer
1822 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1824 * Waits for the request hardware IP to be idle.
1825 * Returns 0 for success or a negative error code on failure.
1827 int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1828 enum amd_ip_block_type block_type)
1832 for (i = 0; i < adev->num_ip_blocks; i++) {
1833 if (!adev->ip_blocks[i].status.valid)
1835 if (adev->ip_blocks[i].version->type == block_type) {
1836 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
1847 * amdgpu_device_ip_is_idle - is the hardware IP idle
1849 * @adev: amdgpu_device pointer
1850 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1852 * Check if the hardware IP is idle or not.
1853 * Returns true if it the IP is idle, false if not.
1855 bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1856 enum amd_ip_block_type block_type)
1860 for (i = 0; i < adev->num_ip_blocks; i++) {
1861 if (!adev->ip_blocks[i].status.valid)
1863 if (adev->ip_blocks[i].version->type == block_type)
1864 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
1871 * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1873 * @adev: amdgpu_device pointer
1874 * @type: Type of hardware IP (SMU, GFX, UVD, etc.)
1876 * Returns a pointer to the hardware IP block structure
1877 * if it exists for the asic, otherwise NULL.
1879 struct amdgpu_ip_block *
1880 amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1881 enum amd_ip_block_type type)
1885 for (i = 0; i < adev->num_ip_blocks; i++)
1886 if (adev->ip_blocks[i].version->type == type)
1887 return &adev->ip_blocks[i];
1893 * amdgpu_device_ip_block_version_cmp
1895 * @adev: amdgpu_device pointer
1896 * @type: enum amd_ip_block_type
1897 * @major: major version
1898 * @minor: minor version
1900 * return 0 if equal or greater
1901 * return 1 if smaller or the ip_block doesn't exist
1903 int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1904 enum amd_ip_block_type type,
1905 u32 major, u32 minor)
1907 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
1909 if (ip_block && ((ip_block->version->major > major) ||
1910 ((ip_block->version->major == major) &&
1911 (ip_block->version->minor >= minor))))
1918 * amdgpu_device_ip_block_add
1920 * @adev: amdgpu_device pointer
1921 * @ip_block_version: pointer to the IP to add
1923 * Adds the IP block driver information to the collection of IPs
1926 int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1927 const struct amdgpu_ip_block_version *ip_block_version)
1929 if (!ip_block_version)
1932 switch (ip_block_version->type) {
1933 case AMD_IP_BLOCK_TYPE_VCN:
1934 if (adev->harvest_ip_mask & AMD_HARVEST_IP_VCN_MASK)
1937 case AMD_IP_BLOCK_TYPE_JPEG:
1938 if (adev->harvest_ip_mask & AMD_HARVEST_IP_JPEG_MASK)
1945 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
1946 ip_block_version->funcs->name);
1948 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1954 * amdgpu_device_enable_virtual_display - enable virtual display feature
1956 * @adev: amdgpu_device pointer
1958 * Enabled the virtual display feature if the user has enabled it via
1959 * the module parameter virtual_display. This feature provides a virtual
1960 * display hardware on headless boards or in virtualized environments.
1961 * This function parses and validates the configuration string specified by
1962 * the user and configues the virtual display configuration (number of
1963 * virtual connectors, crtcs, etc.) specified.
1965 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
1967 adev->enable_virtual_display = false;
1969 if (amdgpu_virtual_display) {
1970 const char *pci_address_name = pci_name(adev->pdev);
1971 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
1973 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1974 pciaddstr_tmp = pciaddstr;
1975 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1976 pciaddname = strsep(&pciaddname_tmp, ",");
1977 if (!strcmp("all", pciaddname)
1978 || !strcmp(pci_address_name, pciaddname)) {
1982 adev->enable_virtual_display = true;
1985 res = kstrtol(pciaddname_tmp, 10,
1993 adev->mode_info.num_crtc = num_crtc;
1995 adev->mode_info.num_crtc = 1;
2001 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
2002 amdgpu_virtual_display, pci_address_name,
2003 adev->enable_virtual_display, adev->mode_info.num_crtc);
2009 void amdgpu_device_set_sriov_virtual_display(struct amdgpu_device *adev)
2011 if (amdgpu_sriov_vf(adev) && !adev->enable_virtual_display) {
2012 adev->mode_info.num_crtc = 1;
2013 adev->enable_virtual_display = true;
2014 DRM_INFO("virtual_display:%d, num_crtc:%d\n",
2015 adev->enable_virtual_display, adev->mode_info.num_crtc);
2020 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
2022 * @adev: amdgpu_device pointer
2024 * Parses the asic configuration parameters specified in the gpu info
2025 * firmware and makes them availale to the driver for use in configuring
2027 * Returns 0 on success, -EINVAL on failure.
2029 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
2031 const char *chip_name;
2034 const struct gpu_info_firmware_header_v1_0 *hdr;
2036 adev->firmware.gpu_info_fw = NULL;
2038 if (adev->mman.discovery_bin) {
2040 * FIXME: The bounding box is still needed by Navi12, so
2041 * temporarily read it from gpu_info firmware. Should be dropped
2042 * when DAL no longer needs it.
2044 if (adev->asic_type != CHIP_NAVI12)
2048 switch (adev->asic_type) {
2052 chip_name = "vega10";
2055 chip_name = "vega12";
2058 if (adev->apu_flags & AMD_APU_IS_RAVEN2)
2059 chip_name = "raven2";
2060 else if (adev->apu_flags & AMD_APU_IS_PICASSO)
2061 chip_name = "picasso";
2063 chip_name = "raven";
2066 chip_name = "arcturus";
2069 chip_name = "navi12";
2073 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
2074 err = amdgpu_ucode_request(adev, &adev->firmware.gpu_info_fw, fw_name);
2077 "Failed to get gpu_info firmware \"%s\"\n",
2082 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
2083 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
2085 switch (hdr->version_major) {
2088 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
2089 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
2090 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2093 * Should be droped when DAL no longer needs it.
2095 if (adev->asic_type == CHIP_NAVI12)
2096 goto parse_soc_bounding_box;
2098 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
2099 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
2100 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
2101 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
2102 adev->gfx.config.max_texture_channel_caches =
2103 le32_to_cpu(gpu_info_fw->gc_num_tccs);
2104 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
2105 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
2106 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
2107 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
2108 adev->gfx.config.double_offchip_lds_buf =
2109 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
2110 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
2111 adev->gfx.cu_info.max_waves_per_simd =
2112 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
2113 adev->gfx.cu_info.max_scratch_slots_per_cu =
2114 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
2115 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
2116 if (hdr->version_minor >= 1) {
2117 const struct gpu_info_firmware_v1_1 *gpu_info_fw =
2118 (const struct gpu_info_firmware_v1_1 *)(adev->firmware.gpu_info_fw->data +
2119 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2120 adev->gfx.config.num_sc_per_sh =
2121 le32_to_cpu(gpu_info_fw->num_sc_per_sh);
2122 adev->gfx.config.num_packer_per_sc =
2123 le32_to_cpu(gpu_info_fw->num_packer_per_sc);
2126 parse_soc_bounding_box:
2128 * soc bounding box info is not integrated in disocovery table,
2129 * we always need to parse it from gpu info firmware if needed.
2131 if (hdr->version_minor == 2) {
2132 const struct gpu_info_firmware_v1_2 *gpu_info_fw =
2133 (const struct gpu_info_firmware_v1_2 *)(adev->firmware.gpu_info_fw->data +
2134 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2135 adev->dm.soc_bounding_box = &gpu_info_fw->soc_bounding_box;
2141 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
2150 * amdgpu_device_ip_early_init - run early init for hardware IPs
2152 * @adev: amdgpu_device pointer
2154 * Early initialization pass for hardware IPs. The hardware IPs that make
2155 * up each asic are discovered each IP's early_init callback is run. This
2156 * is the first stage in initializing the asic.
2157 * Returns 0 on success, negative error code on failure.
2159 static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
2161 struct drm_device *dev = adev_to_drm(adev);
2162 struct pci_dev *parent;
2166 amdgpu_device_enable_virtual_display(adev);
2168 if (amdgpu_sriov_vf(adev)) {
2169 r = amdgpu_virt_request_full_gpu(adev, true);
2174 switch (adev->asic_type) {
2175 #ifdef CONFIG_DRM_AMDGPU_SI
2181 adev->family = AMDGPU_FAMILY_SI;
2182 r = si_set_ip_blocks(adev);
2187 #ifdef CONFIG_DRM_AMDGPU_CIK
2193 if (adev->flags & AMD_IS_APU)
2194 adev->family = AMDGPU_FAMILY_KV;
2196 adev->family = AMDGPU_FAMILY_CI;
2198 r = cik_set_ip_blocks(adev);
2206 case CHIP_POLARIS10:
2207 case CHIP_POLARIS11:
2208 case CHIP_POLARIS12:
2212 if (adev->flags & AMD_IS_APU)
2213 adev->family = AMDGPU_FAMILY_CZ;
2215 adev->family = AMDGPU_FAMILY_VI;
2217 r = vi_set_ip_blocks(adev);
2222 r = amdgpu_discovery_set_ip_blocks(adev);
2228 if (amdgpu_has_atpx() &&
2229 (amdgpu_is_atpx_hybrid() ||
2230 amdgpu_has_atpx_dgpu_power_cntl()) &&
2231 ((adev->flags & AMD_IS_APU) == 0) &&
2232 !pci_is_thunderbolt_attached(to_pci_dev(dev->dev)))
2233 adev->flags |= AMD_IS_PX;
2235 if (!(adev->flags & AMD_IS_APU)) {
2236 parent = pci_upstream_bridge(adev->pdev);
2237 adev->has_pr3 = parent ? pci_pr3_present(parent) : false;
2241 adev->pm.pp_feature = amdgpu_pp_feature_mask;
2242 if (amdgpu_sriov_vf(adev) || sched_policy == KFD_SCHED_POLICY_NO_HWS)
2243 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
2244 if (amdgpu_sriov_vf(adev) && adev->asic_type == CHIP_SIENNA_CICHLID)
2245 adev->pm.pp_feature &= ~PP_OVERDRIVE_MASK;
2248 for (i = 0; i < adev->num_ip_blocks; i++) {
2249 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
2250 DRM_WARN("disabled ip block: %d <%s>\n",
2251 i, adev->ip_blocks[i].version->funcs->name);
2252 adev->ip_blocks[i].status.valid = false;
2254 if (adev->ip_blocks[i].version->funcs->early_init) {
2255 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
2257 adev->ip_blocks[i].status.valid = false;
2259 DRM_ERROR("early_init of IP block <%s> failed %d\n",
2260 adev->ip_blocks[i].version->funcs->name, r);
2263 adev->ip_blocks[i].status.valid = true;
2266 adev->ip_blocks[i].status.valid = true;
2269 /* get the vbios after the asic_funcs are set up */
2270 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) {
2271 r = amdgpu_device_parse_gpu_info_fw(adev);
2276 if (amdgpu_device_read_bios(adev)) {
2277 if (!amdgpu_get_bios(adev))
2280 r = amdgpu_atombios_init(adev);
2282 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
2283 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
2288 /*get pf2vf msg info at it's earliest time*/
2289 if (amdgpu_sriov_vf(adev))
2290 amdgpu_virt_init_data_exchange(adev);
2297 amdgpu_amdkfd_device_probe(adev);
2298 adev->cg_flags &= amdgpu_cg_mask;
2299 adev->pg_flags &= amdgpu_pg_mask;
2304 static int amdgpu_device_ip_hw_init_phase1(struct amdgpu_device *adev)
2308 for (i = 0; i < adev->num_ip_blocks; i++) {
2309 if (!adev->ip_blocks[i].status.sw)
2311 if (adev->ip_blocks[i].status.hw)
2313 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2314 (amdgpu_sriov_vf(adev) && (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)) ||
2315 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
2316 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2318 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2319 adev->ip_blocks[i].version->funcs->name, r);
2322 adev->ip_blocks[i].status.hw = true;
2329 static int amdgpu_device_ip_hw_init_phase2(struct amdgpu_device *adev)
2333 for (i = 0; i < adev->num_ip_blocks; i++) {
2334 if (!adev->ip_blocks[i].status.sw)
2336 if (adev->ip_blocks[i].status.hw)
2338 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2340 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2341 adev->ip_blocks[i].version->funcs->name, r);
2344 adev->ip_blocks[i].status.hw = true;
2350 static int amdgpu_device_fw_loading(struct amdgpu_device *adev)
2354 uint32_t smu_version;
2356 if (adev->asic_type >= CHIP_VEGA10) {
2357 for (i = 0; i < adev->num_ip_blocks; i++) {
2358 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_PSP)
2361 if (!adev->ip_blocks[i].status.sw)
2364 /* no need to do the fw loading again if already done*/
2365 if (adev->ip_blocks[i].status.hw == true)
2368 if (amdgpu_in_reset(adev) || adev->in_suspend) {
2369 r = adev->ip_blocks[i].version->funcs->resume(adev);
2371 DRM_ERROR("resume of IP block <%s> failed %d\n",
2372 adev->ip_blocks[i].version->funcs->name, r);
2376 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2378 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2379 adev->ip_blocks[i].version->funcs->name, r);
2384 adev->ip_blocks[i].status.hw = true;
2389 if (!amdgpu_sriov_vf(adev) || adev->asic_type == CHIP_TONGA)
2390 r = amdgpu_pm_load_smu_firmware(adev, &smu_version);
2395 static int amdgpu_device_init_schedulers(struct amdgpu_device *adev)
2400 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2401 struct amdgpu_ring *ring = adev->rings[i];
2403 /* No need to setup the GPU scheduler for rings that don't need it */
2404 if (!ring || ring->no_scheduler)
2407 switch (ring->funcs->type) {
2408 case AMDGPU_RING_TYPE_GFX:
2409 timeout = adev->gfx_timeout;
2411 case AMDGPU_RING_TYPE_COMPUTE:
2412 timeout = adev->compute_timeout;
2414 case AMDGPU_RING_TYPE_SDMA:
2415 timeout = adev->sdma_timeout;
2418 timeout = adev->video_timeout;
2422 r = drm_sched_init(&ring->sched, &amdgpu_sched_ops,
2423 ring->num_hw_submission, 0,
2424 timeout, adev->reset_domain->wq,
2425 ring->sched_score, ring->name,
2428 DRM_ERROR("Failed to create scheduler on ring %s.\n",
2434 amdgpu_xcp_update_partition_sched_list(adev);
2441 * amdgpu_device_ip_init - run init for hardware IPs
2443 * @adev: amdgpu_device pointer
2445 * Main initialization pass for hardware IPs. The list of all the hardware
2446 * IPs that make up the asic is walked and the sw_init and hw_init callbacks
2447 * are run. sw_init initializes the software state associated with each IP
2448 * and hw_init initializes the hardware associated with each IP.
2449 * Returns 0 on success, negative error code on failure.
2451 static int amdgpu_device_ip_init(struct amdgpu_device *adev)
2455 r = amdgpu_ras_init(adev);
2459 for (i = 0; i < adev->num_ip_blocks; i++) {
2460 if (!adev->ip_blocks[i].status.valid)
2462 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
2464 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
2465 adev->ip_blocks[i].version->funcs->name, r);
2468 adev->ip_blocks[i].status.sw = true;
2470 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) {
2471 /* need to do common hw init early so everything is set up for gmc */
2472 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
2474 DRM_ERROR("hw_init %d failed %d\n", i, r);
2477 adev->ip_blocks[i].status.hw = true;
2478 } else if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
2479 /* need to do gmc hw init early so we can allocate gpu mem */
2480 /* Try to reserve bad pages early */
2481 if (amdgpu_sriov_vf(adev))
2482 amdgpu_virt_exchange_data(adev);
2484 r = amdgpu_device_mem_scratch_init(adev);
2486 DRM_ERROR("amdgpu_mem_scratch_init failed %d\n", r);
2489 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
2491 DRM_ERROR("hw_init %d failed %d\n", i, r);
2494 r = amdgpu_device_wb_init(adev);
2496 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
2499 adev->ip_blocks[i].status.hw = true;
2501 /* right after GMC hw init, we create CSA */
2502 if (adev->gfx.mcbp) {
2503 r = amdgpu_allocate_static_csa(adev, &adev->virt.csa_obj,
2504 AMDGPU_GEM_DOMAIN_VRAM |
2505 AMDGPU_GEM_DOMAIN_GTT,
2508 DRM_ERROR("allocate CSA failed %d\n", r);
2515 if (amdgpu_sriov_vf(adev))
2516 amdgpu_virt_init_data_exchange(adev);
2518 r = amdgpu_ib_pool_init(adev);
2520 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
2521 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
2525 r = amdgpu_ucode_create_bo(adev); /* create ucode bo when sw_init complete*/
2529 r = amdgpu_device_ip_hw_init_phase1(adev);
2533 r = amdgpu_device_fw_loading(adev);
2537 r = amdgpu_device_ip_hw_init_phase2(adev);
2542 * retired pages will be loaded from eeprom and reserved here,
2543 * it should be called after amdgpu_device_ip_hw_init_phase2 since
2544 * for some ASICs the RAS EEPROM code relies on SMU fully functioning
2545 * for I2C communication which only true at this point.
2547 * amdgpu_ras_recovery_init may fail, but the upper only cares the
2548 * failure from bad gpu situation and stop amdgpu init process
2549 * accordingly. For other failed cases, it will still release all
2550 * the resource and print error message, rather than returning one
2551 * negative value to upper level.
2553 * Note: theoretically, this should be called before all vram allocations
2554 * to protect retired page from abusing
2556 r = amdgpu_ras_recovery_init(adev);
2561 * In case of XGMI grab extra reference for reset domain for this device
2563 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2564 if (amdgpu_xgmi_add_device(adev) == 0) {
2565 if (!amdgpu_sriov_vf(adev)) {
2566 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
2568 if (WARN_ON(!hive)) {
2573 if (!hive->reset_domain ||
2574 !amdgpu_reset_get_reset_domain(hive->reset_domain)) {
2576 amdgpu_put_xgmi_hive(hive);
2580 /* Drop the early temporary reset domain we created for device */
2581 amdgpu_reset_put_reset_domain(adev->reset_domain);
2582 adev->reset_domain = hive->reset_domain;
2583 amdgpu_put_xgmi_hive(hive);
2588 r = amdgpu_device_init_schedulers(adev);
2592 /* Don't init kfd if whole hive need to be reset during init */
2593 if (!adev->gmc.xgmi.pending_reset) {
2594 kgd2kfd_init_zone_device(adev);
2595 amdgpu_amdkfd_device_init(adev);
2598 amdgpu_fru_get_product_info(adev);
2606 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
2608 * @adev: amdgpu_device pointer
2610 * Writes a reset magic value to the gart pointer in VRAM. The driver calls
2611 * this function before a GPU reset. If the value is retained after a
2612 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
2614 static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
2616 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
2620 * amdgpu_device_check_vram_lost - check if vram is valid
2622 * @adev: amdgpu_device pointer
2624 * Checks the reset magic value written to the gart pointer in VRAM.
2625 * The driver calls this after a GPU reset to see if the contents of
2626 * VRAM is lost or now.
2627 * returns true if vram is lost, false if not.
2629 static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
2631 if (memcmp(adev->gart.ptr, adev->reset_magic,
2632 AMDGPU_RESET_MAGIC_NUM))
2635 if (!amdgpu_in_reset(adev))
2639 * For all ASICs with baco/mode1 reset, the VRAM is
2640 * always assumed to be lost.
2642 switch (amdgpu_asic_reset_method(adev)) {
2643 case AMD_RESET_METHOD_BACO:
2644 case AMD_RESET_METHOD_MODE1:
2652 * amdgpu_device_set_cg_state - set clockgating for amdgpu device
2654 * @adev: amdgpu_device pointer
2655 * @state: clockgating state (gate or ungate)
2657 * The list of all the hardware IPs that make up the asic is walked and the
2658 * set_clockgating_state callbacks are run.
2659 * Late initialization pass enabling clockgating for hardware IPs.
2660 * Fini or suspend, pass disabling clockgating for hardware IPs.
2661 * Returns 0 on success, negative error code on failure.
2664 int amdgpu_device_set_cg_state(struct amdgpu_device *adev,
2665 enum amd_clockgating_state state)
2669 if (amdgpu_emu_mode == 1)
2672 for (j = 0; j < adev->num_ip_blocks; j++) {
2673 i = state == AMD_CG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
2674 if (!adev->ip_blocks[i].status.late_initialized)
2676 /* skip CG for GFX, SDMA on S0ix */
2677 if (adev->in_s0ix &&
2678 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX ||
2679 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SDMA))
2681 /* skip CG for VCE/UVD, it's handled specially */
2682 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
2683 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
2684 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
2685 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
2686 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
2687 /* enable clockgating to save power */
2688 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
2691 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
2692 adev->ip_blocks[i].version->funcs->name, r);
2701 int amdgpu_device_set_pg_state(struct amdgpu_device *adev,
2702 enum amd_powergating_state state)
2706 if (amdgpu_emu_mode == 1)
2709 for (j = 0; j < adev->num_ip_blocks; j++) {
2710 i = state == AMD_PG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
2711 if (!adev->ip_blocks[i].status.late_initialized)
2713 /* skip PG for GFX, SDMA on S0ix */
2714 if (adev->in_s0ix &&
2715 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX ||
2716 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SDMA))
2718 /* skip CG for VCE/UVD, it's handled specially */
2719 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
2720 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
2721 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
2722 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
2723 adev->ip_blocks[i].version->funcs->set_powergating_state) {
2724 /* enable powergating to save power */
2725 r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
2728 DRM_ERROR("set_powergating_state(gate) of IP block <%s> failed %d\n",
2729 adev->ip_blocks[i].version->funcs->name, r);
2737 static int amdgpu_device_enable_mgpu_fan_boost(void)
2739 struct amdgpu_gpu_instance *gpu_ins;
2740 struct amdgpu_device *adev;
2743 mutex_lock(&mgpu_info.mutex);
2746 * MGPU fan boost feature should be enabled
2747 * only when there are two or more dGPUs in
2750 if (mgpu_info.num_dgpu < 2)
2753 for (i = 0; i < mgpu_info.num_dgpu; i++) {
2754 gpu_ins = &(mgpu_info.gpu_ins[i]);
2755 adev = gpu_ins->adev;
2756 if (!(adev->flags & AMD_IS_APU) &&
2757 !gpu_ins->mgpu_fan_enabled) {
2758 ret = amdgpu_dpm_enable_mgpu_fan_boost(adev);
2762 gpu_ins->mgpu_fan_enabled = 1;
2767 mutex_unlock(&mgpu_info.mutex);
2773 * amdgpu_device_ip_late_init - run late init for hardware IPs
2775 * @adev: amdgpu_device pointer
2777 * Late initialization pass for hardware IPs. The list of all the hardware
2778 * IPs that make up the asic is walked and the late_init callbacks are run.
2779 * late_init covers any special initialization that an IP requires
2780 * after all of the have been initialized or something that needs to happen
2781 * late in the init process.
2782 * Returns 0 on success, negative error code on failure.
2784 static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
2786 struct amdgpu_gpu_instance *gpu_instance;
2789 for (i = 0; i < adev->num_ip_blocks; i++) {
2790 if (!adev->ip_blocks[i].status.hw)
2792 if (adev->ip_blocks[i].version->funcs->late_init) {
2793 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
2795 DRM_ERROR("late_init of IP block <%s> failed %d\n",
2796 adev->ip_blocks[i].version->funcs->name, r);
2800 adev->ip_blocks[i].status.late_initialized = true;
2803 r = amdgpu_ras_late_init(adev);
2805 DRM_ERROR("amdgpu_ras_late_init failed %d", r);
2809 amdgpu_ras_set_error_query_ready(adev, true);
2811 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE);
2812 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_GATE);
2814 amdgpu_device_fill_reset_magic(adev);
2816 r = amdgpu_device_enable_mgpu_fan_boost();
2818 DRM_ERROR("enable mgpu fan boost failed (%d).\n", r);
2820 /* For passthrough configuration on arcturus and aldebaran, enable special handling SBR */
2821 if (amdgpu_passthrough(adev) &&
2822 ((adev->asic_type == CHIP_ARCTURUS && adev->gmc.xgmi.num_physical_nodes > 1) ||
2823 adev->asic_type == CHIP_ALDEBARAN))
2824 amdgpu_dpm_handle_passthrough_sbr(adev, true);
2826 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2827 mutex_lock(&mgpu_info.mutex);
2830 * Reset device p-state to low as this was booted with high.
2832 * This should be performed only after all devices from the same
2833 * hive get initialized.
2835 * However, it's unknown how many device in the hive in advance.
2836 * As this is counted one by one during devices initializations.
2838 * So, we wait for all XGMI interlinked devices initialized.
2839 * This may bring some delays as those devices may come from
2840 * different hives. But that should be OK.
2842 if (mgpu_info.num_dgpu == adev->gmc.xgmi.num_physical_nodes) {
2843 for (i = 0; i < mgpu_info.num_gpu; i++) {
2844 gpu_instance = &(mgpu_info.gpu_ins[i]);
2845 if (gpu_instance->adev->flags & AMD_IS_APU)
2848 r = amdgpu_xgmi_set_pstate(gpu_instance->adev,
2849 AMDGPU_XGMI_PSTATE_MIN);
2851 DRM_ERROR("pstate setting failed (%d).\n", r);
2857 mutex_unlock(&mgpu_info.mutex);
2864 * amdgpu_device_smu_fini_early - smu hw_fini wrapper
2866 * @adev: amdgpu_device pointer
2868 * For ASICs need to disable SMC first
2870 static void amdgpu_device_smu_fini_early(struct amdgpu_device *adev)
2874 if (adev->ip_versions[GC_HWIP][0] > IP_VERSION(9, 0, 0))
2877 for (i = 0; i < adev->num_ip_blocks; i++) {
2878 if (!adev->ip_blocks[i].status.hw)
2880 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
2881 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2882 /* XXX handle errors */
2884 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2885 adev->ip_blocks[i].version->funcs->name, r);
2887 adev->ip_blocks[i].status.hw = false;
2893 static int amdgpu_device_ip_fini_early(struct amdgpu_device *adev)
2897 for (i = 0; i < adev->num_ip_blocks; i++) {
2898 if (!adev->ip_blocks[i].version->funcs->early_fini)
2901 r = adev->ip_blocks[i].version->funcs->early_fini((void *)adev);
2903 DRM_DEBUG("early_fini of IP block <%s> failed %d\n",
2904 adev->ip_blocks[i].version->funcs->name, r);
2908 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2909 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2911 amdgpu_amdkfd_suspend(adev, false);
2913 /* Workaroud for ASICs need to disable SMC first */
2914 amdgpu_device_smu_fini_early(adev);
2916 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2917 if (!adev->ip_blocks[i].status.hw)
2920 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2921 /* XXX handle errors */
2923 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2924 adev->ip_blocks[i].version->funcs->name, r);
2927 adev->ip_blocks[i].status.hw = false;
2930 if (amdgpu_sriov_vf(adev)) {
2931 if (amdgpu_virt_release_full_gpu(adev, false))
2932 DRM_ERROR("failed to release exclusive mode on fini\n");
2939 * amdgpu_device_ip_fini - run fini for hardware IPs
2941 * @adev: amdgpu_device pointer
2943 * Main teardown pass for hardware IPs. The list of all the hardware
2944 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
2945 * are run. hw_fini tears down the hardware associated with each IP
2946 * and sw_fini tears down any software state associated with each IP.
2947 * Returns 0 on success, negative error code on failure.
2949 static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
2953 if (amdgpu_sriov_vf(adev) && adev->virt.ras_init_done)
2954 amdgpu_virt_release_ras_err_handler_data(adev);
2956 if (adev->gmc.xgmi.num_physical_nodes > 1)
2957 amdgpu_xgmi_remove_device(adev);
2959 amdgpu_amdkfd_device_fini_sw(adev);
2961 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2962 if (!adev->ip_blocks[i].status.sw)
2965 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
2966 amdgpu_ucode_free_bo(adev);
2967 amdgpu_free_static_csa(&adev->virt.csa_obj);
2968 amdgpu_device_wb_fini(adev);
2969 amdgpu_device_mem_scratch_fini(adev);
2970 amdgpu_ib_pool_fini(adev);
2973 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
2974 /* XXX handle errors */
2976 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
2977 adev->ip_blocks[i].version->funcs->name, r);
2979 adev->ip_blocks[i].status.sw = false;
2980 adev->ip_blocks[i].status.valid = false;
2983 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2984 if (!adev->ip_blocks[i].status.late_initialized)
2986 if (adev->ip_blocks[i].version->funcs->late_fini)
2987 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
2988 adev->ip_blocks[i].status.late_initialized = false;
2991 amdgpu_ras_fini(adev);
2997 * amdgpu_device_delayed_init_work_handler - work handler for IB tests
2999 * @work: work_struct.
3001 static void amdgpu_device_delayed_init_work_handler(struct work_struct *work)
3003 struct amdgpu_device *adev =
3004 container_of(work, struct amdgpu_device, delayed_init_work.work);
3007 r = amdgpu_ib_ring_tests(adev);
3009 DRM_ERROR("ib ring test failed (%d).\n", r);
3012 static void amdgpu_device_delay_enable_gfx_off(struct work_struct *work)
3014 struct amdgpu_device *adev =
3015 container_of(work, struct amdgpu_device, gfx.gfx_off_delay_work.work);
3017 WARN_ON_ONCE(adev->gfx.gfx_off_state);
3018 WARN_ON_ONCE(adev->gfx.gfx_off_req_count);
3020 if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true))
3021 adev->gfx.gfx_off_state = true;
3025 * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1)
3027 * @adev: amdgpu_device pointer
3029 * Main suspend function for hardware IPs. The list of all the hardware
3030 * IPs that make up the asic is walked, clockgating is disabled and the
3031 * suspend callbacks are run. suspend puts the hardware and software state
3032 * in each IP into a state suitable for suspend.
3033 * Returns 0 on success, negative error code on failure.
3035 static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev)
3039 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
3040 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
3043 * Per PMFW team's suggestion, driver needs to handle gfxoff
3044 * and df cstate features disablement for gpu reset(e.g. Mode1Reset)
3045 * scenario. Add the missing df cstate disablement here.
3047 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_DISALLOW))
3048 dev_warn(adev->dev, "Failed to disallow df cstate");
3050 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
3051 if (!adev->ip_blocks[i].status.valid)
3054 /* displays are handled separately */
3055 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_DCE)
3058 /* XXX handle errors */
3059 r = adev->ip_blocks[i].version->funcs->suspend(adev);
3060 /* XXX handle errors */
3062 DRM_ERROR("suspend of IP block <%s> failed %d\n",
3063 adev->ip_blocks[i].version->funcs->name, r);
3067 adev->ip_blocks[i].status.hw = false;
3074 * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2)
3076 * @adev: amdgpu_device pointer
3078 * Main suspend function for hardware IPs. The list of all the hardware
3079 * IPs that make up the asic is walked, clockgating is disabled and the
3080 * suspend callbacks are run. suspend puts the hardware and software state
3081 * in each IP into a state suitable for suspend.
3082 * Returns 0 on success, negative error code on failure.
3084 static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev)
3089 amdgpu_dpm_gfx_state_change(adev, sGpuChangeState_D3Entry);
3091 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
3092 if (!adev->ip_blocks[i].status.valid)
3094 /* displays are handled in phase1 */
3095 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)
3097 /* PSP lost connection when err_event_athub occurs */
3098 if (amdgpu_ras_intr_triggered() &&
3099 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
3100 adev->ip_blocks[i].status.hw = false;
3104 /* skip unnecessary suspend if we do not initialize them yet */
3105 if (adev->gmc.xgmi.pending_reset &&
3106 !(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3107 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC ||
3108 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3109 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)) {
3110 adev->ip_blocks[i].status.hw = false;
3114 /* skip suspend of gfx/mes and psp for S0ix
3115 * gfx is in gfxoff state, so on resume it will exit gfxoff just
3116 * like at runtime. PSP is also part of the always on hardware
3117 * so no need to suspend it.
3119 if (adev->in_s0ix &&
3120 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP ||
3121 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX ||
3122 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_MES))
3125 /* SDMA 5.x+ is part of GFX power domain so it's covered by GFXOFF */
3126 if (adev->in_s0ix &&
3127 (adev->ip_versions[SDMA0_HWIP][0] >= IP_VERSION(5, 0, 0)) &&
3128 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SDMA))
3131 /* Once swPSP provides the IMU, RLC FW binaries to TOS during cold-boot.
3132 * These are in TMR, hence are expected to be reused by PSP-TOS to reload
3133 * from this location and RLC Autoload automatically also gets loaded
3134 * from here based on PMFW -> PSP message during re-init sequence.
3135 * Therefore, the psp suspend & resume should be skipped to avoid destroy
3136 * the TMR and reload FWs again for IMU enabled APU ASICs.
3138 if (amdgpu_in_reset(adev) &&
3139 (adev->flags & AMD_IS_APU) && adev->gfx.imu.funcs &&
3140 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)
3143 /* XXX handle errors */
3144 r = adev->ip_blocks[i].version->funcs->suspend(adev);
3145 /* XXX handle errors */
3147 DRM_ERROR("suspend of IP block <%s> failed %d\n",
3148 adev->ip_blocks[i].version->funcs->name, r);
3150 adev->ip_blocks[i].status.hw = false;
3151 /* handle putting the SMC in the appropriate state */
3152 if (!amdgpu_sriov_vf(adev)) {
3153 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
3154 r = amdgpu_dpm_set_mp1_state(adev, adev->mp1_state);
3156 DRM_ERROR("SMC failed to set mp1 state %d, %d\n",
3157 adev->mp1_state, r);
3168 * amdgpu_device_ip_suspend - run suspend for hardware IPs
3170 * @adev: amdgpu_device pointer
3172 * Main suspend function for hardware IPs. The list of all the hardware
3173 * IPs that make up the asic is walked, clockgating is disabled and the
3174 * suspend callbacks are run. suspend puts the hardware and software state
3175 * in each IP into a state suitable for suspend.
3176 * Returns 0 on success, negative error code on failure.
3178 int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
3182 if (amdgpu_sriov_vf(adev)) {
3183 amdgpu_virt_fini_data_exchange(adev);
3184 amdgpu_virt_request_full_gpu(adev, false);
3187 r = amdgpu_device_ip_suspend_phase1(adev);
3190 r = amdgpu_device_ip_suspend_phase2(adev);
3192 if (amdgpu_sriov_vf(adev))
3193 amdgpu_virt_release_full_gpu(adev, false);
3198 static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
3202 static enum amd_ip_block_type ip_order[] = {
3203 AMD_IP_BLOCK_TYPE_COMMON,
3204 AMD_IP_BLOCK_TYPE_GMC,
3205 AMD_IP_BLOCK_TYPE_PSP,
3206 AMD_IP_BLOCK_TYPE_IH,
3209 for (i = 0; i < adev->num_ip_blocks; i++) {
3211 struct amdgpu_ip_block *block;
3213 block = &adev->ip_blocks[i];
3214 block->status.hw = false;
3216 for (j = 0; j < ARRAY_SIZE(ip_order); j++) {
3218 if (block->version->type != ip_order[j] ||
3219 !block->status.valid)
3222 r = block->version->funcs->hw_init(adev);
3223 DRM_INFO("RE-INIT-early: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
3226 block->status.hw = true;
3233 static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
3237 static enum amd_ip_block_type ip_order[] = {
3238 AMD_IP_BLOCK_TYPE_SMC,
3239 AMD_IP_BLOCK_TYPE_DCE,
3240 AMD_IP_BLOCK_TYPE_GFX,
3241 AMD_IP_BLOCK_TYPE_SDMA,
3242 AMD_IP_BLOCK_TYPE_MES,
3243 AMD_IP_BLOCK_TYPE_UVD,
3244 AMD_IP_BLOCK_TYPE_VCE,
3245 AMD_IP_BLOCK_TYPE_VCN,
3246 AMD_IP_BLOCK_TYPE_JPEG
3249 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
3251 struct amdgpu_ip_block *block;
3253 for (j = 0; j < adev->num_ip_blocks; j++) {
3254 block = &adev->ip_blocks[j];
3256 if (block->version->type != ip_order[i] ||
3257 !block->status.valid ||
3261 if (block->version->type == AMD_IP_BLOCK_TYPE_SMC)
3262 r = block->version->funcs->resume(adev);
3264 r = block->version->funcs->hw_init(adev);
3266 DRM_INFO("RE-INIT-late: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
3269 block->status.hw = true;
3277 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
3279 * @adev: amdgpu_device pointer
3281 * First resume function for hardware IPs. The list of all the hardware
3282 * IPs that make up the asic is walked and the resume callbacks are run for
3283 * COMMON, GMC, and IH. resume puts the hardware into a functional state
3284 * after a suspend and updates the software state as necessary. This
3285 * function is also used for restoring the GPU after a GPU reset.
3286 * Returns 0 on success, negative error code on failure.
3288 static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
3292 for (i = 0; i < adev->num_ip_blocks; i++) {
3293 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
3295 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3296 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3297 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3298 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP && amdgpu_sriov_vf(adev))) {
3300 r = adev->ip_blocks[i].version->funcs->resume(adev);
3302 DRM_ERROR("resume of IP block <%s> failed %d\n",
3303 adev->ip_blocks[i].version->funcs->name, r);
3306 adev->ip_blocks[i].status.hw = true;
3314 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
3316 * @adev: amdgpu_device pointer
3318 * First resume function for hardware IPs. The list of all the hardware
3319 * IPs that make up the asic is walked and the resume callbacks are run for
3320 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
3321 * functional state after a suspend and updates the software state as
3322 * necessary. This function is also used for restoring the GPU after a GPU
3324 * Returns 0 on success, negative error code on failure.
3326 static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
3330 for (i = 0; i < adev->num_ip_blocks; i++) {
3331 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
3333 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3334 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3335 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3336 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)
3338 r = adev->ip_blocks[i].version->funcs->resume(adev);
3340 DRM_ERROR("resume of IP block <%s> failed %d\n",
3341 adev->ip_blocks[i].version->funcs->name, r);
3344 adev->ip_blocks[i].status.hw = true;
3351 * amdgpu_device_ip_resume - run resume for hardware IPs
3353 * @adev: amdgpu_device pointer
3355 * Main resume function for hardware IPs. The hardware IPs
3356 * are split into two resume functions because they are
3357 * also used in recovering from a GPU reset and some additional
3358 * steps need to be take between them. In this case (S3/S4) they are
3360 * Returns 0 on success, negative error code on failure.
3362 static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
3366 r = amdgpu_device_ip_resume_phase1(adev);
3370 r = amdgpu_device_fw_loading(adev);
3374 r = amdgpu_device_ip_resume_phase2(adev);
3380 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
3382 * @adev: amdgpu_device pointer
3384 * Query the VBIOS data tables to determine if the board supports SR-IOV.
3386 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
3388 if (amdgpu_sriov_vf(adev)) {
3389 if (adev->is_atom_fw) {
3390 if (amdgpu_atomfirmware_gpu_virtualization_supported(adev))
3391 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
3393 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
3394 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
3397 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
3398 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
3403 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
3405 * @asic_type: AMD asic type
3407 * Check if there is DC (new modesetting infrastructre) support for an asic.
3408 * returns true if DC has support, false if not.
3410 bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
3412 switch (asic_type) {
3413 #ifdef CONFIG_DRM_AMDGPU_SI
3417 /* chips with no display hardware */
3419 #if defined(CONFIG_DRM_AMD_DC)
3425 * We have systems in the wild with these ASICs that require
3426 * LVDS and VGA support which is not supported with DC.
3428 * Fallback to the non-DC driver here by default so as not to
3429 * cause regressions.
3431 #if defined(CONFIG_DRM_AMD_DC_SI)
3432 return amdgpu_dc > 0;
3441 * We have systems in the wild with these ASICs that require
3442 * VGA support which is not supported with DC.
3444 * Fallback to the non-DC driver here by default so as not to
3445 * cause regressions.
3447 return amdgpu_dc > 0;
3449 return amdgpu_dc != 0;
3453 DRM_INFO_ONCE("Display Core has been requested via kernel parameter but isn't supported by ASIC, ignoring\n");
3460 * amdgpu_device_has_dc_support - check if dc is supported
3462 * @adev: amdgpu_device pointer
3464 * Returns true for supported, false for not supported
3466 bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
3468 if (adev->enable_virtual_display ||
3469 (adev->harvest_ip_mask & AMD_HARVEST_IP_DMU_MASK))
3472 return amdgpu_device_asic_has_dc_support(adev->asic_type);
3475 static void amdgpu_device_xgmi_reset_func(struct work_struct *__work)
3477 struct amdgpu_device *adev =
3478 container_of(__work, struct amdgpu_device, xgmi_reset_work);
3479 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
3481 /* It's a bug to not have a hive within this function */
3486 * Use task barrier to synchronize all xgmi reset works across the
3487 * hive. task_barrier_enter and task_barrier_exit will block
3488 * until all the threads running the xgmi reset works reach
3489 * those points. task_barrier_full will do both blocks.
3491 if (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) {
3493 task_barrier_enter(&hive->tb);
3494 adev->asic_reset_res = amdgpu_device_baco_enter(adev_to_drm(adev));
3496 if (adev->asic_reset_res)
3499 task_barrier_exit(&hive->tb);
3500 adev->asic_reset_res = amdgpu_device_baco_exit(adev_to_drm(adev));
3502 if (adev->asic_reset_res)
3505 if (adev->mmhub.ras && adev->mmhub.ras->ras_block.hw_ops &&
3506 adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count)
3507 adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count(adev);
3510 task_barrier_full(&hive->tb);
3511 adev->asic_reset_res = amdgpu_asic_reset(adev);
3515 if (adev->asic_reset_res)
3516 DRM_WARN("ASIC reset failed with error, %d for drm dev, %s",
3517 adev->asic_reset_res, adev_to_drm(adev)->unique);
3518 amdgpu_put_xgmi_hive(hive);
3521 static int amdgpu_device_get_job_timeout_settings(struct amdgpu_device *adev)
3523 char *input = amdgpu_lockup_timeout;
3524 char *timeout_setting = NULL;
3530 * By default timeout for non compute jobs is 10000
3531 * and 60000 for compute jobs.
3532 * In SR-IOV or passthrough mode, timeout for compute
3533 * jobs are 60000 by default.
3535 adev->gfx_timeout = msecs_to_jiffies(10000);
3536 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
3537 if (amdgpu_sriov_vf(adev))
3538 adev->compute_timeout = amdgpu_sriov_is_pp_one_vf(adev) ?
3539 msecs_to_jiffies(60000) : msecs_to_jiffies(10000);
3541 adev->compute_timeout = msecs_to_jiffies(60000);
3543 if (strnlen(input, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
3544 while ((timeout_setting = strsep(&input, ",")) &&
3545 strnlen(timeout_setting, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
3546 ret = kstrtol(timeout_setting, 0, &timeout);
3553 } else if (timeout < 0) {
3554 timeout = MAX_SCHEDULE_TIMEOUT;
3555 dev_warn(adev->dev, "lockup timeout disabled");
3556 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
3558 timeout = msecs_to_jiffies(timeout);
3563 adev->gfx_timeout = timeout;
3566 adev->compute_timeout = timeout;
3569 adev->sdma_timeout = timeout;
3572 adev->video_timeout = timeout;
3579 * There is only one value specified and
3580 * it should apply to all non-compute jobs.
3583 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
3584 if (amdgpu_sriov_vf(adev) || amdgpu_passthrough(adev))
3585 adev->compute_timeout = adev->gfx_timeout;
3593 * amdgpu_device_check_iommu_direct_map - check if RAM direct mapped to GPU
3595 * @adev: amdgpu_device pointer
3597 * RAM direct mapped to GPU if IOMMU is not enabled or is pass through mode
3599 static void amdgpu_device_check_iommu_direct_map(struct amdgpu_device *adev)
3601 struct iommu_domain *domain;
3603 domain = iommu_get_domain_for_dev(adev->dev);
3604 if (!domain || domain->type == IOMMU_DOMAIN_IDENTITY)
3605 adev->ram_is_direct_mapped = true;
3608 static const struct attribute *amdgpu_dev_attributes[] = {
3609 &dev_attr_pcie_replay_count.attr,
3613 static void amdgpu_device_set_mcbp(struct amdgpu_device *adev)
3615 if (amdgpu_mcbp == 1)
3616 adev->gfx.mcbp = true;
3617 else if (amdgpu_mcbp == 0)
3618 adev->gfx.mcbp = false;
3619 else if ((adev->ip_versions[GC_HWIP][0] >= IP_VERSION(9, 0, 0)) &&
3620 (adev->ip_versions[GC_HWIP][0] < IP_VERSION(10, 0, 0)) &&
3621 adev->gfx.num_gfx_rings)
3622 adev->gfx.mcbp = true;
3624 if (amdgpu_sriov_vf(adev))
3625 adev->gfx.mcbp = true;
3628 DRM_INFO("MCBP is enabled\n");
3632 * amdgpu_device_init - initialize the driver
3634 * @adev: amdgpu_device pointer
3635 * @flags: driver flags
3637 * Initializes the driver info and hw (all asics).
3638 * Returns 0 for success or an error on failure.
3639 * Called at driver startup.
3641 int amdgpu_device_init(struct amdgpu_device *adev,
3644 struct drm_device *ddev = adev_to_drm(adev);
3645 struct pci_dev *pdev = adev->pdev;
3651 adev->shutdown = false;
3652 adev->flags = flags;
3654 if (amdgpu_force_asic_type >= 0 && amdgpu_force_asic_type < CHIP_LAST)
3655 adev->asic_type = amdgpu_force_asic_type;
3657 adev->asic_type = flags & AMD_ASIC_MASK;
3659 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
3660 if (amdgpu_emu_mode == 1)
3661 adev->usec_timeout *= 10;
3662 adev->gmc.gart_size = 512 * 1024 * 1024;
3663 adev->accel_working = false;
3664 adev->num_rings = 0;
3665 RCU_INIT_POINTER(adev->gang_submit, dma_fence_get_stub());
3666 adev->mman.buffer_funcs = NULL;
3667 adev->mman.buffer_funcs_ring = NULL;
3668 adev->vm_manager.vm_pte_funcs = NULL;
3669 adev->vm_manager.vm_pte_num_scheds = 0;
3670 adev->gmc.gmc_funcs = NULL;
3671 adev->harvest_ip_mask = 0x0;
3672 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
3673 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
3675 adev->smc_rreg = &amdgpu_invalid_rreg;
3676 adev->smc_wreg = &amdgpu_invalid_wreg;
3677 adev->pcie_rreg = &amdgpu_invalid_rreg;
3678 adev->pcie_wreg = &amdgpu_invalid_wreg;
3679 adev->pcie_rreg_ext = &amdgpu_invalid_rreg_ext;
3680 adev->pcie_wreg_ext = &amdgpu_invalid_wreg_ext;
3681 adev->pciep_rreg = &amdgpu_invalid_rreg;
3682 adev->pciep_wreg = &amdgpu_invalid_wreg;
3683 adev->pcie_rreg64 = &amdgpu_invalid_rreg64;
3684 adev->pcie_wreg64 = &amdgpu_invalid_wreg64;
3685 adev->pcie_rreg64_ext = &amdgpu_invalid_rreg64_ext;
3686 adev->pcie_wreg64_ext = &amdgpu_invalid_wreg64_ext;
3687 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
3688 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
3689 adev->didt_rreg = &amdgpu_invalid_rreg;
3690 adev->didt_wreg = &amdgpu_invalid_wreg;
3691 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
3692 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
3693 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
3694 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
3696 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
3697 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
3698 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
3700 /* mutex initialization are all done here so we
3701 * can recall function without having locking issues
3703 mutex_init(&adev->firmware.mutex);
3704 mutex_init(&adev->pm.mutex);
3705 mutex_init(&adev->gfx.gpu_clock_mutex);
3706 mutex_init(&adev->srbm_mutex);
3707 mutex_init(&adev->gfx.pipe_reserve_mutex);
3708 mutex_init(&adev->gfx.gfx_off_mutex);
3709 mutex_init(&adev->gfx.partition_mutex);
3710 mutex_init(&adev->grbm_idx_mutex);
3711 mutex_init(&adev->mn_lock);
3712 mutex_init(&adev->virt.vf_errors.lock);
3713 hash_init(adev->mn_hash);
3714 mutex_init(&adev->psp.mutex);
3715 mutex_init(&adev->notifier_lock);
3716 mutex_init(&adev->pm.stable_pstate_ctx_lock);
3717 mutex_init(&adev->benchmark_mutex);
3719 amdgpu_device_init_apu_flags(adev);
3721 r = amdgpu_device_check_arguments(adev);
3725 spin_lock_init(&adev->mmio_idx_lock);
3726 spin_lock_init(&adev->smc_idx_lock);
3727 spin_lock_init(&adev->pcie_idx_lock);
3728 spin_lock_init(&adev->uvd_ctx_idx_lock);
3729 spin_lock_init(&adev->didt_idx_lock);
3730 spin_lock_init(&adev->gc_cac_idx_lock);
3731 spin_lock_init(&adev->se_cac_idx_lock);
3732 spin_lock_init(&adev->audio_endpt_idx_lock);
3733 spin_lock_init(&adev->mm_stats.lock);
3735 INIT_LIST_HEAD(&adev->shadow_list);
3736 mutex_init(&adev->shadow_list_lock);
3738 INIT_LIST_HEAD(&adev->reset_list);
3740 INIT_LIST_HEAD(&adev->ras_list);
3742 INIT_LIST_HEAD(&adev->pm.od_kobj_list);
3744 INIT_DELAYED_WORK(&adev->delayed_init_work,
3745 amdgpu_device_delayed_init_work_handler);
3746 INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work,
3747 amdgpu_device_delay_enable_gfx_off);
3749 INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
3751 adev->gfx.gfx_off_req_count = 1;
3752 adev->gfx.gfx_off_residency = 0;
3753 adev->gfx.gfx_off_entrycount = 0;
3754 adev->pm.ac_power = power_supply_is_system_supplied() > 0;
3756 atomic_set(&adev->throttling_logging_enabled, 1);
3758 * If throttling continues, logging will be performed every minute
3759 * to avoid log flooding. "-1" is subtracted since the thermal
3760 * throttling interrupt comes every second. Thus, the total logging
3761 * interval is 59 seconds(retelimited printk interval) + 1(waiting
3762 * for throttling interrupt) = 60 seconds.
3764 ratelimit_state_init(&adev->throttling_logging_rs, (60 - 1) * HZ, 1);
3765 ratelimit_set_flags(&adev->throttling_logging_rs, RATELIMIT_MSG_ON_RELEASE);
3767 /* Registers mapping */
3768 /* TODO: block userspace mapping of io register */
3769 if (adev->asic_type >= CHIP_BONAIRE) {
3770 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
3771 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
3773 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
3774 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
3777 for (i = 0; i < AMD_IP_BLOCK_TYPE_NUM; i++)
3778 atomic_set(&adev->pm.pwr_state[i], POWER_STATE_UNKNOWN);
3780 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
3784 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
3785 DRM_INFO("register mmio size: %u\n", (unsigned int)adev->rmmio_size);
3788 * Reset domain needs to be present early, before XGMI hive discovered
3789 * (if any) and intitialized to use reset sem and in_gpu reset flag
3790 * early on during init and before calling to RREG32.
3792 adev->reset_domain = amdgpu_reset_create_reset_domain(SINGLE_DEVICE, "amdgpu-reset-dev");
3793 if (!adev->reset_domain)
3796 /* detect hw virtualization here */
3797 amdgpu_detect_virtualization(adev);
3799 amdgpu_device_get_pcie_info(adev);
3801 r = amdgpu_device_get_job_timeout_settings(adev);
3803 dev_err(adev->dev, "invalid lockup_timeout parameter syntax\n");
3807 /* early init functions */
3808 r = amdgpu_device_ip_early_init(adev);
3812 amdgpu_device_set_mcbp(adev);
3814 /* Get rid of things like offb */
3815 r = drm_aperture_remove_conflicting_pci_framebuffers(adev->pdev, &amdgpu_kms_driver);
3819 /* Enable TMZ based on IP_VERSION */
3820 amdgpu_gmc_tmz_set(adev);
3822 amdgpu_gmc_noretry_set(adev);
3823 /* Need to get xgmi info early to decide the reset behavior*/
3824 if (adev->gmc.xgmi.supported) {
3825 r = adev->gfxhub.funcs->get_xgmi_info(adev);
3830 /* enable PCIE atomic ops */
3831 if (amdgpu_sriov_vf(adev)) {
3832 if (adev->virt.fw_reserve.p_pf2vf)
3833 adev->have_atomics_support = ((struct amd_sriov_msg_pf2vf_info *)
3834 adev->virt.fw_reserve.p_pf2vf)->pcie_atomic_ops_support_flags ==
3835 (PCI_EXP_DEVCAP2_ATOMIC_COMP32 | PCI_EXP_DEVCAP2_ATOMIC_COMP64);
3836 /* APUs w/ gfx9 onwards doesn't reply on PCIe atomics, rather it is a
3837 * internal path natively support atomics, set have_atomics_support to true.
3839 } else if ((adev->flags & AMD_IS_APU) &&
3840 (adev->ip_versions[GC_HWIP][0] > IP_VERSION(9, 0, 0))) {
3841 adev->have_atomics_support = true;
3843 adev->have_atomics_support =
3844 !pci_enable_atomic_ops_to_root(adev->pdev,
3845 PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
3846 PCI_EXP_DEVCAP2_ATOMIC_COMP64);
3849 if (!adev->have_atomics_support)
3850 dev_info(adev->dev, "PCIE atomic ops is not supported\n");
3852 /* doorbell bar mapping and doorbell index init*/
3853 amdgpu_doorbell_init(adev);
3855 if (amdgpu_emu_mode == 1) {
3856 /* post the asic on emulation mode */
3857 emu_soc_asic_init(adev);
3858 goto fence_driver_init;
3861 amdgpu_reset_init(adev);
3863 /* detect if we are with an SRIOV vbios */
3865 amdgpu_device_detect_sriov_bios(adev);
3867 /* check if we need to reset the asic
3868 * E.g., driver was not cleanly unloaded previously, etc.
3870 if (!amdgpu_sriov_vf(adev) && amdgpu_asic_need_reset_on_init(adev)) {
3871 if (adev->gmc.xgmi.num_physical_nodes) {
3872 dev_info(adev->dev, "Pending hive reset.\n");
3873 adev->gmc.xgmi.pending_reset = true;
3874 /* Only need to init necessary block for SMU to handle the reset */
3875 for (i = 0; i < adev->num_ip_blocks; i++) {
3876 if (!adev->ip_blocks[i].status.valid)
3878 if (!(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3879 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3880 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3881 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC)) {
3882 DRM_DEBUG("IP %s disabled for hw_init.\n",
3883 adev->ip_blocks[i].version->funcs->name);
3884 adev->ip_blocks[i].status.hw = true;
3888 tmp = amdgpu_reset_method;
3889 /* It should do a default reset when loading or reloading the driver,
3890 * regardless of the module parameter reset_method.
3892 amdgpu_reset_method = AMD_RESET_METHOD_NONE;
3893 r = amdgpu_asic_reset(adev);
3894 amdgpu_reset_method = tmp;
3896 dev_err(adev->dev, "asic reset on init failed\n");
3902 /* Post card if necessary */
3903 if (amdgpu_device_need_post(adev)) {
3905 dev_err(adev->dev, "no vBIOS found\n");
3909 DRM_INFO("GPU posting now...\n");
3910 r = amdgpu_device_asic_init(adev);
3912 dev_err(adev->dev, "gpu post error!\n");
3918 if (adev->is_atom_fw) {
3919 /* Initialize clocks */
3920 r = amdgpu_atomfirmware_get_clock_info(adev);
3922 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
3923 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3927 /* Initialize clocks */
3928 r = amdgpu_atombios_get_clock_info(adev);
3930 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
3931 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3934 /* init i2c buses */
3935 if (!amdgpu_device_has_dc_support(adev))
3936 amdgpu_atombios_i2c_init(adev);
3942 r = amdgpu_fence_driver_sw_init(adev);
3944 dev_err(adev->dev, "amdgpu_fence_driver_sw_init failed\n");
3945 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
3949 /* init the mode config */
3950 drm_mode_config_init(adev_to_drm(adev));
3952 r = amdgpu_device_ip_init(adev);
3954 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
3955 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
3956 goto release_ras_con;
3959 amdgpu_fence_driver_hw_init(adev);
3962 "SE %d, SH per SE %d, CU per SH %d, active_cu_number %d\n",
3963 adev->gfx.config.max_shader_engines,
3964 adev->gfx.config.max_sh_per_se,
3965 adev->gfx.config.max_cu_per_sh,
3966 adev->gfx.cu_info.number);
3968 adev->accel_working = true;
3970 amdgpu_vm_check_compute_bug(adev);
3972 /* Initialize the buffer migration limit. */
3973 if (amdgpu_moverate >= 0)
3974 max_MBps = amdgpu_moverate;
3976 max_MBps = 8; /* Allow 8 MB/s. */
3977 /* Get a log2 for easy divisions. */
3978 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
3981 * Register gpu instance before amdgpu_device_enable_mgpu_fan_boost.
3982 * Otherwise the mgpu fan boost feature will be skipped due to the
3983 * gpu instance is counted less.
3985 amdgpu_register_gpu_instance(adev);
3987 /* enable clockgating, etc. after ib tests, etc. since some blocks require
3988 * explicit gating rather than handling it automatically.
3990 if (!adev->gmc.xgmi.pending_reset) {
3991 r = amdgpu_device_ip_late_init(adev);
3993 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
3994 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
3995 goto release_ras_con;
3998 amdgpu_ras_resume(adev);
3999 queue_delayed_work(system_wq, &adev->delayed_init_work,
4000 msecs_to_jiffies(AMDGPU_RESUME_MS));
4003 if (amdgpu_sriov_vf(adev)) {
4004 amdgpu_virt_release_full_gpu(adev, true);
4005 flush_delayed_work(&adev->delayed_init_work);
4009 * Place those sysfs registering after `late_init`. As some of those
4010 * operations performed in `late_init` might affect the sysfs
4011 * interfaces creating.
4013 r = amdgpu_atombios_sysfs_init(adev);
4015 drm_err(&adev->ddev,
4016 "registering atombios sysfs failed (%d).\n", r);
4018 r = amdgpu_pm_sysfs_init(adev);
4020 DRM_ERROR("registering pm sysfs failed (%d).\n", r);
4022 r = amdgpu_ucode_sysfs_init(adev);
4024 adev->ucode_sysfs_en = false;
4025 DRM_ERROR("Creating firmware sysfs failed (%d).\n", r);
4027 adev->ucode_sysfs_en = true;
4029 r = sysfs_create_files(&adev->dev->kobj, amdgpu_dev_attributes);
4031 dev_err(adev->dev, "Could not create amdgpu device attr\n");
4033 amdgpu_fru_sysfs_init(adev);
4035 if (IS_ENABLED(CONFIG_PERF_EVENTS))
4036 r = amdgpu_pmu_init(adev);
4038 dev_err(adev->dev, "amdgpu_pmu_init failed\n");
4040 /* Have stored pci confspace at hand for restore in sudden PCI error */
4041 if (amdgpu_device_cache_pci_state(adev->pdev))
4042 pci_restore_state(pdev);
4044 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
4045 /* this will fail for cards that aren't VGA class devices, just
4048 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
4049 vga_client_register(adev->pdev, amdgpu_device_vga_set_decode);
4051 px = amdgpu_device_supports_px(ddev);
4053 if (px || (!pci_is_thunderbolt_attached(adev->pdev) &&
4054 apple_gmux_detect(NULL, NULL)))
4055 vga_switcheroo_register_client(adev->pdev,
4056 &amdgpu_switcheroo_ops, px);
4059 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
4061 if (adev->gmc.xgmi.pending_reset)
4062 queue_delayed_work(system_wq, &mgpu_info.delayed_reset_work,
4063 msecs_to_jiffies(AMDGPU_RESUME_MS));
4065 amdgpu_device_check_iommu_direct_map(adev);
4070 if (amdgpu_sriov_vf(adev))
4071 amdgpu_virt_release_full_gpu(adev, true);
4073 /* failed in exclusive mode due to timeout */
4074 if (amdgpu_sriov_vf(adev) &&
4075 !amdgpu_sriov_runtime(adev) &&
4076 amdgpu_virt_mmio_blocked(adev) &&
4077 !amdgpu_virt_wait_reset(adev)) {
4078 dev_err(adev->dev, "VF exclusive mode timeout\n");
4079 /* Don't send request since VF is inactive. */
4080 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
4081 adev->virt.ops = NULL;
4084 amdgpu_release_ras_context(adev);
4087 amdgpu_vf_error_trans_all(adev);
4092 static void amdgpu_device_unmap_mmio(struct amdgpu_device *adev)
4095 /* Clear all CPU mappings pointing to this device */
4096 unmap_mapping_range(adev->ddev.anon_inode->i_mapping, 0, 0, 1);
4098 /* Unmap all mapped bars - Doorbell, registers and VRAM */
4099 amdgpu_doorbell_fini(adev);
4101 iounmap(adev->rmmio);
4103 if (adev->mman.aper_base_kaddr)
4104 iounmap(adev->mman.aper_base_kaddr);
4105 adev->mman.aper_base_kaddr = NULL;
4107 /* Memory manager related */
4108 if (!adev->gmc.xgmi.connected_to_cpu && !adev->gmc.is_app_apu) {
4109 arch_phys_wc_del(adev->gmc.vram_mtrr);
4110 arch_io_free_memtype_wc(adev->gmc.aper_base, adev->gmc.aper_size);
4115 * amdgpu_device_fini_hw - tear down the driver
4117 * @adev: amdgpu_device pointer
4119 * Tear down the driver info (all asics).
4120 * Called at driver shutdown.
4122 void amdgpu_device_fini_hw(struct amdgpu_device *adev)
4124 dev_info(adev->dev, "amdgpu: finishing device.\n");
4125 flush_delayed_work(&adev->delayed_init_work);
4126 adev->shutdown = true;
4128 /* make sure IB test finished before entering exclusive mode
4129 * to avoid preemption on IB test
4131 if (amdgpu_sriov_vf(adev)) {
4132 amdgpu_virt_request_full_gpu(adev, false);
4133 amdgpu_virt_fini_data_exchange(adev);
4136 /* disable all interrupts */
4137 amdgpu_irq_disable_all(adev);
4138 if (adev->mode_info.mode_config_initialized) {
4139 if (!drm_drv_uses_atomic_modeset(adev_to_drm(adev)))
4140 drm_helper_force_disable_all(adev_to_drm(adev));
4142 drm_atomic_helper_shutdown(adev_to_drm(adev));
4144 amdgpu_fence_driver_hw_fini(adev);
4146 if (adev->mman.initialized)
4147 drain_workqueue(adev->mman.bdev.wq);
4149 if (adev->pm.sysfs_initialized)
4150 amdgpu_pm_sysfs_fini(adev);
4151 if (adev->ucode_sysfs_en)
4152 amdgpu_ucode_sysfs_fini(adev);
4153 sysfs_remove_files(&adev->dev->kobj, amdgpu_dev_attributes);
4154 amdgpu_fru_sysfs_fini(adev);
4156 /* disable ras feature must before hw fini */
4157 amdgpu_ras_pre_fini(adev);
4159 amdgpu_device_ip_fini_early(adev);
4161 amdgpu_irq_fini_hw(adev);
4163 if (adev->mman.initialized)
4164 ttm_device_clear_dma_mappings(&adev->mman.bdev);
4166 amdgpu_gart_dummy_page_fini(adev);
4168 if (drm_dev_is_unplugged(adev_to_drm(adev)))
4169 amdgpu_device_unmap_mmio(adev);
4173 void amdgpu_device_fini_sw(struct amdgpu_device *adev)
4178 amdgpu_fence_driver_sw_fini(adev);
4179 amdgpu_device_ip_fini(adev);
4180 amdgpu_ucode_release(&adev->firmware.gpu_info_fw);
4181 adev->accel_working = false;
4182 dma_fence_put(rcu_dereference_protected(adev->gang_submit, true));
4184 amdgpu_reset_fini(adev);
4186 /* free i2c buses */
4187 if (!amdgpu_device_has_dc_support(adev))
4188 amdgpu_i2c_fini(adev);
4190 if (amdgpu_emu_mode != 1)
4191 amdgpu_atombios_fini(adev);
4196 px = amdgpu_device_supports_px(adev_to_drm(adev));
4198 if (px || (!pci_is_thunderbolt_attached(adev->pdev) &&
4199 apple_gmux_detect(NULL, NULL)))
4200 vga_switcheroo_unregister_client(adev->pdev);
4203 vga_switcheroo_fini_domain_pm_ops(adev->dev);
4205 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
4206 vga_client_unregister(adev->pdev);
4208 if (drm_dev_enter(adev_to_drm(adev), &idx)) {
4210 iounmap(adev->rmmio);
4212 amdgpu_doorbell_fini(adev);
4216 if (IS_ENABLED(CONFIG_PERF_EVENTS))
4217 amdgpu_pmu_fini(adev);
4218 if (adev->mman.discovery_bin)
4219 amdgpu_discovery_fini(adev);
4221 amdgpu_reset_put_reset_domain(adev->reset_domain);
4222 adev->reset_domain = NULL;
4224 kfree(adev->pci_state);
4229 * amdgpu_device_evict_resources - evict device resources
4230 * @adev: amdgpu device object
4232 * Evicts all ttm device resources(vram BOs, gart table) from the lru list
4233 * of the vram memory type. Mainly used for evicting device resources
4237 static int amdgpu_device_evict_resources(struct amdgpu_device *adev)
4241 /* No need to evict vram on APUs for suspend to ram or s2idle */
4242 if ((adev->in_s3 || adev->in_s0ix) && (adev->flags & AMD_IS_APU))
4245 ret = amdgpu_ttm_evict_resources(adev, TTM_PL_VRAM);
4247 DRM_WARN("evicting device resources failed\n");
4255 * amdgpu_device_suspend - initiate device suspend
4257 * @dev: drm dev pointer
4258 * @fbcon : notify the fbdev of suspend
4260 * Puts the hw in the suspend state (all asics).
4261 * Returns 0 for success or an error on failure.
4262 * Called at driver suspend.
4264 int amdgpu_device_suspend(struct drm_device *dev, bool fbcon)
4266 struct amdgpu_device *adev = drm_to_adev(dev);
4269 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
4272 adev->in_suspend = true;
4274 /* Evict the majority of BOs before grabbing the full access */
4275 r = amdgpu_device_evict_resources(adev);
4279 if (amdgpu_sriov_vf(adev)) {
4280 amdgpu_virt_fini_data_exchange(adev);
4281 r = amdgpu_virt_request_full_gpu(adev, false);
4286 if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DEV_D3))
4287 DRM_WARN("smart shift update failed\n");
4290 drm_fb_helper_set_suspend_unlocked(adev_to_drm(adev)->fb_helper, true);
4292 cancel_delayed_work_sync(&adev->delayed_init_work);
4293 flush_delayed_work(&adev->gfx.gfx_off_delay_work);
4295 amdgpu_ras_suspend(adev);
4297 amdgpu_device_ip_suspend_phase1(adev);
4300 amdgpu_amdkfd_suspend(adev, adev->in_runpm);
4302 r = amdgpu_device_evict_resources(adev);
4306 amdgpu_fence_driver_hw_fini(adev);
4308 amdgpu_device_ip_suspend_phase2(adev);
4310 if (amdgpu_sriov_vf(adev))
4311 amdgpu_virt_release_full_gpu(adev, false);
4317 * amdgpu_device_resume - initiate device resume
4319 * @dev: drm dev pointer
4320 * @fbcon : notify the fbdev of resume
4322 * Bring the hw back to operating state (all asics).
4323 * Returns 0 for success or an error on failure.
4324 * Called at driver resume.
4326 int amdgpu_device_resume(struct drm_device *dev, bool fbcon)
4328 struct amdgpu_device *adev = drm_to_adev(dev);
4331 if (amdgpu_sriov_vf(adev)) {
4332 r = amdgpu_virt_request_full_gpu(adev, true);
4337 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
4341 amdgpu_dpm_gfx_state_change(adev, sGpuChangeState_D0Entry);
4344 if (amdgpu_device_need_post(adev)) {
4345 r = amdgpu_device_asic_init(adev);
4347 dev_err(adev->dev, "amdgpu asic init failed\n");
4350 r = amdgpu_device_ip_resume(adev);
4353 dev_err(adev->dev, "amdgpu_device_ip_resume failed (%d).\n", r);
4356 amdgpu_fence_driver_hw_init(adev);
4358 r = amdgpu_device_ip_late_init(adev);
4362 queue_delayed_work(system_wq, &adev->delayed_init_work,
4363 msecs_to_jiffies(AMDGPU_RESUME_MS));
4365 if (!adev->in_s0ix) {
4366 r = amdgpu_amdkfd_resume(adev, adev->in_runpm);
4372 if (amdgpu_sriov_vf(adev)) {
4373 amdgpu_virt_init_data_exchange(adev);
4374 amdgpu_virt_release_full_gpu(adev, true);
4380 /* Make sure IB tests flushed */
4381 flush_delayed_work(&adev->delayed_init_work);
4384 drm_fb_helper_set_suspend_unlocked(adev_to_drm(adev)->fb_helper, false);
4386 amdgpu_ras_resume(adev);
4388 if (adev->mode_info.num_crtc) {
4390 * Most of the connector probing functions try to acquire runtime pm
4391 * refs to ensure that the GPU is powered on when connector polling is
4392 * performed. Since we're calling this from a runtime PM callback,
4393 * trying to acquire rpm refs will cause us to deadlock.
4395 * Since we're guaranteed to be holding the rpm lock, it's safe to
4396 * temporarily disable the rpm helpers so this doesn't deadlock us.
4399 dev->dev->power.disable_depth++;
4401 if (!adev->dc_enabled)
4402 drm_helper_hpd_irq_event(dev);
4404 drm_kms_helper_hotplug_event(dev);
4406 dev->dev->power.disable_depth--;
4409 adev->in_suspend = false;
4411 if (adev->enable_mes)
4412 amdgpu_mes_self_test(adev);
4414 if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DEV_D0))
4415 DRM_WARN("smart shift update failed\n");
4421 * amdgpu_device_ip_check_soft_reset - did soft reset succeed
4423 * @adev: amdgpu_device pointer
4425 * The list of all the hardware IPs that make up the asic is walked and
4426 * the check_soft_reset callbacks are run. check_soft_reset determines
4427 * if the asic is still hung or not.
4428 * Returns true if any of the IPs are still in a hung state, false if not.
4430 static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
4433 bool asic_hang = false;
4435 if (amdgpu_sriov_vf(adev))
4438 if (amdgpu_asic_need_full_reset(adev))
4441 for (i = 0; i < adev->num_ip_blocks; i++) {
4442 if (!adev->ip_blocks[i].status.valid)
4444 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
4445 adev->ip_blocks[i].status.hang =
4446 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
4447 if (adev->ip_blocks[i].status.hang) {
4448 dev_info(adev->dev, "IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
4456 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
4458 * @adev: amdgpu_device pointer
4460 * The list of all the hardware IPs that make up the asic is walked and the
4461 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
4462 * handles any IP specific hardware or software state changes that are
4463 * necessary for a soft reset to succeed.
4464 * Returns 0 on success, negative error code on failure.
4466 static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
4470 for (i = 0; i < adev->num_ip_blocks; i++) {
4471 if (!adev->ip_blocks[i].status.valid)
4473 if (adev->ip_blocks[i].status.hang &&
4474 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
4475 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
4485 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
4487 * @adev: amdgpu_device pointer
4489 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
4490 * reset is necessary to recover.
4491 * Returns true if a full asic reset is required, false if not.
4493 static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
4497 if (amdgpu_asic_need_full_reset(adev))
4500 for (i = 0; i < adev->num_ip_blocks; i++) {
4501 if (!adev->ip_blocks[i].status.valid)
4503 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
4504 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
4505 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
4506 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
4507 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
4508 if (adev->ip_blocks[i].status.hang) {
4509 dev_info(adev->dev, "Some block need full reset!\n");
4518 * amdgpu_device_ip_soft_reset - do a soft reset
4520 * @adev: amdgpu_device pointer
4522 * The list of all the hardware IPs that make up the asic is walked and the
4523 * soft_reset callbacks are run if the block is hung. soft_reset handles any
4524 * IP specific hardware or software state changes that are necessary to soft
4526 * Returns 0 on success, negative error code on failure.
4528 static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
4532 for (i = 0; i < adev->num_ip_blocks; i++) {
4533 if (!adev->ip_blocks[i].status.valid)
4535 if (adev->ip_blocks[i].status.hang &&
4536 adev->ip_blocks[i].version->funcs->soft_reset) {
4537 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
4547 * amdgpu_device_ip_post_soft_reset - clean up from soft reset
4549 * @adev: amdgpu_device pointer
4551 * The list of all the hardware IPs that make up the asic is walked and the
4552 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
4553 * handles any IP specific hardware or software state changes that are
4554 * necessary after the IP has been soft reset.
4555 * Returns 0 on success, negative error code on failure.
4557 static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
4561 for (i = 0; i < adev->num_ip_blocks; i++) {
4562 if (!adev->ip_blocks[i].status.valid)
4564 if (adev->ip_blocks[i].status.hang &&
4565 adev->ip_blocks[i].version->funcs->post_soft_reset)
4566 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
4575 * amdgpu_device_recover_vram - Recover some VRAM contents
4577 * @adev: amdgpu_device pointer
4579 * Restores the contents of VRAM buffers from the shadows in GTT. Used to
4580 * restore things like GPUVM page tables after a GPU reset where
4581 * the contents of VRAM might be lost.
4584 * 0 on success, negative error code on failure.
4586 static int amdgpu_device_recover_vram(struct amdgpu_device *adev)
4588 struct dma_fence *fence = NULL, *next = NULL;
4589 struct amdgpu_bo *shadow;
4590 struct amdgpu_bo_vm *vmbo;
4593 if (amdgpu_sriov_runtime(adev))
4594 tmo = msecs_to_jiffies(8000);
4596 tmo = msecs_to_jiffies(100);
4598 dev_info(adev->dev, "recover vram bo from shadow start\n");
4599 mutex_lock(&adev->shadow_list_lock);
4600 list_for_each_entry(vmbo, &adev->shadow_list, shadow_list) {
4601 /* If vm is compute context or adev is APU, shadow will be NULL */
4604 shadow = vmbo->shadow;
4606 /* No need to recover an evicted BO */
4607 if (shadow->tbo.resource->mem_type != TTM_PL_TT ||
4608 shadow->tbo.resource->start == AMDGPU_BO_INVALID_OFFSET ||
4609 shadow->parent->tbo.resource->mem_type != TTM_PL_VRAM)
4612 r = amdgpu_bo_restore_shadow(shadow, &next);
4617 tmo = dma_fence_wait_timeout(fence, false, tmo);
4618 dma_fence_put(fence);
4623 } else if (tmo < 0) {
4631 mutex_unlock(&adev->shadow_list_lock);
4634 tmo = dma_fence_wait_timeout(fence, false, tmo);
4635 dma_fence_put(fence);
4637 if (r < 0 || tmo <= 0) {
4638 dev_err(adev->dev, "recover vram bo from shadow failed, r is %ld, tmo is %ld\n", r, tmo);
4642 dev_info(adev->dev, "recover vram bo from shadow done\n");
4648 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
4650 * @adev: amdgpu_device pointer
4651 * @from_hypervisor: request from hypervisor
4653 * do VF FLR and reinitialize Asic
4654 * return 0 means succeeded otherwise failed
4656 static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
4657 bool from_hypervisor)
4660 struct amdgpu_hive_info *hive = NULL;
4661 int retry_limit = 0;
4664 amdgpu_amdkfd_pre_reset(adev);
4666 if (from_hypervisor)
4667 r = amdgpu_virt_request_full_gpu(adev, true);
4669 r = amdgpu_virt_reset_gpu(adev);
4672 amdgpu_irq_gpu_reset_resume_helper(adev);
4674 /* some sw clean up VF needs to do before recover */
4675 amdgpu_virt_post_reset(adev);
4677 /* Resume IP prior to SMC */
4678 r = amdgpu_device_ip_reinit_early_sriov(adev);
4682 amdgpu_virt_init_data_exchange(adev);
4684 r = amdgpu_device_fw_loading(adev);
4688 /* now we are okay to resume SMC/CP/SDMA */
4689 r = amdgpu_device_ip_reinit_late_sriov(adev);
4693 hive = amdgpu_get_xgmi_hive(adev);
4694 /* Update PSP FW topology after reset */
4695 if (hive && adev->gmc.xgmi.num_physical_nodes > 1)
4696 r = amdgpu_xgmi_update_topology(hive, adev);
4699 amdgpu_put_xgmi_hive(hive);
4702 r = amdgpu_ib_ring_tests(adev);
4704 amdgpu_amdkfd_post_reset(adev);
4708 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
4709 amdgpu_inc_vram_lost(adev);
4710 r = amdgpu_device_recover_vram(adev);
4712 amdgpu_virt_release_full_gpu(adev, true);
4714 if (AMDGPU_RETRY_SRIOV_RESET(r)) {
4715 if (retry_limit < AMDGPU_MAX_RETRY_LIMIT) {
4719 DRM_ERROR("GPU reset retry is beyond the retry limit\n");
4726 * amdgpu_device_has_job_running - check if there is any job in mirror list
4728 * @adev: amdgpu_device pointer
4730 * check if there is any job in mirror list
4732 bool amdgpu_device_has_job_running(struct amdgpu_device *adev)
4735 struct drm_sched_job *job;
4737 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4738 struct amdgpu_ring *ring = adev->rings[i];
4740 if (!ring || !ring->sched.thread)
4743 spin_lock(&ring->sched.job_list_lock);
4744 job = list_first_entry_or_null(&ring->sched.pending_list,
4745 struct drm_sched_job, list);
4746 spin_unlock(&ring->sched.job_list_lock);
4754 * amdgpu_device_should_recover_gpu - check if we should try GPU recovery
4756 * @adev: amdgpu_device pointer
4758 * Check amdgpu_gpu_recovery and SRIOV status to see if we should try to recover
4761 bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev)
4764 if (amdgpu_gpu_recovery == 0)
4767 /* Skip soft reset check in fatal error mode */
4768 if (!amdgpu_ras_is_poison_mode_supported(adev))
4771 if (amdgpu_sriov_vf(adev))
4774 if (amdgpu_gpu_recovery == -1) {
4775 switch (adev->asic_type) {
4776 #ifdef CONFIG_DRM_AMDGPU_SI
4783 #ifdef CONFIG_DRM_AMDGPU_CIK
4790 case CHIP_CYAN_SKILLFISH:
4800 dev_info(adev->dev, "GPU recovery disabled.\n");
4804 int amdgpu_device_mode1_reset(struct amdgpu_device *adev)
4809 amdgpu_atombios_scratch_regs_engine_hung(adev, true);
4811 dev_info(adev->dev, "GPU mode1 reset\n");
4814 pci_clear_master(adev->pdev);
4816 amdgpu_device_cache_pci_state(adev->pdev);
4818 if (amdgpu_dpm_is_mode1_reset_supported(adev)) {
4819 dev_info(adev->dev, "GPU smu mode1 reset\n");
4820 ret = amdgpu_dpm_mode1_reset(adev);
4822 dev_info(adev->dev, "GPU psp mode1 reset\n");
4823 ret = psp_gpu_reset(adev);
4827 goto mode1_reset_failed;
4829 amdgpu_device_load_pci_state(adev->pdev);
4830 ret = amdgpu_psp_wait_for_bootloader(adev);
4832 goto mode1_reset_failed;
4834 /* wait for asic to come out of reset */
4835 for (i = 0; i < adev->usec_timeout; i++) {
4836 u32 memsize = adev->nbio.funcs->get_memsize(adev);
4838 if (memsize != 0xffffffff)
4843 if (i >= adev->usec_timeout) {
4845 goto mode1_reset_failed;
4848 amdgpu_atombios_scratch_regs_engine_hung(adev, false);
4853 dev_err(adev->dev, "GPU mode1 reset failed\n");
4857 int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
4858 struct amdgpu_reset_context *reset_context)
4861 struct amdgpu_job *job = NULL;
4862 bool need_full_reset =
4863 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4865 if (reset_context->reset_req_dev == adev)
4866 job = reset_context->job;
4868 if (amdgpu_sriov_vf(adev)) {
4869 /* stop the data exchange thread */
4870 amdgpu_virt_fini_data_exchange(adev);
4873 amdgpu_fence_driver_isr_toggle(adev, true);
4875 /* block all schedulers and reset given job's ring */
4876 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4877 struct amdgpu_ring *ring = adev->rings[i];
4879 if (!ring || !ring->sched.thread)
4882 /* Clear job fence from fence drv to avoid force_completion
4883 * leave NULL and vm flush fence in fence drv
4885 amdgpu_fence_driver_clear_job_fences(ring);
4887 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
4888 amdgpu_fence_driver_force_completion(ring);
4891 amdgpu_fence_driver_isr_toggle(adev, false);
4894 drm_sched_increase_karma(&job->base);
4896 r = amdgpu_reset_prepare_hwcontext(adev, reset_context);
4897 /* If reset handler not implemented, continue; otherwise return */
4898 if (r == -EOPNOTSUPP)
4903 /* Don't suspend on bare metal if we are not going to HW reset the ASIC */
4904 if (!amdgpu_sriov_vf(adev)) {
4906 if (!need_full_reset)
4907 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
4909 if (!need_full_reset && amdgpu_gpu_recovery &&
4910 amdgpu_device_ip_check_soft_reset(adev)) {
4911 amdgpu_device_ip_pre_soft_reset(adev);
4912 r = amdgpu_device_ip_soft_reset(adev);
4913 amdgpu_device_ip_post_soft_reset(adev);
4914 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
4915 dev_info(adev->dev, "soft reset failed, will fallback to full reset!\n");
4916 need_full_reset = true;
4920 if (need_full_reset)
4921 r = amdgpu_device_ip_suspend(adev);
4922 if (need_full_reset)
4923 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4925 clear_bit(AMDGPU_NEED_FULL_RESET,
4926 &reset_context->flags);
4932 static int amdgpu_reset_reg_dumps(struct amdgpu_device *adev)
4936 lockdep_assert_held(&adev->reset_domain->sem);
4938 for (i = 0; i < adev->num_regs; i++) {
4939 adev->reset_dump_reg_value[i] = RREG32(adev->reset_dump_reg_list[i]);
4940 trace_amdgpu_reset_reg_dumps(adev->reset_dump_reg_list[i],
4941 adev->reset_dump_reg_value[i]);
4947 #ifdef CONFIG_DEV_COREDUMP
4948 static ssize_t amdgpu_devcoredump_read(char *buffer, loff_t offset,
4949 size_t count, void *data, size_t datalen)
4951 struct drm_printer p;
4952 struct amdgpu_device *adev = data;
4953 struct drm_print_iterator iter;
4958 iter.start = offset;
4959 iter.remain = count;
4961 p = drm_coredump_printer(&iter);
4963 drm_printf(&p, "**** AMDGPU Device Coredump ****\n");
4964 drm_printf(&p, "kernel: " UTS_RELEASE "\n");
4965 drm_printf(&p, "module: " KBUILD_MODNAME "\n");
4966 drm_printf(&p, "time: %lld.%09ld\n", adev->reset_time.tv_sec, adev->reset_time.tv_nsec);
4967 if (adev->reset_task_info.pid)
4968 drm_printf(&p, "process_name: %s PID: %d\n",
4969 adev->reset_task_info.process_name,
4970 adev->reset_task_info.pid);
4972 if (adev->reset_vram_lost)
4973 drm_printf(&p, "VRAM is lost due to GPU reset!\n");
4974 if (adev->num_regs) {
4975 drm_printf(&p, "AMDGPU register dumps:\nOffset: Value:\n");
4977 for (i = 0; i < adev->num_regs; i++)
4978 drm_printf(&p, "0x%08x: 0x%08x\n",
4979 adev->reset_dump_reg_list[i],
4980 adev->reset_dump_reg_value[i]);
4983 return count - iter.remain;
4986 static void amdgpu_devcoredump_free(void *data)
4990 static void amdgpu_reset_capture_coredumpm(struct amdgpu_device *adev)
4992 struct drm_device *dev = adev_to_drm(adev);
4994 ktime_get_ts64(&adev->reset_time);
4995 dev_coredumpm(dev->dev, THIS_MODULE, adev, 0, GFP_NOWAIT,
4996 amdgpu_devcoredump_read, amdgpu_devcoredump_free);
5000 int amdgpu_do_asic_reset(struct list_head *device_list_handle,
5001 struct amdgpu_reset_context *reset_context)
5003 struct amdgpu_device *tmp_adev = NULL;
5004 bool need_full_reset, skip_hw_reset, vram_lost = false;
5006 bool gpu_reset_for_dev_remove = 0;
5008 /* Try reset handler method first */
5009 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
5011 amdgpu_reset_reg_dumps(tmp_adev);
5013 reset_context->reset_device_list = device_list_handle;
5014 r = amdgpu_reset_perform_reset(tmp_adev, reset_context);
5015 /* If reset handler not implemented, continue; otherwise return */
5016 if (r == -EOPNOTSUPP)
5021 /* Reset handler not implemented, use the default method */
5023 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
5024 skip_hw_reset = test_bit(AMDGPU_SKIP_HW_RESET, &reset_context->flags);
5026 gpu_reset_for_dev_remove =
5027 test_bit(AMDGPU_RESET_FOR_DEVICE_REMOVE, &reset_context->flags) &&
5028 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
5031 * ASIC reset has to be done on all XGMI hive nodes ASAP
5032 * to allow proper links negotiation in FW (within 1 sec)
5034 if (!skip_hw_reset && need_full_reset) {
5035 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5036 /* For XGMI run all resets in parallel to speed up the process */
5037 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
5038 tmp_adev->gmc.xgmi.pending_reset = false;
5039 if (!queue_work(system_unbound_wq, &tmp_adev->xgmi_reset_work))
5042 r = amdgpu_asic_reset(tmp_adev);
5045 dev_err(tmp_adev->dev, "ASIC reset failed with error, %d for drm dev, %s",
5046 r, adev_to_drm(tmp_adev)->unique);
5051 /* For XGMI wait for all resets to complete before proceed */
5053 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5054 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
5055 flush_work(&tmp_adev->xgmi_reset_work);
5056 r = tmp_adev->asic_reset_res;
5064 if (!r && amdgpu_ras_intr_triggered()) {
5065 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5066 if (tmp_adev->mmhub.ras && tmp_adev->mmhub.ras->ras_block.hw_ops &&
5067 tmp_adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count)
5068 tmp_adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count(tmp_adev);
5071 amdgpu_ras_intr_cleared();
5074 /* Since the mode1 reset affects base ip blocks, the
5075 * phase1 ip blocks need to be resumed. Otherwise there
5076 * will be a BIOS signature error and the psp bootloader
5077 * can't load kdb on the next amdgpu install.
5079 if (gpu_reset_for_dev_remove) {
5080 list_for_each_entry(tmp_adev, device_list_handle, reset_list)
5081 amdgpu_device_ip_resume_phase1(tmp_adev);
5086 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5087 if (need_full_reset) {
5089 r = amdgpu_device_asic_init(tmp_adev);
5091 dev_warn(tmp_adev->dev, "asic atom init failed!");
5093 dev_info(tmp_adev->dev, "GPU reset succeeded, trying to resume\n");
5095 r = amdgpu_device_ip_resume_phase1(tmp_adev);
5099 vram_lost = amdgpu_device_check_vram_lost(tmp_adev);
5100 #ifdef CONFIG_DEV_COREDUMP
5101 tmp_adev->reset_vram_lost = vram_lost;
5102 memset(&tmp_adev->reset_task_info, 0,
5103 sizeof(tmp_adev->reset_task_info));
5104 if (reset_context->job && reset_context->job->vm)
5105 tmp_adev->reset_task_info =
5106 reset_context->job->vm->task_info;
5107 amdgpu_reset_capture_coredumpm(tmp_adev);
5110 DRM_INFO("VRAM is lost due to GPU reset!\n");
5111 amdgpu_inc_vram_lost(tmp_adev);
5114 r = amdgpu_device_fw_loading(tmp_adev);
5118 r = amdgpu_device_ip_resume_phase2(tmp_adev);
5123 amdgpu_device_fill_reset_magic(tmp_adev);
5126 * Add this ASIC as tracked as reset was already
5127 * complete successfully.
5129 amdgpu_register_gpu_instance(tmp_adev);
5131 if (!reset_context->hive &&
5132 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
5133 amdgpu_xgmi_add_device(tmp_adev);
5135 r = amdgpu_device_ip_late_init(tmp_adev);
5139 drm_fb_helper_set_suspend_unlocked(adev_to_drm(tmp_adev)->fb_helper, false);
5142 * The GPU enters bad state once faulty pages
5143 * by ECC has reached the threshold, and ras
5144 * recovery is scheduled next. So add one check
5145 * here to break recovery if it indeed exceeds
5146 * bad page threshold, and remind user to
5147 * retire this GPU or setting one bigger
5148 * bad_page_threshold value to fix this once
5149 * probing driver again.
5151 if (!amdgpu_ras_eeprom_check_err_threshold(tmp_adev)) {
5153 amdgpu_ras_resume(tmp_adev);
5159 /* Update PSP FW topology after reset */
5160 if (reset_context->hive &&
5161 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
5162 r = amdgpu_xgmi_update_topology(
5163 reset_context->hive, tmp_adev);
5169 amdgpu_irq_gpu_reset_resume_helper(tmp_adev);
5170 r = amdgpu_ib_ring_tests(tmp_adev);
5172 dev_err(tmp_adev->dev, "ib ring test failed (%d).\n", r);
5173 need_full_reset = true;
5180 r = amdgpu_device_recover_vram(tmp_adev);
5182 tmp_adev->asic_reset_res = r;
5186 if (need_full_reset)
5187 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
5189 clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
5193 static void amdgpu_device_set_mp1_state(struct amdgpu_device *adev)
5196 switch (amdgpu_asic_reset_method(adev)) {
5197 case AMD_RESET_METHOD_MODE1:
5198 adev->mp1_state = PP_MP1_STATE_SHUTDOWN;
5200 case AMD_RESET_METHOD_MODE2:
5201 adev->mp1_state = PP_MP1_STATE_RESET;
5204 adev->mp1_state = PP_MP1_STATE_NONE;
5209 static void amdgpu_device_unset_mp1_state(struct amdgpu_device *adev)
5211 amdgpu_vf_error_trans_all(adev);
5212 adev->mp1_state = PP_MP1_STATE_NONE;
5215 static void amdgpu_device_resume_display_audio(struct amdgpu_device *adev)
5217 struct pci_dev *p = NULL;
5219 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
5220 adev->pdev->bus->number, 1);
5222 pm_runtime_enable(&(p->dev));
5223 pm_runtime_resume(&(p->dev));
5229 static int amdgpu_device_suspend_display_audio(struct amdgpu_device *adev)
5231 enum amd_reset_method reset_method;
5232 struct pci_dev *p = NULL;
5236 * For now, only BACO and mode1 reset are confirmed
5237 * to suffer the audio issue without proper suspended.
5239 reset_method = amdgpu_asic_reset_method(adev);
5240 if ((reset_method != AMD_RESET_METHOD_BACO) &&
5241 (reset_method != AMD_RESET_METHOD_MODE1))
5244 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
5245 adev->pdev->bus->number, 1);
5249 expires = pm_runtime_autosuspend_expiration(&(p->dev));
5252 * If we cannot get the audio device autosuspend delay,
5253 * a fixed 4S interval will be used. Considering 3S is
5254 * the audio controller default autosuspend delay setting.
5255 * 4S used here is guaranteed to cover that.
5257 expires = ktime_get_mono_fast_ns() + NSEC_PER_SEC * 4ULL;
5259 while (!pm_runtime_status_suspended(&(p->dev))) {
5260 if (!pm_runtime_suspend(&(p->dev)))
5263 if (expires < ktime_get_mono_fast_ns()) {
5264 dev_warn(adev->dev, "failed to suspend display audio\n");
5266 /* TODO: abort the succeeding gpu reset? */
5271 pm_runtime_disable(&(p->dev));
5277 static inline void amdgpu_device_stop_pending_resets(struct amdgpu_device *adev)
5279 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
5281 #if defined(CONFIG_DEBUG_FS)
5282 if (!amdgpu_sriov_vf(adev))
5283 cancel_work(&adev->reset_work);
5287 cancel_work(&adev->kfd.reset_work);
5289 if (amdgpu_sriov_vf(adev))
5290 cancel_work(&adev->virt.flr_work);
5292 if (con && adev->ras_enabled)
5293 cancel_work(&con->recovery_work);
5298 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
5300 * @adev: amdgpu_device pointer
5301 * @job: which job trigger hang
5302 * @reset_context: amdgpu reset context pointer
5304 * Attempt to reset the GPU if it has hung (all asics).
5305 * Attempt to do soft-reset or full-reset and reinitialize Asic
5306 * Returns 0 for success or an error on failure.
5309 int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
5310 struct amdgpu_job *job,
5311 struct amdgpu_reset_context *reset_context)
5313 struct list_head device_list, *device_list_handle = NULL;
5314 bool job_signaled = false;
5315 struct amdgpu_hive_info *hive = NULL;
5316 struct amdgpu_device *tmp_adev = NULL;
5318 bool need_emergency_restart = false;
5319 bool audio_suspended = false;
5320 bool gpu_reset_for_dev_remove = false;
5322 gpu_reset_for_dev_remove =
5323 test_bit(AMDGPU_RESET_FOR_DEVICE_REMOVE, &reset_context->flags) &&
5324 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
5327 * Special case: RAS triggered and full reset isn't supported
5329 need_emergency_restart = amdgpu_ras_need_emergency_restart(adev);
5332 * Flush RAM to disk so that after reboot
5333 * the user can read log and see why the system rebooted.
5335 if (need_emergency_restart && amdgpu_ras_get_context(adev)->reboot) {
5336 DRM_WARN("Emergency reboot.");
5339 emergency_restart();
5342 dev_info(adev->dev, "GPU %s begin!\n",
5343 need_emergency_restart ? "jobs stop":"reset");
5345 if (!amdgpu_sriov_vf(adev))
5346 hive = amdgpu_get_xgmi_hive(adev);
5348 mutex_lock(&hive->hive_lock);
5350 reset_context->job = job;
5351 reset_context->hive = hive;
5353 * Build list of devices to reset.
5354 * In case we are in XGMI hive mode, resort the device list
5355 * to put adev in the 1st position.
5357 INIT_LIST_HEAD(&device_list);
5358 if (!amdgpu_sriov_vf(adev) && (adev->gmc.xgmi.num_physical_nodes > 1)) {
5359 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
5360 list_add_tail(&tmp_adev->reset_list, &device_list);
5361 if (gpu_reset_for_dev_remove && adev->shutdown)
5362 tmp_adev->shutdown = true;
5364 if (!list_is_first(&adev->reset_list, &device_list))
5365 list_rotate_to_front(&adev->reset_list, &device_list);
5366 device_list_handle = &device_list;
5368 list_add_tail(&adev->reset_list, &device_list);
5369 device_list_handle = &device_list;
5372 /* We need to lock reset domain only once both for XGMI and single device */
5373 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
5375 amdgpu_device_lock_reset_domain(tmp_adev->reset_domain);
5377 /* block all schedulers and reset given job's ring */
5378 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5380 amdgpu_device_set_mp1_state(tmp_adev);
5383 * Try to put the audio codec into suspend state
5384 * before gpu reset started.
5386 * Due to the power domain of the graphics device
5387 * is shared with AZ power domain. Without this,
5388 * we may change the audio hardware from behind
5389 * the audio driver's back. That will trigger
5390 * some audio codec errors.
5392 if (!amdgpu_device_suspend_display_audio(tmp_adev))
5393 audio_suspended = true;
5395 amdgpu_ras_set_error_query_ready(tmp_adev, false);
5397 cancel_delayed_work_sync(&tmp_adev->delayed_init_work);
5399 if (!amdgpu_sriov_vf(tmp_adev))
5400 amdgpu_amdkfd_pre_reset(tmp_adev);
5403 * Mark these ASICs to be reseted as untracked first
5404 * And add them back after reset completed
5406 amdgpu_unregister_gpu_instance(tmp_adev);
5408 drm_fb_helper_set_suspend_unlocked(adev_to_drm(tmp_adev)->fb_helper, true);
5410 /* disable ras on ALL IPs */
5411 if (!need_emergency_restart &&
5412 amdgpu_device_ip_need_full_reset(tmp_adev))
5413 amdgpu_ras_suspend(tmp_adev);
5415 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5416 struct amdgpu_ring *ring = tmp_adev->rings[i];
5418 if (!ring || !ring->sched.thread)
5421 drm_sched_stop(&ring->sched, job ? &job->base : NULL);
5423 if (need_emergency_restart)
5424 amdgpu_job_stop_all_jobs_on_sched(&ring->sched);
5426 atomic_inc(&tmp_adev->gpu_reset_counter);
5429 if (need_emergency_restart)
5430 goto skip_sched_resume;
5433 * Must check guilty signal here since after this point all old
5434 * HW fences are force signaled.
5436 * job->base holds a reference to parent fence
5438 if (job && dma_fence_is_signaled(&job->hw_fence)) {
5439 job_signaled = true;
5440 dev_info(adev->dev, "Guilty job already signaled, skipping HW reset");
5444 retry: /* Rest of adevs pre asic reset from XGMI hive. */
5445 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5446 if (gpu_reset_for_dev_remove) {
5447 /* Workaroud for ASICs need to disable SMC first */
5448 amdgpu_device_smu_fini_early(tmp_adev);
5450 r = amdgpu_device_pre_asic_reset(tmp_adev, reset_context);
5451 /*TODO Should we stop ?*/
5453 dev_err(tmp_adev->dev, "GPU pre asic reset failed with err, %d for drm dev, %s ",
5454 r, adev_to_drm(tmp_adev)->unique);
5455 tmp_adev->asic_reset_res = r;
5459 * Drop all pending non scheduler resets. Scheduler resets
5460 * were already dropped during drm_sched_stop
5462 amdgpu_device_stop_pending_resets(tmp_adev);
5465 /* Actual ASIC resets if needed.*/
5466 /* Host driver will handle XGMI hive reset for SRIOV */
5467 if (amdgpu_sriov_vf(adev)) {
5468 r = amdgpu_device_reset_sriov(adev, job ? false : true);
5470 adev->asic_reset_res = r;
5472 /* Aldebaran and gfx_11_0_3 support ras in SRIOV, so need resume ras during reset */
5473 if (adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 2) ||
5474 adev->ip_versions[GC_HWIP][0] == IP_VERSION(11, 0, 3))
5475 amdgpu_ras_resume(adev);
5477 r = amdgpu_do_asic_reset(device_list_handle, reset_context);
5478 if (r && r == -EAGAIN)
5481 if (!r && gpu_reset_for_dev_remove)
5487 /* Post ASIC reset for all devs .*/
5488 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5490 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5491 struct amdgpu_ring *ring = tmp_adev->rings[i];
5493 if (!ring || !ring->sched.thread)
5496 drm_sched_start(&ring->sched, true);
5499 if (adev->enable_mes && adev->ip_versions[GC_HWIP][0] != IP_VERSION(11, 0, 3))
5500 amdgpu_mes_self_test(tmp_adev);
5502 if (!drm_drv_uses_atomic_modeset(adev_to_drm(tmp_adev)) && !job_signaled)
5503 drm_helper_resume_force_mode(adev_to_drm(tmp_adev));
5505 if (tmp_adev->asic_reset_res)
5506 r = tmp_adev->asic_reset_res;
5508 tmp_adev->asic_reset_res = 0;
5511 /* bad news, how to tell it to userspace ? */
5512 dev_info(tmp_adev->dev, "GPU reset(%d) failed\n", atomic_read(&tmp_adev->gpu_reset_counter));
5513 amdgpu_vf_error_put(tmp_adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
5515 dev_info(tmp_adev->dev, "GPU reset(%d) succeeded!\n", atomic_read(&tmp_adev->gpu_reset_counter));
5516 if (amdgpu_acpi_smart_shift_update(adev_to_drm(tmp_adev), AMDGPU_SS_DEV_D0))
5517 DRM_WARN("smart shift update failed\n");
5522 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5523 /* unlock kfd: SRIOV would do it separately */
5524 if (!need_emergency_restart && !amdgpu_sriov_vf(tmp_adev))
5525 amdgpu_amdkfd_post_reset(tmp_adev);
5527 /* kfd_post_reset will do nothing if kfd device is not initialized,
5528 * need to bring up kfd here if it's not be initialized before
5530 if (!adev->kfd.init_complete)
5531 amdgpu_amdkfd_device_init(adev);
5533 if (audio_suspended)
5534 amdgpu_device_resume_display_audio(tmp_adev);
5536 amdgpu_device_unset_mp1_state(tmp_adev);
5538 amdgpu_ras_set_error_query_ready(tmp_adev, true);
5542 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
5544 amdgpu_device_unlock_reset_domain(tmp_adev->reset_domain);
5547 mutex_unlock(&hive->hive_lock);
5548 amdgpu_put_xgmi_hive(hive);
5552 dev_info(adev->dev, "GPU reset end with ret = %d\n", r);
5554 atomic_set(&adev->reset_domain->reset_res, r);
5559 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
5561 * @adev: amdgpu_device pointer
5563 * Fetchs and stores in the driver the PCIE capabilities (gen speed
5564 * and lanes) of the slot the device is in. Handles APUs and
5565 * virtualized environments where PCIE config space may not be available.
5567 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
5569 struct pci_dev *pdev;
5570 enum pci_bus_speed speed_cap, platform_speed_cap;
5571 enum pcie_link_width platform_link_width;
5573 if (amdgpu_pcie_gen_cap)
5574 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
5576 if (amdgpu_pcie_lane_cap)
5577 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
5579 /* covers APUs as well */
5580 if (pci_is_root_bus(adev->pdev->bus) && !amdgpu_passthrough(adev)) {
5581 if (adev->pm.pcie_gen_mask == 0)
5582 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
5583 if (adev->pm.pcie_mlw_mask == 0)
5584 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
5588 if (adev->pm.pcie_gen_mask && adev->pm.pcie_mlw_mask)
5591 pcie_bandwidth_available(adev->pdev, NULL,
5592 &platform_speed_cap, &platform_link_width);
5594 if (adev->pm.pcie_gen_mask == 0) {
5597 speed_cap = pcie_get_speed_cap(pdev);
5598 if (speed_cap == PCI_SPEED_UNKNOWN) {
5599 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5600 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5601 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
5603 if (speed_cap == PCIE_SPEED_32_0GT)
5604 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5605 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5606 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5607 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4 |
5608 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN5);
5609 else if (speed_cap == PCIE_SPEED_16_0GT)
5610 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5611 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5612 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5613 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4);
5614 else if (speed_cap == PCIE_SPEED_8_0GT)
5615 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5616 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5617 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
5618 else if (speed_cap == PCIE_SPEED_5_0GT)
5619 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5620 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2);
5622 adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1;
5625 if (platform_speed_cap == PCI_SPEED_UNKNOWN) {
5626 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5627 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
5629 if (platform_speed_cap == PCIE_SPEED_32_0GT)
5630 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5631 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5632 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5633 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4 |
5634 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN5);
5635 else if (platform_speed_cap == PCIE_SPEED_16_0GT)
5636 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5637 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5638 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5639 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4);
5640 else if (platform_speed_cap == PCIE_SPEED_8_0GT)
5641 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5642 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5643 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3);
5644 else if (platform_speed_cap == PCIE_SPEED_5_0GT)
5645 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5646 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
5648 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
5652 if (adev->pm.pcie_mlw_mask == 0) {
5653 if (platform_link_width == PCIE_LNK_WIDTH_UNKNOWN) {
5654 adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK;
5656 switch (platform_link_width) {
5658 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
5659 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5660 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5661 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5662 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5663 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5664 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5667 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5668 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5669 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5670 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5671 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5672 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5675 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5676 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5677 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5678 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5679 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5682 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5683 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5684 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5685 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5688 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5689 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5690 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5693 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5694 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5697 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
5707 * amdgpu_device_is_peer_accessible - Check peer access through PCIe BAR
5709 * @adev: amdgpu_device pointer
5710 * @peer_adev: amdgpu_device pointer for peer device trying to access @adev
5712 * Return true if @peer_adev can access (DMA) @adev through the PCIe
5713 * BAR, i.e. @adev is "large BAR" and the BAR matches the DMA mask of
5716 bool amdgpu_device_is_peer_accessible(struct amdgpu_device *adev,
5717 struct amdgpu_device *peer_adev)
5719 #ifdef CONFIG_HSA_AMD_P2P
5720 uint64_t address_mask = peer_adev->dev->dma_mask ?
5721 ~*peer_adev->dev->dma_mask : ~((1ULL << 32) - 1);
5722 resource_size_t aper_limit =
5723 adev->gmc.aper_base + adev->gmc.aper_size - 1;
5725 !adev->gmc.xgmi.connected_to_cpu &&
5726 !(pci_p2pdma_distance(adev->pdev, peer_adev->dev, false) < 0);
5728 return pcie_p2p && p2p_access && (adev->gmc.visible_vram_size &&
5729 adev->gmc.real_vram_size == adev->gmc.visible_vram_size &&
5730 !(adev->gmc.aper_base & address_mask ||
5731 aper_limit & address_mask));
5737 int amdgpu_device_baco_enter(struct drm_device *dev)
5739 struct amdgpu_device *adev = drm_to_adev(dev);
5740 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
5742 if (!amdgpu_device_supports_baco(dev))
5745 if (ras && adev->ras_enabled &&
5746 adev->nbio.funcs->enable_doorbell_interrupt)
5747 adev->nbio.funcs->enable_doorbell_interrupt(adev, false);
5749 return amdgpu_dpm_baco_enter(adev);
5752 int amdgpu_device_baco_exit(struct drm_device *dev)
5754 struct amdgpu_device *adev = drm_to_adev(dev);
5755 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
5758 if (!amdgpu_device_supports_baco(dev))
5761 ret = amdgpu_dpm_baco_exit(adev);
5765 if (ras && adev->ras_enabled &&
5766 adev->nbio.funcs->enable_doorbell_interrupt)
5767 adev->nbio.funcs->enable_doorbell_interrupt(adev, true);
5769 if (amdgpu_passthrough(adev) &&
5770 adev->nbio.funcs->clear_doorbell_interrupt)
5771 adev->nbio.funcs->clear_doorbell_interrupt(adev);
5777 * amdgpu_pci_error_detected - Called when a PCI error is detected.
5778 * @pdev: PCI device struct
5779 * @state: PCI channel state
5781 * Description: Called when a PCI error is detected.
5783 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT.
5785 pci_ers_result_t amdgpu_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
5787 struct drm_device *dev = pci_get_drvdata(pdev);
5788 struct amdgpu_device *adev = drm_to_adev(dev);
5791 DRM_INFO("PCI error: detected callback, state(%d)!!\n", state);
5793 if (adev->gmc.xgmi.num_physical_nodes > 1) {
5794 DRM_WARN("No support for XGMI hive yet...");
5795 return PCI_ERS_RESULT_DISCONNECT;
5798 adev->pci_channel_state = state;
5801 case pci_channel_io_normal:
5802 return PCI_ERS_RESULT_CAN_RECOVER;
5803 /* Fatal error, prepare for slot reset */
5804 case pci_channel_io_frozen:
5806 * Locking adev->reset_domain->sem will prevent any external access
5807 * to GPU during PCI error recovery
5809 amdgpu_device_lock_reset_domain(adev->reset_domain);
5810 amdgpu_device_set_mp1_state(adev);
5813 * Block any work scheduling as we do for regular GPU reset
5814 * for the duration of the recovery
5816 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5817 struct amdgpu_ring *ring = adev->rings[i];
5819 if (!ring || !ring->sched.thread)
5822 drm_sched_stop(&ring->sched, NULL);
5824 atomic_inc(&adev->gpu_reset_counter);
5825 return PCI_ERS_RESULT_NEED_RESET;
5826 case pci_channel_io_perm_failure:
5827 /* Permanent error, prepare for device removal */
5828 return PCI_ERS_RESULT_DISCONNECT;
5831 return PCI_ERS_RESULT_NEED_RESET;
5835 * amdgpu_pci_mmio_enabled - Enable MMIO and dump debug registers
5836 * @pdev: pointer to PCI device
5838 pci_ers_result_t amdgpu_pci_mmio_enabled(struct pci_dev *pdev)
5841 DRM_INFO("PCI error: mmio enabled callback!!\n");
5843 /* TODO - dump whatever for debugging purposes */
5845 /* This called only if amdgpu_pci_error_detected returns
5846 * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still
5847 * works, no need to reset slot.
5850 return PCI_ERS_RESULT_RECOVERED;
5854 * amdgpu_pci_slot_reset - Called when PCI slot has been reset.
5855 * @pdev: PCI device struct
5857 * Description: This routine is called by the pci error recovery
5858 * code after the PCI slot has been reset, just before we
5859 * should resume normal operations.
5861 pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev)
5863 struct drm_device *dev = pci_get_drvdata(pdev);
5864 struct amdgpu_device *adev = drm_to_adev(dev);
5866 struct amdgpu_reset_context reset_context;
5868 struct list_head device_list;
5870 DRM_INFO("PCI error: slot reset callback!!\n");
5872 memset(&reset_context, 0, sizeof(reset_context));
5874 INIT_LIST_HEAD(&device_list);
5875 list_add_tail(&adev->reset_list, &device_list);
5877 /* wait for asic to come out of reset */
5880 /* Restore PCI confspace */
5881 amdgpu_device_load_pci_state(pdev);
5883 /* confirm ASIC came out of reset */
5884 for (i = 0; i < adev->usec_timeout; i++) {
5885 memsize = amdgpu_asic_get_config_memsize(adev);
5887 if (memsize != 0xffffffff)
5891 if (memsize == 0xffffffff) {
5896 reset_context.method = AMD_RESET_METHOD_NONE;
5897 reset_context.reset_req_dev = adev;
5898 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
5899 set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
5901 adev->no_hw_access = true;
5902 r = amdgpu_device_pre_asic_reset(adev, &reset_context);
5903 adev->no_hw_access = false;
5907 r = amdgpu_do_asic_reset(&device_list, &reset_context);
5911 if (amdgpu_device_cache_pci_state(adev->pdev))
5912 pci_restore_state(adev->pdev);
5914 DRM_INFO("PCIe error recovery succeeded\n");
5916 DRM_ERROR("PCIe error recovery failed, err:%d", r);
5917 amdgpu_device_unset_mp1_state(adev);
5918 amdgpu_device_unlock_reset_domain(adev->reset_domain);
5921 return r ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
5925 * amdgpu_pci_resume() - resume normal ops after PCI reset
5926 * @pdev: pointer to PCI device
5928 * Called when the error recovery driver tells us that its
5929 * OK to resume normal operation.
5931 void amdgpu_pci_resume(struct pci_dev *pdev)
5933 struct drm_device *dev = pci_get_drvdata(pdev);
5934 struct amdgpu_device *adev = drm_to_adev(dev);
5938 DRM_INFO("PCI error: resume callback!!\n");
5940 /* Only continue execution for the case of pci_channel_io_frozen */
5941 if (adev->pci_channel_state != pci_channel_io_frozen)
5944 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5945 struct amdgpu_ring *ring = adev->rings[i];
5947 if (!ring || !ring->sched.thread)
5950 drm_sched_start(&ring->sched, true);
5953 amdgpu_device_unset_mp1_state(adev);
5954 amdgpu_device_unlock_reset_domain(adev->reset_domain);
5957 bool amdgpu_device_cache_pci_state(struct pci_dev *pdev)
5959 struct drm_device *dev = pci_get_drvdata(pdev);
5960 struct amdgpu_device *adev = drm_to_adev(dev);
5963 r = pci_save_state(pdev);
5965 kfree(adev->pci_state);
5967 adev->pci_state = pci_store_saved_state(pdev);
5969 if (!adev->pci_state) {
5970 DRM_ERROR("Failed to store PCI saved state");
5974 DRM_WARN("Failed to save PCI state, err:%d\n", r);
5981 bool amdgpu_device_load_pci_state(struct pci_dev *pdev)
5983 struct drm_device *dev = pci_get_drvdata(pdev);
5984 struct amdgpu_device *adev = drm_to_adev(dev);
5987 if (!adev->pci_state)
5990 r = pci_load_saved_state(pdev, adev->pci_state);
5993 pci_restore_state(pdev);
5995 DRM_WARN("Failed to load PCI state, err:%d\n", r);
6002 void amdgpu_device_flush_hdp(struct amdgpu_device *adev,
6003 struct amdgpu_ring *ring)
6005 #ifdef CONFIG_X86_64
6006 if ((adev->flags & AMD_IS_APU) && !amdgpu_passthrough(adev))
6009 if (adev->gmc.xgmi.connected_to_cpu)
6012 if (ring && ring->funcs->emit_hdp_flush)
6013 amdgpu_ring_emit_hdp_flush(ring);
6015 amdgpu_asic_flush_hdp(adev, ring);
6018 void amdgpu_device_invalidate_hdp(struct amdgpu_device *adev,
6019 struct amdgpu_ring *ring)
6021 #ifdef CONFIG_X86_64
6022 if ((adev->flags & AMD_IS_APU) && !amdgpu_passthrough(adev))
6025 if (adev->gmc.xgmi.connected_to_cpu)
6028 amdgpu_asic_invalidate_hdp(adev, ring);
6031 int amdgpu_in_reset(struct amdgpu_device *adev)
6033 return atomic_read(&adev->reset_domain->in_gpu_reset);
6037 * amdgpu_device_halt() - bring hardware to some kind of halt state
6039 * @adev: amdgpu_device pointer
6041 * Bring hardware to some kind of halt state so that no one can touch it
6042 * any more. It will help to maintain error context when error occurred.
6043 * Compare to a simple hang, the system will keep stable at least for SSH
6044 * access. Then it should be trivial to inspect the hardware state and
6045 * see what's going on. Implemented as following:
6047 * 1. drm_dev_unplug() makes device inaccessible to user space(IOCTLs, etc),
6048 * clears all CPU mappings to device, disallows remappings through page faults
6049 * 2. amdgpu_irq_disable_all() disables all interrupts
6050 * 3. amdgpu_fence_driver_hw_fini() signals all HW fences
6051 * 4. set adev->no_hw_access to avoid potential crashes after setp 5
6052 * 5. amdgpu_device_unmap_mmio() clears all MMIO mappings
6053 * 6. pci_disable_device() and pci_wait_for_pending_transaction()
6054 * flush any in flight DMA operations
6056 void amdgpu_device_halt(struct amdgpu_device *adev)
6058 struct pci_dev *pdev = adev->pdev;
6059 struct drm_device *ddev = adev_to_drm(adev);
6061 amdgpu_xcp_dev_unplug(adev);
6062 drm_dev_unplug(ddev);
6064 amdgpu_irq_disable_all(adev);
6066 amdgpu_fence_driver_hw_fini(adev);
6068 adev->no_hw_access = true;
6070 amdgpu_device_unmap_mmio(adev);
6072 pci_disable_device(pdev);
6073 pci_wait_for_pending_transaction(pdev);
6076 u32 amdgpu_device_pcie_port_rreg(struct amdgpu_device *adev,
6079 unsigned long flags, address, data;
6082 address = adev->nbio.funcs->get_pcie_port_index_offset(adev);
6083 data = adev->nbio.funcs->get_pcie_port_data_offset(adev);
6085 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
6086 WREG32(address, reg * 4);
6087 (void)RREG32(address);
6089 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
6093 void amdgpu_device_pcie_port_wreg(struct amdgpu_device *adev,
6096 unsigned long flags, address, data;
6098 address = adev->nbio.funcs->get_pcie_port_index_offset(adev);
6099 data = adev->nbio.funcs->get_pcie_port_data_offset(adev);
6101 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
6102 WREG32(address, reg * 4);
6103 (void)RREG32(address);
6106 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
6110 * amdgpu_device_switch_gang - switch to a new gang
6111 * @adev: amdgpu_device pointer
6112 * @gang: the gang to switch to
6114 * Try to switch to a new gang.
6115 * Returns: NULL if we switched to the new gang or a reference to the current
6118 struct dma_fence *amdgpu_device_switch_gang(struct amdgpu_device *adev,
6119 struct dma_fence *gang)
6121 struct dma_fence *old = NULL;
6126 old = dma_fence_get_rcu_safe(&adev->gang_submit);
6132 if (!dma_fence_is_signaled(old))
6135 } while (cmpxchg((struct dma_fence __force **)&adev->gang_submit,
6142 bool amdgpu_device_has_display_hardware(struct amdgpu_device *adev)
6144 switch (adev->asic_type) {
6145 #ifdef CONFIG_DRM_AMDGPU_SI
6149 /* chips with no display hardware */
6151 #ifdef CONFIG_DRM_AMDGPU_SI
6157 #ifdef CONFIG_DRM_AMDGPU_CIK
6166 case CHIP_POLARIS10:
6167 case CHIP_POLARIS11:
6168 case CHIP_POLARIS12:
6172 /* chips with display hardware */
6176 if (!adev->ip_versions[DCE_HWIP][0] ||
6177 (adev->harvest_ip_mask & AMD_HARVEST_IP_DMU_MASK))
6183 uint32_t amdgpu_device_wait_on_rreg(struct amdgpu_device *adev,
6184 uint32_t inst, uint32_t reg_addr, char reg_name[],
6185 uint32_t expected_value, uint32_t mask)
6189 uint32_t tmp_ = RREG32(reg_addr);
6190 uint32_t loop = adev->usec_timeout;
6192 while ((tmp_ & (mask)) != (expected_value)) {
6194 loop = adev->usec_timeout;
6198 tmp_ = RREG32(reg_addr);
6201 DRM_WARN("Register(%d) [%s] failed to reach value 0x%08x != 0x%08xn",
6202 inst, reg_name, (uint32_t)expected_value,
6203 (uint32_t)(tmp_ & (mask)));