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