2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
24 * Authors: Dave Airlie
28 #include <linux/power_supply.h>
29 #include <linux/kthread.h>
30 #include <linux/console.h>
31 #include <linux/slab.h>
33 #include <drm/drm_crtc_helper.h>
34 #include <drm/drm_atomic_helper.h>
35 #include <drm/amdgpu_drm.h>
36 #include <linux/vgaarb.h>
37 #include <linux/vga_switcheroo.h>
38 #include <linux/efi.h>
40 #include "amdgpu_trace.h"
41 #include "amdgpu_i2c.h"
43 #include "amdgpu_atombios.h"
44 #include "amdgpu_atomfirmware.h"
46 #ifdef CONFIG_DRM_AMDGPU_SI
49 #ifdef CONFIG_DRM_AMDGPU_CIK
54 #include "bif/bif_4_1_d.h"
55 #include <linux/pci.h>
56 #include <linux/firmware.h>
57 #include "amdgpu_vf_error.h"
59 #include "amdgpu_amdkfd.h"
60 #include "amdgpu_pm.h"
62 MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
63 MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
64 MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
66 #define AMDGPU_RESUME_MS 2000
68 static const char *amdgpu_asic_name[] = {
95 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev);
98 * amdgpu_device_is_px - Is the device is a dGPU with HG/PX power control
100 * @dev: drm_device pointer
102 * Returns true if the device is a dGPU with HG/PX power control,
103 * otherwise return false.
105 bool amdgpu_device_is_px(struct drm_device *dev)
107 struct amdgpu_device *adev = dev->dev_private;
109 if (adev->flags & AMD_IS_PX)
115 * MMIO register access helper functions.
118 * amdgpu_mm_rreg - read a memory mapped IO register
120 * @adev: amdgpu_device pointer
121 * @reg: dword aligned register offset
122 * @acc_flags: access flags which require special behavior
124 * Returns the 32 bit value from the offset specified.
126 uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
131 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
132 return amdgpu_virt_kiq_rreg(adev, reg);
134 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
135 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
139 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
140 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
141 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
142 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
144 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
149 * MMIO register read with bytes helper functions
150 * @offset:bytes offset from MMIO start
155 * amdgpu_mm_rreg8 - read a memory mapped IO register
157 * @adev: amdgpu_device pointer
158 * @offset: byte aligned register offset
160 * Returns the 8 bit value from the offset specified.
162 uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset) {
163 if (offset < adev->rmmio_size)
164 return (readb(adev->rmmio + offset));
169 * MMIO register write with bytes helper functions
170 * @offset:bytes offset from MMIO start
171 * @value: the value want to be written to the register
175 * amdgpu_mm_wreg8 - read a memory mapped IO register
177 * @adev: amdgpu_device pointer
178 * @offset: byte aligned register offset
179 * @value: 8 bit value to write
181 * Writes the value specified to the offset specified.
183 void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value) {
184 if (offset < adev->rmmio_size)
185 writeb(value, adev->rmmio + offset);
191 * amdgpu_mm_wreg - write to a memory mapped IO register
193 * @adev: amdgpu_device pointer
194 * @reg: dword aligned register offset
195 * @v: 32 bit value to write to the register
196 * @acc_flags: access flags which require special behavior
198 * Writes the value specified to the offset specified.
200 void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
203 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
205 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
206 adev->last_mm_index = v;
209 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
210 return amdgpu_virt_kiq_wreg(adev, reg, v);
212 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
213 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
217 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
218 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
219 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
220 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
223 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
229 * amdgpu_io_rreg - read an IO register
231 * @adev: amdgpu_device pointer
232 * @reg: dword aligned register offset
234 * Returns the 32 bit value from the offset specified.
236 u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
238 if ((reg * 4) < adev->rio_mem_size)
239 return ioread32(adev->rio_mem + (reg * 4));
241 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
242 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
247 * amdgpu_io_wreg - write to an IO register
249 * @adev: amdgpu_device pointer
250 * @reg: dword aligned register offset
251 * @v: 32 bit value to write to the register
253 * Writes the value specified to the offset specified.
255 void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
257 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
258 adev->last_mm_index = v;
261 if ((reg * 4) < adev->rio_mem_size)
262 iowrite32(v, adev->rio_mem + (reg * 4));
264 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
265 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
268 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
274 * amdgpu_mm_rdoorbell - read a doorbell dword
276 * @adev: amdgpu_device pointer
277 * @index: doorbell index
279 * Returns the value in the doorbell aperture at the
280 * requested doorbell index (CIK).
282 u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
284 if (index < adev->doorbell.num_doorbells) {
285 return readl(adev->doorbell.ptr + index);
287 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
293 * amdgpu_mm_wdoorbell - write a doorbell dword
295 * @adev: amdgpu_device pointer
296 * @index: doorbell index
299 * Writes @v to the doorbell aperture at the
300 * requested doorbell index (CIK).
302 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
304 if (index < adev->doorbell.num_doorbells) {
305 writel(v, adev->doorbell.ptr + index);
307 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
312 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
314 * @adev: amdgpu_device pointer
315 * @index: doorbell index
317 * Returns the value in the doorbell aperture at the
318 * requested doorbell index (VEGA10+).
320 u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
322 if (index < adev->doorbell.num_doorbells) {
323 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
325 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
331 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
333 * @adev: amdgpu_device pointer
334 * @index: doorbell index
337 * Writes @v to the doorbell aperture at the
338 * requested doorbell index (VEGA10+).
340 void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
342 if (index < adev->doorbell.num_doorbells) {
343 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
345 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
350 * amdgpu_invalid_rreg - dummy reg read function
352 * @adev: amdgpu device pointer
353 * @reg: offset of register
355 * Dummy register read function. Used for register blocks
356 * that certain asics don't have (all asics).
357 * Returns the value in the register.
359 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
361 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
367 * amdgpu_invalid_wreg - dummy reg write function
369 * @adev: amdgpu device pointer
370 * @reg: offset of register
371 * @v: value to write to the register
373 * Dummy register read function. Used for register blocks
374 * that certain asics don't have (all asics).
376 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
378 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
384 * amdgpu_block_invalid_rreg - dummy reg read function
386 * @adev: amdgpu device pointer
387 * @block: offset of instance
388 * @reg: offset of register
390 * Dummy register read function. Used for register blocks
391 * that certain asics don't have (all asics).
392 * Returns the value in the register.
394 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
395 uint32_t block, uint32_t reg)
397 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
404 * amdgpu_block_invalid_wreg - dummy reg write function
406 * @adev: amdgpu device pointer
407 * @block: offset of instance
408 * @reg: offset of register
409 * @v: value to write to the register
411 * Dummy register read function. Used for register blocks
412 * that certain asics don't have (all asics).
414 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
416 uint32_t reg, uint32_t v)
418 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
424 * amdgpu_device_vram_scratch_init - allocate the VRAM scratch page
426 * @adev: amdgpu device pointer
428 * Allocates a scratch page of VRAM for use by various things in the
431 static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev)
433 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
434 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
435 &adev->vram_scratch.robj,
436 &adev->vram_scratch.gpu_addr,
437 (void **)&adev->vram_scratch.ptr);
441 * amdgpu_device_vram_scratch_fini - Free the VRAM scratch page
443 * @adev: amdgpu device pointer
445 * Frees the VRAM scratch page.
447 static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev)
449 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
453 * amdgpu_device_program_register_sequence - program an array of registers.
455 * @adev: amdgpu_device pointer
456 * @registers: pointer to the register array
457 * @array_size: size of the register array
459 * Programs an array or registers with and and or masks.
460 * This is a helper for setting golden registers.
462 void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
463 const u32 *registers,
464 const u32 array_size)
466 u32 tmp, reg, and_mask, or_mask;
472 for (i = 0; i < array_size; i +=3) {
473 reg = registers[i + 0];
474 and_mask = registers[i + 1];
475 or_mask = registers[i + 2];
477 if (and_mask == 0xffffffff) {
489 * amdgpu_device_pci_config_reset - reset the GPU
491 * @adev: amdgpu_device pointer
493 * Resets the GPU using the pci config reset sequence.
494 * Only applicable to asics prior to vega10.
496 void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
498 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
502 * GPU doorbell aperture helpers function.
505 * amdgpu_device_doorbell_init - Init doorbell driver information.
507 * @adev: amdgpu_device pointer
509 * Init doorbell driver information (CIK)
510 * Returns 0 on success, error on failure.
512 static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
514 /* No doorbell on SI hardware generation */
515 if (adev->asic_type < CHIP_BONAIRE) {
516 adev->doorbell.base = 0;
517 adev->doorbell.size = 0;
518 adev->doorbell.num_doorbells = 0;
519 adev->doorbell.ptr = NULL;
523 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
526 /* doorbell bar mapping */
527 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
528 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
530 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
531 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
532 if (adev->doorbell.num_doorbells == 0)
535 adev->doorbell.ptr = ioremap(adev->doorbell.base,
536 adev->doorbell.num_doorbells *
538 if (adev->doorbell.ptr == NULL)
545 * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
547 * @adev: amdgpu_device pointer
549 * Tear down doorbell driver information (CIK)
551 static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
553 iounmap(adev->doorbell.ptr);
554 adev->doorbell.ptr = NULL;
560 * amdgpu_device_wb_*()
561 * Writeback is the method by which the GPU updates special pages in memory
562 * with the status of certain GPU events (fences, ring pointers,etc.).
566 * amdgpu_device_wb_fini - Disable Writeback and free memory
568 * @adev: amdgpu_device pointer
570 * Disables Writeback and frees the Writeback memory (all asics).
571 * Used at driver shutdown.
573 static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
575 if (adev->wb.wb_obj) {
576 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
578 (void **)&adev->wb.wb);
579 adev->wb.wb_obj = NULL;
584 * amdgpu_device_wb_init- Init Writeback driver info and allocate memory
586 * @adev: amdgpu_device pointer
588 * Initializes writeback and allocates writeback memory (all asics).
589 * Used at driver startup.
590 * Returns 0 on success or an -error on failure.
592 static int amdgpu_device_wb_init(struct amdgpu_device *adev)
596 if (adev->wb.wb_obj == NULL) {
597 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
598 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
599 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
600 &adev->wb.wb_obj, &adev->wb.gpu_addr,
601 (void **)&adev->wb.wb);
603 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
607 adev->wb.num_wb = AMDGPU_MAX_WB;
608 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
610 /* clear wb memory */
611 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
618 * amdgpu_device_wb_get - Allocate a wb entry
620 * @adev: amdgpu_device pointer
623 * Allocate a wb slot for use by the driver (all asics).
624 * Returns 0 on success or -EINVAL on failure.
626 int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
628 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
630 if (offset < adev->wb.num_wb) {
631 __set_bit(offset, adev->wb.used);
632 *wb = offset << 3; /* convert to dw offset */
640 * amdgpu_device_wb_free - Free a wb entry
642 * @adev: amdgpu_device pointer
645 * Free a wb slot allocated for use by the driver (all asics)
647 void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
650 if (wb < adev->wb.num_wb)
651 __clear_bit(wb, adev->wb.used);
655 * amdgpu_device_vram_location - try to find VRAM location
657 * @adev: amdgpu device structure holding all necessary informations
658 * @mc: memory controller structure holding memory informations
659 * @base: base address at which to put VRAM
661 * Function will try to place VRAM at base address provided
664 void amdgpu_device_vram_location(struct amdgpu_device *adev,
665 struct amdgpu_gmc *mc, u64 base)
667 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
669 mc->vram_start = base;
670 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
671 if (limit && limit < mc->real_vram_size)
672 mc->real_vram_size = limit;
673 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
674 mc->mc_vram_size >> 20, mc->vram_start,
675 mc->vram_end, mc->real_vram_size >> 20);
679 * amdgpu_device_gart_location - try to find GART location
681 * @adev: amdgpu device structure holding all necessary informations
682 * @mc: memory controller structure holding memory informations
684 * Function will place try to place GART before or after VRAM.
686 * If GART size is bigger than space left then we ajust GART size.
687 * Thus function will never fails.
689 void amdgpu_device_gart_location(struct amdgpu_device *adev,
690 struct amdgpu_gmc *mc)
692 u64 size_af, size_bf;
694 mc->gart_size += adev->pm.smu_prv_buffer_size;
696 size_af = adev->gmc.mc_mask - mc->vram_end;
697 size_bf = mc->vram_start;
698 if (size_bf > size_af) {
699 if (mc->gart_size > size_bf) {
700 dev_warn(adev->dev, "limiting GART\n");
701 mc->gart_size = size_bf;
705 if (mc->gart_size > size_af) {
706 dev_warn(adev->dev, "limiting GART\n");
707 mc->gart_size = size_af;
709 /* VCE doesn't like it when BOs cross a 4GB segment, so align
710 * the GART base on a 4GB boundary as well.
712 mc->gart_start = ALIGN(mc->vram_end + 1, 0x100000000ULL);
714 mc->gart_end = mc->gart_start + mc->gart_size - 1;
715 dev_info(adev->dev, "GART: %lluM 0x%016llX - 0x%016llX\n",
716 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
720 * amdgpu_device_resize_fb_bar - try to resize FB BAR
722 * @adev: amdgpu_device pointer
724 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
725 * to fail, but if any of the BARs is not accessible after the size we abort
726 * driver loading by returning -ENODEV.
728 int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
730 u64 space_needed = roundup_pow_of_two(adev->gmc.real_vram_size);
731 u32 rbar_size = order_base_2(((space_needed >> 20) | 1)) - 1;
732 struct pci_bus *root;
733 struct resource *res;
739 if (amdgpu_sriov_vf(adev))
742 /* Check if the root BUS has 64bit memory resources */
743 root = adev->pdev->bus;
747 pci_bus_for_each_resource(root, res, i) {
748 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
749 res->start > 0x100000000ull)
753 /* Trying to resize is pointless without a root hub window above 4GB */
757 /* Disable memory decoding while we change the BAR addresses and size */
758 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
759 pci_write_config_word(adev->pdev, PCI_COMMAND,
760 cmd & ~PCI_COMMAND_MEMORY);
762 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
763 amdgpu_device_doorbell_fini(adev);
764 if (adev->asic_type >= CHIP_BONAIRE)
765 pci_release_resource(adev->pdev, 2);
767 pci_release_resource(adev->pdev, 0);
769 r = pci_resize_resource(adev->pdev, 0, rbar_size);
771 DRM_INFO("Not enough PCI address space for a large BAR.");
772 else if (r && r != -ENOTSUPP)
773 DRM_ERROR("Problem resizing BAR0 (%d).", r);
775 pci_assign_unassigned_bus_resources(adev->pdev->bus);
777 /* When the doorbell or fb BAR isn't available we have no chance of
780 r = amdgpu_device_doorbell_init(adev);
781 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
784 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
790 * GPU helpers function.
793 * amdgpu_device_need_post - check if the hw need post or not
795 * @adev: amdgpu_device pointer
797 * Check if the asic has been initialized (all asics) at driver startup
798 * or post is needed if hw reset is performed.
799 * Returns true if need or false if not.
801 bool amdgpu_device_need_post(struct amdgpu_device *adev)
805 if (amdgpu_sriov_vf(adev))
808 if (amdgpu_passthrough(adev)) {
809 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
810 * some old smc fw still need driver do vPost otherwise gpu hang, while
811 * those smc fw version above 22.15 doesn't have this flaw, so we force
812 * vpost executed for smc version below 22.15
814 if (adev->asic_type == CHIP_FIJI) {
817 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
818 /* force vPost if error occured */
822 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
823 if (fw_ver < 0x00160e00)
828 if (adev->has_hw_reset) {
829 adev->has_hw_reset = false;
833 /* bios scratch used on CIK+ */
834 if (adev->asic_type >= CHIP_BONAIRE)
835 return amdgpu_atombios_scratch_need_asic_init(adev);
837 /* check MEM_SIZE for older asics */
838 reg = amdgpu_asic_get_config_memsize(adev);
840 if ((reg != 0) && (reg != 0xffffffff))
846 /* if we get transitioned to only one device, take VGA back */
848 * amdgpu_device_vga_set_decode - enable/disable vga decode
850 * @cookie: amdgpu_device pointer
851 * @state: enable/disable vga decode
853 * Enable/disable vga decode (all asics).
854 * Returns VGA resource flags.
856 static unsigned int amdgpu_device_vga_set_decode(void *cookie, bool state)
858 struct amdgpu_device *adev = cookie;
859 amdgpu_asic_set_vga_state(adev, state);
861 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
862 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
864 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
868 * amdgpu_device_check_block_size - validate the vm block size
870 * @adev: amdgpu_device pointer
872 * Validates the vm block size specified via module parameter.
873 * The vm block size defines number of bits in page table versus page directory,
874 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
875 * page table and the remaining bits are in the page directory.
877 static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
879 /* defines number of bits in page table versus page directory,
880 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
881 * page table and the remaining bits are in the page directory */
882 if (amdgpu_vm_block_size == -1)
885 if (amdgpu_vm_block_size < 9) {
886 dev_warn(adev->dev, "VM page table size (%d) too small\n",
887 amdgpu_vm_block_size);
888 amdgpu_vm_block_size = -1;
893 * amdgpu_device_check_vm_size - validate the vm size
895 * @adev: amdgpu_device pointer
897 * Validates the vm size in GB specified via module parameter.
898 * The VM size is the size of the GPU virtual memory space in GB.
900 static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
902 /* no need to check the default value */
903 if (amdgpu_vm_size == -1)
906 if (amdgpu_vm_size < 1) {
907 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
913 static void amdgpu_device_check_smu_prv_buffer_size(struct amdgpu_device *adev)
916 bool is_os_64 = (sizeof(void *) == 8) ? true : false;
917 uint64_t total_memory;
918 uint64_t dram_size_seven_GB = 0x1B8000000;
919 uint64_t dram_size_three_GB = 0xB8000000;
921 if (amdgpu_smu_memory_pool_size == 0)
925 DRM_WARN("Not 64-bit OS, feature not supported\n");
929 total_memory = (uint64_t)si.totalram * si.mem_unit;
931 if ((amdgpu_smu_memory_pool_size == 1) ||
932 (amdgpu_smu_memory_pool_size == 2)) {
933 if (total_memory < dram_size_three_GB)
935 } else if ((amdgpu_smu_memory_pool_size == 4) ||
936 (amdgpu_smu_memory_pool_size == 8)) {
937 if (total_memory < dram_size_seven_GB)
940 DRM_WARN("Smu memory pool size not supported\n");
943 adev->pm.smu_prv_buffer_size = amdgpu_smu_memory_pool_size << 28;
948 DRM_WARN("No enough system memory\n");
950 adev->pm.smu_prv_buffer_size = 0;
954 * amdgpu_device_check_arguments - validate module params
956 * @adev: amdgpu_device pointer
958 * Validates certain module parameters and updates
959 * the associated values used by the driver (all asics).
961 static void amdgpu_device_check_arguments(struct amdgpu_device *adev)
963 if (amdgpu_sched_jobs < 4) {
964 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
966 amdgpu_sched_jobs = 4;
967 } else if (!is_power_of_2(amdgpu_sched_jobs)){
968 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
970 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
973 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
974 /* gart size must be greater or equal to 32M */
975 dev_warn(adev->dev, "gart size (%d) too small\n",
977 amdgpu_gart_size = -1;
980 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
981 /* gtt size must be greater or equal to 32M */
982 dev_warn(adev->dev, "gtt size (%d) too small\n",
984 amdgpu_gtt_size = -1;
987 /* valid range is between 4 and 9 inclusive */
988 if (amdgpu_vm_fragment_size != -1 &&
989 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
990 dev_warn(adev->dev, "valid range is between 4 and 9\n");
991 amdgpu_vm_fragment_size = -1;
994 amdgpu_device_check_smu_prv_buffer_size(adev);
996 amdgpu_device_check_vm_size(adev);
998 amdgpu_device_check_block_size(adev);
1000 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
1001 !is_power_of_2(amdgpu_vram_page_split))) {
1002 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1003 amdgpu_vram_page_split);
1004 amdgpu_vram_page_split = 1024;
1007 if (amdgpu_lockup_timeout == 0) {
1008 dev_warn(adev->dev, "lockup_timeout msut be > 0, adjusting to 10000\n");
1009 amdgpu_lockup_timeout = 10000;
1012 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
1016 * amdgpu_switcheroo_set_state - set switcheroo state
1018 * @pdev: pci dev pointer
1019 * @state: vga_switcheroo state
1021 * Callback for the switcheroo driver. Suspends or resumes the
1022 * the asics before or after it is powered up using ACPI methods.
1024 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1026 struct drm_device *dev = pci_get_drvdata(pdev);
1028 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1031 if (state == VGA_SWITCHEROO_ON) {
1032 pr_info("amdgpu: switched on\n");
1033 /* don't suspend or resume card normally */
1034 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1036 amdgpu_device_resume(dev, true, true);
1038 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1039 drm_kms_helper_poll_enable(dev);
1041 pr_info("amdgpu: switched off\n");
1042 drm_kms_helper_poll_disable(dev);
1043 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1044 amdgpu_device_suspend(dev, true, true);
1045 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1050 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1052 * @pdev: pci dev pointer
1054 * Callback for the switcheroo driver. Check of the switcheroo
1055 * state can be changed.
1056 * Returns true if the state can be changed, false if not.
1058 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1060 struct drm_device *dev = pci_get_drvdata(pdev);
1063 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1064 * locking inversion with the driver load path. And the access here is
1065 * completely racy anyway. So don't bother with locking for now.
1067 return dev->open_count == 0;
1070 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1071 .set_gpu_state = amdgpu_switcheroo_set_state,
1073 .can_switch = amdgpu_switcheroo_can_switch,
1077 * amdgpu_device_ip_set_clockgating_state - set the CG state
1079 * @dev: amdgpu_device pointer
1080 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1081 * @state: clockgating state (gate or ungate)
1083 * Sets the requested clockgating state for all instances of
1084 * the hardware IP specified.
1085 * Returns the error code from the last instance.
1087 int amdgpu_device_ip_set_clockgating_state(void *dev,
1088 enum amd_ip_block_type block_type,
1089 enum amd_clockgating_state state)
1091 struct amdgpu_device *adev = dev;
1094 for (i = 0; i < adev->num_ip_blocks; i++) {
1095 if (!adev->ip_blocks[i].status.valid)
1097 if (adev->ip_blocks[i].version->type != block_type)
1099 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1101 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1102 (void *)adev, state);
1104 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1105 adev->ip_blocks[i].version->funcs->name, r);
1111 * amdgpu_device_ip_set_powergating_state - set the PG state
1113 * @dev: amdgpu_device pointer
1114 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1115 * @state: powergating state (gate or ungate)
1117 * Sets the requested powergating state for all instances of
1118 * the hardware IP specified.
1119 * Returns the error code from the last instance.
1121 int amdgpu_device_ip_set_powergating_state(void *dev,
1122 enum amd_ip_block_type block_type,
1123 enum amd_powergating_state state)
1125 struct amdgpu_device *adev = dev;
1128 for (i = 0; i < adev->num_ip_blocks; i++) {
1129 if (!adev->ip_blocks[i].status.valid)
1131 if (adev->ip_blocks[i].version->type != block_type)
1133 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1135 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1136 (void *)adev, state);
1138 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1139 adev->ip_blocks[i].version->funcs->name, r);
1145 * amdgpu_device_ip_get_clockgating_state - get the CG state
1147 * @adev: amdgpu_device pointer
1148 * @flags: clockgating feature flags
1150 * Walks the list of IPs on the device and updates the clockgating
1151 * flags for each IP.
1152 * Updates @flags with the feature flags for each hardware IP where
1153 * clockgating is enabled.
1155 void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
1160 for (i = 0; i < adev->num_ip_blocks; i++) {
1161 if (!adev->ip_blocks[i].status.valid)
1163 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1164 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1169 * amdgpu_device_ip_wait_for_idle - wait for idle
1171 * @adev: amdgpu_device pointer
1172 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1174 * Waits for the request hardware IP to be idle.
1175 * Returns 0 for success or a negative error code on failure.
1177 int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1178 enum amd_ip_block_type block_type)
1182 for (i = 0; i < adev->num_ip_blocks; i++) {
1183 if (!adev->ip_blocks[i].status.valid)
1185 if (adev->ip_blocks[i].version->type == block_type) {
1186 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
1197 * amdgpu_device_ip_is_idle - is the hardware IP idle
1199 * @adev: amdgpu_device pointer
1200 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1202 * Check if the hardware IP is idle or not.
1203 * Returns true if it the IP is idle, false if not.
1205 bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1206 enum amd_ip_block_type block_type)
1210 for (i = 0; i < adev->num_ip_blocks; i++) {
1211 if (!adev->ip_blocks[i].status.valid)
1213 if (adev->ip_blocks[i].version->type == block_type)
1214 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
1221 * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1223 * @adev: amdgpu_device pointer
1224 * @type: Type of hardware IP (SMU, GFX, UVD, etc.)
1226 * Returns a pointer to the hardware IP block structure
1227 * if it exists for the asic, otherwise NULL.
1229 struct amdgpu_ip_block *
1230 amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1231 enum amd_ip_block_type type)
1235 for (i = 0; i < adev->num_ip_blocks; i++)
1236 if (adev->ip_blocks[i].version->type == type)
1237 return &adev->ip_blocks[i];
1243 * amdgpu_device_ip_block_version_cmp
1245 * @adev: amdgpu_device pointer
1246 * @type: enum amd_ip_block_type
1247 * @major: major version
1248 * @minor: minor version
1250 * return 0 if equal or greater
1251 * return 1 if smaller or the ip_block doesn't exist
1253 int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1254 enum amd_ip_block_type type,
1255 u32 major, u32 minor)
1257 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
1259 if (ip_block && ((ip_block->version->major > major) ||
1260 ((ip_block->version->major == major) &&
1261 (ip_block->version->minor >= minor))))
1268 * amdgpu_device_ip_block_add
1270 * @adev: amdgpu_device pointer
1271 * @ip_block_version: pointer to the IP to add
1273 * Adds the IP block driver information to the collection of IPs
1276 int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1277 const struct amdgpu_ip_block_version *ip_block_version)
1279 if (!ip_block_version)
1282 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
1283 ip_block_version->funcs->name);
1285 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1291 * amdgpu_device_enable_virtual_display - enable virtual display feature
1293 * @adev: amdgpu_device pointer
1295 * Enabled the virtual display feature if the user has enabled it via
1296 * the module parameter virtual_display. This feature provides a virtual
1297 * display hardware on headless boards or in virtualized environments.
1298 * This function parses and validates the configuration string specified by
1299 * the user and configues the virtual display configuration (number of
1300 * virtual connectors, crtcs, etc.) specified.
1302 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
1304 adev->enable_virtual_display = false;
1306 if (amdgpu_virtual_display) {
1307 struct drm_device *ddev = adev->ddev;
1308 const char *pci_address_name = pci_name(ddev->pdev);
1309 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
1311 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1312 pciaddstr_tmp = pciaddstr;
1313 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1314 pciaddname = strsep(&pciaddname_tmp, ",");
1315 if (!strcmp("all", pciaddname)
1316 || !strcmp(pci_address_name, pciaddname)) {
1320 adev->enable_virtual_display = true;
1323 res = kstrtol(pciaddname_tmp, 10,
1331 adev->mode_info.num_crtc = num_crtc;
1333 adev->mode_info.num_crtc = 1;
1339 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1340 amdgpu_virtual_display, pci_address_name,
1341 adev->enable_virtual_display, adev->mode_info.num_crtc);
1348 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
1350 * @adev: amdgpu_device pointer
1352 * Parses the asic configuration parameters specified in the gpu info
1353 * firmware and makes them availale to the driver for use in configuring
1355 * Returns 0 on success, -EINVAL on failure.
1357 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1359 const char *chip_name;
1362 const struct gpu_info_firmware_header_v1_0 *hdr;
1364 adev->firmware.gpu_info_fw = NULL;
1366 switch (adev->asic_type) {
1370 case CHIP_POLARIS10:
1371 case CHIP_POLARIS11:
1372 case CHIP_POLARIS12:
1376 #ifdef CONFIG_DRM_AMDGPU_SI
1383 #ifdef CONFIG_DRM_AMDGPU_CIK
1394 chip_name = "vega10";
1397 chip_name = "vega12";
1400 chip_name = "raven";
1404 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
1405 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
1408 "Failed to load gpu_info firmware \"%s\"\n",
1412 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
1415 "Failed to validate gpu_info firmware \"%s\"\n",
1420 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
1421 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1423 switch (hdr->version_major) {
1426 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
1427 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
1428 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1430 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1431 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1432 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1433 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
1434 adev->gfx.config.max_texture_channel_caches =
1435 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1436 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1437 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1438 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1439 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
1440 adev->gfx.config.double_offchip_lds_buf =
1441 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1442 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
1443 adev->gfx.cu_info.max_waves_per_simd =
1444 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1445 adev->gfx.cu_info.max_scratch_slots_per_cu =
1446 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1447 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
1452 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1461 * amdgpu_device_ip_early_init - run early init for hardware IPs
1463 * @adev: amdgpu_device pointer
1465 * Early initialization pass for hardware IPs. The hardware IPs that make
1466 * up each asic are discovered each IP's early_init callback is run. This
1467 * is the first stage in initializing the asic.
1468 * Returns 0 on success, negative error code on failure.
1470 static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
1474 amdgpu_device_enable_virtual_display(adev);
1476 switch (adev->asic_type) {
1480 case CHIP_POLARIS10:
1481 case CHIP_POLARIS11:
1482 case CHIP_POLARIS12:
1486 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
1487 adev->family = AMDGPU_FAMILY_CZ;
1489 adev->family = AMDGPU_FAMILY_VI;
1491 r = vi_set_ip_blocks(adev);
1495 #ifdef CONFIG_DRM_AMDGPU_SI
1501 adev->family = AMDGPU_FAMILY_SI;
1502 r = si_set_ip_blocks(adev);
1507 #ifdef CONFIG_DRM_AMDGPU_CIK
1513 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1514 adev->family = AMDGPU_FAMILY_CI;
1516 adev->family = AMDGPU_FAMILY_KV;
1518 r = cik_set_ip_blocks(adev);
1527 if (adev->asic_type == CHIP_RAVEN)
1528 adev->family = AMDGPU_FAMILY_RV;
1530 adev->family = AMDGPU_FAMILY_AI;
1532 r = soc15_set_ip_blocks(adev);
1537 /* FIXME: not supported yet */
1541 r = amdgpu_device_parse_gpu_info_fw(adev);
1545 amdgpu_amdkfd_device_probe(adev);
1547 if (amdgpu_sriov_vf(adev)) {
1548 r = amdgpu_virt_request_full_gpu(adev, true);
1553 adev->powerplay.pp_feature = amdgpu_pp_feature_mask;
1555 for (i = 0; i < adev->num_ip_blocks; i++) {
1556 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1557 DRM_ERROR("disabled ip block: %d <%s>\n",
1558 i, adev->ip_blocks[i].version->funcs->name);
1559 adev->ip_blocks[i].status.valid = false;
1561 if (adev->ip_blocks[i].version->funcs->early_init) {
1562 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
1564 adev->ip_blocks[i].status.valid = false;
1566 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1567 adev->ip_blocks[i].version->funcs->name, r);
1570 adev->ip_blocks[i].status.valid = true;
1573 adev->ip_blocks[i].status.valid = true;
1578 adev->cg_flags &= amdgpu_cg_mask;
1579 adev->pg_flags &= amdgpu_pg_mask;
1585 * amdgpu_device_ip_init - run init for hardware IPs
1587 * @adev: amdgpu_device pointer
1589 * Main initialization pass for hardware IPs. The list of all the hardware
1590 * IPs that make up the asic is walked and the sw_init and hw_init callbacks
1591 * are run. sw_init initializes the software state associated with each IP
1592 * and hw_init initializes the hardware associated with each IP.
1593 * Returns 0 on success, negative error code on failure.
1595 static int amdgpu_device_ip_init(struct amdgpu_device *adev)
1599 for (i = 0; i < adev->num_ip_blocks; i++) {
1600 if (!adev->ip_blocks[i].status.valid)
1602 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
1604 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1605 adev->ip_blocks[i].version->funcs->name, r);
1608 adev->ip_blocks[i].status.sw = true;
1610 /* need to do gmc hw init early so we can allocate gpu mem */
1611 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1612 r = amdgpu_device_vram_scratch_init(adev);
1614 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
1617 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1619 DRM_ERROR("hw_init %d failed %d\n", i, r);
1622 r = amdgpu_device_wb_init(adev);
1624 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
1627 adev->ip_blocks[i].status.hw = true;
1629 /* right after GMC hw init, we create CSA */
1630 if (amdgpu_sriov_vf(adev)) {
1631 r = amdgpu_allocate_static_csa(adev);
1633 DRM_ERROR("allocate CSA failed %d\n", r);
1640 for (i = 0; i < adev->num_ip_blocks; i++) {
1641 if (!adev->ip_blocks[i].status.sw)
1643 if (adev->ip_blocks[i].status.hw)
1645 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1647 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1648 adev->ip_blocks[i].version->funcs->name, r);
1651 adev->ip_blocks[i].status.hw = true;
1654 amdgpu_amdkfd_device_init(adev);
1656 if (amdgpu_sriov_vf(adev))
1657 amdgpu_virt_release_full_gpu(adev, true);
1663 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
1665 * @adev: amdgpu_device pointer
1667 * Writes a reset magic value to the gart pointer in VRAM. The driver calls
1668 * this function before a GPU reset. If the value is retained after a
1669 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
1671 static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
1673 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1677 * amdgpu_device_check_vram_lost - check if vram is valid
1679 * @adev: amdgpu_device pointer
1681 * Checks the reset magic value written to the gart pointer in VRAM.
1682 * The driver calls this after a GPU reset to see if the contents of
1683 * VRAM is lost or now.
1684 * returns true if vram is lost, false if not.
1686 static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
1688 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1689 AMDGPU_RESET_MAGIC_NUM);
1693 * amdgpu_device_ip_late_set_cg_state - late init for clockgating
1695 * @adev: amdgpu_device pointer
1697 * Late initialization pass enabling clockgating for hardware IPs.
1698 * The list of all the hardware IPs that make up the asic is walked and the
1699 * set_clockgating_state callbacks are run. This stage is run late
1700 * in the init process.
1701 * Returns 0 on success, negative error code on failure.
1703 static int amdgpu_device_ip_late_set_cg_state(struct amdgpu_device *adev)
1707 if (amdgpu_emu_mode == 1)
1710 for (i = 0; i < adev->num_ip_blocks; i++) {
1711 if (!adev->ip_blocks[i].status.valid)
1713 /* skip CG for VCE/UVD, it's handled specially */
1714 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1715 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1716 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
1717 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
1718 /* enable clockgating to save power */
1719 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1722 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1723 adev->ip_blocks[i].version->funcs->name, r);
1732 static int amdgpu_device_ip_late_set_pg_state(struct amdgpu_device *adev)
1736 if (amdgpu_emu_mode == 1)
1739 for (i = 0; i < adev->num_ip_blocks; i++) {
1740 if (!adev->ip_blocks[i].status.valid)
1742 /* skip CG for VCE/UVD, it's handled specially */
1743 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1744 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1745 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
1746 adev->ip_blocks[i].version->funcs->set_powergating_state) {
1747 /* enable powergating to save power */
1748 r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
1751 DRM_ERROR("set_powergating_state(gate) of IP block <%s> failed %d\n",
1752 adev->ip_blocks[i].version->funcs->name, r);
1761 * amdgpu_device_ip_late_init - run late init for hardware IPs
1763 * @adev: amdgpu_device pointer
1765 * Late initialization pass for hardware IPs. The list of all the hardware
1766 * IPs that make up the asic is walked and the late_init callbacks are run.
1767 * late_init covers any special initialization that an IP requires
1768 * after all of the have been initialized or something that needs to happen
1769 * late in the init process.
1770 * Returns 0 on success, negative error code on failure.
1772 static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
1776 for (i = 0; i < adev->num_ip_blocks; i++) {
1777 if (!adev->ip_blocks[i].status.valid)
1779 if (adev->ip_blocks[i].version->funcs->late_init) {
1780 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
1782 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1783 adev->ip_blocks[i].version->funcs->name, r);
1786 adev->ip_blocks[i].status.late_initialized = true;
1790 amdgpu_device_ip_late_set_cg_state(adev);
1791 amdgpu_device_ip_late_set_pg_state(adev);
1793 queue_delayed_work(system_wq, &adev->late_init_work,
1794 msecs_to_jiffies(AMDGPU_RESUME_MS));
1796 amdgpu_device_fill_reset_magic(adev);
1802 * amdgpu_device_ip_fini - run fini for hardware IPs
1804 * @adev: amdgpu_device pointer
1806 * Main teardown pass for hardware IPs. The list of all the hardware
1807 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
1808 * are run. hw_fini tears down the hardware associated with each IP
1809 * and sw_fini tears down any software state associated with each IP.
1810 * Returns 0 on success, negative error code on failure.
1812 static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
1816 amdgpu_amdkfd_device_fini(adev);
1817 /* need to disable SMC first */
1818 for (i = 0; i < adev->num_ip_blocks; i++) {
1819 if (!adev->ip_blocks[i].status.hw)
1821 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC &&
1822 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
1823 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1824 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1825 AMD_CG_STATE_UNGATE);
1827 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1828 adev->ip_blocks[i].version->funcs->name, r);
1831 amdgpu_gfx_off_ctrl(adev, false);
1832 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
1833 /* XXX handle errors */
1835 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1836 adev->ip_blocks[i].version->funcs->name, r);
1838 adev->ip_blocks[i].status.hw = false;
1843 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1844 if (!adev->ip_blocks[i].status.hw)
1847 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1848 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1849 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
1850 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
1851 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1852 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1853 AMD_CG_STATE_UNGATE);
1855 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1856 adev->ip_blocks[i].version->funcs->name, r);
1861 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
1862 /* XXX handle errors */
1864 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1865 adev->ip_blocks[i].version->funcs->name, r);
1868 adev->ip_blocks[i].status.hw = false;
1872 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1873 if (!adev->ip_blocks[i].status.sw)
1876 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1877 amdgpu_free_static_csa(adev);
1878 amdgpu_device_wb_fini(adev);
1879 amdgpu_device_vram_scratch_fini(adev);
1882 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
1883 /* XXX handle errors */
1885 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1886 adev->ip_blocks[i].version->funcs->name, r);
1888 adev->ip_blocks[i].status.sw = false;
1889 adev->ip_blocks[i].status.valid = false;
1892 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1893 if (!adev->ip_blocks[i].status.late_initialized)
1895 if (adev->ip_blocks[i].version->funcs->late_fini)
1896 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1897 adev->ip_blocks[i].status.late_initialized = false;
1900 if (amdgpu_sriov_vf(adev))
1901 if (amdgpu_virt_release_full_gpu(adev, false))
1902 DRM_ERROR("failed to release exclusive mode on fini\n");
1908 * amdgpu_device_ip_late_init_func_handler - work handler for clockgating
1910 * @work: work_struct
1912 * Work handler for amdgpu_device_ip_late_set_cg_state. We put the
1913 * clockgating setup into a worker thread to speed up driver init and
1914 * resume from suspend.
1916 static void amdgpu_device_ip_late_init_func_handler(struct work_struct *work)
1918 struct amdgpu_device *adev =
1919 container_of(work, struct amdgpu_device, late_init_work.work);
1922 r = amdgpu_ib_ring_tests(adev);
1924 DRM_ERROR("ib ring test failed (%d).\n", r);
1927 static void amdgpu_device_delay_enable_gfx_off(struct work_struct *work)
1929 struct amdgpu_device *adev =
1930 container_of(work, struct amdgpu_device, gfx.gfx_off_delay_work.work);
1932 mutex_lock(&adev->gfx.gfx_off_mutex);
1933 if (!adev->gfx.gfx_off_state && !adev->gfx.gfx_off_req_count) {
1934 if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true))
1935 adev->gfx.gfx_off_state = true;
1937 mutex_unlock(&adev->gfx.gfx_off_mutex);
1941 * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1)
1943 * @adev: amdgpu_device pointer
1945 * Main suspend function for hardware IPs. The list of all the hardware
1946 * IPs that make up the asic is walked, clockgating is disabled and the
1947 * suspend callbacks are run. suspend puts the hardware and software state
1948 * in each IP into a state suitable for suspend.
1949 * Returns 0 on success, negative error code on failure.
1951 static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev)
1955 if (amdgpu_sriov_vf(adev))
1956 amdgpu_virt_request_full_gpu(adev, false);
1958 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1959 if (!adev->ip_blocks[i].status.valid)
1961 /* displays are handled separately */
1962 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) {
1963 /* ungate blocks so that suspend can properly shut them down */
1964 if (adev->ip_blocks[i].version->funcs->set_clockgating_state) {
1965 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1966 AMD_CG_STATE_UNGATE);
1968 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1969 adev->ip_blocks[i].version->funcs->name, r);
1972 /* XXX handle errors */
1973 r = adev->ip_blocks[i].version->funcs->suspend(adev);
1974 /* XXX handle errors */
1976 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1977 adev->ip_blocks[i].version->funcs->name, r);
1982 if (amdgpu_sriov_vf(adev))
1983 amdgpu_virt_release_full_gpu(adev, false);
1989 * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2)
1991 * @adev: amdgpu_device pointer
1993 * Main suspend function for hardware IPs. The list of all the hardware
1994 * IPs that make up the asic is walked, clockgating is disabled and the
1995 * suspend callbacks are run. suspend puts the hardware and software state
1996 * in each IP into a state suitable for suspend.
1997 * Returns 0 on success, negative error code on failure.
1999 static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev)
2003 if (amdgpu_sriov_vf(adev))
2004 amdgpu_virt_request_full_gpu(adev, false);
2006 /* ungate SMC block first */
2007 r = amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
2008 AMD_CG_STATE_UNGATE);
2010 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n", r);
2013 /* call smu to disable gfx off feature first when suspend */
2014 amdgpu_gfx_off_ctrl(adev, false);
2016 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2017 if (!adev->ip_blocks[i].status.valid)
2019 /* displays are handled in phase1 */
2020 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)
2022 /* ungate blocks so that suspend can properly shut them down */
2023 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_SMC &&
2024 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
2025 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
2026 AMD_CG_STATE_UNGATE);
2028 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
2029 adev->ip_blocks[i].version->funcs->name, r);
2032 /* XXX handle errors */
2033 r = adev->ip_blocks[i].version->funcs->suspend(adev);
2034 /* XXX handle errors */
2036 DRM_ERROR("suspend of IP block <%s> failed %d\n",
2037 adev->ip_blocks[i].version->funcs->name, r);
2041 if (amdgpu_sriov_vf(adev))
2042 amdgpu_virt_release_full_gpu(adev, false);
2048 * amdgpu_device_ip_suspend - run suspend for hardware IPs
2050 * @adev: amdgpu_device pointer
2052 * Main suspend function for hardware IPs. The list of all the hardware
2053 * IPs that make up the asic is walked, clockgating is disabled and the
2054 * suspend callbacks are run. suspend puts the hardware and software state
2055 * in each IP into a state suitable for suspend.
2056 * Returns 0 on success, negative error code on failure.
2058 int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
2062 r = amdgpu_device_ip_suspend_phase1(adev);
2065 r = amdgpu_device_ip_suspend_phase2(adev);
2070 static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
2074 static enum amd_ip_block_type ip_order[] = {
2075 AMD_IP_BLOCK_TYPE_GMC,
2076 AMD_IP_BLOCK_TYPE_COMMON,
2077 AMD_IP_BLOCK_TYPE_IH,
2080 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
2082 struct amdgpu_ip_block *block;
2084 for (j = 0; j < adev->num_ip_blocks; j++) {
2085 block = &adev->ip_blocks[j];
2087 if (block->version->type != ip_order[i] ||
2088 !block->status.valid)
2091 r = block->version->funcs->hw_init(adev);
2092 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
2101 static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
2105 static enum amd_ip_block_type ip_order[] = {
2106 AMD_IP_BLOCK_TYPE_SMC,
2107 AMD_IP_BLOCK_TYPE_PSP,
2108 AMD_IP_BLOCK_TYPE_DCE,
2109 AMD_IP_BLOCK_TYPE_GFX,
2110 AMD_IP_BLOCK_TYPE_SDMA,
2111 AMD_IP_BLOCK_TYPE_UVD,
2112 AMD_IP_BLOCK_TYPE_VCE
2115 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
2117 struct amdgpu_ip_block *block;
2119 for (j = 0; j < adev->num_ip_blocks; j++) {
2120 block = &adev->ip_blocks[j];
2122 if (block->version->type != ip_order[i] ||
2123 !block->status.valid)
2126 r = block->version->funcs->hw_init(adev);
2127 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
2137 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
2139 * @adev: amdgpu_device pointer
2141 * First resume function for hardware IPs. The list of all the hardware
2142 * IPs that make up the asic is walked and the resume callbacks are run for
2143 * COMMON, GMC, and IH. resume puts the hardware into a functional state
2144 * after a suspend and updates the software state as necessary. This
2145 * function is also used for restoring the GPU after a GPU reset.
2146 * Returns 0 on success, negative error code on failure.
2148 static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
2152 for (i = 0; i < adev->num_ip_blocks; i++) {
2153 if (!adev->ip_blocks[i].status.valid)
2155 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2156 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2157 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
2158 r = adev->ip_blocks[i].version->funcs->resume(adev);
2160 DRM_ERROR("resume of IP block <%s> failed %d\n",
2161 adev->ip_blocks[i].version->funcs->name, r);
2171 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
2173 * @adev: amdgpu_device pointer
2175 * First resume function for hardware IPs. The list of all the hardware
2176 * IPs that make up the asic is walked and the resume callbacks are run for
2177 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
2178 * functional state after a suspend and updates the software state as
2179 * necessary. This function is also used for restoring the GPU after a GPU
2181 * Returns 0 on success, negative error code on failure.
2183 static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
2187 for (i = 0; i < adev->num_ip_blocks; i++) {
2188 if (!adev->ip_blocks[i].status.valid)
2190 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2191 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2192 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)
2194 r = adev->ip_blocks[i].version->funcs->resume(adev);
2196 DRM_ERROR("resume of IP block <%s> failed %d\n",
2197 adev->ip_blocks[i].version->funcs->name, r);
2206 * amdgpu_device_ip_resume - run resume for hardware IPs
2208 * @adev: amdgpu_device pointer
2210 * Main resume function for hardware IPs. The hardware IPs
2211 * are split into two resume functions because they are
2212 * are also used in in recovering from a GPU reset and some additional
2213 * steps need to be take between them. In this case (S3/S4) they are
2215 * Returns 0 on success, negative error code on failure.
2217 static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
2221 r = amdgpu_device_ip_resume_phase1(adev);
2224 r = amdgpu_device_ip_resume_phase2(adev);
2230 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
2232 * @adev: amdgpu_device pointer
2234 * Query the VBIOS data tables to determine if the board supports SR-IOV.
2236 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
2238 if (amdgpu_sriov_vf(adev)) {
2239 if (adev->is_atom_fw) {
2240 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2241 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2243 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2244 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2247 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
2248 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
2253 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
2255 * @asic_type: AMD asic type
2257 * Check if there is DC (new modesetting infrastructre) support for an asic.
2258 * returns true if DC has support, false if not.
2260 bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
2262 switch (asic_type) {
2263 #if defined(CONFIG_DRM_AMD_DC)
2269 * We have systems in the wild with these ASICs that require
2270 * LVDS and VGA support which is not supported with DC.
2272 * Fallback to the non-DC driver here by default so as not to
2273 * cause regressions.
2275 return amdgpu_dc > 0;
2279 case CHIP_POLARIS10:
2280 case CHIP_POLARIS11:
2281 case CHIP_POLARIS12:
2288 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
2291 return amdgpu_dc != 0;
2299 * amdgpu_device_has_dc_support - check if dc is supported
2301 * @adev: amdgpu_device_pointer
2303 * Returns true for supported, false for not supported
2305 bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
2307 if (amdgpu_sriov_vf(adev))
2310 return amdgpu_device_asic_has_dc_support(adev->asic_type);
2314 * amdgpu_device_init - initialize the driver
2316 * @adev: amdgpu_device pointer
2317 * @ddev: drm dev pointer
2318 * @pdev: pci dev pointer
2319 * @flags: driver flags
2321 * Initializes the driver info and hw (all asics).
2322 * Returns 0 for success or an error on failure.
2323 * Called at driver startup.
2325 int amdgpu_device_init(struct amdgpu_device *adev,
2326 struct drm_device *ddev,
2327 struct pci_dev *pdev,
2331 bool runtime = false;
2334 adev->shutdown = false;
2335 adev->dev = &pdev->dev;
2338 adev->flags = flags;
2339 adev->asic_type = flags & AMD_ASIC_MASK;
2340 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
2341 if (amdgpu_emu_mode == 1)
2342 adev->usec_timeout *= 2;
2343 adev->gmc.gart_size = 512 * 1024 * 1024;
2344 adev->accel_working = false;
2345 adev->num_rings = 0;
2346 adev->mman.buffer_funcs = NULL;
2347 adev->mman.buffer_funcs_ring = NULL;
2348 adev->vm_manager.vm_pte_funcs = NULL;
2349 adev->vm_manager.vm_pte_num_rings = 0;
2350 adev->gmc.gmc_funcs = NULL;
2351 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
2352 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
2354 adev->smc_rreg = &amdgpu_invalid_rreg;
2355 adev->smc_wreg = &amdgpu_invalid_wreg;
2356 adev->pcie_rreg = &amdgpu_invalid_rreg;
2357 adev->pcie_wreg = &amdgpu_invalid_wreg;
2358 adev->pciep_rreg = &amdgpu_invalid_rreg;
2359 adev->pciep_wreg = &amdgpu_invalid_wreg;
2360 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2361 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2362 adev->didt_rreg = &amdgpu_invalid_rreg;
2363 adev->didt_wreg = &amdgpu_invalid_wreg;
2364 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2365 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
2366 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2367 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2369 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2370 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2371 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
2373 /* mutex initialization are all done here so we
2374 * can recall function without having locking issues */
2375 atomic_set(&adev->irq.ih.lock, 0);
2376 mutex_init(&adev->firmware.mutex);
2377 mutex_init(&adev->pm.mutex);
2378 mutex_init(&adev->gfx.gpu_clock_mutex);
2379 mutex_init(&adev->srbm_mutex);
2380 mutex_init(&adev->gfx.pipe_reserve_mutex);
2381 mutex_init(&adev->gfx.gfx_off_mutex);
2382 mutex_init(&adev->grbm_idx_mutex);
2383 mutex_init(&adev->mn_lock);
2384 mutex_init(&adev->virt.vf_errors.lock);
2385 hash_init(adev->mn_hash);
2386 mutex_init(&adev->lock_reset);
2388 amdgpu_device_check_arguments(adev);
2390 spin_lock_init(&adev->mmio_idx_lock);
2391 spin_lock_init(&adev->smc_idx_lock);
2392 spin_lock_init(&adev->pcie_idx_lock);
2393 spin_lock_init(&adev->uvd_ctx_idx_lock);
2394 spin_lock_init(&adev->didt_idx_lock);
2395 spin_lock_init(&adev->gc_cac_idx_lock);
2396 spin_lock_init(&adev->se_cac_idx_lock);
2397 spin_lock_init(&adev->audio_endpt_idx_lock);
2398 spin_lock_init(&adev->mm_stats.lock);
2400 INIT_LIST_HEAD(&adev->shadow_list);
2401 mutex_init(&adev->shadow_list_lock);
2403 INIT_LIST_HEAD(&adev->ring_lru_list);
2404 spin_lock_init(&adev->ring_lru_list_lock);
2406 INIT_DELAYED_WORK(&adev->late_init_work,
2407 amdgpu_device_ip_late_init_func_handler);
2408 INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work,
2409 amdgpu_device_delay_enable_gfx_off);
2411 adev->gfx.gfx_off_req_count = 1;
2412 adev->pm.ac_power = power_supply_is_system_supplied() > 0 ? true : false;
2414 /* Registers mapping */
2415 /* TODO: block userspace mapping of io register */
2416 if (adev->asic_type >= CHIP_BONAIRE) {
2417 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2418 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2420 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2421 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2424 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2425 if (adev->rmmio == NULL) {
2428 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2429 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2431 /* doorbell bar mapping */
2432 amdgpu_device_doorbell_init(adev);
2434 /* io port mapping */
2435 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2436 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2437 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2438 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2442 if (adev->rio_mem == NULL)
2443 DRM_INFO("PCI I/O BAR is not found.\n");
2445 amdgpu_device_get_pcie_info(adev);
2447 /* early init functions */
2448 r = amdgpu_device_ip_early_init(adev);
2452 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2453 /* this will fail for cards that aren't VGA class devices, just
2455 vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
2457 if (amdgpu_device_is_px(ddev))
2459 if (!pci_is_thunderbolt_attached(adev->pdev))
2460 vga_switcheroo_register_client(adev->pdev,
2461 &amdgpu_switcheroo_ops, runtime);
2463 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2465 if (amdgpu_emu_mode == 1) {
2466 /* post the asic on emulation mode */
2467 emu_soc_asic_init(adev);
2468 goto fence_driver_init;
2472 if (!amdgpu_get_bios(adev)) {
2477 r = amdgpu_atombios_init(adev);
2479 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
2480 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
2484 /* detect if we are with an SRIOV vbios */
2485 amdgpu_device_detect_sriov_bios(adev);
2487 /* Post card if necessary */
2488 if (amdgpu_device_need_post(adev)) {
2490 dev_err(adev->dev, "no vBIOS found\n");
2494 DRM_INFO("GPU posting now...\n");
2495 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2497 dev_err(adev->dev, "gpu post error!\n");
2502 if (adev->is_atom_fw) {
2503 /* Initialize clocks */
2504 r = amdgpu_atomfirmware_get_clock_info(adev);
2506 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
2507 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2511 /* Initialize clocks */
2512 r = amdgpu_atombios_get_clock_info(adev);
2514 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
2515 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2518 /* init i2c buses */
2519 if (!amdgpu_device_has_dc_support(adev))
2520 amdgpu_atombios_i2c_init(adev);
2525 r = amdgpu_fence_driver_init(adev);
2527 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
2528 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
2532 /* init the mode config */
2533 drm_mode_config_init(adev->ddev);
2535 r = amdgpu_device_ip_init(adev);
2537 /* failed in exclusive mode due to timeout */
2538 if (amdgpu_sriov_vf(adev) &&
2539 !amdgpu_sriov_runtime(adev) &&
2540 amdgpu_virt_mmio_blocked(adev) &&
2541 !amdgpu_virt_wait_reset(adev)) {
2542 dev_err(adev->dev, "VF exclusive mode timeout\n");
2543 /* Don't send request since VF is inactive. */
2544 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
2545 adev->virt.ops = NULL;
2549 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
2550 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
2554 adev->accel_working = true;
2556 amdgpu_vm_check_compute_bug(adev);
2558 /* Initialize the buffer migration limit. */
2559 if (amdgpu_moverate >= 0)
2560 max_MBps = amdgpu_moverate;
2562 max_MBps = 8; /* Allow 8 MB/s. */
2563 /* Get a log2 for easy divisions. */
2564 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2566 r = amdgpu_ib_pool_init(adev);
2568 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
2569 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
2573 if (amdgpu_sriov_vf(adev))
2574 amdgpu_virt_init_data_exchange(adev);
2576 amdgpu_fbdev_init(adev);
2578 r = amdgpu_pm_sysfs_init(adev);
2580 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2582 r = amdgpu_debugfs_gem_init(adev);
2584 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
2586 r = amdgpu_debugfs_regs_init(adev);
2588 DRM_ERROR("registering register debugfs failed (%d).\n", r);
2590 r = amdgpu_debugfs_firmware_init(adev);
2592 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
2594 r = amdgpu_debugfs_init(adev);
2596 DRM_ERROR("Creating debugfs files failed (%d).\n", r);
2598 if ((amdgpu_testing & 1)) {
2599 if (adev->accel_working)
2600 amdgpu_test_moves(adev);
2602 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2604 if (amdgpu_benchmarking) {
2605 if (adev->accel_working)
2606 amdgpu_benchmark(adev, amdgpu_benchmarking);
2608 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2611 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2612 * explicit gating rather than handling it automatically.
2614 r = amdgpu_device_ip_late_init(adev);
2616 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
2617 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
2624 amdgpu_vf_error_trans_all(adev);
2626 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2632 * amdgpu_device_fini - tear down the driver
2634 * @adev: amdgpu_device pointer
2636 * Tear down the driver info (all asics).
2637 * Called at driver shutdown.
2639 void amdgpu_device_fini(struct amdgpu_device *adev)
2643 DRM_INFO("amdgpu: finishing device.\n");
2644 adev->shutdown = true;
2645 /* disable all interrupts */
2646 amdgpu_irq_disable_all(adev);
2647 if (adev->mode_info.mode_config_initialized){
2648 if (!amdgpu_device_has_dc_support(adev))
2649 drm_crtc_force_disable_all(adev->ddev);
2651 drm_atomic_helper_shutdown(adev->ddev);
2653 amdgpu_ib_pool_fini(adev);
2654 amdgpu_fence_driver_fini(adev);
2655 amdgpu_pm_sysfs_fini(adev);
2656 amdgpu_fbdev_fini(adev);
2657 r = amdgpu_device_ip_fini(adev);
2658 if (adev->firmware.gpu_info_fw) {
2659 release_firmware(adev->firmware.gpu_info_fw);
2660 adev->firmware.gpu_info_fw = NULL;
2662 adev->accel_working = false;
2663 cancel_delayed_work_sync(&adev->late_init_work);
2664 /* free i2c buses */
2665 if (!amdgpu_device_has_dc_support(adev))
2666 amdgpu_i2c_fini(adev);
2668 if (amdgpu_emu_mode != 1)
2669 amdgpu_atombios_fini(adev);
2673 if (!pci_is_thunderbolt_attached(adev->pdev))
2674 vga_switcheroo_unregister_client(adev->pdev);
2675 if (adev->flags & AMD_IS_PX)
2676 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2677 vga_client_register(adev->pdev, NULL, NULL, NULL);
2679 pci_iounmap(adev->pdev, adev->rio_mem);
2680 adev->rio_mem = NULL;
2681 iounmap(adev->rmmio);
2683 amdgpu_device_doorbell_fini(adev);
2684 amdgpu_debugfs_regs_cleanup(adev);
2692 * amdgpu_device_suspend - initiate device suspend
2694 * @dev: drm dev pointer
2695 * @suspend: suspend state
2696 * @fbcon : notify the fbdev of suspend
2698 * Puts the hw in the suspend state (all asics).
2699 * Returns 0 for success or an error on failure.
2700 * Called at driver suspend.
2702 int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
2704 struct amdgpu_device *adev;
2705 struct drm_crtc *crtc;
2706 struct drm_connector *connector;
2709 if (dev == NULL || dev->dev_private == NULL) {
2713 adev = dev->dev_private;
2715 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2718 drm_kms_helper_poll_disable(dev);
2721 amdgpu_fbdev_set_suspend(adev, 1);
2723 if (!amdgpu_device_has_dc_support(adev)) {
2724 /* turn off display hw */
2725 drm_modeset_lock_all(dev);
2726 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2727 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2729 drm_modeset_unlock_all(dev);
2730 /* unpin the front buffers and cursors */
2731 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2732 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2733 struct drm_framebuffer *fb = crtc->primary->fb;
2734 struct amdgpu_bo *robj;
2736 if (amdgpu_crtc->cursor_bo) {
2737 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2738 r = amdgpu_bo_reserve(aobj, true);
2740 amdgpu_bo_unpin(aobj);
2741 amdgpu_bo_unreserve(aobj);
2745 if (fb == NULL || fb->obj[0] == NULL) {
2748 robj = gem_to_amdgpu_bo(fb->obj[0]);
2749 /* don't unpin kernel fb objects */
2750 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
2751 r = amdgpu_bo_reserve(robj, true);
2753 amdgpu_bo_unpin(robj);
2754 amdgpu_bo_unreserve(robj);
2760 amdgpu_amdkfd_suspend(adev);
2762 r = amdgpu_device_ip_suspend_phase1(adev);
2764 /* evict vram memory */
2765 amdgpu_bo_evict_vram(adev);
2767 amdgpu_fence_driver_suspend(adev);
2769 r = amdgpu_device_ip_suspend_phase2(adev);
2771 /* evict remaining vram memory
2772 * This second call to evict vram is to evict the gart page table
2775 amdgpu_bo_evict_vram(adev);
2777 pci_save_state(dev->pdev);
2779 /* Shut down the device */
2780 pci_disable_device(dev->pdev);
2781 pci_set_power_state(dev->pdev, PCI_D3hot);
2783 r = amdgpu_asic_reset(adev);
2785 DRM_ERROR("amdgpu asic reset failed\n");
2792 * amdgpu_device_resume - initiate device resume
2794 * @dev: drm dev pointer
2795 * @resume: resume state
2796 * @fbcon : notify the fbdev of resume
2798 * Bring the hw back to operating state (all asics).
2799 * Returns 0 for success or an error on failure.
2800 * Called at driver resume.
2802 int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
2804 struct drm_connector *connector;
2805 struct amdgpu_device *adev = dev->dev_private;
2806 struct drm_crtc *crtc;
2809 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2813 pci_set_power_state(dev->pdev, PCI_D0);
2814 pci_restore_state(dev->pdev);
2815 r = pci_enable_device(dev->pdev);
2821 if (amdgpu_device_need_post(adev)) {
2822 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2824 DRM_ERROR("amdgpu asic init failed\n");
2827 r = amdgpu_device_ip_resume(adev);
2829 DRM_ERROR("amdgpu_device_ip_resume failed (%d).\n", r);
2832 amdgpu_fence_driver_resume(adev);
2835 r = amdgpu_device_ip_late_init(adev);
2839 if (!amdgpu_device_has_dc_support(adev)) {
2841 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2842 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2844 if (amdgpu_crtc->cursor_bo) {
2845 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2846 r = amdgpu_bo_reserve(aobj, true);
2848 r = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM);
2850 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2851 amdgpu_crtc->cursor_addr = amdgpu_bo_gpu_offset(aobj);
2852 amdgpu_bo_unreserve(aobj);
2857 r = amdgpu_amdkfd_resume(adev);
2861 /* Make sure IB tests flushed */
2862 flush_delayed_work(&adev->late_init_work);
2864 /* blat the mode back in */
2866 if (!amdgpu_device_has_dc_support(adev)) {
2868 drm_helper_resume_force_mode(dev);
2870 /* turn on display hw */
2871 drm_modeset_lock_all(dev);
2872 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2873 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2875 drm_modeset_unlock_all(dev);
2877 amdgpu_fbdev_set_suspend(adev, 0);
2880 drm_kms_helper_poll_enable(dev);
2883 * Most of the connector probing functions try to acquire runtime pm
2884 * refs to ensure that the GPU is powered on when connector polling is
2885 * performed. Since we're calling this from a runtime PM callback,
2886 * trying to acquire rpm refs will cause us to deadlock.
2888 * Since we're guaranteed to be holding the rpm lock, it's safe to
2889 * temporarily disable the rpm helpers so this doesn't deadlock us.
2892 dev->dev->power.disable_depth++;
2894 if (!amdgpu_device_has_dc_support(adev))
2895 drm_helper_hpd_irq_event(dev);
2897 drm_kms_helper_hotplug_event(dev);
2899 dev->dev->power.disable_depth--;
2905 * amdgpu_device_ip_check_soft_reset - did soft reset succeed
2907 * @adev: amdgpu_device pointer
2909 * The list of all the hardware IPs that make up the asic is walked and
2910 * the check_soft_reset callbacks are run. check_soft_reset determines
2911 * if the asic is still hung or not.
2912 * Returns true if any of the IPs are still in a hung state, false if not.
2914 static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
2917 bool asic_hang = false;
2919 if (amdgpu_sriov_vf(adev))
2922 if (amdgpu_asic_need_full_reset(adev))
2925 for (i = 0; i < adev->num_ip_blocks; i++) {
2926 if (!adev->ip_blocks[i].status.valid)
2928 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2929 adev->ip_blocks[i].status.hang =
2930 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2931 if (adev->ip_blocks[i].status.hang) {
2932 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
2940 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
2942 * @adev: amdgpu_device pointer
2944 * The list of all the hardware IPs that make up the asic is walked and the
2945 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
2946 * handles any IP specific hardware or software state changes that are
2947 * necessary for a soft reset to succeed.
2948 * Returns 0 on success, negative error code on failure.
2950 static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
2954 for (i = 0; i < adev->num_ip_blocks; i++) {
2955 if (!adev->ip_blocks[i].status.valid)
2957 if (adev->ip_blocks[i].status.hang &&
2958 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2959 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
2969 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
2971 * @adev: amdgpu_device pointer
2973 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
2974 * reset is necessary to recover.
2975 * Returns true if a full asic reset is required, false if not.
2977 static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
2981 if (amdgpu_asic_need_full_reset(adev))
2984 for (i = 0; i < adev->num_ip_blocks; i++) {
2985 if (!adev->ip_blocks[i].status.valid)
2987 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2988 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2989 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2990 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
2991 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
2992 if (adev->ip_blocks[i].status.hang) {
2993 DRM_INFO("Some block need full reset!\n");
3002 * amdgpu_device_ip_soft_reset - do a soft reset
3004 * @adev: amdgpu_device pointer
3006 * The list of all the hardware IPs that make up the asic is walked and the
3007 * soft_reset callbacks are run if the block is hung. soft_reset handles any
3008 * IP specific hardware or software state changes that are necessary to soft
3010 * Returns 0 on success, negative error code on failure.
3012 static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
3016 for (i = 0; i < adev->num_ip_blocks; i++) {
3017 if (!adev->ip_blocks[i].status.valid)
3019 if (adev->ip_blocks[i].status.hang &&
3020 adev->ip_blocks[i].version->funcs->soft_reset) {
3021 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
3031 * amdgpu_device_ip_post_soft_reset - clean up from soft reset
3033 * @adev: amdgpu_device pointer
3035 * The list of all the hardware IPs that make up the asic is walked and the
3036 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
3037 * handles any IP specific hardware or software state changes that are
3038 * necessary after the IP has been soft reset.
3039 * Returns 0 on success, negative error code on failure.
3041 static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
3045 for (i = 0; i < adev->num_ip_blocks; i++) {
3046 if (!adev->ip_blocks[i].status.valid)
3048 if (adev->ip_blocks[i].status.hang &&
3049 adev->ip_blocks[i].version->funcs->post_soft_reset)
3050 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
3059 * amdgpu_device_recover_vram_from_shadow - restore shadowed VRAM buffers
3061 * @adev: amdgpu_device pointer
3062 * @ring: amdgpu_ring for the engine handling the buffer operations
3063 * @bo: amdgpu_bo buffer whose shadow is being restored
3064 * @fence: dma_fence associated with the operation
3066 * Restores the VRAM buffer contents from the shadow in GTT. Used to
3067 * restore things like GPUVM page tables after a GPU reset where
3068 * the contents of VRAM might be lost.
3069 * Returns 0 on success, negative error code on failure.
3071 static int amdgpu_device_recover_vram_from_shadow(struct amdgpu_device *adev,
3072 struct amdgpu_ring *ring,
3073 struct amdgpu_bo *bo,
3074 struct dma_fence **fence)
3082 r = amdgpu_bo_reserve(bo, true);
3085 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
3086 /* if bo has been evicted, then no need to recover */
3087 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
3088 r = amdgpu_bo_validate(bo->shadow);
3090 DRM_ERROR("bo validate failed!\n");
3094 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
3097 DRM_ERROR("recover page table failed!\n");
3102 amdgpu_bo_unreserve(bo);
3107 * amdgpu_device_handle_vram_lost - Handle the loss of VRAM contents
3109 * @adev: amdgpu_device pointer
3111 * Restores the contents of VRAM buffers from the shadows in GTT. Used to
3112 * restore things like GPUVM page tables after a GPU reset where
3113 * the contents of VRAM might be lost.
3114 * Returns 0 on success, 1 on failure.
3116 static int amdgpu_device_handle_vram_lost(struct amdgpu_device *adev)
3118 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
3119 struct amdgpu_bo *bo, *tmp;
3120 struct dma_fence *fence = NULL, *next = NULL;
3125 if (amdgpu_sriov_runtime(adev))
3126 tmo = msecs_to_jiffies(8000);
3128 tmo = msecs_to_jiffies(100);
3130 DRM_INFO("recover vram bo from shadow start\n");
3131 mutex_lock(&adev->shadow_list_lock);
3132 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
3134 amdgpu_device_recover_vram_from_shadow(adev, ring, bo, &next);
3136 r = dma_fence_wait_timeout(fence, false, tmo);
3138 pr_err("wait fence %p[%d] timeout\n", fence, i);
3140 pr_err("wait fence %p[%d] interrupted\n", fence, i);
3142 dma_fence_put(fence);
3149 dma_fence_put(fence);
3152 mutex_unlock(&adev->shadow_list_lock);
3155 r = dma_fence_wait_timeout(fence, false, tmo);
3157 pr_err("wait fence %p[%d] timeout\n", fence, i);
3159 pr_err("wait fence %p[%d] interrupted\n", fence, i);
3162 dma_fence_put(fence);
3165 DRM_INFO("recover vram bo from shadow done\n");
3167 DRM_ERROR("recover vram bo from shadow failed\n");
3169 return (r > 0) ? 0 : 1;
3173 * amdgpu_device_reset - reset ASIC/GPU for bare-metal or passthrough
3175 * @adev: amdgpu device pointer
3177 * attempt to do soft-reset or full-reset and reinitialize Asic
3178 * return 0 means succeeded otherwise failed
3180 static int amdgpu_device_reset(struct amdgpu_device *adev)
3182 bool need_full_reset, vram_lost = 0;
3185 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
3187 if (!need_full_reset) {
3188 amdgpu_device_ip_pre_soft_reset(adev);
3189 r = amdgpu_device_ip_soft_reset(adev);
3190 amdgpu_device_ip_post_soft_reset(adev);
3191 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
3192 DRM_INFO("soft reset failed, will fallback to full reset!\n");
3193 need_full_reset = true;
3197 if (need_full_reset) {
3198 r = amdgpu_device_ip_suspend(adev);
3201 r = amdgpu_asic_reset(adev);
3203 amdgpu_atom_asic_init(adev->mode_info.atom_context);
3206 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
3207 r = amdgpu_device_ip_resume_phase1(adev);
3211 vram_lost = amdgpu_device_check_vram_lost(adev);
3213 DRM_ERROR("VRAM is lost!\n");
3214 atomic_inc(&adev->vram_lost_counter);
3217 r = amdgpu_gtt_mgr_recover(
3218 &adev->mman.bdev.man[TTM_PL_TT]);
3222 r = amdgpu_device_ip_resume_phase2(adev);
3227 amdgpu_device_fill_reset_magic(adev);
3233 amdgpu_irq_gpu_reset_resume_helper(adev);
3234 r = amdgpu_ib_ring_tests(adev);
3236 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
3237 r = amdgpu_device_ip_suspend(adev);
3238 need_full_reset = true;
3243 if (!r && ((need_full_reset && !(adev->flags & AMD_IS_APU)) || vram_lost))
3244 r = amdgpu_device_handle_vram_lost(adev);
3250 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
3252 * @adev: amdgpu device pointer
3253 * @from_hypervisor: request from hypervisor
3255 * do VF FLR and reinitialize Asic
3256 * return 0 means succeeded otherwise failed
3258 static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
3259 bool from_hypervisor)
3263 if (from_hypervisor)
3264 r = amdgpu_virt_request_full_gpu(adev, true);
3266 r = amdgpu_virt_reset_gpu(adev);
3270 /* Resume IP prior to SMC */
3271 r = amdgpu_device_ip_reinit_early_sriov(adev);
3275 /* we need recover gart prior to run SMC/CP/SDMA resume */
3276 amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
3278 /* now we are okay to resume SMC/CP/SDMA */
3279 r = amdgpu_device_ip_reinit_late_sriov(adev);
3283 amdgpu_irq_gpu_reset_resume_helper(adev);
3284 r = amdgpu_ib_ring_tests(adev);
3287 amdgpu_virt_release_full_gpu(adev, true);
3288 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
3289 atomic_inc(&adev->vram_lost_counter);
3290 r = amdgpu_device_handle_vram_lost(adev);
3297 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
3299 * @adev: amdgpu device pointer
3300 * @job: which job trigger hang
3301 * @force: forces reset regardless of amdgpu_gpu_recovery
3303 * Attempt to reset the GPU if it has hung (all asics).
3304 * Returns 0 for success or an error on failure.
3306 int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
3307 struct amdgpu_job *job, bool force)
3311 if (!force && !amdgpu_device_ip_check_soft_reset(adev)) {
3312 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
3316 if (!force && (amdgpu_gpu_recovery == 0 ||
3317 (amdgpu_gpu_recovery == -1 && !amdgpu_sriov_vf(adev)))) {
3318 DRM_INFO("GPU recovery disabled.\n");
3322 dev_info(adev->dev, "GPU reset begin!\n");
3324 mutex_lock(&adev->lock_reset);
3325 atomic_inc(&adev->gpu_reset_counter);
3326 adev->in_gpu_reset = 1;
3329 amdgpu_amdkfd_pre_reset(adev);
3332 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
3334 /* block all schedulers and reset given job's ring */
3335 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3336 struct amdgpu_ring *ring = adev->rings[i];
3338 if (!ring || !ring->sched.thread)
3341 kthread_park(ring->sched.thread);
3343 if (job && job->base.sched == &ring->sched)
3346 drm_sched_hw_job_reset(&ring->sched, job ? &job->base : NULL);
3348 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
3349 amdgpu_fence_driver_force_completion(ring);
3352 if (amdgpu_sriov_vf(adev))
3353 r = amdgpu_device_reset_sriov(adev, job ? false : true);
3355 r = amdgpu_device_reset(adev);
3357 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3358 struct amdgpu_ring *ring = adev->rings[i];
3360 if (!ring || !ring->sched.thread)
3363 /* only need recovery sched of the given job's ring
3364 * or all rings (in the case @job is NULL)
3365 * after above amdgpu_reset accomplished
3367 if ((!job || job->base.sched == &ring->sched) && !r)
3368 drm_sched_job_recovery(&ring->sched);
3370 kthread_unpark(ring->sched.thread);
3373 if (!amdgpu_device_has_dc_support(adev)) {
3374 drm_helper_resume_force_mode(adev->ddev);
3377 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
3380 /* bad news, how to tell it to userspace ? */
3381 dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
3382 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
3384 dev_info(adev->dev, "GPU reset(%d) succeeded!\n",atomic_read(&adev->gpu_reset_counter));
3388 amdgpu_amdkfd_post_reset(adev);
3389 amdgpu_vf_error_trans_all(adev);
3390 adev->in_gpu_reset = 0;
3391 mutex_unlock(&adev->lock_reset);
3396 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
3398 * @adev: amdgpu_device pointer
3400 * Fetchs and stores in the driver the PCIE capabilities (gen speed
3401 * and lanes) of the slot the device is in. Handles APUs and
3402 * virtualized environments where PCIE config space may not be available.
3404 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
3406 struct pci_dev *pdev;
3407 enum pci_bus_speed speed_cap;
3408 enum pcie_link_width link_width;
3410 if (amdgpu_pcie_gen_cap)
3411 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
3413 if (amdgpu_pcie_lane_cap)
3414 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
3416 /* covers APUs as well */
3417 if (pci_is_root_bus(adev->pdev->bus)) {
3418 if (adev->pm.pcie_gen_mask == 0)
3419 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3420 if (adev->pm.pcie_mlw_mask == 0)
3421 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
3425 if (adev->pm.pcie_gen_mask == 0) {
3428 speed_cap = pcie_get_speed_cap(pdev);
3429 if (speed_cap == PCI_SPEED_UNKNOWN) {
3430 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3431 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3432 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3434 if (speed_cap == PCIE_SPEED_16_0GT)
3435 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3436 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3437 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
3438 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4);
3439 else if (speed_cap == PCIE_SPEED_8_0GT)
3440 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3441 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3442 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3443 else if (speed_cap == PCIE_SPEED_5_0GT)
3444 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3445 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2);
3447 adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1;
3450 pdev = adev->ddev->pdev->bus->self;
3451 speed_cap = pcie_get_speed_cap(pdev);
3452 if (speed_cap == PCI_SPEED_UNKNOWN) {
3453 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3454 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
3456 if (speed_cap == PCIE_SPEED_16_0GT)
3457 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3458 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3459 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
3460 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4);
3461 else if (speed_cap == PCIE_SPEED_8_0GT)
3462 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3463 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3464 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3);
3465 else if (speed_cap == PCIE_SPEED_5_0GT)
3466 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3467 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
3469 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
3473 if (adev->pm.pcie_mlw_mask == 0) {
3474 pdev = adev->ddev->pdev->bus->self;
3475 link_width = pcie_get_width_cap(pdev);
3476 if (link_width == PCIE_LNK_WIDTH_UNKNOWN) {
3477 adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK;
3479 switch (link_width) {
3481 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3482 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3483 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3484 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3485 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3486 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3487 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3490 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3491 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3492 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3493 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3494 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3495 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3498 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3499 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3500 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3501 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3502 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3505 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3506 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3507 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3508 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3511 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3512 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3513 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3516 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3517 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3520 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;