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;
1372 static int amdgpu_device_init_apu_flags(struct amdgpu_device *adev)
1374 if (!(adev->flags & AMD_IS_APU) ||
1375 adev->asic_type < CHIP_RAVEN)
1378 switch (adev->asic_type) {
1380 if (adev->pdev->device == 0x15dd)
1381 adev->apu_flags |= AMD_APU_IS_RAVEN;
1382 if (adev->pdev->device == 0x15d8)
1383 adev->apu_flags |= AMD_APU_IS_PICASSO;
1386 if ((adev->pdev->device == 0x1636) ||
1387 (adev->pdev->device == 0x164c))
1388 adev->apu_flags |= AMD_APU_IS_RENOIR;
1390 adev->apu_flags |= AMD_APU_IS_GREEN_SARDINE;
1393 adev->apu_flags |= AMD_APU_IS_VANGOGH;
1395 case CHIP_YELLOW_CARP:
1405 * amdgpu_device_check_arguments - validate module params
1407 * @adev: amdgpu_device pointer
1409 * Validates certain module parameters and updates
1410 * the associated values used by the driver (all asics).
1412 static int amdgpu_device_check_arguments(struct amdgpu_device *adev)
1414 if (amdgpu_sched_jobs < 4) {
1415 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1417 amdgpu_sched_jobs = 4;
1418 } else if (!is_power_of_2(amdgpu_sched_jobs)){
1419 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1421 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1424 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
1425 /* gart size must be greater or equal to 32M */
1426 dev_warn(adev->dev, "gart size (%d) too small\n",
1428 amdgpu_gart_size = -1;
1431 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
1432 /* gtt size must be greater or equal to 32M */
1433 dev_warn(adev->dev, "gtt size (%d) too small\n",
1435 amdgpu_gtt_size = -1;
1438 /* valid range is between 4 and 9 inclusive */
1439 if (amdgpu_vm_fragment_size != -1 &&
1440 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1441 dev_warn(adev->dev, "valid range is between 4 and 9\n");
1442 amdgpu_vm_fragment_size = -1;
1445 if (amdgpu_sched_hw_submission < 2) {
1446 dev_warn(adev->dev, "sched hw submission jobs (%d) must be at least 2\n",
1447 amdgpu_sched_hw_submission);
1448 amdgpu_sched_hw_submission = 2;
1449 } else if (!is_power_of_2(amdgpu_sched_hw_submission)) {
1450 dev_warn(adev->dev, "sched hw submission jobs (%d) must be a power of 2\n",
1451 amdgpu_sched_hw_submission);
1452 amdgpu_sched_hw_submission = roundup_pow_of_two(amdgpu_sched_hw_submission);
1455 amdgpu_device_check_smu_prv_buffer_size(adev);
1457 amdgpu_device_check_vm_size(adev);
1459 amdgpu_device_check_block_size(adev);
1461 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
1463 amdgpu_gmc_tmz_set(adev);
1465 amdgpu_gmc_noretry_set(adev);
1471 * amdgpu_switcheroo_set_state - set switcheroo state
1473 * @pdev: pci dev pointer
1474 * @state: vga_switcheroo state
1476 * Callback for the switcheroo driver. Suspends or resumes the
1477 * the asics before or after it is powered up using ACPI methods.
1479 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev,
1480 enum vga_switcheroo_state state)
1482 struct drm_device *dev = pci_get_drvdata(pdev);
1485 if (amdgpu_device_supports_px(dev) && state == VGA_SWITCHEROO_OFF)
1488 if (state == VGA_SWITCHEROO_ON) {
1489 pr_info("switched on\n");
1490 /* don't suspend or resume card normally */
1491 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1493 pci_set_power_state(pdev, PCI_D0);
1494 amdgpu_device_load_pci_state(pdev);
1495 r = pci_enable_device(pdev);
1497 DRM_WARN("pci_enable_device failed (%d)\n", r);
1498 amdgpu_device_resume(dev, true);
1500 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1502 pr_info("switched off\n");
1503 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1504 amdgpu_device_suspend(dev, true);
1505 amdgpu_device_cache_pci_state(pdev);
1506 /* Shut down the device */
1507 pci_disable_device(pdev);
1508 pci_set_power_state(pdev, PCI_D3cold);
1509 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1514 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1516 * @pdev: pci dev pointer
1518 * Callback for the switcheroo driver. Check of the switcheroo
1519 * state can be changed.
1520 * Returns true if the state can be changed, false if not.
1522 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1524 struct drm_device *dev = pci_get_drvdata(pdev);
1527 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1528 * locking inversion with the driver load path. And the access here is
1529 * completely racy anyway. So don't bother with locking for now.
1531 return atomic_read(&dev->open_count) == 0;
1534 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1535 .set_gpu_state = amdgpu_switcheroo_set_state,
1537 .can_switch = amdgpu_switcheroo_can_switch,
1541 * amdgpu_device_ip_set_clockgating_state - set the CG state
1543 * @dev: amdgpu_device pointer
1544 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1545 * @state: clockgating state (gate or ungate)
1547 * Sets the requested clockgating state for all instances of
1548 * the hardware IP specified.
1549 * Returns the error code from the last instance.
1551 int amdgpu_device_ip_set_clockgating_state(void *dev,
1552 enum amd_ip_block_type block_type,
1553 enum amd_clockgating_state state)
1555 struct amdgpu_device *adev = dev;
1558 for (i = 0; i < adev->num_ip_blocks; i++) {
1559 if (!adev->ip_blocks[i].status.valid)
1561 if (adev->ip_blocks[i].version->type != block_type)
1563 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1565 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1566 (void *)adev, state);
1568 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1569 adev->ip_blocks[i].version->funcs->name, r);
1575 * amdgpu_device_ip_set_powergating_state - set the PG state
1577 * @dev: amdgpu_device pointer
1578 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1579 * @state: powergating state (gate or ungate)
1581 * Sets the requested powergating state for all instances of
1582 * the hardware IP specified.
1583 * Returns the error code from the last instance.
1585 int amdgpu_device_ip_set_powergating_state(void *dev,
1586 enum amd_ip_block_type block_type,
1587 enum amd_powergating_state state)
1589 struct amdgpu_device *adev = dev;
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->type != block_type)
1597 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1599 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1600 (void *)adev, state);
1602 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1603 adev->ip_blocks[i].version->funcs->name, r);
1609 * amdgpu_device_ip_get_clockgating_state - get the CG state
1611 * @adev: amdgpu_device pointer
1612 * @flags: clockgating feature flags
1614 * Walks the list of IPs on the device and updates the clockgating
1615 * flags for each IP.
1616 * Updates @flags with the feature flags for each hardware IP where
1617 * clockgating is enabled.
1619 void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
1624 for (i = 0; i < adev->num_ip_blocks; i++) {
1625 if (!adev->ip_blocks[i].status.valid)
1627 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1628 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1633 * amdgpu_device_ip_wait_for_idle - wait for idle
1635 * @adev: amdgpu_device pointer
1636 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1638 * Waits for the request hardware IP to be idle.
1639 * Returns 0 for success or a negative error code on failure.
1641 int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1642 enum amd_ip_block_type block_type)
1646 for (i = 0; i < adev->num_ip_blocks; i++) {
1647 if (!adev->ip_blocks[i].status.valid)
1649 if (adev->ip_blocks[i].version->type == block_type) {
1650 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
1661 * amdgpu_device_ip_is_idle - is the hardware IP idle
1663 * @adev: amdgpu_device pointer
1664 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1666 * Check if the hardware IP is idle or not.
1667 * Returns true if it the IP is idle, false if not.
1669 bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1670 enum amd_ip_block_type block_type)
1674 for (i = 0; i < adev->num_ip_blocks; i++) {
1675 if (!adev->ip_blocks[i].status.valid)
1677 if (adev->ip_blocks[i].version->type == block_type)
1678 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
1685 * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1687 * @adev: amdgpu_device pointer
1688 * @type: Type of hardware IP (SMU, GFX, UVD, etc.)
1690 * Returns a pointer to the hardware IP block structure
1691 * if it exists for the asic, otherwise NULL.
1693 struct amdgpu_ip_block *
1694 amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1695 enum amd_ip_block_type type)
1699 for (i = 0; i < adev->num_ip_blocks; i++)
1700 if (adev->ip_blocks[i].version->type == type)
1701 return &adev->ip_blocks[i];
1707 * amdgpu_device_ip_block_version_cmp
1709 * @adev: amdgpu_device pointer
1710 * @type: enum amd_ip_block_type
1711 * @major: major version
1712 * @minor: minor version
1714 * return 0 if equal or greater
1715 * return 1 if smaller or the ip_block doesn't exist
1717 int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1718 enum amd_ip_block_type type,
1719 u32 major, u32 minor)
1721 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
1723 if (ip_block && ((ip_block->version->major > major) ||
1724 ((ip_block->version->major == major) &&
1725 (ip_block->version->minor >= minor))))
1732 * amdgpu_device_ip_block_add
1734 * @adev: amdgpu_device pointer
1735 * @ip_block_version: pointer to the IP to add
1737 * Adds the IP block driver information to the collection of IPs
1740 int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1741 const struct amdgpu_ip_block_version *ip_block_version)
1743 if (!ip_block_version)
1746 switch (ip_block_version->type) {
1747 case AMD_IP_BLOCK_TYPE_VCN:
1748 if (adev->harvest_ip_mask & AMD_HARVEST_IP_VCN_MASK)
1751 case AMD_IP_BLOCK_TYPE_JPEG:
1752 if (adev->harvest_ip_mask & AMD_HARVEST_IP_JPEG_MASK)
1759 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
1760 ip_block_version->funcs->name);
1762 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1768 * amdgpu_device_enable_virtual_display - enable virtual display feature
1770 * @adev: amdgpu_device pointer
1772 * Enabled the virtual display feature if the user has enabled it via
1773 * the module parameter virtual_display. This feature provides a virtual
1774 * display hardware on headless boards or in virtualized environments.
1775 * This function parses and validates the configuration string specified by
1776 * the user and configues the virtual display configuration (number of
1777 * virtual connectors, crtcs, etc.) specified.
1779 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
1781 adev->enable_virtual_display = false;
1783 if (amdgpu_virtual_display) {
1784 const char *pci_address_name = pci_name(adev->pdev);
1785 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
1787 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1788 pciaddstr_tmp = pciaddstr;
1789 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1790 pciaddname = strsep(&pciaddname_tmp, ",");
1791 if (!strcmp("all", pciaddname)
1792 || !strcmp(pci_address_name, pciaddname)) {
1796 adev->enable_virtual_display = true;
1799 res = kstrtol(pciaddname_tmp, 10,
1807 adev->mode_info.num_crtc = num_crtc;
1809 adev->mode_info.num_crtc = 1;
1815 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1816 amdgpu_virtual_display, pci_address_name,
1817 adev->enable_virtual_display, adev->mode_info.num_crtc);
1824 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
1826 * @adev: amdgpu_device pointer
1828 * Parses the asic configuration parameters specified in the gpu info
1829 * firmware and makes them availale to the driver for use in configuring
1831 * Returns 0 on success, -EINVAL on failure.
1833 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1835 const char *chip_name;
1838 const struct gpu_info_firmware_header_v1_0 *hdr;
1840 adev->firmware.gpu_info_fw = NULL;
1842 if (adev->mman.discovery_bin) {
1843 amdgpu_discovery_get_gfx_info(adev);
1846 * FIXME: The bounding box is still needed by Navi12, so
1847 * temporarily read it from gpu_info firmware. Should be droped
1848 * when DAL no longer needs it.
1850 if (adev->asic_type != CHIP_NAVI12)
1854 switch (adev->asic_type) {
1855 #ifdef CONFIG_DRM_AMDGPU_SI
1862 #ifdef CONFIG_DRM_AMDGPU_CIK
1872 case CHIP_POLARIS10:
1873 case CHIP_POLARIS11:
1874 case CHIP_POLARIS12:
1879 case CHIP_ALDEBARAN:
1880 case CHIP_SIENNA_CICHLID:
1881 case CHIP_NAVY_FLOUNDER:
1882 case CHIP_DIMGREY_CAVEFISH:
1883 case CHIP_BEIGE_GOBY:
1887 chip_name = "vega10";
1890 chip_name = "vega12";
1893 if (adev->apu_flags & AMD_APU_IS_RAVEN2)
1894 chip_name = "raven2";
1895 else if (adev->apu_flags & AMD_APU_IS_PICASSO)
1896 chip_name = "picasso";
1898 chip_name = "raven";
1901 chip_name = "arcturus";
1904 if (adev->apu_flags & AMD_APU_IS_RENOIR)
1905 chip_name = "renoir";
1907 chip_name = "green_sardine";
1910 chip_name = "navi10";
1913 chip_name = "navi14";
1916 chip_name = "navi12";
1919 chip_name = "vangogh";
1921 case CHIP_YELLOW_CARP:
1922 chip_name = "yellow_carp";
1926 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
1927 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
1930 "Failed to load gpu_info firmware \"%s\"\n",
1934 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
1937 "Failed to validate gpu_info firmware \"%s\"\n",
1942 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
1943 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1945 switch (hdr->version_major) {
1948 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
1949 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
1950 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1953 * Should be droped when DAL no longer needs it.
1955 if (adev->asic_type == CHIP_NAVI12)
1956 goto parse_soc_bounding_box;
1958 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1959 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1960 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1961 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
1962 adev->gfx.config.max_texture_channel_caches =
1963 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1964 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1965 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1966 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1967 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
1968 adev->gfx.config.double_offchip_lds_buf =
1969 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1970 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
1971 adev->gfx.cu_info.max_waves_per_simd =
1972 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1973 adev->gfx.cu_info.max_scratch_slots_per_cu =
1974 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1975 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
1976 if (hdr->version_minor >= 1) {
1977 const struct gpu_info_firmware_v1_1 *gpu_info_fw =
1978 (const struct gpu_info_firmware_v1_1 *)(adev->firmware.gpu_info_fw->data +
1979 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1980 adev->gfx.config.num_sc_per_sh =
1981 le32_to_cpu(gpu_info_fw->num_sc_per_sh);
1982 adev->gfx.config.num_packer_per_sc =
1983 le32_to_cpu(gpu_info_fw->num_packer_per_sc);
1986 parse_soc_bounding_box:
1988 * soc bounding box info is not integrated in disocovery table,
1989 * we always need to parse it from gpu info firmware if needed.
1991 if (hdr->version_minor == 2) {
1992 const struct gpu_info_firmware_v1_2 *gpu_info_fw =
1993 (const struct gpu_info_firmware_v1_2 *)(adev->firmware.gpu_info_fw->data +
1994 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1995 adev->dm.soc_bounding_box = &gpu_info_fw->soc_bounding_box;
2001 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
2010 * amdgpu_device_ip_early_init - run early init for hardware IPs
2012 * @adev: amdgpu_device pointer
2014 * Early initialization pass for hardware IPs. The hardware IPs that make
2015 * up each asic are discovered each IP's early_init callback is run. This
2016 * is the first stage in initializing the asic.
2017 * Returns 0 on success, negative error code on failure.
2019 static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
2023 amdgpu_device_enable_virtual_display(adev);
2025 if (amdgpu_sriov_vf(adev)) {
2026 r = amdgpu_virt_request_full_gpu(adev, true);
2031 switch (adev->asic_type) {
2032 #ifdef CONFIG_DRM_AMDGPU_SI
2038 adev->family = AMDGPU_FAMILY_SI;
2039 r = si_set_ip_blocks(adev);
2044 #ifdef CONFIG_DRM_AMDGPU_CIK
2050 if (adev->flags & AMD_IS_APU)
2051 adev->family = AMDGPU_FAMILY_KV;
2053 adev->family = AMDGPU_FAMILY_CI;
2055 r = cik_set_ip_blocks(adev);
2063 case CHIP_POLARIS10:
2064 case CHIP_POLARIS11:
2065 case CHIP_POLARIS12:
2069 if (adev->flags & AMD_IS_APU)
2070 adev->family = AMDGPU_FAMILY_CZ;
2072 adev->family = AMDGPU_FAMILY_VI;
2074 r = vi_set_ip_blocks(adev);
2084 case CHIP_ALDEBARAN:
2085 if (adev->flags & AMD_IS_APU)
2086 adev->family = AMDGPU_FAMILY_RV;
2088 adev->family = AMDGPU_FAMILY_AI;
2090 r = soc15_set_ip_blocks(adev);
2097 case CHIP_SIENNA_CICHLID:
2098 case CHIP_NAVY_FLOUNDER:
2099 case CHIP_DIMGREY_CAVEFISH:
2100 case CHIP_BEIGE_GOBY:
2102 case CHIP_YELLOW_CARP:
2103 if (adev->asic_type == CHIP_VANGOGH)
2104 adev->family = AMDGPU_FAMILY_VGH;
2105 else if (adev->asic_type == CHIP_YELLOW_CARP)
2106 adev->family = AMDGPU_FAMILY_YC;
2108 adev->family = AMDGPU_FAMILY_NV;
2110 r = nv_set_ip_blocks(adev);
2115 /* FIXME: not supported yet */
2119 amdgpu_amdkfd_device_probe(adev);
2121 adev->pm.pp_feature = amdgpu_pp_feature_mask;
2122 if (amdgpu_sriov_vf(adev) || sched_policy == KFD_SCHED_POLICY_NO_HWS)
2123 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
2124 if (amdgpu_sriov_vf(adev) && adev->asic_type == CHIP_SIENNA_CICHLID)
2125 adev->pm.pp_feature &= ~PP_OVERDRIVE_MASK;
2127 for (i = 0; i < adev->num_ip_blocks; i++) {
2128 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
2129 DRM_ERROR("disabled ip block: %d <%s>\n",
2130 i, adev->ip_blocks[i].version->funcs->name);
2131 adev->ip_blocks[i].status.valid = false;
2133 if (adev->ip_blocks[i].version->funcs->early_init) {
2134 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
2136 adev->ip_blocks[i].status.valid = false;
2138 DRM_ERROR("early_init of IP block <%s> failed %d\n",
2139 adev->ip_blocks[i].version->funcs->name, r);
2142 adev->ip_blocks[i].status.valid = true;
2145 adev->ip_blocks[i].status.valid = true;
2148 /* get the vbios after the asic_funcs are set up */
2149 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) {
2150 r = amdgpu_device_parse_gpu_info_fw(adev);
2155 if (!amdgpu_get_bios(adev))
2158 r = amdgpu_atombios_init(adev);
2160 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
2161 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
2165 /*get pf2vf msg info at it's earliest time*/
2166 if (amdgpu_sriov_vf(adev))
2167 amdgpu_virt_init_data_exchange(adev);
2172 adev->cg_flags &= amdgpu_cg_mask;
2173 adev->pg_flags &= amdgpu_pg_mask;
2178 static int amdgpu_device_ip_hw_init_phase1(struct amdgpu_device *adev)
2182 for (i = 0; i < adev->num_ip_blocks; i++) {
2183 if (!adev->ip_blocks[i].status.sw)
2185 if (adev->ip_blocks[i].status.hw)
2187 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2188 (amdgpu_sriov_vf(adev) && (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)) ||
2189 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
2190 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2192 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2193 adev->ip_blocks[i].version->funcs->name, r);
2196 adev->ip_blocks[i].status.hw = true;
2203 static int amdgpu_device_ip_hw_init_phase2(struct amdgpu_device *adev)
2207 for (i = 0; i < adev->num_ip_blocks; i++) {
2208 if (!adev->ip_blocks[i].status.sw)
2210 if (adev->ip_blocks[i].status.hw)
2212 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2214 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2215 adev->ip_blocks[i].version->funcs->name, r);
2218 adev->ip_blocks[i].status.hw = true;
2224 static int amdgpu_device_fw_loading(struct amdgpu_device *adev)
2228 uint32_t smu_version;
2230 if (adev->asic_type >= CHIP_VEGA10) {
2231 for (i = 0; i < adev->num_ip_blocks; i++) {
2232 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_PSP)
2235 if (!adev->ip_blocks[i].status.sw)
2238 /* no need to do the fw loading again if already done*/
2239 if (adev->ip_blocks[i].status.hw == true)
2242 if (amdgpu_in_reset(adev) || adev->in_suspend) {
2243 r = adev->ip_blocks[i].version->funcs->resume(adev);
2245 DRM_ERROR("resume of IP block <%s> failed %d\n",
2246 adev->ip_blocks[i].version->funcs->name, r);
2250 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2252 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2253 adev->ip_blocks[i].version->funcs->name, r);
2258 adev->ip_blocks[i].status.hw = true;
2263 if (!amdgpu_sriov_vf(adev) || adev->asic_type == CHIP_TONGA)
2264 r = amdgpu_pm_load_smu_firmware(adev, &smu_version);
2270 * amdgpu_device_ip_init - run init for hardware IPs
2272 * @adev: amdgpu_device pointer
2274 * Main initialization pass for hardware IPs. The list of all the hardware
2275 * IPs that make up the asic is walked and the sw_init and hw_init callbacks
2276 * are run. sw_init initializes the software state associated with each IP
2277 * and hw_init initializes the hardware associated with each IP.
2278 * Returns 0 on success, negative error code on failure.
2280 static int amdgpu_device_ip_init(struct amdgpu_device *adev)
2284 r = amdgpu_ras_init(adev);
2288 for (i = 0; i < adev->num_ip_blocks; i++) {
2289 if (!adev->ip_blocks[i].status.valid)
2291 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
2293 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
2294 adev->ip_blocks[i].version->funcs->name, r);
2297 adev->ip_blocks[i].status.sw = true;
2299 /* need to do gmc hw init early so we can allocate gpu mem */
2300 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
2301 r = amdgpu_device_vram_scratch_init(adev);
2303 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
2306 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
2308 DRM_ERROR("hw_init %d failed %d\n", i, r);
2311 r = amdgpu_device_wb_init(adev);
2313 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
2316 adev->ip_blocks[i].status.hw = true;
2318 /* right after GMC hw init, we create CSA */
2319 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) {
2320 r = amdgpu_allocate_static_csa(adev, &adev->virt.csa_obj,
2321 AMDGPU_GEM_DOMAIN_VRAM,
2324 DRM_ERROR("allocate CSA failed %d\n", r);
2331 if (amdgpu_sriov_vf(adev))
2332 amdgpu_virt_init_data_exchange(adev);
2334 r = amdgpu_ib_pool_init(adev);
2336 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
2337 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
2341 r = amdgpu_ucode_create_bo(adev); /* create ucode bo when sw_init complete*/
2345 r = amdgpu_device_ip_hw_init_phase1(adev);
2349 r = amdgpu_device_fw_loading(adev);
2353 r = amdgpu_device_ip_hw_init_phase2(adev);
2358 * retired pages will be loaded from eeprom and reserved here,
2359 * it should be called after amdgpu_device_ip_hw_init_phase2 since
2360 * for some ASICs the RAS EEPROM code relies on SMU fully functioning
2361 * for I2C communication which only true at this point.
2363 * amdgpu_ras_recovery_init may fail, but the upper only cares the
2364 * failure from bad gpu situation and stop amdgpu init process
2365 * accordingly. For other failed cases, it will still release all
2366 * the resource and print error message, rather than returning one
2367 * negative value to upper level.
2369 * Note: theoretically, this should be called before all vram allocations
2370 * to protect retired page from abusing
2372 r = amdgpu_ras_recovery_init(adev);
2376 if (adev->gmc.xgmi.num_physical_nodes > 1)
2377 amdgpu_xgmi_add_device(adev);
2379 /* Don't init kfd if whole hive need to be reset during init */
2380 if (!adev->gmc.xgmi.pending_reset)
2381 amdgpu_amdkfd_device_init(adev);
2383 amdgpu_fru_get_product_info(adev);
2386 if (amdgpu_sriov_vf(adev))
2387 amdgpu_virt_release_full_gpu(adev, true);
2393 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
2395 * @adev: amdgpu_device pointer
2397 * Writes a reset magic value to the gart pointer in VRAM. The driver calls
2398 * this function before a GPU reset. If the value is retained after a
2399 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
2401 static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
2403 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
2407 * amdgpu_device_check_vram_lost - check if vram is valid
2409 * @adev: amdgpu_device pointer
2411 * Checks the reset magic value written to the gart pointer in VRAM.
2412 * The driver calls this after a GPU reset to see if the contents of
2413 * VRAM is lost or now.
2414 * returns true if vram is lost, false if not.
2416 static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
2418 if (memcmp(adev->gart.ptr, adev->reset_magic,
2419 AMDGPU_RESET_MAGIC_NUM))
2422 if (!amdgpu_in_reset(adev))
2426 * For all ASICs with baco/mode1 reset, the VRAM is
2427 * always assumed to be lost.
2429 switch (amdgpu_asic_reset_method(adev)) {
2430 case AMD_RESET_METHOD_BACO:
2431 case AMD_RESET_METHOD_MODE1:
2439 * amdgpu_device_set_cg_state - set clockgating for amdgpu device
2441 * @adev: amdgpu_device pointer
2442 * @state: clockgating state (gate or ungate)
2444 * The list of all the hardware IPs that make up the asic is walked and the
2445 * set_clockgating_state callbacks are run.
2446 * Late initialization pass enabling clockgating for hardware IPs.
2447 * Fini or suspend, pass disabling clockgating for hardware IPs.
2448 * Returns 0 on success, negative error code on failure.
2451 int amdgpu_device_set_cg_state(struct amdgpu_device *adev,
2452 enum amd_clockgating_state state)
2456 if (amdgpu_emu_mode == 1)
2459 for (j = 0; j < adev->num_ip_blocks; j++) {
2460 i = state == AMD_CG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
2461 if (!adev->ip_blocks[i].status.late_initialized)
2463 /* skip CG for GFX on S0ix */
2464 if (adev->in_s0ix &&
2465 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX)
2467 /* skip CG for VCE/UVD, it's handled specially */
2468 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
2469 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
2470 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
2471 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
2472 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
2473 /* enable clockgating to save power */
2474 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
2477 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
2478 adev->ip_blocks[i].version->funcs->name, r);
2487 int amdgpu_device_set_pg_state(struct amdgpu_device *adev,
2488 enum amd_powergating_state state)
2492 if (amdgpu_emu_mode == 1)
2495 for (j = 0; j < adev->num_ip_blocks; j++) {
2496 i = state == AMD_PG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
2497 if (!adev->ip_blocks[i].status.late_initialized)
2499 /* skip PG for GFX on S0ix */
2500 if (adev->in_s0ix &&
2501 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX)
2503 /* skip CG for VCE/UVD, it's handled specially */
2504 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
2505 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
2506 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
2507 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
2508 adev->ip_blocks[i].version->funcs->set_powergating_state) {
2509 /* enable powergating to save power */
2510 r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
2513 DRM_ERROR("set_powergating_state(gate) of IP block <%s> failed %d\n",
2514 adev->ip_blocks[i].version->funcs->name, r);
2522 static int amdgpu_device_enable_mgpu_fan_boost(void)
2524 struct amdgpu_gpu_instance *gpu_ins;
2525 struct amdgpu_device *adev;
2528 mutex_lock(&mgpu_info.mutex);
2531 * MGPU fan boost feature should be enabled
2532 * only when there are two or more dGPUs in
2535 if (mgpu_info.num_dgpu < 2)
2538 for (i = 0; i < mgpu_info.num_dgpu; i++) {
2539 gpu_ins = &(mgpu_info.gpu_ins[i]);
2540 adev = gpu_ins->adev;
2541 if (!(adev->flags & AMD_IS_APU) &&
2542 !gpu_ins->mgpu_fan_enabled) {
2543 ret = amdgpu_dpm_enable_mgpu_fan_boost(adev);
2547 gpu_ins->mgpu_fan_enabled = 1;
2552 mutex_unlock(&mgpu_info.mutex);
2558 * amdgpu_device_ip_late_init - run late init for hardware IPs
2560 * @adev: amdgpu_device pointer
2562 * Late initialization pass for hardware IPs. The list of all the hardware
2563 * IPs that make up the asic is walked and the late_init callbacks are run.
2564 * late_init covers any special initialization that an IP requires
2565 * after all of the have been initialized or something that needs to happen
2566 * late in the init process.
2567 * Returns 0 on success, negative error code on failure.
2569 static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
2571 struct amdgpu_gpu_instance *gpu_instance;
2574 for (i = 0; i < adev->num_ip_blocks; i++) {
2575 if (!adev->ip_blocks[i].status.hw)
2577 if (adev->ip_blocks[i].version->funcs->late_init) {
2578 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
2580 DRM_ERROR("late_init of IP block <%s> failed %d\n",
2581 adev->ip_blocks[i].version->funcs->name, r);
2585 adev->ip_blocks[i].status.late_initialized = true;
2588 amdgpu_ras_set_error_query_ready(adev, true);
2590 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE);
2591 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_GATE);
2593 amdgpu_device_fill_reset_magic(adev);
2595 r = amdgpu_device_enable_mgpu_fan_boost();
2597 DRM_ERROR("enable mgpu fan boost failed (%d).\n", r);
2599 /* For XGMI + passthrough configuration on arcturus, enable light SBR */
2600 if (adev->asic_type == CHIP_ARCTURUS &&
2601 amdgpu_passthrough(adev) &&
2602 adev->gmc.xgmi.num_physical_nodes > 1)
2603 smu_set_light_sbr(&adev->smu, true);
2605 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2606 mutex_lock(&mgpu_info.mutex);
2609 * Reset device p-state to low as this was booted with high.
2611 * This should be performed only after all devices from the same
2612 * hive get initialized.
2614 * However, it's unknown how many device in the hive in advance.
2615 * As this is counted one by one during devices initializations.
2617 * So, we wait for all XGMI interlinked devices initialized.
2618 * This may bring some delays as those devices may come from
2619 * different hives. But that should be OK.
2621 if (mgpu_info.num_dgpu == adev->gmc.xgmi.num_physical_nodes) {
2622 for (i = 0; i < mgpu_info.num_gpu; i++) {
2623 gpu_instance = &(mgpu_info.gpu_ins[i]);
2624 if (gpu_instance->adev->flags & AMD_IS_APU)
2627 r = amdgpu_xgmi_set_pstate(gpu_instance->adev,
2628 AMDGPU_XGMI_PSTATE_MIN);
2630 DRM_ERROR("pstate setting failed (%d).\n", r);
2636 mutex_unlock(&mgpu_info.mutex);
2642 static int amdgpu_device_ip_fini_early(struct amdgpu_device *adev)
2646 for (i = 0; i < adev->num_ip_blocks; i++) {
2647 if (!adev->ip_blocks[i].version->funcs->early_fini)
2650 r = adev->ip_blocks[i].version->funcs->early_fini((void *)adev);
2652 DRM_DEBUG("early_fini of IP block <%s> failed %d\n",
2653 adev->ip_blocks[i].version->funcs->name, r);
2657 amdgpu_amdkfd_suspend(adev, false);
2659 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2660 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2662 /* need to disable SMC first */
2663 for (i = 0; i < adev->num_ip_blocks; i++) {
2664 if (!adev->ip_blocks[i].status.hw)
2666 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
2667 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2668 /* XXX handle errors */
2670 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2671 adev->ip_blocks[i].version->funcs->name, r);
2673 adev->ip_blocks[i].status.hw = false;
2678 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2679 if (!adev->ip_blocks[i].status.hw)
2682 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2683 /* XXX handle errors */
2685 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2686 adev->ip_blocks[i].version->funcs->name, r);
2689 adev->ip_blocks[i].status.hw = false;
2696 * amdgpu_device_ip_fini - run fini for hardware IPs
2698 * @adev: amdgpu_device pointer
2700 * Main teardown pass for hardware IPs. The list of all the hardware
2701 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
2702 * are run. hw_fini tears down the hardware associated with each IP
2703 * and sw_fini tears down any software state associated with each IP.
2704 * Returns 0 on success, negative error code on failure.
2706 static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
2710 if (amdgpu_sriov_vf(adev) && adev->virt.ras_init_done)
2711 amdgpu_virt_release_ras_err_handler_data(adev);
2713 amdgpu_ras_pre_fini(adev);
2715 if (adev->gmc.xgmi.num_physical_nodes > 1)
2716 amdgpu_xgmi_remove_device(adev);
2718 amdgpu_amdkfd_device_fini_sw(adev);
2720 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2721 if (!adev->ip_blocks[i].status.sw)
2724 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
2725 amdgpu_ucode_free_bo(adev);
2726 amdgpu_free_static_csa(&adev->virt.csa_obj);
2727 amdgpu_device_wb_fini(adev);
2728 amdgpu_device_vram_scratch_fini(adev);
2729 amdgpu_ib_pool_fini(adev);
2732 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
2733 /* XXX handle errors */
2735 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
2736 adev->ip_blocks[i].version->funcs->name, r);
2738 adev->ip_blocks[i].status.sw = false;
2739 adev->ip_blocks[i].status.valid = false;
2742 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2743 if (!adev->ip_blocks[i].status.late_initialized)
2745 if (adev->ip_blocks[i].version->funcs->late_fini)
2746 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
2747 adev->ip_blocks[i].status.late_initialized = false;
2750 amdgpu_ras_fini(adev);
2752 if (amdgpu_sriov_vf(adev))
2753 if (amdgpu_virt_release_full_gpu(adev, false))
2754 DRM_ERROR("failed to release exclusive mode on fini\n");
2760 * amdgpu_device_delayed_init_work_handler - work handler for IB tests
2762 * @work: work_struct.
2764 static void amdgpu_device_delayed_init_work_handler(struct work_struct *work)
2766 struct amdgpu_device *adev =
2767 container_of(work, struct amdgpu_device, delayed_init_work.work);
2770 r = amdgpu_ib_ring_tests(adev);
2772 DRM_ERROR("ib ring test failed (%d).\n", r);
2775 static void amdgpu_device_delay_enable_gfx_off(struct work_struct *work)
2777 struct amdgpu_device *adev =
2778 container_of(work, struct amdgpu_device, gfx.gfx_off_delay_work.work);
2780 mutex_lock(&adev->gfx.gfx_off_mutex);
2781 if (!adev->gfx.gfx_off_state && !adev->gfx.gfx_off_req_count) {
2782 if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true))
2783 adev->gfx.gfx_off_state = true;
2785 mutex_unlock(&adev->gfx.gfx_off_mutex);
2789 * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1)
2791 * @adev: amdgpu_device pointer
2793 * Main suspend function for hardware IPs. The list of all the hardware
2794 * IPs that make up the asic is walked, clockgating is disabled and the
2795 * suspend callbacks are run. suspend puts the hardware and software state
2796 * in each IP into a state suitable for suspend.
2797 * Returns 0 on success, negative error code on failure.
2799 static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev)
2803 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2804 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2806 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2807 if (!adev->ip_blocks[i].status.valid)
2810 /* displays are handled separately */
2811 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_DCE)
2814 /* XXX handle errors */
2815 r = adev->ip_blocks[i].version->funcs->suspend(adev);
2816 /* XXX handle errors */
2818 DRM_ERROR("suspend of IP block <%s> failed %d\n",
2819 adev->ip_blocks[i].version->funcs->name, r);
2823 adev->ip_blocks[i].status.hw = false;
2830 * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2)
2832 * @adev: amdgpu_device pointer
2834 * Main suspend function for hardware IPs. The list of all the hardware
2835 * IPs that make up the asic is walked, clockgating is disabled and the
2836 * suspend callbacks are run. suspend puts the hardware and software state
2837 * in each IP into a state suitable for suspend.
2838 * Returns 0 on success, negative error code on failure.
2840 static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev)
2845 amdgpu_gfx_state_change_set(adev, sGpuChangeState_D3Entry);
2847 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2848 if (!adev->ip_blocks[i].status.valid)
2850 /* displays are handled in phase1 */
2851 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)
2853 /* PSP lost connection when err_event_athub occurs */
2854 if (amdgpu_ras_intr_triggered() &&
2855 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
2856 adev->ip_blocks[i].status.hw = false;
2860 /* skip unnecessary suspend if we do not initialize them yet */
2861 if (adev->gmc.xgmi.pending_reset &&
2862 !(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2863 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC ||
2864 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2865 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)) {
2866 adev->ip_blocks[i].status.hw = false;
2870 /* skip suspend of gfx and psp for S0ix
2871 * gfx is in gfxoff state, so on resume it will exit gfxoff just
2872 * like at runtime. PSP is also part of the always on hardware
2873 * so no need to suspend it.
2875 if (adev->in_s0ix &&
2876 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP ||
2877 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX))
2880 /* XXX handle errors */
2881 r = adev->ip_blocks[i].version->funcs->suspend(adev);
2882 /* XXX handle errors */
2884 DRM_ERROR("suspend of IP block <%s> failed %d\n",
2885 adev->ip_blocks[i].version->funcs->name, r);
2887 adev->ip_blocks[i].status.hw = false;
2888 /* handle putting the SMC in the appropriate state */
2889 if(!amdgpu_sriov_vf(adev)){
2890 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
2891 r = amdgpu_dpm_set_mp1_state(adev, adev->mp1_state);
2893 DRM_ERROR("SMC failed to set mp1 state %d, %d\n",
2894 adev->mp1_state, r);
2905 * amdgpu_device_ip_suspend - run suspend for hardware IPs
2907 * @adev: amdgpu_device pointer
2909 * Main suspend function for hardware IPs. The list of all the hardware
2910 * IPs that make up the asic is walked, clockgating is disabled and the
2911 * suspend callbacks are run. suspend puts the hardware and software state
2912 * in each IP into a state suitable for suspend.
2913 * Returns 0 on success, negative error code on failure.
2915 int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
2919 if (amdgpu_sriov_vf(adev)) {
2920 amdgpu_virt_fini_data_exchange(adev);
2921 amdgpu_virt_request_full_gpu(adev, false);
2924 r = amdgpu_device_ip_suspend_phase1(adev);
2927 r = amdgpu_device_ip_suspend_phase2(adev);
2929 if (amdgpu_sriov_vf(adev))
2930 amdgpu_virt_release_full_gpu(adev, false);
2935 static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
2939 static enum amd_ip_block_type ip_order[] = {
2940 AMD_IP_BLOCK_TYPE_GMC,
2941 AMD_IP_BLOCK_TYPE_COMMON,
2942 AMD_IP_BLOCK_TYPE_PSP,
2943 AMD_IP_BLOCK_TYPE_IH,
2946 for (i = 0; i < adev->num_ip_blocks; i++) {
2948 struct amdgpu_ip_block *block;
2950 block = &adev->ip_blocks[i];
2951 block->status.hw = false;
2953 for (j = 0; j < ARRAY_SIZE(ip_order); j++) {
2955 if (block->version->type != ip_order[j] ||
2956 !block->status.valid)
2959 r = block->version->funcs->hw_init(adev);
2960 DRM_INFO("RE-INIT-early: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
2963 block->status.hw = true;
2970 static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
2974 static enum amd_ip_block_type ip_order[] = {
2975 AMD_IP_BLOCK_TYPE_SMC,
2976 AMD_IP_BLOCK_TYPE_DCE,
2977 AMD_IP_BLOCK_TYPE_GFX,
2978 AMD_IP_BLOCK_TYPE_SDMA,
2979 AMD_IP_BLOCK_TYPE_UVD,
2980 AMD_IP_BLOCK_TYPE_VCE,
2981 AMD_IP_BLOCK_TYPE_VCN
2984 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
2986 struct amdgpu_ip_block *block;
2988 for (j = 0; j < adev->num_ip_blocks; j++) {
2989 block = &adev->ip_blocks[j];
2991 if (block->version->type != ip_order[i] ||
2992 !block->status.valid ||
2996 if (block->version->type == AMD_IP_BLOCK_TYPE_SMC)
2997 r = block->version->funcs->resume(adev);
2999 r = block->version->funcs->hw_init(adev);
3001 DRM_INFO("RE-INIT-late: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
3004 block->status.hw = true;
3012 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
3014 * @adev: amdgpu_device pointer
3016 * First resume function for hardware IPs. The list of all the hardware
3017 * IPs that make up the asic is walked and the resume callbacks are run for
3018 * COMMON, GMC, and IH. resume puts the hardware into a functional state
3019 * after a suspend and updates the software state as necessary. This
3020 * function is also used for restoring the GPU after a GPU reset.
3021 * Returns 0 on success, negative error code on failure.
3023 static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
3027 for (i = 0; i < adev->num_ip_blocks; i++) {
3028 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
3030 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3031 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3032 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
3034 r = adev->ip_blocks[i].version->funcs->resume(adev);
3036 DRM_ERROR("resume of IP block <%s> failed %d\n",
3037 adev->ip_blocks[i].version->funcs->name, r);
3040 adev->ip_blocks[i].status.hw = true;
3048 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
3050 * @adev: amdgpu_device pointer
3052 * First resume function for hardware IPs. The list of all the hardware
3053 * IPs that make up the asic is walked and the resume callbacks are run for
3054 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
3055 * functional state after a suspend and updates the software state as
3056 * necessary. This function is also used for restoring the GPU after a GPU
3058 * Returns 0 on success, negative error code on failure.
3060 static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
3064 for (i = 0; i < adev->num_ip_blocks; i++) {
3065 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
3067 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3068 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3069 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3070 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)
3072 r = adev->ip_blocks[i].version->funcs->resume(adev);
3074 DRM_ERROR("resume of IP block <%s> failed %d\n",
3075 adev->ip_blocks[i].version->funcs->name, r);
3078 adev->ip_blocks[i].status.hw = true;
3085 * amdgpu_device_ip_resume - run resume for hardware IPs
3087 * @adev: amdgpu_device pointer
3089 * Main resume function for hardware IPs. The hardware IPs
3090 * are split into two resume functions because they are
3091 * are also used in in recovering from a GPU reset and some additional
3092 * steps need to be take between them. In this case (S3/S4) they are
3094 * Returns 0 on success, negative error code on failure.
3096 static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
3100 r = amdgpu_device_ip_resume_phase1(adev);
3104 r = amdgpu_device_fw_loading(adev);
3108 r = amdgpu_device_ip_resume_phase2(adev);
3114 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
3116 * @adev: amdgpu_device pointer
3118 * Query the VBIOS data tables to determine if the board supports SR-IOV.
3120 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
3122 if (amdgpu_sriov_vf(adev)) {
3123 if (adev->is_atom_fw) {
3124 if (amdgpu_atomfirmware_gpu_virtualization_supported(adev))
3125 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
3127 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
3128 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
3131 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
3132 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
3137 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
3139 * @asic_type: AMD asic type
3141 * Check if there is DC (new modesetting infrastructre) support for an asic.
3142 * returns true if DC has support, false if not.
3144 bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
3146 switch (asic_type) {
3147 #if defined(CONFIG_DRM_AMD_DC)
3148 #if defined(CONFIG_DRM_AMD_DC_SI)
3159 * We have systems in the wild with these ASICs that require
3160 * LVDS and VGA support which is not supported with DC.
3162 * Fallback to the non-DC driver here by default so as not to
3163 * cause regressions.
3165 return amdgpu_dc > 0;
3169 case CHIP_POLARIS10:
3170 case CHIP_POLARIS11:
3171 case CHIP_POLARIS12:
3178 #if defined(CONFIG_DRM_AMD_DC_DCN)
3184 case CHIP_SIENNA_CICHLID:
3185 case CHIP_NAVY_FLOUNDER:
3186 case CHIP_DIMGREY_CAVEFISH:
3187 case CHIP_BEIGE_GOBY:
3189 case CHIP_YELLOW_CARP:
3191 return amdgpu_dc != 0;
3195 DRM_INFO_ONCE("Display Core has been requested via kernel parameter "
3196 "but isn't supported by ASIC, ignoring\n");
3202 * amdgpu_device_has_dc_support - check if dc is supported
3204 * @adev: amdgpu_device pointer
3206 * Returns true for supported, false for not supported
3208 bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
3210 if (amdgpu_sriov_vf(adev) ||
3211 adev->enable_virtual_display ||
3212 (adev->harvest_ip_mask & AMD_HARVEST_IP_DMU_MASK))
3215 return amdgpu_device_asic_has_dc_support(adev->asic_type);
3218 static void amdgpu_device_xgmi_reset_func(struct work_struct *__work)
3220 struct amdgpu_device *adev =
3221 container_of(__work, struct amdgpu_device, xgmi_reset_work);
3222 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
3224 /* It's a bug to not have a hive within this function */
3229 * Use task barrier to synchronize all xgmi reset works across the
3230 * hive. task_barrier_enter and task_barrier_exit will block
3231 * until all the threads running the xgmi reset works reach
3232 * those points. task_barrier_full will do both blocks.
3234 if (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) {
3236 task_barrier_enter(&hive->tb);
3237 adev->asic_reset_res = amdgpu_device_baco_enter(adev_to_drm(adev));
3239 if (adev->asic_reset_res)
3242 task_barrier_exit(&hive->tb);
3243 adev->asic_reset_res = amdgpu_device_baco_exit(adev_to_drm(adev));
3245 if (adev->asic_reset_res)
3248 if (adev->mmhub.ras_funcs &&
3249 adev->mmhub.ras_funcs->reset_ras_error_count)
3250 adev->mmhub.ras_funcs->reset_ras_error_count(adev);
3253 task_barrier_full(&hive->tb);
3254 adev->asic_reset_res = amdgpu_asic_reset(adev);
3258 if (adev->asic_reset_res)
3259 DRM_WARN("ASIC reset failed with error, %d for drm dev, %s",
3260 adev->asic_reset_res, adev_to_drm(adev)->unique);
3261 amdgpu_put_xgmi_hive(hive);
3264 static int amdgpu_device_get_job_timeout_settings(struct amdgpu_device *adev)
3266 char *input = amdgpu_lockup_timeout;
3267 char *timeout_setting = NULL;
3273 * By default timeout for non compute jobs is 10000
3274 * and 60000 for compute jobs.
3275 * In SR-IOV or passthrough mode, timeout for compute
3276 * jobs are 60000 by default.
3278 adev->gfx_timeout = msecs_to_jiffies(10000);
3279 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
3280 if (amdgpu_sriov_vf(adev))
3281 adev->compute_timeout = amdgpu_sriov_is_pp_one_vf(adev) ?
3282 msecs_to_jiffies(60000) : msecs_to_jiffies(10000);
3284 adev->compute_timeout = msecs_to_jiffies(60000);
3286 if (strnlen(input, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
3287 while ((timeout_setting = strsep(&input, ",")) &&
3288 strnlen(timeout_setting, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
3289 ret = kstrtol(timeout_setting, 0, &timeout);
3296 } else if (timeout < 0) {
3297 timeout = MAX_SCHEDULE_TIMEOUT;
3299 timeout = msecs_to_jiffies(timeout);
3304 adev->gfx_timeout = timeout;
3307 adev->compute_timeout = timeout;
3310 adev->sdma_timeout = timeout;
3313 adev->video_timeout = timeout;
3320 * There is only one value specified and
3321 * it should apply to all non-compute jobs.
3324 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
3325 if (amdgpu_sriov_vf(adev) || amdgpu_passthrough(adev))
3326 adev->compute_timeout = adev->gfx_timeout;
3333 static const struct attribute *amdgpu_dev_attributes[] = {
3334 &dev_attr_product_name.attr,
3335 &dev_attr_product_number.attr,
3336 &dev_attr_serial_number.attr,
3337 &dev_attr_pcie_replay_count.attr,
3342 * amdgpu_device_init - initialize the driver
3344 * @adev: amdgpu_device pointer
3345 * @flags: driver flags
3347 * Initializes the driver info and hw (all asics).
3348 * Returns 0 for success or an error on failure.
3349 * Called at driver startup.
3351 int amdgpu_device_init(struct amdgpu_device *adev,
3354 struct drm_device *ddev = adev_to_drm(adev);
3355 struct pci_dev *pdev = adev->pdev;
3360 adev->shutdown = false;
3361 adev->flags = flags;
3363 if (amdgpu_force_asic_type >= 0 && amdgpu_force_asic_type < CHIP_LAST)
3364 adev->asic_type = amdgpu_force_asic_type;
3366 adev->asic_type = flags & AMD_ASIC_MASK;
3368 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
3369 if (amdgpu_emu_mode == 1)
3370 adev->usec_timeout *= 10;
3371 adev->gmc.gart_size = 512 * 1024 * 1024;
3372 adev->accel_working = false;
3373 adev->num_rings = 0;
3374 adev->mman.buffer_funcs = NULL;
3375 adev->mman.buffer_funcs_ring = NULL;
3376 adev->vm_manager.vm_pte_funcs = NULL;
3377 adev->vm_manager.vm_pte_num_scheds = 0;
3378 adev->gmc.gmc_funcs = NULL;
3379 adev->harvest_ip_mask = 0x0;
3380 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
3381 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
3383 adev->smc_rreg = &amdgpu_invalid_rreg;
3384 adev->smc_wreg = &amdgpu_invalid_wreg;
3385 adev->pcie_rreg = &amdgpu_invalid_rreg;
3386 adev->pcie_wreg = &amdgpu_invalid_wreg;
3387 adev->pciep_rreg = &amdgpu_invalid_rreg;
3388 adev->pciep_wreg = &amdgpu_invalid_wreg;
3389 adev->pcie_rreg64 = &amdgpu_invalid_rreg64;
3390 adev->pcie_wreg64 = &amdgpu_invalid_wreg64;
3391 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
3392 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
3393 adev->didt_rreg = &amdgpu_invalid_rreg;
3394 adev->didt_wreg = &amdgpu_invalid_wreg;
3395 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
3396 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
3397 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
3398 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
3400 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
3401 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
3402 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
3404 /* mutex initialization are all done here so we
3405 * can recall function without having locking issues */
3406 mutex_init(&adev->firmware.mutex);
3407 mutex_init(&adev->pm.mutex);
3408 mutex_init(&adev->gfx.gpu_clock_mutex);
3409 mutex_init(&adev->srbm_mutex);
3410 mutex_init(&adev->gfx.pipe_reserve_mutex);
3411 mutex_init(&adev->gfx.gfx_off_mutex);
3412 mutex_init(&adev->grbm_idx_mutex);
3413 mutex_init(&adev->mn_lock);
3414 mutex_init(&adev->virt.vf_errors.lock);
3415 hash_init(adev->mn_hash);
3416 atomic_set(&adev->in_gpu_reset, 0);
3417 init_rwsem(&adev->reset_sem);
3418 mutex_init(&adev->psp.mutex);
3419 mutex_init(&adev->notifier_lock);
3421 r = amdgpu_device_init_apu_flags(adev);
3425 r = amdgpu_device_check_arguments(adev);
3429 spin_lock_init(&adev->mmio_idx_lock);
3430 spin_lock_init(&adev->smc_idx_lock);
3431 spin_lock_init(&adev->pcie_idx_lock);
3432 spin_lock_init(&adev->uvd_ctx_idx_lock);
3433 spin_lock_init(&adev->didt_idx_lock);
3434 spin_lock_init(&adev->gc_cac_idx_lock);
3435 spin_lock_init(&adev->se_cac_idx_lock);
3436 spin_lock_init(&adev->audio_endpt_idx_lock);
3437 spin_lock_init(&adev->mm_stats.lock);
3439 INIT_LIST_HEAD(&adev->shadow_list);
3440 mutex_init(&adev->shadow_list_lock);
3442 INIT_LIST_HEAD(&adev->reset_list);
3444 INIT_DELAYED_WORK(&adev->delayed_init_work,
3445 amdgpu_device_delayed_init_work_handler);
3446 INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work,
3447 amdgpu_device_delay_enable_gfx_off);
3449 INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
3451 adev->gfx.gfx_off_req_count = 1;
3452 adev->pm.ac_power = power_supply_is_system_supplied() > 0;
3454 atomic_set(&adev->throttling_logging_enabled, 1);
3456 * If throttling continues, logging will be performed every minute
3457 * to avoid log flooding. "-1" is subtracted since the thermal
3458 * throttling interrupt comes every second. Thus, the total logging
3459 * interval is 59 seconds(retelimited printk interval) + 1(waiting
3460 * for throttling interrupt) = 60 seconds.
3462 ratelimit_state_init(&adev->throttling_logging_rs, (60 - 1) * HZ, 1);
3463 ratelimit_set_flags(&adev->throttling_logging_rs, RATELIMIT_MSG_ON_RELEASE);
3465 /* Registers mapping */
3466 /* TODO: block userspace mapping of io register */
3467 if (adev->asic_type >= CHIP_BONAIRE) {
3468 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
3469 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
3471 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
3472 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
3475 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
3476 if (adev->rmmio == NULL) {
3479 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
3480 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
3482 /* enable PCIE atomic ops */
3483 r = pci_enable_atomic_ops_to_root(adev->pdev,
3484 PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
3485 PCI_EXP_DEVCAP2_ATOMIC_COMP64);
3487 adev->have_atomics_support = false;
3488 DRM_INFO("PCIE atomic ops is not supported\n");
3490 adev->have_atomics_support = true;
3493 amdgpu_device_get_pcie_info(adev);
3496 DRM_INFO("MCBP is enabled\n");
3498 if (amdgpu_mes && adev->asic_type >= CHIP_NAVI10)
3499 adev->enable_mes = true;
3501 /* detect hw virtualization here */
3502 amdgpu_detect_virtualization(adev);
3504 r = amdgpu_device_get_job_timeout_settings(adev);
3506 dev_err(adev->dev, "invalid lockup_timeout parameter syntax\n");
3510 /* early init functions */
3511 r = amdgpu_device_ip_early_init(adev);
3515 /* doorbell bar mapping and doorbell index init*/
3516 amdgpu_device_doorbell_init(adev);
3518 if (amdgpu_emu_mode == 1) {
3519 /* post the asic on emulation mode */
3520 emu_soc_asic_init(adev);
3521 goto fence_driver_init;
3524 amdgpu_reset_init(adev);
3526 /* detect if we are with an SRIOV vbios */
3527 amdgpu_device_detect_sriov_bios(adev);
3529 /* check if we need to reset the asic
3530 * E.g., driver was not cleanly unloaded previously, etc.
3532 if (!amdgpu_sriov_vf(adev) && amdgpu_asic_need_reset_on_init(adev)) {
3533 if (adev->gmc.xgmi.num_physical_nodes) {
3534 dev_info(adev->dev, "Pending hive reset.\n");
3535 adev->gmc.xgmi.pending_reset = true;
3536 /* Only need to init necessary block for SMU to handle the reset */
3537 for (i = 0; i < adev->num_ip_blocks; i++) {
3538 if (!adev->ip_blocks[i].status.valid)
3540 if (!(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3541 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3542 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3543 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC)) {
3544 DRM_DEBUG("IP %s disabled for hw_init.\n",
3545 adev->ip_blocks[i].version->funcs->name);
3546 adev->ip_blocks[i].status.hw = true;
3550 r = amdgpu_asic_reset(adev);
3552 dev_err(adev->dev, "asic reset on init failed\n");
3558 pci_enable_pcie_error_reporting(adev->pdev);
3560 /* Post card if necessary */
3561 if (amdgpu_device_need_post(adev)) {
3563 dev_err(adev->dev, "no vBIOS found\n");
3567 DRM_INFO("GPU posting now...\n");
3568 r = amdgpu_device_asic_init(adev);
3570 dev_err(adev->dev, "gpu post error!\n");
3575 if (adev->is_atom_fw) {
3576 /* Initialize clocks */
3577 r = amdgpu_atomfirmware_get_clock_info(adev);
3579 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
3580 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3584 /* Initialize clocks */
3585 r = amdgpu_atombios_get_clock_info(adev);
3587 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
3588 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3591 /* init i2c buses */
3592 if (!amdgpu_device_has_dc_support(adev))
3593 amdgpu_atombios_i2c_init(adev);
3598 r = amdgpu_fence_driver_init(adev);
3600 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
3601 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
3605 /* init the mode config */
3606 drm_mode_config_init(adev_to_drm(adev));
3608 r = amdgpu_device_ip_init(adev);
3610 /* failed in exclusive mode due to timeout */
3611 if (amdgpu_sriov_vf(adev) &&
3612 !amdgpu_sriov_runtime(adev) &&
3613 amdgpu_virt_mmio_blocked(adev) &&
3614 !amdgpu_virt_wait_reset(adev)) {
3615 dev_err(adev->dev, "VF exclusive mode timeout\n");
3616 /* Don't send request since VF is inactive. */
3617 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
3618 adev->virt.ops = NULL;
3620 goto release_ras_con;
3622 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
3623 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
3624 goto release_ras_con;
3628 "SE %d, SH per SE %d, CU per SH %d, active_cu_number %d\n",
3629 adev->gfx.config.max_shader_engines,
3630 adev->gfx.config.max_sh_per_se,
3631 adev->gfx.config.max_cu_per_sh,
3632 adev->gfx.cu_info.number);
3634 adev->accel_working = true;
3636 amdgpu_vm_check_compute_bug(adev);
3638 /* Initialize the buffer migration limit. */
3639 if (amdgpu_moverate >= 0)
3640 max_MBps = amdgpu_moverate;
3642 max_MBps = 8; /* Allow 8 MB/s. */
3643 /* Get a log2 for easy divisions. */
3644 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
3646 amdgpu_fbdev_init(adev);
3648 r = amdgpu_pm_sysfs_init(adev);
3650 adev->pm_sysfs_en = false;
3651 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
3653 adev->pm_sysfs_en = true;
3655 r = amdgpu_ucode_sysfs_init(adev);
3657 adev->ucode_sysfs_en = false;
3658 DRM_ERROR("Creating firmware sysfs failed (%d).\n", r);
3660 adev->ucode_sysfs_en = true;
3662 if ((amdgpu_testing & 1)) {
3663 if (adev->accel_working)
3664 amdgpu_test_moves(adev);
3666 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
3668 if (amdgpu_benchmarking) {
3669 if (adev->accel_working)
3670 amdgpu_benchmark(adev, amdgpu_benchmarking);
3672 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
3676 * Register gpu instance before amdgpu_device_enable_mgpu_fan_boost.
3677 * Otherwise the mgpu fan boost feature will be skipped due to the
3678 * gpu instance is counted less.
3680 amdgpu_register_gpu_instance(adev);
3682 /* enable clockgating, etc. after ib tests, etc. since some blocks require
3683 * explicit gating rather than handling it automatically.
3685 if (!adev->gmc.xgmi.pending_reset) {
3686 r = amdgpu_device_ip_late_init(adev);
3688 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
3689 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
3690 goto release_ras_con;
3693 amdgpu_ras_resume(adev);
3694 queue_delayed_work(system_wq, &adev->delayed_init_work,
3695 msecs_to_jiffies(AMDGPU_RESUME_MS));
3698 if (amdgpu_sriov_vf(adev))
3699 flush_delayed_work(&adev->delayed_init_work);
3701 r = sysfs_create_files(&adev->dev->kobj, amdgpu_dev_attributes);
3703 dev_err(adev->dev, "Could not create amdgpu device attr\n");
3705 if (IS_ENABLED(CONFIG_PERF_EVENTS))
3706 r = amdgpu_pmu_init(adev);
3708 dev_err(adev->dev, "amdgpu_pmu_init failed\n");
3710 /* Have stored pci confspace at hand for restore in sudden PCI error */
3711 if (amdgpu_device_cache_pci_state(adev->pdev))
3712 pci_restore_state(pdev);
3714 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
3715 /* this will fail for cards that aren't VGA class devices, just
3717 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
3718 vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
3720 if (amdgpu_device_supports_px(ddev)) {
3722 vga_switcheroo_register_client(adev->pdev,
3723 &amdgpu_switcheroo_ops, px);
3724 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
3727 if (adev->gmc.xgmi.pending_reset)
3728 queue_delayed_work(system_wq, &mgpu_info.delayed_reset_work,
3729 msecs_to_jiffies(AMDGPU_RESUME_MS));
3734 amdgpu_release_ras_context(adev);
3737 amdgpu_vf_error_trans_all(adev);
3742 static void amdgpu_device_unmap_mmio(struct amdgpu_device *adev)
3744 /* Clear all CPU mappings pointing to this device */
3745 unmap_mapping_range(adev->ddev.anon_inode->i_mapping, 0, 0, 1);
3747 /* Unmap all mapped bars - Doorbell, registers and VRAM */
3748 amdgpu_device_doorbell_fini(adev);
3750 iounmap(adev->rmmio);
3752 if (adev->mman.aper_base_kaddr)
3753 iounmap(adev->mman.aper_base_kaddr);
3754 adev->mman.aper_base_kaddr = NULL;
3756 /* Memory manager related */
3757 if (!adev->gmc.xgmi.connected_to_cpu) {
3758 arch_phys_wc_del(adev->gmc.vram_mtrr);
3759 arch_io_free_memtype_wc(adev->gmc.aper_base, adev->gmc.aper_size);
3764 * amdgpu_device_fini - tear down the driver
3766 * @adev: amdgpu_device pointer
3768 * Tear down the driver info (all asics).
3769 * Called at driver shutdown.
3771 void amdgpu_device_fini_hw(struct amdgpu_device *adev)
3773 dev_info(adev->dev, "amdgpu: finishing device.\n");
3774 flush_delayed_work(&adev->delayed_init_work);
3775 ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
3776 adev->shutdown = true;
3778 /* make sure IB test finished before entering exclusive mode
3779 * to avoid preemption on IB test
3781 if (amdgpu_sriov_vf(adev)) {
3782 amdgpu_virt_request_full_gpu(adev, false);
3783 amdgpu_virt_fini_data_exchange(adev);
3786 /* disable all interrupts */
3787 amdgpu_irq_disable_all(adev);
3788 if (adev->mode_info.mode_config_initialized){
3789 if (!amdgpu_device_has_dc_support(adev))
3790 drm_helper_force_disable_all(adev_to_drm(adev));
3792 drm_atomic_helper_shutdown(adev_to_drm(adev));
3794 amdgpu_fence_driver_fini_hw(adev);
3796 if (adev->pm_sysfs_en)
3797 amdgpu_pm_sysfs_fini(adev);
3798 if (adev->ucode_sysfs_en)
3799 amdgpu_ucode_sysfs_fini(adev);
3800 sysfs_remove_files(&adev->dev->kobj, amdgpu_dev_attributes);
3802 amdgpu_fbdev_fini(adev);
3804 amdgpu_irq_fini_hw(adev);
3806 amdgpu_device_ip_fini_early(adev);
3808 amdgpu_gart_dummy_page_fini(adev);
3810 amdgpu_device_unmap_mmio(adev);
3813 void amdgpu_device_fini_sw(struct amdgpu_device *adev)
3815 amdgpu_device_ip_fini(adev);
3816 amdgpu_fence_driver_fini_sw(adev);
3817 release_firmware(adev->firmware.gpu_info_fw);
3818 adev->firmware.gpu_info_fw = NULL;
3819 adev->accel_working = false;
3821 amdgpu_reset_fini(adev);
3823 /* free i2c buses */
3824 if (!amdgpu_device_has_dc_support(adev))
3825 amdgpu_i2c_fini(adev);
3827 if (amdgpu_emu_mode != 1)
3828 amdgpu_atombios_fini(adev);
3832 if (amdgpu_device_supports_px(adev_to_drm(adev))) {
3833 vga_switcheroo_unregister_client(adev->pdev);
3834 vga_switcheroo_fini_domain_pm_ops(adev->dev);
3836 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
3837 vga_client_register(adev->pdev, NULL, NULL, NULL);
3839 if (IS_ENABLED(CONFIG_PERF_EVENTS))
3840 amdgpu_pmu_fini(adev);
3841 if (adev->mman.discovery_bin)
3842 amdgpu_discovery_fini(adev);
3844 kfree(adev->pci_state);
3853 * amdgpu_device_suspend - initiate device suspend
3855 * @dev: drm dev pointer
3856 * @fbcon : notify the fbdev of suspend
3858 * Puts the hw in the suspend state (all asics).
3859 * Returns 0 for success or an error on failure.
3860 * Called at driver suspend.
3862 int amdgpu_device_suspend(struct drm_device *dev, bool fbcon)
3864 struct amdgpu_device *adev = drm_to_adev(dev);
3866 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
3869 adev->in_suspend = true;
3871 if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DEV_D3))
3872 DRM_WARN("smart shift update failed\n");
3874 drm_kms_helper_poll_disable(dev);
3877 amdgpu_fbdev_set_suspend(adev, 1);
3879 cancel_delayed_work_sync(&adev->delayed_init_work);
3881 amdgpu_ras_suspend(adev);
3883 amdgpu_device_ip_suspend_phase1(adev);
3886 amdgpu_amdkfd_suspend(adev, adev->in_runpm);
3888 /* evict vram memory */
3889 amdgpu_bo_evict_vram(adev);
3891 amdgpu_fence_driver_suspend(adev);
3893 amdgpu_device_ip_suspend_phase2(adev);
3894 /* evict remaining vram memory
3895 * This second call to evict vram is to evict the gart page table
3898 amdgpu_bo_evict_vram(adev);
3904 * amdgpu_device_resume - initiate device resume
3906 * @dev: drm dev pointer
3907 * @fbcon : notify the fbdev of resume
3909 * Bring the hw back to operating state (all asics).
3910 * Returns 0 for success or an error on failure.
3911 * Called at driver resume.
3913 int amdgpu_device_resume(struct drm_device *dev, bool fbcon)
3915 struct amdgpu_device *adev = drm_to_adev(dev);
3918 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
3922 amdgpu_gfx_state_change_set(adev, sGpuChangeState_D0Entry);
3925 if (amdgpu_device_need_post(adev)) {
3926 r = amdgpu_device_asic_init(adev);
3928 dev_err(adev->dev, "amdgpu asic init failed\n");
3931 r = amdgpu_device_ip_resume(adev);
3933 dev_err(adev->dev, "amdgpu_device_ip_resume failed (%d).\n", r);
3936 amdgpu_fence_driver_resume(adev);
3939 r = amdgpu_device_ip_late_init(adev);
3943 queue_delayed_work(system_wq, &adev->delayed_init_work,
3944 msecs_to_jiffies(AMDGPU_RESUME_MS));
3946 if (!adev->in_s0ix) {
3947 r = amdgpu_amdkfd_resume(adev, adev->in_runpm);
3952 /* Make sure IB tests flushed */
3953 flush_delayed_work(&adev->delayed_init_work);
3956 amdgpu_fbdev_set_suspend(adev, 0);
3958 drm_kms_helper_poll_enable(dev);
3960 amdgpu_ras_resume(adev);
3963 * Most of the connector probing functions try to acquire runtime pm
3964 * refs to ensure that the GPU is powered on when connector polling is
3965 * performed. Since we're calling this from a runtime PM callback,
3966 * trying to acquire rpm refs will cause us to deadlock.
3968 * Since we're guaranteed to be holding the rpm lock, it's safe to
3969 * temporarily disable the rpm helpers so this doesn't deadlock us.
3972 dev->dev->power.disable_depth++;
3974 if (!amdgpu_device_has_dc_support(adev))
3975 drm_helper_hpd_irq_event(dev);
3977 drm_kms_helper_hotplug_event(dev);
3979 dev->dev->power.disable_depth--;
3981 adev->in_suspend = false;
3983 if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DEV_D0))
3984 DRM_WARN("smart shift update failed\n");
3990 * amdgpu_device_ip_check_soft_reset - did soft reset succeed
3992 * @adev: amdgpu_device pointer
3994 * The list of all the hardware IPs that make up the asic is walked and
3995 * the check_soft_reset callbacks are run. check_soft_reset determines
3996 * if the asic is still hung or not.
3997 * Returns true if any of the IPs are still in a hung state, false if not.
3999 static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
4002 bool asic_hang = false;
4004 if (amdgpu_sriov_vf(adev))
4007 if (amdgpu_asic_need_full_reset(adev))
4010 for (i = 0; i < adev->num_ip_blocks; i++) {
4011 if (!adev->ip_blocks[i].status.valid)
4013 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
4014 adev->ip_blocks[i].status.hang =
4015 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
4016 if (adev->ip_blocks[i].status.hang) {
4017 dev_info(adev->dev, "IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
4025 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
4027 * @adev: amdgpu_device pointer
4029 * The list of all the hardware IPs that make up the asic is walked and the
4030 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
4031 * handles any IP specific hardware or software state changes that are
4032 * necessary for a soft reset to succeed.
4033 * Returns 0 on success, negative error code on failure.
4035 static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
4039 for (i = 0; i < adev->num_ip_blocks; i++) {
4040 if (!adev->ip_blocks[i].status.valid)
4042 if (adev->ip_blocks[i].status.hang &&
4043 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
4044 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
4054 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
4056 * @adev: amdgpu_device pointer
4058 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
4059 * reset is necessary to recover.
4060 * Returns true if a full asic reset is required, false if not.
4062 static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
4066 if (amdgpu_asic_need_full_reset(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].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
4073 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
4074 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
4075 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
4076 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
4077 if (adev->ip_blocks[i].status.hang) {
4078 dev_info(adev->dev, "Some block need full reset!\n");
4087 * amdgpu_device_ip_soft_reset - do a soft reset
4089 * @adev: amdgpu_device pointer
4091 * The list of all the hardware IPs that make up the asic is walked and the
4092 * soft_reset callbacks are run if the block is hung. soft_reset handles any
4093 * IP specific hardware or software state changes that are necessary to soft
4095 * Returns 0 on success, negative error code on failure.
4097 static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
4101 for (i = 0; i < adev->num_ip_blocks; i++) {
4102 if (!adev->ip_blocks[i].status.valid)
4104 if (adev->ip_blocks[i].status.hang &&
4105 adev->ip_blocks[i].version->funcs->soft_reset) {
4106 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
4116 * amdgpu_device_ip_post_soft_reset - clean up from soft reset
4118 * @adev: amdgpu_device pointer
4120 * The list of all the hardware IPs that make up the asic is walked and the
4121 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
4122 * handles any IP specific hardware or software state changes that are
4123 * necessary after the IP has been soft reset.
4124 * Returns 0 on success, negative error code on failure.
4126 static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
4130 for (i = 0; i < adev->num_ip_blocks; i++) {
4131 if (!adev->ip_blocks[i].status.valid)
4133 if (adev->ip_blocks[i].status.hang &&
4134 adev->ip_blocks[i].version->funcs->post_soft_reset)
4135 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
4144 * amdgpu_device_recover_vram - Recover some VRAM contents
4146 * @adev: amdgpu_device pointer
4148 * Restores the contents of VRAM buffers from the shadows in GTT. Used to
4149 * restore things like GPUVM page tables after a GPU reset where
4150 * the contents of VRAM might be lost.
4153 * 0 on success, negative error code on failure.
4155 static int amdgpu_device_recover_vram(struct amdgpu_device *adev)
4157 struct dma_fence *fence = NULL, *next = NULL;
4158 struct amdgpu_bo *shadow;
4159 struct amdgpu_bo_vm *vmbo;
4162 if (amdgpu_sriov_runtime(adev))
4163 tmo = msecs_to_jiffies(8000);
4165 tmo = msecs_to_jiffies(100);
4167 dev_info(adev->dev, "recover vram bo from shadow start\n");
4168 mutex_lock(&adev->shadow_list_lock);
4169 list_for_each_entry(vmbo, &adev->shadow_list, shadow_list) {
4171 /* No need to recover an evicted BO */
4172 if (shadow->tbo.resource->mem_type != TTM_PL_TT ||
4173 shadow->tbo.resource->start == AMDGPU_BO_INVALID_OFFSET ||
4174 shadow->parent->tbo.resource->mem_type != TTM_PL_VRAM)
4177 r = amdgpu_bo_restore_shadow(shadow, &next);
4182 tmo = dma_fence_wait_timeout(fence, false, tmo);
4183 dma_fence_put(fence);
4188 } else if (tmo < 0) {
4196 mutex_unlock(&adev->shadow_list_lock);
4199 tmo = dma_fence_wait_timeout(fence, false, tmo);
4200 dma_fence_put(fence);
4202 if (r < 0 || tmo <= 0) {
4203 dev_err(adev->dev, "recover vram bo from shadow failed, r is %ld, tmo is %ld\n", r, tmo);
4207 dev_info(adev->dev, "recover vram bo from shadow done\n");
4213 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
4215 * @adev: amdgpu_device pointer
4216 * @from_hypervisor: request from hypervisor
4218 * do VF FLR and reinitialize Asic
4219 * return 0 means succeeded otherwise failed
4221 static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
4222 bool from_hypervisor)
4226 if (from_hypervisor)
4227 r = amdgpu_virt_request_full_gpu(adev, true);
4229 r = amdgpu_virt_reset_gpu(adev);
4233 amdgpu_amdkfd_pre_reset(adev);
4235 /* Resume IP prior to SMC */
4236 r = amdgpu_device_ip_reinit_early_sriov(adev);
4240 amdgpu_virt_init_data_exchange(adev);
4241 /* we need recover gart prior to run SMC/CP/SDMA resume */
4242 amdgpu_gtt_mgr_recover(ttm_manager_type(&adev->mman.bdev, TTM_PL_TT));
4244 r = amdgpu_device_fw_loading(adev);
4248 /* now we are okay to resume SMC/CP/SDMA */
4249 r = amdgpu_device_ip_reinit_late_sriov(adev);
4253 amdgpu_irq_gpu_reset_resume_helper(adev);
4254 r = amdgpu_ib_ring_tests(adev);
4255 amdgpu_amdkfd_post_reset(adev);
4258 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
4259 amdgpu_inc_vram_lost(adev);
4260 r = amdgpu_device_recover_vram(adev);
4262 amdgpu_virt_release_full_gpu(adev, true);
4268 * amdgpu_device_has_job_running - check if there is any job in mirror list
4270 * @adev: amdgpu_device pointer
4272 * check if there is any job in mirror list
4274 bool amdgpu_device_has_job_running(struct amdgpu_device *adev)
4277 struct drm_sched_job *job;
4279 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4280 struct amdgpu_ring *ring = adev->rings[i];
4282 if (!ring || !ring->sched.thread)
4285 spin_lock(&ring->sched.job_list_lock);
4286 job = list_first_entry_or_null(&ring->sched.pending_list,
4287 struct drm_sched_job, list);
4288 spin_unlock(&ring->sched.job_list_lock);
4296 * amdgpu_device_should_recover_gpu - check if we should try GPU recovery
4298 * @adev: amdgpu_device pointer
4300 * Check amdgpu_gpu_recovery and SRIOV status to see if we should try to recover
4303 bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev)
4305 if (!amdgpu_device_ip_check_soft_reset(adev)) {
4306 dev_info(adev->dev, "Timeout, but no hardware hang detected.\n");
4310 if (amdgpu_gpu_recovery == 0)
4313 if (amdgpu_sriov_vf(adev))
4316 if (amdgpu_gpu_recovery == -1) {
4317 switch (adev->asic_type) {
4323 case CHIP_POLARIS10:
4324 case CHIP_POLARIS11:
4325 case CHIP_POLARIS12:
4336 case CHIP_SIENNA_CICHLID:
4337 case CHIP_NAVY_FLOUNDER:
4338 case CHIP_DIMGREY_CAVEFISH:
4339 case CHIP_BEIGE_GOBY:
4341 case CHIP_ALDEBARAN:
4351 dev_info(adev->dev, "GPU recovery disabled.\n");
4355 int amdgpu_device_mode1_reset(struct amdgpu_device *adev)
4360 amdgpu_atombios_scratch_regs_engine_hung(adev, true);
4362 dev_info(adev->dev, "GPU mode1 reset\n");
4365 pci_clear_master(adev->pdev);
4367 amdgpu_device_cache_pci_state(adev->pdev);
4369 if (amdgpu_dpm_is_mode1_reset_supported(adev)) {
4370 dev_info(adev->dev, "GPU smu mode1 reset\n");
4371 ret = amdgpu_dpm_mode1_reset(adev);
4373 dev_info(adev->dev, "GPU psp mode1 reset\n");
4374 ret = psp_gpu_reset(adev);
4378 dev_err(adev->dev, "GPU mode1 reset failed\n");
4380 amdgpu_device_load_pci_state(adev->pdev);
4382 /* wait for asic to come out of reset */
4383 for (i = 0; i < adev->usec_timeout; i++) {
4384 u32 memsize = adev->nbio.funcs->get_memsize(adev);
4386 if (memsize != 0xffffffff)
4391 amdgpu_atombios_scratch_regs_engine_hung(adev, false);
4395 int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
4396 struct amdgpu_reset_context *reset_context)
4399 struct amdgpu_job *job = NULL;
4400 bool need_full_reset =
4401 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4403 if (reset_context->reset_req_dev == adev)
4404 job = reset_context->job;
4406 /* no need to dump if device is not in good state during probe period */
4407 if (!adev->gmc.xgmi.pending_reset)
4408 amdgpu_debugfs_wait_dump(adev);
4410 if (amdgpu_sriov_vf(adev)) {
4411 /* stop the data exchange thread */
4412 amdgpu_virt_fini_data_exchange(adev);
4415 /* block all schedulers and reset given job's ring */
4416 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4417 struct amdgpu_ring *ring = adev->rings[i];
4419 if (!ring || !ring->sched.thread)
4422 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
4423 amdgpu_fence_driver_force_completion(ring);
4427 drm_sched_increase_karma(&job->base);
4429 r = amdgpu_reset_prepare_hwcontext(adev, reset_context);
4430 /* If reset handler not implemented, continue; otherwise return */
4436 /* Don't suspend on bare metal if we are not going to HW reset the ASIC */
4437 if (!amdgpu_sriov_vf(adev)) {
4439 if (!need_full_reset)
4440 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
4442 if (!need_full_reset) {
4443 amdgpu_device_ip_pre_soft_reset(adev);
4444 r = amdgpu_device_ip_soft_reset(adev);
4445 amdgpu_device_ip_post_soft_reset(adev);
4446 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
4447 dev_info(adev->dev, "soft reset failed, will fallback to full reset!\n");
4448 need_full_reset = true;
4452 if (need_full_reset)
4453 r = amdgpu_device_ip_suspend(adev);
4454 if (need_full_reset)
4455 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4457 clear_bit(AMDGPU_NEED_FULL_RESET,
4458 &reset_context->flags);
4464 int amdgpu_do_asic_reset(struct list_head *device_list_handle,
4465 struct amdgpu_reset_context *reset_context)
4467 struct amdgpu_device *tmp_adev = NULL;
4468 bool need_full_reset, skip_hw_reset, vram_lost = false;
4471 /* Try reset handler method first */
4472 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
4474 r = amdgpu_reset_perform_reset(tmp_adev, reset_context);
4475 /* If reset handler not implemented, continue; otherwise return */
4481 /* Reset handler not implemented, use the default method */
4483 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4484 skip_hw_reset = test_bit(AMDGPU_SKIP_HW_RESET, &reset_context->flags);
4487 * ASIC reset has to be done on all XGMI hive nodes ASAP
4488 * to allow proper links negotiation in FW (within 1 sec)
4490 if (!skip_hw_reset && need_full_reset) {
4491 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4492 /* For XGMI run all resets in parallel to speed up the process */
4493 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
4494 tmp_adev->gmc.xgmi.pending_reset = false;
4495 if (!queue_work(system_unbound_wq, &tmp_adev->xgmi_reset_work))
4498 r = amdgpu_asic_reset(tmp_adev);
4501 dev_err(tmp_adev->dev, "ASIC reset failed with error, %d for drm dev, %s",
4502 r, adev_to_drm(tmp_adev)->unique);
4507 /* For XGMI wait for all resets to complete before proceed */
4509 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4510 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
4511 flush_work(&tmp_adev->xgmi_reset_work);
4512 r = tmp_adev->asic_reset_res;
4520 if (!r && amdgpu_ras_intr_triggered()) {
4521 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4522 if (tmp_adev->mmhub.ras_funcs &&
4523 tmp_adev->mmhub.ras_funcs->reset_ras_error_count)
4524 tmp_adev->mmhub.ras_funcs->reset_ras_error_count(tmp_adev);
4527 amdgpu_ras_intr_cleared();
4530 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4531 if (need_full_reset) {
4533 r = amdgpu_device_asic_init(tmp_adev);
4535 dev_warn(tmp_adev->dev, "asic atom init failed!");
4537 dev_info(tmp_adev->dev, "GPU reset succeeded, trying to resume\n");
4538 r = amdgpu_device_ip_resume_phase1(tmp_adev);
4542 vram_lost = amdgpu_device_check_vram_lost(tmp_adev);
4544 DRM_INFO("VRAM is lost due to GPU reset!\n");
4545 amdgpu_inc_vram_lost(tmp_adev);
4548 r = amdgpu_gtt_mgr_recover(ttm_manager_type(&tmp_adev->mman.bdev, TTM_PL_TT));
4552 r = amdgpu_device_fw_loading(tmp_adev);
4556 r = amdgpu_device_ip_resume_phase2(tmp_adev);
4561 amdgpu_device_fill_reset_magic(tmp_adev);
4564 * Add this ASIC as tracked as reset was already
4565 * complete successfully.
4567 amdgpu_register_gpu_instance(tmp_adev);
4569 if (!reset_context->hive &&
4570 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
4571 amdgpu_xgmi_add_device(tmp_adev);
4573 r = amdgpu_device_ip_late_init(tmp_adev);
4577 amdgpu_fbdev_set_suspend(tmp_adev, 0);
4580 * The GPU enters bad state once faulty pages
4581 * by ECC has reached the threshold, and ras
4582 * recovery is scheduled next. So add one check
4583 * here to break recovery if it indeed exceeds
4584 * bad page threshold, and remind user to
4585 * retire this GPU or setting one bigger
4586 * bad_page_threshold value to fix this once
4587 * probing driver again.
4589 if (!amdgpu_ras_eeprom_check_err_threshold(tmp_adev)) {
4591 amdgpu_ras_resume(tmp_adev);
4597 /* Update PSP FW topology after reset */
4598 if (reset_context->hive &&
4599 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
4600 r = amdgpu_xgmi_update_topology(
4601 reset_context->hive, tmp_adev);
4607 amdgpu_irq_gpu_reset_resume_helper(tmp_adev);
4608 r = amdgpu_ib_ring_tests(tmp_adev);
4610 dev_err(tmp_adev->dev, "ib ring test failed (%d).\n", r);
4611 need_full_reset = true;
4618 r = amdgpu_device_recover_vram(tmp_adev);
4620 tmp_adev->asic_reset_res = r;
4624 if (need_full_reset)
4625 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4627 clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4631 static bool amdgpu_device_lock_adev(struct amdgpu_device *adev,
4632 struct amdgpu_hive_info *hive)
4634 if (atomic_cmpxchg(&adev->in_gpu_reset, 0, 1) != 0)
4638 down_write_nest_lock(&adev->reset_sem, &hive->hive_lock);
4640 down_write(&adev->reset_sem);
4643 switch (amdgpu_asic_reset_method(adev)) {
4644 case AMD_RESET_METHOD_MODE1:
4645 adev->mp1_state = PP_MP1_STATE_SHUTDOWN;
4647 case AMD_RESET_METHOD_MODE2:
4648 adev->mp1_state = PP_MP1_STATE_RESET;
4651 adev->mp1_state = PP_MP1_STATE_NONE;
4658 static void amdgpu_device_unlock_adev(struct amdgpu_device *adev)
4660 amdgpu_vf_error_trans_all(adev);
4661 adev->mp1_state = PP_MP1_STATE_NONE;
4662 atomic_set(&adev->in_gpu_reset, 0);
4663 up_write(&adev->reset_sem);
4667 * to lockup a list of amdgpu devices in a hive safely, if not a hive
4668 * with multiple nodes, it will be similar as amdgpu_device_lock_adev.
4670 * unlock won't require roll back.
4672 static int amdgpu_device_lock_hive_adev(struct amdgpu_device *adev, struct amdgpu_hive_info *hive)
4674 struct amdgpu_device *tmp_adev = NULL;
4676 if (adev->gmc.xgmi.num_physical_nodes > 1) {
4678 dev_err(adev->dev, "Hive is NULL while device has multiple xgmi nodes");
4681 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
4682 if (!amdgpu_device_lock_adev(tmp_adev, hive))
4685 } else if (!amdgpu_device_lock_adev(adev, hive))
4690 if (!list_is_first(&tmp_adev->gmc.xgmi.head, &hive->device_list)) {
4692 * if the lockup iteration break in the middle of a hive,
4693 * it may means there may has a race issue,
4694 * or a hive device locked up independently.
4695 * we may be in trouble and may not, so will try to roll back
4696 * the lock and give out a warnning.
4698 dev_warn(tmp_adev->dev, "Hive lock iteration broke in the middle. Rolling back to unlock");
4699 list_for_each_entry_continue_reverse(tmp_adev, &hive->device_list, gmc.xgmi.head) {
4700 amdgpu_device_unlock_adev(tmp_adev);
4706 static void amdgpu_device_resume_display_audio(struct amdgpu_device *adev)
4708 struct pci_dev *p = NULL;
4710 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
4711 adev->pdev->bus->number, 1);
4713 pm_runtime_enable(&(p->dev));
4714 pm_runtime_resume(&(p->dev));
4718 static int amdgpu_device_suspend_display_audio(struct amdgpu_device *adev)
4720 enum amd_reset_method reset_method;
4721 struct pci_dev *p = NULL;
4725 * For now, only BACO and mode1 reset are confirmed
4726 * to suffer the audio issue without proper suspended.
4728 reset_method = amdgpu_asic_reset_method(adev);
4729 if ((reset_method != AMD_RESET_METHOD_BACO) &&
4730 (reset_method != AMD_RESET_METHOD_MODE1))
4733 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
4734 adev->pdev->bus->number, 1);
4738 expires = pm_runtime_autosuspend_expiration(&(p->dev));
4741 * If we cannot get the audio device autosuspend delay,
4742 * a fixed 4S interval will be used. Considering 3S is
4743 * the audio controller default autosuspend delay setting.
4744 * 4S used here is guaranteed to cover that.
4746 expires = ktime_get_mono_fast_ns() + NSEC_PER_SEC * 4ULL;
4748 while (!pm_runtime_status_suspended(&(p->dev))) {
4749 if (!pm_runtime_suspend(&(p->dev)))
4752 if (expires < ktime_get_mono_fast_ns()) {
4753 dev_warn(adev->dev, "failed to suspend display audio\n");
4754 /* TODO: abort the succeeding gpu reset? */
4759 pm_runtime_disable(&(p->dev));
4764 static void amdgpu_device_recheck_guilty_jobs(
4765 struct amdgpu_device *adev, struct list_head *device_list_handle,
4766 struct amdgpu_reset_context *reset_context)
4770 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4771 struct amdgpu_ring *ring = adev->rings[i];
4773 struct drm_sched_job *s_job;
4775 if (!ring || !ring->sched.thread)
4778 s_job = list_first_entry_or_null(&ring->sched.pending_list,
4779 struct drm_sched_job, list);
4783 /* clear job's guilty and depend the folowing step to decide the real one */
4784 drm_sched_reset_karma(s_job);
4785 drm_sched_resubmit_jobs_ext(&ring->sched, 1);
4787 ret = dma_fence_wait_timeout(s_job->s_fence->parent, false, ring->sched.timeout);
4788 if (ret == 0) { /* timeout */
4789 DRM_ERROR("Found the real bad job! ring:%s, job_id:%llx\n",
4790 ring->sched.name, s_job->id);
4793 drm_sched_increase_karma(s_job);
4796 if (amdgpu_sriov_vf(adev)) {
4797 amdgpu_virt_fini_data_exchange(adev);
4798 r = amdgpu_device_reset_sriov(adev, false);
4800 adev->asic_reset_res = r;
4802 clear_bit(AMDGPU_SKIP_HW_RESET,
4803 &reset_context->flags);
4804 r = amdgpu_do_asic_reset(device_list_handle,
4806 if (r && r == -EAGAIN)
4811 * add reset counter so that the following
4812 * resubmitted job could flush vmid
4814 atomic_inc(&adev->gpu_reset_counter);
4818 /* got the hw fence, signal finished fence */
4819 atomic_dec(ring->sched.score);
4820 dma_fence_get(&s_job->s_fence->finished);
4821 dma_fence_signal(&s_job->s_fence->finished);
4822 dma_fence_put(&s_job->s_fence->finished);
4824 /* remove node from list and free the job */
4825 spin_lock(&ring->sched.job_list_lock);
4826 list_del_init(&s_job->list);
4827 spin_unlock(&ring->sched.job_list_lock);
4828 ring->sched.ops->free_job(s_job);
4833 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
4835 * @adev: amdgpu_device pointer
4836 * @job: which job trigger hang
4838 * Attempt to reset the GPU if it has hung (all asics).
4839 * Attempt to do soft-reset or full-reset and reinitialize Asic
4840 * Returns 0 for success or an error on failure.
4843 int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
4844 struct amdgpu_job *job)
4846 struct list_head device_list, *device_list_handle = NULL;
4847 bool job_signaled = false;
4848 struct amdgpu_hive_info *hive = NULL;
4849 struct amdgpu_device *tmp_adev = NULL;
4851 bool need_emergency_restart = false;
4852 bool audio_suspended = false;
4853 int tmp_vram_lost_counter;
4854 struct amdgpu_reset_context reset_context;
4856 memset(&reset_context, 0, sizeof(reset_context));
4859 * Special case: RAS triggered and full reset isn't supported
4861 need_emergency_restart = amdgpu_ras_need_emergency_restart(adev);
4864 * Flush RAM to disk so that after reboot
4865 * the user can read log and see why the system rebooted.
4867 if (need_emergency_restart && amdgpu_ras_get_context(adev)->reboot) {
4868 DRM_WARN("Emergency reboot.");
4871 emergency_restart();
4874 dev_info(adev->dev, "GPU %s begin!\n",
4875 need_emergency_restart ? "jobs stop":"reset");
4878 * Here we trylock to avoid chain of resets executing from
4879 * either trigger by jobs on different adevs in XGMI hive or jobs on
4880 * different schedulers for same device while this TO handler is running.
4881 * We always reset all schedulers for device and all devices for XGMI
4882 * hive so that should take care of them too.
4884 hive = amdgpu_get_xgmi_hive(adev);
4886 if (atomic_cmpxchg(&hive->in_reset, 0, 1) != 0) {
4887 DRM_INFO("Bailing on TDR for s_job:%llx, hive: %llx as another already in progress",
4888 job ? job->base.id : -1, hive->hive_id);
4889 amdgpu_put_xgmi_hive(hive);
4891 drm_sched_increase_karma(&job->base);
4894 mutex_lock(&hive->hive_lock);
4897 reset_context.method = AMD_RESET_METHOD_NONE;
4898 reset_context.reset_req_dev = adev;
4899 reset_context.job = job;
4900 reset_context.hive = hive;
4901 clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
4904 * lock the device before we try to operate the linked list
4905 * if didn't get the device lock, don't touch the linked list since
4906 * others may iterating it.
4908 r = amdgpu_device_lock_hive_adev(adev, hive);
4910 dev_info(adev->dev, "Bailing on TDR for s_job:%llx, as another already in progress",
4911 job ? job->base.id : -1);
4913 /* even we skipped this reset, still need to set the job to guilty */
4915 drm_sched_increase_karma(&job->base);
4920 * Build list of devices to reset.
4921 * In case we are in XGMI hive mode, resort the device list
4922 * to put adev in the 1st position.
4924 INIT_LIST_HEAD(&device_list);
4925 if (adev->gmc.xgmi.num_physical_nodes > 1) {
4926 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head)
4927 list_add_tail(&tmp_adev->reset_list, &device_list);
4928 if (!list_is_first(&adev->reset_list, &device_list))
4929 list_rotate_to_front(&adev->reset_list, &device_list);
4930 device_list_handle = &device_list;
4932 list_add_tail(&adev->reset_list, &device_list);
4933 device_list_handle = &device_list;
4936 /* block all schedulers and reset given job's ring */
4937 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4939 * Try to put the audio codec into suspend state
4940 * before gpu reset started.
4942 * Due to the power domain of the graphics device
4943 * is shared with AZ power domain. Without this,
4944 * we may change the audio hardware from behind
4945 * the audio driver's back. That will trigger
4946 * some audio codec errors.
4948 if (!amdgpu_device_suspend_display_audio(tmp_adev))
4949 audio_suspended = true;
4951 amdgpu_ras_set_error_query_ready(tmp_adev, false);
4953 cancel_delayed_work_sync(&tmp_adev->delayed_init_work);
4955 if (!amdgpu_sriov_vf(tmp_adev))
4956 amdgpu_amdkfd_pre_reset(tmp_adev);
4959 * Mark these ASICs to be reseted as untracked first
4960 * And add them back after reset completed
4962 amdgpu_unregister_gpu_instance(tmp_adev);
4964 amdgpu_fbdev_set_suspend(tmp_adev, 1);
4966 /* disable ras on ALL IPs */
4967 if (!need_emergency_restart &&
4968 amdgpu_device_ip_need_full_reset(tmp_adev))
4969 amdgpu_ras_suspend(tmp_adev);
4971 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4972 struct amdgpu_ring *ring = tmp_adev->rings[i];
4974 if (!ring || !ring->sched.thread)
4977 drm_sched_stop(&ring->sched, job ? &job->base : NULL);
4979 if (need_emergency_restart)
4980 amdgpu_job_stop_all_jobs_on_sched(&ring->sched);
4982 atomic_inc(&tmp_adev->gpu_reset_counter);
4985 if (need_emergency_restart)
4986 goto skip_sched_resume;
4989 * Must check guilty signal here since after this point all old
4990 * HW fences are force signaled.
4992 * job->base holds a reference to parent fence
4994 if (job && job->base.s_fence->parent &&
4995 dma_fence_is_signaled(job->base.s_fence->parent)) {
4996 job_signaled = true;
4997 dev_info(adev->dev, "Guilty job already signaled, skipping HW reset");
5001 retry: /* Rest of adevs pre asic reset from XGMI hive. */
5002 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5003 r = amdgpu_device_pre_asic_reset(tmp_adev, &reset_context);
5004 /*TODO Should we stop ?*/
5006 dev_err(tmp_adev->dev, "GPU pre asic reset failed with err, %d for drm dev, %s ",
5007 r, adev_to_drm(tmp_adev)->unique);
5008 tmp_adev->asic_reset_res = r;
5012 tmp_vram_lost_counter = atomic_read(&((adev)->vram_lost_counter));
5013 /* Actual ASIC resets if needed.*/
5014 /* TODO Implement XGMI hive reset logic for SRIOV */
5015 if (amdgpu_sriov_vf(adev)) {
5016 r = amdgpu_device_reset_sriov(adev, job ? false : true);
5018 adev->asic_reset_res = r;
5020 r = amdgpu_do_asic_reset(device_list_handle, &reset_context);
5021 if (r && r == -EAGAIN)
5027 /* Post ASIC reset for all devs .*/
5028 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5031 * Sometimes a later bad compute job can block a good gfx job as gfx
5032 * and compute ring share internal GC HW mutually. We add an additional
5033 * guilty jobs recheck step to find the real guilty job, it synchronously
5034 * submits and pends for the first job being signaled. If it gets timeout,
5035 * we identify it as a real guilty job.
5037 if (amdgpu_gpu_recovery == 2 &&
5038 !(tmp_vram_lost_counter < atomic_read(&adev->vram_lost_counter)))
5039 amdgpu_device_recheck_guilty_jobs(
5040 tmp_adev, device_list_handle, &reset_context);
5042 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5043 struct amdgpu_ring *ring = tmp_adev->rings[i];
5045 if (!ring || !ring->sched.thread)
5048 /* No point to resubmit jobs if we didn't HW reset*/
5049 if (!tmp_adev->asic_reset_res && !job_signaled)
5050 drm_sched_resubmit_jobs(&ring->sched);
5052 drm_sched_start(&ring->sched, !tmp_adev->asic_reset_res);
5055 if (!amdgpu_device_has_dc_support(tmp_adev) && !job_signaled) {
5056 drm_helper_resume_force_mode(adev_to_drm(tmp_adev));
5059 tmp_adev->asic_reset_res = 0;
5062 /* bad news, how to tell it to userspace ? */
5063 dev_info(tmp_adev->dev, "GPU reset(%d) failed\n", atomic_read(&tmp_adev->gpu_reset_counter));
5064 amdgpu_vf_error_put(tmp_adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
5066 dev_info(tmp_adev->dev, "GPU reset(%d) succeeded!\n", atomic_read(&tmp_adev->gpu_reset_counter));
5067 if (amdgpu_acpi_smart_shift_update(adev_to_drm(tmp_adev), AMDGPU_SS_DEV_D0))
5068 DRM_WARN("smart shift update failed\n");
5073 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5074 /* unlock kfd: SRIOV would do it separately */
5075 if (!need_emergency_restart && !amdgpu_sriov_vf(tmp_adev))
5076 amdgpu_amdkfd_post_reset(tmp_adev);
5078 /* kfd_post_reset will do nothing if kfd device is not initialized,
5079 * need to bring up kfd here if it's not be initialized before
5081 if (!adev->kfd.init_complete)
5082 amdgpu_amdkfd_device_init(adev);
5084 if (audio_suspended)
5085 amdgpu_device_resume_display_audio(tmp_adev);
5086 amdgpu_device_unlock_adev(tmp_adev);
5091 atomic_set(&hive->in_reset, 0);
5092 mutex_unlock(&hive->hive_lock);
5093 amdgpu_put_xgmi_hive(hive);
5096 if (r && r != -EAGAIN)
5097 dev_info(adev->dev, "GPU reset end with ret = %d\n", r);
5102 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
5104 * @adev: amdgpu_device pointer
5106 * Fetchs and stores in the driver the PCIE capabilities (gen speed
5107 * and lanes) of the slot the device is in. Handles APUs and
5108 * virtualized environments where PCIE config space may not be available.
5110 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
5112 struct pci_dev *pdev;
5113 enum pci_bus_speed speed_cap, platform_speed_cap;
5114 enum pcie_link_width platform_link_width;
5116 if (amdgpu_pcie_gen_cap)
5117 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
5119 if (amdgpu_pcie_lane_cap)
5120 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
5122 /* covers APUs as well */
5123 if (pci_is_root_bus(adev->pdev->bus)) {
5124 if (adev->pm.pcie_gen_mask == 0)
5125 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
5126 if (adev->pm.pcie_mlw_mask == 0)
5127 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
5131 if (adev->pm.pcie_gen_mask && adev->pm.pcie_mlw_mask)
5134 pcie_bandwidth_available(adev->pdev, NULL,
5135 &platform_speed_cap, &platform_link_width);
5137 if (adev->pm.pcie_gen_mask == 0) {
5140 speed_cap = pcie_get_speed_cap(pdev);
5141 if (speed_cap == PCI_SPEED_UNKNOWN) {
5142 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5143 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5144 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
5146 if (speed_cap == PCIE_SPEED_32_0GT)
5147 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5148 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5149 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5150 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4 |
5151 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN5);
5152 else if (speed_cap == PCIE_SPEED_16_0GT)
5153 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5154 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5155 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5156 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4);
5157 else if (speed_cap == PCIE_SPEED_8_0GT)
5158 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5159 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5160 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
5161 else if (speed_cap == PCIE_SPEED_5_0GT)
5162 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5163 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2);
5165 adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1;
5168 if (platform_speed_cap == PCI_SPEED_UNKNOWN) {
5169 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5170 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
5172 if (platform_speed_cap == PCIE_SPEED_32_0GT)
5173 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5174 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5175 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5176 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4 |
5177 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN5);
5178 else if (platform_speed_cap == PCIE_SPEED_16_0GT)
5179 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5180 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5181 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5182 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4);
5183 else if (platform_speed_cap == PCIE_SPEED_8_0GT)
5184 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5185 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5186 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3);
5187 else if (platform_speed_cap == PCIE_SPEED_5_0GT)
5188 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5189 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
5191 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
5195 if (adev->pm.pcie_mlw_mask == 0) {
5196 if (platform_link_width == PCIE_LNK_WIDTH_UNKNOWN) {
5197 adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK;
5199 switch (platform_link_width) {
5201 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
5202 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5203 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5204 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5205 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5206 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5207 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5210 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5211 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5212 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5213 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5214 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5215 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5218 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5219 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5220 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5221 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5222 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5225 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5226 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5227 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5228 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5231 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5232 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5233 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5236 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5237 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5240 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
5249 int amdgpu_device_baco_enter(struct drm_device *dev)
5251 struct amdgpu_device *adev = drm_to_adev(dev);
5252 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
5254 if (!amdgpu_device_supports_baco(adev_to_drm(adev)))
5257 if (ras && adev->ras_enabled &&
5258 adev->nbio.funcs->enable_doorbell_interrupt)
5259 adev->nbio.funcs->enable_doorbell_interrupt(adev, false);
5261 return amdgpu_dpm_baco_enter(adev);
5264 int amdgpu_device_baco_exit(struct drm_device *dev)
5266 struct amdgpu_device *adev = drm_to_adev(dev);
5267 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
5270 if (!amdgpu_device_supports_baco(adev_to_drm(adev)))
5273 ret = amdgpu_dpm_baco_exit(adev);
5277 if (ras && adev->ras_enabled &&
5278 adev->nbio.funcs->enable_doorbell_interrupt)
5279 adev->nbio.funcs->enable_doorbell_interrupt(adev, true);
5284 static void amdgpu_cancel_all_tdr(struct amdgpu_device *adev)
5288 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5289 struct amdgpu_ring *ring = adev->rings[i];
5291 if (!ring || !ring->sched.thread)
5294 cancel_delayed_work_sync(&ring->sched.work_tdr);
5299 * amdgpu_pci_error_detected - Called when a PCI error is detected.
5300 * @pdev: PCI device struct
5301 * @state: PCI channel state
5303 * Description: Called when a PCI error is detected.
5305 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT.
5307 pci_ers_result_t amdgpu_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
5309 struct drm_device *dev = pci_get_drvdata(pdev);
5310 struct amdgpu_device *adev = drm_to_adev(dev);
5313 DRM_INFO("PCI error: detected callback, state(%d)!!\n", state);
5315 if (adev->gmc.xgmi.num_physical_nodes > 1) {
5316 DRM_WARN("No support for XGMI hive yet...");
5317 return PCI_ERS_RESULT_DISCONNECT;
5321 case pci_channel_io_normal:
5322 return PCI_ERS_RESULT_CAN_RECOVER;
5323 /* Fatal error, prepare for slot reset */
5324 case pci_channel_io_frozen:
5326 * Cancel and wait for all TDRs in progress if failing to
5327 * set adev->in_gpu_reset in amdgpu_device_lock_adev
5329 * Locking adev->reset_sem will prevent any external access
5330 * to GPU during PCI error recovery
5332 while (!amdgpu_device_lock_adev(adev, NULL))
5333 amdgpu_cancel_all_tdr(adev);
5336 * Block any work scheduling as we do for regular GPU reset
5337 * for the duration of the recovery
5339 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5340 struct amdgpu_ring *ring = adev->rings[i];
5342 if (!ring || !ring->sched.thread)
5345 drm_sched_stop(&ring->sched, NULL);
5347 atomic_inc(&adev->gpu_reset_counter);
5348 return PCI_ERS_RESULT_NEED_RESET;
5349 case pci_channel_io_perm_failure:
5350 /* Permanent error, prepare for device removal */
5351 return PCI_ERS_RESULT_DISCONNECT;
5354 return PCI_ERS_RESULT_NEED_RESET;
5358 * amdgpu_pci_mmio_enabled - Enable MMIO and dump debug registers
5359 * @pdev: pointer to PCI device
5361 pci_ers_result_t amdgpu_pci_mmio_enabled(struct pci_dev *pdev)
5364 DRM_INFO("PCI error: mmio enabled callback!!\n");
5366 /* TODO - dump whatever for debugging purposes */
5368 /* This called only if amdgpu_pci_error_detected returns
5369 * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still
5370 * works, no need to reset slot.
5373 return PCI_ERS_RESULT_RECOVERED;
5377 * amdgpu_pci_slot_reset - Called when PCI slot has been reset.
5378 * @pdev: PCI device struct
5380 * Description: This routine is called by the pci error recovery
5381 * code after the PCI slot has been reset, just before we
5382 * should resume normal operations.
5384 pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev)
5386 struct drm_device *dev = pci_get_drvdata(pdev);
5387 struct amdgpu_device *adev = drm_to_adev(dev);
5389 struct amdgpu_reset_context reset_context;
5391 struct list_head device_list;
5393 DRM_INFO("PCI error: slot reset callback!!\n");
5395 memset(&reset_context, 0, sizeof(reset_context));
5397 INIT_LIST_HEAD(&device_list);
5398 list_add_tail(&adev->reset_list, &device_list);
5400 /* wait for asic to come out of reset */
5403 /* Restore PCI confspace */
5404 amdgpu_device_load_pci_state(pdev);
5406 /* confirm ASIC came out of reset */
5407 for (i = 0; i < adev->usec_timeout; i++) {
5408 memsize = amdgpu_asic_get_config_memsize(adev);
5410 if (memsize != 0xffffffff)
5414 if (memsize == 0xffffffff) {
5419 reset_context.method = AMD_RESET_METHOD_NONE;
5420 reset_context.reset_req_dev = adev;
5421 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
5422 set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
5424 adev->no_hw_access = true;
5425 r = amdgpu_device_pre_asic_reset(adev, &reset_context);
5426 adev->no_hw_access = false;
5430 r = amdgpu_do_asic_reset(&device_list, &reset_context);
5434 if (amdgpu_device_cache_pci_state(adev->pdev))
5435 pci_restore_state(adev->pdev);
5437 DRM_INFO("PCIe error recovery succeeded\n");
5439 DRM_ERROR("PCIe error recovery failed, err:%d", r);
5440 amdgpu_device_unlock_adev(adev);
5443 return r ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
5447 * amdgpu_pci_resume() - resume normal ops after PCI reset
5448 * @pdev: pointer to PCI device
5450 * Called when the error recovery driver tells us that its
5451 * OK to resume normal operation.
5453 void amdgpu_pci_resume(struct pci_dev *pdev)
5455 struct drm_device *dev = pci_get_drvdata(pdev);
5456 struct amdgpu_device *adev = drm_to_adev(dev);
5460 DRM_INFO("PCI error: resume callback!!\n");
5462 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5463 struct amdgpu_ring *ring = adev->rings[i];
5465 if (!ring || !ring->sched.thread)
5469 drm_sched_resubmit_jobs(&ring->sched);
5470 drm_sched_start(&ring->sched, true);
5473 amdgpu_device_unlock_adev(adev);
5476 bool amdgpu_device_cache_pci_state(struct pci_dev *pdev)
5478 struct drm_device *dev = pci_get_drvdata(pdev);
5479 struct amdgpu_device *adev = drm_to_adev(dev);
5482 r = pci_save_state(pdev);
5484 kfree(adev->pci_state);
5486 adev->pci_state = pci_store_saved_state(pdev);
5488 if (!adev->pci_state) {
5489 DRM_ERROR("Failed to store PCI saved state");
5493 DRM_WARN("Failed to save PCI state, err:%d\n", r);
5500 bool amdgpu_device_load_pci_state(struct pci_dev *pdev)
5502 struct drm_device *dev = pci_get_drvdata(pdev);
5503 struct amdgpu_device *adev = drm_to_adev(dev);
5506 if (!adev->pci_state)
5509 r = pci_load_saved_state(pdev, adev->pci_state);
5512 pci_restore_state(pdev);
5514 DRM_WARN("Failed to load PCI state, err:%d\n", r);
5521 void amdgpu_device_flush_hdp(struct amdgpu_device *adev,
5522 struct amdgpu_ring *ring)
5524 #ifdef CONFIG_X86_64
5525 if (adev->flags & AMD_IS_APU)
5528 if (adev->gmc.xgmi.connected_to_cpu)
5531 if (ring && ring->funcs->emit_hdp_flush)
5532 amdgpu_ring_emit_hdp_flush(ring);
5534 amdgpu_asic_flush_hdp(adev, ring);
5537 void amdgpu_device_invalidate_hdp(struct amdgpu_device *adev,
5538 struct amdgpu_ring *ring)
5540 #ifdef CONFIG_X86_64
5541 if (adev->flags & AMD_IS_APU)
5544 if (adev->gmc.xgmi.connected_to_cpu)
5547 amdgpu_asic_invalidate_hdp(adev, ring);