2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
24 * Authors: Dave Airlie
28 #include <linux/power_supply.h>
29 #include <linux/kthread.h>
30 #include <linux/module.h>
31 #include <linux/console.h>
32 #include <linux/slab.h>
34 #include <drm/drm_atomic_helper.h>
35 #include <drm/drm_probe_helper.h>
36 #include <drm/amdgpu_drm.h>
37 #include <linux/vgaarb.h>
38 #include <linux/vga_switcheroo.h>
39 #include <linux/efi.h>
41 #include "amdgpu_trace.h"
42 #include "amdgpu_i2c.h"
44 #include "amdgpu_atombios.h"
45 #include "amdgpu_atomfirmware.h"
47 #ifdef CONFIG_DRM_AMDGPU_SI
50 #ifdef CONFIG_DRM_AMDGPU_CIK
56 #include "bif/bif_4_1_d.h"
57 #include <linux/pci.h>
58 #include <linux/firmware.h>
59 #include "amdgpu_vf_error.h"
61 #include "amdgpu_amdkfd.h"
62 #include "amdgpu_pm.h"
64 #include "amdgpu_xgmi.h"
65 #include "amdgpu_ras.h"
66 #include "amdgpu_pmu.h"
67 #include "amdgpu_fru_eeprom.h"
68 #include "amdgpu_reset.h"
70 #include <linux/suspend.h>
71 #include <drm/task_barrier.h>
72 #include <linux/pm_runtime.h>
74 #include <drm/drm_drv.h>
76 MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
77 MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
78 MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
79 MODULE_FIRMWARE("amdgpu/picasso_gpu_info.bin");
80 MODULE_FIRMWARE("amdgpu/raven2_gpu_info.bin");
81 MODULE_FIRMWARE("amdgpu/arcturus_gpu_info.bin");
82 MODULE_FIRMWARE("amdgpu/renoir_gpu_info.bin");
83 MODULE_FIRMWARE("amdgpu/navi10_gpu_info.bin");
84 MODULE_FIRMWARE("amdgpu/navi14_gpu_info.bin");
85 MODULE_FIRMWARE("amdgpu/navi12_gpu_info.bin");
86 MODULE_FIRMWARE("amdgpu/vangogh_gpu_info.bin");
87 MODULE_FIRMWARE("amdgpu/yellow_carp_gpu_info.bin");
89 #define AMDGPU_RESUME_MS 2000
91 const char *amdgpu_asic_name[] = {
131 * DOC: pcie_replay_count
133 * The amdgpu driver provides a sysfs API for reporting the total number
134 * of PCIe replays (NAKs)
135 * The file pcie_replay_count is used for this and returns the total
136 * number of replays as a sum of the NAKs generated and NAKs received
139 static ssize_t amdgpu_device_get_pcie_replay_count(struct device *dev,
140 struct device_attribute *attr, char *buf)
142 struct drm_device *ddev = dev_get_drvdata(dev);
143 struct amdgpu_device *adev = drm_to_adev(ddev);
144 uint64_t cnt = amdgpu_asic_get_pcie_replay_count(adev);
146 return sysfs_emit(buf, "%llu\n", cnt);
149 static DEVICE_ATTR(pcie_replay_count, S_IRUGO,
150 amdgpu_device_get_pcie_replay_count, NULL);
152 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev);
157 * The amdgpu driver provides a sysfs API for reporting the product name
159 * The file serial_number is used for this and returns the product name
160 * as returned from the FRU.
161 * NOTE: This is only available for certain server cards
164 static ssize_t amdgpu_device_get_product_name(struct device *dev,
165 struct device_attribute *attr, char *buf)
167 struct drm_device *ddev = dev_get_drvdata(dev);
168 struct amdgpu_device *adev = drm_to_adev(ddev);
170 return sysfs_emit(buf, "%s\n", adev->product_name);
173 static DEVICE_ATTR(product_name, S_IRUGO,
174 amdgpu_device_get_product_name, NULL);
177 * DOC: product_number
179 * The amdgpu driver provides a sysfs API for reporting the part number
181 * The file serial_number is used for this and returns the part number
182 * as returned from the FRU.
183 * NOTE: This is only available for certain server cards
186 static ssize_t amdgpu_device_get_product_number(struct device *dev,
187 struct device_attribute *attr, char *buf)
189 struct drm_device *ddev = dev_get_drvdata(dev);
190 struct amdgpu_device *adev = drm_to_adev(ddev);
192 return sysfs_emit(buf, "%s\n", adev->product_number);
195 static DEVICE_ATTR(product_number, S_IRUGO,
196 amdgpu_device_get_product_number, NULL);
201 * The amdgpu driver provides a sysfs API for reporting the serial number
203 * The file serial_number is used for this and returns the serial number
204 * as returned from the FRU.
205 * NOTE: This is only available for certain server cards
208 static ssize_t amdgpu_device_get_serial_number(struct device *dev,
209 struct device_attribute *attr, char *buf)
211 struct drm_device *ddev = dev_get_drvdata(dev);
212 struct amdgpu_device *adev = drm_to_adev(ddev);
214 return sysfs_emit(buf, "%s\n", adev->serial);
217 static DEVICE_ATTR(serial_number, S_IRUGO,
218 amdgpu_device_get_serial_number, NULL);
221 * amdgpu_device_supports_px - Is the device a dGPU with ATPX power control
223 * @dev: drm_device pointer
225 * Returns true if the device is a dGPU with ATPX power control,
226 * otherwise return false.
228 bool amdgpu_device_supports_px(struct drm_device *dev)
230 struct amdgpu_device *adev = drm_to_adev(dev);
232 if ((adev->flags & AMD_IS_PX) && !amdgpu_is_atpx_hybrid())
238 * amdgpu_device_supports_boco - Is the device a dGPU with ACPI power resources
240 * @dev: drm_device pointer
242 * Returns true if the device is a dGPU with ACPI power control,
243 * otherwise return false.
245 bool amdgpu_device_supports_boco(struct drm_device *dev)
247 struct amdgpu_device *adev = drm_to_adev(dev);
250 ((adev->flags & AMD_IS_PX) && amdgpu_is_atpx_hybrid()))
256 * amdgpu_device_supports_baco - Does the device support BACO
258 * @dev: drm_device pointer
260 * Returns true if the device supporte BACO,
261 * otherwise return false.
263 bool amdgpu_device_supports_baco(struct drm_device *dev)
265 struct amdgpu_device *adev = drm_to_adev(dev);
267 return amdgpu_asic_supports_baco(adev);
271 * amdgpu_device_supports_smart_shift - Is the device dGPU with
272 * smart shift support
274 * @dev: drm_device pointer
276 * Returns true if the device is a dGPU with Smart Shift support,
277 * otherwise returns false.
279 bool amdgpu_device_supports_smart_shift(struct drm_device *dev)
281 return (amdgpu_device_supports_boco(dev) &&
282 amdgpu_acpi_is_power_shift_control_supported());
286 * VRAM access helper functions
290 * amdgpu_device_vram_access - read/write a buffer in vram
292 * @adev: amdgpu_device pointer
293 * @pos: offset of the buffer in vram
294 * @buf: virtual address of the buffer in system memory
295 * @size: read/write size, sizeof(@buf) must > @size
296 * @write: true - write to vram, otherwise - read from vram
298 void amdgpu_device_vram_access(struct amdgpu_device *adev, loff_t pos,
299 uint32_t *buf, size_t size, bool write)
306 if (!drm_dev_enter(&adev->ddev, &idx))
310 last = min(pos + size, adev->gmc.visible_vram_size);
312 void __iomem *addr = adev->mman.aper_base_kaddr + pos;
313 size_t count = last - pos;
316 memcpy_toio(addr, buf, count);
318 amdgpu_device_flush_hdp(adev, NULL);
320 amdgpu_device_invalidate_hdp(adev, NULL);
322 memcpy_fromio(buf, addr, count);
334 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
335 for (last = pos + size; pos < last; pos += 4) {
336 uint32_t tmp = pos >> 31;
338 WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)pos) | 0x80000000);
340 WREG32_NO_KIQ(mmMM_INDEX_HI, tmp);
344 WREG32_NO_KIQ(mmMM_DATA, *buf++);
346 *buf++ = RREG32_NO_KIQ(mmMM_DATA);
348 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
357 * register access helper functions.
360 /* Check if hw access should be skipped because of hotplug or device error */
361 bool amdgpu_device_skip_hw_access(struct amdgpu_device *adev)
363 if (adev->no_hw_access)
366 #ifdef CONFIG_LOCKDEP
368 * This is a bit complicated to understand, so worth a comment. What we assert
369 * here is that the GPU reset is not running on another thread in parallel.
371 * For this we trylock the read side of the reset semaphore, if that succeeds
372 * we know that the reset is not running in paralell.
374 * If the trylock fails we assert that we are either already holding the read
375 * side of the lock or are the reset thread itself and hold the write side of
379 if (down_read_trylock(&adev->reset_sem))
380 up_read(&adev->reset_sem);
382 lockdep_assert_held(&adev->reset_sem);
389 * amdgpu_device_rreg - read a memory mapped IO or indirect register
391 * @adev: amdgpu_device pointer
392 * @reg: dword aligned register offset
393 * @acc_flags: access flags which require special behavior
395 * Returns the 32 bit value from the offset specified.
397 uint32_t amdgpu_device_rreg(struct amdgpu_device *adev,
398 uint32_t reg, uint32_t acc_flags)
402 if (amdgpu_device_skip_hw_access(adev))
405 if ((reg * 4) < adev->rmmio_size) {
406 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
407 amdgpu_sriov_runtime(adev) &&
408 down_read_trylock(&adev->reset_sem)) {
409 ret = amdgpu_kiq_rreg(adev, reg);
410 up_read(&adev->reset_sem);
412 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
415 ret = adev->pcie_rreg(adev, reg * 4);
418 trace_amdgpu_device_rreg(adev->pdev->device, reg, ret);
424 * MMIO register read with bytes helper functions
425 * @offset:bytes offset from MMIO start
430 * amdgpu_mm_rreg8 - read a memory mapped IO register
432 * @adev: amdgpu_device pointer
433 * @offset: byte aligned register offset
435 * Returns the 8 bit value from the offset specified.
437 uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset)
439 if (amdgpu_device_skip_hw_access(adev))
442 if (offset < adev->rmmio_size)
443 return (readb(adev->rmmio + offset));
448 * MMIO register write with bytes helper functions
449 * @offset:bytes offset from MMIO start
450 * @value: the value want to be written to the register
454 * amdgpu_mm_wreg8 - read a memory mapped IO register
456 * @adev: amdgpu_device pointer
457 * @offset: byte aligned register offset
458 * @value: 8 bit value to write
460 * Writes the value specified to the offset specified.
462 void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value)
464 if (amdgpu_device_skip_hw_access(adev))
467 if (offset < adev->rmmio_size)
468 writeb(value, adev->rmmio + offset);
474 * amdgpu_device_wreg - write to a memory mapped IO or indirect register
476 * @adev: amdgpu_device pointer
477 * @reg: dword aligned register offset
478 * @v: 32 bit value to write to the register
479 * @acc_flags: access flags which require special behavior
481 * Writes the value specified to the offset specified.
483 void amdgpu_device_wreg(struct amdgpu_device *adev,
484 uint32_t reg, uint32_t v,
487 if (amdgpu_device_skip_hw_access(adev))
490 if ((reg * 4) < adev->rmmio_size) {
491 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
492 amdgpu_sriov_runtime(adev) &&
493 down_read_trylock(&adev->reset_sem)) {
494 amdgpu_kiq_wreg(adev, reg, v);
495 up_read(&adev->reset_sem);
497 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
500 adev->pcie_wreg(adev, reg * 4, v);
503 trace_amdgpu_device_wreg(adev->pdev->device, reg, v);
507 * amdgpu_mm_wreg_mmio_rlc - write register either with mmio or with RLC path if in range
509 * this function is invoked only the debugfs register access
511 void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
512 uint32_t reg, uint32_t v)
514 if (amdgpu_device_skip_hw_access(adev))
517 if (amdgpu_sriov_fullaccess(adev) &&
518 adev->gfx.rlc.funcs &&
519 adev->gfx.rlc.funcs->is_rlcg_access_range) {
520 if (adev->gfx.rlc.funcs->is_rlcg_access_range(adev, reg))
521 return adev->gfx.rlc.funcs->rlcg_wreg(adev, reg, v, 0, 0);
523 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
528 * amdgpu_mm_rdoorbell - read a doorbell dword
530 * @adev: amdgpu_device pointer
531 * @index: doorbell index
533 * Returns the value in the doorbell aperture at the
534 * requested doorbell index (CIK).
536 u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
538 if (amdgpu_device_skip_hw_access(adev))
541 if (index < adev->doorbell.num_doorbells) {
542 return readl(adev->doorbell.ptr + index);
544 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
550 * amdgpu_mm_wdoorbell - write a doorbell dword
552 * @adev: amdgpu_device pointer
553 * @index: doorbell index
556 * Writes @v to the doorbell aperture at the
557 * requested doorbell index (CIK).
559 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
561 if (amdgpu_device_skip_hw_access(adev))
564 if (index < adev->doorbell.num_doorbells) {
565 writel(v, adev->doorbell.ptr + index);
567 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
572 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
574 * @adev: amdgpu_device pointer
575 * @index: doorbell index
577 * Returns the value in the doorbell aperture at the
578 * requested doorbell index (VEGA10+).
580 u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
582 if (amdgpu_device_skip_hw_access(adev))
585 if (index < adev->doorbell.num_doorbells) {
586 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
588 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
594 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
596 * @adev: amdgpu_device pointer
597 * @index: doorbell index
600 * Writes @v to the doorbell aperture at the
601 * requested doorbell index (VEGA10+).
603 void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
605 if (amdgpu_device_skip_hw_access(adev))
608 if (index < adev->doorbell.num_doorbells) {
609 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
611 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
616 * amdgpu_device_indirect_rreg - read an indirect register
618 * @adev: amdgpu_device pointer
619 * @pcie_index: mmio register offset
620 * @pcie_data: mmio register offset
621 * @reg_addr: indirect register address to read from
623 * Returns the value of indirect register @reg_addr
625 u32 amdgpu_device_indirect_rreg(struct amdgpu_device *adev,
626 u32 pcie_index, u32 pcie_data,
631 void __iomem *pcie_index_offset;
632 void __iomem *pcie_data_offset;
634 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
635 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
636 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
638 writel(reg_addr, pcie_index_offset);
639 readl(pcie_index_offset);
640 r = readl(pcie_data_offset);
641 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
647 * amdgpu_device_indirect_rreg64 - read a 64bits indirect register
649 * @adev: amdgpu_device pointer
650 * @pcie_index: mmio register offset
651 * @pcie_data: mmio register offset
652 * @reg_addr: indirect register address to read from
654 * Returns the value of indirect register @reg_addr
656 u64 amdgpu_device_indirect_rreg64(struct amdgpu_device *adev,
657 u32 pcie_index, u32 pcie_data,
662 void __iomem *pcie_index_offset;
663 void __iomem *pcie_data_offset;
665 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
666 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
667 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
669 /* read low 32 bits */
670 writel(reg_addr, pcie_index_offset);
671 readl(pcie_index_offset);
672 r = readl(pcie_data_offset);
673 /* read high 32 bits */
674 writel(reg_addr + 4, pcie_index_offset);
675 readl(pcie_index_offset);
676 r |= ((u64)readl(pcie_data_offset) << 32);
677 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
683 * amdgpu_device_indirect_wreg - write an indirect register address
685 * @adev: amdgpu_device pointer
686 * @pcie_index: mmio register offset
687 * @pcie_data: mmio register offset
688 * @reg_addr: indirect register offset
689 * @reg_data: indirect register data
692 void amdgpu_device_indirect_wreg(struct amdgpu_device *adev,
693 u32 pcie_index, u32 pcie_data,
694 u32 reg_addr, u32 reg_data)
697 void __iomem *pcie_index_offset;
698 void __iomem *pcie_data_offset;
700 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
701 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
702 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
704 writel(reg_addr, pcie_index_offset);
705 readl(pcie_index_offset);
706 writel(reg_data, pcie_data_offset);
707 readl(pcie_data_offset);
708 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
712 * amdgpu_device_indirect_wreg64 - write a 64bits indirect register address
714 * @adev: amdgpu_device pointer
715 * @pcie_index: mmio register offset
716 * @pcie_data: mmio register offset
717 * @reg_addr: indirect register offset
718 * @reg_data: indirect register data
721 void amdgpu_device_indirect_wreg64(struct amdgpu_device *adev,
722 u32 pcie_index, u32 pcie_data,
723 u32 reg_addr, u64 reg_data)
726 void __iomem *pcie_index_offset;
727 void __iomem *pcie_data_offset;
729 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
730 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
731 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
733 /* write low 32 bits */
734 writel(reg_addr, pcie_index_offset);
735 readl(pcie_index_offset);
736 writel((u32)(reg_data & 0xffffffffULL), pcie_data_offset);
737 readl(pcie_data_offset);
738 /* write high 32 bits */
739 writel(reg_addr + 4, pcie_index_offset);
740 readl(pcie_index_offset);
741 writel((u32)(reg_data >> 32), pcie_data_offset);
742 readl(pcie_data_offset);
743 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
747 * amdgpu_invalid_rreg - dummy reg read function
749 * @adev: amdgpu_device pointer
750 * @reg: offset of register
752 * Dummy register read function. Used for register blocks
753 * that certain asics don't have (all asics).
754 * Returns the value in the register.
756 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
758 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
764 * amdgpu_invalid_wreg - dummy reg write function
766 * @adev: amdgpu_device pointer
767 * @reg: offset of register
768 * @v: value to write to the register
770 * Dummy register read function. Used for register blocks
771 * that certain asics don't have (all asics).
773 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
775 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
781 * amdgpu_invalid_rreg64 - dummy 64 bit reg read function
783 * @adev: amdgpu_device pointer
784 * @reg: offset of register
786 * Dummy register read function. Used for register blocks
787 * that certain asics don't have (all asics).
788 * Returns the value in the register.
790 static uint64_t amdgpu_invalid_rreg64(struct amdgpu_device *adev, uint32_t reg)
792 DRM_ERROR("Invalid callback to read 64 bit register 0x%04X\n", reg);
798 * amdgpu_invalid_wreg64 - dummy reg write function
800 * @adev: amdgpu_device pointer
801 * @reg: offset of register
802 * @v: value to write to the register
804 * Dummy register read function. Used for register blocks
805 * that certain asics don't have (all asics).
807 static void amdgpu_invalid_wreg64(struct amdgpu_device *adev, uint32_t reg, uint64_t v)
809 DRM_ERROR("Invalid callback to write 64 bit register 0x%04X with 0x%08llX\n",
815 * amdgpu_block_invalid_rreg - dummy reg read function
817 * @adev: amdgpu_device pointer
818 * @block: offset of instance
819 * @reg: offset of register
821 * Dummy register read function. Used for register blocks
822 * that certain asics don't have (all asics).
823 * Returns the value in the register.
825 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
826 uint32_t block, uint32_t reg)
828 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
835 * amdgpu_block_invalid_wreg - dummy reg write function
837 * @adev: amdgpu_device pointer
838 * @block: offset of instance
839 * @reg: offset of register
840 * @v: value to write to the register
842 * Dummy register read function. Used for register blocks
843 * that certain asics don't have (all asics).
845 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
847 uint32_t reg, uint32_t v)
849 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
855 * amdgpu_device_asic_init - Wrapper for atom asic_init
857 * @adev: amdgpu_device pointer
859 * Does any asic specific work and then calls atom asic init.
861 static int amdgpu_device_asic_init(struct amdgpu_device *adev)
863 amdgpu_asic_pre_asic_init(adev);
865 return amdgpu_atom_asic_init(adev->mode_info.atom_context);
869 * amdgpu_device_vram_scratch_init - allocate the VRAM scratch page
871 * @adev: amdgpu_device pointer
873 * Allocates a scratch page of VRAM for use by various things in the
876 static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev)
878 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
879 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
880 &adev->vram_scratch.robj,
881 &adev->vram_scratch.gpu_addr,
882 (void **)&adev->vram_scratch.ptr);
886 * amdgpu_device_vram_scratch_fini - Free the VRAM scratch page
888 * @adev: amdgpu_device pointer
890 * Frees the VRAM scratch page.
892 static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev)
894 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
898 * amdgpu_device_program_register_sequence - program an array of registers.
900 * @adev: amdgpu_device pointer
901 * @registers: pointer to the register array
902 * @array_size: size of the register array
904 * Programs an array or registers with and and or masks.
905 * This is a helper for setting golden registers.
907 void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
908 const u32 *registers,
909 const u32 array_size)
911 u32 tmp, reg, and_mask, or_mask;
917 for (i = 0; i < array_size; i +=3) {
918 reg = registers[i + 0];
919 and_mask = registers[i + 1];
920 or_mask = registers[i + 2];
922 if (and_mask == 0xffffffff) {
927 if (adev->family >= AMDGPU_FAMILY_AI)
928 tmp |= (or_mask & and_mask);
937 * amdgpu_device_pci_config_reset - reset the GPU
939 * @adev: amdgpu_device pointer
941 * Resets the GPU using the pci config reset sequence.
942 * Only applicable to asics prior to vega10.
944 void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
946 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
950 * amdgpu_device_pci_reset - reset the GPU using generic PCI means
952 * @adev: amdgpu_device pointer
954 * Resets the GPU using generic pci reset interfaces (FLR, SBR, etc.).
956 int amdgpu_device_pci_reset(struct amdgpu_device *adev)
958 return pci_reset_function(adev->pdev);
962 * GPU doorbell aperture helpers function.
965 * amdgpu_device_doorbell_init - Init doorbell driver information.
967 * @adev: amdgpu_device pointer
969 * Init doorbell driver information (CIK)
970 * Returns 0 on success, error on failure.
972 static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
975 /* No doorbell on SI hardware generation */
976 if (adev->asic_type < CHIP_BONAIRE) {
977 adev->doorbell.base = 0;
978 adev->doorbell.size = 0;
979 adev->doorbell.num_doorbells = 0;
980 adev->doorbell.ptr = NULL;
984 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
987 amdgpu_asic_init_doorbell_index(adev);
989 /* doorbell bar mapping */
990 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
991 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
993 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
994 adev->doorbell_index.max_assignment+1);
995 if (adev->doorbell.num_doorbells == 0)
998 /* For Vega, reserve and map two pages on doorbell BAR since SDMA
999 * paging queue doorbell use the second page. The
1000 * AMDGPU_DOORBELL64_MAX_ASSIGNMENT definition assumes all the
1001 * doorbells are in the first page. So with paging queue enabled,
1002 * the max num_doorbells should + 1 page (0x400 in dword)
1004 if (adev->asic_type >= CHIP_VEGA10)
1005 adev->doorbell.num_doorbells += 0x400;
1007 adev->doorbell.ptr = ioremap(adev->doorbell.base,
1008 adev->doorbell.num_doorbells *
1010 if (adev->doorbell.ptr == NULL)
1017 * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
1019 * @adev: amdgpu_device pointer
1021 * Tear down doorbell driver information (CIK)
1023 static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
1025 iounmap(adev->doorbell.ptr);
1026 adev->doorbell.ptr = NULL;
1032 * amdgpu_device_wb_*()
1033 * Writeback is the method by which the GPU updates special pages in memory
1034 * with the status of certain GPU events (fences, ring pointers,etc.).
1038 * amdgpu_device_wb_fini - Disable Writeback and free memory
1040 * @adev: amdgpu_device pointer
1042 * Disables Writeback and frees the Writeback memory (all asics).
1043 * Used at driver shutdown.
1045 static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
1047 if (adev->wb.wb_obj) {
1048 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
1050 (void **)&adev->wb.wb);
1051 adev->wb.wb_obj = NULL;
1056 * amdgpu_device_wb_init- Init Writeback driver info and allocate memory
1058 * @adev: amdgpu_device pointer
1060 * Initializes writeback and allocates writeback memory (all asics).
1061 * Used at driver startup.
1062 * Returns 0 on success or an -error on failure.
1064 static int amdgpu_device_wb_init(struct amdgpu_device *adev)
1068 if (adev->wb.wb_obj == NULL) {
1069 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
1070 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
1071 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
1072 &adev->wb.wb_obj, &adev->wb.gpu_addr,
1073 (void **)&adev->wb.wb);
1075 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
1079 adev->wb.num_wb = AMDGPU_MAX_WB;
1080 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
1082 /* clear wb memory */
1083 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
1090 * amdgpu_device_wb_get - Allocate a wb entry
1092 * @adev: amdgpu_device pointer
1095 * Allocate a wb slot for use by the driver (all asics).
1096 * Returns 0 on success or -EINVAL on failure.
1098 int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
1100 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
1102 if (offset < adev->wb.num_wb) {
1103 __set_bit(offset, adev->wb.used);
1104 *wb = offset << 3; /* convert to dw offset */
1112 * amdgpu_device_wb_free - Free a wb entry
1114 * @adev: amdgpu_device pointer
1117 * Free a wb slot allocated for use by the driver (all asics)
1119 void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
1122 if (wb < adev->wb.num_wb)
1123 __clear_bit(wb, adev->wb.used);
1127 * amdgpu_device_resize_fb_bar - try to resize FB BAR
1129 * @adev: amdgpu_device pointer
1131 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
1132 * to fail, but if any of the BARs is not accessible after the size we abort
1133 * driver loading by returning -ENODEV.
1135 int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
1137 int rbar_size = pci_rebar_bytes_to_size(adev->gmc.real_vram_size);
1138 struct pci_bus *root;
1139 struct resource *res;
1145 if (amdgpu_sriov_vf(adev))
1148 /* skip if the bios has already enabled large BAR */
1149 if (adev->gmc.real_vram_size &&
1150 (pci_resource_len(adev->pdev, 0) >= adev->gmc.real_vram_size))
1153 /* Check if the root BUS has 64bit memory resources */
1154 root = adev->pdev->bus;
1155 while (root->parent)
1156 root = root->parent;
1158 pci_bus_for_each_resource(root, res, i) {
1159 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
1160 res->start > 0x100000000ull)
1164 /* Trying to resize is pointless without a root hub window above 4GB */
1168 /* Limit the BAR size to what is available */
1169 rbar_size = min(fls(pci_rebar_get_possible_sizes(adev->pdev, 0)) - 1,
1172 /* Disable memory decoding while we change the BAR addresses and size */
1173 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
1174 pci_write_config_word(adev->pdev, PCI_COMMAND,
1175 cmd & ~PCI_COMMAND_MEMORY);
1177 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
1178 amdgpu_device_doorbell_fini(adev);
1179 if (adev->asic_type >= CHIP_BONAIRE)
1180 pci_release_resource(adev->pdev, 2);
1182 pci_release_resource(adev->pdev, 0);
1184 r = pci_resize_resource(adev->pdev, 0, rbar_size);
1186 DRM_INFO("Not enough PCI address space for a large BAR.");
1187 else if (r && r != -ENOTSUPP)
1188 DRM_ERROR("Problem resizing BAR0 (%d).", r);
1190 pci_assign_unassigned_bus_resources(adev->pdev->bus);
1192 /* When the doorbell or fb BAR isn't available we have no chance of
1195 r = amdgpu_device_doorbell_init(adev);
1196 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
1199 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
1205 * GPU helpers function.
1208 * amdgpu_device_need_post - check if the hw need post or not
1210 * @adev: amdgpu_device pointer
1212 * Check if the asic has been initialized (all asics) at driver startup
1213 * or post is needed if hw reset is performed.
1214 * Returns true if need or false if not.
1216 bool amdgpu_device_need_post(struct amdgpu_device *adev)
1220 if (amdgpu_sriov_vf(adev))
1223 if (amdgpu_passthrough(adev)) {
1224 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
1225 * some old smc fw still need driver do vPost otherwise gpu hang, while
1226 * those smc fw version above 22.15 doesn't have this flaw, so we force
1227 * vpost executed for smc version below 22.15
1229 if (adev->asic_type == CHIP_FIJI) {
1232 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
1233 /* force vPost if error occured */
1237 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
1238 if (fw_ver < 0x00160e00)
1243 /* Don't post if we need to reset whole hive on init */
1244 if (adev->gmc.xgmi.pending_reset)
1247 if (adev->has_hw_reset) {
1248 adev->has_hw_reset = false;
1252 /* bios scratch used on CIK+ */
1253 if (adev->asic_type >= CHIP_BONAIRE)
1254 return amdgpu_atombios_scratch_need_asic_init(adev);
1256 /* check MEM_SIZE for older asics */
1257 reg = amdgpu_asic_get_config_memsize(adev);
1259 if ((reg != 0) && (reg != 0xffffffff))
1265 /* if we get transitioned to only one device, take VGA back */
1267 * amdgpu_device_vga_set_decode - enable/disable vga decode
1269 * @cookie: amdgpu_device pointer
1270 * @state: enable/disable vga decode
1272 * Enable/disable vga decode (all asics).
1273 * Returns VGA resource flags.
1275 static unsigned int amdgpu_device_vga_set_decode(void *cookie, bool state)
1277 struct amdgpu_device *adev = cookie;
1278 amdgpu_asic_set_vga_state(adev, state);
1280 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1281 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1283 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1287 * amdgpu_device_check_block_size - validate the vm block size
1289 * @adev: amdgpu_device pointer
1291 * Validates the vm block size specified via module parameter.
1292 * The vm block size defines number of bits in page table versus page directory,
1293 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1294 * page table and the remaining bits are in the page directory.
1296 static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
1298 /* defines number of bits in page table versus page directory,
1299 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1300 * page table and the remaining bits are in the page directory */
1301 if (amdgpu_vm_block_size == -1)
1304 if (amdgpu_vm_block_size < 9) {
1305 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1306 amdgpu_vm_block_size);
1307 amdgpu_vm_block_size = -1;
1312 * amdgpu_device_check_vm_size - validate the vm size
1314 * @adev: amdgpu_device pointer
1316 * Validates the vm size in GB specified via module parameter.
1317 * The VM size is the size of the GPU virtual memory space in GB.
1319 static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
1321 /* no need to check the default value */
1322 if (amdgpu_vm_size == -1)
1325 if (amdgpu_vm_size < 1) {
1326 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1328 amdgpu_vm_size = -1;
1332 static void amdgpu_device_check_smu_prv_buffer_size(struct amdgpu_device *adev)
1335 bool is_os_64 = (sizeof(void *) == 8);
1336 uint64_t total_memory;
1337 uint64_t dram_size_seven_GB = 0x1B8000000;
1338 uint64_t dram_size_three_GB = 0xB8000000;
1340 if (amdgpu_smu_memory_pool_size == 0)
1344 DRM_WARN("Not 64-bit OS, feature not supported\n");
1348 total_memory = (uint64_t)si.totalram * si.mem_unit;
1350 if ((amdgpu_smu_memory_pool_size == 1) ||
1351 (amdgpu_smu_memory_pool_size == 2)) {
1352 if (total_memory < dram_size_three_GB)
1354 } else if ((amdgpu_smu_memory_pool_size == 4) ||
1355 (amdgpu_smu_memory_pool_size == 8)) {
1356 if (total_memory < dram_size_seven_GB)
1359 DRM_WARN("Smu memory pool size not supported\n");
1362 adev->pm.smu_prv_buffer_size = amdgpu_smu_memory_pool_size << 28;
1367 DRM_WARN("No enough system memory\n");
1369 adev->pm.smu_prv_buffer_size = 0;
1373 * amdgpu_device_check_arguments - validate module params
1375 * @adev: amdgpu_device pointer
1377 * Validates certain module parameters and updates
1378 * the associated values used by the driver (all asics).
1380 static int amdgpu_device_check_arguments(struct amdgpu_device *adev)
1382 if (amdgpu_sched_jobs < 4) {
1383 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1385 amdgpu_sched_jobs = 4;
1386 } else if (!is_power_of_2(amdgpu_sched_jobs)){
1387 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1389 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1392 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
1393 /* gart size must be greater or equal to 32M */
1394 dev_warn(adev->dev, "gart size (%d) too small\n",
1396 amdgpu_gart_size = -1;
1399 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
1400 /* gtt size must be greater or equal to 32M */
1401 dev_warn(adev->dev, "gtt size (%d) too small\n",
1403 amdgpu_gtt_size = -1;
1406 /* valid range is between 4 and 9 inclusive */
1407 if (amdgpu_vm_fragment_size != -1 &&
1408 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1409 dev_warn(adev->dev, "valid range is between 4 and 9\n");
1410 amdgpu_vm_fragment_size = -1;
1413 if (amdgpu_sched_hw_submission < 2) {
1414 dev_warn(adev->dev, "sched hw submission jobs (%d) must be at least 2\n",
1415 amdgpu_sched_hw_submission);
1416 amdgpu_sched_hw_submission = 2;
1417 } else if (!is_power_of_2(amdgpu_sched_hw_submission)) {
1418 dev_warn(adev->dev, "sched hw submission jobs (%d) must be a power of 2\n",
1419 amdgpu_sched_hw_submission);
1420 amdgpu_sched_hw_submission = roundup_pow_of_two(amdgpu_sched_hw_submission);
1423 amdgpu_device_check_smu_prv_buffer_size(adev);
1425 amdgpu_device_check_vm_size(adev);
1427 amdgpu_device_check_block_size(adev);
1429 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
1431 amdgpu_gmc_tmz_set(adev);
1433 amdgpu_gmc_noretry_set(adev);
1439 * amdgpu_switcheroo_set_state - set switcheroo state
1441 * @pdev: pci dev pointer
1442 * @state: vga_switcheroo state
1444 * Callback for the switcheroo driver. Suspends or resumes the
1445 * the asics before or after it is powered up using ACPI methods.
1447 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev,
1448 enum vga_switcheroo_state state)
1450 struct drm_device *dev = pci_get_drvdata(pdev);
1453 if (amdgpu_device_supports_px(dev) && state == VGA_SWITCHEROO_OFF)
1456 if (state == VGA_SWITCHEROO_ON) {
1457 pr_info("switched on\n");
1458 /* don't suspend or resume card normally */
1459 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1461 pci_set_power_state(pdev, PCI_D0);
1462 amdgpu_device_load_pci_state(pdev);
1463 r = pci_enable_device(pdev);
1465 DRM_WARN("pci_enable_device failed (%d)\n", r);
1466 amdgpu_device_resume(dev, true);
1468 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1470 pr_info("switched off\n");
1471 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1472 amdgpu_device_suspend(dev, true);
1473 amdgpu_device_cache_pci_state(pdev);
1474 /* Shut down the device */
1475 pci_disable_device(pdev);
1476 pci_set_power_state(pdev, PCI_D3cold);
1477 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1482 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1484 * @pdev: pci dev pointer
1486 * Callback for the switcheroo driver. Check of the switcheroo
1487 * state can be changed.
1488 * Returns true if the state can be changed, false if not.
1490 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1492 struct drm_device *dev = pci_get_drvdata(pdev);
1495 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1496 * locking inversion with the driver load path. And the access here is
1497 * completely racy anyway. So don't bother with locking for now.
1499 return atomic_read(&dev->open_count) == 0;
1502 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1503 .set_gpu_state = amdgpu_switcheroo_set_state,
1505 .can_switch = amdgpu_switcheroo_can_switch,
1509 * amdgpu_device_ip_set_clockgating_state - set the CG state
1511 * @dev: amdgpu_device pointer
1512 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1513 * @state: clockgating state (gate or ungate)
1515 * Sets the requested clockgating state for all instances of
1516 * the hardware IP specified.
1517 * Returns the error code from the last instance.
1519 int amdgpu_device_ip_set_clockgating_state(void *dev,
1520 enum amd_ip_block_type block_type,
1521 enum amd_clockgating_state state)
1523 struct amdgpu_device *adev = dev;
1526 for (i = 0; i < adev->num_ip_blocks; i++) {
1527 if (!adev->ip_blocks[i].status.valid)
1529 if (adev->ip_blocks[i].version->type != block_type)
1531 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1533 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1534 (void *)adev, state);
1536 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1537 adev->ip_blocks[i].version->funcs->name, r);
1543 * amdgpu_device_ip_set_powergating_state - set the PG state
1545 * @dev: amdgpu_device pointer
1546 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1547 * @state: powergating state (gate or ungate)
1549 * Sets the requested powergating state for all instances of
1550 * the hardware IP specified.
1551 * Returns the error code from the last instance.
1553 int amdgpu_device_ip_set_powergating_state(void *dev,
1554 enum amd_ip_block_type block_type,
1555 enum amd_powergating_state state)
1557 struct amdgpu_device *adev = dev;
1560 for (i = 0; i < adev->num_ip_blocks; i++) {
1561 if (!adev->ip_blocks[i].status.valid)
1563 if (adev->ip_blocks[i].version->type != block_type)
1565 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1567 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1568 (void *)adev, state);
1570 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1571 adev->ip_blocks[i].version->funcs->name, r);
1577 * amdgpu_device_ip_get_clockgating_state - get the CG state
1579 * @adev: amdgpu_device pointer
1580 * @flags: clockgating feature flags
1582 * Walks the list of IPs on the device and updates the clockgating
1583 * flags for each IP.
1584 * Updates @flags with the feature flags for each hardware IP where
1585 * clockgating is enabled.
1587 void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
1592 for (i = 0; i < adev->num_ip_blocks; i++) {
1593 if (!adev->ip_blocks[i].status.valid)
1595 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1596 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1601 * amdgpu_device_ip_wait_for_idle - wait for idle
1603 * @adev: amdgpu_device pointer
1604 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1606 * Waits for the request hardware IP to be idle.
1607 * Returns 0 for success or a negative error code on failure.
1609 int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1610 enum amd_ip_block_type block_type)
1614 for (i = 0; i < adev->num_ip_blocks; i++) {
1615 if (!adev->ip_blocks[i].status.valid)
1617 if (adev->ip_blocks[i].version->type == block_type) {
1618 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
1629 * amdgpu_device_ip_is_idle - is the hardware IP idle
1631 * @adev: amdgpu_device pointer
1632 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1634 * Check if the hardware IP is idle or not.
1635 * Returns true if it the IP is idle, false if not.
1637 bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1638 enum amd_ip_block_type block_type)
1642 for (i = 0; i < adev->num_ip_blocks; i++) {
1643 if (!adev->ip_blocks[i].status.valid)
1645 if (adev->ip_blocks[i].version->type == block_type)
1646 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
1653 * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1655 * @adev: amdgpu_device pointer
1656 * @type: Type of hardware IP (SMU, GFX, UVD, etc.)
1658 * Returns a pointer to the hardware IP block structure
1659 * if it exists for the asic, otherwise NULL.
1661 struct amdgpu_ip_block *
1662 amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1663 enum amd_ip_block_type type)
1667 for (i = 0; i < adev->num_ip_blocks; i++)
1668 if (adev->ip_blocks[i].version->type == type)
1669 return &adev->ip_blocks[i];
1675 * amdgpu_device_ip_block_version_cmp
1677 * @adev: amdgpu_device pointer
1678 * @type: enum amd_ip_block_type
1679 * @major: major version
1680 * @minor: minor version
1682 * return 0 if equal or greater
1683 * return 1 if smaller or the ip_block doesn't exist
1685 int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1686 enum amd_ip_block_type type,
1687 u32 major, u32 minor)
1689 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
1691 if (ip_block && ((ip_block->version->major > major) ||
1692 ((ip_block->version->major == major) &&
1693 (ip_block->version->minor >= minor))))
1700 * amdgpu_device_ip_block_add
1702 * @adev: amdgpu_device pointer
1703 * @ip_block_version: pointer to the IP to add
1705 * Adds the IP block driver information to the collection of IPs
1708 int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1709 const struct amdgpu_ip_block_version *ip_block_version)
1711 if (!ip_block_version)
1714 switch (ip_block_version->type) {
1715 case AMD_IP_BLOCK_TYPE_VCN:
1716 if (adev->harvest_ip_mask & AMD_HARVEST_IP_VCN_MASK)
1719 case AMD_IP_BLOCK_TYPE_JPEG:
1720 if (adev->harvest_ip_mask & AMD_HARVEST_IP_JPEG_MASK)
1727 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
1728 ip_block_version->funcs->name);
1730 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1736 * amdgpu_device_enable_virtual_display - enable virtual display feature
1738 * @adev: amdgpu_device pointer
1740 * Enabled the virtual display feature if the user has enabled it via
1741 * the module parameter virtual_display. This feature provides a virtual
1742 * display hardware on headless boards or in virtualized environments.
1743 * This function parses and validates the configuration string specified by
1744 * the user and configues the virtual display configuration (number of
1745 * virtual connectors, crtcs, etc.) specified.
1747 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
1749 adev->enable_virtual_display = false;
1751 if (amdgpu_virtual_display) {
1752 const char *pci_address_name = pci_name(adev->pdev);
1753 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
1755 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1756 pciaddstr_tmp = pciaddstr;
1757 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1758 pciaddname = strsep(&pciaddname_tmp, ",");
1759 if (!strcmp("all", pciaddname)
1760 || !strcmp(pci_address_name, pciaddname)) {
1764 adev->enable_virtual_display = true;
1767 res = kstrtol(pciaddname_tmp, 10,
1775 adev->mode_info.num_crtc = num_crtc;
1777 adev->mode_info.num_crtc = 1;
1783 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1784 amdgpu_virtual_display, pci_address_name,
1785 adev->enable_virtual_display, adev->mode_info.num_crtc);
1792 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
1794 * @adev: amdgpu_device pointer
1796 * Parses the asic configuration parameters specified in the gpu info
1797 * firmware and makes them availale to the driver for use in configuring
1799 * Returns 0 on success, -EINVAL on failure.
1801 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1803 const char *chip_name;
1806 const struct gpu_info_firmware_header_v1_0 *hdr;
1808 adev->firmware.gpu_info_fw = NULL;
1810 if (adev->mman.discovery_bin) {
1811 amdgpu_discovery_get_gfx_info(adev);
1814 * FIXME: The bounding box is still needed by Navi12, so
1815 * temporarily read it from gpu_info firmware. Should be droped
1816 * when DAL no longer needs it.
1818 if (adev->asic_type != CHIP_NAVI12)
1822 switch (adev->asic_type) {
1823 #ifdef CONFIG_DRM_AMDGPU_SI
1830 #ifdef CONFIG_DRM_AMDGPU_CIK
1840 case CHIP_POLARIS10:
1841 case CHIP_POLARIS11:
1842 case CHIP_POLARIS12:
1847 case CHIP_ALDEBARAN:
1848 case CHIP_SIENNA_CICHLID:
1849 case CHIP_NAVY_FLOUNDER:
1850 case CHIP_DIMGREY_CAVEFISH:
1851 case CHIP_BEIGE_GOBY:
1855 chip_name = "vega10";
1858 chip_name = "vega12";
1861 if (adev->apu_flags & AMD_APU_IS_RAVEN2)
1862 chip_name = "raven2";
1863 else if (adev->apu_flags & AMD_APU_IS_PICASSO)
1864 chip_name = "picasso";
1866 chip_name = "raven";
1869 chip_name = "arcturus";
1872 if (adev->apu_flags & AMD_APU_IS_RENOIR)
1873 chip_name = "renoir";
1875 chip_name = "green_sardine";
1878 chip_name = "navi10";
1881 chip_name = "navi14";
1884 chip_name = "navi12";
1887 chip_name = "vangogh";
1889 case CHIP_YELLOW_CARP:
1890 chip_name = "yellow_carp";
1894 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
1895 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
1898 "Failed to load gpu_info firmware \"%s\"\n",
1902 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
1905 "Failed to validate gpu_info firmware \"%s\"\n",
1910 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
1911 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1913 switch (hdr->version_major) {
1916 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
1917 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
1918 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1921 * Should be droped when DAL no longer needs it.
1923 if (adev->asic_type == CHIP_NAVI12)
1924 goto parse_soc_bounding_box;
1926 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1927 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1928 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1929 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
1930 adev->gfx.config.max_texture_channel_caches =
1931 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1932 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1933 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1934 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1935 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
1936 adev->gfx.config.double_offchip_lds_buf =
1937 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1938 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
1939 adev->gfx.cu_info.max_waves_per_simd =
1940 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1941 adev->gfx.cu_info.max_scratch_slots_per_cu =
1942 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1943 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
1944 if (hdr->version_minor >= 1) {
1945 const struct gpu_info_firmware_v1_1 *gpu_info_fw =
1946 (const struct gpu_info_firmware_v1_1 *)(adev->firmware.gpu_info_fw->data +
1947 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1948 adev->gfx.config.num_sc_per_sh =
1949 le32_to_cpu(gpu_info_fw->num_sc_per_sh);
1950 adev->gfx.config.num_packer_per_sc =
1951 le32_to_cpu(gpu_info_fw->num_packer_per_sc);
1954 parse_soc_bounding_box:
1956 * soc bounding box info is not integrated in disocovery table,
1957 * we always need to parse it from gpu info firmware if needed.
1959 if (hdr->version_minor == 2) {
1960 const struct gpu_info_firmware_v1_2 *gpu_info_fw =
1961 (const struct gpu_info_firmware_v1_2 *)(adev->firmware.gpu_info_fw->data +
1962 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1963 adev->dm.soc_bounding_box = &gpu_info_fw->soc_bounding_box;
1969 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1978 * amdgpu_device_ip_early_init - run early init for hardware IPs
1980 * @adev: amdgpu_device pointer
1982 * Early initialization pass for hardware IPs. The hardware IPs that make
1983 * up each asic are discovered each IP's early_init callback is run. This
1984 * is the first stage in initializing the asic.
1985 * Returns 0 on success, negative error code on failure.
1987 static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
1991 amdgpu_device_enable_virtual_display(adev);
1993 if (amdgpu_sriov_vf(adev)) {
1994 r = amdgpu_virt_request_full_gpu(adev, true);
1999 switch (adev->asic_type) {
2000 #ifdef CONFIG_DRM_AMDGPU_SI
2006 adev->family = AMDGPU_FAMILY_SI;
2007 r = si_set_ip_blocks(adev);
2012 #ifdef CONFIG_DRM_AMDGPU_CIK
2018 if (adev->flags & AMD_IS_APU)
2019 adev->family = AMDGPU_FAMILY_KV;
2021 adev->family = AMDGPU_FAMILY_CI;
2023 r = cik_set_ip_blocks(adev);
2031 case CHIP_POLARIS10:
2032 case CHIP_POLARIS11:
2033 case CHIP_POLARIS12:
2037 if (adev->flags & AMD_IS_APU)
2038 adev->family = AMDGPU_FAMILY_CZ;
2040 adev->family = AMDGPU_FAMILY_VI;
2042 r = vi_set_ip_blocks(adev);
2052 case CHIP_ALDEBARAN:
2053 if (adev->flags & AMD_IS_APU)
2054 adev->family = AMDGPU_FAMILY_RV;
2056 adev->family = AMDGPU_FAMILY_AI;
2058 r = soc15_set_ip_blocks(adev);
2065 case CHIP_SIENNA_CICHLID:
2066 case CHIP_NAVY_FLOUNDER:
2067 case CHIP_DIMGREY_CAVEFISH:
2068 case CHIP_BEIGE_GOBY:
2070 case CHIP_YELLOW_CARP:
2071 if (adev->asic_type == CHIP_VANGOGH)
2072 adev->family = AMDGPU_FAMILY_VGH;
2073 else if (adev->asic_type == CHIP_YELLOW_CARP)
2074 adev->family = AMDGPU_FAMILY_YC;
2076 adev->family = AMDGPU_FAMILY_NV;
2078 r = nv_set_ip_blocks(adev);
2083 /* FIXME: not supported yet */
2087 amdgpu_amdkfd_device_probe(adev);
2089 adev->pm.pp_feature = amdgpu_pp_feature_mask;
2090 if (amdgpu_sriov_vf(adev) || sched_policy == KFD_SCHED_POLICY_NO_HWS)
2091 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
2092 if (amdgpu_sriov_vf(adev) && adev->asic_type == CHIP_SIENNA_CICHLID)
2093 adev->pm.pp_feature &= ~PP_OVERDRIVE_MASK;
2095 for (i = 0; i < adev->num_ip_blocks; i++) {
2096 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
2097 DRM_ERROR("disabled ip block: %d <%s>\n",
2098 i, adev->ip_blocks[i].version->funcs->name);
2099 adev->ip_blocks[i].status.valid = false;
2101 if (adev->ip_blocks[i].version->funcs->early_init) {
2102 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
2104 adev->ip_blocks[i].status.valid = false;
2106 DRM_ERROR("early_init of IP block <%s> failed %d\n",
2107 adev->ip_blocks[i].version->funcs->name, r);
2110 adev->ip_blocks[i].status.valid = true;
2113 adev->ip_blocks[i].status.valid = true;
2116 /* get the vbios after the asic_funcs are set up */
2117 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) {
2118 r = amdgpu_device_parse_gpu_info_fw(adev);
2123 if (!amdgpu_get_bios(adev))
2126 r = amdgpu_atombios_init(adev);
2128 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
2129 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
2133 /*get pf2vf msg info at it's earliest time*/
2134 if (amdgpu_sriov_vf(adev))
2135 amdgpu_virt_init_data_exchange(adev);
2140 adev->cg_flags &= amdgpu_cg_mask;
2141 adev->pg_flags &= amdgpu_pg_mask;
2146 static int amdgpu_device_ip_hw_init_phase1(struct amdgpu_device *adev)
2150 for (i = 0; i < adev->num_ip_blocks; i++) {
2151 if (!adev->ip_blocks[i].status.sw)
2153 if (adev->ip_blocks[i].status.hw)
2155 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2156 (amdgpu_sriov_vf(adev) && (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)) ||
2157 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
2158 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2160 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2161 adev->ip_blocks[i].version->funcs->name, r);
2164 adev->ip_blocks[i].status.hw = true;
2171 static int amdgpu_device_ip_hw_init_phase2(struct amdgpu_device *adev)
2175 for (i = 0; i < adev->num_ip_blocks; i++) {
2176 if (!adev->ip_blocks[i].status.sw)
2178 if (adev->ip_blocks[i].status.hw)
2180 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2182 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2183 adev->ip_blocks[i].version->funcs->name, r);
2186 adev->ip_blocks[i].status.hw = true;
2192 static int amdgpu_device_fw_loading(struct amdgpu_device *adev)
2196 uint32_t smu_version;
2198 if (adev->asic_type >= CHIP_VEGA10) {
2199 for (i = 0; i < adev->num_ip_blocks; i++) {
2200 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_PSP)
2203 if (!adev->ip_blocks[i].status.sw)
2206 /* no need to do the fw loading again if already done*/
2207 if (adev->ip_blocks[i].status.hw == true)
2210 if (amdgpu_in_reset(adev) || adev->in_suspend) {
2211 r = adev->ip_blocks[i].version->funcs->resume(adev);
2213 DRM_ERROR("resume of IP block <%s> failed %d\n",
2214 adev->ip_blocks[i].version->funcs->name, r);
2218 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2220 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2221 adev->ip_blocks[i].version->funcs->name, r);
2226 adev->ip_blocks[i].status.hw = true;
2231 if (!amdgpu_sriov_vf(adev) || adev->asic_type == CHIP_TONGA)
2232 r = amdgpu_pm_load_smu_firmware(adev, &smu_version);
2238 * amdgpu_device_ip_init - run init for hardware IPs
2240 * @adev: amdgpu_device pointer
2242 * Main initialization pass for hardware IPs. The list of all the hardware
2243 * IPs that make up the asic is walked and the sw_init and hw_init callbacks
2244 * are run. sw_init initializes the software state associated with each IP
2245 * and hw_init initializes the hardware associated with each IP.
2246 * Returns 0 on success, negative error code on failure.
2248 static int amdgpu_device_ip_init(struct amdgpu_device *adev)
2252 r = amdgpu_ras_init(adev);
2256 for (i = 0; i < adev->num_ip_blocks; i++) {
2257 if (!adev->ip_blocks[i].status.valid)
2259 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
2261 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
2262 adev->ip_blocks[i].version->funcs->name, r);
2265 adev->ip_blocks[i].status.sw = true;
2267 /* need to do gmc hw init early so we can allocate gpu mem */
2268 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
2269 r = amdgpu_device_vram_scratch_init(adev);
2271 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
2274 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
2276 DRM_ERROR("hw_init %d failed %d\n", i, r);
2279 r = amdgpu_device_wb_init(adev);
2281 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
2284 adev->ip_blocks[i].status.hw = true;
2286 /* right after GMC hw init, we create CSA */
2287 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) {
2288 r = amdgpu_allocate_static_csa(adev, &adev->virt.csa_obj,
2289 AMDGPU_GEM_DOMAIN_VRAM,
2292 DRM_ERROR("allocate CSA failed %d\n", r);
2299 if (amdgpu_sriov_vf(adev))
2300 amdgpu_virt_init_data_exchange(adev);
2302 r = amdgpu_ib_pool_init(adev);
2304 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
2305 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
2309 r = amdgpu_ucode_create_bo(adev); /* create ucode bo when sw_init complete*/
2313 r = amdgpu_device_ip_hw_init_phase1(adev);
2317 r = amdgpu_device_fw_loading(adev);
2321 r = amdgpu_device_ip_hw_init_phase2(adev);
2326 * retired pages will be loaded from eeprom and reserved here,
2327 * it should be called after amdgpu_device_ip_hw_init_phase2 since
2328 * for some ASICs the RAS EEPROM code relies on SMU fully functioning
2329 * for I2C communication which only true at this point.
2331 * amdgpu_ras_recovery_init may fail, but the upper only cares the
2332 * failure from bad gpu situation and stop amdgpu init process
2333 * accordingly. For other failed cases, it will still release all
2334 * the resource and print error message, rather than returning one
2335 * negative value to upper level.
2337 * Note: theoretically, this should be called before all vram allocations
2338 * to protect retired page from abusing
2340 r = amdgpu_ras_recovery_init(adev);
2344 if (adev->gmc.xgmi.num_physical_nodes > 1)
2345 amdgpu_xgmi_add_device(adev);
2347 /* Don't init kfd if whole hive need to be reset during init */
2348 if (!adev->gmc.xgmi.pending_reset)
2349 amdgpu_amdkfd_device_init(adev);
2351 amdgpu_fru_get_product_info(adev);
2354 if (amdgpu_sriov_vf(adev))
2355 amdgpu_virt_release_full_gpu(adev, true);
2361 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
2363 * @adev: amdgpu_device pointer
2365 * Writes a reset magic value to the gart pointer in VRAM. The driver calls
2366 * this function before a GPU reset. If the value is retained after a
2367 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
2369 static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
2371 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
2375 * amdgpu_device_check_vram_lost - check if vram is valid
2377 * @adev: amdgpu_device pointer
2379 * Checks the reset magic value written to the gart pointer in VRAM.
2380 * The driver calls this after a GPU reset to see if the contents of
2381 * VRAM is lost or now.
2382 * returns true if vram is lost, false if not.
2384 static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
2386 if (memcmp(adev->gart.ptr, adev->reset_magic,
2387 AMDGPU_RESET_MAGIC_NUM))
2390 if (!amdgpu_in_reset(adev))
2394 * For all ASICs with baco/mode1 reset, the VRAM is
2395 * always assumed to be lost.
2397 switch (amdgpu_asic_reset_method(adev)) {
2398 case AMD_RESET_METHOD_BACO:
2399 case AMD_RESET_METHOD_MODE1:
2407 * amdgpu_device_set_cg_state - set clockgating for amdgpu device
2409 * @adev: amdgpu_device pointer
2410 * @state: clockgating state (gate or ungate)
2412 * The list of all the hardware IPs that make up the asic is walked and the
2413 * set_clockgating_state callbacks are run.
2414 * Late initialization pass enabling clockgating for hardware IPs.
2415 * Fini or suspend, pass disabling clockgating for hardware IPs.
2416 * Returns 0 on success, negative error code on failure.
2419 int amdgpu_device_set_cg_state(struct amdgpu_device *adev,
2420 enum amd_clockgating_state state)
2424 if (amdgpu_emu_mode == 1)
2427 for (j = 0; j < adev->num_ip_blocks; j++) {
2428 i = state == AMD_CG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
2429 if (!adev->ip_blocks[i].status.late_initialized)
2431 /* skip CG for GFX on S0ix */
2432 if (adev->in_s0ix &&
2433 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX)
2435 /* skip CG for VCE/UVD, it's handled specially */
2436 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
2437 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
2438 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
2439 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
2440 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
2441 /* enable clockgating to save power */
2442 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
2445 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
2446 adev->ip_blocks[i].version->funcs->name, r);
2455 int amdgpu_device_set_pg_state(struct amdgpu_device *adev,
2456 enum amd_powergating_state state)
2460 if (amdgpu_emu_mode == 1)
2463 for (j = 0; j < adev->num_ip_blocks; j++) {
2464 i = state == AMD_PG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
2465 if (!adev->ip_blocks[i].status.late_initialized)
2467 /* skip PG for GFX on S0ix */
2468 if (adev->in_s0ix &&
2469 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX)
2471 /* skip CG for VCE/UVD, it's handled specially */
2472 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
2473 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
2474 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
2475 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
2476 adev->ip_blocks[i].version->funcs->set_powergating_state) {
2477 /* enable powergating to save power */
2478 r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
2481 DRM_ERROR("set_powergating_state(gate) of IP block <%s> failed %d\n",
2482 adev->ip_blocks[i].version->funcs->name, r);
2490 static int amdgpu_device_enable_mgpu_fan_boost(void)
2492 struct amdgpu_gpu_instance *gpu_ins;
2493 struct amdgpu_device *adev;
2496 mutex_lock(&mgpu_info.mutex);
2499 * MGPU fan boost feature should be enabled
2500 * only when there are two or more dGPUs in
2503 if (mgpu_info.num_dgpu < 2)
2506 for (i = 0; i < mgpu_info.num_dgpu; i++) {
2507 gpu_ins = &(mgpu_info.gpu_ins[i]);
2508 adev = gpu_ins->adev;
2509 if (!(adev->flags & AMD_IS_APU) &&
2510 !gpu_ins->mgpu_fan_enabled) {
2511 ret = amdgpu_dpm_enable_mgpu_fan_boost(adev);
2515 gpu_ins->mgpu_fan_enabled = 1;
2520 mutex_unlock(&mgpu_info.mutex);
2526 * amdgpu_device_ip_late_init - run late init for hardware IPs
2528 * @adev: amdgpu_device pointer
2530 * Late initialization pass for hardware IPs. The list of all the hardware
2531 * IPs that make up the asic is walked and the late_init callbacks are run.
2532 * late_init covers any special initialization that an IP requires
2533 * after all of the have been initialized or something that needs to happen
2534 * late in the init process.
2535 * Returns 0 on success, negative error code on failure.
2537 static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
2539 struct amdgpu_gpu_instance *gpu_instance;
2542 for (i = 0; i < adev->num_ip_blocks; i++) {
2543 if (!adev->ip_blocks[i].status.hw)
2545 if (adev->ip_blocks[i].version->funcs->late_init) {
2546 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
2548 DRM_ERROR("late_init of IP block <%s> failed %d\n",
2549 adev->ip_blocks[i].version->funcs->name, r);
2553 adev->ip_blocks[i].status.late_initialized = true;
2556 amdgpu_ras_set_error_query_ready(adev, true);
2558 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE);
2559 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_GATE);
2561 amdgpu_device_fill_reset_magic(adev);
2563 r = amdgpu_device_enable_mgpu_fan_boost();
2565 DRM_ERROR("enable mgpu fan boost failed (%d).\n", r);
2567 /* For XGMI + passthrough configuration on arcturus, enable light SBR */
2568 if (adev->asic_type == CHIP_ARCTURUS &&
2569 amdgpu_passthrough(adev) &&
2570 adev->gmc.xgmi.num_physical_nodes > 1)
2571 smu_set_light_sbr(&adev->smu, true);
2573 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2574 mutex_lock(&mgpu_info.mutex);
2577 * Reset device p-state to low as this was booted with high.
2579 * This should be performed only after all devices from the same
2580 * hive get initialized.
2582 * However, it's unknown how many device in the hive in advance.
2583 * As this is counted one by one during devices initializations.
2585 * So, we wait for all XGMI interlinked devices initialized.
2586 * This may bring some delays as those devices may come from
2587 * different hives. But that should be OK.
2589 if (mgpu_info.num_dgpu == adev->gmc.xgmi.num_physical_nodes) {
2590 for (i = 0; i < mgpu_info.num_gpu; i++) {
2591 gpu_instance = &(mgpu_info.gpu_ins[i]);
2592 if (gpu_instance->adev->flags & AMD_IS_APU)
2595 r = amdgpu_xgmi_set_pstate(gpu_instance->adev,
2596 AMDGPU_XGMI_PSTATE_MIN);
2598 DRM_ERROR("pstate setting failed (%d).\n", r);
2604 mutex_unlock(&mgpu_info.mutex);
2610 static int amdgpu_device_ip_fini_early(struct amdgpu_device *adev)
2614 for (i = 0; i < adev->num_ip_blocks; i++) {
2615 if (!adev->ip_blocks[i].version->funcs->early_fini)
2618 r = adev->ip_blocks[i].version->funcs->early_fini((void *)adev);
2620 DRM_DEBUG("early_fini of IP block <%s> failed %d\n",
2621 adev->ip_blocks[i].version->funcs->name, r);
2625 amdgpu_amdkfd_suspend(adev, false);
2627 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2628 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2630 /* need to disable SMC first */
2631 for (i = 0; i < adev->num_ip_blocks; i++) {
2632 if (!adev->ip_blocks[i].status.hw)
2634 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
2635 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2636 /* XXX handle errors */
2638 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2639 adev->ip_blocks[i].version->funcs->name, r);
2641 adev->ip_blocks[i].status.hw = false;
2646 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2647 if (!adev->ip_blocks[i].status.hw)
2650 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2651 /* XXX handle errors */
2653 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2654 adev->ip_blocks[i].version->funcs->name, r);
2657 adev->ip_blocks[i].status.hw = false;
2664 * amdgpu_device_ip_fini - run fini for hardware IPs
2666 * @adev: amdgpu_device pointer
2668 * Main teardown pass for hardware IPs. The list of all the hardware
2669 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
2670 * are run. hw_fini tears down the hardware associated with each IP
2671 * and sw_fini tears down any software state associated with each IP.
2672 * Returns 0 on success, negative error code on failure.
2674 static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
2678 if (amdgpu_sriov_vf(adev) && adev->virt.ras_init_done)
2679 amdgpu_virt_release_ras_err_handler_data(adev);
2681 amdgpu_ras_pre_fini(adev);
2683 if (adev->gmc.xgmi.num_physical_nodes > 1)
2684 amdgpu_xgmi_remove_device(adev);
2686 amdgpu_amdkfd_device_fini_sw(adev);
2688 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2689 if (!adev->ip_blocks[i].status.sw)
2692 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
2693 amdgpu_ucode_free_bo(adev);
2694 amdgpu_free_static_csa(&adev->virt.csa_obj);
2695 amdgpu_device_wb_fini(adev);
2696 amdgpu_device_vram_scratch_fini(adev);
2697 amdgpu_ib_pool_fini(adev);
2700 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
2701 /* XXX handle errors */
2703 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
2704 adev->ip_blocks[i].version->funcs->name, r);
2706 adev->ip_blocks[i].status.sw = false;
2707 adev->ip_blocks[i].status.valid = false;
2710 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2711 if (!adev->ip_blocks[i].status.late_initialized)
2713 if (adev->ip_blocks[i].version->funcs->late_fini)
2714 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
2715 adev->ip_blocks[i].status.late_initialized = false;
2718 amdgpu_ras_fini(adev);
2720 if (amdgpu_sriov_vf(adev))
2721 if (amdgpu_virt_release_full_gpu(adev, false))
2722 DRM_ERROR("failed to release exclusive mode on fini\n");
2728 * amdgpu_device_delayed_init_work_handler - work handler for IB tests
2730 * @work: work_struct.
2732 static void amdgpu_device_delayed_init_work_handler(struct work_struct *work)
2734 struct amdgpu_device *adev =
2735 container_of(work, struct amdgpu_device, delayed_init_work.work);
2738 r = amdgpu_ib_ring_tests(adev);
2740 DRM_ERROR("ib ring test failed (%d).\n", r);
2743 static void amdgpu_device_delay_enable_gfx_off(struct work_struct *work)
2745 struct amdgpu_device *adev =
2746 container_of(work, struct amdgpu_device, gfx.gfx_off_delay_work.work);
2748 mutex_lock(&adev->gfx.gfx_off_mutex);
2749 if (!adev->gfx.gfx_off_state && !adev->gfx.gfx_off_req_count) {
2750 if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true))
2751 adev->gfx.gfx_off_state = true;
2753 mutex_unlock(&adev->gfx.gfx_off_mutex);
2757 * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1)
2759 * @adev: amdgpu_device pointer
2761 * Main suspend function for hardware IPs. The list of all the hardware
2762 * IPs that make up the asic is walked, clockgating is disabled and the
2763 * suspend callbacks are run. suspend puts the hardware and software state
2764 * in each IP into a state suitable for suspend.
2765 * Returns 0 on success, negative error code on failure.
2767 static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev)
2771 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2772 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2774 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2775 if (!adev->ip_blocks[i].status.valid)
2778 /* displays are handled separately */
2779 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_DCE)
2782 /* XXX handle errors */
2783 r = adev->ip_blocks[i].version->funcs->suspend(adev);
2784 /* XXX handle errors */
2786 DRM_ERROR("suspend of IP block <%s> failed %d\n",
2787 adev->ip_blocks[i].version->funcs->name, r);
2791 adev->ip_blocks[i].status.hw = false;
2798 * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2)
2800 * @adev: amdgpu_device pointer
2802 * Main suspend function for hardware IPs. The list of all the hardware
2803 * IPs that make up the asic is walked, clockgating is disabled and the
2804 * suspend callbacks are run. suspend puts the hardware and software state
2805 * in each IP into a state suitable for suspend.
2806 * Returns 0 on success, negative error code on failure.
2808 static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev)
2813 amdgpu_gfx_state_change_set(adev, sGpuChangeState_D3Entry);
2815 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2816 if (!adev->ip_blocks[i].status.valid)
2818 /* displays are handled in phase1 */
2819 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)
2821 /* PSP lost connection when err_event_athub occurs */
2822 if (amdgpu_ras_intr_triggered() &&
2823 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
2824 adev->ip_blocks[i].status.hw = false;
2828 /* skip unnecessary suspend if we do not initialize them yet */
2829 if (adev->gmc.xgmi.pending_reset &&
2830 !(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2831 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC ||
2832 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2833 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)) {
2834 adev->ip_blocks[i].status.hw = false;
2838 /* skip suspend of gfx and psp for S0ix
2839 * gfx is in gfxoff state, so on resume it will exit gfxoff just
2840 * like at runtime. PSP is also part of the always on hardware
2841 * so no need to suspend it.
2843 if (adev->in_s0ix &&
2844 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP ||
2845 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX))
2848 /* XXX handle errors */
2849 r = adev->ip_blocks[i].version->funcs->suspend(adev);
2850 /* XXX handle errors */
2852 DRM_ERROR("suspend of IP block <%s> failed %d\n",
2853 adev->ip_blocks[i].version->funcs->name, r);
2855 adev->ip_blocks[i].status.hw = false;
2856 /* handle putting the SMC in the appropriate state */
2857 if(!amdgpu_sriov_vf(adev)){
2858 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
2859 r = amdgpu_dpm_set_mp1_state(adev, adev->mp1_state);
2861 DRM_ERROR("SMC failed to set mp1 state %d, %d\n",
2862 adev->mp1_state, r);
2873 * amdgpu_device_ip_suspend - run suspend for hardware IPs
2875 * @adev: amdgpu_device pointer
2877 * Main suspend function for hardware IPs. The list of all the hardware
2878 * IPs that make up the asic is walked, clockgating is disabled and the
2879 * suspend callbacks are run. suspend puts the hardware and software state
2880 * in each IP into a state suitable for suspend.
2881 * Returns 0 on success, negative error code on failure.
2883 int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
2887 if (amdgpu_sriov_vf(adev)) {
2888 amdgpu_virt_fini_data_exchange(adev);
2889 amdgpu_virt_request_full_gpu(adev, false);
2892 r = amdgpu_device_ip_suspend_phase1(adev);
2895 r = amdgpu_device_ip_suspend_phase2(adev);
2897 if (amdgpu_sriov_vf(adev))
2898 amdgpu_virt_release_full_gpu(adev, false);
2903 static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
2907 static enum amd_ip_block_type ip_order[] = {
2908 AMD_IP_BLOCK_TYPE_GMC,
2909 AMD_IP_BLOCK_TYPE_COMMON,
2910 AMD_IP_BLOCK_TYPE_PSP,
2911 AMD_IP_BLOCK_TYPE_IH,
2914 for (i = 0; i < adev->num_ip_blocks; i++) {
2916 struct amdgpu_ip_block *block;
2918 block = &adev->ip_blocks[i];
2919 block->status.hw = false;
2921 for (j = 0; j < ARRAY_SIZE(ip_order); j++) {
2923 if (block->version->type != ip_order[j] ||
2924 !block->status.valid)
2927 r = block->version->funcs->hw_init(adev);
2928 DRM_INFO("RE-INIT-early: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
2931 block->status.hw = true;
2938 static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
2942 static enum amd_ip_block_type ip_order[] = {
2943 AMD_IP_BLOCK_TYPE_SMC,
2944 AMD_IP_BLOCK_TYPE_DCE,
2945 AMD_IP_BLOCK_TYPE_GFX,
2946 AMD_IP_BLOCK_TYPE_SDMA,
2947 AMD_IP_BLOCK_TYPE_UVD,
2948 AMD_IP_BLOCK_TYPE_VCE,
2949 AMD_IP_BLOCK_TYPE_VCN
2952 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
2954 struct amdgpu_ip_block *block;
2956 for (j = 0; j < adev->num_ip_blocks; j++) {
2957 block = &adev->ip_blocks[j];
2959 if (block->version->type != ip_order[i] ||
2960 !block->status.valid ||
2964 if (block->version->type == AMD_IP_BLOCK_TYPE_SMC)
2965 r = block->version->funcs->resume(adev);
2967 r = block->version->funcs->hw_init(adev);
2969 DRM_INFO("RE-INIT-late: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
2972 block->status.hw = true;
2980 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
2982 * @adev: amdgpu_device pointer
2984 * First resume function for hardware IPs. The list of all the hardware
2985 * IPs that make up the asic is walked and the resume callbacks are run for
2986 * COMMON, GMC, and IH. resume puts the hardware into a functional state
2987 * after a suspend and updates the software state as necessary. This
2988 * function is also used for restoring the GPU after a GPU reset.
2989 * Returns 0 on success, negative error code on failure.
2991 static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
2995 for (i = 0; i < adev->num_ip_blocks; i++) {
2996 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
2998 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2999 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3000 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
3002 r = adev->ip_blocks[i].version->funcs->resume(adev);
3004 DRM_ERROR("resume of IP block <%s> failed %d\n",
3005 adev->ip_blocks[i].version->funcs->name, r);
3008 adev->ip_blocks[i].status.hw = true;
3016 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
3018 * @adev: amdgpu_device pointer
3020 * First resume function for hardware IPs. The list of all the hardware
3021 * IPs that make up the asic is walked and the resume callbacks are run for
3022 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
3023 * functional state after a suspend and updates the software state as
3024 * necessary. This function is also used for restoring the GPU after a GPU
3026 * Returns 0 on success, negative error code on failure.
3028 static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
3032 for (i = 0; i < adev->num_ip_blocks; i++) {
3033 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
3035 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3036 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3037 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3038 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)
3040 r = adev->ip_blocks[i].version->funcs->resume(adev);
3042 DRM_ERROR("resume of IP block <%s> failed %d\n",
3043 adev->ip_blocks[i].version->funcs->name, r);
3046 adev->ip_blocks[i].status.hw = true;
3053 * amdgpu_device_ip_resume - run resume for hardware IPs
3055 * @adev: amdgpu_device pointer
3057 * Main resume function for hardware IPs. The hardware IPs
3058 * are split into two resume functions because they are
3059 * are also used in in recovering from a GPU reset and some additional
3060 * steps need to be take between them. In this case (S3/S4) they are
3062 * Returns 0 on success, negative error code on failure.
3064 static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
3068 r = amdgpu_device_ip_resume_phase1(adev);
3072 r = amdgpu_device_fw_loading(adev);
3076 r = amdgpu_device_ip_resume_phase2(adev);
3082 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
3084 * @adev: amdgpu_device pointer
3086 * Query the VBIOS data tables to determine if the board supports SR-IOV.
3088 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
3090 if (amdgpu_sriov_vf(adev)) {
3091 if (adev->is_atom_fw) {
3092 if (amdgpu_atomfirmware_gpu_virtualization_supported(adev))
3093 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
3095 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
3096 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
3099 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
3100 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
3105 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
3107 * @asic_type: AMD asic type
3109 * Check if there is DC (new modesetting infrastructre) support for an asic.
3110 * returns true if DC has support, false if not.
3112 bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
3114 switch (asic_type) {
3115 #if defined(CONFIG_DRM_AMD_DC)
3116 #if defined(CONFIG_DRM_AMD_DC_SI)
3127 * We have systems in the wild with these ASICs that require
3128 * LVDS and VGA support which is not supported with DC.
3130 * Fallback to the non-DC driver here by default so as not to
3131 * cause regressions.
3133 return amdgpu_dc > 0;
3137 case CHIP_POLARIS10:
3138 case CHIP_POLARIS11:
3139 case CHIP_POLARIS12:
3146 #if defined(CONFIG_DRM_AMD_DC_DCN)
3152 case CHIP_SIENNA_CICHLID:
3153 case CHIP_NAVY_FLOUNDER:
3154 case CHIP_DIMGREY_CAVEFISH:
3155 case CHIP_BEIGE_GOBY:
3157 case CHIP_YELLOW_CARP:
3159 return amdgpu_dc != 0;
3163 DRM_INFO_ONCE("Display Core has been requested via kernel parameter "
3164 "but isn't supported by ASIC, ignoring\n");
3170 * amdgpu_device_has_dc_support - check if dc is supported
3172 * @adev: amdgpu_device pointer
3174 * Returns true for supported, false for not supported
3176 bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
3178 if (amdgpu_sriov_vf(adev) ||
3179 adev->enable_virtual_display ||
3180 (adev->harvest_ip_mask & AMD_HARVEST_IP_DMU_MASK))
3183 return amdgpu_device_asic_has_dc_support(adev->asic_type);
3186 static void amdgpu_device_xgmi_reset_func(struct work_struct *__work)
3188 struct amdgpu_device *adev =
3189 container_of(__work, struct amdgpu_device, xgmi_reset_work);
3190 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
3192 /* It's a bug to not have a hive within this function */
3197 * Use task barrier to synchronize all xgmi reset works across the
3198 * hive. task_barrier_enter and task_barrier_exit will block
3199 * until all the threads running the xgmi reset works reach
3200 * those points. task_barrier_full will do both blocks.
3202 if (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) {
3204 task_barrier_enter(&hive->tb);
3205 adev->asic_reset_res = amdgpu_device_baco_enter(adev_to_drm(adev));
3207 if (adev->asic_reset_res)
3210 task_barrier_exit(&hive->tb);
3211 adev->asic_reset_res = amdgpu_device_baco_exit(adev_to_drm(adev));
3213 if (adev->asic_reset_res)
3216 if (adev->mmhub.ras_funcs &&
3217 adev->mmhub.ras_funcs->reset_ras_error_count)
3218 adev->mmhub.ras_funcs->reset_ras_error_count(adev);
3221 task_barrier_full(&hive->tb);
3222 adev->asic_reset_res = amdgpu_asic_reset(adev);
3226 if (adev->asic_reset_res)
3227 DRM_WARN("ASIC reset failed with error, %d for drm dev, %s",
3228 adev->asic_reset_res, adev_to_drm(adev)->unique);
3229 amdgpu_put_xgmi_hive(hive);
3232 static int amdgpu_device_get_job_timeout_settings(struct amdgpu_device *adev)
3234 char *input = amdgpu_lockup_timeout;
3235 char *timeout_setting = NULL;
3241 * By default timeout for non compute jobs is 10000
3242 * and 60000 for compute jobs.
3243 * In SR-IOV or passthrough mode, timeout for compute
3244 * jobs are 60000 by default.
3246 adev->gfx_timeout = msecs_to_jiffies(10000);
3247 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
3248 if (amdgpu_sriov_vf(adev))
3249 adev->compute_timeout = amdgpu_sriov_is_pp_one_vf(adev) ?
3250 msecs_to_jiffies(60000) : msecs_to_jiffies(10000);
3252 adev->compute_timeout = msecs_to_jiffies(60000);
3254 if (strnlen(input, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
3255 while ((timeout_setting = strsep(&input, ",")) &&
3256 strnlen(timeout_setting, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
3257 ret = kstrtol(timeout_setting, 0, &timeout);
3264 } else if (timeout < 0) {
3265 timeout = MAX_SCHEDULE_TIMEOUT;
3267 timeout = msecs_to_jiffies(timeout);
3272 adev->gfx_timeout = timeout;
3275 adev->compute_timeout = timeout;
3278 adev->sdma_timeout = timeout;
3281 adev->video_timeout = timeout;
3288 * There is only one value specified and
3289 * it should apply to all non-compute jobs.
3292 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
3293 if (amdgpu_sriov_vf(adev) || amdgpu_passthrough(adev))
3294 adev->compute_timeout = adev->gfx_timeout;
3301 static const struct attribute *amdgpu_dev_attributes[] = {
3302 &dev_attr_product_name.attr,
3303 &dev_attr_product_number.attr,
3304 &dev_attr_serial_number.attr,
3305 &dev_attr_pcie_replay_count.attr,
3310 * amdgpu_device_init - initialize the driver
3312 * @adev: amdgpu_device pointer
3313 * @flags: driver flags
3315 * Initializes the driver info and hw (all asics).
3316 * Returns 0 for success or an error on failure.
3317 * Called at driver startup.
3319 int amdgpu_device_init(struct amdgpu_device *adev,
3322 struct drm_device *ddev = adev_to_drm(adev);
3323 struct pci_dev *pdev = adev->pdev;
3328 adev->shutdown = false;
3329 adev->flags = flags;
3331 if (amdgpu_force_asic_type >= 0 && amdgpu_force_asic_type < CHIP_LAST)
3332 adev->asic_type = amdgpu_force_asic_type;
3334 adev->asic_type = flags & AMD_ASIC_MASK;
3336 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
3337 if (amdgpu_emu_mode == 1)
3338 adev->usec_timeout *= 10;
3339 adev->gmc.gart_size = 512 * 1024 * 1024;
3340 adev->accel_working = false;
3341 adev->num_rings = 0;
3342 adev->mman.buffer_funcs = NULL;
3343 adev->mman.buffer_funcs_ring = NULL;
3344 adev->vm_manager.vm_pte_funcs = NULL;
3345 adev->vm_manager.vm_pte_num_scheds = 0;
3346 adev->gmc.gmc_funcs = NULL;
3347 adev->harvest_ip_mask = 0x0;
3348 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
3349 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
3351 adev->smc_rreg = &amdgpu_invalid_rreg;
3352 adev->smc_wreg = &amdgpu_invalid_wreg;
3353 adev->pcie_rreg = &amdgpu_invalid_rreg;
3354 adev->pcie_wreg = &amdgpu_invalid_wreg;
3355 adev->pciep_rreg = &amdgpu_invalid_rreg;
3356 adev->pciep_wreg = &amdgpu_invalid_wreg;
3357 adev->pcie_rreg64 = &amdgpu_invalid_rreg64;
3358 adev->pcie_wreg64 = &amdgpu_invalid_wreg64;
3359 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
3360 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
3361 adev->didt_rreg = &amdgpu_invalid_rreg;
3362 adev->didt_wreg = &amdgpu_invalid_wreg;
3363 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
3364 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
3365 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
3366 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
3368 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
3369 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
3370 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
3372 /* mutex initialization are all done here so we
3373 * can recall function without having locking issues */
3374 mutex_init(&adev->firmware.mutex);
3375 mutex_init(&adev->pm.mutex);
3376 mutex_init(&adev->gfx.gpu_clock_mutex);
3377 mutex_init(&adev->srbm_mutex);
3378 mutex_init(&adev->gfx.pipe_reserve_mutex);
3379 mutex_init(&adev->gfx.gfx_off_mutex);
3380 mutex_init(&adev->grbm_idx_mutex);
3381 mutex_init(&adev->mn_lock);
3382 mutex_init(&adev->virt.vf_errors.lock);
3383 hash_init(adev->mn_hash);
3384 atomic_set(&adev->in_gpu_reset, 0);
3385 init_rwsem(&adev->reset_sem);
3386 mutex_init(&adev->psp.mutex);
3387 mutex_init(&adev->notifier_lock);
3389 r = amdgpu_device_check_arguments(adev);
3393 spin_lock_init(&adev->mmio_idx_lock);
3394 spin_lock_init(&adev->smc_idx_lock);
3395 spin_lock_init(&adev->pcie_idx_lock);
3396 spin_lock_init(&adev->uvd_ctx_idx_lock);
3397 spin_lock_init(&adev->didt_idx_lock);
3398 spin_lock_init(&adev->gc_cac_idx_lock);
3399 spin_lock_init(&adev->se_cac_idx_lock);
3400 spin_lock_init(&adev->audio_endpt_idx_lock);
3401 spin_lock_init(&adev->mm_stats.lock);
3403 INIT_LIST_HEAD(&adev->shadow_list);
3404 mutex_init(&adev->shadow_list_lock);
3406 INIT_LIST_HEAD(&adev->reset_list);
3408 INIT_DELAYED_WORK(&adev->delayed_init_work,
3409 amdgpu_device_delayed_init_work_handler);
3410 INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work,
3411 amdgpu_device_delay_enable_gfx_off);
3413 INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
3415 adev->gfx.gfx_off_req_count = 1;
3416 adev->pm.ac_power = power_supply_is_system_supplied() > 0;
3418 atomic_set(&adev->throttling_logging_enabled, 1);
3420 * If throttling continues, logging will be performed every minute
3421 * to avoid log flooding. "-1" is subtracted since the thermal
3422 * throttling interrupt comes every second. Thus, the total logging
3423 * interval is 59 seconds(retelimited printk interval) + 1(waiting
3424 * for throttling interrupt) = 60 seconds.
3426 ratelimit_state_init(&adev->throttling_logging_rs, (60 - 1) * HZ, 1);
3427 ratelimit_set_flags(&adev->throttling_logging_rs, RATELIMIT_MSG_ON_RELEASE);
3429 /* Registers mapping */
3430 /* TODO: block userspace mapping of io register */
3431 if (adev->asic_type >= CHIP_BONAIRE) {
3432 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
3433 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
3435 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
3436 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
3439 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
3440 if (adev->rmmio == NULL) {
3443 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
3444 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
3446 /* enable PCIE atomic ops */
3447 r = pci_enable_atomic_ops_to_root(adev->pdev,
3448 PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
3449 PCI_EXP_DEVCAP2_ATOMIC_COMP64);
3451 adev->have_atomics_support = false;
3452 DRM_INFO("PCIE atomic ops is not supported\n");
3454 adev->have_atomics_support = true;
3457 amdgpu_device_get_pcie_info(adev);
3460 DRM_INFO("MCBP is enabled\n");
3462 if (amdgpu_mes && adev->asic_type >= CHIP_NAVI10)
3463 adev->enable_mes = true;
3465 /* detect hw virtualization here */
3466 amdgpu_detect_virtualization(adev);
3468 r = amdgpu_device_get_job_timeout_settings(adev);
3470 dev_err(adev->dev, "invalid lockup_timeout parameter syntax\n");
3474 /* early init functions */
3475 r = amdgpu_device_ip_early_init(adev);
3479 /* doorbell bar mapping and doorbell index init*/
3480 amdgpu_device_doorbell_init(adev);
3482 if (amdgpu_emu_mode == 1) {
3483 /* post the asic on emulation mode */
3484 emu_soc_asic_init(adev);
3485 goto fence_driver_init;
3488 amdgpu_reset_init(adev);
3490 /* detect if we are with an SRIOV vbios */
3491 amdgpu_device_detect_sriov_bios(adev);
3493 /* check if we need to reset the asic
3494 * E.g., driver was not cleanly unloaded previously, etc.
3496 if (!amdgpu_sriov_vf(adev) && amdgpu_asic_need_reset_on_init(adev)) {
3497 if (adev->gmc.xgmi.num_physical_nodes) {
3498 dev_info(adev->dev, "Pending hive reset.\n");
3499 adev->gmc.xgmi.pending_reset = true;
3500 /* Only need to init necessary block for SMU to handle the reset */
3501 for (i = 0; i < adev->num_ip_blocks; i++) {
3502 if (!adev->ip_blocks[i].status.valid)
3504 if (!(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3505 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3506 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3507 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC)) {
3508 DRM_DEBUG("IP %s disabled for hw_init.\n",
3509 adev->ip_blocks[i].version->funcs->name);
3510 adev->ip_blocks[i].status.hw = true;
3514 r = amdgpu_asic_reset(adev);
3516 dev_err(adev->dev, "asic reset on init failed\n");
3522 pci_enable_pcie_error_reporting(adev->pdev);
3524 /* Post card if necessary */
3525 if (amdgpu_device_need_post(adev)) {
3527 dev_err(adev->dev, "no vBIOS found\n");
3531 DRM_INFO("GPU posting now...\n");
3532 r = amdgpu_device_asic_init(adev);
3534 dev_err(adev->dev, "gpu post error!\n");
3539 if (adev->is_atom_fw) {
3540 /* Initialize clocks */
3541 r = amdgpu_atomfirmware_get_clock_info(adev);
3543 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
3544 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3548 /* Initialize clocks */
3549 r = amdgpu_atombios_get_clock_info(adev);
3551 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
3552 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3555 /* init i2c buses */
3556 if (!amdgpu_device_has_dc_support(adev))
3557 amdgpu_atombios_i2c_init(adev);
3562 r = amdgpu_fence_driver_init(adev);
3564 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
3565 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
3569 /* init the mode config */
3570 drm_mode_config_init(adev_to_drm(adev));
3572 r = amdgpu_device_ip_init(adev);
3574 /* failed in exclusive mode due to timeout */
3575 if (amdgpu_sriov_vf(adev) &&
3576 !amdgpu_sriov_runtime(adev) &&
3577 amdgpu_virt_mmio_blocked(adev) &&
3578 !amdgpu_virt_wait_reset(adev)) {
3579 dev_err(adev->dev, "VF exclusive mode timeout\n");
3580 /* Don't send request since VF is inactive. */
3581 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
3582 adev->virt.ops = NULL;
3584 goto release_ras_con;
3586 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
3587 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
3588 goto release_ras_con;
3592 "SE %d, SH per SE %d, CU per SH %d, active_cu_number %d\n",
3593 adev->gfx.config.max_shader_engines,
3594 adev->gfx.config.max_sh_per_se,
3595 adev->gfx.config.max_cu_per_sh,
3596 adev->gfx.cu_info.number);
3598 adev->accel_working = true;
3600 amdgpu_vm_check_compute_bug(adev);
3602 /* Initialize the buffer migration limit. */
3603 if (amdgpu_moverate >= 0)
3604 max_MBps = amdgpu_moverate;
3606 max_MBps = 8; /* Allow 8 MB/s. */
3607 /* Get a log2 for easy divisions. */
3608 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
3610 amdgpu_fbdev_init(adev);
3612 r = amdgpu_pm_sysfs_init(adev);
3614 adev->pm_sysfs_en = false;
3615 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
3617 adev->pm_sysfs_en = true;
3619 r = amdgpu_ucode_sysfs_init(adev);
3621 adev->ucode_sysfs_en = false;
3622 DRM_ERROR("Creating firmware sysfs failed (%d).\n", r);
3624 adev->ucode_sysfs_en = true;
3626 if ((amdgpu_testing & 1)) {
3627 if (adev->accel_working)
3628 amdgpu_test_moves(adev);
3630 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
3632 if (amdgpu_benchmarking) {
3633 if (adev->accel_working)
3634 amdgpu_benchmark(adev, amdgpu_benchmarking);
3636 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
3640 * Register gpu instance before amdgpu_device_enable_mgpu_fan_boost.
3641 * Otherwise the mgpu fan boost feature will be skipped due to the
3642 * gpu instance is counted less.
3644 amdgpu_register_gpu_instance(adev);
3646 /* enable clockgating, etc. after ib tests, etc. since some blocks require
3647 * explicit gating rather than handling it automatically.
3649 if (!adev->gmc.xgmi.pending_reset) {
3650 r = amdgpu_device_ip_late_init(adev);
3652 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
3653 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
3654 goto release_ras_con;
3657 amdgpu_ras_resume(adev);
3658 queue_delayed_work(system_wq, &adev->delayed_init_work,
3659 msecs_to_jiffies(AMDGPU_RESUME_MS));
3662 if (amdgpu_sriov_vf(adev))
3663 flush_delayed_work(&adev->delayed_init_work);
3665 r = sysfs_create_files(&adev->dev->kobj, amdgpu_dev_attributes);
3667 dev_err(adev->dev, "Could not create amdgpu device attr\n");
3669 if (IS_ENABLED(CONFIG_PERF_EVENTS))
3670 r = amdgpu_pmu_init(adev);
3672 dev_err(adev->dev, "amdgpu_pmu_init failed\n");
3674 /* Have stored pci confspace at hand for restore in sudden PCI error */
3675 if (amdgpu_device_cache_pci_state(adev->pdev))
3676 pci_restore_state(pdev);
3678 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
3679 /* this will fail for cards that aren't VGA class devices, just
3681 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
3682 vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
3684 if (amdgpu_device_supports_px(ddev)) {
3686 vga_switcheroo_register_client(adev->pdev,
3687 &amdgpu_switcheroo_ops, px);
3688 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
3691 if (adev->gmc.xgmi.pending_reset)
3692 queue_delayed_work(system_wq, &mgpu_info.delayed_reset_work,
3693 msecs_to_jiffies(AMDGPU_RESUME_MS));
3698 amdgpu_release_ras_context(adev);
3701 amdgpu_vf_error_trans_all(adev);
3704 iounmap(adev->rmmio);
3710 static void amdgpu_device_unmap_mmio(struct amdgpu_device *adev)
3712 /* Clear all CPU mappings pointing to this device */
3713 unmap_mapping_range(adev->ddev.anon_inode->i_mapping, 0, 0, 1);
3715 /* Unmap all mapped bars - Doorbell, registers and VRAM */
3716 amdgpu_device_doorbell_fini(adev);
3718 iounmap(adev->rmmio);
3720 if (adev->mman.aper_base_kaddr)
3721 iounmap(adev->mman.aper_base_kaddr);
3722 adev->mman.aper_base_kaddr = NULL;
3724 /* Memory manager related */
3725 if (!adev->gmc.xgmi.connected_to_cpu) {
3726 arch_phys_wc_del(adev->gmc.vram_mtrr);
3727 arch_io_free_memtype_wc(adev->gmc.aper_base, adev->gmc.aper_size);
3732 * amdgpu_device_fini - tear down the driver
3734 * @adev: amdgpu_device pointer
3736 * Tear down the driver info (all asics).
3737 * Called at driver shutdown.
3739 void amdgpu_device_fini_hw(struct amdgpu_device *adev)
3741 dev_info(adev->dev, "amdgpu: finishing device.\n");
3742 flush_delayed_work(&adev->delayed_init_work);
3743 ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
3744 adev->shutdown = true;
3746 /* make sure IB test finished before entering exclusive mode
3747 * to avoid preemption on IB test
3749 if (amdgpu_sriov_vf(adev)) {
3750 amdgpu_virt_request_full_gpu(adev, false);
3751 amdgpu_virt_fini_data_exchange(adev);
3754 /* disable all interrupts */
3755 amdgpu_irq_disable_all(adev);
3756 if (adev->mode_info.mode_config_initialized){
3757 if (!amdgpu_device_has_dc_support(adev))
3758 drm_helper_force_disable_all(adev_to_drm(adev));
3760 drm_atomic_helper_shutdown(adev_to_drm(adev));
3762 amdgpu_fence_driver_fini_hw(adev);
3764 if (adev->pm_sysfs_en)
3765 amdgpu_pm_sysfs_fini(adev);
3766 if (adev->ucode_sysfs_en)
3767 amdgpu_ucode_sysfs_fini(adev);
3768 sysfs_remove_files(&adev->dev->kobj, amdgpu_dev_attributes);
3770 amdgpu_fbdev_fini(adev);
3772 amdgpu_irq_fini_hw(adev);
3774 amdgpu_device_ip_fini_early(adev);
3776 amdgpu_gart_dummy_page_fini(adev);
3778 amdgpu_device_unmap_mmio(adev);
3781 void amdgpu_device_fini_sw(struct amdgpu_device *adev)
3783 amdgpu_device_ip_fini(adev);
3784 amdgpu_fence_driver_fini_sw(adev);
3785 release_firmware(adev->firmware.gpu_info_fw);
3786 adev->firmware.gpu_info_fw = NULL;
3787 adev->accel_working = false;
3789 amdgpu_reset_fini(adev);
3791 /* free i2c buses */
3792 if (!amdgpu_device_has_dc_support(adev))
3793 amdgpu_i2c_fini(adev);
3795 if (amdgpu_emu_mode != 1)
3796 amdgpu_atombios_fini(adev);
3800 if (amdgpu_device_supports_px(adev_to_drm(adev))) {
3801 vga_switcheroo_unregister_client(adev->pdev);
3802 vga_switcheroo_fini_domain_pm_ops(adev->dev);
3804 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
3805 vga_client_register(adev->pdev, NULL, NULL, NULL);
3807 if (IS_ENABLED(CONFIG_PERF_EVENTS))
3808 amdgpu_pmu_fini(adev);
3809 if (adev->mman.discovery_bin)
3810 amdgpu_discovery_fini(adev);
3812 kfree(adev->pci_state);
3821 * amdgpu_device_suspend - initiate device suspend
3823 * @dev: drm dev pointer
3824 * @fbcon : notify the fbdev of suspend
3826 * Puts the hw in the suspend state (all asics).
3827 * Returns 0 for success or an error on failure.
3828 * Called at driver suspend.
3830 int amdgpu_device_suspend(struct drm_device *dev, bool fbcon)
3832 struct amdgpu_device *adev = drm_to_adev(dev);
3834 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
3837 adev->in_suspend = true;
3839 if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DEV_D3))
3840 DRM_WARN("smart shift update failed\n");
3842 drm_kms_helper_poll_disable(dev);
3845 amdgpu_fbdev_set_suspend(adev, 1);
3847 cancel_delayed_work_sync(&adev->delayed_init_work);
3849 amdgpu_ras_suspend(adev);
3851 amdgpu_device_ip_suspend_phase1(adev);
3854 amdgpu_amdkfd_suspend(adev, adev->in_runpm);
3856 /* evict vram memory */
3857 amdgpu_bo_evict_vram(adev);
3859 amdgpu_fence_driver_suspend(adev);
3861 amdgpu_device_ip_suspend_phase2(adev);
3862 /* evict remaining vram memory
3863 * This second call to evict vram is to evict the gart page table
3866 amdgpu_bo_evict_vram(adev);
3872 * amdgpu_device_resume - initiate device resume
3874 * @dev: drm dev pointer
3875 * @fbcon : notify the fbdev of resume
3877 * Bring the hw back to operating state (all asics).
3878 * Returns 0 for success or an error on failure.
3879 * Called at driver resume.
3881 int amdgpu_device_resume(struct drm_device *dev, bool fbcon)
3883 struct amdgpu_device *adev = drm_to_adev(dev);
3886 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
3890 amdgpu_gfx_state_change_set(adev, sGpuChangeState_D0Entry);
3893 if (amdgpu_device_need_post(adev)) {
3894 r = amdgpu_device_asic_init(adev);
3896 dev_err(adev->dev, "amdgpu asic init failed\n");
3899 r = amdgpu_device_ip_resume(adev);
3901 dev_err(adev->dev, "amdgpu_device_ip_resume failed (%d).\n", r);
3904 amdgpu_fence_driver_resume(adev);
3907 r = amdgpu_device_ip_late_init(adev);
3911 queue_delayed_work(system_wq, &adev->delayed_init_work,
3912 msecs_to_jiffies(AMDGPU_RESUME_MS));
3914 if (!adev->in_s0ix) {
3915 r = amdgpu_amdkfd_resume(adev, adev->in_runpm);
3920 /* Make sure IB tests flushed */
3921 flush_delayed_work(&adev->delayed_init_work);
3924 amdgpu_fbdev_set_suspend(adev, 0);
3926 drm_kms_helper_poll_enable(dev);
3928 amdgpu_ras_resume(adev);
3931 * Most of the connector probing functions try to acquire runtime pm
3932 * refs to ensure that the GPU is powered on when connector polling is
3933 * performed. Since we're calling this from a runtime PM callback,
3934 * trying to acquire rpm refs will cause us to deadlock.
3936 * Since we're guaranteed to be holding the rpm lock, it's safe to
3937 * temporarily disable the rpm helpers so this doesn't deadlock us.
3940 dev->dev->power.disable_depth++;
3942 if (!amdgpu_device_has_dc_support(adev))
3943 drm_helper_hpd_irq_event(dev);
3945 drm_kms_helper_hotplug_event(dev);
3947 dev->dev->power.disable_depth--;
3949 adev->in_suspend = false;
3951 if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DEV_D0))
3952 DRM_WARN("smart shift update failed\n");
3958 * amdgpu_device_ip_check_soft_reset - did soft reset succeed
3960 * @adev: amdgpu_device pointer
3962 * The list of all the hardware IPs that make up the asic is walked and
3963 * the check_soft_reset callbacks are run. check_soft_reset determines
3964 * if the asic is still hung or not.
3965 * Returns true if any of the IPs are still in a hung state, false if not.
3967 static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
3970 bool asic_hang = false;
3972 if (amdgpu_sriov_vf(adev))
3975 if (amdgpu_asic_need_full_reset(adev))
3978 for (i = 0; i < adev->num_ip_blocks; i++) {
3979 if (!adev->ip_blocks[i].status.valid)
3981 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
3982 adev->ip_blocks[i].status.hang =
3983 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
3984 if (adev->ip_blocks[i].status.hang) {
3985 dev_info(adev->dev, "IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
3993 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
3995 * @adev: amdgpu_device pointer
3997 * The list of all the hardware IPs that make up the asic is walked and the
3998 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
3999 * handles any IP specific hardware or software state changes that are
4000 * necessary for a soft reset to succeed.
4001 * Returns 0 on success, negative error code on failure.
4003 static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
4007 for (i = 0; i < adev->num_ip_blocks; i++) {
4008 if (!adev->ip_blocks[i].status.valid)
4010 if (adev->ip_blocks[i].status.hang &&
4011 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
4012 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
4022 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
4024 * @adev: amdgpu_device pointer
4026 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
4027 * reset is necessary to recover.
4028 * Returns true if a full asic reset is required, false if not.
4030 static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
4034 if (amdgpu_asic_need_full_reset(adev))
4037 for (i = 0; i < adev->num_ip_blocks; i++) {
4038 if (!adev->ip_blocks[i].status.valid)
4040 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
4041 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
4042 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
4043 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
4044 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
4045 if (adev->ip_blocks[i].status.hang) {
4046 dev_info(adev->dev, "Some block need full reset!\n");
4055 * amdgpu_device_ip_soft_reset - do a soft reset
4057 * @adev: amdgpu_device pointer
4059 * The list of all the hardware IPs that make up the asic is walked and the
4060 * soft_reset callbacks are run if the block is hung. soft_reset handles any
4061 * IP specific hardware or software state changes that are necessary to soft
4063 * Returns 0 on success, negative error code on failure.
4065 static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
4069 for (i = 0; i < adev->num_ip_blocks; i++) {
4070 if (!adev->ip_blocks[i].status.valid)
4072 if (adev->ip_blocks[i].status.hang &&
4073 adev->ip_blocks[i].version->funcs->soft_reset) {
4074 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
4084 * amdgpu_device_ip_post_soft_reset - clean up from soft reset
4086 * @adev: amdgpu_device pointer
4088 * The list of all the hardware IPs that make up the asic is walked and the
4089 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
4090 * handles any IP specific hardware or software state changes that are
4091 * necessary after the IP has been soft reset.
4092 * Returns 0 on success, negative error code on failure.
4094 static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
4098 for (i = 0; i < adev->num_ip_blocks; i++) {
4099 if (!adev->ip_blocks[i].status.valid)
4101 if (adev->ip_blocks[i].status.hang &&
4102 adev->ip_blocks[i].version->funcs->post_soft_reset)
4103 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
4112 * amdgpu_device_recover_vram - Recover some VRAM contents
4114 * @adev: amdgpu_device pointer
4116 * Restores the contents of VRAM buffers from the shadows in GTT. Used to
4117 * restore things like GPUVM page tables after a GPU reset where
4118 * the contents of VRAM might be lost.
4121 * 0 on success, negative error code on failure.
4123 static int amdgpu_device_recover_vram(struct amdgpu_device *adev)
4125 struct dma_fence *fence = NULL, *next = NULL;
4126 struct amdgpu_bo *shadow;
4127 struct amdgpu_bo_vm *vmbo;
4130 if (amdgpu_sriov_runtime(adev))
4131 tmo = msecs_to_jiffies(8000);
4133 tmo = msecs_to_jiffies(100);
4135 dev_info(adev->dev, "recover vram bo from shadow start\n");
4136 mutex_lock(&adev->shadow_list_lock);
4137 list_for_each_entry(vmbo, &adev->shadow_list, shadow_list) {
4139 /* No need to recover an evicted BO */
4140 if (shadow->tbo.resource->mem_type != TTM_PL_TT ||
4141 shadow->tbo.resource->start == AMDGPU_BO_INVALID_OFFSET ||
4142 shadow->parent->tbo.resource->mem_type != TTM_PL_VRAM)
4145 r = amdgpu_bo_restore_shadow(shadow, &next);
4150 tmo = dma_fence_wait_timeout(fence, false, tmo);
4151 dma_fence_put(fence);
4156 } else if (tmo < 0) {
4164 mutex_unlock(&adev->shadow_list_lock);
4167 tmo = dma_fence_wait_timeout(fence, false, tmo);
4168 dma_fence_put(fence);
4170 if (r < 0 || tmo <= 0) {
4171 dev_err(adev->dev, "recover vram bo from shadow failed, r is %ld, tmo is %ld\n", r, tmo);
4175 dev_info(adev->dev, "recover vram bo from shadow done\n");
4181 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
4183 * @adev: amdgpu_device pointer
4184 * @from_hypervisor: request from hypervisor
4186 * do VF FLR and reinitialize Asic
4187 * return 0 means succeeded otherwise failed
4189 static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
4190 bool from_hypervisor)
4194 if (from_hypervisor)
4195 r = amdgpu_virt_request_full_gpu(adev, true);
4197 r = amdgpu_virt_reset_gpu(adev);
4201 amdgpu_amdkfd_pre_reset(adev);
4203 /* Resume IP prior to SMC */
4204 r = amdgpu_device_ip_reinit_early_sriov(adev);
4208 amdgpu_virt_init_data_exchange(adev);
4209 /* we need recover gart prior to run SMC/CP/SDMA resume */
4210 amdgpu_gtt_mgr_recover(ttm_manager_type(&adev->mman.bdev, TTM_PL_TT));
4212 r = amdgpu_device_fw_loading(adev);
4216 /* now we are okay to resume SMC/CP/SDMA */
4217 r = amdgpu_device_ip_reinit_late_sriov(adev);
4221 amdgpu_irq_gpu_reset_resume_helper(adev);
4222 r = amdgpu_ib_ring_tests(adev);
4223 amdgpu_amdkfd_post_reset(adev);
4226 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
4227 amdgpu_inc_vram_lost(adev);
4228 r = amdgpu_device_recover_vram(adev);
4230 amdgpu_virt_release_full_gpu(adev, true);
4236 * amdgpu_device_has_job_running - check if there is any job in mirror list
4238 * @adev: amdgpu_device pointer
4240 * check if there is any job in mirror list
4242 bool amdgpu_device_has_job_running(struct amdgpu_device *adev)
4245 struct drm_sched_job *job;
4247 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4248 struct amdgpu_ring *ring = adev->rings[i];
4250 if (!ring || !ring->sched.thread)
4253 spin_lock(&ring->sched.job_list_lock);
4254 job = list_first_entry_or_null(&ring->sched.pending_list,
4255 struct drm_sched_job, list);
4256 spin_unlock(&ring->sched.job_list_lock);
4264 * amdgpu_device_should_recover_gpu - check if we should try GPU recovery
4266 * @adev: amdgpu_device pointer
4268 * Check amdgpu_gpu_recovery and SRIOV status to see if we should try to recover
4271 bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev)
4273 if (!amdgpu_device_ip_check_soft_reset(adev)) {
4274 dev_info(adev->dev, "Timeout, but no hardware hang detected.\n");
4278 if (amdgpu_gpu_recovery == 0)
4281 if (amdgpu_sriov_vf(adev))
4284 if (amdgpu_gpu_recovery == -1) {
4285 switch (adev->asic_type) {
4291 case CHIP_POLARIS10:
4292 case CHIP_POLARIS11:
4293 case CHIP_POLARIS12:
4304 case CHIP_SIENNA_CICHLID:
4305 case CHIP_NAVY_FLOUNDER:
4306 case CHIP_DIMGREY_CAVEFISH:
4308 case CHIP_ALDEBARAN:
4318 dev_info(adev->dev, "GPU recovery disabled.\n");
4322 int amdgpu_device_mode1_reset(struct amdgpu_device *adev)
4327 amdgpu_atombios_scratch_regs_engine_hung(adev, true);
4329 dev_info(adev->dev, "GPU mode1 reset\n");
4332 pci_clear_master(adev->pdev);
4334 amdgpu_device_cache_pci_state(adev->pdev);
4336 if (amdgpu_dpm_is_mode1_reset_supported(adev)) {
4337 dev_info(adev->dev, "GPU smu mode1 reset\n");
4338 ret = amdgpu_dpm_mode1_reset(adev);
4340 dev_info(adev->dev, "GPU psp mode1 reset\n");
4341 ret = psp_gpu_reset(adev);
4345 dev_err(adev->dev, "GPU mode1 reset failed\n");
4347 amdgpu_device_load_pci_state(adev->pdev);
4349 /* wait for asic to come out of reset */
4350 for (i = 0; i < adev->usec_timeout; i++) {
4351 u32 memsize = adev->nbio.funcs->get_memsize(adev);
4353 if (memsize != 0xffffffff)
4358 amdgpu_atombios_scratch_regs_engine_hung(adev, false);
4362 int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
4363 struct amdgpu_reset_context *reset_context)
4366 struct amdgpu_job *job = NULL;
4367 bool need_full_reset =
4368 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4370 if (reset_context->reset_req_dev == adev)
4371 job = reset_context->job;
4373 /* no need to dump if device is not in good state during probe period */
4374 if (!adev->gmc.xgmi.pending_reset)
4375 amdgpu_debugfs_wait_dump(adev);
4377 if (amdgpu_sriov_vf(adev)) {
4378 /* stop the data exchange thread */
4379 amdgpu_virt_fini_data_exchange(adev);
4382 /* block all schedulers and reset given job's ring */
4383 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4384 struct amdgpu_ring *ring = adev->rings[i];
4386 if (!ring || !ring->sched.thread)
4389 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
4390 amdgpu_fence_driver_force_completion(ring);
4394 drm_sched_increase_karma(&job->base);
4396 r = amdgpu_reset_prepare_hwcontext(adev, reset_context);
4397 /* If reset handler not implemented, continue; otherwise return */
4403 /* Don't suspend on bare metal if we are not going to HW reset the ASIC */
4404 if (!amdgpu_sriov_vf(adev)) {
4406 if (!need_full_reset)
4407 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
4409 if (!need_full_reset) {
4410 amdgpu_device_ip_pre_soft_reset(adev);
4411 r = amdgpu_device_ip_soft_reset(adev);
4412 amdgpu_device_ip_post_soft_reset(adev);
4413 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
4414 dev_info(adev->dev, "soft reset failed, will fallback to full reset!\n");
4415 need_full_reset = true;
4419 if (need_full_reset)
4420 r = amdgpu_device_ip_suspend(adev);
4421 if (need_full_reset)
4422 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4424 clear_bit(AMDGPU_NEED_FULL_RESET,
4425 &reset_context->flags);
4431 int amdgpu_do_asic_reset(struct list_head *device_list_handle,
4432 struct amdgpu_reset_context *reset_context)
4434 struct amdgpu_device *tmp_adev = NULL;
4435 bool need_full_reset, skip_hw_reset, vram_lost = false;
4438 /* Try reset handler method first */
4439 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
4441 r = amdgpu_reset_perform_reset(tmp_adev, reset_context);
4442 /* If reset handler not implemented, continue; otherwise return */
4448 /* Reset handler not implemented, use the default method */
4450 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4451 skip_hw_reset = test_bit(AMDGPU_SKIP_HW_RESET, &reset_context->flags);
4454 * ASIC reset has to be done on all XGMI hive nodes ASAP
4455 * to allow proper links negotiation in FW (within 1 sec)
4457 if (!skip_hw_reset && need_full_reset) {
4458 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4459 /* For XGMI run all resets in parallel to speed up the process */
4460 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
4461 tmp_adev->gmc.xgmi.pending_reset = false;
4462 if (!queue_work(system_unbound_wq, &tmp_adev->xgmi_reset_work))
4465 r = amdgpu_asic_reset(tmp_adev);
4468 dev_err(tmp_adev->dev, "ASIC reset failed with error, %d for drm dev, %s",
4469 r, adev_to_drm(tmp_adev)->unique);
4474 /* For XGMI wait for all resets to complete before proceed */
4476 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4477 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
4478 flush_work(&tmp_adev->xgmi_reset_work);
4479 r = tmp_adev->asic_reset_res;
4487 if (!r && amdgpu_ras_intr_triggered()) {
4488 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4489 if (tmp_adev->mmhub.ras_funcs &&
4490 tmp_adev->mmhub.ras_funcs->reset_ras_error_count)
4491 tmp_adev->mmhub.ras_funcs->reset_ras_error_count(tmp_adev);
4494 amdgpu_ras_intr_cleared();
4497 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4498 if (need_full_reset) {
4500 r = amdgpu_device_asic_init(tmp_adev);
4502 dev_warn(tmp_adev->dev, "asic atom init failed!");
4504 dev_info(tmp_adev->dev, "GPU reset succeeded, trying to resume\n");
4505 r = amdgpu_device_ip_resume_phase1(tmp_adev);
4509 vram_lost = amdgpu_device_check_vram_lost(tmp_adev);
4511 DRM_INFO("VRAM is lost due to GPU reset!\n");
4512 amdgpu_inc_vram_lost(tmp_adev);
4515 r = amdgpu_gtt_mgr_recover(ttm_manager_type(&tmp_adev->mman.bdev, TTM_PL_TT));
4519 r = amdgpu_device_fw_loading(tmp_adev);
4523 r = amdgpu_device_ip_resume_phase2(tmp_adev);
4528 amdgpu_device_fill_reset_magic(tmp_adev);
4531 * Add this ASIC as tracked as reset was already
4532 * complete successfully.
4534 amdgpu_register_gpu_instance(tmp_adev);
4536 if (!reset_context->hive &&
4537 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
4538 amdgpu_xgmi_add_device(tmp_adev);
4540 r = amdgpu_device_ip_late_init(tmp_adev);
4544 amdgpu_fbdev_set_suspend(tmp_adev, 0);
4547 * The GPU enters bad state once faulty pages
4548 * by ECC has reached the threshold, and ras
4549 * recovery is scheduled next. So add one check
4550 * here to break recovery if it indeed exceeds
4551 * bad page threshold, and remind user to
4552 * retire this GPU or setting one bigger
4553 * bad_page_threshold value to fix this once
4554 * probing driver again.
4556 if (!amdgpu_ras_eeprom_check_err_threshold(tmp_adev)) {
4558 amdgpu_ras_resume(tmp_adev);
4564 /* Update PSP FW topology after reset */
4565 if (reset_context->hive &&
4566 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
4567 r = amdgpu_xgmi_update_topology(
4568 reset_context->hive, tmp_adev);
4574 amdgpu_irq_gpu_reset_resume_helper(tmp_adev);
4575 r = amdgpu_ib_ring_tests(tmp_adev);
4577 dev_err(tmp_adev->dev, "ib ring test failed (%d).\n", r);
4578 need_full_reset = true;
4585 r = amdgpu_device_recover_vram(tmp_adev);
4587 tmp_adev->asic_reset_res = r;
4591 if (need_full_reset)
4592 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4594 clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4598 static bool amdgpu_device_lock_adev(struct amdgpu_device *adev,
4599 struct amdgpu_hive_info *hive)
4601 if (atomic_cmpxchg(&adev->in_gpu_reset, 0, 1) != 0)
4605 down_write_nest_lock(&adev->reset_sem, &hive->hive_lock);
4607 down_write(&adev->reset_sem);
4610 switch (amdgpu_asic_reset_method(adev)) {
4611 case AMD_RESET_METHOD_MODE1:
4612 adev->mp1_state = PP_MP1_STATE_SHUTDOWN;
4614 case AMD_RESET_METHOD_MODE2:
4615 adev->mp1_state = PP_MP1_STATE_RESET;
4618 adev->mp1_state = PP_MP1_STATE_NONE;
4625 static void amdgpu_device_unlock_adev(struct amdgpu_device *adev)
4627 amdgpu_vf_error_trans_all(adev);
4628 adev->mp1_state = PP_MP1_STATE_NONE;
4629 atomic_set(&adev->in_gpu_reset, 0);
4630 up_write(&adev->reset_sem);
4634 * to lockup a list of amdgpu devices in a hive safely, if not a hive
4635 * with multiple nodes, it will be similar as amdgpu_device_lock_adev.
4637 * unlock won't require roll back.
4639 static int amdgpu_device_lock_hive_adev(struct amdgpu_device *adev, struct amdgpu_hive_info *hive)
4641 struct amdgpu_device *tmp_adev = NULL;
4643 if (adev->gmc.xgmi.num_physical_nodes > 1) {
4645 dev_err(adev->dev, "Hive is NULL while device has multiple xgmi nodes");
4648 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
4649 if (!amdgpu_device_lock_adev(tmp_adev, hive))
4652 } else if (!amdgpu_device_lock_adev(adev, hive))
4657 if (!list_is_first(&tmp_adev->gmc.xgmi.head, &hive->device_list)) {
4659 * if the lockup iteration break in the middle of a hive,
4660 * it may means there may has a race issue,
4661 * or a hive device locked up independently.
4662 * we may be in trouble and may not, so will try to roll back
4663 * the lock and give out a warnning.
4665 dev_warn(tmp_adev->dev, "Hive lock iteration broke in the middle. Rolling back to unlock");
4666 list_for_each_entry_continue_reverse(tmp_adev, &hive->device_list, gmc.xgmi.head) {
4667 amdgpu_device_unlock_adev(tmp_adev);
4673 static void amdgpu_device_resume_display_audio(struct amdgpu_device *adev)
4675 struct pci_dev *p = NULL;
4677 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
4678 adev->pdev->bus->number, 1);
4680 pm_runtime_enable(&(p->dev));
4681 pm_runtime_resume(&(p->dev));
4685 static int amdgpu_device_suspend_display_audio(struct amdgpu_device *adev)
4687 enum amd_reset_method reset_method;
4688 struct pci_dev *p = NULL;
4692 * For now, only BACO and mode1 reset are confirmed
4693 * to suffer the audio issue without proper suspended.
4695 reset_method = amdgpu_asic_reset_method(adev);
4696 if ((reset_method != AMD_RESET_METHOD_BACO) &&
4697 (reset_method != AMD_RESET_METHOD_MODE1))
4700 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
4701 adev->pdev->bus->number, 1);
4705 expires = pm_runtime_autosuspend_expiration(&(p->dev));
4708 * If we cannot get the audio device autosuspend delay,
4709 * a fixed 4S interval will be used. Considering 3S is
4710 * the audio controller default autosuspend delay setting.
4711 * 4S used here is guaranteed to cover that.
4713 expires = ktime_get_mono_fast_ns() + NSEC_PER_SEC * 4ULL;
4715 while (!pm_runtime_status_suspended(&(p->dev))) {
4716 if (!pm_runtime_suspend(&(p->dev)))
4719 if (expires < ktime_get_mono_fast_ns()) {
4720 dev_warn(adev->dev, "failed to suspend display audio\n");
4721 /* TODO: abort the succeeding gpu reset? */
4726 pm_runtime_disable(&(p->dev));
4731 static void amdgpu_device_recheck_guilty_jobs(
4732 struct amdgpu_device *adev, struct list_head *device_list_handle,
4733 struct amdgpu_reset_context *reset_context)
4737 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4738 struct amdgpu_ring *ring = adev->rings[i];
4740 struct drm_sched_job *s_job;
4742 if (!ring || !ring->sched.thread)
4745 s_job = list_first_entry_or_null(&ring->sched.pending_list,
4746 struct drm_sched_job, list);
4750 /* clear job's guilty and depend the folowing step to decide the real one */
4751 drm_sched_reset_karma(s_job);
4752 drm_sched_resubmit_jobs_ext(&ring->sched, 1);
4754 ret = dma_fence_wait_timeout(s_job->s_fence->parent, false, ring->sched.timeout);
4755 if (ret == 0) { /* timeout */
4756 DRM_ERROR("Found the real bad job! ring:%s, job_id:%llx\n",
4757 ring->sched.name, s_job->id);
4760 drm_sched_increase_karma(s_job);
4763 if (amdgpu_sriov_vf(adev)) {
4764 amdgpu_virt_fini_data_exchange(adev);
4765 r = amdgpu_device_reset_sriov(adev, false);
4767 adev->asic_reset_res = r;
4769 clear_bit(AMDGPU_SKIP_HW_RESET,
4770 &reset_context->flags);
4771 r = amdgpu_do_asic_reset(device_list_handle,
4773 if (r && r == -EAGAIN)
4778 * add reset counter so that the following
4779 * resubmitted job could flush vmid
4781 atomic_inc(&adev->gpu_reset_counter);
4785 /* got the hw fence, signal finished fence */
4786 atomic_dec(ring->sched.score);
4787 dma_fence_get(&s_job->s_fence->finished);
4788 dma_fence_signal(&s_job->s_fence->finished);
4789 dma_fence_put(&s_job->s_fence->finished);
4791 /* remove node from list and free the job */
4792 spin_lock(&ring->sched.job_list_lock);
4793 list_del_init(&s_job->list);
4794 spin_unlock(&ring->sched.job_list_lock);
4795 ring->sched.ops->free_job(s_job);
4800 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
4802 * @adev: amdgpu_device pointer
4803 * @job: which job trigger hang
4805 * Attempt to reset the GPU if it has hung (all asics).
4806 * Attempt to do soft-reset or full-reset and reinitialize Asic
4807 * Returns 0 for success or an error on failure.
4810 int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
4811 struct amdgpu_job *job)
4813 struct list_head device_list, *device_list_handle = NULL;
4814 bool job_signaled = false;
4815 struct amdgpu_hive_info *hive = NULL;
4816 struct amdgpu_device *tmp_adev = NULL;
4818 bool need_emergency_restart = false;
4819 bool audio_suspended = false;
4820 int tmp_vram_lost_counter;
4821 struct amdgpu_reset_context reset_context;
4823 memset(&reset_context, 0, sizeof(reset_context));
4826 * Special case: RAS triggered and full reset isn't supported
4828 need_emergency_restart = amdgpu_ras_need_emergency_restart(adev);
4831 * Flush RAM to disk so that after reboot
4832 * the user can read log and see why the system rebooted.
4834 if (need_emergency_restart && amdgpu_ras_get_context(adev)->reboot) {
4835 DRM_WARN("Emergency reboot.");
4838 emergency_restart();
4841 dev_info(adev->dev, "GPU %s begin!\n",
4842 need_emergency_restart ? "jobs stop":"reset");
4845 * Here we trylock to avoid chain of resets executing from
4846 * either trigger by jobs on different adevs in XGMI hive or jobs on
4847 * different schedulers for same device while this TO handler is running.
4848 * We always reset all schedulers for device and all devices for XGMI
4849 * hive so that should take care of them too.
4851 hive = amdgpu_get_xgmi_hive(adev);
4853 if (atomic_cmpxchg(&hive->in_reset, 0, 1) != 0) {
4854 DRM_INFO("Bailing on TDR for s_job:%llx, hive: %llx as another already in progress",
4855 job ? job->base.id : -1, hive->hive_id);
4856 amdgpu_put_xgmi_hive(hive);
4858 drm_sched_increase_karma(&job->base);
4861 mutex_lock(&hive->hive_lock);
4864 reset_context.method = AMD_RESET_METHOD_NONE;
4865 reset_context.reset_req_dev = adev;
4866 reset_context.job = job;
4867 reset_context.hive = hive;
4868 clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
4871 * lock the device before we try to operate the linked list
4872 * if didn't get the device lock, don't touch the linked list since
4873 * others may iterating it.
4875 r = amdgpu_device_lock_hive_adev(adev, hive);
4877 dev_info(adev->dev, "Bailing on TDR for s_job:%llx, as another already in progress",
4878 job ? job->base.id : -1);
4880 /* even we skipped this reset, still need to set the job to guilty */
4882 drm_sched_increase_karma(&job->base);
4887 * Build list of devices to reset.
4888 * In case we are in XGMI hive mode, resort the device list
4889 * to put adev in the 1st position.
4891 INIT_LIST_HEAD(&device_list);
4892 if (adev->gmc.xgmi.num_physical_nodes > 1) {
4893 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head)
4894 list_add_tail(&tmp_adev->reset_list, &device_list);
4895 if (!list_is_first(&adev->reset_list, &device_list))
4896 list_rotate_to_front(&adev->reset_list, &device_list);
4897 device_list_handle = &device_list;
4899 list_add_tail(&adev->reset_list, &device_list);
4900 device_list_handle = &device_list;
4903 /* block all schedulers and reset given job's ring */
4904 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4906 * Try to put the audio codec into suspend state
4907 * before gpu reset started.
4909 * Due to the power domain of the graphics device
4910 * is shared with AZ power domain. Without this,
4911 * we may change the audio hardware from behind
4912 * the audio driver's back. That will trigger
4913 * some audio codec errors.
4915 if (!amdgpu_device_suspend_display_audio(tmp_adev))
4916 audio_suspended = true;
4918 amdgpu_ras_set_error_query_ready(tmp_adev, false);
4920 cancel_delayed_work_sync(&tmp_adev->delayed_init_work);
4922 if (!amdgpu_sriov_vf(tmp_adev))
4923 amdgpu_amdkfd_pre_reset(tmp_adev);
4926 * Mark these ASICs to be reseted as untracked first
4927 * And add them back after reset completed
4929 amdgpu_unregister_gpu_instance(tmp_adev);
4931 amdgpu_fbdev_set_suspend(tmp_adev, 1);
4933 /* disable ras on ALL IPs */
4934 if (!need_emergency_restart &&
4935 amdgpu_device_ip_need_full_reset(tmp_adev))
4936 amdgpu_ras_suspend(tmp_adev);
4938 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4939 struct amdgpu_ring *ring = tmp_adev->rings[i];
4941 if (!ring || !ring->sched.thread)
4944 drm_sched_stop(&ring->sched, job ? &job->base : NULL);
4946 if (need_emergency_restart)
4947 amdgpu_job_stop_all_jobs_on_sched(&ring->sched);
4949 atomic_inc(&tmp_adev->gpu_reset_counter);
4952 if (need_emergency_restart)
4953 goto skip_sched_resume;
4956 * Must check guilty signal here since after this point all old
4957 * HW fences are force signaled.
4959 * job->base holds a reference to parent fence
4961 if (job && job->base.s_fence->parent &&
4962 dma_fence_is_signaled(job->base.s_fence->parent)) {
4963 job_signaled = true;
4964 dev_info(adev->dev, "Guilty job already signaled, skipping HW reset");
4968 retry: /* Rest of adevs pre asic reset from XGMI hive. */
4969 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4970 r = amdgpu_device_pre_asic_reset(tmp_adev, &reset_context);
4971 /*TODO Should we stop ?*/
4973 dev_err(tmp_adev->dev, "GPU pre asic reset failed with err, %d for drm dev, %s ",
4974 r, adev_to_drm(tmp_adev)->unique);
4975 tmp_adev->asic_reset_res = r;
4979 tmp_vram_lost_counter = atomic_read(&((adev)->vram_lost_counter));
4980 /* Actual ASIC resets if needed.*/
4981 /* TODO Implement XGMI hive reset logic for SRIOV */
4982 if (amdgpu_sriov_vf(adev)) {
4983 r = amdgpu_device_reset_sriov(adev, job ? false : true);
4985 adev->asic_reset_res = r;
4987 r = amdgpu_do_asic_reset(device_list_handle, &reset_context);
4988 if (r && r == -EAGAIN)
4994 /* Post ASIC reset for all devs .*/
4995 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4998 * Sometimes a later bad compute job can block a good gfx job as gfx
4999 * and compute ring share internal GC HW mutually. We add an additional
5000 * guilty jobs recheck step to find the real guilty job, it synchronously
5001 * submits and pends for the first job being signaled. If it gets timeout,
5002 * we identify it as a real guilty job.
5004 if (amdgpu_gpu_recovery == 2 &&
5005 !(tmp_vram_lost_counter < atomic_read(&adev->vram_lost_counter)))
5006 amdgpu_device_recheck_guilty_jobs(
5007 tmp_adev, device_list_handle, &reset_context);
5009 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5010 struct amdgpu_ring *ring = tmp_adev->rings[i];
5012 if (!ring || !ring->sched.thread)
5015 /* No point to resubmit jobs if we didn't HW reset*/
5016 if (!tmp_adev->asic_reset_res && !job_signaled)
5017 drm_sched_resubmit_jobs(&ring->sched);
5019 drm_sched_start(&ring->sched, !tmp_adev->asic_reset_res);
5022 if (!amdgpu_device_has_dc_support(tmp_adev) && !job_signaled) {
5023 drm_helper_resume_force_mode(adev_to_drm(tmp_adev));
5026 tmp_adev->asic_reset_res = 0;
5029 /* bad news, how to tell it to userspace ? */
5030 dev_info(tmp_adev->dev, "GPU reset(%d) failed\n", atomic_read(&tmp_adev->gpu_reset_counter));
5031 amdgpu_vf_error_put(tmp_adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
5033 dev_info(tmp_adev->dev, "GPU reset(%d) succeeded!\n", atomic_read(&tmp_adev->gpu_reset_counter));
5034 if (amdgpu_acpi_smart_shift_update(adev_to_drm(tmp_adev), AMDGPU_SS_DEV_D0))
5035 DRM_WARN("smart shift update failed\n");
5040 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5041 /* unlock kfd: SRIOV would do it separately */
5042 if (!need_emergency_restart && !amdgpu_sriov_vf(tmp_adev))
5043 amdgpu_amdkfd_post_reset(tmp_adev);
5045 /* kfd_post_reset will do nothing if kfd device is not initialized,
5046 * need to bring up kfd here if it's not be initialized before
5048 if (!adev->kfd.init_complete)
5049 amdgpu_amdkfd_device_init(adev);
5051 if (audio_suspended)
5052 amdgpu_device_resume_display_audio(tmp_adev);
5053 amdgpu_device_unlock_adev(tmp_adev);
5058 atomic_set(&hive->in_reset, 0);
5059 mutex_unlock(&hive->hive_lock);
5060 amdgpu_put_xgmi_hive(hive);
5063 if (r && r != -EAGAIN)
5064 dev_info(adev->dev, "GPU reset end with ret = %d\n", r);
5069 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
5071 * @adev: amdgpu_device pointer
5073 * Fetchs and stores in the driver the PCIE capabilities (gen speed
5074 * and lanes) of the slot the device is in. Handles APUs and
5075 * virtualized environments where PCIE config space may not be available.
5077 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
5079 struct pci_dev *pdev;
5080 enum pci_bus_speed speed_cap, platform_speed_cap;
5081 enum pcie_link_width platform_link_width;
5083 if (amdgpu_pcie_gen_cap)
5084 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
5086 if (amdgpu_pcie_lane_cap)
5087 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
5089 /* covers APUs as well */
5090 if (pci_is_root_bus(adev->pdev->bus)) {
5091 if (adev->pm.pcie_gen_mask == 0)
5092 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
5093 if (adev->pm.pcie_mlw_mask == 0)
5094 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
5098 if (adev->pm.pcie_gen_mask && adev->pm.pcie_mlw_mask)
5101 pcie_bandwidth_available(adev->pdev, NULL,
5102 &platform_speed_cap, &platform_link_width);
5104 if (adev->pm.pcie_gen_mask == 0) {
5107 speed_cap = pcie_get_speed_cap(pdev);
5108 if (speed_cap == PCI_SPEED_UNKNOWN) {
5109 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5110 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5111 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
5113 if (speed_cap == PCIE_SPEED_32_0GT)
5114 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5115 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5116 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5117 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4 |
5118 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN5);
5119 else if (speed_cap == PCIE_SPEED_16_0GT)
5120 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5121 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5122 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5123 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4);
5124 else if (speed_cap == PCIE_SPEED_8_0GT)
5125 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5126 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5127 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
5128 else if (speed_cap == PCIE_SPEED_5_0GT)
5129 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5130 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2);
5132 adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1;
5135 if (platform_speed_cap == PCI_SPEED_UNKNOWN) {
5136 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5137 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
5139 if (platform_speed_cap == PCIE_SPEED_32_0GT)
5140 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5141 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5142 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5143 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4 |
5144 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN5);
5145 else if (platform_speed_cap == PCIE_SPEED_16_0GT)
5146 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5147 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5148 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5149 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4);
5150 else if (platform_speed_cap == PCIE_SPEED_8_0GT)
5151 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5152 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5153 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3);
5154 else if (platform_speed_cap == PCIE_SPEED_5_0GT)
5155 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5156 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
5158 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
5162 if (adev->pm.pcie_mlw_mask == 0) {
5163 if (platform_link_width == PCIE_LNK_WIDTH_UNKNOWN) {
5164 adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK;
5166 switch (platform_link_width) {
5168 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
5169 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5170 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5171 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5172 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5173 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5174 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5177 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5178 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5179 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5180 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5181 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5182 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5185 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5186 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5187 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5188 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5189 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5192 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5193 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5194 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5195 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5198 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5199 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5200 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5203 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5204 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5207 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
5216 int amdgpu_device_baco_enter(struct drm_device *dev)
5218 struct amdgpu_device *adev = drm_to_adev(dev);
5219 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
5221 if (!amdgpu_device_supports_baco(adev_to_drm(adev)))
5224 if (ras && adev->ras_enabled &&
5225 adev->nbio.funcs->enable_doorbell_interrupt)
5226 adev->nbio.funcs->enable_doorbell_interrupt(adev, false);
5228 return amdgpu_dpm_baco_enter(adev);
5231 int amdgpu_device_baco_exit(struct drm_device *dev)
5233 struct amdgpu_device *adev = drm_to_adev(dev);
5234 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
5237 if (!amdgpu_device_supports_baco(adev_to_drm(adev)))
5240 ret = amdgpu_dpm_baco_exit(adev);
5244 if (ras && adev->ras_enabled &&
5245 adev->nbio.funcs->enable_doorbell_interrupt)
5246 adev->nbio.funcs->enable_doorbell_interrupt(adev, true);
5251 static void amdgpu_cancel_all_tdr(struct amdgpu_device *adev)
5255 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5256 struct amdgpu_ring *ring = adev->rings[i];
5258 if (!ring || !ring->sched.thread)
5261 cancel_delayed_work_sync(&ring->sched.work_tdr);
5266 * amdgpu_pci_error_detected - Called when a PCI error is detected.
5267 * @pdev: PCI device struct
5268 * @state: PCI channel state
5270 * Description: Called when a PCI error is detected.
5272 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT.
5274 pci_ers_result_t amdgpu_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
5276 struct drm_device *dev = pci_get_drvdata(pdev);
5277 struct amdgpu_device *adev = drm_to_adev(dev);
5280 DRM_INFO("PCI error: detected callback, state(%d)!!\n", state);
5282 if (adev->gmc.xgmi.num_physical_nodes > 1) {
5283 DRM_WARN("No support for XGMI hive yet...");
5284 return PCI_ERS_RESULT_DISCONNECT;
5288 case pci_channel_io_normal:
5289 return PCI_ERS_RESULT_CAN_RECOVER;
5290 /* Fatal error, prepare for slot reset */
5291 case pci_channel_io_frozen:
5293 * Cancel and wait for all TDRs in progress if failing to
5294 * set adev->in_gpu_reset in amdgpu_device_lock_adev
5296 * Locking adev->reset_sem will prevent any external access
5297 * to GPU during PCI error recovery
5299 while (!amdgpu_device_lock_adev(adev, NULL))
5300 amdgpu_cancel_all_tdr(adev);
5303 * Block any work scheduling as we do for regular GPU reset
5304 * for the duration of the recovery
5306 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5307 struct amdgpu_ring *ring = adev->rings[i];
5309 if (!ring || !ring->sched.thread)
5312 drm_sched_stop(&ring->sched, NULL);
5314 atomic_inc(&adev->gpu_reset_counter);
5315 return PCI_ERS_RESULT_NEED_RESET;
5316 case pci_channel_io_perm_failure:
5317 /* Permanent error, prepare for device removal */
5318 return PCI_ERS_RESULT_DISCONNECT;
5321 return PCI_ERS_RESULT_NEED_RESET;
5325 * amdgpu_pci_mmio_enabled - Enable MMIO and dump debug registers
5326 * @pdev: pointer to PCI device
5328 pci_ers_result_t amdgpu_pci_mmio_enabled(struct pci_dev *pdev)
5331 DRM_INFO("PCI error: mmio enabled callback!!\n");
5333 /* TODO - dump whatever for debugging purposes */
5335 /* This called only if amdgpu_pci_error_detected returns
5336 * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still
5337 * works, no need to reset slot.
5340 return PCI_ERS_RESULT_RECOVERED;
5344 * amdgpu_pci_slot_reset - Called when PCI slot has been reset.
5345 * @pdev: PCI device struct
5347 * Description: This routine is called by the pci error recovery
5348 * code after the PCI slot has been reset, just before we
5349 * should resume normal operations.
5351 pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev)
5353 struct drm_device *dev = pci_get_drvdata(pdev);
5354 struct amdgpu_device *adev = drm_to_adev(dev);
5356 struct amdgpu_reset_context reset_context;
5358 struct list_head device_list;
5360 DRM_INFO("PCI error: slot reset callback!!\n");
5362 memset(&reset_context, 0, sizeof(reset_context));
5364 INIT_LIST_HEAD(&device_list);
5365 list_add_tail(&adev->reset_list, &device_list);
5367 /* wait for asic to come out of reset */
5370 /* Restore PCI confspace */
5371 amdgpu_device_load_pci_state(pdev);
5373 /* confirm ASIC came out of reset */
5374 for (i = 0; i < adev->usec_timeout; i++) {
5375 memsize = amdgpu_asic_get_config_memsize(adev);
5377 if (memsize != 0xffffffff)
5381 if (memsize == 0xffffffff) {
5386 reset_context.method = AMD_RESET_METHOD_NONE;
5387 reset_context.reset_req_dev = adev;
5388 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
5389 set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
5391 adev->no_hw_access = true;
5392 r = amdgpu_device_pre_asic_reset(adev, &reset_context);
5393 adev->no_hw_access = false;
5397 r = amdgpu_do_asic_reset(&device_list, &reset_context);
5401 if (amdgpu_device_cache_pci_state(adev->pdev))
5402 pci_restore_state(adev->pdev);
5404 DRM_INFO("PCIe error recovery succeeded\n");
5406 DRM_ERROR("PCIe error recovery failed, err:%d", r);
5407 amdgpu_device_unlock_adev(adev);
5410 return r ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
5414 * amdgpu_pci_resume() - resume normal ops after PCI reset
5415 * @pdev: pointer to PCI device
5417 * Called when the error recovery driver tells us that its
5418 * OK to resume normal operation.
5420 void amdgpu_pci_resume(struct pci_dev *pdev)
5422 struct drm_device *dev = pci_get_drvdata(pdev);
5423 struct amdgpu_device *adev = drm_to_adev(dev);
5427 DRM_INFO("PCI error: resume callback!!\n");
5429 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5430 struct amdgpu_ring *ring = adev->rings[i];
5432 if (!ring || !ring->sched.thread)
5436 drm_sched_resubmit_jobs(&ring->sched);
5437 drm_sched_start(&ring->sched, true);
5440 amdgpu_device_unlock_adev(adev);
5443 bool amdgpu_device_cache_pci_state(struct pci_dev *pdev)
5445 struct drm_device *dev = pci_get_drvdata(pdev);
5446 struct amdgpu_device *adev = drm_to_adev(dev);
5449 r = pci_save_state(pdev);
5451 kfree(adev->pci_state);
5453 adev->pci_state = pci_store_saved_state(pdev);
5455 if (!adev->pci_state) {
5456 DRM_ERROR("Failed to store PCI saved state");
5460 DRM_WARN("Failed to save PCI state, err:%d\n", r);
5467 bool amdgpu_device_load_pci_state(struct pci_dev *pdev)
5469 struct drm_device *dev = pci_get_drvdata(pdev);
5470 struct amdgpu_device *adev = drm_to_adev(dev);
5473 if (!adev->pci_state)
5476 r = pci_load_saved_state(pdev, adev->pci_state);
5479 pci_restore_state(pdev);
5481 DRM_WARN("Failed to load PCI state, err:%d\n", r);
5488 void amdgpu_device_flush_hdp(struct amdgpu_device *adev,
5489 struct amdgpu_ring *ring)
5491 #ifdef CONFIG_X86_64
5492 if (adev->flags & AMD_IS_APU)
5495 if (adev->gmc.xgmi.connected_to_cpu)
5498 if (ring && ring->funcs->emit_hdp_flush)
5499 amdgpu_ring_emit_hdp_flush(ring);
5501 amdgpu_asic_flush_hdp(adev, ring);
5504 void amdgpu_device_invalidate_hdp(struct amdgpu_device *adev,
5505 struct amdgpu_ring *ring)
5507 #ifdef CONFIG_X86_64
5508 if (adev->flags & AMD_IS_APU)
5511 if (adev->gmc.xgmi.connected_to_cpu)
5514 amdgpu_asic_invalidate_hdp(adev, ring);