xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c (revision a8fe58cec351c25e09c393bf46117c0c47b5a17c)
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/console.h>
29 #include <linux/slab.h>
30 #include <linux/debugfs.h>
31 #include <drm/drmP.h>
32 #include <drm/drm_crtc_helper.h>
33 #include <drm/amdgpu_drm.h>
34 #include <linux/vgaarb.h>
35 #include <linux/vga_switcheroo.h>
36 #include <linux/efi.h>
37 #include "amdgpu.h"
38 #include "amdgpu_i2c.h"
39 #include "atom.h"
40 #include "amdgpu_atombios.h"
41 #include "amd_pcie.h"
42 #ifdef CONFIG_DRM_AMDGPU_CIK
43 #include "cik.h"
44 #endif
45 #include "vi.h"
46 #include "bif/bif_4_1_d.h"
47 
48 static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
49 static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
50 
51 static const char *amdgpu_asic_name[] = {
52 	"BONAIRE",
53 	"KAVERI",
54 	"KABINI",
55 	"HAWAII",
56 	"MULLINS",
57 	"TOPAZ",
58 	"TONGA",
59 	"FIJI",
60 	"CARRIZO",
61 	"STONEY",
62 	"LAST",
63 };
64 
65 bool amdgpu_device_is_px(struct drm_device *dev)
66 {
67 	struct amdgpu_device *adev = dev->dev_private;
68 
69 	if (adev->flags & AMD_IS_PX)
70 		return true;
71 	return false;
72 }
73 
74 /*
75  * MMIO register access helper functions.
76  */
77 uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
78 			bool always_indirect)
79 {
80 	if ((reg * 4) < adev->rmmio_size && !always_indirect)
81 		return readl(((void __iomem *)adev->rmmio) + (reg * 4));
82 	else {
83 		unsigned long flags;
84 		uint32_t ret;
85 
86 		spin_lock_irqsave(&adev->mmio_idx_lock, flags);
87 		writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
88 		ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
89 		spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
90 
91 		return ret;
92 	}
93 }
94 
95 void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
96 		    bool always_indirect)
97 {
98 	if ((reg * 4) < adev->rmmio_size && !always_indirect)
99 		writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
100 	else {
101 		unsigned long flags;
102 
103 		spin_lock_irqsave(&adev->mmio_idx_lock, flags);
104 		writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
105 		writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
106 		spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
107 	}
108 }
109 
110 u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
111 {
112 	if ((reg * 4) < adev->rio_mem_size)
113 		return ioread32(adev->rio_mem + (reg * 4));
114 	else {
115 		iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
116 		return ioread32(adev->rio_mem + (mmMM_DATA * 4));
117 	}
118 }
119 
120 void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
121 {
122 
123 	if ((reg * 4) < adev->rio_mem_size)
124 		iowrite32(v, adev->rio_mem + (reg * 4));
125 	else {
126 		iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
127 		iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
128 	}
129 }
130 
131 /**
132  * amdgpu_mm_rdoorbell - read a doorbell dword
133  *
134  * @adev: amdgpu_device pointer
135  * @index: doorbell index
136  *
137  * Returns the value in the doorbell aperture at the
138  * requested doorbell index (CIK).
139  */
140 u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
141 {
142 	if (index < adev->doorbell.num_doorbells) {
143 		return readl(adev->doorbell.ptr + index);
144 	} else {
145 		DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
146 		return 0;
147 	}
148 }
149 
150 /**
151  * amdgpu_mm_wdoorbell - write a doorbell dword
152  *
153  * @adev: amdgpu_device pointer
154  * @index: doorbell index
155  * @v: value to write
156  *
157  * Writes @v to the doorbell aperture at the
158  * requested doorbell index (CIK).
159  */
160 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
161 {
162 	if (index < adev->doorbell.num_doorbells) {
163 		writel(v, adev->doorbell.ptr + index);
164 	} else {
165 		DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
166 	}
167 }
168 
169 /**
170  * amdgpu_invalid_rreg - dummy reg read function
171  *
172  * @adev: amdgpu device pointer
173  * @reg: offset of register
174  *
175  * Dummy register read function.  Used for register blocks
176  * that certain asics don't have (all asics).
177  * Returns the value in the register.
178  */
179 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
180 {
181 	DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
182 	BUG();
183 	return 0;
184 }
185 
186 /**
187  * amdgpu_invalid_wreg - dummy reg write function
188  *
189  * @adev: amdgpu device pointer
190  * @reg: offset of register
191  * @v: value to write to the register
192  *
193  * Dummy register read function.  Used for register blocks
194  * that certain asics don't have (all asics).
195  */
196 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
197 {
198 	DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
199 		  reg, v);
200 	BUG();
201 }
202 
203 /**
204  * amdgpu_block_invalid_rreg - dummy reg read function
205  *
206  * @adev: amdgpu device pointer
207  * @block: offset of instance
208  * @reg: offset of register
209  *
210  * Dummy register read function.  Used for register blocks
211  * that certain asics don't have (all asics).
212  * Returns the value in the register.
213  */
214 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
215 					  uint32_t block, uint32_t reg)
216 {
217 	DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
218 		  reg, block);
219 	BUG();
220 	return 0;
221 }
222 
223 /**
224  * amdgpu_block_invalid_wreg - dummy reg write function
225  *
226  * @adev: amdgpu device pointer
227  * @block: offset of instance
228  * @reg: offset of register
229  * @v: value to write to the register
230  *
231  * Dummy register read function.  Used for register blocks
232  * that certain asics don't have (all asics).
233  */
234 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
235 				      uint32_t block,
236 				      uint32_t reg, uint32_t v)
237 {
238 	DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
239 		  reg, block, v);
240 	BUG();
241 }
242 
243 static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
244 {
245 	int r;
246 
247 	if (adev->vram_scratch.robj == NULL) {
248 		r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE,
249 				     PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
250 				     AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
251 				     NULL, NULL, &adev->vram_scratch.robj);
252 		if (r) {
253 			return r;
254 		}
255 	}
256 
257 	r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
258 	if (unlikely(r != 0))
259 		return r;
260 	r = amdgpu_bo_pin(adev->vram_scratch.robj,
261 			  AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
262 	if (r) {
263 		amdgpu_bo_unreserve(adev->vram_scratch.robj);
264 		return r;
265 	}
266 	r = amdgpu_bo_kmap(adev->vram_scratch.robj,
267 				(void **)&adev->vram_scratch.ptr);
268 	if (r)
269 		amdgpu_bo_unpin(adev->vram_scratch.robj);
270 	amdgpu_bo_unreserve(adev->vram_scratch.robj);
271 
272 	return r;
273 }
274 
275 static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
276 {
277 	int r;
278 
279 	if (adev->vram_scratch.robj == NULL) {
280 		return;
281 	}
282 	r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
283 	if (likely(r == 0)) {
284 		amdgpu_bo_kunmap(adev->vram_scratch.robj);
285 		amdgpu_bo_unpin(adev->vram_scratch.robj);
286 		amdgpu_bo_unreserve(adev->vram_scratch.robj);
287 	}
288 	amdgpu_bo_unref(&adev->vram_scratch.robj);
289 }
290 
291 /**
292  * amdgpu_program_register_sequence - program an array of registers.
293  *
294  * @adev: amdgpu_device pointer
295  * @registers: pointer to the register array
296  * @array_size: size of the register array
297  *
298  * Programs an array or registers with and and or masks.
299  * This is a helper for setting golden registers.
300  */
301 void amdgpu_program_register_sequence(struct amdgpu_device *adev,
302 				      const u32 *registers,
303 				      const u32 array_size)
304 {
305 	u32 tmp, reg, and_mask, or_mask;
306 	int i;
307 
308 	if (array_size % 3)
309 		return;
310 
311 	for (i = 0; i < array_size; i +=3) {
312 		reg = registers[i + 0];
313 		and_mask = registers[i + 1];
314 		or_mask = registers[i + 2];
315 
316 		if (and_mask == 0xffffffff) {
317 			tmp = or_mask;
318 		} else {
319 			tmp = RREG32(reg);
320 			tmp &= ~and_mask;
321 			tmp |= or_mask;
322 		}
323 		WREG32(reg, tmp);
324 	}
325 }
326 
327 void amdgpu_pci_config_reset(struct amdgpu_device *adev)
328 {
329 	pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
330 }
331 
332 /*
333  * GPU doorbell aperture helpers function.
334  */
335 /**
336  * amdgpu_doorbell_init - Init doorbell driver information.
337  *
338  * @adev: amdgpu_device pointer
339  *
340  * Init doorbell driver information (CIK)
341  * Returns 0 on success, error on failure.
342  */
343 static int amdgpu_doorbell_init(struct amdgpu_device *adev)
344 {
345 	/* doorbell bar mapping */
346 	adev->doorbell.base = pci_resource_start(adev->pdev, 2);
347 	adev->doorbell.size = pci_resource_len(adev->pdev, 2);
348 
349 	adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
350 					     AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
351 	if (adev->doorbell.num_doorbells == 0)
352 		return -EINVAL;
353 
354 	adev->doorbell.ptr = ioremap(adev->doorbell.base, adev->doorbell.num_doorbells * sizeof(u32));
355 	if (adev->doorbell.ptr == NULL) {
356 		return -ENOMEM;
357 	}
358 	DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)adev->doorbell.base);
359 	DRM_INFO("doorbell mmio size: %u\n", (unsigned)adev->doorbell.size);
360 
361 	return 0;
362 }
363 
364 /**
365  * amdgpu_doorbell_fini - Tear down doorbell driver information.
366  *
367  * @adev: amdgpu_device pointer
368  *
369  * Tear down doorbell driver information (CIK)
370  */
371 static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
372 {
373 	iounmap(adev->doorbell.ptr);
374 	adev->doorbell.ptr = NULL;
375 }
376 
377 /**
378  * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
379  *                                setup amdkfd
380  *
381  * @adev: amdgpu_device pointer
382  * @aperture_base: output returning doorbell aperture base physical address
383  * @aperture_size: output returning doorbell aperture size in bytes
384  * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
385  *
386  * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
387  * takes doorbells required for its own rings and reports the setup to amdkfd.
388  * amdgpu reserved doorbells are at the start of the doorbell aperture.
389  */
390 void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
391 				phys_addr_t *aperture_base,
392 				size_t *aperture_size,
393 				size_t *start_offset)
394 {
395 	/*
396 	 * The first num_doorbells are used by amdgpu.
397 	 * amdkfd takes whatever's left in the aperture.
398 	 */
399 	if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
400 		*aperture_base = adev->doorbell.base;
401 		*aperture_size = adev->doorbell.size;
402 		*start_offset = adev->doorbell.num_doorbells * sizeof(u32);
403 	} else {
404 		*aperture_base = 0;
405 		*aperture_size = 0;
406 		*start_offset = 0;
407 	}
408 }
409 
410 /*
411  * amdgpu_wb_*()
412  * Writeback is the the method by which the the GPU updates special pages
413  * in memory with the status of certain GPU events (fences, ring pointers,
414  * etc.).
415  */
416 
417 /**
418  * amdgpu_wb_fini - Disable Writeback and free memory
419  *
420  * @adev: amdgpu_device pointer
421  *
422  * Disables Writeback and frees the Writeback memory (all asics).
423  * Used at driver shutdown.
424  */
425 static void amdgpu_wb_fini(struct amdgpu_device *adev)
426 {
427 	if (adev->wb.wb_obj) {
428 		if (!amdgpu_bo_reserve(adev->wb.wb_obj, false)) {
429 			amdgpu_bo_kunmap(adev->wb.wb_obj);
430 			amdgpu_bo_unpin(adev->wb.wb_obj);
431 			amdgpu_bo_unreserve(adev->wb.wb_obj);
432 		}
433 		amdgpu_bo_unref(&adev->wb.wb_obj);
434 		adev->wb.wb = NULL;
435 		adev->wb.wb_obj = NULL;
436 	}
437 }
438 
439 /**
440  * amdgpu_wb_init- Init Writeback driver info and allocate memory
441  *
442  * @adev: amdgpu_device pointer
443  *
444  * Disables Writeback and frees the Writeback memory (all asics).
445  * Used at driver startup.
446  * Returns 0 on success or an -error on failure.
447  */
448 static int amdgpu_wb_init(struct amdgpu_device *adev)
449 {
450 	int r;
451 
452 	if (adev->wb.wb_obj == NULL) {
453 		r = amdgpu_bo_create(adev, AMDGPU_MAX_WB * 4, PAGE_SIZE, true,
454 				     AMDGPU_GEM_DOMAIN_GTT, 0,  NULL, NULL,
455 				     &adev->wb.wb_obj);
456 		if (r) {
457 			dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
458 			return r;
459 		}
460 		r = amdgpu_bo_reserve(adev->wb.wb_obj, false);
461 		if (unlikely(r != 0)) {
462 			amdgpu_wb_fini(adev);
463 			return r;
464 		}
465 		r = amdgpu_bo_pin(adev->wb.wb_obj, AMDGPU_GEM_DOMAIN_GTT,
466 				&adev->wb.gpu_addr);
467 		if (r) {
468 			amdgpu_bo_unreserve(adev->wb.wb_obj);
469 			dev_warn(adev->dev, "(%d) pin WB bo failed\n", r);
470 			amdgpu_wb_fini(adev);
471 			return r;
472 		}
473 		r = amdgpu_bo_kmap(adev->wb.wb_obj, (void **)&adev->wb.wb);
474 		amdgpu_bo_unreserve(adev->wb.wb_obj);
475 		if (r) {
476 			dev_warn(adev->dev, "(%d) map WB bo failed\n", r);
477 			amdgpu_wb_fini(adev);
478 			return r;
479 		}
480 
481 		adev->wb.num_wb = AMDGPU_MAX_WB;
482 		memset(&adev->wb.used, 0, sizeof(adev->wb.used));
483 
484 		/* clear wb memory */
485 		memset((char *)adev->wb.wb, 0, AMDGPU_GPU_PAGE_SIZE);
486 	}
487 
488 	return 0;
489 }
490 
491 /**
492  * amdgpu_wb_get - Allocate a wb entry
493  *
494  * @adev: amdgpu_device pointer
495  * @wb: wb index
496  *
497  * Allocate a wb slot for use by the driver (all asics).
498  * Returns 0 on success or -EINVAL on failure.
499  */
500 int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
501 {
502 	unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
503 	if (offset < adev->wb.num_wb) {
504 		__set_bit(offset, adev->wb.used);
505 		*wb = offset;
506 		return 0;
507 	} else {
508 		return -EINVAL;
509 	}
510 }
511 
512 /**
513  * amdgpu_wb_free - Free a wb entry
514  *
515  * @adev: amdgpu_device pointer
516  * @wb: wb index
517  *
518  * Free a wb slot allocated for use by the driver (all asics)
519  */
520 void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
521 {
522 	if (wb < adev->wb.num_wb)
523 		__clear_bit(wb, adev->wb.used);
524 }
525 
526 /**
527  * amdgpu_vram_location - try to find VRAM location
528  * @adev: amdgpu device structure holding all necessary informations
529  * @mc: memory controller structure holding memory informations
530  * @base: base address at which to put VRAM
531  *
532  * Function will place try to place VRAM at base address provided
533  * as parameter (which is so far either PCI aperture address or
534  * for IGP TOM base address).
535  *
536  * If there is not enough space to fit the unvisible VRAM in the 32bits
537  * address space then we limit the VRAM size to the aperture.
538  *
539  * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
540  * this shouldn't be a problem as we are using the PCI aperture as a reference.
541  * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
542  * not IGP.
543  *
544  * Note: we use mc_vram_size as on some board we need to program the mc to
545  * cover the whole aperture even if VRAM size is inferior to aperture size
546  * Novell bug 204882 + along with lots of ubuntu ones
547  *
548  * Note: when limiting vram it's safe to overwritte real_vram_size because
549  * we are not in case where real_vram_size is inferior to mc_vram_size (ie
550  * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
551  * ones)
552  *
553  * Note: IGP TOM addr should be the same as the aperture addr, we don't
554  * explicitly check for that thought.
555  *
556  * FIXME: when reducing VRAM size align new size on power of 2.
557  */
558 void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
559 {
560 	uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
561 
562 	mc->vram_start = base;
563 	if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
564 		dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
565 		mc->real_vram_size = mc->aper_size;
566 		mc->mc_vram_size = mc->aper_size;
567 	}
568 	mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
569 	if (limit && limit < mc->real_vram_size)
570 		mc->real_vram_size = limit;
571 	dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
572 			mc->mc_vram_size >> 20, mc->vram_start,
573 			mc->vram_end, mc->real_vram_size >> 20);
574 }
575 
576 /**
577  * amdgpu_gtt_location - try to find GTT location
578  * @adev: amdgpu device structure holding all necessary informations
579  * @mc: memory controller structure holding memory informations
580  *
581  * Function will place try to place GTT before or after VRAM.
582  *
583  * If GTT size is bigger than space left then we ajust GTT size.
584  * Thus function will never fails.
585  *
586  * FIXME: when reducing GTT size align new size on power of 2.
587  */
588 void amdgpu_gtt_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
589 {
590 	u64 size_af, size_bf;
591 
592 	size_af = ((adev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
593 	size_bf = mc->vram_start & ~mc->gtt_base_align;
594 	if (size_bf > size_af) {
595 		if (mc->gtt_size > size_bf) {
596 			dev_warn(adev->dev, "limiting GTT\n");
597 			mc->gtt_size = size_bf;
598 		}
599 		mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
600 	} else {
601 		if (mc->gtt_size > size_af) {
602 			dev_warn(adev->dev, "limiting GTT\n");
603 			mc->gtt_size = size_af;
604 		}
605 		mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
606 	}
607 	mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
608 	dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
609 			mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
610 }
611 
612 /*
613  * GPU helpers function.
614  */
615 /**
616  * amdgpu_card_posted - check if the hw has already been initialized
617  *
618  * @adev: amdgpu_device pointer
619  *
620  * Check if the asic has been initialized (all asics).
621  * Used at driver startup.
622  * Returns true if initialized or false if not.
623  */
624 bool amdgpu_card_posted(struct amdgpu_device *adev)
625 {
626 	uint32_t reg;
627 
628 	/* then check MEM_SIZE, in case the crtcs are off */
629 	reg = RREG32(mmCONFIG_MEMSIZE);
630 
631 	if (reg)
632 		return true;
633 
634 	return false;
635 
636 }
637 
638 /**
639  * amdgpu_boot_test_post_card - check and possibly initialize the hw
640  *
641  * @adev: amdgpu_device pointer
642  *
643  * Check if the asic is initialized and if not, attempt to initialize
644  * it (all asics).
645  * Returns true if initialized or false if not.
646  */
647 bool amdgpu_boot_test_post_card(struct amdgpu_device *adev)
648 {
649 	if (amdgpu_card_posted(adev))
650 		return true;
651 
652 	if (adev->bios) {
653 		DRM_INFO("GPU not posted. posting now...\n");
654 		if (adev->is_atom_bios)
655 			amdgpu_atom_asic_init(adev->mode_info.atom_context);
656 		return true;
657 	} else {
658 		dev_err(adev->dev, "Card not posted and no BIOS - ignoring\n");
659 		return false;
660 	}
661 }
662 
663 /**
664  * amdgpu_dummy_page_init - init dummy page used by the driver
665  *
666  * @adev: amdgpu_device pointer
667  *
668  * Allocate the dummy page used by the driver (all asics).
669  * This dummy page is used by the driver as a filler for gart entries
670  * when pages are taken out of the GART
671  * Returns 0 on sucess, -ENOMEM on failure.
672  */
673 int amdgpu_dummy_page_init(struct amdgpu_device *adev)
674 {
675 	if (adev->dummy_page.page)
676 		return 0;
677 	adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
678 	if (adev->dummy_page.page == NULL)
679 		return -ENOMEM;
680 	adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
681 					0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
682 	if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
683 		dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
684 		__free_page(adev->dummy_page.page);
685 		adev->dummy_page.page = NULL;
686 		return -ENOMEM;
687 	}
688 	return 0;
689 }
690 
691 /**
692  * amdgpu_dummy_page_fini - free dummy page used by the driver
693  *
694  * @adev: amdgpu_device pointer
695  *
696  * Frees the dummy page used by the driver (all asics).
697  */
698 void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
699 {
700 	if (adev->dummy_page.page == NULL)
701 		return;
702 	pci_unmap_page(adev->pdev, adev->dummy_page.addr,
703 			PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
704 	__free_page(adev->dummy_page.page);
705 	adev->dummy_page.page = NULL;
706 }
707 
708 
709 /* ATOM accessor methods */
710 /*
711  * ATOM is an interpreted byte code stored in tables in the vbios.  The
712  * driver registers callbacks to access registers and the interpreter
713  * in the driver parses the tables and executes then to program specific
714  * actions (set display modes, asic init, etc.).  See amdgpu_atombios.c,
715  * atombios.h, and atom.c
716  */
717 
718 /**
719  * cail_pll_read - read PLL register
720  *
721  * @info: atom card_info pointer
722  * @reg: PLL register offset
723  *
724  * Provides a PLL register accessor for the atom interpreter (r4xx+).
725  * Returns the value of the PLL register.
726  */
727 static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
728 {
729 	return 0;
730 }
731 
732 /**
733  * cail_pll_write - write PLL register
734  *
735  * @info: atom card_info pointer
736  * @reg: PLL register offset
737  * @val: value to write to the pll register
738  *
739  * Provides a PLL register accessor for the atom interpreter (r4xx+).
740  */
741 static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
742 {
743 
744 }
745 
746 /**
747  * cail_mc_read - read MC (Memory Controller) register
748  *
749  * @info: atom card_info pointer
750  * @reg: MC register offset
751  *
752  * Provides an MC register accessor for the atom interpreter (r4xx+).
753  * Returns the value of the MC register.
754  */
755 static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
756 {
757 	return 0;
758 }
759 
760 /**
761  * cail_mc_write - write MC (Memory Controller) register
762  *
763  * @info: atom card_info pointer
764  * @reg: MC register offset
765  * @val: value to write to the pll register
766  *
767  * Provides a MC register accessor for the atom interpreter (r4xx+).
768  */
769 static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
770 {
771 
772 }
773 
774 /**
775  * cail_reg_write - write MMIO register
776  *
777  * @info: atom card_info pointer
778  * @reg: MMIO register offset
779  * @val: value to write to the pll register
780  *
781  * Provides a MMIO register accessor for the atom interpreter (r4xx+).
782  */
783 static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
784 {
785 	struct amdgpu_device *adev = info->dev->dev_private;
786 
787 	WREG32(reg, val);
788 }
789 
790 /**
791  * cail_reg_read - read MMIO register
792  *
793  * @info: atom card_info pointer
794  * @reg: MMIO register offset
795  *
796  * Provides an MMIO register accessor for the atom interpreter (r4xx+).
797  * Returns the value of the MMIO register.
798  */
799 static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
800 {
801 	struct amdgpu_device *adev = info->dev->dev_private;
802 	uint32_t r;
803 
804 	r = RREG32(reg);
805 	return r;
806 }
807 
808 /**
809  * cail_ioreg_write - write IO register
810  *
811  * @info: atom card_info pointer
812  * @reg: IO register offset
813  * @val: value to write to the pll register
814  *
815  * Provides a IO register accessor for the atom interpreter (r4xx+).
816  */
817 static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
818 {
819 	struct amdgpu_device *adev = info->dev->dev_private;
820 
821 	WREG32_IO(reg, val);
822 }
823 
824 /**
825  * cail_ioreg_read - read IO register
826  *
827  * @info: atom card_info pointer
828  * @reg: IO register offset
829  *
830  * Provides an IO register accessor for the atom interpreter (r4xx+).
831  * Returns the value of the IO register.
832  */
833 static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
834 {
835 	struct amdgpu_device *adev = info->dev->dev_private;
836 	uint32_t r;
837 
838 	r = RREG32_IO(reg);
839 	return r;
840 }
841 
842 /**
843  * amdgpu_atombios_fini - free the driver info and callbacks for atombios
844  *
845  * @adev: amdgpu_device pointer
846  *
847  * Frees the driver info and register access callbacks for the ATOM
848  * interpreter (r4xx+).
849  * Called at driver shutdown.
850  */
851 static void amdgpu_atombios_fini(struct amdgpu_device *adev)
852 {
853 	if (adev->mode_info.atom_context)
854 		kfree(adev->mode_info.atom_context->scratch);
855 	kfree(adev->mode_info.atom_context);
856 	adev->mode_info.atom_context = NULL;
857 	kfree(adev->mode_info.atom_card_info);
858 	adev->mode_info.atom_card_info = NULL;
859 }
860 
861 /**
862  * amdgpu_atombios_init - init the driver info and callbacks for atombios
863  *
864  * @adev: amdgpu_device pointer
865  *
866  * Initializes the driver info and register access callbacks for the
867  * ATOM interpreter (r4xx+).
868  * Returns 0 on sucess, -ENOMEM on failure.
869  * Called at driver startup.
870  */
871 static int amdgpu_atombios_init(struct amdgpu_device *adev)
872 {
873 	struct card_info *atom_card_info =
874 	    kzalloc(sizeof(struct card_info), GFP_KERNEL);
875 
876 	if (!atom_card_info)
877 		return -ENOMEM;
878 
879 	adev->mode_info.atom_card_info = atom_card_info;
880 	atom_card_info->dev = adev->ddev;
881 	atom_card_info->reg_read = cail_reg_read;
882 	atom_card_info->reg_write = cail_reg_write;
883 	/* needed for iio ops */
884 	if (adev->rio_mem) {
885 		atom_card_info->ioreg_read = cail_ioreg_read;
886 		atom_card_info->ioreg_write = cail_ioreg_write;
887 	} else {
888 		DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
889 		atom_card_info->ioreg_read = cail_reg_read;
890 		atom_card_info->ioreg_write = cail_reg_write;
891 	}
892 	atom_card_info->mc_read = cail_mc_read;
893 	atom_card_info->mc_write = cail_mc_write;
894 	atom_card_info->pll_read = cail_pll_read;
895 	atom_card_info->pll_write = cail_pll_write;
896 
897 	adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
898 	if (!adev->mode_info.atom_context) {
899 		amdgpu_atombios_fini(adev);
900 		return -ENOMEM;
901 	}
902 
903 	mutex_init(&adev->mode_info.atom_context->mutex);
904 	amdgpu_atombios_scratch_regs_init(adev);
905 	amdgpu_atom_allocate_fb_scratch(adev->mode_info.atom_context);
906 	return 0;
907 }
908 
909 /* if we get transitioned to only one device, take VGA back */
910 /**
911  * amdgpu_vga_set_decode - enable/disable vga decode
912  *
913  * @cookie: amdgpu_device pointer
914  * @state: enable/disable vga decode
915  *
916  * Enable/disable vga decode (all asics).
917  * Returns VGA resource flags.
918  */
919 static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
920 {
921 	struct amdgpu_device *adev = cookie;
922 	amdgpu_asic_set_vga_state(adev, state);
923 	if (state)
924 		return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
925 		       VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
926 	else
927 		return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
928 }
929 
930 /**
931  * amdgpu_check_pot_argument - check that argument is a power of two
932  *
933  * @arg: value to check
934  *
935  * Validates that a certain argument is a power of two (all asics).
936  * Returns true if argument is valid.
937  */
938 static bool amdgpu_check_pot_argument(int arg)
939 {
940 	return (arg & (arg - 1)) == 0;
941 }
942 
943 /**
944  * amdgpu_check_arguments - validate module params
945  *
946  * @adev: amdgpu_device pointer
947  *
948  * Validates certain module parameters and updates
949  * the associated values used by the driver (all asics).
950  */
951 static void amdgpu_check_arguments(struct amdgpu_device *adev)
952 {
953 	if (amdgpu_sched_jobs < 4) {
954 		dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
955 			 amdgpu_sched_jobs);
956 		amdgpu_sched_jobs = 4;
957 	} else if (!amdgpu_check_pot_argument(amdgpu_sched_jobs)){
958 		dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
959 			 amdgpu_sched_jobs);
960 		amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
961 	}
962 
963 	if (amdgpu_gart_size != -1) {
964 		/* gtt size must be power of two and greater or equal to 32M */
965 		if (amdgpu_gart_size < 32) {
966 			dev_warn(adev->dev, "gart size (%d) too small\n",
967 				 amdgpu_gart_size);
968 			amdgpu_gart_size = -1;
969 		} else if (!amdgpu_check_pot_argument(amdgpu_gart_size)) {
970 			dev_warn(adev->dev, "gart size (%d) must be a power of 2\n",
971 				 amdgpu_gart_size);
972 			amdgpu_gart_size = -1;
973 		}
974 	}
975 
976 	if (!amdgpu_check_pot_argument(amdgpu_vm_size)) {
977 		dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
978 			 amdgpu_vm_size);
979 		amdgpu_vm_size = 8;
980 	}
981 
982 	if (amdgpu_vm_size < 1) {
983 		dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
984 			 amdgpu_vm_size);
985 		amdgpu_vm_size = 8;
986 	}
987 
988 	/*
989 	 * Max GPUVM size for Cayman, SI and CI are 40 bits.
990 	 */
991 	if (amdgpu_vm_size > 1024) {
992 		dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
993 			 amdgpu_vm_size);
994 		amdgpu_vm_size = 8;
995 	}
996 
997 	/* defines number of bits in page table versus page directory,
998 	 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
999 	 * page table and the remaining bits are in the page directory */
1000 	if (amdgpu_vm_block_size == -1) {
1001 
1002 		/* Total bits covered by PD + PTs */
1003 		unsigned bits = ilog2(amdgpu_vm_size) + 18;
1004 
1005 		/* Make sure the PD is 4K in size up to 8GB address space.
1006 		   Above that split equal between PD and PTs */
1007 		if (amdgpu_vm_size <= 8)
1008 			amdgpu_vm_block_size = bits - 9;
1009 		else
1010 			amdgpu_vm_block_size = (bits + 3) / 2;
1011 
1012 	} else if (amdgpu_vm_block_size < 9) {
1013 		dev_warn(adev->dev, "VM page table size (%d) too small\n",
1014 			 amdgpu_vm_block_size);
1015 		amdgpu_vm_block_size = 9;
1016 	}
1017 
1018 	if (amdgpu_vm_block_size > 24 ||
1019 	    (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1020 		dev_warn(adev->dev, "VM page table size (%d) too large\n",
1021 			 amdgpu_vm_block_size);
1022 		amdgpu_vm_block_size = 9;
1023 	}
1024 }
1025 
1026 /**
1027  * amdgpu_switcheroo_set_state - set switcheroo state
1028  *
1029  * @pdev: pci dev pointer
1030  * @state: vga_switcheroo state
1031  *
1032  * Callback for the switcheroo driver.  Suspends or resumes the
1033  * the asics before or after it is powered up using ACPI methods.
1034  */
1035 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1036 {
1037 	struct drm_device *dev = pci_get_drvdata(pdev);
1038 
1039 	if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1040 		return;
1041 
1042 	if (state == VGA_SWITCHEROO_ON) {
1043 		unsigned d3_delay = dev->pdev->d3_delay;
1044 
1045 		printk(KERN_INFO "amdgpu: switched on\n");
1046 		/* don't suspend or resume card normally */
1047 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1048 
1049 		amdgpu_resume_kms(dev, true, true);
1050 
1051 		dev->pdev->d3_delay = d3_delay;
1052 
1053 		dev->switch_power_state = DRM_SWITCH_POWER_ON;
1054 		drm_kms_helper_poll_enable(dev);
1055 	} else {
1056 		printk(KERN_INFO "amdgpu: switched off\n");
1057 		drm_kms_helper_poll_disable(dev);
1058 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1059 		amdgpu_suspend_kms(dev, true, true);
1060 		dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1061 	}
1062 }
1063 
1064 /**
1065  * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1066  *
1067  * @pdev: pci dev pointer
1068  *
1069  * Callback for the switcheroo driver.  Check of the switcheroo
1070  * state can be changed.
1071  * Returns true if the state can be changed, false if not.
1072  */
1073 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1074 {
1075 	struct drm_device *dev = pci_get_drvdata(pdev);
1076 
1077 	/*
1078 	* FIXME: open_count is protected by drm_global_mutex but that would lead to
1079 	* locking inversion with the driver load path. And the access here is
1080 	* completely racy anyway. So don't bother with locking for now.
1081 	*/
1082 	return dev->open_count == 0;
1083 }
1084 
1085 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1086 	.set_gpu_state = amdgpu_switcheroo_set_state,
1087 	.reprobe = NULL,
1088 	.can_switch = amdgpu_switcheroo_can_switch,
1089 };
1090 
1091 int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
1092 				  enum amd_ip_block_type block_type,
1093 				  enum amd_clockgating_state state)
1094 {
1095 	int i, r = 0;
1096 
1097 	for (i = 0; i < adev->num_ip_blocks; i++) {
1098 		if (adev->ip_blocks[i].type == block_type) {
1099 			r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1100 									    state);
1101 			if (r)
1102 				return r;
1103 		}
1104 	}
1105 	return r;
1106 }
1107 
1108 int amdgpu_set_powergating_state(struct amdgpu_device *adev,
1109 				  enum amd_ip_block_type block_type,
1110 				  enum amd_powergating_state state)
1111 {
1112 	int i, r = 0;
1113 
1114 	for (i = 0; i < adev->num_ip_blocks; i++) {
1115 		if (adev->ip_blocks[i].type == block_type) {
1116 			r = adev->ip_blocks[i].funcs->set_powergating_state((void *)adev,
1117 									    state);
1118 			if (r)
1119 				return r;
1120 		}
1121 	}
1122 	return r;
1123 }
1124 
1125 const struct amdgpu_ip_block_version * amdgpu_get_ip_block(
1126 					struct amdgpu_device *adev,
1127 					enum amd_ip_block_type type)
1128 {
1129 	int i;
1130 
1131 	for (i = 0; i < adev->num_ip_blocks; i++)
1132 		if (adev->ip_blocks[i].type == type)
1133 			return &adev->ip_blocks[i];
1134 
1135 	return NULL;
1136 }
1137 
1138 /**
1139  * amdgpu_ip_block_version_cmp
1140  *
1141  * @adev: amdgpu_device pointer
1142  * @type: enum amd_ip_block_type
1143  * @major: major version
1144  * @minor: minor version
1145  *
1146  * return 0 if equal or greater
1147  * return 1 if smaller or the ip_block doesn't exist
1148  */
1149 int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
1150 				enum amd_ip_block_type type,
1151 				u32 major, u32 minor)
1152 {
1153 	const struct amdgpu_ip_block_version *ip_block;
1154 	ip_block = amdgpu_get_ip_block(adev, type);
1155 
1156 	if (ip_block && ((ip_block->major > major) ||
1157 			((ip_block->major == major) &&
1158 			(ip_block->minor >= minor))))
1159 		return 0;
1160 
1161 	return 1;
1162 }
1163 
1164 static int amdgpu_early_init(struct amdgpu_device *adev)
1165 {
1166 	int i, r;
1167 
1168 	switch (adev->asic_type) {
1169 	case CHIP_TOPAZ:
1170 	case CHIP_TONGA:
1171 	case CHIP_FIJI:
1172 	case CHIP_CARRIZO:
1173 	case CHIP_STONEY:
1174 		if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
1175 			adev->family = AMDGPU_FAMILY_CZ;
1176 		else
1177 			adev->family = AMDGPU_FAMILY_VI;
1178 
1179 		r = vi_set_ip_blocks(adev);
1180 		if (r)
1181 			return r;
1182 		break;
1183 #ifdef CONFIG_DRM_AMDGPU_CIK
1184 	case CHIP_BONAIRE:
1185 	case CHIP_HAWAII:
1186 	case CHIP_KAVERI:
1187 	case CHIP_KABINI:
1188 	case CHIP_MULLINS:
1189 		if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1190 			adev->family = AMDGPU_FAMILY_CI;
1191 		else
1192 			adev->family = AMDGPU_FAMILY_KV;
1193 
1194 		r = cik_set_ip_blocks(adev);
1195 		if (r)
1196 			return r;
1197 		break;
1198 #endif
1199 	default:
1200 		/* FIXME: not supported yet */
1201 		return -EINVAL;
1202 	}
1203 
1204 	adev->ip_block_status = kcalloc(adev->num_ip_blocks,
1205 					sizeof(struct amdgpu_ip_block_status), GFP_KERNEL);
1206 	if (adev->ip_block_status == NULL)
1207 		return -ENOMEM;
1208 
1209 	if (adev->ip_blocks == NULL) {
1210 		DRM_ERROR("No IP blocks found!\n");
1211 		return r;
1212 	}
1213 
1214 	for (i = 0; i < adev->num_ip_blocks; i++) {
1215 		if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1216 			DRM_ERROR("disabled ip block: %d\n", i);
1217 			adev->ip_block_status[i].valid = false;
1218 		} else {
1219 			if (adev->ip_blocks[i].funcs->early_init) {
1220 				r = adev->ip_blocks[i].funcs->early_init((void *)adev);
1221 				if (r == -ENOENT) {
1222 					adev->ip_block_status[i].valid = false;
1223 				} else if (r) {
1224 					DRM_ERROR("early_init %d failed %d\n", i, r);
1225 					return r;
1226 				} else {
1227 					adev->ip_block_status[i].valid = true;
1228 				}
1229 			} else {
1230 				adev->ip_block_status[i].valid = true;
1231 			}
1232 		}
1233 	}
1234 
1235 	return 0;
1236 }
1237 
1238 static int amdgpu_init(struct amdgpu_device *adev)
1239 {
1240 	int i, r;
1241 
1242 	for (i = 0; i < adev->num_ip_blocks; i++) {
1243 		if (!adev->ip_block_status[i].valid)
1244 			continue;
1245 		r = adev->ip_blocks[i].funcs->sw_init((void *)adev);
1246 		if (r) {
1247 			DRM_ERROR("sw_init %d failed %d\n", i, r);
1248 			return r;
1249 		}
1250 		adev->ip_block_status[i].sw = true;
1251 		/* need to do gmc hw init early so we can allocate gpu mem */
1252 		if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) {
1253 			r = amdgpu_vram_scratch_init(adev);
1254 			if (r) {
1255 				DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
1256 				return r;
1257 			}
1258 			r = adev->ip_blocks[i].funcs->hw_init((void *)adev);
1259 			if (r) {
1260 				DRM_ERROR("hw_init %d failed %d\n", i, r);
1261 				return r;
1262 			}
1263 			r = amdgpu_wb_init(adev);
1264 			if (r) {
1265 				DRM_ERROR("amdgpu_wb_init failed %d\n", r);
1266 				return r;
1267 			}
1268 			adev->ip_block_status[i].hw = true;
1269 		}
1270 	}
1271 
1272 	for (i = 0; i < adev->num_ip_blocks; i++) {
1273 		if (!adev->ip_block_status[i].sw)
1274 			continue;
1275 		/* gmc hw init is done early */
1276 		if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC)
1277 			continue;
1278 		r = adev->ip_blocks[i].funcs->hw_init((void *)adev);
1279 		if (r) {
1280 			DRM_ERROR("hw_init %d failed %d\n", i, r);
1281 			return r;
1282 		}
1283 		adev->ip_block_status[i].hw = true;
1284 	}
1285 
1286 	return 0;
1287 }
1288 
1289 static int amdgpu_late_init(struct amdgpu_device *adev)
1290 {
1291 	int i = 0, r;
1292 
1293 	for (i = 0; i < adev->num_ip_blocks; i++) {
1294 		if (!adev->ip_block_status[i].valid)
1295 			continue;
1296 		/* enable clockgating to save power */
1297 		r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1298 								    AMD_CG_STATE_GATE);
1299 		if (r) {
1300 			DRM_ERROR("set_clockgating_state(gate) %d failed %d\n", i, r);
1301 			return r;
1302 		}
1303 		if (adev->ip_blocks[i].funcs->late_init) {
1304 			r = adev->ip_blocks[i].funcs->late_init((void *)adev);
1305 			if (r) {
1306 				DRM_ERROR("late_init %d failed %d\n", i, r);
1307 				return r;
1308 			}
1309 		}
1310 	}
1311 
1312 	return 0;
1313 }
1314 
1315 static int amdgpu_fini(struct amdgpu_device *adev)
1316 {
1317 	int i, r;
1318 
1319 	for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1320 		if (!adev->ip_block_status[i].hw)
1321 			continue;
1322 		if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) {
1323 			amdgpu_wb_fini(adev);
1324 			amdgpu_vram_scratch_fini(adev);
1325 		}
1326 		/* ungate blocks before hw fini so that we can shutdown the blocks safely */
1327 		r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1328 								    AMD_CG_STATE_UNGATE);
1329 		if (r) {
1330 			DRM_ERROR("set_clockgating_state(ungate) %d failed %d\n", i, r);
1331 			return r;
1332 		}
1333 		r = adev->ip_blocks[i].funcs->hw_fini((void *)adev);
1334 		/* XXX handle errors */
1335 		if (r) {
1336 			DRM_DEBUG("hw_fini %d failed %d\n", i, r);
1337 		}
1338 		adev->ip_block_status[i].hw = false;
1339 	}
1340 
1341 	for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1342 		if (!adev->ip_block_status[i].sw)
1343 			continue;
1344 		r = adev->ip_blocks[i].funcs->sw_fini((void *)adev);
1345 		/* XXX handle errors */
1346 		if (r) {
1347 			DRM_DEBUG("sw_fini %d failed %d\n", i, r);
1348 		}
1349 		adev->ip_block_status[i].sw = false;
1350 		adev->ip_block_status[i].valid = false;
1351 	}
1352 
1353 	return 0;
1354 }
1355 
1356 static int amdgpu_suspend(struct amdgpu_device *adev)
1357 {
1358 	int i, r;
1359 
1360 	for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1361 		if (!adev->ip_block_status[i].valid)
1362 			continue;
1363 		/* ungate blocks so that suspend can properly shut them down */
1364 		r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1365 								    AMD_CG_STATE_UNGATE);
1366 		if (r) {
1367 			DRM_ERROR("set_clockgating_state(ungate) %d failed %d\n", i, r);
1368 		}
1369 		/* XXX handle errors */
1370 		r = adev->ip_blocks[i].funcs->suspend(adev);
1371 		/* XXX handle errors */
1372 		if (r) {
1373 			DRM_ERROR("suspend %d failed %d\n", i, r);
1374 		}
1375 	}
1376 
1377 	return 0;
1378 }
1379 
1380 static int amdgpu_resume(struct amdgpu_device *adev)
1381 {
1382 	int i, r;
1383 
1384 	for (i = 0; i < adev->num_ip_blocks; i++) {
1385 		if (!adev->ip_block_status[i].valid)
1386 			continue;
1387 		r = adev->ip_blocks[i].funcs->resume(adev);
1388 		if (r) {
1389 			DRM_ERROR("resume %d failed %d\n", i, r);
1390 			return r;
1391 		}
1392 	}
1393 
1394 	return 0;
1395 }
1396 
1397 /**
1398  * amdgpu_device_init - initialize the driver
1399  *
1400  * @adev: amdgpu_device pointer
1401  * @pdev: drm dev pointer
1402  * @pdev: pci dev pointer
1403  * @flags: driver flags
1404  *
1405  * Initializes the driver info and hw (all asics).
1406  * Returns 0 for success or an error on failure.
1407  * Called at driver startup.
1408  */
1409 int amdgpu_device_init(struct amdgpu_device *adev,
1410 		       struct drm_device *ddev,
1411 		       struct pci_dev *pdev,
1412 		       uint32_t flags)
1413 {
1414 	int r, i;
1415 	bool runtime = false;
1416 
1417 	adev->shutdown = false;
1418 	adev->dev = &pdev->dev;
1419 	adev->ddev = ddev;
1420 	adev->pdev = pdev;
1421 	adev->flags = flags;
1422 	adev->asic_type = flags & AMD_ASIC_MASK;
1423 	adev->is_atom_bios = false;
1424 	adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
1425 	adev->mc.gtt_size = 512 * 1024 * 1024;
1426 	adev->accel_working = false;
1427 	adev->num_rings = 0;
1428 	adev->mman.buffer_funcs = NULL;
1429 	adev->mman.buffer_funcs_ring = NULL;
1430 	adev->vm_manager.vm_pte_funcs = NULL;
1431 	adev->vm_manager.vm_pte_funcs_ring = NULL;
1432 	adev->gart.gart_funcs = NULL;
1433 	adev->fence_context = fence_context_alloc(AMDGPU_MAX_RINGS);
1434 
1435 	adev->smc_rreg = &amdgpu_invalid_rreg;
1436 	adev->smc_wreg = &amdgpu_invalid_wreg;
1437 	adev->pcie_rreg = &amdgpu_invalid_rreg;
1438 	adev->pcie_wreg = &amdgpu_invalid_wreg;
1439 	adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1440 	adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1441 	adev->didt_rreg = &amdgpu_invalid_rreg;
1442 	adev->didt_wreg = &amdgpu_invalid_wreg;
1443 	adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1444 	adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1445 
1446 	DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1447 		 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1448 		 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
1449 
1450 	/* mutex initialization are all done here so we
1451 	 * can recall function without having locking issues */
1452 	mutex_init(&adev->vm_manager.lock);
1453 	atomic_set(&adev->irq.ih.lock, 0);
1454 	mutex_init(&adev->gem.mutex);
1455 	mutex_init(&adev->pm.mutex);
1456 	mutex_init(&adev->gfx.gpu_clock_mutex);
1457 	mutex_init(&adev->srbm_mutex);
1458 	mutex_init(&adev->grbm_idx_mutex);
1459 	mutex_init(&adev->mn_lock);
1460 	hash_init(adev->mn_hash);
1461 
1462 	amdgpu_check_arguments(adev);
1463 
1464 	/* Registers mapping */
1465 	/* TODO: block userspace mapping of io register */
1466 	spin_lock_init(&adev->mmio_idx_lock);
1467 	spin_lock_init(&adev->smc_idx_lock);
1468 	spin_lock_init(&adev->pcie_idx_lock);
1469 	spin_lock_init(&adev->uvd_ctx_idx_lock);
1470 	spin_lock_init(&adev->didt_idx_lock);
1471 	spin_lock_init(&adev->audio_endpt_idx_lock);
1472 
1473 	adev->rmmio_base = pci_resource_start(adev->pdev, 5);
1474 	adev->rmmio_size = pci_resource_len(adev->pdev, 5);
1475 	adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
1476 	if (adev->rmmio == NULL) {
1477 		return -ENOMEM;
1478 	}
1479 	DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
1480 	DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
1481 
1482 	/* doorbell bar mapping */
1483 	amdgpu_doorbell_init(adev);
1484 
1485 	/* io port mapping */
1486 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1487 		if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
1488 			adev->rio_mem_size = pci_resource_len(adev->pdev, i);
1489 			adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
1490 			break;
1491 		}
1492 	}
1493 	if (adev->rio_mem == NULL)
1494 		DRM_ERROR("Unable to find PCI I/O BAR\n");
1495 
1496 	/* early init functions */
1497 	r = amdgpu_early_init(adev);
1498 	if (r)
1499 		return r;
1500 
1501 	/* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
1502 	/* this will fail for cards that aren't VGA class devices, just
1503 	 * ignore it */
1504 	vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
1505 
1506 	if (amdgpu_runtime_pm == 1)
1507 		runtime = true;
1508 	if (amdgpu_device_is_px(ddev))
1509 		runtime = true;
1510 	vga_switcheroo_register_client(adev->pdev, &amdgpu_switcheroo_ops, runtime);
1511 	if (runtime)
1512 		vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
1513 
1514 	/* Read BIOS */
1515 	if (!amdgpu_get_bios(adev))
1516 		return -EINVAL;
1517 	/* Must be an ATOMBIOS */
1518 	if (!adev->is_atom_bios) {
1519 		dev_err(adev->dev, "Expecting atombios for GPU\n");
1520 		return -EINVAL;
1521 	}
1522 	r = amdgpu_atombios_init(adev);
1523 	if (r) {
1524 		dev_err(adev->dev, "amdgpu_atombios_init failed\n");
1525 		return r;
1526 	}
1527 
1528 	/* Post card if necessary */
1529 	if (!amdgpu_card_posted(adev)) {
1530 		if (!adev->bios) {
1531 			dev_err(adev->dev, "Card not posted and no BIOS - ignoring\n");
1532 			return -EINVAL;
1533 		}
1534 		DRM_INFO("GPU not posted. posting now...\n");
1535 		amdgpu_atom_asic_init(adev->mode_info.atom_context);
1536 	}
1537 
1538 	/* Initialize clocks */
1539 	r = amdgpu_atombios_get_clock_info(adev);
1540 	if (r) {
1541 		dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
1542 		return r;
1543 	}
1544 	/* init i2c buses */
1545 	amdgpu_atombios_i2c_init(adev);
1546 
1547 	/* Fence driver */
1548 	r = amdgpu_fence_driver_init(adev);
1549 	if (r) {
1550 		dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
1551 		return r;
1552 	}
1553 
1554 	/* init the mode config */
1555 	drm_mode_config_init(adev->ddev);
1556 
1557 	r = amdgpu_init(adev);
1558 	if (r) {
1559 		dev_err(adev->dev, "amdgpu_init failed\n");
1560 		amdgpu_fini(adev);
1561 		return r;
1562 	}
1563 
1564 	adev->accel_working = true;
1565 
1566 	amdgpu_fbdev_init(adev);
1567 
1568 	r = amdgpu_ib_pool_init(adev);
1569 	if (r) {
1570 		dev_err(adev->dev, "IB initialization failed (%d).\n", r);
1571 		return r;
1572 	}
1573 
1574 	r = amdgpu_ctx_init(adev, AMD_SCHED_PRIORITY_KERNEL, &adev->kernel_ctx);
1575 	if (r) {
1576 		dev_err(adev->dev, "failed to create kernel context (%d).\n", r);
1577 		return r;
1578 	}
1579 	r = amdgpu_ib_ring_tests(adev);
1580 	if (r)
1581 		DRM_ERROR("ib ring test failed (%d).\n", r);
1582 
1583 	r = amdgpu_gem_debugfs_init(adev);
1584 	if (r) {
1585 		DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1586 	}
1587 
1588 	r = amdgpu_debugfs_regs_init(adev);
1589 	if (r) {
1590 		DRM_ERROR("registering register debugfs failed (%d).\n", r);
1591 	}
1592 
1593 	if ((amdgpu_testing & 1)) {
1594 		if (adev->accel_working)
1595 			amdgpu_test_moves(adev);
1596 		else
1597 			DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
1598 	}
1599 	if ((amdgpu_testing & 2)) {
1600 		if (adev->accel_working)
1601 			amdgpu_test_syncing(adev);
1602 		else
1603 			DRM_INFO("amdgpu: acceleration disabled, skipping sync tests\n");
1604 	}
1605 	if (amdgpu_benchmarking) {
1606 		if (adev->accel_working)
1607 			amdgpu_benchmark(adev, amdgpu_benchmarking);
1608 		else
1609 			DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
1610 	}
1611 
1612 	/* enable clockgating, etc. after ib tests, etc. since some blocks require
1613 	 * explicit gating rather than handling it automatically.
1614 	 */
1615 	r = amdgpu_late_init(adev);
1616 	if (r) {
1617 		dev_err(adev->dev, "amdgpu_late_init failed\n");
1618 		return r;
1619 	}
1620 
1621 	return 0;
1622 }
1623 
1624 static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev);
1625 
1626 /**
1627  * amdgpu_device_fini - tear down the driver
1628  *
1629  * @adev: amdgpu_device pointer
1630  *
1631  * Tear down the driver info (all asics).
1632  * Called at driver shutdown.
1633  */
1634 void amdgpu_device_fini(struct amdgpu_device *adev)
1635 {
1636 	int r;
1637 
1638 	DRM_INFO("amdgpu: finishing device.\n");
1639 	adev->shutdown = true;
1640 	/* evict vram memory */
1641 	amdgpu_bo_evict_vram(adev);
1642 	amdgpu_ctx_fini(&adev->kernel_ctx);
1643 	amdgpu_ib_pool_fini(adev);
1644 	amdgpu_fence_driver_fini(adev);
1645 	amdgpu_fbdev_fini(adev);
1646 	r = amdgpu_fini(adev);
1647 	kfree(adev->ip_block_status);
1648 	adev->ip_block_status = NULL;
1649 	adev->accel_working = false;
1650 	/* free i2c buses */
1651 	amdgpu_i2c_fini(adev);
1652 	amdgpu_atombios_fini(adev);
1653 	kfree(adev->bios);
1654 	adev->bios = NULL;
1655 	vga_switcheroo_unregister_client(adev->pdev);
1656 	vga_client_register(adev->pdev, NULL, NULL, NULL);
1657 	if (adev->rio_mem)
1658 		pci_iounmap(adev->pdev, adev->rio_mem);
1659 	adev->rio_mem = NULL;
1660 	iounmap(adev->rmmio);
1661 	adev->rmmio = NULL;
1662 	amdgpu_doorbell_fini(adev);
1663 	amdgpu_debugfs_regs_cleanup(adev);
1664 	amdgpu_debugfs_remove_files(adev);
1665 }
1666 
1667 
1668 /*
1669  * Suspend & resume.
1670  */
1671 /**
1672  * amdgpu_suspend_kms - initiate device suspend
1673  *
1674  * @pdev: drm dev pointer
1675  * @state: suspend state
1676  *
1677  * Puts the hw in the suspend state (all asics).
1678  * Returns 0 for success or an error on failure.
1679  * Called at driver suspend.
1680  */
1681 int amdgpu_suspend_kms(struct drm_device *dev, bool suspend, bool fbcon)
1682 {
1683 	struct amdgpu_device *adev;
1684 	struct drm_crtc *crtc;
1685 	struct drm_connector *connector;
1686 	int r;
1687 
1688 	if (dev == NULL || dev->dev_private == NULL) {
1689 		return -ENODEV;
1690 	}
1691 
1692 	adev = dev->dev_private;
1693 
1694 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1695 		return 0;
1696 
1697 	drm_kms_helper_poll_disable(dev);
1698 
1699 	/* turn off display hw */
1700 	drm_modeset_lock_all(dev);
1701 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1702 		drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1703 	}
1704 	drm_modeset_unlock_all(dev);
1705 
1706 	/* unpin the front buffers and cursors */
1707 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1708 		struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1709 		struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
1710 		struct amdgpu_bo *robj;
1711 
1712 		if (amdgpu_crtc->cursor_bo) {
1713 			struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
1714 			r = amdgpu_bo_reserve(aobj, false);
1715 			if (r == 0) {
1716 				amdgpu_bo_unpin(aobj);
1717 				amdgpu_bo_unreserve(aobj);
1718 			}
1719 		}
1720 
1721 		if (rfb == NULL || rfb->obj == NULL) {
1722 			continue;
1723 		}
1724 		robj = gem_to_amdgpu_bo(rfb->obj);
1725 		/* don't unpin kernel fb objects */
1726 		if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
1727 			r = amdgpu_bo_reserve(robj, false);
1728 			if (r == 0) {
1729 				amdgpu_bo_unpin(robj);
1730 				amdgpu_bo_unreserve(robj);
1731 			}
1732 		}
1733 	}
1734 	/* evict vram memory */
1735 	amdgpu_bo_evict_vram(adev);
1736 
1737 	amdgpu_fence_driver_suspend(adev);
1738 
1739 	r = amdgpu_suspend(adev);
1740 
1741 	/* evict remaining vram memory */
1742 	amdgpu_bo_evict_vram(adev);
1743 
1744 	pci_save_state(dev->pdev);
1745 	if (suspend) {
1746 		/* Shut down the device */
1747 		pci_disable_device(dev->pdev);
1748 		pci_set_power_state(dev->pdev, PCI_D3hot);
1749 	}
1750 
1751 	if (fbcon) {
1752 		console_lock();
1753 		amdgpu_fbdev_set_suspend(adev, 1);
1754 		console_unlock();
1755 	}
1756 	return 0;
1757 }
1758 
1759 /**
1760  * amdgpu_resume_kms - initiate device resume
1761  *
1762  * @pdev: drm dev pointer
1763  *
1764  * Bring the hw back to operating state (all asics).
1765  * Returns 0 for success or an error on failure.
1766  * Called at driver resume.
1767  */
1768 int amdgpu_resume_kms(struct drm_device *dev, bool resume, bool fbcon)
1769 {
1770 	struct drm_connector *connector;
1771 	struct amdgpu_device *adev = dev->dev_private;
1772 	struct drm_crtc *crtc;
1773 	int r;
1774 
1775 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1776 		return 0;
1777 
1778 	if (fbcon) {
1779 		console_lock();
1780 	}
1781 	if (resume) {
1782 		pci_set_power_state(dev->pdev, PCI_D0);
1783 		pci_restore_state(dev->pdev);
1784 		if (pci_enable_device(dev->pdev)) {
1785 			if (fbcon)
1786 				console_unlock();
1787 			return -1;
1788 		}
1789 	}
1790 
1791 	/* post card */
1792 	amdgpu_atom_asic_init(adev->mode_info.atom_context);
1793 
1794 	r = amdgpu_resume(adev);
1795 
1796 	amdgpu_fence_driver_resume(adev);
1797 
1798 	r = amdgpu_ib_ring_tests(adev);
1799 	if (r)
1800 		DRM_ERROR("ib ring test failed (%d).\n", r);
1801 
1802 	r = amdgpu_late_init(adev);
1803 	if (r)
1804 		return r;
1805 
1806 	/* pin cursors */
1807 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1808 		struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1809 
1810 		if (amdgpu_crtc->cursor_bo) {
1811 			struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
1812 			r = amdgpu_bo_reserve(aobj, false);
1813 			if (r == 0) {
1814 				r = amdgpu_bo_pin(aobj,
1815 						  AMDGPU_GEM_DOMAIN_VRAM,
1816 						  &amdgpu_crtc->cursor_addr);
1817 				if (r != 0)
1818 					DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
1819 				amdgpu_bo_unreserve(aobj);
1820 			}
1821 		}
1822 	}
1823 
1824 	/* blat the mode back in */
1825 	if (fbcon) {
1826 		drm_helper_resume_force_mode(dev);
1827 		/* turn on display hw */
1828 		drm_modeset_lock_all(dev);
1829 		list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1830 			drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
1831 		}
1832 		drm_modeset_unlock_all(dev);
1833 	}
1834 
1835 	drm_kms_helper_poll_enable(dev);
1836 	drm_helper_hpd_irq_event(dev);
1837 
1838 	if (fbcon) {
1839 		amdgpu_fbdev_set_suspend(adev, 0);
1840 		console_unlock();
1841 	}
1842 
1843 	return 0;
1844 }
1845 
1846 /**
1847  * amdgpu_gpu_reset - reset the asic
1848  *
1849  * @adev: amdgpu device pointer
1850  *
1851  * Attempt the reset the GPU if it has hung (all asics).
1852  * Returns 0 for success or an error on failure.
1853  */
1854 int amdgpu_gpu_reset(struct amdgpu_device *adev)
1855 {
1856 	unsigned ring_sizes[AMDGPU_MAX_RINGS];
1857 	uint32_t *ring_data[AMDGPU_MAX_RINGS];
1858 
1859 	bool saved = false;
1860 
1861 	int i, r;
1862 	int resched;
1863 
1864 	atomic_inc(&adev->gpu_reset_counter);
1865 
1866 	/* block TTM */
1867 	resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
1868 
1869 	r = amdgpu_suspend(adev);
1870 
1871 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1872 		struct amdgpu_ring *ring = adev->rings[i];
1873 		if (!ring)
1874 			continue;
1875 
1876 		ring_sizes[i] = amdgpu_ring_backup(ring, &ring_data[i]);
1877 		if (ring_sizes[i]) {
1878 			saved = true;
1879 			dev_info(adev->dev, "Saved %d dwords of commands "
1880 				 "on ring %d.\n", ring_sizes[i], i);
1881 		}
1882 	}
1883 
1884 retry:
1885 	r = amdgpu_asic_reset(adev);
1886 	/* post card */
1887 	amdgpu_atom_asic_init(adev->mode_info.atom_context);
1888 
1889 	if (!r) {
1890 		dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
1891 		r = amdgpu_resume(adev);
1892 	}
1893 
1894 	if (!r) {
1895 		for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1896 			struct amdgpu_ring *ring = adev->rings[i];
1897 			if (!ring)
1898 				continue;
1899 
1900 			amdgpu_ring_restore(ring, ring_sizes[i], ring_data[i]);
1901 			ring_sizes[i] = 0;
1902 			ring_data[i] = NULL;
1903 		}
1904 
1905 		r = amdgpu_ib_ring_tests(adev);
1906 		if (r) {
1907 			dev_err(adev->dev, "ib ring test failed (%d).\n", r);
1908 			if (saved) {
1909 				saved = false;
1910 				r = amdgpu_suspend(adev);
1911 				goto retry;
1912 			}
1913 		}
1914 	} else {
1915 		amdgpu_fence_driver_force_completion(adev);
1916 		for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1917 			if (adev->rings[i])
1918 				kfree(ring_data[i]);
1919 		}
1920 	}
1921 
1922 	drm_helper_resume_force_mode(adev->ddev);
1923 
1924 	ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
1925 	if (r) {
1926 		/* bad news, how to tell it to userspace ? */
1927 		dev_info(adev->dev, "GPU reset failed\n");
1928 	}
1929 
1930 	return r;
1931 }
1932 
1933 void amdgpu_get_pcie_info(struct amdgpu_device *adev)
1934 {
1935 	u32 mask;
1936 	int ret;
1937 
1938 	if (pci_is_root_bus(adev->pdev->bus))
1939 		return;
1940 
1941 	if (amdgpu_pcie_gen2 == 0)
1942 		return;
1943 
1944 	if (adev->flags & AMD_IS_APU)
1945 		return;
1946 
1947 	ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
1948 	if (!ret) {
1949 		adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
1950 					  CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
1951 					  CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
1952 
1953 		if (mask & DRM_PCIE_SPEED_25)
1954 			adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
1955 		if (mask & DRM_PCIE_SPEED_50)
1956 			adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
1957 		if (mask & DRM_PCIE_SPEED_80)
1958 			adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
1959 	}
1960 	ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
1961 	if (!ret) {
1962 		switch (mask) {
1963 		case 32:
1964 			adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
1965 						  CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
1966 						  CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
1967 						  CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
1968 						  CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
1969 						  CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
1970 						  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
1971 			break;
1972 		case 16:
1973 			adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
1974 						  CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
1975 						  CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
1976 						  CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
1977 						  CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
1978 						  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
1979 			break;
1980 		case 12:
1981 			adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
1982 						  CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
1983 						  CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
1984 						  CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
1985 						  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
1986 			break;
1987 		case 8:
1988 			adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
1989 						  CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
1990 						  CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
1991 						  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
1992 			break;
1993 		case 4:
1994 			adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
1995 						  CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
1996 						  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
1997 			break;
1998 		case 2:
1999 			adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2000 						  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2001 			break;
2002 		case 1:
2003 			adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
2004 			break;
2005 		default:
2006 			break;
2007 		}
2008 	}
2009 }
2010 
2011 /*
2012  * Debugfs
2013  */
2014 int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
2015 			     struct drm_info_list *files,
2016 			     unsigned nfiles)
2017 {
2018 	unsigned i;
2019 
2020 	for (i = 0; i < adev->debugfs_count; i++) {
2021 		if (adev->debugfs[i].files == files) {
2022 			/* Already registered */
2023 			return 0;
2024 		}
2025 	}
2026 
2027 	i = adev->debugfs_count + 1;
2028 	if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
2029 		DRM_ERROR("Reached maximum number of debugfs components.\n");
2030 		DRM_ERROR("Report so we increase "
2031 			  "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
2032 		return -EINVAL;
2033 	}
2034 	adev->debugfs[adev->debugfs_count].files = files;
2035 	adev->debugfs[adev->debugfs_count].num_files = nfiles;
2036 	adev->debugfs_count = i;
2037 #if defined(CONFIG_DEBUG_FS)
2038 	drm_debugfs_create_files(files, nfiles,
2039 				 adev->ddev->control->debugfs_root,
2040 				 adev->ddev->control);
2041 	drm_debugfs_create_files(files, nfiles,
2042 				 adev->ddev->primary->debugfs_root,
2043 				 adev->ddev->primary);
2044 #endif
2045 	return 0;
2046 }
2047 
2048 static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev)
2049 {
2050 #if defined(CONFIG_DEBUG_FS)
2051 	unsigned i;
2052 
2053 	for (i = 0; i < adev->debugfs_count; i++) {
2054 		drm_debugfs_remove_files(adev->debugfs[i].files,
2055 					 adev->debugfs[i].num_files,
2056 					 adev->ddev->control);
2057 		drm_debugfs_remove_files(adev->debugfs[i].files,
2058 					 adev->debugfs[i].num_files,
2059 					 adev->ddev->primary);
2060 	}
2061 #endif
2062 }
2063 
2064 #if defined(CONFIG_DEBUG_FS)
2065 
2066 static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
2067 					size_t size, loff_t *pos)
2068 {
2069 	struct amdgpu_device *adev = f->f_inode->i_private;
2070 	ssize_t result = 0;
2071 	int r;
2072 
2073 	if (size & 0x3 || *pos & 0x3)
2074 		return -EINVAL;
2075 
2076 	while (size) {
2077 		uint32_t value;
2078 
2079 		if (*pos > adev->rmmio_size)
2080 			return result;
2081 
2082 		value = RREG32(*pos >> 2);
2083 		r = put_user(value, (uint32_t *)buf);
2084 		if (r)
2085 			return r;
2086 
2087 		result += 4;
2088 		buf += 4;
2089 		*pos += 4;
2090 		size -= 4;
2091 	}
2092 
2093 	return result;
2094 }
2095 
2096 static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
2097 					 size_t size, loff_t *pos)
2098 {
2099 	struct amdgpu_device *adev = f->f_inode->i_private;
2100 	ssize_t result = 0;
2101 	int r;
2102 
2103 	if (size & 0x3 || *pos & 0x3)
2104 		return -EINVAL;
2105 
2106 	while (size) {
2107 		uint32_t value;
2108 
2109 		if (*pos > adev->rmmio_size)
2110 			return result;
2111 
2112 		r = get_user(value, (uint32_t *)buf);
2113 		if (r)
2114 			return r;
2115 
2116 		WREG32(*pos >> 2, value);
2117 
2118 		result += 4;
2119 		buf += 4;
2120 		*pos += 4;
2121 		size -= 4;
2122 	}
2123 
2124 	return result;
2125 }
2126 
2127 static const struct file_operations amdgpu_debugfs_regs_fops = {
2128 	.owner = THIS_MODULE,
2129 	.read = amdgpu_debugfs_regs_read,
2130 	.write = amdgpu_debugfs_regs_write,
2131 	.llseek = default_llseek
2132 };
2133 
2134 static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
2135 {
2136 	struct drm_minor *minor = adev->ddev->primary;
2137 	struct dentry *ent, *root = minor->debugfs_root;
2138 
2139 	ent = debugfs_create_file("amdgpu_regs", S_IFREG | S_IRUGO, root,
2140 				  adev, &amdgpu_debugfs_regs_fops);
2141 	if (IS_ERR(ent))
2142 		return PTR_ERR(ent);
2143 	i_size_write(ent->d_inode, adev->rmmio_size);
2144 	adev->debugfs_regs = ent;
2145 
2146 	return 0;
2147 }
2148 
2149 static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
2150 {
2151 	debugfs_remove(adev->debugfs_regs);
2152 	adev->debugfs_regs = NULL;
2153 }
2154 
2155 int amdgpu_debugfs_init(struct drm_minor *minor)
2156 {
2157 	return 0;
2158 }
2159 
2160 void amdgpu_debugfs_cleanup(struct drm_minor *minor)
2161 {
2162 }
2163 #else
2164 static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
2165 {
2166 	return 0;
2167 }
2168 static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
2169 #endif
2170