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