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