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 ret = amdgpu_device_get_job_timeout_settings(adev);
1030 dev_err(adev->dev, "invalid lockup_timeout parameter syntax\n");
1034 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
1040 * amdgpu_switcheroo_set_state - set switcheroo state
1042 * @pdev: pci dev pointer
1043 * @state: vga_switcheroo state
1045 * Callback for the switcheroo driver. Suspends or resumes the
1046 * the asics before or after it is powered up using ACPI methods.
1048 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1050 struct drm_device *dev = pci_get_drvdata(pdev);
1052 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1055 if (state == VGA_SWITCHEROO_ON) {
1056 pr_info("amdgpu: switched on\n");
1057 /* don't suspend or resume card normally */
1058 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1060 amdgpu_device_resume(dev, true, true);
1062 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1063 drm_kms_helper_poll_enable(dev);
1065 pr_info("amdgpu: switched off\n");
1066 drm_kms_helper_poll_disable(dev);
1067 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1068 amdgpu_device_suspend(dev, true, true);
1069 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1074 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1076 * @pdev: pci dev pointer
1078 * Callback for the switcheroo driver. Check of the switcheroo
1079 * state can be changed.
1080 * Returns true if the state can be changed, false if not.
1082 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1084 struct drm_device *dev = pci_get_drvdata(pdev);
1087 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1088 * locking inversion with the driver load path. And the access here is
1089 * completely racy anyway. So don't bother with locking for now.
1091 return dev->open_count == 0;
1094 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1095 .set_gpu_state = amdgpu_switcheroo_set_state,
1097 .can_switch = amdgpu_switcheroo_can_switch,
1101 * amdgpu_device_ip_set_clockgating_state - set the CG state
1103 * @dev: amdgpu_device pointer
1104 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1105 * @state: clockgating state (gate or ungate)
1107 * Sets the requested clockgating state for all instances of
1108 * the hardware IP specified.
1109 * Returns the error code from the last instance.
1111 int amdgpu_device_ip_set_clockgating_state(void *dev,
1112 enum amd_ip_block_type block_type,
1113 enum amd_clockgating_state state)
1115 struct amdgpu_device *adev = dev;
1118 for (i = 0; i < adev->num_ip_blocks; i++) {
1119 if (!adev->ip_blocks[i].status.valid)
1121 if (adev->ip_blocks[i].version->type != block_type)
1123 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1125 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1126 (void *)adev, state);
1128 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1129 adev->ip_blocks[i].version->funcs->name, r);
1135 * amdgpu_device_ip_set_powergating_state - set the PG state
1137 * @dev: amdgpu_device pointer
1138 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1139 * @state: powergating state (gate or ungate)
1141 * Sets the requested powergating state for all instances of
1142 * the hardware IP specified.
1143 * Returns the error code from the last instance.
1145 int amdgpu_device_ip_set_powergating_state(void *dev,
1146 enum amd_ip_block_type block_type,
1147 enum amd_powergating_state state)
1149 struct amdgpu_device *adev = dev;
1152 for (i = 0; i < adev->num_ip_blocks; i++) {
1153 if (!adev->ip_blocks[i].status.valid)
1155 if (adev->ip_blocks[i].version->type != block_type)
1157 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1159 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1160 (void *)adev, state);
1162 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1163 adev->ip_blocks[i].version->funcs->name, r);
1169 * amdgpu_device_ip_get_clockgating_state - get the CG state
1171 * @adev: amdgpu_device pointer
1172 * @flags: clockgating feature flags
1174 * Walks the list of IPs on the device and updates the clockgating
1175 * flags for each IP.
1176 * Updates @flags with the feature flags for each hardware IP where
1177 * clockgating is enabled.
1179 void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
1184 for (i = 0; i < adev->num_ip_blocks; i++) {
1185 if (!adev->ip_blocks[i].status.valid)
1187 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1188 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1193 * amdgpu_device_ip_wait_for_idle - wait for idle
1195 * @adev: amdgpu_device pointer
1196 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1198 * Waits for the request hardware IP to be idle.
1199 * Returns 0 for success or a negative error code on failure.
1201 int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1202 enum amd_ip_block_type block_type)
1206 for (i = 0; i < adev->num_ip_blocks; i++) {
1207 if (!adev->ip_blocks[i].status.valid)
1209 if (adev->ip_blocks[i].version->type == block_type) {
1210 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
1221 * amdgpu_device_ip_is_idle - is the hardware IP idle
1223 * @adev: amdgpu_device pointer
1224 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1226 * Check if the hardware IP is idle or not.
1227 * Returns true if it the IP is idle, false if not.
1229 bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1230 enum amd_ip_block_type block_type)
1234 for (i = 0; i < adev->num_ip_blocks; i++) {
1235 if (!adev->ip_blocks[i].status.valid)
1237 if (adev->ip_blocks[i].version->type == block_type)
1238 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
1245 * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1247 * @adev: amdgpu_device pointer
1248 * @type: Type of hardware IP (SMU, GFX, UVD, etc.)
1250 * Returns a pointer to the hardware IP block structure
1251 * if it exists for the asic, otherwise NULL.
1253 struct amdgpu_ip_block *
1254 amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1255 enum amd_ip_block_type type)
1259 for (i = 0; i < adev->num_ip_blocks; i++)
1260 if (adev->ip_blocks[i].version->type == type)
1261 return &adev->ip_blocks[i];
1267 * amdgpu_device_ip_block_version_cmp
1269 * @adev: amdgpu_device pointer
1270 * @type: enum amd_ip_block_type
1271 * @major: major version
1272 * @minor: minor version
1274 * return 0 if equal or greater
1275 * return 1 if smaller or the ip_block doesn't exist
1277 int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1278 enum amd_ip_block_type type,
1279 u32 major, u32 minor)
1281 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
1283 if (ip_block && ((ip_block->version->major > major) ||
1284 ((ip_block->version->major == major) &&
1285 (ip_block->version->minor >= minor))))
1292 * amdgpu_device_ip_block_add
1294 * @adev: amdgpu_device pointer
1295 * @ip_block_version: pointer to the IP to add
1297 * Adds the IP block driver information to the collection of IPs
1300 int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1301 const struct amdgpu_ip_block_version *ip_block_version)
1303 if (!ip_block_version)
1306 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
1307 ip_block_version->funcs->name);
1309 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1315 * amdgpu_device_enable_virtual_display - enable virtual display feature
1317 * @adev: amdgpu_device pointer
1319 * Enabled the virtual display feature if the user has enabled it via
1320 * the module parameter virtual_display. This feature provides a virtual
1321 * display hardware on headless boards or in virtualized environments.
1322 * This function parses and validates the configuration string specified by
1323 * the user and configues the virtual display configuration (number of
1324 * virtual connectors, crtcs, etc.) specified.
1326 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
1328 adev->enable_virtual_display = false;
1330 if (amdgpu_virtual_display) {
1331 struct drm_device *ddev = adev->ddev;
1332 const char *pci_address_name = pci_name(ddev->pdev);
1333 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
1335 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1336 pciaddstr_tmp = pciaddstr;
1337 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1338 pciaddname = strsep(&pciaddname_tmp, ",");
1339 if (!strcmp("all", pciaddname)
1340 || !strcmp(pci_address_name, pciaddname)) {
1344 adev->enable_virtual_display = true;
1347 res = kstrtol(pciaddname_tmp, 10,
1355 adev->mode_info.num_crtc = num_crtc;
1357 adev->mode_info.num_crtc = 1;
1363 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1364 amdgpu_virtual_display, pci_address_name,
1365 adev->enable_virtual_display, adev->mode_info.num_crtc);
1372 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
1374 * @adev: amdgpu_device pointer
1376 * Parses the asic configuration parameters specified in the gpu info
1377 * firmware and makes them availale to the driver for use in configuring
1379 * Returns 0 on success, -EINVAL on failure.
1381 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1383 const char *chip_name;
1386 const struct gpu_info_firmware_header_v1_0 *hdr;
1388 adev->firmware.gpu_info_fw = NULL;
1390 switch (adev->asic_type) {
1394 case CHIP_POLARIS10:
1395 case CHIP_POLARIS11:
1396 case CHIP_POLARIS12:
1400 #ifdef CONFIG_DRM_AMDGPU_SI
1407 #ifdef CONFIG_DRM_AMDGPU_CIK
1418 chip_name = "vega10";
1421 chip_name = "vega12";
1424 if (adev->rev_id >= 8)
1425 chip_name = "raven2";
1426 else if (adev->pdev->device == 0x15d8)
1427 chip_name = "picasso";
1429 chip_name = "raven";
1432 chip_name = "arcturus";
1435 chip_name = "renoir";
1438 chip_name = "navi10";
1441 chip_name = "navi14";
1444 chip_name = "navi12";
1448 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
1449 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
1452 "Failed to load gpu_info firmware \"%s\"\n",
1456 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
1459 "Failed to validate gpu_info firmware \"%s\"\n",
1464 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
1465 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1467 switch (hdr->version_major) {
1470 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
1471 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
1472 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1474 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1475 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1476 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1477 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
1478 adev->gfx.config.max_texture_channel_caches =
1479 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1480 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1481 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1482 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1483 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
1484 adev->gfx.config.double_offchip_lds_buf =
1485 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1486 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
1487 adev->gfx.cu_info.max_waves_per_simd =
1488 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1489 adev->gfx.cu_info.max_scratch_slots_per_cu =
1490 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1491 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
1492 if (hdr->version_minor >= 1) {
1493 const struct gpu_info_firmware_v1_1 *gpu_info_fw =
1494 (const struct gpu_info_firmware_v1_1 *)(adev->firmware.gpu_info_fw->data +
1495 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1496 adev->gfx.config.num_sc_per_sh =
1497 le32_to_cpu(gpu_info_fw->num_sc_per_sh);
1498 adev->gfx.config.num_packer_per_sc =
1499 le32_to_cpu(gpu_info_fw->num_packer_per_sc);
1501 #ifdef CONFIG_DRM_AMD_DC_DCN2_0
1502 if (hdr->version_minor == 2) {
1503 const struct gpu_info_firmware_v1_2 *gpu_info_fw =
1504 (const struct gpu_info_firmware_v1_2 *)(adev->firmware.gpu_info_fw->data +
1505 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1506 adev->dm.soc_bounding_box = &gpu_info_fw->soc_bounding_box;
1513 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1522 * amdgpu_device_ip_early_init - run early init for hardware IPs
1524 * @adev: amdgpu_device pointer
1526 * Early initialization pass for hardware IPs. The hardware IPs that make
1527 * up each asic are discovered each IP's early_init callback is run. This
1528 * is the first stage in initializing the asic.
1529 * Returns 0 on success, negative error code on failure.
1531 static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
1535 amdgpu_device_enable_virtual_display(adev);
1537 switch (adev->asic_type) {
1541 case CHIP_POLARIS10:
1542 case CHIP_POLARIS11:
1543 case CHIP_POLARIS12:
1547 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
1548 adev->family = AMDGPU_FAMILY_CZ;
1550 adev->family = AMDGPU_FAMILY_VI;
1552 r = vi_set_ip_blocks(adev);
1556 #ifdef CONFIG_DRM_AMDGPU_SI
1562 adev->family = AMDGPU_FAMILY_SI;
1563 r = si_set_ip_blocks(adev);
1568 #ifdef CONFIG_DRM_AMDGPU_CIK
1574 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1575 adev->family = AMDGPU_FAMILY_CI;
1577 adev->family = AMDGPU_FAMILY_KV;
1579 r = cik_set_ip_blocks(adev);
1590 if (adev->asic_type == CHIP_RAVEN ||
1591 adev->asic_type == CHIP_RENOIR)
1592 adev->family = AMDGPU_FAMILY_RV;
1594 adev->family = AMDGPU_FAMILY_AI;
1596 r = soc15_set_ip_blocks(adev);
1603 adev->family = AMDGPU_FAMILY_NV;
1605 r = nv_set_ip_blocks(adev);
1610 /* FIXME: not supported yet */
1614 r = amdgpu_device_parse_gpu_info_fw(adev);
1618 amdgpu_amdkfd_device_probe(adev);
1620 if (amdgpu_sriov_vf(adev)) {
1621 r = amdgpu_virt_request_full_gpu(adev, true);
1626 adev->pm.pp_feature = amdgpu_pp_feature_mask;
1627 if (amdgpu_sriov_vf(adev)
1628 #ifdef CONFIG_HSA_AMD
1629 || sched_policy == KFD_SCHED_POLICY_NO_HWS
1632 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
1634 for (i = 0; i < adev->num_ip_blocks; i++) {
1635 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1636 DRM_ERROR("disabled ip block: %d <%s>\n",
1637 i, adev->ip_blocks[i].version->funcs->name);
1638 adev->ip_blocks[i].status.valid = false;
1640 if (adev->ip_blocks[i].version->funcs->early_init) {
1641 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
1643 adev->ip_blocks[i].status.valid = false;
1645 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1646 adev->ip_blocks[i].version->funcs->name, r);
1649 adev->ip_blocks[i].status.valid = true;
1652 adev->ip_blocks[i].status.valid = true;
1655 /* get the vbios after the asic_funcs are set up */
1656 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) {
1658 if (!amdgpu_get_bios(adev))
1661 r = amdgpu_atombios_init(adev);
1663 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
1664 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
1670 adev->cg_flags &= amdgpu_cg_mask;
1671 adev->pg_flags &= amdgpu_pg_mask;
1676 static int amdgpu_device_ip_hw_init_phase1(struct amdgpu_device *adev)
1680 for (i = 0; i < adev->num_ip_blocks; i++) {
1681 if (!adev->ip_blocks[i].status.sw)
1683 if (adev->ip_blocks[i].status.hw)
1685 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1686 (amdgpu_sriov_vf(adev) && (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)) ||
1687 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
1688 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
1690 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1691 adev->ip_blocks[i].version->funcs->name, r);
1694 adev->ip_blocks[i].status.hw = true;
1701 static int amdgpu_device_ip_hw_init_phase2(struct amdgpu_device *adev)
1705 for (i = 0; i < adev->num_ip_blocks; i++) {
1706 if (!adev->ip_blocks[i].status.sw)
1708 if (adev->ip_blocks[i].status.hw)
1710 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
1712 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1713 adev->ip_blocks[i].version->funcs->name, r);
1716 adev->ip_blocks[i].status.hw = true;
1722 static int amdgpu_device_fw_loading(struct amdgpu_device *adev)
1726 uint32_t smu_version;
1728 if (adev->asic_type >= CHIP_VEGA10) {
1729 for (i = 0; i < adev->num_ip_blocks; i++) {
1730 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_PSP)
1733 /* no need to do the fw loading again if already done*/
1734 if (adev->ip_blocks[i].status.hw == true)
1737 if (adev->in_gpu_reset || adev->in_suspend) {
1738 r = adev->ip_blocks[i].version->funcs->resume(adev);
1740 DRM_ERROR("resume of IP block <%s> failed %d\n",
1741 adev->ip_blocks[i].version->funcs->name, r);
1745 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
1747 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1748 adev->ip_blocks[i].version->funcs->name, r);
1753 adev->ip_blocks[i].status.hw = true;
1758 r = amdgpu_pm_load_smu_firmware(adev, &smu_version);
1764 * amdgpu_device_ip_init - run init for hardware IPs
1766 * @adev: amdgpu_device pointer
1768 * Main initialization pass for hardware IPs. The list of all the hardware
1769 * IPs that make up the asic is walked and the sw_init and hw_init callbacks
1770 * are run. sw_init initializes the software state associated with each IP
1771 * and hw_init initializes the hardware associated with each IP.
1772 * Returns 0 on success, negative error code on failure.
1774 static int amdgpu_device_ip_init(struct amdgpu_device *adev)
1778 r = amdgpu_ras_init(adev);
1782 for (i = 0; i < adev->num_ip_blocks; i++) {
1783 if (!adev->ip_blocks[i].status.valid)
1785 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
1787 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1788 adev->ip_blocks[i].version->funcs->name, r);
1791 adev->ip_blocks[i].status.sw = true;
1793 /* need to do gmc hw init early so we can allocate gpu mem */
1794 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1795 r = amdgpu_device_vram_scratch_init(adev);
1797 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
1800 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1802 DRM_ERROR("hw_init %d failed %d\n", i, r);
1805 r = amdgpu_device_wb_init(adev);
1807 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
1810 adev->ip_blocks[i].status.hw = true;
1812 /* right after GMC hw init, we create CSA */
1813 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) {
1814 r = amdgpu_allocate_static_csa(adev, &adev->virt.csa_obj,
1815 AMDGPU_GEM_DOMAIN_VRAM,
1818 DRM_ERROR("allocate CSA failed %d\n", r);
1825 r = amdgpu_ib_pool_init(adev);
1827 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
1828 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
1832 r = amdgpu_ucode_create_bo(adev); /* create ucode bo when sw_init complete*/
1836 r = amdgpu_device_ip_hw_init_phase1(adev);
1840 r = amdgpu_device_fw_loading(adev);
1844 r = amdgpu_device_ip_hw_init_phase2(adev);
1848 if (adev->gmc.xgmi.num_physical_nodes > 1)
1849 amdgpu_xgmi_add_device(adev);
1850 amdgpu_amdkfd_device_init(adev);
1853 if (amdgpu_sriov_vf(adev)) {
1855 amdgpu_virt_init_data_exchange(adev);
1856 amdgpu_virt_release_full_gpu(adev, true);
1863 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
1865 * @adev: amdgpu_device pointer
1867 * Writes a reset magic value to the gart pointer in VRAM. The driver calls
1868 * this function before a GPU reset. If the value is retained after a
1869 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
1871 static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
1873 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1877 * amdgpu_device_check_vram_lost - check if vram is valid
1879 * @adev: amdgpu_device pointer
1881 * Checks the reset magic value written to the gart pointer in VRAM.
1882 * The driver calls this after a GPU reset to see if the contents of
1883 * VRAM is lost or now.
1884 * returns true if vram is lost, false if not.
1886 static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
1888 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1889 AMDGPU_RESET_MAGIC_NUM);
1893 * amdgpu_device_set_cg_state - set clockgating for amdgpu device
1895 * @adev: amdgpu_device pointer
1897 * The list of all the hardware IPs that make up the asic is walked and the
1898 * set_clockgating_state callbacks are run.
1899 * Late initialization pass enabling clockgating for hardware IPs.
1900 * Fini or suspend, pass disabling clockgating for hardware IPs.
1901 * Returns 0 on success, negative error code on failure.
1904 static int amdgpu_device_set_cg_state(struct amdgpu_device *adev,
1905 enum amd_clockgating_state state)
1909 if (amdgpu_emu_mode == 1)
1912 for (j = 0; j < adev->num_ip_blocks; j++) {
1913 i = state == AMD_CG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
1914 if (!adev->ip_blocks[i].status.late_initialized)
1916 /* skip CG for VCE/UVD, it's handled specially */
1917 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1918 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1919 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
1920 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
1921 /* enable clockgating to save power */
1922 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1925 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1926 adev->ip_blocks[i].version->funcs->name, r);
1935 static int amdgpu_device_set_pg_state(struct amdgpu_device *adev, enum amd_powergating_state state)
1939 if (amdgpu_emu_mode == 1)
1942 for (j = 0; j < adev->num_ip_blocks; j++) {
1943 i = state == AMD_PG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
1944 if (!adev->ip_blocks[i].status.late_initialized)
1946 /* skip CG for VCE/UVD, it's handled specially */
1947 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1948 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1949 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
1950 adev->ip_blocks[i].version->funcs->set_powergating_state) {
1951 /* enable powergating to save power */
1952 r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
1955 DRM_ERROR("set_powergating_state(gate) of IP block <%s> failed %d\n",
1956 adev->ip_blocks[i].version->funcs->name, r);
1964 static int amdgpu_device_enable_mgpu_fan_boost(void)
1966 struct amdgpu_gpu_instance *gpu_ins;
1967 struct amdgpu_device *adev;
1970 mutex_lock(&mgpu_info.mutex);
1973 * MGPU fan boost feature should be enabled
1974 * only when there are two or more dGPUs in
1977 if (mgpu_info.num_dgpu < 2)
1980 for (i = 0; i < mgpu_info.num_dgpu; i++) {
1981 gpu_ins = &(mgpu_info.gpu_ins[i]);
1982 adev = gpu_ins->adev;
1983 if (!(adev->flags & AMD_IS_APU) &&
1984 !gpu_ins->mgpu_fan_enabled &&
1985 adev->powerplay.pp_funcs &&
1986 adev->powerplay.pp_funcs->enable_mgpu_fan_boost) {
1987 ret = amdgpu_dpm_enable_mgpu_fan_boost(adev);
1991 gpu_ins->mgpu_fan_enabled = 1;
1996 mutex_unlock(&mgpu_info.mutex);
2002 * amdgpu_device_ip_late_init - run late init for hardware IPs
2004 * @adev: amdgpu_device pointer
2006 * Late initialization pass for hardware IPs. The list of all the hardware
2007 * IPs that make up the asic is walked and the late_init callbacks are run.
2008 * late_init covers any special initialization that an IP requires
2009 * after all of the have been initialized or something that needs to happen
2010 * late in the init process.
2011 * Returns 0 on success, negative error code on failure.
2013 static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
2017 for (i = 0; i < adev->num_ip_blocks; i++) {
2018 if (!adev->ip_blocks[i].status.hw)
2020 if (adev->ip_blocks[i].version->funcs->late_init) {
2021 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
2023 DRM_ERROR("late_init of IP block <%s> failed %d\n",
2024 adev->ip_blocks[i].version->funcs->name, r);
2028 adev->ip_blocks[i].status.late_initialized = true;
2031 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE);
2032 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_GATE);
2034 amdgpu_device_fill_reset_magic(adev);
2036 r = amdgpu_device_enable_mgpu_fan_boost();
2038 DRM_ERROR("enable mgpu fan boost failed (%d).\n", r);
2040 /* set to low pstate by default */
2041 amdgpu_xgmi_set_pstate(adev, 0);
2047 * amdgpu_device_ip_fini - run fini for hardware IPs
2049 * @adev: amdgpu_device pointer
2051 * Main teardown pass for hardware IPs. The list of all the hardware
2052 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
2053 * are run. hw_fini tears down the hardware associated with each IP
2054 * and sw_fini tears down any software state associated with each IP.
2055 * Returns 0 on success, negative error code on failure.
2057 static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
2061 amdgpu_ras_pre_fini(adev);
2063 if (adev->gmc.xgmi.num_physical_nodes > 1)
2064 amdgpu_xgmi_remove_device(adev);
2066 amdgpu_amdkfd_device_fini(adev);
2068 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2069 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2071 /* need to disable SMC first */
2072 for (i = 0; i < adev->num_ip_blocks; i++) {
2073 if (!adev->ip_blocks[i].status.hw)
2075 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
2076 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2077 /* XXX handle errors */
2079 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2080 adev->ip_blocks[i].version->funcs->name, r);
2082 adev->ip_blocks[i].status.hw = false;
2087 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2088 if (!adev->ip_blocks[i].status.hw)
2091 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2092 /* XXX handle errors */
2094 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2095 adev->ip_blocks[i].version->funcs->name, r);
2098 adev->ip_blocks[i].status.hw = false;
2102 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2103 if (!adev->ip_blocks[i].status.sw)
2106 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
2107 amdgpu_ucode_free_bo(adev);
2108 amdgpu_free_static_csa(&adev->virt.csa_obj);
2109 amdgpu_device_wb_fini(adev);
2110 amdgpu_device_vram_scratch_fini(adev);
2111 amdgpu_ib_pool_fini(adev);
2114 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
2115 /* XXX handle errors */
2117 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
2118 adev->ip_blocks[i].version->funcs->name, r);
2120 adev->ip_blocks[i].status.sw = false;
2121 adev->ip_blocks[i].status.valid = false;
2124 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2125 if (!adev->ip_blocks[i].status.late_initialized)
2127 if (adev->ip_blocks[i].version->funcs->late_fini)
2128 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
2129 adev->ip_blocks[i].status.late_initialized = false;
2132 amdgpu_ras_fini(adev);
2134 if (amdgpu_sriov_vf(adev))
2135 if (amdgpu_virt_release_full_gpu(adev, false))
2136 DRM_ERROR("failed to release exclusive mode on fini\n");
2142 * amdgpu_device_delayed_init_work_handler - work handler for IB tests
2144 * @work: work_struct.
2146 static void amdgpu_device_delayed_init_work_handler(struct work_struct *work)
2148 struct amdgpu_device *adev =
2149 container_of(work, struct amdgpu_device, delayed_init_work.work);
2152 r = amdgpu_ib_ring_tests(adev);
2154 DRM_ERROR("ib ring test failed (%d).\n", r);
2157 static void amdgpu_device_delay_enable_gfx_off(struct work_struct *work)
2159 struct amdgpu_device *adev =
2160 container_of(work, struct amdgpu_device, gfx.gfx_off_delay_work.work);
2162 mutex_lock(&adev->gfx.gfx_off_mutex);
2163 if (!adev->gfx.gfx_off_state && !adev->gfx.gfx_off_req_count) {
2164 if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true))
2165 adev->gfx.gfx_off_state = true;
2167 mutex_unlock(&adev->gfx.gfx_off_mutex);
2171 * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1)
2173 * @adev: amdgpu_device pointer
2175 * Main suspend function for hardware IPs. The list of all the hardware
2176 * IPs that make up the asic is walked, clockgating is disabled and the
2177 * suspend callbacks are run. suspend puts the hardware and software state
2178 * in each IP into a state suitable for suspend.
2179 * Returns 0 on success, negative error code on failure.
2181 static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev)
2185 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2186 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2188 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2189 if (!adev->ip_blocks[i].status.valid)
2191 /* displays are handled separately */
2192 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) {
2193 /* XXX handle errors */
2194 r = adev->ip_blocks[i].version->funcs->suspend(adev);
2195 /* XXX handle errors */
2197 DRM_ERROR("suspend of IP block <%s> failed %d\n",
2198 adev->ip_blocks[i].version->funcs->name, r);
2201 adev->ip_blocks[i].status.hw = false;
2209 * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2)
2211 * @adev: amdgpu_device pointer
2213 * Main suspend function for hardware IPs. The list of all the hardware
2214 * IPs that make up the asic is walked, clockgating is disabled and the
2215 * suspend callbacks are run. suspend puts the hardware and software state
2216 * in each IP into a state suitable for suspend.
2217 * Returns 0 on success, negative error code on failure.
2219 static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev)
2223 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2224 if (!adev->ip_blocks[i].status.valid)
2226 /* displays are handled in phase1 */
2227 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)
2229 /* XXX handle errors */
2230 r = adev->ip_blocks[i].version->funcs->suspend(adev);
2231 /* XXX handle errors */
2233 DRM_ERROR("suspend of IP block <%s> failed %d\n",
2234 adev->ip_blocks[i].version->funcs->name, r);
2236 adev->ip_blocks[i].status.hw = false;
2237 /* handle putting the SMC in the appropriate state */
2238 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
2239 if (is_support_sw_smu(adev)) {
2241 } else if (adev->powerplay.pp_funcs &&
2242 adev->powerplay.pp_funcs->set_mp1_state) {
2243 r = adev->powerplay.pp_funcs->set_mp1_state(
2244 adev->powerplay.pp_handle,
2247 DRM_ERROR("SMC failed to set mp1 state %d, %d\n",
2248 adev->mp1_state, r);
2254 adev->ip_blocks[i].status.hw = false;
2261 * amdgpu_device_ip_suspend - run suspend for hardware IPs
2263 * @adev: amdgpu_device pointer
2265 * Main suspend function for hardware IPs. The list of all the hardware
2266 * IPs that make up the asic is walked, clockgating is disabled and the
2267 * suspend callbacks are run. suspend puts the hardware and software state
2268 * in each IP into a state suitable for suspend.
2269 * Returns 0 on success, negative error code on failure.
2271 int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
2275 if (amdgpu_sriov_vf(adev))
2276 amdgpu_virt_request_full_gpu(adev, false);
2278 r = amdgpu_device_ip_suspend_phase1(adev);
2281 r = amdgpu_device_ip_suspend_phase2(adev);
2283 if (amdgpu_sriov_vf(adev))
2284 amdgpu_virt_release_full_gpu(adev, false);
2289 static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
2293 static enum amd_ip_block_type ip_order[] = {
2294 AMD_IP_BLOCK_TYPE_GMC,
2295 AMD_IP_BLOCK_TYPE_COMMON,
2296 AMD_IP_BLOCK_TYPE_PSP,
2297 AMD_IP_BLOCK_TYPE_IH,
2300 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
2302 struct amdgpu_ip_block *block;
2304 for (j = 0; j < adev->num_ip_blocks; j++) {
2305 block = &adev->ip_blocks[j];
2307 block->status.hw = false;
2308 if (block->version->type != ip_order[i] ||
2309 !block->status.valid)
2312 r = block->version->funcs->hw_init(adev);
2313 DRM_INFO("RE-INIT-early: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
2316 block->status.hw = true;
2323 static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
2327 static enum amd_ip_block_type ip_order[] = {
2328 AMD_IP_BLOCK_TYPE_SMC,
2329 AMD_IP_BLOCK_TYPE_DCE,
2330 AMD_IP_BLOCK_TYPE_GFX,
2331 AMD_IP_BLOCK_TYPE_SDMA,
2332 AMD_IP_BLOCK_TYPE_UVD,
2333 AMD_IP_BLOCK_TYPE_VCE
2336 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
2338 struct amdgpu_ip_block *block;
2340 for (j = 0; j < adev->num_ip_blocks; j++) {
2341 block = &adev->ip_blocks[j];
2343 if (block->version->type != ip_order[i] ||
2344 !block->status.valid ||
2348 r = block->version->funcs->hw_init(adev);
2349 DRM_INFO("RE-INIT-late: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
2352 block->status.hw = true;
2360 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
2362 * @adev: amdgpu_device pointer
2364 * First resume function for hardware IPs. The list of all the hardware
2365 * IPs that make up the asic is walked and the resume callbacks are run for
2366 * COMMON, GMC, and IH. resume puts the hardware into a functional state
2367 * after a suspend and updates the software state as necessary. This
2368 * function is also used for restoring the GPU after a GPU reset.
2369 * Returns 0 on success, negative error code on failure.
2371 static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
2375 for (i = 0; i < adev->num_ip_blocks; i++) {
2376 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
2378 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2379 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2380 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
2382 r = adev->ip_blocks[i].version->funcs->resume(adev);
2384 DRM_ERROR("resume of IP block <%s> failed %d\n",
2385 adev->ip_blocks[i].version->funcs->name, r);
2388 adev->ip_blocks[i].status.hw = true;
2396 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
2398 * @adev: amdgpu_device pointer
2400 * First resume function for hardware IPs. The list of all the hardware
2401 * IPs that make up the asic is walked and the resume callbacks are run for
2402 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
2403 * functional state after a suspend and updates the software state as
2404 * necessary. This function is also used for restoring the GPU after a GPU
2406 * Returns 0 on success, negative error code on failure.
2408 static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
2412 for (i = 0; i < adev->num_ip_blocks; i++) {
2413 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
2415 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2416 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2417 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
2418 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)
2420 r = adev->ip_blocks[i].version->funcs->resume(adev);
2422 DRM_ERROR("resume of IP block <%s> failed %d\n",
2423 adev->ip_blocks[i].version->funcs->name, r);
2426 adev->ip_blocks[i].status.hw = true;
2433 * amdgpu_device_ip_resume - run resume for hardware IPs
2435 * @adev: amdgpu_device pointer
2437 * Main resume function for hardware IPs. The hardware IPs
2438 * are split into two resume functions because they are
2439 * are also used in in recovering from a GPU reset and some additional
2440 * steps need to be take between them. In this case (S3/S4) they are
2442 * Returns 0 on success, negative error code on failure.
2444 static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
2448 r = amdgpu_device_ip_resume_phase1(adev);
2452 r = amdgpu_device_fw_loading(adev);
2456 r = amdgpu_device_ip_resume_phase2(adev);
2462 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
2464 * @adev: amdgpu_device pointer
2466 * Query the VBIOS data tables to determine if the board supports SR-IOV.
2468 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
2470 if (amdgpu_sriov_vf(adev)) {
2471 if (adev->is_atom_fw) {
2472 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2473 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2475 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2476 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2479 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
2480 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
2485 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
2487 * @asic_type: AMD asic type
2489 * Check if there is DC (new modesetting infrastructre) support for an asic.
2490 * returns true if DC has support, false if not.
2492 bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
2494 switch (asic_type) {
2495 #if defined(CONFIG_DRM_AMD_DC)
2501 * We have systems in the wild with these ASICs that require
2502 * LVDS and VGA support which is not supported with DC.
2504 * Fallback to the non-DC driver here by default so as not to
2505 * cause regressions.
2507 return amdgpu_dc > 0;
2511 case CHIP_POLARIS10:
2512 case CHIP_POLARIS11:
2513 case CHIP_POLARIS12:
2520 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
2523 #if defined(CONFIG_DRM_AMD_DC_DCN2_0)
2528 #if defined(CONFIG_DRM_AMD_DC_DCN2_1)
2531 return amdgpu_dc != 0;
2539 * amdgpu_device_has_dc_support - check if dc is supported
2541 * @adev: amdgpu_device_pointer
2543 * Returns true for supported, false for not supported
2545 bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
2547 if (amdgpu_sriov_vf(adev))
2550 return amdgpu_device_asic_has_dc_support(adev->asic_type);
2554 static void amdgpu_device_xgmi_reset_func(struct work_struct *__work)
2556 struct amdgpu_device *adev =
2557 container_of(__work, struct amdgpu_device, xgmi_reset_work);
2559 adev->asic_reset_res = amdgpu_asic_reset(adev);
2560 if (adev->asic_reset_res)
2561 DRM_WARN("ASIC reset failed with error, %d for drm dev, %s",
2562 adev->asic_reset_res, adev->ddev->unique);
2567 * amdgpu_device_init - initialize the driver
2569 * @adev: amdgpu_device pointer
2570 * @ddev: drm dev pointer
2571 * @pdev: pci dev pointer
2572 * @flags: driver flags
2574 * Initializes the driver info and hw (all asics).
2575 * Returns 0 for success or an error on failure.
2576 * Called at driver startup.
2578 int amdgpu_device_init(struct amdgpu_device *adev,
2579 struct drm_device *ddev,
2580 struct pci_dev *pdev,
2584 bool runtime = false;
2587 adev->shutdown = false;
2588 adev->dev = &pdev->dev;
2591 adev->flags = flags;
2593 if (amdgpu_force_asic_type >= 0 && amdgpu_force_asic_type < CHIP_LAST)
2594 adev->asic_type = amdgpu_force_asic_type;
2596 adev->asic_type = flags & AMD_ASIC_MASK;
2598 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
2599 if (amdgpu_emu_mode == 1)
2600 adev->usec_timeout *= 2;
2601 adev->gmc.gart_size = 512 * 1024 * 1024;
2602 adev->accel_working = false;
2603 adev->num_rings = 0;
2604 adev->mman.buffer_funcs = NULL;
2605 adev->mman.buffer_funcs_ring = NULL;
2606 adev->vm_manager.vm_pte_funcs = NULL;
2607 adev->vm_manager.vm_pte_num_rqs = 0;
2608 adev->gmc.gmc_funcs = NULL;
2609 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
2610 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
2612 adev->smc_rreg = &amdgpu_invalid_rreg;
2613 adev->smc_wreg = &amdgpu_invalid_wreg;
2614 adev->pcie_rreg = &amdgpu_invalid_rreg;
2615 adev->pcie_wreg = &amdgpu_invalid_wreg;
2616 adev->pciep_rreg = &amdgpu_invalid_rreg;
2617 adev->pciep_wreg = &amdgpu_invalid_wreg;
2618 adev->pcie_rreg64 = &amdgpu_invalid_rreg64;
2619 adev->pcie_wreg64 = &amdgpu_invalid_wreg64;
2620 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2621 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2622 adev->didt_rreg = &amdgpu_invalid_rreg;
2623 adev->didt_wreg = &amdgpu_invalid_wreg;
2624 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2625 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
2626 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2627 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2629 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2630 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2631 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
2633 /* mutex initialization are all done here so we
2634 * can recall function without having locking issues */
2635 atomic_set(&adev->irq.ih.lock, 0);
2636 mutex_init(&adev->firmware.mutex);
2637 mutex_init(&adev->pm.mutex);
2638 mutex_init(&adev->gfx.gpu_clock_mutex);
2639 mutex_init(&adev->srbm_mutex);
2640 mutex_init(&adev->gfx.pipe_reserve_mutex);
2641 mutex_init(&adev->gfx.gfx_off_mutex);
2642 mutex_init(&adev->grbm_idx_mutex);
2643 mutex_init(&adev->mn_lock);
2644 mutex_init(&adev->virt.vf_errors.lock);
2645 hash_init(adev->mn_hash);
2646 mutex_init(&adev->lock_reset);
2647 mutex_init(&adev->virt.dpm_mutex);
2648 mutex_init(&adev->psp.mutex);
2650 r = amdgpu_device_check_arguments(adev);
2654 spin_lock_init(&adev->mmio_idx_lock);
2655 spin_lock_init(&adev->smc_idx_lock);
2656 spin_lock_init(&adev->pcie_idx_lock);
2657 spin_lock_init(&adev->uvd_ctx_idx_lock);
2658 spin_lock_init(&adev->didt_idx_lock);
2659 spin_lock_init(&adev->gc_cac_idx_lock);
2660 spin_lock_init(&adev->se_cac_idx_lock);
2661 spin_lock_init(&adev->audio_endpt_idx_lock);
2662 spin_lock_init(&adev->mm_stats.lock);
2664 INIT_LIST_HEAD(&adev->shadow_list);
2665 mutex_init(&adev->shadow_list_lock);
2667 INIT_LIST_HEAD(&adev->ring_lru_list);
2668 spin_lock_init(&adev->ring_lru_list_lock);
2670 INIT_DELAYED_WORK(&adev->delayed_init_work,
2671 amdgpu_device_delayed_init_work_handler);
2672 INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work,
2673 amdgpu_device_delay_enable_gfx_off);
2675 INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
2677 adev->gfx.gfx_off_req_count = 1;
2678 adev->pm.ac_power = power_supply_is_system_supplied() > 0 ? true : false;
2680 /* Registers mapping */
2681 /* TODO: block userspace mapping of io register */
2682 if (adev->asic_type >= CHIP_BONAIRE) {
2683 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2684 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2686 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2687 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2690 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2691 if (adev->rmmio == NULL) {
2694 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2695 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2697 /* io port mapping */
2698 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2699 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2700 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2701 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2705 if (adev->rio_mem == NULL)
2706 DRM_INFO("PCI I/O BAR is not found.\n");
2708 /* enable PCIE atomic ops */
2709 r = pci_enable_atomic_ops_to_root(adev->pdev,
2710 PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
2711 PCI_EXP_DEVCAP2_ATOMIC_COMP64);
2713 adev->have_atomics_support = false;
2714 DRM_INFO("PCIE atomic ops is not supported\n");
2716 adev->have_atomics_support = true;
2719 amdgpu_device_get_pcie_info(adev);
2722 DRM_INFO("MCBP is enabled\n");
2724 if (amdgpu_mes && adev->asic_type >= CHIP_NAVI10)
2725 adev->enable_mes = true;
2727 if (amdgpu_discovery && adev->asic_type >= CHIP_NAVI10) {
2728 r = amdgpu_discovery_init(adev);
2730 dev_err(adev->dev, "amdgpu_discovery_init failed\n");
2735 /* early init functions */
2736 r = amdgpu_device_ip_early_init(adev);
2740 /* doorbell bar mapping and doorbell index init*/
2741 amdgpu_device_doorbell_init(adev);
2743 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2744 /* this will fail for cards that aren't VGA class devices, just
2746 vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
2748 if (amdgpu_device_is_px(ddev))
2750 if (!pci_is_thunderbolt_attached(adev->pdev))
2751 vga_switcheroo_register_client(adev->pdev,
2752 &amdgpu_switcheroo_ops, runtime);
2754 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2756 if (amdgpu_emu_mode == 1) {
2757 /* post the asic on emulation mode */
2758 emu_soc_asic_init(adev);
2759 goto fence_driver_init;
2762 /* detect if we are with an SRIOV vbios */
2763 amdgpu_device_detect_sriov_bios(adev);
2765 /* check if we need to reset the asic
2766 * E.g., driver was not cleanly unloaded previously, etc.
2768 if (!amdgpu_sriov_vf(adev) && amdgpu_asic_need_reset_on_init(adev)) {
2769 r = amdgpu_asic_reset(adev);
2771 dev_err(adev->dev, "asic reset on init failed\n");
2776 /* Post card if necessary */
2777 if (amdgpu_device_need_post(adev)) {
2779 dev_err(adev->dev, "no vBIOS found\n");
2783 DRM_INFO("GPU posting now...\n");
2784 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2786 dev_err(adev->dev, "gpu post error!\n");
2791 if (adev->is_atom_fw) {
2792 /* Initialize clocks */
2793 r = amdgpu_atomfirmware_get_clock_info(adev);
2795 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
2796 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2800 /* Initialize clocks */
2801 r = amdgpu_atombios_get_clock_info(adev);
2803 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
2804 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2807 /* init i2c buses */
2808 if (!amdgpu_device_has_dc_support(adev))
2809 amdgpu_atombios_i2c_init(adev);
2814 r = amdgpu_fence_driver_init(adev);
2816 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
2817 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
2821 /* init the mode config */
2822 drm_mode_config_init(adev->ddev);
2824 r = amdgpu_device_ip_init(adev);
2826 /* failed in exclusive mode due to timeout */
2827 if (amdgpu_sriov_vf(adev) &&
2828 !amdgpu_sriov_runtime(adev) &&
2829 amdgpu_virt_mmio_blocked(adev) &&
2830 !amdgpu_virt_wait_reset(adev)) {
2831 dev_err(adev->dev, "VF exclusive mode timeout\n");
2832 /* Don't send request since VF is inactive. */
2833 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
2834 adev->virt.ops = NULL;
2838 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
2839 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
2840 if (amdgpu_virt_request_full_gpu(adev, false))
2841 amdgpu_virt_release_full_gpu(adev, false);
2845 adev->accel_working = true;
2847 amdgpu_vm_check_compute_bug(adev);
2849 /* Initialize the buffer migration limit. */
2850 if (amdgpu_moverate >= 0)
2851 max_MBps = amdgpu_moverate;
2853 max_MBps = 8; /* Allow 8 MB/s. */
2854 /* Get a log2 for easy divisions. */
2855 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2857 amdgpu_fbdev_init(adev);
2859 if (amdgpu_sriov_vf(adev) && amdgim_is_hwperf(adev))
2860 amdgpu_pm_virt_sysfs_init(adev);
2862 r = amdgpu_pm_sysfs_init(adev);
2864 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2866 r = amdgpu_ucode_sysfs_init(adev);
2868 DRM_ERROR("Creating firmware sysfs failed (%d).\n", r);
2870 r = amdgpu_debugfs_gem_init(adev);
2872 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
2874 r = amdgpu_debugfs_regs_init(adev);
2876 DRM_ERROR("registering register debugfs failed (%d).\n", r);
2878 r = amdgpu_debugfs_firmware_init(adev);
2880 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
2882 r = amdgpu_debugfs_init(adev);
2884 DRM_ERROR("Creating debugfs files failed (%d).\n", r);
2886 if ((amdgpu_testing & 1)) {
2887 if (adev->accel_working)
2888 amdgpu_test_moves(adev);
2890 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2892 if (amdgpu_benchmarking) {
2893 if (adev->accel_working)
2894 amdgpu_benchmark(adev, amdgpu_benchmarking);
2896 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2899 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2900 * explicit gating rather than handling it automatically.
2902 r = amdgpu_device_ip_late_init(adev);
2904 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
2905 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
2910 amdgpu_ras_resume(adev);
2912 queue_delayed_work(system_wq, &adev->delayed_init_work,
2913 msecs_to_jiffies(AMDGPU_RESUME_MS));
2915 r = device_create_file(adev->dev, &dev_attr_pcie_replay_count);
2917 dev_err(adev->dev, "Could not create pcie_replay_count");
2921 if (IS_ENABLED(CONFIG_PERF_EVENTS))
2922 r = amdgpu_pmu_init(adev);
2924 dev_err(adev->dev, "amdgpu_pmu_init failed\n");
2929 amdgpu_vf_error_trans_all(adev);
2931 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2937 * amdgpu_device_fini - tear down the driver
2939 * @adev: amdgpu_device pointer
2941 * Tear down the driver info (all asics).
2942 * Called at driver shutdown.
2944 void amdgpu_device_fini(struct amdgpu_device *adev)
2948 DRM_INFO("amdgpu: finishing device.\n");
2949 adev->shutdown = true;
2950 /* disable all interrupts */
2951 amdgpu_irq_disable_all(adev);
2952 if (adev->mode_info.mode_config_initialized){
2953 if (!amdgpu_device_has_dc_support(adev))
2954 drm_helper_force_disable_all(adev->ddev);
2956 drm_atomic_helper_shutdown(adev->ddev);
2958 amdgpu_fence_driver_fini(adev);
2959 amdgpu_pm_sysfs_fini(adev);
2960 amdgpu_fbdev_fini(adev);
2961 r = amdgpu_device_ip_fini(adev);
2962 if (adev->firmware.gpu_info_fw) {
2963 release_firmware(adev->firmware.gpu_info_fw);
2964 adev->firmware.gpu_info_fw = NULL;
2966 adev->accel_working = false;
2967 cancel_delayed_work_sync(&adev->delayed_init_work);
2968 /* free i2c buses */
2969 if (!amdgpu_device_has_dc_support(adev))
2970 amdgpu_i2c_fini(adev);
2972 if (amdgpu_emu_mode != 1)
2973 amdgpu_atombios_fini(adev);
2977 if (!pci_is_thunderbolt_attached(adev->pdev))
2978 vga_switcheroo_unregister_client(adev->pdev);
2979 if (adev->flags & AMD_IS_PX)
2980 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2981 vga_client_register(adev->pdev, NULL, NULL, NULL);
2983 pci_iounmap(adev->pdev, adev->rio_mem);
2984 adev->rio_mem = NULL;
2985 iounmap(adev->rmmio);
2987 amdgpu_device_doorbell_fini(adev);
2988 if (amdgpu_sriov_vf(adev) && amdgim_is_hwperf(adev))
2989 amdgpu_pm_virt_sysfs_fini(adev);
2991 amdgpu_debugfs_regs_cleanup(adev);
2992 device_remove_file(adev->dev, &dev_attr_pcie_replay_count);
2993 amdgpu_ucode_sysfs_fini(adev);
2994 if (IS_ENABLED(CONFIG_PERF_EVENTS))
2995 amdgpu_pmu_fini(adev);
2996 amdgpu_debugfs_preempt_cleanup(adev);
2997 if (amdgpu_discovery && adev->asic_type >= CHIP_NAVI10)
2998 amdgpu_discovery_fini(adev);
3006 * amdgpu_device_suspend - initiate device suspend
3008 * @dev: drm dev pointer
3009 * @suspend: suspend state
3010 * @fbcon : notify the fbdev of suspend
3012 * Puts the hw in the suspend state (all asics).
3013 * Returns 0 for success or an error on failure.
3014 * Called at driver suspend.
3016 int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
3018 struct amdgpu_device *adev;
3019 struct drm_crtc *crtc;
3020 struct drm_connector *connector;
3023 if (dev == NULL || dev->dev_private == NULL) {
3027 adev = dev->dev_private;
3029 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
3032 adev->in_suspend = true;
3033 drm_kms_helper_poll_disable(dev);
3036 amdgpu_fbdev_set_suspend(adev, 1);
3038 cancel_delayed_work_sync(&adev->delayed_init_work);
3040 if (!amdgpu_device_has_dc_support(adev)) {
3041 /* turn off display hw */
3042 drm_modeset_lock_all(dev);
3043 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3044 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
3046 drm_modeset_unlock_all(dev);
3047 /* unpin the front buffers and cursors */
3048 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3049 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
3050 struct drm_framebuffer *fb = crtc->primary->fb;
3051 struct amdgpu_bo *robj;
3053 if (amdgpu_crtc->cursor_bo && !adev->enable_virtual_display) {
3054 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
3055 r = amdgpu_bo_reserve(aobj, true);
3057 amdgpu_bo_unpin(aobj);
3058 amdgpu_bo_unreserve(aobj);
3062 if (fb == NULL || fb->obj[0] == NULL) {
3065 robj = gem_to_amdgpu_bo(fb->obj[0]);
3066 /* don't unpin kernel fb objects */
3067 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
3068 r = amdgpu_bo_reserve(robj, true);
3070 amdgpu_bo_unpin(robj);
3071 amdgpu_bo_unreserve(robj);
3077 amdgpu_amdkfd_suspend(adev);
3079 amdgpu_ras_suspend(adev);
3081 r = amdgpu_device_ip_suspend_phase1(adev);
3083 /* evict vram memory */
3084 amdgpu_bo_evict_vram(adev);
3086 amdgpu_fence_driver_suspend(adev);
3088 r = amdgpu_device_ip_suspend_phase2(adev);
3090 /* evict remaining vram memory
3091 * This second call to evict vram is to evict the gart page table
3094 amdgpu_bo_evict_vram(adev);
3096 pci_save_state(dev->pdev);
3098 /* Shut down the device */
3099 pci_disable_device(dev->pdev);
3100 pci_set_power_state(dev->pdev, PCI_D3hot);
3102 r = amdgpu_asic_reset(adev);
3104 DRM_ERROR("amdgpu asic reset failed\n");
3111 * amdgpu_device_resume - initiate device resume
3113 * @dev: drm dev pointer
3114 * @resume: resume state
3115 * @fbcon : notify the fbdev of resume
3117 * Bring the hw back to operating state (all asics).
3118 * Returns 0 for success or an error on failure.
3119 * Called at driver resume.
3121 int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
3123 struct drm_connector *connector;
3124 struct amdgpu_device *adev = dev->dev_private;
3125 struct drm_crtc *crtc;
3128 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
3132 pci_set_power_state(dev->pdev, PCI_D0);
3133 pci_restore_state(dev->pdev);
3134 r = pci_enable_device(dev->pdev);
3140 if (amdgpu_device_need_post(adev)) {
3141 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
3143 DRM_ERROR("amdgpu asic init failed\n");
3146 r = amdgpu_device_ip_resume(adev);
3148 DRM_ERROR("amdgpu_device_ip_resume failed (%d).\n", r);
3151 amdgpu_fence_driver_resume(adev);
3154 r = amdgpu_device_ip_late_init(adev);
3158 queue_delayed_work(system_wq, &adev->delayed_init_work,
3159 msecs_to_jiffies(AMDGPU_RESUME_MS));
3161 if (!amdgpu_device_has_dc_support(adev)) {
3163 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3164 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
3166 if (amdgpu_crtc->cursor_bo && !adev->enable_virtual_display) {
3167 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
3168 r = amdgpu_bo_reserve(aobj, true);
3170 r = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM);
3172 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
3173 amdgpu_crtc->cursor_addr = amdgpu_bo_gpu_offset(aobj);
3174 amdgpu_bo_unreserve(aobj);
3179 r = amdgpu_amdkfd_resume(adev);
3183 /* Make sure IB tests flushed */
3184 flush_delayed_work(&adev->delayed_init_work);
3186 /* blat the mode back in */
3188 if (!amdgpu_device_has_dc_support(adev)) {
3190 drm_helper_resume_force_mode(dev);
3192 /* turn on display hw */
3193 drm_modeset_lock_all(dev);
3194 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3195 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
3197 drm_modeset_unlock_all(dev);
3199 amdgpu_fbdev_set_suspend(adev, 0);
3202 drm_kms_helper_poll_enable(dev);
3204 amdgpu_ras_resume(adev);
3207 * Most of the connector probing functions try to acquire runtime pm
3208 * refs to ensure that the GPU is powered on when connector polling is
3209 * performed. Since we're calling this from a runtime PM callback,
3210 * trying to acquire rpm refs will cause us to deadlock.
3212 * Since we're guaranteed to be holding the rpm lock, it's safe to
3213 * temporarily disable the rpm helpers so this doesn't deadlock us.
3216 dev->dev->power.disable_depth++;
3218 if (!amdgpu_device_has_dc_support(adev))
3219 drm_helper_hpd_irq_event(dev);
3221 drm_kms_helper_hotplug_event(dev);
3223 dev->dev->power.disable_depth--;
3225 adev->in_suspend = false;
3231 * amdgpu_device_ip_check_soft_reset - did soft reset succeed
3233 * @adev: amdgpu_device pointer
3235 * The list of all the hardware IPs that make up the asic is walked and
3236 * the check_soft_reset callbacks are run. check_soft_reset determines
3237 * if the asic is still hung or not.
3238 * Returns true if any of the IPs are still in a hung state, false if not.
3240 static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
3243 bool asic_hang = false;
3245 if (amdgpu_sriov_vf(adev))
3248 if (amdgpu_asic_need_full_reset(adev))
3251 for (i = 0; i < adev->num_ip_blocks; i++) {
3252 if (!adev->ip_blocks[i].status.valid)
3254 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
3255 adev->ip_blocks[i].status.hang =
3256 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
3257 if (adev->ip_blocks[i].status.hang) {
3258 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
3266 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
3268 * @adev: amdgpu_device pointer
3270 * The list of all the hardware IPs that make up the asic is walked and the
3271 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
3272 * handles any IP specific hardware or software state changes that are
3273 * necessary for a soft reset to succeed.
3274 * Returns 0 on success, negative error code on failure.
3276 static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
3280 for (i = 0; i < adev->num_ip_blocks; i++) {
3281 if (!adev->ip_blocks[i].status.valid)
3283 if (adev->ip_blocks[i].status.hang &&
3284 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
3285 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
3295 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
3297 * @adev: amdgpu_device pointer
3299 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
3300 * reset is necessary to recover.
3301 * Returns true if a full asic reset is required, false if not.
3303 static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
3307 if (amdgpu_asic_need_full_reset(adev))
3310 for (i = 0; i < adev->num_ip_blocks; i++) {
3311 if (!adev->ip_blocks[i].status.valid)
3313 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
3314 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
3315 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
3316 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
3317 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
3318 if (adev->ip_blocks[i].status.hang) {
3319 DRM_INFO("Some block need full reset!\n");
3328 * amdgpu_device_ip_soft_reset - do a soft reset
3330 * @adev: amdgpu_device pointer
3332 * The list of all the hardware IPs that make up the asic is walked and the
3333 * soft_reset callbacks are run if the block is hung. soft_reset handles any
3334 * IP specific hardware or software state changes that are necessary to soft
3336 * Returns 0 on success, negative error code on failure.
3338 static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
3342 for (i = 0; i < adev->num_ip_blocks; i++) {
3343 if (!adev->ip_blocks[i].status.valid)
3345 if (adev->ip_blocks[i].status.hang &&
3346 adev->ip_blocks[i].version->funcs->soft_reset) {
3347 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
3357 * amdgpu_device_ip_post_soft_reset - clean up from soft reset
3359 * @adev: amdgpu_device pointer
3361 * The list of all the hardware IPs that make up the asic is walked and the
3362 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
3363 * handles any IP specific hardware or software state changes that are
3364 * necessary after the IP has been soft reset.
3365 * Returns 0 on success, negative error code on failure.
3367 static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
3371 for (i = 0; i < adev->num_ip_blocks; i++) {
3372 if (!adev->ip_blocks[i].status.valid)
3374 if (adev->ip_blocks[i].status.hang &&
3375 adev->ip_blocks[i].version->funcs->post_soft_reset)
3376 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
3385 * amdgpu_device_recover_vram - Recover some VRAM contents
3387 * @adev: amdgpu_device pointer
3389 * Restores the contents of VRAM buffers from the shadows in GTT. Used to
3390 * restore things like GPUVM page tables after a GPU reset where
3391 * the contents of VRAM might be lost.
3394 * 0 on success, negative error code on failure.
3396 static int amdgpu_device_recover_vram(struct amdgpu_device *adev)
3398 struct dma_fence *fence = NULL, *next = NULL;
3399 struct amdgpu_bo *shadow;
3402 if (amdgpu_sriov_runtime(adev))
3403 tmo = msecs_to_jiffies(8000);
3405 tmo = msecs_to_jiffies(100);
3407 DRM_INFO("recover vram bo from shadow start\n");
3408 mutex_lock(&adev->shadow_list_lock);
3409 list_for_each_entry(shadow, &adev->shadow_list, shadow_list) {
3411 /* No need to recover an evicted BO */
3412 if (shadow->tbo.mem.mem_type != TTM_PL_TT ||
3413 shadow->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET ||
3414 shadow->parent->tbo.mem.mem_type != TTM_PL_VRAM)
3417 r = amdgpu_bo_restore_shadow(shadow, &next);
3422 tmo = dma_fence_wait_timeout(fence, false, tmo);
3423 dma_fence_put(fence);
3428 } else if (tmo < 0) {
3436 mutex_unlock(&adev->shadow_list_lock);
3439 tmo = dma_fence_wait_timeout(fence, false, tmo);
3440 dma_fence_put(fence);
3442 if (r < 0 || tmo <= 0) {
3443 DRM_ERROR("recover vram bo from shadow failed, r is %ld, tmo is %ld\n", r, tmo);
3447 DRM_INFO("recover vram bo from shadow done\n");
3453 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
3455 * @adev: amdgpu device pointer
3456 * @from_hypervisor: request from hypervisor
3458 * do VF FLR and reinitialize Asic
3459 * return 0 means succeeded otherwise failed
3461 static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
3462 bool from_hypervisor)
3466 if (from_hypervisor)
3467 r = amdgpu_virt_request_full_gpu(adev, true);
3469 r = amdgpu_virt_reset_gpu(adev);
3473 amdgpu_amdkfd_pre_reset(adev);
3475 /* Resume IP prior to SMC */
3476 r = amdgpu_device_ip_reinit_early_sriov(adev);
3480 /* we need recover gart prior to run SMC/CP/SDMA resume */
3481 amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
3483 r = amdgpu_device_fw_loading(adev);
3487 /* now we are okay to resume SMC/CP/SDMA */
3488 r = amdgpu_device_ip_reinit_late_sriov(adev);
3492 amdgpu_irq_gpu_reset_resume_helper(adev);
3493 r = amdgpu_ib_ring_tests(adev);
3494 amdgpu_amdkfd_post_reset(adev);
3497 amdgpu_virt_init_data_exchange(adev);
3498 amdgpu_virt_release_full_gpu(adev, true);
3499 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
3500 amdgpu_inc_vram_lost(adev);
3501 r = amdgpu_device_recover_vram(adev);
3508 * amdgpu_device_should_recover_gpu - check if we should try GPU recovery
3510 * @adev: amdgpu device pointer
3512 * Check amdgpu_gpu_recovery and SRIOV status to see if we should try to recover
3515 bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev)
3517 if (!amdgpu_device_ip_check_soft_reset(adev)) {
3518 DRM_INFO("Timeout, but no hardware hang detected.\n");
3522 if (amdgpu_gpu_recovery == 0)
3525 if (amdgpu_sriov_vf(adev))
3528 if (amdgpu_gpu_recovery == -1) {
3529 switch (adev->asic_type) {
3535 case CHIP_POLARIS10:
3536 case CHIP_POLARIS11:
3537 case CHIP_POLARIS12:
3552 DRM_INFO("GPU recovery disabled.\n");
3557 static int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
3558 struct amdgpu_job *job,
3559 bool *need_full_reset_arg)
3562 bool need_full_reset = *need_full_reset_arg;
3564 /* block all schedulers and reset given job's ring */
3565 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3566 struct amdgpu_ring *ring = adev->rings[i];
3568 if (!ring || !ring->sched.thread)
3571 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
3572 amdgpu_fence_driver_force_completion(ring);
3576 drm_sched_increase_karma(&job->base);
3578 /* Don't suspend on bare metal if we are not going to HW reset the ASIC */
3579 if (!amdgpu_sriov_vf(adev)) {
3581 if (!need_full_reset)
3582 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
3584 if (!need_full_reset) {
3585 amdgpu_device_ip_pre_soft_reset(adev);
3586 r = amdgpu_device_ip_soft_reset(adev);
3587 amdgpu_device_ip_post_soft_reset(adev);
3588 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
3589 DRM_INFO("soft reset failed, will fallback to full reset!\n");
3590 need_full_reset = true;
3594 if (need_full_reset)
3595 r = amdgpu_device_ip_suspend(adev);
3597 *need_full_reset_arg = need_full_reset;
3603 static int amdgpu_do_asic_reset(struct amdgpu_hive_info *hive,
3604 struct list_head *device_list_handle,
3605 bool *need_full_reset_arg)
3607 struct amdgpu_device *tmp_adev = NULL;
3608 bool need_full_reset = *need_full_reset_arg, vram_lost = false;
3612 * ASIC reset has to be done on all HGMI hive nodes ASAP
3613 * to allow proper links negotiation in FW (within 1 sec)
3615 if (need_full_reset) {
3616 list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
3617 /* For XGMI run all resets in parallel to speed up the process */
3618 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
3619 if (!queue_work(system_highpri_wq, &tmp_adev->xgmi_reset_work))
3622 r = amdgpu_asic_reset(tmp_adev);
3625 DRM_ERROR("ASIC reset failed with error, %d for drm dev, %s",
3626 r, tmp_adev->ddev->unique);
3631 /* For XGMI wait for all PSP resets to complete before proceed */
3633 list_for_each_entry(tmp_adev, device_list_handle,
3635 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
3636 flush_work(&tmp_adev->xgmi_reset_work);
3637 r = tmp_adev->asic_reset_res;
3646 list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
3647 if (need_full_reset) {
3649 if (amdgpu_atom_asic_init(tmp_adev->mode_info.atom_context))
3650 DRM_WARN("asic atom init failed!");
3653 dev_info(tmp_adev->dev, "GPU reset succeeded, trying to resume\n");
3654 r = amdgpu_device_ip_resume_phase1(tmp_adev);
3658 vram_lost = amdgpu_device_check_vram_lost(tmp_adev);
3660 DRM_INFO("VRAM is lost due to GPU reset!\n");
3661 amdgpu_inc_vram_lost(tmp_adev);
3664 r = amdgpu_gtt_mgr_recover(
3665 &tmp_adev->mman.bdev.man[TTM_PL_TT]);
3669 r = amdgpu_device_fw_loading(tmp_adev);
3673 r = amdgpu_device_ip_resume_phase2(tmp_adev);
3678 amdgpu_device_fill_reset_magic(tmp_adev);
3681 * Add this ASIC as tracked as reset was already
3682 * complete successfully.
3684 amdgpu_register_gpu_instance(tmp_adev);
3686 r = amdgpu_device_ip_late_init(tmp_adev);
3691 amdgpu_ras_resume(tmp_adev);
3693 /* Update PSP FW topology after reset */
3694 if (hive && tmp_adev->gmc.xgmi.num_physical_nodes > 1)
3695 r = amdgpu_xgmi_update_topology(hive, tmp_adev);
3702 amdgpu_irq_gpu_reset_resume_helper(tmp_adev);
3703 r = amdgpu_ib_ring_tests(tmp_adev);
3705 dev_err(tmp_adev->dev, "ib ring test failed (%d).\n", r);
3706 r = amdgpu_device_ip_suspend(tmp_adev);
3707 need_full_reset = true;
3714 r = amdgpu_device_recover_vram(tmp_adev);
3716 tmp_adev->asic_reset_res = r;
3720 *need_full_reset_arg = need_full_reset;
3724 static bool amdgpu_device_lock_adev(struct amdgpu_device *adev, bool trylock)
3727 if (!mutex_trylock(&adev->lock_reset))
3730 mutex_lock(&adev->lock_reset);
3732 atomic_inc(&adev->gpu_reset_counter);
3733 adev->in_gpu_reset = 1;
3734 switch (amdgpu_asic_reset_method(adev)) {
3735 case AMD_RESET_METHOD_MODE1:
3736 adev->mp1_state = PP_MP1_STATE_SHUTDOWN;
3738 case AMD_RESET_METHOD_MODE2:
3739 adev->mp1_state = PP_MP1_STATE_RESET;
3742 adev->mp1_state = PP_MP1_STATE_NONE;
3749 static void amdgpu_device_unlock_adev(struct amdgpu_device *adev)
3751 amdgpu_vf_error_trans_all(adev);
3752 adev->mp1_state = PP_MP1_STATE_NONE;
3753 adev->in_gpu_reset = 0;
3754 mutex_unlock(&adev->lock_reset);
3758 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
3760 * @adev: amdgpu device pointer
3761 * @job: which job trigger hang
3763 * Attempt to reset the GPU if it has hung (all asics).
3764 * Attempt to do soft-reset or full-reset and reinitialize Asic
3765 * Returns 0 for success or an error on failure.
3768 int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
3769 struct amdgpu_job *job)
3771 struct list_head device_list, *device_list_handle = NULL;
3772 bool need_full_reset, job_signaled;
3773 struct amdgpu_hive_info *hive = NULL;
3774 struct amdgpu_device *tmp_adev = NULL;
3776 bool in_ras_intr = amdgpu_ras_intr_triggered();
3779 * Flush RAM to disk so that after reboot
3780 * the user can read log and see why the system rebooted.
3782 if (in_ras_intr && amdgpu_ras_get_context(adev)->reboot) {
3784 DRM_WARN("Emergency reboot.");
3787 emergency_restart();
3790 need_full_reset = job_signaled = false;
3791 INIT_LIST_HEAD(&device_list);
3793 dev_info(adev->dev, "GPU %s begin!\n", in_ras_intr ? "jobs stop":"reset");
3795 cancel_delayed_work_sync(&adev->delayed_init_work);
3797 hive = amdgpu_get_xgmi_hive(adev, false);
3800 * Here we trylock to avoid chain of resets executing from
3801 * either trigger by jobs on different adevs in XGMI hive or jobs on
3802 * different schedulers for same device while this TO handler is running.
3803 * We always reset all schedulers for device and all devices for XGMI
3804 * hive so that should take care of them too.
3807 if (hive && !mutex_trylock(&hive->reset_lock)) {
3808 DRM_INFO("Bailing on TDR for s_job:%llx, hive: %llx as another already in progress",
3809 job ? job->base.id : -1, hive->hive_id);
3813 /* Start with adev pre asic reset first for soft reset check.*/
3814 if (!amdgpu_device_lock_adev(adev, !hive)) {
3815 DRM_INFO("Bailing on TDR for s_job:%llx, as another already in progress",
3816 job ? job->base.id : -1);
3820 /* Block kfd: SRIOV would do it separately */
3821 if (!amdgpu_sriov_vf(adev))
3822 amdgpu_amdkfd_pre_reset(adev);
3824 /* Build list of devices to reset */
3825 if (adev->gmc.xgmi.num_physical_nodes > 1) {
3827 /*unlock kfd: SRIOV would do it separately */
3828 if (!amdgpu_sriov_vf(adev))
3829 amdgpu_amdkfd_post_reset(adev);
3830 amdgpu_device_unlock_adev(adev);
3835 * In case we are in XGMI hive mode device reset is done for all the
3836 * nodes in the hive to retrain all XGMI links and hence the reset
3837 * sequence is executed in loop on all nodes.
3839 device_list_handle = &hive->device_list;
3841 list_add_tail(&adev->gmc.xgmi.head, &device_list);
3842 device_list_handle = &device_list;
3845 /* block all schedulers and reset given job's ring */
3846 list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
3847 if (tmp_adev != adev) {
3848 amdgpu_device_lock_adev(tmp_adev, false);
3849 if (!amdgpu_sriov_vf(tmp_adev))
3850 amdgpu_amdkfd_pre_reset(tmp_adev);
3854 * Mark these ASICs to be reseted as untracked first
3855 * And add them back after reset completed
3857 amdgpu_unregister_gpu_instance(tmp_adev);
3859 /* disable ras on ALL IPs */
3860 if (!in_ras_intr && amdgpu_device_ip_need_full_reset(tmp_adev))
3861 amdgpu_ras_suspend(tmp_adev);
3863 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3864 struct amdgpu_ring *ring = tmp_adev->rings[i];
3866 if (!ring || !ring->sched.thread)
3869 drm_sched_stop(&ring->sched, job ? &job->base : NULL);
3872 amdgpu_job_stop_all_jobs_on_sched(&ring->sched);
3878 goto skip_sched_resume;
3881 * Must check guilty signal here since after this point all old
3882 * HW fences are force signaled.
3884 * job->base holds a reference to parent fence
3886 if (job && job->base.s_fence->parent &&
3887 dma_fence_is_signaled(job->base.s_fence->parent))
3888 job_signaled = true;
3891 dev_info(adev->dev, "Guilty job already signaled, skipping HW reset");
3896 /* Guilty job will be freed after this*/
3897 r = amdgpu_device_pre_asic_reset(adev, job, &need_full_reset);
3899 /*TODO Should we stop ?*/
3900 DRM_ERROR("GPU pre asic reset failed with err, %d for drm dev, %s ",
3901 r, adev->ddev->unique);
3902 adev->asic_reset_res = r;
3905 retry: /* Rest of adevs pre asic reset from XGMI hive. */
3906 list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
3908 if (tmp_adev == adev)
3911 r = amdgpu_device_pre_asic_reset(tmp_adev,
3914 /*TODO Should we stop ?*/
3916 DRM_ERROR("GPU pre asic reset failed with err, %d for drm dev, %s ",
3917 r, tmp_adev->ddev->unique);
3918 tmp_adev->asic_reset_res = r;
3922 /* Actual ASIC resets if needed.*/
3923 /* TODO Implement XGMI hive reset logic for SRIOV */
3924 if (amdgpu_sriov_vf(adev)) {
3925 r = amdgpu_device_reset_sriov(adev, job ? false : true);
3927 adev->asic_reset_res = r;
3929 r = amdgpu_do_asic_reset(hive, device_list_handle, &need_full_reset);
3930 if (r && r == -EAGAIN)
3936 /* Post ASIC reset for all devs .*/
3937 list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
3939 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3940 struct amdgpu_ring *ring = tmp_adev->rings[i];
3942 if (!ring || !ring->sched.thread)
3945 /* No point to resubmit jobs if we didn't HW reset*/
3946 if (!tmp_adev->asic_reset_res && !job_signaled)
3947 drm_sched_resubmit_jobs(&ring->sched);
3949 drm_sched_start(&ring->sched, !tmp_adev->asic_reset_res);
3952 if (!amdgpu_device_has_dc_support(tmp_adev) && !job_signaled) {
3953 drm_helper_resume_force_mode(tmp_adev->ddev);
3956 tmp_adev->asic_reset_res = 0;
3959 /* bad news, how to tell it to userspace ? */
3960 dev_info(tmp_adev->dev, "GPU reset(%d) failed\n", atomic_read(&tmp_adev->gpu_reset_counter));
3961 amdgpu_vf_error_put(tmp_adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
3963 dev_info(tmp_adev->dev, "GPU reset(%d) succeeded!\n", atomic_read(&tmp_adev->gpu_reset_counter));
3968 list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
3969 /*unlock kfd: SRIOV would do it separately */
3970 if (!in_ras_intr && !amdgpu_sriov_vf(tmp_adev))
3971 amdgpu_amdkfd_post_reset(tmp_adev);
3972 amdgpu_device_unlock_adev(tmp_adev);
3976 mutex_unlock(&hive->reset_lock);
3979 dev_info(adev->dev, "GPU reset end with ret = %d\n", r);
3984 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
3986 * @adev: amdgpu_device pointer
3988 * Fetchs and stores in the driver the PCIE capabilities (gen speed
3989 * and lanes) of the slot the device is in. Handles APUs and
3990 * virtualized environments where PCIE config space may not be available.
3992 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
3994 struct pci_dev *pdev;
3995 enum pci_bus_speed speed_cap, platform_speed_cap;
3996 enum pcie_link_width platform_link_width;
3998 if (amdgpu_pcie_gen_cap)
3999 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
4001 if (amdgpu_pcie_lane_cap)
4002 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
4004 /* covers APUs as well */
4005 if (pci_is_root_bus(adev->pdev->bus)) {
4006 if (adev->pm.pcie_gen_mask == 0)
4007 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
4008 if (adev->pm.pcie_mlw_mask == 0)
4009 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
4013 if (adev->pm.pcie_gen_mask && adev->pm.pcie_mlw_mask)
4016 pcie_bandwidth_available(adev->pdev, NULL,
4017 &platform_speed_cap, &platform_link_width);
4019 if (adev->pm.pcie_gen_mask == 0) {
4022 speed_cap = pcie_get_speed_cap(pdev);
4023 if (speed_cap == PCI_SPEED_UNKNOWN) {
4024 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4025 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
4026 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
4028 if (speed_cap == PCIE_SPEED_16_0GT)
4029 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4030 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
4031 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
4032 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4);
4033 else if (speed_cap == PCIE_SPEED_8_0GT)
4034 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4035 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
4036 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
4037 else if (speed_cap == PCIE_SPEED_5_0GT)
4038 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4039 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2);
4041 adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1;
4044 if (platform_speed_cap == PCI_SPEED_UNKNOWN) {
4045 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4046 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
4048 if (platform_speed_cap == PCIE_SPEED_16_0GT)
4049 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4050 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
4051 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
4052 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4);
4053 else if (platform_speed_cap == PCIE_SPEED_8_0GT)
4054 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4055 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
4056 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3);
4057 else if (platform_speed_cap == PCIE_SPEED_5_0GT)
4058 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4059 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
4061 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
4065 if (adev->pm.pcie_mlw_mask == 0) {
4066 if (platform_link_width == PCIE_LNK_WIDTH_UNKNOWN) {
4067 adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK;
4069 switch (platform_link_width) {
4071 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
4072 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
4073 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
4074 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
4075 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
4076 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
4077 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
4080 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
4081 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
4082 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
4083 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
4084 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
4085 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
4088 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
4089 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
4090 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
4091 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
4092 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
4095 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
4096 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
4097 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
4098 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
4101 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
4102 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
4103 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
4106 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
4107 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
4110 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;