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/vega12_gpu_info.bin");
63 MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
65 #define AMDGPU_RESUME_MS 2000
67 static const char *amdgpu_asic_name[] = {
94 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev);
97 * amdgpu_device_is_px - Is the device is a dGPU with HG/PX power control
99 * @dev: drm_device pointer
101 * Returns true if the device is a dGPU with HG/PX power control,
102 * otherwise return false.
104 bool amdgpu_device_is_px(struct drm_device *dev)
106 struct amdgpu_device *adev = dev->dev_private;
108 if (adev->flags & AMD_IS_PX)
114 * MMIO register access helper functions.
117 * amdgpu_mm_rreg - read a memory mapped IO register
119 * @adev: amdgpu_device pointer
120 * @reg: dword aligned register offset
121 * @acc_flags: access flags which require special behavior
123 * Returns the 32 bit value from the offset specified.
125 uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
130 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
131 return amdgpu_virt_kiq_rreg(adev, reg);
133 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
134 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
138 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
139 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
140 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
141 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
143 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
148 * MMIO register read with bytes helper functions
149 * @offset:bytes offset from MMIO start
154 * amdgpu_mm_rreg8 - read a memory mapped IO register
156 * @adev: amdgpu_device pointer
157 * @offset: byte aligned register offset
159 * Returns the 8 bit value from the offset specified.
161 uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset) {
162 if (offset < adev->rmmio_size)
163 return (readb(adev->rmmio + offset));
168 * MMIO register write with bytes helper functions
169 * @offset:bytes offset from MMIO start
170 * @value: the value want to be written to the register
174 * amdgpu_mm_wreg8 - read a memory mapped IO register
176 * @adev: amdgpu_device pointer
177 * @offset: byte aligned register offset
178 * @value: 8 bit value to write
180 * Writes the value specified to the offset specified.
182 void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value) {
183 if (offset < adev->rmmio_size)
184 writeb(value, adev->rmmio + offset);
190 * amdgpu_mm_wreg - write to a memory mapped IO register
192 * @adev: amdgpu_device pointer
193 * @reg: dword aligned register offset
194 * @v: 32 bit value to write to the register
195 * @acc_flags: access flags which require special behavior
197 * Writes the value specified to the offset specified.
199 void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
202 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
204 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
205 adev->last_mm_index = v;
208 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
209 return amdgpu_virt_kiq_wreg(adev, reg, v);
211 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
212 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
216 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
217 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
218 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
219 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
222 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
228 * amdgpu_io_rreg - read an IO register
230 * @adev: amdgpu_device pointer
231 * @reg: dword aligned register offset
233 * Returns the 32 bit value from the offset specified.
235 u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
237 if ((reg * 4) < adev->rio_mem_size)
238 return ioread32(adev->rio_mem + (reg * 4));
240 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
241 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
246 * amdgpu_io_wreg - write to an IO register
248 * @adev: amdgpu_device pointer
249 * @reg: dword aligned register offset
250 * @v: 32 bit value to write to the register
252 * Writes the value specified to the offset specified.
254 void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
256 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
257 adev->last_mm_index = v;
260 if ((reg * 4) < adev->rio_mem_size)
261 iowrite32(v, adev->rio_mem + (reg * 4));
263 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
264 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
267 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
273 * amdgpu_mm_rdoorbell - read a doorbell dword
275 * @adev: amdgpu_device pointer
276 * @index: doorbell index
278 * Returns the value in the doorbell aperture at the
279 * requested doorbell index (CIK).
281 u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
283 if (index < adev->doorbell.num_doorbells) {
284 return readl(adev->doorbell.ptr + index);
286 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
292 * amdgpu_mm_wdoorbell - write a doorbell dword
294 * @adev: amdgpu_device pointer
295 * @index: doorbell index
298 * Writes @v to the doorbell aperture at the
299 * requested doorbell index (CIK).
301 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
303 if (index < adev->doorbell.num_doorbells) {
304 writel(v, adev->doorbell.ptr + index);
306 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
311 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
313 * @adev: amdgpu_device pointer
314 * @index: doorbell index
316 * Returns the value in the doorbell aperture at the
317 * requested doorbell index (VEGA10+).
319 u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
321 if (index < adev->doorbell.num_doorbells) {
322 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
324 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
330 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
332 * @adev: amdgpu_device pointer
333 * @index: doorbell index
336 * Writes @v to the doorbell aperture at the
337 * requested doorbell index (VEGA10+).
339 void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
341 if (index < adev->doorbell.num_doorbells) {
342 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
344 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
349 * amdgpu_invalid_rreg - dummy reg read function
351 * @adev: amdgpu device pointer
352 * @reg: offset of register
354 * Dummy register read function. Used for register blocks
355 * that certain asics don't have (all asics).
356 * Returns the value in the register.
358 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
360 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
366 * amdgpu_invalid_wreg - dummy reg write function
368 * @adev: amdgpu device pointer
369 * @reg: offset of register
370 * @v: value to write to the register
372 * Dummy register read function. Used for register blocks
373 * that certain asics don't have (all asics).
375 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
377 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
383 * amdgpu_block_invalid_rreg - dummy reg read function
385 * @adev: amdgpu device pointer
386 * @block: offset of instance
387 * @reg: offset of register
389 * Dummy register read function. Used for register blocks
390 * that certain asics don't have (all asics).
391 * Returns the value in the register.
393 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
394 uint32_t block, uint32_t reg)
396 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
403 * amdgpu_block_invalid_wreg - dummy reg write function
405 * @adev: amdgpu device pointer
406 * @block: offset of instance
407 * @reg: offset of register
408 * @v: value to write to the register
410 * Dummy register read function. Used for register blocks
411 * that certain asics don't have (all asics).
413 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
415 uint32_t reg, uint32_t v)
417 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
423 * amdgpu_device_vram_scratch_init - allocate the VRAM scratch page
425 * @adev: amdgpu device pointer
427 * Allocates a scratch page of VRAM for use by various things in the
430 static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev)
432 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
433 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
434 &adev->vram_scratch.robj,
435 &adev->vram_scratch.gpu_addr,
436 (void **)&adev->vram_scratch.ptr);
440 * amdgpu_device_vram_scratch_fini - Free the VRAM scratch page
442 * @adev: amdgpu device pointer
444 * Frees the VRAM scratch page.
446 static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev)
448 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
452 * amdgpu_device_program_register_sequence - program an array of registers.
454 * @adev: amdgpu_device pointer
455 * @registers: pointer to the register array
456 * @array_size: size of the register array
458 * Programs an array or registers with and and or masks.
459 * This is a helper for setting golden registers.
461 void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
462 const u32 *registers,
463 const u32 array_size)
465 u32 tmp, reg, and_mask, or_mask;
471 for (i = 0; i < array_size; i +=3) {
472 reg = registers[i + 0];
473 and_mask = registers[i + 1];
474 or_mask = registers[i + 2];
476 if (and_mask == 0xffffffff) {
488 * amdgpu_device_pci_config_reset - reset the GPU
490 * @adev: amdgpu_device pointer
492 * Resets the GPU using the pci config reset sequence.
493 * Only applicable to asics prior to vega10.
495 void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
497 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
501 * GPU doorbell aperture helpers function.
504 * amdgpu_device_doorbell_init - Init doorbell driver information.
506 * @adev: amdgpu_device pointer
508 * Init doorbell driver information (CIK)
509 * Returns 0 on success, error on failure.
511 static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
513 /* No doorbell on SI hardware generation */
514 if (adev->asic_type < CHIP_BONAIRE) {
515 adev->doorbell.base = 0;
516 adev->doorbell.size = 0;
517 adev->doorbell.num_doorbells = 0;
518 adev->doorbell.ptr = NULL;
522 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
525 /* doorbell bar mapping */
526 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
527 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
529 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
530 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
531 if (adev->doorbell.num_doorbells == 0)
534 adev->doorbell.ptr = ioremap(adev->doorbell.base,
535 adev->doorbell.num_doorbells *
537 if (adev->doorbell.ptr == NULL)
544 * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
546 * @adev: amdgpu_device pointer
548 * Tear down doorbell driver information (CIK)
550 static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
552 iounmap(adev->doorbell.ptr);
553 adev->doorbell.ptr = NULL;
559 * amdgpu_device_wb_*()
560 * Writeback is the method by which the GPU updates special pages in memory
561 * with the status of certain GPU events (fences, ring pointers,etc.).
565 * amdgpu_device_wb_fini - Disable Writeback and free memory
567 * @adev: amdgpu_device pointer
569 * Disables Writeback and frees the Writeback memory (all asics).
570 * Used at driver shutdown.
572 static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
574 if (adev->wb.wb_obj) {
575 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
577 (void **)&adev->wb.wb);
578 adev->wb.wb_obj = NULL;
583 * amdgpu_device_wb_init- Init Writeback driver info and allocate memory
585 * @adev: amdgpu_device pointer
587 * Initializes writeback and allocates writeback memory (all asics).
588 * Used at driver startup.
589 * Returns 0 on success or an -error on failure.
591 static int amdgpu_device_wb_init(struct amdgpu_device *adev)
595 if (adev->wb.wb_obj == NULL) {
596 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
597 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
598 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
599 &adev->wb.wb_obj, &adev->wb.gpu_addr,
600 (void **)&adev->wb.wb);
602 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
606 adev->wb.num_wb = AMDGPU_MAX_WB;
607 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
609 /* clear wb memory */
610 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
617 * amdgpu_device_wb_get - Allocate a wb entry
619 * @adev: amdgpu_device pointer
622 * Allocate a wb slot for use by the driver (all asics).
623 * Returns 0 on success or -EINVAL on failure.
625 int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
627 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
629 if (offset < adev->wb.num_wb) {
630 __set_bit(offset, adev->wb.used);
631 *wb = offset << 3; /* convert to dw offset */
639 * amdgpu_device_wb_free - Free a wb entry
641 * @adev: amdgpu_device pointer
644 * Free a wb slot allocated for use by the driver (all asics)
646 void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
649 if (wb < adev->wb.num_wb)
650 __clear_bit(wb, adev->wb.used);
654 * amdgpu_device_vram_location - try to find VRAM location
656 * @adev: amdgpu device structure holding all necessary informations
657 * @mc: memory controller structure holding memory informations
658 * @base: base address at which to put VRAM
660 * Function will try to place VRAM at base address provided
663 void amdgpu_device_vram_location(struct amdgpu_device *adev,
664 struct amdgpu_gmc *mc, u64 base)
666 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
668 mc->vram_start = base;
669 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
670 if (limit && limit < mc->real_vram_size)
671 mc->real_vram_size = limit;
672 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
673 mc->mc_vram_size >> 20, mc->vram_start,
674 mc->vram_end, mc->real_vram_size >> 20);
678 * amdgpu_device_gart_location - try to find GTT location
680 * @adev: amdgpu device structure holding all necessary informations
681 * @mc: memory controller structure holding memory informations
683 * Function will place try to place GTT before or after VRAM.
685 * If GTT size is bigger than space left then we ajust GTT size.
686 * Thus function will never fails.
688 * FIXME: when reducing GTT size align new size on power of 2.
690 void amdgpu_device_gart_location(struct amdgpu_device *adev,
691 struct amdgpu_gmc *mc)
693 u64 size_af, size_bf;
695 mc->gart_size += adev->pm.smu_prv_buffer_size;
697 size_af = adev->gmc.mc_mask - mc->vram_end;
698 size_bf = mc->vram_start;
699 if (size_bf > size_af) {
700 if (mc->gart_size > size_bf) {
701 dev_warn(adev->dev, "limiting GTT\n");
702 mc->gart_size = size_bf;
706 if (mc->gart_size > size_af) {
707 dev_warn(adev->dev, "limiting GTT\n");
708 mc->gart_size = size_af;
710 /* VCE doesn't like it when BOs cross a 4GB segment, so align
711 * the GART base on a 4GB boundary as well.
713 mc->gart_start = ALIGN(mc->vram_end + 1, 0x100000000ULL);
715 mc->gart_end = mc->gart_start + mc->gart_size - 1;
716 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
717 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
721 * amdgpu_device_resize_fb_bar - try to resize FB BAR
723 * @adev: amdgpu_device pointer
725 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
726 * to fail, but if any of the BARs is not accessible after the size we abort
727 * driver loading by returning -ENODEV.
729 int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
731 u64 space_needed = roundup_pow_of_two(adev->gmc.real_vram_size);
732 u32 rbar_size = order_base_2(((space_needed >> 20) | 1)) - 1;
733 struct pci_bus *root;
734 struct resource *res;
740 if (amdgpu_sriov_vf(adev))
743 /* Check if the root BUS has 64bit memory resources */
744 root = adev->pdev->bus;
748 pci_bus_for_each_resource(root, res, i) {
749 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
750 res->start > 0x100000000ull)
754 /* Trying to resize is pointless without a root hub window above 4GB */
758 /* Disable memory decoding while we change the BAR addresses and size */
759 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
760 pci_write_config_word(adev->pdev, PCI_COMMAND,
761 cmd & ~PCI_COMMAND_MEMORY);
763 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
764 amdgpu_device_doorbell_fini(adev);
765 if (adev->asic_type >= CHIP_BONAIRE)
766 pci_release_resource(adev->pdev, 2);
768 pci_release_resource(adev->pdev, 0);
770 r = pci_resize_resource(adev->pdev, 0, rbar_size);
772 DRM_INFO("Not enough PCI address space for a large BAR.");
773 else if (r && r != -ENOTSUPP)
774 DRM_ERROR("Problem resizing BAR0 (%d).", r);
776 pci_assign_unassigned_bus_resources(adev->pdev->bus);
778 /* When the doorbell or fb BAR isn't available we have no chance of
781 r = amdgpu_device_doorbell_init(adev);
782 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
785 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
791 * GPU helpers function.
794 * amdgpu_device_need_post - check if the hw need post or not
796 * @adev: amdgpu_device pointer
798 * Check if the asic has been initialized (all asics) at driver startup
799 * or post is needed if hw reset is performed.
800 * Returns true if need or false if not.
802 bool amdgpu_device_need_post(struct amdgpu_device *adev)
806 if (amdgpu_sriov_vf(adev))
809 if (amdgpu_passthrough(adev)) {
810 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
811 * some old smc fw still need driver do vPost otherwise gpu hang, while
812 * those smc fw version above 22.15 doesn't have this flaw, so we force
813 * vpost executed for smc version below 22.15
815 if (adev->asic_type == CHIP_FIJI) {
818 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
819 /* force vPost if error occured */
823 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
824 if (fw_ver < 0x00160e00)
829 if (adev->has_hw_reset) {
830 adev->has_hw_reset = false;
834 /* bios scratch used on CIK+ */
835 if (adev->asic_type >= CHIP_BONAIRE)
836 return amdgpu_atombios_scratch_need_asic_init(adev);
838 /* check MEM_SIZE for older asics */
839 reg = amdgpu_asic_get_config_memsize(adev);
841 if ((reg != 0) && (reg != 0xffffffff))
847 /* if we get transitioned to only one device, take VGA back */
849 * amdgpu_device_vga_set_decode - enable/disable vga decode
851 * @cookie: amdgpu_device pointer
852 * @state: enable/disable vga decode
854 * Enable/disable vga decode (all asics).
855 * Returns VGA resource flags.
857 static unsigned int amdgpu_device_vga_set_decode(void *cookie, bool state)
859 struct amdgpu_device *adev = cookie;
860 amdgpu_asic_set_vga_state(adev, state);
862 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
863 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
865 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
869 * amdgpu_device_check_block_size - validate the vm block size
871 * @adev: amdgpu_device pointer
873 * Validates the vm block size specified via module parameter.
874 * The vm block size 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.
878 static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
880 /* defines number of bits in page table versus page directory,
881 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
882 * page table and the remaining bits are in the page directory */
883 if (amdgpu_vm_block_size == -1)
886 if (amdgpu_vm_block_size < 9) {
887 dev_warn(adev->dev, "VM page table size (%d) too small\n",
888 amdgpu_vm_block_size);
889 amdgpu_vm_block_size = -1;
894 * amdgpu_device_check_vm_size - validate the vm size
896 * @adev: amdgpu_device pointer
898 * Validates the vm size in GB specified via module parameter.
899 * The VM size is the size of the GPU virtual memory space in GB.
901 static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
903 /* no need to check the default value */
904 if (amdgpu_vm_size == -1)
907 if (amdgpu_vm_size < 1) {
908 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
914 static void amdgpu_device_check_smu_prv_buffer_size(struct amdgpu_device *adev)
917 bool is_os_64 = (sizeof(void *) == 8) ? true : false;
918 uint64_t total_memory;
919 uint64_t dram_size_seven_GB = 0x1B8000000;
920 uint64_t dram_size_three_GB = 0xB8000000;
922 if (amdgpu_smu_memory_pool_size == 0)
926 DRM_WARN("Not 64-bit OS, feature not supported\n");
930 total_memory = (uint64_t)si.totalram * si.mem_unit;
932 if ((amdgpu_smu_memory_pool_size == 1) ||
933 (amdgpu_smu_memory_pool_size == 2)) {
934 if (total_memory < dram_size_three_GB)
936 } else if ((amdgpu_smu_memory_pool_size == 4) ||
937 (amdgpu_smu_memory_pool_size == 8)) {
938 if (total_memory < dram_size_seven_GB)
941 DRM_WARN("Smu memory pool size not supported\n");
944 adev->pm.smu_prv_buffer_size = amdgpu_smu_memory_pool_size << 28;
949 DRM_WARN("No enough system memory\n");
951 adev->pm.smu_prv_buffer_size = 0;
955 * amdgpu_device_check_arguments - validate module params
957 * @adev: amdgpu_device pointer
959 * Validates certain module parameters and updates
960 * the associated values used by the driver (all asics).
962 static void amdgpu_device_check_arguments(struct amdgpu_device *adev)
964 if (amdgpu_sched_jobs < 4) {
965 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
967 amdgpu_sched_jobs = 4;
968 } else if (!is_power_of_2(amdgpu_sched_jobs)){
969 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
971 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
974 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
975 /* gart size must be greater or equal to 32M */
976 dev_warn(adev->dev, "gart size (%d) too small\n",
978 amdgpu_gart_size = -1;
981 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
982 /* gtt size must be greater or equal to 32M */
983 dev_warn(adev->dev, "gtt size (%d) too small\n",
985 amdgpu_gtt_size = -1;
988 /* valid range is between 4 and 9 inclusive */
989 if (amdgpu_vm_fragment_size != -1 &&
990 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
991 dev_warn(adev->dev, "valid range is between 4 and 9\n");
992 amdgpu_vm_fragment_size = -1;
995 amdgpu_device_check_smu_prv_buffer_size(adev);
997 amdgpu_device_check_vm_size(adev);
999 amdgpu_device_check_block_size(adev);
1001 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
1002 !is_power_of_2(amdgpu_vram_page_split))) {
1003 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1004 amdgpu_vram_page_split);
1005 amdgpu_vram_page_split = 1024;
1008 if (amdgpu_lockup_timeout == 0) {
1009 dev_warn(adev->dev, "lockup_timeout msut be > 0, adjusting to 10000\n");
1010 amdgpu_lockup_timeout = 10000;
1013 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
1017 * amdgpu_switcheroo_set_state - set switcheroo state
1019 * @pdev: pci dev pointer
1020 * @state: vga_switcheroo state
1022 * Callback for the switcheroo driver. Suspends or resumes the
1023 * the asics before or after it is powered up using ACPI methods.
1025 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1027 struct drm_device *dev = pci_get_drvdata(pdev);
1029 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1032 if (state == VGA_SWITCHEROO_ON) {
1033 pr_info("amdgpu: switched on\n");
1034 /* don't suspend or resume card normally */
1035 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1037 amdgpu_device_resume(dev, true, true);
1039 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1040 drm_kms_helper_poll_enable(dev);
1042 pr_info("amdgpu: switched off\n");
1043 drm_kms_helper_poll_disable(dev);
1044 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1045 amdgpu_device_suspend(dev, true, true);
1046 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1051 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1053 * @pdev: pci dev pointer
1055 * Callback for the switcheroo driver. Check of the switcheroo
1056 * state can be changed.
1057 * Returns true if the state can be changed, false if not.
1059 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1061 struct drm_device *dev = pci_get_drvdata(pdev);
1064 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1065 * locking inversion with the driver load path. And the access here is
1066 * completely racy anyway. So don't bother with locking for now.
1068 return dev->open_count == 0;
1071 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1072 .set_gpu_state = amdgpu_switcheroo_set_state,
1074 .can_switch = amdgpu_switcheroo_can_switch,
1078 * amdgpu_device_ip_set_clockgating_state - set the CG state
1080 * @adev: amdgpu_device pointer
1081 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1082 * @state: clockgating state (gate or ungate)
1084 * Sets the requested clockgating state for all instances of
1085 * the hardware IP specified.
1086 * Returns the error code from the last instance.
1088 int amdgpu_device_ip_set_clockgating_state(void *dev,
1089 enum amd_ip_block_type block_type,
1090 enum amd_clockgating_state state)
1092 struct amdgpu_device *adev = dev;
1095 for (i = 0; i < adev->num_ip_blocks; i++) {
1096 if (!adev->ip_blocks[i].status.valid)
1098 if (adev->ip_blocks[i].version->type != block_type)
1100 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1102 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1103 (void *)adev, state);
1105 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1106 adev->ip_blocks[i].version->funcs->name, r);
1112 * amdgpu_device_ip_set_powergating_state - set the PG state
1114 * @adev: amdgpu_device pointer
1115 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1116 * @state: powergating state (gate or ungate)
1118 * Sets the requested powergating state for all instances of
1119 * the hardware IP specified.
1120 * Returns the error code from the last instance.
1122 int amdgpu_device_ip_set_powergating_state(void *dev,
1123 enum amd_ip_block_type block_type,
1124 enum amd_powergating_state state)
1126 struct amdgpu_device *adev = dev;
1129 for (i = 0; i < adev->num_ip_blocks; i++) {
1130 if (!adev->ip_blocks[i].status.valid)
1132 if (adev->ip_blocks[i].version->type != block_type)
1134 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1136 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1137 (void *)adev, state);
1139 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1140 adev->ip_blocks[i].version->funcs->name, r);
1146 * amdgpu_device_ip_get_clockgating_state - get the CG state
1148 * @adev: amdgpu_device pointer
1149 * @flags: clockgating feature flags
1151 * Walks the list of IPs on the device and updates the clockgating
1152 * flags for each IP.
1153 * Updates @flags with the feature flags for each hardware IP where
1154 * clockgating is enabled.
1156 void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
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->funcs->get_clockgating_state)
1165 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1170 * amdgpu_device_ip_wait_for_idle - wait for idle
1172 * @adev: amdgpu_device pointer
1173 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1175 * Waits for the request hardware IP to be idle.
1176 * Returns 0 for success or a negative error code on failure.
1178 int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1179 enum amd_ip_block_type block_type)
1183 for (i = 0; i < adev->num_ip_blocks; i++) {
1184 if (!adev->ip_blocks[i].status.valid)
1186 if (adev->ip_blocks[i].version->type == block_type) {
1187 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
1198 * amdgpu_device_ip_is_idle - is the hardware IP idle
1200 * @adev: amdgpu_device pointer
1201 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1203 * Check if the hardware IP is idle or not.
1204 * Returns true if it the IP is idle, false if not.
1206 bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1207 enum amd_ip_block_type block_type)
1211 for (i = 0; i < adev->num_ip_blocks; i++) {
1212 if (!adev->ip_blocks[i].status.valid)
1214 if (adev->ip_blocks[i].version->type == block_type)
1215 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
1222 * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1224 * @adev: amdgpu_device pointer
1225 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1227 * Returns a pointer to the hardware IP block structure
1228 * if it exists for the asic, otherwise NULL.
1230 struct amdgpu_ip_block *
1231 amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1232 enum amd_ip_block_type type)
1236 for (i = 0; i < adev->num_ip_blocks; i++)
1237 if (adev->ip_blocks[i].version->type == type)
1238 return &adev->ip_blocks[i];
1244 * amdgpu_device_ip_block_version_cmp
1246 * @adev: amdgpu_device pointer
1247 * @type: enum amd_ip_block_type
1248 * @major: major version
1249 * @minor: minor version
1251 * return 0 if equal or greater
1252 * return 1 if smaller or the ip_block doesn't exist
1254 int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1255 enum amd_ip_block_type type,
1256 u32 major, u32 minor)
1258 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
1260 if (ip_block && ((ip_block->version->major > major) ||
1261 ((ip_block->version->major == major) &&
1262 (ip_block->version->minor >= minor))))
1269 * amdgpu_device_ip_block_add
1271 * @adev: amdgpu_device pointer
1272 * @ip_block_version: pointer to the IP to add
1274 * Adds the IP block driver information to the collection of IPs
1277 int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1278 const struct amdgpu_ip_block_version *ip_block_version)
1280 if (!ip_block_version)
1283 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
1284 ip_block_version->funcs->name);
1286 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1292 * amdgpu_device_enable_virtual_display - enable virtual display feature
1294 * @adev: amdgpu_device pointer
1296 * Enabled the virtual display feature if the user has enabled it via
1297 * the module parameter virtual_display. This feature provides a virtual
1298 * display hardware on headless boards or in virtualized environments.
1299 * This function parses and validates the configuration string specified by
1300 * the user and configues the virtual display configuration (number of
1301 * virtual connectors, crtcs, etc.) specified.
1303 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
1305 adev->enable_virtual_display = false;
1307 if (amdgpu_virtual_display) {
1308 struct drm_device *ddev = adev->ddev;
1309 const char *pci_address_name = pci_name(ddev->pdev);
1310 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
1312 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1313 pciaddstr_tmp = pciaddstr;
1314 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1315 pciaddname = strsep(&pciaddname_tmp, ",");
1316 if (!strcmp("all", pciaddname)
1317 || !strcmp(pci_address_name, pciaddname)) {
1321 adev->enable_virtual_display = true;
1324 res = kstrtol(pciaddname_tmp, 10,
1332 adev->mode_info.num_crtc = num_crtc;
1334 adev->mode_info.num_crtc = 1;
1340 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1341 amdgpu_virtual_display, pci_address_name,
1342 adev->enable_virtual_display, adev->mode_info.num_crtc);
1349 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
1351 * @adev: amdgpu_device pointer
1353 * Parses the asic configuration parameters specified in the gpu info
1354 * firmware and makes them availale to the driver for use in configuring
1356 * Returns 0 on success, -EINVAL on failure.
1358 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1360 const char *chip_name;
1363 const struct gpu_info_firmware_header_v1_0 *hdr;
1365 adev->firmware.gpu_info_fw = NULL;
1367 switch (adev->asic_type) {
1371 case CHIP_POLARIS10:
1372 case CHIP_POLARIS11:
1373 case CHIP_POLARIS12:
1377 #ifdef CONFIG_DRM_AMDGPU_SI
1384 #ifdef CONFIG_DRM_AMDGPU_CIK
1395 chip_name = "vega10";
1398 chip_name = "vega12";
1401 chip_name = "raven";
1405 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
1406 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
1409 "Failed to load gpu_info firmware \"%s\"\n",
1413 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
1416 "Failed to validate gpu_info firmware \"%s\"\n",
1421 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
1422 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1424 switch (hdr->version_major) {
1427 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
1428 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
1429 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1431 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1432 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1433 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1434 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
1435 adev->gfx.config.max_texture_channel_caches =
1436 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1437 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1438 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1439 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1440 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
1441 adev->gfx.config.double_offchip_lds_buf =
1442 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1443 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
1444 adev->gfx.cu_info.max_waves_per_simd =
1445 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1446 adev->gfx.cu_info.max_scratch_slots_per_cu =
1447 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1448 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
1453 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1462 * amdgpu_device_ip_early_init - run early init for hardware IPs
1464 * @adev: amdgpu_device pointer
1466 * Early initialization pass for hardware IPs. The hardware IPs that make
1467 * up each asic are discovered each IP's early_init callback is run. This
1468 * is the first stage in initializing the asic.
1469 * Returns 0 on success, negative error code on failure.
1471 static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
1475 amdgpu_device_enable_virtual_display(adev);
1477 switch (adev->asic_type) {
1481 case CHIP_POLARIS10:
1482 case CHIP_POLARIS11:
1483 case CHIP_POLARIS12:
1487 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
1488 adev->family = AMDGPU_FAMILY_CZ;
1490 adev->family = AMDGPU_FAMILY_VI;
1492 r = vi_set_ip_blocks(adev);
1496 #ifdef CONFIG_DRM_AMDGPU_SI
1502 adev->family = AMDGPU_FAMILY_SI;
1503 r = si_set_ip_blocks(adev);
1508 #ifdef CONFIG_DRM_AMDGPU_CIK
1514 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1515 adev->family = AMDGPU_FAMILY_CI;
1517 adev->family = AMDGPU_FAMILY_KV;
1519 r = cik_set_ip_blocks(adev);
1528 if (adev->asic_type == CHIP_RAVEN)
1529 adev->family = AMDGPU_FAMILY_RV;
1531 adev->family = AMDGPU_FAMILY_AI;
1533 r = soc15_set_ip_blocks(adev);
1538 /* FIXME: not supported yet */
1542 r = amdgpu_device_parse_gpu_info_fw(adev);
1546 amdgpu_amdkfd_device_probe(adev);
1548 if (amdgpu_sriov_vf(adev)) {
1549 r = amdgpu_virt_request_full_gpu(adev, true);
1554 adev->powerplay.pp_feature = amdgpu_pp_feature_mask;
1556 for (i = 0; i < adev->num_ip_blocks; i++) {
1557 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1558 DRM_ERROR("disabled ip block: %d <%s>\n",
1559 i, adev->ip_blocks[i].version->funcs->name);
1560 adev->ip_blocks[i].status.valid = false;
1562 if (adev->ip_blocks[i].version->funcs->early_init) {
1563 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
1565 adev->ip_blocks[i].status.valid = false;
1567 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1568 adev->ip_blocks[i].version->funcs->name, r);
1571 adev->ip_blocks[i].status.valid = true;
1574 adev->ip_blocks[i].status.valid = true;
1579 adev->cg_flags &= amdgpu_cg_mask;
1580 adev->pg_flags &= amdgpu_pg_mask;
1586 * amdgpu_device_ip_init - run init for hardware IPs
1588 * @adev: amdgpu_device pointer
1590 * Main initialization pass for hardware IPs. The list of all the hardware
1591 * IPs that make up the asic is walked and the sw_init and hw_init callbacks
1592 * are run. sw_init initializes the software state associated with each IP
1593 * and hw_init initializes the hardware associated with each IP.
1594 * Returns 0 on success, negative error code on failure.
1596 static int amdgpu_device_ip_init(struct amdgpu_device *adev)
1600 for (i = 0; i < adev->num_ip_blocks; i++) {
1601 if (!adev->ip_blocks[i].status.valid)
1603 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
1605 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1606 adev->ip_blocks[i].version->funcs->name, r);
1609 adev->ip_blocks[i].status.sw = true;
1611 /* need to do gmc hw init early so we can allocate gpu mem */
1612 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1613 r = amdgpu_device_vram_scratch_init(adev);
1615 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
1618 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1620 DRM_ERROR("hw_init %d failed %d\n", i, r);
1623 r = amdgpu_device_wb_init(adev);
1625 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
1628 adev->ip_blocks[i].status.hw = true;
1630 /* right after GMC hw init, we create CSA */
1631 if (amdgpu_sriov_vf(adev)) {
1632 r = amdgpu_allocate_static_csa(adev);
1634 DRM_ERROR("allocate CSA failed %d\n", r);
1641 for (i = 0; i < adev->num_ip_blocks; i++) {
1642 if (!adev->ip_blocks[i].status.sw)
1644 if (adev->ip_blocks[i].status.hw)
1646 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1648 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1649 adev->ip_blocks[i].version->funcs->name, r);
1652 adev->ip_blocks[i].status.hw = true;
1655 amdgpu_amdkfd_device_init(adev);
1657 if (amdgpu_sriov_vf(adev))
1658 amdgpu_virt_release_full_gpu(adev, true);
1664 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
1666 * @adev: amdgpu_device pointer
1668 * Writes a reset magic value to the gart pointer in VRAM. The driver calls
1669 * this function before a GPU reset. If the value is retained after a
1670 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
1672 static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
1674 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1678 * amdgpu_device_check_vram_lost - check if vram is valid
1680 * @adev: amdgpu_device pointer
1682 * Checks the reset magic value written to the gart pointer in VRAM.
1683 * The driver calls this after a GPU reset to see if the contents of
1684 * VRAM is lost or now.
1685 * returns true if vram is lost, false if not.
1687 static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
1689 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1690 AMDGPU_RESET_MAGIC_NUM);
1694 * amdgpu_device_ip_late_set_cg_state - late init for clockgating
1696 * @adev: amdgpu_device pointer
1698 * Late initialization pass enabling clockgating for hardware IPs.
1699 * The list of all the hardware IPs that make up the asic is walked and the
1700 * set_clockgating_state callbacks are run. This stage is run late
1701 * in the init process.
1702 * Returns 0 on success, negative error code on failure.
1704 static int amdgpu_device_ip_late_set_cg_state(struct amdgpu_device *adev)
1708 if (amdgpu_emu_mode == 1)
1711 r = amdgpu_ib_ring_tests(adev);
1713 DRM_ERROR("ib ring test failed (%d).\n", r);
1715 for (i = 0; i < adev->num_ip_blocks; i++) {
1716 if (!adev->ip_blocks[i].status.valid)
1718 /* skip CG for VCE/UVD, it's handled specially */
1719 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1720 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1721 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
1722 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
1723 /* enable clockgating to save power */
1724 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1727 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1728 adev->ip_blocks[i].version->funcs->name, r);
1734 if (adev->powerplay.pp_feature & PP_GFXOFF_MASK) {
1735 /* enable gfx powergating */
1736 amdgpu_device_ip_set_powergating_state(adev,
1737 AMD_IP_BLOCK_TYPE_GFX,
1740 amdgpu_device_ip_set_powergating_state(adev,
1741 AMD_IP_BLOCK_TYPE_SMC,
1749 * amdgpu_device_ip_late_init - run late init for hardware IPs
1751 * @adev: amdgpu_device pointer
1753 * Late initialization pass for hardware IPs. The list of all the hardware
1754 * IPs that make up the asic is walked and the late_init callbacks are run.
1755 * late_init covers any special initialization that an IP requires
1756 * after all of the have been initialized or something that needs to happen
1757 * late in the init process.
1758 * Returns 0 on success, negative error code on failure.
1760 static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
1764 for (i = 0; i < adev->num_ip_blocks; i++) {
1765 if (!adev->ip_blocks[i].status.valid)
1767 if (adev->ip_blocks[i].version->funcs->late_init) {
1768 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
1770 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1771 adev->ip_blocks[i].version->funcs->name, r);
1774 adev->ip_blocks[i].status.late_initialized = true;
1778 queue_delayed_work(system_wq, &adev->late_init_work,
1779 msecs_to_jiffies(AMDGPU_RESUME_MS));
1781 amdgpu_device_fill_reset_magic(adev);
1787 * amdgpu_device_ip_fini - run fini for hardware IPs
1789 * @adev: amdgpu_device pointer
1791 * Main teardown pass for hardware IPs. The list of all the hardware
1792 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
1793 * are run. hw_fini tears down the hardware associated with each IP
1794 * and sw_fini tears down any software state associated with each IP.
1795 * Returns 0 on success, negative error code on failure.
1797 static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
1801 amdgpu_amdkfd_device_fini(adev);
1802 /* need to disable SMC first */
1803 for (i = 0; i < adev->num_ip_blocks; i++) {
1804 if (!adev->ip_blocks[i].status.hw)
1806 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC &&
1807 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
1808 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1809 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1810 AMD_CG_STATE_UNGATE);
1812 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1813 adev->ip_blocks[i].version->funcs->name, r);
1816 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
1817 /* XXX handle errors */
1819 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1820 adev->ip_blocks[i].version->funcs->name, r);
1822 adev->ip_blocks[i].status.hw = false;
1827 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1828 if (!adev->ip_blocks[i].status.hw)
1831 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1832 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1833 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
1834 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
1835 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1836 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1837 AMD_CG_STATE_UNGATE);
1839 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1840 adev->ip_blocks[i].version->funcs->name, r);
1845 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
1846 /* XXX handle errors */
1848 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1849 adev->ip_blocks[i].version->funcs->name, r);
1852 adev->ip_blocks[i].status.hw = false;
1856 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1857 if (!adev->ip_blocks[i].status.sw)
1860 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1861 amdgpu_free_static_csa(adev);
1862 amdgpu_device_wb_fini(adev);
1863 amdgpu_device_vram_scratch_fini(adev);
1866 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
1867 /* XXX handle errors */
1869 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1870 adev->ip_blocks[i].version->funcs->name, r);
1872 adev->ip_blocks[i].status.sw = false;
1873 adev->ip_blocks[i].status.valid = false;
1876 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1877 if (!adev->ip_blocks[i].status.late_initialized)
1879 if (adev->ip_blocks[i].version->funcs->late_fini)
1880 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1881 adev->ip_blocks[i].status.late_initialized = false;
1884 if (amdgpu_sriov_vf(adev))
1885 if (amdgpu_virt_release_full_gpu(adev, false))
1886 DRM_ERROR("failed to release exclusive mode on fini\n");
1892 * amdgpu_device_ip_late_init_func_handler - work handler for clockgating
1894 * @work: work_struct
1896 * Work handler for amdgpu_device_ip_late_set_cg_state. We put the
1897 * clockgating setup into a worker thread to speed up driver init and
1898 * resume from suspend.
1900 static void amdgpu_device_ip_late_init_func_handler(struct work_struct *work)
1902 struct amdgpu_device *adev =
1903 container_of(work, struct amdgpu_device, late_init_work.work);
1904 amdgpu_device_ip_late_set_cg_state(adev);
1908 * amdgpu_device_ip_suspend - run suspend for hardware IPs
1910 * @adev: amdgpu_device pointer
1912 * Main suspend function for hardware IPs. The list of all the hardware
1913 * IPs that make up the asic is walked, clockgating is disabled and the
1914 * suspend callbacks are run. suspend puts the hardware and software state
1915 * in each IP into a state suitable for suspend.
1916 * Returns 0 on success, negative error code on failure.
1918 int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
1922 if (amdgpu_sriov_vf(adev))
1923 amdgpu_virt_request_full_gpu(adev, false);
1925 /* ungate SMC block powergating */
1926 if (adev->powerplay.pp_feature & PP_GFXOFF_MASK)
1927 amdgpu_device_ip_set_powergating_state(adev,
1928 AMD_IP_BLOCK_TYPE_SMC,
1929 AMD_CG_STATE_UNGATE);
1931 /* ungate SMC block first */
1932 r = amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1933 AMD_CG_STATE_UNGATE);
1935 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n", r);
1938 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1939 if (!adev->ip_blocks[i].status.valid)
1941 /* ungate blocks so that suspend can properly shut them down */
1942 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_SMC &&
1943 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
1944 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1945 AMD_CG_STATE_UNGATE);
1947 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1948 adev->ip_blocks[i].version->funcs->name, r);
1951 /* XXX handle errors */
1952 r = adev->ip_blocks[i].version->funcs->suspend(adev);
1953 /* XXX handle errors */
1955 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1956 adev->ip_blocks[i].version->funcs->name, r);
1960 if (amdgpu_sriov_vf(adev))
1961 amdgpu_virt_release_full_gpu(adev, false);
1966 static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
1970 static enum amd_ip_block_type ip_order[] = {
1971 AMD_IP_BLOCK_TYPE_GMC,
1972 AMD_IP_BLOCK_TYPE_COMMON,
1973 AMD_IP_BLOCK_TYPE_IH,
1976 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1978 struct amdgpu_ip_block *block;
1980 for (j = 0; j < adev->num_ip_blocks; j++) {
1981 block = &adev->ip_blocks[j];
1983 if (block->version->type != ip_order[i] ||
1984 !block->status.valid)
1987 r = block->version->funcs->hw_init(adev);
1988 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
1997 static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
2001 static enum amd_ip_block_type ip_order[] = {
2002 AMD_IP_BLOCK_TYPE_SMC,
2003 AMD_IP_BLOCK_TYPE_PSP,
2004 AMD_IP_BLOCK_TYPE_DCE,
2005 AMD_IP_BLOCK_TYPE_GFX,
2006 AMD_IP_BLOCK_TYPE_SDMA,
2007 AMD_IP_BLOCK_TYPE_UVD,
2008 AMD_IP_BLOCK_TYPE_VCE
2011 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
2013 struct amdgpu_ip_block *block;
2015 for (j = 0; j < adev->num_ip_blocks; j++) {
2016 block = &adev->ip_blocks[j];
2018 if (block->version->type != ip_order[i] ||
2019 !block->status.valid)
2022 r = block->version->funcs->hw_init(adev);
2023 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
2033 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
2035 * @adev: amdgpu_device pointer
2037 * First resume function for hardware IPs. The list of all the hardware
2038 * IPs that make up the asic is walked and the resume callbacks are run for
2039 * COMMON, GMC, and IH. resume puts the hardware into a functional state
2040 * after a suspend and updates the software state as necessary. This
2041 * function is also used for restoring the GPU after a GPU reset.
2042 * Returns 0 on success, negative error code on failure.
2044 static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
2048 for (i = 0; i < adev->num_ip_blocks; i++) {
2049 if (!adev->ip_blocks[i].status.valid)
2051 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2052 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2053 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
2054 r = adev->ip_blocks[i].version->funcs->resume(adev);
2056 DRM_ERROR("resume of IP block <%s> failed %d\n",
2057 adev->ip_blocks[i].version->funcs->name, r);
2067 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
2069 * @adev: amdgpu_device pointer
2071 * First resume function for hardware IPs. The list of all the hardware
2072 * IPs that make up the asic is walked and the resume callbacks are run for
2073 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
2074 * functional state after a suspend and updates the software state as
2075 * necessary. This function is also used for restoring the GPU after a GPU
2077 * Returns 0 on success, negative error code on failure.
2079 static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
2083 for (i = 0; i < adev->num_ip_blocks; i++) {
2084 if (!adev->ip_blocks[i].status.valid)
2086 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2087 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2088 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)
2090 r = adev->ip_blocks[i].version->funcs->resume(adev);
2092 DRM_ERROR("resume of IP block <%s> failed %d\n",
2093 adev->ip_blocks[i].version->funcs->name, r);
2102 * amdgpu_device_ip_resume - run resume for hardware IPs
2104 * @adev: amdgpu_device pointer
2106 * Main resume function for hardware IPs. The hardware IPs
2107 * are split into two resume functions because they are
2108 * are also used in in recovering from a GPU reset and some additional
2109 * steps need to be take between them. In this case (S3/S4) they are
2111 * Returns 0 on success, negative error code on failure.
2113 static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
2117 r = amdgpu_device_ip_resume_phase1(adev);
2120 r = amdgpu_device_ip_resume_phase2(adev);
2126 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
2128 * @adev: amdgpu_device pointer
2130 * Query the VBIOS data tables to determine if the board supports SR-IOV.
2132 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
2134 if (amdgpu_sriov_vf(adev)) {
2135 if (adev->is_atom_fw) {
2136 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2137 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2139 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2140 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2143 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
2144 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
2149 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
2151 * @asic_type: AMD asic type
2153 * Check if there is DC (new modesetting infrastructre) support for an asic.
2154 * returns true if DC has support, false if not.
2156 bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
2158 switch (asic_type) {
2159 #if defined(CONFIG_DRM_AMD_DC)
2167 case CHIP_POLARIS10:
2168 case CHIP_POLARIS11:
2169 case CHIP_POLARIS12:
2176 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
2179 return amdgpu_dc != 0;
2187 * amdgpu_device_has_dc_support - check if dc is supported
2189 * @adev: amdgpu_device_pointer
2191 * Returns true for supported, false for not supported
2193 bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
2195 if (amdgpu_sriov_vf(adev))
2198 return amdgpu_device_asic_has_dc_support(adev->asic_type);
2202 * amdgpu_device_init - initialize the driver
2204 * @adev: amdgpu_device pointer
2205 * @pdev: drm dev pointer
2206 * @pdev: pci dev pointer
2207 * @flags: driver flags
2209 * Initializes the driver info and hw (all asics).
2210 * Returns 0 for success or an error on failure.
2211 * Called at driver startup.
2213 int amdgpu_device_init(struct amdgpu_device *adev,
2214 struct drm_device *ddev,
2215 struct pci_dev *pdev,
2219 bool runtime = false;
2222 adev->shutdown = false;
2223 adev->dev = &pdev->dev;
2226 adev->flags = flags;
2227 adev->asic_type = flags & AMD_ASIC_MASK;
2228 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
2229 if (amdgpu_emu_mode == 1)
2230 adev->usec_timeout *= 2;
2231 adev->gmc.gart_size = 512 * 1024 * 1024;
2232 adev->accel_working = false;
2233 adev->num_rings = 0;
2234 adev->mman.buffer_funcs = NULL;
2235 adev->mman.buffer_funcs_ring = NULL;
2236 adev->vm_manager.vm_pte_funcs = NULL;
2237 adev->vm_manager.vm_pte_num_rings = 0;
2238 adev->gmc.gmc_funcs = NULL;
2239 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
2240 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
2242 adev->smc_rreg = &amdgpu_invalid_rreg;
2243 adev->smc_wreg = &amdgpu_invalid_wreg;
2244 adev->pcie_rreg = &amdgpu_invalid_rreg;
2245 adev->pcie_wreg = &amdgpu_invalid_wreg;
2246 adev->pciep_rreg = &amdgpu_invalid_rreg;
2247 adev->pciep_wreg = &amdgpu_invalid_wreg;
2248 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2249 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2250 adev->didt_rreg = &amdgpu_invalid_rreg;
2251 adev->didt_wreg = &amdgpu_invalid_wreg;
2252 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2253 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
2254 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2255 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2257 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2258 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2259 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
2261 /* mutex initialization are all done here so we
2262 * can recall function without having locking issues */
2263 atomic_set(&adev->irq.ih.lock, 0);
2264 mutex_init(&adev->firmware.mutex);
2265 mutex_init(&adev->pm.mutex);
2266 mutex_init(&adev->gfx.gpu_clock_mutex);
2267 mutex_init(&adev->srbm_mutex);
2268 mutex_init(&adev->gfx.pipe_reserve_mutex);
2269 mutex_init(&adev->grbm_idx_mutex);
2270 mutex_init(&adev->mn_lock);
2271 mutex_init(&adev->virt.vf_errors.lock);
2272 hash_init(adev->mn_hash);
2273 mutex_init(&adev->lock_reset);
2275 amdgpu_device_check_arguments(adev);
2277 spin_lock_init(&adev->mmio_idx_lock);
2278 spin_lock_init(&adev->smc_idx_lock);
2279 spin_lock_init(&adev->pcie_idx_lock);
2280 spin_lock_init(&adev->uvd_ctx_idx_lock);
2281 spin_lock_init(&adev->didt_idx_lock);
2282 spin_lock_init(&adev->gc_cac_idx_lock);
2283 spin_lock_init(&adev->se_cac_idx_lock);
2284 spin_lock_init(&adev->audio_endpt_idx_lock);
2285 spin_lock_init(&adev->mm_stats.lock);
2287 INIT_LIST_HEAD(&adev->shadow_list);
2288 mutex_init(&adev->shadow_list_lock);
2290 INIT_LIST_HEAD(&adev->ring_lru_list);
2291 spin_lock_init(&adev->ring_lru_list_lock);
2293 INIT_DELAYED_WORK(&adev->late_init_work,
2294 amdgpu_device_ip_late_init_func_handler);
2296 /* Registers mapping */
2297 /* TODO: block userspace mapping of io register */
2298 if (adev->asic_type >= CHIP_BONAIRE) {
2299 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2300 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2302 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2303 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2306 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2307 if (adev->rmmio == NULL) {
2310 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2311 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2313 /* doorbell bar mapping */
2314 amdgpu_device_doorbell_init(adev);
2316 /* io port mapping */
2317 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2318 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2319 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2320 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2324 if (adev->rio_mem == NULL)
2325 DRM_INFO("PCI I/O BAR is not found.\n");
2327 amdgpu_device_get_pcie_info(adev);
2329 /* early init functions */
2330 r = amdgpu_device_ip_early_init(adev);
2334 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2335 /* this will fail for cards that aren't VGA class devices, just
2337 vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
2339 if (amdgpu_device_is_px(ddev))
2341 if (!pci_is_thunderbolt_attached(adev->pdev))
2342 vga_switcheroo_register_client(adev->pdev,
2343 &amdgpu_switcheroo_ops, runtime);
2345 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2347 if (amdgpu_emu_mode == 1) {
2348 /* post the asic on emulation mode */
2349 emu_soc_asic_init(adev);
2350 goto fence_driver_init;
2354 if (!amdgpu_get_bios(adev)) {
2359 r = amdgpu_atombios_init(adev);
2361 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
2362 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
2366 /* detect if we are with an SRIOV vbios */
2367 amdgpu_device_detect_sriov_bios(adev);
2369 /* Post card if necessary */
2370 if (amdgpu_device_need_post(adev)) {
2372 dev_err(adev->dev, "no vBIOS found\n");
2376 DRM_INFO("GPU posting now...\n");
2377 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2379 dev_err(adev->dev, "gpu post error!\n");
2384 if (adev->is_atom_fw) {
2385 /* Initialize clocks */
2386 r = amdgpu_atomfirmware_get_clock_info(adev);
2388 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
2389 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2393 /* Initialize clocks */
2394 r = amdgpu_atombios_get_clock_info(adev);
2396 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
2397 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2400 /* init i2c buses */
2401 if (!amdgpu_device_has_dc_support(adev))
2402 amdgpu_atombios_i2c_init(adev);
2407 r = amdgpu_fence_driver_init(adev);
2409 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
2410 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
2414 /* init the mode config */
2415 drm_mode_config_init(adev->ddev);
2417 r = amdgpu_device_ip_init(adev);
2419 /* failed in exclusive mode due to timeout */
2420 if (amdgpu_sriov_vf(adev) &&
2421 !amdgpu_sriov_runtime(adev) &&
2422 amdgpu_virt_mmio_blocked(adev) &&
2423 !amdgpu_virt_wait_reset(adev)) {
2424 dev_err(adev->dev, "VF exclusive mode timeout\n");
2425 /* Don't send request since VF is inactive. */
2426 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
2427 adev->virt.ops = NULL;
2431 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
2432 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
2436 adev->accel_working = true;
2438 amdgpu_vm_check_compute_bug(adev);
2440 /* Initialize the buffer migration limit. */
2441 if (amdgpu_moverate >= 0)
2442 max_MBps = amdgpu_moverate;
2444 max_MBps = 8; /* Allow 8 MB/s. */
2445 /* Get a log2 for easy divisions. */
2446 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2448 r = amdgpu_ib_pool_init(adev);
2450 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
2451 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
2455 if (amdgpu_sriov_vf(adev))
2456 amdgpu_virt_init_data_exchange(adev);
2458 amdgpu_fbdev_init(adev);
2460 r = amdgpu_pm_sysfs_init(adev);
2462 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2464 r = amdgpu_debugfs_gem_init(adev);
2466 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
2468 r = amdgpu_debugfs_regs_init(adev);
2470 DRM_ERROR("registering register debugfs failed (%d).\n", r);
2472 r = amdgpu_debugfs_firmware_init(adev);
2474 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
2476 r = amdgpu_debugfs_init(adev);
2478 DRM_ERROR("Creating debugfs files failed (%d).\n", r);
2480 if ((amdgpu_testing & 1)) {
2481 if (adev->accel_working)
2482 amdgpu_test_moves(adev);
2484 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2486 if (amdgpu_benchmarking) {
2487 if (adev->accel_working)
2488 amdgpu_benchmark(adev, amdgpu_benchmarking);
2490 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2493 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2494 * explicit gating rather than handling it automatically.
2496 r = amdgpu_device_ip_late_init(adev);
2498 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
2499 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
2506 amdgpu_vf_error_trans_all(adev);
2508 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2514 * amdgpu_device_fini - tear down the driver
2516 * @adev: amdgpu_device pointer
2518 * Tear down the driver info (all asics).
2519 * Called at driver shutdown.
2521 void amdgpu_device_fini(struct amdgpu_device *adev)
2525 DRM_INFO("amdgpu: finishing device.\n");
2526 adev->shutdown = true;
2527 /* disable all interrupts */
2528 amdgpu_irq_disable_all(adev);
2529 if (adev->mode_info.mode_config_initialized){
2530 if (!amdgpu_device_has_dc_support(adev))
2531 drm_crtc_force_disable_all(adev->ddev);
2533 drm_atomic_helper_shutdown(adev->ddev);
2535 amdgpu_ib_pool_fini(adev);
2536 amdgpu_fence_driver_fini(adev);
2537 amdgpu_pm_sysfs_fini(adev);
2538 amdgpu_fbdev_fini(adev);
2539 r = amdgpu_device_ip_fini(adev);
2540 if (adev->firmware.gpu_info_fw) {
2541 release_firmware(adev->firmware.gpu_info_fw);
2542 adev->firmware.gpu_info_fw = NULL;
2544 adev->accel_working = false;
2545 cancel_delayed_work_sync(&adev->late_init_work);
2546 /* free i2c buses */
2547 if (!amdgpu_device_has_dc_support(adev))
2548 amdgpu_i2c_fini(adev);
2550 if (amdgpu_emu_mode != 1)
2551 amdgpu_atombios_fini(adev);
2555 if (!pci_is_thunderbolt_attached(adev->pdev))
2556 vga_switcheroo_unregister_client(adev->pdev);
2557 if (adev->flags & AMD_IS_PX)
2558 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2559 vga_client_register(adev->pdev, NULL, NULL, NULL);
2561 pci_iounmap(adev->pdev, adev->rio_mem);
2562 adev->rio_mem = NULL;
2563 iounmap(adev->rmmio);
2565 amdgpu_device_doorbell_fini(adev);
2566 amdgpu_debugfs_regs_cleanup(adev);
2574 * amdgpu_device_suspend - initiate device suspend
2576 * @pdev: drm dev pointer
2577 * @state: suspend state
2579 * Puts the hw in the suspend state (all asics).
2580 * Returns 0 for success or an error on failure.
2581 * Called at driver suspend.
2583 int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
2585 struct amdgpu_device *adev;
2586 struct drm_crtc *crtc;
2587 struct drm_connector *connector;
2590 if (dev == NULL || dev->dev_private == NULL) {
2594 adev = dev->dev_private;
2596 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2599 drm_kms_helper_poll_disable(dev);
2601 if (!amdgpu_device_has_dc_support(adev)) {
2602 /* turn off display hw */
2603 drm_modeset_lock_all(dev);
2604 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2605 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2607 drm_modeset_unlock_all(dev);
2610 amdgpu_amdkfd_suspend(adev);
2612 /* unpin the front buffers and cursors */
2613 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2614 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2615 struct drm_framebuffer *fb = crtc->primary->fb;
2616 struct amdgpu_bo *robj;
2618 if (amdgpu_crtc->cursor_bo) {
2619 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2620 r = amdgpu_bo_reserve(aobj, true);
2622 amdgpu_bo_unpin(aobj);
2623 amdgpu_bo_unreserve(aobj);
2627 if (fb == NULL || fb->obj[0] == NULL) {
2630 robj = gem_to_amdgpu_bo(fb->obj[0]);
2631 /* don't unpin kernel fb objects */
2632 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
2633 r = amdgpu_bo_reserve(robj, true);
2635 amdgpu_bo_unpin(robj);
2636 amdgpu_bo_unreserve(robj);
2640 /* evict vram memory */
2641 amdgpu_bo_evict_vram(adev);
2643 amdgpu_fence_driver_suspend(adev);
2645 r = amdgpu_device_ip_suspend(adev);
2647 /* evict remaining vram memory
2648 * This second call to evict vram is to evict the gart page table
2651 amdgpu_bo_evict_vram(adev);
2653 pci_save_state(dev->pdev);
2655 /* Shut down the device */
2656 pci_disable_device(dev->pdev);
2657 pci_set_power_state(dev->pdev, PCI_D3hot);
2659 r = amdgpu_asic_reset(adev);
2661 DRM_ERROR("amdgpu asic reset failed\n");
2666 amdgpu_fbdev_set_suspend(adev, 1);
2673 * amdgpu_device_resume - initiate device resume
2675 * @pdev: drm dev pointer
2677 * Bring the hw back to operating state (all asics).
2678 * Returns 0 for success or an error on failure.
2679 * Called at driver resume.
2681 int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
2683 struct drm_connector *connector;
2684 struct amdgpu_device *adev = dev->dev_private;
2685 struct drm_crtc *crtc;
2688 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2695 pci_set_power_state(dev->pdev, PCI_D0);
2696 pci_restore_state(dev->pdev);
2697 r = pci_enable_device(dev->pdev);
2703 if (amdgpu_device_need_post(adev)) {
2704 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2706 DRM_ERROR("amdgpu asic init failed\n");
2709 r = amdgpu_device_ip_resume(adev);
2711 DRM_ERROR("amdgpu_device_ip_resume failed (%d).\n", r);
2714 amdgpu_fence_driver_resume(adev);
2717 r = amdgpu_device_ip_late_init(adev);
2722 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2723 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2725 if (amdgpu_crtc->cursor_bo) {
2726 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2727 r = amdgpu_bo_reserve(aobj, true);
2729 r = amdgpu_bo_pin(aobj,
2730 AMDGPU_GEM_DOMAIN_VRAM,
2731 &amdgpu_crtc->cursor_addr);
2733 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2734 amdgpu_bo_unreserve(aobj);
2738 r = amdgpu_amdkfd_resume(adev);
2742 /* blat the mode back in */
2744 if (!amdgpu_device_has_dc_support(adev)) {
2746 drm_helper_resume_force_mode(dev);
2748 /* turn on display hw */
2749 drm_modeset_lock_all(dev);
2750 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2751 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2753 drm_modeset_unlock_all(dev);
2757 drm_kms_helper_poll_enable(dev);
2760 * Most of the connector probing functions try to acquire runtime pm
2761 * refs to ensure that the GPU is powered on when connector polling is
2762 * performed. Since we're calling this from a runtime PM callback,
2763 * trying to acquire rpm refs will cause us to deadlock.
2765 * Since we're guaranteed to be holding the rpm lock, it's safe to
2766 * temporarily disable the rpm helpers so this doesn't deadlock us.
2769 dev->dev->power.disable_depth++;
2771 if (!amdgpu_device_has_dc_support(adev))
2772 drm_helper_hpd_irq_event(dev);
2774 drm_kms_helper_hotplug_event(dev);
2776 dev->dev->power.disable_depth--;
2780 amdgpu_fbdev_set_suspend(adev, 0);
2790 * amdgpu_device_ip_check_soft_reset - did soft reset succeed
2792 * @adev: amdgpu_device pointer
2794 * The list of all the hardware IPs that make up the asic is walked and
2795 * the check_soft_reset callbacks are run. check_soft_reset determines
2796 * if the asic is still hung or not.
2797 * Returns true if any of the IPs are still in a hung state, false if not.
2799 static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
2802 bool asic_hang = false;
2804 if (amdgpu_sriov_vf(adev))
2807 if (amdgpu_asic_need_full_reset(adev))
2810 for (i = 0; i < adev->num_ip_blocks; i++) {
2811 if (!adev->ip_blocks[i].status.valid)
2813 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2814 adev->ip_blocks[i].status.hang =
2815 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2816 if (adev->ip_blocks[i].status.hang) {
2817 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
2825 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
2827 * @adev: amdgpu_device pointer
2829 * The list of all the hardware IPs that make up the asic is walked and the
2830 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
2831 * handles any IP specific hardware or software state changes that are
2832 * necessary for a soft reset to succeed.
2833 * Returns 0 on success, negative error code on failure.
2835 static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
2839 for (i = 0; i < adev->num_ip_blocks; i++) {
2840 if (!adev->ip_blocks[i].status.valid)
2842 if (adev->ip_blocks[i].status.hang &&
2843 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2844 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
2854 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
2856 * @adev: amdgpu_device pointer
2858 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
2859 * reset is necessary to recover.
2860 * Returns true if a full asic reset is required, false if not.
2862 static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
2866 if (amdgpu_asic_need_full_reset(adev))
2869 for (i = 0; i < adev->num_ip_blocks; i++) {
2870 if (!adev->ip_blocks[i].status.valid)
2872 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2873 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2874 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2875 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
2876 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
2877 if (adev->ip_blocks[i].status.hang) {
2878 DRM_INFO("Some block need full reset!\n");
2887 * amdgpu_device_ip_soft_reset - do a soft reset
2889 * @adev: amdgpu_device pointer
2891 * The list of all the hardware IPs that make up the asic is walked and the
2892 * soft_reset callbacks are run if the block is hung. soft_reset handles any
2893 * IP specific hardware or software state changes that are necessary to soft
2895 * Returns 0 on success, negative error code on failure.
2897 static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
2901 for (i = 0; i < adev->num_ip_blocks; i++) {
2902 if (!adev->ip_blocks[i].status.valid)
2904 if (adev->ip_blocks[i].status.hang &&
2905 adev->ip_blocks[i].version->funcs->soft_reset) {
2906 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
2916 * amdgpu_device_ip_post_soft_reset - clean up from soft reset
2918 * @adev: amdgpu_device pointer
2920 * The list of all the hardware IPs that make up the asic is walked and the
2921 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
2922 * handles any IP specific hardware or software state changes that are
2923 * necessary after the IP has been soft reset.
2924 * Returns 0 on success, negative error code on failure.
2926 static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
2930 for (i = 0; i < adev->num_ip_blocks; i++) {
2931 if (!adev->ip_blocks[i].status.valid)
2933 if (adev->ip_blocks[i].status.hang &&
2934 adev->ip_blocks[i].version->funcs->post_soft_reset)
2935 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
2944 * amdgpu_device_recover_vram_from_shadow - restore shadowed VRAM buffers
2946 * @adev: amdgpu_device pointer
2947 * @ring: amdgpu_ring for the engine handling the buffer operations
2948 * @bo: amdgpu_bo buffer whose shadow is being restored
2949 * @fence: dma_fence associated with the operation
2951 * Restores the VRAM buffer contents from the shadow in GTT. Used to
2952 * restore things like GPUVM page tables after a GPU reset where
2953 * the contents of VRAM might be lost.
2954 * Returns 0 on success, negative error code on failure.
2956 static int amdgpu_device_recover_vram_from_shadow(struct amdgpu_device *adev,
2957 struct amdgpu_ring *ring,
2958 struct amdgpu_bo *bo,
2959 struct dma_fence **fence)
2967 r = amdgpu_bo_reserve(bo, true);
2970 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2971 /* if bo has been evicted, then no need to recover */
2972 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
2973 r = amdgpu_bo_validate(bo->shadow);
2975 DRM_ERROR("bo validate failed!\n");
2979 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
2982 DRM_ERROR("recover page table failed!\n");
2987 amdgpu_bo_unreserve(bo);
2992 * amdgpu_device_handle_vram_lost - Handle the loss of VRAM contents
2994 * @adev: amdgpu_device pointer
2996 * Restores the contents of VRAM buffers from the shadows in GTT. Used to
2997 * restore things like GPUVM page tables after a GPU reset where
2998 * the contents of VRAM might be lost.
2999 * Returns 0 on success, 1 on failure.
3001 static int amdgpu_device_handle_vram_lost(struct amdgpu_device *adev)
3003 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
3004 struct amdgpu_bo *bo, *tmp;
3005 struct dma_fence *fence = NULL, *next = NULL;
3010 if (amdgpu_sriov_runtime(adev))
3011 tmo = msecs_to_jiffies(amdgpu_lockup_timeout);
3013 tmo = msecs_to_jiffies(100);
3015 DRM_INFO("recover vram bo from shadow start\n");
3016 mutex_lock(&adev->shadow_list_lock);
3017 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
3019 amdgpu_device_recover_vram_from_shadow(adev, ring, bo, &next);
3021 r = dma_fence_wait_timeout(fence, false, tmo);
3023 pr_err("wait fence %p[%d] timeout\n", fence, i);
3025 pr_err("wait fence %p[%d] interrupted\n", fence, i);
3027 dma_fence_put(fence);
3034 dma_fence_put(fence);
3037 mutex_unlock(&adev->shadow_list_lock);
3040 r = dma_fence_wait_timeout(fence, false, tmo);
3042 pr_err("wait fence %p[%d] timeout\n", fence, i);
3044 pr_err("wait fence %p[%d] interrupted\n", fence, i);
3047 dma_fence_put(fence);
3050 DRM_INFO("recover vram bo from shadow done\n");
3052 DRM_ERROR("recover vram bo from shadow failed\n");
3054 return (r > 0) ? 0 : 1;
3058 * amdgpu_device_reset - reset ASIC/GPU for bare-metal or passthrough
3060 * @adev: amdgpu device pointer
3062 * attempt to do soft-reset or full-reset and reinitialize Asic
3063 * return 0 means successed otherwise failed
3065 static int amdgpu_device_reset(struct amdgpu_device *adev)
3067 bool need_full_reset, vram_lost = 0;
3070 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
3072 if (!need_full_reset) {
3073 amdgpu_device_ip_pre_soft_reset(adev);
3074 r = amdgpu_device_ip_soft_reset(adev);
3075 amdgpu_device_ip_post_soft_reset(adev);
3076 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
3077 DRM_INFO("soft reset failed, will fallback to full reset!\n");
3078 need_full_reset = true;
3082 if (need_full_reset) {
3083 r = amdgpu_device_ip_suspend(adev);
3086 r = amdgpu_asic_reset(adev);
3088 amdgpu_atom_asic_init(adev->mode_info.atom_context);
3091 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
3092 r = amdgpu_device_ip_resume_phase1(adev);
3096 vram_lost = amdgpu_device_check_vram_lost(adev);
3098 DRM_ERROR("VRAM is lost!\n");
3099 atomic_inc(&adev->vram_lost_counter);
3102 r = amdgpu_gtt_mgr_recover(
3103 &adev->mman.bdev.man[TTM_PL_TT]);
3107 r = amdgpu_device_ip_resume_phase2(adev);
3112 amdgpu_device_fill_reset_magic(adev);
3118 amdgpu_irq_gpu_reset_resume_helper(adev);
3119 r = amdgpu_ib_ring_tests(adev);
3121 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
3122 r = amdgpu_device_ip_suspend(adev);
3123 need_full_reset = true;
3128 if (!r && ((need_full_reset && !(adev->flags & AMD_IS_APU)) || vram_lost))
3129 r = amdgpu_device_handle_vram_lost(adev);
3135 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
3137 * @adev: amdgpu device pointer
3139 * do VF FLR and reinitialize Asic
3140 * return 0 means successed otherwise failed
3142 static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
3143 bool from_hypervisor)
3147 if (from_hypervisor)
3148 r = amdgpu_virt_request_full_gpu(adev, true);
3150 r = amdgpu_virt_reset_gpu(adev);
3154 /* Resume IP prior to SMC */
3155 r = amdgpu_device_ip_reinit_early_sriov(adev);
3159 /* we need recover gart prior to run SMC/CP/SDMA resume */
3160 amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
3162 /* now we are okay to resume SMC/CP/SDMA */
3163 r = amdgpu_device_ip_reinit_late_sriov(adev);
3167 amdgpu_irq_gpu_reset_resume_helper(adev);
3168 r = amdgpu_ib_ring_tests(adev);
3171 amdgpu_virt_release_full_gpu(adev, true);
3172 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
3173 atomic_inc(&adev->vram_lost_counter);
3174 r = amdgpu_device_handle_vram_lost(adev);
3181 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
3183 * @adev: amdgpu device pointer
3184 * @job: which job trigger hang
3185 * @force forces reset regardless of amdgpu_gpu_recovery
3187 * Attempt to reset the GPU if it has hung (all asics).
3188 * Returns 0 for success or an error on failure.
3190 int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
3191 struct amdgpu_job *job, bool force)
3195 if (!force && !amdgpu_device_ip_check_soft_reset(adev)) {
3196 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
3200 if (!force && (amdgpu_gpu_recovery == 0 ||
3201 (amdgpu_gpu_recovery == -1 && !amdgpu_sriov_vf(adev)))) {
3202 DRM_INFO("GPU recovery disabled.\n");
3206 dev_info(adev->dev, "GPU reset begin!\n");
3208 mutex_lock(&adev->lock_reset);
3209 atomic_inc(&adev->gpu_reset_counter);
3210 adev->in_gpu_reset = 1;
3213 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
3215 /* block all schedulers and reset given job's ring */
3216 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3217 struct amdgpu_ring *ring = adev->rings[i];
3219 if (!ring || !ring->sched.thread)
3222 kthread_park(ring->sched.thread);
3224 if (job && job->ring->idx != i)
3227 drm_sched_hw_job_reset(&ring->sched, &job->base);
3229 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
3230 amdgpu_fence_driver_force_completion(ring);
3233 if (amdgpu_sriov_vf(adev))
3234 r = amdgpu_device_reset_sriov(adev, job ? false : true);
3236 r = amdgpu_device_reset(adev);
3238 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3239 struct amdgpu_ring *ring = adev->rings[i];
3241 if (!ring || !ring->sched.thread)
3244 /* only need recovery sched of the given job's ring
3245 * or all rings (in the case @job is NULL)
3246 * after above amdgpu_reset accomplished
3248 if ((!job || job->ring->idx == i) && !r)
3249 drm_sched_job_recovery(&ring->sched);
3251 kthread_unpark(ring->sched.thread);
3254 if (!amdgpu_device_has_dc_support(adev)) {
3255 drm_helper_resume_force_mode(adev->ddev);
3258 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
3261 /* bad news, how to tell it to userspace ? */
3262 dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
3263 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
3265 dev_info(adev->dev, "GPU reset(%d) successed!\n",atomic_read(&adev->gpu_reset_counter));
3268 amdgpu_vf_error_trans_all(adev);
3269 adev->in_gpu_reset = 0;
3270 mutex_unlock(&adev->lock_reset);
3275 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
3277 * @adev: amdgpu_device pointer
3279 * Fetchs and stores in the driver the PCIE capabilities (gen speed
3280 * and lanes) of the slot the device is in. Handles APUs and
3281 * virtualized environments where PCIE config space may not be available.
3283 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
3288 if (amdgpu_pcie_gen_cap)
3289 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
3291 if (amdgpu_pcie_lane_cap)
3292 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
3294 /* covers APUs as well */
3295 if (pci_is_root_bus(adev->pdev->bus)) {
3296 if (adev->pm.pcie_gen_mask == 0)
3297 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3298 if (adev->pm.pcie_mlw_mask == 0)
3299 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
3303 if (adev->pm.pcie_gen_mask == 0) {
3304 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
3306 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3307 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3308 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3310 if (mask & DRM_PCIE_SPEED_25)
3311 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
3312 if (mask & DRM_PCIE_SPEED_50)
3313 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
3314 if (mask & DRM_PCIE_SPEED_80)
3315 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
3317 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3320 if (adev->pm.pcie_mlw_mask == 0) {
3321 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
3325 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3326 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3327 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3328 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3329 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3330 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3331 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3334 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3335 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3336 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3337 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3338 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3339 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3342 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3343 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3344 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3345 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3346 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3349 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3350 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3351 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3352 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3355 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3356 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3357 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3360 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3361 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3364 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3370 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;