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