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>
32 #include <drm/drm_crtc_helper.h>
33 #include <drm/drm_atomic_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 const char *amdgpu_asic_name[] = {
90 bool amdgpu_device_is_px(struct drm_device *dev)
92 struct amdgpu_device *adev = dev->dev_private;
94 if (adev->flags & AMD_IS_PX)
100 * MMIO register access helper functions.
102 uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
107 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
108 return amdgpu_virt_kiq_rreg(adev, reg);
110 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
111 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
115 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
116 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
117 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
118 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
120 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
124 void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
127 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
129 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
130 adev->last_mm_index = v;
133 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
134 return amdgpu_virt_kiq_wreg(adev, reg, v);
136 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
137 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
141 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
142 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
143 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
144 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
147 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
152 u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
154 if ((reg * 4) < adev->rio_mem_size)
155 return ioread32(adev->rio_mem + (reg * 4));
157 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
158 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
162 void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
164 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
165 adev->last_mm_index = v;
168 if ((reg * 4) < adev->rio_mem_size)
169 iowrite32(v, adev->rio_mem + (reg * 4));
171 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
172 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
175 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
181 * amdgpu_mm_rdoorbell - read a doorbell dword
183 * @adev: amdgpu_device pointer
184 * @index: doorbell index
186 * Returns the value in the doorbell aperture at the
187 * requested doorbell index (CIK).
189 u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
191 if (index < adev->doorbell.num_doorbells) {
192 return readl(adev->doorbell.ptr + index);
194 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
200 * amdgpu_mm_wdoorbell - write a doorbell dword
202 * @adev: amdgpu_device pointer
203 * @index: doorbell index
206 * Writes @v to the doorbell aperture at the
207 * requested doorbell index (CIK).
209 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
211 if (index < adev->doorbell.num_doorbells) {
212 writel(v, adev->doorbell.ptr + index);
214 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
219 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
221 * @adev: amdgpu_device pointer
222 * @index: doorbell index
224 * Returns the value in the doorbell aperture at the
225 * requested doorbell index (VEGA10+).
227 u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
229 if (index < adev->doorbell.num_doorbells) {
230 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
232 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
238 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
240 * @adev: amdgpu_device pointer
241 * @index: doorbell index
244 * Writes @v to the doorbell aperture at the
245 * requested doorbell index (VEGA10+).
247 void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
249 if (index < adev->doorbell.num_doorbells) {
250 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
252 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
257 * amdgpu_invalid_rreg - dummy reg read function
259 * @adev: amdgpu device pointer
260 * @reg: offset of register
262 * Dummy register read function. Used for register blocks
263 * that certain asics don't have (all asics).
264 * Returns the value in the register.
266 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
268 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
274 * amdgpu_invalid_wreg - dummy reg write function
276 * @adev: amdgpu device pointer
277 * @reg: offset of register
278 * @v: value to write to the register
280 * Dummy register read function. Used for register blocks
281 * that certain asics don't have (all asics).
283 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
285 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
291 * amdgpu_block_invalid_rreg - dummy reg read function
293 * @adev: amdgpu device pointer
294 * @block: offset of instance
295 * @reg: offset of register
297 * Dummy register read function. Used for register blocks
298 * that certain asics don't have (all asics).
299 * Returns the value in the register.
301 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
302 uint32_t block, uint32_t reg)
304 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
311 * amdgpu_block_invalid_wreg - dummy reg write function
313 * @adev: amdgpu device pointer
314 * @block: offset of instance
315 * @reg: offset of register
316 * @v: value to write to the register
318 * Dummy register read function. Used for register blocks
319 * that certain asics don't have (all asics).
321 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
323 uint32_t reg, uint32_t v)
325 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
330 static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev)
332 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
333 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
334 &adev->vram_scratch.robj,
335 &adev->vram_scratch.gpu_addr,
336 (void **)&adev->vram_scratch.ptr);
339 static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev)
341 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
345 * amdgpu_device_program_register_sequence - program an array of registers.
347 * @adev: amdgpu_device pointer
348 * @registers: pointer to the register array
349 * @array_size: size of the register array
351 * Programs an array or registers with and and or masks.
352 * This is a helper for setting golden registers.
354 void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
355 const u32 *registers,
356 const u32 array_size)
358 u32 tmp, reg, and_mask, or_mask;
364 for (i = 0; i < array_size; i +=3) {
365 reg = registers[i + 0];
366 and_mask = registers[i + 1];
367 or_mask = registers[i + 2];
369 if (and_mask == 0xffffffff) {
380 void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
382 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
386 * GPU doorbell aperture helpers function.
389 * amdgpu_device_doorbell_init - Init doorbell driver information.
391 * @adev: amdgpu_device pointer
393 * Init doorbell driver information (CIK)
394 * Returns 0 on success, error on failure.
396 static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
398 /* No doorbell on SI hardware generation */
399 if (adev->asic_type < CHIP_BONAIRE) {
400 adev->doorbell.base = 0;
401 adev->doorbell.size = 0;
402 adev->doorbell.num_doorbells = 0;
403 adev->doorbell.ptr = NULL;
407 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
410 /* doorbell bar mapping */
411 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
412 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
414 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
415 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
416 if (adev->doorbell.num_doorbells == 0)
419 adev->doorbell.ptr = ioremap(adev->doorbell.base,
420 adev->doorbell.num_doorbells *
422 if (adev->doorbell.ptr == NULL)
429 * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
431 * @adev: amdgpu_device pointer
433 * Tear down doorbell driver information (CIK)
435 static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
437 iounmap(adev->doorbell.ptr);
438 adev->doorbell.ptr = NULL;
444 * amdgpu_device_wb_*()
445 * Writeback is the method by which the GPU updates special pages in memory
446 * with the status of certain GPU events (fences, ring pointers,etc.).
450 * amdgpu_device_wb_fini - Disable Writeback and free memory
452 * @adev: amdgpu_device pointer
454 * Disables Writeback and frees the Writeback memory (all asics).
455 * Used at driver shutdown.
457 static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
459 if (adev->wb.wb_obj) {
460 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
462 (void **)&adev->wb.wb);
463 adev->wb.wb_obj = NULL;
468 * amdgpu_device_wb_init- Init Writeback driver info and allocate memory
470 * @adev: amdgpu_device pointer
472 * Initializes writeback and allocates writeback memory (all asics).
473 * Used at driver startup.
474 * Returns 0 on success or an -error on failure.
476 static int amdgpu_device_wb_init(struct amdgpu_device *adev)
480 if (adev->wb.wb_obj == NULL) {
481 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
482 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
483 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
484 &adev->wb.wb_obj, &adev->wb.gpu_addr,
485 (void **)&adev->wb.wb);
487 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
491 adev->wb.num_wb = AMDGPU_MAX_WB;
492 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
494 /* clear wb memory */
495 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
502 * amdgpu_device_wb_get - Allocate a wb entry
504 * @adev: amdgpu_device pointer
507 * Allocate a wb slot for use by the driver (all asics).
508 * Returns 0 on success or -EINVAL on failure.
510 int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
512 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
514 if (offset < adev->wb.num_wb) {
515 __set_bit(offset, adev->wb.used);
516 *wb = offset << 3; /* convert to dw offset */
524 * amdgpu_device_wb_free - Free a wb entry
526 * @adev: amdgpu_device pointer
529 * Free a wb slot allocated for use by the driver (all asics)
531 void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
533 if (wb < adev->wb.num_wb)
534 __clear_bit(wb >> 3, adev->wb.used);
538 * amdgpu_device_vram_location - try to find VRAM location
539 * @adev: amdgpu device structure holding all necessary informations
540 * @mc: memory controller structure holding memory informations
541 * @base: base address at which to put VRAM
543 * Function will try to place VRAM at base address provided
546 void amdgpu_device_vram_location(struct amdgpu_device *adev,
547 struct amdgpu_mc *mc, u64 base)
549 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
551 mc->vram_start = base;
552 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
553 if (limit && limit < mc->real_vram_size)
554 mc->real_vram_size = limit;
555 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
556 mc->mc_vram_size >> 20, mc->vram_start,
557 mc->vram_end, mc->real_vram_size >> 20);
561 * amdgpu_device_gart_location - try to find GTT location
562 * @adev: amdgpu device structure holding all necessary informations
563 * @mc: memory controller structure holding memory informations
565 * Function will place try to place GTT before or after VRAM.
567 * If GTT size is bigger than space left then we ajust GTT size.
568 * Thus function will never fails.
570 * FIXME: when reducing GTT size align new size on power of 2.
572 void amdgpu_device_gart_location(struct amdgpu_device *adev,
573 struct amdgpu_mc *mc)
575 u64 size_af, size_bf;
577 size_af = adev->mc.mc_mask - mc->vram_end;
578 size_bf = mc->vram_start;
579 if (size_bf > size_af) {
580 if (mc->gart_size > size_bf) {
581 dev_warn(adev->dev, "limiting GTT\n");
582 mc->gart_size = size_bf;
586 if (mc->gart_size > size_af) {
587 dev_warn(adev->dev, "limiting GTT\n");
588 mc->gart_size = size_af;
590 /* VCE doesn't like it when BOs cross a 4GB segment, so align
591 * the GART base on a 4GB boundary as well.
593 mc->gart_start = ALIGN(mc->vram_end + 1, 0x100000000ULL);
595 mc->gart_end = mc->gart_start + mc->gart_size - 1;
596 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
597 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
601 * amdgpu_device_resize_fb_bar - try to resize FB BAR
603 * @adev: amdgpu_device pointer
605 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
606 * to fail, but if any of the BARs is not accessible after the size we abort
607 * driver loading by returning -ENODEV.
609 int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
611 u64 space_needed = roundup_pow_of_two(adev->mc.real_vram_size);
612 u32 rbar_size = order_base_2(((space_needed >> 20) | 1)) - 1;
613 struct pci_bus *root;
614 struct resource *res;
620 if (amdgpu_sriov_vf(adev))
623 /* Check if the root BUS has 64bit memory resources */
624 root = adev->pdev->bus;
628 pci_bus_for_each_resource(root, res, i) {
629 if (res && res->flags & IORESOURCE_MEM_64 &&
630 res->start > 0x100000000ull)
634 /* Trying to resize is pointless without a root hub window above 4GB */
638 /* Disable memory decoding while we change the BAR addresses and size */
639 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
640 pci_write_config_word(adev->pdev, PCI_COMMAND,
641 cmd & ~PCI_COMMAND_MEMORY);
643 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
644 amdgpu_device_doorbell_fini(adev);
645 if (adev->asic_type >= CHIP_BONAIRE)
646 pci_release_resource(adev->pdev, 2);
648 pci_release_resource(adev->pdev, 0);
650 r = pci_resize_resource(adev->pdev, 0, rbar_size);
652 DRM_INFO("Not enough PCI address space for a large BAR.");
653 else if (r && r != -ENOTSUPP)
654 DRM_ERROR("Problem resizing BAR0 (%d).", r);
656 pci_assign_unassigned_bus_resources(adev->pdev->bus);
658 /* When the doorbell or fb BAR isn't available we have no chance of
661 r = amdgpu_device_doorbell_init(adev);
662 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
665 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
671 * GPU helpers function.
674 * amdgpu_device_need_post - check if the hw need post or not
676 * @adev: amdgpu_device pointer
678 * Check if the asic has been initialized (all asics) at driver startup
679 * or post is needed if hw reset is performed.
680 * Returns true if need or false if not.
682 bool amdgpu_device_need_post(struct amdgpu_device *adev)
686 if (amdgpu_sriov_vf(adev))
689 if (amdgpu_passthrough(adev)) {
690 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
691 * some old smc fw still need driver do vPost otherwise gpu hang, while
692 * those smc fw version above 22.15 doesn't have this flaw, so we force
693 * vpost executed for smc version below 22.15
695 if (adev->asic_type == CHIP_FIJI) {
698 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
699 /* force vPost if error occured */
703 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
704 if (fw_ver < 0x00160e00)
709 if (adev->has_hw_reset) {
710 adev->has_hw_reset = false;
714 /* bios scratch used on CIK+ */
715 if (adev->asic_type >= CHIP_BONAIRE)
716 return amdgpu_atombios_scratch_need_asic_init(adev);
718 /* check MEM_SIZE for older asics */
719 reg = amdgpu_asic_get_config_memsize(adev);
721 if ((reg != 0) && (reg != 0xffffffff))
727 /* if we get transitioned to only one device, take VGA back */
729 * amdgpu_device_vga_set_decode - enable/disable vga decode
731 * @cookie: amdgpu_device pointer
732 * @state: enable/disable vga decode
734 * Enable/disable vga decode (all asics).
735 * Returns VGA resource flags.
737 static unsigned int amdgpu_device_vga_set_decode(void *cookie, bool state)
739 struct amdgpu_device *adev = cookie;
740 amdgpu_asic_set_vga_state(adev, state);
742 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
743 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
745 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
748 static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
750 /* defines number of bits in page table versus page directory,
751 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
752 * page table and the remaining bits are in the page directory */
753 if (amdgpu_vm_block_size == -1)
756 if (amdgpu_vm_block_size < 9) {
757 dev_warn(adev->dev, "VM page table size (%d) too small\n",
758 amdgpu_vm_block_size);
759 amdgpu_vm_block_size = -1;
763 static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
765 /* no need to check the default value */
766 if (amdgpu_vm_size == -1)
769 if (amdgpu_vm_size < 1) {
770 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
777 * amdgpu_device_check_arguments - validate module params
779 * @adev: amdgpu_device pointer
781 * Validates certain module parameters and updates
782 * the associated values used by the driver (all asics).
784 static void amdgpu_device_check_arguments(struct amdgpu_device *adev)
786 if (amdgpu_sched_jobs < 4) {
787 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
789 amdgpu_sched_jobs = 4;
790 } else if (!is_power_of_2(amdgpu_sched_jobs)){
791 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
793 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
796 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
797 /* gart size must be greater or equal to 32M */
798 dev_warn(adev->dev, "gart size (%d) too small\n",
800 amdgpu_gart_size = -1;
803 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
804 /* gtt size must be greater or equal to 32M */
805 dev_warn(adev->dev, "gtt size (%d) too small\n",
807 amdgpu_gtt_size = -1;
810 /* valid range is between 4 and 9 inclusive */
811 if (amdgpu_vm_fragment_size != -1 &&
812 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
813 dev_warn(adev->dev, "valid range is between 4 and 9\n");
814 amdgpu_vm_fragment_size = -1;
817 amdgpu_device_check_vm_size(adev);
819 amdgpu_device_check_block_size(adev);
821 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
822 !is_power_of_2(amdgpu_vram_page_split))) {
823 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
824 amdgpu_vram_page_split);
825 amdgpu_vram_page_split = 1024;
828 if (amdgpu_lockup_timeout == 0) {
829 dev_warn(adev->dev, "lockup_timeout msut be > 0, adjusting to 10000\n");
830 amdgpu_lockup_timeout = 10000;
835 * amdgpu_switcheroo_set_state - set switcheroo state
837 * @pdev: pci dev pointer
838 * @state: vga_switcheroo state
840 * Callback for the switcheroo driver. Suspends or resumes the
841 * the asics before or after it is powered up using ACPI methods.
843 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
845 struct drm_device *dev = pci_get_drvdata(pdev);
847 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
850 if (state == VGA_SWITCHEROO_ON) {
851 pr_info("amdgpu: switched on\n");
852 /* don't suspend or resume card normally */
853 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
855 amdgpu_device_resume(dev, true, true);
857 dev->switch_power_state = DRM_SWITCH_POWER_ON;
858 drm_kms_helper_poll_enable(dev);
860 pr_info("amdgpu: switched off\n");
861 drm_kms_helper_poll_disable(dev);
862 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
863 amdgpu_device_suspend(dev, true, true);
864 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
869 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
871 * @pdev: pci dev pointer
873 * Callback for the switcheroo driver. Check of the switcheroo
874 * state can be changed.
875 * Returns true if the state can be changed, false if not.
877 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
879 struct drm_device *dev = pci_get_drvdata(pdev);
882 * FIXME: open_count is protected by drm_global_mutex but that would lead to
883 * locking inversion with the driver load path. And the access here is
884 * completely racy anyway. So don't bother with locking for now.
886 return dev->open_count == 0;
889 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
890 .set_gpu_state = amdgpu_switcheroo_set_state,
892 .can_switch = amdgpu_switcheroo_can_switch,
895 int amdgpu_device_ip_set_clockgating_state(struct amdgpu_device *adev,
896 enum amd_ip_block_type block_type,
897 enum amd_clockgating_state state)
901 for (i = 0; i < adev->num_ip_blocks; i++) {
902 if (!adev->ip_blocks[i].status.valid)
904 if (adev->ip_blocks[i].version->type != block_type)
906 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
908 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
909 (void *)adev, state);
911 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
912 adev->ip_blocks[i].version->funcs->name, r);
917 int amdgpu_device_ip_set_powergating_state(struct amdgpu_device *adev,
918 enum amd_ip_block_type block_type,
919 enum amd_powergating_state state)
923 for (i = 0; i < adev->num_ip_blocks; i++) {
924 if (!adev->ip_blocks[i].status.valid)
926 if (adev->ip_blocks[i].version->type != block_type)
928 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
930 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
931 (void *)adev, state);
933 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
934 adev->ip_blocks[i].version->funcs->name, r);
939 void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
944 for (i = 0; i < adev->num_ip_blocks; i++) {
945 if (!adev->ip_blocks[i].status.valid)
947 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
948 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
952 int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
953 enum amd_ip_block_type block_type)
957 for (i = 0; i < adev->num_ip_blocks; i++) {
958 if (!adev->ip_blocks[i].status.valid)
960 if (adev->ip_blocks[i].version->type == block_type) {
961 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
971 bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
972 enum amd_ip_block_type block_type)
976 for (i = 0; i < adev->num_ip_blocks; i++) {
977 if (!adev->ip_blocks[i].status.valid)
979 if (adev->ip_blocks[i].version->type == block_type)
980 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
986 struct amdgpu_ip_block *
987 amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
988 enum amd_ip_block_type type)
992 for (i = 0; i < adev->num_ip_blocks; i++)
993 if (adev->ip_blocks[i].version->type == type)
994 return &adev->ip_blocks[i];
1000 * amdgpu_device_ip_block_version_cmp
1002 * @adev: amdgpu_device pointer
1003 * @type: enum amd_ip_block_type
1004 * @major: major version
1005 * @minor: minor version
1007 * return 0 if equal or greater
1008 * return 1 if smaller or the ip_block doesn't exist
1010 int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1011 enum amd_ip_block_type type,
1012 u32 major, u32 minor)
1014 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
1016 if (ip_block && ((ip_block->version->major > major) ||
1017 ((ip_block->version->major == major) &&
1018 (ip_block->version->minor >= minor))))
1025 * amdgpu_device_ip_block_add
1027 * @adev: amdgpu_device pointer
1028 * @ip_block_version: pointer to the IP to add
1030 * Adds the IP block driver information to the collection of IPs
1033 int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1034 const struct amdgpu_ip_block_version *ip_block_version)
1036 if (!ip_block_version)
1039 DRM_DEBUG("add ip block number %d <%s>\n", adev->num_ip_blocks,
1040 ip_block_version->funcs->name);
1042 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1047 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
1049 adev->enable_virtual_display = false;
1051 if (amdgpu_virtual_display) {
1052 struct drm_device *ddev = adev->ddev;
1053 const char *pci_address_name = pci_name(ddev->pdev);
1054 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
1056 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1057 pciaddstr_tmp = pciaddstr;
1058 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1059 pciaddname = strsep(&pciaddname_tmp, ",");
1060 if (!strcmp("all", pciaddname)
1061 || !strcmp(pci_address_name, pciaddname)) {
1065 adev->enable_virtual_display = true;
1068 res = kstrtol(pciaddname_tmp, 10,
1076 adev->mode_info.num_crtc = num_crtc;
1078 adev->mode_info.num_crtc = 1;
1084 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1085 amdgpu_virtual_display, pci_address_name,
1086 adev->enable_virtual_display, adev->mode_info.num_crtc);
1092 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1094 const char *chip_name;
1097 const struct gpu_info_firmware_header_v1_0 *hdr;
1099 adev->firmware.gpu_info_fw = NULL;
1101 switch (adev->asic_type) {
1105 case CHIP_POLARIS11:
1106 case CHIP_POLARIS10:
1107 case CHIP_POLARIS12:
1110 #ifdef CONFIG_DRM_AMDGPU_SI
1117 #ifdef CONFIG_DRM_AMDGPU_CIK
1127 chip_name = "vega10";
1130 chip_name = "raven";
1134 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
1135 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
1138 "Failed to load gpu_info firmware \"%s\"\n",
1142 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
1145 "Failed to validate gpu_info firmware \"%s\"\n",
1150 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
1151 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1153 switch (hdr->version_major) {
1156 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
1157 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
1158 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1160 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1161 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1162 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1163 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
1164 adev->gfx.config.max_texture_channel_caches =
1165 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1166 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1167 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1168 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1169 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
1170 adev->gfx.config.double_offchip_lds_buf =
1171 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1172 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
1173 adev->gfx.cu_info.max_waves_per_simd =
1174 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1175 adev->gfx.cu_info.max_scratch_slots_per_cu =
1176 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1177 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
1182 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1190 static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
1194 amdgpu_device_enable_virtual_display(adev);
1196 switch (adev->asic_type) {
1200 case CHIP_POLARIS11:
1201 case CHIP_POLARIS10:
1202 case CHIP_POLARIS12:
1205 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
1206 adev->family = AMDGPU_FAMILY_CZ;
1208 adev->family = AMDGPU_FAMILY_VI;
1210 r = vi_set_ip_blocks(adev);
1214 #ifdef CONFIG_DRM_AMDGPU_SI
1220 adev->family = AMDGPU_FAMILY_SI;
1221 r = si_set_ip_blocks(adev);
1226 #ifdef CONFIG_DRM_AMDGPU_CIK
1232 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1233 adev->family = AMDGPU_FAMILY_CI;
1235 adev->family = AMDGPU_FAMILY_KV;
1237 r = cik_set_ip_blocks(adev);
1244 if (adev->asic_type == CHIP_RAVEN)
1245 adev->family = AMDGPU_FAMILY_RV;
1247 adev->family = AMDGPU_FAMILY_AI;
1249 r = soc15_set_ip_blocks(adev);
1254 /* FIXME: not supported yet */
1258 r = amdgpu_device_parse_gpu_info_fw(adev);
1262 amdgpu_amdkfd_device_probe(adev);
1264 if (amdgpu_sriov_vf(adev)) {
1265 r = amdgpu_virt_request_full_gpu(adev, true);
1270 for (i = 0; i < adev->num_ip_blocks; i++) {
1271 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1272 DRM_ERROR("disabled ip block: %d <%s>\n",
1273 i, adev->ip_blocks[i].version->funcs->name);
1274 adev->ip_blocks[i].status.valid = false;
1276 if (adev->ip_blocks[i].version->funcs->early_init) {
1277 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
1279 adev->ip_blocks[i].status.valid = false;
1281 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1282 adev->ip_blocks[i].version->funcs->name, r);
1285 adev->ip_blocks[i].status.valid = true;
1288 adev->ip_blocks[i].status.valid = true;
1293 adev->cg_flags &= amdgpu_cg_mask;
1294 adev->pg_flags &= amdgpu_pg_mask;
1299 static int amdgpu_device_ip_init(struct amdgpu_device *adev)
1303 for (i = 0; i < adev->num_ip_blocks; i++) {
1304 if (!adev->ip_blocks[i].status.valid)
1306 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
1308 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1309 adev->ip_blocks[i].version->funcs->name, r);
1312 adev->ip_blocks[i].status.sw = true;
1313 /* need to do gmc hw init early so we can allocate gpu mem */
1314 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1315 r = amdgpu_device_vram_scratch_init(adev);
1317 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
1320 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1322 DRM_ERROR("hw_init %d failed %d\n", i, r);
1325 r = amdgpu_device_wb_init(adev);
1327 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
1330 adev->ip_blocks[i].status.hw = true;
1332 /* right after GMC hw init, we create CSA */
1333 if (amdgpu_sriov_vf(adev)) {
1334 r = amdgpu_allocate_static_csa(adev);
1336 DRM_ERROR("allocate CSA failed %d\n", r);
1343 for (i = 0; i < adev->num_ip_blocks; i++) {
1344 if (!adev->ip_blocks[i].status.sw)
1346 /* gmc hw init is done early */
1347 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
1349 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1351 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1352 adev->ip_blocks[i].version->funcs->name, r);
1355 adev->ip_blocks[i].status.hw = true;
1358 amdgpu_amdkfd_device_init(adev);
1360 if (amdgpu_sriov_vf(adev))
1361 amdgpu_virt_release_full_gpu(adev, true);
1366 static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
1368 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1371 static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
1373 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1374 AMDGPU_RESET_MAGIC_NUM);
1377 static int amdgpu_device_ip_late_set_cg_state(struct amdgpu_device *adev)
1381 for (i = 0; i < adev->num_ip_blocks; i++) {
1382 if (!adev->ip_blocks[i].status.valid)
1384 /* skip CG for VCE/UVD, it's handled specially */
1385 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1386 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1387 /* enable clockgating to save power */
1388 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1391 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1392 adev->ip_blocks[i].version->funcs->name, r);
1400 static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
1404 for (i = 0; i < adev->num_ip_blocks; i++) {
1405 if (!adev->ip_blocks[i].status.valid)
1407 if (adev->ip_blocks[i].version->funcs->late_init) {
1408 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
1410 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1411 adev->ip_blocks[i].version->funcs->name, r);
1414 adev->ip_blocks[i].status.late_initialized = true;
1418 mod_delayed_work(system_wq, &adev->late_init_work,
1419 msecs_to_jiffies(AMDGPU_RESUME_MS));
1421 amdgpu_device_fill_reset_magic(adev);
1426 static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
1430 amdgpu_amdkfd_device_fini(adev);
1431 /* need to disable SMC first */
1432 for (i = 0; i < adev->num_ip_blocks; i++) {
1433 if (!adev->ip_blocks[i].status.hw)
1435 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
1436 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1437 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1438 AMD_CG_STATE_UNGATE);
1440 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1441 adev->ip_blocks[i].version->funcs->name, r);
1444 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
1445 /* XXX handle errors */
1447 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1448 adev->ip_blocks[i].version->funcs->name, r);
1450 adev->ip_blocks[i].status.hw = false;
1455 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1456 if (!adev->ip_blocks[i].status.hw)
1458 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1459 amdgpu_free_static_csa(adev);
1460 amdgpu_device_wb_fini(adev);
1461 amdgpu_device_vram_scratch_fini(adev);
1464 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1465 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1466 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1467 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1468 AMD_CG_STATE_UNGATE);
1470 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1471 adev->ip_blocks[i].version->funcs->name, r);
1476 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
1477 /* XXX handle errors */
1479 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1480 adev->ip_blocks[i].version->funcs->name, r);
1483 adev->ip_blocks[i].status.hw = false;
1486 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1487 if (!adev->ip_blocks[i].status.sw)
1489 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
1490 /* XXX handle errors */
1492 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1493 adev->ip_blocks[i].version->funcs->name, r);
1495 adev->ip_blocks[i].status.sw = false;
1496 adev->ip_blocks[i].status.valid = false;
1499 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1500 if (!adev->ip_blocks[i].status.late_initialized)
1502 if (adev->ip_blocks[i].version->funcs->late_fini)
1503 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1504 adev->ip_blocks[i].status.late_initialized = false;
1507 if (amdgpu_sriov_vf(adev))
1508 if (amdgpu_virt_release_full_gpu(adev, false))
1509 DRM_ERROR("failed to release exclusive mode on fini\n");
1514 static void amdgpu_device_ip_late_init_func_handler(struct work_struct *work)
1516 struct amdgpu_device *adev =
1517 container_of(work, struct amdgpu_device, late_init_work.work);
1518 amdgpu_device_ip_late_set_cg_state(adev);
1521 int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
1525 if (amdgpu_sriov_vf(adev))
1526 amdgpu_virt_request_full_gpu(adev, false);
1528 /* ungate SMC block first */
1529 r = amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1530 AMD_CG_STATE_UNGATE);
1532 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n", r);
1535 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1536 if (!adev->ip_blocks[i].status.valid)
1538 /* ungate blocks so that suspend can properly shut them down */
1539 if (i != AMD_IP_BLOCK_TYPE_SMC) {
1540 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1541 AMD_CG_STATE_UNGATE);
1543 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1544 adev->ip_blocks[i].version->funcs->name, r);
1547 /* XXX handle errors */
1548 r = adev->ip_blocks[i].version->funcs->suspend(adev);
1549 /* XXX handle errors */
1551 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1552 adev->ip_blocks[i].version->funcs->name, r);
1556 if (amdgpu_sriov_vf(adev))
1557 amdgpu_virt_release_full_gpu(adev, false);
1562 static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
1566 static enum amd_ip_block_type ip_order[] = {
1567 AMD_IP_BLOCK_TYPE_GMC,
1568 AMD_IP_BLOCK_TYPE_COMMON,
1569 AMD_IP_BLOCK_TYPE_IH,
1572 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1574 struct amdgpu_ip_block *block;
1576 for (j = 0; j < adev->num_ip_blocks; j++) {
1577 block = &adev->ip_blocks[j];
1579 if (block->version->type != ip_order[i] ||
1580 !block->status.valid)
1583 r = block->version->funcs->hw_init(adev);
1584 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
1591 static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
1595 static enum amd_ip_block_type ip_order[] = {
1596 AMD_IP_BLOCK_TYPE_SMC,
1597 AMD_IP_BLOCK_TYPE_PSP,
1598 AMD_IP_BLOCK_TYPE_DCE,
1599 AMD_IP_BLOCK_TYPE_GFX,
1600 AMD_IP_BLOCK_TYPE_SDMA,
1601 AMD_IP_BLOCK_TYPE_UVD,
1602 AMD_IP_BLOCK_TYPE_VCE
1605 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1607 struct amdgpu_ip_block *block;
1609 for (j = 0; j < adev->num_ip_blocks; j++) {
1610 block = &adev->ip_blocks[j];
1612 if (block->version->type != ip_order[i] ||
1613 !block->status.valid)
1616 r = block->version->funcs->hw_init(adev);
1617 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
1624 static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
1628 for (i = 0; i < adev->num_ip_blocks; i++) {
1629 if (!adev->ip_blocks[i].status.valid)
1631 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1632 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1633 adev->ip_blocks[i].version->type ==
1634 AMD_IP_BLOCK_TYPE_IH) {
1635 r = adev->ip_blocks[i].version->funcs->resume(adev);
1637 DRM_ERROR("resume of IP block <%s> failed %d\n",
1638 adev->ip_blocks[i].version->funcs->name, r);
1647 static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
1651 for (i = 0; i < adev->num_ip_blocks; i++) {
1652 if (!adev->ip_blocks[i].status.valid)
1654 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1655 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1656 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
1658 r = adev->ip_blocks[i].version->funcs->resume(adev);
1660 DRM_ERROR("resume of IP block <%s> failed %d\n",
1661 adev->ip_blocks[i].version->funcs->name, r);
1669 static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
1673 r = amdgpu_device_ip_resume_phase1(adev);
1676 r = amdgpu_device_ip_resume_phase2(adev);
1681 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
1683 if (amdgpu_sriov_vf(adev)) {
1684 if (adev->is_atom_fw) {
1685 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
1686 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1688 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
1689 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1692 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
1693 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
1697 bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
1699 switch (asic_type) {
1700 #if defined(CONFIG_DRM_AMD_DC)
1706 case CHIP_POLARIS11:
1707 case CHIP_POLARIS10:
1708 case CHIP_POLARIS12:
1711 #if defined(CONFIG_DRM_AMD_DC_PRE_VEGA)
1712 return amdgpu_dc != 0;
1716 return amdgpu_dc > 0;
1718 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
1721 return amdgpu_dc != 0;
1729 * amdgpu_device_has_dc_support - check if dc is supported
1731 * @adev: amdgpu_device_pointer
1733 * Returns true for supported, false for not supported
1735 bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
1737 if (amdgpu_sriov_vf(adev))
1740 return amdgpu_device_asic_has_dc_support(adev->asic_type);
1744 * amdgpu_device_init - initialize the driver
1746 * @adev: amdgpu_device pointer
1747 * @pdev: drm dev pointer
1748 * @pdev: pci dev pointer
1749 * @flags: driver flags
1751 * Initializes the driver info and hw (all asics).
1752 * Returns 0 for success or an error on failure.
1753 * Called at driver startup.
1755 int amdgpu_device_init(struct amdgpu_device *adev,
1756 struct drm_device *ddev,
1757 struct pci_dev *pdev,
1761 bool runtime = false;
1764 adev->shutdown = false;
1765 adev->dev = &pdev->dev;
1768 adev->flags = flags;
1769 adev->asic_type = flags & AMD_ASIC_MASK;
1770 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
1771 adev->mc.gart_size = 512 * 1024 * 1024;
1772 adev->accel_working = false;
1773 adev->num_rings = 0;
1774 adev->mman.buffer_funcs = NULL;
1775 adev->mman.buffer_funcs_ring = NULL;
1776 adev->vm_manager.vm_pte_funcs = NULL;
1777 adev->vm_manager.vm_pte_num_rings = 0;
1778 adev->gart.gart_funcs = NULL;
1779 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
1780 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
1782 adev->smc_rreg = &amdgpu_invalid_rreg;
1783 adev->smc_wreg = &amdgpu_invalid_wreg;
1784 adev->pcie_rreg = &amdgpu_invalid_rreg;
1785 adev->pcie_wreg = &amdgpu_invalid_wreg;
1786 adev->pciep_rreg = &amdgpu_invalid_rreg;
1787 adev->pciep_wreg = &amdgpu_invalid_wreg;
1788 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1789 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1790 adev->didt_rreg = &amdgpu_invalid_rreg;
1791 adev->didt_wreg = &amdgpu_invalid_wreg;
1792 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
1793 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
1794 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1795 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1797 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1798 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1799 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
1801 /* mutex initialization are all done here so we
1802 * can recall function without having locking issues */
1803 atomic_set(&adev->irq.ih.lock, 0);
1804 mutex_init(&adev->firmware.mutex);
1805 mutex_init(&adev->pm.mutex);
1806 mutex_init(&adev->gfx.gpu_clock_mutex);
1807 mutex_init(&adev->srbm_mutex);
1808 mutex_init(&adev->gfx.pipe_reserve_mutex);
1809 mutex_init(&adev->grbm_idx_mutex);
1810 mutex_init(&adev->mn_lock);
1811 mutex_init(&adev->virt.vf_errors.lock);
1812 hash_init(adev->mn_hash);
1813 mutex_init(&adev->lock_reset);
1815 amdgpu_device_check_arguments(adev);
1817 spin_lock_init(&adev->mmio_idx_lock);
1818 spin_lock_init(&adev->smc_idx_lock);
1819 spin_lock_init(&adev->pcie_idx_lock);
1820 spin_lock_init(&adev->uvd_ctx_idx_lock);
1821 spin_lock_init(&adev->didt_idx_lock);
1822 spin_lock_init(&adev->gc_cac_idx_lock);
1823 spin_lock_init(&adev->se_cac_idx_lock);
1824 spin_lock_init(&adev->audio_endpt_idx_lock);
1825 spin_lock_init(&adev->mm_stats.lock);
1827 INIT_LIST_HEAD(&adev->shadow_list);
1828 mutex_init(&adev->shadow_list_lock);
1830 INIT_LIST_HEAD(&adev->ring_lru_list);
1831 spin_lock_init(&adev->ring_lru_list_lock);
1833 INIT_DELAYED_WORK(&adev->late_init_work,
1834 amdgpu_device_ip_late_init_func_handler);
1836 /* Registers mapping */
1837 /* TODO: block userspace mapping of io register */
1838 if (adev->asic_type >= CHIP_BONAIRE) {
1839 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
1840 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
1842 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
1843 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
1846 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
1847 if (adev->rmmio == NULL) {
1850 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
1851 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
1853 /* doorbell bar mapping */
1854 amdgpu_device_doorbell_init(adev);
1856 /* io port mapping */
1857 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1858 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
1859 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
1860 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
1864 if (adev->rio_mem == NULL)
1865 DRM_INFO("PCI I/O BAR is not found.\n");
1867 /* early init functions */
1868 r = amdgpu_device_ip_early_init(adev);
1872 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
1873 /* this will fail for cards that aren't VGA class devices, just
1875 vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
1877 if (amdgpu_runtime_pm == 1)
1879 if (amdgpu_device_is_px(ddev))
1881 if (!pci_is_thunderbolt_attached(adev->pdev))
1882 vga_switcheroo_register_client(adev->pdev,
1883 &amdgpu_switcheroo_ops, runtime);
1885 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
1888 if (!amdgpu_get_bios(adev)) {
1893 r = amdgpu_atombios_init(adev);
1895 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
1896 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
1900 /* detect if we are with an SRIOV vbios */
1901 amdgpu_device_detect_sriov_bios(adev);
1903 /* Post card if necessary */
1904 if (amdgpu_device_need_post(adev)) {
1906 dev_err(adev->dev, "no vBIOS found\n");
1910 DRM_INFO("GPU posting now...\n");
1911 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
1913 dev_err(adev->dev, "gpu post error!\n");
1918 if (adev->is_atom_fw) {
1919 /* Initialize clocks */
1920 r = amdgpu_atomfirmware_get_clock_info(adev);
1922 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
1923 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
1927 /* Initialize clocks */
1928 r = amdgpu_atombios_get_clock_info(adev);
1930 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
1931 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
1934 /* init i2c buses */
1935 if (!amdgpu_device_has_dc_support(adev))
1936 amdgpu_atombios_i2c_init(adev);
1940 r = amdgpu_fence_driver_init(adev);
1942 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
1943 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
1947 /* init the mode config */
1948 drm_mode_config_init(adev->ddev);
1950 r = amdgpu_device_ip_init(adev);
1952 /* failed in exclusive mode due to timeout */
1953 if (amdgpu_sriov_vf(adev) &&
1954 !amdgpu_sriov_runtime(adev) &&
1955 amdgpu_virt_mmio_blocked(adev) &&
1956 !amdgpu_virt_wait_reset(adev)) {
1957 dev_err(adev->dev, "VF exclusive mode timeout\n");
1958 /* Don't send request since VF is inactive. */
1959 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
1960 adev->virt.ops = NULL;
1964 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
1965 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
1966 amdgpu_device_ip_fini(adev);
1970 adev->accel_working = true;
1972 amdgpu_vm_check_compute_bug(adev);
1974 /* Initialize the buffer migration limit. */
1975 if (amdgpu_moverate >= 0)
1976 max_MBps = amdgpu_moverate;
1978 max_MBps = 8; /* Allow 8 MB/s. */
1979 /* Get a log2 for easy divisions. */
1980 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
1982 r = amdgpu_ib_pool_init(adev);
1984 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
1985 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
1989 r = amdgpu_ib_ring_tests(adev);
1991 DRM_ERROR("ib ring test failed (%d).\n", r);
1993 if (amdgpu_sriov_vf(adev))
1994 amdgpu_virt_init_data_exchange(adev);
1996 amdgpu_fbdev_init(adev);
1998 r = amdgpu_pm_sysfs_init(adev);
2000 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2002 r = amdgpu_debugfs_gem_init(adev);
2004 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
2006 r = amdgpu_debugfs_regs_init(adev);
2008 DRM_ERROR("registering register debugfs failed (%d).\n", r);
2010 r = amdgpu_debugfs_firmware_init(adev);
2012 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
2014 r = amdgpu_debugfs_init(adev);
2016 DRM_ERROR("Creating debugfs files failed (%d).\n", r);
2018 if ((amdgpu_testing & 1)) {
2019 if (adev->accel_working)
2020 amdgpu_test_moves(adev);
2022 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2024 if (amdgpu_benchmarking) {
2025 if (adev->accel_working)
2026 amdgpu_benchmark(adev, amdgpu_benchmarking);
2028 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2031 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2032 * explicit gating rather than handling it automatically.
2034 r = amdgpu_device_ip_late_init(adev);
2036 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
2037 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
2044 amdgpu_vf_error_trans_all(adev);
2046 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2052 * amdgpu_device_fini - tear down the driver
2054 * @adev: amdgpu_device pointer
2056 * Tear down the driver info (all asics).
2057 * Called at driver shutdown.
2059 void amdgpu_device_fini(struct amdgpu_device *adev)
2063 DRM_INFO("amdgpu: finishing device.\n");
2064 adev->shutdown = true;
2065 if (adev->mode_info.mode_config_initialized)
2066 drm_crtc_force_disable_all(adev->ddev);
2068 amdgpu_ib_pool_fini(adev);
2069 amdgpu_fence_driver_fini(adev);
2070 amdgpu_fbdev_fini(adev);
2071 r = amdgpu_device_ip_fini(adev);
2072 if (adev->firmware.gpu_info_fw) {
2073 release_firmware(adev->firmware.gpu_info_fw);
2074 adev->firmware.gpu_info_fw = NULL;
2076 adev->accel_working = false;
2077 cancel_delayed_work_sync(&adev->late_init_work);
2078 /* free i2c buses */
2079 if (!amdgpu_device_has_dc_support(adev))
2080 amdgpu_i2c_fini(adev);
2081 amdgpu_atombios_fini(adev);
2084 if (!pci_is_thunderbolt_attached(adev->pdev))
2085 vga_switcheroo_unregister_client(adev->pdev);
2086 if (adev->flags & AMD_IS_PX)
2087 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2088 vga_client_register(adev->pdev, NULL, NULL, NULL);
2090 pci_iounmap(adev->pdev, adev->rio_mem);
2091 adev->rio_mem = NULL;
2092 iounmap(adev->rmmio);
2094 amdgpu_device_doorbell_fini(adev);
2095 amdgpu_pm_sysfs_fini(adev);
2096 amdgpu_debugfs_regs_cleanup(adev);
2104 * amdgpu_device_suspend - initiate device suspend
2106 * @pdev: drm dev pointer
2107 * @state: suspend state
2109 * Puts the hw in the suspend state (all asics).
2110 * Returns 0 for success or an error on failure.
2111 * Called at driver suspend.
2113 int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
2115 struct amdgpu_device *adev;
2116 struct drm_crtc *crtc;
2117 struct drm_connector *connector;
2120 if (dev == NULL || dev->dev_private == NULL) {
2124 adev = dev->dev_private;
2126 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2129 drm_kms_helper_poll_disable(dev);
2131 if (!amdgpu_device_has_dc_support(adev)) {
2132 /* turn off display hw */
2133 drm_modeset_lock_all(dev);
2134 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2135 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2137 drm_modeset_unlock_all(dev);
2140 amdgpu_amdkfd_suspend(adev);
2142 /* unpin the front buffers and cursors */
2143 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2144 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2145 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2146 struct amdgpu_bo *robj;
2148 if (amdgpu_crtc->cursor_bo) {
2149 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2150 r = amdgpu_bo_reserve(aobj, true);
2152 amdgpu_bo_unpin(aobj);
2153 amdgpu_bo_unreserve(aobj);
2157 if (rfb == NULL || rfb->obj == NULL) {
2160 robj = gem_to_amdgpu_bo(rfb->obj);
2161 /* don't unpin kernel fb objects */
2162 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
2163 r = amdgpu_bo_reserve(robj, true);
2165 amdgpu_bo_unpin(robj);
2166 amdgpu_bo_unreserve(robj);
2170 /* evict vram memory */
2171 amdgpu_bo_evict_vram(adev);
2173 amdgpu_fence_driver_suspend(adev);
2175 r = amdgpu_device_ip_suspend(adev);
2177 /* evict remaining vram memory
2178 * This second call to evict vram is to evict the gart page table
2181 amdgpu_bo_evict_vram(adev);
2183 pci_save_state(dev->pdev);
2185 /* Shut down the device */
2186 pci_disable_device(dev->pdev);
2187 pci_set_power_state(dev->pdev, PCI_D3hot);
2189 r = amdgpu_asic_reset(adev);
2191 DRM_ERROR("amdgpu asic reset failed\n");
2196 amdgpu_fbdev_set_suspend(adev, 1);
2203 * amdgpu_device_resume - initiate device resume
2205 * @pdev: drm dev pointer
2207 * Bring the hw back to operating state (all asics).
2208 * Returns 0 for success or an error on failure.
2209 * Called at driver resume.
2211 int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
2213 struct drm_connector *connector;
2214 struct amdgpu_device *adev = dev->dev_private;
2215 struct drm_crtc *crtc;
2218 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2225 pci_set_power_state(dev->pdev, PCI_D0);
2226 pci_restore_state(dev->pdev);
2227 r = pci_enable_device(dev->pdev);
2233 if (amdgpu_device_need_post(adev)) {
2234 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2236 DRM_ERROR("amdgpu asic init failed\n");
2239 r = amdgpu_device_ip_resume(adev);
2241 DRM_ERROR("amdgpu_device_ip_resume failed (%d).\n", r);
2244 amdgpu_fence_driver_resume(adev);
2247 r = amdgpu_ib_ring_tests(adev);
2249 DRM_ERROR("ib ring test failed (%d).\n", r);
2252 r = amdgpu_device_ip_late_init(adev);
2257 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2258 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2260 if (amdgpu_crtc->cursor_bo) {
2261 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2262 r = amdgpu_bo_reserve(aobj, true);
2264 r = amdgpu_bo_pin(aobj,
2265 AMDGPU_GEM_DOMAIN_VRAM,
2266 &amdgpu_crtc->cursor_addr);
2268 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2269 amdgpu_bo_unreserve(aobj);
2273 r = amdgpu_amdkfd_resume(adev);
2277 /* blat the mode back in */
2279 if (!amdgpu_device_has_dc_support(adev)) {
2281 drm_helper_resume_force_mode(dev);
2283 /* turn on display hw */
2284 drm_modeset_lock_all(dev);
2285 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2286 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2288 drm_modeset_unlock_all(dev);
2291 * There is no equivalent atomic helper to turn on
2292 * display, so we defined our own function for this,
2293 * once suspend resume is supported by the atomic
2294 * framework this will be reworked
2296 amdgpu_dm_display_resume(adev);
2300 drm_kms_helper_poll_enable(dev);
2303 * Most of the connector probing functions try to acquire runtime pm
2304 * refs to ensure that the GPU is powered on when connector polling is
2305 * performed. Since we're calling this from a runtime PM callback,
2306 * trying to acquire rpm refs will cause us to deadlock.
2308 * Since we're guaranteed to be holding the rpm lock, it's safe to
2309 * temporarily disable the rpm helpers so this doesn't deadlock us.
2312 dev->dev->power.disable_depth++;
2314 if (!amdgpu_device_has_dc_support(adev))
2315 drm_helper_hpd_irq_event(dev);
2317 drm_kms_helper_hotplug_event(dev);
2319 dev->dev->power.disable_depth--;
2323 amdgpu_fbdev_set_suspend(adev, 0);
2332 static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
2335 bool asic_hang = false;
2337 if (amdgpu_sriov_vf(adev))
2340 for (i = 0; i < adev->num_ip_blocks; i++) {
2341 if (!adev->ip_blocks[i].status.valid)
2343 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2344 adev->ip_blocks[i].status.hang =
2345 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2346 if (adev->ip_blocks[i].status.hang) {
2347 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
2354 static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
2358 for (i = 0; i < adev->num_ip_blocks; i++) {
2359 if (!adev->ip_blocks[i].status.valid)
2361 if (adev->ip_blocks[i].status.hang &&
2362 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2363 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
2372 static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
2376 for (i = 0; i < adev->num_ip_blocks; i++) {
2377 if (!adev->ip_blocks[i].status.valid)
2379 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2380 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2381 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2382 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
2383 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
2384 if (adev->ip_blocks[i].status.hang) {
2385 DRM_INFO("Some block need full reset!\n");
2393 static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
2397 for (i = 0; i < adev->num_ip_blocks; i++) {
2398 if (!adev->ip_blocks[i].status.valid)
2400 if (adev->ip_blocks[i].status.hang &&
2401 adev->ip_blocks[i].version->funcs->soft_reset) {
2402 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
2411 static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
2415 for (i = 0; i < adev->num_ip_blocks; i++) {
2416 if (!adev->ip_blocks[i].status.valid)
2418 if (adev->ip_blocks[i].status.hang &&
2419 adev->ip_blocks[i].version->funcs->post_soft_reset)
2420 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
2428 static int amdgpu_device_recover_vram_from_shadow(struct amdgpu_device *adev,
2429 struct amdgpu_ring *ring,
2430 struct amdgpu_bo *bo,
2431 struct dma_fence **fence)
2439 r = amdgpu_bo_reserve(bo, true);
2442 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2443 /* if bo has been evicted, then no need to recover */
2444 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
2445 r = amdgpu_bo_validate(bo->shadow);
2447 DRM_ERROR("bo validate failed!\n");
2451 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
2454 DRM_ERROR("recover page table failed!\n");
2459 amdgpu_bo_unreserve(bo);
2464 * amdgpu_device_reset - reset ASIC/GPU for bare-metal or passthrough
2466 * @adev: amdgpu device pointer
2467 * @reset_flags: output param tells caller the reset result
2469 * attempt to do soft-reset or full-reset and reinitialize Asic
2470 * return 0 means successed otherwise failed
2472 static int amdgpu_device_reset(struct amdgpu_device *adev,
2473 uint64_t* reset_flags)
2475 bool need_full_reset, vram_lost = 0;
2478 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
2480 if (!need_full_reset) {
2481 amdgpu_device_ip_pre_soft_reset(adev);
2482 r = amdgpu_device_ip_soft_reset(adev);
2483 amdgpu_device_ip_post_soft_reset(adev);
2484 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
2485 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2486 need_full_reset = true;
2491 if (need_full_reset) {
2492 r = amdgpu_device_ip_suspend(adev);
2495 r = amdgpu_asic_reset(adev);
2497 amdgpu_atom_asic_init(adev->mode_info.atom_context);
2500 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
2501 r = amdgpu_device_ip_resume_phase1(adev);
2505 vram_lost = amdgpu_device_check_vram_lost(adev);
2507 DRM_ERROR("VRAM is lost!\n");
2508 atomic_inc(&adev->vram_lost_counter);
2511 r = amdgpu_gtt_mgr_recover(
2512 &adev->mman.bdev.man[TTM_PL_TT]);
2516 r = amdgpu_device_ip_resume_phase2(adev);
2521 amdgpu_device_fill_reset_magic(adev);
2527 amdgpu_irq_gpu_reset_resume_helper(adev);
2528 r = amdgpu_ib_ring_tests(adev);
2530 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
2531 r = amdgpu_device_ip_suspend(adev);
2532 need_full_reset = true;
2539 (*reset_flags) |= AMDGPU_RESET_INFO_VRAM_LOST;
2541 if (need_full_reset)
2542 (*reset_flags) |= AMDGPU_RESET_INFO_FULLRESET;
2549 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
2551 * @adev: amdgpu device pointer
2552 * @reset_flags: output param tells caller the reset result
2554 * do VF FLR and reinitialize Asic
2555 * return 0 means successed otherwise failed
2557 static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
2558 uint64_t *reset_flags,
2559 bool from_hypervisor)
2563 if (from_hypervisor)
2564 r = amdgpu_virt_request_full_gpu(adev, true);
2566 r = amdgpu_virt_reset_gpu(adev);
2570 /* Resume IP prior to SMC */
2571 r = amdgpu_device_ip_reinit_early_sriov(adev);
2575 /* we need recover gart prior to run SMC/CP/SDMA resume */
2576 amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
2578 /* now we are okay to resume SMC/CP/SDMA */
2579 r = amdgpu_device_ip_reinit_late_sriov(adev);
2583 amdgpu_irq_gpu_reset_resume_helper(adev);
2584 r = amdgpu_ib_ring_tests(adev);
2586 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2589 /* release full control of GPU after ib test */
2590 amdgpu_virt_release_full_gpu(adev, true);
2593 if (adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
2594 (*reset_flags) |= AMDGPU_RESET_INFO_VRAM_LOST;
2595 atomic_inc(&adev->vram_lost_counter);
2598 /* VF FLR or hotlink reset is always full-reset */
2599 (*reset_flags) |= AMDGPU_RESET_INFO_FULLRESET;
2606 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
2608 * @adev: amdgpu device pointer
2609 * @job: which job trigger hang
2610 * @force forces reset regardless of amdgpu_gpu_recovery
2612 * Attempt to reset the GPU if it has hung (all asics).
2613 * Returns 0 for success or an error on failure.
2615 int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
2616 struct amdgpu_job *job, bool force)
2618 struct drm_atomic_state *state = NULL;
2619 uint64_t reset_flags = 0;
2622 if (!amdgpu_device_ip_check_soft_reset(adev)) {
2623 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2627 if (!force && (amdgpu_gpu_recovery == 0 ||
2628 (amdgpu_gpu_recovery == -1 && !amdgpu_sriov_vf(adev)))) {
2629 DRM_INFO("GPU recovery disabled.\n");
2633 dev_info(adev->dev, "GPU reset begin!\n");
2635 mutex_lock(&adev->lock_reset);
2636 atomic_inc(&adev->gpu_reset_counter);
2637 adev->in_gpu_reset = 1;
2640 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2641 /* store modesetting */
2642 if (amdgpu_device_has_dc_support(adev))
2643 state = drm_atomic_helper_suspend(adev->ddev);
2645 /* block scheduler */
2646 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2647 struct amdgpu_ring *ring = adev->rings[i];
2649 if (!ring || !ring->sched.thread)
2652 /* only focus on the ring hit timeout if &job not NULL */
2653 if (job && job->ring->idx != i)
2656 kthread_park(ring->sched.thread);
2657 drm_sched_hw_job_reset(&ring->sched, &job->base);
2659 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2660 amdgpu_fence_driver_force_completion(ring);
2663 if (amdgpu_sriov_vf(adev))
2664 r = amdgpu_device_reset_sriov(adev, &reset_flags, job ? false : true);
2666 r = amdgpu_device_reset(adev, &reset_flags);
2669 if (((reset_flags & AMDGPU_RESET_INFO_FULLRESET) && !(adev->flags & AMD_IS_APU)) ||
2670 (reset_flags & AMDGPU_RESET_INFO_VRAM_LOST)) {
2671 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2672 struct amdgpu_bo *bo, *tmp;
2673 struct dma_fence *fence = NULL, *next = NULL;
2675 DRM_INFO("recover vram bo from shadow\n");
2676 mutex_lock(&adev->shadow_list_lock);
2677 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2679 amdgpu_device_recover_vram_from_shadow(adev, ring, bo, &next);
2681 r = dma_fence_wait(fence, false);
2683 WARN(r, "recovery from shadow isn't completed\n");
2688 dma_fence_put(fence);
2691 mutex_unlock(&adev->shadow_list_lock);
2693 r = dma_fence_wait(fence, false);
2695 WARN(r, "recovery from shadow isn't completed\n");
2697 dma_fence_put(fence);
2700 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2701 struct amdgpu_ring *ring = adev->rings[i];
2703 if (!ring || !ring->sched.thread)
2706 /* only focus on the ring hit timeout if &job not NULL */
2707 if (job && job->ring->idx != i)
2710 drm_sched_job_recovery(&ring->sched);
2711 kthread_unpark(ring->sched.thread);
2714 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2715 struct amdgpu_ring *ring = adev->rings[i];
2717 if (!ring || !ring->sched.thread)
2720 /* only focus on the ring hit timeout if &job not NULL */
2721 if (job && job->ring->idx != i)
2724 kthread_unpark(adev->rings[i]->sched.thread);
2728 if (amdgpu_device_has_dc_support(adev)) {
2729 if (drm_atomic_helper_resume(adev->ddev, state))
2730 dev_info(adev->dev, "drm resume failed:%d\n", r);
2731 amdgpu_dm_display_resume(adev);
2733 drm_helper_resume_force_mode(adev->ddev);
2736 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2739 /* bad news, how to tell it to userspace ? */
2740 dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
2741 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
2743 dev_info(adev->dev, "GPU reset(%d) successed!\n",atomic_read(&adev->gpu_reset_counter));
2746 amdgpu_vf_error_trans_all(adev);
2747 adev->in_gpu_reset = 0;
2748 mutex_unlock(&adev->lock_reset);
2752 void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
2757 if (amdgpu_pcie_gen_cap)
2758 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2760 if (amdgpu_pcie_lane_cap)
2761 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2763 /* covers APUs as well */
2764 if (pci_is_root_bus(adev->pdev->bus)) {
2765 if (adev->pm.pcie_gen_mask == 0)
2766 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2767 if (adev->pm.pcie_mlw_mask == 0)
2768 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
2772 if (adev->pm.pcie_gen_mask == 0) {
2773 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2775 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2776 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2777 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2779 if (mask & DRM_PCIE_SPEED_25)
2780 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2781 if (mask & DRM_PCIE_SPEED_50)
2782 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2783 if (mask & DRM_PCIE_SPEED_80)
2784 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2786 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2789 if (adev->pm.pcie_mlw_mask == 0) {
2790 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2794 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
2795 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2796 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2797 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2798 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2799 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2800 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2803 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2804 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2805 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2806 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2807 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2808 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2811 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2812 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2813 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2814 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2815 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2818 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2819 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2820 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2821 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2824 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2825 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2826 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2829 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2830 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2833 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
2839 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;