Home | History | Annotate | Line # | Download | only in pci
agp.c revision 1.28.2.1
      1  1.28.2.1   darrenr /*	$NetBSD: agp.c,v 1.28.2.1 2003/07/02 15:26:10 darrenr Exp $	*/
      2       1.1      fvdl 
      3       1.1      fvdl /*-
      4       1.1      fvdl  * Copyright (c) 2000 Doug Rabson
      5       1.1      fvdl  * All rights reserved.
      6       1.1      fvdl  *
      7       1.1      fvdl  * Redistribution and use in source and binary forms, with or without
      8       1.1      fvdl  * modification, are permitted provided that the following conditions
      9       1.1      fvdl  * are met:
     10       1.1      fvdl  * 1. Redistributions of source code must retain the above copyright
     11       1.1      fvdl  *    notice, this list of conditions and the following disclaimer.
     12       1.1      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1      fvdl  *    notice, this list of conditions and the following disclaimer in the
     14       1.1      fvdl  *    documentation and/or other materials provided with the distribution.
     15       1.1      fvdl  *
     16       1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17       1.1      fvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18       1.1      fvdl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19       1.1      fvdl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20       1.1      fvdl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21       1.1      fvdl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22       1.1      fvdl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23       1.1      fvdl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24       1.1      fvdl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25       1.1      fvdl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26       1.1      fvdl  * SUCH DAMAGE.
     27       1.1      fvdl  *
     28       1.1      fvdl  *	$FreeBSD: src/sys/pci/agp.c,v 1.12 2001/05/19 01:28:07 alfred Exp $
     29       1.1      fvdl  */
     30       1.1      fvdl 
     31       1.1      fvdl /*
     32       1.1      fvdl  * Copyright (c) 2001 Wasabi Systems, Inc.
     33       1.1      fvdl  * All rights reserved.
     34       1.1      fvdl  *
     35       1.1      fvdl  * Written by Frank van der Linden for Wasabi Systems, Inc.
     36       1.1      fvdl  *
     37       1.1      fvdl  * Redistribution and use in source and binary forms, with or without
     38       1.1      fvdl  * modification, are permitted provided that the following conditions
     39       1.1      fvdl  * are met:
     40       1.1      fvdl  * 1. Redistributions of source code must retain the above copyright
     41       1.1      fvdl  *    notice, this list of conditions and the following disclaimer.
     42       1.1      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     43       1.1      fvdl  *    notice, this list of conditions and the following disclaimer in the
     44       1.1      fvdl  *    documentation and/or other materials provided with the distribution.
     45       1.1      fvdl  * 3. All advertising materials mentioning features or use of this software
     46       1.1      fvdl  *    must display the following acknowledgement:
     47       1.1      fvdl  *      This product includes software developed for the NetBSD Project by
     48       1.1      fvdl  *      Wasabi Systems, Inc.
     49       1.1      fvdl  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     50       1.1      fvdl  *    or promote products derived from this software without specific prior
     51       1.1      fvdl  *    written permission.
     52       1.1      fvdl  *
     53       1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     54       1.1      fvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     55       1.1      fvdl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     56       1.1      fvdl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     57       1.1      fvdl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     58       1.1      fvdl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     59       1.1      fvdl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     60       1.1      fvdl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     61       1.1      fvdl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     62       1.1      fvdl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     63       1.1      fvdl  * POSSIBILITY OF SUCH DAMAGE.
     64       1.1      fvdl  */
     65       1.1      fvdl 
     66      1.12     lukem 
     67      1.12     lukem #include <sys/cdefs.h>
     68  1.28.2.1   darrenr __KERNEL_RCSID(0, "$NetBSD: agp.c,v 1.28.2.1 2003/07/02 15:26:10 darrenr Exp $");
     69       1.1      fvdl 
     70       1.1      fvdl #include <sys/param.h>
     71       1.1      fvdl #include <sys/systm.h>
     72       1.1      fvdl #include <sys/malloc.h>
     73       1.1      fvdl #include <sys/kernel.h>
     74       1.1      fvdl #include <sys/device.h>
     75       1.1      fvdl #include <sys/conf.h>
     76       1.1      fvdl #include <sys/ioctl.h>
     77       1.1      fvdl #include <sys/fcntl.h>
     78       1.1      fvdl #include <sys/agpio.h>
     79       1.1      fvdl #include <sys/proc.h>
     80       1.1      fvdl 
     81       1.1      fvdl #include <uvm/uvm_extern.h>
     82       1.1      fvdl 
     83       1.1      fvdl #include <dev/pci/pcireg.h>
     84       1.1      fvdl #include <dev/pci/pcivar.h>
     85       1.1      fvdl #include <dev/pci/agpvar.h>
     86       1.1      fvdl #include <dev/pci/agpreg.h>
     87       1.1      fvdl #include <dev/pci/pcidevs.h>
     88       1.1      fvdl 
     89       1.1      fvdl #include <machine/bus.h>
     90      1.25   thorpej 
     91      1.25   thorpej MALLOC_DEFINE(M_AGP, "AGP", "AGP memory");
     92       1.1      fvdl 
     93       1.1      fvdl /* Helper functions for implementing chipset mini drivers. */
     94       1.1      fvdl /* XXXfvdl get rid of this one. */
     95       1.1      fvdl 
     96       1.1      fvdl extern struct cfdriver agp_cd;
     97      1.17   gehenna 
     98      1.17   gehenna dev_type_open(agpopen);
     99      1.17   gehenna dev_type_close(agpclose);
    100      1.17   gehenna dev_type_ioctl(agpioctl);
    101      1.17   gehenna dev_type_mmap(agpmmap);
    102      1.17   gehenna 
    103      1.17   gehenna const struct cdevsw agp_cdevsw = {
    104      1.17   gehenna 	agpopen, agpclose, noread, nowrite, agpioctl,
    105      1.22  jdolecek 	nostop, notty, nopoll, agpmmap, nokqfilter,
    106      1.17   gehenna };
    107       1.1      fvdl 
    108       1.1      fvdl int agpmatch(struct device *, struct cfdata *, void *);
    109       1.1      fvdl void agpattach(struct device *, struct device *, void *);
    110       1.1      fvdl 
    111      1.20   thorpej CFATTACH_DECL(agp, sizeof(struct agp_softc),
    112      1.21   thorpej     agpmatch, agpattach, NULL, NULL);
    113       1.1      fvdl 
    114       1.1      fvdl static int agp_info_user(struct agp_softc *, agp_info *);
    115       1.1      fvdl static int agp_setup_user(struct agp_softc *, agp_setup *);
    116       1.1      fvdl static int agp_allocate_user(struct agp_softc *, agp_allocate *);
    117       1.1      fvdl static int agp_deallocate_user(struct agp_softc *, int);
    118       1.1      fvdl static int agp_bind_user(struct agp_softc *, agp_bind *);
    119       1.1      fvdl static int agp_unbind_user(struct agp_softc *, agp_unbind *);
    120       1.1      fvdl static int agpdev_match(struct pci_attach_args *);
    121       1.1      fvdl 
    122       1.7   thorpej #include "agp_ali.h"
    123       1.7   thorpej #include "agp_amd.h"
    124       1.7   thorpej #include "agp_i810.h"
    125       1.7   thorpej #include "agp_intel.h"
    126       1.7   thorpej #include "agp_sis.h"
    127       1.7   thorpej #include "agp_via.h"
    128       1.7   thorpej 
    129       1.5   thorpej const struct agp_product {
    130       1.5   thorpej 	uint32_t	ap_vendor;
    131       1.5   thorpej 	uint32_t	ap_product;
    132       1.5   thorpej 	int		(*ap_match)(const struct pci_attach_args *);
    133       1.5   thorpej 	int		(*ap_attach)(struct device *, struct device *, void *);
    134       1.5   thorpej } agp_products[] = {
    135       1.7   thorpej #if NAGP_ALI > 0
    136       1.5   thorpej 	{ PCI_VENDOR_ALI,	-1,
    137       1.5   thorpej 	  NULL,			agp_ali_attach },
    138       1.7   thorpej #endif
    139       1.5   thorpej 
    140       1.7   thorpej #if NAGP_AMD > 0
    141       1.5   thorpej 	{ PCI_VENDOR_AMD,	-1,
    142       1.5   thorpej 	  agp_amd_match,	agp_amd_attach },
    143       1.7   thorpej #endif
    144       1.5   thorpej 
    145       1.7   thorpej #if NAGP_I810 > 0
    146       1.5   thorpej 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82810_MCH,
    147       1.5   thorpej 	  NULL,			agp_i810_attach },
    148       1.5   thorpej 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82810_DC100_MCH,
    149       1.5   thorpej 	  NULL,			agp_i810_attach },
    150       1.5   thorpej 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82810E_MCH,
    151       1.5   thorpej 	  NULL,			agp_i810_attach },
    152       1.5   thorpej 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82815_FULL_HUB,
    153       1.5   thorpej 	  NULL,			agp_i810_attach },
    154      1.11      fvdl 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82840_HB,
    155      1.13  augustss 	  NULL,			agp_i810_attach },
    156      1.13  augustss 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82830MP_IO_1,
    157      1.11      fvdl 	  NULL,			agp_i810_attach },
    158      1.23       scw 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82845G_DRAM,
    159      1.23       scw 	  NULL,			agp_i810_attach },
    160       1.7   thorpej #endif
    161       1.5   thorpej 
    162       1.7   thorpej #if NAGP_INTEL > 0
    163       1.5   thorpej 	{ PCI_VENDOR_INTEL,	-1,
    164       1.5   thorpej 	  NULL,			agp_intel_attach },
    165       1.7   thorpej #endif
    166       1.5   thorpej 
    167       1.7   thorpej #if NAGP_SIS > 0
    168       1.5   thorpej 	{ PCI_VENDOR_SIS,	-1,
    169       1.5   thorpej 	  NULL,			agp_sis_attach },
    170       1.7   thorpej #endif
    171       1.5   thorpej 
    172       1.7   thorpej #if NAGP_VIA > 0
    173       1.5   thorpej 	{ PCI_VENDOR_VIATECH,	-1,
    174       1.5   thorpej 	  NULL,			agp_via_attach },
    175       1.7   thorpej #endif
    176       1.5   thorpej 
    177       1.5   thorpej 	{ 0,			0,
    178       1.5   thorpej 	  NULL,			NULL },
    179       1.5   thorpej };
    180       1.5   thorpej 
    181       1.5   thorpej static const struct agp_product *
    182       1.5   thorpej agp_lookup(const struct pci_attach_args *pa)
    183       1.5   thorpej {
    184       1.5   thorpej 	const struct agp_product *ap;
    185       1.5   thorpej 
    186       1.5   thorpej 	/* First find the vendor. */
    187       1.5   thorpej 	for (ap = agp_products; ap->ap_attach != NULL; ap++) {
    188       1.5   thorpej 		if (PCI_VENDOR(pa->pa_id) == ap->ap_vendor)
    189       1.5   thorpej 			break;
    190       1.5   thorpej 	}
    191       1.5   thorpej 
    192       1.5   thorpej 	if (ap->ap_attach == NULL)
    193       1.5   thorpej 		return (NULL);
    194       1.5   thorpej 
    195       1.5   thorpej 	/* Now find the product within the vendor's domain. */
    196       1.5   thorpej 	for (; ap->ap_attach != NULL; ap++) {
    197       1.5   thorpej 		if (PCI_VENDOR(pa->pa_id) != ap->ap_vendor) {
    198       1.5   thorpej 			/* Ran out of this vendor's section of the table. */
    199       1.5   thorpej 			return (NULL);
    200       1.5   thorpej 		}
    201       1.5   thorpej 		if (ap->ap_product == PCI_PRODUCT(pa->pa_id)) {
    202       1.5   thorpej 			/* Exact match. */
    203       1.5   thorpej 			break;
    204       1.5   thorpej 		}
    205       1.5   thorpej 		if (ap->ap_product == (uint32_t) -1) {
    206       1.5   thorpej 			/* Wildcard match. */
    207       1.5   thorpej 			break;
    208       1.5   thorpej 		}
    209       1.5   thorpej 	}
    210       1.5   thorpej 
    211       1.5   thorpej 	if (ap->ap_attach == NULL)
    212       1.5   thorpej 		return (NULL);
    213       1.5   thorpej 
    214       1.5   thorpej 	/* Now let the product-specific driver filter the match. */
    215       1.5   thorpej 	if (ap->ap_match != NULL && (*ap->ap_match)(pa) == 0)
    216       1.5   thorpej 		return (NULL);
    217       1.5   thorpej 
    218       1.5   thorpej 	return (ap);
    219       1.5   thorpej }
    220       1.5   thorpej 
    221       1.1      fvdl int
    222       1.1      fvdl agpmatch(struct device *parent, struct cfdata *match, void *aux)
    223       1.1      fvdl {
    224       1.5   thorpej 	struct agpbus_attach_args *apa = aux;
    225       1.1      fvdl 	struct pci_attach_args *pa = &apa->apa_pci_args;
    226       1.1      fvdl 
    227       1.5   thorpej 	if (strcmp(apa->apa_busname, "agp") != 0)
    228       1.5   thorpej 		return (0);
    229       1.5   thorpej 
    230       1.5   thorpej 	if (agp_lookup(pa) == NULL)
    231       1.5   thorpej 		return (0);
    232       1.1      fvdl 
    233       1.5   thorpej 	return (1);
    234       1.1      fvdl }
    235       1.1      fvdl 
    236       1.1      fvdl static int agp_max[][2] = {
    237       1.1      fvdl 	{0,	0},
    238       1.1      fvdl 	{32,	4},
    239       1.1      fvdl 	{64,	28},
    240       1.1      fvdl 	{128,	96},
    241       1.1      fvdl 	{256,	204},
    242       1.1      fvdl 	{512,	440},
    243       1.1      fvdl 	{1024,	942},
    244       1.1      fvdl 	{2048,	1920},
    245       1.1      fvdl 	{4096,	3932}
    246       1.1      fvdl };
    247       1.1      fvdl #define agp_max_size	(sizeof(agp_max) / sizeof(agp_max[0]))
    248       1.1      fvdl 
    249       1.1      fvdl void
    250       1.1      fvdl agpattach(struct device *parent, struct device *self, void *aux)
    251       1.1      fvdl {
    252       1.5   thorpej 	struct agpbus_attach_args *apa = aux;
    253       1.1      fvdl 	struct pci_attach_args *pa = &apa->apa_pci_args;
    254       1.1      fvdl 	struct agp_softc *sc = (void *)self;
    255       1.5   thorpej 	const struct agp_product *ap;
    256       1.1      fvdl 	int memsize, i, ret;
    257       1.1      fvdl 
    258       1.5   thorpej 	ap = agp_lookup(pa);
    259       1.5   thorpej 	if (ap == NULL) {
    260       1.5   thorpej 		printf("\n");
    261       1.5   thorpej 		panic("agpattach: impossible");
    262       1.5   thorpej 	}
    263       1.1      fvdl 
    264      1.24   thorpej 	aprint_naive(": AGP controller\n");
    265      1.24   thorpej 
    266       1.1      fvdl 	sc->as_dmat = pa->pa_dmat;
    267       1.1      fvdl 	sc->as_pc = pa->pa_pc;
    268       1.1      fvdl 	sc->as_tag = pa->pa_tag;
    269       1.1      fvdl 	sc->as_id = pa->pa_id;
    270       1.1      fvdl 
    271       1.1      fvdl 	/*
    272       1.1      fvdl 	 * Work out an upper bound for agp memory allocation. This
    273       1.1      fvdl 	 * uses a heurisitc table from the Linux driver.
    274       1.1      fvdl 	 */
    275       1.1      fvdl 	memsize = ptoa(physmem) >> 20;
    276       1.1      fvdl 	for (i = 0; i < agp_max_size; i++) {
    277       1.1      fvdl 		if (memsize <= agp_max[i][0])
    278       1.1      fvdl 			break;
    279       1.1      fvdl 	}
    280       1.1      fvdl 	if (i == agp_max_size)
    281       1.1      fvdl 		i = agp_max_size - 1;
    282       1.1      fvdl 	sc->as_maxmem = agp_max[i][1] << 20U;
    283       1.1      fvdl 
    284       1.1      fvdl 	/*
    285       1.1      fvdl 	 * The lock is used to prevent re-entry to
    286       1.1      fvdl 	 * agp_generic_bind_memory() since that function can sleep.
    287       1.1      fvdl 	 */
    288       1.1      fvdl 	lockinit(&sc->as_lock, PZERO|PCATCH, "agplk", 0, 0);
    289       1.1      fvdl 
    290       1.1      fvdl 	TAILQ_INIT(&sc->as_memory);
    291       1.1      fvdl 
    292       1.5   thorpej 	ret = (*ap->ap_attach)(parent, self, pa);
    293       1.1      fvdl 	if (ret == 0)
    294      1.24   thorpej 		aprint_normal(": aperture at 0x%lx, size 0x%lx\n",
    295       1.1      fvdl 		    (unsigned long)sc->as_apaddr,
    296       1.1      fvdl 		    (unsigned long)AGP_GET_APERTURE(sc));
    297       1.1      fvdl 	else
    298       1.1      fvdl 		sc->as_chipc = NULL;
    299       1.1      fvdl }
    300       1.1      fvdl int
    301       1.1      fvdl agp_map_aperture(struct pci_attach_args *pa, struct agp_softc *sc)
    302       1.1      fvdl {
    303       1.1      fvdl 	/*
    304      1.18   nathanw 	 * Find the aperture. Don't map it (yet), this would
    305      1.11      fvdl 	 * eat KVA.
    306       1.1      fvdl 	 */
    307      1.11      fvdl 	if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, AGP_APBASE,
    308      1.11      fvdl 	    PCI_MAPREG_TYPE_MEM, &sc->as_apaddr, &sc->as_apsize,
    309      1.11      fvdl 	    &sc->as_apflags) != 0)
    310       1.1      fvdl 		return ENXIO;
    311       1.8  drochner 
    312      1.11      fvdl 	sc->as_apt = pa->pa_memt;
    313      1.11      fvdl 
    314       1.1      fvdl 	return 0;
    315       1.1      fvdl }
    316       1.1      fvdl 
    317       1.1      fvdl struct agp_gatt *
    318       1.1      fvdl agp_alloc_gatt(struct agp_softc *sc)
    319       1.1      fvdl {
    320       1.1      fvdl 	u_int32_t apsize = AGP_GET_APERTURE(sc);
    321       1.1      fvdl 	u_int32_t entries = apsize >> AGP_PAGE_SHIFT;
    322       1.1      fvdl 	struct agp_gatt *gatt;
    323       1.1      fvdl 	int dummyseg;
    324       1.1      fvdl 
    325       1.1      fvdl 	gatt = malloc(sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
    326       1.1      fvdl 	if (!gatt)
    327       1.1      fvdl 		return NULL;
    328       1.1      fvdl 	gatt->ag_entries = entries;
    329       1.1      fvdl 
    330       1.1      fvdl 	if (agp_alloc_dmamem(sc->as_dmat, entries * sizeof(u_int32_t),
    331       1.1      fvdl 	    0, &gatt->ag_dmamap, (caddr_t *)&gatt->ag_virtual,
    332       1.1      fvdl 	    &gatt->ag_physical, &gatt->ag_dmaseg, 1, &dummyseg) != 0)
    333       1.1      fvdl 		return NULL;
    334       1.1      fvdl 
    335       1.1      fvdl 	gatt->ag_size = entries * sizeof(u_int32_t);
    336       1.1      fvdl 	memset(gatt->ag_virtual, 0, gatt->ag_size);
    337       1.1      fvdl 	agp_flush_cache();
    338       1.1      fvdl 
    339       1.1      fvdl 	return gatt;
    340       1.1      fvdl }
    341       1.1      fvdl 
    342       1.1      fvdl void
    343       1.1      fvdl agp_free_gatt(struct agp_softc *sc, struct agp_gatt *gatt)
    344       1.1      fvdl {
    345       1.1      fvdl 	agp_free_dmamem(sc->as_dmat, gatt->ag_size, gatt->ag_dmamap,
    346       1.1      fvdl 	    (caddr_t)gatt->ag_virtual, &gatt->ag_dmaseg, 1);
    347       1.1      fvdl 	free(gatt, M_AGP);
    348       1.1      fvdl }
    349       1.1      fvdl 
    350       1.1      fvdl 
    351       1.1      fvdl int
    352       1.1      fvdl agp_generic_detach(struct agp_softc *sc)
    353       1.1      fvdl {
    354       1.1      fvdl 	lockmgr(&sc->as_lock, LK_DRAIN, 0);
    355       1.1      fvdl 	agp_flush_cache();
    356       1.1      fvdl 	return 0;
    357       1.1      fvdl }
    358       1.1      fvdl 
    359       1.1      fvdl static int
    360       1.1      fvdl agpdev_match(struct pci_attach_args *pa)
    361       1.1      fvdl {
    362       1.1      fvdl 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY &&
    363       1.1      fvdl 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_DISPLAY_VGA)
    364      1.26      tron 		if (pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP,
    365      1.26      tron 		    NULL, NULL))
    366       1.1      fvdl 		return 1;
    367       1.1      fvdl 
    368       1.1      fvdl 	return 0;
    369       1.1      fvdl }
    370       1.1      fvdl 
    371       1.1      fvdl int
    372       1.1      fvdl agp_generic_enable(struct agp_softc *sc, u_int32_t mode)
    373       1.1      fvdl {
    374       1.1      fvdl 	struct pci_attach_args pa;
    375       1.1      fvdl 	pcireg_t tstatus, mstatus;
    376       1.1      fvdl 	pcireg_t command;
    377       1.1      fvdl 	int rq, sba, fw, rate, capoff;
    378       1.1      fvdl 
    379       1.1      fvdl 	if (pci_find_device(&pa, agpdev_match) == 0 ||
    380       1.1      fvdl 	    pci_get_capability(pa.pa_pc, pa.pa_tag, PCI_CAP_AGP,
    381       1.1      fvdl 	     &capoff, NULL) == 0) {
    382       1.1      fvdl 		printf("%s: can't find display\n", sc->as_dev.dv_xname);
    383       1.1      fvdl 		return ENXIO;
    384       1.1      fvdl 	}
    385       1.1      fvdl 
    386       1.1      fvdl 	tstatus = pci_conf_read(sc->as_pc, sc->as_tag,
    387       1.1      fvdl 	    sc->as_capoff + AGP_STATUS);
    388       1.1      fvdl 	mstatus = pci_conf_read(pa.pa_pc, pa.pa_tag,
    389       1.1      fvdl 	    capoff + AGP_STATUS);
    390       1.1      fvdl 
    391       1.1      fvdl 	/* Set RQ to the min of mode, tstatus and mstatus */
    392       1.1      fvdl 	rq = AGP_MODE_GET_RQ(mode);
    393       1.1      fvdl 	if (AGP_MODE_GET_RQ(tstatus) < rq)
    394       1.1      fvdl 		rq = AGP_MODE_GET_RQ(tstatus);
    395       1.1      fvdl 	if (AGP_MODE_GET_RQ(mstatus) < rq)
    396       1.1      fvdl 		rq = AGP_MODE_GET_RQ(mstatus);
    397       1.1      fvdl 
    398       1.1      fvdl 	/* Set SBA if all three can deal with SBA */
    399       1.1      fvdl 	sba = (AGP_MODE_GET_SBA(tstatus)
    400       1.1      fvdl 	       & AGP_MODE_GET_SBA(mstatus)
    401       1.1      fvdl 	       & AGP_MODE_GET_SBA(mode));
    402       1.1      fvdl 
    403       1.1      fvdl 	/* Similar for FW */
    404       1.1      fvdl 	fw = (AGP_MODE_GET_FW(tstatus)
    405       1.1      fvdl 	       & AGP_MODE_GET_FW(mstatus)
    406       1.1      fvdl 	       & AGP_MODE_GET_FW(mode));
    407       1.1      fvdl 
    408       1.1      fvdl 	/* Figure out the max rate */
    409       1.1      fvdl 	rate = (AGP_MODE_GET_RATE(tstatus)
    410       1.1      fvdl 		& AGP_MODE_GET_RATE(mstatus)
    411       1.1      fvdl 		& AGP_MODE_GET_RATE(mode));
    412       1.1      fvdl 	if (rate & AGP_MODE_RATE_4x)
    413       1.1      fvdl 		rate = AGP_MODE_RATE_4x;
    414       1.1      fvdl 	else if (rate & AGP_MODE_RATE_2x)
    415       1.1      fvdl 		rate = AGP_MODE_RATE_2x;
    416       1.1      fvdl 	else
    417       1.1      fvdl 		rate = AGP_MODE_RATE_1x;
    418       1.1      fvdl 
    419       1.1      fvdl 	/* Construct the new mode word and tell the hardware */
    420       1.1      fvdl 	command = AGP_MODE_SET_RQ(0, rq);
    421       1.1      fvdl 	command = AGP_MODE_SET_SBA(command, sba);
    422       1.1      fvdl 	command = AGP_MODE_SET_FW(command, fw);
    423       1.1      fvdl 	command = AGP_MODE_SET_RATE(command, rate);
    424       1.1      fvdl 	command = AGP_MODE_SET_AGP(command, 1);
    425       1.1      fvdl 	pci_conf_write(sc->as_pc, sc->as_tag,
    426       1.1      fvdl 	    sc->as_capoff + AGP_COMMAND, command);
    427       1.1      fvdl 	pci_conf_write(pa.pa_pc, pa.pa_tag, capoff + AGP_COMMAND, command);
    428       1.1      fvdl 
    429       1.1      fvdl 	return 0;
    430       1.1      fvdl }
    431       1.1      fvdl 
    432       1.1      fvdl struct agp_memory *
    433       1.1      fvdl agp_generic_alloc_memory(struct agp_softc *sc, int type, vsize_t size)
    434       1.1      fvdl {
    435       1.1      fvdl 	struct agp_memory *mem;
    436       1.1      fvdl 
    437       1.1      fvdl 	if ((size & (AGP_PAGE_SIZE - 1)) != 0)
    438       1.1      fvdl 		return 0;
    439       1.1      fvdl 
    440       1.1      fvdl 	if (sc->as_allocated + size > sc->as_maxmem)
    441       1.1      fvdl 		return 0;
    442       1.1      fvdl 
    443       1.1      fvdl 	if (type != 0) {
    444       1.1      fvdl 		printf("agp_generic_alloc_memory: unsupported type %d\n",
    445       1.1      fvdl 		       type);
    446       1.1      fvdl 		return 0;
    447       1.1      fvdl 	}
    448       1.1      fvdl 
    449       1.1      fvdl 	mem = malloc(sizeof *mem, M_AGP, M_WAITOK);
    450       1.1      fvdl 	if (mem == NULL)
    451       1.1      fvdl 		return NULL;
    452       1.1      fvdl 
    453       1.3  drochner 	if (bus_dmamap_create(sc->as_dmat, size, size / PAGE_SIZE + 1,
    454       1.3  drochner 			      size, 0, BUS_DMA_NOWAIT, &mem->am_dmamap) != 0) {
    455       1.1      fvdl 		free(mem, M_AGP);
    456       1.1      fvdl 		return NULL;
    457       1.1      fvdl 	}
    458       1.1      fvdl 
    459       1.1      fvdl 	mem->am_id = sc->as_nextid++;
    460       1.1      fvdl 	mem->am_size = size;
    461       1.1      fvdl 	mem->am_type = 0;
    462       1.1      fvdl 	mem->am_physical = 0;
    463       1.1      fvdl 	mem->am_offset = 0;
    464       1.1      fvdl 	mem->am_is_bound = 0;
    465       1.1      fvdl 	TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
    466       1.1      fvdl 	sc->as_allocated += size;
    467       1.1      fvdl 
    468       1.1      fvdl 	return mem;
    469       1.1      fvdl }
    470       1.1      fvdl 
    471       1.1      fvdl int
    472       1.1      fvdl agp_generic_free_memory(struct agp_softc *sc, struct agp_memory *mem)
    473       1.1      fvdl {
    474       1.1      fvdl 	if (mem->am_is_bound)
    475       1.1      fvdl 		return EBUSY;
    476       1.1      fvdl 
    477       1.1      fvdl 	sc->as_allocated -= mem->am_size;
    478       1.1      fvdl 	TAILQ_REMOVE(&sc->as_memory, mem, am_link);
    479       1.1      fvdl 	bus_dmamap_destroy(sc->as_dmat, mem->am_dmamap);
    480       1.1      fvdl 	free(mem, M_AGP);
    481       1.1      fvdl 	return 0;
    482       1.1      fvdl }
    483       1.1      fvdl 
    484       1.1      fvdl int
    485       1.1      fvdl agp_generic_bind_memory(struct agp_softc *sc, struct agp_memory *mem,
    486       1.1      fvdl 			off_t offset)
    487       1.1      fvdl {
    488       1.1      fvdl 	off_t i, k;
    489       1.1      fvdl 	bus_size_t done, j;
    490       1.1      fvdl 	int error;
    491       1.1      fvdl 	bus_dma_segment_t *segs, *seg;
    492       1.1      fvdl 	bus_addr_t pa;
    493       1.1      fvdl 	int contigpages, nseg;
    494       1.1      fvdl 
    495       1.1      fvdl 	lockmgr(&sc->as_lock, LK_EXCLUSIVE, 0);
    496       1.1      fvdl 
    497       1.1      fvdl 	if (mem->am_is_bound) {
    498       1.1      fvdl 		printf("%s: memory already bound\n", sc->as_dev.dv_xname);
    499       1.1      fvdl 		lockmgr(&sc->as_lock, LK_RELEASE, 0);
    500       1.1      fvdl 		return EINVAL;
    501       1.1      fvdl 	}
    502       1.1      fvdl 
    503       1.1      fvdl 	if (offset < 0
    504       1.1      fvdl 	    || (offset & (AGP_PAGE_SIZE - 1)) != 0
    505       1.1      fvdl 	    || offset + mem->am_size > AGP_GET_APERTURE(sc)) {
    506       1.1      fvdl 		printf("%s: binding memory at bad offset %#lx\n",
    507       1.1      fvdl 			      sc->as_dev.dv_xname, (unsigned long) offset);
    508       1.1      fvdl 		lockmgr(&sc->as_lock, LK_RELEASE, 0);
    509       1.1      fvdl 		return EINVAL;
    510       1.1      fvdl 	}
    511       1.1      fvdl 
    512       1.1      fvdl 	/*
    513       1.1      fvdl 	 * XXXfvdl
    514       1.1      fvdl 	 * The memory here needs to be directly accessable from the
    515       1.1      fvdl 	 * AGP video card, so it should be allocated using bus_dma.
    516       1.1      fvdl 	 * However, it need not be contiguous, since individual pages
    517       1.1      fvdl 	 * are translated using the GATT.
    518       1.1      fvdl 	 *
    519       1.1      fvdl 	 * Using a large chunk of contiguous memory may get in the way
    520       1.1      fvdl 	 * of other subsystems that may need one, so we try to be friendly
    521       1.1      fvdl 	 * and ask for allocation in chunks of a minimum of 8 pages
    522       1.1      fvdl 	 * of contiguous memory on average, falling back to 4, 2 and 1
    523       1.1      fvdl 	 * if really needed. Larger chunks are preferred, since allocating
    524       1.1      fvdl 	 * a bus_dma_segment per page would be overkill.
    525       1.1      fvdl 	 */
    526       1.1      fvdl 
    527       1.1      fvdl 	for (contigpages = 8; contigpages > 0; contigpages >>= 1) {
    528       1.1      fvdl 		nseg = (mem->am_size / (contigpages * PAGE_SIZE)) + 1;
    529       1.3  drochner 		segs = malloc(nseg * sizeof *segs, M_AGP, M_WAITOK);
    530      1.16  drochner 		if (segs == NULL) {
    531      1.16  drochner 			lockmgr(&sc->as_lock, LK_RELEASE, 0);
    532      1.10   thorpej 			return ENOMEM;
    533      1.16  drochner 		}
    534       1.1      fvdl 		if (bus_dmamem_alloc(sc->as_dmat, mem->am_size, PAGE_SIZE, 0,
    535       1.4  drochner 				     segs, nseg, &mem->am_nseg,
    536      1.15  drochner 				     contigpages > 1 ?
    537      1.15  drochner 				     BUS_DMA_NOWAIT : BUS_DMA_WAITOK) != 0) {
    538       1.4  drochner 			free(segs, M_AGP);
    539       1.1      fvdl 			continue;
    540       1.4  drochner 		}
    541       1.1      fvdl 		if (bus_dmamem_map(sc->as_dmat, segs, mem->am_nseg,
    542       1.1      fvdl 		    mem->am_size, &mem->am_virtual, BUS_DMA_WAITOK) != 0) {
    543       1.1      fvdl 			bus_dmamem_free(sc->as_dmat, segs, mem->am_nseg);
    544       1.4  drochner 			free(segs, M_AGP);
    545       1.1      fvdl 			continue;
    546       1.1      fvdl 		}
    547       1.1      fvdl 		if (bus_dmamap_load(sc->as_dmat, mem->am_dmamap,
    548       1.1      fvdl 		    mem->am_virtual, mem->am_size, NULL, BUS_DMA_WAITOK) != 0) {
    549       1.1      fvdl 			bus_dmamem_unmap(sc->as_dmat, mem->am_virtual,
    550       1.1      fvdl 			    mem->am_size);
    551       1.1      fvdl 			bus_dmamem_free(sc->as_dmat, segs, mem->am_nseg);
    552       1.4  drochner 			free(segs, M_AGP);
    553       1.1      fvdl 			continue;
    554       1.1      fvdl 		}
    555       1.1      fvdl 		mem->am_dmaseg = segs;
    556       1.1      fvdl 		break;
    557       1.1      fvdl 	}
    558       1.1      fvdl 
    559       1.1      fvdl 	if (contigpages == 0) {
    560       1.1      fvdl 		lockmgr(&sc->as_lock, LK_RELEASE, 0);
    561       1.1      fvdl 		return ENOMEM;
    562       1.1      fvdl 	}
    563       1.1      fvdl 
    564       1.1      fvdl 
    565       1.1      fvdl 	/*
    566       1.1      fvdl 	 * Bind the individual pages and flush the chipset's
    567       1.1      fvdl 	 * TLB.
    568       1.1      fvdl 	 */
    569       1.1      fvdl 	done = 0;
    570       1.1      fvdl 	for (i = 0; i < mem->am_dmamap->dm_nsegs; i++) {
    571       1.1      fvdl 		seg = &mem->am_dmamap->dm_segs[i];
    572       1.1      fvdl 		/*
    573       1.1      fvdl 		 * Install entries in the GATT, making sure that if
    574       1.1      fvdl 		 * AGP_PAGE_SIZE < PAGE_SIZE and mem->am_size is not
    575       1.1      fvdl 		 * aligned to PAGE_SIZE, we don't modify too many GATT
    576       1.1      fvdl 		 * entries.
    577       1.1      fvdl 		 */
    578       1.1      fvdl 		for (j = 0; j < seg->ds_len && (done + j) < mem->am_size;
    579       1.1      fvdl 		     j += AGP_PAGE_SIZE) {
    580       1.1      fvdl 			pa = seg->ds_addr + j;
    581       1.3  drochner 			AGP_DPF("binding offset %#lx to pa %#lx\n",
    582       1.3  drochner 				(unsigned long)(offset + done + j),
    583       1.3  drochner 				(unsigned long)pa);
    584       1.1      fvdl 			error = AGP_BIND_PAGE(sc, offset + done + j, pa);
    585       1.1      fvdl 			if (error) {
    586       1.1      fvdl 				/*
    587       1.1      fvdl 				 * Bail out. Reverse all the mappings
    588       1.1      fvdl 				 * and unwire the pages.
    589       1.1      fvdl 				 */
    590       1.1      fvdl 				for (k = 0; k < done + j; k += AGP_PAGE_SIZE)
    591       1.1      fvdl 					AGP_UNBIND_PAGE(sc, offset + k);
    592       1.1      fvdl 
    593       1.4  drochner 				bus_dmamap_unload(sc->as_dmat, mem->am_dmamap);
    594       1.4  drochner 				bus_dmamem_unmap(sc->as_dmat, mem->am_virtual,
    595       1.4  drochner 						 mem->am_size);
    596       1.4  drochner 				bus_dmamem_free(sc->as_dmat, mem->am_dmaseg,
    597       1.4  drochner 						mem->am_nseg);
    598       1.4  drochner 				free(mem->am_dmaseg, M_AGP);
    599       1.1      fvdl 				lockmgr(&sc->as_lock, LK_RELEASE, 0);
    600       1.1      fvdl 				return error;
    601       1.1      fvdl 			}
    602       1.1      fvdl 		}
    603       1.1      fvdl 		done += seg->ds_len;
    604       1.1      fvdl 	}
    605       1.1      fvdl 
    606       1.1      fvdl 	/*
    607       1.1      fvdl 	 * Flush the cpu cache since we are providing a new mapping
    608       1.1      fvdl 	 * for these pages.
    609       1.1      fvdl 	 */
    610       1.1      fvdl 	agp_flush_cache();
    611       1.1      fvdl 
    612       1.1      fvdl 	/*
    613       1.1      fvdl 	 * Make sure the chipset gets the new mappings.
    614       1.1      fvdl 	 */
    615       1.1      fvdl 	AGP_FLUSH_TLB(sc);
    616       1.1      fvdl 
    617       1.1      fvdl 	mem->am_offset = offset;
    618       1.1      fvdl 	mem->am_is_bound = 1;
    619       1.1      fvdl 
    620       1.1      fvdl 	lockmgr(&sc->as_lock, LK_RELEASE, 0);
    621       1.1      fvdl 
    622       1.1      fvdl 	return 0;
    623       1.1      fvdl }
    624       1.1      fvdl 
    625       1.1      fvdl int
    626       1.1      fvdl agp_generic_unbind_memory(struct agp_softc *sc, struct agp_memory *mem)
    627       1.1      fvdl {
    628       1.1      fvdl 	int i;
    629       1.1      fvdl 
    630       1.1      fvdl 	lockmgr(&sc->as_lock, LK_EXCLUSIVE, 0);
    631       1.1      fvdl 
    632       1.1      fvdl 	if (!mem->am_is_bound) {
    633       1.1      fvdl 		printf("%s: memory is not bound\n", sc->as_dev.dv_xname);
    634       1.1      fvdl 		lockmgr(&sc->as_lock, LK_RELEASE, 0);
    635       1.1      fvdl 		return EINVAL;
    636       1.1      fvdl 	}
    637       1.1      fvdl 
    638       1.1      fvdl 
    639       1.1      fvdl 	/*
    640       1.1      fvdl 	 * Unbind the individual pages and flush the chipset's
    641       1.1      fvdl 	 * TLB. Unwire the pages so they can be swapped.
    642       1.1      fvdl 	 */
    643       1.1      fvdl 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
    644       1.1      fvdl 		AGP_UNBIND_PAGE(sc, mem->am_offset + i);
    645       1.1      fvdl 
    646       1.1      fvdl 	agp_flush_cache();
    647       1.1      fvdl 	AGP_FLUSH_TLB(sc);
    648       1.1      fvdl 
    649       1.1      fvdl 	bus_dmamap_unload(sc->as_dmat, mem->am_dmamap);
    650       1.1      fvdl 	bus_dmamem_unmap(sc->as_dmat, mem->am_virtual, mem->am_size);
    651       1.1      fvdl 	bus_dmamem_free(sc->as_dmat, mem->am_dmaseg, mem->am_nseg);
    652       1.1      fvdl 
    653       1.1      fvdl 	free(mem->am_dmaseg, M_AGP);
    654       1.1      fvdl 
    655       1.1      fvdl 	mem->am_offset = 0;
    656       1.1      fvdl 	mem->am_is_bound = 0;
    657       1.1      fvdl 
    658       1.1      fvdl 	lockmgr(&sc->as_lock, LK_RELEASE, 0);
    659       1.1      fvdl 
    660       1.1      fvdl 	return 0;
    661       1.1      fvdl }
    662       1.1      fvdl 
    663       1.1      fvdl /* Helper functions for implementing user/kernel api */
    664       1.1      fvdl 
    665       1.1      fvdl static int
    666       1.1      fvdl agp_acquire_helper(struct agp_softc *sc, enum agp_acquire_state state)
    667       1.1      fvdl {
    668       1.1      fvdl 	if (sc->as_state != AGP_ACQUIRE_FREE)
    669       1.1      fvdl 		return EBUSY;
    670       1.1      fvdl 	sc->as_state = state;
    671       1.1      fvdl 
    672       1.1      fvdl 	return 0;
    673       1.1      fvdl }
    674       1.1      fvdl 
    675       1.1      fvdl static int
    676       1.1      fvdl agp_release_helper(struct agp_softc *sc, enum agp_acquire_state state)
    677       1.1      fvdl {
    678       1.1      fvdl 	struct agp_memory *mem;
    679       1.1      fvdl 
    680       1.1      fvdl 	if (sc->as_state == AGP_ACQUIRE_FREE)
    681       1.1      fvdl 		return 0;
    682       1.1      fvdl 
    683       1.1      fvdl 	if (sc->as_state != state)
    684       1.1      fvdl 		return EBUSY;
    685       1.1      fvdl 
    686       1.1      fvdl 	/*
    687      1.16  drochner 	 * Clear out outstanding aperture mappings.
    688      1.16  drochner 	 * (should not be necessary, done by caller)
    689       1.1      fvdl 	 */
    690       1.4  drochner 	TAILQ_FOREACH(mem, &sc->as_memory, am_link) {
    691       1.4  drochner 		if (mem->am_is_bound) {
    692       1.4  drochner 			printf("agp_release_helper: mem %d is bound\n",
    693       1.4  drochner 			       mem->am_id);
    694       1.1      fvdl 			AGP_UNBIND_MEMORY(sc, mem);
    695       1.4  drochner 		}
    696       1.1      fvdl 	}
    697       1.1      fvdl 
    698       1.1      fvdl 	sc->as_state = AGP_ACQUIRE_FREE;
    699       1.1      fvdl 	return 0;
    700       1.1      fvdl }
    701       1.1      fvdl 
    702       1.1      fvdl static struct agp_memory *
    703       1.1      fvdl agp_find_memory(struct agp_softc *sc, int id)
    704       1.1      fvdl {
    705       1.1      fvdl 	struct agp_memory *mem;
    706       1.1      fvdl 
    707       1.1      fvdl 	AGP_DPF("searching for memory block %d\n", id);
    708       1.1      fvdl 	TAILQ_FOREACH(mem, &sc->as_memory, am_link) {
    709       1.1      fvdl 		AGP_DPF("considering memory block %d\n", mem->am_id);
    710       1.1      fvdl 		if (mem->am_id == id)
    711       1.1      fvdl 			return mem;
    712       1.1      fvdl 	}
    713       1.1      fvdl 	return 0;
    714       1.1      fvdl }
    715       1.1      fvdl 
    716       1.1      fvdl /* Implementation of the userland ioctl api */
    717       1.1      fvdl 
    718       1.1      fvdl static int
    719       1.1      fvdl agp_info_user(struct agp_softc *sc, agp_info *info)
    720       1.1      fvdl {
    721       1.1      fvdl 	memset(info, 0, sizeof *info);
    722       1.1      fvdl 	info->bridge_id = sc->as_id;
    723       1.3  drochner 	if (sc->as_capoff != 0)
    724       1.3  drochner 		info->agp_mode = pci_conf_read(sc->as_pc, sc->as_tag,
    725       1.3  drochner 					       sc->as_capoff + AGP_STATUS);
    726       1.3  drochner 	else
    727       1.3  drochner 		info->agp_mode = 0; /* i810 doesn't have real AGP */
    728       1.1      fvdl 	info->aper_base = sc->as_apaddr;
    729       1.1      fvdl 	info->aper_size = AGP_GET_APERTURE(sc) >> 20;
    730       1.1      fvdl 	info->pg_total = info->pg_system = sc->as_maxmem >> AGP_PAGE_SHIFT;
    731       1.1      fvdl 	info->pg_used = sc->as_allocated >> AGP_PAGE_SHIFT;
    732       1.1      fvdl 
    733       1.1      fvdl 	return 0;
    734       1.1      fvdl }
    735       1.1      fvdl 
    736       1.1      fvdl static int
    737       1.1      fvdl agp_setup_user(struct agp_softc *sc, agp_setup *setup)
    738       1.1      fvdl {
    739       1.1      fvdl 	return AGP_ENABLE(sc, setup->agp_mode);
    740       1.1      fvdl }
    741       1.1      fvdl 
    742       1.1      fvdl static int
    743       1.1      fvdl agp_allocate_user(struct agp_softc *sc, agp_allocate *alloc)
    744       1.1      fvdl {
    745       1.1      fvdl 	struct agp_memory *mem;
    746       1.1      fvdl 
    747       1.1      fvdl 	mem = AGP_ALLOC_MEMORY(sc,
    748       1.1      fvdl 			       alloc->type,
    749       1.1      fvdl 			       alloc->pg_count << AGP_PAGE_SHIFT);
    750       1.1      fvdl 	if (mem) {
    751       1.1      fvdl 		alloc->key = mem->am_id;
    752       1.1      fvdl 		alloc->physical = mem->am_physical;
    753       1.1      fvdl 		return 0;
    754       1.1      fvdl 	} else {
    755       1.1      fvdl 		return ENOMEM;
    756       1.1      fvdl 	}
    757       1.1      fvdl }
    758       1.1      fvdl 
    759       1.1      fvdl static int
    760       1.1      fvdl agp_deallocate_user(struct agp_softc *sc, int id)
    761       1.1      fvdl {
    762       1.1      fvdl 	struct agp_memory *mem = agp_find_memory(sc, id);
    763       1.1      fvdl 
    764       1.1      fvdl 	if (mem) {
    765       1.1      fvdl 		AGP_FREE_MEMORY(sc, mem);
    766       1.1      fvdl 		return 0;
    767       1.1      fvdl 	} else {
    768       1.1      fvdl 		return ENOENT;
    769       1.1      fvdl 	}
    770       1.1      fvdl }
    771       1.1      fvdl 
    772       1.1      fvdl static int
    773       1.1      fvdl agp_bind_user(struct agp_softc *sc, agp_bind *bind)
    774       1.1      fvdl {
    775       1.1      fvdl 	struct agp_memory *mem = agp_find_memory(sc, bind->key);
    776       1.1      fvdl 
    777       1.1      fvdl 	if (!mem)
    778       1.1      fvdl 		return ENOENT;
    779       1.1      fvdl 
    780       1.1      fvdl 	return AGP_BIND_MEMORY(sc, mem, bind->pg_start << AGP_PAGE_SHIFT);
    781       1.1      fvdl }
    782       1.1      fvdl 
    783       1.1      fvdl static int
    784       1.1      fvdl agp_unbind_user(struct agp_softc *sc, agp_unbind *unbind)
    785       1.1      fvdl {
    786       1.1      fvdl 	struct agp_memory *mem = agp_find_memory(sc, unbind->key);
    787       1.1      fvdl 
    788       1.1      fvdl 	if (!mem)
    789       1.1      fvdl 		return ENOENT;
    790       1.1      fvdl 
    791       1.1      fvdl 	return AGP_UNBIND_MEMORY(sc, mem);
    792       1.1      fvdl }
    793       1.1      fvdl 
    794       1.1      fvdl int
    795  1.28.2.1   darrenr agpopen(dev_t dev, int oflags, int devtype, struct lwp *l)
    796       1.1      fvdl {
    797       1.1      fvdl 	struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
    798       1.9   thorpej 
    799       1.9   thorpej 	if (sc == NULL)
    800       1.9   thorpej 		return ENXIO;
    801       1.1      fvdl 
    802       1.1      fvdl 	if (sc->as_chipc == NULL)
    803       1.1      fvdl 		return ENXIO;
    804       1.1      fvdl 
    805       1.1      fvdl 	if (!sc->as_isopen)
    806       1.1      fvdl 		sc->as_isopen = 1;
    807       1.1      fvdl 	else
    808       1.1      fvdl 		return EBUSY;
    809       1.1      fvdl 
    810       1.1      fvdl 	return 0;
    811       1.1      fvdl }
    812       1.1      fvdl 
    813       1.1      fvdl int
    814  1.28.2.1   darrenr agpclose(dev_t dev, int fflag, int devtype, struct lwp *l)
    815       1.1      fvdl {
    816       1.1      fvdl 	struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
    817      1.16  drochner 	struct agp_memory *mem;
    818       1.1      fvdl 
    819       1.1      fvdl 	/*
    820       1.1      fvdl 	 * Clear the GATT and force release on last close
    821       1.1      fvdl 	 */
    822      1.16  drochner 	if (sc->as_state == AGP_ACQUIRE_USER) {
    823      1.16  drochner 		while ((mem = TAILQ_FIRST(&sc->as_memory))) {
    824      1.16  drochner 			if (mem->am_is_bound) {
    825      1.16  drochner 				printf("agpclose: mem %d is bound\n",
    826      1.16  drochner 				       mem->am_id);
    827      1.16  drochner 				AGP_UNBIND_MEMORY(sc, mem);
    828      1.16  drochner 			}
    829      1.16  drochner 			/*
    830      1.16  drochner 			 * XXX it is not documented, but if the protocol allows
    831      1.16  drochner 			 * allocate->acquire->bind, it would be possible that
    832      1.16  drochner 			 * memory ranges are allocated by the kernel here,
    833      1.16  drochner 			 * which we shouldn't free. We'd have to keep track of
    834      1.16  drochner 			 * the memory range's owner.
    835      1.16  drochner 			 * The kernel API is unsed yet, so we get away with
    836      1.16  drochner 			 * freeing all.
    837      1.16  drochner 			 */
    838      1.16  drochner 			AGP_FREE_MEMORY(sc, mem);
    839      1.16  drochner 		}
    840       1.1      fvdl 		agp_release_helper(sc, AGP_ACQUIRE_USER);
    841      1.16  drochner 	}
    842       1.1      fvdl 	sc->as_isopen = 0;
    843       1.1      fvdl 
    844       1.1      fvdl 	return 0;
    845       1.1      fvdl }
    846       1.1      fvdl 
    847       1.1      fvdl int
    848  1.28.2.1   darrenr agpioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct lwp *l)
    849       1.1      fvdl {
    850       1.1      fvdl 	struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
    851       1.1      fvdl 
    852       1.1      fvdl 	if (sc == NULL)
    853       1.1      fvdl 		return ENODEV;
    854       1.1      fvdl 
    855       1.1      fvdl 	if ((fflag & FWRITE) == 0 && cmd != AGPIOC_INFO)
    856       1.1      fvdl 		return EPERM;
    857       1.1      fvdl 
    858       1.1      fvdl 	switch (cmd) {
    859       1.1      fvdl 	case AGPIOC_INFO:
    860       1.1      fvdl 		return agp_info_user(sc, (agp_info *) data);
    861       1.1      fvdl 
    862       1.1      fvdl 	case AGPIOC_ACQUIRE:
    863       1.1      fvdl 		return agp_acquire_helper(sc, AGP_ACQUIRE_USER);
    864       1.1      fvdl 
    865       1.1      fvdl 	case AGPIOC_RELEASE:
    866       1.1      fvdl 		return agp_release_helper(sc, AGP_ACQUIRE_USER);
    867       1.1      fvdl 
    868       1.1      fvdl 	case AGPIOC_SETUP:
    869       1.1      fvdl 		return agp_setup_user(sc, (agp_setup *)data);
    870       1.1      fvdl 
    871       1.1      fvdl 	case AGPIOC_ALLOCATE:
    872       1.1      fvdl 		return agp_allocate_user(sc, (agp_allocate *)data);
    873       1.1      fvdl 
    874       1.1      fvdl 	case AGPIOC_DEALLOCATE:
    875       1.1      fvdl 		return agp_deallocate_user(sc, *(int *) data);
    876       1.1      fvdl 
    877       1.1      fvdl 	case AGPIOC_BIND:
    878       1.1      fvdl 		return agp_bind_user(sc, (agp_bind *)data);
    879       1.1      fvdl 
    880       1.1      fvdl 	case AGPIOC_UNBIND:
    881       1.1      fvdl 		return agp_unbind_user(sc, (agp_unbind *)data);
    882       1.1      fvdl 
    883       1.1      fvdl 	}
    884       1.1      fvdl 
    885       1.1      fvdl 	return EINVAL;
    886       1.1      fvdl }
    887       1.1      fvdl 
    888       1.1      fvdl paddr_t
    889       1.1      fvdl agpmmap(dev_t dev, off_t offset, int prot)
    890       1.1      fvdl {
    891       1.1      fvdl 	struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
    892       1.1      fvdl 
    893       1.1      fvdl 	if (offset > AGP_GET_APERTURE(sc))
    894       1.1      fvdl 		return -1;
    895       1.6   thorpej 
    896       1.6   thorpej 	return (bus_space_mmap(sc->as_apt, sc->as_apaddr, offset, prot,
    897       1.6   thorpej 	    BUS_SPACE_MAP_LINEAR));
    898       1.1      fvdl }
    899       1.1      fvdl 
    900       1.1      fvdl /* Implementation of the kernel api */
    901       1.1      fvdl 
    902       1.1      fvdl void *
    903       1.1      fvdl agp_find_device(int unit)
    904       1.1      fvdl {
    905       1.1      fvdl 	return device_lookup(&agp_cd, unit);
    906       1.1      fvdl }
    907       1.1      fvdl 
    908       1.1      fvdl enum agp_acquire_state
    909       1.1      fvdl agp_state(void *devcookie)
    910       1.1      fvdl {
    911       1.1      fvdl 	struct agp_softc *sc = devcookie;
    912       1.1      fvdl 	return sc->as_state;
    913       1.1      fvdl }
    914       1.1      fvdl 
    915       1.1      fvdl void
    916       1.1      fvdl agp_get_info(void *devcookie, struct agp_info *info)
    917       1.1      fvdl {
    918       1.1      fvdl 	struct agp_softc *sc = devcookie;
    919       1.1      fvdl 
    920       1.1      fvdl 	info->ai_mode = pci_conf_read(sc->as_pc, sc->as_tag,
    921       1.1      fvdl 	    sc->as_capoff + AGP_STATUS);
    922       1.1      fvdl 	info->ai_aperture_base = sc->as_apaddr;
    923       1.1      fvdl 	info->ai_aperture_size = sc->as_apsize;	/* XXXfvdl inconsistent */
    924       1.1      fvdl 	info->ai_memory_allowed = sc->as_maxmem;
    925       1.1      fvdl 	info->ai_memory_used = sc->as_allocated;
    926       1.1      fvdl }
    927       1.1      fvdl 
    928       1.1      fvdl int
    929       1.1      fvdl agp_acquire(void *dev)
    930       1.1      fvdl {
    931       1.1      fvdl 	return agp_acquire_helper(dev, AGP_ACQUIRE_KERNEL);
    932       1.1      fvdl }
    933       1.1      fvdl 
    934       1.1      fvdl int
    935       1.1      fvdl agp_release(void *dev)
    936       1.1      fvdl {
    937       1.1      fvdl 	return agp_release_helper(dev, AGP_ACQUIRE_KERNEL);
    938       1.1      fvdl }
    939       1.1      fvdl 
    940       1.1      fvdl int
    941       1.1      fvdl agp_enable(void *dev, u_int32_t mode)
    942       1.1      fvdl {
    943       1.1      fvdl 	struct agp_softc *sc = dev;
    944       1.1      fvdl 
    945       1.1      fvdl 	return AGP_ENABLE(sc, mode);
    946       1.1      fvdl }
    947       1.1      fvdl 
    948       1.1      fvdl void *agp_alloc_memory(void *dev, int type, vsize_t bytes)
    949       1.1      fvdl {
    950       1.1      fvdl 	struct agp_softc *sc = dev;
    951       1.1      fvdl 
    952       1.1      fvdl 	return (void *)AGP_ALLOC_MEMORY(sc, type, bytes);
    953       1.1      fvdl }
    954       1.1      fvdl 
    955       1.1      fvdl void agp_free_memory(void *dev, void *handle)
    956       1.1      fvdl {
    957       1.1      fvdl 	struct agp_softc *sc = dev;
    958       1.1      fvdl 	struct agp_memory *mem = (struct agp_memory *) handle;
    959       1.1      fvdl 	AGP_FREE_MEMORY(sc, mem);
    960       1.1      fvdl }
    961       1.1      fvdl 
    962       1.1      fvdl int agp_bind_memory(void *dev, void *handle, off_t offset)
    963       1.1      fvdl {
    964       1.1      fvdl 	struct agp_softc *sc = dev;
    965       1.1      fvdl 	struct agp_memory *mem = (struct agp_memory *) handle;
    966       1.1      fvdl 
    967       1.1      fvdl 	return AGP_BIND_MEMORY(sc, mem, offset);
    968       1.1      fvdl }
    969       1.1      fvdl 
    970       1.1      fvdl int agp_unbind_memory(void *dev, void *handle)
    971       1.1      fvdl {
    972       1.1      fvdl 	struct agp_softc *sc = dev;
    973       1.1      fvdl 	struct agp_memory *mem = (struct agp_memory *) handle;
    974       1.1      fvdl 
    975       1.1      fvdl 	return AGP_UNBIND_MEMORY(sc, mem);
    976       1.1      fvdl }
    977       1.1      fvdl 
    978       1.1      fvdl void agp_memory_info(void *dev, void *handle, struct agp_memory_info *mi)
    979       1.1      fvdl {
    980       1.1      fvdl 	struct agp_memory *mem = (struct agp_memory *) handle;
    981       1.1      fvdl 
    982       1.1      fvdl 	mi->ami_size = mem->am_size;
    983       1.1      fvdl 	mi->ami_physical = mem->am_physical;
    984       1.1      fvdl 	mi->ami_offset = mem->am_offset;
    985       1.1      fvdl 	mi->ami_is_bound = mem->am_is_bound;
    986       1.1      fvdl }
    987       1.1      fvdl 
    988       1.1      fvdl int
    989       1.1      fvdl agp_alloc_dmamem(bus_dma_tag_t tag, size_t size, int flags,
    990       1.1      fvdl 		 bus_dmamap_t *mapp, caddr_t *vaddr, bus_addr_t *baddr,
    991       1.1      fvdl 		 bus_dma_segment_t *seg, int nseg, int *rseg)
    992       1.1      fvdl 
    993       1.1      fvdl {
    994       1.1      fvdl 	int error, level = 0;
    995       1.1      fvdl 
    996       1.1      fvdl 	if ((error = bus_dmamem_alloc(tag, size, PAGE_SIZE, 0,
    997       1.1      fvdl 			seg, nseg, rseg, BUS_DMA_NOWAIT)) != 0)
    998       1.1      fvdl 		goto out;
    999       1.1      fvdl 	level++;
   1000       1.1      fvdl 
   1001       1.1      fvdl 	if ((error = bus_dmamem_map(tag, seg, *rseg, size, vaddr,
   1002       1.1      fvdl 			BUS_DMA_NOWAIT | flags)) != 0)
   1003       1.1      fvdl 		goto out;
   1004       1.1      fvdl 	level++;
   1005       1.1      fvdl 
   1006       1.3  drochner 	if ((error = bus_dmamap_create(tag, size, *rseg, size, 0,
   1007       1.1      fvdl 			BUS_DMA_NOWAIT, mapp)) != 0)
   1008       1.1      fvdl 		goto out;
   1009       1.1      fvdl 	level++;
   1010       1.1      fvdl 
   1011       1.1      fvdl 	if ((error = bus_dmamap_load(tag, *mapp, *vaddr, size, NULL,
   1012       1.1      fvdl 			BUS_DMA_NOWAIT)) != 0)
   1013       1.1      fvdl 		goto out;
   1014       1.1      fvdl 
   1015       1.1      fvdl 	*baddr = (*mapp)->dm_segs[0].ds_addr;
   1016       1.1      fvdl 
   1017       1.1      fvdl 	return 0;
   1018       1.1      fvdl out:
   1019       1.1      fvdl 	switch (level) {
   1020       1.1      fvdl 	case 3:
   1021       1.1      fvdl 		bus_dmamap_destroy(tag, *mapp);
   1022       1.1      fvdl 		/* FALLTHROUGH */
   1023       1.1      fvdl 	case 2:
   1024       1.1      fvdl 		bus_dmamem_unmap(tag, *vaddr, size);
   1025       1.1      fvdl 		/* FALLTHROUGH */
   1026       1.1      fvdl 	case 1:
   1027       1.1      fvdl 		bus_dmamem_free(tag, seg, *rseg);
   1028       1.1      fvdl 		break;
   1029       1.1      fvdl 	default:
   1030       1.1      fvdl 		break;
   1031       1.1      fvdl 	}
   1032       1.1      fvdl 
   1033       1.1      fvdl 	return error;
   1034       1.1      fvdl }
   1035       1.1      fvdl 
   1036       1.1      fvdl void
   1037       1.1      fvdl agp_free_dmamem(bus_dma_tag_t tag, size_t size, bus_dmamap_t map,
   1038       1.1      fvdl 		caddr_t vaddr, bus_dma_segment_t *seg, int nseg)
   1039       1.1      fvdl {
   1040       1.1      fvdl 
   1041       1.1      fvdl 	bus_dmamap_unload(tag, map);
   1042       1.1      fvdl 	bus_dmamap_destroy(tag, map);
   1043       1.1      fvdl 	bus_dmamem_unmap(tag, vaddr, size);
   1044       1.1      fvdl 	bus_dmamem_free(tag, seg, nseg);
   1045       1.1      fvdl }
   1046