• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2008 Advanced Micro Devices, Inc.
3  * Copyright 2008 Red Hat Inc.
4  * Copyright 2009 Jerome Glisse.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
23  *
24  * Authors: Dave Airlie
25  *          Alex Deucher
26  *          Jerome Glisse
27  */
28 #include <linux/kthread.h>
29 #include <linux/console.h>
30 #include <linux/slab.h>
31 #include <linux/debugfs.h>
32 #include <drm/drmP.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>
38 #include "amdgpu.h"
39 #include "amdgpu_trace.h"
40 #include "amdgpu_i2c.h"
41 #include "atom.h"
42 #include "amdgpu_atombios.h"
43 #include "amdgpu_atomfirmware.h"
44 #include "amd_pcie.h"
45 #ifdef CONFIG_DRM_AMDGPU_SI
46 #include "si.h"
47 #endif
48 #ifdef CONFIG_DRM_AMDGPU_CIK
49 #include "cik.h"
50 #endif
51 #include "vi.h"
52 #include "soc15.h"
53 #include "bif/bif_4_1_d.h"
54 #include <linux/pci.h>
55 #include <linux/firmware.h>
56 #include "amdgpu_vf_error.h"
57 
58 #include "amdgpu_amdkfd.h"
59 
60 MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
61 MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
62 
63 #define AMDGPU_RESUME_MS		2000
64 
65 static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
66 static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
67 static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev);
68 
69 static const char *amdgpu_asic_name[] = {
70 	"TAHITI",
71 	"PITCAIRN",
72 	"VERDE",
73 	"OLAND",
74 	"HAINAN",
75 	"BONAIRE",
76 	"KAVERI",
77 	"KABINI",
78 	"HAWAII",
79 	"MULLINS",
80 	"TOPAZ",
81 	"TONGA",
82 	"FIJI",
83 	"CARRIZO",
84 	"STONEY",
85 	"POLARIS10",
86 	"POLARIS11",
87 	"POLARIS12",
88 	"VEGA10",
89 	"RAVEN",
90 	"LAST",
91 };
92 
amdgpu_device_is_px(struct drm_device * dev)93 bool amdgpu_device_is_px(struct drm_device *dev)
94 {
95 	struct amdgpu_device *adev = dev->dev_private;
96 
97 	if (adev->flags & AMD_IS_PX)
98 		return true;
99 	return false;
100 }
101 
102 /*
103  * MMIO register access helper functions.
104  */
amdgpu_mm_rreg(struct amdgpu_device * adev,uint32_t reg,uint32_t acc_flags)105 uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
106 			uint32_t acc_flags)
107 {
108 	uint32_t ret;
109 
110 	if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
111 		BUG_ON(in_interrupt());
112 		return amdgpu_virt_kiq_rreg(adev, reg);
113 	}
114 
115 	if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
116 		ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
117 	else {
118 		unsigned long flags;
119 
120 		spin_lock_irqsave(&adev->mmio_idx_lock, flags);
121 		writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
122 		ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
123 		spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
124 	}
125 	trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
126 	return ret;
127 }
128 
amdgpu_mm_wreg(struct amdgpu_device * adev,uint32_t reg,uint32_t v,uint32_t acc_flags)129 void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
130 		    uint32_t acc_flags)
131 {
132 	trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
133 
134 	if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
135 		adev->last_mm_index = v;
136 	}
137 
138 	if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
139 		BUG_ON(in_interrupt());
140 		return amdgpu_virt_kiq_wreg(adev, reg, v);
141 	}
142 
143 	if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
144 		writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
145 	else {
146 		unsigned long flags;
147 
148 		spin_lock_irqsave(&adev->mmio_idx_lock, flags);
149 		writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
150 		writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
151 		spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
152 	}
153 
154 	if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
155 		udelay(500);
156 	}
157 }
158 
amdgpu_io_rreg(struct amdgpu_device * adev,u32 reg)159 u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
160 {
161 	if ((reg * 4) < adev->rio_mem_size)
162 		return ioread32(adev->rio_mem + (reg * 4));
163 	else {
164 		iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
165 		return ioread32(adev->rio_mem + (mmMM_DATA * 4));
166 	}
167 }
168 
amdgpu_io_wreg(struct amdgpu_device * adev,u32 reg,u32 v)169 void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
170 {
171 	if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
172 		adev->last_mm_index = v;
173 	}
174 
175 	if ((reg * 4) < adev->rio_mem_size)
176 		iowrite32(v, adev->rio_mem + (reg * 4));
177 	else {
178 		iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
179 		iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
180 	}
181 
182 	if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
183 		udelay(500);
184 	}
185 }
186 
187 /**
188  * amdgpu_mm_rdoorbell - read a doorbell dword
189  *
190  * @adev: amdgpu_device pointer
191  * @index: doorbell index
192  *
193  * Returns the value in the doorbell aperture at the
194  * requested doorbell index (CIK).
195  */
amdgpu_mm_rdoorbell(struct amdgpu_device * adev,u32 index)196 u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
197 {
198 	if (index < adev->doorbell.num_doorbells) {
199 		return readl(adev->doorbell.ptr + index);
200 	} else {
201 		DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
202 		return 0;
203 	}
204 }
205 
206 /**
207  * amdgpu_mm_wdoorbell - write a doorbell dword
208  *
209  * @adev: amdgpu_device pointer
210  * @index: doorbell index
211  * @v: value to write
212  *
213  * Writes @v to the doorbell aperture at the
214  * requested doorbell index (CIK).
215  */
amdgpu_mm_wdoorbell(struct amdgpu_device * adev,u32 index,u32 v)216 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
217 {
218 	if (index < adev->doorbell.num_doorbells) {
219 		writel(v, adev->doorbell.ptr + index);
220 	} else {
221 		DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
222 	}
223 }
224 
225 /**
226  * amdgpu_mm_rdoorbell64 - read a doorbell Qword
227  *
228  * @adev: amdgpu_device pointer
229  * @index: doorbell index
230  *
231  * Returns the value in the doorbell aperture at the
232  * requested doorbell index (VEGA10+).
233  */
amdgpu_mm_rdoorbell64(struct amdgpu_device * adev,u32 index)234 u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
235 {
236 	if (index < adev->doorbell.num_doorbells) {
237 		return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
238 	} else {
239 		DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
240 		return 0;
241 	}
242 }
243 
244 /**
245  * amdgpu_mm_wdoorbell64 - write a doorbell Qword
246  *
247  * @adev: amdgpu_device pointer
248  * @index: doorbell index
249  * @v: value to write
250  *
251  * Writes @v to the doorbell aperture at the
252  * requested doorbell index (VEGA10+).
253  */
amdgpu_mm_wdoorbell64(struct amdgpu_device * adev,u32 index,u64 v)254 void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
255 {
256 	if (index < adev->doorbell.num_doorbells) {
257 		atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
258 	} else {
259 		DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
260 	}
261 }
262 
263 /**
264  * amdgpu_invalid_rreg - dummy reg read function
265  *
266  * @adev: amdgpu device pointer
267  * @reg: offset of register
268  *
269  * Dummy register read function.  Used for register blocks
270  * that certain asics don't have (all asics).
271  * Returns the value in the register.
272  */
amdgpu_invalid_rreg(struct amdgpu_device * adev,uint32_t reg)273 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
274 {
275 	DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
276 	BUG();
277 	return 0;
278 }
279 
280 /**
281  * amdgpu_invalid_wreg - dummy reg write function
282  *
283  * @adev: amdgpu device pointer
284  * @reg: offset of register
285  * @v: value to write to the register
286  *
287  * Dummy register read function.  Used for register blocks
288  * that certain asics don't have (all asics).
289  */
amdgpu_invalid_wreg(struct amdgpu_device * adev,uint32_t reg,uint32_t v)290 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
291 {
292 	DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
293 		  reg, v);
294 	BUG();
295 }
296 
297 /**
298  * amdgpu_block_invalid_rreg - dummy reg read function
299  *
300  * @adev: amdgpu device pointer
301  * @block: offset of instance
302  * @reg: offset of register
303  *
304  * Dummy register read function.  Used for register blocks
305  * that certain asics don't have (all asics).
306  * Returns the value in the register.
307  */
amdgpu_block_invalid_rreg(struct amdgpu_device * adev,uint32_t block,uint32_t reg)308 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
309 					  uint32_t block, uint32_t reg)
310 {
311 	DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
312 		  reg, block);
313 	BUG();
314 	return 0;
315 }
316 
317 /**
318  * amdgpu_block_invalid_wreg - dummy reg write function
319  *
320  * @adev: amdgpu device pointer
321  * @block: offset of instance
322  * @reg: offset of register
323  * @v: value to write to the register
324  *
325  * Dummy register read function.  Used for register blocks
326  * that certain asics don't have (all asics).
327  */
amdgpu_block_invalid_wreg(struct amdgpu_device * adev,uint32_t block,uint32_t reg,uint32_t v)328 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
329 				      uint32_t block,
330 				      uint32_t reg, uint32_t v)
331 {
332 	DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
333 		  reg, block, v);
334 	BUG();
335 }
336 
amdgpu_vram_scratch_init(struct amdgpu_device * adev)337 static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
338 {
339 	return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
340 				       PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
341 				       &adev->vram_scratch.robj,
342 				       &adev->vram_scratch.gpu_addr,
343 				       (void **)&adev->vram_scratch.ptr);
344 }
345 
amdgpu_vram_scratch_fini(struct amdgpu_device * adev)346 static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
347 {
348 	amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
349 }
350 
351 /**
352  * amdgpu_program_register_sequence - program an array of registers.
353  *
354  * @adev: amdgpu_device pointer
355  * @registers: pointer to the register array
356  * @array_size: size of the register array
357  *
358  * Programs an array or registers with and and or masks.
359  * This is a helper for setting golden registers.
360  */
amdgpu_program_register_sequence(struct amdgpu_device * adev,const u32 * registers,const u32 array_size)361 void amdgpu_program_register_sequence(struct amdgpu_device *adev,
362 				      const u32 *registers,
363 				      const u32 array_size)
364 {
365 	u32 tmp, reg, and_mask, or_mask;
366 	int i;
367 
368 	if (array_size % 3)
369 		return;
370 
371 	for (i = 0; i < array_size; i +=3) {
372 		reg = registers[i + 0];
373 		and_mask = registers[i + 1];
374 		or_mask = registers[i + 2];
375 
376 		if (and_mask == 0xffffffff) {
377 			tmp = or_mask;
378 		} else {
379 			tmp = RREG32(reg);
380 			tmp &= ~and_mask;
381 			tmp |= or_mask;
382 		}
383 		WREG32(reg, tmp);
384 	}
385 }
386 
amdgpu_pci_config_reset(struct amdgpu_device * adev)387 void amdgpu_pci_config_reset(struct amdgpu_device *adev)
388 {
389 	pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
390 }
391 
392 /*
393  * GPU doorbell aperture helpers function.
394  */
395 /**
396  * amdgpu_doorbell_init - Init doorbell driver information.
397  *
398  * @adev: amdgpu_device pointer
399  *
400  * Init doorbell driver information (CIK)
401  * Returns 0 on success, error on failure.
402  */
amdgpu_doorbell_init(struct amdgpu_device * adev)403 static int amdgpu_doorbell_init(struct amdgpu_device *adev)
404 {
405 	/* doorbell bar mapping */
406 	adev->doorbell.base = pci_resource_start(adev->pdev, 2);
407 	adev->doorbell.size = pci_resource_len(adev->pdev, 2);
408 
409 	adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
410 					     AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
411 	if (adev->doorbell.num_doorbells == 0)
412 		return -EINVAL;
413 
414 	adev->doorbell.ptr = ioremap(adev->doorbell.base,
415 				     adev->doorbell.num_doorbells *
416 				     sizeof(u32));
417 	if (adev->doorbell.ptr == NULL)
418 		return -ENOMEM;
419 
420 	return 0;
421 }
422 
423 /**
424  * amdgpu_doorbell_fini - Tear down doorbell driver information.
425  *
426  * @adev: amdgpu_device pointer
427  *
428  * Tear down doorbell driver information (CIK)
429  */
amdgpu_doorbell_fini(struct amdgpu_device * adev)430 static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
431 {
432 	iounmap(adev->doorbell.ptr);
433 	adev->doorbell.ptr = NULL;
434 }
435 
436 /**
437  * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
438  *                                setup amdkfd
439  *
440  * @adev: amdgpu_device pointer
441  * @aperture_base: output returning doorbell aperture base physical address
442  * @aperture_size: output returning doorbell aperture size in bytes
443  * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
444  *
445  * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
446  * takes doorbells required for its own rings and reports the setup to amdkfd.
447  * amdgpu reserved doorbells are at the start of the doorbell aperture.
448  */
amdgpu_doorbell_get_kfd_info(struct amdgpu_device * adev,phys_addr_t * aperture_base,size_t * aperture_size,size_t * start_offset)449 void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
450 				phys_addr_t *aperture_base,
451 				size_t *aperture_size,
452 				size_t *start_offset)
453 {
454 	/*
455 	 * The first num_doorbells are used by amdgpu.
456 	 * amdkfd takes whatever's left in the aperture.
457 	 */
458 	if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
459 		*aperture_base = adev->doorbell.base;
460 		*aperture_size = adev->doorbell.size;
461 		*start_offset = adev->doorbell.num_doorbells * sizeof(u32);
462 	} else {
463 		*aperture_base = 0;
464 		*aperture_size = 0;
465 		*start_offset = 0;
466 	}
467 }
468 
469 /*
470  * amdgpu_wb_*()
471  * Writeback is the method by which the GPU updates special pages in memory
472  * with the status of certain GPU events (fences, ring pointers,etc.).
473  */
474 
475 /**
476  * amdgpu_wb_fini - Disable Writeback and free memory
477  *
478  * @adev: amdgpu_device pointer
479  *
480  * Disables Writeback and frees the Writeback memory (all asics).
481  * Used at driver shutdown.
482  */
amdgpu_wb_fini(struct amdgpu_device * adev)483 static void amdgpu_wb_fini(struct amdgpu_device *adev)
484 {
485 	if (adev->wb.wb_obj) {
486 		amdgpu_bo_free_kernel(&adev->wb.wb_obj,
487 				      &adev->wb.gpu_addr,
488 				      (void **)&adev->wb.wb);
489 		adev->wb.wb_obj = NULL;
490 	}
491 }
492 
493 /**
494  * amdgpu_wb_init- Init Writeback driver info and allocate memory
495  *
496  * @adev: amdgpu_device pointer
497  *
498  * Initializes writeback and allocates writeback memory (all asics).
499  * Used at driver startup.
500  * Returns 0 on success or an -error on failure.
501  */
amdgpu_wb_init(struct amdgpu_device * adev)502 static int amdgpu_wb_init(struct amdgpu_device *adev)
503 {
504 	int r;
505 
506 	if (adev->wb.wb_obj == NULL) {
507 		/* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
508 		r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
509 					    PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
510 					    &adev->wb.wb_obj, &adev->wb.gpu_addr,
511 					    (void **)&adev->wb.wb);
512 		if (r) {
513 			dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
514 			return r;
515 		}
516 
517 		adev->wb.num_wb = AMDGPU_MAX_WB;
518 		memset(&adev->wb.used, 0, sizeof(adev->wb.used));
519 
520 		/* clear wb memory */
521 		memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
522 	}
523 
524 	return 0;
525 }
526 
527 /**
528  * amdgpu_wb_get - Allocate a wb entry
529  *
530  * @adev: amdgpu_device pointer
531  * @wb: wb index
532  *
533  * Allocate a wb slot for use by the driver (all asics).
534  * Returns 0 on success or -EINVAL on failure.
535  */
amdgpu_wb_get(struct amdgpu_device * adev,u32 * wb)536 int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
537 {
538 	unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
539 
540 	if (offset < adev->wb.num_wb) {
541 		__set_bit(offset, adev->wb.used);
542 		*wb = offset * 8; /* convert to dw offset */
543 		return 0;
544 	} else {
545 		return -EINVAL;
546 	}
547 }
548 
549 /**
550  * amdgpu_wb_free - Free a wb entry
551  *
552  * @adev: amdgpu_device pointer
553  * @wb: wb index
554  *
555  * Free a wb slot allocated for use by the driver (all asics)
556  */
amdgpu_wb_free(struct amdgpu_device * adev,u32 wb)557 void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
558 {
559 	if (wb < adev->wb.num_wb)
560 		__clear_bit(wb, adev->wb.used);
561 }
562 
563 /**
564  * amdgpu_vram_location - try to find VRAM location
565  * @adev: amdgpu device structure holding all necessary informations
566  * @mc: memory controller structure holding memory informations
567  * @base: base address at which to put VRAM
568  *
569  * Function will try to place VRAM at base address provided
570  * as parameter (which is so far either PCI aperture address or
571  * for IGP TOM base address).
572  *
573  * If there is not enough space to fit the unvisible VRAM in the 32bits
574  * address space then we limit the VRAM size to the aperture.
575  *
576  * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
577  * this shouldn't be a problem as we are using the PCI aperture as a reference.
578  * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
579  * not IGP.
580  *
581  * Note: we use mc_vram_size as on some board we need to program the mc to
582  * cover the whole aperture even if VRAM size is inferior to aperture size
583  * Novell bug 204882 + along with lots of ubuntu ones
584  *
585  * Note: when limiting vram it's safe to overwritte real_vram_size because
586  * we are not in case where real_vram_size is inferior to mc_vram_size (ie
587  * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
588  * ones)
589  *
590  * Note: IGP TOM addr should be the same as the aperture addr, we don't
591  * explicitly check for that though.
592  *
593  * FIXME: when reducing VRAM size align new size on power of 2.
594  */
amdgpu_vram_location(struct amdgpu_device * adev,struct amdgpu_mc * mc,u64 base)595 void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
596 {
597 	uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
598 
599 	mc->vram_start = base;
600 	if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
601 		dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
602 		mc->real_vram_size = mc->aper_size;
603 		mc->mc_vram_size = mc->aper_size;
604 	}
605 	mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
606 	if (limit && limit < mc->real_vram_size)
607 		mc->real_vram_size = limit;
608 	dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
609 			mc->mc_vram_size >> 20, mc->vram_start,
610 			mc->vram_end, mc->real_vram_size >> 20);
611 }
612 
613 /**
614  * amdgpu_gart_location - try to find GTT location
615  * @adev: amdgpu device structure holding all necessary informations
616  * @mc: memory controller structure holding memory informations
617  *
618  * Function will place try to place GTT before or after VRAM.
619  *
620  * If GTT size is bigger than space left then we ajust GTT size.
621  * Thus function will never fails.
622  *
623  * FIXME: when reducing GTT size align new size on power of 2.
624  */
amdgpu_gart_location(struct amdgpu_device * adev,struct amdgpu_mc * mc)625 void amdgpu_gart_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
626 {
627 	u64 size_af, size_bf;
628 
629 	size_af = adev->mc.mc_mask - mc->vram_end;
630 	size_bf = mc->vram_start;
631 	if (size_bf > size_af) {
632 		if (mc->gart_size > size_bf) {
633 			dev_warn(adev->dev, "limiting GTT\n");
634 			mc->gart_size = size_bf;
635 		}
636 		mc->gart_start = 0;
637 	} else {
638 		if (mc->gart_size > size_af) {
639 			dev_warn(adev->dev, "limiting GTT\n");
640 			mc->gart_size = size_af;
641 		}
642 		mc->gart_start = mc->vram_end + 1;
643 	}
644 	mc->gart_end = mc->gart_start + mc->gart_size - 1;
645 	dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
646 			mc->gart_size >> 20, mc->gart_start, mc->gart_end);
647 }
648 
649 /*
650  * GPU helpers function.
651  */
652 /**
653  * amdgpu_need_post - check if the hw need post or not
654  *
655  * @adev: amdgpu_device pointer
656  *
657  * Check if the asic has been initialized (all asics) at driver startup
658  * or post is needed if  hw reset is performed.
659  * Returns true if need or false if not.
660  */
amdgpu_need_post(struct amdgpu_device * adev)661 bool amdgpu_need_post(struct amdgpu_device *adev)
662 {
663 	uint32_t reg;
664 
665 	if (adev->has_hw_reset) {
666 		adev->has_hw_reset = false;
667 		return true;
668 	}
669 
670 	/* bios scratch used on CIK+ */
671 	if (adev->asic_type >= CHIP_BONAIRE)
672 		return amdgpu_atombios_scratch_need_asic_init(adev);
673 
674 	/* check MEM_SIZE for older asics */
675 	reg = amdgpu_asic_get_config_memsize(adev);
676 
677 	if ((reg != 0) && (reg != 0xffffffff))
678 		return false;
679 
680 	return true;
681 
682 }
683 
amdgpu_vpost_needed(struct amdgpu_device * adev)684 static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
685 {
686 	if (amdgpu_sriov_vf(adev))
687 		return false;
688 
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
694 		 */
695 		if (adev->asic_type == CHIP_FIJI) {
696 			int err;
697 			uint32_t fw_ver;
698 			err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
699 			/* force vPost if error occured */
700 			if (err)
701 				return true;
702 
703 			fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
704 			if (fw_ver < 0x00160e00)
705 				return true;
706 		}
707 	}
708 	return amdgpu_need_post(adev);
709 }
710 
711 /**
712  * amdgpu_dummy_page_init - init dummy page used by the driver
713  *
714  * @adev: amdgpu_device pointer
715  *
716  * Allocate the dummy page used by the driver (all asics).
717  * This dummy page is used by the driver as a filler for gart entries
718  * when pages are taken out of the GART
719  * Returns 0 on sucess, -ENOMEM on failure.
720  */
amdgpu_dummy_page_init(struct amdgpu_device * adev)721 int amdgpu_dummy_page_init(struct amdgpu_device *adev)
722 {
723 	if (adev->dummy_page.page)
724 		return 0;
725 	adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
726 	if (adev->dummy_page.page == NULL)
727 		return -ENOMEM;
728 	adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
729 					0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
730 	if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
731 		dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
732 		__free_page(adev->dummy_page.page);
733 		adev->dummy_page.page = NULL;
734 		return -ENOMEM;
735 	}
736 	return 0;
737 }
738 
739 /**
740  * amdgpu_dummy_page_fini - free dummy page used by the driver
741  *
742  * @adev: amdgpu_device pointer
743  *
744  * Frees the dummy page used by the driver (all asics).
745  */
amdgpu_dummy_page_fini(struct amdgpu_device * adev)746 void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
747 {
748 	if (adev->dummy_page.page == NULL)
749 		return;
750 	pci_unmap_page(adev->pdev, adev->dummy_page.addr,
751 			PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
752 	__free_page(adev->dummy_page.page);
753 	adev->dummy_page.page = NULL;
754 }
755 
756 
757 /* ATOM accessor methods */
758 /*
759  * ATOM is an interpreted byte code stored in tables in the vbios.  The
760  * driver registers callbacks to access registers and the interpreter
761  * in the driver parses the tables and executes then to program specific
762  * actions (set display modes, asic init, etc.).  See amdgpu_atombios.c,
763  * atombios.h, and atom.c
764  */
765 
766 /**
767  * cail_pll_read - read PLL register
768  *
769  * @info: atom card_info pointer
770  * @reg: PLL register offset
771  *
772  * Provides a PLL register accessor for the atom interpreter (r4xx+).
773  * Returns the value of the PLL register.
774  */
cail_pll_read(struct card_info * info,uint32_t reg)775 static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
776 {
777 	return 0;
778 }
779 
780 /**
781  * cail_pll_write - write PLL register
782  *
783  * @info: atom card_info pointer
784  * @reg: PLL register offset
785  * @val: value to write to the pll register
786  *
787  * Provides a PLL register accessor for the atom interpreter (r4xx+).
788  */
cail_pll_write(struct card_info * info,uint32_t reg,uint32_t val)789 static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
790 {
791 
792 }
793 
794 /**
795  * cail_mc_read - read MC (Memory Controller) register
796  *
797  * @info: atom card_info pointer
798  * @reg: MC register offset
799  *
800  * Provides an MC register accessor for the atom interpreter (r4xx+).
801  * Returns the value of the MC register.
802  */
cail_mc_read(struct card_info * info,uint32_t reg)803 static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
804 {
805 	return 0;
806 }
807 
808 /**
809  * cail_mc_write - write MC (Memory Controller) register
810  *
811  * @info: atom card_info pointer
812  * @reg: MC register offset
813  * @val: value to write to the pll register
814  *
815  * Provides a MC register accessor for the atom interpreter (r4xx+).
816  */
cail_mc_write(struct card_info * info,uint32_t reg,uint32_t val)817 static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
818 {
819 
820 }
821 
822 /**
823  * cail_reg_write - write MMIO register
824  *
825  * @info: atom card_info pointer
826  * @reg: MMIO register offset
827  * @val: value to write to the pll register
828  *
829  * Provides a MMIO register accessor for the atom interpreter (r4xx+).
830  */
cail_reg_write(struct card_info * info,uint32_t reg,uint32_t val)831 static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
832 {
833 	struct amdgpu_device *adev = info->dev->dev_private;
834 
835 	WREG32(reg, val);
836 }
837 
838 /**
839  * cail_reg_read - read MMIO register
840  *
841  * @info: atom card_info pointer
842  * @reg: MMIO register offset
843  *
844  * Provides an MMIO register accessor for the atom interpreter (r4xx+).
845  * Returns the value of the MMIO register.
846  */
cail_reg_read(struct card_info * info,uint32_t reg)847 static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
848 {
849 	struct amdgpu_device *adev = info->dev->dev_private;
850 	uint32_t r;
851 
852 	r = RREG32(reg);
853 	return r;
854 }
855 
856 /**
857  * cail_ioreg_write - write IO register
858  *
859  * @info: atom card_info pointer
860  * @reg: IO register offset
861  * @val: value to write to the pll register
862  *
863  * Provides a IO register accessor for the atom interpreter (r4xx+).
864  */
cail_ioreg_write(struct card_info * info,uint32_t reg,uint32_t val)865 static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
866 {
867 	struct amdgpu_device *adev = info->dev->dev_private;
868 
869 	WREG32_IO(reg, val);
870 }
871 
872 /**
873  * cail_ioreg_read - read IO register
874  *
875  * @info: atom card_info pointer
876  * @reg: IO register offset
877  *
878  * Provides an IO register accessor for the atom interpreter (r4xx+).
879  * Returns the value of the IO register.
880  */
cail_ioreg_read(struct card_info * info,uint32_t reg)881 static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
882 {
883 	struct amdgpu_device *adev = info->dev->dev_private;
884 	uint32_t r;
885 
886 	r = RREG32_IO(reg);
887 	return r;
888 }
889 
890 /**
891  * amdgpu_atombios_fini - free the driver info and callbacks for atombios
892  *
893  * @adev: amdgpu_device pointer
894  *
895  * Frees the driver info and register access callbacks for the ATOM
896  * interpreter (r4xx+).
897  * Called at driver shutdown.
898  */
amdgpu_atombios_fini(struct amdgpu_device * adev)899 static void amdgpu_atombios_fini(struct amdgpu_device *adev)
900 {
901 	if (adev->mode_info.atom_context) {
902 		kfree(adev->mode_info.atom_context->scratch);
903 		kfree(adev->mode_info.atom_context->iio);
904 	}
905 	kfree(adev->mode_info.atom_context);
906 	adev->mode_info.atom_context = NULL;
907 	kfree(adev->mode_info.atom_card_info);
908 	adev->mode_info.atom_card_info = NULL;
909 }
910 
911 /**
912  * amdgpu_atombios_init - init the driver info and callbacks for atombios
913  *
914  * @adev: amdgpu_device pointer
915  *
916  * Initializes the driver info and register access callbacks for the
917  * ATOM interpreter (r4xx+).
918  * Returns 0 on sucess, -ENOMEM on failure.
919  * Called at driver startup.
920  */
amdgpu_atombios_init(struct amdgpu_device * adev)921 static int amdgpu_atombios_init(struct amdgpu_device *adev)
922 {
923 	struct card_info *atom_card_info =
924 	    kzalloc(sizeof(struct card_info), GFP_KERNEL);
925 
926 	if (!atom_card_info)
927 		return -ENOMEM;
928 
929 	adev->mode_info.atom_card_info = atom_card_info;
930 	atom_card_info->dev = adev->ddev;
931 	atom_card_info->reg_read = cail_reg_read;
932 	atom_card_info->reg_write = cail_reg_write;
933 	/* needed for iio ops */
934 	if (adev->rio_mem) {
935 		atom_card_info->ioreg_read = cail_ioreg_read;
936 		atom_card_info->ioreg_write = cail_ioreg_write;
937 	} else {
938 		DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
939 		atom_card_info->ioreg_read = cail_reg_read;
940 		atom_card_info->ioreg_write = cail_reg_write;
941 	}
942 	atom_card_info->mc_read = cail_mc_read;
943 	atom_card_info->mc_write = cail_mc_write;
944 	atom_card_info->pll_read = cail_pll_read;
945 	atom_card_info->pll_write = cail_pll_write;
946 
947 	adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
948 	if (!adev->mode_info.atom_context) {
949 		amdgpu_atombios_fini(adev);
950 		return -ENOMEM;
951 	}
952 
953 	mutex_init(&adev->mode_info.atom_context->mutex);
954 	if (adev->is_atom_fw) {
955 		amdgpu_atomfirmware_scratch_regs_init(adev);
956 		amdgpu_atomfirmware_allocate_fb_scratch(adev);
957 	} else {
958 		amdgpu_atombios_scratch_regs_init(adev);
959 		amdgpu_atombios_allocate_fb_scratch(adev);
960 	}
961 	return 0;
962 }
963 
964 /* if we get transitioned to only one device, take VGA back */
965 /**
966  * amdgpu_vga_set_decode - enable/disable vga decode
967  *
968  * @cookie: amdgpu_device pointer
969  * @state: enable/disable vga decode
970  *
971  * Enable/disable vga decode (all asics).
972  * Returns VGA resource flags.
973  */
amdgpu_vga_set_decode(void * cookie,bool state)974 static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
975 {
976 	struct amdgpu_device *adev = cookie;
977 	amdgpu_asic_set_vga_state(adev, state);
978 	if (state)
979 		return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
980 		       VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
981 	else
982 		return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
983 }
984 
amdgpu_check_block_size(struct amdgpu_device * adev)985 static void amdgpu_check_block_size(struct amdgpu_device *adev)
986 {
987 	/* defines number of bits in page table versus page directory,
988 	 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
989 	 * page table and the remaining bits are in the page directory */
990 	if (amdgpu_vm_block_size == -1)
991 		return;
992 
993 	if (amdgpu_vm_block_size < 9) {
994 		dev_warn(adev->dev, "VM page table size (%d) too small\n",
995 			 amdgpu_vm_block_size);
996 		goto def_value;
997 	}
998 
999 	if (amdgpu_vm_block_size > 24 ||
1000 	    (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1001 		dev_warn(adev->dev, "VM page table size (%d) too large\n",
1002 			 amdgpu_vm_block_size);
1003 		goto def_value;
1004 	}
1005 
1006 	return;
1007 
1008 def_value:
1009 	amdgpu_vm_block_size = -1;
1010 }
1011 
amdgpu_check_vm_size(struct amdgpu_device * adev)1012 static void amdgpu_check_vm_size(struct amdgpu_device *adev)
1013 {
1014 	/* no need to check the default value */
1015 	if (amdgpu_vm_size == -1)
1016 		return;
1017 
1018 	if (!is_power_of_2(amdgpu_vm_size)) {
1019 		dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
1020 			 amdgpu_vm_size);
1021 		goto def_value;
1022 	}
1023 
1024 	if (amdgpu_vm_size < 1) {
1025 		dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1026 			 amdgpu_vm_size);
1027 		goto def_value;
1028 	}
1029 
1030 	/*
1031 	 * Max GPUVM size for Cayman, SI, CI VI are 40 bits.
1032 	 */
1033 	if (amdgpu_vm_size > 1024) {
1034 		dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1035 			 amdgpu_vm_size);
1036 		goto def_value;
1037 	}
1038 
1039 	return;
1040 
1041 def_value:
1042 	amdgpu_vm_size = -1;
1043 }
1044 
1045 /**
1046  * amdgpu_check_arguments - validate module params
1047  *
1048  * @adev: amdgpu_device pointer
1049  *
1050  * Validates certain module parameters and updates
1051  * the associated values used by the driver (all asics).
1052  */
amdgpu_check_arguments(struct amdgpu_device * adev)1053 static void amdgpu_check_arguments(struct amdgpu_device *adev)
1054 {
1055 	if (amdgpu_sched_jobs < 4) {
1056 		dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1057 			 amdgpu_sched_jobs);
1058 		amdgpu_sched_jobs = 4;
1059 	} else if (!is_power_of_2(amdgpu_sched_jobs)){
1060 		dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1061 			 amdgpu_sched_jobs);
1062 		amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1063 	}
1064 
1065 	if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
1066 		/* gart size must be greater or equal to 32M */
1067 		dev_warn(adev->dev, "gart size (%d) too small\n",
1068 			 amdgpu_gart_size);
1069 		amdgpu_gart_size = -1;
1070 	}
1071 
1072 	if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
1073 		/* gtt size must be greater or equal to 32M */
1074 		dev_warn(adev->dev, "gtt size (%d) too small\n",
1075 				 amdgpu_gtt_size);
1076 		amdgpu_gtt_size = -1;
1077 	}
1078 
1079 	/* valid range is between 4 and 9 inclusive */
1080 	if (amdgpu_vm_fragment_size != -1 &&
1081 	    (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1082 		dev_warn(adev->dev, "valid range is between 4 and 9\n");
1083 		amdgpu_vm_fragment_size = -1;
1084 	}
1085 
1086 	amdgpu_check_vm_size(adev);
1087 
1088 	amdgpu_check_block_size(adev);
1089 
1090 	if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
1091 	    !is_power_of_2(amdgpu_vram_page_split))) {
1092 		dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1093 			 amdgpu_vram_page_split);
1094 		amdgpu_vram_page_split = 1024;
1095 	}
1096 }
1097 
1098 /**
1099  * amdgpu_switcheroo_set_state - set switcheroo state
1100  *
1101  * @pdev: pci dev pointer
1102  * @state: vga_switcheroo state
1103  *
1104  * Callback for the switcheroo driver.  Suspends or resumes the
1105  * the asics before or after it is powered up using ACPI methods.
1106  */
amdgpu_switcheroo_set_state(struct pci_dev * pdev,enum vga_switcheroo_state state)1107 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1108 {
1109 	struct drm_device *dev = pci_get_drvdata(pdev);
1110 
1111 	if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1112 		return;
1113 
1114 	if (state == VGA_SWITCHEROO_ON) {
1115 		pr_info("amdgpu: switched on\n");
1116 		/* don't suspend or resume card normally */
1117 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1118 
1119 		amdgpu_device_resume(dev, true, true);
1120 
1121 		dev->switch_power_state = DRM_SWITCH_POWER_ON;
1122 		drm_kms_helper_poll_enable(dev);
1123 	} else {
1124 		pr_info("amdgpu: switched off\n");
1125 		drm_kms_helper_poll_disable(dev);
1126 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1127 		amdgpu_device_suspend(dev, true, true);
1128 		dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1129 	}
1130 }
1131 
1132 /**
1133  * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1134  *
1135  * @pdev: pci dev pointer
1136  *
1137  * Callback for the switcheroo driver.  Check of the switcheroo
1138  * state can be changed.
1139  * Returns true if the state can be changed, false if not.
1140  */
amdgpu_switcheroo_can_switch(struct pci_dev * pdev)1141 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1142 {
1143 	struct drm_device *dev = pci_get_drvdata(pdev);
1144 
1145 	/*
1146 	* FIXME: open_count is protected by drm_global_mutex but that would lead to
1147 	* locking inversion with the driver load path. And the access here is
1148 	* completely racy anyway. So don't bother with locking for now.
1149 	*/
1150 	return dev->open_count == 0;
1151 }
1152 
1153 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1154 	.set_gpu_state = amdgpu_switcheroo_set_state,
1155 	.reprobe = NULL,
1156 	.can_switch = amdgpu_switcheroo_can_switch,
1157 };
1158 
amdgpu_set_clockgating_state(struct amdgpu_device * adev,enum amd_ip_block_type block_type,enum amd_clockgating_state state)1159 int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
1160 				  enum amd_ip_block_type block_type,
1161 				  enum amd_clockgating_state state)
1162 {
1163 	int i, r = 0;
1164 
1165 	for (i = 0; i < adev->num_ip_blocks; i++) {
1166 		if (!adev->ip_blocks[i].status.valid)
1167 			continue;
1168 		if (adev->ip_blocks[i].version->type != block_type)
1169 			continue;
1170 		if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1171 			continue;
1172 		r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1173 			(void *)adev, state);
1174 		if (r)
1175 			DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1176 				  adev->ip_blocks[i].version->funcs->name, r);
1177 	}
1178 	return r;
1179 }
1180 
amdgpu_set_powergating_state(struct amdgpu_device * adev,enum amd_ip_block_type block_type,enum amd_powergating_state state)1181 int amdgpu_set_powergating_state(struct amdgpu_device *adev,
1182 				  enum amd_ip_block_type block_type,
1183 				  enum amd_powergating_state state)
1184 {
1185 	int i, r = 0;
1186 
1187 	for (i = 0; i < adev->num_ip_blocks; i++) {
1188 		if (!adev->ip_blocks[i].status.valid)
1189 			continue;
1190 		if (adev->ip_blocks[i].version->type != block_type)
1191 			continue;
1192 		if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1193 			continue;
1194 		r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1195 			(void *)adev, state);
1196 		if (r)
1197 			DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1198 				  adev->ip_blocks[i].version->funcs->name, r);
1199 	}
1200 	return r;
1201 }
1202 
amdgpu_get_clockgating_state(struct amdgpu_device * adev,u32 * flags)1203 void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1204 {
1205 	int i;
1206 
1207 	for (i = 0; i < adev->num_ip_blocks; i++) {
1208 		if (!adev->ip_blocks[i].status.valid)
1209 			continue;
1210 		if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1211 			adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1212 	}
1213 }
1214 
amdgpu_wait_for_idle(struct amdgpu_device * adev,enum amd_ip_block_type block_type)1215 int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1216 			 enum amd_ip_block_type block_type)
1217 {
1218 	int i, r;
1219 
1220 	for (i = 0; i < adev->num_ip_blocks; i++) {
1221 		if (!adev->ip_blocks[i].status.valid)
1222 			continue;
1223 		if (adev->ip_blocks[i].version->type == block_type) {
1224 			r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
1225 			if (r)
1226 				return r;
1227 			break;
1228 		}
1229 	}
1230 	return 0;
1231 
1232 }
1233 
amdgpu_is_idle(struct amdgpu_device * adev,enum amd_ip_block_type block_type)1234 bool amdgpu_is_idle(struct amdgpu_device *adev,
1235 		    enum amd_ip_block_type block_type)
1236 {
1237 	int i;
1238 
1239 	for (i = 0; i < adev->num_ip_blocks; i++) {
1240 		if (!adev->ip_blocks[i].status.valid)
1241 			continue;
1242 		if (adev->ip_blocks[i].version->type == block_type)
1243 			return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
1244 	}
1245 	return true;
1246 
1247 }
1248 
amdgpu_get_ip_block(struct amdgpu_device * adev,enum amd_ip_block_type type)1249 struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1250 					     enum amd_ip_block_type type)
1251 {
1252 	int i;
1253 
1254 	for (i = 0; i < adev->num_ip_blocks; i++)
1255 		if (adev->ip_blocks[i].version->type == type)
1256 			return &adev->ip_blocks[i];
1257 
1258 	return NULL;
1259 }
1260 
1261 /**
1262  * amdgpu_ip_block_version_cmp
1263  *
1264  * @adev: amdgpu_device pointer
1265  * @type: enum amd_ip_block_type
1266  * @major: major version
1267  * @minor: minor version
1268  *
1269  * return 0 if equal or greater
1270  * return 1 if smaller or the ip_block doesn't exist
1271  */
amdgpu_ip_block_version_cmp(struct amdgpu_device * adev,enum amd_ip_block_type type,u32 major,u32 minor)1272 int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
1273 				enum amd_ip_block_type type,
1274 				u32 major, u32 minor)
1275 {
1276 	struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
1277 
1278 	if (ip_block && ((ip_block->version->major > major) ||
1279 			((ip_block->version->major == major) &&
1280 			(ip_block->version->minor >= minor))))
1281 		return 0;
1282 
1283 	return 1;
1284 }
1285 
1286 /**
1287  * amdgpu_ip_block_add
1288  *
1289  * @adev: amdgpu_device pointer
1290  * @ip_block_version: pointer to the IP to add
1291  *
1292  * Adds the IP block driver information to the collection of IPs
1293  * on the asic.
1294  */
amdgpu_ip_block_add(struct amdgpu_device * adev,const struct amdgpu_ip_block_version * ip_block_version)1295 int amdgpu_ip_block_add(struct amdgpu_device *adev,
1296 			const struct amdgpu_ip_block_version *ip_block_version)
1297 {
1298 	if (!ip_block_version)
1299 		return -EINVAL;
1300 
1301 	DRM_DEBUG("add ip block number %d <%s>\n", adev->num_ip_blocks,
1302 		  ip_block_version->funcs->name);
1303 
1304 	adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1305 
1306 	return 0;
1307 }
1308 
amdgpu_device_enable_virtual_display(struct amdgpu_device * adev)1309 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
1310 {
1311 	adev->enable_virtual_display = false;
1312 
1313 	if (amdgpu_virtual_display) {
1314 		struct drm_device *ddev = adev->ddev;
1315 		const char *pci_address_name = pci_name(ddev->pdev);
1316 		char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
1317 
1318 		pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1319 		pciaddstr_tmp = pciaddstr;
1320 		while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1321 			pciaddname = strsep(&pciaddname_tmp, ",");
1322 			if (!strcmp("all", pciaddname)
1323 			    || !strcmp(pci_address_name, pciaddname)) {
1324 				long num_crtc;
1325 				int res = -1;
1326 
1327 				adev->enable_virtual_display = true;
1328 
1329 				if (pciaddname_tmp)
1330 					res = kstrtol(pciaddname_tmp, 10,
1331 						      &num_crtc);
1332 
1333 				if (!res) {
1334 					if (num_crtc < 1)
1335 						num_crtc = 1;
1336 					if (num_crtc > 6)
1337 						num_crtc = 6;
1338 					adev->mode_info.num_crtc = num_crtc;
1339 				} else {
1340 					adev->mode_info.num_crtc = 1;
1341 				}
1342 				break;
1343 			}
1344 		}
1345 
1346 		DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1347 			 amdgpu_virtual_display, pci_address_name,
1348 			 adev->enable_virtual_display, adev->mode_info.num_crtc);
1349 
1350 		kfree(pciaddstr);
1351 	}
1352 }
1353 
amdgpu_device_parse_gpu_info_fw(struct amdgpu_device * adev)1354 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1355 {
1356 	const char *chip_name;
1357 	char fw_name[30];
1358 	int err;
1359 	const struct gpu_info_firmware_header_v1_0 *hdr;
1360 
1361 	adev->firmware.gpu_info_fw = NULL;
1362 
1363 	switch (adev->asic_type) {
1364 	case CHIP_TOPAZ:
1365 	case CHIP_TONGA:
1366 	case CHIP_FIJI:
1367 	case CHIP_POLARIS11:
1368 	case CHIP_POLARIS10:
1369 	case CHIP_POLARIS12:
1370 	case CHIP_CARRIZO:
1371 	case CHIP_STONEY:
1372 #ifdef CONFIG_DRM_AMDGPU_SI
1373 	case CHIP_VERDE:
1374 	case CHIP_TAHITI:
1375 	case CHIP_PITCAIRN:
1376 	case CHIP_OLAND:
1377 	case CHIP_HAINAN:
1378 #endif
1379 #ifdef CONFIG_DRM_AMDGPU_CIK
1380 	case CHIP_BONAIRE:
1381 	case CHIP_HAWAII:
1382 	case CHIP_KAVERI:
1383 	case CHIP_KABINI:
1384 	case CHIP_MULLINS:
1385 #endif
1386 	default:
1387 		return 0;
1388 	case CHIP_VEGA10:
1389 		chip_name = "vega10";
1390 		break;
1391 	case CHIP_RAVEN:
1392 		chip_name = "raven";
1393 		break;
1394 	}
1395 
1396 	snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
1397 	err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
1398 	if (err) {
1399 		dev_err(adev->dev,
1400 			"Failed to load gpu_info firmware \"%s\"\n",
1401 			fw_name);
1402 		goto out;
1403 	}
1404 	err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
1405 	if (err) {
1406 		dev_err(adev->dev,
1407 			"Failed to validate gpu_info firmware \"%s\"\n",
1408 			fw_name);
1409 		goto out;
1410 	}
1411 
1412 	hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
1413 	amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1414 
1415 	switch (hdr->version_major) {
1416 	case 1:
1417 	{
1418 		const struct gpu_info_firmware_v1_0 *gpu_info_fw =
1419 			(const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
1420 								le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1421 
1422 		adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1423 		adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1424 		adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1425 		adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
1426 		adev->gfx.config.max_texture_channel_caches =
1427 			le32_to_cpu(gpu_info_fw->gc_num_tccs);
1428 		adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1429 		adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1430 		adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1431 		adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
1432 		adev->gfx.config.double_offchip_lds_buf =
1433 			le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1434 		adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
1435 		adev->gfx.cu_info.max_waves_per_simd =
1436 			le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1437 		adev->gfx.cu_info.max_scratch_slots_per_cu =
1438 			le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1439 		adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
1440 		break;
1441 	}
1442 	default:
1443 		dev_err(adev->dev,
1444 			"Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1445 		err = -EINVAL;
1446 		goto out;
1447 	}
1448 out:
1449 	return err;
1450 }
1451 
amdgpu_early_init(struct amdgpu_device * adev)1452 static int amdgpu_early_init(struct amdgpu_device *adev)
1453 {
1454 	int i, r;
1455 
1456 	amdgpu_device_enable_virtual_display(adev);
1457 
1458 	switch (adev->asic_type) {
1459 	case CHIP_TOPAZ:
1460 	case CHIP_TONGA:
1461 	case CHIP_FIJI:
1462 	case CHIP_POLARIS11:
1463 	case CHIP_POLARIS10:
1464 	case CHIP_POLARIS12:
1465 	case CHIP_CARRIZO:
1466 	case CHIP_STONEY:
1467 		if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
1468 			adev->family = AMDGPU_FAMILY_CZ;
1469 		else
1470 			adev->family = AMDGPU_FAMILY_VI;
1471 
1472 		r = vi_set_ip_blocks(adev);
1473 		if (r)
1474 			return r;
1475 		break;
1476 #ifdef CONFIG_DRM_AMDGPU_SI
1477 	case CHIP_VERDE:
1478 	case CHIP_TAHITI:
1479 	case CHIP_PITCAIRN:
1480 	case CHIP_OLAND:
1481 	case CHIP_HAINAN:
1482 		adev->family = AMDGPU_FAMILY_SI;
1483 		r = si_set_ip_blocks(adev);
1484 		if (r)
1485 			return r;
1486 		break;
1487 #endif
1488 #ifdef CONFIG_DRM_AMDGPU_CIK
1489 	case CHIP_BONAIRE:
1490 	case CHIP_HAWAII:
1491 	case CHIP_KAVERI:
1492 	case CHIP_KABINI:
1493 	case CHIP_MULLINS:
1494 		if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1495 			adev->family = AMDGPU_FAMILY_CI;
1496 		else
1497 			adev->family = AMDGPU_FAMILY_KV;
1498 
1499 		r = cik_set_ip_blocks(adev);
1500 		if (r)
1501 			return r;
1502 		break;
1503 #endif
1504 	case  CHIP_VEGA10:
1505 	case  CHIP_RAVEN:
1506 		if (adev->asic_type == CHIP_RAVEN)
1507 			adev->family = AMDGPU_FAMILY_RV;
1508 		else
1509 			adev->family = AMDGPU_FAMILY_AI;
1510 
1511 		r = soc15_set_ip_blocks(adev);
1512 		if (r)
1513 			return r;
1514 		break;
1515 	default:
1516 		/* FIXME: not supported yet */
1517 		return -EINVAL;
1518 	}
1519 
1520 	r = amdgpu_device_parse_gpu_info_fw(adev);
1521 	if (r)
1522 		return r;
1523 
1524 	if (amdgpu_sriov_vf(adev)) {
1525 		r = amdgpu_virt_request_full_gpu(adev, true);
1526 		if (r)
1527 			return r;
1528 	}
1529 
1530 	for (i = 0; i < adev->num_ip_blocks; i++) {
1531 		if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1532 			DRM_ERROR("disabled ip block: %d <%s>\n",
1533 				  i, adev->ip_blocks[i].version->funcs->name);
1534 			adev->ip_blocks[i].status.valid = false;
1535 		} else {
1536 			if (adev->ip_blocks[i].version->funcs->early_init) {
1537 				r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
1538 				if (r == -ENOENT) {
1539 					adev->ip_blocks[i].status.valid = false;
1540 				} else if (r) {
1541 					DRM_ERROR("early_init of IP block <%s> failed %d\n",
1542 						  adev->ip_blocks[i].version->funcs->name, r);
1543 					return r;
1544 				} else {
1545 					adev->ip_blocks[i].status.valid = true;
1546 				}
1547 			} else {
1548 				adev->ip_blocks[i].status.valid = true;
1549 			}
1550 		}
1551 	}
1552 
1553 	adev->cg_flags &= amdgpu_cg_mask;
1554 	adev->pg_flags &= amdgpu_pg_mask;
1555 
1556 	return 0;
1557 }
1558 
amdgpu_init(struct amdgpu_device * adev)1559 static int amdgpu_init(struct amdgpu_device *adev)
1560 {
1561 	int i, r;
1562 
1563 	for (i = 0; i < adev->num_ip_blocks; i++) {
1564 		if (!adev->ip_blocks[i].status.valid)
1565 			continue;
1566 		r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
1567 		if (r) {
1568 			DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1569 				  adev->ip_blocks[i].version->funcs->name, r);
1570 			return r;
1571 		}
1572 		adev->ip_blocks[i].status.sw = true;
1573 		/* need to do gmc hw init early so we can allocate gpu mem */
1574 		if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1575 			r = amdgpu_vram_scratch_init(adev);
1576 			if (r) {
1577 				DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
1578 				return r;
1579 			}
1580 			r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1581 			if (r) {
1582 				DRM_ERROR("hw_init %d failed %d\n", i, r);
1583 				return r;
1584 			}
1585 			r = amdgpu_wb_init(adev);
1586 			if (r) {
1587 				DRM_ERROR("amdgpu_wb_init failed %d\n", r);
1588 				return r;
1589 			}
1590 			adev->ip_blocks[i].status.hw = true;
1591 
1592 			/* right after GMC hw init, we create CSA */
1593 			if (amdgpu_sriov_vf(adev)) {
1594 				r = amdgpu_allocate_static_csa(adev);
1595 				if (r) {
1596 					DRM_ERROR("allocate CSA failed %d\n", r);
1597 					return r;
1598 				}
1599 			}
1600 		}
1601 	}
1602 
1603 	for (i = 0; i < adev->num_ip_blocks; i++) {
1604 		if (!adev->ip_blocks[i].status.sw)
1605 			continue;
1606 		/* gmc hw init is done early */
1607 		if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
1608 			continue;
1609 		r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1610 		if (r) {
1611 			DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1612 				  adev->ip_blocks[i].version->funcs->name, r);
1613 			return r;
1614 		}
1615 		adev->ip_blocks[i].status.hw = true;
1616 	}
1617 
1618 	return 0;
1619 }
1620 
amdgpu_fill_reset_magic(struct amdgpu_device * adev)1621 static void amdgpu_fill_reset_magic(struct amdgpu_device *adev)
1622 {
1623 	memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1624 }
1625 
amdgpu_check_vram_lost(struct amdgpu_device * adev)1626 static bool amdgpu_check_vram_lost(struct amdgpu_device *adev)
1627 {
1628 	return !!memcmp(adev->gart.ptr, adev->reset_magic,
1629 			AMDGPU_RESET_MAGIC_NUM);
1630 }
1631 
amdgpu_late_set_cg_state(struct amdgpu_device * adev)1632 static int amdgpu_late_set_cg_state(struct amdgpu_device *adev)
1633 {
1634 	int i = 0, r;
1635 
1636 	for (i = 0; i < adev->num_ip_blocks; i++) {
1637 		if (!adev->ip_blocks[i].status.valid)
1638 			continue;
1639 		/* skip CG for VCE/UVD, it's handled specially */
1640 		if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1641 		    adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1642 			/* enable clockgating to save power */
1643 			r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1644 										     AMD_CG_STATE_GATE);
1645 			if (r) {
1646 				DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1647 					  adev->ip_blocks[i].version->funcs->name, r);
1648 				return r;
1649 			}
1650 		}
1651 	}
1652 	return 0;
1653 }
1654 
amdgpu_late_init(struct amdgpu_device * adev)1655 static int amdgpu_late_init(struct amdgpu_device *adev)
1656 {
1657 	int i = 0, r;
1658 
1659 	for (i = 0; i < adev->num_ip_blocks; i++) {
1660 		if (!adev->ip_blocks[i].status.valid)
1661 			continue;
1662 		if (adev->ip_blocks[i].version->funcs->late_init) {
1663 			r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
1664 			if (r) {
1665 				DRM_ERROR("late_init of IP block <%s> failed %d\n",
1666 					  adev->ip_blocks[i].version->funcs->name, r);
1667 				return r;
1668 			}
1669 			adev->ip_blocks[i].status.late_initialized = true;
1670 		}
1671 	}
1672 
1673 	mod_delayed_work(system_wq, &adev->late_init_work,
1674 			msecs_to_jiffies(AMDGPU_RESUME_MS));
1675 
1676 	amdgpu_fill_reset_magic(adev);
1677 
1678 	return 0;
1679 }
1680 
amdgpu_fini(struct amdgpu_device * adev)1681 static int amdgpu_fini(struct amdgpu_device *adev)
1682 {
1683 	int i, r;
1684 
1685 	/* need to disable SMC first */
1686 	for (i = 0; i < adev->num_ip_blocks; i++) {
1687 		if (!adev->ip_blocks[i].status.hw)
1688 			continue;
1689 		if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
1690 			/* ungate blocks before hw fini so that we can shutdown the blocks safely */
1691 			r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1692 										     AMD_CG_STATE_UNGATE);
1693 			if (r) {
1694 				DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1695 					  adev->ip_blocks[i].version->funcs->name, r);
1696 				return r;
1697 			}
1698 			r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
1699 			/* XXX handle errors */
1700 			if (r) {
1701 				DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1702 					  adev->ip_blocks[i].version->funcs->name, r);
1703 			}
1704 			adev->ip_blocks[i].status.hw = false;
1705 			break;
1706 		}
1707 	}
1708 
1709 	for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1710 		if (!adev->ip_blocks[i].status.hw)
1711 			continue;
1712 		if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1713 			amdgpu_wb_fini(adev);
1714 			amdgpu_vram_scratch_fini(adev);
1715 		}
1716 
1717 		if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1718 			adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1719 			/* ungate blocks before hw fini so that we can shutdown the blocks safely */
1720 			r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1721 										     AMD_CG_STATE_UNGATE);
1722 			if (r) {
1723 				DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1724 					  adev->ip_blocks[i].version->funcs->name, r);
1725 				return r;
1726 			}
1727 		}
1728 
1729 		r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
1730 		/* XXX handle errors */
1731 		if (r) {
1732 			DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1733 				  adev->ip_blocks[i].version->funcs->name, r);
1734 		}
1735 
1736 		adev->ip_blocks[i].status.hw = false;
1737 	}
1738 
1739 	for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1740 		if (!adev->ip_blocks[i].status.sw)
1741 			continue;
1742 		r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
1743 		/* XXX handle errors */
1744 		if (r) {
1745 			DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1746 				  adev->ip_blocks[i].version->funcs->name, r);
1747 		}
1748 		adev->ip_blocks[i].status.sw = false;
1749 		adev->ip_blocks[i].status.valid = false;
1750 	}
1751 
1752 	for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1753 		if (!adev->ip_blocks[i].status.late_initialized)
1754 			continue;
1755 		if (adev->ip_blocks[i].version->funcs->late_fini)
1756 			adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1757 		adev->ip_blocks[i].status.late_initialized = false;
1758 	}
1759 
1760 	if (amdgpu_sriov_vf(adev)) {
1761 		amdgpu_bo_free_kernel(&adev->virt.csa_obj, &adev->virt.csa_vmid0_addr, NULL);
1762 		amdgpu_virt_release_full_gpu(adev, false);
1763 	}
1764 
1765 	return 0;
1766 }
1767 
amdgpu_late_init_func_handler(struct work_struct * work)1768 static void amdgpu_late_init_func_handler(struct work_struct *work)
1769 {
1770 	struct amdgpu_device *adev =
1771 		container_of(work, struct amdgpu_device, late_init_work.work);
1772 	amdgpu_late_set_cg_state(adev);
1773 }
1774 
amdgpu_suspend(struct amdgpu_device * adev)1775 int amdgpu_suspend(struct amdgpu_device *adev)
1776 {
1777 	int i, r;
1778 
1779 	if (amdgpu_sriov_vf(adev))
1780 		amdgpu_virt_request_full_gpu(adev, false);
1781 
1782 	/* ungate SMC block first */
1783 	r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1784 					 AMD_CG_STATE_UNGATE);
1785 	if (r) {
1786 		DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1787 	}
1788 
1789 	for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1790 		if (!adev->ip_blocks[i].status.valid)
1791 			continue;
1792 		/* ungate blocks so that suspend can properly shut them down */
1793 		if (i != AMD_IP_BLOCK_TYPE_SMC) {
1794 			r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1795 										     AMD_CG_STATE_UNGATE);
1796 			if (r) {
1797 				DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1798 					  adev->ip_blocks[i].version->funcs->name, r);
1799 			}
1800 		}
1801 		/* XXX handle errors */
1802 		r = adev->ip_blocks[i].version->funcs->suspend(adev);
1803 		/* XXX handle errors */
1804 		if (r) {
1805 			DRM_ERROR("suspend of IP block <%s> failed %d\n",
1806 				  adev->ip_blocks[i].version->funcs->name, r);
1807 		}
1808 	}
1809 
1810 	if (amdgpu_sriov_vf(adev))
1811 		amdgpu_virt_release_full_gpu(adev, false);
1812 
1813 	return 0;
1814 }
1815 
amdgpu_sriov_reinit_early(struct amdgpu_device * adev)1816 static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
1817 {
1818 	int i, r;
1819 
1820 	static enum amd_ip_block_type ip_order[] = {
1821 		AMD_IP_BLOCK_TYPE_GMC,
1822 		AMD_IP_BLOCK_TYPE_COMMON,
1823 		AMD_IP_BLOCK_TYPE_IH,
1824 	};
1825 
1826 	for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1827 		int j;
1828 		struct amdgpu_ip_block *block;
1829 
1830 		for (j = 0; j < adev->num_ip_blocks; j++) {
1831 			block = &adev->ip_blocks[j];
1832 
1833 			if (block->version->type != ip_order[i] ||
1834 				!block->status.valid)
1835 				continue;
1836 
1837 			r = block->version->funcs->hw_init(adev);
1838 			DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
1839 		}
1840 	}
1841 
1842 	return 0;
1843 }
1844 
amdgpu_sriov_reinit_late(struct amdgpu_device * adev)1845 static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
1846 {
1847 	int i, r;
1848 
1849 	static enum amd_ip_block_type ip_order[] = {
1850 		AMD_IP_BLOCK_TYPE_SMC,
1851 		AMD_IP_BLOCK_TYPE_DCE,
1852 		AMD_IP_BLOCK_TYPE_GFX,
1853 		AMD_IP_BLOCK_TYPE_SDMA,
1854 		AMD_IP_BLOCK_TYPE_UVD,
1855 		AMD_IP_BLOCK_TYPE_VCE
1856 	};
1857 
1858 	for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1859 		int j;
1860 		struct amdgpu_ip_block *block;
1861 
1862 		for (j = 0; j < adev->num_ip_blocks; j++) {
1863 			block = &adev->ip_blocks[j];
1864 
1865 			if (block->version->type != ip_order[i] ||
1866 				!block->status.valid)
1867 				continue;
1868 
1869 			r = block->version->funcs->hw_init(adev);
1870 			DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
1871 		}
1872 	}
1873 
1874 	return 0;
1875 }
1876 
amdgpu_resume_phase1(struct amdgpu_device * adev)1877 static int amdgpu_resume_phase1(struct amdgpu_device *adev)
1878 {
1879 	int i, r;
1880 
1881 	for (i = 0; i < adev->num_ip_blocks; i++) {
1882 		if (!adev->ip_blocks[i].status.valid)
1883 			continue;
1884 		if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1885 				adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1886 				adev->ip_blocks[i].version->type ==
1887 				AMD_IP_BLOCK_TYPE_IH) {
1888 			r = adev->ip_blocks[i].version->funcs->resume(adev);
1889 			if (r) {
1890 				DRM_ERROR("resume of IP block <%s> failed %d\n",
1891 					  adev->ip_blocks[i].version->funcs->name, r);
1892 				return r;
1893 			}
1894 		}
1895 	}
1896 
1897 	return 0;
1898 }
1899 
amdgpu_resume_phase2(struct amdgpu_device * adev)1900 static int amdgpu_resume_phase2(struct amdgpu_device *adev)
1901 {
1902 	int i, r;
1903 
1904 	for (i = 0; i < adev->num_ip_blocks; i++) {
1905 		if (!adev->ip_blocks[i].status.valid)
1906 			continue;
1907 		if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1908 				adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1909 				adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
1910 			continue;
1911 		r = adev->ip_blocks[i].version->funcs->resume(adev);
1912 		if (r) {
1913 			DRM_ERROR("resume of IP block <%s> failed %d\n",
1914 				  adev->ip_blocks[i].version->funcs->name, r);
1915 			return r;
1916 		}
1917 	}
1918 
1919 	return 0;
1920 }
1921 
amdgpu_resume(struct amdgpu_device * adev)1922 static int amdgpu_resume(struct amdgpu_device *adev)
1923 {
1924 	int r;
1925 
1926 	r = amdgpu_resume_phase1(adev);
1927 	if (r)
1928 		return r;
1929 	r = amdgpu_resume_phase2(adev);
1930 
1931 	return r;
1932 }
1933 
amdgpu_device_detect_sriov_bios(struct amdgpu_device * adev)1934 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
1935 {
1936 	if (adev->is_atom_fw) {
1937 		if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
1938 			adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1939 	} else {
1940 		if (amdgpu_atombios_has_gpu_virtualization_table(adev))
1941 			adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1942 	}
1943 }
1944 
1945 /**
1946  * amdgpu_device_init - initialize the driver
1947  *
1948  * @adev: amdgpu_device pointer
1949  * @pdev: drm dev pointer
1950  * @pdev: pci dev pointer
1951  * @flags: driver flags
1952  *
1953  * Initializes the driver info and hw (all asics).
1954  * Returns 0 for success or an error on failure.
1955  * Called at driver startup.
1956  */
amdgpu_device_init(struct amdgpu_device * adev,struct drm_device * ddev,struct pci_dev * pdev,uint32_t flags)1957 int amdgpu_device_init(struct amdgpu_device *adev,
1958 		       struct drm_device *ddev,
1959 		       struct pci_dev *pdev,
1960 		       uint32_t flags)
1961 {
1962 	int r, i;
1963 	bool runtime = false;
1964 	u32 max_MBps;
1965 
1966 	adev->shutdown = false;
1967 	adev->dev = &pdev->dev;
1968 	adev->ddev = ddev;
1969 	adev->pdev = pdev;
1970 	adev->flags = flags;
1971 	adev->asic_type = flags & AMD_ASIC_MASK;
1972 	adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
1973 	adev->mc.gart_size = 512 * 1024 * 1024;
1974 	adev->accel_working = false;
1975 	adev->num_rings = 0;
1976 	adev->mman.buffer_funcs = NULL;
1977 	adev->mman.buffer_funcs_ring = NULL;
1978 	adev->vm_manager.vm_pte_funcs = NULL;
1979 	adev->vm_manager.vm_pte_num_rings = 0;
1980 	adev->gart.gart_funcs = NULL;
1981 	adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
1982 
1983 	adev->smc_rreg = &amdgpu_invalid_rreg;
1984 	adev->smc_wreg = &amdgpu_invalid_wreg;
1985 	adev->pcie_rreg = &amdgpu_invalid_rreg;
1986 	adev->pcie_wreg = &amdgpu_invalid_wreg;
1987 	adev->pciep_rreg = &amdgpu_invalid_rreg;
1988 	adev->pciep_wreg = &amdgpu_invalid_wreg;
1989 	adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1990 	adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1991 	adev->didt_rreg = &amdgpu_invalid_rreg;
1992 	adev->didt_wreg = &amdgpu_invalid_wreg;
1993 	adev->gc_cac_rreg = &amdgpu_invalid_rreg;
1994 	adev->gc_cac_wreg = &amdgpu_invalid_wreg;
1995 	adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1996 	adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1997 
1998 
1999 	DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2000 		 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2001 		 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
2002 
2003 	/* mutex initialization are all done here so we
2004 	 * can recall function without having locking issues */
2005 	atomic_set(&adev->irq.ih.lock, 0);
2006 	mutex_init(&adev->firmware.mutex);
2007 	mutex_init(&adev->pm.mutex);
2008 	mutex_init(&adev->gfx.gpu_clock_mutex);
2009 	mutex_init(&adev->srbm_mutex);
2010 	mutex_init(&adev->grbm_idx_mutex);
2011 	mutex_init(&adev->mn_lock);
2012 	hash_init(adev->mn_hash);
2013 
2014 	amdgpu_check_arguments(adev);
2015 
2016 	spin_lock_init(&adev->mmio_idx_lock);
2017 	spin_lock_init(&adev->smc_idx_lock);
2018 	spin_lock_init(&adev->pcie_idx_lock);
2019 	spin_lock_init(&adev->uvd_ctx_idx_lock);
2020 	spin_lock_init(&adev->didt_idx_lock);
2021 	spin_lock_init(&adev->gc_cac_idx_lock);
2022 	spin_lock_init(&adev->se_cac_idx_lock);
2023 	spin_lock_init(&adev->audio_endpt_idx_lock);
2024 	spin_lock_init(&adev->mm_stats.lock);
2025 
2026 	INIT_LIST_HEAD(&adev->shadow_list);
2027 	mutex_init(&adev->shadow_list_lock);
2028 
2029 	INIT_LIST_HEAD(&adev->gtt_list);
2030 	spin_lock_init(&adev->gtt_list_lock);
2031 
2032 	INIT_LIST_HEAD(&adev->ring_lru_list);
2033 	spin_lock_init(&adev->ring_lru_list_lock);
2034 
2035 	INIT_DELAYED_WORK(&adev->late_init_work, amdgpu_late_init_func_handler);
2036 
2037 	/* Registers mapping */
2038 	/* TODO: block userspace mapping of io register */
2039 	if (adev->asic_type >= CHIP_BONAIRE) {
2040 		adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2041 		adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2042 	} else {
2043 		adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2044 		adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2045 	}
2046 
2047 	adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2048 	if (adev->rmmio == NULL) {
2049 		return -ENOMEM;
2050 	}
2051 	DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2052 	DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2053 
2054 	if (adev->asic_type >= CHIP_BONAIRE)
2055 		/* doorbell bar mapping */
2056 		amdgpu_doorbell_init(adev);
2057 
2058 	/* io port mapping */
2059 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2060 		if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2061 			adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2062 			adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2063 			break;
2064 		}
2065 	}
2066 	if (adev->rio_mem == NULL)
2067 		DRM_INFO("PCI I/O BAR is not found.\n");
2068 
2069 	/* early init functions */
2070 	r = amdgpu_early_init(adev);
2071 	if (r)
2072 		return r;
2073 
2074 	/* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2075 	/* this will fail for cards that aren't VGA class devices, just
2076 	 * ignore it */
2077 	vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
2078 
2079 	if (amdgpu_device_is_px(ddev))
2080 		runtime = true;
2081 	if (!pci_is_thunderbolt_attached(adev->pdev))
2082 		vga_switcheroo_register_client(adev->pdev,
2083 					       &amdgpu_switcheroo_ops, runtime);
2084 	if (runtime)
2085 		vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2086 
2087 	/* Read BIOS */
2088 	if (!amdgpu_get_bios(adev)) {
2089 		r = -EINVAL;
2090 		goto failed;
2091 	}
2092 
2093 	r = amdgpu_atombios_init(adev);
2094 	if (r) {
2095 		dev_err(adev->dev, "amdgpu_atombios_init failed\n");
2096 		amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
2097 		goto failed;
2098 	}
2099 
2100 	/* detect if we are with an SRIOV vbios */
2101 	amdgpu_device_detect_sriov_bios(adev);
2102 
2103 	/* Post card if necessary */
2104 	if (amdgpu_vpost_needed(adev)) {
2105 		if (!adev->bios) {
2106 			dev_err(adev->dev, "no vBIOS found\n");
2107 			amdgpu_vf_error_put(AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
2108 			r = -EINVAL;
2109 			goto failed;
2110 		}
2111 		DRM_INFO("GPU posting now...\n");
2112 		r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2113 		if (r) {
2114 			dev_err(adev->dev, "gpu post error!\n");
2115 			amdgpu_vf_error_put(AMDGIM_ERROR_VF_GPU_POST_ERROR, 0, 0);
2116 			goto failed;
2117 		}
2118 	} else {
2119 		DRM_INFO("GPU post is not needed\n");
2120 	}
2121 
2122 	if (adev->is_atom_fw) {
2123 		/* Initialize clocks */
2124 		r = amdgpu_atomfirmware_get_clock_info(adev);
2125 		if (r) {
2126 			dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
2127 			amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2128 			goto failed;
2129 		}
2130 	} else {
2131 		/* Initialize clocks */
2132 		r = amdgpu_atombios_get_clock_info(adev);
2133 		if (r) {
2134 			dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
2135 			amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2136 			goto failed;
2137 		}
2138 		/* init i2c buses */
2139 		amdgpu_atombios_i2c_init(adev);
2140 	}
2141 
2142 	/* Fence driver */
2143 	r = amdgpu_fence_driver_init(adev);
2144 	if (r) {
2145 		dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
2146 		amdgpu_vf_error_put(AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
2147 		goto failed;
2148 	}
2149 
2150 	/* init the mode config */
2151 	drm_mode_config_init(adev->ddev);
2152 
2153 	r = amdgpu_init(adev);
2154 	if (r) {
2155 		dev_err(adev->dev, "amdgpu_init failed\n");
2156 		amdgpu_vf_error_put(AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
2157 		amdgpu_fini(adev);
2158 		goto failed;
2159 	}
2160 
2161 	adev->accel_working = true;
2162 
2163 	amdgpu_vm_check_compute_bug(adev);
2164 
2165 	/* Initialize the buffer migration limit. */
2166 	if (amdgpu_moverate >= 0)
2167 		max_MBps = amdgpu_moverate;
2168 	else
2169 		max_MBps = 8; /* Allow 8 MB/s. */
2170 	/* Get a log2 for easy divisions. */
2171 	adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2172 
2173 	r = amdgpu_ib_pool_init(adev);
2174 	if (r) {
2175 		dev_err(adev->dev, "IB initialization failed (%d).\n", r);
2176 		amdgpu_vf_error_put(AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
2177 		goto failed;
2178 	}
2179 
2180 	r = amdgpu_ib_ring_tests(adev);
2181 	if (r)
2182 		DRM_ERROR("ib ring test failed (%d).\n", r);
2183 
2184 	amdgpu_fbdev_init(adev);
2185 
2186 	r = amdgpu_gem_debugfs_init(adev);
2187 	if (r)
2188 		DRM_ERROR("registering gem debugfs failed (%d).\n", r);
2189 
2190 	r = amdgpu_debugfs_regs_init(adev);
2191 	if (r)
2192 		DRM_ERROR("registering register debugfs failed (%d).\n", r);
2193 
2194 	r = amdgpu_debugfs_test_ib_ring_init(adev);
2195 	if (r)
2196 		DRM_ERROR("registering register test ib ring debugfs failed (%d).\n", r);
2197 
2198 	r = amdgpu_debugfs_firmware_init(adev);
2199 	if (r)
2200 		DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
2201 
2202 	if ((amdgpu_testing & 1)) {
2203 		if (adev->accel_working)
2204 			amdgpu_test_moves(adev);
2205 		else
2206 			DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2207 	}
2208 	if (amdgpu_benchmarking) {
2209 		if (adev->accel_working)
2210 			amdgpu_benchmark(adev, amdgpu_benchmarking);
2211 		else
2212 			DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2213 	}
2214 
2215 	/* enable clockgating, etc. after ib tests, etc. since some blocks require
2216 	 * explicit gating rather than handling it automatically.
2217 	 */
2218 	r = amdgpu_late_init(adev);
2219 	if (r) {
2220 		dev_err(adev->dev, "amdgpu_late_init failed\n");
2221 		amdgpu_vf_error_put(AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
2222 		goto failed;
2223 	}
2224 
2225 	return 0;
2226 
2227 failed:
2228 	amdgpu_vf_error_trans_all(adev);
2229 	if (runtime)
2230 		vga_switcheroo_fini_domain_pm_ops(adev->dev);
2231 	return r;
2232 }
2233 
2234 /**
2235  * amdgpu_device_fini - tear down the driver
2236  *
2237  * @adev: amdgpu_device pointer
2238  *
2239  * Tear down the driver info (all asics).
2240  * Called at driver shutdown.
2241  */
amdgpu_device_fini(struct amdgpu_device * adev)2242 void amdgpu_device_fini(struct amdgpu_device *adev)
2243 {
2244 	int r;
2245 
2246 	DRM_INFO("amdgpu: finishing device.\n");
2247 	adev->shutdown = true;
2248 	if (adev->mode_info.mode_config_initialized)
2249 		drm_crtc_force_disable_all(adev->ddev);
2250 	/* evict vram memory */
2251 	amdgpu_bo_evict_vram(adev);
2252 	amdgpu_ib_pool_fini(adev);
2253 	amdgpu_fence_driver_fini(adev);
2254 	amdgpu_fbdev_fini(adev);
2255 	r = amdgpu_fini(adev);
2256 	if (adev->firmware.gpu_info_fw) {
2257 		release_firmware(adev->firmware.gpu_info_fw);
2258 		adev->firmware.gpu_info_fw = NULL;
2259 	}
2260 	adev->accel_working = false;
2261 	cancel_delayed_work_sync(&adev->late_init_work);
2262 	/* free i2c buses */
2263 	amdgpu_i2c_fini(adev);
2264 	amdgpu_atombios_fini(adev);
2265 	kfree(adev->bios);
2266 	adev->bios = NULL;
2267 	if (!pci_is_thunderbolt_attached(adev->pdev))
2268 		vga_switcheroo_unregister_client(adev->pdev);
2269 	if (adev->flags & AMD_IS_PX)
2270 		vga_switcheroo_fini_domain_pm_ops(adev->dev);
2271 	vga_client_register(adev->pdev, NULL, NULL, NULL);
2272 	if (adev->rio_mem)
2273 		pci_iounmap(adev->pdev, adev->rio_mem);
2274 	adev->rio_mem = NULL;
2275 	iounmap(adev->rmmio);
2276 	adev->rmmio = NULL;
2277 	if (adev->asic_type >= CHIP_BONAIRE)
2278 		amdgpu_doorbell_fini(adev);
2279 	amdgpu_debugfs_regs_cleanup(adev);
2280 }
2281 
2282 
2283 /*
2284  * Suspend & resume.
2285  */
2286 /**
2287  * amdgpu_device_suspend - initiate device suspend
2288  *
2289  * @pdev: drm dev pointer
2290  * @state: suspend state
2291  *
2292  * Puts the hw in the suspend state (all asics).
2293  * Returns 0 for success or an error on failure.
2294  * Called at driver suspend.
2295  */
amdgpu_device_suspend(struct drm_device * dev,bool suspend,bool fbcon)2296 int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
2297 {
2298 	struct amdgpu_device *adev;
2299 	struct drm_crtc *crtc;
2300 	struct drm_connector *connector;
2301 	int r;
2302 
2303 	if (dev == NULL || dev->dev_private == NULL) {
2304 		return -ENODEV;
2305 	}
2306 
2307 	adev = dev->dev_private;
2308 
2309 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2310 		return 0;
2311 
2312 	drm_kms_helper_poll_disable(dev);
2313 
2314 	/* turn off display hw */
2315 	drm_modeset_lock_all(dev);
2316 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2317 		drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2318 	}
2319 	drm_modeset_unlock_all(dev);
2320 
2321 	amdgpu_amdkfd_suspend(adev);
2322 
2323 	/* unpin the front buffers and cursors */
2324 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2325 		struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2326 		struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2327 		struct amdgpu_bo *robj;
2328 
2329 		if (amdgpu_crtc->cursor_bo) {
2330 			struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2331 			r = amdgpu_bo_reserve(aobj, true);
2332 			if (r == 0) {
2333 				amdgpu_bo_unpin(aobj);
2334 				amdgpu_bo_unreserve(aobj);
2335 			}
2336 		}
2337 
2338 		if (rfb == NULL || rfb->obj == NULL) {
2339 			continue;
2340 		}
2341 		robj = gem_to_amdgpu_bo(rfb->obj);
2342 		/* don't unpin kernel fb objects */
2343 		if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
2344 			r = amdgpu_bo_reserve(robj, true);
2345 			if (r == 0) {
2346 				amdgpu_bo_unpin(robj);
2347 				amdgpu_bo_unreserve(robj);
2348 			}
2349 		}
2350 	}
2351 	/* evict vram memory */
2352 	amdgpu_bo_evict_vram(adev);
2353 
2354 	amdgpu_fence_driver_suspend(adev);
2355 
2356 	r = amdgpu_suspend(adev);
2357 
2358 	/* evict remaining vram memory
2359 	 * This second call to evict vram is to evict the gart page table
2360 	 * using the CPU.
2361 	 */
2362 	amdgpu_bo_evict_vram(adev);
2363 
2364 	amdgpu_atombios_scratch_regs_save(adev);
2365 	pci_save_state(dev->pdev);
2366 	if (suspend) {
2367 		/* Shut down the device */
2368 		pci_disable_device(dev->pdev);
2369 		pci_set_power_state(dev->pdev, PCI_D3hot);
2370 	} else {
2371 		r = amdgpu_asic_reset(adev);
2372 		if (r)
2373 			DRM_ERROR("amdgpu asic reset failed\n");
2374 	}
2375 
2376 	if (fbcon) {
2377 		console_lock();
2378 		amdgpu_fbdev_set_suspend(adev, 1);
2379 		console_unlock();
2380 	}
2381 	return 0;
2382 }
2383 
2384 /**
2385  * amdgpu_device_resume - initiate device resume
2386  *
2387  * @pdev: drm dev pointer
2388  *
2389  * Bring the hw back to operating state (all asics).
2390  * Returns 0 for success or an error on failure.
2391  * Called at driver resume.
2392  */
amdgpu_device_resume(struct drm_device * dev,bool resume,bool fbcon)2393 int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
2394 {
2395 	struct drm_connector *connector;
2396 	struct amdgpu_device *adev = dev->dev_private;
2397 	struct drm_crtc *crtc;
2398 	int r = 0;
2399 
2400 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2401 		return 0;
2402 
2403 	if (fbcon)
2404 		console_lock();
2405 
2406 	if (resume) {
2407 		pci_set_power_state(dev->pdev, PCI_D0);
2408 		pci_restore_state(dev->pdev);
2409 		r = pci_enable_device(dev->pdev);
2410 		if (r)
2411 			goto unlock;
2412 	}
2413 	amdgpu_atombios_scratch_regs_restore(adev);
2414 
2415 	/* post card */
2416 	if (amdgpu_need_post(adev)) {
2417 		r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2418 		if (r)
2419 			DRM_ERROR("amdgpu asic init failed\n");
2420 	}
2421 
2422 	r = amdgpu_resume(adev);
2423 	if (r) {
2424 		DRM_ERROR("amdgpu_resume failed (%d).\n", r);
2425 		goto unlock;
2426 	}
2427 	amdgpu_fence_driver_resume(adev);
2428 
2429 	if (resume) {
2430 		r = amdgpu_ib_ring_tests(adev);
2431 		if (r)
2432 			DRM_ERROR("ib ring test failed (%d).\n", r);
2433 	}
2434 
2435 	r = amdgpu_late_init(adev);
2436 	if (r)
2437 		goto unlock;
2438 
2439 	/* pin cursors */
2440 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2441 		struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2442 
2443 		if (amdgpu_crtc->cursor_bo) {
2444 			struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2445 			r = amdgpu_bo_reserve(aobj, true);
2446 			if (r == 0) {
2447 				r = amdgpu_bo_pin(aobj,
2448 						  AMDGPU_GEM_DOMAIN_VRAM,
2449 						  &amdgpu_crtc->cursor_addr);
2450 				if (r != 0)
2451 					DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2452 				amdgpu_bo_unreserve(aobj);
2453 			}
2454 		}
2455 	}
2456 	r = amdgpu_amdkfd_resume(adev);
2457 	if (r)
2458 		return r;
2459 
2460 	/* blat the mode back in */
2461 	if (fbcon) {
2462 		drm_helper_resume_force_mode(dev);
2463 		/* turn on display hw */
2464 		drm_modeset_lock_all(dev);
2465 		list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2466 			drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2467 		}
2468 		drm_modeset_unlock_all(dev);
2469 	}
2470 
2471 	drm_kms_helper_poll_enable(dev);
2472 
2473 	/*
2474 	 * Most of the connector probing functions try to acquire runtime pm
2475 	 * refs to ensure that the GPU is powered on when connector polling is
2476 	 * performed. Since we're calling this from a runtime PM callback,
2477 	 * trying to acquire rpm refs will cause us to deadlock.
2478 	 *
2479 	 * Since we're guaranteed to be holding the rpm lock, it's safe to
2480 	 * temporarily disable the rpm helpers so this doesn't deadlock us.
2481 	 */
2482 #ifdef CONFIG_PM
2483 	dev->dev->power.disable_depth++;
2484 #endif
2485 	drm_helper_hpd_irq_event(dev);
2486 #ifdef CONFIG_PM
2487 	dev->dev->power.disable_depth--;
2488 #endif
2489 
2490 	if (fbcon)
2491 		amdgpu_fbdev_set_suspend(adev, 0);
2492 
2493 unlock:
2494 	if (fbcon)
2495 		console_unlock();
2496 
2497 	return r;
2498 }
2499 
amdgpu_check_soft_reset(struct amdgpu_device * adev)2500 static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2501 {
2502 	int i;
2503 	bool asic_hang = false;
2504 
2505 	for (i = 0; i < adev->num_ip_blocks; i++) {
2506 		if (!adev->ip_blocks[i].status.valid)
2507 			continue;
2508 		if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2509 			adev->ip_blocks[i].status.hang =
2510 				adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2511 		if (adev->ip_blocks[i].status.hang) {
2512 			DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
2513 			asic_hang = true;
2514 		}
2515 	}
2516 	return asic_hang;
2517 }
2518 
amdgpu_pre_soft_reset(struct amdgpu_device * adev)2519 static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
2520 {
2521 	int i, r = 0;
2522 
2523 	for (i = 0; i < adev->num_ip_blocks; i++) {
2524 		if (!adev->ip_blocks[i].status.valid)
2525 			continue;
2526 		if (adev->ip_blocks[i].status.hang &&
2527 		    adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2528 			r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
2529 			if (r)
2530 				return r;
2531 		}
2532 	}
2533 
2534 	return 0;
2535 }
2536 
amdgpu_need_full_reset(struct amdgpu_device * adev)2537 static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2538 {
2539 	int i;
2540 
2541 	for (i = 0; i < adev->num_ip_blocks; i++) {
2542 		if (!adev->ip_blocks[i].status.valid)
2543 			continue;
2544 		if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2545 		    (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2546 		    (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2547 		    (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)) {
2548 			if (adev->ip_blocks[i].status.hang) {
2549 				DRM_INFO("Some block need full reset!\n");
2550 				return true;
2551 			}
2552 		}
2553 	}
2554 	return false;
2555 }
2556 
amdgpu_soft_reset(struct amdgpu_device * adev)2557 static int amdgpu_soft_reset(struct amdgpu_device *adev)
2558 {
2559 	int i, r = 0;
2560 
2561 	for (i = 0; i < adev->num_ip_blocks; i++) {
2562 		if (!adev->ip_blocks[i].status.valid)
2563 			continue;
2564 		if (adev->ip_blocks[i].status.hang &&
2565 		    adev->ip_blocks[i].version->funcs->soft_reset) {
2566 			r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
2567 			if (r)
2568 				return r;
2569 		}
2570 	}
2571 
2572 	return 0;
2573 }
2574 
amdgpu_post_soft_reset(struct amdgpu_device * adev)2575 static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2576 {
2577 	int i, r = 0;
2578 
2579 	for (i = 0; i < adev->num_ip_blocks; i++) {
2580 		if (!adev->ip_blocks[i].status.valid)
2581 			continue;
2582 		if (adev->ip_blocks[i].status.hang &&
2583 		    adev->ip_blocks[i].version->funcs->post_soft_reset)
2584 			r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
2585 		if (r)
2586 			return r;
2587 	}
2588 
2589 	return 0;
2590 }
2591 
amdgpu_need_backup(struct amdgpu_device * adev)2592 bool amdgpu_need_backup(struct amdgpu_device *adev)
2593 {
2594 	if (adev->flags & AMD_IS_APU)
2595 		return false;
2596 
2597 	return amdgpu_lockup_timeout > 0 ? true : false;
2598 }
2599 
amdgpu_recover_vram_from_shadow(struct amdgpu_device * adev,struct amdgpu_ring * ring,struct amdgpu_bo * bo,struct dma_fence ** fence)2600 static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2601 					   struct amdgpu_ring *ring,
2602 					   struct amdgpu_bo *bo,
2603 					   struct dma_fence **fence)
2604 {
2605 	uint32_t domain;
2606 	int r;
2607 
2608 	if (!bo->shadow)
2609 		return 0;
2610 
2611 	r = amdgpu_bo_reserve(bo, true);
2612 	if (r)
2613 		return r;
2614 	domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2615 	/* if bo has been evicted, then no need to recover */
2616 	if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
2617 		r = amdgpu_bo_validate(bo->shadow);
2618 		if (r) {
2619 			DRM_ERROR("bo validate failed!\n");
2620 			goto err;
2621 		}
2622 
2623 		r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
2624 						 NULL, fence, true);
2625 		if (r) {
2626 			DRM_ERROR("recover page table failed!\n");
2627 			goto err;
2628 		}
2629 	}
2630 err:
2631 	amdgpu_bo_unreserve(bo);
2632 	return r;
2633 }
2634 
2635 /**
2636  * amdgpu_sriov_gpu_reset - reset the asic
2637  *
2638  * @adev: amdgpu device pointer
2639  * @job: which job trigger hang
2640  *
2641  * Attempt the reset the GPU if it has hung (all asics).
2642  * for SRIOV case.
2643  * Returns 0 for success or an error on failure.
2644  */
amdgpu_sriov_gpu_reset(struct amdgpu_device * adev,struct amdgpu_job * job)2645 int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, struct amdgpu_job *job)
2646 {
2647 	int i, j, r = 0;
2648 	int resched;
2649 	struct amdgpu_bo *bo, *tmp;
2650 	struct amdgpu_ring *ring;
2651 	struct dma_fence *fence = NULL, *next = NULL;
2652 
2653 	mutex_lock(&adev->virt.lock_reset);
2654 	atomic_inc(&adev->gpu_reset_counter);
2655 	adev->gfx.in_reset = true;
2656 
2657 	/* block TTM */
2658 	resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2659 
2660 	/* we start from the ring trigger GPU hang */
2661 	j = job ? job->ring->idx : 0;
2662 
2663 	/* block scheduler */
2664 	for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2665 		ring = adev->rings[i % AMDGPU_MAX_RINGS];
2666 		if (!ring || !ring->sched.thread)
2667 			continue;
2668 
2669 		kthread_park(ring->sched.thread);
2670 
2671 		if (job && j != i)
2672 			continue;
2673 
2674 		/* here give the last chance to check if job removed from mirror-list
2675 		 * since we already pay some time on kthread_park */
2676 		if (job && list_empty(&job->base.node)) {
2677 			kthread_unpark(ring->sched.thread);
2678 			goto give_up_reset;
2679 		}
2680 
2681 		if (amd_sched_invalidate_job(&job->base, amdgpu_job_hang_limit))
2682 			amd_sched_job_kickout(&job->base);
2683 
2684 		/* only do job_reset on the hang ring if @job not NULL */
2685 		amd_sched_hw_job_reset(&ring->sched);
2686 
2687 		/* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2688 		amdgpu_fence_driver_force_completion_ring(ring);
2689 	}
2690 
2691 	/* request to take full control of GPU before re-initialization  */
2692 	if (job)
2693 		amdgpu_virt_reset_gpu(adev);
2694 	else
2695 		amdgpu_virt_request_full_gpu(adev, true);
2696 
2697 
2698 	/* Resume IP prior to SMC */
2699 	amdgpu_sriov_reinit_early(adev);
2700 
2701 	/* we need recover gart prior to run SMC/CP/SDMA resume */
2702 	amdgpu_ttm_recover_gart(adev);
2703 
2704 	/* now we are okay to resume SMC/CP/SDMA */
2705 	amdgpu_sriov_reinit_late(adev);
2706 
2707 	amdgpu_irq_gpu_reset_resume_helper(adev);
2708 
2709 	if (amdgpu_ib_ring_tests(adev))
2710 		dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2711 
2712 	/* release full control of GPU after ib test */
2713 	amdgpu_virt_release_full_gpu(adev, true);
2714 
2715 	DRM_INFO("recover vram bo from shadow\n");
2716 
2717 	ring = adev->mman.buffer_funcs_ring;
2718 	mutex_lock(&adev->shadow_list_lock);
2719 	list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2720 		next = NULL;
2721 		amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2722 		if (fence) {
2723 			r = dma_fence_wait(fence, false);
2724 			if (r) {
2725 				WARN(r, "recovery from shadow isn't completed\n");
2726 				break;
2727 			}
2728 		}
2729 
2730 		dma_fence_put(fence);
2731 		fence = next;
2732 	}
2733 	mutex_unlock(&adev->shadow_list_lock);
2734 
2735 	if (fence) {
2736 		r = dma_fence_wait(fence, false);
2737 		if (r)
2738 			WARN(r, "recovery from shadow isn't completed\n");
2739 	}
2740 	dma_fence_put(fence);
2741 
2742 	for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2743 		ring = adev->rings[i % AMDGPU_MAX_RINGS];
2744 		if (!ring || !ring->sched.thread)
2745 			continue;
2746 
2747 		if (job && j != i) {
2748 			kthread_unpark(ring->sched.thread);
2749 			continue;
2750 		}
2751 
2752 		amd_sched_job_recovery(&ring->sched);
2753 		kthread_unpark(ring->sched.thread);
2754 	}
2755 
2756 	drm_helper_resume_force_mode(adev->ddev);
2757 give_up_reset:
2758 	ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2759 	if (r) {
2760 		/* bad news, how to tell it to userspace ? */
2761 		dev_info(adev->dev, "GPU reset failed\n");
2762 	} else {
2763 		dev_info(adev->dev, "GPU reset successed!\n");
2764 	}
2765 
2766 	adev->gfx.in_reset = false;
2767 	mutex_unlock(&adev->virt.lock_reset);
2768 	return r;
2769 }
2770 
2771 /**
2772  * amdgpu_gpu_reset - reset the asic
2773  *
2774  * @adev: amdgpu device pointer
2775  *
2776  * Attempt the reset the GPU if it has hung (all asics).
2777  * Returns 0 for success or an error on failure.
2778  */
amdgpu_gpu_reset(struct amdgpu_device * adev)2779 int amdgpu_gpu_reset(struct amdgpu_device *adev)
2780 {
2781 	int i, r;
2782 	int resched;
2783 	bool need_full_reset, vram_lost = false;
2784 
2785 	if (!amdgpu_check_soft_reset(adev)) {
2786 		DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2787 		return 0;
2788 	}
2789 
2790 	atomic_inc(&adev->gpu_reset_counter);
2791 
2792 	/* block TTM */
2793 	resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2794 
2795 	/* block scheduler */
2796 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2797 		struct amdgpu_ring *ring = adev->rings[i];
2798 
2799 		if (!ring || !ring->sched.thread)
2800 			continue;
2801 		kthread_park(ring->sched.thread);
2802 		amd_sched_hw_job_reset(&ring->sched);
2803 	}
2804 	/* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2805 	amdgpu_fence_driver_force_completion(adev);
2806 
2807 	need_full_reset = amdgpu_need_full_reset(adev);
2808 
2809 	if (!need_full_reset) {
2810 		amdgpu_pre_soft_reset(adev);
2811 		r = amdgpu_soft_reset(adev);
2812 		amdgpu_post_soft_reset(adev);
2813 		if (r || amdgpu_check_soft_reset(adev)) {
2814 			DRM_INFO("soft reset failed, will fallback to full reset!\n");
2815 			need_full_reset = true;
2816 		}
2817 	}
2818 
2819 	if (need_full_reset) {
2820 		r = amdgpu_suspend(adev);
2821 
2822 retry:
2823 		amdgpu_atombios_scratch_regs_save(adev);
2824 		r = amdgpu_asic_reset(adev);
2825 		amdgpu_atombios_scratch_regs_restore(adev);
2826 		/* post card */
2827 		amdgpu_atom_asic_init(adev->mode_info.atom_context);
2828 
2829 		if (!r) {
2830 			dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
2831 			r = amdgpu_resume_phase1(adev);
2832 			if (r)
2833 				goto out;
2834 			vram_lost = amdgpu_check_vram_lost(adev);
2835 			if (vram_lost) {
2836 				DRM_ERROR("VRAM is lost!\n");
2837 				atomic_inc(&adev->vram_lost_counter);
2838 			}
2839 			r = amdgpu_ttm_recover_gart(adev);
2840 			if (r)
2841 				goto out;
2842 			r = amdgpu_resume_phase2(adev);
2843 			if (r)
2844 				goto out;
2845 			if (vram_lost)
2846 				amdgpu_fill_reset_magic(adev);
2847 		}
2848 	}
2849 out:
2850 	if (!r) {
2851 		amdgpu_irq_gpu_reset_resume_helper(adev);
2852 		r = amdgpu_ib_ring_tests(adev);
2853 		if (r) {
2854 			dev_err(adev->dev, "ib ring test failed (%d).\n", r);
2855 			r = amdgpu_suspend(adev);
2856 			need_full_reset = true;
2857 			goto retry;
2858 		}
2859 		/**
2860 		 * recovery vm page tables, since we cannot depend on VRAM is
2861 		 * consistent after gpu full reset.
2862 		 */
2863 		if (need_full_reset && amdgpu_need_backup(adev)) {
2864 			struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2865 			struct amdgpu_bo *bo, *tmp;
2866 			struct dma_fence *fence = NULL, *next = NULL;
2867 
2868 			DRM_INFO("recover vram bo from shadow\n");
2869 			mutex_lock(&adev->shadow_list_lock);
2870 			list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2871 				next = NULL;
2872 				amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2873 				if (fence) {
2874 					r = dma_fence_wait(fence, false);
2875 					if (r) {
2876 						WARN(r, "recovery from shadow isn't completed\n");
2877 						break;
2878 					}
2879 				}
2880 
2881 				dma_fence_put(fence);
2882 				fence = next;
2883 			}
2884 			mutex_unlock(&adev->shadow_list_lock);
2885 			if (fence) {
2886 				r = dma_fence_wait(fence, false);
2887 				if (r)
2888 					WARN(r, "recovery from shadow isn't completed\n");
2889 			}
2890 			dma_fence_put(fence);
2891 		}
2892 		for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2893 			struct amdgpu_ring *ring = adev->rings[i];
2894 
2895 			if (!ring || !ring->sched.thread)
2896 				continue;
2897 
2898 			amd_sched_job_recovery(&ring->sched);
2899 			kthread_unpark(ring->sched.thread);
2900 		}
2901 	} else {
2902 		dev_err(adev->dev, "asic resume failed (%d).\n", r);
2903 		amdgpu_vf_error_put(AMDGIM_ERROR_VF_ASIC_RESUME_FAIL, 0, r);
2904 		for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2905 			if (adev->rings[i] && adev->rings[i]->sched.thread) {
2906 				kthread_unpark(adev->rings[i]->sched.thread);
2907 			}
2908 		}
2909 	}
2910 
2911 	drm_helper_resume_force_mode(adev->ddev);
2912 
2913 	ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2914 	if (r) {
2915 		/* bad news, how to tell it to userspace ? */
2916 		dev_info(adev->dev, "GPU reset failed\n");
2917 		amdgpu_vf_error_put(AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
2918 	}
2919 	else {
2920 		dev_info(adev->dev, "GPU reset successed!\n");
2921 	}
2922 
2923 	amdgpu_vf_error_trans_all(adev);
2924 	return r;
2925 }
2926 
amdgpu_get_pcie_info(struct amdgpu_device * adev)2927 void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2928 {
2929 	u32 mask;
2930 	int ret;
2931 
2932 	if (amdgpu_pcie_gen_cap)
2933 		adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2934 
2935 	if (amdgpu_pcie_lane_cap)
2936 		adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2937 
2938 	/* covers APUs as well */
2939 	if (pci_is_root_bus(adev->pdev->bus)) {
2940 		if (adev->pm.pcie_gen_mask == 0)
2941 			adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2942 		if (adev->pm.pcie_mlw_mask == 0)
2943 			adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
2944 		return;
2945 	}
2946 
2947 	if (adev->pm.pcie_gen_mask == 0) {
2948 		ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2949 		if (!ret) {
2950 			adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2951 						  CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2952 						  CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2953 
2954 			if (mask & DRM_PCIE_SPEED_25)
2955 				adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2956 			if (mask & DRM_PCIE_SPEED_50)
2957 				adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2958 			if (mask & DRM_PCIE_SPEED_80)
2959 				adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2960 		} else {
2961 			adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2962 		}
2963 	}
2964 	if (adev->pm.pcie_mlw_mask == 0) {
2965 		ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2966 		if (!ret) {
2967 			switch (mask) {
2968 			case 32:
2969 				adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
2970 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2971 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2972 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2973 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2974 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2975 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2976 				break;
2977 			case 16:
2978 				adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2979 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2980 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2981 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2982 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2983 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2984 				break;
2985 			case 12:
2986 				adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2987 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2988 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2989 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2990 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2991 				break;
2992 			case 8:
2993 				adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2994 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2995 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2996 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2997 				break;
2998 			case 4:
2999 				adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3000 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3001 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3002 				break;
3003 			case 2:
3004 				adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3005 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3006 				break;
3007 			case 1:
3008 				adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3009 				break;
3010 			default:
3011 				break;
3012 			}
3013 		} else {
3014 			adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
3015 		}
3016 	}
3017 }
3018 
3019 /*
3020  * Debugfs
3021  */
amdgpu_debugfs_add_files(struct amdgpu_device * adev,const struct drm_info_list * files,unsigned nfiles)3022 int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
3023 			     const struct drm_info_list *files,
3024 			     unsigned nfiles)
3025 {
3026 	unsigned i;
3027 
3028 	for (i = 0; i < adev->debugfs_count; i++) {
3029 		if (adev->debugfs[i].files == files) {
3030 			/* Already registered */
3031 			return 0;
3032 		}
3033 	}
3034 
3035 	i = adev->debugfs_count + 1;
3036 	if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
3037 		DRM_ERROR("Reached maximum number of debugfs components.\n");
3038 		DRM_ERROR("Report so we increase "
3039 			  "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
3040 		return -EINVAL;
3041 	}
3042 	adev->debugfs[adev->debugfs_count].files = files;
3043 	adev->debugfs[adev->debugfs_count].num_files = nfiles;
3044 	adev->debugfs_count = i;
3045 #if defined(CONFIG_DEBUG_FS)
3046 	drm_debugfs_create_files(files, nfiles,
3047 				 adev->ddev->primary->debugfs_root,
3048 				 adev->ddev->primary);
3049 #endif
3050 	return 0;
3051 }
3052 
3053 #if defined(CONFIG_DEBUG_FS)
3054 
amdgpu_debugfs_regs_read(struct file * f,char __user * buf,size_t size,loff_t * pos)3055 static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
3056 					size_t size, loff_t *pos)
3057 {
3058 	struct amdgpu_device *adev = file_inode(f)->i_private;
3059 	ssize_t result = 0;
3060 	int r;
3061 	bool pm_pg_lock, use_bank;
3062 	unsigned instance_bank, sh_bank, se_bank;
3063 
3064 	if (size & 0x3 || *pos & 0x3)
3065 		return -EINVAL;
3066 
3067 	/* are we reading registers for which a PG lock is necessary? */
3068 	pm_pg_lock = (*pos >> 23) & 1;
3069 
3070 	if (*pos & (1ULL << 62)) {
3071 		se_bank = (*pos >> 24) & 0x3FF;
3072 		sh_bank = (*pos >> 34) & 0x3FF;
3073 		instance_bank = (*pos >> 44) & 0x3FF;
3074 
3075 		if (se_bank == 0x3FF)
3076 			se_bank = 0xFFFFFFFF;
3077 		if (sh_bank == 0x3FF)
3078 			sh_bank = 0xFFFFFFFF;
3079 		if (instance_bank == 0x3FF)
3080 			instance_bank = 0xFFFFFFFF;
3081 		use_bank = 1;
3082 	} else {
3083 		use_bank = 0;
3084 	}
3085 
3086 	*pos &= (1UL << 22) - 1;
3087 
3088 	if (use_bank) {
3089 		if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3090 		    (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3091 			return -EINVAL;
3092 		mutex_lock(&adev->grbm_idx_mutex);
3093 		amdgpu_gfx_select_se_sh(adev, se_bank,
3094 					sh_bank, instance_bank);
3095 	}
3096 
3097 	if (pm_pg_lock)
3098 		mutex_lock(&adev->pm.mutex);
3099 
3100 	while (size) {
3101 		uint32_t value;
3102 
3103 		if (*pos > adev->rmmio_size)
3104 			goto end;
3105 
3106 		value = RREG32(*pos >> 2);
3107 		r = put_user(value, (uint32_t *)buf);
3108 		if (r) {
3109 			result = r;
3110 			goto end;
3111 		}
3112 
3113 		result += 4;
3114 		buf += 4;
3115 		*pos += 4;
3116 		size -= 4;
3117 	}
3118 
3119 end:
3120 	if (use_bank) {
3121 		amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3122 		mutex_unlock(&adev->grbm_idx_mutex);
3123 	}
3124 
3125 	if (pm_pg_lock)
3126 		mutex_unlock(&adev->pm.mutex);
3127 
3128 	return result;
3129 }
3130 
amdgpu_debugfs_regs_write(struct file * f,const char __user * buf,size_t size,loff_t * pos)3131 static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
3132 					 size_t size, loff_t *pos)
3133 {
3134 	struct amdgpu_device *adev = file_inode(f)->i_private;
3135 	ssize_t result = 0;
3136 	int r;
3137 	bool pm_pg_lock, use_bank;
3138 	unsigned instance_bank, sh_bank, se_bank;
3139 
3140 	if (size & 0x3 || *pos & 0x3)
3141 		return -EINVAL;
3142 
3143 	/* are we reading registers for which a PG lock is necessary? */
3144 	pm_pg_lock = (*pos >> 23) & 1;
3145 
3146 	if (*pos & (1ULL << 62)) {
3147 		se_bank = (*pos >> 24) & 0x3FF;
3148 		sh_bank = (*pos >> 34) & 0x3FF;
3149 		instance_bank = (*pos >> 44) & 0x3FF;
3150 
3151 		if (se_bank == 0x3FF)
3152 			se_bank = 0xFFFFFFFF;
3153 		if (sh_bank == 0x3FF)
3154 			sh_bank = 0xFFFFFFFF;
3155 		if (instance_bank == 0x3FF)
3156 			instance_bank = 0xFFFFFFFF;
3157 		use_bank = 1;
3158 	} else {
3159 		use_bank = 0;
3160 	}
3161 
3162 	*pos &= (1UL << 22) - 1;
3163 
3164 	if (use_bank) {
3165 		if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3166 		    (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3167 			return -EINVAL;
3168 		mutex_lock(&adev->grbm_idx_mutex);
3169 		amdgpu_gfx_select_se_sh(adev, se_bank,
3170 					sh_bank, instance_bank);
3171 	}
3172 
3173 	if (pm_pg_lock)
3174 		mutex_lock(&adev->pm.mutex);
3175 
3176 	while (size) {
3177 		uint32_t value;
3178 
3179 		if (*pos > adev->rmmio_size)
3180 			return result;
3181 
3182 		r = get_user(value, (uint32_t *)buf);
3183 		if (r)
3184 			return r;
3185 
3186 		WREG32(*pos >> 2, value);
3187 
3188 		result += 4;
3189 		buf += 4;
3190 		*pos += 4;
3191 		size -= 4;
3192 	}
3193 
3194 	if (use_bank) {
3195 		amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3196 		mutex_unlock(&adev->grbm_idx_mutex);
3197 	}
3198 
3199 	if (pm_pg_lock)
3200 		mutex_unlock(&adev->pm.mutex);
3201 
3202 	return result;
3203 }
3204 
amdgpu_debugfs_regs_pcie_read(struct file * f,char __user * buf,size_t size,loff_t * pos)3205 static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
3206 					size_t size, loff_t *pos)
3207 {
3208 	struct amdgpu_device *adev = file_inode(f)->i_private;
3209 	ssize_t result = 0;
3210 	int r;
3211 
3212 	if (size & 0x3 || *pos & 0x3)
3213 		return -EINVAL;
3214 
3215 	while (size) {
3216 		uint32_t value;
3217 
3218 		value = RREG32_PCIE(*pos >> 2);
3219 		r = put_user(value, (uint32_t *)buf);
3220 		if (r)
3221 			return r;
3222 
3223 		result += 4;
3224 		buf += 4;
3225 		*pos += 4;
3226 		size -= 4;
3227 	}
3228 
3229 	return result;
3230 }
3231 
amdgpu_debugfs_regs_pcie_write(struct file * f,const char __user * buf,size_t size,loff_t * pos)3232 static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
3233 					 size_t size, loff_t *pos)
3234 {
3235 	struct amdgpu_device *adev = file_inode(f)->i_private;
3236 	ssize_t result = 0;
3237 	int r;
3238 
3239 	if (size & 0x3 || *pos & 0x3)
3240 		return -EINVAL;
3241 
3242 	while (size) {
3243 		uint32_t value;
3244 
3245 		r = get_user(value, (uint32_t *)buf);
3246 		if (r)
3247 			return r;
3248 
3249 		WREG32_PCIE(*pos >> 2, value);
3250 
3251 		result += 4;
3252 		buf += 4;
3253 		*pos += 4;
3254 		size -= 4;
3255 	}
3256 
3257 	return result;
3258 }
3259 
amdgpu_debugfs_regs_didt_read(struct file * f,char __user * buf,size_t size,loff_t * pos)3260 static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
3261 					size_t size, loff_t *pos)
3262 {
3263 	struct amdgpu_device *adev = file_inode(f)->i_private;
3264 	ssize_t result = 0;
3265 	int r;
3266 
3267 	if (size & 0x3 || *pos & 0x3)
3268 		return -EINVAL;
3269 
3270 	while (size) {
3271 		uint32_t value;
3272 
3273 		value = RREG32_DIDT(*pos >> 2);
3274 		r = put_user(value, (uint32_t *)buf);
3275 		if (r)
3276 			return r;
3277 
3278 		result += 4;
3279 		buf += 4;
3280 		*pos += 4;
3281 		size -= 4;
3282 	}
3283 
3284 	return result;
3285 }
3286 
amdgpu_debugfs_regs_didt_write(struct file * f,const char __user * buf,size_t size,loff_t * pos)3287 static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3288 					 size_t size, loff_t *pos)
3289 {
3290 	struct amdgpu_device *adev = file_inode(f)->i_private;
3291 	ssize_t result = 0;
3292 	int r;
3293 
3294 	if (size & 0x3 || *pos & 0x3)
3295 		return -EINVAL;
3296 
3297 	while (size) {
3298 		uint32_t value;
3299 
3300 		r = get_user(value, (uint32_t *)buf);
3301 		if (r)
3302 			return r;
3303 
3304 		WREG32_DIDT(*pos >> 2, value);
3305 
3306 		result += 4;
3307 		buf += 4;
3308 		*pos += 4;
3309 		size -= 4;
3310 	}
3311 
3312 	return result;
3313 }
3314 
amdgpu_debugfs_regs_smc_read(struct file * f,char __user * buf,size_t size,loff_t * pos)3315 static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3316 					size_t size, loff_t *pos)
3317 {
3318 	struct amdgpu_device *adev = file_inode(f)->i_private;
3319 	ssize_t result = 0;
3320 	int r;
3321 
3322 	if (size & 0x3 || *pos & 0x3)
3323 		return -EINVAL;
3324 
3325 	while (size) {
3326 		uint32_t value;
3327 
3328 		value = RREG32_SMC(*pos);
3329 		r = put_user(value, (uint32_t *)buf);
3330 		if (r)
3331 			return r;
3332 
3333 		result += 4;
3334 		buf += 4;
3335 		*pos += 4;
3336 		size -= 4;
3337 	}
3338 
3339 	return result;
3340 }
3341 
amdgpu_debugfs_regs_smc_write(struct file * f,const char __user * buf,size_t size,loff_t * pos)3342 static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3343 					 size_t size, loff_t *pos)
3344 {
3345 	struct amdgpu_device *adev = file_inode(f)->i_private;
3346 	ssize_t result = 0;
3347 	int r;
3348 
3349 	if (size & 0x3 || *pos & 0x3)
3350 		return -EINVAL;
3351 
3352 	while (size) {
3353 		uint32_t value;
3354 
3355 		r = get_user(value, (uint32_t *)buf);
3356 		if (r)
3357 			return r;
3358 
3359 		WREG32_SMC(*pos, value);
3360 
3361 		result += 4;
3362 		buf += 4;
3363 		*pos += 4;
3364 		size -= 4;
3365 	}
3366 
3367 	return result;
3368 }
3369 
amdgpu_debugfs_gca_config_read(struct file * f,char __user * buf,size_t size,loff_t * pos)3370 static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3371 					size_t size, loff_t *pos)
3372 {
3373 	struct amdgpu_device *adev = file_inode(f)->i_private;
3374 	ssize_t result = 0;
3375 	int r;
3376 	uint32_t *config, no_regs = 0;
3377 
3378 	if (size & 0x3 || *pos & 0x3)
3379 		return -EINVAL;
3380 
3381 	config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
3382 	if (!config)
3383 		return -ENOMEM;
3384 
3385 	/* version, increment each time something is added */
3386 	config[no_regs++] = 3;
3387 	config[no_regs++] = adev->gfx.config.max_shader_engines;
3388 	config[no_regs++] = adev->gfx.config.max_tile_pipes;
3389 	config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3390 	config[no_regs++] = adev->gfx.config.max_sh_per_se;
3391 	config[no_regs++] = adev->gfx.config.max_backends_per_se;
3392 	config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3393 	config[no_regs++] = adev->gfx.config.max_gprs;
3394 	config[no_regs++] = adev->gfx.config.max_gs_threads;
3395 	config[no_regs++] = adev->gfx.config.max_hw_contexts;
3396 	config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3397 	config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3398 	config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3399 	config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3400 	config[no_regs++] = adev->gfx.config.num_tile_pipes;
3401 	config[no_regs++] = adev->gfx.config.backend_enable_mask;
3402 	config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3403 	config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3404 	config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3405 	config[no_regs++] = adev->gfx.config.num_gpus;
3406 	config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3407 	config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3408 	config[no_regs++] = adev->gfx.config.gb_addr_config;
3409 	config[no_regs++] = adev->gfx.config.num_rbs;
3410 
3411 	/* rev==1 */
3412 	config[no_regs++] = adev->rev_id;
3413 	config[no_regs++] = adev->pg_flags;
3414 	config[no_regs++] = adev->cg_flags;
3415 
3416 	/* rev==2 */
3417 	config[no_regs++] = adev->family;
3418 	config[no_regs++] = adev->external_rev_id;
3419 
3420 	/* rev==3 */
3421 	config[no_regs++] = adev->pdev->device;
3422 	config[no_regs++] = adev->pdev->revision;
3423 	config[no_regs++] = adev->pdev->subsystem_device;
3424 	config[no_regs++] = adev->pdev->subsystem_vendor;
3425 
3426 	while (size && (*pos < no_regs * 4)) {
3427 		uint32_t value;
3428 
3429 		value = config[*pos >> 2];
3430 		r = put_user(value, (uint32_t *)buf);
3431 		if (r) {
3432 			kfree(config);
3433 			return r;
3434 		}
3435 
3436 		result += 4;
3437 		buf += 4;
3438 		*pos += 4;
3439 		size -= 4;
3440 	}
3441 
3442 	kfree(config);
3443 	return result;
3444 }
3445 
amdgpu_debugfs_sensor_read(struct file * f,char __user * buf,size_t size,loff_t * pos)3446 static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
3447 					size_t size, loff_t *pos)
3448 {
3449 	struct amdgpu_device *adev = file_inode(f)->i_private;
3450 	int idx, x, outsize, r, valuesize;
3451 	uint32_t values[16];
3452 
3453 	if (size & 3 || *pos & 0x3)
3454 		return -EINVAL;
3455 
3456 	if (amdgpu_dpm == 0)
3457 		return -EINVAL;
3458 
3459 	/* convert offset to sensor number */
3460 	idx = *pos >> 2;
3461 
3462 	valuesize = sizeof(values);
3463 	if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
3464 		r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &values[0], &valuesize);
3465 	else if (adev->pm.funcs && adev->pm.funcs->read_sensor)
3466 		r = adev->pm.funcs->read_sensor(adev, idx, &values[0],
3467 						&valuesize);
3468 	else
3469 		return -EINVAL;
3470 
3471 	if (size > valuesize)
3472 		return -EINVAL;
3473 
3474 	outsize = 0;
3475 	x = 0;
3476 	if (!r) {
3477 		while (size) {
3478 			r = put_user(values[x++], (int32_t *)buf);
3479 			buf += 4;
3480 			size -= 4;
3481 			outsize += 4;
3482 		}
3483 	}
3484 
3485 	return !r ? outsize : r;
3486 }
3487 
amdgpu_debugfs_wave_read(struct file * f,char __user * buf,size_t size,loff_t * pos)3488 static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3489 					size_t size, loff_t *pos)
3490 {
3491 	struct amdgpu_device *adev = f->f_inode->i_private;
3492 	int r, x;
3493 	ssize_t result=0;
3494 	uint32_t offset, se, sh, cu, wave, simd, data[32];
3495 
3496 	if (size & 3 || *pos & 3)
3497 		return -EINVAL;
3498 
3499 	/* decode offset */
3500 	offset = (*pos & 0x7F);
3501 	se = ((*pos >> 7) & 0xFF);
3502 	sh = ((*pos >> 15) & 0xFF);
3503 	cu = ((*pos >> 23) & 0xFF);
3504 	wave = ((*pos >> 31) & 0xFF);
3505 	simd = ((*pos >> 37) & 0xFF);
3506 
3507 	/* switch to the specific se/sh/cu */
3508 	mutex_lock(&adev->grbm_idx_mutex);
3509 	amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3510 
3511 	x = 0;
3512 	if (adev->gfx.funcs->read_wave_data)
3513 		adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
3514 
3515 	amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3516 	mutex_unlock(&adev->grbm_idx_mutex);
3517 
3518 	if (!x)
3519 		return -EINVAL;
3520 
3521 	while (size && (offset < x * 4)) {
3522 		uint32_t value;
3523 
3524 		value = data[offset >> 2];
3525 		r = put_user(value, (uint32_t *)buf);
3526 		if (r)
3527 			return r;
3528 
3529 		result += 4;
3530 		buf += 4;
3531 		offset += 4;
3532 		size -= 4;
3533 	}
3534 
3535 	return result;
3536 }
3537 
amdgpu_debugfs_gpr_read(struct file * f,char __user * buf,size_t size,loff_t * pos)3538 static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3539 					size_t size, loff_t *pos)
3540 {
3541 	struct amdgpu_device *adev = f->f_inode->i_private;
3542 	int r;
3543 	ssize_t result = 0;
3544 	uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3545 
3546 	if (size & 3 || *pos & 3)
3547 		return -EINVAL;
3548 
3549 	/* decode offset */
3550 	offset = (*pos & 0xFFF);       /* in dwords */
3551 	se = ((*pos >> 12) & 0xFF);
3552 	sh = ((*pos >> 20) & 0xFF);
3553 	cu = ((*pos >> 28) & 0xFF);
3554 	wave = ((*pos >> 36) & 0xFF);
3555 	simd = ((*pos >> 44) & 0xFF);
3556 	thread = ((*pos >> 52) & 0xFF);
3557 	bank = ((*pos >> 60) & 1);
3558 
3559 	data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3560 	if (!data)
3561 		return -ENOMEM;
3562 
3563 	/* switch to the specific se/sh/cu */
3564 	mutex_lock(&adev->grbm_idx_mutex);
3565 	amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3566 
3567 	if (bank == 0) {
3568 		if (adev->gfx.funcs->read_wave_vgprs)
3569 			adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3570 	} else {
3571 		if (adev->gfx.funcs->read_wave_sgprs)
3572 			adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3573 	}
3574 
3575 	amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3576 	mutex_unlock(&adev->grbm_idx_mutex);
3577 
3578 	while (size) {
3579 		uint32_t value;
3580 
3581 		value = data[offset++];
3582 		r = put_user(value, (uint32_t *)buf);
3583 		if (r) {
3584 			result = r;
3585 			goto err;
3586 		}
3587 
3588 		result += 4;
3589 		buf += 4;
3590 		size -= 4;
3591 	}
3592 
3593 err:
3594 	kfree(data);
3595 	return result;
3596 }
3597 
3598 static const struct file_operations amdgpu_debugfs_regs_fops = {
3599 	.owner = THIS_MODULE,
3600 	.read = amdgpu_debugfs_regs_read,
3601 	.write = amdgpu_debugfs_regs_write,
3602 	.llseek = default_llseek
3603 };
3604 static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3605 	.owner = THIS_MODULE,
3606 	.read = amdgpu_debugfs_regs_didt_read,
3607 	.write = amdgpu_debugfs_regs_didt_write,
3608 	.llseek = default_llseek
3609 };
3610 static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3611 	.owner = THIS_MODULE,
3612 	.read = amdgpu_debugfs_regs_pcie_read,
3613 	.write = amdgpu_debugfs_regs_pcie_write,
3614 	.llseek = default_llseek
3615 };
3616 static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3617 	.owner = THIS_MODULE,
3618 	.read = amdgpu_debugfs_regs_smc_read,
3619 	.write = amdgpu_debugfs_regs_smc_write,
3620 	.llseek = default_llseek
3621 };
3622 
3623 static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3624 	.owner = THIS_MODULE,
3625 	.read = amdgpu_debugfs_gca_config_read,
3626 	.llseek = default_llseek
3627 };
3628 
3629 static const struct file_operations amdgpu_debugfs_sensors_fops = {
3630 	.owner = THIS_MODULE,
3631 	.read = amdgpu_debugfs_sensor_read,
3632 	.llseek = default_llseek
3633 };
3634 
3635 static const struct file_operations amdgpu_debugfs_wave_fops = {
3636 	.owner = THIS_MODULE,
3637 	.read = amdgpu_debugfs_wave_read,
3638 	.llseek = default_llseek
3639 };
3640 static const struct file_operations amdgpu_debugfs_gpr_fops = {
3641 	.owner = THIS_MODULE,
3642 	.read = amdgpu_debugfs_gpr_read,
3643 	.llseek = default_llseek
3644 };
3645 
3646 static const struct file_operations *debugfs_regs[] = {
3647 	&amdgpu_debugfs_regs_fops,
3648 	&amdgpu_debugfs_regs_didt_fops,
3649 	&amdgpu_debugfs_regs_pcie_fops,
3650 	&amdgpu_debugfs_regs_smc_fops,
3651 	&amdgpu_debugfs_gca_config_fops,
3652 	&amdgpu_debugfs_sensors_fops,
3653 	&amdgpu_debugfs_wave_fops,
3654 	&amdgpu_debugfs_gpr_fops,
3655 };
3656 
3657 static const char *debugfs_regs_names[] = {
3658 	"amdgpu_regs",
3659 	"amdgpu_regs_didt",
3660 	"amdgpu_regs_pcie",
3661 	"amdgpu_regs_smc",
3662 	"amdgpu_gca_config",
3663 	"amdgpu_sensors",
3664 	"amdgpu_wave",
3665 	"amdgpu_gpr",
3666 };
3667 
amdgpu_debugfs_regs_init(struct amdgpu_device * adev)3668 static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3669 {
3670 	struct drm_minor *minor = adev->ddev->primary;
3671 	struct dentry *ent, *root = minor->debugfs_root;
3672 	unsigned i, j;
3673 
3674 	for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3675 		ent = debugfs_create_file(debugfs_regs_names[i],
3676 					  S_IFREG | S_IRUGO, root,
3677 					  adev, debugfs_regs[i]);
3678 		if (IS_ERR(ent)) {
3679 			for (j = 0; j < i; j++) {
3680 				debugfs_remove(adev->debugfs_regs[i]);
3681 				adev->debugfs_regs[i] = NULL;
3682 			}
3683 			return PTR_ERR(ent);
3684 		}
3685 
3686 		if (!i)
3687 			i_size_write(ent->d_inode, adev->rmmio_size);
3688 		adev->debugfs_regs[i] = ent;
3689 	}
3690 
3691 	return 0;
3692 }
3693 
amdgpu_debugfs_regs_cleanup(struct amdgpu_device * adev)3694 static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3695 {
3696 	unsigned i;
3697 
3698 	for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3699 		if (adev->debugfs_regs[i]) {
3700 			debugfs_remove(adev->debugfs_regs[i]);
3701 			adev->debugfs_regs[i] = NULL;
3702 		}
3703 	}
3704 }
3705 
amdgpu_debugfs_test_ib(struct seq_file * m,void * data)3706 static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data)
3707 {
3708 	struct drm_info_node *node = (struct drm_info_node *) m->private;
3709 	struct drm_device *dev = node->minor->dev;
3710 	struct amdgpu_device *adev = dev->dev_private;
3711 	int r = 0, i;
3712 
3713 	/* hold on the scheduler */
3714 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3715 		struct amdgpu_ring *ring = adev->rings[i];
3716 
3717 		if (!ring || !ring->sched.thread)
3718 			continue;
3719 		kthread_park(ring->sched.thread);
3720 	}
3721 
3722 	seq_printf(m, "run ib test:\n");
3723 	r = amdgpu_ib_ring_tests(adev);
3724 	if (r)
3725 		seq_printf(m, "ib ring tests failed (%d).\n", r);
3726 	else
3727 		seq_printf(m, "ib ring tests passed.\n");
3728 
3729 	/* go on the scheduler */
3730 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3731 		struct amdgpu_ring *ring = adev->rings[i];
3732 
3733 		if (!ring || !ring->sched.thread)
3734 			continue;
3735 		kthread_unpark(ring->sched.thread);
3736 	}
3737 
3738 	return 0;
3739 }
3740 
3741 static const struct drm_info_list amdgpu_debugfs_test_ib_ring_list[] = {
3742 	{"amdgpu_test_ib", &amdgpu_debugfs_test_ib}
3743 };
3744 
amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device * adev)3745 static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
3746 {
3747 	return amdgpu_debugfs_add_files(adev,
3748 					amdgpu_debugfs_test_ib_ring_list, 1);
3749 }
3750 
amdgpu_debugfs_init(struct drm_minor * minor)3751 int amdgpu_debugfs_init(struct drm_minor *minor)
3752 {
3753 	return 0;
3754 }
3755 #else
amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device * adev)3756 static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
3757 {
3758 	return 0;
3759 }
amdgpu_debugfs_regs_init(struct amdgpu_device * adev)3760 static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3761 {
3762 	return 0;
3763 }
amdgpu_debugfs_regs_cleanup(struct amdgpu_device * adev)3764 static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
3765 #endif
3766