Home | History | Annotate | Line # | Download | only in pci
agp_i810.c revision 1.26.2.7
      1  1.26.2.7      yamt /*	$NetBSD: agp_i810.c,v 1.26.2.7 2008/01/21 09:43:33 yamt Exp $	*/
      2       1.1      fvdl 
      3       1.1      fvdl /*-
      4       1.1      fvdl  * Copyright (c) 2000 Doug Rabson
      5       1.1      fvdl  * Copyright (c) 2000 Ruslan Ermilov
      6       1.1      fvdl  * All rights reserved.
      7       1.1      fvdl  *
      8       1.1      fvdl  * Redistribution and use in source and binary forms, with or without
      9       1.1      fvdl  * modification, are permitted provided that the following conditions
     10       1.1      fvdl  * are met:
     11       1.1      fvdl  * 1. Redistributions of source code must retain the above copyright
     12       1.1      fvdl  *    notice, this list of conditions and the following disclaimer.
     13       1.1      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1      fvdl  *    notice, this list of conditions and the following disclaimer in the
     15       1.1      fvdl  *    documentation and/or other materials provided with the distribution.
     16       1.1      fvdl  *
     17       1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18       1.1      fvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19       1.1      fvdl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20       1.1      fvdl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21       1.1      fvdl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22       1.1      fvdl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23       1.1      fvdl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24       1.1      fvdl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25       1.1      fvdl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26       1.1      fvdl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27       1.1      fvdl  * SUCH DAMAGE.
     28       1.1      fvdl  *
     29       1.1      fvdl  *	$FreeBSD: src/sys/pci/agp_i810.c,v 1.4 2001/07/05 21:28:47 jhb Exp $
     30       1.1      fvdl  */
     31       1.9     lukem 
     32       1.9     lukem #include <sys/cdefs.h>
     33  1.26.2.7      yamt __KERNEL_RCSID(0, "$NetBSD: agp_i810.c,v 1.26.2.7 2008/01/21 09:43:33 yamt 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 <uvm/uvm_extern.h>
     44       1.1      fvdl 
     45       1.1      fvdl #include <dev/pci/pcivar.h>
     46       1.1      fvdl #include <dev/pci/pcireg.h>
     47       1.1      fvdl #include <dev/pci/pcidevs.h>
     48       1.1      fvdl #include <dev/pci/agpvar.h>
     49       1.1      fvdl #include <dev/pci/agpreg.h>
     50       1.1      fvdl 
     51       1.1      fvdl #include <sys/agpio.h>
     52       1.1      fvdl 
     53  1.26.2.4      yamt #include <sys/bus.h>
     54       1.1      fvdl 
     55      1.20      tron #include "agp_intel.h"
     56      1.20      tron 
     57       1.1      fvdl #define READ1(off)	bus_space_read_1(isc->bst, isc->bsh, off)
     58      1.14       scw #define READ4(off)	bus_space_read_4(isc->bst, isc->bsh, off)
     59       1.1      fvdl #define WRITE4(off,v)	bus_space_write_4(isc->bst, isc->bsh, off, v)
     60  1.26.2.1      yamt #define WRITEGTT(off, v)						\
     61  1.26.2.1      yamt 	do {								\
     62  1.26.2.5      yamt 		if (isc->chiptype == CHIP_I915 || isc->chiptype == CHIP_G33) { \
     63  1.26.2.1      yamt 			bus_space_write_4(isc->gtt_bst, isc->gtt_bsh,	\
     64  1.26.2.1      yamt 			    (u_int32_t)((off) >> AGP_PAGE_SHIFT) * 4,	\
     65  1.26.2.1      yamt 			    (v));					\
     66  1.26.2.3      yamt 		} else if (isc->chiptype == CHIP_I965) {		\
     67  1.26.2.3      yamt 			WRITE4(AGP_I965_GTT +				\
     68  1.26.2.3      yamt 			    (u_int32_t)((off) >> AGP_PAGE_SHIFT) * 4,	\
     69  1.26.2.3      yamt 			    (v));					\
     70  1.26.2.1      yamt 		} else {						\
     71  1.26.2.1      yamt 			WRITE4(AGP_I810_GTT +				\
     72  1.26.2.1      yamt 			    (u_int32_t)((off) >> AGP_PAGE_SHIFT) * 4,	\
     73  1.26.2.1      yamt 			    (v));					\
     74  1.26.2.1      yamt 		}							\
     75  1.26.2.1      yamt 	} while (0)
     76       1.1      fvdl 
     77      1.14       scw #define CHIP_I810 0	/* i810/i815 */
     78      1.17   hannken #define CHIP_I830 1	/* 830M/845G */
     79      1.17   hannken #define CHIP_I855 2	/* 852GM/855GM/865G */
     80  1.26.2.2      yamt #define CHIP_I915 3	/* 915G/915GM/945G/945GM */
     81  1.26.2.5      yamt #define CHIP_I965 4	/* 965Q/965PM */
     82  1.26.2.5      yamt #define CHIP_G33  5	/* G33/Q33/Q35 */
     83      1.14       scw 
     84       1.1      fvdl struct agp_i810_softc {
     85       1.1      fvdl 	u_int32_t initial_aperture;	/* aperture size at startup */
     86       1.1      fvdl 	struct agp_gatt *gatt;
     87      1.14       scw 	int chiptype;			/* i810-like or i830 */
     88      1.14       scw 	u_int32_t dcache_size;		/* i810 only */
     89      1.14       scw 	u_int32_t stolen;		/* number of i830/845 gtt entries
     90      1.14       scw 					   for stolen memory */
     91  1.26.2.1      yamt 	bus_space_tag_t bst;		/* register bus_space tag */
     92  1.26.2.1      yamt 	bus_space_handle_t bsh;		/* register bus_space handle */
     93  1.26.2.1      yamt 	bus_space_tag_t gtt_bst;	/* GTT bus_space tag */
     94  1.26.2.1      yamt 	bus_space_handle_t gtt_bsh;	/* GTT bus_space handle */
     95       1.1      fvdl 	struct pci_attach_args vga_pa;
     96      1.24  jmcneill 
     97  1.26.2.7      yamt 	u_int32_t pgtblctl;
     98       1.1      fvdl };
     99       1.1      fvdl 
    100       1.1      fvdl static u_int32_t agp_i810_get_aperture(struct agp_softc *);
    101       1.1      fvdl static int agp_i810_set_aperture(struct agp_softc *, u_int32_t);
    102       1.1      fvdl static int agp_i810_bind_page(struct agp_softc *, off_t, bus_addr_t);
    103       1.1      fvdl static int agp_i810_unbind_page(struct agp_softc *, off_t);
    104       1.1      fvdl static void agp_i810_flush_tlb(struct agp_softc *);
    105       1.1      fvdl static int agp_i810_enable(struct agp_softc *, u_int32_t mode);
    106       1.1      fvdl static struct agp_memory *agp_i810_alloc_memory(struct agp_softc *, int,
    107       1.1      fvdl 						vsize_t);
    108       1.1      fvdl static int agp_i810_free_memory(struct agp_softc *, struct agp_memory *);
    109       1.1      fvdl static int agp_i810_bind_memory(struct agp_softc *, struct agp_memory *, off_t);
    110       1.1      fvdl static int agp_i810_unbind_memory(struct agp_softc *, struct agp_memory *);
    111  1.26.2.7      yamt 
    112  1.26.2.7      yamt static bool agp_i810_resume(device_t);
    113  1.26.2.7      yamt static int agp_i810_init(struct agp_softc *);
    114       1.1      fvdl 
    115  1.26.2.5      yamt static int agp_i810_init(struct agp_softc *);
    116  1.26.2.5      yamt 
    117      1.26   thorpej static struct agp_methods agp_i810_methods = {
    118       1.1      fvdl 	agp_i810_get_aperture,
    119       1.1      fvdl 	agp_i810_set_aperture,
    120       1.1      fvdl 	agp_i810_bind_page,
    121       1.1      fvdl 	agp_i810_unbind_page,
    122       1.1      fvdl 	agp_i810_flush_tlb,
    123       1.1      fvdl 	agp_i810_enable,
    124       1.1      fvdl 	agp_i810_alloc_memory,
    125       1.1      fvdl 	agp_i810_free_memory,
    126       1.1      fvdl 	agp_i810_bind_memory,
    127       1.1      fvdl 	agp_i810_unbind_memory,
    128       1.1      fvdl };
    129       1.1      fvdl 
    130       1.6   thorpej /* XXXthorpej -- duplicated code (see arch/i386/pci/pchb.c) */
    131       1.1      fvdl static int
    132       1.1      fvdl agp_i810_vgamatch(struct pci_attach_args *pa)
    133       1.1      fvdl {
    134       1.6   thorpej 
    135       1.2      fvdl 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
    136       1.2      fvdl 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
    137       1.6   thorpej 		return (0);
    138       1.6   thorpej 
    139       1.1      fvdl 	switch (PCI_PRODUCT(pa->pa_id)) {
    140       1.1      fvdl 	case PCI_PRODUCT_INTEL_82810_GC:
    141       1.1      fvdl 	case PCI_PRODUCT_INTEL_82810_DC100_GC:
    142       1.1      fvdl 	case PCI_PRODUCT_INTEL_82810E_GC:
    143       1.1      fvdl 	case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
    144      1.14       scw 	case PCI_PRODUCT_INTEL_82830MP_IV:
    145      1.14       scw 	case PCI_PRODUCT_INTEL_82845G_IGD:
    146      1.17   hannken 	case PCI_PRODUCT_INTEL_82855GM_IGD:
    147      1.18      tron 	case PCI_PRODUCT_INTEL_82865_IGD:
    148  1.26.2.1      yamt 	case PCI_PRODUCT_INTEL_82915G_IGD:
    149  1.26.2.1      yamt 	case PCI_PRODUCT_INTEL_82915GM_IGD:
    150  1.26.2.2      yamt 	case PCI_PRODUCT_INTEL_82945P_IGD:
    151  1.26.2.2      yamt 	case PCI_PRODUCT_INTEL_82945GM_IGD:
    152  1.26.2.2      yamt 	case PCI_PRODUCT_INTEL_82945GM_IGD_1:
    153  1.26.2.3      yamt 	case PCI_PRODUCT_INTEL_82965Q_IGD:
    154  1.26.2.3      yamt 	case PCI_PRODUCT_INTEL_82965Q_IGD_1:
    155  1.26.2.5      yamt 	case PCI_PRODUCT_INTEL_82965PM_IGD:
    156  1.26.2.5      yamt 	case PCI_PRODUCT_INTEL_82965PM_IGD_1:
    157  1.26.2.5      yamt 	case PCI_PRODUCT_INTEL_82G33_IGD:
    158  1.26.2.5      yamt 	case PCI_PRODUCT_INTEL_82G33_IGD_1:
    159  1.26.2.5      yamt 	case PCI_PRODUCT_INTEL_82965G_IGD:
    160  1.26.2.5      yamt 	case PCI_PRODUCT_INTEL_82965G_IGD_1:
    161  1.26.2.6      yamt 	case PCI_PRODUCT_INTEL_82Q35_IGD:
    162  1.26.2.6      yamt 	case PCI_PRODUCT_INTEL_82Q35_IGD_1:
    163  1.26.2.6      yamt 	case PCI_PRODUCT_INTEL_82Q33_IGD:
    164  1.26.2.6      yamt 	case PCI_PRODUCT_INTEL_82Q33_IGD_1:
    165       1.6   thorpej 		return (1);
    166       1.1      fvdl 	}
    167       1.1      fvdl 
    168       1.6   thorpej 	return (0);
    169       1.1      fvdl }
    170       1.1      fvdl 
    171  1.26.2.3      yamt static int
    172  1.26.2.3      yamt agp_i965_map_aperture(struct pci_attach_args *pa, struct agp_softc *sc, int reg)
    173  1.26.2.3      yamt {
    174  1.26.2.3      yamt         /*
    175  1.26.2.3      yamt          * Find the aperture. Don't map it (yet), this would
    176  1.26.2.3      yamt          * eat KVA.
    177  1.26.2.3      yamt          */
    178  1.26.2.3      yamt         if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg,
    179  1.26.2.3      yamt             PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_64BIT, &sc->as_apaddr, &sc->as_apsize,
    180  1.26.2.3      yamt             &sc->as_apflags) != 0)
    181  1.26.2.3      yamt                 return ENXIO;
    182  1.26.2.3      yamt 
    183  1.26.2.3      yamt         sc->as_apt = pa->pa_memt;
    184  1.26.2.3      yamt 
    185  1.26.2.3      yamt         return 0;
    186  1.26.2.3      yamt }
    187  1.26.2.3      yamt 
    188       1.1      fvdl int
    189       1.1      fvdl agp_i810_attach(struct device *parent, struct device *self, void *aux)
    190       1.1      fvdl {
    191       1.1      fvdl 	struct agp_softc *sc = (void *)self;
    192       1.1      fvdl 	struct agp_i810_softc *isc;
    193       1.1      fvdl 	struct agp_gatt *gatt;
    194  1.26.2.1      yamt 	int error, apbase;
    195  1.26.2.3      yamt 	bus_size_t mmadrsize;
    196       1.1      fvdl 
    197      1.10   tsutsui 	isc = malloc(sizeof *isc, M_AGP, M_NOWAIT|M_ZERO);
    198       1.1      fvdl 	if (isc == NULL) {
    199      1.15   thorpej 		aprint_error(": can't allocate chipset-specific softc\n");
    200       1.1      fvdl 		return ENOMEM;
    201       1.1      fvdl 	}
    202       1.1      fvdl 	sc->as_chipc = isc;
    203       1.1      fvdl 	sc->as_methods = &agp_i810_methods;
    204       1.1      fvdl 
    205       1.1      fvdl 	if (pci_find_device(&isc->vga_pa, agp_i810_vgamatch) == 0) {
    206      1.20      tron #if NAGP_INTEL > 0
    207      1.19      tron 		const struct pci_attach_args *pa = aux;
    208      1.19      tron 
    209      1.19      tron 		switch (PCI_PRODUCT(pa->pa_id)) {
    210      1.19      tron 		case PCI_PRODUCT_INTEL_82840_HB:
    211      1.19      tron 		case PCI_PRODUCT_INTEL_82865_HB:
    212      1.21      tron 		case PCI_PRODUCT_INTEL_82845G_DRAM:
    213      1.23   xtraeme 		case PCI_PRODUCT_INTEL_82815_FULL_HUB:
    214      1.19      tron 			return agp_intel_attach(parent, self, aux);
    215      1.20      tron 		}
    216      1.20      tron #endif
    217      1.15   thorpej 		aprint_error(": can't find internal VGA device config space\n");
    218       1.1      fvdl 		free(isc, M_AGP);
    219       1.1      fvdl 		return ENOENT;
    220       1.1      fvdl 	}
    221       1.1      fvdl 
    222       1.1      fvdl 	/* XXXfvdl */
    223       1.1      fvdl 	sc->as_dmat = isc->vga_pa.pa_dmat;
    224       1.1      fvdl 
    225      1.14       scw 	switch (PCI_PRODUCT(isc->vga_pa.pa_id)) {
    226      1.14       scw 	case PCI_PRODUCT_INTEL_82810_GC:
    227      1.14       scw 	case PCI_PRODUCT_INTEL_82810_DC100_GC:
    228      1.14       scw 	case PCI_PRODUCT_INTEL_82810E_GC:
    229      1.14       scw 	case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
    230      1.14       scw 		isc->chiptype = CHIP_I810;
    231      1.14       scw 		break;
    232      1.14       scw 	case PCI_PRODUCT_INTEL_82830MP_IV:
    233      1.14       scw 	case PCI_PRODUCT_INTEL_82845G_IGD:
    234      1.14       scw 		isc->chiptype = CHIP_I830;
    235      1.14       scw 		break;
    236      1.17   hannken 	case PCI_PRODUCT_INTEL_82855GM_IGD:
    237      1.18      tron 	case PCI_PRODUCT_INTEL_82865_IGD:
    238      1.17   hannken 		isc->chiptype = CHIP_I855;
    239      1.17   hannken 		break;
    240  1.26.2.1      yamt 	case PCI_PRODUCT_INTEL_82915G_IGD:
    241  1.26.2.1      yamt 	case PCI_PRODUCT_INTEL_82915GM_IGD:
    242  1.26.2.2      yamt 	case PCI_PRODUCT_INTEL_82945P_IGD:
    243  1.26.2.2      yamt 	case PCI_PRODUCT_INTEL_82945GM_IGD:
    244  1.26.2.2      yamt 	case PCI_PRODUCT_INTEL_82945GM_IGD_1:
    245  1.26.2.1      yamt 		isc->chiptype = CHIP_I915;
    246  1.26.2.1      yamt 		break;
    247  1.26.2.3      yamt 	case PCI_PRODUCT_INTEL_82965Q_IGD:
    248  1.26.2.3      yamt 	case PCI_PRODUCT_INTEL_82965Q_IGD_1:
    249  1.26.2.5      yamt 	case PCI_PRODUCT_INTEL_82965PM_IGD:
    250  1.26.2.5      yamt 	case PCI_PRODUCT_INTEL_82965PM_IGD_1:
    251  1.26.2.5      yamt 	case PCI_PRODUCT_INTEL_82965G_IGD:
    252  1.26.2.5      yamt 	case PCI_PRODUCT_INTEL_82965G_IGD_1:
    253  1.26.2.3      yamt 		isc->chiptype = CHIP_I965;
    254  1.26.2.3      yamt 		break;
    255  1.26.2.6      yamt 	case PCI_PRODUCT_INTEL_82Q35_IGD:
    256  1.26.2.6      yamt 	case PCI_PRODUCT_INTEL_82Q35_IGD_1:
    257  1.26.2.5      yamt 	case PCI_PRODUCT_INTEL_82G33_IGD:
    258  1.26.2.5      yamt 	case PCI_PRODUCT_INTEL_82G33_IGD_1:
    259  1.26.2.6      yamt 	case PCI_PRODUCT_INTEL_82Q33_IGD:
    260  1.26.2.6      yamt 	case PCI_PRODUCT_INTEL_82Q33_IGD_1:
    261  1.26.2.5      yamt 		isc->chiptype = CHIP_G33;
    262  1.26.2.5      yamt 		break;
    263      1.14       scw 	}
    264      1.14       scw 
    265  1.26.2.5      yamt 	switch (isc->chiptype) {
    266  1.26.2.5      yamt 	case CHIP_I915:
    267  1.26.2.5      yamt 	case CHIP_G33:
    268  1.26.2.5      yamt 		apbase = AGP_I915_GMADR;
    269  1.26.2.5      yamt 		break;
    270  1.26.2.5      yamt 	default:
    271  1.26.2.5      yamt 		apbase = AGP_I810_GMADR;
    272  1.26.2.5      yamt 		break;
    273  1.26.2.5      yamt 	}
    274  1.26.2.3      yamt 	if (isc->chiptype == CHIP_I965) {
    275  1.26.2.3      yamt 		error = agp_i965_map_aperture(&isc->vga_pa, sc, AGP_I965_GMADR);
    276  1.26.2.3      yamt 	} else {
    277  1.26.2.3      yamt 		error = agp_map_aperture(&isc->vga_pa, sc, apbase);
    278  1.26.2.3      yamt 	}
    279       1.1      fvdl 	if (error != 0) {
    280  1.26.2.1      yamt 		aprint_error(": can't map aperture\n");
    281  1.26.2.1      yamt 		free(isc, M_AGP);
    282       1.1      fvdl 		return error;
    283       1.1      fvdl 	}
    284       1.1      fvdl 
    285  1.26.2.5      yamt 	if (isc->chiptype == CHIP_I915 || isc->chiptype == CHIP_G33) {
    286  1.26.2.1      yamt 		error = pci_mapreg_map(&isc->vga_pa, AGP_I915_MMADR,
    287  1.26.2.3      yamt 		    PCI_MAPREG_TYPE_MEM, 0, &isc->bst, &isc->bsh,
    288  1.26.2.3      yamt 		    NULL, &mmadrsize);
    289  1.26.2.1      yamt 		if (error != 0) {
    290  1.26.2.1      yamt 			aprint_error(": can't map mmadr registers\n");
    291  1.26.2.1      yamt 			agp_generic_detach(sc);
    292  1.26.2.1      yamt 			return error;
    293  1.26.2.1      yamt 		}
    294  1.26.2.1      yamt 		error = pci_mapreg_map(&isc->vga_pa, AGP_I915_GTTADR,
    295  1.26.2.1      yamt 		    PCI_MAPREG_TYPE_MEM, 0, &isc->gtt_bst, &isc->gtt_bsh,
    296  1.26.2.1      yamt 		    NULL, NULL);
    297  1.26.2.1      yamt 		if (error != 0) {
    298  1.26.2.1      yamt 			aprint_error(": can't map gttadr registers\n");
    299  1.26.2.1      yamt 			/* XXX we should release mmadr here */
    300  1.26.2.1      yamt 			agp_generic_detach(sc);
    301  1.26.2.1      yamt 			return error;
    302  1.26.2.1      yamt 		}
    303  1.26.2.3      yamt 	} else if (isc->chiptype == CHIP_I965) {
    304  1.26.2.3      yamt 		error = pci_mapreg_map(&isc->vga_pa, AGP_I965_MMADR,
    305  1.26.2.3      yamt 		    PCI_MAPREG_TYPE_MEM, 0, &isc->bst, &isc->bsh,
    306  1.26.2.3      yamt 		    NULL, &mmadrsize);
    307  1.26.2.3      yamt 		if (error != 0) {
    308  1.26.2.3      yamt 			aprint_error(": can't map mmadr registers\n");
    309  1.26.2.3      yamt 			agp_generic_detach(sc);
    310  1.26.2.3      yamt 			return error;
    311  1.26.2.3      yamt 		}
    312  1.26.2.1      yamt 	} else {
    313  1.26.2.1      yamt 		error = pci_mapreg_map(&isc->vga_pa, AGP_I810_MMADR,
    314  1.26.2.3      yamt 		    PCI_MAPREG_TYPE_MEM, 0, &isc->bst, &isc->bsh,
    315  1.26.2.3      yamt 		    NULL, &mmadrsize);
    316  1.26.2.1      yamt 		if (error != 0) {
    317  1.26.2.1      yamt 			aprint_error(": can't map mmadr registers\n");
    318  1.26.2.1      yamt 			agp_generic_detach(sc);
    319  1.26.2.1      yamt 			return error;
    320  1.26.2.1      yamt 		}
    321  1.26.2.1      yamt 	}
    322  1.26.2.1      yamt 
    323       1.1      fvdl 	isc->initial_aperture = AGP_GET_APERTURE(sc);
    324       1.1      fvdl 
    325      1.14       scw 	gatt = malloc(sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
    326      1.14       scw 	if (!gatt) {
    327      1.14       scw  		agp_generic_detach(sc);
    328      1.14       scw  		return ENOMEM;
    329      1.14       scw 	}
    330      1.14       scw 	isc->gatt = gatt;
    331      1.14       scw 
    332      1.14       scw 	gatt->ag_entries = AGP_GET_APERTURE(sc) >> AGP_PAGE_SHIFT;
    333       1.1      fvdl 
    334  1.26.2.7      yamt 	if (!pmf_device_register(self, NULL, agp_i810_resume))
    335  1.26.2.7      yamt 		aprint_error_dev(self, "couldn't establish power handler\n");
    336  1.26.2.7      yamt 
    337  1.26.2.5      yamt 	return agp_i810_init(sc);
    338  1.26.2.5      yamt }
    339  1.26.2.5      yamt 
    340  1.26.2.5      yamt static int agp_i810_init(struct agp_softc *sc)
    341  1.26.2.5      yamt {
    342  1.26.2.5      yamt 	struct agp_i810_softc *isc;
    343  1.26.2.5      yamt 	struct agp_gatt *gatt;
    344  1.26.2.5      yamt 
    345  1.26.2.5      yamt 	isc = sc->as_chipc;
    346  1.26.2.5      yamt 	gatt = isc->gatt;
    347  1.26.2.5      yamt 
    348      1.14       scw 	if (isc->chiptype == CHIP_I810) {
    349  1.26.2.3      yamt 		void *virtual;
    350      1.14       scw 		int dummyseg;
    351  1.26.2.2      yamt 
    352      1.14       scw 		/* Some i810s have on-chip memory called dcache */
    353      1.14       scw 		if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
    354      1.14       scw 			isc->dcache_size = 4 * 1024 * 1024;
    355      1.14       scw 		else
    356      1.14       scw 			isc->dcache_size = 0;
    357      1.14       scw 
    358      1.14       scw 		/* According to the specs the gatt on the i810 must be 64k */
    359      1.14       scw 		if (agp_alloc_dmamem(sc->as_dmat, 64 * 1024,
    360  1.26.2.2      yamt 		    0, &gatt->ag_dmamap, &virtual, &gatt->ag_physical,
    361  1.26.2.2      yamt 		    &gatt->ag_dmaseg, 1, &dummyseg) != 0) {
    362      1.14       scw 			free(gatt, M_AGP);
    363       1.1      fvdl 			agp_generic_detach(sc);
    364       1.1      fvdl 			return ENOMEM;
    365       1.1      fvdl 		}
    366  1.26.2.2      yamt 		gatt->ag_virtual = (uint32_t *)virtual;
    367      1.14       scw 		gatt->ag_size = gatt->ag_entries * sizeof(u_int32_t);
    368      1.14       scw 		memset(gatt->ag_virtual, 0, gatt->ag_size);
    369      1.25     perry 
    370      1.14       scw 		agp_flush_cache();
    371      1.14       scw 		/* Install the GATT. */
    372      1.14       scw 		WRITE4(AGP_I810_PGTBL_CTL, gatt->ag_physical | 1);
    373      1.17   hannken 	} else if (isc->chiptype == CHIP_I830) {
    374      1.14       scw 		/* The i830 automatically initializes the 128k gatt on boot. */
    375      1.14       scw 		pcireg_t reg;
    376      1.14       scw 		u_int32_t pgtblctl;
    377      1.14       scw 		u_int16_t gcc1;
    378      1.14       scw 
    379      1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
    380      1.14       scw 		gcc1 = (u_int16_t)(reg >> 16);
    381      1.14       scw 		switch (gcc1 & AGP_I830_GCC1_GMS) {
    382      1.14       scw 		case AGP_I830_GCC1_GMS_STOLEN_512:
    383      1.14       scw 			isc->stolen = (512 - 132) * 1024 / 4096;
    384      1.14       scw 			break;
    385      1.25     perry 		case AGP_I830_GCC1_GMS_STOLEN_1024:
    386      1.14       scw 			isc->stolen = (1024 - 132) * 1024 / 4096;
    387      1.14       scw 			break;
    388      1.25     perry 		case AGP_I830_GCC1_GMS_STOLEN_8192:
    389      1.14       scw 			isc->stolen = (8192 - 132) * 1024 / 4096;
    390      1.14       scw 			break;
    391      1.14       scw 		default:
    392      1.14       scw 			isc->stolen = 0;
    393      1.15   thorpej 			aprint_error(
    394      1.15   thorpej 			    ": unknown memory configuration, disabling\n");
    395      1.14       scw 			agp_generic_detach(sc);
    396      1.14       scw 			return EINVAL;
    397      1.14       scw 		}
    398  1.26.2.5      yamt 
    399      1.14       scw 		if (isc->stolen > 0) {
    400      1.17   hannken 			aprint_error(": detected %dk stolen memory\n%s",
    401      1.17   hannken 			    isc->stolen * 4, sc->as_dev.dv_xname);
    402      1.14       scw 		}
    403      1.17   hannken 
    404      1.17   hannken 		/* GATT address is already in there, make sure it's enabled */
    405      1.17   hannken 		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
    406      1.17   hannken 		pgtblctl |= 1;
    407      1.17   hannken 		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
    408      1.17   hannken 
    409      1.17   hannken 		gatt->ag_physical = pgtblctl & ~1;
    410  1.26.2.3      yamt 	} else if (isc->chiptype == CHIP_I855 || isc->chiptype == CHIP_I915 ||
    411  1.26.2.5      yamt 		   isc->chiptype == CHIP_I965 || isc->chiptype == CHIP_G33) {
    412      1.17   hannken 		pcireg_t reg;
    413  1.26.2.3      yamt 		u_int32_t pgtblctl, stolen;
    414      1.17   hannken 		u_int16_t gcc1;
    415      1.17   hannken 
    416  1.26.2.5      yamt 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I855_GCC1);
    417  1.26.2.5      yamt 		gcc1 = (u_int16_t)(reg >> 16);
    418  1.26.2.5      yamt 
    419  1.26.2.3      yamt 		/* Stolen memory is set up at the beginning of the aperture by
    420  1.26.2.3      yamt                  * the BIOS, consisting of the GATT followed by 4kb for the
    421  1.26.2.3      yamt 		 * BIOS display.
    422  1.26.2.3      yamt                  */
    423  1.26.2.3      yamt                 switch (isc->chiptype) {
    424  1.26.2.3      yamt 		case CHIP_I855:
    425  1.26.2.3      yamt 			stolen = 128 + 4;
    426  1.26.2.3      yamt 			break;
    427  1.26.2.3      yamt                 case CHIP_I915:
    428  1.26.2.3      yamt 			stolen = 256 + 4;
    429  1.26.2.3      yamt 			break;
    430  1.26.2.3      yamt 		case CHIP_I965:
    431  1.26.2.3      yamt 			stolen = 512 + 4;
    432  1.26.2.3      yamt 			break;
    433  1.26.2.5      yamt 		case CHIP_G33:
    434  1.26.2.5      yamt 			switch (gcc1 & AGP_G33_PGTBL_SIZE_MASK) {
    435  1.26.2.5      yamt 			case AGP_G33_PGTBL_SIZE_1M:
    436  1.26.2.5      yamt 				stolen = 1024 + 4;
    437  1.26.2.5      yamt 				break;
    438  1.26.2.5      yamt 			case AGP_G33_PGTBL_SIZE_2M:
    439  1.26.2.5      yamt 				stolen = 2048 + 4;
    440  1.26.2.5      yamt 				break;
    441  1.26.2.5      yamt 			default:
    442  1.26.2.5      yamt 				aprint_error(": bad gtt size\n");
    443  1.26.2.5      yamt 				agp_generic_detach(sc);
    444  1.26.2.5      yamt 				return EINVAL;
    445  1.26.2.5      yamt 			}
    446  1.26.2.5      yamt 			break;
    447  1.26.2.3      yamt 		default:
    448  1.26.2.3      yamt 			aprint_error(": bad chiptype\n");
    449  1.26.2.3      yamt 			agp_generic_detach(sc);
    450  1.26.2.3      yamt 			return EINVAL;
    451  1.26.2.3      yamt                }
    452  1.26.2.3      yamt 
    453      1.17   hannken 		switch (gcc1 & AGP_I855_GCC1_GMS) {
    454      1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_1M:
    455  1.26.2.3      yamt 			isc->stolen = (1024 - stolen) * 1024 / 4096;
    456      1.17   hannken 			break;
    457      1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_4M:
    458  1.26.2.3      yamt 			isc->stolen = (4096 - stolen) * 1024 / 4096;
    459      1.17   hannken 			break;
    460      1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_8M:
    461  1.26.2.3      yamt 			isc->stolen = (8192 - stolen) * 1024 / 4096;
    462      1.17   hannken 			break;
    463      1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_16M:
    464  1.26.2.3      yamt 			isc->stolen = (16384 - stolen) * 1024 / 4096;
    465      1.17   hannken 			break;
    466      1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_32M:
    467  1.26.2.3      yamt 			isc->stolen = (32768 - stolen) * 1024 / 4096;
    468  1.26.2.1      yamt 			break;
    469  1.26.2.3      yamt 		case AGP_I915_GCC1_GMS_STOLEN_48M:
    470  1.26.2.3      yamt 			isc->stolen = (49152 - stolen) * 1024 / 4096;
    471  1.26.2.1      yamt 			break;
    472  1.26.2.3      yamt 		case AGP_I915_GCC1_GMS_STOLEN_64M:
    473  1.26.2.3      yamt 			isc->stolen = (65536 - stolen) * 1024 / 4096;
    474  1.26.2.1      yamt 			break;
    475  1.26.2.6      yamt 		case AGP_G33_GCC1_GMS_STOLEN_128M:
    476  1.26.2.6      yamt 			isc->stolen = ((128 * 1024) - stolen) * 1024 / 4096;
    477  1.26.2.6      yamt 			break;
    478  1.26.2.6      yamt 		case AGP_G33_GCC1_GMS_STOLEN_256M:
    479  1.26.2.6      yamt 			isc->stolen = ((256 * 1024) - stolen) * 1024 / 4096;
    480  1.26.2.6      yamt 			break;
    481  1.26.2.1      yamt 		default:
    482  1.26.2.1      yamt 			isc->stolen = 0;
    483  1.26.2.1      yamt 			aprint_error(
    484  1.26.2.1      yamt 			    ": unknown memory configuration, disabling\n");
    485  1.26.2.1      yamt 			agp_generic_detach(sc);
    486  1.26.2.1      yamt 			return EINVAL;
    487  1.26.2.1      yamt 		}
    488  1.26.2.1      yamt 		if (isc->stolen > 0) {
    489  1.26.2.1      yamt 			aprint_error(": detected %dk stolen memory\n%s",
    490  1.26.2.1      yamt 			    isc->stolen * 4, sc->as_dev.dv_xname);
    491  1.26.2.1      yamt 		}
    492  1.26.2.1      yamt 
    493  1.26.2.1      yamt 		/* GATT address is already in there, make sure it's enabled */
    494  1.26.2.1      yamt 		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
    495  1.26.2.1      yamt 		pgtblctl |= 1;
    496  1.26.2.1      yamt 		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
    497  1.26.2.1      yamt 
    498  1.26.2.1      yamt 		gatt->ag_physical = pgtblctl & ~1;
    499       1.1      fvdl 	}
    500       1.1      fvdl 
    501       1.1      fvdl 	/*
    502       1.1      fvdl 	 * Make sure the chipset can see everything.
    503       1.1      fvdl 	 */
    504       1.1      fvdl 	agp_flush_cache();
    505      1.14       scw 
    506  1.26.2.3      yamt #if 0
    507  1.26.2.3      yamt 	/*
    508  1.26.2.3      yamt 	 * another device (drm) may need access to this region
    509  1.26.2.3      yamt 	 * we do not need it anymore
    510  1.26.2.3      yamt 	 */
    511  1.26.2.3      yamt 	bus_space_unmap(isc->bst, isc->bsh, mmadrsize);
    512  1.26.2.3      yamt #endif
    513  1.26.2.3      yamt 
    514       1.1      fvdl 	return 0;
    515       1.1      fvdl }
    516       1.1      fvdl 
    517       1.1      fvdl #if 0
    518       1.1      fvdl static int
    519       1.1      fvdl agp_i810_detach(struct agp_softc *sc)
    520       1.1      fvdl {
    521       1.1      fvdl 	int error;
    522       1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    523       1.1      fvdl 
    524       1.1      fvdl 	error = agp_generic_detach(sc);
    525       1.1      fvdl 	if (error)
    526       1.1      fvdl 		return error;
    527       1.1      fvdl 
    528       1.1      fvdl 	/* Clear the GATT base. */
    529      1.14       scw 	if (sc->chiptype == CHIP_I810) {
    530      1.14       scw 		WRITE4(AGP_I810_PGTBL_CTL, 0);
    531      1.14       scw 	} else {
    532      1.14       scw 		unsigned int pgtblctl;
    533      1.14       scw 		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
    534      1.14       scw 		pgtblctl &= ~1;
    535      1.14       scw 		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
    536      1.14       scw 	}
    537       1.1      fvdl 
    538       1.1      fvdl 	/* Put the aperture back the way it started. */
    539       1.1      fvdl 	AGP_SET_APERTURE(sc, isc->initial_aperture);
    540       1.1      fvdl 
    541      1.14       scw 	if (sc->chiptype == CHIP_I810) {
    542      1.14       scw 		agp_free_dmamem(sc->as_dmat, gatt->ag_size, gatt->ag_dmamap,
    543  1.26.2.3      yamt 		    (void *)gatt->ag_virtual, &gatt->ag_dmaseg, 1);
    544      1.14       scw 	}
    545      1.14       scw 	free(sc->gatt, M_AGP);
    546       1.1      fvdl 
    547       1.1      fvdl 	return 0;
    548       1.1      fvdl }
    549       1.1      fvdl #endif
    550       1.1      fvdl 
    551       1.1      fvdl static u_int32_t
    552       1.1      fvdl agp_i810_get_aperture(struct agp_softc *sc)
    553       1.1      fvdl {
    554      1.14       scw 	struct agp_i810_softc *isc = sc->as_chipc;
    555      1.14       scw 	pcireg_t reg;
    556  1.26.2.3      yamt 	u_int16_t miscc, gcc1, msac;
    557      1.14       scw 
    558  1.26.2.3      yamt 	switch (isc->chiptype) {
    559  1.26.2.3      yamt 	case CHIP_I810:
    560      1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
    561      1.14       scw 		miscc = (u_int16_t)(reg >> 16);
    562      1.14       scw 		if ((miscc & AGP_I810_MISCC_WINSIZE) ==
    563      1.14       scw 		    AGP_I810_MISCC_WINSIZE_32)
    564      1.14       scw 			return 32 * 1024 * 1024;
    565      1.14       scw 		else
    566      1.14       scw 			return 64 * 1024 * 1024;
    567  1.26.2.3      yamt 	case CHIP_I830:
    568      1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
    569      1.14       scw 		gcc1 = (u_int16_t)(reg >> 16);
    570      1.14       scw 		if ((gcc1 & AGP_I830_GCC1_GMASIZE) == AGP_I830_GCC1_GMASIZE_64)
    571      1.14       scw 			return 64 * 1024 * 1024;
    572      1.14       scw 		else
    573      1.14       scw 			return 128 * 1024 * 1024;
    574  1.26.2.3      yamt 	case CHIP_I855:
    575      1.17   hannken 		return 128 * 1024 * 1024;
    576  1.26.2.3      yamt 	case CHIP_I915:
    577  1.26.2.5      yamt 	case CHIP_G33:
    578  1.26.2.1      yamt 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I915_MSAC);
    579  1.26.2.1      yamt 		msac = (u_int16_t)(reg >> 16);
    580  1.26.2.1      yamt 		if (msac & AGP_I915_MSAC_APER_128M)
    581  1.26.2.1      yamt 			return 128 * 1024 * 1024;
    582  1.26.2.1      yamt 		else
    583  1.26.2.1      yamt 			return 256 * 1024 * 1024;
    584  1.26.2.3      yamt 	case CHIP_I965:
    585  1.26.2.3      yamt 		return 512 * 1024 * 1024;
    586  1.26.2.3      yamt 	default:
    587  1.26.2.3      yamt 		aprint_error(": Unknown chipset\n");
    588      1.14       scw 	}
    589  1.26.2.3      yamt 
    590  1.26.2.3      yamt 	return 0;
    591       1.1      fvdl }
    592       1.1      fvdl 
    593       1.1      fvdl static int
    594       1.1      fvdl agp_i810_set_aperture(struct agp_softc *sc, u_int32_t aperture)
    595       1.1      fvdl {
    596      1.14       scw 	struct agp_i810_softc *isc = sc->as_chipc;
    597      1.14       scw 	pcireg_t reg;
    598  1.26.2.3      yamt 	u_int16_t miscc, gcc1;
    599      1.14       scw 
    600  1.26.2.3      yamt 	switch (isc->chiptype) {
    601  1.26.2.3      yamt 	case CHIP_I810:
    602      1.14       scw 		/*
    603      1.14       scw 		 * Double check for sanity.
    604      1.14       scw 		 */
    605      1.14       scw 		if (aperture != (32 * 1024 * 1024) &&
    606      1.14       scw 		    aperture != (64 * 1024 * 1024)) {
    607      1.14       scw 			printf("%s: bad aperture size %d\n",
    608      1.14       scw 			    sc->as_dev.dv_xname, aperture);
    609      1.14       scw 			return EINVAL;
    610      1.14       scw 		}
    611       1.1      fvdl 
    612      1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
    613      1.14       scw 		miscc = (u_int16_t)(reg >> 16);
    614      1.14       scw 		miscc &= ~AGP_I810_MISCC_WINSIZE;
    615      1.14       scw 		if (aperture == 32 * 1024 * 1024)
    616      1.14       scw 			miscc |= AGP_I810_MISCC_WINSIZE_32;
    617      1.14       scw 		else
    618      1.14       scw 			miscc |= AGP_I810_MISCC_WINSIZE_64;
    619      1.14       scw 
    620      1.14       scw 		reg &= 0x0000ffff;
    621      1.14       scw 		reg |= ((pcireg_t)miscc) << 16;
    622      1.14       scw 		pci_conf_write(sc->as_pc, sc->as_tag, AGP_I810_SMRAM, reg);
    623  1.26.2.3      yamt 		break;
    624  1.26.2.3      yamt 	case CHIP_I830:
    625      1.14       scw 		if (aperture != (64 * 1024 * 1024) &&
    626      1.14       scw 		    aperture != (128 * 1024 * 1024)) {
    627      1.14       scw 			printf("%s: bad aperture size %d\n",
    628      1.14       scw 			    sc->as_dev.dv_xname, aperture);
    629      1.14       scw 			return EINVAL;
    630      1.14       scw 		}
    631      1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
    632      1.14       scw 		gcc1 = (u_int16_t)(reg >> 16);
    633      1.14       scw 		gcc1 &= ~AGP_I830_GCC1_GMASIZE;
    634      1.14       scw 		if (aperture == 64 * 1024 * 1024)
    635      1.14       scw 			gcc1 |= AGP_I830_GCC1_GMASIZE_64;
    636      1.14       scw 		else
    637      1.14       scw 			gcc1 |= AGP_I830_GCC1_GMASIZE_128;
    638      1.14       scw 
    639      1.14       scw 		reg &= 0x0000ffff;
    640      1.14       scw 		reg |= ((pcireg_t)gcc1) << 16;
    641      1.14       scw 		pci_conf_write(sc->as_pc, sc->as_tag, AGP_I830_GCC0, reg);
    642  1.26.2.3      yamt 		break;
    643  1.26.2.3      yamt 	case CHIP_I855:
    644  1.26.2.3      yamt 	case CHIP_I915:
    645  1.26.2.1      yamt 		if (aperture != agp_i810_get_aperture(sc)) {
    646      1.17   hannken 			printf("%s: bad aperture size %d\n",
    647      1.17   hannken 			    sc->as_dev.dv_xname, aperture);
    648      1.17   hannken 			return EINVAL;
    649      1.17   hannken 		}
    650  1.26.2.3      yamt 		break;
    651  1.26.2.3      yamt 	case CHIP_I965:
    652  1.26.2.3      yamt 		if (aperture != 512 * 1024 * 1024) {
    653  1.26.2.3      yamt 			printf("%s: bad aperture size %d\n",
    654  1.26.2.3      yamt 			    sc->as_dev.dv_xname, aperture);
    655  1.26.2.3      yamt 			return EINVAL;
    656  1.26.2.3      yamt 		}
    657  1.26.2.3      yamt 		break;
    658       1.1      fvdl 	}
    659       1.1      fvdl 
    660       1.1      fvdl 	return 0;
    661       1.1      fvdl }
    662       1.1      fvdl 
    663       1.1      fvdl static int
    664       1.1      fvdl agp_i810_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
    665       1.1      fvdl {
    666       1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    667       1.1      fvdl 
    668      1.14       scw 	if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
    669  1.26.2.1      yamt #ifdef AGP_DEBUG
    670      1.14       scw 		printf("%s: failed: offset 0x%08x, shift %d, entries %d\n",
    671      1.14       scw 		    sc->as_dev.dv_xname, (int)offset, AGP_PAGE_SHIFT,
    672      1.14       scw 		    isc->gatt->ag_entries);
    673      1.14       scw #endif
    674       1.1      fvdl 		return EINVAL;
    675      1.14       scw 	}
    676      1.14       scw 
    677      1.17   hannken 	if (isc->chiptype != CHIP_I830) {
    678      1.14       scw 		if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
    679  1.26.2.1      yamt #ifdef AGP_DEBUG
    680      1.14       scw 			printf("%s: trying to bind into stolen memory",
    681      1.14       scw 			    sc->as_dev.dv_xname);
    682      1.14       scw #endif
    683      1.14       scw 			return EINVAL;
    684      1.14       scw 		}
    685      1.14       scw 	}
    686       1.1      fvdl 
    687  1.26.2.1      yamt 	WRITEGTT(offset, physical | 1);
    688       1.1      fvdl 	return 0;
    689       1.1      fvdl }
    690       1.1      fvdl 
    691       1.1      fvdl static int
    692       1.1      fvdl agp_i810_unbind_page(struct agp_softc *sc, off_t offset)
    693       1.1      fvdl {
    694       1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    695       1.1      fvdl 
    696       1.1      fvdl 	if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
    697       1.1      fvdl 		return EINVAL;
    698       1.1      fvdl 
    699      1.17   hannken 	if (isc->chiptype != CHIP_I810 ) {
    700      1.14       scw 		if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
    701  1.26.2.1      yamt #ifdef AGP_DEBUG
    702      1.14       scw 			printf("%s: trying to unbind from stolen memory",
    703      1.14       scw 			    sc->as_dev.dv_xname);
    704      1.14       scw #endif
    705      1.14       scw 			return EINVAL;
    706      1.14       scw 		}
    707      1.14       scw 	}
    708      1.14       scw 
    709  1.26.2.1      yamt 	WRITEGTT(offset, 0);
    710       1.1      fvdl 	return 0;
    711       1.1      fvdl }
    712       1.1      fvdl 
    713       1.1      fvdl /*
    714       1.1      fvdl  * Writing via memory mapped registers already flushes all TLBs.
    715       1.1      fvdl  */
    716       1.1      fvdl static void
    717       1.1      fvdl agp_i810_flush_tlb(struct agp_softc *sc)
    718       1.1      fvdl {
    719       1.1      fvdl }
    720       1.1      fvdl 
    721       1.1      fvdl static int
    722       1.1      fvdl agp_i810_enable(struct agp_softc *sc, u_int32_t mode)
    723       1.1      fvdl {
    724       1.1      fvdl 
    725       1.1      fvdl 	return 0;
    726       1.1      fvdl }
    727       1.1      fvdl 
    728       1.1      fvdl static struct agp_memory *
    729       1.1      fvdl agp_i810_alloc_memory(struct agp_softc *sc, int type, vsize_t size)
    730       1.1      fvdl {
    731       1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    732       1.1      fvdl 	struct agp_memory *mem;
    733       1.1      fvdl 
    734  1.26.2.1      yamt #ifdef AGP_DEBUG
    735  1.26.2.1      yamt 	printf("AGP: alloc(%d, 0x%x)\n", type, (int) size);
    736  1.26.2.1      yamt #endif
    737  1.26.2.1      yamt 
    738       1.1      fvdl 	if ((size & (AGP_PAGE_SIZE - 1)) != 0)
    739       1.1      fvdl 		return 0;
    740       1.1      fvdl 
    741       1.1      fvdl 	if (sc->as_allocated + size > sc->as_maxmem)
    742       1.1      fvdl 		return 0;
    743       1.1      fvdl 
    744       1.1      fvdl 	if (type == 1) {
    745       1.1      fvdl 		/*
    746       1.1      fvdl 		 * Mapping local DRAM into GATT.
    747       1.1      fvdl 		 */
    748      1.17   hannken 		if (isc->chiptype != CHIP_I810 )
    749      1.14       scw 			return 0;
    750       1.1      fvdl 		if (size != isc->dcache_size)
    751       1.1      fvdl 			return 0;
    752       1.1      fvdl 	} else if (type == 2) {
    753       1.1      fvdl 		/*
    754  1.26.2.1      yamt 		 * Bogus mapping for the hardware cursor.
    755       1.1      fvdl 		 */
    756  1.26.2.1      yamt 		if (size != AGP_PAGE_SIZE && size != 4 * AGP_PAGE_SIZE)
    757       1.1      fvdl 			return 0;
    758       1.1      fvdl 	}
    759       1.1      fvdl 
    760      1.10   tsutsui 	mem = malloc(sizeof *mem, M_AGP, M_WAITOK|M_ZERO);
    761       1.1      fvdl 	if (mem == NULL)
    762       1.1      fvdl 		return NULL;
    763       1.1      fvdl 	mem->am_id = sc->as_nextid++;
    764       1.1      fvdl 	mem->am_size = size;
    765       1.1      fvdl 	mem->am_type = type;
    766       1.1      fvdl 
    767       1.1      fvdl 	if (type == 2) {
    768       1.1      fvdl 		/*
    769  1.26.2.1      yamt 		 * Allocate and wire down the memory now so that we can
    770       1.1      fvdl 		 * get its physical address.
    771       1.1      fvdl 		 */
    772       1.1      fvdl 		mem->am_dmaseg = malloc(sizeof *mem->am_dmaseg, M_AGP,
    773       1.1      fvdl 		    M_WAITOK);
    774       1.1      fvdl 		if (mem->am_dmaseg == NULL) {
    775       1.1      fvdl 			free(mem, M_AGP);
    776       1.1      fvdl 			return NULL;
    777       1.1      fvdl 		}
    778       1.1      fvdl 		if (agp_alloc_dmamem(sc->as_dmat, size, 0,
    779       1.1      fvdl 		    &mem->am_dmamap, &mem->am_virtual, &mem->am_physical,
    780       1.1      fvdl 		    mem->am_dmaseg, 1, &mem->am_nseg) != 0) {
    781       1.1      fvdl 			free(mem->am_dmaseg, M_AGP);
    782       1.1      fvdl 			free(mem, M_AGP);
    783       1.1      fvdl 			return NULL;
    784       1.1      fvdl 		}
    785  1.26.2.1      yamt 		memset(mem->am_virtual, 0, size);
    786       1.1      fvdl 	} else if (type != 1) {
    787       1.4  drochner 		if (bus_dmamap_create(sc->as_dmat, size, size / PAGE_SIZE + 1,
    788       1.4  drochner 				      size, 0, BUS_DMA_NOWAIT,
    789       1.4  drochner 				      &mem->am_dmamap) != 0) {
    790       1.1      fvdl 			free(mem, M_AGP);
    791       1.1      fvdl 			return NULL;
    792       1.1      fvdl 		}
    793       1.1      fvdl 	}
    794       1.1      fvdl 
    795       1.1      fvdl 	TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
    796       1.1      fvdl 	sc->as_allocated += size;
    797       1.1      fvdl 
    798       1.1      fvdl 	return mem;
    799       1.1      fvdl }
    800       1.1      fvdl 
    801       1.1      fvdl static int
    802       1.1      fvdl agp_i810_free_memory(struct agp_softc *sc, struct agp_memory *mem)
    803       1.1      fvdl {
    804       1.1      fvdl 	if (mem->am_is_bound)
    805       1.1      fvdl 		return EBUSY;
    806       1.1      fvdl 
    807       1.1      fvdl 	if (mem->am_type == 2) {
    808       1.1      fvdl 		agp_free_dmamem(sc->as_dmat, mem->am_size, mem->am_dmamap,
    809       1.1      fvdl 		    mem->am_virtual, mem->am_dmaseg, mem->am_nseg);
    810       1.1      fvdl 		free(mem->am_dmaseg, M_AGP);
    811       1.1      fvdl 	}
    812       1.1      fvdl 
    813       1.1      fvdl 	sc->as_allocated -= mem->am_size;
    814       1.1      fvdl 	TAILQ_REMOVE(&sc->as_memory, mem, am_link);
    815       1.1      fvdl 	free(mem, M_AGP);
    816       1.1      fvdl 	return 0;
    817       1.1      fvdl }
    818       1.1      fvdl 
    819       1.1      fvdl static int
    820       1.1      fvdl agp_i810_bind_memory(struct agp_softc *sc, struct agp_memory *mem,
    821       1.1      fvdl 		     off_t offset)
    822       1.1      fvdl {
    823       1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    824       1.4  drochner 	u_int32_t regval, i;
    825       1.4  drochner 
    826       1.4  drochner 	/*
    827       1.4  drochner 	 * XXX evil hack: the PGTBL_CTL appearently gets overwritten by the
    828       1.4  drochner 	 * X server for mysterious reasons which leads to crashes if we write
    829       1.4  drochner 	 * to the GTT through the MMIO window.
    830       1.4  drochner 	 * Until the issue is solved, simply restore it.
    831       1.4  drochner 	 */
    832  1.26.2.3      yamt 
    833  1.26.2.3      yamt #if 0
    834       1.4  drochner 	regval = bus_space_read_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL);
    835       1.4  drochner 	if (regval != (isc->gatt->ag_physical | 1)) {
    836       1.4  drochner 		printf("agp_i810_bind_memory: PGTBL_CTL is 0x%x - fixing\n",
    837       1.4  drochner 		       regval);
    838       1.4  drochner 		bus_space_write_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL,
    839       1.4  drochner 				  isc->gatt->ag_physical | 1);
    840       1.4  drochner 	}
    841  1.26.2.3      yamt #endif
    842  1.26.2.3      yamt 	regval = 0;
    843       1.1      fvdl 
    844       1.5  drochner 	if (mem->am_type == 2) {
    845  1.26.2.1      yamt 		WRITEGTT(offset, mem->am_physical | 1);
    846       1.5  drochner 		mem->am_offset = offset;
    847       1.5  drochner 		mem->am_is_bound = 1;
    848       1.1      fvdl 		return 0;
    849       1.5  drochner 	}
    850       1.5  drochner 
    851       1.1      fvdl 	if (mem->am_type != 1)
    852       1.1      fvdl 		return agp_generic_bind_memory(sc, mem, offset);
    853       1.1      fvdl 
    854      1.17   hannken 	if (isc->chiptype != CHIP_I810)
    855      1.14       scw 		return EINVAL;
    856      1.14       scw 
    857  1.26.2.1      yamt 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
    858  1.26.2.1      yamt 		WRITEGTT(offset, i | 3);
    859      1.13  drochner 	mem->am_is_bound = 1;
    860       1.1      fvdl 	return 0;
    861       1.1      fvdl }
    862       1.1      fvdl 
    863       1.1      fvdl static int
    864       1.1      fvdl agp_i810_unbind_memory(struct agp_softc *sc, struct agp_memory *mem)
    865       1.1      fvdl {
    866       1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    867       1.1      fvdl 	u_int32_t i;
    868       1.1      fvdl 
    869       1.5  drochner 	if (mem->am_type == 2) {
    870  1.26.2.1      yamt 		WRITEGTT(mem->am_offset, 0);
    871       1.5  drochner 		mem->am_offset = 0;
    872       1.5  drochner 		mem->am_is_bound = 0;
    873       1.1      fvdl 		return 0;
    874       1.5  drochner 	}
    875       1.1      fvdl 
    876       1.1      fvdl 	if (mem->am_type != 1)
    877       1.1      fvdl 		return agp_generic_unbind_memory(sc, mem);
    878      1.14       scw 
    879      1.17   hannken 	if (isc->chiptype != CHIP_I810)
    880      1.14       scw 		return EINVAL;
    881       1.1      fvdl 
    882       1.1      fvdl 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
    883  1.26.2.1      yamt 		WRITEGTT(i, 0);
    884      1.13  drochner 	mem->am_is_bound = 0;
    885       1.1      fvdl 	return 0;
    886       1.1      fvdl }
    887      1.24  jmcneill 
    888  1.26.2.7      yamt static bool
    889  1.26.2.7      yamt agp_i810_resume(device_t dv)
    890      1.24  jmcneill {
    891  1.26.2.7      yamt 	struct agp_softc *sc = device_private(dv);
    892      1.24  jmcneill 	struct agp_i810_softc *isc = sc->as_chipc;
    893      1.24  jmcneill 
    894  1.26.2.7      yamt 	isc->pgtblctl = READ4(AGP_I810_PGTBL_CTL);
    895  1.26.2.7      yamt 	agp_flush_cache();
    896      1.24  jmcneill 
    897  1.26.2.7      yamt 	return true;
    898      1.24  jmcneill }
    899