Home | History | Annotate | Line # | Download | only in pci
agp_i810.c revision 1.112.2.4
      1  1.112.2.4       riz /*	$NetBSD: agp_i810.c,v 1.112.2.4 2015/11/06 22:55:10 riz Exp $	*/
      2        1.1      fvdl 
      3        1.1      fvdl /*-
      4        1.1      fvdl  * Copyright (c) 2000 Doug Rabson
      5        1.1      fvdl  * Copyright (c) 2000 Ruslan Ermilov
      6        1.1      fvdl  * All rights reserved.
      7        1.1      fvdl  *
      8        1.1      fvdl  * Redistribution and use in source and binary forms, with or without
      9        1.1      fvdl  * modification, are permitted provided that the following conditions
     10        1.1      fvdl  * are met:
     11        1.1      fvdl  * 1. Redistributions of source code must retain the above copyright
     12        1.1      fvdl  *    notice, this list of conditions and the following disclaimer.
     13        1.1      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     14        1.1      fvdl  *    notice, this list of conditions and the following disclaimer in the
     15        1.1      fvdl  *    documentation and/or other materials provided with the distribution.
     16        1.1      fvdl  *
     17        1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18        1.1      fvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19        1.1      fvdl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20        1.1      fvdl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21        1.1      fvdl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22        1.1      fvdl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23        1.1      fvdl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24        1.1      fvdl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25        1.1      fvdl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26        1.1      fvdl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27        1.1      fvdl  * SUCH DAMAGE.
     28        1.1      fvdl  *
     29       1.74  riastrad  *	$FreeBSD$
     30        1.1      fvdl  */
     31        1.9     lukem 
     32        1.9     lukem #include <sys/cdefs.h>
     33  1.112.2.4       riz __KERNEL_RCSID(0, "$NetBSD: agp_i810.c,v 1.112.2.4 2015/11/06 22:55:10 riz Exp $");
     34        1.1      fvdl 
     35        1.1      fvdl #include <sys/param.h>
     36        1.1      fvdl #include <sys/systm.h>
     37  1.112.2.2       riz #include <sys/atomic.h>
     38        1.1      fvdl #include <sys/malloc.h>
     39        1.1      fvdl #include <sys/kernel.h>
     40        1.1      fvdl #include <sys/proc.h>
     41        1.1      fvdl #include <sys/device.h>
     42        1.1      fvdl #include <sys/conf.h>
     43       1.75  riastrad #include <sys/xcall.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.74  riastrad #include <dev/pci/agp_i810var.h>
     51        1.1      fvdl 
     52        1.1      fvdl #include <sys/agpio.h>
     53        1.1      fvdl 
     54       1.43        ad #include <sys/bus.h>
     55        1.1      fvdl 
     56       1.20      tron #include "agp_intel.h"
     57       1.20      tron 
     58      1.106  riastrad #ifdef AGP_DEBUG
     59      1.106  riastrad #define	DPRINTF(sc, fmt, ...)						      \
     60      1.106  riastrad 	device_printf((sc)->as_dev, "%s: " fmt, __func__, ##__VA_ARGS__)
     61      1.106  riastrad #else
     62      1.106  riastrad #define	DPRINTF(sc, fmt, ...)	do {} while (0)
     63      1.106  riastrad #endif
     64      1.106  riastrad 
     65       1.74  riastrad struct agp_softc *agp_i810_sc = NULL;
     66       1.74  riastrad 
     67        1.1      fvdl #define READ1(off)	bus_space_read_1(isc->bst, isc->bsh, off)
     68       1.14       scw #define READ4(off)	bus_space_read_4(isc->bst, isc->bsh, off)
     69        1.1      fvdl #define WRITE4(off,v)	bus_space_write_4(isc->bst, isc->bsh, off, v)
     70        1.1      fvdl 
     71       1.14       scw #define CHIP_I810 0	/* i810/i815 */
     72       1.17   hannken #define CHIP_I830 1	/* 830M/845G */
     73       1.17   hannken #define CHIP_I855 2	/* 852GM/855GM/865G */
     74       1.56       tnn #define CHIP_I915 3	/* 915G/915GM/945G/945GM/945GME */
     75       1.45     joerg #define CHIP_I965 4	/* 965Q/965PM */
     76       1.45     joerg #define CHIP_G33  5	/* G33/Q33/Q35 */
     77       1.58  christos #define CHIP_G4X  6	/* G45/Q45 */
     78       1.14       scw 
     79       1.49  drochner /* XXX hack, see below */
     80       1.50  drochner static bus_addr_t agp_i810_vga_regbase;
     81       1.99  riastrad static bus_size_t agp_i810_vga_regsize;
     82       1.99  riastrad static bus_space_tag_t agp_i810_vga_bst;
     83       1.50  drochner static bus_space_handle_t agp_i810_vga_bsh;
     84       1.49  drochner 
     85        1.1      fvdl static u_int32_t agp_i810_get_aperture(struct agp_softc *);
     86        1.1      fvdl static int agp_i810_set_aperture(struct agp_softc *, u_int32_t);
     87        1.1      fvdl static int agp_i810_bind_page(struct agp_softc *, off_t, bus_addr_t);
     88        1.1      fvdl static int agp_i810_unbind_page(struct agp_softc *, off_t);
     89        1.1      fvdl static void agp_i810_flush_tlb(struct agp_softc *);
     90        1.1      fvdl static int agp_i810_enable(struct agp_softc *, u_int32_t mode);
     91        1.1      fvdl static struct agp_memory *agp_i810_alloc_memory(struct agp_softc *, int,
     92        1.1      fvdl 						vsize_t);
     93        1.1      fvdl static int agp_i810_free_memory(struct agp_softc *, struct agp_memory *);
     94       1.86  riastrad static int agp_i810_bind_memory(struct agp_softc *, struct agp_memory *,
     95       1.86  riastrad 		off_t);
     96       1.86  riastrad static int agp_i810_bind_memory_dcache(struct agp_softc *, struct agp_memory *,
     97       1.86  riastrad 		off_t);
     98       1.86  riastrad static int agp_i810_bind_memory_hwcursor(struct agp_softc *,
     99       1.86  riastrad 		struct agp_memory *, off_t);
    100        1.1      fvdl static int agp_i810_unbind_memory(struct agp_softc *, struct agp_memory *);
    101       1.47  jmcneill 
    102       1.66    dyoung static bool agp_i810_resume(device_t, const pmf_qual_t *);
    103       1.47  jmcneill static int agp_i810_init(struct agp_softc *);
    104        1.1      fvdl 
    105       1.75  riastrad static int agp_i810_setup_chipset_flush_page(struct agp_softc *);
    106       1.79  riastrad static void agp_i810_teardown_chipset_flush_page(struct agp_softc *);
    107       1.45     joerg static int agp_i810_init(struct agp_softc *);
    108       1.45     joerg 
    109       1.26   thorpej static struct agp_methods agp_i810_methods = {
    110        1.1      fvdl 	agp_i810_get_aperture,
    111        1.1      fvdl 	agp_i810_set_aperture,
    112        1.1      fvdl 	agp_i810_bind_page,
    113        1.1      fvdl 	agp_i810_unbind_page,
    114        1.1      fvdl 	agp_i810_flush_tlb,
    115        1.1      fvdl 	agp_i810_enable,
    116        1.1      fvdl 	agp_i810_alloc_memory,
    117        1.1      fvdl 	agp_i810_free_memory,
    118        1.1      fvdl 	agp_i810_bind_memory,
    119        1.1      fvdl 	agp_i810_unbind_memory,
    120        1.1      fvdl };
    121        1.1      fvdl 
    122       1.74  riastrad int
    123  1.112.2.2       riz agp_i810_write_gtt_entry(struct agp_i810_softc *isc, off_t off,
    124  1.112.2.2       riz     bus_addr_t addr, int flags)
    125       1.58  christos {
    126       1.71    gsutre 	u_int32_t pte;
    127       1.71    gsutre 
    128  1.112.2.2       riz 	/*
    129  1.112.2.2       riz 	 * Bits 11:4 (physical start address extension) should be zero.
    130  1.112.2.2       riz 	 * Flag bits 3:0 should be zero too.
    131  1.112.2.2       riz 	 *
    132  1.112.2.2       riz 	 * XXX This should be a kassert -- no reason for this routine
    133  1.112.2.2       riz 	 * to allow failure.
    134  1.112.2.2       riz 	 */
    135  1.112.2.2       riz 	if ((addr & 0xfff) != 0)
    136       1.71    gsutre 		return EINVAL;
    137  1.112.2.2       riz 	KASSERT(flags == (flags & 0x7));
    138       1.71    gsutre 
    139  1.112.2.2       riz 	pte = (u_int32_t)addr;
    140       1.71    gsutre 	/*
    141       1.71    gsutre 	 * We need to massage the pte if bus_addr_t is wider than 32 bits.
    142       1.71    gsutre 	 * The compiler isn't smart enough, hence the casts to uintmax_t.
    143       1.71    gsutre 	 */
    144       1.71    gsutre 	if (sizeof(bus_addr_t) > sizeof(u_int32_t)) {
    145       1.71    gsutre 		/* 965+ can do 36-bit addressing, add in the extra bits. */
    146       1.71    gsutre 		if (isc->chiptype == CHIP_I965 ||
    147       1.71    gsutre 		    isc->chiptype == CHIP_G33 ||
    148       1.71    gsutre 		    isc->chiptype == CHIP_G4X) {
    149  1.112.2.2       riz 			if (((uintmax_t)addr >> 36) != 0)
    150       1.71    gsutre 				return EINVAL;
    151  1.112.2.2       riz 			pte |= (addr >> 28) & 0xf0;
    152       1.71    gsutre 		} else {
    153  1.112.2.2       riz 			if (((uintmax_t)addr >> 32) != 0)
    154       1.71    gsutre 				return EINVAL;
    155       1.71    gsutre 		}
    156       1.71    gsutre 	}
    157       1.58  christos 
    158       1.79  riastrad 	bus_space_write_4(isc->gtt_bst, isc->gtt_bsh,
    159  1.112.2.2       riz 	    4*(off >> AGP_PAGE_SHIFT), pte | flags);
    160       1.58  christos 
    161       1.71    gsutre 	return 0;
    162       1.58  christos }
    163       1.58  christos 
    164       1.74  riastrad void
    165       1.74  riastrad agp_i810_post_gtt_entry(struct agp_i810_softc *isc, off_t off)
    166       1.74  riastrad {
    167       1.74  riastrad 
    168  1.112.2.2       riz 	/*
    169  1.112.2.2       riz 	 * See <https://bugs.freedesktop.org/show_bug.cgi?id=88191>.
    170  1.112.2.2       riz 	 * Out of paranoia, let's do the write barrier and posting
    171  1.112.2.2       riz 	 * read, because I don't have enough time or hardware to
    172  1.112.2.2       riz 	 * conduct conclusive tests.
    173  1.112.2.2       riz 	 */
    174  1.112.2.2       riz 	membar_producer();
    175       1.79  riastrad 	(void)bus_space_read_4(isc->gtt_bst, isc->gtt_bsh,
    176       1.79  riastrad 	    4*(off >> AGP_PAGE_SHIFT));
    177       1.74  riastrad }
    178       1.74  riastrad 
    179       1.75  riastrad static void
    180       1.75  riastrad agp_flush_cache_xc(void *a __unused, void *b __unused)
    181       1.75  riastrad {
    182       1.75  riastrad 
    183       1.75  riastrad 	agp_flush_cache();
    184       1.75  riastrad }
    185       1.75  riastrad 
    186       1.75  riastrad void
    187       1.75  riastrad agp_i810_chipset_flush(struct agp_i810_softc *isc)
    188       1.75  riastrad {
    189       1.75  riastrad 	unsigned int timo = 20000; /* * 50 us = 1 s */
    190       1.75  riastrad 
    191       1.75  riastrad 	switch (isc->chiptype) {
    192       1.75  riastrad 	case CHIP_I810:
    193       1.75  riastrad 		break;
    194       1.75  riastrad 	case CHIP_I830:
    195       1.75  riastrad 	case CHIP_I855:
    196       1.77  riastrad 		/*
    197       1.77  riastrad 		 * Flush all CPU caches.  If we're cold, we can't run
    198       1.77  riastrad 		 * xcalls, but there should be only one CPU up, so
    199       1.77  riastrad 		 * flushing only the local CPU's cache should suffice.
    200       1.77  riastrad 		 *
    201       1.77  riastrad 		 * XXX Come to think of it, do these chipsets appear in
    202       1.77  riastrad 		 * any multi-CPU systems?
    203       1.77  riastrad 		 */
    204       1.77  riastrad 		if (cold)
    205       1.77  riastrad 			agp_flush_cache();
    206       1.77  riastrad 		else
    207       1.77  riastrad 			xc_wait(xc_broadcast(0, &agp_flush_cache_xc,
    208       1.77  riastrad 				NULL, NULL));
    209       1.75  riastrad 		WRITE4(AGP_I830_HIC, READ4(AGP_I830_HIC) | __BIT(31));
    210       1.75  riastrad 		while (ISSET(READ4(AGP_I830_HIC), __BIT(31))) {
    211       1.75  riastrad 			if (timo-- == 0)
    212       1.75  riastrad 				break;
    213       1.75  riastrad 			DELAY(50);
    214       1.75  riastrad 		}
    215       1.75  riastrad 		break;
    216       1.75  riastrad 	case CHIP_I915:
    217       1.75  riastrad 	case CHIP_I965:
    218       1.75  riastrad 	case CHIP_G33:
    219       1.75  riastrad 	case CHIP_G4X:
    220       1.75  riastrad 		bus_space_write_4(isc->flush_bst, isc->flush_bsh, 0, 1);
    221       1.75  riastrad 		break;
    222       1.75  riastrad 	}
    223       1.75  riastrad }
    224       1.75  riastrad 
    225       1.55  matthias /* XXXthorpej -- duplicated code (see arch/x86/pci/pchb.c) */
    226        1.1      fvdl static int
    227       1.73    dyoung agp_i810_vgamatch(const struct pci_attach_args *pa)
    228        1.1      fvdl {
    229        1.6   thorpej 
    230        1.2      fvdl 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
    231        1.2      fvdl 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
    232        1.6   thorpej 		return (0);
    233        1.6   thorpej 
    234        1.1      fvdl 	switch (PCI_PRODUCT(pa->pa_id)) {
    235        1.1      fvdl 	case PCI_PRODUCT_INTEL_82810_GC:
    236        1.1      fvdl 	case PCI_PRODUCT_INTEL_82810_DC100_GC:
    237        1.1      fvdl 	case PCI_PRODUCT_INTEL_82810E_GC:
    238        1.1      fvdl 	case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
    239       1.14       scw 	case PCI_PRODUCT_INTEL_82830MP_IV:
    240       1.14       scw 	case PCI_PRODUCT_INTEL_82845G_IGD:
    241       1.17   hannken 	case PCI_PRODUCT_INTEL_82855GM_IGD:
    242       1.18      tron 	case PCI_PRODUCT_INTEL_82865_IGD:
    243       1.28  christos 	case PCI_PRODUCT_INTEL_82915G_IGD:
    244       1.28  christos 	case PCI_PRODUCT_INTEL_82915GM_IGD:
    245       1.32    simonb 	case PCI_PRODUCT_INTEL_82945P_IGD:
    246       1.32    simonb 	case PCI_PRODUCT_INTEL_82945GM_IGD:
    247       1.32    simonb 	case PCI_PRODUCT_INTEL_82945GM_IGD_1:
    248       1.56       tnn 	case PCI_PRODUCT_INTEL_82945GME_IGD:
    249       1.68       riz 	case PCI_PRODUCT_INTEL_E7221_IGD:
    250       1.42     markd 	case PCI_PRODUCT_INTEL_82965Q_IGD:
    251       1.42     markd 	case PCI_PRODUCT_INTEL_82965Q_IGD_1:
    252       1.45     joerg 	case PCI_PRODUCT_INTEL_82965PM_IGD:
    253       1.45     joerg 	case PCI_PRODUCT_INTEL_82965PM_IGD_1:
    254       1.45     joerg 	case PCI_PRODUCT_INTEL_82G33_IGD:
    255       1.45     joerg 	case PCI_PRODUCT_INTEL_82G33_IGD_1:
    256       1.44   jnemeth 	case PCI_PRODUCT_INTEL_82965G_IGD:
    257       1.44   jnemeth 	case PCI_PRODUCT_INTEL_82965G_IGD_1:
    258       1.68       riz 	case PCI_PRODUCT_INTEL_82965GME_IGD:
    259       1.46     markd 	case PCI_PRODUCT_INTEL_82Q35_IGD:
    260       1.46     markd 	case PCI_PRODUCT_INTEL_82Q35_IGD_1:
    261       1.46     markd 	case PCI_PRODUCT_INTEL_82Q33_IGD:
    262       1.46     markd 	case PCI_PRODUCT_INTEL_82Q33_IGD_1:
    263       1.57  christos 	case PCI_PRODUCT_INTEL_82G35_IGD:
    264       1.57  christos 	case PCI_PRODUCT_INTEL_82G35_IGD_1:
    265       1.55  matthias 	case PCI_PRODUCT_INTEL_82946GZ_IGD:
    266       1.58  christos 	case PCI_PRODUCT_INTEL_82GM45_IGD:
    267       1.58  christos 	case PCI_PRODUCT_INTEL_82GM45_IGD_1:
    268       1.62     markd 	case PCI_PRODUCT_INTEL_82IGD_E_IGD:
    269       1.62     markd 	case PCI_PRODUCT_INTEL_82Q45_IGD:
    270       1.62     markd 	case PCI_PRODUCT_INTEL_82G45_IGD:
    271       1.68       riz 	case PCI_PRODUCT_INTEL_82G41_IGD:
    272       1.68       riz 	case PCI_PRODUCT_INTEL_82B43_IGD:
    273       1.68       riz 	case PCI_PRODUCT_INTEL_IRONLAKE_D_IGD:
    274       1.68       riz 	case PCI_PRODUCT_INTEL_IRONLAKE_M_IGD:
    275       1.72      matt 	case PCI_PRODUCT_INTEL_PINEVIEW_IGD:
    276       1.72      matt 	case PCI_PRODUCT_INTEL_PINEVIEW_M_IGD:
    277        1.6   thorpej 		return (1);
    278        1.1      fvdl 	}
    279        1.1      fvdl 
    280        1.6   thorpej 	return (0);
    281        1.1      fvdl }
    282        1.1      fvdl 
    283       1.42     markd static int
    284       1.42     markd agp_i965_map_aperture(struct pci_attach_args *pa, struct agp_softc *sc, int reg)
    285       1.42     markd {
    286       1.42     markd         /*
    287       1.42     markd          * Find the aperture. Don't map it (yet), this would
    288       1.42     markd          * eat KVA.
    289       1.42     markd          */
    290       1.42     markd         if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg,
    291       1.42     markd             PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_64BIT, &sc->as_apaddr, &sc->as_apsize,
    292       1.42     markd             &sc->as_apflags) != 0)
    293       1.42     markd                 return ENXIO;
    294       1.42     markd 
    295       1.42     markd         sc->as_apt = pa->pa_memt;
    296       1.42     markd 
    297       1.42     markd         return 0;
    298       1.42     markd }
    299       1.42     markd 
    300        1.1      fvdl int
    301       1.54     freza agp_i810_attach(device_t parent, device_t self, void *aux)
    302        1.1      fvdl {
    303       1.54     freza 	struct agp_softc *sc = device_private(self);
    304        1.1      fvdl 	struct agp_i810_softc *isc;
    305       1.79  riastrad 	int apbase, mmadr_bar, gtt_bar;
    306       1.79  riastrad 	int mmadr_type, mmadr_flags;
    307       1.98  riastrad 	bus_addr_t mmadr;
    308       1.98  riastrad 	bus_size_t mmadr_size, gtt_off;
    309       1.79  riastrad 	int error;
    310        1.1      fvdl 
    311       1.10   tsutsui 	isc = malloc(sizeof *isc, M_AGP, M_NOWAIT|M_ZERO);
    312        1.1      fvdl 	if (isc == NULL) {
    313       1.15   thorpej 		aprint_error(": can't allocate chipset-specific softc\n");
    314       1.79  riastrad 		error = ENOMEM;
    315       1.79  riastrad 		goto fail0;
    316        1.1      fvdl 	}
    317        1.1      fvdl 	sc->as_chipc = isc;
    318        1.1      fvdl 	sc->as_methods = &agp_i810_methods;
    319        1.1      fvdl 
    320        1.1      fvdl 	if (pci_find_device(&isc->vga_pa, agp_i810_vgamatch) == 0) {
    321       1.20      tron #if NAGP_INTEL > 0
    322       1.19      tron 		const struct pci_attach_args *pa = aux;
    323       1.19      tron 
    324       1.19      tron 		switch (PCI_PRODUCT(pa->pa_id)) {
    325       1.19      tron 		case PCI_PRODUCT_INTEL_82840_HB:
    326       1.19      tron 		case PCI_PRODUCT_INTEL_82865_HB:
    327       1.21      tron 		case PCI_PRODUCT_INTEL_82845G_DRAM:
    328       1.23   xtraeme 		case PCI_PRODUCT_INTEL_82815_FULL_HUB:
    329       1.67  jakllsch 		case PCI_PRODUCT_INTEL_82855GM_MCH:
    330       1.79  riastrad 			free(isc, M_AGP);
    331       1.19      tron 			return agp_intel_attach(parent, self, aux);
    332       1.20      tron 		}
    333       1.20      tron #endif
    334       1.83  riastrad 		aprint_error(": can't find internal VGA"
    335       1.83  riastrad 		    " config space\n");
    336       1.79  riastrad 		error = ENOENT;
    337       1.79  riastrad 		goto fail1;
    338        1.1      fvdl 	}
    339        1.1      fvdl 
    340        1.1      fvdl 	/* XXXfvdl */
    341        1.1      fvdl 	sc->as_dmat = isc->vga_pa.pa_dmat;
    342        1.1      fvdl 
    343       1.14       scw 	switch (PCI_PRODUCT(isc->vga_pa.pa_id)) {
    344       1.14       scw 	case PCI_PRODUCT_INTEL_82810_GC:
    345       1.14       scw 	case PCI_PRODUCT_INTEL_82810_DC100_GC:
    346       1.14       scw 	case PCI_PRODUCT_INTEL_82810E_GC:
    347       1.14       scw 	case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
    348       1.14       scw 		isc->chiptype = CHIP_I810;
    349       1.82  riastrad 		aprint_normal(": i810-family chipset\n");
    350       1.14       scw 		break;
    351       1.14       scw 	case PCI_PRODUCT_INTEL_82830MP_IV:
    352       1.14       scw 	case PCI_PRODUCT_INTEL_82845G_IGD:
    353       1.14       scw 		isc->chiptype = CHIP_I830;
    354       1.82  riastrad 		aprint_normal(": i830-family chipset\n");
    355       1.14       scw 		break;
    356       1.17   hannken 	case PCI_PRODUCT_INTEL_82855GM_IGD:
    357       1.18      tron 	case PCI_PRODUCT_INTEL_82865_IGD:
    358       1.17   hannken 		isc->chiptype = CHIP_I855;
    359       1.82  riastrad 		aprint_normal(": i855-family chipset\n");
    360       1.17   hannken 		break;
    361       1.28  christos 	case PCI_PRODUCT_INTEL_82915G_IGD:
    362       1.28  christos 	case PCI_PRODUCT_INTEL_82915GM_IGD:
    363       1.32    simonb 	case PCI_PRODUCT_INTEL_82945P_IGD:
    364       1.32    simonb 	case PCI_PRODUCT_INTEL_82945GM_IGD:
    365       1.32    simonb 	case PCI_PRODUCT_INTEL_82945GM_IGD_1:
    366       1.56       tnn 	case PCI_PRODUCT_INTEL_82945GME_IGD:
    367       1.68       riz 	case PCI_PRODUCT_INTEL_E7221_IGD:
    368       1.72      matt 	case PCI_PRODUCT_INTEL_PINEVIEW_IGD:
    369       1.72      matt 	case PCI_PRODUCT_INTEL_PINEVIEW_M_IGD:
    370       1.28  christos 		isc->chiptype = CHIP_I915;
    371       1.82  riastrad 		aprint_normal(": i915-family chipset\n");
    372       1.28  christos 		break;
    373       1.42     markd 	case PCI_PRODUCT_INTEL_82965Q_IGD:
    374       1.42     markd 	case PCI_PRODUCT_INTEL_82965Q_IGD_1:
    375       1.45     joerg 	case PCI_PRODUCT_INTEL_82965PM_IGD:
    376       1.45     joerg 	case PCI_PRODUCT_INTEL_82965PM_IGD_1:
    377       1.44   jnemeth 	case PCI_PRODUCT_INTEL_82965G_IGD:
    378       1.44   jnemeth 	case PCI_PRODUCT_INTEL_82965G_IGD_1:
    379       1.68       riz 	case PCI_PRODUCT_INTEL_82965GME_IGD:
    380       1.55  matthias 	case PCI_PRODUCT_INTEL_82946GZ_IGD:
    381       1.57  christos 	case PCI_PRODUCT_INTEL_82G35_IGD:
    382       1.57  christos 	case PCI_PRODUCT_INTEL_82G35_IGD_1:
    383       1.42     markd 		isc->chiptype = CHIP_I965;
    384       1.82  riastrad 		aprint_normal(": i965-family chipset\n");
    385       1.42     markd 		break;
    386       1.46     markd 	case PCI_PRODUCT_INTEL_82Q35_IGD:
    387       1.46     markd 	case PCI_PRODUCT_INTEL_82Q35_IGD_1:
    388       1.45     joerg 	case PCI_PRODUCT_INTEL_82G33_IGD:
    389       1.45     joerg 	case PCI_PRODUCT_INTEL_82G33_IGD_1:
    390       1.46     markd 	case PCI_PRODUCT_INTEL_82Q33_IGD:
    391       1.46     markd 	case PCI_PRODUCT_INTEL_82Q33_IGD_1:
    392       1.45     joerg 		isc->chiptype = CHIP_G33;
    393       1.82  riastrad 		aprint_normal(": G33-family chipset\n");
    394       1.63     markd 		break;
    395       1.58  christos 	case PCI_PRODUCT_INTEL_82GM45_IGD:
    396       1.58  christos 	case PCI_PRODUCT_INTEL_82GM45_IGD_1:
    397       1.62     markd 	case PCI_PRODUCT_INTEL_82IGD_E_IGD:
    398       1.62     markd 	case PCI_PRODUCT_INTEL_82Q45_IGD:
    399       1.62     markd 	case PCI_PRODUCT_INTEL_82G45_IGD:
    400       1.68       riz 	case PCI_PRODUCT_INTEL_82G41_IGD:
    401       1.68       riz 	case PCI_PRODUCT_INTEL_82B43_IGD:
    402       1.68       riz 	case PCI_PRODUCT_INTEL_IRONLAKE_D_IGD:
    403       1.68       riz 	case PCI_PRODUCT_INTEL_IRONLAKE_M_IGD:
    404       1.58  christos 		isc->chiptype = CHIP_G4X;
    405       1.82  riastrad 		aprint_normal(": G4X-family chipset\n");
    406       1.45     joerg 		break;
    407       1.14       scw 	}
    408       1.82  riastrad 	aprint_naive("\n");
    409       1.14       scw 
    410  1.112.2.4       riz 	/* Discriminate on the chipset to choose the relevant BARs.  */
    411       1.45     joerg 	switch (isc->chiptype) {
    412       1.45     joerg 	case CHIP_I915:
    413       1.45     joerg 	case CHIP_G33:
    414       1.45     joerg 		apbase = AGP_I915_GMADR;
    415       1.79  riastrad 		mmadr_bar = AGP_I915_MMADR;
    416       1.79  riastrad 		gtt_bar = AGP_I915_GTTADR;
    417      1.100  riastrad 		gtt_off = ~(bus_size_t)0; /* XXXGCC */
    418       1.45     joerg 		break;
    419       1.58  christos 	case CHIP_I965:
    420       1.79  riastrad 		apbase = AGP_I965_GMADR;
    421       1.79  riastrad 		mmadr_bar = AGP_I965_MMADR;
    422       1.79  riastrad 		gtt_bar = 0;
    423       1.79  riastrad 		gtt_off = AGP_I965_GTT;
    424       1.79  riastrad 		break;
    425       1.58  christos 	case CHIP_G4X:
    426       1.58  christos 		apbase = AGP_I965_GMADR;
    427       1.79  riastrad 		mmadr_bar = AGP_I965_MMADR;
    428       1.79  riastrad 		gtt_bar = 0;
    429       1.79  riastrad 		gtt_off = AGP_G4X_GTT;
    430       1.58  christos 		break;
    431       1.45     joerg 	default:
    432       1.45     joerg 		apbase = AGP_I810_GMADR;
    433       1.79  riastrad 		mmadr_bar = AGP_I810_MMADR;
    434       1.79  riastrad 		gtt_bar = 0;
    435       1.79  riastrad 		gtt_off = AGP_I810_GTT;
    436       1.45     joerg 		break;
    437       1.45     joerg 	}
    438       1.58  christos 
    439  1.112.2.4       riz 	/*
    440  1.112.2.4       riz 	 * Ensure the MMIO BAR is, in fact, a memory BAR.
    441  1.112.2.4       riz 	 *
    442  1.112.2.4       riz 	 * XXX This is required because we use pa_memt below.  It is
    443  1.112.2.4       riz 	 * not a priori clear to me there is any other reason to
    444  1.112.2.4       riz 	 * require this.
    445  1.112.2.4       riz 	 */
    446  1.112.2.4       riz 	mmadr_type = pci_mapreg_type(isc->vga_pa.pa_pc, isc->vga_pa.pa_tag,
    447  1.112.2.4       riz 	    mmadr_bar);
    448  1.112.2.4       riz 	if ((mmadr_type & PCI_MAPREG_TYPE_MEM) != PCI_MAPREG_TYPE_MEM) {
    449  1.112.2.4       riz 		aprint_error_dev(self, "non-memory device MMIO registers\n");
    450  1.112.2.4       riz 		error = ENXIO;
    451  1.112.2.4       riz 		goto fail1;
    452  1.112.2.4       riz 	}
    453  1.112.2.4       riz 
    454  1.112.2.4       riz 	/*
    455  1.112.2.4       riz 	 * Determine the size of the MMIO registers.
    456  1.112.2.4       riz 	 *
    457  1.112.2.4       riz 	 * XXX The size of the MMIO registers we use is statically
    458  1.112.2.4       riz 	 * determined, as a function of the chipset, by the driver's
    459  1.112.2.4       riz 	 * implementation.
    460  1.112.2.4       riz 	 *
    461  1.112.2.4       riz 	 * On some chipsets, the GTT is part of the MMIO register BAR.
    462  1.112.2.4       riz 	 * We would like to map the GTT separately, so that we can map
    463  1.112.2.4       riz 	 * it prefetchable, which we can't do with the MMIO registers.
    464  1.112.2.4       riz 	 * Consequently, we would especially like to map a fixed size
    465  1.112.2.4       riz 	 * of MMIO registers, not just whatever size the BAR says.
    466  1.112.2.4       riz 	 *
    467  1.112.2.4       riz 	 * However, old drm assumes that the combined GTT/MMIO register
    468  1.112.2.4       riz 	 * space is a single bus space mapping, so mapping them
    469  1.112.2.4       riz 	 * separately breaks that.  Once we rip out old drm, we can
    470  1.112.2.4       riz 	 * replace the pci_mapreg_info call by the chipset switch.
    471  1.112.2.4       riz 	 */
    472  1.112.2.4       riz #if notyet
    473  1.112.2.4       riz 	switch (isc->chiptype) {
    474  1.112.2.4       riz 	case CHIP_I810:
    475  1.112.2.4       riz 	case CHIP_I830:
    476  1.112.2.4       riz 	case CHIP_I855:
    477  1.112.2.4       riz 	case CHIP_I915:
    478  1.112.2.4       riz 	case CHIP_G33:
    479  1.112.2.4       riz 	case CHIP_I965:
    480  1.112.2.4       riz 	case CHIP_G4X:
    481  1.112.2.4       riz 		isc->size = 512*1024;
    482  1.112.2.4       riz 		break;
    483  1.112.2.4       riz 	case CHIP_SANDYBRIDGE:
    484  1.112.2.4       riz 	case CHIP_IVYBRIDGE:
    485  1.112.2.4       riz 	case CHIP_HASWELL:
    486  1.112.2.4       riz 		isc->size = 2*1024*1024;
    487  1.112.2.4       riz 		break;
    488  1.112.2.4       riz 	}
    489  1.112.2.4       riz #else
    490  1.112.2.4       riz 	if (pci_mapreg_info(isc->vga_pa.pa_pc, isc->vga_pa.pa_tag,
    491  1.112.2.4       riz 		mmadr_bar, mmadr_type, NULL, &isc->size, NULL))
    492  1.112.2.4       riz 		isc->size = 512*1024;
    493  1.112.2.4       riz #endif	/* notyet */
    494  1.112.2.4       riz 
    495       1.79  riastrad 	/* Map (or, rather, find the address and size of) the aperture.  */
    496       1.79  riastrad 	if (isc->chiptype == CHIP_I965 || isc->chiptype == CHIP_G4X)
    497       1.58  christos 		error = agp_i965_map_aperture(&isc->vga_pa, sc, apbase);
    498       1.79  riastrad 	else
    499       1.42     markd 		error = agp_map_aperture(&isc->vga_pa, sc, apbase);
    500       1.79  riastrad 	if (error) {
    501       1.82  riastrad 		aprint_error_dev(self, "can't map aperture: %d\n", error);
    502       1.79  riastrad 		goto fail1;
    503        1.1      fvdl 	}
    504        1.1      fvdl 
    505       1.79  riastrad 	/* Map the memory-mapped I/O registers, or the non-GTT part.  */
    506       1.79  riastrad 	if (pci_mapreg_info(isc->vga_pa.pa_pc, isc->vga_pa.pa_tag, mmadr_bar,
    507       1.79  riastrad 		mmadr_type, &mmadr, &mmadr_size, &mmadr_flags)) {
    508       1.79  riastrad 		aprint_error_dev(self, "can't find MMIO registers\n");
    509       1.79  riastrad 		error = ENXIO;
    510       1.79  riastrad 		goto fail1;
    511       1.79  riastrad 	}
    512       1.97  riastrad 	if (mmadr_size < isc->size) {
    513       1.97  riastrad 		aprint_error_dev(self, "MMIO registers too small"
    514       1.97  riastrad 		    ": %"PRIuMAX" < %"PRIuMAX"\n",
    515       1.97  riastrad 		    (uintmax_t)mmadr_size, (uintmax_t)isc->size);
    516       1.97  riastrad 		error = ENXIO;
    517       1.97  riastrad 		goto fail1;
    518       1.79  riastrad 	}
    519       1.79  riastrad 	isc->bst = isc->vga_pa.pa_memt;
    520       1.79  riastrad 	error = bus_space_map(isc->bst, mmadr, isc->size, mmadr_flags,
    521       1.79  riastrad 	    &isc->bsh);
    522       1.79  riastrad 	if (error) {
    523       1.83  riastrad 		aprint_error_dev(self, "can't map MMIO registers: %d\n",
    524       1.83  riastrad 		    error);
    525       1.79  riastrad 		error = ENXIO;
    526       1.79  riastrad 		goto fail1;
    527       1.79  riastrad 	}
    528       1.79  riastrad 
    529       1.85  riastrad 	/* Set up a chipset flush page if necessary.  */
    530       1.85  riastrad 	switch (isc->chiptype) {
    531       1.85  riastrad 	case CHIP_I915:
    532       1.85  riastrad 	case CHIP_I965:
    533       1.85  riastrad 	case CHIP_G33:
    534       1.85  riastrad 	case CHIP_G4X:
    535       1.85  riastrad 		error = agp_i810_setup_chipset_flush_page(sc);
    536       1.85  riastrad 		if (error) {
    537       1.85  riastrad 			aprint_error_dev(self,
    538       1.85  riastrad 			    "can't set up chipset flush page: %d\n", error);
    539       1.85  riastrad 			goto fail2;
    540       1.85  riastrad 		}
    541       1.85  riastrad 		break;
    542       1.85  riastrad 	}
    543       1.85  riastrad 
    544       1.85  riastrad 	/*
    545       1.85  riastrad 	 * XXX horrible hack to allow drm code to use our mapping
    546       1.85  riastrad 	 * of VGA chip registers
    547       1.85  riastrad 	 */
    548       1.85  riastrad 	agp_i810_vga_regbase = mmadr;
    549       1.99  riastrad 	agp_i810_vga_regsize = isc->size;
    550       1.99  riastrad 	agp_i810_vga_bst = isc->bst;
    551       1.85  riastrad 	agp_i810_vga_bsh = isc->bsh;
    552       1.85  riastrad 
    553       1.85  riastrad 	/* Initialize the chipset.  */
    554       1.85  riastrad 	error = agp_i810_init(sc);
    555       1.85  riastrad 	if (error)
    556       1.85  riastrad 		goto fail3;
    557       1.85  riastrad 
    558       1.79  riastrad 	/* Map the GTT, from either part of the MMIO region or its own BAR.  */
    559       1.79  riastrad 	if (gtt_bar == 0) {
    560       1.79  riastrad 		isc->gtt_bst = isc->bst;
    561       1.87  riastrad 		if ((mmadr_size - gtt_off) < isc->gtt_size) {
    562       1.85  riastrad 			aprint_error_dev(self, "GTTMMADR too small for GTT"
    563       1.87  riastrad 			    ": (%"PRIxMAX" - %"PRIxMAX") < %"PRIxMAX"\n",
    564       1.85  riastrad 			    (uintmax_t)mmadr_size,
    565       1.87  riastrad 			    (uintmax_t)gtt_off,
    566       1.87  riastrad 			    (uintmax_t)isc->gtt_size);
    567       1.85  riastrad 			error = ENXIO;
    568       1.85  riastrad 			goto fail4;
    569       1.85  riastrad 		}
    570       1.97  riastrad 		/*
    571       1.97  riastrad 		 * Map the GTT separately if we can, so that we can map
    572       1.97  riastrad 		 * it prefetchable, but in early models, there are MMIO
    573       1.97  riastrad 		 * registers before and after the GTT, so we can only
    574       1.97  riastrad 		 * take a subregion.
    575       1.97  riastrad 		 */
    576       1.97  riastrad 		if (isc->size < gtt_off)
    577       1.97  riastrad 			error = bus_space_map(isc->gtt_bst, (mmadr + gtt_off),
    578       1.97  riastrad 			    isc->gtt_size, mmadr_flags, &isc->gtt_bsh);
    579       1.97  riastrad 		else
    580       1.97  riastrad 			error = bus_space_subregion(isc->bst, isc->bsh,
    581       1.97  riastrad 			    gtt_off, isc->gtt_size, &isc->gtt_bsh);
    582       1.79  riastrad 		if (error) {
    583       1.79  riastrad 			aprint_error_dev(self, "can't map GTT: %d\n", error);
    584       1.79  riastrad 			error = ENXIO;
    585       1.85  riastrad 			goto fail4;
    586       1.28  christos 		}
    587       1.79  riastrad 	} else {
    588       1.85  riastrad 		bus_size_t gtt_bar_size;
    589       1.79  riastrad 		/*
    590       1.79  riastrad 		 * All chipsets with a separate BAR for the GTT, namely
    591       1.79  riastrad 		 * the i915 and G33 families, have 32-bit GTT BARs.
    592       1.79  riastrad 		 *
    593       1.79  riastrad 		 * XXX [citation needed]
    594       1.79  riastrad 		 */
    595       1.79  riastrad 		if (pci_mapreg_map(&isc->vga_pa, gtt_bar, PCI_MAPREG_TYPE_MEM,
    596       1.79  riastrad 			0,
    597       1.85  riastrad 			&isc->gtt_bst, &isc->gtt_bsh, NULL, &gtt_bar_size)) {
    598       1.79  riastrad 			aprint_error_dev(self, "can't map GTT\n");
    599       1.79  riastrad 			error = ENXIO;
    600       1.85  riastrad 			goto fail4;
    601       1.42     markd 		}
    602       1.85  riastrad 		if (gtt_bar_size != isc->gtt_size) {
    603       1.83  riastrad 			aprint_error_dev(self,
    604       1.85  riastrad 			    "BAR size %"PRIxMAX
    605       1.85  riastrad 			    " mismatches detected GTT size %"PRIxMAX
    606       1.85  riastrad 			    "; trusting BAR\n",
    607       1.85  riastrad 			    (uintmax_t)gtt_bar_size,
    608       1.85  riastrad 			    (uintmax_t)isc->gtt_size);
    609       1.85  riastrad 			isc->gtt_size = gtt_bar_size;
    610       1.28  christos 		}
    611       1.28  christos 	}
    612       1.28  christos 
    613       1.79  riastrad 	/* Power management.  (XXX Nothing to save on suspend?  Fishy...)  */
    614       1.47  jmcneill 	if (!pmf_device_register(self, NULL, agp_i810_resume))
    615       1.82  riastrad 		aprint_error_dev(self, "can't establish power handler\n");
    616       1.47  jmcneill 
    617       1.82  riastrad 	/* Match the generic AGP code's autoconf output format.  */
    618       1.82  riastrad 	aprint_normal("%s", device_xname(self));
    619       1.82  riastrad 
    620       1.79  riastrad 	/* Success!  */
    621       1.79  riastrad 	return 0;
    622       1.79  riastrad 
    623       1.85  riastrad fail5: __unused
    624       1.85  riastrad 	pmf_device_deregister(self);
    625       1.97  riastrad 	if ((gtt_bar != 0) || (isc->size < gtt_off))
    626       1.97  riastrad 		bus_space_unmap(isc->gtt_bst, isc->gtt_bsh, isc->gtt_size);
    627       1.85  riastrad 	isc->gtt_size = 0;
    628       1.85  riastrad fail4:
    629       1.79  riastrad #if notyet
    630       1.79  riastrad 	agp_i810_fini(sc);
    631       1.79  riastrad #endif
    632       1.85  riastrad fail3:	switch (isc->chiptype) {
    633       1.75  riastrad 	case CHIP_I915:
    634       1.75  riastrad 	case CHIP_I965:
    635       1.75  riastrad 	case CHIP_G33:
    636       1.75  riastrad 	case CHIP_G4X:
    637       1.79  riastrad 		agp_i810_teardown_chipset_flush_page(sc);
    638       1.75  riastrad 		break;
    639       1.75  riastrad 	}
    640       1.79  riastrad fail2:	bus_space_unmap(isc->bst, isc->bsh, isc->size);
    641       1.79  riastrad 	isc->size = 0;
    642       1.79  riastrad fail1:	free(isc, M_AGP);
    643       1.79  riastrad 	sc->as_chipc = NULL;
    644       1.79  riastrad fail0:	agp_generic_detach(sc);
    645       1.79  riastrad 	KASSERT(error);
    646       1.79  riastrad 	return error;
    647       1.45     joerg }
    648       1.45     joerg 
    649  1.112.2.3       snj /*
    650  1.112.2.3       snj  * Skip pages reserved by the BIOS.  Notably, skip 0xa0000-0xfffff,
    651  1.112.2.3       snj  * which includes the video BIOS at 0xc0000-0xdffff which the display
    652  1.112.2.3       snj  * drivers need for video mode detection.
    653  1.112.2.3       snj  *
    654  1.112.2.3       snj  * XXX Is there an MI name for this, or a conventional x86 name?  Or
    655  1.112.2.3       snj  * should we really use bus_dma instead?
    656  1.112.2.3       snj  */
    657  1.112.2.3       snj #define	PCIBIOS_MIN_MEM		0x100000
    658  1.112.2.3       snj 
    659       1.75  riastrad static int
    660       1.75  riastrad agp_i810_setup_chipset_flush_page(struct agp_softc *sc)
    661       1.75  riastrad {
    662       1.75  riastrad 	struct agp_i810_softc *const isc = sc->as_chipc;
    663       1.84  riastrad 	const pci_chipset_tag_t pc = sc->as_pc;
    664       1.84  riastrad 	const pcitag_t tag = sc->as_tag;
    665       1.83  riastrad 	pcireg_t lo, hi;
    666       1.75  riastrad 	bus_addr_t addr, minaddr, maxaddr;
    667       1.75  riastrad 	int error;
    668       1.75  riastrad 
    669       1.75  riastrad 	/* We always use memory-mapped I/O.  */
    670       1.75  riastrad 	isc->flush_bst = isc->vga_pa.pa_memt;
    671       1.75  riastrad 
    672       1.75  riastrad 	/* No page allocated yet.  */
    673       1.75  riastrad 	isc->flush_addr = 0;
    674       1.75  riastrad 
    675       1.75  riastrad 	/* Read the PCI config register: 4-byte on gen3, 8-byte on gen>=4.  */
    676       1.75  riastrad 	if (isc->chiptype == CHIP_I915) {
    677       1.83  riastrad 		addr = pci_conf_read(pc, tag, AGP_I915_IFPADDR);
    678  1.112.2.3       snj 		minaddr = PCIBIOS_MIN_MEM;
    679       1.75  riastrad 		maxaddr = UINT32_MAX;
    680       1.75  riastrad 	} else {
    681       1.83  riastrad 		hi = pci_conf_read(pc, tag, AGP_I965_IFPADDR+4);
    682       1.83  riastrad 		lo = pci_conf_read(pc, tag, AGP_I965_IFPADDR);
    683       1.76  riastrad 		/*
    684       1.76  riastrad 		 * Convert to uint64_t, rather than bus_addr_t which
    685       1.76  riastrad 		 * may be 32-bit, to avoid undefined behaviour with a
    686       1.76  riastrad 		 * too-wide shift.  Since the BIOS doesn't know whether
    687       1.76  riastrad 		 * the OS will run 64-bit or with PAE, it ought to
    688       1.76  riastrad 		 * configure at most a 32-bit physical address, so
    689       1.76  riastrad 		 * let's print a warning in case that happens.
    690       1.76  riastrad 		 */
    691       1.76  riastrad 		addr = ((uint64_t)hi << 32) | lo;
    692       1.76  riastrad 		if (hi) {
    693       1.76  riastrad 			aprint_error_dev(sc->as_dev,
    694       1.76  riastrad 			    "BIOS configured >32-bit flush page address"
    695       1.76  riastrad 			    ": %"PRIx64"\n", ((uint64_t)hi << 32) | lo);
    696       1.76  riastrad #if __i386__ && !PAE
    697       1.76  riastrad 			return EIO;
    698       1.76  riastrad #endif
    699       1.76  riastrad 		}
    700  1.112.2.3       snj 		minaddr = PCIBIOS_MIN_MEM;
    701       1.76  riastrad 		maxaddr = MIN(UINT64_MAX, ~(bus_addr_t)0);
    702       1.75  riastrad 	}
    703       1.75  riastrad 
    704       1.75  riastrad 	/* Allocate or map a pre-allocated a page for it.  */
    705       1.75  riastrad 	if (ISSET(addr, 1)) {
    706       1.75  riastrad 		/* BIOS allocated it for us.  Use that.  */
    707       1.75  riastrad 		error = bus_space_map(isc->flush_bst, addr & ~1, PAGE_SIZE, 0,
    708       1.75  riastrad 		    &isc->flush_bsh);
    709       1.75  riastrad 		if (error)
    710       1.75  riastrad 			return error;
    711       1.75  riastrad 	} else {
    712       1.75  riastrad 		/* None allocated.  Allocate one.  */
    713       1.75  riastrad 		error = bus_space_alloc(isc->flush_bst, minaddr, maxaddr,
    714       1.75  riastrad 		    PAGE_SIZE, PAGE_SIZE, 0, 0,
    715       1.75  riastrad 		    &isc->flush_addr, &isc->flush_bsh);
    716       1.75  riastrad 		if (error)
    717       1.75  riastrad 			return error;
    718       1.75  riastrad 		KASSERT(isc->flush_addr != 0);
    719       1.75  riastrad 		/* Write it into the PCI config register.  */
    720       1.75  riastrad 		addr = isc->flush_addr | 1;
    721       1.75  riastrad 		if (isc->chiptype == CHIP_I915) {
    722       1.83  riastrad 			pci_conf_write(pc, tag, AGP_I915_IFPADDR, addr);
    723       1.75  riastrad 		} else {
    724       1.83  riastrad 			hi = __SHIFTOUT(addr, __BITS(63, 32));
    725       1.84  riastrad 			lo = __SHIFTOUT(addr, __BITS(31, 0));
    726       1.83  riastrad 			pci_conf_write(pc, tag, AGP_I965_IFPADDR+4, hi);
    727       1.83  riastrad 			pci_conf_write(pc, tag, AGP_I965_IFPADDR, lo);
    728       1.75  riastrad 		}
    729       1.75  riastrad 	}
    730       1.75  riastrad 
    731       1.75  riastrad 	/* Success!  */
    732       1.75  riastrad 	return 0;
    733       1.75  riastrad }
    734       1.75  riastrad 
    735       1.79  riastrad static void
    736       1.79  riastrad agp_i810_teardown_chipset_flush_page(struct agp_softc *sc)
    737       1.79  riastrad {
    738       1.79  riastrad 	struct agp_i810_softc *const isc = sc->as_chipc;
    739       1.79  riastrad 
    740       1.79  riastrad 	if (isc->flush_addr) {
    741       1.79  riastrad 		/* If we allocated a page, clear it.  */
    742       1.79  riastrad 		if (isc->chiptype == CHIP_I915) {
    743       1.79  riastrad 			pci_conf_write(sc->as_pc, sc->as_tag, AGP_I915_IFPADDR,
    744       1.79  riastrad 			    0);
    745       1.79  riastrad 		} else {
    746       1.79  riastrad 			pci_conf_write(sc->as_pc, sc->as_tag,
    747       1.79  riastrad 			    AGP_I965_IFPADDR, 0);
    748       1.79  riastrad 			pci_conf_write(sc->as_pc, sc->as_tag,
    749       1.79  riastrad 			    AGP_I965_IFPADDR + 4, 0);
    750       1.79  riastrad 		}
    751       1.79  riastrad 		isc->flush_addr = 0;
    752  1.112.2.3       snj 		bus_space_free(isc->flush_bst, isc->flush_bsh, PAGE_SIZE);
    753       1.79  riastrad 	} else {
    754       1.79  riastrad 		/* Otherwise, just unmap the pre-allocated page.  */
    755       1.79  riastrad 		bus_space_unmap(isc->flush_bst, isc->flush_bsh, PAGE_SIZE);
    756       1.79  riastrad 	}
    757       1.79  riastrad }
    758       1.79  riastrad 
    759       1.49  drochner /*
    760       1.49  drochner  * XXX horrible hack to allow drm code to use our mapping
    761       1.49  drochner  * of VGA chip registers
    762       1.49  drochner  */
    763       1.49  drochner int
    764       1.99  riastrad agp_i810_borrow(bus_addr_t base, bus_size_t size, bus_space_handle_t *hdlp)
    765       1.49  drochner {
    766       1.49  drochner 
    767       1.99  riastrad 	if (agp_i810_vga_regbase == 0)
    768       1.99  riastrad 		return 0;
    769       1.99  riastrad 	if (base < agp_i810_vga_regbase)
    770       1.99  riastrad 		return 0;
    771       1.99  riastrad 	if (agp_i810_vga_regsize < size)
    772       1.99  riastrad 		return 0;
    773       1.99  riastrad 	if ((base - agp_i810_vga_regbase) > (agp_i810_vga_regsize - size))
    774       1.99  riastrad 		return 0;
    775       1.99  riastrad 	if (bus_space_subregion(agp_i810_vga_bst, agp_i810_vga_bsh,
    776       1.99  riastrad 		(base - agp_i810_vga_regbase), (agp_i810_vga_regsize - size),
    777       1.99  riastrad 		hdlp))
    778       1.49  drochner 		return 0;
    779       1.49  drochner 	return 1;
    780       1.49  drochner }
    781       1.49  drochner 
    782       1.82  riastrad static int
    783       1.82  riastrad agp_i810_init(struct agp_softc *sc)
    784       1.45     joerg {
    785       1.45     joerg 	struct agp_i810_softc *isc;
    786       1.82  riastrad 	int error;
    787       1.45     joerg 
    788       1.45     joerg 	isc = sc->as_chipc;
    789       1.45     joerg 
    790       1.14       scw 	if (isc->chiptype == CHIP_I810) {
    791       1.85  riastrad 		struct agp_gatt *gatt;
    792       1.36  christos 		void *virtual;
    793       1.14       scw 		int dummyseg;
    794       1.31      tron 
    795       1.14       scw 		/* Some i810s have on-chip memory called dcache */
    796       1.14       scw 		if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
    797       1.14       scw 			isc->dcache_size = 4 * 1024 * 1024;
    798       1.14       scw 		else
    799       1.14       scw 			isc->dcache_size = 0;
    800       1.14       scw 
    801       1.14       scw 		/* According to the specs the gatt on the i810 must be 64k */
    802       1.85  riastrad 		isc->gtt_size = 64 * 1024;
    803       1.85  riastrad 		gatt = malloc(sizeof(*gatt), M_AGP, M_NOWAIT);
    804       1.85  riastrad 		if (gatt == NULL) {
    805       1.85  riastrad 			aprint_error_dev(sc->as_dev,
    806       1.85  riastrad 			    "can't malloc GATT record\n");
    807       1.85  riastrad 			error = ENOMEM;
    808       1.85  riastrad 			goto fail0;
    809       1.85  riastrad 		}
    810       1.85  riastrad 		gatt->ag_entries = isc->gtt_size / sizeof(uint32_t);
    811       1.85  riastrad 		error = agp_alloc_dmamem(sc->as_dmat, isc->gtt_size,
    812       1.31      tron 		    0, &gatt->ag_dmamap, &virtual, &gatt->ag_physical,
    813       1.82  riastrad 		    &gatt->ag_dmaseg, 1, &dummyseg);
    814       1.82  riastrad 		if (error) {
    815       1.82  riastrad 			aprint_error_dev(sc->as_dev,
    816       1.82  riastrad 			    "can't allocate memory for GTT: %d\n", error);
    817       1.85  riastrad 			free(gatt, M_AGP);
    818       1.82  riastrad 			goto fail0;
    819        1.1      fvdl 		}
    820       1.82  riastrad 
    821       1.31      tron 		gatt->ag_virtual = (uint32_t *)virtual;
    822       1.85  riastrad 		gatt->ag_size = gatt->ag_entries * sizeof(uint32_t);
    823       1.14       scw 		memset(gatt->ag_virtual, 0, gatt->ag_size);
    824       1.85  riastrad 		agp_flush_cache();
    825       1.25     perry 
    826       1.14       scw 		/* Install the GATT. */
    827       1.85  riastrad 		isc->pgtblctl = gatt->ag_physical | 1;
    828       1.85  riastrad 		WRITE4(AGP_I810_PGTBL_CTL, isc->pgtblctl);
    829       1.85  riastrad 		isc->gatt = gatt;
    830       1.17   hannken 	} else if (isc->chiptype == CHIP_I830) {
    831       1.14       scw 		/* The i830 automatically initializes the 128k gatt on boot. */
    832       1.85  riastrad 		/* XXX [citation needed] */
    833       1.14       scw 		pcireg_t reg;
    834       1.14       scw 		u_int16_t gcc1;
    835       1.14       scw 
    836       1.85  riastrad 		isc->gtt_size = 128 * 1024;
    837       1.85  riastrad 
    838       1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
    839       1.14       scw 		gcc1 = (u_int16_t)(reg >> 16);
    840       1.14       scw 		switch (gcc1 & AGP_I830_GCC1_GMS) {
    841       1.14       scw 		case AGP_I830_GCC1_GMS_STOLEN_512:
    842       1.14       scw 			isc->stolen = (512 - 132) * 1024 / 4096;
    843       1.14       scw 			break;
    844       1.25     perry 		case AGP_I830_GCC1_GMS_STOLEN_1024:
    845       1.14       scw 			isc->stolen = (1024 - 132) * 1024 / 4096;
    846       1.14       scw 			break;
    847       1.25     perry 		case AGP_I830_GCC1_GMS_STOLEN_8192:
    848       1.14       scw 			isc->stolen = (8192 - 132) * 1024 / 4096;
    849       1.14       scw 			break;
    850       1.14       scw 		default:
    851       1.14       scw 			isc->stolen = 0;
    852       1.82  riastrad 			aprint_error_dev(sc->as_dev,
    853       1.82  riastrad 			    "unknown memory configuration, disabling\n");
    854       1.82  riastrad 			error = ENXIO;
    855       1.82  riastrad 			goto fail0;
    856       1.14       scw 		}
    857       1.45     joerg 
    858       1.14       scw 		if (isc->stolen > 0) {
    859       1.82  riastrad 			aprint_normal_dev(sc->as_dev,
    860       1.82  riastrad 			    "detected %dk stolen memory\n",
    861       1.82  riastrad 			    isc->stolen * 4);
    862       1.14       scw 		}
    863       1.17   hannken 
    864       1.17   hannken 		/* GATT address is already in there, make sure it's enabled */
    865       1.85  riastrad 		isc->pgtblctl = READ4(AGP_I810_PGTBL_CTL);
    866       1.85  riastrad 		isc->pgtblctl |= 1;
    867       1.85  riastrad 		WRITE4(AGP_I810_PGTBL_CTL, isc->pgtblctl);
    868       1.42     markd 	} else if (isc->chiptype == CHIP_I855 || isc->chiptype == CHIP_I915 ||
    869       1.58  christos 		   isc->chiptype == CHIP_I965 || isc->chiptype == CHIP_G33 ||
    870       1.58  christos 		   isc->chiptype == CHIP_G4X) {
    871       1.17   hannken 		pcireg_t reg;
    872       1.85  riastrad 		u_int32_t gtt_size, stolen;	/* XXX kilobytes */
    873       1.17   hannken 		u_int16_t gcc1;
    874       1.17   hannken 
    875       1.45     joerg 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I855_GCC1);
    876       1.45     joerg 		gcc1 = (u_int16_t)(reg >> 16);
    877       1.45     joerg 
    878       1.85  riastrad 		isc->pgtblctl = READ4(AGP_I810_PGTBL_CTL);
    879       1.58  christos 
    880       1.42     markd 		/* Stolen memory is set up at the beginning of the aperture by
    881       1.42     markd                  * the BIOS, consisting of the GATT followed by 4kb for the
    882       1.42     markd 		 * BIOS display.
    883       1.42     markd                  */
    884       1.42     markd                 switch (isc->chiptype) {
    885       1.42     markd 		case CHIP_I855:
    886       1.58  christos 			gtt_size = 128;
    887       1.42     markd 			break;
    888       1.42     markd                 case CHIP_I915:
    889       1.58  christos 			gtt_size = 256;
    890       1.42     markd 			break;
    891       1.42     markd 		case CHIP_I965:
    892       1.85  riastrad 			switch (isc->pgtblctl & AGP_I810_PGTBL_SIZE_MASK) {
    893       1.58  christos 			case AGP_I810_PGTBL_SIZE_128KB:
    894       1.58  christos 			case AGP_I810_PGTBL_SIZE_512KB:
    895       1.58  christos 				gtt_size = 512;
    896       1.58  christos 				break;
    897       1.58  christos 			case AGP_I965_PGTBL_SIZE_1MB:
    898       1.58  christos 				gtt_size = 1024;
    899       1.58  christos 				break;
    900       1.58  christos 			case AGP_I965_PGTBL_SIZE_2MB:
    901       1.61    sketch 				gtt_size = 2048;
    902       1.58  christos 				break;
    903       1.58  christos 			case AGP_I965_PGTBL_SIZE_1_5MB:
    904       1.61    sketch 				gtt_size = 1024 + 512;
    905       1.58  christos 				break;
    906       1.58  christos 			default:
    907       1.82  riastrad 				aprint_error_dev(sc->as_dev,
    908       1.82  riastrad 				    "bad PGTBL size\n");
    909       1.82  riastrad 				error = ENXIO;
    910       1.82  riastrad 				goto fail0;
    911       1.58  christos 			}
    912       1.42     markd 			break;
    913       1.45     joerg 		case CHIP_G33:
    914       1.45     joerg 			switch (gcc1 & AGP_G33_PGTBL_SIZE_MASK) {
    915       1.45     joerg 			case AGP_G33_PGTBL_SIZE_1M:
    916       1.58  christos 				gtt_size = 1024;
    917       1.45     joerg 				break;
    918       1.45     joerg 			case AGP_G33_PGTBL_SIZE_2M:
    919       1.58  christos 				gtt_size = 2048;
    920       1.45     joerg 				break;
    921       1.45     joerg 			default:
    922       1.82  riastrad 				aprint_error_dev(sc->as_dev,
    923       1.82  riastrad 				    "bad PGTBL size\n");
    924       1.82  riastrad 				error = ENXIO;
    925       1.82  riastrad 				goto fail0;
    926       1.45     joerg 			}
    927       1.45     joerg 			break;
    928       1.58  christos 		case CHIP_G4X:
    929      1.103  riastrad 			switch (isc->pgtblctl & AGP_G4X_PGTBL_SIZE_MASK) {
    930      1.103  riastrad 			case AGP_G4X_PGTBL_SIZE_512K:
    931      1.103  riastrad 				gtt_size = 512;
    932      1.103  riastrad 				break;
    933      1.103  riastrad 			case AGP_G4X_PGTBL_SIZE_256K:
    934      1.103  riastrad 				gtt_size = 256;
    935      1.103  riastrad 				break;
    936      1.103  riastrad 			case AGP_G4X_PGTBL_SIZE_128K:
    937      1.103  riastrad 				gtt_size = 128;
    938      1.103  riastrad 				break;
    939      1.103  riastrad 			case AGP_G4X_PGTBL_SIZE_1M:
    940      1.103  riastrad 				gtt_size = 1*1024;
    941      1.103  riastrad 				break;
    942      1.103  riastrad 			case AGP_G4X_PGTBL_SIZE_2M:
    943      1.103  riastrad 				gtt_size = 2*1024;
    944      1.103  riastrad 				break;
    945      1.103  riastrad 			case AGP_G4X_PGTBL_SIZE_1_5M:
    946      1.103  riastrad 				gtt_size = 1*1024 + 512;
    947      1.103  riastrad 				break;
    948      1.103  riastrad 			default:
    949      1.103  riastrad 				aprint_error_dev(sc->as_dev,
    950      1.103  riastrad 				    "bad PGTBL size\n");
    951      1.103  riastrad 				error = ENXIO;
    952      1.103  riastrad 				goto fail0;
    953      1.103  riastrad 			}
    954       1.58  christos 			break;
    955       1.42     markd 		default:
    956       1.82  riastrad 			panic("impossible chiptype %d", isc->chiptype);
    957       1.58  christos 		}
    958       1.42     markd 
    959       1.85  riastrad 		/*
    960       1.85  riastrad 		 * XXX If I'm reading the datasheets right, this stolen
    961       1.85  riastrad 		 * memory detection logic is totally wrong.
    962       1.85  riastrad 		 */
    963       1.17   hannken 		switch (gcc1 & AGP_I855_GCC1_GMS) {
    964       1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_1M:
    965       1.58  christos 			stolen = 1024;
    966       1.17   hannken 			break;
    967       1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_4M:
    968       1.58  christos 			stolen = 4 * 1024;
    969       1.17   hannken 			break;
    970       1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_8M:
    971       1.58  christos 			stolen = 8 * 1024;
    972       1.17   hannken 			break;
    973       1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_16M:
    974       1.58  christos 			stolen = 16 * 1024;
    975       1.17   hannken 			break;
    976       1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_32M:
    977       1.58  christos 			stolen = 32 * 1024;
    978       1.41  sborrill 			break;
    979       1.41  sborrill 		case AGP_I915_GCC1_GMS_STOLEN_48M:
    980       1.58  christos 			stolen = 48 * 1024;
    981       1.41  sborrill 			break;
    982       1.41  sborrill 		case AGP_I915_GCC1_GMS_STOLEN_64M:
    983       1.58  christos 			stolen = 64 * 1024;
    984       1.41  sborrill 			break;
    985       1.46     markd 		case AGP_G33_GCC1_GMS_STOLEN_128M:
    986       1.58  christos 			stolen = 128 * 1024;
    987       1.46     markd 			break;
    988       1.46     markd 		case AGP_G33_GCC1_GMS_STOLEN_256M:
    989       1.58  christos 			stolen = 256 * 1024;
    990       1.58  christos 			break;
    991       1.58  christos 		case AGP_G4X_GCC1_GMS_STOLEN_96M:
    992       1.58  christos 			stolen = 96 * 1024;
    993       1.58  christos 			break;
    994       1.58  christos 		case AGP_G4X_GCC1_GMS_STOLEN_160M:
    995       1.58  christos 			stolen = 160 * 1024;
    996       1.58  christos 			break;
    997       1.58  christos 		case AGP_G4X_GCC1_GMS_STOLEN_224M:
    998       1.58  christos 			stolen = 224 * 1024;
    999       1.58  christos 			break;
   1000       1.58  christos 		case AGP_G4X_GCC1_GMS_STOLEN_352M:
   1001       1.58  christos 			stolen = 352 * 1024;
   1002       1.46     markd 			break;
   1003       1.28  christos 		default:
   1004       1.82  riastrad 			aprint_error_dev(sc->as_dev,
   1005       1.82  riastrad 			    "unknown memory configuration, disabling\n");
   1006       1.82  riastrad 			error = ENXIO;
   1007       1.82  riastrad 			goto fail0;
   1008       1.28  christos 		}
   1009       1.58  christos 
   1010       1.58  christos 		switch (gcc1 & AGP_I855_GCC1_GMS) {
   1011       1.58  christos 		case AGP_I915_GCC1_GMS_STOLEN_48M:
   1012       1.58  christos 		case AGP_I915_GCC1_GMS_STOLEN_64M:
   1013       1.58  christos 			if (isc->chiptype != CHIP_I915 &&
   1014       1.58  christos 			    isc->chiptype != CHIP_I965 &&
   1015       1.58  christos 			    isc->chiptype != CHIP_G33 &&
   1016       1.58  christos 			    isc->chiptype != CHIP_G4X)
   1017       1.58  christos 				stolen = 0;
   1018       1.58  christos 			break;
   1019       1.58  christos 		case AGP_G33_GCC1_GMS_STOLEN_128M:
   1020       1.58  christos 		case AGP_G33_GCC1_GMS_STOLEN_256M:
   1021       1.58  christos 			if (isc->chiptype != CHIP_I965 &&
   1022       1.58  christos 			    isc->chiptype != CHIP_G33 &&
   1023       1.58  christos 			    isc->chiptype != CHIP_G4X)
   1024       1.58  christos 				stolen = 0;
   1025       1.58  christos 			break;
   1026       1.58  christos 		case AGP_G4X_GCC1_GMS_STOLEN_96M:
   1027       1.58  christos 		case AGP_G4X_GCC1_GMS_STOLEN_160M:
   1028       1.58  christos 		case AGP_G4X_GCC1_GMS_STOLEN_224M:
   1029       1.58  christos 		case AGP_G4X_GCC1_GMS_STOLEN_352M:
   1030       1.58  christos 			if (isc->chiptype != CHIP_I965 &&
   1031       1.58  christos 			    isc->chiptype != CHIP_G4X)
   1032       1.58  christos 				stolen = 0;
   1033       1.58  christos 			break;
   1034       1.58  christos 		}
   1035       1.58  christos 
   1036       1.85  riastrad 		isc->gtt_size = gtt_size * 1024;
   1037       1.85  riastrad 
   1038       1.58  christos 		/* BIOS space */
   1039       1.85  riastrad 		/* XXX [citation needed] */
   1040       1.62     markd 		gtt_size += 4;
   1041       1.58  christos 
   1042       1.85  riastrad 		/* XXX [citation needed] for this subtraction */
   1043       1.58  christos 		isc->stolen = (stolen - gtt_size) * 1024 / 4096;
   1044       1.58  christos 
   1045       1.28  christos 		if (isc->stolen > 0) {
   1046       1.82  riastrad 			aprint_normal_dev(sc->as_dev,
   1047       1.82  riastrad 			    "detected %dk stolen memory\n",
   1048       1.82  riastrad 			    isc->stolen * 4);
   1049       1.28  christos 		}
   1050       1.28  christos 
   1051       1.28  christos 		/* GATT address is already in there, make sure it's enabled */
   1052       1.85  riastrad 		isc->pgtblctl |= 1;
   1053       1.85  riastrad 		WRITE4(AGP_I810_PGTBL_CTL, isc->pgtblctl);
   1054        1.1      fvdl 	}
   1055        1.1      fvdl 
   1056        1.1      fvdl 	/*
   1057        1.1      fvdl 	 * Make sure the chipset can see everything.
   1058        1.1      fvdl 	 */
   1059        1.1      fvdl 	agp_flush_cache();
   1060       1.14       scw 
   1061       1.74  riastrad 	/*
   1062       1.74  riastrad 	 * Publish what we found for kludgey drivers (I'm looking at
   1063       1.74  riastrad 	 * you, drm).
   1064       1.74  riastrad 	 */
   1065       1.74  riastrad 	if (agp_i810_sc == NULL)
   1066       1.74  riastrad 		agp_i810_sc = sc;
   1067       1.74  riastrad 	else
   1068       1.82  riastrad 		aprint_error_dev(sc->as_dev, "agp already attached\n");
   1069       1.74  riastrad 
   1070       1.82  riastrad 	/* Success!  */
   1071        1.1      fvdl 	return 0;
   1072       1.82  riastrad 
   1073       1.82  riastrad fail0:	KASSERT(error);
   1074       1.82  riastrad 	return error;
   1075        1.1      fvdl }
   1076        1.1      fvdl 
   1077        1.1      fvdl #if 0
   1078        1.1      fvdl static int
   1079        1.1      fvdl agp_i810_detach(struct agp_softc *sc)
   1080        1.1      fvdl {
   1081        1.1      fvdl 	int error;
   1082        1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
   1083        1.1      fvdl 
   1084        1.1      fvdl 	error = agp_generic_detach(sc);
   1085        1.1      fvdl 	if (error)
   1086        1.1      fvdl 		return error;
   1087        1.1      fvdl 
   1088       1.75  riastrad 	switch (isc->chiptype) {
   1089       1.75  riastrad 	case CHIP_I915:
   1090       1.75  riastrad 	case CHIP_I965:
   1091       1.75  riastrad 	case CHIP_G33:
   1092       1.75  riastrad 	case CHIP_G4X:
   1093       1.79  riastrad 		agp_i810_teardown_chipset_flush_page(sc);
   1094       1.75  riastrad 		break;
   1095       1.75  riastrad 	}
   1096       1.75  riastrad 
   1097        1.1      fvdl 	/* Clear the GATT base. */
   1098       1.14       scw 	if (sc->chiptype == CHIP_I810) {
   1099       1.14       scw 		WRITE4(AGP_I810_PGTBL_CTL, 0);
   1100       1.14       scw 	} else {
   1101       1.14       scw 		unsigned int pgtblctl;
   1102       1.14       scw 		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
   1103       1.14       scw 		pgtblctl &= ~1;
   1104       1.14       scw 		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
   1105       1.14       scw 	}
   1106        1.1      fvdl 
   1107       1.14       scw 	if (sc->chiptype == CHIP_I810) {
   1108       1.14       scw 		agp_free_dmamem(sc->as_dmat, gatt->ag_size, gatt->ag_dmamap,
   1109       1.36  christos 		    (void *)gatt->ag_virtual, &gatt->ag_dmaseg, 1);
   1110       1.85  riastrad 		free(isc->gatt, M_AGP);
   1111       1.14       scw 	}
   1112        1.1      fvdl 
   1113        1.1      fvdl 	return 0;
   1114        1.1      fvdl }
   1115        1.1      fvdl #endif
   1116        1.1      fvdl 
   1117        1.1      fvdl static u_int32_t
   1118        1.1      fvdl agp_i810_get_aperture(struct agp_softc *sc)
   1119        1.1      fvdl {
   1120       1.14       scw 	struct agp_i810_softc *isc = sc->as_chipc;
   1121       1.14       scw 	pcireg_t reg;
   1122       1.58  christos 	u_int32_t size;
   1123       1.88  riastrad 	u_int16_t miscc, gcc1;
   1124       1.14       scw 
   1125       1.58  christos 	size = 0;
   1126       1.58  christos 
   1127       1.42     markd 	switch (isc->chiptype) {
   1128       1.42     markd 	case CHIP_I810:
   1129       1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
   1130       1.14       scw 		miscc = (u_int16_t)(reg >> 16);
   1131       1.14       scw 		if ((miscc & AGP_I810_MISCC_WINSIZE) ==
   1132       1.14       scw 		    AGP_I810_MISCC_WINSIZE_32)
   1133       1.58  christos 			size = 32 * 1024 * 1024;
   1134       1.14       scw 		else
   1135       1.58  christos 			size = 64 * 1024 * 1024;
   1136       1.58  christos 		break;
   1137       1.42     markd 	case CHIP_I830:
   1138       1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
   1139       1.14       scw 		gcc1 = (u_int16_t)(reg >> 16);
   1140       1.14       scw 		if ((gcc1 & AGP_I830_GCC1_GMASIZE) == AGP_I830_GCC1_GMASIZE_64)
   1141       1.58  christos 			size = 64 * 1024 * 1024;
   1142       1.14       scw 		else
   1143       1.58  christos 			size = 128 * 1024 * 1024;
   1144       1.58  christos 		break;
   1145       1.42     markd 	case CHIP_I855:
   1146       1.58  christos 		size = 128 * 1024 * 1024;
   1147       1.58  christos 		break;
   1148       1.42     markd 	case CHIP_I915:
   1149       1.45     joerg 	case CHIP_G33:
   1150       1.64     markd 	case CHIP_G4X:
   1151       1.88  riastrad 		size = sc->as_apsize;
   1152       1.58  christos 		break;
   1153       1.42     markd 	case CHIP_I965:
   1154       1.58  christos 		size = 512 * 1024 * 1024;
   1155       1.58  christos 		break;
   1156       1.42     markd 	default:
   1157       1.42     markd 		aprint_error(": Unknown chipset\n");
   1158       1.14       scw 	}
   1159       1.42     markd 
   1160       1.58  christos 	return size;
   1161        1.1      fvdl }
   1162        1.1      fvdl 
   1163        1.1      fvdl static int
   1164       1.86  riastrad agp_i810_set_aperture(struct agp_softc *sc __unused,
   1165       1.86  riastrad     uint32_t aperture __unused)
   1166        1.1      fvdl {
   1167       1.14       scw 
   1168       1.86  riastrad 	return ENOSYS;
   1169        1.1      fvdl }
   1170        1.1      fvdl 
   1171        1.1      fvdl static int
   1172        1.1      fvdl agp_i810_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
   1173        1.1      fvdl {
   1174        1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
   1175        1.1      fvdl 
   1176       1.85  riastrad 	if (offset < 0 || offset >= ((isc->gtt_size/4) << AGP_PAGE_SHIFT)) {
   1177      1.106  riastrad 		DPRINTF(sc, "failed"
   1178      1.107  riastrad 		    ": offset 0x%"PRIxMAX", shift %u, entries %"PRIuMAX"\n",
   1179      1.107  riastrad 		    (uintmax_t)offset,
   1180      1.107  riastrad 		    (unsigned)AGP_PAGE_SHIFT,
   1181      1.104  riastrad 		    (uintmax_t)isc->gtt_size/4);
   1182        1.1      fvdl 		return EINVAL;
   1183       1.14       scw 	}
   1184       1.14       scw 
   1185       1.70    gsutre 	if (isc->chiptype != CHIP_I810) {
   1186       1.14       scw 		if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
   1187      1.106  riastrad 			DPRINTF(sc, "trying to bind into stolen memory\n");
   1188       1.14       scw 			return EINVAL;
   1189       1.14       scw 		}
   1190       1.14       scw 	}
   1191        1.1      fvdl 
   1192  1.112.2.2       riz 	return agp_i810_write_gtt_entry(isc, offset, physical,
   1193  1.112.2.2       riz 	    AGP_I810_GTT_VALID);
   1194        1.1      fvdl }
   1195        1.1      fvdl 
   1196        1.1      fvdl static int
   1197        1.1      fvdl agp_i810_unbind_page(struct agp_softc *sc, off_t offset)
   1198        1.1      fvdl {
   1199        1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
   1200        1.1      fvdl 
   1201       1.85  riastrad 	if (offset < 0 || offset >= ((isc->gtt_size/4) << AGP_PAGE_SHIFT))
   1202        1.1      fvdl 		return EINVAL;
   1203        1.1      fvdl 
   1204       1.17   hannken 	if (isc->chiptype != CHIP_I810 ) {
   1205       1.14       scw 		if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
   1206      1.106  riastrad 			DPRINTF(sc, "trying to unbind from stolen memory\n");
   1207       1.14       scw 			return EINVAL;
   1208       1.14       scw 		}
   1209       1.14       scw 	}
   1210       1.14       scw 
   1211  1.112.2.2       riz 	return agp_i810_write_gtt_entry(isc, offset, 0, 0);
   1212        1.1      fvdl }
   1213        1.1      fvdl 
   1214        1.1      fvdl /*
   1215        1.1      fvdl  * Writing via memory mapped registers already flushes all TLBs.
   1216        1.1      fvdl  */
   1217        1.1      fvdl static void
   1218       1.35  christos agp_i810_flush_tlb(struct agp_softc *sc)
   1219        1.1      fvdl {
   1220        1.1      fvdl }
   1221        1.1      fvdl 
   1222        1.1      fvdl static int
   1223       1.35  christos agp_i810_enable(struct agp_softc *sc, u_int32_t mode)
   1224        1.1      fvdl {
   1225        1.1      fvdl 
   1226        1.1      fvdl 	return 0;
   1227        1.1      fvdl }
   1228        1.1      fvdl 
   1229       1.86  riastrad #define	AGP_I810_MEMTYPE_MAIN		0
   1230       1.86  riastrad #define	AGP_I810_MEMTYPE_DCACHE		1
   1231       1.86  riastrad #define	AGP_I810_MEMTYPE_HWCURSOR	2
   1232       1.86  riastrad 
   1233        1.1      fvdl static struct agp_memory *
   1234        1.1      fvdl agp_i810_alloc_memory(struct agp_softc *sc, int type, vsize_t size)
   1235        1.1      fvdl {
   1236        1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
   1237        1.1      fvdl 	struct agp_memory *mem;
   1238       1.86  riastrad 	int error;
   1239        1.1      fvdl 
   1240      1.107  riastrad 	DPRINTF(sc, "AGP: alloc(%d, 0x%"PRIxMAX")\n", type, (uintmax_t)size);
   1241       1.28  christos 
   1242       1.86  riastrad 	if (size <= 0)
   1243       1.86  riastrad 		return NULL;
   1244        1.1      fvdl 	if ((size & (AGP_PAGE_SIZE - 1)) != 0)
   1245       1.86  riastrad 		return NULL;
   1246       1.89  riastrad 	KASSERT(sc->as_allocated <= sc->as_maxmem);
   1247       1.89  riastrad 	if (size > (sc->as_maxmem - sc->as_allocated))
   1248       1.86  riastrad 		return NULL;
   1249      1.101  riastrad 	if (size > ((isc->gtt_size/4) << AGP_PAGE_SHIFT))
   1250      1.101  riastrad 		return NULL;
   1251      1.101  riastrad 
   1252       1.86  riastrad 	switch (type) {
   1253       1.86  riastrad 	case AGP_I810_MEMTYPE_MAIN:
   1254       1.86  riastrad 		break;
   1255       1.86  riastrad 	case AGP_I810_MEMTYPE_DCACHE:
   1256       1.86  riastrad 		if (isc->chiptype != CHIP_I810)
   1257       1.86  riastrad 			return NULL;
   1258        1.1      fvdl 		if (size != isc->dcache_size)
   1259       1.86  riastrad 			return NULL;
   1260       1.86  riastrad 		break;
   1261       1.86  riastrad 	case AGP_I810_MEMTYPE_HWCURSOR:
   1262       1.86  riastrad 		if ((size != AGP_PAGE_SIZE) &&
   1263       1.86  riastrad 		    (size != AGP_PAGE_SIZE*4))
   1264       1.86  riastrad 			return NULL;
   1265       1.86  riastrad 		break;
   1266       1.86  riastrad 	default:
   1267       1.86  riastrad 		return NULL;
   1268        1.1      fvdl 	}
   1269        1.1      fvdl 
   1270       1.86  riastrad 	mem = malloc(sizeof(*mem), M_AGP, M_WAITOK|M_ZERO);
   1271        1.1      fvdl 	if (mem == NULL)
   1272       1.86  riastrad 		goto fail0;
   1273        1.1      fvdl 	mem->am_id = sc->as_nextid++;
   1274        1.1      fvdl 	mem->am_size = size;
   1275        1.1      fvdl 	mem->am_type = type;
   1276        1.1      fvdl 
   1277       1.86  riastrad 	switch (type) {
   1278       1.86  riastrad 	case AGP_I810_MEMTYPE_MAIN:
   1279       1.86  riastrad 		error = bus_dmamap_create(sc->as_dmat, size,
   1280       1.86  riastrad 		    (size >> AGP_PAGE_SHIFT) + 1, size, 0, BUS_DMA_WAITOK,
   1281       1.86  riastrad 		    &mem->am_dmamap);
   1282       1.86  riastrad 		if (error)
   1283       1.86  riastrad 			goto fail1;
   1284       1.86  riastrad 		break;
   1285       1.86  riastrad 	case AGP_I810_MEMTYPE_DCACHE:
   1286       1.86  riastrad 		break;
   1287       1.86  riastrad 	case AGP_I810_MEMTYPE_HWCURSOR:
   1288       1.86  riastrad 		mem->am_dmaseg = malloc(sizeof(*mem->am_dmaseg), M_AGP,
   1289        1.1      fvdl 		    M_WAITOK);
   1290       1.86  riastrad 		error = agp_alloc_dmamem(sc->as_dmat, size, 0, &mem->am_dmamap,
   1291       1.86  riastrad 		    &mem->am_virtual, &mem->am_physical, mem->am_dmaseg, 1,
   1292       1.86  riastrad 		    &mem->am_nseg);
   1293       1.86  riastrad 		if (error) {
   1294        1.1      fvdl 			free(mem->am_dmaseg, M_AGP);
   1295       1.86  riastrad 			goto fail1;
   1296        1.1      fvdl 		}
   1297       1.86  riastrad 		(void)memset(mem->am_virtual, 0, size);
   1298       1.86  riastrad 		break;
   1299       1.86  riastrad 	default:
   1300       1.86  riastrad 		panic("invalid agp memory type: %d", type);
   1301        1.1      fvdl 	}
   1302        1.1      fvdl 
   1303        1.1      fvdl 	TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
   1304        1.1      fvdl 	sc->as_allocated += size;
   1305        1.1      fvdl 
   1306        1.1      fvdl 	return mem;
   1307       1.86  riastrad 
   1308       1.86  riastrad fail1:	free(mem, M_AGP);
   1309       1.86  riastrad fail0:	return NULL;
   1310        1.1      fvdl }
   1311        1.1      fvdl 
   1312        1.1      fvdl static int
   1313        1.1      fvdl agp_i810_free_memory(struct agp_softc *sc, struct agp_memory *mem)
   1314        1.1      fvdl {
   1315       1.86  riastrad 
   1316        1.1      fvdl 	if (mem->am_is_bound)
   1317        1.1      fvdl 		return EBUSY;
   1318        1.1      fvdl 
   1319       1.86  riastrad 	switch (mem->am_type) {
   1320       1.86  riastrad 	case AGP_I810_MEMTYPE_MAIN:
   1321       1.90  riastrad 		bus_dmamap_destroy(sc->as_dmat, mem->am_dmamap);
   1322       1.90  riastrad 		break;
   1323       1.86  riastrad 	case AGP_I810_MEMTYPE_DCACHE:
   1324       1.86  riastrad 		break;
   1325       1.86  riastrad 	case AGP_I810_MEMTYPE_HWCURSOR:
   1326        1.1      fvdl 		agp_free_dmamem(sc->as_dmat, mem->am_size, mem->am_dmamap,
   1327        1.1      fvdl 		    mem->am_virtual, mem->am_dmaseg, mem->am_nseg);
   1328        1.1      fvdl 		free(mem->am_dmaseg, M_AGP);
   1329       1.86  riastrad 		break;
   1330       1.86  riastrad 	default:
   1331       1.86  riastrad 		panic("invalid agp i810 memory type: %d", mem->am_type);
   1332        1.1      fvdl 	}
   1333        1.1      fvdl 
   1334        1.1      fvdl 	sc->as_allocated -= mem->am_size;
   1335        1.1      fvdl 	TAILQ_REMOVE(&sc->as_memory, mem, am_link);
   1336        1.1      fvdl 	free(mem, M_AGP);
   1337       1.86  riastrad 
   1338        1.1      fvdl 	return 0;
   1339        1.1      fvdl }
   1340        1.1      fvdl 
   1341        1.1      fvdl static int
   1342        1.1      fvdl agp_i810_bind_memory(struct agp_softc *sc, struct agp_memory *mem,
   1343       1.86  riastrad     off_t offset)
   1344        1.1      fvdl {
   1345        1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
   1346       1.86  riastrad 	uint32_t pgtblctl;
   1347       1.86  riastrad 	int error;
   1348        1.4  drochner 
   1349       1.86  riastrad 	if (mem->am_is_bound)
   1350       1.70    gsutre 		return EINVAL;
   1351       1.70    gsutre 
   1352        1.4  drochner 	/*
   1353        1.4  drochner 	 * XXX evil hack: the PGTBL_CTL appearently gets overwritten by the
   1354        1.4  drochner 	 * X server for mysterious reasons which leads to crashes if we write
   1355        1.4  drochner 	 * to the GTT through the MMIO window.
   1356        1.4  drochner 	 * Until the issue is solved, simply restore it.
   1357        1.4  drochner 	 */
   1358       1.86  riastrad 	pgtblctl = bus_space_read_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL);
   1359       1.86  riastrad 	if (pgtblctl != isc->pgtblctl) {
   1360       1.86  riastrad 		printf("agp_i810_bind_memory: PGTBL_CTL is 0x%"PRIx32
   1361       1.86  riastrad 		    " - fixing\n", pgtblctl);
   1362        1.4  drochner 		bus_space_write_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL,
   1363       1.85  riastrad 		    isc->pgtblctl);
   1364        1.4  drochner 	}
   1365        1.1      fvdl 
   1366       1.86  riastrad 	switch (mem->am_type) {
   1367       1.86  riastrad 	case AGP_I810_MEMTYPE_MAIN:
   1368      1.101  riastrad 		return agp_generic_bind_memory_bounded(sc, mem, offset,
   1369      1.101  riastrad 		    0, (isc->gtt_size/4) << AGP_PAGE_SHIFT);
   1370       1.86  riastrad 	case AGP_I810_MEMTYPE_DCACHE:
   1371       1.86  riastrad 		error = agp_i810_bind_memory_dcache(sc, mem, offset);
   1372       1.86  riastrad 		break;
   1373       1.86  riastrad 	case AGP_I810_MEMTYPE_HWCURSOR:
   1374       1.86  riastrad 		error = agp_i810_bind_memory_hwcursor(sc, mem, offset);
   1375       1.86  riastrad 		break;
   1376       1.86  riastrad 	default:
   1377       1.86  riastrad 		panic("invalid agp i810 memory type: %d", mem->am_type);
   1378        1.5  drochner 	}
   1379       1.86  riastrad 	if (error)
   1380       1.86  riastrad 		return error;
   1381        1.5  drochner 
   1382       1.86  riastrad 	/* Success!  */
   1383       1.86  riastrad 	mem->am_is_bound = 1;
   1384       1.86  riastrad 	return 0;
   1385       1.86  riastrad }
   1386       1.86  riastrad 
   1387       1.86  riastrad static int
   1388       1.86  riastrad agp_i810_bind_memory_dcache(struct agp_softc *sc, struct agp_memory *mem,
   1389       1.86  riastrad     off_t offset)
   1390       1.86  riastrad {
   1391       1.86  riastrad 	struct agp_i810_softc *const isc __diagused = sc->as_chipc;
   1392       1.86  riastrad 	uint32_t i, j;
   1393       1.86  riastrad 	int error;
   1394       1.86  riastrad 
   1395       1.86  riastrad 	KASSERT(isc->chiptype == CHIP_I810);
   1396       1.86  riastrad 
   1397       1.86  riastrad 	KASSERT((mem->am_size & (AGP_PAGE_SIZE - 1)) == 0);
   1398       1.86  riastrad 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
   1399      1.108  riastrad 		error = agp_i810_write_gtt_entry(isc, offset + i,
   1400  1.112.2.2       riz 		    i, AGP_I810_GTT_VALID | AGP_I810_GTT_I810_DCACHE);
   1401       1.86  riastrad 		if (error)
   1402       1.86  riastrad 			goto fail0;
   1403       1.86  riastrad 	}
   1404       1.86  riastrad 
   1405       1.86  riastrad 	/* Success!  */
   1406      1.111  riastrad 	mem->am_offset = offset;
   1407       1.86  riastrad 	return 0;
   1408       1.14       scw 
   1409       1.86  riastrad fail0:	for (j = 0; j < i; j += AGP_PAGE_SIZE)
   1410       1.86  riastrad 		(void)agp_i810_unbind_page(sc, offset + j);
   1411       1.86  riastrad 	return error;
   1412       1.86  riastrad }
   1413       1.86  riastrad 
   1414       1.86  riastrad static int
   1415       1.86  riastrad agp_i810_bind_memory_hwcursor(struct agp_softc *sc, struct agp_memory *mem,
   1416       1.86  riastrad     off_t offset)
   1417       1.86  riastrad {
   1418       1.86  riastrad 	const bus_addr_t pa = mem->am_physical;
   1419       1.86  riastrad 	uint32_t i, j;
   1420       1.86  riastrad 	int error;
   1421       1.86  riastrad 
   1422       1.86  riastrad 	KASSERT((mem->am_size & (AGP_PAGE_SIZE - 1)) == 0);
   1423       1.86  riastrad 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
   1424       1.86  riastrad 		error = agp_i810_bind_page(sc, offset + i, pa + i);
   1425       1.86  riastrad 		if (error)
   1426       1.86  riastrad 			goto fail0;
   1427       1.86  riastrad 	}
   1428       1.86  riastrad 
   1429       1.86  riastrad 	/* Success!  */
   1430       1.86  riastrad 	mem->am_offset = offset;
   1431        1.1      fvdl 	return 0;
   1432       1.86  riastrad 
   1433       1.86  riastrad fail0:	for (j = 0; j < i; j += AGP_PAGE_SIZE)
   1434       1.86  riastrad 		(void)agp_i810_unbind_page(sc, offset + j);
   1435       1.86  riastrad 	return error;
   1436        1.1      fvdl }
   1437        1.1      fvdl 
   1438        1.1      fvdl static int
   1439        1.1      fvdl agp_i810_unbind_memory(struct agp_softc *sc, struct agp_memory *mem)
   1440        1.1      fvdl {
   1441      1.112  riastrad 	struct agp_i810_softc *isc __diagused = sc->as_chipc;
   1442        1.1      fvdl 	u_int32_t i;
   1443        1.1      fvdl 
   1444       1.86  riastrad 	if (!mem->am_is_bound)
   1445       1.70    gsutre 		return EINVAL;
   1446       1.70    gsutre 
   1447       1.86  riastrad 	switch (mem->am_type) {
   1448       1.86  riastrad 	case AGP_I810_MEMTYPE_MAIN:
   1449      1.101  riastrad 		return agp_generic_unbind_memory(sc, mem);
   1450       1.86  riastrad 	case AGP_I810_MEMTYPE_DCACHE:
   1451       1.86  riastrad 		KASSERT(isc->chiptype == CHIP_I810);
   1452      1.111  riastrad 		/* FALLTHROUGH */
   1453       1.94  riastrad 	case AGP_I810_MEMTYPE_HWCURSOR:
   1454       1.94  riastrad 		for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
   1455       1.94  riastrad 			(void)agp_i810_unbind_page(sc, mem->am_offset + i);
   1456       1.94  riastrad 		mem->am_offset = 0;
   1457       1.94  riastrad 		break;
   1458       1.86  riastrad 	default:
   1459       1.86  riastrad 		panic("invalid agp i810 memory type: %d", mem->am_type);
   1460        1.5  drochner 	}
   1461        1.1      fvdl 
   1462       1.13  drochner 	mem->am_is_bound = 0;
   1463        1.1      fvdl 	return 0;
   1464        1.1      fvdl }
   1465       1.24  jmcneill 
   1466       1.47  jmcneill static bool
   1467       1.66    dyoung agp_i810_resume(device_t dv, const pmf_qual_t *qual)
   1468       1.24  jmcneill {
   1469       1.47  jmcneill 	struct agp_softc *sc = device_private(dv);
   1470       1.24  jmcneill 	struct agp_i810_softc *isc = sc->as_chipc;
   1471       1.24  jmcneill 
   1472       1.79  riastrad 	/*
   1473       1.85  riastrad 	 * XXX Nothing uses this!  Save on suspend, restore on resume?
   1474       1.79  riastrad 	 */
   1475       1.85  riastrad 	isc->pgtblctl_resume_hack = READ4(AGP_I810_PGTBL_CTL);
   1476       1.47  jmcneill 	agp_flush_cache();
   1477       1.24  jmcneill 
   1478       1.47  jmcneill 	return true;
   1479       1.24  jmcneill }
   1480