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