Home | History | Annotate | Line # | Download | only in pci
agp.c revision 1.17
      1 /*	$NetBSD: agp.c,v 1.17 2002/09/06 13:18:43 gehenna Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 Doug Rabson
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  *
     28  *	$FreeBSD: src/sys/pci/agp.c,v 1.12 2001/05/19 01:28:07 alfred Exp $
     29  */
     30 
     31 /*
     32  * Copyright (c) 2001 Wasabi Systems, Inc.
     33  * All rights reserved.
     34  *
     35  * Written by Frank van der Linden for Wasabi Systems, Inc.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  * 3. All advertising materials mentioning features or use of this software
     46  *    must display the following acknowledgement:
     47  *      This product includes software developed for the NetBSD Project by
     48  *      Wasabi Systems, Inc.
     49  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     50  *    or promote products derived from this software without specific prior
     51  *    written permission.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     55  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     56  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     57  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     58  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     59  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     60  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     61  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     62  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     63  * POSSIBILITY OF SUCH DAMAGE.
     64  */
     65 
     66 
     67 #include <sys/cdefs.h>
     68 __KERNEL_RCSID(0, "$NetBSD: agp.c,v 1.17 2002/09/06 13:18:43 gehenna Exp $");
     69 
     70 #include <sys/param.h>
     71 #include <sys/systm.h>
     72 #include <sys/malloc.h>
     73 #include <sys/kernel.h>
     74 #include <sys/device.h>
     75 #include <sys/conf.h>
     76 #include <sys/ioctl.h>
     77 #include <sys/fcntl.h>
     78 #include <sys/agpio.h>
     79 #include <sys/proc.h>
     80 
     81 #include <uvm/uvm_extern.h>
     82 
     83 #include <dev/pci/pcireg.h>
     84 #include <dev/pci/pcivar.h>
     85 #include <dev/pci/agpvar.h>
     86 #include <dev/pci/agpreg.h>
     87 #include <dev/pci/pcidevs.h>
     88 
     89 #include <machine/bus.h>
     90 
     91 /* Helper functions for implementing chipset mini drivers. */
     92 /* XXXfvdl get rid of this one. */
     93 
     94 extern struct cfdriver agp_cd;
     95 
     96 dev_type_open(agpopen);
     97 dev_type_close(agpclose);
     98 dev_type_ioctl(agpioctl);
     99 dev_type_mmap(agpmmap);
    100 
    101 const struct cdevsw agp_cdevsw = {
    102 	agpopen, agpclose, noread, nowrite, agpioctl,
    103 	nostop, notty, nopoll, agpmmap,
    104 };
    105 
    106 int agpmatch(struct device *, struct cfdata *, void *);
    107 void agpattach(struct device *, struct device *, void *);
    108 
    109 struct cfattach agp_ca = {
    110 	sizeof(struct agp_softc), agpmatch, agpattach
    111 };
    112 
    113 static int agp_info_user(struct agp_softc *, agp_info *);
    114 static int agp_setup_user(struct agp_softc *, agp_setup *);
    115 static int agp_allocate_user(struct agp_softc *, agp_allocate *);
    116 static int agp_deallocate_user(struct agp_softc *, int);
    117 static int agp_bind_user(struct agp_softc *, agp_bind *);
    118 static int agp_unbind_user(struct agp_softc *, agp_unbind *);
    119 static int agpdev_match(struct pci_attach_args *);
    120 
    121 #include "agp_ali.h"
    122 #include "agp_amd.h"
    123 #include "agp_i810.h"
    124 #include "agp_intel.h"
    125 #include "agp_sis.h"
    126 #include "agp_via.h"
    127 
    128 const struct agp_product {
    129 	uint32_t	ap_vendor;
    130 	uint32_t	ap_product;
    131 	int		(*ap_match)(const struct pci_attach_args *);
    132 	int		(*ap_attach)(struct device *, struct device *, void *);
    133 } agp_products[] = {
    134 #if NAGP_ALI > 0
    135 	{ PCI_VENDOR_ALI,	-1,
    136 	  NULL,			agp_ali_attach },
    137 #endif
    138 
    139 #if NAGP_AMD > 0
    140 	{ PCI_VENDOR_AMD,	-1,
    141 	  agp_amd_match,	agp_amd_attach },
    142 #endif
    143 
    144 #if NAGP_I810 > 0
    145 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82810_MCH,
    146 	  NULL,			agp_i810_attach },
    147 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82810_DC100_MCH,
    148 	  NULL,			agp_i810_attach },
    149 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82810E_MCH,
    150 	  NULL,			agp_i810_attach },
    151 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82815_FULL_HUB,
    152 	  NULL,			agp_i810_attach },
    153 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82840_HB,
    154 	  NULL,			agp_i810_attach },
    155 #if 0
    156 /* XXX needs somewhat different driver */
    157 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82830MP_IO_1,
    158 	  NULL,			agp_i810_attach },
    159 #endif
    160 #endif
    161 
    162 #if NAGP_INTEL > 0
    163 	{ PCI_VENDOR_INTEL,	-1,
    164 	  NULL,			agp_intel_attach },
    165 #endif
    166 
    167 #if NAGP_SIS > 0
    168 	{ PCI_VENDOR_SIS,	-1,
    169 	  NULL,			agp_sis_attach },
    170 #endif
    171 
    172 #if NAGP_VIA > 0
    173 	{ PCI_VENDOR_VIATECH,	-1,
    174 	  NULL,			agp_via_attach },
    175 #endif
    176 
    177 	{ 0,			0,
    178 	  NULL,			NULL },
    179 };
    180 
    181 static const struct agp_product *
    182 agp_lookup(const struct pci_attach_args *pa)
    183 {
    184 	const struct agp_product *ap;
    185 
    186 	/* First find the vendor. */
    187 	for (ap = agp_products; ap->ap_attach != NULL; ap++) {
    188 		if (PCI_VENDOR(pa->pa_id) == ap->ap_vendor)
    189 			break;
    190 	}
    191 
    192 	if (ap->ap_attach == NULL)
    193 		return (NULL);
    194 
    195 	/* Now find the product within the vendor's domain. */
    196 	for (; ap->ap_attach != NULL; ap++) {
    197 		if (PCI_VENDOR(pa->pa_id) != ap->ap_vendor) {
    198 			/* Ran out of this vendor's section of the table. */
    199 			return (NULL);
    200 		}
    201 		if (ap->ap_product == PCI_PRODUCT(pa->pa_id)) {
    202 			/* Exact match. */
    203 			break;
    204 		}
    205 		if (ap->ap_product == (uint32_t) -1) {
    206 			/* Wildcard match. */
    207 			break;
    208 		}
    209 	}
    210 
    211 	if (ap->ap_attach == NULL)
    212 		return (NULL);
    213 
    214 	/* Now let the product-specific driver filter the match. */
    215 	if (ap->ap_match != NULL && (*ap->ap_match)(pa) == 0)
    216 		return (NULL);
    217 
    218 	return (ap);
    219 }
    220 
    221 int
    222 agpmatch(struct device *parent, struct cfdata *match, void *aux)
    223 {
    224 	struct agpbus_attach_args *apa = aux;
    225 	struct pci_attach_args *pa = &apa->apa_pci_args;
    226 
    227 	if (strcmp(apa->apa_busname, "agp") != 0)
    228 		return (0);
    229 
    230 	if (agp_lookup(pa) == NULL)
    231 		return (0);
    232 
    233 	return (1);
    234 }
    235 
    236 static int agp_max[][2] = {
    237 	{0,	0},
    238 	{32,	4},
    239 	{64,	28},
    240 	{128,	96},
    241 	{256,	204},
    242 	{512,	440},
    243 	{1024,	942},
    244 	{2048,	1920},
    245 	{4096,	3932}
    246 };
    247 #define agp_max_size	(sizeof(agp_max) / sizeof(agp_max[0]))
    248 
    249 void
    250 agpattach(struct device *parent, struct device *self, void *aux)
    251 {
    252 	struct agpbus_attach_args *apa = aux;
    253 	struct pci_attach_args *pa = &apa->apa_pci_args;
    254 	struct agp_softc *sc = (void *)self;
    255 	const struct agp_product *ap;
    256 	int memsize, i, ret;
    257 
    258 	ap = agp_lookup(pa);
    259 	if (ap == NULL) {
    260 		printf("\n");
    261 		panic("agpattach: impossible");
    262 	}
    263 
    264 	sc->as_dmat = pa->pa_dmat;
    265 	sc->as_pc = pa->pa_pc;
    266 	sc->as_tag = pa->pa_tag;
    267 	sc->as_id = pa->pa_id;
    268 
    269 	/*
    270 	 * Work out an upper bound for agp memory allocation. This
    271 	 * uses a heurisitc table from the Linux driver.
    272 	 */
    273 	memsize = ptoa(physmem) >> 20;
    274 	for (i = 0; i < agp_max_size; i++) {
    275 		if (memsize <= agp_max[i][0])
    276 			break;
    277 	}
    278 	if (i == agp_max_size)
    279 		i = agp_max_size - 1;
    280 	sc->as_maxmem = agp_max[i][1] << 20U;
    281 
    282 	/*
    283 	 * The lock is used to prevent re-entry to
    284 	 * agp_generic_bind_memory() since that function can sleep.
    285 	 */
    286 	lockinit(&sc->as_lock, PZERO|PCATCH, "agplk", 0, 0);
    287 
    288 	TAILQ_INIT(&sc->as_memory);
    289 
    290 	ret = (*ap->ap_attach)(parent, self, pa);
    291 	if (ret == 0)
    292 		printf(": aperture at 0x%lx, size 0x%lx\n",
    293 		    (unsigned long)sc->as_apaddr,
    294 		    (unsigned long)AGP_GET_APERTURE(sc));
    295 	else
    296 		sc->as_chipc = NULL;
    297 }
    298 int
    299 agp_map_aperture(struct pci_attach_args *pa, struct agp_softc *sc)
    300 {
    301 	/*
    302 	 * Find and the aperture. Don't map it (yet), this would
    303 	 * eat KVA.
    304 	 */
    305 	if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, AGP_APBASE,
    306 	    PCI_MAPREG_TYPE_MEM, &sc->as_apaddr, &sc->as_apsize,
    307 	    &sc->as_apflags) != 0)
    308 		return ENXIO;
    309 
    310 	sc->as_apt = pa->pa_memt;
    311 
    312 	return 0;
    313 }
    314 
    315 struct agp_gatt *
    316 agp_alloc_gatt(struct agp_softc *sc)
    317 {
    318 	u_int32_t apsize = AGP_GET_APERTURE(sc);
    319 	u_int32_t entries = apsize >> AGP_PAGE_SHIFT;
    320 	struct agp_gatt *gatt;
    321 	int dummyseg;
    322 
    323 	gatt = malloc(sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
    324 	if (!gatt)
    325 		return NULL;
    326 	gatt->ag_entries = entries;
    327 
    328 	if (agp_alloc_dmamem(sc->as_dmat, entries * sizeof(u_int32_t),
    329 	    0, &gatt->ag_dmamap, (caddr_t *)&gatt->ag_virtual,
    330 	    &gatt->ag_physical, &gatt->ag_dmaseg, 1, &dummyseg) != 0)
    331 		return NULL;
    332 
    333 	gatt->ag_size = entries * sizeof(u_int32_t);
    334 	memset(gatt->ag_virtual, 0, gatt->ag_size);
    335 	agp_flush_cache();
    336 
    337 	return gatt;
    338 }
    339 
    340 void
    341 agp_free_gatt(struct agp_softc *sc, struct agp_gatt *gatt)
    342 {
    343 	agp_free_dmamem(sc->as_dmat, gatt->ag_size, gatt->ag_dmamap,
    344 	    (caddr_t)gatt->ag_virtual, &gatt->ag_dmaseg, 1);
    345 	free(gatt, M_AGP);
    346 }
    347 
    348 
    349 int
    350 agp_generic_detach(struct agp_softc *sc)
    351 {
    352 	lockmgr(&sc->as_lock, LK_DRAIN, 0);
    353 	agp_flush_cache();
    354 	return 0;
    355 }
    356 
    357 static int
    358 agpdev_match(struct pci_attach_args *pa)
    359 {
    360 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY &&
    361 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_DISPLAY_VGA)
    362 		return 1;
    363 
    364 	return 0;
    365 }
    366 
    367 int
    368 agp_generic_enable(struct agp_softc *sc, u_int32_t mode)
    369 {
    370 	struct pci_attach_args pa;
    371 	pcireg_t tstatus, mstatus;
    372 	pcireg_t command;
    373 	int rq, sba, fw, rate, capoff;
    374 
    375 	if (pci_find_device(&pa, agpdev_match) == 0 ||
    376 	    pci_get_capability(pa.pa_pc, pa.pa_tag, PCI_CAP_AGP,
    377 	     &capoff, NULL) == 0) {
    378 		printf("%s: can't find display\n", sc->as_dev.dv_xname);
    379 		return ENXIO;
    380 	}
    381 
    382 	tstatus = pci_conf_read(sc->as_pc, sc->as_tag,
    383 	    sc->as_capoff + AGP_STATUS);
    384 	mstatus = pci_conf_read(pa.pa_pc, pa.pa_tag,
    385 	    capoff + AGP_STATUS);
    386 
    387 	/* Set RQ to the min of mode, tstatus and mstatus */
    388 	rq = AGP_MODE_GET_RQ(mode);
    389 	if (AGP_MODE_GET_RQ(tstatus) < rq)
    390 		rq = AGP_MODE_GET_RQ(tstatus);
    391 	if (AGP_MODE_GET_RQ(mstatus) < rq)
    392 		rq = AGP_MODE_GET_RQ(mstatus);
    393 
    394 	/* Set SBA if all three can deal with SBA */
    395 	sba = (AGP_MODE_GET_SBA(tstatus)
    396 	       & AGP_MODE_GET_SBA(mstatus)
    397 	       & AGP_MODE_GET_SBA(mode));
    398 
    399 	/* Similar for FW */
    400 	fw = (AGP_MODE_GET_FW(tstatus)
    401 	       & AGP_MODE_GET_FW(mstatus)
    402 	       & AGP_MODE_GET_FW(mode));
    403 
    404 	/* Figure out the max rate */
    405 	rate = (AGP_MODE_GET_RATE(tstatus)
    406 		& AGP_MODE_GET_RATE(mstatus)
    407 		& AGP_MODE_GET_RATE(mode));
    408 	if (rate & AGP_MODE_RATE_4x)
    409 		rate = AGP_MODE_RATE_4x;
    410 	else if (rate & AGP_MODE_RATE_2x)
    411 		rate = AGP_MODE_RATE_2x;
    412 	else
    413 		rate = AGP_MODE_RATE_1x;
    414 
    415 	/* Construct the new mode word and tell the hardware */
    416 	command = AGP_MODE_SET_RQ(0, rq);
    417 	command = AGP_MODE_SET_SBA(command, sba);
    418 	command = AGP_MODE_SET_FW(command, fw);
    419 	command = AGP_MODE_SET_RATE(command, rate);
    420 	command = AGP_MODE_SET_AGP(command, 1);
    421 	pci_conf_write(sc->as_pc, sc->as_tag,
    422 	    sc->as_capoff + AGP_COMMAND, command);
    423 	pci_conf_write(pa.pa_pc, pa.pa_tag, capoff + AGP_COMMAND, command);
    424 
    425 	return 0;
    426 }
    427 
    428 struct agp_memory *
    429 agp_generic_alloc_memory(struct agp_softc *sc, int type, vsize_t size)
    430 {
    431 	struct agp_memory *mem;
    432 
    433 	if ((size & (AGP_PAGE_SIZE - 1)) != 0)
    434 		return 0;
    435 
    436 	if (sc->as_allocated + size > sc->as_maxmem)
    437 		return 0;
    438 
    439 	if (type != 0) {
    440 		printf("agp_generic_alloc_memory: unsupported type %d\n",
    441 		       type);
    442 		return 0;
    443 	}
    444 
    445 	mem = malloc(sizeof *mem, M_AGP, M_WAITOK);
    446 	if (mem == NULL)
    447 		return NULL;
    448 
    449 	if (bus_dmamap_create(sc->as_dmat, size, size / PAGE_SIZE + 1,
    450 			      size, 0, BUS_DMA_NOWAIT, &mem->am_dmamap) != 0) {
    451 		free(mem, M_AGP);
    452 		return NULL;
    453 	}
    454 
    455 	mem->am_id = sc->as_nextid++;
    456 	mem->am_size = size;
    457 	mem->am_type = 0;
    458 	mem->am_physical = 0;
    459 	mem->am_offset = 0;
    460 	mem->am_is_bound = 0;
    461 	TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
    462 	sc->as_allocated += size;
    463 
    464 	return mem;
    465 }
    466 
    467 int
    468 agp_generic_free_memory(struct agp_softc *sc, struct agp_memory *mem)
    469 {
    470 	if (mem->am_is_bound)
    471 		return EBUSY;
    472 
    473 	sc->as_allocated -= mem->am_size;
    474 	TAILQ_REMOVE(&sc->as_memory, mem, am_link);
    475 	bus_dmamap_destroy(sc->as_dmat, mem->am_dmamap);
    476 	free(mem, M_AGP);
    477 	return 0;
    478 }
    479 
    480 int
    481 agp_generic_bind_memory(struct agp_softc *sc, struct agp_memory *mem,
    482 			off_t offset)
    483 {
    484 	off_t i, k;
    485 	bus_size_t done, j;
    486 	int error;
    487 	bus_dma_segment_t *segs, *seg;
    488 	bus_addr_t pa;
    489 	int contigpages, nseg;
    490 
    491 	lockmgr(&sc->as_lock, LK_EXCLUSIVE, 0);
    492 
    493 	if (mem->am_is_bound) {
    494 		printf("%s: memory already bound\n", sc->as_dev.dv_xname);
    495 		lockmgr(&sc->as_lock, LK_RELEASE, 0);
    496 		return EINVAL;
    497 	}
    498 
    499 	if (offset < 0
    500 	    || (offset & (AGP_PAGE_SIZE - 1)) != 0
    501 	    || offset + mem->am_size > AGP_GET_APERTURE(sc)) {
    502 		printf("%s: binding memory at bad offset %#lx\n",
    503 			      sc->as_dev.dv_xname, (unsigned long) offset);
    504 		lockmgr(&sc->as_lock, LK_RELEASE, 0);
    505 		return EINVAL;
    506 	}
    507 
    508 	/*
    509 	 * XXXfvdl
    510 	 * The memory here needs to be directly accessable from the
    511 	 * AGP video card, so it should be allocated using bus_dma.
    512 	 * However, it need not be contiguous, since individual pages
    513 	 * are translated using the GATT.
    514 	 *
    515 	 * Using a large chunk of contiguous memory may get in the way
    516 	 * of other subsystems that may need one, so we try to be friendly
    517 	 * and ask for allocation in chunks of a minimum of 8 pages
    518 	 * of contiguous memory on average, falling back to 4, 2 and 1
    519 	 * if really needed. Larger chunks are preferred, since allocating
    520 	 * a bus_dma_segment per page would be overkill.
    521 	 */
    522 
    523 	for (contigpages = 8; contigpages > 0; contigpages >>= 1) {
    524 		nseg = (mem->am_size / (contigpages * PAGE_SIZE)) + 1;
    525 		segs = malloc(nseg * sizeof *segs, M_AGP, M_WAITOK);
    526 		if (segs == NULL) {
    527 			lockmgr(&sc->as_lock, LK_RELEASE, 0);
    528 			return ENOMEM;
    529 		}
    530 		if (bus_dmamem_alloc(sc->as_dmat, mem->am_size, PAGE_SIZE, 0,
    531 				     segs, nseg, &mem->am_nseg,
    532 				     contigpages > 1 ?
    533 				     BUS_DMA_NOWAIT : BUS_DMA_WAITOK) != 0) {
    534 			free(segs, M_AGP);
    535 			continue;
    536 		}
    537 		if (bus_dmamem_map(sc->as_dmat, segs, mem->am_nseg,
    538 		    mem->am_size, &mem->am_virtual, BUS_DMA_WAITOK) != 0) {
    539 			bus_dmamem_free(sc->as_dmat, segs, mem->am_nseg);
    540 			free(segs, M_AGP);
    541 			continue;
    542 		}
    543 		if (bus_dmamap_load(sc->as_dmat, mem->am_dmamap,
    544 		    mem->am_virtual, mem->am_size, NULL, BUS_DMA_WAITOK) != 0) {
    545 			bus_dmamem_unmap(sc->as_dmat, mem->am_virtual,
    546 			    mem->am_size);
    547 			bus_dmamem_free(sc->as_dmat, segs, mem->am_nseg);
    548 			free(segs, M_AGP);
    549 			continue;
    550 		}
    551 		mem->am_dmaseg = segs;
    552 		break;
    553 	}
    554 
    555 	if (contigpages == 0) {
    556 		lockmgr(&sc->as_lock, LK_RELEASE, 0);
    557 		return ENOMEM;
    558 	}
    559 
    560 
    561 	/*
    562 	 * Bind the individual pages and flush the chipset's
    563 	 * TLB.
    564 	 */
    565 	done = 0;
    566 	for (i = 0; i < mem->am_dmamap->dm_nsegs; i++) {
    567 		seg = &mem->am_dmamap->dm_segs[i];
    568 		/*
    569 		 * Install entries in the GATT, making sure that if
    570 		 * AGP_PAGE_SIZE < PAGE_SIZE and mem->am_size is not
    571 		 * aligned to PAGE_SIZE, we don't modify too many GATT
    572 		 * entries.
    573 		 */
    574 		for (j = 0; j < seg->ds_len && (done + j) < mem->am_size;
    575 		     j += AGP_PAGE_SIZE) {
    576 			pa = seg->ds_addr + j;
    577 			AGP_DPF("binding offset %#lx to pa %#lx\n",
    578 				(unsigned long)(offset + done + j),
    579 				(unsigned long)pa);
    580 			error = AGP_BIND_PAGE(sc, offset + done + j, pa);
    581 			if (error) {
    582 				/*
    583 				 * Bail out. Reverse all the mappings
    584 				 * and unwire the pages.
    585 				 */
    586 				for (k = 0; k < done + j; k += AGP_PAGE_SIZE)
    587 					AGP_UNBIND_PAGE(sc, offset + k);
    588 
    589 				bus_dmamap_unload(sc->as_dmat, mem->am_dmamap);
    590 				bus_dmamem_unmap(sc->as_dmat, mem->am_virtual,
    591 						 mem->am_size);
    592 				bus_dmamem_free(sc->as_dmat, mem->am_dmaseg,
    593 						mem->am_nseg);
    594 				free(mem->am_dmaseg, M_AGP);
    595 				lockmgr(&sc->as_lock, LK_RELEASE, 0);
    596 				return error;
    597 			}
    598 		}
    599 		done += seg->ds_len;
    600 	}
    601 
    602 	/*
    603 	 * Flush the cpu cache since we are providing a new mapping
    604 	 * for these pages.
    605 	 */
    606 	agp_flush_cache();
    607 
    608 	/*
    609 	 * Make sure the chipset gets the new mappings.
    610 	 */
    611 	AGP_FLUSH_TLB(sc);
    612 
    613 	mem->am_offset = offset;
    614 	mem->am_is_bound = 1;
    615 
    616 	lockmgr(&sc->as_lock, LK_RELEASE, 0);
    617 
    618 	return 0;
    619 }
    620 
    621 int
    622 agp_generic_unbind_memory(struct agp_softc *sc, struct agp_memory *mem)
    623 {
    624 	int i;
    625 
    626 	lockmgr(&sc->as_lock, LK_EXCLUSIVE, 0);
    627 
    628 	if (!mem->am_is_bound) {
    629 		printf("%s: memory is not bound\n", sc->as_dev.dv_xname);
    630 		lockmgr(&sc->as_lock, LK_RELEASE, 0);
    631 		return EINVAL;
    632 	}
    633 
    634 
    635 	/*
    636 	 * Unbind the individual pages and flush the chipset's
    637 	 * TLB. Unwire the pages so they can be swapped.
    638 	 */
    639 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
    640 		AGP_UNBIND_PAGE(sc, mem->am_offset + i);
    641 
    642 	agp_flush_cache();
    643 	AGP_FLUSH_TLB(sc);
    644 
    645 	bus_dmamap_unload(sc->as_dmat, mem->am_dmamap);
    646 	bus_dmamem_unmap(sc->as_dmat, mem->am_virtual, mem->am_size);
    647 	bus_dmamem_free(sc->as_dmat, mem->am_dmaseg, mem->am_nseg);
    648 
    649 	free(mem->am_dmaseg, M_AGP);
    650 
    651 	mem->am_offset = 0;
    652 	mem->am_is_bound = 0;
    653 
    654 	lockmgr(&sc->as_lock, LK_RELEASE, 0);
    655 
    656 	return 0;
    657 }
    658 
    659 /* Helper functions for implementing user/kernel api */
    660 
    661 static int
    662 agp_acquire_helper(struct agp_softc *sc, enum agp_acquire_state state)
    663 {
    664 	if (sc->as_state != AGP_ACQUIRE_FREE)
    665 		return EBUSY;
    666 	sc->as_state = state;
    667 
    668 	return 0;
    669 }
    670 
    671 static int
    672 agp_release_helper(struct agp_softc *sc, enum agp_acquire_state state)
    673 {
    674 	struct agp_memory *mem;
    675 
    676 	if (sc->as_state == AGP_ACQUIRE_FREE)
    677 		return 0;
    678 
    679 	if (sc->as_state != state)
    680 		return EBUSY;
    681 
    682 	/*
    683 	 * Clear out outstanding aperture mappings.
    684 	 * (should not be necessary, done by caller)
    685 	 */
    686 	TAILQ_FOREACH(mem, &sc->as_memory, am_link) {
    687 		if (mem->am_is_bound) {
    688 			printf("agp_release_helper: mem %d is bound\n",
    689 			       mem->am_id);
    690 			AGP_UNBIND_MEMORY(sc, mem);
    691 		}
    692 	}
    693 
    694 	sc->as_state = AGP_ACQUIRE_FREE;
    695 	return 0;
    696 }
    697 
    698 static struct agp_memory *
    699 agp_find_memory(struct agp_softc *sc, int id)
    700 {
    701 	struct agp_memory *mem;
    702 
    703 	AGP_DPF("searching for memory block %d\n", id);
    704 	TAILQ_FOREACH(mem, &sc->as_memory, am_link) {
    705 		AGP_DPF("considering memory block %d\n", mem->am_id);
    706 		if (mem->am_id == id)
    707 			return mem;
    708 	}
    709 	return 0;
    710 }
    711 
    712 /* Implementation of the userland ioctl api */
    713 
    714 static int
    715 agp_info_user(struct agp_softc *sc, agp_info *info)
    716 {
    717 	memset(info, 0, sizeof *info);
    718 	info->bridge_id = sc->as_id;
    719 	if (sc->as_capoff != 0)
    720 		info->agp_mode = pci_conf_read(sc->as_pc, sc->as_tag,
    721 					       sc->as_capoff + AGP_STATUS);
    722 	else
    723 		info->agp_mode = 0; /* i810 doesn't have real AGP */
    724 	info->aper_base = sc->as_apaddr;
    725 	info->aper_size = AGP_GET_APERTURE(sc) >> 20;
    726 	info->pg_total = info->pg_system = sc->as_maxmem >> AGP_PAGE_SHIFT;
    727 	info->pg_used = sc->as_allocated >> AGP_PAGE_SHIFT;
    728 
    729 	return 0;
    730 }
    731 
    732 static int
    733 agp_setup_user(struct agp_softc *sc, agp_setup *setup)
    734 {
    735 	return AGP_ENABLE(sc, setup->agp_mode);
    736 }
    737 
    738 static int
    739 agp_allocate_user(struct agp_softc *sc, agp_allocate *alloc)
    740 {
    741 	struct agp_memory *mem;
    742 
    743 	mem = AGP_ALLOC_MEMORY(sc,
    744 			       alloc->type,
    745 			       alloc->pg_count << AGP_PAGE_SHIFT);
    746 	if (mem) {
    747 		alloc->key = mem->am_id;
    748 		alloc->physical = mem->am_physical;
    749 		return 0;
    750 	} else {
    751 		return ENOMEM;
    752 	}
    753 }
    754 
    755 static int
    756 agp_deallocate_user(struct agp_softc *sc, int id)
    757 {
    758 	struct agp_memory *mem = agp_find_memory(sc, id);
    759 
    760 	if (mem) {
    761 		AGP_FREE_MEMORY(sc, mem);
    762 		return 0;
    763 	} else {
    764 		return ENOENT;
    765 	}
    766 }
    767 
    768 static int
    769 agp_bind_user(struct agp_softc *sc, agp_bind *bind)
    770 {
    771 	struct agp_memory *mem = agp_find_memory(sc, bind->key);
    772 
    773 	if (!mem)
    774 		return ENOENT;
    775 
    776 	return AGP_BIND_MEMORY(sc, mem, bind->pg_start << AGP_PAGE_SHIFT);
    777 }
    778 
    779 static int
    780 agp_unbind_user(struct agp_softc *sc, agp_unbind *unbind)
    781 {
    782 	struct agp_memory *mem = agp_find_memory(sc, unbind->key);
    783 
    784 	if (!mem)
    785 		return ENOENT;
    786 
    787 	return AGP_UNBIND_MEMORY(sc, mem);
    788 }
    789 
    790 int
    791 agpopen(dev_t dev, int oflags, int devtype, struct proc *p)
    792 {
    793 	struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
    794 
    795 	if (sc == NULL)
    796 		return ENXIO;
    797 
    798 	if (sc->as_chipc == NULL)
    799 		return ENXIO;
    800 
    801 	if (!sc->as_isopen)
    802 		sc->as_isopen = 1;
    803 	else
    804 		return EBUSY;
    805 
    806 	return 0;
    807 }
    808 
    809 int
    810 agpclose(dev_t dev, int fflag, int devtype, struct proc *p)
    811 {
    812 	struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
    813 	struct agp_memory *mem;
    814 
    815 	/*
    816 	 * Clear the GATT and force release on last close
    817 	 */
    818 	if (sc->as_state == AGP_ACQUIRE_USER) {
    819 		while ((mem = TAILQ_FIRST(&sc->as_memory))) {
    820 			if (mem->am_is_bound) {
    821 				printf("agpclose: mem %d is bound\n",
    822 				       mem->am_id);
    823 				AGP_UNBIND_MEMORY(sc, mem);
    824 			}
    825 			/*
    826 			 * XXX it is not documented, but if the protocol allows
    827 			 * allocate->acquire->bind, it would be possible that
    828 			 * memory ranges are allocated by the kernel here,
    829 			 * which we shouldn't free. We'd have to keep track of
    830 			 * the memory range's owner.
    831 			 * The kernel API is unsed yet, so we get away with
    832 			 * freeing all.
    833 			 */
    834 			AGP_FREE_MEMORY(sc, mem);
    835 		}
    836 		agp_release_helper(sc, AGP_ACQUIRE_USER);
    837 	}
    838 	sc->as_isopen = 0;
    839 
    840 	return 0;
    841 }
    842 
    843 int
    844 agpioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p)
    845 {
    846 	struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
    847 
    848 	if (sc == NULL)
    849 		return ENODEV;
    850 
    851 	if ((fflag & FWRITE) == 0 && cmd != AGPIOC_INFO)
    852 		return EPERM;
    853 
    854 	switch (cmd) {
    855 	case AGPIOC_INFO:
    856 		return agp_info_user(sc, (agp_info *) data);
    857 
    858 	case AGPIOC_ACQUIRE:
    859 		return agp_acquire_helper(sc, AGP_ACQUIRE_USER);
    860 
    861 	case AGPIOC_RELEASE:
    862 		return agp_release_helper(sc, AGP_ACQUIRE_USER);
    863 
    864 	case AGPIOC_SETUP:
    865 		return agp_setup_user(sc, (agp_setup *)data);
    866 
    867 	case AGPIOC_ALLOCATE:
    868 		return agp_allocate_user(sc, (agp_allocate *)data);
    869 
    870 	case AGPIOC_DEALLOCATE:
    871 		return agp_deallocate_user(sc, *(int *) data);
    872 
    873 	case AGPIOC_BIND:
    874 		return agp_bind_user(sc, (agp_bind *)data);
    875 
    876 	case AGPIOC_UNBIND:
    877 		return agp_unbind_user(sc, (agp_unbind *)data);
    878 
    879 	}
    880 
    881 	return EINVAL;
    882 }
    883 
    884 paddr_t
    885 agpmmap(dev_t dev, off_t offset, int prot)
    886 {
    887 	struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
    888 
    889 	if (offset > AGP_GET_APERTURE(sc))
    890 		return -1;
    891 
    892 	return (bus_space_mmap(sc->as_apt, sc->as_apaddr, offset, prot,
    893 	    BUS_SPACE_MAP_LINEAR));
    894 }
    895 
    896 /* Implementation of the kernel api */
    897 
    898 void *
    899 agp_find_device(int unit)
    900 {
    901 	return device_lookup(&agp_cd, unit);
    902 }
    903 
    904 enum agp_acquire_state
    905 agp_state(void *devcookie)
    906 {
    907 	struct agp_softc *sc = devcookie;
    908 	return sc->as_state;
    909 }
    910 
    911 void
    912 agp_get_info(void *devcookie, struct agp_info *info)
    913 {
    914 	struct agp_softc *sc = devcookie;
    915 
    916 	info->ai_mode = pci_conf_read(sc->as_pc, sc->as_tag,
    917 	    sc->as_capoff + AGP_STATUS);
    918 	info->ai_aperture_base = sc->as_apaddr;
    919 	info->ai_aperture_size = sc->as_apsize;	/* XXXfvdl inconsistent */
    920 	info->ai_memory_allowed = sc->as_maxmem;
    921 	info->ai_memory_used = sc->as_allocated;
    922 }
    923 
    924 int
    925 agp_acquire(void *dev)
    926 {
    927 	return agp_acquire_helper(dev, AGP_ACQUIRE_KERNEL);
    928 }
    929 
    930 int
    931 agp_release(void *dev)
    932 {
    933 	return agp_release_helper(dev, AGP_ACQUIRE_KERNEL);
    934 }
    935 
    936 int
    937 agp_enable(void *dev, u_int32_t mode)
    938 {
    939 	struct agp_softc *sc = dev;
    940 
    941 	return AGP_ENABLE(sc, mode);
    942 }
    943 
    944 void *agp_alloc_memory(void *dev, int type, vsize_t bytes)
    945 {
    946 	struct agp_softc *sc = dev;
    947 
    948 	return (void *)AGP_ALLOC_MEMORY(sc, type, bytes);
    949 }
    950 
    951 void agp_free_memory(void *dev, void *handle)
    952 {
    953 	struct agp_softc *sc = dev;
    954 	struct agp_memory *mem = (struct agp_memory *) handle;
    955 	AGP_FREE_MEMORY(sc, mem);
    956 }
    957 
    958 int agp_bind_memory(void *dev, void *handle, off_t offset)
    959 {
    960 	struct agp_softc *sc = dev;
    961 	struct agp_memory *mem = (struct agp_memory *) handle;
    962 
    963 	return AGP_BIND_MEMORY(sc, mem, offset);
    964 }
    965 
    966 int agp_unbind_memory(void *dev, void *handle)
    967 {
    968 	struct agp_softc *sc = dev;
    969 	struct agp_memory *mem = (struct agp_memory *) handle;
    970 
    971 	return AGP_UNBIND_MEMORY(sc, mem);
    972 }
    973 
    974 void agp_memory_info(void *dev, void *handle, struct agp_memory_info *mi)
    975 {
    976 	struct agp_memory *mem = (struct agp_memory *) handle;
    977 
    978 	mi->ami_size = mem->am_size;
    979 	mi->ami_physical = mem->am_physical;
    980 	mi->ami_offset = mem->am_offset;
    981 	mi->ami_is_bound = mem->am_is_bound;
    982 }
    983 
    984 int
    985 agp_alloc_dmamem(bus_dma_tag_t tag, size_t size, int flags,
    986 		 bus_dmamap_t *mapp, caddr_t *vaddr, bus_addr_t *baddr,
    987 		 bus_dma_segment_t *seg, int nseg, int *rseg)
    988 
    989 {
    990 	int error, level = 0;
    991 
    992 	if ((error = bus_dmamem_alloc(tag, size, PAGE_SIZE, 0,
    993 			seg, nseg, rseg, BUS_DMA_NOWAIT)) != 0)
    994 		goto out;
    995 	level++;
    996 
    997 	if ((error = bus_dmamem_map(tag, seg, *rseg, size, vaddr,
    998 			BUS_DMA_NOWAIT | flags)) != 0)
    999 		goto out;
   1000 	level++;
   1001 
   1002 	if ((error = bus_dmamap_create(tag, size, *rseg, size, 0,
   1003 			BUS_DMA_NOWAIT, mapp)) != 0)
   1004 		goto out;
   1005 	level++;
   1006 
   1007 	if ((error = bus_dmamap_load(tag, *mapp, *vaddr, size, NULL,
   1008 			BUS_DMA_NOWAIT)) != 0)
   1009 		goto out;
   1010 
   1011 	*baddr = (*mapp)->dm_segs[0].ds_addr;
   1012 
   1013 	return 0;
   1014 out:
   1015 	switch (level) {
   1016 	case 3:
   1017 		bus_dmamap_destroy(tag, *mapp);
   1018 		/* FALLTHROUGH */
   1019 	case 2:
   1020 		bus_dmamem_unmap(tag, *vaddr, size);
   1021 		/* FALLTHROUGH */
   1022 	case 1:
   1023 		bus_dmamem_free(tag, seg, *rseg);
   1024 		break;
   1025 	default:
   1026 		break;
   1027 	}
   1028 
   1029 	return error;
   1030 }
   1031 
   1032 void
   1033 agp_free_dmamem(bus_dma_tag_t tag, size_t size, bus_dmamap_t map,
   1034 		caddr_t vaddr, bus_dma_segment_t *seg, int nseg)
   1035 {
   1036 
   1037 	bus_dmamap_unload(tag, map);
   1038 	bus_dmamap_destroy(tag, map);
   1039 	bus_dmamem_unmap(tag, vaddr, size);
   1040 	bus_dmamem_free(tag, seg, nseg);
   1041 }
   1042