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