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