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