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