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 << 3; /* 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 >> 3, 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 (amdgpu_sriov_vf(adev))
750 if (amdgpu_passthrough(adev)) {
751 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
752 * some old smc fw still need driver do vPost otherwise gpu hang, while
753 * those smc fw version above 22.15 doesn't have this flaw, so we force
754 * vpost executed for smc version below 22.15
756 if (adev->asic_type == CHIP_FIJI) {
759 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
760 /* force vPost if error occured */
764 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
765 if (fw_ver < 0x00160e00)
770 if (adev->has_hw_reset) {
771 adev->has_hw_reset = false;
775 /* bios scratch used on CIK+ */
776 if (adev->asic_type >= CHIP_BONAIRE)
777 return amdgpu_atombios_scratch_need_asic_init(adev);
779 /* check MEM_SIZE for older asics */
780 reg = amdgpu_asic_get_config_memsize(adev);
782 if ((reg != 0) && (reg != 0xffffffff))
789 * amdgpu_dummy_page_init - init dummy page used by the driver
791 * @adev: amdgpu_device pointer
793 * Allocate the dummy page used by the driver (all asics).
794 * This dummy page is used by the driver as a filler for gart entries
795 * when pages are taken out of the GART
796 * Returns 0 on sucess, -ENOMEM on failure.
798 int amdgpu_dummy_page_init(struct amdgpu_device *adev)
800 if (adev->dummy_page.page)
802 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
803 if (adev->dummy_page.page == NULL)
805 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
806 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
807 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
808 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
809 __free_page(adev->dummy_page.page);
810 adev->dummy_page.page = NULL;
817 * amdgpu_dummy_page_fini - free dummy page used by the driver
819 * @adev: amdgpu_device pointer
821 * Frees the dummy page used by the driver (all asics).
823 void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
825 if (adev->dummy_page.page == NULL)
827 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
828 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
829 __free_page(adev->dummy_page.page);
830 adev->dummy_page.page = NULL;
834 /* ATOM accessor methods */
836 * ATOM is an interpreted byte code stored in tables in the vbios. The
837 * driver registers callbacks to access registers and the interpreter
838 * in the driver parses the tables and executes then to program specific
839 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
840 * atombios.h, and atom.c
844 * cail_pll_read - read PLL register
846 * @info: atom card_info pointer
847 * @reg: PLL register offset
849 * Provides a PLL register accessor for the atom interpreter (r4xx+).
850 * Returns the value of the PLL register.
852 static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
858 * cail_pll_write - write PLL register
860 * @info: atom card_info pointer
861 * @reg: PLL register offset
862 * @val: value to write to the pll register
864 * Provides a PLL register accessor for the atom interpreter (r4xx+).
866 static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
872 * cail_mc_read - read MC (Memory Controller) register
874 * @info: atom card_info pointer
875 * @reg: MC register offset
877 * Provides an MC register accessor for the atom interpreter (r4xx+).
878 * Returns the value of the MC register.
880 static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
886 * cail_mc_write - write MC (Memory Controller) register
888 * @info: atom card_info pointer
889 * @reg: MC register offset
890 * @val: value to write to the pll register
892 * Provides a MC register accessor for the atom interpreter (r4xx+).
894 static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
900 * cail_reg_write - write MMIO register
902 * @info: atom card_info pointer
903 * @reg: MMIO register offset
904 * @val: value to write to the pll register
906 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
908 static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
910 struct amdgpu_device *adev = info->dev->dev_private;
916 * cail_reg_read - read MMIO register
918 * @info: atom card_info pointer
919 * @reg: MMIO register offset
921 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
922 * Returns the value of the MMIO register.
924 static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
926 struct amdgpu_device *adev = info->dev->dev_private;
934 * cail_ioreg_write - write IO register
936 * @info: atom card_info pointer
937 * @reg: IO register offset
938 * @val: value to write to the pll register
940 * Provides a IO register accessor for the atom interpreter (r4xx+).
942 static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
944 struct amdgpu_device *adev = info->dev->dev_private;
950 * cail_ioreg_read - read IO register
952 * @info: atom card_info pointer
953 * @reg: IO register offset
955 * Provides an IO register accessor for the atom interpreter (r4xx+).
956 * Returns the value of the IO register.
958 static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
960 struct amdgpu_device *adev = info->dev->dev_private;
967 static ssize_t amdgpu_atombios_get_vbios_version(struct device *dev,
968 struct device_attribute *attr,
971 struct drm_device *ddev = dev_get_drvdata(dev);
972 struct amdgpu_device *adev = ddev->dev_private;
973 struct atom_context *ctx = adev->mode_info.atom_context;
975 return snprintf(buf, PAGE_SIZE, "%s\n", ctx->vbios_version);
978 static DEVICE_ATTR(vbios_version, 0444, amdgpu_atombios_get_vbios_version,
982 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
984 * @adev: amdgpu_device pointer
986 * Frees the driver info and register access callbacks for the ATOM
987 * interpreter (r4xx+).
988 * Called at driver shutdown.
990 static void amdgpu_atombios_fini(struct amdgpu_device *adev)
992 if (adev->mode_info.atom_context) {
993 kfree(adev->mode_info.atom_context->scratch);
994 kfree(adev->mode_info.atom_context->iio);
996 kfree(adev->mode_info.atom_context);
997 adev->mode_info.atom_context = NULL;
998 kfree(adev->mode_info.atom_card_info);
999 adev->mode_info.atom_card_info = NULL;
1000 device_remove_file(adev->dev, &dev_attr_vbios_version);
1004 * amdgpu_atombios_init - init the driver info and callbacks for atombios
1006 * @adev: amdgpu_device pointer
1008 * Initializes the driver info and register access callbacks for the
1009 * ATOM interpreter (r4xx+).
1010 * Returns 0 on sucess, -ENOMEM on failure.
1011 * Called at driver startup.
1013 static int amdgpu_atombios_init(struct amdgpu_device *adev)
1015 struct card_info *atom_card_info =
1016 kzalloc(sizeof(struct card_info), GFP_KERNEL);
1019 if (!atom_card_info)
1022 adev->mode_info.atom_card_info = atom_card_info;
1023 atom_card_info->dev = adev->ddev;
1024 atom_card_info->reg_read = cail_reg_read;
1025 atom_card_info->reg_write = cail_reg_write;
1026 /* needed for iio ops */
1027 if (adev->rio_mem) {
1028 atom_card_info->ioreg_read = cail_ioreg_read;
1029 atom_card_info->ioreg_write = cail_ioreg_write;
1031 DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
1032 atom_card_info->ioreg_read = cail_reg_read;
1033 atom_card_info->ioreg_write = cail_reg_write;
1035 atom_card_info->mc_read = cail_mc_read;
1036 atom_card_info->mc_write = cail_mc_write;
1037 atom_card_info->pll_read = cail_pll_read;
1038 atom_card_info->pll_write = cail_pll_write;
1040 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
1041 if (!adev->mode_info.atom_context) {
1042 amdgpu_atombios_fini(adev);
1046 mutex_init(&adev->mode_info.atom_context->mutex);
1047 if (adev->is_atom_fw) {
1048 amdgpu_atomfirmware_scratch_regs_init(adev);
1049 amdgpu_atomfirmware_allocate_fb_scratch(adev);
1051 amdgpu_atombios_scratch_regs_init(adev);
1052 amdgpu_atombios_allocate_fb_scratch(adev);
1055 ret = device_create_file(adev->dev, &dev_attr_vbios_version);
1057 DRM_ERROR("Failed to create device file for VBIOS version\n");
1064 /* if we get transitioned to only one device, take VGA back */
1066 * amdgpu_vga_set_decode - enable/disable vga decode
1068 * @cookie: amdgpu_device pointer
1069 * @state: enable/disable vga decode
1071 * Enable/disable vga decode (all asics).
1072 * Returns VGA resource flags.
1074 static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
1076 struct amdgpu_device *adev = cookie;
1077 amdgpu_asic_set_vga_state(adev, state);
1079 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1080 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1082 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1085 static void amdgpu_check_block_size(struct amdgpu_device *adev)
1087 /* defines number of bits in page table versus page directory,
1088 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1089 * page table and the remaining bits are in the page directory */
1090 if (amdgpu_vm_block_size == -1)
1093 if (amdgpu_vm_block_size < 9) {
1094 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1095 amdgpu_vm_block_size);
1099 if (amdgpu_vm_block_size > 24 ||
1100 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1101 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1102 amdgpu_vm_block_size);
1109 amdgpu_vm_block_size = -1;
1112 static void amdgpu_check_vm_size(struct amdgpu_device *adev)
1114 /* no need to check the default value */
1115 if (amdgpu_vm_size == -1)
1118 if (!is_power_of_2(amdgpu_vm_size)) {
1119 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
1124 if (amdgpu_vm_size < 1) {
1125 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1131 * Max GPUVM size for Cayman, SI, CI VI are 40 bits.
1133 if (amdgpu_vm_size > 1024) {
1134 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1142 amdgpu_vm_size = -1;
1146 * amdgpu_check_arguments - validate module params
1148 * @adev: amdgpu_device pointer
1150 * Validates certain module parameters and updates
1151 * the associated values used by the driver (all asics).
1153 static void amdgpu_check_arguments(struct amdgpu_device *adev)
1155 if (amdgpu_sched_jobs < 4) {
1156 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1158 amdgpu_sched_jobs = 4;
1159 } else if (!is_power_of_2(amdgpu_sched_jobs)){
1160 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1162 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1165 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
1166 /* gart size must be greater or equal to 32M */
1167 dev_warn(adev->dev, "gart size (%d) too small\n",
1169 amdgpu_gart_size = -1;
1172 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
1173 /* gtt size must be greater or equal to 32M */
1174 dev_warn(adev->dev, "gtt size (%d) too small\n",
1176 amdgpu_gtt_size = -1;
1179 /* valid range is between 4 and 9 inclusive */
1180 if (amdgpu_vm_fragment_size != -1 &&
1181 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1182 dev_warn(adev->dev, "valid range is between 4 and 9\n");
1183 amdgpu_vm_fragment_size = -1;
1186 amdgpu_check_vm_size(adev);
1188 amdgpu_check_block_size(adev);
1190 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
1191 !is_power_of_2(amdgpu_vram_page_split))) {
1192 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1193 amdgpu_vram_page_split);
1194 amdgpu_vram_page_split = 1024;
1199 * amdgpu_switcheroo_set_state - set switcheroo state
1201 * @pdev: pci dev pointer
1202 * @state: vga_switcheroo state
1204 * Callback for the switcheroo driver. Suspends or resumes the
1205 * the asics before or after it is powered up using ACPI methods.
1207 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1209 struct drm_device *dev = pci_get_drvdata(pdev);
1211 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1214 if (state == VGA_SWITCHEROO_ON) {
1215 pr_info("amdgpu: switched on\n");
1216 /* don't suspend or resume card normally */
1217 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1219 amdgpu_device_resume(dev, true, true);
1221 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1222 drm_kms_helper_poll_enable(dev);
1224 pr_info("amdgpu: switched off\n");
1225 drm_kms_helper_poll_disable(dev);
1226 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1227 amdgpu_device_suspend(dev, true, true);
1228 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1233 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1235 * @pdev: pci dev pointer
1237 * Callback for the switcheroo driver. Check of the switcheroo
1238 * state can be changed.
1239 * Returns true if the state can be changed, false if not.
1241 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1243 struct drm_device *dev = pci_get_drvdata(pdev);
1246 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1247 * locking inversion with the driver load path. And the access here is
1248 * completely racy anyway. So don't bother with locking for now.
1250 return dev->open_count == 0;
1253 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1254 .set_gpu_state = amdgpu_switcheroo_set_state,
1256 .can_switch = amdgpu_switcheroo_can_switch,
1259 int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
1260 enum amd_ip_block_type block_type,
1261 enum amd_clockgating_state state)
1265 for (i = 0; i < adev->num_ip_blocks; i++) {
1266 if (!adev->ip_blocks[i].status.valid)
1268 if (adev->ip_blocks[i].version->type != block_type)
1270 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1272 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1273 (void *)adev, state);
1275 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1276 adev->ip_blocks[i].version->funcs->name, r);
1281 int amdgpu_set_powergating_state(struct amdgpu_device *adev,
1282 enum amd_ip_block_type block_type,
1283 enum amd_powergating_state state)
1287 for (i = 0; i < adev->num_ip_blocks; i++) {
1288 if (!adev->ip_blocks[i].status.valid)
1290 if (adev->ip_blocks[i].version->type != block_type)
1292 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1294 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1295 (void *)adev, state);
1297 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1298 adev->ip_blocks[i].version->funcs->name, r);
1303 void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1307 for (i = 0; i < adev->num_ip_blocks; i++) {
1308 if (!adev->ip_blocks[i].status.valid)
1310 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1311 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1315 int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1316 enum amd_ip_block_type block_type)
1320 for (i = 0; i < adev->num_ip_blocks; i++) {
1321 if (!adev->ip_blocks[i].status.valid)
1323 if (adev->ip_blocks[i].version->type == block_type) {
1324 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
1334 bool amdgpu_is_idle(struct amdgpu_device *adev,
1335 enum amd_ip_block_type block_type)
1339 for (i = 0; i < adev->num_ip_blocks; i++) {
1340 if (!adev->ip_blocks[i].status.valid)
1342 if (adev->ip_blocks[i].version->type == block_type)
1343 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
1349 struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1350 enum amd_ip_block_type type)
1354 for (i = 0; i < adev->num_ip_blocks; i++)
1355 if (adev->ip_blocks[i].version->type == type)
1356 return &adev->ip_blocks[i];
1362 * amdgpu_ip_block_version_cmp
1364 * @adev: amdgpu_device pointer
1365 * @type: enum amd_ip_block_type
1366 * @major: major version
1367 * @minor: minor version
1369 * return 0 if equal or greater
1370 * return 1 if smaller or the ip_block doesn't exist
1372 int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
1373 enum amd_ip_block_type type,
1374 u32 major, u32 minor)
1376 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
1378 if (ip_block && ((ip_block->version->major > major) ||
1379 ((ip_block->version->major == major) &&
1380 (ip_block->version->minor >= minor))))
1387 * amdgpu_ip_block_add
1389 * @adev: amdgpu_device pointer
1390 * @ip_block_version: pointer to the IP to add
1392 * Adds the IP block driver information to the collection of IPs
1395 int amdgpu_ip_block_add(struct amdgpu_device *adev,
1396 const struct amdgpu_ip_block_version *ip_block_version)
1398 if (!ip_block_version)
1401 DRM_DEBUG("add ip block number %d <%s>\n", adev->num_ip_blocks,
1402 ip_block_version->funcs->name);
1404 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1409 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
1411 adev->enable_virtual_display = false;
1413 if (amdgpu_virtual_display) {
1414 struct drm_device *ddev = adev->ddev;
1415 const char *pci_address_name = pci_name(ddev->pdev);
1416 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
1418 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1419 pciaddstr_tmp = pciaddstr;
1420 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1421 pciaddname = strsep(&pciaddname_tmp, ",");
1422 if (!strcmp("all", pciaddname)
1423 || !strcmp(pci_address_name, pciaddname)) {
1427 adev->enable_virtual_display = true;
1430 res = kstrtol(pciaddname_tmp, 10,
1438 adev->mode_info.num_crtc = num_crtc;
1440 adev->mode_info.num_crtc = 1;
1446 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1447 amdgpu_virtual_display, pci_address_name,
1448 adev->enable_virtual_display, adev->mode_info.num_crtc);
1454 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1456 const char *chip_name;
1459 const struct gpu_info_firmware_header_v1_0 *hdr;
1461 adev->firmware.gpu_info_fw = NULL;
1463 switch (adev->asic_type) {
1467 case CHIP_POLARIS11:
1468 case CHIP_POLARIS10:
1469 case CHIP_POLARIS12:
1472 #ifdef CONFIG_DRM_AMDGPU_SI
1479 #ifdef CONFIG_DRM_AMDGPU_CIK
1489 chip_name = "vega10";
1492 chip_name = "raven";
1496 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
1497 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
1500 "Failed to load gpu_info firmware \"%s\"\n",
1504 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
1507 "Failed to validate gpu_info firmware \"%s\"\n",
1512 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
1513 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1515 switch (hdr->version_major) {
1518 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
1519 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
1520 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1522 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1523 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1524 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1525 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
1526 adev->gfx.config.max_texture_channel_caches =
1527 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1528 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1529 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1530 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1531 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
1532 adev->gfx.config.double_offchip_lds_buf =
1533 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1534 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
1535 adev->gfx.cu_info.max_waves_per_simd =
1536 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1537 adev->gfx.cu_info.max_scratch_slots_per_cu =
1538 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1539 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
1544 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1552 static int amdgpu_early_init(struct amdgpu_device *adev)
1556 amdgpu_device_enable_virtual_display(adev);
1558 switch (adev->asic_type) {
1562 case CHIP_POLARIS11:
1563 case CHIP_POLARIS10:
1564 case CHIP_POLARIS12:
1567 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
1568 adev->family = AMDGPU_FAMILY_CZ;
1570 adev->family = AMDGPU_FAMILY_VI;
1572 r = vi_set_ip_blocks(adev);
1576 #ifdef CONFIG_DRM_AMDGPU_SI
1582 adev->family = AMDGPU_FAMILY_SI;
1583 r = si_set_ip_blocks(adev);
1588 #ifdef CONFIG_DRM_AMDGPU_CIK
1594 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1595 adev->family = AMDGPU_FAMILY_CI;
1597 adev->family = AMDGPU_FAMILY_KV;
1599 r = cik_set_ip_blocks(adev);
1606 if (adev->asic_type == CHIP_RAVEN)
1607 adev->family = AMDGPU_FAMILY_RV;
1609 adev->family = AMDGPU_FAMILY_AI;
1611 r = soc15_set_ip_blocks(adev);
1616 /* FIXME: not supported yet */
1620 r = amdgpu_device_parse_gpu_info_fw(adev);
1624 if (amdgpu_sriov_vf(adev)) {
1625 r = amdgpu_virt_request_full_gpu(adev, true);
1630 for (i = 0; i < adev->num_ip_blocks; i++) {
1631 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1632 DRM_ERROR("disabled ip block: %d <%s>\n",
1633 i, adev->ip_blocks[i].version->funcs->name);
1634 adev->ip_blocks[i].status.valid = false;
1636 if (adev->ip_blocks[i].version->funcs->early_init) {
1637 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
1639 adev->ip_blocks[i].status.valid = false;
1641 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1642 adev->ip_blocks[i].version->funcs->name, r);
1645 adev->ip_blocks[i].status.valid = true;
1648 adev->ip_blocks[i].status.valid = true;
1653 adev->cg_flags &= amdgpu_cg_mask;
1654 adev->pg_flags &= amdgpu_pg_mask;
1659 static int amdgpu_init(struct amdgpu_device *adev)
1663 for (i = 0; i < adev->num_ip_blocks; i++) {
1664 if (!adev->ip_blocks[i].status.valid)
1666 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
1668 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1669 adev->ip_blocks[i].version->funcs->name, r);
1672 adev->ip_blocks[i].status.sw = true;
1673 /* need to do gmc hw init early so we can allocate gpu mem */
1674 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1675 r = amdgpu_vram_scratch_init(adev);
1677 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
1680 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1682 DRM_ERROR("hw_init %d failed %d\n", i, r);
1685 r = amdgpu_wb_init(adev);
1687 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
1690 adev->ip_blocks[i].status.hw = true;
1692 /* right after GMC hw init, we create CSA */
1693 if (amdgpu_sriov_vf(adev)) {
1694 r = amdgpu_allocate_static_csa(adev);
1696 DRM_ERROR("allocate CSA failed %d\n", r);
1703 for (i = 0; i < adev->num_ip_blocks; i++) {
1704 if (!adev->ip_blocks[i].status.sw)
1706 /* gmc hw init is done early */
1707 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
1709 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1711 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1712 adev->ip_blocks[i].version->funcs->name, r);
1715 adev->ip_blocks[i].status.hw = true;
1721 static void amdgpu_fill_reset_magic(struct amdgpu_device *adev)
1723 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1726 static bool amdgpu_check_vram_lost(struct amdgpu_device *adev)
1728 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1729 AMDGPU_RESET_MAGIC_NUM);
1732 static int amdgpu_late_set_cg_state(struct amdgpu_device *adev)
1736 for (i = 0; i < adev->num_ip_blocks; i++) {
1737 if (!adev->ip_blocks[i].status.valid)
1739 /* skip CG for VCE/UVD, it's handled specially */
1740 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1741 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1742 /* enable clockgating to save power */
1743 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1746 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1747 adev->ip_blocks[i].version->funcs->name, r);
1755 static int amdgpu_late_init(struct amdgpu_device *adev)
1759 for (i = 0; i < adev->num_ip_blocks; i++) {
1760 if (!adev->ip_blocks[i].status.valid)
1762 if (adev->ip_blocks[i].version->funcs->late_init) {
1763 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
1765 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1766 adev->ip_blocks[i].version->funcs->name, r);
1769 adev->ip_blocks[i].status.late_initialized = true;
1773 mod_delayed_work(system_wq, &adev->late_init_work,
1774 msecs_to_jiffies(AMDGPU_RESUME_MS));
1776 amdgpu_fill_reset_magic(adev);
1781 static int amdgpu_fini(struct amdgpu_device *adev)
1785 /* need to disable SMC first */
1786 for (i = 0; i < adev->num_ip_blocks; i++) {
1787 if (!adev->ip_blocks[i].status.hw)
1789 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
1790 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1791 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1792 AMD_CG_STATE_UNGATE);
1794 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1795 adev->ip_blocks[i].version->funcs->name, r);
1798 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
1799 /* XXX handle errors */
1801 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1802 adev->ip_blocks[i].version->funcs->name, r);
1804 adev->ip_blocks[i].status.hw = false;
1809 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1810 if (!adev->ip_blocks[i].status.hw)
1812 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1813 amdgpu_wb_fini(adev);
1814 amdgpu_vram_scratch_fini(adev);
1817 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1818 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1819 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1820 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1821 AMD_CG_STATE_UNGATE);
1823 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1824 adev->ip_blocks[i].version->funcs->name, r);
1829 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
1830 /* XXX handle errors */
1832 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1833 adev->ip_blocks[i].version->funcs->name, r);
1836 adev->ip_blocks[i].status.hw = false;
1839 if (adev->firmware.load_type == AMDGPU_FW_LOAD_SMU)
1840 amdgpu_ucode_fini_bo(adev);
1842 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1843 if (!adev->ip_blocks[i].status.sw)
1845 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
1846 /* XXX handle errors */
1848 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1849 adev->ip_blocks[i].version->funcs->name, r);
1851 adev->ip_blocks[i].status.sw = false;
1852 adev->ip_blocks[i].status.valid = false;
1855 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1856 if (!adev->ip_blocks[i].status.late_initialized)
1858 if (adev->ip_blocks[i].version->funcs->late_fini)
1859 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1860 adev->ip_blocks[i].status.late_initialized = false;
1863 if (amdgpu_sriov_vf(adev))
1864 amdgpu_virt_release_full_gpu(adev, false);
1869 static void amdgpu_late_init_func_handler(struct work_struct *work)
1871 struct amdgpu_device *adev =
1872 container_of(work, struct amdgpu_device, late_init_work.work);
1873 amdgpu_late_set_cg_state(adev);
1876 int amdgpu_suspend(struct amdgpu_device *adev)
1880 if (amdgpu_sriov_vf(adev))
1881 amdgpu_virt_request_full_gpu(adev, false);
1883 /* ungate SMC block first */
1884 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1885 AMD_CG_STATE_UNGATE);
1887 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1890 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1891 if (!adev->ip_blocks[i].status.valid)
1893 /* ungate blocks so that suspend can properly shut them down */
1894 if (i != AMD_IP_BLOCK_TYPE_SMC) {
1895 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1896 AMD_CG_STATE_UNGATE);
1898 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1899 adev->ip_blocks[i].version->funcs->name, r);
1902 /* XXX handle errors */
1903 r = adev->ip_blocks[i].version->funcs->suspend(adev);
1904 /* XXX handle errors */
1906 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1907 adev->ip_blocks[i].version->funcs->name, r);
1911 if (amdgpu_sriov_vf(adev))
1912 amdgpu_virt_release_full_gpu(adev, false);
1917 static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
1921 static enum amd_ip_block_type ip_order[] = {
1922 AMD_IP_BLOCK_TYPE_GMC,
1923 AMD_IP_BLOCK_TYPE_COMMON,
1924 AMD_IP_BLOCK_TYPE_IH,
1927 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1929 struct amdgpu_ip_block *block;
1931 for (j = 0; j < adev->num_ip_blocks; j++) {
1932 block = &adev->ip_blocks[j];
1934 if (block->version->type != ip_order[i] ||
1935 !block->status.valid)
1938 r = block->version->funcs->hw_init(adev);
1939 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
1946 static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
1950 static enum amd_ip_block_type ip_order[] = {
1951 AMD_IP_BLOCK_TYPE_SMC,
1952 AMD_IP_BLOCK_TYPE_PSP,
1953 AMD_IP_BLOCK_TYPE_DCE,
1954 AMD_IP_BLOCK_TYPE_GFX,
1955 AMD_IP_BLOCK_TYPE_SDMA,
1956 AMD_IP_BLOCK_TYPE_UVD,
1957 AMD_IP_BLOCK_TYPE_VCE
1960 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1962 struct amdgpu_ip_block *block;
1964 for (j = 0; j < adev->num_ip_blocks; j++) {
1965 block = &adev->ip_blocks[j];
1967 if (block->version->type != ip_order[i] ||
1968 !block->status.valid)
1971 r = block->version->funcs->hw_init(adev);
1972 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
1979 static int amdgpu_resume_phase1(struct amdgpu_device *adev)
1983 for (i = 0; i < adev->num_ip_blocks; i++) {
1984 if (!adev->ip_blocks[i].status.valid)
1986 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1987 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1988 adev->ip_blocks[i].version->type ==
1989 AMD_IP_BLOCK_TYPE_IH) {
1990 r = adev->ip_blocks[i].version->funcs->resume(adev);
1992 DRM_ERROR("resume of IP block <%s> failed %d\n",
1993 adev->ip_blocks[i].version->funcs->name, r);
2002 static int amdgpu_resume_phase2(struct amdgpu_device *adev)
2006 for (i = 0; i < adev->num_ip_blocks; i++) {
2007 if (!adev->ip_blocks[i].status.valid)
2009 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2010 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2011 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
2013 r = adev->ip_blocks[i].version->funcs->resume(adev);
2015 DRM_ERROR("resume of IP block <%s> failed %d\n",
2016 adev->ip_blocks[i].version->funcs->name, r);
2024 static int amdgpu_resume(struct amdgpu_device *adev)
2028 r = amdgpu_resume_phase1(adev);
2031 r = amdgpu_resume_phase2(adev);
2036 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
2038 if (amdgpu_sriov_vf(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;
2047 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
2048 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
2053 * amdgpu_device_init - initialize the driver
2055 * @adev: amdgpu_device pointer
2056 * @pdev: drm dev pointer
2057 * @pdev: pci dev pointer
2058 * @flags: driver flags
2060 * Initializes the driver info and hw (all asics).
2061 * Returns 0 for success or an error on failure.
2062 * Called at driver startup.
2064 int amdgpu_device_init(struct amdgpu_device *adev,
2065 struct drm_device *ddev,
2066 struct pci_dev *pdev,
2070 bool runtime = false;
2073 adev->shutdown = false;
2074 adev->dev = &pdev->dev;
2077 adev->flags = flags;
2078 adev->asic_type = flags & AMD_ASIC_MASK;
2079 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
2080 adev->mc.gart_size = 512 * 1024 * 1024;
2081 adev->accel_working = false;
2082 adev->num_rings = 0;
2083 adev->mman.buffer_funcs = NULL;
2084 adev->mman.buffer_funcs_ring = NULL;
2085 adev->vm_manager.vm_pte_funcs = NULL;
2086 adev->vm_manager.vm_pte_num_rings = 0;
2087 adev->gart.gart_funcs = NULL;
2088 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
2089 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
2091 adev->smc_rreg = &amdgpu_invalid_rreg;
2092 adev->smc_wreg = &amdgpu_invalid_wreg;
2093 adev->pcie_rreg = &amdgpu_invalid_rreg;
2094 adev->pcie_wreg = &amdgpu_invalid_wreg;
2095 adev->pciep_rreg = &amdgpu_invalid_rreg;
2096 adev->pciep_wreg = &amdgpu_invalid_wreg;
2097 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2098 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2099 adev->didt_rreg = &amdgpu_invalid_rreg;
2100 adev->didt_wreg = &amdgpu_invalid_wreg;
2101 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2102 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
2103 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2104 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2107 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2108 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2109 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
2111 /* mutex initialization are all done here so we
2112 * can recall function without having locking issues */
2113 atomic_set(&adev->irq.ih.lock, 0);
2114 mutex_init(&adev->firmware.mutex);
2115 mutex_init(&adev->pm.mutex);
2116 mutex_init(&adev->gfx.gpu_clock_mutex);
2117 mutex_init(&adev->srbm_mutex);
2118 mutex_init(&adev->gfx.pipe_reserve_mutex);
2119 mutex_init(&adev->grbm_idx_mutex);
2120 mutex_init(&adev->mn_lock);
2121 mutex_init(&adev->virt.vf_errors.lock);
2122 hash_init(adev->mn_hash);
2124 amdgpu_check_arguments(adev);
2126 spin_lock_init(&adev->mmio_idx_lock);
2127 spin_lock_init(&adev->smc_idx_lock);
2128 spin_lock_init(&adev->pcie_idx_lock);
2129 spin_lock_init(&adev->uvd_ctx_idx_lock);
2130 spin_lock_init(&adev->didt_idx_lock);
2131 spin_lock_init(&adev->gc_cac_idx_lock);
2132 spin_lock_init(&adev->se_cac_idx_lock);
2133 spin_lock_init(&adev->audio_endpt_idx_lock);
2134 spin_lock_init(&adev->mm_stats.lock);
2136 INIT_LIST_HEAD(&adev->shadow_list);
2137 mutex_init(&adev->shadow_list_lock);
2139 INIT_LIST_HEAD(&adev->gtt_list);
2140 spin_lock_init(&adev->gtt_list_lock);
2142 INIT_LIST_HEAD(&adev->ring_lru_list);
2143 spin_lock_init(&adev->ring_lru_list_lock);
2145 INIT_DELAYED_WORK(&adev->late_init_work, amdgpu_late_init_func_handler);
2147 /* Registers mapping */
2148 /* TODO: block userspace mapping of io register */
2149 if (adev->asic_type >= CHIP_BONAIRE) {
2150 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2151 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2153 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2154 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2157 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2158 if (adev->rmmio == NULL) {
2161 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2162 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2164 /* doorbell bar mapping */
2165 amdgpu_doorbell_init(adev);
2167 /* io port mapping */
2168 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2169 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2170 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2171 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2175 if (adev->rio_mem == NULL)
2176 DRM_INFO("PCI I/O BAR is not found.\n");
2178 /* early init functions */
2179 r = amdgpu_early_init(adev);
2183 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2184 /* this will fail for cards that aren't VGA class devices, just
2186 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
2188 if (amdgpu_runtime_pm == 1)
2190 if (amdgpu_device_is_px(ddev))
2192 if (!pci_is_thunderbolt_attached(adev->pdev))
2193 vga_switcheroo_register_client(adev->pdev,
2194 &amdgpu_switcheroo_ops, runtime);
2196 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2199 if (!amdgpu_get_bios(adev)) {
2204 r = amdgpu_atombios_init(adev);
2206 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
2207 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
2211 /* detect if we are with an SRIOV vbios */
2212 amdgpu_device_detect_sriov_bios(adev);
2214 /* Post card if necessary */
2215 if (amdgpu_need_post(adev)) {
2217 dev_err(adev->dev, "no vBIOS found\n");
2221 DRM_INFO("GPU posting now...\n");
2222 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2224 dev_err(adev->dev, "gpu post error!\n");
2228 DRM_INFO("GPU post is not needed\n");
2231 if (adev->is_atom_fw) {
2232 /* Initialize clocks */
2233 r = amdgpu_atomfirmware_get_clock_info(adev);
2235 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
2236 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2240 /* Initialize clocks */
2241 r = amdgpu_atombios_get_clock_info(adev);
2243 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
2244 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2247 /* init i2c buses */
2248 amdgpu_atombios_i2c_init(adev);
2252 r = amdgpu_fence_driver_init(adev);
2254 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
2255 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
2259 /* init the mode config */
2260 drm_mode_config_init(adev->ddev);
2262 r = amdgpu_init(adev);
2264 dev_err(adev->dev, "amdgpu_init failed\n");
2265 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
2270 adev->accel_working = true;
2272 amdgpu_vm_check_compute_bug(adev);
2274 /* Initialize the buffer migration limit. */
2275 if (amdgpu_moverate >= 0)
2276 max_MBps = amdgpu_moverate;
2278 max_MBps = 8; /* Allow 8 MB/s. */
2279 /* Get a log2 for easy divisions. */
2280 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2282 r = amdgpu_ib_pool_init(adev);
2284 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
2285 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
2289 r = amdgpu_ib_ring_tests(adev);
2291 DRM_ERROR("ib ring test failed (%d).\n", r);
2293 if (amdgpu_sriov_vf(adev))
2294 amdgpu_virt_init_data_exchange(adev);
2296 amdgpu_fbdev_init(adev);
2298 r = amdgpu_pm_sysfs_init(adev);
2300 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2302 r = amdgpu_gem_debugfs_init(adev);
2304 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
2306 r = amdgpu_debugfs_regs_init(adev);
2308 DRM_ERROR("registering register debugfs failed (%d).\n", r);
2310 r = amdgpu_debugfs_test_ib_ring_init(adev);
2312 DRM_ERROR("registering register test ib ring debugfs failed (%d).\n", r);
2314 r = amdgpu_debugfs_firmware_init(adev);
2316 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
2318 r = amdgpu_debugfs_vbios_dump_init(adev);
2320 DRM_ERROR("Creating vbios dump debugfs failed (%d).\n", r);
2322 if ((amdgpu_testing & 1)) {
2323 if (adev->accel_working)
2324 amdgpu_test_moves(adev);
2326 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2328 if (amdgpu_benchmarking) {
2329 if (adev->accel_working)
2330 amdgpu_benchmark(adev, amdgpu_benchmarking);
2332 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2335 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2336 * explicit gating rather than handling it automatically.
2338 r = amdgpu_late_init(adev);
2340 dev_err(adev->dev, "amdgpu_late_init failed\n");
2341 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
2348 amdgpu_vf_error_trans_all(adev);
2350 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2355 * amdgpu_device_fini - tear down the driver
2357 * @adev: amdgpu_device pointer
2359 * Tear down the driver info (all asics).
2360 * Called at driver shutdown.
2362 void amdgpu_device_fini(struct amdgpu_device *adev)
2366 DRM_INFO("amdgpu: finishing device.\n");
2367 adev->shutdown = true;
2368 if (adev->mode_info.mode_config_initialized)
2369 drm_crtc_force_disable_all(adev->ddev);
2370 /* evict vram memory */
2371 amdgpu_bo_evict_vram(adev);
2372 amdgpu_ib_pool_fini(adev);
2373 amdgpu_fw_reserve_vram_fini(adev);
2374 amdgpu_fence_driver_fini(adev);
2375 amdgpu_fbdev_fini(adev);
2376 r = amdgpu_fini(adev);
2377 if (adev->firmware.gpu_info_fw) {
2378 release_firmware(adev->firmware.gpu_info_fw);
2379 adev->firmware.gpu_info_fw = NULL;
2381 adev->accel_working = false;
2382 cancel_delayed_work_sync(&adev->late_init_work);
2383 /* free i2c buses */
2384 amdgpu_i2c_fini(adev);
2385 amdgpu_atombios_fini(adev);
2388 if (!pci_is_thunderbolt_attached(adev->pdev))
2389 vga_switcheroo_unregister_client(adev->pdev);
2390 if (adev->flags & AMD_IS_PX)
2391 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2392 vga_client_register(adev->pdev, NULL, NULL, NULL);
2394 pci_iounmap(adev->pdev, adev->rio_mem);
2395 adev->rio_mem = NULL;
2396 iounmap(adev->rmmio);
2398 amdgpu_doorbell_fini(adev);
2399 amdgpu_pm_sysfs_fini(adev);
2400 amdgpu_debugfs_regs_cleanup(adev);
2408 * amdgpu_device_suspend - initiate device suspend
2410 * @pdev: drm dev pointer
2411 * @state: suspend state
2413 * Puts the hw in the suspend state (all asics).
2414 * Returns 0 for success or an error on failure.
2415 * Called at driver suspend.
2417 int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
2419 struct amdgpu_device *adev;
2420 struct drm_crtc *crtc;
2421 struct drm_connector *connector;
2424 if (dev == NULL || dev->dev_private == NULL) {
2428 adev = dev->dev_private;
2430 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2433 drm_kms_helper_poll_disable(dev);
2435 /* turn off display hw */
2436 drm_modeset_lock_all(dev);
2437 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2438 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2440 drm_modeset_unlock_all(dev);
2442 amdgpu_amdkfd_suspend(adev);
2444 /* unpin the front buffers and cursors */
2445 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2446 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2447 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2448 struct amdgpu_bo *robj;
2450 if (amdgpu_crtc->cursor_bo) {
2451 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2452 r = amdgpu_bo_reserve(aobj, true);
2454 amdgpu_bo_unpin(aobj);
2455 amdgpu_bo_unreserve(aobj);
2459 if (rfb == NULL || rfb->obj == NULL) {
2462 robj = gem_to_amdgpu_bo(rfb->obj);
2463 /* don't unpin kernel fb objects */
2464 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
2465 r = amdgpu_bo_reserve(robj, true);
2467 amdgpu_bo_unpin(robj);
2468 amdgpu_bo_unreserve(robj);
2472 /* evict vram memory */
2473 amdgpu_bo_evict_vram(adev);
2475 amdgpu_fence_driver_suspend(adev);
2477 r = amdgpu_suspend(adev);
2479 /* evict remaining vram memory
2480 * This second call to evict vram is to evict the gart page table
2483 amdgpu_bo_evict_vram(adev);
2485 amdgpu_atombios_scratch_regs_save(adev);
2486 pci_save_state(dev->pdev);
2488 /* Shut down the device */
2489 pci_disable_device(dev->pdev);
2490 pci_set_power_state(dev->pdev, PCI_D3hot);
2492 r = amdgpu_asic_reset(adev);
2494 DRM_ERROR("amdgpu asic reset failed\n");
2499 amdgpu_fbdev_set_suspend(adev, 1);
2506 * amdgpu_device_resume - initiate device resume
2508 * @pdev: drm dev pointer
2510 * Bring the hw back to operating state (all asics).
2511 * Returns 0 for success or an error on failure.
2512 * Called at driver resume.
2514 int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
2516 struct drm_connector *connector;
2517 struct amdgpu_device *adev = dev->dev_private;
2518 struct drm_crtc *crtc;
2521 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2528 pci_set_power_state(dev->pdev, PCI_D0);
2529 pci_restore_state(dev->pdev);
2530 r = pci_enable_device(dev->pdev);
2534 amdgpu_atombios_scratch_regs_restore(adev);
2537 if (amdgpu_need_post(adev)) {
2538 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2540 DRM_ERROR("amdgpu asic init failed\n");
2543 r = amdgpu_resume(adev);
2545 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
2548 amdgpu_fence_driver_resume(adev);
2551 r = amdgpu_ib_ring_tests(adev);
2553 DRM_ERROR("ib ring test failed (%d).\n", r);
2556 r = amdgpu_late_init(adev);
2561 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2562 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2564 if (amdgpu_crtc->cursor_bo) {
2565 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2566 r = amdgpu_bo_reserve(aobj, true);
2568 r = amdgpu_bo_pin(aobj,
2569 AMDGPU_GEM_DOMAIN_VRAM,
2570 &amdgpu_crtc->cursor_addr);
2572 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2573 amdgpu_bo_unreserve(aobj);
2577 r = amdgpu_amdkfd_resume(adev);
2581 /* blat the mode back in */
2583 drm_helper_resume_force_mode(dev);
2584 /* turn on display hw */
2585 drm_modeset_lock_all(dev);
2586 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2587 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2589 drm_modeset_unlock_all(dev);
2592 drm_kms_helper_poll_enable(dev);
2595 * Most of the connector probing functions try to acquire runtime pm
2596 * refs to ensure that the GPU is powered on when connector polling is
2597 * performed. Since we're calling this from a runtime PM callback,
2598 * trying to acquire rpm refs will cause us to deadlock.
2600 * Since we're guaranteed to be holding the rpm lock, it's safe to
2601 * temporarily disable the rpm helpers so this doesn't deadlock us.
2604 dev->dev->power.disable_depth++;
2606 drm_helper_hpd_irq_event(dev);
2608 dev->dev->power.disable_depth--;
2612 amdgpu_fbdev_set_suspend(adev, 0);
2621 static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2624 bool asic_hang = false;
2626 if (amdgpu_sriov_vf(adev))
2629 for (i = 0; i < adev->num_ip_blocks; i++) {
2630 if (!adev->ip_blocks[i].status.valid)
2632 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2633 adev->ip_blocks[i].status.hang =
2634 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2635 if (adev->ip_blocks[i].status.hang) {
2636 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
2643 static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
2647 for (i = 0; i < adev->num_ip_blocks; i++) {
2648 if (!adev->ip_blocks[i].status.valid)
2650 if (adev->ip_blocks[i].status.hang &&
2651 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2652 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
2661 static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2665 for (i = 0; i < adev->num_ip_blocks; i++) {
2666 if (!adev->ip_blocks[i].status.valid)
2668 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2669 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2670 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2671 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
2672 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
2673 if (adev->ip_blocks[i].status.hang) {
2674 DRM_INFO("Some block need full reset!\n");
2682 static int amdgpu_soft_reset(struct amdgpu_device *adev)
2686 for (i = 0; i < adev->num_ip_blocks; i++) {
2687 if (!adev->ip_blocks[i].status.valid)
2689 if (adev->ip_blocks[i].status.hang &&
2690 adev->ip_blocks[i].version->funcs->soft_reset) {
2691 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
2700 static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2704 for (i = 0; i < adev->num_ip_blocks; i++) {
2705 if (!adev->ip_blocks[i].status.valid)
2707 if (adev->ip_blocks[i].status.hang &&
2708 adev->ip_blocks[i].version->funcs->post_soft_reset)
2709 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
2717 bool amdgpu_need_backup(struct amdgpu_device *adev)
2719 if (adev->flags & AMD_IS_APU)
2722 return amdgpu_lockup_timeout > 0 ? true : false;
2725 static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2726 struct amdgpu_ring *ring,
2727 struct amdgpu_bo *bo,
2728 struct dma_fence **fence)
2736 r = amdgpu_bo_reserve(bo, true);
2739 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2740 /* if bo has been evicted, then no need to recover */
2741 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
2742 r = amdgpu_bo_validate(bo->shadow);
2744 DRM_ERROR("bo validate failed!\n");
2748 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
2751 DRM_ERROR("recover page table failed!\n");
2756 amdgpu_bo_unreserve(bo);
2761 * amdgpu_sriov_gpu_reset - reset the asic
2763 * @adev: amdgpu device pointer
2764 * @job: which job trigger hang
2766 * Attempt the reset the GPU if it has hung (all asics).
2768 * Returns 0 for success or an error on failure.
2770 int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, struct amdgpu_job *job)
2774 struct amdgpu_bo *bo, *tmp;
2775 struct amdgpu_ring *ring;
2776 struct dma_fence *fence = NULL, *next = NULL;
2778 mutex_lock(&adev->virt.lock_reset);
2779 atomic_inc(&adev->gpu_reset_counter);
2780 adev->in_sriov_reset = true;
2783 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2785 /* we start from the ring trigger GPU hang */
2786 j = job ? job->ring->idx : 0;
2788 /* block scheduler */
2789 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2790 ring = adev->rings[i % AMDGPU_MAX_RINGS];
2791 if (!ring || !ring->sched.thread)
2794 kthread_park(ring->sched.thread);
2799 /* here give the last chance to check if job removed from mirror-list
2800 * since we already pay some time on kthread_park */
2801 if (job && list_empty(&job->base.node)) {
2802 kthread_unpark(ring->sched.thread);
2806 if (amd_sched_invalidate_job(&job->base, amdgpu_job_hang_limit))
2807 amd_sched_job_kickout(&job->base);
2809 /* only do job_reset on the hang ring if @job not NULL */
2810 amd_sched_hw_job_reset(&ring->sched);
2812 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2813 amdgpu_fence_driver_force_completion_ring(ring);
2816 /* request to take full control of GPU before re-initialization */
2818 amdgpu_virt_reset_gpu(adev);
2820 amdgpu_virt_request_full_gpu(adev, true);
2823 /* Resume IP prior to SMC */
2824 amdgpu_sriov_reinit_early(adev);
2826 /* we need recover gart prior to run SMC/CP/SDMA resume */
2827 amdgpu_ttm_recover_gart(adev);
2829 /* now we are okay to resume SMC/CP/SDMA */
2830 amdgpu_sriov_reinit_late(adev);
2832 amdgpu_irq_gpu_reset_resume_helper(adev);
2834 if (amdgpu_ib_ring_tests(adev))
2835 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2837 /* release full control of GPU after ib test */
2838 amdgpu_virt_release_full_gpu(adev, true);
2840 DRM_INFO("recover vram bo from shadow\n");
2842 ring = adev->mman.buffer_funcs_ring;
2843 mutex_lock(&adev->shadow_list_lock);
2844 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2846 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2848 r = dma_fence_wait(fence, false);
2850 WARN(r, "recovery from shadow isn't completed\n");
2855 dma_fence_put(fence);
2858 mutex_unlock(&adev->shadow_list_lock);
2861 r = dma_fence_wait(fence, false);
2863 WARN(r, "recovery from shadow isn't completed\n");
2865 dma_fence_put(fence);
2867 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2868 ring = adev->rings[i % AMDGPU_MAX_RINGS];
2869 if (!ring || !ring->sched.thread)
2872 if (job && j != i) {
2873 kthread_unpark(ring->sched.thread);
2877 amd_sched_job_recovery(&ring->sched);
2878 kthread_unpark(ring->sched.thread);
2881 drm_helper_resume_force_mode(adev->ddev);
2883 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2885 /* bad news, how to tell it to userspace ? */
2886 dev_info(adev->dev, "GPU reset failed\n");
2888 dev_info(adev->dev, "GPU reset successed!\n");
2891 adev->in_sriov_reset = false;
2892 mutex_unlock(&adev->virt.lock_reset);
2897 * amdgpu_gpu_reset - reset the asic
2899 * @adev: amdgpu device pointer
2901 * Attempt the reset the GPU if it has hung (all asics).
2902 * Returns 0 for success or an error on failure.
2904 int amdgpu_gpu_reset(struct amdgpu_device *adev)
2908 bool need_full_reset, vram_lost = false;
2910 if (!amdgpu_check_soft_reset(adev)) {
2911 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2915 atomic_inc(&adev->gpu_reset_counter);
2918 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2920 /* block scheduler */
2921 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2922 struct amdgpu_ring *ring = adev->rings[i];
2924 if (!ring || !ring->sched.thread)
2926 kthread_park(ring->sched.thread);
2927 amd_sched_hw_job_reset(&ring->sched);
2929 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2930 amdgpu_fence_driver_force_completion(adev);
2932 need_full_reset = amdgpu_need_full_reset(adev);
2934 if (!need_full_reset) {
2935 amdgpu_pre_soft_reset(adev);
2936 r = amdgpu_soft_reset(adev);
2937 amdgpu_post_soft_reset(adev);
2938 if (r || amdgpu_check_soft_reset(adev)) {
2939 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2940 need_full_reset = true;
2944 if (need_full_reset) {
2945 r = amdgpu_suspend(adev);
2948 amdgpu_atombios_scratch_regs_save(adev);
2949 r = amdgpu_asic_reset(adev);
2950 amdgpu_atombios_scratch_regs_restore(adev);
2952 amdgpu_atom_asic_init(adev->mode_info.atom_context);
2955 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
2956 r = amdgpu_resume_phase1(adev);
2959 vram_lost = amdgpu_check_vram_lost(adev);
2961 DRM_ERROR("VRAM is lost!\n");
2962 atomic_inc(&adev->vram_lost_counter);
2964 r = amdgpu_ttm_recover_gart(adev);
2967 r = amdgpu_resume_phase2(adev);
2971 amdgpu_fill_reset_magic(adev);
2976 amdgpu_irq_gpu_reset_resume_helper(adev);
2977 r = amdgpu_ib_ring_tests(adev);
2979 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
2980 r = amdgpu_suspend(adev);
2981 need_full_reset = true;
2985 * recovery vm page tables, since we cannot depend on VRAM is
2986 * consistent after gpu full reset.
2988 if (need_full_reset && amdgpu_need_backup(adev)) {
2989 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2990 struct amdgpu_bo *bo, *tmp;
2991 struct dma_fence *fence = NULL, *next = NULL;
2993 DRM_INFO("recover vram bo from shadow\n");
2994 mutex_lock(&adev->shadow_list_lock);
2995 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2997 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2999 r = dma_fence_wait(fence, false);
3001 WARN(r, "recovery from shadow isn't completed\n");
3006 dma_fence_put(fence);
3009 mutex_unlock(&adev->shadow_list_lock);
3011 r = dma_fence_wait(fence, false);
3013 WARN(r, "recovery from shadow isn't completed\n");
3015 dma_fence_put(fence);
3017 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3018 struct amdgpu_ring *ring = adev->rings[i];
3020 if (!ring || !ring->sched.thread)
3023 amd_sched_job_recovery(&ring->sched);
3024 kthread_unpark(ring->sched.thread);
3027 dev_err(adev->dev, "asic resume failed (%d).\n", r);
3028 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3029 if (adev->rings[i] && adev->rings[i]->sched.thread) {
3030 kthread_unpark(adev->rings[i]->sched.thread);
3035 drm_helper_resume_force_mode(adev->ddev);
3037 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
3039 /* bad news, how to tell it to userspace ? */
3040 dev_info(adev->dev, "GPU reset failed\n");
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 & GENMASK_ULL(33, 24)) >> 24;
3195 sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34;
3196 instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44;
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 & GENMASK_ULL(33, 24)) >> 24;
3271 sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34;
3272 instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44;
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 & GENMASK_ULL(6, 0));
3621 se = (*pos & GENMASK_ULL(14, 7)) >> 7;
3622 sh = (*pos & GENMASK_ULL(22, 15)) >> 15;
3623 cu = (*pos & GENMASK_ULL(30, 23)) >> 23;
3624 wave = (*pos & GENMASK_ULL(36, 31)) >> 31;
3625 simd = (*pos & GENMASK_ULL(44, 37)) >> 37;
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 & GENMASK_ULL(11, 0);
3671 se = (*pos & GENMASK_ULL(19, 12)) >> 12;
3672 sh = (*pos & GENMASK_ULL(27, 20)) >> 20;
3673 cu = (*pos & GENMASK_ULL(35, 28)) >> 28;
3674 wave = (*pos & GENMASK_ULL(43, 36)) >> 36;
3675 simd = (*pos & GENMASK_ULL(51, 44)) >> 44;
3676 thread = (*pos & GENMASK_ULL(59, 52)) >> 52;
3677 bank = (*pos & GENMASK_ULL(61, 60)) >> 60;
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) { }