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