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