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 * Intel hosts such as Raptor Lake and Sapphire Rapids don't support dynamic
1363 * speed switching. Until we have confirmation from Intel that a specific host
1364 * supports it, it's safer that we keep it disabled for all.
1366 * 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/
1367 * https://gitlab.freedesktop.org/drm/amd/-/issues/2663
1369 bool amdgpu_device_pcie_dynamic_switching_supported(void)
1371 #if IS_ENABLED(CONFIG_X86)
1372 struct cpuinfo_x86 *c = &cpu_data(0);
1374 if (c->x86_vendor == X86_VENDOR_INTEL)
1381 * amdgpu_device_should_use_aspm - check if the device should program ASPM
1383 * @adev: amdgpu_device pointer
1385 * Confirm whether the module parameter and pcie bridge agree that ASPM should
1386 * be set for this device.
1388 * Returns true if it should be used or false if not.
1390 bool amdgpu_device_should_use_aspm(struct amdgpu_device *adev)
1392 switch (amdgpu_aspm) {
1402 return pcie_aspm_enabled(adev->pdev);
1405 bool amdgpu_device_aspm_support_quirk(void)
1407 #if IS_ENABLED(CONFIG_X86)
1408 struct cpuinfo_x86 *c = &cpu_data(0);
1410 return !(c->x86 == 6 && c->x86_model == INTEL_FAM6_ALDERLAKE);
1416 /* if we get transitioned to only one device, take VGA back */
1418 * amdgpu_device_vga_set_decode - enable/disable vga decode
1420 * @pdev: PCI device pointer
1421 * @state: enable/disable vga decode
1423 * Enable/disable vga decode (all asics).
1424 * Returns VGA resource flags.
1426 static unsigned int amdgpu_device_vga_set_decode(struct pci_dev *pdev,
1429 struct amdgpu_device *adev = drm_to_adev(pci_get_drvdata(pdev));
1431 amdgpu_asic_set_vga_state(adev, state);
1433 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1434 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1436 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1440 * amdgpu_device_check_block_size - validate the vm block size
1442 * @adev: amdgpu_device pointer
1444 * Validates the vm block size specified via module parameter.
1445 * The vm block size defines number of bits in page table versus page directory,
1446 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1447 * page table and the remaining bits are in the page directory.
1449 static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
1451 /* defines number of bits in page table versus page directory,
1452 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1453 * page table and the remaining bits are in the page directory
1455 if (amdgpu_vm_block_size == -1)
1458 if (amdgpu_vm_block_size < 9) {
1459 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1460 amdgpu_vm_block_size);
1461 amdgpu_vm_block_size = -1;
1466 * amdgpu_device_check_vm_size - validate the vm size
1468 * @adev: amdgpu_device pointer
1470 * Validates the vm size in GB specified via module parameter.
1471 * The VM size is the size of the GPU virtual memory space in GB.
1473 static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
1475 /* no need to check the default value */
1476 if (amdgpu_vm_size == -1)
1479 if (amdgpu_vm_size < 1) {
1480 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1482 amdgpu_vm_size = -1;
1486 static void amdgpu_device_check_smu_prv_buffer_size(struct amdgpu_device *adev)
1489 bool is_os_64 = (sizeof(void *) == 8);
1490 uint64_t total_memory;
1491 uint64_t dram_size_seven_GB = 0x1B8000000;
1492 uint64_t dram_size_three_GB = 0xB8000000;
1494 if (amdgpu_smu_memory_pool_size == 0)
1498 DRM_WARN("Not 64-bit OS, feature not supported\n");
1502 total_memory = (uint64_t)si.totalram * si.mem_unit;
1504 if ((amdgpu_smu_memory_pool_size == 1) ||
1505 (amdgpu_smu_memory_pool_size == 2)) {
1506 if (total_memory < dram_size_three_GB)
1508 } else if ((amdgpu_smu_memory_pool_size == 4) ||
1509 (amdgpu_smu_memory_pool_size == 8)) {
1510 if (total_memory < dram_size_seven_GB)
1513 DRM_WARN("Smu memory pool size not supported\n");
1516 adev->pm.smu_prv_buffer_size = amdgpu_smu_memory_pool_size << 28;
1521 DRM_WARN("No enough system memory\n");
1523 adev->pm.smu_prv_buffer_size = 0;
1526 static int amdgpu_device_init_apu_flags(struct amdgpu_device *adev)
1528 if (!(adev->flags & AMD_IS_APU) ||
1529 adev->asic_type < CHIP_RAVEN)
1532 switch (adev->asic_type) {
1534 if (adev->pdev->device == 0x15dd)
1535 adev->apu_flags |= AMD_APU_IS_RAVEN;
1536 if (adev->pdev->device == 0x15d8)
1537 adev->apu_flags |= AMD_APU_IS_PICASSO;
1540 if ((adev->pdev->device == 0x1636) ||
1541 (adev->pdev->device == 0x164c))
1542 adev->apu_flags |= AMD_APU_IS_RENOIR;
1544 adev->apu_flags |= AMD_APU_IS_GREEN_SARDINE;
1547 adev->apu_flags |= AMD_APU_IS_VANGOGH;
1549 case CHIP_YELLOW_CARP:
1551 case CHIP_CYAN_SKILLFISH:
1552 if ((adev->pdev->device == 0x13FE) ||
1553 (adev->pdev->device == 0x143F))
1554 adev->apu_flags |= AMD_APU_IS_CYAN_SKILLFISH2;
1564 * amdgpu_device_check_arguments - validate module params
1566 * @adev: amdgpu_device pointer
1568 * Validates certain module parameters and updates
1569 * the associated values used by the driver (all asics).
1571 static int amdgpu_device_check_arguments(struct amdgpu_device *adev)
1573 if (amdgpu_sched_jobs < 4) {
1574 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1576 amdgpu_sched_jobs = 4;
1577 } else if (!is_power_of_2(amdgpu_sched_jobs)) {
1578 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1580 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1583 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
1584 /* gart size must be greater or equal to 32M */
1585 dev_warn(adev->dev, "gart size (%d) too small\n",
1587 amdgpu_gart_size = -1;
1590 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
1591 /* gtt size must be greater or equal to 32M */
1592 dev_warn(adev->dev, "gtt size (%d) too small\n",
1594 amdgpu_gtt_size = -1;
1597 /* valid range is between 4 and 9 inclusive */
1598 if (amdgpu_vm_fragment_size != -1 &&
1599 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1600 dev_warn(adev->dev, "valid range is between 4 and 9\n");
1601 amdgpu_vm_fragment_size = -1;
1604 if (amdgpu_sched_hw_submission < 2) {
1605 dev_warn(adev->dev, "sched hw submission jobs (%d) must be at least 2\n",
1606 amdgpu_sched_hw_submission);
1607 amdgpu_sched_hw_submission = 2;
1608 } else if (!is_power_of_2(amdgpu_sched_hw_submission)) {
1609 dev_warn(adev->dev, "sched hw submission jobs (%d) must be a power of 2\n",
1610 amdgpu_sched_hw_submission);
1611 amdgpu_sched_hw_submission = roundup_pow_of_two(amdgpu_sched_hw_submission);
1614 if (amdgpu_reset_method < -1 || amdgpu_reset_method > 4) {
1615 dev_warn(adev->dev, "invalid option for reset method, reverting to default\n");
1616 amdgpu_reset_method = -1;
1619 amdgpu_device_check_smu_prv_buffer_size(adev);
1621 amdgpu_device_check_vm_size(adev);
1623 amdgpu_device_check_block_size(adev);
1625 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
1631 * amdgpu_switcheroo_set_state - set switcheroo state
1633 * @pdev: pci dev pointer
1634 * @state: vga_switcheroo state
1636 * Callback for the switcheroo driver. Suspends or resumes
1637 * the asics before or after it is powered up using ACPI methods.
1639 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev,
1640 enum vga_switcheroo_state state)
1642 struct drm_device *dev = pci_get_drvdata(pdev);
1645 if (amdgpu_device_supports_px(dev) && state == VGA_SWITCHEROO_OFF)
1648 if (state == VGA_SWITCHEROO_ON) {
1649 pr_info("switched on\n");
1650 /* don't suspend or resume card normally */
1651 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1653 pci_set_power_state(pdev, PCI_D0);
1654 amdgpu_device_load_pci_state(pdev);
1655 r = pci_enable_device(pdev);
1657 DRM_WARN("pci_enable_device failed (%d)\n", r);
1658 amdgpu_device_resume(dev, true);
1660 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1662 pr_info("switched off\n");
1663 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1664 amdgpu_device_suspend(dev, true);
1665 amdgpu_device_cache_pci_state(pdev);
1666 /* Shut down the device */
1667 pci_disable_device(pdev);
1668 pci_set_power_state(pdev, PCI_D3cold);
1669 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1674 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1676 * @pdev: pci dev pointer
1678 * Callback for the switcheroo driver. Check of the switcheroo
1679 * state can be changed.
1680 * Returns true if the state can be changed, false if not.
1682 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1684 struct drm_device *dev = pci_get_drvdata(pdev);
1687 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1688 * locking inversion with the driver load path. And the access here is
1689 * completely racy anyway. So don't bother with locking for now.
1691 return atomic_read(&dev->open_count) == 0;
1694 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1695 .set_gpu_state = amdgpu_switcheroo_set_state,
1697 .can_switch = amdgpu_switcheroo_can_switch,
1701 * amdgpu_device_ip_set_clockgating_state - set the CG state
1703 * @dev: amdgpu_device pointer
1704 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1705 * @state: clockgating state (gate or ungate)
1707 * Sets the requested clockgating state for all instances of
1708 * the hardware IP specified.
1709 * Returns the error code from the last instance.
1711 int amdgpu_device_ip_set_clockgating_state(void *dev,
1712 enum amd_ip_block_type block_type,
1713 enum amd_clockgating_state state)
1715 struct amdgpu_device *adev = dev;
1718 for (i = 0; i < adev->num_ip_blocks; i++) {
1719 if (!adev->ip_blocks[i].status.valid)
1721 if (adev->ip_blocks[i].version->type != block_type)
1723 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1725 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1726 (void *)adev, state);
1728 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1729 adev->ip_blocks[i].version->funcs->name, r);
1735 * amdgpu_device_ip_set_powergating_state - set the PG state
1737 * @dev: amdgpu_device pointer
1738 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1739 * @state: powergating state (gate or ungate)
1741 * Sets the requested powergating state for all instances of
1742 * the hardware IP specified.
1743 * Returns the error code from the last instance.
1745 int amdgpu_device_ip_set_powergating_state(void *dev,
1746 enum amd_ip_block_type block_type,
1747 enum amd_powergating_state state)
1749 struct amdgpu_device *adev = dev;
1752 for (i = 0; i < adev->num_ip_blocks; i++) {
1753 if (!adev->ip_blocks[i].status.valid)
1755 if (adev->ip_blocks[i].version->type != block_type)
1757 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1759 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1760 (void *)adev, state);
1762 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1763 adev->ip_blocks[i].version->funcs->name, r);
1769 * amdgpu_device_ip_get_clockgating_state - get the CG state
1771 * @adev: amdgpu_device pointer
1772 * @flags: clockgating feature flags
1774 * Walks the list of IPs on the device and updates the clockgating
1775 * flags for each IP.
1776 * Updates @flags with the feature flags for each hardware IP where
1777 * clockgating is enabled.
1779 void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
1784 for (i = 0; i < adev->num_ip_blocks; i++) {
1785 if (!adev->ip_blocks[i].status.valid)
1787 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1788 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1793 * amdgpu_device_ip_wait_for_idle - wait for idle
1795 * @adev: amdgpu_device pointer
1796 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1798 * Waits for the request hardware IP to be idle.
1799 * Returns 0 for success or a negative error code on failure.
1801 int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1802 enum amd_ip_block_type block_type)
1806 for (i = 0; i < adev->num_ip_blocks; i++) {
1807 if (!adev->ip_blocks[i].status.valid)
1809 if (adev->ip_blocks[i].version->type == block_type) {
1810 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
1821 * amdgpu_device_ip_is_idle - is the hardware IP idle
1823 * @adev: amdgpu_device pointer
1824 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1826 * Check if the hardware IP is idle or not.
1827 * Returns true if it the IP is idle, false if not.
1829 bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1830 enum amd_ip_block_type block_type)
1834 for (i = 0; i < adev->num_ip_blocks; i++) {
1835 if (!adev->ip_blocks[i].status.valid)
1837 if (adev->ip_blocks[i].version->type == block_type)
1838 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
1845 * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1847 * @adev: amdgpu_device pointer
1848 * @type: Type of hardware IP (SMU, GFX, UVD, etc.)
1850 * Returns a pointer to the hardware IP block structure
1851 * if it exists for the asic, otherwise NULL.
1853 struct amdgpu_ip_block *
1854 amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1855 enum amd_ip_block_type type)
1859 for (i = 0; i < adev->num_ip_blocks; i++)
1860 if (adev->ip_blocks[i].version->type == type)
1861 return &adev->ip_blocks[i];
1867 * amdgpu_device_ip_block_version_cmp
1869 * @adev: amdgpu_device pointer
1870 * @type: enum amd_ip_block_type
1871 * @major: major version
1872 * @minor: minor version
1874 * return 0 if equal or greater
1875 * return 1 if smaller or the ip_block doesn't exist
1877 int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1878 enum amd_ip_block_type type,
1879 u32 major, u32 minor)
1881 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
1883 if (ip_block && ((ip_block->version->major > major) ||
1884 ((ip_block->version->major == major) &&
1885 (ip_block->version->minor >= minor))))
1892 * amdgpu_device_ip_block_add
1894 * @adev: amdgpu_device pointer
1895 * @ip_block_version: pointer to the IP to add
1897 * Adds the IP block driver information to the collection of IPs
1900 int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1901 const struct amdgpu_ip_block_version *ip_block_version)
1903 if (!ip_block_version)
1906 switch (ip_block_version->type) {
1907 case AMD_IP_BLOCK_TYPE_VCN:
1908 if (adev->harvest_ip_mask & AMD_HARVEST_IP_VCN_MASK)
1911 case AMD_IP_BLOCK_TYPE_JPEG:
1912 if (adev->harvest_ip_mask & AMD_HARVEST_IP_JPEG_MASK)
1919 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
1920 ip_block_version->funcs->name);
1922 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1928 * amdgpu_device_enable_virtual_display - enable virtual display feature
1930 * @adev: amdgpu_device pointer
1932 * Enabled the virtual display feature if the user has enabled it via
1933 * the module parameter virtual_display. This feature provides a virtual
1934 * display hardware on headless boards or in virtualized environments.
1935 * This function parses and validates the configuration string specified by
1936 * the user and configues the virtual display configuration (number of
1937 * virtual connectors, crtcs, etc.) specified.
1939 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
1941 adev->enable_virtual_display = false;
1943 if (amdgpu_virtual_display) {
1944 const char *pci_address_name = pci_name(adev->pdev);
1945 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
1947 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1948 pciaddstr_tmp = pciaddstr;
1949 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1950 pciaddname = strsep(&pciaddname_tmp, ",");
1951 if (!strcmp("all", pciaddname)
1952 || !strcmp(pci_address_name, pciaddname)) {
1956 adev->enable_virtual_display = true;
1959 res = kstrtol(pciaddname_tmp, 10,
1967 adev->mode_info.num_crtc = num_crtc;
1969 adev->mode_info.num_crtc = 1;
1975 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1976 amdgpu_virtual_display, pci_address_name,
1977 adev->enable_virtual_display, adev->mode_info.num_crtc);
1983 void amdgpu_device_set_sriov_virtual_display(struct amdgpu_device *adev)
1985 if (amdgpu_sriov_vf(adev) && !adev->enable_virtual_display) {
1986 adev->mode_info.num_crtc = 1;
1987 adev->enable_virtual_display = true;
1988 DRM_INFO("virtual_display:%d, num_crtc:%d\n",
1989 adev->enable_virtual_display, adev->mode_info.num_crtc);
1994 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
1996 * @adev: amdgpu_device pointer
1998 * Parses the asic configuration parameters specified in the gpu info
1999 * firmware and makes them availale to the driver for use in configuring
2001 * Returns 0 on success, -EINVAL on failure.
2003 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
2005 const char *chip_name;
2008 const struct gpu_info_firmware_header_v1_0 *hdr;
2010 adev->firmware.gpu_info_fw = NULL;
2012 if (adev->mman.discovery_bin) {
2014 * FIXME: The bounding box is still needed by Navi12, so
2015 * temporarily read it from gpu_info firmware. Should be dropped
2016 * when DAL no longer needs it.
2018 if (adev->asic_type != CHIP_NAVI12)
2022 switch (adev->asic_type) {
2026 chip_name = "vega10";
2029 chip_name = "vega12";
2032 if (adev->apu_flags & AMD_APU_IS_RAVEN2)
2033 chip_name = "raven2";
2034 else if (adev->apu_flags & AMD_APU_IS_PICASSO)
2035 chip_name = "picasso";
2037 chip_name = "raven";
2040 chip_name = "arcturus";
2043 chip_name = "navi12";
2047 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
2048 err = amdgpu_ucode_request(adev, &adev->firmware.gpu_info_fw, fw_name);
2051 "Failed to get gpu_info firmware \"%s\"\n",
2056 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
2057 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
2059 switch (hdr->version_major) {
2062 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
2063 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
2064 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2067 * Should be droped when DAL no longer needs it.
2069 if (adev->asic_type == CHIP_NAVI12)
2070 goto parse_soc_bounding_box;
2072 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
2073 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
2074 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
2075 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
2076 adev->gfx.config.max_texture_channel_caches =
2077 le32_to_cpu(gpu_info_fw->gc_num_tccs);
2078 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
2079 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
2080 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
2081 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
2082 adev->gfx.config.double_offchip_lds_buf =
2083 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
2084 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
2085 adev->gfx.cu_info.max_waves_per_simd =
2086 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
2087 adev->gfx.cu_info.max_scratch_slots_per_cu =
2088 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
2089 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
2090 if (hdr->version_minor >= 1) {
2091 const struct gpu_info_firmware_v1_1 *gpu_info_fw =
2092 (const struct gpu_info_firmware_v1_1 *)(adev->firmware.gpu_info_fw->data +
2093 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2094 adev->gfx.config.num_sc_per_sh =
2095 le32_to_cpu(gpu_info_fw->num_sc_per_sh);
2096 adev->gfx.config.num_packer_per_sc =
2097 le32_to_cpu(gpu_info_fw->num_packer_per_sc);
2100 parse_soc_bounding_box:
2102 * soc bounding box info is not integrated in disocovery table,
2103 * we always need to parse it from gpu info firmware if needed.
2105 if (hdr->version_minor == 2) {
2106 const struct gpu_info_firmware_v1_2 *gpu_info_fw =
2107 (const struct gpu_info_firmware_v1_2 *)(adev->firmware.gpu_info_fw->data +
2108 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2109 adev->dm.soc_bounding_box = &gpu_info_fw->soc_bounding_box;
2115 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
2124 * amdgpu_device_ip_early_init - run early init for hardware IPs
2126 * @adev: amdgpu_device pointer
2128 * Early initialization pass for hardware IPs. The hardware IPs that make
2129 * up each asic are discovered each IP's early_init callback is run. This
2130 * is the first stage in initializing the asic.
2131 * Returns 0 on success, negative error code on failure.
2133 static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
2135 struct drm_device *dev = adev_to_drm(adev);
2136 struct pci_dev *parent;
2140 amdgpu_device_enable_virtual_display(adev);
2142 if (amdgpu_sriov_vf(adev)) {
2143 r = amdgpu_virt_request_full_gpu(adev, true);
2148 switch (adev->asic_type) {
2149 #ifdef CONFIG_DRM_AMDGPU_SI
2155 adev->family = AMDGPU_FAMILY_SI;
2156 r = si_set_ip_blocks(adev);
2161 #ifdef CONFIG_DRM_AMDGPU_CIK
2167 if (adev->flags & AMD_IS_APU)
2168 adev->family = AMDGPU_FAMILY_KV;
2170 adev->family = AMDGPU_FAMILY_CI;
2172 r = cik_set_ip_blocks(adev);
2180 case CHIP_POLARIS10:
2181 case CHIP_POLARIS11:
2182 case CHIP_POLARIS12:
2186 if (adev->flags & AMD_IS_APU)
2187 adev->family = AMDGPU_FAMILY_CZ;
2189 adev->family = AMDGPU_FAMILY_VI;
2191 r = vi_set_ip_blocks(adev);
2196 r = amdgpu_discovery_set_ip_blocks(adev);
2202 if (amdgpu_has_atpx() &&
2203 (amdgpu_is_atpx_hybrid() ||
2204 amdgpu_has_atpx_dgpu_power_cntl()) &&
2205 ((adev->flags & AMD_IS_APU) == 0) &&
2206 !pci_is_thunderbolt_attached(to_pci_dev(dev->dev)))
2207 adev->flags |= AMD_IS_PX;
2209 if (!(adev->flags & AMD_IS_APU)) {
2210 parent = pci_upstream_bridge(adev->pdev);
2211 adev->has_pr3 = parent ? pci_pr3_present(parent) : false;
2215 adev->pm.pp_feature = amdgpu_pp_feature_mask;
2216 if (amdgpu_sriov_vf(adev) || sched_policy == KFD_SCHED_POLICY_NO_HWS)
2217 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
2218 if (amdgpu_sriov_vf(adev) && adev->asic_type == CHIP_SIENNA_CICHLID)
2219 adev->pm.pp_feature &= ~PP_OVERDRIVE_MASK;
2222 for (i = 0; i < adev->num_ip_blocks; i++) {
2223 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
2224 DRM_WARN("disabled ip block: %d <%s>\n",
2225 i, adev->ip_blocks[i].version->funcs->name);
2226 adev->ip_blocks[i].status.valid = false;
2228 if (adev->ip_blocks[i].version->funcs->early_init) {
2229 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
2231 adev->ip_blocks[i].status.valid = false;
2233 DRM_ERROR("early_init of IP block <%s> failed %d\n",
2234 adev->ip_blocks[i].version->funcs->name, r);
2237 adev->ip_blocks[i].status.valid = true;
2240 adev->ip_blocks[i].status.valid = true;
2243 /* get the vbios after the asic_funcs are set up */
2244 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) {
2245 r = amdgpu_device_parse_gpu_info_fw(adev);
2250 if (amdgpu_device_read_bios(adev)) {
2251 if (!amdgpu_get_bios(adev))
2254 r = amdgpu_atombios_init(adev);
2256 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
2257 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
2262 /*get pf2vf msg info at it's earliest time*/
2263 if (amdgpu_sriov_vf(adev))
2264 amdgpu_virt_init_data_exchange(adev);
2271 amdgpu_amdkfd_device_probe(adev);
2272 adev->cg_flags &= amdgpu_cg_mask;
2273 adev->pg_flags &= amdgpu_pg_mask;
2278 static int amdgpu_device_ip_hw_init_phase1(struct amdgpu_device *adev)
2282 for (i = 0; i < adev->num_ip_blocks; i++) {
2283 if (!adev->ip_blocks[i].status.sw)
2285 if (adev->ip_blocks[i].status.hw)
2287 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2288 (amdgpu_sriov_vf(adev) && (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)) ||
2289 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
2290 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2292 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2293 adev->ip_blocks[i].version->funcs->name, r);
2296 adev->ip_blocks[i].status.hw = true;
2303 static int amdgpu_device_ip_hw_init_phase2(struct amdgpu_device *adev)
2307 for (i = 0; i < adev->num_ip_blocks; i++) {
2308 if (!adev->ip_blocks[i].status.sw)
2310 if (adev->ip_blocks[i].status.hw)
2312 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2314 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2315 adev->ip_blocks[i].version->funcs->name, r);
2318 adev->ip_blocks[i].status.hw = true;
2324 static int amdgpu_device_fw_loading(struct amdgpu_device *adev)
2328 uint32_t smu_version;
2330 if (adev->asic_type >= CHIP_VEGA10) {
2331 for (i = 0; i < adev->num_ip_blocks; i++) {
2332 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_PSP)
2335 if (!adev->ip_blocks[i].status.sw)
2338 /* no need to do the fw loading again if already done*/
2339 if (adev->ip_blocks[i].status.hw == true)
2342 if (amdgpu_in_reset(adev) || adev->in_suspend) {
2343 r = adev->ip_blocks[i].version->funcs->resume(adev);
2345 DRM_ERROR("resume of IP block <%s> failed %d\n",
2346 adev->ip_blocks[i].version->funcs->name, r);
2350 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2352 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2353 adev->ip_blocks[i].version->funcs->name, r);
2358 adev->ip_blocks[i].status.hw = true;
2363 if (!amdgpu_sriov_vf(adev) || adev->asic_type == CHIP_TONGA)
2364 r = amdgpu_pm_load_smu_firmware(adev, &smu_version);
2369 static int amdgpu_device_init_schedulers(struct amdgpu_device *adev)
2374 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2375 struct amdgpu_ring *ring = adev->rings[i];
2377 /* No need to setup the GPU scheduler for rings that don't need it */
2378 if (!ring || ring->no_scheduler)
2381 switch (ring->funcs->type) {
2382 case AMDGPU_RING_TYPE_GFX:
2383 timeout = adev->gfx_timeout;
2385 case AMDGPU_RING_TYPE_COMPUTE:
2386 timeout = adev->compute_timeout;
2388 case AMDGPU_RING_TYPE_SDMA:
2389 timeout = adev->sdma_timeout;
2392 timeout = adev->video_timeout;
2396 r = drm_sched_init(&ring->sched, &amdgpu_sched_ops,
2397 ring->num_hw_submission, 0,
2398 timeout, adev->reset_domain->wq,
2399 ring->sched_score, ring->name,
2402 DRM_ERROR("Failed to create scheduler on ring %s.\n",
2408 amdgpu_xcp_update_partition_sched_list(adev);
2415 * amdgpu_device_ip_init - run init for hardware IPs
2417 * @adev: amdgpu_device pointer
2419 * Main initialization pass for hardware IPs. The list of all the hardware
2420 * IPs that make up the asic is walked and the sw_init and hw_init callbacks
2421 * are run. sw_init initializes the software state associated with each IP
2422 * and hw_init initializes the hardware associated with each IP.
2423 * Returns 0 on success, negative error code on failure.
2425 static int amdgpu_device_ip_init(struct amdgpu_device *adev)
2429 r = amdgpu_ras_init(adev);
2433 for (i = 0; i < adev->num_ip_blocks; i++) {
2434 if (!adev->ip_blocks[i].status.valid)
2436 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
2438 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
2439 adev->ip_blocks[i].version->funcs->name, r);
2442 adev->ip_blocks[i].status.sw = true;
2444 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) {
2445 /* need to do common hw init early so everything is set up for gmc */
2446 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
2448 DRM_ERROR("hw_init %d failed %d\n", i, r);
2451 adev->ip_blocks[i].status.hw = true;
2452 } else if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
2453 /* need to do gmc hw init early so we can allocate gpu mem */
2454 /* Try to reserve bad pages early */
2455 if (amdgpu_sriov_vf(adev))
2456 amdgpu_virt_exchange_data(adev);
2458 r = amdgpu_device_mem_scratch_init(adev);
2460 DRM_ERROR("amdgpu_mem_scratch_init failed %d\n", r);
2463 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
2465 DRM_ERROR("hw_init %d failed %d\n", i, r);
2468 r = amdgpu_device_wb_init(adev);
2470 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
2473 adev->ip_blocks[i].status.hw = true;
2475 /* right after GMC hw init, we create CSA */
2476 if (adev->gfx.mcbp) {
2477 r = amdgpu_allocate_static_csa(adev, &adev->virt.csa_obj,
2478 AMDGPU_GEM_DOMAIN_VRAM |
2479 AMDGPU_GEM_DOMAIN_GTT,
2482 DRM_ERROR("allocate CSA failed %d\n", r);
2489 if (amdgpu_sriov_vf(adev))
2490 amdgpu_virt_init_data_exchange(adev);
2492 r = amdgpu_ib_pool_init(adev);
2494 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
2495 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
2499 r = amdgpu_ucode_create_bo(adev); /* create ucode bo when sw_init complete*/
2503 r = amdgpu_device_ip_hw_init_phase1(adev);
2507 r = amdgpu_device_fw_loading(adev);
2511 r = amdgpu_device_ip_hw_init_phase2(adev);
2516 * retired pages will be loaded from eeprom and reserved here,
2517 * it should be called after amdgpu_device_ip_hw_init_phase2 since
2518 * for some ASICs the RAS EEPROM code relies on SMU fully functioning
2519 * for I2C communication which only true at this point.
2521 * amdgpu_ras_recovery_init may fail, but the upper only cares the
2522 * failure from bad gpu situation and stop amdgpu init process
2523 * accordingly. For other failed cases, it will still release all
2524 * the resource and print error message, rather than returning one
2525 * negative value to upper level.
2527 * Note: theoretically, this should be called before all vram allocations
2528 * to protect retired page from abusing
2530 r = amdgpu_ras_recovery_init(adev);
2535 * In case of XGMI grab extra reference for reset domain for this device
2537 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2538 if (amdgpu_xgmi_add_device(adev) == 0) {
2539 if (!amdgpu_sriov_vf(adev)) {
2540 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
2542 if (WARN_ON(!hive)) {
2547 if (!hive->reset_domain ||
2548 !amdgpu_reset_get_reset_domain(hive->reset_domain)) {
2550 amdgpu_put_xgmi_hive(hive);
2554 /* Drop the early temporary reset domain we created for device */
2555 amdgpu_reset_put_reset_domain(adev->reset_domain);
2556 adev->reset_domain = hive->reset_domain;
2557 amdgpu_put_xgmi_hive(hive);
2562 r = amdgpu_device_init_schedulers(adev);
2566 /* Don't init kfd if whole hive need to be reset during init */
2567 if (!adev->gmc.xgmi.pending_reset) {
2568 kgd2kfd_init_zone_device(adev);
2569 amdgpu_amdkfd_device_init(adev);
2572 amdgpu_fru_get_product_info(adev);
2580 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
2582 * @adev: amdgpu_device pointer
2584 * Writes a reset magic value to the gart pointer in VRAM. The driver calls
2585 * this function before a GPU reset. If the value is retained after a
2586 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
2588 static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
2590 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
2594 * amdgpu_device_check_vram_lost - check if vram is valid
2596 * @adev: amdgpu_device pointer
2598 * Checks the reset magic value written to the gart pointer in VRAM.
2599 * The driver calls this after a GPU reset to see if the contents of
2600 * VRAM is lost or now.
2601 * returns true if vram is lost, false if not.
2603 static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
2605 if (memcmp(adev->gart.ptr, adev->reset_magic,
2606 AMDGPU_RESET_MAGIC_NUM))
2609 if (!amdgpu_in_reset(adev))
2613 * For all ASICs with baco/mode1 reset, the VRAM is
2614 * always assumed to be lost.
2616 switch (amdgpu_asic_reset_method(adev)) {
2617 case AMD_RESET_METHOD_BACO:
2618 case AMD_RESET_METHOD_MODE1:
2626 * amdgpu_device_set_cg_state - set clockgating for amdgpu device
2628 * @adev: amdgpu_device pointer
2629 * @state: clockgating state (gate or ungate)
2631 * The list of all the hardware IPs that make up the asic is walked and the
2632 * set_clockgating_state callbacks are run.
2633 * Late initialization pass enabling clockgating for hardware IPs.
2634 * Fini or suspend, pass disabling clockgating for hardware IPs.
2635 * Returns 0 on success, negative error code on failure.
2638 int amdgpu_device_set_cg_state(struct amdgpu_device *adev,
2639 enum amd_clockgating_state state)
2643 if (amdgpu_emu_mode == 1)
2646 for (j = 0; j < adev->num_ip_blocks; j++) {
2647 i = state == AMD_CG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
2648 if (!adev->ip_blocks[i].status.late_initialized)
2650 /* skip CG for GFX, SDMA on S0ix */
2651 if (adev->in_s0ix &&
2652 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX ||
2653 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SDMA))
2655 /* skip CG for VCE/UVD, it's handled specially */
2656 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
2657 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
2658 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
2659 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
2660 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
2661 /* enable clockgating to save power */
2662 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
2665 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
2666 adev->ip_blocks[i].version->funcs->name, r);
2675 int amdgpu_device_set_pg_state(struct amdgpu_device *adev,
2676 enum amd_powergating_state state)
2680 if (amdgpu_emu_mode == 1)
2683 for (j = 0; j < adev->num_ip_blocks; j++) {
2684 i = state == AMD_PG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
2685 if (!adev->ip_blocks[i].status.late_initialized)
2687 /* skip PG for GFX, SDMA on S0ix */
2688 if (adev->in_s0ix &&
2689 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX ||
2690 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SDMA))
2692 /* skip CG for VCE/UVD, it's handled specially */
2693 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
2694 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
2695 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
2696 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
2697 adev->ip_blocks[i].version->funcs->set_powergating_state) {
2698 /* enable powergating to save power */
2699 r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
2702 DRM_ERROR("set_powergating_state(gate) of IP block <%s> failed %d\n",
2703 adev->ip_blocks[i].version->funcs->name, r);
2711 static int amdgpu_device_enable_mgpu_fan_boost(void)
2713 struct amdgpu_gpu_instance *gpu_ins;
2714 struct amdgpu_device *adev;
2717 mutex_lock(&mgpu_info.mutex);
2720 * MGPU fan boost feature should be enabled
2721 * only when there are two or more dGPUs in
2724 if (mgpu_info.num_dgpu < 2)
2727 for (i = 0; i < mgpu_info.num_dgpu; i++) {
2728 gpu_ins = &(mgpu_info.gpu_ins[i]);
2729 adev = gpu_ins->adev;
2730 if (!(adev->flags & AMD_IS_APU) &&
2731 !gpu_ins->mgpu_fan_enabled) {
2732 ret = amdgpu_dpm_enable_mgpu_fan_boost(adev);
2736 gpu_ins->mgpu_fan_enabled = 1;
2741 mutex_unlock(&mgpu_info.mutex);
2747 * amdgpu_device_ip_late_init - run late init for hardware IPs
2749 * @adev: amdgpu_device pointer
2751 * Late initialization pass for hardware IPs. The list of all the hardware
2752 * IPs that make up the asic is walked and the late_init callbacks are run.
2753 * late_init covers any special initialization that an IP requires
2754 * after all of the have been initialized or something that needs to happen
2755 * late in the init process.
2756 * Returns 0 on success, negative error code on failure.
2758 static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
2760 struct amdgpu_gpu_instance *gpu_instance;
2763 for (i = 0; i < adev->num_ip_blocks; i++) {
2764 if (!adev->ip_blocks[i].status.hw)
2766 if (adev->ip_blocks[i].version->funcs->late_init) {
2767 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
2769 DRM_ERROR("late_init of IP block <%s> failed %d\n",
2770 adev->ip_blocks[i].version->funcs->name, r);
2774 adev->ip_blocks[i].status.late_initialized = true;
2777 r = amdgpu_ras_late_init(adev);
2779 DRM_ERROR("amdgpu_ras_late_init failed %d", r);
2783 amdgpu_ras_set_error_query_ready(adev, true);
2785 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE);
2786 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_GATE);
2788 amdgpu_device_fill_reset_magic(adev);
2790 r = amdgpu_device_enable_mgpu_fan_boost();
2792 DRM_ERROR("enable mgpu fan boost failed (%d).\n", r);
2794 /* For passthrough configuration on arcturus and aldebaran, enable special handling SBR */
2795 if (amdgpu_passthrough(adev) &&
2796 ((adev->asic_type == CHIP_ARCTURUS && adev->gmc.xgmi.num_physical_nodes > 1) ||
2797 adev->asic_type == CHIP_ALDEBARAN))
2798 amdgpu_dpm_handle_passthrough_sbr(adev, true);
2800 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2801 mutex_lock(&mgpu_info.mutex);
2804 * Reset device p-state to low as this was booted with high.
2806 * This should be performed only after all devices from the same
2807 * hive get initialized.
2809 * However, it's unknown how many device in the hive in advance.
2810 * As this is counted one by one during devices initializations.
2812 * So, we wait for all XGMI interlinked devices initialized.
2813 * This may bring some delays as those devices may come from
2814 * different hives. But that should be OK.
2816 if (mgpu_info.num_dgpu == adev->gmc.xgmi.num_physical_nodes) {
2817 for (i = 0; i < mgpu_info.num_gpu; i++) {
2818 gpu_instance = &(mgpu_info.gpu_ins[i]);
2819 if (gpu_instance->adev->flags & AMD_IS_APU)
2822 r = amdgpu_xgmi_set_pstate(gpu_instance->adev,
2823 AMDGPU_XGMI_PSTATE_MIN);
2825 DRM_ERROR("pstate setting failed (%d).\n", r);
2831 mutex_unlock(&mgpu_info.mutex);
2838 * amdgpu_device_smu_fini_early - smu hw_fini wrapper
2840 * @adev: amdgpu_device pointer
2842 * For ASICs need to disable SMC first
2844 static void amdgpu_device_smu_fini_early(struct amdgpu_device *adev)
2848 if (adev->ip_versions[GC_HWIP][0] > IP_VERSION(9, 0, 0))
2851 for (i = 0; i < adev->num_ip_blocks; i++) {
2852 if (!adev->ip_blocks[i].status.hw)
2854 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
2855 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2856 /* XXX handle errors */
2858 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2859 adev->ip_blocks[i].version->funcs->name, r);
2861 adev->ip_blocks[i].status.hw = false;
2867 static int amdgpu_device_ip_fini_early(struct amdgpu_device *adev)
2871 for (i = 0; i < adev->num_ip_blocks; i++) {
2872 if (!adev->ip_blocks[i].version->funcs->early_fini)
2875 r = adev->ip_blocks[i].version->funcs->early_fini((void *)adev);
2877 DRM_DEBUG("early_fini of IP block <%s> failed %d\n",
2878 adev->ip_blocks[i].version->funcs->name, r);
2882 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2883 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2885 amdgpu_amdkfd_suspend(adev, false);
2887 /* Workaroud for ASICs need to disable SMC first */
2888 amdgpu_device_smu_fini_early(adev);
2890 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2891 if (!adev->ip_blocks[i].status.hw)
2894 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2895 /* XXX handle errors */
2897 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2898 adev->ip_blocks[i].version->funcs->name, r);
2901 adev->ip_blocks[i].status.hw = false;
2904 if (amdgpu_sriov_vf(adev)) {
2905 if (amdgpu_virt_release_full_gpu(adev, false))
2906 DRM_ERROR("failed to release exclusive mode on fini\n");
2913 * amdgpu_device_ip_fini - run fini for hardware IPs
2915 * @adev: amdgpu_device pointer
2917 * Main teardown pass for hardware IPs. The list of all the hardware
2918 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
2919 * are run. hw_fini tears down the hardware associated with each IP
2920 * and sw_fini tears down any software state associated with each IP.
2921 * Returns 0 on success, negative error code on failure.
2923 static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
2927 if (amdgpu_sriov_vf(adev) && adev->virt.ras_init_done)
2928 amdgpu_virt_release_ras_err_handler_data(adev);
2930 if (adev->gmc.xgmi.num_physical_nodes > 1)
2931 amdgpu_xgmi_remove_device(adev);
2933 amdgpu_amdkfd_device_fini_sw(adev);
2935 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2936 if (!adev->ip_blocks[i].status.sw)
2939 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
2940 amdgpu_ucode_free_bo(adev);
2941 amdgpu_free_static_csa(&adev->virt.csa_obj);
2942 amdgpu_device_wb_fini(adev);
2943 amdgpu_device_mem_scratch_fini(adev);
2944 amdgpu_ib_pool_fini(adev);
2947 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
2948 /* XXX handle errors */
2950 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
2951 adev->ip_blocks[i].version->funcs->name, r);
2953 adev->ip_blocks[i].status.sw = false;
2954 adev->ip_blocks[i].status.valid = false;
2957 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2958 if (!adev->ip_blocks[i].status.late_initialized)
2960 if (adev->ip_blocks[i].version->funcs->late_fini)
2961 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
2962 adev->ip_blocks[i].status.late_initialized = false;
2965 amdgpu_ras_fini(adev);
2971 * amdgpu_device_delayed_init_work_handler - work handler for IB tests
2973 * @work: work_struct.
2975 static void amdgpu_device_delayed_init_work_handler(struct work_struct *work)
2977 struct amdgpu_device *adev =
2978 container_of(work, struct amdgpu_device, delayed_init_work.work);
2981 r = amdgpu_ib_ring_tests(adev);
2983 DRM_ERROR("ib ring test failed (%d).\n", r);
2986 static void amdgpu_device_delay_enable_gfx_off(struct work_struct *work)
2988 struct amdgpu_device *adev =
2989 container_of(work, struct amdgpu_device, gfx.gfx_off_delay_work.work);
2991 WARN_ON_ONCE(adev->gfx.gfx_off_state);
2992 WARN_ON_ONCE(adev->gfx.gfx_off_req_count);
2994 if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true))
2995 adev->gfx.gfx_off_state = true;
2999 * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1)
3001 * @adev: amdgpu_device pointer
3003 * Main suspend function for hardware IPs. The list of all the hardware
3004 * IPs that make up the asic is walked, clockgating is disabled and the
3005 * suspend callbacks are run. suspend puts the hardware and software state
3006 * in each IP into a state suitable for suspend.
3007 * Returns 0 on success, negative error code on failure.
3009 static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev)
3013 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
3014 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
3017 * Per PMFW team's suggestion, driver needs to handle gfxoff
3018 * and df cstate features disablement for gpu reset(e.g. Mode1Reset)
3019 * scenario. Add the missing df cstate disablement here.
3021 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_DISALLOW))
3022 dev_warn(adev->dev, "Failed to disallow df cstate");
3024 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
3025 if (!adev->ip_blocks[i].status.valid)
3028 /* displays are handled separately */
3029 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_DCE)
3032 /* XXX handle errors */
3033 r = adev->ip_blocks[i].version->funcs->suspend(adev);
3034 /* XXX handle errors */
3036 DRM_ERROR("suspend of IP block <%s> failed %d\n",
3037 adev->ip_blocks[i].version->funcs->name, r);
3041 adev->ip_blocks[i].status.hw = false;
3048 * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2)
3050 * @adev: amdgpu_device pointer
3052 * Main suspend function for hardware IPs. The list of all the hardware
3053 * IPs that make up the asic is walked, clockgating is disabled and the
3054 * suspend callbacks are run. suspend puts the hardware and software state
3055 * in each IP into a state suitable for suspend.
3056 * Returns 0 on success, negative error code on failure.
3058 static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev)
3063 amdgpu_dpm_gfx_state_change(adev, sGpuChangeState_D3Entry);
3065 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
3066 if (!adev->ip_blocks[i].status.valid)
3068 /* displays are handled in phase1 */
3069 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)
3071 /* PSP lost connection when err_event_athub occurs */
3072 if (amdgpu_ras_intr_triggered() &&
3073 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
3074 adev->ip_blocks[i].status.hw = false;
3078 /* skip unnecessary suspend if we do not initialize them yet */
3079 if (adev->gmc.xgmi.pending_reset &&
3080 !(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3081 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC ||
3082 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3083 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)) {
3084 adev->ip_blocks[i].status.hw = false;
3088 /* skip suspend of gfx/mes and psp for S0ix
3089 * gfx is in gfxoff state, so on resume it will exit gfxoff just
3090 * like at runtime. PSP is also part of the always on hardware
3091 * so no need to suspend it.
3093 if (adev->in_s0ix &&
3094 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP ||
3095 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX ||
3096 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_MES))
3099 /* SDMA 5.x+ is part of GFX power domain so it's covered by GFXOFF */
3100 if (adev->in_s0ix &&
3101 (adev->ip_versions[SDMA0_HWIP][0] >= IP_VERSION(5, 0, 0)) &&
3102 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SDMA))
3105 /* Once swPSP provides the IMU, RLC FW binaries to TOS during cold-boot.
3106 * These are in TMR, hence are expected to be reused by PSP-TOS to reload
3107 * from this location and RLC Autoload automatically also gets loaded
3108 * from here based on PMFW -> PSP message during re-init sequence.
3109 * Therefore, the psp suspend & resume should be skipped to avoid destroy
3110 * the TMR and reload FWs again for IMU enabled APU ASICs.
3112 if (amdgpu_in_reset(adev) &&
3113 (adev->flags & AMD_IS_APU) && adev->gfx.imu.funcs &&
3114 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)
3117 /* XXX handle errors */
3118 r = adev->ip_blocks[i].version->funcs->suspend(adev);
3119 /* XXX handle errors */
3121 DRM_ERROR("suspend of IP block <%s> failed %d\n",
3122 adev->ip_blocks[i].version->funcs->name, r);
3124 adev->ip_blocks[i].status.hw = false;
3125 /* handle putting the SMC in the appropriate state */
3126 if (!amdgpu_sriov_vf(adev)) {
3127 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
3128 r = amdgpu_dpm_set_mp1_state(adev, adev->mp1_state);
3130 DRM_ERROR("SMC failed to set mp1 state %d, %d\n",
3131 adev->mp1_state, r);
3142 * amdgpu_device_ip_suspend - run suspend for hardware IPs
3144 * @adev: amdgpu_device pointer
3146 * Main suspend function for hardware IPs. The list of all the hardware
3147 * IPs that make up the asic is walked, clockgating is disabled and the
3148 * suspend callbacks are run. suspend puts the hardware and software state
3149 * in each IP into a state suitable for suspend.
3150 * Returns 0 on success, negative error code on failure.
3152 int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
3156 if (amdgpu_sriov_vf(adev)) {
3157 amdgpu_virt_fini_data_exchange(adev);
3158 amdgpu_virt_request_full_gpu(adev, false);
3161 r = amdgpu_device_ip_suspend_phase1(adev);
3164 r = amdgpu_device_ip_suspend_phase2(adev);
3166 if (amdgpu_sriov_vf(adev))
3167 amdgpu_virt_release_full_gpu(adev, false);
3172 static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
3176 static enum amd_ip_block_type ip_order[] = {
3177 AMD_IP_BLOCK_TYPE_COMMON,
3178 AMD_IP_BLOCK_TYPE_GMC,
3179 AMD_IP_BLOCK_TYPE_PSP,
3180 AMD_IP_BLOCK_TYPE_IH,
3183 for (i = 0; i < adev->num_ip_blocks; i++) {
3185 struct amdgpu_ip_block *block;
3187 block = &adev->ip_blocks[i];
3188 block->status.hw = false;
3190 for (j = 0; j < ARRAY_SIZE(ip_order); j++) {
3192 if (block->version->type != ip_order[j] ||
3193 !block->status.valid)
3196 r = block->version->funcs->hw_init(adev);
3197 DRM_INFO("RE-INIT-early: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
3200 block->status.hw = true;
3207 static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
3211 static enum amd_ip_block_type ip_order[] = {
3212 AMD_IP_BLOCK_TYPE_SMC,
3213 AMD_IP_BLOCK_TYPE_DCE,
3214 AMD_IP_BLOCK_TYPE_GFX,
3215 AMD_IP_BLOCK_TYPE_SDMA,
3216 AMD_IP_BLOCK_TYPE_MES,
3217 AMD_IP_BLOCK_TYPE_UVD,
3218 AMD_IP_BLOCK_TYPE_VCE,
3219 AMD_IP_BLOCK_TYPE_VCN,
3220 AMD_IP_BLOCK_TYPE_JPEG
3223 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
3225 struct amdgpu_ip_block *block;
3227 for (j = 0; j < adev->num_ip_blocks; j++) {
3228 block = &adev->ip_blocks[j];
3230 if (block->version->type != ip_order[i] ||
3231 !block->status.valid ||
3235 if (block->version->type == AMD_IP_BLOCK_TYPE_SMC)
3236 r = block->version->funcs->resume(adev);
3238 r = block->version->funcs->hw_init(adev);
3240 DRM_INFO("RE-INIT-late: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
3243 block->status.hw = true;
3251 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
3253 * @adev: amdgpu_device pointer
3255 * First resume function for hardware IPs. The list of all the hardware
3256 * IPs that make up the asic is walked and the resume callbacks are run for
3257 * COMMON, GMC, and IH. resume puts the hardware into a functional state
3258 * after a suspend and updates the software state as necessary. This
3259 * function is also used for restoring the GPU after a GPU reset.
3260 * Returns 0 on success, negative error code on failure.
3262 static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
3266 for (i = 0; i < adev->num_ip_blocks; i++) {
3267 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
3269 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3270 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3271 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3272 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP && amdgpu_sriov_vf(adev))) {
3274 r = adev->ip_blocks[i].version->funcs->resume(adev);
3276 DRM_ERROR("resume of IP block <%s> failed %d\n",
3277 adev->ip_blocks[i].version->funcs->name, r);
3280 adev->ip_blocks[i].status.hw = true;
3288 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
3290 * @adev: amdgpu_device pointer
3292 * First resume function for hardware IPs. The list of all the hardware
3293 * IPs that make up the asic is walked and the resume callbacks are run for
3294 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
3295 * functional state after a suspend and updates the software state as
3296 * necessary. This function is also used for restoring the GPU after a GPU
3298 * Returns 0 on success, negative error code on failure.
3300 static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
3304 for (i = 0; i < adev->num_ip_blocks; i++) {
3305 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
3307 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3308 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3309 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3310 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)
3312 r = adev->ip_blocks[i].version->funcs->resume(adev);
3314 DRM_ERROR("resume of IP block <%s> failed %d\n",
3315 adev->ip_blocks[i].version->funcs->name, r);
3318 adev->ip_blocks[i].status.hw = true;
3325 * amdgpu_device_ip_resume - run resume for hardware IPs
3327 * @adev: amdgpu_device pointer
3329 * Main resume function for hardware IPs. The hardware IPs
3330 * are split into two resume functions because they are
3331 * also used in recovering from a GPU reset and some additional
3332 * steps need to be take between them. In this case (S3/S4) they are
3334 * Returns 0 on success, negative error code on failure.
3336 static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
3340 r = amdgpu_device_ip_resume_phase1(adev);
3344 r = amdgpu_device_fw_loading(adev);
3348 r = amdgpu_device_ip_resume_phase2(adev);
3354 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
3356 * @adev: amdgpu_device pointer
3358 * Query the VBIOS data tables to determine if the board supports SR-IOV.
3360 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
3362 if (amdgpu_sriov_vf(adev)) {
3363 if (adev->is_atom_fw) {
3364 if (amdgpu_atomfirmware_gpu_virtualization_supported(adev))
3365 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
3367 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
3368 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
3371 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
3372 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
3377 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
3379 * @asic_type: AMD asic type
3381 * Check if there is DC (new modesetting infrastructre) support for an asic.
3382 * returns true if DC has support, false if not.
3384 bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
3386 switch (asic_type) {
3387 #ifdef CONFIG_DRM_AMDGPU_SI
3391 /* chips with no display hardware */
3393 #if defined(CONFIG_DRM_AMD_DC)
3399 * We have systems in the wild with these ASICs that require
3400 * LVDS and VGA support which is not supported with DC.
3402 * Fallback to the non-DC driver here by default so as not to
3403 * cause regressions.
3405 #if defined(CONFIG_DRM_AMD_DC_SI)
3406 return amdgpu_dc > 0;
3415 * We have systems in the wild with these ASICs that require
3416 * VGA support which is not supported with DC.
3418 * Fallback to the non-DC driver here by default so as not to
3419 * cause regressions.
3421 return amdgpu_dc > 0;
3423 return amdgpu_dc != 0;
3427 DRM_INFO_ONCE("Display Core has been requested via kernel parameter but isn't supported by ASIC, ignoring\n");
3434 * amdgpu_device_has_dc_support - check if dc is supported
3436 * @adev: amdgpu_device pointer
3438 * Returns true for supported, false for not supported
3440 bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
3442 if (adev->enable_virtual_display ||
3443 (adev->harvest_ip_mask & AMD_HARVEST_IP_DMU_MASK))
3446 return amdgpu_device_asic_has_dc_support(adev->asic_type);
3449 static void amdgpu_device_xgmi_reset_func(struct work_struct *__work)
3451 struct amdgpu_device *adev =
3452 container_of(__work, struct amdgpu_device, xgmi_reset_work);
3453 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
3455 /* It's a bug to not have a hive within this function */
3460 * Use task barrier to synchronize all xgmi reset works across the
3461 * hive. task_barrier_enter and task_barrier_exit will block
3462 * until all the threads running the xgmi reset works reach
3463 * those points. task_barrier_full will do both blocks.
3465 if (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) {
3467 task_barrier_enter(&hive->tb);
3468 adev->asic_reset_res = amdgpu_device_baco_enter(adev_to_drm(adev));
3470 if (adev->asic_reset_res)
3473 task_barrier_exit(&hive->tb);
3474 adev->asic_reset_res = amdgpu_device_baco_exit(adev_to_drm(adev));
3476 if (adev->asic_reset_res)
3479 if (adev->mmhub.ras && adev->mmhub.ras->ras_block.hw_ops &&
3480 adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count)
3481 adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count(adev);
3484 task_barrier_full(&hive->tb);
3485 adev->asic_reset_res = amdgpu_asic_reset(adev);
3489 if (adev->asic_reset_res)
3490 DRM_WARN("ASIC reset failed with error, %d for drm dev, %s",
3491 adev->asic_reset_res, adev_to_drm(adev)->unique);
3492 amdgpu_put_xgmi_hive(hive);
3495 static int amdgpu_device_get_job_timeout_settings(struct amdgpu_device *adev)
3497 char *input = amdgpu_lockup_timeout;
3498 char *timeout_setting = NULL;
3504 * By default timeout for non compute jobs is 10000
3505 * and 60000 for compute jobs.
3506 * In SR-IOV or passthrough mode, timeout for compute
3507 * jobs are 60000 by default.
3509 adev->gfx_timeout = msecs_to_jiffies(10000);
3510 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
3511 if (amdgpu_sriov_vf(adev))
3512 adev->compute_timeout = amdgpu_sriov_is_pp_one_vf(adev) ?
3513 msecs_to_jiffies(60000) : msecs_to_jiffies(10000);
3515 adev->compute_timeout = msecs_to_jiffies(60000);
3517 if (strnlen(input, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
3518 while ((timeout_setting = strsep(&input, ",")) &&
3519 strnlen(timeout_setting, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
3520 ret = kstrtol(timeout_setting, 0, &timeout);
3527 } else if (timeout < 0) {
3528 timeout = MAX_SCHEDULE_TIMEOUT;
3529 dev_warn(adev->dev, "lockup timeout disabled");
3530 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
3532 timeout = msecs_to_jiffies(timeout);
3537 adev->gfx_timeout = timeout;
3540 adev->compute_timeout = timeout;
3543 adev->sdma_timeout = timeout;
3546 adev->video_timeout = timeout;
3553 * There is only one value specified and
3554 * it should apply to all non-compute jobs.
3557 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
3558 if (amdgpu_sriov_vf(adev) || amdgpu_passthrough(adev))
3559 adev->compute_timeout = adev->gfx_timeout;
3567 * amdgpu_device_check_iommu_direct_map - check if RAM direct mapped to GPU
3569 * @adev: amdgpu_device pointer
3571 * RAM direct mapped to GPU if IOMMU is not enabled or is pass through mode
3573 static void amdgpu_device_check_iommu_direct_map(struct amdgpu_device *adev)
3575 struct iommu_domain *domain;
3577 domain = iommu_get_domain_for_dev(adev->dev);
3578 if (!domain || domain->type == IOMMU_DOMAIN_IDENTITY)
3579 adev->ram_is_direct_mapped = true;
3582 static const struct attribute *amdgpu_dev_attributes[] = {
3583 &dev_attr_pcie_replay_count.attr,
3587 static void amdgpu_device_set_mcbp(struct amdgpu_device *adev)
3589 if (amdgpu_mcbp == 1)
3590 adev->gfx.mcbp = true;
3591 else if (amdgpu_mcbp == 0)
3592 adev->gfx.mcbp = false;
3593 else if ((adev->ip_versions[GC_HWIP][0] >= IP_VERSION(9, 0, 0)) &&
3594 (adev->ip_versions[GC_HWIP][0] < IP_VERSION(10, 0, 0)) &&
3595 adev->gfx.num_gfx_rings)
3596 adev->gfx.mcbp = true;
3598 if (amdgpu_sriov_vf(adev))
3599 adev->gfx.mcbp = true;
3602 DRM_INFO("MCBP is enabled\n");
3606 * amdgpu_device_init - initialize the driver
3608 * @adev: amdgpu_device pointer
3609 * @flags: driver flags
3611 * Initializes the driver info and hw (all asics).
3612 * Returns 0 for success or an error on failure.
3613 * Called at driver startup.
3615 int amdgpu_device_init(struct amdgpu_device *adev,
3618 struct drm_device *ddev = adev_to_drm(adev);
3619 struct pci_dev *pdev = adev->pdev;
3625 adev->shutdown = false;
3626 adev->flags = flags;
3628 if (amdgpu_force_asic_type >= 0 && amdgpu_force_asic_type < CHIP_LAST)
3629 adev->asic_type = amdgpu_force_asic_type;
3631 adev->asic_type = flags & AMD_ASIC_MASK;
3633 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
3634 if (amdgpu_emu_mode == 1)
3635 adev->usec_timeout *= 10;
3636 adev->gmc.gart_size = 512 * 1024 * 1024;
3637 adev->accel_working = false;
3638 adev->num_rings = 0;
3639 RCU_INIT_POINTER(adev->gang_submit, dma_fence_get_stub());
3640 adev->mman.buffer_funcs = NULL;
3641 adev->mman.buffer_funcs_ring = NULL;
3642 adev->vm_manager.vm_pte_funcs = NULL;
3643 adev->vm_manager.vm_pte_num_scheds = 0;
3644 adev->gmc.gmc_funcs = NULL;
3645 adev->harvest_ip_mask = 0x0;
3646 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
3647 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
3649 adev->smc_rreg = &amdgpu_invalid_rreg;
3650 adev->smc_wreg = &amdgpu_invalid_wreg;
3651 adev->pcie_rreg = &amdgpu_invalid_rreg;
3652 adev->pcie_wreg = &amdgpu_invalid_wreg;
3653 adev->pcie_rreg_ext = &amdgpu_invalid_rreg_ext;
3654 adev->pcie_wreg_ext = &amdgpu_invalid_wreg_ext;
3655 adev->pciep_rreg = &amdgpu_invalid_rreg;
3656 adev->pciep_wreg = &amdgpu_invalid_wreg;
3657 adev->pcie_rreg64 = &amdgpu_invalid_rreg64;
3658 adev->pcie_wreg64 = &amdgpu_invalid_wreg64;
3659 adev->pcie_rreg64_ext = &amdgpu_invalid_rreg64_ext;
3660 adev->pcie_wreg64_ext = &amdgpu_invalid_wreg64_ext;
3661 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
3662 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
3663 adev->didt_rreg = &amdgpu_invalid_rreg;
3664 adev->didt_wreg = &amdgpu_invalid_wreg;
3665 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
3666 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
3667 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
3668 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
3670 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
3671 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
3672 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
3674 /* mutex initialization are all done here so we
3675 * can recall function without having locking issues
3677 mutex_init(&adev->firmware.mutex);
3678 mutex_init(&adev->pm.mutex);
3679 mutex_init(&adev->gfx.gpu_clock_mutex);
3680 mutex_init(&adev->srbm_mutex);
3681 mutex_init(&adev->gfx.pipe_reserve_mutex);
3682 mutex_init(&adev->gfx.gfx_off_mutex);
3683 mutex_init(&adev->gfx.partition_mutex);
3684 mutex_init(&adev->grbm_idx_mutex);
3685 mutex_init(&adev->mn_lock);
3686 mutex_init(&adev->virt.vf_errors.lock);
3687 hash_init(adev->mn_hash);
3688 mutex_init(&adev->psp.mutex);
3689 mutex_init(&adev->notifier_lock);
3690 mutex_init(&adev->pm.stable_pstate_ctx_lock);
3691 mutex_init(&adev->benchmark_mutex);
3693 amdgpu_device_init_apu_flags(adev);
3695 r = amdgpu_device_check_arguments(adev);
3699 spin_lock_init(&adev->mmio_idx_lock);
3700 spin_lock_init(&adev->smc_idx_lock);
3701 spin_lock_init(&adev->pcie_idx_lock);
3702 spin_lock_init(&adev->uvd_ctx_idx_lock);
3703 spin_lock_init(&adev->didt_idx_lock);
3704 spin_lock_init(&adev->gc_cac_idx_lock);
3705 spin_lock_init(&adev->se_cac_idx_lock);
3706 spin_lock_init(&adev->audio_endpt_idx_lock);
3707 spin_lock_init(&adev->mm_stats.lock);
3709 INIT_LIST_HEAD(&adev->shadow_list);
3710 mutex_init(&adev->shadow_list_lock);
3712 INIT_LIST_HEAD(&adev->reset_list);
3714 INIT_LIST_HEAD(&adev->ras_list);
3716 INIT_LIST_HEAD(&adev->pm.od_kobj_list);
3718 INIT_DELAYED_WORK(&adev->delayed_init_work,
3719 amdgpu_device_delayed_init_work_handler);
3720 INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work,
3721 amdgpu_device_delay_enable_gfx_off);
3723 INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
3725 adev->gfx.gfx_off_req_count = 1;
3726 adev->gfx.gfx_off_residency = 0;
3727 adev->gfx.gfx_off_entrycount = 0;
3728 adev->pm.ac_power = power_supply_is_system_supplied() > 0;
3730 atomic_set(&adev->throttling_logging_enabled, 1);
3732 * If throttling continues, logging will be performed every minute
3733 * to avoid log flooding. "-1" is subtracted since the thermal
3734 * throttling interrupt comes every second. Thus, the total logging
3735 * interval is 59 seconds(retelimited printk interval) + 1(waiting
3736 * for throttling interrupt) = 60 seconds.
3738 ratelimit_state_init(&adev->throttling_logging_rs, (60 - 1) * HZ, 1);
3739 ratelimit_set_flags(&adev->throttling_logging_rs, RATELIMIT_MSG_ON_RELEASE);
3741 /* Registers mapping */
3742 /* TODO: block userspace mapping of io register */
3743 if (adev->asic_type >= CHIP_BONAIRE) {
3744 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
3745 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
3747 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
3748 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
3751 for (i = 0; i < AMD_IP_BLOCK_TYPE_NUM; i++)
3752 atomic_set(&adev->pm.pwr_state[i], POWER_STATE_UNKNOWN);
3754 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
3758 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
3759 DRM_INFO("register mmio size: %u\n", (unsigned int)adev->rmmio_size);
3762 * Reset domain needs to be present early, before XGMI hive discovered
3763 * (if any) and intitialized to use reset sem and in_gpu reset flag
3764 * early on during init and before calling to RREG32.
3766 adev->reset_domain = amdgpu_reset_create_reset_domain(SINGLE_DEVICE, "amdgpu-reset-dev");
3767 if (!adev->reset_domain)
3770 /* detect hw virtualization here */
3771 amdgpu_detect_virtualization(adev);
3773 amdgpu_device_get_pcie_info(adev);
3775 r = amdgpu_device_get_job_timeout_settings(adev);
3777 dev_err(adev->dev, "invalid lockup_timeout parameter syntax\n");
3781 /* early init functions */
3782 r = amdgpu_device_ip_early_init(adev);
3786 amdgpu_device_set_mcbp(adev);
3788 /* Get rid of things like offb */
3789 r = drm_aperture_remove_conflicting_pci_framebuffers(adev->pdev, &amdgpu_kms_driver);
3793 /* Enable TMZ based on IP_VERSION */
3794 amdgpu_gmc_tmz_set(adev);
3796 amdgpu_gmc_noretry_set(adev);
3797 /* Need to get xgmi info early to decide the reset behavior*/
3798 if (adev->gmc.xgmi.supported) {
3799 r = adev->gfxhub.funcs->get_xgmi_info(adev);
3804 /* enable PCIE atomic ops */
3805 if (amdgpu_sriov_vf(adev)) {
3806 if (adev->virt.fw_reserve.p_pf2vf)
3807 adev->have_atomics_support = ((struct amd_sriov_msg_pf2vf_info *)
3808 adev->virt.fw_reserve.p_pf2vf)->pcie_atomic_ops_support_flags ==
3809 (PCI_EXP_DEVCAP2_ATOMIC_COMP32 | PCI_EXP_DEVCAP2_ATOMIC_COMP64);
3810 /* APUs w/ gfx9 onwards doesn't reply on PCIe atomics, rather it is a
3811 * internal path natively support atomics, set have_atomics_support to true.
3813 } else if ((adev->flags & AMD_IS_APU) &&
3814 (adev->ip_versions[GC_HWIP][0] > IP_VERSION(9, 0, 0))) {
3815 adev->have_atomics_support = true;
3817 adev->have_atomics_support =
3818 !pci_enable_atomic_ops_to_root(adev->pdev,
3819 PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
3820 PCI_EXP_DEVCAP2_ATOMIC_COMP64);
3823 if (!adev->have_atomics_support)
3824 dev_info(adev->dev, "PCIE atomic ops is not supported\n");
3826 /* doorbell bar mapping and doorbell index init*/
3827 amdgpu_doorbell_init(adev);
3829 if (amdgpu_emu_mode == 1) {
3830 /* post the asic on emulation mode */
3831 emu_soc_asic_init(adev);
3832 goto fence_driver_init;
3835 amdgpu_reset_init(adev);
3837 /* detect if we are with an SRIOV vbios */
3839 amdgpu_device_detect_sriov_bios(adev);
3841 /* check if we need to reset the asic
3842 * E.g., driver was not cleanly unloaded previously, etc.
3844 if (!amdgpu_sriov_vf(adev) && amdgpu_asic_need_reset_on_init(adev)) {
3845 if (adev->gmc.xgmi.num_physical_nodes) {
3846 dev_info(adev->dev, "Pending hive reset.\n");
3847 adev->gmc.xgmi.pending_reset = true;
3848 /* Only need to init necessary block for SMU to handle the reset */
3849 for (i = 0; i < adev->num_ip_blocks; i++) {
3850 if (!adev->ip_blocks[i].status.valid)
3852 if (!(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3853 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3854 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3855 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC)) {
3856 DRM_DEBUG("IP %s disabled for hw_init.\n",
3857 adev->ip_blocks[i].version->funcs->name);
3858 adev->ip_blocks[i].status.hw = true;
3862 tmp = amdgpu_reset_method;
3863 /* It should do a default reset when loading or reloading the driver,
3864 * regardless of the module parameter reset_method.
3866 amdgpu_reset_method = AMD_RESET_METHOD_NONE;
3867 r = amdgpu_asic_reset(adev);
3868 amdgpu_reset_method = tmp;
3870 dev_err(adev->dev, "asic reset on init failed\n");
3876 /* Post card if necessary */
3877 if (amdgpu_device_need_post(adev)) {
3879 dev_err(adev->dev, "no vBIOS found\n");
3883 DRM_INFO("GPU posting now...\n");
3884 r = amdgpu_device_asic_init(adev);
3886 dev_err(adev->dev, "gpu post error!\n");
3892 if (adev->is_atom_fw) {
3893 /* Initialize clocks */
3894 r = amdgpu_atomfirmware_get_clock_info(adev);
3896 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
3897 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3901 /* Initialize clocks */
3902 r = amdgpu_atombios_get_clock_info(adev);
3904 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
3905 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3908 /* init i2c buses */
3909 if (!amdgpu_device_has_dc_support(adev))
3910 amdgpu_atombios_i2c_init(adev);
3916 r = amdgpu_fence_driver_sw_init(adev);
3918 dev_err(adev->dev, "amdgpu_fence_driver_sw_init failed\n");
3919 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
3923 /* init the mode config */
3924 drm_mode_config_init(adev_to_drm(adev));
3926 r = amdgpu_device_ip_init(adev);
3928 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
3929 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
3930 goto release_ras_con;
3933 amdgpu_fence_driver_hw_init(adev);
3936 "SE %d, SH per SE %d, CU per SH %d, active_cu_number %d\n",
3937 adev->gfx.config.max_shader_engines,
3938 adev->gfx.config.max_sh_per_se,
3939 adev->gfx.config.max_cu_per_sh,
3940 adev->gfx.cu_info.number);
3942 adev->accel_working = true;
3944 amdgpu_vm_check_compute_bug(adev);
3946 /* Initialize the buffer migration limit. */
3947 if (amdgpu_moverate >= 0)
3948 max_MBps = amdgpu_moverate;
3950 max_MBps = 8; /* Allow 8 MB/s. */
3951 /* Get a log2 for easy divisions. */
3952 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
3955 * Register gpu instance before amdgpu_device_enable_mgpu_fan_boost.
3956 * Otherwise the mgpu fan boost feature will be skipped due to the
3957 * gpu instance is counted less.
3959 amdgpu_register_gpu_instance(adev);
3961 /* enable clockgating, etc. after ib tests, etc. since some blocks require
3962 * explicit gating rather than handling it automatically.
3964 if (!adev->gmc.xgmi.pending_reset) {
3965 r = amdgpu_device_ip_late_init(adev);
3967 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
3968 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
3969 goto release_ras_con;
3972 amdgpu_ras_resume(adev);
3973 queue_delayed_work(system_wq, &adev->delayed_init_work,
3974 msecs_to_jiffies(AMDGPU_RESUME_MS));
3977 if (amdgpu_sriov_vf(adev)) {
3978 amdgpu_virt_release_full_gpu(adev, true);
3979 flush_delayed_work(&adev->delayed_init_work);
3983 * Place those sysfs registering after `late_init`. As some of those
3984 * operations performed in `late_init` might affect the sysfs
3985 * interfaces creating.
3987 r = amdgpu_atombios_sysfs_init(adev);
3989 drm_err(&adev->ddev,
3990 "registering atombios sysfs failed (%d).\n", r);
3992 r = amdgpu_pm_sysfs_init(adev);
3994 DRM_ERROR("registering pm sysfs failed (%d).\n", r);
3996 r = amdgpu_ucode_sysfs_init(adev);
3998 adev->ucode_sysfs_en = false;
3999 DRM_ERROR("Creating firmware sysfs failed (%d).\n", r);
4001 adev->ucode_sysfs_en = true;
4003 r = sysfs_create_files(&adev->dev->kobj, amdgpu_dev_attributes);
4005 dev_err(adev->dev, "Could not create amdgpu device attr\n");
4007 amdgpu_fru_sysfs_init(adev);
4009 if (IS_ENABLED(CONFIG_PERF_EVENTS))
4010 r = amdgpu_pmu_init(adev);
4012 dev_err(adev->dev, "amdgpu_pmu_init failed\n");
4014 /* Have stored pci confspace at hand for restore in sudden PCI error */
4015 if (amdgpu_device_cache_pci_state(adev->pdev))
4016 pci_restore_state(pdev);
4018 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
4019 /* this will fail for cards that aren't VGA class devices, just
4022 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
4023 vga_client_register(adev->pdev, amdgpu_device_vga_set_decode);
4025 px = amdgpu_device_supports_px(ddev);
4027 if (px || (!pci_is_thunderbolt_attached(adev->pdev) &&
4028 apple_gmux_detect(NULL, NULL)))
4029 vga_switcheroo_register_client(adev->pdev,
4030 &amdgpu_switcheroo_ops, px);
4033 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
4035 if (adev->gmc.xgmi.pending_reset)
4036 queue_delayed_work(system_wq, &mgpu_info.delayed_reset_work,
4037 msecs_to_jiffies(AMDGPU_RESUME_MS));
4039 amdgpu_device_check_iommu_direct_map(adev);
4044 if (amdgpu_sriov_vf(adev))
4045 amdgpu_virt_release_full_gpu(adev, true);
4047 /* failed in exclusive mode due to timeout */
4048 if (amdgpu_sriov_vf(adev) &&
4049 !amdgpu_sriov_runtime(adev) &&
4050 amdgpu_virt_mmio_blocked(adev) &&
4051 !amdgpu_virt_wait_reset(adev)) {
4052 dev_err(adev->dev, "VF exclusive mode timeout\n");
4053 /* Don't send request since VF is inactive. */
4054 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
4055 adev->virt.ops = NULL;
4058 amdgpu_release_ras_context(adev);
4061 amdgpu_vf_error_trans_all(adev);
4066 static void amdgpu_device_unmap_mmio(struct amdgpu_device *adev)
4069 /* Clear all CPU mappings pointing to this device */
4070 unmap_mapping_range(adev->ddev.anon_inode->i_mapping, 0, 0, 1);
4072 /* Unmap all mapped bars - Doorbell, registers and VRAM */
4073 amdgpu_doorbell_fini(adev);
4075 iounmap(adev->rmmio);
4077 if (adev->mman.aper_base_kaddr)
4078 iounmap(adev->mman.aper_base_kaddr);
4079 adev->mman.aper_base_kaddr = NULL;
4081 /* Memory manager related */
4082 if (!adev->gmc.xgmi.connected_to_cpu && !adev->gmc.is_app_apu) {
4083 arch_phys_wc_del(adev->gmc.vram_mtrr);
4084 arch_io_free_memtype_wc(adev->gmc.aper_base, adev->gmc.aper_size);
4089 * amdgpu_device_fini_hw - tear down the driver
4091 * @adev: amdgpu_device pointer
4093 * Tear down the driver info (all asics).
4094 * Called at driver shutdown.
4096 void amdgpu_device_fini_hw(struct amdgpu_device *adev)
4098 dev_info(adev->dev, "amdgpu: finishing device.\n");
4099 flush_delayed_work(&adev->delayed_init_work);
4100 adev->shutdown = true;
4102 /* make sure IB test finished before entering exclusive mode
4103 * to avoid preemption on IB test
4105 if (amdgpu_sriov_vf(adev)) {
4106 amdgpu_virt_request_full_gpu(adev, false);
4107 amdgpu_virt_fini_data_exchange(adev);
4110 /* disable all interrupts */
4111 amdgpu_irq_disable_all(adev);
4112 if (adev->mode_info.mode_config_initialized) {
4113 if (!drm_drv_uses_atomic_modeset(adev_to_drm(adev)))
4114 drm_helper_force_disable_all(adev_to_drm(adev));
4116 drm_atomic_helper_shutdown(adev_to_drm(adev));
4118 amdgpu_fence_driver_hw_fini(adev);
4120 if (adev->mman.initialized)
4121 drain_workqueue(adev->mman.bdev.wq);
4123 if (adev->pm.sysfs_initialized)
4124 amdgpu_pm_sysfs_fini(adev);
4125 if (adev->ucode_sysfs_en)
4126 amdgpu_ucode_sysfs_fini(adev);
4127 sysfs_remove_files(&adev->dev->kobj, amdgpu_dev_attributes);
4128 amdgpu_fru_sysfs_fini(adev);
4130 /* disable ras feature must before hw fini */
4131 amdgpu_ras_pre_fini(adev);
4133 amdgpu_device_ip_fini_early(adev);
4135 amdgpu_irq_fini_hw(adev);
4137 if (adev->mman.initialized)
4138 ttm_device_clear_dma_mappings(&adev->mman.bdev);
4140 amdgpu_gart_dummy_page_fini(adev);
4142 if (drm_dev_is_unplugged(adev_to_drm(adev)))
4143 amdgpu_device_unmap_mmio(adev);
4147 void amdgpu_device_fini_sw(struct amdgpu_device *adev)
4152 amdgpu_fence_driver_sw_fini(adev);
4153 amdgpu_device_ip_fini(adev);
4154 amdgpu_ucode_release(&adev->firmware.gpu_info_fw);
4155 adev->accel_working = false;
4156 dma_fence_put(rcu_dereference_protected(adev->gang_submit, true));
4158 amdgpu_reset_fini(adev);
4160 /* free i2c buses */
4161 if (!amdgpu_device_has_dc_support(adev))
4162 amdgpu_i2c_fini(adev);
4164 if (amdgpu_emu_mode != 1)
4165 amdgpu_atombios_fini(adev);
4170 px = amdgpu_device_supports_px(adev_to_drm(adev));
4172 if (px || (!pci_is_thunderbolt_attached(adev->pdev) &&
4173 apple_gmux_detect(NULL, NULL)))
4174 vga_switcheroo_unregister_client(adev->pdev);
4177 vga_switcheroo_fini_domain_pm_ops(adev->dev);
4179 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
4180 vga_client_unregister(adev->pdev);
4182 if (drm_dev_enter(adev_to_drm(adev), &idx)) {
4184 iounmap(adev->rmmio);
4186 amdgpu_doorbell_fini(adev);
4190 if (IS_ENABLED(CONFIG_PERF_EVENTS))
4191 amdgpu_pmu_fini(adev);
4192 if (adev->mman.discovery_bin)
4193 amdgpu_discovery_fini(adev);
4195 amdgpu_reset_put_reset_domain(adev->reset_domain);
4196 adev->reset_domain = NULL;
4198 kfree(adev->pci_state);
4203 * amdgpu_device_evict_resources - evict device resources
4204 * @adev: amdgpu device object
4206 * Evicts all ttm device resources(vram BOs, gart table) from the lru list
4207 * of the vram memory type. Mainly used for evicting device resources
4211 static int amdgpu_device_evict_resources(struct amdgpu_device *adev)
4215 /* No need to evict vram on APUs for suspend to ram or s2idle */
4216 if ((adev->in_s3 || adev->in_s0ix) && (adev->flags & AMD_IS_APU))
4219 ret = amdgpu_ttm_evict_resources(adev, TTM_PL_VRAM);
4221 DRM_WARN("evicting device resources failed\n");
4229 * amdgpu_device_suspend - initiate device suspend
4231 * @dev: drm dev pointer
4232 * @fbcon : notify the fbdev of suspend
4234 * Puts the hw in the suspend state (all asics).
4235 * Returns 0 for success or an error on failure.
4236 * Called at driver suspend.
4238 int amdgpu_device_suspend(struct drm_device *dev, bool fbcon)
4240 struct amdgpu_device *adev = drm_to_adev(dev);
4243 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
4246 adev->in_suspend = true;
4248 /* Evict the majority of BOs before grabbing the full access */
4249 r = amdgpu_device_evict_resources(adev);
4253 if (amdgpu_sriov_vf(adev)) {
4254 amdgpu_virt_fini_data_exchange(adev);
4255 r = amdgpu_virt_request_full_gpu(adev, false);
4260 if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DEV_D3))
4261 DRM_WARN("smart shift update failed\n");
4264 drm_fb_helper_set_suspend_unlocked(adev_to_drm(adev)->fb_helper, true);
4266 cancel_delayed_work_sync(&adev->delayed_init_work);
4267 flush_delayed_work(&adev->gfx.gfx_off_delay_work);
4269 amdgpu_ras_suspend(adev);
4271 amdgpu_device_ip_suspend_phase1(adev);
4274 amdgpu_amdkfd_suspend(adev, adev->in_runpm);
4276 r = amdgpu_device_evict_resources(adev);
4280 amdgpu_fence_driver_hw_fini(adev);
4282 amdgpu_device_ip_suspend_phase2(adev);
4284 if (amdgpu_sriov_vf(adev))
4285 amdgpu_virt_release_full_gpu(adev, false);
4291 * amdgpu_device_resume - initiate device resume
4293 * @dev: drm dev pointer
4294 * @fbcon : notify the fbdev of resume
4296 * Bring the hw back to operating state (all asics).
4297 * Returns 0 for success or an error on failure.
4298 * Called at driver resume.
4300 int amdgpu_device_resume(struct drm_device *dev, bool fbcon)
4302 struct amdgpu_device *adev = drm_to_adev(dev);
4305 if (amdgpu_sriov_vf(adev)) {
4306 r = amdgpu_virt_request_full_gpu(adev, true);
4311 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
4315 amdgpu_dpm_gfx_state_change(adev, sGpuChangeState_D0Entry);
4318 if (amdgpu_device_need_post(adev)) {
4319 r = amdgpu_device_asic_init(adev);
4321 dev_err(adev->dev, "amdgpu asic init failed\n");
4324 r = amdgpu_device_ip_resume(adev);
4327 dev_err(adev->dev, "amdgpu_device_ip_resume failed (%d).\n", r);
4330 amdgpu_fence_driver_hw_init(adev);
4332 r = amdgpu_device_ip_late_init(adev);
4336 queue_delayed_work(system_wq, &adev->delayed_init_work,
4337 msecs_to_jiffies(AMDGPU_RESUME_MS));
4339 if (!adev->in_s0ix) {
4340 r = amdgpu_amdkfd_resume(adev, adev->in_runpm);
4346 if (amdgpu_sriov_vf(adev)) {
4347 amdgpu_virt_init_data_exchange(adev);
4348 amdgpu_virt_release_full_gpu(adev, true);
4354 /* Make sure IB tests flushed */
4355 flush_delayed_work(&adev->delayed_init_work);
4358 drm_fb_helper_set_suspend_unlocked(adev_to_drm(adev)->fb_helper, false);
4360 amdgpu_ras_resume(adev);
4362 if (adev->mode_info.num_crtc) {
4364 * Most of the connector probing functions try to acquire runtime pm
4365 * refs to ensure that the GPU is powered on when connector polling is
4366 * performed. Since we're calling this from a runtime PM callback,
4367 * trying to acquire rpm refs will cause us to deadlock.
4369 * Since we're guaranteed to be holding the rpm lock, it's safe to
4370 * temporarily disable the rpm helpers so this doesn't deadlock us.
4373 dev->dev->power.disable_depth++;
4375 if (!adev->dc_enabled)
4376 drm_helper_hpd_irq_event(dev);
4378 drm_kms_helper_hotplug_event(dev);
4380 dev->dev->power.disable_depth--;
4383 adev->in_suspend = false;
4385 if (adev->enable_mes)
4386 amdgpu_mes_self_test(adev);
4388 if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DEV_D0))
4389 DRM_WARN("smart shift update failed\n");
4395 * amdgpu_device_ip_check_soft_reset - did soft reset succeed
4397 * @adev: amdgpu_device pointer
4399 * The list of all the hardware IPs that make up the asic is walked and
4400 * the check_soft_reset callbacks are run. check_soft_reset determines
4401 * if the asic is still hung or not.
4402 * Returns true if any of the IPs are still in a hung state, false if not.
4404 static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
4407 bool asic_hang = false;
4409 if (amdgpu_sriov_vf(adev))
4412 if (amdgpu_asic_need_full_reset(adev))
4415 for (i = 0; i < adev->num_ip_blocks; i++) {
4416 if (!adev->ip_blocks[i].status.valid)
4418 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
4419 adev->ip_blocks[i].status.hang =
4420 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
4421 if (adev->ip_blocks[i].status.hang) {
4422 dev_info(adev->dev, "IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
4430 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
4432 * @adev: amdgpu_device pointer
4434 * The list of all the hardware IPs that make up the asic is walked and the
4435 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
4436 * handles any IP specific hardware or software state changes that are
4437 * necessary for a soft reset to succeed.
4438 * Returns 0 on success, negative error code on failure.
4440 static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
4444 for (i = 0; i < adev->num_ip_blocks; i++) {
4445 if (!adev->ip_blocks[i].status.valid)
4447 if (adev->ip_blocks[i].status.hang &&
4448 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
4449 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
4459 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
4461 * @adev: amdgpu_device pointer
4463 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
4464 * reset is necessary to recover.
4465 * Returns true if a full asic reset is required, false if not.
4467 static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
4471 if (amdgpu_asic_need_full_reset(adev))
4474 for (i = 0; i < adev->num_ip_blocks; i++) {
4475 if (!adev->ip_blocks[i].status.valid)
4477 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
4478 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
4479 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
4480 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
4481 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
4482 if (adev->ip_blocks[i].status.hang) {
4483 dev_info(adev->dev, "Some block need full reset!\n");
4492 * amdgpu_device_ip_soft_reset - do a soft reset
4494 * @adev: amdgpu_device pointer
4496 * The list of all the hardware IPs that make up the asic is walked and the
4497 * soft_reset callbacks are run if the block is hung. soft_reset handles any
4498 * IP specific hardware or software state changes that are necessary to soft
4500 * Returns 0 on success, negative error code on failure.
4502 static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
4506 for (i = 0; i < adev->num_ip_blocks; i++) {
4507 if (!adev->ip_blocks[i].status.valid)
4509 if (adev->ip_blocks[i].status.hang &&
4510 adev->ip_blocks[i].version->funcs->soft_reset) {
4511 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
4521 * amdgpu_device_ip_post_soft_reset - clean up from soft reset
4523 * @adev: amdgpu_device pointer
4525 * The list of all the hardware IPs that make up the asic is walked and the
4526 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
4527 * handles any IP specific hardware or software state changes that are
4528 * necessary after the IP has been soft reset.
4529 * Returns 0 on success, negative error code on failure.
4531 static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
4535 for (i = 0; i < adev->num_ip_blocks; i++) {
4536 if (!adev->ip_blocks[i].status.valid)
4538 if (adev->ip_blocks[i].status.hang &&
4539 adev->ip_blocks[i].version->funcs->post_soft_reset)
4540 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
4549 * amdgpu_device_recover_vram - Recover some VRAM contents
4551 * @adev: amdgpu_device pointer
4553 * Restores the contents of VRAM buffers from the shadows in GTT. Used to
4554 * restore things like GPUVM page tables after a GPU reset where
4555 * the contents of VRAM might be lost.
4558 * 0 on success, negative error code on failure.
4560 static int amdgpu_device_recover_vram(struct amdgpu_device *adev)
4562 struct dma_fence *fence = NULL, *next = NULL;
4563 struct amdgpu_bo *shadow;
4564 struct amdgpu_bo_vm *vmbo;
4567 if (amdgpu_sriov_runtime(adev))
4568 tmo = msecs_to_jiffies(8000);
4570 tmo = msecs_to_jiffies(100);
4572 dev_info(adev->dev, "recover vram bo from shadow start\n");
4573 mutex_lock(&adev->shadow_list_lock);
4574 list_for_each_entry(vmbo, &adev->shadow_list, shadow_list) {
4575 /* If vm is compute context or adev is APU, shadow will be NULL */
4578 shadow = vmbo->shadow;
4580 /* No need to recover an evicted BO */
4581 if (shadow->tbo.resource->mem_type != TTM_PL_TT ||
4582 shadow->tbo.resource->start == AMDGPU_BO_INVALID_OFFSET ||
4583 shadow->parent->tbo.resource->mem_type != TTM_PL_VRAM)
4586 r = amdgpu_bo_restore_shadow(shadow, &next);
4591 tmo = dma_fence_wait_timeout(fence, false, tmo);
4592 dma_fence_put(fence);
4597 } else if (tmo < 0) {
4605 mutex_unlock(&adev->shadow_list_lock);
4608 tmo = dma_fence_wait_timeout(fence, false, tmo);
4609 dma_fence_put(fence);
4611 if (r < 0 || tmo <= 0) {
4612 dev_err(adev->dev, "recover vram bo from shadow failed, r is %ld, tmo is %ld\n", r, tmo);
4616 dev_info(adev->dev, "recover vram bo from shadow done\n");
4622 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
4624 * @adev: amdgpu_device pointer
4625 * @from_hypervisor: request from hypervisor
4627 * do VF FLR and reinitialize Asic
4628 * return 0 means succeeded otherwise failed
4630 static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
4631 bool from_hypervisor)
4634 struct amdgpu_hive_info *hive = NULL;
4635 int retry_limit = 0;
4638 amdgpu_amdkfd_pre_reset(adev);
4640 if (from_hypervisor)
4641 r = amdgpu_virt_request_full_gpu(adev, true);
4643 r = amdgpu_virt_reset_gpu(adev);
4646 amdgpu_irq_gpu_reset_resume_helper(adev);
4648 /* some sw clean up VF needs to do before recover */
4649 amdgpu_virt_post_reset(adev);
4651 /* Resume IP prior to SMC */
4652 r = amdgpu_device_ip_reinit_early_sriov(adev);
4656 amdgpu_virt_init_data_exchange(adev);
4658 r = amdgpu_device_fw_loading(adev);
4662 /* now we are okay to resume SMC/CP/SDMA */
4663 r = amdgpu_device_ip_reinit_late_sriov(adev);
4667 hive = amdgpu_get_xgmi_hive(adev);
4668 /* Update PSP FW topology after reset */
4669 if (hive && adev->gmc.xgmi.num_physical_nodes > 1)
4670 r = amdgpu_xgmi_update_topology(hive, adev);
4673 amdgpu_put_xgmi_hive(hive);
4676 r = amdgpu_ib_ring_tests(adev);
4678 amdgpu_amdkfd_post_reset(adev);
4682 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
4683 amdgpu_inc_vram_lost(adev);
4684 r = amdgpu_device_recover_vram(adev);
4686 amdgpu_virt_release_full_gpu(adev, true);
4688 if (AMDGPU_RETRY_SRIOV_RESET(r)) {
4689 if (retry_limit < AMDGPU_MAX_RETRY_LIMIT) {
4693 DRM_ERROR("GPU reset retry is beyond the retry limit\n");
4700 * amdgpu_device_has_job_running - check if there is any job in mirror list
4702 * @adev: amdgpu_device pointer
4704 * check if there is any job in mirror list
4706 bool amdgpu_device_has_job_running(struct amdgpu_device *adev)
4709 struct drm_sched_job *job;
4711 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4712 struct amdgpu_ring *ring = adev->rings[i];
4714 if (!ring || !ring->sched.thread)
4717 spin_lock(&ring->sched.job_list_lock);
4718 job = list_first_entry_or_null(&ring->sched.pending_list,
4719 struct drm_sched_job, list);
4720 spin_unlock(&ring->sched.job_list_lock);
4728 * amdgpu_device_should_recover_gpu - check if we should try GPU recovery
4730 * @adev: amdgpu_device pointer
4732 * Check amdgpu_gpu_recovery and SRIOV status to see if we should try to recover
4735 bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev)
4738 if (amdgpu_gpu_recovery == 0)
4741 /* Skip soft reset check in fatal error mode */
4742 if (!amdgpu_ras_is_poison_mode_supported(adev))
4745 if (amdgpu_sriov_vf(adev))
4748 if (amdgpu_gpu_recovery == -1) {
4749 switch (adev->asic_type) {
4750 #ifdef CONFIG_DRM_AMDGPU_SI
4757 #ifdef CONFIG_DRM_AMDGPU_CIK
4764 case CHIP_CYAN_SKILLFISH:
4774 dev_info(adev->dev, "GPU recovery disabled.\n");
4778 int amdgpu_device_mode1_reset(struct amdgpu_device *adev)
4783 amdgpu_atombios_scratch_regs_engine_hung(adev, true);
4785 dev_info(adev->dev, "GPU mode1 reset\n");
4788 pci_clear_master(adev->pdev);
4790 amdgpu_device_cache_pci_state(adev->pdev);
4792 if (amdgpu_dpm_is_mode1_reset_supported(adev)) {
4793 dev_info(adev->dev, "GPU smu mode1 reset\n");
4794 ret = amdgpu_dpm_mode1_reset(adev);
4796 dev_info(adev->dev, "GPU psp mode1 reset\n");
4797 ret = psp_gpu_reset(adev);
4801 goto mode1_reset_failed;
4803 amdgpu_device_load_pci_state(adev->pdev);
4804 ret = amdgpu_psp_wait_for_bootloader(adev);
4806 goto mode1_reset_failed;
4808 /* wait for asic to come out of reset */
4809 for (i = 0; i < adev->usec_timeout; i++) {
4810 u32 memsize = adev->nbio.funcs->get_memsize(adev);
4812 if (memsize != 0xffffffff)
4817 if (i >= adev->usec_timeout) {
4819 goto mode1_reset_failed;
4822 amdgpu_atombios_scratch_regs_engine_hung(adev, false);
4827 dev_err(adev->dev, "GPU mode1 reset failed\n");
4831 int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
4832 struct amdgpu_reset_context *reset_context)
4835 struct amdgpu_job *job = NULL;
4836 bool need_full_reset =
4837 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4839 if (reset_context->reset_req_dev == adev)
4840 job = reset_context->job;
4842 if (amdgpu_sriov_vf(adev)) {
4843 /* stop the data exchange thread */
4844 amdgpu_virt_fini_data_exchange(adev);
4847 amdgpu_fence_driver_isr_toggle(adev, true);
4849 /* block all schedulers and reset given job's ring */
4850 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4851 struct amdgpu_ring *ring = adev->rings[i];
4853 if (!ring || !ring->sched.thread)
4856 /* Clear job fence from fence drv to avoid force_completion
4857 * leave NULL and vm flush fence in fence drv
4859 amdgpu_fence_driver_clear_job_fences(ring);
4861 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
4862 amdgpu_fence_driver_force_completion(ring);
4865 amdgpu_fence_driver_isr_toggle(adev, false);
4868 drm_sched_increase_karma(&job->base);
4870 r = amdgpu_reset_prepare_hwcontext(adev, reset_context);
4871 /* If reset handler not implemented, continue; otherwise return */
4872 if (r == -EOPNOTSUPP)
4877 /* Don't suspend on bare metal if we are not going to HW reset the ASIC */
4878 if (!amdgpu_sriov_vf(adev)) {
4880 if (!need_full_reset)
4881 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
4883 if (!need_full_reset && amdgpu_gpu_recovery &&
4884 amdgpu_device_ip_check_soft_reset(adev)) {
4885 amdgpu_device_ip_pre_soft_reset(adev);
4886 r = amdgpu_device_ip_soft_reset(adev);
4887 amdgpu_device_ip_post_soft_reset(adev);
4888 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
4889 dev_info(adev->dev, "soft reset failed, will fallback to full reset!\n");
4890 need_full_reset = true;
4894 if (need_full_reset)
4895 r = amdgpu_device_ip_suspend(adev);
4896 if (need_full_reset)
4897 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4899 clear_bit(AMDGPU_NEED_FULL_RESET,
4900 &reset_context->flags);
4906 static int amdgpu_reset_reg_dumps(struct amdgpu_device *adev)
4910 lockdep_assert_held(&adev->reset_domain->sem);
4912 for (i = 0; i < adev->num_regs; i++) {
4913 adev->reset_dump_reg_value[i] = RREG32(adev->reset_dump_reg_list[i]);
4914 trace_amdgpu_reset_reg_dumps(adev->reset_dump_reg_list[i],
4915 adev->reset_dump_reg_value[i]);
4921 #ifdef CONFIG_DEV_COREDUMP
4922 static ssize_t amdgpu_devcoredump_read(char *buffer, loff_t offset,
4923 size_t count, void *data, size_t datalen)
4925 struct drm_printer p;
4926 struct amdgpu_device *adev = data;
4927 struct drm_print_iterator iter;
4932 iter.start = offset;
4933 iter.remain = count;
4935 p = drm_coredump_printer(&iter);
4937 drm_printf(&p, "**** AMDGPU Device Coredump ****\n");
4938 drm_printf(&p, "kernel: " UTS_RELEASE "\n");
4939 drm_printf(&p, "module: " KBUILD_MODNAME "\n");
4940 drm_printf(&p, "time: %lld.%09ld\n", adev->reset_time.tv_sec, adev->reset_time.tv_nsec);
4941 if (adev->reset_task_info.pid)
4942 drm_printf(&p, "process_name: %s PID: %d\n",
4943 adev->reset_task_info.process_name,
4944 adev->reset_task_info.pid);
4946 if (adev->reset_vram_lost)
4947 drm_printf(&p, "VRAM is lost due to GPU reset!\n");
4948 if (adev->num_regs) {
4949 drm_printf(&p, "AMDGPU register dumps:\nOffset: Value:\n");
4951 for (i = 0; i < adev->num_regs; i++)
4952 drm_printf(&p, "0x%08x: 0x%08x\n",
4953 adev->reset_dump_reg_list[i],
4954 adev->reset_dump_reg_value[i]);
4957 return count - iter.remain;
4960 static void amdgpu_devcoredump_free(void *data)
4964 static void amdgpu_reset_capture_coredumpm(struct amdgpu_device *adev)
4966 struct drm_device *dev = adev_to_drm(adev);
4968 ktime_get_ts64(&adev->reset_time);
4969 dev_coredumpm(dev->dev, THIS_MODULE, adev, 0, GFP_NOWAIT,
4970 amdgpu_devcoredump_read, amdgpu_devcoredump_free);
4974 int amdgpu_do_asic_reset(struct list_head *device_list_handle,
4975 struct amdgpu_reset_context *reset_context)
4977 struct amdgpu_device *tmp_adev = NULL;
4978 bool need_full_reset, skip_hw_reset, vram_lost = false;
4980 bool gpu_reset_for_dev_remove = 0;
4982 /* Try reset handler method first */
4983 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
4985 amdgpu_reset_reg_dumps(tmp_adev);
4987 reset_context->reset_device_list = device_list_handle;
4988 r = amdgpu_reset_perform_reset(tmp_adev, reset_context);
4989 /* If reset handler not implemented, continue; otherwise return */
4990 if (r == -EOPNOTSUPP)
4995 /* Reset handler not implemented, use the default method */
4997 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4998 skip_hw_reset = test_bit(AMDGPU_SKIP_HW_RESET, &reset_context->flags);
5000 gpu_reset_for_dev_remove =
5001 test_bit(AMDGPU_RESET_FOR_DEVICE_REMOVE, &reset_context->flags) &&
5002 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
5005 * ASIC reset has to be done on all XGMI hive nodes ASAP
5006 * to allow proper links negotiation in FW (within 1 sec)
5008 if (!skip_hw_reset && need_full_reset) {
5009 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5010 /* For XGMI run all resets in parallel to speed up the process */
5011 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
5012 tmp_adev->gmc.xgmi.pending_reset = false;
5013 if (!queue_work(system_unbound_wq, &tmp_adev->xgmi_reset_work))
5016 r = amdgpu_asic_reset(tmp_adev);
5019 dev_err(tmp_adev->dev, "ASIC reset failed with error, %d for drm dev, %s",
5020 r, adev_to_drm(tmp_adev)->unique);
5025 /* For XGMI wait for all resets to complete before proceed */
5027 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5028 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
5029 flush_work(&tmp_adev->xgmi_reset_work);
5030 r = tmp_adev->asic_reset_res;
5038 if (!r && amdgpu_ras_intr_triggered()) {
5039 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5040 if (tmp_adev->mmhub.ras && tmp_adev->mmhub.ras->ras_block.hw_ops &&
5041 tmp_adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count)
5042 tmp_adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count(tmp_adev);
5045 amdgpu_ras_intr_cleared();
5048 /* Since the mode1 reset affects base ip blocks, the
5049 * phase1 ip blocks need to be resumed. Otherwise there
5050 * will be a BIOS signature error and the psp bootloader
5051 * can't load kdb on the next amdgpu install.
5053 if (gpu_reset_for_dev_remove) {
5054 list_for_each_entry(tmp_adev, device_list_handle, reset_list)
5055 amdgpu_device_ip_resume_phase1(tmp_adev);
5060 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5061 if (need_full_reset) {
5063 r = amdgpu_device_asic_init(tmp_adev);
5065 dev_warn(tmp_adev->dev, "asic atom init failed!");
5067 dev_info(tmp_adev->dev, "GPU reset succeeded, trying to resume\n");
5069 r = amdgpu_device_ip_resume_phase1(tmp_adev);
5073 vram_lost = amdgpu_device_check_vram_lost(tmp_adev);
5074 #ifdef CONFIG_DEV_COREDUMP
5075 tmp_adev->reset_vram_lost = vram_lost;
5076 memset(&tmp_adev->reset_task_info, 0,
5077 sizeof(tmp_adev->reset_task_info));
5078 if (reset_context->job && reset_context->job->vm)
5079 tmp_adev->reset_task_info =
5080 reset_context->job->vm->task_info;
5081 amdgpu_reset_capture_coredumpm(tmp_adev);
5084 DRM_INFO("VRAM is lost due to GPU reset!\n");
5085 amdgpu_inc_vram_lost(tmp_adev);
5088 r = amdgpu_device_fw_loading(tmp_adev);
5092 r = amdgpu_device_ip_resume_phase2(tmp_adev);
5097 amdgpu_device_fill_reset_magic(tmp_adev);
5100 * Add this ASIC as tracked as reset was already
5101 * complete successfully.
5103 amdgpu_register_gpu_instance(tmp_adev);
5105 if (!reset_context->hive &&
5106 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
5107 amdgpu_xgmi_add_device(tmp_adev);
5109 r = amdgpu_device_ip_late_init(tmp_adev);
5113 drm_fb_helper_set_suspend_unlocked(adev_to_drm(tmp_adev)->fb_helper, false);
5116 * The GPU enters bad state once faulty pages
5117 * by ECC has reached the threshold, and ras
5118 * recovery is scheduled next. So add one check
5119 * here to break recovery if it indeed exceeds
5120 * bad page threshold, and remind user to
5121 * retire this GPU or setting one bigger
5122 * bad_page_threshold value to fix this once
5123 * probing driver again.
5125 if (!amdgpu_ras_eeprom_check_err_threshold(tmp_adev)) {
5127 amdgpu_ras_resume(tmp_adev);
5133 /* Update PSP FW topology after reset */
5134 if (reset_context->hive &&
5135 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
5136 r = amdgpu_xgmi_update_topology(
5137 reset_context->hive, tmp_adev);
5143 amdgpu_irq_gpu_reset_resume_helper(tmp_adev);
5144 r = amdgpu_ib_ring_tests(tmp_adev);
5146 dev_err(tmp_adev->dev, "ib ring test failed (%d).\n", r);
5147 need_full_reset = true;
5154 r = amdgpu_device_recover_vram(tmp_adev);
5156 tmp_adev->asic_reset_res = r;
5160 if (need_full_reset)
5161 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
5163 clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
5167 static void amdgpu_device_set_mp1_state(struct amdgpu_device *adev)
5170 switch (amdgpu_asic_reset_method(adev)) {
5171 case AMD_RESET_METHOD_MODE1:
5172 adev->mp1_state = PP_MP1_STATE_SHUTDOWN;
5174 case AMD_RESET_METHOD_MODE2:
5175 adev->mp1_state = PP_MP1_STATE_RESET;
5178 adev->mp1_state = PP_MP1_STATE_NONE;
5183 static void amdgpu_device_unset_mp1_state(struct amdgpu_device *adev)
5185 amdgpu_vf_error_trans_all(adev);
5186 adev->mp1_state = PP_MP1_STATE_NONE;
5189 static void amdgpu_device_resume_display_audio(struct amdgpu_device *adev)
5191 struct pci_dev *p = NULL;
5193 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
5194 adev->pdev->bus->number, 1);
5196 pm_runtime_enable(&(p->dev));
5197 pm_runtime_resume(&(p->dev));
5203 static int amdgpu_device_suspend_display_audio(struct amdgpu_device *adev)
5205 enum amd_reset_method reset_method;
5206 struct pci_dev *p = NULL;
5210 * For now, only BACO and mode1 reset are confirmed
5211 * to suffer the audio issue without proper suspended.
5213 reset_method = amdgpu_asic_reset_method(adev);
5214 if ((reset_method != AMD_RESET_METHOD_BACO) &&
5215 (reset_method != AMD_RESET_METHOD_MODE1))
5218 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
5219 adev->pdev->bus->number, 1);
5223 expires = pm_runtime_autosuspend_expiration(&(p->dev));
5226 * If we cannot get the audio device autosuspend delay,
5227 * a fixed 4S interval will be used. Considering 3S is
5228 * the audio controller default autosuspend delay setting.
5229 * 4S used here is guaranteed to cover that.
5231 expires = ktime_get_mono_fast_ns() + NSEC_PER_SEC * 4ULL;
5233 while (!pm_runtime_status_suspended(&(p->dev))) {
5234 if (!pm_runtime_suspend(&(p->dev)))
5237 if (expires < ktime_get_mono_fast_ns()) {
5238 dev_warn(adev->dev, "failed to suspend display audio\n");
5240 /* TODO: abort the succeeding gpu reset? */
5245 pm_runtime_disable(&(p->dev));
5251 static inline void amdgpu_device_stop_pending_resets(struct amdgpu_device *adev)
5253 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
5255 #if defined(CONFIG_DEBUG_FS)
5256 if (!amdgpu_sriov_vf(adev))
5257 cancel_work(&adev->reset_work);
5261 cancel_work(&adev->kfd.reset_work);
5263 if (amdgpu_sriov_vf(adev))
5264 cancel_work(&adev->virt.flr_work);
5266 if (con && adev->ras_enabled)
5267 cancel_work(&con->recovery_work);
5272 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
5274 * @adev: amdgpu_device pointer
5275 * @job: which job trigger hang
5276 * @reset_context: amdgpu reset context pointer
5278 * Attempt to reset the GPU if it has hung (all asics).
5279 * Attempt to do soft-reset or full-reset and reinitialize Asic
5280 * Returns 0 for success or an error on failure.
5283 int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
5284 struct amdgpu_job *job,
5285 struct amdgpu_reset_context *reset_context)
5287 struct list_head device_list, *device_list_handle = NULL;
5288 bool job_signaled = false;
5289 struct amdgpu_hive_info *hive = NULL;
5290 struct amdgpu_device *tmp_adev = NULL;
5292 bool need_emergency_restart = false;
5293 bool audio_suspended = false;
5294 bool gpu_reset_for_dev_remove = false;
5296 gpu_reset_for_dev_remove =
5297 test_bit(AMDGPU_RESET_FOR_DEVICE_REMOVE, &reset_context->flags) &&
5298 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
5301 * Special case: RAS triggered and full reset isn't supported
5303 need_emergency_restart = amdgpu_ras_need_emergency_restart(adev);
5306 * Flush RAM to disk so that after reboot
5307 * the user can read log and see why the system rebooted.
5309 if (need_emergency_restart && amdgpu_ras_get_context(adev)->reboot) {
5310 DRM_WARN("Emergency reboot.");
5313 emergency_restart();
5316 dev_info(adev->dev, "GPU %s begin!\n",
5317 need_emergency_restart ? "jobs stop":"reset");
5319 if (!amdgpu_sriov_vf(adev))
5320 hive = amdgpu_get_xgmi_hive(adev);
5322 mutex_lock(&hive->hive_lock);
5324 reset_context->job = job;
5325 reset_context->hive = hive;
5327 * Build list of devices to reset.
5328 * In case we are in XGMI hive mode, resort the device list
5329 * to put adev in the 1st position.
5331 INIT_LIST_HEAD(&device_list);
5332 if (!amdgpu_sriov_vf(adev) && (adev->gmc.xgmi.num_physical_nodes > 1)) {
5333 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
5334 list_add_tail(&tmp_adev->reset_list, &device_list);
5335 if (gpu_reset_for_dev_remove && adev->shutdown)
5336 tmp_adev->shutdown = true;
5338 if (!list_is_first(&adev->reset_list, &device_list))
5339 list_rotate_to_front(&adev->reset_list, &device_list);
5340 device_list_handle = &device_list;
5342 list_add_tail(&adev->reset_list, &device_list);
5343 device_list_handle = &device_list;
5346 /* We need to lock reset domain only once both for XGMI and single device */
5347 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
5349 amdgpu_device_lock_reset_domain(tmp_adev->reset_domain);
5351 /* block all schedulers and reset given job's ring */
5352 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5354 amdgpu_device_set_mp1_state(tmp_adev);
5357 * Try to put the audio codec into suspend state
5358 * before gpu reset started.
5360 * Due to the power domain of the graphics device
5361 * is shared with AZ power domain. Without this,
5362 * we may change the audio hardware from behind
5363 * the audio driver's back. That will trigger
5364 * some audio codec errors.
5366 if (!amdgpu_device_suspend_display_audio(tmp_adev))
5367 audio_suspended = true;
5369 amdgpu_ras_set_error_query_ready(tmp_adev, false);
5371 cancel_delayed_work_sync(&tmp_adev->delayed_init_work);
5373 if (!amdgpu_sriov_vf(tmp_adev))
5374 amdgpu_amdkfd_pre_reset(tmp_adev);
5377 * Mark these ASICs to be reseted as untracked first
5378 * And add them back after reset completed
5380 amdgpu_unregister_gpu_instance(tmp_adev);
5382 drm_fb_helper_set_suspend_unlocked(adev_to_drm(tmp_adev)->fb_helper, true);
5384 /* disable ras on ALL IPs */
5385 if (!need_emergency_restart &&
5386 amdgpu_device_ip_need_full_reset(tmp_adev))
5387 amdgpu_ras_suspend(tmp_adev);
5389 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5390 struct amdgpu_ring *ring = tmp_adev->rings[i];
5392 if (!ring || !ring->sched.thread)
5395 drm_sched_stop(&ring->sched, job ? &job->base : NULL);
5397 if (need_emergency_restart)
5398 amdgpu_job_stop_all_jobs_on_sched(&ring->sched);
5400 atomic_inc(&tmp_adev->gpu_reset_counter);
5403 if (need_emergency_restart)
5404 goto skip_sched_resume;
5407 * Must check guilty signal here since after this point all old
5408 * HW fences are force signaled.
5410 * job->base holds a reference to parent fence
5412 if (job && dma_fence_is_signaled(&job->hw_fence)) {
5413 job_signaled = true;
5414 dev_info(adev->dev, "Guilty job already signaled, skipping HW reset");
5418 retry: /* Rest of adevs pre asic reset from XGMI hive. */
5419 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5420 if (gpu_reset_for_dev_remove) {
5421 /* Workaroud for ASICs need to disable SMC first */
5422 amdgpu_device_smu_fini_early(tmp_adev);
5424 r = amdgpu_device_pre_asic_reset(tmp_adev, reset_context);
5425 /*TODO Should we stop ?*/
5427 dev_err(tmp_adev->dev, "GPU pre asic reset failed with err, %d for drm dev, %s ",
5428 r, adev_to_drm(tmp_adev)->unique);
5429 tmp_adev->asic_reset_res = r;
5433 * Drop all pending non scheduler resets. Scheduler resets
5434 * were already dropped during drm_sched_stop
5436 amdgpu_device_stop_pending_resets(tmp_adev);
5439 /* Actual ASIC resets if needed.*/
5440 /* Host driver will handle XGMI hive reset for SRIOV */
5441 if (amdgpu_sriov_vf(adev)) {
5442 r = amdgpu_device_reset_sriov(adev, job ? false : true);
5444 adev->asic_reset_res = r;
5446 /* Aldebaran and gfx_11_0_3 support ras in SRIOV, so need resume ras during reset */
5447 if (adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 2) ||
5448 adev->ip_versions[GC_HWIP][0] == IP_VERSION(11, 0, 3))
5449 amdgpu_ras_resume(adev);
5451 r = amdgpu_do_asic_reset(device_list_handle, reset_context);
5452 if (r && r == -EAGAIN)
5455 if (!r && gpu_reset_for_dev_remove)
5461 /* Post ASIC reset for all devs .*/
5462 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5464 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5465 struct amdgpu_ring *ring = tmp_adev->rings[i];
5467 if (!ring || !ring->sched.thread)
5470 drm_sched_start(&ring->sched, true);
5473 if (adev->enable_mes && adev->ip_versions[GC_HWIP][0] != IP_VERSION(11, 0, 3))
5474 amdgpu_mes_self_test(tmp_adev);
5476 if (!drm_drv_uses_atomic_modeset(adev_to_drm(tmp_adev)) && !job_signaled)
5477 drm_helper_resume_force_mode(adev_to_drm(tmp_adev));
5479 if (tmp_adev->asic_reset_res)
5480 r = tmp_adev->asic_reset_res;
5482 tmp_adev->asic_reset_res = 0;
5485 /* bad news, how to tell it to userspace ? */
5486 dev_info(tmp_adev->dev, "GPU reset(%d) failed\n", atomic_read(&tmp_adev->gpu_reset_counter));
5487 amdgpu_vf_error_put(tmp_adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
5489 dev_info(tmp_adev->dev, "GPU reset(%d) succeeded!\n", atomic_read(&tmp_adev->gpu_reset_counter));
5490 if (amdgpu_acpi_smart_shift_update(adev_to_drm(tmp_adev), AMDGPU_SS_DEV_D0))
5491 DRM_WARN("smart shift update failed\n");
5496 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5497 /* unlock kfd: SRIOV would do it separately */
5498 if (!need_emergency_restart && !amdgpu_sriov_vf(tmp_adev))
5499 amdgpu_amdkfd_post_reset(tmp_adev);
5501 /* kfd_post_reset will do nothing if kfd device is not initialized,
5502 * need to bring up kfd here if it's not be initialized before
5504 if (!adev->kfd.init_complete)
5505 amdgpu_amdkfd_device_init(adev);
5507 if (audio_suspended)
5508 amdgpu_device_resume_display_audio(tmp_adev);
5510 amdgpu_device_unset_mp1_state(tmp_adev);
5512 amdgpu_ras_set_error_query_ready(tmp_adev, true);
5516 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
5518 amdgpu_device_unlock_reset_domain(tmp_adev->reset_domain);
5521 mutex_unlock(&hive->hive_lock);
5522 amdgpu_put_xgmi_hive(hive);
5526 dev_info(adev->dev, "GPU reset end with ret = %d\n", r);
5528 atomic_set(&adev->reset_domain->reset_res, r);
5533 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
5535 * @adev: amdgpu_device pointer
5537 * Fetchs and stores in the driver the PCIE capabilities (gen speed
5538 * and lanes) of the slot the device is in. Handles APUs and
5539 * virtualized environments where PCIE config space may not be available.
5541 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
5543 struct pci_dev *pdev;
5544 enum pci_bus_speed speed_cap, platform_speed_cap;
5545 enum pcie_link_width platform_link_width;
5547 if (amdgpu_pcie_gen_cap)
5548 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
5550 if (amdgpu_pcie_lane_cap)
5551 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
5553 /* covers APUs as well */
5554 if (pci_is_root_bus(adev->pdev->bus) && !amdgpu_passthrough(adev)) {
5555 if (adev->pm.pcie_gen_mask == 0)
5556 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
5557 if (adev->pm.pcie_mlw_mask == 0)
5558 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
5562 if (adev->pm.pcie_gen_mask && adev->pm.pcie_mlw_mask)
5565 pcie_bandwidth_available(adev->pdev, NULL,
5566 &platform_speed_cap, &platform_link_width);
5568 if (adev->pm.pcie_gen_mask == 0) {
5571 speed_cap = pcie_get_speed_cap(pdev);
5572 if (speed_cap == PCI_SPEED_UNKNOWN) {
5573 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5574 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5575 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
5577 if (speed_cap == PCIE_SPEED_32_0GT)
5578 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5579 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5580 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5581 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4 |
5582 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN5);
5583 else if (speed_cap == PCIE_SPEED_16_0GT)
5584 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5585 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5586 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5587 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4);
5588 else if (speed_cap == PCIE_SPEED_8_0GT)
5589 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5590 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5591 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
5592 else if (speed_cap == PCIE_SPEED_5_0GT)
5593 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5594 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2);
5596 adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1;
5599 if (platform_speed_cap == PCI_SPEED_UNKNOWN) {
5600 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5601 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
5603 if (platform_speed_cap == PCIE_SPEED_32_0GT)
5604 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5605 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5606 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5607 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4 |
5608 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN5);
5609 else if (platform_speed_cap == PCIE_SPEED_16_0GT)
5610 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5611 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5612 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5613 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4);
5614 else if (platform_speed_cap == PCIE_SPEED_8_0GT)
5615 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5616 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5617 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3);
5618 else if (platform_speed_cap == PCIE_SPEED_5_0GT)
5619 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5620 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
5622 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
5626 if (adev->pm.pcie_mlw_mask == 0) {
5627 if (platform_link_width == PCIE_LNK_WIDTH_UNKNOWN) {
5628 adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK;
5630 switch (platform_link_width) {
5632 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
5633 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5634 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5635 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5636 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5637 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5638 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5641 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5642 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5643 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5644 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5645 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5646 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5649 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5650 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5651 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5652 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5653 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5656 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5657 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5658 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5659 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5662 adev->pm.pcie_mlw_mask = (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_X2 |
5668 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5671 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
5681 * amdgpu_device_is_peer_accessible - Check peer access through PCIe BAR
5683 * @adev: amdgpu_device pointer
5684 * @peer_adev: amdgpu_device pointer for peer device trying to access @adev
5686 * Return true if @peer_adev can access (DMA) @adev through the PCIe
5687 * BAR, i.e. @adev is "large BAR" and the BAR matches the DMA mask of
5690 bool amdgpu_device_is_peer_accessible(struct amdgpu_device *adev,
5691 struct amdgpu_device *peer_adev)
5693 #ifdef CONFIG_HSA_AMD_P2P
5694 uint64_t address_mask = peer_adev->dev->dma_mask ?
5695 ~*peer_adev->dev->dma_mask : ~((1ULL << 32) - 1);
5696 resource_size_t aper_limit =
5697 adev->gmc.aper_base + adev->gmc.aper_size - 1;
5699 !adev->gmc.xgmi.connected_to_cpu &&
5700 !(pci_p2pdma_distance(adev->pdev, peer_adev->dev, false) < 0);
5702 return pcie_p2p && p2p_access && (adev->gmc.visible_vram_size &&
5703 adev->gmc.real_vram_size == adev->gmc.visible_vram_size &&
5704 !(adev->gmc.aper_base & address_mask ||
5705 aper_limit & address_mask));
5711 int amdgpu_device_baco_enter(struct drm_device *dev)
5713 struct amdgpu_device *adev = drm_to_adev(dev);
5714 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
5716 if (!amdgpu_device_supports_baco(dev))
5719 if (ras && adev->ras_enabled &&
5720 adev->nbio.funcs->enable_doorbell_interrupt)
5721 adev->nbio.funcs->enable_doorbell_interrupt(adev, false);
5723 return amdgpu_dpm_baco_enter(adev);
5726 int amdgpu_device_baco_exit(struct drm_device *dev)
5728 struct amdgpu_device *adev = drm_to_adev(dev);
5729 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
5732 if (!amdgpu_device_supports_baco(dev))
5735 ret = amdgpu_dpm_baco_exit(adev);
5739 if (ras && adev->ras_enabled &&
5740 adev->nbio.funcs->enable_doorbell_interrupt)
5741 adev->nbio.funcs->enable_doorbell_interrupt(adev, true);
5743 if (amdgpu_passthrough(adev) &&
5744 adev->nbio.funcs->clear_doorbell_interrupt)
5745 adev->nbio.funcs->clear_doorbell_interrupt(adev);
5751 * amdgpu_pci_error_detected - Called when a PCI error is detected.
5752 * @pdev: PCI device struct
5753 * @state: PCI channel state
5755 * Description: Called when a PCI error is detected.
5757 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT.
5759 pci_ers_result_t amdgpu_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
5761 struct drm_device *dev = pci_get_drvdata(pdev);
5762 struct amdgpu_device *adev = drm_to_adev(dev);
5765 DRM_INFO("PCI error: detected callback, state(%d)!!\n", state);
5767 if (adev->gmc.xgmi.num_physical_nodes > 1) {
5768 DRM_WARN("No support for XGMI hive yet...");
5769 return PCI_ERS_RESULT_DISCONNECT;
5772 adev->pci_channel_state = state;
5775 case pci_channel_io_normal:
5776 return PCI_ERS_RESULT_CAN_RECOVER;
5777 /* Fatal error, prepare for slot reset */
5778 case pci_channel_io_frozen:
5780 * Locking adev->reset_domain->sem will prevent any external access
5781 * to GPU during PCI error recovery
5783 amdgpu_device_lock_reset_domain(adev->reset_domain);
5784 amdgpu_device_set_mp1_state(adev);
5787 * Block any work scheduling as we do for regular GPU reset
5788 * for the duration of the recovery
5790 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5791 struct amdgpu_ring *ring = adev->rings[i];
5793 if (!ring || !ring->sched.thread)
5796 drm_sched_stop(&ring->sched, NULL);
5798 atomic_inc(&adev->gpu_reset_counter);
5799 return PCI_ERS_RESULT_NEED_RESET;
5800 case pci_channel_io_perm_failure:
5801 /* Permanent error, prepare for device removal */
5802 return PCI_ERS_RESULT_DISCONNECT;
5805 return PCI_ERS_RESULT_NEED_RESET;
5809 * amdgpu_pci_mmio_enabled - Enable MMIO and dump debug registers
5810 * @pdev: pointer to PCI device
5812 pci_ers_result_t amdgpu_pci_mmio_enabled(struct pci_dev *pdev)
5815 DRM_INFO("PCI error: mmio enabled callback!!\n");
5817 /* TODO - dump whatever for debugging purposes */
5819 /* This called only if amdgpu_pci_error_detected returns
5820 * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still
5821 * works, no need to reset slot.
5824 return PCI_ERS_RESULT_RECOVERED;
5828 * amdgpu_pci_slot_reset - Called when PCI slot has been reset.
5829 * @pdev: PCI device struct
5831 * Description: This routine is called by the pci error recovery
5832 * code after the PCI slot has been reset, just before we
5833 * should resume normal operations.
5835 pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev)
5837 struct drm_device *dev = pci_get_drvdata(pdev);
5838 struct amdgpu_device *adev = drm_to_adev(dev);
5840 struct amdgpu_reset_context reset_context;
5842 struct list_head device_list;
5844 DRM_INFO("PCI error: slot reset callback!!\n");
5846 memset(&reset_context, 0, sizeof(reset_context));
5848 INIT_LIST_HEAD(&device_list);
5849 list_add_tail(&adev->reset_list, &device_list);
5851 /* wait for asic to come out of reset */
5854 /* Restore PCI confspace */
5855 amdgpu_device_load_pci_state(pdev);
5857 /* confirm ASIC came out of reset */
5858 for (i = 0; i < adev->usec_timeout; i++) {
5859 memsize = amdgpu_asic_get_config_memsize(adev);
5861 if (memsize != 0xffffffff)
5865 if (memsize == 0xffffffff) {
5870 reset_context.method = AMD_RESET_METHOD_NONE;
5871 reset_context.reset_req_dev = adev;
5872 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
5873 set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
5875 adev->no_hw_access = true;
5876 r = amdgpu_device_pre_asic_reset(adev, &reset_context);
5877 adev->no_hw_access = false;
5881 r = amdgpu_do_asic_reset(&device_list, &reset_context);
5885 if (amdgpu_device_cache_pci_state(adev->pdev))
5886 pci_restore_state(adev->pdev);
5888 DRM_INFO("PCIe error recovery succeeded\n");
5890 DRM_ERROR("PCIe error recovery failed, err:%d", r);
5891 amdgpu_device_unset_mp1_state(adev);
5892 amdgpu_device_unlock_reset_domain(adev->reset_domain);
5895 return r ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
5899 * amdgpu_pci_resume() - resume normal ops after PCI reset
5900 * @pdev: pointer to PCI device
5902 * Called when the error recovery driver tells us that its
5903 * OK to resume normal operation.
5905 void amdgpu_pci_resume(struct pci_dev *pdev)
5907 struct drm_device *dev = pci_get_drvdata(pdev);
5908 struct amdgpu_device *adev = drm_to_adev(dev);
5912 DRM_INFO("PCI error: resume callback!!\n");
5914 /* Only continue execution for the case of pci_channel_io_frozen */
5915 if (adev->pci_channel_state != pci_channel_io_frozen)
5918 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5919 struct amdgpu_ring *ring = adev->rings[i];
5921 if (!ring || !ring->sched.thread)
5924 drm_sched_start(&ring->sched, true);
5927 amdgpu_device_unset_mp1_state(adev);
5928 amdgpu_device_unlock_reset_domain(adev->reset_domain);
5931 bool amdgpu_device_cache_pci_state(struct pci_dev *pdev)
5933 struct drm_device *dev = pci_get_drvdata(pdev);
5934 struct amdgpu_device *adev = drm_to_adev(dev);
5937 r = pci_save_state(pdev);
5939 kfree(adev->pci_state);
5941 adev->pci_state = pci_store_saved_state(pdev);
5943 if (!adev->pci_state) {
5944 DRM_ERROR("Failed to store PCI saved state");
5948 DRM_WARN("Failed to save PCI state, err:%d\n", r);
5955 bool amdgpu_device_load_pci_state(struct pci_dev *pdev)
5957 struct drm_device *dev = pci_get_drvdata(pdev);
5958 struct amdgpu_device *adev = drm_to_adev(dev);
5961 if (!adev->pci_state)
5964 r = pci_load_saved_state(pdev, adev->pci_state);
5967 pci_restore_state(pdev);
5969 DRM_WARN("Failed to load PCI state, err:%d\n", r);
5976 void amdgpu_device_flush_hdp(struct amdgpu_device *adev,
5977 struct amdgpu_ring *ring)
5979 #ifdef CONFIG_X86_64
5980 if ((adev->flags & AMD_IS_APU) && !amdgpu_passthrough(adev))
5983 if (adev->gmc.xgmi.connected_to_cpu)
5986 if (ring && ring->funcs->emit_hdp_flush)
5987 amdgpu_ring_emit_hdp_flush(ring);
5989 amdgpu_asic_flush_hdp(adev, ring);
5992 void amdgpu_device_invalidate_hdp(struct amdgpu_device *adev,
5993 struct amdgpu_ring *ring)
5995 #ifdef CONFIG_X86_64
5996 if ((adev->flags & AMD_IS_APU) && !amdgpu_passthrough(adev))
5999 if (adev->gmc.xgmi.connected_to_cpu)
6002 amdgpu_asic_invalidate_hdp(adev, ring);
6005 int amdgpu_in_reset(struct amdgpu_device *adev)
6007 return atomic_read(&adev->reset_domain->in_gpu_reset);
6011 * amdgpu_device_halt() - bring hardware to some kind of halt state
6013 * @adev: amdgpu_device pointer
6015 * Bring hardware to some kind of halt state so that no one can touch it
6016 * any more. It will help to maintain error context when error occurred.
6017 * Compare to a simple hang, the system will keep stable at least for SSH
6018 * access. Then it should be trivial to inspect the hardware state and
6019 * see what's going on. Implemented as following:
6021 * 1. drm_dev_unplug() makes device inaccessible to user space(IOCTLs, etc),
6022 * clears all CPU mappings to device, disallows remappings through page faults
6023 * 2. amdgpu_irq_disable_all() disables all interrupts
6024 * 3. amdgpu_fence_driver_hw_fini() signals all HW fences
6025 * 4. set adev->no_hw_access to avoid potential crashes after setp 5
6026 * 5. amdgpu_device_unmap_mmio() clears all MMIO mappings
6027 * 6. pci_disable_device() and pci_wait_for_pending_transaction()
6028 * flush any in flight DMA operations
6030 void amdgpu_device_halt(struct amdgpu_device *adev)
6032 struct pci_dev *pdev = adev->pdev;
6033 struct drm_device *ddev = adev_to_drm(adev);
6035 amdgpu_xcp_dev_unplug(adev);
6036 drm_dev_unplug(ddev);
6038 amdgpu_irq_disable_all(adev);
6040 amdgpu_fence_driver_hw_fini(adev);
6042 adev->no_hw_access = true;
6044 amdgpu_device_unmap_mmio(adev);
6046 pci_disable_device(pdev);
6047 pci_wait_for_pending_transaction(pdev);
6050 u32 amdgpu_device_pcie_port_rreg(struct amdgpu_device *adev,
6053 unsigned long flags, address, data;
6056 address = adev->nbio.funcs->get_pcie_port_index_offset(adev);
6057 data = adev->nbio.funcs->get_pcie_port_data_offset(adev);
6059 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
6060 WREG32(address, reg * 4);
6061 (void)RREG32(address);
6063 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
6067 void amdgpu_device_pcie_port_wreg(struct amdgpu_device *adev,
6070 unsigned long flags, address, data;
6072 address = adev->nbio.funcs->get_pcie_port_index_offset(adev);
6073 data = adev->nbio.funcs->get_pcie_port_data_offset(adev);
6075 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
6076 WREG32(address, reg * 4);
6077 (void)RREG32(address);
6080 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
6084 * amdgpu_device_switch_gang - switch to a new gang
6085 * @adev: amdgpu_device pointer
6086 * @gang: the gang to switch to
6088 * Try to switch to a new gang.
6089 * Returns: NULL if we switched to the new gang or a reference to the current
6092 struct dma_fence *amdgpu_device_switch_gang(struct amdgpu_device *adev,
6093 struct dma_fence *gang)
6095 struct dma_fence *old = NULL;
6100 old = dma_fence_get_rcu_safe(&adev->gang_submit);
6106 if (!dma_fence_is_signaled(old))
6109 } while (cmpxchg((struct dma_fence __force **)&adev->gang_submit,
6116 bool amdgpu_device_has_display_hardware(struct amdgpu_device *adev)
6118 switch (adev->asic_type) {
6119 #ifdef CONFIG_DRM_AMDGPU_SI
6123 /* chips with no display hardware */
6125 #ifdef CONFIG_DRM_AMDGPU_SI
6131 #ifdef CONFIG_DRM_AMDGPU_CIK
6140 case CHIP_POLARIS10:
6141 case CHIP_POLARIS11:
6142 case CHIP_POLARIS12:
6146 /* chips with display hardware */
6150 if (!adev->ip_versions[DCE_HWIP][0] ||
6151 (adev->harvest_ip_mask & AMD_HARVEST_IP_DMU_MASK))
6157 uint32_t amdgpu_device_wait_on_rreg(struct amdgpu_device *adev,
6158 uint32_t inst, uint32_t reg_addr, char reg_name[],
6159 uint32_t expected_value, uint32_t mask)
6163 uint32_t tmp_ = RREG32(reg_addr);
6164 uint32_t loop = adev->usec_timeout;
6166 while ((tmp_ & (mask)) != (expected_value)) {
6168 loop = adev->usec_timeout;
6172 tmp_ = RREG32(reg_addr);
6175 DRM_WARN("Register(%d) [%s] failed to reach value 0x%08x != 0x%08xn",
6176 inst, reg_name, (uint32_t)expected_value,
6177 (uint32_t)(tmp_ & (mask)));