xref: /openbmc/linux/drivers/gpu/drm/radeon/radeon_device.c (revision fe0d36e03a08a6f88e5cb9f81c3b819f797e8024)
1771fe6b9SJerome Glisse /*
2771fe6b9SJerome Glisse  * Copyright 2008 Advanced Micro Devices, Inc.
3771fe6b9SJerome Glisse  * Copyright 2008 Red Hat Inc.
4771fe6b9SJerome Glisse  * Copyright 2009 Jerome Glisse.
5771fe6b9SJerome Glisse  *
6771fe6b9SJerome Glisse  * Permission is hereby granted, free of charge, to any person obtaining a
7771fe6b9SJerome Glisse  * copy of this software and associated documentation files (the "Software"),
8771fe6b9SJerome Glisse  * to deal in the Software without restriction, including without limitation
9771fe6b9SJerome Glisse  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10771fe6b9SJerome Glisse  * and/or sell copies of the Software, and to permit persons to whom the
11771fe6b9SJerome Glisse  * Software is furnished to do so, subject to the following conditions:
12771fe6b9SJerome Glisse  *
13771fe6b9SJerome Glisse  * The above copyright notice and this permission notice shall be included in
14771fe6b9SJerome Glisse  * all copies or substantial portions of the Software.
15771fe6b9SJerome Glisse  *
16771fe6b9SJerome Glisse  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17771fe6b9SJerome Glisse  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18771fe6b9SJerome Glisse  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19771fe6b9SJerome Glisse  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20771fe6b9SJerome Glisse  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21771fe6b9SJerome Glisse  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22771fe6b9SJerome Glisse  * OTHER DEALINGS IN THE SOFTWARE.
23771fe6b9SJerome Glisse  *
24771fe6b9SJerome Glisse  * Authors: Dave Airlie
25771fe6b9SJerome Glisse  *          Alex Deucher
26771fe6b9SJerome Glisse  *          Jerome Glisse
27771fe6b9SJerome Glisse  */
28771fe6b9SJerome Glisse #include <linux/console.h>
295a0e3ad6STejun Heo #include <linux/slab.h>
30771fe6b9SJerome Glisse #include <drm/drmP.h>
31771fe6b9SJerome Glisse #include <drm/drm_crtc_helper.h>
32771fe6b9SJerome Glisse #include <drm/radeon_drm.h>
3328d52043SDave Airlie #include <linux/vgaarb.h>
346a9ee8afSDave Airlie #include <linux/vga_switcheroo.h>
35bcc65fd8SMatthew Garrett #include <linux/efi.h>
36771fe6b9SJerome Glisse #include "radeon_reg.h"
37771fe6b9SJerome Glisse #include "radeon.h"
38771fe6b9SJerome Glisse #include "atom.h"
39771fe6b9SJerome Glisse 
401b5331d9SJerome Glisse static const char radeon_family_name[][16] = {
411b5331d9SJerome Glisse 	"R100",
421b5331d9SJerome Glisse 	"RV100",
431b5331d9SJerome Glisse 	"RS100",
441b5331d9SJerome Glisse 	"RV200",
451b5331d9SJerome Glisse 	"RS200",
461b5331d9SJerome Glisse 	"R200",
471b5331d9SJerome Glisse 	"RV250",
481b5331d9SJerome Glisse 	"RS300",
491b5331d9SJerome Glisse 	"RV280",
501b5331d9SJerome Glisse 	"R300",
511b5331d9SJerome Glisse 	"R350",
521b5331d9SJerome Glisse 	"RV350",
531b5331d9SJerome Glisse 	"RV380",
541b5331d9SJerome Glisse 	"R420",
551b5331d9SJerome Glisse 	"R423",
561b5331d9SJerome Glisse 	"RV410",
571b5331d9SJerome Glisse 	"RS400",
581b5331d9SJerome Glisse 	"RS480",
591b5331d9SJerome Glisse 	"RS600",
601b5331d9SJerome Glisse 	"RS690",
611b5331d9SJerome Glisse 	"RS740",
621b5331d9SJerome Glisse 	"RV515",
631b5331d9SJerome Glisse 	"R520",
641b5331d9SJerome Glisse 	"RV530",
651b5331d9SJerome Glisse 	"RV560",
661b5331d9SJerome Glisse 	"RV570",
671b5331d9SJerome Glisse 	"R580",
681b5331d9SJerome Glisse 	"R600",
691b5331d9SJerome Glisse 	"RV610",
701b5331d9SJerome Glisse 	"RV630",
711b5331d9SJerome Glisse 	"RV670",
721b5331d9SJerome Glisse 	"RV620",
731b5331d9SJerome Glisse 	"RV635",
741b5331d9SJerome Glisse 	"RS780",
751b5331d9SJerome Glisse 	"RS880",
761b5331d9SJerome Glisse 	"RV770",
771b5331d9SJerome Glisse 	"RV730",
781b5331d9SJerome Glisse 	"RV710",
791b5331d9SJerome Glisse 	"RV740",
801b5331d9SJerome Glisse 	"CEDAR",
811b5331d9SJerome Glisse 	"REDWOOD",
821b5331d9SJerome Glisse 	"JUNIPER",
831b5331d9SJerome Glisse 	"CYPRESS",
841b5331d9SJerome Glisse 	"HEMLOCK",
85b08ebe7eSAlex Deucher 	"PALM",
864df64e65SAlex Deucher 	"SUMO",
874df64e65SAlex Deucher 	"SUMO2",
881fe18305SAlex Deucher 	"BARTS",
891fe18305SAlex Deucher 	"TURKS",
901fe18305SAlex Deucher 	"CAICOS",
91b7cfc9feSAlex Deucher 	"CAYMAN",
928848f759SAlex Deucher 	"ARUBA",
93cb28bb34SAlex Deucher 	"TAHITI",
94cb28bb34SAlex Deucher 	"PITCAIRN",
95cb28bb34SAlex Deucher 	"VERDE",
96624d3524SAlex Deucher 	"OLAND",
97b5d9d726SAlex Deucher 	"HAINAN",
986eac752eSAlex Deucher 	"BONAIRE",
996eac752eSAlex Deucher 	"KAVERI",
1006eac752eSAlex Deucher 	"KABINI",
1013bf599e8SAlex Deucher 	"HAWAII",
102b0a9f22aSSamuel Li 	"MULLINS",
1031b5331d9SJerome Glisse 	"LAST",
1041b5331d9SJerome Glisse };
1051b5331d9SJerome Glisse 
106e64c952eSAlex Deucher #if defined(CONFIG_VGA_SWITCHEROO)
107e64c952eSAlex Deucher bool radeon_has_atpx_dgpu_power_cntl(void);
108e64c952eSAlex Deucher #else
109e64c952eSAlex Deucher static inline bool radeon_has_atpx_dgpu_power_cntl(void) { return false; }
110e64c952eSAlex Deucher #endif
111e64c952eSAlex Deucher 
1124807c5a8SAlex Deucher #define RADEON_PX_QUIRK_DISABLE_PX  (1 << 0)
1134807c5a8SAlex Deucher #define RADEON_PX_QUIRK_LONG_WAKEUP (1 << 1)
1144807c5a8SAlex Deucher 
1154807c5a8SAlex Deucher struct radeon_px_quirk {
1164807c5a8SAlex Deucher 	u32 chip_vendor;
1174807c5a8SAlex Deucher 	u32 chip_device;
1184807c5a8SAlex Deucher 	u32 subsys_vendor;
1194807c5a8SAlex Deucher 	u32 subsys_device;
1204807c5a8SAlex Deucher 	u32 px_quirk_flags;
1214807c5a8SAlex Deucher };
1224807c5a8SAlex Deucher 
1234807c5a8SAlex Deucher static struct radeon_px_quirk radeon_px_quirk_list[] = {
1244807c5a8SAlex Deucher 	/* Acer aspire 5560g (CPU: AMD A4-3305M; GPU: AMD Radeon HD 6480g + 7470m)
1254807c5a8SAlex Deucher 	 * https://bugzilla.kernel.org/show_bug.cgi?id=74551
1264807c5a8SAlex Deucher 	 */
1274807c5a8SAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6760, 0x1025, 0x0672, RADEON_PX_QUIRK_DISABLE_PX },
1284807c5a8SAlex Deucher 	/* Asus K73TA laptop with AMD A6-3400M APU and Radeon 6550 GPU
1294807c5a8SAlex Deucher 	 * https://bugzilla.kernel.org/show_bug.cgi?id=51381
1304807c5a8SAlex Deucher 	 */
1314807c5a8SAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6741, 0x1043, 0x108c, RADEON_PX_QUIRK_DISABLE_PX },
132ff1b1294SAlex Deucher 	/* Asus K53TK laptop with AMD A6-3420M APU and Radeon 7670m GPU
133ff1b1294SAlex Deucher 	 * https://bugzilla.kernel.org/show_bug.cgi?id=51381
134ff1b1294SAlex Deucher 	 */
135ff1b1294SAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6840, 0x1043, 0x2122, RADEON_PX_QUIRK_DISABLE_PX },
1364807c5a8SAlex Deucher 	/* macbook pro 8.2 */
1374807c5a8SAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6741, PCI_VENDOR_ID_APPLE, 0x00e2, RADEON_PX_QUIRK_LONG_WAKEUP },
1384807c5a8SAlex Deucher 	{ 0, 0, 0, 0, 0 },
1394807c5a8SAlex Deucher };
1404807c5a8SAlex Deucher 
14190c4cde9SAlex Deucher bool radeon_is_px(struct drm_device *dev)
14290c4cde9SAlex Deucher {
14390c4cde9SAlex Deucher 	struct radeon_device *rdev = dev->dev_private;
14490c4cde9SAlex Deucher 
14590c4cde9SAlex Deucher 	if (rdev->flags & RADEON_IS_PX)
14690c4cde9SAlex Deucher 		return true;
14790c4cde9SAlex Deucher 	return false;
14890c4cde9SAlex Deucher }
14910ebc0bcSDave Airlie 
1504807c5a8SAlex Deucher static void radeon_device_handle_px_quirks(struct radeon_device *rdev)
1514807c5a8SAlex Deucher {
1524807c5a8SAlex Deucher 	struct radeon_px_quirk *p = radeon_px_quirk_list;
1534807c5a8SAlex Deucher 
1544807c5a8SAlex Deucher 	/* Apply PX quirks */
1554807c5a8SAlex Deucher 	while (p && p->chip_device != 0) {
1564807c5a8SAlex Deucher 		if (rdev->pdev->vendor == p->chip_vendor &&
1574807c5a8SAlex Deucher 		    rdev->pdev->device == p->chip_device &&
1584807c5a8SAlex Deucher 		    rdev->pdev->subsystem_vendor == p->subsys_vendor &&
1594807c5a8SAlex Deucher 		    rdev->pdev->subsystem_device == p->subsys_device) {
1604807c5a8SAlex Deucher 			rdev->px_quirk_flags = p->px_quirk_flags;
1614807c5a8SAlex Deucher 			break;
1624807c5a8SAlex Deucher 		}
1634807c5a8SAlex Deucher 		++p;
1644807c5a8SAlex Deucher 	}
1654807c5a8SAlex Deucher 
1664807c5a8SAlex Deucher 	if (rdev->px_quirk_flags & RADEON_PX_QUIRK_DISABLE_PX)
1674807c5a8SAlex Deucher 		rdev->flags &= ~RADEON_IS_PX;
1684807c5a8SAlex Deucher }
1694807c5a8SAlex Deucher 
1700c195119SAlex Deucher /**
1712e1b65f9SAlex Deucher  * radeon_program_register_sequence - program an array of registers.
1722e1b65f9SAlex Deucher  *
1732e1b65f9SAlex Deucher  * @rdev: radeon_device pointer
1742e1b65f9SAlex Deucher  * @registers: pointer to the register array
1752e1b65f9SAlex Deucher  * @array_size: size of the register array
1762e1b65f9SAlex Deucher  *
1772e1b65f9SAlex Deucher  * Programs an array or registers with and and or masks.
1782e1b65f9SAlex Deucher  * This is a helper for setting golden registers.
1792e1b65f9SAlex Deucher  */
1802e1b65f9SAlex Deucher void radeon_program_register_sequence(struct radeon_device *rdev,
1812e1b65f9SAlex Deucher 				      const u32 *registers,
1822e1b65f9SAlex Deucher 				      const u32 array_size)
1832e1b65f9SAlex Deucher {
1842e1b65f9SAlex Deucher 	u32 tmp, reg, and_mask, or_mask;
1852e1b65f9SAlex Deucher 	int i;
1862e1b65f9SAlex Deucher 
1872e1b65f9SAlex Deucher 	if (array_size % 3)
1882e1b65f9SAlex Deucher 		return;
1892e1b65f9SAlex Deucher 
1902e1b65f9SAlex Deucher 	for (i = 0; i < array_size; i +=3) {
1912e1b65f9SAlex Deucher 		reg = registers[i + 0];
1922e1b65f9SAlex Deucher 		and_mask = registers[i + 1];
1932e1b65f9SAlex Deucher 		or_mask = registers[i + 2];
1942e1b65f9SAlex Deucher 
1952e1b65f9SAlex Deucher 		if (and_mask == 0xffffffff) {
1962e1b65f9SAlex Deucher 			tmp = or_mask;
1972e1b65f9SAlex Deucher 		} else {
1982e1b65f9SAlex Deucher 			tmp = RREG32(reg);
1992e1b65f9SAlex Deucher 			tmp &= ~and_mask;
2002e1b65f9SAlex Deucher 			tmp |= or_mask;
2012e1b65f9SAlex Deucher 		}
2022e1b65f9SAlex Deucher 		WREG32(reg, tmp);
2032e1b65f9SAlex Deucher 	}
2042e1b65f9SAlex Deucher }
2052e1b65f9SAlex Deucher 
2061a0041b8SAlex Deucher void radeon_pci_config_reset(struct radeon_device *rdev)
2071a0041b8SAlex Deucher {
2081a0041b8SAlex Deucher 	pci_write_config_dword(rdev->pdev, 0x7c, RADEON_ASIC_RESET_DATA);
2091a0041b8SAlex Deucher }
2101a0041b8SAlex Deucher 
2112e1b65f9SAlex Deucher /**
2120c195119SAlex Deucher  * radeon_surface_init - Clear GPU surface registers.
2130c195119SAlex Deucher  *
2140c195119SAlex Deucher  * @rdev: radeon_device pointer
2150c195119SAlex Deucher  *
2160c195119SAlex Deucher  * Clear GPU surface registers (r1xx-r5xx).
217b1e3a6d1SMichel Dänzer  */
2183ce0a23dSJerome Glisse void radeon_surface_init(struct radeon_device *rdev)
219b1e3a6d1SMichel Dänzer {
220b1e3a6d1SMichel Dänzer 	/* FIXME: check this out */
221b1e3a6d1SMichel Dänzer 	if (rdev->family < CHIP_R600) {
222b1e3a6d1SMichel Dänzer 		int i;
223b1e3a6d1SMichel Dänzer 
224550e2d92SDave Airlie 		for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
225550e2d92SDave Airlie 			if (rdev->surface_regs[i].bo)
226550e2d92SDave Airlie 				radeon_bo_get_surface_reg(rdev->surface_regs[i].bo);
227550e2d92SDave Airlie 			else
228550e2d92SDave Airlie 				radeon_clear_surface_reg(rdev, i);
229b1e3a6d1SMichel Dänzer 		}
230e024e110SDave Airlie 		/* enable surfaces */
231e024e110SDave Airlie 		WREG32(RADEON_SURFACE_CNTL, 0);
232b1e3a6d1SMichel Dänzer 	}
233b1e3a6d1SMichel Dänzer }
234b1e3a6d1SMichel Dänzer 
235b1e3a6d1SMichel Dänzer /*
236771fe6b9SJerome Glisse  * GPU scratch registers helpers function.
237771fe6b9SJerome Glisse  */
2380c195119SAlex Deucher /**
2390c195119SAlex Deucher  * radeon_scratch_init - Init scratch register driver information.
2400c195119SAlex Deucher  *
2410c195119SAlex Deucher  * @rdev: radeon_device pointer
2420c195119SAlex Deucher  *
2430c195119SAlex Deucher  * Init CP scratch register driver information (r1xx-r5xx)
2440c195119SAlex Deucher  */
2453ce0a23dSJerome Glisse void radeon_scratch_init(struct radeon_device *rdev)
246771fe6b9SJerome Glisse {
247771fe6b9SJerome Glisse 	int i;
248771fe6b9SJerome Glisse 
249771fe6b9SJerome Glisse 	/* FIXME: check this out */
250771fe6b9SJerome Glisse 	if (rdev->family < CHIP_R300) {
251771fe6b9SJerome Glisse 		rdev->scratch.num_reg = 5;
252771fe6b9SJerome Glisse 	} else {
253771fe6b9SJerome Glisse 		rdev->scratch.num_reg = 7;
254771fe6b9SJerome Glisse 	}
255724c80e1SAlex Deucher 	rdev->scratch.reg_base = RADEON_SCRATCH_REG0;
256771fe6b9SJerome Glisse 	for (i = 0; i < rdev->scratch.num_reg; i++) {
257771fe6b9SJerome Glisse 		rdev->scratch.free[i] = true;
258724c80e1SAlex Deucher 		rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4);
259771fe6b9SJerome Glisse 	}
260771fe6b9SJerome Glisse }
261771fe6b9SJerome Glisse 
2620c195119SAlex Deucher /**
2630c195119SAlex Deucher  * radeon_scratch_get - Allocate a scratch register
2640c195119SAlex Deucher  *
2650c195119SAlex Deucher  * @rdev: radeon_device pointer
2660c195119SAlex Deucher  * @reg: scratch register mmio offset
2670c195119SAlex Deucher  *
2680c195119SAlex Deucher  * Allocate a CP scratch register for use by the driver (all asics).
2690c195119SAlex Deucher  * Returns 0 on success or -EINVAL on failure.
2700c195119SAlex Deucher  */
271771fe6b9SJerome Glisse int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg)
272771fe6b9SJerome Glisse {
273771fe6b9SJerome Glisse 	int i;
274771fe6b9SJerome Glisse 
275771fe6b9SJerome Glisse 	for (i = 0; i < rdev->scratch.num_reg; i++) {
276771fe6b9SJerome Glisse 		if (rdev->scratch.free[i]) {
277771fe6b9SJerome Glisse 			rdev->scratch.free[i] = false;
278771fe6b9SJerome Glisse 			*reg = rdev->scratch.reg[i];
279771fe6b9SJerome Glisse 			return 0;
280771fe6b9SJerome Glisse 		}
281771fe6b9SJerome Glisse 	}
282771fe6b9SJerome Glisse 	return -EINVAL;
283771fe6b9SJerome Glisse }
284771fe6b9SJerome Glisse 
2850c195119SAlex Deucher /**
2860c195119SAlex Deucher  * radeon_scratch_free - Free a scratch register
2870c195119SAlex Deucher  *
2880c195119SAlex Deucher  * @rdev: radeon_device pointer
2890c195119SAlex Deucher  * @reg: scratch register mmio offset
2900c195119SAlex Deucher  *
2910c195119SAlex Deucher  * Free a CP scratch register allocated for use by the driver (all asics)
2920c195119SAlex Deucher  */
293771fe6b9SJerome Glisse void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg)
294771fe6b9SJerome Glisse {
295771fe6b9SJerome Glisse 	int i;
296771fe6b9SJerome Glisse 
297771fe6b9SJerome Glisse 	for (i = 0; i < rdev->scratch.num_reg; i++) {
298771fe6b9SJerome Glisse 		if (rdev->scratch.reg[i] == reg) {
299771fe6b9SJerome Glisse 			rdev->scratch.free[i] = true;
300771fe6b9SJerome Glisse 			return;
301771fe6b9SJerome Glisse 		}
302771fe6b9SJerome Glisse 	}
303771fe6b9SJerome Glisse }
304771fe6b9SJerome Glisse 
3050c195119SAlex Deucher /*
30675efdee1SAlex Deucher  * GPU doorbell aperture helpers function.
30775efdee1SAlex Deucher  */
30875efdee1SAlex Deucher /**
30975efdee1SAlex Deucher  * radeon_doorbell_init - Init doorbell driver information.
31075efdee1SAlex Deucher  *
31175efdee1SAlex Deucher  * @rdev: radeon_device pointer
31275efdee1SAlex Deucher  *
31375efdee1SAlex Deucher  * Init doorbell driver information (CIK)
31475efdee1SAlex Deucher  * Returns 0 on success, error on failure.
31575efdee1SAlex Deucher  */
31628f5a6cdSRashika Kheria static int radeon_doorbell_init(struct radeon_device *rdev)
31775efdee1SAlex Deucher {
31875efdee1SAlex Deucher 	/* doorbell bar mapping */
31975efdee1SAlex Deucher 	rdev->doorbell.base = pci_resource_start(rdev->pdev, 2);
32075efdee1SAlex Deucher 	rdev->doorbell.size = pci_resource_len(rdev->pdev, 2);
32175efdee1SAlex Deucher 
322d5754ab8SAndrew Lewycky 	rdev->doorbell.num_doorbells = min_t(u32, rdev->doorbell.size / sizeof(u32), RADEON_MAX_DOORBELLS);
323d5754ab8SAndrew Lewycky 	if (rdev->doorbell.num_doorbells == 0)
324d5754ab8SAndrew Lewycky 		return -EINVAL;
32575efdee1SAlex Deucher 
326d5754ab8SAndrew Lewycky 	rdev->doorbell.ptr = ioremap(rdev->doorbell.base, rdev->doorbell.num_doorbells * sizeof(u32));
32775efdee1SAlex Deucher 	if (rdev->doorbell.ptr == NULL) {
32875efdee1SAlex Deucher 		return -ENOMEM;
32975efdee1SAlex Deucher 	}
33075efdee1SAlex Deucher 	DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)rdev->doorbell.base);
33175efdee1SAlex Deucher 	DRM_INFO("doorbell mmio size: %u\n", (unsigned)rdev->doorbell.size);
33275efdee1SAlex Deucher 
333d5754ab8SAndrew Lewycky 	memset(&rdev->doorbell.used, 0, sizeof(rdev->doorbell.used));
33475efdee1SAlex Deucher 
33575efdee1SAlex Deucher 	return 0;
33675efdee1SAlex Deucher }
33775efdee1SAlex Deucher 
33875efdee1SAlex Deucher /**
33975efdee1SAlex Deucher  * radeon_doorbell_fini - Tear down doorbell driver information.
34075efdee1SAlex Deucher  *
34175efdee1SAlex Deucher  * @rdev: radeon_device pointer
34275efdee1SAlex Deucher  *
34375efdee1SAlex Deucher  * Tear down doorbell driver information (CIK)
34475efdee1SAlex Deucher  */
34528f5a6cdSRashika Kheria static void radeon_doorbell_fini(struct radeon_device *rdev)
34675efdee1SAlex Deucher {
34775efdee1SAlex Deucher 	iounmap(rdev->doorbell.ptr);
34875efdee1SAlex Deucher 	rdev->doorbell.ptr = NULL;
34975efdee1SAlex Deucher }
35075efdee1SAlex Deucher 
35175efdee1SAlex Deucher /**
352d5754ab8SAndrew Lewycky  * radeon_doorbell_get - Allocate a doorbell entry
35375efdee1SAlex Deucher  *
35475efdee1SAlex Deucher  * @rdev: radeon_device pointer
355d5754ab8SAndrew Lewycky  * @doorbell: doorbell index
35675efdee1SAlex Deucher  *
357d5754ab8SAndrew Lewycky  * Allocate a doorbell for use by the driver (all asics).
35875efdee1SAlex Deucher  * Returns 0 on success or -EINVAL on failure.
35975efdee1SAlex Deucher  */
36075efdee1SAlex Deucher int radeon_doorbell_get(struct radeon_device *rdev, u32 *doorbell)
36175efdee1SAlex Deucher {
362d5754ab8SAndrew Lewycky 	unsigned long offset = find_first_zero_bit(rdev->doorbell.used, rdev->doorbell.num_doorbells);
363d5754ab8SAndrew Lewycky 	if (offset < rdev->doorbell.num_doorbells) {
364d5754ab8SAndrew Lewycky 		__set_bit(offset, rdev->doorbell.used);
365d5754ab8SAndrew Lewycky 		*doorbell = offset;
36675efdee1SAlex Deucher 		return 0;
367d5754ab8SAndrew Lewycky 	} else {
36875efdee1SAlex Deucher 		return -EINVAL;
36975efdee1SAlex Deucher 	}
370d5754ab8SAndrew Lewycky }
37175efdee1SAlex Deucher 
37275efdee1SAlex Deucher /**
373d5754ab8SAndrew Lewycky  * radeon_doorbell_free - Free a doorbell entry
37475efdee1SAlex Deucher  *
37575efdee1SAlex Deucher  * @rdev: radeon_device pointer
376d5754ab8SAndrew Lewycky  * @doorbell: doorbell index
37775efdee1SAlex Deucher  *
378d5754ab8SAndrew Lewycky  * Free a doorbell allocated for use by the driver (all asics)
37975efdee1SAlex Deucher  */
38075efdee1SAlex Deucher void radeon_doorbell_free(struct radeon_device *rdev, u32 doorbell)
38175efdee1SAlex Deucher {
382d5754ab8SAndrew Lewycky 	if (doorbell < rdev->doorbell.num_doorbells)
383d5754ab8SAndrew Lewycky 		__clear_bit(doorbell, rdev->doorbell.used);
38475efdee1SAlex Deucher }
38575efdee1SAlex Deucher 
386ebff8453SOded Gabbay /**
387ebff8453SOded Gabbay  * radeon_doorbell_get_kfd_info - Report doorbell configuration required to
388ebff8453SOded Gabbay  *                                setup KFD
389ebff8453SOded Gabbay  *
390ebff8453SOded Gabbay  * @rdev: radeon_device pointer
391ebff8453SOded Gabbay  * @aperture_base: output returning doorbell aperture base physical address
392ebff8453SOded Gabbay  * @aperture_size: output returning doorbell aperture size in bytes
393ebff8453SOded Gabbay  * @start_offset: output returning # of doorbell bytes reserved for radeon.
394ebff8453SOded Gabbay  *
395ebff8453SOded Gabbay  * Radeon and the KFD share the doorbell aperture. Radeon sets it up,
396ebff8453SOded Gabbay  * takes doorbells required for its own rings and reports the setup to KFD.
397ebff8453SOded Gabbay  * Radeon reserved doorbells are at the start of the doorbell aperture.
398ebff8453SOded Gabbay  */
399ebff8453SOded Gabbay void radeon_doorbell_get_kfd_info(struct radeon_device *rdev,
400ebff8453SOded Gabbay 				  phys_addr_t *aperture_base,
401ebff8453SOded Gabbay 				  size_t *aperture_size,
402ebff8453SOded Gabbay 				  size_t *start_offset)
403ebff8453SOded Gabbay {
404ebff8453SOded Gabbay 	/* The first num_doorbells are used by radeon.
405ebff8453SOded Gabbay 	 * KFD takes whatever's left in the aperture. */
406ebff8453SOded Gabbay 	if (rdev->doorbell.size > rdev->doorbell.num_doorbells * sizeof(u32)) {
407ebff8453SOded Gabbay 		*aperture_base = rdev->doorbell.base;
408ebff8453SOded Gabbay 		*aperture_size = rdev->doorbell.size;
409ebff8453SOded Gabbay 		*start_offset = rdev->doorbell.num_doorbells * sizeof(u32);
410ebff8453SOded Gabbay 	} else {
411ebff8453SOded Gabbay 		*aperture_base = 0;
412ebff8453SOded Gabbay 		*aperture_size = 0;
413ebff8453SOded Gabbay 		*start_offset = 0;
414ebff8453SOded Gabbay 	}
415ebff8453SOded Gabbay }
416ebff8453SOded Gabbay 
41775efdee1SAlex Deucher /*
4180c195119SAlex Deucher  * radeon_wb_*()
4190c195119SAlex Deucher  * Writeback is the the method by which the the GPU updates special pages
4200c195119SAlex Deucher  * in memory with the status of certain GPU events (fences, ring pointers,
4210c195119SAlex Deucher  * etc.).
4220c195119SAlex Deucher  */
4230c195119SAlex Deucher 
4240c195119SAlex Deucher /**
4250c195119SAlex Deucher  * radeon_wb_disable - Disable Writeback
4260c195119SAlex Deucher  *
4270c195119SAlex Deucher  * @rdev: radeon_device pointer
4280c195119SAlex Deucher  *
4290c195119SAlex Deucher  * Disables Writeback (all asics).  Used for suspend.
4300c195119SAlex Deucher  */
431724c80e1SAlex Deucher void radeon_wb_disable(struct radeon_device *rdev)
432724c80e1SAlex Deucher {
433724c80e1SAlex Deucher 	rdev->wb.enabled = false;
434724c80e1SAlex Deucher }
435724c80e1SAlex Deucher 
4360c195119SAlex Deucher /**
4370c195119SAlex Deucher  * radeon_wb_fini - Disable Writeback and free memory
4380c195119SAlex Deucher  *
4390c195119SAlex Deucher  * @rdev: radeon_device pointer
4400c195119SAlex Deucher  *
4410c195119SAlex Deucher  * Disables Writeback and frees the Writeback memory (all asics).
4420c195119SAlex Deucher  * Used at driver shutdown.
4430c195119SAlex Deucher  */
444724c80e1SAlex Deucher void radeon_wb_fini(struct radeon_device *rdev)
445724c80e1SAlex Deucher {
446724c80e1SAlex Deucher 	radeon_wb_disable(rdev);
447724c80e1SAlex Deucher 	if (rdev->wb.wb_obj) {
448089920f2SJerome Glisse 		if (!radeon_bo_reserve(rdev->wb.wb_obj, false)) {
449089920f2SJerome Glisse 			radeon_bo_kunmap(rdev->wb.wb_obj);
450089920f2SJerome Glisse 			radeon_bo_unpin(rdev->wb.wb_obj);
451089920f2SJerome Glisse 			radeon_bo_unreserve(rdev->wb.wb_obj);
452089920f2SJerome Glisse 		}
453724c80e1SAlex Deucher 		radeon_bo_unref(&rdev->wb.wb_obj);
454724c80e1SAlex Deucher 		rdev->wb.wb = NULL;
455724c80e1SAlex Deucher 		rdev->wb.wb_obj = NULL;
456724c80e1SAlex Deucher 	}
457724c80e1SAlex Deucher }
458724c80e1SAlex Deucher 
4590c195119SAlex Deucher /**
4600c195119SAlex Deucher  * radeon_wb_init- Init Writeback driver info and allocate memory
4610c195119SAlex Deucher  *
4620c195119SAlex Deucher  * @rdev: radeon_device pointer
4630c195119SAlex Deucher  *
4640c195119SAlex Deucher  * Disables Writeback and frees the Writeback memory (all asics).
4650c195119SAlex Deucher  * Used at driver startup.
4660c195119SAlex Deucher  * Returns 0 on success or an -error on failure.
4670c195119SAlex Deucher  */
468724c80e1SAlex Deucher int radeon_wb_init(struct radeon_device *rdev)
469724c80e1SAlex Deucher {
470724c80e1SAlex Deucher 	int r;
471724c80e1SAlex Deucher 
472724c80e1SAlex Deucher 	if (rdev->wb.wb_obj == NULL) {
473441921d5SDaniel Vetter 		r = radeon_bo_create(rdev, RADEON_GPU_PAGE_SIZE, PAGE_SIZE, true,
474831b6966SMaarten Lankhorst 				     RADEON_GEM_DOMAIN_GTT, 0, NULL, NULL,
47502376d82SMichel Dänzer 				     &rdev->wb.wb_obj);
476724c80e1SAlex Deucher 		if (r) {
477724c80e1SAlex Deucher 			dev_warn(rdev->dev, "(%d) create WB bo failed\n", r);
478724c80e1SAlex Deucher 			return r;
479724c80e1SAlex Deucher 		}
480724c80e1SAlex Deucher 		r = radeon_bo_reserve(rdev->wb.wb_obj, false);
481724c80e1SAlex Deucher 		if (unlikely(r != 0)) {
482724c80e1SAlex Deucher 			radeon_wb_fini(rdev);
483724c80e1SAlex Deucher 			return r;
484724c80e1SAlex Deucher 		}
485724c80e1SAlex Deucher 		r = radeon_bo_pin(rdev->wb.wb_obj, RADEON_GEM_DOMAIN_GTT,
486724c80e1SAlex Deucher 				&rdev->wb.gpu_addr);
487724c80e1SAlex Deucher 		if (r) {
488724c80e1SAlex Deucher 			radeon_bo_unreserve(rdev->wb.wb_obj);
489724c80e1SAlex Deucher 			dev_warn(rdev->dev, "(%d) pin WB bo failed\n", r);
490724c80e1SAlex Deucher 			radeon_wb_fini(rdev);
491724c80e1SAlex Deucher 			return r;
492724c80e1SAlex Deucher 		}
493724c80e1SAlex Deucher 		r = radeon_bo_kmap(rdev->wb.wb_obj, (void **)&rdev->wb.wb);
494724c80e1SAlex Deucher 		radeon_bo_unreserve(rdev->wb.wb_obj);
495724c80e1SAlex Deucher 		if (r) {
496724c80e1SAlex Deucher 			dev_warn(rdev->dev, "(%d) map WB bo failed\n", r);
497724c80e1SAlex Deucher 			radeon_wb_fini(rdev);
498724c80e1SAlex Deucher 			return r;
499724c80e1SAlex Deucher 		}
500089920f2SJerome Glisse 	}
501724c80e1SAlex Deucher 
502e6ba7599SAlex Deucher 	/* clear wb memory */
503e6ba7599SAlex Deucher 	memset((char *)rdev->wb.wb, 0, RADEON_GPU_PAGE_SIZE);
504d0f8a854SAlex Deucher 	/* disable event_write fences */
505d0f8a854SAlex Deucher 	rdev->wb.use_event = false;
506724c80e1SAlex Deucher 	/* disabled via module param */
5073b7a2b24SJerome Glisse 	if (radeon_no_wb == 1) {
508724c80e1SAlex Deucher 		rdev->wb.enabled = false;
5093b7a2b24SJerome Glisse 	} else {
510724c80e1SAlex Deucher 		if (rdev->flags & RADEON_IS_AGP) {
51128eebb70SAlex Deucher 			/* often unreliable on AGP */
51228eebb70SAlex Deucher 			rdev->wb.enabled = false;
51328eebb70SAlex Deucher 		} else if (rdev->family < CHIP_R300) {
51428eebb70SAlex Deucher 			/* often unreliable on pre-r300 */
515724c80e1SAlex Deucher 			rdev->wb.enabled = false;
516d0f8a854SAlex Deucher 		} else {
517724c80e1SAlex Deucher 			rdev->wb.enabled = true;
518d0f8a854SAlex Deucher 			/* event_write fences are only available on r600+ */
5193b7a2b24SJerome Glisse 			if (rdev->family >= CHIP_R600) {
520d0f8a854SAlex Deucher 				rdev->wb.use_event = true;
521d0f8a854SAlex Deucher 			}
522724c80e1SAlex Deucher 		}
5233b7a2b24SJerome Glisse 	}
524c994ead6SAlex Deucher 	/* always use writeback/events on NI, APUs */
525c994ead6SAlex Deucher 	if (rdev->family >= CHIP_PALM) {
5267d52785dSAlex Deucher 		rdev->wb.enabled = true;
5277d52785dSAlex Deucher 		rdev->wb.use_event = true;
5287d52785dSAlex Deucher 	}
529724c80e1SAlex Deucher 
530724c80e1SAlex Deucher 	dev_info(rdev->dev, "WB %sabled\n", rdev->wb.enabled ? "en" : "dis");
531724c80e1SAlex Deucher 
532724c80e1SAlex Deucher 	return 0;
533724c80e1SAlex Deucher }
534724c80e1SAlex Deucher 
535d594e46aSJerome Glisse /**
536d594e46aSJerome Glisse  * radeon_vram_location - try to find VRAM location
537d594e46aSJerome Glisse  * @rdev: radeon device structure holding all necessary informations
538d594e46aSJerome Glisse  * @mc: memory controller structure holding memory informations
539d594e46aSJerome Glisse  * @base: base address at which to put VRAM
540d594e46aSJerome Glisse  *
541d594e46aSJerome Glisse  * Function will place try to place VRAM at base address provided
542d594e46aSJerome Glisse  * as parameter (which is so far either PCI aperture address or
543d594e46aSJerome Glisse  * for IGP TOM base address).
544d594e46aSJerome Glisse  *
545d594e46aSJerome Glisse  * If there is not enough space to fit the unvisible VRAM in the 32bits
546d594e46aSJerome Glisse  * address space then we limit the VRAM size to the aperture.
547d594e46aSJerome Glisse  *
548d594e46aSJerome Glisse  * If we are using AGP and if the AGP aperture doesn't allow us to have
549d594e46aSJerome Glisse  * room for all the VRAM than we restrict the VRAM to the PCI aperture
550d594e46aSJerome Glisse  * size and print a warning.
551d594e46aSJerome Glisse  *
552d594e46aSJerome Glisse  * This function will never fails, worst case are limiting VRAM.
553d594e46aSJerome Glisse  *
554d594e46aSJerome Glisse  * Note: GTT start, end, size should be initialized before calling this
555d594e46aSJerome Glisse  * function on AGP platform.
556d594e46aSJerome Glisse  *
55725985edcSLucas De Marchi  * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
558d594e46aSJerome Glisse  * this shouldn't be a problem as we are using the PCI aperture as a reference.
559d594e46aSJerome Glisse  * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
560d594e46aSJerome Glisse  * not IGP.
561d594e46aSJerome Glisse  *
562d594e46aSJerome Glisse  * Note: we use mc_vram_size as on some board we need to program the mc to
563d594e46aSJerome Glisse  * cover the whole aperture even if VRAM size is inferior to aperture size
564d594e46aSJerome Glisse  * Novell bug 204882 + along with lots of ubuntu ones
565d594e46aSJerome Glisse  *
566d594e46aSJerome Glisse  * Note: when limiting vram it's safe to overwritte real_vram_size because
567d594e46aSJerome Glisse  * we are not in case where real_vram_size is inferior to mc_vram_size (ie
568d594e46aSJerome Glisse  * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
569d594e46aSJerome Glisse  * ones)
570d594e46aSJerome Glisse  *
571d594e46aSJerome Glisse  * Note: IGP TOM addr should be the same as the aperture addr, we don't
572d594e46aSJerome Glisse  * explicitly check for that thought.
573d594e46aSJerome Glisse  *
574d594e46aSJerome Glisse  * FIXME: when reducing VRAM size align new size on power of 2.
575771fe6b9SJerome Glisse  */
576d594e46aSJerome Glisse void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base)
577771fe6b9SJerome Glisse {
5781bcb04f7SChristian König 	uint64_t limit = (uint64_t)radeon_vram_limit << 20;
5791bcb04f7SChristian König 
580d594e46aSJerome Glisse 	mc->vram_start = base;
5819ed8b1f9SAlex Deucher 	if (mc->mc_vram_size > (rdev->mc.mc_mask - base + 1)) {
582d594e46aSJerome Glisse 		dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
583d594e46aSJerome Glisse 		mc->real_vram_size = mc->aper_size;
584d594e46aSJerome Glisse 		mc->mc_vram_size = mc->aper_size;
585771fe6b9SJerome Glisse 	}
586d594e46aSJerome Glisse 	mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
5872cbeb4efSJerome Glisse 	if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_start <= mc->gtt_end) {
588d594e46aSJerome Glisse 		dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
589d594e46aSJerome Glisse 		mc->real_vram_size = mc->aper_size;
590d594e46aSJerome Glisse 		mc->mc_vram_size = mc->aper_size;
591771fe6b9SJerome Glisse 	}
592d594e46aSJerome Glisse 	mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
5931bcb04f7SChristian König 	if (limit && limit < mc->real_vram_size)
5941bcb04f7SChristian König 		mc->real_vram_size = limit;
595dd7cc55aSAlex Deucher 	dev_info(rdev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
596d594e46aSJerome Glisse 			mc->mc_vram_size >> 20, mc->vram_start,
597d594e46aSJerome Glisse 			mc->vram_end, mc->real_vram_size >> 20);
598771fe6b9SJerome Glisse }
599771fe6b9SJerome Glisse 
600d594e46aSJerome Glisse /**
601d594e46aSJerome Glisse  * radeon_gtt_location - try to find GTT location
602d594e46aSJerome Glisse  * @rdev: radeon device structure holding all necessary informations
603d594e46aSJerome Glisse  * @mc: memory controller structure holding memory informations
604d594e46aSJerome Glisse  *
605d594e46aSJerome Glisse  * Function will place try to place GTT before or after VRAM.
606d594e46aSJerome Glisse  *
607d594e46aSJerome Glisse  * If GTT size is bigger than space left then we ajust GTT size.
608d594e46aSJerome Glisse  * Thus function will never fails.
609d594e46aSJerome Glisse  *
610d594e46aSJerome Glisse  * FIXME: when reducing GTT size align new size on power of 2.
611d594e46aSJerome Glisse  */
612d594e46aSJerome Glisse void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc)
613d594e46aSJerome Glisse {
614d594e46aSJerome Glisse 	u64 size_af, size_bf;
615d594e46aSJerome Glisse 
6169ed8b1f9SAlex Deucher 	size_af = ((rdev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
6178d369bb1SAlex Deucher 	size_bf = mc->vram_start & ~mc->gtt_base_align;
618d594e46aSJerome Glisse 	if (size_bf > size_af) {
619d594e46aSJerome Glisse 		if (mc->gtt_size > size_bf) {
620d594e46aSJerome Glisse 			dev_warn(rdev->dev, "limiting GTT\n");
621d594e46aSJerome Glisse 			mc->gtt_size = size_bf;
622d594e46aSJerome Glisse 		}
6238d369bb1SAlex Deucher 		mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
624d594e46aSJerome Glisse 	} else {
625d594e46aSJerome Glisse 		if (mc->gtt_size > size_af) {
626d594e46aSJerome Glisse 			dev_warn(rdev->dev, "limiting GTT\n");
627d594e46aSJerome Glisse 			mc->gtt_size = size_af;
628d594e46aSJerome Glisse 		}
6298d369bb1SAlex Deucher 		mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
630d594e46aSJerome Glisse 	}
631d594e46aSJerome Glisse 	mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
632dd7cc55aSAlex Deucher 	dev_info(rdev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
633d594e46aSJerome Glisse 			mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
634d594e46aSJerome Glisse }
635771fe6b9SJerome Glisse 
636771fe6b9SJerome Glisse /*
637771fe6b9SJerome Glisse  * GPU helpers function.
638771fe6b9SJerome Glisse  */
6390c195119SAlex Deucher /**
6400c195119SAlex Deucher  * radeon_card_posted - check if the hw has already been initialized
6410c195119SAlex Deucher  *
6420c195119SAlex Deucher  * @rdev: radeon_device pointer
6430c195119SAlex Deucher  *
6440c195119SAlex Deucher  * Check if the asic has been initialized (all asics).
6450c195119SAlex Deucher  * Used at driver startup.
6460c195119SAlex Deucher  * Returns true if initialized or false if not.
6470c195119SAlex Deucher  */
6489f022ddfSJerome Glisse bool radeon_card_posted(struct radeon_device *rdev)
649771fe6b9SJerome Glisse {
650771fe6b9SJerome Glisse 	uint32_t reg;
651771fe6b9SJerome Glisse 
65250a583f6SAlex Deucher 	/* required for EFI mode on macbook2,1 which uses an r5xx asic */
65383e68189SMatt Fleming 	if (efi_enabled(EFI_BOOT) &&
65450a583f6SAlex Deucher 	    (rdev->pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE) &&
65550a583f6SAlex Deucher 	    (rdev->family < CHIP_R600))
656bcc65fd8SMatthew Garrett 		return false;
657bcc65fd8SMatthew Garrett 
6582cf3a4fcSAlex Deucher 	if (ASIC_IS_NODCE(rdev))
6592cf3a4fcSAlex Deucher 		goto check_memsize;
6602cf3a4fcSAlex Deucher 
661771fe6b9SJerome Glisse 	/* first check CRTCs */
66209fb8bd1SAlex Deucher 	if (ASIC_IS_DCE4(rdev)) {
66318007401SAlex Deucher 		reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
66418007401SAlex Deucher 			RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET);
66509fb8bd1SAlex Deucher 			if (rdev->num_crtc >= 4) {
66609fb8bd1SAlex Deucher 				reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) |
66709fb8bd1SAlex Deucher 					RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET);
66809fb8bd1SAlex Deucher 			}
66909fb8bd1SAlex Deucher 			if (rdev->num_crtc >= 6) {
67009fb8bd1SAlex Deucher 				reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) |
671bcc1c2a1SAlex Deucher 					RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET);
67209fb8bd1SAlex Deucher 			}
673bcc1c2a1SAlex Deucher 		if (reg & EVERGREEN_CRTC_MASTER_EN)
674bcc1c2a1SAlex Deucher 			return true;
675bcc1c2a1SAlex Deucher 	} else if (ASIC_IS_AVIVO(rdev)) {
676771fe6b9SJerome Glisse 		reg = RREG32(AVIVO_D1CRTC_CONTROL) |
677771fe6b9SJerome Glisse 		      RREG32(AVIVO_D2CRTC_CONTROL);
678771fe6b9SJerome Glisse 		if (reg & AVIVO_CRTC_EN) {
679771fe6b9SJerome Glisse 			return true;
680771fe6b9SJerome Glisse 		}
681771fe6b9SJerome Glisse 	} else {
682771fe6b9SJerome Glisse 		reg = RREG32(RADEON_CRTC_GEN_CNTL) |
683771fe6b9SJerome Glisse 		      RREG32(RADEON_CRTC2_GEN_CNTL);
684771fe6b9SJerome Glisse 		if (reg & RADEON_CRTC_EN) {
685771fe6b9SJerome Glisse 			return true;
686771fe6b9SJerome Glisse 		}
687771fe6b9SJerome Glisse 	}
688771fe6b9SJerome Glisse 
6892cf3a4fcSAlex Deucher check_memsize:
690771fe6b9SJerome Glisse 	/* then check MEM_SIZE, in case the crtcs are off */
691771fe6b9SJerome Glisse 	if (rdev->family >= CHIP_R600)
692771fe6b9SJerome Glisse 		reg = RREG32(R600_CONFIG_MEMSIZE);
693771fe6b9SJerome Glisse 	else
694771fe6b9SJerome Glisse 		reg = RREG32(RADEON_CONFIG_MEMSIZE);
695771fe6b9SJerome Glisse 
696771fe6b9SJerome Glisse 	if (reg)
697771fe6b9SJerome Glisse 		return true;
698771fe6b9SJerome Glisse 
699771fe6b9SJerome Glisse 	return false;
700771fe6b9SJerome Glisse 
701771fe6b9SJerome Glisse }
702771fe6b9SJerome Glisse 
7030c195119SAlex Deucher /**
7040c195119SAlex Deucher  * radeon_update_bandwidth_info - update display bandwidth params
7050c195119SAlex Deucher  *
7060c195119SAlex Deucher  * @rdev: radeon_device pointer
7070c195119SAlex Deucher  *
7080c195119SAlex Deucher  * Used when sclk/mclk are switched or display modes are set.
7090c195119SAlex Deucher  * params are used to calculate display watermarks (all asics)
7100c195119SAlex Deucher  */
711f47299c5SAlex Deucher void radeon_update_bandwidth_info(struct radeon_device *rdev)
712f47299c5SAlex Deucher {
713f47299c5SAlex Deucher 	fixed20_12 a;
7148807286eSAlex Deucher 	u32 sclk = rdev->pm.current_sclk;
7158807286eSAlex Deucher 	u32 mclk = rdev->pm.current_mclk;
716f47299c5SAlex Deucher 
7178807286eSAlex Deucher 	/* sclk/mclk in Mhz */
71868adac5eSBen Skeggs 	a.full = dfixed_const(100);
71968adac5eSBen Skeggs 	rdev->pm.sclk.full = dfixed_const(sclk);
72068adac5eSBen Skeggs 	rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a);
72168adac5eSBen Skeggs 	rdev->pm.mclk.full = dfixed_const(mclk);
72268adac5eSBen Skeggs 	rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a);
723f47299c5SAlex Deucher 
7248807286eSAlex Deucher 	if (rdev->flags & RADEON_IS_IGP) {
72568adac5eSBen Skeggs 		a.full = dfixed_const(16);
726f47299c5SAlex Deucher 		/* core_bandwidth = sclk(Mhz) * 16 */
72768adac5eSBen Skeggs 		rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a);
728f47299c5SAlex Deucher 	}
729f47299c5SAlex Deucher }
730f47299c5SAlex Deucher 
7310c195119SAlex Deucher /**
7320c195119SAlex Deucher  * radeon_boot_test_post_card - check and possibly initialize the hw
7330c195119SAlex Deucher  *
7340c195119SAlex Deucher  * @rdev: radeon_device pointer
7350c195119SAlex Deucher  *
7360c195119SAlex Deucher  * Check if the asic is initialized and if not, attempt to initialize
7370c195119SAlex Deucher  * it (all asics).
7380c195119SAlex Deucher  * Returns true if initialized or false if not.
7390c195119SAlex Deucher  */
74072542d77SDave Airlie bool radeon_boot_test_post_card(struct radeon_device *rdev)
74172542d77SDave Airlie {
74272542d77SDave Airlie 	if (radeon_card_posted(rdev))
74372542d77SDave Airlie 		return true;
74472542d77SDave Airlie 
74572542d77SDave Airlie 	if (rdev->bios) {
74672542d77SDave Airlie 		DRM_INFO("GPU not posted. posting now...\n");
74772542d77SDave Airlie 		if (rdev->is_atom_bios)
74872542d77SDave Airlie 			atom_asic_init(rdev->mode_info.atom_context);
74972542d77SDave Airlie 		else
75072542d77SDave Airlie 			radeon_combios_asic_init(rdev->ddev);
75172542d77SDave Airlie 		return true;
75272542d77SDave Airlie 	} else {
75372542d77SDave Airlie 		dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
75472542d77SDave Airlie 		return false;
75572542d77SDave Airlie 	}
75672542d77SDave Airlie }
75772542d77SDave Airlie 
7580c195119SAlex Deucher /**
7590c195119SAlex Deucher  * radeon_dummy_page_init - init dummy page used by the driver
7600c195119SAlex Deucher  *
7610c195119SAlex Deucher  * @rdev: radeon_device pointer
7620c195119SAlex Deucher  *
7630c195119SAlex Deucher  * Allocate the dummy page used by the driver (all asics).
7640c195119SAlex Deucher  * This dummy page is used by the driver as a filler for gart entries
7650c195119SAlex Deucher  * when pages are taken out of the GART
7660c195119SAlex Deucher  * Returns 0 on sucess, -ENOMEM on failure.
7670c195119SAlex Deucher  */
7683ce0a23dSJerome Glisse int radeon_dummy_page_init(struct radeon_device *rdev)
7693ce0a23dSJerome Glisse {
77082568565SDave Airlie 	if (rdev->dummy_page.page)
77182568565SDave Airlie 		return 0;
7723ce0a23dSJerome Glisse 	rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
7733ce0a23dSJerome Glisse 	if (rdev->dummy_page.page == NULL)
7743ce0a23dSJerome Glisse 		return -ENOMEM;
7753ce0a23dSJerome Glisse 	rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page,
7763ce0a23dSJerome Glisse 					0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
777a30f6fb7SBenjamin Herrenschmidt 	if (pci_dma_mapping_error(rdev->pdev, rdev->dummy_page.addr)) {
778a30f6fb7SBenjamin Herrenschmidt 		dev_err(&rdev->pdev->dev, "Failed to DMA MAP the dummy page\n");
7793ce0a23dSJerome Glisse 		__free_page(rdev->dummy_page.page);
7803ce0a23dSJerome Glisse 		rdev->dummy_page.page = NULL;
7813ce0a23dSJerome Glisse 		return -ENOMEM;
7823ce0a23dSJerome Glisse 	}
783cb658906SMichel Dänzer 	rdev->dummy_page.entry = radeon_gart_get_page_entry(rdev->dummy_page.addr,
784cb658906SMichel Dänzer 							    RADEON_GART_PAGE_DUMMY);
7853ce0a23dSJerome Glisse 	return 0;
7863ce0a23dSJerome Glisse }
7873ce0a23dSJerome Glisse 
7880c195119SAlex Deucher /**
7890c195119SAlex Deucher  * radeon_dummy_page_fini - free dummy page used by the driver
7900c195119SAlex Deucher  *
7910c195119SAlex Deucher  * @rdev: radeon_device pointer
7920c195119SAlex Deucher  *
7930c195119SAlex Deucher  * Frees the dummy page used by the driver (all asics).
7940c195119SAlex Deucher  */
7953ce0a23dSJerome Glisse void radeon_dummy_page_fini(struct radeon_device *rdev)
7963ce0a23dSJerome Glisse {
7973ce0a23dSJerome Glisse 	if (rdev->dummy_page.page == NULL)
7983ce0a23dSJerome Glisse 		return;
7993ce0a23dSJerome Glisse 	pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
8003ce0a23dSJerome Glisse 			PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
8013ce0a23dSJerome Glisse 	__free_page(rdev->dummy_page.page);
8023ce0a23dSJerome Glisse 	rdev->dummy_page.page = NULL;
8033ce0a23dSJerome Glisse }
8043ce0a23dSJerome Glisse 
805771fe6b9SJerome Glisse 
806771fe6b9SJerome Glisse /* ATOM accessor methods */
8070c195119SAlex Deucher /*
8080c195119SAlex Deucher  * ATOM is an interpreted byte code stored in tables in the vbios.  The
8090c195119SAlex Deucher  * driver registers callbacks to access registers and the interpreter
8100c195119SAlex Deucher  * in the driver parses the tables and executes then to program specific
8110c195119SAlex Deucher  * actions (set display modes, asic init, etc.).  See radeon_atombios.c,
8120c195119SAlex Deucher  * atombios.h, and atom.c
8130c195119SAlex Deucher  */
8140c195119SAlex Deucher 
8150c195119SAlex Deucher /**
8160c195119SAlex Deucher  * cail_pll_read - read PLL register
8170c195119SAlex Deucher  *
8180c195119SAlex Deucher  * @info: atom card_info pointer
8190c195119SAlex Deucher  * @reg: PLL register offset
8200c195119SAlex Deucher  *
8210c195119SAlex Deucher  * Provides a PLL register accessor for the atom interpreter (r4xx+).
8220c195119SAlex Deucher  * Returns the value of the PLL register.
8230c195119SAlex Deucher  */
824771fe6b9SJerome Glisse static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
825771fe6b9SJerome Glisse {
826771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
827771fe6b9SJerome Glisse 	uint32_t r;
828771fe6b9SJerome Glisse 
829771fe6b9SJerome Glisse 	r = rdev->pll_rreg(rdev, reg);
830771fe6b9SJerome Glisse 	return r;
831771fe6b9SJerome Glisse }
832771fe6b9SJerome Glisse 
8330c195119SAlex Deucher /**
8340c195119SAlex Deucher  * cail_pll_write - write PLL register
8350c195119SAlex Deucher  *
8360c195119SAlex Deucher  * @info: atom card_info pointer
8370c195119SAlex Deucher  * @reg: PLL register offset
8380c195119SAlex Deucher  * @val: value to write to the pll register
8390c195119SAlex Deucher  *
8400c195119SAlex Deucher  * Provides a PLL register accessor for the atom interpreter (r4xx+).
8410c195119SAlex Deucher  */
842771fe6b9SJerome Glisse static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
843771fe6b9SJerome Glisse {
844771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
845771fe6b9SJerome Glisse 
846771fe6b9SJerome Glisse 	rdev->pll_wreg(rdev, reg, val);
847771fe6b9SJerome Glisse }
848771fe6b9SJerome Glisse 
8490c195119SAlex Deucher /**
8500c195119SAlex Deucher  * cail_mc_read - read MC (Memory Controller) register
8510c195119SAlex Deucher  *
8520c195119SAlex Deucher  * @info: atom card_info pointer
8530c195119SAlex Deucher  * @reg: MC register offset
8540c195119SAlex Deucher  *
8550c195119SAlex Deucher  * Provides an MC register accessor for the atom interpreter (r4xx+).
8560c195119SAlex Deucher  * Returns the value of the MC register.
8570c195119SAlex Deucher  */
858771fe6b9SJerome Glisse static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
859771fe6b9SJerome Glisse {
860771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
861771fe6b9SJerome Glisse 	uint32_t r;
862771fe6b9SJerome Glisse 
863771fe6b9SJerome Glisse 	r = rdev->mc_rreg(rdev, reg);
864771fe6b9SJerome Glisse 	return r;
865771fe6b9SJerome Glisse }
866771fe6b9SJerome Glisse 
8670c195119SAlex Deucher /**
8680c195119SAlex Deucher  * cail_mc_write - write MC (Memory Controller) register
8690c195119SAlex Deucher  *
8700c195119SAlex Deucher  * @info: atom card_info pointer
8710c195119SAlex Deucher  * @reg: MC register offset
8720c195119SAlex Deucher  * @val: value to write to the pll register
8730c195119SAlex Deucher  *
8740c195119SAlex Deucher  * Provides a MC register accessor for the atom interpreter (r4xx+).
8750c195119SAlex Deucher  */
876771fe6b9SJerome Glisse static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
877771fe6b9SJerome Glisse {
878771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
879771fe6b9SJerome Glisse 
880771fe6b9SJerome Glisse 	rdev->mc_wreg(rdev, reg, val);
881771fe6b9SJerome Glisse }
882771fe6b9SJerome Glisse 
8830c195119SAlex Deucher /**
8840c195119SAlex Deucher  * cail_reg_write - write MMIO register
8850c195119SAlex Deucher  *
8860c195119SAlex Deucher  * @info: atom card_info pointer
8870c195119SAlex Deucher  * @reg: MMIO register offset
8880c195119SAlex Deucher  * @val: value to write to the pll register
8890c195119SAlex Deucher  *
8900c195119SAlex Deucher  * Provides a MMIO register accessor for the atom interpreter (r4xx+).
8910c195119SAlex Deucher  */
892771fe6b9SJerome Glisse static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
893771fe6b9SJerome Glisse {
894771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
895771fe6b9SJerome Glisse 
896771fe6b9SJerome Glisse 	WREG32(reg*4, val);
897771fe6b9SJerome Glisse }
898771fe6b9SJerome Glisse 
8990c195119SAlex Deucher /**
9000c195119SAlex Deucher  * cail_reg_read - read MMIO register
9010c195119SAlex Deucher  *
9020c195119SAlex Deucher  * @info: atom card_info pointer
9030c195119SAlex Deucher  * @reg: MMIO register offset
9040c195119SAlex Deucher  *
9050c195119SAlex Deucher  * Provides an MMIO register accessor for the atom interpreter (r4xx+).
9060c195119SAlex Deucher  * Returns the value of the MMIO register.
9070c195119SAlex Deucher  */
908771fe6b9SJerome Glisse static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
909771fe6b9SJerome Glisse {
910771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
911771fe6b9SJerome Glisse 	uint32_t r;
912771fe6b9SJerome Glisse 
913771fe6b9SJerome Glisse 	r = RREG32(reg*4);
914771fe6b9SJerome Glisse 	return r;
915771fe6b9SJerome Glisse }
916771fe6b9SJerome Glisse 
9170c195119SAlex Deucher /**
9180c195119SAlex Deucher  * cail_ioreg_write - write IO register
9190c195119SAlex Deucher  *
9200c195119SAlex Deucher  * @info: atom card_info pointer
9210c195119SAlex Deucher  * @reg: IO register offset
9220c195119SAlex Deucher  * @val: value to write to the pll register
9230c195119SAlex Deucher  *
9240c195119SAlex Deucher  * Provides a IO register accessor for the atom interpreter (r4xx+).
9250c195119SAlex Deucher  */
926351a52a2SAlex Deucher static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
927351a52a2SAlex Deucher {
928351a52a2SAlex Deucher 	struct radeon_device *rdev = info->dev->dev_private;
929351a52a2SAlex Deucher 
930351a52a2SAlex Deucher 	WREG32_IO(reg*4, val);
931351a52a2SAlex Deucher }
932351a52a2SAlex Deucher 
9330c195119SAlex Deucher /**
9340c195119SAlex Deucher  * cail_ioreg_read - read IO register
9350c195119SAlex Deucher  *
9360c195119SAlex Deucher  * @info: atom card_info pointer
9370c195119SAlex Deucher  * @reg: IO register offset
9380c195119SAlex Deucher  *
9390c195119SAlex Deucher  * Provides an IO register accessor for the atom interpreter (r4xx+).
9400c195119SAlex Deucher  * Returns the value of the IO register.
9410c195119SAlex Deucher  */
942351a52a2SAlex Deucher static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
943351a52a2SAlex Deucher {
944351a52a2SAlex Deucher 	struct radeon_device *rdev = info->dev->dev_private;
945351a52a2SAlex Deucher 	uint32_t r;
946351a52a2SAlex Deucher 
947351a52a2SAlex Deucher 	r = RREG32_IO(reg*4);
948351a52a2SAlex Deucher 	return r;
949351a52a2SAlex Deucher }
950351a52a2SAlex Deucher 
9510c195119SAlex Deucher /**
9520c195119SAlex Deucher  * radeon_atombios_init - init the driver info and callbacks for atombios
9530c195119SAlex Deucher  *
9540c195119SAlex Deucher  * @rdev: radeon_device pointer
9550c195119SAlex Deucher  *
9560c195119SAlex Deucher  * Initializes the driver info and register access callbacks for the
9570c195119SAlex Deucher  * ATOM interpreter (r4xx+).
9580c195119SAlex Deucher  * Returns 0 on sucess, -ENOMEM on failure.
9590c195119SAlex Deucher  * Called at driver startup.
9600c195119SAlex Deucher  */
961771fe6b9SJerome Glisse int radeon_atombios_init(struct radeon_device *rdev)
962771fe6b9SJerome Glisse {
96361c4b24bSMathias Fröhlich 	struct card_info *atom_card_info =
96461c4b24bSMathias Fröhlich 	    kzalloc(sizeof(struct card_info), GFP_KERNEL);
96561c4b24bSMathias Fröhlich 
96661c4b24bSMathias Fröhlich 	if (!atom_card_info)
96761c4b24bSMathias Fröhlich 		return -ENOMEM;
96861c4b24bSMathias Fröhlich 
96961c4b24bSMathias Fröhlich 	rdev->mode_info.atom_card_info = atom_card_info;
97061c4b24bSMathias Fröhlich 	atom_card_info->dev = rdev->ddev;
97161c4b24bSMathias Fröhlich 	atom_card_info->reg_read = cail_reg_read;
97261c4b24bSMathias Fröhlich 	atom_card_info->reg_write = cail_reg_write;
973351a52a2SAlex Deucher 	/* needed for iio ops */
974351a52a2SAlex Deucher 	if (rdev->rio_mem) {
975351a52a2SAlex Deucher 		atom_card_info->ioreg_read = cail_ioreg_read;
976351a52a2SAlex Deucher 		atom_card_info->ioreg_write = cail_ioreg_write;
977351a52a2SAlex Deucher 	} else {
978351a52a2SAlex Deucher 		DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
979351a52a2SAlex Deucher 		atom_card_info->ioreg_read = cail_reg_read;
980351a52a2SAlex Deucher 		atom_card_info->ioreg_write = cail_reg_write;
981351a52a2SAlex Deucher 	}
98261c4b24bSMathias Fröhlich 	atom_card_info->mc_read = cail_mc_read;
98361c4b24bSMathias Fröhlich 	atom_card_info->mc_write = cail_mc_write;
98461c4b24bSMathias Fröhlich 	atom_card_info->pll_read = cail_pll_read;
98561c4b24bSMathias Fröhlich 	atom_card_info->pll_write = cail_pll_write;
98661c4b24bSMathias Fröhlich 
98761c4b24bSMathias Fröhlich 	rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
9880e34d094STim Gardner 	if (!rdev->mode_info.atom_context) {
9890e34d094STim Gardner 		radeon_atombios_fini(rdev);
9900e34d094STim Gardner 		return -ENOMEM;
9910e34d094STim Gardner 	}
9920e34d094STim Gardner 
993c31ad97fSRafał Miłecki 	mutex_init(&rdev->mode_info.atom_context->mutex);
9941c949842SDave Airlie 	mutex_init(&rdev->mode_info.atom_context->scratch_mutex);
995771fe6b9SJerome Glisse 	radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
996d904ef9bSDave Airlie 	atom_allocate_fb_scratch(rdev->mode_info.atom_context);
997771fe6b9SJerome Glisse 	return 0;
998771fe6b9SJerome Glisse }
999771fe6b9SJerome Glisse 
10000c195119SAlex Deucher /**
10010c195119SAlex Deucher  * radeon_atombios_fini - free the driver info and callbacks for atombios
10020c195119SAlex Deucher  *
10030c195119SAlex Deucher  * @rdev: radeon_device pointer
10040c195119SAlex Deucher  *
10050c195119SAlex Deucher  * Frees the driver info and register access callbacks for the ATOM
10060c195119SAlex Deucher  * interpreter (r4xx+).
10070c195119SAlex Deucher  * Called at driver shutdown.
10080c195119SAlex Deucher  */
1009771fe6b9SJerome Glisse void radeon_atombios_fini(struct radeon_device *rdev)
1010771fe6b9SJerome Glisse {
10114a04a844SJerome Glisse 	if (rdev->mode_info.atom_context) {
1012d904ef9bSDave Airlie 		kfree(rdev->mode_info.atom_context->scratch);
10134a04a844SJerome Glisse 	}
10140e34d094STim Gardner 	kfree(rdev->mode_info.atom_context);
10150e34d094STim Gardner 	rdev->mode_info.atom_context = NULL;
101661c4b24bSMathias Fröhlich 	kfree(rdev->mode_info.atom_card_info);
10170e34d094STim Gardner 	rdev->mode_info.atom_card_info = NULL;
1018771fe6b9SJerome Glisse }
1019771fe6b9SJerome Glisse 
10200c195119SAlex Deucher /* COMBIOS */
10210c195119SAlex Deucher /*
10220c195119SAlex Deucher  * COMBIOS is the bios format prior to ATOM. It provides
10230c195119SAlex Deucher  * command tables similar to ATOM, but doesn't have a unified
10240c195119SAlex Deucher  * parser.  See radeon_combios.c
10250c195119SAlex Deucher  */
10260c195119SAlex Deucher 
10270c195119SAlex Deucher /**
10280c195119SAlex Deucher  * radeon_combios_init - init the driver info for combios
10290c195119SAlex Deucher  *
10300c195119SAlex Deucher  * @rdev: radeon_device pointer
10310c195119SAlex Deucher  *
10320c195119SAlex Deucher  * Initializes the driver info for combios (r1xx-r3xx).
10330c195119SAlex Deucher  * Returns 0 on sucess.
10340c195119SAlex Deucher  * Called at driver startup.
10350c195119SAlex Deucher  */
1036771fe6b9SJerome Glisse int radeon_combios_init(struct radeon_device *rdev)
1037771fe6b9SJerome Glisse {
1038771fe6b9SJerome Glisse 	radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
1039771fe6b9SJerome Glisse 	return 0;
1040771fe6b9SJerome Glisse }
1041771fe6b9SJerome Glisse 
10420c195119SAlex Deucher /**
10430c195119SAlex Deucher  * radeon_combios_fini - free the driver info for combios
10440c195119SAlex Deucher  *
10450c195119SAlex Deucher  * @rdev: radeon_device pointer
10460c195119SAlex Deucher  *
10470c195119SAlex Deucher  * Frees the driver info for combios (r1xx-r3xx).
10480c195119SAlex Deucher  * Called at driver shutdown.
10490c195119SAlex Deucher  */
1050771fe6b9SJerome Glisse void radeon_combios_fini(struct radeon_device *rdev)
1051771fe6b9SJerome Glisse {
1052771fe6b9SJerome Glisse }
1053771fe6b9SJerome Glisse 
10540c195119SAlex Deucher /* if we get transitioned to only one device, take VGA back */
10550c195119SAlex Deucher /**
10560c195119SAlex Deucher  * radeon_vga_set_decode - enable/disable vga decode
10570c195119SAlex Deucher  *
10580c195119SAlex Deucher  * @cookie: radeon_device pointer
10590c195119SAlex Deucher  * @state: enable/disable vga decode
10600c195119SAlex Deucher  *
10610c195119SAlex Deucher  * Enable/disable vga decode (all asics).
10620c195119SAlex Deucher  * Returns VGA resource flags.
10630c195119SAlex Deucher  */
106428d52043SDave Airlie static unsigned int radeon_vga_set_decode(void *cookie, bool state)
106528d52043SDave Airlie {
106628d52043SDave Airlie 	struct radeon_device *rdev = cookie;
106728d52043SDave Airlie 	radeon_vga_set_state(rdev, state);
106828d52043SDave Airlie 	if (state)
106928d52043SDave Airlie 		return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
107028d52043SDave Airlie 		       VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
107128d52043SDave Airlie 	else
107228d52043SDave Airlie 		return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
107328d52043SDave Airlie }
1074c1176d6fSDave Airlie 
10750c195119SAlex Deucher /**
10761bcb04f7SChristian König  * radeon_check_pot_argument - check that argument is a power of two
10771bcb04f7SChristian König  *
10781bcb04f7SChristian König  * @arg: value to check
10791bcb04f7SChristian König  *
10801bcb04f7SChristian König  * Validates that a certain argument is a power of two (all asics).
10811bcb04f7SChristian König  * Returns true if argument is valid.
10821bcb04f7SChristian König  */
10831bcb04f7SChristian König static bool radeon_check_pot_argument(int arg)
10841bcb04f7SChristian König {
10851bcb04f7SChristian König 	return (arg & (arg - 1)) == 0;
10861bcb04f7SChristian König }
10871bcb04f7SChristian König 
10881bcb04f7SChristian König /**
10895e3c4f90SGrigori Goronzy  * Determine a sensible default GART size according to ASIC family.
10905e3c4f90SGrigori Goronzy  *
10915e3c4f90SGrigori Goronzy  * @family ASIC family name
10925e3c4f90SGrigori Goronzy  */
10935e3c4f90SGrigori Goronzy static int radeon_gart_size_auto(enum radeon_family family)
10945e3c4f90SGrigori Goronzy {
10955e3c4f90SGrigori Goronzy 	/* default to a larger gart size on newer asics */
10965e3c4f90SGrigori Goronzy 	if (family >= CHIP_TAHITI)
10975e3c4f90SGrigori Goronzy 		return 2048;
10985e3c4f90SGrigori Goronzy 	else if (family >= CHIP_RV770)
10995e3c4f90SGrigori Goronzy 		return 1024;
11005e3c4f90SGrigori Goronzy 	else
11015e3c4f90SGrigori Goronzy 		return 512;
11025e3c4f90SGrigori Goronzy }
11035e3c4f90SGrigori Goronzy 
11045e3c4f90SGrigori Goronzy /**
11050c195119SAlex Deucher  * radeon_check_arguments - validate module params
11060c195119SAlex Deucher  *
11070c195119SAlex Deucher  * @rdev: radeon_device pointer
11080c195119SAlex Deucher  *
11090c195119SAlex Deucher  * Validates certain module parameters and updates
11100c195119SAlex Deucher  * the associated values used by the driver (all asics).
11110c195119SAlex Deucher  */
11121109ca09SLauri Kasanen static void radeon_check_arguments(struct radeon_device *rdev)
111336421338SJerome Glisse {
111436421338SJerome Glisse 	/* vramlimit must be a power of two */
11151bcb04f7SChristian König 	if (!radeon_check_pot_argument(radeon_vram_limit)) {
111636421338SJerome Glisse 		dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n",
111736421338SJerome Glisse 				radeon_vram_limit);
111836421338SJerome Glisse 		radeon_vram_limit = 0;
111936421338SJerome Glisse 	}
11201bcb04f7SChristian König 
1121edcd26e8SAlex Deucher 	if (radeon_gart_size == -1) {
11225e3c4f90SGrigori Goronzy 		radeon_gart_size = radeon_gart_size_auto(rdev->family);
1123edcd26e8SAlex Deucher 	}
112436421338SJerome Glisse 	/* gtt size must be power of two and greater or equal to 32M */
11251bcb04f7SChristian König 	if (radeon_gart_size < 32) {
1126edcd26e8SAlex Deucher 		dev_warn(rdev->dev, "gart size (%d) too small\n",
112736421338SJerome Glisse 				radeon_gart_size);
11285e3c4f90SGrigori Goronzy 		radeon_gart_size = radeon_gart_size_auto(rdev->family);
11291bcb04f7SChristian König 	} else if (!radeon_check_pot_argument(radeon_gart_size)) {
113036421338SJerome Glisse 		dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
113136421338SJerome Glisse 				radeon_gart_size);
11325e3c4f90SGrigori Goronzy 		radeon_gart_size = radeon_gart_size_auto(rdev->family);
113336421338SJerome Glisse 	}
11341bcb04f7SChristian König 	rdev->mc.gtt_size = (uint64_t)radeon_gart_size << 20;
11351bcb04f7SChristian König 
113636421338SJerome Glisse 	/* AGP mode can only be -1, 1, 2, 4, 8 */
113736421338SJerome Glisse 	switch (radeon_agpmode) {
113836421338SJerome Glisse 	case -1:
113936421338SJerome Glisse 	case 0:
114036421338SJerome Glisse 	case 1:
114136421338SJerome Glisse 	case 2:
114236421338SJerome Glisse 	case 4:
114336421338SJerome Glisse 	case 8:
114436421338SJerome Glisse 		break;
114536421338SJerome Glisse 	default:
114636421338SJerome Glisse 		dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
114736421338SJerome Glisse 				"-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
114836421338SJerome Glisse 		radeon_agpmode = 0;
114936421338SJerome Glisse 		break;
115036421338SJerome Glisse 	}
1151c1c44132SChristian König 
1152c1c44132SChristian König 	if (!radeon_check_pot_argument(radeon_vm_size)) {
1153c1c44132SChristian König 		dev_warn(rdev->dev, "VM size (%d) must be a power of 2\n",
1154c1c44132SChristian König 			 radeon_vm_size);
115520b2656dSChristian König 		radeon_vm_size = 4;
1156c1c44132SChristian König 	}
1157c1c44132SChristian König 
115820b2656dSChristian König 	if (radeon_vm_size < 1) {
115913c240efSAlexandre Demers 		dev_warn(rdev->dev, "VM size (%d) too small, min is 1GB\n",
1160c1c44132SChristian König 			 radeon_vm_size);
116120b2656dSChristian König 		radeon_vm_size = 4;
1162c1c44132SChristian König 	}
1163c1c44132SChristian König 
1164c1c44132SChristian König 	/*
1165c1c44132SChristian König 	 * Max GPUVM size for Cayman, SI and CI are 40 bits.
1166c1c44132SChristian König 	 */
116720b2656dSChristian König 	if (radeon_vm_size > 1024) {
116820b2656dSChristian König 		dev_warn(rdev->dev, "VM size (%d) too large, max is 1TB\n",
1169c1c44132SChristian König 			 radeon_vm_size);
117020b2656dSChristian König 		radeon_vm_size = 4;
1171c1c44132SChristian König 	}
11724510fb98SChristian König 
11734510fb98SChristian König 	/* defines number of bits in page table versus page directory,
11744510fb98SChristian König 	 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
11754510fb98SChristian König 	 * page table and the remaining bits are in the page directory */
1176dfc230f9SChristian König 	if (radeon_vm_block_size == -1) {
1177dfc230f9SChristian König 
1178dfc230f9SChristian König 		/* Total bits covered by PD + PTs */
11798e66e134SAlex Deucher 		unsigned bits = ilog2(radeon_vm_size) + 18;
1180dfc230f9SChristian König 
1181dfc230f9SChristian König 		/* Make sure the PD is 4K in size up to 8GB address space.
1182dfc230f9SChristian König 		   Above that split equal between PD and PTs */
1183dfc230f9SChristian König 		if (radeon_vm_size <= 8)
1184dfc230f9SChristian König 			radeon_vm_block_size = bits - 9;
1185dfc230f9SChristian König 		else
1186dfc230f9SChristian König 			radeon_vm_block_size = (bits + 3) / 2;
1187dfc230f9SChristian König 
1188dfc230f9SChristian König 	} else if (radeon_vm_block_size < 9) {
118920b2656dSChristian König 		dev_warn(rdev->dev, "VM page table size (%d) too small\n",
11904510fb98SChristian König 			 radeon_vm_block_size);
11914510fb98SChristian König 		radeon_vm_block_size = 9;
11924510fb98SChristian König 	}
11934510fb98SChristian König 
11944510fb98SChristian König 	if (radeon_vm_block_size > 24 ||
119520b2656dSChristian König 	    (radeon_vm_size * 1024) < (1ull << radeon_vm_block_size)) {
119620b2656dSChristian König 		dev_warn(rdev->dev, "VM page table size (%d) too large\n",
11974510fb98SChristian König 			 radeon_vm_block_size);
11984510fb98SChristian König 		radeon_vm_block_size = 9;
11994510fb98SChristian König 	}
120036421338SJerome Glisse }
120136421338SJerome Glisse 
12020c195119SAlex Deucher /**
12030c195119SAlex Deucher  * radeon_switcheroo_set_state - set switcheroo state
12040c195119SAlex Deucher  *
12050c195119SAlex Deucher  * @pdev: pci dev pointer
12068e5de1d8SLukas Wunner  * @state: vga_switcheroo state
12070c195119SAlex Deucher  *
12080c195119SAlex Deucher  * Callback for the switcheroo driver.  Suspends or resumes the
12090c195119SAlex Deucher  * the asics before or after it is powered up using ACPI methods.
12100c195119SAlex Deucher  */
12116a9ee8afSDave Airlie static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
12126a9ee8afSDave Airlie {
12136a9ee8afSDave Airlie 	struct drm_device *dev = pci_get_drvdata(pdev);
12144807c5a8SAlex Deucher 	struct radeon_device *rdev = dev->dev_private;
121510ebc0bcSDave Airlie 
121690c4cde9SAlex Deucher 	if (radeon_is_px(dev) && state == VGA_SWITCHEROO_OFF)
121710ebc0bcSDave Airlie 		return;
121810ebc0bcSDave Airlie 
12196a9ee8afSDave Airlie 	if (state == VGA_SWITCHEROO_ON) {
1220d1f9809eSMaarten Lankhorst 		unsigned d3_delay = dev->pdev->d3_delay;
1221d1f9809eSMaarten Lankhorst 
12226a9ee8afSDave Airlie 		printk(KERN_INFO "radeon: switched on\n");
12236a9ee8afSDave Airlie 		/* don't suspend or resume card normally */
12245bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1225d1f9809eSMaarten Lankhorst 
12264807c5a8SAlex Deucher 		if (d3_delay < 20 && (rdev->px_quirk_flags & RADEON_PX_QUIRK_LONG_WAKEUP))
1227d1f9809eSMaarten Lankhorst 			dev->pdev->d3_delay = 20;
1228d1f9809eSMaarten Lankhorst 
122910ebc0bcSDave Airlie 		radeon_resume_kms(dev, true, true);
1230d1f9809eSMaarten Lankhorst 
1231d1f9809eSMaarten Lankhorst 		dev->pdev->d3_delay = d3_delay;
1232d1f9809eSMaarten Lankhorst 
12335bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_ON;
1234fbf81762SDave Airlie 		drm_kms_helper_poll_enable(dev);
12356a9ee8afSDave Airlie 	} else {
12366a9ee8afSDave Airlie 		printk(KERN_INFO "radeon: switched off\n");
1237fbf81762SDave Airlie 		drm_kms_helper_poll_disable(dev);
12385bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
123910ebc0bcSDave Airlie 		radeon_suspend_kms(dev, true, true);
12405bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_OFF;
12416a9ee8afSDave Airlie 	}
12426a9ee8afSDave Airlie }
12436a9ee8afSDave Airlie 
12440c195119SAlex Deucher /**
12450c195119SAlex Deucher  * radeon_switcheroo_can_switch - see if switcheroo state can change
12460c195119SAlex Deucher  *
12470c195119SAlex Deucher  * @pdev: pci dev pointer
12480c195119SAlex Deucher  *
12490c195119SAlex Deucher  * Callback for the switcheroo driver.  Check of the switcheroo
12500c195119SAlex Deucher  * state can be changed.
12510c195119SAlex Deucher  * Returns true if the state can be changed, false if not.
12520c195119SAlex Deucher  */
12536a9ee8afSDave Airlie static bool radeon_switcheroo_can_switch(struct pci_dev *pdev)
12546a9ee8afSDave Airlie {
12556a9ee8afSDave Airlie 	struct drm_device *dev = pci_get_drvdata(pdev);
12566a9ee8afSDave Airlie 
1257fc8fd40eSDaniel Vetter 	/*
1258fc8fd40eSDaniel Vetter 	 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1259fc8fd40eSDaniel Vetter 	 * locking inversion with the driver load path. And the access here is
1260fc8fd40eSDaniel Vetter 	 * completely racy anyway. So don't bother with locking for now.
1261fc8fd40eSDaniel Vetter 	 */
1262fc8fd40eSDaniel Vetter 	return dev->open_count == 0;
12636a9ee8afSDave Airlie }
12646a9ee8afSDave Airlie 
126526ec685fSTakashi Iwai static const struct vga_switcheroo_client_ops radeon_switcheroo_ops = {
126626ec685fSTakashi Iwai 	.set_gpu_state = radeon_switcheroo_set_state,
126726ec685fSTakashi Iwai 	.reprobe = NULL,
126826ec685fSTakashi Iwai 	.can_switch = radeon_switcheroo_can_switch,
126926ec685fSTakashi Iwai };
12706a9ee8afSDave Airlie 
12710c195119SAlex Deucher /**
12720c195119SAlex Deucher  * radeon_device_init - initialize the driver
12730c195119SAlex Deucher  *
12740c195119SAlex Deucher  * @rdev: radeon_device pointer
12750c195119SAlex Deucher  * @pdev: drm dev pointer
12760c195119SAlex Deucher  * @pdev: pci dev pointer
12770c195119SAlex Deucher  * @flags: driver flags
12780c195119SAlex Deucher  *
12790c195119SAlex Deucher  * Initializes the driver info and hw (all asics).
12800c195119SAlex Deucher  * Returns 0 for success or an error on failure.
12810c195119SAlex Deucher  * Called at driver startup.
12820c195119SAlex Deucher  */
1283771fe6b9SJerome Glisse int radeon_device_init(struct radeon_device *rdev,
1284771fe6b9SJerome Glisse 		       struct drm_device *ddev,
1285771fe6b9SJerome Glisse 		       struct pci_dev *pdev,
1286771fe6b9SJerome Glisse 		       uint32_t flags)
1287771fe6b9SJerome Glisse {
1288351a52a2SAlex Deucher 	int r, i;
1289ad49f501SDave Airlie 	int dma_bits;
129010ebc0bcSDave Airlie 	bool runtime = false;
1291771fe6b9SJerome Glisse 
1292771fe6b9SJerome Glisse 	rdev->shutdown = false;
12939f022ddfSJerome Glisse 	rdev->dev = &pdev->dev;
1294771fe6b9SJerome Glisse 	rdev->ddev = ddev;
1295771fe6b9SJerome Glisse 	rdev->pdev = pdev;
1296771fe6b9SJerome Glisse 	rdev->flags = flags;
1297771fe6b9SJerome Glisse 	rdev->family = flags & RADEON_FAMILY_MASK;
1298771fe6b9SJerome Glisse 	rdev->is_atom_bios = false;
1299771fe6b9SJerome Glisse 	rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
1300edcd26e8SAlex Deucher 	rdev->mc.gtt_size = 512 * 1024 * 1024;
1301733289c2SJerome Glisse 	rdev->accel_working = false;
13028b25ed34SAlex Deucher 	/* set up ring ids */
13038b25ed34SAlex Deucher 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
13048b25ed34SAlex Deucher 		rdev->ring[i].idx = i;
13058b25ed34SAlex Deucher 	}
1306954605caSMaarten Lankhorst 	rdev->fence_context = fence_context_alloc(RADEON_NUM_RINGS);
13071b5331d9SJerome Glisse 
1308*fe0d36e0SAlex Deucher 	DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1309d522d9ccSThomas Reim 		 radeon_family_name[rdev->family], pdev->vendor, pdev->device,
1310*fe0d36e0SAlex Deucher 		 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
13111b5331d9SJerome Glisse 
1312771fe6b9SJerome Glisse 	/* mutex initialization are all done here so we
1313771fe6b9SJerome Glisse 	 * can recall function without having locking issues */
1314d6999bc7SChristian König 	mutex_init(&rdev->ring_lock);
131540bacf16SAlex Deucher 	mutex_init(&rdev->dc_hw_i2c_mutex);
1316c20dc369SChristian Koenig 	atomic_set(&rdev->ih.lock, 0);
13174c788679SJerome Glisse 	mutex_init(&rdev->gem.mutex);
1318c913e23aSRafał Miłecki 	mutex_init(&rdev->pm.mutex);
13196759a0a7SMarek Olšák 	mutex_init(&rdev->gpu_clock_mutex);
1320f61d5b46SAlex Deucher 	mutex_init(&rdev->srbm_mutex);
13211c0a4625SOded Gabbay 	mutex_init(&rdev->grbm_idx_mutex);
1322db7fce39SChristian König 	init_rwsem(&rdev->pm.mclk_lock);
1323dee53e7fSJerome Glisse 	init_rwsem(&rdev->exclusive_lock);
132473a6d3fcSRafał Miłecki 	init_waitqueue_head(&rdev->irq.vblank_queue);
1325341cb9e4SChristian König 	mutex_init(&rdev->mn_lock);
1326341cb9e4SChristian König 	hash_init(rdev->mn_hash);
13271b9c3dd0SAlex Deucher 	r = radeon_gem_init(rdev);
13281b9c3dd0SAlex Deucher 	if (r)
13291b9c3dd0SAlex Deucher 		return r;
1330529364e0SChristian König 
1331c1c44132SChristian König 	radeon_check_arguments(rdev);
133223d4f1f2SAlex Deucher 	/* Adjust VM size here.
1333c1c44132SChristian König 	 * Max GPUVM size for cayman+ is 40 bits.
133423d4f1f2SAlex Deucher 	 */
133520b2656dSChristian König 	rdev->vm_manager.max_pfn = radeon_vm_size << 18;
1336771fe6b9SJerome Glisse 
13374aac0473SJerome Glisse 	/* Set asic functions */
13384aac0473SJerome Glisse 	r = radeon_asic_init(rdev);
133936421338SJerome Glisse 	if (r)
13404aac0473SJerome Glisse 		return r;
13414aac0473SJerome Glisse 
1342f95df9caSAlex Deucher 	/* all of the newer IGP chips have an internal gart
1343f95df9caSAlex Deucher 	 * However some rs4xx report as AGP, so remove that here.
1344f95df9caSAlex Deucher 	 */
1345f95df9caSAlex Deucher 	if ((rdev->family >= CHIP_RS400) &&
1346f95df9caSAlex Deucher 	    (rdev->flags & RADEON_IS_IGP)) {
1347f95df9caSAlex Deucher 		rdev->flags &= ~RADEON_IS_AGP;
1348f95df9caSAlex Deucher 	}
1349f95df9caSAlex Deucher 
135030256a3fSJerome Glisse 	if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
1351b574f251SJerome Glisse 		radeon_agp_disable(rdev);
1352771fe6b9SJerome Glisse 	}
1353771fe6b9SJerome Glisse 
13549ed8b1f9SAlex Deucher 	/* Set the internal MC address mask
13559ed8b1f9SAlex Deucher 	 * This is the max address of the GPU's
13569ed8b1f9SAlex Deucher 	 * internal address space.
13579ed8b1f9SAlex Deucher 	 */
13589ed8b1f9SAlex Deucher 	if (rdev->family >= CHIP_CAYMAN)
13599ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xffffffffffULL; /* 40 bit MC */
13609ed8b1f9SAlex Deucher 	else if (rdev->family >= CHIP_CEDAR)
13619ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xfffffffffULL; /* 36 bit MC */
13629ed8b1f9SAlex Deucher 	else
13639ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xffffffffULL; /* 32 bit MC */
13649ed8b1f9SAlex Deucher 
1365ad49f501SDave Airlie 	/* set DMA mask + need_dma32 flags.
1366ad49f501SDave Airlie 	 * PCIE - can handle 40-bits.
1367005a83f1SAlex Deucher 	 * IGP - can handle 40-bits
1368ad49f501SDave Airlie 	 * AGP - generally dma32 is safest
1369005a83f1SAlex Deucher 	 * PCI - dma32 for legacy pci gart, 40 bits on newer asics
1370ad49f501SDave Airlie 	 */
1371ad49f501SDave Airlie 	rdev->need_dma32 = false;
1372ad49f501SDave Airlie 	if (rdev->flags & RADEON_IS_AGP)
1373ad49f501SDave Airlie 		rdev->need_dma32 = true;
1374005a83f1SAlex Deucher 	if ((rdev->flags & RADEON_IS_PCI) &&
13754a2b6662SJerome Glisse 	    (rdev->family <= CHIP_RS740))
1376ad49f501SDave Airlie 		rdev->need_dma32 = true;
1377ad49f501SDave Airlie 
1378ad49f501SDave Airlie 	dma_bits = rdev->need_dma32 ? 32 : 40;
1379ad49f501SDave Airlie 	r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
1380771fe6b9SJerome Glisse 	if (r) {
138162fff811SDaniel Haid 		rdev->need_dma32 = true;
1382c52494f6SKonrad Rzeszutek Wilk 		dma_bits = 32;
1383771fe6b9SJerome Glisse 		printk(KERN_WARNING "radeon: No suitable DMA available.\n");
1384771fe6b9SJerome Glisse 	}
1385c52494f6SKonrad Rzeszutek Wilk 	r = pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
1386c52494f6SKonrad Rzeszutek Wilk 	if (r) {
1387c52494f6SKonrad Rzeszutek Wilk 		pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(32));
1388c52494f6SKonrad Rzeszutek Wilk 		printk(KERN_WARNING "radeon: No coherent DMA available.\n");
1389c52494f6SKonrad Rzeszutek Wilk 	}
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);
1413771fe6b9SJerome Glisse 	if (rdev->rmmio == NULL) {
1414771fe6b9SJerome Glisse 		return -ENOMEM;
1415771fe6b9SJerome Glisse 	}
1416771fe6b9SJerome Glisse 	DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base);
1417771fe6b9SJerome Glisse 	DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size);
1418771fe6b9SJerome Glisse 
141975efdee1SAlex Deucher 	/* doorbell bar mapping */
142075efdee1SAlex Deucher 	if (rdev->family >= CHIP_BONAIRE)
142175efdee1SAlex Deucher 		radeon_doorbell_init(rdev);
142275efdee1SAlex Deucher 
1423351a52a2SAlex Deucher 	/* io port mapping */
1424351a52a2SAlex Deucher 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1425351a52a2SAlex Deucher 		if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) {
1426351a52a2SAlex Deucher 			rdev->rio_mem_size = pci_resource_len(rdev->pdev, i);
1427351a52a2SAlex Deucher 			rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size);
1428351a52a2SAlex Deucher 			break;
1429351a52a2SAlex Deucher 		}
1430351a52a2SAlex Deucher 	}
1431351a52a2SAlex Deucher 	if (rdev->rio_mem == NULL)
1432351a52a2SAlex Deucher 		DRM_ERROR("Unable to find PCI I/O BAR\n");
1433351a52a2SAlex Deucher 
14344807c5a8SAlex Deucher 	if (rdev->flags & RADEON_IS_PX)
14354807c5a8SAlex Deucher 		radeon_device_handle_px_quirks(rdev);
14364807c5a8SAlex Deucher 
143728d52043SDave Airlie 	/* if we have > 1 VGA cards, then disable the radeon VGA resources */
143893239ea1SDave Airlie 	/* this will fail for cards that aren't VGA class devices, just
143993239ea1SDave Airlie 	 * ignore it */
144093239ea1SDave Airlie 	vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
144110ebc0bcSDave Airlie 
1442e64c952eSAlex Deucher 	if ((rdev->flags & RADEON_IS_PX) && radeon_has_atpx_dgpu_power_cntl())
144310ebc0bcSDave Airlie 		runtime = true;
144410ebc0bcSDave Airlie 	vga_switcheroo_register_client(rdev->pdev, &radeon_switcheroo_ops, runtime);
144510ebc0bcSDave Airlie 	if (runtime)
144610ebc0bcSDave Airlie 		vga_switcheroo_init_domain_pm_ops(rdev->dev, &rdev->vga_pm_domain);
144728d52043SDave Airlie 
14483ce0a23dSJerome Glisse 	r = radeon_init(rdev);
1449b574f251SJerome Glisse 	if (r)
14502e97140dSAlex Deucher 		goto failed;
1451b1e3a6d1SMichel Dänzer 
1452409851f4SJerome Glisse 	r = radeon_gem_debugfs_init(rdev);
1453409851f4SJerome Glisse 	if (r) {
1454409851f4SJerome Glisse 		DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1455409851f4SJerome Glisse 	}
1456409851f4SJerome Glisse 
14579843ead0SDave Airlie 	r = radeon_mst_debugfs_init(rdev);
14589843ead0SDave Airlie 	if (r) {
14599843ead0SDave Airlie 		DRM_ERROR("registering mst debugfs failed (%d).\n", r);
14609843ead0SDave Airlie 	}
14619843ead0SDave Airlie 
1462b574f251SJerome Glisse 	if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
1463b574f251SJerome Glisse 		/* Acceleration not working on AGP card try again
1464b574f251SJerome Glisse 		 * with fallback to PCI or PCIE GART
1465b574f251SJerome Glisse 		 */
1466a2d07b74SJerome Glisse 		radeon_asic_reset(rdev);
1467b574f251SJerome Glisse 		radeon_fini(rdev);
1468b574f251SJerome Glisse 		radeon_agp_disable(rdev);
1469b574f251SJerome Glisse 		r = radeon_init(rdev);
14704aac0473SJerome Glisse 		if (r)
14712e97140dSAlex Deucher 			goto failed;
14723ce0a23dSJerome Glisse 	}
14736c7bcceaSAlex Deucher 
147413a7d299SChristian König 	r = radeon_ib_ring_tests(rdev);
147513a7d299SChristian König 	if (r)
147613a7d299SChristian König 		DRM_ERROR("ib ring test failed (%d).\n", r);
147713a7d299SChristian König 
14786dfd1972SJérôme Glisse 	/*
14796dfd1972SJérôme Glisse 	 * Turks/Thames GPU will freeze whole laptop if DPM is not restarted
14806dfd1972SJérôme Glisse 	 * after the CP ring have chew one packet at least. Hence here we stop
14816dfd1972SJérôme Glisse 	 * and restart DPM after the radeon_ib_ring_tests().
14826dfd1972SJérôme Glisse 	 */
14836dfd1972SJérôme Glisse 	if (rdev->pm.dpm_enabled &&
14846dfd1972SJérôme Glisse 	    (rdev->pm.pm_method == PM_METHOD_DPM) &&
14856dfd1972SJérôme Glisse 	    (rdev->family == CHIP_TURKS) &&
14866dfd1972SJérôme Glisse 	    (rdev->flags & RADEON_IS_MOBILITY)) {
14876dfd1972SJérôme Glisse 		mutex_lock(&rdev->pm.mutex);
14886dfd1972SJérôme Glisse 		radeon_dpm_disable(rdev);
14896dfd1972SJérôme Glisse 		radeon_dpm_enable(rdev);
14906dfd1972SJérôme Glisse 		mutex_unlock(&rdev->pm.mutex);
14916dfd1972SJérôme Glisse 	}
14926dfd1972SJérôme Glisse 
149360a7e396SChristian König 	if ((radeon_testing & 1)) {
14944a1132a0SAlex Deucher 		if (rdev->accel_working)
1495ecc0b326SMichel Dänzer 			radeon_test_moves(rdev);
14964a1132a0SAlex Deucher 		else
14974a1132a0SAlex Deucher 			DRM_INFO("radeon: acceleration disabled, skipping move tests\n");
1498ecc0b326SMichel Dänzer 	}
149960a7e396SChristian König 	if ((radeon_testing & 2)) {
15004a1132a0SAlex Deucher 		if (rdev->accel_working)
150160a7e396SChristian König 			radeon_test_syncing(rdev);
15024a1132a0SAlex Deucher 		else
15034a1132a0SAlex Deucher 			DRM_INFO("radeon: acceleration disabled, skipping sync tests\n");
150460a7e396SChristian König 	}
1505771fe6b9SJerome Glisse 	if (radeon_benchmarking) {
15064a1132a0SAlex Deucher 		if (rdev->accel_working)
1507638dd7dbSIlija Hadzic 			radeon_benchmark(rdev, radeon_benchmarking);
15084a1132a0SAlex Deucher 		else
15094a1132a0SAlex Deucher 			DRM_INFO("radeon: acceleration disabled, skipping benchmarks\n");
1510771fe6b9SJerome Glisse 	}
15116cf8a3f5SJerome Glisse 	return 0;
15122e97140dSAlex Deucher 
15132e97140dSAlex Deucher failed:
15142e97140dSAlex Deucher 	if (runtime)
15152e97140dSAlex Deucher 		vga_switcheroo_fini_domain_pm_ops(rdev->dev);
15162e97140dSAlex Deucher 	return r;
1517771fe6b9SJerome Glisse }
1518771fe6b9SJerome Glisse 
15194d8bf9aeSChristian König static void radeon_debugfs_remove_files(struct radeon_device *rdev);
15204d8bf9aeSChristian König 
15210c195119SAlex Deucher /**
15220c195119SAlex Deucher  * radeon_device_fini - tear down the driver
15230c195119SAlex Deucher  *
15240c195119SAlex Deucher  * @rdev: radeon_device pointer
15250c195119SAlex Deucher  *
15260c195119SAlex Deucher  * Tear down the driver info (all asics).
15270c195119SAlex Deucher  * Called at driver shutdown.
15280c195119SAlex Deucher  */
1529771fe6b9SJerome Glisse void radeon_device_fini(struct radeon_device *rdev)
1530771fe6b9SJerome Glisse {
1531771fe6b9SJerome Glisse 	DRM_INFO("radeon: finishing device.\n");
1532771fe6b9SJerome Glisse 	rdev->shutdown = true;
153390aca4d2SJerome Glisse 	/* evict vram memory */
153490aca4d2SJerome Glisse 	radeon_bo_evict_vram(rdev);
15353ce0a23dSJerome Glisse 	radeon_fini(rdev);
15366a9ee8afSDave Airlie 	vga_switcheroo_unregister_client(rdev->pdev);
15372e97140dSAlex Deucher 	if (rdev->flags & RADEON_IS_PX)
15382e97140dSAlex Deucher 		vga_switcheroo_fini_domain_pm_ops(rdev->dev);
1539c1176d6fSDave Airlie 	vga_client_register(rdev->pdev, NULL, NULL, NULL);
1540e0a2ca73SAlex Deucher 	if (rdev->rio_mem)
1541351a52a2SAlex Deucher 		pci_iounmap(rdev->pdev, rdev->rio_mem);
1542351a52a2SAlex Deucher 	rdev->rio_mem = NULL;
1543771fe6b9SJerome Glisse 	iounmap(rdev->rmmio);
1544771fe6b9SJerome Glisse 	rdev->rmmio = NULL;
154575efdee1SAlex Deucher 	if (rdev->family >= CHIP_BONAIRE)
154675efdee1SAlex Deucher 		radeon_doorbell_fini(rdev);
15474d8bf9aeSChristian König 	radeon_debugfs_remove_files(rdev);
1548771fe6b9SJerome Glisse }
1549771fe6b9SJerome Glisse 
1550771fe6b9SJerome Glisse 
1551771fe6b9SJerome Glisse /*
1552771fe6b9SJerome Glisse  * Suspend & resume.
1553771fe6b9SJerome Glisse  */
15540c195119SAlex Deucher /**
15550c195119SAlex Deucher  * radeon_suspend_kms - initiate device suspend
15560c195119SAlex Deucher  *
15570c195119SAlex Deucher  * @pdev: drm dev pointer
15580c195119SAlex Deucher  * @state: suspend state
15590c195119SAlex Deucher  *
15600c195119SAlex Deucher  * Puts the hw in the suspend state (all asics).
15610c195119SAlex Deucher  * Returns 0 for success or an error on failure.
15620c195119SAlex Deucher  * Called at driver suspend.
15630c195119SAlex Deucher  */
156410ebc0bcSDave Airlie int radeon_suspend_kms(struct drm_device *dev, bool suspend, bool fbcon)
1565771fe6b9SJerome Glisse {
1566875c1866SDarren Jenkins 	struct radeon_device *rdev;
1567771fe6b9SJerome Glisse 	struct drm_crtc *crtc;
1568d8dcaa1dSAlex Deucher 	struct drm_connector *connector;
15697465280cSAlex Deucher 	int i, r;
1570771fe6b9SJerome Glisse 
1571875c1866SDarren Jenkins 	if (dev == NULL || dev->dev_private == NULL) {
1572771fe6b9SJerome Glisse 		return -ENODEV;
1573771fe6b9SJerome Glisse 	}
15747473e830SDave Airlie 
1575875c1866SDarren Jenkins 	rdev = dev->dev_private;
1576875c1866SDarren Jenkins 
15775bcf719bSDave Airlie 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
15786a9ee8afSDave Airlie 		return 0;
1579d8dcaa1dSAlex Deucher 
158086698c20SSeth Forshee 	drm_kms_helper_poll_disable(dev);
158186698c20SSeth Forshee 
15826adaed5bSDaniel Vetter 	drm_modeset_lock_all(dev);
1583d8dcaa1dSAlex Deucher 	/* turn off display hw */
1584d8dcaa1dSAlex Deucher 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1585d8dcaa1dSAlex Deucher 		drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1586d8dcaa1dSAlex Deucher 	}
15876adaed5bSDaniel Vetter 	drm_modeset_unlock_all(dev);
1588d8dcaa1dSAlex Deucher 
1589f3cbb17bSGrigori Goronzy 	/* unpin the front buffers and cursors */
1590771fe6b9SJerome Glisse 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1591f3cbb17bSGrigori Goronzy 		struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1592f4510a27SMatt Roper 		struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->primary->fb);
15934c788679SJerome Glisse 		struct radeon_bo *robj;
1594771fe6b9SJerome Glisse 
1595f3cbb17bSGrigori Goronzy 		if (radeon_crtc->cursor_bo) {
1596f3cbb17bSGrigori Goronzy 			struct radeon_bo *robj = gem_to_radeon_bo(radeon_crtc->cursor_bo);
1597f3cbb17bSGrigori Goronzy 			r = radeon_bo_reserve(robj, false);
1598f3cbb17bSGrigori Goronzy 			if (r == 0) {
1599f3cbb17bSGrigori Goronzy 				radeon_bo_unpin(robj);
1600f3cbb17bSGrigori Goronzy 				radeon_bo_unreserve(robj);
1601f3cbb17bSGrigori Goronzy 			}
1602f3cbb17bSGrigori Goronzy 		}
1603f3cbb17bSGrigori Goronzy 
1604771fe6b9SJerome Glisse 		if (rfb == NULL || rfb->obj == NULL) {
1605771fe6b9SJerome Glisse 			continue;
1606771fe6b9SJerome Glisse 		}
16077e4d15d9SDaniel Vetter 		robj = gem_to_radeon_bo(rfb->obj);
160838651674SDave Airlie 		/* don't unpin kernel fb objects */
160938651674SDave Airlie 		if (!radeon_fbdev_robj_is_fb(rdev, robj)) {
16104c788679SJerome Glisse 			r = radeon_bo_reserve(robj, false);
161138651674SDave Airlie 			if (r == 0) {
16124c788679SJerome Glisse 				radeon_bo_unpin(robj);
16134c788679SJerome Glisse 				radeon_bo_unreserve(robj);
16144c788679SJerome Glisse 			}
1615771fe6b9SJerome Glisse 		}
1616771fe6b9SJerome Glisse 	}
1617771fe6b9SJerome Glisse 	/* evict vram memory */
16184c788679SJerome Glisse 	radeon_bo_evict_vram(rdev);
16198a47cc9eSChristian König 
1620771fe6b9SJerome Glisse 	/* wait for gpu to finish processing current batch */
16215f8f635eSJerome Glisse 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
162237615527SChristian König 		r = radeon_fence_wait_empty(rdev, i);
16235f8f635eSJerome Glisse 		if (r) {
16245f8f635eSJerome Glisse 			/* delay GPU reset to resume */
1625eb98c709SChristian König 			radeon_fence_driver_force_completion(rdev, i);
16265f8f635eSJerome Glisse 		}
16275f8f635eSJerome Glisse 	}
1628771fe6b9SJerome Glisse 
1629f657c2a7SYang Zhao 	radeon_save_bios_scratch_regs(rdev);
1630f657c2a7SYang Zhao 
16313ce0a23dSJerome Glisse 	radeon_suspend(rdev);
1632d4877cf2SAlex Deucher 	radeon_hpd_fini(rdev);
1633771fe6b9SJerome Glisse 	/* evict remaining vram memory */
16344c788679SJerome Glisse 	radeon_bo_evict_vram(rdev);
1635771fe6b9SJerome Glisse 
163610b06122SJerome Glisse 	radeon_agp_suspend(rdev);
163710b06122SJerome Glisse 
1638771fe6b9SJerome Glisse 	pci_save_state(dev->pdev);
16397473e830SDave Airlie 	if (suspend) {
1640771fe6b9SJerome Glisse 		/* Shut down the device */
1641771fe6b9SJerome Glisse 		pci_disable_device(dev->pdev);
1642771fe6b9SJerome Glisse 		pci_set_power_state(dev->pdev, PCI_D3hot);
1643771fe6b9SJerome Glisse 	}
164410ebc0bcSDave Airlie 
164510ebc0bcSDave Airlie 	if (fbcon) {
1646ac751efaSTorben Hohn 		console_lock();
164738651674SDave Airlie 		radeon_fbdev_set_suspend(rdev, 1);
1648ac751efaSTorben Hohn 		console_unlock();
164910ebc0bcSDave Airlie 	}
1650771fe6b9SJerome Glisse 	return 0;
1651771fe6b9SJerome Glisse }
1652771fe6b9SJerome Glisse 
16530c195119SAlex Deucher /**
16540c195119SAlex Deucher  * radeon_resume_kms - initiate device resume
16550c195119SAlex Deucher  *
16560c195119SAlex Deucher  * @pdev: drm dev pointer
16570c195119SAlex Deucher  *
16580c195119SAlex Deucher  * Bring the hw back to operating state (all asics).
16590c195119SAlex Deucher  * Returns 0 for success or an error on failure.
16600c195119SAlex Deucher  * Called at driver resume.
16610c195119SAlex Deucher  */
166210ebc0bcSDave Airlie int radeon_resume_kms(struct drm_device *dev, bool resume, bool fbcon)
1663771fe6b9SJerome Glisse {
166409bdf591SCedric Godin 	struct drm_connector *connector;
1665771fe6b9SJerome Glisse 	struct radeon_device *rdev = dev->dev_private;
1666f3cbb17bSGrigori Goronzy 	struct drm_crtc *crtc;
166704eb2206SChristian König 	int r;
1668771fe6b9SJerome Glisse 
16695bcf719bSDave Airlie 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
16706a9ee8afSDave Airlie 		return 0;
16716a9ee8afSDave Airlie 
167210ebc0bcSDave Airlie 	if (fbcon) {
1673ac751efaSTorben Hohn 		console_lock();
167410ebc0bcSDave Airlie 	}
16757473e830SDave Airlie 	if (resume) {
1676771fe6b9SJerome Glisse 		pci_set_power_state(dev->pdev, PCI_D0);
1677771fe6b9SJerome Glisse 		pci_restore_state(dev->pdev);
1678771fe6b9SJerome Glisse 		if (pci_enable_device(dev->pdev)) {
167910ebc0bcSDave Airlie 			if (fbcon)
1680ac751efaSTorben Hohn 				console_unlock();
1681771fe6b9SJerome Glisse 			return -1;
1682771fe6b9SJerome Glisse 		}
16837473e830SDave Airlie 	}
16840ebf1717SDave Airlie 	/* resume AGP if in use */
16850ebf1717SDave Airlie 	radeon_agp_resume(rdev);
16863ce0a23dSJerome Glisse 	radeon_resume(rdev);
168704eb2206SChristian König 
168804eb2206SChristian König 	r = radeon_ib_ring_tests(rdev);
168904eb2206SChristian König 	if (r)
169004eb2206SChristian König 		DRM_ERROR("ib ring test failed (%d).\n", r);
169104eb2206SChristian König 
1692bc6a6295SAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
16936c7bcceaSAlex Deucher 		/* do dpm late init */
16946c7bcceaSAlex Deucher 		r = radeon_pm_late_init(rdev);
16956c7bcceaSAlex Deucher 		if (r) {
16966c7bcceaSAlex Deucher 			rdev->pm.dpm_enabled = false;
16976c7bcceaSAlex Deucher 			DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
16986c7bcceaSAlex Deucher 		}
1699bc6a6295SAlex Deucher 	} else {
1700bc6a6295SAlex Deucher 		/* resume old pm late */
1701bc6a6295SAlex Deucher 		radeon_pm_resume(rdev);
17026c7bcceaSAlex Deucher 	}
17036c7bcceaSAlex Deucher 
1704f657c2a7SYang Zhao 	radeon_restore_bios_scratch_regs(rdev);
170509bdf591SCedric Godin 
1706f3cbb17bSGrigori Goronzy 	/* pin cursors */
1707f3cbb17bSGrigori Goronzy 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1708f3cbb17bSGrigori Goronzy 		struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1709f3cbb17bSGrigori Goronzy 
1710f3cbb17bSGrigori Goronzy 		if (radeon_crtc->cursor_bo) {
1711f3cbb17bSGrigori Goronzy 			struct radeon_bo *robj = gem_to_radeon_bo(radeon_crtc->cursor_bo);
1712f3cbb17bSGrigori Goronzy 			r = radeon_bo_reserve(robj, false);
1713f3cbb17bSGrigori Goronzy 			if (r == 0) {
1714f3cbb17bSGrigori Goronzy 				/* Only 27 bit offset for legacy cursor */
1715f3cbb17bSGrigori Goronzy 				r = radeon_bo_pin_restricted(robj,
1716f3cbb17bSGrigori Goronzy 							     RADEON_GEM_DOMAIN_VRAM,
1717f3cbb17bSGrigori Goronzy 							     ASIC_IS_AVIVO(rdev) ?
1718f3cbb17bSGrigori Goronzy 							     0 : 1 << 27,
1719f3cbb17bSGrigori Goronzy 							     &radeon_crtc->cursor_addr);
1720f3cbb17bSGrigori Goronzy 				if (r != 0)
1721f3cbb17bSGrigori Goronzy 					DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
1722f3cbb17bSGrigori Goronzy 				radeon_bo_unreserve(robj);
1723f3cbb17bSGrigori Goronzy 			}
1724f3cbb17bSGrigori Goronzy 		}
1725f3cbb17bSGrigori Goronzy 	}
1726f3cbb17bSGrigori Goronzy 
17273fa47d9eSAlex Deucher 	/* init dig PHYs, disp eng pll */
17283fa47d9eSAlex Deucher 	if (rdev->is_atom_bios) {
1729ac89af1eSAlex Deucher 		radeon_atom_encoder_init(rdev);
1730f3f1f03eSAlex Deucher 		radeon_atom_disp_eng_pll_init(rdev);
1731bced76f2SAlex Deucher 		/* turn on the BL */
1732bced76f2SAlex Deucher 		if (rdev->mode_info.bl_encoder) {
1733bced76f2SAlex Deucher 			u8 bl_level = radeon_get_backlight_level(rdev,
1734bced76f2SAlex Deucher 								 rdev->mode_info.bl_encoder);
1735bced76f2SAlex Deucher 			radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
1736bced76f2SAlex Deucher 						   bl_level);
1737bced76f2SAlex Deucher 		}
17383fa47d9eSAlex Deucher 	}
1739d4877cf2SAlex Deucher 	/* reset hpd state */
1740d4877cf2SAlex Deucher 	radeon_hpd_init(rdev);
1741771fe6b9SJerome Glisse 	/* blat the mode back in */
1742ec9954fcSDave Airlie 	if (fbcon) {
1743771fe6b9SJerome Glisse 		drm_helper_resume_force_mode(dev);
1744a93f344dSAlex Deucher 		/* turn on display hw */
17456adaed5bSDaniel Vetter 		drm_modeset_lock_all(dev);
1746a93f344dSAlex Deucher 		list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1747a93f344dSAlex Deucher 			drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
1748a93f344dSAlex Deucher 		}
17496adaed5bSDaniel Vetter 		drm_modeset_unlock_all(dev);
1750ec9954fcSDave Airlie 	}
175186698c20SSeth Forshee 
175286698c20SSeth Forshee 	drm_kms_helper_poll_enable(dev);
175318ee37a4SDaniel Vetter 
17543640da2fSAlex Deucher 	/* set the power state here in case we are a PX system or headless */
17553640da2fSAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled)
17563640da2fSAlex Deucher 		radeon_pm_compute_clocks(rdev);
17573640da2fSAlex Deucher 
175818ee37a4SDaniel Vetter 	if (fbcon) {
175918ee37a4SDaniel Vetter 		radeon_fbdev_set_suspend(rdev, 0);
176018ee37a4SDaniel Vetter 		console_unlock();
176118ee37a4SDaniel Vetter 	}
176218ee37a4SDaniel Vetter 
1763771fe6b9SJerome Glisse 	return 0;
1764771fe6b9SJerome Glisse }
1765771fe6b9SJerome Glisse 
17660c195119SAlex Deucher /**
17670c195119SAlex Deucher  * radeon_gpu_reset - reset the asic
17680c195119SAlex Deucher  *
17690c195119SAlex Deucher  * @rdev: radeon device pointer
17700c195119SAlex Deucher  *
17710c195119SAlex Deucher  * Attempt the reset the GPU if it has hung (all asics).
17720c195119SAlex Deucher  * Returns 0 for success or an error on failure.
17730c195119SAlex Deucher  */
177490aca4d2SJerome Glisse int radeon_gpu_reset(struct radeon_device *rdev)
177590aca4d2SJerome Glisse {
177655d7c221SChristian König 	unsigned ring_sizes[RADEON_NUM_RINGS];
177755d7c221SChristian König 	uint32_t *ring_data[RADEON_NUM_RINGS];
177855d7c221SChristian König 
177955d7c221SChristian König 	bool saved = false;
178055d7c221SChristian König 
178155d7c221SChristian König 	int i, r;
17828fd1b84cSDave Airlie 	int resched;
178390aca4d2SJerome Glisse 
1784dee53e7fSJerome Glisse 	down_write(&rdev->exclusive_lock);
1785f9eaf9aeSChristian König 
1786f9eaf9aeSChristian König 	if (!rdev->needs_reset) {
1787f9eaf9aeSChristian König 		up_write(&rdev->exclusive_lock);
1788f9eaf9aeSChristian König 		return 0;
1789f9eaf9aeSChristian König 	}
1790f9eaf9aeSChristian König 
179172b9076bSMarek Olšák 	atomic_inc(&rdev->gpu_reset_counter);
179272b9076bSMarek Olšák 
179390aca4d2SJerome Glisse 	radeon_save_bios_scratch_regs(rdev);
17948fd1b84cSDave Airlie 	/* block TTM */
17958fd1b84cSDave Airlie 	resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
179690aca4d2SJerome Glisse 	radeon_suspend(rdev);
179773ef0e0dSAlex Deucher 	radeon_hpd_fini(rdev);
179890aca4d2SJerome Glisse 
179955d7c221SChristian König 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
180055d7c221SChristian König 		ring_sizes[i] = radeon_ring_backup(rdev, &rdev->ring[i],
180155d7c221SChristian König 						   &ring_data[i]);
180255d7c221SChristian König 		if (ring_sizes[i]) {
180355d7c221SChristian König 			saved = true;
180455d7c221SChristian König 			dev_info(rdev->dev, "Saved %d dwords of commands "
180555d7c221SChristian König 				 "on ring %d.\n", ring_sizes[i], i);
180655d7c221SChristian König 		}
180755d7c221SChristian König 	}
180855d7c221SChristian König 
180990aca4d2SJerome Glisse 	r = radeon_asic_reset(rdev);
181090aca4d2SJerome Glisse 	if (!r) {
181155d7c221SChristian König 		dev_info(rdev->dev, "GPU reset succeeded, trying to resume\n");
181290aca4d2SJerome Glisse 		radeon_resume(rdev);
181355d7c221SChristian König 	}
181404eb2206SChristian König 
181590aca4d2SJerome Glisse 	radeon_restore_bios_scratch_regs(rdev);
181655d7c221SChristian König 
181755d7c221SChristian König 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
18189bb39ff4SMaarten Lankhorst 		if (!r && ring_data[i]) {
181955d7c221SChristian König 			radeon_ring_restore(rdev, &rdev->ring[i],
182055d7c221SChristian König 					    ring_sizes[i], ring_data[i]);
182155d7c221SChristian König 		} else {
1822eb98c709SChristian König 			radeon_fence_driver_force_completion(rdev, i);
182355d7c221SChristian König 			kfree(ring_data[i]);
182455d7c221SChristian König 		}
182555d7c221SChristian König 	}
182655d7c221SChristian König 
1827c940b447SAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
1828c940b447SAlex Deucher 		/* do dpm late init */
1829c940b447SAlex Deucher 		r = radeon_pm_late_init(rdev);
1830c940b447SAlex Deucher 		if (r) {
1831c940b447SAlex Deucher 			rdev->pm.dpm_enabled = false;
1832c940b447SAlex Deucher 			DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
1833c940b447SAlex Deucher 		}
1834c940b447SAlex Deucher 	} else {
1835c940b447SAlex Deucher 		/* resume old pm late */
183695f59509SAlex Deucher 		radeon_pm_resume(rdev);
1837c940b447SAlex Deucher 	}
1838c940b447SAlex Deucher 
183973ef0e0dSAlex Deucher 	/* init dig PHYs, disp eng pll */
184073ef0e0dSAlex Deucher 	if (rdev->is_atom_bios) {
184173ef0e0dSAlex Deucher 		radeon_atom_encoder_init(rdev);
184273ef0e0dSAlex Deucher 		radeon_atom_disp_eng_pll_init(rdev);
184373ef0e0dSAlex Deucher 		/* turn on the BL */
184473ef0e0dSAlex Deucher 		if (rdev->mode_info.bl_encoder) {
184573ef0e0dSAlex Deucher 			u8 bl_level = radeon_get_backlight_level(rdev,
184673ef0e0dSAlex Deucher 								 rdev->mode_info.bl_encoder);
184773ef0e0dSAlex Deucher 			radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
184873ef0e0dSAlex Deucher 						   bl_level);
184973ef0e0dSAlex Deucher 		}
185073ef0e0dSAlex Deucher 	}
185173ef0e0dSAlex Deucher 	/* reset hpd state */
185273ef0e0dSAlex Deucher 	radeon_hpd_init(rdev);
185373ef0e0dSAlex Deucher 
18549bb39ff4SMaarten Lankhorst 	ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
18553c036389SChristian König 
18563c036389SChristian König 	rdev->in_reset = true;
18573c036389SChristian König 	rdev->needs_reset = false;
18583c036389SChristian König 
18599bb39ff4SMaarten Lankhorst 	downgrade_write(&rdev->exclusive_lock);
18609bb39ff4SMaarten Lankhorst 
1861d3493574SJerome Glisse 	drm_helper_resume_force_mode(rdev->ddev);
1862d3493574SJerome Glisse 
1863c940b447SAlex Deucher 	/* set the power state here in case we are a PX system or headless */
1864c940b447SAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled)
1865c940b447SAlex Deucher 		radeon_pm_compute_clocks(rdev);
1866c940b447SAlex Deucher 
18679bb39ff4SMaarten Lankhorst 	if (!r) {
18689bb39ff4SMaarten Lankhorst 		r = radeon_ib_ring_tests(rdev);
18699bb39ff4SMaarten Lankhorst 		if (r && saved)
18709bb39ff4SMaarten Lankhorst 			r = -EAGAIN;
18719bb39ff4SMaarten Lankhorst 	} else {
187290aca4d2SJerome Glisse 		/* bad news, how to tell it to userspace ? */
187390aca4d2SJerome Glisse 		dev_info(rdev->dev, "GPU reset failed\n");
18747a1619b9SMichel Dänzer 	}
18757a1619b9SMichel Dänzer 
18769bb39ff4SMaarten Lankhorst 	rdev->needs_reset = r == -EAGAIN;
18779bb39ff4SMaarten Lankhorst 	rdev->in_reset = false;
18789bb39ff4SMaarten Lankhorst 
18799bb39ff4SMaarten Lankhorst 	up_read(&rdev->exclusive_lock);
188090aca4d2SJerome Glisse 	return r;
188190aca4d2SJerome Glisse }
188290aca4d2SJerome Glisse 
1883771fe6b9SJerome Glisse 
1884771fe6b9SJerome Glisse /*
1885771fe6b9SJerome Glisse  * Debugfs
1886771fe6b9SJerome Glisse  */
1887771fe6b9SJerome Glisse int radeon_debugfs_add_files(struct radeon_device *rdev,
1888771fe6b9SJerome Glisse 			     struct drm_info_list *files,
1889771fe6b9SJerome Glisse 			     unsigned nfiles)
1890771fe6b9SJerome Glisse {
1891771fe6b9SJerome Glisse 	unsigned i;
1892771fe6b9SJerome Glisse 
18934d8bf9aeSChristian König 	for (i = 0; i < rdev->debugfs_count; i++) {
18944d8bf9aeSChristian König 		if (rdev->debugfs[i].files == files) {
1895771fe6b9SJerome Glisse 			/* Already registered */
1896771fe6b9SJerome Glisse 			return 0;
1897771fe6b9SJerome Glisse 		}
1898771fe6b9SJerome Glisse 	}
1899c245cb9eSMichael Witten 
19004d8bf9aeSChristian König 	i = rdev->debugfs_count + 1;
1901c245cb9eSMichael Witten 	if (i > RADEON_DEBUGFS_MAX_COMPONENTS) {
1902c245cb9eSMichael Witten 		DRM_ERROR("Reached maximum number of debugfs components.\n");
1903c245cb9eSMichael Witten 		DRM_ERROR("Report so we increase "
1904c245cb9eSMichael Witten 			  "RADEON_DEBUGFS_MAX_COMPONENTS.\n");
1905771fe6b9SJerome Glisse 		return -EINVAL;
1906771fe6b9SJerome Glisse 	}
19074d8bf9aeSChristian König 	rdev->debugfs[rdev->debugfs_count].files = files;
19084d8bf9aeSChristian König 	rdev->debugfs[rdev->debugfs_count].num_files = nfiles;
19094d8bf9aeSChristian König 	rdev->debugfs_count = i;
1910771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS)
1911771fe6b9SJerome Glisse 	drm_debugfs_create_files(files, nfiles,
1912771fe6b9SJerome Glisse 				 rdev->ddev->control->debugfs_root,
1913771fe6b9SJerome Glisse 				 rdev->ddev->control);
1914771fe6b9SJerome Glisse 	drm_debugfs_create_files(files, nfiles,
1915771fe6b9SJerome Glisse 				 rdev->ddev->primary->debugfs_root,
1916771fe6b9SJerome Glisse 				 rdev->ddev->primary);
1917771fe6b9SJerome Glisse #endif
1918771fe6b9SJerome Glisse 	return 0;
1919771fe6b9SJerome Glisse }
1920771fe6b9SJerome Glisse 
19214d8bf9aeSChristian König static void radeon_debugfs_remove_files(struct radeon_device *rdev)
19224d8bf9aeSChristian König {
19234d8bf9aeSChristian König #if defined(CONFIG_DEBUG_FS)
19244d8bf9aeSChristian König 	unsigned i;
19254d8bf9aeSChristian König 
19264d8bf9aeSChristian König 	for (i = 0; i < rdev->debugfs_count; i++) {
19274d8bf9aeSChristian König 		drm_debugfs_remove_files(rdev->debugfs[i].files,
19284d8bf9aeSChristian König 					 rdev->debugfs[i].num_files,
19294d8bf9aeSChristian König 					 rdev->ddev->control);
19304d8bf9aeSChristian König 		drm_debugfs_remove_files(rdev->debugfs[i].files,
19314d8bf9aeSChristian König 					 rdev->debugfs[i].num_files,
19324d8bf9aeSChristian König 					 rdev->ddev->primary);
19334d8bf9aeSChristian König 	}
19344d8bf9aeSChristian König #endif
19354d8bf9aeSChristian König }
19364d8bf9aeSChristian König 
1937771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS)
1938771fe6b9SJerome Glisse int radeon_debugfs_init(struct drm_minor *minor)
1939771fe6b9SJerome Glisse {
1940771fe6b9SJerome Glisse 	return 0;
1941771fe6b9SJerome Glisse }
1942771fe6b9SJerome Glisse 
1943771fe6b9SJerome Glisse void radeon_debugfs_cleanup(struct drm_minor *minor)
1944771fe6b9SJerome Glisse {
1945771fe6b9SJerome Glisse }
1946771fe6b9SJerome Glisse #endif
1947