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