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