xref: /openbmc/linux/drivers/gpu/drm/radeon/radeon_device.c (revision 089920f21db0108fb105ecfd81de4c92d88f06d0)
1771fe6b9SJerome Glisse /*
2771fe6b9SJerome Glisse  * Copyright 2008 Advanced Micro Devices, Inc.
3771fe6b9SJerome Glisse  * Copyright 2008 Red Hat Inc.
4771fe6b9SJerome Glisse  * Copyright 2009 Jerome Glisse.
5771fe6b9SJerome Glisse  *
6771fe6b9SJerome Glisse  * Permission is hereby granted, free of charge, to any person obtaining a
7771fe6b9SJerome Glisse  * copy of this software and associated documentation files (the "Software"),
8771fe6b9SJerome Glisse  * to deal in the Software without restriction, including without limitation
9771fe6b9SJerome Glisse  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10771fe6b9SJerome Glisse  * and/or sell copies of the Software, and to permit persons to whom the
11771fe6b9SJerome Glisse  * Software is furnished to do so, subject to the following conditions:
12771fe6b9SJerome Glisse  *
13771fe6b9SJerome Glisse  * The above copyright notice and this permission notice shall be included in
14771fe6b9SJerome Glisse  * all copies or substantial portions of the Software.
15771fe6b9SJerome Glisse  *
16771fe6b9SJerome Glisse  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17771fe6b9SJerome Glisse  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18771fe6b9SJerome Glisse  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19771fe6b9SJerome Glisse  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20771fe6b9SJerome Glisse  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21771fe6b9SJerome Glisse  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22771fe6b9SJerome Glisse  * OTHER DEALINGS IN THE SOFTWARE.
23771fe6b9SJerome Glisse  *
24771fe6b9SJerome Glisse  * Authors: Dave Airlie
25771fe6b9SJerome Glisse  *          Alex Deucher
26771fe6b9SJerome Glisse  *          Jerome Glisse
27771fe6b9SJerome Glisse  */
28771fe6b9SJerome Glisse #include <linux/console.h>
295a0e3ad6STejun Heo #include <linux/slab.h>
30771fe6b9SJerome Glisse #include <drm/drmP.h>
31771fe6b9SJerome Glisse #include <drm/drm_crtc_helper.h>
32771fe6b9SJerome Glisse #include <drm/radeon_drm.h>
3328d52043SDave Airlie #include <linux/vgaarb.h>
346a9ee8afSDave Airlie #include <linux/vga_switcheroo.h>
35bcc65fd8SMatthew Garrett #include <linux/efi.h>
36771fe6b9SJerome Glisse #include "radeon_reg.h"
37771fe6b9SJerome Glisse #include "radeon.h"
38771fe6b9SJerome Glisse #include "atom.h"
39771fe6b9SJerome Glisse 
401b5331d9SJerome Glisse static const char radeon_family_name[][16] = {
411b5331d9SJerome Glisse 	"R100",
421b5331d9SJerome Glisse 	"RV100",
431b5331d9SJerome Glisse 	"RS100",
441b5331d9SJerome Glisse 	"RV200",
451b5331d9SJerome Glisse 	"RS200",
461b5331d9SJerome Glisse 	"R200",
471b5331d9SJerome Glisse 	"RV250",
481b5331d9SJerome Glisse 	"RS300",
491b5331d9SJerome Glisse 	"RV280",
501b5331d9SJerome Glisse 	"R300",
511b5331d9SJerome Glisse 	"R350",
521b5331d9SJerome Glisse 	"RV350",
531b5331d9SJerome Glisse 	"RV380",
541b5331d9SJerome Glisse 	"R420",
551b5331d9SJerome Glisse 	"R423",
561b5331d9SJerome Glisse 	"RV410",
571b5331d9SJerome Glisse 	"RS400",
581b5331d9SJerome Glisse 	"RS480",
591b5331d9SJerome Glisse 	"RS600",
601b5331d9SJerome Glisse 	"RS690",
611b5331d9SJerome Glisse 	"RS740",
621b5331d9SJerome Glisse 	"RV515",
631b5331d9SJerome Glisse 	"R520",
641b5331d9SJerome Glisse 	"RV530",
651b5331d9SJerome Glisse 	"RV560",
661b5331d9SJerome Glisse 	"RV570",
671b5331d9SJerome Glisse 	"R580",
681b5331d9SJerome Glisse 	"R600",
691b5331d9SJerome Glisse 	"RV610",
701b5331d9SJerome Glisse 	"RV630",
711b5331d9SJerome Glisse 	"RV670",
721b5331d9SJerome Glisse 	"RV620",
731b5331d9SJerome Glisse 	"RV635",
741b5331d9SJerome Glisse 	"RS780",
751b5331d9SJerome Glisse 	"RS880",
761b5331d9SJerome Glisse 	"RV770",
771b5331d9SJerome Glisse 	"RV730",
781b5331d9SJerome Glisse 	"RV710",
791b5331d9SJerome Glisse 	"RV740",
801b5331d9SJerome Glisse 	"CEDAR",
811b5331d9SJerome Glisse 	"REDWOOD",
821b5331d9SJerome Glisse 	"JUNIPER",
831b5331d9SJerome Glisse 	"CYPRESS",
841b5331d9SJerome Glisse 	"HEMLOCK",
85b08ebe7eSAlex Deucher 	"PALM",
864df64e65SAlex Deucher 	"SUMO",
874df64e65SAlex Deucher 	"SUMO2",
881fe18305SAlex Deucher 	"BARTS",
891fe18305SAlex Deucher 	"TURKS",
901fe18305SAlex Deucher 	"CAICOS",
91b7cfc9feSAlex Deucher 	"CAYMAN",
928848f759SAlex Deucher 	"ARUBA",
93cb28bb34SAlex Deucher 	"TAHITI",
94cb28bb34SAlex Deucher 	"PITCAIRN",
95cb28bb34SAlex Deucher 	"VERDE",
96624d3524SAlex Deucher 	"OLAND",
97b5d9d726SAlex Deucher 	"HAINAN",
981b5331d9SJerome Glisse 	"LAST",
991b5331d9SJerome Glisse };
1001b5331d9SJerome Glisse 
1010c195119SAlex Deucher /**
1022e1b65f9SAlex Deucher  * radeon_program_register_sequence - program an array of registers.
1032e1b65f9SAlex Deucher  *
1042e1b65f9SAlex Deucher  * @rdev: radeon_device pointer
1052e1b65f9SAlex Deucher  * @registers: pointer to the register array
1062e1b65f9SAlex Deucher  * @array_size: size of the register array
1072e1b65f9SAlex Deucher  *
1082e1b65f9SAlex Deucher  * Programs an array or registers with and and or masks.
1092e1b65f9SAlex Deucher  * This is a helper for setting golden registers.
1102e1b65f9SAlex Deucher  */
1112e1b65f9SAlex Deucher void radeon_program_register_sequence(struct radeon_device *rdev,
1122e1b65f9SAlex Deucher 				      const u32 *registers,
1132e1b65f9SAlex Deucher 				      const u32 array_size)
1142e1b65f9SAlex Deucher {
1152e1b65f9SAlex Deucher 	u32 tmp, reg, and_mask, or_mask;
1162e1b65f9SAlex Deucher 	int i;
1172e1b65f9SAlex Deucher 
1182e1b65f9SAlex Deucher 	if (array_size % 3)
1192e1b65f9SAlex Deucher 		return;
1202e1b65f9SAlex Deucher 
1212e1b65f9SAlex Deucher 	for (i = 0; i < array_size; i +=3) {
1222e1b65f9SAlex Deucher 		reg = registers[i + 0];
1232e1b65f9SAlex Deucher 		and_mask = registers[i + 1];
1242e1b65f9SAlex Deucher 		or_mask = registers[i + 2];
1252e1b65f9SAlex Deucher 
1262e1b65f9SAlex Deucher 		if (and_mask == 0xffffffff) {
1272e1b65f9SAlex Deucher 			tmp = or_mask;
1282e1b65f9SAlex Deucher 		} else {
1292e1b65f9SAlex Deucher 			tmp = RREG32(reg);
1302e1b65f9SAlex Deucher 			tmp &= ~and_mask;
1312e1b65f9SAlex Deucher 			tmp |= or_mask;
1322e1b65f9SAlex Deucher 		}
1332e1b65f9SAlex Deucher 		WREG32(reg, tmp);
1342e1b65f9SAlex Deucher 	}
1352e1b65f9SAlex Deucher }
1362e1b65f9SAlex Deucher 
1372e1b65f9SAlex Deucher /**
1380c195119SAlex Deucher  * radeon_surface_init - Clear GPU surface registers.
1390c195119SAlex Deucher  *
1400c195119SAlex Deucher  * @rdev: radeon_device pointer
1410c195119SAlex Deucher  *
1420c195119SAlex Deucher  * Clear GPU surface registers (r1xx-r5xx).
143b1e3a6d1SMichel Dänzer  */
1443ce0a23dSJerome Glisse void radeon_surface_init(struct radeon_device *rdev)
145b1e3a6d1SMichel Dänzer {
146b1e3a6d1SMichel Dänzer 	/* FIXME: check this out */
147b1e3a6d1SMichel Dänzer 	if (rdev->family < CHIP_R600) {
148b1e3a6d1SMichel Dänzer 		int i;
149b1e3a6d1SMichel Dänzer 
150550e2d92SDave Airlie 		for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
151550e2d92SDave Airlie 			if (rdev->surface_regs[i].bo)
152550e2d92SDave Airlie 				radeon_bo_get_surface_reg(rdev->surface_regs[i].bo);
153550e2d92SDave Airlie 			else
154550e2d92SDave Airlie 				radeon_clear_surface_reg(rdev, i);
155b1e3a6d1SMichel Dänzer 		}
156e024e110SDave Airlie 		/* enable surfaces */
157e024e110SDave Airlie 		WREG32(RADEON_SURFACE_CNTL, 0);
158b1e3a6d1SMichel Dänzer 	}
159b1e3a6d1SMichel Dänzer }
160b1e3a6d1SMichel Dänzer 
161b1e3a6d1SMichel Dänzer /*
162771fe6b9SJerome Glisse  * GPU scratch registers helpers function.
163771fe6b9SJerome Glisse  */
1640c195119SAlex Deucher /**
1650c195119SAlex Deucher  * radeon_scratch_init - Init scratch register driver information.
1660c195119SAlex Deucher  *
1670c195119SAlex Deucher  * @rdev: radeon_device pointer
1680c195119SAlex Deucher  *
1690c195119SAlex Deucher  * Init CP scratch register driver information (r1xx-r5xx)
1700c195119SAlex Deucher  */
1713ce0a23dSJerome Glisse void radeon_scratch_init(struct radeon_device *rdev)
172771fe6b9SJerome Glisse {
173771fe6b9SJerome Glisse 	int i;
174771fe6b9SJerome Glisse 
175771fe6b9SJerome Glisse 	/* FIXME: check this out */
176771fe6b9SJerome Glisse 	if (rdev->family < CHIP_R300) {
177771fe6b9SJerome Glisse 		rdev->scratch.num_reg = 5;
178771fe6b9SJerome Glisse 	} else {
179771fe6b9SJerome Glisse 		rdev->scratch.num_reg = 7;
180771fe6b9SJerome Glisse 	}
181724c80e1SAlex Deucher 	rdev->scratch.reg_base = RADEON_SCRATCH_REG0;
182771fe6b9SJerome Glisse 	for (i = 0; i < rdev->scratch.num_reg; i++) {
183771fe6b9SJerome Glisse 		rdev->scratch.free[i] = true;
184724c80e1SAlex Deucher 		rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4);
185771fe6b9SJerome Glisse 	}
186771fe6b9SJerome Glisse }
187771fe6b9SJerome Glisse 
1880c195119SAlex Deucher /**
1890c195119SAlex Deucher  * radeon_scratch_get - Allocate a scratch register
1900c195119SAlex Deucher  *
1910c195119SAlex Deucher  * @rdev: radeon_device pointer
1920c195119SAlex Deucher  * @reg: scratch register mmio offset
1930c195119SAlex Deucher  *
1940c195119SAlex Deucher  * Allocate a CP scratch register for use by the driver (all asics).
1950c195119SAlex Deucher  * Returns 0 on success or -EINVAL on failure.
1960c195119SAlex Deucher  */
197771fe6b9SJerome Glisse int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg)
198771fe6b9SJerome Glisse {
199771fe6b9SJerome Glisse 	int i;
200771fe6b9SJerome Glisse 
201771fe6b9SJerome Glisse 	for (i = 0; i < rdev->scratch.num_reg; i++) {
202771fe6b9SJerome Glisse 		if (rdev->scratch.free[i]) {
203771fe6b9SJerome Glisse 			rdev->scratch.free[i] = false;
204771fe6b9SJerome Glisse 			*reg = rdev->scratch.reg[i];
205771fe6b9SJerome Glisse 			return 0;
206771fe6b9SJerome Glisse 		}
207771fe6b9SJerome Glisse 	}
208771fe6b9SJerome Glisse 	return -EINVAL;
209771fe6b9SJerome Glisse }
210771fe6b9SJerome Glisse 
2110c195119SAlex Deucher /**
2120c195119SAlex Deucher  * radeon_scratch_free - Free a scratch register
2130c195119SAlex Deucher  *
2140c195119SAlex Deucher  * @rdev: radeon_device pointer
2150c195119SAlex Deucher  * @reg: scratch register mmio offset
2160c195119SAlex Deucher  *
2170c195119SAlex Deucher  * Free a CP scratch register allocated for use by the driver (all asics)
2180c195119SAlex Deucher  */
219771fe6b9SJerome Glisse void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg)
220771fe6b9SJerome Glisse {
221771fe6b9SJerome Glisse 	int i;
222771fe6b9SJerome Glisse 
223771fe6b9SJerome Glisse 	for (i = 0; i < rdev->scratch.num_reg; i++) {
224771fe6b9SJerome Glisse 		if (rdev->scratch.reg[i] == reg) {
225771fe6b9SJerome Glisse 			rdev->scratch.free[i] = true;
226771fe6b9SJerome Glisse 			return;
227771fe6b9SJerome Glisse 		}
228771fe6b9SJerome Glisse 	}
229771fe6b9SJerome Glisse }
230771fe6b9SJerome Glisse 
2310c195119SAlex Deucher /*
2320c195119SAlex Deucher  * radeon_wb_*()
2330c195119SAlex Deucher  * Writeback is the the method by which the the GPU updates special pages
2340c195119SAlex Deucher  * in memory with the status of certain GPU events (fences, ring pointers,
2350c195119SAlex Deucher  * etc.).
2360c195119SAlex Deucher  */
2370c195119SAlex Deucher 
2380c195119SAlex Deucher /**
2390c195119SAlex Deucher  * radeon_wb_disable - Disable Writeback
2400c195119SAlex Deucher  *
2410c195119SAlex Deucher  * @rdev: radeon_device pointer
2420c195119SAlex Deucher  *
2430c195119SAlex Deucher  * Disables Writeback (all asics).  Used for suspend.
2440c195119SAlex Deucher  */
245724c80e1SAlex Deucher void radeon_wb_disable(struct radeon_device *rdev)
246724c80e1SAlex Deucher {
247724c80e1SAlex Deucher 	rdev->wb.enabled = false;
248724c80e1SAlex Deucher }
249724c80e1SAlex Deucher 
2500c195119SAlex Deucher /**
2510c195119SAlex Deucher  * radeon_wb_fini - Disable Writeback and free memory
2520c195119SAlex Deucher  *
2530c195119SAlex Deucher  * @rdev: radeon_device pointer
2540c195119SAlex Deucher  *
2550c195119SAlex Deucher  * Disables Writeback and frees the Writeback memory (all asics).
2560c195119SAlex Deucher  * Used at driver shutdown.
2570c195119SAlex Deucher  */
258724c80e1SAlex Deucher void radeon_wb_fini(struct radeon_device *rdev)
259724c80e1SAlex Deucher {
260724c80e1SAlex Deucher 	radeon_wb_disable(rdev);
261724c80e1SAlex Deucher 	if (rdev->wb.wb_obj) {
262*089920f2SJerome Glisse 		if (!radeon_bo_reserve(rdev->wb.wb_obj, false)) {
263*089920f2SJerome Glisse 			radeon_bo_kunmap(rdev->wb.wb_obj);
264*089920f2SJerome Glisse 			radeon_bo_unpin(rdev->wb.wb_obj);
265*089920f2SJerome Glisse 			radeon_bo_unreserve(rdev->wb.wb_obj);
266*089920f2SJerome Glisse 		}
267724c80e1SAlex Deucher 		radeon_bo_unref(&rdev->wb.wb_obj);
268724c80e1SAlex Deucher 		rdev->wb.wb = NULL;
269724c80e1SAlex Deucher 		rdev->wb.wb_obj = NULL;
270724c80e1SAlex Deucher 	}
271724c80e1SAlex Deucher }
272724c80e1SAlex Deucher 
2730c195119SAlex Deucher /**
2740c195119SAlex Deucher  * radeon_wb_init- Init Writeback driver info and allocate memory
2750c195119SAlex Deucher  *
2760c195119SAlex Deucher  * @rdev: radeon_device pointer
2770c195119SAlex Deucher  *
2780c195119SAlex Deucher  * Disables Writeback and frees the Writeback memory (all asics).
2790c195119SAlex Deucher  * Used at driver startup.
2800c195119SAlex Deucher  * Returns 0 on success or an -error on failure.
2810c195119SAlex Deucher  */
282724c80e1SAlex Deucher int radeon_wb_init(struct radeon_device *rdev)
283724c80e1SAlex Deucher {
284724c80e1SAlex Deucher 	int r;
285724c80e1SAlex Deucher 
286724c80e1SAlex Deucher 	if (rdev->wb.wb_obj == NULL) {
287441921d5SDaniel Vetter 		r = radeon_bo_create(rdev, RADEON_GPU_PAGE_SIZE, PAGE_SIZE, true,
28840f5cf99SAlex Deucher 				     RADEON_GEM_DOMAIN_GTT, NULL, &rdev->wb.wb_obj);
289724c80e1SAlex Deucher 		if (r) {
290724c80e1SAlex Deucher 			dev_warn(rdev->dev, "(%d) create WB bo failed\n", r);
291724c80e1SAlex Deucher 			return r;
292724c80e1SAlex Deucher 		}
293724c80e1SAlex Deucher 		r = radeon_bo_reserve(rdev->wb.wb_obj, false);
294724c80e1SAlex Deucher 		if (unlikely(r != 0)) {
295724c80e1SAlex Deucher 			radeon_wb_fini(rdev);
296724c80e1SAlex Deucher 			return r;
297724c80e1SAlex Deucher 		}
298724c80e1SAlex Deucher 		r = radeon_bo_pin(rdev->wb.wb_obj, RADEON_GEM_DOMAIN_GTT,
299724c80e1SAlex Deucher 				&rdev->wb.gpu_addr);
300724c80e1SAlex Deucher 		if (r) {
301724c80e1SAlex Deucher 			radeon_bo_unreserve(rdev->wb.wb_obj);
302724c80e1SAlex Deucher 			dev_warn(rdev->dev, "(%d) pin WB bo failed\n", r);
303724c80e1SAlex Deucher 			radeon_wb_fini(rdev);
304724c80e1SAlex Deucher 			return r;
305724c80e1SAlex Deucher 		}
306724c80e1SAlex Deucher 		r = radeon_bo_kmap(rdev->wb.wb_obj, (void **)&rdev->wb.wb);
307724c80e1SAlex Deucher 		radeon_bo_unreserve(rdev->wb.wb_obj);
308724c80e1SAlex Deucher 		if (r) {
309724c80e1SAlex Deucher 			dev_warn(rdev->dev, "(%d) map WB bo failed\n", r);
310724c80e1SAlex Deucher 			radeon_wb_fini(rdev);
311724c80e1SAlex Deucher 			return r;
312724c80e1SAlex Deucher 		}
313*089920f2SJerome Glisse 	}
314724c80e1SAlex Deucher 
315e6ba7599SAlex Deucher 	/* clear wb memory */
316e6ba7599SAlex Deucher 	memset((char *)rdev->wb.wb, 0, RADEON_GPU_PAGE_SIZE);
317d0f8a854SAlex Deucher 	/* disable event_write fences */
318d0f8a854SAlex Deucher 	rdev->wb.use_event = false;
319724c80e1SAlex Deucher 	/* disabled via module param */
3203b7a2b24SJerome Glisse 	if (radeon_no_wb == 1) {
321724c80e1SAlex Deucher 		rdev->wb.enabled = false;
3223b7a2b24SJerome Glisse 	} else {
323724c80e1SAlex Deucher 		if (rdev->flags & RADEON_IS_AGP) {
32428eebb70SAlex Deucher 			/* often unreliable on AGP */
32528eebb70SAlex Deucher 			rdev->wb.enabled = false;
32628eebb70SAlex Deucher 		} else if (rdev->family < CHIP_R300) {
32728eebb70SAlex Deucher 			/* often unreliable on pre-r300 */
328724c80e1SAlex Deucher 			rdev->wb.enabled = false;
329d0f8a854SAlex Deucher 		} else {
330724c80e1SAlex Deucher 			rdev->wb.enabled = true;
331d0f8a854SAlex Deucher 			/* event_write fences are only available on r600+ */
3323b7a2b24SJerome Glisse 			if (rdev->family >= CHIP_R600) {
333d0f8a854SAlex Deucher 				rdev->wb.use_event = true;
334d0f8a854SAlex Deucher 			}
335724c80e1SAlex Deucher 		}
3363b7a2b24SJerome Glisse 	}
337c994ead6SAlex Deucher 	/* always use writeback/events on NI, APUs */
338c994ead6SAlex Deucher 	if (rdev->family >= CHIP_PALM) {
3397d52785dSAlex Deucher 		rdev->wb.enabled = true;
3407d52785dSAlex Deucher 		rdev->wb.use_event = true;
3417d52785dSAlex Deucher 	}
342724c80e1SAlex Deucher 
343724c80e1SAlex Deucher 	dev_info(rdev->dev, "WB %sabled\n", rdev->wb.enabled ? "en" : "dis");
344724c80e1SAlex Deucher 
345724c80e1SAlex Deucher 	return 0;
346724c80e1SAlex Deucher }
347724c80e1SAlex Deucher 
348d594e46aSJerome Glisse /**
349d594e46aSJerome Glisse  * radeon_vram_location - try to find VRAM location
350d594e46aSJerome Glisse  * @rdev: radeon device structure holding all necessary informations
351d594e46aSJerome Glisse  * @mc: memory controller structure holding memory informations
352d594e46aSJerome Glisse  * @base: base address at which to put VRAM
353d594e46aSJerome Glisse  *
354d594e46aSJerome Glisse  * Function will place try to place VRAM at base address provided
355d594e46aSJerome Glisse  * as parameter (which is so far either PCI aperture address or
356d594e46aSJerome Glisse  * for IGP TOM base address).
357d594e46aSJerome Glisse  *
358d594e46aSJerome Glisse  * If there is not enough space to fit the unvisible VRAM in the 32bits
359d594e46aSJerome Glisse  * address space then we limit the VRAM size to the aperture.
360d594e46aSJerome Glisse  *
361d594e46aSJerome Glisse  * If we are using AGP and if the AGP aperture doesn't allow us to have
362d594e46aSJerome Glisse  * room for all the VRAM than we restrict the VRAM to the PCI aperture
363d594e46aSJerome Glisse  * size and print a warning.
364d594e46aSJerome Glisse  *
365d594e46aSJerome Glisse  * This function will never fails, worst case are limiting VRAM.
366d594e46aSJerome Glisse  *
367d594e46aSJerome Glisse  * Note: GTT start, end, size should be initialized before calling this
368d594e46aSJerome Glisse  * function on AGP platform.
369d594e46aSJerome Glisse  *
37025985edcSLucas De Marchi  * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
371d594e46aSJerome Glisse  * this shouldn't be a problem as we are using the PCI aperture as a reference.
372d594e46aSJerome Glisse  * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
373d594e46aSJerome Glisse  * not IGP.
374d594e46aSJerome Glisse  *
375d594e46aSJerome Glisse  * Note: we use mc_vram_size as on some board we need to program the mc to
376d594e46aSJerome Glisse  * cover the whole aperture even if VRAM size is inferior to aperture size
377d594e46aSJerome Glisse  * Novell bug 204882 + along with lots of ubuntu ones
378d594e46aSJerome Glisse  *
379d594e46aSJerome Glisse  * Note: when limiting vram it's safe to overwritte real_vram_size because
380d594e46aSJerome Glisse  * we are not in case where real_vram_size is inferior to mc_vram_size (ie
381d594e46aSJerome Glisse  * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
382d594e46aSJerome Glisse  * ones)
383d594e46aSJerome Glisse  *
384d594e46aSJerome Glisse  * Note: IGP TOM addr should be the same as the aperture addr, we don't
385d594e46aSJerome Glisse  * explicitly check for that thought.
386d594e46aSJerome Glisse  *
387d594e46aSJerome Glisse  * FIXME: when reducing VRAM size align new size on power of 2.
388771fe6b9SJerome Glisse  */
389d594e46aSJerome Glisse void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base)
390771fe6b9SJerome Glisse {
3911bcb04f7SChristian König 	uint64_t limit = (uint64_t)radeon_vram_limit << 20;
3921bcb04f7SChristian König 
393d594e46aSJerome Glisse 	mc->vram_start = base;
3949ed8b1f9SAlex Deucher 	if (mc->mc_vram_size > (rdev->mc.mc_mask - base + 1)) {
395d594e46aSJerome Glisse 		dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
396d594e46aSJerome Glisse 		mc->real_vram_size = mc->aper_size;
397d594e46aSJerome Glisse 		mc->mc_vram_size = mc->aper_size;
398771fe6b9SJerome Glisse 	}
399d594e46aSJerome Glisse 	mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
4002cbeb4efSJerome Glisse 	if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_start <= mc->gtt_end) {
401d594e46aSJerome Glisse 		dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
402d594e46aSJerome Glisse 		mc->real_vram_size = mc->aper_size;
403d594e46aSJerome Glisse 		mc->mc_vram_size = mc->aper_size;
404771fe6b9SJerome Glisse 	}
405d594e46aSJerome Glisse 	mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
4061bcb04f7SChristian König 	if (limit && limit < mc->real_vram_size)
4071bcb04f7SChristian König 		mc->real_vram_size = limit;
408dd7cc55aSAlex Deucher 	dev_info(rdev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
409d594e46aSJerome Glisse 			mc->mc_vram_size >> 20, mc->vram_start,
410d594e46aSJerome Glisse 			mc->vram_end, mc->real_vram_size >> 20);
411771fe6b9SJerome Glisse }
412771fe6b9SJerome Glisse 
413d594e46aSJerome Glisse /**
414d594e46aSJerome Glisse  * radeon_gtt_location - try to find GTT location
415d594e46aSJerome Glisse  * @rdev: radeon device structure holding all necessary informations
416d594e46aSJerome Glisse  * @mc: memory controller structure holding memory informations
417d594e46aSJerome Glisse  *
418d594e46aSJerome Glisse  * Function will place try to place GTT before or after VRAM.
419d594e46aSJerome Glisse  *
420d594e46aSJerome Glisse  * If GTT size is bigger than space left then we ajust GTT size.
421d594e46aSJerome Glisse  * Thus function will never fails.
422d594e46aSJerome Glisse  *
423d594e46aSJerome Glisse  * FIXME: when reducing GTT size align new size on power of 2.
424d594e46aSJerome Glisse  */
425d594e46aSJerome Glisse void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc)
426d594e46aSJerome Glisse {
427d594e46aSJerome Glisse 	u64 size_af, size_bf;
428d594e46aSJerome Glisse 
4299ed8b1f9SAlex Deucher 	size_af = ((rdev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
4308d369bb1SAlex Deucher 	size_bf = mc->vram_start & ~mc->gtt_base_align;
431d594e46aSJerome Glisse 	if (size_bf > size_af) {
432d594e46aSJerome Glisse 		if (mc->gtt_size > size_bf) {
433d594e46aSJerome Glisse 			dev_warn(rdev->dev, "limiting GTT\n");
434d594e46aSJerome Glisse 			mc->gtt_size = size_bf;
435d594e46aSJerome Glisse 		}
4368d369bb1SAlex Deucher 		mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
437d594e46aSJerome Glisse 	} else {
438d594e46aSJerome Glisse 		if (mc->gtt_size > size_af) {
439d594e46aSJerome Glisse 			dev_warn(rdev->dev, "limiting GTT\n");
440d594e46aSJerome Glisse 			mc->gtt_size = size_af;
441d594e46aSJerome Glisse 		}
4428d369bb1SAlex Deucher 		mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
443d594e46aSJerome Glisse 	}
444d594e46aSJerome Glisse 	mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
445dd7cc55aSAlex Deucher 	dev_info(rdev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
446d594e46aSJerome Glisse 			mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
447d594e46aSJerome Glisse }
448771fe6b9SJerome Glisse 
449771fe6b9SJerome Glisse /*
450771fe6b9SJerome Glisse  * GPU helpers function.
451771fe6b9SJerome Glisse  */
4520c195119SAlex Deucher /**
4530c195119SAlex Deucher  * radeon_card_posted - check if the hw has already been initialized
4540c195119SAlex Deucher  *
4550c195119SAlex Deucher  * @rdev: radeon_device pointer
4560c195119SAlex Deucher  *
4570c195119SAlex Deucher  * Check if the asic has been initialized (all asics).
4580c195119SAlex Deucher  * Used at driver startup.
4590c195119SAlex Deucher  * Returns true if initialized or false if not.
4600c195119SAlex Deucher  */
4619f022ddfSJerome Glisse bool radeon_card_posted(struct radeon_device *rdev)
462771fe6b9SJerome Glisse {
463771fe6b9SJerome Glisse 	uint32_t reg;
464771fe6b9SJerome Glisse 
46550a583f6SAlex Deucher 	/* required for EFI mode on macbook2,1 which uses an r5xx asic */
46683e68189SMatt Fleming 	if (efi_enabled(EFI_BOOT) &&
46750a583f6SAlex Deucher 	    (rdev->pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE) &&
46850a583f6SAlex Deucher 	    (rdev->family < CHIP_R600))
469bcc65fd8SMatthew Garrett 		return false;
470bcc65fd8SMatthew Garrett 
4712cf3a4fcSAlex Deucher 	if (ASIC_IS_NODCE(rdev))
4722cf3a4fcSAlex Deucher 		goto check_memsize;
4732cf3a4fcSAlex Deucher 
474771fe6b9SJerome Glisse 	/* first check CRTCs */
47509fb8bd1SAlex Deucher 	if (ASIC_IS_DCE4(rdev)) {
47618007401SAlex Deucher 		reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
47718007401SAlex Deucher 			RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET);
47809fb8bd1SAlex Deucher 			if (rdev->num_crtc >= 4) {
47909fb8bd1SAlex Deucher 				reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) |
48009fb8bd1SAlex Deucher 					RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET);
48109fb8bd1SAlex Deucher 			}
48209fb8bd1SAlex Deucher 			if (rdev->num_crtc >= 6) {
48309fb8bd1SAlex Deucher 				reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) |
484bcc1c2a1SAlex Deucher 					RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET);
48509fb8bd1SAlex Deucher 			}
486bcc1c2a1SAlex Deucher 		if (reg & EVERGREEN_CRTC_MASTER_EN)
487bcc1c2a1SAlex Deucher 			return true;
488bcc1c2a1SAlex Deucher 	} else if (ASIC_IS_AVIVO(rdev)) {
489771fe6b9SJerome Glisse 		reg = RREG32(AVIVO_D1CRTC_CONTROL) |
490771fe6b9SJerome Glisse 		      RREG32(AVIVO_D2CRTC_CONTROL);
491771fe6b9SJerome Glisse 		if (reg & AVIVO_CRTC_EN) {
492771fe6b9SJerome Glisse 			return true;
493771fe6b9SJerome Glisse 		}
494771fe6b9SJerome Glisse 	} else {
495771fe6b9SJerome Glisse 		reg = RREG32(RADEON_CRTC_GEN_CNTL) |
496771fe6b9SJerome Glisse 		      RREG32(RADEON_CRTC2_GEN_CNTL);
497771fe6b9SJerome Glisse 		if (reg & RADEON_CRTC_EN) {
498771fe6b9SJerome Glisse 			return true;
499771fe6b9SJerome Glisse 		}
500771fe6b9SJerome Glisse 	}
501771fe6b9SJerome Glisse 
5022cf3a4fcSAlex Deucher check_memsize:
503771fe6b9SJerome Glisse 	/* then check MEM_SIZE, in case the crtcs are off */
504771fe6b9SJerome Glisse 	if (rdev->family >= CHIP_R600)
505771fe6b9SJerome Glisse 		reg = RREG32(R600_CONFIG_MEMSIZE);
506771fe6b9SJerome Glisse 	else
507771fe6b9SJerome Glisse 		reg = RREG32(RADEON_CONFIG_MEMSIZE);
508771fe6b9SJerome Glisse 
509771fe6b9SJerome Glisse 	if (reg)
510771fe6b9SJerome Glisse 		return true;
511771fe6b9SJerome Glisse 
512771fe6b9SJerome Glisse 	return false;
513771fe6b9SJerome Glisse 
514771fe6b9SJerome Glisse }
515771fe6b9SJerome Glisse 
5160c195119SAlex Deucher /**
5170c195119SAlex Deucher  * radeon_update_bandwidth_info - update display bandwidth params
5180c195119SAlex Deucher  *
5190c195119SAlex Deucher  * @rdev: radeon_device pointer
5200c195119SAlex Deucher  *
5210c195119SAlex Deucher  * Used when sclk/mclk are switched or display modes are set.
5220c195119SAlex Deucher  * params are used to calculate display watermarks (all asics)
5230c195119SAlex Deucher  */
524f47299c5SAlex Deucher void radeon_update_bandwidth_info(struct radeon_device *rdev)
525f47299c5SAlex Deucher {
526f47299c5SAlex Deucher 	fixed20_12 a;
5278807286eSAlex Deucher 	u32 sclk = rdev->pm.current_sclk;
5288807286eSAlex Deucher 	u32 mclk = rdev->pm.current_mclk;
529f47299c5SAlex Deucher 
5308807286eSAlex Deucher 	/* sclk/mclk in Mhz */
53168adac5eSBen Skeggs 	a.full = dfixed_const(100);
53268adac5eSBen Skeggs 	rdev->pm.sclk.full = dfixed_const(sclk);
53368adac5eSBen Skeggs 	rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a);
53468adac5eSBen Skeggs 	rdev->pm.mclk.full = dfixed_const(mclk);
53568adac5eSBen Skeggs 	rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a);
536f47299c5SAlex Deucher 
5378807286eSAlex Deucher 	if (rdev->flags & RADEON_IS_IGP) {
53868adac5eSBen Skeggs 		a.full = dfixed_const(16);
539f47299c5SAlex Deucher 		/* core_bandwidth = sclk(Mhz) * 16 */
54068adac5eSBen Skeggs 		rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a);
541f47299c5SAlex Deucher 	}
542f47299c5SAlex Deucher }
543f47299c5SAlex Deucher 
5440c195119SAlex Deucher /**
5450c195119SAlex Deucher  * radeon_boot_test_post_card - check and possibly initialize the hw
5460c195119SAlex Deucher  *
5470c195119SAlex Deucher  * @rdev: radeon_device pointer
5480c195119SAlex Deucher  *
5490c195119SAlex Deucher  * Check if the asic is initialized and if not, attempt to initialize
5500c195119SAlex Deucher  * it (all asics).
5510c195119SAlex Deucher  * Returns true if initialized or false if not.
5520c195119SAlex Deucher  */
55372542d77SDave Airlie bool radeon_boot_test_post_card(struct radeon_device *rdev)
55472542d77SDave Airlie {
55572542d77SDave Airlie 	if (radeon_card_posted(rdev))
55672542d77SDave Airlie 		return true;
55772542d77SDave Airlie 
55872542d77SDave Airlie 	if (rdev->bios) {
55972542d77SDave Airlie 		DRM_INFO("GPU not posted. posting now...\n");
56072542d77SDave Airlie 		if (rdev->is_atom_bios)
56172542d77SDave Airlie 			atom_asic_init(rdev->mode_info.atom_context);
56272542d77SDave Airlie 		else
56372542d77SDave Airlie 			radeon_combios_asic_init(rdev->ddev);
56472542d77SDave Airlie 		return true;
56572542d77SDave Airlie 	} else {
56672542d77SDave Airlie 		dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
56772542d77SDave Airlie 		return false;
56872542d77SDave Airlie 	}
56972542d77SDave Airlie }
57072542d77SDave Airlie 
5710c195119SAlex Deucher /**
5720c195119SAlex Deucher  * radeon_dummy_page_init - init dummy page used by the driver
5730c195119SAlex Deucher  *
5740c195119SAlex Deucher  * @rdev: radeon_device pointer
5750c195119SAlex Deucher  *
5760c195119SAlex Deucher  * Allocate the dummy page used by the driver (all asics).
5770c195119SAlex Deucher  * This dummy page is used by the driver as a filler for gart entries
5780c195119SAlex Deucher  * when pages are taken out of the GART
5790c195119SAlex Deucher  * Returns 0 on sucess, -ENOMEM on failure.
5800c195119SAlex Deucher  */
5813ce0a23dSJerome Glisse int radeon_dummy_page_init(struct radeon_device *rdev)
5823ce0a23dSJerome Glisse {
58382568565SDave Airlie 	if (rdev->dummy_page.page)
58482568565SDave Airlie 		return 0;
5853ce0a23dSJerome Glisse 	rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
5863ce0a23dSJerome Glisse 	if (rdev->dummy_page.page == NULL)
5873ce0a23dSJerome Glisse 		return -ENOMEM;
5883ce0a23dSJerome Glisse 	rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page,
5893ce0a23dSJerome Glisse 					0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
590a30f6fb7SBenjamin Herrenschmidt 	if (pci_dma_mapping_error(rdev->pdev, rdev->dummy_page.addr)) {
591a30f6fb7SBenjamin Herrenschmidt 		dev_err(&rdev->pdev->dev, "Failed to DMA MAP the dummy page\n");
5923ce0a23dSJerome Glisse 		__free_page(rdev->dummy_page.page);
5933ce0a23dSJerome Glisse 		rdev->dummy_page.page = NULL;
5943ce0a23dSJerome Glisse 		return -ENOMEM;
5953ce0a23dSJerome Glisse 	}
5963ce0a23dSJerome Glisse 	return 0;
5973ce0a23dSJerome Glisse }
5983ce0a23dSJerome Glisse 
5990c195119SAlex Deucher /**
6000c195119SAlex Deucher  * radeon_dummy_page_fini - free dummy page used by the driver
6010c195119SAlex Deucher  *
6020c195119SAlex Deucher  * @rdev: radeon_device pointer
6030c195119SAlex Deucher  *
6040c195119SAlex Deucher  * Frees the dummy page used by the driver (all asics).
6050c195119SAlex Deucher  */
6063ce0a23dSJerome Glisse void radeon_dummy_page_fini(struct radeon_device *rdev)
6073ce0a23dSJerome Glisse {
6083ce0a23dSJerome Glisse 	if (rdev->dummy_page.page == NULL)
6093ce0a23dSJerome Glisse 		return;
6103ce0a23dSJerome Glisse 	pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
6113ce0a23dSJerome Glisse 			PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
6123ce0a23dSJerome Glisse 	__free_page(rdev->dummy_page.page);
6133ce0a23dSJerome Glisse 	rdev->dummy_page.page = NULL;
6143ce0a23dSJerome Glisse }
6153ce0a23dSJerome Glisse 
616771fe6b9SJerome Glisse 
617771fe6b9SJerome Glisse /* ATOM accessor methods */
6180c195119SAlex Deucher /*
6190c195119SAlex Deucher  * ATOM is an interpreted byte code stored in tables in the vbios.  The
6200c195119SAlex Deucher  * driver registers callbacks to access registers and the interpreter
6210c195119SAlex Deucher  * in the driver parses the tables and executes then to program specific
6220c195119SAlex Deucher  * actions (set display modes, asic init, etc.).  See radeon_atombios.c,
6230c195119SAlex Deucher  * atombios.h, and atom.c
6240c195119SAlex Deucher  */
6250c195119SAlex Deucher 
6260c195119SAlex Deucher /**
6270c195119SAlex Deucher  * cail_pll_read - read PLL register
6280c195119SAlex Deucher  *
6290c195119SAlex Deucher  * @info: atom card_info pointer
6300c195119SAlex Deucher  * @reg: PLL register offset
6310c195119SAlex Deucher  *
6320c195119SAlex Deucher  * Provides a PLL register accessor for the atom interpreter (r4xx+).
6330c195119SAlex Deucher  * Returns the value of the PLL register.
6340c195119SAlex Deucher  */
635771fe6b9SJerome Glisse static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
636771fe6b9SJerome Glisse {
637771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
638771fe6b9SJerome Glisse 	uint32_t r;
639771fe6b9SJerome Glisse 
640771fe6b9SJerome Glisse 	r = rdev->pll_rreg(rdev, reg);
641771fe6b9SJerome Glisse 	return r;
642771fe6b9SJerome Glisse }
643771fe6b9SJerome Glisse 
6440c195119SAlex Deucher /**
6450c195119SAlex Deucher  * cail_pll_write - write PLL register
6460c195119SAlex Deucher  *
6470c195119SAlex Deucher  * @info: atom card_info pointer
6480c195119SAlex Deucher  * @reg: PLL register offset
6490c195119SAlex Deucher  * @val: value to write to the pll register
6500c195119SAlex Deucher  *
6510c195119SAlex Deucher  * Provides a PLL register accessor for the atom interpreter (r4xx+).
6520c195119SAlex Deucher  */
653771fe6b9SJerome Glisse static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
654771fe6b9SJerome Glisse {
655771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
656771fe6b9SJerome Glisse 
657771fe6b9SJerome Glisse 	rdev->pll_wreg(rdev, reg, val);
658771fe6b9SJerome Glisse }
659771fe6b9SJerome Glisse 
6600c195119SAlex Deucher /**
6610c195119SAlex Deucher  * cail_mc_read - read MC (Memory Controller) register
6620c195119SAlex Deucher  *
6630c195119SAlex Deucher  * @info: atom card_info pointer
6640c195119SAlex Deucher  * @reg: MC register offset
6650c195119SAlex Deucher  *
6660c195119SAlex Deucher  * Provides an MC register accessor for the atom interpreter (r4xx+).
6670c195119SAlex Deucher  * Returns the value of the MC register.
6680c195119SAlex Deucher  */
669771fe6b9SJerome Glisse static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
670771fe6b9SJerome Glisse {
671771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
672771fe6b9SJerome Glisse 	uint32_t r;
673771fe6b9SJerome Glisse 
674771fe6b9SJerome Glisse 	r = rdev->mc_rreg(rdev, reg);
675771fe6b9SJerome Glisse 	return r;
676771fe6b9SJerome Glisse }
677771fe6b9SJerome Glisse 
6780c195119SAlex Deucher /**
6790c195119SAlex Deucher  * cail_mc_write - write MC (Memory Controller) register
6800c195119SAlex Deucher  *
6810c195119SAlex Deucher  * @info: atom card_info pointer
6820c195119SAlex Deucher  * @reg: MC register offset
6830c195119SAlex Deucher  * @val: value to write to the pll register
6840c195119SAlex Deucher  *
6850c195119SAlex Deucher  * Provides a MC register accessor for the atom interpreter (r4xx+).
6860c195119SAlex Deucher  */
687771fe6b9SJerome Glisse static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
688771fe6b9SJerome Glisse {
689771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
690771fe6b9SJerome Glisse 
691771fe6b9SJerome Glisse 	rdev->mc_wreg(rdev, reg, val);
692771fe6b9SJerome Glisse }
693771fe6b9SJerome Glisse 
6940c195119SAlex Deucher /**
6950c195119SAlex Deucher  * cail_reg_write - write MMIO register
6960c195119SAlex Deucher  *
6970c195119SAlex Deucher  * @info: atom card_info pointer
6980c195119SAlex Deucher  * @reg: MMIO register offset
6990c195119SAlex Deucher  * @val: value to write to the pll register
7000c195119SAlex Deucher  *
7010c195119SAlex Deucher  * Provides a MMIO register accessor for the atom interpreter (r4xx+).
7020c195119SAlex Deucher  */
703771fe6b9SJerome Glisse static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
704771fe6b9SJerome Glisse {
705771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
706771fe6b9SJerome Glisse 
707771fe6b9SJerome Glisse 	WREG32(reg*4, val);
708771fe6b9SJerome Glisse }
709771fe6b9SJerome Glisse 
7100c195119SAlex Deucher /**
7110c195119SAlex Deucher  * cail_reg_read - read MMIO register
7120c195119SAlex Deucher  *
7130c195119SAlex Deucher  * @info: atom card_info pointer
7140c195119SAlex Deucher  * @reg: MMIO register offset
7150c195119SAlex Deucher  *
7160c195119SAlex Deucher  * Provides an MMIO register accessor for the atom interpreter (r4xx+).
7170c195119SAlex Deucher  * Returns the value of the MMIO register.
7180c195119SAlex Deucher  */
719771fe6b9SJerome Glisse static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
720771fe6b9SJerome Glisse {
721771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
722771fe6b9SJerome Glisse 	uint32_t r;
723771fe6b9SJerome Glisse 
724771fe6b9SJerome Glisse 	r = RREG32(reg*4);
725771fe6b9SJerome Glisse 	return r;
726771fe6b9SJerome Glisse }
727771fe6b9SJerome Glisse 
7280c195119SAlex Deucher /**
7290c195119SAlex Deucher  * cail_ioreg_write - write IO register
7300c195119SAlex Deucher  *
7310c195119SAlex Deucher  * @info: atom card_info pointer
7320c195119SAlex Deucher  * @reg: IO register offset
7330c195119SAlex Deucher  * @val: value to write to the pll register
7340c195119SAlex Deucher  *
7350c195119SAlex Deucher  * Provides a IO register accessor for the atom interpreter (r4xx+).
7360c195119SAlex Deucher  */
737351a52a2SAlex Deucher static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
738351a52a2SAlex Deucher {
739351a52a2SAlex Deucher 	struct radeon_device *rdev = info->dev->dev_private;
740351a52a2SAlex Deucher 
741351a52a2SAlex Deucher 	WREG32_IO(reg*4, val);
742351a52a2SAlex Deucher }
743351a52a2SAlex Deucher 
7440c195119SAlex Deucher /**
7450c195119SAlex Deucher  * cail_ioreg_read - read IO register
7460c195119SAlex Deucher  *
7470c195119SAlex Deucher  * @info: atom card_info pointer
7480c195119SAlex Deucher  * @reg: IO register offset
7490c195119SAlex Deucher  *
7500c195119SAlex Deucher  * Provides an IO register accessor for the atom interpreter (r4xx+).
7510c195119SAlex Deucher  * Returns the value of the IO register.
7520c195119SAlex Deucher  */
753351a52a2SAlex Deucher static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
754351a52a2SAlex Deucher {
755351a52a2SAlex Deucher 	struct radeon_device *rdev = info->dev->dev_private;
756351a52a2SAlex Deucher 	uint32_t r;
757351a52a2SAlex Deucher 
758351a52a2SAlex Deucher 	r = RREG32_IO(reg*4);
759351a52a2SAlex Deucher 	return r;
760351a52a2SAlex Deucher }
761351a52a2SAlex Deucher 
7620c195119SAlex Deucher /**
7630c195119SAlex Deucher  * radeon_atombios_init - init the driver info and callbacks for atombios
7640c195119SAlex Deucher  *
7650c195119SAlex Deucher  * @rdev: radeon_device pointer
7660c195119SAlex Deucher  *
7670c195119SAlex Deucher  * Initializes the driver info and register access callbacks for the
7680c195119SAlex Deucher  * ATOM interpreter (r4xx+).
7690c195119SAlex Deucher  * Returns 0 on sucess, -ENOMEM on failure.
7700c195119SAlex Deucher  * Called at driver startup.
7710c195119SAlex Deucher  */
772771fe6b9SJerome Glisse int radeon_atombios_init(struct radeon_device *rdev)
773771fe6b9SJerome Glisse {
77461c4b24bSMathias Fröhlich 	struct card_info *atom_card_info =
77561c4b24bSMathias Fröhlich 	    kzalloc(sizeof(struct card_info), GFP_KERNEL);
77661c4b24bSMathias Fröhlich 
77761c4b24bSMathias Fröhlich 	if (!atom_card_info)
77861c4b24bSMathias Fröhlich 		return -ENOMEM;
77961c4b24bSMathias Fröhlich 
78061c4b24bSMathias Fröhlich 	rdev->mode_info.atom_card_info = atom_card_info;
78161c4b24bSMathias Fröhlich 	atom_card_info->dev = rdev->ddev;
78261c4b24bSMathias Fröhlich 	atom_card_info->reg_read = cail_reg_read;
78361c4b24bSMathias Fröhlich 	atom_card_info->reg_write = cail_reg_write;
784351a52a2SAlex Deucher 	/* needed for iio ops */
785351a52a2SAlex Deucher 	if (rdev->rio_mem) {
786351a52a2SAlex Deucher 		atom_card_info->ioreg_read = cail_ioreg_read;
787351a52a2SAlex Deucher 		atom_card_info->ioreg_write = cail_ioreg_write;
788351a52a2SAlex Deucher 	} else {
789351a52a2SAlex Deucher 		DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
790351a52a2SAlex Deucher 		atom_card_info->ioreg_read = cail_reg_read;
791351a52a2SAlex Deucher 		atom_card_info->ioreg_write = cail_reg_write;
792351a52a2SAlex Deucher 	}
79361c4b24bSMathias Fröhlich 	atom_card_info->mc_read = cail_mc_read;
79461c4b24bSMathias Fröhlich 	atom_card_info->mc_write = cail_mc_write;
79561c4b24bSMathias Fröhlich 	atom_card_info->pll_read = cail_pll_read;
79661c4b24bSMathias Fröhlich 	atom_card_info->pll_write = cail_pll_write;
79761c4b24bSMathias Fröhlich 
79861c4b24bSMathias Fröhlich 	rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
7990e34d094STim Gardner 	if (!rdev->mode_info.atom_context) {
8000e34d094STim Gardner 		radeon_atombios_fini(rdev);
8010e34d094STim Gardner 		return -ENOMEM;
8020e34d094STim Gardner 	}
8030e34d094STim Gardner 
804c31ad97fSRafał Miłecki 	mutex_init(&rdev->mode_info.atom_context->mutex);
805771fe6b9SJerome Glisse 	radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
806d904ef9bSDave Airlie 	atom_allocate_fb_scratch(rdev->mode_info.atom_context);
807771fe6b9SJerome Glisse 	return 0;
808771fe6b9SJerome Glisse }
809771fe6b9SJerome Glisse 
8100c195119SAlex Deucher /**
8110c195119SAlex Deucher  * radeon_atombios_fini - free the driver info and callbacks for atombios
8120c195119SAlex Deucher  *
8130c195119SAlex Deucher  * @rdev: radeon_device pointer
8140c195119SAlex Deucher  *
8150c195119SAlex Deucher  * Frees the driver info and register access callbacks for the ATOM
8160c195119SAlex Deucher  * interpreter (r4xx+).
8170c195119SAlex Deucher  * Called at driver shutdown.
8180c195119SAlex Deucher  */
819771fe6b9SJerome Glisse void radeon_atombios_fini(struct radeon_device *rdev)
820771fe6b9SJerome Glisse {
8214a04a844SJerome Glisse 	if (rdev->mode_info.atom_context) {
822d904ef9bSDave Airlie 		kfree(rdev->mode_info.atom_context->scratch);
8234a04a844SJerome Glisse 	}
8240e34d094STim Gardner 	kfree(rdev->mode_info.atom_context);
8250e34d094STim Gardner 	rdev->mode_info.atom_context = NULL;
82661c4b24bSMathias Fröhlich 	kfree(rdev->mode_info.atom_card_info);
8270e34d094STim Gardner 	rdev->mode_info.atom_card_info = NULL;
828771fe6b9SJerome Glisse }
829771fe6b9SJerome Glisse 
8300c195119SAlex Deucher /* COMBIOS */
8310c195119SAlex Deucher /*
8320c195119SAlex Deucher  * COMBIOS is the bios format prior to ATOM. It provides
8330c195119SAlex Deucher  * command tables similar to ATOM, but doesn't have a unified
8340c195119SAlex Deucher  * parser.  See radeon_combios.c
8350c195119SAlex Deucher  */
8360c195119SAlex Deucher 
8370c195119SAlex Deucher /**
8380c195119SAlex Deucher  * radeon_combios_init - init the driver info for combios
8390c195119SAlex Deucher  *
8400c195119SAlex Deucher  * @rdev: radeon_device pointer
8410c195119SAlex Deucher  *
8420c195119SAlex Deucher  * Initializes the driver info for combios (r1xx-r3xx).
8430c195119SAlex Deucher  * Returns 0 on sucess.
8440c195119SAlex Deucher  * Called at driver startup.
8450c195119SAlex Deucher  */
846771fe6b9SJerome Glisse int radeon_combios_init(struct radeon_device *rdev)
847771fe6b9SJerome Glisse {
848771fe6b9SJerome Glisse 	radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
849771fe6b9SJerome Glisse 	return 0;
850771fe6b9SJerome Glisse }
851771fe6b9SJerome Glisse 
8520c195119SAlex Deucher /**
8530c195119SAlex Deucher  * radeon_combios_fini - free the driver info for combios
8540c195119SAlex Deucher  *
8550c195119SAlex Deucher  * @rdev: radeon_device pointer
8560c195119SAlex Deucher  *
8570c195119SAlex Deucher  * Frees the driver info for combios (r1xx-r3xx).
8580c195119SAlex Deucher  * Called at driver shutdown.
8590c195119SAlex Deucher  */
860771fe6b9SJerome Glisse void radeon_combios_fini(struct radeon_device *rdev)
861771fe6b9SJerome Glisse {
862771fe6b9SJerome Glisse }
863771fe6b9SJerome Glisse 
8640c195119SAlex Deucher /* if we get transitioned to only one device, take VGA back */
8650c195119SAlex Deucher /**
8660c195119SAlex Deucher  * radeon_vga_set_decode - enable/disable vga decode
8670c195119SAlex Deucher  *
8680c195119SAlex Deucher  * @cookie: radeon_device pointer
8690c195119SAlex Deucher  * @state: enable/disable vga decode
8700c195119SAlex Deucher  *
8710c195119SAlex Deucher  * Enable/disable vga decode (all asics).
8720c195119SAlex Deucher  * Returns VGA resource flags.
8730c195119SAlex Deucher  */
87428d52043SDave Airlie static unsigned int radeon_vga_set_decode(void *cookie, bool state)
87528d52043SDave Airlie {
87628d52043SDave Airlie 	struct radeon_device *rdev = cookie;
87728d52043SDave Airlie 	radeon_vga_set_state(rdev, state);
87828d52043SDave Airlie 	if (state)
87928d52043SDave Airlie 		return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
88028d52043SDave Airlie 		       VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
88128d52043SDave Airlie 	else
88228d52043SDave Airlie 		return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
88328d52043SDave Airlie }
884c1176d6fSDave Airlie 
8850c195119SAlex Deucher /**
8861bcb04f7SChristian König  * radeon_check_pot_argument - check that argument is a power of two
8871bcb04f7SChristian König  *
8881bcb04f7SChristian König  * @arg: value to check
8891bcb04f7SChristian König  *
8901bcb04f7SChristian König  * Validates that a certain argument is a power of two (all asics).
8911bcb04f7SChristian König  * Returns true if argument is valid.
8921bcb04f7SChristian König  */
8931bcb04f7SChristian König static bool radeon_check_pot_argument(int arg)
8941bcb04f7SChristian König {
8951bcb04f7SChristian König 	return (arg & (arg - 1)) == 0;
8961bcb04f7SChristian König }
8971bcb04f7SChristian König 
8981bcb04f7SChristian König /**
8990c195119SAlex Deucher  * radeon_check_arguments - validate module params
9000c195119SAlex Deucher  *
9010c195119SAlex Deucher  * @rdev: radeon_device pointer
9020c195119SAlex Deucher  *
9030c195119SAlex Deucher  * Validates certain module parameters and updates
9040c195119SAlex Deucher  * the associated values used by the driver (all asics).
9050c195119SAlex Deucher  */
9061109ca09SLauri Kasanen static void radeon_check_arguments(struct radeon_device *rdev)
90736421338SJerome Glisse {
90836421338SJerome Glisse 	/* vramlimit must be a power of two */
9091bcb04f7SChristian König 	if (!radeon_check_pot_argument(radeon_vram_limit)) {
91036421338SJerome Glisse 		dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n",
91136421338SJerome Glisse 				radeon_vram_limit);
91236421338SJerome Glisse 		radeon_vram_limit = 0;
91336421338SJerome Glisse 	}
9141bcb04f7SChristian König 
91536421338SJerome Glisse 	/* gtt size must be power of two and greater or equal to 32M */
9161bcb04f7SChristian König 	if (radeon_gart_size < 32) {
91736421338SJerome Glisse 		dev_warn(rdev->dev, "gart size (%d) too small forcing to 512M\n",
91836421338SJerome Glisse 				radeon_gart_size);
91936421338SJerome Glisse 		radeon_gart_size = 512;
9201bcb04f7SChristian König 
9211bcb04f7SChristian König 	} else if (!radeon_check_pot_argument(radeon_gart_size)) {
92236421338SJerome Glisse 		dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
92336421338SJerome Glisse 				radeon_gart_size);
92436421338SJerome Glisse 		radeon_gart_size = 512;
92536421338SJerome Glisse 	}
9261bcb04f7SChristian König 	rdev->mc.gtt_size = (uint64_t)radeon_gart_size << 20;
9271bcb04f7SChristian König 
92836421338SJerome Glisse 	/* AGP mode can only be -1, 1, 2, 4, 8 */
92936421338SJerome Glisse 	switch (radeon_agpmode) {
93036421338SJerome Glisse 	case -1:
93136421338SJerome Glisse 	case 0:
93236421338SJerome Glisse 	case 1:
93336421338SJerome Glisse 	case 2:
93436421338SJerome Glisse 	case 4:
93536421338SJerome Glisse 	case 8:
93636421338SJerome Glisse 		break;
93736421338SJerome Glisse 	default:
93836421338SJerome Glisse 		dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
93936421338SJerome Glisse 				"-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
94036421338SJerome Glisse 		radeon_agpmode = 0;
94136421338SJerome Glisse 		break;
94236421338SJerome Glisse 	}
94336421338SJerome Glisse }
94436421338SJerome Glisse 
9450c195119SAlex Deucher /**
946d1f9809eSMaarten Lankhorst  * radeon_switcheroo_quirk_long_wakeup - return true if longer d3 delay is
947d1f9809eSMaarten Lankhorst  * needed for waking up.
948d1f9809eSMaarten Lankhorst  *
949d1f9809eSMaarten Lankhorst  * @pdev: pci dev pointer
950d1f9809eSMaarten Lankhorst  */
951d1f9809eSMaarten Lankhorst static bool radeon_switcheroo_quirk_long_wakeup(struct pci_dev *pdev)
952d1f9809eSMaarten Lankhorst {
953d1f9809eSMaarten Lankhorst 
954d1f9809eSMaarten Lankhorst 	/* 6600m in a macbook pro */
955d1f9809eSMaarten Lankhorst 	if (pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE &&
956d1f9809eSMaarten Lankhorst 	    pdev->subsystem_device == 0x00e2) {
957d1f9809eSMaarten Lankhorst 		printk(KERN_INFO "radeon: quirking longer d3 wakeup delay\n");
958d1f9809eSMaarten Lankhorst 		return true;
959d1f9809eSMaarten Lankhorst 	}
960d1f9809eSMaarten Lankhorst 
961d1f9809eSMaarten Lankhorst 	return false;
962d1f9809eSMaarten Lankhorst }
963d1f9809eSMaarten Lankhorst 
964d1f9809eSMaarten Lankhorst /**
9650c195119SAlex Deucher  * radeon_switcheroo_set_state - set switcheroo state
9660c195119SAlex Deucher  *
9670c195119SAlex Deucher  * @pdev: pci dev pointer
9680c195119SAlex Deucher  * @state: vga switcheroo state
9690c195119SAlex Deucher  *
9700c195119SAlex Deucher  * Callback for the switcheroo driver.  Suspends or resumes the
9710c195119SAlex Deucher  * the asics before or after it is powered up using ACPI methods.
9720c195119SAlex Deucher  */
9736a9ee8afSDave Airlie static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
9746a9ee8afSDave Airlie {
9756a9ee8afSDave Airlie 	struct drm_device *dev = pci_get_drvdata(pdev);
9766a9ee8afSDave Airlie 	pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
9776a9ee8afSDave Airlie 	if (state == VGA_SWITCHEROO_ON) {
978d1f9809eSMaarten Lankhorst 		unsigned d3_delay = dev->pdev->d3_delay;
979d1f9809eSMaarten Lankhorst 
9806a9ee8afSDave Airlie 		printk(KERN_INFO "radeon: switched on\n");
9816a9ee8afSDave Airlie 		/* don't suspend or resume card normally */
9825bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
983d1f9809eSMaarten Lankhorst 
984d1f9809eSMaarten Lankhorst 		if (d3_delay < 20 && radeon_switcheroo_quirk_long_wakeup(pdev))
985d1f9809eSMaarten Lankhorst 			dev->pdev->d3_delay = 20;
986d1f9809eSMaarten Lankhorst 
9876a9ee8afSDave Airlie 		radeon_resume_kms(dev);
988d1f9809eSMaarten Lankhorst 
989d1f9809eSMaarten Lankhorst 		dev->pdev->d3_delay = d3_delay;
990d1f9809eSMaarten Lankhorst 
9915bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_ON;
992fbf81762SDave Airlie 		drm_kms_helper_poll_enable(dev);
9936a9ee8afSDave Airlie 	} else {
9946a9ee8afSDave Airlie 		printk(KERN_INFO "radeon: switched off\n");
995fbf81762SDave Airlie 		drm_kms_helper_poll_disable(dev);
9965bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
9976a9ee8afSDave Airlie 		radeon_suspend_kms(dev, pmm);
9985bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_OFF;
9996a9ee8afSDave Airlie 	}
10006a9ee8afSDave Airlie }
10016a9ee8afSDave Airlie 
10020c195119SAlex Deucher /**
10030c195119SAlex Deucher  * radeon_switcheroo_can_switch - see if switcheroo state can change
10040c195119SAlex Deucher  *
10050c195119SAlex Deucher  * @pdev: pci dev pointer
10060c195119SAlex Deucher  *
10070c195119SAlex Deucher  * Callback for the switcheroo driver.  Check of the switcheroo
10080c195119SAlex Deucher  * state can be changed.
10090c195119SAlex Deucher  * Returns true if the state can be changed, false if not.
10100c195119SAlex Deucher  */
10116a9ee8afSDave Airlie static bool radeon_switcheroo_can_switch(struct pci_dev *pdev)
10126a9ee8afSDave Airlie {
10136a9ee8afSDave Airlie 	struct drm_device *dev = pci_get_drvdata(pdev);
10146a9ee8afSDave Airlie 	bool can_switch;
10156a9ee8afSDave Airlie 
10166a9ee8afSDave Airlie 	spin_lock(&dev->count_lock);
10176a9ee8afSDave Airlie 	can_switch = (dev->open_count == 0);
10186a9ee8afSDave Airlie 	spin_unlock(&dev->count_lock);
10196a9ee8afSDave Airlie 	return can_switch;
10206a9ee8afSDave Airlie }
10216a9ee8afSDave Airlie 
102226ec685fSTakashi Iwai static const struct vga_switcheroo_client_ops radeon_switcheroo_ops = {
102326ec685fSTakashi Iwai 	.set_gpu_state = radeon_switcheroo_set_state,
102426ec685fSTakashi Iwai 	.reprobe = NULL,
102526ec685fSTakashi Iwai 	.can_switch = radeon_switcheroo_can_switch,
102626ec685fSTakashi Iwai };
10276a9ee8afSDave Airlie 
10280c195119SAlex Deucher /**
10290c195119SAlex Deucher  * radeon_device_init - initialize the driver
10300c195119SAlex Deucher  *
10310c195119SAlex Deucher  * @rdev: radeon_device pointer
10320c195119SAlex Deucher  * @pdev: drm dev pointer
10330c195119SAlex Deucher  * @pdev: pci dev pointer
10340c195119SAlex Deucher  * @flags: driver flags
10350c195119SAlex Deucher  *
10360c195119SAlex Deucher  * Initializes the driver info and hw (all asics).
10370c195119SAlex Deucher  * Returns 0 for success or an error on failure.
10380c195119SAlex Deucher  * Called at driver startup.
10390c195119SAlex Deucher  */
1040771fe6b9SJerome Glisse int radeon_device_init(struct radeon_device *rdev,
1041771fe6b9SJerome Glisse 		       struct drm_device *ddev,
1042771fe6b9SJerome Glisse 		       struct pci_dev *pdev,
1043771fe6b9SJerome Glisse 		       uint32_t flags)
1044771fe6b9SJerome Glisse {
1045351a52a2SAlex Deucher 	int r, i;
1046ad49f501SDave Airlie 	int dma_bits;
1047771fe6b9SJerome Glisse 
1048771fe6b9SJerome Glisse 	rdev->shutdown = false;
10499f022ddfSJerome Glisse 	rdev->dev = &pdev->dev;
1050771fe6b9SJerome Glisse 	rdev->ddev = ddev;
1051771fe6b9SJerome Glisse 	rdev->pdev = pdev;
1052771fe6b9SJerome Glisse 	rdev->flags = flags;
1053771fe6b9SJerome Glisse 	rdev->family = flags & RADEON_FAMILY_MASK;
1054771fe6b9SJerome Glisse 	rdev->is_atom_bios = false;
1055771fe6b9SJerome Glisse 	rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
1056771fe6b9SJerome Glisse 	rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
1057733289c2SJerome Glisse 	rdev->accel_working = false;
10588b25ed34SAlex Deucher 	/* set up ring ids */
10598b25ed34SAlex Deucher 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
10608b25ed34SAlex Deucher 		rdev->ring[i].idx = i;
10618b25ed34SAlex Deucher 	}
10621b5331d9SJerome Glisse 
1063d522d9ccSThomas Reim 	DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X).\n",
1064d522d9ccSThomas Reim 		radeon_family_name[rdev->family], pdev->vendor, pdev->device,
1065d522d9ccSThomas Reim 		pdev->subsystem_vendor, pdev->subsystem_device);
10661b5331d9SJerome Glisse 
1067771fe6b9SJerome Glisse 	/* mutex initialization are all done here so we
1068771fe6b9SJerome Glisse 	 * can recall function without having locking issues */
1069d6999bc7SChristian König 	mutex_init(&rdev->ring_lock);
107040bacf16SAlex Deucher 	mutex_init(&rdev->dc_hw_i2c_mutex);
1071c20dc369SChristian Koenig 	atomic_set(&rdev->ih.lock, 0);
10724c788679SJerome Glisse 	mutex_init(&rdev->gem.mutex);
1073c913e23aSRafał Miłecki 	mutex_init(&rdev->pm.mutex);
10746759a0a7SMarek Olšák 	mutex_init(&rdev->gpu_clock_mutex);
1075db7fce39SChristian König 	init_rwsem(&rdev->pm.mclk_lock);
1076dee53e7fSJerome Glisse 	init_rwsem(&rdev->exclusive_lock);
107773a6d3fcSRafał Miłecki 	init_waitqueue_head(&rdev->irq.vblank_queue);
10781b9c3dd0SAlex Deucher 	r = radeon_gem_init(rdev);
10791b9c3dd0SAlex Deucher 	if (r)
10801b9c3dd0SAlex Deucher 		return r;
1081721604a1SJerome Glisse 	/* initialize vm here */
108236ff39c4SChristian König 	mutex_init(&rdev->vm_manager.lock);
108323d4f1f2SAlex Deucher 	/* Adjust VM size here.
108423d4f1f2SAlex Deucher 	 * Currently set to 4GB ((1 << 20) 4k pages).
108523d4f1f2SAlex Deucher 	 * Max GPUVM size for cayman and SI is 40 bits.
108623d4f1f2SAlex Deucher 	 */
1087721604a1SJerome Glisse 	rdev->vm_manager.max_pfn = 1 << 20;
1088721604a1SJerome Glisse 	INIT_LIST_HEAD(&rdev->vm_manager.lru_vm);
1089771fe6b9SJerome Glisse 
10904aac0473SJerome Glisse 	/* Set asic functions */
10914aac0473SJerome Glisse 	r = radeon_asic_init(rdev);
109236421338SJerome Glisse 	if (r)
10934aac0473SJerome Glisse 		return r;
109436421338SJerome Glisse 	radeon_check_arguments(rdev);
10954aac0473SJerome Glisse 
1096f95df9caSAlex Deucher 	/* all of the newer IGP chips have an internal gart
1097f95df9caSAlex Deucher 	 * However some rs4xx report as AGP, so remove that here.
1098f95df9caSAlex Deucher 	 */
1099f95df9caSAlex Deucher 	if ((rdev->family >= CHIP_RS400) &&
1100f95df9caSAlex Deucher 	    (rdev->flags & RADEON_IS_IGP)) {
1101f95df9caSAlex Deucher 		rdev->flags &= ~RADEON_IS_AGP;
1102f95df9caSAlex Deucher 	}
1103f95df9caSAlex Deucher 
110430256a3fSJerome Glisse 	if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
1105b574f251SJerome Glisse 		radeon_agp_disable(rdev);
1106771fe6b9SJerome Glisse 	}
1107771fe6b9SJerome Glisse 
11089ed8b1f9SAlex Deucher 	/* Set the internal MC address mask
11099ed8b1f9SAlex Deucher 	 * This is the max address of the GPU's
11109ed8b1f9SAlex Deucher 	 * internal address space.
11119ed8b1f9SAlex Deucher 	 */
11129ed8b1f9SAlex Deucher 	if (rdev->family >= CHIP_CAYMAN)
11139ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xffffffffffULL; /* 40 bit MC */
11149ed8b1f9SAlex Deucher 	else if (rdev->family >= CHIP_CEDAR)
11159ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xfffffffffULL; /* 36 bit MC */
11169ed8b1f9SAlex Deucher 	else
11179ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xffffffffULL; /* 32 bit MC */
11189ed8b1f9SAlex Deucher 
1119ad49f501SDave Airlie 	/* set DMA mask + need_dma32 flags.
1120ad49f501SDave Airlie 	 * PCIE - can handle 40-bits.
1121005a83f1SAlex Deucher 	 * IGP - can handle 40-bits
1122ad49f501SDave Airlie 	 * AGP - generally dma32 is safest
1123005a83f1SAlex Deucher 	 * PCI - dma32 for legacy pci gart, 40 bits on newer asics
1124ad49f501SDave Airlie 	 */
1125ad49f501SDave Airlie 	rdev->need_dma32 = false;
1126ad49f501SDave Airlie 	if (rdev->flags & RADEON_IS_AGP)
1127ad49f501SDave Airlie 		rdev->need_dma32 = true;
1128005a83f1SAlex Deucher 	if ((rdev->flags & RADEON_IS_PCI) &&
11294a2b6662SJerome Glisse 	    (rdev->family <= CHIP_RS740))
1130ad49f501SDave Airlie 		rdev->need_dma32 = true;
1131ad49f501SDave Airlie 
1132ad49f501SDave Airlie 	dma_bits = rdev->need_dma32 ? 32 : 40;
1133ad49f501SDave Airlie 	r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
1134771fe6b9SJerome Glisse 	if (r) {
113562fff811SDaniel Haid 		rdev->need_dma32 = true;
1136c52494f6SKonrad Rzeszutek Wilk 		dma_bits = 32;
1137771fe6b9SJerome Glisse 		printk(KERN_WARNING "radeon: No suitable DMA available.\n");
1138771fe6b9SJerome Glisse 	}
1139c52494f6SKonrad Rzeszutek Wilk 	r = pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
1140c52494f6SKonrad Rzeszutek Wilk 	if (r) {
1141c52494f6SKonrad Rzeszutek Wilk 		pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(32));
1142c52494f6SKonrad Rzeszutek Wilk 		printk(KERN_WARNING "radeon: No coherent DMA available.\n");
1143c52494f6SKonrad Rzeszutek Wilk 	}
1144771fe6b9SJerome Glisse 
1145771fe6b9SJerome Glisse 	/* Registers mapping */
1146771fe6b9SJerome Glisse 	/* TODO: block userspace mapping of io register */
11472c385151SDaniel Vetter 	spin_lock_init(&rdev->mmio_idx_lock);
114801d73a69SJordan Crouse 	rdev->rmmio_base = pci_resource_start(rdev->pdev, 2);
114901d73a69SJordan Crouse 	rdev->rmmio_size = pci_resource_len(rdev->pdev, 2);
1150771fe6b9SJerome Glisse 	rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size);
1151771fe6b9SJerome Glisse 	if (rdev->rmmio == NULL) {
1152771fe6b9SJerome Glisse 		return -ENOMEM;
1153771fe6b9SJerome Glisse 	}
1154771fe6b9SJerome Glisse 	DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base);
1155771fe6b9SJerome Glisse 	DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size);
1156771fe6b9SJerome Glisse 
1157351a52a2SAlex Deucher 	/* io port mapping */
1158351a52a2SAlex Deucher 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1159351a52a2SAlex Deucher 		if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) {
1160351a52a2SAlex Deucher 			rdev->rio_mem_size = pci_resource_len(rdev->pdev, i);
1161351a52a2SAlex Deucher 			rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size);
1162351a52a2SAlex Deucher 			break;
1163351a52a2SAlex Deucher 		}
1164351a52a2SAlex Deucher 	}
1165351a52a2SAlex Deucher 	if (rdev->rio_mem == NULL)
1166351a52a2SAlex Deucher 		DRM_ERROR("Unable to find PCI I/O BAR\n");
1167351a52a2SAlex Deucher 
116828d52043SDave Airlie 	/* if we have > 1 VGA cards, then disable the radeon VGA resources */
116993239ea1SDave Airlie 	/* this will fail for cards that aren't VGA class devices, just
117093239ea1SDave Airlie 	 * ignore it */
117193239ea1SDave Airlie 	vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
117226ec685fSTakashi Iwai 	vga_switcheroo_register_client(rdev->pdev, &radeon_switcheroo_ops);
117328d52043SDave Airlie 
11743ce0a23dSJerome Glisse 	r = radeon_init(rdev);
1175b574f251SJerome Glisse 	if (r)
1176b574f251SJerome Glisse 		return r;
1177b1e3a6d1SMichel Dänzer 
117804eb2206SChristian König 	r = radeon_ib_ring_tests(rdev);
117904eb2206SChristian König 	if (r)
118004eb2206SChristian König 		DRM_ERROR("ib ring test failed (%d).\n", r);
118104eb2206SChristian König 
1182409851f4SJerome Glisse 	r = radeon_gem_debugfs_init(rdev);
1183409851f4SJerome Glisse 	if (r) {
1184409851f4SJerome Glisse 		DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1185409851f4SJerome Glisse 	}
1186409851f4SJerome Glisse 
1187b574f251SJerome Glisse 	if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
1188b574f251SJerome Glisse 		/* Acceleration not working on AGP card try again
1189b574f251SJerome Glisse 		 * with fallback to PCI or PCIE GART
1190b574f251SJerome Glisse 		 */
1191a2d07b74SJerome Glisse 		radeon_asic_reset(rdev);
1192b574f251SJerome Glisse 		radeon_fini(rdev);
1193b574f251SJerome Glisse 		radeon_agp_disable(rdev);
1194b574f251SJerome Glisse 		r = radeon_init(rdev);
11954aac0473SJerome Glisse 		if (r)
11964aac0473SJerome Glisse 			return r;
11973ce0a23dSJerome Glisse 	}
119860a7e396SChristian König 	if ((radeon_testing & 1)) {
1199ecc0b326SMichel Dänzer 		radeon_test_moves(rdev);
1200ecc0b326SMichel Dänzer 	}
120160a7e396SChristian König 	if ((radeon_testing & 2)) {
120260a7e396SChristian König 		radeon_test_syncing(rdev);
120360a7e396SChristian König 	}
1204771fe6b9SJerome Glisse 	if (radeon_benchmarking) {
1205638dd7dbSIlija Hadzic 		radeon_benchmark(rdev, radeon_benchmarking);
1206771fe6b9SJerome Glisse 	}
12076cf8a3f5SJerome Glisse 	return 0;
1208771fe6b9SJerome Glisse }
1209771fe6b9SJerome Glisse 
12104d8bf9aeSChristian König static void radeon_debugfs_remove_files(struct radeon_device *rdev);
12114d8bf9aeSChristian König 
12120c195119SAlex Deucher /**
12130c195119SAlex Deucher  * radeon_device_fini - tear down the driver
12140c195119SAlex Deucher  *
12150c195119SAlex Deucher  * @rdev: radeon_device pointer
12160c195119SAlex Deucher  *
12170c195119SAlex Deucher  * Tear down the driver info (all asics).
12180c195119SAlex Deucher  * Called at driver shutdown.
12190c195119SAlex Deucher  */
1220771fe6b9SJerome Glisse void radeon_device_fini(struct radeon_device *rdev)
1221771fe6b9SJerome Glisse {
1222771fe6b9SJerome Glisse 	DRM_INFO("radeon: finishing device.\n");
1223771fe6b9SJerome Glisse 	rdev->shutdown = true;
122490aca4d2SJerome Glisse 	/* evict vram memory */
122590aca4d2SJerome Glisse 	radeon_bo_evict_vram(rdev);
12263ce0a23dSJerome Glisse 	radeon_fini(rdev);
12276a9ee8afSDave Airlie 	vga_switcheroo_unregister_client(rdev->pdev);
1228c1176d6fSDave Airlie 	vga_client_register(rdev->pdev, NULL, NULL, NULL);
1229e0a2ca73SAlex Deucher 	if (rdev->rio_mem)
1230351a52a2SAlex Deucher 		pci_iounmap(rdev->pdev, rdev->rio_mem);
1231351a52a2SAlex Deucher 	rdev->rio_mem = NULL;
1232771fe6b9SJerome Glisse 	iounmap(rdev->rmmio);
1233771fe6b9SJerome Glisse 	rdev->rmmio = NULL;
12344d8bf9aeSChristian König 	radeon_debugfs_remove_files(rdev);
1235771fe6b9SJerome Glisse }
1236771fe6b9SJerome Glisse 
1237771fe6b9SJerome Glisse 
1238771fe6b9SJerome Glisse /*
1239771fe6b9SJerome Glisse  * Suspend & resume.
1240771fe6b9SJerome Glisse  */
12410c195119SAlex Deucher /**
12420c195119SAlex Deucher  * radeon_suspend_kms - initiate device suspend
12430c195119SAlex Deucher  *
12440c195119SAlex Deucher  * @pdev: drm dev pointer
12450c195119SAlex Deucher  * @state: suspend state
12460c195119SAlex Deucher  *
12470c195119SAlex Deucher  * Puts the hw in the suspend state (all asics).
12480c195119SAlex Deucher  * Returns 0 for success or an error on failure.
12490c195119SAlex Deucher  * Called at driver suspend.
12500c195119SAlex Deucher  */
1251771fe6b9SJerome Glisse int radeon_suspend_kms(struct drm_device *dev, pm_message_t state)
1252771fe6b9SJerome Glisse {
1253875c1866SDarren Jenkins 	struct radeon_device *rdev;
1254771fe6b9SJerome Glisse 	struct drm_crtc *crtc;
1255d8dcaa1dSAlex Deucher 	struct drm_connector *connector;
12567465280cSAlex Deucher 	int i, r;
12575f8f635eSJerome Glisse 	bool force_completion = false;
1258771fe6b9SJerome Glisse 
1259875c1866SDarren Jenkins 	if (dev == NULL || dev->dev_private == NULL) {
1260771fe6b9SJerome Glisse 		return -ENODEV;
1261771fe6b9SJerome Glisse 	}
1262771fe6b9SJerome Glisse 	if (state.event == PM_EVENT_PRETHAW) {
1263771fe6b9SJerome Glisse 		return 0;
1264771fe6b9SJerome Glisse 	}
1265875c1866SDarren Jenkins 	rdev = dev->dev_private;
1266875c1866SDarren Jenkins 
12675bcf719bSDave Airlie 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
12686a9ee8afSDave Airlie 		return 0;
1269d8dcaa1dSAlex Deucher 
127086698c20SSeth Forshee 	drm_kms_helper_poll_disable(dev);
127186698c20SSeth Forshee 
1272d8dcaa1dSAlex Deucher 	/* turn off display hw */
1273d8dcaa1dSAlex Deucher 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1274d8dcaa1dSAlex Deucher 		drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1275d8dcaa1dSAlex Deucher 	}
1276d8dcaa1dSAlex Deucher 
1277771fe6b9SJerome Glisse 	/* unpin the front buffers */
1278771fe6b9SJerome Glisse 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1279771fe6b9SJerome Glisse 		struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->fb);
12804c788679SJerome Glisse 		struct radeon_bo *robj;
1281771fe6b9SJerome Glisse 
1282771fe6b9SJerome Glisse 		if (rfb == NULL || rfb->obj == NULL) {
1283771fe6b9SJerome Glisse 			continue;
1284771fe6b9SJerome Glisse 		}
12857e4d15d9SDaniel Vetter 		robj = gem_to_radeon_bo(rfb->obj);
128638651674SDave Airlie 		/* don't unpin kernel fb objects */
128738651674SDave Airlie 		if (!radeon_fbdev_robj_is_fb(rdev, robj)) {
12884c788679SJerome Glisse 			r = radeon_bo_reserve(robj, false);
128938651674SDave Airlie 			if (r == 0) {
12904c788679SJerome Glisse 				radeon_bo_unpin(robj);
12914c788679SJerome Glisse 				radeon_bo_unreserve(robj);
12924c788679SJerome Glisse 			}
1293771fe6b9SJerome Glisse 		}
1294771fe6b9SJerome Glisse 	}
1295771fe6b9SJerome Glisse 	/* evict vram memory */
12964c788679SJerome Glisse 	radeon_bo_evict_vram(rdev);
12978a47cc9eSChristian König 
12988a47cc9eSChristian König 	mutex_lock(&rdev->ring_lock);
1299771fe6b9SJerome Glisse 	/* wait for gpu to finish processing current batch */
13005f8f635eSJerome Glisse 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
13015f8f635eSJerome Glisse 		r = radeon_fence_wait_empty_locked(rdev, i);
13025f8f635eSJerome Glisse 		if (r) {
13035f8f635eSJerome Glisse 			/* delay GPU reset to resume */
13045f8f635eSJerome Glisse 			force_completion = true;
13055f8f635eSJerome Glisse 		}
13065f8f635eSJerome Glisse 	}
13075f8f635eSJerome Glisse 	if (force_completion) {
13085f8f635eSJerome Glisse 		radeon_fence_driver_force_completion(rdev);
13095f8f635eSJerome Glisse 	}
13108a47cc9eSChristian König 	mutex_unlock(&rdev->ring_lock);
1311771fe6b9SJerome Glisse 
1312f657c2a7SYang Zhao 	radeon_save_bios_scratch_regs(rdev);
1313f657c2a7SYang Zhao 
1314ce8f5370SAlex Deucher 	radeon_pm_suspend(rdev);
13153ce0a23dSJerome Glisse 	radeon_suspend(rdev);
1316d4877cf2SAlex Deucher 	radeon_hpd_fini(rdev);
1317771fe6b9SJerome Glisse 	/* evict remaining vram memory */
13184c788679SJerome Glisse 	radeon_bo_evict_vram(rdev);
1319771fe6b9SJerome Glisse 
132010b06122SJerome Glisse 	radeon_agp_suspend(rdev);
132110b06122SJerome Glisse 
1322771fe6b9SJerome Glisse 	pci_save_state(dev->pdev);
1323771fe6b9SJerome Glisse 	if (state.event == PM_EVENT_SUSPEND) {
1324771fe6b9SJerome Glisse 		/* Shut down the device */
1325771fe6b9SJerome Glisse 		pci_disable_device(dev->pdev);
1326771fe6b9SJerome Glisse 		pci_set_power_state(dev->pdev, PCI_D3hot);
1327771fe6b9SJerome Glisse 	}
1328ac751efaSTorben Hohn 	console_lock();
132938651674SDave Airlie 	radeon_fbdev_set_suspend(rdev, 1);
1330ac751efaSTorben Hohn 	console_unlock();
1331771fe6b9SJerome Glisse 	return 0;
1332771fe6b9SJerome Glisse }
1333771fe6b9SJerome Glisse 
13340c195119SAlex Deucher /**
13350c195119SAlex Deucher  * radeon_resume_kms - initiate device resume
13360c195119SAlex Deucher  *
13370c195119SAlex Deucher  * @pdev: drm dev pointer
13380c195119SAlex Deucher  *
13390c195119SAlex Deucher  * Bring the hw back to operating state (all asics).
13400c195119SAlex Deucher  * Returns 0 for success or an error on failure.
13410c195119SAlex Deucher  * Called at driver resume.
13420c195119SAlex Deucher  */
1343771fe6b9SJerome Glisse int radeon_resume_kms(struct drm_device *dev)
1344771fe6b9SJerome Glisse {
134509bdf591SCedric Godin 	struct drm_connector *connector;
1346771fe6b9SJerome Glisse 	struct radeon_device *rdev = dev->dev_private;
134704eb2206SChristian König 	int r;
1348771fe6b9SJerome Glisse 
13495bcf719bSDave Airlie 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
13506a9ee8afSDave Airlie 		return 0;
13516a9ee8afSDave Airlie 
1352ac751efaSTorben Hohn 	console_lock();
1353771fe6b9SJerome Glisse 	pci_set_power_state(dev->pdev, PCI_D0);
1354771fe6b9SJerome Glisse 	pci_restore_state(dev->pdev);
1355771fe6b9SJerome Glisse 	if (pci_enable_device(dev->pdev)) {
1356ac751efaSTorben Hohn 		console_unlock();
1357771fe6b9SJerome Glisse 		return -1;
1358771fe6b9SJerome Glisse 	}
13590ebf1717SDave Airlie 	/* resume AGP if in use */
13600ebf1717SDave Airlie 	radeon_agp_resume(rdev);
13613ce0a23dSJerome Glisse 	radeon_resume(rdev);
136204eb2206SChristian König 
136304eb2206SChristian König 	r = radeon_ib_ring_tests(rdev);
136404eb2206SChristian König 	if (r)
136504eb2206SChristian König 		DRM_ERROR("ib ring test failed (%d).\n", r);
136604eb2206SChristian König 
1367ce8f5370SAlex Deucher 	radeon_pm_resume(rdev);
1368f657c2a7SYang Zhao 	radeon_restore_bios_scratch_regs(rdev);
136909bdf591SCedric Godin 
137038651674SDave Airlie 	radeon_fbdev_set_suspend(rdev, 0);
1371ac751efaSTorben Hohn 	console_unlock();
1372771fe6b9SJerome Glisse 
13733fa47d9eSAlex Deucher 	/* init dig PHYs, disp eng pll */
13743fa47d9eSAlex Deucher 	if (rdev->is_atom_bios) {
1375ac89af1eSAlex Deucher 		radeon_atom_encoder_init(rdev);
1376f3f1f03eSAlex Deucher 		radeon_atom_disp_eng_pll_init(rdev);
1377bced76f2SAlex Deucher 		/* turn on the BL */
1378bced76f2SAlex Deucher 		if (rdev->mode_info.bl_encoder) {
1379bced76f2SAlex Deucher 			u8 bl_level = radeon_get_backlight_level(rdev,
1380bced76f2SAlex Deucher 								 rdev->mode_info.bl_encoder);
1381bced76f2SAlex Deucher 			radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
1382bced76f2SAlex Deucher 						   bl_level);
1383bced76f2SAlex Deucher 		}
13843fa47d9eSAlex Deucher 	}
1385d4877cf2SAlex Deucher 	/* reset hpd state */
1386d4877cf2SAlex Deucher 	radeon_hpd_init(rdev);
1387771fe6b9SJerome Glisse 	/* blat the mode back in */
1388771fe6b9SJerome Glisse 	drm_helper_resume_force_mode(dev);
1389a93f344dSAlex Deucher 	/* turn on display hw */
1390a93f344dSAlex Deucher 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1391a93f344dSAlex Deucher 		drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
1392a93f344dSAlex Deucher 	}
139386698c20SSeth Forshee 
139486698c20SSeth Forshee 	drm_kms_helper_poll_enable(dev);
1395771fe6b9SJerome Glisse 	return 0;
1396771fe6b9SJerome Glisse }
1397771fe6b9SJerome Glisse 
13980c195119SAlex Deucher /**
13990c195119SAlex Deucher  * radeon_gpu_reset - reset the asic
14000c195119SAlex Deucher  *
14010c195119SAlex Deucher  * @rdev: radeon device pointer
14020c195119SAlex Deucher  *
14030c195119SAlex Deucher  * Attempt the reset the GPU if it has hung (all asics).
14040c195119SAlex Deucher  * Returns 0 for success or an error on failure.
14050c195119SAlex Deucher  */
140690aca4d2SJerome Glisse int radeon_gpu_reset(struct radeon_device *rdev)
140790aca4d2SJerome Glisse {
140855d7c221SChristian König 	unsigned ring_sizes[RADEON_NUM_RINGS];
140955d7c221SChristian König 	uint32_t *ring_data[RADEON_NUM_RINGS];
141055d7c221SChristian König 
141155d7c221SChristian König 	bool saved = false;
141255d7c221SChristian König 
141355d7c221SChristian König 	int i, r;
14148fd1b84cSDave Airlie 	int resched;
141590aca4d2SJerome Glisse 
1416dee53e7fSJerome Glisse 	down_write(&rdev->exclusive_lock);
141790aca4d2SJerome Glisse 	radeon_save_bios_scratch_regs(rdev);
14188fd1b84cSDave Airlie 	/* block TTM */
14198fd1b84cSDave Airlie 	resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
142090aca4d2SJerome Glisse 	radeon_suspend(rdev);
142190aca4d2SJerome Glisse 
142255d7c221SChristian König 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
142355d7c221SChristian König 		ring_sizes[i] = radeon_ring_backup(rdev, &rdev->ring[i],
142455d7c221SChristian König 						   &ring_data[i]);
142555d7c221SChristian König 		if (ring_sizes[i]) {
142655d7c221SChristian König 			saved = true;
142755d7c221SChristian König 			dev_info(rdev->dev, "Saved %d dwords of commands "
142855d7c221SChristian König 				 "on ring %d.\n", ring_sizes[i], i);
142955d7c221SChristian König 		}
143055d7c221SChristian König 	}
143155d7c221SChristian König 
143255d7c221SChristian König retry:
143390aca4d2SJerome Glisse 	r = radeon_asic_reset(rdev);
143490aca4d2SJerome Glisse 	if (!r) {
143555d7c221SChristian König 		dev_info(rdev->dev, "GPU reset succeeded, trying to resume\n");
143690aca4d2SJerome Glisse 		radeon_resume(rdev);
143755d7c221SChristian König 	}
143804eb2206SChristian König 
143990aca4d2SJerome Glisse 	radeon_restore_bios_scratch_regs(rdev);
144055d7c221SChristian König 
144155d7c221SChristian König 	if (!r) {
144255d7c221SChristian König 		for (i = 0; i < RADEON_NUM_RINGS; ++i) {
144355d7c221SChristian König 			radeon_ring_restore(rdev, &rdev->ring[i],
144455d7c221SChristian König 					    ring_sizes[i], ring_data[i]);
1445f54b350dSChristian König 			ring_sizes[i] = 0;
1446f54b350dSChristian König 			ring_data[i] = NULL;
144790aca4d2SJerome Glisse 		}
14487a1619b9SMichel Dänzer 
144955d7c221SChristian König 		r = radeon_ib_ring_tests(rdev);
145055d7c221SChristian König 		if (r) {
145155d7c221SChristian König 			dev_err(rdev->dev, "ib ring test failed (%d).\n", r);
145255d7c221SChristian König 			if (saved) {
1453f54b350dSChristian König 				saved = false;
145455d7c221SChristian König 				radeon_suspend(rdev);
145555d7c221SChristian König 				goto retry;
145655d7c221SChristian König 			}
145755d7c221SChristian König 		}
145855d7c221SChristian König 	} else {
145976903b96SJerome Glisse 		radeon_fence_driver_force_completion(rdev);
146055d7c221SChristian König 		for (i = 0; i < RADEON_NUM_RINGS; ++i) {
146155d7c221SChristian König 			kfree(ring_data[i]);
146255d7c221SChristian König 		}
146355d7c221SChristian König 	}
146455d7c221SChristian König 
1465d3493574SJerome Glisse 	drm_helper_resume_force_mode(rdev->ddev);
1466d3493574SJerome Glisse 
146755d7c221SChristian König 	ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
14687a1619b9SMichel Dänzer 	if (r) {
146990aca4d2SJerome Glisse 		/* bad news, how to tell it to userspace ? */
147090aca4d2SJerome Glisse 		dev_info(rdev->dev, "GPU reset failed\n");
14717a1619b9SMichel Dänzer 	}
14727a1619b9SMichel Dänzer 
1473dee53e7fSJerome Glisse 	up_write(&rdev->exclusive_lock);
147490aca4d2SJerome Glisse 	return r;
147590aca4d2SJerome Glisse }
147690aca4d2SJerome Glisse 
1477771fe6b9SJerome Glisse 
1478771fe6b9SJerome Glisse /*
1479771fe6b9SJerome Glisse  * Debugfs
1480771fe6b9SJerome Glisse  */
1481771fe6b9SJerome Glisse int radeon_debugfs_add_files(struct radeon_device *rdev,
1482771fe6b9SJerome Glisse 			     struct drm_info_list *files,
1483771fe6b9SJerome Glisse 			     unsigned nfiles)
1484771fe6b9SJerome Glisse {
1485771fe6b9SJerome Glisse 	unsigned i;
1486771fe6b9SJerome Glisse 
14874d8bf9aeSChristian König 	for (i = 0; i < rdev->debugfs_count; i++) {
14884d8bf9aeSChristian König 		if (rdev->debugfs[i].files == files) {
1489771fe6b9SJerome Glisse 			/* Already registered */
1490771fe6b9SJerome Glisse 			return 0;
1491771fe6b9SJerome Glisse 		}
1492771fe6b9SJerome Glisse 	}
1493c245cb9eSMichael Witten 
14944d8bf9aeSChristian König 	i = rdev->debugfs_count + 1;
1495c245cb9eSMichael Witten 	if (i > RADEON_DEBUGFS_MAX_COMPONENTS) {
1496c245cb9eSMichael Witten 		DRM_ERROR("Reached maximum number of debugfs components.\n");
1497c245cb9eSMichael Witten 		DRM_ERROR("Report so we increase "
1498c245cb9eSMichael Witten 		          "RADEON_DEBUGFS_MAX_COMPONENTS.\n");
1499771fe6b9SJerome Glisse 		return -EINVAL;
1500771fe6b9SJerome Glisse 	}
15014d8bf9aeSChristian König 	rdev->debugfs[rdev->debugfs_count].files = files;
15024d8bf9aeSChristian König 	rdev->debugfs[rdev->debugfs_count].num_files = nfiles;
15034d8bf9aeSChristian König 	rdev->debugfs_count = i;
1504771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS)
1505771fe6b9SJerome Glisse 	drm_debugfs_create_files(files, nfiles,
1506771fe6b9SJerome Glisse 				 rdev->ddev->control->debugfs_root,
1507771fe6b9SJerome Glisse 				 rdev->ddev->control);
1508771fe6b9SJerome Glisse 	drm_debugfs_create_files(files, nfiles,
1509771fe6b9SJerome Glisse 				 rdev->ddev->primary->debugfs_root,
1510771fe6b9SJerome Glisse 				 rdev->ddev->primary);
1511771fe6b9SJerome Glisse #endif
1512771fe6b9SJerome Glisse 	return 0;
1513771fe6b9SJerome Glisse }
1514771fe6b9SJerome Glisse 
15154d8bf9aeSChristian König static void radeon_debugfs_remove_files(struct radeon_device *rdev)
15164d8bf9aeSChristian König {
15174d8bf9aeSChristian König #if defined(CONFIG_DEBUG_FS)
15184d8bf9aeSChristian König 	unsigned i;
15194d8bf9aeSChristian König 
15204d8bf9aeSChristian König 	for (i = 0; i < rdev->debugfs_count; i++) {
15214d8bf9aeSChristian König 		drm_debugfs_remove_files(rdev->debugfs[i].files,
15224d8bf9aeSChristian König 					 rdev->debugfs[i].num_files,
15234d8bf9aeSChristian König 					 rdev->ddev->control);
15244d8bf9aeSChristian König 		drm_debugfs_remove_files(rdev->debugfs[i].files,
15254d8bf9aeSChristian König 					 rdev->debugfs[i].num_files,
15264d8bf9aeSChristian König 					 rdev->ddev->primary);
15274d8bf9aeSChristian König 	}
15284d8bf9aeSChristian König #endif
15294d8bf9aeSChristian König }
15304d8bf9aeSChristian König 
1531771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS)
1532771fe6b9SJerome Glisse int radeon_debugfs_init(struct drm_minor *minor)
1533771fe6b9SJerome Glisse {
1534771fe6b9SJerome Glisse 	return 0;
1535771fe6b9SJerome Glisse }
1536771fe6b9SJerome Glisse 
1537771fe6b9SJerome Glisse void radeon_debugfs_cleanup(struct drm_minor *minor)
1538771fe6b9SJerome Glisse {
1539771fe6b9SJerome Glisse }
1540771fe6b9SJerome Glisse #endif
1541