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"
68 #include <linux/suspend.h>
70 MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
71 MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
72 MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
73 MODULE_FIRMWARE("amdgpu/picasso_gpu_info.bin");
74 MODULE_FIRMWARE("amdgpu/raven2_gpu_info.bin");
75 MODULE_FIRMWARE("amdgpu/arcturus_gpu_info.bin");
76 MODULE_FIRMWARE("amdgpu/renoir_gpu_info.bin");
77 MODULE_FIRMWARE("amdgpu/navi10_gpu_info.bin");
78 MODULE_FIRMWARE("amdgpu/navi14_gpu_info.bin");
79 MODULE_FIRMWARE("amdgpu/navi12_gpu_info.bin");
81 #define AMDGPU_RESUME_MS 2000
83 const char *amdgpu_asic_name[] = {
116 * DOC: pcie_replay_count
118 * The amdgpu driver provides a sysfs API for reporting the total number
119 * of PCIe replays (NAKs)
120 * The file pcie_replay_count is used for this and returns the total
121 * number of replays as a sum of the NAKs generated and NAKs received
124 static ssize_t amdgpu_device_get_pcie_replay_count(struct device *dev,
125 struct device_attribute *attr, char *buf)
127 struct drm_device *ddev = dev_get_drvdata(dev);
128 struct amdgpu_device *adev = ddev->dev_private;
129 uint64_t cnt = amdgpu_asic_get_pcie_replay_count(adev);
131 return snprintf(buf, PAGE_SIZE, "%llu\n", cnt);
134 static DEVICE_ATTR(pcie_replay_count, S_IRUGO,
135 amdgpu_device_get_pcie_replay_count, NULL);
137 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev);
140 * amdgpu_device_is_px - Is the device is a dGPU with HG/PX power control
142 * @dev: drm_device pointer
144 * Returns true if the device is a dGPU with HG/PX power control,
145 * otherwise return false.
147 bool amdgpu_device_is_px(struct drm_device *dev)
149 struct amdgpu_device *adev = dev->dev_private;
151 if (adev->flags & AMD_IS_PX)
157 * MMIO register access helper functions.
160 * amdgpu_mm_rreg - read a memory mapped IO register
162 * @adev: amdgpu_device pointer
163 * @reg: dword aligned register offset
164 * @acc_flags: access flags which require special behavior
166 * Returns the 32 bit value from the offset specified.
168 uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
173 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
174 return amdgpu_virt_kiq_rreg(adev, reg);
176 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
177 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
181 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
182 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
183 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
184 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
186 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
191 * MMIO register read with bytes helper functions
192 * @offset:bytes offset from MMIO start
197 * amdgpu_mm_rreg8 - read a memory mapped IO register
199 * @adev: amdgpu_device pointer
200 * @offset: byte aligned register offset
202 * Returns the 8 bit value from the offset specified.
204 uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset) {
205 if (offset < adev->rmmio_size)
206 return (readb(adev->rmmio + offset));
211 * MMIO register write with bytes helper functions
212 * @offset:bytes offset from MMIO start
213 * @value: the value want to be written to the register
217 * amdgpu_mm_wreg8 - read a memory mapped IO register
219 * @adev: amdgpu_device pointer
220 * @offset: byte aligned register offset
221 * @value: 8 bit value to write
223 * Writes the value specified to the offset specified.
225 void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value) {
226 if (offset < adev->rmmio_size)
227 writeb(value, adev->rmmio + offset);
233 * amdgpu_mm_wreg - write to a memory mapped IO register
235 * @adev: amdgpu_device pointer
236 * @reg: dword aligned register offset
237 * @v: 32 bit value to write to the register
238 * @acc_flags: access flags which require special behavior
240 * Writes the value specified to the offset specified.
242 void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
245 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
247 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
248 adev->last_mm_index = v;
251 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
252 return amdgpu_virt_kiq_wreg(adev, reg, v);
254 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
255 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
259 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
260 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
261 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
262 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
265 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
271 * amdgpu_io_rreg - read an IO register
273 * @adev: amdgpu_device pointer
274 * @reg: dword aligned register offset
276 * Returns the 32 bit value from the offset specified.
278 u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
280 if ((reg * 4) < adev->rio_mem_size)
281 return ioread32(adev->rio_mem + (reg * 4));
283 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
284 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
289 * amdgpu_io_wreg - write to an IO register
291 * @adev: amdgpu_device pointer
292 * @reg: dword aligned register offset
293 * @v: 32 bit value to write to the register
295 * Writes the value specified to the offset specified.
297 void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
299 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
300 adev->last_mm_index = v;
303 if ((reg * 4) < adev->rio_mem_size)
304 iowrite32(v, adev->rio_mem + (reg * 4));
306 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
307 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
310 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
316 * amdgpu_mm_rdoorbell - read a doorbell dword
318 * @adev: amdgpu_device pointer
319 * @index: doorbell index
321 * Returns the value in the doorbell aperture at the
322 * requested doorbell index (CIK).
324 u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
326 if (index < adev->doorbell.num_doorbells) {
327 return readl(adev->doorbell.ptr + index);
329 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
335 * amdgpu_mm_wdoorbell - write a doorbell dword
337 * @adev: amdgpu_device pointer
338 * @index: doorbell index
341 * Writes @v to the doorbell aperture at the
342 * requested doorbell index (CIK).
344 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
346 if (index < adev->doorbell.num_doorbells) {
347 writel(v, adev->doorbell.ptr + index);
349 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
354 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
356 * @adev: amdgpu_device pointer
357 * @index: doorbell index
359 * Returns the value in the doorbell aperture at the
360 * requested doorbell index (VEGA10+).
362 u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
364 if (index < adev->doorbell.num_doorbells) {
365 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
367 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
373 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
375 * @adev: amdgpu_device pointer
376 * @index: doorbell index
379 * Writes @v to the doorbell aperture at the
380 * requested doorbell index (VEGA10+).
382 void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
384 if (index < adev->doorbell.num_doorbells) {
385 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
387 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
392 * amdgpu_invalid_rreg - dummy reg read function
394 * @adev: amdgpu device pointer
395 * @reg: offset of register
397 * Dummy register read function. Used for register blocks
398 * that certain asics don't have (all asics).
399 * Returns the value in the register.
401 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
403 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
409 * amdgpu_invalid_wreg - dummy reg write function
411 * @adev: amdgpu device pointer
412 * @reg: offset of register
413 * @v: value to write to the register
415 * Dummy register read function. Used for register blocks
416 * that certain asics don't have (all asics).
418 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
420 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
426 * amdgpu_invalid_rreg64 - dummy 64 bit reg read function
428 * @adev: amdgpu device pointer
429 * @reg: offset of register
431 * Dummy register read function. Used for register blocks
432 * that certain asics don't have (all asics).
433 * Returns the value in the register.
435 static uint64_t amdgpu_invalid_rreg64(struct amdgpu_device *adev, uint32_t reg)
437 DRM_ERROR("Invalid callback to read 64 bit register 0x%04X\n", reg);
443 * amdgpu_invalid_wreg64 - dummy reg write function
445 * @adev: amdgpu device pointer
446 * @reg: offset of register
447 * @v: value to write to the register
449 * Dummy register read function. Used for register blocks
450 * that certain asics don't have (all asics).
452 static void amdgpu_invalid_wreg64(struct amdgpu_device *adev, uint32_t reg, uint64_t v)
454 DRM_ERROR("Invalid callback to write 64 bit register 0x%04X with 0x%08llX\n",
460 * amdgpu_block_invalid_rreg - dummy reg read function
462 * @adev: amdgpu device pointer
463 * @block: offset of instance
464 * @reg: offset of register
466 * Dummy register read function. Used for register blocks
467 * that certain asics don't have (all asics).
468 * Returns the value in the register.
470 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
471 uint32_t block, uint32_t reg)
473 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
480 * amdgpu_block_invalid_wreg - dummy reg write function
482 * @adev: amdgpu device pointer
483 * @block: offset of instance
484 * @reg: offset of register
485 * @v: value to write to the register
487 * Dummy register read function. Used for register blocks
488 * that certain asics don't have (all asics).
490 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
492 uint32_t reg, uint32_t v)
494 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
500 * amdgpu_device_vram_scratch_init - allocate the VRAM scratch page
502 * @adev: amdgpu device pointer
504 * Allocates a scratch page of VRAM for use by various things in the
507 static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev)
509 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
510 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
511 &adev->vram_scratch.robj,
512 &adev->vram_scratch.gpu_addr,
513 (void **)&adev->vram_scratch.ptr);
517 * amdgpu_device_vram_scratch_fini - Free the VRAM scratch page
519 * @adev: amdgpu device pointer
521 * Frees the VRAM scratch page.
523 static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev)
525 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
529 * amdgpu_device_program_register_sequence - program an array of registers.
531 * @adev: amdgpu_device pointer
532 * @registers: pointer to the register array
533 * @array_size: size of the register array
535 * Programs an array or registers with and and or masks.
536 * This is a helper for setting golden registers.
538 void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
539 const u32 *registers,
540 const u32 array_size)
542 u32 tmp, reg, and_mask, or_mask;
548 for (i = 0; i < array_size; i +=3) {
549 reg = registers[i + 0];
550 and_mask = registers[i + 1];
551 or_mask = registers[i + 2];
553 if (and_mask == 0xffffffff) {
558 if (adev->family >= AMDGPU_FAMILY_AI)
559 tmp |= (or_mask & and_mask);
568 * amdgpu_device_pci_config_reset - reset the GPU
570 * @adev: amdgpu_device pointer
572 * Resets the GPU using the pci config reset sequence.
573 * Only applicable to asics prior to vega10.
575 void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
577 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
581 * GPU doorbell aperture helpers function.
584 * amdgpu_device_doorbell_init - Init doorbell driver information.
586 * @adev: amdgpu_device pointer
588 * Init doorbell driver information (CIK)
589 * Returns 0 on success, error on failure.
591 static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
594 /* No doorbell on SI hardware generation */
595 if (adev->asic_type < CHIP_BONAIRE) {
596 adev->doorbell.base = 0;
597 adev->doorbell.size = 0;
598 adev->doorbell.num_doorbells = 0;
599 adev->doorbell.ptr = NULL;
603 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
606 amdgpu_asic_init_doorbell_index(adev);
608 /* doorbell bar mapping */
609 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
610 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
612 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
613 adev->doorbell_index.max_assignment+1);
614 if (adev->doorbell.num_doorbells == 0)
617 /* For Vega, reserve and map two pages on doorbell BAR since SDMA
618 * paging queue doorbell use the second page. The
619 * AMDGPU_DOORBELL64_MAX_ASSIGNMENT definition assumes all the
620 * doorbells are in the first page. So with paging queue enabled,
621 * the max num_doorbells should + 1 page (0x400 in dword)
623 if (adev->asic_type >= CHIP_VEGA10)
624 adev->doorbell.num_doorbells += 0x400;
626 adev->doorbell.ptr = ioremap(adev->doorbell.base,
627 adev->doorbell.num_doorbells *
629 if (adev->doorbell.ptr == NULL)
636 * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
638 * @adev: amdgpu_device pointer
640 * Tear down doorbell driver information (CIK)
642 static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
644 iounmap(adev->doorbell.ptr);
645 adev->doorbell.ptr = NULL;
651 * amdgpu_device_wb_*()
652 * Writeback is the method by which the GPU updates special pages in memory
653 * with the status of certain GPU events (fences, ring pointers,etc.).
657 * amdgpu_device_wb_fini - Disable Writeback and free memory
659 * @adev: amdgpu_device pointer
661 * Disables Writeback and frees the Writeback memory (all asics).
662 * Used at driver shutdown.
664 static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
666 if (adev->wb.wb_obj) {
667 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
669 (void **)&adev->wb.wb);
670 adev->wb.wb_obj = NULL;
675 * amdgpu_device_wb_init- Init Writeback driver info and allocate memory
677 * @adev: amdgpu_device pointer
679 * Initializes writeback and allocates writeback memory (all asics).
680 * Used at driver startup.
681 * Returns 0 on success or an -error on failure.
683 static int amdgpu_device_wb_init(struct amdgpu_device *adev)
687 if (adev->wb.wb_obj == NULL) {
688 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
689 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
690 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
691 &adev->wb.wb_obj, &adev->wb.gpu_addr,
692 (void **)&adev->wb.wb);
694 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
698 adev->wb.num_wb = AMDGPU_MAX_WB;
699 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
701 /* clear wb memory */
702 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
709 * amdgpu_device_wb_get - Allocate a wb entry
711 * @adev: amdgpu_device pointer
714 * Allocate a wb slot for use by the driver (all asics).
715 * Returns 0 on success or -EINVAL on failure.
717 int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
719 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
721 if (offset < adev->wb.num_wb) {
722 __set_bit(offset, adev->wb.used);
723 *wb = offset << 3; /* convert to dw offset */
731 * amdgpu_device_wb_free - Free a wb entry
733 * @adev: amdgpu_device pointer
736 * Free a wb slot allocated for use by the driver (all asics)
738 void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
741 if (wb < adev->wb.num_wb)
742 __clear_bit(wb, adev->wb.used);
746 * amdgpu_device_resize_fb_bar - try to resize FB BAR
748 * @adev: amdgpu_device pointer
750 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
751 * to fail, but if any of the BARs is not accessible after the size we abort
752 * driver loading by returning -ENODEV.
754 int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
756 u64 space_needed = roundup_pow_of_two(adev->gmc.real_vram_size);
757 u32 rbar_size = order_base_2(((space_needed >> 20) | 1)) - 1;
758 struct pci_bus *root;
759 struct resource *res;
765 if (amdgpu_sriov_vf(adev))
768 /* Check if the root BUS has 64bit memory resources */
769 root = adev->pdev->bus;
773 pci_bus_for_each_resource(root, res, i) {
774 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
775 res->start > 0x100000000ull)
779 /* Trying to resize is pointless without a root hub window above 4GB */
783 /* Disable memory decoding while we change the BAR addresses and size */
784 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
785 pci_write_config_word(adev->pdev, PCI_COMMAND,
786 cmd & ~PCI_COMMAND_MEMORY);
788 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
789 amdgpu_device_doorbell_fini(adev);
790 if (adev->asic_type >= CHIP_BONAIRE)
791 pci_release_resource(adev->pdev, 2);
793 pci_release_resource(adev->pdev, 0);
795 r = pci_resize_resource(adev->pdev, 0, rbar_size);
797 DRM_INFO("Not enough PCI address space for a large BAR.");
798 else if (r && r != -ENOTSUPP)
799 DRM_ERROR("Problem resizing BAR0 (%d).", r);
801 pci_assign_unassigned_bus_resources(adev->pdev->bus);
803 /* When the doorbell or fb BAR isn't available we have no chance of
806 r = amdgpu_device_doorbell_init(adev);
807 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
810 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
816 * GPU helpers function.
819 * amdgpu_device_need_post - check if the hw need post or not
821 * @adev: amdgpu_device pointer
823 * Check if the asic has been initialized (all asics) at driver startup
824 * or post is needed if hw reset is performed.
825 * Returns true if need or false if not.
827 bool amdgpu_device_need_post(struct amdgpu_device *adev)
831 if (amdgpu_sriov_vf(adev))
834 if (amdgpu_passthrough(adev)) {
835 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
836 * some old smc fw still need driver do vPost otherwise gpu hang, while
837 * those smc fw version above 22.15 doesn't have this flaw, so we force
838 * vpost executed for smc version below 22.15
840 if (adev->asic_type == CHIP_FIJI) {
843 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
844 /* force vPost if error occured */
848 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
849 if (fw_ver < 0x00160e00)
854 if (adev->has_hw_reset) {
855 adev->has_hw_reset = false;
859 /* bios scratch used on CIK+ */
860 if (adev->asic_type >= CHIP_BONAIRE)
861 return amdgpu_atombios_scratch_need_asic_init(adev);
863 /* check MEM_SIZE for older asics */
864 reg = amdgpu_asic_get_config_memsize(adev);
866 if ((reg != 0) && (reg != 0xffffffff))
872 /* if we get transitioned to only one device, take VGA back */
874 * amdgpu_device_vga_set_decode - enable/disable vga decode
876 * @cookie: amdgpu_device pointer
877 * @state: enable/disable vga decode
879 * Enable/disable vga decode (all asics).
880 * Returns VGA resource flags.
882 static unsigned int amdgpu_device_vga_set_decode(void *cookie, bool state)
884 struct amdgpu_device *adev = cookie;
885 amdgpu_asic_set_vga_state(adev, state);
887 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
888 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
890 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
894 * amdgpu_device_check_block_size - validate the vm block size
896 * @adev: amdgpu_device pointer
898 * Validates the vm block size specified via module parameter.
899 * The vm block size defines number of bits in page table versus page directory,
900 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
901 * page table and the remaining bits are in the page directory.
903 static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
905 /* defines number of bits in page table versus page directory,
906 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
907 * page table and the remaining bits are in the page directory */
908 if (amdgpu_vm_block_size == -1)
911 if (amdgpu_vm_block_size < 9) {
912 dev_warn(adev->dev, "VM page table size (%d) too small\n",
913 amdgpu_vm_block_size);
914 amdgpu_vm_block_size = -1;
919 * amdgpu_device_check_vm_size - validate the vm size
921 * @adev: amdgpu_device pointer
923 * Validates the vm size in GB specified via module parameter.
924 * The VM size is the size of the GPU virtual memory space in GB.
926 static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
928 /* no need to check the default value */
929 if (amdgpu_vm_size == -1)
932 if (amdgpu_vm_size < 1) {
933 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
939 static void amdgpu_device_check_smu_prv_buffer_size(struct amdgpu_device *adev)
942 bool is_os_64 = (sizeof(void *) == 8) ? true : false;
943 uint64_t total_memory;
944 uint64_t dram_size_seven_GB = 0x1B8000000;
945 uint64_t dram_size_three_GB = 0xB8000000;
947 if (amdgpu_smu_memory_pool_size == 0)
951 DRM_WARN("Not 64-bit OS, feature not supported\n");
955 total_memory = (uint64_t)si.totalram * si.mem_unit;
957 if ((amdgpu_smu_memory_pool_size == 1) ||
958 (amdgpu_smu_memory_pool_size == 2)) {
959 if (total_memory < dram_size_three_GB)
961 } else if ((amdgpu_smu_memory_pool_size == 4) ||
962 (amdgpu_smu_memory_pool_size == 8)) {
963 if (total_memory < dram_size_seven_GB)
966 DRM_WARN("Smu memory pool size not supported\n");
969 adev->pm.smu_prv_buffer_size = amdgpu_smu_memory_pool_size << 28;
974 DRM_WARN("No enough system memory\n");
976 adev->pm.smu_prv_buffer_size = 0;
980 * amdgpu_device_check_arguments - validate module params
982 * @adev: amdgpu_device pointer
984 * Validates certain module parameters and updates
985 * the associated values used by the driver (all asics).
987 static int amdgpu_device_check_arguments(struct amdgpu_device *adev)
991 if (amdgpu_sched_jobs < 4) {
992 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
994 amdgpu_sched_jobs = 4;
995 } else if (!is_power_of_2(amdgpu_sched_jobs)){
996 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
998 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1001 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
1002 /* gart size must be greater or equal to 32M */
1003 dev_warn(adev->dev, "gart size (%d) too small\n",
1005 amdgpu_gart_size = -1;
1008 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
1009 /* gtt size must be greater or equal to 32M */
1010 dev_warn(adev->dev, "gtt size (%d) too small\n",
1012 amdgpu_gtt_size = -1;
1015 /* valid range is between 4 and 9 inclusive */
1016 if (amdgpu_vm_fragment_size != -1 &&
1017 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1018 dev_warn(adev->dev, "valid range is between 4 and 9\n");
1019 amdgpu_vm_fragment_size = -1;
1022 amdgpu_device_check_smu_prv_buffer_size(adev);
1024 amdgpu_device_check_vm_size(adev);
1026 amdgpu_device_check_block_size(adev);
1028 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
1034 * amdgpu_switcheroo_set_state - set switcheroo state
1036 * @pdev: pci dev pointer
1037 * @state: vga_switcheroo state
1039 * Callback for the switcheroo driver. Suspends or resumes the
1040 * the asics before or after it is powered up using ACPI methods.
1042 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1044 struct drm_device *dev = pci_get_drvdata(pdev);
1046 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1049 if (state == VGA_SWITCHEROO_ON) {
1050 pr_info("amdgpu: switched on\n");
1051 /* don't suspend or resume card normally */
1052 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1054 amdgpu_device_resume(dev, true, true);
1056 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1057 drm_kms_helper_poll_enable(dev);
1059 pr_info("amdgpu: switched off\n");
1060 drm_kms_helper_poll_disable(dev);
1061 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1062 amdgpu_device_suspend(dev, true, true);
1063 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1068 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1070 * @pdev: pci dev pointer
1072 * Callback for the switcheroo driver. Check of the switcheroo
1073 * state can be changed.
1074 * Returns true if the state can be changed, false if not.
1076 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1078 struct drm_device *dev = pci_get_drvdata(pdev);
1081 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1082 * locking inversion with the driver load path. And the access here is
1083 * completely racy anyway. So don't bother with locking for now.
1085 return dev->open_count == 0;
1088 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1089 .set_gpu_state = amdgpu_switcheroo_set_state,
1091 .can_switch = amdgpu_switcheroo_can_switch,
1095 * amdgpu_device_ip_set_clockgating_state - set the CG state
1097 * @dev: amdgpu_device pointer
1098 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1099 * @state: clockgating state (gate or ungate)
1101 * Sets the requested clockgating state for all instances of
1102 * the hardware IP specified.
1103 * Returns the error code from the last instance.
1105 int amdgpu_device_ip_set_clockgating_state(void *dev,
1106 enum amd_ip_block_type block_type,
1107 enum amd_clockgating_state state)
1109 struct amdgpu_device *adev = dev;
1112 for (i = 0; i < adev->num_ip_blocks; i++) {
1113 if (!adev->ip_blocks[i].status.valid)
1115 if (adev->ip_blocks[i].version->type != block_type)
1117 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1119 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1120 (void *)adev, state);
1122 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1123 adev->ip_blocks[i].version->funcs->name, r);
1129 * amdgpu_device_ip_set_powergating_state - set the PG state
1131 * @dev: amdgpu_device pointer
1132 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1133 * @state: powergating state (gate or ungate)
1135 * Sets the requested powergating state for all instances of
1136 * the hardware IP specified.
1137 * Returns the error code from the last instance.
1139 int amdgpu_device_ip_set_powergating_state(void *dev,
1140 enum amd_ip_block_type block_type,
1141 enum amd_powergating_state state)
1143 struct amdgpu_device *adev = dev;
1146 for (i = 0; i < adev->num_ip_blocks; i++) {
1147 if (!adev->ip_blocks[i].status.valid)
1149 if (adev->ip_blocks[i].version->type != block_type)
1151 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1153 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1154 (void *)adev, state);
1156 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1157 adev->ip_blocks[i].version->funcs->name, r);
1163 * amdgpu_device_ip_get_clockgating_state - get the CG state
1165 * @adev: amdgpu_device pointer
1166 * @flags: clockgating feature flags
1168 * Walks the list of IPs on the device and updates the clockgating
1169 * flags for each IP.
1170 * Updates @flags with the feature flags for each hardware IP where
1171 * clockgating is enabled.
1173 void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
1178 for (i = 0; i < adev->num_ip_blocks; i++) {
1179 if (!adev->ip_blocks[i].status.valid)
1181 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1182 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1187 * amdgpu_device_ip_wait_for_idle - wait for idle
1189 * @adev: amdgpu_device pointer
1190 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1192 * Waits for the request hardware IP to be idle.
1193 * Returns 0 for success or a negative error code on failure.
1195 int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1196 enum amd_ip_block_type block_type)
1200 for (i = 0; i < adev->num_ip_blocks; i++) {
1201 if (!adev->ip_blocks[i].status.valid)
1203 if (adev->ip_blocks[i].version->type == block_type) {
1204 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
1215 * amdgpu_device_ip_is_idle - is the hardware IP idle
1217 * @adev: amdgpu_device pointer
1218 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1220 * Check if the hardware IP is idle or not.
1221 * Returns true if it the IP is idle, false if not.
1223 bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1224 enum amd_ip_block_type block_type)
1228 for (i = 0; i < adev->num_ip_blocks; i++) {
1229 if (!adev->ip_blocks[i].status.valid)
1231 if (adev->ip_blocks[i].version->type == block_type)
1232 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
1239 * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1241 * @adev: amdgpu_device pointer
1242 * @type: Type of hardware IP (SMU, GFX, UVD, etc.)
1244 * Returns a pointer to the hardware IP block structure
1245 * if it exists for the asic, otherwise NULL.
1247 struct amdgpu_ip_block *
1248 amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1249 enum amd_ip_block_type type)
1253 for (i = 0; i < adev->num_ip_blocks; i++)
1254 if (adev->ip_blocks[i].version->type == type)
1255 return &adev->ip_blocks[i];
1261 * amdgpu_device_ip_block_version_cmp
1263 * @adev: amdgpu_device pointer
1264 * @type: enum amd_ip_block_type
1265 * @major: major version
1266 * @minor: minor version
1268 * return 0 if equal or greater
1269 * return 1 if smaller or the ip_block doesn't exist
1271 int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1272 enum amd_ip_block_type type,
1273 u32 major, u32 minor)
1275 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
1277 if (ip_block && ((ip_block->version->major > major) ||
1278 ((ip_block->version->major == major) &&
1279 (ip_block->version->minor >= minor))))
1286 * amdgpu_device_ip_block_add
1288 * @adev: amdgpu_device pointer
1289 * @ip_block_version: pointer to the IP to add
1291 * Adds the IP block driver information to the collection of IPs
1294 int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1295 const struct amdgpu_ip_block_version *ip_block_version)
1297 if (!ip_block_version)
1300 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
1301 ip_block_version->funcs->name);
1303 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1309 * amdgpu_device_enable_virtual_display - enable virtual display feature
1311 * @adev: amdgpu_device pointer
1313 * Enabled the virtual display feature if the user has enabled it via
1314 * the module parameter virtual_display. This feature provides a virtual
1315 * display hardware on headless boards or in virtualized environments.
1316 * This function parses and validates the configuration string specified by
1317 * the user and configues the virtual display configuration (number of
1318 * virtual connectors, crtcs, etc.) specified.
1320 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
1322 adev->enable_virtual_display = false;
1324 if (amdgpu_virtual_display) {
1325 struct drm_device *ddev = adev->ddev;
1326 const char *pci_address_name = pci_name(ddev->pdev);
1327 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
1329 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1330 pciaddstr_tmp = pciaddstr;
1331 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1332 pciaddname = strsep(&pciaddname_tmp, ",");
1333 if (!strcmp("all", pciaddname)
1334 || !strcmp(pci_address_name, pciaddname)) {
1338 adev->enable_virtual_display = true;
1341 res = kstrtol(pciaddname_tmp, 10,
1349 adev->mode_info.num_crtc = num_crtc;
1351 adev->mode_info.num_crtc = 1;
1357 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1358 amdgpu_virtual_display, pci_address_name,
1359 adev->enable_virtual_display, adev->mode_info.num_crtc);
1366 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
1368 * @adev: amdgpu_device pointer
1370 * Parses the asic configuration parameters specified in the gpu info
1371 * firmware and makes them availale to the driver for use in configuring
1373 * Returns 0 on success, -EINVAL on failure.
1375 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1377 const char *chip_name;
1380 const struct gpu_info_firmware_header_v1_0 *hdr;
1382 adev->firmware.gpu_info_fw = NULL;
1384 switch (adev->asic_type) {
1388 case CHIP_POLARIS10:
1389 case CHIP_POLARIS11:
1390 case CHIP_POLARIS12:
1394 #ifdef CONFIG_DRM_AMDGPU_SI
1401 #ifdef CONFIG_DRM_AMDGPU_CIK
1412 chip_name = "vega10";
1415 chip_name = "vega12";
1418 if (adev->rev_id >= 8)
1419 chip_name = "raven2";
1420 else if (adev->pdev->device == 0x15d8)
1421 chip_name = "picasso";
1423 chip_name = "raven";
1426 chip_name = "arcturus";
1429 chip_name = "renoir";
1432 chip_name = "navi10";
1435 chip_name = "navi14";
1438 chip_name = "navi12";
1442 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
1443 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
1446 "Failed to load gpu_info firmware \"%s\"\n",
1450 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
1453 "Failed to validate gpu_info firmware \"%s\"\n",
1458 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
1459 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1461 switch (hdr->version_major) {
1464 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
1465 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
1466 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1468 if (amdgpu_discovery && adev->asic_type >= CHIP_NAVI10)
1469 goto parse_soc_bounding_box;
1471 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1472 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1473 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1474 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
1475 adev->gfx.config.max_texture_channel_caches =
1476 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1477 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1478 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1479 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1480 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
1481 adev->gfx.config.double_offchip_lds_buf =
1482 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1483 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
1484 adev->gfx.cu_info.max_waves_per_simd =
1485 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1486 adev->gfx.cu_info.max_scratch_slots_per_cu =
1487 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1488 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
1489 if (hdr->version_minor >= 1) {
1490 const struct gpu_info_firmware_v1_1 *gpu_info_fw =
1491 (const struct gpu_info_firmware_v1_1 *)(adev->firmware.gpu_info_fw->data +
1492 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1493 adev->gfx.config.num_sc_per_sh =
1494 le32_to_cpu(gpu_info_fw->num_sc_per_sh);
1495 adev->gfx.config.num_packer_per_sc =
1496 le32_to_cpu(gpu_info_fw->num_packer_per_sc);
1499 parse_soc_bounding_box:
1500 #ifdef CONFIG_DRM_AMD_DC_DCN2_0
1502 * soc bounding box info is not integrated in disocovery table,
1503 * we always need to parse it from gpu info firmware.
1505 if (hdr->version_minor == 2) {
1506 const struct gpu_info_firmware_v1_2 *gpu_info_fw =
1507 (const struct gpu_info_firmware_v1_2 *)(adev->firmware.gpu_info_fw->data +
1508 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1509 adev->dm.soc_bounding_box = &gpu_info_fw->soc_bounding_box;
1516 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1525 * amdgpu_device_ip_early_init - run early init for hardware IPs
1527 * @adev: amdgpu_device pointer
1529 * Early initialization pass for hardware IPs. The hardware IPs that make
1530 * up each asic are discovered each IP's early_init callback is run. This
1531 * is the first stage in initializing the asic.
1532 * Returns 0 on success, negative error code on failure.
1534 static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
1538 amdgpu_device_enable_virtual_display(adev);
1540 switch (adev->asic_type) {
1544 case CHIP_POLARIS10:
1545 case CHIP_POLARIS11:
1546 case CHIP_POLARIS12:
1550 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
1551 adev->family = AMDGPU_FAMILY_CZ;
1553 adev->family = AMDGPU_FAMILY_VI;
1555 r = vi_set_ip_blocks(adev);
1559 #ifdef CONFIG_DRM_AMDGPU_SI
1565 adev->family = AMDGPU_FAMILY_SI;
1566 r = si_set_ip_blocks(adev);
1571 #ifdef CONFIG_DRM_AMDGPU_CIK
1577 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1578 adev->family = AMDGPU_FAMILY_CI;
1580 adev->family = AMDGPU_FAMILY_KV;
1582 r = cik_set_ip_blocks(adev);
1593 if (adev->asic_type == CHIP_RAVEN ||
1594 adev->asic_type == CHIP_RENOIR)
1595 adev->family = AMDGPU_FAMILY_RV;
1597 adev->family = AMDGPU_FAMILY_AI;
1599 r = soc15_set_ip_blocks(adev);
1606 adev->family = AMDGPU_FAMILY_NV;
1608 r = nv_set_ip_blocks(adev);
1613 /* FIXME: not supported yet */
1617 r = amdgpu_device_parse_gpu_info_fw(adev);
1621 if (amdgpu_discovery && adev->asic_type >= CHIP_NAVI10)
1622 amdgpu_discovery_get_gfx_info(adev);
1624 amdgpu_amdkfd_device_probe(adev);
1626 if (amdgpu_sriov_vf(adev)) {
1627 r = amdgpu_virt_request_full_gpu(adev, true);
1632 adev->pm.pp_feature = amdgpu_pp_feature_mask;
1633 if (amdgpu_sriov_vf(adev) || sched_policy == KFD_SCHED_POLICY_NO_HWS)
1634 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
1636 for (i = 0; i < adev->num_ip_blocks; i++) {
1637 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1638 DRM_ERROR("disabled ip block: %d <%s>\n",
1639 i, adev->ip_blocks[i].version->funcs->name);
1640 adev->ip_blocks[i].status.valid = false;
1642 if (adev->ip_blocks[i].version->funcs->early_init) {
1643 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
1645 adev->ip_blocks[i].status.valid = false;
1647 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1648 adev->ip_blocks[i].version->funcs->name, r);
1651 adev->ip_blocks[i].status.valid = true;
1654 adev->ip_blocks[i].status.valid = true;
1657 /* get the vbios after the asic_funcs are set up */
1658 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) {
1660 if (!amdgpu_get_bios(adev))
1663 r = amdgpu_atombios_init(adev);
1665 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
1666 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
1672 adev->cg_flags &= amdgpu_cg_mask;
1673 adev->pg_flags &= amdgpu_pg_mask;
1678 static int amdgpu_device_ip_hw_init_phase1(struct amdgpu_device *adev)
1682 for (i = 0; i < adev->num_ip_blocks; i++) {
1683 if (!adev->ip_blocks[i].status.sw)
1685 if (adev->ip_blocks[i].status.hw)
1687 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1688 (amdgpu_sriov_vf(adev) && (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)) ||
1689 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
1690 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
1692 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1693 adev->ip_blocks[i].version->funcs->name, r);
1696 adev->ip_blocks[i].status.hw = true;
1703 static int amdgpu_device_ip_hw_init_phase2(struct amdgpu_device *adev)
1707 for (i = 0; i < adev->num_ip_blocks; i++) {
1708 if (!adev->ip_blocks[i].status.sw)
1710 if (adev->ip_blocks[i].status.hw)
1712 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
1714 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1715 adev->ip_blocks[i].version->funcs->name, r);
1718 adev->ip_blocks[i].status.hw = true;
1724 static int amdgpu_device_fw_loading(struct amdgpu_device *adev)
1728 uint32_t smu_version;
1730 if (adev->asic_type >= CHIP_VEGA10) {
1731 for (i = 0; i < adev->num_ip_blocks; i++) {
1732 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_PSP)
1735 /* no need to do the fw loading again if already done*/
1736 if (adev->ip_blocks[i].status.hw == true)
1739 if (adev->in_gpu_reset || adev->in_suspend) {
1740 r = adev->ip_blocks[i].version->funcs->resume(adev);
1742 DRM_ERROR("resume of IP block <%s> failed %d\n",
1743 adev->ip_blocks[i].version->funcs->name, r);
1747 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
1749 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1750 adev->ip_blocks[i].version->funcs->name, r);
1755 adev->ip_blocks[i].status.hw = true;
1760 r = amdgpu_pm_load_smu_firmware(adev, &smu_version);
1766 * amdgpu_device_ip_init - run init for hardware IPs
1768 * @adev: amdgpu_device pointer
1770 * Main initialization pass for hardware IPs. The list of all the hardware
1771 * IPs that make up the asic is walked and the sw_init and hw_init callbacks
1772 * are run. sw_init initializes the software state associated with each IP
1773 * and hw_init initializes the hardware associated with each IP.
1774 * Returns 0 on success, negative error code on failure.
1776 static int amdgpu_device_ip_init(struct amdgpu_device *adev)
1780 r = amdgpu_ras_init(adev);
1784 for (i = 0; i < adev->num_ip_blocks; i++) {
1785 if (!adev->ip_blocks[i].status.valid)
1787 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
1789 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1790 adev->ip_blocks[i].version->funcs->name, r);
1793 adev->ip_blocks[i].status.sw = true;
1795 /* need to do gmc hw init early so we can allocate gpu mem */
1796 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1797 r = amdgpu_device_vram_scratch_init(adev);
1799 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
1802 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1804 DRM_ERROR("hw_init %d failed %d\n", i, r);
1807 r = amdgpu_device_wb_init(adev);
1809 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
1812 adev->ip_blocks[i].status.hw = true;
1814 /* right after GMC hw init, we create CSA */
1815 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) {
1816 r = amdgpu_allocate_static_csa(adev, &adev->virt.csa_obj,
1817 AMDGPU_GEM_DOMAIN_VRAM,
1820 DRM_ERROR("allocate CSA failed %d\n", r);
1827 r = amdgpu_ib_pool_init(adev);
1829 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
1830 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
1834 r = amdgpu_ucode_create_bo(adev); /* create ucode bo when sw_init complete*/
1838 r = amdgpu_device_ip_hw_init_phase1(adev);
1842 r = amdgpu_device_fw_loading(adev);
1846 r = amdgpu_device_ip_hw_init_phase2(adev);
1850 if (adev->gmc.xgmi.num_physical_nodes > 1)
1851 amdgpu_xgmi_add_device(adev);
1852 amdgpu_amdkfd_device_init(adev);
1855 if (amdgpu_sriov_vf(adev)) {
1857 amdgpu_virt_init_data_exchange(adev);
1858 amdgpu_virt_release_full_gpu(adev, true);
1865 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
1867 * @adev: amdgpu_device pointer
1869 * Writes a reset magic value to the gart pointer in VRAM. The driver calls
1870 * this function before a GPU reset. If the value is retained after a
1871 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
1873 static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
1875 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1879 * amdgpu_device_check_vram_lost - check if vram is valid
1881 * @adev: amdgpu_device pointer
1883 * Checks the reset magic value written to the gart pointer in VRAM.
1884 * The driver calls this after a GPU reset to see if the contents of
1885 * VRAM is lost or now.
1886 * returns true if vram is lost, false if not.
1888 static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
1890 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1891 AMDGPU_RESET_MAGIC_NUM);
1895 * amdgpu_device_set_cg_state - set clockgating for amdgpu device
1897 * @adev: amdgpu_device pointer
1899 * The list of all the hardware IPs that make up the asic is walked and the
1900 * set_clockgating_state callbacks are run.
1901 * Late initialization pass enabling clockgating for hardware IPs.
1902 * Fini or suspend, pass disabling clockgating for hardware IPs.
1903 * Returns 0 on success, negative error code on failure.
1906 static int amdgpu_device_set_cg_state(struct amdgpu_device *adev,
1907 enum amd_clockgating_state state)
1911 if (amdgpu_emu_mode == 1)
1914 for (j = 0; j < adev->num_ip_blocks; j++) {
1915 i = state == AMD_CG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
1916 if (!adev->ip_blocks[i].status.late_initialized)
1918 /* skip CG for VCE/UVD, it's handled specially */
1919 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1920 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1921 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
1922 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
1923 /* enable clockgating to save power */
1924 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1927 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1928 adev->ip_blocks[i].version->funcs->name, r);
1937 static int amdgpu_device_set_pg_state(struct amdgpu_device *adev, enum amd_powergating_state state)
1941 if (amdgpu_emu_mode == 1)
1944 for (j = 0; j < adev->num_ip_blocks; j++) {
1945 i = state == AMD_PG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
1946 if (!adev->ip_blocks[i].status.late_initialized)
1948 /* skip CG for VCE/UVD, it's handled specially */
1949 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1950 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1951 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
1952 adev->ip_blocks[i].version->funcs->set_powergating_state) {
1953 /* enable powergating to save power */
1954 r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
1957 DRM_ERROR("set_powergating_state(gate) of IP block <%s> failed %d\n",
1958 adev->ip_blocks[i].version->funcs->name, r);
1966 static int amdgpu_device_enable_mgpu_fan_boost(void)
1968 struct amdgpu_gpu_instance *gpu_ins;
1969 struct amdgpu_device *adev;
1972 mutex_lock(&mgpu_info.mutex);
1975 * MGPU fan boost feature should be enabled
1976 * only when there are two or more dGPUs in
1979 if (mgpu_info.num_dgpu < 2)
1982 for (i = 0; i < mgpu_info.num_dgpu; i++) {
1983 gpu_ins = &(mgpu_info.gpu_ins[i]);
1984 adev = gpu_ins->adev;
1985 if (!(adev->flags & AMD_IS_APU) &&
1986 !gpu_ins->mgpu_fan_enabled &&
1987 adev->powerplay.pp_funcs &&
1988 adev->powerplay.pp_funcs->enable_mgpu_fan_boost) {
1989 ret = amdgpu_dpm_enable_mgpu_fan_boost(adev);
1993 gpu_ins->mgpu_fan_enabled = 1;
1998 mutex_unlock(&mgpu_info.mutex);
2004 * amdgpu_device_ip_late_init - run late init for hardware IPs
2006 * @adev: amdgpu_device pointer
2008 * Late initialization pass for hardware IPs. The list of all the hardware
2009 * IPs that make up the asic is walked and the late_init callbacks are run.
2010 * late_init covers any special initialization that an IP requires
2011 * after all of the have been initialized or something that needs to happen
2012 * late in the init process.
2013 * Returns 0 on success, negative error code on failure.
2015 static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
2019 for (i = 0; i < adev->num_ip_blocks; i++) {
2020 if (!adev->ip_blocks[i].status.hw)
2022 if (adev->ip_blocks[i].version->funcs->late_init) {
2023 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
2025 DRM_ERROR("late_init of IP block <%s> failed %d\n",
2026 adev->ip_blocks[i].version->funcs->name, r);
2030 adev->ip_blocks[i].status.late_initialized = true;
2033 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE);
2034 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_GATE);
2036 amdgpu_device_fill_reset_magic(adev);
2038 r = amdgpu_device_enable_mgpu_fan_boost();
2040 DRM_ERROR("enable mgpu fan boost failed (%d).\n", r);
2042 /* set to low pstate by default */
2043 amdgpu_xgmi_set_pstate(adev, 0);
2049 * amdgpu_device_ip_fini - run fini for hardware IPs
2051 * @adev: amdgpu_device pointer
2053 * Main teardown pass for hardware IPs. The list of all the hardware
2054 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
2055 * are run. hw_fini tears down the hardware associated with each IP
2056 * and sw_fini tears down any software state associated with each IP.
2057 * Returns 0 on success, negative error code on failure.
2059 static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
2063 amdgpu_ras_pre_fini(adev);
2065 if (adev->gmc.xgmi.num_physical_nodes > 1)
2066 amdgpu_xgmi_remove_device(adev);
2068 amdgpu_amdkfd_device_fini(adev);
2070 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2071 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2073 /* need to disable SMC first */
2074 for (i = 0; i < adev->num_ip_blocks; i++) {
2075 if (!adev->ip_blocks[i].status.hw)
2077 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
2078 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2079 /* XXX handle errors */
2081 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2082 adev->ip_blocks[i].version->funcs->name, r);
2084 adev->ip_blocks[i].status.hw = false;
2089 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2090 if (!adev->ip_blocks[i].status.hw)
2093 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2094 /* XXX handle errors */
2096 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2097 adev->ip_blocks[i].version->funcs->name, r);
2100 adev->ip_blocks[i].status.hw = false;
2104 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2105 if (!adev->ip_blocks[i].status.sw)
2108 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
2109 amdgpu_ucode_free_bo(adev);
2110 amdgpu_free_static_csa(&adev->virt.csa_obj);
2111 amdgpu_device_wb_fini(adev);
2112 amdgpu_device_vram_scratch_fini(adev);
2113 amdgpu_ib_pool_fini(adev);
2116 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
2117 /* XXX handle errors */
2119 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
2120 adev->ip_blocks[i].version->funcs->name, r);
2122 adev->ip_blocks[i].status.sw = false;
2123 adev->ip_blocks[i].status.valid = false;
2126 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2127 if (!adev->ip_blocks[i].status.late_initialized)
2129 if (adev->ip_blocks[i].version->funcs->late_fini)
2130 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
2131 adev->ip_blocks[i].status.late_initialized = false;
2134 amdgpu_ras_fini(adev);
2136 if (amdgpu_sriov_vf(adev))
2137 if (amdgpu_virt_release_full_gpu(adev, false))
2138 DRM_ERROR("failed to release exclusive mode on fini\n");
2144 * amdgpu_device_delayed_init_work_handler - work handler for IB tests
2146 * @work: work_struct.
2148 static void amdgpu_device_delayed_init_work_handler(struct work_struct *work)
2150 struct amdgpu_device *adev =
2151 container_of(work, struct amdgpu_device, delayed_init_work.work);
2154 r = amdgpu_ib_ring_tests(adev);
2156 DRM_ERROR("ib ring test failed (%d).\n", r);
2159 static void amdgpu_device_delay_enable_gfx_off(struct work_struct *work)
2161 struct amdgpu_device *adev =
2162 container_of(work, struct amdgpu_device, gfx.gfx_off_delay_work.work);
2164 mutex_lock(&adev->gfx.gfx_off_mutex);
2165 if (!adev->gfx.gfx_off_state && !adev->gfx.gfx_off_req_count) {
2166 if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true))
2167 adev->gfx.gfx_off_state = true;
2169 mutex_unlock(&adev->gfx.gfx_off_mutex);
2173 * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1)
2175 * @adev: amdgpu_device pointer
2177 * Main suspend function for hardware IPs. The list of all the hardware
2178 * IPs that make up the asic is walked, clockgating is disabled and the
2179 * suspend callbacks are run. suspend puts the hardware and software state
2180 * in each IP into a state suitable for suspend.
2181 * Returns 0 on success, negative error code on failure.
2183 static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev)
2187 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2188 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2190 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2191 if (!adev->ip_blocks[i].status.valid)
2193 /* displays are handled separately */
2194 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) {
2195 /* XXX handle errors */
2196 r = adev->ip_blocks[i].version->funcs->suspend(adev);
2197 /* XXX handle errors */
2199 DRM_ERROR("suspend of IP block <%s> failed %d\n",
2200 adev->ip_blocks[i].version->funcs->name, r);
2203 adev->ip_blocks[i].status.hw = false;
2211 * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2)
2213 * @adev: amdgpu_device pointer
2215 * Main suspend function for hardware IPs. The list of all the hardware
2216 * IPs that make up the asic is walked, clockgating is disabled and the
2217 * suspend callbacks are run. suspend puts the hardware and software state
2218 * in each IP into a state suitable for suspend.
2219 * Returns 0 on success, negative error code on failure.
2221 static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev)
2225 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2226 if (!adev->ip_blocks[i].status.valid)
2228 /* displays are handled in phase1 */
2229 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)
2231 /* XXX handle errors */
2232 r = adev->ip_blocks[i].version->funcs->suspend(adev);
2233 /* XXX handle errors */
2235 DRM_ERROR("suspend of IP block <%s> failed %d\n",
2236 adev->ip_blocks[i].version->funcs->name, r);
2238 adev->ip_blocks[i].status.hw = false;
2239 /* handle putting the SMC in the appropriate state */
2240 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
2241 if (is_support_sw_smu(adev)) {
2242 r = smu_set_mp1_state(&adev->smu, adev->mp1_state);
2243 } else if (adev->powerplay.pp_funcs &&
2244 adev->powerplay.pp_funcs->set_mp1_state) {
2245 r = adev->powerplay.pp_funcs->set_mp1_state(
2246 adev->powerplay.pp_handle,
2250 DRM_ERROR("SMC failed to set mp1 state %d, %d\n",
2251 adev->mp1_state, r);
2256 adev->ip_blocks[i].status.hw = false;
2263 * amdgpu_device_ip_suspend - run suspend for hardware IPs
2265 * @adev: amdgpu_device pointer
2267 * Main suspend function for hardware IPs. The list of all the hardware
2268 * IPs that make up the asic is walked, clockgating is disabled and the
2269 * suspend callbacks are run. suspend puts the hardware and software state
2270 * in each IP into a state suitable for suspend.
2271 * Returns 0 on success, negative error code on failure.
2273 int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
2277 if (amdgpu_sriov_vf(adev))
2278 amdgpu_virt_request_full_gpu(adev, false);
2280 r = amdgpu_device_ip_suspend_phase1(adev);
2283 r = amdgpu_device_ip_suspend_phase2(adev);
2285 if (amdgpu_sriov_vf(adev))
2286 amdgpu_virt_release_full_gpu(adev, false);
2291 static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
2295 static enum amd_ip_block_type ip_order[] = {
2296 AMD_IP_BLOCK_TYPE_GMC,
2297 AMD_IP_BLOCK_TYPE_COMMON,
2298 AMD_IP_BLOCK_TYPE_PSP,
2299 AMD_IP_BLOCK_TYPE_IH,
2302 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
2304 struct amdgpu_ip_block *block;
2306 for (j = 0; j < adev->num_ip_blocks; j++) {
2307 block = &adev->ip_blocks[j];
2309 block->status.hw = false;
2310 if (block->version->type != ip_order[i] ||
2311 !block->status.valid)
2314 r = block->version->funcs->hw_init(adev);
2315 DRM_INFO("RE-INIT-early: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
2318 block->status.hw = true;
2325 static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
2329 static enum amd_ip_block_type ip_order[] = {
2330 AMD_IP_BLOCK_TYPE_SMC,
2331 AMD_IP_BLOCK_TYPE_DCE,
2332 AMD_IP_BLOCK_TYPE_GFX,
2333 AMD_IP_BLOCK_TYPE_SDMA,
2334 AMD_IP_BLOCK_TYPE_UVD,
2335 AMD_IP_BLOCK_TYPE_VCE
2338 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
2340 struct amdgpu_ip_block *block;
2342 for (j = 0; j < adev->num_ip_blocks; j++) {
2343 block = &adev->ip_blocks[j];
2345 if (block->version->type != ip_order[i] ||
2346 !block->status.valid ||
2350 r = block->version->funcs->hw_init(adev);
2351 DRM_INFO("RE-INIT-late: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
2354 block->status.hw = true;
2362 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
2364 * @adev: amdgpu_device pointer
2366 * First resume function for hardware IPs. The list of all the hardware
2367 * IPs that make up the asic is walked and the resume callbacks are run for
2368 * COMMON, GMC, and IH. resume puts the hardware into a functional state
2369 * after a suspend and updates the software state as necessary. This
2370 * function is also used for restoring the GPU after a GPU reset.
2371 * Returns 0 on success, negative error code on failure.
2373 static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
2377 for (i = 0; i < adev->num_ip_blocks; i++) {
2378 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
2380 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2381 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2382 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
2384 r = adev->ip_blocks[i].version->funcs->resume(adev);
2386 DRM_ERROR("resume of IP block <%s> failed %d\n",
2387 adev->ip_blocks[i].version->funcs->name, r);
2390 adev->ip_blocks[i].status.hw = true;
2398 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
2400 * @adev: amdgpu_device pointer
2402 * First resume function for hardware IPs. The list of all the hardware
2403 * IPs that make up the asic is walked and the resume callbacks are run for
2404 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
2405 * functional state after a suspend and updates the software state as
2406 * necessary. This function is also used for restoring the GPU after a GPU
2408 * Returns 0 on success, negative error code on failure.
2410 static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
2414 for (i = 0; i < adev->num_ip_blocks; i++) {
2415 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
2417 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2418 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2419 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
2420 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)
2422 r = adev->ip_blocks[i].version->funcs->resume(adev);
2424 DRM_ERROR("resume of IP block <%s> failed %d\n",
2425 adev->ip_blocks[i].version->funcs->name, r);
2428 adev->ip_blocks[i].status.hw = true;
2435 * amdgpu_device_ip_resume - run resume for hardware IPs
2437 * @adev: amdgpu_device pointer
2439 * Main resume function for hardware IPs. The hardware IPs
2440 * are split into two resume functions because they are
2441 * are also used in in recovering from a GPU reset and some additional
2442 * steps need to be take between them. In this case (S3/S4) they are
2444 * Returns 0 on success, negative error code on failure.
2446 static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
2450 r = amdgpu_device_ip_resume_phase1(adev);
2454 r = amdgpu_device_fw_loading(adev);
2458 r = amdgpu_device_ip_resume_phase2(adev);
2464 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
2466 * @adev: amdgpu_device pointer
2468 * Query the VBIOS data tables to determine if the board supports SR-IOV.
2470 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
2472 if (amdgpu_sriov_vf(adev)) {
2473 if (adev->is_atom_fw) {
2474 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2475 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2477 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2478 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2481 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
2482 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
2487 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
2489 * @asic_type: AMD asic type
2491 * Check if there is DC (new modesetting infrastructre) support for an asic.
2492 * returns true if DC has support, false if not.
2494 bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
2496 switch (asic_type) {
2497 #if defined(CONFIG_DRM_AMD_DC)
2503 * We have systems in the wild with these ASICs that require
2504 * LVDS and VGA support which is not supported with DC.
2506 * Fallback to the non-DC driver here by default so as not to
2507 * cause regressions.
2509 return amdgpu_dc > 0;
2513 case CHIP_POLARIS10:
2514 case CHIP_POLARIS11:
2515 case CHIP_POLARIS12:
2522 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
2525 #if defined(CONFIG_DRM_AMD_DC_DCN2_0)
2530 #if defined(CONFIG_DRM_AMD_DC_DCN2_1)
2533 return amdgpu_dc != 0;
2541 * amdgpu_device_has_dc_support - check if dc is supported
2543 * @adev: amdgpu_device_pointer
2545 * Returns true for supported, false for not supported
2547 bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
2549 if (amdgpu_sriov_vf(adev))
2552 return amdgpu_device_asic_has_dc_support(adev->asic_type);
2556 static void amdgpu_device_xgmi_reset_func(struct work_struct *__work)
2558 struct amdgpu_device *adev =
2559 container_of(__work, struct amdgpu_device, xgmi_reset_work);
2561 adev->asic_reset_res = amdgpu_asic_reset(adev);
2562 if (adev->asic_reset_res)
2563 DRM_WARN("ASIC reset failed with error, %d for drm dev, %s",
2564 adev->asic_reset_res, adev->ddev->unique);
2569 * amdgpu_device_init - initialize the driver
2571 * @adev: amdgpu_device pointer
2572 * @ddev: drm dev pointer
2573 * @pdev: pci dev pointer
2574 * @flags: driver flags
2576 * Initializes the driver info and hw (all asics).
2577 * Returns 0 for success or an error on failure.
2578 * Called at driver startup.
2580 int amdgpu_device_init(struct amdgpu_device *adev,
2581 struct drm_device *ddev,
2582 struct pci_dev *pdev,
2586 bool runtime = false;
2589 adev->shutdown = false;
2590 adev->dev = &pdev->dev;
2593 adev->flags = flags;
2595 if (amdgpu_force_asic_type >= 0 && amdgpu_force_asic_type < CHIP_LAST)
2596 adev->asic_type = amdgpu_force_asic_type;
2598 adev->asic_type = flags & AMD_ASIC_MASK;
2600 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
2601 if (amdgpu_emu_mode == 1)
2602 adev->usec_timeout *= 2;
2603 adev->gmc.gart_size = 512 * 1024 * 1024;
2604 adev->accel_working = false;
2605 adev->num_rings = 0;
2606 adev->mman.buffer_funcs = NULL;
2607 adev->mman.buffer_funcs_ring = NULL;
2608 adev->vm_manager.vm_pte_funcs = NULL;
2609 adev->vm_manager.vm_pte_num_rqs = 0;
2610 adev->gmc.gmc_funcs = NULL;
2611 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
2612 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
2614 adev->smc_rreg = &amdgpu_invalid_rreg;
2615 adev->smc_wreg = &amdgpu_invalid_wreg;
2616 adev->pcie_rreg = &amdgpu_invalid_rreg;
2617 adev->pcie_wreg = &amdgpu_invalid_wreg;
2618 adev->pciep_rreg = &amdgpu_invalid_rreg;
2619 adev->pciep_wreg = &amdgpu_invalid_wreg;
2620 adev->pcie_rreg64 = &amdgpu_invalid_rreg64;
2621 adev->pcie_wreg64 = &amdgpu_invalid_wreg64;
2622 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2623 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2624 adev->didt_rreg = &amdgpu_invalid_rreg;
2625 adev->didt_wreg = &amdgpu_invalid_wreg;
2626 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2627 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
2628 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2629 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2631 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2632 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2633 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
2635 /* mutex initialization are all done here so we
2636 * can recall function without having locking issues */
2637 atomic_set(&adev->irq.ih.lock, 0);
2638 mutex_init(&adev->firmware.mutex);
2639 mutex_init(&adev->pm.mutex);
2640 mutex_init(&adev->gfx.gpu_clock_mutex);
2641 mutex_init(&adev->srbm_mutex);
2642 mutex_init(&adev->gfx.pipe_reserve_mutex);
2643 mutex_init(&adev->gfx.gfx_off_mutex);
2644 mutex_init(&adev->grbm_idx_mutex);
2645 mutex_init(&adev->mn_lock);
2646 mutex_init(&adev->virt.vf_errors.lock);
2647 hash_init(adev->mn_hash);
2648 mutex_init(&adev->lock_reset);
2649 mutex_init(&adev->virt.dpm_mutex);
2650 mutex_init(&adev->psp.mutex);
2652 r = amdgpu_device_check_arguments(adev);
2656 spin_lock_init(&adev->mmio_idx_lock);
2657 spin_lock_init(&adev->smc_idx_lock);
2658 spin_lock_init(&adev->pcie_idx_lock);
2659 spin_lock_init(&adev->uvd_ctx_idx_lock);
2660 spin_lock_init(&adev->didt_idx_lock);
2661 spin_lock_init(&adev->gc_cac_idx_lock);
2662 spin_lock_init(&adev->se_cac_idx_lock);
2663 spin_lock_init(&adev->audio_endpt_idx_lock);
2664 spin_lock_init(&adev->mm_stats.lock);
2666 INIT_LIST_HEAD(&adev->shadow_list);
2667 mutex_init(&adev->shadow_list_lock);
2669 INIT_LIST_HEAD(&adev->ring_lru_list);
2670 spin_lock_init(&adev->ring_lru_list_lock);
2672 INIT_DELAYED_WORK(&adev->delayed_init_work,
2673 amdgpu_device_delayed_init_work_handler);
2674 INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work,
2675 amdgpu_device_delay_enable_gfx_off);
2677 INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
2679 adev->gfx.gfx_off_req_count = 1;
2680 adev->pm.ac_power = power_supply_is_system_supplied() > 0 ? true : false;
2682 /* Registers mapping */
2683 /* TODO: block userspace mapping of io register */
2684 if (adev->asic_type >= CHIP_BONAIRE) {
2685 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2686 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2688 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2689 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2692 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2693 if (adev->rmmio == NULL) {
2696 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2697 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2699 /* io port mapping */
2700 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2701 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2702 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2703 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2707 if (adev->rio_mem == NULL)
2708 DRM_INFO("PCI I/O BAR is not found.\n");
2710 /* enable PCIE atomic ops */
2711 r = pci_enable_atomic_ops_to_root(adev->pdev,
2712 PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
2713 PCI_EXP_DEVCAP2_ATOMIC_COMP64);
2715 adev->have_atomics_support = false;
2716 DRM_INFO("PCIE atomic ops is not supported\n");
2718 adev->have_atomics_support = true;
2721 amdgpu_device_get_pcie_info(adev);
2724 DRM_INFO("MCBP is enabled\n");
2726 if (amdgpu_mes && adev->asic_type >= CHIP_NAVI10)
2727 adev->enable_mes = true;
2729 if (amdgpu_discovery && adev->asic_type >= CHIP_NAVI10) {
2730 r = amdgpu_discovery_init(adev);
2732 dev_err(adev->dev, "amdgpu_discovery_init failed\n");
2737 /* early init functions */
2738 r = amdgpu_device_ip_early_init(adev);
2742 r = amdgpu_device_get_job_timeout_settings(adev);
2744 dev_err(adev->dev, "invalid lockup_timeout parameter syntax\n");
2748 /* doorbell bar mapping and doorbell index init*/
2749 amdgpu_device_doorbell_init(adev);
2751 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2752 /* this will fail for cards that aren't VGA class devices, just
2754 vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
2756 if (amdgpu_device_is_px(ddev))
2758 if (!pci_is_thunderbolt_attached(adev->pdev))
2759 vga_switcheroo_register_client(adev->pdev,
2760 &amdgpu_switcheroo_ops, runtime);
2762 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2764 if (amdgpu_emu_mode == 1) {
2765 /* post the asic on emulation mode */
2766 emu_soc_asic_init(adev);
2767 goto fence_driver_init;
2770 /* detect if we are with an SRIOV vbios */
2771 amdgpu_device_detect_sriov_bios(adev);
2773 /* check if we need to reset the asic
2774 * E.g., driver was not cleanly unloaded previously, etc.
2776 if (!amdgpu_sriov_vf(adev) && amdgpu_asic_need_reset_on_init(adev)) {
2777 r = amdgpu_asic_reset(adev);
2779 dev_err(adev->dev, "asic reset on init failed\n");
2784 /* Post card if necessary */
2785 if (amdgpu_device_need_post(adev)) {
2787 dev_err(adev->dev, "no vBIOS found\n");
2791 DRM_INFO("GPU posting now...\n");
2792 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2794 dev_err(adev->dev, "gpu post error!\n");
2799 if (adev->is_atom_fw) {
2800 /* Initialize clocks */
2801 r = amdgpu_atomfirmware_get_clock_info(adev);
2803 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
2804 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2808 /* Initialize clocks */
2809 r = amdgpu_atombios_get_clock_info(adev);
2811 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
2812 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2815 /* init i2c buses */
2816 if (!amdgpu_device_has_dc_support(adev))
2817 amdgpu_atombios_i2c_init(adev);
2822 r = amdgpu_fence_driver_init(adev);
2824 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
2825 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
2829 /* init the mode config */
2830 drm_mode_config_init(adev->ddev);
2832 r = amdgpu_device_ip_init(adev);
2834 /* failed in exclusive mode due to timeout */
2835 if (amdgpu_sriov_vf(adev) &&
2836 !amdgpu_sriov_runtime(adev) &&
2837 amdgpu_virt_mmio_blocked(adev) &&
2838 !amdgpu_virt_wait_reset(adev)) {
2839 dev_err(adev->dev, "VF exclusive mode timeout\n");
2840 /* Don't send request since VF is inactive. */
2841 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
2842 adev->virt.ops = NULL;
2846 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
2847 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
2848 if (amdgpu_virt_request_full_gpu(adev, false))
2849 amdgpu_virt_release_full_gpu(adev, false);
2853 adev->accel_working = true;
2855 amdgpu_vm_check_compute_bug(adev);
2857 /* Initialize the buffer migration limit. */
2858 if (amdgpu_moverate >= 0)
2859 max_MBps = amdgpu_moverate;
2861 max_MBps = 8; /* Allow 8 MB/s. */
2862 /* Get a log2 for easy divisions. */
2863 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2865 amdgpu_fbdev_init(adev);
2867 if (amdgpu_sriov_vf(adev) && amdgim_is_hwperf(adev))
2868 amdgpu_pm_virt_sysfs_init(adev);
2870 r = amdgpu_pm_sysfs_init(adev);
2872 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2874 r = amdgpu_ucode_sysfs_init(adev);
2876 DRM_ERROR("Creating firmware sysfs failed (%d).\n", r);
2878 r = amdgpu_debugfs_gem_init(adev);
2880 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
2882 r = amdgpu_debugfs_regs_init(adev);
2884 DRM_ERROR("registering register debugfs failed (%d).\n", r);
2886 r = amdgpu_debugfs_firmware_init(adev);
2888 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
2890 r = amdgpu_debugfs_init(adev);
2892 DRM_ERROR("Creating debugfs files failed (%d).\n", r);
2894 if ((amdgpu_testing & 1)) {
2895 if (adev->accel_working)
2896 amdgpu_test_moves(adev);
2898 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2900 if (amdgpu_benchmarking) {
2901 if (adev->accel_working)
2902 amdgpu_benchmark(adev, amdgpu_benchmarking);
2904 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2907 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2908 * explicit gating rather than handling it automatically.
2910 r = amdgpu_device_ip_late_init(adev);
2912 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
2913 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
2918 amdgpu_ras_resume(adev);
2920 queue_delayed_work(system_wq, &adev->delayed_init_work,
2921 msecs_to_jiffies(AMDGPU_RESUME_MS));
2923 r = device_create_file(adev->dev, &dev_attr_pcie_replay_count);
2925 dev_err(adev->dev, "Could not create pcie_replay_count");
2929 if (IS_ENABLED(CONFIG_PERF_EVENTS))
2930 r = amdgpu_pmu_init(adev);
2932 dev_err(adev->dev, "amdgpu_pmu_init failed\n");
2937 amdgpu_vf_error_trans_all(adev);
2939 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2945 * amdgpu_device_fini - tear down the driver
2947 * @adev: amdgpu_device pointer
2949 * Tear down the driver info (all asics).
2950 * Called at driver shutdown.
2952 void amdgpu_device_fini(struct amdgpu_device *adev)
2956 DRM_INFO("amdgpu: finishing device.\n");
2957 adev->shutdown = true;
2958 /* disable all interrupts */
2959 amdgpu_irq_disable_all(adev);
2960 if (adev->mode_info.mode_config_initialized){
2961 if (!amdgpu_device_has_dc_support(adev))
2962 drm_helper_force_disable_all(adev->ddev);
2964 drm_atomic_helper_shutdown(adev->ddev);
2966 amdgpu_fence_driver_fini(adev);
2967 amdgpu_pm_sysfs_fini(adev);
2968 amdgpu_fbdev_fini(adev);
2969 r = amdgpu_device_ip_fini(adev);
2970 if (adev->firmware.gpu_info_fw) {
2971 release_firmware(adev->firmware.gpu_info_fw);
2972 adev->firmware.gpu_info_fw = NULL;
2974 adev->accel_working = false;
2975 cancel_delayed_work_sync(&adev->delayed_init_work);
2976 /* free i2c buses */
2977 if (!amdgpu_device_has_dc_support(adev))
2978 amdgpu_i2c_fini(adev);
2980 if (amdgpu_emu_mode != 1)
2981 amdgpu_atombios_fini(adev);
2985 if (!pci_is_thunderbolt_attached(adev->pdev))
2986 vga_switcheroo_unregister_client(adev->pdev);
2987 if (adev->flags & AMD_IS_PX)
2988 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2989 vga_client_register(adev->pdev, NULL, NULL, NULL);
2991 pci_iounmap(adev->pdev, adev->rio_mem);
2992 adev->rio_mem = NULL;
2993 iounmap(adev->rmmio);
2995 amdgpu_device_doorbell_fini(adev);
2996 if (amdgpu_sriov_vf(adev) && amdgim_is_hwperf(adev))
2997 amdgpu_pm_virt_sysfs_fini(adev);
2999 amdgpu_debugfs_regs_cleanup(adev);
3000 device_remove_file(adev->dev, &dev_attr_pcie_replay_count);
3001 amdgpu_ucode_sysfs_fini(adev);
3002 if (IS_ENABLED(CONFIG_PERF_EVENTS))
3003 amdgpu_pmu_fini(adev);
3004 amdgpu_debugfs_preempt_cleanup(adev);
3005 if (amdgpu_discovery && adev->asic_type >= CHIP_NAVI10)
3006 amdgpu_discovery_fini(adev);
3014 * amdgpu_device_suspend - initiate device suspend
3016 * @dev: drm dev pointer
3017 * @suspend: suspend state
3018 * @fbcon : notify the fbdev of suspend
3020 * Puts the hw in the suspend state (all asics).
3021 * Returns 0 for success or an error on failure.
3022 * Called at driver suspend.
3024 int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
3026 struct amdgpu_device *adev;
3027 struct drm_crtc *crtc;
3028 struct drm_connector *connector;
3029 struct drm_connector_list_iter iter;
3032 if (dev == NULL || dev->dev_private == NULL) {
3036 adev = dev->dev_private;
3038 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
3041 adev->in_suspend = true;
3042 drm_kms_helper_poll_disable(dev);
3045 amdgpu_fbdev_set_suspend(adev, 1);
3047 cancel_delayed_work_sync(&adev->delayed_init_work);
3049 if (!amdgpu_device_has_dc_support(adev)) {
3050 /* turn off display hw */
3051 drm_modeset_lock_all(dev);
3052 drm_connector_list_iter_begin(dev, &iter);
3053 drm_for_each_connector_iter(connector, &iter)
3054 drm_helper_connector_dpms(connector,
3056 drm_connector_list_iter_end(&iter);
3057 drm_modeset_unlock_all(dev);
3058 /* unpin the front buffers and cursors */
3059 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3060 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
3061 struct drm_framebuffer *fb = crtc->primary->fb;
3062 struct amdgpu_bo *robj;
3064 if (amdgpu_crtc->cursor_bo && !adev->enable_virtual_display) {
3065 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
3066 r = amdgpu_bo_reserve(aobj, true);
3068 amdgpu_bo_unpin(aobj);
3069 amdgpu_bo_unreserve(aobj);
3073 if (fb == NULL || fb->obj[0] == NULL) {
3076 robj = gem_to_amdgpu_bo(fb->obj[0]);
3077 /* don't unpin kernel fb objects */
3078 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
3079 r = amdgpu_bo_reserve(robj, true);
3081 amdgpu_bo_unpin(robj);
3082 amdgpu_bo_unreserve(robj);
3088 amdgpu_amdkfd_suspend(adev);
3090 amdgpu_ras_suspend(adev);
3092 r = amdgpu_device_ip_suspend_phase1(adev);
3094 /* evict vram memory */
3095 amdgpu_bo_evict_vram(adev);
3097 amdgpu_fence_driver_suspend(adev);
3099 r = amdgpu_device_ip_suspend_phase2(adev);
3101 /* evict remaining vram memory
3102 * This second call to evict vram is to evict the gart page table
3105 amdgpu_bo_evict_vram(adev);
3107 pci_save_state(dev->pdev);
3109 /* Shut down the device */
3110 pci_disable_device(dev->pdev);
3111 pci_set_power_state(dev->pdev, PCI_D3hot);
3113 r = amdgpu_asic_reset(adev);
3115 DRM_ERROR("amdgpu asic reset failed\n");
3122 * amdgpu_device_resume - initiate device resume
3124 * @dev: drm dev pointer
3125 * @resume: resume state
3126 * @fbcon : notify the fbdev of resume
3128 * Bring the hw back to operating state (all asics).
3129 * Returns 0 for success or an error on failure.
3130 * Called at driver resume.
3132 int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
3134 struct drm_connector *connector;
3135 struct drm_connector_list_iter iter;
3136 struct amdgpu_device *adev = dev->dev_private;
3137 struct drm_crtc *crtc;
3140 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
3144 pci_set_power_state(dev->pdev, PCI_D0);
3145 pci_restore_state(dev->pdev);
3146 r = pci_enable_device(dev->pdev);
3152 if (amdgpu_device_need_post(adev)) {
3153 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
3155 DRM_ERROR("amdgpu asic init failed\n");
3158 r = amdgpu_device_ip_resume(adev);
3160 DRM_ERROR("amdgpu_device_ip_resume failed (%d).\n", r);
3163 amdgpu_fence_driver_resume(adev);
3166 r = amdgpu_device_ip_late_init(adev);
3170 queue_delayed_work(system_wq, &adev->delayed_init_work,
3171 msecs_to_jiffies(AMDGPU_RESUME_MS));
3173 if (!amdgpu_device_has_dc_support(adev)) {
3175 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3176 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
3178 if (amdgpu_crtc->cursor_bo && !adev->enable_virtual_display) {
3179 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
3180 r = amdgpu_bo_reserve(aobj, true);
3182 r = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM);
3184 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
3185 amdgpu_crtc->cursor_addr = amdgpu_bo_gpu_offset(aobj);
3186 amdgpu_bo_unreserve(aobj);
3191 r = amdgpu_amdkfd_resume(adev);
3195 /* Make sure IB tests flushed */
3196 flush_delayed_work(&adev->delayed_init_work);
3198 /* blat the mode back in */
3200 if (!amdgpu_device_has_dc_support(adev)) {
3202 drm_helper_resume_force_mode(dev);
3204 /* turn on display hw */
3205 drm_modeset_lock_all(dev);
3207 drm_connector_list_iter_begin(dev, &iter);
3208 drm_for_each_connector_iter(connector, &iter)
3209 drm_helper_connector_dpms(connector,
3211 drm_connector_list_iter_end(&iter);
3213 drm_modeset_unlock_all(dev);
3215 amdgpu_fbdev_set_suspend(adev, 0);
3218 drm_kms_helper_poll_enable(dev);
3220 amdgpu_ras_resume(adev);
3223 * Most of the connector probing functions try to acquire runtime pm
3224 * refs to ensure that the GPU is powered on when connector polling is
3225 * performed. Since we're calling this from a runtime PM callback,
3226 * trying to acquire rpm refs will cause us to deadlock.
3228 * Since we're guaranteed to be holding the rpm lock, it's safe to
3229 * temporarily disable the rpm helpers so this doesn't deadlock us.
3232 dev->dev->power.disable_depth++;
3234 if (!amdgpu_device_has_dc_support(adev))
3235 drm_helper_hpd_irq_event(dev);
3237 drm_kms_helper_hotplug_event(dev);
3239 dev->dev->power.disable_depth--;
3241 adev->in_suspend = false;
3247 * amdgpu_device_ip_check_soft_reset - did soft reset succeed
3249 * @adev: amdgpu_device pointer
3251 * The list of all the hardware IPs that make up the asic is walked and
3252 * the check_soft_reset callbacks are run. check_soft_reset determines
3253 * if the asic is still hung or not.
3254 * Returns true if any of the IPs are still in a hung state, false if not.
3256 static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
3259 bool asic_hang = false;
3261 if (amdgpu_sriov_vf(adev))
3264 if (amdgpu_asic_need_full_reset(adev))
3267 for (i = 0; i < adev->num_ip_blocks; i++) {
3268 if (!adev->ip_blocks[i].status.valid)
3270 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
3271 adev->ip_blocks[i].status.hang =
3272 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
3273 if (adev->ip_blocks[i].status.hang) {
3274 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
3282 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
3284 * @adev: amdgpu_device pointer
3286 * The list of all the hardware IPs that make up the asic is walked and the
3287 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
3288 * handles any IP specific hardware or software state changes that are
3289 * necessary for a soft reset to succeed.
3290 * Returns 0 on success, negative error code on failure.
3292 static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
3296 for (i = 0; i < adev->num_ip_blocks; i++) {
3297 if (!adev->ip_blocks[i].status.valid)
3299 if (adev->ip_blocks[i].status.hang &&
3300 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
3301 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
3311 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
3313 * @adev: amdgpu_device pointer
3315 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
3316 * reset is necessary to recover.
3317 * Returns true if a full asic reset is required, false if not.
3319 static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
3323 if (amdgpu_asic_need_full_reset(adev))
3326 for (i = 0; i < adev->num_ip_blocks; i++) {
3327 if (!adev->ip_blocks[i].status.valid)
3329 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
3330 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
3331 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
3332 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
3333 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
3334 if (adev->ip_blocks[i].status.hang) {
3335 DRM_INFO("Some block need full reset!\n");
3344 * amdgpu_device_ip_soft_reset - do a soft reset
3346 * @adev: amdgpu_device pointer
3348 * The list of all the hardware IPs that make up the asic is walked and the
3349 * soft_reset callbacks are run if the block is hung. soft_reset handles any
3350 * IP specific hardware or software state changes that are necessary to soft
3352 * Returns 0 on success, negative error code on failure.
3354 static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
3358 for (i = 0; i < adev->num_ip_blocks; i++) {
3359 if (!adev->ip_blocks[i].status.valid)
3361 if (adev->ip_blocks[i].status.hang &&
3362 adev->ip_blocks[i].version->funcs->soft_reset) {
3363 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
3373 * amdgpu_device_ip_post_soft_reset - clean up from soft reset
3375 * @adev: amdgpu_device pointer
3377 * The list of all the hardware IPs that make up the asic is walked and the
3378 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
3379 * handles any IP specific hardware or software state changes that are
3380 * necessary after the IP has been soft reset.
3381 * Returns 0 on success, negative error code on failure.
3383 static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
3387 for (i = 0; i < adev->num_ip_blocks; i++) {
3388 if (!adev->ip_blocks[i].status.valid)
3390 if (adev->ip_blocks[i].status.hang &&
3391 adev->ip_blocks[i].version->funcs->post_soft_reset)
3392 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
3401 * amdgpu_device_recover_vram - Recover some VRAM contents
3403 * @adev: amdgpu_device pointer
3405 * Restores the contents of VRAM buffers from the shadows in GTT. Used to
3406 * restore things like GPUVM page tables after a GPU reset where
3407 * the contents of VRAM might be lost.
3410 * 0 on success, negative error code on failure.
3412 static int amdgpu_device_recover_vram(struct amdgpu_device *adev)
3414 struct dma_fence *fence = NULL, *next = NULL;
3415 struct amdgpu_bo *shadow;
3418 if (amdgpu_sriov_runtime(adev))
3419 tmo = msecs_to_jiffies(8000);
3421 tmo = msecs_to_jiffies(100);
3423 DRM_INFO("recover vram bo from shadow start\n");
3424 mutex_lock(&adev->shadow_list_lock);
3425 list_for_each_entry(shadow, &adev->shadow_list, shadow_list) {
3427 /* No need to recover an evicted BO */
3428 if (shadow->tbo.mem.mem_type != TTM_PL_TT ||
3429 shadow->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET ||
3430 shadow->parent->tbo.mem.mem_type != TTM_PL_VRAM)
3433 r = amdgpu_bo_restore_shadow(shadow, &next);
3438 tmo = dma_fence_wait_timeout(fence, false, tmo);
3439 dma_fence_put(fence);
3444 } else if (tmo < 0) {
3452 mutex_unlock(&adev->shadow_list_lock);
3455 tmo = dma_fence_wait_timeout(fence, false, tmo);
3456 dma_fence_put(fence);
3458 if (r < 0 || tmo <= 0) {
3459 DRM_ERROR("recover vram bo from shadow failed, r is %ld, tmo is %ld\n", r, tmo);
3463 DRM_INFO("recover vram bo from shadow done\n");
3469 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
3471 * @adev: amdgpu device pointer
3472 * @from_hypervisor: request from hypervisor
3474 * do VF FLR and reinitialize Asic
3475 * return 0 means succeeded otherwise failed
3477 static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
3478 bool from_hypervisor)
3482 if (from_hypervisor)
3483 r = amdgpu_virt_request_full_gpu(adev, true);
3485 r = amdgpu_virt_reset_gpu(adev);
3489 amdgpu_amdkfd_pre_reset(adev);
3491 /* Resume IP prior to SMC */
3492 r = amdgpu_device_ip_reinit_early_sriov(adev);
3496 /* we need recover gart prior to run SMC/CP/SDMA resume */
3497 amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
3499 r = amdgpu_device_fw_loading(adev);
3503 /* now we are okay to resume SMC/CP/SDMA */
3504 r = amdgpu_device_ip_reinit_late_sriov(adev);
3508 amdgpu_irq_gpu_reset_resume_helper(adev);
3509 r = amdgpu_ib_ring_tests(adev);
3510 amdgpu_amdkfd_post_reset(adev);
3513 amdgpu_virt_init_data_exchange(adev);
3514 amdgpu_virt_release_full_gpu(adev, true);
3515 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
3516 amdgpu_inc_vram_lost(adev);
3517 r = amdgpu_device_recover_vram(adev);
3524 * amdgpu_device_should_recover_gpu - check if we should try GPU recovery
3526 * @adev: amdgpu device pointer
3528 * Check amdgpu_gpu_recovery and SRIOV status to see if we should try to recover
3531 bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev)
3533 if (!amdgpu_device_ip_check_soft_reset(adev)) {
3534 DRM_INFO("Timeout, but no hardware hang detected.\n");
3538 if (amdgpu_gpu_recovery == 0)
3541 if (amdgpu_sriov_vf(adev))
3544 if (amdgpu_gpu_recovery == -1) {
3545 switch (adev->asic_type) {
3551 case CHIP_POLARIS10:
3552 case CHIP_POLARIS11:
3553 case CHIP_POLARIS12:
3568 DRM_INFO("GPU recovery disabled.\n");
3573 static int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
3574 struct amdgpu_job *job,
3575 bool *need_full_reset_arg)
3578 bool need_full_reset = *need_full_reset_arg;
3580 /* block all schedulers and reset given job's ring */
3581 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3582 struct amdgpu_ring *ring = adev->rings[i];
3584 if (!ring || !ring->sched.thread)
3587 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
3588 amdgpu_fence_driver_force_completion(ring);
3592 drm_sched_increase_karma(&job->base);
3594 /* Don't suspend on bare metal if we are not going to HW reset the ASIC */
3595 if (!amdgpu_sriov_vf(adev)) {
3597 if (!need_full_reset)
3598 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
3600 if (!need_full_reset) {
3601 amdgpu_device_ip_pre_soft_reset(adev);
3602 r = amdgpu_device_ip_soft_reset(adev);
3603 amdgpu_device_ip_post_soft_reset(adev);
3604 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
3605 DRM_INFO("soft reset failed, will fallback to full reset!\n");
3606 need_full_reset = true;
3610 if (need_full_reset)
3611 r = amdgpu_device_ip_suspend(adev);
3613 *need_full_reset_arg = need_full_reset;
3619 static int amdgpu_do_asic_reset(struct amdgpu_hive_info *hive,
3620 struct list_head *device_list_handle,
3621 bool *need_full_reset_arg)
3623 struct amdgpu_device *tmp_adev = NULL;
3624 bool need_full_reset = *need_full_reset_arg, vram_lost = false;
3628 * ASIC reset has to be done on all HGMI hive nodes ASAP
3629 * to allow proper links negotiation in FW (within 1 sec)
3631 if (need_full_reset) {
3632 list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
3633 /* For XGMI run all resets in parallel to speed up the process */
3634 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
3635 if (!queue_work(system_highpri_wq, &tmp_adev->xgmi_reset_work))
3638 r = amdgpu_asic_reset(tmp_adev);
3641 DRM_ERROR("ASIC reset failed with error, %d for drm dev, %s",
3642 r, tmp_adev->ddev->unique);
3647 /* For XGMI wait for all PSP resets to complete before proceed */
3649 list_for_each_entry(tmp_adev, device_list_handle,
3651 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
3652 flush_work(&tmp_adev->xgmi_reset_work);
3653 r = tmp_adev->asic_reset_res;
3662 list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
3663 if (need_full_reset) {
3665 if (amdgpu_atom_asic_init(tmp_adev->mode_info.atom_context))
3666 DRM_WARN("asic atom init failed!");
3669 dev_info(tmp_adev->dev, "GPU reset succeeded, trying to resume\n");
3670 r = amdgpu_device_ip_resume_phase1(tmp_adev);
3674 vram_lost = amdgpu_device_check_vram_lost(tmp_adev);
3676 DRM_INFO("VRAM is lost due to GPU reset!\n");
3677 amdgpu_inc_vram_lost(tmp_adev);
3680 r = amdgpu_gtt_mgr_recover(
3681 &tmp_adev->mman.bdev.man[TTM_PL_TT]);
3685 r = amdgpu_device_fw_loading(tmp_adev);
3689 r = amdgpu_device_ip_resume_phase2(tmp_adev);
3694 amdgpu_device_fill_reset_magic(tmp_adev);
3697 * Add this ASIC as tracked as reset was already
3698 * complete successfully.
3700 amdgpu_register_gpu_instance(tmp_adev);
3702 r = amdgpu_device_ip_late_init(tmp_adev);
3707 amdgpu_ras_resume(tmp_adev);
3709 /* Update PSP FW topology after reset */
3710 if (hive && tmp_adev->gmc.xgmi.num_physical_nodes > 1)
3711 r = amdgpu_xgmi_update_topology(hive, tmp_adev);
3718 amdgpu_irq_gpu_reset_resume_helper(tmp_adev);
3719 r = amdgpu_ib_ring_tests(tmp_adev);
3721 dev_err(tmp_adev->dev, "ib ring test failed (%d).\n", r);
3722 r = amdgpu_device_ip_suspend(tmp_adev);
3723 need_full_reset = true;
3730 r = amdgpu_device_recover_vram(tmp_adev);
3732 tmp_adev->asic_reset_res = r;
3736 *need_full_reset_arg = need_full_reset;
3740 static bool amdgpu_device_lock_adev(struct amdgpu_device *adev, bool trylock)
3743 if (!mutex_trylock(&adev->lock_reset))
3746 mutex_lock(&adev->lock_reset);
3748 atomic_inc(&adev->gpu_reset_counter);
3749 adev->in_gpu_reset = 1;
3750 switch (amdgpu_asic_reset_method(adev)) {
3751 case AMD_RESET_METHOD_MODE1:
3752 adev->mp1_state = PP_MP1_STATE_SHUTDOWN;
3754 case AMD_RESET_METHOD_MODE2:
3755 adev->mp1_state = PP_MP1_STATE_RESET;
3758 adev->mp1_state = PP_MP1_STATE_NONE;
3765 static void amdgpu_device_unlock_adev(struct amdgpu_device *adev)
3767 amdgpu_vf_error_trans_all(adev);
3768 adev->mp1_state = PP_MP1_STATE_NONE;
3769 adev->in_gpu_reset = 0;
3770 mutex_unlock(&adev->lock_reset);
3774 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
3776 * @adev: amdgpu device pointer
3777 * @job: which job trigger hang
3779 * Attempt to reset the GPU if it has hung (all asics).
3780 * Attempt to do soft-reset or full-reset and reinitialize Asic
3781 * Returns 0 for success or an error on failure.
3784 int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
3785 struct amdgpu_job *job)
3787 struct list_head device_list, *device_list_handle = NULL;
3788 bool need_full_reset, job_signaled;
3789 struct amdgpu_hive_info *hive = NULL;
3790 struct amdgpu_device *tmp_adev = NULL;
3792 bool in_ras_intr = amdgpu_ras_intr_triggered();
3795 * Flush RAM to disk so that after reboot
3796 * the user can read log and see why the system rebooted.
3798 if (in_ras_intr && amdgpu_ras_get_context(adev)->reboot) {
3800 DRM_WARN("Emergency reboot.");
3803 emergency_restart();
3806 need_full_reset = job_signaled = false;
3807 INIT_LIST_HEAD(&device_list);
3809 dev_info(adev->dev, "GPU %s begin!\n", in_ras_intr ? "jobs stop":"reset");
3811 cancel_delayed_work_sync(&adev->delayed_init_work);
3813 hive = amdgpu_get_xgmi_hive(adev, false);
3816 * Here we trylock to avoid chain of resets executing from
3817 * either trigger by jobs on different adevs in XGMI hive or jobs on
3818 * different schedulers for same device while this TO handler is running.
3819 * We always reset all schedulers for device and all devices for XGMI
3820 * hive so that should take care of them too.
3823 if (hive && !mutex_trylock(&hive->reset_lock)) {
3824 DRM_INFO("Bailing on TDR for s_job:%llx, hive: %llx as another already in progress",
3825 job ? job->base.id : -1, hive->hive_id);
3829 /* Start with adev pre asic reset first for soft reset check.*/
3830 if (!amdgpu_device_lock_adev(adev, !hive)) {
3831 DRM_INFO("Bailing on TDR for s_job:%llx, as another already in progress",
3832 job ? job->base.id : -1);
3836 /* Block kfd: SRIOV would do it separately */
3837 if (!amdgpu_sriov_vf(adev))
3838 amdgpu_amdkfd_pre_reset(adev);
3840 /* Build list of devices to reset */
3841 if (adev->gmc.xgmi.num_physical_nodes > 1) {
3843 /*unlock kfd: SRIOV would do it separately */
3844 if (!amdgpu_sriov_vf(adev))
3845 amdgpu_amdkfd_post_reset(adev);
3846 amdgpu_device_unlock_adev(adev);
3851 * In case we are in XGMI hive mode device reset is done for all the
3852 * nodes in the hive to retrain all XGMI links and hence the reset
3853 * sequence is executed in loop on all nodes.
3855 device_list_handle = &hive->device_list;
3857 list_add_tail(&adev->gmc.xgmi.head, &device_list);
3858 device_list_handle = &device_list;
3861 /* block all schedulers and reset given job's ring */
3862 list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
3863 if (tmp_adev != adev) {
3864 amdgpu_device_lock_adev(tmp_adev, false);
3865 if (!amdgpu_sriov_vf(tmp_adev))
3866 amdgpu_amdkfd_pre_reset(tmp_adev);
3870 * Mark these ASICs to be reseted as untracked first
3871 * And add them back after reset completed
3873 amdgpu_unregister_gpu_instance(tmp_adev);
3875 /* disable ras on ALL IPs */
3876 if (!in_ras_intr && amdgpu_device_ip_need_full_reset(tmp_adev))
3877 amdgpu_ras_suspend(tmp_adev);
3879 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3880 struct amdgpu_ring *ring = tmp_adev->rings[i];
3882 if (!ring || !ring->sched.thread)
3885 drm_sched_stop(&ring->sched, job ? &job->base : NULL);
3888 amdgpu_job_stop_all_jobs_on_sched(&ring->sched);
3894 goto skip_sched_resume;
3897 * Must check guilty signal here since after this point all old
3898 * HW fences are force signaled.
3900 * job->base holds a reference to parent fence
3902 if (job && job->base.s_fence->parent &&
3903 dma_fence_is_signaled(job->base.s_fence->parent))
3904 job_signaled = true;
3907 dev_info(adev->dev, "Guilty job already signaled, skipping HW reset");
3912 /* Guilty job will be freed after this*/
3913 r = amdgpu_device_pre_asic_reset(adev, job, &need_full_reset);
3915 /*TODO Should we stop ?*/
3916 DRM_ERROR("GPU pre asic reset failed with err, %d for drm dev, %s ",
3917 r, adev->ddev->unique);
3918 adev->asic_reset_res = r;
3921 retry: /* Rest of adevs pre asic reset from XGMI hive. */
3922 list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
3924 if (tmp_adev == adev)
3927 r = amdgpu_device_pre_asic_reset(tmp_adev,
3930 /*TODO Should we stop ?*/
3932 DRM_ERROR("GPU pre asic reset failed with err, %d for drm dev, %s ",
3933 r, tmp_adev->ddev->unique);
3934 tmp_adev->asic_reset_res = r;
3938 /* Actual ASIC resets if needed.*/
3939 /* TODO Implement XGMI hive reset logic for SRIOV */
3940 if (amdgpu_sriov_vf(adev)) {
3941 r = amdgpu_device_reset_sriov(adev, job ? false : true);
3943 adev->asic_reset_res = r;
3945 r = amdgpu_do_asic_reset(hive, device_list_handle, &need_full_reset);
3946 if (r && r == -EAGAIN)
3952 /* Post ASIC reset for all devs .*/
3953 list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
3955 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3956 struct amdgpu_ring *ring = tmp_adev->rings[i];
3958 if (!ring || !ring->sched.thread)
3961 /* No point to resubmit jobs if we didn't HW reset*/
3962 if (!tmp_adev->asic_reset_res && !job_signaled)
3963 drm_sched_resubmit_jobs(&ring->sched);
3965 drm_sched_start(&ring->sched, !tmp_adev->asic_reset_res);
3968 if (!amdgpu_device_has_dc_support(tmp_adev) && !job_signaled) {
3969 drm_helper_resume_force_mode(tmp_adev->ddev);
3972 tmp_adev->asic_reset_res = 0;
3975 /* bad news, how to tell it to userspace ? */
3976 dev_info(tmp_adev->dev, "GPU reset(%d) failed\n", atomic_read(&tmp_adev->gpu_reset_counter));
3977 amdgpu_vf_error_put(tmp_adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
3979 dev_info(tmp_adev->dev, "GPU reset(%d) succeeded!\n", atomic_read(&tmp_adev->gpu_reset_counter));
3984 list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
3985 /*unlock kfd: SRIOV would do it separately */
3986 if (!in_ras_intr && !amdgpu_sriov_vf(tmp_adev))
3987 amdgpu_amdkfd_post_reset(tmp_adev);
3988 amdgpu_device_unlock_adev(tmp_adev);
3992 mutex_unlock(&hive->reset_lock);
3995 dev_info(adev->dev, "GPU reset end with ret = %d\n", r);
4000 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
4002 * @adev: amdgpu_device pointer
4004 * Fetchs and stores in the driver the PCIE capabilities (gen speed
4005 * and lanes) of the slot the device is in. Handles APUs and
4006 * virtualized environments where PCIE config space may not be available.
4008 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
4010 struct pci_dev *pdev;
4011 enum pci_bus_speed speed_cap, platform_speed_cap;
4012 enum pcie_link_width platform_link_width;
4014 if (amdgpu_pcie_gen_cap)
4015 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
4017 if (amdgpu_pcie_lane_cap)
4018 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
4020 /* covers APUs as well */
4021 if (pci_is_root_bus(adev->pdev->bus)) {
4022 if (adev->pm.pcie_gen_mask == 0)
4023 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
4024 if (adev->pm.pcie_mlw_mask == 0)
4025 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
4029 if (adev->pm.pcie_gen_mask && adev->pm.pcie_mlw_mask)
4032 pcie_bandwidth_available(adev->pdev, NULL,
4033 &platform_speed_cap, &platform_link_width);
4035 if (adev->pm.pcie_gen_mask == 0) {
4038 speed_cap = pcie_get_speed_cap(pdev);
4039 if (speed_cap == PCI_SPEED_UNKNOWN) {
4040 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4041 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
4042 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
4044 if (speed_cap == PCIE_SPEED_16_0GT)
4045 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4046 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
4047 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
4048 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4);
4049 else if (speed_cap == PCIE_SPEED_8_0GT)
4050 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4051 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
4052 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
4053 else if (speed_cap == PCIE_SPEED_5_0GT)
4054 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4055 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2);
4057 adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1;
4060 if (platform_speed_cap == PCI_SPEED_UNKNOWN) {
4061 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4062 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
4064 if (platform_speed_cap == PCIE_SPEED_16_0GT)
4065 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4066 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
4067 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
4068 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4);
4069 else if (platform_speed_cap == PCIE_SPEED_8_0GT)
4070 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4071 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
4072 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3);
4073 else if (platform_speed_cap == PCIE_SPEED_5_0GT)
4074 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4075 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
4077 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
4081 if (adev->pm.pcie_mlw_mask == 0) {
4082 if (platform_link_width == PCIE_LNK_WIDTH_UNKNOWN) {
4083 adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK;
4085 switch (platform_link_width) {
4087 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
4088 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
4089 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
4090 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
4091 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
4092 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
4093 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
4096 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
4097 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
4098 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
4099 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
4100 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
4101 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
4104 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
4105 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
4106 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
4107 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
4108 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
4111 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
4112 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
4113 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
4114 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
4117 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
4118 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
4119 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
4122 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
4123 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
4126 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;