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