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