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