Home | History | Annotate | Line # | Download | only in radeon
radeon_device.c revision 1.1.1.2
      1 /*	$NetBSD: radeon_device.c,v 1.1.1.2 2018/08/27 01:34:58 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright 2008 Advanced Micro Devices, Inc.
      5  * Copyright 2008 Red Hat Inc.
      6  * Copyright 2009 Jerome Glisse.
      7  *
      8  * Permission is hereby granted, free of charge, to any person obtaining a
      9  * copy of this software and associated documentation files (the "Software"),
     10  * to deal in the Software without restriction, including without limitation
     11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     12  * and/or sell copies of the Software, and to permit persons to whom the
     13  * Software is furnished to do so, subject to the following conditions:
     14  *
     15  * The above copyright notice and this permission notice shall be included in
     16  * all copies or substantial portions of the Software.
     17  *
     18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     21  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     24  * OTHER DEALINGS IN THE SOFTWARE.
     25  *
     26  * Authors: Dave Airlie
     27  *          Alex Deucher
     28  *          Jerome Glisse
     29  */
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: radeon_device.c,v 1.1.1.2 2018/08/27 01:34:58 riastradh Exp $");
     32 
     33 #include <linux/console.h>
     34 #include <linux/slab.h>
     35 #include <drm/drmP.h>
     36 #include <drm/drm_crtc_helper.h>
     37 #include <drm/radeon_drm.h>
     38 #include <linux/vgaarb.h>
     39 #include <linux/vga_switcheroo.h>
     40 #include <linux/efi.h>
     41 #include "radeon_reg.h"
     42 #include "radeon.h"
     43 #include "atom.h"
     44 
     45 static const char radeon_family_name[][16] = {
     46 	"R100",
     47 	"RV100",
     48 	"RS100",
     49 	"RV200",
     50 	"RS200",
     51 	"R200",
     52 	"RV250",
     53 	"RS300",
     54 	"RV280",
     55 	"R300",
     56 	"R350",
     57 	"RV350",
     58 	"RV380",
     59 	"R420",
     60 	"R423",
     61 	"RV410",
     62 	"RS400",
     63 	"RS480",
     64 	"RS600",
     65 	"RS690",
     66 	"RS740",
     67 	"RV515",
     68 	"R520",
     69 	"RV530",
     70 	"RV560",
     71 	"RV570",
     72 	"R580",
     73 	"R600",
     74 	"RV610",
     75 	"RV630",
     76 	"RV670",
     77 	"RV620",
     78 	"RV635",
     79 	"RS780",
     80 	"RS880",
     81 	"RV770",
     82 	"RV730",
     83 	"RV710",
     84 	"RV740",
     85 	"CEDAR",
     86 	"REDWOOD",
     87 	"JUNIPER",
     88 	"CYPRESS",
     89 	"HEMLOCK",
     90 	"PALM",
     91 	"SUMO",
     92 	"SUMO2",
     93 	"BARTS",
     94 	"TURKS",
     95 	"CAICOS",
     96 	"CAYMAN",
     97 	"ARUBA",
     98 	"TAHITI",
     99 	"PITCAIRN",
    100 	"VERDE",
    101 	"OLAND",
    102 	"HAINAN",
    103 	"BONAIRE",
    104 	"KAVERI",
    105 	"KABINI",
    106 	"HAWAII",
    107 	"MULLINS",
    108 	"LAST",
    109 };
    110 
    111 #define RADEON_PX_QUIRK_DISABLE_PX  (1 << 0)
    112 #define RADEON_PX_QUIRK_LONG_WAKEUP (1 << 1)
    113 
    114 struct radeon_px_quirk {
    115 	u32 chip_vendor;
    116 	u32 chip_device;
    117 	u32 subsys_vendor;
    118 	u32 subsys_device;
    119 	u32 px_quirk_flags;
    120 };
    121 
    122 static struct radeon_px_quirk radeon_px_quirk_list[] = {
    123 	/* Acer aspire 5560g (CPU: AMD A4-3305M; GPU: AMD Radeon HD 6480g + 7470m)
    124 	 * https://bugzilla.kernel.org/show_bug.cgi?id=74551
    125 	 */
    126 	{ PCI_VENDOR_ID_ATI, 0x6760, 0x1025, 0x0672, RADEON_PX_QUIRK_DISABLE_PX },
    127 	/* Asus K73TA laptop with AMD A6-3400M APU and Radeon 6550 GPU
    128 	 * https://bugzilla.kernel.org/show_bug.cgi?id=51381
    129 	 */
    130 	{ PCI_VENDOR_ID_ATI, 0x6741, 0x1043, 0x108c, RADEON_PX_QUIRK_DISABLE_PX },
    131 	/* Asus K53TK laptop with AMD A6-3420M APU and Radeon 7670m GPU
    132 	 * https://bugzilla.kernel.org/show_bug.cgi?id=51381
    133 	 */
    134 	{ PCI_VENDOR_ID_ATI, 0x6840, 0x1043, 0x2122, RADEON_PX_QUIRK_DISABLE_PX },
    135 	/* Asus K53TK laptop with AMD A6-3420M APU and Radeon 7670m GPU
    136 	 * https://bugs.freedesktop.org/show_bug.cgi?id=101491
    137 	 */
    138 	{ PCI_VENDOR_ID_ATI, 0x6741, 0x1043, 0x2122, RADEON_PX_QUIRK_DISABLE_PX },
    139 	/* macbook pro 8.2 */
    140 	{ PCI_VENDOR_ID_ATI, 0x6741, PCI_VENDOR_ID_APPLE, 0x00e2, RADEON_PX_QUIRK_LONG_WAKEUP },
    141 	{ 0, 0, 0, 0, 0 },
    142 };
    143 
    144 bool radeon_is_px(struct drm_device *dev)
    145 {
    146 	struct radeon_device *rdev = dev->dev_private;
    147 
    148 	if (rdev->flags & RADEON_IS_PX)
    149 		return true;
    150 	return false;
    151 }
    152 
    153 static void radeon_device_handle_px_quirks(struct radeon_device *rdev)
    154 {
    155 	struct radeon_px_quirk *p = radeon_px_quirk_list;
    156 
    157 	/* Apply PX quirks */
    158 	while (p && p->chip_device != 0) {
    159 		if (rdev->pdev->vendor == p->chip_vendor &&
    160 		    rdev->pdev->device == p->chip_device &&
    161 		    rdev->pdev->subsystem_vendor == p->subsys_vendor &&
    162 		    rdev->pdev->subsystem_device == p->subsys_device) {
    163 			rdev->px_quirk_flags = p->px_quirk_flags;
    164 			break;
    165 		}
    166 		++p;
    167 	}
    168 
    169 	if (rdev->px_quirk_flags & RADEON_PX_QUIRK_DISABLE_PX)
    170 		rdev->flags &= ~RADEON_IS_PX;
    171 }
    172 
    173 /**
    174  * radeon_program_register_sequence - program an array of registers.
    175  *
    176  * @rdev: radeon_device pointer
    177  * @registers: pointer to the register array
    178  * @array_size: size of the register array
    179  *
    180  * Programs an array or registers with and and or masks.
    181  * This is a helper for setting golden registers.
    182  */
    183 void radeon_program_register_sequence(struct radeon_device *rdev,
    184 				      const u32 *registers,
    185 				      const u32 array_size)
    186 {
    187 	u32 tmp, reg, and_mask, or_mask;
    188 	int i;
    189 
    190 	if (array_size % 3)
    191 		return;
    192 
    193 	for (i = 0; i < array_size; i +=3) {
    194 		reg = registers[i + 0];
    195 		and_mask = registers[i + 1];
    196 		or_mask = registers[i + 2];
    197 
    198 		if (and_mask == 0xffffffff) {
    199 			tmp = or_mask;
    200 		} else {
    201 			tmp = RREG32(reg);
    202 			tmp &= ~and_mask;
    203 			tmp |= or_mask;
    204 		}
    205 		WREG32(reg, tmp);
    206 	}
    207 }
    208 
    209 void radeon_pci_config_reset(struct radeon_device *rdev)
    210 {
    211 	pci_write_config_dword(rdev->pdev, 0x7c, RADEON_ASIC_RESET_DATA);
    212 }
    213 
    214 /**
    215  * radeon_surface_init - Clear GPU surface registers.
    216  *
    217  * @rdev: radeon_device pointer
    218  *
    219  * Clear GPU surface registers (r1xx-r5xx).
    220  */
    221 void radeon_surface_init(struct radeon_device *rdev)
    222 {
    223 	/* FIXME: check this out */
    224 	if (rdev->family < CHIP_R600) {
    225 		int i;
    226 
    227 		for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
    228 			if (rdev->surface_regs[i].bo)
    229 				radeon_bo_get_surface_reg(rdev->surface_regs[i].bo);
    230 			else
    231 				radeon_clear_surface_reg(rdev, i);
    232 		}
    233 		/* enable surfaces */
    234 		WREG32(RADEON_SURFACE_CNTL, 0);
    235 	}
    236 }
    237 
    238 /*
    239  * GPU scratch registers helpers function.
    240  */
    241 /**
    242  * radeon_scratch_init - Init scratch register driver information.
    243  *
    244  * @rdev: radeon_device pointer
    245  *
    246  * Init CP scratch register driver information (r1xx-r5xx)
    247  */
    248 void radeon_scratch_init(struct radeon_device *rdev)
    249 {
    250 	int i;
    251 
    252 	/* FIXME: check this out */
    253 	if (rdev->family < CHIP_R300) {
    254 		rdev->scratch.num_reg = 5;
    255 	} else {
    256 		rdev->scratch.num_reg = 7;
    257 	}
    258 	rdev->scratch.reg_base = RADEON_SCRATCH_REG0;
    259 	for (i = 0; i < rdev->scratch.num_reg; i++) {
    260 		rdev->scratch.free[i] = true;
    261 		rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4);
    262 	}
    263 }
    264 
    265 /**
    266  * radeon_scratch_get - Allocate a scratch register
    267  *
    268  * @rdev: radeon_device pointer
    269  * @reg: scratch register mmio offset
    270  *
    271  * Allocate a CP scratch register for use by the driver (all asics).
    272  * Returns 0 on success or -EINVAL on failure.
    273  */
    274 int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg)
    275 {
    276 	int i;
    277 
    278 	for (i = 0; i < rdev->scratch.num_reg; i++) {
    279 		if (rdev->scratch.free[i]) {
    280 			rdev->scratch.free[i] = false;
    281 			*reg = rdev->scratch.reg[i];
    282 			return 0;
    283 		}
    284 	}
    285 	return -EINVAL;
    286 }
    287 
    288 /**
    289  * radeon_scratch_free - Free a scratch register
    290  *
    291  * @rdev: radeon_device pointer
    292  * @reg: scratch register mmio offset
    293  *
    294  * Free a CP scratch register allocated for use by the driver (all asics)
    295  */
    296 void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg)
    297 {
    298 	int i;
    299 
    300 	for (i = 0; i < rdev->scratch.num_reg; i++) {
    301 		if (rdev->scratch.reg[i] == reg) {
    302 			rdev->scratch.free[i] = true;
    303 			return;
    304 		}
    305 	}
    306 }
    307 
    308 /*
    309  * GPU doorbell aperture helpers function.
    310  */
    311 /**
    312  * radeon_doorbell_init - Init doorbell driver information.
    313  *
    314  * @rdev: radeon_device pointer
    315  *
    316  * Init doorbell driver information (CIK)
    317  * Returns 0 on success, error on failure.
    318  */
    319 static int radeon_doorbell_init(struct radeon_device *rdev)
    320 {
    321 	/* doorbell bar mapping */
    322 	rdev->doorbell.base = pci_resource_start(rdev->pdev, 2);
    323 	rdev->doorbell.size = pci_resource_len(rdev->pdev, 2);
    324 
    325 	rdev->doorbell.num_doorbells = min_t(u32, rdev->doorbell.size / sizeof(u32), RADEON_MAX_DOORBELLS);
    326 	if (rdev->doorbell.num_doorbells == 0)
    327 		return -EINVAL;
    328 
    329 	rdev->doorbell.ptr = ioremap(rdev->doorbell.base, rdev->doorbell.num_doorbells * sizeof(u32));
    330 	if (rdev->doorbell.ptr == NULL) {
    331 		return -ENOMEM;
    332 	}
    333 	DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)rdev->doorbell.base);
    334 	DRM_INFO("doorbell mmio size: %u\n", (unsigned)rdev->doorbell.size);
    335 
    336 	memset(&rdev->doorbell.used, 0, sizeof(rdev->doorbell.used));
    337 
    338 	return 0;
    339 }
    340 
    341 /**
    342  * radeon_doorbell_fini - Tear down doorbell driver information.
    343  *
    344  * @rdev: radeon_device pointer
    345  *
    346  * Tear down doorbell driver information (CIK)
    347  */
    348 static void radeon_doorbell_fini(struct radeon_device *rdev)
    349 {
    350 	iounmap(rdev->doorbell.ptr);
    351 	rdev->doorbell.ptr = NULL;
    352 }
    353 
    354 /**
    355  * radeon_doorbell_get - Allocate a doorbell entry
    356  *
    357  * @rdev: radeon_device pointer
    358  * @doorbell: doorbell index
    359  *
    360  * Allocate a doorbell for use by the driver (all asics).
    361  * Returns 0 on success or -EINVAL on failure.
    362  */
    363 int radeon_doorbell_get(struct radeon_device *rdev, u32 *doorbell)
    364 {
    365 	unsigned long offset = find_first_zero_bit(rdev->doorbell.used, rdev->doorbell.num_doorbells);
    366 	if (offset < rdev->doorbell.num_doorbells) {
    367 		__set_bit(offset, rdev->doorbell.used);
    368 		*doorbell = offset;
    369 		return 0;
    370 	} else {
    371 		return -EINVAL;
    372 	}
    373 }
    374 
    375 /**
    376  * radeon_doorbell_free - Free a doorbell entry
    377  *
    378  * @rdev: radeon_device pointer
    379  * @doorbell: doorbell index
    380  *
    381  * Free a doorbell allocated for use by the driver (all asics)
    382  */
    383 void radeon_doorbell_free(struct radeon_device *rdev, u32 doorbell)
    384 {
    385 	if (doorbell < rdev->doorbell.num_doorbells)
    386 		__clear_bit(doorbell, rdev->doorbell.used);
    387 }
    388 
    389 /**
    390  * radeon_doorbell_get_kfd_info - Report doorbell configuration required to
    391  *                                setup KFD
    392  *
    393  * @rdev: radeon_device pointer
    394  * @aperture_base: output returning doorbell aperture base physical address
    395  * @aperture_size: output returning doorbell aperture size in bytes
    396  * @start_offset: output returning # of doorbell bytes reserved for radeon.
    397  *
    398  * Radeon and the KFD share the doorbell aperture. Radeon sets it up,
    399  * takes doorbells required for its own rings and reports the setup to KFD.
    400  * Radeon reserved doorbells are at the start of the doorbell aperture.
    401  */
    402 void radeon_doorbell_get_kfd_info(struct radeon_device *rdev,
    403 				  phys_addr_t *aperture_base,
    404 				  size_t *aperture_size,
    405 				  size_t *start_offset)
    406 {
    407 	/* The first num_doorbells are used by radeon.
    408 	 * KFD takes whatever's left in the aperture. */
    409 	if (rdev->doorbell.size > rdev->doorbell.num_doorbells * sizeof(u32)) {
    410 		*aperture_base = rdev->doorbell.base;
    411 		*aperture_size = rdev->doorbell.size;
    412 		*start_offset = rdev->doorbell.num_doorbells * sizeof(u32);
    413 	} else {
    414 		*aperture_base = 0;
    415 		*aperture_size = 0;
    416 		*start_offset = 0;
    417 	}
    418 }
    419 
    420 /*
    421  * radeon_wb_*()
    422  * Writeback is the the method by which the the GPU updates special pages
    423  * in memory with the status of certain GPU events (fences, ring pointers,
    424  * etc.).
    425  */
    426 
    427 /**
    428  * radeon_wb_disable - Disable Writeback
    429  *
    430  * @rdev: radeon_device pointer
    431  *
    432  * Disables Writeback (all asics).  Used for suspend.
    433  */
    434 void radeon_wb_disable(struct radeon_device *rdev)
    435 {
    436 	rdev->wb.enabled = false;
    437 }
    438 
    439 /**
    440  * radeon_wb_fini - Disable Writeback and free memory
    441  *
    442  * @rdev: radeon_device pointer
    443  *
    444  * Disables Writeback and frees the Writeback memory (all asics).
    445  * Used at driver shutdown.
    446  */
    447 void radeon_wb_fini(struct radeon_device *rdev)
    448 {
    449 	radeon_wb_disable(rdev);
    450 	if (rdev->wb.wb_obj) {
    451 		if (!radeon_bo_reserve(rdev->wb.wb_obj, false)) {
    452 			radeon_bo_kunmap(rdev->wb.wb_obj);
    453 			radeon_bo_unpin(rdev->wb.wb_obj);
    454 			radeon_bo_unreserve(rdev->wb.wb_obj);
    455 		}
    456 		radeon_bo_unref(&rdev->wb.wb_obj);
    457 		rdev->wb.wb = NULL;
    458 		rdev->wb.wb_obj = NULL;
    459 	}
    460 }
    461 
    462 /**
    463  * radeon_wb_init- Init Writeback driver info and allocate memory
    464  *
    465  * @rdev: radeon_device pointer
    466  *
    467  * Disables Writeback and frees the Writeback memory (all asics).
    468  * Used at driver startup.
    469  * Returns 0 on success or an -error on failure.
    470  */
    471 int radeon_wb_init(struct radeon_device *rdev)
    472 {
    473 	int r;
    474 
    475 	if (rdev->wb.wb_obj == NULL) {
    476 		r = radeon_bo_create(rdev, RADEON_GPU_PAGE_SIZE, PAGE_SIZE, true,
    477 				     RADEON_GEM_DOMAIN_GTT, 0, NULL, NULL,
    478 				     &rdev->wb.wb_obj);
    479 		if (r) {
    480 			dev_warn(rdev->dev, "(%d) create WB bo failed\n", r);
    481 			return r;
    482 		}
    483 		r = radeon_bo_reserve(rdev->wb.wb_obj, false);
    484 		if (unlikely(r != 0)) {
    485 			radeon_wb_fini(rdev);
    486 			return r;
    487 		}
    488 		r = radeon_bo_pin(rdev->wb.wb_obj, RADEON_GEM_DOMAIN_GTT,
    489 				&rdev->wb.gpu_addr);
    490 		if (r) {
    491 			radeon_bo_unreserve(rdev->wb.wb_obj);
    492 			dev_warn(rdev->dev, "(%d) pin WB bo failed\n", r);
    493 			radeon_wb_fini(rdev);
    494 			return r;
    495 		}
    496 		r = radeon_bo_kmap(rdev->wb.wb_obj, (void **)&rdev->wb.wb);
    497 		radeon_bo_unreserve(rdev->wb.wb_obj);
    498 		if (r) {
    499 			dev_warn(rdev->dev, "(%d) map WB bo failed\n", r);
    500 			radeon_wb_fini(rdev);
    501 			return r;
    502 		}
    503 	}
    504 
    505 	/* clear wb memory */
    506 	memset((char *)rdev->wb.wb, 0, RADEON_GPU_PAGE_SIZE);
    507 	/* disable event_write fences */
    508 	rdev->wb.use_event = false;
    509 	/* disabled via module param */
    510 	if (radeon_no_wb == 1) {
    511 		rdev->wb.enabled = false;
    512 	} else {
    513 		if (rdev->flags & RADEON_IS_AGP) {
    514 			/* often unreliable on AGP */
    515 			rdev->wb.enabled = false;
    516 		} else if (rdev->family < CHIP_R300) {
    517 			/* often unreliable on pre-r300 */
    518 			rdev->wb.enabled = false;
    519 		} else {
    520 			rdev->wb.enabled = true;
    521 			/* event_write fences are only available on r600+ */
    522 			if (rdev->family >= CHIP_R600) {
    523 				rdev->wb.use_event = true;
    524 			}
    525 		}
    526 	}
    527 	/* always use writeback/events on NI, APUs */
    528 	if (rdev->family >= CHIP_PALM) {
    529 		rdev->wb.enabled = true;
    530 		rdev->wb.use_event = true;
    531 	}
    532 
    533 	dev_info(rdev->dev, "WB %sabled\n", rdev->wb.enabled ? "en" : "dis");
    534 
    535 	return 0;
    536 }
    537 
    538 /**
    539  * radeon_vram_location - try to find VRAM location
    540  * @rdev: radeon device structure holding all necessary informations
    541  * @mc: memory controller structure holding memory informations
    542  * @base: base address at which to put VRAM
    543  *
    544  * Function will place try to place VRAM at base address provided
    545  * as parameter (which is so far either PCI aperture address or
    546  * for IGP TOM base address).
    547  *
    548  * If there is not enough space to fit the unvisible VRAM in the 32bits
    549  * address space then we limit the VRAM size to the aperture.
    550  *
    551  * If we are using AGP and if the AGP aperture doesn't allow us to have
    552  * room for all the VRAM than we restrict the VRAM to the PCI aperture
    553  * size and print a warning.
    554  *
    555  * This function will never fails, worst case are limiting VRAM.
    556  *
    557  * Note: GTT start, end, size should be initialized before calling this
    558  * function on AGP platform.
    559  *
    560  * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
    561  * this shouldn't be a problem as we are using the PCI aperture as a reference.
    562  * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
    563  * not IGP.
    564  *
    565  * Note: we use mc_vram_size as on some board we need to program the mc to
    566  * cover the whole aperture even if VRAM size is inferior to aperture size
    567  * Novell bug 204882 + along with lots of ubuntu ones
    568  *
    569  * Note: when limiting vram it's safe to overwritte real_vram_size because
    570  * we are not in case where real_vram_size is inferior to mc_vram_size (ie
    571  * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
    572  * ones)
    573  *
    574  * Note: IGP TOM addr should be the same as the aperture addr, we don't
    575  * explicitly check for that thought.
    576  *
    577  * FIXME: when reducing VRAM size align new size on power of 2.
    578  */
    579 void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base)
    580 {
    581 	uint64_t limit = (uint64_t)radeon_vram_limit << 20;
    582 
    583 	mc->vram_start = base;
    584 	if (mc->mc_vram_size > (rdev->mc.mc_mask - base + 1)) {
    585 		dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
    586 		mc->real_vram_size = mc->aper_size;
    587 		mc->mc_vram_size = mc->aper_size;
    588 	}
    589 	mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
    590 	if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_start <= mc->gtt_end) {
    591 		dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
    592 		mc->real_vram_size = mc->aper_size;
    593 		mc->mc_vram_size = mc->aper_size;
    594 	}
    595 	mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
    596 	if (limit && limit < mc->real_vram_size)
    597 		mc->real_vram_size = limit;
    598 	dev_info(rdev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
    599 			mc->mc_vram_size >> 20, mc->vram_start,
    600 			mc->vram_end, mc->real_vram_size >> 20);
    601 }
    602 
    603 /**
    604  * radeon_gtt_location - try to find GTT location
    605  * @rdev: radeon device structure holding all necessary informations
    606  * @mc: memory controller structure holding memory informations
    607  *
    608  * Function will place try to place GTT before or after VRAM.
    609  *
    610  * If GTT size is bigger than space left then we ajust GTT size.
    611  * Thus function will never fails.
    612  *
    613  * FIXME: when reducing GTT size align new size on power of 2.
    614  */
    615 void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc)
    616 {
    617 	u64 size_af, size_bf;
    618 
    619 	size_af = ((rdev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
    620 	size_bf = mc->vram_start & ~mc->gtt_base_align;
    621 	if (size_bf > size_af) {
    622 		if (mc->gtt_size > size_bf) {
    623 			dev_warn(rdev->dev, "limiting GTT\n");
    624 			mc->gtt_size = size_bf;
    625 		}
    626 		mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
    627 	} else {
    628 		if (mc->gtt_size > size_af) {
    629 			dev_warn(rdev->dev, "limiting GTT\n");
    630 			mc->gtt_size = size_af;
    631 		}
    632 		mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
    633 	}
    634 	mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
    635 	dev_info(rdev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
    636 			mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
    637 }
    638 
    639 /*
    640  * GPU helpers function.
    641  */
    642 
    643 /**
    644  * radeon_device_is_virtual - check if we are running is a virtual environment
    645  *
    646  * Check if the asic has been passed through to a VM (all asics).
    647  * Used at driver startup.
    648  * Returns true if virtual or false if not.
    649  */
    650 static bool radeon_device_is_virtual(void)
    651 {
    652 #ifdef CONFIG_X86
    653 	return boot_cpu_has(X86_FEATURE_HYPERVISOR);
    654 #else
    655 	return false;
    656 #endif
    657 }
    658 
    659 /**
    660  * radeon_card_posted - check if the hw has already been initialized
    661  *
    662  * @rdev: radeon_device pointer
    663  *
    664  * Check if the asic has been initialized (all asics).
    665  * Used at driver startup.
    666  * Returns true if initialized or false if not.
    667  */
    668 bool radeon_card_posted(struct radeon_device *rdev)
    669 {
    670 	uint32_t reg;
    671 
    672 	/* for pass through, always force asic_init for CI */
    673 	if (rdev->family >= CHIP_BONAIRE &&
    674 	    radeon_device_is_virtual())
    675 		return false;
    676 
    677 	/* required for EFI mode on macbook2,1 which uses an r5xx asic */
    678 	if (efi_enabled(EFI_BOOT) &&
    679 	    (rdev->pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE) &&
    680 	    (rdev->family < CHIP_R600))
    681 		return false;
    682 
    683 	if (ASIC_IS_NODCE(rdev))
    684 		goto check_memsize;
    685 
    686 	/* first check CRTCs */
    687 	if (ASIC_IS_DCE4(rdev)) {
    688 		reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
    689 			RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET);
    690 			if (rdev->num_crtc >= 4) {
    691 				reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) |
    692 					RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET);
    693 			}
    694 			if (rdev->num_crtc >= 6) {
    695 				reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) |
    696 					RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET);
    697 			}
    698 		if (reg & EVERGREEN_CRTC_MASTER_EN)
    699 			return true;
    700 	} else if (ASIC_IS_AVIVO(rdev)) {
    701 		reg = RREG32(AVIVO_D1CRTC_CONTROL) |
    702 		      RREG32(AVIVO_D2CRTC_CONTROL);
    703 		if (reg & AVIVO_CRTC_EN) {
    704 			return true;
    705 		}
    706 	} else {
    707 		reg = RREG32(RADEON_CRTC_GEN_CNTL) |
    708 		      RREG32(RADEON_CRTC2_GEN_CNTL);
    709 		if (reg & RADEON_CRTC_EN) {
    710 			return true;
    711 		}
    712 	}
    713 
    714 check_memsize:
    715 	/* then check MEM_SIZE, in case the crtcs are off */
    716 	if (rdev->family >= CHIP_R600)
    717 		reg = RREG32(R600_CONFIG_MEMSIZE);
    718 	else
    719 		reg = RREG32(RADEON_CONFIG_MEMSIZE);
    720 
    721 	if (reg)
    722 		return true;
    723 
    724 	return false;
    725 
    726 }
    727 
    728 /**
    729  * radeon_update_bandwidth_info - update display bandwidth params
    730  *
    731  * @rdev: radeon_device pointer
    732  *
    733  * Used when sclk/mclk are switched or display modes are set.
    734  * params are used to calculate display watermarks (all asics)
    735  */
    736 void radeon_update_bandwidth_info(struct radeon_device *rdev)
    737 {
    738 	fixed20_12 a;
    739 	u32 sclk = rdev->pm.current_sclk;
    740 	u32 mclk = rdev->pm.current_mclk;
    741 
    742 	/* sclk/mclk in Mhz */
    743 	a.full = dfixed_const(100);
    744 	rdev->pm.sclk.full = dfixed_const(sclk);
    745 	rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a);
    746 	rdev->pm.mclk.full = dfixed_const(mclk);
    747 	rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a);
    748 
    749 	if (rdev->flags & RADEON_IS_IGP) {
    750 		a.full = dfixed_const(16);
    751 		/* core_bandwidth = sclk(Mhz) * 16 */
    752 		rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a);
    753 	}
    754 }
    755 
    756 /**
    757  * radeon_boot_test_post_card - check and possibly initialize the hw
    758  *
    759  * @rdev: radeon_device pointer
    760  *
    761  * Check if the asic is initialized and if not, attempt to initialize
    762  * it (all asics).
    763  * Returns true if initialized or false if not.
    764  */
    765 bool radeon_boot_test_post_card(struct radeon_device *rdev)
    766 {
    767 	if (radeon_card_posted(rdev))
    768 		return true;
    769 
    770 	if (rdev->bios) {
    771 		DRM_INFO("GPU not posted. posting now...\n");
    772 		if (rdev->is_atom_bios)
    773 			atom_asic_init(rdev->mode_info.atom_context);
    774 		else
    775 			radeon_combios_asic_init(rdev->ddev);
    776 		return true;
    777 	} else {
    778 		dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
    779 		return false;
    780 	}
    781 }
    782 
    783 /**
    784  * radeon_dummy_page_init - init dummy page used by the driver
    785  *
    786  * @rdev: radeon_device pointer
    787  *
    788  * Allocate the dummy page used by the driver (all asics).
    789  * This dummy page is used by the driver as a filler for gart entries
    790  * when pages are taken out of the GART
    791  * Returns 0 on sucess, -ENOMEM on failure.
    792  */
    793 int radeon_dummy_page_init(struct radeon_device *rdev)
    794 {
    795 	if (rdev->dummy_page.page)
    796 		return 0;
    797 	rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
    798 	if (rdev->dummy_page.page == NULL)
    799 		return -ENOMEM;
    800 	rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page,
    801 					0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
    802 	if (pci_dma_mapping_error(rdev->pdev, rdev->dummy_page.addr)) {
    803 		dev_err(&rdev->pdev->dev, "Failed to DMA MAP the dummy page\n");
    804 		__free_page(rdev->dummy_page.page);
    805 		rdev->dummy_page.page = NULL;
    806 		return -ENOMEM;
    807 	}
    808 	rdev->dummy_page.entry = radeon_gart_get_page_entry(rdev->dummy_page.addr,
    809 							    RADEON_GART_PAGE_DUMMY);
    810 	return 0;
    811 }
    812 
    813 /**
    814  * radeon_dummy_page_fini - free dummy page used by the driver
    815  *
    816  * @rdev: radeon_device pointer
    817  *
    818  * Frees the dummy page used by the driver (all asics).
    819  */
    820 void radeon_dummy_page_fini(struct radeon_device *rdev)
    821 {
    822 	if (rdev->dummy_page.page == NULL)
    823 		return;
    824 	pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
    825 			PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
    826 	__free_page(rdev->dummy_page.page);
    827 	rdev->dummy_page.page = NULL;
    828 }
    829 
    830 
    831 /* ATOM accessor methods */
    832 /*
    833  * ATOM is an interpreted byte code stored in tables in the vbios.  The
    834  * driver registers callbacks to access registers and the interpreter
    835  * in the driver parses the tables and executes then to program specific
    836  * actions (set display modes, asic init, etc.).  See radeon_atombios.c,
    837  * atombios.h, and atom.c
    838  */
    839 
    840 /**
    841  * cail_pll_read - read PLL register
    842  *
    843  * @info: atom card_info pointer
    844  * @reg: PLL register offset
    845  *
    846  * Provides a PLL register accessor for the atom interpreter (r4xx+).
    847  * Returns the value of the PLL register.
    848  */
    849 static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
    850 {
    851 	struct radeon_device *rdev = info->dev->dev_private;
    852 	uint32_t r;
    853 
    854 	r = rdev->pll_rreg(rdev, reg);
    855 	return r;
    856 }
    857 
    858 /**
    859  * cail_pll_write - write PLL register
    860  *
    861  * @info: atom card_info pointer
    862  * @reg: PLL register offset
    863  * @val: value to write to the pll register
    864  *
    865  * Provides a PLL register accessor for the atom interpreter (r4xx+).
    866  */
    867 static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
    868 {
    869 	struct radeon_device *rdev = info->dev->dev_private;
    870 
    871 	rdev->pll_wreg(rdev, reg, val);
    872 }
    873 
    874 /**
    875  * cail_mc_read - read MC (Memory Controller) register
    876  *
    877  * @info: atom card_info pointer
    878  * @reg: MC register offset
    879  *
    880  * Provides an MC register accessor for the atom interpreter (r4xx+).
    881  * Returns the value of the MC register.
    882  */
    883 static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
    884 {
    885 	struct radeon_device *rdev = info->dev->dev_private;
    886 	uint32_t r;
    887 
    888 	r = rdev->mc_rreg(rdev, reg);
    889 	return r;
    890 }
    891 
    892 /**
    893  * cail_mc_write - write MC (Memory Controller) register
    894  *
    895  * @info: atom card_info pointer
    896  * @reg: MC register offset
    897  * @val: value to write to the pll register
    898  *
    899  * Provides a MC register accessor for the atom interpreter (r4xx+).
    900  */
    901 static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
    902 {
    903 	struct radeon_device *rdev = info->dev->dev_private;
    904 
    905 	rdev->mc_wreg(rdev, reg, val);
    906 }
    907 
    908 /**
    909  * cail_reg_write - write MMIO register
    910  *
    911  * @info: atom card_info pointer
    912  * @reg: MMIO register offset
    913  * @val: value to write to the pll register
    914  *
    915  * Provides a MMIO register accessor for the atom interpreter (r4xx+).
    916  */
    917 static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
    918 {
    919 	struct radeon_device *rdev = info->dev->dev_private;
    920 
    921 	WREG32(reg*4, val);
    922 }
    923 
    924 /**
    925  * cail_reg_read - read MMIO register
    926  *
    927  * @info: atom card_info pointer
    928  * @reg: MMIO register offset
    929  *
    930  * Provides an MMIO register accessor for the atom interpreter (r4xx+).
    931  * Returns the value of the MMIO register.
    932  */
    933 static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
    934 {
    935 	struct radeon_device *rdev = info->dev->dev_private;
    936 	uint32_t r;
    937 
    938 	r = RREG32(reg*4);
    939 	return r;
    940 }
    941 
    942 /**
    943  * cail_ioreg_write - write IO register
    944  *
    945  * @info: atom card_info pointer
    946  * @reg: IO register offset
    947  * @val: value to write to the pll register
    948  *
    949  * Provides a IO register accessor for the atom interpreter (r4xx+).
    950  */
    951 static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
    952 {
    953 	struct radeon_device *rdev = info->dev->dev_private;
    954 
    955 	WREG32_IO(reg*4, val);
    956 }
    957 
    958 /**
    959  * cail_ioreg_read - read IO register
    960  *
    961  * @info: atom card_info pointer
    962  * @reg: IO register offset
    963  *
    964  * Provides an IO register accessor for the atom interpreter (r4xx+).
    965  * Returns the value of the IO register.
    966  */
    967 static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
    968 {
    969 	struct radeon_device *rdev = info->dev->dev_private;
    970 	uint32_t r;
    971 
    972 	r = RREG32_IO(reg*4);
    973 	return r;
    974 }
    975 
    976 /**
    977  * radeon_atombios_init - init the driver info and callbacks for atombios
    978  *
    979  * @rdev: radeon_device pointer
    980  *
    981  * Initializes the driver info and register access callbacks for the
    982  * ATOM interpreter (r4xx+).
    983  * Returns 0 on sucess, -ENOMEM on failure.
    984  * Called at driver startup.
    985  */
    986 int radeon_atombios_init(struct radeon_device *rdev)
    987 {
    988 	struct card_info *atom_card_info =
    989 	    kzalloc(sizeof(struct card_info), GFP_KERNEL);
    990 
    991 	if (!atom_card_info)
    992 		return -ENOMEM;
    993 
    994 	rdev->mode_info.atom_card_info = atom_card_info;
    995 	atom_card_info->dev = rdev->ddev;
    996 	atom_card_info->reg_read = cail_reg_read;
    997 	atom_card_info->reg_write = cail_reg_write;
    998 	/* needed for iio ops */
    999 	if (rdev->rio_mem) {
   1000 		atom_card_info->ioreg_read = cail_ioreg_read;
   1001 		atom_card_info->ioreg_write = cail_ioreg_write;
   1002 	} else {
   1003 		DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
   1004 		atom_card_info->ioreg_read = cail_reg_read;
   1005 		atom_card_info->ioreg_write = cail_reg_write;
   1006 	}
   1007 	atom_card_info->mc_read = cail_mc_read;
   1008 	atom_card_info->mc_write = cail_mc_write;
   1009 	atom_card_info->pll_read = cail_pll_read;
   1010 	atom_card_info->pll_write = cail_pll_write;
   1011 
   1012 	rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
   1013 	if (!rdev->mode_info.atom_context) {
   1014 		radeon_atombios_fini(rdev);
   1015 		return -ENOMEM;
   1016 	}
   1017 
   1018 	mutex_init(&rdev->mode_info.atom_context->mutex);
   1019 	mutex_init(&rdev->mode_info.atom_context->scratch_mutex);
   1020 	radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
   1021 	atom_allocate_fb_scratch(rdev->mode_info.atom_context);
   1022 	return 0;
   1023 }
   1024 
   1025 /**
   1026  * radeon_atombios_fini - free the driver info and callbacks for atombios
   1027  *
   1028  * @rdev: radeon_device pointer
   1029  *
   1030  * Frees the driver info and register access callbacks for the ATOM
   1031  * interpreter (r4xx+).
   1032  * Called at driver shutdown.
   1033  */
   1034 void radeon_atombios_fini(struct radeon_device *rdev)
   1035 {
   1036 	if (rdev->mode_info.atom_context) {
   1037 		kfree(rdev->mode_info.atom_context->scratch);
   1038 	}
   1039 	kfree(rdev->mode_info.atom_context);
   1040 	rdev->mode_info.atom_context = NULL;
   1041 	kfree(rdev->mode_info.atom_card_info);
   1042 	rdev->mode_info.atom_card_info = NULL;
   1043 }
   1044 
   1045 /* COMBIOS */
   1046 /*
   1047  * COMBIOS is the bios format prior to ATOM. It provides
   1048  * command tables similar to ATOM, but doesn't have a unified
   1049  * parser.  See radeon_combios.c
   1050  */
   1051 
   1052 /**
   1053  * radeon_combios_init - init the driver info for combios
   1054  *
   1055  * @rdev: radeon_device pointer
   1056  *
   1057  * Initializes the driver info for combios (r1xx-r3xx).
   1058  * Returns 0 on sucess.
   1059  * Called at driver startup.
   1060  */
   1061 int radeon_combios_init(struct radeon_device *rdev)
   1062 {
   1063 	radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
   1064 	return 0;
   1065 }
   1066 
   1067 /**
   1068  * radeon_combios_fini - free the driver info for combios
   1069  *
   1070  * @rdev: radeon_device pointer
   1071  *
   1072  * Frees the driver info for combios (r1xx-r3xx).
   1073  * Called at driver shutdown.
   1074  */
   1075 void radeon_combios_fini(struct radeon_device *rdev)
   1076 {
   1077 }
   1078 
   1079 /* if we get transitioned to only one device, take VGA back */
   1080 /**
   1081  * radeon_vga_set_decode - enable/disable vga decode
   1082  *
   1083  * @cookie: radeon_device pointer
   1084  * @state: enable/disable vga decode
   1085  *
   1086  * Enable/disable vga decode (all asics).
   1087  * Returns VGA resource flags.
   1088  */
   1089 static unsigned int radeon_vga_set_decode(void *cookie, bool state)
   1090 {
   1091 	struct radeon_device *rdev = cookie;
   1092 	radeon_vga_set_state(rdev, state);
   1093 	if (state)
   1094 		return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
   1095 		       VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
   1096 	else
   1097 		return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
   1098 }
   1099 
   1100 /**
   1101  * radeon_check_pot_argument - check that argument is a power of two
   1102  *
   1103  * @arg: value to check
   1104  *
   1105  * Validates that a certain argument is a power of two (all asics).
   1106  * Returns true if argument is valid.
   1107  */
   1108 static bool radeon_check_pot_argument(int arg)
   1109 {
   1110 	return (arg & (arg - 1)) == 0;
   1111 }
   1112 
   1113 /**
   1114  * Determine a sensible default GART size according to ASIC family.
   1115  *
   1116  * @family ASIC family name
   1117  */
   1118 static int radeon_gart_size_auto(enum radeon_family family)
   1119 {
   1120 	/* default to a larger gart size on newer asics */
   1121 	if (family >= CHIP_TAHITI)
   1122 		return 2048;
   1123 	else if (family >= CHIP_RV770)
   1124 		return 1024;
   1125 	else
   1126 		return 512;
   1127 }
   1128 
   1129 /**
   1130  * radeon_check_arguments - validate module params
   1131  *
   1132  * @rdev: radeon_device pointer
   1133  *
   1134  * Validates certain module parameters and updates
   1135  * the associated values used by the driver (all asics).
   1136  */
   1137 static void radeon_check_arguments(struct radeon_device *rdev)
   1138 {
   1139 	/* vramlimit must be a power of two */
   1140 	if (!radeon_check_pot_argument(radeon_vram_limit)) {
   1141 		dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n",
   1142 				radeon_vram_limit);
   1143 		radeon_vram_limit = 0;
   1144 	}
   1145 
   1146 	if (radeon_gart_size == -1) {
   1147 		radeon_gart_size = radeon_gart_size_auto(rdev->family);
   1148 	}
   1149 	/* gtt size must be power of two and greater or equal to 32M */
   1150 	if (radeon_gart_size < 32) {
   1151 		dev_warn(rdev->dev, "gart size (%d) too small\n",
   1152 				radeon_gart_size);
   1153 		radeon_gart_size = radeon_gart_size_auto(rdev->family);
   1154 	} else if (!radeon_check_pot_argument(radeon_gart_size)) {
   1155 		dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
   1156 				radeon_gart_size);
   1157 		radeon_gart_size = radeon_gart_size_auto(rdev->family);
   1158 	}
   1159 	rdev->mc.gtt_size = (uint64_t)radeon_gart_size << 20;
   1160 
   1161 	/* AGP mode can only be -1, 1, 2, 4, 8 */
   1162 	switch (radeon_agpmode) {
   1163 	case -1:
   1164 	case 0:
   1165 	case 1:
   1166 	case 2:
   1167 	case 4:
   1168 	case 8:
   1169 		break;
   1170 	default:
   1171 		dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
   1172 				"-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
   1173 		radeon_agpmode = 0;
   1174 		break;
   1175 	}
   1176 
   1177 	if (!radeon_check_pot_argument(radeon_vm_size)) {
   1178 		dev_warn(rdev->dev, "VM size (%d) must be a power of 2\n",
   1179 			 radeon_vm_size);
   1180 		radeon_vm_size = 4;
   1181 	}
   1182 
   1183 	if (radeon_vm_size < 1) {
   1184 		dev_warn(rdev->dev, "VM size (%d) to small, min is 1GB\n",
   1185 			 radeon_vm_size);
   1186 		radeon_vm_size = 4;
   1187 	}
   1188 
   1189        /*
   1190         * Max GPUVM size for Cayman, SI and CI are 40 bits.
   1191         */
   1192 	if (radeon_vm_size > 1024) {
   1193 		dev_warn(rdev->dev, "VM size (%d) too large, max is 1TB\n",
   1194 			 radeon_vm_size);
   1195 		radeon_vm_size = 4;
   1196 	}
   1197 
   1198 	/* defines number of bits in page table versus page directory,
   1199 	 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
   1200 	 * page table and the remaining bits are in the page directory */
   1201 	if (radeon_vm_block_size == -1) {
   1202 
   1203 		/* Total bits covered by PD + PTs */
   1204 		unsigned bits = ilog2(radeon_vm_size) + 18;
   1205 
   1206 		/* Make sure the PD is 4K in size up to 8GB address space.
   1207 		   Above that split equal between PD and PTs */
   1208 		if (radeon_vm_size <= 8)
   1209 			radeon_vm_block_size = bits - 9;
   1210 		else
   1211 			radeon_vm_block_size = (bits + 3) / 2;
   1212 
   1213 	} else if (radeon_vm_block_size < 9) {
   1214 		dev_warn(rdev->dev, "VM page table size (%d) too small\n",
   1215 			 radeon_vm_block_size);
   1216 		radeon_vm_block_size = 9;
   1217 	}
   1218 
   1219 	if (radeon_vm_block_size > 24 ||
   1220 	    (radeon_vm_size * 1024) < (1ull << radeon_vm_block_size)) {
   1221 		dev_warn(rdev->dev, "VM page table size (%d) too large\n",
   1222 			 radeon_vm_block_size);
   1223 		radeon_vm_block_size = 9;
   1224 	}
   1225 }
   1226 
   1227 /**
   1228  * radeon_switcheroo_set_state - set switcheroo state
   1229  *
   1230  * @pdev: pci dev pointer
   1231  * @state: vga_switcheroo state
   1232  *
   1233  * Callback for the switcheroo driver.  Suspends or resumes the
   1234  * the asics before or after it is powered up using ACPI methods.
   1235  */
   1236 static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
   1237 {
   1238 	struct drm_device *dev = pci_get_drvdata(pdev);
   1239 	struct radeon_device *rdev = dev->dev_private;
   1240 
   1241 	if (radeon_is_px(dev) && state == VGA_SWITCHEROO_OFF)
   1242 		return;
   1243 
   1244 	if (state == VGA_SWITCHEROO_ON) {
   1245 		unsigned d3_delay = dev->pdev->d3_delay;
   1246 
   1247 		printk(KERN_INFO "radeon: switched on\n");
   1248 		/* don't suspend or resume card normally */
   1249 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
   1250 
   1251 		if (d3_delay < 20 && (rdev->px_quirk_flags & RADEON_PX_QUIRK_LONG_WAKEUP))
   1252 			dev->pdev->d3_delay = 20;
   1253 
   1254 		radeon_resume_kms(dev, true, true);
   1255 
   1256 		dev->pdev->d3_delay = d3_delay;
   1257 
   1258 		dev->switch_power_state = DRM_SWITCH_POWER_ON;
   1259 		drm_kms_helper_poll_enable(dev);
   1260 	} else {
   1261 		printk(KERN_INFO "radeon: switched off\n");
   1262 		drm_kms_helper_poll_disable(dev);
   1263 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
   1264 		radeon_suspend_kms(dev, true, true);
   1265 		dev->switch_power_state = DRM_SWITCH_POWER_OFF;
   1266 	}
   1267 }
   1268 
   1269 /**
   1270  * radeon_switcheroo_can_switch - see if switcheroo state can change
   1271  *
   1272  * @pdev: pci dev pointer
   1273  *
   1274  * Callback for the switcheroo driver.  Check of the switcheroo
   1275  * state can be changed.
   1276  * Returns true if the state can be changed, false if not.
   1277  */
   1278 static bool radeon_switcheroo_can_switch(struct pci_dev *pdev)
   1279 {
   1280 	struct drm_device *dev = pci_get_drvdata(pdev);
   1281 
   1282 	/*
   1283 	 * FIXME: open_count is protected by drm_global_mutex but that would lead to
   1284 	 * locking inversion with the driver load path. And the access here is
   1285 	 * completely racy anyway. So don't bother with locking for now.
   1286 	 */
   1287 	return dev->open_count == 0;
   1288 }
   1289 
   1290 static const struct vga_switcheroo_client_ops radeon_switcheroo_ops = {
   1291 	.set_gpu_state = radeon_switcheroo_set_state,
   1292 	.reprobe = NULL,
   1293 	.can_switch = radeon_switcheroo_can_switch,
   1294 };
   1295 
   1296 /**
   1297  * radeon_device_init - initialize the driver
   1298  *
   1299  * @rdev: radeon_device pointer
   1300  * @pdev: drm dev pointer
   1301  * @pdev: pci dev pointer
   1302  * @flags: driver flags
   1303  *
   1304  * Initializes the driver info and hw (all asics).
   1305  * Returns 0 for success or an error on failure.
   1306  * Called at driver startup.
   1307  */
   1308 int radeon_device_init(struct radeon_device *rdev,
   1309 		       struct drm_device *ddev,
   1310 		       struct pci_dev *pdev,
   1311 		       uint32_t flags)
   1312 {
   1313 	int r, i;
   1314 	int dma_bits;
   1315 	bool runtime = false;
   1316 
   1317 	rdev->shutdown = false;
   1318 	rdev->dev = &pdev->dev;
   1319 	rdev->ddev = ddev;
   1320 	rdev->pdev = pdev;
   1321 	rdev->flags = flags;
   1322 	rdev->family = flags & RADEON_FAMILY_MASK;
   1323 	rdev->is_atom_bios = false;
   1324 	rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
   1325 	rdev->mc.gtt_size = 512 * 1024 * 1024;
   1326 	rdev->accel_working = false;
   1327 	/* set up ring ids */
   1328 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
   1329 		rdev->ring[i].idx = i;
   1330 	}
   1331 	rdev->fence_context = fence_context_alloc(RADEON_NUM_RINGS);
   1332 
   1333 	DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X).\n",
   1334 		radeon_family_name[rdev->family], pdev->vendor, pdev->device,
   1335 		pdev->subsystem_vendor, pdev->subsystem_device);
   1336 
   1337 	/* mutex initialization are all done here so we
   1338 	 * can recall function without having locking issues */
   1339 	mutex_init(&rdev->ring_lock);
   1340 	mutex_init(&rdev->dc_hw_i2c_mutex);
   1341 	atomic_set(&rdev->ih.lock, 0);
   1342 	mutex_init(&rdev->gem.mutex);
   1343 	mutex_init(&rdev->pm.mutex);
   1344 	mutex_init(&rdev->gpu_clock_mutex);
   1345 	mutex_init(&rdev->srbm_mutex);
   1346 	mutex_init(&rdev->grbm_idx_mutex);
   1347 	init_rwsem(&rdev->pm.mclk_lock);
   1348 	init_rwsem(&rdev->exclusive_lock);
   1349 	init_waitqueue_head(&rdev->irq.vblank_queue);
   1350 	mutex_init(&rdev->mn_lock);
   1351 	hash_init(rdev->mn_hash);
   1352 	r = radeon_gem_init(rdev);
   1353 	if (r)
   1354 		return r;
   1355 
   1356 	radeon_check_arguments(rdev);
   1357 	/* Adjust VM size here.
   1358 	 * Max GPUVM size for cayman+ is 40 bits.
   1359 	 */
   1360 	rdev->vm_manager.max_pfn = radeon_vm_size << 18;
   1361 
   1362 	/* Set asic functions */
   1363 	r = radeon_asic_init(rdev);
   1364 	if (r)
   1365 		return r;
   1366 
   1367 	/* all of the newer IGP chips have an internal gart
   1368 	 * However some rs4xx report as AGP, so remove that here.
   1369 	 */
   1370 	if ((rdev->family >= CHIP_RS400) &&
   1371 	    (rdev->flags & RADEON_IS_IGP)) {
   1372 		rdev->flags &= ~RADEON_IS_AGP;
   1373 	}
   1374 
   1375 	if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
   1376 		radeon_agp_disable(rdev);
   1377 	}
   1378 
   1379 	/* Set the internal MC address mask
   1380 	 * This is the max address of the GPU's
   1381 	 * internal address space.
   1382 	 */
   1383 	if (rdev->family >= CHIP_CAYMAN)
   1384 		rdev->mc.mc_mask = 0xffffffffffULL; /* 40 bit MC */
   1385 	else if (rdev->family >= CHIP_CEDAR)
   1386 		rdev->mc.mc_mask = 0xfffffffffULL; /* 36 bit MC */
   1387 	else
   1388 		rdev->mc.mc_mask = 0xffffffffULL; /* 32 bit MC */
   1389 
   1390 	/* set DMA mask + need_dma32 flags.
   1391 	 * PCIE - can handle 40-bits.
   1392 	 * IGP - can handle 40-bits
   1393 	 * AGP - generally dma32 is safest
   1394 	 * PCI - dma32 for legacy pci gart, 40 bits on newer asics
   1395 	 */
   1396 	rdev->need_dma32 = false;
   1397 	if (rdev->flags & RADEON_IS_AGP)
   1398 		rdev->need_dma32 = true;
   1399 	if ((rdev->flags & RADEON_IS_PCI) &&
   1400 	    (rdev->family <= CHIP_RS740))
   1401 		rdev->need_dma32 = true;
   1402 
   1403 	dma_bits = rdev->need_dma32 ? 32 : 40;
   1404 	r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
   1405 	if (r) {
   1406 		rdev->need_dma32 = true;
   1407 		dma_bits = 32;
   1408 		printk(KERN_WARNING "radeon: No suitable DMA available.\n");
   1409 	}
   1410 	r = pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
   1411 	if (r) {
   1412 		pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(32));
   1413 		printk(KERN_WARNING "radeon: No coherent DMA available.\n");
   1414 	}
   1415 
   1416 	/* Registers mapping */
   1417 	/* TODO: block userspace mapping of io register */
   1418 	spin_lock_init(&rdev->mmio_idx_lock);
   1419 	spin_lock_init(&rdev->smc_idx_lock);
   1420 	spin_lock_init(&rdev->pll_idx_lock);
   1421 	spin_lock_init(&rdev->mc_idx_lock);
   1422 	spin_lock_init(&rdev->pcie_idx_lock);
   1423 	spin_lock_init(&rdev->pciep_idx_lock);
   1424 	spin_lock_init(&rdev->pif_idx_lock);
   1425 	spin_lock_init(&rdev->cg_idx_lock);
   1426 	spin_lock_init(&rdev->uvd_idx_lock);
   1427 	spin_lock_init(&rdev->rcu_idx_lock);
   1428 	spin_lock_init(&rdev->didt_idx_lock);
   1429 	spin_lock_init(&rdev->end_idx_lock);
   1430 	if (rdev->family >= CHIP_BONAIRE) {
   1431 		rdev->rmmio_base = pci_resource_start(rdev->pdev, 5);
   1432 		rdev->rmmio_size = pci_resource_len(rdev->pdev, 5);
   1433 	} else {
   1434 		rdev->rmmio_base = pci_resource_start(rdev->pdev, 2);
   1435 		rdev->rmmio_size = pci_resource_len(rdev->pdev, 2);
   1436 	}
   1437 	rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size);
   1438 	if (rdev->rmmio == NULL) {
   1439 		return -ENOMEM;
   1440 	}
   1441 	DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base);
   1442 	DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size);
   1443 
   1444 	/* doorbell bar mapping */
   1445 	if (rdev->family >= CHIP_BONAIRE)
   1446 		radeon_doorbell_init(rdev);
   1447 
   1448 	/* io port mapping */
   1449 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
   1450 		if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) {
   1451 			rdev->rio_mem_size = pci_resource_len(rdev->pdev, i);
   1452 			rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size);
   1453 			break;
   1454 		}
   1455 	}
   1456 	if (rdev->rio_mem == NULL)
   1457 		DRM_ERROR("Unable to find PCI I/O BAR\n");
   1458 
   1459 	if (rdev->flags & RADEON_IS_PX)
   1460 		radeon_device_handle_px_quirks(rdev);
   1461 
   1462 	/* if we have > 1 VGA cards, then disable the radeon VGA resources */
   1463 	/* this will fail for cards that aren't VGA class devices, just
   1464 	 * ignore it */
   1465 	vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
   1466 
   1467 	if (rdev->flags & RADEON_IS_PX)
   1468 		runtime = true;
   1469 	vga_switcheroo_register_client(rdev->pdev, &radeon_switcheroo_ops, runtime);
   1470 	if (runtime)
   1471 		vga_switcheroo_init_domain_pm_ops(rdev->dev, &rdev->vga_pm_domain);
   1472 
   1473 	r = radeon_init(rdev);
   1474 	if (r)
   1475 		goto failed;
   1476 
   1477 	r = radeon_gem_debugfs_init(rdev);
   1478 	if (r) {
   1479 		DRM_ERROR("registering gem debugfs failed (%d).\n", r);
   1480 	}
   1481 
   1482 	r = radeon_mst_debugfs_init(rdev);
   1483 	if (r) {
   1484 		DRM_ERROR("registering mst debugfs failed (%d).\n", r);
   1485 	}
   1486 
   1487 	if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
   1488 		/* Acceleration not working on AGP card try again
   1489 		 * with fallback to PCI or PCIE GART
   1490 		 */
   1491 		radeon_asic_reset(rdev);
   1492 		radeon_fini(rdev);
   1493 		radeon_agp_disable(rdev);
   1494 		r = radeon_init(rdev);
   1495 		if (r)
   1496 			goto failed;
   1497 	}
   1498 
   1499 	r = radeon_ib_ring_tests(rdev);
   1500 	if (r)
   1501 		DRM_ERROR("ib ring test failed (%d).\n", r);
   1502 
   1503 	/*
   1504 	 * Turks/Thames GPU will freeze whole laptop if DPM is not restarted
   1505 	 * after the CP ring have chew one packet at least. Hence here we stop
   1506 	 * and restart DPM after the radeon_ib_ring_tests().
   1507 	 */
   1508 	if (rdev->pm.dpm_enabled &&
   1509 	    (rdev->pm.pm_method == PM_METHOD_DPM) &&
   1510 	    (rdev->family == CHIP_TURKS) &&
   1511 	    (rdev->flags & RADEON_IS_MOBILITY)) {
   1512 		mutex_lock(&rdev->pm.mutex);
   1513 		radeon_dpm_disable(rdev);
   1514 		radeon_dpm_enable(rdev);
   1515 		mutex_unlock(&rdev->pm.mutex);
   1516 	}
   1517 
   1518 	if ((radeon_testing & 1)) {
   1519 		if (rdev->accel_working)
   1520 			radeon_test_moves(rdev);
   1521 		else
   1522 			DRM_INFO("radeon: acceleration disabled, skipping move tests\n");
   1523 	}
   1524 	if ((radeon_testing & 2)) {
   1525 		if (rdev->accel_working)
   1526 			radeon_test_syncing(rdev);
   1527 		else
   1528 			DRM_INFO("radeon: acceleration disabled, skipping sync tests\n");
   1529 	}
   1530 	if (radeon_benchmarking) {
   1531 		if (rdev->accel_working)
   1532 			radeon_benchmark(rdev, radeon_benchmarking);
   1533 		else
   1534 			DRM_INFO("radeon: acceleration disabled, skipping benchmarks\n");
   1535 	}
   1536 	return 0;
   1537 
   1538 failed:
   1539 	if (runtime)
   1540 		vga_switcheroo_fini_domain_pm_ops(rdev->dev);
   1541 	return r;
   1542 }
   1543 
   1544 static void radeon_debugfs_remove_files(struct radeon_device *rdev);
   1545 
   1546 /**
   1547  * radeon_device_fini - tear down the driver
   1548  *
   1549  * @rdev: radeon_device pointer
   1550  *
   1551  * Tear down the driver info (all asics).
   1552  * Called at driver shutdown.
   1553  */
   1554 void radeon_device_fini(struct radeon_device *rdev)
   1555 {
   1556 	DRM_INFO("radeon: finishing device.\n");
   1557 	rdev->shutdown = true;
   1558 	/* evict vram memory */
   1559 	radeon_bo_evict_vram(rdev);
   1560 	radeon_fini(rdev);
   1561 	vga_switcheroo_unregister_client(rdev->pdev);
   1562 	if (rdev->flags & RADEON_IS_PX)
   1563 		vga_switcheroo_fini_domain_pm_ops(rdev->dev);
   1564 	vga_client_register(rdev->pdev, NULL, NULL, NULL);
   1565 	if (rdev->rio_mem)
   1566 		pci_iounmap(rdev->pdev, rdev->rio_mem);
   1567 	rdev->rio_mem = NULL;
   1568 	iounmap(rdev->rmmio);
   1569 	rdev->rmmio = NULL;
   1570 	if (rdev->family >= CHIP_BONAIRE)
   1571 		radeon_doorbell_fini(rdev);
   1572 	radeon_debugfs_remove_files(rdev);
   1573 }
   1574 
   1575 
   1576 /*
   1577  * Suspend & resume.
   1578  */
   1579 /**
   1580  * radeon_suspend_kms - initiate device suspend
   1581  *
   1582  * @pdev: drm dev pointer
   1583  * @state: suspend state
   1584  *
   1585  * Puts the hw in the suspend state (all asics).
   1586  * Returns 0 for success or an error on failure.
   1587  * Called at driver suspend.
   1588  */
   1589 int radeon_suspend_kms(struct drm_device *dev, bool suspend, bool fbcon)
   1590 {
   1591 	struct radeon_device *rdev;
   1592 	struct drm_crtc *crtc;
   1593 	struct drm_connector *connector;
   1594 	int i, r;
   1595 
   1596 	if (dev == NULL || dev->dev_private == NULL) {
   1597 		return -ENODEV;
   1598 	}
   1599 
   1600 	rdev = dev->dev_private;
   1601 
   1602 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
   1603 		return 0;
   1604 
   1605 	drm_kms_helper_poll_disable(dev);
   1606 
   1607 	drm_modeset_lock_all(dev);
   1608 	/* turn off display hw */
   1609 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
   1610 		drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
   1611 	}
   1612 	drm_modeset_unlock_all(dev);
   1613 
   1614 	/* unpin the front buffers and cursors */
   1615 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
   1616 		struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
   1617 		struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->primary->fb);
   1618 		struct radeon_bo *robj;
   1619 
   1620 		if (radeon_crtc->cursor_bo) {
   1621 			struct radeon_bo *robj = gem_to_radeon_bo(radeon_crtc->cursor_bo);
   1622 			r = radeon_bo_reserve(robj, false);
   1623 			if (r == 0) {
   1624 				radeon_bo_unpin(robj);
   1625 				radeon_bo_unreserve(robj);
   1626 			}
   1627 		}
   1628 
   1629 		if (rfb == NULL || rfb->obj == NULL) {
   1630 			continue;
   1631 		}
   1632 		robj = gem_to_radeon_bo(rfb->obj);
   1633 		/* don't unpin kernel fb objects */
   1634 		if (!radeon_fbdev_robj_is_fb(rdev, robj)) {
   1635 			r = radeon_bo_reserve(robj, false);
   1636 			if (r == 0) {
   1637 				radeon_bo_unpin(robj);
   1638 				radeon_bo_unreserve(robj);
   1639 			}
   1640 		}
   1641 	}
   1642 	/* evict vram memory */
   1643 	radeon_bo_evict_vram(rdev);
   1644 
   1645 	/* wait for gpu to finish processing current batch */
   1646 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
   1647 		r = radeon_fence_wait_empty(rdev, i);
   1648 		if (r) {
   1649 			/* delay GPU reset to resume */
   1650 			radeon_fence_driver_force_completion(rdev, i);
   1651 		}
   1652 	}
   1653 
   1654 	radeon_save_bios_scratch_regs(rdev);
   1655 
   1656 	radeon_suspend(rdev);
   1657 	radeon_hpd_fini(rdev);
   1658 	/* evict remaining vram memory */
   1659 	radeon_bo_evict_vram(rdev);
   1660 
   1661 	radeon_agp_suspend(rdev);
   1662 
   1663 	pci_save_state(dev->pdev);
   1664 	if (suspend) {
   1665 		/* Shut down the device */
   1666 		pci_disable_device(dev->pdev);
   1667 		pci_set_power_state(dev->pdev, PCI_D3hot);
   1668 	}
   1669 
   1670 	if (fbcon) {
   1671 		console_lock();
   1672 		radeon_fbdev_set_suspend(rdev, 1);
   1673 		console_unlock();
   1674 	}
   1675 	return 0;
   1676 }
   1677 
   1678 /**
   1679  * radeon_resume_kms - initiate device resume
   1680  *
   1681  * @pdev: drm dev pointer
   1682  *
   1683  * Bring the hw back to operating state (all asics).
   1684  * Returns 0 for success or an error on failure.
   1685  * Called at driver resume.
   1686  */
   1687 int radeon_resume_kms(struct drm_device *dev, bool resume, bool fbcon)
   1688 {
   1689 	struct drm_connector *connector;
   1690 	struct radeon_device *rdev = dev->dev_private;
   1691 	struct drm_crtc *crtc;
   1692 	int r;
   1693 
   1694 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
   1695 		return 0;
   1696 
   1697 	if (fbcon) {
   1698 		console_lock();
   1699 	}
   1700 	if (resume) {
   1701 		pci_set_power_state(dev->pdev, PCI_D0);
   1702 		pci_restore_state(dev->pdev);
   1703 		if (pci_enable_device(dev->pdev)) {
   1704 			if (fbcon)
   1705 				console_unlock();
   1706 			return -1;
   1707 		}
   1708 	}
   1709 	/* resume AGP if in use */
   1710 	radeon_agp_resume(rdev);
   1711 	radeon_resume(rdev);
   1712 
   1713 	r = radeon_ib_ring_tests(rdev);
   1714 	if (r)
   1715 		DRM_ERROR("ib ring test failed (%d).\n", r);
   1716 
   1717 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
   1718 		/* do dpm late init */
   1719 		r = radeon_pm_late_init(rdev);
   1720 		if (r) {
   1721 			rdev->pm.dpm_enabled = false;
   1722 			DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
   1723 		}
   1724 	} else {
   1725 		/* resume old pm late */
   1726 		radeon_pm_resume(rdev);
   1727 	}
   1728 
   1729 	radeon_restore_bios_scratch_regs(rdev);
   1730 
   1731 	/* pin cursors */
   1732 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
   1733 		struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
   1734 
   1735 		if (radeon_crtc->cursor_bo) {
   1736 			struct radeon_bo *robj = gem_to_radeon_bo(radeon_crtc->cursor_bo);
   1737 			r = radeon_bo_reserve(robj, false);
   1738 			if (r == 0) {
   1739 				/* Only 27 bit offset for legacy cursor */
   1740 				r = radeon_bo_pin_restricted(robj,
   1741 							     RADEON_GEM_DOMAIN_VRAM,
   1742 							     ASIC_IS_AVIVO(rdev) ?
   1743 							     0 : 1 << 27,
   1744 							     &radeon_crtc->cursor_addr);
   1745 				if (r != 0)
   1746 					DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
   1747 				radeon_bo_unreserve(robj);
   1748 			}
   1749 		}
   1750 	}
   1751 
   1752 	/* init dig PHYs, disp eng pll */
   1753 	if (rdev->is_atom_bios) {
   1754 		radeon_atom_encoder_init(rdev);
   1755 		radeon_atom_disp_eng_pll_init(rdev);
   1756 		/* turn on the BL */
   1757 		if (rdev->mode_info.bl_encoder) {
   1758 			u8 bl_level = radeon_get_backlight_level(rdev,
   1759 								 rdev->mode_info.bl_encoder);
   1760 			radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
   1761 						   bl_level);
   1762 		}
   1763 	}
   1764 	/* reset hpd state */
   1765 	radeon_hpd_init(rdev);
   1766 	/* blat the mode back in */
   1767 	if (fbcon) {
   1768 		drm_helper_resume_force_mode(dev);
   1769 		/* turn on display hw */
   1770 		drm_modeset_lock_all(dev);
   1771 		list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
   1772 			drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
   1773 		}
   1774 		drm_modeset_unlock_all(dev);
   1775 	}
   1776 
   1777 	drm_kms_helper_poll_enable(dev);
   1778 
   1779 	/* set the power state here in case we are a PX system or headless */
   1780 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled)
   1781 		radeon_pm_compute_clocks(rdev);
   1782 
   1783 	if (fbcon) {
   1784 		radeon_fbdev_set_suspend(rdev, 0);
   1785 		console_unlock();
   1786 	}
   1787 
   1788 	return 0;
   1789 }
   1790 
   1791 /**
   1792  * radeon_gpu_reset - reset the asic
   1793  *
   1794  * @rdev: radeon device pointer
   1795  *
   1796  * Attempt the reset the GPU if it has hung (all asics).
   1797  * Returns 0 for success or an error on failure.
   1798  */
   1799 int radeon_gpu_reset(struct radeon_device *rdev)
   1800 {
   1801 	unsigned ring_sizes[RADEON_NUM_RINGS];
   1802 	uint32_t *ring_data[RADEON_NUM_RINGS];
   1803 
   1804 	bool saved = false;
   1805 
   1806 	int i, r;
   1807 	int resched;
   1808 
   1809 	down_write(&rdev->exclusive_lock);
   1810 
   1811 	if (!rdev->needs_reset) {
   1812 		up_write(&rdev->exclusive_lock);
   1813 		return 0;
   1814 	}
   1815 
   1816 	atomic_inc(&rdev->gpu_reset_counter);
   1817 
   1818 	radeon_save_bios_scratch_regs(rdev);
   1819 	/* block TTM */
   1820 	resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
   1821 	radeon_suspend(rdev);
   1822 	radeon_hpd_fini(rdev);
   1823 
   1824 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
   1825 		ring_sizes[i] = radeon_ring_backup(rdev, &rdev->ring[i],
   1826 						   &ring_data[i]);
   1827 		if (ring_sizes[i]) {
   1828 			saved = true;
   1829 			dev_info(rdev->dev, "Saved %d dwords of commands "
   1830 				 "on ring %d.\n", ring_sizes[i], i);
   1831 		}
   1832 	}
   1833 
   1834 	r = radeon_asic_reset(rdev);
   1835 	if (!r) {
   1836 		dev_info(rdev->dev, "GPU reset succeeded, trying to resume\n");
   1837 		radeon_resume(rdev);
   1838 	}
   1839 
   1840 	radeon_restore_bios_scratch_regs(rdev);
   1841 
   1842 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
   1843 		if (!r && ring_data[i]) {
   1844 			radeon_ring_restore(rdev, &rdev->ring[i],
   1845 					    ring_sizes[i], ring_data[i]);
   1846 		} else {
   1847 			radeon_fence_driver_force_completion(rdev, i);
   1848 			kfree(ring_data[i]);
   1849 		}
   1850 	}
   1851 
   1852 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
   1853 		/* do dpm late init */
   1854 		r = radeon_pm_late_init(rdev);
   1855 		if (r) {
   1856 			rdev->pm.dpm_enabled = false;
   1857 			DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
   1858 		}
   1859 	} else {
   1860 		/* resume old pm late */
   1861 		radeon_pm_resume(rdev);
   1862 	}
   1863 
   1864 	/* init dig PHYs, disp eng pll */
   1865 	if (rdev->is_atom_bios) {
   1866 		radeon_atom_encoder_init(rdev);
   1867 		radeon_atom_disp_eng_pll_init(rdev);
   1868 		/* turn on the BL */
   1869 		if (rdev->mode_info.bl_encoder) {
   1870 			u8 bl_level = radeon_get_backlight_level(rdev,
   1871 								 rdev->mode_info.bl_encoder);
   1872 			radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
   1873 						   bl_level);
   1874 		}
   1875 	}
   1876 	/* reset hpd state */
   1877 	radeon_hpd_init(rdev);
   1878 
   1879 	ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
   1880 
   1881 	rdev->in_reset = true;
   1882 	rdev->needs_reset = false;
   1883 
   1884 	downgrade_write(&rdev->exclusive_lock);
   1885 
   1886 	drm_helper_resume_force_mode(rdev->ddev);
   1887 
   1888 	/* set the power state here in case we are a PX system or headless */
   1889 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled)
   1890 		radeon_pm_compute_clocks(rdev);
   1891 
   1892 	if (!r) {
   1893 		r = radeon_ib_ring_tests(rdev);
   1894 		if (r && saved)
   1895 			r = -EAGAIN;
   1896 	} else {
   1897 		/* bad news, how to tell it to userspace ? */
   1898 		dev_info(rdev->dev, "GPU reset failed\n");
   1899 	}
   1900 
   1901 	rdev->needs_reset = r == -EAGAIN;
   1902 	rdev->in_reset = false;
   1903 
   1904 	up_read(&rdev->exclusive_lock);
   1905 	return r;
   1906 }
   1907 
   1908 
   1909 /*
   1910  * Debugfs
   1911  */
   1912 int radeon_debugfs_add_files(struct radeon_device *rdev,
   1913 			     struct drm_info_list *files,
   1914 			     unsigned nfiles)
   1915 {
   1916 	unsigned i;
   1917 
   1918 	for (i = 0; i < rdev->debugfs_count; i++) {
   1919 		if (rdev->debugfs[i].files == files) {
   1920 			/* Already registered */
   1921 			return 0;
   1922 		}
   1923 	}
   1924 
   1925 	i = rdev->debugfs_count + 1;
   1926 	if (i > RADEON_DEBUGFS_MAX_COMPONENTS) {
   1927 		DRM_ERROR("Reached maximum number of debugfs components.\n");
   1928 		DRM_ERROR("Report so we increase "
   1929 		          "RADEON_DEBUGFS_MAX_COMPONENTS.\n");
   1930 		return -EINVAL;
   1931 	}
   1932 	rdev->debugfs[rdev->debugfs_count].files = files;
   1933 	rdev->debugfs[rdev->debugfs_count].num_files = nfiles;
   1934 	rdev->debugfs_count = i;
   1935 #if defined(CONFIG_DEBUG_FS)
   1936 	drm_debugfs_create_files(files, nfiles,
   1937 				 rdev->ddev->control->debugfs_root,
   1938 				 rdev->ddev->control);
   1939 	drm_debugfs_create_files(files, nfiles,
   1940 				 rdev->ddev->primary->debugfs_root,
   1941 				 rdev->ddev->primary);
   1942 #endif
   1943 	return 0;
   1944 }
   1945 
   1946 static void radeon_debugfs_remove_files(struct radeon_device *rdev)
   1947 {
   1948 #if defined(CONFIG_DEBUG_FS)
   1949 	unsigned i;
   1950 
   1951 	for (i = 0; i < rdev->debugfs_count; i++) {
   1952 		drm_debugfs_remove_files(rdev->debugfs[i].files,
   1953 					 rdev->debugfs[i].num_files,
   1954 					 rdev->ddev->control);
   1955 		drm_debugfs_remove_files(rdev->debugfs[i].files,
   1956 					 rdev->debugfs[i].num_files,
   1957 					 rdev->ddev->primary);
   1958 	}
   1959 #endif
   1960 }
   1961 
   1962 #if defined(CONFIG_DEBUG_FS)
   1963 int radeon_debugfs_init(struct drm_minor *minor)
   1964 {
   1965 	return 0;
   1966 }
   1967 
   1968 void radeon_debugfs_cleanup(struct drm_minor *minor)
   1969 {
   1970 }
   1971 #endif
   1972