Home | History | Annotate | Line # | Download | only in pci
agp_i810.c revision 1.101
      1 /*	$NetBSD: agp_i810.c,v 1.101 2014/06/12 18:33:42 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 Doug Rabson
      5  * Copyright (c) 2000 Ruslan Ermilov
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  *
     29  *	$FreeBSD$
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: agp_i810.c,v 1.101 2014/06/12 18:33:42 riastradh Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/malloc.h>
     38 #include <sys/kernel.h>
     39 #include <sys/proc.h>
     40 #include <sys/device.h>
     41 #include <sys/conf.h>
     42 #include <sys/xcall.h>
     43 
     44 #include <dev/pci/pcivar.h>
     45 #include <dev/pci/pcireg.h>
     46 #include <dev/pci/pcidevs.h>
     47 #include <dev/pci/agpvar.h>
     48 #include <dev/pci/agpreg.h>
     49 #include <dev/pci/agp_i810var.h>
     50 
     51 #include <sys/agpio.h>
     52 
     53 #include <sys/bus.h>
     54 
     55 #include "agp_intel.h"
     56 
     57 struct agp_softc *agp_i810_sc = NULL;
     58 
     59 #define READ1(off)	bus_space_read_1(isc->bst, isc->bsh, off)
     60 #define READ4(off)	bus_space_read_4(isc->bst, isc->bsh, off)
     61 #define WRITE4(off,v)	bus_space_write_4(isc->bst, isc->bsh, off, v)
     62 
     63 #define CHIP_I810 0	/* i810/i815 */
     64 #define CHIP_I830 1	/* 830M/845G */
     65 #define CHIP_I855 2	/* 852GM/855GM/865G */
     66 #define CHIP_I915 3	/* 915G/915GM/945G/945GM/945GME */
     67 #define CHIP_I965 4	/* 965Q/965PM */
     68 #define CHIP_G33  5	/* G33/Q33/Q35 */
     69 #define CHIP_G4X  6	/* G45/Q45 */
     70 
     71 /* XXX hack, see below */
     72 static bus_addr_t agp_i810_vga_regbase;
     73 static bus_size_t agp_i810_vga_regsize;
     74 static bus_space_tag_t agp_i810_vga_bst;
     75 static bus_space_handle_t agp_i810_vga_bsh;
     76 
     77 static u_int32_t agp_i810_get_aperture(struct agp_softc *);
     78 static int agp_i810_set_aperture(struct agp_softc *, u_int32_t);
     79 static int agp_i810_bind_page(struct agp_softc *, off_t, bus_addr_t);
     80 static int agp_i810_unbind_page(struct agp_softc *, off_t);
     81 static void agp_i810_flush_tlb(struct agp_softc *);
     82 static int agp_i810_enable(struct agp_softc *, u_int32_t mode);
     83 static struct agp_memory *agp_i810_alloc_memory(struct agp_softc *, int,
     84 						vsize_t);
     85 static int agp_i810_free_memory(struct agp_softc *, struct agp_memory *);
     86 static int agp_i810_bind_memory(struct agp_softc *, struct agp_memory *,
     87 		off_t);
     88 static int agp_i810_bind_memory_dcache(struct agp_softc *, struct agp_memory *,
     89 		off_t);
     90 static int agp_i810_bind_memory_hwcursor(struct agp_softc *,
     91 		struct agp_memory *, off_t);
     92 static int agp_i810_unbind_memory(struct agp_softc *, struct agp_memory *);
     93 
     94 static bool agp_i810_resume(device_t, const pmf_qual_t *);
     95 static int agp_i810_init(struct agp_softc *);
     96 
     97 static int agp_i810_setup_chipset_flush_page(struct agp_softc *);
     98 static void agp_i810_teardown_chipset_flush_page(struct agp_softc *);
     99 static int agp_i810_init(struct agp_softc *);
    100 
    101 static struct agp_methods agp_i810_methods = {
    102 	agp_i810_get_aperture,
    103 	agp_i810_set_aperture,
    104 	agp_i810_bind_page,
    105 	agp_i810_unbind_page,
    106 	agp_i810_flush_tlb,
    107 	agp_i810_enable,
    108 	agp_i810_alloc_memory,
    109 	agp_i810_free_memory,
    110 	agp_i810_bind_memory,
    111 	agp_i810_unbind_memory,
    112 };
    113 
    114 int
    115 agp_i810_write_gtt_entry(struct agp_i810_softc *isc, off_t off, bus_addr_t v)
    116 {
    117 	u_int32_t pte;
    118 
    119 	/* Bits 11:4 (physical start address extension) should be zero. */
    120 	if ((v & 0xff0) != 0)
    121 		return EINVAL;
    122 
    123 	pte = (u_int32_t)v;
    124 	/*
    125 	 * We need to massage the pte if bus_addr_t is wider than 32 bits.
    126 	 * The compiler isn't smart enough, hence the casts to uintmax_t.
    127 	 */
    128 	if (sizeof(bus_addr_t) > sizeof(u_int32_t)) {
    129 		/* 965+ can do 36-bit addressing, add in the extra bits. */
    130 		if (isc->chiptype == CHIP_I965 ||
    131 		    isc->chiptype == CHIP_G33 ||
    132 		    isc->chiptype == CHIP_G4X) {
    133 			if (((uintmax_t)v >> 36) != 0)
    134 				return EINVAL;
    135 			pte |= (v >> 28) & 0xf0;
    136 		} else {
    137 			if (((uintmax_t)v >> 32) != 0)
    138 				return EINVAL;
    139 		}
    140 	}
    141 
    142 	bus_space_write_4(isc->gtt_bst, isc->gtt_bsh,
    143 	    4*(off >> AGP_PAGE_SHIFT), pte);
    144 
    145 	return 0;
    146 }
    147 
    148 void
    149 agp_i810_post_gtt_entry(struct agp_i810_softc *isc, off_t off)
    150 {
    151 
    152 	(void)bus_space_read_4(isc->gtt_bst, isc->gtt_bsh,
    153 	    4*(off >> AGP_PAGE_SHIFT));
    154 }
    155 
    156 static void
    157 agp_flush_cache_xc(void *a __unused, void *b __unused)
    158 {
    159 
    160 	agp_flush_cache();
    161 }
    162 
    163 void
    164 agp_i810_chipset_flush(struct agp_i810_softc *isc)
    165 {
    166 	unsigned int timo = 20000; /* * 50 us = 1 s */
    167 
    168 	switch (isc->chiptype) {
    169 	case CHIP_I810:
    170 		break;
    171 	case CHIP_I830:
    172 	case CHIP_I855:
    173 		/*
    174 		 * Flush all CPU caches.  If we're cold, we can't run
    175 		 * xcalls, but there should be only one CPU up, so
    176 		 * flushing only the local CPU's cache should suffice.
    177 		 *
    178 		 * XXX Come to think of it, do these chipsets appear in
    179 		 * any multi-CPU systems?
    180 		 */
    181 		if (cold)
    182 			agp_flush_cache();
    183 		else
    184 			xc_wait(xc_broadcast(0, &agp_flush_cache_xc,
    185 				NULL, NULL));
    186 		WRITE4(AGP_I830_HIC, READ4(AGP_I830_HIC) | __BIT(31));
    187 		while (ISSET(READ4(AGP_I830_HIC), __BIT(31))) {
    188 			if (timo-- == 0)
    189 				break;
    190 			DELAY(50);
    191 		}
    192 		break;
    193 	case CHIP_I915:
    194 	case CHIP_I965:
    195 	case CHIP_G33:
    196 	case CHIP_G4X:
    197 		bus_space_write_4(isc->flush_bst, isc->flush_bsh, 0, 1);
    198 		break;
    199 	}
    200 }
    201 
    202 /* XXXthorpej -- duplicated code (see arch/x86/pci/pchb.c) */
    203 static int
    204 agp_i810_vgamatch(const struct pci_attach_args *pa)
    205 {
    206 
    207 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
    208 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
    209 		return (0);
    210 
    211 	switch (PCI_PRODUCT(pa->pa_id)) {
    212 	case PCI_PRODUCT_INTEL_82810_GC:
    213 	case PCI_PRODUCT_INTEL_82810_DC100_GC:
    214 	case PCI_PRODUCT_INTEL_82810E_GC:
    215 	case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
    216 	case PCI_PRODUCT_INTEL_82830MP_IV:
    217 	case PCI_PRODUCT_INTEL_82845G_IGD:
    218 	case PCI_PRODUCT_INTEL_82855GM_IGD:
    219 	case PCI_PRODUCT_INTEL_82865_IGD:
    220 	case PCI_PRODUCT_INTEL_82915G_IGD:
    221 	case PCI_PRODUCT_INTEL_82915GM_IGD:
    222 	case PCI_PRODUCT_INTEL_82945P_IGD:
    223 	case PCI_PRODUCT_INTEL_82945GM_IGD:
    224 	case PCI_PRODUCT_INTEL_82945GM_IGD_1:
    225 	case PCI_PRODUCT_INTEL_82945GME_IGD:
    226 	case PCI_PRODUCT_INTEL_E7221_IGD:
    227 	case PCI_PRODUCT_INTEL_82965Q_IGD:
    228 	case PCI_PRODUCT_INTEL_82965Q_IGD_1:
    229 	case PCI_PRODUCT_INTEL_82965PM_IGD:
    230 	case PCI_PRODUCT_INTEL_82965PM_IGD_1:
    231 	case PCI_PRODUCT_INTEL_82G33_IGD:
    232 	case PCI_PRODUCT_INTEL_82G33_IGD_1:
    233 	case PCI_PRODUCT_INTEL_82965G_IGD:
    234 	case PCI_PRODUCT_INTEL_82965G_IGD_1:
    235 	case PCI_PRODUCT_INTEL_82965GME_IGD:
    236 	case PCI_PRODUCT_INTEL_82Q35_IGD:
    237 	case PCI_PRODUCT_INTEL_82Q35_IGD_1:
    238 	case PCI_PRODUCT_INTEL_82Q33_IGD:
    239 	case PCI_PRODUCT_INTEL_82Q33_IGD_1:
    240 	case PCI_PRODUCT_INTEL_82G35_IGD:
    241 	case PCI_PRODUCT_INTEL_82G35_IGD_1:
    242 	case PCI_PRODUCT_INTEL_82946GZ_IGD:
    243 	case PCI_PRODUCT_INTEL_82GM45_IGD:
    244 	case PCI_PRODUCT_INTEL_82GM45_IGD_1:
    245 	case PCI_PRODUCT_INTEL_82IGD_E_IGD:
    246 	case PCI_PRODUCT_INTEL_82Q45_IGD:
    247 	case PCI_PRODUCT_INTEL_82G45_IGD:
    248 	case PCI_PRODUCT_INTEL_82G41_IGD:
    249 	case PCI_PRODUCT_INTEL_82B43_IGD:
    250 	case PCI_PRODUCT_INTEL_IRONLAKE_D_IGD:
    251 	case PCI_PRODUCT_INTEL_IRONLAKE_M_IGD:
    252 	case PCI_PRODUCT_INTEL_PINEVIEW_IGD:
    253 	case PCI_PRODUCT_INTEL_PINEVIEW_M_IGD:
    254 		return (1);
    255 	}
    256 
    257 	return (0);
    258 }
    259 
    260 static int
    261 agp_i965_map_aperture(struct pci_attach_args *pa, struct agp_softc *sc, int reg)
    262 {
    263         /*
    264          * Find the aperture. Don't map it (yet), this would
    265          * eat KVA.
    266          */
    267         if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg,
    268             PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_64BIT, &sc->as_apaddr, &sc->as_apsize,
    269             &sc->as_apflags) != 0)
    270                 return ENXIO;
    271 
    272         sc->as_apt = pa->pa_memt;
    273 
    274         return 0;
    275 }
    276 
    277 int
    278 agp_i810_attach(device_t parent, device_t self, void *aux)
    279 {
    280 	struct agp_softc *sc = device_private(self);
    281 	struct agp_i810_softc *isc;
    282 	int apbase, mmadr_bar, gtt_bar;
    283 	int mmadr_type, mmadr_flags;
    284 	bus_addr_t mmadr;
    285 	bus_size_t mmadr_size, gtt_off;
    286 	int error;
    287 
    288 	isc = malloc(sizeof *isc, M_AGP, M_NOWAIT|M_ZERO);
    289 	if (isc == NULL) {
    290 		aprint_error(": can't allocate chipset-specific softc\n");
    291 		error = ENOMEM;
    292 		goto fail0;
    293 	}
    294 	sc->as_chipc = isc;
    295 	sc->as_methods = &agp_i810_methods;
    296 
    297 	if (pci_find_device(&isc->vga_pa, agp_i810_vgamatch) == 0) {
    298 #if NAGP_INTEL > 0
    299 		const struct pci_attach_args *pa = aux;
    300 
    301 		switch (PCI_PRODUCT(pa->pa_id)) {
    302 		case PCI_PRODUCT_INTEL_82840_HB:
    303 		case PCI_PRODUCT_INTEL_82865_HB:
    304 		case PCI_PRODUCT_INTEL_82845G_DRAM:
    305 		case PCI_PRODUCT_INTEL_82815_FULL_HUB:
    306 		case PCI_PRODUCT_INTEL_82855GM_MCH:
    307 			free(isc, M_AGP);
    308 			return agp_intel_attach(parent, self, aux);
    309 		}
    310 #endif
    311 		aprint_error(": can't find internal VGA"
    312 		    " config space\n");
    313 		error = ENOENT;
    314 		goto fail1;
    315 	}
    316 
    317 	/* XXXfvdl */
    318 	sc->as_dmat = isc->vga_pa.pa_dmat;
    319 
    320 	switch (PCI_PRODUCT(isc->vga_pa.pa_id)) {
    321 	case PCI_PRODUCT_INTEL_82810_GC:
    322 	case PCI_PRODUCT_INTEL_82810_DC100_GC:
    323 	case PCI_PRODUCT_INTEL_82810E_GC:
    324 	case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
    325 		isc->chiptype = CHIP_I810;
    326 		aprint_normal(": i810-family chipset\n");
    327 		break;
    328 	case PCI_PRODUCT_INTEL_82830MP_IV:
    329 	case PCI_PRODUCT_INTEL_82845G_IGD:
    330 		isc->chiptype = CHIP_I830;
    331 		aprint_normal(": i830-family chipset\n");
    332 		break;
    333 	case PCI_PRODUCT_INTEL_82855GM_IGD:
    334 	case PCI_PRODUCT_INTEL_82865_IGD:
    335 		isc->chiptype = CHIP_I855;
    336 		aprint_normal(": i855-family chipset\n");
    337 		break;
    338 	case PCI_PRODUCT_INTEL_82915G_IGD:
    339 	case PCI_PRODUCT_INTEL_82915GM_IGD:
    340 	case PCI_PRODUCT_INTEL_82945P_IGD:
    341 	case PCI_PRODUCT_INTEL_82945GM_IGD:
    342 	case PCI_PRODUCT_INTEL_82945GM_IGD_1:
    343 	case PCI_PRODUCT_INTEL_82945GME_IGD:
    344 	case PCI_PRODUCT_INTEL_E7221_IGD:
    345 	case PCI_PRODUCT_INTEL_PINEVIEW_IGD:
    346 	case PCI_PRODUCT_INTEL_PINEVIEW_M_IGD:
    347 		isc->chiptype = CHIP_I915;
    348 		aprint_normal(": i915-family chipset\n");
    349 		break;
    350 	case PCI_PRODUCT_INTEL_82965Q_IGD:
    351 	case PCI_PRODUCT_INTEL_82965Q_IGD_1:
    352 	case PCI_PRODUCT_INTEL_82965PM_IGD:
    353 	case PCI_PRODUCT_INTEL_82965PM_IGD_1:
    354 	case PCI_PRODUCT_INTEL_82965G_IGD:
    355 	case PCI_PRODUCT_INTEL_82965G_IGD_1:
    356 	case PCI_PRODUCT_INTEL_82965GME_IGD:
    357 	case PCI_PRODUCT_INTEL_82946GZ_IGD:
    358 	case PCI_PRODUCT_INTEL_82G35_IGD:
    359 	case PCI_PRODUCT_INTEL_82G35_IGD_1:
    360 		isc->chiptype = CHIP_I965;
    361 		aprint_normal(": i965-family chipset\n");
    362 		break;
    363 	case PCI_PRODUCT_INTEL_82Q35_IGD:
    364 	case PCI_PRODUCT_INTEL_82Q35_IGD_1:
    365 	case PCI_PRODUCT_INTEL_82G33_IGD:
    366 	case PCI_PRODUCT_INTEL_82G33_IGD_1:
    367 	case PCI_PRODUCT_INTEL_82Q33_IGD:
    368 	case PCI_PRODUCT_INTEL_82Q33_IGD_1:
    369 		isc->chiptype = CHIP_G33;
    370 		aprint_normal(": G33-family chipset\n");
    371 		break;
    372 	case PCI_PRODUCT_INTEL_82GM45_IGD:
    373 	case PCI_PRODUCT_INTEL_82GM45_IGD_1:
    374 	case PCI_PRODUCT_INTEL_82IGD_E_IGD:
    375 	case PCI_PRODUCT_INTEL_82Q45_IGD:
    376 	case PCI_PRODUCT_INTEL_82G45_IGD:
    377 	case PCI_PRODUCT_INTEL_82G41_IGD:
    378 	case PCI_PRODUCT_INTEL_82B43_IGD:
    379 	case PCI_PRODUCT_INTEL_IRONLAKE_D_IGD:
    380 	case PCI_PRODUCT_INTEL_IRONLAKE_M_IGD:
    381 		isc->chiptype = CHIP_G4X;
    382 		aprint_normal(": G4X-family chipset\n");
    383 		break;
    384 	}
    385 	aprint_naive("\n");
    386 
    387 	mmadr_type = PCI_MAPREG_TYPE_MEM;
    388 	switch (isc->chiptype) {
    389 	case CHIP_I915:
    390 	case CHIP_G33:
    391 		apbase = AGP_I915_GMADR;
    392 		mmadr_bar = AGP_I915_MMADR;
    393 		isc->size = 2*1024*1024;
    394 		gtt_bar = AGP_I915_GTTADR;
    395 		gtt_off = ~(bus_size_t)0; /* XXXGCC */
    396 		break;
    397 	case CHIP_I965:
    398 		apbase = AGP_I965_GMADR;
    399 		mmadr_bar = AGP_I965_MMADR;
    400 		mmadr_type |= PCI_MAPREG_MEM_TYPE_64BIT;
    401 		isc->size = 2*1024*1024;
    402 		gtt_bar = 0;
    403 		gtt_off = AGP_I965_GTT;
    404 		break;
    405 	case CHIP_G4X:
    406 		apbase = AGP_I965_GMADR;
    407 		mmadr_bar = AGP_I965_MMADR;
    408 		mmadr_type |= PCI_MAPREG_MEM_TYPE_64BIT;
    409 		isc->size = 2*1024*1024;
    410 		gtt_bar = 0;
    411 		gtt_off = AGP_G4X_GTT;
    412 		break;
    413 	default:
    414 		apbase = AGP_I810_GMADR;
    415 		mmadr_bar = AGP_I810_MMADR;
    416 		isc->size = 512*1024;
    417 		gtt_bar = 0;
    418 		gtt_off = AGP_I810_GTT;
    419 		break;
    420 	}
    421 
    422 	/* Map (or, rather, find the address and size of) the aperture.  */
    423 	if (isc->chiptype == CHIP_I965 || isc->chiptype == CHIP_G4X)
    424 		error = agp_i965_map_aperture(&isc->vga_pa, sc, apbase);
    425 	else
    426 		error = agp_map_aperture(&isc->vga_pa, sc, apbase);
    427 	if (error) {
    428 		aprint_error_dev(self, "can't map aperture: %d\n", error);
    429 		goto fail1;
    430 	}
    431 
    432 	/* Map the memory-mapped I/O registers, or the non-GTT part.  */
    433 	if (pci_mapreg_info(isc->vga_pa.pa_pc, isc->vga_pa.pa_tag, mmadr_bar,
    434 		mmadr_type, &mmadr, &mmadr_size, &mmadr_flags)) {
    435 		aprint_error_dev(self, "can't find MMIO registers\n");
    436 		error = ENXIO;
    437 		goto fail1;
    438 	}
    439 	if (mmadr_size < isc->size) {
    440 		aprint_error_dev(self, "MMIO registers too small"
    441 		    ": %"PRIuMAX" < %"PRIuMAX"\n",
    442 		    (uintmax_t)mmadr_size, (uintmax_t)isc->size);
    443 		error = ENXIO;
    444 		goto fail1;
    445 	}
    446 	isc->bst = isc->vga_pa.pa_memt;
    447 	error = bus_space_map(isc->bst, mmadr, isc->size, mmadr_flags,
    448 	    &isc->bsh);
    449 	if (error) {
    450 		aprint_error_dev(self, "can't map MMIO registers: %d\n",
    451 		    error);
    452 		error = ENXIO;
    453 		goto fail1;
    454 	}
    455 
    456 	/* Set up a chipset flush page if necessary.  */
    457 	switch (isc->chiptype) {
    458 	case CHIP_I915:
    459 	case CHIP_I965:
    460 	case CHIP_G33:
    461 	case CHIP_G4X:
    462 		error = agp_i810_setup_chipset_flush_page(sc);
    463 		if (error) {
    464 			aprint_error_dev(self,
    465 			    "can't set up chipset flush page: %d\n", error);
    466 			goto fail2;
    467 		}
    468 		break;
    469 	}
    470 
    471 	/*
    472 	 * XXX horrible hack to allow drm code to use our mapping
    473 	 * of VGA chip registers
    474 	 */
    475 	agp_i810_vga_regbase = mmadr;
    476 	agp_i810_vga_regsize = isc->size;
    477 	agp_i810_vga_bst = isc->bst;
    478 	agp_i810_vga_bsh = isc->bsh;
    479 
    480 	/* Initialize the chipset.  */
    481 	error = agp_i810_init(sc);
    482 	if (error)
    483 		goto fail3;
    484 
    485 	/* Map the GTT, from either part of the MMIO region or its own BAR.  */
    486 	if (gtt_bar == 0) {
    487 		isc->gtt_bst = isc->bst;
    488 		if ((mmadr_size - gtt_off) < isc->gtt_size) {
    489 			aprint_error_dev(self, "GTTMMADR too small for GTT"
    490 			    ": (%"PRIxMAX" - %"PRIxMAX") < %"PRIxMAX"\n",
    491 			    (uintmax_t)mmadr_size,
    492 			    (uintmax_t)gtt_off,
    493 			    (uintmax_t)isc->gtt_size);
    494 			error = ENXIO;
    495 			goto fail4;
    496 		}
    497 		/*
    498 		 * Map the GTT separately if we can, so that we can map
    499 		 * it prefetchable, but in early models, there are MMIO
    500 		 * registers before and after the GTT, so we can only
    501 		 * take a subregion.
    502 		 */
    503 		if (isc->size < gtt_off)
    504 			error = bus_space_map(isc->gtt_bst, (mmadr + gtt_off),
    505 			    isc->gtt_size, mmadr_flags, &isc->gtt_bsh);
    506 		else
    507 			error = bus_space_subregion(isc->bst, isc->bsh,
    508 			    gtt_off, isc->gtt_size, &isc->gtt_bsh);
    509 		if (error) {
    510 			aprint_error_dev(self, "can't map GTT: %d\n", error);
    511 			error = ENXIO;
    512 			goto fail4;
    513 		}
    514 	} else {
    515 		bus_size_t gtt_bar_size;
    516 		/*
    517 		 * All chipsets with a separate BAR for the GTT, namely
    518 		 * the i915 and G33 families, have 32-bit GTT BARs.
    519 		 *
    520 		 * XXX [citation needed]
    521 		 */
    522 		if (pci_mapreg_map(&isc->vga_pa, gtt_bar, PCI_MAPREG_TYPE_MEM,
    523 			0,
    524 			&isc->gtt_bst, &isc->gtt_bsh, NULL, &gtt_bar_size)) {
    525 			aprint_error_dev(self, "can't map GTT\n");
    526 			error = ENXIO;
    527 			goto fail4;
    528 		}
    529 		if (gtt_bar_size != isc->gtt_size) {
    530 			aprint_error_dev(self,
    531 			    "BAR size %"PRIxMAX
    532 			    " mismatches detected GTT size %"PRIxMAX
    533 			    "; trusting BAR\n",
    534 			    (uintmax_t)gtt_bar_size,
    535 			    (uintmax_t)isc->gtt_size);
    536 			isc->gtt_size = gtt_bar_size;
    537 		}
    538 	}
    539 
    540 	/* Power management.  (XXX Nothing to save on suspend?  Fishy...)  */
    541 	if (!pmf_device_register(self, NULL, agp_i810_resume))
    542 		aprint_error_dev(self, "can't establish power handler\n");
    543 
    544 	/* Match the generic AGP code's autoconf output format.  */
    545 	aprint_normal("%s", device_xname(self));
    546 
    547 	/* Success!  */
    548 	return 0;
    549 
    550 fail5: __unused
    551 	pmf_device_deregister(self);
    552 	if ((gtt_bar != 0) || (isc->size < gtt_off))
    553 		bus_space_unmap(isc->gtt_bst, isc->gtt_bsh, isc->gtt_size);
    554 	isc->gtt_size = 0;
    555 fail4:
    556 #if notyet
    557 	agp_i810_fini(sc);
    558 #endif
    559 fail3:	switch (isc->chiptype) {
    560 	case CHIP_I915:
    561 	case CHIP_I965:
    562 	case CHIP_G33:
    563 	case CHIP_G4X:
    564 		agp_i810_teardown_chipset_flush_page(sc);
    565 		break;
    566 	}
    567 fail2:	bus_space_unmap(isc->bst, isc->bsh, isc->size);
    568 	isc->size = 0;
    569 fail1:	free(isc, M_AGP);
    570 	sc->as_chipc = NULL;
    571 fail0:	agp_generic_detach(sc);
    572 	KASSERT(error);
    573 	return error;
    574 }
    575 
    576 static int
    577 agp_i810_setup_chipset_flush_page(struct agp_softc *sc)
    578 {
    579 	struct agp_i810_softc *const isc = sc->as_chipc;
    580 	const pci_chipset_tag_t pc = sc->as_pc;
    581 	const pcitag_t tag = sc->as_tag;
    582 	pcireg_t lo, hi;
    583 	bus_addr_t addr, minaddr, maxaddr;
    584 	int error;
    585 
    586 	/* We always use memory-mapped I/O.  */
    587 	isc->flush_bst = isc->vga_pa.pa_memt;
    588 
    589 	/* No page allocated yet.  */
    590 	isc->flush_addr = 0;
    591 
    592 	/* Read the PCI config register: 4-byte on gen3, 8-byte on gen>=4.  */
    593 	if (isc->chiptype == CHIP_I915) {
    594 		addr = pci_conf_read(pc, tag, AGP_I915_IFPADDR);
    595 		minaddr = PAGE_SIZE;	/* XXX PCIBIOS_MIN_MEM?  */
    596 		maxaddr = UINT32_MAX;
    597 	} else {
    598 		hi = pci_conf_read(pc, tag, AGP_I965_IFPADDR+4);
    599 		lo = pci_conf_read(pc, tag, AGP_I965_IFPADDR);
    600 		/*
    601 		 * Convert to uint64_t, rather than bus_addr_t which
    602 		 * may be 32-bit, to avoid undefined behaviour with a
    603 		 * too-wide shift.  Since the BIOS doesn't know whether
    604 		 * the OS will run 64-bit or with PAE, it ought to
    605 		 * configure at most a 32-bit physical address, so
    606 		 * let's print a warning in case that happens.
    607 		 */
    608 		addr = ((uint64_t)hi << 32) | lo;
    609 		if (hi) {
    610 			aprint_error_dev(sc->as_dev,
    611 			    "BIOS configured >32-bit flush page address"
    612 			    ": %"PRIx64"\n", ((uint64_t)hi << 32) | lo);
    613 #if __i386__ && !PAE
    614 			return EIO;
    615 #endif
    616 		}
    617 		minaddr = PAGE_SIZE;	/* XXX PCIBIOS_MIN_MEM?  */
    618 		maxaddr = MIN(UINT64_MAX, ~(bus_addr_t)0);
    619 	}
    620 
    621 	/* Allocate or map a pre-allocated a page for it.  */
    622 	if (ISSET(addr, 1)) {
    623 		/* BIOS allocated it for us.  Use that.  */
    624 		error = bus_space_map(isc->flush_bst, addr & ~1, PAGE_SIZE, 0,
    625 		    &isc->flush_bsh);
    626 		if (error)
    627 			return error;
    628 	} else {
    629 		/* None allocated.  Allocate one.  */
    630 		error = bus_space_alloc(isc->flush_bst, minaddr, maxaddr,
    631 		    PAGE_SIZE, PAGE_SIZE, 0, 0,
    632 		    &isc->flush_addr, &isc->flush_bsh);
    633 		if (error)
    634 			return error;
    635 		KASSERT(isc->flush_addr != 0);
    636 		/* Write it into the PCI config register.  */
    637 		addr = isc->flush_addr | 1;
    638 		if (isc->chiptype == CHIP_I915) {
    639 			pci_conf_write(pc, tag, AGP_I915_IFPADDR, addr);
    640 		} else {
    641 			hi = __SHIFTOUT(addr, __BITS(63, 32));
    642 			lo = __SHIFTOUT(addr, __BITS(31, 0));
    643 			pci_conf_write(pc, tag, AGP_I965_IFPADDR+4, hi);
    644 			pci_conf_write(pc, tag, AGP_I965_IFPADDR, lo);
    645 		}
    646 	}
    647 
    648 	/* Success!  */
    649 	return 0;
    650 }
    651 
    652 static void
    653 agp_i810_teardown_chipset_flush_page(struct agp_softc *sc)
    654 {
    655 	struct agp_i810_softc *const isc = sc->as_chipc;
    656 
    657 	if (isc->flush_addr) {
    658 		/* If we allocated a page, clear it.  */
    659 		if (isc->chiptype == CHIP_I915) {
    660 			pci_conf_write(sc->as_pc, sc->as_tag, AGP_I915_IFPADDR,
    661 			    0);
    662 		} else {
    663 			pci_conf_write(sc->as_pc, sc->as_tag,
    664 			    AGP_I965_IFPADDR, 0);
    665 			pci_conf_write(sc->as_pc, sc->as_tag,
    666 			    AGP_I965_IFPADDR + 4, 0);
    667 		}
    668 		isc->flush_addr = 0;
    669 		bus_space_free(isc->flush_bst, isc->flush_bsh,
    670 		    PAGE_SIZE);
    671 	} else {
    672 		/* Otherwise, just unmap the pre-allocated page.  */
    673 		bus_space_unmap(isc->flush_bst, isc->flush_bsh, PAGE_SIZE);
    674 	}
    675 }
    676 
    677 /*
    678  * XXX horrible hack to allow drm code to use our mapping
    679  * of VGA chip registers
    680  */
    681 int
    682 agp_i810_borrow(bus_addr_t base, bus_size_t size, bus_space_handle_t *hdlp)
    683 {
    684 
    685 	if (agp_i810_vga_regbase == 0)
    686 		return 0;
    687 	if (base < agp_i810_vga_regbase)
    688 		return 0;
    689 	if (agp_i810_vga_regsize < size)
    690 		return 0;
    691 	if ((base - agp_i810_vga_regbase) > (agp_i810_vga_regsize - size))
    692 		return 0;
    693 	if (bus_space_subregion(agp_i810_vga_bst, agp_i810_vga_bsh,
    694 		(base - agp_i810_vga_regbase), (agp_i810_vga_regsize - size),
    695 		hdlp))
    696 		return 0;
    697 	return 1;
    698 }
    699 
    700 static int
    701 agp_i810_init(struct agp_softc *sc)
    702 {
    703 	struct agp_i810_softc *isc;
    704 	int error;
    705 
    706 	isc = sc->as_chipc;
    707 
    708 	if (isc->chiptype == CHIP_I810) {
    709 		struct agp_gatt *gatt;
    710 		void *virtual;
    711 		int dummyseg;
    712 
    713 		/* Some i810s have on-chip memory called dcache */
    714 		if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
    715 			isc->dcache_size = 4 * 1024 * 1024;
    716 		else
    717 			isc->dcache_size = 0;
    718 
    719 		/* According to the specs the gatt on the i810 must be 64k */
    720 		isc->gtt_size = 64 * 1024;
    721 		gatt = malloc(sizeof(*gatt), M_AGP, M_NOWAIT);
    722 		if (gatt == NULL) {
    723 			aprint_error_dev(sc->as_dev,
    724 			    "can't malloc GATT record\n");
    725 			error = ENOMEM;
    726 			goto fail0;
    727 		}
    728 		gatt->ag_entries = isc->gtt_size / sizeof(uint32_t);
    729 		error = agp_alloc_dmamem(sc->as_dmat, isc->gtt_size,
    730 		    0, &gatt->ag_dmamap, &virtual, &gatt->ag_physical,
    731 		    &gatt->ag_dmaseg, 1, &dummyseg);
    732 		if (error) {
    733 			aprint_error_dev(sc->as_dev,
    734 			    "can't allocate memory for GTT: %d\n", error);
    735 			free(gatt, M_AGP);
    736 			goto fail0;
    737 		}
    738 
    739 		gatt->ag_virtual = (uint32_t *)virtual;
    740 		gatt->ag_size = gatt->ag_entries * sizeof(uint32_t);
    741 		memset(gatt->ag_virtual, 0, gatt->ag_size);
    742 		agp_flush_cache();
    743 
    744 		/* Install the GATT. */
    745 		isc->pgtblctl = gatt->ag_physical | 1;
    746 		WRITE4(AGP_I810_PGTBL_CTL, isc->pgtblctl);
    747 		isc->gatt = gatt;
    748 	} else if (isc->chiptype == CHIP_I830) {
    749 		/* The i830 automatically initializes the 128k gatt on boot. */
    750 		/* XXX [citation needed] */
    751 		pcireg_t reg;
    752 		u_int16_t gcc1;
    753 
    754 		isc->gtt_size = 128 * 1024;
    755 
    756 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
    757 		gcc1 = (u_int16_t)(reg >> 16);
    758 		switch (gcc1 & AGP_I830_GCC1_GMS) {
    759 		case AGP_I830_GCC1_GMS_STOLEN_512:
    760 			isc->stolen = (512 - 132) * 1024 / 4096;
    761 			break;
    762 		case AGP_I830_GCC1_GMS_STOLEN_1024:
    763 			isc->stolen = (1024 - 132) * 1024 / 4096;
    764 			break;
    765 		case AGP_I830_GCC1_GMS_STOLEN_8192:
    766 			isc->stolen = (8192 - 132) * 1024 / 4096;
    767 			break;
    768 		default:
    769 			isc->stolen = 0;
    770 			aprint_error_dev(sc->as_dev,
    771 			    "unknown memory configuration, disabling\n");
    772 			error = ENXIO;
    773 			goto fail0;
    774 		}
    775 
    776 		if (isc->stolen > 0) {
    777 			aprint_normal_dev(sc->as_dev,
    778 			    "detected %dk stolen memory\n",
    779 			    isc->stolen * 4);
    780 		}
    781 
    782 		/* GATT address is already in there, make sure it's enabled */
    783 		isc->pgtblctl = READ4(AGP_I810_PGTBL_CTL);
    784 		isc->pgtblctl |= 1;
    785 		WRITE4(AGP_I810_PGTBL_CTL, isc->pgtblctl);
    786 	} else if (isc->chiptype == CHIP_I855 || isc->chiptype == CHIP_I915 ||
    787 		   isc->chiptype == CHIP_I965 || isc->chiptype == CHIP_G33 ||
    788 		   isc->chiptype == CHIP_G4X) {
    789 		pcireg_t reg;
    790 		u_int32_t gtt_size, stolen;	/* XXX kilobytes */
    791 		u_int16_t gcc1;
    792 
    793 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I855_GCC1);
    794 		gcc1 = (u_int16_t)(reg >> 16);
    795 
    796 		isc->pgtblctl = READ4(AGP_I810_PGTBL_CTL);
    797 
    798 		/* Stolen memory is set up at the beginning of the aperture by
    799                  * the BIOS, consisting of the GATT followed by 4kb for the
    800 		 * BIOS display.
    801                  */
    802                 switch (isc->chiptype) {
    803 		case CHIP_I855:
    804 			gtt_size = 128;
    805 			break;
    806                 case CHIP_I915:
    807 			gtt_size = 256;
    808 			break;
    809 		case CHIP_I965:
    810 			switch (isc->pgtblctl & AGP_I810_PGTBL_SIZE_MASK) {
    811 			case AGP_I810_PGTBL_SIZE_128KB:
    812 			case AGP_I810_PGTBL_SIZE_512KB:
    813 				gtt_size = 512;
    814 				break;
    815 			case AGP_I965_PGTBL_SIZE_1MB:
    816 				gtt_size = 1024;
    817 				break;
    818 			case AGP_I965_PGTBL_SIZE_2MB:
    819 				gtt_size = 2048;
    820 				break;
    821 			case AGP_I965_PGTBL_SIZE_1_5MB:
    822 				gtt_size = 1024 + 512;
    823 				break;
    824 			default:
    825 				aprint_error_dev(sc->as_dev,
    826 				    "bad PGTBL size\n");
    827 				error = ENXIO;
    828 				goto fail0;
    829 			}
    830 			break;
    831 		case CHIP_G33:
    832 			switch (gcc1 & AGP_G33_PGTBL_SIZE_MASK) {
    833 			case AGP_G33_PGTBL_SIZE_1M:
    834 				gtt_size = 1024;
    835 				break;
    836 			case AGP_G33_PGTBL_SIZE_2M:
    837 				gtt_size = 2048;
    838 				break;
    839 			default:
    840 				aprint_error_dev(sc->as_dev,
    841 				    "bad PGTBL size\n");
    842 				error = ENXIO;
    843 				goto fail0;
    844 			}
    845 			break;
    846 		case CHIP_G4X:
    847 			gtt_size = 256;
    848 			break;
    849 		default:
    850 			panic("impossible chiptype %d", isc->chiptype);
    851 		}
    852 
    853 		/*
    854 		 * XXX If I'm reading the datasheets right, this stolen
    855 		 * memory detection logic is totally wrong.
    856 		 */
    857 		switch (gcc1 & AGP_I855_GCC1_GMS) {
    858 		case AGP_I855_GCC1_GMS_STOLEN_1M:
    859 			stolen = 1024;
    860 			break;
    861 		case AGP_I855_GCC1_GMS_STOLEN_4M:
    862 			stolen = 4 * 1024;
    863 			break;
    864 		case AGP_I855_GCC1_GMS_STOLEN_8M:
    865 			stolen = 8 * 1024;
    866 			break;
    867 		case AGP_I855_GCC1_GMS_STOLEN_16M:
    868 			stolen = 16 * 1024;
    869 			break;
    870 		case AGP_I855_GCC1_GMS_STOLEN_32M:
    871 			stolen = 32 * 1024;
    872 			break;
    873 		case AGP_I915_GCC1_GMS_STOLEN_48M:
    874 			stolen = 48 * 1024;
    875 			break;
    876 		case AGP_I915_GCC1_GMS_STOLEN_64M:
    877 			stolen = 64 * 1024;
    878 			break;
    879 		case AGP_G33_GCC1_GMS_STOLEN_128M:
    880 			stolen = 128 * 1024;
    881 			break;
    882 		case AGP_G33_GCC1_GMS_STOLEN_256M:
    883 			stolen = 256 * 1024;
    884 			break;
    885 		case AGP_G4X_GCC1_GMS_STOLEN_96M:
    886 			stolen = 96 * 1024;
    887 			break;
    888 		case AGP_G4X_GCC1_GMS_STOLEN_160M:
    889 			stolen = 160 * 1024;
    890 			break;
    891 		case AGP_G4X_GCC1_GMS_STOLEN_224M:
    892 			stolen = 224 * 1024;
    893 			break;
    894 		case AGP_G4X_GCC1_GMS_STOLEN_352M:
    895 			stolen = 352 * 1024;
    896 			break;
    897 		default:
    898 			aprint_error_dev(sc->as_dev,
    899 			    "unknown memory configuration, disabling\n");
    900 			error = ENXIO;
    901 			goto fail0;
    902 		}
    903 
    904 		switch (gcc1 & AGP_I855_GCC1_GMS) {
    905 		case AGP_I915_GCC1_GMS_STOLEN_48M:
    906 		case AGP_I915_GCC1_GMS_STOLEN_64M:
    907 			if (isc->chiptype != CHIP_I915 &&
    908 			    isc->chiptype != CHIP_I965 &&
    909 			    isc->chiptype != CHIP_G33 &&
    910 			    isc->chiptype != CHIP_G4X)
    911 				stolen = 0;
    912 			break;
    913 		case AGP_G33_GCC1_GMS_STOLEN_128M:
    914 		case AGP_G33_GCC1_GMS_STOLEN_256M:
    915 			if (isc->chiptype != CHIP_I965 &&
    916 			    isc->chiptype != CHIP_G33 &&
    917 			    isc->chiptype != CHIP_G4X)
    918 				stolen = 0;
    919 			break;
    920 		case AGP_G4X_GCC1_GMS_STOLEN_96M:
    921 		case AGP_G4X_GCC1_GMS_STOLEN_160M:
    922 		case AGP_G4X_GCC1_GMS_STOLEN_224M:
    923 		case AGP_G4X_GCC1_GMS_STOLEN_352M:
    924 			if (isc->chiptype != CHIP_I965 &&
    925 			    isc->chiptype != CHIP_G4X)
    926 				stolen = 0;
    927 			break;
    928 		}
    929 
    930 		isc->gtt_size = gtt_size * 1024;
    931 
    932 		/* BIOS space */
    933 		/* XXX [citation needed] */
    934 		gtt_size += 4;
    935 
    936 		/* XXX [citation needed] for this subtraction */
    937 		isc->stolen = (stolen - gtt_size) * 1024 / 4096;
    938 
    939 		if (isc->stolen > 0) {
    940 			aprint_normal_dev(sc->as_dev,
    941 			    "detected %dk stolen memory\n",
    942 			    isc->stolen * 4);
    943 		}
    944 
    945 		/* GATT address is already in there, make sure it's enabled */
    946 		isc->pgtblctl |= 1;
    947 		WRITE4(AGP_I810_PGTBL_CTL, isc->pgtblctl);
    948 	}
    949 
    950 	/*
    951 	 * Make sure the chipset can see everything.
    952 	 */
    953 	agp_flush_cache();
    954 
    955 	/*
    956 	 * Publish what we found for kludgey drivers (I'm looking at
    957 	 * you, drm).
    958 	 */
    959 	if (agp_i810_sc == NULL)
    960 		agp_i810_sc = sc;
    961 	else
    962 		aprint_error_dev(sc->as_dev, "agp already attached\n");
    963 
    964 	/* Success!  */
    965 	return 0;
    966 
    967 fail0:	KASSERT(error);
    968 	return error;
    969 }
    970 
    971 #if 0
    972 static int
    973 agp_i810_detach(struct agp_softc *sc)
    974 {
    975 	int error;
    976 	struct agp_i810_softc *isc = sc->as_chipc;
    977 
    978 	error = agp_generic_detach(sc);
    979 	if (error)
    980 		return error;
    981 
    982 	switch (isc->chiptype) {
    983 	case CHIP_I915:
    984 	case CHIP_I965:
    985 	case CHIP_G33:
    986 	case CHIP_G4X:
    987 		agp_i810_teardown_chipset_flush_page(sc);
    988 		break;
    989 	}
    990 
    991 	/* Clear the GATT base. */
    992 	if (sc->chiptype == CHIP_I810) {
    993 		WRITE4(AGP_I810_PGTBL_CTL, 0);
    994 	} else {
    995 		unsigned int pgtblctl;
    996 		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
    997 		pgtblctl &= ~1;
    998 		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
    999 	}
   1000 
   1001 	if (sc->chiptype == CHIP_I810) {
   1002 		agp_free_dmamem(sc->as_dmat, gatt->ag_size, gatt->ag_dmamap,
   1003 		    (void *)gatt->ag_virtual, &gatt->ag_dmaseg, 1);
   1004 		free(isc->gatt, M_AGP);
   1005 	}
   1006 
   1007 	return 0;
   1008 }
   1009 #endif
   1010 
   1011 static u_int32_t
   1012 agp_i810_get_aperture(struct agp_softc *sc)
   1013 {
   1014 	struct agp_i810_softc *isc = sc->as_chipc;
   1015 	pcireg_t reg;
   1016 	u_int32_t size;
   1017 	u_int16_t miscc, gcc1;
   1018 
   1019 	size = 0;
   1020 
   1021 	switch (isc->chiptype) {
   1022 	case CHIP_I810:
   1023 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
   1024 		miscc = (u_int16_t)(reg >> 16);
   1025 		if ((miscc & AGP_I810_MISCC_WINSIZE) ==
   1026 		    AGP_I810_MISCC_WINSIZE_32)
   1027 			size = 32 * 1024 * 1024;
   1028 		else
   1029 			size = 64 * 1024 * 1024;
   1030 		break;
   1031 	case CHIP_I830:
   1032 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
   1033 		gcc1 = (u_int16_t)(reg >> 16);
   1034 		if ((gcc1 & AGP_I830_GCC1_GMASIZE) == AGP_I830_GCC1_GMASIZE_64)
   1035 			size = 64 * 1024 * 1024;
   1036 		else
   1037 			size = 128 * 1024 * 1024;
   1038 		break;
   1039 	case CHIP_I855:
   1040 		size = 128 * 1024 * 1024;
   1041 		break;
   1042 	case CHIP_I915:
   1043 	case CHIP_G33:
   1044 	case CHIP_G4X:
   1045 		size = sc->as_apsize;
   1046 		break;
   1047 	case CHIP_I965:
   1048 		size = 512 * 1024 * 1024;
   1049 		break;
   1050 	default:
   1051 		aprint_error(": Unknown chipset\n");
   1052 	}
   1053 
   1054 	return size;
   1055 }
   1056 
   1057 static int
   1058 agp_i810_set_aperture(struct agp_softc *sc __unused,
   1059     uint32_t aperture __unused)
   1060 {
   1061 
   1062 	return ENOSYS;
   1063 }
   1064 
   1065 static int
   1066 agp_i810_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
   1067 {
   1068 	struct agp_i810_softc *isc = sc->as_chipc;
   1069 
   1070 	if (offset < 0 || offset >= ((isc->gtt_size/4) << AGP_PAGE_SHIFT)) {
   1071 #ifdef AGP_DEBUG
   1072 		printf("%s: failed: offset 0x%08x, shift %d, entries %d\n",
   1073 		    device_xname(sc->as_dev), (int)offset, AGP_PAGE_SHIFT,
   1074 		    isc->gtt_size/4);
   1075 #endif
   1076 		return EINVAL;
   1077 	}
   1078 
   1079 	if (isc->chiptype != CHIP_I810) {
   1080 		if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
   1081 #ifdef AGP_DEBUG
   1082 			printf("%s: trying to bind into stolen memory\n",
   1083 			    device_xname(sc->as_dev));
   1084 #endif
   1085 			return EINVAL;
   1086 		}
   1087 	}
   1088 
   1089 	return agp_i810_write_gtt_entry(isc, offset, physical | 1);
   1090 }
   1091 
   1092 static int
   1093 agp_i810_unbind_page(struct agp_softc *sc, off_t offset)
   1094 {
   1095 	struct agp_i810_softc *isc = sc->as_chipc;
   1096 
   1097 	if (offset < 0 || offset >= ((isc->gtt_size/4) << AGP_PAGE_SHIFT))
   1098 		return EINVAL;
   1099 
   1100 	if (isc->chiptype != CHIP_I810 ) {
   1101 		if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
   1102 #ifdef AGP_DEBUG
   1103 			printf("%s: trying to unbind from stolen memory\n",
   1104 			    device_xname(sc->as_dev));
   1105 #endif
   1106 			return EINVAL;
   1107 		}
   1108 	}
   1109 
   1110 	return agp_i810_write_gtt_entry(isc, offset, 0);
   1111 }
   1112 
   1113 /*
   1114  * Writing via memory mapped registers already flushes all TLBs.
   1115  */
   1116 static void
   1117 agp_i810_flush_tlb(struct agp_softc *sc)
   1118 {
   1119 }
   1120 
   1121 static int
   1122 agp_i810_enable(struct agp_softc *sc, u_int32_t mode)
   1123 {
   1124 
   1125 	return 0;
   1126 }
   1127 
   1128 #define	AGP_I810_MEMTYPE_MAIN		0
   1129 #define	AGP_I810_MEMTYPE_DCACHE		1
   1130 #define	AGP_I810_MEMTYPE_HWCURSOR	2
   1131 
   1132 static struct agp_memory *
   1133 agp_i810_alloc_memory(struct agp_softc *sc, int type, vsize_t size)
   1134 {
   1135 	struct agp_i810_softc *isc = sc->as_chipc;
   1136 	struct agp_memory *mem;
   1137 	int error;
   1138 
   1139 #ifdef AGP_DEBUG
   1140 	printf("AGP: alloc(%d, 0x%x)\n", type, (int) size);
   1141 #endif
   1142 
   1143 	if (size <= 0)
   1144 		return NULL;
   1145 	if ((size & (AGP_PAGE_SIZE - 1)) != 0)
   1146 		return NULL;
   1147 	KASSERT(sc->as_allocated <= sc->as_maxmem);
   1148 	if (size > (sc->as_maxmem - sc->as_allocated))
   1149 		return NULL;
   1150 	if (size > ((isc->gtt_size/4) << AGP_PAGE_SHIFT))
   1151 		return NULL;
   1152 
   1153 	switch (type) {
   1154 	case AGP_I810_MEMTYPE_MAIN:
   1155 		break;
   1156 	case AGP_I810_MEMTYPE_DCACHE:
   1157 		if (isc->chiptype != CHIP_I810)
   1158 			return NULL;
   1159 		if (size != isc->dcache_size)
   1160 			return NULL;
   1161 		break;
   1162 	case AGP_I810_MEMTYPE_HWCURSOR:
   1163 		if ((size != AGP_PAGE_SIZE) &&
   1164 		    (size != AGP_PAGE_SIZE*4))
   1165 			return NULL;
   1166 		break;
   1167 	default:
   1168 		return NULL;
   1169 	}
   1170 
   1171 	mem = malloc(sizeof(*mem), M_AGP, M_WAITOK|M_ZERO);
   1172 	if (mem == NULL)
   1173 		goto fail0;
   1174 	mem->am_id = sc->as_nextid++;
   1175 	mem->am_size = size;
   1176 	mem->am_type = type;
   1177 
   1178 	switch (type) {
   1179 	case AGP_I810_MEMTYPE_MAIN:
   1180 		error = bus_dmamap_create(sc->as_dmat, size,
   1181 		    (size >> AGP_PAGE_SHIFT) + 1, size, 0, BUS_DMA_WAITOK,
   1182 		    &mem->am_dmamap);
   1183 		if (error)
   1184 			goto fail1;
   1185 		break;
   1186 	case AGP_I810_MEMTYPE_DCACHE:
   1187 		break;
   1188 	case AGP_I810_MEMTYPE_HWCURSOR:
   1189 		mem->am_dmaseg = malloc(sizeof(*mem->am_dmaseg), M_AGP,
   1190 		    M_WAITOK);
   1191 		error = agp_alloc_dmamem(sc->as_dmat, size, 0, &mem->am_dmamap,
   1192 		    &mem->am_virtual, &mem->am_physical, mem->am_dmaseg, 1,
   1193 		    &mem->am_nseg);
   1194 		if (error) {
   1195 			free(mem->am_dmaseg, M_AGP);
   1196 			goto fail1;
   1197 		}
   1198 		(void)memset(mem->am_virtual, 0, size);
   1199 		break;
   1200 	default:
   1201 		panic("invalid agp memory type: %d", type);
   1202 	}
   1203 
   1204 	TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
   1205 	sc->as_allocated += size;
   1206 
   1207 	return mem;
   1208 
   1209 fail1:	free(mem, M_AGP);
   1210 fail0:	return NULL;
   1211 }
   1212 
   1213 static int
   1214 agp_i810_free_memory(struct agp_softc *sc, struct agp_memory *mem)
   1215 {
   1216 
   1217 	if (mem->am_is_bound)
   1218 		return EBUSY;
   1219 
   1220 	switch (mem->am_type) {
   1221 	case AGP_I810_MEMTYPE_MAIN:
   1222 		bus_dmamap_destroy(sc->as_dmat, mem->am_dmamap);
   1223 		break;
   1224 	case AGP_I810_MEMTYPE_DCACHE:
   1225 		break;
   1226 	case AGP_I810_MEMTYPE_HWCURSOR:
   1227 		agp_free_dmamem(sc->as_dmat, mem->am_size, mem->am_dmamap,
   1228 		    mem->am_virtual, mem->am_dmaseg, mem->am_nseg);
   1229 		free(mem->am_dmaseg, M_AGP);
   1230 		break;
   1231 	default:
   1232 		panic("invalid agp i810 memory type: %d", mem->am_type);
   1233 	}
   1234 
   1235 	sc->as_allocated -= mem->am_size;
   1236 	TAILQ_REMOVE(&sc->as_memory, mem, am_link);
   1237 	free(mem, M_AGP);
   1238 
   1239 	return 0;
   1240 }
   1241 
   1242 static int
   1243 agp_i810_bind_memory(struct agp_softc *sc, struct agp_memory *mem,
   1244     off_t offset)
   1245 {
   1246 	struct agp_i810_softc *isc = sc->as_chipc;
   1247 	uint32_t pgtblctl;
   1248 	int error;
   1249 
   1250 	if (mem->am_is_bound)
   1251 		return EINVAL;
   1252 
   1253 	/*
   1254 	 * XXX evil hack: the PGTBL_CTL appearently gets overwritten by the
   1255 	 * X server for mysterious reasons which leads to crashes if we write
   1256 	 * to the GTT through the MMIO window.
   1257 	 * Until the issue is solved, simply restore it.
   1258 	 */
   1259 	pgtblctl = bus_space_read_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL);
   1260 	if (pgtblctl != isc->pgtblctl) {
   1261 		printf("agp_i810_bind_memory: PGTBL_CTL is 0x%"PRIx32
   1262 		    " - fixing\n", pgtblctl);
   1263 		bus_space_write_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL,
   1264 		    isc->pgtblctl);
   1265 	}
   1266 
   1267 	switch (mem->am_type) {
   1268 	case AGP_I810_MEMTYPE_MAIN:
   1269 		return agp_generic_bind_memory_bounded(sc, mem, offset,
   1270 		    0, (isc->gtt_size/4) << AGP_PAGE_SHIFT);
   1271 	case AGP_I810_MEMTYPE_DCACHE:
   1272 		error = agp_i810_bind_memory_dcache(sc, mem, offset);
   1273 		break;
   1274 	case AGP_I810_MEMTYPE_HWCURSOR:
   1275 		error = agp_i810_bind_memory_hwcursor(sc, mem, offset);
   1276 		break;
   1277 	default:
   1278 		panic("invalid agp i810 memory type: %d", mem->am_type);
   1279 	}
   1280 	if (error)
   1281 		return error;
   1282 
   1283 	/* Success!  */
   1284 	mem->am_is_bound = 1;
   1285 	return 0;
   1286 }
   1287 
   1288 #define	I810_GTT_PTE_VALID	0x01
   1289 #define	I810_GTT_PTE_DCACHE	0x02
   1290 
   1291 static int
   1292 agp_i810_bind_memory_dcache(struct agp_softc *sc, struct agp_memory *mem,
   1293     off_t offset)
   1294 {
   1295 	struct agp_i810_softc *const isc __diagused = sc->as_chipc;
   1296 	uint32_t i, j;
   1297 	int error;
   1298 
   1299 	KASSERT(isc->chiptype == CHIP_I810);
   1300 
   1301 	KASSERT((mem->am_size & (AGP_PAGE_SIZE - 1)) == 0);
   1302 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
   1303 		/* XXX No offset?  */
   1304 		error = agp_i810_write_gtt_entry(isc, i,
   1305 		    i | I810_GTT_PTE_VALID | I810_GTT_PTE_DCACHE);
   1306 		if (error)
   1307 			goto fail0;
   1308 	}
   1309 
   1310 	/* Success!  */
   1311 	return 0;
   1312 
   1313 fail0:	for (j = 0; j < i; j += AGP_PAGE_SIZE)
   1314 		(void)agp_i810_unbind_page(sc, offset + j);
   1315 	return error;
   1316 }
   1317 
   1318 static int
   1319 agp_i810_bind_memory_hwcursor(struct agp_softc *sc, struct agp_memory *mem,
   1320     off_t offset)
   1321 {
   1322 	const bus_addr_t pa = mem->am_physical;
   1323 	uint32_t i, j;
   1324 	int error;
   1325 
   1326 	KASSERT((mem->am_size & (AGP_PAGE_SIZE - 1)) == 0);
   1327 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
   1328 		error = agp_i810_bind_page(sc, offset + i, pa + i);
   1329 		if (error)
   1330 			goto fail0;
   1331 	}
   1332 
   1333 	/* Success!  */
   1334 	mem->am_offset = offset;
   1335 	return 0;
   1336 
   1337 fail0:	for (j = 0; j < i; j += AGP_PAGE_SIZE)
   1338 		(void)agp_i810_unbind_page(sc, offset + j);
   1339 	return error;
   1340 }
   1341 
   1342 static int
   1343 agp_i810_unbind_memory(struct agp_softc *sc, struct agp_memory *mem)
   1344 {
   1345 	struct agp_i810_softc *isc = sc->as_chipc;
   1346 	u_int32_t i;
   1347 
   1348 	if (!mem->am_is_bound)
   1349 		return EINVAL;
   1350 
   1351 	switch (mem->am_type) {
   1352 	case AGP_I810_MEMTYPE_MAIN:
   1353 		return agp_generic_unbind_memory(sc, mem);
   1354 	case AGP_I810_MEMTYPE_DCACHE:
   1355 		KASSERT(isc->chiptype == CHIP_I810);
   1356 		for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
   1357 			(void)agp_i810_write_gtt_entry(isc, i, 0);
   1358 		break;
   1359 	case AGP_I810_MEMTYPE_HWCURSOR:
   1360 		for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
   1361 			(void)agp_i810_unbind_page(sc, mem->am_offset + i);
   1362 		mem->am_offset = 0;
   1363 		break;
   1364 	default:
   1365 		panic("invalid agp i810 memory type: %d", mem->am_type);
   1366 	}
   1367 
   1368 	mem->am_is_bound = 0;
   1369 	return 0;
   1370 }
   1371 
   1372 static bool
   1373 agp_i810_resume(device_t dv, const pmf_qual_t *qual)
   1374 {
   1375 	struct agp_softc *sc = device_private(dv);
   1376 	struct agp_i810_softc *isc = sc->as_chipc;
   1377 
   1378 	/*
   1379 	 * XXX Nothing uses this!  Save on suspend, restore on resume?
   1380 	 */
   1381 	isc->pgtblctl_resume_hack = READ4(AGP_I810_PGTBL_CTL);
   1382 	agp_flush_cache();
   1383 
   1384 	return true;
   1385 }
   1386