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 BUG_ON(in_interrupt());
114 return amdgpu_virt_kiq_rreg(adev, reg);
117 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
118 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
122 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
123 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
124 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
125 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
127 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
131 void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
134 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
136 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
137 adev->last_mm_index = v;
140 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
141 BUG_ON(in_interrupt());
142 return amdgpu_virt_kiq_wreg(adev, reg, v);
145 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
146 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
150 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
151 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
152 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
153 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
156 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
161 u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
163 if ((reg * 4) < adev->rio_mem_size)
164 return ioread32(adev->rio_mem + (reg * 4));
166 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
167 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
171 void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
173 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
174 adev->last_mm_index = v;
177 if ((reg * 4) < adev->rio_mem_size)
178 iowrite32(v, adev->rio_mem + (reg * 4));
180 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
181 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
184 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
190 * amdgpu_mm_rdoorbell - read a doorbell dword
192 * @adev: amdgpu_device pointer
193 * @index: doorbell index
195 * Returns the value in the doorbell aperture at the
196 * requested doorbell index (CIK).
198 u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
200 if (index < adev->doorbell.num_doorbells) {
201 return readl(adev->doorbell.ptr + index);
203 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
209 * amdgpu_mm_wdoorbell - write a doorbell dword
211 * @adev: amdgpu_device pointer
212 * @index: doorbell index
215 * Writes @v to the doorbell aperture at the
216 * requested doorbell index (CIK).
218 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
220 if (index < adev->doorbell.num_doorbells) {
221 writel(v, adev->doorbell.ptr + index);
223 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
228 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
230 * @adev: amdgpu_device pointer
231 * @index: doorbell index
233 * Returns the value in the doorbell aperture at the
234 * requested doorbell index (VEGA10+).
236 u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
238 if (index < adev->doorbell.num_doorbells) {
239 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
241 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
247 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
249 * @adev: amdgpu_device pointer
250 * @index: doorbell index
253 * Writes @v to the doorbell aperture at the
254 * requested doorbell index (VEGA10+).
256 void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
258 if (index < adev->doorbell.num_doorbells) {
259 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
261 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
266 * amdgpu_invalid_rreg - dummy reg read function
268 * @adev: amdgpu device pointer
269 * @reg: offset of register
271 * Dummy register read function. Used for register blocks
272 * that certain asics don't have (all asics).
273 * Returns the value in the register.
275 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
277 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
283 * amdgpu_invalid_wreg - dummy reg write function
285 * @adev: amdgpu device pointer
286 * @reg: offset of register
287 * @v: value to write to the register
289 * Dummy register read function. Used for register blocks
290 * that certain asics don't have (all asics).
292 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
294 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
300 * amdgpu_block_invalid_rreg - dummy reg read function
302 * @adev: amdgpu device pointer
303 * @block: offset of instance
304 * @reg: offset of register
306 * Dummy register read function. Used for register blocks
307 * that certain asics don't have (all asics).
308 * Returns the value in the register.
310 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
311 uint32_t block, uint32_t reg)
313 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
320 * amdgpu_block_invalid_wreg - dummy reg write function
322 * @adev: amdgpu device pointer
323 * @block: offset of instance
324 * @reg: offset of register
325 * @v: value to write to the register
327 * Dummy register read function. Used for register blocks
328 * that certain asics don't have (all asics).
330 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
332 uint32_t reg, uint32_t v)
334 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
339 static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
341 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
342 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
343 &adev->vram_scratch.robj,
344 &adev->vram_scratch.gpu_addr,
345 (void **)&adev->vram_scratch.ptr);
348 static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
350 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
354 * amdgpu_program_register_sequence - program an array of registers.
356 * @adev: amdgpu_device pointer
357 * @registers: pointer to the register array
358 * @array_size: size of the register array
360 * Programs an array or registers with and and or masks.
361 * This is a helper for setting golden registers.
363 void amdgpu_program_register_sequence(struct amdgpu_device *adev,
364 const u32 *registers,
365 const u32 array_size)
367 u32 tmp, reg, and_mask, or_mask;
373 for (i = 0; i < array_size; i +=3) {
374 reg = registers[i + 0];
375 and_mask = registers[i + 1];
376 or_mask = registers[i + 2];
378 if (and_mask == 0xffffffff) {
389 void amdgpu_pci_config_reset(struct amdgpu_device *adev)
391 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
395 * GPU doorbell aperture helpers function.
398 * amdgpu_doorbell_init - Init doorbell driver information.
400 * @adev: amdgpu_device pointer
402 * Init doorbell driver information (CIK)
403 * Returns 0 on success, error on failure.
405 static int amdgpu_doorbell_init(struct amdgpu_device *adev)
407 /* No doorbell on SI hardware generation */
408 if (adev->asic_type < CHIP_BONAIRE) {
409 adev->doorbell.base = 0;
410 adev->doorbell.size = 0;
411 adev->doorbell.num_doorbells = 0;
412 adev->doorbell.ptr = NULL;
416 /* doorbell bar mapping */
417 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
418 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
420 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
421 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
422 if (adev->doorbell.num_doorbells == 0)
425 adev->doorbell.ptr = ioremap(adev->doorbell.base,
426 adev->doorbell.num_doorbells *
428 if (adev->doorbell.ptr == NULL)
435 * amdgpu_doorbell_fini - Tear down doorbell driver information.
437 * @adev: amdgpu_device pointer
439 * Tear down doorbell driver information (CIK)
441 static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
443 iounmap(adev->doorbell.ptr);
444 adev->doorbell.ptr = NULL;
448 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
451 * @adev: amdgpu_device pointer
452 * @aperture_base: output returning doorbell aperture base physical address
453 * @aperture_size: output returning doorbell aperture size in bytes
454 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
456 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
457 * takes doorbells required for its own rings and reports the setup to amdkfd.
458 * amdgpu reserved doorbells are at the start of the doorbell aperture.
460 void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
461 phys_addr_t *aperture_base,
462 size_t *aperture_size,
463 size_t *start_offset)
466 * The first num_doorbells are used by amdgpu.
467 * amdkfd takes whatever's left in the aperture.
469 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
470 *aperture_base = adev->doorbell.base;
471 *aperture_size = adev->doorbell.size;
472 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
482 * Writeback is the method by which the GPU updates special pages in memory
483 * with the status of certain GPU events (fences, ring pointers,etc.).
487 * amdgpu_wb_fini - Disable Writeback and free memory
489 * @adev: amdgpu_device pointer
491 * Disables Writeback and frees the Writeback memory (all asics).
492 * Used at driver shutdown.
494 static void amdgpu_wb_fini(struct amdgpu_device *adev)
496 if (adev->wb.wb_obj) {
497 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
499 (void **)&adev->wb.wb);
500 adev->wb.wb_obj = NULL;
505 * amdgpu_wb_init- Init Writeback driver info and allocate memory
507 * @adev: amdgpu_device pointer
509 * Initializes writeback and allocates writeback memory (all asics).
510 * Used at driver startup.
511 * Returns 0 on success or an -error on failure.
513 static int amdgpu_wb_init(struct amdgpu_device *adev)
517 if (adev->wb.wb_obj == NULL) {
518 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
519 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
520 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
521 &adev->wb.wb_obj, &adev->wb.gpu_addr,
522 (void **)&adev->wb.wb);
524 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
528 adev->wb.num_wb = AMDGPU_MAX_WB;
529 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
531 /* clear wb memory */
532 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
539 * amdgpu_wb_get - Allocate a wb entry
541 * @adev: amdgpu_device pointer
544 * Allocate a wb slot for use by the driver (all asics).
545 * Returns 0 on success or -EINVAL on failure.
547 int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
549 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);
553 *wb = offset * 8; /* convert to dw offset */
561 * amdgpu_wb_free - Free a wb entry
563 * @adev: amdgpu_device pointer
566 * Free a wb slot allocated for use by the driver (all asics)
568 void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
570 if (wb < adev->wb.num_wb)
571 __clear_bit(wb, adev->wb.used);
575 * amdgpu_vram_location - try to find VRAM location
576 * @adev: amdgpu device structure holding all necessary informations
577 * @mc: memory controller structure holding memory informations
578 * @base: base address at which to put VRAM
580 * Function will try to place VRAM at base address provided
581 * as parameter (which is so far either PCI aperture address or
582 * for IGP TOM base address).
584 * If there is not enough space to fit the unvisible VRAM in the 32bits
585 * address space then we limit the VRAM size to the aperture.
587 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
588 * this shouldn't be a problem as we are using the PCI aperture as a reference.
589 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
592 * Note: we use mc_vram_size as on some board we need to program the mc to
593 * cover the whole aperture even if VRAM size is inferior to aperture size
594 * Novell bug 204882 + along with lots of ubuntu ones
596 * Note: when limiting vram it's safe to overwritte real_vram_size because
597 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
598 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
601 * Note: IGP TOM addr should be the same as the aperture addr, we don't
602 * explicitly check for that though.
604 * FIXME: when reducing VRAM size align new size on power of 2.
606 void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
608 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
610 mc->vram_start = base;
611 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
612 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
613 mc->real_vram_size = mc->aper_size;
614 mc->mc_vram_size = mc->aper_size;
616 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
617 if (limit && limit < mc->real_vram_size)
618 mc->real_vram_size = limit;
619 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
620 mc->mc_vram_size >> 20, mc->vram_start,
621 mc->vram_end, mc->real_vram_size >> 20);
625 * amdgpu_gart_location - try to find GTT location
626 * @adev: amdgpu device structure holding all necessary informations
627 * @mc: memory controller structure holding memory informations
629 * Function will place try to place GTT before or after VRAM.
631 * If GTT size is bigger than space left then we ajust GTT size.
632 * Thus function will never fails.
634 * FIXME: when reducing GTT size align new size on power of 2.
636 void amdgpu_gart_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
638 u64 size_af, size_bf;
640 size_af = adev->mc.mc_mask - mc->vram_end;
641 size_bf = mc->vram_start;
642 if (size_bf > size_af) {
643 if (mc->gart_size > size_bf) {
644 dev_warn(adev->dev, "limiting GTT\n");
645 mc->gart_size = size_bf;
649 if (mc->gart_size > size_af) {
650 dev_warn(adev->dev, "limiting GTT\n");
651 mc->gart_size = size_af;
653 mc->gart_start = mc->vram_end + 1;
655 mc->gart_end = mc->gart_start + mc->gart_size - 1;
656 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
657 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
661 * Firmware Reservation functions
664 * amdgpu_fw_reserve_vram_fini - free fw reserved vram
666 * @adev: amdgpu_device pointer
668 * free fw reserved vram if it has been reserved.
670 void amdgpu_fw_reserve_vram_fini(struct amdgpu_device *adev)
672 amdgpu_bo_free_kernel(&adev->fw_vram_usage.reserved_bo,
673 NULL, &adev->fw_vram_usage.va);
677 * amdgpu_fw_reserve_vram_init - create bo vram reservation from fw
679 * @adev: amdgpu_device pointer
681 * create bo vram reservation from fw.
683 int amdgpu_fw_reserve_vram_init(struct amdgpu_device *adev)
687 u64 vram_size = adev->mc.visible_vram_size;
689 adev->fw_vram_usage.va = NULL;
690 adev->fw_vram_usage.reserved_bo = NULL;
692 if (adev->fw_vram_usage.size > 0 &&
693 adev->fw_vram_usage.size <= vram_size) {
695 r = amdgpu_bo_create(adev, adev->fw_vram_usage.size,
697 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
698 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS, NULL, NULL, 0,
699 &adev->fw_vram_usage.reserved_bo);
703 r = amdgpu_bo_reserve(adev->fw_vram_usage.reserved_bo, false);
706 r = amdgpu_bo_pin_restricted(adev->fw_vram_usage.reserved_bo,
707 AMDGPU_GEM_DOMAIN_VRAM,
708 adev->fw_vram_usage.start_offset,
709 (adev->fw_vram_usage.start_offset +
710 adev->fw_vram_usage.size), &gpu_addr);
713 r = amdgpu_bo_kmap(adev->fw_vram_usage.reserved_bo,
714 &adev->fw_vram_usage.va);
718 amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
723 amdgpu_bo_unpin(adev->fw_vram_usage.reserved_bo);
725 amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
727 amdgpu_bo_unref(&adev->fw_vram_usage.reserved_bo);
729 adev->fw_vram_usage.va = NULL;
730 adev->fw_vram_usage.reserved_bo = NULL;
736 * GPU helpers function.
739 * amdgpu_need_post - check if the hw need post or not
741 * @adev: amdgpu_device pointer
743 * Check if the asic has been initialized (all asics) at driver startup
744 * or post is needed if hw reset is performed.
745 * Returns true if need or false if not.
747 bool amdgpu_need_post(struct amdgpu_device *adev)
751 if (adev->has_hw_reset) {
752 adev->has_hw_reset = false;
756 /* bios scratch used on CIK+ */
757 if (adev->asic_type >= CHIP_BONAIRE)
758 return amdgpu_atombios_scratch_need_asic_init(adev);
760 /* check MEM_SIZE for older asics */
761 reg = amdgpu_asic_get_config_memsize(adev);
763 if ((reg != 0) && (reg != 0xffffffff))
770 static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
772 if (amdgpu_sriov_vf(adev))
775 if (amdgpu_passthrough(adev)) {
776 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
777 * some old smc fw still need driver do vPost otherwise gpu hang, while
778 * those smc fw version above 22.15 doesn't have this flaw, so we force
779 * vpost executed for smc version below 22.15
781 if (adev->asic_type == CHIP_FIJI) {
784 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
785 /* force vPost if error occured */
789 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
790 if (fw_ver < 0x00160e00)
794 return amdgpu_need_post(adev);
798 * amdgpu_dummy_page_init - init dummy page used by the driver
800 * @adev: amdgpu_device pointer
802 * Allocate the dummy page used by the driver (all asics).
803 * This dummy page is used by the driver as a filler for gart entries
804 * when pages are taken out of the GART
805 * Returns 0 on sucess, -ENOMEM on failure.
807 int amdgpu_dummy_page_init(struct amdgpu_device *adev)
809 if (adev->dummy_page.page)
811 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
812 if (adev->dummy_page.page == NULL)
814 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
815 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
816 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
817 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
818 __free_page(adev->dummy_page.page);
819 adev->dummy_page.page = NULL;
826 * amdgpu_dummy_page_fini - free dummy page used by the driver
828 * @adev: amdgpu_device pointer
830 * Frees the dummy page used by the driver (all asics).
832 void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
834 if (adev->dummy_page.page == NULL)
836 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
837 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
838 __free_page(adev->dummy_page.page);
839 adev->dummy_page.page = NULL;
843 /* ATOM accessor methods */
845 * ATOM is an interpreted byte code stored in tables in the vbios. The
846 * driver registers callbacks to access registers and the interpreter
847 * in the driver parses the tables and executes then to program specific
848 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
849 * atombios.h, and atom.c
853 * cail_pll_read - read PLL register
855 * @info: atom card_info pointer
856 * @reg: PLL register offset
858 * Provides a PLL register accessor for the atom interpreter (r4xx+).
859 * Returns the value of the PLL register.
861 static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
867 * cail_pll_write - write PLL register
869 * @info: atom card_info pointer
870 * @reg: PLL register offset
871 * @val: value to write to the pll register
873 * Provides a PLL register accessor for the atom interpreter (r4xx+).
875 static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
881 * cail_mc_read - read MC (Memory Controller) register
883 * @info: atom card_info pointer
884 * @reg: MC register offset
886 * Provides an MC register accessor for the atom interpreter (r4xx+).
887 * Returns the value of the MC register.
889 static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
895 * cail_mc_write - write MC (Memory Controller) register
897 * @info: atom card_info pointer
898 * @reg: MC register offset
899 * @val: value to write to the pll register
901 * Provides a MC register accessor for the atom interpreter (r4xx+).
903 static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
909 * cail_reg_write - write MMIO register
911 * @info: atom card_info pointer
912 * @reg: MMIO register offset
913 * @val: value to write to the pll register
915 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
917 static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
919 struct amdgpu_device *adev = info->dev->dev_private;
925 * cail_reg_read - read MMIO register
927 * @info: atom card_info pointer
928 * @reg: MMIO register offset
930 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
931 * Returns the value of the MMIO register.
933 static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
935 struct amdgpu_device *adev = info->dev->dev_private;
943 * cail_ioreg_write - write IO register
945 * @info: atom card_info pointer
946 * @reg: IO register offset
947 * @val: value to write to the pll register
949 * Provides a IO register accessor for the atom interpreter (r4xx+).
951 static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
953 struct amdgpu_device *adev = info->dev->dev_private;
959 * cail_ioreg_read - read IO register
961 * @info: atom card_info pointer
962 * @reg: IO register offset
964 * Provides an IO register accessor for the atom interpreter (r4xx+).
965 * Returns the value of the IO register.
967 static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
969 struct amdgpu_device *adev = info->dev->dev_private;
976 static ssize_t amdgpu_atombios_get_vbios_version(struct device *dev,
977 struct device_attribute *attr,
980 struct drm_device *ddev = dev_get_drvdata(dev);
981 struct amdgpu_device *adev = ddev->dev_private;
982 struct atom_context *ctx = adev->mode_info.atom_context;
984 return snprintf(buf, PAGE_SIZE, "%s\n", ctx->vbios_version);
987 static DEVICE_ATTR(vbios_version, 0444, amdgpu_atombios_get_vbios_version,
991 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
993 * @adev: amdgpu_device pointer
995 * Frees the driver info and register access callbacks for the ATOM
996 * interpreter (r4xx+).
997 * Called at driver shutdown.
999 static void amdgpu_atombios_fini(struct amdgpu_device *adev)
1001 if (adev->mode_info.atom_context) {
1002 kfree(adev->mode_info.atom_context->scratch);
1003 kfree(adev->mode_info.atom_context->iio);
1005 kfree(adev->mode_info.atom_context);
1006 adev->mode_info.atom_context = NULL;
1007 kfree(adev->mode_info.atom_card_info);
1008 adev->mode_info.atom_card_info = NULL;
1009 device_remove_file(adev->dev, &dev_attr_vbios_version);
1013 * amdgpu_atombios_init - init the driver info and callbacks for atombios
1015 * @adev: amdgpu_device pointer
1017 * Initializes the driver info and register access callbacks for the
1018 * ATOM interpreter (r4xx+).
1019 * Returns 0 on sucess, -ENOMEM on failure.
1020 * Called at driver startup.
1022 static int amdgpu_atombios_init(struct amdgpu_device *adev)
1024 struct card_info *atom_card_info =
1025 kzalloc(sizeof(struct card_info), GFP_KERNEL);
1028 if (!atom_card_info)
1031 adev->mode_info.atom_card_info = atom_card_info;
1032 atom_card_info->dev = adev->ddev;
1033 atom_card_info->reg_read = cail_reg_read;
1034 atom_card_info->reg_write = cail_reg_write;
1035 /* needed for iio ops */
1036 if (adev->rio_mem) {
1037 atom_card_info->ioreg_read = cail_ioreg_read;
1038 atom_card_info->ioreg_write = cail_ioreg_write;
1040 DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
1041 atom_card_info->ioreg_read = cail_reg_read;
1042 atom_card_info->ioreg_write = cail_reg_write;
1044 atom_card_info->mc_read = cail_mc_read;
1045 atom_card_info->mc_write = cail_mc_write;
1046 atom_card_info->pll_read = cail_pll_read;
1047 atom_card_info->pll_write = cail_pll_write;
1049 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
1050 if (!adev->mode_info.atom_context) {
1051 amdgpu_atombios_fini(adev);
1055 mutex_init(&adev->mode_info.atom_context->mutex);
1056 if (adev->is_atom_fw) {
1057 amdgpu_atomfirmware_scratch_regs_init(adev);
1058 amdgpu_atomfirmware_allocate_fb_scratch(adev);
1060 amdgpu_atombios_scratch_regs_init(adev);
1061 amdgpu_atombios_allocate_fb_scratch(adev);
1064 ret = device_create_file(adev->dev, &dev_attr_vbios_version);
1066 DRM_ERROR("Failed to create device file for VBIOS version\n");
1073 /* if we get transitioned to only one device, take VGA back */
1075 * amdgpu_vga_set_decode - enable/disable vga decode
1077 * @cookie: amdgpu_device pointer
1078 * @state: enable/disable vga decode
1080 * Enable/disable vga decode (all asics).
1081 * Returns VGA resource flags.
1083 static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
1085 struct amdgpu_device *adev = cookie;
1086 amdgpu_asic_set_vga_state(adev, state);
1088 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1089 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1091 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1094 static void amdgpu_check_block_size(struct amdgpu_device *adev)
1096 /* defines number of bits in page table versus page directory,
1097 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1098 * page table and the remaining bits are in the page directory */
1099 if (amdgpu_vm_block_size == -1)
1102 if (amdgpu_vm_block_size < 9) {
1103 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1104 amdgpu_vm_block_size);
1108 if (amdgpu_vm_block_size > 24 ||
1109 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1110 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1111 amdgpu_vm_block_size);
1118 amdgpu_vm_block_size = -1;
1121 static void amdgpu_check_vm_size(struct amdgpu_device *adev)
1123 /* no need to check the default value */
1124 if (amdgpu_vm_size == -1)
1127 if (!is_power_of_2(amdgpu_vm_size)) {
1128 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
1133 if (amdgpu_vm_size < 1) {
1134 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1140 * Max GPUVM size for Cayman, SI, CI VI are 40 bits.
1142 if (amdgpu_vm_size > 1024) {
1143 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1151 amdgpu_vm_size = -1;
1155 * amdgpu_check_arguments - validate module params
1157 * @adev: amdgpu_device pointer
1159 * Validates certain module parameters and updates
1160 * the associated values used by the driver (all asics).
1162 static void amdgpu_check_arguments(struct amdgpu_device *adev)
1164 if (amdgpu_sched_jobs < 4) {
1165 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1167 amdgpu_sched_jobs = 4;
1168 } else if (!is_power_of_2(amdgpu_sched_jobs)){
1169 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1171 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1174 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
1175 /* gart size must be greater or equal to 32M */
1176 dev_warn(adev->dev, "gart size (%d) too small\n",
1178 amdgpu_gart_size = -1;
1181 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
1182 /* gtt size must be greater or equal to 32M */
1183 dev_warn(adev->dev, "gtt size (%d) too small\n",
1185 amdgpu_gtt_size = -1;
1188 /* valid range is between 4 and 9 inclusive */
1189 if (amdgpu_vm_fragment_size != -1 &&
1190 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1191 dev_warn(adev->dev, "valid range is between 4 and 9\n");
1192 amdgpu_vm_fragment_size = -1;
1195 amdgpu_check_vm_size(adev);
1197 amdgpu_check_block_size(adev);
1199 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
1200 !is_power_of_2(amdgpu_vram_page_split))) {
1201 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1202 amdgpu_vram_page_split);
1203 amdgpu_vram_page_split = 1024;
1208 * amdgpu_switcheroo_set_state - set switcheroo state
1210 * @pdev: pci dev pointer
1211 * @state: vga_switcheroo state
1213 * Callback for the switcheroo driver. Suspends or resumes the
1214 * the asics before or after it is powered up using ACPI methods.
1216 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1218 struct drm_device *dev = pci_get_drvdata(pdev);
1220 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1223 if (state == VGA_SWITCHEROO_ON) {
1224 pr_info("amdgpu: switched on\n");
1225 /* don't suspend or resume card normally */
1226 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1228 amdgpu_device_resume(dev, true, true);
1230 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1231 drm_kms_helper_poll_enable(dev);
1233 pr_info("amdgpu: switched off\n");
1234 drm_kms_helper_poll_disable(dev);
1235 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1236 amdgpu_device_suspend(dev, true, true);
1237 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1242 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1244 * @pdev: pci dev pointer
1246 * Callback for the switcheroo driver. Check of the switcheroo
1247 * state can be changed.
1248 * Returns true if the state can be changed, false if not.
1250 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1252 struct drm_device *dev = pci_get_drvdata(pdev);
1255 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1256 * locking inversion with the driver load path. And the access here is
1257 * completely racy anyway. So don't bother with locking for now.
1259 return dev->open_count == 0;
1262 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1263 .set_gpu_state = amdgpu_switcheroo_set_state,
1265 .can_switch = amdgpu_switcheroo_can_switch,
1268 int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
1269 enum amd_ip_block_type block_type,
1270 enum amd_clockgating_state state)
1274 for (i = 0; i < adev->num_ip_blocks; i++) {
1275 if (!adev->ip_blocks[i].status.valid)
1277 if (adev->ip_blocks[i].version->type != block_type)
1279 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1281 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1282 (void *)adev, state);
1284 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1285 adev->ip_blocks[i].version->funcs->name, r);
1290 int amdgpu_set_powergating_state(struct amdgpu_device *adev,
1291 enum amd_ip_block_type block_type,
1292 enum amd_powergating_state state)
1296 for (i = 0; i < adev->num_ip_blocks; i++) {
1297 if (!adev->ip_blocks[i].status.valid)
1299 if (adev->ip_blocks[i].version->type != block_type)
1301 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1303 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1304 (void *)adev, state);
1306 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1307 adev->ip_blocks[i].version->funcs->name, r);
1312 void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1316 for (i = 0; i < adev->num_ip_blocks; i++) {
1317 if (!adev->ip_blocks[i].status.valid)
1319 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1320 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1324 int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1325 enum amd_ip_block_type block_type)
1329 for (i = 0; i < adev->num_ip_blocks; i++) {
1330 if (!adev->ip_blocks[i].status.valid)
1332 if (adev->ip_blocks[i].version->type == block_type) {
1333 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
1343 bool amdgpu_is_idle(struct amdgpu_device *adev,
1344 enum amd_ip_block_type block_type)
1348 for (i = 0; i < adev->num_ip_blocks; i++) {
1349 if (!adev->ip_blocks[i].status.valid)
1351 if (adev->ip_blocks[i].version->type == block_type)
1352 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
1358 struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1359 enum amd_ip_block_type type)
1363 for (i = 0; i < adev->num_ip_blocks; i++)
1364 if (adev->ip_blocks[i].version->type == type)
1365 return &adev->ip_blocks[i];
1371 * amdgpu_ip_block_version_cmp
1373 * @adev: amdgpu_device pointer
1374 * @type: enum amd_ip_block_type
1375 * @major: major version
1376 * @minor: minor version
1378 * return 0 if equal or greater
1379 * return 1 if smaller or the ip_block doesn't exist
1381 int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
1382 enum amd_ip_block_type type,
1383 u32 major, u32 minor)
1385 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
1387 if (ip_block && ((ip_block->version->major > major) ||
1388 ((ip_block->version->major == major) &&
1389 (ip_block->version->minor >= minor))))
1396 * amdgpu_ip_block_add
1398 * @adev: amdgpu_device pointer
1399 * @ip_block_version: pointer to the IP to add
1401 * Adds the IP block driver information to the collection of IPs
1404 int amdgpu_ip_block_add(struct amdgpu_device *adev,
1405 const struct amdgpu_ip_block_version *ip_block_version)
1407 if (!ip_block_version)
1410 DRM_DEBUG("add ip block number %d <%s>\n", adev->num_ip_blocks,
1411 ip_block_version->funcs->name);
1413 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1418 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
1420 adev->enable_virtual_display = false;
1422 if (amdgpu_virtual_display) {
1423 struct drm_device *ddev = adev->ddev;
1424 const char *pci_address_name = pci_name(ddev->pdev);
1425 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
1427 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1428 pciaddstr_tmp = pciaddstr;
1429 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1430 pciaddname = strsep(&pciaddname_tmp, ",");
1431 if (!strcmp("all", pciaddname)
1432 || !strcmp(pci_address_name, pciaddname)) {
1436 adev->enable_virtual_display = true;
1439 res = kstrtol(pciaddname_tmp, 10,
1447 adev->mode_info.num_crtc = num_crtc;
1449 adev->mode_info.num_crtc = 1;
1455 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1456 amdgpu_virtual_display, pci_address_name,
1457 adev->enable_virtual_display, adev->mode_info.num_crtc);
1463 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1465 const char *chip_name;
1468 const struct gpu_info_firmware_header_v1_0 *hdr;
1470 adev->firmware.gpu_info_fw = NULL;
1472 switch (adev->asic_type) {
1476 case CHIP_POLARIS11:
1477 case CHIP_POLARIS10:
1478 case CHIP_POLARIS12:
1481 #ifdef CONFIG_DRM_AMDGPU_SI
1488 #ifdef CONFIG_DRM_AMDGPU_CIK
1498 chip_name = "vega10";
1501 chip_name = "raven";
1505 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
1506 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
1509 "Failed to load gpu_info firmware \"%s\"\n",
1513 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
1516 "Failed to validate gpu_info firmware \"%s\"\n",
1521 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
1522 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1524 switch (hdr->version_major) {
1527 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
1528 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
1529 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1531 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1532 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1533 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1534 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
1535 adev->gfx.config.max_texture_channel_caches =
1536 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1537 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1538 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1539 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1540 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
1541 adev->gfx.config.double_offchip_lds_buf =
1542 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1543 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
1544 adev->gfx.cu_info.max_waves_per_simd =
1545 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1546 adev->gfx.cu_info.max_scratch_slots_per_cu =
1547 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1548 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
1553 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1561 static int amdgpu_early_init(struct amdgpu_device *adev)
1565 amdgpu_device_enable_virtual_display(adev);
1567 switch (adev->asic_type) {
1571 case CHIP_POLARIS11:
1572 case CHIP_POLARIS10:
1573 case CHIP_POLARIS12:
1576 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
1577 adev->family = AMDGPU_FAMILY_CZ;
1579 adev->family = AMDGPU_FAMILY_VI;
1581 r = vi_set_ip_blocks(adev);
1585 #ifdef CONFIG_DRM_AMDGPU_SI
1591 adev->family = AMDGPU_FAMILY_SI;
1592 r = si_set_ip_blocks(adev);
1597 #ifdef CONFIG_DRM_AMDGPU_CIK
1603 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1604 adev->family = AMDGPU_FAMILY_CI;
1606 adev->family = AMDGPU_FAMILY_KV;
1608 r = cik_set_ip_blocks(adev);
1615 if (adev->asic_type == CHIP_RAVEN)
1616 adev->family = AMDGPU_FAMILY_RV;
1618 adev->family = AMDGPU_FAMILY_AI;
1620 r = soc15_set_ip_blocks(adev);
1625 /* FIXME: not supported yet */
1629 r = amdgpu_device_parse_gpu_info_fw(adev);
1633 if (amdgpu_sriov_vf(adev)) {
1634 r = amdgpu_virt_request_full_gpu(adev, true);
1639 for (i = 0; i < adev->num_ip_blocks; i++) {
1640 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1641 DRM_ERROR("disabled ip block: %d <%s>\n",
1642 i, adev->ip_blocks[i].version->funcs->name);
1643 adev->ip_blocks[i].status.valid = false;
1645 if (adev->ip_blocks[i].version->funcs->early_init) {
1646 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
1648 adev->ip_blocks[i].status.valid = false;
1650 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1651 adev->ip_blocks[i].version->funcs->name, r);
1654 adev->ip_blocks[i].status.valid = true;
1657 adev->ip_blocks[i].status.valid = true;
1662 adev->cg_flags &= amdgpu_cg_mask;
1663 adev->pg_flags &= amdgpu_pg_mask;
1668 static int amdgpu_init(struct amdgpu_device *adev)
1672 for (i = 0; i < adev->num_ip_blocks; i++) {
1673 if (!adev->ip_blocks[i].status.valid)
1675 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
1677 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1678 adev->ip_blocks[i].version->funcs->name, r);
1681 adev->ip_blocks[i].status.sw = true;
1682 /* need to do gmc hw init early so we can allocate gpu mem */
1683 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1684 r = amdgpu_vram_scratch_init(adev);
1686 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
1689 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1691 DRM_ERROR("hw_init %d failed %d\n", i, r);
1694 r = amdgpu_wb_init(adev);
1696 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
1699 adev->ip_blocks[i].status.hw = true;
1701 /* right after GMC hw init, we create CSA */
1702 if (amdgpu_sriov_vf(adev)) {
1703 r = amdgpu_allocate_static_csa(adev);
1705 DRM_ERROR("allocate CSA failed %d\n", r);
1712 for (i = 0; i < adev->num_ip_blocks; i++) {
1713 if (!adev->ip_blocks[i].status.sw)
1715 /* gmc hw init is done early */
1716 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
1718 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1720 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1721 adev->ip_blocks[i].version->funcs->name, r);
1724 adev->ip_blocks[i].status.hw = true;
1730 static void amdgpu_fill_reset_magic(struct amdgpu_device *adev)
1732 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1735 static bool amdgpu_check_vram_lost(struct amdgpu_device *adev)
1737 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1738 AMDGPU_RESET_MAGIC_NUM);
1741 static int amdgpu_late_set_cg_state(struct amdgpu_device *adev)
1745 for (i = 0; i < adev->num_ip_blocks; i++) {
1746 if (!adev->ip_blocks[i].status.valid)
1748 /* skip CG for VCE/UVD, it's handled specially */
1749 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1750 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1751 /* enable clockgating to save power */
1752 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1755 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1756 adev->ip_blocks[i].version->funcs->name, r);
1764 static int amdgpu_late_init(struct amdgpu_device *adev)
1768 for (i = 0; i < adev->num_ip_blocks; i++) {
1769 if (!adev->ip_blocks[i].status.valid)
1771 if (adev->ip_blocks[i].version->funcs->late_init) {
1772 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
1774 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1775 adev->ip_blocks[i].version->funcs->name, r);
1778 adev->ip_blocks[i].status.late_initialized = true;
1782 mod_delayed_work(system_wq, &adev->late_init_work,
1783 msecs_to_jiffies(AMDGPU_RESUME_MS));
1785 amdgpu_fill_reset_magic(adev);
1790 static int amdgpu_fini(struct amdgpu_device *adev)
1794 /* need to disable SMC first */
1795 for (i = 0; i < adev->num_ip_blocks; i++) {
1796 if (!adev->ip_blocks[i].status.hw)
1798 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
1799 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1800 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1801 AMD_CG_STATE_UNGATE);
1803 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1804 adev->ip_blocks[i].version->funcs->name, r);
1807 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
1808 /* XXX handle errors */
1810 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1811 adev->ip_blocks[i].version->funcs->name, r);
1813 adev->ip_blocks[i].status.hw = false;
1818 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1819 if (!adev->ip_blocks[i].status.hw)
1821 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1822 amdgpu_wb_fini(adev);
1823 amdgpu_vram_scratch_fini(adev);
1826 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1827 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1828 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1829 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1830 AMD_CG_STATE_UNGATE);
1832 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1833 adev->ip_blocks[i].version->funcs->name, r);
1838 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
1839 /* XXX handle errors */
1841 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1842 adev->ip_blocks[i].version->funcs->name, r);
1845 adev->ip_blocks[i].status.hw = false;
1848 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1849 if (!adev->ip_blocks[i].status.sw)
1851 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
1852 /* XXX handle errors */
1854 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1855 adev->ip_blocks[i].version->funcs->name, r);
1857 adev->ip_blocks[i].status.sw = false;
1858 adev->ip_blocks[i].status.valid = false;
1861 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1862 if (!adev->ip_blocks[i].status.late_initialized)
1864 if (adev->ip_blocks[i].version->funcs->late_fini)
1865 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1866 adev->ip_blocks[i].status.late_initialized = false;
1869 if (amdgpu_sriov_vf(adev))
1870 amdgpu_virt_release_full_gpu(adev, false);
1875 static void amdgpu_late_init_func_handler(struct work_struct *work)
1877 struct amdgpu_device *adev =
1878 container_of(work, struct amdgpu_device, late_init_work.work);
1879 amdgpu_late_set_cg_state(adev);
1882 int amdgpu_suspend(struct amdgpu_device *adev)
1886 if (amdgpu_sriov_vf(adev))
1887 amdgpu_virt_request_full_gpu(adev, false);
1889 /* ungate SMC block first */
1890 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1891 AMD_CG_STATE_UNGATE);
1893 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1896 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1897 if (!adev->ip_blocks[i].status.valid)
1899 /* ungate blocks so that suspend can properly shut them down */
1900 if (i != AMD_IP_BLOCK_TYPE_SMC) {
1901 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1902 AMD_CG_STATE_UNGATE);
1904 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1905 adev->ip_blocks[i].version->funcs->name, r);
1908 /* XXX handle errors */
1909 r = adev->ip_blocks[i].version->funcs->suspend(adev);
1910 /* XXX handle errors */
1912 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1913 adev->ip_blocks[i].version->funcs->name, r);
1917 if (amdgpu_sriov_vf(adev))
1918 amdgpu_virt_release_full_gpu(adev, false);
1923 static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
1927 static enum amd_ip_block_type ip_order[] = {
1928 AMD_IP_BLOCK_TYPE_GMC,
1929 AMD_IP_BLOCK_TYPE_COMMON,
1930 AMD_IP_BLOCK_TYPE_IH,
1933 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1935 struct amdgpu_ip_block *block;
1937 for (j = 0; j < adev->num_ip_blocks; j++) {
1938 block = &adev->ip_blocks[j];
1940 if (block->version->type != ip_order[i] ||
1941 !block->status.valid)
1944 r = block->version->funcs->hw_init(adev);
1945 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
1952 static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
1956 static enum amd_ip_block_type ip_order[] = {
1957 AMD_IP_BLOCK_TYPE_SMC,
1958 AMD_IP_BLOCK_TYPE_DCE,
1959 AMD_IP_BLOCK_TYPE_GFX,
1960 AMD_IP_BLOCK_TYPE_SDMA,
1961 AMD_IP_BLOCK_TYPE_UVD,
1962 AMD_IP_BLOCK_TYPE_VCE
1965 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1967 struct amdgpu_ip_block *block;
1969 for (j = 0; j < adev->num_ip_blocks; j++) {
1970 block = &adev->ip_blocks[j];
1972 if (block->version->type != ip_order[i] ||
1973 !block->status.valid)
1976 r = block->version->funcs->hw_init(adev);
1977 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
1984 static int amdgpu_resume_phase1(struct amdgpu_device *adev)
1988 for (i = 0; i < adev->num_ip_blocks; i++) {
1989 if (!adev->ip_blocks[i].status.valid)
1991 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1992 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1993 adev->ip_blocks[i].version->type ==
1994 AMD_IP_BLOCK_TYPE_IH) {
1995 r = adev->ip_blocks[i].version->funcs->resume(adev);
1997 DRM_ERROR("resume of IP block <%s> failed %d\n",
1998 adev->ip_blocks[i].version->funcs->name, r);
2007 static int amdgpu_resume_phase2(struct amdgpu_device *adev)
2011 for (i = 0; i < adev->num_ip_blocks; i++) {
2012 if (!adev->ip_blocks[i].status.valid)
2014 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2015 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2016 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
2018 r = adev->ip_blocks[i].version->funcs->resume(adev);
2020 DRM_ERROR("resume of IP block <%s> failed %d\n",
2021 adev->ip_blocks[i].version->funcs->name, r);
2029 static int amdgpu_resume(struct amdgpu_device *adev)
2033 r = amdgpu_resume_phase1(adev);
2036 r = amdgpu_resume_phase2(adev);
2041 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
2043 if (adev->is_atom_fw) {
2044 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2045 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2047 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2048 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
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_vpost_needed(adev)) {
2217 dev_err(adev->dev, "no vBIOS found\n");
2218 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
2222 DRM_INFO("GPU posting now...\n");
2223 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2225 dev_err(adev->dev, "gpu post error!\n");
2226 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_POST_ERROR, 0, 0);
2230 DRM_INFO("GPU post is not needed\n");
2233 if (adev->is_atom_fw) {
2234 /* Initialize clocks */
2235 r = amdgpu_atomfirmware_get_clock_info(adev);
2237 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
2238 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2242 /* Initialize clocks */
2243 r = amdgpu_atombios_get_clock_info(adev);
2245 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
2246 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2249 /* init i2c buses */
2250 amdgpu_atombios_i2c_init(adev);
2254 r = amdgpu_fence_driver_init(adev);
2256 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
2257 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
2261 /* init the mode config */
2262 drm_mode_config_init(adev->ddev);
2264 r = amdgpu_init(adev);
2266 dev_err(adev->dev, "amdgpu_init failed\n");
2267 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
2272 adev->accel_working = true;
2274 amdgpu_vm_check_compute_bug(adev);
2276 /* Initialize the buffer migration limit. */
2277 if (amdgpu_moverate >= 0)
2278 max_MBps = amdgpu_moverate;
2280 max_MBps = 8; /* Allow 8 MB/s. */
2281 /* Get a log2 for easy divisions. */
2282 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2284 r = amdgpu_ib_pool_init(adev);
2286 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
2287 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
2291 r = amdgpu_ib_ring_tests(adev);
2293 DRM_ERROR("ib ring test failed (%d).\n", r);
2295 if (amdgpu_sriov_vf(adev))
2296 amdgpu_virt_init_data_exchange(adev);
2298 amdgpu_fbdev_init(adev);
2300 r = amdgpu_pm_sysfs_init(adev);
2302 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2304 r = amdgpu_gem_debugfs_init(adev);
2306 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
2308 r = amdgpu_debugfs_regs_init(adev);
2310 DRM_ERROR("registering register debugfs failed (%d).\n", r);
2312 r = amdgpu_debugfs_test_ib_ring_init(adev);
2314 DRM_ERROR("registering register test ib ring debugfs failed (%d).\n", r);
2316 r = amdgpu_debugfs_firmware_init(adev);
2318 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
2320 r = amdgpu_debugfs_vbios_dump_init(adev);
2322 DRM_ERROR("Creating vbios dump debugfs failed (%d).\n", r);
2324 if ((amdgpu_testing & 1)) {
2325 if (adev->accel_working)
2326 amdgpu_test_moves(adev);
2328 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2330 if (amdgpu_benchmarking) {
2331 if (adev->accel_working)
2332 amdgpu_benchmark(adev, amdgpu_benchmarking);
2334 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2337 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2338 * explicit gating rather than handling it automatically.
2340 r = amdgpu_late_init(adev);
2342 dev_err(adev->dev, "amdgpu_late_init failed\n");
2343 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
2350 amdgpu_vf_error_trans_all(adev);
2352 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2357 * amdgpu_device_fini - tear down the driver
2359 * @adev: amdgpu_device pointer
2361 * Tear down the driver info (all asics).
2362 * Called at driver shutdown.
2364 void amdgpu_device_fini(struct amdgpu_device *adev)
2368 DRM_INFO("amdgpu: finishing device.\n");
2369 adev->shutdown = true;
2370 if (adev->mode_info.mode_config_initialized)
2371 drm_crtc_force_disable_all(adev->ddev);
2372 /* evict vram memory */
2373 amdgpu_bo_evict_vram(adev);
2374 amdgpu_ib_pool_fini(adev);
2375 amdgpu_fw_reserve_vram_fini(adev);
2376 amdgpu_fence_driver_fini(adev);
2377 amdgpu_fbdev_fini(adev);
2378 r = amdgpu_fini(adev);
2379 if (adev->firmware.gpu_info_fw) {
2380 release_firmware(adev->firmware.gpu_info_fw);
2381 adev->firmware.gpu_info_fw = NULL;
2383 adev->accel_working = false;
2384 cancel_delayed_work_sync(&adev->late_init_work);
2385 /* free i2c buses */
2386 amdgpu_i2c_fini(adev);
2387 amdgpu_atombios_fini(adev);
2390 if (!pci_is_thunderbolt_attached(adev->pdev))
2391 vga_switcheroo_unregister_client(adev->pdev);
2392 if (adev->flags & AMD_IS_PX)
2393 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2394 vga_client_register(adev->pdev, NULL, NULL, NULL);
2396 pci_iounmap(adev->pdev, adev->rio_mem);
2397 adev->rio_mem = NULL;
2398 iounmap(adev->rmmio);
2400 amdgpu_doorbell_fini(adev);
2401 amdgpu_pm_sysfs_fini(adev);
2402 amdgpu_debugfs_regs_cleanup(adev);
2410 * amdgpu_device_suspend - initiate device suspend
2412 * @pdev: drm dev pointer
2413 * @state: suspend state
2415 * Puts the hw in the suspend state (all asics).
2416 * Returns 0 for success or an error on failure.
2417 * Called at driver suspend.
2419 int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
2421 struct amdgpu_device *adev;
2422 struct drm_crtc *crtc;
2423 struct drm_connector *connector;
2426 if (dev == NULL || dev->dev_private == NULL) {
2430 adev = dev->dev_private;
2432 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2435 drm_kms_helper_poll_disable(dev);
2437 /* turn off display hw */
2438 drm_modeset_lock_all(dev);
2439 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2440 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2442 drm_modeset_unlock_all(dev);
2444 amdgpu_amdkfd_suspend(adev);
2446 /* unpin the front buffers and cursors */
2447 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2448 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2449 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2450 struct amdgpu_bo *robj;
2452 if (amdgpu_crtc->cursor_bo) {
2453 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2454 r = amdgpu_bo_reserve(aobj, true);
2456 amdgpu_bo_unpin(aobj);
2457 amdgpu_bo_unreserve(aobj);
2461 if (rfb == NULL || rfb->obj == NULL) {
2464 robj = gem_to_amdgpu_bo(rfb->obj);
2465 /* don't unpin kernel fb objects */
2466 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
2467 r = amdgpu_bo_reserve(robj, true);
2469 amdgpu_bo_unpin(robj);
2470 amdgpu_bo_unreserve(robj);
2474 /* evict vram memory */
2475 amdgpu_bo_evict_vram(adev);
2477 amdgpu_fence_driver_suspend(adev);
2479 r = amdgpu_suspend(adev);
2481 /* evict remaining vram memory
2482 * This second call to evict vram is to evict the gart page table
2485 amdgpu_bo_evict_vram(adev);
2487 amdgpu_atombios_scratch_regs_save(adev);
2488 pci_save_state(dev->pdev);
2490 /* Shut down the device */
2491 pci_disable_device(dev->pdev);
2492 pci_set_power_state(dev->pdev, PCI_D3hot);
2494 r = amdgpu_asic_reset(adev);
2496 DRM_ERROR("amdgpu asic reset failed\n");
2501 amdgpu_fbdev_set_suspend(adev, 1);
2508 * amdgpu_device_resume - initiate device resume
2510 * @pdev: drm dev pointer
2512 * Bring the hw back to operating state (all asics).
2513 * Returns 0 for success or an error on failure.
2514 * Called at driver resume.
2516 int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
2518 struct drm_connector *connector;
2519 struct amdgpu_device *adev = dev->dev_private;
2520 struct drm_crtc *crtc;
2523 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2530 pci_set_power_state(dev->pdev, PCI_D0);
2531 pci_restore_state(dev->pdev);
2532 r = pci_enable_device(dev->pdev);
2536 amdgpu_atombios_scratch_regs_restore(adev);
2539 if (amdgpu_need_post(adev)) {
2540 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2542 DRM_ERROR("amdgpu asic init failed\n");
2545 r = amdgpu_resume(adev);
2547 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
2550 amdgpu_fence_driver_resume(adev);
2553 r = amdgpu_ib_ring_tests(adev);
2555 DRM_ERROR("ib ring test failed (%d).\n", r);
2558 r = amdgpu_late_init(adev);
2563 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2564 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2566 if (amdgpu_crtc->cursor_bo) {
2567 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2568 r = amdgpu_bo_reserve(aobj, true);
2570 r = amdgpu_bo_pin(aobj,
2571 AMDGPU_GEM_DOMAIN_VRAM,
2572 &amdgpu_crtc->cursor_addr);
2574 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2575 amdgpu_bo_unreserve(aobj);
2579 r = amdgpu_amdkfd_resume(adev);
2583 /* blat the mode back in */
2585 drm_helper_resume_force_mode(dev);
2586 /* turn on display hw */
2587 drm_modeset_lock_all(dev);
2588 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2589 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2591 drm_modeset_unlock_all(dev);
2594 drm_kms_helper_poll_enable(dev);
2597 * Most of the connector probing functions try to acquire runtime pm
2598 * refs to ensure that the GPU is powered on when connector polling is
2599 * performed. Since we're calling this from a runtime PM callback,
2600 * trying to acquire rpm refs will cause us to deadlock.
2602 * Since we're guaranteed to be holding the rpm lock, it's safe to
2603 * temporarily disable the rpm helpers so this doesn't deadlock us.
2606 dev->dev->power.disable_depth++;
2608 drm_helper_hpd_irq_event(dev);
2610 dev->dev->power.disable_depth--;
2614 amdgpu_fbdev_set_suspend(adev, 0);
2623 static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2626 bool asic_hang = false;
2628 for (i = 0; i < adev->num_ip_blocks; i++) {
2629 if (!adev->ip_blocks[i].status.valid)
2631 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2632 adev->ip_blocks[i].status.hang =
2633 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2634 if (adev->ip_blocks[i].status.hang) {
2635 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
2642 static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
2646 for (i = 0; i < adev->num_ip_blocks; i++) {
2647 if (!adev->ip_blocks[i].status.valid)
2649 if (adev->ip_blocks[i].status.hang &&
2650 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2651 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
2660 static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2664 for (i = 0; i < adev->num_ip_blocks; i++) {
2665 if (!adev->ip_blocks[i].status.valid)
2667 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2668 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2669 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2670 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
2671 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
2672 if (adev->ip_blocks[i].status.hang) {
2673 DRM_INFO("Some block need full reset!\n");
2681 static int amdgpu_soft_reset(struct amdgpu_device *adev)
2685 for (i = 0; i < adev->num_ip_blocks; i++) {
2686 if (!adev->ip_blocks[i].status.valid)
2688 if (adev->ip_blocks[i].status.hang &&
2689 adev->ip_blocks[i].version->funcs->soft_reset) {
2690 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
2699 static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2703 for (i = 0; i < adev->num_ip_blocks; i++) {
2704 if (!adev->ip_blocks[i].status.valid)
2706 if (adev->ip_blocks[i].status.hang &&
2707 adev->ip_blocks[i].version->funcs->post_soft_reset)
2708 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
2716 bool amdgpu_need_backup(struct amdgpu_device *adev)
2718 if (adev->flags & AMD_IS_APU)
2721 return amdgpu_lockup_timeout > 0 ? true : false;
2724 static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2725 struct amdgpu_ring *ring,
2726 struct amdgpu_bo *bo,
2727 struct dma_fence **fence)
2735 r = amdgpu_bo_reserve(bo, true);
2738 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2739 /* if bo has been evicted, then no need to recover */
2740 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
2741 r = amdgpu_bo_validate(bo->shadow);
2743 DRM_ERROR("bo validate failed!\n");
2747 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
2750 DRM_ERROR("recover page table failed!\n");
2755 amdgpu_bo_unreserve(bo);
2760 * amdgpu_sriov_gpu_reset - reset the asic
2762 * @adev: amdgpu device pointer
2763 * @job: which job trigger hang
2765 * Attempt the reset the GPU if it has hung (all asics).
2767 * Returns 0 for success or an error on failure.
2769 int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, struct amdgpu_job *job)
2773 struct amdgpu_bo *bo, *tmp;
2774 struct amdgpu_ring *ring;
2775 struct dma_fence *fence = NULL, *next = NULL;
2777 mutex_lock(&adev->virt.lock_reset);
2778 atomic_inc(&adev->gpu_reset_counter);
2779 adev->in_sriov_reset = true;
2782 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2784 /* we start from the ring trigger GPU hang */
2785 j = job ? job->ring->idx : 0;
2787 /* block scheduler */
2788 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2789 ring = adev->rings[i % AMDGPU_MAX_RINGS];
2790 if (!ring || !ring->sched.thread)
2793 kthread_park(ring->sched.thread);
2798 /* here give the last chance to check if job removed from mirror-list
2799 * since we already pay some time on kthread_park */
2800 if (job && list_empty(&job->base.node)) {
2801 kthread_unpark(ring->sched.thread);
2805 if (amd_sched_invalidate_job(&job->base, amdgpu_job_hang_limit))
2806 amd_sched_job_kickout(&job->base);
2808 /* only do job_reset on the hang ring if @job not NULL */
2809 amd_sched_hw_job_reset(&ring->sched);
2811 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2812 amdgpu_fence_driver_force_completion_ring(ring);
2815 /* request to take full control of GPU before re-initialization */
2817 amdgpu_virt_reset_gpu(adev);
2819 amdgpu_virt_request_full_gpu(adev, true);
2822 /* Resume IP prior to SMC */
2823 amdgpu_sriov_reinit_early(adev);
2825 /* we need recover gart prior to run SMC/CP/SDMA resume */
2826 amdgpu_ttm_recover_gart(adev);
2828 /* now we are okay to resume SMC/CP/SDMA */
2829 amdgpu_sriov_reinit_late(adev);
2831 amdgpu_irq_gpu_reset_resume_helper(adev);
2833 if (amdgpu_ib_ring_tests(adev))
2834 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2836 /* release full control of GPU after ib test */
2837 amdgpu_virt_release_full_gpu(adev, true);
2839 DRM_INFO("recover vram bo from shadow\n");
2841 ring = adev->mman.buffer_funcs_ring;
2842 mutex_lock(&adev->shadow_list_lock);
2843 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2845 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2847 r = dma_fence_wait(fence, false);
2849 WARN(r, "recovery from shadow isn't completed\n");
2854 dma_fence_put(fence);
2857 mutex_unlock(&adev->shadow_list_lock);
2860 r = dma_fence_wait(fence, false);
2862 WARN(r, "recovery from shadow isn't completed\n");
2864 dma_fence_put(fence);
2866 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2867 ring = adev->rings[i % AMDGPU_MAX_RINGS];
2868 if (!ring || !ring->sched.thread)
2871 if (job && j != i) {
2872 kthread_unpark(ring->sched.thread);
2876 amd_sched_job_recovery(&ring->sched);
2877 kthread_unpark(ring->sched.thread);
2880 drm_helper_resume_force_mode(adev->ddev);
2882 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2884 /* bad news, how to tell it to userspace ? */
2885 dev_info(adev->dev, "GPU reset failed\n");
2887 dev_info(adev->dev, "GPU reset successed!\n");
2890 adev->in_sriov_reset = false;
2891 mutex_unlock(&adev->virt.lock_reset);
2896 * amdgpu_gpu_reset - reset the asic
2898 * @adev: amdgpu device pointer
2900 * Attempt the reset the GPU if it has hung (all asics).
2901 * Returns 0 for success or an error on failure.
2903 int amdgpu_gpu_reset(struct amdgpu_device *adev)
2907 bool need_full_reset, vram_lost = false;
2909 if (!amdgpu_check_soft_reset(adev)) {
2910 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2914 atomic_inc(&adev->gpu_reset_counter);
2917 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2919 /* block scheduler */
2920 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2921 struct amdgpu_ring *ring = adev->rings[i];
2923 if (!ring || !ring->sched.thread)
2925 kthread_park(ring->sched.thread);
2926 amd_sched_hw_job_reset(&ring->sched);
2928 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2929 amdgpu_fence_driver_force_completion(adev);
2931 need_full_reset = amdgpu_need_full_reset(adev);
2933 if (!need_full_reset) {
2934 amdgpu_pre_soft_reset(adev);
2935 r = amdgpu_soft_reset(adev);
2936 amdgpu_post_soft_reset(adev);
2937 if (r || amdgpu_check_soft_reset(adev)) {
2938 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2939 need_full_reset = true;
2943 if (need_full_reset) {
2944 r = amdgpu_suspend(adev);
2947 amdgpu_atombios_scratch_regs_save(adev);
2948 r = amdgpu_asic_reset(adev);
2949 amdgpu_atombios_scratch_regs_restore(adev);
2951 amdgpu_atom_asic_init(adev->mode_info.atom_context);
2954 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
2955 r = amdgpu_resume_phase1(adev);
2958 vram_lost = amdgpu_check_vram_lost(adev);
2960 DRM_ERROR("VRAM is lost!\n");
2961 atomic_inc(&adev->vram_lost_counter);
2963 r = amdgpu_ttm_recover_gart(adev);
2966 r = amdgpu_resume_phase2(adev);
2970 amdgpu_fill_reset_magic(adev);
2975 amdgpu_irq_gpu_reset_resume_helper(adev);
2976 r = amdgpu_ib_ring_tests(adev);
2978 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
2979 r = amdgpu_suspend(adev);
2980 need_full_reset = true;
2984 * recovery vm page tables, since we cannot depend on VRAM is
2985 * consistent after gpu full reset.
2987 if (need_full_reset && amdgpu_need_backup(adev)) {
2988 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2989 struct amdgpu_bo *bo, *tmp;
2990 struct dma_fence *fence = NULL, *next = NULL;
2992 DRM_INFO("recover vram bo from shadow\n");
2993 mutex_lock(&adev->shadow_list_lock);
2994 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2996 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2998 r = dma_fence_wait(fence, false);
3000 WARN(r, "recovery from shadow isn't completed\n");
3005 dma_fence_put(fence);
3008 mutex_unlock(&adev->shadow_list_lock);
3010 r = dma_fence_wait(fence, false);
3012 WARN(r, "recovery from shadow isn't completed\n");
3014 dma_fence_put(fence);
3016 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3017 struct amdgpu_ring *ring = adev->rings[i];
3019 if (!ring || !ring->sched.thread)
3022 amd_sched_job_recovery(&ring->sched);
3023 kthread_unpark(ring->sched.thread);
3026 dev_err(adev->dev, "asic resume failed (%d).\n", r);
3027 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ASIC_RESUME_FAIL, 0, 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");
3041 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
3044 dev_info(adev->dev, "GPU reset successed!\n");
3047 amdgpu_vf_error_trans_all(adev);
3051 void amdgpu_get_pcie_info(struct amdgpu_device *adev)
3056 if (amdgpu_pcie_gen_cap)
3057 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
3059 if (amdgpu_pcie_lane_cap)
3060 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
3062 /* covers APUs as well */
3063 if (pci_is_root_bus(adev->pdev->bus)) {
3064 if (adev->pm.pcie_gen_mask == 0)
3065 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3066 if (adev->pm.pcie_mlw_mask == 0)
3067 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
3071 if (adev->pm.pcie_gen_mask == 0) {
3072 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
3074 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3075 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3076 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3078 if (mask & DRM_PCIE_SPEED_25)
3079 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
3080 if (mask & DRM_PCIE_SPEED_50)
3081 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
3082 if (mask & DRM_PCIE_SPEED_80)
3083 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
3085 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3088 if (adev->pm.pcie_mlw_mask == 0) {
3089 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
3093 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3094 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3095 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3096 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3097 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3098 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3099 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3102 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3103 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3104 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3105 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3106 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3107 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3110 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3111 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3112 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3113 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3114 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3117 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3118 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3119 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3120 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3123 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3124 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3125 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3128 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3129 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3132 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3138 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
3146 int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
3147 const struct drm_info_list *files,
3152 for (i = 0; i < adev->debugfs_count; i++) {
3153 if (adev->debugfs[i].files == files) {
3154 /* Already registered */
3159 i = adev->debugfs_count + 1;
3160 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
3161 DRM_ERROR("Reached maximum number of debugfs components.\n");
3162 DRM_ERROR("Report so we increase "
3163 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
3166 adev->debugfs[adev->debugfs_count].files = files;
3167 adev->debugfs[adev->debugfs_count].num_files = nfiles;
3168 adev->debugfs_count = i;
3169 #if defined(CONFIG_DEBUG_FS)
3170 drm_debugfs_create_files(files, nfiles,
3171 adev->ddev->primary->debugfs_root,
3172 adev->ddev->primary);
3177 #if defined(CONFIG_DEBUG_FS)
3179 static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
3180 size_t size, loff_t *pos)
3182 struct amdgpu_device *adev = file_inode(f)->i_private;
3185 bool pm_pg_lock, use_bank;
3186 unsigned instance_bank, sh_bank, se_bank;
3188 if (size & 0x3 || *pos & 0x3)
3191 /* are we reading registers for which a PG lock is necessary? */
3192 pm_pg_lock = (*pos >> 23) & 1;
3194 if (*pos & (1ULL << 62)) {
3195 se_bank = (*pos >> 24) & 0x3FF;
3196 sh_bank = (*pos >> 34) & 0x3FF;
3197 instance_bank = (*pos >> 44) & 0x3FF;
3199 if (se_bank == 0x3FF)
3200 se_bank = 0xFFFFFFFF;
3201 if (sh_bank == 0x3FF)
3202 sh_bank = 0xFFFFFFFF;
3203 if (instance_bank == 0x3FF)
3204 instance_bank = 0xFFFFFFFF;
3210 *pos &= (1UL << 22) - 1;
3213 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3214 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3216 mutex_lock(&adev->grbm_idx_mutex);
3217 amdgpu_gfx_select_se_sh(adev, se_bank,
3218 sh_bank, instance_bank);
3222 mutex_lock(&adev->pm.mutex);
3227 if (*pos > adev->rmmio_size)
3230 value = RREG32(*pos >> 2);
3231 r = put_user(value, (uint32_t *)buf);
3245 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3246 mutex_unlock(&adev->grbm_idx_mutex);
3250 mutex_unlock(&adev->pm.mutex);
3255 static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
3256 size_t size, loff_t *pos)
3258 struct amdgpu_device *adev = file_inode(f)->i_private;
3261 bool pm_pg_lock, use_bank;
3262 unsigned instance_bank, sh_bank, se_bank;
3264 if (size & 0x3 || *pos & 0x3)
3267 /* are we reading registers for which a PG lock is necessary? */
3268 pm_pg_lock = (*pos >> 23) & 1;
3270 if (*pos & (1ULL << 62)) {
3271 se_bank = (*pos >> 24) & 0x3FF;
3272 sh_bank = (*pos >> 34) & 0x3FF;
3273 instance_bank = (*pos >> 44) & 0x3FF;
3275 if (se_bank == 0x3FF)
3276 se_bank = 0xFFFFFFFF;
3277 if (sh_bank == 0x3FF)
3278 sh_bank = 0xFFFFFFFF;
3279 if (instance_bank == 0x3FF)
3280 instance_bank = 0xFFFFFFFF;
3286 *pos &= (1UL << 22) - 1;
3289 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3290 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3292 mutex_lock(&adev->grbm_idx_mutex);
3293 amdgpu_gfx_select_se_sh(adev, se_bank,
3294 sh_bank, instance_bank);
3298 mutex_lock(&adev->pm.mutex);
3303 if (*pos > adev->rmmio_size)
3306 r = get_user(value, (uint32_t *)buf);
3310 WREG32(*pos >> 2, value);
3319 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3320 mutex_unlock(&adev->grbm_idx_mutex);
3324 mutex_unlock(&adev->pm.mutex);
3329 static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
3330 size_t size, loff_t *pos)
3332 struct amdgpu_device *adev = file_inode(f)->i_private;
3336 if (size & 0x3 || *pos & 0x3)
3342 value = RREG32_PCIE(*pos >> 2);
3343 r = put_user(value, (uint32_t *)buf);
3356 static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
3357 size_t size, loff_t *pos)
3359 struct amdgpu_device *adev = file_inode(f)->i_private;
3363 if (size & 0x3 || *pos & 0x3)
3369 r = get_user(value, (uint32_t *)buf);
3373 WREG32_PCIE(*pos >> 2, value);
3384 static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
3385 size_t size, loff_t *pos)
3387 struct amdgpu_device *adev = file_inode(f)->i_private;
3391 if (size & 0x3 || *pos & 0x3)
3397 value = RREG32_DIDT(*pos >> 2);
3398 r = put_user(value, (uint32_t *)buf);
3411 static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3412 size_t size, loff_t *pos)
3414 struct amdgpu_device *adev = file_inode(f)->i_private;
3418 if (size & 0x3 || *pos & 0x3)
3424 r = get_user(value, (uint32_t *)buf);
3428 WREG32_DIDT(*pos >> 2, value);
3439 static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3440 size_t size, loff_t *pos)
3442 struct amdgpu_device *adev = file_inode(f)->i_private;
3446 if (size & 0x3 || *pos & 0x3)
3452 value = RREG32_SMC(*pos);
3453 r = put_user(value, (uint32_t *)buf);
3466 static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3467 size_t size, loff_t *pos)
3469 struct amdgpu_device *adev = file_inode(f)->i_private;
3473 if (size & 0x3 || *pos & 0x3)
3479 r = get_user(value, (uint32_t *)buf);
3483 WREG32_SMC(*pos, value);
3494 static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3495 size_t size, loff_t *pos)
3497 struct amdgpu_device *adev = file_inode(f)->i_private;
3500 uint32_t *config, no_regs = 0;
3502 if (size & 0x3 || *pos & 0x3)
3505 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
3509 /* version, increment each time something is added */
3510 config[no_regs++] = 3;
3511 config[no_regs++] = adev->gfx.config.max_shader_engines;
3512 config[no_regs++] = adev->gfx.config.max_tile_pipes;
3513 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3514 config[no_regs++] = adev->gfx.config.max_sh_per_se;
3515 config[no_regs++] = adev->gfx.config.max_backends_per_se;
3516 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3517 config[no_regs++] = adev->gfx.config.max_gprs;
3518 config[no_regs++] = adev->gfx.config.max_gs_threads;
3519 config[no_regs++] = adev->gfx.config.max_hw_contexts;
3520 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3521 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3522 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3523 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3524 config[no_regs++] = adev->gfx.config.num_tile_pipes;
3525 config[no_regs++] = adev->gfx.config.backend_enable_mask;
3526 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3527 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3528 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3529 config[no_regs++] = adev->gfx.config.num_gpus;
3530 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3531 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3532 config[no_regs++] = adev->gfx.config.gb_addr_config;
3533 config[no_regs++] = adev->gfx.config.num_rbs;
3536 config[no_regs++] = adev->rev_id;
3537 config[no_regs++] = adev->pg_flags;
3538 config[no_regs++] = adev->cg_flags;
3541 config[no_regs++] = adev->family;
3542 config[no_regs++] = adev->external_rev_id;
3545 config[no_regs++] = adev->pdev->device;
3546 config[no_regs++] = adev->pdev->revision;
3547 config[no_regs++] = adev->pdev->subsystem_device;
3548 config[no_regs++] = adev->pdev->subsystem_vendor;
3550 while (size && (*pos < no_regs * 4)) {
3553 value = config[*pos >> 2];
3554 r = put_user(value, (uint32_t *)buf);
3570 static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
3571 size_t size, loff_t *pos)
3573 struct amdgpu_device *adev = file_inode(f)->i_private;
3574 int idx, x, outsize, r, valuesize;
3575 uint32_t values[16];
3577 if (size & 3 || *pos & 0x3)
3580 if (amdgpu_dpm == 0)
3583 /* convert offset to sensor number */
3586 valuesize = sizeof(values);
3587 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
3588 r = amdgpu_dpm_read_sensor(adev, idx, &values[0], &valuesize);
3592 if (size > valuesize)
3599 r = put_user(values[x++], (int32_t *)buf);
3606 return !r ? outsize : r;
3609 static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3610 size_t size, loff_t *pos)
3612 struct amdgpu_device *adev = f->f_inode->i_private;
3615 uint32_t offset, se, sh, cu, wave, simd, data[32];
3617 if (size & 3 || *pos & 3)
3621 offset = (*pos & 0x7F);
3622 se = ((*pos >> 7) & 0xFF);
3623 sh = ((*pos >> 15) & 0xFF);
3624 cu = ((*pos >> 23) & 0xFF);
3625 wave = ((*pos >> 31) & 0xFF);
3626 simd = ((*pos >> 37) & 0xFF);
3628 /* switch to the specific se/sh/cu */
3629 mutex_lock(&adev->grbm_idx_mutex);
3630 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3633 if (adev->gfx.funcs->read_wave_data)
3634 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
3636 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3637 mutex_unlock(&adev->grbm_idx_mutex);
3642 while (size && (offset < x * 4)) {
3645 value = data[offset >> 2];
3646 r = put_user(value, (uint32_t *)buf);
3659 static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3660 size_t size, loff_t *pos)
3662 struct amdgpu_device *adev = f->f_inode->i_private;
3665 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3667 if (size & 3 || *pos & 3)
3671 offset = (*pos & 0xFFF); /* in dwords */
3672 se = ((*pos >> 12) & 0xFF);
3673 sh = ((*pos >> 20) & 0xFF);
3674 cu = ((*pos >> 28) & 0xFF);
3675 wave = ((*pos >> 36) & 0xFF);
3676 simd = ((*pos >> 44) & 0xFF);
3677 thread = ((*pos >> 52) & 0xFF);
3678 bank = ((*pos >> 60) & 1);
3680 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3684 /* switch to the specific se/sh/cu */
3685 mutex_lock(&adev->grbm_idx_mutex);
3686 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3689 if (adev->gfx.funcs->read_wave_vgprs)
3690 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3692 if (adev->gfx.funcs->read_wave_sgprs)
3693 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3696 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3697 mutex_unlock(&adev->grbm_idx_mutex);
3702 value = data[offset++];
3703 r = put_user(value, (uint32_t *)buf);
3719 static const struct file_operations amdgpu_debugfs_regs_fops = {
3720 .owner = THIS_MODULE,
3721 .read = amdgpu_debugfs_regs_read,
3722 .write = amdgpu_debugfs_regs_write,
3723 .llseek = default_llseek
3725 static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3726 .owner = THIS_MODULE,
3727 .read = amdgpu_debugfs_regs_didt_read,
3728 .write = amdgpu_debugfs_regs_didt_write,
3729 .llseek = default_llseek
3731 static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3732 .owner = THIS_MODULE,
3733 .read = amdgpu_debugfs_regs_pcie_read,
3734 .write = amdgpu_debugfs_regs_pcie_write,
3735 .llseek = default_llseek
3737 static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3738 .owner = THIS_MODULE,
3739 .read = amdgpu_debugfs_regs_smc_read,
3740 .write = amdgpu_debugfs_regs_smc_write,
3741 .llseek = default_llseek
3744 static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3745 .owner = THIS_MODULE,
3746 .read = amdgpu_debugfs_gca_config_read,
3747 .llseek = default_llseek
3750 static const struct file_operations amdgpu_debugfs_sensors_fops = {
3751 .owner = THIS_MODULE,
3752 .read = amdgpu_debugfs_sensor_read,
3753 .llseek = default_llseek
3756 static const struct file_operations amdgpu_debugfs_wave_fops = {
3757 .owner = THIS_MODULE,
3758 .read = amdgpu_debugfs_wave_read,
3759 .llseek = default_llseek
3761 static const struct file_operations amdgpu_debugfs_gpr_fops = {
3762 .owner = THIS_MODULE,
3763 .read = amdgpu_debugfs_gpr_read,
3764 .llseek = default_llseek
3767 static const struct file_operations *debugfs_regs[] = {
3768 &amdgpu_debugfs_regs_fops,
3769 &amdgpu_debugfs_regs_didt_fops,
3770 &amdgpu_debugfs_regs_pcie_fops,
3771 &amdgpu_debugfs_regs_smc_fops,
3772 &amdgpu_debugfs_gca_config_fops,
3773 &amdgpu_debugfs_sensors_fops,
3774 &amdgpu_debugfs_wave_fops,
3775 &amdgpu_debugfs_gpr_fops,
3778 static const char *debugfs_regs_names[] = {
3783 "amdgpu_gca_config",
3789 static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3791 struct drm_minor *minor = adev->ddev->primary;
3792 struct dentry *ent, *root = minor->debugfs_root;
3795 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3796 ent = debugfs_create_file(debugfs_regs_names[i],
3797 S_IFREG | S_IRUGO, root,
3798 adev, debugfs_regs[i]);
3800 for (j = 0; j < i; j++) {
3801 debugfs_remove(adev->debugfs_regs[i]);
3802 adev->debugfs_regs[i] = NULL;
3804 return PTR_ERR(ent);
3808 i_size_write(ent->d_inode, adev->rmmio_size);
3809 adev->debugfs_regs[i] = ent;
3815 static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3819 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3820 if (adev->debugfs_regs[i]) {
3821 debugfs_remove(adev->debugfs_regs[i]);
3822 adev->debugfs_regs[i] = NULL;
3827 static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data)
3829 struct drm_info_node *node = (struct drm_info_node *) m->private;
3830 struct drm_device *dev = node->minor->dev;
3831 struct amdgpu_device *adev = dev->dev_private;
3834 /* hold on the scheduler */
3835 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3836 struct amdgpu_ring *ring = adev->rings[i];
3838 if (!ring || !ring->sched.thread)
3840 kthread_park(ring->sched.thread);
3843 seq_printf(m, "run ib test:\n");
3844 r = amdgpu_ib_ring_tests(adev);
3846 seq_printf(m, "ib ring tests failed (%d).\n", r);
3848 seq_printf(m, "ib ring tests passed.\n");
3850 /* go on the scheduler */
3851 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3852 struct amdgpu_ring *ring = adev->rings[i];
3854 if (!ring || !ring->sched.thread)
3856 kthread_unpark(ring->sched.thread);
3862 static const struct drm_info_list amdgpu_debugfs_test_ib_ring_list[] = {
3863 {"amdgpu_test_ib", &amdgpu_debugfs_test_ib}
3866 static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
3868 return amdgpu_debugfs_add_files(adev,
3869 amdgpu_debugfs_test_ib_ring_list, 1);
3872 int amdgpu_debugfs_init(struct drm_minor *minor)
3877 static int amdgpu_debugfs_get_vbios_dump(struct seq_file *m, void *data)
3879 struct drm_info_node *node = (struct drm_info_node *) m->private;
3880 struct drm_device *dev = node->minor->dev;
3881 struct amdgpu_device *adev = dev->dev_private;
3883 seq_write(m, adev->bios, adev->bios_size);
3887 static const struct drm_info_list amdgpu_vbios_dump_list[] = {
3889 amdgpu_debugfs_get_vbios_dump,
3893 static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev)
3895 return amdgpu_debugfs_add_files(adev,
3896 amdgpu_vbios_dump_list, 1);
3899 static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
3903 static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3907 static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev)
3911 static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }