Home | History | Annotate | Line # | Download | only in pci
agp_i810.c revision 1.16
      1  1.16  drochner /*	$NetBSD: agp_i810.c,v 1.16 2003/03/24 09:12:55 drochner Exp $	*/
      2   1.1      fvdl 
      3   1.1      fvdl /*-
      4   1.1      fvdl  * Copyright (c) 2000 Doug Rabson
      5   1.1      fvdl  * Copyright (c) 2000 Ruslan Ermilov
      6   1.1      fvdl  * All rights reserved.
      7   1.1      fvdl  *
      8   1.1      fvdl  * Redistribution and use in source and binary forms, with or without
      9   1.1      fvdl  * modification, are permitted provided that the following conditions
     10   1.1      fvdl  * are met:
     11   1.1      fvdl  * 1. Redistributions of source code must retain the above copyright
     12   1.1      fvdl  *    notice, this list of conditions and the following disclaimer.
     13   1.1      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1      fvdl  *    notice, this list of conditions and the following disclaimer in the
     15   1.1      fvdl  *    documentation and/or other materials provided with the distribution.
     16   1.1      fvdl  *
     17   1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18   1.1      fvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19   1.1      fvdl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20   1.1      fvdl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21   1.1      fvdl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22   1.1      fvdl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23   1.1      fvdl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24   1.1      fvdl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25   1.1      fvdl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26   1.1      fvdl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27   1.1      fvdl  * SUCH DAMAGE.
     28   1.1      fvdl  *
     29   1.1      fvdl  *	$FreeBSD: src/sys/pci/agp_i810.c,v 1.4 2001/07/05 21:28:47 jhb Exp $
     30   1.1      fvdl  */
     31   1.9     lukem 
     32   1.9     lukem #include <sys/cdefs.h>
     33  1.16  drochner __KERNEL_RCSID(0, "$NetBSD: agp_i810.c,v 1.16 2003/03/24 09:12:55 drochner Exp $");
     34   1.1      fvdl 
     35   1.1      fvdl #include <sys/param.h>
     36   1.1      fvdl #include <sys/systm.h>
     37   1.1      fvdl #include <sys/malloc.h>
     38   1.1      fvdl #include <sys/kernel.h>
     39   1.1      fvdl #include <sys/lock.h>
     40   1.1      fvdl #include <sys/proc.h>
     41   1.1      fvdl #include <sys/device.h>
     42   1.1      fvdl #include <sys/conf.h>
     43   1.1      fvdl 
     44   1.1      fvdl #include <uvm/uvm_extern.h>
     45   1.1      fvdl 
     46   1.1      fvdl #include <dev/pci/pcivar.h>
     47   1.1      fvdl #include <dev/pci/pcireg.h>
     48   1.1      fvdl #include <dev/pci/pcidevs.h>
     49   1.1      fvdl #include <dev/pci/agpvar.h>
     50   1.1      fvdl #include <dev/pci/agpreg.h>
     51   1.1      fvdl 
     52   1.1      fvdl #include <sys/agpio.h>
     53   1.1      fvdl 
     54   1.1      fvdl #include <machine/bus.h>
     55   1.1      fvdl 
     56   1.1      fvdl #define READ1(off)	bus_space_read_1(isc->bst, isc->bsh, off)
     57  1.14       scw #define READ4(off)	bus_space_read_4(isc->bst, isc->bsh, off)
     58   1.1      fvdl #define WRITE4(off,v)	bus_space_write_4(isc->bst, isc->bsh, off, v)
     59   1.1      fvdl 
     60  1.14       scw #define CHIP_I810 0	/* i810/i815 */
     61  1.14       scw #define CHIP_I830 1	/* i830/i845 */
     62  1.14       scw 
     63   1.1      fvdl struct agp_i810_softc {
     64   1.1      fvdl 	u_int32_t initial_aperture;	/* aperture size at startup */
     65   1.1      fvdl 	struct agp_gatt *gatt;
     66  1.14       scw 	int chiptype;			/* i810-like or i830 */
     67  1.14       scw 	u_int32_t dcache_size;		/* i810 only */
     68  1.14       scw 	u_int32_t stolen;		/* number of i830/845 gtt entries
     69  1.14       scw 					   for stolen memory */
     70   1.1      fvdl 	bus_space_tag_t bst;		/* bus_space tag */
     71   1.1      fvdl 	bus_space_handle_t bsh;		/* bus_space handle */
     72   1.1      fvdl 	struct pci_attach_args vga_pa;
     73   1.1      fvdl };
     74   1.1      fvdl 
     75   1.1      fvdl static u_int32_t agp_i810_get_aperture(struct agp_softc *);
     76   1.1      fvdl static int agp_i810_set_aperture(struct agp_softc *, u_int32_t);
     77   1.1      fvdl static int agp_i810_bind_page(struct agp_softc *, off_t, bus_addr_t);
     78   1.1      fvdl static int agp_i810_unbind_page(struct agp_softc *, off_t);
     79   1.1      fvdl static void agp_i810_flush_tlb(struct agp_softc *);
     80   1.1      fvdl static int agp_i810_enable(struct agp_softc *, u_int32_t mode);
     81   1.1      fvdl static struct agp_memory *agp_i810_alloc_memory(struct agp_softc *, int,
     82   1.1      fvdl 						vsize_t);
     83   1.1      fvdl static int agp_i810_free_memory(struct agp_softc *, struct agp_memory *);
     84   1.1      fvdl static int agp_i810_bind_memory(struct agp_softc *, struct agp_memory *, off_t);
     85   1.1      fvdl static int agp_i810_unbind_memory(struct agp_softc *, struct agp_memory *);
     86   1.1      fvdl 
     87   1.1      fvdl struct agp_methods agp_i810_methods = {
     88   1.1      fvdl 	agp_i810_get_aperture,
     89   1.1      fvdl 	agp_i810_set_aperture,
     90   1.1      fvdl 	agp_i810_bind_page,
     91   1.1      fvdl 	agp_i810_unbind_page,
     92   1.1      fvdl 	agp_i810_flush_tlb,
     93   1.1      fvdl 	agp_i810_enable,
     94   1.1      fvdl 	agp_i810_alloc_memory,
     95   1.1      fvdl 	agp_i810_free_memory,
     96   1.1      fvdl 	agp_i810_bind_memory,
     97   1.1      fvdl 	agp_i810_unbind_memory,
     98   1.1      fvdl };
     99   1.1      fvdl 
    100   1.6   thorpej /* XXXthorpej -- duplicated code (see arch/i386/pci/pchb.c) */
    101   1.1      fvdl static int
    102   1.1      fvdl agp_i810_vgamatch(struct pci_attach_args *pa)
    103   1.1      fvdl {
    104   1.6   thorpej 
    105   1.2      fvdl 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
    106   1.2      fvdl 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
    107   1.6   thorpej 		return (0);
    108   1.6   thorpej 
    109   1.1      fvdl 	switch (PCI_PRODUCT(pa->pa_id)) {
    110   1.1      fvdl 	case PCI_PRODUCT_INTEL_82810_GC:
    111   1.1      fvdl 	case PCI_PRODUCT_INTEL_82810_DC100_GC:
    112   1.1      fvdl 	case PCI_PRODUCT_INTEL_82810E_GC:
    113   1.1      fvdl 	case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
    114  1.14       scw 	case PCI_PRODUCT_INTEL_82830MP_IV:
    115  1.14       scw 	case PCI_PRODUCT_INTEL_82845G_IGD:
    116   1.6   thorpej 		return (1);
    117   1.1      fvdl 	}
    118   1.1      fvdl 
    119   1.6   thorpej 	return (0);
    120   1.1      fvdl }
    121   1.1      fvdl 
    122   1.1      fvdl int
    123   1.1      fvdl agp_i810_attach(struct device *parent, struct device *self, void *aux)
    124   1.1      fvdl {
    125   1.1      fvdl 	struct agp_softc *sc = (void *)self;
    126   1.1      fvdl 	struct agp_i810_softc *isc;
    127   1.1      fvdl 	struct agp_gatt *gatt;
    128   1.1      fvdl 	int error;
    129   1.1      fvdl 
    130  1.10   tsutsui 	isc = malloc(sizeof *isc, M_AGP, M_NOWAIT|M_ZERO);
    131   1.1      fvdl 	if (isc == NULL) {
    132  1.15   thorpej 		aprint_error(": can't allocate chipset-specific softc\n");
    133   1.1      fvdl 		return ENOMEM;
    134   1.1      fvdl 	}
    135   1.1      fvdl 	sc->as_chipc = isc;
    136   1.1      fvdl 	sc->as_methods = &agp_i810_methods;
    137   1.1      fvdl 
    138   1.1      fvdl 	if (pci_find_device(&isc->vga_pa, agp_i810_vgamatch) == 0) {
    139  1.15   thorpej 		aprint_error(": can't find internal VGA device config space\n");
    140   1.1      fvdl 		free(isc, M_AGP);
    141   1.1      fvdl 		return ENOENT;
    142   1.1      fvdl 	}
    143   1.1      fvdl 
    144   1.1      fvdl 	/* XXXfvdl */
    145   1.1      fvdl 	sc->as_dmat = isc->vga_pa.pa_dmat;
    146   1.1      fvdl 
    147   1.1      fvdl 	error = agp_map_aperture(&isc->vga_pa, sc);
    148   1.1      fvdl 	if (error != 0) {
    149  1.15   thorpej 		aprint_error(": can't map aperture\n");
    150   1.1      fvdl 		free(isc, M_AGP);
    151   1.1      fvdl 		return error;
    152   1.1      fvdl 	}
    153   1.1      fvdl 
    154  1.14       scw 	switch (PCI_PRODUCT(isc->vga_pa.pa_id)) {
    155  1.14       scw 	case PCI_PRODUCT_INTEL_82810_GC:
    156  1.14       scw 	case PCI_PRODUCT_INTEL_82810_DC100_GC:
    157  1.14       scw 	case PCI_PRODUCT_INTEL_82810E_GC:
    158  1.14       scw 	case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
    159  1.14       scw 		isc->chiptype = CHIP_I810;
    160  1.14       scw 		break;
    161  1.14       scw 	case PCI_PRODUCT_INTEL_82830MP_IV:
    162  1.14       scw 	case PCI_PRODUCT_INTEL_82845G_IGD:
    163  1.14       scw 		isc->chiptype = CHIP_I830;
    164  1.14       scw 		break;
    165  1.14       scw 	}
    166  1.14       scw 
    167   1.1      fvdl 	error = pci_mapreg_map(&isc->vga_pa, AGP_I810_MMADR,
    168   1.1      fvdl 	    PCI_MAPREG_TYPE_MEM, 0, &isc->bst, &isc->bsh, NULL, NULL);
    169   1.1      fvdl 	if (error != 0) {
    170  1.15   thorpej 		aprint_error(": can't map mmadr registers\n");
    171   1.1      fvdl 		return error;
    172   1.1      fvdl 	}
    173   1.1      fvdl 
    174   1.1      fvdl 	isc->initial_aperture = AGP_GET_APERTURE(sc);
    175   1.1      fvdl 
    176  1.14       scw 	gatt = malloc(sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
    177  1.14       scw 	if (!gatt) {
    178  1.14       scw  		agp_generic_detach(sc);
    179  1.14       scw  		return ENOMEM;
    180  1.14       scw 	}
    181  1.14       scw 	isc->gatt = gatt;
    182  1.14       scw 
    183  1.14       scw 	gatt->ag_entries = AGP_GET_APERTURE(sc) >> AGP_PAGE_SHIFT;
    184   1.1      fvdl 
    185  1.14       scw 	if (isc->chiptype == CHIP_I810) {
    186  1.14       scw 		int dummyseg;
    187  1.14       scw 		/* Some i810s have on-chip memory called dcache */
    188  1.14       scw 		if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
    189  1.14       scw 			isc->dcache_size = 4 * 1024 * 1024;
    190  1.14       scw 		else
    191  1.14       scw 			isc->dcache_size = 0;
    192  1.14       scw 
    193  1.14       scw 		/* According to the specs the gatt on the i810 must be 64k */
    194  1.14       scw 		if (agp_alloc_dmamem(sc->as_dmat, 64 * 1024,
    195  1.14       scw 		    0, &gatt->ag_dmamap, (caddr_t *)&gatt->ag_virtual,
    196  1.14       scw 		    &gatt->ag_physical, &gatt->ag_dmaseg, 1, &dummyseg) != 0) {
    197  1.14       scw 			free(gatt, M_AGP);
    198   1.1      fvdl 			agp_generic_detach(sc);
    199   1.1      fvdl 			return ENOMEM;
    200   1.1      fvdl 		}
    201  1.14       scw 
    202  1.14       scw 		gatt->ag_size = gatt->ag_entries * sizeof(u_int32_t);
    203  1.14       scw 		memset(gatt->ag_virtual, 0, gatt->ag_size);
    204  1.14       scw 
    205  1.14       scw 		agp_flush_cache();
    206  1.14       scw 		/* Install the GATT. */
    207  1.14       scw 		WRITE4(AGP_I810_PGTBL_CTL, gatt->ag_physical | 1);
    208  1.14       scw 	} else {
    209  1.14       scw 		/* The i830 automatically initializes the 128k gatt on boot. */
    210  1.14       scw 		pcireg_t reg;
    211  1.14       scw 		u_int32_t pgtblctl;
    212  1.14       scw 		u_int16_t gcc1;
    213  1.14       scw 
    214  1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
    215  1.14       scw 		gcc1 = (u_int16_t)(reg >> 16);
    216  1.14       scw 		switch (gcc1 & AGP_I830_GCC1_GMS) {
    217  1.14       scw 		case AGP_I830_GCC1_GMS_STOLEN_512:
    218  1.14       scw 			isc->stolen = (512 - 132) * 1024 / 4096;
    219  1.14       scw 			break;
    220  1.14       scw 		case AGP_I830_GCC1_GMS_STOLEN_1024:
    221  1.14       scw 			isc->stolen = (1024 - 132) * 1024 / 4096;
    222  1.14       scw 			break;
    223  1.14       scw 		case AGP_I830_GCC1_GMS_STOLEN_8192:
    224  1.14       scw 			isc->stolen = (8192 - 132) * 1024 / 4096;
    225  1.14       scw 			break;
    226  1.14       scw 		default:
    227  1.14       scw 			isc->stolen = 0;
    228  1.15   thorpej 			aprint_error(
    229  1.15   thorpej 			    ": unknown memory configuration, disabling\n");
    230  1.14       scw 			agp_generic_detach(sc);
    231  1.14       scw 			return EINVAL;
    232  1.14       scw 		}
    233  1.14       scw 		if (isc->stolen > 0) {
    234  1.15   thorpej 			aprint_error(": detected %dk stolen memory\n",
    235  1.14       scw 			    isc->stolen * 4);
    236  1.14       scw 		}
    237  1.15   thorpej 		aprint_error("%s: aperture size is %dM\n", sc->as_dev.dv_xname,
    238  1.14       scw 		       isc->initial_aperture / 1024 / 1024);
    239  1.14       scw 
    240  1.14       scw 		/* GATT address is already in there, make sure it's enabled */
    241  1.14       scw 		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
    242  1.14       scw 		pgtblctl |= 1;
    243  1.14       scw 		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
    244  1.14       scw 
    245  1.14       scw 		gatt->ag_physical = pgtblctl & ~1;
    246   1.1      fvdl 	}
    247   1.1      fvdl 
    248   1.1      fvdl 	/*
    249   1.1      fvdl 	 * Make sure the chipset can see everything.
    250   1.1      fvdl 	 */
    251   1.1      fvdl 	agp_flush_cache();
    252  1.14       scw 
    253   1.1      fvdl 	return 0;
    254   1.1      fvdl }
    255   1.1      fvdl 
    256   1.1      fvdl #if 0
    257   1.1      fvdl static int
    258   1.1      fvdl agp_i810_detach(struct agp_softc *sc)
    259   1.1      fvdl {
    260   1.1      fvdl 	int error;
    261   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    262   1.1      fvdl 
    263   1.1      fvdl 	error = agp_generic_detach(sc);
    264   1.1      fvdl 	if (error)
    265   1.1      fvdl 		return error;
    266   1.1      fvdl 
    267   1.1      fvdl 	/* Clear the GATT base. */
    268  1.14       scw 	if (sc->chiptype == CHIP_I810) {
    269  1.14       scw 		WRITE4(AGP_I810_PGTBL_CTL, 0);
    270  1.14       scw 	} else {
    271  1.14       scw 		unsigned int pgtblctl;
    272  1.14       scw 		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
    273  1.14       scw 		pgtblctl &= ~1;
    274  1.14       scw 		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
    275  1.14       scw 	}
    276   1.1      fvdl 
    277   1.1      fvdl 	/* Put the aperture back the way it started. */
    278   1.1      fvdl 	AGP_SET_APERTURE(sc, isc->initial_aperture);
    279   1.1      fvdl 
    280  1.14       scw 	if (sc->chiptype == CHIP_I810) {
    281  1.14       scw 		agp_free_dmamem(sc->as_dmat, gatt->ag_size, gatt->ag_dmamap,
    282  1.14       scw 		    (caddr_t)gatt->ag_virtual, &gatt->ag_dmaseg, 1);
    283  1.14       scw 	}
    284  1.14       scw 	free(sc->gatt, M_AGP);
    285   1.1      fvdl 
    286   1.1      fvdl 	return 0;
    287   1.1      fvdl }
    288   1.1      fvdl #endif
    289   1.1      fvdl 
    290   1.1      fvdl static u_int32_t
    291   1.1      fvdl agp_i810_get_aperture(struct agp_softc *sc)
    292   1.1      fvdl {
    293  1.14       scw 	struct agp_i810_softc *isc = sc->as_chipc;
    294  1.14       scw 	pcireg_t reg;
    295  1.14       scw 
    296  1.14       scw 	if (isc->chiptype == CHIP_I810) {
    297  1.14       scw 		u_int16_t miscc;
    298   1.1      fvdl 
    299  1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
    300  1.14       scw 		miscc = (u_int16_t)(reg >> 16);
    301  1.14       scw 		if ((miscc & AGP_I810_MISCC_WINSIZE) ==
    302  1.14       scw 		    AGP_I810_MISCC_WINSIZE_32)
    303  1.14       scw 			return 32 * 1024 * 1024;
    304  1.14       scw 		else
    305  1.14       scw 			return 64 * 1024 * 1024;
    306  1.14       scw 	} else {		/* I830 */
    307  1.14       scw 		u_int16_t gcc1;
    308  1.14       scw 
    309  1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
    310  1.14       scw 		gcc1 = (u_int16_t)(reg >> 16);
    311  1.14       scw 		if ((gcc1 & AGP_I830_GCC1_GMASIZE) == AGP_I830_GCC1_GMASIZE_64)
    312  1.14       scw 			return 64 * 1024 * 1024;
    313  1.14       scw 		else
    314  1.14       scw 			return 128 * 1024 * 1024;
    315  1.14       scw 	}
    316   1.1      fvdl }
    317   1.1      fvdl 
    318   1.1      fvdl static int
    319   1.1      fvdl agp_i810_set_aperture(struct agp_softc *sc, u_int32_t aperture)
    320   1.1      fvdl {
    321  1.14       scw 	struct agp_i810_softc *isc = sc->as_chipc;
    322  1.14       scw 	pcireg_t reg;
    323  1.14       scw 
    324  1.14       scw 	if (isc->chiptype == CHIP_I810) {
    325  1.14       scw 		u_int16_t miscc;
    326  1.14       scw 
    327  1.14       scw 		/*
    328  1.14       scw 		 * Double check for sanity.
    329  1.14       scw 		 */
    330  1.14       scw 		if (aperture != (32 * 1024 * 1024) &&
    331  1.14       scw 		    aperture != (64 * 1024 * 1024)) {
    332  1.14       scw 			printf("%s: bad aperture size %d\n",
    333  1.14       scw 			    sc->as_dev.dv_xname, aperture);
    334  1.14       scw 			return EINVAL;
    335  1.14       scw 		}
    336   1.1      fvdl 
    337  1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
    338  1.14       scw 		miscc = (u_int16_t)(reg >> 16);
    339  1.14       scw 		miscc &= ~AGP_I810_MISCC_WINSIZE;
    340  1.14       scw 		if (aperture == 32 * 1024 * 1024)
    341  1.14       scw 			miscc |= AGP_I810_MISCC_WINSIZE_32;
    342  1.14       scw 		else
    343  1.14       scw 			miscc |= AGP_I810_MISCC_WINSIZE_64;
    344  1.14       scw 
    345  1.14       scw 		reg &= 0x0000ffff;
    346  1.14       scw 		reg |= ((pcireg_t)miscc) << 16;
    347  1.14       scw 		pci_conf_write(sc->as_pc, sc->as_tag, AGP_I810_SMRAM, reg);
    348  1.14       scw 	} else {		/* I830 */
    349  1.14       scw 		u_int16_t gcc1;
    350  1.14       scw 
    351  1.14       scw 		if (aperture != (64 * 1024 * 1024) &&
    352  1.14       scw 		    aperture != (128 * 1024 * 1024)) {
    353  1.14       scw 			printf("%s: bad aperture size %d\n",
    354  1.14       scw 			    sc->as_dev.dv_xname, aperture);
    355  1.14       scw 			return EINVAL;
    356  1.14       scw 		}
    357  1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
    358  1.14       scw 		gcc1 = (u_int16_t)(reg >> 16);
    359  1.14       scw 		gcc1 &= ~AGP_I830_GCC1_GMASIZE;
    360  1.14       scw 		if (aperture == 64 * 1024 * 1024)
    361  1.14       scw 			gcc1 |= AGP_I830_GCC1_GMASIZE_64;
    362  1.14       scw 		else
    363  1.14       scw 			gcc1 |= AGP_I830_GCC1_GMASIZE_128;
    364  1.14       scw 
    365  1.14       scw 		reg &= 0x0000ffff;
    366  1.14       scw 		reg |= ((pcireg_t)gcc1) << 16;
    367  1.14       scw 		pci_conf_write(sc->as_pc, sc->as_tag, AGP_I830_GCC0, reg);
    368   1.1      fvdl 	}
    369   1.1      fvdl 
    370   1.1      fvdl 	return 0;
    371   1.1      fvdl }
    372   1.1      fvdl 
    373   1.1      fvdl static int
    374   1.1      fvdl agp_i810_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
    375   1.1      fvdl {
    376   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    377   1.1      fvdl 
    378  1.14       scw 	if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
    379  1.14       scw #ifdef DEBUG
    380  1.14       scw 		printf("%s: failed: offset 0x%08x, shift %d, entries %d\n",
    381  1.14       scw 		    sc->as_dev.dv_xname, (int)offset, AGP_PAGE_SHIFT,
    382  1.14       scw 		    isc->gatt->ag_entries);
    383  1.14       scw #endif
    384   1.1      fvdl 		return EINVAL;
    385  1.14       scw 	}
    386  1.14       scw 
    387  1.14       scw 	if (isc->chiptype == CHIP_I810) {
    388  1.14       scw 		if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
    389  1.14       scw #ifdef DEBUG
    390  1.14       scw 			printf("%s: trying to bind into stolen memory",
    391  1.14       scw 			    sc->as_dev.dv_xname);
    392  1.14       scw #endif
    393  1.14       scw 			return EINVAL;
    394  1.14       scw 		}
    395  1.14       scw 	}
    396   1.1      fvdl 
    397   1.1      fvdl 	WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
    398   1.1      fvdl 	    physical | 1);
    399   1.1      fvdl 	return 0;
    400   1.1      fvdl }
    401   1.1      fvdl 
    402   1.1      fvdl static int
    403   1.1      fvdl agp_i810_unbind_page(struct agp_softc *sc, off_t offset)
    404   1.1      fvdl {
    405   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    406   1.1      fvdl 
    407   1.1      fvdl 	if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
    408   1.1      fvdl 		return EINVAL;
    409   1.1      fvdl 
    410  1.14       scw 	if (isc->chiptype == CHIP_I830 ) {
    411  1.14       scw 		if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
    412  1.14       scw #ifdef DEBUG
    413  1.14       scw 			printf("%s: trying to unbind from stolen memory",
    414  1.14       scw 			    sc->as_dev.dv_xname);
    415  1.14       scw #endif
    416  1.14       scw 			return EINVAL;
    417  1.14       scw 		}
    418  1.14       scw 	}
    419  1.14       scw 
    420   1.1      fvdl 	WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4, 0);
    421   1.1      fvdl 	return 0;
    422   1.1      fvdl }
    423   1.1      fvdl 
    424   1.1      fvdl /*
    425   1.1      fvdl  * Writing via memory mapped registers already flushes all TLBs.
    426   1.1      fvdl  */
    427   1.1      fvdl static void
    428   1.1      fvdl agp_i810_flush_tlb(struct agp_softc *sc)
    429   1.1      fvdl {
    430   1.1      fvdl }
    431   1.1      fvdl 
    432   1.1      fvdl static int
    433   1.1      fvdl agp_i810_enable(struct agp_softc *sc, u_int32_t mode)
    434   1.1      fvdl {
    435   1.1      fvdl 
    436   1.1      fvdl 	return 0;
    437   1.1      fvdl }
    438   1.1      fvdl 
    439   1.1      fvdl static struct agp_memory *
    440   1.1      fvdl agp_i810_alloc_memory(struct agp_softc *sc, int type, vsize_t size)
    441   1.1      fvdl {
    442   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    443   1.1      fvdl 	struct agp_memory *mem;
    444   1.1      fvdl 
    445   1.1      fvdl 	if ((size & (AGP_PAGE_SIZE - 1)) != 0)
    446   1.1      fvdl 		return 0;
    447   1.1      fvdl 
    448   1.1      fvdl 	if (sc->as_allocated + size > sc->as_maxmem)
    449   1.1      fvdl 		return 0;
    450   1.1      fvdl 
    451   1.1      fvdl 	if (type == 1) {
    452   1.1      fvdl 		/*
    453   1.1      fvdl 		 * Mapping local DRAM into GATT.
    454   1.1      fvdl 		 */
    455  1.14       scw 		if (isc->chiptype == CHIP_I830 )
    456  1.14       scw 			return 0;
    457   1.1      fvdl 		if (size != isc->dcache_size)
    458   1.1      fvdl 			return 0;
    459   1.1      fvdl 	} else if (type == 2) {
    460   1.1      fvdl 		/*
    461   1.1      fvdl 		 * Bogus mapping of a single page for the hardware cursor.
    462   1.1      fvdl 		 */
    463   1.1      fvdl 		if (size != AGP_PAGE_SIZE)
    464   1.1      fvdl 			return 0;
    465   1.1      fvdl 	}
    466   1.1      fvdl 
    467  1.10   tsutsui 	mem = malloc(sizeof *mem, M_AGP, M_WAITOK|M_ZERO);
    468   1.1      fvdl 	if (mem == NULL)
    469   1.1      fvdl 		return NULL;
    470   1.1      fvdl 	mem->am_id = sc->as_nextid++;
    471   1.1      fvdl 	mem->am_size = size;
    472   1.1      fvdl 	mem->am_type = type;
    473   1.1      fvdl 
    474   1.1      fvdl 	if (type == 2) {
    475   1.1      fvdl 		/*
    476   1.1      fvdl 		 * Allocate and wire down the page now so that we can
    477   1.1      fvdl 		 * get its physical address.
    478   1.1      fvdl 		 */
    479   1.1      fvdl 		mem->am_dmaseg = malloc(sizeof *mem->am_dmaseg, M_AGP,
    480   1.1      fvdl 		    M_WAITOK);
    481   1.1      fvdl 		if (mem->am_dmaseg == NULL) {
    482   1.1      fvdl 			free(mem, M_AGP);
    483   1.1      fvdl 			return NULL;
    484   1.1      fvdl 		}
    485   1.1      fvdl 		if (agp_alloc_dmamem(sc->as_dmat, size, 0,
    486   1.1      fvdl 		    &mem->am_dmamap, &mem->am_virtual, &mem->am_physical,
    487   1.1      fvdl 		    mem->am_dmaseg, 1, &mem->am_nseg) != 0) {
    488   1.1      fvdl 			free(mem->am_dmaseg, M_AGP);
    489   1.1      fvdl 			free(mem, M_AGP);
    490   1.1      fvdl 			return NULL;
    491   1.1      fvdl 		}
    492   1.1      fvdl 	} else if (type != 1) {
    493   1.4  drochner 		if (bus_dmamap_create(sc->as_dmat, size, size / PAGE_SIZE + 1,
    494   1.4  drochner 				      size, 0, BUS_DMA_NOWAIT,
    495   1.4  drochner 				      &mem->am_dmamap) != 0) {
    496   1.1      fvdl 			free(mem, M_AGP);
    497   1.1      fvdl 			return NULL;
    498   1.1      fvdl 		}
    499   1.1      fvdl 	}
    500   1.1      fvdl 
    501   1.1      fvdl 	TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
    502   1.1      fvdl 	sc->as_allocated += size;
    503   1.1      fvdl 
    504   1.1      fvdl 	return mem;
    505   1.1      fvdl }
    506   1.1      fvdl 
    507   1.1      fvdl static int
    508   1.1      fvdl agp_i810_free_memory(struct agp_softc *sc, struct agp_memory *mem)
    509   1.1      fvdl {
    510   1.1      fvdl 	if (mem->am_is_bound)
    511   1.1      fvdl 		return EBUSY;
    512   1.1      fvdl 
    513   1.1      fvdl 	if (mem->am_type == 2) {
    514   1.1      fvdl 		agp_free_dmamem(sc->as_dmat, mem->am_size, mem->am_dmamap,
    515   1.1      fvdl 		    mem->am_virtual, mem->am_dmaseg, mem->am_nseg);
    516   1.1      fvdl 		free(mem->am_dmaseg, M_AGP);
    517   1.1      fvdl 	}
    518   1.1      fvdl 
    519   1.1      fvdl 	sc->as_allocated -= mem->am_size;
    520   1.1      fvdl 	TAILQ_REMOVE(&sc->as_memory, mem, am_link);
    521   1.1      fvdl 	free(mem, M_AGP);
    522   1.1      fvdl 	return 0;
    523   1.1      fvdl }
    524   1.1      fvdl 
    525   1.1      fvdl static int
    526   1.1      fvdl agp_i810_bind_memory(struct agp_softc *sc, struct agp_memory *mem,
    527   1.1      fvdl 		     off_t offset)
    528   1.1      fvdl {
    529   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    530   1.4  drochner 	u_int32_t regval, i;
    531   1.4  drochner 
    532   1.4  drochner 	/*
    533   1.4  drochner 	 * XXX evil hack: the PGTBL_CTL appearently gets overwritten by the
    534   1.4  drochner 	 * X server for mysterious reasons which leads to crashes if we write
    535   1.4  drochner 	 * to the GTT through the MMIO window.
    536   1.4  drochner 	 * Until the issue is solved, simply restore it.
    537   1.4  drochner 	 */
    538   1.4  drochner 	regval = bus_space_read_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL);
    539   1.4  drochner 	if (regval != (isc->gatt->ag_physical | 1)) {
    540   1.4  drochner 		printf("agp_i810_bind_memory: PGTBL_CTL is 0x%x - fixing\n",
    541   1.4  drochner 		       regval);
    542   1.4  drochner 		bus_space_write_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL,
    543   1.4  drochner 				  isc->gatt->ag_physical | 1);
    544   1.4  drochner 	}
    545   1.1      fvdl 
    546   1.5  drochner 	if (mem->am_type == 2) {
    547   1.5  drochner 		WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
    548   1.5  drochner 		       mem->am_physical | 1);
    549   1.5  drochner 		mem->am_offset = offset;
    550   1.5  drochner 		mem->am_is_bound = 1;
    551   1.1      fvdl 		return 0;
    552   1.5  drochner 	}
    553   1.5  drochner 
    554   1.1      fvdl 	if (mem->am_type != 1)
    555   1.1      fvdl 		return agp_generic_bind_memory(sc, mem, offset);
    556   1.1      fvdl 
    557  1.14       scw 	if (isc->chiptype == CHIP_I830)
    558  1.14       scw 		return EINVAL;
    559  1.14       scw 
    560   1.1      fvdl 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
    561   1.1      fvdl 		WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
    562   1.1      fvdl 		       i | 3);
    563   1.1      fvdl 	}
    564  1.13  drochner 	mem->am_is_bound = 1;
    565   1.1      fvdl 	return 0;
    566   1.1      fvdl }
    567   1.1      fvdl 
    568   1.1      fvdl static int
    569   1.1      fvdl agp_i810_unbind_memory(struct agp_softc *sc, struct agp_memory *mem)
    570   1.1      fvdl {
    571   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    572   1.1      fvdl 	u_int32_t i;
    573   1.1      fvdl 
    574   1.5  drochner 	if (mem->am_type == 2) {
    575   1.5  drochner 		WRITE4(AGP_I810_GTT +
    576   1.5  drochner 		       (u_int32_t)(mem->am_offset >> AGP_PAGE_SHIFT) * 4,
    577   1.5  drochner 		       0);
    578   1.5  drochner 		mem->am_offset = 0;
    579   1.5  drochner 		mem->am_is_bound = 0;
    580   1.1      fvdl 		return 0;
    581   1.5  drochner 	}
    582   1.1      fvdl 
    583   1.1      fvdl 	if (mem->am_type != 1)
    584   1.1      fvdl 		return agp_generic_unbind_memory(sc, mem);
    585  1.14       scw 
    586  1.14       scw 	if (isc->chiptype == CHIP_I830)
    587  1.14       scw 		return EINVAL;
    588   1.1      fvdl 
    589   1.1      fvdl 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
    590   1.1      fvdl 		WRITE4(AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0);
    591  1.13  drochner 	mem->am_is_bound = 0;
    592   1.1      fvdl 	return 0;
    593   1.1      fvdl }
    594