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/kthread.h>
29 #include <linux/console.h>
30 #include <linux/slab.h>
32 #include <drm/drm_crtc_helper.h>
33 #include <drm/drm_atomic_helper.h>
34 #include <drm/amdgpu_drm.h>
35 #include <linux/vgaarb.h>
36 #include <linux/vga_switcheroo.h>
37 #include <linux/efi.h>
39 #include "amdgpu_trace.h"
40 #include "amdgpu_i2c.h"
42 #include "amdgpu_atombios.h"
43 #include "amdgpu_atomfirmware.h"
45 #ifdef CONFIG_DRM_AMDGPU_SI
48 #ifdef CONFIG_DRM_AMDGPU_CIK
53 #include "bif/bif_4_1_d.h"
54 #include <linux/pci.h>
55 #include <linux/firmware.h>
56 #include "amdgpu_vf_error.h"
58 #include "amdgpu_amdkfd.h"
59 #include "amdgpu_pm.h"
61 MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
62 MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
64 #define AMDGPU_RESUME_MS 2000
66 static const char *amdgpu_asic_name[] = {
90 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev);
93 * amdgpu_device_is_px - Is the device is a dGPU with HG/PX power control
95 * @dev: drm_device pointer
97 * Returns true if the device is a dGPU with HG/PX power control,
98 * otherwise return false.
100 bool amdgpu_device_is_px(struct drm_device *dev)
102 struct amdgpu_device *adev = dev->dev_private;
104 if (adev->flags & AMD_IS_PX)
110 * MMIO register access helper functions.
113 * amdgpu_mm_rreg - read a memory mapped IO register
115 * @adev: amdgpu_device pointer
116 * @reg: dword aligned register offset
117 * @acc_flags: access flags which require special behavior
119 * Returns the 32 bit value from the offset specified.
121 uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
126 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
127 return amdgpu_virt_kiq_rreg(adev, reg);
129 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
130 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
134 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
135 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
136 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
137 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
139 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
144 * MMIO register read with bytes helper functions
145 * @offset:bytes offset from MMIO start
150 * amdgpu_mm_rreg8 - read a memory mapped IO register
152 * @adev: amdgpu_device pointer
153 * @offset: byte aligned register offset
155 * Returns the 8 bit value from the offset specified.
157 uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset) {
158 if (offset < adev->rmmio_size)
159 return (readb(adev->rmmio + offset));
164 * MMIO register write with bytes helper functions
165 * @offset:bytes offset from MMIO start
166 * @value: the value want to be written to the register
170 * amdgpu_mm_wreg8 - read a memory mapped IO register
172 * @adev: amdgpu_device pointer
173 * @offset: byte aligned register offset
174 * @value: 8 bit value to write
176 * Writes the value specified to the offset specified.
178 void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value) {
179 if (offset < adev->rmmio_size)
180 writeb(value, adev->rmmio + offset);
186 * amdgpu_mm_wreg - write to a memory mapped IO register
188 * @adev: amdgpu_device pointer
189 * @reg: dword aligned register offset
190 * @v: 32 bit value to write to the register
191 * @acc_flags: access flags which require special behavior
193 * Writes the value specified to the offset specified.
195 void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
198 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
200 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
201 adev->last_mm_index = v;
204 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
205 return amdgpu_virt_kiq_wreg(adev, reg, v);
207 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
208 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
212 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
213 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
214 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
215 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
218 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
224 * amdgpu_io_rreg - read an IO register
226 * @adev: amdgpu_device pointer
227 * @reg: dword aligned register offset
229 * Returns the 32 bit value from the offset specified.
231 u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
233 if ((reg * 4) < adev->rio_mem_size)
234 return ioread32(adev->rio_mem + (reg * 4));
236 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
237 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
242 * amdgpu_io_wreg - write to an IO register
244 * @adev: amdgpu_device pointer
245 * @reg: dword aligned register offset
246 * @v: 32 bit value to write to the register
248 * Writes the value specified to the offset specified.
250 void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
252 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
253 adev->last_mm_index = v;
256 if ((reg * 4) < adev->rio_mem_size)
257 iowrite32(v, adev->rio_mem + (reg * 4));
259 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
260 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
263 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
269 * amdgpu_mm_rdoorbell - read a doorbell dword
271 * @adev: amdgpu_device pointer
272 * @index: doorbell index
274 * Returns the value in the doorbell aperture at the
275 * requested doorbell index (CIK).
277 u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
279 if (index < adev->doorbell.num_doorbells) {
280 return readl(adev->doorbell.ptr + index);
282 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
288 * amdgpu_mm_wdoorbell - write a doorbell dword
290 * @adev: amdgpu_device pointer
291 * @index: doorbell index
294 * Writes @v to the doorbell aperture at the
295 * requested doorbell index (CIK).
297 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
299 if (index < adev->doorbell.num_doorbells) {
300 writel(v, adev->doorbell.ptr + index);
302 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
307 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
309 * @adev: amdgpu_device pointer
310 * @index: doorbell index
312 * Returns the value in the doorbell aperture at the
313 * requested doorbell index (VEGA10+).
315 u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
317 if (index < adev->doorbell.num_doorbells) {
318 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
320 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
326 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
328 * @adev: amdgpu_device pointer
329 * @index: doorbell index
332 * Writes @v to the doorbell aperture at the
333 * requested doorbell index (VEGA10+).
335 void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
337 if (index < adev->doorbell.num_doorbells) {
338 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
340 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
345 * amdgpu_invalid_rreg - dummy reg read function
347 * @adev: amdgpu device pointer
348 * @reg: offset of register
350 * Dummy register read function. Used for register blocks
351 * that certain asics don't have (all asics).
352 * Returns the value in the register.
354 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
356 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
362 * amdgpu_invalid_wreg - dummy reg write function
364 * @adev: amdgpu device pointer
365 * @reg: offset of register
366 * @v: value to write to the register
368 * Dummy register read function. Used for register blocks
369 * that certain asics don't have (all asics).
371 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
373 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
379 * amdgpu_block_invalid_rreg - dummy reg read function
381 * @adev: amdgpu device pointer
382 * @block: offset of instance
383 * @reg: offset of register
385 * Dummy register read function. Used for register blocks
386 * that certain asics don't have (all asics).
387 * Returns the value in the register.
389 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
390 uint32_t block, uint32_t reg)
392 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
399 * amdgpu_block_invalid_wreg - dummy reg write function
401 * @adev: amdgpu device pointer
402 * @block: offset of instance
403 * @reg: offset of register
404 * @v: value to write to the register
406 * Dummy register read function. Used for register blocks
407 * that certain asics don't have (all asics).
409 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
411 uint32_t reg, uint32_t v)
413 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
419 * amdgpu_device_vram_scratch_init - allocate the VRAM scratch page
421 * @adev: amdgpu device pointer
423 * Allocates a scratch page of VRAM for use by various things in the
426 static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev)
428 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
429 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
430 &adev->vram_scratch.robj,
431 &adev->vram_scratch.gpu_addr,
432 (void **)&adev->vram_scratch.ptr);
436 * amdgpu_device_vram_scratch_fini - Free the VRAM scratch page
438 * @adev: amdgpu device pointer
440 * Frees the VRAM scratch page.
442 static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev)
444 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
448 * amdgpu_device_program_register_sequence - program an array of registers.
450 * @adev: amdgpu_device pointer
451 * @registers: pointer to the register array
452 * @array_size: size of the register array
454 * Programs an array or registers with and and or masks.
455 * This is a helper for setting golden registers.
457 void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
458 const u32 *registers,
459 const u32 array_size)
461 u32 tmp, reg, and_mask, or_mask;
467 for (i = 0; i < array_size; i +=3) {
468 reg = registers[i + 0];
469 and_mask = registers[i + 1];
470 or_mask = registers[i + 2];
472 if (and_mask == 0xffffffff) {
484 * amdgpu_device_pci_config_reset - reset the GPU
486 * @adev: amdgpu_device pointer
488 * Resets the GPU using the pci config reset sequence.
489 * Only applicable to asics prior to vega10.
491 void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
493 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
497 * GPU doorbell aperture helpers function.
500 * amdgpu_device_doorbell_init - Init doorbell driver information.
502 * @adev: amdgpu_device pointer
504 * Init doorbell driver information (CIK)
505 * Returns 0 on success, error on failure.
507 static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
509 /* No doorbell on SI hardware generation */
510 if (adev->asic_type < CHIP_BONAIRE) {
511 adev->doorbell.base = 0;
512 adev->doorbell.size = 0;
513 adev->doorbell.num_doorbells = 0;
514 adev->doorbell.ptr = NULL;
518 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
521 /* doorbell bar mapping */
522 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
523 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
525 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
526 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
527 if (adev->doorbell.num_doorbells == 0)
530 adev->doorbell.ptr = ioremap(adev->doorbell.base,
531 adev->doorbell.num_doorbells *
533 if (adev->doorbell.ptr == NULL)
540 * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
542 * @adev: amdgpu_device pointer
544 * Tear down doorbell driver information (CIK)
546 static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
548 iounmap(adev->doorbell.ptr);
549 adev->doorbell.ptr = NULL;
555 * amdgpu_device_wb_*()
556 * Writeback is the method by which the GPU updates special pages in memory
557 * with the status of certain GPU events (fences, ring pointers,etc.).
561 * amdgpu_device_wb_fini - Disable Writeback and free memory
563 * @adev: amdgpu_device pointer
565 * Disables Writeback and frees the Writeback memory (all asics).
566 * Used at driver shutdown.
568 static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
570 if (adev->wb.wb_obj) {
571 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
573 (void **)&adev->wb.wb);
574 adev->wb.wb_obj = NULL;
579 * amdgpu_device_wb_init- Init Writeback driver info and allocate memory
581 * @adev: amdgpu_device pointer
583 * Initializes writeback and allocates writeback memory (all asics).
584 * Used at driver startup.
585 * Returns 0 on success or an -error on failure.
587 static int amdgpu_device_wb_init(struct amdgpu_device *adev)
591 if (adev->wb.wb_obj == NULL) {
592 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
593 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
594 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
595 &adev->wb.wb_obj, &adev->wb.gpu_addr,
596 (void **)&adev->wb.wb);
598 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
602 adev->wb.num_wb = AMDGPU_MAX_WB;
603 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
605 /* clear wb memory */
606 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
613 * amdgpu_device_wb_get - Allocate a wb entry
615 * @adev: amdgpu_device pointer
618 * Allocate a wb slot for use by the driver (all asics).
619 * Returns 0 on success or -EINVAL on failure.
621 int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
623 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
625 if (offset < adev->wb.num_wb) {
626 __set_bit(offset, adev->wb.used);
627 *wb = offset << 3; /* convert to dw offset */
635 * amdgpu_device_wb_free - Free a wb entry
637 * @adev: amdgpu_device pointer
640 * Free a wb slot allocated for use by the driver (all asics)
642 void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
645 if (wb < adev->wb.num_wb)
646 __clear_bit(wb, adev->wb.used);
650 * amdgpu_device_vram_location - try to find VRAM location
652 * @adev: amdgpu device structure holding all necessary informations
653 * @mc: memory controller structure holding memory informations
654 * @base: base address at which to put VRAM
656 * Function will try to place VRAM at base address provided
659 void amdgpu_device_vram_location(struct amdgpu_device *adev,
660 struct amdgpu_gmc *mc, u64 base)
662 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
664 mc->vram_start = base;
665 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
666 if (limit && limit < mc->real_vram_size)
667 mc->real_vram_size = limit;
668 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
669 mc->mc_vram_size >> 20, mc->vram_start,
670 mc->vram_end, mc->real_vram_size >> 20);
674 * amdgpu_device_gart_location - try to find GTT location
676 * @adev: amdgpu device structure holding all necessary informations
677 * @mc: memory controller structure holding memory informations
679 * Function will place try to place GTT before or after VRAM.
681 * If GTT size is bigger than space left then we ajust GTT size.
682 * Thus function will never fails.
684 * FIXME: when reducing GTT size align new size on power of 2.
686 void amdgpu_device_gart_location(struct amdgpu_device *adev,
687 struct amdgpu_gmc *mc)
689 u64 size_af, size_bf;
691 size_af = adev->gmc.mc_mask - mc->vram_end;
692 size_bf = mc->vram_start;
693 if (size_bf > size_af) {
694 if (mc->gart_size > size_bf) {
695 dev_warn(adev->dev, "limiting GTT\n");
696 mc->gart_size = size_bf;
700 if (mc->gart_size > size_af) {
701 dev_warn(adev->dev, "limiting GTT\n");
702 mc->gart_size = size_af;
704 /* VCE doesn't like it when BOs cross a 4GB segment, so align
705 * the GART base on a 4GB boundary as well.
707 mc->gart_start = ALIGN(mc->vram_end + 1, 0x100000000ULL);
709 mc->gart_end = mc->gart_start + mc->gart_size - 1;
710 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
711 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
715 * amdgpu_device_resize_fb_bar - try to resize FB BAR
717 * @adev: amdgpu_device pointer
719 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
720 * to fail, but if any of the BARs is not accessible after the size we abort
721 * driver loading by returning -ENODEV.
723 int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
725 u64 space_needed = roundup_pow_of_two(adev->gmc.real_vram_size);
726 u32 rbar_size = order_base_2(((space_needed >> 20) | 1)) - 1;
727 struct pci_bus *root;
728 struct resource *res;
734 if (amdgpu_sriov_vf(adev))
737 /* Check if the root BUS has 64bit memory resources */
738 root = adev->pdev->bus;
742 pci_bus_for_each_resource(root, res, i) {
743 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
744 res->start > 0x100000000ull)
748 /* Trying to resize is pointless without a root hub window above 4GB */
752 /* Disable memory decoding while we change the BAR addresses and size */
753 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
754 pci_write_config_word(adev->pdev, PCI_COMMAND,
755 cmd & ~PCI_COMMAND_MEMORY);
757 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
758 amdgpu_device_doorbell_fini(adev);
759 if (adev->asic_type >= CHIP_BONAIRE)
760 pci_release_resource(adev->pdev, 2);
762 pci_release_resource(adev->pdev, 0);
764 r = pci_resize_resource(adev->pdev, 0, rbar_size);
766 DRM_INFO("Not enough PCI address space for a large BAR.");
767 else if (r && r != -ENOTSUPP)
768 DRM_ERROR("Problem resizing BAR0 (%d).", r);
770 pci_assign_unassigned_bus_resources(adev->pdev->bus);
772 /* When the doorbell or fb BAR isn't available we have no chance of
775 r = amdgpu_device_doorbell_init(adev);
776 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
779 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
785 * GPU helpers function.
788 * amdgpu_device_need_post - check if the hw need post or not
790 * @adev: amdgpu_device pointer
792 * Check if the asic has been initialized (all asics) at driver startup
793 * or post is needed if hw reset is performed.
794 * Returns true if need or false if not.
796 bool amdgpu_device_need_post(struct amdgpu_device *adev)
800 if (amdgpu_sriov_vf(adev))
803 if (amdgpu_passthrough(adev)) {
804 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
805 * some old smc fw still need driver do vPost otherwise gpu hang, while
806 * those smc fw version above 22.15 doesn't have this flaw, so we force
807 * vpost executed for smc version below 22.15
809 if (adev->asic_type == CHIP_FIJI) {
812 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
813 /* force vPost if error occured */
817 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
818 if (fw_ver < 0x00160e00)
823 if (adev->has_hw_reset) {
824 adev->has_hw_reset = false;
828 /* bios scratch used on CIK+ */
829 if (adev->asic_type >= CHIP_BONAIRE)
830 return amdgpu_atombios_scratch_need_asic_init(adev);
832 /* check MEM_SIZE for older asics */
833 reg = amdgpu_asic_get_config_memsize(adev);
835 if ((reg != 0) && (reg != 0xffffffff))
841 /* if we get transitioned to only one device, take VGA back */
843 * amdgpu_device_vga_set_decode - enable/disable vga decode
845 * @cookie: amdgpu_device pointer
846 * @state: enable/disable vga decode
848 * Enable/disable vga decode (all asics).
849 * Returns VGA resource flags.
851 static unsigned int amdgpu_device_vga_set_decode(void *cookie, bool state)
853 struct amdgpu_device *adev = cookie;
854 amdgpu_asic_set_vga_state(adev, state);
856 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
857 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
859 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
863 * amdgpu_device_check_block_size - validate the vm block size
865 * @adev: amdgpu_device pointer
867 * Validates the vm block size specified via module parameter.
868 * The vm block size defines number of bits in page table versus page directory,
869 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
870 * page table and the remaining bits are in the page directory.
872 static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
874 /* defines number of bits in page table versus page directory,
875 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
876 * page table and the remaining bits are in the page directory */
877 if (amdgpu_vm_block_size == -1)
880 if (amdgpu_vm_block_size < 9) {
881 dev_warn(adev->dev, "VM page table size (%d) too small\n",
882 amdgpu_vm_block_size);
883 amdgpu_vm_block_size = -1;
888 * amdgpu_device_check_vm_size - validate the vm size
890 * @adev: amdgpu_device pointer
892 * Validates the vm size in GB specified via module parameter.
893 * The VM size is the size of the GPU virtual memory space in GB.
895 static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
897 /* no need to check the default value */
898 if (amdgpu_vm_size == -1)
901 if (amdgpu_vm_size < 1) {
902 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
909 * amdgpu_device_check_arguments - validate module params
911 * @adev: amdgpu_device pointer
913 * Validates certain module parameters and updates
914 * the associated values used by the driver (all asics).
916 static void amdgpu_device_check_arguments(struct amdgpu_device *adev)
918 if (amdgpu_sched_jobs < 4) {
919 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
921 amdgpu_sched_jobs = 4;
922 } else if (!is_power_of_2(amdgpu_sched_jobs)){
923 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
925 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
928 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
929 /* gart size must be greater or equal to 32M */
930 dev_warn(adev->dev, "gart size (%d) too small\n",
932 amdgpu_gart_size = -1;
935 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
936 /* gtt size must be greater or equal to 32M */
937 dev_warn(adev->dev, "gtt size (%d) too small\n",
939 amdgpu_gtt_size = -1;
942 /* valid range is between 4 and 9 inclusive */
943 if (amdgpu_vm_fragment_size != -1 &&
944 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
945 dev_warn(adev->dev, "valid range is between 4 and 9\n");
946 amdgpu_vm_fragment_size = -1;
949 amdgpu_device_check_vm_size(adev);
951 amdgpu_device_check_block_size(adev);
953 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
954 !is_power_of_2(amdgpu_vram_page_split))) {
955 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
956 amdgpu_vram_page_split);
957 amdgpu_vram_page_split = 1024;
960 if (amdgpu_lockup_timeout == 0) {
961 dev_warn(adev->dev, "lockup_timeout msut be > 0, adjusting to 10000\n");
962 amdgpu_lockup_timeout = 10000;
965 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
969 * amdgpu_switcheroo_set_state - set switcheroo state
971 * @pdev: pci dev pointer
972 * @state: vga_switcheroo state
974 * Callback for the switcheroo driver. Suspends or resumes the
975 * the asics before or after it is powered up using ACPI methods.
977 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
979 struct drm_device *dev = pci_get_drvdata(pdev);
981 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
984 if (state == VGA_SWITCHEROO_ON) {
985 pr_info("amdgpu: switched on\n");
986 /* don't suspend or resume card normally */
987 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
989 amdgpu_device_resume(dev, true, true);
991 dev->switch_power_state = DRM_SWITCH_POWER_ON;
992 drm_kms_helper_poll_enable(dev);
994 pr_info("amdgpu: switched off\n");
995 drm_kms_helper_poll_disable(dev);
996 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
997 amdgpu_device_suspend(dev, true, true);
998 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1003 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1005 * @pdev: pci dev pointer
1007 * Callback for the switcheroo driver. Check of the switcheroo
1008 * state can be changed.
1009 * Returns true if the state can be changed, false if not.
1011 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1013 struct drm_device *dev = pci_get_drvdata(pdev);
1016 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1017 * locking inversion with the driver load path. And the access here is
1018 * completely racy anyway. So don't bother with locking for now.
1020 return dev->open_count == 0;
1023 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1024 .set_gpu_state = amdgpu_switcheroo_set_state,
1026 .can_switch = amdgpu_switcheroo_can_switch,
1030 * amdgpu_device_ip_set_clockgating_state - set the CG state
1032 * @adev: amdgpu_device pointer
1033 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1034 * @state: clockgating state (gate or ungate)
1036 * Sets the requested clockgating state for all instances of
1037 * the hardware IP specified.
1038 * Returns the error code from the last instance.
1040 int amdgpu_device_ip_set_clockgating_state(struct amdgpu_device *adev,
1041 enum amd_ip_block_type block_type,
1042 enum amd_clockgating_state state)
1046 for (i = 0; i < adev->num_ip_blocks; i++) {
1047 if (!adev->ip_blocks[i].status.valid)
1049 if (adev->ip_blocks[i].version->type != block_type)
1051 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1053 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1054 (void *)adev, state);
1056 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1057 adev->ip_blocks[i].version->funcs->name, r);
1063 * amdgpu_device_ip_set_powergating_state - set the PG state
1065 * @adev: amdgpu_device pointer
1066 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1067 * @state: powergating state (gate or ungate)
1069 * Sets the requested powergating state for all instances of
1070 * the hardware IP specified.
1071 * Returns the error code from the last instance.
1073 int amdgpu_device_ip_set_powergating_state(struct amdgpu_device *adev,
1074 enum amd_ip_block_type block_type,
1075 enum amd_powergating_state state)
1079 for (i = 0; i < adev->num_ip_blocks; i++) {
1080 if (!adev->ip_blocks[i].status.valid)
1082 if (adev->ip_blocks[i].version->type != block_type)
1084 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1086 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1087 (void *)adev, state);
1089 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1090 adev->ip_blocks[i].version->funcs->name, r);
1096 * amdgpu_device_ip_get_clockgating_state - get the CG state
1098 * @adev: amdgpu_device pointer
1099 * @flags: clockgating feature flags
1101 * Walks the list of IPs on the device and updates the clockgating
1102 * flags for each IP.
1103 * Updates @flags with the feature flags for each hardware IP where
1104 * clockgating is enabled.
1106 void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
1111 for (i = 0; i < adev->num_ip_blocks; i++) {
1112 if (!adev->ip_blocks[i].status.valid)
1114 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1115 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1120 * amdgpu_device_ip_wait_for_idle - wait for idle
1122 * @adev: amdgpu_device pointer
1123 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1125 * Waits for the request hardware IP to be idle.
1126 * Returns 0 for success or a negative error code on failure.
1128 int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1129 enum amd_ip_block_type block_type)
1133 for (i = 0; i < adev->num_ip_blocks; i++) {
1134 if (!adev->ip_blocks[i].status.valid)
1136 if (adev->ip_blocks[i].version->type == block_type) {
1137 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
1148 * amdgpu_device_ip_is_idle - is the hardware IP idle
1150 * @adev: amdgpu_device pointer
1151 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1153 * Check if the hardware IP is idle or not.
1154 * Returns true if it the IP is idle, false if not.
1156 bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1157 enum amd_ip_block_type block_type)
1161 for (i = 0; i < adev->num_ip_blocks; i++) {
1162 if (!adev->ip_blocks[i].status.valid)
1164 if (adev->ip_blocks[i].version->type == block_type)
1165 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
1172 * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1174 * @adev: amdgpu_device pointer
1175 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1177 * Returns a pointer to the hardware IP block structure
1178 * if it exists for the asic, otherwise NULL.
1180 struct amdgpu_ip_block *
1181 amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1182 enum amd_ip_block_type type)
1186 for (i = 0; i < adev->num_ip_blocks; i++)
1187 if (adev->ip_blocks[i].version->type == type)
1188 return &adev->ip_blocks[i];
1194 * amdgpu_device_ip_block_version_cmp
1196 * @adev: amdgpu_device pointer
1197 * @type: enum amd_ip_block_type
1198 * @major: major version
1199 * @minor: minor version
1201 * return 0 if equal or greater
1202 * return 1 if smaller or the ip_block doesn't exist
1204 int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1205 enum amd_ip_block_type type,
1206 u32 major, u32 minor)
1208 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
1210 if (ip_block && ((ip_block->version->major > major) ||
1211 ((ip_block->version->major == major) &&
1212 (ip_block->version->minor >= minor))))
1219 * amdgpu_device_ip_block_add
1221 * @adev: amdgpu_device pointer
1222 * @ip_block_version: pointer to the IP to add
1224 * Adds the IP block driver information to the collection of IPs
1227 int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1228 const struct amdgpu_ip_block_version *ip_block_version)
1230 if (!ip_block_version)
1233 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
1234 ip_block_version->funcs->name);
1236 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1242 * amdgpu_device_enable_virtual_display - enable virtual display feature
1244 * @adev: amdgpu_device pointer
1246 * Enabled the virtual display feature if the user has enabled it via
1247 * the module parameter virtual_display. This feature provides a virtual
1248 * display hardware on headless boards or in virtualized environments.
1249 * This function parses and validates the configuration string specified by
1250 * the user and configues the virtual display configuration (number of
1251 * virtual connectors, crtcs, etc.) specified.
1253 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
1255 adev->enable_virtual_display = false;
1257 if (amdgpu_virtual_display) {
1258 struct drm_device *ddev = adev->ddev;
1259 const char *pci_address_name = pci_name(ddev->pdev);
1260 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
1262 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1263 pciaddstr_tmp = pciaddstr;
1264 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1265 pciaddname = strsep(&pciaddname_tmp, ",");
1266 if (!strcmp("all", pciaddname)
1267 || !strcmp(pci_address_name, pciaddname)) {
1271 adev->enable_virtual_display = true;
1274 res = kstrtol(pciaddname_tmp, 10,
1282 adev->mode_info.num_crtc = num_crtc;
1284 adev->mode_info.num_crtc = 1;
1290 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1291 amdgpu_virtual_display, pci_address_name,
1292 adev->enable_virtual_display, adev->mode_info.num_crtc);
1299 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
1301 * @adev: amdgpu_device pointer
1303 * Parses the asic configuration parameters specified in the gpu info
1304 * firmware and makes them availale to the driver for use in configuring
1306 * Returns 0 on success, -EINVAL on failure.
1308 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1310 const char *chip_name;
1313 const struct gpu_info_firmware_header_v1_0 *hdr;
1315 adev->firmware.gpu_info_fw = NULL;
1317 switch (adev->asic_type) {
1321 case CHIP_POLARIS11:
1322 case CHIP_POLARIS10:
1323 case CHIP_POLARIS12:
1326 #ifdef CONFIG_DRM_AMDGPU_SI
1333 #ifdef CONFIG_DRM_AMDGPU_CIK
1343 chip_name = "vega10";
1346 chip_name = "raven";
1350 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
1351 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
1354 "Failed to load gpu_info firmware \"%s\"\n",
1358 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
1361 "Failed to validate gpu_info firmware \"%s\"\n",
1366 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
1367 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1369 switch (hdr->version_major) {
1372 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
1373 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
1374 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1376 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1377 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1378 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1379 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
1380 adev->gfx.config.max_texture_channel_caches =
1381 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1382 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1383 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1384 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1385 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
1386 adev->gfx.config.double_offchip_lds_buf =
1387 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1388 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
1389 adev->gfx.cu_info.max_waves_per_simd =
1390 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1391 adev->gfx.cu_info.max_scratch_slots_per_cu =
1392 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1393 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
1398 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1407 * amdgpu_device_ip_early_init - run early init for hardware IPs
1409 * @adev: amdgpu_device pointer
1411 * Early initialization pass for hardware IPs. The hardware IPs that make
1412 * up each asic are discovered each IP's early_init callback is run. This
1413 * is the first stage in initializing the asic.
1414 * Returns 0 on success, negative error code on failure.
1416 static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
1420 amdgpu_device_enable_virtual_display(adev);
1422 switch (adev->asic_type) {
1426 case CHIP_POLARIS11:
1427 case CHIP_POLARIS10:
1428 case CHIP_POLARIS12:
1431 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
1432 adev->family = AMDGPU_FAMILY_CZ;
1434 adev->family = AMDGPU_FAMILY_VI;
1436 r = vi_set_ip_blocks(adev);
1440 #ifdef CONFIG_DRM_AMDGPU_SI
1446 adev->family = AMDGPU_FAMILY_SI;
1447 r = si_set_ip_blocks(adev);
1452 #ifdef CONFIG_DRM_AMDGPU_CIK
1458 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1459 adev->family = AMDGPU_FAMILY_CI;
1461 adev->family = AMDGPU_FAMILY_KV;
1463 r = cik_set_ip_blocks(adev);
1470 if (adev->asic_type == CHIP_RAVEN)
1471 adev->family = AMDGPU_FAMILY_RV;
1473 adev->family = AMDGPU_FAMILY_AI;
1475 r = soc15_set_ip_blocks(adev);
1480 /* FIXME: not supported yet */
1484 r = amdgpu_device_parse_gpu_info_fw(adev);
1488 amdgpu_amdkfd_device_probe(adev);
1490 if (amdgpu_sriov_vf(adev)) {
1491 r = amdgpu_virt_request_full_gpu(adev, true);
1496 for (i = 0; i < adev->num_ip_blocks; i++) {
1497 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1498 DRM_ERROR("disabled ip block: %d <%s>\n",
1499 i, adev->ip_blocks[i].version->funcs->name);
1500 adev->ip_blocks[i].status.valid = false;
1502 if (adev->ip_blocks[i].version->funcs->early_init) {
1503 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
1505 adev->ip_blocks[i].status.valid = false;
1507 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1508 adev->ip_blocks[i].version->funcs->name, r);
1511 adev->ip_blocks[i].status.valid = true;
1514 adev->ip_blocks[i].status.valid = true;
1519 adev->cg_flags &= amdgpu_cg_mask;
1520 adev->pg_flags &= amdgpu_pg_mask;
1526 * amdgpu_device_ip_init - run init for hardware IPs
1528 * @adev: amdgpu_device pointer
1530 * Main initialization pass for hardware IPs. The list of all the hardware
1531 * IPs that make up the asic is walked and the sw_init and hw_init callbacks
1532 * are run. sw_init initializes the software state associated with each IP
1533 * and hw_init initializes the hardware associated with each IP.
1534 * Returns 0 on success, negative error code on failure.
1536 static int amdgpu_device_ip_init(struct amdgpu_device *adev)
1540 for (i = 0; i < adev->num_ip_blocks; i++) {
1541 if (!adev->ip_blocks[i].status.valid)
1543 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
1545 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1546 adev->ip_blocks[i].version->funcs->name, r);
1549 adev->ip_blocks[i].status.sw = true;
1551 /* need to do gmc hw init early so we can allocate gpu mem */
1552 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1553 r = amdgpu_device_vram_scratch_init(adev);
1555 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
1558 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1560 DRM_ERROR("hw_init %d failed %d\n", i, r);
1563 r = amdgpu_device_wb_init(adev);
1565 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
1568 adev->ip_blocks[i].status.hw = true;
1570 /* right after GMC hw init, we create CSA */
1571 if (amdgpu_sriov_vf(adev)) {
1572 r = amdgpu_allocate_static_csa(adev);
1574 DRM_ERROR("allocate CSA failed %d\n", r);
1581 for (i = 0; i < adev->num_ip_blocks; i++) {
1582 if (!adev->ip_blocks[i].status.sw)
1584 if (adev->ip_blocks[i].status.hw)
1586 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1588 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1589 adev->ip_blocks[i].version->funcs->name, r);
1592 adev->ip_blocks[i].status.hw = true;
1595 amdgpu_amdkfd_device_init(adev);
1597 if (amdgpu_sriov_vf(adev))
1598 amdgpu_virt_release_full_gpu(adev, true);
1604 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
1606 * @adev: amdgpu_device pointer
1608 * Writes a reset magic value to the gart pointer in VRAM. The driver calls
1609 * this function before a GPU reset. If the value is retained after a
1610 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
1612 static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
1614 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1618 * amdgpu_device_check_vram_lost - check if vram is valid
1620 * @adev: amdgpu_device pointer
1622 * Checks the reset magic value written to the gart pointer in VRAM.
1623 * The driver calls this after a GPU reset to see if the contents of
1624 * VRAM is lost or now.
1625 * returns true if vram is lost, false if not.
1627 static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
1629 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1630 AMDGPU_RESET_MAGIC_NUM);
1634 * amdgpu_device_ip_late_set_cg_state - late init for clockgating
1636 * @adev: amdgpu_device pointer
1638 * Late initialization pass enabling clockgating for hardware IPs.
1639 * The list of all the hardware IPs that make up the asic is walked and the
1640 * set_clockgating_state callbacks are run. This stage is run late
1641 * in the init process.
1642 * Returns 0 on success, negative error code on failure.
1644 static int amdgpu_device_ip_late_set_cg_state(struct amdgpu_device *adev)
1648 if (amdgpu_emu_mode == 1)
1651 for (i = 0; i < adev->num_ip_blocks; i++) {
1652 if (!adev->ip_blocks[i].status.valid)
1654 /* skip CG for VCE/UVD, it's handled specially */
1655 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1656 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1657 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
1658 /* enable clockgating to save power */
1659 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1662 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1663 adev->ip_blocks[i].version->funcs->name, r);
1672 * amdgpu_device_ip_late_init - run late init for hardware IPs
1674 * @adev: amdgpu_device pointer
1676 * Late initialization pass for hardware IPs. The list of all the hardware
1677 * IPs that make up the asic is walked and the late_init callbacks are run.
1678 * late_init covers any special initialization that an IP requires
1679 * after all of the have been initialized or something that needs to happen
1680 * late in the init process.
1681 * Returns 0 on success, negative error code on failure.
1683 static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
1687 for (i = 0; i < adev->num_ip_blocks; i++) {
1688 if (!adev->ip_blocks[i].status.valid)
1690 if (adev->ip_blocks[i].version->funcs->late_init) {
1691 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
1693 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1694 adev->ip_blocks[i].version->funcs->name, r);
1697 adev->ip_blocks[i].status.late_initialized = true;
1701 mod_delayed_work(system_wq, &adev->late_init_work,
1702 msecs_to_jiffies(AMDGPU_RESUME_MS));
1704 amdgpu_device_fill_reset_magic(adev);
1710 * amdgpu_device_ip_fini - run fini for hardware IPs
1712 * @adev: amdgpu_device pointer
1714 * Main teardown pass for hardware IPs. The list of all the hardware
1715 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
1716 * are run. hw_fini tears down the hardware associated with each IP
1717 * and sw_fini tears down any software state associated with each IP.
1718 * Returns 0 on success, negative error code on failure.
1720 static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
1724 amdgpu_amdkfd_device_fini(adev);
1725 /* need to disable SMC first */
1726 for (i = 0; i < adev->num_ip_blocks; i++) {
1727 if (!adev->ip_blocks[i].status.hw)
1729 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC &&
1730 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
1731 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1732 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1733 AMD_CG_STATE_UNGATE);
1735 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1736 adev->ip_blocks[i].version->funcs->name, r);
1739 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
1740 /* XXX handle errors */
1742 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1743 adev->ip_blocks[i].version->funcs->name, r);
1745 adev->ip_blocks[i].status.hw = false;
1750 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1751 if (!adev->ip_blocks[i].status.hw)
1754 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1755 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1756 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
1757 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1758 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1759 AMD_CG_STATE_UNGATE);
1761 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1762 adev->ip_blocks[i].version->funcs->name, r);
1767 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
1768 /* XXX handle errors */
1770 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1771 adev->ip_blocks[i].version->funcs->name, r);
1774 adev->ip_blocks[i].status.hw = false;
1778 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1779 if (!adev->ip_blocks[i].status.sw)
1782 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1783 amdgpu_free_static_csa(adev);
1784 amdgpu_device_wb_fini(adev);
1785 amdgpu_device_vram_scratch_fini(adev);
1788 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
1789 /* XXX handle errors */
1791 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1792 adev->ip_blocks[i].version->funcs->name, r);
1794 adev->ip_blocks[i].status.sw = false;
1795 adev->ip_blocks[i].status.valid = false;
1798 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1799 if (!adev->ip_blocks[i].status.late_initialized)
1801 if (adev->ip_blocks[i].version->funcs->late_fini)
1802 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1803 adev->ip_blocks[i].status.late_initialized = false;
1806 if (amdgpu_sriov_vf(adev))
1807 if (amdgpu_virt_release_full_gpu(adev, false))
1808 DRM_ERROR("failed to release exclusive mode on fini\n");
1814 * amdgpu_device_ip_late_init_func_handler - work handler for clockgating
1816 * @work: work_struct
1818 * Work handler for amdgpu_device_ip_late_set_cg_state. We put the
1819 * clockgating setup into a worker thread to speed up driver init and
1820 * resume from suspend.
1822 static void amdgpu_device_ip_late_init_func_handler(struct work_struct *work)
1824 struct amdgpu_device *adev =
1825 container_of(work, struct amdgpu_device, late_init_work.work);
1826 amdgpu_device_ip_late_set_cg_state(adev);
1830 * amdgpu_device_ip_suspend - run suspend for hardware IPs
1832 * @adev: amdgpu_device pointer
1834 * Main suspend function for hardware IPs. The list of all the hardware
1835 * IPs that make up the asic is walked, clockgating is disabled and the
1836 * suspend callbacks are run. suspend puts the hardware and software state
1837 * in each IP into a state suitable for suspend.
1838 * Returns 0 on success, negative error code on failure.
1840 int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
1844 if (amdgpu_sriov_vf(adev))
1845 amdgpu_virt_request_full_gpu(adev, false);
1847 /* ungate SMC block first */
1848 r = amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1849 AMD_CG_STATE_UNGATE);
1851 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n", r);
1854 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1855 if (!adev->ip_blocks[i].status.valid)
1857 /* ungate blocks so that suspend can properly shut them down */
1858 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_SMC &&
1859 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
1860 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1861 AMD_CG_STATE_UNGATE);
1863 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1864 adev->ip_blocks[i].version->funcs->name, r);
1867 /* XXX handle errors */
1868 r = adev->ip_blocks[i].version->funcs->suspend(adev);
1869 /* XXX handle errors */
1871 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1872 adev->ip_blocks[i].version->funcs->name, r);
1876 if (amdgpu_sriov_vf(adev))
1877 amdgpu_virt_release_full_gpu(adev, false);
1882 static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
1886 static enum amd_ip_block_type ip_order[] = {
1887 AMD_IP_BLOCK_TYPE_GMC,
1888 AMD_IP_BLOCK_TYPE_COMMON,
1889 AMD_IP_BLOCK_TYPE_IH,
1892 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1894 struct amdgpu_ip_block *block;
1896 for (j = 0; j < adev->num_ip_blocks; j++) {
1897 block = &adev->ip_blocks[j];
1899 if (block->version->type != ip_order[i] ||
1900 !block->status.valid)
1903 r = block->version->funcs->hw_init(adev);
1904 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
1913 static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
1917 static enum amd_ip_block_type ip_order[] = {
1918 AMD_IP_BLOCK_TYPE_SMC,
1919 AMD_IP_BLOCK_TYPE_PSP,
1920 AMD_IP_BLOCK_TYPE_DCE,
1921 AMD_IP_BLOCK_TYPE_GFX,
1922 AMD_IP_BLOCK_TYPE_SDMA,
1923 AMD_IP_BLOCK_TYPE_UVD,
1924 AMD_IP_BLOCK_TYPE_VCE
1927 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1929 struct amdgpu_ip_block *block;
1931 for (j = 0; j < adev->num_ip_blocks; j++) {
1932 block = &adev->ip_blocks[j];
1934 if (block->version->type != ip_order[i] ||
1935 !block->status.valid)
1938 r = block->version->funcs->hw_init(adev);
1939 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
1949 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
1951 * @adev: amdgpu_device pointer
1953 * First resume function for hardware IPs. The list of all the hardware
1954 * IPs that make up the asic is walked and the resume callbacks are run for
1955 * COMMON, GMC, and IH. resume puts the hardware into a functional state
1956 * after a suspend and updates the software state as necessary. This
1957 * function is also used for restoring the GPU after a GPU reset.
1958 * Returns 0 on success, negative error code on failure.
1960 static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
1964 for (i = 0; i < adev->num_ip_blocks; i++) {
1965 if (!adev->ip_blocks[i].status.valid)
1967 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1968 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1969 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
1970 r = adev->ip_blocks[i].version->funcs->resume(adev);
1972 DRM_ERROR("resume of IP block <%s> failed %d\n",
1973 adev->ip_blocks[i].version->funcs->name, r);
1983 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
1985 * @adev: amdgpu_device pointer
1987 * First resume function for hardware IPs. The list of all the hardware
1988 * IPs that make up the asic is walked and the resume callbacks are run for
1989 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
1990 * functional state after a suspend and updates the software state as
1991 * necessary. This function is also used for restoring the GPU after a GPU
1993 * Returns 0 on success, negative error code on failure.
1995 static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
1999 for (i = 0; i < adev->num_ip_blocks; i++) {
2000 if (!adev->ip_blocks[i].status.valid)
2002 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2003 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2004 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)
2006 r = adev->ip_blocks[i].version->funcs->resume(adev);
2008 DRM_ERROR("resume of IP block <%s> failed %d\n",
2009 adev->ip_blocks[i].version->funcs->name, r);
2018 * amdgpu_device_ip_resume - run resume for hardware IPs
2020 * @adev: amdgpu_device pointer
2022 * Main resume function for hardware IPs. The hardware IPs
2023 * are split into two resume functions because they are
2024 * are also used in in recovering from a GPU reset and some additional
2025 * steps need to be take between them. In this case (S3/S4) they are
2027 * Returns 0 on success, negative error code on failure.
2029 static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
2033 r = amdgpu_device_ip_resume_phase1(adev);
2036 r = amdgpu_device_ip_resume_phase2(adev);
2042 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
2044 * @adev: amdgpu_device pointer
2046 * Query the VBIOS data tables to determine if the board supports SR-IOV.
2048 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
2050 if (amdgpu_sriov_vf(adev)) {
2051 if (adev->is_atom_fw) {
2052 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2053 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2055 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2056 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2059 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
2060 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
2065 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
2067 * @asic_type: AMD asic type
2069 * Check if there is DC (new modesetting infrastructre) support for an asic.
2070 * returns true if DC has support, false if not.
2072 bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
2074 switch (asic_type) {
2075 #if defined(CONFIG_DRM_AMD_DC)
2083 case CHIP_POLARIS11:
2084 case CHIP_POLARIS10:
2085 case CHIP_POLARIS12:
2088 #if defined(CONFIG_DRM_AMD_DC_PRE_VEGA)
2089 return amdgpu_dc != 0;
2092 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
2095 return amdgpu_dc != 0;
2103 * amdgpu_device_has_dc_support - check if dc is supported
2105 * @adev: amdgpu_device_pointer
2107 * Returns true for supported, false for not supported
2109 bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
2111 if (amdgpu_sriov_vf(adev))
2114 return amdgpu_device_asic_has_dc_support(adev->asic_type);
2118 * amdgpu_device_init - initialize the driver
2120 * @adev: amdgpu_device pointer
2121 * @pdev: drm dev pointer
2122 * @pdev: pci dev pointer
2123 * @flags: driver flags
2125 * Initializes the driver info and hw (all asics).
2126 * Returns 0 for success or an error on failure.
2127 * Called at driver startup.
2129 int amdgpu_device_init(struct amdgpu_device *adev,
2130 struct drm_device *ddev,
2131 struct pci_dev *pdev,
2135 bool runtime = false;
2138 adev->shutdown = false;
2139 adev->dev = &pdev->dev;
2142 adev->flags = flags;
2143 adev->asic_type = flags & AMD_ASIC_MASK;
2144 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
2145 if (amdgpu_emu_mode == 1)
2146 adev->usec_timeout *= 2;
2147 adev->gmc.gart_size = 512 * 1024 * 1024;
2148 adev->accel_working = false;
2149 adev->num_rings = 0;
2150 adev->mman.buffer_funcs = NULL;
2151 adev->mman.buffer_funcs_ring = NULL;
2152 adev->vm_manager.vm_pte_funcs = NULL;
2153 adev->vm_manager.vm_pte_num_rings = 0;
2154 adev->gmc.gmc_funcs = NULL;
2155 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
2156 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
2158 adev->smc_rreg = &amdgpu_invalid_rreg;
2159 adev->smc_wreg = &amdgpu_invalid_wreg;
2160 adev->pcie_rreg = &amdgpu_invalid_rreg;
2161 adev->pcie_wreg = &amdgpu_invalid_wreg;
2162 adev->pciep_rreg = &amdgpu_invalid_rreg;
2163 adev->pciep_wreg = &amdgpu_invalid_wreg;
2164 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2165 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2166 adev->didt_rreg = &amdgpu_invalid_rreg;
2167 adev->didt_wreg = &amdgpu_invalid_wreg;
2168 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2169 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
2170 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2171 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2173 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2174 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2175 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
2177 /* mutex initialization are all done here so we
2178 * can recall function without having locking issues */
2179 atomic_set(&adev->irq.ih.lock, 0);
2180 mutex_init(&adev->firmware.mutex);
2181 mutex_init(&adev->pm.mutex);
2182 mutex_init(&adev->gfx.gpu_clock_mutex);
2183 mutex_init(&adev->srbm_mutex);
2184 mutex_init(&adev->gfx.pipe_reserve_mutex);
2185 mutex_init(&adev->grbm_idx_mutex);
2186 mutex_init(&adev->mn_lock);
2187 mutex_init(&adev->virt.vf_errors.lock);
2188 hash_init(adev->mn_hash);
2189 mutex_init(&adev->lock_reset);
2191 amdgpu_device_check_arguments(adev);
2193 spin_lock_init(&adev->mmio_idx_lock);
2194 spin_lock_init(&adev->smc_idx_lock);
2195 spin_lock_init(&adev->pcie_idx_lock);
2196 spin_lock_init(&adev->uvd_ctx_idx_lock);
2197 spin_lock_init(&adev->didt_idx_lock);
2198 spin_lock_init(&adev->gc_cac_idx_lock);
2199 spin_lock_init(&adev->se_cac_idx_lock);
2200 spin_lock_init(&adev->audio_endpt_idx_lock);
2201 spin_lock_init(&adev->mm_stats.lock);
2203 INIT_LIST_HEAD(&adev->shadow_list);
2204 mutex_init(&adev->shadow_list_lock);
2206 INIT_LIST_HEAD(&adev->ring_lru_list);
2207 spin_lock_init(&adev->ring_lru_list_lock);
2209 INIT_DELAYED_WORK(&adev->late_init_work,
2210 amdgpu_device_ip_late_init_func_handler);
2212 /* Registers mapping */
2213 /* TODO: block userspace mapping of io register */
2214 if (adev->asic_type >= CHIP_BONAIRE) {
2215 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2216 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2218 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2219 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2222 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2223 if (adev->rmmio == NULL) {
2226 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2227 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2229 /* doorbell bar mapping */
2230 amdgpu_device_doorbell_init(adev);
2232 /* io port mapping */
2233 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2234 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2235 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2236 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2240 if (adev->rio_mem == NULL)
2241 DRM_INFO("PCI I/O BAR is not found.\n");
2243 amdgpu_device_get_pcie_info(adev);
2245 /* early init functions */
2246 r = amdgpu_device_ip_early_init(adev);
2250 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2251 /* this will fail for cards that aren't VGA class devices, just
2253 vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
2255 if (amdgpu_device_is_px(ddev))
2257 if (!pci_is_thunderbolt_attached(adev->pdev))
2258 vga_switcheroo_register_client(adev->pdev,
2259 &amdgpu_switcheroo_ops, runtime);
2261 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2263 if (amdgpu_emu_mode == 1) {
2264 /* post the asic on emulation mode */
2265 emu_soc_asic_init(adev);
2266 goto fence_driver_init;
2270 if (!amdgpu_get_bios(adev)) {
2275 r = amdgpu_atombios_init(adev);
2277 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
2278 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
2282 /* detect if we are with an SRIOV vbios */
2283 amdgpu_device_detect_sriov_bios(adev);
2285 /* Post card if necessary */
2286 if (amdgpu_device_need_post(adev)) {
2288 dev_err(adev->dev, "no vBIOS found\n");
2292 DRM_INFO("GPU posting now...\n");
2293 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2295 dev_err(adev->dev, "gpu post error!\n");
2300 if (adev->is_atom_fw) {
2301 /* Initialize clocks */
2302 r = amdgpu_atomfirmware_get_clock_info(adev);
2304 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
2305 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2309 /* Initialize clocks */
2310 r = amdgpu_atombios_get_clock_info(adev);
2312 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
2313 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2316 /* init i2c buses */
2317 if (!amdgpu_device_has_dc_support(adev))
2318 amdgpu_atombios_i2c_init(adev);
2323 r = amdgpu_fence_driver_init(adev);
2325 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
2326 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
2330 /* init the mode config */
2331 drm_mode_config_init(adev->ddev);
2333 r = amdgpu_device_ip_init(adev);
2335 /* failed in exclusive mode due to timeout */
2336 if (amdgpu_sriov_vf(adev) &&
2337 !amdgpu_sriov_runtime(adev) &&
2338 amdgpu_virt_mmio_blocked(adev) &&
2339 !amdgpu_virt_wait_reset(adev)) {
2340 dev_err(adev->dev, "VF exclusive mode timeout\n");
2341 /* Don't send request since VF is inactive. */
2342 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
2343 adev->virt.ops = NULL;
2347 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
2348 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
2349 amdgpu_device_ip_fini(adev);
2353 adev->accel_working = true;
2355 amdgpu_vm_check_compute_bug(adev);
2357 /* Initialize the buffer migration limit. */
2358 if (amdgpu_moverate >= 0)
2359 max_MBps = amdgpu_moverate;
2361 max_MBps = 8; /* Allow 8 MB/s. */
2362 /* Get a log2 for easy divisions. */
2363 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2365 r = amdgpu_ib_pool_init(adev);
2367 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
2368 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
2372 r = amdgpu_ib_ring_tests(adev);
2374 DRM_ERROR("ib ring test failed (%d).\n", r);
2376 if (amdgpu_sriov_vf(adev))
2377 amdgpu_virt_init_data_exchange(adev);
2379 amdgpu_fbdev_init(adev);
2381 r = amdgpu_pm_sysfs_init(adev);
2383 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2385 r = amdgpu_debugfs_gem_init(adev);
2387 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
2389 r = amdgpu_debugfs_regs_init(adev);
2391 DRM_ERROR("registering register debugfs failed (%d).\n", r);
2393 r = amdgpu_debugfs_firmware_init(adev);
2395 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
2397 r = amdgpu_debugfs_init(adev);
2399 DRM_ERROR("Creating debugfs files failed (%d).\n", r);
2401 if ((amdgpu_testing & 1)) {
2402 if (adev->accel_working)
2403 amdgpu_test_moves(adev);
2405 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2407 if (amdgpu_benchmarking) {
2408 if (adev->accel_working)
2409 amdgpu_benchmark(adev, amdgpu_benchmarking);
2411 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2414 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2415 * explicit gating rather than handling it automatically.
2417 r = amdgpu_device_ip_late_init(adev);
2419 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
2420 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
2427 amdgpu_vf_error_trans_all(adev);
2429 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2435 * amdgpu_device_fini - tear down the driver
2437 * @adev: amdgpu_device pointer
2439 * Tear down the driver info (all asics).
2440 * Called at driver shutdown.
2442 void amdgpu_device_fini(struct amdgpu_device *adev)
2446 DRM_INFO("amdgpu: finishing device.\n");
2447 adev->shutdown = true;
2448 /* disable all interrupts */
2449 amdgpu_irq_disable_all(adev);
2450 if (adev->mode_info.mode_config_initialized){
2451 if (!amdgpu_device_has_dc_support(adev))
2452 drm_crtc_force_disable_all(adev->ddev);
2454 drm_atomic_helper_shutdown(adev->ddev);
2456 amdgpu_ib_pool_fini(adev);
2457 amdgpu_fence_driver_fini(adev);
2458 amdgpu_pm_sysfs_fini(adev);
2459 amdgpu_fbdev_fini(adev);
2460 r = amdgpu_device_ip_fini(adev);
2461 if (adev->firmware.gpu_info_fw) {
2462 release_firmware(adev->firmware.gpu_info_fw);
2463 adev->firmware.gpu_info_fw = NULL;
2465 adev->accel_working = false;
2466 cancel_delayed_work_sync(&adev->late_init_work);
2467 /* free i2c buses */
2468 if (!amdgpu_device_has_dc_support(adev))
2469 amdgpu_i2c_fini(adev);
2471 if (amdgpu_emu_mode != 1)
2472 amdgpu_atombios_fini(adev);
2476 if (!pci_is_thunderbolt_attached(adev->pdev))
2477 vga_switcheroo_unregister_client(adev->pdev);
2478 if (adev->flags & AMD_IS_PX)
2479 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2480 vga_client_register(adev->pdev, NULL, NULL, NULL);
2482 pci_iounmap(adev->pdev, adev->rio_mem);
2483 adev->rio_mem = NULL;
2484 iounmap(adev->rmmio);
2486 amdgpu_device_doorbell_fini(adev);
2487 amdgpu_debugfs_regs_cleanup(adev);
2495 * amdgpu_device_suspend - initiate device suspend
2497 * @pdev: drm dev pointer
2498 * @state: suspend state
2500 * Puts the hw in the suspend state (all asics).
2501 * Returns 0 for success or an error on failure.
2502 * Called at driver suspend.
2504 int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
2506 struct amdgpu_device *adev;
2507 struct drm_crtc *crtc;
2508 struct drm_connector *connector;
2511 if (dev == NULL || dev->dev_private == NULL) {
2515 adev = dev->dev_private;
2517 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2520 drm_kms_helper_poll_disable(dev);
2522 if (!amdgpu_device_has_dc_support(adev)) {
2523 /* turn off display hw */
2524 drm_modeset_lock_all(dev);
2525 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2526 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2528 drm_modeset_unlock_all(dev);
2531 amdgpu_amdkfd_suspend(adev);
2533 /* unpin the front buffers and cursors */
2534 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2535 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2536 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2537 struct amdgpu_bo *robj;
2539 if (amdgpu_crtc->cursor_bo) {
2540 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2541 r = amdgpu_bo_reserve(aobj, true);
2543 amdgpu_bo_unpin(aobj);
2544 amdgpu_bo_unreserve(aobj);
2548 if (rfb == NULL || rfb->obj == NULL) {
2551 robj = gem_to_amdgpu_bo(rfb->obj);
2552 /* don't unpin kernel fb objects */
2553 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
2554 r = amdgpu_bo_reserve(robj, true);
2556 amdgpu_bo_unpin(robj);
2557 amdgpu_bo_unreserve(robj);
2561 /* evict vram memory */
2562 amdgpu_bo_evict_vram(adev);
2564 amdgpu_fence_driver_suspend(adev);
2566 r = amdgpu_device_ip_suspend(adev);
2568 /* evict remaining vram memory
2569 * This second call to evict vram is to evict the gart page table
2572 amdgpu_bo_evict_vram(adev);
2574 pci_save_state(dev->pdev);
2576 /* Shut down the device */
2577 pci_disable_device(dev->pdev);
2578 pci_set_power_state(dev->pdev, PCI_D3hot);
2580 r = amdgpu_asic_reset(adev);
2582 DRM_ERROR("amdgpu asic reset failed\n");
2587 amdgpu_fbdev_set_suspend(adev, 1);
2594 * amdgpu_device_resume - initiate device resume
2596 * @pdev: drm dev pointer
2598 * Bring the hw back to operating state (all asics).
2599 * Returns 0 for success or an error on failure.
2600 * Called at driver resume.
2602 int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
2604 struct drm_connector *connector;
2605 struct amdgpu_device *adev = dev->dev_private;
2606 struct drm_crtc *crtc;
2609 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2616 pci_set_power_state(dev->pdev, PCI_D0);
2617 pci_restore_state(dev->pdev);
2618 r = pci_enable_device(dev->pdev);
2624 if (amdgpu_device_need_post(adev)) {
2625 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2627 DRM_ERROR("amdgpu asic init failed\n");
2630 r = amdgpu_device_ip_resume(adev);
2632 DRM_ERROR("amdgpu_device_ip_resume failed (%d).\n", r);
2635 amdgpu_fence_driver_resume(adev);
2638 r = amdgpu_ib_ring_tests(adev);
2640 DRM_ERROR("ib ring test failed (%d).\n", r);
2643 r = amdgpu_device_ip_late_init(adev);
2648 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2649 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2651 if (amdgpu_crtc->cursor_bo) {
2652 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2653 r = amdgpu_bo_reserve(aobj, true);
2655 r = amdgpu_bo_pin(aobj,
2656 AMDGPU_GEM_DOMAIN_VRAM,
2657 &amdgpu_crtc->cursor_addr);
2659 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2660 amdgpu_bo_unreserve(aobj);
2664 r = amdgpu_amdkfd_resume(adev);
2668 /* blat the mode back in */
2670 if (!amdgpu_device_has_dc_support(adev)) {
2672 drm_helper_resume_force_mode(dev);
2674 /* turn on display hw */
2675 drm_modeset_lock_all(dev);
2676 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2677 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2679 drm_modeset_unlock_all(dev);
2683 drm_kms_helper_poll_enable(dev);
2686 * Most of the connector probing functions try to acquire runtime pm
2687 * refs to ensure that the GPU is powered on when connector polling is
2688 * performed. Since we're calling this from a runtime PM callback,
2689 * trying to acquire rpm refs will cause us to deadlock.
2691 * Since we're guaranteed to be holding the rpm lock, it's safe to
2692 * temporarily disable the rpm helpers so this doesn't deadlock us.
2695 dev->dev->power.disable_depth++;
2697 if (!amdgpu_device_has_dc_support(adev))
2698 drm_helper_hpd_irq_event(dev);
2700 drm_kms_helper_hotplug_event(dev);
2702 dev->dev->power.disable_depth--;
2706 amdgpu_fbdev_set_suspend(adev, 0);
2716 * amdgpu_device_ip_check_soft_reset - did soft reset succeed
2718 * @adev: amdgpu_device pointer
2720 * The list of all the hardware IPs that make up the asic is walked and
2721 * the check_soft_reset callbacks are run. check_soft_reset determines
2722 * if the asic is still hung or not.
2723 * Returns true if any of the IPs are still in a hung state, false if not.
2725 static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
2728 bool asic_hang = false;
2730 if (amdgpu_sriov_vf(adev))
2733 for (i = 0; i < adev->num_ip_blocks; i++) {
2734 if (!adev->ip_blocks[i].status.valid)
2736 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2737 adev->ip_blocks[i].status.hang =
2738 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2739 if (adev->ip_blocks[i].status.hang) {
2740 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
2748 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
2750 * @adev: amdgpu_device pointer
2752 * The list of all the hardware IPs that make up the asic is walked and the
2753 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
2754 * handles any IP specific hardware or software state changes that are
2755 * necessary for a soft reset to succeed.
2756 * Returns 0 on success, negative error code on failure.
2758 static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
2762 for (i = 0; i < adev->num_ip_blocks; i++) {
2763 if (!adev->ip_blocks[i].status.valid)
2765 if (adev->ip_blocks[i].status.hang &&
2766 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2767 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
2777 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
2779 * @adev: amdgpu_device pointer
2781 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
2782 * reset is necessary to recover.
2783 * Returns true if a full asic reset is required, false if not.
2785 static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
2789 for (i = 0; i < adev->num_ip_blocks; i++) {
2790 if (!adev->ip_blocks[i].status.valid)
2792 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2793 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2794 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2795 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
2796 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
2797 if (adev->ip_blocks[i].status.hang) {
2798 DRM_INFO("Some block need full reset!\n");
2807 * amdgpu_device_ip_soft_reset - do a soft reset
2809 * @adev: amdgpu_device pointer
2811 * The list of all the hardware IPs that make up the asic is walked and the
2812 * soft_reset callbacks are run if the block is hung. soft_reset handles any
2813 * IP specific hardware or software state changes that are necessary to soft
2815 * Returns 0 on success, negative error code on failure.
2817 static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
2821 for (i = 0; i < adev->num_ip_blocks; i++) {
2822 if (!adev->ip_blocks[i].status.valid)
2824 if (adev->ip_blocks[i].status.hang &&
2825 adev->ip_blocks[i].version->funcs->soft_reset) {
2826 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
2836 * amdgpu_device_ip_post_soft_reset - clean up from soft reset
2838 * @adev: amdgpu_device pointer
2840 * The list of all the hardware IPs that make up the asic is walked and the
2841 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
2842 * handles any IP specific hardware or software state changes that are
2843 * necessary after the IP has been soft reset.
2844 * Returns 0 on success, negative error code on failure.
2846 static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
2850 for (i = 0; i < adev->num_ip_blocks; i++) {
2851 if (!adev->ip_blocks[i].status.valid)
2853 if (adev->ip_blocks[i].status.hang &&
2854 adev->ip_blocks[i].version->funcs->post_soft_reset)
2855 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
2864 * amdgpu_device_recover_vram_from_shadow - restore shadowed VRAM buffers
2866 * @adev: amdgpu_device pointer
2867 * @ring: amdgpu_ring for the engine handling the buffer operations
2868 * @bo: amdgpu_bo buffer whose shadow is being restored
2869 * @fence: dma_fence associated with the operation
2871 * Restores the VRAM buffer contents from the shadow in GTT. Used to
2872 * restore things like GPUVM page tables after a GPU reset where
2873 * the contents of VRAM might be lost.
2874 * Returns 0 on success, negative error code on failure.
2876 static int amdgpu_device_recover_vram_from_shadow(struct amdgpu_device *adev,
2877 struct amdgpu_ring *ring,
2878 struct amdgpu_bo *bo,
2879 struct dma_fence **fence)
2887 r = amdgpu_bo_reserve(bo, true);
2890 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2891 /* if bo has been evicted, then no need to recover */
2892 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
2893 r = amdgpu_bo_validate(bo->shadow);
2895 DRM_ERROR("bo validate failed!\n");
2899 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
2902 DRM_ERROR("recover page table failed!\n");
2907 amdgpu_bo_unreserve(bo);
2912 * amdgpu_device_handle_vram_lost - Handle the loss of VRAM contents
2914 * @adev: amdgpu_device pointer
2916 * Restores the contents of VRAM buffers from the shadows in GTT. Used to
2917 * restore things like GPUVM page tables after a GPU reset where
2918 * the contents of VRAM might be lost.
2919 * Returns 0 on success, 1 on failure.
2921 static int amdgpu_device_handle_vram_lost(struct amdgpu_device *adev)
2923 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2924 struct amdgpu_bo *bo, *tmp;
2925 struct dma_fence *fence = NULL, *next = NULL;
2930 if (amdgpu_sriov_runtime(adev))
2931 tmo = msecs_to_jiffies(amdgpu_lockup_timeout);
2933 tmo = msecs_to_jiffies(100);
2935 DRM_INFO("recover vram bo from shadow start\n");
2936 mutex_lock(&adev->shadow_list_lock);
2937 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2939 amdgpu_device_recover_vram_from_shadow(adev, ring, bo, &next);
2941 r = dma_fence_wait_timeout(fence, false, tmo);
2943 pr_err("wait fence %p[%d] timeout\n", fence, i);
2945 pr_err("wait fence %p[%d] interrupted\n", fence, i);
2947 dma_fence_put(fence);
2954 dma_fence_put(fence);
2957 mutex_unlock(&adev->shadow_list_lock);
2960 r = dma_fence_wait_timeout(fence, false, tmo);
2962 pr_err("wait fence %p[%d] timeout\n", fence, i);
2964 pr_err("wait fence %p[%d] interrupted\n", fence, i);
2967 dma_fence_put(fence);
2970 DRM_INFO("recover vram bo from shadow done\n");
2972 DRM_ERROR("recover vram bo from shadow failed\n");
2974 return (r > 0) ? 0 : 1;
2978 * amdgpu_device_reset - reset ASIC/GPU for bare-metal or passthrough
2980 * @adev: amdgpu device pointer
2982 * attempt to do soft-reset or full-reset and reinitialize Asic
2983 * return 0 means successed otherwise failed
2985 static int amdgpu_device_reset(struct amdgpu_device *adev)
2987 bool need_full_reset, vram_lost = 0;
2990 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
2992 if (!need_full_reset) {
2993 amdgpu_device_ip_pre_soft_reset(adev);
2994 r = amdgpu_device_ip_soft_reset(adev);
2995 amdgpu_device_ip_post_soft_reset(adev);
2996 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
2997 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2998 need_full_reset = true;
3002 if (need_full_reset) {
3003 r = amdgpu_device_ip_suspend(adev);
3006 r = amdgpu_asic_reset(adev);
3008 amdgpu_atom_asic_init(adev->mode_info.atom_context);
3011 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
3012 r = amdgpu_device_ip_resume_phase1(adev);
3016 vram_lost = amdgpu_device_check_vram_lost(adev);
3018 DRM_ERROR("VRAM is lost!\n");
3019 atomic_inc(&adev->vram_lost_counter);
3022 r = amdgpu_gtt_mgr_recover(
3023 &adev->mman.bdev.man[TTM_PL_TT]);
3027 r = amdgpu_device_ip_resume_phase2(adev);
3032 amdgpu_device_fill_reset_magic(adev);
3038 amdgpu_irq_gpu_reset_resume_helper(adev);
3039 r = amdgpu_ib_ring_tests(adev);
3041 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
3042 r = amdgpu_device_ip_suspend(adev);
3043 need_full_reset = true;
3048 if (!r && ((need_full_reset && !(adev->flags & AMD_IS_APU)) || vram_lost))
3049 r = amdgpu_device_handle_vram_lost(adev);
3055 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
3057 * @adev: amdgpu device pointer
3059 * do VF FLR and reinitialize Asic
3060 * return 0 means successed otherwise failed
3062 static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
3063 bool from_hypervisor)
3067 if (from_hypervisor)
3068 r = amdgpu_virt_request_full_gpu(adev, true);
3070 r = amdgpu_virt_reset_gpu(adev);
3074 /* Resume IP prior to SMC */
3075 r = amdgpu_device_ip_reinit_early_sriov(adev);
3079 /* we need recover gart prior to run SMC/CP/SDMA resume */
3080 amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
3082 /* now we are okay to resume SMC/CP/SDMA */
3083 r = amdgpu_device_ip_reinit_late_sriov(adev);
3084 amdgpu_virt_release_full_gpu(adev, true);
3088 amdgpu_irq_gpu_reset_resume_helper(adev);
3089 r = amdgpu_ib_ring_tests(adev);
3091 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
3092 atomic_inc(&adev->vram_lost_counter);
3093 r = amdgpu_device_handle_vram_lost(adev);
3102 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
3104 * @adev: amdgpu device pointer
3105 * @job: which job trigger hang
3106 * @force forces reset regardless of amdgpu_gpu_recovery
3108 * Attempt to reset the GPU if it has hung (all asics).
3109 * Returns 0 for success or an error on failure.
3111 int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
3112 struct amdgpu_job *job, bool force)
3114 struct drm_atomic_state *state = NULL;
3117 if (!force && !amdgpu_device_ip_check_soft_reset(adev)) {
3118 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
3122 if (!force && (amdgpu_gpu_recovery == 0 ||
3123 (amdgpu_gpu_recovery == -1 && !amdgpu_sriov_vf(adev)))) {
3124 DRM_INFO("GPU recovery disabled.\n");
3128 dev_info(adev->dev, "GPU reset begin!\n");
3130 mutex_lock(&adev->lock_reset);
3131 atomic_inc(&adev->gpu_reset_counter);
3132 adev->in_gpu_reset = 1;
3135 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
3137 /* store modesetting */
3138 if (amdgpu_device_has_dc_support(adev))
3139 state = drm_atomic_helper_suspend(adev->ddev);
3141 /* block all schedulers and reset given job's ring */
3142 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3143 struct amdgpu_ring *ring = adev->rings[i];
3145 if (!ring || !ring->sched.thread)
3148 kthread_park(ring->sched.thread);
3150 if (job && job->ring->idx != i)
3153 drm_sched_hw_job_reset(&ring->sched, &job->base);
3155 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
3156 amdgpu_fence_driver_force_completion(ring);
3159 if (amdgpu_sriov_vf(adev))
3160 r = amdgpu_device_reset_sriov(adev, job ? false : true);
3162 r = amdgpu_device_reset(adev);
3164 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3165 struct amdgpu_ring *ring = adev->rings[i];
3167 if (!ring || !ring->sched.thread)
3170 /* only need recovery sched of the given job's ring
3171 * or all rings (in the case @job is NULL)
3172 * after above amdgpu_reset accomplished
3174 if ((!job || job->ring->idx == i) && !r)
3175 drm_sched_job_recovery(&ring->sched);
3177 kthread_unpark(ring->sched.thread);
3180 if (amdgpu_device_has_dc_support(adev)) {
3181 if (drm_atomic_helper_resume(adev->ddev, state))
3182 dev_info(adev->dev, "drm resume failed:%d\n", r);
3184 drm_helper_resume_force_mode(adev->ddev);
3187 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
3190 /* bad news, how to tell it to userspace ? */
3191 dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
3192 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
3194 dev_info(adev->dev, "GPU reset(%d) successed!\n",atomic_read(&adev->gpu_reset_counter));
3197 amdgpu_vf_error_trans_all(adev);
3198 adev->in_gpu_reset = 0;
3199 mutex_unlock(&adev->lock_reset);
3204 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
3206 * @adev: amdgpu_device pointer
3208 * Fetchs and stores in the driver the PCIE capabilities (gen speed
3209 * and lanes) of the slot the device is in. Handles APUs and
3210 * virtualized environments where PCIE config space may not be available.
3212 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
3217 if (amdgpu_pcie_gen_cap)
3218 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
3220 if (amdgpu_pcie_lane_cap)
3221 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
3223 /* covers APUs as well */
3224 if (pci_is_root_bus(adev->pdev->bus)) {
3225 if (adev->pm.pcie_gen_mask == 0)
3226 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3227 if (adev->pm.pcie_mlw_mask == 0)
3228 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
3232 if (adev->pm.pcie_gen_mask == 0) {
3233 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
3235 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3236 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3237 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3239 if (mask & DRM_PCIE_SPEED_25)
3240 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
3241 if (mask & DRM_PCIE_SPEED_50)
3242 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
3243 if (mask & DRM_PCIE_SPEED_80)
3244 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
3246 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3249 if (adev->pm.pcie_mlw_mask == 0) {
3250 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
3254 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3255 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3256 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3257 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3258 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3259 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3260 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3263 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3264 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3265 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3266 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3267 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3268 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3271 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3272 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3273 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3274 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3275 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3278 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3279 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3280 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3281 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3284 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3285 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3286 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3289 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3290 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3293 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3299 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;