Home | History | Annotate | Line # | Download | only in pci
agp_i810.c revision 1.73
      1  1.73    dyoung /*	$NetBSD: agp_i810.c,v 1.73 2011/04/04 20:37:56 dyoung 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.73    dyoung __KERNEL_RCSID(0, "$NetBSD: agp_i810.c,v 1.73 2011/04/04 20:37:56 dyoung 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/proc.h>
     40   1.1      fvdl #include <sys/device.h>
     41   1.1      fvdl #include <sys/conf.h>
     42   1.1      fvdl 
     43   1.1      fvdl #include <dev/pci/pcivar.h>
     44   1.1      fvdl #include <dev/pci/pcireg.h>
     45   1.1      fvdl #include <dev/pci/pcidevs.h>
     46   1.1      fvdl #include <dev/pci/agpvar.h>
     47   1.1      fvdl #include <dev/pci/agpreg.h>
     48   1.1      fvdl 
     49   1.1      fvdl #include <sys/agpio.h>
     50   1.1      fvdl 
     51  1.43        ad #include <sys/bus.h>
     52   1.1      fvdl 
     53  1.20      tron #include "agp_intel.h"
     54  1.20      tron 
     55   1.1      fvdl #define READ1(off)	bus_space_read_1(isc->bst, isc->bsh, off)
     56  1.14       scw #define READ4(off)	bus_space_read_4(isc->bst, isc->bsh, off)
     57   1.1      fvdl #define WRITE4(off,v)	bus_space_write_4(isc->bst, isc->bsh, off, v)
     58   1.1      fvdl 
     59  1.14       scw #define CHIP_I810 0	/* i810/i815 */
     60  1.17   hannken #define CHIP_I830 1	/* 830M/845G */
     61  1.17   hannken #define CHIP_I855 2	/* 852GM/855GM/865G */
     62  1.56       tnn #define CHIP_I915 3	/* 915G/915GM/945G/945GM/945GME */
     63  1.45     joerg #define CHIP_I965 4	/* 965Q/965PM */
     64  1.45     joerg #define CHIP_G33  5	/* G33/Q33/Q35 */
     65  1.58  christos #define CHIP_G4X  6	/* G45/Q45 */
     66  1.14       scw 
     67   1.1      fvdl struct agp_i810_softc {
     68   1.1      fvdl 	u_int32_t initial_aperture;	/* aperture size at startup */
     69   1.1      fvdl 	struct agp_gatt *gatt;
     70  1.14       scw 	int chiptype;			/* i810-like or i830 */
     71  1.14       scw 	u_int32_t dcache_size;		/* i810 only */
     72  1.14       scw 	u_int32_t stolen;		/* number of i830/845 gtt entries
     73  1.14       scw 					   for stolen memory */
     74  1.28  christos 	bus_space_tag_t bst;		/* register bus_space tag */
     75  1.28  christos 	bus_space_handle_t bsh;		/* register bus_space handle */
     76  1.28  christos 	bus_space_tag_t gtt_bst;	/* GTT bus_space tag */
     77  1.28  christos 	bus_space_handle_t gtt_bsh;	/* GTT bus_space handle */
     78   1.1      fvdl 	struct pci_attach_args vga_pa;
     79  1.24  jmcneill 
     80  1.47  jmcneill 	u_int32_t pgtblctl;
     81   1.1      fvdl };
     82   1.1      fvdl 
     83  1.49  drochner /* XXX hack, see below */
     84  1.50  drochner static bus_addr_t agp_i810_vga_regbase;
     85  1.50  drochner static bus_space_handle_t agp_i810_vga_bsh;
     86  1.49  drochner 
     87   1.1      fvdl static u_int32_t agp_i810_get_aperture(struct agp_softc *);
     88   1.1      fvdl static int agp_i810_set_aperture(struct agp_softc *, u_int32_t);
     89   1.1      fvdl static int agp_i810_bind_page(struct agp_softc *, off_t, bus_addr_t);
     90   1.1      fvdl static int agp_i810_unbind_page(struct agp_softc *, off_t);
     91   1.1      fvdl static void agp_i810_flush_tlb(struct agp_softc *);
     92   1.1      fvdl static int agp_i810_enable(struct agp_softc *, u_int32_t mode);
     93   1.1      fvdl static struct agp_memory *agp_i810_alloc_memory(struct agp_softc *, int,
     94   1.1      fvdl 						vsize_t);
     95   1.1      fvdl static int agp_i810_free_memory(struct agp_softc *, struct agp_memory *);
     96   1.1      fvdl static int agp_i810_bind_memory(struct agp_softc *, struct agp_memory *, off_t);
     97   1.1      fvdl static int agp_i810_unbind_memory(struct agp_softc *, struct agp_memory *);
     98  1.47  jmcneill 
     99  1.66    dyoung static bool agp_i810_resume(device_t, const pmf_qual_t *);
    100  1.47  jmcneill static int agp_i810_init(struct agp_softc *);
    101   1.1      fvdl 
    102  1.45     joerg static int agp_i810_init(struct agp_softc *);
    103  1.71    gsutre static int agp_i810_write_gtt_entry(struct agp_i810_softc *, off_t,
    104  1.71    gsutre 				    bus_addr_t);
    105  1.45     joerg 
    106  1.26   thorpej static struct agp_methods agp_i810_methods = {
    107   1.1      fvdl 	agp_i810_get_aperture,
    108   1.1      fvdl 	agp_i810_set_aperture,
    109   1.1      fvdl 	agp_i810_bind_page,
    110   1.1      fvdl 	agp_i810_unbind_page,
    111   1.1      fvdl 	agp_i810_flush_tlb,
    112   1.1      fvdl 	agp_i810_enable,
    113   1.1      fvdl 	agp_i810_alloc_memory,
    114   1.1      fvdl 	agp_i810_free_memory,
    115   1.1      fvdl 	agp_i810_bind_memory,
    116   1.1      fvdl 	agp_i810_unbind_memory,
    117   1.1      fvdl };
    118   1.1      fvdl 
    119  1.71    gsutre static int
    120  1.71    gsutre agp_i810_write_gtt_entry(struct agp_i810_softc *isc, off_t off, bus_addr_t v)
    121  1.58  christos {
    122  1.71    gsutre 	u_int32_t pte;
    123  1.71    gsutre 	bus_size_t base_off, wroff;
    124  1.71    gsutre 
    125  1.71    gsutre 	/* Bits 11:4 (physical start address extension) should be zero. */
    126  1.71    gsutre 	if ((v & 0xff0) != 0)
    127  1.71    gsutre 		return EINVAL;
    128  1.71    gsutre 
    129  1.71    gsutre 	pte = (u_int32_t)v;
    130  1.71    gsutre 	/*
    131  1.71    gsutre 	 * We need to massage the pte if bus_addr_t is wider than 32 bits.
    132  1.71    gsutre 	 * The compiler isn't smart enough, hence the casts to uintmax_t.
    133  1.71    gsutre 	 */
    134  1.71    gsutre 	if (sizeof(bus_addr_t) > sizeof(u_int32_t)) {
    135  1.71    gsutre 		/* 965+ can do 36-bit addressing, add in the extra bits. */
    136  1.71    gsutre 		if (isc->chiptype == CHIP_I965 ||
    137  1.71    gsutre 		    isc->chiptype == CHIP_G33 ||
    138  1.71    gsutre 		    isc->chiptype == CHIP_G4X) {
    139  1.71    gsutre 			if (((uintmax_t)v >> 36) != 0)
    140  1.71    gsutre 				return EINVAL;
    141  1.71    gsutre 			pte |= (v >> 28) & 0xf0;
    142  1.71    gsutre 		} else {
    143  1.71    gsutre 			if (((uintmax_t)v >> 32) != 0)
    144  1.71    gsutre 				return EINVAL;
    145  1.71    gsutre 		}
    146  1.71    gsutre 	}
    147  1.58  christos 
    148  1.58  christos 	base_off = 0;
    149  1.71    gsutre 	wroff = (off >> AGP_PAGE_SHIFT) * 4;
    150  1.58  christos 
    151  1.58  christos 	switch (isc->chiptype) {
    152  1.58  christos 	case CHIP_I810:
    153  1.58  christos 	case CHIP_I830:
    154  1.58  christos 	case CHIP_I855:
    155  1.58  christos 		base_off = AGP_I810_GTT;
    156  1.58  christos 		break;
    157  1.58  christos 	case CHIP_I965:
    158  1.58  christos 		base_off = AGP_I965_GTT;
    159  1.58  christos 		break;
    160  1.58  christos 	case CHIP_G4X:
    161  1.58  christos 		base_off = AGP_G4X_GTT;
    162  1.58  christos 		break;
    163  1.58  christos 	case CHIP_I915:
    164  1.58  christos 	case CHIP_G33:
    165  1.71    gsutre 		bus_space_write_4(isc->gtt_bst, isc->gtt_bsh, wroff, pte);
    166  1.71    gsutre 		return 0;
    167  1.58  christos 	}
    168  1.71    gsutre 
    169  1.71    gsutre 	WRITE4(base_off + wroff, pte);
    170  1.71    gsutre 	return 0;
    171  1.58  christos }
    172  1.58  christos 
    173  1.55  matthias /* XXXthorpej -- duplicated code (see arch/x86/pci/pchb.c) */
    174   1.1      fvdl static int
    175  1.73    dyoung agp_i810_vgamatch(const struct pci_attach_args *pa)
    176   1.1      fvdl {
    177   1.6   thorpej 
    178   1.2      fvdl 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
    179   1.2      fvdl 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
    180   1.6   thorpej 		return (0);
    181   1.6   thorpej 
    182   1.1      fvdl 	switch (PCI_PRODUCT(pa->pa_id)) {
    183   1.1      fvdl 	case PCI_PRODUCT_INTEL_82810_GC:
    184   1.1      fvdl 	case PCI_PRODUCT_INTEL_82810_DC100_GC:
    185   1.1      fvdl 	case PCI_PRODUCT_INTEL_82810E_GC:
    186   1.1      fvdl 	case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
    187  1.14       scw 	case PCI_PRODUCT_INTEL_82830MP_IV:
    188  1.14       scw 	case PCI_PRODUCT_INTEL_82845G_IGD:
    189  1.17   hannken 	case PCI_PRODUCT_INTEL_82855GM_IGD:
    190  1.18      tron 	case PCI_PRODUCT_INTEL_82865_IGD:
    191  1.28  christos 	case PCI_PRODUCT_INTEL_82915G_IGD:
    192  1.28  christos 	case PCI_PRODUCT_INTEL_82915GM_IGD:
    193  1.32    simonb 	case PCI_PRODUCT_INTEL_82945P_IGD:
    194  1.32    simonb 	case PCI_PRODUCT_INTEL_82945GM_IGD:
    195  1.32    simonb 	case PCI_PRODUCT_INTEL_82945GM_IGD_1:
    196  1.56       tnn 	case PCI_PRODUCT_INTEL_82945GME_IGD:
    197  1.68       riz 	case PCI_PRODUCT_INTEL_E7221_IGD:
    198  1.42     markd 	case PCI_PRODUCT_INTEL_82965Q_IGD:
    199  1.42     markd 	case PCI_PRODUCT_INTEL_82965Q_IGD_1:
    200  1.45     joerg 	case PCI_PRODUCT_INTEL_82965PM_IGD:
    201  1.45     joerg 	case PCI_PRODUCT_INTEL_82965PM_IGD_1:
    202  1.45     joerg 	case PCI_PRODUCT_INTEL_82G33_IGD:
    203  1.45     joerg 	case PCI_PRODUCT_INTEL_82G33_IGD_1:
    204  1.44   jnemeth 	case PCI_PRODUCT_INTEL_82965G_IGD:
    205  1.44   jnemeth 	case PCI_PRODUCT_INTEL_82965G_IGD_1:
    206  1.68       riz 	case PCI_PRODUCT_INTEL_82965GME_IGD:
    207  1.46     markd 	case PCI_PRODUCT_INTEL_82Q35_IGD:
    208  1.46     markd 	case PCI_PRODUCT_INTEL_82Q35_IGD_1:
    209  1.46     markd 	case PCI_PRODUCT_INTEL_82Q33_IGD:
    210  1.46     markd 	case PCI_PRODUCT_INTEL_82Q33_IGD_1:
    211  1.57  christos 	case PCI_PRODUCT_INTEL_82G35_IGD:
    212  1.57  christos 	case PCI_PRODUCT_INTEL_82G35_IGD_1:
    213  1.55  matthias 	case PCI_PRODUCT_INTEL_82946GZ_IGD:
    214  1.58  christos 	case PCI_PRODUCT_INTEL_82GM45_IGD:
    215  1.58  christos 	case PCI_PRODUCT_INTEL_82GM45_IGD_1:
    216  1.62     markd 	case PCI_PRODUCT_INTEL_82IGD_E_IGD:
    217  1.62     markd 	case PCI_PRODUCT_INTEL_82Q45_IGD:
    218  1.62     markd 	case PCI_PRODUCT_INTEL_82G45_IGD:
    219  1.68       riz 	case PCI_PRODUCT_INTEL_82G41_IGD:
    220  1.68       riz 	case PCI_PRODUCT_INTEL_82B43_IGD:
    221  1.68       riz 	case PCI_PRODUCT_INTEL_IRONLAKE_D_IGD:
    222  1.68       riz 	case PCI_PRODUCT_INTEL_IRONLAKE_M_IGD:
    223  1.72      matt 	case PCI_PRODUCT_INTEL_PINEVIEW_IGD:
    224  1.72      matt 	case PCI_PRODUCT_INTEL_PINEVIEW_M_IGD:
    225   1.6   thorpej 		return (1);
    226   1.1      fvdl 	}
    227   1.1      fvdl 
    228   1.6   thorpej 	return (0);
    229   1.1      fvdl }
    230   1.1      fvdl 
    231  1.42     markd static int
    232  1.42     markd agp_i965_map_aperture(struct pci_attach_args *pa, struct agp_softc *sc, int reg)
    233  1.42     markd {
    234  1.42     markd         /*
    235  1.42     markd          * Find the aperture. Don't map it (yet), this would
    236  1.42     markd          * eat KVA.
    237  1.42     markd          */
    238  1.42     markd         if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg,
    239  1.42     markd             PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_64BIT, &sc->as_apaddr, &sc->as_apsize,
    240  1.42     markd             &sc->as_apflags) != 0)
    241  1.42     markd                 return ENXIO;
    242  1.42     markd 
    243  1.42     markd         sc->as_apt = pa->pa_memt;
    244  1.42     markd 
    245  1.42     markd         return 0;
    246  1.42     markd }
    247  1.42     markd 
    248   1.1      fvdl int
    249  1.54     freza agp_i810_attach(device_t parent, device_t self, void *aux)
    250   1.1      fvdl {
    251  1.54     freza 	struct agp_softc *sc = device_private(self);
    252   1.1      fvdl 	struct agp_i810_softc *isc;
    253   1.1      fvdl 	struct agp_gatt *gatt;
    254  1.28  christos 	int error, apbase;
    255  1.49  drochner 	bus_addr_t mmadr;
    256  1.37  drochner 	bus_size_t mmadrsize;
    257   1.1      fvdl 
    258  1.10   tsutsui 	isc = malloc(sizeof *isc, M_AGP, M_NOWAIT|M_ZERO);
    259   1.1      fvdl 	if (isc == NULL) {
    260  1.15   thorpej 		aprint_error(": can't allocate chipset-specific softc\n");
    261   1.1      fvdl 		return ENOMEM;
    262   1.1      fvdl 	}
    263   1.1      fvdl 	sc->as_chipc = isc;
    264   1.1      fvdl 	sc->as_methods = &agp_i810_methods;
    265   1.1      fvdl 
    266   1.1      fvdl 	if (pci_find_device(&isc->vga_pa, agp_i810_vgamatch) == 0) {
    267  1.20      tron #if NAGP_INTEL > 0
    268  1.19      tron 		const struct pci_attach_args *pa = aux;
    269  1.19      tron 
    270  1.19      tron 		switch (PCI_PRODUCT(pa->pa_id)) {
    271  1.19      tron 		case PCI_PRODUCT_INTEL_82840_HB:
    272  1.19      tron 		case PCI_PRODUCT_INTEL_82865_HB:
    273  1.21      tron 		case PCI_PRODUCT_INTEL_82845G_DRAM:
    274  1.23   xtraeme 		case PCI_PRODUCT_INTEL_82815_FULL_HUB:
    275  1.67  jakllsch 		case PCI_PRODUCT_INTEL_82855GM_MCH:
    276  1.19      tron 			return agp_intel_attach(parent, self, aux);
    277  1.20      tron 		}
    278  1.20      tron #endif
    279  1.15   thorpej 		aprint_error(": can't find internal VGA device config space\n");
    280   1.1      fvdl 		free(isc, M_AGP);
    281   1.1      fvdl 		return ENOENT;
    282   1.1      fvdl 	}
    283   1.1      fvdl 
    284   1.1      fvdl 	/* XXXfvdl */
    285   1.1      fvdl 	sc->as_dmat = isc->vga_pa.pa_dmat;
    286   1.1      fvdl 
    287  1.14       scw 	switch (PCI_PRODUCT(isc->vga_pa.pa_id)) {
    288  1.14       scw 	case PCI_PRODUCT_INTEL_82810_GC:
    289  1.14       scw 	case PCI_PRODUCT_INTEL_82810_DC100_GC:
    290  1.14       scw 	case PCI_PRODUCT_INTEL_82810E_GC:
    291  1.14       scw 	case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
    292  1.14       scw 		isc->chiptype = CHIP_I810;
    293  1.14       scw 		break;
    294  1.14       scw 	case PCI_PRODUCT_INTEL_82830MP_IV:
    295  1.14       scw 	case PCI_PRODUCT_INTEL_82845G_IGD:
    296  1.14       scw 		isc->chiptype = CHIP_I830;
    297  1.14       scw 		break;
    298  1.17   hannken 	case PCI_PRODUCT_INTEL_82855GM_IGD:
    299  1.18      tron 	case PCI_PRODUCT_INTEL_82865_IGD:
    300  1.17   hannken 		isc->chiptype = CHIP_I855;
    301  1.17   hannken 		break;
    302  1.28  christos 	case PCI_PRODUCT_INTEL_82915G_IGD:
    303  1.28  christos 	case PCI_PRODUCT_INTEL_82915GM_IGD:
    304  1.32    simonb 	case PCI_PRODUCT_INTEL_82945P_IGD:
    305  1.32    simonb 	case PCI_PRODUCT_INTEL_82945GM_IGD:
    306  1.32    simonb 	case PCI_PRODUCT_INTEL_82945GM_IGD_1:
    307  1.56       tnn 	case PCI_PRODUCT_INTEL_82945GME_IGD:
    308  1.68       riz 	case PCI_PRODUCT_INTEL_E7221_IGD:
    309  1.72      matt 	case PCI_PRODUCT_INTEL_PINEVIEW_IGD:
    310  1.72      matt 	case PCI_PRODUCT_INTEL_PINEVIEW_M_IGD:
    311  1.28  christos 		isc->chiptype = CHIP_I915;
    312  1.28  christos 		break;
    313  1.42     markd 	case PCI_PRODUCT_INTEL_82965Q_IGD:
    314  1.42     markd 	case PCI_PRODUCT_INTEL_82965Q_IGD_1:
    315  1.45     joerg 	case PCI_PRODUCT_INTEL_82965PM_IGD:
    316  1.45     joerg 	case PCI_PRODUCT_INTEL_82965PM_IGD_1:
    317  1.44   jnemeth 	case PCI_PRODUCT_INTEL_82965G_IGD:
    318  1.44   jnemeth 	case PCI_PRODUCT_INTEL_82965G_IGD_1:
    319  1.68       riz 	case PCI_PRODUCT_INTEL_82965GME_IGD:
    320  1.55  matthias 	case PCI_PRODUCT_INTEL_82946GZ_IGD:
    321  1.57  christos 	case PCI_PRODUCT_INTEL_82G35_IGD:
    322  1.57  christos 	case PCI_PRODUCT_INTEL_82G35_IGD_1:
    323  1.42     markd 		isc->chiptype = CHIP_I965;
    324  1.42     markd 		break;
    325  1.46     markd 	case PCI_PRODUCT_INTEL_82Q35_IGD:
    326  1.46     markd 	case PCI_PRODUCT_INTEL_82Q35_IGD_1:
    327  1.45     joerg 	case PCI_PRODUCT_INTEL_82G33_IGD:
    328  1.45     joerg 	case PCI_PRODUCT_INTEL_82G33_IGD_1:
    329  1.46     markd 	case PCI_PRODUCT_INTEL_82Q33_IGD:
    330  1.46     markd 	case PCI_PRODUCT_INTEL_82Q33_IGD_1:
    331  1.45     joerg 		isc->chiptype = CHIP_G33;
    332  1.63     markd 		break;
    333  1.58  christos 	case PCI_PRODUCT_INTEL_82GM45_IGD:
    334  1.58  christos 	case PCI_PRODUCT_INTEL_82GM45_IGD_1:
    335  1.62     markd 	case PCI_PRODUCT_INTEL_82IGD_E_IGD:
    336  1.62     markd 	case PCI_PRODUCT_INTEL_82Q45_IGD:
    337  1.62     markd 	case PCI_PRODUCT_INTEL_82G45_IGD:
    338  1.68       riz 	case PCI_PRODUCT_INTEL_82G41_IGD:
    339  1.68       riz 	case PCI_PRODUCT_INTEL_82B43_IGD:
    340  1.68       riz 	case PCI_PRODUCT_INTEL_IRONLAKE_D_IGD:
    341  1.68       riz 	case PCI_PRODUCT_INTEL_IRONLAKE_M_IGD:
    342  1.58  christos 		isc->chiptype = CHIP_G4X;
    343  1.45     joerg 		break;
    344  1.14       scw 	}
    345  1.14       scw 
    346  1.45     joerg 	switch (isc->chiptype) {
    347  1.45     joerg 	case CHIP_I915:
    348  1.45     joerg 	case CHIP_G33:
    349  1.45     joerg 		apbase = AGP_I915_GMADR;
    350  1.45     joerg 		break;
    351  1.58  christos 	case CHIP_I965:
    352  1.58  christos 	case CHIP_G4X:
    353  1.58  christos 		apbase = AGP_I965_GMADR;
    354  1.58  christos 		break;
    355  1.45     joerg 	default:
    356  1.45     joerg 		apbase = AGP_I810_GMADR;
    357  1.45     joerg 		break;
    358  1.45     joerg 	}
    359  1.58  christos 
    360  1.58  christos 	if (isc->chiptype == CHIP_I965 || isc->chiptype == CHIP_G4X) {
    361  1.58  christos 		error = agp_i965_map_aperture(&isc->vga_pa, sc, apbase);
    362  1.42     markd 	} else {
    363  1.42     markd 		error = agp_map_aperture(&isc->vga_pa, sc, apbase);
    364  1.42     markd 	}
    365   1.1      fvdl 	if (error != 0) {
    366  1.28  christos 		aprint_error(": can't map aperture\n");
    367  1.28  christos 		free(isc, M_AGP);
    368   1.1      fvdl 		return error;
    369   1.1      fvdl 	}
    370   1.1      fvdl 
    371  1.45     joerg 	if (isc->chiptype == CHIP_I915 || isc->chiptype == CHIP_G33) {
    372  1.28  christos 		error = pci_mapreg_map(&isc->vga_pa, AGP_I915_MMADR,
    373  1.39  drochner 		    PCI_MAPREG_TYPE_MEM, 0, &isc->bst, &isc->bsh,
    374  1.49  drochner 		    &mmadr, &mmadrsize);
    375  1.28  christos 		if (error != 0) {
    376  1.28  christos 			aprint_error(": can't map mmadr registers\n");
    377  1.28  christos 			agp_generic_detach(sc);
    378  1.28  christos 			return error;
    379  1.28  christos 		}
    380  1.28  christos 		error = pci_mapreg_map(&isc->vga_pa, AGP_I915_GTTADR,
    381  1.28  christos 		    PCI_MAPREG_TYPE_MEM, 0, &isc->gtt_bst, &isc->gtt_bsh,
    382  1.28  christos 		    NULL, NULL);
    383  1.28  christos 		if (error != 0) {
    384  1.28  christos 			aprint_error(": can't map gttadr registers\n");
    385  1.28  christos 			/* XXX we should release mmadr here */
    386  1.28  christos 			agp_generic_detach(sc);
    387  1.28  christos 			return error;
    388  1.28  christos 		}
    389  1.58  christos 	} else if (isc->chiptype == CHIP_I965 || isc->chiptype == CHIP_G4X) {
    390  1.42     markd 		error = pci_mapreg_map(&isc->vga_pa, AGP_I965_MMADR,
    391  1.42     markd 		    PCI_MAPREG_TYPE_MEM, 0, &isc->bst, &isc->bsh,
    392  1.49  drochner 		    &mmadr, &mmadrsize);
    393  1.42     markd 		if (error != 0) {
    394  1.42     markd 			aprint_error(": can't map mmadr registers\n");
    395  1.42     markd 			agp_generic_detach(sc);
    396  1.42     markd 			return error;
    397  1.42     markd 		}
    398  1.28  christos 	} else {
    399  1.28  christos 		error = pci_mapreg_map(&isc->vga_pa, AGP_I810_MMADR,
    400  1.39  drochner 		    PCI_MAPREG_TYPE_MEM, 0, &isc->bst, &isc->bsh,
    401  1.49  drochner 		    &mmadr, &mmadrsize);
    402  1.28  christos 		if (error != 0) {
    403  1.28  christos 			aprint_error(": can't map mmadr registers\n");
    404  1.28  christos 			agp_generic_detach(sc);
    405  1.28  christos 			return error;
    406  1.28  christos 		}
    407  1.28  christos 	}
    408  1.28  christos 
    409   1.1      fvdl 	isc->initial_aperture = AGP_GET_APERTURE(sc);
    410   1.1      fvdl 
    411  1.14       scw 	gatt = malloc(sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
    412  1.14       scw 	if (!gatt) {
    413  1.14       scw  		agp_generic_detach(sc);
    414  1.14       scw  		return ENOMEM;
    415  1.14       scw 	}
    416  1.14       scw 	isc->gatt = gatt;
    417  1.14       scw 
    418  1.14       scw 	gatt->ag_entries = AGP_GET_APERTURE(sc) >> AGP_PAGE_SHIFT;
    419   1.1      fvdl 
    420  1.47  jmcneill 	if (!pmf_device_register(self, NULL, agp_i810_resume))
    421  1.47  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    422  1.47  jmcneill 
    423  1.49  drochner 	/*
    424  1.49  drochner 	 * XXX horrible hack to allow drm code to use our mapping
    425  1.49  drochner 	 * of VGA chip registers
    426  1.49  drochner 	 */
    427  1.49  drochner 	agp_i810_vga_regbase = mmadr;
    428  1.49  drochner 	agp_i810_vga_bsh = isc->bsh;
    429  1.49  drochner 
    430  1.45     joerg 	return agp_i810_init(sc);
    431  1.45     joerg }
    432  1.45     joerg 
    433  1.49  drochner /*
    434  1.49  drochner  * XXX horrible hack to allow drm code to use our mapping
    435  1.49  drochner  * of VGA chip registers
    436  1.49  drochner  */
    437  1.49  drochner int
    438  1.49  drochner agp_i810_borrow(bus_addr_t base, bus_space_handle_t *hdlp)
    439  1.49  drochner {
    440  1.49  drochner 
    441  1.49  drochner 	if (!agp_i810_vga_regbase || base != agp_i810_vga_regbase)
    442  1.49  drochner 		return 0;
    443  1.49  drochner 	*hdlp = agp_i810_vga_bsh;
    444  1.49  drochner 	return 1;
    445  1.49  drochner }
    446  1.49  drochner 
    447  1.45     joerg static int agp_i810_init(struct agp_softc *sc)
    448  1.45     joerg {
    449  1.45     joerg 	struct agp_i810_softc *isc;
    450  1.45     joerg 	struct agp_gatt *gatt;
    451  1.45     joerg 
    452  1.45     joerg 	isc = sc->as_chipc;
    453  1.45     joerg 	gatt = isc->gatt;
    454  1.45     joerg 
    455  1.14       scw 	if (isc->chiptype == CHIP_I810) {
    456  1.36  christos 		void *virtual;
    457  1.14       scw 		int dummyseg;
    458  1.31      tron 
    459  1.14       scw 		/* Some i810s have on-chip memory called dcache */
    460  1.14       scw 		if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
    461  1.14       scw 			isc->dcache_size = 4 * 1024 * 1024;
    462  1.14       scw 		else
    463  1.14       scw 			isc->dcache_size = 0;
    464  1.14       scw 
    465  1.14       scw 		/* According to the specs the gatt on the i810 must be 64k */
    466  1.14       scw 		if (agp_alloc_dmamem(sc->as_dmat, 64 * 1024,
    467  1.31      tron 		    0, &gatt->ag_dmamap, &virtual, &gatt->ag_physical,
    468  1.31      tron 		    &gatt->ag_dmaseg, 1, &dummyseg) != 0) {
    469  1.14       scw 			free(gatt, M_AGP);
    470   1.1      fvdl 			agp_generic_detach(sc);
    471   1.1      fvdl 			return ENOMEM;
    472   1.1      fvdl 		}
    473  1.31      tron 		gatt->ag_virtual = (uint32_t *)virtual;
    474  1.14       scw 		gatt->ag_size = gatt->ag_entries * sizeof(u_int32_t);
    475  1.14       scw 		memset(gatt->ag_virtual, 0, gatt->ag_size);
    476  1.25     perry 
    477  1.14       scw 		agp_flush_cache();
    478  1.14       scw 		/* Install the GATT. */
    479  1.14       scw 		WRITE4(AGP_I810_PGTBL_CTL, gatt->ag_physical | 1);
    480  1.17   hannken 	} else if (isc->chiptype == CHIP_I830) {
    481  1.14       scw 		/* The i830 automatically initializes the 128k gatt on boot. */
    482  1.14       scw 		pcireg_t reg;
    483  1.14       scw 		u_int32_t pgtblctl;
    484  1.14       scw 		u_int16_t gcc1;
    485  1.14       scw 
    486  1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
    487  1.14       scw 		gcc1 = (u_int16_t)(reg >> 16);
    488  1.14       scw 		switch (gcc1 & AGP_I830_GCC1_GMS) {
    489  1.14       scw 		case AGP_I830_GCC1_GMS_STOLEN_512:
    490  1.14       scw 			isc->stolen = (512 - 132) * 1024 / 4096;
    491  1.14       scw 			break;
    492  1.25     perry 		case AGP_I830_GCC1_GMS_STOLEN_1024:
    493  1.14       scw 			isc->stolen = (1024 - 132) * 1024 / 4096;
    494  1.14       scw 			break;
    495  1.25     perry 		case AGP_I830_GCC1_GMS_STOLEN_8192:
    496  1.14       scw 			isc->stolen = (8192 - 132) * 1024 / 4096;
    497  1.14       scw 			break;
    498  1.14       scw 		default:
    499  1.14       scw 			isc->stolen = 0;
    500  1.15   thorpej 			aprint_error(
    501  1.15   thorpej 			    ": unknown memory configuration, disabling\n");
    502  1.14       scw 			agp_generic_detach(sc);
    503  1.14       scw 			return EINVAL;
    504  1.14       scw 		}
    505  1.45     joerg 
    506  1.14       scw 		if (isc->stolen > 0) {
    507  1.53  jmcneill 			aprint_normal(": detected %dk stolen memory\n%s",
    508  1.54     freza 			    isc->stolen * 4, device_xname(sc->as_dev));
    509  1.14       scw 		}
    510  1.17   hannken 
    511  1.17   hannken 		/* GATT address is already in there, make sure it's enabled */
    512  1.17   hannken 		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
    513  1.17   hannken 		pgtblctl |= 1;
    514  1.17   hannken 		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
    515  1.17   hannken 
    516  1.17   hannken 		gatt->ag_physical = pgtblctl & ~1;
    517  1.42     markd 	} else if (isc->chiptype == CHIP_I855 || isc->chiptype == CHIP_I915 ||
    518  1.58  christos 		   isc->chiptype == CHIP_I965 || isc->chiptype == CHIP_G33 ||
    519  1.58  christos 		   isc->chiptype == CHIP_G4X) {
    520  1.17   hannken 		pcireg_t reg;
    521  1.58  christos 		u_int32_t pgtblctl, gtt_size, stolen;
    522  1.17   hannken 		u_int16_t gcc1;
    523  1.17   hannken 
    524  1.45     joerg 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I855_GCC1);
    525  1.45     joerg 		gcc1 = (u_int16_t)(reg >> 16);
    526  1.45     joerg 
    527  1.58  christos 		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
    528  1.58  christos 
    529  1.42     markd 		/* Stolen memory is set up at the beginning of the aperture by
    530  1.42     markd                  * the BIOS, consisting of the GATT followed by 4kb for the
    531  1.42     markd 		 * BIOS display.
    532  1.42     markd                  */
    533  1.42     markd                 switch (isc->chiptype) {
    534  1.42     markd 		case CHIP_I855:
    535  1.58  christos 			gtt_size = 128;
    536  1.42     markd 			break;
    537  1.42     markd                 case CHIP_I915:
    538  1.58  christos 			gtt_size = 256;
    539  1.42     markd 			break;
    540  1.42     markd 		case CHIP_I965:
    541  1.60  christos 			switch (pgtblctl & AGP_I810_PGTBL_SIZE_MASK) {
    542  1.58  christos 			case AGP_I810_PGTBL_SIZE_128KB:
    543  1.58  christos 			case AGP_I810_PGTBL_SIZE_512KB:
    544  1.58  christos 				gtt_size = 512;
    545  1.58  christos 				break;
    546  1.58  christos 			case AGP_I965_PGTBL_SIZE_1MB:
    547  1.58  christos 				gtt_size = 1024;
    548  1.58  christos 				break;
    549  1.58  christos 			case AGP_I965_PGTBL_SIZE_2MB:
    550  1.61    sketch 				gtt_size = 2048;
    551  1.58  christos 				break;
    552  1.58  christos 			case AGP_I965_PGTBL_SIZE_1_5MB:
    553  1.61    sketch 				gtt_size = 1024 + 512;
    554  1.58  christos 				break;
    555  1.58  christos 			default:
    556  1.58  christos 				aprint_error("Bad PGTBL size\n");
    557  1.58  christos 				agp_generic_detach(sc);
    558  1.58  christos 				return EINVAL;
    559  1.58  christos 			}
    560  1.42     markd 			break;
    561  1.45     joerg 		case CHIP_G33:
    562  1.45     joerg 			switch (gcc1 & AGP_G33_PGTBL_SIZE_MASK) {
    563  1.45     joerg 			case AGP_G33_PGTBL_SIZE_1M:
    564  1.58  christos 				gtt_size = 1024;
    565  1.45     joerg 				break;
    566  1.45     joerg 			case AGP_G33_PGTBL_SIZE_2M:
    567  1.58  christos 				gtt_size = 2048;
    568  1.45     joerg 				break;
    569  1.45     joerg 			default:
    570  1.58  christos 				aprint_error(": Bad PGTBL size\n");
    571  1.45     joerg 				agp_generic_detach(sc);
    572  1.45     joerg 				return EINVAL;
    573  1.45     joerg 			}
    574  1.45     joerg 			break;
    575  1.58  christos 		case CHIP_G4X:
    576  1.58  christos 			gtt_size = 0;
    577  1.58  christos 			break;
    578  1.42     markd 		default:
    579  1.42     markd 			aprint_error(": bad chiptype\n");
    580  1.42     markd 			agp_generic_detach(sc);
    581  1.42     markd 			return EINVAL;
    582  1.58  christos 		}
    583  1.42     markd 
    584  1.17   hannken 		switch (gcc1 & AGP_I855_GCC1_GMS) {
    585  1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_1M:
    586  1.58  christos 			stolen = 1024;
    587  1.17   hannken 			break;
    588  1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_4M:
    589  1.58  christos 			stolen = 4 * 1024;
    590  1.17   hannken 			break;
    591  1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_8M:
    592  1.58  christos 			stolen = 8 * 1024;
    593  1.17   hannken 			break;
    594  1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_16M:
    595  1.58  christos 			stolen = 16 * 1024;
    596  1.17   hannken 			break;
    597  1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_32M:
    598  1.58  christos 			stolen = 32 * 1024;
    599  1.41  sborrill 			break;
    600  1.41  sborrill 		case AGP_I915_GCC1_GMS_STOLEN_48M:
    601  1.58  christos 			stolen = 48 * 1024;
    602  1.41  sborrill 			break;
    603  1.41  sborrill 		case AGP_I915_GCC1_GMS_STOLEN_64M:
    604  1.58  christos 			stolen = 64 * 1024;
    605  1.41  sborrill 			break;
    606  1.46     markd 		case AGP_G33_GCC1_GMS_STOLEN_128M:
    607  1.58  christos 			stolen = 128 * 1024;
    608  1.46     markd 			break;
    609  1.46     markd 		case AGP_G33_GCC1_GMS_STOLEN_256M:
    610  1.58  christos 			stolen = 256 * 1024;
    611  1.58  christos 			break;
    612  1.58  christos 		case AGP_G4X_GCC1_GMS_STOLEN_96M:
    613  1.58  christos 			stolen = 96 * 1024;
    614  1.58  christos 			break;
    615  1.58  christos 		case AGP_G4X_GCC1_GMS_STOLEN_160M:
    616  1.58  christos 			stolen = 160 * 1024;
    617  1.58  christos 			break;
    618  1.58  christos 		case AGP_G4X_GCC1_GMS_STOLEN_224M:
    619  1.58  christos 			stolen = 224 * 1024;
    620  1.58  christos 			break;
    621  1.58  christos 		case AGP_G4X_GCC1_GMS_STOLEN_352M:
    622  1.58  christos 			stolen = 352 * 1024;
    623  1.46     markd 			break;
    624  1.28  christos 		default:
    625  1.28  christos 			aprint_error(
    626  1.28  christos 			    ": unknown memory configuration, disabling\n");
    627  1.28  christos 			agp_generic_detach(sc);
    628  1.28  christos 			return EINVAL;
    629  1.28  christos 		}
    630  1.58  christos 
    631  1.58  christos 		switch (gcc1 & AGP_I855_GCC1_GMS) {
    632  1.58  christos 		case AGP_I915_GCC1_GMS_STOLEN_48M:
    633  1.58  christos 		case AGP_I915_GCC1_GMS_STOLEN_64M:
    634  1.58  christos 			if (isc->chiptype != CHIP_I915 &&
    635  1.58  christos 			    isc->chiptype != CHIP_I965 &&
    636  1.58  christos 			    isc->chiptype != CHIP_G33 &&
    637  1.58  christos 			    isc->chiptype != CHIP_G4X)
    638  1.58  christos 				stolen = 0;
    639  1.58  christos 			break;
    640  1.58  christos 		case AGP_G33_GCC1_GMS_STOLEN_128M:
    641  1.58  christos 		case AGP_G33_GCC1_GMS_STOLEN_256M:
    642  1.58  christos 			if (isc->chiptype != CHIP_I965 &&
    643  1.58  christos 			    isc->chiptype != CHIP_G33 &&
    644  1.58  christos 			    isc->chiptype != CHIP_G4X)
    645  1.58  christos 				stolen = 0;
    646  1.58  christos 			break;
    647  1.58  christos 		case AGP_G4X_GCC1_GMS_STOLEN_96M:
    648  1.58  christos 		case AGP_G4X_GCC1_GMS_STOLEN_160M:
    649  1.58  christos 		case AGP_G4X_GCC1_GMS_STOLEN_224M:
    650  1.58  christos 		case AGP_G4X_GCC1_GMS_STOLEN_352M:
    651  1.58  christos 			if (isc->chiptype != CHIP_I965 &&
    652  1.58  christos 			    isc->chiptype != CHIP_G4X)
    653  1.58  christos 				stolen = 0;
    654  1.58  christos 			break;
    655  1.58  christos 		}
    656  1.58  christos 
    657  1.58  christos 		/* BIOS space */
    658  1.62     markd 		gtt_size += 4;
    659  1.58  christos 
    660  1.58  christos 		isc->stolen = (stolen - gtt_size) * 1024 / 4096;
    661  1.58  christos 
    662  1.28  christos 		if (isc->stolen > 0) {
    663  1.53  jmcneill 			aprint_normal(": detected %dk stolen memory\n%s",
    664  1.54     freza 			    isc->stolen * 4, device_xname(sc->as_dev));
    665  1.28  christos 		}
    666  1.28  christos 
    667  1.28  christos 		/* GATT address is already in there, make sure it's enabled */
    668  1.28  christos 		pgtblctl |= 1;
    669  1.28  christos 		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
    670  1.28  christos 
    671  1.28  christos 		gatt->ag_physical = pgtblctl & ~1;
    672   1.1      fvdl 	}
    673   1.1      fvdl 
    674   1.1      fvdl 	/*
    675   1.1      fvdl 	 * Make sure the chipset can see everything.
    676   1.1      fvdl 	 */
    677   1.1      fvdl 	agp_flush_cache();
    678  1.14       scw 
    679   1.1      fvdl 	return 0;
    680   1.1      fvdl }
    681   1.1      fvdl 
    682   1.1      fvdl #if 0
    683   1.1      fvdl static int
    684   1.1      fvdl agp_i810_detach(struct agp_softc *sc)
    685   1.1      fvdl {
    686   1.1      fvdl 	int error;
    687   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    688   1.1      fvdl 
    689   1.1      fvdl 	error = agp_generic_detach(sc);
    690   1.1      fvdl 	if (error)
    691   1.1      fvdl 		return error;
    692   1.1      fvdl 
    693   1.1      fvdl 	/* Clear the GATT base. */
    694  1.14       scw 	if (sc->chiptype == CHIP_I810) {
    695  1.14       scw 		WRITE4(AGP_I810_PGTBL_CTL, 0);
    696  1.14       scw 	} else {
    697  1.14       scw 		unsigned int pgtblctl;
    698  1.14       scw 		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
    699  1.14       scw 		pgtblctl &= ~1;
    700  1.14       scw 		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
    701  1.14       scw 	}
    702   1.1      fvdl 
    703   1.1      fvdl 	/* Put the aperture back the way it started. */
    704   1.1      fvdl 	AGP_SET_APERTURE(sc, isc->initial_aperture);
    705   1.1      fvdl 
    706  1.14       scw 	if (sc->chiptype == CHIP_I810) {
    707  1.14       scw 		agp_free_dmamem(sc->as_dmat, gatt->ag_size, gatt->ag_dmamap,
    708  1.36  christos 		    (void *)gatt->ag_virtual, &gatt->ag_dmaseg, 1);
    709  1.14       scw 	}
    710  1.14       scw 	free(sc->gatt, M_AGP);
    711   1.1      fvdl 
    712   1.1      fvdl 	return 0;
    713   1.1      fvdl }
    714   1.1      fvdl #endif
    715   1.1      fvdl 
    716   1.1      fvdl static u_int32_t
    717   1.1      fvdl agp_i810_get_aperture(struct agp_softc *sc)
    718   1.1      fvdl {
    719  1.14       scw 	struct agp_i810_softc *isc = sc->as_chipc;
    720  1.14       scw 	pcireg_t reg;
    721  1.58  christos 	u_int32_t size;
    722  1.42     markd 	u_int16_t miscc, gcc1, msac;
    723  1.14       scw 
    724  1.58  christos 	size = 0;
    725  1.58  christos 
    726  1.42     markd 	switch (isc->chiptype) {
    727  1.42     markd 	case CHIP_I810:
    728  1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
    729  1.14       scw 		miscc = (u_int16_t)(reg >> 16);
    730  1.14       scw 		if ((miscc & AGP_I810_MISCC_WINSIZE) ==
    731  1.14       scw 		    AGP_I810_MISCC_WINSIZE_32)
    732  1.58  christos 			size = 32 * 1024 * 1024;
    733  1.14       scw 		else
    734  1.58  christos 			size = 64 * 1024 * 1024;
    735  1.58  christos 		break;
    736  1.42     markd 	case CHIP_I830:
    737  1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
    738  1.14       scw 		gcc1 = (u_int16_t)(reg >> 16);
    739  1.14       scw 		if ((gcc1 & AGP_I830_GCC1_GMASIZE) == AGP_I830_GCC1_GMASIZE_64)
    740  1.58  christos 			size = 64 * 1024 * 1024;
    741  1.14       scw 		else
    742  1.58  christos 			size = 128 * 1024 * 1024;
    743  1.58  christos 		break;
    744  1.42     markd 	case CHIP_I855:
    745  1.58  christos 		size = 128 * 1024 * 1024;
    746  1.58  christos 		break;
    747  1.42     markd 	case CHIP_I915:
    748  1.45     joerg 	case CHIP_G33:
    749  1.64     markd 	case CHIP_G4X:
    750  1.28  christos 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I915_MSAC);
    751  1.28  christos 		msac = (u_int16_t)(reg >> 16);
    752  1.28  christos 		if (msac & AGP_I915_MSAC_APER_128M)
    753  1.58  christos 			size = 128 * 1024 * 1024;
    754  1.28  christos 		else
    755  1.58  christos 			size = 256 * 1024 * 1024;
    756  1.58  christos 		break;
    757  1.42     markd 	case CHIP_I965:
    758  1.58  christos 		size = 512 * 1024 * 1024;
    759  1.58  christos 		break;
    760  1.42     markd 	default:
    761  1.42     markd 		aprint_error(": Unknown chipset\n");
    762  1.14       scw 	}
    763  1.42     markd 
    764  1.58  christos 	return size;
    765   1.1      fvdl }
    766   1.1      fvdl 
    767   1.1      fvdl static int
    768   1.1      fvdl agp_i810_set_aperture(struct agp_softc *sc, u_int32_t aperture)
    769   1.1      fvdl {
    770  1.14       scw 	struct agp_i810_softc *isc = sc->as_chipc;
    771  1.14       scw 	pcireg_t reg;
    772  1.42     markd 	u_int16_t miscc, gcc1;
    773  1.14       scw 
    774  1.42     markd 	switch (isc->chiptype) {
    775  1.42     markd 	case CHIP_I810:
    776  1.14       scw 		/*
    777  1.14       scw 		 * Double check for sanity.
    778  1.14       scw 		 */
    779  1.14       scw 		if (aperture != (32 * 1024 * 1024) &&
    780  1.14       scw 		    aperture != (64 * 1024 * 1024)) {
    781  1.54     freza 			aprint_error_dev(sc->as_dev, "bad aperture size %d\n",
    782  1.52    cegger 			    aperture);
    783  1.14       scw 			return EINVAL;
    784  1.14       scw 		}
    785   1.1      fvdl 
    786  1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
    787  1.14       scw 		miscc = (u_int16_t)(reg >> 16);
    788  1.14       scw 		miscc &= ~AGP_I810_MISCC_WINSIZE;
    789  1.14       scw 		if (aperture == 32 * 1024 * 1024)
    790  1.14       scw 			miscc |= AGP_I810_MISCC_WINSIZE_32;
    791  1.14       scw 		else
    792  1.14       scw 			miscc |= AGP_I810_MISCC_WINSIZE_64;
    793  1.14       scw 
    794  1.14       scw 		reg &= 0x0000ffff;
    795  1.14       scw 		reg |= ((pcireg_t)miscc) << 16;
    796  1.14       scw 		pci_conf_write(sc->as_pc, sc->as_tag, AGP_I810_SMRAM, reg);
    797  1.42     markd 		break;
    798  1.42     markd 	case CHIP_I830:
    799  1.14       scw 		if (aperture != (64 * 1024 * 1024) &&
    800  1.14       scw 		    aperture != (128 * 1024 * 1024)) {
    801  1.54     freza 			aprint_error_dev(sc->as_dev, "bad aperture size %d\n",
    802  1.52    cegger 			    aperture);
    803  1.14       scw 			return EINVAL;
    804  1.14       scw 		}
    805  1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
    806  1.14       scw 		gcc1 = (u_int16_t)(reg >> 16);
    807  1.14       scw 		gcc1 &= ~AGP_I830_GCC1_GMASIZE;
    808  1.14       scw 		if (aperture == 64 * 1024 * 1024)
    809  1.14       scw 			gcc1 |= AGP_I830_GCC1_GMASIZE_64;
    810  1.14       scw 		else
    811  1.14       scw 			gcc1 |= AGP_I830_GCC1_GMASIZE_128;
    812  1.14       scw 
    813  1.14       scw 		reg &= 0x0000ffff;
    814  1.14       scw 		reg |= ((pcireg_t)gcc1) << 16;
    815  1.14       scw 		pci_conf_write(sc->as_pc, sc->as_tag, AGP_I830_GCC0, reg);
    816  1.42     markd 		break;
    817  1.42     markd 	case CHIP_I855:
    818  1.42     markd 	case CHIP_I915:
    819  1.28  christos 		if (aperture != agp_i810_get_aperture(sc)) {
    820  1.54     freza 			aprint_error_dev(sc->as_dev, "bad aperture size %d\n",
    821  1.52    cegger 			    aperture);
    822  1.17   hannken 			return EINVAL;
    823  1.17   hannken 		}
    824  1.42     markd 		break;
    825  1.42     markd 	case CHIP_I965:
    826  1.42     markd 		if (aperture != 512 * 1024 * 1024) {
    827  1.54     freza 			aprint_error_dev(sc->as_dev, "bad aperture size %d\n",
    828  1.52    cegger 			    aperture);
    829  1.42     markd 			return EINVAL;
    830  1.42     markd 		}
    831  1.42     markd 		break;
    832   1.1      fvdl 	}
    833   1.1      fvdl 
    834   1.1      fvdl 	return 0;
    835   1.1      fvdl }
    836   1.1      fvdl 
    837   1.1      fvdl static int
    838   1.1      fvdl agp_i810_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
    839   1.1      fvdl {
    840   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    841   1.1      fvdl 
    842  1.14       scw 	if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
    843  1.29    rpaulo #ifdef AGP_DEBUG
    844  1.14       scw 		printf("%s: failed: offset 0x%08x, shift %d, entries %d\n",
    845  1.54     freza 		    device_xname(sc->as_dev), (int)offset, AGP_PAGE_SHIFT,
    846  1.14       scw 		    isc->gatt->ag_entries);
    847  1.14       scw #endif
    848   1.1      fvdl 		return EINVAL;
    849  1.14       scw 	}
    850  1.14       scw 
    851  1.70    gsutre 	if (isc->chiptype != CHIP_I810) {
    852  1.14       scw 		if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
    853  1.29    rpaulo #ifdef AGP_DEBUG
    854  1.70    gsutre 			printf("%s: trying to bind into stolen memory\n",
    855  1.54     freza 			    device_xname(sc->as_dev));
    856  1.14       scw #endif
    857  1.14       scw 			return EINVAL;
    858  1.14       scw 		}
    859  1.14       scw 	}
    860   1.1      fvdl 
    861  1.71    gsutre 	return agp_i810_write_gtt_entry(isc, offset, physical | 1);
    862   1.1      fvdl }
    863   1.1      fvdl 
    864   1.1      fvdl static int
    865   1.1      fvdl agp_i810_unbind_page(struct agp_softc *sc, off_t offset)
    866   1.1      fvdl {
    867   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    868   1.1      fvdl 
    869   1.1      fvdl 	if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
    870   1.1      fvdl 		return EINVAL;
    871   1.1      fvdl 
    872  1.17   hannken 	if (isc->chiptype != CHIP_I810 ) {
    873  1.14       scw 		if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
    874  1.29    rpaulo #ifdef AGP_DEBUG
    875  1.70    gsutre 			printf("%s: trying to unbind from stolen memory\n",
    876  1.54     freza 			    device_xname(sc->as_dev));
    877  1.14       scw #endif
    878  1.14       scw 			return EINVAL;
    879  1.14       scw 		}
    880  1.14       scw 	}
    881  1.14       scw 
    882  1.71    gsutre 	return agp_i810_write_gtt_entry(isc, offset, 0);
    883   1.1      fvdl }
    884   1.1      fvdl 
    885   1.1      fvdl /*
    886   1.1      fvdl  * Writing via memory mapped registers already flushes all TLBs.
    887   1.1      fvdl  */
    888   1.1      fvdl static void
    889  1.35  christos agp_i810_flush_tlb(struct agp_softc *sc)
    890   1.1      fvdl {
    891   1.1      fvdl }
    892   1.1      fvdl 
    893   1.1      fvdl static int
    894  1.35  christos agp_i810_enable(struct agp_softc *sc, u_int32_t mode)
    895   1.1      fvdl {
    896   1.1      fvdl 
    897   1.1      fvdl 	return 0;
    898   1.1      fvdl }
    899   1.1      fvdl 
    900   1.1      fvdl static struct agp_memory *
    901   1.1      fvdl agp_i810_alloc_memory(struct agp_softc *sc, int type, vsize_t size)
    902   1.1      fvdl {
    903   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    904   1.1      fvdl 	struct agp_memory *mem;
    905   1.1      fvdl 
    906  1.29    rpaulo #ifdef AGP_DEBUG
    907  1.28  christos 	printf("AGP: alloc(%d, 0x%x)\n", type, (int) size);
    908  1.28  christos #endif
    909  1.28  christos 
    910   1.1      fvdl 	if ((size & (AGP_PAGE_SIZE - 1)) != 0)
    911   1.1      fvdl 		return 0;
    912   1.1      fvdl 
    913   1.1      fvdl 	if (sc->as_allocated + size > sc->as_maxmem)
    914   1.1      fvdl 		return 0;
    915   1.1      fvdl 
    916   1.1      fvdl 	if (type == 1) {
    917   1.1      fvdl 		/*
    918   1.1      fvdl 		 * Mapping local DRAM into GATT.
    919   1.1      fvdl 		 */
    920  1.17   hannken 		if (isc->chiptype != CHIP_I810 )
    921  1.14       scw 			return 0;
    922   1.1      fvdl 		if (size != isc->dcache_size)
    923   1.1      fvdl 			return 0;
    924   1.1      fvdl 	} else if (type == 2) {
    925   1.1      fvdl 		/*
    926  1.28  christos 		 * Bogus mapping for the hardware cursor.
    927   1.1      fvdl 		 */
    928  1.28  christos 		if (size != AGP_PAGE_SIZE && size != 4 * AGP_PAGE_SIZE)
    929   1.1      fvdl 			return 0;
    930   1.1      fvdl 	}
    931   1.1      fvdl 
    932  1.10   tsutsui 	mem = malloc(sizeof *mem, M_AGP, M_WAITOK|M_ZERO);
    933   1.1      fvdl 	if (mem == NULL)
    934   1.1      fvdl 		return NULL;
    935   1.1      fvdl 	mem->am_id = sc->as_nextid++;
    936   1.1      fvdl 	mem->am_size = size;
    937   1.1      fvdl 	mem->am_type = type;
    938   1.1      fvdl 
    939   1.1      fvdl 	if (type == 2) {
    940   1.1      fvdl 		/*
    941  1.28  christos 		 * Allocate and wire down the memory now so that we can
    942   1.1      fvdl 		 * get its physical address.
    943   1.1      fvdl 		 */
    944   1.1      fvdl 		mem->am_dmaseg = malloc(sizeof *mem->am_dmaseg, M_AGP,
    945   1.1      fvdl 		    M_WAITOK);
    946   1.1      fvdl 		if (mem->am_dmaseg == NULL) {
    947   1.1      fvdl 			free(mem, M_AGP);
    948   1.1      fvdl 			return NULL;
    949   1.1      fvdl 		}
    950   1.1      fvdl 		if (agp_alloc_dmamem(sc->as_dmat, size, 0,
    951   1.1      fvdl 		    &mem->am_dmamap, &mem->am_virtual, &mem->am_physical,
    952   1.1      fvdl 		    mem->am_dmaseg, 1, &mem->am_nseg) != 0) {
    953   1.1      fvdl 			free(mem->am_dmaseg, M_AGP);
    954   1.1      fvdl 			free(mem, M_AGP);
    955   1.1      fvdl 			return NULL;
    956   1.1      fvdl 		}
    957  1.28  christos 		memset(mem->am_virtual, 0, size);
    958   1.1      fvdl 	} else if (type != 1) {
    959   1.4  drochner 		if (bus_dmamap_create(sc->as_dmat, size, size / PAGE_SIZE + 1,
    960   1.4  drochner 				      size, 0, BUS_DMA_NOWAIT,
    961   1.4  drochner 				      &mem->am_dmamap) != 0) {
    962   1.1      fvdl 			free(mem, M_AGP);
    963   1.1      fvdl 			return NULL;
    964   1.1      fvdl 		}
    965   1.1      fvdl 	}
    966   1.1      fvdl 
    967   1.1      fvdl 	TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
    968   1.1      fvdl 	sc->as_allocated += size;
    969   1.1      fvdl 
    970   1.1      fvdl 	return mem;
    971   1.1      fvdl }
    972   1.1      fvdl 
    973   1.1      fvdl static int
    974   1.1      fvdl agp_i810_free_memory(struct agp_softc *sc, struct agp_memory *mem)
    975   1.1      fvdl {
    976   1.1      fvdl 	if (mem->am_is_bound)
    977   1.1      fvdl 		return EBUSY;
    978   1.1      fvdl 
    979   1.1      fvdl 	if (mem->am_type == 2) {
    980   1.1      fvdl 		agp_free_dmamem(sc->as_dmat, mem->am_size, mem->am_dmamap,
    981   1.1      fvdl 		    mem->am_virtual, mem->am_dmaseg, mem->am_nseg);
    982   1.1      fvdl 		free(mem->am_dmaseg, M_AGP);
    983   1.1      fvdl 	}
    984   1.1      fvdl 
    985   1.1      fvdl 	sc->as_allocated -= mem->am_size;
    986   1.1      fvdl 	TAILQ_REMOVE(&sc->as_memory, mem, am_link);
    987   1.1      fvdl 	free(mem, M_AGP);
    988   1.1      fvdl 	return 0;
    989   1.1      fvdl }
    990   1.1      fvdl 
    991   1.1      fvdl static int
    992   1.1      fvdl agp_i810_bind_memory(struct agp_softc *sc, struct agp_memory *mem,
    993   1.1      fvdl 		     off_t offset)
    994   1.1      fvdl {
    995   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    996   1.4  drochner 	u_int32_t regval, i;
    997   1.4  drochner 
    998  1.70    gsutre 	if (mem->am_is_bound != 0)
    999  1.70    gsutre 		return EINVAL;
   1000  1.70    gsutre 
   1001   1.4  drochner 	/*
   1002   1.4  drochner 	 * XXX evil hack: the PGTBL_CTL appearently gets overwritten by the
   1003   1.4  drochner 	 * X server for mysterious reasons which leads to crashes if we write
   1004   1.4  drochner 	 * to the GTT through the MMIO window.
   1005   1.4  drochner 	 * Until the issue is solved, simply restore it.
   1006   1.4  drochner 	 */
   1007   1.4  drochner 	regval = bus_space_read_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL);
   1008   1.4  drochner 	if (regval != (isc->gatt->ag_physical | 1)) {
   1009   1.4  drochner 		printf("agp_i810_bind_memory: PGTBL_CTL is 0x%x - fixing\n",
   1010   1.4  drochner 		       regval);
   1011   1.4  drochner 		bus_space_write_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL,
   1012   1.4  drochner 				  isc->gatt->ag_physical | 1);
   1013   1.4  drochner 	}
   1014   1.1      fvdl 
   1015   1.5  drochner 	if (mem->am_type == 2) {
   1016  1.70    gsutre 		for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
   1017  1.70    gsutre 			agp_i810_bind_page(sc, offset + i,
   1018  1.70    gsutre 			    mem->am_physical + i);
   1019   1.5  drochner 		mem->am_offset = offset;
   1020   1.5  drochner 		mem->am_is_bound = 1;
   1021   1.1      fvdl 		return 0;
   1022   1.5  drochner 	}
   1023   1.5  drochner 
   1024   1.1      fvdl 	if (mem->am_type != 1)
   1025   1.1      fvdl 		return agp_generic_bind_memory(sc, mem, offset);
   1026   1.1      fvdl 
   1027  1.17   hannken 	if (isc->chiptype != CHIP_I810)
   1028  1.14       scw 		return EINVAL;
   1029  1.14       scw 
   1030  1.28  christos 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
   1031  1.70    gsutre 		agp_i810_write_gtt_entry(isc, i, i | 3);
   1032  1.13  drochner 	mem->am_is_bound = 1;
   1033   1.1      fvdl 	return 0;
   1034   1.1      fvdl }
   1035   1.1      fvdl 
   1036   1.1      fvdl static int
   1037   1.1      fvdl agp_i810_unbind_memory(struct agp_softc *sc, struct agp_memory *mem)
   1038   1.1      fvdl {
   1039   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
   1040   1.1      fvdl 	u_int32_t i;
   1041   1.1      fvdl 
   1042  1.70    gsutre 	if (mem->am_is_bound == 0)
   1043  1.70    gsutre 		return EINVAL;
   1044  1.70    gsutre 
   1045   1.5  drochner 	if (mem->am_type == 2) {
   1046  1.70    gsutre 		for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
   1047  1.70    gsutre 			agp_i810_unbind_page(sc, mem->am_offset + i);
   1048   1.5  drochner 		mem->am_offset = 0;
   1049   1.5  drochner 		mem->am_is_bound = 0;
   1050   1.1      fvdl 		return 0;
   1051   1.5  drochner 	}
   1052   1.1      fvdl 
   1053   1.1      fvdl 	if (mem->am_type != 1)
   1054   1.1      fvdl 		return agp_generic_unbind_memory(sc, mem);
   1055  1.14       scw 
   1056  1.17   hannken 	if (isc->chiptype != CHIP_I810)
   1057  1.14       scw 		return EINVAL;
   1058   1.1      fvdl 
   1059   1.1      fvdl 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
   1060  1.58  christos 		agp_i810_write_gtt_entry(isc, i, 0);
   1061  1.13  drochner 	mem->am_is_bound = 0;
   1062   1.1      fvdl 	return 0;
   1063   1.1      fvdl }
   1064  1.24  jmcneill 
   1065  1.47  jmcneill static bool
   1066  1.66    dyoung agp_i810_resume(device_t dv, const pmf_qual_t *qual)
   1067  1.24  jmcneill {
   1068  1.47  jmcneill 	struct agp_softc *sc = device_private(dv);
   1069  1.24  jmcneill 	struct agp_i810_softc *isc = sc->as_chipc;
   1070  1.24  jmcneill 
   1071  1.47  jmcneill 	isc->pgtblctl = READ4(AGP_I810_PGTBL_CTL);
   1072  1.47  jmcneill 	agp_flush_cache();
   1073  1.24  jmcneill 
   1074  1.47  jmcneill 	return true;
   1075  1.24  jmcneill }
   1076