xref: /openbmc/linux/drivers/gpu/drm/radeon/radeon_device.c (revision b8779475869a26ffcd2fde279f7b364ec5722d0d)
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  */
28f9183127SSam Ravnborg 
29771fe6b9SJerome Glisse #include <linux/console.h>
30bcc65fd8SMatthew Garrett #include <linux/efi.h>
312ef79416SThomas Zimmermann #include <linux/pci.h>
32f9183127SSam Ravnborg #include <linux/pm_runtime.h>
33f9183127SSam Ravnborg #include <linux/slab.h>
34f9183127SSam Ravnborg #include <linux/vga_switcheroo.h>
35f9183127SSam Ravnborg #include <linux/vgaarb.h>
36f9183127SSam Ravnborg 
37f9183127SSam Ravnborg #include <drm/drm_cache.h>
38f9183127SSam Ravnborg #include <drm/drm_crtc_helper.h>
39f9183127SSam Ravnborg #include <drm/drm_device.h>
40f9183127SSam Ravnborg #include <drm/drm_file.h>
41f9183127SSam Ravnborg #include <drm/drm_probe_helper.h>
42f9183127SSam Ravnborg #include <drm/radeon_drm.h>
43f9183127SSam Ravnborg 
442aa3b7c8SLee Jones #include "radeon_device.h"
45771fe6b9SJerome Glisse #include "radeon_reg.h"
46771fe6b9SJerome Glisse #include "radeon.h"
47771fe6b9SJerome Glisse #include "atom.h"
48771fe6b9SJerome Glisse 
491b5331d9SJerome Glisse static const char radeon_family_name[][16] = {
501b5331d9SJerome Glisse 	"R100",
511b5331d9SJerome Glisse 	"RV100",
521b5331d9SJerome Glisse 	"RS100",
531b5331d9SJerome Glisse 	"RV200",
541b5331d9SJerome Glisse 	"RS200",
551b5331d9SJerome Glisse 	"R200",
561b5331d9SJerome Glisse 	"RV250",
571b5331d9SJerome Glisse 	"RS300",
581b5331d9SJerome Glisse 	"RV280",
591b5331d9SJerome Glisse 	"R300",
601b5331d9SJerome Glisse 	"R350",
611b5331d9SJerome Glisse 	"RV350",
621b5331d9SJerome Glisse 	"RV380",
631b5331d9SJerome Glisse 	"R420",
641b5331d9SJerome Glisse 	"R423",
651b5331d9SJerome Glisse 	"RV410",
661b5331d9SJerome Glisse 	"RS400",
671b5331d9SJerome Glisse 	"RS480",
681b5331d9SJerome Glisse 	"RS600",
691b5331d9SJerome Glisse 	"RS690",
701b5331d9SJerome Glisse 	"RS740",
711b5331d9SJerome Glisse 	"RV515",
721b5331d9SJerome Glisse 	"R520",
731b5331d9SJerome Glisse 	"RV530",
741b5331d9SJerome Glisse 	"RV560",
751b5331d9SJerome Glisse 	"RV570",
761b5331d9SJerome Glisse 	"R580",
771b5331d9SJerome Glisse 	"R600",
781b5331d9SJerome Glisse 	"RV610",
791b5331d9SJerome Glisse 	"RV630",
801b5331d9SJerome Glisse 	"RV670",
811b5331d9SJerome Glisse 	"RV620",
821b5331d9SJerome Glisse 	"RV635",
831b5331d9SJerome Glisse 	"RS780",
841b5331d9SJerome Glisse 	"RS880",
851b5331d9SJerome Glisse 	"RV770",
861b5331d9SJerome Glisse 	"RV730",
871b5331d9SJerome Glisse 	"RV710",
881b5331d9SJerome Glisse 	"RV740",
891b5331d9SJerome Glisse 	"CEDAR",
901b5331d9SJerome Glisse 	"REDWOOD",
911b5331d9SJerome Glisse 	"JUNIPER",
921b5331d9SJerome Glisse 	"CYPRESS",
931b5331d9SJerome Glisse 	"HEMLOCK",
94b08ebe7eSAlex Deucher 	"PALM",
954df64e65SAlex Deucher 	"SUMO",
964df64e65SAlex Deucher 	"SUMO2",
971fe18305SAlex Deucher 	"BARTS",
981fe18305SAlex Deucher 	"TURKS",
991fe18305SAlex Deucher 	"CAICOS",
100b7cfc9feSAlex Deucher 	"CAYMAN",
1018848f759SAlex Deucher 	"ARUBA",
102cb28bb34SAlex Deucher 	"TAHITI",
103cb28bb34SAlex Deucher 	"PITCAIRN",
104cb28bb34SAlex Deucher 	"VERDE",
105624d3524SAlex Deucher 	"OLAND",
106b5d9d726SAlex Deucher 	"HAINAN",
1076eac752eSAlex Deucher 	"BONAIRE",
1086eac752eSAlex Deucher 	"KAVERI",
1096eac752eSAlex Deucher 	"KABINI",
1103bf599e8SAlex Deucher 	"HAWAII",
111b0a9f22aSSamuel Li 	"MULLINS",
1121b5331d9SJerome Glisse 	"LAST",
1131b5331d9SJerome Glisse };
1141b5331d9SJerome Glisse 
115066f1f0bSAlex Deucher #if defined(CONFIG_VGA_SWITCHEROO)
116066f1f0bSAlex Deucher bool radeon_has_atpx_dgpu_power_cntl(void);
117066f1f0bSAlex Deucher bool radeon_is_atpx_hybrid(void);
118066f1f0bSAlex Deucher #else
119066f1f0bSAlex Deucher static inline bool radeon_has_atpx_dgpu_power_cntl(void) { return false; }
120066f1f0bSAlex Deucher static inline bool radeon_is_atpx_hybrid(void) { return false; }
121066f1f0bSAlex Deucher #endif
122066f1f0bSAlex Deucher 
1234807c5a8SAlex Deucher #define RADEON_PX_QUIRK_DISABLE_PX  (1 << 0)
1244807c5a8SAlex Deucher 
1254807c5a8SAlex Deucher struct radeon_px_quirk {
1264807c5a8SAlex Deucher 	u32 chip_vendor;
1274807c5a8SAlex Deucher 	u32 chip_device;
1284807c5a8SAlex Deucher 	u32 subsys_vendor;
1294807c5a8SAlex Deucher 	u32 subsys_device;
1304807c5a8SAlex Deucher 	u32 px_quirk_flags;
1314807c5a8SAlex Deucher };
1324807c5a8SAlex Deucher 
1334807c5a8SAlex Deucher static struct radeon_px_quirk radeon_px_quirk_list[] = {
1344807c5a8SAlex Deucher 	/* Acer aspire 5560g (CPU: AMD A4-3305M; GPU: AMD Radeon HD 6480g + 7470m)
1354807c5a8SAlex Deucher 	 * https://bugzilla.kernel.org/show_bug.cgi?id=74551
1364807c5a8SAlex Deucher 	 */
1374807c5a8SAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6760, 0x1025, 0x0672, RADEON_PX_QUIRK_DISABLE_PX },
1384807c5a8SAlex Deucher 	/* Asus K73TA laptop with AMD A6-3400M APU and Radeon 6550 GPU
1394807c5a8SAlex Deucher 	 * https://bugzilla.kernel.org/show_bug.cgi?id=51381
1404807c5a8SAlex Deucher 	 */
1414807c5a8SAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6741, 0x1043, 0x108c, RADEON_PX_QUIRK_DISABLE_PX },
142ff1b1294SAlex Deucher 	/* Asus K53TK laptop with AMD A6-3420M APU and Radeon 7670m GPU
143ff1b1294SAlex Deucher 	 * https://bugzilla.kernel.org/show_bug.cgi?id=51381
144ff1b1294SAlex Deucher 	 */
145ff1b1294SAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6840, 0x1043, 0x2122, RADEON_PX_QUIRK_DISABLE_PX },
1464eb59793SAlex Deucher 	/* Asus K53TK laptop with AMD A6-3420M APU and Radeon 7670m GPU
1474eb59793SAlex Deucher 	 * https://bugs.freedesktop.org/show_bug.cgi?id=101491
1484eb59793SAlex Deucher 	 */
1494eb59793SAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6741, 0x1043, 0x2122, RADEON_PX_QUIRK_DISABLE_PX },
150eb40c86aSNico Sneck 	/* Asus K73TK laptop with AMD A6-3420M APU and Radeon 7670m GPU
151eb40c86aSNico Sneck 	 * https://bugzilla.kernel.org/show_bug.cgi?id=51381#c52
152eb40c86aSNico Sneck 	 */
153eb40c86aSNico Sneck 	{ PCI_VENDOR_ID_ATI, 0x6840, 0x1043, 0x2123, RADEON_PX_QUIRK_DISABLE_PX },
1544807c5a8SAlex Deucher 	{ 0, 0, 0, 0, 0 },
1554807c5a8SAlex Deucher };
1564807c5a8SAlex Deucher 
15790c4cde9SAlex Deucher bool radeon_is_px(struct drm_device *dev)
15890c4cde9SAlex Deucher {
15990c4cde9SAlex Deucher 	struct radeon_device *rdev = dev->dev_private;
16090c4cde9SAlex Deucher 
16190c4cde9SAlex Deucher 	if (rdev->flags & RADEON_IS_PX)
16290c4cde9SAlex Deucher 		return true;
16390c4cde9SAlex Deucher 	return false;
16490c4cde9SAlex Deucher }
16510ebc0bcSDave Airlie 
1664807c5a8SAlex Deucher static void radeon_device_handle_px_quirks(struct radeon_device *rdev)
1674807c5a8SAlex Deucher {
1684807c5a8SAlex Deucher 	struct radeon_px_quirk *p = radeon_px_quirk_list;
1694807c5a8SAlex Deucher 
1704807c5a8SAlex Deucher 	/* Apply PX quirks */
1714807c5a8SAlex Deucher 	while (p && p->chip_device != 0) {
1724807c5a8SAlex Deucher 		if (rdev->pdev->vendor == p->chip_vendor &&
1734807c5a8SAlex Deucher 		    rdev->pdev->device == p->chip_device &&
1744807c5a8SAlex Deucher 		    rdev->pdev->subsystem_vendor == p->subsys_vendor &&
1754807c5a8SAlex Deucher 		    rdev->pdev->subsystem_device == p->subsys_device) {
1764807c5a8SAlex Deucher 			rdev->px_quirk_flags = p->px_quirk_flags;
1774807c5a8SAlex Deucher 			break;
1784807c5a8SAlex Deucher 		}
1794807c5a8SAlex Deucher 		++p;
1804807c5a8SAlex Deucher 	}
1814807c5a8SAlex Deucher 
1824807c5a8SAlex Deucher 	if (rdev->px_quirk_flags & RADEON_PX_QUIRK_DISABLE_PX)
1834807c5a8SAlex Deucher 		rdev->flags &= ~RADEON_IS_PX;
184066f1f0bSAlex Deucher 
185066f1f0bSAlex Deucher 	/* disable PX is the system doesn't support dGPU power control or hybrid gfx */
186066f1f0bSAlex Deucher 	if (!radeon_is_atpx_hybrid() &&
187066f1f0bSAlex Deucher 	    !radeon_has_atpx_dgpu_power_cntl())
188066f1f0bSAlex Deucher 		rdev->flags &= ~RADEON_IS_PX;
1894807c5a8SAlex Deucher }
1904807c5a8SAlex Deucher 
1910c195119SAlex Deucher /**
1922e1b65f9SAlex Deucher  * radeon_program_register_sequence - program an array of registers.
1932e1b65f9SAlex Deucher  *
1942e1b65f9SAlex Deucher  * @rdev: radeon_device pointer
1952e1b65f9SAlex Deucher  * @registers: pointer to the register array
1962e1b65f9SAlex Deucher  * @array_size: size of the register array
1972e1b65f9SAlex Deucher  *
1982e1b65f9SAlex Deucher  * Programs an array or registers with and and or masks.
1992e1b65f9SAlex Deucher  * This is a helper for setting golden registers.
2002e1b65f9SAlex Deucher  */
2012e1b65f9SAlex Deucher void radeon_program_register_sequence(struct radeon_device *rdev,
2022e1b65f9SAlex Deucher 				      const u32 *registers,
2032e1b65f9SAlex Deucher 				      const u32 array_size)
2042e1b65f9SAlex Deucher {
2052e1b65f9SAlex Deucher 	u32 tmp, reg, and_mask, or_mask;
2062e1b65f9SAlex Deucher 	int i;
2072e1b65f9SAlex Deucher 
2082e1b65f9SAlex Deucher 	if (array_size % 3)
2092e1b65f9SAlex Deucher 		return;
2102e1b65f9SAlex Deucher 
2112e1b65f9SAlex Deucher 	for (i = 0; i < array_size; i +=3) {
2122e1b65f9SAlex Deucher 		reg = registers[i + 0];
2132e1b65f9SAlex Deucher 		and_mask = registers[i + 1];
2142e1b65f9SAlex Deucher 		or_mask = registers[i + 2];
2152e1b65f9SAlex Deucher 
2162e1b65f9SAlex Deucher 		if (and_mask == 0xffffffff) {
2172e1b65f9SAlex Deucher 			tmp = or_mask;
2182e1b65f9SAlex Deucher 		} else {
2192e1b65f9SAlex Deucher 			tmp = RREG32(reg);
2202e1b65f9SAlex Deucher 			tmp &= ~and_mask;
2212e1b65f9SAlex Deucher 			tmp |= or_mask;
2222e1b65f9SAlex Deucher 		}
2232e1b65f9SAlex Deucher 		WREG32(reg, tmp);
2242e1b65f9SAlex Deucher 	}
2252e1b65f9SAlex Deucher }
2262e1b65f9SAlex Deucher 
2271a0041b8SAlex Deucher void radeon_pci_config_reset(struct radeon_device *rdev)
2281a0041b8SAlex Deucher {
2291a0041b8SAlex Deucher 	pci_write_config_dword(rdev->pdev, 0x7c, RADEON_ASIC_RESET_DATA);
2301a0041b8SAlex Deucher }
2311a0041b8SAlex Deucher 
2322e1b65f9SAlex Deucher /**
2330c195119SAlex Deucher  * radeon_surface_init - Clear GPU surface registers.
2340c195119SAlex Deucher  *
2350c195119SAlex Deucher  * @rdev: radeon_device pointer
2360c195119SAlex Deucher  *
2370c195119SAlex Deucher  * Clear GPU surface registers (r1xx-r5xx).
238b1e3a6d1SMichel Dänzer  */
2393ce0a23dSJerome Glisse void radeon_surface_init(struct radeon_device *rdev)
240b1e3a6d1SMichel Dänzer {
241b1e3a6d1SMichel Dänzer 	/* FIXME: check this out */
242b1e3a6d1SMichel Dänzer 	if (rdev->family < CHIP_R600) {
243b1e3a6d1SMichel Dänzer 		int i;
244b1e3a6d1SMichel Dänzer 
245550e2d92SDave Airlie 		for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
246550e2d92SDave Airlie 			if (rdev->surface_regs[i].bo)
247550e2d92SDave Airlie 				radeon_bo_get_surface_reg(rdev->surface_regs[i].bo);
248550e2d92SDave Airlie 			else
249550e2d92SDave Airlie 				radeon_clear_surface_reg(rdev, i);
250b1e3a6d1SMichel Dänzer 		}
251e024e110SDave Airlie 		/* enable surfaces */
252e024e110SDave Airlie 		WREG32(RADEON_SURFACE_CNTL, 0);
253b1e3a6d1SMichel Dänzer 	}
254b1e3a6d1SMichel Dänzer }
255b1e3a6d1SMichel Dänzer 
256b1e3a6d1SMichel Dänzer /*
257771fe6b9SJerome Glisse  * GPU scratch registers helpers function.
258771fe6b9SJerome Glisse  */
2590c195119SAlex Deucher /**
2600c195119SAlex Deucher  * radeon_scratch_init - Init scratch register driver information.
2610c195119SAlex Deucher  *
2620c195119SAlex Deucher  * @rdev: radeon_device pointer
2630c195119SAlex Deucher  *
2640c195119SAlex Deucher  * Init CP scratch register driver information (r1xx-r5xx)
2650c195119SAlex Deucher  */
2663ce0a23dSJerome Glisse void radeon_scratch_init(struct radeon_device *rdev)
267771fe6b9SJerome Glisse {
268771fe6b9SJerome Glisse 	int i;
269771fe6b9SJerome Glisse 
270771fe6b9SJerome Glisse 	/* FIXME: check this out */
271771fe6b9SJerome Glisse 	if (rdev->family < CHIP_R300) {
272771fe6b9SJerome Glisse 		rdev->scratch.num_reg = 5;
273771fe6b9SJerome Glisse 	} else {
274771fe6b9SJerome Glisse 		rdev->scratch.num_reg = 7;
275771fe6b9SJerome Glisse 	}
276724c80e1SAlex Deucher 	rdev->scratch.reg_base = RADEON_SCRATCH_REG0;
277771fe6b9SJerome Glisse 	for (i = 0; i < rdev->scratch.num_reg; i++) {
278771fe6b9SJerome Glisse 		rdev->scratch.free[i] = true;
279724c80e1SAlex Deucher 		rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4);
280771fe6b9SJerome Glisse 	}
281771fe6b9SJerome Glisse }
282771fe6b9SJerome Glisse 
2830c195119SAlex Deucher /**
2840c195119SAlex Deucher  * radeon_scratch_get - Allocate a scratch register
2850c195119SAlex Deucher  *
2860c195119SAlex Deucher  * @rdev: radeon_device pointer
2870c195119SAlex Deucher  * @reg: scratch register mmio offset
2880c195119SAlex Deucher  *
2890c195119SAlex Deucher  * Allocate a CP scratch register for use by the driver (all asics).
2900c195119SAlex Deucher  * Returns 0 on success or -EINVAL on failure.
2910c195119SAlex Deucher  */
292771fe6b9SJerome Glisse int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg)
293771fe6b9SJerome Glisse {
294771fe6b9SJerome Glisse 	int i;
295771fe6b9SJerome Glisse 
296771fe6b9SJerome Glisse 	for (i = 0; i < rdev->scratch.num_reg; i++) {
297771fe6b9SJerome Glisse 		if (rdev->scratch.free[i]) {
298771fe6b9SJerome Glisse 			rdev->scratch.free[i] = false;
299771fe6b9SJerome Glisse 			*reg = rdev->scratch.reg[i];
300771fe6b9SJerome Glisse 			return 0;
301771fe6b9SJerome Glisse 		}
302771fe6b9SJerome Glisse 	}
303771fe6b9SJerome Glisse 	return -EINVAL;
304771fe6b9SJerome Glisse }
305771fe6b9SJerome Glisse 
3060c195119SAlex Deucher /**
3070c195119SAlex Deucher  * radeon_scratch_free - Free a scratch register
3080c195119SAlex Deucher  *
3090c195119SAlex Deucher  * @rdev: radeon_device pointer
3100c195119SAlex Deucher  * @reg: scratch register mmio offset
3110c195119SAlex Deucher  *
3120c195119SAlex Deucher  * Free a CP scratch register allocated for use by the driver (all asics)
3130c195119SAlex Deucher  */
314771fe6b9SJerome Glisse void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg)
315771fe6b9SJerome Glisse {
316771fe6b9SJerome Glisse 	int i;
317771fe6b9SJerome Glisse 
318771fe6b9SJerome Glisse 	for (i = 0; i < rdev->scratch.num_reg; i++) {
319771fe6b9SJerome Glisse 		if (rdev->scratch.reg[i] == reg) {
320771fe6b9SJerome Glisse 			rdev->scratch.free[i] = true;
321771fe6b9SJerome Glisse 			return;
322771fe6b9SJerome Glisse 		}
323771fe6b9SJerome Glisse 	}
324771fe6b9SJerome Glisse }
325771fe6b9SJerome Glisse 
3260c195119SAlex Deucher /*
32775efdee1SAlex Deucher  * GPU doorbell aperture helpers function.
32875efdee1SAlex Deucher  */
32975efdee1SAlex Deucher /**
33075efdee1SAlex Deucher  * radeon_doorbell_init - Init doorbell driver information.
33175efdee1SAlex Deucher  *
33275efdee1SAlex Deucher  * @rdev: radeon_device pointer
33375efdee1SAlex Deucher  *
33475efdee1SAlex Deucher  * Init doorbell driver information (CIK)
33575efdee1SAlex Deucher  * Returns 0 on success, error on failure.
33675efdee1SAlex Deucher  */
33728f5a6cdSRashika Kheria static int radeon_doorbell_init(struct radeon_device *rdev)
33875efdee1SAlex Deucher {
33975efdee1SAlex Deucher 	/* doorbell bar mapping */
34075efdee1SAlex Deucher 	rdev->doorbell.base = pci_resource_start(rdev->pdev, 2);
34175efdee1SAlex Deucher 	rdev->doorbell.size = pci_resource_len(rdev->pdev, 2);
34275efdee1SAlex Deucher 
343d5754ab8SAndrew Lewycky 	rdev->doorbell.num_doorbells = min_t(u32, rdev->doorbell.size / sizeof(u32), RADEON_MAX_DOORBELLS);
344d5754ab8SAndrew Lewycky 	if (rdev->doorbell.num_doorbells == 0)
345d5754ab8SAndrew Lewycky 		return -EINVAL;
34675efdee1SAlex Deucher 
347d5754ab8SAndrew Lewycky 	rdev->doorbell.ptr = ioremap(rdev->doorbell.base, rdev->doorbell.num_doorbells * sizeof(u32));
34875efdee1SAlex Deucher 	if (rdev->doorbell.ptr == NULL) {
34975efdee1SAlex Deucher 		return -ENOMEM;
35075efdee1SAlex Deucher 	}
35175efdee1SAlex Deucher 	DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)rdev->doorbell.base);
35275efdee1SAlex Deucher 	DRM_INFO("doorbell mmio size: %u\n", (unsigned)rdev->doorbell.size);
35375efdee1SAlex Deucher 
354d5754ab8SAndrew Lewycky 	memset(&rdev->doorbell.used, 0, sizeof(rdev->doorbell.used));
35575efdee1SAlex Deucher 
35675efdee1SAlex Deucher 	return 0;
35775efdee1SAlex Deucher }
35875efdee1SAlex Deucher 
35975efdee1SAlex Deucher /**
36075efdee1SAlex Deucher  * radeon_doorbell_fini - Tear down doorbell driver information.
36175efdee1SAlex Deucher  *
36275efdee1SAlex Deucher  * @rdev: radeon_device pointer
36375efdee1SAlex Deucher  *
36475efdee1SAlex Deucher  * Tear down doorbell driver information (CIK)
36575efdee1SAlex Deucher  */
36628f5a6cdSRashika Kheria static void radeon_doorbell_fini(struct radeon_device *rdev)
36775efdee1SAlex Deucher {
36875efdee1SAlex Deucher 	iounmap(rdev->doorbell.ptr);
36975efdee1SAlex Deucher 	rdev->doorbell.ptr = NULL;
37075efdee1SAlex Deucher }
37175efdee1SAlex Deucher 
37275efdee1SAlex Deucher /**
373d5754ab8SAndrew Lewycky  * radeon_doorbell_get - Allocate a doorbell entry
37475efdee1SAlex Deucher  *
37575efdee1SAlex Deucher  * @rdev: radeon_device pointer
376d5754ab8SAndrew Lewycky  * @doorbell: doorbell index
37775efdee1SAlex Deucher  *
378d5754ab8SAndrew Lewycky  * Allocate a doorbell for use by the driver (all asics).
37975efdee1SAlex Deucher  * Returns 0 on success or -EINVAL on failure.
38075efdee1SAlex Deucher  */
38175efdee1SAlex Deucher int radeon_doorbell_get(struct radeon_device *rdev, u32 *doorbell)
38275efdee1SAlex Deucher {
383d5754ab8SAndrew Lewycky 	unsigned long offset = find_first_zero_bit(rdev->doorbell.used, rdev->doorbell.num_doorbells);
384d5754ab8SAndrew Lewycky 	if (offset < rdev->doorbell.num_doorbells) {
385d5754ab8SAndrew Lewycky 		__set_bit(offset, rdev->doorbell.used);
386d5754ab8SAndrew Lewycky 		*doorbell = offset;
38775efdee1SAlex Deucher 		return 0;
388d5754ab8SAndrew Lewycky 	} else {
38975efdee1SAlex Deucher 		return -EINVAL;
39075efdee1SAlex Deucher 	}
391d5754ab8SAndrew Lewycky }
39275efdee1SAlex Deucher 
39375efdee1SAlex Deucher /**
394d5754ab8SAndrew Lewycky  * radeon_doorbell_free - Free a doorbell entry
39575efdee1SAlex Deucher  *
39675efdee1SAlex Deucher  * @rdev: radeon_device pointer
397d5754ab8SAndrew Lewycky  * @doorbell: doorbell index
39875efdee1SAlex Deucher  *
399d5754ab8SAndrew Lewycky  * Free a doorbell allocated for use by the driver (all asics)
40075efdee1SAlex Deucher  */
40175efdee1SAlex Deucher void radeon_doorbell_free(struct radeon_device *rdev, u32 doorbell)
40275efdee1SAlex Deucher {
403d5754ab8SAndrew Lewycky 	if (doorbell < rdev->doorbell.num_doorbells)
404d5754ab8SAndrew Lewycky 		__clear_bit(doorbell, rdev->doorbell.used);
40575efdee1SAlex Deucher }
40675efdee1SAlex Deucher 
40775efdee1SAlex Deucher /*
4080c195119SAlex Deucher  * radeon_wb_*()
4090c195119SAlex Deucher  * Writeback is the the method by which the the GPU updates special pages
4100c195119SAlex Deucher  * in memory with the status of certain GPU events (fences, ring pointers,
4110c195119SAlex Deucher  * etc.).
4120c195119SAlex Deucher  */
4130c195119SAlex Deucher 
4140c195119SAlex Deucher /**
4150c195119SAlex Deucher  * radeon_wb_disable - Disable Writeback
4160c195119SAlex Deucher  *
4170c195119SAlex Deucher  * @rdev: radeon_device pointer
4180c195119SAlex Deucher  *
4190c195119SAlex Deucher  * Disables Writeback (all asics).  Used for suspend.
4200c195119SAlex Deucher  */
421724c80e1SAlex Deucher void radeon_wb_disable(struct radeon_device *rdev)
422724c80e1SAlex Deucher {
423724c80e1SAlex Deucher 	rdev->wb.enabled = false;
424724c80e1SAlex Deucher }
425724c80e1SAlex Deucher 
4260c195119SAlex Deucher /**
4270c195119SAlex Deucher  * radeon_wb_fini - Disable Writeback and free memory
4280c195119SAlex Deucher  *
4290c195119SAlex Deucher  * @rdev: radeon_device pointer
4300c195119SAlex Deucher  *
4310c195119SAlex Deucher  * Disables Writeback and frees the Writeback memory (all asics).
4320c195119SAlex Deucher  * Used at driver shutdown.
4330c195119SAlex Deucher  */
434724c80e1SAlex Deucher void radeon_wb_fini(struct radeon_device *rdev)
435724c80e1SAlex Deucher {
436724c80e1SAlex Deucher 	radeon_wb_disable(rdev);
437724c80e1SAlex Deucher 	if (rdev->wb.wb_obj) {
438089920f2SJerome Glisse 		if (!radeon_bo_reserve(rdev->wb.wb_obj, false)) {
439089920f2SJerome Glisse 			radeon_bo_kunmap(rdev->wb.wb_obj);
440089920f2SJerome Glisse 			radeon_bo_unpin(rdev->wb.wb_obj);
441089920f2SJerome Glisse 			radeon_bo_unreserve(rdev->wb.wb_obj);
442089920f2SJerome Glisse 		}
443724c80e1SAlex Deucher 		radeon_bo_unref(&rdev->wb.wb_obj);
444724c80e1SAlex Deucher 		rdev->wb.wb = NULL;
445724c80e1SAlex Deucher 		rdev->wb.wb_obj = NULL;
446724c80e1SAlex Deucher 	}
447724c80e1SAlex Deucher }
448724c80e1SAlex Deucher 
4490c195119SAlex Deucher /**
4500c195119SAlex Deucher  * radeon_wb_init- Init Writeback driver info and allocate memory
4510c195119SAlex Deucher  *
4520c195119SAlex Deucher  * @rdev: radeon_device pointer
4530c195119SAlex Deucher  *
4540c195119SAlex Deucher  * Disables Writeback and frees the Writeback memory (all asics).
4550c195119SAlex Deucher  * Used at driver startup.
4560c195119SAlex Deucher  * Returns 0 on success or an -error on failure.
4570c195119SAlex Deucher  */
458724c80e1SAlex Deucher int radeon_wb_init(struct radeon_device *rdev)
459724c80e1SAlex Deucher {
460724c80e1SAlex Deucher 	int r;
461724c80e1SAlex Deucher 
462724c80e1SAlex Deucher 	if (rdev->wb.wb_obj == NULL) {
463441921d5SDaniel Vetter 		r = radeon_bo_create(rdev, RADEON_GPU_PAGE_SIZE, PAGE_SIZE, true,
464831b6966SMaarten Lankhorst 				     RADEON_GEM_DOMAIN_GTT, 0, NULL, NULL,
46502376d82SMichel Dänzer 				     &rdev->wb.wb_obj);
466724c80e1SAlex Deucher 		if (r) {
467724c80e1SAlex Deucher 			dev_warn(rdev->dev, "(%d) create WB bo failed\n", r);
468724c80e1SAlex Deucher 			return r;
469724c80e1SAlex Deucher 		}
470724c80e1SAlex Deucher 		r = radeon_bo_reserve(rdev->wb.wb_obj, false);
471724c80e1SAlex Deucher 		if (unlikely(r != 0)) {
472724c80e1SAlex Deucher 			radeon_wb_fini(rdev);
473724c80e1SAlex Deucher 			return r;
474724c80e1SAlex Deucher 		}
475724c80e1SAlex Deucher 		r = radeon_bo_pin(rdev->wb.wb_obj, RADEON_GEM_DOMAIN_GTT,
476724c80e1SAlex Deucher 				&rdev->wb.gpu_addr);
477724c80e1SAlex Deucher 		if (r) {
478724c80e1SAlex Deucher 			radeon_bo_unreserve(rdev->wb.wb_obj);
479724c80e1SAlex Deucher 			dev_warn(rdev->dev, "(%d) pin WB bo failed\n", r);
480724c80e1SAlex Deucher 			radeon_wb_fini(rdev);
481724c80e1SAlex Deucher 			return r;
482724c80e1SAlex Deucher 		}
483724c80e1SAlex Deucher 		r = radeon_bo_kmap(rdev->wb.wb_obj, (void **)&rdev->wb.wb);
484724c80e1SAlex Deucher 		radeon_bo_unreserve(rdev->wb.wb_obj);
485724c80e1SAlex Deucher 		if (r) {
486724c80e1SAlex Deucher 			dev_warn(rdev->dev, "(%d) map WB bo failed\n", r);
487724c80e1SAlex Deucher 			radeon_wb_fini(rdev);
488724c80e1SAlex Deucher 			return r;
489724c80e1SAlex Deucher 		}
490089920f2SJerome Glisse 	}
491724c80e1SAlex Deucher 
492e6ba7599SAlex Deucher 	/* clear wb memory */
493e6ba7599SAlex Deucher 	memset((char *)rdev->wb.wb, 0, RADEON_GPU_PAGE_SIZE);
494d0f8a854SAlex Deucher 	/* disable event_write fences */
495d0f8a854SAlex Deucher 	rdev->wb.use_event = false;
496724c80e1SAlex Deucher 	/* disabled via module param */
4973b7a2b24SJerome Glisse 	if (radeon_no_wb == 1) {
498724c80e1SAlex Deucher 		rdev->wb.enabled = false;
4993b7a2b24SJerome Glisse 	} else {
500724c80e1SAlex Deucher 		if (rdev->flags & RADEON_IS_AGP) {
50128eebb70SAlex Deucher 			/* often unreliable on AGP */
50228eebb70SAlex Deucher 			rdev->wb.enabled = false;
50328eebb70SAlex Deucher 		} else if (rdev->family < CHIP_R300) {
50428eebb70SAlex Deucher 			/* often unreliable on pre-r300 */
505724c80e1SAlex Deucher 			rdev->wb.enabled = false;
506d0f8a854SAlex Deucher 		} else {
507724c80e1SAlex Deucher 			rdev->wb.enabled = true;
508d0f8a854SAlex Deucher 			/* event_write fences are only available on r600+ */
5093b7a2b24SJerome Glisse 			if (rdev->family >= CHIP_R600) {
510d0f8a854SAlex Deucher 				rdev->wb.use_event = true;
511d0f8a854SAlex Deucher 			}
512724c80e1SAlex Deucher 		}
5133b7a2b24SJerome Glisse 	}
514c994ead6SAlex Deucher 	/* always use writeback/events on NI, APUs */
515c994ead6SAlex Deucher 	if (rdev->family >= CHIP_PALM) {
5167d52785dSAlex Deucher 		rdev->wb.enabled = true;
5177d52785dSAlex Deucher 		rdev->wb.use_event = true;
5187d52785dSAlex Deucher 	}
519724c80e1SAlex Deucher 
520724c80e1SAlex Deucher 	dev_info(rdev->dev, "WB %sabled\n", rdev->wb.enabled ? "en" : "dis");
521724c80e1SAlex Deucher 
522724c80e1SAlex Deucher 	return 0;
523724c80e1SAlex Deucher }
524724c80e1SAlex Deucher 
525d594e46aSJerome Glisse /**
526d594e46aSJerome Glisse  * radeon_vram_location - try to find VRAM location
527d594e46aSJerome Glisse  * @rdev: radeon device structure holding all necessary informations
528d594e46aSJerome Glisse  * @mc: memory controller structure holding memory informations
529d594e46aSJerome Glisse  * @base: base address at which to put VRAM
530d594e46aSJerome Glisse  *
531d594e46aSJerome Glisse  * Function will place try to place VRAM at base address provided
532d594e46aSJerome Glisse  * as parameter (which is so far either PCI aperture address or
533d594e46aSJerome Glisse  * for IGP TOM base address).
534d594e46aSJerome Glisse  *
535d594e46aSJerome Glisse  * If there is not enough space to fit the unvisible VRAM in the 32bits
536d594e46aSJerome Glisse  * address space then we limit the VRAM size to the aperture.
537d594e46aSJerome Glisse  *
538d594e46aSJerome Glisse  * If we are using AGP and if the AGP aperture doesn't allow us to have
539d594e46aSJerome Glisse  * room for all the VRAM than we restrict the VRAM to the PCI aperture
540d594e46aSJerome Glisse  * size and print a warning.
541d594e46aSJerome Glisse  *
542d594e46aSJerome Glisse  * This function will never fails, worst case are limiting VRAM.
543d594e46aSJerome Glisse  *
544d594e46aSJerome Glisse  * Note: GTT start, end, size should be initialized before calling this
545d594e46aSJerome Glisse  * function on AGP platform.
546d594e46aSJerome Glisse  *
547f017853eSLee Jones  * Note 1: We don't explicitly enforce VRAM start to be aligned on VRAM size,
548d594e46aSJerome Glisse  * this shouldn't be a problem as we are using the PCI aperture as a reference.
549d594e46aSJerome Glisse  * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
550d594e46aSJerome Glisse  * not IGP.
551d594e46aSJerome Glisse  *
552f017853eSLee Jones  * Note 2: we use mc_vram_size as on some board we need to program the mc to
553d594e46aSJerome Glisse  * cover the whole aperture even if VRAM size is inferior to aperture size
554d594e46aSJerome Glisse  * Novell bug 204882 + along with lots of ubuntu ones
555d594e46aSJerome Glisse  *
556f017853eSLee Jones  * Note 3: when limiting vram it's safe to overwritte real_vram_size because
557d594e46aSJerome Glisse  * we are not in case where real_vram_size is inferior to mc_vram_size (ie
558d594e46aSJerome Glisse  * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
559d594e46aSJerome Glisse  * ones)
560d594e46aSJerome Glisse  *
561f017853eSLee Jones  * Note 4: IGP TOM addr should be the same as the aperture addr, we don't
562d594e46aSJerome Glisse  * explicitly check for that thought.
563d594e46aSJerome Glisse  *
564d594e46aSJerome Glisse  * FIXME: when reducing VRAM size align new size on power of 2.
565771fe6b9SJerome Glisse  */
566d594e46aSJerome Glisse void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base)
567771fe6b9SJerome Glisse {
5681bcb04f7SChristian König 	uint64_t limit = (uint64_t)radeon_vram_limit << 20;
5691bcb04f7SChristian König 
570d594e46aSJerome Glisse 	mc->vram_start = base;
5719ed8b1f9SAlex Deucher 	if (mc->mc_vram_size > (rdev->mc.mc_mask - base + 1)) {
572d594e46aSJerome Glisse 		dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
573d594e46aSJerome Glisse 		mc->real_vram_size = mc->aper_size;
574d594e46aSJerome Glisse 		mc->mc_vram_size = mc->aper_size;
575771fe6b9SJerome Glisse 	}
576d594e46aSJerome Glisse 	mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
5772cbeb4efSJerome Glisse 	if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_start <= mc->gtt_end) {
578d594e46aSJerome Glisse 		dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
579d594e46aSJerome Glisse 		mc->real_vram_size = mc->aper_size;
580d594e46aSJerome Glisse 		mc->mc_vram_size = mc->aper_size;
581771fe6b9SJerome Glisse 	}
582d594e46aSJerome Glisse 	mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
5831bcb04f7SChristian König 	if (limit && limit < mc->real_vram_size)
5841bcb04f7SChristian König 		mc->real_vram_size = limit;
585dd7cc55aSAlex Deucher 	dev_info(rdev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
586d594e46aSJerome Glisse 			mc->mc_vram_size >> 20, mc->vram_start,
587d594e46aSJerome Glisse 			mc->vram_end, mc->real_vram_size >> 20);
588771fe6b9SJerome Glisse }
589771fe6b9SJerome Glisse 
590d594e46aSJerome Glisse /**
591d594e46aSJerome Glisse  * radeon_gtt_location - try to find GTT location
592d594e46aSJerome Glisse  * @rdev: radeon device structure holding all necessary informations
593d594e46aSJerome Glisse  * @mc: memory controller structure holding memory informations
594d594e46aSJerome Glisse  *
595d594e46aSJerome Glisse  * Function will place try to place GTT before or after VRAM.
596d594e46aSJerome Glisse  *
597d594e46aSJerome Glisse  * If GTT size is bigger than space left then we ajust GTT size.
598d594e46aSJerome Glisse  * Thus function will never fails.
599d594e46aSJerome Glisse  *
600d594e46aSJerome Glisse  * FIXME: when reducing GTT size align new size on power of 2.
601d594e46aSJerome Glisse  */
602d594e46aSJerome Glisse void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc)
603d594e46aSJerome Glisse {
604d594e46aSJerome Glisse 	u64 size_af, size_bf;
605d594e46aSJerome Glisse 
6069ed8b1f9SAlex Deucher 	size_af = ((rdev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
6078d369bb1SAlex Deucher 	size_bf = mc->vram_start & ~mc->gtt_base_align;
608d594e46aSJerome Glisse 	if (size_bf > size_af) {
609d594e46aSJerome Glisse 		if (mc->gtt_size > size_bf) {
610d594e46aSJerome Glisse 			dev_warn(rdev->dev, "limiting GTT\n");
611d594e46aSJerome Glisse 			mc->gtt_size = size_bf;
612d594e46aSJerome Glisse 		}
6138d369bb1SAlex Deucher 		mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
614d594e46aSJerome Glisse 	} else {
615d594e46aSJerome Glisse 		if (mc->gtt_size > size_af) {
616d594e46aSJerome Glisse 			dev_warn(rdev->dev, "limiting GTT\n");
617d594e46aSJerome Glisse 			mc->gtt_size = size_af;
618d594e46aSJerome Glisse 		}
6198d369bb1SAlex Deucher 		mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
620d594e46aSJerome Glisse 	}
621d594e46aSJerome Glisse 	mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
622dd7cc55aSAlex Deucher 	dev_info(rdev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
623d594e46aSJerome Glisse 			mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
624d594e46aSJerome Glisse }
625771fe6b9SJerome Glisse 
626771fe6b9SJerome Glisse /*
627771fe6b9SJerome Glisse  * GPU helpers function.
628771fe6b9SJerome Glisse  */
62905082b8bSAlex Deucher 
630f017853eSLee Jones /*
63105082b8bSAlex Deucher  * radeon_device_is_virtual - check if we are running is a virtual environment
63205082b8bSAlex Deucher  *
63305082b8bSAlex Deucher  * Check if the asic has been passed through to a VM (all asics).
63405082b8bSAlex Deucher  * Used at driver startup.
63505082b8bSAlex Deucher  * Returns true if virtual or false if not.
63605082b8bSAlex Deucher  */
637a801abe4SAlex Deucher bool radeon_device_is_virtual(void)
63805082b8bSAlex Deucher {
63905082b8bSAlex Deucher #ifdef CONFIG_X86
64005082b8bSAlex Deucher 	return boot_cpu_has(X86_FEATURE_HYPERVISOR);
64105082b8bSAlex Deucher #else
64205082b8bSAlex Deucher 	return false;
64305082b8bSAlex Deucher #endif
64405082b8bSAlex Deucher }
64505082b8bSAlex Deucher 
6460c195119SAlex Deucher /**
6470c195119SAlex Deucher  * radeon_card_posted - check if the hw has already been initialized
6480c195119SAlex Deucher  *
6490c195119SAlex Deucher  * @rdev: radeon_device pointer
6500c195119SAlex Deucher  *
6510c195119SAlex Deucher  * Check if the asic has been initialized (all asics).
6520c195119SAlex Deucher  * Used at driver startup.
6530c195119SAlex Deucher  * Returns true if initialized or false if not.
6540c195119SAlex Deucher  */
6559f022ddfSJerome Glisse bool radeon_card_posted(struct radeon_device *rdev)
656771fe6b9SJerome Glisse {
657771fe6b9SJerome Glisse 	uint32_t reg;
658771fe6b9SJerome Glisse 
659884031f0SAlex Deucher 	/* for pass through, always force asic_init for CI */
660884031f0SAlex Deucher 	if (rdev->family >= CHIP_BONAIRE &&
661884031f0SAlex Deucher 	    radeon_device_is_virtual())
66205082b8bSAlex Deucher 		return false;
66305082b8bSAlex Deucher 
66450a583f6SAlex Deucher 	/* required for EFI mode on macbook2,1 which uses an r5xx asic */
66583e68189SMatt Fleming 	if (efi_enabled(EFI_BOOT) &&
66650a583f6SAlex Deucher 	    (rdev->pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE) &&
66750a583f6SAlex Deucher 	    (rdev->family < CHIP_R600))
668bcc65fd8SMatthew Garrett 		return false;
669bcc65fd8SMatthew Garrett 
6702cf3a4fcSAlex Deucher 	if (ASIC_IS_NODCE(rdev))
6712cf3a4fcSAlex Deucher 		goto check_memsize;
6722cf3a4fcSAlex Deucher 
673771fe6b9SJerome Glisse 	/* first check CRTCs */
67409fb8bd1SAlex Deucher 	if (ASIC_IS_DCE4(rdev)) {
67518007401SAlex Deucher 		reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
67618007401SAlex Deucher 			RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET);
67709fb8bd1SAlex Deucher 			if (rdev->num_crtc >= 4) {
67809fb8bd1SAlex Deucher 				reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) |
67909fb8bd1SAlex Deucher 					RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET);
68009fb8bd1SAlex Deucher 			}
68109fb8bd1SAlex Deucher 			if (rdev->num_crtc >= 6) {
68209fb8bd1SAlex Deucher 				reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) |
683bcc1c2a1SAlex Deucher 					RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET);
68409fb8bd1SAlex Deucher 			}
685bcc1c2a1SAlex Deucher 		if (reg & EVERGREEN_CRTC_MASTER_EN)
686bcc1c2a1SAlex Deucher 			return true;
687bcc1c2a1SAlex Deucher 	} else if (ASIC_IS_AVIVO(rdev)) {
688771fe6b9SJerome Glisse 		reg = RREG32(AVIVO_D1CRTC_CONTROL) |
689771fe6b9SJerome Glisse 		      RREG32(AVIVO_D2CRTC_CONTROL);
690771fe6b9SJerome Glisse 		if (reg & AVIVO_CRTC_EN) {
691771fe6b9SJerome Glisse 			return true;
692771fe6b9SJerome Glisse 		}
693771fe6b9SJerome Glisse 	} else {
694771fe6b9SJerome Glisse 		reg = RREG32(RADEON_CRTC_GEN_CNTL) |
695771fe6b9SJerome Glisse 		      RREG32(RADEON_CRTC2_GEN_CNTL);
696771fe6b9SJerome Glisse 		if (reg & RADEON_CRTC_EN) {
697771fe6b9SJerome Glisse 			return true;
698771fe6b9SJerome Glisse 		}
699771fe6b9SJerome Glisse 	}
700771fe6b9SJerome Glisse 
7012cf3a4fcSAlex Deucher check_memsize:
702771fe6b9SJerome Glisse 	/* then check MEM_SIZE, in case the crtcs are off */
703771fe6b9SJerome Glisse 	if (rdev->family >= CHIP_R600)
704771fe6b9SJerome Glisse 		reg = RREG32(R600_CONFIG_MEMSIZE);
705771fe6b9SJerome Glisse 	else
706771fe6b9SJerome Glisse 		reg = RREG32(RADEON_CONFIG_MEMSIZE);
707771fe6b9SJerome Glisse 
708771fe6b9SJerome Glisse 	if (reg)
709771fe6b9SJerome Glisse 		return true;
710771fe6b9SJerome Glisse 
711771fe6b9SJerome Glisse 	return false;
712771fe6b9SJerome Glisse 
713771fe6b9SJerome Glisse }
714771fe6b9SJerome Glisse 
7150c195119SAlex Deucher /**
7160c195119SAlex Deucher  * radeon_update_bandwidth_info - update display bandwidth params
7170c195119SAlex Deucher  *
7180c195119SAlex Deucher  * @rdev: radeon_device pointer
7190c195119SAlex Deucher  *
7200c195119SAlex Deucher  * Used when sclk/mclk are switched or display modes are set.
7210c195119SAlex Deucher  * params are used to calculate display watermarks (all asics)
7220c195119SAlex Deucher  */
723f47299c5SAlex Deucher void radeon_update_bandwidth_info(struct radeon_device *rdev)
724f47299c5SAlex Deucher {
725f47299c5SAlex Deucher 	fixed20_12 a;
7268807286eSAlex Deucher 	u32 sclk = rdev->pm.current_sclk;
7278807286eSAlex Deucher 	u32 mclk = rdev->pm.current_mclk;
728f47299c5SAlex Deucher 
7298807286eSAlex Deucher 	/* sclk/mclk in Mhz */
73068adac5eSBen Skeggs 	a.full = dfixed_const(100);
73168adac5eSBen Skeggs 	rdev->pm.sclk.full = dfixed_const(sclk);
73268adac5eSBen Skeggs 	rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a);
73368adac5eSBen Skeggs 	rdev->pm.mclk.full = dfixed_const(mclk);
73468adac5eSBen Skeggs 	rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a);
735f47299c5SAlex Deucher 
7368807286eSAlex Deucher 	if (rdev->flags & RADEON_IS_IGP) {
73768adac5eSBen Skeggs 		a.full = dfixed_const(16);
738f47299c5SAlex Deucher 		/* core_bandwidth = sclk(Mhz) * 16 */
73968adac5eSBen Skeggs 		rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a);
740f47299c5SAlex Deucher 	}
741f47299c5SAlex Deucher }
742f47299c5SAlex Deucher 
7430c195119SAlex Deucher /**
7440c195119SAlex Deucher  * radeon_boot_test_post_card - check and possibly initialize the hw
7450c195119SAlex Deucher  *
7460c195119SAlex Deucher  * @rdev: radeon_device pointer
7470c195119SAlex Deucher  *
7480c195119SAlex Deucher  * Check if the asic is initialized and if not, attempt to initialize
7490c195119SAlex Deucher  * it (all asics).
7500c195119SAlex Deucher  * Returns true if initialized or false if not.
7510c195119SAlex Deucher  */
75272542d77SDave Airlie bool radeon_boot_test_post_card(struct radeon_device *rdev)
75372542d77SDave Airlie {
75472542d77SDave Airlie 	if (radeon_card_posted(rdev))
75572542d77SDave Airlie 		return true;
75672542d77SDave Airlie 
75772542d77SDave Airlie 	if (rdev->bios) {
75872542d77SDave Airlie 		DRM_INFO("GPU not posted. posting now...\n");
75972542d77SDave Airlie 		if (rdev->is_atom_bios)
76072542d77SDave Airlie 			atom_asic_init(rdev->mode_info.atom_context);
76172542d77SDave Airlie 		else
76272542d77SDave Airlie 			radeon_combios_asic_init(rdev->ddev);
76372542d77SDave Airlie 		return true;
76472542d77SDave Airlie 	} else {
76572542d77SDave Airlie 		dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
76672542d77SDave Airlie 		return false;
76772542d77SDave Airlie 	}
76872542d77SDave Airlie }
76972542d77SDave Airlie 
7700c195119SAlex Deucher /**
7710c195119SAlex Deucher  * radeon_dummy_page_init - init dummy page used by the driver
7720c195119SAlex Deucher  *
7730c195119SAlex Deucher  * @rdev: radeon_device pointer
7740c195119SAlex Deucher  *
7750c195119SAlex Deucher  * Allocate the dummy page used by the driver (all asics).
7760c195119SAlex Deucher  * This dummy page is used by the driver as a filler for gart entries
7770c195119SAlex Deucher  * when pages are taken out of the GART
7780c195119SAlex Deucher  * Returns 0 on sucess, -ENOMEM on failure.
7790c195119SAlex Deucher  */
7803ce0a23dSJerome Glisse int radeon_dummy_page_init(struct radeon_device *rdev)
7813ce0a23dSJerome Glisse {
78282568565SDave Airlie 	if (rdev->dummy_page.page)
78382568565SDave Airlie 		return 0;
7843ce0a23dSJerome Glisse 	rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
7853ce0a23dSJerome Glisse 	if (rdev->dummy_page.page == NULL)
7863ce0a23dSJerome Glisse 		return -ENOMEM;
7877e7726ecSNirmoy Das 	rdev->dummy_page.addr = dma_map_page(&rdev->pdev->dev, rdev->dummy_page.page,
7883ce0a23dSJerome Glisse 					0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
7897e7726ecSNirmoy Das 	if (dma_mapping_error(&rdev->pdev->dev, rdev->dummy_page.addr)) {
790a30f6fb7SBenjamin Herrenschmidt 		dev_err(&rdev->pdev->dev, "Failed to DMA MAP the dummy page\n");
7913ce0a23dSJerome Glisse 		__free_page(rdev->dummy_page.page);
7923ce0a23dSJerome Glisse 		rdev->dummy_page.page = NULL;
7933ce0a23dSJerome Glisse 		return -ENOMEM;
7943ce0a23dSJerome Glisse 	}
795cb658906SMichel Dänzer 	rdev->dummy_page.entry = radeon_gart_get_page_entry(rdev->dummy_page.addr,
796cb658906SMichel Dänzer 							    RADEON_GART_PAGE_DUMMY);
7973ce0a23dSJerome Glisse 	return 0;
7983ce0a23dSJerome Glisse }
7993ce0a23dSJerome Glisse 
8000c195119SAlex Deucher /**
8010c195119SAlex Deucher  * radeon_dummy_page_fini - free dummy page used by the driver
8020c195119SAlex Deucher  *
8030c195119SAlex Deucher  * @rdev: radeon_device pointer
8040c195119SAlex Deucher  *
8050c195119SAlex Deucher  * Frees the dummy page used by the driver (all asics).
8060c195119SAlex Deucher  */
8073ce0a23dSJerome Glisse void radeon_dummy_page_fini(struct radeon_device *rdev)
8083ce0a23dSJerome Glisse {
8093ce0a23dSJerome Glisse 	if (rdev->dummy_page.page == NULL)
8103ce0a23dSJerome Glisse 		return;
8113ce0a23dSJerome Glisse 	pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
8123ce0a23dSJerome Glisse 			PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
8133ce0a23dSJerome Glisse 	__free_page(rdev->dummy_page.page);
8143ce0a23dSJerome Glisse 	rdev->dummy_page.page = NULL;
8153ce0a23dSJerome Glisse }
8163ce0a23dSJerome Glisse 
817771fe6b9SJerome Glisse 
818771fe6b9SJerome Glisse /* ATOM accessor methods */
8190c195119SAlex Deucher /*
8200c195119SAlex Deucher  * ATOM is an interpreted byte code stored in tables in the vbios.  The
8210c195119SAlex Deucher  * driver registers callbacks to access registers and the interpreter
8220c195119SAlex Deucher  * in the driver parses the tables and executes then to program specific
8230c195119SAlex Deucher  * actions (set display modes, asic init, etc.).  See radeon_atombios.c,
8240c195119SAlex Deucher  * atombios.h, and atom.c
8250c195119SAlex Deucher  */
8260c195119SAlex Deucher 
8270c195119SAlex Deucher /**
8280c195119SAlex Deucher  * cail_pll_read - read PLL register
8290c195119SAlex Deucher  *
8300c195119SAlex Deucher  * @info: atom card_info pointer
8310c195119SAlex Deucher  * @reg: PLL register offset
8320c195119SAlex Deucher  *
8330c195119SAlex Deucher  * Provides a PLL register accessor for the atom interpreter (r4xx+).
8340c195119SAlex Deucher  * Returns the value of the PLL register.
8350c195119SAlex Deucher  */
836771fe6b9SJerome Glisse static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
837771fe6b9SJerome Glisse {
838771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
839771fe6b9SJerome Glisse 	uint32_t r;
840771fe6b9SJerome Glisse 
841771fe6b9SJerome Glisse 	r = rdev->pll_rreg(rdev, reg);
842771fe6b9SJerome Glisse 	return r;
843771fe6b9SJerome Glisse }
844771fe6b9SJerome Glisse 
8450c195119SAlex Deucher /**
8460c195119SAlex Deucher  * cail_pll_write - write PLL register
8470c195119SAlex Deucher  *
8480c195119SAlex Deucher  * @info: atom card_info pointer
8490c195119SAlex Deucher  * @reg: PLL register offset
8500c195119SAlex Deucher  * @val: value to write to the pll register
8510c195119SAlex Deucher  *
8520c195119SAlex Deucher  * Provides a PLL register accessor for the atom interpreter (r4xx+).
8530c195119SAlex Deucher  */
854771fe6b9SJerome Glisse static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
855771fe6b9SJerome Glisse {
856771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
857771fe6b9SJerome Glisse 
858771fe6b9SJerome Glisse 	rdev->pll_wreg(rdev, reg, val);
859771fe6b9SJerome Glisse }
860771fe6b9SJerome Glisse 
8610c195119SAlex Deucher /**
8620c195119SAlex Deucher  * cail_mc_read - read MC (Memory Controller) register
8630c195119SAlex Deucher  *
8640c195119SAlex Deucher  * @info: atom card_info pointer
8650c195119SAlex Deucher  * @reg: MC register offset
8660c195119SAlex Deucher  *
8670c195119SAlex Deucher  * Provides an MC register accessor for the atom interpreter (r4xx+).
8680c195119SAlex Deucher  * Returns the value of the MC register.
8690c195119SAlex Deucher  */
870771fe6b9SJerome Glisse static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
871771fe6b9SJerome Glisse {
872771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
873771fe6b9SJerome Glisse 	uint32_t r;
874771fe6b9SJerome Glisse 
875771fe6b9SJerome Glisse 	r = rdev->mc_rreg(rdev, reg);
876771fe6b9SJerome Glisse 	return r;
877771fe6b9SJerome Glisse }
878771fe6b9SJerome Glisse 
8790c195119SAlex Deucher /**
8800c195119SAlex Deucher  * cail_mc_write - write MC (Memory Controller) register
8810c195119SAlex Deucher  *
8820c195119SAlex Deucher  * @info: atom card_info pointer
8830c195119SAlex Deucher  * @reg: MC register offset
8840c195119SAlex Deucher  * @val: value to write to the pll register
8850c195119SAlex Deucher  *
8860c195119SAlex Deucher  * Provides a MC register accessor for the atom interpreter (r4xx+).
8870c195119SAlex Deucher  */
888771fe6b9SJerome Glisse static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
889771fe6b9SJerome Glisse {
890771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
891771fe6b9SJerome Glisse 
892771fe6b9SJerome Glisse 	rdev->mc_wreg(rdev, reg, val);
893771fe6b9SJerome Glisse }
894771fe6b9SJerome Glisse 
8950c195119SAlex Deucher /**
8960c195119SAlex Deucher  * cail_reg_write - write MMIO register
8970c195119SAlex Deucher  *
8980c195119SAlex Deucher  * @info: atom card_info pointer
8990c195119SAlex Deucher  * @reg: MMIO register offset
9000c195119SAlex Deucher  * @val: value to write to the pll register
9010c195119SAlex Deucher  *
9020c195119SAlex Deucher  * Provides a MMIO register accessor for the atom interpreter (r4xx+).
9030c195119SAlex Deucher  */
904771fe6b9SJerome Glisse static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
905771fe6b9SJerome Glisse {
906771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
907771fe6b9SJerome Glisse 
908771fe6b9SJerome Glisse 	WREG32(reg*4, val);
909771fe6b9SJerome Glisse }
910771fe6b9SJerome Glisse 
9110c195119SAlex Deucher /**
9120c195119SAlex Deucher  * cail_reg_read - read MMIO register
9130c195119SAlex Deucher  *
9140c195119SAlex Deucher  * @info: atom card_info pointer
9150c195119SAlex Deucher  * @reg: MMIO register offset
9160c195119SAlex Deucher  *
9170c195119SAlex Deucher  * Provides an MMIO register accessor for the atom interpreter (r4xx+).
9180c195119SAlex Deucher  * Returns the value of the MMIO register.
9190c195119SAlex Deucher  */
920771fe6b9SJerome Glisse static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
921771fe6b9SJerome Glisse {
922771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
923771fe6b9SJerome Glisse 	uint32_t r;
924771fe6b9SJerome Glisse 
925771fe6b9SJerome Glisse 	r = RREG32(reg*4);
926771fe6b9SJerome Glisse 	return r;
927771fe6b9SJerome Glisse }
928771fe6b9SJerome Glisse 
9290c195119SAlex Deucher /**
9300c195119SAlex Deucher  * cail_ioreg_write - write IO register
9310c195119SAlex Deucher  *
9320c195119SAlex Deucher  * @info: atom card_info pointer
9330c195119SAlex Deucher  * @reg: IO register offset
9340c195119SAlex Deucher  * @val: value to write to the pll register
9350c195119SAlex Deucher  *
9360c195119SAlex Deucher  * Provides a IO register accessor for the atom interpreter (r4xx+).
9370c195119SAlex Deucher  */
938351a52a2SAlex Deucher static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
939351a52a2SAlex Deucher {
940351a52a2SAlex Deucher 	struct radeon_device *rdev = info->dev->dev_private;
941351a52a2SAlex Deucher 
942351a52a2SAlex Deucher 	WREG32_IO(reg*4, val);
943351a52a2SAlex Deucher }
944351a52a2SAlex Deucher 
9450c195119SAlex Deucher /**
9460c195119SAlex Deucher  * cail_ioreg_read - read IO register
9470c195119SAlex Deucher  *
9480c195119SAlex Deucher  * @info: atom card_info pointer
9490c195119SAlex Deucher  * @reg: IO register offset
9500c195119SAlex Deucher  *
9510c195119SAlex Deucher  * Provides an IO register accessor for the atom interpreter (r4xx+).
9520c195119SAlex Deucher  * Returns the value of the IO register.
9530c195119SAlex Deucher  */
954351a52a2SAlex Deucher static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
955351a52a2SAlex Deucher {
956351a52a2SAlex Deucher 	struct radeon_device *rdev = info->dev->dev_private;
957351a52a2SAlex Deucher 	uint32_t r;
958351a52a2SAlex Deucher 
959351a52a2SAlex Deucher 	r = RREG32_IO(reg*4);
960351a52a2SAlex Deucher 	return r;
961351a52a2SAlex Deucher }
962351a52a2SAlex Deucher 
9630c195119SAlex Deucher /**
9640c195119SAlex Deucher  * radeon_atombios_init - init the driver info and callbacks for atombios
9650c195119SAlex Deucher  *
9660c195119SAlex Deucher  * @rdev: radeon_device pointer
9670c195119SAlex Deucher  *
9680c195119SAlex Deucher  * Initializes the driver info and register access callbacks for the
9690c195119SAlex Deucher  * ATOM interpreter (r4xx+).
9700c195119SAlex Deucher  * Returns 0 on sucess, -ENOMEM on failure.
9710c195119SAlex Deucher  * Called at driver startup.
9720c195119SAlex Deucher  */
973771fe6b9SJerome Glisse int radeon_atombios_init(struct radeon_device *rdev)
974771fe6b9SJerome Glisse {
97561c4b24bSMathias Fröhlich 	struct card_info *atom_card_info =
97661c4b24bSMathias Fröhlich 	    kzalloc(sizeof(struct card_info), GFP_KERNEL);
97761c4b24bSMathias Fröhlich 
97861c4b24bSMathias Fröhlich 	if (!atom_card_info)
97961c4b24bSMathias Fröhlich 		return -ENOMEM;
98061c4b24bSMathias Fröhlich 
98161c4b24bSMathias Fröhlich 	rdev->mode_info.atom_card_info = atom_card_info;
98261c4b24bSMathias Fröhlich 	atom_card_info->dev = rdev->ddev;
98361c4b24bSMathias Fröhlich 	atom_card_info->reg_read = cail_reg_read;
98461c4b24bSMathias Fröhlich 	atom_card_info->reg_write = cail_reg_write;
985351a52a2SAlex Deucher 	/* needed for iio ops */
986351a52a2SAlex Deucher 	if (rdev->rio_mem) {
987351a52a2SAlex Deucher 		atom_card_info->ioreg_read = cail_ioreg_read;
988351a52a2SAlex Deucher 		atom_card_info->ioreg_write = cail_ioreg_write;
989351a52a2SAlex Deucher 	} else {
990351a52a2SAlex Deucher 		DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
991351a52a2SAlex Deucher 		atom_card_info->ioreg_read = cail_reg_read;
992351a52a2SAlex Deucher 		atom_card_info->ioreg_write = cail_reg_write;
993351a52a2SAlex Deucher 	}
99461c4b24bSMathias Fröhlich 	atom_card_info->mc_read = cail_mc_read;
99561c4b24bSMathias Fröhlich 	atom_card_info->mc_write = cail_mc_write;
99661c4b24bSMathias Fröhlich 	atom_card_info->pll_read = cail_pll_read;
99761c4b24bSMathias Fröhlich 	atom_card_info->pll_write = cail_pll_write;
99861c4b24bSMathias Fröhlich 
99961c4b24bSMathias Fröhlich 	rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
10000e34d094STim Gardner 	if (!rdev->mode_info.atom_context) {
10010e34d094STim Gardner 		radeon_atombios_fini(rdev);
10020e34d094STim Gardner 		return -ENOMEM;
10030e34d094STim Gardner 	}
10040e34d094STim Gardner 
1005c31ad97fSRafał Miłecki 	mutex_init(&rdev->mode_info.atom_context->mutex);
10061c949842SDave Airlie 	mutex_init(&rdev->mode_info.atom_context->scratch_mutex);
1007771fe6b9SJerome Glisse 	radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
1008d904ef9bSDave Airlie 	atom_allocate_fb_scratch(rdev->mode_info.atom_context);
1009771fe6b9SJerome Glisse 	return 0;
1010771fe6b9SJerome Glisse }
1011771fe6b9SJerome Glisse 
10120c195119SAlex Deucher /**
10130c195119SAlex Deucher  * radeon_atombios_fini - free the driver info and callbacks for atombios
10140c195119SAlex Deucher  *
10150c195119SAlex Deucher  * @rdev: radeon_device pointer
10160c195119SAlex Deucher  *
10170c195119SAlex Deucher  * Frees the driver info and register access callbacks for the ATOM
10180c195119SAlex Deucher  * interpreter (r4xx+).
10190c195119SAlex Deucher  * Called at driver shutdown.
10200c195119SAlex Deucher  */
1021771fe6b9SJerome Glisse void radeon_atombios_fini(struct radeon_device *rdev)
1022771fe6b9SJerome Glisse {
10234a04a844SJerome Glisse 	if (rdev->mode_info.atom_context) {
1024d904ef9bSDave Airlie 		kfree(rdev->mode_info.atom_context->scratch);
10254a04a844SJerome Glisse 	}
10260e34d094STim Gardner 	kfree(rdev->mode_info.atom_context);
10270e34d094STim Gardner 	rdev->mode_info.atom_context = NULL;
102861c4b24bSMathias Fröhlich 	kfree(rdev->mode_info.atom_card_info);
10290e34d094STim Gardner 	rdev->mode_info.atom_card_info = NULL;
1030771fe6b9SJerome Glisse }
1031771fe6b9SJerome Glisse 
10320c195119SAlex Deucher /* COMBIOS */
10330c195119SAlex Deucher /*
10340c195119SAlex Deucher  * COMBIOS is the bios format prior to ATOM. It provides
10350c195119SAlex Deucher  * command tables similar to ATOM, but doesn't have a unified
10360c195119SAlex Deucher  * parser.  See radeon_combios.c
10370c195119SAlex Deucher  */
10380c195119SAlex Deucher 
10390c195119SAlex Deucher /**
10400c195119SAlex Deucher  * radeon_combios_init - init the driver info for combios
10410c195119SAlex Deucher  *
10420c195119SAlex Deucher  * @rdev: radeon_device pointer
10430c195119SAlex Deucher  *
10440c195119SAlex Deucher  * Initializes the driver info for combios (r1xx-r3xx).
10450c195119SAlex Deucher  * Returns 0 on sucess.
10460c195119SAlex Deucher  * Called at driver startup.
10470c195119SAlex Deucher  */
1048771fe6b9SJerome Glisse int radeon_combios_init(struct radeon_device *rdev)
1049771fe6b9SJerome Glisse {
1050771fe6b9SJerome Glisse 	radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
1051771fe6b9SJerome Glisse 	return 0;
1052771fe6b9SJerome Glisse }
1053771fe6b9SJerome Glisse 
10540c195119SAlex Deucher /**
10550c195119SAlex Deucher  * radeon_combios_fini - free the driver info for combios
10560c195119SAlex Deucher  *
10570c195119SAlex Deucher  * @rdev: radeon_device pointer
10580c195119SAlex Deucher  *
10590c195119SAlex Deucher  * Frees the driver info for combios (r1xx-r3xx).
10600c195119SAlex Deucher  * Called at driver shutdown.
10610c195119SAlex Deucher  */
1062771fe6b9SJerome Glisse void radeon_combios_fini(struct radeon_device *rdev)
1063771fe6b9SJerome Glisse {
1064771fe6b9SJerome Glisse }
1065771fe6b9SJerome Glisse 
10660c195119SAlex Deucher /* if we get transitioned to only one device, take VGA back */
10670c195119SAlex Deucher /**
10680c195119SAlex Deucher  * radeon_vga_set_decode - enable/disable vga decode
10690c195119SAlex Deucher  *
10700c195119SAlex Deucher  * @cookie: radeon_device pointer
10710c195119SAlex Deucher  * @state: enable/disable vga decode
10720c195119SAlex Deucher  *
10730c195119SAlex Deucher  * Enable/disable vga decode (all asics).
10740c195119SAlex Deucher  * Returns VGA resource flags.
10750c195119SAlex Deucher  */
107628d52043SDave Airlie static unsigned int radeon_vga_set_decode(void *cookie, bool state)
107728d52043SDave Airlie {
107828d52043SDave Airlie 	struct radeon_device *rdev = cookie;
107928d52043SDave Airlie 	radeon_vga_set_state(rdev, state);
108028d52043SDave Airlie 	if (state)
108128d52043SDave Airlie 		return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
108228d52043SDave Airlie 		       VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
108328d52043SDave Airlie 	else
108428d52043SDave Airlie 		return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
108528d52043SDave Airlie }
1086c1176d6fSDave Airlie 
10870c195119SAlex Deucher /**
10881bcb04f7SChristian König  * radeon_check_pot_argument - check that argument is a power of two
10891bcb04f7SChristian König  *
10901bcb04f7SChristian König  * @arg: value to check
10911bcb04f7SChristian König  *
10921bcb04f7SChristian König  * Validates that a certain argument is a power of two (all asics).
10931bcb04f7SChristian König  * Returns true if argument is valid.
10941bcb04f7SChristian König  */
10951bcb04f7SChristian König static bool radeon_check_pot_argument(int arg)
10961bcb04f7SChristian König {
10971bcb04f7SChristian König 	return (arg & (arg - 1)) == 0;
10981bcb04f7SChristian König }
10991bcb04f7SChristian König 
11001bcb04f7SChristian König /**
1101d3da76aaSLee Jones  * radeon_gart_size_auto - Determine a sensible default GART size
1102d3da76aaSLee Jones  *                         according to ASIC family.
11035e3c4f90SGrigori Goronzy  *
1104f017853eSLee Jones  * @family: ASIC family name
11055e3c4f90SGrigori Goronzy  */
11065e3c4f90SGrigori Goronzy static int radeon_gart_size_auto(enum radeon_family family)
11075e3c4f90SGrigori Goronzy {
11085e3c4f90SGrigori Goronzy 	/* default to a larger gart size on newer asics */
11095e3c4f90SGrigori Goronzy 	if (family >= CHIP_TAHITI)
11105e3c4f90SGrigori Goronzy 		return 2048;
11115e3c4f90SGrigori Goronzy 	else if (family >= CHIP_RV770)
11125e3c4f90SGrigori Goronzy 		return 1024;
11135e3c4f90SGrigori Goronzy 	else
11145e3c4f90SGrigori Goronzy 		return 512;
11155e3c4f90SGrigori Goronzy }
11165e3c4f90SGrigori Goronzy 
11175e3c4f90SGrigori Goronzy /**
11180c195119SAlex Deucher  * radeon_check_arguments - validate module params
11190c195119SAlex Deucher  *
11200c195119SAlex Deucher  * @rdev: radeon_device pointer
11210c195119SAlex Deucher  *
11220c195119SAlex Deucher  * Validates certain module parameters and updates
11230c195119SAlex Deucher  * the associated values used by the driver (all asics).
11240c195119SAlex Deucher  */
11251109ca09SLauri Kasanen static void radeon_check_arguments(struct radeon_device *rdev)
112636421338SJerome Glisse {
112736421338SJerome Glisse 	/* vramlimit must be a power of two */
11281bcb04f7SChristian König 	if (!radeon_check_pot_argument(radeon_vram_limit)) {
112936421338SJerome Glisse 		dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n",
113036421338SJerome Glisse 				radeon_vram_limit);
113136421338SJerome Glisse 		radeon_vram_limit = 0;
113236421338SJerome Glisse 	}
11331bcb04f7SChristian König 
1134edcd26e8SAlex Deucher 	if (radeon_gart_size == -1) {
11355e3c4f90SGrigori Goronzy 		radeon_gart_size = radeon_gart_size_auto(rdev->family);
1136edcd26e8SAlex Deucher 	}
113736421338SJerome Glisse 	/* gtt size must be power of two and greater or equal to 32M */
11381bcb04f7SChristian König 	if (radeon_gart_size < 32) {
1139edcd26e8SAlex Deucher 		dev_warn(rdev->dev, "gart size (%d) too small\n",
114036421338SJerome Glisse 				radeon_gart_size);
11415e3c4f90SGrigori Goronzy 		radeon_gart_size = radeon_gart_size_auto(rdev->family);
11421bcb04f7SChristian König 	} else if (!radeon_check_pot_argument(radeon_gart_size)) {
114336421338SJerome Glisse 		dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
114436421338SJerome Glisse 				radeon_gart_size);
11455e3c4f90SGrigori Goronzy 		radeon_gart_size = radeon_gart_size_auto(rdev->family);
114636421338SJerome Glisse 	}
11471bcb04f7SChristian König 	rdev->mc.gtt_size = (uint64_t)radeon_gart_size << 20;
11481bcb04f7SChristian König 
114936421338SJerome Glisse 	/* AGP mode can only be -1, 1, 2, 4, 8 */
115036421338SJerome Glisse 	switch (radeon_agpmode) {
115136421338SJerome Glisse 	case -1:
115236421338SJerome Glisse 	case 0:
115336421338SJerome Glisse 	case 1:
115436421338SJerome Glisse 	case 2:
115536421338SJerome Glisse 	case 4:
115636421338SJerome Glisse 	case 8:
115736421338SJerome Glisse 		break;
115836421338SJerome Glisse 	default:
115936421338SJerome Glisse 		dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
116036421338SJerome Glisse 				"-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
116136421338SJerome Glisse 		radeon_agpmode = 0;
116236421338SJerome Glisse 		break;
116336421338SJerome Glisse 	}
1164c1c44132SChristian König 
1165c1c44132SChristian König 	if (!radeon_check_pot_argument(radeon_vm_size)) {
1166c1c44132SChristian König 		dev_warn(rdev->dev, "VM size (%d) must be a power of 2\n",
1167c1c44132SChristian König 			 radeon_vm_size);
116820b2656dSChristian König 		radeon_vm_size = 4;
1169c1c44132SChristian König 	}
1170c1c44132SChristian König 
117120b2656dSChristian König 	if (radeon_vm_size < 1) {
117213c240efSAlexandre Demers 		dev_warn(rdev->dev, "VM size (%d) too small, min is 1GB\n",
1173c1c44132SChristian König 			 radeon_vm_size);
117420b2656dSChristian König 		radeon_vm_size = 4;
1175c1c44132SChristian König 	}
1176c1c44132SChristian König 
1177c1c44132SChristian König 	/*
1178c1c44132SChristian König 	 * Max GPUVM size for Cayman, SI and CI are 40 bits.
1179c1c44132SChristian König 	 */
118020b2656dSChristian König 	if (radeon_vm_size > 1024) {
118120b2656dSChristian König 		dev_warn(rdev->dev, "VM size (%d) too large, max is 1TB\n",
1182c1c44132SChristian König 			 radeon_vm_size);
118320b2656dSChristian König 		radeon_vm_size = 4;
1184c1c44132SChristian König 	}
11854510fb98SChristian König 
11864510fb98SChristian König 	/* defines number of bits in page table versus page directory,
11874510fb98SChristian König 	 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
11884510fb98SChristian König 	 * page table and the remaining bits are in the page directory */
1189dfc230f9SChristian König 	if (radeon_vm_block_size == -1) {
1190dfc230f9SChristian König 
1191dfc230f9SChristian König 		/* Total bits covered by PD + PTs */
11928e66e134SAlex Deucher 		unsigned bits = ilog2(radeon_vm_size) + 18;
1193dfc230f9SChristian König 
1194dfc230f9SChristian König 		/* Make sure the PD is 4K in size up to 8GB address space.
1195dfc230f9SChristian König 		   Above that split equal between PD and PTs */
1196dfc230f9SChristian König 		if (radeon_vm_size <= 8)
1197dfc230f9SChristian König 			radeon_vm_block_size = bits - 9;
1198dfc230f9SChristian König 		else
1199dfc230f9SChristian König 			radeon_vm_block_size = (bits + 3) / 2;
1200dfc230f9SChristian König 
1201dfc230f9SChristian König 	} else if (radeon_vm_block_size < 9) {
120220b2656dSChristian König 		dev_warn(rdev->dev, "VM page table size (%d) too small\n",
12034510fb98SChristian König 			 radeon_vm_block_size);
12044510fb98SChristian König 		radeon_vm_block_size = 9;
12054510fb98SChristian König 	}
12064510fb98SChristian König 
12074510fb98SChristian König 	if (radeon_vm_block_size > 24 ||
120820b2656dSChristian König 	    (radeon_vm_size * 1024) < (1ull << radeon_vm_block_size)) {
120920b2656dSChristian König 		dev_warn(rdev->dev, "VM page table size (%d) too large\n",
12104510fb98SChristian König 			 radeon_vm_block_size);
12114510fb98SChristian König 		radeon_vm_block_size = 9;
12124510fb98SChristian König 	}
121336421338SJerome Glisse }
121436421338SJerome Glisse 
12150c195119SAlex Deucher /**
12160c195119SAlex Deucher  * radeon_switcheroo_set_state - set switcheroo state
12170c195119SAlex Deucher  *
12180c195119SAlex Deucher  * @pdev: pci dev pointer
12198e5de1d8SLukas Wunner  * @state: vga_switcheroo state
12200c195119SAlex Deucher  *
12210c195119SAlex Deucher  * Callback for the switcheroo driver.  Suspends or resumes the
12220c195119SAlex Deucher  * the asics before or after it is powered up using ACPI methods.
12230c195119SAlex Deucher  */
12246a9ee8afSDave Airlie static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
12256a9ee8afSDave Airlie {
12266a9ee8afSDave Airlie 	struct drm_device *dev = pci_get_drvdata(pdev);
122710ebc0bcSDave Airlie 
122890c4cde9SAlex Deucher 	if (radeon_is_px(dev) && state == VGA_SWITCHEROO_OFF)
122910ebc0bcSDave Airlie 		return;
123010ebc0bcSDave Airlie 
12316a9ee8afSDave Airlie 	if (state == VGA_SWITCHEROO_ON) {
12327ca85295SJoe Perches 		pr_info("radeon: switched on\n");
12336a9ee8afSDave Airlie 		/* don't suspend or resume card normally */
12345bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1235d1f9809eSMaarten Lankhorst 
123610ebc0bcSDave Airlie 		radeon_resume_kms(dev, true, true);
1237d1f9809eSMaarten Lankhorst 
12385bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_ON;
1239fbf81762SDave Airlie 		drm_kms_helper_poll_enable(dev);
12406a9ee8afSDave Airlie 	} else {
12417ca85295SJoe Perches 		pr_info("radeon: switched off\n");
1242fbf81762SDave Airlie 		drm_kms_helper_poll_disable(dev);
12435bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1244274ad65cSJérome Glisse 		radeon_suspend_kms(dev, true, true, false);
12455bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_OFF;
12466a9ee8afSDave Airlie 	}
12476a9ee8afSDave Airlie }
12486a9ee8afSDave Airlie 
12490c195119SAlex Deucher /**
12500c195119SAlex Deucher  * radeon_switcheroo_can_switch - see if switcheroo state can change
12510c195119SAlex Deucher  *
12520c195119SAlex Deucher  * @pdev: pci dev pointer
12530c195119SAlex Deucher  *
12540c195119SAlex Deucher  * Callback for the switcheroo driver.  Check of the switcheroo
12550c195119SAlex Deucher  * state can be changed.
12560c195119SAlex Deucher  * Returns true if the state can be changed, false if not.
12570c195119SAlex Deucher  */
12586a9ee8afSDave Airlie static bool radeon_switcheroo_can_switch(struct pci_dev *pdev)
12596a9ee8afSDave Airlie {
12606a9ee8afSDave Airlie 	struct drm_device *dev = pci_get_drvdata(pdev);
12616a9ee8afSDave Airlie 
1262fc8fd40eSDaniel Vetter 	/*
1263fc8fd40eSDaniel Vetter 	 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1264fc8fd40eSDaniel Vetter 	 * locking inversion with the driver load path. And the access here is
1265fc8fd40eSDaniel Vetter 	 * completely racy anyway. So don't bother with locking for now.
1266fc8fd40eSDaniel Vetter 	 */
12677e13ad89SChris Wilson 	return atomic_read(&dev->open_count) == 0;
12686a9ee8afSDave Airlie }
12696a9ee8afSDave Airlie 
127026ec685fSTakashi Iwai static const struct vga_switcheroo_client_ops radeon_switcheroo_ops = {
127126ec685fSTakashi Iwai 	.set_gpu_state = radeon_switcheroo_set_state,
127226ec685fSTakashi Iwai 	.reprobe = NULL,
127326ec685fSTakashi Iwai 	.can_switch = radeon_switcheroo_can_switch,
127426ec685fSTakashi Iwai };
12756a9ee8afSDave Airlie 
12760c195119SAlex Deucher /**
12770c195119SAlex Deucher  * radeon_device_init - initialize the driver
12780c195119SAlex Deucher  *
12790c195119SAlex Deucher  * @rdev: radeon_device pointer
1280f017853eSLee Jones  * @ddev: drm dev pointer
12810c195119SAlex Deucher  * @pdev: pci dev pointer
12820c195119SAlex Deucher  * @flags: driver flags
12830c195119SAlex Deucher  *
12840c195119SAlex Deucher  * Initializes the driver info and hw (all asics).
12850c195119SAlex Deucher  * Returns 0 for success or an error on failure.
12860c195119SAlex Deucher  * Called at driver startup.
12870c195119SAlex Deucher  */
1288771fe6b9SJerome Glisse int radeon_device_init(struct radeon_device *rdev,
1289771fe6b9SJerome Glisse 		       struct drm_device *ddev,
1290771fe6b9SJerome Glisse 		       struct pci_dev *pdev,
1291771fe6b9SJerome Glisse 		       uint32_t flags)
1292771fe6b9SJerome Glisse {
1293351a52a2SAlex Deucher 	int r, i;
1294ad49f501SDave Airlie 	int dma_bits;
129510ebc0bcSDave Airlie 	bool runtime = false;
1296771fe6b9SJerome Glisse 
1297771fe6b9SJerome Glisse 	rdev->shutdown = false;
12989f022ddfSJerome Glisse 	rdev->dev = &pdev->dev;
1299771fe6b9SJerome Glisse 	rdev->ddev = ddev;
1300771fe6b9SJerome Glisse 	rdev->pdev = pdev;
1301771fe6b9SJerome Glisse 	rdev->flags = flags;
1302771fe6b9SJerome Glisse 	rdev->family = flags & RADEON_FAMILY_MASK;
1303771fe6b9SJerome Glisse 	rdev->is_atom_bios = false;
1304771fe6b9SJerome Glisse 	rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
1305edcd26e8SAlex Deucher 	rdev->mc.gtt_size = 512 * 1024 * 1024;
1306733289c2SJerome Glisse 	rdev->accel_working = false;
13078b25ed34SAlex Deucher 	/* set up ring ids */
13088b25ed34SAlex Deucher 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
13098b25ed34SAlex Deucher 		rdev->ring[i].idx = i;
13108b25ed34SAlex Deucher 	}
1311f54d1867SChris Wilson 	rdev->fence_context = dma_fence_context_alloc(RADEON_NUM_RINGS);
13121b5331d9SJerome Glisse 
1313fe0d36e0SAlex Deucher 	DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1314d522d9ccSThomas Reim 		 radeon_family_name[rdev->family], pdev->vendor, pdev->device,
1315fe0d36e0SAlex Deucher 		 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
13161b5331d9SJerome Glisse 
1317771fe6b9SJerome Glisse 	/* mutex initialization are all done here so we
1318771fe6b9SJerome Glisse 	 * can recall function without having locking issues */
1319d6999bc7SChristian König 	mutex_init(&rdev->ring_lock);
132040bacf16SAlex Deucher 	mutex_init(&rdev->dc_hw_i2c_mutex);
1321c20dc369SChristian Koenig 	atomic_set(&rdev->ih.lock, 0);
13224c788679SJerome Glisse 	mutex_init(&rdev->gem.mutex);
1323c913e23aSRafał Miłecki 	mutex_init(&rdev->pm.mutex);
13246759a0a7SMarek Olšák 	mutex_init(&rdev->gpu_clock_mutex);
1325f61d5b46SAlex Deucher 	mutex_init(&rdev->srbm_mutex);
1326db7fce39SChristian König 	init_rwsem(&rdev->pm.mclk_lock);
1327dee53e7fSJerome Glisse 	init_rwsem(&rdev->exclusive_lock);
132873a6d3fcSRafał Miłecki 	init_waitqueue_head(&rdev->irq.vblank_queue);
13291b9c3dd0SAlex Deucher 	r = radeon_gem_init(rdev);
13301b9c3dd0SAlex Deucher 	if (r)
13311b9c3dd0SAlex Deucher 		return r;
1332529364e0SChristian König 
1333c1c44132SChristian König 	radeon_check_arguments(rdev);
133423d4f1f2SAlex Deucher 	/* Adjust VM size here.
1335c1c44132SChristian König 	 * Max GPUVM size for cayman+ is 40 bits.
133623d4f1f2SAlex Deucher 	 */
133720b2656dSChristian König 	rdev->vm_manager.max_pfn = radeon_vm_size << 18;
1338771fe6b9SJerome Glisse 
13394aac0473SJerome Glisse 	/* Set asic functions */
13404aac0473SJerome Glisse 	r = radeon_asic_init(rdev);
134136421338SJerome Glisse 	if (r)
13424aac0473SJerome Glisse 		return r;
13434aac0473SJerome Glisse 
1344f95df9caSAlex Deucher 	/* all of the newer IGP chips have an internal gart
1345f95df9caSAlex Deucher 	 * However some rs4xx report as AGP, so remove that here.
1346f95df9caSAlex Deucher 	 */
1347f95df9caSAlex Deucher 	if ((rdev->family >= CHIP_RS400) &&
1348f95df9caSAlex Deucher 	    (rdev->flags & RADEON_IS_IGP)) {
1349f95df9caSAlex Deucher 		rdev->flags &= ~RADEON_IS_AGP;
1350f95df9caSAlex Deucher 	}
1351f95df9caSAlex Deucher 
135230256a3fSJerome Glisse 	if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
1353b574f251SJerome Glisse 		radeon_agp_disable(rdev);
1354771fe6b9SJerome Glisse 	}
1355771fe6b9SJerome Glisse 
13569ed8b1f9SAlex Deucher 	/* Set the internal MC address mask
13579ed8b1f9SAlex Deucher 	 * This is the max address of the GPU's
13589ed8b1f9SAlex Deucher 	 * internal address space.
13599ed8b1f9SAlex Deucher 	 */
13609ed8b1f9SAlex Deucher 	if (rdev->family >= CHIP_CAYMAN)
13619ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xffffffffffULL; /* 40 bit MC */
13629ed8b1f9SAlex Deucher 	else if (rdev->family >= CHIP_CEDAR)
13639ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xfffffffffULL; /* 36 bit MC */
13649ed8b1f9SAlex Deucher 	else
13659ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xffffffffULL; /* 32 bit MC */
13669ed8b1f9SAlex Deucher 
136733b3ad37SChristoph Hellwig 	/* set DMA mask.
1368ad49f501SDave Airlie 	 * PCIE - can handle 40-bits.
1369005a83f1SAlex Deucher 	 * IGP - can handle 40-bits
1370ad49f501SDave Airlie 	 * AGP - generally dma32 is safest
1371005a83f1SAlex Deucher 	 * PCI - dma32 for legacy pci gart, 40 bits on newer asics
1372ad49f501SDave Airlie 	 */
137333b3ad37SChristoph Hellwig 	dma_bits = 40;
1374ad49f501SDave Airlie 	if (rdev->flags & RADEON_IS_AGP)
137533b3ad37SChristoph Hellwig 		dma_bits = 32;
1376005a83f1SAlex Deucher 	if ((rdev->flags & RADEON_IS_PCI) &&
13774a2b6662SJerome Glisse 	    (rdev->family <= CHIP_RS740))
137833b3ad37SChristoph Hellwig 		dma_bits = 32;
1379bcb0b981SBen Crocker #ifdef CONFIG_PPC64
1380bcb0b981SBen Crocker 	if (rdev->family == CHIP_CEDAR)
138133b3ad37SChristoph Hellwig 		dma_bits = 32;
1382bcb0b981SBen Crocker #endif
1383ad49f501SDave Airlie 
138403127c58SChristoph Hellwig 	r = dma_set_mask_and_coherent(&rdev->pdev->dev, DMA_BIT_MASK(dma_bits));
1385771fe6b9SJerome Glisse 	if (r) {
13867ca85295SJoe Perches 		pr_warn("radeon: No suitable DMA available\n");
138703127c58SChristoph Hellwig 		return r;
1388c52494f6SKonrad Rzeszutek Wilk 	}
1389913b2cb7SMichael D Labriola 	rdev->need_swiotlb = drm_need_swiotlb(dma_bits);
1390771fe6b9SJerome Glisse 
1391771fe6b9SJerome Glisse 	/* Registers mapping */
1392771fe6b9SJerome Glisse 	/* TODO: block userspace mapping of io register */
13932c385151SDaniel Vetter 	spin_lock_init(&rdev->mmio_idx_lock);
1394fe78118cSAlex Deucher 	spin_lock_init(&rdev->smc_idx_lock);
13950a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pll_idx_lock);
13960a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->mc_idx_lock);
13970a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pcie_idx_lock);
13980a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pciep_idx_lock);
13990a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pif_idx_lock);
14000a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->cg_idx_lock);
14010a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->uvd_idx_lock);
14020a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->rcu_idx_lock);
14030a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->didt_idx_lock);
14040a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->end_idx_lock);
1405efad86dbSAlex Deucher 	if (rdev->family >= CHIP_BONAIRE) {
1406efad86dbSAlex Deucher 		rdev->rmmio_base = pci_resource_start(rdev->pdev, 5);
1407efad86dbSAlex Deucher 		rdev->rmmio_size = pci_resource_len(rdev->pdev, 5);
1408efad86dbSAlex Deucher 	} else {
140901d73a69SJordan Crouse 		rdev->rmmio_base = pci_resource_start(rdev->pdev, 2);
141001d73a69SJordan Crouse 		rdev->rmmio_size = pci_resource_len(rdev->pdev, 2);
1411efad86dbSAlex Deucher 	}
1412771fe6b9SJerome Glisse 	rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size);
1413a33c1a82SAndy Shevchenko 	if (rdev->rmmio == NULL)
1414771fe6b9SJerome Glisse 		return -ENOMEM;
1415771fe6b9SJerome Glisse 
141675efdee1SAlex Deucher 	/* doorbell bar mapping */
141775efdee1SAlex Deucher 	if (rdev->family >= CHIP_BONAIRE)
141875efdee1SAlex Deucher 		radeon_doorbell_init(rdev);
141975efdee1SAlex Deucher 
1420351a52a2SAlex Deucher 	/* io port mapping */
1421351a52a2SAlex Deucher 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1422351a52a2SAlex Deucher 		if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) {
1423351a52a2SAlex Deucher 			rdev->rio_mem_size = pci_resource_len(rdev->pdev, i);
1424351a52a2SAlex Deucher 			rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size);
1425351a52a2SAlex Deucher 			break;
1426351a52a2SAlex Deucher 		}
1427351a52a2SAlex Deucher 	}
1428351a52a2SAlex Deucher 	if (rdev->rio_mem == NULL)
1429351a52a2SAlex Deucher 		DRM_ERROR("Unable to find PCI I/O BAR\n");
1430351a52a2SAlex Deucher 
14314807c5a8SAlex Deucher 	if (rdev->flags & RADEON_IS_PX)
14324807c5a8SAlex Deucher 		radeon_device_handle_px_quirks(rdev);
14334807c5a8SAlex Deucher 
143428d52043SDave Airlie 	/* if we have > 1 VGA cards, then disable the radeon VGA resources */
143593239ea1SDave Airlie 	/* this will fail for cards that aren't VGA class devices, just
143693239ea1SDave Airlie 	 * ignore it */
143793239ea1SDave Airlie 	vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
143810ebc0bcSDave Airlie 
1439bfaddd9fSAlex Deucher 	if (rdev->flags & RADEON_IS_PX)
144010ebc0bcSDave Airlie 		runtime = true;
14417ffb0ce3SLukas Wunner 	if (!pci_is_thunderbolt_attached(rdev->pdev))
14427ffb0ce3SLukas Wunner 		vga_switcheroo_register_client(rdev->pdev,
14437ffb0ce3SLukas Wunner 					       &radeon_switcheroo_ops, runtime);
144410ebc0bcSDave Airlie 	if (runtime)
144510ebc0bcSDave Airlie 		vga_switcheroo_init_domain_pm_ops(rdev->dev, &rdev->vga_pm_domain);
144628d52043SDave Airlie 
14473ce0a23dSJerome Glisse 	r = radeon_init(rdev);
1448b574f251SJerome Glisse 	if (r)
14492e97140dSAlex Deucher 		goto failed;
1450b1e3a6d1SMichel Dänzer 
14515b54d679SNirmoy Das 	radeon_gem_debugfs_init(rdev);
14525b54d679SNirmoy Das 	radeon_mst_debugfs_init(rdev);
14539843ead0SDave Airlie 
1454b574f251SJerome Glisse 	if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
1455b574f251SJerome Glisse 		/* Acceleration not working on AGP card try again
1456b574f251SJerome Glisse 		 * with fallback to PCI or PCIE GART
1457b574f251SJerome Glisse 		 */
1458a2d07b74SJerome Glisse 		radeon_asic_reset(rdev);
1459b574f251SJerome Glisse 		radeon_fini(rdev);
1460b574f251SJerome Glisse 		radeon_agp_disable(rdev);
1461b574f251SJerome Glisse 		r = radeon_init(rdev);
14624aac0473SJerome Glisse 		if (r)
14632e97140dSAlex Deucher 			goto failed;
14643ce0a23dSJerome Glisse 	}
14656c7bcceaSAlex Deucher 
146613a7d299SChristian König 	r = radeon_ib_ring_tests(rdev);
146713a7d299SChristian König 	if (r)
146813a7d299SChristian König 		DRM_ERROR("ib ring test failed (%d).\n", r);
146913a7d299SChristian König 
14706dfd1972SJérôme Glisse 	/*
14716dfd1972SJérôme Glisse 	 * Turks/Thames GPU will freeze whole laptop if DPM is not restarted
14726dfd1972SJérôme Glisse 	 * after the CP ring have chew one packet at least. Hence here we stop
14736dfd1972SJérôme Glisse 	 * and restart DPM after the radeon_ib_ring_tests().
14746dfd1972SJérôme Glisse 	 */
14756dfd1972SJérôme Glisse 	if (rdev->pm.dpm_enabled &&
14766dfd1972SJérôme Glisse 	    (rdev->pm.pm_method == PM_METHOD_DPM) &&
14776dfd1972SJérôme Glisse 	    (rdev->family == CHIP_TURKS) &&
14786dfd1972SJérôme Glisse 	    (rdev->flags & RADEON_IS_MOBILITY)) {
14796dfd1972SJérôme Glisse 		mutex_lock(&rdev->pm.mutex);
14806dfd1972SJérôme Glisse 		radeon_dpm_disable(rdev);
14816dfd1972SJérôme Glisse 		radeon_dpm_enable(rdev);
14826dfd1972SJérôme Glisse 		mutex_unlock(&rdev->pm.mutex);
14836dfd1972SJérôme Glisse 	}
14846dfd1972SJérôme Glisse 
148560a7e396SChristian König 	if ((radeon_testing & 1)) {
14864a1132a0SAlex Deucher 		if (rdev->accel_working)
1487ecc0b326SMichel Dänzer 			radeon_test_moves(rdev);
14884a1132a0SAlex Deucher 		else
14894a1132a0SAlex Deucher 			DRM_INFO("radeon: acceleration disabled, skipping move tests\n");
1490ecc0b326SMichel Dänzer 	}
149160a7e396SChristian König 	if ((radeon_testing & 2)) {
14924a1132a0SAlex Deucher 		if (rdev->accel_working)
149360a7e396SChristian König 			radeon_test_syncing(rdev);
14944a1132a0SAlex Deucher 		else
14954a1132a0SAlex Deucher 			DRM_INFO("radeon: acceleration disabled, skipping sync tests\n");
149660a7e396SChristian König 	}
1497771fe6b9SJerome Glisse 	if (radeon_benchmarking) {
14984a1132a0SAlex Deucher 		if (rdev->accel_working)
1499638dd7dbSIlija Hadzic 			radeon_benchmark(rdev, radeon_benchmarking);
15004a1132a0SAlex Deucher 		else
15014a1132a0SAlex Deucher 			DRM_INFO("radeon: acceleration disabled, skipping benchmarks\n");
1502771fe6b9SJerome Glisse 	}
15036cf8a3f5SJerome Glisse 	return 0;
15042e97140dSAlex Deucher 
15052e97140dSAlex Deucher failed:
1506b8751946SLukas Wunner 	/* balance pm_runtime_get_sync() in radeon_driver_unload_kms() */
1507b8751946SLukas Wunner 	if (radeon_is_px(ddev))
1508b8751946SLukas Wunner 		pm_runtime_put_noidle(ddev->dev);
15092e97140dSAlex Deucher 	if (runtime)
15102e97140dSAlex Deucher 		vga_switcheroo_fini_domain_pm_ops(rdev->dev);
15112e97140dSAlex Deucher 	return r;
1512771fe6b9SJerome Glisse }
1513771fe6b9SJerome Glisse 
15140c195119SAlex Deucher /**
15150c195119SAlex Deucher  * radeon_device_fini - tear down the driver
15160c195119SAlex Deucher  *
15170c195119SAlex Deucher  * @rdev: radeon_device pointer
15180c195119SAlex Deucher  *
15190c195119SAlex Deucher  * Tear down the driver info (all asics).
15200c195119SAlex Deucher  * Called at driver shutdown.
15210c195119SAlex Deucher  */
1522771fe6b9SJerome Glisse void radeon_device_fini(struct radeon_device *rdev)
1523771fe6b9SJerome Glisse {
1524771fe6b9SJerome Glisse 	DRM_INFO("radeon: finishing device.\n");
1525771fe6b9SJerome Glisse 	rdev->shutdown = true;
152690aca4d2SJerome Glisse 	/* evict vram memory */
152790aca4d2SJerome Glisse 	radeon_bo_evict_vram(rdev);
15283ce0a23dSJerome Glisse 	radeon_fini(rdev);
15297ffb0ce3SLukas Wunner 	if (!pci_is_thunderbolt_attached(rdev->pdev))
15306a9ee8afSDave Airlie 		vga_switcheroo_unregister_client(rdev->pdev);
15312e97140dSAlex Deucher 	if (rdev->flags & RADEON_IS_PX)
15322e97140dSAlex Deucher 		vga_switcheroo_fini_domain_pm_ops(rdev->dev);
1533*b8779475SChristoph Hellwig 	vga_client_unregister(rdev->pdev);
1534e0a2ca73SAlex Deucher 	if (rdev->rio_mem)
1535351a52a2SAlex Deucher 		pci_iounmap(rdev->pdev, rdev->rio_mem);
1536351a52a2SAlex Deucher 	rdev->rio_mem = NULL;
1537771fe6b9SJerome Glisse 	iounmap(rdev->rmmio);
1538771fe6b9SJerome Glisse 	rdev->rmmio = NULL;
153975efdee1SAlex Deucher 	if (rdev->family >= CHIP_BONAIRE)
154075efdee1SAlex Deucher 		radeon_doorbell_fini(rdev);
1541771fe6b9SJerome Glisse }
1542771fe6b9SJerome Glisse 
1543771fe6b9SJerome Glisse 
1544771fe6b9SJerome Glisse /*
1545771fe6b9SJerome Glisse  * Suspend & resume.
1546771fe6b9SJerome Glisse  */
1547f017853eSLee Jones /*
15480c195119SAlex Deucher  * radeon_suspend_kms - initiate device suspend
15490c195119SAlex Deucher  *
15500c195119SAlex Deucher  * Puts the hw in the suspend state (all asics).
15510c195119SAlex Deucher  * Returns 0 for success or an error on failure.
15520c195119SAlex Deucher  * Called at driver suspend.
15530c195119SAlex Deucher  */
1554274ad65cSJérome Glisse int radeon_suspend_kms(struct drm_device *dev, bool suspend,
1555274ad65cSJérome Glisse 		       bool fbcon, bool freeze)
1556771fe6b9SJerome Glisse {
1557875c1866SDarren Jenkins 	struct radeon_device *rdev;
1558d86a4126SThomas Zimmermann 	struct pci_dev *pdev;
1559771fe6b9SJerome Glisse 	struct drm_crtc *crtc;
1560d8dcaa1dSAlex Deucher 	struct drm_connector *connector;
15617465280cSAlex Deucher 	int i, r;
1562771fe6b9SJerome Glisse 
1563875c1866SDarren Jenkins 	if (dev == NULL || dev->dev_private == NULL) {
1564771fe6b9SJerome Glisse 		return -ENODEV;
1565771fe6b9SJerome Glisse 	}
15667473e830SDave Airlie 
1567875c1866SDarren Jenkins 	rdev = dev->dev_private;
1568d86a4126SThomas Zimmermann 	pdev = to_pci_dev(dev->dev);
1569875c1866SDarren Jenkins 
1570f2aba352SAlex Deucher 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
15716a9ee8afSDave Airlie 		return 0;
1572d8dcaa1dSAlex Deucher 
157386698c20SSeth Forshee 	drm_kms_helper_poll_disable(dev);
157486698c20SSeth Forshee 
15756adaed5bSDaniel Vetter 	drm_modeset_lock_all(dev);
1576d8dcaa1dSAlex Deucher 	/* turn off display hw */
1577d8dcaa1dSAlex Deucher 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1578d8dcaa1dSAlex Deucher 		drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1579d8dcaa1dSAlex Deucher 	}
15806adaed5bSDaniel Vetter 	drm_modeset_unlock_all(dev);
1581d8dcaa1dSAlex Deucher 
1582f3cbb17bSGrigori Goronzy 	/* unpin the front buffers and cursors */
1583771fe6b9SJerome Glisse 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1584f3cbb17bSGrigori Goronzy 		struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
15859a0f0c9dSDaniel Stone 		struct drm_framebuffer *fb = crtc->primary->fb;
15864c788679SJerome Glisse 		struct radeon_bo *robj;
1587771fe6b9SJerome Glisse 
1588f3cbb17bSGrigori Goronzy 		if (radeon_crtc->cursor_bo) {
1589f3cbb17bSGrigori Goronzy 			struct radeon_bo *robj = gem_to_radeon_bo(radeon_crtc->cursor_bo);
1590f3cbb17bSGrigori Goronzy 			r = radeon_bo_reserve(robj, false);
1591f3cbb17bSGrigori Goronzy 			if (r == 0) {
1592f3cbb17bSGrigori Goronzy 				radeon_bo_unpin(robj);
1593f3cbb17bSGrigori Goronzy 				radeon_bo_unreserve(robj);
1594f3cbb17bSGrigori Goronzy 			}
1595f3cbb17bSGrigori Goronzy 		}
1596f3cbb17bSGrigori Goronzy 
15979a0f0c9dSDaniel Stone 		if (fb == NULL || fb->obj[0] == NULL) {
1598771fe6b9SJerome Glisse 			continue;
1599771fe6b9SJerome Glisse 		}
16009a0f0c9dSDaniel Stone 		robj = gem_to_radeon_bo(fb->obj[0]);
160138651674SDave Airlie 		/* don't unpin kernel fb objects */
160238651674SDave Airlie 		if (!radeon_fbdev_robj_is_fb(rdev, robj)) {
16034c788679SJerome Glisse 			r = radeon_bo_reserve(robj, false);
160438651674SDave Airlie 			if (r == 0) {
16054c788679SJerome Glisse 				radeon_bo_unpin(robj);
16064c788679SJerome Glisse 				radeon_bo_unreserve(robj);
16074c788679SJerome Glisse 			}
1608771fe6b9SJerome Glisse 		}
1609771fe6b9SJerome Glisse 	}
1610771fe6b9SJerome Glisse 	/* evict vram memory */
16114c788679SJerome Glisse 	radeon_bo_evict_vram(rdev);
16128a47cc9eSChristian König 
1613771fe6b9SJerome Glisse 	/* wait for gpu to finish processing current batch */
16145f8f635eSJerome Glisse 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
161537615527SChristian König 		r = radeon_fence_wait_empty(rdev, i);
16165f8f635eSJerome Glisse 		if (r) {
16175f8f635eSJerome Glisse 			/* delay GPU reset to resume */
1618eb98c709SChristian König 			radeon_fence_driver_force_completion(rdev, i);
16195f8f635eSJerome Glisse 		}
16205f8f635eSJerome Glisse 	}
1621771fe6b9SJerome Glisse 
1622f657c2a7SYang Zhao 	radeon_save_bios_scratch_regs(rdev);
1623f657c2a7SYang Zhao 
16243ce0a23dSJerome Glisse 	radeon_suspend(rdev);
1625d4877cf2SAlex Deucher 	radeon_hpd_fini(rdev);
1626ec9aaaffSAlex Deucher 	/* evict remaining vram memory
1627ec9aaaffSAlex Deucher 	 * This second call to evict vram is to evict the gart page table
1628ec9aaaffSAlex Deucher 	 * using the CPU.
1629ec9aaaffSAlex Deucher 	 */
16304c788679SJerome Glisse 	radeon_bo_evict_vram(rdev);
1631771fe6b9SJerome Glisse 
163210b06122SJerome Glisse 	radeon_agp_suspend(rdev);
163310b06122SJerome Glisse 
1634d86a4126SThomas Zimmermann 	pci_save_state(pdev);
163582060854SAlex Deucher 	if (freeze && rdev->family >= CHIP_CEDAR && !(rdev->flags & RADEON_IS_IGP)) {
1636274ad65cSJérome Glisse 		rdev->asic->asic_reset(rdev, true);
1637d86a4126SThomas Zimmermann 		pci_restore_state(pdev);
1638274ad65cSJérome Glisse 	} else if (suspend) {
1639771fe6b9SJerome Glisse 		/* Shut down the device */
1640d86a4126SThomas Zimmermann 		pci_disable_device(pdev);
1641d86a4126SThomas Zimmermann 		pci_set_power_state(pdev, PCI_D3hot);
1642771fe6b9SJerome Glisse 	}
164310ebc0bcSDave Airlie 
164410ebc0bcSDave Airlie 	if (fbcon) {
1645ac751efaSTorben Hohn 		console_lock();
164638651674SDave Airlie 		radeon_fbdev_set_suspend(rdev, 1);
1647ac751efaSTorben Hohn 		console_unlock();
164810ebc0bcSDave Airlie 	}
1649771fe6b9SJerome Glisse 	return 0;
1650771fe6b9SJerome Glisse }
1651771fe6b9SJerome Glisse 
1652f017853eSLee Jones /*
16530c195119SAlex Deucher  * radeon_resume_kms - initiate device resume
16540c195119SAlex Deucher  *
16550c195119SAlex Deucher  * Bring the hw back to operating state (all asics).
16560c195119SAlex Deucher  * Returns 0 for success or an error on failure.
16570c195119SAlex Deucher  * Called at driver resume.
16580c195119SAlex Deucher  */
165910ebc0bcSDave Airlie int radeon_resume_kms(struct drm_device *dev, bool resume, bool fbcon)
1660771fe6b9SJerome Glisse {
166109bdf591SCedric Godin 	struct drm_connector *connector;
1662771fe6b9SJerome Glisse 	struct radeon_device *rdev = dev->dev_private;
1663d86a4126SThomas Zimmermann 	struct pci_dev *pdev = to_pci_dev(dev->dev);
1664f3cbb17bSGrigori Goronzy 	struct drm_crtc *crtc;
166504eb2206SChristian König 	int r;
1666771fe6b9SJerome Glisse 
1667f2aba352SAlex Deucher 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
16686a9ee8afSDave Airlie 		return 0;
16696a9ee8afSDave Airlie 
167010ebc0bcSDave Airlie 	if (fbcon) {
1671ac751efaSTorben Hohn 		console_lock();
167210ebc0bcSDave Airlie 	}
16737473e830SDave Airlie 	if (resume) {
1674d86a4126SThomas Zimmermann 		pci_set_power_state(pdev, PCI_D0);
1675d86a4126SThomas Zimmermann 		pci_restore_state(pdev);
1676d86a4126SThomas Zimmermann 		if (pci_enable_device(pdev)) {
167710ebc0bcSDave Airlie 			if (fbcon)
1678ac751efaSTorben Hohn 				console_unlock();
1679771fe6b9SJerome Glisse 			return -1;
1680771fe6b9SJerome Glisse 		}
16817473e830SDave Airlie 	}
16820ebf1717SDave Airlie 	/* resume AGP if in use */
16830ebf1717SDave Airlie 	radeon_agp_resume(rdev);
16843ce0a23dSJerome Glisse 	radeon_resume(rdev);
168504eb2206SChristian König 
168604eb2206SChristian König 	r = radeon_ib_ring_tests(rdev);
168704eb2206SChristian König 	if (r)
168804eb2206SChristian König 		DRM_ERROR("ib ring test failed (%d).\n", r);
168904eb2206SChristian König 
1690bc6a6295SAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
16916c7bcceaSAlex Deucher 		/* do dpm late init */
16926c7bcceaSAlex Deucher 		r = radeon_pm_late_init(rdev);
16936c7bcceaSAlex Deucher 		if (r) {
16946c7bcceaSAlex Deucher 			rdev->pm.dpm_enabled = false;
16956c7bcceaSAlex Deucher 			DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
16966c7bcceaSAlex Deucher 		}
1697bc6a6295SAlex Deucher 	} else {
1698bc6a6295SAlex Deucher 		/* resume old pm late */
1699bc6a6295SAlex Deucher 		radeon_pm_resume(rdev);
17006c7bcceaSAlex Deucher 	}
17016c7bcceaSAlex Deucher 
1702f657c2a7SYang Zhao 	radeon_restore_bios_scratch_regs(rdev);
170309bdf591SCedric Godin 
1704f3cbb17bSGrigori Goronzy 	/* pin cursors */
1705f3cbb17bSGrigori Goronzy 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1706f3cbb17bSGrigori Goronzy 		struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1707f3cbb17bSGrigori Goronzy 
1708f3cbb17bSGrigori Goronzy 		if (radeon_crtc->cursor_bo) {
1709f3cbb17bSGrigori Goronzy 			struct radeon_bo *robj = gem_to_radeon_bo(radeon_crtc->cursor_bo);
1710f3cbb17bSGrigori Goronzy 			r = radeon_bo_reserve(robj, false);
1711f3cbb17bSGrigori Goronzy 			if (r == 0) {
1712f3cbb17bSGrigori Goronzy 				/* Only 27 bit offset for legacy cursor */
1713f3cbb17bSGrigori Goronzy 				r = radeon_bo_pin_restricted(robj,
1714f3cbb17bSGrigori Goronzy 							     RADEON_GEM_DOMAIN_VRAM,
1715f3cbb17bSGrigori Goronzy 							     ASIC_IS_AVIVO(rdev) ?
1716f3cbb17bSGrigori Goronzy 							     0 : 1 << 27,
1717f3cbb17bSGrigori Goronzy 							     &radeon_crtc->cursor_addr);
1718f3cbb17bSGrigori Goronzy 				if (r != 0)
1719f3cbb17bSGrigori Goronzy 					DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
1720f3cbb17bSGrigori Goronzy 				radeon_bo_unreserve(robj);
1721f3cbb17bSGrigori Goronzy 			}
1722f3cbb17bSGrigori Goronzy 		}
1723f3cbb17bSGrigori Goronzy 	}
1724f3cbb17bSGrigori Goronzy 
17253fa47d9eSAlex Deucher 	/* init dig PHYs, disp eng pll */
17263fa47d9eSAlex Deucher 	if (rdev->is_atom_bios) {
1727ac89af1eSAlex Deucher 		radeon_atom_encoder_init(rdev);
1728f3f1f03eSAlex Deucher 		radeon_atom_disp_eng_pll_init(rdev);
1729bced76f2SAlex Deucher 		/* turn on the BL */
1730bced76f2SAlex Deucher 		if (rdev->mode_info.bl_encoder) {
1731bced76f2SAlex Deucher 			u8 bl_level = radeon_get_backlight_level(rdev,
1732bced76f2SAlex Deucher 								 rdev->mode_info.bl_encoder);
1733bced76f2SAlex Deucher 			radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
1734bced76f2SAlex Deucher 						   bl_level);
1735bced76f2SAlex Deucher 		}
17363fa47d9eSAlex Deucher 	}
1737d4877cf2SAlex Deucher 	/* reset hpd state */
1738d4877cf2SAlex Deucher 	radeon_hpd_init(rdev);
1739771fe6b9SJerome Glisse 	/* blat the mode back in */
1740ec9954fcSDave Airlie 	if (fbcon) {
1741771fe6b9SJerome Glisse 		drm_helper_resume_force_mode(dev);
1742a93f344dSAlex Deucher 		/* turn on display hw */
17436adaed5bSDaniel Vetter 		drm_modeset_lock_all(dev);
1744a93f344dSAlex Deucher 		list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1745a93f344dSAlex Deucher 			drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
1746a93f344dSAlex Deucher 		}
17476adaed5bSDaniel Vetter 		drm_modeset_unlock_all(dev);
1748ec9954fcSDave Airlie 	}
174986698c20SSeth Forshee 
175086698c20SSeth Forshee 	drm_kms_helper_poll_enable(dev);
175118ee37a4SDaniel Vetter 
17523640da2fSAlex Deucher 	/* set the power state here in case we are a PX system or headless */
17533640da2fSAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled)
17543640da2fSAlex Deucher 		radeon_pm_compute_clocks(rdev);
17553640da2fSAlex Deucher 
175618ee37a4SDaniel Vetter 	if (fbcon) {
175718ee37a4SDaniel Vetter 		radeon_fbdev_set_suspend(rdev, 0);
175818ee37a4SDaniel Vetter 		console_unlock();
175918ee37a4SDaniel Vetter 	}
176018ee37a4SDaniel Vetter 
1761771fe6b9SJerome Glisse 	return 0;
1762771fe6b9SJerome Glisse }
1763771fe6b9SJerome Glisse 
17640c195119SAlex Deucher /**
17650c195119SAlex Deucher  * radeon_gpu_reset - reset the asic
17660c195119SAlex Deucher  *
17670c195119SAlex Deucher  * @rdev: radeon device pointer
17680c195119SAlex Deucher  *
17690c195119SAlex Deucher  * Attempt the reset the GPU if it has hung (all asics).
17700c195119SAlex Deucher  * Returns 0 for success or an error on failure.
17710c195119SAlex Deucher  */
177290aca4d2SJerome Glisse int radeon_gpu_reset(struct radeon_device *rdev)
177390aca4d2SJerome Glisse {
177455d7c221SChristian König 	unsigned ring_sizes[RADEON_NUM_RINGS];
177555d7c221SChristian König 	uint32_t *ring_data[RADEON_NUM_RINGS];
177655d7c221SChristian König 
177755d7c221SChristian König 	bool saved = false;
177855d7c221SChristian König 
177955d7c221SChristian König 	int i, r;
17808fd1b84cSDave Airlie 	int resched;
178190aca4d2SJerome Glisse 
1782dee53e7fSJerome Glisse 	down_write(&rdev->exclusive_lock);
1783f9eaf9aeSChristian König 
1784f9eaf9aeSChristian König 	if (!rdev->needs_reset) {
1785f9eaf9aeSChristian König 		up_write(&rdev->exclusive_lock);
1786f9eaf9aeSChristian König 		return 0;
1787f9eaf9aeSChristian König 	}
1788f9eaf9aeSChristian König 
178972b9076bSMarek Olšák 	atomic_inc(&rdev->gpu_reset_counter);
179072b9076bSMarek Olšák 
179190aca4d2SJerome Glisse 	radeon_save_bios_scratch_regs(rdev);
17928fd1b84cSDave Airlie 	/* block TTM */
17938fd1b84cSDave Airlie 	resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
179490aca4d2SJerome Glisse 	radeon_suspend(rdev);
179573ef0e0dSAlex Deucher 	radeon_hpd_fini(rdev);
179690aca4d2SJerome Glisse 
179755d7c221SChristian König 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
179855d7c221SChristian König 		ring_sizes[i] = radeon_ring_backup(rdev, &rdev->ring[i],
179955d7c221SChristian König 						   &ring_data[i]);
180055d7c221SChristian König 		if (ring_sizes[i]) {
180155d7c221SChristian König 			saved = true;
180255d7c221SChristian König 			dev_info(rdev->dev, "Saved %d dwords of commands "
180355d7c221SChristian König 				 "on ring %d.\n", ring_sizes[i], i);
180455d7c221SChristian König 		}
180555d7c221SChristian König 	}
180655d7c221SChristian König 
180790aca4d2SJerome Glisse 	r = radeon_asic_reset(rdev);
180890aca4d2SJerome Glisse 	if (!r) {
180955d7c221SChristian König 		dev_info(rdev->dev, "GPU reset succeeded, trying to resume\n");
181090aca4d2SJerome Glisse 		radeon_resume(rdev);
181155d7c221SChristian König 	}
181204eb2206SChristian König 
181390aca4d2SJerome Glisse 	radeon_restore_bios_scratch_regs(rdev);
181455d7c221SChristian König 
181555d7c221SChristian König 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
18169bb39ff4SMaarten Lankhorst 		if (!r && ring_data[i]) {
181755d7c221SChristian König 			radeon_ring_restore(rdev, &rdev->ring[i],
181855d7c221SChristian König 					    ring_sizes[i], ring_data[i]);
181955d7c221SChristian König 		} else {
1820eb98c709SChristian König 			radeon_fence_driver_force_completion(rdev, i);
182155d7c221SChristian König 			kfree(ring_data[i]);
182255d7c221SChristian König 		}
182355d7c221SChristian König 	}
182455d7c221SChristian König 
1825c940b447SAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
1826c940b447SAlex Deucher 		/* do dpm late init */
1827c940b447SAlex Deucher 		r = radeon_pm_late_init(rdev);
1828c940b447SAlex Deucher 		if (r) {
1829c940b447SAlex Deucher 			rdev->pm.dpm_enabled = false;
1830c940b447SAlex Deucher 			DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
1831c940b447SAlex Deucher 		}
1832c940b447SAlex Deucher 	} else {
1833c940b447SAlex Deucher 		/* resume old pm late */
183495f59509SAlex Deucher 		radeon_pm_resume(rdev);
1835c940b447SAlex Deucher 	}
1836c940b447SAlex Deucher 
183773ef0e0dSAlex Deucher 	/* init dig PHYs, disp eng pll */
183873ef0e0dSAlex Deucher 	if (rdev->is_atom_bios) {
183973ef0e0dSAlex Deucher 		radeon_atom_encoder_init(rdev);
184073ef0e0dSAlex Deucher 		radeon_atom_disp_eng_pll_init(rdev);
184173ef0e0dSAlex Deucher 		/* turn on the BL */
184273ef0e0dSAlex Deucher 		if (rdev->mode_info.bl_encoder) {
184373ef0e0dSAlex Deucher 			u8 bl_level = radeon_get_backlight_level(rdev,
184473ef0e0dSAlex Deucher 								 rdev->mode_info.bl_encoder);
184573ef0e0dSAlex Deucher 			radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
184673ef0e0dSAlex Deucher 						   bl_level);
184773ef0e0dSAlex Deucher 		}
184873ef0e0dSAlex Deucher 	}
184973ef0e0dSAlex Deucher 	/* reset hpd state */
185073ef0e0dSAlex Deucher 	radeon_hpd_init(rdev);
185173ef0e0dSAlex Deucher 
18529bb39ff4SMaarten Lankhorst 	ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
18533c036389SChristian König 
18543c036389SChristian König 	rdev->in_reset = true;
18553c036389SChristian König 	rdev->needs_reset = false;
18563c036389SChristian König 
18579bb39ff4SMaarten Lankhorst 	downgrade_write(&rdev->exclusive_lock);
18589bb39ff4SMaarten Lankhorst 
1859d3493574SJerome Glisse 	drm_helper_resume_force_mode(rdev->ddev);
1860d3493574SJerome Glisse 
1861c940b447SAlex Deucher 	/* set the power state here in case we are a PX system or headless */
1862c940b447SAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled)
1863c940b447SAlex Deucher 		radeon_pm_compute_clocks(rdev);
1864c940b447SAlex Deucher 
18659bb39ff4SMaarten Lankhorst 	if (!r) {
18669bb39ff4SMaarten Lankhorst 		r = radeon_ib_ring_tests(rdev);
18679bb39ff4SMaarten Lankhorst 		if (r && saved)
18689bb39ff4SMaarten Lankhorst 			r = -EAGAIN;
18699bb39ff4SMaarten Lankhorst 	} else {
187090aca4d2SJerome Glisse 		/* bad news, how to tell it to userspace ? */
187190aca4d2SJerome Glisse 		dev_info(rdev->dev, "GPU reset failed\n");
18727a1619b9SMichel Dänzer 	}
18737a1619b9SMichel Dänzer 
18749bb39ff4SMaarten Lankhorst 	rdev->needs_reset = r == -EAGAIN;
18759bb39ff4SMaarten Lankhorst 	rdev->in_reset = false;
18769bb39ff4SMaarten Lankhorst 
18779bb39ff4SMaarten Lankhorst 	up_read(&rdev->exclusive_lock);
187890aca4d2SJerome Glisse 	return r;
187990aca4d2SJerome Glisse }
1880