Home | History | Annotate | Line # | Download | only in pci
agp_i810.c revision 1.17
      1 /*	$NetBSD: agp_i810.c,v 1.17 2003/07/20 18:56:32 hannken 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: src/sys/pci/agp_i810.c,v 1.4 2001/07/05 21:28:47 jhb Exp $
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: agp_i810.c,v 1.17 2003/07/20 18:56:32 hannken 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/lock.h>
     40 #include <sys/proc.h>
     41 #include <sys/device.h>
     42 #include <sys/conf.h>
     43 
     44 #include <uvm/uvm_extern.h>
     45 
     46 #include <dev/pci/pcivar.h>
     47 #include <dev/pci/pcireg.h>
     48 #include <dev/pci/pcidevs.h>
     49 #include <dev/pci/agpvar.h>
     50 #include <dev/pci/agpreg.h>
     51 
     52 #include <sys/agpio.h>
     53 
     54 #include <machine/bus.h>
     55 
     56 #define READ1(off)	bus_space_read_1(isc->bst, isc->bsh, off)
     57 #define READ4(off)	bus_space_read_4(isc->bst, isc->bsh, off)
     58 #define WRITE4(off,v)	bus_space_write_4(isc->bst, isc->bsh, off, v)
     59 
     60 #define CHIP_I810 0	/* i810/i815 */
     61 #define CHIP_I830 1	/* 830M/845G */
     62 #define CHIP_I855 2	/* 852GM/855GM/865G */
     63 
     64 struct agp_i810_softc {
     65 	u_int32_t initial_aperture;	/* aperture size at startup */
     66 	struct agp_gatt *gatt;
     67 	int chiptype;			/* i810-like or i830 */
     68 	u_int32_t dcache_size;		/* i810 only */
     69 	u_int32_t stolen;		/* number of i830/845 gtt entries
     70 					   for stolen memory */
     71 	bus_space_tag_t bst;		/* bus_space tag */
     72 	bus_space_handle_t bsh;		/* bus_space handle */
     73 	struct pci_attach_args vga_pa;
     74 };
     75 
     76 static u_int32_t agp_i810_get_aperture(struct agp_softc *);
     77 static int agp_i810_set_aperture(struct agp_softc *, u_int32_t);
     78 static int agp_i810_bind_page(struct agp_softc *, off_t, bus_addr_t);
     79 static int agp_i810_unbind_page(struct agp_softc *, off_t);
     80 static void agp_i810_flush_tlb(struct agp_softc *);
     81 static int agp_i810_enable(struct agp_softc *, u_int32_t mode);
     82 static struct agp_memory *agp_i810_alloc_memory(struct agp_softc *, int,
     83 						vsize_t);
     84 static int agp_i810_free_memory(struct agp_softc *, struct agp_memory *);
     85 static int agp_i810_bind_memory(struct agp_softc *, struct agp_memory *, off_t);
     86 static int agp_i810_unbind_memory(struct agp_softc *, struct agp_memory *);
     87 
     88 struct agp_methods agp_i810_methods = {
     89 	agp_i810_get_aperture,
     90 	agp_i810_set_aperture,
     91 	agp_i810_bind_page,
     92 	agp_i810_unbind_page,
     93 	agp_i810_flush_tlb,
     94 	agp_i810_enable,
     95 	agp_i810_alloc_memory,
     96 	agp_i810_free_memory,
     97 	agp_i810_bind_memory,
     98 	agp_i810_unbind_memory,
     99 };
    100 
    101 /* XXXthorpej -- duplicated code (see arch/i386/pci/pchb.c) */
    102 static int
    103 agp_i810_vgamatch(struct pci_attach_args *pa)
    104 {
    105 
    106 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
    107 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
    108 		return (0);
    109 
    110 	switch (PCI_PRODUCT(pa->pa_id)) {
    111 	case PCI_PRODUCT_INTEL_82810_GC:
    112 	case PCI_PRODUCT_INTEL_82810_DC100_GC:
    113 	case PCI_PRODUCT_INTEL_82810E_GC:
    114 	case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
    115 	case PCI_PRODUCT_INTEL_82830MP_IV:
    116 	case PCI_PRODUCT_INTEL_82845G_IGD:
    117 	case PCI_PRODUCT_INTEL_82855GM_IGD:
    118 		return (1);
    119 	}
    120 
    121 	return (0);
    122 }
    123 
    124 int
    125 agp_i810_attach(struct device *parent, struct device *self, void *aux)
    126 {
    127 	struct agp_softc *sc = (void *)self;
    128 	struct agp_i810_softc *isc;
    129 	struct agp_gatt *gatt;
    130 	int error;
    131 
    132 	isc = malloc(sizeof *isc, M_AGP, M_NOWAIT|M_ZERO);
    133 	if (isc == NULL) {
    134 		aprint_error(": can't allocate chipset-specific softc\n");
    135 		return ENOMEM;
    136 	}
    137 	sc->as_chipc = isc;
    138 	sc->as_methods = &agp_i810_methods;
    139 
    140 	if (pci_find_device(&isc->vga_pa, agp_i810_vgamatch) == 0) {
    141 		aprint_error(": can't find internal VGA device config space\n");
    142 		free(isc, M_AGP);
    143 		return ENOENT;
    144 	}
    145 
    146 	/* XXXfvdl */
    147 	sc->as_dmat = isc->vga_pa.pa_dmat;
    148 
    149 	error = agp_map_aperture(&isc->vga_pa, sc);
    150 	if (error != 0) {
    151 		aprint_error(": can't map aperture\n");
    152 		free(isc, M_AGP);
    153 		return error;
    154 	}
    155 
    156 	switch (PCI_PRODUCT(isc->vga_pa.pa_id)) {
    157 	case PCI_PRODUCT_INTEL_82810_GC:
    158 	case PCI_PRODUCT_INTEL_82810_DC100_GC:
    159 	case PCI_PRODUCT_INTEL_82810E_GC:
    160 	case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
    161 		isc->chiptype = CHIP_I810;
    162 		break;
    163 	case PCI_PRODUCT_INTEL_82830MP_IV:
    164 	case PCI_PRODUCT_INTEL_82845G_IGD:
    165 		isc->chiptype = CHIP_I830;
    166 		break;
    167 	case PCI_PRODUCT_INTEL_82855GM_IGD:
    168 		isc->chiptype = CHIP_I855;
    169 		break;
    170 	}
    171 
    172 	error = pci_mapreg_map(&isc->vga_pa, AGP_I810_MMADR,
    173 	    PCI_MAPREG_TYPE_MEM, 0, &isc->bst, &isc->bsh, NULL, NULL);
    174 	if (error != 0) {
    175 		aprint_error(": can't map mmadr registers\n");
    176 		return error;
    177 	}
    178 
    179 	isc->initial_aperture = AGP_GET_APERTURE(sc);
    180 
    181 	gatt = malloc(sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
    182 	if (!gatt) {
    183  		agp_generic_detach(sc);
    184  		return ENOMEM;
    185 	}
    186 	isc->gatt = gatt;
    187 
    188 	gatt->ag_entries = AGP_GET_APERTURE(sc) >> AGP_PAGE_SHIFT;
    189 
    190 	if (isc->chiptype == CHIP_I810) {
    191 		int dummyseg;
    192 		/* Some i810s have on-chip memory called dcache */
    193 		if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
    194 			isc->dcache_size = 4 * 1024 * 1024;
    195 		else
    196 			isc->dcache_size = 0;
    197 
    198 		/* According to the specs the gatt on the i810 must be 64k */
    199 		if (agp_alloc_dmamem(sc->as_dmat, 64 * 1024,
    200 		    0, &gatt->ag_dmamap, (caddr_t *)&gatt->ag_virtual,
    201 		    &gatt->ag_physical, &gatt->ag_dmaseg, 1, &dummyseg) != 0) {
    202 			free(gatt, M_AGP);
    203 			agp_generic_detach(sc);
    204 			return ENOMEM;
    205 		}
    206 
    207 		gatt->ag_size = gatt->ag_entries * sizeof(u_int32_t);
    208 		memset(gatt->ag_virtual, 0, gatt->ag_size);
    209 
    210 		agp_flush_cache();
    211 		/* Install the GATT. */
    212 		WRITE4(AGP_I810_PGTBL_CTL, gatt->ag_physical | 1);
    213 	} else if (isc->chiptype == CHIP_I830) {
    214 		/* The i830 automatically initializes the 128k gatt on boot. */
    215 		pcireg_t reg;
    216 		u_int32_t pgtblctl;
    217 		u_int16_t gcc1;
    218 
    219 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
    220 		gcc1 = (u_int16_t)(reg >> 16);
    221 		switch (gcc1 & AGP_I830_GCC1_GMS) {
    222 		case AGP_I830_GCC1_GMS_STOLEN_512:
    223 			isc->stolen = (512 - 132) * 1024 / 4096;
    224 			break;
    225 		case AGP_I830_GCC1_GMS_STOLEN_1024:
    226 			isc->stolen = (1024 - 132) * 1024 / 4096;
    227 			break;
    228 		case AGP_I830_GCC1_GMS_STOLEN_8192:
    229 			isc->stolen = (8192 - 132) * 1024 / 4096;
    230 			break;
    231 		default:
    232 			isc->stolen = 0;
    233 			aprint_error(
    234 			    ": unknown memory configuration, disabling\n");
    235 			agp_generic_detach(sc);
    236 			return EINVAL;
    237 		}
    238 		if (isc->stolen > 0) {
    239 			aprint_error(": detected %dk stolen memory\n%s",
    240 			    isc->stolen * 4, sc->as_dev.dv_xname);
    241 		}
    242 		aprint_error(": aperture size is %dM\n%s",
    243 		       isc->initial_aperture / 1024 / 1024,
    244 		       sc->as_dev.dv_xname);
    245 
    246 		/* GATT address is already in there, make sure it's enabled */
    247 		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
    248 		pgtblctl |= 1;
    249 		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
    250 
    251 		gatt->ag_physical = pgtblctl & ~1;
    252 	} else {	/* CHIP_I855 */
    253 		/* The 855GM automatically initializes the 128k gatt on boot. */
    254 		pcireg_t reg;
    255 		u_int32_t pgtblctl;
    256 		u_int16_t gcc1;
    257 
    258 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I855_GCC1);
    259 		gcc1 = (u_int16_t)(reg >> 16);
    260 		switch (gcc1 & AGP_I855_GCC1_GMS) {
    261 		case AGP_I855_GCC1_GMS_STOLEN_1M:
    262 			isc->stolen = (1024 - 132) * 1024 / 4096;
    263 			break;
    264 		case AGP_I855_GCC1_GMS_STOLEN_4M:
    265 			isc->stolen = (4096 - 132) * 1024 / 4096;
    266 			break;
    267 		case AGP_I855_GCC1_GMS_STOLEN_8M:
    268 			isc->stolen = (8192 - 132) * 1024 / 4096;
    269 			break;
    270 		case AGP_I855_GCC1_GMS_STOLEN_16M:
    271 			isc->stolen = (16384 - 132) * 1024 / 4096;
    272 			break;
    273 		case AGP_I855_GCC1_GMS_STOLEN_32M:
    274 			isc->stolen = (32768 - 132) * 1024 / 4096;
    275 			break;
    276 		default:
    277 			isc->stolen = 0;
    278 			aprint_error(
    279 			    ": unknown memory configuration, disabling\n");
    280 			agp_generic_detach(sc);
    281 			return EINVAL;
    282 		}
    283 		if (isc->stolen > 0) {
    284 			aprint_error(": detected %dk stolen memory\n%s",
    285 			    isc->stolen * 4, sc->as_dev.dv_xname);
    286 		}
    287 		aprint_error(": aperture size is %dM\n%s",
    288 			isc->initial_aperture / 1024 / 1024,
    289 			sc->as_dev.dv_xname);
    290 
    291 		/* GATT address is already in there, make sure it's enabled */
    292 		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
    293 		pgtblctl |= 1;
    294 		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
    295 
    296 		gatt->ag_physical = pgtblctl & ~1;
    297 	}
    298 
    299 	/*
    300 	 * Make sure the chipset can see everything.
    301 	 */
    302 	agp_flush_cache();
    303 
    304 	return 0;
    305 }
    306 
    307 #if 0
    308 static int
    309 agp_i810_detach(struct agp_softc *sc)
    310 {
    311 	int error;
    312 	struct agp_i810_softc *isc = sc->as_chipc;
    313 
    314 	error = agp_generic_detach(sc);
    315 	if (error)
    316 		return error;
    317 
    318 	/* Clear the GATT base. */
    319 	if (sc->chiptype == CHIP_I810) {
    320 		WRITE4(AGP_I810_PGTBL_CTL, 0);
    321 	} else {
    322 		unsigned int pgtblctl;
    323 		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
    324 		pgtblctl &= ~1;
    325 		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
    326 	}
    327 
    328 	/* Put the aperture back the way it started. */
    329 	AGP_SET_APERTURE(sc, isc->initial_aperture);
    330 
    331 	if (sc->chiptype == CHIP_I810) {
    332 		agp_free_dmamem(sc->as_dmat, gatt->ag_size, gatt->ag_dmamap,
    333 		    (caddr_t)gatt->ag_virtual, &gatt->ag_dmaseg, 1);
    334 	}
    335 	free(sc->gatt, M_AGP);
    336 
    337 	return 0;
    338 }
    339 #endif
    340 
    341 static u_int32_t
    342 agp_i810_get_aperture(struct agp_softc *sc)
    343 {
    344 	struct agp_i810_softc *isc = sc->as_chipc;
    345 	pcireg_t reg;
    346 
    347 	if (isc->chiptype == CHIP_I810) {
    348 		u_int16_t miscc;
    349 
    350 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
    351 		miscc = (u_int16_t)(reg >> 16);
    352 		if ((miscc & AGP_I810_MISCC_WINSIZE) ==
    353 		    AGP_I810_MISCC_WINSIZE_32)
    354 			return 32 * 1024 * 1024;
    355 		else
    356 			return 64 * 1024 * 1024;
    357 	} else if (isc->chiptype == CHIP_I830) {
    358 		u_int16_t gcc1;
    359 
    360 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
    361 		gcc1 = (u_int16_t)(reg >> 16);
    362 		if ((gcc1 & AGP_I830_GCC1_GMASIZE) == AGP_I830_GCC1_GMASIZE_64)
    363 			return 64 * 1024 * 1024;
    364 		else
    365 			return 128 * 1024 * 1024;
    366 	} else {	/* CHIP_I855 */
    367 		return 128 * 1024 * 1024;
    368 	}
    369 }
    370 
    371 static int
    372 agp_i810_set_aperture(struct agp_softc *sc, u_int32_t aperture)
    373 {
    374 	struct agp_i810_softc *isc = sc->as_chipc;
    375 	pcireg_t reg;
    376 
    377 	if (isc->chiptype == CHIP_I810) {
    378 		u_int16_t miscc;
    379 
    380 		/*
    381 		 * Double check for sanity.
    382 		 */
    383 		if (aperture != (32 * 1024 * 1024) &&
    384 		    aperture != (64 * 1024 * 1024)) {
    385 			printf("%s: bad aperture size %d\n",
    386 			    sc->as_dev.dv_xname, aperture);
    387 			return EINVAL;
    388 		}
    389 
    390 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
    391 		miscc = (u_int16_t)(reg >> 16);
    392 		miscc &= ~AGP_I810_MISCC_WINSIZE;
    393 		if (aperture == 32 * 1024 * 1024)
    394 			miscc |= AGP_I810_MISCC_WINSIZE_32;
    395 		else
    396 			miscc |= AGP_I810_MISCC_WINSIZE_64;
    397 
    398 		reg &= 0x0000ffff;
    399 		reg |= ((pcireg_t)miscc) << 16;
    400 		pci_conf_write(sc->as_pc, sc->as_tag, AGP_I810_SMRAM, reg);
    401 	} if (isc->chiptype == CHIP_I830) {
    402 		u_int16_t gcc1;
    403 
    404 		if (aperture != (64 * 1024 * 1024) &&
    405 		    aperture != (128 * 1024 * 1024)) {
    406 			printf("%s: bad aperture size %d\n",
    407 			    sc->as_dev.dv_xname, aperture);
    408 			return EINVAL;
    409 		}
    410 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
    411 		gcc1 = (u_int16_t)(reg >> 16);
    412 		gcc1 &= ~AGP_I830_GCC1_GMASIZE;
    413 		if (aperture == 64 * 1024 * 1024)
    414 			gcc1 |= AGP_I830_GCC1_GMASIZE_64;
    415 		else
    416 			gcc1 |= AGP_I830_GCC1_GMASIZE_128;
    417 
    418 		reg &= 0x0000ffff;
    419 		reg |= ((pcireg_t)gcc1) << 16;
    420 		pci_conf_write(sc->as_pc, sc->as_tag, AGP_I830_GCC0, reg);
    421 	} else {	/* CHIP_I855 */
    422 		if (aperture != 128 * 1024 * 1024) {
    423 			printf("%s: bad aperture size %d\n",
    424 			    sc->as_dev.dv_xname, aperture);
    425 			return EINVAL;
    426 		}
    427 	}
    428 
    429 	return 0;
    430 }
    431 
    432 static int
    433 agp_i810_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
    434 {
    435 	struct agp_i810_softc *isc = sc->as_chipc;
    436 
    437 	if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
    438 #ifdef DEBUG
    439 		printf("%s: failed: offset 0x%08x, shift %d, entries %d\n",
    440 		    sc->as_dev.dv_xname, (int)offset, AGP_PAGE_SHIFT,
    441 		    isc->gatt->ag_entries);
    442 #endif
    443 		return EINVAL;
    444 	}
    445 
    446 	if (isc->chiptype != CHIP_I830) {
    447 		if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
    448 #ifdef DEBUG
    449 			printf("%s: trying to bind into stolen memory",
    450 			    sc->as_dev.dv_xname);
    451 #endif
    452 			return EINVAL;
    453 		}
    454 	}
    455 
    456 	WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
    457 	    physical | 1);
    458 	return 0;
    459 }
    460 
    461 static int
    462 agp_i810_unbind_page(struct agp_softc *sc, off_t offset)
    463 {
    464 	struct agp_i810_softc *isc = sc->as_chipc;
    465 
    466 	if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
    467 		return EINVAL;
    468 
    469 	if (isc->chiptype != CHIP_I810 ) {
    470 		if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
    471 #ifdef DEBUG
    472 			printf("%s: trying to unbind from stolen memory",
    473 			    sc->as_dev.dv_xname);
    474 #endif
    475 			return EINVAL;
    476 		}
    477 	}
    478 
    479 	WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4, 0);
    480 	return 0;
    481 }
    482 
    483 /*
    484  * Writing via memory mapped registers already flushes all TLBs.
    485  */
    486 static void
    487 agp_i810_flush_tlb(struct agp_softc *sc)
    488 {
    489 }
    490 
    491 static int
    492 agp_i810_enable(struct agp_softc *sc, u_int32_t mode)
    493 {
    494 
    495 	return 0;
    496 }
    497 
    498 static struct agp_memory *
    499 agp_i810_alloc_memory(struct agp_softc *sc, int type, vsize_t size)
    500 {
    501 	struct agp_i810_softc *isc = sc->as_chipc;
    502 	struct agp_memory *mem;
    503 
    504 	if ((size & (AGP_PAGE_SIZE - 1)) != 0)
    505 		return 0;
    506 
    507 	if (sc->as_allocated + size > sc->as_maxmem)
    508 		return 0;
    509 
    510 	if (type == 1) {
    511 		/*
    512 		 * Mapping local DRAM into GATT.
    513 		 */
    514 		if (isc->chiptype != CHIP_I810 )
    515 			return 0;
    516 		if (size != isc->dcache_size)
    517 			return 0;
    518 	} else if (type == 2) {
    519 		/*
    520 		 * Bogus mapping of a single page for the hardware cursor.
    521 		 */
    522 		if (size != AGP_PAGE_SIZE)
    523 			return 0;
    524 	}
    525 
    526 	mem = malloc(sizeof *mem, M_AGP, M_WAITOK|M_ZERO);
    527 	if (mem == NULL)
    528 		return NULL;
    529 	mem->am_id = sc->as_nextid++;
    530 	mem->am_size = size;
    531 	mem->am_type = type;
    532 
    533 	if (type == 2) {
    534 		/*
    535 		 * Allocate and wire down the page now so that we can
    536 		 * get its physical address.
    537 		 */
    538 		mem->am_dmaseg = malloc(sizeof *mem->am_dmaseg, M_AGP,
    539 		    M_WAITOK);
    540 		if (mem->am_dmaseg == NULL) {
    541 			free(mem, M_AGP);
    542 			return NULL;
    543 		}
    544 		if (agp_alloc_dmamem(sc->as_dmat, size, 0,
    545 		    &mem->am_dmamap, &mem->am_virtual, &mem->am_physical,
    546 		    mem->am_dmaseg, 1, &mem->am_nseg) != 0) {
    547 			free(mem->am_dmaseg, M_AGP);
    548 			free(mem, M_AGP);
    549 			return NULL;
    550 		}
    551 	} else if (type != 1) {
    552 		if (bus_dmamap_create(sc->as_dmat, size, size / PAGE_SIZE + 1,
    553 				      size, 0, BUS_DMA_NOWAIT,
    554 				      &mem->am_dmamap) != 0) {
    555 			free(mem, M_AGP);
    556 			return NULL;
    557 		}
    558 	}
    559 
    560 	TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
    561 	sc->as_allocated += size;
    562 
    563 	return mem;
    564 }
    565 
    566 static int
    567 agp_i810_free_memory(struct agp_softc *sc, struct agp_memory *mem)
    568 {
    569 	if (mem->am_is_bound)
    570 		return EBUSY;
    571 
    572 	if (mem->am_type == 2) {
    573 		agp_free_dmamem(sc->as_dmat, mem->am_size, mem->am_dmamap,
    574 		    mem->am_virtual, mem->am_dmaseg, mem->am_nseg);
    575 		free(mem->am_dmaseg, M_AGP);
    576 	}
    577 
    578 	sc->as_allocated -= mem->am_size;
    579 	TAILQ_REMOVE(&sc->as_memory, mem, am_link);
    580 	free(mem, M_AGP);
    581 	return 0;
    582 }
    583 
    584 static int
    585 agp_i810_bind_memory(struct agp_softc *sc, struct agp_memory *mem,
    586 		     off_t offset)
    587 {
    588 	struct agp_i810_softc *isc = sc->as_chipc;
    589 	u_int32_t regval, i;
    590 
    591 	/*
    592 	 * XXX evil hack: the PGTBL_CTL appearently gets overwritten by the
    593 	 * X server for mysterious reasons which leads to crashes if we write
    594 	 * to the GTT through the MMIO window.
    595 	 * Until the issue is solved, simply restore it.
    596 	 */
    597 	regval = bus_space_read_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL);
    598 	if (regval != (isc->gatt->ag_physical | 1)) {
    599 		printf("agp_i810_bind_memory: PGTBL_CTL is 0x%x - fixing\n",
    600 		       regval);
    601 		bus_space_write_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL,
    602 				  isc->gatt->ag_physical | 1);
    603 	}
    604 
    605 	if (mem->am_type == 2) {
    606 		WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
    607 		       mem->am_physical | 1);
    608 		mem->am_offset = offset;
    609 		mem->am_is_bound = 1;
    610 		return 0;
    611 	}
    612 
    613 	if (mem->am_type != 1)
    614 		return agp_generic_bind_memory(sc, mem, offset);
    615 
    616 	if (isc->chiptype != CHIP_I810)
    617 		return EINVAL;
    618 
    619 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
    620 		WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
    621 		       i | 3);
    622 	}
    623 	mem->am_is_bound = 1;
    624 	return 0;
    625 }
    626 
    627 static int
    628 agp_i810_unbind_memory(struct agp_softc *sc, struct agp_memory *mem)
    629 {
    630 	struct agp_i810_softc *isc = sc->as_chipc;
    631 	u_int32_t i;
    632 
    633 	if (mem->am_type == 2) {
    634 		WRITE4(AGP_I810_GTT +
    635 		       (u_int32_t)(mem->am_offset >> AGP_PAGE_SHIFT) * 4,
    636 		       0);
    637 		mem->am_offset = 0;
    638 		mem->am_is_bound = 0;
    639 		return 0;
    640 	}
    641 
    642 	if (mem->am_type != 1)
    643 		return agp_generic_unbind_memory(sc, mem);
    644 
    645 	if (isc->chiptype != CHIP_I810)
    646 		return EINVAL;
    647 
    648 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
    649 		WRITE4(AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0);
    650 	mem->am_is_bound = 0;
    651 	return 0;
    652 }
    653