Home | History | Annotate | Line # | Download | only in pci
agp_i810.c revision 1.18
      1  1.18      tron /*	$NetBSD: agp_i810.c,v 1.18 2003/08/26 17:33:23 tron 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.18      tron __KERNEL_RCSID(0, "$NetBSD: agp_i810.c,v 1.18 2003/08/26 17:33:23 tron 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.17   hannken #define CHIP_I830 1	/* 830M/845G */
     62  1.17   hannken #define CHIP_I855 2	/* 852GM/855GM/865G */
     63  1.14       scw 
     64   1.1      fvdl struct agp_i810_softc {
     65   1.1      fvdl 	u_int32_t initial_aperture;	/* aperture size at startup */
     66   1.1      fvdl 	struct agp_gatt *gatt;
     67  1.14       scw 	int chiptype;			/* i810-like or i830 */
     68  1.14       scw 	u_int32_t dcache_size;		/* i810 only */
     69  1.14       scw 	u_int32_t stolen;		/* number of i830/845 gtt entries
     70  1.14       scw 					   for stolen memory */
     71   1.1      fvdl 	bus_space_tag_t bst;		/* bus_space tag */
     72   1.1      fvdl 	bus_space_handle_t bsh;		/* bus_space handle */
     73   1.1      fvdl 	struct pci_attach_args vga_pa;
     74   1.1      fvdl };
     75   1.1      fvdl 
     76   1.1      fvdl static u_int32_t agp_i810_get_aperture(struct agp_softc *);
     77   1.1      fvdl static int agp_i810_set_aperture(struct agp_softc *, u_int32_t);
     78   1.1      fvdl static int agp_i810_bind_page(struct agp_softc *, off_t, bus_addr_t);
     79   1.1      fvdl static int agp_i810_unbind_page(struct agp_softc *, off_t);
     80   1.1      fvdl static void agp_i810_flush_tlb(struct agp_softc *);
     81   1.1      fvdl static int agp_i810_enable(struct agp_softc *, u_int32_t mode);
     82   1.1      fvdl static struct agp_memory *agp_i810_alloc_memory(struct agp_softc *, int,
     83   1.1      fvdl 						vsize_t);
     84   1.1      fvdl static int agp_i810_free_memory(struct agp_softc *, struct agp_memory *);
     85   1.1      fvdl static int agp_i810_bind_memory(struct agp_softc *, struct agp_memory *, off_t);
     86   1.1      fvdl static int agp_i810_unbind_memory(struct agp_softc *, struct agp_memory *);
     87   1.1      fvdl 
     88   1.1      fvdl struct agp_methods agp_i810_methods = {
     89   1.1      fvdl 	agp_i810_get_aperture,
     90   1.1      fvdl 	agp_i810_set_aperture,
     91   1.1      fvdl 	agp_i810_bind_page,
     92   1.1      fvdl 	agp_i810_unbind_page,
     93   1.1      fvdl 	agp_i810_flush_tlb,
     94   1.1      fvdl 	agp_i810_enable,
     95   1.1      fvdl 	agp_i810_alloc_memory,
     96   1.1      fvdl 	agp_i810_free_memory,
     97   1.1      fvdl 	agp_i810_bind_memory,
     98   1.1      fvdl 	agp_i810_unbind_memory,
     99   1.1      fvdl };
    100   1.1      fvdl 
    101   1.6   thorpej /* XXXthorpej -- duplicated code (see arch/i386/pci/pchb.c) */
    102   1.1      fvdl static int
    103   1.1      fvdl agp_i810_vgamatch(struct pci_attach_args *pa)
    104   1.1      fvdl {
    105   1.6   thorpej 
    106   1.2      fvdl 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
    107   1.2      fvdl 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
    108   1.6   thorpej 		return (0);
    109   1.6   thorpej 
    110   1.1      fvdl 	switch (PCI_PRODUCT(pa->pa_id)) {
    111   1.1      fvdl 	case PCI_PRODUCT_INTEL_82810_GC:
    112   1.1      fvdl 	case PCI_PRODUCT_INTEL_82810_DC100_GC:
    113   1.1      fvdl 	case PCI_PRODUCT_INTEL_82810E_GC:
    114   1.1      fvdl 	case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
    115  1.14       scw 	case PCI_PRODUCT_INTEL_82830MP_IV:
    116  1.14       scw 	case PCI_PRODUCT_INTEL_82845G_IGD:
    117  1.17   hannken 	case PCI_PRODUCT_INTEL_82855GM_IGD:
    118  1.18      tron 	case PCI_PRODUCT_INTEL_82865_IGD:
    119   1.6   thorpej 		return (1);
    120   1.1      fvdl 	}
    121   1.1      fvdl 
    122   1.6   thorpej 	return (0);
    123   1.1      fvdl }
    124   1.1      fvdl 
    125   1.1      fvdl int
    126   1.1      fvdl agp_i810_attach(struct device *parent, struct device *self, void *aux)
    127   1.1      fvdl {
    128   1.1      fvdl 	struct agp_softc *sc = (void *)self;
    129   1.1      fvdl 	struct agp_i810_softc *isc;
    130   1.1      fvdl 	struct agp_gatt *gatt;
    131   1.1      fvdl 	int error;
    132   1.1      fvdl 
    133  1.10   tsutsui 	isc = malloc(sizeof *isc, M_AGP, M_NOWAIT|M_ZERO);
    134   1.1      fvdl 	if (isc == NULL) {
    135  1.15   thorpej 		aprint_error(": can't allocate chipset-specific softc\n");
    136   1.1      fvdl 		return ENOMEM;
    137   1.1      fvdl 	}
    138   1.1      fvdl 	sc->as_chipc = isc;
    139   1.1      fvdl 	sc->as_methods = &agp_i810_methods;
    140   1.1      fvdl 
    141   1.1      fvdl 	if (pci_find_device(&isc->vga_pa, agp_i810_vgamatch) == 0) {
    142  1.15   thorpej 		aprint_error(": can't find internal VGA device config space\n");
    143   1.1      fvdl 		free(isc, M_AGP);
    144   1.1      fvdl 		return ENOENT;
    145   1.1      fvdl 	}
    146   1.1      fvdl 
    147   1.1      fvdl 	/* XXXfvdl */
    148   1.1      fvdl 	sc->as_dmat = isc->vga_pa.pa_dmat;
    149   1.1      fvdl 
    150   1.1      fvdl 	error = agp_map_aperture(&isc->vga_pa, sc);
    151   1.1      fvdl 	if (error != 0) {
    152  1.15   thorpej 		aprint_error(": can't map aperture\n");
    153   1.1      fvdl 		free(isc, M_AGP);
    154   1.1      fvdl 		return error;
    155   1.1      fvdl 	}
    156   1.1      fvdl 
    157  1.14       scw 	switch (PCI_PRODUCT(isc->vga_pa.pa_id)) {
    158  1.14       scw 	case PCI_PRODUCT_INTEL_82810_GC:
    159  1.14       scw 	case PCI_PRODUCT_INTEL_82810_DC100_GC:
    160  1.14       scw 	case PCI_PRODUCT_INTEL_82810E_GC:
    161  1.14       scw 	case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
    162  1.14       scw 		isc->chiptype = CHIP_I810;
    163  1.14       scw 		break;
    164  1.14       scw 	case PCI_PRODUCT_INTEL_82830MP_IV:
    165  1.14       scw 	case PCI_PRODUCT_INTEL_82845G_IGD:
    166  1.14       scw 		isc->chiptype = CHIP_I830;
    167  1.14       scw 		break;
    168  1.17   hannken 	case PCI_PRODUCT_INTEL_82855GM_IGD:
    169  1.18      tron 	case PCI_PRODUCT_INTEL_82865_IGD:
    170  1.17   hannken 		isc->chiptype = CHIP_I855;
    171  1.17   hannken 		break;
    172  1.14       scw 	}
    173  1.14       scw 
    174   1.1      fvdl 	error = pci_mapreg_map(&isc->vga_pa, AGP_I810_MMADR,
    175   1.1      fvdl 	    PCI_MAPREG_TYPE_MEM, 0, &isc->bst, &isc->bsh, NULL, NULL);
    176   1.1      fvdl 	if (error != 0) {
    177  1.15   thorpej 		aprint_error(": can't map mmadr registers\n");
    178   1.1      fvdl 		return error;
    179   1.1      fvdl 	}
    180   1.1      fvdl 
    181   1.1      fvdl 	isc->initial_aperture = AGP_GET_APERTURE(sc);
    182   1.1      fvdl 
    183  1.14       scw 	gatt = malloc(sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
    184  1.14       scw 	if (!gatt) {
    185  1.14       scw  		agp_generic_detach(sc);
    186  1.14       scw  		return ENOMEM;
    187  1.14       scw 	}
    188  1.14       scw 	isc->gatt = gatt;
    189  1.14       scw 
    190  1.14       scw 	gatt->ag_entries = AGP_GET_APERTURE(sc) >> AGP_PAGE_SHIFT;
    191   1.1      fvdl 
    192  1.14       scw 	if (isc->chiptype == CHIP_I810) {
    193  1.14       scw 		int dummyseg;
    194  1.14       scw 		/* Some i810s have on-chip memory called dcache */
    195  1.14       scw 		if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
    196  1.14       scw 			isc->dcache_size = 4 * 1024 * 1024;
    197  1.14       scw 		else
    198  1.14       scw 			isc->dcache_size = 0;
    199  1.14       scw 
    200  1.14       scw 		/* According to the specs the gatt on the i810 must be 64k */
    201  1.14       scw 		if (agp_alloc_dmamem(sc->as_dmat, 64 * 1024,
    202  1.14       scw 		    0, &gatt->ag_dmamap, (caddr_t *)&gatt->ag_virtual,
    203  1.14       scw 		    &gatt->ag_physical, &gatt->ag_dmaseg, 1, &dummyseg) != 0) {
    204  1.14       scw 			free(gatt, M_AGP);
    205   1.1      fvdl 			agp_generic_detach(sc);
    206   1.1      fvdl 			return ENOMEM;
    207   1.1      fvdl 		}
    208  1.14       scw 
    209  1.14       scw 		gatt->ag_size = gatt->ag_entries * sizeof(u_int32_t);
    210  1.14       scw 		memset(gatt->ag_virtual, 0, gatt->ag_size);
    211  1.14       scw 
    212  1.14       scw 		agp_flush_cache();
    213  1.14       scw 		/* Install the GATT. */
    214  1.14       scw 		WRITE4(AGP_I810_PGTBL_CTL, gatt->ag_physical | 1);
    215  1.17   hannken 	} else if (isc->chiptype == CHIP_I830) {
    216  1.14       scw 		/* The i830 automatically initializes the 128k gatt on boot. */
    217  1.14       scw 		pcireg_t reg;
    218  1.14       scw 		u_int32_t pgtblctl;
    219  1.14       scw 		u_int16_t gcc1;
    220  1.14       scw 
    221  1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
    222  1.14       scw 		gcc1 = (u_int16_t)(reg >> 16);
    223  1.14       scw 		switch (gcc1 & AGP_I830_GCC1_GMS) {
    224  1.14       scw 		case AGP_I830_GCC1_GMS_STOLEN_512:
    225  1.14       scw 			isc->stolen = (512 - 132) * 1024 / 4096;
    226  1.14       scw 			break;
    227  1.14       scw 		case AGP_I830_GCC1_GMS_STOLEN_1024:
    228  1.14       scw 			isc->stolen = (1024 - 132) * 1024 / 4096;
    229  1.14       scw 			break;
    230  1.14       scw 		case AGP_I830_GCC1_GMS_STOLEN_8192:
    231  1.14       scw 			isc->stolen = (8192 - 132) * 1024 / 4096;
    232  1.14       scw 			break;
    233  1.14       scw 		default:
    234  1.14       scw 			isc->stolen = 0;
    235  1.15   thorpej 			aprint_error(
    236  1.15   thorpej 			    ": unknown memory configuration, disabling\n");
    237  1.14       scw 			agp_generic_detach(sc);
    238  1.14       scw 			return EINVAL;
    239  1.14       scw 		}
    240  1.14       scw 		if (isc->stolen > 0) {
    241  1.17   hannken 			aprint_error(": detected %dk stolen memory\n%s",
    242  1.17   hannken 			    isc->stolen * 4, sc->as_dev.dv_xname);
    243  1.14       scw 		}
    244  1.17   hannken 		aprint_error(": aperture size is %dM\n%s",
    245  1.17   hannken 		       isc->initial_aperture / 1024 / 1024,
    246  1.17   hannken 		       sc->as_dev.dv_xname);
    247  1.17   hannken 
    248  1.17   hannken 		/* GATT address is already in there, make sure it's enabled */
    249  1.17   hannken 		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
    250  1.17   hannken 		pgtblctl |= 1;
    251  1.17   hannken 		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
    252  1.17   hannken 
    253  1.17   hannken 		gatt->ag_physical = pgtblctl & ~1;
    254  1.17   hannken 	} else {	/* CHIP_I855 */
    255  1.17   hannken 		/* The 855GM automatically initializes the 128k gatt on boot. */
    256  1.17   hannken 		pcireg_t reg;
    257  1.17   hannken 		u_int32_t pgtblctl;
    258  1.17   hannken 		u_int16_t gcc1;
    259  1.17   hannken 
    260  1.17   hannken 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I855_GCC1);
    261  1.17   hannken 		gcc1 = (u_int16_t)(reg >> 16);
    262  1.17   hannken 		switch (gcc1 & AGP_I855_GCC1_GMS) {
    263  1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_1M:
    264  1.17   hannken 			isc->stolen = (1024 - 132) * 1024 / 4096;
    265  1.17   hannken 			break;
    266  1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_4M:
    267  1.17   hannken 			isc->stolen = (4096 - 132) * 1024 / 4096;
    268  1.17   hannken 			break;
    269  1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_8M:
    270  1.17   hannken 			isc->stolen = (8192 - 132) * 1024 / 4096;
    271  1.17   hannken 			break;
    272  1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_16M:
    273  1.17   hannken 			isc->stolen = (16384 - 132) * 1024 / 4096;
    274  1.17   hannken 			break;
    275  1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_32M:
    276  1.17   hannken 			isc->stolen = (32768 - 132) * 1024 / 4096;
    277  1.17   hannken 			break;
    278  1.17   hannken 		default:
    279  1.17   hannken 			isc->stolen = 0;
    280  1.17   hannken 			aprint_error(
    281  1.17   hannken 			    ": unknown memory configuration, disabling\n");
    282  1.17   hannken 			agp_generic_detach(sc);
    283  1.17   hannken 			return EINVAL;
    284  1.17   hannken 		}
    285  1.17   hannken 		if (isc->stolen > 0) {
    286  1.17   hannken 			aprint_error(": detected %dk stolen memory\n%s",
    287  1.17   hannken 			    isc->stolen * 4, sc->as_dev.dv_xname);
    288  1.17   hannken 		}
    289  1.17   hannken 		aprint_error(": aperture size is %dM\n%s",
    290  1.17   hannken 			isc->initial_aperture / 1024 / 1024,
    291  1.17   hannken 			sc->as_dev.dv_xname);
    292  1.14       scw 
    293  1.14       scw 		/* GATT address is already in there, make sure it's enabled */
    294  1.14       scw 		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
    295  1.14       scw 		pgtblctl |= 1;
    296  1.14       scw 		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
    297  1.14       scw 
    298  1.14       scw 		gatt->ag_physical = pgtblctl & ~1;
    299   1.1      fvdl 	}
    300   1.1      fvdl 
    301   1.1      fvdl 	/*
    302   1.1      fvdl 	 * Make sure the chipset can see everything.
    303   1.1      fvdl 	 */
    304   1.1      fvdl 	agp_flush_cache();
    305  1.14       scw 
    306   1.1      fvdl 	return 0;
    307   1.1      fvdl }
    308   1.1      fvdl 
    309   1.1      fvdl #if 0
    310   1.1      fvdl static int
    311   1.1      fvdl agp_i810_detach(struct agp_softc *sc)
    312   1.1      fvdl {
    313   1.1      fvdl 	int error;
    314   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    315   1.1      fvdl 
    316   1.1      fvdl 	error = agp_generic_detach(sc);
    317   1.1      fvdl 	if (error)
    318   1.1      fvdl 		return error;
    319   1.1      fvdl 
    320   1.1      fvdl 	/* Clear the GATT base. */
    321  1.14       scw 	if (sc->chiptype == CHIP_I810) {
    322  1.14       scw 		WRITE4(AGP_I810_PGTBL_CTL, 0);
    323  1.14       scw 	} else {
    324  1.14       scw 		unsigned int pgtblctl;
    325  1.14       scw 		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
    326  1.14       scw 		pgtblctl &= ~1;
    327  1.14       scw 		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
    328  1.14       scw 	}
    329   1.1      fvdl 
    330   1.1      fvdl 	/* Put the aperture back the way it started. */
    331   1.1      fvdl 	AGP_SET_APERTURE(sc, isc->initial_aperture);
    332   1.1      fvdl 
    333  1.14       scw 	if (sc->chiptype == CHIP_I810) {
    334  1.14       scw 		agp_free_dmamem(sc->as_dmat, gatt->ag_size, gatt->ag_dmamap,
    335  1.14       scw 		    (caddr_t)gatt->ag_virtual, &gatt->ag_dmaseg, 1);
    336  1.14       scw 	}
    337  1.14       scw 	free(sc->gatt, M_AGP);
    338   1.1      fvdl 
    339   1.1      fvdl 	return 0;
    340   1.1      fvdl }
    341   1.1      fvdl #endif
    342   1.1      fvdl 
    343   1.1      fvdl static u_int32_t
    344   1.1      fvdl agp_i810_get_aperture(struct agp_softc *sc)
    345   1.1      fvdl {
    346  1.14       scw 	struct agp_i810_softc *isc = sc->as_chipc;
    347  1.14       scw 	pcireg_t reg;
    348  1.14       scw 
    349  1.14       scw 	if (isc->chiptype == CHIP_I810) {
    350  1.14       scw 		u_int16_t miscc;
    351   1.1      fvdl 
    352  1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
    353  1.14       scw 		miscc = (u_int16_t)(reg >> 16);
    354  1.14       scw 		if ((miscc & AGP_I810_MISCC_WINSIZE) ==
    355  1.14       scw 		    AGP_I810_MISCC_WINSIZE_32)
    356  1.14       scw 			return 32 * 1024 * 1024;
    357  1.14       scw 		else
    358  1.14       scw 			return 64 * 1024 * 1024;
    359  1.17   hannken 	} else if (isc->chiptype == CHIP_I830) {
    360  1.14       scw 		u_int16_t gcc1;
    361  1.14       scw 
    362  1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
    363  1.14       scw 		gcc1 = (u_int16_t)(reg >> 16);
    364  1.14       scw 		if ((gcc1 & AGP_I830_GCC1_GMASIZE) == AGP_I830_GCC1_GMASIZE_64)
    365  1.14       scw 			return 64 * 1024 * 1024;
    366  1.14       scw 		else
    367  1.14       scw 			return 128 * 1024 * 1024;
    368  1.17   hannken 	} else {	/* CHIP_I855 */
    369  1.17   hannken 		return 128 * 1024 * 1024;
    370  1.14       scw 	}
    371   1.1      fvdl }
    372   1.1      fvdl 
    373   1.1      fvdl static int
    374   1.1      fvdl agp_i810_set_aperture(struct agp_softc *sc, u_int32_t aperture)
    375   1.1      fvdl {
    376  1.14       scw 	struct agp_i810_softc *isc = sc->as_chipc;
    377  1.14       scw 	pcireg_t reg;
    378  1.14       scw 
    379  1.14       scw 	if (isc->chiptype == CHIP_I810) {
    380  1.14       scw 		u_int16_t miscc;
    381  1.14       scw 
    382  1.14       scw 		/*
    383  1.14       scw 		 * Double check for sanity.
    384  1.14       scw 		 */
    385  1.14       scw 		if (aperture != (32 * 1024 * 1024) &&
    386  1.14       scw 		    aperture != (64 * 1024 * 1024)) {
    387  1.14       scw 			printf("%s: bad aperture size %d\n",
    388  1.14       scw 			    sc->as_dev.dv_xname, aperture);
    389  1.14       scw 			return EINVAL;
    390  1.14       scw 		}
    391   1.1      fvdl 
    392  1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
    393  1.14       scw 		miscc = (u_int16_t)(reg >> 16);
    394  1.14       scw 		miscc &= ~AGP_I810_MISCC_WINSIZE;
    395  1.14       scw 		if (aperture == 32 * 1024 * 1024)
    396  1.14       scw 			miscc |= AGP_I810_MISCC_WINSIZE_32;
    397  1.14       scw 		else
    398  1.14       scw 			miscc |= AGP_I810_MISCC_WINSIZE_64;
    399  1.14       scw 
    400  1.14       scw 		reg &= 0x0000ffff;
    401  1.14       scw 		reg |= ((pcireg_t)miscc) << 16;
    402  1.14       scw 		pci_conf_write(sc->as_pc, sc->as_tag, AGP_I810_SMRAM, reg);
    403  1.17   hannken 	} if (isc->chiptype == CHIP_I830) {
    404  1.14       scw 		u_int16_t gcc1;
    405  1.14       scw 
    406  1.14       scw 		if (aperture != (64 * 1024 * 1024) &&
    407  1.14       scw 		    aperture != (128 * 1024 * 1024)) {
    408  1.14       scw 			printf("%s: bad aperture size %d\n",
    409  1.14       scw 			    sc->as_dev.dv_xname, aperture);
    410  1.14       scw 			return EINVAL;
    411  1.14       scw 		}
    412  1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
    413  1.14       scw 		gcc1 = (u_int16_t)(reg >> 16);
    414  1.14       scw 		gcc1 &= ~AGP_I830_GCC1_GMASIZE;
    415  1.14       scw 		if (aperture == 64 * 1024 * 1024)
    416  1.14       scw 			gcc1 |= AGP_I830_GCC1_GMASIZE_64;
    417  1.14       scw 		else
    418  1.14       scw 			gcc1 |= AGP_I830_GCC1_GMASIZE_128;
    419  1.14       scw 
    420  1.14       scw 		reg &= 0x0000ffff;
    421  1.14       scw 		reg |= ((pcireg_t)gcc1) << 16;
    422  1.14       scw 		pci_conf_write(sc->as_pc, sc->as_tag, AGP_I830_GCC0, reg);
    423  1.17   hannken 	} else {	/* CHIP_I855 */
    424  1.17   hannken 		if (aperture != 128 * 1024 * 1024) {
    425  1.17   hannken 			printf("%s: bad aperture size %d\n",
    426  1.17   hannken 			    sc->as_dev.dv_xname, aperture);
    427  1.17   hannken 			return EINVAL;
    428  1.17   hannken 		}
    429   1.1      fvdl 	}
    430   1.1      fvdl 
    431   1.1      fvdl 	return 0;
    432   1.1      fvdl }
    433   1.1      fvdl 
    434   1.1      fvdl static int
    435   1.1      fvdl agp_i810_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
    436   1.1      fvdl {
    437   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    438   1.1      fvdl 
    439  1.14       scw 	if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
    440  1.14       scw #ifdef DEBUG
    441  1.14       scw 		printf("%s: failed: offset 0x%08x, shift %d, entries %d\n",
    442  1.14       scw 		    sc->as_dev.dv_xname, (int)offset, AGP_PAGE_SHIFT,
    443  1.14       scw 		    isc->gatt->ag_entries);
    444  1.14       scw #endif
    445   1.1      fvdl 		return EINVAL;
    446  1.14       scw 	}
    447  1.14       scw 
    448  1.17   hannken 	if (isc->chiptype != CHIP_I830) {
    449  1.14       scw 		if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
    450  1.14       scw #ifdef DEBUG
    451  1.14       scw 			printf("%s: trying to bind into stolen memory",
    452  1.14       scw 			    sc->as_dev.dv_xname);
    453  1.14       scw #endif
    454  1.14       scw 			return EINVAL;
    455  1.14       scw 		}
    456  1.14       scw 	}
    457   1.1      fvdl 
    458   1.1      fvdl 	WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
    459   1.1      fvdl 	    physical | 1);
    460   1.1      fvdl 	return 0;
    461   1.1      fvdl }
    462   1.1      fvdl 
    463   1.1      fvdl static int
    464   1.1      fvdl agp_i810_unbind_page(struct agp_softc *sc, off_t offset)
    465   1.1      fvdl {
    466   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    467   1.1      fvdl 
    468   1.1      fvdl 	if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
    469   1.1      fvdl 		return EINVAL;
    470   1.1      fvdl 
    471  1.17   hannken 	if (isc->chiptype != CHIP_I810 ) {
    472  1.14       scw 		if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
    473  1.14       scw #ifdef DEBUG
    474  1.14       scw 			printf("%s: trying to unbind from stolen memory",
    475  1.14       scw 			    sc->as_dev.dv_xname);
    476  1.14       scw #endif
    477  1.14       scw 			return EINVAL;
    478  1.14       scw 		}
    479  1.14       scw 	}
    480  1.14       scw 
    481   1.1      fvdl 	WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4, 0);
    482   1.1      fvdl 	return 0;
    483   1.1      fvdl }
    484   1.1      fvdl 
    485   1.1      fvdl /*
    486   1.1      fvdl  * Writing via memory mapped registers already flushes all TLBs.
    487   1.1      fvdl  */
    488   1.1      fvdl static void
    489   1.1      fvdl agp_i810_flush_tlb(struct agp_softc *sc)
    490   1.1      fvdl {
    491   1.1      fvdl }
    492   1.1      fvdl 
    493   1.1      fvdl static int
    494   1.1      fvdl agp_i810_enable(struct agp_softc *sc, u_int32_t mode)
    495   1.1      fvdl {
    496   1.1      fvdl 
    497   1.1      fvdl 	return 0;
    498   1.1      fvdl }
    499   1.1      fvdl 
    500   1.1      fvdl static struct agp_memory *
    501   1.1      fvdl agp_i810_alloc_memory(struct agp_softc *sc, int type, vsize_t size)
    502   1.1      fvdl {
    503   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    504   1.1      fvdl 	struct agp_memory *mem;
    505   1.1      fvdl 
    506   1.1      fvdl 	if ((size & (AGP_PAGE_SIZE - 1)) != 0)
    507   1.1      fvdl 		return 0;
    508   1.1      fvdl 
    509   1.1      fvdl 	if (sc->as_allocated + size > sc->as_maxmem)
    510   1.1      fvdl 		return 0;
    511   1.1      fvdl 
    512   1.1      fvdl 	if (type == 1) {
    513   1.1      fvdl 		/*
    514   1.1      fvdl 		 * Mapping local DRAM into GATT.
    515   1.1      fvdl 		 */
    516  1.17   hannken 		if (isc->chiptype != CHIP_I810 )
    517  1.14       scw 			return 0;
    518   1.1      fvdl 		if (size != isc->dcache_size)
    519   1.1      fvdl 			return 0;
    520   1.1      fvdl 	} else if (type == 2) {
    521   1.1      fvdl 		/*
    522   1.1      fvdl 		 * Bogus mapping of a single page for the hardware cursor.
    523   1.1      fvdl 		 */
    524   1.1      fvdl 		if (size != AGP_PAGE_SIZE)
    525   1.1      fvdl 			return 0;
    526   1.1      fvdl 	}
    527   1.1      fvdl 
    528  1.10   tsutsui 	mem = malloc(sizeof *mem, M_AGP, M_WAITOK|M_ZERO);
    529   1.1      fvdl 	if (mem == NULL)
    530   1.1      fvdl 		return NULL;
    531   1.1      fvdl 	mem->am_id = sc->as_nextid++;
    532   1.1      fvdl 	mem->am_size = size;
    533   1.1      fvdl 	mem->am_type = type;
    534   1.1      fvdl 
    535   1.1      fvdl 	if (type == 2) {
    536   1.1      fvdl 		/*
    537   1.1      fvdl 		 * Allocate and wire down the page now so that we can
    538   1.1      fvdl 		 * get its physical address.
    539   1.1      fvdl 		 */
    540   1.1      fvdl 		mem->am_dmaseg = malloc(sizeof *mem->am_dmaseg, M_AGP,
    541   1.1      fvdl 		    M_WAITOK);
    542   1.1      fvdl 		if (mem->am_dmaseg == NULL) {
    543   1.1      fvdl 			free(mem, M_AGP);
    544   1.1      fvdl 			return NULL;
    545   1.1      fvdl 		}
    546   1.1      fvdl 		if (agp_alloc_dmamem(sc->as_dmat, size, 0,
    547   1.1      fvdl 		    &mem->am_dmamap, &mem->am_virtual, &mem->am_physical,
    548   1.1      fvdl 		    mem->am_dmaseg, 1, &mem->am_nseg) != 0) {
    549   1.1      fvdl 			free(mem->am_dmaseg, M_AGP);
    550   1.1      fvdl 			free(mem, M_AGP);
    551   1.1      fvdl 			return NULL;
    552   1.1      fvdl 		}
    553   1.1      fvdl 	} else if (type != 1) {
    554   1.4  drochner 		if (bus_dmamap_create(sc->as_dmat, size, size / PAGE_SIZE + 1,
    555   1.4  drochner 				      size, 0, BUS_DMA_NOWAIT,
    556   1.4  drochner 				      &mem->am_dmamap) != 0) {
    557   1.1      fvdl 			free(mem, M_AGP);
    558   1.1      fvdl 			return NULL;
    559   1.1      fvdl 		}
    560   1.1      fvdl 	}
    561   1.1      fvdl 
    562   1.1      fvdl 	TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
    563   1.1      fvdl 	sc->as_allocated += size;
    564   1.1      fvdl 
    565   1.1      fvdl 	return mem;
    566   1.1      fvdl }
    567   1.1      fvdl 
    568   1.1      fvdl static int
    569   1.1      fvdl agp_i810_free_memory(struct agp_softc *sc, struct agp_memory *mem)
    570   1.1      fvdl {
    571   1.1      fvdl 	if (mem->am_is_bound)
    572   1.1      fvdl 		return EBUSY;
    573   1.1      fvdl 
    574   1.1      fvdl 	if (mem->am_type == 2) {
    575   1.1      fvdl 		agp_free_dmamem(sc->as_dmat, mem->am_size, mem->am_dmamap,
    576   1.1      fvdl 		    mem->am_virtual, mem->am_dmaseg, mem->am_nseg);
    577   1.1      fvdl 		free(mem->am_dmaseg, M_AGP);
    578   1.1      fvdl 	}
    579   1.1      fvdl 
    580   1.1      fvdl 	sc->as_allocated -= mem->am_size;
    581   1.1      fvdl 	TAILQ_REMOVE(&sc->as_memory, mem, am_link);
    582   1.1      fvdl 	free(mem, M_AGP);
    583   1.1      fvdl 	return 0;
    584   1.1      fvdl }
    585   1.1      fvdl 
    586   1.1      fvdl static int
    587   1.1      fvdl agp_i810_bind_memory(struct agp_softc *sc, struct agp_memory *mem,
    588   1.1      fvdl 		     off_t offset)
    589   1.1      fvdl {
    590   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    591   1.4  drochner 	u_int32_t regval, i;
    592   1.4  drochner 
    593   1.4  drochner 	/*
    594   1.4  drochner 	 * XXX evil hack: the PGTBL_CTL appearently gets overwritten by the
    595   1.4  drochner 	 * X server for mysterious reasons which leads to crashes if we write
    596   1.4  drochner 	 * to the GTT through the MMIO window.
    597   1.4  drochner 	 * Until the issue is solved, simply restore it.
    598   1.4  drochner 	 */
    599   1.4  drochner 	regval = bus_space_read_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL);
    600   1.4  drochner 	if (regval != (isc->gatt->ag_physical | 1)) {
    601   1.4  drochner 		printf("agp_i810_bind_memory: PGTBL_CTL is 0x%x - fixing\n",
    602   1.4  drochner 		       regval);
    603   1.4  drochner 		bus_space_write_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL,
    604   1.4  drochner 				  isc->gatt->ag_physical | 1);
    605   1.4  drochner 	}
    606   1.1      fvdl 
    607   1.5  drochner 	if (mem->am_type == 2) {
    608   1.5  drochner 		WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
    609   1.5  drochner 		       mem->am_physical | 1);
    610   1.5  drochner 		mem->am_offset = offset;
    611   1.5  drochner 		mem->am_is_bound = 1;
    612   1.1      fvdl 		return 0;
    613   1.5  drochner 	}
    614   1.5  drochner 
    615   1.1      fvdl 	if (mem->am_type != 1)
    616   1.1      fvdl 		return agp_generic_bind_memory(sc, mem, offset);
    617   1.1      fvdl 
    618  1.17   hannken 	if (isc->chiptype != CHIP_I810)
    619  1.14       scw 		return EINVAL;
    620  1.14       scw 
    621   1.1      fvdl 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
    622   1.1      fvdl 		WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
    623   1.1      fvdl 		       i | 3);
    624   1.1      fvdl 	}
    625  1.13  drochner 	mem->am_is_bound = 1;
    626   1.1      fvdl 	return 0;
    627   1.1      fvdl }
    628   1.1      fvdl 
    629   1.1      fvdl static int
    630   1.1      fvdl agp_i810_unbind_memory(struct agp_softc *sc, struct agp_memory *mem)
    631   1.1      fvdl {
    632   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    633   1.1      fvdl 	u_int32_t i;
    634   1.1      fvdl 
    635   1.5  drochner 	if (mem->am_type == 2) {
    636   1.5  drochner 		WRITE4(AGP_I810_GTT +
    637   1.5  drochner 		       (u_int32_t)(mem->am_offset >> AGP_PAGE_SHIFT) * 4,
    638   1.5  drochner 		       0);
    639   1.5  drochner 		mem->am_offset = 0;
    640   1.5  drochner 		mem->am_is_bound = 0;
    641   1.1      fvdl 		return 0;
    642   1.5  drochner 	}
    643   1.1      fvdl 
    644   1.1      fvdl 	if (mem->am_type != 1)
    645   1.1      fvdl 		return agp_generic_unbind_memory(sc, mem);
    646  1.14       scw 
    647  1.17   hannken 	if (isc->chiptype != CHIP_I810)
    648  1.14       scw 		return EINVAL;
    649   1.1      fvdl 
    650   1.1      fvdl 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
    651   1.1      fvdl 		WRITE4(AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0);
    652  1.13  drochner 	mem->am_is_bound = 0;
    653   1.1      fvdl 	return 0;
    654   1.1      fvdl }
    655