xref: /openbmc/linux/drivers/gpu/drm/radeon/radeon_device.c (revision 13a7d299dbbcd4c76ff088ec240d7cd896174c2e)
1771fe6b9SJerome Glisse /*
2771fe6b9SJerome Glisse  * Copyright 2008 Advanced Micro Devices, Inc.
3771fe6b9SJerome Glisse  * Copyright 2008 Red Hat Inc.
4771fe6b9SJerome Glisse  * Copyright 2009 Jerome Glisse.
5771fe6b9SJerome Glisse  *
6771fe6b9SJerome Glisse  * Permission is hereby granted, free of charge, to any person obtaining a
7771fe6b9SJerome Glisse  * copy of this software and associated documentation files (the "Software"),
8771fe6b9SJerome Glisse  * to deal in the Software without restriction, including without limitation
9771fe6b9SJerome Glisse  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10771fe6b9SJerome Glisse  * and/or sell copies of the Software, and to permit persons to whom the
11771fe6b9SJerome Glisse  * Software is furnished to do so, subject to the following conditions:
12771fe6b9SJerome Glisse  *
13771fe6b9SJerome Glisse  * The above copyright notice and this permission notice shall be included in
14771fe6b9SJerome Glisse  * all copies or substantial portions of the Software.
15771fe6b9SJerome Glisse  *
16771fe6b9SJerome Glisse  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17771fe6b9SJerome Glisse  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18771fe6b9SJerome Glisse  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19771fe6b9SJerome Glisse  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20771fe6b9SJerome Glisse  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21771fe6b9SJerome Glisse  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22771fe6b9SJerome Glisse  * OTHER DEALINGS IN THE SOFTWARE.
23771fe6b9SJerome Glisse  *
24771fe6b9SJerome Glisse  * Authors: Dave Airlie
25771fe6b9SJerome Glisse  *          Alex Deucher
26771fe6b9SJerome Glisse  *          Jerome Glisse
27771fe6b9SJerome Glisse  */
28771fe6b9SJerome Glisse #include <linux/console.h>
295a0e3ad6STejun Heo #include <linux/slab.h>
30771fe6b9SJerome Glisse #include <drm/drmP.h>
31771fe6b9SJerome Glisse #include <drm/drm_crtc_helper.h>
32771fe6b9SJerome Glisse #include <drm/radeon_drm.h>
3328d52043SDave Airlie #include <linux/vgaarb.h>
346a9ee8afSDave Airlie #include <linux/vga_switcheroo.h>
35bcc65fd8SMatthew Garrett #include <linux/efi.h>
36771fe6b9SJerome Glisse #include "radeon_reg.h"
37771fe6b9SJerome Glisse #include "radeon.h"
38771fe6b9SJerome Glisse #include "atom.h"
39771fe6b9SJerome Glisse 
401b5331d9SJerome Glisse static const char radeon_family_name[][16] = {
411b5331d9SJerome Glisse 	"R100",
421b5331d9SJerome Glisse 	"RV100",
431b5331d9SJerome Glisse 	"RS100",
441b5331d9SJerome Glisse 	"RV200",
451b5331d9SJerome Glisse 	"RS200",
461b5331d9SJerome Glisse 	"R200",
471b5331d9SJerome Glisse 	"RV250",
481b5331d9SJerome Glisse 	"RS300",
491b5331d9SJerome Glisse 	"RV280",
501b5331d9SJerome Glisse 	"R300",
511b5331d9SJerome Glisse 	"R350",
521b5331d9SJerome Glisse 	"RV350",
531b5331d9SJerome Glisse 	"RV380",
541b5331d9SJerome Glisse 	"R420",
551b5331d9SJerome Glisse 	"R423",
561b5331d9SJerome Glisse 	"RV410",
571b5331d9SJerome Glisse 	"RS400",
581b5331d9SJerome Glisse 	"RS480",
591b5331d9SJerome Glisse 	"RS600",
601b5331d9SJerome Glisse 	"RS690",
611b5331d9SJerome Glisse 	"RS740",
621b5331d9SJerome Glisse 	"RV515",
631b5331d9SJerome Glisse 	"R520",
641b5331d9SJerome Glisse 	"RV530",
651b5331d9SJerome Glisse 	"RV560",
661b5331d9SJerome Glisse 	"RV570",
671b5331d9SJerome Glisse 	"R580",
681b5331d9SJerome Glisse 	"R600",
691b5331d9SJerome Glisse 	"RV610",
701b5331d9SJerome Glisse 	"RV630",
711b5331d9SJerome Glisse 	"RV670",
721b5331d9SJerome Glisse 	"RV620",
731b5331d9SJerome Glisse 	"RV635",
741b5331d9SJerome Glisse 	"RS780",
751b5331d9SJerome Glisse 	"RS880",
761b5331d9SJerome Glisse 	"RV770",
771b5331d9SJerome Glisse 	"RV730",
781b5331d9SJerome Glisse 	"RV710",
791b5331d9SJerome Glisse 	"RV740",
801b5331d9SJerome Glisse 	"CEDAR",
811b5331d9SJerome Glisse 	"REDWOOD",
821b5331d9SJerome Glisse 	"JUNIPER",
831b5331d9SJerome Glisse 	"CYPRESS",
841b5331d9SJerome Glisse 	"HEMLOCK",
85b08ebe7eSAlex Deucher 	"PALM",
864df64e65SAlex Deucher 	"SUMO",
874df64e65SAlex Deucher 	"SUMO2",
881fe18305SAlex Deucher 	"BARTS",
891fe18305SAlex Deucher 	"TURKS",
901fe18305SAlex Deucher 	"CAICOS",
91b7cfc9feSAlex Deucher 	"CAYMAN",
928848f759SAlex Deucher 	"ARUBA",
93cb28bb34SAlex Deucher 	"TAHITI",
94cb28bb34SAlex Deucher 	"PITCAIRN",
95cb28bb34SAlex Deucher 	"VERDE",
96624d3524SAlex Deucher 	"OLAND",
97b5d9d726SAlex Deucher 	"HAINAN",
986eac752eSAlex Deucher 	"BONAIRE",
996eac752eSAlex Deucher 	"KAVERI",
1006eac752eSAlex Deucher 	"KABINI",
1013bf599e8SAlex Deucher 	"HAWAII",
102b0a9f22aSSamuel Li 	"MULLINS",
1031b5331d9SJerome Glisse 	"LAST",
1041b5331d9SJerome Glisse };
1051b5331d9SJerome Glisse 
1064807c5a8SAlex Deucher #define RADEON_PX_QUIRK_DISABLE_PX  (1 << 0)
1074807c5a8SAlex Deucher #define RADEON_PX_QUIRK_LONG_WAKEUP (1 << 1)
1084807c5a8SAlex Deucher 
1094807c5a8SAlex Deucher struct radeon_px_quirk {
1104807c5a8SAlex Deucher 	u32 chip_vendor;
1114807c5a8SAlex Deucher 	u32 chip_device;
1124807c5a8SAlex Deucher 	u32 subsys_vendor;
1134807c5a8SAlex Deucher 	u32 subsys_device;
1144807c5a8SAlex Deucher 	u32 px_quirk_flags;
1154807c5a8SAlex Deucher };
1164807c5a8SAlex Deucher 
1174807c5a8SAlex Deucher static struct radeon_px_quirk radeon_px_quirk_list[] = {
1184807c5a8SAlex Deucher 	/* Acer aspire 5560g (CPU: AMD A4-3305M; GPU: AMD Radeon HD 6480g + 7470m)
1194807c5a8SAlex Deucher 	 * https://bugzilla.kernel.org/show_bug.cgi?id=74551
1204807c5a8SAlex Deucher 	 */
1214807c5a8SAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6760, 0x1025, 0x0672, RADEON_PX_QUIRK_DISABLE_PX },
1224807c5a8SAlex Deucher 	/* Asus K73TA laptop with AMD A6-3400M APU and Radeon 6550 GPU
1234807c5a8SAlex Deucher 	 * https://bugzilla.kernel.org/show_bug.cgi?id=51381
1244807c5a8SAlex Deucher 	 */
1254807c5a8SAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6741, 0x1043, 0x108c, RADEON_PX_QUIRK_DISABLE_PX },
1264807c5a8SAlex Deucher 	/* macbook pro 8.2 */
1274807c5a8SAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6741, PCI_VENDOR_ID_APPLE, 0x00e2, RADEON_PX_QUIRK_LONG_WAKEUP },
1284807c5a8SAlex Deucher 	{ 0, 0, 0, 0, 0 },
1294807c5a8SAlex Deucher };
1304807c5a8SAlex Deucher 
13190c4cde9SAlex Deucher bool radeon_is_px(struct drm_device *dev)
13290c4cde9SAlex Deucher {
13390c4cde9SAlex Deucher 	struct radeon_device *rdev = dev->dev_private;
13490c4cde9SAlex Deucher 
13590c4cde9SAlex Deucher 	if (rdev->flags & RADEON_IS_PX)
13690c4cde9SAlex Deucher 		return true;
13790c4cde9SAlex Deucher 	return false;
13890c4cde9SAlex Deucher }
13910ebc0bcSDave Airlie 
1404807c5a8SAlex Deucher static void radeon_device_handle_px_quirks(struct radeon_device *rdev)
1414807c5a8SAlex Deucher {
1424807c5a8SAlex Deucher 	struct radeon_px_quirk *p = radeon_px_quirk_list;
1434807c5a8SAlex Deucher 
1444807c5a8SAlex Deucher 	/* Apply PX quirks */
1454807c5a8SAlex Deucher 	while (p && p->chip_device != 0) {
1464807c5a8SAlex Deucher 		if (rdev->pdev->vendor == p->chip_vendor &&
1474807c5a8SAlex Deucher 		    rdev->pdev->device == p->chip_device &&
1484807c5a8SAlex Deucher 		    rdev->pdev->subsystem_vendor == p->subsys_vendor &&
1494807c5a8SAlex Deucher 		    rdev->pdev->subsystem_device == p->subsys_device) {
1504807c5a8SAlex Deucher 			rdev->px_quirk_flags = p->px_quirk_flags;
1514807c5a8SAlex Deucher 			break;
1524807c5a8SAlex Deucher 		}
1534807c5a8SAlex Deucher 		++p;
1544807c5a8SAlex Deucher 	}
1554807c5a8SAlex Deucher 
1564807c5a8SAlex Deucher 	if (rdev->px_quirk_flags & RADEON_PX_QUIRK_DISABLE_PX)
1574807c5a8SAlex Deucher 		rdev->flags &= ~RADEON_IS_PX;
1584807c5a8SAlex Deucher }
1594807c5a8SAlex Deucher 
1600c195119SAlex Deucher /**
1612e1b65f9SAlex Deucher  * radeon_program_register_sequence - program an array of registers.
1622e1b65f9SAlex Deucher  *
1632e1b65f9SAlex Deucher  * @rdev: radeon_device pointer
1642e1b65f9SAlex Deucher  * @registers: pointer to the register array
1652e1b65f9SAlex Deucher  * @array_size: size of the register array
1662e1b65f9SAlex Deucher  *
1672e1b65f9SAlex Deucher  * Programs an array or registers with and and or masks.
1682e1b65f9SAlex Deucher  * This is a helper for setting golden registers.
1692e1b65f9SAlex Deucher  */
1702e1b65f9SAlex Deucher void radeon_program_register_sequence(struct radeon_device *rdev,
1712e1b65f9SAlex Deucher 				      const u32 *registers,
1722e1b65f9SAlex Deucher 				      const u32 array_size)
1732e1b65f9SAlex Deucher {
1742e1b65f9SAlex Deucher 	u32 tmp, reg, and_mask, or_mask;
1752e1b65f9SAlex Deucher 	int i;
1762e1b65f9SAlex Deucher 
1772e1b65f9SAlex Deucher 	if (array_size % 3)
1782e1b65f9SAlex Deucher 		return;
1792e1b65f9SAlex Deucher 
1802e1b65f9SAlex Deucher 	for (i = 0; i < array_size; i +=3) {
1812e1b65f9SAlex Deucher 		reg = registers[i + 0];
1822e1b65f9SAlex Deucher 		and_mask = registers[i + 1];
1832e1b65f9SAlex Deucher 		or_mask = registers[i + 2];
1842e1b65f9SAlex Deucher 
1852e1b65f9SAlex Deucher 		if (and_mask == 0xffffffff) {
1862e1b65f9SAlex Deucher 			tmp = or_mask;
1872e1b65f9SAlex Deucher 		} else {
1882e1b65f9SAlex Deucher 			tmp = RREG32(reg);
1892e1b65f9SAlex Deucher 			tmp &= ~and_mask;
1902e1b65f9SAlex Deucher 			tmp |= or_mask;
1912e1b65f9SAlex Deucher 		}
1922e1b65f9SAlex Deucher 		WREG32(reg, tmp);
1932e1b65f9SAlex Deucher 	}
1942e1b65f9SAlex Deucher }
1952e1b65f9SAlex Deucher 
1961a0041b8SAlex Deucher void radeon_pci_config_reset(struct radeon_device *rdev)
1971a0041b8SAlex Deucher {
1981a0041b8SAlex Deucher 	pci_write_config_dword(rdev->pdev, 0x7c, RADEON_ASIC_RESET_DATA);
1991a0041b8SAlex Deucher }
2001a0041b8SAlex Deucher 
2012e1b65f9SAlex Deucher /**
2020c195119SAlex Deucher  * radeon_surface_init - Clear GPU surface registers.
2030c195119SAlex Deucher  *
2040c195119SAlex Deucher  * @rdev: radeon_device pointer
2050c195119SAlex Deucher  *
2060c195119SAlex Deucher  * Clear GPU surface registers (r1xx-r5xx).
207b1e3a6d1SMichel Dänzer  */
2083ce0a23dSJerome Glisse void radeon_surface_init(struct radeon_device *rdev)
209b1e3a6d1SMichel Dänzer {
210b1e3a6d1SMichel Dänzer 	/* FIXME: check this out */
211b1e3a6d1SMichel Dänzer 	if (rdev->family < CHIP_R600) {
212b1e3a6d1SMichel Dänzer 		int i;
213b1e3a6d1SMichel Dänzer 
214550e2d92SDave Airlie 		for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
215550e2d92SDave Airlie 			if (rdev->surface_regs[i].bo)
216550e2d92SDave Airlie 				radeon_bo_get_surface_reg(rdev->surface_regs[i].bo);
217550e2d92SDave Airlie 			else
218550e2d92SDave Airlie 				radeon_clear_surface_reg(rdev, i);
219b1e3a6d1SMichel Dänzer 		}
220e024e110SDave Airlie 		/* enable surfaces */
221e024e110SDave Airlie 		WREG32(RADEON_SURFACE_CNTL, 0);
222b1e3a6d1SMichel Dänzer 	}
223b1e3a6d1SMichel Dänzer }
224b1e3a6d1SMichel Dänzer 
225b1e3a6d1SMichel Dänzer /*
226771fe6b9SJerome Glisse  * GPU scratch registers helpers function.
227771fe6b9SJerome Glisse  */
2280c195119SAlex Deucher /**
2290c195119SAlex Deucher  * radeon_scratch_init - Init scratch register driver information.
2300c195119SAlex Deucher  *
2310c195119SAlex Deucher  * @rdev: radeon_device pointer
2320c195119SAlex Deucher  *
2330c195119SAlex Deucher  * Init CP scratch register driver information (r1xx-r5xx)
2340c195119SAlex Deucher  */
2353ce0a23dSJerome Glisse void radeon_scratch_init(struct radeon_device *rdev)
236771fe6b9SJerome Glisse {
237771fe6b9SJerome Glisse 	int i;
238771fe6b9SJerome Glisse 
239771fe6b9SJerome Glisse 	/* FIXME: check this out */
240771fe6b9SJerome Glisse 	if (rdev->family < CHIP_R300) {
241771fe6b9SJerome Glisse 		rdev->scratch.num_reg = 5;
242771fe6b9SJerome Glisse 	} else {
243771fe6b9SJerome Glisse 		rdev->scratch.num_reg = 7;
244771fe6b9SJerome Glisse 	}
245724c80e1SAlex Deucher 	rdev->scratch.reg_base = RADEON_SCRATCH_REG0;
246771fe6b9SJerome Glisse 	for (i = 0; i < rdev->scratch.num_reg; i++) {
247771fe6b9SJerome Glisse 		rdev->scratch.free[i] = true;
248724c80e1SAlex Deucher 		rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4);
249771fe6b9SJerome Glisse 	}
250771fe6b9SJerome Glisse }
251771fe6b9SJerome Glisse 
2520c195119SAlex Deucher /**
2530c195119SAlex Deucher  * radeon_scratch_get - Allocate a scratch register
2540c195119SAlex Deucher  *
2550c195119SAlex Deucher  * @rdev: radeon_device pointer
2560c195119SAlex Deucher  * @reg: scratch register mmio offset
2570c195119SAlex Deucher  *
2580c195119SAlex Deucher  * Allocate a CP scratch register for use by the driver (all asics).
2590c195119SAlex Deucher  * Returns 0 on success or -EINVAL on failure.
2600c195119SAlex Deucher  */
261771fe6b9SJerome Glisse int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg)
262771fe6b9SJerome Glisse {
263771fe6b9SJerome Glisse 	int i;
264771fe6b9SJerome Glisse 
265771fe6b9SJerome Glisse 	for (i = 0; i < rdev->scratch.num_reg; i++) {
266771fe6b9SJerome Glisse 		if (rdev->scratch.free[i]) {
267771fe6b9SJerome Glisse 			rdev->scratch.free[i] = false;
268771fe6b9SJerome Glisse 			*reg = rdev->scratch.reg[i];
269771fe6b9SJerome Glisse 			return 0;
270771fe6b9SJerome Glisse 		}
271771fe6b9SJerome Glisse 	}
272771fe6b9SJerome Glisse 	return -EINVAL;
273771fe6b9SJerome Glisse }
274771fe6b9SJerome Glisse 
2750c195119SAlex Deucher /**
2760c195119SAlex Deucher  * radeon_scratch_free - Free a scratch register
2770c195119SAlex Deucher  *
2780c195119SAlex Deucher  * @rdev: radeon_device pointer
2790c195119SAlex Deucher  * @reg: scratch register mmio offset
2800c195119SAlex Deucher  *
2810c195119SAlex Deucher  * Free a CP scratch register allocated for use by the driver (all asics)
2820c195119SAlex Deucher  */
283771fe6b9SJerome Glisse void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg)
284771fe6b9SJerome Glisse {
285771fe6b9SJerome Glisse 	int i;
286771fe6b9SJerome Glisse 
287771fe6b9SJerome Glisse 	for (i = 0; i < rdev->scratch.num_reg; i++) {
288771fe6b9SJerome Glisse 		if (rdev->scratch.reg[i] == reg) {
289771fe6b9SJerome Glisse 			rdev->scratch.free[i] = true;
290771fe6b9SJerome Glisse 			return;
291771fe6b9SJerome Glisse 		}
292771fe6b9SJerome Glisse 	}
293771fe6b9SJerome Glisse }
294771fe6b9SJerome Glisse 
2950c195119SAlex Deucher /*
29675efdee1SAlex Deucher  * GPU doorbell aperture helpers function.
29775efdee1SAlex Deucher  */
29875efdee1SAlex Deucher /**
29975efdee1SAlex Deucher  * radeon_doorbell_init - Init doorbell driver information.
30075efdee1SAlex Deucher  *
30175efdee1SAlex Deucher  * @rdev: radeon_device pointer
30275efdee1SAlex Deucher  *
30375efdee1SAlex Deucher  * Init doorbell driver information (CIK)
30475efdee1SAlex Deucher  * Returns 0 on success, error on failure.
30575efdee1SAlex Deucher  */
30628f5a6cdSRashika Kheria static int radeon_doorbell_init(struct radeon_device *rdev)
30775efdee1SAlex Deucher {
30875efdee1SAlex Deucher 	/* doorbell bar mapping */
30975efdee1SAlex Deucher 	rdev->doorbell.base = pci_resource_start(rdev->pdev, 2);
31075efdee1SAlex Deucher 	rdev->doorbell.size = pci_resource_len(rdev->pdev, 2);
31175efdee1SAlex Deucher 
312d5754ab8SAndrew Lewycky 	rdev->doorbell.num_doorbells = min_t(u32, rdev->doorbell.size / sizeof(u32), RADEON_MAX_DOORBELLS);
313d5754ab8SAndrew Lewycky 	if (rdev->doorbell.num_doorbells == 0)
314d5754ab8SAndrew Lewycky 		return -EINVAL;
31575efdee1SAlex Deucher 
316d5754ab8SAndrew Lewycky 	rdev->doorbell.ptr = ioremap(rdev->doorbell.base, rdev->doorbell.num_doorbells * sizeof(u32));
31775efdee1SAlex Deucher 	if (rdev->doorbell.ptr == NULL) {
31875efdee1SAlex Deucher 		return -ENOMEM;
31975efdee1SAlex Deucher 	}
32075efdee1SAlex Deucher 	DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)rdev->doorbell.base);
32175efdee1SAlex Deucher 	DRM_INFO("doorbell mmio size: %u\n", (unsigned)rdev->doorbell.size);
32275efdee1SAlex Deucher 
323d5754ab8SAndrew Lewycky 	memset(&rdev->doorbell.used, 0, sizeof(rdev->doorbell.used));
32475efdee1SAlex Deucher 
32575efdee1SAlex Deucher 	return 0;
32675efdee1SAlex Deucher }
32775efdee1SAlex Deucher 
32875efdee1SAlex Deucher /**
32975efdee1SAlex Deucher  * radeon_doorbell_fini - Tear down doorbell driver information.
33075efdee1SAlex Deucher  *
33175efdee1SAlex Deucher  * @rdev: radeon_device pointer
33275efdee1SAlex Deucher  *
33375efdee1SAlex Deucher  * Tear down doorbell driver information (CIK)
33475efdee1SAlex Deucher  */
33528f5a6cdSRashika Kheria static void radeon_doorbell_fini(struct radeon_device *rdev)
33675efdee1SAlex Deucher {
33775efdee1SAlex Deucher 	iounmap(rdev->doorbell.ptr);
33875efdee1SAlex Deucher 	rdev->doorbell.ptr = NULL;
33975efdee1SAlex Deucher }
34075efdee1SAlex Deucher 
34175efdee1SAlex Deucher /**
342d5754ab8SAndrew Lewycky  * radeon_doorbell_get - Allocate a doorbell entry
34375efdee1SAlex Deucher  *
34475efdee1SAlex Deucher  * @rdev: radeon_device pointer
345d5754ab8SAndrew Lewycky  * @doorbell: doorbell index
34675efdee1SAlex Deucher  *
347d5754ab8SAndrew Lewycky  * Allocate a doorbell for use by the driver (all asics).
34875efdee1SAlex Deucher  * Returns 0 on success or -EINVAL on failure.
34975efdee1SAlex Deucher  */
35075efdee1SAlex Deucher int radeon_doorbell_get(struct radeon_device *rdev, u32 *doorbell)
35175efdee1SAlex Deucher {
352d5754ab8SAndrew Lewycky 	unsigned long offset = find_first_zero_bit(rdev->doorbell.used, rdev->doorbell.num_doorbells);
353d5754ab8SAndrew Lewycky 	if (offset < rdev->doorbell.num_doorbells) {
354d5754ab8SAndrew Lewycky 		__set_bit(offset, rdev->doorbell.used);
355d5754ab8SAndrew Lewycky 		*doorbell = offset;
35675efdee1SAlex Deucher 		return 0;
357d5754ab8SAndrew Lewycky 	} else {
35875efdee1SAlex Deucher 		return -EINVAL;
35975efdee1SAlex Deucher 	}
360d5754ab8SAndrew Lewycky }
36175efdee1SAlex Deucher 
36275efdee1SAlex Deucher /**
363d5754ab8SAndrew Lewycky  * radeon_doorbell_free - Free a doorbell entry
36475efdee1SAlex Deucher  *
36575efdee1SAlex Deucher  * @rdev: radeon_device pointer
366d5754ab8SAndrew Lewycky  * @doorbell: doorbell index
36775efdee1SAlex Deucher  *
368d5754ab8SAndrew Lewycky  * Free a doorbell allocated for use by the driver (all asics)
36975efdee1SAlex Deucher  */
37075efdee1SAlex Deucher void radeon_doorbell_free(struct radeon_device *rdev, u32 doorbell)
37175efdee1SAlex Deucher {
372d5754ab8SAndrew Lewycky 	if (doorbell < rdev->doorbell.num_doorbells)
373d5754ab8SAndrew Lewycky 		__clear_bit(doorbell, rdev->doorbell.used);
37475efdee1SAlex Deucher }
37575efdee1SAlex Deucher 
37675efdee1SAlex Deucher /*
3770c195119SAlex Deucher  * radeon_wb_*()
3780c195119SAlex Deucher  * Writeback is the the method by which the the GPU updates special pages
3790c195119SAlex Deucher  * in memory with the status of certain GPU events (fences, ring pointers,
3800c195119SAlex Deucher  * etc.).
3810c195119SAlex Deucher  */
3820c195119SAlex Deucher 
3830c195119SAlex Deucher /**
3840c195119SAlex Deucher  * radeon_wb_disable - Disable Writeback
3850c195119SAlex Deucher  *
3860c195119SAlex Deucher  * @rdev: radeon_device pointer
3870c195119SAlex Deucher  *
3880c195119SAlex Deucher  * Disables Writeback (all asics).  Used for suspend.
3890c195119SAlex Deucher  */
390724c80e1SAlex Deucher void radeon_wb_disable(struct radeon_device *rdev)
391724c80e1SAlex Deucher {
392724c80e1SAlex Deucher 	rdev->wb.enabled = false;
393724c80e1SAlex Deucher }
394724c80e1SAlex Deucher 
3950c195119SAlex Deucher /**
3960c195119SAlex Deucher  * radeon_wb_fini - Disable Writeback and free memory
3970c195119SAlex Deucher  *
3980c195119SAlex Deucher  * @rdev: radeon_device pointer
3990c195119SAlex Deucher  *
4000c195119SAlex Deucher  * Disables Writeback and frees the Writeback memory (all asics).
4010c195119SAlex Deucher  * Used at driver shutdown.
4020c195119SAlex Deucher  */
403724c80e1SAlex Deucher void radeon_wb_fini(struct radeon_device *rdev)
404724c80e1SAlex Deucher {
405724c80e1SAlex Deucher 	radeon_wb_disable(rdev);
406724c80e1SAlex Deucher 	if (rdev->wb.wb_obj) {
407089920f2SJerome Glisse 		if (!radeon_bo_reserve(rdev->wb.wb_obj, false)) {
408089920f2SJerome Glisse 			radeon_bo_kunmap(rdev->wb.wb_obj);
409089920f2SJerome Glisse 			radeon_bo_unpin(rdev->wb.wb_obj);
410089920f2SJerome Glisse 			radeon_bo_unreserve(rdev->wb.wb_obj);
411089920f2SJerome Glisse 		}
412724c80e1SAlex Deucher 		radeon_bo_unref(&rdev->wb.wb_obj);
413724c80e1SAlex Deucher 		rdev->wb.wb = NULL;
414724c80e1SAlex Deucher 		rdev->wb.wb_obj = NULL;
415724c80e1SAlex Deucher 	}
416724c80e1SAlex Deucher }
417724c80e1SAlex Deucher 
4180c195119SAlex Deucher /**
4190c195119SAlex Deucher  * radeon_wb_init- Init Writeback driver info and allocate memory
4200c195119SAlex Deucher  *
4210c195119SAlex Deucher  * @rdev: radeon_device pointer
4220c195119SAlex Deucher  *
4230c195119SAlex Deucher  * Disables Writeback and frees the Writeback memory (all asics).
4240c195119SAlex Deucher  * Used at driver startup.
4250c195119SAlex Deucher  * Returns 0 on success or an -error on failure.
4260c195119SAlex Deucher  */
427724c80e1SAlex Deucher int radeon_wb_init(struct radeon_device *rdev)
428724c80e1SAlex Deucher {
429724c80e1SAlex Deucher 	int r;
430724c80e1SAlex Deucher 
431724c80e1SAlex Deucher 	if (rdev->wb.wb_obj == NULL) {
432441921d5SDaniel Vetter 		r = radeon_bo_create(rdev, RADEON_GPU_PAGE_SIZE, PAGE_SIZE, true,
43302376d82SMichel Dänzer 				     RADEON_GEM_DOMAIN_GTT, 0, NULL,
43402376d82SMichel Dänzer 				     &rdev->wb.wb_obj);
435724c80e1SAlex Deucher 		if (r) {
436724c80e1SAlex Deucher 			dev_warn(rdev->dev, "(%d) create WB bo failed\n", r);
437724c80e1SAlex Deucher 			return r;
438724c80e1SAlex Deucher 		}
439724c80e1SAlex Deucher 		r = radeon_bo_reserve(rdev->wb.wb_obj, false);
440724c80e1SAlex Deucher 		if (unlikely(r != 0)) {
441724c80e1SAlex Deucher 			radeon_wb_fini(rdev);
442724c80e1SAlex Deucher 			return r;
443724c80e1SAlex Deucher 		}
444724c80e1SAlex Deucher 		r = radeon_bo_pin(rdev->wb.wb_obj, RADEON_GEM_DOMAIN_GTT,
445724c80e1SAlex Deucher 				&rdev->wb.gpu_addr);
446724c80e1SAlex Deucher 		if (r) {
447724c80e1SAlex Deucher 			radeon_bo_unreserve(rdev->wb.wb_obj);
448724c80e1SAlex Deucher 			dev_warn(rdev->dev, "(%d) pin WB bo failed\n", r);
449724c80e1SAlex Deucher 			radeon_wb_fini(rdev);
450724c80e1SAlex Deucher 			return r;
451724c80e1SAlex Deucher 		}
452724c80e1SAlex Deucher 		r = radeon_bo_kmap(rdev->wb.wb_obj, (void **)&rdev->wb.wb);
453724c80e1SAlex Deucher 		radeon_bo_unreserve(rdev->wb.wb_obj);
454724c80e1SAlex Deucher 		if (r) {
455724c80e1SAlex Deucher 			dev_warn(rdev->dev, "(%d) map WB bo failed\n", r);
456724c80e1SAlex Deucher 			radeon_wb_fini(rdev);
457724c80e1SAlex Deucher 			return r;
458724c80e1SAlex Deucher 		}
459089920f2SJerome Glisse 	}
460724c80e1SAlex Deucher 
461e6ba7599SAlex Deucher 	/* clear wb memory */
462e6ba7599SAlex Deucher 	memset((char *)rdev->wb.wb, 0, RADEON_GPU_PAGE_SIZE);
463d0f8a854SAlex Deucher 	/* disable event_write fences */
464d0f8a854SAlex Deucher 	rdev->wb.use_event = false;
465724c80e1SAlex Deucher 	/* disabled via module param */
4663b7a2b24SJerome Glisse 	if (radeon_no_wb == 1) {
467724c80e1SAlex Deucher 		rdev->wb.enabled = false;
4683b7a2b24SJerome Glisse 	} else {
469724c80e1SAlex Deucher 		if (rdev->flags & RADEON_IS_AGP) {
47028eebb70SAlex Deucher 			/* often unreliable on AGP */
47128eebb70SAlex Deucher 			rdev->wb.enabled = false;
47228eebb70SAlex Deucher 		} else if (rdev->family < CHIP_R300) {
47328eebb70SAlex Deucher 			/* often unreliable on pre-r300 */
474724c80e1SAlex Deucher 			rdev->wb.enabled = false;
475d0f8a854SAlex Deucher 		} else {
476724c80e1SAlex Deucher 			rdev->wb.enabled = true;
477d0f8a854SAlex Deucher 			/* event_write fences are only available on r600+ */
4783b7a2b24SJerome Glisse 			if (rdev->family >= CHIP_R600) {
479d0f8a854SAlex Deucher 				rdev->wb.use_event = true;
480d0f8a854SAlex Deucher 			}
481724c80e1SAlex Deucher 		}
4823b7a2b24SJerome Glisse 	}
483c994ead6SAlex Deucher 	/* always use writeback/events on NI, APUs */
484c994ead6SAlex Deucher 	if (rdev->family >= CHIP_PALM) {
4857d52785dSAlex Deucher 		rdev->wb.enabled = true;
4867d52785dSAlex Deucher 		rdev->wb.use_event = true;
4877d52785dSAlex Deucher 	}
488724c80e1SAlex Deucher 
489724c80e1SAlex Deucher 	dev_info(rdev->dev, "WB %sabled\n", rdev->wb.enabled ? "en" : "dis");
490724c80e1SAlex Deucher 
491724c80e1SAlex Deucher 	return 0;
492724c80e1SAlex Deucher }
493724c80e1SAlex Deucher 
494d594e46aSJerome Glisse /**
495d594e46aSJerome Glisse  * radeon_vram_location - try to find VRAM location
496d594e46aSJerome Glisse  * @rdev: radeon device structure holding all necessary informations
497d594e46aSJerome Glisse  * @mc: memory controller structure holding memory informations
498d594e46aSJerome Glisse  * @base: base address at which to put VRAM
499d594e46aSJerome Glisse  *
500d594e46aSJerome Glisse  * Function will place try to place VRAM at base address provided
501d594e46aSJerome Glisse  * as parameter (which is so far either PCI aperture address or
502d594e46aSJerome Glisse  * for IGP TOM base address).
503d594e46aSJerome Glisse  *
504d594e46aSJerome Glisse  * If there is not enough space to fit the unvisible VRAM in the 32bits
505d594e46aSJerome Glisse  * address space then we limit the VRAM size to the aperture.
506d594e46aSJerome Glisse  *
507d594e46aSJerome Glisse  * If we are using AGP and if the AGP aperture doesn't allow us to have
508d594e46aSJerome Glisse  * room for all the VRAM than we restrict the VRAM to the PCI aperture
509d594e46aSJerome Glisse  * size and print a warning.
510d594e46aSJerome Glisse  *
511d594e46aSJerome Glisse  * This function will never fails, worst case are limiting VRAM.
512d594e46aSJerome Glisse  *
513d594e46aSJerome Glisse  * Note: GTT start, end, size should be initialized before calling this
514d594e46aSJerome Glisse  * function on AGP platform.
515d594e46aSJerome Glisse  *
51625985edcSLucas De Marchi  * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
517d594e46aSJerome Glisse  * this shouldn't be a problem as we are using the PCI aperture as a reference.
518d594e46aSJerome Glisse  * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
519d594e46aSJerome Glisse  * not IGP.
520d594e46aSJerome Glisse  *
521d594e46aSJerome Glisse  * Note: we use mc_vram_size as on some board we need to program the mc to
522d594e46aSJerome Glisse  * cover the whole aperture even if VRAM size is inferior to aperture size
523d594e46aSJerome Glisse  * Novell bug 204882 + along with lots of ubuntu ones
524d594e46aSJerome Glisse  *
525d594e46aSJerome Glisse  * Note: when limiting vram it's safe to overwritte real_vram_size because
526d594e46aSJerome Glisse  * we are not in case where real_vram_size is inferior to mc_vram_size (ie
527d594e46aSJerome Glisse  * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
528d594e46aSJerome Glisse  * ones)
529d594e46aSJerome Glisse  *
530d594e46aSJerome Glisse  * Note: IGP TOM addr should be the same as the aperture addr, we don't
531d594e46aSJerome Glisse  * explicitly check for that thought.
532d594e46aSJerome Glisse  *
533d594e46aSJerome Glisse  * FIXME: when reducing VRAM size align new size on power of 2.
534771fe6b9SJerome Glisse  */
535d594e46aSJerome Glisse void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base)
536771fe6b9SJerome Glisse {
5371bcb04f7SChristian König 	uint64_t limit = (uint64_t)radeon_vram_limit << 20;
5381bcb04f7SChristian König 
539d594e46aSJerome Glisse 	mc->vram_start = base;
5409ed8b1f9SAlex Deucher 	if (mc->mc_vram_size > (rdev->mc.mc_mask - base + 1)) {
541d594e46aSJerome Glisse 		dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
542d594e46aSJerome Glisse 		mc->real_vram_size = mc->aper_size;
543d594e46aSJerome Glisse 		mc->mc_vram_size = mc->aper_size;
544771fe6b9SJerome Glisse 	}
545d594e46aSJerome Glisse 	mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
5462cbeb4efSJerome Glisse 	if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_start <= mc->gtt_end) {
547d594e46aSJerome Glisse 		dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
548d594e46aSJerome Glisse 		mc->real_vram_size = mc->aper_size;
549d594e46aSJerome Glisse 		mc->mc_vram_size = mc->aper_size;
550771fe6b9SJerome Glisse 	}
551d594e46aSJerome Glisse 	mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
5521bcb04f7SChristian König 	if (limit && limit < mc->real_vram_size)
5531bcb04f7SChristian König 		mc->real_vram_size = limit;
554dd7cc55aSAlex Deucher 	dev_info(rdev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
555d594e46aSJerome Glisse 			mc->mc_vram_size >> 20, mc->vram_start,
556d594e46aSJerome Glisse 			mc->vram_end, mc->real_vram_size >> 20);
557771fe6b9SJerome Glisse }
558771fe6b9SJerome Glisse 
559d594e46aSJerome Glisse /**
560d594e46aSJerome Glisse  * radeon_gtt_location - try to find GTT location
561d594e46aSJerome Glisse  * @rdev: radeon device structure holding all necessary informations
562d594e46aSJerome Glisse  * @mc: memory controller structure holding memory informations
563d594e46aSJerome Glisse  *
564d594e46aSJerome Glisse  * Function will place try to place GTT before or after VRAM.
565d594e46aSJerome Glisse  *
566d594e46aSJerome Glisse  * If GTT size is bigger than space left then we ajust GTT size.
567d594e46aSJerome Glisse  * Thus function will never fails.
568d594e46aSJerome Glisse  *
569d594e46aSJerome Glisse  * FIXME: when reducing GTT size align new size on power of 2.
570d594e46aSJerome Glisse  */
571d594e46aSJerome Glisse void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc)
572d594e46aSJerome Glisse {
573d594e46aSJerome Glisse 	u64 size_af, size_bf;
574d594e46aSJerome Glisse 
5759ed8b1f9SAlex Deucher 	size_af = ((rdev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
5768d369bb1SAlex Deucher 	size_bf = mc->vram_start & ~mc->gtt_base_align;
577d594e46aSJerome Glisse 	if (size_bf > size_af) {
578d594e46aSJerome Glisse 		if (mc->gtt_size > size_bf) {
579d594e46aSJerome Glisse 			dev_warn(rdev->dev, "limiting GTT\n");
580d594e46aSJerome Glisse 			mc->gtt_size = size_bf;
581d594e46aSJerome Glisse 		}
5828d369bb1SAlex Deucher 		mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
583d594e46aSJerome Glisse 	} else {
584d594e46aSJerome Glisse 		if (mc->gtt_size > size_af) {
585d594e46aSJerome Glisse 			dev_warn(rdev->dev, "limiting GTT\n");
586d594e46aSJerome Glisse 			mc->gtt_size = size_af;
587d594e46aSJerome Glisse 		}
5888d369bb1SAlex Deucher 		mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
589d594e46aSJerome Glisse 	}
590d594e46aSJerome Glisse 	mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
591dd7cc55aSAlex Deucher 	dev_info(rdev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
592d594e46aSJerome Glisse 			mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
593d594e46aSJerome Glisse }
594771fe6b9SJerome Glisse 
595771fe6b9SJerome Glisse /*
596771fe6b9SJerome Glisse  * GPU helpers function.
597771fe6b9SJerome Glisse  */
5980c195119SAlex Deucher /**
5990c195119SAlex Deucher  * radeon_card_posted - check if the hw has already been initialized
6000c195119SAlex Deucher  *
6010c195119SAlex Deucher  * @rdev: radeon_device pointer
6020c195119SAlex Deucher  *
6030c195119SAlex Deucher  * Check if the asic has been initialized (all asics).
6040c195119SAlex Deucher  * Used at driver startup.
6050c195119SAlex Deucher  * Returns true if initialized or false if not.
6060c195119SAlex Deucher  */
6079f022ddfSJerome Glisse bool radeon_card_posted(struct radeon_device *rdev)
608771fe6b9SJerome Glisse {
609771fe6b9SJerome Glisse 	uint32_t reg;
610771fe6b9SJerome Glisse 
61150a583f6SAlex Deucher 	/* required for EFI mode on macbook2,1 which uses an r5xx asic */
61283e68189SMatt Fleming 	if (efi_enabled(EFI_BOOT) &&
61350a583f6SAlex Deucher 	    (rdev->pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE) &&
61450a583f6SAlex Deucher 	    (rdev->family < CHIP_R600))
615bcc65fd8SMatthew Garrett 		return false;
616bcc65fd8SMatthew Garrett 
6172cf3a4fcSAlex Deucher 	if (ASIC_IS_NODCE(rdev))
6182cf3a4fcSAlex Deucher 		goto check_memsize;
6192cf3a4fcSAlex Deucher 
620771fe6b9SJerome Glisse 	/* first check CRTCs */
62109fb8bd1SAlex Deucher 	if (ASIC_IS_DCE4(rdev)) {
62218007401SAlex Deucher 		reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
62318007401SAlex Deucher 			RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET);
62409fb8bd1SAlex Deucher 			if (rdev->num_crtc >= 4) {
62509fb8bd1SAlex Deucher 				reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) |
62609fb8bd1SAlex Deucher 					RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET);
62709fb8bd1SAlex Deucher 			}
62809fb8bd1SAlex Deucher 			if (rdev->num_crtc >= 6) {
62909fb8bd1SAlex Deucher 				reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) |
630bcc1c2a1SAlex Deucher 					RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET);
63109fb8bd1SAlex Deucher 			}
632bcc1c2a1SAlex Deucher 		if (reg & EVERGREEN_CRTC_MASTER_EN)
633bcc1c2a1SAlex Deucher 			return true;
634bcc1c2a1SAlex Deucher 	} else if (ASIC_IS_AVIVO(rdev)) {
635771fe6b9SJerome Glisse 		reg = RREG32(AVIVO_D1CRTC_CONTROL) |
636771fe6b9SJerome Glisse 		      RREG32(AVIVO_D2CRTC_CONTROL);
637771fe6b9SJerome Glisse 		if (reg & AVIVO_CRTC_EN) {
638771fe6b9SJerome Glisse 			return true;
639771fe6b9SJerome Glisse 		}
640771fe6b9SJerome Glisse 	} else {
641771fe6b9SJerome Glisse 		reg = RREG32(RADEON_CRTC_GEN_CNTL) |
642771fe6b9SJerome Glisse 		      RREG32(RADEON_CRTC2_GEN_CNTL);
643771fe6b9SJerome Glisse 		if (reg & RADEON_CRTC_EN) {
644771fe6b9SJerome Glisse 			return true;
645771fe6b9SJerome Glisse 		}
646771fe6b9SJerome Glisse 	}
647771fe6b9SJerome Glisse 
6482cf3a4fcSAlex Deucher check_memsize:
649771fe6b9SJerome Glisse 	/* then check MEM_SIZE, in case the crtcs are off */
650771fe6b9SJerome Glisse 	if (rdev->family >= CHIP_R600)
651771fe6b9SJerome Glisse 		reg = RREG32(R600_CONFIG_MEMSIZE);
652771fe6b9SJerome Glisse 	else
653771fe6b9SJerome Glisse 		reg = RREG32(RADEON_CONFIG_MEMSIZE);
654771fe6b9SJerome Glisse 
655771fe6b9SJerome Glisse 	if (reg)
656771fe6b9SJerome Glisse 		return true;
657771fe6b9SJerome Glisse 
658771fe6b9SJerome Glisse 	return false;
659771fe6b9SJerome Glisse 
660771fe6b9SJerome Glisse }
661771fe6b9SJerome Glisse 
6620c195119SAlex Deucher /**
6630c195119SAlex Deucher  * radeon_update_bandwidth_info - update display bandwidth params
6640c195119SAlex Deucher  *
6650c195119SAlex Deucher  * @rdev: radeon_device pointer
6660c195119SAlex Deucher  *
6670c195119SAlex Deucher  * Used when sclk/mclk are switched or display modes are set.
6680c195119SAlex Deucher  * params are used to calculate display watermarks (all asics)
6690c195119SAlex Deucher  */
670f47299c5SAlex Deucher void radeon_update_bandwidth_info(struct radeon_device *rdev)
671f47299c5SAlex Deucher {
672f47299c5SAlex Deucher 	fixed20_12 a;
6738807286eSAlex Deucher 	u32 sclk = rdev->pm.current_sclk;
6748807286eSAlex Deucher 	u32 mclk = rdev->pm.current_mclk;
675f47299c5SAlex Deucher 
6768807286eSAlex Deucher 	/* sclk/mclk in Mhz */
67768adac5eSBen Skeggs 	a.full = dfixed_const(100);
67868adac5eSBen Skeggs 	rdev->pm.sclk.full = dfixed_const(sclk);
67968adac5eSBen Skeggs 	rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a);
68068adac5eSBen Skeggs 	rdev->pm.mclk.full = dfixed_const(mclk);
68168adac5eSBen Skeggs 	rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a);
682f47299c5SAlex Deucher 
6838807286eSAlex Deucher 	if (rdev->flags & RADEON_IS_IGP) {
68468adac5eSBen Skeggs 		a.full = dfixed_const(16);
685f47299c5SAlex Deucher 		/* core_bandwidth = sclk(Mhz) * 16 */
68668adac5eSBen Skeggs 		rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a);
687f47299c5SAlex Deucher 	}
688f47299c5SAlex Deucher }
689f47299c5SAlex Deucher 
6900c195119SAlex Deucher /**
6910c195119SAlex Deucher  * radeon_boot_test_post_card - check and possibly initialize the hw
6920c195119SAlex Deucher  *
6930c195119SAlex Deucher  * @rdev: radeon_device pointer
6940c195119SAlex Deucher  *
6950c195119SAlex Deucher  * Check if the asic is initialized and if not, attempt to initialize
6960c195119SAlex Deucher  * it (all asics).
6970c195119SAlex Deucher  * Returns true if initialized or false if not.
6980c195119SAlex Deucher  */
69972542d77SDave Airlie bool radeon_boot_test_post_card(struct radeon_device *rdev)
70072542d77SDave Airlie {
70172542d77SDave Airlie 	if (radeon_card_posted(rdev))
70272542d77SDave Airlie 		return true;
70372542d77SDave Airlie 
70472542d77SDave Airlie 	if (rdev->bios) {
70572542d77SDave Airlie 		DRM_INFO("GPU not posted. posting now...\n");
70672542d77SDave Airlie 		if (rdev->is_atom_bios)
70772542d77SDave Airlie 			atom_asic_init(rdev->mode_info.atom_context);
70872542d77SDave Airlie 		else
70972542d77SDave Airlie 			radeon_combios_asic_init(rdev->ddev);
71072542d77SDave Airlie 		return true;
71172542d77SDave Airlie 	} else {
71272542d77SDave Airlie 		dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
71372542d77SDave Airlie 		return false;
71472542d77SDave Airlie 	}
71572542d77SDave Airlie }
71672542d77SDave Airlie 
7170c195119SAlex Deucher /**
7180c195119SAlex Deucher  * radeon_dummy_page_init - init dummy page used by the driver
7190c195119SAlex Deucher  *
7200c195119SAlex Deucher  * @rdev: radeon_device pointer
7210c195119SAlex Deucher  *
7220c195119SAlex Deucher  * Allocate the dummy page used by the driver (all asics).
7230c195119SAlex Deucher  * This dummy page is used by the driver as a filler for gart entries
7240c195119SAlex Deucher  * when pages are taken out of the GART
7250c195119SAlex Deucher  * Returns 0 on sucess, -ENOMEM on failure.
7260c195119SAlex Deucher  */
7273ce0a23dSJerome Glisse int radeon_dummy_page_init(struct radeon_device *rdev)
7283ce0a23dSJerome Glisse {
72982568565SDave Airlie 	if (rdev->dummy_page.page)
73082568565SDave Airlie 		return 0;
7313ce0a23dSJerome Glisse 	rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
7323ce0a23dSJerome Glisse 	if (rdev->dummy_page.page == NULL)
7333ce0a23dSJerome Glisse 		return -ENOMEM;
7343ce0a23dSJerome Glisse 	rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page,
7353ce0a23dSJerome Glisse 					0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
736a30f6fb7SBenjamin Herrenschmidt 	if (pci_dma_mapping_error(rdev->pdev, rdev->dummy_page.addr)) {
737a30f6fb7SBenjamin Herrenschmidt 		dev_err(&rdev->pdev->dev, "Failed to DMA MAP the dummy page\n");
7383ce0a23dSJerome Glisse 		__free_page(rdev->dummy_page.page);
7393ce0a23dSJerome Glisse 		rdev->dummy_page.page = NULL;
7403ce0a23dSJerome Glisse 		return -ENOMEM;
7413ce0a23dSJerome Glisse 	}
7423ce0a23dSJerome Glisse 	return 0;
7433ce0a23dSJerome Glisse }
7443ce0a23dSJerome Glisse 
7450c195119SAlex Deucher /**
7460c195119SAlex Deucher  * radeon_dummy_page_fini - free dummy page used by the driver
7470c195119SAlex Deucher  *
7480c195119SAlex Deucher  * @rdev: radeon_device pointer
7490c195119SAlex Deucher  *
7500c195119SAlex Deucher  * Frees the dummy page used by the driver (all asics).
7510c195119SAlex Deucher  */
7523ce0a23dSJerome Glisse void radeon_dummy_page_fini(struct radeon_device *rdev)
7533ce0a23dSJerome Glisse {
7543ce0a23dSJerome Glisse 	if (rdev->dummy_page.page == NULL)
7553ce0a23dSJerome Glisse 		return;
7563ce0a23dSJerome Glisse 	pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
7573ce0a23dSJerome Glisse 			PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
7583ce0a23dSJerome Glisse 	__free_page(rdev->dummy_page.page);
7593ce0a23dSJerome Glisse 	rdev->dummy_page.page = NULL;
7603ce0a23dSJerome Glisse }
7613ce0a23dSJerome Glisse 
762771fe6b9SJerome Glisse 
763771fe6b9SJerome Glisse /* ATOM accessor methods */
7640c195119SAlex Deucher /*
7650c195119SAlex Deucher  * ATOM is an interpreted byte code stored in tables in the vbios.  The
7660c195119SAlex Deucher  * driver registers callbacks to access registers and the interpreter
7670c195119SAlex Deucher  * in the driver parses the tables and executes then to program specific
7680c195119SAlex Deucher  * actions (set display modes, asic init, etc.).  See radeon_atombios.c,
7690c195119SAlex Deucher  * atombios.h, and atom.c
7700c195119SAlex Deucher  */
7710c195119SAlex Deucher 
7720c195119SAlex Deucher /**
7730c195119SAlex Deucher  * cail_pll_read - read PLL register
7740c195119SAlex Deucher  *
7750c195119SAlex Deucher  * @info: atom card_info pointer
7760c195119SAlex Deucher  * @reg: PLL register offset
7770c195119SAlex Deucher  *
7780c195119SAlex Deucher  * Provides a PLL register accessor for the atom interpreter (r4xx+).
7790c195119SAlex Deucher  * Returns the value of the PLL register.
7800c195119SAlex Deucher  */
781771fe6b9SJerome Glisse static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
782771fe6b9SJerome Glisse {
783771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
784771fe6b9SJerome Glisse 	uint32_t r;
785771fe6b9SJerome Glisse 
786771fe6b9SJerome Glisse 	r = rdev->pll_rreg(rdev, reg);
787771fe6b9SJerome Glisse 	return r;
788771fe6b9SJerome Glisse }
789771fe6b9SJerome Glisse 
7900c195119SAlex Deucher /**
7910c195119SAlex Deucher  * cail_pll_write - write PLL register
7920c195119SAlex Deucher  *
7930c195119SAlex Deucher  * @info: atom card_info pointer
7940c195119SAlex Deucher  * @reg: PLL register offset
7950c195119SAlex Deucher  * @val: value to write to the pll register
7960c195119SAlex Deucher  *
7970c195119SAlex Deucher  * Provides a PLL register accessor for the atom interpreter (r4xx+).
7980c195119SAlex Deucher  */
799771fe6b9SJerome Glisse static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
800771fe6b9SJerome Glisse {
801771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
802771fe6b9SJerome Glisse 
803771fe6b9SJerome Glisse 	rdev->pll_wreg(rdev, reg, val);
804771fe6b9SJerome Glisse }
805771fe6b9SJerome Glisse 
8060c195119SAlex Deucher /**
8070c195119SAlex Deucher  * cail_mc_read - read MC (Memory Controller) register
8080c195119SAlex Deucher  *
8090c195119SAlex Deucher  * @info: atom card_info pointer
8100c195119SAlex Deucher  * @reg: MC register offset
8110c195119SAlex Deucher  *
8120c195119SAlex Deucher  * Provides an MC register accessor for the atom interpreter (r4xx+).
8130c195119SAlex Deucher  * Returns the value of the MC register.
8140c195119SAlex Deucher  */
815771fe6b9SJerome Glisse static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
816771fe6b9SJerome Glisse {
817771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
818771fe6b9SJerome Glisse 	uint32_t r;
819771fe6b9SJerome Glisse 
820771fe6b9SJerome Glisse 	r = rdev->mc_rreg(rdev, reg);
821771fe6b9SJerome Glisse 	return r;
822771fe6b9SJerome Glisse }
823771fe6b9SJerome Glisse 
8240c195119SAlex Deucher /**
8250c195119SAlex Deucher  * cail_mc_write - write MC (Memory Controller) register
8260c195119SAlex Deucher  *
8270c195119SAlex Deucher  * @info: atom card_info pointer
8280c195119SAlex Deucher  * @reg: MC register offset
8290c195119SAlex Deucher  * @val: value to write to the pll register
8300c195119SAlex Deucher  *
8310c195119SAlex Deucher  * Provides a MC register accessor for the atom interpreter (r4xx+).
8320c195119SAlex Deucher  */
833771fe6b9SJerome Glisse static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
834771fe6b9SJerome Glisse {
835771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
836771fe6b9SJerome Glisse 
837771fe6b9SJerome Glisse 	rdev->mc_wreg(rdev, reg, val);
838771fe6b9SJerome Glisse }
839771fe6b9SJerome Glisse 
8400c195119SAlex Deucher /**
8410c195119SAlex Deucher  * cail_reg_write - write MMIO register
8420c195119SAlex Deucher  *
8430c195119SAlex Deucher  * @info: atom card_info pointer
8440c195119SAlex Deucher  * @reg: MMIO register offset
8450c195119SAlex Deucher  * @val: value to write to the pll register
8460c195119SAlex Deucher  *
8470c195119SAlex Deucher  * Provides a MMIO register accessor for the atom interpreter (r4xx+).
8480c195119SAlex Deucher  */
849771fe6b9SJerome Glisse static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
850771fe6b9SJerome Glisse {
851771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
852771fe6b9SJerome Glisse 
853771fe6b9SJerome Glisse 	WREG32(reg*4, val);
854771fe6b9SJerome Glisse }
855771fe6b9SJerome Glisse 
8560c195119SAlex Deucher /**
8570c195119SAlex Deucher  * cail_reg_read - read MMIO register
8580c195119SAlex Deucher  *
8590c195119SAlex Deucher  * @info: atom card_info pointer
8600c195119SAlex Deucher  * @reg: MMIO register offset
8610c195119SAlex Deucher  *
8620c195119SAlex Deucher  * Provides an MMIO register accessor for the atom interpreter (r4xx+).
8630c195119SAlex Deucher  * Returns the value of the MMIO register.
8640c195119SAlex Deucher  */
865771fe6b9SJerome Glisse static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
866771fe6b9SJerome Glisse {
867771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
868771fe6b9SJerome Glisse 	uint32_t r;
869771fe6b9SJerome Glisse 
870771fe6b9SJerome Glisse 	r = RREG32(reg*4);
871771fe6b9SJerome Glisse 	return r;
872771fe6b9SJerome Glisse }
873771fe6b9SJerome Glisse 
8740c195119SAlex Deucher /**
8750c195119SAlex Deucher  * cail_ioreg_write - write IO register
8760c195119SAlex Deucher  *
8770c195119SAlex Deucher  * @info: atom card_info pointer
8780c195119SAlex Deucher  * @reg: IO register offset
8790c195119SAlex Deucher  * @val: value to write to the pll register
8800c195119SAlex Deucher  *
8810c195119SAlex Deucher  * Provides a IO register accessor for the atom interpreter (r4xx+).
8820c195119SAlex Deucher  */
883351a52a2SAlex Deucher static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
884351a52a2SAlex Deucher {
885351a52a2SAlex Deucher 	struct radeon_device *rdev = info->dev->dev_private;
886351a52a2SAlex Deucher 
887351a52a2SAlex Deucher 	WREG32_IO(reg*4, val);
888351a52a2SAlex Deucher }
889351a52a2SAlex Deucher 
8900c195119SAlex Deucher /**
8910c195119SAlex Deucher  * cail_ioreg_read - read IO register
8920c195119SAlex Deucher  *
8930c195119SAlex Deucher  * @info: atom card_info pointer
8940c195119SAlex Deucher  * @reg: IO register offset
8950c195119SAlex Deucher  *
8960c195119SAlex Deucher  * Provides an IO register accessor for the atom interpreter (r4xx+).
8970c195119SAlex Deucher  * Returns the value of the IO register.
8980c195119SAlex Deucher  */
899351a52a2SAlex Deucher static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
900351a52a2SAlex Deucher {
901351a52a2SAlex Deucher 	struct radeon_device *rdev = info->dev->dev_private;
902351a52a2SAlex Deucher 	uint32_t r;
903351a52a2SAlex Deucher 
904351a52a2SAlex Deucher 	r = RREG32_IO(reg*4);
905351a52a2SAlex Deucher 	return r;
906351a52a2SAlex Deucher }
907351a52a2SAlex Deucher 
9080c195119SAlex Deucher /**
9090c195119SAlex Deucher  * radeon_atombios_init - init the driver info and callbacks for atombios
9100c195119SAlex Deucher  *
9110c195119SAlex Deucher  * @rdev: radeon_device pointer
9120c195119SAlex Deucher  *
9130c195119SAlex Deucher  * Initializes the driver info and register access callbacks for the
9140c195119SAlex Deucher  * ATOM interpreter (r4xx+).
9150c195119SAlex Deucher  * Returns 0 on sucess, -ENOMEM on failure.
9160c195119SAlex Deucher  * Called at driver startup.
9170c195119SAlex Deucher  */
918771fe6b9SJerome Glisse int radeon_atombios_init(struct radeon_device *rdev)
919771fe6b9SJerome Glisse {
92061c4b24bSMathias Fröhlich 	struct card_info *atom_card_info =
92161c4b24bSMathias Fröhlich 	    kzalloc(sizeof(struct card_info), GFP_KERNEL);
92261c4b24bSMathias Fröhlich 
92361c4b24bSMathias Fröhlich 	if (!atom_card_info)
92461c4b24bSMathias Fröhlich 		return -ENOMEM;
92561c4b24bSMathias Fröhlich 
92661c4b24bSMathias Fröhlich 	rdev->mode_info.atom_card_info = atom_card_info;
92761c4b24bSMathias Fröhlich 	atom_card_info->dev = rdev->ddev;
92861c4b24bSMathias Fröhlich 	atom_card_info->reg_read = cail_reg_read;
92961c4b24bSMathias Fröhlich 	atom_card_info->reg_write = cail_reg_write;
930351a52a2SAlex Deucher 	/* needed for iio ops */
931351a52a2SAlex Deucher 	if (rdev->rio_mem) {
932351a52a2SAlex Deucher 		atom_card_info->ioreg_read = cail_ioreg_read;
933351a52a2SAlex Deucher 		atom_card_info->ioreg_write = cail_ioreg_write;
934351a52a2SAlex Deucher 	} else {
935351a52a2SAlex Deucher 		DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
936351a52a2SAlex Deucher 		atom_card_info->ioreg_read = cail_reg_read;
937351a52a2SAlex Deucher 		atom_card_info->ioreg_write = cail_reg_write;
938351a52a2SAlex Deucher 	}
93961c4b24bSMathias Fröhlich 	atom_card_info->mc_read = cail_mc_read;
94061c4b24bSMathias Fröhlich 	atom_card_info->mc_write = cail_mc_write;
94161c4b24bSMathias Fröhlich 	atom_card_info->pll_read = cail_pll_read;
94261c4b24bSMathias Fröhlich 	atom_card_info->pll_write = cail_pll_write;
94361c4b24bSMathias Fröhlich 
94461c4b24bSMathias Fröhlich 	rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
9450e34d094STim Gardner 	if (!rdev->mode_info.atom_context) {
9460e34d094STim Gardner 		radeon_atombios_fini(rdev);
9470e34d094STim Gardner 		return -ENOMEM;
9480e34d094STim Gardner 	}
9490e34d094STim Gardner 
950c31ad97fSRafał Miłecki 	mutex_init(&rdev->mode_info.atom_context->mutex);
951771fe6b9SJerome Glisse 	radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
952d904ef9bSDave Airlie 	atom_allocate_fb_scratch(rdev->mode_info.atom_context);
953771fe6b9SJerome Glisse 	return 0;
954771fe6b9SJerome Glisse }
955771fe6b9SJerome Glisse 
9560c195119SAlex Deucher /**
9570c195119SAlex Deucher  * radeon_atombios_fini - free the driver info and callbacks for atombios
9580c195119SAlex Deucher  *
9590c195119SAlex Deucher  * @rdev: radeon_device pointer
9600c195119SAlex Deucher  *
9610c195119SAlex Deucher  * Frees the driver info and register access callbacks for the ATOM
9620c195119SAlex Deucher  * interpreter (r4xx+).
9630c195119SAlex Deucher  * Called at driver shutdown.
9640c195119SAlex Deucher  */
965771fe6b9SJerome Glisse void radeon_atombios_fini(struct radeon_device *rdev)
966771fe6b9SJerome Glisse {
9674a04a844SJerome Glisse 	if (rdev->mode_info.atom_context) {
968d904ef9bSDave Airlie 		kfree(rdev->mode_info.atom_context->scratch);
9694a04a844SJerome Glisse 	}
9700e34d094STim Gardner 	kfree(rdev->mode_info.atom_context);
9710e34d094STim Gardner 	rdev->mode_info.atom_context = NULL;
97261c4b24bSMathias Fröhlich 	kfree(rdev->mode_info.atom_card_info);
9730e34d094STim Gardner 	rdev->mode_info.atom_card_info = NULL;
974771fe6b9SJerome Glisse }
975771fe6b9SJerome Glisse 
9760c195119SAlex Deucher /* COMBIOS */
9770c195119SAlex Deucher /*
9780c195119SAlex Deucher  * COMBIOS is the bios format prior to ATOM. It provides
9790c195119SAlex Deucher  * command tables similar to ATOM, but doesn't have a unified
9800c195119SAlex Deucher  * parser.  See radeon_combios.c
9810c195119SAlex Deucher  */
9820c195119SAlex Deucher 
9830c195119SAlex Deucher /**
9840c195119SAlex Deucher  * radeon_combios_init - init the driver info for combios
9850c195119SAlex Deucher  *
9860c195119SAlex Deucher  * @rdev: radeon_device pointer
9870c195119SAlex Deucher  *
9880c195119SAlex Deucher  * Initializes the driver info for combios (r1xx-r3xx).
9890c195119SAlex Deucher  * Returns 0 on sucess.
9900c195119SAlex Deucher  * Called at driver startup.
9910c195119SAlex Deucher  */
992771fe6b9SJerome Glisse int radeon_combios_init(struct radeon_device *rdev)
993771fe6b9SJerome Glisse {
994771fe6b9SJerome Glisse 	radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
995771fe6b9SJerome Glisse 	return 0;
996771fe6b9SJerome Glisse }
997771fe6b9SJerome Glisse 
9980c195119SAlex Deucher /**
9990c195119SAlex Deucher  * radeon_combios_fini - free the driver info for combios
10000c195119SAlex Deucher  *
10010c195119SAlex Deucher  * @rdev: radeon_device pointer
10020c195119SAlex Deucher  *
10030c195119SAlex Deucher  * Frees the driver info for combios (r1xx-r3xx).
10040c195119SAlex Deucher  * Called at driver shutdown.
10050c195119SAlex Deucher  */
1006771fe6b9SJerome Glisse void radeon_combios_fini(struct radeon_device *rdev)
1007771fe6b9SJerome Glisse {
1008771fe6b9SJerome Glisse }
1009771fe6b9SJerome Glisse 
10100c195119SAlex Deucher /* if we get transitioned to only one device, take VGA back */
10110c195119SAlex Deucher /**
10120c195119SAlex Deucher  * radeon_vga_set_decode - enable/disable vga decode
10130c195119SAlex Deucher  *
10140c195119SAlex Deucher  * @cookie: radeon_device pointer
10150c195119SAlex Deucher  * @state: enable/disable vga decode
10160c195119SAlex Deucher  *
10170c195119SAlex Deucher  * Enable/disable vga decode (all asics).
10180c195119SAlex Deucher  * Returns VGA resource flags.
10190c195119SAlex Deucher  */
102028d52043SDave Airlie static unsigned int radeon_vga_set_decode(void *cookie, bool state)
102128d52043SDave Airlie {
102228d52043SDave Airlie 	struct radeon_device *rdev = cookie;
102328d52043SDave Airlie 	radeon_vga_set_state(rdev, state);
102428d52043SDave Airlie 	if (state)
102528d52043SDave Airlie 		return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
102628d52043SDave Airlie 		       VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
102728d52043SDave Airlie 	else
102828d52043SDave Airlie 		return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
102928d52043SDave Airlie }
1030c1176d6fSDave Airlie 
10310c195119SAlex Deucher /**
10321bcb04f7SChristian König  * radeon_check_pot_argument - check that argument is a power of two
10331bcb04f7SChristian König  *
10341bcb04f7SChristian König  * @arg: value to check
10351bcb04f7SChristian König  *
10361bcb04f7SChristian König  * Validates that a certain argument is a power of two (all asics).
10371bcb04f7SChristian König  * Returns true if argument is valid.
10381bcb04f7SChristian König  */
10391bcb04f7SChristian König static bool radeon_check_pot_argument(int arg)
10401bcb04f7SChristian König {
10411bcb04f7SChristian König 	return (arg & (arg - 1)) == 0;
10421bcb04f7SChristian König }
10431bcb04f7SChristian König 
10441bcb04f7SChristian König /**
10450c195119SAlex Deucher  * radeon_check_arguments - validate module params
10460c195119SAlex Deucher  *
10470c195119SAlex Deucher  * @rdev: radeon_device pointer
10480c195119SAlex Deucher  *
10490c195119SAlex Deucher  * Validates certain module parameters and updates
10500c195119SAlex Deucher  * the associated values used by the driver (all asics).
10510c195119SAlex Deucher  */
10521109ca09SLauri Kasanen static void radeon_check_arguments(struct radeon_device *rdev)
105336421338SJerome Glisse {
105436421338SJerome Glisse 	/* vramlimit must be a power of two */
10551bcb04f7SChristian König 	if (!radeon_check_pot_argument(radeon_vram_limit)) {
105636421338SJerome Glisse 		dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n",
105736421338SJerome Glisse 				radeon_vram_limit);
105836421338SJerome Glisse 		radeon_vram_limit = 0;
105936421338SJerome Glisse 	}
10601bcb04f7SChristian König 
1061edcd26e8SAlex Deucher 	if (radeon_gart_size == -1) {
1062edcd26e8SAlex Deucher 		/* default to a larger gart size on newer asics */
1063edcd26e8SAlex Deucher 		if (rdev->family >= CHIP_RV770)
1064edcd26e8SAlex Deucher 			radeon_gart_size = 1024;
1065edcd26e8SAlex Deucher 		else
1066edcd26e8SAlex Deucher 			radeon_gart_size = 512;
1067edcd26e8SAlex Deucher 	}
106836421338SJerome Glisse 	/* gtt size must be power of two and greater or equal to 32M */
10691bcb04f7SChristian König 	if (radeon_gart_size < 32) {
1070edcd26e8SAlex Deucher 		dev_warn(rdev->dev, "gart size (%d) too small\n",
107136421338SJerome Glisse 				radeon_gart_size);
1072edcd26e8SAlex Deucher 		if (rdev->family >= CHIP_RV770)
1073edcd26e8SAlex Deucher 			radeon_gart_size = 1024;
1074edcd26e8SAlex Deucher 		else
107536421338SJerome Glisse 			radeon_gart_size = 512;
10761bcb04f7SChristian König 	} else if (!radeon_check_pot_argument(radeon_gart_size)) {
107736421338SJerome Glisse 		dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
107836421338SJerome Glisse 				radeon_gart_size);
1079edcd26e8SAlex Deucher 		if (rdev->family >= CHIP_RV770)
1080edcd26e8SAlex Deucher 			radeon_gart_size = 1024;
1081edcd26e8SAlex Deucher 		else
108236421338SJerome Glisse 			radeon_gart_size = 512;
108336421338SJerome Glisse 	}
10841bcb04f7SChristian König 	rdev->mc.gtt_size = (uint64_t)radeon_gart_size << 20;
10851bcb04f7SChristian König 
108636421338SJerome Glisse 	/* AGP mode can only be -1, 1, 2, 4, 8 */
108736421338SJerome Glisse 	switch (radeon_agpmode) {
108836421338SJerome Glisse 	case -1:
108936421338SJerome Glisse 	case 0:
109036421338SJerome Glisse 	case 1:
109136421338SJerome Glisse 	case 2:
109236421338SJerome Glisse 	case 4:
109336421338SJerome Glisse 	case 8:
109436421338SJerome Glisse 		break;
109536421338SJerome Glisse 	default:
109636421338SJerome Glisse 		dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
109736421338SJerome Glisse 				"-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
109836421338SJerome Glisse 		radeon_agpmode = 0;
109936421338SJerome Glisse 		break;
110036421338SJerome Glisse 	}
1101c1c44132SChristian König 
1102c1c44132SChristian König 	if (!radeon_check_pot_argument(radeon_vm_size)) {
1103c1c44132SChristian König 		dev_warn(rdev->dev, "VM size (%d) must be a power of 2\n",
1104c1c44132SChristian König 			 radeon_vm_size);
110520b2656dSChristian König 		radeon_vm_size = 4;
1106c1c44132SChristian König 	}
1107c1c44132SChristian König 
110820b2656dSChristian König 	if (radeon_vm_size < 1) {
110920b2656dSChristian König 		dev_warn(rdev->dev, "VM size (%d) to small, min is 1GB\n",
1110c1c44132SChristian König 			 radeon_vm_size);
111120b2656dSChristian König 		radeon_vm_size = 4;
1112c1c44132SChristian König 	}
1113c1c44132SChristian König 
1114c1c44132SChristian König        /*
1115c1c44132SChristian König         * Max GPUVM size for Cayman, SI and CI are 40 bits.
1116c1c44132SChristian König         */
111720b2656dSChristian König 	if (radeon_vm_size > 1024) {
111820b2656dSChristian König 		dev_warn(rdev->dev, "VM size (%d) too large, max is 1TB\n",
1119c1c44132SChristian König 			 radeon_vm_size);
112020b2656dSChristian König 		radeon_vm_size = 4;
1121c1c44132SChristian König 	}
11224510fb98SChristian König 
11234510fb98SChristian König 	/* defines number of bits in page table versus page directory,
11244510fb98SChristian König 	 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
11254510fb98SChristian König 	 * page table and the remaining bits are in the page directory */
1126dfc230f9SChristian König 	if (radeon_vm_block_size == -1) {
1127dfc230f9SChristian König 
1128dfc230f9SChristian König 		/* Total bits covered by PD + PTs */
1129dfc230f9SChristian König 		unsigned bits = ilog2(radeon_vm_size) + 17;
1130dfc230f9SChristian König 
1131dfc230f9SChristian König 		/* Make sure the PD is 4K in size up to 8GB address space.
1132dfc230f9SChristian König 		   Above that split equal between PD and PTs */
1133dfc230f9SChristian König 		if (radeon_vm_size <= 8)
1134dfc230f9SChristian König 			radeon_vm_block_size = bits - 9;
1135dfc230f9SChristian König 		else
1136dfc230f9SChristian König 			radeon_vm_block_size = (bits + 3) / 2;
1137dfc230f9SChristian König 
1138dfc230f9SChristian König 	} else if (radeon_vm_block_size < 9) {
113920b2656dSChristian König 		dev_warn(rdev->dev, "VM page table size (%d) too small\n",
11404510fb98SChristian König 			 radeon_vm_block_size);
11414510fb98SChristian König 		radeon_vm_block_size = 9;
11424510fb98SChristian König 	}
11434510fb98SChristian König 
11444510fb98SChristian König 	if (radeon_vm_block_size > 24 ||
114520b2656dSChristian König 	    (radeon_vm_size * 1024) < (1ull << radeon_vm_block_size)) {
114620b2656dSChristian König 		dev_warn(rdev->dev, "VM page table size (%d) too large\n",
11474510fb98SChristian König 			 radeon_vm_block_size);
11484510fb98SChristian König 		radeon_vm_block_size = 9;
11494510fb98SChristian König 	}
115036421338SJerome Glisse }
115136421338SJerome Glisse 
11520c195119SAlex Deucher /**
11530c195119SAlex Deucher  * radeon_switcheroo_set_state - set switcheroo state
11540c195119SAlex Deucher  *
11550c195119SAlex Deucher  * @pdev: pci dev pointer
11560c195119SAlex Deucher  * @state: vga switcheroo state
11570c195119SAlex Deucher  *
11580c195119SAlex Deucher  * Callback for the switcheroo driver.  Suspends or resumes the
11590c195119SAlex Deucher  * the asics before or after it is powered up using ACPI methods.
11600c195119SAlex Deucher  */
11616a9ee8afSDave Airlie static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
11626a9ee8afSDave Airlie {
11636a9ee8afSDave Airlie 	struct drm_device *dev = pci_get_drvdata(pdev);
11644807c5a8SAlex Deucher 	struct radeon_device *rdev = dev->dev_private;
116510ebc0bcSDave Airlie 
116690c4cde9SAlex Deucher 	if (radeon_is_px(dev) && state == VGA_SWITCHEROO_OFF)
116710ebc0bcSDave Airlie 		return;
116810ebc0bcSDave Airlie 
11696a9ee8afSDave Airlie 	if (state == VGA_SWITCHEROO_ON) {
1170d1f9809eSMaarten Lankhorst 		unsigned d3_delay = dev->pdev->d3_delay;
1171d1f9809eSMaarten Lankhorst 
11726a9ee8afSDave Airlie 		printk(KERN_INFO "radeon: switched on\n");
11736a9ee8afSDave Airlie 		/* don't suspend or resume card normally */
11745bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1175d1f9809eSMaarten Lankhorst 
11764807c5a8SAlex Deucher 		if (d3_delay < 20 && (rdev->px_quirk_flags & RADEON_PX_QUIRK_LONG_WAKEUP))
1177d1f9809eSMaarten Lankhorst 			dev->pdev->d3_delay = 20;
1178d1f9809eSMaarten Lankhorst 
117910ebc0bcSDave Airlie 		radeon_resume_kms(dev, true, true);
1180d1f9809eSMaarten Lankhorst 
1181d1f9809eSMaarten Lankhorst 		dev->pdev->d3_delay = d3_delay;
1182d1f9809eSMaarten Lankhorst 
11835bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_ON;
1184fbf81762SDave Airlie 		drm_kms_helper_poll_enable(dev);
11856a9ee8afSDave Airlie 	} else {
11866a9ee8afSDave Airlie 		printk(KERN_INFO "radeon: switched off\n");
1187fbf81762SDave Airlie 		drm_kms_helper_poll_disable(dev);
11885bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
118910ebc0bcSDave Airlie 		radeon_suspend_kms(dev, true, true);
11905bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_OFF;
11916a9ee8afSDave Airlie 	}
11926a9ee8afSDave Airlie }
11936a9ee8afSDave Airlie 
11940c195119SAlex Deucher /**
11950c195119SAlex Deucher  * radeon_switcheroo_can_switch - see if switcheroo state can change
11960c195119SAlex Deucher  *
11970c195119SAlex Deucher  * @pdev: pci dev pointer
11980c195119SAlex Deucher  *
11990c195119SAlex Deucher  * Callback for the switcheroo driver.  Check of the switcheroo
12000c195119SAlex Deucher  * state can be changed.
12010c195119SAlex Deucher  * Returns true if the state can be changed, false if not.
12020c195119SAlex Deucher  */
12036a9ee8afSDave Airlie static bool radeon_switcheroo_can_switch(struct pci_dev *pdev)
12046a9ee8afSDave Airlie {
12056a9ee8afSDave Airlie 	struct drm_device *dev = pci_get_drvdata(pdev);
12066a9ee8afSDave Airlie 
1207fc8fd40eSDaniel Vetter 	/*
1208fc8fd40eSDaniel Vetter 	 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1209fc8fd40eSDaniel Vetter 	 * locking inversion with the driver load path. And the access here is
1210fc8fd40eSDaniel Vetter 	 * completely racy anyway. So don't bother with locking for now.
1211fc8fd40eSDaniel Vetter 	 */
1212fc8fd40eSDaniel Vetter 	return dev->open_count == 0;
12136a9ee8afSDave Airlie }
12146a9ee8afSDave Airlie 
121526ec685fSTakashi Iwai static const struct vga_switcheroo_client_ops radeon_switcheroo_ops = {
121626ec685fSTakashi Iwai 	.set_gpu_state = radeon_switcheroo_set_state,
121726ec685fSTakashi Iwai 	.reprobe = NULL,
121826ec685fSTakashi Iwai 	.can_switch = radeon_switcheroo_can_switch,
121926ec685fSTakashi Iwai };
12206a9ee8afSDave Airlie 
12210c195119SAlex Deucher /**
12220c195119SAlex Deucher  * radeon_device_init - initialize the driver
12230c195119SAlex Deucher  *
12240c195119SAlex Deucher  * @rdev: radeon_device pointer
12250c195119SAlex Deucher  * @pdev: drm dev pointer
12260c195119SAlex Deucher  * @pdev: pci dev pointer
12270c195119SAlex Deucher  * @flags: driver flags
12280c195119SAlex Deucher  *
12290c195119SAlex Deucher  * Initializes the driver info and hw (all asics).
12300c195119SAlex Deucher  * Returns 0 for success or an error on failure.
12310c195119SAlex Deucher  * Called at driver startup.
12320c195119SAlex Deucher  */
1233771fe6b9SJerome Glisse int radeon_device_init(struct radeon_device *rdev,
1234771fe6b9SJerome Glisse 		       struct drm_device *ddev,
1235771fe6b9SJerome Glisse 		       struct pci_dev *pdev,
1236771fe6b9SJerome Glisse 		       uint32_t flags)
1237771fe6b9SJerome Glisse {
1238351a52a2SAlex Deucher 	int r, i;
1239ad49f501SDave Airlie 	int dma_bits;
124010ebc0bcSDave Airlie 	bool runtime = false;
1241771fe6b9SJerome Glisse 
1242771fe6b9SJerome Glisse 	rdev->shutdown = false;
12439f022ddfSJerome Glisse 	rdev->dev = &pdev->dev;
1244771fe6b9SJerome Glisse 	rdev->ddev = ddev;
1245771fe6b9SJerome Glisse 	rdev->pdev = pdev;
1246771fe6b9SJerome Glisse 	rdev->flags = flags;
1247771fe6b9SJerome Glisse 	rdev->family = flags & RADEON_FAMILY_MASK;
1248771fe6b9SJerome Glisse 	rdev->is_atom_bios = false;
1249771fe6b9SJerome Glisse 	rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
1250edcd26e8SAlex Deucher 	rdev->mc.gtt_size = 512 * 1024 * 1024;
1251733289c2SJerome Glisse 	rdev->accel_working = false;
12528b25ed34SAlex Deucher 	/* set up ring ids */
12538b25ed34SAlex Deucher 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
12548b25ed34SAlex Deucher 		rdev->ring[i].idx = i;
12558b25ed34SAlex Deucher 	}
12561b5331d9SJerome Glisse 
1257d522d9ccSThomas Reim 	DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X).\n",
1258d522d9ccSThomas Reim 		radeon_family_name[rdev->family], pdev->vendor, pdev->device,
1259d522d9ccSThomas Reim 		pdev->subsystem_vendor, pdev->subsystem_device);
12601b5331d9SJerome Glisse 
1261771fe6b9SJerome Glisse 	/* mutex initialization are all done here so we
1262771fe6b9SJerome Glisse 	 * can recall function without having locking issues */
1263d6999bc7SChristian König 	mutex_init(&rdev->ring_lock);
126440bacf16SAlex Deucher 	mutex_init(&rdev->dc_hw_i2c_mutex);
1265c20dc369SChristian Koenig 	atomic_set(&rdev->ih.lock, 0);
12664c788679SJerome Glisse 	mutex_init(&rdev->gem.mutex);
1267c913e23aSRafał Miłecki 	mutex_init(&rdev->pm.mutex);
12686759a0a7SMarek Olšák 	mutex_init(&rdev->gpu_clock_mutex);
1269f61d5b46SAlex Deucher 	mutex_init(&rdev->srbm_mutex);
1270db7fce39SChristian König 	init_rwsem(&rdev->pm.mclk_lock);
1271dee53e7fSJerome Glisse 	init_rwsem(&rdev->exclusive_lock);
127273a6d3fcSRafał Miłecki 	init_waitqueue_head(&rdev->irq.vblank_queue);
1273341cb9e4SChristian König 	mutex_init(&rdev->mn_lock);
1274341cb9e4SChristian König 	hash_init(rdev->mn_hash);
12751b9c3dd0SAlex Deucher 	r = radeon_gem_init(rdev);
12761b9c3dd0SAlex Deucher 	if (r)
12771b9c3dd0SAlex Deucher 		return r;
1278529364e0SChristian König 
1279c1c44132SChristian König 	radeon_check_arguments(rdev);
128023d4f1f2SAlex Deucher 	/* Adjust VM size here.
1281c1c44132SChristian König 	 * Max GPUVM size for cayman+ is 40 bits.
128223d4f1f2SAlex Deucher 	 */
128320b2656dSChristian König 	rdev->vm_manager.max_pfn = radeon_vm_size << 18;
1284771fe6b9SJerome Glisse 
12854aac0473SJerome Glisse 	/* Set asic functions */
12864aac0473SJerome Glisse 	r = radeon_asic_init(rdev);
128736421338SJerome Glisse 	if (r)
12884aac0473SJerome Glisse 		return r;
12894aac0473SJerome Glisse 
1290f95df9caSAlex Deucher 	/* all of the newer IGP chips have an internal gart
1291f95df9caSAlex Deucher 	 * However some rs4xx report as AGP, so remove that here.
1292f95df9caSAlex Deucher 	 */
1293f95df9caSAlex Deucher 	if ((rdev->family >= CHIP_RS400) &&
1294f95df9caSAlex Deucher 	    (rdev->flags & RADEON_IS_IGP)) {
1295f95df9caSAlex Deucher 		rdev->flags &= ~RADEON_IS_AGP;
1296f95df9caSAlex Deucher 	}
1297f95df9caSAlex Deucher 
129830256a3fSJerome Glisse 	if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
1299b574f251SJerome Glisse 		radeon_agp_disable(rdev);
1300771fe6b9SJerome Glisse 	}
1301771fe6b9SJerome Glisse 
13029ed8b1f9SAlex Deucher 	/* Set the internal MC address mask
13039ed8b1f9SAlex Deucher 	 * This is the max address of the GPU's
13049ed8b1f9SAlex Deucher 	 * internal address space.
13059ed8b1f9SAlex Deucher 	 */
13069ed8b1f9SAlex Deucher 	if (rdev->family >= CHIP_CAYMAN)
13079ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xffffffffffULL; /* 40 bit MC */
13089ed8b1f9SAlex Deucher 	else if (rdev->family >= CHIP_CEDAR)
13099ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xfffffffffULL; /* 36 bit MC */
13109ed8b1f9SAlex Deucher 	else
13119ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xffffffffULL; /* 32 bit MC */
13129ed8b1f9SAlex Deucher 
1313ad49f501SDave Airlie 	/* set DMA mask + need_dma32 flags.
1314ad49f501SDave Airlie 	 * PCIE - can handle 40-bits.
1315005a83f1SAlex Deucher 	 * IGP - can handle 40-bits
1316ad49f501SDave Airlie 	 * AGP - generally dma32 is safest
1317005a83f1SAlex Deucher 	 * PCI - dma32 for legacy pci gart, 40 bits on newer asics
1318ad49f501SDave Airlie 	 */
1319ad49f501SDave Airlie 	rdev->need_dma32 = false;
1320ad49f501SDave Airlie 	if (rdev->flags & RADEON_IS_AGP)
1321ad49f501SDave Airlie 		rdev->need_dma32 = true;
1322005a83f1SAlex Deucher 	if ((rdev->flags & RADEON_IS_PCI) &&
13234a2b6662SJerome Glisse 	    (rdev->family <= CHIP_RS740))
1324ad49f501SDave Airlie 		rdev->need_dma32 = true;
1325ad49f501SDave Airlie 
1326ad49f501SDave Airlie 	dma_bits = rdev->need_dma32 ? 32 : 40;
1327ad49f501SDave Airlie 	r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
1328771fe6b9SJerome Glisse 	if (r) {
132962fff811SDaniel Haid 		rdev->need_dma32 = true;
1330c52494f6SKonrad Rzeszutek Wilk 		dma_bits = 32;
1331771fe6b9SJerome Glisse 		printk(KERN_WARNING "radeon: No suitable DMA available.\n");
1332771fe6b9SJerome Glisse 	}
1333c52494f6SKonrad Rzeszutek Wilk 	r = pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
1334c52494f6SKonrad Rzeszutek Wilk 	if (r) {
1335c52494f6SKonrad Rzeszutek Wilk 		pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(32));
1336c52494f6SKonrad Rzeszutek Wilk 		printk(KERN_WARNING "radeon: No coherent DMA available.\n");
1337c52494f6SKonrad Rzeszutek Wilk 	}
1338771fe6b9SJerome Glisse 
1339771fe6b9SJerome Glisse 	/* Registers mapping */
1340771fe6b9SJerome Glisse 	/* TODO: block userspace mapping of io register */
13412c385151SDaniel Vetter 	spin_lock_init(&rdev->mmio_idx_lock);
1342fe78118cSAlex Deucher 	spin_lock_init(&rdev->smc_idx_lock);
13430a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pll_idx_lock);
13440a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->mc_idx_lock);
13450a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pcie_idx_lock);
13460a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pciep_idx_lock);
13470a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pif_idx_lock);
13480a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->cg_idx_lock);
13490a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->uvd_idx_lock);
13500a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->rcu_idx_lock);
13510a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->didt_idx_lock);
13520a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->end_idx_lock);
1353efad86dbSAlex Deucher 	if (rdev->family >= CHIP_BONAIRE) {
1354efad86dbSAlex Deucher 		rdev->rmmio_base = pci_resource_start(rdev->pdev, 5);
1355efad86dbSAlex Deucher 		rdev->rmmio_size = pci_resource_len(rdev->pdev, 5);
1356efad86dbSAlex Deucher 	} else {
135701d73a69SJordan Crouse 		rdev->rmmio_base = pci_resource_start(rdev->pdev, 2);
135801d73a69SJordan Crouse 		rdev->rmmio_size = pci_resource_len(rdev->pdev, 2);
1359efad86dbSAlex Deucher 	}
1360771fe6b9SJerome Glisse 	rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size);
1361771fe6b9SJerome Glisse 	if (rdev->rmmio == NULL) {
1362771fe6b9SJerome Glisse 		return -ENOMEM;
1363771fe6b9SJerome Glisse 	}
1364771fe6b9SJerome Glisse 	DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base);
1365771fe6b9SJerome Glisse 	DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size);
1366771fe6b9SJerome Glisse 
136775efdee1SAlex Deucher 	/* doorbell bar mapping */
136875efdee1SAlex Deucher 	if (rdev->family >= CHIP_BONAIRE)
136975efdee1SAlex Deucher 		radeon_doorbell_init(rdev);
137075efdee1SAlex Deucher 
1371351a52a2SAlex Deucher 	/* io port mapping */
1372351a52a2SAlex Deucher 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1373351a52a2SAlex Deucher 		if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) {
1374351a52a2SAlex Deucher 			rdev->rio_mem_size = pci_resource_len(rdev->pdev, i);
1375351a52a2SAlex Deucher 			rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size);
1376351a52a2SAlex Deucher 			break;
1377351a52a2SAlex Deucher 		}
1378351a52a2SAlex Deucher 	}
1379351a52a2SAlex Deucher 	if (rdev->rio_mem == NULL)
1380351a52a2SAlex Deucher 		DRM_ERROR("Unable to find PCI I/O BAR\n");
1381351a52a2SAlex Deucher 
13824807c5a8SAlex Deucher 	if (rdev->flags & RADEON_IS_PX)
13834807c5a8SAlex Deucher 		radeon_device_handle_px_quirks(rdev);
13844807c5a8SAlex Deucher 
138528d52043SDave Airlie 	/* if we have > 1 VGA cards, then disable the radeon VGA resources */
138693239ea1SDave Airlie 	/* this will fail for cards that aren't VGA class devices, just
138793239ea1SDave Airlie 	 * ignore it */
138893239ea1SDave Airlie 	vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
138910ebc0bcSDave Airlie 
139090c4cde9SAlex Deucher 	if (rdev->flags & RADEON_IS_PX)
139110ebc0bcSDave Airlie 		runtime = true;
139210ebc0bcSDave Airlie 	vga_switcheroo_register_client(rdev->pdev, &radeon_switcheroo_ops, runtime);
139310ebc0bcSDave Airlie 	if (runtime)
139410ebc0bcSDave Airlie 		vga_switcheroo_init_domain_pm_ops(rdev->dev, &rdev->vga_pm_domain);
139528d52043SDave Airlie 
13963ce0a23dSJerome Glisse 	r = radeon_init(rdev);
1397b574f251SJerome Glisse 	if (r)
1398b574f251SJerome Glisse 		return r;
1399b1e3a6d1SMichel Dänzer 
1400409851f4SJerome Glisse 	r = radeon_gem_debugfs_init(rdev);
1401409851f4SJerome Glisse 	if (r) {
1402409851f4SJerome Glisse 		DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1403409851f4SJerome Glisse 	}
1404409851f4SJerome Glisse 
1405b574f251SJerome Glisse 	if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
1406b574f251SJerome Glisse 		/* Acceleration not working on AGP card try again
1407b574f251SJerome Glisse 		 * with fallback to PCI or PCIE GART
1408b574f251SJerome Glisse 		 */
1409a2d07b74SJerome Glisse 		radeon_asic_reset(rdev);
1410b574f251SJerome Glisse 		radeon_fini(rdev);
1411b574f251SJerome Glisse 		radeon_agp_disable(rdev);
1412b574f251SJerome Glisse 		r = radeon_init(rdev);
14134aac0473SJerome Glisse 		if (r)
14144aac0473SJerome Glisse 			return r;
14153ce0a23dSJerome Glisse 	}
14166c7bcceaSAlex Deucher 
1417*13a7d299SChristian König 	r = radeon_ib_ring_tests(rdev);
1418*13a7d299SChristian König 	if (r)
1419*13a7d299SChristian König 		DRM_ERROR("ib ring test failed (%d).\n", r);
1420*13a7d299SChristian König 
142160a7e396SChristian König 	if ((radeon_testing & 1)) {
14224a1132a0SAlex Deucher 		if (rdev->accel_working)
1423ecc0b326SMichel Dänzer 			radeon_test_moves(rdev);
14244a1132a0SAlex Deucher 		else
14254a1132a0SAlex Deucher 			DRM_INFO("radeon: acceleration disabled, skipping move tests\n");
1426ecc0b326SMichel Dänzer 	}
142760a7e396SChristian König 	if ((radeon_testing & 2)) {
14284a1132a0SAlex Deucher 		if (rdev->accel_working)
142960a7e396SChristian König 			radeon_test_syncing(rdev);
14304a1132a0SAlex Deucher 		else
14314a1132a0SAlex Deucher 			DRM_INFO("radeon: acceleration disabled, skipping sync tests\n");
143260a7e396SChristian König 	}
1433771fe6b9SJerome Glisse 	if (radeon_benchmarking) {
14344a1132a0SAlex Deucher 		if (rdev->accel_working)
1435638dd7dbSIlija Hadzic 			radeon_benchmark(rdev, radeon_benchmarking);
14364a1132a0SAlex Deucher 		else
14374a1132a0SAlex Deucher 			DRM_INFO("radeon: acceleration disabled, skipping benchmarks\n");
1438771fe6b9SJerome Glisse 	}
14396cf8a3f5SJerome Glisse 	return 0;
1440771fe6b9SJerome Glisse }
1441771fe6b9SJerome Glisse 
14424d8bf9aeSChristian König static void radeon_debugfs_remove_files(struct radeon_device *rdev);
14434d8bf9aeSChristian König 
14440c195119SAlex Deucher /**
14450c195119SAlex Deucher  * radeon_device_fini - tear down the driver
14460c195119SAlex Deucher  *
14470c195119SAlex Deucher  * @rdev: radeon_device pointer
14480c195119SAlex Deucher  *
14490c195119SAlex Deucher  * Tear down the driver info (all asics).
14500c195119SAlex Deucher  * Called at driver shutdown.
14510c195119SAlex Deucher  */
1452771fe6b9SJerome Glisse void radeon_device_fini(struct radeon_device *rdev)
1453771fe6b9SJerome Glisse {
1454771fe6b9SJerome Glisse 	DRM_INFO("radeon: finishing device.\n");
1455771fe6b9SJerome Glisse 	rdev->shutdown = true;
145690aca4d2SJerome Glisse 	/* evict vram memory */
145790aca4d2SJerome Glisse 	radeon_bo_evict_vram(rdev);
14583ce0a23dSJerome Glisse 	radeon_fini(rdev);
14596a9ee8afSDave Airlie 	vga_switcheroo_unregister_client(rdev->pdev);
1460c1176d6fSDave Airlie 	vga_client_register(rdev->pdev, NULL, NULL, NULL);
1461e0a2ca73SAlex Deucher 	if (rdev->rio_mem)
1462351a52a2SAlex Deucher 		pci_iounmap(rdev->pdev, rdev->rio_mem);
1463351a52a2SAlex Deucher 	rdev->rio_mem = NULL;
1464771fe6b9SJerome Glisse 	iounmap(rdev->rmmio);
1465771fe6b9SJerome Glisse 	rdev->rmmio = NULL;
146675efdee1SAlex Deucher 	if (rdev->family >= CHIP_BONAIRE)
146775efdee1SAlex Deucher 		radeon_doorbell_fini(rdev);
14684d8bf9aeSChristian König 	radeon_debugfs_remove_files(rdev);
1469771fe6b9SJerome Glisse }
1470771fe6b9SJerome Glisse 
1471771fe6b9SJerome Glisse 
1472771fe6b9SJerome Glisse /*
1473771fe6b9SJerome Glisse  * Suspend & resume.
1474771fe6b9SJerome Glisse  */
14750c195119SAlex Deucher /**
14760c195119SAlex Deucher  * radeon_suspend_kms - initiate device suspend
14770c195119SAlex Deucher  *
14780c195119SAlex Deucher  * @pdev: drm dev pointer
14790c195119SAlex Deucher  * @state: suspend state
14800c195119SAlex Deucher  *
14810c195119SAlex Deucher  * Puts the hw in the suspend state (all asics).
14820c195119SAlex Deucher  * Returns 0 for success or an error on failure.
14830c195119SAlex Deucher  * Called at driver suspend.
14840c195119SAlex Deucher  */
148510ebc0bcSDave Airlie int radeon_suspend_kms(struct drm_device *dev, bool suspend, bool fbcon)
1486771fe6b9SJerome Glisse {
1487875c1866SDarren Jenkins 	struct radeon_device *rdev;
1488771fe6b9SJerome Glisse 	struct drm_crtc *crtc;
1489d8dcaa1dSAlex Deucher 	struct drm_connector *connector;
14907465280cSAlex Deucher 	int i, r;
14915f8f635eSJerome Glisse 	bool force_completion = false;
1492771fe6b9SJerome Glisse 
1493875c1866SDarren Jenkins 	if (dev == NULL || dev->dev_private == NULL) {
1494771fe6b9SJerome Glisse 		return -ENODEV;
1495771fe6b9SJerome Glisse 	}
14967473e830SDave Airlie 
1497875c1866SDarren Jenkins 	rdev = dev->dev_private;
1498875c1866SDarren Jenkins 
14995bcf719bSDave Airlie 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
15006a9ee8afSDave Airlie 		return 0;
1501d8dcaa1dSAlex Deucher 
150286698c20SSeth Forshee 	drm_kms_helper_poll_disable(dev);
150386698c20SSeth Forshee 
1504d8dcaa1dSAlex Deucher 	/* turn off display hw */
1505d8dcaa1dSAlex Deucher 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1506d8dcaa1dSAlex Deucher 		drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1507d8dcaa1dSAlex Deucher 	}
1508d8dcaa1dSAlex Deucher 
1509771fe6b9SJerome Glisse 	/* unpin the front buffers */
1510771fe6b9SJerome Glisse 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1511f4510a27SMatt Roper 		struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->primary->fb);
15124c788679SJerome Glisse 		struct radeon_bo *robj;
1513771fe6b9SJerome Glisse 
1514771fe6b9SJerome Glisse 		if (rfb == NULL || rfb->obj == NULL) {
1515771fe6b9SJerome Glisse 			continue;
1516771fe6b9SJerome Glisse 		}
15177e4d15d9SDaniel Vetter 		robj = gem_to_radeon_bo(rfb->obj);
151838651674SDave Airlie 		/* don't unpin kernel fb objects */
151938651674SDave Airlie 		if (!radeon_fbdev_robj_is_fb(rdev, robj)) {
15204c788679SJerome Glisse 			r = radeon_bo_reserve(robj, false);
152138651674SDave Airlie 			if (r == 0) {
15224c788679SJerome Glisse 				radeon_bo_unpin(robj);
15234c788679SJerome Glisse 				radeon_bo_unreserve(robj);
15244c788679SJerome Glisse 			}
1525771fe6b9SJerome Glisse 		}
1526771fe6b9SJerome Glisse 	}
1527771fe6b9SJerome Glisse 	/* evict vram memory */
15284c788679SJerome Glisse 	radeon_bo_evict_vram(rdev);
15298a47cc9eSChristian König 
1530771fe6b9SJerome Glisse 	/* wait for gpu to finish processing current batch */
15315f8f635eSJerome Glisse 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
153237615527SChristian König 		r = radeon_fence_wait_empty(rdev, i);
15335f8f635eSJerome Glisse 		if (r) {
15345f8f635eSJerome Glisse 			/* delay GPU reset to resume */
15355f8f635eSJerome Glisse 			force_completion = true;
15365f8f635eSJerome Glisse 		}
15375f8f635eSJerome Glisse 	}
15385f8f635eSJerome Glisse 	if (force_completion) {
15395f8f635eSJerome Glisse 		radeon_fence_driver_force_completion(rdev);
15405f8f635eSJerome Glisse 	}
1541771fe6b9SJerome Glisse 
1542f657c2a7SYang Zhao 	radeon_save_bios_scratch_regs(rdev);
1543f657c2a7SYang Zhao 
15443ce0a23dSJerome Glisse 	radeon_suspend(rdev);
1545d4877cf2SAlex Deucher 	radeon_hpd_fini(rdev);
1546771fe6b9SJerome Glisse 	/* evict remaining vram memory */
15474c788679SJerome Glisse 	radeon_bo_evict_vram(rdev);
1548771fe6b9SJerome Glisse 
154910b06122SJerome Glisse 	radeon_agp_suspend(rdev);
155010b06122SJerome Glisse 
1551771fe6b9SJerome Glisse 	pci_save_state(dev->pdev);
15527473e830SDave Airlie 	if (suspend) {
1553771fe6b9SJerome Glisse 		/* Shut down the device */
1554771fe6b9SJerome Glisse 		pci_disable_device(dev->pdev);
1555771fe6b9SJerome Glisse 		pci_set_power_state(dev->pdev, PCI_D3hot);
1556771fe6b9SJerome Glisse 	}
155710ebc0bcSDave Airlie 
155810ebc0bcSDave Airlie 	if (fbcon) {
1559ac751efaSTorben Hohn 		console_lock();
156038651674SDave Airlie 		radeon_fbdev_set_suspend(rdev, 1);
1561ac751efaSTorben Hohn 		console_unlock();
156210ebc0bcSDave Airlie 	}
1563771fe6b9SJerome Glisse 	return 0;
1564771fe6b9SJerome Glisse }
1565771fe6b9SJerome Glisse 
15660c195119SAlex Deucher /**
15670c195119SAlex Deucher  * radeon_resume_kms - initiate device resume
15680c195119SAlex Deucher  *
15690c195119SAlex Deucher  * @pdev: drm dev pointer
15700c195119SAlex Deucher  *
15710c195119SAlex Deucher  * Bring the hw back to operating state (all asics).
15720c195119SAlex Deucher  * Returns 0 for success or an error on failure.
15730c195119SAlex Deucher  * Called at driver resume.
15740c195119SAlex Deucher  */
157510ebc0bcSDave Airlie int radeon_resume_kms(struct drm_device *dev, bool resume, bool fbcon)
1576771fe6b9SJerome Glisse {
157709bdf591SCedric Godin 	struct drm_connector *connector;
1578771fe6b9SJerome Glisse 	struct radeon_device *rdev = dev->dev_private;
157904eb2206SChristian König 	int r;
1580771fe6b9SJerome Glisse 
15815bcf719bSDave Airlie 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
15826a9ee8afSDave Airlie 		return 0;
15836a9ee8afSDave Airlie 
158410ebc0bcSDave Airlie 	if (fbcon) {
1585ac751efaSTorben Hohn 		console_lock();
158610ebc0bcSDave Airlie 	}
15877473e830SDave Airlie 	if (resume) {
1588771fe6b9SJerome Glisse 		pci_set_power_state(dev->pdev, PCI_D0);
1589771fe6b9SJerome Glisse 		pci_restore_state(dev->pdev);
1590771fe6b9SJerome Glisse 		if (pci_enable_device(dev->pdev)) {
159110ebc0bcSDave Airlie 			if (fbcon)
1592ac751efaSTorben Hohn 				console_unlock();
1593771fe6b9SJerome Glisse 			return -1;
1594771fe6b9SJerome Glisse 		}
15957473e830SDave Airlie 	}
15960ebf1717SDave Airlie 	/* resume AGP if in use */
15970ebf1717SDave Airlie 	radeon_agp_resume(rdev);
15983ce0a23dSJerome Glisse 	radeon_resume(rdev);
159904eb2206SChristian König 
160004eb2206SChristian König 	r = radeon_ib_ring_tests(rdev);
160104eb2206SChristian König 	if (r)
160204eb2206SChristian König 		DRM_ERROR("ib ring test failed (%d).\n", r);
160304eb2206SChristian König 
1604bc6a6295SAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
16056c7bcceaSAlex Deucher 		/* do dpm late init */
16066c7bcceaSAlex Deucher 		r = radeon_pm_late_init(rdev);
16076c7bcceaSAlex Deucher 		if (r) {
16086c7bcceaSAlex Deucher 			rdev->pm.dpm_enabled = false;
16096c7bcceaSAlex Deucher 			DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
16106c7bcceaSAlex Deucher 		}
1611bc6a6295SAlex Deucher 	} else {
1612bc6a6295SAlex Deucher 		/* resume old pm late */
1613bc6a6295SAlex Deucher 		radeon_pm_resume(rdev);
16146c7bcceaSAlex Deucher 	}
16156c7bcceaSAlex Deucher 
1616f657c2a7SYang Zhao 	radeon_restore_bios_scratch_regs(rdev);
161709bdf591SCedric Godin 
16183fa47d9eSAlex Deucher 	/* init dig PHYs, disp eng pll */
16193fa47d9eSAlex Deucher 	if (rdev->is_atom_bios) {
1620ac89af1eSAlex Deucher 		radeon_atom_encoder_init(rdev);
1621f3f1f03eSAlex Deucher 		radeon_atom_disp_eng_pll_init(rdev);
1622bced76f2SAlex Deucher 		/* turn on the BL */
1623bced76f2SAlex Deucher 		if (rdev->mode_info.bl_encoder) {
1624bced76f2SAlex Deucher 			u8 bl_level = radeon_get_backlight_level(rdev,
1625bced76f2SAlex Deucher 								 rdev->mode_info.bl_encoder);
1626bced76f2SAlex Deucher 			radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
1627bced76f2SAlex Deucher 						   bl_level);
1628bced76f2SAlex Deucher 		}
16293fa47d9eSAlex Deucher 	}
1630d4877cf2SAlex Deucher 	/* reset hpd state */
1631d4877cf2SAlex Deucher 	radeon_hpd_init(rdev);
1632771fe6b9SJerome Glisse 	/* blat the mode back in */
1633ec9954fcSDave Airlie 	if (fbcon) {
1634771fe6b9SJerome Glisse 		drm_helper_resume_force_mode(dev);
1635a93f344dSAlex Deucher 		/* turn on display hw */
1636a93f344dSAlex Deucher 		list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1637a93f344dSAlex Deucher 			drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
1638a93f344dSAlex Deucher 		}
1639ec9954fcSDave Airlie 	}
164086698c20SSeth Forshee 
164186698c20SSeth Forshee 	drm_kms_helper_poll_enable(dev);
164218ee37a4SDaniel Vetter 
16433640da2fSAlex Deucher 	/* set the power state here in case we are a PX system or headless */
16443640da2fSAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled)
16453640da2fSAlex Deucher 		radeon_pm_compute_clocks(rdev);
16463640da2fSAlex Deucher 
164718ee37a4SDaniel Vetter 	if (fbcon) {
164818ee37a4SDaniel Vetter 		radeon_fbdev_set_suspend(rdev, 0);
164918ee37a4SDaniel Vetter 		console_unlock();
165018ee37a4SDaniel Vetter 	}
165118ee37a4SDaniel Vetter 
1652771fe6b9SJerome Glisse 	return 0;
1653771fe6b9SJerome Glisse }
1654771fe6b9SJerome Glisse 
16550c195119SAlex Deucher /**
16560c195119SAlex Deucher  * radeon_gpu_reset - reset the asic
16570c195119SAlex Deucher  *
16580c195119SAlex Deucher  * @rdev: radeon device pointer
16590c195119SAlex Deucher  *
16600c195119SAlex Deucher  * Attempt the reset the GPU if it has hung (all asics).
16610c195119SAlex Deucher  * Returns 0 for success or an error on failure.
16620c195119SAlex Deucher  */
166390aca4d2SJerome Glisse int radeon_gpu_reset(struct radeon_device *rdev)
166490aca4d2SJerome Glisse {
166555d7c221SChristian König 	unsigned ring_sizes[RADEON_NUM_RINGS];
166655d7c221SChristian König 	uint32_t *ring_data[RADEON_NUM_RINGS];
166755d7c221SChristian König 
166855d7c221SChristian König 	bool saved = false;
166955d7c221SChristian König 
167055d7c221SChristian König 	int i, r;
16718fd1b84cSDave Airlie 	int resched;
167290aca4d2SJerome Glisse 
1673dee53e7fSJerome Glisse 	down_write(&rdev->exclusive_lock);
1674f9eaf9aeSChristian König 
1675f9eaf9aeSChristian König 	if (!rdev->needs_reset) {
1676f9eaf9aeSChristian König 		up_write(&rdev->exclusive_lock);
1677f9eaf9aeSChristian König 		return 0;
1678f9eaf9aeSChristian König 	}
1679f9eaf9aeSChristian König 
1680f9eaf9aeSChristian König 	rdev->needs_reset = false;
1681f9eaf9aeSChristian König 
168290aca4d2SJerome Glisse 	radeon_save_bios_scratch_regs(rdev);
16838fd1b84cSDave Airlie 	/* block TTM */
16848fd1b84cSDave Airlie 	resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
168590aca4d2SJerome Glisse 	radeon_suspend(rdev);
168673ef0e0dSAlex Deucher 	radeon_hpd_fini(rdev);
168790aca4d2SJerome Glisse 
168855d7c221SChristian König 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
168955d7c221SChristian König 		ring_sizes[i] = radeon_ring_backup(rdev, &rdev->ring[i],
169055d7c221SChristian König 						   &ring_data[i]);
169155d7c221SChristian König 		if (ring_sizes[i]) {
169255d7c221SChristian König 			saved = true;
169355d7c221SChristian König 			dev_info(rdev->dev, "Saved %d dwords of commands "
169455d7c221SChristian König 				 "on ring %d.\n", ring_sizes[i], i);
169555d7c221SChristian König 		}
169655d7c221SChristian König 	}
169755d7c221SChristian König 
169855d7c221SChristian König retry:
169990aca4d2SJerome Glisse 	r = radeon_asic_reset(rdev);
170090aca4d2SJerome Glisse 	if (!r) {
170155d7c221SChristian König 		dev_info(rdev->dev, "GPU reset succeeded, trying to resume\n");
170290aca4d2SJerome Glisse 		radeon_resume(rdev);
170355d7c221SChristian König 	}
170404eb2206SChristian König 
170590aca4d2SJerome Glisse 	radeon_restore_bios_scratch_regs(rdev);
170655d7c221SChristian König 
170755d7c221SChristian König 	if (!r) {
170855d7c221SChristian König 		for (i = 0; i < RADEON_NUM_RINGS; ++i) {
170955d7c221SChristian König 			radeon_ring_restore(rdev, &rdev->ring[i],
171055d7c221SChristian König 					    ring_sizes[i], ring_data[i]);
1711f54b350dSChristian König 			ring_sizes[i] = 0;
1712f54b350dSChristian König 			ring_data[i] = NULL;
171390aca4d2SJerome Glisse 		}
17147a1619b9SMichel Dänzer 
171555d7c221SChristian König 		r = radeon_ib_ring_tests(rdev);
171655d7c221SChristian König 		if (r) {
171755d7c221SChristian König 			dev_err(rdev->dev, "ib ring test failed (%d).\n", r);
171855d7c221SChristian König 			if (saved) {
1719f54b350dSChristian König 				saved = false;
172055d7c221SChristian König 				radeon_suspend(rdev);
172155d7c221SChristian König 				goto retry;
172255d7c221SChristian König 			}
172355d7c221SChristian König 		}
172455d7c221SChristian König 	} else {
172576903b96SJerome Glisse 		radeon_fence_driver_force_completion(rdev);
172655d7c221SChristian König 		for (i = 0; i < RADEON_NUM_RINGS; ++i) {
172755d7c221SChristian König 			kfree(ring_data[i]);
172855d7c221SChristian König 		}
172955d7c221SChristian König 	}
173055d7c221SChristian König 
1731c940b447SAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
1732c940b447SAlex Deucher 		/* do dpm late init */
1733c940b447SAlex Deucher 		r = radeon_pm_late_init(rdev);
1734c940b447SAlex Deucher 		if (r) {
1735c940b447SAlex Deucher 			rdev->pm.dpm_enabled = false;
1736c940b447SAlex Deucher 			DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
1737c940b447SAlex Deucher 		}
1738c940b447SAlex Deucher 	} else {
1739c940b447SAlex Deucher 		/* resume old pm late */
174095f59509SAlex Deucher 		radeon_pm_resume(rdev);
1741c940b447SAlex Deucher 	}
1742c940b447SAlex Deucher 
174373ef0e0dSAlex Deucher 	/* init dig PHYs, disp eng pll */
174473ef0e0dSAlex Deucher 	if (rdev->is_atom_bios) {
174573ef0e0dSAlex Deucher 		radeon_atom_encoder_init(rdev);
174673ef0e0dSAlex Deucher 		radeon_atom_disp_eng_pll_init(rdev);
174773ef0e0dSAlex Deucher 		/* turn on the BL */
174873ef0e0dSAlex Deucher 		if (rdev->mode_info.bl_encoder) {
174973ef0e0dSAlex Deucher 			u8 bl_level = radeon_get_backlight_level(rdev,
175073ef0e0dSAlex Deucher 								 rdev->mode_info.bl_encoder);
175173ef0e0dSAlex Deucher 			radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
175273ef0e0dSAlex Deucher 						   bl_level);
175373ef0e0dSAlex Deucher 		}
175473ef0e0dSAlex Deucher 	}
175573ef0e0dSAlex Deucher 	/* reset hpd state */
175673ef0e0dSAlex Deucher 	radeon_hpd_init(rdev);
175773ef0e0dSAlex Deucher 
1758d3493574SJerome Glisse 	drm_helper_resume_force_mode(rdev->ddev);
1759d3493574SJerome Glisse 
1760c940b447SAlex Deucher 	/* set the power state here in case we are a PX system or headless */
1761c940b447SAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled)
1762c940b447SAlex Deucher 		radeon_pm_compute_clocks(rdev);
1763c940b447SAlex Deucher 
176455d7c221SChristian König 	ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
17657a1619b9SMichel Dänzer 	if (r) {
176690aca4d2SJerome Glisse 		/* bad news, how to tell it to userspace ? */
176790aca4d2SJerome Glisse 		dev_info(rdev->dev, "GPU reset failed\n");
17687a1619b9SMichel Dänzer 	}
17697a1619b9SMichel Dänzer 
1770dee53e7fSJerome Glisse 	up_write(&rdev->exclusive_lock);
177190aca4d2SJerome Glisse 	return r;
177290aca4d2SJerome Glisse }
177390aca4d2SJerome Glisse 
1774771fe6b9SJerome Glisse 
1775771fe6b9SJerome Glisse /*
1776771fe6b9SJerome Glisse  * Debugfs
1777771fe6b9SJerome Glisse  */
1778771fe6b9SJerome Glisse int radeon_debugfs_add_files(struct radeon_device *rdev,
1779771fe6b9SJerome Glisse 			     struct drm_info_list *files,
1780771fe6b9SJerome Glisse 			     unsigned nfiles)
1781771fe6b9SJerome Glisse {
1782771fe6b9SJerome Glisse 	unsigned i;
1783771fe6b9SJerome Glisse 
17844d8bf9aeSChristian König 	for (i = 0; i < rdev->debugfs_count; i++) {
17854d8bf9aeSChristian König 		if (rdev->debugfs[i].files == files) {
1786771fe6b9SJerome Glisse 			/* Already registered */
1787771fe6b9SJerome Glisse 			return 0;
1788771fe6b9SJerome Glisse 		}
1789771fe6b9SJerome Glisse 	}
1790c245cb9eSMichael Witten 
17914d8bf9aeSChristian König 	i = rdev->debugfs_count + 1;
1792c245cb9eSMichael Witten 	if (i > RADEON_DEBUGFS_MAX_COMPONENTS) {
1793c245cb9eSMichael Witten 		DRM_ERROR("Reached maximum number of debugfs components.\n");
1794c245cb9eSMichael Witten 		DRM_ERROR("Report so we increase "
1795c245cb9eSMichael Witten 		          "RADEON_DEBUGFS_MAX_COMPONENTS.\n");
1796771fe6b9SJerome Glisse 		return -EINVAL;
1797771fe6b9SJerome Glisse 	}
17984d8bf9aeSChristian König 	rdev->debugfs[rdev->debugfs_count].files = files;
17994d8bf9aeSChristian König 	rdev->debugfs[rdev->debugfs_count].num_files = nfiles;
18004d8bf9aeSChristian König 	rdev->debugfs_count = i;
1801771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS)
1802771fe6b9SJerome Glisse 	drm_debugfs_create_files(files, nfiles,
1803771fe6b9SJerome Glisse 				 rdev->ddev->control->debugfs_root,
1804771fe6b9SJerome Glisse 				 rdev->ddev->control);
1805771fe6b9SJerome Glisse 	drm_debugfs_create_files(files, nfiles,
1806771fe6b9SJerome Glisse 				 rdev->ddev->primary->debugfs_root,
1807771fe6b9SJerome Glisse 				 rdev->ddev->primary);
1808771fe6b9SJerome Glisse #endif
1809771fe6b9SJerome Glisse 	return 0;
1810771fe6b9SJerome Glisse }
1811771fe6b9SJerome Glisse 
18124d8bf9aeSChristian König static void radeon_debugfs_remove_files(struct radeon_device *rdev)
18134d8bf9aeSChristian König {
18144d8bf9aeSChristian König #if defined(CONFIG_DEBUG_FS)
18154d8bf9aeSChristian König 	unsigned i;
18164d8bf9aeSChristian König 
18174d8bf9aeSChristian König 	for (i = 0; i < rdev->debugfs_count; i++) {
18184d8bf9aeSChristian König 		drm_debugfs_remove_files(rdev->debugfs[i].files,
18194d8bf9aeSChristian König 					 rdev->debugfs[i].num_files,
18204d8bf9aeSChristian König 					 rdev->ddev->control);
18214d8bf9aeSChristian König 		drm_debugfs_remove_files(rdev->debugfs[i].files,
18224d8bf9aeSChristian König 					 rdev->debugfs[i].num_files,
18234d8bf9aeSChristian König 					 rdev->ddev->primary);
18244d8bf9aeSChristian König 	}
18254d8bf9aeSChristian König #endif
18264d8bf9aeSChristian König }
18274d8bf9aeSChristian König 
1828771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS)
1829771fe6b9SJerome Glisse int radeon_debugfs_init(struct drm_minor *minor)
1830771fe6b9SJerome Glisse {
1831771fe6b9SJerome Glisse 	return 0;
1832771fe6b9SJerome Glisse }
1833771fe6b9SJerome Glisse 
1834771fe6b9SJerome Glisse void radeon_debugfs_cleanup(struct drm_minor *minor)
1835771fe6b9SJerome Glisse {
1836771fe6b9SJerome Glisse }
1837771fe6b9SJerome Glisse #endif
1838