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