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