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