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>
31 #include <linux/debugfs.h>
33 #include <drm/drm_crtc_helper.h>
34 #include <drm/amdgpu_drm.h>
35 #include <linux/vgaarb.h>
36 #include <linux/vga_switcheroo.h>
37 #include <linux/efi.h>
39 #include "amdgpu_trace.h"
40 #include "amdgpu_i2c.h"
42 #include "amdgpu_atombios.h"
43 #include "amdgpu_atomfirmware.h"
45 #ifdef CONFIG_DRM_AMDGPU_SI
48 #ifdef CONFIG_DRM_AMDGPU_CIK
53 #include "bif/bif_4_1_d.h"
54 #include <linux/pci.h>
55 #include <linux/firmware.h>
56 #include "amdgpu_vf_error.h"
58 #include "amdgpu_amdkfd.h"
59 #include "amdgpu_pm.h"
61 MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
62 MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
64 #define AMDGPU_RESUME_MS 2000
66 static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
67 static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
68 static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev);
69 static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev);
71 static const char *amdgpu_asic_name[] = {
95 bool amdgpu_device_is_px(struct drm_device *dev)
97 struct amdgpu_device *adev = dev->dev_private;
99 if (adev->flags & AMD_IS_PX)
105 * MMIO register access helper functions.
107 uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
112 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
113 return amdgpu_virt_kiq_rreg(adev, reg);
115 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
116 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
120 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
121 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
122 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
123 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
125 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
129 void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
132 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
134 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
135 adev->last_mm_index = v;
138 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
139 return amdgpu_virt_kiq_wreg(adev, reg, v);
141 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
142 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
146 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
147 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
148 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
149 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
152 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
157 u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
159 if ((reg * 4) < adev->rio_mem_size)
160 return ioread32(adev->rio_mem + (reg * 4));
162 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
163 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
167 void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
169 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
170 adev->last_mm_index = v;
173 if ((reg * 4) < adev->rio_mem_size)
174 iowrite32(v, adev->rio_mem + (reg * 4));
176 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
177 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
180 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
186 * amdgpu_mm_rdoorbell - read a doorbell dword
188 * @adev: amdgpu_device pointer
189 * @index: doorbell index
191 * Returns the value in the doorbell aperture at the
192 * requested doorbell index (CIK).
194 u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
196 if (index < adev->doorbell.num_doorbells) {
197 return readl(adev->doorbell.ptr + index);
199 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
205 * amdgpu_mm_wdoorbell - write a doorbell dword
207 * @adev: amdgpu_device pointer
208 * @index: doorbell index
211 * Writes @v to the doorbell aperture at the
212 * requested doorbell index (CIK).
214 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
216 if (index < adev->doorbell.num_doorbells) {
217 writel(v, adev->doorbell.ptr + index);
219 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
224 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
226 * @adev: amdgpu_device pointer
227 * @index: doorbell index
229 * Returns the value in the doorbell aperture at the
230 * requested doorbell index (VEGA10+).
232 u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
234 if (index < adev->doorbell.num_doorbells) {
235 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
237 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
243 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
245 * @adev: amdgpu_device pointer
246 * @index: doorbell index
249 * Writes @v to the doorbell aperture at the
250 * requested doorbell index (VEGA10+).
252 void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
254 if (index < adev->doorbell.num_doorbells) {
255 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
257 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
262 * amdgpu_invalid_rreg - dummy reg read function
264 * @adev: amdgpu device pointer
265 * @reg: offset of register
267 * Dummy register read function. Used for register blocks
268 * that certain asics don't have (all asics).
269 * Returns the value in the register.
271 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
273 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
279 * amdgpu_invalid_wreg - dummy reg write function
281 * @adev: amdgpu device pointer
282 * @reg: offset of register
283 * @v: value to write to the register
285 * Dummy register read function. Used for register blocks
286 * that certain asics don't have (all asics).
288 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
290 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
296 * amdgpu_block_invalid_rreg - dummy reg read function
298 * @adev: amdgpu device pointer
299 * @block: offset of instance
300 * @reg: offset of register
302 * Dummy register read function. Used for register blocks
303 * that certain asics don't have (all asics).
304 * Returns the value in the register.
306 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
307 uint32_t block, uint32_t reg)
309 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
316 * amdgpu_block_invalid_wreg - dummy reg write function
318 * @adev: amdgpu device pointer
319 * @block: offset of instance
320 * @reg: offset of register
321 * @v: value to write to the register
323 * Dummy register read function. Used for register blocks
324 * that certain asics don't have (all asics).
326 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
328 uint32_t reg, uint32_t v)
330 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
335 static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
337 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
338 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
339 &adev->vram_scratch.robj,
340 &adev->vram_scratch.gpu_addr,
341 (void **)&adev->vram_scratch.ptr);
344 static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
346 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
350 * amdgpu_program_register_sequence - program an array of registers.
352 * @adev: amdgpu_device pointer
353 * @registers: pointer to the register array
354 * @array_size: size of the register array
356 * Programs an array or registers with and and or masks.
357 * This is a helper for setting golden registers.
359 void amdgpu_program_register_sequence(struct amdgpu_device *adev,
360 const u32 *registers,
361 const u32 array_size)
363 u32 tmp, reg, and_mask, or_mask;
369 for (i = 0; i < array_size; i +=3) {
370 reg = registers[i + 0];
371 and_mask = registers[i + 1];
372 or_mask = registers[i + 2];
374 if (and_mask == 0xffffffff) {
385 void amdgpu_pci_config_reset(struct amdgpu_device *adev)
387 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
391 * GPU doorbell aperture helpers function.
394 * amdgpu_doorbell_init - Init doorbell driver information.
396 * @adev: amdgpu_device pointer
398 * Init doorbell driver information (CIK)
399 * Returns 0 on success, error on failure.
401 static int amdgpu_doorbell_init(struct amdgpu_device *adev)
403 /* No doorbell on SI hardware generation */
404 if (adev->asic_type < CHIP_BONAIRE) {
405 adev->doorbell.base = 0;
406 adev->doorbell.size = 0;
407 adev->doorbell.num_doorbells = 0;
408 adev->doorbell.ptr = NULL;
412 /* doorbell bar mapping */
413 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
414 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
416 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
417 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
418 if (adev->doorbell.num_doorbells == 0)
421 adev->doorbell.ptr = ioremap(adev->doorbell.base,
422 adev->doorbell.num_doorbells *
424 if (adev->doorbell.ptr == NULL)
431 * amdgpu_doorbell_fini - Tear down doorbell driver information.
433 * @adev: amdgpu_device pointer
435 * Tear down doorbell driver information (CIK)
437 static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
439 iounmap(adev->doorbell.ptr);
440 adev->doorbell.ptr = NULL;
444 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
447 * @adev: amdgpu_device pointer
448 * @aperture_base: output returning doorbell aperture base physical address
449 * @aperture_size: output returning doorbell aperture size in bytes
450 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
452 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
453 * takes doorbells required for its own rings and reports the setup to amdkfd.
454 * amdgpu reserved doorbells are at the start of the doorbell aperture.
456 void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
457 phys_addr_t *aperture_base,
458 size_t *aperture_size,
459 size_t *start_offset)
462 * The first num_doorbells are used by amdgpu.
463 * amdkfd takes whatever's left in the aperture.
465 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
466 *aperture_base = adev->doorbell.base;
467 *aperture_size = adev->doorbell.size;
468 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
478 * Writeback is the method by which the GPU updates special pages in memory
479 * with the status of certain GPU events (fences, ring pointers,etc.).
483 * amdgpu_wb_fini - Disable Writeback and free memory
485 * @adev: amdgpu_device pointer
487 * Disables Writeback and frees the Writeback memory (all asics).
488 * Used at driver shutdown.
490 static void amdgpu_wb_fini(struct amdgpu_device *adev)
492 if (adev->wb.wb_obj) {
493 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
495 (void **)&adev->wb.wb);
496 adev->wb.wb_obj = NULL;
501 * amdgpu_wb_init- Init Writeback driver info and allocate memory
503 * @adev: amdgpu_device pointer
505 * Initializes writeback and allocates writeback memory (all asics).
506 * Used at driver startup.
507 * Returns 0 on success or an -error on failure.
509 static int amdgpu_wb_init(struct amdgpu_device *adev)
513 if (adev->wb.wb_obj == NULL) {
514 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
515 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
516 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
517 &adev->wb.wb_obj, &adev->wb.gpu_addr,
518 (void **)&adev->wb.wb);
520 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
524 adev->wb.num_wb = AMDGPU_MAX_WB;
525 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
527 /* clear wb memory */
528 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
535 * amdgpu_wb_get - Allocate a wb entry
537 * @adev: amdgpu_device pointer
540 * Allocate a wb slot for use by the driver (all asics).
541 * Returns 0 on success or -EINVAL on failure.
543 int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
545 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
547 if (offset < adev->wb.num_wb) {
548 __set_bit(offset, adev->wb.used);
549 *wb = offset * 8; /* convert to dw offset */
557 * amdgpu_wb_free - Free a wb entry
559 * @adev: amdgpu_device pointer
562 * Free a wb slot allocated for use by the driver (all asics)
564 void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
566 if (wb < adev->wb.num_wb)
567 __clear_bit(wb, adev->wb.used);
571 * amdgpu_vram_location - try to find VRAM location
572 * @adev: amdgpu device structure holding all necessary informations
573 * @mc: memory controller structure holding memory informations
574 * @base: base address at which to put VRAM
576 * Function will try to place VRAM at base address provided
577 * as parameter (which is so far either PCI aperture address or
578 * for IGP TOM base address).
580 * If there is not enough space to fit the unvisible VRAM in the 32bits
581 * address space then we limit the VRAM size to the aperture.
583 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
584 * this shouldn't be a problem as we are using the PCI aperture as a reference.
585 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
588 * Note: we use mc_vram_size as on some board we need to program the mc to
589 * cover the whole aperture even if VRAM size is inferior to aperture size
590 * Novell bug 204882 + along with lots of ubuntu ones
592 * Note: when limiting vram it's safe to overwritte real_vram_size because
593 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
594 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
597 * Note: IGP TOM addr should be the same as the aperture addr, we don't
598 * explicitly check for that though.
600 * FIXME: when reducing VRAM size align new size on power of 2.
602 void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
604 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
606 mc->vram_start = base;
607 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
608 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
609 mc->real_vram_size = mc->aper_size;
610 mc->mc_vram_size = mc->aper_size;
612 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
613 if (limit && limit < mc->real_vram_size)
614 mc->real_vram_size = limit;
615 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
616 mc->mc_vram_size >> 20, mc->vram_start,
617 mc->vram_end, mc->real_vram_size >> 20);
621 * amdgpu_gart_location - try to find GTT location
622 * @adev: amdgpu device structure holding all necessary informations
623 * @mc: memory controller structure holding memory informations
625 * Function will place try to place GTT before or after VRAM.
627 * If GTT size is bigger than space left then we ajust GTT size.
628 * Thus function will never fails.
630 * FIXME: when reducing GTT size align new size on power of 2.
632 void amdgpu_gart_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
634 u64 size_af, size_bf;
636 size_af = adev->mc.mc_mask - mc->vram_end;
637 size_bf = mc->vram_start;
638 if (size_bf > size_af) {
639 if (mc->gart_size > size_bf) {
640 dev_warn(adev->dev, "limiting GTT\n");
641 mc->gart_size = size_bf;
645 if (mc->gart_size > size_af) {
646 dev_warn(adev->dev, "limiting GTT\n");
647 mc->gart_size = size_af;
649 mc->gart_start = mc->vram_end + 1;
651 mc->gart_end = mc->gart_start + mc->gart_size - 1;
652 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
653 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
657 * Firmware Reservation functions
660 * amdgpu_fw_reserve_vram_fini - free fw reserved vram
662 * @adev: amdgpu_device pointer
664 * free fw reserved vram if it has been reserved.
666 void amdgpu_fw_reserve_vram_fini(struct amdgpu_device *adev)
668 amdgpu_bo_free_kernel(&adev->fw_vram_usage.reserved_bo,
669 NULL, &adev->fw_vram_usage.va);
673 * amdgpu_fw_reserve_vram_init - create bo vram reservation from fw
675 * @adev: amdgpu_device pointer
677 * create bo vram reservation from fw.
679 int amdgpu_fw_reserve_vram_init(struct amdgpu_device *adev)
683 u64 vram_size = adev->mc.visible_vram_size;
685 adev->fw_vram_usage.va = NULL;
686 adev->fw_vram_usage.reserved_bo = NULL;
688 if (adev->fw_vram_usage.size > 0 &&
689 adev->fw_vram_usage.size <= vram_size) {
691 r = amdgpu_bo_create(adev, adev->fw_vram_usage.size,
693 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
694 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS, NULL, NULL, 0,
695 &adev->fw_vram_usage.reserved_bo);
699 r = amdgpu_bo_reserve(adev->fw_vram_usage.reserved_bo, false);
702 r = amdgpu_bo_pin_restricted(adev->fw_vram_usage.reserved_bo,
703 AMDGPU_GEM_DOMAIN_VRAM,
704 adev->fw_vram_usage.start_offset,
705 (adev->fw_vram_usage.start_offset +
706 adev->fw_vram_usage.size), &gpu_addr);
709 r = amdgpu_bo_kmap(adev->fw_vram_usage.reserved_bo,
710 &adev->fw_vram_usage.va);
714 amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
719 amdgpu_bo_unpin(adev->fw_vram_usage.reserved_bo);
721 amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
723 amdgpu_bo_unref(&adev->fw_vram_usage.reserved_bo);
725 adev->fw_vram_usage.va = NULL;
726 adev->fw_vram_usage.reserved_bo = NULL;
732 * GPU helpers function.
735 * amdgpu_need_post - check if the hw need post or not
737 * @adev: amdgpu_device pointer
739 * Check if the asic has been initialized (all asics) at driver startup
740 * or post is needed if hw reset is performed.
741 * Returns true if need or false if not.
743 bool amdgpu_need_post(struct amdgpu_device *adev)
747 if (adev->has_hw_reset) {
748 adev->has_hw_reset = false;
752 /* bios scratch used on CIK+ */
753 if (adev->asic_type >= CHIP_BONAIRE)
754 return amdgpu_atombios_scratch_need_asic_init(adev);
756 /* check MEM_SIZE for older asics */
757 reg = amdgpu_asic_get_config_memsize(adev);
759 if ((reg != 0) && (reg != 0xffffffff))
766 static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
768 if (amdgpu_sriov_vf(adev))
771 if (amdgpu_passthrough(adev)) {
772 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
773 * some old smc fw still need driver do vPost otherwise gpu hang, while
774 * those smc fw version above 22.15 doesn't have this flaw, so we force
775 * vpost executed for smc version below 22.15
777 if (adev->asic_type == CHIP_FIJI) {
780 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
781 /* force vPost if error occured */
785 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
786 if (fw_ver < 0x00160e00)
790 return amdgpu_need_post(adev);
794 * amdgpu_dummy_page_init - init dummy page used by the driver
796 * @adev: amdgpu_device pointer
798 * Allocate the dummy page used by the driver (all asics).
799 * This dummy page is used by the driver as a filler for gart entries
800 * when pages are taken out of the GART
801 * Returns 0 on sucess, -ENOMEM on failure.
803 int amdgpu_dummy_page_init(struct amdgpu_device *adev)
805 if (adev->dummy_page.page)
807 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
808 if (adev->dummy_page.page == NULL)
810 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
811 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
812 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
813 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
814 __free_page(adev->dummy_page.page);
815 adev->dummy_page.page = NULL;
822 * amdgpu_dummy_page_fini - free dummy page used by the driver
824 * @adev: amdgpu_device pointer
826 * Frees the dummy page used by the driver (all asics).
828 void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
830 if (adev->dummy_page.page == NULL)
832 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
833 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
834 __free_page(adev->dummy_page.page);
835 adev->dummy_page.page = NULL;
839 /* ATOM accessor methods */
841 * ATOM is an interpreted byte code stored in tables in the vbios. The
842 * driver registers callbacks to access registers and the interpreter
843 * in the driver parses the tables and executes then to program specific
844 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
845 * atombios.h, and atom.c
849 * cail_pll_read - read PLL register
851 * @info: atom card_info pointer
852 * @reg: PLL register offset
854 * Provides a PLL register accessor for the atom interpreter (r4xx+).
855 * Returns the value of the PLL register.
857 static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
863 * cail_pll_write - write PLL register
865 * @info: atom card_info pointer
866 * @reg: PLL register offset
867 * @val: value to write to the pll register
869 * Provides a PLL register accessor for the atom interpreter (r4xx+).
871 static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
877 * cail_mc_read - read MC (Memory Controller) register
879 * @info: atom card_info pointer
880 * @reg: MC register offset
882 * Provides an MC register accessor for the atom interpreter (r4xx+).
883 * Returns the value of the MC register.
885 static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
891 * cail_mc_write - write MC (Memory Controller) register
893 * @info: atom card_info pointer
894 * @reg: MC register offset
895 * @val: value to write to the pll register
897 * Provides a MC register accessor for the atom interpreter (r4xx+).
899 static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
905 * cail_reg_write - write MMIO register
907 * @info: atom card_info pointer
908 * @reg: MMIO register offset
909 * @val: value to write to the pll register
911 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
913 static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
915 struct amdgpu_device *adev = info->dev->dev_private;
921 * cail_reg_read - read MMIO register
923 * @info: atom card_info pointer
924 * @reg: MMIO register offset
926 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
927 * Returns the value of the MMIO register.
929 static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
931 struct amdgpu_device *adev = info->dev->dev_private;
939 * cail_ioreg_write - write IO register
941 * @info: atom card_info pointer
942 * @reg: IO register offset
943 * @val: value to write to the pll register
945 * Provides a IO register accessor for the atom interpreter (r4xx+).
947 static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
949 struct amdgpu_device *adev = info->dev->dev_private;
955 * cail_ioreg_read - read IO register
957 * @info: atom card_info pointer
958 * @reg: IO register offset
960 * Provides an IO register accessor for the atom interpreter (r4xx+).
961 * Returns the value of the IO register.
963 static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
965 struct amdgpu_device *adev = info->dev->dev_private;
972 static ssize_t amdgpu_atombios_get_vbios_version(struct device *dev,
973 struct device_attribute *attr,
976 struct drm_device *ddev = dev_get_drvdata(dev);
977 struct amdgpu_device *adev = ddev->dev_private;
978 struct atom_context *ctx = adev->mode_info.atom_context;
980 return snprintf(buf, PAGE_SIZE, "%s\n", ctx->vbios_version);
983 static DEVICE_ATTR(vbios_version, 0444, amdgpu_atombios_get_vbios_version,
987 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
989 * @adev: amdgpu_device pointer
991 * Frees the driver info and register access callbacks for the ATOM
992 * interpreter (r4xx+).
993 * Called at driver shutdown.
995 static void amdgpu_atombios_fini(struct amdgpu_device *adev)
997 if (adev->mode_info.atom_context) {
998 kfree(adev->mode_info.atom_context->scratch);
999 kfree(adev->mode_info.atom_context->iio);
1001 kfree(adev->mode_info.atom_context);
1002 adev->mode_info.atom_context = NULL;
1003 kfree(adev->mode_info.atom_card_info);
1004 adev->mode_info.atom_card_info = NULL;
1005 device_remove_file(adev->dev, &dev_attr_vbios_version);
1009 * amdgpu_atombios_init - init the driver info and callbacks for atombios
1011 * @adev: amdgpu_device pointer
1013 * Initializes the driver info and register access callbacks for the
1014 * ATOM interpreter (r4xx+).
1015 * Returns 0 on sucess, -ENOMEM on failure.
1016 * Called at driver startup.
1018 static int amdgpu_atombios_init(struct amdgpu_device *adev)
1020 struct card_info *atom_card_info =
1021 kzalloc(sizeof(struct card_info), GFP_KERNEL);
1024 if (!atom_card_info)
1027 adev->mode_info.atom_card_info = atom_card_info;
1028 atom_card_info->dev = adev->ddev;
1029 atom_card_info->reg_read = cail_reg_read;
1030 atom_card_info->reg_write = cail_reg_write;
1031 /* needed for iio ops */
1032 if (adev->rio_mem) {
1033 atom_card_info->ioreg_read = cail_ioreg_read;
1034 atom_card_info->ioreg_write = cail_ioreg_write;
1036 DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
1037 atom_card_info->ioreg_read = cail_reg_read;
1038 atom_card_info->ioreg_write = cail_reg_write;
1040 atom_card_info->mc_read = cail_mc_read;
1041 atom_card_info->mc_write = cail_mc_write;
1042 atom_card_info->pll_read = cail_pll_read;
1043 atom_card_info->pll_write = cail_pll_write;
1045 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
1046 if (!adev->mode_info.atom_context) {
1047 amdgpu_atombios_fini(adev);
1051 mutex_init(&adev->mode_info.atom_context->mutex);
1052 if (adev->is_atom_fw) {
1053 amdgpu_atomfirmware_scratch_regs_init(adev);
1054 amdgpu_atomfirmware_allocate_fb_scratch(adev);
1056 amdgpu_atombios_scratch_regs_init(adev);
1057 amdgpu_atombios_allocate_fb_scratch(adev);
1060 ret = device_create_file(adev->dev, &dev_attr_vbios_version);
1062 DRM_ERROR("Failed to create device file for VBIOS version\n");
1069 /* if we get transitioned to only one device, take VGA back */
1071 * amdgpu_vga_set_decode - enable/disable vga decode
1073 * @cookie: amdgpu_device pointer
1074 * @state: enable/disable vga decode
1076 * Enable/disable vga decode (all asics).
1077 * Returns VGA resource flags.
1079 static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
1081 struct amdgpu_device *adev = cookie;
1082 amdgpu_asic_set_vga_state(adev, state);
1084 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1085 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1087 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1090 static void amdgpu_check_block_size(struct amdgpu_device *adev)
1092 /* defines number of bits in page table versus page directory,
1093 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1094 * page table and the remaining bits are in the page directory */
1095 if (amdgpu_vm_block_size == -1)
1098 if (amdgpu_vm_block_size < 9) {
1099 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1100 amdgpu_vm_block_size);
1104 if (amdgpu_vm_block_size > 24 ||
1105 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1106 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1107 amdgpu_vm_block_size);
1114 amdgpu_vm_block_size = -1;
1117 static void amdgpu_check_vm_size(struct amdgpu_device *adev)
1119 /* no need to check the default value */
1120 if (amdgpu_vm_size == -1)
1123 if (!is_power_of_2(amdgpu_vm_size)) {
1124 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
1129 if (amdgpu_vm_size < 1) {
1130 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1136 * Max GPUVM size for Cayman, SI, CI VI are 40 bits.
1138 if (amdgpu_vm_size > 1024) {
1139 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1147 amdgpu_vm_size = -1;
1151 * amdgpu_check_arguments - validate module params
1153 * @adev: amdgpu_device pointer
1155 * Validates certain module parameters and updates
1156 * the associated values used by the driver (all asics).
1158 static void amdgpu_check_arguments(struct amdgpu_device *adev)
1160 if (amdgpu_sched_jobs < 4) {
1161 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1163 amdgpu_sched_jobs = 4;
1164 } else if (!is_power_of_2(amdgpu_sched_jobs)){
1165 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1167 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1170 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
1171 /* gart size must be greater or equal to 32M */
1172 dev_warn(adev->dev, "gart size (%d) too small\n",
1174 amdgpu_gart_size = -1;
1177 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
1178 /* gtt size must be greater or equal to 32M */
1179 dev_warn(adev->dev, "gtt size (%d) too small\n",
1181 amdgpu_gtt_size = -1;
1184 /* valid range is between 4 and 9 inclusive */
1185 if (amdgpu_vm_fragment_size != -1 &&
1186 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1187 dev_warn(adev->dev, "valid range is between 4 and 9\n");
1188 amdgpu_vm_fragment_size = -1;
1191 amdgpu_check_vm_size(adev);
1193 amdgpu_check_block_size(adev);
1195 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
1196 !is_power_of_2(amdgpu_vram_page_split))) {
1197 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1198 amdgpu_vram_page_split);
1199 amdgpu_vram_page_split = 1024;
1204 * amdgpu_switcheroo_set_state - set switcheroo state
1206 * @pdev: pci dev pointer
1207 * @state: vga_switcheroo state
1209 * Callback for the switcheroo driver. Suspends or resumes the
1210 * the asics before or after it is powered up using ACPI methods.
1212 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1214 struct drm_device *dev = pci_get_drvdata(pdev);
1216 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1219 if (state == VGA_SWITCHEROO_ON) {
1220 pr_info("amdgpu: switched on\n");
1221 /* don't suspend or resume card normally */
1222 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1224 amdgpu_device_resume(dev, true, true);
1226 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1227 drm_kms_helper_poll_enable(dev);
1229 pr_info("amdgpu: switched off\n");
1230 drm_kms_helper_poll_disable(dev);
1231 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1232 amdgpu_device_suspend(dev, true, true);
1233 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1238 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1240 * @pdev: pci dev pointer
1242 * Callback for the switcheroo driver. Check of the switcheroo
1243 * state can be changed.
1244 * Returns true if the state can be changed, false if not.
1246 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1248 struct drm_device *dev = pci_get_drvdata(pdev);
1251 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1252 * locking inversion with the driver load path. And the access here is
1253 * completely racy anyway. So don't bother with locking for now.
1255 return dev->open_count == 0;
1258 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1259 .set_gpu_state = amdgpu_switcheroo_set_state,
1261 .can_switch = amdgpu_switcheroo_can_switch,
1264 int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
1265 enum amd_ip_block_type block_type,
1266 enum amd_clockgating_state state)
1270 for (i = 0; i < adev->num_ip_blocks; i++) {
1271 if (!adev->ip_blocks[i].status.valid)
1273 if (adev->ip_blocks[i].version->type != block_type)
1275 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1277 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1278 (void *)adev, state);
1280 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1281 adev->ip_blocks[i].version->funcs->name, r);
1286 int amdgpu_set_powergating_state(struct amdgpu_device *adev,
1287 enum amd_ip_block_type block_type,
1288 enum amd_powergating_state state)
1292 for (i = 0; i < adev->num_ip_blocks; i++) {
1293 if (!adev->ip_blocks[i].status.valid)
1295 if (adev->ip_blocks[i].version->type != block_type)
1297 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1299 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1300 (void *)adev, state);
1302 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1303 adev->ip_blocks[i].version->funcs->name, r);
1308 void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1312 for (i = 0; i < adev->num_ip_blocks; i++) {
1313 if (!adev->ip_blocks[i].status.valid)
1315 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1316 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1320 int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1321 enum amd_ip_block_type block_type)
1325 for (i = 0; i < adev->num_ip_blocks; i++) {
1326 if (!adev->ip_blocks[i].status.valid)
1328 if (adev->ip_blocks[i].version->type == block_type) {
1329 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
1339 bool amdgpu_is_idle(struct amdgpu_device *adev,
1340 enum amd_ip_block_type block_type)
1344 for (i = 0; i < adev->num_ip_blocks; i++) {
1345 if (!adev->ip_blocks[i].status.valid)
1347 if (adev->ip_blocks[i].version->type == block_type)
1348 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
1354 struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1355 enum amd_ip_block_type type)
1359 for (i = 0; i < adev->num_ip_blocks; i++)
1360 if (adev->ip_blocks[i].version->type == type)
1361 return &adev->ip_blocks[i];
1367 * amdgpu_ip_block_version_cmp
1369 * @adev: amdgpu_device pointer
1370 * @type: enum amd_ip_block_type
1371 * @major: major version
1372 * @minor: minor version
1374 * return 0 if equal or greater
1375 * return 1 if smaller or the ip_block doesn't exist
1377 int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
1378 enum amd_ip_block_type type,
1379 u32 major, u32 minor)
1381 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
1383 if (ip_block && ((ip_block->version->major > major) ||
1384 ((ip_block->version->major == major) &&
1385 (ip_block->version->minor >= minor))))
1392 * amdgpu_ip_block_add
1394 * @adev: amdgpu_device pointer
1395 * @ip_block_version: pointer to the IP to add
1397 * Adds the IP block driver information to the collection of IPs
1400 int amdgpu_ip_block_add(struct amdgpu_device *adev,
1401 const struct amdgpu_ip_block_version *ip_block_version)
1403 if (!ip_block_version)
1406 DRM_DEBUG("add ip block number %d <%s>\n", adev->num_ip_blocks,
1407 ip_block_version->funcs->name);
1409 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1414 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
1416 adev->enable_virtual_display = false;
1418 if (amdgpu_virtual_display) {
1419 struct drm_device *ddev = adev->ddev;
1420 const char *pci_address_name = pci_name(ddev->pdev);
1421 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
1423 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1424 pciaddstr_tmp = pciaddstr;
1425 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1426 pciaddname = strsep(&pciaddname_tmp, ",");
1427 if (!strcmp("all", pciaddname)
1428 || !strcmp(pci_address_name, pciaddname)) {
1432 adev->enable_virtual_display = true;
1435 res = kstrtol(pciaddname_tmp, 10,
1443 adev->mode_info.num_crtc = num_crtc;
1445 adev->mode_info.num_crtc = 1;
1451 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1452 amdgpu_virtual_display, pci_address_name,
1453 adev->enable_virtual_display, adev->mode_info.num_crtc);
1459 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1461 const char *chip_name;
1464 const struct gpu_info_firmware_header_v1_0 *hdr;
1466 adev->firmware.gpu_info_fw = NULL;
1468 switch (adev->asic_type) {
1472 case CHIP_POLARIS11:
1473 case CHIP_POLARIS10:
1474 case CHIP_POLARIS12:
1477 #ifdef CONFIG_DRM_AMDGPU_SI
1484 #ifdef CONFIG_DRM_AMDGPU_CIK
1494 chip_name = "vega10";
1497 chip_name = "raven";
1501 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
1502 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
1505 "Failed to load gpu_info firmware \"%s\"\n",
1509 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
1512 "Failed to validate gpu_info firmware \"%s\"\n",
1517 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
1518 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1520 switch (hdr->version_major) {
1523 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
1524 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
1525 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1527 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1528 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1529 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1530 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
1531 adev->gfx.config.max_texture_channel_caches =
1532 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1533 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1534 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1535 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1536 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
1537 adev->gfx.config.double_offchip_lds_buf =
1538 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1539 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
1540 adev->gfx.cu_info.max_waves_per_simd =
1541 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1542 adev->gfx.cu_info.max_scratch_slots_per_cu =
1543 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1544 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
1549 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1557 static int amdgpu_early_init(struct amdgpu_device *adev)
1561 amdgpu_device_enable_virtual_display(adev);
1563 switch (adev->asic_type) {
1567 case CHIP_POLARIS11:
1568 case CHIP_POLARIS10:
1569 case CHIP_POLARIS12:
1572 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
1573 adev->family = AMDGPU_FAMILY_CZ;
1575 adev->family = AMDGPU_FAMILY_VI;
1577 r = vi_set_ip_blocks(adev);
1581 #ifdef CONFIG_DRM_AMDGPU_SI
1587 adev->family = AMDGPU_FAMILY_SI;
1588 r = si_set_ip_blocks(adev);
1593 #ifdef CONFIG_DRM_AMDGPU_CIK
1599 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1600 adev->family = AMDGPU_FAMILY_CI;
1602 adev->family = AMDGPU_FAMILY_KV;
1604 r = cik_set_ip_blocks(adev);
1611 if (adev->asic_type == CHIP_RAVEN)
1612 adev->family = AMDGPU_FAMILY_RV;
1614 adev->family = AMDGPU_FAMILY_AI;
1616 r = soc15_set_ip_blocks(adev);
1621 /* FIXME: not supported yet */
1625 r = amdgpu_device_parse_gpu_info_fw(adev);
1629 if (amdgpu_sriov_vf(adev)) {
1630 r = amdgpu_virt_request_full_gpu(adev, true);
1635 for (i = 0; i < adev->num_ip_blocks; i++) {
1636 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1637 DRM_ERROR("disabled ip block: %d <%s>\n",
1638 i, adev->ip_blocks[i].version->funcs->name);
1639 adev->ip_blocks[i].status.valid = false;
1641 if (adev->ip_blocks[i].version->funcs->early_init) {
1642 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
1644 adev->ip_blocks[i].status.valid = false;
1646 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1647 adev->ip_blocks[i].version->funcs->name, r);
1650 adev->ip_blocks[i].status.valid = true;
1653 adev->ip_blocks[i].status.valid = true;
1658 adev->cg_flags &= amdgpu_cg_mask;
1659 adev->pg_flags &= amdgpu_pg_mask;
1664 static int amdgpu_init(struct amdgpu_device *adev)
1668 for (i = 0; i < adev->num_ip_blocks; i++) {
1669 if (!adev->ip_blocks[i].status.valid)
1671 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
1673 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1674 adev->ip_blocks[i].version->funcs->name, r);
1677 adev->ip_blocks[i].status.sw = true;
1678 /* need to do gmc hw init early so we can allocate gpu mem */
1679 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1680 r = amdgpu_vram_scratch_init(adev);
1682 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
1685 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1687 DRM_ERROR("hw_init %d failed %d\n", i, r);
1690 r = amdgpu_wb_init(adev);
1692 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
1695 adev->ip_blocks[i].status.hw = true;
1697 /* right after GMC hw init, we create CSA */
1698 if (amdgpu_sriov_vf(adev)) {
1699 r = amdgpu_allocate_static_csa(adev);
1701 DRM_ERROR("allocate CSA failed %d\n", r);
1708 for (i = 0; i < adev->num_ip_blocks; i++) {
1709 if (!adev->ip_blocks[i].status.sw)
1711 /* gmc hw init is done early */
1712 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
1714 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1716 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1717 adev->ip_blocks[i].version->funcs->name, r);
1720 adev->ip_blocks[i].status.hw = true;
1726 static void amdgpu_fill_reset_magic(struct amdgpu_device *adev)
1728 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1731 static bool amdgpu_check_vram_lost(struct amdgpu_device *adev)
1733 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1734 AMDGPU_RESET_MAGIC_NUM);
1737 static int amdgpu_late_set_cg_state(struct amdgpu_device *adev)
1741 for (i = 0; i < adev->num_ip_blocks; i++) {
1742 if (!adev->ip_blocks[i].status.valid)
1744 /* skip CG for VCE/UVD, it's handled specially */
1745 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1746 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1747 /* enable clockgating to save power */
1748 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1751 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1752 adev->ip_blocks[i].version->funcs->name, r);
1760 static int amdgpu_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 mod_delayed_work(system_wq, &adev->late_init_work,
1779 msecs_to_jiffies(AMDGPU_RESUME_MS));
1781 amdgpu_fill_reset_magic(adev);
1786 static int amdgpu_fini(struct amdgpu_device *adev)
1790 /* need to disable SMC first */
1791 for (i = 0; i < adev->num_ip_blocks; i++) {
1792 if (!adev->ip_blocks[i].status.hw)
1794 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
1795 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1796 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1797 AMD_CG_STATE_UNGATE);
1799 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1800 adev->ip_blocks[i].version->funcs->name, r);
1803 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
1804 /* XXX handle errors */
1806 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1807 adev->ip_blocks[i].version->funcs->name, r);
1809 adev->ip_blocks[i].status.hw = false;
1814 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1815 if (!adev->ip_blocks[i].status.hw)
1817 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1818 amdgpu_wb_fini(adev);
1819 amdgpu_vram_scratch_fini(adev);
1822 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1823 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1824 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1825 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1826 AMD_CG_STATE_UNGATE);
1828 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1829 adev->ip_blocks[i].version->funcs->name, r);
1834 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
1835 /* XXX handle errors */
1837 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1838 adev->ip_blocks[i].version->funcs->name, r);
1841 adev->ip_blocks[i].status.hw = false;
1844 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1845 if (!adev->ip_blocks[i].status.sw)
1847 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
1848 /* XXX handle errors */
1850 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1851 adev->ip_blocks[i].version->funcs->name, r);
1853 adev->ip_blocks[i].status.sw = false;
1854 adev->ip_blocks[i].status.valid = false;
1857 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1858 if (!adev->ip_blocks[i].status.late_initialized)
1860 if (adev->ip_blocks[i].version->funcs->late_fini)
1861 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1862 adev->ip_blocks[i].status.late_initialized = false;
1865 if (amdgpu_sriov_vf(adev))
1866 amdgpu_virt_release_full_gpu(adev, false);
1871 static void amdgpu_late_init_func_handler(struct work_struct *work)
1873 struct amdgpu_device *adev =
1874 container_of(work, struct amdgpu_device, late_init_work.work);
1875 amdgpu_late_set_cg_state(adev);
1878 int amdgpu_suspend(struct amdgpu_device *adev)
1882 if (amdgpu_sriov_vf(adev))
1883 amdgpu_virt_request_full_gpu(adev, false);
1885 /* ungate SMC block first */
1886 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1887 AMD_CG_STATE_UNGATE);
1889 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1892 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1893 if (!adev->ip_blocks[i].status.valid)
1895 /* ungate blocks so that suspend can properly shut them down */
1896 if (i != AMD_IP_BLOCK_TYPE_SMC) {
1897 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1898 AMD_CG_STATE_UNGATE);
1900 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1901 adev->ip_blocks[i].version->funcs->name, r);
1904 /* XXX handle errors */
1905 r = adev->ip_blocks[i].version->funcs->suspend(adev);
1906 /* XXX handle errors */
1908 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1909 adev->ip_blocks[i].version->funcs->name, r);
1913 if (amdgpu_sriov_vf(adev))
1914 amdgpu_virt_release_full_gpu(adev, false);
1919 static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
1923 static enum amd_ip_block_type ip_order[] = {
1924 AMD_IP_BLOCK_TYPE_GMC,
1925 AMD_IP_BLOCK_TYPE_COMMON,
1926 AMD_IP_BLOCK_TYPE_IH,
1929 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1931 struct amdgpu_ip_block *block;
1933 for (j = 0; j < adev->num_ip_blocks; j++) {
1934 block = &adev->ip_blocks[j];
1936 if (block->version->type != ip_order[i] ||
1937 !block->status.valid)
1940 r = block->version->funcs->hw_init(adev);
1941 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
1948 static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
1952 static enum amd_ip_block_type ip_order[] = {
1953 AMD_IP_BLOCK_TYPE_SMC,
1954 AMD_IP_BLOCK_TYPE_DCE,
1955 AMD_IP_BLOCK_TYPE_GFX,
1956 AMD_IP_BLOCK_TYPE_SDMA,
1957 AMD_IP_BLOCK_TYPE_UVD,
1958 AMD_IP_BLOCK_TYPE_VCE
1961 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1963 struct amdgpu_ip_block *block;
1965 for (j = 0; j < adev->num_ip_blocks; j++) {
1966 block = &adev->ip_blocks[j];
1968 if (block->version->type != ip_order[i] ||
1969 !block->status.valid)
1972 r = block->version->funcs->hw_init(adev);
1973 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
1980 static int amdgpu_resume_phase1(struct amdgpu_device *adev)
1984 for (i = 0; i < adev->num_ip_blocks; i++) {
1985 if (!adev->ip_blocks[i].status.valid)
1987 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1988 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1989 adev->ip_blocks[i].version->type ==
1990 AMD_IP_BLOCK_TYPE_IH) {
1991 r = adev->ip_blocks[i].version->funcs->resume(adev);
1993 DRM_ERROR("resume of IP block <%s> failed %d\n",
1994 adev->ip_blocks[i].version->funcs->name, r);
2003 static int amdgpu_resume_phase2(struct amdgpu_device *adev)
2007 for (i = 0; i < adev->num_ip_blocks; i++) {
2008 if (!adev->ip_blocks[i].status.valid)
2010 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2011 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2012 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
2014 r = adev->ip_blocks[i].version->funcs->resume(adev);
2016 DRM_ERROR("resume of IP block <%s> failed %d\n",
2017 adev->ip_blocks[i].version->funcs->name, r);
2025 static int amdgpu_resume(struct amdgpu_device *adev)
2029 r = amdgpu_resume_phase1(adev);
2032 r = amdgpu_resume_phase2(adev);
2037 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
2039 if (adev->is_atom_fw) {
2040 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2041 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2043 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2044 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2049 * amdgpu_device_init - initialize the driver
2051 * @adev: amdgpu_device pointer
2052 * @pdev: drm dev pointer
2053 * @pdev: pci dev pointer
2054 * @flags: driver flags
2056 * Initializes the driver info and hw (all asics).
2057 * Returns 0 for success or an error on failure.
2058 * Called at driver startup.
2060 int amdgpu_device_init(struct amdgpu_device *adev,
2061 struct drm_device *ddev,
2062 struct pci_dev *pdev,
2066 bool runtime = false;
2069 adev->shutdown = false;
2070 adev->dev = &pdev->dev;
2073 adev->flags = flags;
2074 adev->asic_type = flags & AMD_ASIC_MASK;
2075 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
2076 adev->mc.gart_size = 512 * 1024 * 1024;
2077 adev->accel_working = false;
2078 adev->num_rings = 0;
2079 adev->mman.buffer_funcs = NULL;
2080 adev->mman.buffer_funcs_ring = NULL;
2081 adev->vm_manager.vm_pte_funcs = NULL;
2082 adev->vm_manager.vm_pte_num_rings = 0;
2083 adev->gart.gart_funcs = NULL;
2084 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
2085 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
2087 adev->smc_rreg = &amdgpu_invalid_rreg;
2088 adev->smc_wreg = &amdgpu_invalid_wreg;
2089 adev->pcie_rreg = &amdgpu_invalid_rreg;
2090 adev->pcie_wreg = &amdgpu_invalid_wreg;
2091 adev->pciep_rreg = &amdgpu_invalid_rreg;
2092 adev->pciep_wreg = &amdgpu_invalid_wreg;
2093 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2094 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2095 adev->didt_rreg = &amdgpu_invalid_rreg;
2096 adev->didt_wreg = &amdgpu_invalid_wreg;
2097 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2098 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
2099 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2100 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2103 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2104 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2105 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
2107 /* mutex initialization are all done here so we
2108 * can recall function without having locking issues */
2109 atomic_set(&adev->irq.ih.lock, 0);
2110 mutex_init(&adev->firmware.mutex);
2111 mutex_init(&adev->pm.mutex);
2112 mutex_init(&adev->gfx.gpu_clock_mutex);
2113 mutex_init(&adev->srbm_mutex);
2114 mutex_init(&adev->gfx.pipe_reserve_mutex);
2115 mutex_init(&adev->grbm_idx_mutex);
2116 mutex_init(&adev->mn_lock);
2117 mutex_init(&adev->virt.vf_errors.lock);
2118 hash_init(adev->mn_hash);
2120 amdgpu_check_arguments(adev);
2122 spin_lock_init(&adev->mmio_idx_lock);
2123 spin_lock_init(&adev->smc_idx_lock);
2124 spin_lock_init(&adev->pcie_idx_lock);
2125 spin_lock_init(&adev->uvd_ctx_idx_lock);
2126 spin_lock_init(&adev->didt_idx_lock);
2127 spin_lock_init(&adev->gc_cac_idx_lock);
2128 spin_lock_init(&adev->se_cac_idx_lock);
2129 spin_lock_init(&adev->audio_endpt_idx_lock);
2130 spin_lock_init(&adev->mm_stats.lock);
2132 INIT_LIST_HEAD(&adev->shadow_list);
2133 mutex_init(&adev->shadow_list_lock);
2135 INIT_LIST_HEAD(&adev->gtt_list);
2136 spin_lock_init(&adev->gtt_list_lock);
2138 INIT_LIST_HEAD(&adev->ring_lru_list);
2139 spin_lock_init(&adev->ring_lru_list_lock);
2141 INIT_DELAYED_WORK(&adev->late_init_work, amdgpu_late_init_func_handler);
2143 /* Registers mapping */
2144 /* TODO: block userspace mapping of io register */
2145 if (adev->asic_type >= CHIP_BONAIRE) {
2146 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2147 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2149 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2150 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2153 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2154 if (adev->rmmio == NULL) {
2157 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2158 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2160 /* doorbell bar mapping */
2161 amdgpu_doorbell_init(adev);
2163 /* io port mapping */
2164 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2165 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2166 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2167 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2171 if (adev->rio_mem == NULL)
2172 DRM_INFO("PCI I/O BAR is not found.\n");
2174 /* early init functions */
2175 r = amdgpu_early_init(adev);
2179 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2180 /* this will fail for cards that aren't VGA class devices, just
2182 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
2184 if (amdgpu_runtime_pm == 1)
2186 if (amdgpu_device_is_px(ddev))
2188 if (!pci_is_thunderbolt_attached(adev->pdev))
2189 vga_switcheroo_register_client(adev->pdev,
2190 &amdgpu_switcheroo_ops, runtime);
2192 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2195 if (!amdgpu_get_bios(adev)) {
2200 r = amdgpu_atombios_init(adev);
2202 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
2203 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
2207 /* detect if we are with an SRIOV vbios */
2208 amdgpu_device_detect_sriov_bios(adev);
2210 /* Post card if necessary */
2211 if (amdgpu_vpost_needed(adev)) {
2213 dev_err(adev->dev, "no vBIOS found\n");
2214 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
2218 DRM_INFO("GPU posting now...\n");
2219 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2221 dev_err(adev->dev, "gpu post error!\n");
2222 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_POST_ERROR, 0, 0);
2226 DRM_INFO("GPU post is not needed\n");
2229 if (adev->is_atom_fw) {
2230 /* Initialize clocks */
2231 r = amdgpu_atomfirmware_get_clock_info(adev);
2233 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
2234 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2238 /* Initialize clocks */
2239 r = amdgpu_atombios_get_clock_info(adev);
2241 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
2242 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2245 /* init i2c buses */
2246 amdgpu_atombios_i2c_init(adev);
2250 r = amdgpu_fence_driver_init(adev);
2252 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
2253 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
2257 /* init the mode config */
2258 drm_mode_config_init(adev->ddev);
2260 r = amdgpu_init(adev);
2262 dev_err(adev->dev, "amdgpu_init failed\n");
2263 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
2268 adev->accel_working = true;
2270 amdgpu_vm_check_compute_bug(adev);
2272 /* Initialize the buffer migration limit. */
2273 if (amdgpu_moverate >= 0)
2274 max_MBps = amdgpu_moverate;
2276 max_MBps = 8; /* Allow 8 MB/s. */
2277 /* Get a log2 for easy divisions. */
2278 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2280 r = amdgpu_ib_pool_init(adev);
2282 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
2283 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
2287 r = amdgpu_ib_ring_tests(adev);
2289 DRM_ERROR("ib ring test failed (%d).\n", r);
2291 if (amdgpu_sriov_vf(adev))
2292 amdgpu_virt_init_data_exchange(adev);
2294 amdgpu_fbdev_init(adev);
2296 r = amdgpu_pm_sysfs_init(adev);
2298 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2300 r = amdgpu_gem_debugfs_init(adev);
2302 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
2304 r = amdgpu_debugfs_regs_init(adev);
2306 DRM_ERROR("registering register debugfs failed (%d).\n", r);
2308 r = amdgpu_debugfs_test_ib_ring_init(adev);
2310 DRM_ERROR("registering register test ib ring debugfs failed (%d).\n", r);
2312 r = amdgpu_debugfs_firmware_init(adev);
2314 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
2316 r = amdgpu_debugfs_vbios_dump_init(adev);
2318 DRM_ERROR("Creating vbios dump debugfs failed (%d).\n", r);
2320 if ((amdgpu_testing & 1)) {
2321 if (adev->accel_working)
2322 amdgpu_test_moves(adev);
2324 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2326 if (amdgpu_benchmarking) {
2327 if (adev->accel_working)
2328 amdgpu_benchmark(adev, amdgpu_benchmarking);
2330 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2333 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2334 * explicit gating rather than handling it automatically.
2336 r = amdgpu_late_init(adev);
2338 dev_err(adev->dev, "amdgpu_late_init failed\n");
2339 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
2346 amdgpu_vf_error_trans_all(adev);
2348 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2353 * amdgpu_device_fini - tear down the driver
2355 * @adev: amdgpu_device pointer
2357 * Tear down the driver info (all asics).
2358 * Called at driver shutdown.
2360 void amdgpu_device_fini(struct amdgpu_device *adev)
2364 DRM_INFO("amdgpu: finishing device.\n");
2365 adev->shutdown = true;
2366 if (adev->mode_info.mode_config_initialized)
2367 drm_crtc_force_disable_all(adev->ddev);
2368 /* evict vram memory */
2369 amdgpu_bo_evict_vram(adev);
2370 amdgpu_ib_pool_fini(adev);
2371 amdgpu_fw_reserve_vram_fini(adev);
2372 amdgpu_fence_driver_fini(adev);
2373 amdgpu_fbdev_fini(adev);
2374 r = amdgpu_fini(adev);
2375 if (adev->firmware.gpu_info_fw) {
2376 release_firmware(adev->firmware.gpu_info_fw);
2377 adev->firmware.gpu_info_fw = NULL;
2379 adev->accel_working = false;
2380 cancel_delayed_work_sync(&adev->late_init_work);
2381 /* free i2c buses */
2382 amdgpu_i2c_fini(adev);
2383 amdgpu_atombios_fini(adev);
2386 if (!pci_is_thunderbolt_attached(adev->pdev))
2387 vga_switcheroo_unregister_client(adev->pdev);
2388 if (adev->flags & AMD_IS_PX)
2389 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2390 vga_client_register(adev->pdev, NULL, NULL, NULL);
2392 pci_iounmap(adev->pdev, adev->rio_mem);
2393 adev->rio_mem = NULL;
2394 iounmap(adev->rmmio);
2396 amdgpu_doorbell_fini(adev);
2397 amdgpu_pm_sysfs_fini(adev);
2398 amdgpu_debugfs_regs_cleanup(adev);
2406 * amdgpu_device_suspend - initiate device suspend
2408 * @pdev: drm dev pointer
2409 * @state: suspend state
2411 * Puts the hw in the suspend state (all asics).
2412 * Returns 0 for success or an error on failure.
2413 * Called at driver suspend.
2415 int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
2417 struct amdgpu_device *adev;
2418 struct drm_crtc *crtc;
2419 struct drm_connector *connector;
2422 if (dev == NULL || dev->dev_private == NULL) {
2426 adev = dev->dev_private;
2428 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2431 drm_kms_helper_poll_disable(dev);
2433 /* turn off display hw */
2434 drm_modeset_lock_all(dev);
2435 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2436 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2438 drm_modeset_unlock_all(dev);
2440 amdgpu_amdkfd_suspend(adev);
2442 /* unpin the front buffers and cursors */
2443 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2444 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2445 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2446 struct amdgpu_bo *robj;
2448 if (amdgpu_crtc->cursor_bo) {
2449 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2450 r = amdgpu_bo_reserve(aobj, true);
2452 amdgpu_bo_unpin(aobj);
2453 amdgpu_bo_unreserve(aobj);
2457 if (rfb == NULL || rfb->obj == NULL) {
2460 robj = gem_to_amdgpu_bo(rfb->obj);
2461 /* don't unpin kernel fb objects */
2462 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
2463 r = amdgpu_bo_reserve(robj, true);
2465 amdgpu_bo_unpin(robj);
2466 amdgpu_bo_unreserve(robj);
2470 /* evict vram memory */
2471 amdgpu_bo_evict_vram(adev);
2473 amdgpu_fence_driver_suspend(adev);
2475 r = amdgpu_suspend(adev);
2477 /* evict remaining vram memory
2478 * This second call to evict vram is to evict the gart page table
2481 amdgpu_bo_evict_vram(adev);
2483 amdgpu_atombios_scratch_regs_save(adev);
2484 pci_save_state(dev->pdev);
2486 /* Shut down the device */
2487 pci_disable_device(dev->pdev);
2488 pci_set_power_state(dev->pdev, PCI_D3hot);
2490 r = amdgpu_asic_reset(adev);
2492 DRM_ERROR("amdgpu asic reset failed\n");
2497 amdgpu_fbdev_set_suspend(adev, 1);
2504 * amdgpu_device_resume - initiate device resume
2506 * @pdev: drm dev pointer
2508 * Bring the hw back to operating state (all asics).
2509 * Returns 0 for success or an error on failure.
2510 * Called at driver resume.
2512 int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
2514 struct drm_connector *connector;
2515 struct amdgpu_device *adev = dev->dev_private;
2516 struct drm_crtc *crtc;
2519 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2526 pci_set_power_state(dev->pdev, PCI_D0);
2527 pci_restore_state(dev->pdev);
2528 r = pci_enable_device(dev->pdev);
2532 amdgpu_atombios_scratch_regs_restore(adev);
2535 if (amdgpu_need_post(adev)) {
2536 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2538 DRM_ERROR("amdgpu asic init failed\n");
2541 r = amdgpu_resume(adev);
2543 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
2546 amdgpu_fence_driver_resume(adev);
2549 r = amdgpu_ib_ring_tests(adev);
2551 DRM_ERROR("ib ring test failed (%d).\n", r);
2554 r = amdgpu_late_init(adev);
2559 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2560 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2562 if (amdgpu_crtc->cursor_bo) {
2563 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2564 r = amdgpu_bo_reserve(aobj, true);
2566 r = amdgpu_bo_pin(aobj,
2567 AMDGPU_GEM_DOMAIN_VRAM,
2568 &amdgpu_crtc->cursor_addr);
2570 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2571 amdgpu_bo_unreserve(aobj);
2575 r = amdgpu_amdkfd_resume(adev);
2579 /* blat the mode back in */
2581 drm_helper_resume_force_mode(dev);
2582 /* turn on display hw */
2583 drm_modeset_lock_all(dev);
2584 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2585 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2587 drm_modeset_unlock_all(dev);
2590 drm_kms_helper_poll_enable(dev);
2593 * Most of the connector probing functions try to acquire runtime pm
2594 * refs to ensure that the GPU is powered on when connector polling is
2595 * performed. Since we're calling this from a runtime PM callback,
2596 * trying to acquire rpm refs will cause us to deadlock.
2598 * Since we're guaranteed to be holding the rpm lock, it's safe to
2599 * temporarily disable the rpm helpers so this doesn't deadlock us.
2602 dev->dev->power.disable_depth++;
2604 drm_helper_hpd_irq_event(dev);
2606 dev->dev->power.disable_depth--;
2610 amdgpu_fbdev_set_suspend(adev, 0);
2619 static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2622 bool asic_hang = false;
2624 if (amdgpu_sriov_vf(adev))
2627 for (i = 0; i < adev->num_ip_blocks; i++) {
2628 if (!adev->ip_blocks[i].status.valid)
2630 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2631 adev->ip_blocks[i].status.hang =
2632 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2633 if (adev->ip_blocks[i].status.hang) {
2634 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
2641 static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
2645 for (i = 0; i < adev->num_ip_blocks; i++) {
2646 if (!adev->ip_blocks[i].status.valid)
2648 if (adev->ip_blocks[i].status.hang &&
2649 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2650 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
2659 static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2663 for (i = 0; i < adev->num_ip_blocks; i++) {
2664 if (!adev->ip_blocks[i].status.valid)
2666 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2667 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2668 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2669 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
2670 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
2671 if (adev->ip_blocks[i].status.hang) {
2672 DRM_INFO("Some block need full reset!\n");
2680 static int amdgpu_soft_reset(struct amdgpu_device *adev)
2684 for (i = 0; i < adev->num_ip_blocks; i++) {
2685 if (!adev->ip_blocks[i].status.valid)
2687 if (adev->ip_blocks[i].status.hang &&
2688 adev->ip_blocks[i].version->funcs->soft_reset) {
2689 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
2698 static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2702 for (i = 0; i < adev->num_ip_blocks; i++) {
2703 if (!adev->ip_blocks[i].status.valid)
2705 if (adev->ip_blocks[i].status.hang &&
2706 adev->ip_blocks[i].version->funcs->post_soft_reset)
2707 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
2715 bool amdgpu_need_backup(struct amdgpu_device *adev)
2717 if (adev->flags & AMD_IS_APU)
2720 return amdgpu_lockup_timeout > 0 ? true : false;
2723 static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2724 struct amdgpu_ring *ring,
2725 struct amdgpu_bo *bo,
2726 struct dma_fence **fence)
2734 r = amdgpu_bo_reserve(bo, true);
2737 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2738 /* if bo has been evicted, then no need to recover */
2739 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
2740 r = amdgpu_bo_validate(bo->shadow);
2742 DRM_ERROR("bo validate failed!\n");
2746 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
2749 DRM_ERROR("recover page table failed!\n");
2754 amdgpu_bo_unreserve(bo);
2759 * amdgpu_sriov_gpu_reset - reset the asic
2761 * @adev: amdgpu device pointer
2762 * @job: which job trigger hang
2764 * Attempt the reset the GPU if it has hung (all asics).
2766 * Returns 0 for success or an error on failure.
2768 int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, struct amdgpu_job *job)
2772 struct amdgpu_bo *bo, *tmp;
2773 struct amdgpu_ring *ring;
2774 struct dma_fence *fence = NULL, *next = NULL;
2776 mutex_lock(&adev->virt.lock_reset);
2777 atomic_inc(&adev->gpu_reset_counter);
2778 adev->in_sriov_reset = true;
2781 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2783 /* we start from the ring trigger GPU hang */
2784 j = job ? job->ring->idx : 0;
2786 /* block scheduler */
2787 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2788 ring = adev->rings[i % AMDGPU_MAX_RINGS];
2789 if (!ring || !ring->sched.thread)
2792 kthread_park(ring->sched.thread);
2797 /* here give the last chance to check if job removed from mirror-list
2798 * since we already pay some time on kthread_park */
2799 if (job && list_empty(&job->base.node)) {
2800 kthread_unpark(ring->sched.thread);
2804 if (amd_sched_invalidate_job(&job->base, amdgpu_job_hang_limit))
2805 amd_sched_job_kickout(&job->base);
2807 /* only do job_reset on the hang ring if @job not NULL */
2808 amd_sched_hw_job_reset(&ring->sched);
2810 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2811 amdgpu_fence_driver_force_completion_ring(ring);
2814 /* request to take full control of GPU before re-initialization */
2816 amdgpu_virt_reset_gpu(adev);
2818 amdgpu_virt_request_full_gpu(adev, true);
2821 /* Resume IP prior to SMC */
2822 amdgpu_sriov_reinit_early(adev);
2824 /* we need recover gart prior to run SMC/CP/SDMA resume */
2825 amdgpu_ttm_recover_gart(adev);
2827 /* now we are okay to resume SMC/CP/SDMA */
2828 amdgpu_sriov_reinit_late(adev);
2830 amdgpu_irq_gpu_reset_resume_helper(adev);
2832 if (amdgpu_ib_ring_tests(adev))
2833 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2835 /* release full control of GPU after ib test */
2836 amdgpu_virt_release_full_gpu(adev, true);
2838 DRM_INFO("recover vram bo from shadow\n");
2840 ring = adev->mman.buffer_funcs_ring;
2841 mutex_lock(&adev->shadow_list_lock);
2842 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2844 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2846 r = dma_fence_wait(fence, false);
2848 WARN(r, "recovery from shadow isn't completed\n");
2853 dma_fence_put(fence);
2856 mutex_unlock(&adev->shadow_list_lock);
2859 r = dma_fence_wait(fence, false);
2861 WARN(r, "recovery from shadow isn't completed\n");
2863 dma_fence_put(fence);
2865 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2866 ring = adev->rings[i % AMDGPU_MAX_RINGS];
2867 if (!ring || !ring->sched.thread)
2870 if (job && j != i) {
2871 kthread_unpark(ring->sched.thread);
2875 amd_sched_job_recovery(&ring->sched);
2876 kthread_unpark(ring->sched.thread);
2879 drm_helper_resume_force_mode(adev->ddev);
2881 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2883 /* bad news, how to tell it to userspace ? */
2884 dev_info(adev->dev, "GPU reset failed\n");
2886 dev_info(adev->dev, "GPU reset successed!\n");
2889 adev->in_sriov_reset = false;
2890 mutex_unlock(&adev->virt.lock_reset);
2895 * amdgpu_gpu_reset - reset the asic
2897 * @adev: amdgpu device pointer
2899 * Attempt the reset the GPU if it has hung (all asics).
2900 * Returns 0 for success or an error on failure.
2902 int amdgpu_gpu_reset(struct amdgpu_device *adev)
2906 bool need_full_reset, vram_lost = false;
2908 if (!amdgpu_check_soft_reset(adev)) {
2909 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2913 atomic_inc(&adev->gpu_reset_counter);
2916 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2918 /* block scheduler */
2919 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2920 struct amdgpu_ring *ring = adev->rings[i];
2922 if (!ring || !ring->sched.thread)
2924 kthread_park(ring->sched.thread);
2925 amd_sched_hw_job_reset(&ring->sched);
2927 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2928 amdgpu_fence_driver_force_completion(adev);
2930 need_full_reset = amdgpu_need_full_reset(adev);
2932 if (!need_full_reset) {
2933 amdgpu_pre_soft_reset(adev);
2934 r = amdgpu_soft_reset(adev);
2935 amdgpu_post_soft_reset(adev);
2936 if (r || amdgpu_check_soft_reset(adev)) {
2937 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2938 need_full_reset = true;
2942 if (need_full_reset) {
2943 r = amdgpu_suspend(adev);
2946 amdgpu_atombios_scratch_regs_save(adev);
2947 r = amdgpu_asic_reset(adev);
2948 amdgpu_atombios_scratch_regs_restore(adev);
2950 amdgpu_atom_asic_init(adev->mode_info.atom_context);
2953 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
2954 r = amdgpu_resume_phase1(adev);
2957 vram_lost = amdgpu_check_vram_lost(adev);
2959 DRM_ERROR("VRAM is lost!\n");
2960 atomic_inc(&adev->vram_lost_counter);
2962 r = amdgpu_ttm_recover_gart(adev);
2965 r = amdgpu_resume_phase2(adev);
2969 amdgpu_fill_reset_magic(adev);
2974 amdgpu_irq_gpu_reset_resume_helper(adev);
2975 r = amdgpu_ib_ring_tests(adev);
2977 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
2978 r = amdgpu_suspend(adev);
2979 need_full_reset = true;
2983 * recovery vm page tables, since we cannot depend on VRAM is
2984 * consistent after gpu full reset.
2986 if (need_full_reset && amdgpu_need_backup(adev)) {
2987 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2988 struct amdgpu_bo *bo, *tmp;
2989 struct dma_fence *fence = NULL, *next = NULL;
2991 DRM_INFO("recover vram bo from shadow\n");
2992 mutex_lock(&adev->shadow_list_lock);
2993 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2995 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2997 r = dma_fence_wait(fence, false);
2999 WARN(r, "recovery from shadow isn't completed\n");
3004 dma_fence_put(fence);
3007 mutex_unlock(&adev->shadow_list_lock);
3009 r = dma_fence_wait(fence, false);
3011 WARN(r, "recovery from shadow isn't completed\n");
3013 dma_fence_put(fence);
3015 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3016 struct amdgpu_ring *ring = adev->rings[i];
3018 if (!ring || !ring->sched.thread)
3021 amd_sched_job_recovery(&ring->sched);
3022 kthread_unpark(ring->sched.thread);
3025 dev_err(adev->dev, "asic resume failed (%d).\n", r);
3026 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ASIC_RESUME_FAIL, 0, r);
3027 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3028 if (adev->rings[i] && adev->rings[i]->sched.thread) {
3029 kthread_unpark(adev->rings[i]->sched.thread);
3034 drm_helper_resume_force_mode(adev->ddev);
3036 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
3038 /* bad news, how to tell it to userspace ? */
3039 dev_info(adev->dev, "GPU reset failed\n");
3040 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
3043 dev_info(adev->dev, "GPU reset successed!\n");
3046 amdgpu_vf_error_trans_all(adev);
3050 void amdgpu_get_pcie_info(struct amdgpu_device *adev)
3055 if (amdgpu_pcie_gen_cap)
3056 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
3058 if (amdgpu_pcie_lane_cap)
3059 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
3061 /* covers APUs as well */
3062 if (pci_is_root_bus(adev->pdev->bus)) {
3063 if (adev->pm.pcie_gen_mask == 0)
3064 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3065 if (adev->pm.pcie_mlw_mask == 0)
3066 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
3070 if (adev->pm.pcie_gen_mask == 0) {
3071 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
3073 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3074 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3075 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3077 if (mask & DRM_PCIE_SPEED_25)
3078 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
3079 if (mask & DRM_PCIE_SPEED_50)
3080 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
3081 if (mask & DRM_PCIE_SPEED_80)
3082 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
3084 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3087 if (adev->pm.pcie_mlw_mask == 0) {
3088 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
3092 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3093 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3094 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3095 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3096 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3097 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3098 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3101 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3102 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3103 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3104 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3105 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3106 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3109 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3110 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3111 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3112 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3113 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3116 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3117 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3118 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3119 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3122 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3123 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3124 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3127 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3128 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3131 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3137 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
3145 int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
3146 const struct drm_info_list *files,
3151 for (i = 0; i < adev->debugfs_count; i++) {
3152 if (adev->debugfs[i].files == files) {
3153 /* Already registered */
3158 i = adev->debugfs_count + 1;
3159 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
3160 DRM_ERROR("Reached maximum number of debugfs components.\n");
3161 DRM_ERROR("Report so we increase "
3162 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
3165 adev->debugfs[adev->debugfs_count].files = files;
3166 adev->debugfs[adev->debugfs_count].num_files = nfiles;
3167 adev->debugfs_count = i;
3168 #if defined(CONFIG_DEBUG_FS)
3169 drm_debugfs_create_files(files, nfiles,
3170 adev->ddev->primary->debugfs_root,
3171 adev->ddev->primary);
3176 #if defined(CONFIG_DEBUG_FS)
3178 static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
3179 size_t size, loff_t *pos)
3181 struct amdgpu_device *adev = file_inode(f)->i_private;
3184 bool pm_pg_lock, use_bank;
3185 unsigned instance_bank, sh_bank, se_bank;
3187 if (size & 0x3 || *pos & 0x3)
3190 /* are we reading registers for which a PG lock is necessary? */
3191 pm_pg_lock = (*pos >> 23) & 1;
3193 if (*pos & (1ULL << 62)) {
3194 se_bank = (*pos >> 24) & 0x3FF;
3195 sh_bank = (*pos >> 34) & 0x3FF;
3196 instance_bank = (*pos >> 44) & 0x3FF;
3198 if (se_bank == 0x3FF)
3199 se_bank = 0xFFFFFFFF;
3200 if (sh_bank == 0x3FF)
3201 sh_bank = 0xFFFFFFFF;
3202 if (instance_bank == 0x3FF)
3203 instance_bank = 0xFFFFFFFF;
3209 *pos &= (1UL << 22) - 1;
3212 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3213 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3215 mutex_lock(&adev->grbm_idx_mutex);
3216 amdgpu_gfx_select_se_sh(adev, se_bank,
3217 sh_bank, instance_bank);
3221 mutex_lock(&adev->pm.mutex);
3226 if (*pos > adev->rmmio_size)
3229 value = RREG32(*pos >> 2);
3230 r = put_user(value, (uint32_t *)buf);
3244 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3245 mutex_unlock(&adev->grbm_idx_mutex);
3249 mutex_unlock(&adev->pm.mutex);
3254 static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
3255 size_t size, loff_t *pos)
3257 struct amdgpu_device *adev = file_inode(f)->i_private;
3260 bool pm_pg_lock, use_bank;
3261 unsigned instance_bank, sh_bank, se_bank;
3263 if (size & 0x3 || *pos & 0x3)
3266 /* are we reading registers for which a PG lock is necessary? */
3267 pm_pg_lock = (*pos >> 23) & 1;
3269 if (*pos & (1ULL << 62)) {
3270 se_bank = (*pos >> 24) & 0x3FF;
3271 sh_bank = (*pos >> 34) & 0x3FF;
3272 instance_bank = (*pos >> 44) & 0x3FF;
3274 if (se_bank == 0x3FF)
3275 se_bank = 0xFFFFFFFF;
3276 if (sh_bank == 0x3FF)
3277 sh_bank = 0xFFFFFFFF;
3278 if (instance_bank == 0x3FF)
3279 instance_bank = 0xFFFFFFFF;
3285 *pos &= (1UL << 22) - 1;
3288 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3289 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3291 mutex_lock(&adev->grbm_idx_mutex);
3292 amdgpu_gfx_select_se_sh(adev, se_bank,
3293 sh_bank, instance_bank);
3297 mutex_lock(&adev->pm.mutex);
3302 if (*pos > adev->rmmio_size)
3305 r = get_user(value, (uint32_t *)buf);
3309 WREG32(*pos >> 2, value);
3318 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3319 mutex_unlock(&adev->grbm_idx_mutex);
3323 mutex_unlock(&adev->pm.mutex);
3328 static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
3329 size_t size, loff_t *pos)
3331 struct amdgpu_device *adev = file_inode(f)->i_private;
3335 if (size & 0x3 || *pos & 0x3)
3341 value = RREG32_PCIE(*pos >> 2);
3342 r = put_user(value, (uint32_t *)buf);
3355 static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
3356 size_t size, loff_t *pos)
3358 struct amdgpu_device *adev = file_inode(f)->i_private;
3362 if (size & 0x3 || *pos & 0x3)
3368 r = get_user(value, (uint32_t *)buf);
3372 WREG32_PCIE(*pos >> 2, value);
3383 static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
3384 size_t size, loff_t *pos)
3386 struct amdgpu_device *adev = file_inode(f)->i_private;
3390 if (size & 0x3 || *pos & 0x3)
3396 value = RREG32_DIDT(*pos >> 2);
3397 r = put_user(value, (uint32_t *)buf);
3410 static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3411 size_t size, loff_t *pos)
3413 struct amdgpu_device *adev = file_inode(f)->i_private;
3417 if (size & 0x3 || *pos & 0x3)
3423 r = get_user(value, (uint32_t *)buf);
3427 WREG32_DIDT(*pos >> 2, value);
3438 static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3439 size_t size, loff_t *pos)
3441 struct amdgpu_device *adev = file_inode(f)->i_private;
3445 if (size & 0x3 || *pos & 0x3)
3451 value = RREG32_SMC(*pos);
3452 r = put_user(value, (uint32_t *)buf);
3465 static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3466 size_t size, loff_t *pos)
3468 struct amdgpu_device *adev = file_inode(f)->i_private;
3472 if (size & 0x3 || *pos & 0x3)
3478 r = get_user(value, (uint32_t *)buf);
3482 WREG32_SMC(*pos, value);
3493 static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3494 size_t size, loff_t *pos)
3496 struct amdgpu_device *adev = file_inode(f)->i_private;
3499 uint32_t *config, no_regs = 0;
3501 if (size & 0x3 || *pos & 0x3)
3504 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
3508 /* version, increment each time something is added */
3509 config[no_regs++] = 3;
3510 config[no_regs++] = adev->gfx.config.max_shader_engines;
3511 config[no_regs++] = adev->gfx.config.max_tile_pipes;
3512 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3513 config[no_regs++] = adev->gfx.config.max_sh_per_se;
3514 config[no_regs++] = adev->gfx.config.max_backends_per_se;
3515 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3516 config[no_regs++] = adev->gfx.config.max_gprs;
3517 config[no_regs++] = adev->gfx.config.max_gs_threads;
3518 config[no_regs++] = adev->gfx.config.max_hw_contexts;
3519 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3520 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3521 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3522 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3523 config[no_regs++] = adev->gfx.config.num_tile_pipes;
3524 config[no_regs++] = adev->gfx.config.backend_enable_mask;
3525 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3526 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3527 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3528 config[no_regs++] = adev->gfx.config.num_gpus;
3529 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3530 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3531 config[no_regs++] = adev->gfx.config.gb_addr_config;
3532 config[no_regs++] = adev->gfx.config.num_rbs;
3535 config[no_regs++] = adev->rev_id;
3536 config[no_regs++] = adev->pg_flags;
3537 config[no_regs++] = adev->cg_flags;
3540 config[no_regs++] = adev->family;
3541 config[no_regs++] = adev->external_rev_id;
3544 config[no_regs++] = adev->pdev->device;
3545 config[no_regs++] = adev->pdev->revision;
3546 config[no_regs++] = adev->pdev->subsystem_device;
3547 config[no_regs++] = adev->pdev->subsystem_vendor;
3549 while (size && (*pos < no_regs * 4)) {
3552 value = config[*pos >> 2];
3553 r = put_user(value, (uint32_t *)buf);
3569 static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
3570 size_t size, loff_t *pos)
3572 struct amdgpu_device *adev = file_inode(f)->i_private;
3573 int idx, x, outsize, r, valuesize;
3574 uint32_t values[16];
3576 if (size & 3 || *pos & 0x3)
3579 if (amdgpu_dpm == 0)
3582 /* convert offset to sensor number */
3585 valuesize = sizeof(values);
3586 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
3587 r = amdgpu_dpm_read_sensor(adev, idx, &values[0], &valuesize);
3591 if (size > valuesize)
3598 r = put_user(values[x++], (int32_t *)buf);
3605 return !r ? outsize : r;
3608 static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3609 size_t size, loff_t *pos)
3611 struct amdgpu_device *adev = f->f_inode->i_private;
3614 uint32_t offset, se, sh, cu, wave, simd, data[32];
3616 if (size & 3 || *pos & 3)
3620 offset = (*pos & 0x7F);
3621 se = ((*pos >> 7) & 0xFF);
3622 sh = ((*pos >> 15) & 0xFF);
3623 cu = ((*pos >> 23) & 0xFF);
3624 wave = ((*pos >> 31) & 0xFF);
3625 simd = ((*pos >> 37) & 0xFF);
3627 /* switch to the specific se/sh/cu */
3628 mutex_lock(&adev->grbm_idx_mutex);
3629 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3632 if (adev->gfx.funcs->read_wave_data)
3633 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
3635 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3636 mutex_unlock(&adev->grbm_idx_mutex);
3641 while (size && (offset < x * 4)) {
3644 value = data[offset >> 2];
3645 r = put_user(value, (uint32_t *)buf);
3658 static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3659 size_t size, loff_t *pos)
3661 struct amdgpu_device *adev = f->f_inode->i_private;
3664 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3666 if (size & 3 || *pos & 3)
3670 offset = (*pos & 0xFFF); /* in dwords */
3671 se = ((*pos >> 12) & 0xFF);
3672 sh = ((*pos >> 20) & 0xFF);
3673 cu = ((*pos >> 28) & 0xFF);
3674 wave = ((*pos >> 36) & 0xFF);
3675 simd = ((*pos >> 44) & 0xFF);
3676 thread = ((*pos >> 52) & 0xFF);
3677 bank = ((*pos >> 60) & 1);
3679 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3683 /* switch to the specific se/sh/cu */
3684 mutex_lock(&adev->grbm_idx_mutex);
3685 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3688 if (adev->gfx.funcs->read_wave_vgprs)
3689 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3691 if (adev->gfx.funcs->read_wave_sgprs)
3692 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3695 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3696 mutex_unlock(&adev->grbm_idx_mutex);
3701 value = data[offset++];
3702 r = put_user(value, (uint32_t *)buf);
3718 static const struct file_operations amdgpu_debugfs_regs_fops = {
3719 .owner = THIS_MODULE,
3720 .read = amdgpu_debugfs_regs_read,
3721 .write = amdgpu_debugfs_regs_write,
3722 .llseek = default_llseek
3724 static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3725 .owner = THIS_MODULE,
3726 .read = amdgpu_debugfs_regs_didt_read,
3727 .write = amdgpu_debugfs_regs_didt_write,
3728 .llseek = default_llseek
3730 static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3731 .owner = THIS_MODULE,
3732 .read = amdgpu_debugfs_regs_pcie_read,
3733 .write = amdgpu_debugfs_regs_pcie_write,
3734 .llseek = default_llseek
3736 static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3737 .owner = THIS_MODULE,
3738 .read = amdgpu_debugfs_regs_smc_read,
3739 .write = amdgpu_debugfs_regs_smc_write,
3740 .llseek = default_llseek
3743 static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3744 .owner = THIS_MODULE,
3745 .read = amdgpu_debugfs_gca_config_read,
3746 .llseek = default_llseek
3749 static const struct file_operations amdgpu_debugfs_sensors_fops = {
3750 .owner = THIS_MODULE,
3751 .read = amdgpu_debugfs_sensor_read,
3752 .llseek = default_llseek
3755 static const struct file_operations amdgpu_debugfs_wave_fops = {
3756 .owner = THIS_MODULE,
3757 .read = amdgpu_debugfs_wave_read,
3758 .llseek = default_llseek
3760 static const struct file_operations amdgpu_debugfs_gpr_fops = {
3761 .owner = THIS_MODULE,
3762 .read = amdgpu_debugfs_gpr_read,
3763 .llseek = default_llseek
3766 static const struct file_operations *debugfs_regs[] = {
3767 &amdgpu_debugfs_regs_fops,
3768 &amdgpu_debugfs_regs_didt_fops,
3769 &amdgpu_debugfs_regs_pcie_fops,
3770 &amdgpu_debugfs_regs_smc_fops,
3771 &amdgpu_debugfs_gca_config_fops,
3772 &amdgpu_debugfs_sensors_fops,
3773 &amdgpu_debugfs_wave_fops,
3774 &amdgpu_debugfs_gpr_fops,
3777 static const char *debugfs_regs_names[] = {
3782 "amdgpu_gca_config",
3788 static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3790 struct drm_minor *minor = adev->ddev->primary;
3791 struct dentry *ent, *root = minor->debugfs_root;
3794 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3795 ent = debugfs_create_file(debugfs_regs_names[i],
3796 S_IFREG | S_IRUGO, root,
3797 adev, debugfs_regs[i]);
3799 for (j = 0; j < i; j++) {
3800 debugfs_remove(adev->debugfs_regs[i]);
3801 adev->debugfs_regs[i] = NULL;
3803 return PTR_ERR(ent);
3807 i_size_write(ent->d_inode, adev->rmmio_size);
3808 adev->debugfs_regs[i] = ent;
3814 static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3818 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3819 if (adev->debugfs_regs[i]) {
3820 debugfs_remove(adev->debugfs_regs[i]);
3821 adev->debugfs_regs[i] = NULL;
3826 static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data)
3828 struct drm_info_node *node = (struct drm_info_node *) m->private;
3829 struct drm_device *dev = node->minor->dev;
3830 struct amdgpu_device *adev = dev->dev_private;
3833 /* hold on the scheduler */
3834 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3835 struct amdgpu_ring *ring = adev->rings[i];
3837 if (!ring || !ring->sched.thread)
3839 kthread_park(ring->sched.thread);
3842 seq_printf(m, "run ib test:\n");
3843 r = amdgpu_ib_ring_tests(adev);
3845 seq_printf(m, "ib ring tests failed (%d).\n", r);
3847 seq_printf(m, "ib ring tests passed.\n");
3849 /* go on the scheduler */
3850 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3851 struct amdgpu_ring *ring = adev->rings[i];
3853 if (!ring || !ring->sched.thread)
3855 kthread_unpark(ring->sched.thread);
3861 static const struct drm_info_list amdgpu_debugfs_test_ib_ring_list[] = {
3862 {"amdgpu_test_ib", &amdgpu_debugfs_test_ib}
3865 static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
3867 return amdgpu_debugfs_add_files(adev,
3868 amdgpu_debugfs_test_ib_ring_list, 1);
3871 int amdgpu_debugfs_init(struct drm_minor *minor)
3876 static int amdgpu_debugfs_get_vbios_dump(struct seq_file *m, void *data)
3878 struct drm_info_node *node = (struct drm_info_node *) m->private;
3879 struct drm_device *dev = node->minor->dev;
3880 struct amdgpu_device *adev = dev->dev_private;
3882 seq_write(m, adev->bios, adev->bios_size);
3886 static const struct drm_info_list amdgpu_vbios_dump_list[] = {
3888 amdgpu_debugfs_get_vbios_dump,
3892 static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev)
3894 return amdgpu_debugfs_add_files(adev,
3895 amdgpu_vbios_dump_list, 1);
3898 static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
3902 static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3906 static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev)
3910 static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }