xref: /openbmc/linux/drivers/gpu/drm/radeon/radeon_device.c (revision 90c4cde9d5a2bb6239cb3e253bb3832ed89dc75c)
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",
1021b5331d9SJerome Glisse 	"LAST",
1031b5331d9SJerome Glisse };
1041b5331d9SJerome Glisse 
105*90c4cde9SAlex Deucher bool radeon_is_px(struct drm_device *dev)
106*90c4cde9SAlex Deucher {
107*90c4cde9SAlex Deucher 	struct radeon_device *rdev = dev->dev_private;
108*90c4cde9SAlex Deucher 
109*90c4cde9SAlex Deucher 	if (rdev->flags & RADEON_IS_PX)
110*90c4cde9SAlex Deucher 		return true;
111*90c4cde9SAlex Deucher 	return false;
112*90c4cde9SAlex Deucher }
11310ebc0bcSDave Airlie 
1140c195119SAlex Deucher /**
1152e1b65f9SAlex Deucher  * radeon_program_register_sequence - program an array of registers.
1162e1b65f9SAlex Deucher  *
1172e1b65f9SAlex Deucher  * @rdev: radeon_device pointer
1182e1b65f9SAlex Deucher  * @registers: pointer to the register array
1192e1b65f9SAlex Deucher  * @array_size: size of the register array
1202e1b65f9SAlex Deucher  *
1212e1b65f9SAlex Deucher  * Programs an array or registers with and and or masks.
1222e1b65f9SAlex Deucher  * This is a helper for setting golden registers.
1232e1b65f9SAlex Deucher  */
1242e1b65f9SAlex Deucher void radeon_program_register_sequence(struct radeon_device *rdev,
1252e1b65f9SAlex Deucher 				      const u32 *registers,
1262e1b65f9SAlex Deucher 				      const u32 array_size)
1272e1b65f9SAlex Deucher {
1282e1b65f9SAlex Deucher 	u32 tmp, reg, and_mask, or_mask;
1292e1b65f9SAlex Deucher 	int i;
1302e1b65f9SAlex Deucher 
1312e1b65f9SAlex Deucher 	if (array_size % 3)
1322e1b65f9SAlex Deucher 		return;
1332e1b65f9SAlex Deucher 
1342e1b65f9SAlex Deucher 	for (i = 0; i < array_size; i +=3) {
1352e1b65f9SAlex Deucher 		reg = registers[i + 0];
1362e1b65f9SAlex Deucher 		and_mask = registers[i + 1];
1372e1b65f9SAlex Deucher 		or_mask = registers[i + 2];
1382e1b65f9SAlex Deucher 
1392e1b65f9SAlex Deucher 		if (and_mask == 0xffffffff) {
1402e1b65f9SAlex Deucher 			tmp = or_mask;
1412e1b65f9SAlex Deucher 		} else {
1422e1b65f9SAlex Deucher 			tmp = RREG32(reg);
1432e1b65f9SAlex Deucher 			tmp &= ~and_mask;
1442e1b65f9SAlex Deucher 			tmp |= or_mask;
1452e1b65f9SAlex Deucher 		}
1462e1b65f9SAlex Deucher 		WREG32(reg, tmp);
1472e1b65f9SAlex Deucher 	}
1482e1b65f9SAlex Deucher }
1492e1b65f9SAlex Deucher 
1501a0041b8SAlex Deucher void radeon_pci_config_reset(struct radeon_device *rdev)
1511a0041b8SAlex Deucher {
1521a0041b8SAlex Deucher 	pci_write_config_dword(rdev->pdev, 0x7c, RADEON_ASIC_RESET_DATA);
1531a0041b8SAlex Deucher }
1541a0041b8SAlex Deucher 
1552e1b65f9SAlex Deucher /**
1560c195119SAlex Deucher  * radeon_surface_init - Clear GPU surface registers.
1570c195119SAlex Deucher  *
1580c195119SAlex Deucher  * @rdev: radeon_device pointer
1590c195119SAlex Deucher  *
1600c195119SAlex Deucher  * Clear GPU surface registers (r1xx-r5xx).
161b1e3a6d1SMichel Dänzer  */
1623ce0a23dSJerome Glisse void radeon_surface_init(struct radeon_device *rdev)
163b1e3a6d1SMichel Dänzer {
164b1e3a6d1SMichel Dänzer 	/* FIXME: check this out */
165b1e3a6d1SMichel Dänzer 	if (rdev->family < CHIP_R600) {
166b1e3a6d1SMichel Dänzer 		int i;
167b1e3a6d1SMichel Dänzer 
168550e2d92SDave Airlie 		for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
169550e2d92SDave Airlie 			if (rdev->surface_regs[i].bo)
170550e2d92SDave Airlie 				radeon_bo_get_surface_reg(rdev->surface_regs[i].bo);
171550e2d92SDave Airlie 			else
172550e2d92SDave Airlie 				radeon_clear_surface_reg(rdev, i);
173b1e3a6d1SMichel Dänzer 		}
174e024e110SDave Airlie 		/* enable surfaces */
175e024e110SDave Airlie 		WREG32(RADEON_SURFACE_CNTL, 0);
176b1e3a6d1SMichel Dänzer 	}
177b1e3a6d1SMichel Dänzer }
178b1e3a6d1SMichel Dänzer 
179b1e3a6d1SMichel Dänzer /*
180771fe6b9SJerome Glisse  * GPU scratch registers helpers function.
181771fe6b9SJerome Glisse  */
1820c195119SAlex Deucher /**
1830c195119SAlex Deucher  * radeon_scratch_init - Init scratch register driver information.
1840c195119SAlex Deucher  *
1850c195119SAlex Deucher  * @rdev: radeon_device pointer
1860c195119SAlex Deucher  *
1870c195119SAlex Deucher  * Init CP scratch register driver information (r1xx-r5xx)
1880c195119SAlex Deucher  */
1893ce0a23dSJerome Glisse void radeon_scratch_init(struct radeon_device *rdev)
190771fe6b9SJerome Glisse {
191771fe6b9SJerome Glisse 	int i;
192771fe6b9SJerome Glisse 
193771fe6b9SJerome Glisse 	/* FIXME: check this out */
194771fe6b9SJerome Glisse 	if (rdev->family < CHIP_R300) {
195771fe6b9SJerome Glisse 		rdev->scratch.num_reg = 5;
196771fe6b9SJerome Glisse 	} else {
197771fe6b9SJerome Glisse 		rdev->scratch.num_reg = 7;
198771fe6b9SJerome Glisse 	}
199724c80e1SAlex Deucher 	rdev->scratch.reg_base = RADEON_SCRATCH_REG0;
200771fe6b9SJerome Glisse 	for (i = 0; i < rdev->scratch.num_reg; i++) {
201771fe6b9SJerome Glisse 		rdev->scratch.free[i] = true;
202724c80e1SAlex Deucher 		rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4);
203771fe6b9SJerome Glisse 	}
204771fe6b9SJerome Glisse }
205771fe6b9SJerome Glisse 
2060c195119SAlex Deucher /**
2070c195119SAlex Deucher  * radeon_scratch_get - Allocate a scratch register
2080c195119SAlex Deucher  *
2090c195119SAlex Deucher  * @rdev: radeon_device pointer
2100c195119SAlex Deucher  * @reg: scratch register mmio offset
2110c195119SAlex Deucher  *
2120c195119SAlex Deucher  * Allocate a CP scratch register for use by the driver (all asics).
2130c195119SAlex Deucher  * Returns 0 on success or -EINVAL on failure.
2140c195119SAlex Deucher  */
215771fe6b9SJerome Glisse int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg)
216771fe6b9SJerome Glisse {
217771fe6b9SJerome Glisse 	int i;
218771fe6b9SJerome Glisse 
219771fe6b9SJerome Glisse 	for (i = 0; i < rdev->scratch.num_reg; i++) {
220771fe6b9SJerome Glisse 		if (rdev->scratch.free[i]) {
221771fe6b9SJerome Glisse 			rdev->scratch.free[i] = false;
222771fe6b9SJerome Glisse 			*reg = rdev->scratch.reg[i];
223771fe6b9SJerome Glisse 			return 0;
224771fe6b9SJerome Glisse 		}
225771fe6b9SJerome Glisse 	}
226771fe6b9SJerome Glisse 	return -EINVAL;
227771fe6b9SJerome Glisse }
228771fe6b9SJerome Glisse 
2290c195119SAlex Deucher /**
2300c195119SAlex Deucher  * radeon_scratch_free - Free a scratch register
2310c195119SAlex Deucher  *
2320c195119SAlex Deucher  * @rdev: radeon_device pointer
2330c195119SAlex Deucher  * @reg: scratch register mmio offset
2340c195119SAlex Deucher  *
2350c195119SAlex Deucher  * Free a CP scratch register allocated for use by the driver (all asics)
2360c195119SAlex Deucher  */
237771fe6b9SJerome Glisse void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg)
238771fe6b9SJerome Glisse {
239771fe6b9SJerome Glisse 	int i;
240771fe6b9SJerome Glisse 
241771fe6b9SJerome Glisse 	for (i = 0; i < rdev->scratch.num_reg; i++) {
242771fe6b9SJerome Glisse 		if (rdev->scratch.reg[i] == reg) {
243771fe6b9SJerome Glisse 			rdev->scratch.free[i] = true;
244771fe6b9SJerome Glisse 			return;
245771fe6b9SJerome Glisse 		}
246771fe6b9SJerome Glisse 	}
247771fe6b9SJerome Glisse }
248771fe6b9SJerome Glisse 
2490c195119SAlex Deucher /*
25075efdee1SAlex Deucher  * GPU doorbell aperture helpers function.
25175efdee1SAlex Deucher  */
25275efdee1SAlex Deucher /**
25375efdee1SAlex Deucher  * radeon_doorbell_init - Init doorbell driver information.
25475efdee1SAlex Deucher  *
25575efdee1SAlex Deucher  * @rdev: radeon_device pointer
25675efdee1SAlex Deucher  *
25775efdee1SAlex Deucher  * Init doorbell driver information (CIK)
25875efdee1SAlex Deucher  * Returns 0 on success, error on failure.
25975efdee1SAlex Deucher  */
26028f5a6cdSRashika Kheria static int radeon_doorbell_init(struct radeon_device *rdev)
26175efdee1SAlex Deucher {
26275efdee1SAlex Deucher 	/* doorbell bar mapping */
26375efdee1SAlex Deucher 	rdev->doorbell.base = pci_resource_start(rdev->pdev, 2);
26475efdee1SAlex Deucher 	rdev->doorbell.size = pci_resource_len(rdev->pdev, 2);
26575efdee1SAlex Deucher 
266d5754ab8SAndrew Lewycky 	rdev->doorbell.num_doorbells = min_t(u32, rdev->doorbell.size / sizeof(u32), RADEON_MAX_DOORBELLS);
267d5754ab8SAndrew Lewycky 	if (rdev->doorbell.num_doorbells == 0)
268d5754ab8SAndrew Lewycky 		return -EINVAL;
26975efdee1SAlex Deucher 
270d5754ab8SAndrew Lewycky 	rdev->doorbell.ptr = ioremap(rdev->doorbell.base, rdev->doorbell.num_doorbells * sizeof(u32));
27175efdee1SAlex Deucher 	if (rdev->doorbell.ptr == NULL) {
27275efdee1SAlex Deucher 		return -ENOMEM;
27375efdee1SAlex Deucher 	}
27475efdee1SAlex Deucher 	DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)rdev->doorbell.base);
27575efdee1SAlex Deucher 	DRM_INFO("doorbell mmio size: %u\n", (unsigned)rdev->doorbell.size);
27675efdee1SAlex Deucher 
277d5754ab8SAndrew Lewycky 	memset(&rdev->doorbell.used, 0, sizeof(rdev->doorbell.used));
27875efdee1SAlex Deucher 
27975efdee1SAlex Deucher 	return 0;
28075efdee1SAlex Deucher }
28175efdee1SAlex Deucher 
28275efdee1SAlex Deucher /**
28375efdee1SAlex Deucher  * radeon_doorbell_fini - Tear down doorbell driver information.
28475efdee1SAlex Deucher  *
28575efdee1SAlex Deucher  * @rdev: radeon_device pointer
28675efdee1SAlex Deucher  *
28775efdee1SAlex Deucher  * Tear down doorbell driver information (CIK)
28875efdee1SAlex Deucher  */
28928f5a6cdSRashika Kheria static void radeon_doorbell_fini(struct radeon_device *rdev)
29075efdee1SAlex Deucher {
29175efdee1SAlex Deucher 	iounmap(rdev->doorbell.ptr);
29275efdee1SAlex Deucher 	rdev->doorbell.ptr = NULL;
29375efdee1SAlex Deucher }
29475efdee1SAlex Deucher 
29575efdee1SAlex Deucher /**
296d5754ab8SAndrew Lewycky  * radeon_doorbell_get - Allocate a doorbell entry
29775efdee1SAlex Deucher  *
29875efdee1SAlex Deucher  * @rdev: radeon_device pointer
299d5754ab8SAndrew Lewycky  * @doorbell: doorbell index
30075efdee1SAlex Deucher  *
301d5754ab8SAndrew Lewycky  * Allocate a doorbell for use by the driver (all asics).
30275efdee1SAlex Deucher  * Returns 0 on success or -EINVAL on failure.
30375efdee1SAlex Deucher  */
30475efdee1SAlex Deucher int radeon_doorbell_get(struct radeon_device *rdev, u32 *doorbell)
30575efdee1SAlex Deucher {
306d5754ab8SAndrew Lewycky 	unsigned long offset = find_first_zero_bit(rdev->doorbell.used, rdev->doorbell.num_doorbells);
307d5754ab8SAndrew Lewycky 	if (offset < rdev->doorbell.num_doorbells) {
308d5754ab8SAndrew Lewycky 		__set_bit(offset, rdev->doorbell.used);
309d5754ab8SAndrew Lewycky 		*doorbell = offset;
31075efdee1SAlex Deucher 		return 0;
311d5754ab8SAndrew Lewycky 	} else {
31275efdee1SAlex Deucher 		return -EINVAL;
31375efdee1SAlex Deucher 	}
314d5754ab8SAndrew Lewycky }
31575efdee1SAlex Deucher 
31675efdee1SAlex Deucher /**
317d5754ab8SAndrew Lewycky  * radeon_doorbell_free - Free a doorbell entry
31875efdee1SAlex Deucher  *
31975efdee1SAlex Deucher  * @rdev: radeon_device pointer
320d5754ab8SAndrew Lewycky  * @doorbell: doorbell index
32175efdee1SAlex Deucher  *
322d5754ab8SAndrew Lewycky  * Free a doorbell allocated for use by the driver (all asics)
32375efdee1SAlex Deucher  */
32475efdee1SAlex Deucher void radeon_doorbell_free(struct radeon_device *rdev, u32 doorbell)
32575efdee1SAlex Deucher {
326d5754ab8SAndrew Lewycky 	if (doorbell < rdev->doorbell.num_doorbells)
327d5754ab8SAndrew Lewycky 		__clear_bit(doorbell, rdev->doorbell.used);
32875efdee1SAlex Deucher }
32975efdee1SAlex Deucher 
33075efdee1SAlex Deucher /*
3310c195119SAlex Deucher  * radeon_wb_*()
3320c195119SAlex Deucher  * Writeback is the the method by which the the GPU updates special pages
3330c195119SAlex Deucher  * in memory with the status of certain GPU events (fences, ring pointers,
3340c195119SAlex Deucher  * etc.).
3350c195119SAlex Deucher  */
3360c195119SAlex Deucher 
3370c195119SAlex Deucher /**
3380c195119SAlex Deucher  * radeon_wb_disable - Disable Writeback
3390c195119SAlex Deucher  *
3400c195119SAlex Deucher  * @rdev: radeon_device pointer
3410c195119SAlex Deucher  *
3420c195119SAlex Deucher  * Disables Writeback (all asics).  Used for suspend.
3430c195119SAlex Deucher  */
344724c80e1SAlex Deucher void radeon_wb_disable(struct radeon_device *rdev)
345724c80e1SAlex Deucher {
346724c80e1SAlex Deucher 	rdev->wb.enabled = false;
347724c80e1SAlex Deucher }
348724c80e1SAlex Deucher 
3490c195119SAlex Deucher /**
3500c195119SAlex Deucher  * radeon_wb_fini - Disable Writeback and free memory
3510c195119SAlex Deucher  *
3520c195119SAlex Deucher  * @rdev: radeon_device pointer
3530c195119SAlex Deucher  *
3540c195119SAlex Deucher  * Disables Writeback and frees the Writeback memory (all asics).
3550c195119SAlex Deucher  * Used at driver shutdown.
3560c195119SAlex Deucher  */
357724c80e1SAlex Deucher void radeon_wb_fini(struct radeon_device *rdev)
358724c80e1SAlex Deucher {
359724c80e1SAlex Deucher 	radeon_wb_disable(rdev);
360724c80e1SAlex Deucher 	if (rdev->wb.wb_obj) {
361089920f2SJerome Glisse 		if (!radeon_bo_reserve(rdev->wb.wb_obj, false)) {
362089920f2SJerome Glisse 			radeon_bo_kunmap(rdev->wb.wb_obj);
363089920f2SJerome Glisse 			radeon_bo_unpin(rdev->wb.wb_obj);
364089920f2SJerome Glisse 			radeon_bo_unreserve(rdev->wb.wb_obj);
365089920f2SJerome Glisse 		}
366724c80e1SAlex Deucher 		radeon_bo_unref(&rdev->wb.wb_obj);
367724c80e1SAlex Deucher 		rdev->wb.wb = NULL;
368724c80e1SAlex Deucher 		rdev->wb.wb_obj = NULL;
369724c80e1SAlex Deucher 	}
370724c80e1SAlex Deucher }
371724c80e1SAlex Deucher 
3720c195119SAlex Deucher /**
3730c195119SAlex Deucher  * radeon_wb_init- Init Writeback driver info and allocate memory
3740c195119SAlex Deucher  *
3750c195119SAlex Deucher  * @rdev: radeon_device pointer
3760c195119SAlex Deucher  *
3770c195119SAlex Deucher  * Disables Writeback and frees the Writeback memory (all asics).
3780c195119SAlex Deucher  * Used at driver startup.
3790c195119SAlex Deucher  * Returns 0 on success or an -error on failure.
3800c195119SAlex Deucher  */
381724c80e1SAlex Deucher int radeon_wb_init(struct radeon_device *rdev)
382724c80e1SAlex Deucher {
383724c80e1SAlex Deucher 	int r;
384724c80e1SAlex Deucher 
385724c80e1SAlex Deucher 	if (rdev->wb.wb_obj == NULL) {
386441921d5SDaniel Vetter 		r = radeon_bo_create(rdev, RADEON_GPU_PAGE_SIZE, PAGE_SIZE, true,
38740f5cf99SAlex Deucher 				     RADEON_GEM_DOMAIN_GTT, NULL, &rdev->wb.wb_obj);
388724c80e1SAlex Deucher 		if (r) {
389724c80e1SAlex Deucher 			dev_warn(rdev->dev, "(%d) create WB bo failed\n", r);
390724c80e1SAlex Deucher 			return r;
391724c80e1SAlex Deucher 		}
392724c80e1SAlex Deucher 		r = radeon_bo_reserve(rdev->wb.wb_obj, false);
393724c80e1SAlex Deucher 		if (unlikely(r != 0)) {
394724c80e1SAlex Deucher 			radeon_wb_fini(rdev);
395724c80e1SAlex Deucher 			return r;
396724c80e1SAlex Deucher 		}
397724c80e1SAlex Deucher 		r = radeon_bo_pin(rdev->wb.wb_obj, RADEON_GEM_DOMAIN_GTT,
398724c80e1SAlex Deucher 				&rdev->wb.gpu_addr);
399724c80e1SAlex Deucher 		if (r) {
400724c80e1SAlex Deucher 			radeon_bo_unreserve(rdev->wb.wb_obj);
401724c80e1SAlex Deucher 			dev_warn(rdev->dev, "(%d) pin WB bo failed\n", r);
402724c80e1SAlex Deucher 			radeon_wb_fini(rdev);
403724c80e1SAlex Deucher 			return r;
404724c80e1SAlex Deucher 		}
405724c80e1SAlex Deucher 		r = radeon_bo_kmap(rdev->wb.wb_obj, (void **)&rdev->wb.wb);
406724c80e1SAlex Deucher 		radeon_bo_unreserve(rdev->wb.wb_obj);
407724c80e1SAlex Deucher 		if (r) {
408724c80e1SAlex Deucher 			dev_warn(rdev->dev, "(%d) map WB bo failed\n", r);
409724c80e1SAlex Deucher 			radeon_wb_fini(rdev);
410724c80e1SAlex Deucher 			return r;
411724c80e1SAlex Deucher 		}
412089920f2SJerome Glisse 	}
413724c80e1SAlex Deucher 
414e6ba7599SAlex Deucher 	/* clear wb memory */
415e6ba7599SAlex Deucher 	memset((char *)rdev->wb.wb, 0, RADEON_GPU_PAGE_SIZE);
416d0f8a854SAlex Deucher 	/* disable event_write fences */
417d0f8a854SAlex Deucher 	rdev->wb.use_event = false;
418724c80e1SAlex Deucher 	/* disabled via module param */
4193b7a2b24SJerome Glisse 	if (radeon_no_wb == 1) {
420724c80e1SAlex Deucher 		rdev->wb.enabled = false;
4213b7a2b24SJerome Glisse 	} else {
422724c80e1SAlex Deucher 		if (rdev->flags & RADEON_IS_AGP) {
42328eebb70SAlex Deucher 			/* often unreliable on AGP */
42428eebb70SAlex Deucher 			rdev->wb.enabled = false;
42528eebb70SAlex Deucher 		} else if (rdev->family < CHIP_R300) {
42628eebb70SAlex Deucher 			/* often unreliable on pre-r300 */
427724c80e1SAlex Deucher 			rdev->wb.enabled = false;
428d0f8a854SAlex Deucher 		} else {
429724c80e1SAlex Deucher 			rdev->wb.enabled = true;
430d0f8a854SAlex Deucher 			/* event_write fences are only available on r600+ */
4313b7a2b24SJerome Glisse 			if (rdev->family >= CHIP_R600) {
432d0f8a854SAlex Deucher 				rdev->wb.use_event = true;
433d0f8a854SAlex Deucher 			}
434724c80e1SAlex Deucher 		}
4353b7a2b24SJerome Glisse 	}
436c994ead6SAlex Deucher 	/* always use writeback/events on NI, APUs */
437c994ead6SAlex Deucher 	if (rdev->family >= CHIP_PALM) {
4387d52785dSAlex Deucher 		rdev->wb.enabled = true;
4397d52785dSAlex Deucher 		rdev->wb.use_event = true;
4407d52785dSAlex Deucher 	}
441724c80e1SAlex Deucher 
442724c80e1SAlex Deucher 	dev_info(rdev->dev, "WB %sabled\n", rdev->wb.enabled ? "en" : "dis");
443724c80e1SAlex Deucher 
444724c80e1SAlex Deucher 	return 0;
445724c80e1SAlex Deucher }
446724c80e1SAlex Deucher 
447d594e46aSJerome Glisse /**
448d594e46aSJerome Glisse  * radeon_vram_location - try to find VRAM location
449d594e46aSJerome Glisse  * @rdev: radeon device structure holding all necessary informations
450d594e46aSJerome Glisse  * @mc: memory controller structure holding memory informations
451d594e46aSJerome Glisse  * @base: base address at which to put VRAM
452d594e46aSJerome Glisse  *
453d594e46aSJerome Glisse  * Function will place try to place VRAM at base address provided
454d594e46aSJerome Glisse  * as parameter (which is so far either PCI aperture address or
455d594e46aSJerome Glisse  * for IGP TOM base address).
456d594e46aSJerome Glisse  *
457d594e46aSJerome Glisse  * If there is not enough space to fit the unvisible VRAM in the 32bits
458d594e46aSJerome Glisse  * address space then we limit the VRAM size to the aperture.
459d594e46aSJerome Glisse  *
460d594e46aSJerome Glisse  * If we are using AGP and if the AGP aperture doesn't allow us to have
461d594e46aSJerome Glisse  * room for all the VRAM than we restrict the VRAM to the PCI aperture
462d594e46aSJerome Glisse  * size and print a warning.
463d594e46aSJerome Glisse  *
464d594e46aSJerome Glisse  * This function will never fails, worst case are limiting VRAM.
465d594e46aSJerome Glisse  *
466d594e46aSJerome Glisse  * Note: GTT start, end, size should be initialized before calling this
467d594e46aSJerome Glisse  * function on AGP platform.
468d594e46aSJerome Glisse  *
46925985edcSLucas De Marchi  * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
470d594e46aSJerome Glisse  * this shouldn't be a problem as we are using the PCI aperture as a reference.
471d594e46aSJerome Glisse  * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
472d594e46aSJerome Glisse  * not IGP.
473d594e46aSJerome Glisse  *
474d594e46aSJerome Glisse  * Note: we use mc_vram_size as on some board we need to program the mc to
475d594e46aSJerome Glisse  * cover the whole aperture even if VRAM size is inferior to aperture size
476d594e46aSJerome Glisse  * Novell bug 204882 + along with lots of ubuntu ones
477d594e46aSJerome Glisse  *
478d594e46aSJerome Glisse  * Note: when limiting vram it's safe to overwritte real_vram_size because
479d594e46aSJerome Glisse  * we are not in case where real_vram_size is inferior to mc_vram_size (ie
480d594e46aSJerome Glisse  * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
481d594e46aSJerome Glisse  * ones)
482d594e46aSJerome Glisse  *
483d594e46aSJerome Glisse  * Note: IGP TOM addr should be the same as the aperture addr, we don't
484d594e46aSJerome Glisse  * explicitly check for that thought.
485d594e46aSJerome Glisse  *
486d594e46aSJerome Glisse  * FIXME: when reducing VRAM size align new size on power of 2.
487771fe6b9SJerome Glisse  */
488d594e46aSJerome Glisse void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base)
489771fe6b9SJerome Glisse {
4901bcb04f7SChristian König 	uint64_t limit = (uint64_t)radeon_vram_limit << 20;
4911bcb04f7SChristian König 
492d594e46aSJerome Glisse 	mc->vram_start = base;
4939ed8b1f9SAlex Deucher 	if (mc->mc_vram_size > (rdev->mc.mc_mask - base + 1)) {
494d594e46aSJerome Glisse 		dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
495d594e46aSJerome Glisse 		mc->real_vram_size = mc->aper_size;
496d594e46aSJerome Glisse 		mc->mc_vram_size = mc->aper_size;
497771fe6b9SJerome Glisse 	}
498d594e46aSJerome Glisse 	mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
4992cbeb4efSJerome Glisse 	if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_start <= mc->gtt_end) {
500d594e46aSJerome Glisse 		dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
501d594e46aSJerome Glisse 		mc->real_vram_size = mc->aper_size;
502d594e46aSJerome Glisse 		mc->mc_vram_size = mc->aper_size;
503771fe6b9SJerome Glisse 	}
504d594e46aSJerome Glisse 	mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
5051bcb04f7SChristian König 	if (limit && limit < mc->real_vram_size)
5061bcb04f7SChristian König 		mc->real_vram_size = limit;
507dd7cc55aSAlex Deucher 	dev_info(rdev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
508d594e46aSJerome Glisse 			mc->mc_vram_size >> 20, mc->vram_start,
509d594e46aSJerome Glisse 			mc->vram_end, mc->real_vram_size >> 20);
510771fe6b9SJerome Glisse }
511771fe6b9SJerome Glisse 
512d594e46aSJerome Glisse /**
513d594e46aSJerome Glisse  * radeon_gtt_location - try to find GTT location
514d594e46aSJerome Glisse  * @rdev: radeon device structure holding all necessary informations
515d594e46aSJerome Glisse  * @mc: memory controller structure holding memory informations
516d594e46aSJerome Glisse  *
517d594e46aSJerome Glisse  * Function will place try to place GTT before or after VRAM.
518d594e46aSJerome Glisse  *
519d594e46aSJerome Glisse  * If GTT size is bigger than space left then we ajust GTT size.
520d594e46aSJerome Glisse  * Thus function will never fails.
521d594e46aSJerome Glisse  *
522d594e46aSJerome Glisse  * FIXME: when reducing GTT size align new size on power of 2.
523d594e46aSJerome Glisse  */
524d594e46aSJerome Glisse void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc)
525d594e46aSJerome Glisse {
526d594e46aSJerome Glisse 	u64 size_af, size_bf;
527d594e46aSJerome Glisse 
5289ed8b1f9SAlex Deucher 	size_af = ((rdev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
5298d369bb1SAlex Deucher 	size_bf = mc->vram_start & ~mc->gtt_base_align;
530d594e46aSJerome Glisse 	if (size_bf > size_af) {
531d594e46aSJerome Glisse 		if (mc->gtt_size > size_bf) {
532d594e46aSJerome Glisse 			dev_warn(rdev->dev, "limiting GTT\n");
533d594e46aSJerome Glisse 			mc->gtt_size = size_bf;
534d594e46aSJerome Glisse 		}
5358d369bb1SAlex Deucher 		mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
536d594e46aSJerome Glisse 	} else {
537d594e46aSJerome Glisse 		if (mc->gtt_size > size_af) {
538d594e46aSJerome Glisse 			dev_warn(rdev->dev, "limiting GTT\n");
539d594e46aSJerome Glisse 			mc->gtt_size = size_af;
540d594e46aSJerome Glisse 		}
5418d369bb1SAlex Deucher 		mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
542d594e46aSJerome Glisse 	}
543d594e46aSJerome Glisse 	mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
544dd7cc55aSAlex Deucher 	dev_info(rdev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
545d594e46aSJerome Glisse 			mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
546d594e46aSJerome Glisse }
547771fe6b9SJerome Glisse 
548771fe6b9SJerome Glisse /*
549771fe6b9SJerome Glisse  * GPU helpers function.
550771fe6b9SJerome Glisse  */
5510c195119SAlex Deucher /**
5520c195119SAlex Deucher  * radeon_card_posted - check if the hw has already been initialized
5530c195119SAlex Deucher  *
5540c195119SAlex Deucher  * @rdev: radeon_device pointer
5550c195119SAlex Deucher  *
5560c195119SAlex Deucher  * Check if the asic has been initialized (all asics).
5570c195119SAlex Deucher  * Used at driver startup.
5580c195119SAlex Deucher  * Returns true if initialized or false if not.
5590c195119SAlex Deucher  */
5609f022ddfSJerome Glisse bool radeon_card_posted(struct radeon_device *rdev)
561771fe6b9SJerome Glisse {
562771fe6b9SJerome Glisse 	uint32_t reg;
563771fe6b9SJerome Glisse 
56450a583f6SAlex Deucher 	/* required for EFI mode on macbook2,1 which uses an r5xx asic */
56583e68189SMatt Fleming 	if (efi_enabled(EFI_BOOT) &&
56650a583f6SAlex Deucher 	    (rdev->pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE) &&
56750a583f6SAlex Deucher 	    (rdev->family < CHIP_R600))
568bcc65fd8SMatthew Garrett 		return false;
569bcc65fd8SMatthew Garrett 
5702cf3a4fcSAlex Deucher 	if (ASIC_IS_NODCE(rdev))
5712cf3a4fcSAlex Deucher 		goto check_memsize;
5722cf3a4fcSAlex Deucher 
573771fe6b9SJerome Glisse 	/* first check CRTCs */
57409fb8bd1SAlex Deucher 	if (ASIC_IS_DCE4(rdev)) {
57518007401SAlex Deucher 		reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
57618007401SAlex Deucher 			RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET);
57709fb8bd1SAlex Deucher 			if (rdev->num_crtc >= 4) {
57809fb8bd1SAlex Deucher 				reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) |
57909fb8bd1SAlex Deucher 					RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET);
58009fb8bd1SAlex Deucher 			}
58109fb8bd1SAlex Deucher 			if (rdev->num_crtc >= 6) {
58209fb8bd1SAlex Deucher 				reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) |
583bcc1c2a1SAlex Deucher 					RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET);
58409fb8bd1SAlex Deucher 			}
585bcc1c2a1SAlex Deucher 		if (reg & EVERGREEN_CRTC_MASTER_EN)
586bcc1c2a1SAlex Deucher 			return true;
587bcc1c2a1SAlex Deucher 	} else if (ASIC_IS_AVIVO(rdev)) {
588771fe6b9SJerome Glisse 		reg = RREG32(AVIVO_D1CRTC_CONTROL) |
589771fe6b9SJerome Glisse 		      RREG32(AVIVO_D2CRTC_CONTROL);
590771fe6b9SJerome Glisse 		if (reg & AVIVO_CRTC_EN) {
591771fe6b9SJerome Glisse 			return true;
592771fe6b9SJerome Glisse 		}
593771fe6b9SJerome Glisse 	} else {
594771fe6b9SJerome Glisse 		reg = RREG32(RADEON_CRTC_GEN_CNTL) |
595771fe6b9SJerome Glisse 		      RREG32(RADEON_CRTC2_GEN_CNTL);
596771fe6b9SJerome Glisse 		if (reg & RADEON_CRTC_EN) {
597771fe6b9SJerome Glisse 			return true;
598771fe6b9SJerome Glisse 		}
599771fe6b9SJerome Glisse 	}
600771fe6b9SJerome Glisse 
6012cf3a4fcSAlex Deucher check_memsize:
602771fe6b9SJerome Glisse 	/* then check MEM_SIZE, in case the crtcs are off */
603771fe6b9SJerome Glisse 	if (rdev->family >= CHIP_R600)
604771fe6b9SJerome Glisse 		reg = RREG32(R600_CONFIG_MEMSIZE);
605771fe6b9SJerome Glisse 	else
606771fe6b9SJerome Glisse 		reg = RREG32(RADEON_CONFIG_MEMSIZE);
607771fe6b9SJerome Glisse 
608771fe6b9SJerome Glisse 	if (reg)
609771fe6b9SJerome Glisse 		return true;
610771fe6b9SJerome Glisse 
611771fe6b9SJerome Glisse 	return false;
612771fe6b9SJerome Glisse 
613771fe6b9SJerome Glisse }
614771fe6b9SJerome Glisse 
6150c195119SAlex Deucher /**
6160c195119SAlex Deucher  * radeon_update_bandwidth_info - update display bandwidth params
6170c195119SAlex Deucher  *
6180c195119SAlex Deucher  * @rdev: radeon_device pointer
6190c195119SAlex Deucher  *
6200c195119SAlex Deucher  * Used when sclk/mclk are switched or display modes are set.
6210c195119SAlex Deucher  * params are used to calculate display watermarks (all asics)
6220c195119SAlex Deucher  */
623f47299c5SAlex Deucher void radeon_update_bandwidth_info(struct radeon_device *rdev)
624f47299c5SAlex Deucher {
625f47299c5SAlex Deucher 	fixed20_12 a;
6268807286eSAlex Deucher 	u32 sclk = rdev->pm.current_sclk;
6278807286eSAlex Deucher 	u32 mclk = rdev->pm.current_mclk;
628f47299c5SAlex Deucher 
6298807286eSAlex Deucher 	/* sclk/mclk in Mhz */
63068adac5eSBen Skeggs 	a.full = dfixed_const(100);
63168adac5eSBen Skeggs 	rdev->pm.sclk.full = dfixed_const(sclk);
63268adac5eSBen Skeggs 	rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a);
63368adac5eSBen Skeggs 	rdev->pm.mclk.full = dfixed_const(mclk);
63468adac5eSBen Skeggs 	rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a);
635f47299c5SAlex Deucher 
6368807286eSAlex Deucher 	if (rdev->flags & RADEON_IS_IGP) {
63768adac5eSBen Skeggs 		a.full = dfixed_const(16);
638f47299c5SAlex Deucher 		/* core_bandwidth = sclk(Mhz) * 16 */
63968adac5eSBen Skeggs 		rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a);
640f47299c5SAlex Deucher 	}
641f47299c5SAlex Deucher }
642f47299c5SAlex Deucher 
6430c195119SAlex Deucher /**
6440c195119SAlex Deucher  * radeon_boot_test_post_card - check and possibly initialize the hw
6450c195119SAlex Deucher  *
6460c195119SAlex Deucher  * @rdev: radeon_device pointer
6470c195119SAlex Deucher  *
6480c195119SAlex Deucher  * Check if the asic is initialized and if not, attempt to initialize
6490c195119SAlex Deucher  * it (all asics).
6500c195119SAlex Deucher  * Returns true if initialized or false if not.
6510c195119SAlex Deucher  */
65272542d77SDave Airlie bool radeon_boot_test_post_card(struct radeon_device *rdev)
65372542d77SDave Airlie {
65472542d77SDave Airlie 	if (radeon_card_posted(rdev))
65572542d77SDave Airlie 		return true;
65672542d77SDave Airlie 
65772542d77SDave Airlie 	if (rdev->bios) {
65872542d77SDave Airlie 		DRM_INFO("GPU not posted. posting now...\n");
65972542d77SDave Airlie 		if (rdev->is_atom_bios)
66072542d77SDave Airlie 			atom_asic_init(rdev->mode_info.atom_context);
66172542d77SDave Airlie 		else
66272542d77SDave Airlie 			radeon_combios_asic_init(rdev->ddev);
66372542d77SDave Airlie 		return true;
66472542d77SDave Airlie 	} else {
66572542d77SDave Airlie 		dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
66672542d77SDave Airlie 		return false;
66772542d77SDave Airlie 	}
66872542d77SDave Airlie }
66972542d77SDave Airlie 
6700c195119SAlex Deucher /**
6710c195119SAlex Deucher  * radeon_dummy_page_init - init dummy page used by the driver
6720c195119SAlex Deucher  *
6730c195119SAlex Deucher  * @rdev: radeon_device pointer
6740c195119SAlex Deucher  *
6750c195119SAlex Deucher  * Allocate the dummy page used by the driver (all asics).
6760c195119SAlex Deucher  * This dummy page is used by the driver as a filler for gart entries
6770c195119SAlex Deucher  * when pages are taken out of the GART
6780c195119SAlex Deucher  * Returns 0 on sucess, -ENOMEM on failure.
6790c195119SAlex Deucher  */
6803ce0a23dSJerome Glisse int radeon_dummy_page_init(struct radeon_device *rdev)
6813ce0a23dSJerome Glisse {
68282568565SDave Airlie 	if (rdev->dummy_page.page)
68382568565SDave Airlie 		return 0;
6843ce0a23dSJerome Glisse 	rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
6853ce0a23dSJerome Glisse 	if (rdev->dummy_page.page == NULL)
6863ce0a23dSJerome Glisse 		return -ENOMEM;
6873ce0a23dSJerome Glisse 	rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page,
6883ce0a23dSJerome Glisse 					0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
689a30f6fb7SBenjamin Herrenschmidt 	if (pci_dma_mapping_error(rdev->pdev, rdev->dummy_page.addr)) {
690a30f6fb7SBenjamin Herrenschmidt 		dev_err(&rdev->pdev->dev, "Failed to DMA MAP the dummy page\n");
6913ce0a23dSJerome Glisse 		__free_page(rdev->dummy_page.page);
6923ce0a23dSJerome Glisse 		rdev->dummy_page.page = NULL;
6933ce0a23dSJerome Glisse 		return -ENOMEM;
6943ce0a23dSJerome Glisse 	}
6953ce0a23dSJerome Glisse 	return 0;
6963ce0a23dSJerome Glisse }
6973ce0a23dSJerome Glisse 
6980c195119SAlex Deucher /**
6990c195119SAlex Deucher  * radeon_dummy_page_fini - free dummy page used by the driver
7000c195119SAlex Deucher  *
7010c195119SAlex Deucher  * @rdev: radeon_device pointer
7020c195119SAlex Deucher  *
7030c195119SAlex Deucher  * Frees the dummy page used by the driver (all asics).
7040c195119SAlex Deucher  */
7053ce0a23dSJerome Glisse void radeon_dummy_page_fini(struct radeon_device *rdev)
7063ce0a23dSJerome Glisse {
7073ce0a23dSJerome Glisse 	if (rdev->dummy_page.page == NULL)
7083ce0a23dSJerome Glisse 		return;
7093ce0a23dSJerome Glisse 	pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
7103ce0a23dSJerome Glisse 			PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
7113ce0a23dSJerome Glisse 	__free_page(rdev->dummy_page.page);
7123ce0a23dSJerome Glisse 	rdev->dummy_page.page = NULL;
7133ce0a23dSJerome Glisse }
7143ce0a23dSJerome Glisse 
715771fe6b9SJerome Glisse 
716771fe6b9SJerome Glisse /* ATOM accessor methods */
7170c195119SAlex Deucher /*
7180c195119SAlex Deucher  * ATOM is an interpreted byte code stored in tables in the vbios.  The
7190c195119SAlex Deucher  * driver registers callbacks to access registers and the interpreter
7200c195119SAlex Deucher  * in the driver parses the tables and executes then to program specific
7210c195119SAlex Deucher  * actions (set display modes, asic init, etc.).  See radeon_atombios.c,
7220c195119SAlex Deucher  * atombios.h, and atom.c
7230c195119SAlex Deucher  */
7240c195119SAlex Deucher 
7250c195119SAlex Deucher /**
7260c195119SAlex Deucher  * cail_pll_read - read PLL register
7270c195119SAlex Deucher  *
7280c195119SAlex Deucher  * @info: atom card_info pointer
7290c195119SAlex Deucher  * @reg: PLL register offset
7300c195119SAlex Deucher  *
7310c195119SAlex Deucher  * Provides a PLL register accessor for the atom interpreter (r4xx+).
7320c195119SAlex Deucher  * Returns the value of the PLL register.
7330c195119SAlex Deucher  */
734771fe6b9SJerome Glisse static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
735771fe6b9SJerome Glisse {
736771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
737771fe6b9SJerome Glisse 	uint32_t r;
738771fe6b9SJerome Glisse 
739771fe6b9SJerome Glisse 	r = rdev->pll_rreg(rdev, reg);
740771fe6b9SJerome Glisse 	return r;
741771fe6b9SJerome Glisse }
742771fe6b9SJerome Glisse 
7430c195119SAlex Deucher /**
7440c195119SAlex Deucher  * cail_pll_write - write PLL register
7450c195119SAlex Deucher  *
7460c195119SAlex Deucher  * @info: atom card_info pointer
7470c195119SAlex Deucher  * @reg: PLL register offset
7480c195119SAlex Deucher  * @val: value to write to the pll register
7490c195119SAlex Deucher  *
7500c195119SAlex Deucher  * Provides a PLL register accessor for the atom interpreter (r4xx+).
7510c195119SAlex Deucher  */
752771fe6b9SJerome Glisse static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
753771fe6b9SJerome Glisse {
754771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
755771fe6b9SJerome Glisse 
756771fe6b9SJerome Glisse 	rdev->pll_wreg(rdev, reg, val);
757771fe6b9SJerome Glisse }
758771fe6b9SJerome Glisse 
7590c195119SAlex Deucher /**
7600c195119SAlex Deucher  * cail_mc_read - read MC (Memory Controller) register
7610c195119SAlex Deucher  *
7620c195119SAlex Deucher  * @info: atom card_info pointer
7630c195119SAlex Deucher  * @reg: MC register offset
7640c195119SAlex Deucher  *
7650c195119SAlex Deucher  * Provides an MC register accessor for the atom interpreter (r4xx+).
7660c195119SAlex Deucher  * Returns the value of the MC register.
7670c195119SAlex Deucher  */
768771fe6b9SJerome Glisse static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
769771fe6b9SJerome Glisse {
770771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
771771fe6b9SJerome Glisse 	uint32_t r;
772771fe6b9SJerome Glisse 
773771fe6b9SJerome Glisse 	r = rdev->mc_rreg(rdev, reg);
774771fe6b9SJerome Glisse 	return r;
775771fe6b9SJerome Glisse }
776771fe6b9SJerome Glisse 
7770c195119SAlex Deucher /**
7780c195119SAlex Deucher  * cail_mc_write - write MC (Memory Controller) register
7790c195119SAlex Deucher  *
7800c195119SAlex Deucher  * @info: atom card_info pointer
7810c195119SAlex Deucher  * @reg: MC register offset
7820c195119SAlex Deucher  * @val: value to write to the pll register
7830c195119SAlex Deucher  *
7840c195119SAlex Deucher  * Provides a MC register accessor for the atom interpreter (r4xx+).
7850c195119SAlex Deucher  */
786771fe6b9SJerome Glisse static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
787771fe6b9SJerome Glisse {
788771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
789771fe6b9SJerome Glisse 
790771fe6b9SJerome Glisse 	rdev->mc_wreg(rdev, reg, val);
791771fe6b9SJerome Glisse }
792771fe6b9SJerome Glisse 
7930c195119SAlex Deucher /**
7940c195119SAlex Deucher  * cail_reg_write - write MMIO register
7950c195119SAlex Deucher  *
7960c195119SAlex Deucher  * @info: atom card_info pointer
7970c195119SAlex Deucher  * @reg: MMIO register offset
7980c195119SAlex Deucher  * @val: value to write to the pll register
7990c195119SAlex Deucher  *
8000c195119SAlex Deucher  * Provides a MMIO register accessor for the atom interpreter (r4xx+).
8010c195119SAlex Deucher  */
802771fe6b9SJerome Glisse static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
803771fe6b9SJerome Glisse {
804771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
805771fe6b9SJerome Glisse 
806771fe6b9SJerome Glisse 	WREG32(reg*4, val);
807771fe6b9SJerome Glisse }
808771fe6b9SJerome Glisse 
8090c195119SAlex Deucher /**
8100c195119SAlex Deucher  * cail_reg_read - read MMIO register
8110c195119SAlex Deucher  *
8120c195119SAlex Deucher  * @info: atom card_info pointer
8130c195119SAlex Deucher  * @reg: MMIO register offset
8140c195119SAlex Deucher  *
8150c195119SAlex Deucher  * Provides an MMIO register accessor for the atom interpreter (r4xx+).
8160c195119SAlex Deucher  * Returns the value of the MMIO register.
8170c195119SAlex Deucher  */
818771fe6b9SJerome Glisse static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
819771fe6b9SJerome Glisse {
820771fe6b9SJerome Glisse 	struct radeon_device *rdev = info->dev->dev_private;
821771fe6b9SJerome Glisse 	uint32_t r;
822771fe6b9SJerome Glisse 
823771fe6b9SJerome Glisse 	r = RREG32(reg*4);
824771fe6b9SJerome Glisse 	return r;
825771fe6b9SJerome Glisse }
826771fe6b9SJerome Glisse 
8270c195119SAlex Deucher /**
8280c195119SAlex Deucher  * cail_ioreg_write - write IO register
8290c195119SAlex Deucher  *
8300c195119SAlex Deucher  * @info: atom card_info pointer
8310c195119SAlex Deucher  * @reg: IO register offset
8320c195119SAlex Deucher  * @val: value to write to the pll register
8330c195119SAlex Deucher  *
8340c195119SAlex Deucher  * Provides a IO register accessor for the atom interpreter (r4xx+).
8350c195119SAlex Deucher  */
836351a52a2SAlex Deucher static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
837351a52a2SAlex Deucher {
838351a52a2SAlex Deucher 	struct radeon_device *rdev = info->dev->dev_private;
839351a52a2SAlex Deucher 
840351a52a2SAlex Deucher 	WREG32_IO(reg*4, val);
841351a52a2SAlex Deucher }
842351a52a2SAlex Deucher 
8430c195119SAlex Deucher /**
8440c195119SAlex Deucher  * cail_ioreg_read - read IO register
8450c195119SAlex Deucher  *
8460c195119SAlex Deucher  * @info: atom card_info pointer
8470c195119SAlex Deucher  * @reg: IO register offset
8480c195119SAlex Deucher  *
8490c195119SAlex Deucher  * Provides an IO register accessor for the atom interpreter (r4xx+).
8500c195119SAlex Deucher  * Returns the value of the IO register.
8510c195119SAlex Deucher  */
852351a52a2SAlex Deucher static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
853351a52a2SAlex Deucher {
854351a52a2SAlex Deucher 	struct radeon_device *rdev = info->dev->dev_private;
855351a52a2SAlex Deucher 	uint32_t r;
856351a52a2SAlex Deucher 
857351a52a2SAlex Deucher 	r = RREG32_IO(reg*4);
858351a52a2SAlex Deucher 	return r;
859351a52a2SAlex Deucher }
860351a52a2SAlex Deucher 
8610c195119SAlex Deucher /**
8620c195119SAlex Deucher  * radeon_atombios_init - init the driver info and callbacks for atombios
8630c195119SAlex Deucher  *
8640c195119SAlex Deucher  * @rdev: radeon_device pointer
8650c195119SAlex Deucher  *
8660c195119SAlex Deucher  * Initializes the driver info and register access callbacks for the
8670c195119SAlex Deucher  * ATOM interpreter (r4xx+).
8680c195119SAlex Deucher  * Returns 0 on sucess, -ENOMEM on failure.
8690c195119SAlex Deucher  * Called at driver startup.
8700c195119SAlex Deucher  */
871771fe6b9SJerome Glisse int radeon_atombios_init(struct radeon_device *rdev)
872771fe6b9SJerome Glisse {
87361c4b24bSMathias Fröhlich 	struct card_info *atom_card_info =
87461c4b24bSMathias Fröhlich 	    kzalloc(sizeof(struct card_info), GFP_KERNEL);
87561c4b24bSMathias Fröhlich 
87661c4b24bSMathias Fröhlich 	if (!atom_card_info)
87761c4b24bSMathias Fröhlich 		return -ENOMEM;
87861c4b24bSMathias Fröhlich 
87961c4b24bSMathias Fröhlich 	rdev->mode_info.atom_card_info = atom_card_info;
88061c4b24bSMathias Fröhlich 	atom_card_info->dev = rdev->ddev;
88161c4b24bSMathias Fröhlich 	atom_card_info->reg_read = cail_reg_read;
88261c4b24bSMathias Fröhlich 	atom_card_info->reg_write = cail_reg_write;
883351a52a2SAlex Deucher 	/* needed for iio ops */
884351a52a2SAlex Deucher 	if (rdev->rio_mem) {
885351a52a2SAlex Deucher 		atom_card_info->ioreg_read = cail_ioreg_read;
886351a52a2SAlex Deucher 		atom_card_info->ioreg_write = cail_ioreg_write;
887351a52a2SAlex Deucher 	} else {
888351a52a2SAlex Deucher 		DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
889351a52a2SAlex Deucher 		atom_card_info->ioreg_read = cail_reg_read;
890351a52a2SAlex Deucher 		atom_card_info->ioreg_write = cail_reg_write;
891351a52a2SAlex Deucher 	}
89261c4b24bSMathias Fröhlich 	atom_card_info->mc_read = cail_mc_read;
89361c4b24bSMathias Fröhlich 	atom_card_info->mc_write = cail_mc_write;
89461c4b24bSMathias Fröhlich 	atom_card_info->pll_read = cail_pll_read;
89561c4b24bSMathias Fröhlich 	atom_card_info->pll_write = cail_pll_write;
89661c4b24bSMathias Fröhlich 
89761c4b24bSMathias Fröhlich 	rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
8980e34d094STim Gardner 	if (!rdev->mode_info.atom_context) {
8990e34d094STim Gardner 		radeon_atombios_fini(rdev);
9000e34d094STim Gardner 		return -ENOMEM;
9010e34d094STim Gardner 	}
9020e34d094STim Gardner 
903c31ad97fSRafał Miłecki 	mutex_init(&rdev->mode_info.atom_context->mutex);
904771fe6b9SJerome Glisse 	radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
905d904ef9bSDave Airlie 	atom_allocate_fb_scratch(rdev->mode_info.atom_context);
906771fe6b9SJerome Glisse 	return 0;
907771fe6b9SJerome Glisse }
908771fe6b9SJerome Glisse 
9090c195119SAlex Deucher /**
9100c195119SAlex Deucher  * radeon_atombios_fini - free the driver info and callbacks for atombios
9110c195119SAlex Deucher  *
9120c195119SAlex Deucher  * @rdev: radeon_device pointer
9130c195119SAlex Deucher  *
9140c195119SAlex Deucher  * Frees the driver info and register access callbacks for the ATOM
9150c195119SAlex Deucher  * interpreter (r4xx+).
9160c195119SAlex Deucher  * Called at driver shutdown.
9170c195119SAlex Deucher  */
918771fe6b9SJerome Glisse void radeon_atombios_fini(struct radeon_device *rdev)
919771fe6b9SJerome Glisse {
9204a04a844SJerome Glisse 	if (rdev->mode_info.atom_context) {
921d904ef9bSDave Airlie 		kfree(rdev->mode_info.atom_context->scratch);
9224a04a844SJerome Glisse 	}
9230e34d094STim Gardner 	kfree(rdev->mode_info.atom_context);
9240e34d094STim Gardner 	rdev->mode_info.atom_context = NULL;
92561c4b24bSMathias Fröhlich 	kfree(rdev->mode_info.atom_card_info);
9260e34d094STim Gardner 	rdev->mode_info.atom_card_info = NULL;
927771fe6b9SJerome Glisse }
928771fe6b9SJerome Glisse 
9290c195119SAlex Deucher /* COMBIOS */
9300c195119SAlex Deucher /*
9310c195119SAlex Deucher  * COMBIOS is the bios format prior to ATOM. It provides
9320c195119SAlex Deucher  * command tables similar to ATOM, but doesn't have a unified
9330c195119SAlex Deucher  * parser.  See radeon_combios.c
9340c195119SAlex Deucher  */
9350c195119SAlex Deucher 
9360c195119SAlex Deucher /**
9370c195119SAlex Deucher  * radeon_combios_init - init the driver info for combios
9380c195119SAlex Deucher  *
9390c195119SAlex Deucher  * @rdev: radeon_device pointer
9400c195119SAlex Deucher  *
9410c195119SAlex Deucher  * Initializes the driver info for combios (r1xx-r3xx).
9420c195119SAlex Deucher  * Returns 0 on sucess.
9430c195119SAlex Deucher  * Called at driver startup.
9440c195119SAlex Deucher  */
945771fe6b9SJerome Glisse int radeon_combios_init(struct radeon_device *rdev)
946771fe6b9SJerome Glisse {
947771fe6b9SJerome Glisse 	radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
948771fe6b9SJerome Glisse 	return 0;
949771fe6b9SJerome Glisse }
950771fe6b9SJerome Glisse 
9510c195119SAlex Deucher /**
9520c195119SAlex Deucher  * radeon_combios_fini - free the driver info for combios
9530c195119SAlex Deucher  *
9540c195119SAlex Deucher  * @rdev: radeon_device pointer
9550c195119SAlex Deucher  *
9560c195119SAlex Deucher  * Frees the driver info for combios (r1xx-r3xx).
9570c195119SAlex Deucher  * Called at driver shutdown.
9580c195119SAlex Deucher  */
959771fe6b9SJerome Glisse void radeon_combios_fini(struct radeon_device *rdev)
960771fe6b9SJerome Glisse {
961771fe6b9SJerome Glisse }
962771fe6b9SJerome Glisse 
9630c195119SAlex Deucher /* if we get transitioned to only one device, take VGA back */
9640c195119SAlex Deucher /**
9650c195119SAlex Deucher  * radeon_vga_set_decode - enable/disable vga decode
9660c195119SAlex Deucher  *
9670c195119SAlex Deucher  * @cookie: radeon_device pointer
9680c195119SAlex Deucher  * @state: enable/disable vga decode
9690c195119SAlex Deucher  *
9700c195119SAlex Deucher  * Enable/disable vga decode (all asics).
9710c195119SAlex Deucher  * Returns VGA resource flags.
9720c195119SAlex Deucher  */
97328d52043SDave Airlie static unsigned int radeon_vga_set_decode(void *cookie, bool state)
97428d52043SDave Airlie {
97528d52043SDave Airlie 	struct radeon_device *rdev = cookie;
97628d52043SDave Airlie 	radeon_vga_set_state(rdev, state);
97728d52043SDave Airlie 	if (state)
97828d52043SDave Airlie 		return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
97928d52043SDave Airlie 		       VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
98028d52043SDave Airlie 	else
98128d52043SDave Airlie 		return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
98228d52043SDave Airlie }
983c1176d6fSDave Airlie 
9840c195119SAlex Deucher /**
9851bcb04f7SChristian König  * radeon_check_pot_argument - check that argument is a power of two
9861bcb04f7SChristian König  *
9871bcb04f7SChristian König  * @arg: value to check
9881bcb04f7SChristian König  *
9891bcb04f7SChristian König  * Validates that a certain argument is a power of two (all asics).
9901bcb04f7SChristian König  * Returns true if argument is valid.
9911bcb04f7SChristian König  */
9921bcb04f7SChristian König static bool radeon_check_pot_argument(int arg)
9931bcb04f7SChristian König {
9941bcb04f7SChristian König 	return (arg & (arg - 1)) == 0;
9951bcb04f7SChristian König }
9961bcb04f7SChristian König 
9971bcb04f7SChristian König /**
9980c195119SAlex Deucher  * radeon_check_arguments - validate module params
9990c195119SAlex Deucher  *
10000c195119SAlex Deucher  * @rdev: radeon_device pointer
10010c195119SAlex Deucher  *
10020c195119SAlex Deucher  * Validates certain module parameters and updates
10030c195119SAlex Deucher  * the associated values used by the driver (all asics).
10040c195119SAlex Deucher  */
10051109ca09SLauri Kasanen static void radeon_check_arguments(struct radeon_device *rdev)
100636421338SJerome Glisse {
100736421338SJerome Glisse 	/* vramlimit must be a power of two */
10081bcb04f7SChristian König 	if (!radeon_check_pot_argument(radeon_vram_limit)) {
100936421338SJerome Glisse 		dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n",
101036421338SJerome Glisse 				radeon_vram_limit);
101136421338SJerome Glisse 		radeon_vram_limit = 0;
101236421338SJerome Glisse 	}
10131bcb04f7SChristian König 
1014edcd26e8SAlex Deucher 	if (radeon_gart_size == -1) {
1015edcd26e8SAlex Deucher 		/* default to a larger gart size on newer asics */
1016edcd26e8SAlex Deucher 		if (rdev->family >= CHIP_RV770)
1017edcd26e8SAlex Deucher 			radeon_gart_size = 1024;
1018edcd26e8SAlex Deucher 		else
1019edcd26e8SAlex Deucher 			radeon_gart_size = 512;
1020edcd26e8SAlex Deucher 	}
102136421338SJerome Glisse 	/* gtt size must be power of two and greater or equal to 32M */
10221bcb04f7SChristian König 	if (radeon_gart_size < 32) {
1023edcd26e8SAlex Deucher 		dev_warn(rdev->dev, "gart size (%d) too small\n",
102436421338SJerome Glisse 				radeon_gart_size);
1025edcd26e8SAlex Deucher 		if (rdev->family >= CHIP_RV770)
1026edcd26e8SAlex Deucher 			radeon_gart_size = 1024;
1027edcd26e8SAlex Deucher 		else
102836421338SJerome Glisse 			radeon_gart_size = 512;
10291bcb04f7SChristian König 	} else if (!radeon_check_pot_argument(radeon_gart_size)) {
103036421338SJerome Glisse 		dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
103136421338SJerome Glisse 				radeon_gart_size);
1032edcd26e8SAlex Deucher 		if (rdev->family >= CHIP_RV770)
1033edcd26e8SAlex Deucher 			radeon_gart_size = 1024;
1034edcd26e8SAlex Deucher 		else
103536421338SJerome Glisse 			radeon_gart_size = 512;
103636421338SJerome Glisse 	}
10371bcb04f7SChristian König 	rdev->mc.gtt_size = (uint64_t)radeon_gart_size << 20;
10381bcb04f7SChristian König 
103936421338SJerome Glisse 	/* AGP mode can only be -1, 1, 2, 4, 8 */
104036421338SJerome Glisse 	switch (radeon_agpmode) {
104136421338SJerome Glisse 	case -1:
104236421338SJerome Glisse 	case 0:
104336421338SJerome Glisse 	case 1:
104436421338SJerome Glisse 	case 2:
104536421338SJerome Glisse 	case 4:
104636421338SJerome Glisse 	case 8:
104736421338SJerome Glisse 		break;
104836421338SJerome Glisse 	default:
104936421338SJerome Glisse 		dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
105036421338SJerome Glisse 				"-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
105136421338SJerome Glisse 		radeon_agpmode = 0;
105236421338SJerome Glisse 		break;
105336421338SJerome Glisse 	}
105436421338SJerome Glisse }
105536421338SJerome Glisse 
10560c195119SAlex Deucher /**
1057d1f9809eSMaarten Lankhorst  * radeon_switcheroo_quirk_long_wakeup - return true if longer d3 delay is
1058d1f9809eSMaarten Lankhorst  * needed for waking up.
1059d1f9809eSMaarten Lankhorst  *
1060d1f9809eSMaarten Lankhorst  * @pdev: pci dev pointer
1061d1f9809eSMaarten Lankhorst  */
1062d1f9809eSMaarten Lankhorst static bool radeon_switcheroo_quirk_long_wakeup(struct pci_dev *pdev)
1063d1f9809eSMaarten Lankhorst {
1064d1f9809eSMaarten Lankhorst 
1065d1f9809eSMaarten Lankhorst 	/* 6600m in a macbook pro */
1066d1f9809eSMaarten Lankhorst 	if (pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE &&
1067d1f9809eSMaarten Lankhorst 	    pdev->subsystem_device == 0x00e2) {
1068d1f9809eSMaarten Lankhorst 		printk(KERN_INFO "radeon: quirking longer d3 wakeup delay\n");
1069d1f9809eSMaarten Lankhorst 		return true;
1070d1f9809eSMaarten Lankhorst 	}
1071d1f9809eSMaarten Lankhorst 
1072d1f9809eSMaarten Lankhorst 	return false;
1073d1f9809eSMaarten Lankhorst }
1074d1f9809eSMaarten Lankhorst 
1075d1f9809eSMaarten Lankhorst /**
10760c195119SAlex Deucher  * radeon_switcheroo_set_state - set switcheroo state
10770c195119SAlex Deucher  *
10780c195119SAlex Deucher  * @pdev: pci dev pointer
10790c195119SAlex Deucher  * @state: vga switcheroo state
10800c195119SAlex Deucher  *
10810c195119SAlex Deucher  * Callback for the switcheroo driver.  Suspends or resumes the
10820c195119SAlex Deucher  * the asics before or after it is powered up using ACPI methods.
10830c195119SAlex Deucher  */
10846a9ee8afSDave Airlie static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
10856a9ee8afSDave Airlie {
10866a9ee8afSDave Airlie 	struct drm_device *dev = pci_get_drvdata(pdev);
108710ebc0bcSDave Airlie 
1088*90c4cde9SAlex Deucher 	if (radeon_is_px(dev) && state == VGA_SWITCHEROO_OFF)
108910ebc0bcSDave Airlie 		return;
109010ebc0bcSDave Airlie 
10916a9ee8afSDave Airlie 	if (state == VGA_SWITCHEROO_ON) {
1092d1f9809eSMaarten Lankhorst 		unsigned d3_delay = dev->pdev->d3_delay;
1093d1f9809eSMaarten Lankhorst 
10946a9ee8afSDave Airlie 		printk(KERN_INFO "radeon: switched on\n");
10956a9ee8afSDave Airlie 		/* don't suspend or resume card normally */
10965bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1097d1f9809eSMaarten Lankhorst 
1098d1f9809eSMaarten Lankhorst 		if (d3_delay < 20 && radeon_switcheroo_quirk_long_wakeup(pdev))
1099d1f9809eSMaarten Lankhorst 			dev->pdev->d3_delay = 20;
1100d1f9809eSMaarten Lankhorst 
110110ebc0bcSDave Airlie 		radeon_resume_kms(dev, true, true);
1102d1f9809eSMaarten Lankhorst 
1103d1f9809eSMaarten Lankhorst 		dev->pdev->d3_delay = d3_delay;
1104d1f9809eSMaarten Lankhorst 
11055bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_ON;
1106fbf81762SDave Airlie 		drm_kms_helper_poll_enable(dev);
11076a9ee8afSDave Airlie 	} else {
11086a9ee8afSDave Airlie 		printk(KERN_INFO "radeon: switched off\n");
1109fbf81762SDave Airlie 		drm_kms_helper_poll_disable(dev);
11105bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
111110ebc0bcSDave Airlie 		radeon_suspend_kms(dev, true, true);
11125bcf719bSDave Airlie 		dev->switch_power_state = DRM_SWITCH_POWER_OFF;
11136a9ee8afSDave Airlie 	}
11146a9ee8afSDave Airlie }
11156a9ee8afSDave Airlie 
11160c195119SAlex Deucher /**
11170c195119SAlex Deucher  * radeon_switcheroo_can_switch - see if switcheroo state can change
11180c195119SAlex Deucher  *
11190c195119SAlex Deucher  * @pdev: pci dev pointer
11200c195119SAlex Deucher  *
11210c195119SAlex Deucher  * Callback for the switcheroo driver.  Check of the switcheroo
11220c195119SAlex Deucher  * state can be changed.
11230c195119SAlex Deucher  * Returns true if the state can be changed, false if not.
11240c195119SAlex Deucher  */
11256a9ee8afSDave Airlie static bool radeon_switcheroo_can_switch(struct pci_dev *pdev)
11266a9ee8afSDave Airlie {
11276a9ee8afSDave Airlie 	struct drm_device *dev = pci_get_drvdata(pdev);
11286a9ee8afSDave Airlie 	bool can_switch;
11296a9ee8afSDave Airlie 
11306a9ee8afSDave Airlie 	spin_lock(&dev->count_lock);
11316a9ee8afSDave Airlie 	can_switch = (dev->open_count == 0);
11326a9ee8afSDave Airlie 	spin_unlock(&dev->count_lock);
11336a9ee8afSDave Airlie 	return can_switch;
11346a9ee8afSDave Airlie }
11356a9ee8afSDave Airlie 
113626ec685fSTakashi Iwai static const struct vga_switcheroo_client_ops radeon_switcheroo_ops = {
113726ec685fSTakashi Iwai 	.set_gpu_state = radeon_switcheroo_set_state,
113826ec685fSTakashi Iwai 	.reprobe = NULL,
113926ec685fSTakashi Iwai 	.can_switch = radeon_switcheroo_can_switch,
114026ec685fSTakashi Iwai };
11416a9ee8afSDave Airlie 
11420c195119SAlex Deucher /**
11430c195119SAlex Deucher  * radeon_device_init - initialize the driver
11440c195119SAlex Deucher  *
11450c195119SAlex Deucher  * @rdev: radeon_device pointer
11460c195119SAlex Deucher  * @pdev: drm dev pointer
11470c195119SAlex Deucher  * @pdev: pci dev pointer
11480c195119SAlex Deucher  * @flags: driver flags
11490c195119SAlex Deucher  *
11500c195119SAlex Deucher  * Initializes the driver info and hw (all asics).
11510c195119SAlex Deucher  * Returns 0 for success or an error on failure.
11520c195119SAlex Deucher  * Called at driver startup.
11530c195119SAlex Deucher  */
1154771fe6b9SJerome Glisse int radeon_device_init(struct radeon_device *rdev,
1155771fe6b9SJerome Glisse 		       struct drm_device *ddev,
1156771fe6b9SJerome Glisse 		       struct pci_dev *pdev,
1157771fe6b9SJerome Glisse 		       uint32_t flags)
1158771fe6b9SJerome Glisse {
1159351a52a2SAlex Deucher 	int r, i;
1160ad49f501SDave Airlie 	int dma_bits;
116110ebc0bcSDave Airlie 	bool runtime = false;
1162771fe6b9SJerome Glisse 
1163771fe6b9SJerome Glisse 	rdev->shutdown = false;
11649f022ddfSJerome Glisse 	rdev->dev = &pdev->dev;
1165771fe6b9SJerome Glisse 	rdev->ddev = ddev;
1166771fe6b9SJerome Glisse 	rdev->pdev = pdev;
1167771fe6b9SJerome Glisse 	rdev->flags = flags;
1168771fe6b9SJerome Glisse 	rdev->family = flags & RADEON_FAMILY_MASK;
1169771fe6b9SJerome Glisse 	rdev->is_atom_bios = false;
1170771fe6b9SJerome Glisse 	rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
1171edcd26e8SAlex Deucher 	rdev->mc.gtt_size = 512 * 1024 * 1024;
1172733289c2SJerome Glisse 	rdev->accel_working = false;
11738b25ed34SAlex Deucher 	/* set up ring ids */
11748b25ed34SAlex Deucher 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
11758b25ed34SAlex Deucher 		rdev->ring[i].idx = i;
11768b25ed34SAlex Deucher 	}
11771b5331d9SJerome Glisse 
1178d522d9ccSThomas Reim 	DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X).\n",
1179d522d9ccSThomas Reim 		radeon_family_name[rdev->family], pdev->vendor, pdev->device,
1180d522d9ccSThomas Reim 		pdev->subsystem_vendor, pdev->subsystem_device);
11811b5331d9SJerome Glisse 
1182771fe6b9SJerome Glisse 	/* mutex initialization are all done here so we
1183771fe6b9SJerome Glisse 	 * can recall function without having locking issues */
1184d6999bc7SChristian König 	mutex_init(&rdev->ring_lock);
118540bacf16SAlex Deucher 	mutex_init(&rdev->dc_hw_i2c_mutex);
1186c20dc369SChristian Koenig 	atomic_set(&rdev->ih.lock, 0);
11874c788679SJerome Glisse 	mutex_init(&rdev->gem.mutex);
1188c913e23aSRafał Miłecki 	mutex_init(&rdev->pm.mutex);
11896759a0a7SMarek Olšák 	mutex_init(&rdev->gpu_clock_mutex);
1190f61d5b46SAlex Deucher 	mutex_init(&rdev->srbm_mutex);
1191db7fce39SChristian König 	init_rwsem(&rdev->pm.mclk_lock);
1192dee53e7fSJerome Glisse 	init_rwsem(&rdev->exclusive_lock);
119373a6d3fcSRafał Miłecki 	init_waitqueue_head(&rdev->irq.vblank_queue);
11941b9c3dd0SAlex Deucher 	r = radeon_gem_init(rdev);
11951b9c3dd0SAlex Deucher 	if (r)
11961b9c3dd0SAlex Deucher 		return r;
1197529364e0SChristian König 
119823d4f1f2SAlex Deucher 	/* Adjust VM size here.
119923d4f1f2SAlex Deucher 	 * Currently set to 4GB ((1 << 20) 4k pages).
120023d4f1f2SAlex Deucher 	 * Max GPUVM size for cayman and SI is 40 bits.
120123d4f1f2SAlex Deucher 	 */
1202721604a1SJerome Glisse 	rdev->vm_manager.max_pfn = 1 << 20;
1203771fe6b9SJerome Glisse 
12044aac0473SJerome Glisse 	/* Set asic functions */
12054aac0473SJerome Glisse 	r = radeon_asic_init(rdev);
120636421338SJerome Glisse 	if (r)
12074aac0473SJerome Glisse 		return r;
120836421338SJerome Glisse 	radeon_check_arguments(rdev);
12094aac0473SJerome Glisse 
1210f95df9caSAlex Deucher 	/* all of the newer IGP chips have an internal gart
1211f95df9caSAlex Deucher 	 * However some rs4xx report as AGP, so remove that here.
1212f95df9caSAlex Deucher 	 */
1213f95df9caSAlex Deucher 	if ((rdev->family >= CHIP_RS400) &&
1214f95df9caSAlex Deucher 	    (rdev->flags & RADEON_IS_IGP)) {
1215f95df9caSAlex Deucher 		rdev->flags &= ~RADEON_IS_AGP;
1216f95df9caSAlex Deucher 	}
1217f95df9caSAlex Deucher 
121830256a3fSJerome Glisse 	if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
1219b574f251SJerome Glisse 		radeon_agp_disable(rdev);
1220771fe6b9SJerome Glisse 	}
1221771fe6b9SJerome Glisse 
12229ed8b1f9SAlex Deucher 	/* Set the internal MC address mask
12239ed8b1f9SAlex Deucher 	 * This is the max address of the GPU's
12249ed8b1f9SAlex Deucher 	 * internal address space.
12259ed8b1f9SAlex Deucher 	 */
12269ed8b1f9SAlex Deucher 	if (rdev->family >= CHIP_CAYMAN)
12279ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xffffffffffULL; /* 40 bit MC */
12289ed8b1f9SAlex Deucher 	else if (rdev->family >= CHIP_CEDAR)
12299ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xfffffffffULL; /* 36 bit MC */
12309ed8b1f9SAlex Deucher 	else
12319ed8b1f9SAlex Deucher 		rdev->mc.mc_mask = 0xffffffffULL; /* 32 bit MC */
12329ed8b1f9SAlex Deucher 
1233ad49f501SDave Airlie 	/* set DMA mask + need_dma32 flags.
1234ad49f501SDave Airlie 	 * PCIE - can handle 40-bits.
1235005a83f1SAlex Deucher 	 * IGP - can handle 40-bits
1236ad49f501SDave Airlie 	 * AGP - generally dma32 is safest
1237005a83f1SAlex Deucher 	 * PCI - dma32 for legacy pci gart, 40 bits on newer asics
1238ad49f501SDave Airlie 	 */
1239ad49f501SDave Airlie 	rdev->need_dma32 = false;
1240ad49f501SDave Airlie 	if (rdev->flags & RADEON_IS_AGP)
1241ad49f501SDave Airlie 		rdev->need_dma32 = true;
1242005a83f1SAlex Deucher 	if ((rdev->flags & RADEON_IS_PCI) &&
12434a2b6662SJerome Glisse 	    (rdev->family <= CHIP_RS740))
1244ad49f501SDave Airlie 		rdev->need_dma32 = true;
1245ad49f501SDave Airlie 
1246ad49f501SDave Airlie 	dma_bits = rdev->need_dma32 ? 32 : 40;
1247ad49f501SDave Airlie 	r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
1248771fe6b9SJerome Glisse 	if (r) {
124962fff811SDaniel Haid 		rdev->need_dma32 = true;
1250c52494f6SKonrad Rzeszutek Wilk 		dma_bits = 32;
1251771fe6b9SJerome Glisse 		printk(KERN_WARNING "radeon: No suitable DMA available.\n");
1252771fe6b9SJerome Glisse 	}
1253c52494f6SKonrad Rzeszutek Wilk 	r = pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
1254c52494f6SKonrad Rzeszutek Wilk 	if (r) {
1255c52494f6SKonrad Rzeszutek Wilk 		pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(32));
1256c52494f6SKonrad Rzeszutek Wilk 		printk(KERN_WARNING "radeon: No coherent DMA available.\n");
1257c52494f6SKonrad Rzeszutek Wilk 	}
1258771fe6b9SJerome Glisse 
1259771fe6b9SJerome Glisse 	/* Registers mapping */
1260771fe6b9SJerome Glisse 	/* TODO: block userspace mapping of io register */
12612c385151SDaniel Vetter 	spin_lock_init(&rdev->mmio_idx_lock);
1262fe78118cSAlex Deucher 	spin_lock_init(&rdev->smc_idx_lock);
12630a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pll_idx_lock);
12640a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->mc_idx_lock);
12650a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pcie_idx_lock);
12660a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pciep_idx_lock);
12670a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->pif_idx_lock);
12680a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->cg_idx_lock);
12690a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->uvd_idx_lock);
12700a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->rcu_idx_lock);
12710a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->didt_idx_lock);
12720a5b7b0bSAlex Deucher 	spin_lock_init(&rdev->end_idx_lock);
1273efad86dbSAlex Deucher 	if (rdev->family >= CHIP_BONAIRE) {
1274efad86dbSAlex Deucher 		rdev->rmmio_base = pci_resource_start(rdev->pdev, 5);
1275efad86dbSAlex Deucher 		rdev->rmmio_size = pci_resource_len(rdev->pdev, 5);
1276efad86dbSAlex Deucher 	} else {
127701d73a69SJordan Crouse 		rdev->rmmio_base = pci_resource_start(rdev->pdev, 2);
127801d73a69SJordan Crouse 		rdev->rmmio_size = pci_resource_len(rdev->pdev, 2);
1279efad86dbSAlex Deucher 	}
1280771fe6b9SJerome Glisse 	rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size);
1281771fe6b9SJerome Glisse 	if (rdev->rmmio == NULL) {
1282771fe6b9SJerome Glisse 		return -ENOMEM;
1283771fe6b9SJerome Glisse 	}
1284771fe6b9SJerome Glisse 	DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base);
1285771fe6b9SJerome Glisse 	DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size);
1286771fe6b9SJerome Glisse 
128775efdee1SAlex Deucher 	/* doorbell bar mapping */
128875efdee1SAlex Deucher 	if (rdev->family >= CHIP_BONAIRE)
128975efdee1SAlex Deucher 		radeon_doorbell_init(rdev);
129075efdee1SAlex Deucher 
1291351a52a2SAlex Deucher 	/* io port mapping */
1292351a52a2SAlex Deucher 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1293351a52a2SAlex Deucher 		if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) {
1294351a52a2SAlex Deucher 			rdev->rio_mem_size = pci_resource_len(rdev->pdev, i);
1295351a52a2SAlex Deucher 			rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size);
1296351a52a2SAlex Deucher 			break;
1297351a52a2SAlex Deucher 		}
1298351a52a2SAlex Deucher 	}
1299351a52a2SAlex Deucher 	if (rdev->rio_mem == NULL)
1300351a52a2SAlex Deucher 		DRM_ERROR("Unable to find PCI I/O BAR\n");
1301351a52a2SAlex Deucher 
130228d52043SDave Airlie 	/* if we have > 1 VGA cards, then disable the radeon VGA resources */
130393239ea1SDave Airlie 	/* this will fail for cards that aren't VGA class devices, just
130493239ea1SDave Airlie 	 * ignore it */
130593239ea1SDave Airlie 	vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
130610ebc0bcSDave Airlie 
1307*90c4cde9SAlex Deucher 	if (rdev->flags & RADEON_IS_PX)
130810ebc0bcSDave Airlie 		runtime = true;
130910ebc0bcSDave Airlie 	vga_switcheroo_register_client(rdev->pdev, &radeon_switcheroo_ops, runtime);
131010ebc0bcSDave Airlie 	if (runtime)
131110ebc0bcSDave Airlie 		vga_switcheroo_init_domain_pm_ops(rdev->dev, &rdev->vga_pm_domain);
131228d52043SDave Airlie 
13133ce0a23dSJerome Glisse 	r = radeon_init(rdev);
1314b574f251SJerome Glisse 	if (r)
1315b574f251SJerome Glisse 		return r;
1316b1e3a6d1SMichel Dänzer 
131704eb2206SChristian König 	r = radeon_ib_ring_tests(rdev);
131804eb2206SChristian König 	if (r)
131904eb2206SChristian König 		DRM_ERROR("ib ring test failed (%d).\n", r);
132004eb2206SChristian König 
1321409851f4SJerome Glisse 	r = radeon_gem_debugfs_init(rdev);
1322409851f4SJerome Glisse 	if (r) {
1323409851f4SJerome Glisse 		DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1324409851f4SJerome Glisse 	}
1325409851f4SJerome Glisse 
1326b574f251SJerome Glisse 	if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
1327b574f251SJerome Glisse 		/* Acceleration not working on AGP card try again
1328b574f251SJerome Glisse 		 * with fallback to PCI or PCIE GART
1329b574f251SJerome Glisse 		 */
1330a2d07b74SJerome Glisse 		radeon_asic_reset(rdev);
1331b574f251SJerome Glisse 		radeon_fini(rdev);
1332b574f251SJerome Glisse 		radeon_agp_disable(rdev);
1333b574f251SJerome Glisse 		r = radeon_init(rdev);
13344aac0473SJerome Glisse 		if (r)
13354aac0473SJerome Glisse 			return r;
13363ce0a23dSJerome Glisse 	}
13376c7bcceaSAlex Deucher 
133860a7e396SChristian König 	if ((radeon_testing & 1)) {
13394a1132a0SAlex Deucher 		if (rdev->accel_working)
1340ecc0b326SMichel Dänzer 			radeon_test_moves(rdev);
13414a1132a0SAlex Deucher 		else
13424a1132a0SAlex Deucher 			DRM_INFO("radeon: acceleration disabled, skipping move tests\n");
1343ecc0b326SMichel Dänzer 	}
134460a7e396SChristian König 	if ((radeon_testing & 2)) {
13454a1132a0SAlex Deucher 		if (rdev->accel_working)
134660a7e396SChristian König 			radeon_test_syncing(rdev);
13474a1132a0SAlex Deucher 		else
13484a1132a0SAlex Deucher 			DRM_INFO("radeon: acceleration disabled, skipping sync tests\n");
134960a7e396SChristian König 	}
1350771fe6b9SJerome Glisse 	if (radeon_benchmarking) {
13514a1132a0SAlex Deucher 		if (rdev->accel_working)
1352638dd7dbSIlija Hadzic 			radeon_benchmark(rdev, radeon_benchmarking);
13534a1132a0SAlex Deucher 		else
13544a1132a0SAlex Deucher 			DRM_INFO("radeon: acceleration disabled, skipping benchmarks\n");
1355771fe6b9SJerome Glisse 	}
13566cf8a3f5SJerome Glisse 	return 0;
1357771fe6b9SJerome Glisse }
1358771fe6b9SJerome Glisse 
13594d8bf9aeSChristian König static void radeon_debugfs_remove_files(struct radeon_device *rdev);
13604d8bf9aeSChristian König 
13610c195119SAlex Deucher /**
13620c195119SAlex Deucher  * radeon_device_fini - tear down the driver
13630c195119SAlex Deucher  *
13640c195119SAlex Deucher  * @rdev: radeon_device pointer
13650c195119SAlex Deucher  *
13660c195119SAlex Deucher  * Tear down the driver info (all asics).
13670c195119SAlex Deucher  * Called at driver shutdown.
13680c195119SAlex Deucher  */
1369771fe6b9SJerome Glisse void radeon_device_fini(struct radeon_device *rdev)
1370771fe6b9SJerome Glisse {
1371771fe6b9SJerome Glisse 	DRM_INFO("radeon: finishing device.\n");
1372771fe6b9SJerome Glisse 	rdev->shutdown = true;
137390aca4d2SJerome Glisse 	/* evict vram memory */
137490aca4d2SJerome Glisse 	radeon_bo_evict_vram(rdev);
13753ce0a23dSJerome Glisse 	radeon_fini(rdev);
13766a9ee8afSDave Airlie 	vga_switcheroo_unregister_client(rdev->pdev);
1377c1176d6fSDave Airlie 	vga_client_register(rdev->pdev, NULL, NULL, NULL);
1378e0a2ca73SAlex Deucher 	if (rdev->rio_mem)
1379351a52a2SAlex Deucher 		pci_iounmap(rdev->pdev, rdev->rio_mem);
1380351a52a2SAlex Deucher 	rdev->rio_mem = NULL;
1381771fe6b9SJerome Glisse 	iounmap(rdev->rmmio);
1382771fe6b9SJerome Glisse 	rdev->rmmio = NULL;
138375efdee1SAlex Deucher 	if (rdev->family >= CHIP_BONAIRE)
138475efdee1SAlex Deucher 		radeon_doorbell_fini(rdev);
13854d8bf9aeSChristian König 	radeon_debugfs_remove_files(rdev);
1386771fe6b9SJerome Glisse }
1387771fe6b9SJerome Glisse 
1388771fe6b9SJerome Glisse 
1389771fe6b9SJerome Glisse /*
1390771fe6b9SJerome Glisse  * Suspend & resume.
1391771fe6b9SJerome Glisse  */
13920c195119SAlex Deucher /**
13930c195119SAlex Deucher  * radeon_suspend_kms - initiate device suspend
13940c195119SAlex Deucher  *
13950c195119SAlex Deucher  * @pdev: drm dev pointer
13960c195119SAlex Deucher  * @state: suspend state
13970c195119SAlex Deucher  *
13980c195119SAlex Deucher  * Puts the hw in the suspend state (all asics).
13990c195119SAlex Deucher  * Returns 0 for success or an error on failure.
14000c195119SAlex Deucher  * Called at driver suspend.
14010c195119SAlex Deucher  */
140210ebc0bcSDave Airlie int radeon_suspend_kms(struct drm_device *dev, bool suspend, bool fbcon)
1403771fe6b9SJerome Glisse {
1404875c1866SDarren Jenkins 	struct radeon_device *rdev;
1405771fe6b9SJerome Glisse 	struct drm_crtc *crtc;
1406d8dcaa1dSAlex Deucher 	struct drm_connector *connector;
14077465280cSAlex Deucher 	int i, r;
14085f8f635eSJerome Glisse 	bool force_completion = false;
1409771fe6b9SJerome Glisse 
1410875c1866SDarren Jenkins 	if (dev == NULL || dev->dev_private == NULL) {
1411771fe6b9SJerome Glisse 		return -ENODEV;
1412771fe6b9SJerome Glisse 	}
14137473e830SDave Airlie 
1414875c1866SDarren Jenkins 	rdev = dev->dev_private;
1415875c1866SDarren Jenkins 
14165bcf719bSDave Airlie 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
14176a9ee8afSDave Airlie 		return 0;
1418d8dcaa1dSAlex Deucher 
141986698c20SSeth Forshee 	drm_kms_helper_poll_disable(dev);
142086698c20SSeth Forshee 
1421d8dcaa1dSAlex Deucher 	/* turn off display hw */
1422d8dcaa1dSAlex Deucher 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1423d8dcaa1dSAlex Deucher 		drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1424d8dcaa1dSAlex Deucher 	}
1425d8dcaa1dSAlex Deucher 
1426771fe6b9SJerome Glisse 	/* unpin the front buffers */
1427771fe6b9SJerome Glisse 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1428f4510a27SMatt Roper 		struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->primary->fb);
14294c788679SJerome Glisse 		struct radeon_bo *robj;
1430771fe6b9SJerome Glisse 
1431771fe6b9SJerome Glisse 		if (rfb == NULL || rfb->obj == NULL) {
1432771fe6b9SJerome Glisse 			continue;
1433771fe6b9SJerome Glisse 		}
14347e4d15d9SDaniel Vetter 		robj = gem_to_radeon_bo(rfb->obj);
143538651674SDave Airlie 		/* don't unpin kernel fb objects */
143638651674SDave Airlie 		if (!radeon_fbdev_robj_is_fb(rdev, robj)) {
14374c788679SJerome Glisse 			r = radeon_bo_reserve(robj, false);
143838651674SDave Airlie 			if (r == 0) {
14394c788679SJerome Glisse 				radeon_bo_unpin(robj);
14404c788679SJerome Glisse 				radeon_bo_unreserve(robj);
14414c788679SJerome Glisse 			}
1442771fe6b9SJerome Glisse 		}
1443771fe6b9SJerome Glisse 	}
1444771fe6b9SJerome Glisse 	/* evict vram memory */
14454c788679SJerome Glisse 	radeon_bo_evict_vram(rdev);
14468a47cc9eSChristian König 
1447771fe6b9SJerome Glisse 	/* wait for gpu to finish processing current batch */
14485f8f635eSJerome Glisse 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
144937615527SChristian König 		r = radeon_fence_wait_empty(rdev, i);
14505f8f635eSJerome Glisse 		if (r) {
14515f8f635eSJerome Glisse 			/* delay GPU reset to resume */
14525f8f635eSJerome Glisse 			force_completion = true;
14535f8f635eSJerome Glisse 		}
14545f8f635eSJerome Glisse 	}
14555f8f635eSJerome Glisse 	if (force_completion) {
14565f8f635eSJerome Glisse 		radeon_fence_driver_force_completion(rdev);
14575f8f635eSJerome Glisse 	}
1458771fe6b9SJerome Glisse 
1459f657c2a7SYang Zhao 	radeon_save_bios_scratch_regs(rdev);
1460f657c2a7SYang Zhao 
14613ce0a23dSJerome Glisse 	radeon_suspend(rdev);
1462d4877cf2SAlex Deucher 	radeon_hpd_fini(rdev);
1463771fe6b9SJerome Glisse 	/* evict remaining vram memory */
14644c788679SJerome Glisse 	radeon_bo_evict_vram(rdev);
1465771fe6b9SJerome Glisse 
146610b06122SJerome Glisse 	radeon_agp_suspend(rdev);
146710b06122SJerome Glisse 
1468771fe6b9SJerome Glisse 	pci_save_state(dev->pdev);
14697473e830SDave Airlie 	if (suspend) {
1470771fe6b9SJerome Glisse 		/* Shut down the device */
1471771fe6b9SJerome Glisse 		pci_disable_device(dev->pdev);
1472771fe6b9SJerome Glisse 		pci_set_power_state(dev->pdev, PCI_D3hot);
1473771fe6b9SJerome Glisse 	}
147410ebc0bcSDave Airlie 
147510ebc0bcSDave Airlie 	if (fbcon) {
1476ac751efaSTorben Hohn 		console_lock();
147738651674SDave Airlie 		radeon_fbdev_set_suspend(rdev, 1);
1478ac751efaSTorben Hohn 		console_unlock();
147910ebc0bcSDave Airlie 	}
1480771fe6b9SJerome Glisse 	return 0;
1481771fe6b9SJerome Glisse }
1482771fe6b9SJerome Glisse 
14830c195119SAlex Deucher /**
14840c195119SAlex Deucher  * radeon_resume_kms - initiate device resume
14850c195119SAlex Deucher  *
14860c195119SAlex Deucher  * @pdev: drm dev pointer
14870c195119SAlex Deucher  *
14880c195119SAlex Deucher  * Bring the hw back to operating state (all asics).
14890c195119SAlex Deucher  * Returns 0 for success or an error on failure.
14900c195119SAlex Deucher  * Called at driver resume.
14910c195119SAlex Deucher  */
149210ebc0bcSDave Airlie int radeon_resume_kms(struct drm_device *dev, bool resume, bool fbcon)
1493771fe6b9SJerome Glisse {
149409bdf591SCedric Godin 	struct drm_connector *connector;
1495771fe6b9SJerome Glisse 	struct radeon_device *rdev = dev->dev_private;
149604eb2206SChristian König 	int r;
1497771fe6b9SJerome Glisse 
14985bcf719bSDave Airlie 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
14996a9ee8afSDave Airlie 		return 0;
15006a9ee8afSDave Airlie 
150110ebc0bcSDave Airlie 	if (fbcon) {
1502ac751efaSTorben Hohn 		console_lock();
150310ebc0bcSDave Airlie 	}
15047473e830SDave Airlie 	if (resume) {
1505771fe6b9SJerome Glisse 		pci_set_power_state(dev->pdev, PCI_D0);
1506771fe6b9SJerome Glisse 		pci_restore_state(dev->pdev);
1507771fe6b9SJerome Glisse 		if (pci_enable_device(dev->pdev)) {
150810ebc0bcSDave Airlie 			if (fbcon)
1509ac751efaSTorben Hohn 				console_unlock();
1510771fe6b9SJerome Glisse 			return -1;
1511771fe6b9SJerome Glisse 		}
15127473e830SDave Airlie 	}
15130ebf1717SDave Airlie 	/* resume AGP if in use */
15140ebf1717SDave Airlie 	radeon_agp_resume(rdev);
15153ce0a23dSJerome Glisse 	radeon_resume(rdev);
151604eb2206SChristian König 
151704eb2206SChristian König 	r = radeon_ib_ring_tests(rdev);
151804eb2206SChristian König 	if (r)
151904eb2206SChristian König 		DRM_ERROR("ib ring test failed (%d).\n", r);
152004eb2206SChristian König 
1521bc6a6295SAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
15226c7bcceaSAlex Deucher 		/* do dpm late init */
15236c7bcceaSAlex Deucher 		r = radeon_pm_late_init(rdev);
15246c7bcceaSAlex Deucher 		if (r) {
15256c7bcceaSAlex Deucher 			rdev->pm.dpm_enabled = false;
15266c7bcceaSAlex Deucher 			DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
15276c7bcceaSAlex Deucher 		}
1528bc6a6295SAlex Deucher 	} else {
1529bc6a6295SAlex Deucher 		/* resume old pm late */
1530bc6a6295SAlex Deucher 		radeon_pm_resume(rdev);
15316c7bcceaSAlex Deucher 	}
15326c7bcceaSAlex Deucher 
1533f657c2a7SYang Zhao 	radeon_restore_bios_scratch_regs(rdev);
153409bdf591SCedric Godin 
153510ebc0bcSDave Airlie 	if (fbcon) {
153638651674SDave Airlie 		radeon_fbdev_set_suspend(rdev, 0);
1537ac751efaSTorben Hohn 		console_unlock();
153810ebc0bcSDave Airlie 	}
1539771fe6b9SJerome Glisse 
15403fa47d9eSAlex Deucher 	/* init dig PHYs, disp eng pll */
15413fa47d9eSAlex Deucher 	if (rdev->is_atom_bios) {
1542ac89af1eSAlex Deucher 		radeon_atom_encoder_init(rdev);
1543f3f1f03eSAlex Deucher 		radeon_atom_disp_eng_pll_init(rdev);
1544bced76f2SAlex Deucher 		/* turn on the BL */
1545bced76f2SAlex Deucher 		if (rdev->mode_info.bl_encoder) {
1546bced76f2SAlex Deucher 			u8 bl_level = radeon_get_backlight_level(rdev,
1547bced76f2SAlex Deucher 								 rdev->mode_info.bl_encoder);
1548bced76f2SAlex Deucher 			radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
1549bced76f2SAlex Deucher 						   bl_level);
1550bced76f2SAlex Deucher 		}
15513fa47d9eSAlex Deucher 	}
1552d4877cf2SAlex Deucher 	/* reset hpd state */
1553d4877cf2SAlex Deucher 	radeon_hpd_init(rdev);
1554771fe6b9SJerome Glisse 	/* blat the mode back in */
1555ec9954fcSDave Airlie 	if (fbcon) {
1556771fe6b9SJerome Glisse 		drm_helper_resume_force_mode(dev);
1557a93f344dSAlex Deucher 		/* turn on display hw */
1558a93f344dSAlex Deucher 		list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1559a93f344dSAlex Deucher 			drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
1560a93f344dSAlex Deucher 		}
1561ec9954fcSDave Airlie 	}
156286698c20SSeth Forshee 
156386698c20SSeth Forshee 	drm_kms_helper_poll_enable(dev);
1564771fe6b9SJerome Glisse 	return 0;
1565771fe6b9SJerome Glisse }
1566771fe6b9SJerome Glisse 
15670c195119SAlex Deucher /**
15680c195119SAlex Deucher  * radeon_gpu_reset - reset the asic
15690c195119SAlex Deucher  *
15700c195119SAlex Deucher  * @rdev: radeon device pointer
15710c195119SAlex Deucher  *
15720c195119SAlex Deucher  * Attempt the reset the GPU if it has hung (all asics).
15730c195119SAlex Deucher  * Returns 0 for success or an error on failure.
15740c195119SAlex Deucher  */
157590aca4d2SJerome Glisse int radeon_gpu_reset(struct radeon_device *rdev)
157690aca4d2SJerome Glisse {
157755d7c221SChristian König 	unsigned ring_sizes[RADEON_NUM_RINGS];
157855d7c221SChristian König 	uint32_t *ring_data[RADEON_NUM_RINGS];
157955d7c221SChristian König 
158055d7c221SChristian König 	bool saved = false;
158155d7c221SChristian König 
158255d7c221SChristian König 	int i, r;
15838fd1b84cSDave Airlie 	int resched;
158490aca4d2SJerome Glisse 
1585dee53e7fSJerome Glisse 	down_write(&rdev->exclusive_lock);
1586f9eaf9aeSChristian König 
1587f9eaf9aeSChristian König 	if (!rdev->needs_reset) {
1588f9eaf9aeSChristian König 		up_write(&rdev->exclusive_lock);
1589f9eaf9aeSChristian König 		return 0;
1590f9eaf9aeSChristian König 	}
1591f9eaf9aeSChristian König 
1592f9eaf9aeSChristian König 	rdev->needs_reset = false;
1593f9eaf9aeSChristian König 
159490aca4d2SJerome Glisse 	radeon_save_bios_scratch_regs(rdev);
15958fd1b84cSDave Airlie 	/* block TTM */
15968fd1b84cSDave Airlie 	resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
159795f59509SAlex Deucher 	radeon_pm_suspend(rdev);
159890aca4d2SJerome Glisse 	radeon_suspend(rdev);
159990aca4d2SJerome Glisse 
160055d7c221SChristian König 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
160155d7c221SChristian König 		ring_sizes[i] = radeon_ring_backup(rdev, &rdev->ring[i],
160255d7c221SChristian König 						   &ring_data[i]);
160355d7c221SChristian König 		if (ring_sizes[i]) {
160455d7c221SChristian König 			saved = true;
160555d7c221SChristian König 			dev_info(rdev->dev, "Saved %d dwords of commands "
160655d7c221SChristian König 				 "on ring %d.\n", ring_sizes[i], i);
160755d7c221SChristian König 		}
160855d7c221SChristian König 	}
160955d7c221SChristian König 
161055d7c221SChristian König retry:
161190aca4d2SJerome Glisse 	r = radeon_asic_reset(rdev);
161290aca4d2SJerome Glisse 	if (!r) {
161355d7c221SChristian König 		dev_info(rdev->dev, "GPU reset succeeded, trying to resume\n");
161490aca4d2SJerome Glisse 		radeon_resume(rdev);
161555d7c221SChristian König 	}
161604eb2206SChristian König 
161790aca4d2SJerome Glisse 	radeon_restore_bios_scratch_regs(rdev);
161855d7c221SChristian König 
161955d7c221SChristian König 	if (!r) {
162055d7c221SChristian König 		for (i = 0; i < RADEON_NUM_RINGS; ++i) {
162155d7c221SChristian König 			radeon_ring_restore(rdev, &rdev->ring[i],
162255d7c221SChristian König 					    ring_sizes[i], ring_data[i]);
1623f54b350dSChristian König 			ring_sizes[i] = 0;
1624f54b350dSChristian König 			ring_data[i] = NULL;
162590aca4d2SJerome Glisse 		}
16267a1619b9SMichel Dänzer 
162755d7c221SChristian König 		r = radeon_ib_ring_tests(rdev);
162855d7c221SChristian König 		if (r) {
162955d7c221SChristian König 			dev_err(rdev->dev, "ib ring test failed (%d).\n", r);
163055d7c221SChristian König 			if (saved) {
1631f54b350dSChristian König 				saved = false;
163255d7c221SChristian König 				radeon_suspend(rdev);
163355d7c221SChristian König 				goto retry;
163455d7c221SChristian König 			}
163555d7c221SChristian König 		}
163655d7c221SChristian König 	} else {
163776903b96SJerome Glisse 		radeon_fence_driver_force_completion(rdev);
163855d7c221SChristian König 		for (i = 0; i < RADEON_NUM_RINGS; ++i) {
163955d7c221SChristian König 			kfree(ring_data[i]);
164055d7c221SChristian König 		}
164155d7c221SChristian König 	}
164255d7c221SChristian König 
164395f59509SAlex Deucher 	radeon_pm_resume(rdev);
1644d3493574SJerome Glisse 	drm_helper_resume_force_mode(rdev->ddev);
1645d3493574SJerome Glisse 
164655d7c221SChristian König 	ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
16477a1619b9SMichel Dänzer 	if (r) {
164890aca4d2SJerome Glisse 		/* bad news, how to tell it to userspace ? */
164990aca4d2SJerome Glisse 		dev_info(rdev->dev, "GPU reset failed\n");
16507a1619b9SMichel Dänzer 	}
16517a1619b9SMichel Dänzer 
1652dee53e7fSJerome Glisse 	up_write(&rdev->exclusive_lock);
165390aca4d2SJerome Glisse 	return r;
165490aca4d2SJerome Glisse }
165590aca4d2SJerome Glisse 
1656771fe6b9SJerome Glisse 
1657771fe6b9SJerome Glisse /*
1658771fe6b9SJerome Glisse  * Debugfs
1659771fe6b9SJerome Glisse  */
1660771fe6b9SJerome Glisse int radeon_debugfs_add_files(struct radeon_device *rdev,
1661771fe6b9SJerome Glisse 			     struct drm_info_list *files,
1662771fe6b9SJerome Glisse 			     unsigned nfiles)
1663771fe6b9SJerome Glisse {
1664771fe6b9SJerome Glisse 	unsigned i;
1665771fe6b9SJerome Glisse 
16664d8bf9aeSChristian König 	for (i = 0; i < rdev->debugfs_count; i++) {
16674d8bf9aeSChristian König 		if (rdev->debugfs[i].files == files) {
1668771fe6b9SJerome Glisse 			/* Already registered */
1669771fe6b9SJerome Glisse 			return 0;
1670771fe6b9SJerome Glisse 		}
1671771fe6b9SJerome Glisse 	}
1672c245cb9eSMichael Witten 
16734d8bf9aeSChristian König 	i = rdev->debugfs_count + 1;
1674c245cb9eSMichael Witten 	if (i > RADEON_DEBUGFS_MAX_COMPONENTS) {
1675c245cb9eSMichael Witten 		DRM_ERROR("Reached maximum number of debugfs components.\n");
1676c245cb9eSMichael Witten 		DRM_ERROR("Report so we increase "
1677c245cb9eSMichael Witten 		          "RADEON_DEBUGFS_MAX_COMPONENTS.\n");
1678771fe6b9SJerome Glisse 		return -EINVAL;
1679771fe6b9SJerome Glisse 	}
16804d8bf9aeSChristian König 	rdev->debugfs[rdev->debugfs_count].files = files;
16814d8bf9aeSChristian König 	rdev->debugfs[rdev->debugfs_count].num_files = nfiles;
16824d8bf9aeSChristian König 	rdev->debugfs_count = i;
1683771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS)
1684771fe6b9SJerome Glisse 	drm_debugfs_create_files(files, nfiles,
1685771fe6b9SJerome Glisse 				 rdev->ddev->control->debugfs_root,
1686771fe6b9SJerome Glisse 				 rdev->ddev->control);
1687771fe6b9SJerome Glisse 	drm_debugfs_create_files(files, nfiles,
1688771fe6b9SJerome Glisse 				 rdev->ddev->primary->debugfs_root,
1689771fe6b9SJerome Glisse 				 rdev->ddev->primary);
1690771fe6b9SJerome Glisse #endif
1691771fe6b9SJerome Glisse 	return 0;
1692771fe6b9SJerome Glisse }
1693771fe6b9SJerome Glisse 
16944d8bf9aeSChristian König static void radeon_debugfs_remove_files(struct radeon_device *rdev)
16954d8bf9aeSChristian König {
16964d8bf9aeSChristian König #if defined(CONFIG_DEBUG_FS)
16974d8bf9aeSChristian König 	unsigned i;
16984d8bf9aeSChristian König 
16994d8bf9aeSChristian König 	for (i = 0; i < rdev->debugfs_count; i++) {
17004d8bf9aeSChristian König 		drm_debugfs_remove_files(rdev->debugfs[i].files,
17014d8bf9aeSChristian König 					 rdev->debugfs[i].num_files,
17024d8bf9aeSChristian König 					 rdev->ddev->control);
17034d8bf9aeSChristian König 		drm_debugfs_remove_files(rdev->debugfs[i].files,
17044d8bf9aeSChristian König 					 rdev->debugfs[i].num_files,
17054d8bf9aeSChristian König 					 rdev->ddev->primary);
17064d8bf9aeSChristian König 	}
17074d8bf9aeSChristian König #endif
17084d8bf9aeSChristian König }
17094d8bf9aeSChristian König 
1710771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS)
1711771fe6b9SJerome Glisse int radeon_debugfs_init(struct drm_minor *minor)
1712771fe6b9SJerome Glisse {
1713771fe6b9SJerome Glisse 	return 0;
1714771fe6b9SJerome Glisse }
1715771fe6b9SJerome Glisse 
1716771fe6b9SJerome Glisse void radeon_debugfs_cleanup(struct drm_minor *minor)
1717771fe6b9SJerome Glisse {
1718771fe6b9SJerome Glisse }
1719771fe6b9SJerome Glisse #endif
1720