xref: /openbmc/linux/drivers/gpu/drm/radeon/radeon_device.c (revision ccaa2c12fba72f3e547d18e66820e2e6c5883113)
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 
1064807c5a8SAlex Deucher #define RADEON_PX_QUIRK_DISABLE_PX  (1 << 0)
1074807c5a8SAlex Deucher #define RADEON_PX_QUIRK_LONG_WAKEUP (1 << 1)
1084807c5a8SAlex Deucher 
1094807c5a8SAlex Deucher struct radeon_px_quirk {
1104807c5a8SAlex Deucher 	u32 chip_vendor;
1114807c5a8SAlex Deucher 	u32 chip_device;
1124807c5a8SAlex Deucher 	u32 subsys_vendor;
1134807c5a8SAlex Deucher 	u32 subsys_device;
1144807c5a8SAlex Deucher 	u32 px_quirk_flags;
1154807c5a8SAlex Deucher };
1164807c5a8SAlex Deucher 
1174807c5a8SAlex Deucher static struct radeon_px_quirk radeon_px_quirk_list[] = {
1184807c5a8SAlex Deucher 	/* Acer aspire 5560g (CPU: AMD A4-3305M; GPU: AMD Radeon HD 6480g + 7470m)
1194807c5a8SAlex Deucher 	 * https://bugzilla.kernel.org/show_bug.cgi?id=74551
1204807c5a8SAlex Deucher 	 */
1214807c5a8SAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6760, 0x1025, 0x0672, RADEON_PX_QUIRK_DISABLE_PX },
1224807c5a8SAlex Deucher 	/* Asus K73TA laptop with AMD A6-3400M APU and Radeon 6550 GPU
1234807c5a8SAlex Deucher 	 * https://bugzilla.kernel.org/show_bug.cgi?id=51381
1244807c5a8SAlex Deucher 	 */
1254807c5a8SAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6741, 0x1043, 0x108c, RADEON_PX_QUIRK_DISABLE_PX },
126ff1b1294SAlex Deucher 	/* Asus K53TK laptop with AMD A6-3420M APU and Radeon 7670m GPU
127ff1b1294SAlex Deucher 	 * https://bugzilla.kernel.org/show_bug.cgi?id=51381
128ff1b1294SAlex Deucher 	 */
129ff1b1294SAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6840, 0x1043, 0x2122, RADEON_PX_QUIRK_DISABLE_PX },
1304807c5a8SAlex Deucher 	/* macbook pro 8.2 */
1314807c5a8SAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6741, PCI_VENDOR_ID_APPLE, 0x00e2, RADEON_PX_QUIRK_LONG_WAKEUP },
1324807c5a8SAlex Deucher 	{ 0, 0, 0, 0, 0 },
1334807c5a8SAlex Deucher };
1344807c5a8SAlex Deucher 
13590c4cde9SAlex Deucher bool radeon_is_px(struct drm_device *dev)
13690c4cde9SAlex Deucher {
13790c4cde9SAlex Deucher 	struct radeon_device *rdev = dev->dev_private;
13890c4cde9SAlex Deucher 
13990c4cde9SAlex Deucher 	if (rdev->flags & RADEON_IS_PX)
14090c4cde9SAlex Deucher 		return true;
14190c4cde9SAlex Deucher 	return false;
14290c4cde9SAlex Deucher }
14310ebc0bcSDave Airlie 
1444807c5a8SAlex Deucher static void radeon_device_handle_px_quirks(struct radeon_device *rdev)
1454807c5a8SAlex Deucher {
1464807c5a8SAlex Deucher 	struct radeon_px_quirk *p = radeon_px_quirk_list;
1474807c5a8SAlex Deucher 
1484807c5a8SAlex Deucher 	/* Apply PX quirks */
1494807c5a8SAlex Deucher 	while (p && p->chip_device != 0) {
1504807c5a8SAlex Deucher 		if (rdev->pdev->vendor == p->chip_vendor &&
1514807c5a8SAlex Deucher 		    rdev->pdev->device == p->chip_device &&
1524807c5a8SAlex Deucher 		    rdev->pdev->subsystem_vendor == p->subsys_vendor &&
1534807c5a8SAlex Deucher 		    rdev->pdev->subsystem_device == p->subsys_device) {
1544807c5a8SAlex Deucher 			rdev->px_quirk_flags = p->px_quirk_flags;
1554807c5a8SAlex Deucher 			break;
1564807c5a8SAlex Deucher 		}
1574807c5a8SAlex Deucher 		++p;
1584807c5a8SAlex Deucher 	}
1594807c5a8SAlex Deucher 
1604807c5a8SAlex Deucher 	if (rdev->px_quirk_flags & RADEON_PX_QUIRK_DISABLE_PX)
1614807c5a8SAlex Deucher 		rdev->flags &= ~RADEON_IS_PX;
1624807c5a8SAlex Deucher }
1634807c5a8SAlex Deucher 
1640c195119SAlex Deucher /**
1652e1b65f9SAlex Deucher  * radeon_program_register_sequence - program an array of registers.
1662e1b65f9SAlex Deucher  *
1672e1b65f9SAlex Deucher  * @rdev: radeon_device pointer
1682e1b65f9SAlex Deucher  * @registers: pointer to the register array
1692e1b65f9SAlex Deucher  * @array_size: size of the register array
1702e1b65f9SAlex Deucher  *
1712e1b65f9SAlex Deucher  * Programs an array or registers with and and or masks.
1722e1b65f9SAlex Deucher  * This is a helper for setting golden registers.
1732e1b65f9SAlex Deucher  */
1742e1b65f9SAlex Deucher void radeon_program_register_sequence(struct radeon_device *rdev,
1752e1b65f9SAlex Deucher 				      const u32 *registers,
1762e1b65f9SAlex Deucher 				      const u32 array_size)
1772e1b65f9SAlex Deucher {
1782e1b65f9SAlex Deucher 	u32 tmp, reg, and_mask, or_mask;
1792e1b65f9SAlex Deucher 	int i;
1802e1b65f9SAlex Deucher 
1812e1b65f9SAlex Deucher 	if (array_size % 3)
1822e1b65f9SAlex Deucher 		return;
1832e1b65f9SAlex Deucher 
1842e1b65f9SAlex Deucher 	for (i = 0; i < array_size; i +=3) {
1852e1b65f9SAlex Deucher 		reg = registers[i + 0];
1862e1b65f9SAlex Deucher 		and_mask = registers[i + 1];
1872e1b65f9SAlex Deucher 		or_mask = registers[i + 2];
1882e1b65f9SAlex Deucher 
1892e1b65f9SAlex Deucher 		if (and_mask == 0xffffffff) {
1902e1b65f9SAlex Deucher 			tmp = or_mask;
1912e1b65f9SAlex Deucher 		} else {
1922e1b65f9SAlex Deucher 			tmp = RREG32(reg);
1932e1b65f9SAlex Deucher 			tmp &= ~and_mask;
1942e1b65f9SAlex Deucher 			tmp |= or_mask;
1952e1b65f9SAlex Deucher 		}
1962e1b65f9SAlex Deucher 		WREG32(reg, tmp);
1972e1b65f9SAlex Deucher 	}
1982e1b65f9SAlex Deucher }
1992e1b65f9SAlex Deucher 
2001a0041b8SAlex Deucher void radeon_pci_config_reset(struct radeon_device *rdev)
2011a0041b8SAlex Deucher {
2021a0041b8SAlex Deucher 	pci_write_config_dword(rdev->pdev, 0x7c, RADEON_ASIC_RESET_DATA);
2031a0041b8SAlex Deucher }
2041a0041b8SAlex Deucher 
2052e1b65f9SAlex Deucher /**
2060c195119SAlex Deucher  * radeon_surface_init - Clear GPU surface registers.
2070c195119SAlex Deucher  *
2080c195119SAlex Deucher  * @rdev: radeon_device pointer
2090c195119SAlex Deucher  *
2100c195119SAlex Deucher  * Clear GPU surface registers (r1xx-r5xx).
211b1e3a6d1SMichel Dänzer  */
2123ce0a23dSJerome Glisse void radeon_surface_init(struct radeon_device *rdev)
213b1e3a6d1SMichel Dänzer {
214b1e3a6d1SMichel Dänzer 	/* FIXME: check this out */
215b1e3a6d1SMichel Dänzer 	if (rdev->family < CHIP_R600) {
216b1e3a6d1SMichel Dänzer 		int i;
217b1e3a6d1SMichel Dänzer 
218550e2d92SDave Airlie 		for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
219550e2d92SDave Airlie 			if (rdev->surface_regs[i].bo)
220550e2d92SDave Airlie 				radeon_bo_get_surface_reg(rdev->surface_regs[i].bo);
221550e2d92SDave Airlie 			else
222550e2d92SDave Airlie 				radeon_clear_surface_reg(rdev, i);
223b1e3a6d1SMichel Dänzer 		}
224e024e110SDave Airlie 		/* enable surfaces */
225e024e110SDave Airlie 		WREG32(RADEON_SURFACE_CNTL, 0);
226b1e3a6d1SMichel Dänzer 	}
227b1e3a6d1SMichel Dänzer }
228b1e3a6d1SMichel Dänzer 
229b1e3a6d1SMichel Dänzer /*
230771fe6b9SJerome Glisse  * GPU scratch registers helpers function.
231771fe6b9SJerome Glisse  */
2320c195119SAlex Deucher /**
2330c195119SAlex Deucher  * radeon_scratch_init - Init scratch register driver information.
2340c195119SAlex Deucher  *
2350c195119SAlex Deucher  * @rdev: radeon_device pointer
2360c195119SAlex Deucher  *
2370c195119SAlex Deucher  * Init CP scratch register driver information (r1xx-r5xx)
2380c195119SAlex Deucher  */
2393ce0a23dSJerome Glisse void radeon_scratch_init(struct radeon_device *rdev)
240771fe6b9SJerome Glisse {
241771fe6b9SJerome Glisse 	int i;
242771fe6b9SJerome Glisse 
243771fe6b9SJerome Glisse 	/* FIXME: check this out */
244771fe6b9SJerome Glisse 	if (rdev->family < CHIP_R300) {
245771fe6b9SJerome Glisse 		rdev->scratch.num_reg = 5;
246771fe6b9SJerome Glisse 	} else {
247771fe6b9SJerome Glisse 		rdev->scratch.num_reg = 7;
248771fe6b9SJerome Glisse 	}
249724c80e1SAlex Deucher 	rdev->scratch.reg_base = RADEON_SCRATCH_REG0;
250771fe6b9SJerome Glisse 	for (i = 0; i < rdev->scratch.num_reg; i++) {
251771fe6b9SJerome Glisse 		rdev->scratch.free[i] = true;
252724c80e1SAlex Deucher 		rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4);
253771fe6b9SJerome Glisse 	}
254771fe6b9SJerome Glisse }
255771fe6b9SJerome Glisse 
2560c195119SAlex Deucher /**
2570c195119SAlex Deucher  * radeon_scratch_get - Allocate a scratch register
2580c195119SAlex Deucher  *
2590c195119SAlex Deucher  * @rdev: radeon_device pointer
2600c195119SAlex Deucher  * @reg: scratch register mmio offset
2610c195119SAlex Deucher  *
2620c195119SAlex Deucher  * Allocate a CP scratch register for use by the driver (all asics).
2630c195119SAlex Deucher  * Returns 0 on success or -EINVAL on failure.
2640c195119SAlex Deucher  */
265771fe6b9SJerome Glisse int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg)
266771fe6b9SJerome Glisse {
267771fe6b9SJerome Glisse 	int i;
268771fe6b9SJerome Glisse 
269771fe6b9SJerome Glisse 	for (i = 0; i < rdev->scratch.num_reg; i++) {
270771fe6b9SJerome Glisse 		if (rdev->scratch.free[i]) {
271771fe6b9SJerome Glisse 			rdev->scratch.free[i] = false;
272771fe6b9SJerome Glisse 			*reg = rdev->scratch.reg[i];
273771fe6b9SJerome Glisse 			return 0;
274771fe6b9SJerome Glisse 		}
275771fe6b9SJerome Glisse 	}
276771fe6b9SJerome Glisse 	return -EINVAL;
277771fe6b9SJerome Glisse }
278771fe6b9SJerome Glisse 
2790c195119SAlex Deucher /**
2800c195119SAlex Deucher  * radeon_scratch_free - Free a scratch register
2810c195119SAlex Deucher  *
2820c195119SAlex Deucher  * @rdev: radeon_device pointer
2830c195119SAlex Deucher  * @reg: scratch register mmio offset
2840c195119SAlex Deucher  *
2850c195119SAlex Deucher  * Free a CP scratch register allocated for use by the driver (all asics)
2860c195119SAlex Deucher  */
287771fe6b9SJerome Glisse void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg)
288771fe6b9SJerome Glisse {
289771fe6b9SJerome Glisse 	int i;
290771fe6b9SJerome Glisse 
291771fe6b9SJerome Glisse 	for (i = 0; i < rdev->scratch.num_reg; i++) {
292771fe6b9SJerome Glisse 		if (rdev->scratch.reg[i] == reg) {
293771fe6b9SJerome Glisse 			rdev->scratch.free[i] = true;
294771fe6b9SJerome Glisse 			return;
295771fe6b9SJerome Glisse 		}
296771fe6b9SJerome Glisse 	}
297771fe6b9SJerome Glisse }
298771fe6b9SJerome Glisse 
2990c195119SAlex Deucher /*
30075efdee1SAlex Deucher  * GPU doorbell aperture helpers function.
30175efdee1SAlex Deucher  */
30275efdee1SAlex Deucher /**
30375efdee1SAlex Deucher  * radeon_doorbell_init - Init doorbell driver information.
30475efdee1SAlex Deucher  *
30575efdee1SAlex Deucher  * @rdev: radeon_device pointer
30675efdee1SAlex Deucher  *
30775efdee1SAlex Deucher  * Init doorbell driver information (CIK)
30875efdee1SAlex Deucher  * Returns 0 on success, error on failure.
30975efdee1SAlex Deucher  */
31028f5a6cdSRashika Kheria static int radeon_doorbell_init(struct radeon_device *rdev)
31175efdee1SAlex Deucher {
31275efdee1SAlex Deucher 	/* doorbell bar mapping */
31375efdee1SAlex Deucher 	rdev->doorbell.base = pci_resource_start(rdev->pdev, 2);
31475efdee1SAlex Deucher 	rdev->doorbell.size = pci_resource_len(rdev->pdev, 2);
31575efdee1SAlex Deucher 
316d5754ab8SAndrew Lewycky 	rdev->doorbell.num_doorbells = min_t(u32, rdev->doorbell.size / sizeof(u32), RADEON_MAX_DOORBELLS);
317d5754ab8SAndrew Lewycky 	if (rdev->doorbell.num_doorbells == 0)
318d5754ab8SAndrew Lewycky 		return -EINVAL;
31975efdee1SAlex Deucher 
320d5754ab8SAndrew Lewycky 	rdev->doorbell.ptr = ioremap(rdev->doorbell.base, rdev->doorbell.num_doorbells * sizeof(u32));
32175efdee1SAlex Deucher 	if (rdev->doorbell.ptr == NULL) {
32275efdee1SAlex Deucher 		return -ENOMEM;
32375efdee1SAlex Deucher 	}
32475efdee1SAlex Deucher 	DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)rdev->doorbell.base);
32575efdee1SAlex Deucher 	DRM_INFO("doorbell mmio size: %u\n", (unsigned)rdev->doorbell.size);
32675efdee1SAlex Deucher 
327d5754ab8SAndrew Lewycky 	memset(&rdev->doorbell.used, 0, sizeof(rdev->doorbell.used));
32875efdee1SAlex Deucher 
32975efdee1SAlex Deucher 	return 0;
33075efdee1SAlex Deucher }
33175efdee1SAlex Deucher 
33275efdee1SAlex Deucher /**
33375efdee1SAlex Deucher  * radeon_doorbell_fini - Tear down doorbell driver information.
33475efdee1SAlex Deucher  *
33575efdee1SAlex Deucher  * @rdev: radeon_device pointer
33675efdee1SAlex Deucher  *
33775efdee1SAlex Deucher  * Tear down doorbell driver information (CIK)
33875efdee1SAlex Deucher  */
33928f5a6cdSRashika Kheria static void radeon_doorbell_fini(struct radeon_device *rdev)
34075efdee1SAlex Deucher {
34175efdee1SAlex Deucher 	iounmap(rdev->doorbell.ptr);
34275efdee1SAlex Deucher 	rdev->doorbell.ptr = NULL;
34375efdee1SAlex Deucher }
34475efdee1SAlex Deucher 
34575efdee1SAlex Deucher /**
346d5754ab8SAndrew Lewycky  * radeon_doorbell_get - Allocate a doorbell entry
34775efdee1SAlex Deucher  *
34875efdee1SAlex Deucher  * @rdev: radeon_device pointer
349d5754ab8SAndrew Lewycky  * @doorbell: doorbell index
35075efdee1SAlex Deucher  *
351d5754ab8SAndrew Lewycky  * Allocate a doorbell for use by the driver (all asics).
35275efdee1SAlex Deucher  * Returns 0 on success or -EINVAL on failure.
35375efdee1SAlex Deucher  */
35475efdee1SAlex Deucher int radeon_doorbell_get(struct radeon_device *rdev, u32 *doorbell)
35575efdee1SAlex Deucher {
356d5754ab8SAndrew Lewycky 	unsigned long offset = find_first_zero_bit(rdev->doorbell.used, rdev->doorbell.num_doorbells);
357d5754ab8SAndrew Lewycky 	if (offset < rdev->doorbell.num_doorbells) {
358d5754ab8SAndrew Lewycky 		__set_bit(offset, rdev->doorbell.used);
359d5754ab8SAndrew Lewycky 		*doorbell = offset;
36075efdee1SAlex Deucher 		return 0;
361d5754ab8SAndrew Lewycky 	} else {
36275efdee1SAlex Deucher 		return -EINVAL;
36375efdee1SAlex Deucher 	}
364d5754ab8SAndrew Lewycky }
36575efdee1SAlex Deucher 
36675efdee1SAlex Deucher /**
367d5754ab8SAndrew Lewycky  * radeon_doorbell_free - Free a doorbell entry
36875efdee1SAlex Deucher  *
36975efdee1SAlex Deucher  * @rdev: radeon_device pointer
370d5754ab8SAndrew Lewycky  * @doorbell: doorbell index
37175efdee1SAlex Deucher  *
372d5754ab8SAndrew Lewycky  * Free a doorbell allocated for use by the driver (all asics)
37375efdee1SAlex Deucher  */
37475efdee1SAlex Deucher void radeon_doorbell_free(struct radeon_device *rdev, u32 doorbell)
37575efdee1SAlex Deucher {
376d5754ab8SAndrew Lewycky 	if (doorbell < rdev->doorbell.num_doorbells)
377d5754ab8SAndrew Lewycky 		__clear_bit(doorbell, rdev->doorbell.used);
37875efdee1SAlex Deucher }
37975efdee1SAlex Deucher 
380ebff8453SOded Gabbay /**
381ebff8453SOded Gabbay  * radeon_doorbell_get_kfd_info - Report doorbell configuration required to
382ebff8453SOded Gabbay  *                                setup KFD
383ebff8453SOded Gabbay  *
384ebff8453SOded Gabbay  * @rdev: radeon_device pointer
385ebff8453SOded Gabbay  * @aperture_base: output returning doorbell aperture base physical address
386ebff8453SOded Gabbay  * @aperture_size: output returning doorbell aperture size in bytes
387ebff8453SOded Gabbay  * @start_offset: output returning # of doorbell bytes reserved for radeon.
388ebff8453SOded Gabbay  *
389ebff8453SOded Gabbay  * Radeon and the KFD share the doorbell aperture. Radeon sets it up,
390ebff8453SOded Gabbay  * takes doorbells required for its own rings and reports the setup to KFD.
391ebff8453SOded Gabbay  * Radeon reserved doorbells are at the start of the doorbell aperture.
392ebff8453SOded Gabbay  */
393ebff8453SOded Gabbay void radeon_doorbell_get_kfd_info(struct radeon_device *rdev,
394ebff8453SOded Gabbay 				  phys_addr_t *aperture_base,
395ebff8453SOded Gabbay 				  size_t *aperture_size,
396ebff8453SOded Gabbay 				  size_t *start_offset)
397ebff8453SOded Gabbay {
398ebff8453SOded Gabbay 	/* The first num_doorbells are used by radeon.
399ebff8453SOded Gabbay 	 * KFD takes whatever's left in the aperture. */
400ebff8453SOded Gabbay 	if (rdev->doorbell.size > rdev->doorbell.num_doorbells * sizeof(u32)) {
401ebff8453SOded Gabbay 		*aperture_base = rdev->doorbell.base;
402ebff8453SOded Gabbay 		*aperture_size = rdev->doorbell.size;
403ebff8453SOded Gabbay 		*start_offset = rdev->doorbell.num_doorbells * sizeof(u32);
404ebff8453SOded Gabbay 	} else {
405ebff8453SOded Gabbay 		*aperture_base = 0;
406ebff8453SOded Gabbay 		*aperture_size = 0;
407ebff8453SOded Gabbay 		*start_offset = 0;
408ebff8453SOded Gabbay 	}
409ebff8453SOded Gabbay }
410ebff8453SOded Gabbay 
41175efdee1SAlex Deucher /*
4120c195119SAlex Deucher  * radeon_wb_*()
4130c195119SAlex Deucher  * Writeback is the the method by which the the GPU updates special pages
4140c195119SAlex Deucher  * in memory with the status of certain GPU events (fences, ring pointers,
4150c195119SAlex Deucher  * etc.).
4160c195119SAlex Deucher  */
4170c195119SAlex Deucher 
4180c195119SAlex Deucher /**
4190c195119SAlex Deucher  * radeon_wb_disable - Disable Writeback
4200c195119SAlex Deucher  *
4210c195119SAlex Deucher  * @rdev: radeon_device pointer
4220c195119SAlex Deucher  *
4230c195119SAlex Deucher  * Disables Writeback (all asics).  Used for suspend.
4240c195119SAlex Deucher  */
425724c80e1SAlex Deucher void radeon_wb_disable(struct radeon_device *rdev)
426724c80e1SAlex Deucher {
427724c80e1SAlex Deucher 	rdev->wb.enabled = false;
428724c80e1SAlex Deucher }
429724c80e1SAlex Deucher 
4300c195119SAlex Deucher /**
4310c195119SAlex Deucher  * radeon_wb_fini - Disable Writeback and free memory
4320c195119SAlex Deucher  *
4330c195119SAlex Deucher  * @rdev: radeon_device pointer
4340c195119SAlex Deucher  *
4350c195119SAlex Deucher  * Disables Writeback and frees the Writeback memory (all asics).
4360c195119SAlex Deucher  * Used at driver shutdown.
4370c195119SAlex Deucher  */
438724c80e1SAlex Deucher void radeon_wb_fini(struct radeon_device *rdev)
439724c80e1SAlex Deucher {
440724c80e1SAlex Deucher 	radeon_wb_disable(rdev);
441724c80e1SAlex Deucher 	if (rdev->wb.wb_obj) {
442089920f2SJerome Glisse 		if (!radeon_bo_reserve(rdev->wb.wb_obj, false)) {
443089920f2SJerome Glisse 			radeon_bo_kunmap(rdev->wb.wb_obj);
444089920f2SJerome Glisse 			radeon_bo_unpin(rdev->wb.wb_obj);
445089920f2SJerome Glisse 			radeon_bo_unreserve(rdev->wb.wb_obj);
446089920f2SJerome Glisse 		}
447724c80e1SAlex Deucher 		radeon_bo_unref(&rdev->wb.wb_obj);
448724c80e1SAlex Deucher 		rdev->wb.wb = NULL;
449724c80e1SAlex Deucher 		rdev->wb.wb_obj = NULL;
450724c80e1SAlex Deucher 	}
451724c80e1SAlex Deucher }
452724c80e1SAlex Deucher 
4530c195119SAlex Deucher /**
4540c195119SAlex Deucher  * radeon_wb_init- Init Writeback driver info and allocate memory
4550c195119SAlex Deucher  *
4560c195119SAlex Deucher  * @rdev: radeon_device pointer
4570c195119SAlex Deucher  *
4580c195119SAlex Deucher  * Disables Writeback and frees the Writeback memory (all asics).
4590c195119SAlex Deucher  * Used at driver startup.
4600c195119SAlex Deucher  * Returns 0 on success or an -error on failure.
4610c195119SAlex Deucher  */
462724c80e1SAlex Deucher int radeon_wb_init(struct radeon_device *rdev)
463724c80e1SAlex Deucher {
464724c80e1SAlex Deucher 	int r;
465724c80e1SAlex Deucher 
466724c80e1SAlex Deucher 	if (rdev->wb.wb_obj == NULL) {
467441921d5SDaniel Vetter 		r = radeon_bo_create(rdev, RADEON_GPU_PAGE_SIZE, PAGE_SIZE, true,
468831b6966SMaarten Lankhorst 				     RADEON_GEM_DOMAIN_GTT, 0, NULL, NULL,
46902376d82SMichel Dänzer 				     &rdev->wb.wb_obj);
470724c80e1SAlex Deucher 		if (r) {
471724c80e1SAlex Deucher 			dev_warn(rdev->dev, "(%d) create WB bo failed\n", r);
472724c80e1SAlex Deucher 			return r;
473724c80e1SAlex Deucher 		}
474724c80e1SAlex Deucher 		r = radeon_bo_reserve(rdev->wb.wb_obj, false);
475724c80e1SAlex Deucher 		if (unlikely(r != 0)) {
476724c80e1SAlex Deucher 			radeon_wb_fini(rdev);
477724c80e1SAlex Deucher 			return r;
478724c80e1SAlex Deucher 		}
479724c80e1SAlex Deucher 		r = radeon_bo_pin(rdev->wb.wb_obj, RADEON_GEM_DOMAIN_GTT,
480724c80e1SAlex Deucher 				&rdev->wb.gpu_addr);
481724c80e1SAlex Deucher 		if (r) {
482724c80e1SAlex Deucher 			radeon_bo_unreserve(rdev->wb.wb_obj);
483724c80e1SAlex Deucher 			dev_warn(rdev->dev, "(%d) pin WB bo failed\n", r);
484724c80e1SAlex Deucher 			radeon_wb_fini(rdev);
485724c80e1SAlex Deucher 			return r;
486724c80e1SAlex Deucher 		}
487724c80e1SAlex Deucher 		r = radeon_bo_kmap(rdev->wb.wb_obj, (void **)&rdev->wb.wb);
488724c80e1SAlex Deucher 		radeon_bo_unreserve(rdev->wb.wb_obj);
489724c80e1SAlex Deucher 		if (r) {
490724c80e1SAlex Deucher 			dev_warn(rdev->dev, "(%d) map WB bo failed\n", r);
491724c80e1SAlex Deucher 			radeon_wb_fini(rdev);
492724c80e1SAlex Deucher 			return r;
493724c80e1SAlex Deucher 		}
494089920f2SJerome Glisse 	}
495724c80e1SAlex Deucher 
496e6ba7599SAlex Deucher 	/* clear wb memory */
497e6ba7599SAlex Deucher 	memset((char *)rdev->wb.wb, 0, RADEON_GPU_PAGE_SIZE);
498d0f8a854SAlex Deucher 	/* disable event_write fences */
499d0f8a854SAlex Deucher 	rdev->wb.use_event = false;
500724c80e1SAlex Deucher 	/* disabled via module param */
5013b7a2b24SJerome Glisse 	if (radeon_no_wb == 1) {
502724c80e1SAlex Deucher 		rdev->wb.enabled = false;
5033b7a2b24SJerome Glisse 	} else {
504724c80e1SAlex Deucher 		if (rdev->flags & RADEON_IS_AGP) {
50528eebb70SAlex Deucher 			/* often unreliable on AGP */
50628eebb70SAlex Deucher 			rdev->wb.enabled = false;
50728eebb70SAlex Deucher 		} else if (rdev->family < CHIP_R300) {
50828eebb70SAlex Deucher 			/* often unreliable on pre-r300 */
509724c80e1SAlex Deucher 			rdev->wb.enabled = false;
510d0f8a854SAlex Deucher 		} else {
511724c80e1SAlex Deucher 			rdev->wb.enabled = true;
512d0f8a854SAlex Deucher 			/* event_write fences are only available on r600+ */
5133b7a2b24SJerome Glisse 			if (rdev->family >= CHIP_R600) {
514d0f8a854SAlex Deucher 				rdev->wb.use_event = true;
515d0f8a854SAlex Deucher 			}
516724c80e1SAlex Deucher 		}
5173b7a2b24SJerome Glisse 	}
518c994ead6SAlex Deucher 	/* always use writeback/events on NI, APUs */
519c994ead6SAlex Deucher 	if (rdev->family >= CHIP_PALM) {
5207d52785dSAlex Deucher 		rdev->wb.enabled = true;
5217d52785dSAlex Deucher 		rdev->wb.use_event = true;
5227d52785dSAlex Deucher 	}
523724c80e1SAlex Deucher 
524724c80e1SAlex Deucher 	dev_info(rdev->dev, "WB %sabled\n", rdev->wb.enabled ? "en" : "dis");
525724c80e1SAlex Deucher 
526724c80e1SAlex Deucher 	return 0;
527724c80e1SAlex Deucher }
528724c80e1SAlex Deucher 
529d594e46aSJerome Glisse /**
530d594e46aSJerome Glisse  * radeon_vram_location - try to find VRAM location
531d594e46aSJerome Glisse  * @rdev: radeon device structure holding all necessary informations
532d594e46aSJerome Glisse  * @mc: memory controller structure holding memory informations
533d594e46aSJerome Glisse  * @base: base address at which to put VRAM
534d594e46aSJerome Glisse  *
535d594e46aSJerome Glisse  * Function will place try to place VRAM at base address provided
536d594e46aSJerome Glisse  * as parameter (which is so far either PCI aperture address or
537d594e46aSJerome Glisse  * for IGP TOM base address).
538d594e46aSJerome Glisse  *
539d594e46aSJerome Glisse  * If there is not enough space to fit the unvisible VRAM in the 32bits
540d594e46aSJerome Glisse  * address space then we limit the VRAM size to the aperture.
541d594e46aSJerome Glisse  *
542d594e46aSJerome Glisse  * If we are using AGP and if the AGP aperture doesn't allow us to have
543d594e46aSJerome Glisse  * room for all the VRAM than we restrict the VRAM to the PCI aperture
544d594e46aSJerome Glisse  * size and print a warning.
545d594e46aSJerome Glisse  *
546d594e46aSJerome Glisse  * This function will never fails, worst case are limiting VRAM.
547d594e46aSJerome Glisse  *
548d594e46aSJerome Glisse  * Note: GTT start, end, size should be initialized before calling this
549d594e46aSJerome Glisse  * function on AGP platform.
550d594e46aSJerome Glisse  *
55125985edcSLucas De Marchi  * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
552d594e46aSJerome Glisse  * this shouldn't be a problem as we are using the PCI aperture as a reference.
553d594e46aSJerome Glisse  * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
554d594e46aSJerome Glisse  * not IGP.
555d594e46aSJerome Glisse  *
556d594e46aSJerome Glisse  * Note: we use mc_vram_size as on some board we need to program the mc to
557d594e46aSJerome Glisse  * cover the whole aperture even if VRAM size is inferior to aperture size
558d594e46aSJerome Glisse  * Novell bug 204882 + along with lots of ubuntu ones
559d594e46aSJerome Glisse  *
560d594e46aSJerome Glisse  * Note: when limiting vram it's safe to overwritte real_vram_size because
561d594e46aSJerome Glisse  * we are not in case where real_vram_size is inferior to mc_vram_size (ie
562d594e46aSJerome Glisse  * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
563d594e46aSJerome Glisse  * ones)
564d594e46aSJerome Glisse  *
565d594e46aSJerome Glisse  * Note: IGP TOM addr should be the same as the aperture addr, we don't
566d594e46aSJerome Glisse  * explicitly check for that thought.
567d594e46aSJerome Glisse  *
568d594e46aSJerome Glisse  * FIXME: when reducing VRAM size align new size on power of 2.
569771fe6b9SJerome Glisse  */
570d594e46aSJerome Glisse void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base)
571771fe6b9SJerome Glisse {
5721bcb04f7SChristian König 	uint64_t limit = (uint64_t)radeon_vram_limit << 20;
5731bcb04f7SChristian König 
574d594e46aSJerome Glisse 	mc->vram_start = base;
5759ed8b1f9SAlex Deucher 	if (mc->mc_vram_size > (rdev->mc.mc_mask - base + 1)) {
576d594e46aSJerome Glisse 		dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
577d594e46aSJerome Glisse 		mc->real_vram_size = mc->aper_size;
578d594e46aSJerome Glisse 		mc->mc_vram_size = mc->aper_size;
579771fe6b9SJerome Glisse 	}
580d594e46aSJerome Glisse 	mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
5812cbeb4efSJerome Glisse 	if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_start <= mc->gtt_end) {
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;
5871bcb04f7SChristian König 	if (limit && limit < mc->real_vram_size)
5881bcb04f7SChristian König 		mc->real_vram_size = limit;
589dd7cc55aSAlex Deucher 	dev_info(rdev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
590d594e46aSJerome Glisse 			mc->mc_vram_size >> 20, mc->vram_start,
591d594e46aSJerome Glisse 			mc->vram_end, mc->real_vram_size >> 20);
592771fe6b9SJerome Glisse }
593771fe6b9SJerome Glisse 
594d594e46aSJerome Glisse /**
595d594e46aSJerome Glisse  * radeon_gtt_location - try to find GTT location
596d594e46aSJerome Glisse  * @rdev: radeon device structure holding all necessary informations
597d594e46aSJerome Glisse  * @mc: memory controller structure holding memory informations
598d594e46aSJerome Glisse  *
599d594e46aSJerome Glisse  * Function will place try to place GTT before or after VRAM.
600d594e46aSJerome Glisse  *
601d594e46aSJerome Glisse  * If GTT size is bigger than space left then we ajust GTT size.
602d594e46aSJerome Glisse  * Thus function will never fails.
603d594e46aSJerome Glisse  *
604d594e46aSJerome Glisse  * FIXME: when reducing GTT size align new size on power of 2.
605d594e46aSJerome Glisse  */
606d594e46aSJerome Glisse void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc)
607d594e46aSJerome Glisse {
608d594e46aSJerome Glisse 	u64 size_af, size_bf;
609d594e46aSJerome Glisse 
6109ed8b1f9SAlex Deucher 	size_af = ((rdev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
6118d369bb1SAlex Deucher 	size_bf = mc->vram_start & ~mc->gtt_base_align;
612d594e46aSJerome Glisse 	if (size_bf > size_af) {
613d594e46aSJerome Glisse 		if (mc->gtt_size > size_bf) {
614d594e46aSJerome Glisse 			dev_warn(rdev->dev, "limiting GTT\n");
615d594e46aSJerome Glisse 			mc->gtt_size = size_bf;
616d594e46aSJerome Glisse 		}
6178d369bb1SAlex Deucher 		mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
618d594e46aSJerome Glisse 	} else {
619d594e46aSJerome Glisse 		if (mc->gtt_size > size_af) {
620d594e46aSJerome Glisse 			dev_warn(rdev->dev, "limiting GTT\n");
621d594e46aSJerome Glisse 			mc->gtt_size = size_af;
622d594e46aSJerome Glisse 		}
6238d369bb1SAlex Deucher 		mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
624d594e46aSJerome Glisse 	}
625d594e46aSJerome Glisse 	mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
626dd7cc55aSAlex Deucher 	dev_info(rdev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
627d594e46aSJerome Glisse 			mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
628d594e46aSJerome Glisse }
629771fe6b9SJerome Glisse 
630771fe6b9SJerome Glisse /*
631771fe6b9SJerome Glisse  * GPU helpers function.
632771fe6b9SJerome Glisse  */
6330c195119SAlex Deucher /**
6340c195119SAlex Deucher  * radeon_card_posted - check if the hw has already been initialized
6350c195119SAlex Deucher  *
6360c195119SAlex Deucher  * @rdev: radeon_device pointer
6370c195119SAlex Deucher  *
6380c195119SAlex Deucher  * Check if the asic has been initialized (all asics).
6390c195119SAlex Deucher  * Used at driver startup.
6400c195119SAlex Deucher  * Returns true if initialized or false if not.
6410c195119SAlex Deucher  */
6429f022ddfSJerome Glisse bool radeon_card_posted(struct radeon_device *rdev)
643771fe6b9SJerome Glisse {
644771fe6b9SJerome Glisse 	uint32_t reg;
645771fe6b9SJerome Glisse 
64650a583f6SAlex Deucher 	/* required for EFI mode on macbook2,1 which uses an r5xx asic */
64783e68189SMatt Fleming 	if (efi_enabled(EFI_BOOT) &&
64850a583f6SAlex Deucher 	    (rdev->pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE) &&
64950a583f6SAlex Deucher 	    (rdev->family < CHIP_R600))
650bcc65fd8SMatthew Garrett 		return false;
651bcc65fd8SMatthew Garrett 
6522cf3a4fcSAlex Deucher 	if (ASIC_IS_NODCE(rdev))
6532cf3a4fcSAlex Deucher 		goto check_memsize;
6542cf3a4fcSAlex Deucher 
655771fe6b9SJerome Glisse 	/* first check CRTCs */
65609fb8bd1SAlex Deucher 	if (ASIC_IS_DCE4(rdev)) {
65718007401SAlex Deucher 		reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
65818007401SAlex Deucher 			RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET);
65909fb8bd1SAlex Deucher 			if (rdev->num_crtc >= 4) {
66009fb8bd1SAlex Deucher 				reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) |
66109fb8bd1SAlex Deucher 					RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET);
66209fb8bd1SAlex Deucher 			}
66309fb8bd1SAlex Deucher 			if (rdev->num_crtc >= 6) {
66409fb8bd1SAlex Deucher 				reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) |
665bcc1c2a1SAlex Deucher 					RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET);
66609fb8bd1SAlex Deucher 			}
667bcc1c2a1SAlex Deucher 		if (reg & EVERGREEN_CRTC_MASTER_EN)
668bcc1c2a1SAlex Deucher 			return true;
669bcc1c2a1SAlex Deucher 	} else if (ASIC_IS_AVIVO(rdev)) {
670771fe6b9SJerome Glisse 		reg = RREG32(AVIVO_D1CRTC_CONTROL) |
671771fe6b9SJerome Glisse 		      RREG32(AVIVO_D2CRTC_CONTROL);
672771fe6b9SJerome Glisse 		if (reg & AVIVO_CRTC_EN) {
673771fe6b9SJerome Glisse 			return true;
674771fe6b9SJerome Glisse 		}
675771fe6b9SJerome Glisse 	} else {
676771fe6b9SJerome Glisse 		reg = RREG32(RADEON_CRTC_GEN_CNTL) |
677771fe6b9SJerome Glisse 		      RREG32(RADEON_CRTC2_GEN_CNTL);
678771fe6b9SJerome Glisse 		if (reg & RADEON_CRTC_EN) {
679771fe6b9SJerome Glisse 			return true;
680771fe6b9SJerome Glisse 		}
681771fe6b9SJerome Glisse 	}
682771fe6b9SJerome Glisse 
6832cf3a4fcSAlex Deucher check_memsize:
684771fe6b9SJerome Glisse 	/* then check MEM_SIZE, in case the crtcs are off */
685771fe6b9SJerome Glisse 	if (rdev->family >= CHIP_R600)
686771fe6b9SJerome Glisse 		reg = RREG32(R600_CONFIG_MEMSIZE);
687771fe6b9SJerome Glisse 	else
688771fe6b9SJerome Glisse 		reg = RREG32(RADEON_CONFIG_MEMSIZE);
689771fe6b9SJerome Glisse 
690771fe6b9SJerome Glisse 	if (reg)
691771fe6b9SJerome Glisse 		return true;
692771fe6b9SJerome Glisse 
693771fe6b9SJerome Glisse 	return false;
694771fe6b9SJerome Glisse 
695771fe6b9SJerome Glisse }
696771fe6b9SJerome Glisse 
6970c195119SAlex Deucher /**
6980c195119SAlex Deucher  * radeon_update_bandwidth_info - update display bandwidth params
6990c195119SAlex Deucher  *
7000c195119SAlex Deucher  * @rdev: radeon_device pointer
7010c195119SAlex Deucher  *
7020c195119SAlex Deucher  * Used when sclk/mclk are switched or display modes are set.
7030c195119SAlex Deucher  * params are used to calculate display watermarks (all asics)
7040c195119SAlex Deucher  */
705f47299c5SAlex Deucher void radeon_update_bandwidth_info(struct radeon_device *rdev)
706f47299c5SAlex Deucher {
707f47299c5SAlex Deucher 	fixed20_12 a;
7088807286eSAlex Deucher 	u32 sclk = rdev->pm.current_sclk;
7098807286eSAlex Deucher 	u32 mclk = rdev->pm.current_mclk;
710f47299c5SAlex Deucher 
7118807286eSAlex Deucher 	/* sclk/mclk in Mhz */
71268adac5eSBen Skeggs 	a.full = dfixed_const(100);
71368adac5eSBen Skeggs 	rdev->pm.sclk.full = dfixed_const(sclk);
71468adac5eSBen Skeggs 	rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a);
71568adac5eSBen Skeggs 	rdev->pm.mclk.full = dfixed_const(mclk);
71668adac5eSBen Skeggs 	rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a);
717f47299c5SAlex Deucher 
7188807286eSAlex Deucher 	if (rdev->flags & RADEON_IS_IGP) {
71968adac5eSBen Skeggs 		a.full = dfixed_const(16);
720f47299c5SAlex Deucher 		/* core_bandwidth = sclk(Mhz) * 16 */
72168adac5eSBen Skeggs 		rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a);
722f47299c5SAlex Deucher 	}
723f47299c5SAlex Deucher }
724f47299c5SAlex Deucher 
7250c195119SAlex Deucher /**
7260c195119SAlex Deucher  * radeon_boot_test_post_card - check and possibly initialize the hw
7270c195119SAlex Deucher  *
7280c195119SAlex Deucher  * @rdev: radeon_device pointer
7290c195119SAlex Deucher  *
7300c195119SAlex Deucher  * Check if the asic is initialized and if not, attempt to initialize
7310c195119SAlex Deucher  * it (all asics).
7320c195119SAlex Deucher  * Returns true if initialized or false if not.
7330c195119SAlex Deucher  */
73472542d77SDave Airlie bool radeon_boot_test_post_card(struct radeon_device *rdev)
73572542d77SDave Airlie {
73672542d77SDave Airlie 	if (radeon_card_posted(rdev))
73772542d77SDave Airlie 		return true;
73872542d77SDave Airlie 
73972542d77SDave Airlie 	if (rdev->bios) {
74072542d77SDave Airlie 		DRM_INFO("GPU not posted. posting now...\n");
74172542d77SDave Airlie 		if (rdev->is_atom_bios)
74272542d77SDave Airlie 			atom_asic_init(rdev->mode_info.atom_context);
74372542d77SDave Airlie 		else
74472542d77SDave Airlie 			radeon_combios_asic_init(rdev->ddev);
74572542d77SDave Airlie 		return true;
74672542d77SDave Airlie 	} else {
74772542d77SDave Airlie 		dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
74872542d77SDave Airlie 		return false;
74972542d77SDave Airlie 	}
75072542d77SDave Airlie }
75172542d77SDave Airlie 
7520c195119SAlex Deucher /**
7530c195119SAlex Deucher  * radeon_dummy_page_init - init dummy page used by the driver
7540c195119SAlex Deucher  *
7550c195119SAlex Deucher  * @rdev: radeon_device pointer
7560c195119SAlex Deucher  *
7570c195119SAlex Deucher  * Allocate the dummy page used by the driver (all asics).
7580c195119SAlex Deucher  * This dummy page is used by the driver as a filler for gart entries
7590c195119SAlex Deucher  * when pages are taken out of the GART
7600c195119SAlex Deucher  * Returns 0 on sucess, -ENOMEM on failure.
7610c195119SAlex Deucher  */
7623ce0a23dSJerome Glisse int radeon_dummy_page_init(struct radeon_device *rdev)
7633ce0a23dSJerome Glisse {
76482568565SDave Airlie 	if (rdev->dummy_page.page)
76582568565SDave Airlie 		return 0;
7663ce0a23dSJerome Glisse 	rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
7673ce0a23dSJerome Glisse 	if (rdev->dummy_page.page == NULL)
7683ce0a23dSJerome Glisse 		return -ENOMEM;
7693ce0a23dSJerome Glisse 	rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page,
7703ce0a23dSJerome Glisse 					0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
771a30f6fb7SBenjamin Herrenschmidt 	if (pci_dma_mapping_error(rdev->pdev, rdev->dummy_page.addr)) {
772a30f6fb7SBenjamin Herrenschmidt 		dev_err(&rdev->pdev->dev, "Failed to DMA MAP the dummy page\n");
7733ce0a23dSJerome Glisse 		__free_page(rdev->dummy_page.page);
7743ce0a23dSJerome Glisse 		rdev->dummy_page.page = NULL;
7753ce0a23dSJerome Glisse 		return -ENOMEM;
7763ce0a23dSJerome Glisse 	}
777cb658906SMichel Dänzer 	rdev->dummy_page.entry = radeon_gart_get_page_entry(rdev->dummy_page.addr,
778cb658906SMichel Dänzer 							    RADEON_GART_PAGE_DUMMY);
7793ce0a23dSJerome Glisse 	return 0;
7803ce0a23dSJerome Glisse }
7813ce0a23dSJerome Glisse 
7820c195119SAlex Deucher /**
7830c195119SAlex Deucher  * radeon_dummy_page_fini - free dummy page used by the driver
7840c195119SAlex Deucher  *
7850c195119SAlex Deucher  * @rdev: radeon_device pointer
7860c195119SAlex Deucher  *
7870c195119SAlex Deucher  * Frees the dummy page used by the driver (all asics).
7880c195119SAlex Deucher  */
7893ce0a23dSJerome Glisse void radeon_dummy_page_fini(struct radeon_device *rdev)
7903ce0a23dSJerome Glisse {
7913ce0a23dSJerome Glisse 	if (rdev->dummy_page.page == NULL)
7923ce0a23dSJerome Glisse 		return;
7933ce0a23dSJerome Glisse 	pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
7943ce0a23dSJerome Glisse 			PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
7953ce0a23dSJerome Glisse 	__free_page(rdev->dummy_page.page);
7963ce0a23dSJerome Glisse 	rdev->dummy_page.page = NULL;
7973ce0a23dSJerome Glisse }
7983ce0a23dSJerome Glisse 
799771fe6b9SJerome Glisse 
800771fe6b9SJerome Glisse /* ATOM accessor methods */
8010c195119SAlex Deucher /*
8020c195119SAlex Deucher  * ATOM is an interpreted byte code stored in tables in the vbios.  The
8030c195119SAlex Deucher  * driver registers callbacks to access registers and the interpreter
8040c195119SAlex Deucher  * in the driver parses the tables and executes then to program specific
8050c195119SAlex Deucher  * actions (set display modes, asic init, etc.).  See radeon_atombios.c,
8060c195119SAlex Deucher  * atombios.h, and atom.c
8070c195119SAlex Deucher  */
8080c195119SAlex Deucher 
8090c195119SAlex Deucher /**
8100c195119SAlex Deucher  * cail_pll_read - read PLL register
8110c195119SAlex Deucher  *
8120c195119SAlex Deucher  * @info: atom card_info pointer
8130c195119SAlex Deucher  * @reg: PLL register offset
8140c195119SAlex Deucher  *
8150c195119SAlex Deucher  * Provides a PLL register accessor for the atom interpreter (r4xx+).
8160c195119SAlex Deucher  * Returns the value of the PLL register.
8170c195119SAlex Deucher  */
818771fe6b9SJerome Glisse static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
819771fe6b9SJerome Glisse {
820771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
821771fe6b9SJerome Glisse 	uint32_t r;
822771fe6b9SJerome Glisse 
823771fe6b9SJerome Glisse 	r = rdev->pll_rreg(rdev, reg);
824771fe6b9SJerome Glisse 	return r;
825771fe6b9SJerome Glisse }
826771fe6b9SJerome Glisse 
8270c195119SAlex Deucher /**
8280c195119SAlex Deucher  * cail_pll_write - write PLL register
8290c195119SAlex Deucher  *
8300c195119SAlex Deucher  * @info: atom card_info pointer
8310c195119SAlex Deucher  * @reg: PLL register offset
8320c195119SAlex Deucher  * @val: value to write to the pll register
8330c195119SAlex Deucher  *
8340c195119SAlex Deucher  * Provides a PLL register accessor for the atom interpreter (r4xx+).
8350c195119SAlex Deucher  */
836771fe6b9SJerome Glisse static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
837771fe6b9SJerome Glisse {
838771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
839771fe6b9SJerome Glisse 
840771fe6b9SJerome Glisse 	rdev->pll_wreg(rdev, reg, val);
841771fe6b9SJerome Glisse }
842771fe6b9SJerome Glisse 
8430c195119SAlex Deucher /**
8440c195119SAlex Deucher  * cail_mc_read - read MC (Memory Controller) register
8450c195119SAlex Deucher  *
8460c195119SAlex Deucher  * @info: atom card_info pointer
8470c195119SAlex Deucher  * @reg: MC register offset
8480c195119SAlex Deucher  *
8490c195119SAlex Deucher  * Provides an MC register accessor for the atom interpreter (r4xx+).
8500c195119SAlex Deucher  * Returns the value of the MC register.
8510c195119SAlex Deucher  */
852771fe6b9SJerome Glisse static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
853771fe6b9SJerome Glisse {
854771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
855771fe6b9SJerome Glisse 	uint32_t r;
856771fe6b9SJerome Glisse 
857771fe6b9SJerome Glisse 	r = rdev->mc_rreg(rdev, reg);
858771fe6b9SJerome Glisse 	return r;
859771fe6b9SJerome Glisse }
860771fe6b9SJerome Glisse 
8610c195119SAlex Deucher /**
8620c195119SAlex Deucher  * cail_mc_write - write MC (Memory Controller) register
8630c195119SAlex Deucher  *
8640c195119SAlex Deucher  * @info: atom card_info pointer
8650c195119SAlex Deucher  * @reg: MC register offset
8660c195119SAlex Deucher  * @val: value to write to the pll register
8670c195119SAlex Deucher  *
8680c195119SAlex Deucher  * Provides a MC register accessor for the atom interpreter (r4xx+).
8690c195119SAlex Deucher  */
870771fe6b9SJerome Glisse static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
871771fe6b9SJerome Glisse {
872771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
873771fe6b9SJerome Glisse 
874771fe6b9SJerome Glisse 	rdev->mc_wreg(rdev, reg, val);
875771fe6b9SJerome Glisse }
876771fe6b9SJerome Glisse 
8770c195119SAlex Deucher /**
8780c195119SAlex Deucher  * cail_reg_write - write MMIO register
8790c195119SAlex Deucher  *
8800c195119SAlex Deucher  * @info: atom card_info pointer
8810c195119SAlex Deucher  * @reg: MMIO register offset
8820c195119SAlex Deucher  * @val: value to write to the pll register
8830c195119SAlex Deucher  *
8840c195119SAlex Deucher  * Provides a MMIO register accessor for the atom interpreter (r4xx+).
8850c195119SAlex Deucher  */
886771fe6b9SJerome Glisse static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
887771fe6b9SJerome Glisse {
888771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
889771fe6b9SJerome Glisse 
890771fe6b9SJerome Glisse 	WREG32(reg*4, val);
891771fe6b9SJerome Glisse }
892771fe6b9SJerome Glisse 
8930c195119SAlex Deucher /**
8940c195119SAlex Deucher  * cail_reg_read - read MMIO register
8950c195119SAlex Deucher  *
8960c195119SAlex Deucher  * @info: atom card_info pointer
8970c195119SAlex Deucher  * @reg: MMIO register offset
8980c195119SAlex Deucher  *
8990c195119SAlex Deucher  * Provides an MMIO register accessor for the atom interpreter (r4xx+).
9000c195119SAlex Deucher  * Returns the value of the MMIO register.
9010c195119SAlex Deucher  */
902771fe6b9SJerome Glisse static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
903771fe6b9SJerome Glisse {
904771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
905771fe6b9SJerome Glisse 	uint32_t r;
906771fe6b9SJerome Glisse 
907771fe6b9SJerome Glisse 	r = RREG32(reg*4);
908771fe6b9SJerome Glisse 	return r;
909771fe6b9SJerome Glisse }
910771fe6b9SJerome Glisse 
9110c195119SAlex Deucher /**
9120c195119SAlex Deucher  * cail_ioreg_write - write IO register
9130c195119SAlex Deucher  *
9140c195119SAlex Deucher  * @info: atom card_info pointer
9150c195119SAlex Deucher  * @reg: IO register offset
9160c195119SAlex Deucher  * @val: value to write to the pll register
9170c195119SAlex Deucher  *
9180c195119SAlex Deucher  * Provides a IO register accessor for the atom interpreter (r4xx+).
9190c195119SAlex Deucher  */
920351a52a2SAlex Deucher static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
921351a52a2SAlex Deucher {
922351a52a2SAlex Deucher 	struct radeon_device *rdev = info->dev->dev_private;
923351a52a2SAlex Deucher 
924351a52a2SAlex Deucher 	WREG32_IO(reg*4, val);
925351a52a2SAlex Deucher }
926351a52a2SAlex Deucher 
9270c195119SAlex Deucher /**
9280c195119SAlex Deucher  * cail_ioreg_read - read IO register
9290c195119SAlex Deucher  *
9300c195119SAlex Deucher  * @info: atom card_info pointer
9310c195119SAlex Deucher  * @reg: IO register offset
9320c195119SAlex Deucher  *
9330c195119SAlex Deucher  * Provides an IO register accessor for the atom interpreter (r4xx+).
9340c195119SAlex Deucher  * Returns the value of the IO register.
9350c195119SAlex Deucher  */
936351a52a2SAlex Deucher static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
937351a52a2SAlex Deucher {
938351a52a2SAlex Deucher 	struct radeon_device *rdev = info->dev->dev_private;
939351a52a2SAlex Deucher 	uint32_t r;
940351a52a2SAlex Deucher 
941351a52a2SAlex Deucher 	r = RREG32_IO(reg*4);
942351a52a2SAlex Deucher 	return r;
943351a52a2SAlex Deucher }
944351a52a2SAlex Deucher 
9450c195119SAlex Deucher /**
9460c195119SAlex Deucher  * radeon_atombios_init - init the driver info and callbacks for atombios
9470c195119SAlex Deucher  *
9480c195119SAlex Deucher  * @rdev: radeon_device pointer
9490c195119SAlex Deucher  *
9500c195119SAlex Deucher  * Initializes the driver info and register access callbacks for the
9510c195119SAlex Deucher  * ATOM interpreter (r4xx+).
9520c195119SAlex Deucher  * Returns 0 on sucess, -ENOMEM on failure.
9530c195119SAlex Deucher  * Called at driver startup.
9540c195119SAlex Deucher  */
955771fe6b9SJerome Glisse int radeon_atombios_init(struct radeon_device *rdev)
956771fe6b9SJerome Glisse {
95761c4b24bSMathias Fröhlich 	struct card_info *atom_card_info =
95861c4b24bSMathias Fröhlich 	    kzalloc(sizeof(struct card_info), GFP_KERNEL);
95961c4b24bSMathias Fröhlich 
96061c4b24bSMathias Fröhlich 	if (!atom_card_info)
96161c4b24bSMathias Fröhlich 		return -ENOMEM;
96261c4b24bSMathias Fröhlich 
96361c4b24bSMathias Fröhlich 	rdev->mode_info.atom_card_info = atom_card_info;
96461c4b24bSMathias Fröhlich 	atom_card_info->dev = rdev->ddev;
96561c4b24bSMathias Fröhlich 	atom_card_info->reg_read = cail_reg_read;
96661c4b24bSMathias Fröhlich 	atom_card_info->reg_write = cail_reg_write;
967351a52a2SAlex Deucher 	/* needed for iio ops */
968351a52a2SAlex Deucher 	if (rdev->rio_mem) {
969351a52a2SAlex Deucher 		atom_card_info->ioreg_read = cail_ioreg_read;
970351a52a2SAlex Deucher 		atom_card_info->ioreg_write = cail_ioreg_write;
971351a52a2SAlex Deucher 	} else {
972351a52a2SAlex Deucher 		DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
973351a52a2SAlex Deucher 		atom_card_info->ioreg_read = cail_reg_read;
974351a52a2SAlex Deucher 		atom_card_info->ioreg_write = cail_reg_write;
975351a52a2SAlex Deucher 	}
97661c4b24bSMathias Fröhlich 	atom_card_info->mc_read = cail_mc_read;
97761c4b24bSMathias Fröhlich 	atom_card_info->mc_write = cail_mc_write;
97861c4b24bSMathias Fröhlich 	atom_card_info->pll_read = cail_pll_read;
97961c4b24bSMathias Fröhlich 	atom_card_info->pll_write = cail_pll_write;
98061c4b24bSMathias Fröhlich 
98161c4b24bSMathias Fröhlich 	rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
9820e34d094STim Gardner 	if (!rdev->mode_info.atom_context) {
9830e34d094STim Gardner 		radeon_atombios_fini(rdev);
9840e34d094STim Gardner 		return -ENOMEM;
9850e34d094STim Gardner 	}
9860e34d094STim Gardner 
987c31ad97fSRafał Miłecki 	mutex_init(&rdev->mode_info.atom_context->mutex);
9881c949842SDave Airlie 	mutex_init(&rdev->mode_info.atom_context->scratch_mutex);
989771fe6b9SJerome Glisse 	radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
990d904ef9bSDave Airlie 	atom_allocate_fb_scratch(rdev->mode_info.atom_context);
991771fe6b9SJerome Glisse 	return 0;
992771fe6b9SJerome Glisse }
993771fe6b9SJerome Glisse 
9940c195119SAlex Deucher /**
9950c195119SAlex Deucher  * radeon_atombios_fini - free the driver info and callbacks for atombios
9960c195119SAlex Deucher  *
9970c195119SAlex Deucher  * @rdev: radeon_device pointer
9980c195119SAlex Deucher  *
9990c195119SAlex Deucher  * Frees the driver info and register access callbacks for the ATOM
10000c195119SAlex Deucher  * interpreter (r4xx+).
10010c195119SAlex Deucher  * Called at driver shutdown.
10020c195119SAlex Deucher  */
1003771fe6b9SJerome Glisse void radeon_atombios_fini(struct radeon_device *rdev)
1004771fe6b9SJerome Glisse {
10054a04a844SJerome Glisse 	if (rdev->mode_info.atom_context) {
1006d904ef9bSDave Airlie 		kfree(rdev->mode_info.atom_context->scratch);
10074a04a844SJerome Glisse 	}
10080e34d094STim Gardner 	kfree(rdev->mode_info.atom_context);
10090e34d094STim Gardner 	rdev->mode_info.atom_context = NULL;
101061c4b24bSMathias Fröhlich 	kfree(rdev->mode_info.atom_card_info);
10110e34d094STim Gardner 	rdev->mode_info.atom_card_info = NULL;
1012771fe6b9SJerome Glisse }
1013771fe6b9SJerome Glisse 
10140c195119SAlex Deucher /* COMBIOS */
10150c195119SAlex Deucher /*
10160c195119SAlex Deucher  * COMBIOS is the bios format prior to ATOM. It provides
10170c195119SAlex Deucher  * command tables similar to ATOM, but doesn't have a unified
10180c195119SAlex Deucher  * parser.  See radeon_combios.c
10190c195119SAlex Deucher  */
10200c195119SAlex Deucher 
10210c195119SAlex Deucher /**
10220c195119SAlex Deucher  * radeon_combios_init - init the driver info for combios
10230c195119SAlex Deucher  *
10240c195119SAlex Deucher  * @rdev: radeon_device pointer
10250c195119SAlex Deucher  *
10260c195119SAlex Deucher  * Initializes the driver info for combios (r1xx-r3xx).
10270c195119SAlex Deucher  * Returns 0 on sucess.
10280c195119SAlex Deucher  * Called at driver startup.
10290c195119SAlex Deucher  */
1030771fe6b9SJerome Glisse int radeon_combios_init(struct radeon_device *rdev)
1031771fe6b9SJerome Glisse {
1032771fe6b9SJerome Glisse 	radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
1033771fe6b9SJerome Glisse 	return 0;
1034771fe6b9SJerome Glisse }
1035771fe6b9SJerome Glisse 
10360c195119SAlex Deucher /**
10370c195119SAlex Deucher  * radeon_combios_fini - free the driver info for combios
10380c195119SAlex Deucher  *
10390c195119SAlex Deucher  * @rdev: radeon_device pointer
10400c195119SAlex Deucher  *
10410c195119SAlex Deucher  * Frees the driver info for combios (r1xx-r3xx).
10420c195119SAlex Deucher  * Called at driver shutdown.
10430c195119SAlex Deucher  */
1044771fe6b9SJerome Glisse void radeon_combios_fini(struct radeon_device *rdev)
1045771fe6b9SJerome Glisse {
1046771fe6b9SJerome Glisse }
1047771fe6b9SJerome Glisse 
10480c195119SAlex Deucher /* if we get transitioned to only one device, take VGA back */
10490c195119SAlex Deucher /**
10500c195119SAlex Deucher  * radeon_vga_set_decode - enable/disable vga decode
10510c195119SAlex Deucher  *
10520c195119SAlex Deucher  * @cookie: radeon_device pointer
10530c195119SAlex Deucher  * @state: enable/disable vga decode
10540c195119SAlex Deucher  *
10550c195119SAlex Deucher  * Enable/disable vga decode (all asics).
10560c195119SAlex Deucher  * Returns VGA resource flags.
10570c195119SAlex Deucher  */
105828d52043SDave Airlie static unsigned int radeon_vga_set_decode(void *cookie, bool state)
105928d52043SDave Airlie {
106028d52043SDave Airlie 	struct radeon_device *rdev = cookie;
106128d52043SDave Airlie 	radeon_vga_set_state(rdev, state);
106228d52043SDave Airlie 	if (state)
106328d52043SDave Airlie 		return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
106428d52043SDave Airlie 		       VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
106528d52043SDave Airlie 	else
106628d52043SDave Airlie 		return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
106728d52043SDave Airlie }
1068c1176d6fSDave Airlie 
10690c195119SAlex Deucher /**
10701bcb04f7SChristian König  * radeon_check_pot_argument - check that argument is a power of two
10711bcb04f7SChristian König  *
10721bcb04f7SChristian König  * @arg: value to check
10731bcb04f7SChristian König  *
10741bcb04f7SChristian König  * Validates that a certain argument is a power of two (all asics).
10751bcb04f7SChristian König  * Returns true if argument is valid.
10761bcb04f7SChristian König  */
10771bcb04f7SChristian König static bool radeon_check_pot_argument(int arg)
10781bcb04f7SChristian König {
10791bcb04f7SChristian König 	return (arg & (arg - 1)) == 0;
10801bcb04f7SChristian König }
10811bcb04f7SChristian König 
10821bcb04f7SChristian König /**
10835e3c4f90SGrigori Goronzy  * Determine a sensible default GART size according to ASIC family.
10845e3c4f90SGrigori Goronzy  *
10855e3c4f90SGrigori Goronzy  * @family ASIC family name
10865e3c4f90SGrigori Goronzy  */
10875e3c4f90SGrigori Goronzy static int radeon_gart_size_auto(enum radeon_family family)
10885e3c4f90SGrigori Goronzy {
10895e3c4f90SGrigori Goronzy 	/* default to a larger gart size on newer asics */
10905e3c4f90SGrigori Goronzy 	if (family >= CHIP_TAHITI)
10915e3c4f90SGrigori Goronzy 		return 2048;
10925e3c4f90SGrigori Goronzy 	else if (family >= CHIP_RV770)
10935e3c4f90SGrigori Goronzy 		return 1024;
10945e3c4f90SGrigori Goronzy 	else
10955e3c4f90SGrigori Goronzy 		return 512;
10965e3c4f90SGrigori Goronzy }
10975e3c4f90SGrigori Goronzy 
10985e3c4f90SGrigori Goronzy /**
10990c195119SAlex Deucher  * radeon_check_arguments - validate module params
11000c195119SAlex Deucher  *
11010c195119SAlex Deucher  * @rdev: radeon_device pointer
11020c195119SAlex Deucher  *
11030c195119SAlex Deucher  * Validates certain module parameters and updates
11040c195119SAlex Deucher  * the associated values used by the driver (all asics).
11050c195119SAlex Deucher  */
11061109ca09SLauri Kasanen static void radeon_check_arguments(struct radeon_device *rdev)
110736421338SJerome Glisse {
110836421338SJerome Glisse 	/* vramlimit must be a power of two */
11091bcb04f7SChristian König 	if (!radeon_check_pot_argument(radeon_vram_limit)) {
111036421338SJerome Glisse 		dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n",
111136421338SJerome Glisse 				radeon_vram_limit);
111236421338SJerome Glisse 		radeon_vram_limit = 0;
111336421338SJerome Glisse 	}
11141bcb04f7SChristian König 
1115edcd26e8SAlex Deucher 	if (radeon_gart_size == -1) {
11165e3c4f90SGrigori Goronzy 		radeon_gart_size = radeon_gart_size_auto(rdev->family);
1117edcd26e8SAlex Deucher 	}
111836421338SJerome Glisse 	/* gtt size must be power of two and greater or equal to 32M */
11191bcb04f7SChristian König 	if (radeon_gart_size < 32) {
1120edcd26e8SAlex Deucher 		dev_warn(rdev->dev, "gart size (%d) too small\n",
112136421338SJerome Glisse 				radeon_gart_size);
11225e3c4f90SGrigori Goronzy 		radeon_gart_size = radeon_gart_size_auto(rdev->family);
11231bcb04f7SChristian König 	} else if (!radeon_check_pot_argument(radeon_gart_size)) {
112436421338SJerome Glisse 		dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
112536421338SJerome Glisse 				radeon_gart_size);
11265e3c4f90SGrigori Goronzy 		radeon_gart_size = radeon_gart_size_auto(rdev->family);
112736421338SJerome Glisse 	}
11281bcb04f7SChristian König 	rdev->mc.gtt_size = (uint64_t)radeon_gart_size << 20;
11291bcb04f7SChristian König 
113036421338SJerome Glisse 	/* AGP mode can only be -1, 1, 2, 4, 8 */
113136421338SJerome Glisse 	switch (radeon_agpmode) {
113236421338SJerome Glisse 	case -1:
113336421338SJerome Glisse 	case 0:
113436421338SJerome Glisse 	case 1:
113536421338SJerome Glisse 	case 2:
113636421338SJerome Glisse 	case 4:
113736421338SJerome Glisse 	case 8:
113836421338SJerome Glisse 		break;
113936421338SJerome Glisse 	default:
114036421338SJerome Glisse 		dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
114136421338SJerome Glisse 				"-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
114236421338SJerome Glisse 		radeon_agpmode = 0;
114336421338SJerome Glisse 		break;
114436421338SJerome Glisse 	}
1145c1c44132SChristian König 
1146c1c44132SChristian König 	if (!radeon_check_pot_argument(radeon_vm_size)) {
1147c1c44132SChristian König 		dev_warn(rdev->dev, "VM size (%d) must be a power of 2\n",
1148c1c44132SChristian König 			 radeon_vm_size);
114920b2656dSChristian König 		radeon_vm_size = 4;
1150c1c44132SChristian König 	}
1151c1c44132SChristian König 
115220b2656dSChristian König 	if (radeon_vm_size < 1) {
115313c240efSAlexandre Demers 		dev_warn(rdev->dev, "VM size (%d) too small, min is 1GB\n",
1154c1c44132SChristian König 			 radeon_vm_size);
115520b2656dSChristian König 		radeon_vm_size = 4;
1156c1c44132SChristian König 	}
1157c1c44132SChristian König 
1158c1c44132SChristian König 	/*
1159c1c44132SChristian König 	 * Max GPUVM size for Cayman, SI and CI are 40 bits.
1160c1c44132SChristian König 	 */
116120b2656dSChristian König 	if (radeon_vm_size > 1024) {
116220b2656dSChristian König 		dev_warn(rdev->dev, "VM size (%d) too large, max is 1TB\n",
1163c1c44132SChristian König 			 radeon_vm_size);
116420b2656dSChristian König 		radeon_vm_size = 4;
1165c1c44132SChristian König 	}
11664510fb98SChristian König 
11674510fb98SChristian König 	/* defines number of bits in page table versus page directory,
11684510fb98SChristian König 	 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
11694510fb98SChristian König 	 * page table and the remaining bits are in the page directory */
1170dfc230f9SChristian König 	if (radeon_vm_block_size == -1) {
1171dfc230f9SChristian König 
1172dfc230f9SChristian König 		/* Total bits covered by PD + PTs */
11738e66e134SAlex Deucher 		unsigned bits = ilog2(radeon_vm_size) + 18;
1174dfc230f9SChristian König 
1175dfc230f9SChristian König 		/* Make sure the PD is 4K in size up to 8GB address space.
1176dfc230f9SChristian König 		   Above that split equal between PD and PTs */
1177dfc230f9SChristian König 		if (radeon_vm_size <= 8)
1178dfc230f9SChristian König 			radeon_vm_block_size = bits - 9;
1179dfc230f9SChristian König 		else
1180dfc230f9SChristian König 			radeon_vm_block_size = (bits + 3) / 2;
1181dfc230f9SChristian König 
1182dfc230f9SChristian König 	} else if (radeon_vm_block_size < 9) {
118320b2656dSChristian König 		dev_warn(rdev->dev, "VM page table size (%d) too small\n",
11844510fb98SChristian König 			 radeon_vm_block_size);
11854510fb98SChristian König 		radeon_vm_block_size = 9;
11864510fb98SChristian König 	}
11874510fb98SChristian König 
11884510fb98SChristian König 	if (radeon_vm_block_size > 24 ||
118920b2656dSChristian König 	    (radeon_vm_size * 1024) < (1ull << radeon_vm_block_size)) {
119020b2656dSChristian König 		dev_warn(rdev->dev, "VM page table size (%d) too large\n",
11914510fb98SChristian König 			 radeon_vm_block_size);
11924510fb98SChristian König 		radeon_vm_block_size = 9;
11934510fb98SChristian König 	}
119436421338SJerome Glisse }
119536421338SJerome Glisse 
11960c195119SAlex Deucher /**
11970c195119SAlex Deucher  * radeon_switcheroo_set_state - set switcheroo state
11980c195119SAlex Deucher  *
11990c195119SAlex Deucher  * @pdev: pci dev pointer
12008e5de1d8SLukas Wunner  * @state: vga_switcheroo state
12010c195119SAlex Deucher  *
12020c195119SAlex Deucher  * Callback for the switcheroo driver.  Suspends or resumes the
12030c195119SAlex Deucher  * the asics before or after it is powered up using ACPI methods.
12040c195119SAlex Deucher  */
12056a9ee8afSDave Airlie static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
12066a9ee8afSDave Airlie {
12076a9ee8afSDave Airlie 	struct drm_device *dev = pci_get_drvdata(pdev);
12084807c5a8SAlex Deucher 	struct radeon_device *rdev = dev->dev_private;
120910ebc0bcSDave Airlie 
121090c4cde9SAlex Deucher 	if (radeon_is_px(dev) && state == VGA_SWITCHEROO_OFF)
121110ebc0bcSDave Airlie 		return;
121210ebc0bcSDave Airlie 
12136a9ee8afSDave Airlie 	if (state == VGA_SWITCHEROO_ON) {
1214d1f9809eSMaarten Lankhorst 		unsigned d3_delay = dev->pdev->d3_delay;
1215d1f9809eSMaarten Lankhorst 
12166a9ee8afSDave Airlie 		printk(KERN_INFO "radeon: switched on\n");
12176a9ee8afSDave Airlie 		/* don't suspend or resume card normally */
12185bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1219d1f9809eSMaarten Lankhorst 
12204807c5a8SAlex Deucher 		if (d3_delay < 20 && (rdev->px_quirk_flags & RADEON_PX_QUIRK_LONG_WAKEUP))
1221d1f9809eSMaarten Lankhorst 			dev->pdev->d3_delay = 20;
1222d1f9809eSMaarten Lankhorst 
122310ebc0bcSDave Airlie 		radeon_resume_kms(dev, true, true);
1224d1f9809eSMaarten Lankhorst 
1225d1f9809eSMaarten Lankhorst 		dev->pdev->d3_delay = d3_delay;
1226d1f9809eSMaarten Lankhorst 
12275bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_ON;
1228fbf81762SDave Airlie 		drm_kms_helper_poll_enable(dev);
12296a9ee8afSDave Airlie 	} else {
12306a9ee8afSDave Airlie 		printk(KERN_INFO "radeon: switched off\n");
1231fbf81762SDave Airlie 		drm_kms_helper_poll_disable(dev);
12325bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1233274ad65cSJérome Glisse 		radeon_suspend_kms(dev, true, true, false);
12345bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_OFF;
12356a9ee8afSDave Airlie 	}
12366a9ee8afSDave Airlie }
12376a9ee8afSDave Airlie 
12380c195119SAlex Deucher /**
12390c195119SAlex Deucher  * radeon_switcheroo_can_switch - see if switcheroo state can change
12400c195119SAlex Deucher  *
12410c195119SAlex Deucher  * @pdev: pci dev pointer
12420c195119SAlex Deucher  *
12430c195119SAlex Deucher  * Callback for the switcheroo driver.  Check of the switcheroo
12440c195119SAlex Deucher  * state can be changed.
12450c195119SAlex Deucher  * Returns true if the state can be changed, false if not.
12460c195119SAlex Deucher  */
12476a9ee8afSDave Airlie static bool radeon_switcheroo_can_switch(struct pci_dev *pdev)
12486a9ee8afSDave Airlie {
12496a9ee8afSDave Airlie 	struct drm_device *dev = pci_get_drvdata(pdev);
12506a9ee8afSDave Airlie 
1251fc8fd40eSDaniel Vetter 	/*
1252fc8fd40eSDaniel Vetter 	 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1253fc8fd40eSDaniel Vetter 	 * locking inversion with the driver load path. And the access here is
1254fc8fd40eSDaniel Vetter 	 * completely racy anyway. So don't bother with locking for now.
1255fc8fd40eSDaniel Vetter 	 */
1256fc8fd40eSDaniel Vetter 	return dev->open_count == 0;
12576a9ee8afSDave Airlie }
12586a9ee8afSDave Airlie 
125926ec685fSTakashi Iwai static const struct vga_switcheroo_client_ops radeon_switcheroo_ops = {
126026ec685fSTakashi Iwai 	.set_gpu_state = radeon_switcheroo_set_state,
126126ec685fSTakashi Iwai 	.reprobe = NULL,
126226ec685fSTakashi Iwai 	.can_switch = radeon_switcheroo_can_switch,
126326ec685fSTakashi Iwai };
12646a9ee8afSDave Airlie 
12650c195119SAlex Deucher /**
12660c195119SAlex Deucher  * radeon_device_init - initialize the driver
12670c195119SAlex Deucher  *
12680c195119SAlex Deucher  * @rdev: radeon_device pointer
12690c195119SAlex Deucher  * @pdev: drm dev pointer
12700c195119SAlex Deucher  * @pdev: pci dev pointer
12710c195119SAlex Deucher  * @flags: driver flags
12720c195119SAlex Deucher  *
12730c195119SAlex Deucher  * Initializes the driver info and hw (all asics).
12740c195119SAlex Deucher  * Returns 0 for success or an error on failure.
12750c195119SAlex Deucher  * Called at driver startup.
12760c195119SAlex Deucher  */
1277771fe6b9SJerome Glisse int radeon_device_init(struct radeon_device *rdev,
1278771fe6b9SJerome Glisse 		       struct drm_device *ddev,
1279771fe6b9SJerome Glisse 		       struct pci_dev *pdev,
1280771fe6b9SJerome Glisse 		       uint32_t flags)
1281771fe6b9SJerome Glisse {
1282351a52a2SAlex Deucher 	int r, i;
1283ad49f501SDave Airlie 	int dma_bits;
128410ebc0bcSDave Airlie 	bool runtime = false;
1285771fe6b9SJerome Glisse 
1286771fe6b9SJerome Glisse 	rdev->shutdown = false;
12879f022ddfSJerome Glisse 	rdev->dev = &pdev->dev;
1288771fe6b9SJerome Glisse 	rdev->ddev = ddev;
1289771fe6b9SJerome Glisse 	rdev->pdev = pdev;
1290771fe6b9SJerome Glisse 	rdev->flags = flags;
1291771fe6b9SJerome Glisse 	rdev->family = flags & RADEON_FAMILY_MASK;
1292771fe6b9SJerome Glisse 	rdev->is_atom_bios = false;
1293771fe6b9SJerome Glisse 	rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
1294edcd26e8SAlex Deucher 	rdev->mc.gtt_size = 512 * 1024 * 1024;
1295733289c2SJerome Glisse 	rdev->accel_working = false;
12968b25ed34SAlex Deucher 	/* set up ring ids */
12978b25ed34SAlex Deucher 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
12988b25ed34SAlex Deucher 		rdev->ring[i].idx = i;
12998b25ed34SAlex Deucher 	}
1300954605caSMaarten Lankhorst 	rdev->fence_context = fence_context_alloc(RADEON_NUM_RINGS);
13011b5331d9SJerome Glisse 
1302fe0d36e0SAlex Deucher 	DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1303d522d9ccSThomas Reim 		 radeon_family_name[rdev->family], pdev->vendor, pdev->device,
1304fe0d36e0SAlex Deucher 		 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
13051b5331d9SJerome Glisse 
1306771fe6b9SJerome Glisse 	/* mutex initialization are all done here so we
1307771fe6b9SJerome Glisse 	 * can recall function without having locking issues */
1308d6999bc7SChristian König 	mutex_init(&rdev->ring_lock);
130940bacf16SAlex Deucher 	mutex_init(&rdev->dc_hw_i2c_mutex);
1310c20dc369SChristian Koenig 	atomic_set(&rdev->ih.lock, 0);
13114c788679SJerome Glisse 	mutex_init(&rdev->gem.mutex);
1312c913e23aSRafał Miłecki 	mutex_init(&rdev->pm.mutex);
13136759a0a7SMarek Olšák 	mutex_init(&rdev->gpu_clock_mutex);
1314f61d5b46SAlex Deucher 	mutex_init(&rdev->srbm_mutex);
13151c0a4625SOded Gabbay 	mutex_init(&rdev->grbm_idx_mutex);
1316db7fce39SChristian König 	init_rwsem(&rdev->pm.mclk_lock);
1317dee53e7fSJerome Glisse 	init_rwsem(&rdev->exclusive_lock);
131873a6d3fcSRafał Miłecki 	init_waitqueue_head(&rdev->irq.vblank_queue);
1319341cb9e4SChristian König 	mutex_init(&rdev->mn_lock);
1320341cb9e4SChristian König 	hash_init(rdev->mn_hash);
13211b9c3dd0SAlex Deucher 	r = radeon_gem_init(rdev);
13221b9c3dd0SAlex Deucher 	if (r)
13231b9c3dd0SAlex Deucher 		return r;
1324529364e0SChristian König 
1325c1c44132SChristian König 	radeon_check_arguments(rdev);
132623d4f1f2SAlex Deucher 	/* Adjust VM size here.
1327c1c44132SChristian König 	 * Max GPUVM size for cayman+ is 40 bits.
132823d4f1f2SAlex Deucher 	 */
132920b2656dSChristian König 	rdev->vm_manager.max_pfn = radeon_vm_size << 18;
1330771fe6b9SJerome Glisse 
13314aac0473SJerome Glisse 	/* Set asic functions */
13324aac0473SJerome Glisse 	r = radeon_asic_init(rdev);
133336421338SJerome Glisse 	if (r)
13344aac0473SJerome Glisse 		return r;
13354aac0473SJerome Glisse 
1336f95df9caSAlex Deucher 	/* all of the newer IGP chips have an internal gart
1337f95df9caSAlex Deucher 	 * However some rs4xx report as AGP, so remove that here.
1338f95df9caSAlex Deucher 	 */
1339f95df9caSAlex Deucher 	if ((rdev->family >= CHIP_RS400) &&
1340f95df9caSAlex Deucher 	    (rdev->flags & RADEON_IS_IGP)) {
1341f95df9caSAlex Deucher 		rdev->flags &= ~RADEON_IS_AGP;
1342f95df9caSAlex Deucher 	}
1343f95df9caSAlex Deucher 
134430256a3fSJerome Glisse 	if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
1345b574f251SJerome Glisse 		radeon_agp_disable(rdev);
1346771fe6b9SJerome Glisse 	}
1347771fe6b9SJerome Glisse 
13489ed8b1f9SAlex Deucher 	/* Set the internal MC address mask
13499ed8b1f9SAlex Deucher 	 * This is the max address of the GPU's
13509ed8b1f9SAlex Deucher 	 * internal address space.
13519ed8b1f9SAlex Deucher 	 */
13529ed8b1f9SAlex Deucher 	if (rdev->family >= CHIP_CAYMAN)
13539ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xffffffffffULL; /* 40 bit MC */
13549ed8b1f9SAlex Deucher 	else if (rdev->family >= CHIP_CEDAR)
13559ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xfffffffffULL; /* 36 bit MC */
13569ed8b1f9SAlex Deucher 	else
13579ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xffffffffULL; /* 32 bit MC */
13589ed8b1f9SAlex Deucher 
1359ad49f501SDave Airlie 	/* set DMA mask + need_dma32 flags.
1360ad49f501SDave Airlie 	 * PCIE - can handle 40-bits.
1361005a83f1SAlex Deucher 	 * IGP - can handle 40-bits
1362ad49f501SDave Airlie 	 * AGP - generally dma32 is safest
1363005a83f1SAlex Deucher 	 * PCI - dma32 for legacy pci gart, 40 bits on newer asics
1364ad49f501SDave Airlie 	 */
1365ad49f501SDave Airlie 	rdev->need_dma32 = false;
1366ad49f501SDave Airlie 	if (rdev->flags & RADEON_IS_AGP)
1367ad49f501SDave Airlie 		rdev->need_dma32 = true;
1368005a83f1SAlex Deucher 	if ((rdev->flags & RADEON_IS_PCI) &&
13694a2b6662SJerome Glisse 	    (rdev->family <= CHIP_RS740))
1370ad49f501SDave Airlie 		rdev->need_dma32 = true;
1371ad49f501SDave Airlie 
1372ad49f501SDave Airlie 	dma_bits = rdev->need_dma32 ? 32 : 40;
1373ad49f501SDave Airlie 	r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
1374771fe6b9SJerome Glisse 	if (r) {
137562fff811SDaniel Haid 		rdev->need_dma32 = true;
1376c52494f6SKonrad Rzeszutek Wilk 		dma_bits = 32;
1377771fe6b9SJerome Glisse 		printk(KERN_WARNING "radeon: No suitable DMA available.\n");
1378771fe6b9SJerome Glisse 	}
1379c52494f6SKonrad Rzeszutek Wilk 	r = pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
1380c52494f6SKonrad Rzeszutek Wilk 	if (r) {
1381c52494f6SKonrad Rzeszutek Wilk 		pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(32));
1382c52494f6SKonrad Rzeszutek Wilk 		printk(KERN_WARNING "radeon: No coherent DMA available.\n");
1383c52494f6SKonrad Rzeszutek Wilk 	}
1384771fe6b9SJerome Glisse 
1385771fe6b9SJerome Glisse 	/* Registers mapping */
1386771fe6b9SJerome Glisse 	/* TODO: block userspace mapping of io register */
13872c385151SDaniel Vetter 	spin_lock_init(&rdev->mmio_idx_lock);
1388fe78118cSAlex Deucher 	spin_lock_init(&rdev->smc_idx_lock);
13890a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pll_idx_lock);
13900a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->mc_idx_lock);
13910a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pcie_idx_lock);
13920a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pciep_idx_lock);
13930a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pif_idx_lock);
13940a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->cg_idx_lock);
13950a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->uvd_idx_lock);
13960a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->rcu_idx_lock);
13970a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->didt_idx_lock);
13980a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->end_idx_lock);
1399efad86dbSAlex Deucher 	if (rdev->family >= CHIP_BONAIRE) {
1400efad86dbSAlex Deucher 		rdev->rmmio_base = pci_resource_start(rdev->pdev, 5);
1401efad86dbSAlex Deucher 		rdev->rmmio_size = pci_resource_len(rdev->pdev, 5);
1402efad86dbSAlex Deucher 	} else {
140301d73a69SJordan Crouse 		rdev->rmmio_base = pci_resource_start(rdev->pdev, 2);
140401d73a69SJordan Crouse 		rdev->rmmio_size = pci_resource_len(rdev->pdev, 2);
1405efad86dbSAlex Deucher 	}
1406771fe6b9SJerome Glisse 	rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size);
1407771fe6b9SJerome Glisse 	if (rdev->rmmio == NULL) {
1408771fe6b9SJerome Glisse 		return -ENOMEM;
1409771fe6b9SJerome Glisse 	}
1410771fe6b9SJerome Glisse 	DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base);
1411771fe6b9SJerome Glisse 	DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size);
1412771fe6b9SJerome Glisse 
141375efdee1SAlex Deucher 	/* doorbell bar mapping */
141475efdee1SAlex Deucher 	if (rdev->family >= CHIP_BONAIRE)
141575efdee1SAlex Deucher 		radeon_doorbell_init(rdev);
141675efdee1SAlex Deucher 
1417351a52a2SAlex Deucher 	/* io port mapping */
1418351a52a2SAlex Deucher 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1419351a52a2SAlex Deucher 		if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) {
1420351a52a2SAlex Deucher 			rdev->rio_mem_size = pci_resource_len(rdev->pdev, i);
1421351a52a2SAlex Deucher 			rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size);
1422351a52a2SAlex Deucher 			break;
1423351a52a2SAlex Deucher 		}
1424351a52a2SAlex Deucher 	}
1425351a52a2SAlex Deucher 	if (rdev->rio_mem == NULL)
1426351a52a2SAlex Deucher 		DRM_ERROR("Unable to find PCI I/O BAR\n");
1427351a52a2SAlex Deucher 
14284807c5a8SAlex Deucher 	if (rdev->flags & RADEON_IS_PX)
14294807c5a8SAlex Deucher 		radeon_device_handle_px_quirks(rdev);
14304807c5a8SAlex Deucher 
143128d52043SDave Airlie 	/* if we have > 1 VGA cards, then disable the radeon VGA resources */
143293239ea1SDave Airlie 	/* this will fail for cards that aren't VGA class devices, just
143393239ea1SDave Airlie 	 * ignore it */
143493239ea1SDave Airlie 	vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
143510ebc0bcSDave Airlie 
1436bfaddd9fSAlex Deucher 	if (rdev->flags & RADEON_IS_PX)
143710ebc0bcSDave Airlie 		runtime = true;
143810ebc0bcSDave Airlie 	vga_switcheroo_register_client(rdev->pdev, &radeon_switcheroo_ops, runtime);
143910ebc0bcSDave Airlie 	if (runtime)
144010ebc0bcSDave Airlie 		vga_switcheroo_init_domain_pm_ops(rdev->dev, &rdev->vga_pm_domain);
144128d52043SDave Airlie 
14423ce0a23dSJerome Glisse 	r = radeon_init(rdev);
1443b574f251SJerome Glisse 	if (r)
14442e97140dSAlex Deucher 		goto failed;
1445b1e3a6d1SMichel Dänzer 
1446409851f4SJerome Glisse 	r = radeon_gem_debugfs_init(rdev);
1447409851f4SJerome Glisse 	if (r) {
1448409851f4SJerome Glisse 		DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1449409851f4SJerome Glisse 	}
1450409851f4SJerome Glisse 
14519843ead0SDave Airlie 	r = radeon_mst_debugfs_init(rdev);
14529843ead0SDave Airlie 	if (r) {
14539843ead0SDave Airlie 		DRM_ERROR("registering mst debugfs failed (%d).\n", r);
14549843ead0SDave Airlie 	}
14559843ead0SDave Airlie 
1456b574f251SJerome Glisse 	if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
1457b574f251SJerome Glisse 		/* Acceleration not working on AGP card try again
1458b574f251SJerome Glisse 		 * with fallback to PCI or PCIE GART
1459b574f251SJerome Glisse 		 */
1460a2d07b74SJerome Glisse 		radeon_asic_reset(rdev);
1461b574f251SJerome Glisse 		radeon_fini(rdev);
1462b574f251SJerome Glisse 		radeon_agp_disable(rdev);
1463b574f251SJerome Glisse 		r = radeon_init(rdev);
14644aac0473SJerome Glisse 		if (r)
14652e97140dSAlex Deucher 			goto failed;
14663ce0a23dSJerome Glisse 	}
14676c7bcceaSAlex Deucher 
146813a7d299SChristian König 	r = radeon_ib_ring_tests(rdev);
146913a7d299SChristian König 	if (r)
147013a7d299SChristian König 		DRM_ERROR("ib ring test failed (%d).\n", r);
147113a7d299SChristian König 
14726dfd1972SJérôme Glisse 	/*
14736dfd1972SJérôme Glisse 	 * Turks/Thames GPU will freeze whole laptop if DPM is not restarted
14746dfd1972SJérôme Glisse 	 * after the CP ring have chew one packet at least. Hence here we stop
14756dfd1972SJérôme Glisse 	 * and restart DPM after the radeon_ib_ring_tests().
14766dfd1972SJérôme Glisse 	 */
14776dfd1972SJérôme Glisse 	if (rdev->pm.dpm_enabled &&
14786dfd1972SJérôme Glisse 	    (rdev->pm.pm_method == PM_METHOD_DPM) &&
14796dfd1972SJérôme Glisse 	    (rdev->family == CHIP_TURKS) &&
14806dfd1972SJérôme Glisse 	    (rdev->flags & RADEON_IS_MOBILITY)) {
14816dfd1972SJérôme Glisse 		mutex_lock(&rdev->pm.mutex);
14826dfd1972SJérôme Glisse 		radeon_dpm_disable(rdev);
14836dfd1972SJérôme Glisse 		radeon_dpm_enable(rdev);
14846dfd1972SJérôme Glisse 		mutex_unlock(&rdev->pm.mutex);
14856dfd1972SJérôme Glisse 	}
14866dfd1972SJérôme Glisse 
148760a7e396SChristian König 	if ((radeon_testing & 1)) {
14884a1132a0SAlex Deucher 		if (rdev->accel_working)
1489ecc0b326SMichel Dänzer 			radeon_test_moves(rdev);
14904a1132a0SAlex Deucher 		else
14914a1132a0SAlex Deucher 			DRM_INFO("radeon: acceleration disabled, skipping move tests\n");
1492ecc0b326SMichel Dänzer 	}
149360a7e396SChristian König 	if ((radeon_testing & 2)) {
14944a1132a0SAlex Deucher 		if (rdev->accel_working)
149560a7e396SChristian König 			radeon_test_syncing(rdev);
14964a1132a0SAlex Deucher 		else
14974a1132a0SAlex Deucher 			DRM_INFO("radeon: acceleration disabled, skipping sync tests\n");
149860a7e396SChristian König 	}
1499771fe6b9SJerome Glisse 	if (radeon_benchmarking) {
15004a1132a0SAlex Deucher 		if (rdev->accel_working)
1501638dd7dbSIlija Hadzic 			radeon_benchmark(rdev, radeon_benchmarking);
15024a1132a0SAlex Deucher 		else
15034a1132a0SAlex Deucher 			DRM_INFO("radeon: acceleration disabled, skipping benchmarks\n");
1504771fe6b9SJerome Glisse 	}
15056cf8a3f5SJerome Glisse 	return 0;
15062e97140dSAlex Deucher 
15072e97140dSAlex Deucher failed:
15082e97140dSAlex Deucher 	if (runtime)
15092e97140dSAlex Deucher 		vga_switcheroo_fini_domain_pm_ops(rdev->dev);
15102e97140dSAlex Deucher 	return r;
1511771fe6b9SJerome Glisse }
1512771fe6b9SJerome Glisse 
15134d8bf9aeSChristian König static void radeon_debugfs_remove_files(struct radeon_device *rdev);
15144d8bf9aeSChristian König 
15150c195119SAlex Deucher /**
15160c195119SAlex Deucher  * radeon_device_fini - tear down the driver
15170c195119SAlex Deucher  *
15180c195119SAlex Deucher  * @rdev: radeon_device pointer
15190c195119SAlex Deucher  *
15200c195119SAlex Deucher  * Tear down the driver info (all asics).
15210c195119SAlex Deucher  * Called at driver shutdown.
15220c195119SAlex Deucher  */
1523771fe6b9SJerome Glisse void radeon_device_fini(struct radeon_device *rdev)
1524771fe6b9SJerome Glisse {
1525771fe6b9SJerome Glisse 	DRM_INFO("radeon: finishing device.\n");
1526771fe6b9SJerome Glisse 	rdev->shutdown = true;
152790aca4d2SJerome Glisse 	/* evict vram memory */
152890aca4d2SJerome Glisse 	radeon_bo_evict_vram(rdev);
15293ce0a23dSJerome Glisse 	radeon_fini(rdev);
15306a9ee8afSDave Airlie 	vga_switcheroo_unregister_client(rdev->pdev);
15312e97140dSAlex Deucher 	if (rdev->flags & RADEON_IS_PX)
15322e97140dSAlex Deucher 		vga_switcheroo_fini_domain_pm_ops(rdev->dev);
1533c1176d6fSDave Airlie 	vga_client_register(rdev->pdev, NULL, NULL, NULL);
1534e0a2ca73SAlex Deucher 	if (rdev->rio_mem)
1535351a52a2SAlex Deucher 		pci_iounmap(rdev->pdev, rdev->rio_mem);
1536351a52a2SAlex Deucher 	rdev->rio_mem = NULL;
1537771fe6b9SJerome Glisse 	iounmap(rdev->rmmio);
1538771fe6b9SJerome Glisse 	rdev->rmmio = NULL;
153975efdee1SAlex Deucher 	if (rdev->family >= CHIP_BONAIRE)
154075efdee1SAlex Deucher 		radeon_doorbell_fini(rdev);
15414d8bf9aeSChristian König 	radeon_debugfs_remove_files(rdev);
1542771fe6b9SJerome Glisse }
1543771fe6b9SJerome Glisse 
1544771fe6b9SJerome Glisse 
1545771fe6b9SJerome Glisse /*
1546771fe6b9SJerome Glisse  * Suspend & resume.
1547771fe6b9SJerome Glisse  */
15480c195119SAlex Deucher /**
15490c195119SAlex Deucher  * radeon_suspend_kms - initiate device suspend
15500c195119SAlex Deucher  *
15510c195119SAlex Deucher  * @pdev: drm dev pointer
15520c195119SAlex Deucher  * @state: suspend state
15530c195119SAlex Deucher  *
15540c195119SAlex Deucher  * Puts the hw in the suspend state (all asics).
15550c195119SAlex Deucher  * Returns 0 for success or an error on failure.
15560c195119SAlex Deucher  * Called at driver suspend.
15570c195119SAlex Deucher  */
1558274ad65cSJérome Glisse int radeon_suspend_kms(struct drm_device *dev, bool suspend,
1559274ad65cSJérome Glisse 		       bool fbcon, bool freeze)
1560771fe6b9SJerome Glisse {
1561875c1866SDarren Jenkins 	struct radeon_device *rdev;
1562771fe6b9SJerome Glisse 	struct drm_crtc *crtc;
1563d8dcaa1dSAlex Deucher 	struct drm_connector *connector;
15647465280cSAlex Deucher 	int i, r;
1565771fe6b9SJerome Glisse 
1566875c1866SDarren Jenkins 	if (dev == NULL || dev->dev_private == NULL) {
1567771fe6b9SJerome Glisse 		return -ENODEV;
1568771fe6b9SJerome Glisse 	}
15697473e830SDave Airlie 
1570875c1866SDarren Jenkins 	rdev = dev->dev_private;
1571875c1866SDarren Jenkins 
15725bcf719bSDave Airlie 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
15736a9ee8afSDave Airlie 		return 0;
1574d8dcaa1dSAlex Deucher 
157586698c20SSeth Forshee 	drm_kms_helper_poll_disable(dev);
157686698c20SSeth Forshee 
15776adaed5bSDaniel Vetter 	drm_modeset_lock_all(dev);
1578d8dcaa1dSAlex Deucher 	/* turn off display hw */
1579d8dcaa1dSAlex Deucher 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1580d8dcaa1dSAlex Deucher 		drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1581d8dcaa1dSAlex Deucher 	}
15826adaed5bSDaniel Vetter 	drm_modeset_unlock_all(dev);
1583d8dcaa1dSAlex Deucher 
1584f3cbb17bSGrigori Goronzy 	/* unpin the front buffers and cursors */
1585771fe6b9SJerome Glisse 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1586f3cbb17bSGrigori Goronzy 		struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1587f4510a27SMatt Roper 		struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->primary->fb);
15884c788679SJerome Glisse 		struct radeon_bo *robj;
1589771fe6b9SJerome Glisse 
1590f3cbb17bSGrigori Goronzy 		if (radeon_crtc->cursor_bo) {
1591f3cbb17bSGrigori Goronzy 			struct radeon_bo *robj = gem_to_radeon_bo(radeon_crtc->cursor_bo);
1592f3cbb17bSGrigori Goronzy 			r = radeon_bo_reserve(robj, false);
1593f3cbb17bSGrigori Goronzy 			if (r == 0) {
1594f3cbb17bSGrigori Goronzy 				radeon_bo_unpin(robj);
1595f3cbb17bSGrigori Goronzy 				radeon_bo_unreserve(robj);
1596f3cbb17bSGrigori Goronzy 			}
1597f3cbb17bSGrigori Goronzy 		}
1598f3cbb17bSGrigori Goronzy 
1599771fe6b9SJerome Glisse 		if (rfb == NULL || rfb->obj == NULL) {
1600771fe6b9SJerome Glisse 			continue;
1601771fe6b9SJerome Glisse 		}
16027e4d15d9SDaniel Vetter 		robj = gem_to_radeon_bo(rfb->obj);
160338651674SDave Airlie 		/* don't unpin kernel fb objects */
160438651674SDave Airlie 		if (!radeon_fbdev_robj_is_fb(rdev, robj)) {
16054c788679SJerome Glisse 			r = radeon_bo_reserve(robj, false);
160638651674SDave Airlie 			if (r == 0) {
16074c788679SJerome Glisse 				radeon_bo_unpin(robj);
16084c788679SJerome Glisse 				radeon_bo_unreserve(robj);
16094c788679SJerome Glisse 			}
1610771fe6b9SJerome Glisse 		}
1611771fe6b9SJerome Glisse 	}
1612771fe6b9SJerome Glisse 	/* evict vram memory */
16134c788679SJerome Glisse 	radeon_bo_evict_vram(rdev);
16148a47cc9eSChristian König 
1615771fe6b9SJerome Glisse 	/* wait for gpu to finish processing current batch */
16165f8f635eSJerome Glisse 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
161737615527SChristian König 		r = radeon_fence_wait_empty(rdev, i);
16185f8f635eSJerome Glisse 		if (r) {
16195f8f635eSJerome Glisse 			/* delay GPU reset to resume */
1620eb98c709SChristian König 			radeon_fence_driver_force_completion(rdev, i);
16215f8f635eSJerome Glisse 		}
16225f8f635eSJerome Glisse 	}
1623771fe6b9SJerome Glisse 
1624f657c2a7SYang Zhao 	radeon_save_bios_scratch_regs(rdev);
1625f657c2a7SYang Zhao 
16263ce0a23dSJerome Glisse 	radeon_suspend(rdev);
1627d4877cf2SAlex Deucher 	radeon_hpd_fini(rdev);
1628771fe6b9SJerome Glisse 	/* evict remaining vram memory */
16294c788679SJerome Glisse 	radeon_bo_evict_vram(rdev);
1630771fe6b9SJerome Glisse 
163110b06122SJerome Glisse 	radeon_agp_suspend(rdev);
163210b06122SJerome Glisse 
1633771fe6b9SJerome Glisse 	pci_save_state(dev->pdev);
1634*ccaa2c12SJérôme Glisse 	if (freeze && rdev->family >= CHIP_CEDAR) {
1635274ad65cSJérome Glisse 		rdev->asic->asic_reset(rdev, true);
1636274ad65cSJérome Glisse 		pci_restore_state(dev->pdev);
1637274ad65cSJérome Glisse 	} else if (suspend) {
1638771fe6b9SJerome Glisse 		/* Shut down the device */
1639771fe6b9SJerome Glisse 		pci_disable_device(dev->pdev);
1640771fe6b9SJerome Glisse 		pci_set_power_state(dev->pdev, PCI_D3hot);
1641771fe6b9SJerome Glisse 	}
164210ebc0bcSDave Airlie 
164310ebc0bcSDave Airlie 	if (fbcon) {
1644ac751efaSTorben Hohn 		console_lock();
164538651674SDave Airlie 		radeon_fbdev_set_suspend(rdev, 1);
1646ac751efaSTorben Hohn 		console_unlock();
164710ebc0bcSDave Airlie 	}
1648771fe6b9SJerome Glisse 	return 0;
1649771fe6b9SJerome Glisse }
1650771fe6b9SJerome Glisse 
16510c195119SAlex Deucher /**
16520c195119SAlex Deucher  * radeon_resume_kms - initiate device resume
16530c195119SAlex Deucher  *
16540c195119SAlex Deucher  * @pdev: drm dev pointer
16550c195119SAlex Deucher  *
16560c195119SAlex Deucher  * Bring the hw back to operating state (all asics).
16570c195119SAlex Deucher  * Returns 0 for success or an error on failure.
16580c195119SAlex Deucher  * Called at driver resume.
16590c195119SAlex Deucher  */
166010ebc0bcSDave Airlie int radeon_resume_kms(struct drm_device *dev, bool resume, bool fbcon)
1661771fe6b9SJerome Glisse {
166209bdf591SCedric Godin 	struct drm_connector *connector;
1663771fe6b9SJerome Glisse 	struct radeon_device *rdev = dev->dev_private;
1664f3cbb17bSGrigori Goronzy 	struct drm_crtc *crtc;
166504eb2206SChristian König 	int r;
1666771fe6b9SJerome Glisse 
16675bcf719bSDave Airlie 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
16686a9ee8afSDave Airlie 		return 0;
16696a9ee8afSDave Airlie 
167010ebc0bcSDave Airlie 	if (fbcon) {
1671ac751efaSTorben Hohn 		console_lock();
167210ebc0bcSDave Airlie 	}
16737473e830SDave Airlie 	if (resume) {
1674771fe6b9SJerome Glisse 		pci_set_power_state(dev->pdev, PCI_D0);
1675771fe6b9SJerome Glisse 		pci_restore_state(dev->pdev);
1676771fe6b9SJerome Glisse 		if (pci_enable_device(dev->pdev)) {
167710ebc0bcSDave Airlie 			if (fbcon)
1678ac751efaSTorben Hohn 				console_unlock();
1679771fe6b9SJerome Glisse 			return -1;
1680771fe6b9SJerome Glisse 		}
16817473e830SDave Airlie 	}
16820ebf1717SDave Airlie 	/* resume AGP if in use */
16830ebf1717SDave Airlie 	radeon_agp_resume(rdev);
16843ce0a23dSJerome Glisse 	radeon_resume(rdev);
168504eb2206SChristian König 
168604eb2206SChristian König 	r = radeon_ib_ring_tests(rdev);
168704eb2206SChristian König 	if (r)
168804eb2206SChristian König 		DRM_ERROR("ib ring test failed (%d).\n", r);
168904eb2206SChristian König 
1690bc6a6295SAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
16916c7bcceaSAlex Deucher 		/* do dpm late init */
16926c7bcceaSAlex Deucher 		r = radeon_pm_late_init(rdev);
16936c7bcceaSAlex Deucher 		if (r) {
16946c7bcceaSAlex Deucher 			rdev->pm.dpm_enabled = false;
16956c7bcceaSAlex Deucher 			DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
16966c7bcceaSAlex Deucher 		}
1697bc6a6295SAlex Deucher 	} else {
1698bc6a6295SAlex Deucher 		/* resume old pm late */
1699bc6a6295SAlex Deucher 		radeon_pm_resume(rdev);
17006c7bcceaSAlex Deucher 	}
17016c7bcceaSAlex Deucher 
1702f657c2a7SYang Zhao 	radeon_restore_bios_scratch_regs(rdev);
170309bdf591SCedric Godin 
1704f3cbb17bSGrigori Goronzy 	/* pin cursors */
1705f3cbb17bSGrigori Goronzy 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1706f3cbb17bSGrigori Goronzy 		struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1707f3cbb17bSGrigori Goronzy 
1708f3cbb17bSGrigori Goronzy 		if (radeon_crtc->cursor_bo) {
1709f3cbb17bSGrigori Goronzy 			struct radeon_bo *robj = gem_to_radeon_bo(radeon_crtc->cursor_bo);
1710f3cbb17bSGrigori Goronzy 			r = radeon_bo_reserve(robj, false);
1711f3cbb17bSGrigori Goronzy 			if (r == 0) {
1712f3cbb17bSGrigori Goronzy 				/* Only 27 bit offset for legacy cursor */
1713f3cbb17bSGrigori Goronzy 				r = radeon_bo_pin_restricted(robj,
1714f3cbb17bSGrigori Goronzy 							     RADEON_GEM_DOMAIN_VRAM,
1715f3cbb17bSGrigori Goronzy 							     ASIC_IS_AVIVO(rdev) ?
1716f3cbb17bSGrigori Goronzy 							     0 : 1 << 27,
1717f3cbb17bSGrigori Goronzy 							     &radeon_crtc->cursor_addr);
1718f3cbb17bSGrigori Goronzy 				if (r != 0)
1719f3cbb17bSGrigori Goronzy 					DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
1720f3cbb17bSGrigori Goronzy 				radeon_bo_unreserve(robj);
1721f3cbb17bSGrigori Goronzy 			}
1722f3cbb17bSGrigori Goronzy 		}
1723f3cbb17bSGrigori Goronzy 	}
1724f3cbb17bSGrigori Goronzy 
17253fa47d9eSAlex Deucher 	/* init dig PHYs, disp eng pll */
17263fa47d9eSAlex Deucher 	if (rdev->is_atom_bios) {
1727ac89af1eSAlex Deucher 		radeon_atom_encoder_init(rdev);
1728f3f1f03eSAlex Deucher 		radeon_atom_disp_eng_pll_init(rdev);
1729bced76f2SAlex Deucher 		/* turn on the BL */
1730bced76f2SAlex Deucher 		if (rdev->mode_info.bl_encoder) {
1731bced76f2SAlex Deucher 			u8 bl_level = radeon_get_backlight_level(rdev,
1732bced76f2SAlex Deucher 								 rdev->mode_info.bl_encoder);
1733bced76f2SAlex Deucher 			radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
1734bced76f2SAlex Deucher 						   bl_level);
1735bced76f2SAlex Deucher 		}
17363fa47d9eSAlex Deucher 	}
1737d4877cf2SAlex Deucher 	/* reset hpd state */
1738d4877cf2SAlex Deucher 	radeon_hpd_init(rdev);
1739771fe6b9SJerome Glisse 	/* blat the mode back in */
1740ec9954fcSDave Airlie 	if (fbcon) {
1741771fe6b9SJerome Glisse 		drm_helper_resume_force_mode(dev);
1742a93f344dSAlex Deucher 		/* turn on display hw */
17436adaed5bSDaniel Vetter 		drm_modeset_lock_all(dev);
1744a93f344dSAlex Deucher 		list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1745a93f344dSAlex Deucher 			drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
1746a93f344dSAlex Deucher 		}
17476adaed5bSDaniel Vetter 		drm_modeset_unlock_all(dev);
1748ec9954fcSDave Airlie 	}
174986698c20SSeth Forshee 
175086698c20SSeth Forshee 	drm_kms_helper_poll_enable(dev);
175118ee37a4SDaniel Vetter 
17523640da2fSAlex Deucher 	/* set the power state here in case we are a PX system or headless */
17533640da2fSAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled)
17543640da2fSAlex Deucher 		radeon_pm_compute_clocks(rdev);
17553640da2fSAlex Deucher 
175618ee37a4SDaniel Vetter 	if (fbcon) {
175718ee37a4SDaniel Vetter 		radeon_fbdev_set_suspend(rdev, 0);
175818ee37a4SDaniel Vetter 		console_unlock();
175918ee37a4SDaniel Vetter 	}
176018ee37a4SDaniel Vetter 
1761771fe6b9SJerome Glisse 	return 0;
1762771fe6b9SJerome Glisse }
1763771fe6b9SJerome Glisse 
17640c195119SAlex Deucher /**
17650c195119SAlex Deucher  * radeon_gpu_reset - reset the asic
17660c195119SAlex Deucher  *
17670c195119SAlex Deucher  * @rdev: radeon device pointer
17680c195119SAlex Deucher  *
17690c195119SAlex Deucher  * Attempt the reset the GPU if it has hung (all asics).
17700c195119SAlex Deucher  * Returns 0 for success or an error on failure.
17710c195119SAlex Deucher  */
177290aca4d2SJerome Glisse int radeon_gpu_reset(struct radeon_device *rdev)
177390aca4d2SJerome Glisse {
177455d7c221SChristian König 	unsigned ring_sizes[RADEON_NUM_RINGS];
177555d7c221SChristian König 	uint32_t *ring_data[RADEON_NUM_RINGS];
177655d7c221SChristian König 
177755d7c221SChristian König 	bool saved = false;
177855d7c221SChristian König 
177955d7c221SChristian König 	int i, r;
17808fd1b84cSDave Airlie 	int resched;
178190aca4d2SJerome Glisse 
1782dee53e7fSJerome Glisse 	down_write(&rdev->exclusive_lock);
1783f9eaf9aeSChristian König 
1784f9eaf9aeSChristian König 	if (!rdev->needs_reset) {
1785f9eaf9aeSChristian König 		up_write(&rdev->exclusive_lock);
1786f9eaf9aeSChristian König 		return 0;
1787f9eaf9aeSChristian König 	}
1788f9eaf9aeSChristian König 
178972b9076bSMarek Olšák 	atomic_inc(&rdev->gpu_reset_counter);
179072b9076bSMarek Olšák 
179190aca4d2SJerome Glisse 	radeon_save_bios_scratch_regs(rdev);
17928fd1b84cSDave Airlie 	/* block TTM */
17938fd1b84cSDave Airlie 	resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
179490aca4d2SJerome Glisse 	radeon_suspend(rdev);
179573ef0e0dSAlex Deucher 	radeon_hpd_fini(rdev);
179690aca4d2SJerome Glisse 
179755d7c221SChristian König 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
179855d7c221SChristian König 		ring_sizes[i] = radeon_ring_backup(rdev, &rdev->ring[i],
179955d7c221SChristian König 						   &ring_data[i]);
180055d7c221SChristian König 		if (ring_sizes[i]) {
180155d7c221SChristian König 			saved = true;
180255d7c221SChristian König 			dev_info(rdev->dev, "Saved %d dwords of commands "
180355d7c221SChristian König 				 "on ring %d.\n", ring_sizes[i], i);
180455d7c221SChristian König 		}
180555d7c221SChristian König 	}
180655d7c221SChristian König 
180790aca4d2SJerome Glisse 	r = radeon_asic_reset(rdev);
180890aca4d2SJerome Glisse 	if (!r) {
180955d7c221SChristian König 		dev_info(rdev->dev, "GPU reset succeeded, trying to resume\n");
181090aca4d2SJerome Glisse 		radeon_resume(rdev);
181155d7c221SChristian König 	}
181204eb2206SChristian König 
181390aca4d2SJerome Glisse 	radeon_restore_bios_scratch_regs(rdev);
181455d7c221SChristian König 
181555d7c221SChristian König 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
18169bb39ff4SMaarten Lankhorst 		if (!r && ring_data[i]) {
181755d7c221SChristian König 			radeon_ring_restore(rdev, &rdev->ring[i],
181855d7c221SChristian König 					    ring_sizes[i], ring_data[i]);
181955d7c221SChristian König 		} else {
1820eb98c709SChristian König 			radeon_fence_driver_force_completion(rdev, i);
182155d7c221SChristian König 			kfree(ring_data[i]);
182255d7c221SChristian König 		}
182355d7c221SChristian König 	}
182455d7c221SChristian König 
1825c940b447SAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
1826c940b447SAlex Deucher 		/* do dpm late init */
1827c940b447SAlex Deucher 		r = radeon_pm_late_init(rdev);
1828c940b447SAlex Deucher 		if (r) {
1829c940b447SAlex Deucher 			rdev->pm.dpm_enabled = false;
1830c940b447SAlex Deucher 			DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
1831c940b447SAlex Deucher 		}
1832c940b447SAlex Deucher 	} else {
1833c940b447SAlex Deucher 		/* resume old pm late */
183495f59509SAlex Deucher 		radeon_pm_resume(rdev);
1835c940b447SAlex Deucher 	}
1836c940b447SAlex Deucher 
183773ef0e0dSAlex Deucher 	/* init dig PHYs, disp eng pll */
183873ef0e0dSAlex Deucher 	if (rdev->is_atom_bios) {
183973ef0e0dSAlex Deucher 		radeon_atom_encoder_init(rdev);
184073ef0e0dSAlex Deucher 		radeon_atom_disp_eng_pll_init(rdev);
184173ef0e0dSAlex Deucher 		/* turn on the BL */
184273ef0e0dSAlex Deucher 		if (rdev->mode_info.bl_encoder) {
184373ef0e0dSAlex Deucher 			u8 bl_level = radeon_get_backlight_level(rdev,
184473ef0e0dSAlex Deucher 								 rdev->mode_info.bl_encoder);
184573ef0e0dSAlex Deucher 			radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
184673ef0e0dSAlex Deucher 						   bl_level);
184773ef0e0dSAlex Deucher 		}
184873ef0e0dSAlex Deucher 	}
184973ef0e0dSAlex Deucher 	/* reset hpd state */
185073ef0e0dSAlex Deucher 	radeon_hpd_init(rdev);
185173ef0e0dSAlex Deucher 
18529bb39ff4SMaarten Lankhorst 	ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
18533c036389SChristian König 
18543c036389SChristian König 	rdev->in_reset = true;
18553c036389SChristian König 	rdev->needs_reset = false;
18563c036389SChristian König 
18579bb39ff4SMaarten Lankhorst 	downgrade_write(&rdev->exclusive_lock);
18589bb39ff4SMaarten Lankhorst 
1859d3493574SJerome Glisse 	drm_helper_resume_force_mode(rdev->ddev);
1860d3493574SJerome Glisse 
1861c940b447SAlex Deucher 	/* set the power state here in case we are a PX system or headless */
1862c940b447SAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled)
1863c940b447SAlex Deucher 		radeon_pm_compute_clocks(rdev);
1864c940b447SAlex Deucher 
18659bb39ff4SMaarten Lankhorst 	if (!r) {
18669bb39ff4SMaarten Lankhorst 		r = radeon_ib_ring_tests(rdev);
18679bb39ff4SMaarten Lankhorst 		if (r && saved)
18689bb39ff4SMaarten Lankhorst 			r = -EAGAIN;
18699bb39ff4SMaarten Lankhorst 	} else {
187090aca4d2SJerome Glisse 		/* bad news, how to tell it to userspace ? */
187190aca4d2SJerome Glisse 		dev_info(rdev->dev, "GPU reset failed\n");
18727a1619b9SMichel Dänzer 	}
18737a1619b9SMichel Dänzer 
18749bb39ff4SMaarten Lankhorst 	rdev->needs_reset = r == -EAGAIN;
18759bb39ff4SMaarten Lankhorst 	rdev->in_reset = false;
18769bb39ff4SMaarten Lankhorst 
18779bb39ff4SMaarten Lankhorst 	up_read(&rdev->exclusive_lock);
187890aca4d2SJerome Glisse 	return r;
187990aca4d2SJerome Glisse }
188090aca4d2SJerome Glisse 
1881771fe6b9SJerome Glisse 
1882771fe6b9SJerome Glisse /*
1883771fe6b9SJerome Glisse  * Debugfs
1884771fe6b9SJerome Glisse  */
1885771fe6b9SJerome Glisse int radeon_debugfs_add_files(struct radeon_device *rdev,
1886771fe6b9SJerome Glisse 			     struct drm_info_list *files,
1887771fe6b9SJerome Glisse 			     unsigned nfiles)
1888771fe6b9SJerome Glisse {
1889771fe6b9SJerome Glisse 	unsigned i;
1890771fe6b9SJerome Glisse 
18914d8bf9aeSChristian König 	for (i = 0; i < rdev->debugfs_count; i++) {
18924d8bf9aeSChristian König 		if (rdev->debugfs[i].files == files) {
1893771fe6b9SJerome Glisse 			/* Already registered */
1894771fe6b9SJerome Glisse 			return 0;
1895771fe6b9SJerome Glisse 		}
1896771fe6b9SJerome Glisse 	}
1897c245cb9eSMichael Witten 
18984d8bf9aeSChristian König 	i = rdev->debugfs_count + 1;
1899c245cb9eSMichael Witten 	if (i > RADEON_DEBUGFS_MAX_COMPONENTS) {
1900c245cb9eSMichael Witten 		DRM_ERROR("Reached maximum number of debugfs components.\n");
1901c245cb9eSMichael Witten 		DRM_ERROR("Report so we increase "
1902c245cb9eSMichael Witten 			  "RADEON_DEBUGFS_MAX_COMPONENTS.\n");
1903771fe6b9SJerome Glisse 		return -EINVAL;
1904771fe6b9SJerome Glisse 	}
19054d8bf9aeSChristian König 	rdev->debugfs[rdev->debugfs_count].files = files;
19064d8bf9aeSChristian König 	rdev->debugfs[rdev->debugfs_count].num_files = nfiles;
19074d8bf9aeSChristian König 	rdev->debugfs_count = i;
1908771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS)
1909771fe6b9SJerome Glisse 	drm_debugfs_create_files(files, nfiles,
1910771fe6b9SJerome Glisse 				 rdev->ddev->control->debugfs_root,
1911771fe6b9SJerome Glisse 				 rdev->ddev->control);
1912771fe6b9SJerome Glisse 	drm_debugfs_create_files(files, nfiles,
1913771fe6b9SJerome Glisse 				 rdev->ddev->primary->debugfs_root,
1914771fe6b9SJerome Glisse 				 rdev->ddev->primary);
1915771fe6b9SJerome Glisse #endif
1916771fe6b9SJerome Glisse 	return 0;
1917771fe6b9SJerome Glisse }
1918771fe6b9SJerome Glisse 
19194d8bf9aeSChristian König static void radeon_debugfs_remove_files(struct radeon_device *rdev)
19204d8bf9aeSChristian König {
19214d8bf9aeSChristian König #if defined(CONFIG_DEBUG_FS)
19224d8bf9aeSChristian König 	unsigned i;
19234d8bf9aeSChristian König 
19244d8bf9aeSChristian König 	for (i = 0; i < rdev->debugfs_count; i++) {
19254d8bf9aeSChristian König 		drm_debugfs_remove_files(rdev->debugfs[i].files,
19264d8bf9aeSChristian König 					 rdev->debugfs[i].num_files,
19274d8bf9aeSChristian König 					 rdev->ddev->control);
19284d8bf9aeSChristian König 		drm_debugfs_remove_files(rdev->debugfs[i].files,
19294d8bf9aeSChristian König 					 rdev->debugfs[i].num_files,
19304d8bf9aeSChristian König 					 rdev->ddev->primary);
19314d8bf9aeSChristian König 	}
19324d8bf9aeSChristian König #endif
19334d8bf9aeSChristian König }
19344d8bf9aeSChristian König 
1935771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS)
1936771fe6b9SJerome Glisse int radeon_debugfs_init(struct drm_minor *minor)
1937771fe6b9SJerome Glisse {
1938771fe6b9SJerome Glisse 	return 0;
1939771fe6b9SJerome Glisse }
1940771fe6b9SJerome Glisse 
1941771fe6b9SJerome Glisse void radeon_debugfs_cleanup(struct drm_minor *minor)
1942771fe6b9SJerome Glisse {
1943771fe6b9SJerome Glisse }
1944771fe6b9SJerome Glisse #endif
1945