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