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