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