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>
57 MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
59 static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
60 static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
62 static const char *amdgpu_asic_name[] = {
85 bool amdgpu_device_is_px(struct drm_device *dev)
87 struct amdgpu_device *adev = dev->dev_private;
89 if (adev->flags & AMD_IS_PX)
95 * MMIO register access helper functions.
97 uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
102 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
103 BUG_ON(in_interrupt());
104 return amdgpu_virt_kiq_rreg(adev, reg);
107 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
108 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
112 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
113 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
114 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
115 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
117 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
121 void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
124 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
126 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
127 BUG_ON(in_interrupt());
128 return amdgpu_virt_kiq_wreg(adev, reg, v);
131 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
132 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
136 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
137 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
138 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
139 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
143 u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
145 if ((reg * 4) < adev->rio_mem_size)
146 return ioread32(adev->rio_mem + (reg * 4));
148 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
149 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
153 void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
156 if ((reg * 4) < adev->rio_mem_size)
157 iowrite32(v, adev->rio_mem + (reg * 4));
159 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
160 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
165 * amdgpu_mm_rdoorbell - read a doorbell dword
167 * @adev: amdgpu_device pointer
168 * @index: doorbell index
170 * Returns the value in the doorbell aperture at the
171 * requested doorbell index (CIK).
173 u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
175 if (index < adev->doorbell.num_doorbells) {
176 return readl(adev->doorbell.ptr + index);
178 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
184 * amdgpu_mm_wdoorbell - write a doorbell dword
186 * @adev: amdgpu_device pointer
187 * @index: doorbell index
190 * Writes @v to the doorbell aperture at the
191 * requested doorbell index (CIK).
193 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
195 if (index < adev->doorbell.num_doorbells) {
196 writel(v, adev->doorbell.ptr + index);
198 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
203 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
205 * @adev: amdgpu_device pointer
206 * @index: doorbell index
208 * Returns the value in the doorbell aperture at the
209 * requested doorbell index (VEGA10+).
211 u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
213 if (index < adev->doorbell.num_doorbells) {
214 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
216 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
222 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
224 * @adev: amdgpu_device pointer
225 * @index: doorbell index
228 * Writes @v to the doorbell aperture at the
229 * requested doorbell index (VEGA10+).
231 void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
233 if (index < adev->doorbell.num_doorbells) {
234 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
236 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
241 * amdgpu_invalid_rreg - dummy reg read function
243 * @adev: amdgpu device pointer
244 * @reg: offset of register
246 * Dummy register read function. Used for register blocks
247 * that certain asics don't have (all asics).
248 * Returns the value in the register.
250 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
252 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
258 * amdgpu_invalid_wreg - dummy reg write function
260 * @adev: amdgpu device pointer
261 * @reg: offset of register
262 * @v: value to write to the register
264 * Dummy register read function. Used for register blocks
265 * that certain asics don't have (all asics).
267 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
269 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
275 * amdgpu_block_invalid_rreg - dummy reg read function
277 * @adev: amdgpu device pointer
278 * @block: offset of instance
279 * @reg: offset of register
281 * Dummy register read function. Used for register blocks
282 * that certain asics don't have (all asics).
283 * Returns the value in the register.
285 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
286 uint32_t block, uint32_t reg)
288 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
295 * amdgpu_block_invalid_wreg - dummy reg write function
297 * @adev: amdgpu device pointer
298 * @block: offset of instance
299 * @reg: offset of register
300 * @v: value to write to the register
302 * Dummy register read function. Used for register blocks
303 * that certain asics don't have (all asics).
305 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
307 uint32_t reg, uint32_t v)
309 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
314 static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
318 if (adev->vram_scratch.robj == NULL) {
319 r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE,
320 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
321 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
322 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
323 NULL, NULL, &adev->vram_scratch.robj);
329 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
330 if (unlikely(r != 0))
332 r = amdgpu_bo_pin(adev->vram_scratch.robj,
333 AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
335 amdgpu_bo_unreserve(adev->vram_scratch.robj);
338 r = amdgpu_bo_kmap(adev->vram_scratch.robj,
339 (void **)&adev->vram_scratch.ptr);
341 amdgpu_bo_unpin(adev->vram_scratch.robj);
342 amdgpu_bo_unreserve(adev->vram_scratch.robj);
347 static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
351 if (adev->vram_scratch.robj == NULL) {
354 r = amdgpu_bo_reserve(adev->vram_scratch.robj, true);
355 if (likely(r == 0)) {
356 amdgpu_bo_kunmap(adev->vram_scratch.robj);
357 amdgpu_bo_unpin(adev->vram_scratch.robj);
358 amdgpu_bo_unreserve(adev->vram_scratch.robj);
360 amdgpu_bo_unref(&adev->vram_scratch.robj);
364 * amdgpu_program_register_sequence - program an array of registers.
366 * @adev: amdgpu_device pointer
367 * @registers: pointer to the register array
368 * @array_size: size of the register array
370 * Programs an array or registers with and and or masks.
371 * This is a helper for setting golden registers.
373 void amdgpu_program_register_sequence(struct amdgpu_device *adev,
374 const u32 *registers,
375 const u32 array_size)
377 u32 tmp, reg, and_mask, or_mask;
383 for (i = 0; i < array_size; i +=3) {
384 reg = registers[i + 0];
385 and_mask = registers[i + 1];
386 or_mask = registers[i + 2];
388 if (and_mask == 0xffffffff) {
399 void amdgpu_pci_config_reset(struct amdgpu_device *adev)
401 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
405 * GPU doorbell aperture helpers function.
408 * amdgpu_doorbell_init - Init doorbell driver information.
410 * @adev: amdgpu_device pointer
412 * Init doorbell driver information (CIK)
413 * Returns 0 on success, error on failure.
415 static int amdgpu_doorbell_init(struct amdgpu_device *adev)
417 /* doorbell bar mapping */
418 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
419 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
421 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
422 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
423 if (adev->doorbell.num_doorbells == 0)
426 adev->doorbell.ptr = ioremap(adev->doorbell.base,
427 adev->doorbell.num_doorbells *
429 if (adev->doorbell.ptr == NULL)
436 * amdgpu_doorbell_fini - Tear down doorbell driver information.
438 * @adev: amdgpu_device pointer
440 * Tear down doorbell driver information (CIK)
442 static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
444 iounmap(adev->doorbell.ptr);
445 adev->doorbell.ptr = NULL;
449 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
452 * @adev: amdgpu_device pointer
453 * @aperture_base: output returning doorbell aperture base physical address
454 * @aperture_size: output returning doorbell aperture size in bytes
455 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
457 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
458 * takes doorbells required for its own rings and reports the setup to amdkfd.
459 * amdgpu reserved doorbells are at the start of the doorbell aperture.
461 void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
462 phys_addr_t *aperture_base,
463 size_t *aperture_size,
464 size_t *start_offset)
467 * The first num_doorbells are used by amdgpu.
468 * amdkfd takes whatever's left in the aperture.
470 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
471 *aperture_base = adev->doorbell.base;
472 *aperture_size = adev->doorbell.size;
473 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
483 * Writeback is the the method by which the the GPU updates special pages
484 * in memory with the status of certain GPU events (fences, ring pointers,
489 * amdgpu_wb_fini - Disable Writeback and free memory
491 * @adev: amdgpu_device pointer
493 * Disables Writeback and frees the Writeback memory (all asics).
494 * Used at driver shutdown.
496 static void amdgpu_wb_fini(struct amdgpu_device *adev)
498 if (adev->wb.wb_obj) {
499 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
501 (void **)&adev->wb.wb);
502 adev->wb.wb_obj = NULL;
507 * amdgpu_wb_init- Init Writeback driver info and allocate memory
509 * @adev: amdgpu_device pointer
511 * Disables Writeback and frees the Writeback memory (all asics).
512 * Used at driver startup.
513 * Returns 0 on success or an -error on failure.
515 static int amdgpu_wb_init(struct amdgpu_device *adev)
519 if (adev->wb.wb_obj == NULL) {
520 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t),
521 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
522 &adev->wb.wb_obj, &adev->wb.gpu_addr,
523 (void **)&adev->wb.wb);
525 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
529 adev->wb.num_wb = AMDGPU_MAX_WB;
530 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
532 /* clear wb memory */
533 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
540 * amdgpu_wb_get - Allocate a wb entry
542 * @adev: amdgpu_device pointer
545 * Allocate a wb slot for use by the driver (all asics).
546 * Returns 0 on success or -EINVAL on failure.
548 int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
550 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
551 if (offset < adev->wb.num_wb) {
552 __set_bit(offset, adev->wb.used);
561 * amdgpu_wb_get_64bit - Allocate a wb entry
563 * @adev: amdgpu_device pointer
566 * Allocate a wb slot for use by the driver (all asics).
567 * Returns 0 on success or -EINVAL on failure.
569 int amdgpu_wb_get_64bit(struct amdgpu_device *adev, u32 *wb)
571 unsigned long offset = bitmap_find_next_zero_area_off(adev->wb.used,
572 adev->wb.num_wb, 0, 2, 7, 0);
573 if ((offset + 1) < adev->wb.num_wb) {
574 __set_bit(offset, adev->wb.used);
575 __set_bit(offset + 1, adev->wb.used);
584 * amdgpu_wb_free - Free a wb entry
586 * @adev: amdgpu_device pointer
589 * Free a wb slot allocated for use by the driver (all asics)
591 void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
593 if (wb < adev->wb.num_wb)
594 __clear_bit(wb, adev->wb.used);
598 * amdgpu_wb_free_64bit - Free a wb entry
600 * @adev: amdgpu_device pointer
603 * Free a wb slot allocated for use by the driver (all asics)
605 void amdgpu_wb_free_64bit(struct amdgpu_device *adev, u32 wb)
607 if ((wb + 1) < adev->wb.num_wb) {
608 __clear_bit(wb, adev->wb.used);
609 __clear_bit(wb + 1, adev->wb.used);
614 * amdgpu_vram_location - try to find VRAM location
615 * @adev: amdgpu device structure holding all necessary informations
616 * @mc: memory controller structure holding memory informations
617 * @base: base address at which to put VRAM
619 * Function will place try to place VRAM at base address provided
620 * as parameter (which is so far either PCI aperture address or
621 * for IGP TOM base address).
623 * If there is not enough space to fit the unvisible VRAM in the 32bits
624 * address space then we limit the VRAM size to the aperture.
626 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
627 * this shouldn't be a problem as we are using the PCI aperture as a reference.
628 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
631 * Note: we use mc_vram_size as on some board we need to program the mc to
632 * cover the whole aperture even if VRAM size is inferior to aperture size
633 * Novell bug 204882 + along with lots of ubuntu ones
635 * Note: when limiting vram it's safe to overwritte real_vram_size because
636 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
637 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
640 * Note: IGP TOM addr should be the same as the aperture addr, we don't
641 * explicitly check for that thought.
643 * FIXME: when reducing VRAM size align new size on power of 2.
645 void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
647 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
649 mc->vram_start = base;
650 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
651 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
652 mc->real_vram_size = mc->aper_size;
653 mc->mc_vram_size = mc->aper_size;
655 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
656 if (limit && limit < mc->real_vram_size)
657 mc->real_vram_size = limit;
658 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
659 mc->mc_vram_size >> 20, mc->vram_start,
660 mc->vram_end, mc->real_vram_size >> 20);
664 * amdgpu_gtt_location - try to find GTT location
665 * @adev: amdgpu device structure holding all necessary informations
666 * @mc: memory controller structure holding memory informations
668 * Function will place try to place GTT before or after VRAM.
670 * If GTT size is bigger than space left then we ajust GTT size.
671 * Thus function will never fails.
673 * FIXME: when reducing GTT size align new size on power of 2.
675 void amdgpu_gtt_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
677 u64 size_af, size_bf;
679 size_af = ((adev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
680 size_bf = mc->vram_start & ~mc->gtt_base_align;
681 if (size_bf > size_af) {
682 if (mc->gtt_size > size_bf) {
683 dev_warn(adev->dev, "limiting GTT\n");
684 mc->gtt_size = size_bf;
688 if (mc->gtt_size > size_af) {
689 dev_warn(adev->dev, "limiting GTT\n");
690 mc->gtt_size = size_af;
692 mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
694 mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
695 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
696 mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
700 * GPU helpers function.
703 * amdgpu_need_post - check if the hw need post or not
705 * @adev: amdgpu_device pointer
707 * Check if the asic has been initialized (all asics) at driver startup
708 * or post is needed if hw reset is performed.
709 * Returns true if need or false if not.
711 bool amdgpu_need_post(struct amdgpu_device *adev)
715 if (adev->has_hw_reset) {
716 adev->has_hw_reset = false;
719 /* then check MEM_SIZE, in case the crtcs are off */
720 reg = amdgpu_asic_get_config_memsize(adev);
722 if ((reg != 0) && (reg != 0xffffffff))
729 static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
731 if (amdgpu_sriov_vf(adev))
734 if (amdgpu_passthrough(adev)) {
735 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
736 * some old smc fw still need driver do vPost otherwise gpu hang, while
737 * those smc fw version above 22.15 doesn't have this flaw, so we force
738 * vpost executed for smc version below 22.15
740 if (adev->asic_type == CHIP_FIJI) {
743 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
744 /* force vPost if error occured */
748 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
749 if (fw_ver < 0x00160e00)
753 return amdgpu_need_post(adev);
757 * amdgpu_dummy_page_init - init dummy page used by the driver
759 * @adev: amdgpu_device pointer
761 * Allocate the dummy page used by the driver (all asics).
762 * This dummy page is used by the driver as a filler for gart entries
763 * when pages are taken out of the GART
764 * Returns 0 on sucess, -ENOMEM on failure.
766 int amdgpu_dummy_page_init(struct amdgpu_device *adev)
768 if (adev->dummy_page.page)
770 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
771 if (adev->dummy_page.page == NULL)
773 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
774 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
775 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
776 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
777 __free_page(adev->dummy_page.page);
778 adev->dummy_page.page = NULL;
785 * amdgpu_dummy_page_fini - free dummy page used by the driver
787 * @adev: amdgpu_device pointer
789 * Frees the dummy page used by the driver (all asics).
791 void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
793 if (adev->dummy_page.page == NULL)
795 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
796 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
797 __free_page(adev->dummy_page.page);
798 adev->dummy_page.page = NULL;
802 /* ATOM accessor methods */
804 * ATOM is an interpreted byte code stored in tables in the vbios. The
805 * driver registers callbacks to access registers and the interpreter
806 * in the driver parses the tables and executes then to program specific
807 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
808 * atombios.h, and atom.c
812 * cail_pll_read - read PLL register
814 * @info: atom card_info pointer
815 * @reg: PLL register offset
817 * Provides a PLL register accessor for the atom interpreter (r4xx+).
818 * Returns the value of the PLL register.
820 static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
826 * cail_pll_write - write PLL register
828 * @info: atom card_info pointer
829 * @reg: PLL register offset
830 * @val: value to write to the pll register
832 * Provides a PLL register accessor for the atom interpreter (r4xx+).
834 static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
840 * cail_mc_read - read MC (Memory Controller) register
842 * @info: atom card_info pointer
843 * @reg: MC register offset
845 * Provides an MC register accessor for the atom interpreter (r4xx+).
846 * Returns the value of the MC register.
848 static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
854 * cail_mc_write - write MC (Memory Controller) register
856 * @info: atom card_info pointer
857 * @reg: MC register offset
858 * @val: value to write to the pll register
860 * Provides a MC register accessor for the atom interpreter (r4xx+).
862 static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
868 * cail_reg_write - write MMIO register
870 * @info: atom card_info pointer
871 * @reg: MMIO register offset
872 * @val: value to write to the pll register
874 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
876 static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
878 struct amdgpu_device *adev = info->dev->dev_private;
884 * cail_reg_read - read MMIO register
886 * @info: atom card_info pointer
887 * @reg: MMIO register offset
889 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
890 * Returns the value of the MMIO register.
892 static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
894 struct amdgpu_device *adev = info->dev->dev_private;
902 * cail_ioreg_write - write IO register
904 * @info: atom card_info pointer
905 * @reg: IO register offset
906 * @val: value to write to the pll register
908 * Provides a IO register accessor for the atom interpreter (r4xx+).
910 static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
912 struct amdgpu_device *adev = info->dev->dev_private;
918 * cail_ioreg_read - read IO register
920 * @info: atom card_info pointer
921 * @reg: IO register offset
923 * Provides an IO register accessor for the atom interpreter (r4xx+).
924 * Returns the value of the IO register.
926 static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
928 struct amdgpu_device *adev = info->dev->dev_private;
936 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
938 * @adev: amdgpu_device pointer
940 * Frees the driver info and register access callbacks for the ATOM
941 * interpreter (r4xx+).
942 * Called at driver shutdown.
944 static void amdgpu_atombios_fini(struct amdgpu_device *adev)
946 if (adev->mode_info.atom_context) {
947 kfree(adev->mode_info.atom_context->scratch);
948 kfree(adev->mode_info.atom_context->iio);
950 kfree(adev->mode_info.atom_context);
951 adev->mode_info.atom_context = NULL;
952 kfree(adev->mode_info.atom_card_info);
953 adev->mode_info.atom_card_info = NULL;
957 * amdgpu_atombios_init - init the driver info and callbacks for atombios
959 * @adev: amdgpu_device pointer
961 * Initializes the driver info and register access callbacks for the
962 * ATOM interpreter (r4xx+).
963 * Returns 0 on sucess, -ENOMEM on failure.
964 * Called at driver startup.
966 static int amdgpu_atombios_init(struct amdgpu_device *adev)
968 struct card_info *atom_card_info =
969 kzalloc(sizeof(struct card_info), GFP_KERNEL);
974 adev->mode_info.atom_card_info = atom_card_info;
975 atom_card_info->dev = adev->ddev;
976 atom_card_info->reg_read = cail_reg_read;
977 atom_card_info->reg_write = cail_reg_write;
978 /* needed for iio ops */
980 atom_card_info->ioreg_read = cail_ioreg_read;
981 atom_card_info->ioreg_write = cail_ioreg_write;
983 DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
984 atom_card_info->ioreg_read = cail_reg_read;
985 atom_card_info->ioreg_write = cail_reg_write;
987 atom_card_info->mc_read = cail_mc_read;
988 atom_card_info->mc_write = cail_mc_write;
989 atom_card_info->pll_read = cail_pll_read;
990 atom_card_info->pll_write = cail_pll_write;
992 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
993 if (!adev->mode_info.atom_context) {
994 amdgpu_atombios_fini(adev);
998 mutex_init(&adev->mode_info.atom_context->mutex);
999 if (adev->is_atom_fw) {
1000 amdgpu_atomfirmware_scratch_regs_init(adev);
1001 amdgpu_atomfirmware_allocate_fb_scratch(adev);
1003 amdgpu_atombios_scratch_regs_init(adev);
1004 amdgpu_atombios_allocate_fb_scratch(adev);
1009 /* if we get transitioned to only one device, take VGA back */
1011 * amdgpu_vga_set_decode - enable/disable vga decode
1013 * @cookie: amdgpu_device pointer
1014 * @state: enable/disable vga decode
1016 * Enable/disable vga decode (all asics).
1017 * Returns VGA resource flags.
1019 static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
1021 struct amdgpu_device *adev = cookie;
1022 amdgpu_asic_set_vga_state(adev, state);
1024 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1025 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1027 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1031 * amdgpu_check_pot_argument - check that argument is a power of two
1033 * @arg: value to check
1035 * Validates that a certain argument is a power of two (all asics).
1036 * Returns true if argument is valid.
1038 static bool amdgpu_check_pot_argument(int arg)
1040 return (arg & (arg - 1)) == 0;
1043 static void amdgpu_check_block_size(struct amdgpu_device *adev)
1045 /* defines number of bits in page table versus page directory,
1046 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1047 * page table and the remaining bits are in the page directory */
1048 if (amdgpu_vm_block_size == -1)
1051 if (amdgpu_vm_block_size < 9) {
1052 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1053 amdgpu_vm_block_size);
1057 if (amdgpu_vm_block_size > 24 ||
1058 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1059 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1060 amdgpu_vm_block_size);
1067 amdgpu_vm_block_size = -1;
1070 static void amdgpu_check_vm_size(struct amdgpu_device *adev)
1072 if (!amdgpu_check_pot_argument(amdgpu_vm_size)) {
1073 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
1078 if (amdgpu_vm_size < 1) {
1079 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1085 * Max GPUVM size for Cayman, SI, CI VI are 40 bits.
1087 if (amdgpu_vm_size > 1024) {
1088 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1096 amdgpu_vm_size = -1;
1100 * amdgpu_check_arguments - validate module params
1102 * @adev: amdgpu_device pointer
1104 * Validates certain module parameters and updates
1105 * the associated values used by the driver (all asics).
1107 static void amdgpu_check_arguments(struct amdgpu_device *adev)
1109 if (amdgpu_sched_jobs < 4) {
1110 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1112 amdgpu_sched_jobs = 4;
1113 } else if (!amdgpu_check_pot_argument(amdgpu_sched_jobs)){
1114 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1116 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1119 if (amdgpu_gart_size != -1) {
1120 /* gtt size must be greater or equal to 32M */
1121 if (amdgpu_gart_size < 32) {
1122 dev_warn(adev->dev, "gart size (%d) too small\n",
1124 amdgpu_gart_size = -1;
1128 amdgpu_check_vm_size(adev);
1130 amdgpu_check_block_size(adev);
1132 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
1133 !amdgpu_check_pot_argument(amdgpu_vram_page_split))) {
1134 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1135 amdgpu_vram_page_split);
1136 amdgpu_vram_page_split = 1024;
1141 * amdgpu_switcheroo_set_state - set switcheroo state
1143 * @pdev: pci dev pointer
1144 * @state: vga_switcheroo state
1146 * Callback for the switcheroo driver. Suspends or resumes the
1147 * the asics before or after it is powered up using ACPI methods.
1149 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1151 struct drm_device *dev = pci_get_drvdata(pdev);
1153 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1156 if (state == VGA_SWITCHEROO_ON) {
1157 unsigned d3_delay = dev->pdev->d3_delay;
1159 pr_info("amdgpu: switched on\n");
1160 /* don't suspend or resume card normally */
1161 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1163 amdgpu_device_resume(dev, true, true);
1165 dev->pdev->d3_delay = d3_delay;
1167 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1168 drm_kms_helper_poll_enable(dev);
1170 pr_info("amdgpu: switched off\n");
1171 drm_kms_helper_poll_disable(dev);
1172 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1173 amdgpu_device_suspend(dev, true, true);
1174 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1179 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1181 * @pdev: pci dev pointer
1183 * Callback for the switcheroo driver. Check of the switcheroo
1184 * state can be changed.
1185 * Returns true if the state can be changed, false if not.
1187 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1189 struct drm_device *dev = pci_get_drvdata(pdev);
1192 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1193 * locking inversion with the driver load path. And the access here is
1194 * completely racy anyway. So don't bother with locking for now.
1196 return dev->open_count == 0;
1199 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1200 .set_gpu_state = amdgpu_switcheroo_set_state,
1202 .can_switch = amdgpu_switcheroo_can_switch,
1205 int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
1206 enum amd_ip_block_type block_type,
1207 enum amd_clockgating_state state)
1211 for (i = 0; i < adev->num_ip_blocks; i++) {
1212 if (!adev->ip_blocks[i].status.valid)
1214 if (adev->ip_blocks[i].version->type != block_type)
1216 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1218 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1219 (void *)adev, state);
1221 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1222 adev->ip_blocks[i].version->funcs->name, r);
1227 int amdgpu_set_powergating_state(struct amdgpu_device *adev,
1228 enum amd_ip_block_type block_type,
1229 enum amd_powergating_state state)
1233 for (i = 0; i < adev->num_ip_blocks; i++) {
1234 if (!adev->ip_blocks[i].status.valid)
1236 if (adev->ip_blocks[i].version->type != block_type)
1238 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1240 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1241 (void *)adev, state);
1243 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1244 adev->ip_blocks[i].version->funcs->name, r);
1249 void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1253 for (i = 0; i < adev->num_ip_blocks; i++) {
1254 if (!adev->ip_blocks[i].status.valid)
1256 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1257 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1261 int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1262 enum amd_ip_block_type block_type)
1266 for (i = 0; i < adev->num_ip_blocks; i++) {
1267 if (!adev->ip_blocks[i].status.valid)
1269 if (adev->ip_blocks[i].version->type == block_type) {
1270 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
1280 bool amdgpu_is_idle(struct amdgpu_device *adev,
1281 enum amd_ip_block_type block_type)
1285 for (i = 0; i < adev->num_ip_blocks; i++) {
1286 if (!adev->ip_blocks[i].status.valid)
1288 if (adev->ip_blocks[i].version->type == block_type)
1289 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
1295 struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1296 enum amd_ip_block_type type)
1300 for (i = 0; i < adev->num_ip_blocks; i++)
1301 if (adev->ip_blocks[i].version->type == type)
1302 return &adev->ip_blocks[i];
1308 * amdgpu_ip_block_version_cmp
1310 * @adev: amdgpu_device pointer
1311 * @type: enum amd_ip_block_type
1312 * @major: major version
1313 * @minor: minor version
1315 * return 0 if equal or greater
1316 * return 1 if smaller or the ip_block doesn't exist
1318 int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
1319 enum amd_ip_block_type type,
1320 u32 major, u32 minor)
1322 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
1324 if (ip_block && ((ip_block->version->major > major) ||
1325 ((ip_block->version->major == major) &&
1326 (ip_block->version->minor >= minor))))
1333 * amdgpu_ip_block_add
1335 * @adev: amdgpu_device pointer
1336 * @ip_block_version: pointer to the IP to add
1338 * Adds the IP block driver information to the collection of IPs
1341 int amdgpu_ip_block_add(struct amdgpu_device *adev,
1342 const struct amdgpu_ip_block_version *ip_block_version)
1344 if (!ip_block_version)
1347 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1352 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
1354 adev->enable_virtual_display = false;
1356 if (amdgpu_virtual_display) {
1357 struct drm_device *ddev = adev->ddev;
1358 const char *pci_address_name = pci_name(ddev->pdev);
1359 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
1361 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1362 pciaddstr_tmp = pciaddstr;
1363 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1364 pciaddname = strsep(&pciaddname_tmp, ",");
1365 if (!strcmp("all", pciaddname)
1366 || !strcmp(pci_address_name, pciaddname)) {
1370 adev->enable_virtual_display = true;
1373 res = kstrtol(pciaddname_tmp, 10,
1381 adev->mode_info.num_crtc = num_crtc;
1383 adev->mode_info.num_crtc = 1;
1389 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1390 amdgpu_virtual_display, pci_address_name,
1391 adev->enable_virtual_display, adev->mode_info.num_crtc);
1397 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1399 const struct firmware *fw;
1400 const char *chip_name;
1403 const struct gpu_info_firmware_header_v1_0 *hdr;
1405 switch (adev->asic_type) {
1409 case CHIP_POLARIS11:
1410 case CHIP_POLARIS10:
1411 case CHIP_POLARIS12:
1414 #ifdef CONFIG_DRM_AMDGPU_SI
1421 #ifdef CONFIG_DRM_AMDGPU_CIK
1431 chip_name = "vega10";
1435 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
1436 err = request_firmware(&fw, fw_name, adev->dev);
1439 "Failed to load gpu_info firmware \"%s\"\n",
1443 err = amdgpu_ucode_validate(fw);
1446 "Failed to validate gpu_info firmware \"%s\"\n",
1451 hdr = (const struct gpu_info_firmware_header_v1_0 *)fw->data;
1452 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1454 switch (hdr->version_major) {
1457 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
1458 (const struct gpu_info_firmware_v1_0 *)(fw->data +
1459 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1461 adev->gfx.config.max_shader_engines = gpu_info_fw->gc_num_se;
1462 adev->gfx.config.max_cu_per_sh = gpu_info_fw->gc_num_cu_per_sh;
1463 adev->gfx.config.max_sh_per_se = gpu_info_fw->gc_num_sh_per_se;
1464 adev->gfx.config.max_backends_per_se = gpu_info_fw->gc_num_rb_per_se;
1465 adev->gfx.config.max_texture_channel_caches =
1466 gpu_info_fw->gc_num_tccs;
1467 adev->gfx.config.max_gprs = gpu_info_fw->gc_num_gprs;
1468 adev->gfx.config.max_gs_threads = gpu_info_fw->gc_num_max_gs_thds;
1469 adev->gfx.config.gs_vgt_table_depth = gpu_info_fw->gc_gs_table_depth;
1470 adev->gfx.config.gs_prim_buffer_depth = gpu_info_fw->gc_gsprim_buff_depth;
1471 adev->gfx.config.double_offchip_lds_buf =
1472 gpu_info_fw->gc_double_offchip_lds_buffer;
1473 adev->gfx.cu_info.wave_front_size = gpu_info_fw->gc_wave_size;
1478 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1483 release_firmware(fw);
1489 static int amdgpu_early_init(struct amdgpu_device *adev)
1493 amdgpu_device_enable_virtual_display(adev);
1495 switch (adev->asic_type) {
1499 case CHIP_POLARIS11:
1500 case CHIP_POLARIS10:
1501 case CHIP_POLARIS12:
1504 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
1505 adev->family = AMDGPU_FAMILY_CZ;
1507 adev->family = AMDGPU_FAMILY_VI;
1509 r = vi_set_ip_blocks(adev);
1513 #ifdef CONFIG_DRM_AMDGPU_SI
1519 adev->family = AMDGPU_FAMILY_SI;
1520 r = si_set_ip_blocks(adev);
1525 #ifdef CONFIG_DRM_AMDGPU_CIK
1531 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1532 adev->family = AMDGPU_FAMILY_CI;
1534 adev->family = AMDGPU_FAMILY_KV;
1536 r = cik_set_ip_blocks(adev);
1542 adev->family = AMDGPU_FAMILY_AI;
1544 r = soc15_set_ip_blocks(adev);
1549 /* FIXME: not supported yet */
1553 r = amdgpu_device_parse_gpu_info_fw(adev);
1557 if (amdgpu_sriov_vf(adev)) {
1558 r = amdgpu_virt_request_full_gpu(adev, true);
1563 for (i = 0; i < adev->num_ip_blocks; i++) {
1564 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1565 DRM_ERROR("disabled ip block: %d\n", i);
1566 adev->ip_blocks[i].status.valid = false;
1568 if (adev->ip_blocks[i].version->funcs->early_init) {
1569 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
1571 adev->ip_blocks[i].status.valid = false;
1573 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1574 adev->ip_blocks[i].version->funcs->name, r);
1577 adev->ip_blocks[i].status.valid = true;
1580 adev->ip_blocks[i].status.valid = true;
1585 adev->cg_flags &= amdgpu_cg_mask;
1586 adev->pg_flags &= amdgpu_pg_mask;
1591 static int amdgpu_init(struct amdgpu_device *adev)
1595 for (i = 0; i < adev->num_ip_blocks; i++) {
1596 if (!adev->ip_blocks[i].status.valid)
1598 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
1600 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1601 adev->ip_blocks[i].version->funcs->name, r);
1604 adev->ip_blocks[i].status.sw = true;
1605 /* need to do gmc hw init early so we can allocate gpu mem */
1606 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1607 r = amdgpu_vram_scratch_init(adev);
1609 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
1612 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1614 DRM_ERROR("hw_init %d failed %d\n", i, r);
1617 r = amdgpu_wb_init(adev);
1619 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
1622 adev->ip_blocks[i].status.hw = true;
1624 /* right after GMC hw init, we create CSA */
1625 if (amdgpu_sriov_vf(adev)) {
1626 r = amdgpu_allocate_static_csa(adev);
1628 DRM_ERROR("allocate CSA failed %d\n", r);
1635 for (i = 0; i < adev->num_ip_blocks; i++) {
1636 if (!adev->ip_blocks[i].status.sw)
1638 /* gmc hw init is done early */
1639 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
1641 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1643 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1644 adev->ip_blocks[i].version->funcs->name, r);
1647 adev->ip_blocks[i].status.hw = true;
1653 static int amdgpu_late_init(struct amdgpu_device *adev)
1657 for (i = 0; i < adev->num_ip_blocks; i++) {
1658 if (!adev->ip_blocks[i].status.valid)
1660 if (adev->ip_blocks[i].version->funcs->late_init) {
1661 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
1663 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1664 adev->ip_blocks[i].version->funcs->name, r);
1667 adev->ip_blocks[i].status.late_initialized = true;
1669 /* skip CG for VCE/UVD, it's handled specially */
1670 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1671 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1672 /* enable clockgating to save power */
1673 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1676 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1677 adev->ip_blocks[i].version->funcs->name, r);
1686 static int amdgpu_fini(struct amdgpu_device *adev)
1690 /* need to disable SMC first */
1691 for (i = 0; i < adev->num_ip_blocks; i++) {
1692 if (!adev->ip_blocks[i].status.hw)
1694 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
1695 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1696 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1697 AMD_CG_STATE_UNGATE);
1699 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1700 adev->ip_blocks[i].version->funcs->name, r);
1703 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
1704 /* XXX handle errors */
1706 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1707 adev->ip_blocks[i].version->funcs->name, r);
1709 adev->ip_blocks[i].status.hw = false;
1714 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1715 if (!adev->ip_blocks[i].status.hw)
1717 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1718 amdgpu_wb_fini(adev);
1719 amdgpu_vram_scratch_fini(adev);
1722 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1723 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1724 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1725 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1726 AMD_CG_STATE_UNGATE);
1728 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1729 adev->ip_blocks[i].version->funcs->name, r);
1734 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
1735 /* XXX handle errors */
1737 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1738 adev->ip_blocks[i].version->funcs->name, r);
1741 adev->ip_blocks[i].status.hw = false;
1744 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1745 if (!adev->ip_blocks[i].status.sw)
1747 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
1748 /* XXX handle errors */
1750 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1751 adev->ip_blocks[i].version->funcs->name, r);
1753 adev->ip_blocks[i].status.sw = false;
1754 adev->ip_blocks[i].status.valid = false;
1757 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1758 if (!adev->ip_blocks[i].status.late_initialized)
1760 if (adev->ip_blocks[i].version->funcs->late_fini)
1761 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1762 adev->ip_blocks[i].status.late_initialized = false;
1765 if (amdgpu_sriov_vf(adev)) {
1766 amdgpu_bo_free_kernel(&adev->virt.csa_obj, &adev->virt.csa_vmid0_addr, NULL);
1767 amdgpu_virt_release_full_gpu(adev, false);
1773 int amdgpu_suspend(struct amdgpu_device *adev)
1777 if (amdgpu_sriov_vf(adev))
1778 amdgpu_virt_request_full_gpu(adev, false);
1780 /* ungate SMC block first */
1781 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1782 AMD_CG_STATE_UNGATE);
1784 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1787 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1788 if (!adev->ip_blocks[i].status.valid)
1790 /* ungate blocks so that suspend can properly shut them down */
1791 if (i != AMD_IP_BLOCK_TYPE_SMC) {
1792 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1793 AMD_CG_STATE_UNGATE);
1795 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1796 adev->ip_blocks[i].version->funcs->name, r);
1799 /* XXX handle errors */
1800 r = adev->ip_blocks[i].version->funcs->suspend(adev);
1801 /* XXX handle errors */
1803 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1804 adev->ip_blocks[i].version->funcs->name, r);
1808 if (amdgpu_sriov_vf(adev))
1809 amdgpu_virt_release_full_gpu(adev, false);
1814 static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
1818 static enum amd_ip_block_type ip_order[] = {
1819 AMD_IP_BLOCK_TYPE_GMC,
1820 AMD_IP_BLOCK_TYPE_COMMON,
1821 AMD_IP_BLOCK_TYPE_GFXHUB,
1822 AMD_IP_BLOCK_TYPE_MMHUB,
1823 AMD_IP_BLOCK_TYPE_IH,
1826 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1828 struct amdgpu_ip_block *block;
1830 for (j = 0; j < adev->num_ip_blocks; j++) {
1831 block = &adev->ip_blocks[j];
1833 if (block->version->type != ip_order[i] ||
1834 !block->status.valid)
1837 r = block->version->funcs->hw_init(adev);
1838 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
1845 static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
1849 static enum amd_ip_block_type ip_order[] = {
1850 AMD_IP_BLOCK_TYPE_SMC,
1851 AMD_IP_BLOCK_TYPE_DCE,
1852 AMD_IP_BLOCK_TYPE_GFX,
1853 AMD_IP_BLOCK_TYPE_SDMA,
1854 AMD_IP_BLOCK_TYPE_VCE,
1857 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1859 struct amdgpu_ip_block *block;
1861 for (j = 0; j < adev->num_ip_blocks; j++) {
1862 block = &adev->ip_blocks[j];
1864 if (block->version->type != ip_order[i] ||
1865 !block->status.valid)
1868 r = block->version->funcs->hw_init(adev);
1869 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
1876 static int amdgpu_resume(struct amdgpu_device *adev)
1880 for (i = 0; i < adev->num_ip_blocks; i++) {
1881 if (!adev->ip_blocks[i].status.valid)
1883 r = adev->ip_blocks[i].version->funcs->resume(adev);
1885 DRM_ERROR("resume of IP block <%s> failed %d\n",
1886 adev->ip_blocks[i].version->funcs->name, r);
1894 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
1896 if (adev->is_atom_fw) {
1897 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
1898 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1900 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
1901 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1906 * amdgpu_device_init - initialize the driver
1908 * @adev: amdgpu_device pointer
1909 * @pdev: drm dev pointer
1910 * @pdev: pci dev pointer
1911 * @flags: driver flags
1913 * Initializes the driver info and hw (all asics).
1914 * Returns 0 for success or an error on failure.
1915 * Called at driver startup.
1917 int amdgpu_device_init(struct amdgpu_device *adev,
1918 struct drm_device *ddev,
1919 struct pci_dev *pdev,
1923 bool runtime = false;
1926 adev->shutdown = false;
1927 adev->dev = &pdev->dev;
1930 adev->flags = flags;
1931 adev->asic_type = flags & AMD_ASIC_MASK;
1932 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
1933 adev->mc.gtt_size = 512 * 1024 * 1024;
1934 adev->accel_working = false;
1935 adev->num_rings = 0;
1936 adev->mman.buffer_funcs = NULL;
1937 adev->mman.buffer_funcs_ring = NULL;
1938 adev->vm_manager.vm_pte_funcs = NULL;
1939 adev->vm_manager.vm_pte_num_rings = 0;
1940 adev->gart.gart_funcs = NULL;
1941 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
1943 adev->smc_rreg = &amdgpu_invalid_rreg;
1944 adev->smc_wreg = &amdgpu_invalid_wreg;
1945 adev->pcie_rreg = &amdgpu_invalid_rreg;
1946 adev->pcie_wreg = &amdgpu_invalid_wreg;
1947 adev->pciep_rreg = &amdgpu_invalid_rreg;
1948 adev->pciep_wreg = &amdgpu_invalid_wreg;
1949 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1950 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1951 adev->didt_rreg = &amdgpu_invalid_rreg;
1952 adev->didt_wreg = &amdgpu_invalid_wreg;
1953 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
1954 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
1955 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1956 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1959 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1960 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1961 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
1963 /* mutex initialization are all done here so we
1964 * can recall function without having locking issues */
1965 atomic_set(&adev->irq.ih.lock, 0);
1966 mutex_init(&adev->firmware.mutex);
1967 mutex_init(&adev->pm.mutex);
1968 mutex_init(&adev->gfx.gpu_clock_mutex);
1969 mutex_init(&adev->srbm_mutex);
1970 mutex_init(&adev->grbm_idx_mutex);
1971 mutex_init(&adev->mn_lock);
1972 hash_init(adev->mn_hash);
1974 amdgpu_check_arguments(adev);
1976 /* Registers mapping */
1977 /* TODO: block userspace mapping of io register */
1978 spin_lock_init(&adev->mmio_idx_lock);
1979 spin_lock_init(&adev->smc_idx_lock);
1980 spin_lock_init(&adev->pcie_idx_lock);
1981 spin_lock_init(&adev->uvd_ctx_idx_lock);
1982 spin_lock_init(&adev->didt_idx_lock);
1983 spin_lock_init(&adev->gc_cac_idx_lock);
1984 spin_lock_init(&adev->audio_endpt_idx_lock);
1985 spin_lock_init(&adev->mm_stats.lock);
1987 INIT_LIST_HEAD(&adev->shadow_list);
1988 mutex_init(&adev->shadow_list_lock);
1990 INIT_LIST_HEAD(&adev->gtt_list);
1991 spin_lock_init(&adev->gtt_list_lock);
1993 if (adev->asic_type >= CHIP_BONAIRE) {
1994 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
1995 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
1997 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
1998 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2001 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2002 if (adev->rmmio == NULL) {
2005 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2006 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2008 if (adev->asic_type >= CHIP_BONAIRE)
2009 /* doorbell bar mapping */
2010 amdgpu_doorbell_init(adev);
2012 /* io port mapping */
2013 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2014 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2015 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2016 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2020 if (adev->rio_mem == NULL)
2021 DRM_INFO("PCI I/O BAR is not found.\n");
2023 /* early init functions */
2024 r = amdgpu_early_init(adev);
2028 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2029 /* this will fail for cards that aren't VGA class devices, just
2031 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
2033 if (amdgpu_runtime_pm == 1)
2035 if (amdgpu_device_is_px(ddev))
2037 if (!pci_is_thunderbolt_attached(adev->pdev))
2038 vga_switcheroo_register_client(adev->pdev,
2039 &amdgpu_switcheroo_ops, runtime);
2041 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2044 if (!amdgpu_get_bios(adev)) {
2049 r = amdgpu_atombios_init(adev);
2051 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
2055 /* detect if we are with an SRIOV vbios */
2056 amdgpu_device_detect_sriov_bios(adev);
2058 /* Post card if necessary */
2059 if (amdgpu_vpost_needed(adev)) {
2061 dev_err(adev->dev, "no vBIOS found\n");
2065 DRM_INFO("GPU posting now...\n");
2066 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2068 dev_err(adev->dev, "gpu post error!\n");
2072 DRM_INFO("GPU post is not needed\n");
2075 if (!adev->is_atom_fw) {
2076 /* Initialize clocks */
2077 r = amdgpu_atombios_get_clock_info(adev);
2079 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
2082 /* init i2c buses */
2083 amdgpu_atombios_i2c_init(adev);
2087 r = amdgpu_fence_driver_init(adev);
2089 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
2093 /* init the mode config */
2094 drm_mode_config_init(adev->ddev);
2096 r = amdgpu_init(adev);
2098 dev_err(adev->dev, "amdgpu_init failed\n");
2103 adev->accel_working = true;
2105 /* Initialize the buffer migration limit. */
2106 if (amdgpu_moverate >= 0)
2107 max_MBps = amdgpu_moverate;
2109 max_MBps = 8; /* Allow 8 MB/s. */
2110 /* Get a log2 for easy divisions. */
2111 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2113 r = amdgpu_ib_pool_init(adev);
2115 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
2119 r = amdgpu_ib_ring_tests(adev);
2121 DRM_ERROR("ib ring test failed (%d).\n", r);
2123 amdgpu_fbdev_init(adev);
2125 r = amdgpu_gem_debugfs_init(adev);
2127 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
2129 r = amdgpu_debugfs_regs_init(adev);
2131 DRM_ERROR("registering register debugfs failed (%d).\n", r);
2133 r = amdgpu_debugfs_firmware_init(adev);
2135 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
2137 if ((amdgpu_testing & 1)) {
2138 if (adev->accel_working)
2139 amdgpu_test_moves(adev);
2141 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2143 if (amdgpu_benchmarking) {
2144 if (adev->accel_working)
2145 amdgpu_benchmark(adev, amdgpu_benchmarking);
2147 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2150 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2151 * explicit gating rather than handling it automatically.
2153 r = amdgpu_late_init(adev);
2155 dev_err(adev->dev, "amdgpu_late_init failed\n");
2163 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2168 * amdgpu_device_fini - tear down the driver
2170 * @adev: amdgpu_device pointer
2172 * Tear down the driver info (all asics).
2173 * Called at driver shutdown.
2175 void amdgpu_device_fini(struct amdgpu_device *adev)
2179 DRM_INFO("amdgpu: finishing device.\n");
2180 adev->shutdown = true;
2181 if (adev->mode_info.mode_config_initialized)
2182 drm_crtc_force_disable_all(adev->ddev);
2183 /* evict vram memory */
2184 amdgpu_bo_evict_vram(adev);
2185 amdgpu_ib_pool_fini(adev);
2186 amdgpu_fence_driver_fini(adev);
2187 amdgpu_fbdev_fini(adev);
2188 r = amdgpu_fini(adev);
2189 adev->accel_working = false;
2190 /* free i2c buses */
2191 amdgpu_i2c_fini(adev);
2192 amdgpu_atombios_fini(adev);
2195 if (!pci_is_thunderbolt_attached(adev->pdev))
2196 vga_switcheroo_unregister_client(adev->pdev);
2197 if (adev->flags & AMD_IS_PX)
2198 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2199 vga_client_register(adev->pdev, NULL, NULL, NULL);
2201 pci_iounmap(adev->pdev, adev->rio_mem);
2202 adev->rio_mem = NULL;
2203 iounmap(adev->rmmio);
2205 if (adev->asic_type >= CHIP_BONAIRE)
2206 amdgpu_doorbell_fini(adev);
2207 amdgpu_debugfs_regs_cleanup(adev);
2215 * amdgpu_device_suspend - initiate device suspend
2217 * @pdev: drm dev pointer
2218 * @state: suspend state
2220 * Puts the hw in the suspend state (all asics).
2221 * Returns 0 for success or an error on failure.
2222 * Called at driver suspend.
2224 int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
2226 struct amdgpu_device *adev;
2227 struct drm_crtc *crtc;
2228 struct drm_connector *connector;
2231 if (dev == NULL || dev->dev_private == NULL) {
2235 adev = dev->dev_private;
2237 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2240 drm_kms_helper_poll_disable(dev);
2242 /* turn off display hw */
2243 drm_modeset_lock_all(dev);
2244 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2245 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2247 drm_modeset_unlock_all(dev);
2249 /* unpin the front buffers and cursors */
2250 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2251 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2252 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2253 struct amdgpu_bo *robj;
2255 if (amdgpu_crtc->cursor_bo) {
2256 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2257 r = amdgpu_bo_reserve(aobj, true);
2259 amdgpu_bo_unpin(aobj);
2260 amdgpu_bo_unreserve(aobj);
2264 if (rfb == NULL || rfb->obj == NULL) {
2267 robj = gem_to_amdgpu_bo(rfb->obj);
2268 /* don't unpin kernel fb objects */
2269 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
2270 r = amdgpu_bo_reserve(robj, true);
2272 amdgpu_bo_unpin(robj);
2273 amdgpu_bo_unreserve(robj);
2277 /* evict vram memory */
2278 amdgpu_bo_evict_vram(adev);
2280 amdgpu_fence_driver_suspend(adev);
2282 r = amdgpu_suspend(adev);
2284 /* evict remaining vram memory
2285 * This second call to evict vram is to evict the gart page table
2288 amdgpu_bo_evict_vram(adev);
2290 if (adev->is_atom_fw)
2291 amdgpu_atomfirmware_scratch_regs_save(adev);
2293 amdgpu_atombios_scratch_regs_save(adev);
2294 pci_save_state(dev->pdev);
2296 /* Shut down the device */
2297 pci_disable_device(dev->pdev);
2298 pci_set_power_state(dev->pdev, PCI_D3hot);
2300 r = amdgpu_asic_reset(adev);
2302 DRM_ERROR("amdgpu asic reset failed\n");
2307 amdgpu_fbdev_set_suspend(adev, 1);
2314 * amdgpu_device_resume - initiate device resume
2316 * @pdev: drm dev pointer
2318 * Bring the hw back to operating state (all asics).
2319 * Returns 0 for success or an error on failure.
2320 * Called at driver resume.
2322 int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
2324 struct drm_connector *connector;
2325 struct amdgpu_device *adev = dev->dev_private;
2326 struct drm_crtc *crtc;
2329 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2336 pci_set_power_state(dev->pdev, PCI_D0);
2337 pci_restore_state(dev->pdev);
2338 r = pci_enable_device(dev->pdev);
2342 if (adev->is_atom_fw)
2343 amdgpu_atomfirmware_scratch_regs_restore(adev);
2345 amdgpu_atombios_scratch_regs_restore(adev);
2348 if (amdgpu_need_post(adev)) {
2349 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2351 DRM_ERROR("amdgpu asic init failed\n");
2354 r = amdgpu_resume(adev);
2356 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
2359 amdgpu_fence_driver_resume(adev);
2362 r = amdgpu_ib_ring_tests(adev);
2364 DRM_ERROR("ib ring test failed (%d).\n", r);
2367 r = amdgpu_late_init(adev);
2372 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2373 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2375 if (amdgpu_crtc->cursor_bo) {
2376 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2377 r = amdgpu_bo_reserve(aobj, true);
2379 r = amdgpu_bo_pin(aobj,
2380 AMDGPU_GEM_DOMAIN_VRAM,
2381 &amdgpu_crtc->cursor_addr);
2383 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2384 amdgpu_bo_unreserve(aobj);
2389 /* blat the mode back in */
2391 drm_helper_resume_force_mode(dev);
2392 /* turn on display hw */
2393 drm_modeset_lock_all(dev);
2394 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2395 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2397 drm_modeset_unlock_all(dev);
2400 drm_kms_helper_poll_enable(dev);
2403 * Most of the connector probing functions try to acquire runtime pm
2404 * refs to ensure that the GPU is powered on when connector polling is
2405 * performed. Since we're calling this from a runtime PM callback,
2406 * trying to acquire rpm refs will cause us to deadlock.
2408 * Since we're guaranteed to be holding the rpm lock, it's safe to
2409 * temporarily disable the rpm helpers so this doesn't deadlock us.
2412 dev->dev->power.disable_depth++;
2414 drm_helper_hpd_irq_event(dev);
2416 dev->dev->power.disable_depth--;
2420 amdgpu_fbdev_set_suspend(adev, 0);
2429 static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2432 bool asic_hang = false;
2434 for (i = 0; i < adev->num_ip_blocks; i++) {
2435 if (!adev->ip_blocks[i].status.valid)
2437 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2438 adev->ip_blocks[i].status.hang =
2439 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2440 if (adev->ip_blocks[i].status.hang) {
2441 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
2448 static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
2452 for (i = 0; i < adev->num_ip_blocks; i++) {
2453 if (!adev->ip_blocks[i].status.valid)
2455 if (adev->ip_blocks[i].status.hang &&
2456 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2457 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
2466 static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2470 for (i = 0; i < adev->num_ip_blocks; i++) {
2471 if (!adev->ip_blocks[i].status.valid)
2473 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2474 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2475 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2476 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)) {
2477 if (adev->ip_blocks[i].status.hang) {
2478 DRM_INFO("Some block need full reset!\n");
2486 static int amdgpu_soft_reset(struct amdgpu_device *adev)
2490 for (i = 0; i < adev->num_ip_blocks; i++) {
2491 if (!adev->ip_blocks[i].status.valid)
2493 if (adev->ip_blocks[i].status.hang &&
2494 adev->ip_blocks[i].version->funcs->soft_reset) {
2495 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
2504 static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2508 for (i = 0; i < adev->num_ip_blocks; i++) {
2509 if (!adev->ip_blocks[i].status.valid)
2511 if (adev->ip_blocks[i].status.hang &&
2512 adev->ip_blocks[i].version->funcs->post_soft_reset)
2513 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
2521 bool amdgpu_need_backup(struct amdgpu_device *adev)
2523 if (adev->flags & AMD_IS_APU)
2526 return amdgpu_lockup_timeout > 0 ? true : false;
2529 static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2530 struct amdgpu_ring *ring,
2531 struct amdgpu_bo *bo,
2532 struct dma_fence **fence)
2540 r = amdgpu_bo_reserve(bo, true);
2543 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2544 /* if bo has been evicted, then no need to recover */
2545 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
2546 r = amdgpu_bo_validate(bo->shadow);
2548 DRM_ERROR("bo validate failed!\n");
2552 r = amdgpu_ttm_bind(&bo->shadow->tbo, &bo->shadow->tbo.mem);
2554 DRM_ERROR("%p bind failed\n", bo->shadow);
2558 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
2561 DRM_ERROR("recover page table failed!\n");
2566 amdgpu_bo_unreserve(bo);
2571 * amdgpu_sriov_gpu_reset - reset the asic
2573 * @adev: amdgpu device pointer
2574 * @voluntary: if this reset is requested by guest.
2575 * (true means by guest and false means by HYPERVISOR )
2577 * Attempt the reset the GPU if it has hung (all asics).
2579 * Returns 0 for success or an error on failure.
2581 int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, bool voluntary)
2585 struct amdgpu_bo *bo, *tmp;
2586 struct amdgpu_ring *ring;
2587 struct dma_fence *fence = NULL, *next = NULL;
2589 mutex_lock(&adev->virt.lock_reset);
2590 atomic_inc(&adev->gpu_reset_counter);
2591 adev->gfx.in_reset = true;
2594 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2596 /* block scheduler */
2597 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2598 ring = adev->rings[i];
2600 if (!ring || !ring->sched.thread)
2603 kthread_park(ring->sched.thread);
2604 amd_sched_hw_job_reset(&ring->sched);
2607 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2608 amdgpu_fence_driver_force_completion(adev);
2610 /* request to take full control of GPU before re-initialization */
2612 amdgpu_virt_reset_gpu(adev);
2614 amdgpu_virt_request_full_gpu(adev, true);
2617 /* Resume IP prior to SMC */
2618 amdgpu_sriov_reinit_early(adev);
2620 /* we need recover gart prior to run SMC/CP/SDMA resume */
2621 amdgpu_ttm_recover_gart(adev);
2623 /* now we are okay to resume SMC/CP/SDMA */
2624 amdgpu_sriov_reinit_late(adev);
2626 amdgpu_irq_gpu_reset_resume_helper(adev);
2628 if (amdgpu_ib_ring_tests(adev))
2629 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2631 /* release full control of GPU after ib test */
2632 amdgpu_virt_release_full_gpu(adev, true);
2634 DRM_INFO("recover vram bo from shadow\n");
2636 ring = adev->mman.buffer_funcs_ring;
2637 mutex_lock(&adev->shadow_list_lock);
2638 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2640 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2642 r = dma_fence_wait(fence, false);
2644 WARN(r, "recovery from shadow isn't completed\n");
2649 dma_fence_put(fence);
2652 mutex_unlock(&adev->shadow_list_lock);
2655 r = dma_fence_wait(fence, false);
2657 WARN(r, "recovery from shadow isn't completed\n");
2659 dma_fence_put(fence);
2661 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2662 struct amdgpu_ring *ring = adev->rings[i];
2663 if (!ring || !ring->sched.thread)
2666 amd_sched_job_recovery(&ring->sched);
2667 kthread_unpark(ring->sched.thread);
2670 drm_helper_resume_force_mode(adev->ddev);
2671 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2673 /* bad news, how to tell it to userspace ? */
2674 dev_info(adev->dev, "GPU reset failed\n");
2677 adev->gfx.in_reset = false;
2678 mutex_unlock(&adev->virt.lock_reset);
2683 * amdgpu_gpu_reset - reset the asic
2685 * @adev: amdgpu device pointer
2687 * Attempt the reset the GPU if it has hung (all asics).
2688 * Returns 0 for success or an error on failure.
2690 int amdgpu_gpu_reset(struct amdgpu_device *adev)
2694 bool need_full_reset;
2696 if (amdgpu_sriov_vf(adev))
2697 return amdgpu_sriov_gpu_reset(adev, true);
2699 if (!amdgpu_check_soft_reset(adev)) {
2700 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2704 atomic_inc(&adev->gpu_reset_counter);
2707 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2709 /* block scheduler */
2710 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2711 struct amdgpu_ring *ring = adev->rings[i];
2713 if (!ring || !ring->sched.thread)
2715 kthread_park(ring->sched.thread);
2716 amd_sched_hw_job_reset(&ring->sched);
2718 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2719 amdgpu_fence_driver_force_completion(adev);
2721 need_full_reset = amdgpu_need_full_reset(adev);
2723 if (!need_full_reset) {
2724 amdgpu_pre_soft_reset(adev);
2725 r = amdgpu_soft_reset(adev);
2726 amdgpu_post_soft_reset(adev);
2727 if (r || amdgpu_check_soft_reset(adev)) {
2728 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2729 need_full_reset = true;
2733 if (need_full_reset) {
2734 r = amdgpu_suspend(adev);
2737 /* Disable fb access */
2738 if (adev->mode_info.num_crtc) {
2739 struct amdgpu_mode_mc_save save;
2740 amdgpu_display_stop_mc_access(adev, &save);
2741 amdgpu_wait_for_idle(adev, AMD_IP_BLOCK_TYPE_GMC);
2743 if (adev->is_atom_fw)
2744 amdgpu_atomfirmware_scratch_regs_save(adev);
2746 amdgpu_atombios_scratch_regs_save(adev);
2747 r = amdgpu_asic_reset(adev);
2748 if (adev->is_atom_fw)
2749 amdgpu_atomfirmware_scratch_regs_restore(adev);
2751 amdgpu_atombios_scratch_regs_restore(adev);
2753 amdgpu_atom_asic_init(adev->mode_info.atom_context);
2756 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
2757 r = amdgpu_resume(adev);
2761 amdgpu_irq_gpu_reset_resume_helper(adev);
2762 if (need_full_reset && amdgpu_need_backup(adev)) {
2763 r = amdgpu_ttm_recover_gart(adev);
2765 DRM_ERROR("gart recovery failed!!!\n");
2767 r = amdgpu_ib_ring_tests(adev);
2769 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
2770 r = amdgpu_suspend(adev);
2771 need_full_reset = true;
2775 * recovery vm page tables, since we cannot depend on VRAM is
2776 * consistent after gpu full reset.
2778 if (need_full_reset && amdgpu_need_backup(adev)) {
2779 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2780 struct amdgpu_bo *bo, *tmp;
2781 struct dma_fence *fence = NULL, *next = NULL;
2783 DRM_INFO("recover vram bo from shadow\n");
2784 mutex_lock(&adev->shadow_list_lock);
2785 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2787 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2789 r = dma_fence_wait(fence, false);
2791 WARN(r, "recovery from shadow isn't completed\n");
2796 dma_fence_put(fence);
2799 mutex_unlock(&adev->shadow_list_lock);
2801 r = dma_fence_wait(fence, false);
2803 WARN(r, "recovery from shadow isn't completed\n");
2805 dma_fence_put(fence);
2807 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2808 struct amdgpu_ring *ring = adev->rings[i];
2810 if (!ring || !ring->sched.thread)
2813 amd_sched_job_recovery(&ring->sched);
2814 kthread_unpark(ring->sched.thread);
2817 dev_err(adev->dev, "asic resume failed (%d).\n", r);
2818 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2819 if (adev->rings[i] && adev->rings[i]->sched.thread) {
2820 kthread_unpark(adev->rings[i]->sched.thread);
2825 drm_helper_resume_force_mode(adev->ddev);
2827 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2829 /* bad news, how to tell it to userspace ? */
2830 dev_info(adev->dev, "GPU reset failed\n");
2836 void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2841 if (amdgpu_pcie_gen_cap)
2842 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2844 if (amdgpu_pcie_lane_cap)
2845 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2847 /* covers APUs as well */
2848 if (pci_is_root_bus(adev->pdev->bus)) {
2849 if (adev->pm.pcie_gen_mask == 0)
2850 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2851 if (adev->pm.pcie_mlw_mask == 0)
2852 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
2856 if (adev->pm.pcie_gen_mask == 0) {
2857 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2859 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2860 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2861 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2863 if (mask & DRM_PCIE_SPEED_25)
2864 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2865 if (mask & DRM_PCIE_SPEED_50)
2866 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2867 if (mask & DRM_PCIE_SPEED_80)
2868 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2870 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2873 if (adev->pm.pcie_mlw_mask == 0) {
2874 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2878 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
2879 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2880 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2881 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2882 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2883 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2884 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2887 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2888 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2889 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2890 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2891 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2892 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2895 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2896 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2897 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2898 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2899 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2902 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2903 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2904 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2905 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2908 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2909 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2910 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2913 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2914 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2917 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
2923 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
2931 int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
2932 const struct drm_info_list *files,
2937 for (i = 0; i < adev->debugfs_count; i++) {
2938 if (adev->debugfs[i].files == files) {
2939 /* Already registered */
2944 i = adev->debugfs_count + 1;
2945 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
2946 DRM_ERROR("Reached maximum number of debugfs components.\n");
2947 DRM_ERROR("Report so we increase "
2948 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
2951 adev->debugfs[adev->debugfs_count].files = files;
2952 adev->debugfs[adev->debugfs_count].num_files = nfiles;
2953 adev->debugfs_count = i;
2954 #if defined(CONFIG_DEBUG_FS)
2955 drm_debugfs_create_files(files, nfiles,
2956 adev->ddev->primary->debugfs_root,
2957 adev->ddev->primary);
2962 #if defined(CONFIG_DEBUG_FS)
2964 static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
2965 size_t size, loff_t *pos)
2967 struct amdgpu_device *adev = file_inode(f)->i_private;
2970 bool pm_pg_lock, use_bank;
2971 unsigned instance_bank, sh_bank, se_bank;
2973 if (size & 0x3 || *pos & 0x3)
2976 /* are we reading registers for which a PG lock is necessary? */
2977 pm_pg_lock = (*pos >> 23) & 1;
2979 if (*pos & (1ULL << 62)) {
2980 se_bank = (*pos >> 24) & 0x3FF;
2981 sh_bank = (*pos >> 34) & 0x3FF;
2982 instance_bank = (*pos >> 44) & 0x3FF;
2984 if (se_bank == 0x3FF)
2985 se_bank = 0xFFFFFFFF;
2986 if (sh_bank == 0x3FF)
2987 sh_bank = 0xFFFFFFFF;
2988 if (instance_bank == 0x3FF)
2989 instance_bank = 0xFFFFFFFF;
2995 *pos &= (1UL << 22) - 1;
2998 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
2999 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3001 mutex_lock(&adev->grbm_idx_mutex);
3002 amdgpu_gfx_select_se_sh(adev, se_bank,
3003 sh_bank, instance_bank);
3007 mutex_lock(&adev->pm.mutex);
3012 if (*pos > adev->rmmio_size)
3015 value = RREG32(*pos >> 2);
3016 r = put_user(value, (uint32_t *)buf);
3030 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3031 mutex_unlock(&adev->grbm_idx_mutex);
3035 mutex_unlock(&adev->pm.mutex);
3040 static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
3041 size_t size, loff_t *pos)
3043 struct amdgpu_device *adev = file_inode(f)->i_private;
3046 bool pm_pg_lock, use_bank;
3047 unsigned instance_bank, sh_bank, se_bank;
3049 if (size & 0x3 || *pos & 0x3)
3052 /* are we reading registers for which a PG lock is necessary? */
3053 pm_pg_lock = (*pos >> 23) & 1;
3055 if (*pos & (1ULL << 62)) {
3056 se_bank = (*pos >> 24) & 0x3FF;
3057 sh_bank = (*pos >> 34) & 0x3FF;
3058 instance_bank = (*pos >> 44) & 0x3FF;
3060 if (se_bank == 0x3FF)
3061 se_bank = 0xFFFFFFFF;
3062 if (sh_bank == 0x3FF)
3063 sh_bank = 0xFFFFFFFF;
3064 if (instance_bank == 0x3FF)
3065 instance_bank = 0xFFFFFFFF;
3071 *pos &= (1UL << 22) - 1;
3074 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3075 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3077 mutex_lock(&adev->grbm_idx_mutex);
3078 amdgpu_gfx_select_se_sh(adev, se_bank,
3079 sh_bank, instance_bank);
3083 mutex_lock(&adev->pm.mutex);
3088 if (*pos > adev->rmmio_size)
3091 r = get_user(value, (uint32_t *)buf);
3095 WREG32(*pos >> 2, value);
3104 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3105 mutex_unlock(&adev->grbm_idx_mutex);
3109 mutex_unlock(&adev->pm.mutex);
3114 static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
3115 size_t size, loff_t *pos)
3117 struct amdgpu_device *adev = file_inode(f)->i_private;
3121 if (size & 0x3 || *pos & 0x3)
3127 value = RREG32_PCIE(*pos >> 2);
3128 r = put_user(value, (uint32_t *)buf);
3141 static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
3142 size_t size, loff_t *pos)
3144 struct amdgpu_device *adev = file_inode(f)->i_private;
3148 if (size & 0x3 || *pos & 0x3)
3154 r = get_user(value, (uint32_t *)buf);
3158 WREG32_PCIE(*pos >> 2, value);
3169 static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
3170 size_t size, loff_t *pos)
3172 struct amdgpu_device *adev = file_inode(f)->i_private;
3176 if (size & 0x3 || *pos & 0x3)
3182 value = RREG32_DIDT(*pos >> 2);
3183 r = put_user(value, (uint32_t *)buf);
3196 static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3197 size_t size, loff_t *pos)
3199 struct amdgpu_device *adev = file_inode(f)->i_private;
3203 if (size & 0x3 || *pos & 0x3)
3209 r = get_user(value, (uint32_t *)buf);
3213 WREG32_DIDT(*pos >> 2, value);
3224 static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3225 size_t size, loff_t *pos)
3227 struct amdgpu_device *adev = file_inode(f)->i_private;
3231 if (size & 0x3 || *pos & 0x3)
3237 value = RREG32_SMC(*pos);
3238 r = put_user(value, (uint32_t *)buf);
3251 static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3252 size_t size, loff_t *pos)
3254 struct amdgpu_device *adev = file_inode(f)->i_private;
3258 if (size & 0x3 || *pos & 0x3)
3264 r = get_user(value, (uint32_t *)buf);
3268 WREG32_SMC(*pos, value);
3279 static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3280 size_t size, loff_t *pos)
3282 struct amdgpu_device *adev = file_inode(f)->i_private;
3285 uint32_t *config, no_regs = 0;
3287 if (size & 0x3 || *pos & 0x3)
3290 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
3294 /* version, increment each time something is added */
3295 config[no_regs++] = 3;
3296 config[no_regs++] = adev->gfx.config.max_shader_engines;
3297 config[no_regs++] = adev->gfx.config.max_tile_pipes;
3298 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3299 config[no_regs++] = adev->gfx.config.max_sh_per_se;
3300 config[no_regs++] = adev->gfx.config.max_backends_per_se;
3301 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3302 config[no_regs++] = adev->gfx.config.max_gprs;
3303 config[no_regs++] = adev->gfx.config.max_gs_threads;
3304 config[no_regs++] = adev->gfx.config.max_hw_contexts;
3305 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3306 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3307 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3308 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3309 config[no_regs++] = adev->gfx.config.num_tile_pipes;
3310 config[no_regs++] = adev->gfx.config.backend_enable_mask;
3311 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3312 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3313 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3314 config[no_regs++] = adev->gfx.config.num_gpus;
3315 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3316 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3317 config[no_regs++] = adev->gfx.config.gb_addr_config;
3318 config[no_regs++] = adev->gfx.config.num_rbs;
3321 config[no_regs++] = adev->rev_id;
3322 config[no_regs++] = adev->pg_flags;
3323 config[no_regs++] = adev->cg_flags;
3326 config[no_regs++] = adev->family;
3327 config[no_regs++] = adev->external_rev_id;
3330 config[no_regs++] = adev->pdev->device;
3331 config[no_regs++] = adev->pdev->revision;
3332 config[no_regs++] = adev->pdev->subsystem_device;
3333 config[no_regs++] = adev->pdev->subsystem_vendor;
3335 while (size && (*pos < no_regs * 4)) {
3338 value = config[*pos >> 2];
3339 r = put_user(value, (uint32_t *)buf);
3355 static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
3356 size_t size, loff_t *pos)
3358 struct amdgpu_device *adev = file_inode(f)->i_private;
3359 int idx, x, outsize, r, valuesize;
3360 uint32_t values[16];
3362 if (size & 3 || *pos & 0x3)
3365 if (amdgpu_dpm == 0)
3368 /* convert offset to sensor number */
3371 valuesize = sizeof(values);
3372 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
3373 r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &values[0], &valuesize);
3374 else if (adev->pm.funcs && adev->pm.funcs->read_sensor)
3375 r = adev->pm.funcs->read_sensor(adev, idx, &values[0],
3380 if (size > valuesize)
3387 r = put_user(values[x++], (int32_t *)buf);
3394 return !r ? outsize : r;
3397 static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3398 size_t size, loff_t *pos)
3400 struct amdgpu_device *adev = f->f_inode->i_private;
3403 uint32_t offset, se, sh, cu, wave, simd, data[32];
3405 if (size & 3 || *pos & 3)
3409 offset = (*pos & 0x7F);
3410 se = ((*pos >> 7) & 0xFF);
3411 sh = ((*pos >> 15) & 0xFF);
3412 cu = ((*pos >> 23) & 0xFF);
3413 wave = ((*pos >> 31) & 0xFF);
3414 simd = ((*pos >> 37) & 0xFF);
3416 /* switch to the specific se/sh/cu */
3417 mutex_lock(&adev->grbm_idx_mutex);
3418 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3421 if (adev->gfx.funcs->read_wave_data)
3422 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
3424 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3425 mutex_unlock(&adev->grbm_idx_mutex);
3430 while (size && (offset < x * 4)) {
3433 value = data[offset >> 2];
3434 r = put_user(value, (uint32_t *)buf);
3447 static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3448 size_t size, loff_t *pos)
3450 struct amdgpu_device *adev = f->f_inode->i_private;
3453 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3455 if (size & 3 || *pos & 3)
3459 offset = (*pos & 0xFFF); /* in dwords */
3460 se = ((*pos >> 12) & 0xFF);
3461 sh = ((*pos >> 20) & 0xFF);
3462 cu = ((*pos >> 28) & 0xFF);
3463 wave = ((*pos >> 36) & 0xFF);
3464 simd = ((*pos >> 44) & 0xFF);
3465 thread = ((*pos >> 52) & 0xFF);
3466 bank = ((*pos >> 60) & 1);
3468 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3472 /* switch to the specific se/sh/cu */
3473 mutex_lock(&adev->grbm_idx_mutex);
3474 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3477 if (adev->gfx.funcs->read_wave_vgprs)
3478 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3480 if (adev->gfx.funcs->read_wave_sgprs)
3481 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3484 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3485 mutex_unlock(&adev->grbm_idx_mutex);
3490 value = data[offset++];
3491 r = put_user(value, (uint32_t *)buf);
3507 static const struct file_operations amdgpu_debugfs_regs_fops = {
3508 .owner = THIS_MODULE,
3509 .read = amdgpu_debugfs_regs_read,
3510 .write = amdgpu_debugfs_regs_write,
3511 .llseek = default_llseek
3513 static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3514 .owner = THIS_MODULE,
3515 .read = amdgpu_debugfs_regs_didt_read,
3516 .write = amdgpu_debugfs_regs_didt_write,
3517 .llseek = default_llseek
3519 static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3520 .owner = THIS_MODULE,
3521 .read = amdgpu_debugfs_regs_pcie_read,
3522 .write = amdgpu_debugfs_regs_pcie_write,
3523 .llseek = default_llseek
3525 static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3526 .owner = THIS_MODULE,
3527 .read = amdgpu_debugfs_regs_smc_read,
3528 .write = amdgpu_debugfs_regs_smc_write,
3529 .llseek = default_llseek
3532 static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3533 .owner = THIS_MODULE,
3534 .read = amdgpu_debugfs_gca_config_read,
3535 .llseek = default_llseek
3538 static const struct file_operations amdgpu_debugfs_sensors_fops = {
3539 .owner = THIS_MODULE,
3540 .read = amdgpu_debugfs_sensor_read,
3541 .llseek = default_llseek
3544 static const struct file_operations amdgpu_debugfs_wave_fops = {
3545 .owner = THIS_MODULE,
3546 .read = amdgpu_debugfs_wave_read,
3547 .llseek = default_llseek
3549 static const struct file_operations amdgpu_debugfs_gpr_fops = {
3550 .owner = THIS_MODULE,
3551 .read = amdgpu_debugfs_gpr_read,
3552 .llseek = default_llseek
3555 static const struct file_operations *debugfs_regs[] = {
3556 &amdgpu_debugfs_regs_fops,
3557 &amdgpu_debugfs_regs_didt_fops,
3558 &amdgpu_debugfs_regs_pcie_fops,
3559 &amdgpu_debugfs_regs_smc_fops,
3560 &amdgpu_debugfs_gca_config_fops,
3561 &amdgpu_debugfs_sensors_fops,
3562 &amdgpu_debugfs_wave_fops,
3563 &amdgpu_debugfs_gpr_fops,
3566 static const char *debugfs_regs_names[] = {
3571 "amdgpu_gca_config",
3577 static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3579 struct drm_minor *minor = adev->ddev->primary;
3580 struct dentry *ent, *root = minor->debugfs_root;
3583 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3584 ent = debugfs_create_file(debugfs_regs_names[i],
3585 S_IFREG | S_IRUGO, root,
3586 adev, debugfs_regs[i]);
3588 for (j = 0; j < i; j++) {
3589 debugfs_remove(adev->debugfs_regs[i]);
3590 adev->debugfs_regs[i] = NULL;
3592 return PTR_ERR(ent);
3596 i_size_write(ent->d_inode, adev->rmmio_size);
3597 adev->debugfs_regs[i] = ent;
3603 static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3607 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3608 if (adev->debugfs_regs[i]) {
3609 debugfs_remove(adev->debugfs_regs[i]);
3610 adev->debugfs_regs[i] = NULL;
3615 int amdgpu_debugfs_init(struct drm_minor *minor)
3620 static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3624 static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }