xref: /openbmc/linux/drivers/gpu/drm/radeon/radeon_device.c (revision ec9aaaff66668217c0c5634d062b350429d8cfd2)
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>
33b8751946SLukas Wunner #include <linux/pm_runtime.h>
3428d52043SDave Airlie #include <linux/vgaarb.h>
356a9ee8afSDave Airlie #include <linux/vga_switcheroo.h>
36bcc65fd8SMatthew Garrett #include <linux/efi.h>
37771fe6b9SJerome Glisse #include "radeon_reg.h"
38771fe6b9SJerome Glisse #include "radeon.h"
39771fe6b9SJerome Glisse #include "atom.h"
40771fe6b9SJerome Glisse 
411b5331d9SJerome Glisse static const char radeon_family_name[][16] = {
421b5331d9SJerome Glisse 	"R100",
431b5331d9SJerome Glisse 	"RV100",
441b5331d9SJerome Glisse 	"RS100",
451b5331d9SJerome Glisse 	"RV200",
461b5331d9SJerome Glisse 	"RS200",
471b5331d9SJerome Glisse 	"R200",
481b5331d9SJerome Glisse 	"RV250",
491b5331d9SJerome Glisse 	"RS300",
501b5331d9SJerome Glisse 	"RV280",
511b5331d9SJerome Glisse 	"R300",
521b5331d9SJerome Glisse 	"R350",
531b5331d9SJerome Glisse 	"RV350",
541b5331d9SJerome Glisse 	"RV380",
551b5331d9SJerome Glisse 	"R420",
561b5331d9SJerome Glisse 	"R423",
571b5331d9SJerome Glisse 	"RV410",
581b5331d9SJerome Glisse 	"RS400",
591b5331d9SJerome Glisse 	"RS480",
601b5331d9SJerome Glisse 	"RS600",
611b5331d9SJerome Glisse 	"RS690",
621b5331d9SJerome Glisse 	"RS740",
631b5331d9SJerome Glisse 	"RV515",
641b5331d9SJerome Glisse 	"R520",
651b5331d9SJerome Glisse 	"RV530",
661b5331d9SJerome Glisse 	"RV560",
671b5331d9SJerome Glisse 	"RV570",
681b5331d9SJerome Glisse 	"R580",
691b5331d9SJerome Glisse 	"R600",
701b5331d9SJerome Glisse 	"RV610",
711b5331d9SJerome Glisse 	"RV630",
721b5331d9SJerome Glisse 	"RV670",
731b5331d9SJerome Glisse 	"RV620",
741b5331d9SJerome Glisse 	"RV635",
751b5331d9SJerome Glisse 	"RS780",
761b5331d9SJerome Glisse 	"RS880",
771b5331d9SJerome Glisse 	"RV770",
781b5331d9SJerome Glisse 	"RV730",
791b5331d9SJerome Glisse 	"RV710",
801b5331d9SJerome Glisse 	"RV740",
811b5331d9SJerome Glisse 	"CEDAR",
821b5331d9SJerome Glisse 	"REDWOOD",
831b5331d9SJerome Glisse 	"JUNIPER",
841b5331d9SJerome Glisse 	"CYPRESS",
851b5331d9SJerome Glisse 	"HEMLOCK",
86b08ebe7eSAlex Deucher 	"PALM",
874df64e65SAlex Deucher 	"SUMO",
884df64e65SAlex Deucher 	"SUMO2",
891fe18305SAlex Deucher 	"BARTS",
901fe18305SAlex Deucher 	"TURKS",
911fe18305SAlex Deucher 	"CAICOS",
92b7cfc9feSAlex Deucher 	"CAYMAN",
938848f759SAlex Deucher 	"ARUBA",
94cb28bb34SAlex Deucher 	"TAHITI",
95cb28bb34SAlex Deucher 	"PITCAIRN",
96cb28bb34SAlex Deucher 	"VERDE",
97624d3524SAlex Deucher 	"OLAND",
98b5d9d726SAlex Deucher 	"HAINAN",
996eac752eSAlex Deucher 	"BONAIRE",
1006eac752eSAlex Deucher 	"KAVERI",
1016eac752eSAlex Deucher 	"KABINI",
1023bf599e8SAlex Deucher 	"HAWAII",
103b0a9f22aSSamuel Li 	"MULLINS",
1041b5331d9SJerome Glisse 	"LAST",
1051b5331d9SJerome Glisse };
1061b5331d9SJerome Glisse 
1074807c5a8SAlex Deucher #define RADEON_PX_QUIRK_DISABLE_PX  (1 << 0)
1084807c5a8SAlex Deucher #define RADEON_PX_QUIRK_LONG_WAKEUP (1 << 1)
1094807c5a8SAlex Deucher 
1104807c5a8SAlex Deucher struct radeon_px_quirk {
1114807c5a8SAlex Deucher 	u32 chip_vendor;
1124807c5a8SAlex Deucher 	u32 chip_device;
1134807c5a8SAlex Deucher 	u32 subsys_vendor;
1144807c5a8SAlex Deucher 	u32 subsys_device;
1154807c5a8SAlex Deucher 	u32 px_quirk_flags;
1164807c5a8SAlex Deucher };
1174807c5a8SAlex Deucher 
1184807c5a8SAlex Deucher static struct radeon_px_quirk radeon_px_quirk_list[] = {
1194807c5a8SAlex Deucher 	/* Acer aspire 5560g (CPU: AMD A4-3305M; GPU: AMD Radeon HD 6480g + 7470m)
1204807c5a8SAlex Deucher 	 * https://bugzilla.kernel.org/show_bug.cgi?id=74551
1214807c5a8SAlex Deucher 	 */
1224807c5a8SAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6760, 0x1025, 0x0672, RADEON_PX_QUIRK_DISABLE_PX },
1234807c5a8SAlex Deucher 	/* Asus K73TA laptop with AMD A6-3400M APU and Radeon 6550 GPU
1244807c5a8SAlex Deucher 	 * https://bugzilla.kernel.org/show_bug.cgi?id=51381
1254807c5a8SAlex Deucher 	 */
1264807c5a8SAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6741, 0x1043, 0x108c, RADEON_PX_QUIRK_DISABLE_PX },
127ff1b1294SAlex Deucher 	/* Asus K53TK laptop with AMD A6-3420M APU and Radeon 7670m GPU
128ff1b1294SAlex Deucher 	 * https://bugzilla.kernel.org/show_bug.cgi?id=51381
129ff1b1294SAlex Deucher 	 */
130ff1b1294SAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6840, 0x1043, 0x2122, RADEON_PX_QUIRK_DISABLE_PX },
1314807c5a8SAlex Deucher 	/* macbook pro 8.2 */
1324807c5a8SAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6741, PCI_VENDOR_ID_APPLE, 0x00e2, RADEON_PX_QUIRK_LONG_WAKEUP },
1334807c5a8SAlex Deucher 	{ 0, 0, 0, 0, 0 },
1344807c5a8SAlex Deucher };
1354807c5a8SAlex Deucher 
13690c4cde9SAlex Deucher bool radeon_is_px(struct drm_device *dev)
13790c4cde9SAlex Deucher {
13890c4cde9SAlex Deucher 	struct radeon_device *rdev = dev->dev_private;
13990c4cde9SAlex Deucher 
14090c4cde9SAlex Deucher 	if (rdev->flags & RADEON_IS_PX)
14190c4cde9SAlex Deucher 		return true;
14290c4cde9SAlex Deucher 	return false;
14390c4cde9SAlex Deucher }
14410ebc0bcSDave Airlie 
1454807c5a8SAlex Deucher static void radeon_device_handle_px_quirks(struct radeon_device *rdev)
1464807c5a8SAlex Deucher {
1474807c5a8SAlex Deucher 	struct radeon_px_quirk *p = radeon_px_quirk_list;
1484807c5a8SAlex Deucher 
1494807c5a8SAlex Deucher 	/* Apply PX quirks */
1504807c5a8SAlex Deucher 	while (p && p->chip_device != 0) {
1514807c5a8SAlex Deucher 		if (rdev->pdev->vendor == p->chip_vendor &&
1524807c5a8SAlex Deucher 		    rdev->pdev->device == p->chip_device &&
1534807c5a8SAlex Deucher 		    rdev->pdev->subsystem_vendor == p->subsys_vendor &&
1544807c5a8SAlex Deucher 		    rdev->pdev->subsystem_device == p->subsys_device) {
1554807c5a8SAlex Deucher 			rdev->px_quirk_flags = p->px_quirk_flags;
1564807c5a8SAlex Deucher 			break;
1574807c5a8SAlex Deucher 		}
1584807c5a8SAlex Deucher 		++p;
1594807c5a8SAlex Deucher 	}
1604807c5a8SAlex Deucher 
1614807c5a8SAlex Deucher 	if (rdev->px_quirk_flags & RADEON_PX_QUIRK_DISABLE_PX)
1624807c5a8SAlex Deucher 		rdev->flags &= ~RADEON_IS_PX;
1634807c5a8SAlex Deucher }
1644807c5a8SAlex Deucher 
1650c195119SAlex Deucher /**
1662e1b65f9SAlex Deucher  * radeon_program_register_sequence - program an array of registers.
1672e1b65f9SAlex Deucher  *
1682e1b65f9SAlex Deucher  * @rdev: radeon_device pointer
1692e1b65f9SAlex Deucher  * @registers: pointer to the register array
1702e1b65f9SAlex Deucher  * @array_size: size of the register array
1712e1b65f9SAlex Deucher  *
1722e1b65f9SAlex Deucher  * Programs an array or registers with and and or masks.
1732e1b65f9SAlex Deucher  * This is a helper for setting golden registers.
1742e1b65f9SAlex Deucher  */
1752e1b65f9SAlex Deucher void radeon_program_register_sequence(struct radeon_device *rdev,
1762e1b65f9SAlex Deucher 				      const u32 *registers,
1772e1b65f9SAlex Deucher 				      const u32 array_size)
1782e1b65f9SAlex Deucher {
1792e1b65f9SAlex Deucher 	u32 tmp, reg, and_mask, or_mask;
1802e1b65f9SAlex Deucher 	int i;
1812e1b65f9SAlex Deucher 
1822e1b65f9SAlex Deucher 	if (array_size % 3)
1832e1b65f9SAlex Deucher 		return;
1842e1b65f9SAlex Deucher 
1852e1b65f9SAlex Deucher 	for (i = 0; i < array_size; i +=3) {
1862e1b65f9SAlex Deucher 		reg = registers[i + 0];
1872e1b65f9SAlex Deucher 		and_mask = registers[i + 1];
1882e1b65f9SAlex Deucher 		or_mask = registers[i + 2];
1892e1b65f9SAlex Deucher 
1902e1b65f9SAlex Deucher 		if (and_mask == 0xffffffff) {
1912e1b65f9SAlex Deucher 			tmp = or_mask;
1922e1b65f9SAlex Deucher 		} else {
1932e1b65f9SAlex Deucher 			tmp = RREG32(reg);
1942e1b65f9SAlex Deucher 			tmp &= ~and_mask;
1952e1b65f9SAlex Deucher 			tmp |= or_mask;
1962e1b65f9SAlex Deucher 		}
1972e1b65f9SAlex Deucher 		WREG32(reg, tmp);
1982e1b65f9SAlex Deucher 	}
1992e1b65f9SAlex Deucher }
2002e1b65f9SAlex Deucher 
2011a0041b8SAlex Deucher void radeon_pci_config_reset(struct radeon_device *rdev)
2021a0041b8SAlex Deucher {
2031a0041b8SAlex Deucher 	pci_write_config_dword(rdev->pdev, 0x7c, RADEON_ASIC_RESET_DATA);
2041a0041b8SAlex Deucher }
2051a0041b8SAlex Deucher 
2062e1b65f9SAlex Deucher /**
2070c195119SAlex Deucher  * radeon_surface_init - Clear GPU surface registers.
2080c195119SAlex Deucher  *
2090c195119SAlex Deucher  * @rdev: radeon_device pointer
2100c195119SAlex Deucher  *
2110c195119SAlex Deucher  * Clear GPU surface registers (r1xx-r5xx).
212b1e3a6d1SMichel Dänzer  */
2133ce0a23dSJerome Glisse void radeon_surface_init(struct radeon_device *rdev)
214b1e3a6d1SMichel Dänzer {
215b1e3a6d1SMichel Dänzer 	/* FIXME: check this out */
216b1e3a6d1SMichel Dänzer 	if (rdev->family < CHIP_R600) {
217b1e3a6d1SMichel Dänzer 		int i;
218b1e3a6d1SMichel Dänzer 
219550e2d92SDave Airlie 		for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
220550e2d92SDave Airlie 			if (rdev->surface_regs[i].bo)
221550e2d92SDave Airlie 				radeon_bo_get_surface_reg(rdev->surface_regs[i].bo);
222550e2d92SDave Airlie 			else
223550e2d92SDave Airlie 				radeon_clear_surface_reg(rdev, i);
224b1e3a6d1SMichel Dänzer 		}
225e024e110SDave Airlie 		/* enable surfaces */
226e024e110SDave Airlie 		WREG32(RADEON_SURFACE_CNTL, 0);
227b1e3a6d1SMichel Dänzer 	}
228b1e3a6d1SMichel Dänzer }
229b1e3a6d1SMichel Dänzer 
230b1e3a6d1SMichel Dänzer /*
231771fe6b9SJerome Glisse  * GPU scratch registers helpers function.
232771fe6b9SJerome Glisse  */
2330c195119SAlex Deucher /**
2340c195119SAlex Deucher  * radeon_scratch_init - Init scratch register driver information.
2350c195119SAlex Deucher  *
2360c195119SAlex Deucher  * @rdev: radeon_device pointer
2370c195119SAlex Deucher  *
2380c195119SAlex Deucher  * Init CP scratch register driver information (r1xx-r5xx)
2390c195119SAlex Deucher  */
2403ce0a23dSJerome Glisse void radeon_scratch_init(struct radeon_device *rdev)
241771fe6b9SJerome Glisse {
242771fe6b9SJerome Glisse 	int i;
243771fe6b9SJerome Glisse 
244771fe6b9SJerome Glisse 	/* FIXME: check this out */
245771fe6b9SJerome Glisse 	if (rdev->family < CHIP_R300) {
246771fe6b9SJerome Glisse 		rdev->scratch.num_reg = 5;
247771fe6b9SJerome Glisse 	} else {
248771fe6b9SJerome Glisse 		rdev->scratch.num_reg = 7;
249771fe6b9SJerome Glisse 	}
250724c80e1SAlex Deucher 	rdev->scratch.reg_base = RADEON_SCRATCH_REG0;
251771fe6b9SJerome Glisse 	for (i = 0; i < rdev->scratch.num_reg; i++) {
252771fe6b9SJerome Glisse 		rdev->scratch.free[i] = true;
253724c80e1SAlex Deucher 		rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4);
254771fe6b9SJerome Glisse 	}
255771fe6b9SJerome Glisse }
256771fe6b9SJerome Glisse 
2570c195119SAlex Deucher /**
2580c195119SAlex Deucher  * radeon_scratch_get - Allocate a scratch register
2590c195119SAlex Deucher  *
2600c195119SAlex Deucher  * @rdev: radeon_device pointer
2610c195119SAlex Deucher  * @reg: scratch register mmio offset
2620c195119SAlex Deucher  *
2630c195119SAlex Deucher  * Allocate a CP scratch register for use by the driver (all asics).
2640c195119SAlex Deucher  * Returns 0 on success or -EINVAL on failure.
2650c195119SAlex Deucher  */
266771fe6b9SJerome Glisse int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg)
267771fe6b9SJerome Glisse {
268771fe6b9SJerome Glisse 	int i;
269771fe6b9SJerome Glisse 
270771fe6b9SJerome Glisse 	for (i = 0; i < rdev->scratch.num_reg; i++) {
271771fe6b9SJerome Glisse 		if (rdev->scratch.free[i]) {
272771fe6b9SJerome Glisse 			rdev->scratch.free[i] = false;
273771fe6b9SJerome Glisse 			*reg = rdev->scratch.reg[i];
274771fe6b9SJerome Glisse 			return 0;
275771fe6b9SJerome Glisse 		}
276771fe6b9SJerome Glisse 	}
277771fe6b9SJerome Glisse 	return -EINVAL;
278771fe6b9SJerome Glisse }
279771fe6b9SJerome Glisse 
2800c195119SAlex Deucher /**
2810c195119SAlex Deucher  * radeon_scratch_free - Free a scratch register
2820c195119SAlex Deucher  *
2830c195119SAlex Deucher  * @rdev: radeon_device pointer
2840c195119SAlex Deucher  * @reg: scratch register mmio offset
2850c195119SAlex Deucher  *
2860c195119SAlex Deucher  * Free a CP scratch register allocated for use by the driver (all asics)
2870c195119SAlex Deucher  */
288771fe6b9SJerome Glisse void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg)
289771fe6b9SJerome Glisse {
290771fe6b9SJerome Glisse 	int i;
291771fe6b9SJerome Glisse 
292771fe6b9SJerome Glisse 	for (i = 0; i < rdev->scratch.num_reg; i++) {
293771fe6b9SJerome Glisse 		if (rdev->scratch.reg[i] == reg) {
294771fe6b9SJerome Glisse 			rdev->scratch.free[i] = true;
295771fe6b9SJerome Glisse 			return;
296771fe6b9SJerome Glisse 		}
297771fe6b9SJerome Glisse 	}
298771fe6b9SJerome Glisse }
299771fe6b9SJerome Glisse 
3000c195119SAlex Deucher /*
30175efdee1SAlex Deucher  * GPU doorbell aperture helpers function.
30275efdee1SAlex Deucher  */
30375efdee1SAlex Deucher /**
30475efdee1SAlex Deucher  * radeon_doorbell_init - Init doorbell driver information.
30575efdee1SAlex Deucher  *
30675efdee1SAlex Deucher  * @rdev: radeon_device pointer
30775efdee1SAlex Deucher  *
30875efdee1SAlex Deucher  * Init doorbell driver information (CIK)
30975efdee1SAlex Deucher  * Returns 0 on success, error on failure.
31075efdee1SAlex Deucher  */
31128f5a6cdSRashika Kheria static int radeon_doorbell_init(struct radeon_device *rdev)
31275efdee1SAlex Deucher {
31375efdee1SAlex Deucher 	/* doorbell bar mapping */
31475efdee1SAlex Deucher 	rdev->doorbell.base = pci_resource_start(rdev->pdev, 2);
31575efdee1SAlex Deucher 	rdev->doorbell.size = pci_resource_len(rdev->pdev, 2);
31675efdee1SAlex Deucher 
317d5754ab8SAndrew Lewycky 	rdev->doorbell.num_doorbells = min_t(u32, rdev->doorbell.size / sizeof(u32), RADEON_MAX_DOORBELLS);
318d5754ab8SAndrew Lewycky 	if (rdev->doorbell.num_doorbells == 0)
319d5754ab8SAndrew Lewycky 		return -EINVAL;
32075efdee1SAlex Deucher 
321d5754ab8SAndrew Lewycky 	rdev->doorbell.ptr = ioremap(rdev->doorbell.base, rdev->doorbell.num_doorbells * sizeof(u32));
32275efdee1SAlex Deucher 	if (rdev->doorbell.ptr == NULL) {
32375efdee1SAlex Deucher 		return -ENOMEM;
32475efdee1SAlex Deucher 	}
32575efdee1SAlex Deucher 	DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)rdev->doorbell.base);
32675efdee1SAlex Deucher 	DRM_INFO("doorbell mmio size: %u\n", (unsigned)rdev->doorbell.size);
32775efdee1SAlex Deucher 
328d5754ab8SAndrew Lewycky 	memset(&rdev->doorbell.used, 0, sizeof(rdev->doorbell.used));
32975efdee1SAlex Deucher 
33075efdee1SAlex Deucher 	return 0;
33175efdee1SAlex Deucher }
33275efdee1SAlex Deucher 
33375efdee1SAlex Deucher /**
33475efdee1SAlex Deucher  * radeon_doorbell_fini - Tear down doorbell driver information.
33575efdee1SAlex Deucher  *
33675efdee1SAlex Deucher  * @rdev: radeon_device pointer
33775efdee1SAlex Deucher  *
33875efdee1SAlex Deucher  * Tear down doorbell driver information (CIK)
33975efdee1SAlex Deucher  */
34028f5a6cdSRashika Kheria static void radeon_doorbell_fini(struct radeon_device *rdev)
34175efdee1SAlex Deucher {
34275efdee1SAlex Deucher 	iounmap(rdev->doorbell.ptr);
34375efdee1SAlex Deucher 	rdev->doorbell.ptr = NULL;
34475efdee1SAlex Deucher }
34575efdee1SAlex Deucher 
34675efdee1SAlex Deucher /**
347d5754ab8SAndrew Lewycky  * radeon_doorbell_get - Allocate a doorbell entry
34875efdee1SAlex Deucher  *
34975efdee1SAlex Deucher  * @rdev: radeon_device pointer
350d5754ab8SAndrew Lewycky  * @doorbell: doorbell index
35175efdee1SAlex Deucher  *
352d5754ab8SAndrew Lewycky  * Allocate a doorbell for use by the driver (all asics).
35375efdee1SAlex Deucher  * Returns 0 on success or -EINVAL on failure.
35475efdee1SAlex Deucher  */
35575efdee1SAlex Deucher int radeon_doorbell_get(struct radeon_device *rdev, u32 *doorbell)
35675efdee1SAlex Deucher {
357d5754ab8SAndrew Lewycky 	unsigned long offset = find_first_zero_bit(rdev->doorbell.used, rdev->doorbell.num_doorbells);
358d5754ab8SAndrew Lewycky 	if (offset < rdev->doorbell.num_doorbells) {
359d5754ab8SAndrew Lewycky 		__set_bit(offset, rdev->doorbell.used);
360d5754ab8SAndrew Lewycky 		*doorbell = offset;
36175efdee1SAlex Deucher 		return 0;
362d5754ab8SAndrew Lewycky 	} else {
36375efdee1SAlex Deucher 		return -EINVAL;
36475efdee1SAlex Deucher 	}
365d5754ab8SAndrew Lewycky }
36675efdee1SAlex Deucher 
36775efdee1SAlex Deucher /**
368d5754ab8SAndrew Lewycky  * radeon_doorbell_free - Free a doorbell entry
36975efdee1SAlex Deucher  *
37075efdee1SAlex Deucher  * @rdev: radeon_device pointer
371d5754ab8SAndrew Lewycky  * @doorbell: doorbell index
37275efdee1SAlex Deucher  *
373d5754ab8SAndrew Lewycky  * Free a doorbell allocated for use by the driver (all asics)
37475efdee1SAlex Deucher  */
37575efdee1SAlex Deucher void radeon_doorbell_free(struct radeon_device *rdev, u32 doorbell)
37675efdee1SAlex Deucher {
377d5754ab8SAndrew Lewycky 	if (doorbell < rdev->doorbell.num_doorbells)
378d5754ab8SAndrew Lewycky 		__clear_bit(doorbell, rdev->doorbell.used);
37975efdee1SAlex Deucher }
38075efdee1SAlex Deucher 
381ebff8453SOded Gabbay /**
382ebff8453SOded Gabbay  * radeon_doorbell_get_kfd_info - Report doorbell configuration required to
383ebff8453SOded Gabbay  *                                setup KFD
384ebff8453SOded Gabbay  *
385ebff8453SOded Gabbay  * @rdev: radeon_device pointer
386ebff8453SOded Gabbay  * @aperture_base: output returning doorbell aperture base physical address
387ebff8453SOded Gabbay  * @aperture_size: output returning doorbell aperture size in bytes
388ebff8453SOded Gabbay  * @start_offset: output returning # of doorbell bytes reserved for radeon.
389ebff8453SOded Gabbay  *
390ebff8453SOded Gabbay  * Radeon and the KFD share the doorbell aperture. Radeon sets it up,
391ebff8453SOded Gabbay  * takes doorbells required for its own rings and reports the setup to KFD.
392ebff8453SOded Gabbay  * Radeon reserved doorbells are at the start of the doorbell aperture.
393ebff8453SOded Gabbay  */
394ebff8453SOded Gabbay void radeon_doorbell_get_kfd_info(struct radeon_device *rdev,
395ebff8453SOded Gabbay 				  phys_addr_t *aperture_base,
396ebff8453SOded Gabbay 				  size_t *aperture_size,
397ebff8453SOded Gabbay 				  size_t *start_offset)
398ebff8453SOded Gabbay {
399ebff8453SOded Gabbay 	/* The first num_doorbells are used by radeon.
400ebff8453SOded Gabbay 	 * KFD takes whatever's left in the aperture. */
401ebff8453SOded Gabbay 	if (rdev->doorbell.size > rdev->doorbell.num_doorbells * sizeof(u32)) {
402ebff8453SOded Gabbay 		*aperture_base = rdev->doorbell.base;
403ebff8453SOded Gabbay 		*aperture_size = rdev->doorbell.size;
404ebff8453SOded Gabbay 		*start_offset = rdev->doorbell.num_doorbells * sizeof(u32);
405ebff8453SOded Gabbay 	} else {
406ebff8453SOded Gabbay 		*aperture_base = 0;
407ebff8453SOded Gabbay 		*aperture_size = 0;
408ebff8453SOded Gabbay 		*start_offset = 0;
409ebff8453SOded Gabbay 	}
410ebff8453SOded Gabbay }
411ebff8453SOded Gabbay 
41275efdee1SAlex Deucher /*
4130c195119SAlex Deucher  * radeon_wb_*()
4140c195119SAlex Deucher  * Writeback is the the method by which the the GPU updates special pages
4150c195119SAlex Deucher  * in memory with the status of certain GPU events (fences, ring pointers,
4160c195119SAlex Deucher  * etc.).
4170c195119SAlex Deucher  */
4180c195119SAlex Deucher 
4190c195119SAlex Deucher /**
4200c195119SAlex Deucher  * radeon_wb_disable - Disable Writeback
4210c195119SAlex Deucher  *
4220c195119SAlex Deucher  * @rdev: radeon_device pointer
4230c195119SAlex Deucher  *
4240c195119SAlex Deucher  * Disables Writeback (all asics).  Used for suspend.
4250c195119SAlex Deucher  */
426724c80e1SAlex Deucher void radeon_wb_disable(struct radeon_device *rdev)
427724c80e1SAlex Deucher {
428724c80e1SAlex Deucher 	rdev->wb.enabled = false;
429724c80e1SAlex Deucher }
430724c80e1SAlex Deucher 
4310c195119SAlex Deucher /**
4320c195119SAlex Deucher  * radeon_wb_fini - Disable Writeback and free memory
4330c195119SAlex Deucher  *
4340c195119SAlex Deucher  * @rdev: radeon_device pointer
4350c195119SAlex Deucher  *
4360c195119SAlex Deucher  * Disables Writeback and frees the Writeback memory (all asics).
4370c195119SAlex Deucher  * Used at driver shutdown.
4380c195119SAlex Deucher  */
439724c80e1SAlex Deucher void radeon_wb_fini(struct radeon_device *rdev)
440724c80e1SAlex Deucher {
441724c80e1SAlex Deucher 	radeon_wb_disable(rdev);
442724c80e1SAlex Deucher 	if (rdev->wb.wb_obj) {
443089920f2SJerome Glisse 		if (!radeon_bo_reserve(rdev->wb.wb_obj, false)) {
444089920f2SJerome Glisse 			radeon_bo_kunmap(rdev->wb.wb_obj);
445089920f2SJerome Glisse 			radeon_bo_unpin(rdev->wb.wb_obj);
446089920f2SJerome Glisse 			radeon_bo_unreserve(rdev->wb.wb_obj);
447089920f2SJerome Glisse 		}
448724c80e1SAlex Deucher 		radeon_bo_unref(&rdev->wb.wb_obj);
449724c80e1SAlex Deucher 		rdev->wb.wb = NULL;
450724c80e1SAlex Deucher 		rdev->wb.wb_obj = NULL;
451724c80e1SAlex Deucher 	}
452724c80e1SAlex Deucher }
453724c80e1SAlex Deucher 
4540c195119SAlex Deucher /**
4550c195119SAlex Deucher  * radeon_wb_init- Init Writeback driver info and allocate memory
4560c195119SAlex Deucher  *
4570c195119SAlex Deucher  * @rdev: radeon_device pointer
4580c195119SAlex Deucher  *
4590c195119SAlex Deucher  * Disables Writeback and frees the Writeback memory (all asics).
4600c195119SAlex Deucher  * Used at driver startup.
4610c195119SAlex Deucher  * Returns 0 on success or an -error on failure.
4620c195119SAlex Deucher  */
463724c80e1SAlex Deucher int radeon_wb_init(struct radeon_device *rdev)
464724c80e1SAlex Deucher {
465724c80e1SAlex Deucher 	int r;
466724c80e1SAlex Deucher 
467724c80e1SAlex Deucher 	if (rdev->wb.wb_obj == NULL) {
468441921d5SDaniel Vetter 		r = radeon_bo_create(rdev, RADEON_GPU_PAGE_SIZE, PAGE_SIZE, true,
469831b6966SMaarten Lankhorst 				     RADEON_GEM_DOMAIN_GTT, 0, NULL, NULL,
47002376d82SMichel Dänzer 				     &rdev->wb.wb_obj);
471724c80e1SAlex Deucher 		if (r) {
472724c80e1SAlex Deucher 			dev_warn(rdev->dev, "(%d) create WB bo failed\n", r);
473724c80e1SAlex Deucher 			return r;
474724c80e1SAlex Deucher 		}
475724c80e1SAlex Deucher 		r = radeon_bo_reserve(rdev->wb.wb_obj, false);
476724c80e1SAlex Deucher 		if (unlikely(r != 0)) {
477724c80e1SAlex Deucher 			radeon_wb_fini(rdev);
478724c80e1SAlex Deucher 			return r;
479724c80e1SAlex Deucher 		}
480724c80e1SAlex Deucher 		r = radeon_bo_pin(rdev->wb.wb_obj, RADEON_GEM_DOMAIN_GTT,
481724c80e1SAlex Deucher 				&rdev->wb.gpu_addr);
482724c80e1SAlex Deucher 		if (r) {
483724c80e1SAlex Deucher 			radeon_bo_unreserve(rdev->wb.wb_obj);
484724c80e1SAlex Deucher 			dev_warn(rdev->dev, "(%d) pin WB bo failed\n", r);
485724c80e1SAlex Deucher 			radeon_wb_fini(rdev);
486724c80e1SAlex Deucher 			return r;
487724c80e1SAlex Deucher 		}
488724c80e1SAlex Deucher 		r = radeon_bo_kmap(rdev->wb.wb_obj, (void **)&rdev->wb.wb);
489724c80e1SAlex Deucher 		radeon_bo_unreserve(rdev->wb.wb_obj);
490724c80e1SAlex Deucher 		if (r) {
491724c80e1SAlex Deucher 			dev_warn(rdev->dev, "(%d) map WB bo failed\n", r);
492724c80e1SAlex Deucher 			radeon_wb_fini(rdev);
493724c80e1SAlex Deucher 			return r;
494724c80e1SAlex Deucher 		}
495089920f2SJerome Glisse 	}
496724c80e1SAlex Deucher 
497e6ba7599SAlex Deucher 	/* clear wb memory */
498e6ba7599SAlex Deucher 	memset((char *)rdev->wb.wb, 0, RADEON_GPU_PAGE_SIZE);
499d0f8a854SAlex Deucher 	/* disable event_write fences */
500d0f8a854SAlex Deucher 	rdev->wb.use_event = false;
501724c80e1SAlex Deucher 	/* disabled via module param */
5023b7a2b24SJerome Glisse 	if (radeon_no_wb == 1) {
503724c80e1SAlex Deucher 		rdev->wb.enabled = false;
5043b7a2b24SJerome Glisse 	} else {
505724c80e1SAlex Deucher 		if (rdev->flags & RADEON_IS_AGP) {
50628eebb70SAlex Deucher 			/* often unreliable on AGP */
50728eebb70SAlex Deucher 			rdev->wb.enabled = false;
50828eebb70SAlex Deucher 		} else if (rdev->family < CHIP_R300) {
50928eebb70SAlex Deucher 			/* often unreliable on pre-r300 */
510724c80e1SAlex Deucher 			rdev->wb.enabled = false;
511d0f8a854SAlex Deucher 		} else {
512724c80e1SAlex Deucher 			rdev->wb.enabled = true;
513d0f8a854SAlex Deucher 			/* event_write fences are only available on r600+ */
5143b7a2b24SJerome Glisse 			if (rdev->family >= CHIP_R600) {
515d0f8a854SAlex Deucher 				rdev->wb.use_event = true;
516d0f8a854SAlex Deucher 			}
517724c80e1SAlex Deucher 		}
5183b7a2b24SJerome Glisse 	}
519c994ead6SAlex Deucher 	/* always use writeback/events on NI, APUs */
520c994ead6SAlex Deucher 	if (rdev->family >= CHIP_PALM) {
5217d52785dSAlex Deucher 		rdev->wb.enabled = true;
5227d52785dSAlex Deucher 		rdev->wb.use_event = true;
5237d52785dSAlex Deucher 	}
524724c80e1SAlex Deucher 
525724c80e1SAlex Deucher 	dev_info(rdev->dev, "WB %sabled\n", rdev->wb.enabled ? "en" : "dis");
526724c80e1SAlex Deucher 
527724c80e1SAlex Deucher 	return 0;
528724c80e1SAlex Deucher }
529724c80e1SAlex Deucher 
530d594e46aSJerome Glisse /**
531d594e46aSJerome Glisse  * radeon_vram_location - try to find VRAM location
532d594e46aSJerome Glisse  * @rdev: radeon device structure holding all necessary informations
533d594e46aSJerome Glisse  * @mc: memory controller structure holding memory informations
534d594e46aSJerome Glisse  * @base: base address at which to put VRAM
535d594e46aSJerome Glisse  *
536d594e46aSJerome Glisse  * Function will place try to place VRAM at base address provided
537d594e46aSJerome Glisse  * as parameter (which is so far either PCI aperture address or
538d594e46aSJerome Glisse  * for IGP TOM base address).
539d594e46aSJerome Glisse  *
540d594e46aSJerome Glisse  * If there is not enough space to fit the unvisible VRAM in the 32bits
541d594e46aSJerome Glisse  * address space then we limit the VRAM size to the aperture.
542d594e46aSJerome Glisse  *
543d594e46aSJerome Glisse  * If we are using AGP and if the AGP aperture doesn't allow us to have
544d594e46aSJerome Glisse  * room for all the VRAM than we restrict the VRAM to the PCI aperture
545d594e46aSJerome Glisse  * size and print a warning.
546d594e46aSJerome Glisse  *
547d594e46aSJerome Glisse  * This function will never fails, worst case are limiting VRAM.
548d594e46aSJerome Glisse  *
549d594e46aSJerome Glisse  * Note: GTT start, end, size should be initialized before calling this
550d594e46aSJerome Glisse  * function on AGP platform.
551d594e46aSJerome Glisse  *
55225985edcSLucas De Marchi  * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
553d594e46aSJerome Glisse  * this shouldn't be a problem as we are using the PCI aperture as a reference.
554d594e46aSJerome Glisse  * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
555d594e46aSJerome Glisse  * not IGP.
556d594e46aSJerome Glisse  *
557d594e46aSJerome Glisse  * Note: we use mc_vram_size as on some board we need to program the mc to
558d594e46aSJerome Glisse  * cover the whole aperture even if VRAM size is inferior to aperture size
559d594e46aSJerome Glisse  * Novell bug 204882 + along with lots of ubuntu ones
560d594e46aSJerome Glisse  *
561d594e46aSJerome Glisse  * Note: when limiting vram it's safe to overwritte real_vram_size because
562d594e46aSJerome Glisse  * we are not in case where real_vram_size is inferior to mc_vram_size (ie
563d594e46aSJerome Glisse  * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
564d594e46aSJerome Glisse  * ones)
565d594e46aSJerome Glisse  *
566d594e46aSJerome Glisse  * Note: IGP TOM addr should be the same as the aperture addr, we don't
567d594e46aSJerome Glisse  * explicitly check for that thought.
568d594e46aSJerome Glisse  *
569d594e46aSJerome Glisse  * FIXME: when reducing VRAM size align new size on power of 2.
570771fe6b9SJerome Glisse  */
571d594e46aSJerome Glisse void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base)
572771fe6b9SJerome Glisse {
5731bcb04f7SChristian König 	uint64_t limit = (uint64_t)radeon_vram_limit << 20;
5741bcb04f7SChristian König 
575d594e46aSJerome Glisse 	mc->vram_start = base;
5769ed8b1f9SAlex Deucher 	if (mc->mc_vram_size > (rdev->mc.mc_mask - base + 1)) {
577d594e46aSJerome Glisse 		dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
578d594e46aSJerome Glisse 		mc->real_vram_size = mc->aper_size;
579d594e46aSJerome Glisse 		mc->mc_vram_size = mc->aper_size;
580771fe6b9SJerome Glisse 	}
581d594e46aSJerome Glisse 	mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
5822cbeb4efSJerome Glisse 	if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_start <= mc->gtt_end) {
583d594e46aSJerome Glisse 		dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
584d594e46aSJerome Glisse 		mc->real_vram_size = mc->aper_size;
585d594e46aSJerome Glisse 		mc->mc_vram_size = mc->aper_size;
586771fe6b9SJerome Glisse 	}
587d594e46aSJerome Glisse 	mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
5881bcb04f7SChristian König 	if (limit && limit < mc->real_vram_size)
5891bcb04f7SChristian König 		mc->real_vram_size = limit;
590dd7cc55aSAlex Deucher 	dev_info(rdev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
591d594e46aSJerome Glisse 			mc->mc_vram_size >> 20, mc->vram_start,
592d594e46aSJerome Glisse 			mc->vram_end, mc->real_vram_size >> 20);
593771fe6b9SJerome Glisse }
594771fe6b9SJerome Glisse 
595d594e46aSJerome Glisse /**
596d594e46aSJerome Glisse  * radeon_gtt_location - try to find GTT location
597d594e46aSJerome Glisse  * @rdev: radeon device structure holding all necessary informations
598d594e46aSJerome Glisse  * @mc: memory controller structure holding memory informations
599d594e46aSJerome Glisse  *
600d594e46aSJerome Glisse  * Function will place try to place GTT before or after VRAM.
601d594e46aSJerome Glisse  *
602d594e46aSJerome Glisse  * If GTT size is bigger than space left then we ajust GTT size.
603d594e46aSJerome Glisse  * Thus function will never fails.
604d594e46aSJerome Glisse  *
605d594e46aSJerome Glisse  * FIXME: when reducing GTT size align new size on power of 2.
606d594e46aSJerome Glisse  */
607d594e46aSJerome Glisse void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc)
608d594e46aSJerome Glisse {
609d594e46aSJerome Glisse 	u64 size_af, size_bf;
610d594e46aSJerome Glisse 
6119ed8b1f9SAlex Deucher 	size_af = ((rdev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
6128d369bb1SAlex Deucher 	size_bf = mc->vram_start & ~mc->gtt_base_align;
613d594e46aSJerome Glisse 	if (size_bf > size_af) {
614d594e46aSJerome Glisse 		if (mc->gtt_size > size_bf) {
615d594e46aSJerome Glisse 			dev_warn(rdev->dev, "limiting GTT\n");
616d594e46aSJerome Glisse 			mc->gtt_size = size_bf;
617d594e46aSJerome Glisse 		}
6188d369bb1SAlex Deucher 		mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
619d594e46aSJerome Glisse 	} else {
620d594e46aSJerome Glisse 		if (mc->gtt_size > size_af) {
621d594e46aSJerome Glisse 			dev_warn(rdev->dev, "limiting GTT\n");
622d594e46aSJerome Glisse 			mc->gtt_size = size_af;
623d594e46aSJerome Glisse 		}
6248d369bb1SAlex Deucher 		mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
625d594e46aSJerome Glisse 	}
626d594e46aSJerome Glisse 	mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
627dd7cc55aSAlex Deucher 	dev_info(rdev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
628d594e46aSJerome Glisse 			mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
629d594e46aSJerome Glisse }
630771fe6b9SJerome Glisse 
631771fe6b9SJerome Glisse /*
632771fe6b9SJerome Glisse  * GPU helpers function.
633771fe6b9SJerome Glisse  */
63405082b8bSAlex Deucher 
63505082b8bSAlex Deucher /**
63605082b8bSAlex Deucher  * radeon_device_is_virtual - check if we are running is a virtual environment
63705082b8bSAlex Deucher  *
63805082b8bSAlex Deucher  * Check if the asic has been passed through to a VM (all asics).
63905082b8bSAlex Deucher  * Used at driver startup.
64005082b8bSAlex Deucher  * Returns true if virtual or false if not.
64105082b8bSAlex Deucher  */
642a801abe4SAlex Deucher bool radeon_device_is_virtual(void)
64305082b8bSAlex Deucher {
64405082b8bSAlex Deucher #ifdef CONFIG_X86
64505082b8bSAlex Deucher 	return boot_cpu_has(X86_FEATURE_HYPERVISOR);
64605082b8bSAlex Deucher #else
64705082b8bSAlex Deucher 	return false;
64805082b8bSAlex Deucher #endif
64905082b8bSAlex Deucher }
65005082b8bSAlex Deucher 
6510c195119SAlex Deucher /**
6520c195119SAlex Deucher  * radeon_card_posted - check if the hw has already been initialized
6530c195119SAlex Deucher  *
6540c195119SAlex Deucher  * @rdev: radeon_device pointer
6550c195119SAlex Deucher  *
6560c195119SAlex Deucher  * Check if the asic has been initialized (all asics).
6570c195119SAlex Deucher  * Used at driver startup.
6580c195119SAlex Deucher  * Returns true if initialized or false if not.
6590c195119SAlex Deucher  */
6609f022ddfSJerome Glisse bool radeon_card_posted(struct radeon_device *rdev)
661771fe6b9SJerome Glisse {
662771fe6b9SJerome Glisse 	uint32_t reg;
663771fe6b9SJerome Glisse 
664884031f0SAlex Deucher 	/* for pass through, always force asic_init for CI */
665884031f0SAlex Deucher 	if (rdev->family >= CHIP_BONAIRE &&
666884031f0SAlex Deucher 	    radeon_device_is_virtual())
66705082b8bSAlex Deucher 		return false;
66805082b8bSAlex Deucher 
66950a583f6SAlex Deucher 	/* required for EFI mode on macbook2,1 which uses an r5xx asic */
67083e68189SMatt Fleming 	if (efi_enabled(EFI_BOOT) &&
67150a583f6SAlex Deucher 	    (rdev->pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE) &&
67250a583f6SAlex Deucher 	    (rdev->family < CHIP_R600))
673bcc65fd8SMatthew Garrett 		return false;
674bcc65fd8SMatthew Garrett 
6752cf3a4fcSAlex Deucher 	if (ASIC_IS_NODCE(rdev))
6762cf3a4fcSAlex Deucher 		goto check_memsize;
6772cf3a4fcSAlex Deucher 
678771fe6b9SJerome Glisse 	/* first check CRTCs */
67909fb8bd1SAlex Deucher 	if (ASIC_IS_DCE4(rdev)) {
68018007401SAlex Deucher 		reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
68118007401SAlex Deucher 			RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET);
68209fb8bd1SAlex Deucher 			if (rdev->num_crtc >= 4) {
68309fb8bd1SAlex Deucher 				reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) |
68409fb8bd1SAlex Deucher 					RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET);
68509fb8bd1SAlex Deucher 			}
68609fb8bd1SAlex Deucher 			if (rdev->num_crtc >= 6) {
68709fb8bd1SAlex Deucher 				reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) |
688bcc1c2a1SAlex Deucher 					RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET);
68909fb8bd1SAlex Deucher 			}
690bcc1c2a1SAlex Deucher 		if (reg & EVERGREEN_CRTC_MASTER_EN)
691bcc1c2a1SAlex Deucher 			return true;
692bcc1c2a1SAlex Deucher 	} else if (ASIC_IS_AVIVO(rdev)) {
693771fe6b9SJerome Glisse 		reg = RREG32(AVIVO_D1CRTC_CONTROL) |
694771fe6b9SJerome Glisse 		      RREG32(AVIVO_D2CRTC_CONTROL);
695771fe6b9SJerome Glisse 		if (reg & AVIVO_CRTC_EN) {
696771fe6b9SJerome Glisse 			return true;
697771fe6b9SJerome Glisse 		}
698771fe6b9SJerome Glisse 	} else {
699771fe6b9SJerome Glisse 		reg = RREG32(RADEON_CRTC_GEN_CNTL) |
700771fe6b9SJerome Glisse 		      RREG32(RADEON_CRTC2_GEN_CNTL);
701771fe6b9SJerome Glisse 		if (reg & RADEON_CRTC_EN) {
702771fe6b9SJerome Glisse 			return true;
703771fe6b9SJerome Glisse 		}
704771fe6b9SJerome Glisse 	}
705771fe6b9SJerome Glisse 
7062cf3a4fcSAlex Deucher check_memsize:
707771fe6b9SJerome Glisse 	/* then check MEM_SIZE, in case the crtcs are off */
708771fe6b9SJerome Glisse 	if (rdev->family >= CHIP_R600)
709771fe6b9SJerome Glisse 		reg = RREG32(R600_CONFIG_MEMSIZE);
710771fe6b9SJerome Glisse 	else
711771fe6b9SJerome Glisse 		reg = RREG32(RADEON_CONFIG_MEMSIZE);
712771fe6b9SJerome Glisse 
713771fe6b9SJerome Glisse 	if (reg)
714771fe6b9SJerome Glisse 		return true;
715771fe6b9SJerome Glisse 
716771fe6b9SJerome Glisse 	return false;
717771fe6b9SJerome Glisse 
718771fe6b9SJerome Glisse }
719771fe6b9SJerome Glisse 
7200c195119SAlex Deucher /**
7210c195119SAlex Deucher  * radeon_update_bandwidth_info - update display bandwidth params
7220c195119SAlex Deucher  *
7230c195119SAlex Deucher  * @rdev: radeon_device pointer
7240c195119SAlex Deucher  *
7250c195119SAlex Deucher  * Used when sclk/mclk are switched or display modes are set.
7260c195119SAlex Deucher  * params are used to calculate display watermarks (all asics)
7270c195119SAlex Deucher  */
728f47299c5SAlex Deucher void radeon_update_bandwidth_info(struct radeon_device *rdev)
729f47299c5SAlex Deucher {
730f47299c5SAlex Deucher 	fixed20_12 a;
7318807286eSAlex Deucher 	u32 sclk = rdev->pm.current_sclk;
7328807286eSAlex Deucher 	u32 mclk = rdev->pm.current_mclk;
733f47299c5SAlex Deucher 
7348807286eSAlex Deucher 	/* sclk/mclk in Mhz */
73568adac5eSBen Skeggs 	a.full = dfixed_const(100);
73668adac5eSBen Skeggs 	rdev->pm.sclk.full = dfixed_const(sclk);
73768adac5eSBen Skeggs 	rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a);
73868adac5eSBen Skeggs 	rdev->pm.mclk.full = dfixed_const(mclk);
73968adac5eSBen Skeggs 	rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a);
740f47299c5SAlex Deucher 
7418807286eSAlex Deucher 	if (rdev->flags & RADEON_IS_IGP) {
74268adac5eSBen Skeggs 		a.full = dfixed_const(16);
743f47299c5SAlex Deucher 		/* core_bandwidth = sclk(Mhz) * 16 */
74468adac5eSBen Skeggs 		rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a);
745f47299c5SAlex Deucher 	}
746f47299c5SAlex Deucher }
747f47299c5SAlex Deucher 
7480c195119SAlex Deucher /**
7490c195119SAlex Deucher  * radeon_boot_test_post_card - check and possibly initialize the hw
7500c195119SAlex Deucher  *
7510c195119SAlex Deucher  * @rdev: radeon_device pointer
7520c195119SAlex Deucher  *
7530c195119SAlex Deucher  * Check if the asic is initialized and if not, attempt to initialize
7540c195119SAlex Deucher  * it (all asics).
7550c195119SAlex Deucher  * Returns true if initialized or false if not.
7560c195119SAlex Deucher  */
75772542d77SDave Airlie bool radeon_boot_test_post_card(struct radeon_device *rdev)
75872542d77SDave Airlie {
75972542d77SDave Airlie 	if (radeon_card_posted(rdev))
76072542d77SDave Airlie 		return true;
76172542d77SDave Airlie 
76272542d77SDave Airlie 	if (rdev->bios) {
76372542d77SDave Airlie 		DRM_INFO("GPU not posted. posting now...\n");
76472542d77SDave Airlie 		if (rdev->is_atom_bios)
76572542d77SDave Airlie 			atom_asic_init(rdev->mode_info.atom_context);
76672542d77SDave Airlie 		else
76772542d77SDave Airlie 			radeon_combios_asic_init(rdev->ddev);
76872542d77SDave Airlie 		return true;
76972542d77SDave Airlie 	} else {
77072542d77SDave Airlie 		dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
77172542d77SDave Airlie 		return false;
77272542d77SDave Airlie 	}
77372542d77SDave Airlie }
77472542d77SDave Airlie 
7750c195119SAlex Deucher /**
7760c195119SAlex Deucher  * radeon_dummy_page_init - init dummy page used by the driver
7770c195119SAlex Deucher  *
7780c195119SAlex Deucher  * @rdev: radeon_device pointer
7790c195119SAlex Deucher  *
7800c195119SAlex Deucher  * Allocate the dummy page used by the driver (all asics).
7810c195119SAlex Deucher  * This dummy page is used by the driver as a filler for gart entries
7820c195119SAlex Deucher  * when pages are taken out of the GART
7830c195119SAlex Deucher  * Returns 0 on sucess, -ENOMEM on failure.
7840c195119SAlex Deucher  */
7853ce0a23dSJerome Glisse int radeon_dummy_page_init(struct radeon_device *rdev)
7863ce0a23dSJerome Glisse {
78782568565SDave Airlie 	if (rdev->dummy_page.page)
78882568565SDave Airlie 		return 0;
7893ce0a23dSJerome Glisse 	rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
7903ce0a23dSJerome Glisse 	if (rdev->dummy_page.page == NULL)
7913ce0a23dSJerome Glisse 		return -ENOMEM;
7923ce0a23dSJerome Glisse 	rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page,
7933ce0a23dSJerome Glisse 					0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
794a30f6fb7SBenjamin Herrenschmidt 	if (pci_dma_mapping_error(rdev->pdev, rdev->dummy_page.addr)) {
795a30f6fb7SBenjamin Herrenschmidt 		dev_err(&rdev->pdev->dev, "Failed to DMA MAP the dummy page\n");
7963ce0a23dSJerome Glisse 		__free_page(rdev->dummy_page.page);
7973ce0a23dSJerome Glisse 		rdev->dummy_page.page = NULL;
7983ce0a23dSJerome Glisse 		return -ENOMEM;
7993ce0a23dSJerome Glisse 	}
800cb658906SMichel Dänzer 	rdev->dummy_page.entry = radeon_gart_get_page_entry(rdev->dummy_page.addr,
801cb658906SMichel Dänzer 							    RADEON_GART_PAGE_DUMMY);
8023ce0a23dSJerome Glisse 	return 0;
8033ce0a23dSJerome Glisse }
8043ce0a23dSJerome Glisse 
8050c195119SAlex Deucher /**
8060c195119SAlex Deucher  * radeon_dummy_page_fini - free dummy page used by the driver
8070c195119SAlex Deucher  *
8080c195119SAlex Deucher  * @rdev: radeon_device pointer
8090c195119SAlex Deucher  *
8100c195119SAlex Deucher  * Frees the dummy page used by the driver (all asics).
8110c195119SAlex Deucher  */
8123ce0a23dSJerome Glisse void radeon_dummy_page_fini(struct radeon_device *rdev)
8133ce0a23dSJerome Glisse {
8143ce0a23dSJerome Glisse 	if (rdev->dummy_page.page == NULL)
8153ce0a23dSJerome Glisse 		return;
8163ce0a23dSJerome Glisse 	pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
8173ce0a23dSJerome Glisse 			PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
8183ce0a23dSJerome Glisse 	__free_page(rdev->dummy_page.page);
8193ce0a23dSJerome Glisse 	rdev->dummy_page.page = NULL;
8203ce0a23dSJerome Glisse }
8213ce0a23dSJerome Glisse 
822771fe6b9SJerome Glisse 
823771fe6b9SJerome Glisse /* ATOM accessor methods */
8240c195119SAlex Deucher /*
8250c195119SAlex Deucher  * ATOM is an interpreted byte code stored in tables in the vbios.  The
8260c195119SAlex Deucher  * driver registers callbacks to access registers and the interpreter
8270c195119SAlex Deucher  * in the driver parses the tables and executes then to program specific
8280c195119SAlex Deucher  * actions (set display modes, asic init, etc.).  See radeon_atombios.c,
8290c195119SAlex Deucher  * atombios.h, and atom.c
8300c195119SAlex Deucher  */
8310c195119SAlex Deucher 
8320c195119SAlex Deucher /**
8330c195119SAlex Deucher  * cail_pll_read - read PLL register
8340c195119SAlex Deucher  *
8350c195119SAlex Deucher  * @info: atom card_info pointer
8360c195119SAlex Deucher  * @reg: PLL register offset
8370c195119SAlex Deucher  *
8380c195119SAlex Deucher  * Provides a PLL register accessor for the atom interpreter (r4xx+).
8390c195119SAlex Deucher  * Returns the value of the PLL register.
8400c195119SAlex Deucher  */
841771fe6b9SJerome Glisse static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
842771fe6b9SJerome Glisse {
843771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
844771fe6b9SJerome Glisse 	uint32_t r;
845771fe6b9SJerome Glisse 
846771fe6b9SJerome Glisse 	r = rdev->pll_rreg(rdev, reg);
847771fe6b9SJerome Glisse 	return r;
848771fe6b9SJerome Glisse }
849771fe6b9SJerome Glisse 
8500c195119SAlex Deucher /**
8510c195119SAlex Deucher  * cail_pll_write - write PLL register
8520c195119SAlex Deucher  *
8530c195119SAlex Deucher  * @info: atom card_info pointer
8540c195119SAlex Deucher  * @reg: PLL register offset
8550c195119SAlex Deucher  * @val: value to write to the pll register
8560c195119SAlex Deucher  *
8570c195119SAlex Deucher  * Provides a PLL register accessor for the atom interpreter (r4xx+).
8580c195119SAlex Deucher  */
859771fe6b9SJerome Glisse static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
860771fe6b9SJerome Glisse {
861771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
862771fe6b9SJerome Glisse 
863771fe6b9SJerome Glisse 	rdev->pll_wreg(rdev, reg, val);
864771fe6b9SJerome Glisse }
865771fe6b9SJerome Glisse 
8660c195119SAlex Deucher /**
8670c195119SAlex Deucher  * cail_mc_read - read MC (Memory Controller) register
8680c195119SAlex Deucher  *
8690c195119SAlex Deucher  * @info: atom card_info pointer
8700c195119SAlex Deucher  * @reg: MC register offset
8710c195119SAlex Deucher  *
8720c195119SAlex Deucher  * Provides an MC register accessor for the atom interpreter (r4xx+).
8730c195119SAlex Deucher  * Returns the value of the MC register.
8740c195119SAlex Deucher  */
875771fe6b9SJerome Glisse static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
876771fe6b9SJerome Glisse {
877771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
878771fe6b9SJerome Glisse 	uint32_t r;
879771fe6b9SJerome Glisse 
880771fe6b9SJerome Glisse 	r = rdev->mc_rreg(rdev, reg);
881771fe6b9SJerome Glisse 	return r;
882771fe6b9SJerome Glisse }
883771fe6b9SJerome Glisse 
8840c195119SAlex Deucher /**
8850c195119SAlex Deucher  * cail_mc_write - write MC (Memory Controller) register
8860c195119SAlex Deucher  *
8870c195119SAlex Deucher  * @info: atom card_info pointer
8880c195119SAlex Deucher  * @reg: MC register offset
8890c195119SAlex Deucher  * @val: value to write to the pll register
8900c195119SAlex Deucher  *
8910c195119SAlex Deucher  * Provides a MC register accessor for the atom interpreter (r4xx+).
8920c195119SAlex Deucher  */
893771fe6b9SJerome Glisse static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
894771fe6b9SJerome Glisse {
895771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
896771fe6b9SJerome Glisse 
897771fe6b9SJerome Glisse 	rdev->mc_wreg(rdev, reg, val);
898771fe6b9SJerome Glisse }
899771fe6b9SJerome Glisse 
9000c195119SAlex Deucher /**
9010c195119SAlex Deucher  * cail_reg_write - write MMIO register
9020c195119SAlex Deucher  *
9030c195119SAlex Deucher  * @info: atom card_info pointer
9040c195119SAlex Deucher  * @reg: MMIO register offset
9050c195119SAlex Deucher  * @val: value to write to the pll register
9060c195119SAlex Deucher  *
9070c195119SAlex Deucher  * Provides a MMIO register accessor for the atom interpreter (r4xx+).
9080c195119SAlex Deucher  */
909771fe6b9SJerome Glisse static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
910771fe6b9SJerome Glisse {
911771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
912771fe6b9SJerome Glisse 
913771fe6b9SJerome Glisse 	WREG32(reg*4, val);
914771fe6b9SJerome Glisse }
915771fe6b9SJerome Glisse 
9160c195119SAlex Deucher /**
9170c195119SAlex Deucher  * cail_reg_read - read MMIO register
9180c195119SAlex Deucher  *
9190c195119SAlex Deucher  * @info: atom card_info pointer
9200c195119SAlex Deucher  * @reg: MMIO register offset
9210c195119SAlex Deucher  *
9220c195119SAlex Deucher  * Provides an MMIO register accessor for the atom interpreter (r4xx+).
9230c195119SAlex Deucher  * Returns the value of the MMIO register.
9240c195119SAlex Deucher  */
925771fe6b9SJerome Glisse static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
926771fe6b9SJerome Glisse {
927771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
928771fe6b9SJerome Glisse 	uint32_t r;
929771fe6b9SJerome Glisse 
930771fe6b9SJerome Glisse 	r = RREG32(reg*4);
931771fe6b9SJerome Glisse 	return r;
932771fe6b9SJerome Glisse }
933771fe6b9SJerome Glisse 
9340c195119SAlex Deucher /**
9350c195119SAlex Deucher  * cail_ioreg_write - write IO register
9360c195119SAlex Deucher  *
9370c195119SAlex Deucher  * @info: atom card_info pointer
9380c195119SAlex Deucher  * @reg: IO register offset
9390c195119SAlex Deucher  * @val: value to write to the pll register
9400c195119SAlex Deucher  *
9410c195119SAlex Deucher  * Provides a IO register accessor for the atom interpreter (r4xx+).
9420c195119SAlex Deucher  */
943351a52a2SAlex Deucher static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
944351a52a2SAlex Deucher {
945351a52a2SAlex Deucher 	struct radeon_device *rdev = info->dev->dev_private;
946351a52a2SAlex Deucher 
947351a52a2SAlex Deucher 	WREG32_IO(reg*4, val);
948351a52a2SAlex Deucher }
949351a52a2SAlex Deucher 
9500c195119SAlex Deucher /**
9510c195119SAlex Deucher  * cail_ioreg_read - read IO register
9520c195119SAlex Deucher  *
9530c195119SAlex Deucher  * @info: atom card_info pointer
9540c195119SAlex Deucher  * @reg: IO register offset
9550c195119SAlex Deucher  *
9560c195119SAlex Deucher  * Provides an IO register accessor for the atom interpreter (r4xx+).
9570c195119SAlex Deucher  * Returns the value of the IO register.
9580c195119SAlex Deucher  */
959351a52a2SAlex Deucher static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
960351a52a2SAlex Deucher {
961351a52a2SAlex Deucher 	struct radeon_device *rdev = info->dev->dev_private;
962351a52a2SAlex Deucher 	uint32_t r;
963351a52a2SAlex Deucher 
964351a52a2SAlex Deucher 	r = RREG32_IO(reg*4);
965351a52a2SAlex Deucher 	return r;
966351a52a2SAlex Deucher }
967351a52a2SAlex Deucher 
9680c195119SAlex Deucher /**
9690c195119SAlex Deucher  * radeon_atombios_init - init the driver info and callbacks for atombios
9700c195119SAlex Deucher  *
9710c195119SAlex Deucher  * @rdev: radeon_device pointer
9720c195119SAlex Deucher  *
9730c195119SAlex Deucher  * Initializes the driver info and register access callbacks for the
9740c195119SAlex Deucher  * ATOM interpreter (r4xx+).
9750c195119SAlex Deucher  * Returns 0 on sucess, -ENOMEM on failure.
9760c195119SAlex Deucher  * Called at driver startup.
9770c195119SAlex Deucher  */
978771fe6b9SJerome Glisse int radeon_atombios_init(struct radeon_device *rdev)
979771fe6b9SJerome Glisse {
98061c4b24bSMathias Fröhlich 	struct card_info *atom_card_info =
98161c4b24bSMathias Fröhlich 	    kzalloc(sizeof(struct card_info), GFP_KERNEL);
98261c4b24bSMathias Fröhlich 
98361c4b24bSMathias Fröhlich 	if (!atom_card_info)
98461c4b24bSMathias Fröhlich 		return -ENOMEM;
98561c4b24bSMathias Fröhlich 
98661c4b24bSMathias Fröhlich 	rdev->mode_info.atom_card_info = atom_card_info;
98761c4b24bSMathias Fröhlich 	atom_card_info->dev = rdev->ddev;
98861c4b24bSMathias Fröhlich 	atom_card_info->reg_read = cail_reg_read;
98961c4b24bSMathias Fröhlich 	atom_card_info->reg_write = cail_reg_write;
990351a52a2SAlex Deucher 	/* needed for iio ops */
991351a52a2SAlex Deucher 	if (rdev->rio_mem) {
992351a52a2SAlex Deucher 		atom_card_info->ioreg_read = cail_ioreg_read;
993351a52a2SAlex Deucher 		atom_card_info->ioreg_write = cail_ioreg_write;
994351a52a2SAlex Deucher 	} else {
995351a52a2SAlex Deucher 		DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
996351a52a2SAlex Deucher 		atom_card_info->ioreg_read = cail_reg_read;
997351a52a2SAlex Deucher 		atom_card_info->ioreg_write = cail_reg_write;
998351a52a2SAlex Deucher 	}
99961c4b24bSMathias Fröhlich 	atom_card_info->mc_read = cail_mc_read;
100061c4b24bSMathias Fröhlich 	atom_card_info->mc_write = cail_mc_write;
100161c4b24bSMathias Fröhlich 	atom_card_info->pll_read = cail_pll_read;
100261c4b24bSMathias Fröhlich 	atom_card_info->pll_write = cail_pll_write;
100361c4b24bSMathias Fröhlich 
100461c4b24bSMathias Fröhlich 	rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
10050e34d094STim Gardner 	if (!rdev->mode_info.atom_context) {
10060e34d094STim Gardner 		radeon_atombios_fini(rdev);
10070e34d094STim Gardner 		return -ENOMEM;
10080e34d094STim Gardner 	}
10090e34d094STim Gardner 
1010c31ad97fSRafał Miłecki 	mutex_init(&rdev->mode_info.atom_context->mutex);
10111c949842SDave Airlie 	mutex_init(&rdev->mode_info.atom_context->scratch_mutex);
1012771fe6b9SJerome Glisse 	radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
1013d904ef9bSDave Airlie 	atom_allocate_fb_scratch(rdev->mode_info.atom_context);
1014771fe6b9SJerome Glisse 	return 0;
1015771fe6b9SJerome Glisse }
1016771fe6b9SJerome Glisse 
10170c195119SAlex Deucher /**
10180c195119SAlex Deucher  * radeon_atombios_fini - free the driver info and callbacks for atombios
10190c195119SAlex Deucher  *
10200c195119SAlex Deucher  * @rdev: radeon_device pointer
10210c195119SAlex Deucher  *
10220c195119SAlex Deucher  * Frees the driver info and register access callbacks for the ATOM
10230c195119SAlex Deucher  * interpreter (r4xx+).
10240c195119SAlex Deucher  * Called at driver shutdown.
10250c195119SAlex Deucher  */
1026771fe6b9SJerome Glisse void radeon_atombios_fini(struct radeon_device *rdev)
1027771fe6b9SJerome Glisse {
10284a04a844SJerome Glisse 	if (rdev->mode_info.atom_context) {
1029d904ef9bSDave Airlie 		kfree(rdev->mode_info.atom_context->scratch);
10304a04a844SJerome Glisse 	}
10310e34d094STim Gardner 	kfree(rdev->mode_info.atom_context);
10320e34d094STim Gardner 	rdev->mode_info.atom_context = NULL;
103361c4b24bSMathias Fröhlich 	kfree(rdev->mode_info.atom_card_info);
10340e34d094STim Gardner 	rdev->mode_info.atom_card_info = NULL;
1035771fe6b9SJerome Glisse }
1036771fe6b9SJerome Glisse 
10370c195119SAlex Deucher /* COMBIOS */
10380c195119SAlex Deucher /*
10390c195119SAlex Deucher  * COMBIOS is the bios format prior to ATOM. It provides
10400c195119SAlex Deucher  * command tables similar to ATOM, but doesn't have a unified
10410c195119SAlex Deucher  * parser.  See radeon_combios.c
10420c195119SAlex Deucher  */
10430c195119SAlex Deucher 
10440c195119SAlex Deucher /**
10450c195119SAlex Deucher  * radeon_combios_init - init the driver info for combios
10460c195119SAlex Deucher  *
10470c195119SAlex Deucher  * @rdev: radeon_device pointer
10480c195119SAlex Deucher  *
10490c195119SAlex Deucher  * Initializes the driver info for combios (r1xx-r3xx).
10500c195119SAlex Deucher  * Returns 0 on sucess.
10510c195119SAlex Deucher  * Called at driver startup.
10520c195119SAlex Deucher  */
1053771fe6b9SJerome Glisse int radeon_combios_init(struct radeon_device *rdev)
1054771fe6b9SJerome Glisse {
1055771fe6b9SJerome Glisse 	radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
1056771fe6b9SJerome Glisse 	return 0;
1057771fe6b9SJerome Glisse }
1058771fe6b9SJerome Glisse 
10590c195119SAlex Deucher /**
10600c195119SAlex Deucher  * radeon_combios_fini - free the driver info for combios
10610c195119SAlex Deucher  *
10620c195119SAlex Deucher  * @rdev: radeon_device pointer
10630c195119SAlex Deucher  *
10640c195119SAlex Deucher  * Frees the driver info for combios (r1xx-r3xx).
10650c195119SAlex Deucher  * Called at driver shutdown.
10660c195119SAlex Deucher  */
1067771fe6b9SJerome Glisse void radeon_combios_fini(struct radeon_device *rdev)
1068771fe6b9SJerome Glisse {
1069771fe6b9SJerome Glisse }
1070771fe6b9SJerome Glisse 
10710c195119SAlex Deucher /* if we get transitioned to only one device, take VGA back */
10720c195119SAlex Deucher /**
10730c195119SAlex Deucher  * radeon_vga_set_decode - enable/disable vga decode
10740c195119SAlex Deucher  *
10750c195119SAlex Deucher  * @cookie: radeon_device pointer
10760c195119SAlex Deucher  * @state: enable/disable vga decode
10770c195119SAlex Deucher  *
10780c195119SAlex Deucher  * Enable/disable vga decode (all asics).
10790c195119SAlex Deucher  * Returns VGA resource flags.
10800c195119SAlex Deucher  */
108128d52043SDave Airlie static unsigned int radeon_vga_set_decode(void *cookie, bool state)
108228d52043SDave Airlie {
108328d52043SDave Airlie 	struct radeon_device *rdev = cookie;
108428d52043SDave Airlie 	radeon_vga_set_state(rdev, state);
108528d52043SDave Airlie 	if (state)
108628d52043SDave Airlie 		return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
108728d52043SDave Airlie 		       VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
108828d52043SDave Airlie 	else
108928d52043SDave Airlie 		return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
109028d52043SDave Airlie }
1091c1176d6fSDave Airlie 
10920c195119SAlex Deucher /**
10931bcb04f7SChristian König  * radeon_check_pot_argument - check that argument is a power of two
10941bcb04f7SChristian König  *
10951bcb04f7SChristian König  * @arg: value to check
10961bcb04f7SChristian König  *
10971bcb04f7SChristian König  * Validates that a certain argument is a power of two (all asics).
10981bcb04f7SChristian König  * Returns true if argument is valid.
10991bcb04f7SChristian König  */
11001bcb04f7SChristian König static bool radeon_check_pot_argument(int arg)
11011bcb04f7SChristian König {
11021bcb04f7SChristian König 	return (arg & (arg - 1)) == 0;
11031bcb04f7SChristian König }
11041bcb04f7SChristian König 
11051bcb04f7SChristian König /**
11065e3c4f90SGrigori Goronzy  * Determine a sensible default GART size according to ASIC family.
11075e3c4f90SGrigori Goronzy  *
11085e3c4f90SGrigori Goronzy  * @family ASIC family name
11095e3c4f90SGrigori Goronzy  */
11105e3c4f90SGrigori Goronzy static int radeon_gart_size_auto(enum radeon_family family)
11115e3c4f90SGrigori Goronzy {
11125e3c4f90SGrigori Goronzy 	/* default to a larger gart size on newer asics */
11135e3c4f90SGrigori Goronzy 	if (family >= CHIP_TAHITI)
11145e3c4f90SGrigori Goronzy 		return 2048;
11155e3c4f90SGrigori Goronzy 	else if (family >= CHIP_RV770)
11165e3c4f90SGrigori Goronzy 		return 1024;
11175e3c4f90SGrigori Goronzy 	else
11185e3c4f90SGrigori Goronzy 		return 512;
11195e3c4f90SGrigori Goronzy }
11205e3c4f90SGrigori Goronzy 
11215e3c4f90SGrigori Goronzy /**
11220c195119SAlex Deucher  * radeon_check_arguments - validate module params
11230c195119SAlex Deucher  *
11240c195119SAlex Deucher  * @rdev: radeon_device pointer
11250c195119SAlex Deucher  *
11260c195119SAlex Deucher  * Validates certain module parameters and updates
11270c195119SAlex Deucher  * the associated values used by the driver (all asics).
11280c195119SAlex Deucher  */
11291109ca09SLauri Kasanen static void radeon_check_arguments(struct radeon_device *rdev)
113036421338SJerome Glisse {
113136421338SJerome Glisse 	/* vramlimit must be a power of two */
11321bcb04f7SChristian König 	if (!radeon_check_pot_argument(radeon_vram_limit)) {
113336421338SJerome Glisse 		dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n",
113436421338SJerome Glisse 				radeon_vram_limit);
113536421338SJerome Glisse 		radeon_vram_limit = 0;
113636421338SJerome Glisse 	}
11371bcb04f7SChristian König 
1138edcd26e8SAlex Deucher 	if (radeon_gart_size == -1) {
11395e3c4f90SGrigori Goronzy 		radeon_gart_size = radeon_gart_size_auto(rdev->family);
1140edcd26e8SAlex Deucher 	}
114136421338SJerome Glisse 	/* gtt size must be power of two and greater or equal to 32M */
11421bcb04f7SChristian König 	if (radeon_gart_size < 32) {
1143edcd26e8SAlex Deucher 		dev_warn(rdev->dev, "gart size (%d) too small\n",
114436421338SJerome Glisse 				radeon_gart_size);
11455e3c4f90SGrigori Goronzy 		radeon_gart_size = radeon_gart_size_auto(rdev->family);
11461bcb04f7SChristian König 	} else if (!radeon_check_pot_argument(radeon_gart_size)) {
114736421338SJerome Glisse 		dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
114836421338SJerome Glisse 				radeon_gart_size);
11495e3c4f90SGrigori Goronzy 		radeon_gart_size = radeon_gart_size_auto(rdev->family);
115036421338SJerome Glisse 	}
11511bcb04f7SChristian König 	rdev->mc.gtt_size = (uint64_t)radeon_gart_size << 20;
11521bcb04f7SChristian König 
115336421338SJerome Glisse 	/* AGP mode can only be -1, 1, 2, 4, 8 */
115436421338SJerome Glisse 	switch (radeon_agpmode) {
115536421338SJerome Glisse 	case -1:
115636421338SJerome Glisse 	case 0:
115736421338SJerome Glisse 	case 1:
115836421338SJerome Glisse 	case 2:
115936421338SJerome Glisse 	case 4:
116036421338SJerome Glisse 	case 8:
116136421338SJerome Glisse 		break;
116236421338SJerome Glisse 	default:
116336421338SJerome Glisse 		dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
116436421338SJerome Glisse 				"-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
116536421338SJerome Glisse 		radeon_agpmode = 0;
116636421338SJerome Glisse 		break;
116736421338SJerome Glisse 	}
1168c1c44132SChristian König 
1169c1c44132SChristian König 	if (!radeon_check_pot_argument(radeon_vm_size)) {
1170c1c44132SChristian König 		dev_warn(rdev->dev, "VM size (%d) must be a power of 2\n",
1171c1c44132SChristian König 			 radeon_vm_size);
117220b2656dSChristian König 		radeon_vm_size = 4;
1173c1c44132SChristian König 	}
1174c1c44132SChristian König 
117520b2656dSChristian König 	if (radeon_vm_size < 1) {
117613c240efSAlexandre Demers 		dev_warn(rdev->dev, "VM size (%d) too small, min is 1GB\n",
1177c1c44132SChristian König 			 radeon_vm_size);
117820b2656dSChristian König 		radeon_vm_size = 4;
1179c1c44132SChristian König 	}
1180c1c44132SChristian König 
1181c1c44132SChristian König 	/*
1182c1c44132SChristian König 	 * Max GPUVM size for Cayman, SI and CI are 40 bits.
1183c1c44132SChristian König 	 */
118420b2656dSChristian König 	if (radeon_vm_size > 1024) {
118520b2656dSChristian König 		dev_warn(rdev->dev, "VM size (%d) too large, max is 1TB\n",
1186c1c44132SChristian König 			 radeon_vm_size);
118720b2656dSChristian König 		radeon_vm_size = 4;
1188c1c44132SChristian König 	}
11894510fb98SChristian König 
11904510fb98SChristian König 	/* defines number of bits in page table versus page directory,
11914510fb98SChristian König 	 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
11924510fb98SChristian König 	 * page table and the remaining bits are in the page directory */
1193dfc230f9SChristian König 	if (radeon_vm_block_size == -1) {
1194dfc230f9SChristian König 
1195dfc230f9SChristian König 		/* Total bits covered by PD + PTs */
11968e66e134SAlex Deucher 		unsigned bits = ilog2(radeon_vm_size) + 18;
1197dfc230f9SChristian König 
1198dfc230f9SChristian König 		/* Make sure the PD is 4K in size up to 8GB address space.
1199dfc230f9SChristian König 		   Above that split equal between PD and PTs */
1200dfc230f9SChristian König 		if (radeon_vm_size <= 8)
1201dfc230f9SChristian König 			radeon_vm_block_size = bits - 9;
1202dfc230f9SChristian König 		else
1203dfc230f9SChristian König 			radeon_vm_block_size = (bits + 3) / 2;
1204dfc230f9SChristian König 
1205dfc230f9SChristian König 	} else if (radeon_vm_block_size < 9) {
120620b2656dSChristian König 		dev_warn(rdev->dev, "VM page table size (%d) too small\n",
12074510fb98SChristian König 			 radeon_vm_block_size);
12084510fb98SChristian König 		radeon_vm_block_size = 9;
12094510fb98SChristian König 	}
12104510fb98SChristian König 
12114510fb98SChristian König 	if (radeon_vm_block_size > 24 ||
121220b2656dSChristian König 	    (radeon_vm_size * 1024) < (1ull << radeon_vm_block_size)) {
121320b2656dSChristian König 		dev_warn(rdev->dev, "VM page table size (%d) too large\n",
12144510fb98SChristian König 			 radeon_vm_block_size);
12154510fb98SChristian König 		radeon_vm_block_size = 9;
12164510fb98SChristian König 	}
121736421338SJerome Glisse }
121836421338SJerome Glisse 
12190c195119SAlex Deucher /**
12200c195119SAlex Deucher  * radeon_switcheroo_set_state - set switcheroo state
12210c195119SAlex Deucher  *
12220c195119SAlex Deucher  * @pdev: pci dev pointer
12238e5de1d8SLukas Wunner  * @state: vga_switcheroo state
12240c195119SAlex Deucher  *
12250c195119SAlex Deucher  * Callback for the switcheroo driver.  Suspends or resumes the
12260c195119SAlex Deucher  * the asics before or after it is powered up using ACPI methods.
12270c195119SAlex Deucher  */
12286a9ee8afSDave Airlie static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
12296a9ee8afSDave Airlie {
12306a9ee8afSDave Airlie 	struct drm_device *dev = pci_get_drvdata(pdev);
12314807c5a8SAlex Deucher 	struct radeon_device *rdev = dev->dev_private;
123210ebc0bcSDave Airlie 
123390c4cde9SAlex Deucher 	if (radeon_is_px(dev) && state == VGA_SWITCHEROO_OFF)
123410ebc0bcSDave Airlie 		return;
123510ebc0bcSDave Airlie 
12366a9ee8afSDave Airlie 	if (state == VGA_SWITCHEROO_ON) {
1237d1f9809eSMaarten Lankhorst 		unsigned d3_delay = dev->pdev->d3_delay;
1238d1f9809eSMaarten Lankhorst 
12396a9ee8afSDave Airlie 		printk(KERN_INFO "radeon: switched on\n");
12406a9ee8afSDave Airlie 		/* don't suspend or resume card normally */
12415bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1242d1f9809eSMaarten Lankhorst 
12434807c5a8SAlex Deucher 		if (d3_delay < 20 && (rdev->px_quirk_flags & RADEON_PX_QUIRK_LONG_WAKEUP))
1244d1f9809eSMaarten Lankhorst 			dev->pdev->d3_delay = 20;
1245d1f9809eSMaarten Lankhorst 
124610ebc0bcSDave Airlie 		radeon_resume_kms(dev, true, true);
1247d1f9809eSMaarten Lankhorst 
1248d1f9809eSMaarten Lankhorst 		dev->pdev->d3_delay = d3_delay;
1249d1f9809eSMaarten Lankhorst 
12505bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_ON;
1251fbf81762SDave Airlie 		drm_kms_helper_poll_enable(dev);
12526a9ee8afSDave Airlie 	} else {
12536a9ee8afSDave Airlie 		printk(KERN_INFO "radeon: switched off\n");
1254fbf81762SDave Airlie 		drm_kms_helper_poll_disable(dev);
12555bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1256274ad65cSJérome Glisse 		radeon_suspend_kms(dev, true, true, false);
12575bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_OFF;
12586a9ee8afSDave Airlie 	}
12596a9ee8afSDave Airlie }
12606a9ee8afSDave Airlie 
12610c195119SAlex Deucher /**
12620c195119SAlex Deucher  * radeon_switcheroo_can_switch - see if switcheroo state can change
12630c195119SAlex Deucher  *
12640c195119SAlex Deucher  * @pdev: pci dev pointer
12650c195119SAlex Deucher  *
12660c195119SAlex Deucher  * Callback for the switcheroo driver.  Check of the switcheroo
12670c195119SAlex Deucher  * state can be changed.
12680c195119SAlex Deucher  * Returns true if the state can be changed, false if not.
12690c195119SAlex Deucher  */
12706a9ee8afSDave Airlie static bool radeon_switcheroo_can_switch(struct pci_dev *pdev)
12716a9ee8afSDave Airlie {
12726a9ee8afSDave Airlie 	struct drm_device *dev = pci_get_drvdata(pdev);
12736a9ee8afSDave Airlie 
1274fc8fd40eSDaniel Vetter 	/*
1275fc8fd40eSDaniel Vetter 	 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1276fc8fd40eSDaniel Vetter 	 * locking inversion with the driver load path. And the access here is
1277fc8fd40eSDaniel Vetter 	 * completely racy anyway. So don't bother with locking for now.
1278fc8fd40eSDaniel Vetter 	 */
1279fc8fd40eSDaniel Vetter 	return dev->open_count == 0;
12806a9ee8afSDave Airlie }
12816a9ee8afSDave Airlie 
128226ec685fSTakashi Iwai static const struct vga_switcheroo_client_ops radeon_switcheroo_ops = {
128326ec685fSTakashi Iwai 	.set_gpu_state = radeon_switcheroo_set_state,
128426ec685fSTakashi Iwai 	.reprobe = NULL,
128526ec685fSTakashi Iwai 	.can_switch = radeon_switcheroo_can_switch,
128626ec685fSTakashi Iwai };
12876a9ee8afSDave Airlie 
12880c195119SAlex Deucher /**
12890c195119SAlex Deucher  * radeon_device_init - initialize the driver
12900c195119SAlex Deucher  *
12910c195119SAlex Deucher  * @rdev: radeon_device pointer
12920c195119SAlex Deucher  * @pdev: drm dev pointer
12930c195119SAlex Deucher  * @pdev: pci dev pointer
12940c195119SAlex Deucher  * @flags: driver flags
12950c195119SAlex Deucher  *
12960c195119SAlex Deucher  * Initializes the driver info and hw (all asics).
12970c195119SAlex Deucher  * Returns 0 for success or an error on failure.
12980c195119SAlex Deucher  * Called at driver startup.
12990c195119SAlex Deucher  */
1300771fe6b9SJerome Glisse int radeon_device_init(struct radeon_device *rdev,
1301771fe6b9SJerome Glisse 		       struct drm_device *ddev,
1302771fe6b9SJerome Glisse 		       struct pci_dev *pdev,
1303771fe6b9SJerome Glisse 		       uint32_t flags)
1304771fe6b9SJerome Glisse {
1305351a52a2SAlex Deucher 	int r, i;
1306ad49f501SDave Airlie 	int dma_bits;
130710ebc0bcSDave Airlie 	bool runtime = false;
1308771fe6b9SJerome Glisse 
1309771fe6b9SJerome Glisse 	rdev->shutdown = false;
13109f022ddfSJerome Glisse 	rdev->dev = &pdev->dev;
1311771fe6b9SJerome Glisse 	rdev->ddev = ddev;
1312771fe6b9SJerome Glisse 	rdev->pdev = pdev;
1313771fe6b9SJerome Glisse 	rdev->flags = flags;
1314771fe6b9SJerome Glisse 	rdev->family = flags & RADEON_FAMILY_MASK;
1315771fe6b9SJerome Glisse 	rdev->is_atom_bios = false;
1316771fe6b9SJerome Glisse 	rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
1317edcd26e8SAlex Deucher 	rdev->mc.gtt_size = 512 * 1024 * 1024;
1318733289c2SJerome Glisse 	rdev->accel_working = false;
13198b25ed34SAlex Deucher 	/* set up ring ids */
13208b25ed34SAlex Deucher 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
13218b25ed34SAlex Deucher 		rdev->ring[i].idx = i;
13228b25ed34SAlex Deucher 	}
1323954605caSMaarten Lankhorst 	rdev->fence_context = fence_context_alloc(RADEON_NUM_RINGS);
13241b5331d9SJerome Glisse 
1325fe0d36e0SAlex Deucher 	DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1326d522d9ccSThomas Reim 		 radeon_family_name[rdev->family], pdev->vendor, pdev->device,
1327fe0d36e0SAlex Deucher 		 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
13281b5331d9SJerome Glisse 
1329771fe6b9SJerome Glisse 	/* mutex initialization are all done here so we
1330771fe6b9SJerome Glisse 	 * can recall function without having locking issues */
1331d6999bc7SChristian König 	mutex_init(&rdev->ring_lock);
133240bacf16SAlex Deucher 	mutex_init(&rdev->dc_hw_i2c_mutex);
1333c20dc369SChristian Koenig 	atomic_set(&rdev->ih.lock, 0);
13344c788679SJerome Glisse 	mutex_init(&rdev->gem.mutex);
1335c913e23aSRafał Miłecki 	mutex_init(&rdev->pm.mutex);
13366759a0a7SMarek Olšák 	mutex_init(&rdev->gpu_clock_mutex);
1337f61d5b46SAlex Deucher 	mutex_init(&rdev->srbm_mutex);
13381c0a4625SOded Gabbay 	mutex_init(&rdev->grbm_idx_mutex);
1339db7fce39SChristian König 	init_rwsem(&rdev->pm.mclk_lock);
1340dee53e7fSJerome Glisse 	init_rwsem(&rdev->exclusive_lock);
134173a6d3fcSRafał Miłecki 	init_waitqueue_head(&rdev->irq.vblank_queue);
1342341cb9e4SChristian König 	mutex_init(&rdev->mn_lock);
1343341cb9e4SChristian König 	hash_init(rdev->mn_hash);
13441b9c3dd0SAlex Deucher 	r = radeon_gem_init(rdev);
13451b9c3dd0SAlex Deucher 	if (r)
13461b9c3dd0SAlex Deucher 		return r;
1347529364e0SChristian König 
1348c1c44132SChristian König 	radeon_check_arguments(rdev);
134923d4f1f2SAlex Deucher 	/* Adjust VM size here.
1350c1c44132SChristian König 	 * Max GPUVM size for cayman+ is 40 bits.
135123d4f1f2SAlex Deucher 	 */
135220b2656dSChristian König 	rdev->vm_manager.max_pfn = radeon_vm_size << 18;
1353771fe6b9SJerome Glisse 
13544aac0473SJerome Glisse 	/* Set asic functions */
13554aac0473SJerome Glisse 	r = radeon_asic_init(rdev);
135636421338SJerome Glisse 	if (r)
13574aac0473SJerome Glisse 		return r;
13584aac0473SJerome Glisse 
1359f95df9caSAlex Deucher 	/* all of the newer IGP chips have an internal gart
1360f95df9caSAlex Deucher 	 * However some rs4xx report as AGP, so remove that here.
1361f95df9caSAlex Deucher 	 */
1362f95df9caSAlex Deucher 	if ((rdev->family >= CHIP_RS400) &&
1363f95df9caSAlex Deucher 	    (rdev->flags & RADEON_IS_IGP)) {
1364f95df9caSAlex Deucher 		rdev->flags &= ~RADEON_IS_AGP;
1365f95df9caSAlex Deucher 	}
1366f95df9caSAlex Deucher 
136730256a3fSJerome Glisse 	if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
1368b574f251SJerome Glisse 		radeon_agp_disable(rdev);
1369771fe6b9SJerome Glisse 	}
1370771fe6b9SJerome Glisse 
13719ed8b1f9SAlex Deucher 	/* Set the internal MC address mask
13729ed8b1f9SAlex Deucher 	 * This is the max address of the GPU's
13739ed8b1f9SAlex Deucher 	 * internal address space.
13749ed8b1f9SAlex Deucher 	 */
13759ed8b1f9SAlex Deucher 	if (rdev->family >= CHIP_CAYMAN)
13769ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xffffffffffULL; /* 40 bit MC */
13779ed8b1f9SAlex Deucher 	else if (rdev->family >= CHIP_CEDAR)
13789ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xfffffffffULL; /* 36 bit MC */
13799ed8b1f9SAlex Deucher 	else
13809ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xffffffffULL; /* 32 bit MC */
13819ed8b1f9SAlex Deucher 
1382ad49f501SDave Airlie 	/* set DMA mask + need_dma32 flags.
1383ad49f501SDave Airlie 	 * PCIE - can handle 40-bits.
1384005a83f1SAlex Deucher 	 * IGP - can handle 40-bits
1385ad49f501SDave Airlie 	 * AGP - generally dma32 is safest
1386005a83f1SAlex Deucher 	 * PCI - dma32 for legacy pci gart, 40 bits on newer asics
1387ad49f501SDave Airlie 	 */
1388ad49f501SDave Airlie 	rdev->need_dma32 = false;
1389ad49f501SDave Airlie 	if (rdev->flags & RADEON_IS_AGP)
1390ad49f501SDave Airlie 		rdev->need_dma32 = true;
1391005a83f1SAlex Deucher 	if ((rdev->flags & RADEON_IS_PCI) &&
13924a2b6662SJerome Glisse 	    (rdev->family <= CHIP_RS740))
1393ad49f501SDave Airlie 		rdev->need_dma32 = true;
1394ad49f501SDave Airlie 
1395ad49f501SDave Airlie 	dma_bits = rdev->need_dma32 ? 32 : 40;
1396ad49f501SDave Airlie 	r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
1397771fe6b9SJerome Glisse 	if (r) {
139862fff811SDaniel Haid 		rdev->need_dma32 = true;
1399c52494f6SKonrad Rzeszutek Wilk 		dma_bits = 32;
1400771fe6b9SJerome Glisse 		printk(KERN_WARNING "radeon: No suitable DMA available.\n");
1401771fe6b9SJerome Glisse 	}
1402c52494f6SKonrad Rzeszutek Wilk 	r = pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
1403c52494f6SKonrad Rzeszutek Wilk 	if (r) {
1404c52494f6SKonrad Rzeszutek Wilk 		pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(32));
1405c52494f6SKonrad Rzeszutek Wilk 		printk(KERN_WARNING "radeon: No coherent DMA available.\n");
1406c52494f6SKonrad Rzeszutek Wilk 	}
1407771fe6b9SJerome Glisse 
1408771fe6b9SJerome Glisse 	/* Registers mapping */
1409771fe6b9SJerome Glisse 	/* TODO: block userspace mapping of io register */
14102c385151SDaniel Vetter 	spin_lock_init(&rdev->mmio_idx_lock);
1411fe78118cSAlex Deucher 	spin_lock_init(&rdev->smc_idx_lock);
14120a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pll_idx_lock);
14130a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->mc_idx_lock);
14140a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pcie_idx_lock);
14150a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pciep_idx_lock);
14160a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pif_idx_lock);
14170a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->cg_idx_lock);
14180a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->uvd_idx_lock);
14190a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->rcu_idx_lock);
14200a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->didt_idx_lock);
14210a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->end_idx_lock);
1422efad86dbSAlex Deucher 	if (rdev->family >= CHIP_BONAIRE) {
1423efad86dbSAlex Deucher 		rdev->rmmio_base = pci_resource_start(rdev->pdev, 5);
1424efad86dbSAlex Deucher 		rdev->rmmio_size = pci_resource_len(rdev->pdev, 5);
1425efad86dbSAlex Deucher 	} else {
142601d73a69SJordan Crouse 		rdev->rmmio_base = pci_resource_start(rdev->pdev, 2);
142701d73a69SJordan Crouse 		rdev->rmmio_size = pci_resource_len(rdev->pdev, 2);
1428efad86dbSAlex Deucher 	}
1429771fe6b9SJerome Glisse 	rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size);
1430771fe6b9SJerome Glisse 	if (rdev->rmmio == NULL) {
1431771fe6b9SJerome Glisse 		return -ENOMEM;
1432771fe6b9SJerome Glisse 	}
1433771fe6b9SJerome Glisse 	DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base);
1434771fe6b9SJerome Glisse 	DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size);
1435771fe6b9SJerome Glisse 
143675efdee1SAlex Deucher 	/* doorbell bar mapping */
143775efdee1SAlex Deucher 	if (rdev->family >= CHIP_BONAIRE)
143875efdee1SAlex Deucher 		radeon_doorbell_init(rdev);
143975efdee1SAlex Deucher 
1440351a52a2SAlex Deucher 	/* io port mapping */
1441351a52a2SAlex Deucher 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1442351a52a2SAlex Deucher 		if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) {
1443351a52a2SAlex Deucher 			rdev->rio_mem_size = pci_resource_len(rdev->pdev, i);
1444351a52a2SAlex Deucher 			rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size);
1445351a52a2SAlex Deucher 			break;
1446351a52a2SAlex Deucher 		}
1447351a52a2SAlex Deucher 	}
1448351a52a2SAlex Deucher 	if (rdev->rio_mem == NULL)
1449351a52a2SAlex Deucher 		DRM_ERROR("Unable to find PCI I/O BAR\n");
1450351a52a2SAlex Deucher 
14514807c5a8SAlex Deucher 	if (rdev->flags & RADEON_IS_PX)
14524807c5a8SAlex Deucher 		radeon_device_handle_px_quirks(rdev);
14534807c5a8SAlex Deucher 
145428d52043SDave Airlie 	/* if we have > 1 VGA cards, then disable the radeon VGA resources */
145593239ea1SDave Airlie 	/* this will fail for cards that aren't VGA class devices, just
145693239ea1SDave Airlie 	 * ignore it */
145793239ea1SDave Airlie 	vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
145810ebc0bcSDave Airlie 
1459bfaddd9fSAlex Deucher 	if (rdev->flags & RADEON_IS_PX)
146010ebc0bcSDave Airlie 		runtime = true;
146110ebc0bcSDave Airlie 	vga_switcheroo_register_client(rdev->pdev, &radeon_switcheroo_ops, runtime);
146210ebc0bcSDave Airlie 	if (runtime)
146310ebc0bcSDave Airlie 		vga_switcheroo_init_domain_pm_ops(rdev->dev, &rdev->vga_pm_domain);
146428d52043SDave Airlie 
14653ce0a23dSJerome Glisse 	r = radeon_init(rdev);
1466b574f251SJerome Glisse 	if (r)
14672e97140dSAlex Deucher 		goto failed;
1468b1e3a6d1SMichel Dänzer 
1469409851f4SJerome Glisse 	r = radeon_gem_debugfs_init(rdev);
1470409851f4SJerome Glisse 	if (r) {
1471409851f4SJerome Glisse 		DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1472409851f4SJerome Glisse 	}
1473409851f4SJerome Glisse 
14749843ead0SDave Airlie 	r = radeon_mst_debugfs_init(rdev);
14759843ead0SDave Airlie 	if (r) {
14769843ead0SDave Airlie 		DRM_ERROR("registering mst debugfs failed (%d).\n", r);
14779843ead0SDave Airlie 	}
14789843ead0SDave Airlie 
1479b574f251SJerome Glisse 	if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
1480b574f251SJerome Glisse 		/* Acceleration not working on AGP card try again
1481b574f251SJerome Glisse 		 * with fallback to PCI or PCIE GART
1482b574f251SJerome Glisse 		 */
1483a2d07b74SJerome Glisse 		radeon_asic_reset(rdev);
1484b574f251SJerome Glisse 		radeon_fini(rdev);
1485b574f251SJerome Glisse 		radeon_agp_disable(rdev);
1486b574f251SJerome Glisse 		r = radeon_init(rdev);
14874aac0473SJerome Glisse 		if (r)
14882e97140dSAlex Deucher 			goto failed;
14893ce0a23dSJerome Glisse 	}
14906c7bcceaSAlex Deucher 
149113a7d299SChristian König 	r = radeon_ib_ring_tests(rdev);
149213a7d299SChristian König 	if (r)
149313a7d299SChristian König 		DRM_ERROR("ib ring test failed (%d).\n", r);
149413a7d299SChristian König 
14956dfd1972SJérôme Glisse 	/*
14966dfd1972SJérôme Glisse 	 * Turks/Thames GPU will freeze whole laptop if DPM is not restarted
14976dfd1972SJérôme Glisse 	 * after the CP ring have chew one packet at least. Hence here we stop
14986dfd1972SJérôme Glisse 	 * and restart DPM after the radeon_ib_ring_tests().
14996dfd1972SJérôme Glisse 	 */
15006dfd1972SJérôme Glisse 	if (rdev->pm.dpm_enabled &&
15016dfd1972SJérôme Glisse 	    (rdev->pm.pm_method == PM_METHOD_DPM) &&
15026dfd1972SJérôme Glisse 	    (rdev->family == CHIP_TURKS) &&
15036dfd1972SJérôme Glisse 	    (rdev->flags & RADEON_IS_MOBILITY)) {
15046dfd1972SJérôme Glisse 		mutex_lock(&rdev->pm.mutex);
15056dfd1972SJérôme Glisse 		radeon_dpm_disable(rdev);
15066dfd1972SJérôme Glisse 		radeon_dpm_enable(rdev);
15076dfd1972SJérôme Glisse 		mutex_unlock(&rdev->pm.mutex);
15086dfd1972SJérôme Glisse 	}
15096dfd1972SJérôme Glisse 
151060a7e396SChristian König 	if ((radeon_testing & 1)) {
15114a1132a0SAlex Deucher 		if (rdev->accel_working)
1512ecc0b326SMichel Dänzer 			radeon_test_moves(rdev);
15134a1132a0SAlex Deucher 		else
15144a1132a0SAlex Deucher 			DRM_INFO("radeon: acceleration disabled, skipping move tests\n");
1515ecc0b326SMichel Dänzer 	}
151660a7e396SChristian König 	if ((radeon_testing & 2)) {
15174a1132a0SAlex Deucher 		if (rdev->accel_working)
151860a7e396SChristian König 			radeon_test_syncing(rdev);
15194a1132a0SAlex Deucher 		else
15204a1132a0SAlex Deucher 			DRM_INFO("radeon: acceleration disabled, skipping sync tests\n");
152160a7e396SChristian König 	}
1522771fe6b9SJerome Glisse 	if (radeon_benchmarking) {
15234a1132a0SAlex Deucher 		if (rdev->accel_working)
1524638dd7dbSIlija Hadzic 			radeon_benchmark(rdev, radeon_benchmarking);
15254a1132a0SAlex Deucher 		else
15264a1132a0SAlex Deucher 			DRM_INFO("radeon: acceleration disabled, skipping benchmarks\n");
1527771fe6b9SJerome Glisse 	}
15286cf8a3f5SJerome Glisse 	return 0;
15292e97140dSAlex Deucher 
15302e97140dSAlex Deucher failed:
1531b8751946SLukas Wunner 	/* balance pm_runtime_get_sync() in radeon_driver_unload_kms() */
1532b8751946SLukas Wunner 	if (radeon_is_px(ddev))
1533b8751946SLukas Wunner 		pm_runtime_put_noidle(ddev->dev);
15342e97140dSAlex Deucher 	if (runtime)
15352e97140dSAlex Deucher 		vga_switcheroo_fini_domain_pm_ops(rdev->dev);
15362e97140dSAlex Deucher 	return r;
1537771fe6b9SJerome Glisse }
1538771fe6b9SJerome Glisse 
15394d8bf9aeSChristian König static void radeon_debugfs_remove_files(struct radeon_device *rdev);
15404d8bf9aeSChristian König 
15410c195119SAlex Deucher /**
15420c195119SAlex Deucher  * radeon_device_fini - tear down the driver
15430c195119SAlex Deucher  *
15440c195119SAlex Deucher  * @rdev: radeon_device pointer
15450c195119SAlex Deucher  *
15460c195119SAlex Deucher  * Tear down the driver info (all asics).
15470c195119SAlex Deucher  * Called at driver shutdown.
15480c195119SAlex Deucher  */
1549771fe6b9SJerome Glisse void radeon_device_fini(struct radeon_device *rdev)
1550771fe6b9SJerome Glisse {
1551771fe6b9SJerome Glisse 	DRM_INFO("radeon: finishing device.\n");
1552771fe6b9SJerome Glisse 	rdev->shutdown = true;
155390aca4d2SJerome Glisse 	/* evict vram memory */
155490aca4d2SJerome Glisse 	radeon_bo_evict_vram(rdev);
15553ce0a23dSJerome Glisse 	radeon_fini(rdev);
15566a9ee8afSDave Airlie 	vga_switcheroo_unregister_client(rdev->pdev);
15572e97140dSAlex Deucher 	if (rdev->flags & RADEON_IS_PX)
15582e97140dSAlex Deucher 		vga_switcheroo_fini_domain_pm_ops(rdev->dev);
1559c1176d6fSDave Airlie 	vga_client_register(rdev->pdev, NULL, NULL, NULL);
1560e0a2ca73SAlex Deucher 	if (rdev->rio_mem)
1561351a52a2SAlex Deucher 		pci_iounmap(rdev->pdev, rdev->rio_mem);
1562351a52a2SAlex Deucher 	rdev->rio_mem = NULL;
1563771fe6b9SJerome Glisse 	iounmap(rdev->rmmio);
1564771fe6b9SJerome Glisse 	rdev->rmmio = NULL;
156575efdee1SAlex Deucher 	if (rdev->family >= CHIP_BONAIRE)
156675efdee1SAlex Deucher 		radeon_doorbell_fini(rdev);
15674d8bf9aeSChristian König 	radeon_debugfs_remove_files(rdev);
1568771fe6b9SJerome Glisse }
1569771fe6b9SJerome Glisse 
1570771fe6b9SJerome Glisse 
1571771fe6b9SJerome Glisse /*
1572771fe6b9SJerome Glisse  * Suspend & resume.
1573771fe6b9SJerome Glisse  */
15740c195119SAlex Deucher /**
15750c195119SAlex Deucher  * radeon_suspend_kms - initiate device suspend
15760c195119SAlex Deucher  *
15770c195119SAlex Deucher  * @pdev: drm dev pointer
15780c195119SAlex Deucher  * @state: suspend state
15790c195119SAlex Deucher  *
15800c195119SAlex Deucher  * Puts the hw in the suspend state (all asics).
15810c195119SAlex Deucher  * Returns 0 for success or an error on failure.
15820c195119SAlex Deucher  * Called at driver suspend.
15830c195119SAlex Deucher  */
1584274ad65cSJérome Glisse int radeon_suspend_kms(struct drm_device *dev, bool suspend,
1585274ad65cSJérome Glisse 		       bool fbcon, bool freeze)
1586771fe6b9SJerome Glisse {
1587875c1866SDarren Jenkins 	struct radeon_device *rdev;
1588771fe6b9SJerome Glisse 	struct drm_crtc *crtc;
1589d8dcaa1dSAlex Deucher 	struct drm_connector *connector;
15907465280cSAlex Deucher 	int i, r;
1591771fe6b9SJerome Glisse 
1592875c1866SDarren Jenkins 	if (dev == NULL || dev->dev_private == NULL) {
1593771fe6b9SJerome Glisse 		return -ENODEV;
1594771fe6b9SJerome Glisse 	}
15957473e830SDave Airlie 
1596875c1866SDarren Jenkins 	rdev = dev->dev_private;
1597875c1866SDarren Jenkins 
1598f2aba352SAlex Deucher 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
15996a9ee8afSDave Airlie 		return 0;
1600d8dcaa1dSAlex Deucher 
160186698c20SSeth Forshee 	drm_kms_helper_poll_disable(dev);
160286698c20SSeth Forshee 
16036adaed5bSDaniel Vetter 	drm_modeset_lock_all(dev);
1604d8dcaa1dSAlex Deucher 	/* turn off display hw */
1605d8dcaa1dSAlex Deucher 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1606d8dcaa1dSAlex Deucher 		drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1607d8dcaa1dSAlex Deucher 	}
16086adaed5bSDaniel Vetter 	drm_modeset_unlock_all(dev);
1609d8dcaa1dSAlex Deucher 
1610f3cbb17bSGrigori Goronzy 	/* unpin the front buffers and cursors */
1611771fe6b9SJerome Glisse 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1612f3cbb17bSGrigori Goronzy 		struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1613f4510a27SMatt Roper 		struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->primary->fb);
16144c788679SJerome Glisse 		struct radeon_bo *robj;
1615771fe6b9SJerome Glisse 
1616f3cbb17bSGrigori Goronzy 		if (radeon_crtc->cursor_bo) {
1617f3cbb17bSGrigori Goronzy 			struct radeon_bo *robj = gem_to_radeon_bo(radeon_crtc->cursor_bo);
1618f3cbb17bSGrigori Goronzy 			r = radeon_bo_reserve(robj, false);
1619f3cbb17bSGrigori Goronzy 			if (r == 0) {
1620f3cbb17bSGrigori Goronzy 				radeon_bo_unpin(robj);
1621f3cbb17bSGrigori Goronzy 				radeon_bo_unreserve(robj);
1622f3cbb17bSGrigori Goronzy 			}
1623f3cbb17bSGrigori Goronzy 		}
1624f3cbb17bSGrigori Goronzy 
1625771fe6b9SJerome Glisse 		if (rfb == NULL || rfb->obj == NULL) {
1626771fe6b9SJerome Glisse 			continue;
1627771fe6b9SJerome Glisse 		}
16287e4d15d9SDaniel Vetter 		robj = gem_to_radeon_bo(rfb->obj);
162938651674SDave Airlie 		/* don't unpin kernel fb objects */
163038651674SDave Airlie 		if (!radeon_fbdev_robj_is_fb(rdev, robj)) {
16314c788679SJerome Glisse 			r = radeon_bo_reserve(robj, false);
163238651674SDave Airlie 			if (r == 0) {
16334c788679SJerome Glisse 				radeon_bo_unpin(robj);
16344c788679SJerome Glisse 				radeon_bo_unreserve(robj);
16354c788679SJerome Glisse 			}
1636771fe6b9SJerome Glisse 		}
1637771fe6b9SJerome Glisse 	}
1638771fe6b9SJerome Glisse 	/* evict vram memory */
16394c788679SJerome Glisse 	radeon_bo_evict_vram(rdev);
16408a47cc9eSChristian König 
1641771fe6b9SJerome Glisse 	/* wait for gpu to finish processing current batch */
16425f8f635eSJerome Glisse 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
164337615527SChristian König 		r = radeon_fence_wait_empty(rdev, i);
16445f8f635eSJerome Glisse 		if (r) {
16455f8f635eSJerome Glisse 			/* delay GPU reset to resume */
1646eb98c709SChristian König 			radeon_fence_driver_force_completion(rdev, i);
16475f8f635eSJerome Glisse 		}
16485f8f635eSJerome Glisse 	}
1649771fe6b9SJerome Glisse 
1650f657c2a7SYang Zhao 	radeon_save_bios_scratch_regs(rdev);
1651f657c2a7SYang Zhao 
16523ce0a23dSJerome Glisse 	radeon_suspend(rdev);
1653d4877cf2SAlex Deucher 	radeon_hpd_fini(rdev);
1654*ec9aaaffSAlex Deucher 	/* evict remaining vram memory
1655*ec9aaaffSAlex Deucher 	 * This second call to evict vram is to evict the gart page table
1656*ec9aaaffSAlex Deucher 	 * using the CPU.
1657*ec9aaaffSAlex Deucher 	 */
16584c788679SJerome Glisse 	radeon_bo_evict_vram(rdev);
1659771fe6b9SJerome Glisse 
166010b06122SJerome Glisse 	radeon_agp_suspend(rdev);
166110b06122SJerome Glisse 
1662771fe6b9SJerome Glisse 	pci_save_state(dev->pdev);
1663ccaa2c12SJérôme Glisse 	if (freeze && rdev->family >= CHIP_CEDAR) {
1664274ad65cSJérome Glisse 		rdev->asic->asic_reset(rdev, true);
1665274ad65cSJérome Glisse 		pci_restore_state(dev->pdev);
1666274ad65cSJérome Glisse 	} else if (suspend) {
1667771fe6b9SJerome Glisse 		/* Shut down the device */
1668771fe6b9SJerome Glisse 		pci_disable_device(dev->pdev);
1669771fe6b9SJerome Glisse 		pci_set_power_state(dev->pdev, PCI_D3hot);
1670771fe6b9SJerome Glisse 	}
167110ebc0bcSDave Airlie 
167210ebc0bcSDave Airlie 	if (fbcon) {
1673ac751efaSTorben Hohn 		console_lock();
167438651674SDave Airlie 		radeon_fbdev_set_suspend(rdev, 1);
1675ac751efaSTorben Hohn 		console_unlock();
167610ebc0bcSDave Airlie 	}
1677771fe6b9SJerome Glisse 	return 0;
1678771fe6b9SJerome Glisse }
1679771fe6b9SJerome Glisse 
16800c195119SAlex Deucher /**
16810c195119SAlex Deucher  * radeon_resume_kms - initiate device resume
16820c195119SAlex Deucher  *
16830c195119SAlex Deucher  * @pdev: drm dev pointer
16840c195119SAlex Deucher  *
16850c195119SAlex Deucher  * Bring the hw back to operating state (all asics).
16860c195119SAlex Deucher  * Returns 0 for success or an error on failure.
16870c195119SAlex Deucher  * Called at driver resume.
16880c195119SAlex Deucher  */
168910ebc0bcSDave Airlie int radeon_resume_kms(struct drm_device *dev, bool resume, bool fbcon)
1690771fe6b9SJerome Glisse {
169109bdf591SCedric Godin 	struct drm_connector *connector;
1692771fe6b9SJerome Glisse 	struct radeon_device *rdev = dev->dev_private;
1693f3cbb17bSGrigori Goronzy 	struct drm_crtc *crtc;
169404eb2206SChristian König 	int r;
1695771fe6b9SJerome Glisse 
1696f2aba352SAlex Deucher 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
16976a9ee8afSDave Airlie 		return 0;
16986a9ee8afSDave Airlie 
169910ebc0bcSDave Airlie 	if (fbcon) {
1700ac751efaSTorben Hohn 		console_lock();
170110ebc0bcSDave Airlie 	}
17027473e830SDave Airlie 	if (resume) {
1703771fe6b9SJerome Glisse 		pci_set_power_state(dev->pdev, PCI_D0);
1704771fe6b9SJerome Glisse 		pci_restore_state(dev->pdev);
1705771fe6b9SJerome Glisse 		if (pci_enable_device(dev->pdev)) {
170610ebc0bcSDave Airlie 			if (fbcon)
1707ac751efaSTorben Hohn 				console_unlock();
1708771fe6b9SJerome Glisse 			return -1;
1709771fe6b9SJerome Glisse 		}
17107473e830SDave Airlie 	}
17110ebf1717SDave Airlie 	/* resume AGP if in use */
17120ebf1717SDave Airlie 	radeon_agp_resume(rdev);
17133ce0a23dSJerome Glisse 	radeon_resume(rdev);
171404eb2206SChristian König 
171504eb2206SChristian König 	r = radeon_ib_ring_tests(rdev);
171604eb2206SChristian König 	if (r)
171704eb2206SChristian König 		DRM_ERROR("ib ring test failed (%d).\n", r);
171804eb2206SChristian König 
1719bc6a6295SAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
17206c7bcceaSAlex Deucher 		/* do dpm late init */
17216c7bcceaSAlex Deucher 		r = radeon_pm_late_init(rdev);
17226c7bcceaSAlex Deucher 		if (r) {
17236c7bcceaSAlex Deucher 			rdev->pm.dpm_enabled = false;
17246c7bcceaSAlex Deucher 			DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
17256c7bcceaSAlex Deucher 		}
1726bc6a6295SAlex Deucher 	} else {
1727bc6a6295SAlex Deucher 		/* resume old pm late */
1728bc6a6295SAlex Deucher 		radeon_pm_resume(rdev);
17296c7bcceaSAlex Deucher 	}
17306c7bcceaSAlex Deucher 
1731f657c2a7SYang Zhao 	radeon_restore_bios_scratch_regs(rdev);
173209bdf591SCedric Godin 
1733f3cbb17bSGrigori Goronzy 	/* pin cursors */
1734f3cbb17bSGrigori Goronzy 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1735f3cbb17bSGrigori Goronzy 		struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1736f3cbb17bSGrigori Goronzy 
1737f3cbb17bSGrigori Goronzy 		if (radeon_crtc->cursor_bo) {
1738f3cbb17bSGrigori Goronzy 			struct radeon_bo *robj = gem_to_radeon_bo(radeon_crtc->cursor_bo);
1739f3cbb17bSGrigori Goronzy 			r = radeon_bo_reserve(robj, false);
1740f3cbb17bSGrigori Goronzy 			if (r == 0) {
1741f3cbb17bSGrigori Goronzy 				/* Only 27 bit offset for legacy cursor */
1742f3cbb17bSGrigori Goronzy 				r = radeon_bo_pin_restricted(robj,
1743f3cbb17bSGrigori Goronzy 							     RADEON_GEM_DOMAIN_VRAM,
1744f3cbb17bSGrigori Goronzy 							     ASIC_IS_AVIVO(rdev) ?
1745f3cbb17bSGrigori Goronzy 							     0 : 1 << 27,
1746f3cbb17bSGrigori Goronzy 							     &radeon_crtc->cursor_addr);
1747f3cbb17bSGrigori Goronzy 				if (r != 0)
1748f3cbb17bSGrigori Goronzy 					DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
1749f3cbb17bSGrigori Goronzy 				radeon_bo_unreserve(robj);
1750f3cbb17bSGrigori Goronzy 			}
1751f3cbb17bSGrigori Goronzy 		}
1752f3cbb17bSGrigori Goronzy 	}
1753f3cbb17bSGrigori Goronzy 
17543fa47d9eSAlex Deucher 	/* init dig PHYs, disp eng pll */
17553fa47d9eSAlex Deucher 	if (rdev->is_atom_bios) {
1756ac89af1eSAlex Deucher 		radeon_atom_encoder_init(rdev);
1757f3f1f03eSAlex Deucher 		radeon_atom_disp_eng_pll_init(rdev);
1758bced76f2SAlex Deucher 		/* turn on the BL */
1759bced76f2SAlex Deucher 		if (rdev->mode_info.bl_encoder) {
1760bced76f2SAlex Deucher 			u8 bl_level = radeon_get_backlight_level(rdev,
1761bced76f2SAlex Deucher 								 rdev->mode_info.bl_encoder);
1762bced76f2SAlex Deucher 			radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
1763bced76f2SAlex Deucher 						   bl_level);
1764bced76f2SAlex Deucher 		}
17653fa47d9eSAlex Deucher 	}
1766d4877cf2SAlex Deucher 	/* reset hpd state */
1767d4877cf2SAlex Deucher 	radeon_hpd_init(rdev);
1768771fe6b9SJerome Glisse 	/* blat the mode back in */
1769ec9954fcSDave Airlie 	if (fbcon) {
1770771fe6b9SJerome Glisse 		drm_helper_resume_force_mode(dev);
1771a93f344dSAlex Deucher 		/* turn on display hw */
17726adaed5bSDaniel Vetter 		drm_modeset_lock_all(dev);
1773a93f344dSAlex Deucher 		list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1774a93f344dSAlex Deucher 			drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
1775a93f344dSAlex Deucher 		}
17766adaed5bSDaniel Vetter 		drm_modeset_unlock_all(dev);
1777ec9954fcSDave Airlie 	}
177886698c20SSeth Forshee 
177986698c20SSeth Forshee 	drm_kms_helper_poll_enable(dev);
178018ee37a4SDaniel Vetter 
17813640da2fSAlex Deucher 	/* set the power state here in case we are a PX system or headless */
17823640da2fSAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled)
17833640da2fSAlex Deucher 		radeon_pm_compute_clocks(rdev);
17843640da2fSAlex Deucher 
178518ee37a4SDaniel Vetter 	if (fbcon) {
178618ee37a4SDaniel Vetter 		radeon_fbdev_set_suspend(rdev, 0);
178718ee37a4SDaniel Vetter 		console_unlock();
178818ee37a4SDaniel Vetter 	}
178918ee37a4SDaniel Vetter 
1790771fe6b9SJerome Glisse 	return 0;
1791771fe6b9SJerome Glisse }
1792771fe6b9SJerome Glisse 
17930c195119SAlex Deucher /**
17940c195119SAlex Deucher  * radeon_gpu_reset - reset the asic
17950c195119SAlex Deucher  *
17960c195119SAlex Deucher  * @rdev: radeon device pointer
17970c195119SAlex Deucher  *
17980c195119SAlex Deucher  * Attempt the reset the GPU if it has hung (all asics).
17990c195119SAlex Deucher  * Returns 0 for success or an error on failure.
18000c195119SAlex Deucher  */
180190aca4d2SJerome Glisse int radeon_gpu_reset(struct radeon_device *rdev)
180290aca4d2SJerome Glisse {
180355d7c221SChristian König 	unsigned ring_sizes[RADEON_NUM_RINGS];
180455d7c221SChristian König 	uint32_t *ring_data[RADEON_NUM_RINGS];
180555d7c221SChristian König 
180655d7c221SChristian König 	bool saved = false;
180755d7c221SChristian König 
180855d7c221SChristian König 	int i, r;
18098fd1b84cSDave Airlie 	int resched;
181090aca4d2SJerome Glisse 
1811dee53e7fSJerome Glisse 	down_write(&rdev->exclusive_lock);
1812f9eaf9aeSChristian König 
1813f9eaf9aeSChristian König 	if (!rdev->needs_reset) {
1814f9eaf9aeSChristian König 		up_write(&rdev->exclusive_lock);
1815f9eaf9aeSChristian König 		return 0;
1816f9eaf9aeSChristian König 	}
1817f9eaf9aeSChristian König 
181872b9076bSMarek Olšák 	atomic_inc(&rdev->gpu_reset_counter);
181972b9076bSMarek Olšák 
182090aca4d2SJerome Glisse 	radeon_save_bios_scratch_regs(rdev);
18218fd1b84cSDave Airlie 	/* block TTM */
18228fd1b84cSDave Airlie 	resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
182390aca4d2SJerome Glisse 	radeon_suspend(rdev);
182473ef0e0dSAlex Deucher 	radeon_hpd_fini(rdev);
182590aca4d2SJerome Glisse 
182655d7c221SChristian König 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
182755d7c221SChristian König 		ring_sizes[i] = radeon_ring_backup(rdev, &rdev->ring[i],
182855d7c221SChristian König 						   &ring_data[i]);
182955d7c221SChristian König 		if (ring_sizes[i]) {
183055d7c221SChristian König 			saved = true;
183155d7c221SChristian König 			dev_info(rdev->dev, "Saved %d dwords of commands "
183255d7c221SChristian König 				 "on ring %d.\n", ring_sizes[i], i);
183355d7c221SChristian König 		}
183455d7c221SChristian König 	}
183555d7c221SChristian König 
183690aca4d2SJerome Glisse 	r = radeon_asic_reset(rdev);
183790aca4d2SJerome Glisse 	if (!r) {
183855d7c221SChristian König 		dev_info(rdev->dev, "GPU reset succeeded, trying to resume\n");
183990aca4d2SJerome Glisse 		radeon_resume(rdev);
184055d7c221SChristian König 	}
184104eb2206SChristian König 
184290aca4d2SJerome Glisse 	radeon_restore_bios_scratch_regs(rdev);
184355d7c221SChristian König 
184455d7c221SChristian König 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
18459bb39ff4SMaarten Lankhorst 		if (!r && ring_data[i]) {
184655d7c221SChristian König 			radeon_ring_restore(rdev, &rdev->ring[i],
184755d7c221SChristian König 					    ring_sizes[i], ring_data[i]);
184855d7c221SChristian König 		} else {
1849eb98c709SChristian König 			radeon_fence_driver_force_completion(rdev, i);
185055d7c221SChristian König 			kfree(ring_data[i]);
185155d7c221SChristian König 		}
185255d7c221SChristian König 	}
185355d7c221SChristian König 
1854c940b447SAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
1855c940b447SAlex Deucher 		/* do dpm late init */
1856c940b447SAlex Deucher 		r = radeon_pm_late_init(rdev);
1857c940b447SAlex Deucher 		if (r) {
1858c940b447SAlex Deucher 			rdev->pm.dpm_enabled = false;
1859c940b447SAlex Deucher 			DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
1860c940b447SAlex Deucher 		}
1861c940b447SAlex Deucher 	} else {
1862c940b447SAlex Deucher 		/* resume old pm late */
186395f59509SAlex Deucher 		radeon_pm_resume(rdev);
1864c940b447SAlex Deucher 	}
1865c940b447SAlex Deucher 
186673ef0e0dSAlex Deucher 	/* init dig PHYs, disp eng pll */
186773ef0e0dSAlex Deucher 	if (rdev->is_atom_bios) {
186873ef0e0dSAlex Deucher 		radeon_atom_encoder_init(rdev);
186973ef0e0dSAlex Deucher 		radeon_atom_disp_eng_pll_init(rdev);
187073ef0e0dSAlex Deucher 		/* turn on the BL */
187173ef0e0dSAlex Deucher 		if (rdev->mode_info.bl_encoder) {
187273ef0e0dSAlex Deucher 			u8 bl_level = radeon_get_backlight_level(rdev,
187373ef0e0dSAlex Deucher 								 rdev->mode_info.bl_encoder);
187473ef0e0dSAlex Deucher 			radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
187573ef0e0dSAlex Deucher 						   bl_level);
187673ef0e0dSAlex Deucher 		}
187773ef0e0dSAlex Deucher 	}
187873ef0e0dSAlex Deucher 	/* reset hpd state */
187973ef0e0dSAlex Deucher 	radeon_hpd_init(rdev);
188073ef0e0dSAlex Deucher 
18819bb39ff4SMaarten Lankhorst 	ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
18823c036389SChristian König 
18833c036389SChristian König 	rdev->in_reset = true;
18843c036389SChristian König 	rdev->needs_reset = false;
18853c036389SChristian König 
18869bb39ff4SMaarten Lankhorst 	downgrade_write(&rdev->exclusive_lock);
18879bb39ff4SMaarten Lankhorst 
1888d3493574SJerome Glisse 	drm_helper_resume_force_mode(rdev->ddev);
1889d3493574SJerome Glisse 
1890c940b447SAlex Deucher 	/* set the power state here in case we are a PX system or headless */
1891c940b447SAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled)
1892c940b447SAlex Deucher 		radeon_pm_compute_clocks(rdev);
1893c940b447SAlex Deucher 
18949bb39ff4SMaarten Lankhorst 	if (!r) {
18959bb39ff4SMaarten Lankhorst 		r = radeon_ib_ring_tests(rdev);
18969bb39ff4SMaarten Lankhorst 		if (r && saved)
18979bb39ff4SMaarten Lankhorst 			r = -EAGAIN;
18989bb39ff4SMaarten Lankhorst 	} else {
189990aca4d2SJerome Glisse 		/* bad news, how to tell it to userspace ? */
190090aca4d2SJerome Glisse 		dev_info(rdev->dev, "GPU reset failed\n");
19017a1619b9SMichel Dänzer 	}
19027a1619b9SMichel Dänzer 
19039bb39ff4SMaarten Lankhorst 	rdev->needs_reset = r == -EAGAIN;
19049bb39ff4SMaarten Lankhorst 	rdev->in_reset = false;
19059bb39ff4SMaarten Lankhorst 
19069bb39ff4SMaarten Lankhorst 	up_read(&rdev->exclusive_lock);
190790aca4d2SJerome Glisse 	return r;
190890aca4d2SJerome Glisse }
190990aca4d2SJerome Glisse 
1910771fe6b9SJerome Glisse 
1911771fe6b9SJerome Glisse /*
1912771fe6b9SJerome Glisse  * Debugfs
1913771fe6b9SJerome Glisse  */
1914771fe6b9SJerome Glisse int radeon_debugfs_add_files(struct radeon_device *rdev,
1915771fe6b9SJerome Glisse 			     struct drm_info_list *files,
1916771fe6b9SJerome Glisse 			     unsigned nfiles)
1917771fe6b9SJerome Glisse {
1918771fe6b9SJerome Glisse 	unsigned i;
1919771fe6b9SJerome Glisse 
19204d8bf9aeSChristian König 	for (i = 0; i < rdev->debugfs_count; i++) {
19214d8bf9aeSChristian König 		if (rdev->debugfs[i].files == files) {
1922771fe6b9SJerome Glisse 			/* Already registered */
1923771fe6b9SJerome Glisse 			return 0;
1924771fe6b9SJerome Glisse 		}
1925771fe6b9SJerome Glisse 	}
1926c245cb9eSMichael Witten 
19274d8bf9aeSChristian König 	i = rdev->debugfs_count + 1;
1928c245cb9eSMichael Witten 	if (i > RADEON_DEBUGFS_MAX_COMPONENTS) {
1929c245cb9eSMichael Witten 		DRM_ERROR("Reached maximum number of debugfs components.\n");
1930c245cb9eSMichael Witten 		DRM_ERROR("Report so we increase "
1931c245cb9eSMichael Witten 			  "RADEON_DEBUGFS_MAX_COMPONENTS.\n");
1932771fe6b9SJerome Glisse 		return -EINVAL;
1933771fe6b9SJerome Glisse 	}
19344d8bf9aeSChristian König 	rdev->debugfs[rdev->debugfs_count].files = files;
19354d8bf9aeSChristian König 	rdev->debugfs[rdev->debugfs_count].num_files = nfiles;
19364d8bf9aeSChristian König 	rdev->debugfs_count = i;
1937771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS)
1938771fe6b9SJerome Glisse 	drm_debugfs_create_files(files, nfiles,
1939771fe6b9SJerome Glisse 				 rdev->ddev->control->debugfs_root,
1940771fe6b9SJerome Glisse 				 rdev->ddev->control);
1941771fe6b9SJerome Glisse 	drm_debugfs_create_files(files, nfiles,
1942771fe6b9SJerome Glisse 				 rdev->ddev->primary->debugfs_root,
1943771fe6b9SJerome Glisse 				 rdev->ddev->primary);
1944771fe6b9SJerome Glisse #endif
1945771fe6b9SJerome Glisse 	return 0;
1946771fe6b9SJerome Glisse }
1947771fe6b9SJerome Glisse 
19484d8bf9aeSChristian König static void radeon_debugfs_remove_files(struct radeon_device *rdev)
19494d8bf9aeSChristian König {
19504d8bf9aeSChristian König #if defined(CONFIG_DEBUG_FS)
19514d8bf9aeSChristian König 	unsigned i;
19524d8bf9aeSChristian König 
19534d8bf9aeSChristian König 	for (i = 0; i < rdev->debugfs_count; i++) {
19544d8bf9aeSChristian König 		drm_debugfs_remove_files(rdev->debugfs[i].files,
19554d8bf9aeSChristian König 					 rdev->debugfs[i].num_files,
19564d8bf9aeSChristian König 					 rdev->ddev->control);
19574d8bf9aeSChristian König 		drm_debugfs_remove_files(rdev->debugfs[i].files,
19584d8bf9aeSChristian König 					 rdev->debugfs[i].num_files,
19594d8bf9aeSChristian König 					 rdev->ddev->primary);
19604d8bf9aeSChristian König 	}
19614d8bf9aeSChristian König #endif
19624d8bf9aeSChristian König }
1963