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