Home | History | Annotate | Line # | Download | only in pci
agp_i810.c revision 1.8.2.5
      1  1.8.2.5  nathanw /*	$NetBSD: agp_i810.c,v 1.8.2.5 2002/08/13 02:19:35 nathanw Exp $	*/
      2  1.8.2.2  nathanw 
      3  1.8.2.2  nathanw /*-
      4  1.8.2.2  nathanw  * Copyright (c) 2000 Doug Rabson
      5  1.8.2.2  nathanw  * Copyright (c) 2000 Ruslan Ermilov
      6  1.8.2.2  nathanw  * All rights reserved.
      7  1.8.2.2  nathanw  *
      8  1.8.2.2  nathanw  * Redistribution and use in source and binary forms, with or without
      9  1.8.2.2  nathanw  * modification, are permitted provided that the following conditions
     10  1.8.2.2  nathanw  * are met:
     11  1.8.2.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     12  1.8.2.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     13  1.8.2.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.8.2.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     15  1.8.2.2  nathanw  *    documentation and/or other materials provided with the distribution.
     16  1.8.2.2  nathanw  *
     17  1.8.2.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18  1.8.2.2  nathanw  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  1.8.2.2  nathanw  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  1.8.2.2  nathanw  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  1.8.2.2  nathanw  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  1.8.2.2  nathanw  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  1.8.2.2  nathanw  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  1.8.2.2  nathanw  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  1.8.2.2  nathanw  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  1.8.2.2  nathanw  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  1.8.2.2  nathanw  * SUCH DAMAGE.
     28  1.8.2.2  nathanw  *
     29  1.8.2.2  nathanw  *	$FreeBSD: src/sys/pci/agp_i810.c,v 1.4 2001/07/05 21:28:47 jhb Exp $
     30  1.8.2.2  nathanw  */
     31  1.8.2.2  nathanw 
     32  1.8.2.3  nathanw #include <sys/cdefs.h>
     33  1.8.2.5  nathanw __KERNEL_RCSID(0, "$NetBSD: agp_i810.c,v 1.8.2.5 2002/08/13 02:19:35 nathanw Exp $");
     34  1.8.2.3  nathanw 
     35  1.8.2.2  nathanw #include <sys/param.h>
     36  1.8.2.2  nathanw #include <sys/systm.h>
     37  1.8.2.2  nathanw #include <sys/malloc.h>
     38  1.8.2.2  nathanw #include <sys/kernel.h>
     39  1.8.2.2  nathanw #include <sys/lock.h>
     40  1.8.2.2  nathanw #include <sys/proc.h>
     41  1.8.2.2  nathanw #include <sys/device.h>
     42  1.8.2.2  nathanw #include <sys/conf.h>
     43  1.8.2.2  nathanw 
     44  1.8.2.2  nathanw #include <uvm/uvm_extern.h>
     45  1.8.2.2  nathanw 
     46  1.8.2.2  nathanw #include <dev/pci/pcivar.h>
     47  1.8.2.2  nathanw #include <dev/pci/pcireg.h>
     48  1.8.2.2  nathanw #include <dev/pci/pcidevs.h>
     49  1.8.2.2  nathanw #include <dev/pci/agpvar.h>
     50  1.8.2.2  nathanw #include <dev/pci/agpreg.h>
     51  1.8.2.2  nathanw 
     52  1.8.2.2  nathanw #include <sys/agpio.h>
     53  1.8.2.2  nathanw 
     54  1.8.2.2  nathanw #include <machine/bus.h>
     55  1.8.2.2  nathanw 
     56  1.8.2.2  nathanw #define READ1(off)	bus_space_read_1(isc->bst, isc->bsh, off)
     57  1.8.2.2  nathanw #define WRITE4(off,v)	bus_space_write_4(isc->bst, isc->bsh, off, v)
     58  1.8.2.2  nathanw 
     59  1.8.2.2  nathanw struct agp_i810_softc {
     60  1.8.2.2  nathanw 	u_int32_t initial_aperture;	/* aperture size at startup */
     61  1.8.2.2  nathanw 	struct agp_gatt *gatt;
     62  1.8.2.2  nathanw 	u_int32_t dcache_size;
     63  1.8.2.2  nathanw 	bus_space_tag_t bst;		/* bus_space tag */
     64  1.8.2.2  nathanw 	bus_space_handle_t bsh;		/* bus_space handle */
     65  1.8.2.2  nathanw 	struct pci_attach_args vga_pa;
     66  1.8.2.2  nathanw };
     67  1.8.2.2  nathanw 
     68  1.8.2.2  nathanw static u_int32_t agp_i810_get_aperture(struct agp_softc *);
     69  1.8.2.2  nathanw static int agp_i810_set_aperture(struct agp_softc *, u_int32_t);
     70  1.8.2.2  nathanw static int agp_i810_bind_page(struct agp_softc *, off_t, bus_addr_t);
     71  1.8.2.2  nathanw static int agp_i810_unbind_page(struct agp_softc *, off_t);
     72  1.8.2.2  nathanw static void agp_i810_flush_tlb(struct agp_softc *);
     73  1.8.2.2  nathanw static int agp_i810_enable(struct agp_softc *, u_int32_t mode);
     74  1.8.2.2  nathanw static struct agp_memory *agp_i810_alloc_memory(struct agp_softc *, int,
     75  1.8.2.2  nathanw 						vsize_t);
     76  1.8.2.2  nathanw static int agp_i810_free_memory(struct agp_softc *, struct agp_memory *);
     77  1.8.2.2  nathanw static int agp_i810_bind_memory(struct agp_softc *, struct agp_memory *, off_t);
     78  1.8.2.2  nathanw static int agp_i810_unbind_memory(struct agp_softc *, struct agp_memory *);
     79  1.8.2.2  nathanw 
     80  1.8.2.2  nathanw struct agp_methods agp_i810_methods = {
     81  1.8.2.2  nathanw 	agp_i810_get_aperture,
     82  1.8.2.2  nathanw 	agp_i810_set_aperture,
     83  1.8.2.2  nathanw 	agp_i810_bind_page,
     84  1.8.2.2  nathanw 	agp_i810_unbind_page,
     85  1.8.2.2  nathanw 	agp_i810_flush_tlb,
     86  1.8.2.2  nathanw 	agp_i810_enable,
     87  1.8.2.2  nathanw 	agp_i810_alloc_memory,
     88  1.8.2.2  nathanw 	agp_i810_free_memory,
     89  1.8.2.2  nathanw 	agp_i810_bind_memory,
     90  1.8.2.2  nathanw 	agp_i810_unbind_memory,
     91  1.8.2.2  nathanw };
     92  1.8.2.2  nathanw 
     93  1.8.2.2  nathanw /* XXXthorpej -- duplicated code (see arch/i386/pci/pchb.c) */
     94  1.8.2.2  nathanw static int
     95  1.8.2.2  nathanw agp_i810_vgamatch(struct pci_attach_args *pa)
     96  1.8.2.2  nathanw {
     97  1.8.2.2  nathanw 
     98  1.8.2.2  nathanw 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
     99  1.8.2.2  nathanw 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
    100  1.8.2.2  nathanw 		return (0);
    101  1.8.2.2  nathanw 
    102  1.8.2.2  nathanw 	switch (PCI_PRODUCT(pa->pa_id)) {
    103  1.8.2.2  nathanw 	case PCI_PRODUCT_INTEL_82810_GC:
    104  1.8.2.2  nathanw 	case PCI_PRODUCT_INTEL_82810_DC100_GC:
    105  1.8.2.2  nathanw 	case PCI_PRODUCT_INTEL_82810E_GC:
    106  1.8.2.2  nathanw 	case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
    107  1.8.2.4  nathanw 	/*case PCI_PRODUCT_INTEL_82830MP_IV: XXX needs somewhat different driver */
    108  1.8.2.2  nathanw 		return (1);
    109  1.8.2.2  nathanw 	}
    110  1.8.2.2  nathanw 
    111  1.8.2.2  nathanw 	return (0);
    112  1.8.2.2  nathanw }
    113  1.8.2.2  nathanw 
    114  1.8.2.2  nathanw int
    115  1.8.2.2  nathanw agp_i810_attach(struct device *parent, struct device *self, void *aux)
    116  1.8.2.2  nathanw {
    117  1.8.2.2  nathanw 	struct agp_softc *sc = (void *)self;
    118  1.8.2.2  nathanw 	struct agp_i810_softc *isc;
    119  1.8.2.2  nathanw 	struct agp_gatt *gatt;
    120  1.8.2.2  nathanw 	int error;
    121  1.8.2.2  nathanw 
    122  1.8.2.4  nathanw 	isc = malloc(sizeof *isc, M_AGP, M_NOWAIT|M_ZERO);
    123  1.8.2.2  nathanw 	if (isc == NULL) {
    124  1.8.2.2  nathanw 		printf(": can't allocate chipset-specific softc\n");
    125  1.8.2.2  nathanw 		return ENOMEM;
    126  1.8.2.2  nathanw 	}
    127  1.8.2.2  nathanw 	sc->as_chipc = isc;
    128  1.8.2.2  nathanw 	sc->as_methods = &agp_i810_methods;
    129  1.8.2.2  nathanw 
    130  1.8.2.2  nathanw 	if (pci_find_device(&isc->vga_pa, agp_i810_vgamatch) == 0) {
    131  1.8.2.2  nathanw 		printf(": can't find internal VGA device config space\n");
    132  1.8.2.2  nathanw 		free(isc, M_AGP);
    133  1.8.2.2  nathanw 		return ENOENT;
    134  1.8.2.2  nathanw 	}
    135  1.8.2.2  nathanw 
    136  1.8.2.2  nathanw 	/* XXXfvdl */
    137  1.8.2.2  nathanw 	sc->as_dmat = isc->vga_pa.pa_dmat;
    138  1.8.2.2  nathanw 
    139  1.8.2.2  nathanw 	error = agp_map_aperture(&isc->vga_pa, sc);
    140  1.8.2.2  nathanw 	if (error != 0) {
    141  1.8.2.2  nathanw 		printf(": can't map aperture\n");
    142  1.8.2.2  nathanw 		free(isc, M_AGP);
    143  1.8.2.2  nathanw 		return error;
    144  1.8.2.2  nathanw 	}
    145  1.8.2.2  nathanw 
    146  1.8.2.2  nathanw 	error = pci_mapreg_map(&isc->vga_pa, AGP_I810_MMADR,
    147  1.8.2.2  nathanw 	    PCI_MAPREG_TYPE_MEM, 0, &isc->bst, &isc->bsh, NULL, NULL);
    148  1.8.2.2  nathanw 	if (error != 0) {
    149  1.8.2.2  nathanw 		printf(": can't map mmadr registers\n");
    150  1.8.2.2  nathanw 		return error;
    151  1.8.2.2  nathanw 	}
    152  1.8.2.2  nathanw 
    153  1.8.2.2  nathanw 	isc->initial_aperture = AGP_GET_APERTURE(sc);
    154  1.8.2.2  nathanw 
    155  1.8.2.2  nathanw 	if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
    156  1.8.2.2  nathanw 		isc->dcache_size = 4 * 1024 * 1024;
    157  1.8.2.2  nathanw 	else
    158  1.8.2.2  nathanw 		isc->dcache_size = 0;
    159  1.8.2.2  nathanw 
    160  1.8.2.2  nathanw 	for (;;) {
    161  1.8.2.2  nathanw 		gatt = agp_alloc_gatt(sc);
    162  1.8.2.2  nathanw 		if (gatt)
    163  1.8.2.2  nathanw 			break;
    164  1.8.2.2  nathanw 
    165  1.8.2.2  nathanw 		/*
    166  1.8.2.2  nathanw 		 * Probably contigmalloc failure. Try reducing the
    167  1.8.2.2  nathanw 		 * aperture so that the gatt size reduces.
    168  1.8.2.2  nathanw 		 */
    169  1.8.2.2  nathanw 		if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) {
    170  1.8.2.2  nathanw 			agp_generic_detach(sc);
    171  1.8.2.2  nathanw 			return ENOMEM;
    172  1.8.2.2  nathanw 		}
    173  1.8.2.2  nathanw 	}
    174  1.8.2.2  nathanw 	isc->gatt = gatt;
    175  1.8.2.2  nathanw 
    176  1.8.2.2  nathanw 	/* Install the GATT. */
    177  1.8.2.2  nathanw 	WRITE4(AGP_I810_PGTBL_CTL, gatt->ag_physical | 1);
    178  1.8.2.2  nathanw 
    179  1.8.2.2  nathanw 	/*
    180  1.8.2.2  nathanw 	 * Make sure the chipset can see everything.
    181  1.8.2.2  nathanw 	 */
    182  1.8.2.2  nathanw 	agp_flush_cache();
    183  1.8.2.2  nathanw 
    184  1.8.2.2  nathanw 	return 0;
    185  1.8.2.2  nathanw }
    186  1.8.2.2  nathanw 
    187  1.8.2.2  nathanw #if 0
    188  1.8.2.2  nathanw static int
    189  1.8.2.2  nathanw agp_i810_detach(struct agp_softc *sc)
    190  1.8.2.2  nathanw {
    191  1.8.2.2  nathanw 	int error;
    192  1.8.2.2  nathanw 	struct agp_i810_softc *isc = sc->as_chipc;
    193  1.8.2.2  nathanw 
    194  1.8.2.2  nathanw 	error = agp_generic_detach(sc);
    195  1.8.2.2  nathanw 	if (error)
    196  1.8.2.2  nathanw 		return error;
    197  1.8.2.2  nathanw 
    198  1.8.2.2  nathanw 	/* Clear the GATT base. */
    199  1.8.2.2  nathanw 	WRITE4(AGP_I810_PGTBL_CTL, 0);
    200  1.8.2.2  nathanw 
    201  1.8.2.2  nathanw 	/* Put the aperture back the way it started. */
    202  1.8.2.2  nathanw 	AGP_SET_APERTURE(sc, isc->initial_aperture);
    203  1.8.2.2  nathanw 
    204  1.8.2.2  nathanw 	agp_free_gatt(sc, isc->gatt);
    205  1.8.2.2  nathanw 
    206  1.8.2.2  nathanw 	return 0;
    207  1.8.2.2  nathanw }
    208  1.8.2.2  nathanw #endif
    209  1.8.2.2  nathanw 
    210  1.8.2.2  nathanw static u_int32_t
    211  1.8.2.2  nathanw agp_i810_get_aperture(struct agp_softc *sc)
    212  1.8.2.2  nathanw {
    213  1.8.2.2  nathanw 	u_int16_t miscc;
    214  1.8.2.2  nathanw 
    215  1.8.2.2  nathanw 	miscc = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM) >> 16;
    216  1.8.2.2  nathanw 	if ((miscc & AGP_I810_MISCC_WINSIZE) == AGP_I810_MISCC_WINSIZE_32)
    217  1.8.2.2  nathanw 		return 32 * 1024 * 1024;
    218  1.8.2.2  nathanw 	else
    219  1.8.2.2  nathanw 		return 64 * 1024 * 1024;
    220  1.8.2.2  nathanw }
    221  1.8.2.2  nathanw 
    222  1.8.2.2  nathanw static int
    223  1.8.2.2  nathanw agp_i810_set_aperture(struct agp_softc *sc, u_int32_t aperture)
    224  1.8.2.2  nathanw {
    225  1.8.2.2  nathanw 	pcireg_t reg, miscc;
    226  1.8.2.2  nathanw 
    227  1.8.2.2  nathanw 	/*
    228  1.8.2.2  nathanw 	 * Double check for sanity.
    229  1.8.2.2  nathanw 	 */
    230  1.8.2.2  nathanw 	if (aperture != 32 * 1024 * 1024 && aperture != 64 * 1024 * 1024) {
    231  1.8.2.2  nathanw 		printf("%s: bad aperture size %d\n", sc->as_dev.dv_xname,
    232  1.8.2.2  nathanw 		       aperture);
    233  1.8.2.2  nathanw 		return EINVAL;
    234  1.8.2.2  nathanw 	}
    235  1.8.2.2  nathanw 
    236  1.8.2.2  nathanw 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
    237  1.8.2.2  nathanw 	miscc = reg >> 16;
    238  1.8.2.2  nathanw 	miscc &= ~AGP_I810_MISCC_WINSIZE;
    239  1.8.2.2  nathanw 	if (aperture == 32 * 1024 * 1024)
    240  1.8.2.2  nathanw 		miscc |= AGP_I810_MISCC_WINSIZE_32;
    241  1.8.2.2  nathanw 	else
    242  1.8.2.2  nathanw 		miscc |= AGP_I810_MISCC_WINSIZE_64;
    243  1.8.2.2  nathanw 
    244  1.8.2.2  nathanw 	reg &= 0x0000ffff;
    245  1.8.2.2  nathanw 	reg |= (miscc << 16);
    246  1.8.2.2  nathanw 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_I810_SMRAM, miscc);
    247  1.8.2.2  nathanw 
    248  1.8.2.2  nathanw 	return 0;
    249  1.8.2.2  nathanw }
    250  1.8.2.2  nathanw 
    251  1.8.2.2  nathanw static int
    252  1.8.2.2  nathanw agp_i810_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
    253  1.8.2.2  nathanw {
    254  1.8.2.2  nathanw 	struct agp_i810_softc *isc = sc->as_chipc;
    255  1.8.2.2  nathanw 
    256  1.8.2.2  nathanw 	if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
    257  1.8.2.2  nathanw 		return EINVAL;
    258  1.8.2.2  nathanw 
    259  1.8.2.2  nathanw 	WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
    260  1.8.2.2  nathanw 	    physical | 1);
    261  1.8.2.2  nathanw 	return 0;
    262  1.8.2.2  nathanw }
    263  1.8.2.2  nathanw 
    264  1.8.2.2  nathanw static int
    265  1.8.2.2  nathanw agp_i810_unbind_page(struct agp_softc *sc, off_t offset)
    266  1.8.2.2  nathanw {
    267  1.8.2.2  nathanw 	struct agp_i810_softc *isc = sc->as_chipc;
    268  1.8.2.2  nathanw 
    269  1.8.2.2  nathanw 	if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
    270  1.8.2.2  nathanw 		return EINVAL;
    271  1.8.2.2  nathanw 
    272  1.8.2.2  nathanw 	WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4, 0);
    273  1.8.2.2  nathanw 	return 0;
    274  1.8.2.2  nathanw }
    275  1.8.2.2  nathanw 
    276  1.8.2.2  nathanw /*
    277  1.8.2.2  nathanw  * Writing via memory mapped registers already flushes all TLBs.
    278  1.8.2.2  nathanw  */
    279  1.8.2.2  nathanw static void
    280  1.8.2.2  nathanw agp_i810_flush_tlb(struct agp_softc *sc)
    281  1.8.2.2  nathanw {
    282  1.8.2.2  nathanw }
    283  1.8.2.2  nathanw 
    284  1.8.2.2  nathanw static int
    285  1.8.2.2  nathanw agp_i810_enable(struct agp_softc *sc, u_int32_t mode)
    286  1.8.2.2  nathanw {
    287  1.8.2.2  nathanw 
    288  1.8.2.2  nathanw 	return 0;
    289  1.8.2.2  nathanw }
    290  1.8.2.2  nathanw 
    291  1.8.2.2  nathanw static struct agp_memory *
    292  1.8.2.2  nathanw agp_i810_alloc_memory(struct agp_softc *sc, int type, vsize_t size)
    293  1.8.2.2  nathanw {
    294  1.8.2.2  nathanw 	struct agp_i810_softc *isc = sc->as_chipc;
    295  1.8.2.2  nathanw 	struct agp_memory *mem;
    296  1.8.2.2  nathanw 
    297  1.8.2.2  nathanw 	if ((size & (AGP_PAGE_SIZE - 1)) != 0)
    298  1.8.2.2  nathanw 		return 0;
    299  1.8.2.2  nathanw 
    300  1.8.2.2  nathanw 	if (sc->as_allocated + size > sc->as_maxmem)
    301  1.8.2.2  nathanw 		return 0;
    302  1.8.2.2  nathanw 
    303  1.8.2.2  nathanw 	if (type == 1) {
    304  1.8.2.2  nathanw 		/*
    305  1.8.2.2  nathanw 		 * Mapping local DRAM into GATT.
    306  1.8.2.2  nathanw 		 */
    307  1.8.2.2  nathanw 		if (size != isc->dcache_size)
    308  1.8.2.2  nathanw 			return 0;
    309  1.8.2.2  nathanw 	} else if (type == 2) {
    310  1.8.2.2  nathanw 		/*
    311  1.8.2.2  nathanw 		 * Bogus mapping of a single page for the hardware cursor.
    312  1.8.2.2  nathanw 		 */
    313  1.8.2.2  nathanw 		if (size != AGP_PAGE_SIZE)
    314  1.8.2.2  nathanw 			return 0;
    315  1.8.2.2  nathanw 	}
    316  1.8.2.2  nathanw 
    317  1.8.2.4  nathanw 	mem = malloc(sizeof *mem, M_AGP, M_WAITOK|M_ZERO);
    318  1.8.2.2  nathanw 	if (mem == NULL)
    319  1.8.2.2  nathanw 		return NULL;
    320  1.8.2.2  nathanw 	mem->am_id = sc->as_nextid++;
    321  1.8.2.2  nathanw 	mem->am_size = size;
    322  1.8.2.2  nathanw 	mem->am_type = type;
    323  1.8.2.2  nathanw 
    324  1.8.2.2  nathanw 	if (type == 2) {
    325  1.8.2.2  nathanw 		/*
    326  1.8.2.2  nathanw 		 * Allocate and wire down the page now so that we can
    327  1.8.2.2  nathanw 		 * get its physical address.
    328  1.8.2.2  nathanw 		 */
    329  1.8.2.2  nathanw 		mem->am_dmaseg = malloc(sizeof *mem->am_dmaseg, M_AGP,
    330  1.8.2.2  nathanw 		    M_WAITOK);
    331  1.8.2.2  nathanw 		if (mem->am_dmaseg == NULL) {
    332  1.8.2.2  nathanw 			free(mem, M_AGP);
    333  1.8.2.2  nathanw 			return NULL;
    334  1.8.2.2  nathanw 		}
    335  1.8.2.2  nathanw 		if (agp_alloc_dmamem(sc->as_dmat, size, 0,
    336  1.8.2.2  nathanw 		    &mem->am_dmamap, &mem->am_virtual, &mem->am_physical,
    337  1.8.2.2  nathanw 		    mem->am_dmaseg, 1, &mem->am_nseg) != 0) {
    338  1.8.2.2  nathanw 			free(mem->am_dmaseg, M_AGP);
    339  1.8.2.2  nathanw 			free(mem, M_AGP);
    340  1.8.2.2  nathanw 			return NULL;
    341  1.8.2.2  nathanw 		}
    342  1.8.2.2  nathanw 	} else if (type != 1) {
    343  1.8.2.2  nathanw 		if (bus_dmamap_create(sc->as_dmat, size, size / PAGE_SIZE + 1,
    344  1.8.2.2  nathanw 				      size, 0, BUS_DMA_NOWAIT,
    345  1.8.2.2  nathanw 				      &mem->am_dmamap) != 0) {
    346  1.8.2.2  nathanw 			free(mem, M_AGP);
    347  1.8.2.2  nathanw 			return NULL;
    348  1.8.2.2  nathanw 		}
    349  1.8.2.2  nathanw 	}
    350  1.8.2.2  nathanw 
    351  1.8.2.2  nathanw 	TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
    352  1.8.2.2  nathanw 	sc->as_allocated += size;
    353  1.8.2.2  nathanw 
    354  1.8.2.2  nathanw 	return mem;
    355  1.8.2.2  nathanw }
    356  1.8.2.2  nathanw 
    357  1.8.2.2  nathanw static int
    358  1.8.2.2  nathanw agp_i810_free_memory(struct agp_softc *sc, struct agp_memory *mem)
    359  1.8.2.2  nathanw {
    360  1.8.2.2  nathanw 	if (mem->am_is_bound)
    361  1.8.2.2  nathanw 		return EBUSY;
    362  1.8.2.2  nathanw 
    363  1.8.2.2  nathanw 	if (mem->am_type == 2) {
    364  1.8.2.2  nathanw 		agp_free_dmamem(sc->as_dmat, mem->am_size, mem->am_dmamap,
    365  1.8.2.2  nathanw 		    mem->am_virtual, mem->am_dmaseg, mem->am_nseg);
    366  1.8.2.2  nathanw 		free(mem->am_dmaseg, M_AGP);
    367  1.8.2.2  nathanw 	}
    368  1.8.2.2  nathanw 
    369  1.8.2.2  nathanw 	sc->as_allocated -= mem->am_size;
    370  1.8.2.2  nathanw 	TAILQ_REMOVE(&sc->as_memory, mem, am_link);
    371  1.8.2.2  nathanw 	free(mem, M_AGP);
    372  1.8.2.2  nathanw 	return 0;
    373  1.8.2.2  nathanw }
    374  1.8.2.2  nathanw 
    375  1.8.2.2  nathanw static int
    376  1.8.2.2  nathanw agp_i810_bind_memory(struct agp_softc *sc, struct agp_memory *mem,
    377  1.8.2.2  nathanw 		     off_t offset)
    378  1.8.2.2  nathanw {
    379  1.8.2.2  nathanw 	struct agp_i810_softc *isc = sc->as_chipc;
    380  1.8.2.2  nathanw 	u_int32_t regval, i;
    381  1.8.2.2  nathanw 
    382  1.8.2.2  nathanw 	/*
    383  1.8.2.2  nathanw 	 * XXX evil hack: the PGTBL_CTL appearently gets overwritten by the
    384  1.8.2.2  nathanw 	 * X server for mysterious reasons which leads to crashes if we write
    385  1.8.2.2  nathanw 	 * to the GTT through the MMIO window.
    386  1.8.2.2  nathanw 	 * Until the issue is solved, simply restore it.
    387  1.8.2.2  nathanw 	 */
    388  1.8.2.2  nathanw 	regval = bus_space_read_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL);
    389  1.8.2.2  nathanw 	if (regval != (isc->gatt->ag_physical | 1)) {
    390  1.8.2.2  nathanw 		printf("agp_i810_bind_memory: PGTBL_CTL is 0x%x - fixing\n",
    391  1.8.2.2  nathanw 		       regval);
    392  1.8.2.2  nathanw 		bus_space_write_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL,
    393  1.8.2.2  nathanw 				  isc->gatt->ag_physical | 1);
    394  1.8.2.2  nathanw 	}
    395  1.8.2.2  nathanw 
    396  1.8.2.2  nathanw 	if (mem->am_type == 2) {
    397  1.8.2.2  nathanw 		WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
    398  1.8.2.2  nathanw 		       mem->am_physical | 1);
    399  1.8.2.2  nathanw 		mem->am_offset = offset;
    400  1.8.2.2  nathanw 		mem->am_is_bound = 1;
    401  1.8.2.2  nathanw 		return 0;
    402  1.8.2.2  nathanw 	}
    403  1.8.2.2  nathanw 
    404  1.8.2.2  nathanw 	if (mem->am_type != 1)
    405  1.8.2.2  nathanw 		return agp_generic_bind_memory(sc, mem, offset);
    406  1.8.2.2  nathanw 
    407  1.8.2.2  nathanw 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
    408  1.8.2.2  nathanw 		WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
    409  1.8.2.2  nathanw 		       i | 3);
    410  1.8.2.2  nathanw 	}
    411  1.8.2.5  nathanw 	mem->am_is_bound = 1;
    412  1.8.2.2  nathanw 	return 0;
    413  1.8.2.2  nathanw }
    414  1.8.2.2  nathanw 
    415  1.8.2.2  nathanw static int
    416  1.8.2.2  nathanw agp_i810_unbind_memory(struct agp_softc *sc, struct agp_memory *mem)
    417  1.8.2.2  nathanw {
    418  1.8.2.2  nathanw 	struct agp_i810_softc *isc = sc->as_chipc;
    419  1.8.2.2  nathanw 	u_int32_t i;
    420  1.8.2.2  nathanw 
    421  1.8.2.2  nathanw 	if (mem->am_type == 2) {
    422  1.8.2.2  nathanw 		WRITE4(AGP_I810_GTT +
    423  1.8.2.2  nathanw 		       (u_int32_t)(mem->am_offset >> AGP_PAGE_SHIFT) * 4,
    424  1.8.2.2  nathanw 		       0);
    425  1.8.2.2  nathanw 		mem->am_offset = 0;
    426  1.8.2.2  nathanw 		mem->am_is_bound = 0;
    427  1.8.2.2  nathanw 		return 0;
    428  1.8.2.2  nathanw 	}
    429  1.8.2.2  nathanw 
    430  1.8.2.2  nathanw 	if (mem->am_type != 1)
    431  1.8.2.2  nathanw 		return agp_generic_unbind_memory(sc, mem);
    432  1.8.2.2  nathanw 
    433  1.8.2.2  nathanw 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
    434  1.8.2.2  nathanw 		WRITE4(AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0);
    435  1.8.2.5  nathanw 	mem->am_is_bound = 0;
    436  1.8.2.2  nathanw 	return 0;
    437  1.8.2.2  nathanw }
    438