xref: /openbmc/linux/drivers/gpu/drm/radeon/radeon_device.c (revision 05082b8bbd1a0ffc74235449c4b8930a8c240f85)
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  */
633*05082b8bSAlex Deucher 
634*05082b8bSAlex Deucher /**
635*05082b8bSAlex Deucher  * radeon_device_is_virtual - check if we are running is a virtual environment
636*05082b8bSAlex Deucher  *
637*05082b8bSAlex Deucher  * Check if the asic has been passed through to a VM (all asics).
638*05082b8bSAlex Deucher  * Used at driver startup.
639*05082b8bSAlex Deucher  * Returns true if virtual or false if not.
640*05082b8bSAlex Deucher  */
641*05082b8bSAlex Deucher static bool radeon_device_is_virtual(void)
642*05082b8bSAlex Deucher {
643*05082b8bSAlex Deucher #ifdef CONFIG_X86
644*05082b8bSAlex Deucher 	return boot_cpu_has(X86_FEATURE_HYPERVISOR);
645*05082b8bSAlex Deucher #else
646*05082b8bSAlex Deucher 	return false;
647*05082b8bSAlex Deucher #endif
648*05082b8bSAlex Deucher }
649*05082b8bSAlex Deucher 
6500c195119SAlex Deucher /**
6510c195119SAlex Deucher  * radeon_card_posted - check if the hw has already been initialized
6520c195119SAlex Deucher  *
6530c195119SAlex Deucher  * @rdev: radeon_device pointer
6540c195119SAlex Deucher  *
6550c195119SAlex Deucher  * Check if the asic has been initialized (all asics).
6560c195119SAlex Deucher  * Used at driver startup.
6570c195119SAlex Deucher  * Returns true if initialized or false if not.
6580c195119SAlex Deucher  */
6599f022ddfSJerome Glisse bool radeon_card_posted(struct radeon_device *rdev)
660771fe6b9SJerome Glisse {
661771fe6b9SJerome Glisse 	uint32_t reg;
662771fe6b9SJerome Glisse 
663*05082b8bSAlex Deucher 	/* for pass through, always force asic_init */
664*05082b8bSAlex Deucher 	if (radeon_device_is_virtual())
665*05082b8bSAlex Deucher 		return false;
666*05082b8bSAlex Deucher 
66750a583f6SAlex Deucher 	/* required for EFI mode on macbook2,1 which uses an r5xx asic */
66883e68189SMatt Fleming 	if (efi_enabled(EFI_BOOT) &&
66950a583f6SAlex Deucher 	    (rdev->pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE) &&
67050a583f6SAlex Deucher 	    (rdev->family < CHIP_R600))
671bcc65fd8SMatthew Garrett 		return false;
672bcc65fd8SMatthew Garrett 
6732cf3a4fcSAlex Deucher 	if (ASIC_IS_NODCE(rdev))
6742cf3a4fcSAlex Deucher 		goto check_memsize;
6752cf3a4fcSAlex Deucher 
676771fe6b9SJerome Glisse 	/* first check CRTCs */
67709fb8bd1SAlex Deucher 	if (ASIC_IS_DCE4(rdev)) {
67818007401SAlex Deucher 		reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
67918007401SAlex Deucher 			RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET);
68009fb8bd1SAlex Deucher 			if (rdev->num_crtc >= 4) {
68109fb8bd1SAlex Deucher 				reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) |
68209fb8bd1SAlex Deucher 					RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET);
68309fb8bd1SAlex Deucher 			}
68409fb8bd1SAlex Deucher 			if (rdev->num_crtc >= 6) {
68509fb8bd1SAlex Deucher 				reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) |
686bcc1c2a1SAlex Deucher 					RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET);
68709fb8bd1SAlex Deucher 			}
688bcc1c2a1SAlex Deucher 		if (reg & EVERGREEN_CRTC_MASTER_EN)
689bcc1c2a1SAlex Deucher 			return true;
690bcc1c2a1SAlex Deucher 	} else if (ASIC_IS_AVIVO(rdev)) {
691771fe6b9SJerome Glisse 		reg = RREG32(AVIVO_D1CRTC_CONTROL) |
692771fe6b9SJerome Glisse 		      RREG32(AVIVO_D2CRTC_CONTROL);
693771fe6b9SJerome Glisse 		if (reg & AVIVO_CRTC_EN) {
694771fe6b9SJerome Glisse 			return true;
695771fe6b9SJerome Glisse 		}
696771fe6b9SJerome Glisse 	} else {
697771fe6b9SJerome Glisse 		reg = RREG32(RADEON_CRTC_GEN_CNTL) |
698771fe6b9SJerome Glisse 		      RREG32(RADEON_CRTC2_GEN_CNTL);
699771fe6b9SJerome Glisse 		if (reg & RADEON_CRTC_EN) {
700771fe6b9SJerome Glisse 			return true;
701771fe6b9SJerome Glisse 		}
702771fe6b9SJerome Glisse 	}
703771fe6b9SJerome Glisse 
7042cf3a4fcSAlex Deucher check_memsize:
705771fe6b9SJerome Glisse 	/* then check MEM_SIZE, in case the crtcs are off */
706771fe6b9SJerome Glisse 	if (rdev->family >= CHIP_R600)
707771fe6b9SJerome Glisse 		reg = RREG32(R600_CONFIG_MEMSIZE);
708771fe6b9SJerome Glisse 	else
709771fe6b9SJerome Glisse 		reg = RREG32(RADEON_CONFIG_MEMSIZE);
710771fe6b9SJerome Glisse 
711771fe6b9SJerome Glisse 	if (reg)
712771fe6b9SJerome Glisse 		return true;
713771fe6b9SJerome Glisse 
714771fe6b9SJerome Glisse 	return false;
715771fe6b9SJerome Glisse 
716771fe6b9SJerome Glisse }
717771fe6b9SJerome Glisse 
7180c195119SAlex Deucher /**
7190c195119SAlex Deucher  * radeon_update_bandwidth_info - update display bandwidth params
7200c195119SAlex Deucher  *
7210c195119SAlex Deucher  * @rdev: radeon_device pointer
7220c195119SAlex Deucher  *
7230c195119SAlex Deucher  * Used when sclk/mclk are switched or display modes are set.
7240c195119SAlex Deucher  * params are used to calculate display watermarks (all asics)
7250c195119SAlex Deucher  */
726f47299c5SAlex Deucher void radeon_update_bandwidth_info(struct radeon_device *rdev)
727f47299c5SAlex Deucher {
728f47299c5SAlex Deucher 	fixed20_12 a;
7298807286eSAlex Deucher 	u32 sclk = rdev->pm.current_sclk;
7308807286eSAlex Deucher 	u32 mclk = rdev->pm.current_mclk;
731f47299c5SAlex Deucher 
7328807286eSAlex Deucher 	/* sclk/mclk in Mhz */
73368adac5eSBen Skeggs 	a.full = dfixed_const(100);
73468adac5eSBen Skeggs 	rdev->pm.sclk.full = dfixed_const(sclk);
73568adac5eSBen Skeggs 	rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a);
73668adac5eSBen Skeggs 	rdev->pm.mclk.full = dfixed_const(mclk);
73768adac5eSBen Skeggs 	rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a);
738f47299c5SAlex Deucher 
7398807286eSAlex Deucher 	if (rdev->flags & RADEON_IS_IGP) {
74068adac5eSBen Skeggs 		a.full = dfixed_const(16);
741f47299c5SAlex Deucher 		/* core_bandwidth = sclk(Mhz) * 16 */
74268adac5eSBen Skeggs 		rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a);
743f47299c5SAlex Deucher 	}
744f47299c5SAlex Deucher }
745f47299c5SAlex Deucher 
7460c195119SAlex Deucher /**
7470c195119SAlex Deucher  * radeon_boot_test_post_card - check and possibly initialize the hw
7480c195119SAlex Deucher  *
7490c195119SAlex Deucher  * @rdev: radeon_device pointer
7500c195119SAlex Deucher  *
7510c195119SAlex Deucher  * Check if the asic is initialized and if not, attempt to initialize
7520c195119SAlex Deucher  * it (all asics).
7530c195119SAlex Deucher  * Returns true if initialized or false if not.
7540c195119SAlex Deucher  */
75572542d77SDave Airlie bool radeon_boot_test_post_card(struct radeon_device *rdev)
75672542d77SDave Airlie {
75772542d77SDave Airlie 	if (radeon_card_posted(rdev))
75872542d77SDave Airlie 		return true;
75972542d77SDave Airlie 
76072542d77SDave Airlie 	if (rdev->bios) {
76172542d77SDave Airlie 		DRM_INFO("GPU not posted. posting now...\n");
76272542d77SDave Airlie 		if (rdev->is_atom_bios)
76372542d77SDave Airlie 			atom_asic_init(rdev->mode_info.atom_context);
76472542d77SDave Airlie 		else
76572542d77SDave Airlie 			radeon_combios_asic_init(rdev->ddev);
76672542d77SDave Airlie 		return true;
76772542d77SDave Airlie 	} else {
76872542d77SDave Airlie 		dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
76972542d77SDave Airlie 		return false;
77072542d77SDave Airlie 	}
77172542d77SDave Airlie }
77272542d77SDave Airlie 
7730c195119SAlex Deucher /**
7740c195119SAlex Deucher  * radeon_dummy_page_init - init dummy page used by the driver
7750c195119SAlex Deucher  *
7760c195119SAlex Deucher  * @rdev: radeon_device pointer
7770c195119SAlex Deucher  *
7780c195119SAlex Deucher  * Allocate the dummy page used by the driver (all asics).
7790c195119SAlex Deucher  * This dummy page is used by the driver as a filler for gart entries
7800c195119SAlex Deucher  * when pages are taken out of the GART
7810c195119SAlex Deucher  * Returns 0 on sucess, -ENOMEM on failure.
7820c195119SAlex Deucher  */
7833ce0a23dSJerome Glisse int radeon_dummy_page_init(struct radeon_device *rdev)
7843ce0a23dSJerome Glisse {
78582568565SDave Airlie 	if (rdev->dummy_page.page)
78682568565SDave Airlie 		return 0;
7873ce0a23dSJerome Glisse 	rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
7883ce0a23dSJerome Glisse 	if (rdev->dummy_page.page == NULL)
7893ce0a23dSJerome Glisse 		return -ENOMEM;
7903ce0a23dSJerome Glisse 	rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page,
7913ce0a23dSJerome Glisse 					0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
792a30f6fb7SBenjamin Herrenschmidt 	if (pci_dma_mapping_error(rdev->pdev, rdev->dummy_page.addr)) {
793a30f6fb7SBenjamin Herrenschmidt 		dev_err(&rdev->pdev->dev, "Failed to DMA MAP the dummy page\n");
7943ce0a23dSJerome Glisse 		__free_page(rdev->dummy_page.page);
7953ce0a23dSJerome Glisse 		rdev->dummy_page.page = NULL;
7963ce0a23dSJerome Glisse 		return -ENOMEM;
7973ce0a23dSJerome Glisse 	}
798cb658906SMichel Dänzer 	rdev->dummy_page.entry = radeon_gart_get_page_entry(rdev->dummy_page.addr,
799cb658906SMichel Dänzer 							    RADEON_GART_PAGE_DUMMY);
8003ce0a23dSJerome Glisse 	return 0;
8013ce0a23dSJerome Glisse }
8023ce0a23dSJerome Glisse 
8030c195119SAlex Deucher /**
8040c195119SAlex Deucher  * radeon_dummy_page_fini - free dummy page used by the driver
8050c195119SAlex Deucher  *
8060c195119SAlex Deucher  * @rdev: radeon_device pointer
8070c195119SAlex Deucher  *
8080c195119SAlex Deucher  * Frees the dummy page used by the driver (all asics).
8090c195119SAlex Deucher  */
8103ce0a23dSJerome Glisse void radeon_dummy_page_fini(struct radeon_device *rdev)
8113ce0a23dSJerome Glisse {
8123ce0a23dSJerome Glisse 	if (rdev->dummy_page.page == NULL)
8133ce0a23dSJerome Glisse 		return;
8143ce0a23dSJerome Glisse 	pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
8153ce0a23dSJerome Glisse 			PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
8163ce0a23dSJerome Glisse 	__free_page(rdev->dummy_page.page);
8173ce0a23dSJerome Glisse 	rdev->dummy_page.page = NULL;
8183ce0a23dSJerome Glisse }
8193ce0a23dSJerome Glisse 
820771fe6b9SJerome Glisse 
821771fe6b9SJerome Glisse /* ATOM accessor methods */
8220c195119SAlex Deucher /*
8230c195119SAlex Deucher  * ATOM is an interpreted byte code stored in tables in the vbios.  The
8240c195119SAlex Deucher  * driver registers callbacks to access registers and the interpreter
8250c195119SAlex Deucher  * in the driver parses the tables and executes then to program specific
8260c195119SAlex Deucher  * actions (set display modes, asic init, etc.).  See radeon_atombios.c,
8270c195119SAlex Deucher  * atombios.h, and atom.c
8280c195119SAlex Deucher  */
8290c195119SAlex Deucher 
8300c195119SAlex Deucher /**
8310c195119SAlex Deucher  * cail_pll_read - read PLL register
8320c195119SAlex Deucher  *
8330c195119SAlex Deucher  * @info: atom card_info pointer
8340c195119SAlex Deucher  * @reg: PLL register offset
8350c195119SAlex Deucher  *
8360c195119SAlex Deucher  * Provides a PLL register accessor for the atom interpreter (r4xx+).
8370c195119SAlex Deucher  * Returns the value of the PLL register.
8380c195119SAlex Deucher  */
839771fe6b9SJerome Glisse static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
840771fe6b9SJerome Glisse {
841771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
842771fe6b9SJerome Glisse 	uint32_t r;
843771fe6b9SJerome Glisse 
844771fe6b9SJerome Glisse 	r = rdev->pll_rreg(rdev, reg);
845771fe6b9SJerome Glisse 	return r;
846771fe6b9SJerome Glisse }
847771fe6b9SJerome Glisse 
8480c195119SAlex Deucher /**
8490c195119SAlex Deucher  * cail_pll_write - write PLL register
8500c195119SAlex Deucher  *
8510c195119SAlex Deucher  * @info: atom card_info pointer
8520c195119SAlex Deucher  * @reg: PLL register offset
8530c195119SAlex Deucher  * @val: value to write to the pll register
8540c195119SAlex Deucher  *
8550c195119SAlex Deucher  * Provides a PLL register accessor for the atom interpreter (r4xx+).
8560c195119SAlex Deucher  */
857771fe6b9SJerome Glisse static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
858771fe6b9SJerome Glisse {
859771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
860771fe6b9SJerome Glisse 
861771fe6b9SJerome Glisse 	rdev->pll_wreg(rdev, reg, val);
862771fe6b9SJerome Glisse }
863771fe6b9SJerome Glisse 
8640c195119SAlex Deucher /**
8650c195119SAlex Deucher  * cail_mc_read - read MC (Memory Controller) register
8660c195119SAlex Deucher  *
8670c195119SAlex Deucher  * @info: atom card_info pointer
8680c195119SAlex Deucher  * @reg: MC register offset
8690c195119SAlex Deucher  *
8700c195119SAlex Deucher  * Provides an MC register accessor for the atom interpreter (r4xx+).
8710c195119SAlex Deucher  * Returns the value of the MC register.
8720c195119SAlex Deucher  */
873771fe6b9SJerome Glisse static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
874771fe6b9SJerome Glisse {
875771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
876771fe6b9SJerome Glisse 	uint32_t r;
877771fe6b9SJerome Glisse 
878771fe6b9SJerome Glisse 	r = rdev->mc_rreg(rdev, reg);
879771fe6b9SJerome Glisse 	return r;
880771fe6b9SJerome Glisse }
881771fe6b9SJerome Glisse 
8820c195119SAlex Deucher /**
8830c195119SAlex Deucher  * cail_mc_write - write MC (Memory Controller) register
8840c195119SAlex Deucher  *
8850c195119SAlex Deucher  * @info: atom card_info pointer
8860c195119SAlex Deucher  * @reg: MC register offset
8870c195119SAlex Deucher  * @val: value to write to the pll register
8880c195119SAlex Deucher  *
8890c195119SAlex Deucher  * Provides a MC register accessor for the atom interpreter (r4xx+).
8900c195119SAlex Deucher  */
891771fe6b9SJerome Glisse static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
892771fe6b9SJerome Glisse {
893771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
894771fe6b9SJerome Glisse 
895771fe6b9SJerome Glisse 	rdev->mc_wreg(rdev, reg, val);
896771fe6b9SJerome Glisse }
897771fe6b9SJerome Glisse 
8980c195119SAlex Deucher /**
8990c195119SAlex Deucher  * cail_reg_write - write MMIO register
9000c195119SAlex Deucher  *
9010c195119SAlex Deucher  * @info: atom card_info pointer
9020c195119SAlex Deucher  * @reg: MMIO register offset
9030c195119SAlex Deucher  * @val: value to write to the pll register
9040c195119SAlex Deucher  *
9050c195119SAlex Deucher  * Provides a MMIO register accessor for the atom interpreter (r4xx+).
9060c195119SAlex Deucher  */
907771fe6b9SJerome Glisse static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
908771fe6b9SJerome Glisse {
909771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
910771fe6b9SJerome Glisse 
911771fe6b9SJerome Glisse 	WREG32(reg*4, val);
912771fe6b9SJerome Glisse }
913771fe6b9SJerome Glisse 
9140c195119SAlex Deucher /**
9150c195119SAlex Deucher  * cail_reg_read - read MMIO register
9160c195119SAlex Deucher  *
9170c195119SAlex Deucher  * @info: atom card_info pointer
9180c195119SAlex Deucher  * @reg: MMIO register offset
9190c195119SAlex Deucher  *
9200c195119SAlex Deucher  * Provides an MMIO register accessor for the atom interpreter (r4xx+).
9210c195119SAlex Deucher  * Returns the value of the MMIO register.
9220c195119SAlex Deucher  */
923771fe6b9SJerome Glisse static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
924771fe6b9SJerome Glisse {
925771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
926771fe6b9SJerome Glisse 	uint32_t r;
927771fe6b9SJerome Glisse 
928771fe6b9SJerome Glisse 	r = RREG32(reg*4);
929771fe6b9SJerome Glisse 	return r;
930771fe6b9SJerome Glisse }
931771fe6b9SJerome Glisse 
9320c195119SAlex Deucher /**
9330c195119SAlex Deucher  * cail_ioreg_write - write IO register
9340c195119SAlex Deucher  *
9350c195119SAlex Deucher  * @info: atom card_info pointer
9360c195119SAlex Deucher  * @reg: IO register offset
9370c195119SAlex Deucher  * @val: value to write to the pll register
9380c195119SAlex Deucher  *
9390c195119SAlex Deucher  * Provides a IO register accessor for the atom interpreter (r4xx+).
9400c195119SAlex Deucher  */
941351a52a2SAlex Deucher static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
942351a52a2SAlex Deucher {
943351a52a2SAlex Deucher 	struct radeon_device *rdev = info->dev->dev_private;
944351a52a2SAlex Deucher 
945351a52a2SAlex Deucher 	WREG32_IO(reg*4, val);
946351a52a2SAlex Deucher }
947351a52a2SAlex Deucher 
9480c195119SAlex Deucher /**
9490c195119SAlex Deucher  * cail_ioreg_read - read IO register
9500c195119SAlex Deucher  *
9510c195119SAlex Deucher  * @info: atom card_info pointer
9520c195119SAlex Deucher  * @reg: IO register offset
9530c195119SAlex Deucher  *
9540c195119SAlex Deucher  * Provides an IO register accessor for the atom interpreter (r4xx+).
9550c195119SAlex Deucher  * Returns the value of the IO register.
9560c195119SAlex Deucher  */
957351a52a2SAlex Deucher static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
958351a52a2SAlex Deucher {
959351a52a2SAlex Deucher 	struct radeon_device *rdev = info->dev->dev_private;
960351a52a2SAlex Deucher 	uint32_t r;
961351a52a2SAlex Deucher 
962351a52a2SAlex Deucher 	r = RREG32_IO(reg*4);
963351a52a2SAlex Deucher 	return r;
964351a52a2SAlex Deucher }
965351a52a2SAlex Deucher 
9660c195119SAlex Deucher /**
9670c195119SAlex Deucher  * radeon_atombios_init - init the driver info and callbacks for atombios
9680c195119SAlex Deucher  *
9690c195119SAlex Deucher  * @rdev: radeon_device pointer
9700c195119SAlex Deucher  *
9710c195119SAlex Deucher  * Initializes the driver info and register access callbacks for the
9720c195119SAlex Deucher  * ATOM interpreter (r4xx+).
9730c195119SAlex Deucher  * Returns 0 on sucess, -ENOMEM on failure.
9740c195119SAlex Deucher  * Called at driver startup.
9750c195119SAlex Deucher  */
976771fe6b9SJerome Glisse int radeon_atombios_init(struct radeon_device *rdev)
977771fe6b9SJerome Glisse {
97861c4b24bSMathias Fröhlich 	struct card_info *atom_card_info =
97961c4b24bSMathias Fröhlich 	    kzalloc(sizeof(struct card_info), GFP_KERNEL);
98061c4b24bSMathias Fröhlich 
98161c4b24bSMathias Fröhlich 	if (!atom_card_info)
98261c4b24bSMathias Fröhlich 		return -ENOMEM;
98361c4b24bSMathias Fröhlich 
98461c4b24bSMathias Fröhlich 	rdev->mode_info.atom_card_info = atom_card_info;
98561c4b24bSMathias Fröhlich 	atom_card_info->dev = rdev->ddev;
98661c4b24bSMathias Fröhlich 	atom_card_info->reg_read = cail_reg_read;
98761c4b24bSMathias Fröhlich 	atom_card_info->reg_write = cail_reg_write;
988351a52a2SAlex Deucher 	/* needed for iio ops */
989351a52a2SAlex Deucher 	if (rdev->rio_mem) {
990351a52a2SAlex Deucher 		atom_card_info->ioreg_read = cail_ioreg_read;
991351a52a2SAlex Deucher 		atom_card_info->ioreg_write = cail_ioreg_write;
992351a52a2SAlex Deucher 	} else {
993351a52a2SAlex Deucher 		DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
994351a52a2SAlex Deucher 		atom_card_info->ioreg_read = cail_reg_read;
995351a52a2SAlex Deucher 		atom_card_info->ioreg_write = cail_reg_write;
996351a52a2SAlex Deucher 	}
99761c4b24bSMathias Fröhlich 	atom_card_info->mc_read = cail_mc_read;
99861c4b24bSMathias Fröhlich 	atom_card_info->mc_write = cail_mc_write;
99961c4b24bSMathias Fröhlich 	atom_card_info->pll_read = cail_pll_read;
100061c4b24bSMathias Fröhlich 	atom_card_info->pll_write = cail_pll_write;
100161c4b24bSMathias Fröhlich 
100261c4b24bSMathias Fröhlich 	rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
10030e34d094STim Gardner 	if (!rdev->mode_info.atom_context) {
10040e34d094STim Gardner 		radeon_atombios_fini(rdev);
10050e34d094STim Gardner 		return -ENOMEM;
10060e34d094STim Gardner 	}
10070e34d094STim Gardner 
1008c31ad97fSRafał Miłecki 	mutex_init(&rdev->mode_info.atom_context->mutex);
10091c949842SDave Airlie 	mutex_init(&rdev->mode_info.atom_context->scratch_mutex);
1010771fe6b9SJerome Glisse 	radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
1011d904ef9bSDave Airlie 	atom_allocate_fb_scratch(rdev->mode_info.atom_context);
1012771fe6b9SJerome Glisse 	return 0;
1013771fe6b9SJerome Glisse }
1014771fe6b9SJerome Glisse 
10150c195119SAlex Deucher /**
10160c195119SAlex Deucher  * radeon_atombios_fini - free the driver info and callbacks for atombios
10170c195119SAlex Deucher  *
10180c195119SAlex Deucher  * @rdev: radeon_device pointer
10190c195119SAlex Deucher  *
10200c195119SAlex Deucher  * Frees the driver info and register access callbacks for the ATOM
10210c195119SAlex Deucher  * interpreter (r4xx+).
10220c195119SAlex Deucher  * Called at driver shutdown.
10230c195119SAlex Deucher  */
1024771fe6b9SJerome Glisse void radeon_atombios_fini(struct radeon_device *rdev)
1025771fe6b9SJerome Glisse {
10264a04a844SJerome Glisse 	if (rdev->mode_info.atom_context) {
1027d904ef9bSDave Airlie 		kfree(rdev->mode_info.atom_context->scratch);
10284a04a844SJerome Glisse 	}
10290e34d094STim Gardner 	kfree(rdev->mode_info.atom_context);
10300e34d094STim Gardner 	rdev->mode_info.atom_context = NULL;
103161c4b24bSMathias Fröhlich 	kfree(rdev->mode_info.atom_card_info);
10320e34d094STim Gardner 	rdev->mode_info.atom_card_info = NULL;
1033771fe6b9SJerome Glisse }
1034771fe6b9SJerome Glisse 
10350c195119SAlex Deucher /* COMBIOS */
10360c195119SAlex Deucher /*
10370c195119SAlex Deucher  * COMBIOS is the bios format prior to ATOM. It provides
10380c195119SAlex Deucher  * command tables similar to ATOM, but doesn't have a unified
10390c195119SAlex Deucher  * parser.  See radeon_combios.c
10400c195119SAlex Deucher  */
10410c195119SAlex Deucher 
10420c195119SAlex Deucher /**
10430c195119SAlex Deucher  * radeon_combios_init - init the driver info for combios
10440c195119SAlex Deucher  *
10450c195119SAlex Deucher  * @rdev: radeon_device pointer
10460c195119SAlex Deucher  *
10470c195119SAlex Deucher  * Initializes the driver info for combios (r1xx-r3xx).
10480c195119SAlex Deucher  * Returns 0 on sucess.
10490c195119SAlex Deucher  * Called at driver startup.
10500c195119SAlex Deucher  */
1051771fe6b9SJerome Glisse int radeon_combios_init(struct radeon_device *rdev)
1052771fe6b9SJerome Glisse {
1053771fe6b9SJerome Glisse 	radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
1054771fe6b9SJerome Glisse 	return 0;
1055771fe6b9SJerome Glisse }
1056771fe6b9SJerome Glisse 
10570c195119SAlex Deucher /**
10580c195119SAlex Deucher  * radeon_combios_fini - free the driver info for combios
10590c195119SAlex Deucher  *
10600c195119SAlex Deucher  * @rdev: radeon_device pointer
10610c195119SAlex Deucher  *
10620c195119SAlex Deucher  * Frees the driver info for combios (r1xx-r3xx).
10630c195119SAlex Deucher  * Called at driver shutdown.
10640c195119SAlex Deucher  */
1065771fe6b9SJerome Glisse void radeon_combios_fini(struct radeon_device *rdev)
1066771fe6b9SJerome Glisse {
1067771fe6b9SJerome Glisse }
1068771fe6b9SJerome Glisse 
10690c195119SAlex Deucher /* if we get transitioned to only one device, take VGA back */
10700c195119SAlex Deucher /**
10710c195119SAlex Deucher  * radeon_vga_set_decode - enable/disable vga decode
10720c195119SAlex Deucher  *
10730c195119SAlex Deucher  * @cookie: radeon_device pointer
10740c195119SAlex Deucher  * @state: enable/disable vga decode
10750c195119SAlex Deucher  *
10760c195119SAlex Deucher  * Enable/disable vga decode (all asics).
10770c195119SAlex Deucher  * Returns VGA resource flags.
10780c195119SAlex Deucher  */
107928d52043SDave Airlie static unsigned int radeon_vga_set_decode(void *cookie, bool state)
108028d52043SDave Airlie {
108128d52043SDave Airlie 	struct radeon_device *rdev = cookie;
108228d52043SDave Airlie 	radeon_vga_set_state(rdev, state);
108328d52043SDave Airlie 	if (state)
108428d52043SDave Airlie 		return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
108528d52043SDave Airlie 		       VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
108628d52043SDave Airlie 	else
108728d52043SDave Airlie 		return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
108828d52043SDave Airlie }
1089c1176d6fSDave Airlie 
10900c195119SAlex Deucher /**
10911bcb04f7SChristian König  * radeon_check_pot_argument - check that argument is a power of two
10921bcb04f7SChristian König  *
10931bcb04f7SChristian König  * @arg: value to check
10941bcb04f7SChristian König  *
10951bcb04f7SChristian König  * Validates that a certain argument is a power of two (all asics).
10961bcb04f7SChristian König  * Returns true if argument is valid.
10971bcb04f7SChristian König  */
10981bcb04f7SChristian König static bool radeon_check_pot_argument(int arg)
10991bcb04f7SChristian König {
11001bcb04f7SChristian König 	return (arg & (arg - 1)) == 0;
11011bcb04f7SChristian König }
11021bcb04f7SChristian König 
11031bcb04f7SChristian König /**
11045e3c4f90SGrigori Goronzy  * Determine a sensible default GART size according to ASIC family.
11055e3c4f90SGrigori Goronzy  *
11065e3c4f90SGrigori Goronzy  * @family ASIC family name
11075e3c4f90SGrigori Goronzy  */
11085e3c4f90SGrigori Goronzy static int radeon_gart_size_auto(enum radeon_family family)
11095e3c4f90SGrigori Goronzy {
11105e3c4f90SGrigori Goronzy 	/* default to a larger gart size on newer asics */
11115e3c4f90SGrigori Goronzy 	if (family >= CHIP_TAHITI)
11125e3c4f90SGrigori Goronzy 		return 2048;
11135e3c4f90SGrigori Goronzy 	else if (family >= CHIP_RV770)
11145e3c4f90SGrigori Goronzy 		return 1024;
11155e3c4f90SGrigori Goronzy 	else
11165e3c4f90SGrigori Goronzy 		return 512;
11175e3c4f90SGrigori Goronzy }
11185e3c4f90SGrigori Goronzy 
11195e3c4f90SGrigori Goronzy /**
11200c195119SAlex Deucher  * radeon_check_arguments - validate module params
11210c195119SAlex Deucher  *
11220c195119SAlex Deucher  * @rdev: radeon_device pointer
11230c195119SAlex Deucher  *
11240c195119SAlex Deucher  * Validates certain module parameters and updates
11250c195119SAlex Deucher  * the associated values used by the driver (all asics).
11260c195119SAlex Deucher  */
11271109ca09SLauri Kasanen static void radeon_check_arguments(struct radeon_device *rdev)
112836421338SJerome Glisse {
112936421338SJerome Glisse 	/* vramlimit must be a power of two */
11301bcb04f7SChristian König 	if (!radeon_check_pot_argument(radeon_vram_limit)) {
113136421338SJerome Glisse 		dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n",
113236421338SJerome Glisse 				radeon_vram_limit);
113336421338SJerome Glisse 		radeon_vram_limit = 0;
113436421338SJerome Glisse 	}
11351bcb04f7SChristian König 
1136edcd26e8SAlex Deucher 	if (radeon_gart_size == -1) {
11375e3c4f90SGrigori Goronzy 		radeon_gart_size = radeon_gart_size_auto(rdev->family);
1138edcd26e8SAlex Deucher 	}
113936421338SJerome Glisse 	/* gtt size must be power of two and greater or equal to 32M */
11401bcb04f7SChristian König 	if (radeon_gart_size < 32) {
1141edcd26e8SAlex Deucher 		dev_warn(rdev->dev, "gart size (%d) too small\n",
114236421338SJerome Glisse 				radeon_gart_size);
11435e3c4f90SGrigori Goronzy 		radeon_gart_size = radeon_gart_size_auto(rdev->family);
11441bcb04f7SChristian König 	} else if (!radeon_check_pot_argument(radeon_gart_size)) {
114536421338SJerome Glisse 		dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
114636421338SJerome Glisse 				radeon_gart_size);
11475e3c4f90SGrigori Goronzy 		radeon_gart_size = radeon_gart_size_auto(rdev->family);
114836421338SJerome Glisse 	}
11491bcb04f7SChristian König 	rdev->mc.gtt_size = (uint64_t)radeon_gart_size << 20;
11501bcb04f7SChristian König 
115136421338SJerome Glisse 	/* AGP mode can only be -1, 1, 2, 4, 8 */
115236421338SJerome Glisse 	switch (radeon_agpmode) {
115336421338SJerome Glisse 	case -1:
115436421338SJerome Glisse 	case 0:
115536421338SJerome Glisse 	case 1:
115636421338SJerome Glisse 	case 2:
115736421338SJerome Glisse 	case 4:
115836421338SJerome Glisse 	case 8:
115936421338SJerome Glisse 		break;
116036421338SJerome Glisse 	default:
116136421338SJerome Glisse 		dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
116236421338SJerome Glisse 				"-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
116336421338SJerome Glisse 		radeon_agpmode = 0;
116436421338SJerome Glisse 		break;
116536421338SJerome Glisse 	}
1166c1c44132SChristian König 
1167c1c44132SChristian König 	if (!radeon_check_pot_argument(radeon_vm_size)) {
1168c1c44132SChristian König 		dev_warn(rdev->dev, "VM size (%d) must be a power of 2\n",
1169c1c44132SChristian König 			 radeon_vm_size);
117020b2656dSChristian König 		radeon_vm_size = 4;
1171c1c44132SChristian König 	}
1172c1c44132SChristian König 
117320b2656dSChristian König 	if (radeon_vm_size < 1) {
117413c240efSAlexandre Demers 		dev_warn(rdev->dev, "VM size (%d) too small, min is 1GB\n",
1175c1c44132SChristian König 			 radeon_vm_size);
117620b2656dSChristian König 		radeon_vm_size = 4;
1177c1c44132SChristian König 	}
1178c1c44132SChristian König 
1179c1c44132SChristian König 	/*
1180c1c44132SChristian König 	 * Max GPUVM size for Cayman, SI and CI are 40 bits.
1181c1c44132SChristian König 	 */
118220b2656dSChristian König 	if (radeon_vm_size > 1024) {
118320b2656dSChristian König 		dev_warn(rdev->dev, "VM size (%d) too large, max is 1TB\n",
1184c1c44132SChristian König 			 radeon_vm_size);
118520b2656dSChristian König 		radeon_vm_size = 4;
1186c1c44132SChristian König 	}
11874510fb98SChristian König 
11884510fb98SChristian König 	/* defines number of bits in page table versus page directory,
11894510fb98SChristian König 	 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
11904510fb98SChristian König 	 * page table and the remaining bits are in the page directory */
1191dfc230f9SChristian König 	if (radeon_vm_block_size == -1) {
1192dfc230f9SChristian König 
1193dfc230f9SChristian König 		/* Total bits covered by PD + PTs */
11948e66e134SAlex Deucher 		unsigned bits = ilog2(radeon_vm_size) + 18;
1195dfc230f9SChristian König 
1196dfc230f9SChristian König 		/* Make sure the PD is 4K in size up to 8GB address space.
1197dfc230f9SChristian König 		   Above that split equal between PD and PTs */
1198dfc230f9SChristian König 		if (radeon_vm_size <= 8)
1199dfc230f9SChristian König 			radeon_vm_block_size = bits - 9;
1200dfc230f9SChristian König 		else
1201dfc230f9SChristian König 			radeon_vm_block_size = (bits + 3) / 2;
1202dfc230f9SChristian König 
1203dfc230f9SChristian König 	} else if (radeon_vm_block_size < 9) {
120420b2656dSChristian König 		dev_warn(rdev->dev, "VM page table size (%d) too small\n",
12054510fb98SChristian König 			 radeon_vm_block_size);
12064510fb98SChristian König 		radeon_vm_block_size = 9;
12074510fb98SChristian König 	}
12084510fb98SChristian König 
12094510fb98SChristian König 	if (radeon_vm_block_size > 24 ||
121020b2656dSChristian König 	    (radeon_vm_size * 1024) < (1ull << radeon_vm_block_size)) {
121120b2656dSChristian König 		dev_warn(rdev->dev, "VM page table size (%d) too large\n",
12124510fb98SChristian König 			 radeon_vm_block_size);
12134510fb98SChristian König 		radeon_vm_block_size = 9;
12144510fb98SChristian König 	}
121536421338SJerome Glisse }
121636421338SJerome Glisse 
12170c195119SAlex Deucher /**
12180c195119SAlex Deucher  * radeon_switcheroo_set_state - set switcheroo state
12190c195119SAlex Deucher  *
12200c195119SAlex Deucher  * @pdev: pci dev pointer
12218e5de1d8SLukas Wunner  * @state: vga_switcheroo state
12220c195119SAlex Deucher  *
12230c195119SAlex Deucher  * Callback for the switcheroo driver.  Suspends or resumes the
12240c195119SAlex Deucher  * the asics before or after it is powered up using ACPI methods.
12250c195119SAlex Deucher  */
12266a9ee8afSDave Airlie static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
12276a9ee8afSDave Airlie {
12286a9ee8afSDave Airlie 	struct drm_device *dev = pci_get_drvdata(pdev);
12294807c5a8SAlex Deucher 	struct radeon_device *rdev = dev->dev_private;
123010ebc0bcSDave Airlie 
123190c4cde9SAlex Deucher 	if (radeon_is_px(dev) && state == VGA_SWITCHEROO_OFF)
123210ebc0bcSDave Airlie 		return;
123310ebc0bcSDave Airlie 
12346a9ee8afSDave Airlie 	if (state == VGA_SWITCHEROO_ON) {
1235d1f9809eSMaarten Lankhorst 		unsigned d3_delay = dev->pdev->d3_delay;
1236d1f9809eSMaarten Lankhorst 
12376a9ee8afSDave Airlie 		printk(KERN_INFO "radeon: switched on\n");
12386a9ee8afSDave Airlie 		/* don't suspend or resume card normally */
12395bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1240d1f9809eSMaarten Lankhorst 
12414807c5a8SAlex Deucher 		if (d3_delay < 20 && (rdev->px_quirk_flags & RADEON_PX_QUIRK_LONG_WAKEUP))
1242d1f9809eSMaarten Lankhorst 			dev->pdev->d3_delay = 20;
1243d1f9809eSMaarten Lankhorst 
124410ebc0bcSDave Airlie 		radeon_resume_kms(dev, true, true);
1245d1f9809eSMaarten Lankhorst 
1246d1f9809eSMaarten Lankhorst 		dev->pdev->d3_delay = d3_delay;
1247d1f9809eSMaarten Lankhorst 
12485bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_ON;
1249fbf81762SDave Airlie 		drm_kms_helper_poll_enable(dev);
12506a9ee8afSDave Airlie 	} else {
12516a9ee8afSDave Airlie 		printk(KERN_INFO "radeon: switched off\n");
1252fbf81762SDave Airlie 		drm_kms_helper_poll_disable(dev);
12535bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1254274ad65cSJérome Glisse 		radeon_suspend_kms(dev, true, true, false);
12555bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_OFF;
12566a9ee8afSDave Airlie 	}
12576a9ee8afSDave Airlie }
12586a9ee8afSDave Airlie 
12590c195119SAlex Deucher /**
12600c195119SAlex Deucher  * radeon_switcheroo_can_switch - see if switcheroo state can change
12610c195119SAlex Deucher  *
12620c195119SAlex Deucher  * @pdev: pci dev pointer
12630c195119SAlex Deucher  *
12640c195119SAlex Deucher  * Callback for the switcheroo driver.  Check of the switcheroo
12650c195119SAlex Deucher  * state can be changed.
12660c195119SAlex Deucher  * Returns true if the state can be changed, false if not.
12670c195119SAlex Deucher  */
12686a9ee8afSDave Airlie static bool radeon_switcheroo_can_switch(struct pci_dev *pdev)
12696a9ee8afSDave Airlie {
12706a9ee8afSDave Airlie 	struct drm_device *dev = pci_get_drvdata(pdev);
12716a9ee8afSDave Airlie 
1272fc8fd40eSDaniel Vetter 	/*
1273fc8fd40eSDaniel Vetter 	 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1274fc8fd40eSDaniel Vetter 	 * locking inversion with the driver load path. And the access here is
1275fc8fd40eSDaniel Vetter 	 * completely racy anyway. So don't bother with locking for now.
1276fc8fd40eSDaniel Vetter 	 */
1277fc8fd40eSDaniel Vetter 	return dev->open_count == 0;
12786a9ee8afSDave Airlie }
12796a9ee8afSDave Airlie 
128026ec685fSTakashi Iwai static const struct vga_switcheroo_client_ops radeon_switcheroo_ops = {
128126ec685fSTakashi Iwai 	.set_gpu_state = radeon_switcheroo_set_state,
128226ec685fSTakashi Iwai 	.reprobe = NULL,
128326ec685fSTakashi Iwai 	.can_switch = radeon_switcheroo_can_switch,
128426ec685fSTakashi Iwai };
12856a9ee8afSDave Airlie 
12860c195119SAlex Deucher /**
12870c195119SAlex Deucher  * radeon_device_init - initialize the driver
12880c195119SAlex Deucher  *
12890c195119SAlex Deucher  * @rdev: radeon_device pointer
12900c195119SAlex Deucher  * @pdev: drm dev pointer
12910c195119SAlex Deucher  * @pdev: pci dev pointer
12920c195119SAlex Deucher  * @flags: driver flags
12930c195119SAlex Deucher  *
12940c195119SAlex Deucher  * Initializes the driver info and hw (all asics).
12950c195119SAlex Deucher  * Returns 0 for success or an error on failure.
12960c195119SAlex Deucher  * Called at driver startup.
12970c195119SAlex Deucher  */
1298771fe6b9SJerome Glisse int radeon_device_init(struct radeon_device *rdev,
1299771fe6b9SJerome Glisse 		       struct drm_device *ddev,
1300771fe6b9SJerome Glisse 		       struct pci_dev *pdev,
1301771fe6b9SJerome Glisse 		       uint32_t flags)
1302771fe6b9SJerome Glisse {
1303351a52a2SAlex Deucher 	int r, i;
1304ad49f501SDave Airlie 	int dma_bits;
130510ebc0bcSDave Airlie 	bool runtime = false;
1306771fe6b9SJerome Glisse 
1307771fe6b9SJerome Glisse 	rdev->shutdown = false;
13089f022ddfSJerome Glisse 	rdev->dev = &pdev->dev;
1309771fe6b9SJerome Glisse 	rdev->ddev = ddev;
1310771fe6b9SJerome Glisse 	rdev->pdev = pdev;
1311771fe6b9SJerome Glisse 	rdev->flags = flags;
1312771fe6b9SJerome Glisse 	rdev->family = flags & RADEON_FAMILY_MASK;
1313771fe6b9SJerome Glisse 	rdev->is_atom_bios = false;
1314771fe6b9SJerome Glisse 	rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
1315edcd26e8SAlex Deucher 	rdev->mc.gtt_size = 512 * 1024 * 1024;
1316733289c2SJerome Glisse 	rdev->accel_working = false;
13178b25ed34SAlex Deucher 	/* set up ring ids */
13188b25ed34SAlex Deucher 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
13198b25ed34SAlex Deucher 		rdev->ring[i].idx = i;
13208b25ed34SAlex Deucher 	}
1321954605caSMaarten Lankhorst 	rdev->fence_context = fence_context_alloc(RADEON_NUM_RINGS);
13221b5331d9SJerome Glisse 
1323fe0d36e0SAlex Deucher 	DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1324d522d9ccSThomas Reim 		 radeon_family_name[rdev->family], pdev->vendor, pdev->device,
1325fe0d36e0SAlex Deucher 		 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
13261b5331d9SJerome Glisse 
1327771fe6b9SJerome Glisse 	/* mutex initialization are all done here so we
1328771fe6b9SJerome Glisse 	 * can recall function without having locking issues */
1329d6999bc7SChristian König 	mutex_init(&rdev->ring_lock);
133040bacf16SAlex Deucher 	mutex_init(&rdev->dc_hw_i2c_mutex);
1331c20dc369SChristian Koenig 	atomic_set(&rdev->ih.lock, 0);
13324c788679SJerome Glisse 	mutex_init(&rdev->gem.mutex);
1333c913e23aSRafał Miłecki 	mutex_init(&rdev->pm.mutex);
13346759a0a7SMarek Olšák 	mutex_init(&rdev->gpu_clock_mutex);
1335f61d5b46SAlex Deucher 	mutex_init(&rdev->srbm_mutex);
13361c0a4625SOded Gabbay 	mutex_init(&rdev->grbm_idx_mutex);
1337db7fce39SChristian König 	init_rwsem(&rdev->pm.mclk_lock);
1338dee53e7fSJerome Glisse 	init_rwsem(&rdev->exclusive_lock);
133973a6d3fcSRafał Miłecki 	init_waitqueue_head(&rdev->irq.vblank_queue);
1340341cb9e4SChristian König 	mutex_init(&rdev->mn_lock);
1341341cb9e4SChristian König 	hash_init(rdev->mn_hash);
13421b9c3dd0SAlex Deucher 	r = radeon_gem_init(rdev);
13431b9c3dd0SAlex Deucher 	if (r)
13441b9c3dd0SAlex Deucher 		return r;
1345529364e0SChristian König 
1346c1c44132SChristian König 	radeon_check_arguments(rdev);
134723d4f1f2SAlex Deucher 	/* Adjust VM size here.
1348c1c44132SChristian König 	 * Max GPUVM size for cayman+ is 40 bits.
134923d4f1f2SAlex Deucher 	 */
135020b2656dSChristian König 	rdev->vm_manager.max_pfn = radeon_vm_size << 18;
1351771fe6b9SJerome Glisse 
13524aac0473SJerome Glisse 	/* Set asic functions */
13534aac0473SJerome Glisse 	r = radeon_asic_init(rdev);
135436421338SJerome Glisse 	if (r)
13554aac0473SJerome Glisse 		return r;
13564aac0473SJerome Glisse 
1357f95df9caSAlex Deucher 	/* all of the newer IGP chips have an internal gart
1358f95df9caSAlex Deucher 	 * However some rs4xx report as AGP, so remove that here.
1359f95df9caSAlex Deucher 	 */
1360f95df9caSAlex Deucher 	if ((rdev->family >= CHIP_RS400) &&
1361f95df9caSAlex Deucher 	    (rdev->flags & RADEON_IS_IGP)) {
1362f95df9caSAlex Deucher 		rdev->flags &= ~RADEON_IS_AGP;
1363f95df9caSAlex Deucher 	}
1364f95df9caSAlex Deucher 
136530256a3fSJerome Glisse 	if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
1366b574f251SJerome Glisse 		radeon_agp_disable(rdev);
1367771fe6b9SJerome Glisse 	}
1368771fe6b9SJerome Glisse 
13699ed8b1f9SAlex Deucher 	/* Set the internal MC address mask
13709ed8b1f9SAlex Deucher 	 * This is the max address of the GPU's
13719ed8b1f9SAlex Deucher 	 * internal address space.
13729ed8b1f9SAlex Deucher 	 */
13739ed8b1f9SAlex Deucher 	if (rdev->family >= CHIP_CAYMAN)
13749ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xffffffffffULL; /* 40 bit MC */
13759ed8b1f9SAlex Deucher 	else if (rdev->family >= CHIP_CEDAR)
13769ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xfffffffffULL; /* 36 bit MC */
13779ed8b1f9SAlex Deucher 	else
13789ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xffffffffULL; /* 32 bit MC */
13799ed8b1f9SAlex Deucher 
1380ad49f501SDave Airlie 	/* set DMA mask + need_dma32 flags.
1381ad49f501SDave Airlie 	 * PCIE - can handle 40-bits.
1382005a83f1SAlex Deucher 	 * IGP - can handle 40-bits
1383ad49f501SDave Airlie 	 * AGP - generally dma32 is safest
1384005a83f1SAlex Deucher 	 * PCI - dma32 for legacy pci gart, 40 bits on newer asics
1385ad49f501SDave Airlie 	 */
1386ad49f501SDave Airlie 	rdev->need_dma32 = false;
1387ad49f501SDave Airlie 	if (rdev->flags & RADEON_IS_AGP)
1388ad49f501SDave Airlie 		rdev->need_dma32 = true;
1389005a83f1SAlex Deucher 	if ((rdev->flags & RADEON_IS_PCI) &&
13904a2b6662SJerome Glisse 	    (rdev->family <= CHIP_RS740))
1391ad49f501SDave Airlie 		rdev->need_dma32 = true;
1392ad49f501SDave Airlie 
1393ad49f501SDave Airlie 	dma_bits = rdev->need_dma32 ? 32 : 40;
1394ad49f501SDave Airlie 	r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
1395771fe6b9SJerome Glisse 	if (r) {
139662fff811SDaniel Haid 		rdev->need_dma32 = true;
1397c52494f6SKonrad Rzeszutek Wilk 		dma_bits = 32;
1398771fe6b9SJerome Glisse 		printk(KERN_WARNING "radeon: No suitable DMA available.\n");
1399771fe6b9SJerome Glisse 	}
1400c52494f6SKonrad Rzeszutek Wilk 	r = pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
1401c52494f6SKonrad Rzeszutek Wilk 	if (r) {
1402c52494f6SKonrad Rzeszutek Wilk 		pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(32));
1403c52494f6SKonrad Rzeszutek Wilk 		printk(KERN_WARNING "radeon: No coherent DMA available.\n");
1404c52494f6SKonrad Rzeszutek Wilk 	}
1405771fe6b9SJerome Glisse 
1406771fe6b9SJerome Glisse 	/* Registers mapping */
1407771fe6b9SJerome Glisse 	/* TODO: block userspace mapping of io register */
14082c385151SDaniel Vetter 	spin_lock_init(&rdev->mmio_idx_lock);
1409fe78118cSAlex Deucher 	spin_lock_init(&rdev->smc_idx_lock);
14100a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pll_idx_lock);
14110a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->mc_idx_lock);
14120a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pcie_idx_lock);
14130a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pciep_idx_lock);
14140a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pif_idx_lock);
14150a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->cg_idx_lock);
14160a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->uvd_idx_lock);
14170a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->rcu_idx_lock);
14180a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->didt_idx_lock);
14190a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->end_idx_lock);
1420efad86dbSAlex Deucher 	if (rdev->family >= CHIP_BONAIRE) {
1421efad86dbSAlex Deucher 		rdev->rmmio_base = pci_resource_start(rdev->pdev, 5);
1422efad86dbSAlex Deucher 		rdev->rmmio_size = pci_resource_len(rdev->pdev, 5);
1423efad86dbSAlex Deucher 	} else {
142401d73a69SJordan Crouse 		rdev->rmmio_base = pci_resource_start(rdev->pdev, 2);
142501d73a69SJordan Crouse 		rdev->rmmio_size = pci_resource_len(rdev->pdev, 2);
1426efad86dbSAlex Deucher 	}
1427771fe6b9SJerome Glisse 	rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size);
1428771fe6b9SJerome Glisse 	if (rdev->rmmio == NULL) {
1429771fe6b9SJerome Glisse 		return -ENOMEM;
1430771fe6b9SJerome Glisse 	}
1431771fe6b9SJerome Glisse 	DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base);
1432771fe6b9SJerome Glisse 	DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size);
1433771fe6b9SJerome Glisse 
143475efdee1SAlex Deucher 	/* doorbell bar mapping */
143575efdee1SAlex Deucher 	if (rdev->family >= CHIP_BONAIRE)
143675efdee1SAlex Deucher 		radeon_doorbell_init(rdev);
143775efdee1SAlex Deucher 
1438351a52a2SAlex Deucher 	/* io port mapping */
1439351a52a2SAlex Deucher 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1440351a52a2SAlex Deucher 		if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) {
1441351a52a2SAlex Deucher 			rdev->rio_mem_size = pci_resource_len(rdev->pdev, i);
1442351a52a2SAlex Deucher 			rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size);
1443351a52a2SAlex Deucher 			break;
1444351a52a2SAlex Deucher 		}
1445351a52a2SAlex Deucher 	}
1446351a52a2SAlex Deucher 	if (rdev->rio_mem == NULL)
1447351a52a2SAlex Deucher 		DRM_ERROR("Unable to find PCI I/O BAR\n");
1448351a52a2SAlex Deucher 
14494807c5a8SAlex Deucher 	if (rdev->flags & RADEON_IS_PX)
14504807c5a8SAlex Deucher 		radeon_device_handle_px_quirks(rdev);
14514807c5a8SAlex Deucher 
145228d52043SDave Airlie 	/* if we have > 1 VGA cards, then disable the radeon VGA resources */
145393239ea1SDave Airlie 	/* this will fail for cards that aren't VGA class devices, just
145493239ea1SDave Airlie 	 * ignore it */
145593239ea1SDave Airlie 	vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
145610ebc0bcSDave Airlie 
1457bfaddd9fSAlex Deucher 	if (rdev->flags & RADEON_IS_PX)
145810ebc0bcSDave Airlie 		runtime = true;
145910ebc0bcSDave Airlie 	vga_switcheroo_register_client(rdev->pdev, &radeon_switcheroo_ops, runtime);
146010ebc0bcSDave Airlie 	if (runtime)
146110ebc0bcSDave Airlie 		vga_switcheroo_init_domain_pm_ops(rdev->dev, &rdev->vga_pm_domain);
146228d52043SDave Airlie 
14633ce0a23dSJerome Glisse 	r = radeon_init(rdev);
1464b574f251SJerome Glisse 	if (r)
14652e97140dSAlex Deucher 		goto failed;
1466b1e3a6d1SMichel Dänzer 
1467409851f4SJerome Glisse 	r = radeon_gem_debugfs_init(rdev);
1468409851f4SJerome Glisse 	if (r) {
1469409851f4SJerome Glisse 		DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1470409851f4SJerome Glisse 	}
1471409851f4SJerome Glisse 
14729843ead0SDave Airlie 	r = radeon_mst_debugfs_init(rdev);
14739843ead0SDave Airlie 	if (r) {
14749843ead0SDave Airlie 		DRM_ERROR("registering mst debugfs failed (%d).\n", r);
14759843ead0SDave Airlie 	}
14769843ead0SDave Airlie 
1477b574f251SJerome Glisse 	if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
1478b574f251SJerome Glisse 		/* Acceleration not working on AGP card try again
1479b574f251SJerome Glisse 		 * with fallback to PCI or PCIE GART
1480b574f251SJerome Glisse 		 */
1481a2d07b74SJerome Glisse 		radeon_asic_reset(rdev);
1482b574f251SJerome Glisse 		radeon_fini(rdev);
1483b574f251SJerome Glisse 		radeon_agp_disable(rdev);
1484b574f251SJerome Glisse 		r = radeon_init(rdev);
14854aac0473SJerome Glisse 		if (r)
14862e97140dSAlex Deucher 			goto failed;
14873ce0a23dSJerome Glisse 	}
14886c7bcceaSAlex Deucher 
148913a7d299SChristian König 	r = radeon_ib_ring_tests(rdev);
149013a7d299SChristian König 	if (r)
149113a7d299SChristian König 		DRM_ERROR("ib ring test failed (%d).\n", r);
149213a7d299SChristian König 
14936dfd1972SJérôme Glisse 	/*
14946dfd1972SJérôme Glisse 	 * Turks/Thames GPU will freeze whole laptop if DPM is not restarted
14956dfd1972SJérôme Glisse 	 * after the CP ring have chew one packet at least. Hence here we stop
14966dfd1972SJérôme Glisse 	 * and restart DPM after the radeon_ib_ring_tests().
14976dfd1972SJérôme Glisse 	 */
14986dfd1972SJérôme Glisse 	if (rdev->pm.dpm_enabled &&
14996dfd1972SJérôme Glisse 	    (rdev->pm.pm_method == PM_METHOD_DPM) &&
15006dfd1972SJérôme Glisse 	    (rdev->family == CHIP_TURKS) &&
15016dfd1972SJérôme Glisse 	    (rdev->flags & RADEON_IS_MOBILITY)) {
15026dfd1972SJérôme Glisse 		mutex_lock(&rdev->pm.mutex);
15036dfd1972SJérôme Glisse 		radeon_dpm_disable(rdev);
15046dfd1972SJérôme Glisse 		radeon_dpm_enable(rdev);
15056dfd1972SJérôme Glisse 		mutex_unlock(&rdev->pm.mutex);
15066dfd1972SJérôme Glisse 	}
15076dfd1972SJérôme Glisse 
150860a7e396SChristian König 	if ((radeon_testing & 1)) {
15094a1132a0SAlex Deucher 		if (rdev->accel_working)
1510ecc0b326SMichel Dänzer 			radeon_test_moves(rdev);
15114a1132a0SAlex Deucher 		else
15124a1132a0SAlex Deucher 			DRM_INFO("radeon: acceleration disabled, skipping move tests\n");
1513ecc0b326SMichel Dänzer 	}
151460a7e396SChristian König 	if ((radeon_testing & 2)) {
15154a1132a0SAlex Deucher 		if (rdev->accel_working)
151660a7e396SChristian König 			radeon_test_syncing(rdev);
15174a1132a0SAlex Deucher 		else
15184a1132a0SAlex Deucher 			DRM_INFO("radeon: acceleration disabled, skipping sync tests\n");
151960a7e396SChristian König 	}
1520771fe6b9SJerome Glisse 	if (radeon_benchmarking) {
15214a1132a0SAlex Deucher 		if (rdev->accel_working)
1522638dd7dbSIlija Hadzic 			radeon_benchmark(rdev, radeon_benchmarking);
15234a1132a0SAlex Deucher 		else
15244a1132a0SAlex Deucher 			DRM_INFO("radeon: acceleration disabled, skipping benchmarks\n");
1525771fe6b9SJerome Glisse 	}
15266cf8a3f5SJerome Glisse 	return 0;
15272e97140dSAlex Deucher 
15282e97140dSAlex Deucher failed:
15292e97140dSAlex Deucher 	if (runtime)
15302e97140dSAlex Deucher 		vga_switcheroo_fini_domain_pm_ops(rdev->dev);
15312e97140dSAlex Deucher 	return r;
1532771fe6b9SJerome Glisse }
1533771fe6b9SJerome Glisse 
15344d8bf9aeSChristian König static void radeon_debugfs_remove_files(struct radeon_device *rdev);
15354d8bf9aeSChristian König 
15360c195119SAlex Deucher /**
15370c195119SAlex Deucher  * radeon_device_fini - tear down the driver
15380c195119SAlex Deucher  *
15390c195119SAlex Deucher  * @rdev: radeon_device pointer
15400c195119SAlex Deucher  *
15410c195119SAlex Deucher  * Tear down the driver info (all asics).
15420c195119SAlex Deucher  * Called at driver shutdown.
15430c195119SAlex Deucher  */
1544771fe6b9SJerome Glisse void radeon_device_fini(struct radeon_device *rdev)
1545771fe6b9SJerome Glisse {
1546771fe6b9SJerome Glisse 	DRM_INFO("radeon: finishing device.\n");
1547771fe6b9SJerome Glisse 	rdev->shutdown = true;
154890aca4d2SJerome Glisse 	/* evict vram memory */
154990aca4d2SJerome Glisse 	radeon_bo_evict_vram(rdev);
15503ce0a23dSJerome Glisse 	radeon_fini(rdev);
15516a9ee8afSDave Airlie 	vga_switcheroo_unregister_client(rdev->pdev);
15522e97140dSAlex Deucher 	if (rdev->flags & RADEON_IS_PX)
15532e97140dSAlex Deucher 		vga_switcheroo_fini_domain_pm_ops(rdev->dev);
1554c1176d6fSDave Airlie 	vga_client_register(rdev->pdev, NULL, NULL, NULL);
1555e0a2ca73SAlex Deucher 	if (rdev->rio_mem)
1556351a52a2SAlex Deucher 		pci_iounmap(rdev->pdev, rdev->rio_mem);
1557351a52a2SAlex Deucher 	rdev->rio_mem = NULL;
1558771fe6b9SJerome Glisse 	iounmap(rdev->rmmio);
1559771fe6b9SJerome Glisse 	rdev->rmmio = NULL;
156075efdee1SAlex Deucher 	if (rdev->family >= CHIP_BONAIRE)
156175efdee1SAlex Deucher 		radeon_doorbell_fini(rdev);
15624d8bf9aeSChristian König 	radeon_debugfs_remove_files(rdev);
1563771fe6b9SJerome Glisse }
1564771fe6b9SJerome Glisse 
1565771fe6b9SJerome Glisse 
1566771fe6b9SJerome Glisse /*
1567771fe6b9SJerome Glisse  * Suspend & resume.
1568771fe6b9SJerome Glisse  */
15690c195119SAlex Deucher /**
15700c195119SAlex Deucher  * radeon_suspend_kms - initiate device suspend
15710c195119SAlex Deucher  *
15720c195119SAlex Deucher  * @pdev: drm dev pointer
15730c195119SAlex Deucher  * @state: suspend state
15740c195119SAlex Deucher  *
15750c195119SAlex Deucher  * Puts the hw in the suspend state (all asics).
15760c195119SAlex Deucher  * Returns 0 for success or an error on failure.
15770c195119SAlex Deucher  * Called at driver suspend.
15780c195119SAlex Deucher  */
1579274ad65cSJérome Glisse int radeon_suspend_kms(struct drm_device *dev, bool suspend,
1580274ad65cSJérome Glisse 		       bool fbcon, bool freeze)
1581771fe6b9SJerome Glisse {
1582875c1866SDarren Jenkins 	struct radeon_device *rdev;
1583771fe6b9SJerome Glisse 	struct drm_crtc *crtc;
1584d8dcaa1dSAlex Deucher 	struct drm_connector *connector;
15857465280cSAlex Deucher 	int i, r;
1586771fe6b9SJerome Glisse 
1587875c1866SDarren Jenkins 	if (dev == NULL || dev->dev_private == NULL) {
1588771fe6b9SJerome Glisse 		return -ENODEV;
1589771fe6b9SJerome Glisse 	}
15907473e830SDave Airlie 
1591875c1866SDarren Jenkins 	rdev = dev->dev_private;
1592875c1866SDarren Jenkins 
15935bcf719bSDave Airlie 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
15946a9ee8afSDave Airlie 		return 0;
1595d8dcaa1dSAlex Deucher 
159686698c20SSeth Forshee 	drm_kms_helper_poll_disable(dev);
159786698c20SSeth Forshee 
15986adaed5bSDaniel Vetter 	drm_modeset_lock_all(dev);
1599d8dcaa1dSAlex Deucher 	/* turn off display hw */
1600d8dcaa1dSAlex Deucher 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1601d8dcaa1dSAlex Deucher 		drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1602d8dcaa1dSAlex Deucher 	}
16036adaed5bSDaniel Vetter 	drm_modeset_unlock_all(dev);
1604d8dcaa1dSAlex Deucher 
1605f3cbb17bSGrigori Goronzy 	/* unpin the front buffers and cursors */
1606771fe6b9SJerome Glisse 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1607f3cbb17bSGrigori Goronzy 		struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1608f4510a27SMatt Roper 		struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->primary->fb);
16094c788679SJerome Glisse 		struct radeon_bo *robj;
1610771fe6b9SJerome Glisse 
1611f3cbb17bSGrigori Goronzy 		if (radeon_crtc->cursor_bo) {
1612f3cbb17bSGrigori Goronzy 			struct radeon_bo *robj = gem_to_radeon_bo(radeon_crtc->cursor_bo);
1613f3cbb17bSGrigori Goronzy 			r = radeon_bo_reserve(robj, false);
1614f3cbb17bSGrigori Goronzy 			if (r == 0) {
1615f3cbb17bSGrigori Goronzy 				radeon_bo_unpin(robj);
1616f3cbb17bSGrigori Goronzy 				radeon_bo_unreserve(robj);
1617f3cbb17bSGrigori Goronzy 			}
1618f3cbb17bSGrigori Goronzy 		}
1619f3cbb17bSGrigori Goronzy 
1620771fe6b9SJerome Glisse 		if (rfb == NULL || rfb->obj == NULL) {
1621771fe6b9SJerome Glisse 			continue;
1622771fe6b9SJerome Glisse 		}
16237e4d15d9SDaniel Vetter 		robj = gem_to_radeon_bo(rfb->obj);
162438651674SDave Airlie 		/* don't unpin kernel fb objects */
162538651674SDave Airlie 		if (!radeon_fbdev_robj_is_fb(rdev, robj)) {
16264c788679SJerome Glisse 			r = radeon_bo_reserve(robj, false);
162738651674SDave Airlie 			if (r == 0) {
16284c788679SJerome Glisse 				radeon_bo_unpin(robj);
16294c788679SJerome Glisse 				radeon_bo_unreserve(robj);
16304c788679SJerome Glisse 			}
1631771fe6b9SJerome Glisse 		}
1632771fe6b9SJerome Glisse 	}
1633771fe6b9SJerome Glisse 	/* evict vram memory */
16344c788679SJerome Glisse 	radeon_bo_evict_vram(rdev);
16358a47cc9eSChristian König 
1636771fe6b9SJerome Glisse 	/* wait for gpu to finish processing current batch */
16375f8f635eSJerome Glisse 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
163837615527SChristian König 		r = radeon_fence_wait_empty(rdev, i);
16395f8f635eSJerome Glisse 		if (r) {
16405f8f635eSJerome Glisse 			/* delay GPU reset to resume */
1641eb98c709SChristian König 			radeon_fence_driver_force_completion(rdev, i);
16425f8f635eSJerome Glisse 		}
16435f8f635eSJerome Glisse 	}
1644771fe6b9SJerome Glisse 
1645f657c2a7SYang Zhao 	radeon_save_bios_scratch_regs(rdev);
1646f657c2a7SYang Zhao 
16473ce0a23dSJerome Glisse 	radeon_suspend(rdev);
1648d4877cf2SAlex Deucher 	radeon_hpd_fini(rdev);
1649771fe6b9SJerome Glisse 	/* evict remaining vram memory */
16504c788679SJerome Glisse 	radeon_bo_evict_vram(rdev);
1651771fe6b9SJerome Glisse 
165210b06122SJerome Glisse 	radeon_agp_suspend(rdev);
165310b06122SJerome Glisse 
1654771fe6b9SJerome Glisse 	pci_save_state(dev->pdev);
1655ccaa2c12SJérôme Glisse 	if (freeze && rdev->family >= CHIP_CEDAR) {
1656274ad65cSJérome Glisse 		rdev->asic->asic_reset(rdev, true);
1657274ad65cSJérome Glisse 		pci_restore_state(dev->pdev);
1658274ad65cSJérome Glisse 	} else if (suspend) {
1659771fe6b9SJerome Glisse 		/* Shut down the device */
1660771fe6b9SJerome Glisse 		pci_disable_device(dev->pdev);
1661771fe6b9SJerome Glisse 		pci_set_power_state(dev->pdev, PCI_D3hot);
1662771fe6b9SJerome Glisse 	}
166310ebc0bcSDave Airlie 
166410ebc0bcSDave Airlie 	if (fbcon) {
1665ac751efaSTorben Hohn 		console_lock();
166638651674SDave Airlie 		radeon_fbdev_set_suspend(rdev, 1);
1667ac751efaSTorben Hohn 		console_unlock();
166810ebc0bcSDave Airlie 	}
1669771fe6b9SJerome Glisse 	return 0;
1670771fe6b9SJerome Glisse }
1671771fe6b9SJerome Glisse 
16720c195119SAlex Deucher /**
16730c195119SAlex Deucher  * radeon_resume_kms - initiate device resume
16740c195119SAlex Deucher  *
16750c195119SAlex Deucher  * @pdev: drm dev pointer
16760c195119SAlex Deucher  *
16770c195119SAlex Deucher  * Bring the hw back to operating state (all asics).
16780c195119SAlex Deucher  * Returns 0 for success or an error on failure.
16790c195119SAlex Deucher  * Called at driver resume.
16800c195119SAlex Deucher  */
168110ebc0bcSDave Airlie int radeon_resume_kms(struct drm_device *dev, bool resume, bool fbcon)
1682771fe6b9SJerome Glisse {
168309bdf591SCedric Godin 	struct drm_connector *connector;
1684771fe6b9SJerome Glisse 	struct radeon_device *rdev = dev->dev_private;
1685f3cbb17bSGrigori Goronzy 	struct drm_crtc *crtc;
168604eb2206SChristian König 	int r;
1687771fe6b9SJerome Glisse 
16885bcf719bSDave Airlie 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
16896a9ee8afSDave Airlie 		return 0;
16906a9ee8afSDave Airlie 
169110ebc0bcSDave Airlie 	if (fbcon) {
1692ac751efaSTorben Hohn 		console_lock();
169310ebc0bcSDave Airlie 	}
16947473e830SDave Airlie 	if (resume) {
1695771fe6b9SJerome Glisse 		pci_set_power_state(dev->pdev, PCI_D0);
1696771fe6b9SJerome Glisse 		pci_restore_state(dev->pdev);
1697771fe6b9SJerome Glisse 		if (pci_enable_device(dev->pdev)) {
169810ebc0bcSDave Airlie 			if (fbcon)
1699ac751efaSTorben Hohn 				console_unlock();
1700771fe6b9SJerome Glisse 			return -1;
1701771fe6b9SJerome Glisse 		}
17027473e830SDave Airlie 	}
17030ebf1717SDave Airlie 	/* resume AGP if in use */
17040ebf1717SDave Airlie 	radeon_agp_resume(rdev);
17053ce0a23dSJerome Glisse 	radeon_resume(rdev);
170604eb2206SChristian König 
170704eb2206SChristian König 	r = radeon_ib_ring_tests(rdev);
170804eb2206SChristian König 	if (r)
170904eb2206SChristian König 		DRM_ERROR("ib ring test failed (%d).\n", r);
171004eb2206SChristian König 
1711bc6a6295SAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
17126c7bcceaSAlex Deucher 		/* do dpm late init */
17136c7bcceaSAlex Deucher 		r = radeon_pm_late_init(rdev);
17146c7bcceaSAlex Deucher 		if (r) {
17156c7bcceaSAlex Deucher 			rdev->pm.dpm_enabled = false;
17166c7bcceaSAlex Deucher 			DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
17176c7bcceaSAlex Deucher 		}
1718bc6a6295SAlex Deucher 	} else {
1719bc6a6295SAlex Deucher 		/* resume old pm late */
1720bc6a6295SAlex Deucher 		radeon_pm_resume(rdev);
17216c7bcceaSAlex Deucher 	}
17226c7bcceaSAlex Deucher 
1723f657c2a7SYang Zhao 	radeon_restore_bios_scratch_regs(rdev);
172409bdf591SCedric Godin 
1725f3cbb17bSGrigori Goronzy 	/* pin cursors */
1726f3cbb17bSGrigori Goronzy 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1727f3cbb17bSGrigori Goronzy 		struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1728f3cbb17bSGrigori Goronzy 
1729f3cbb17bSGrigori Goronzy 		if (radeon_crtc->cursor_bo) {
1730f3cbb17bSGrigori Goronzy 			struct radeon_bo *robj = gem_to_radeon_bo(radeon_crtc->cursor_bo);
1731f3cbb17bSGrigori Goronzy 			r = radeon_bo_reserve(robj, false);
1732f3cbb17bSGrigori Goronzy 			if (r == 0) {
1733f3cbb17bSGrigori Goronzy 				/* Only 27 bit offset for legacy cursor */
1734f3cbb17bSGrigori Goronzy 				r = radeon_bo_pin_restricted(robj,
1735f3cbb17bSGrigori Goronzy 							     RADEON_GEM_DOMAIN_VRAM,
1736f3cbb17bSGrigori Goronzy 							     ASIC_IS_AVIVO(rdev) ?
1737f3cbb17bSGrigori Goronzy 							     0 : 1 << 27,
1738f3cbb17bSGrigori Goronzy 							     &radeon_crtc->cursor_addr);
1739f3cbb17bSGrigori Goronzy 				if (r != 0)
1740f3cbb17bSGrigori Goronzy 					DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
1741f3cbb17bSGrigori Goronzy 				radeon_bo_unreserve(robj);
1742f3cbb17bSGrigori Goronzy 			}
1743f3cbb17bSGrigori Goronzy 		}
1744f3cbb17bSGrigori Goronzy 	}
1745f3cbb17bSGrigori Goronzy 
17463fa47d9eSAlex Deucher 	/* init dig PHYs, disp eng pll */
17473fa47d9eSAlex Deucher 	if (rdev->is_atom_bios) {
1748ac89af1eSAlex Deucher 		radeon_atom_encoder_init(rdev);
1749f3f1f03eSAlex Deucher 		radeon_atom_disp_eng_pll_init(rdev);
1750bced76f2SAlex Deucher 		/* turn on the BL */
1751bced76f2SAlex Deucher 		if (rdev->mode_info.bl_encoder) {
1752bced76f2SAlex Deucher 			u8 bl_level = radeon_get_backlight_level(rdev,
1753bced76f2SAlex Deucher 								 rdev->mode_info.bl_encoder);
1754bced76f2SAlex Deucher 			radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
1755bced76f2SAlex Deucher 						   bl_level);
1756bced76f2SAlex Deucher 		}
17573fa47d9eSAlex Deucher 	}
1758d4877cf2SAlex Deucher 	/* reset hpd state */
1759d4877cf2SAlex Deucher 	radeon_hpd_init(rdev);
1760771fe6b9SJerome Glisse 	/* blat the mode back in */
1761ec9954fcSDave Airlie 	if (fbcon) {
1762771fe6b9SJerome Glisse 		drm_helper_resume_force_mode(dev);
1763a93f344dSAlex Deucher 		/* turn on display hw */
17646adaed5bSDaniel Vetter 		drm_modeset_lock_all(dev);
1765a93f344dSAlex Deucher 		list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1766a93f344dSAlex Deucher 			drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
1767a93f344dSAlex Deucher 		}
17686adaed5bSDaniel Vetter 		drm_modeset_unlock_all(dev);
1769ec9954fcSDave Airlie 	}
177086698c20SSeth Forshee 
177186698c20SSeth Forshee 	drm_kms_helper_poll_enable(dev);
177218ee37a4SDaniel Vetter 
17733640da2fSAlex Deucher 	/* set the power state here in case we are a PX system or headless */
17743640da2fSAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled)
17753640da2fSAlex Deucher 		radeon_pm_compute_clocks(rdev);
17763640da2fSAlex Deucher 
177718ee37a4SDaniel Vetter 	if (fbcon) {
177818ee37a4SDaniel Vetter 		radeon_fbdev_set_suspend(rdev, 0);
177918ee37a4SDaniel Vetter 		console_unlock();
178018ee37a4SDaniel Vetter 	}
178118ee37a4SDaniel Vetter 
1782771fe6b9SJerome Glisse 	return 0;
1783771fe6b9SJerome Glisse }
1784771fe6b9SJerome Glisse 
17850c195119SAlex Deucher /**
17860c195119SAlex Deucher  * radeon_gpu_reset - reset the asic
17870c195119SAlex Deucher  *
17880c195119SAlex Deucher  * @rdev: radeon device pointer
17890c195119SAlex Deucher  *
17900c195119SAlex Deucher  * Attempt the reset the GPU if it has hung (all asics).
17910c195119SAlex Deucher  * Returns 0 for success or an error on failure.
17920c195119SAlex Deucher  */
179390aca4d2SJerome Glisse int radeon_gpu_reset(struct radeon_device *rdev)
179490aca4d2SJerome Glisse {
179555d7c221SChristian König 	unsigned ring_sizes[RADEON_NUM_RINGS];
179655d7c221SChristian König 	uint32_t *ring_data[RADEON_NUM_RINGS];
179755d7c221SChristian König 
179855d7c221SChristian König 	bool saved = false;
179955d7c221SChristian König 
180055d7c221SChristian König 	int i, r;
18018fd1b84cSDave Airlie 	int resched;
180290aca4d2SJerome Glisse 
1803dee53e7fSJerome Glisse 	down_write(&rdev->exclusive_lock);
1804f9eaf9aeSChristian König 
1805f9eaf9aeSChristian König 	if (!rdev->needs_reset) {
1806f9eaf9aeSChristian König 		up_write(&rdev->exclusive_lock);
1807f9eaf9aeSChristian König 		return 0;
1808f9eaf9aeSChristian König 	}
1809f9eaf9aeSChristian König 
181072b9076bSMarek Olšák 	atomic_inc(&rdev->gpu_reset_counter);
181172b9076bSMarek Olšák 
181290aca4d2SJerome Glisse 	radeon_save_bios_scratch_regs(rdev);
18138fd1b84cSDave Airlie 	/* block TTM */
18148fd1b84cSDave Airlie 	resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
181590aca4d2SJerome Glisse 	radeon_suspend(rdev);
181673ef0e0dSAlex Deucher 	radeon_hpd_fini(rdev);
181790aca4d2SJerome Glisse 
181855d7c221SChristian König 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
181955d7c221SChristian König 		ring_sizes[i] = radeon_ring_backup(rdev, &rdev->ring[i],
182055d7c221SChristian König 						   &ring_data[i]);
182155d7c221SChristian König 		if (ring_sizes[i]) {
182255d7c221SChristian König 			saved = true;
182355d7c221SChristian König 			dev_info(rdev->dev, "Saved %d dwords of commands "
182455d7c221SChristian König 				 "on ring %d.\n", ring_sizes[i], i);
182555d7c221SChristian König 		}
182655d7c221SChristian König 	}
182755d7c221SChristian König 
182890aca4d2SJerome Glisse 	r = radeon_asic_reset(rdev);
182990aca4d2SJerome Glisse 	if (!r) {
183055d7c221SChristian König 		dev_info(rdev->dev, "GPU reset succeeded, trying to resume\n");
183190aca4d2SJerome Glisse 		radeon_resume(rdev);
183255d7c221SChristian König 	}
183304eb2206SChristian König 
183490aca4d2SJerome Glisse 	radeon_restore_bios_scratch_regs(rdev);
183555d7c221SChristian König 
183655d7c221SChristian König 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
18379bb39ff4SMaarten Lankhorst 		if (!r && ring_data[i]) {
183855d7c221SChristian König 			radeon_ring_restore(rdev, &rdev->ring[i],
183955d7c221SChristian König 					    ring_sizes[i], ring_data[i]);
184055d7c221SChristian König 		} else {
1841eb98c709SChristian König 			radeon_fence_driver_force_completion(rdev, i);
184255d7c221SChristian König 			kfree(ring_data[i]);
184355d7c221SChristian König 		}
184455d7c221SChristian König 	}
184555d7c221SChristian König 
1846c940b447SAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
1847c940b447SAlex Deucher 		/* do dpm late init */
1848c940b447SAlex Deucher 		r = radeon_pm_late_init(rdev);
1849c940b447SAlex Deucher 		if (r) {
1850c940b447SAlex Deucher 			rdev->pm.dpm_enabled = false;
1851c940b447SAlex Deucher 			DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
1852c940b447SAlex Deucher 		}
1853c940b447SAlex Deucher 	} else {
1854c940b447SAlex Deucher 		/* resume old pm late */
185595f59509SAlex Deucher 		radeon_pm_resume(rdev);
1856c940b447SAlex Deucher 	}
1857c940b447SAlex Deucher 
185873ef0e0dSAlex Deucher 	/* init dig PHYs, disp eng pll */
185973ef0e0dSAlex Deucher 	if (rdev->is_atom_bios) {
186073ef0e0dSAlex Deucher 		radeon_atom_encoder_init(rdev);
186173ef0e0dSAlex Deucher 		radeon_atom_disp_eng_pll_init(rdev);
186273ef0e0dSAlex Deucher 		/* turn on the BL */
186373ef0e0dSAlex Deucher 		if (rdev->mode_info.bl_encoder) {
186473ef0e0dSAlex Deucher 			u8 bl_level = radeon_get_backlight_level(rdev,
186573ef0e0dSAlex Deucher 								 rdev->mode_info.bl_encoder);
186673ef0e0dSAlex Deucher 			radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
186773ef0e0dSAlex Deucher 						   bl_level);
186873ef0e0dSAlex Deucher 		}
186973ef0e0dSAlex Deucher 	}
187073ef0e0dSAlex Deucher 	/* reset hpd state */
187173ef0e0dSAlex Deucher 	radeon_hpd_init(rdev);
187273ef0e0dSAlex Deucher 
18739bb39ff4SMaarten Lankhorst 	ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
18743c036389SChristian König 
18753c036389SChristian König 	rdev->in_reset = true;
18763c036389SChristian König 	rdev->needs_reset = false;
18773c036389SChristian König 
18789bb39ff4SMaarten Lankhorst 	downgrade_write(&rdev->exclusive_lock);
18799bb39ff4SMaarten Lankhorst 
1880d3493574SJerome Glisse 	drm_helper_resume_force_mode(rdev->ddev);
1881d3493574SJerome Glisse 
1882c940b447SAlex Deucher 	/* set the power state here in case we are a PX system or headless */
1883c940b447SAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled)
1884c940b447SAlex Deucher 		radeon_pm_compute_clocks(rdev);
1885c940b447SAlex Deucher 
18869bb39ff4SMaarten Lankhorst 	if (!r) {
18879bb39ff4SMaarten Lankhorst 		r = radeon_ib_ring_tests(rdev);
18889bb39ff4SMaarten Lankhorst 		if (r && saved)
18899bb39ff4SMaarten Lankhorst 			r = -EAGAIN;
18909bb39ff4SMaarten Lankhorst 	} else {
189190aca4d2SJerome Glisse 		/* bad news, how to tell it to userspace ? */
189290aca4d2SJerome Glisse 		dev_info(rdev->dev, "GPU reset failed\n");
18937a1619b9SMichel Dänzer 	}
18947a1619b9SMichel Dänzer 
18959bb39ff4SMaarten Lankhorst 	rdev->needs_reset = r == -EAGAIN;
18969bb39ff4SMaarten Lankhorst 	rdev->in_reset = false;
18979bb39ff4SMaarten Lankhorst 
18989bb39ff4SMaarten Lankhorst 	up_read(&rdev->exclusive_lock);
189990aca4d2SJerome Glisse 	return r;
190090aca4d2SJerome Glisse }
190190aca4d2SJerome Glisse 
1902771fe6b9SJerome Glisse 
1903771fe6b9SJerome Glisse /*
1904771fe6b9SJerome Glisse  * Debugfs
1905771fe6b9SJerome Glisse  */
1906771fe6b9SJerome Glisse int radeon_debugfs_add_files(struct radeon_device *rdev,
1907771fe6b9SJerome Glisse 			     struct drm_info_list *files,
1908771fe6b9SJerome Glisse 			     unsigned nfiles)
1909771fe6b9SJerome Glisse {
1910771fe6b9SJerome Glisse 	unsigned i;
1911771fe6b9SJerome Glisse 
19124d8bf9aeSChristian König 	for (i = 0; i < rdev->debugfs_count; i++) {
19134d8bf9aeSChristian König 		if (rdev->debugfs[i].files == files) {
1914771fe6b9SJerome Glisse 			/* Already registered */
1915771fe6b9SJerome Glisse 			return 0;
1916771fe6b9SJerome Glisse 		}
1917771fe6b9SJerome Glisse 	}
1918c245cb9eSMichael Witten 
19194d8bf9aeSChristian König 	i = rdev->debugfs_count + 1;
1920c245cb9eSMichael Witten 	if (i > RADEON_DEBUGFS_MAX_COMPONENTS) {
1921c245cb9eSMichael Witten 		DRM_ERROR("Reached maximum number of debugfs components.\n");
1922c245cb9eSMichael Witten 		DRM_ERROR("Report so we increase "
1923c245cb9eSMichael Witten 			  "RADEON_DEBUGFS_MAX_COMPONENTS.\n");
1924771fe6b9SJerome Glisse 		return -EINVAL;
1925771fe6b9SJerome Glisse 	}
19264d8bf9aeSChristian König 	rdev->debugfs[rdev->debugfs_count].files = files;
19274d8bf9aeSChristian König 	rdev->debugfs[rdev->debugfs_count].num_files = nfiles;
19284d8bf9aeSChristian König 	rdev->debugfs_count = i;
1929771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS)
1930771fe6b9SJerome Glisse 	drm_debugfs_create_files(files, nfiles,
1931771fe6b9SJerome Glisse 				 rdev->ddev->control->debugfs_root,
1932771fe6b9SJerome Glisse 				 rdev->ddev->control);
1933771fe6b9SJerome Glisse 	drm_debugfs_create_files(files, nfiles,
1934771fe6b9SJerome Glisse 				 rdev->ddev->primary->debugfs_root,
1935771fe6b9SJerome Glisse 				 rdev->ddev->primary);
1936771fe6b9SJerome Glisse #endif
1937771fe6b9SJerome Glisse 	return 0;
1938771fe6b9SJerome Glisse }
1939771fe6b9SJerome Glisse 
19404d8bf9aeSChristian König static void radeon_debugfs_remove_files(struct radeon_device *rdev)
19414d8bf9aeSChristian König {
19424d8bf9aeSChristian König #if defined(CONFIG_DEBUG_FS)
19434d8bf9aeSChristian König 	unsigned i;
19444d8bf9aeSChristian König 
19454d8bf9aeSChristian König 	for (i = 0; i < rdev->debugfs_count; i++) {
19464d8bf9aeSChristian König 		drm_debugfs_remove_files(rdev->debugfs[i].files,
19474d8bf9aeSChristian König 					 rdev->debugfs[i].num_files,
19484d8bf9aeSChristian König 					 rdev->ddev->control);
19494d8bf9aeSChristian König 		drm_debugfs_remove_files(rdev->debugfs[i].files,
19504d8bf9aeSChristian König 					 rdev->debugfs[i].num_files,
19514d8bf9aeSChristian König 					 rdev->ddev->primary);
19524d8bf9aeSChristian König 	}
19534d8bf9aeSChristian König #endif
19544d8bf9aeSChristian König }
19554d8bf9aeSChristian König 
1956771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS)
1957771fe6b9SJerome Glisse int radeon_debugfs_init(struct drm_minor *minor)
1958771fe6b9SJerome Glisse {
1959771fe6b9SJerome Glisse 	return 0;
1960771fe6b9SJerome Glisse }
1961771fe6b9SJerome Glisse 
1962771fe6b9SJerome Glisse void radeon_debugfs_cleanup(struct drm_minor *minor)
1963771fe6b9SJerome Glisse {
1964771fe6b9SJerome Glisse }
1965771fe6b9SJerome Glisse #endif
1966