Home | History | Annotate | Line # | Download | only in pci
agp_i810.c revision 1.24
      1  1.24  jmcneill /*	$NetBSD: agp_i810.c,v 1.24 2005/01/27 02:43:42 jmcneill 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.24  jmcneill __KERNEL_RCSID(0, "$NetBSD: agp_i810.c,v 1.24 2005/01/27 02:43:42 jmcneill 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.1      fvdl #include <machine/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.1      fvdl 
     62  1.14       scw #define CHIP_I810 0	/* i810/i815 */
     63  1.17   hannken #define CHIP_I830 1	/* 830M/845G */
     64  1.17   hannken #define CHIP_I855 2	/* 852GM/855GM/865G */
     65  1.14       scw 
     66   1.1      fvdl struct agp_i810_softc {
     67   1.1      fvdl 	u_int32_t initial_aperture;	/* aperture size at startup */
     68   1.1      fvdl 	struct agp_gatt *gatt;
     69  1.14       scw 	int chiptype;			/* i810-like or i830 */
     70  1.14       scw 	u_int32_t dcache_size;		/* i810 only */
     71  1.14       scw 	u_int32_t stolen;		/* number of i830/845 gtt entries
     72  1.14       scw 					   for stolen memory */
     73   1.1      fvdl 	bus_space_tag_t bst;		/* bus_space tag */
     74   1.1      fvdl 	bus_space_handle_t bsh;		/* bus_space handle */
     75   1.1      fvdl 	struct pci_attach_args vga_pa;
     76  1.24  jmcneill 
     77  1.24  jmcneill 	void *sc_powerhook;
     78  1.24  jmcneill 	struct pci_conf_state sc_pciconf;
     79   1.1      fvdl };
     80   1.1      fvdl 
     81   1.1      fvdl static u_int32_t agp_i810_get_aperture(struct agp_softc *);
     82   1.1      fvdl static int agp_i810_set_aperture(struct agp_softc *, u_int32_t);
     83   1.1      fvdl static int agp_i810_bind_page(struct agp_softc *, off_t, bus_addr_t);
     84   1.1      fvdl static int agp_i810_unbind_page(struct agp_softc *, off_t);
     85   1.1      fvdl static void agp_i810_flush_tlb(struct agp_softc *);
     86   1.1      fvdl static int agp_i810_enable(struct agp_softc *, u_int32_t mode);
     87   1.1      fvdl static struct agp_memory *agp_i810_alloc_memory(struct agp_softc *, int,
     88   1.1      fvdl 						vsize_t);
     89   1.1      fvdl static int agp_i810_free_memory(struct agp_softc *, struct agp_memory *);
     90   1.1      fvdl static int agp_i810_bind_memory(struct agp_softc *, struct agp_memory *, off_t);
     91   1.1      fvdl static int agp_i810_unbind_memory(struct agp_softc *, struct agp_memory *);
     92  1.24  jmcneill static void agp_i810_powerhook(int, void *);
     93   1.1      fvdl 
     94   1.1      fvdl struct agp_methods agp_i810_methods = {
     95   1.1      fvdl 	agp_i810_get_aperture,
     96   1.1      fvdl 	agp_i810_set_aperture,
     97   1.1      fvdl 	agp_i810_bind_page,
     98   1.1      fvdl 	agp_i810_unbind_page,
     99   1.1      fvdl 	agp_i810_flush_tlb,
    100   1.1      fvdl 	agp_i810_enable,
    101   1.1      fvdl 	agp_i810_alloc_memory,
    102   1.1      fvdl 	agp_i810_free_memory,
    103   1.1      fvdl 	agp_i810_bind_memory,
    104   1.1      fvdl 	agp_i810_unbind_memory,
    105   1.1      fvdl };
    106   1.1      fvdl 
    107   1.6   thorpej /* XXXthorpej -- duplicated code (see arch/i386/pci/pchb.c) */
    108   1.1      fvdl static int
    109   1.1      fvdl agp_i810_vgamatch(struct pci_attach_args *pa)
    110   1.1      fvdl {
    111   1.6   thorpej 
    112   1.2      fvdl 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
    113   1.2      fvdl 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
    114   1.6   thorpej 		return (0);
    115   1.6   thorpej 
    116   1.1      fvdl 	switch (PCI_PRODUCT(pa->pa_id)) {
    117   1.1      fvdl 	case PCI_PRODUCT_INTEL_82810_GC:
    118   1.1      fvdl 	case PCI_PRODUCT_INTEL_82810_DC100_GC:
    119   1.1      fvdl 	case PCI_PRODUCT_INTEL_82810E_GC:
    120   1.1      fvdl 	case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
    121  1.14       scw 	case PCI_PRODUCT_INTEL_82830MP_IV:
    122  1.14       scw 	case PCI_PRODUCT_INTEL_82845G_IGD:
    123  1.17   hannken 	case PCI_PRODUCT_INTEL_82855GM_IGD:
    124  1.18      tron 	case PCI_PRODUCT_INTEL_82865_IGD:
    125   1.6   thorpej 		return (1);
    126   1.1      fvdl 	}
    127   1.1      fvdl 
    128   1.6   thorpej 	return (0);
    129   1.1      fvdl }
    130   1.1      fvdl 
    131   1.1      fvdl int
    132   1.1      fvdl agp_i810_attach(struct device *parent, struct device *self, void *aux)
    133   1.1      fvdl {
    134   1.1      fvdl 	struct agp_softc *sc = (void *)self;
    135   1.1      fvdl 	struct agp_i810_softc *isc;
    136   1.1      fvdl 	struct agp_gatt *gatt;
    137   1.1      fvdl 	int error;
    138   1.1      fvdl 
    139  1.10   tsutsui 	isc = malloc(sizeof *isc, M_AGP, M_NOWAIT|M_ZERO);
    140   1.1      fvdl 	if (isc == NULL) {
    141  1.15   thorpej 		aprint_error(": can't allocate chipset-specific softc\n");
    142   1.1      fvdl 		return ENOMEM;
    143   1.1      fvdl 	}
    144   1.1      fvdl 	sc->as_chipc = isc;
    145   1.1      fvdl 	sc->as_methods = &agp_i810_methods;
    146   1.1      fvdl 
    147   1.1      fvdl 	if (pci_find_device(&isc->vga_pa, agp_i810_vgamatch) == 0) {
    148  1.20      tron #if NAGP_INTEL > 0
    149  1.19      tron 		const struct pci_attach_args *pa = aux;
    150  1.19      tron 
    151  1.19      tron 		switch (PCI_PRODUCT(pa->pa_id)) {
    152  1.19      tron 		case PCI_PRODUCT_INTEL_82840_HB:
    153  1.19      tron 		case PCI_PRODUCT_INTEL_82865_HB:
    154  1.21      tron 		case PCI_PRODUCT_INTEL_82845G_DRAM:
    155  1.23   xtraeme 		case PCI_PRODUCT_INTEL_82815_FULL_HUB:
    156  1.19      tron 			return agp_intel_attach(parent, self, aux);
    157  1.20      tron 		}
    158  1.20      tron #endif
    159  1.15   thorpej 		aprint_error(": can't find internal VGA device config space\n");
    160   1.1      fvdl 		free(isc, M_AGP);
    161   1.1      fvdl 		return ENOENT;
    162   1.1      fvdl 	}
    163   1.1      fvdl 
    164   1.1      fvdl 	/* XXXfvdl */
    165   1.1      fvdl 	sc->as_dmat = isc->vga_pa.pa_dmat;
    166   1.1      fvdl 
    167   1.1      fvdl 	error = agp_map_aperture(&isc->vga_pa, sc);
    168   1.1      fvdl 	if (error != 0) {
    169  1.15   thorpej 		aprint_error(": can't map aperture\n");
    170   1.1      fvdl 		free(isc, M_AGP);
    171   1.1      fvdl 		return error;
    172   1.1      fvdl 	}
    173   1.1      fvdl 
    174  1.14       scw 	switch (PCI_PRODUCT(isc->vga_pa.pa_id)) {
    175  1.14       scw 	case PCI_PRODUCT_INTEL_82810_GC:
    176  1.14       scw 	case PCI_PRODUCT_INTEL_82810_DC100_GC:
    177  1.14       scw 	case PCI_PRODUCT_INTEL_82810E_GC:
    178  1.14       scw 	case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
    179  1.14       scw 		isc->chiptype = CHIP_I810;
    180  1.14       scw 		break;
    181  1.14       scw 	case PCI_PRODUCT_INTEL_82830MP_IV:
    182  1.14       scw 	case PCI_PRODUCT_INTEL_82845G_IGD:
    183  1.14       scw 		isc->chiptype = CHIP_I830;
    184  1.14       scw 		break;
    185  1.17   hannken 	case PCI_PRODUCT_INTEL_82855GM_IGD:
    186  1.18      tron 	case PCI_PRODUCT_INTEL_82865_IGD:
    187  1.17   hannken 		isc->chiptype = CHIP_I855;
    188  1.17   hannken 		break;
    189  1.14       scw 	}
    190  1.14       scw 
    191   1.1      fvdl 	error = pci_mapreg_map(&isc->vga_pa, AGP_I810_MMADR,
    192   1.1      fvdl 	    PCI_MAPREG_TYPE_MEM, 0, &isc->bst, &isc->bsh, NULL, NULL);
    193   1.1      fvdl 	if (error != 0) {
    194  1.15   thorpej 		aprint_error(": can't map mmadr registers\n");
    195   1.1      fvdl 		return error;
    196   1.1      fvdl 	}
    197   1.1      fvdl 
    198   1.1      fvdl 	isc->initial_aperture = AGP_GET_APERTURE(sc);
    199   1.1      fvdl 
    200  1.14       scw 	gatt = malloc(sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
    201  1.14       scw 	if (!gatt) {
    202  1.14       scw  		agp_generic_detach(sc);
    203  1.14       scw  		return ENOMEM;
    204  1.14       scw 	}
    205  1.14       scw 	isc->gatt = gatt;
    206  1.14       scw 
    207  1.14       scw 	gatt->ag_entries = AGP_GET_APERTURE(sc) >> AGP_PAGE_SHIFT;
    208   1.1      fvdl 
    209  1.14       scw 	if (isc->chiptype == CHIP_I810) {
    210  1.14       scw 		int dummyseg;
    211  1.14       scw 		/* Some i810s have on-chip memory called dcache */
    212  1.14       scw 		if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
    213  1.14       scw 			isc->dcache_size = 4 * 1024 * 1024;
    214  1.14       scw 		else
    215  1.14       scw 			isc->dcache_size = 0;
    216  1.14       scw 
    217  1.14       scw 		/* According to the specs the gatt on the i810 must be 64k */
    218  1.14       scw 		if (agp_alloc_dmamem(sc->as_dmat, 64 * 1024,
    219  1.14       scw 		    0, &gatt->ag_dmamap, (caddr_t *)&gatt->ag_virtual,
    220  1.14       scw 		    &gatt->ag_physical, &gatt->ag_dmaseg, 1, &dummyseg) != 0) {
    221  1.14       scw 			free(gatt, M_AGP);
    222   1.1      fvdl 			agp_generic_detach(sc);
    223   1.1      fvdl 			return ENOMEM;
    224   1.1      fvdl 		}
    225  1.14       scw 
    226  1.14       scw 		gatt->ag_size = gatt->ag_entries * sizeof(u_int32_t);
    227  1.14       scw 		memset(gatt->ag_virtual, 0, gatt->ag_size);
    228  1.14       scw 
    229  1.14       scw 		agp_flush_cache();
    230  1.14       scw 		/* Install the GATT. */
    231  1.14       scw 		WRITE4(AGP_I810_PGTBL_CTL, gatt->ag_physical | 1);
    232  1.17   hannken 	} else if (isc->chiptype == CHIP_I830) {
    233  1.14       scw 		/* The i830 automatically initializes the 128k gatt on boot. */
    234  1.14       scw 		pcireg_t reg;
    235  1.14       scw 		u_int32_t pgtblctl;
    236  1.14       scw 		u_int16_t gcc1;
    237  1.14       scw 
    238  1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
    239  1.14       scw 		gcc1 = (u_int16_t)(reg >> 16);
    240  1.14       scw 		switch (gcc1 & AGP_I830_GCC1_GMS) {
    241  1.14       scw 		case AGP_I830_GCC1_GMS_STOLEN_512:
    242  1.14       scw 			isc->stolen = (512 - 132) * 1024 / 4096;
    243  1.14       scw 			break;
    244  1.14       scw 		case AGP_I830_GCC1_GMS_STOLEN_1024:
    245  1.14       scw 			isc->stolen = (1024 - 132) * 1024 / 4096;
    246  1.14       scw 			break;
    247  1.14       scw 		case AGP_I830_GCC1_GMS_STOLEN_8192:
    248  1.14       scw 			isc->stolen = (8192 - 132) * 1024 / 4096;
    249  1.14       scw 			break;
    250  1.14       scw 		default:
    251  1.14       scw 			isc->stolen = 0;
    252  1.15   thorpej 			aprint_error(
    253  1.15   thorpej 			    ": unknown memory configuration, disabling\n");
    254  1.14       scw 			agp_generic_detach(sc);
    255  1.14       scw 			return EINVAL;
    256  1.14       scw 		}
    257  1.14       scw 		if (isc->stolen > 0) {
    258  1.17   hannken 			aprint_error(": detected %dk stolen memory\n%s",
    259  1.17   hannken 			    isc->stolen * 4, sc->as_dev.dv_xname);
    260  1.14       scw 		}
    261  1.17   hannken 
    262  1.17   hannken 		/* GATT address is already in there, make sure it's enabled */
    263  1.17   hannken 		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
    264  1.17   hannken 		pgtblctl |= 1;
    265  1.17   hannken 		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
    266  1.17   hannken 
    267  1.17   hannken 		gatt->ag_physical = pgtblctl & ~1;
    268  1.17   hannken 	} else {	/* CHIP_I855 */
    269  1.17   hannken 		/* The 855GM automatically initializes the 128k gatt on boot. */
    270  1.17   hannken 		pcireg_t reg;
    271  1.17   hannken 		u_int32_t pgtblctl;
    272  1.17   hannken 		u_int16_t gcc1;
    273  1.17   hannken 
    274  1.17   hannken 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I855_GCC1);
    275  1.17   hannken 		gcc1 = (u_int16_t)(reg >> 16);
    276  1.17   hannken 		switch (gcc1 & AGP_I855_GCC1_GMS) {
    277  1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_1M:
    278  1.17   hannken 			isc->stolen = (1024 - 132) * 1024 / 4096;
    279  1.17   hannken 			break;
    280  1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_4M:
    281  1.17   hannken 			isc->stolen = (4096 - 132) * 1024 / 4096;
    282  1.17   hannken 			break;
    283  1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_8M:
    284  1.17   hannken 			isc->stolen = (8192 - 132) * 1024 / 4096;
    285  1.17   hannken 			break;
    286  1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_16M:
    287  1.17   hannken 			isc->stolen = (16384 - 132) * 1024 / 4096;
    288  1.17   hannken 			break;
    289  1.17   hannken 		case AGP_I855_GCC1_GMS_STOLEN_32M:
    290  1.17   hannken 			isc->stolen = (32768 - 132) * 1024 / 4096;
    291  1.17   hannken 			break;
    292  1.17   hannken 		default:
    293  1.17   hannken 			isc->stolen = 0;
    294  1.17   hannken 			aprint_error(
    295  1.17   hannken 			    ": unknown memory configuration, disabling\n");
    296  1.17   hannken 			agp_generic_detach(sc);
    297  1.17   hannken 			return EINVAL;
    298  1.17   hannken 		}
    299  1.17   hannken 		if (isc->stolen > 0) {
    300  1.17   hannken 			aprint_error(": detected %dk stolen memory\n%s",
    301  1.17   hannken 			    isc->stolen * 4, sc->as_dev.dv_xname);
    302  1.17   hannken 		}
    303  1.14       scw 
    304  1.14       scw 		/* GATT address is already in there, make sure it's enabled */
    305  1.14       scw 		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
    306  1.14       scw 		pgtblctl |= 1;
    307  1.14       scw 		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
    308  1.14       scw 
    309  1.14       scw 		gatt->ag_physical = pgtblctl & ~1;
    310   1.1      fvdl 	}
    311   1.1      fvdl 
    312   1.1      fvdl 	/*
    313   1.1      fvdl 	 * Make sure the chipset can see everything.
    314   1.1      fvdl 	 */
    315   1.1      fvdl 	agp_flush_cache();
    316  1.14       scw 
    317  1.24  jmcneill 	isc->sc_powerhook = powerhook_establish(agp_i810_powerhook, sc);
    318  1.24  jmcneill 	if (isc->sc_powerhook == NULL)
    319  1.24  jmcneill 		printf("%s: WARNING: unable to establish PCI power hook\n",
    320  1.24  jmcneill 		    sc->as_dev.dv_xname);
    321  1.24  jmcneill 
    322   1.1      fvdl 	return 0;
    323   1.1      fvdl }
    324   1.1      fvdl 
    325   1.1      fvdl #if 0
    326   1.1      fvdl static int
    327   1.1      fvdl agp_i810_detach(struct agp_softc *sc)
    328   1.1      fvdl {
    329   1.1      fvdl 	int error;
    330   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    331   1.1      fvdl 
    332   1.1      fvdl 	error = agp_generic_detach(sc);
    333   1.1      fvdl 	if (error)
    334   1.1      fvdl 		return error;
    335   1.1      fvdl 
    336   1.1      fvdl 	/* Clear the GATT base. */
    337  1.14       scw 	if (sc->chiptype == CHIP_I810) {
    338  1.14       scw 		WRITE4(AGP_I810_PGTBL_CTL, 0);
    339  1.14       scw 	} else {
    340  1.14       scw 		unsigned int pgtblctl;
    341  1.14       scw 		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
    342  1.14       scw 		pgtblctl &= ~1;
    343  1.14       scw 		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
    344  1.14       scw 	}
    345   1.1      fvdl 
    346   1.1      fvdl 	/* Put the aperture back the way it started. */
    347   1.1      fvdl 	AGP_SET_APERTURE(sc, isc->initial_aperture);
    348   1.1      fvdl 
    349  1.14       scw 	if (sc->chiptype == CHIP_I810) {
    350  1.14       scw 		agp_free_dmamem(sc->as_dmat, gatt->ag_size, gatt->ag_dmamap,
    351  1.14       scw 		    (caddr_t)gatt->ag_virtual, &gatt->ag_dmaseg, 1);
    352  1.14       scw 	}
    353  1.14       scw 	free(sc->gatt, M_AGP);
    354   1.1      fvdl 
    355   1.1      fvdl 	return 0;
    356   1.1      fvdl }
    357   1.1      fvdl #endif
    358   1.1      fvdl 
    359   1.1      fvdl static u_int32_t
    360   1.1      fvdl agp_i810_get_aperture(struct agp_softc *sc)
    361   1.1      fvdl {
    362  1.14       scw 	struct agp_i810_softc *isc = sc->as_chipc;
    363  1.14       scw 	pcireg_t reg;
    364  1.14       scw 
    365  1.14       scw 	if (isc->chiptype == CHIP_I810) {
    366  1.14       scw 		u_int16_t miscc;
    367   1.1      fvdl 
    368  1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
    369  1.14       scw 		miscc = (u_int16_t)(reg >> 16);
    370  1.14       scw 		if ((miscc & AGP_I810_MISCC_WINSIZE) ==
    371  1.14       scw 		    AGP_I810_MISCC_WINSIZE_32)
    372  1.14       scw 			return 32 * 1024 * 1024;
    373  1.14       scw 		else
    374  1.14       scw 			return 64 * 1024 * 1024;
    375  1.17   hannken 	} else if (isc->chiptype == CHIP_I830) {
    376  1.14       scw 		u_int16_t gcc1;
    377  1.14       scw 
    378  1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
    379  1.14       scw 		gcc1 = (u_int16_t)(reg >> 16);
    380  1.14       scw 		if ((gcc1 & AGP_I830_GCC1_GMASIZE) == AGP_I830_GCC1_GMASIZE_64)
    381  1.14       scw 			return 64 * 1024 * 1024;
    382  1.14       scw 		else
    383  1.14       scw 			return 128 * 1024 * 1024;
    384  1.17   hannken 	} else {	/* CHIP_I855 */
    385  1.17   hannken 		return 128 * 1024 * 1024;
    386  1.14       scw 	}
    387   1.1      fvdl }
    388   1.1      fvdl 
    389   1.1      fvdl static int
    390   1.1      fvdl agp_i810_set_aperture(struct agp_softc *sc, u_int32_t aperture)
    391   1.1      fvdl {
    392  1.14       scw 	struct agp_i810_softc *isc = sc->as_chipc;
    393  1.14       scw 	pcireg_t reg;
    394  1.14       scw 
    395  1.14       scw 	if (isc->chiptype == CHIP_I810) {
    396  1.14       scw 		u_int16_t miscc;
    397  1.14       scw 
    398  1.14       scw 		/*
    399  1.14       scw 		 * Double check for sanity.
    400  1.14       scw 		 */
    401  1.14       scw 		if (aperture != (32 * 1024 * 1024) &&
    402  1.14       scw 		    aperture != (64 * 1024 * 1024)) {
    403  1.14       scw 			printf("%s: bad aperture size %d\n",
    404  1.14       scw 			    sc->as_dev.dv_xname, aperture);
    405  1.14       scw 			return EINVAL;
    406  1.14       scw 		}
    407   1.1      fvdl 
    408  1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
    409  1.14       scw 		miscc = (u_int16_t)(reg >> 16);
    410  1.14       scw 		miscc &= ~AGP_I810_MISCC_WINSIZE;
    411  1.14       scw 		if (aperture == 32 * 1024 * 1024)
    412  1.14       scw 			miscc |= AGP_I810_MISCC_WINSIZE_32;
    413  1.14       scw 		else
    414  1.14       scw 			miscc |= AGP_I810_MISCC_WINSIZE_64;
    415  1.14       scw 
    416  1.14       scw 		reg &= 0x0000ffff;
    417  1.14       scw 		reg |= ((pcireg_t)miscc) << 16;
    418  1.14       scw 		pci_conf_write(sc->as_pc, sc->as_tag, AGP_I810_SMRAM, reg);
    419  1.17   hannken 	} if (isc->chiptype == CHIP_I830) {
    420  1.14       scw 		u_int16_t gcc1;
    421  1.14       scw 
    422  1.14       scw 		if (aperture != (64 * 1024 * 1024) &&
    423  1.14       scw 		    aperture != (128 * 1024 * 1024)) {
    424  1.14       scw 			printf("%s: bad aperture size %d\n",
    425  1.14       scw 			    sc->as_dev.dv_xname, aperture);
    426  1.14       scw 			return EINVAL;
    427  1.14       scw 		}
    428  1.14       scw 		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
    429  1.14       scw 		gcc1 = (u_int16_t)(reg >> 16);
    430  1.14       scw 		gcc1 &= ~AGP_I830_GCC1_GMASIZE;
    431  1.14       scw 		if (aperture == 64 * 1024 * 1024)
    432  1.14       scw 			gcc1 |= AGP_I830_GCC1_GMASIZE_64;
    433  1.14       scw 		else
    434  1.14       scw 			gcc1 |= AGP_I830_GCC1_GMASIZE_128;
    435  1.14       scw 
    436  1.14       scw 		reg &= 0x0000ffff;
    437  1.14       scw 		reg |= ((pcireg_t)gcc1) << 16;
    438  1.14       scw 		pci_conf_write(sc->as_pc, sc->as_tag, AGP_I830_GCC0, reg);
    439  1.17   hannken 	} else {	/* CHIP_I855 */
    440  1.17   hannken 		if (aperture != 128 * 1024 * 1024) {
    441  1.17   hannken 			printf("%s: bad aperture size %d\n",
    442  1.17   hannken 			    sc->as_dev.dv_xname, aperture);
    443  1.17   hannken 			return EINVAL;
    444  1.17   hannken 		}
    445   1.1      fvdl 	}
    446   1.1      fvdl 
    447   1.1      fvdl 	return 0;
    448   1.1      fvdl }
    449   1.1      fvdl 
    450   1.1      fvdl static int
    451   1.1      fvdl agp_i810_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
    452   1.1      fvdl {
    453   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    454   1.1      fvdl 
    455  1.14       scw 	if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
    456  1.14       scw #ifdef DEBUG
    457  1.14       scw 		printf("%s: failed: offset 0x%08x, shift %d, entries %d\n",
    458  1.14       scw 		    sc->as_dev.dv_xname, (int)offset, AGP_PAGE_SHIFT,
    459  1.14       scw 		    isc->gatt->ag_entries);
    460  1.14       scw #endif
    461   1.1      fvdl 		return EINVAL;
    462  1.14       scw 	}
    463  1.14       scw 
    464  1.17   hannken 	if (isc->chiptype != CHIP_I830) {
    465  1.14       scw 		if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
    466  1.14       scw #ifdef DEBUG
    467  1.14       scw 			printf("%s: trying to bind into stolen memory",
    468  1.14       scw 			    sc->as_dev.dv_xname);
    469  1.14       scw #endif
    470  1.14       scw 			return EINVAL;
    471  1.14       scw 		}
    472  1.14       scw 	}
    473   1.1      fvdl 
    474   1.1      fvdl 	WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
    475   1.1      fvdl 	    physical | 1);
    476   1.1      fvdl 	return 0;
    477   1.1      fvdl }
    478   1.1      fvdl 
    479   1.1      fvdl static int
    480   1.1      fvdl agp_i810_unbind_page(struct agp_softc *sc, off_t offset)
    481   1.1      fvdl {
    482   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    483   1.1      fvdl 
    484   1.1      fvdl 	if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
    485   1.1      fvdl 		return EINVAL;
    486   1.1      fvdl 
    487  1.17   hannken 	if (isc->chiptype != CHIP_I810 ) {
    488  1.14       scw 		if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
    489  1.14       scw #ifdef DEBUG
    490  1.14       scw 			printf("%s: trying to unbind from stolen memory",
    491  1.14       scw 			    sc->as_dev.dv_xname);
    492  1.14       scw #endif
    493  1.14       scw 			return EINVAL;
    494  1.14       scw 		}
    495  1.14       scw 	}
    496  1.14       scw 
    497   1.1      fvdl 	WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4, 0);
    498   1.1      fvdl 	return 0;
    499   1.1      fvdl }
    500   1.1      fvdl 
    501   1.1      fvdl /*
    502   1.1      fvdl  * Writing via memory mapped registers already flushes all TLBs.
    503   1.1      fvdl  */
    504   1.1      fvdl static void
    505   1.1      fvdl agp_i810_flush_tlb(struct agp_softc *sc)
    506   1.1      fvdl {
    507   1.1      fvdl }
    508   1.1      fvdl 
    509   1.1      fvdl static int
    510   1.1      fvdl agp_i810_enable(struct agp_softc *sc, u_int32_t mode)
    511   1.1      fvdl {
    512   1.1      fvdl 
    513   1.1      fvdl 	return 0;
    514   1.1      fvdl }
    515   1.1      fvdl 
    516   1.1      fvdl static struct agp_memory *
    517   1.1      fvdl agp_i810_alloc_memory(struct agp_softc *sc, int type, vsize_t size)
    518   1.1      fvdl {
    519   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    520   1.1      fvdl 	struct agp_memory *mem;
    521   1.1      fvdl 
    522   1.1      fvdl 	if ((size & (AGP_PAGE_SIZE - 1)) != 0)
    523   1.1      fvdl 		return 0;
    524   1.1      fvdl 
    525   1.1      fvdl 	if (sc->as_allocated + size > sc->as_maxmem)
    526   1.1      fvdl 		return 0;
    527   1.1      fvdl 
    528   1.1      fvdl 	if (type == 1) {
    529   1.1      fvdl 		/*
    530   1.1      fvdl 		 * Mapping local DRAM into GATT.
    531   1.1      fvdl 		 */
    532  1.17   hannken 		if (isc->chiptype != CHIP_I810 )
    533  1.14       scw 			return 0;
    534   1.1      fvdl 		if (size != isc->dcache_size)
    535   1.1      fvdl 			return 0;
    536   1.1      fvdl 	} else if (type == 2) {
    537   1.1      fvdl 		/*
    538   1.1      fvdl 		 * Bogus mapping of a single page for the hardware cursor.
    539   1.1      fvdl 		 */
    540   1.1      fvdl 		if (size != AGP_PAGE_SIZE)
    541   1.1      fvdl 			return 0;
    542   1.1      fvdl 	}
    543   1.1      fvdl 
    544  1.10   tsutsui 	mem = malloc(sizeof *mem, M_AGP, M_WAITOK|M_ZERO);
    545   1.1      fvdl 	if (mem == NULL)
    546   1.1      fvdl 		return NULL;
    547   1.1      fvdl 	mem->am_id = sc->as_nextid++;
    548   1.1      fvdl 	mem->am_size = size;
    549   1.1      fvdl 	mem->am_type = type;
    550   1.1      fvdl 
    551   1.1      fvdl 	if (type == 2) {
    552   1.1      fvdl 		/*
    553   1.1      fvdl 		 * Allocate and wire down the page now so that we can
    554   1.1      fvdl 		 * get its physical address.
    555   1.1      fvdl 		 */
    556   1.1      fvdl 		mem->am_dmaseg = malloc(sizeof *mem->am_dmaseg, M_AGP,
    557   1.1      fvdl 		    M_WAITOK);
    558   1.1      fvdl 		if (mem->am_dmaseg == NULL) {
    559   1.1      fvdl 			free(mem, M_AGP);
    560   1.1      fvdl 			return NULL;
    561   1.1      fvdl 		}
    562   1.1      fvdl 		if (agp_alloc_dmamem(sc->as_dmat, size, 0,
    563   1.1      fvdl 		    &mem->am_dmamap, &mem->am_virtual, &mem->am_physical,
    564   1.1      fvdl 		    mem->am_dmaseg, 1, &mem->am_nseg) != 0) {
    565   1.1      fvdl 			free(mem->am_dmaseg, M_AGP);
    566   1.1      fvdl 			free(mem, M_AGP);
    567   1.1      fvdl 			return NULL;
    568   1.1      fvdl 		}
    569   1.1      fvdl 	} else if (type != 1) {
    570   1.4  drochner 		if (bus_dmamap_create(sc->as_dmat, size, size / PAGE_SIZE + 1,
    571   1.4  drochner 				      size, 0, BUS_DMA_NOWAIT,
    572   1.4  drochner 				      &mem->am_dmamap) != 0) {
    573   1.1      fvdl 			free(mem, M_AGP);
    574   1.1      fvdl 			return NULL;
    575   1.1      fvdl 		}
    576   1.1      fvdl 	}
    577   1.1      fvdl 
    578   1.1      fvdl 	TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
    579   1.1      fvdl 	sc->as_allocated += size;
    580   1.1      fvdl 
    581   1.1      fvdl 	return mem;
    582   1.1      fvdl }
    583   1.1      fvdl 
    584   1.1      fvdl static int
    585   1.1      fvdl agp_i810_free_memory(struct agp_softc *sc, struct agp_memory *mem)
    586   1.1      fvdl {
    587   1.1      fvdl 	if (mem->am_is_bound)
    588   1.1      fvdl 		return EBUSY;
    589   1.1      fvdl 
    590   1.1      fvdl 	if (mem->am_type == 2) {
    591   1.1      fvdl 		agp_free_dmamem(sc->as_dmat, mem->am_size, mem->am_dmamap,
    592   1.1      fvdl 		    mem->am_virtual, mem->am_dmaseg, mem->am_nseg);
    593   1.1      fvdl 		free(mem->am_dmaseg, M_AGP);
    594   1.1      fvdl 	}
    595   1.1      fvdl 
    596   1.1      fvdl 	sc->as_allocated -= mem->am_size;
    597   1.1      fvdl 	TAILQ_REMOVE(&sc->as_memory, mem, am_link);
    598   1.1      fvdl 	free(mem, M_AGP);
    599   1.1      fvdl 	return 0;
    600   1.1      fvdl }
    601   1.1      fvdl 
    602   1.1      fvdl static int
    603   1.1      fvdl agp_i810_bind_memory(struct agp_softc *sc, struct agp_memory *mem,
    604   1.1      fvdl 		     off_t offset)
    605   1.1      fvdl {
    606   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    607   1.4  drochner 	u_int32_t regval, i;
    608   1.4  drochner 
    609   1.4  drochner 	/*
    610   1.4  drochner 	 * XXX evil hack: the PGTBL_CTL appearently gets overwritten by the
    611   1.4  drochner 	 * X server for mysterious reasons which leads to crashes if we write
    612   1.4  drochner 	 * to the GTT through the MMIO window.
    613   1.4  drochner 	 * Until the issue is solved, simply restore it.
    614   1.4  drochner 	 */
    615   1.4  drochner 	regval = bus_space_read_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL);
    616   1.4  drochner 	if (regval != (isc->gatt->ag_physical | 1)) {
    617   1.4  drochner 		printf("agp_i810_bind_memory: PGTBL_CTL is 0x%x - fixing\n",
    618   1.4  drochner 		       regval);
    619   1.4  drochner 		bus_space_write_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL,
    620   1.4  drochner 				  isc->gatt->ag_physical | 1);
    621   1.4  drochner 	}
    622   1.1      fvdl 
    623   1.5  drochner 	if (mem->am_type == 2) {
    624   1.5  drochner 		WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
    625   1.5  drochner 		       mem->am_physical | 1);
    626   1.5  drochner 		mem->am_offset = offset;
    627   1.5  drochner 		mem->am_is_bound = 1;
    628   1.1      fvdl 		return 0;
    629   1.5  drochner 	}
    630   1.5  drochner 
    631   1.1      fvdl 	if (mem->am_type != 1)
    632   1.1      fvdl 		return agp_generic_bind_memory(sc, mem, offset);
    633   1.1      fvdl 
    634  1.17   hannken 	if (isc->chiptype != CHIP_I810)
    635  1.14       scw 		return EINVAL;
    636  1.14       scw 
    637   1.1      fvdl 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
    638   1.1      fvdl 		WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
    639   1.1      fvdl 		       i | 3);
    640   1.1      fvdl 	}
    641  1.13  drochner 	mem->am_is_bound = 1;
    642   1.1      fvdl 	return 0;
    643   1.1      fvdl }
    644   1.1      fvdl 
    645   1.1      fvdl static int
    646   1.1      fvdl agp_i810_unbind_memory(struct agp_softc *sc, struct agp_memory *mem)
    647   1.1      fvdl {
    648   1.1      fvdl 	struct agp_i810_softc *isc = sc->as_chipc;
    649   1.1      fvdl 	u_int32_t i;
    650   1.1      fvdl 
    651   1.5  drochner 	if (mem->am_type == 2) {
    652   1.5  drochner 		WRITE4(AGP_I810_GTT +
    653   1.5  drochner 		       (u_int32_t)(mem->am_offset >> AGP_PAGE_SHIFT) * 4,
    654   1.5  drochner 		       0);
    655   1.5  drochner 		mem->am_offset = 0;
    656   1.5  drochner 		mem->am_is_bound = 0;
    657   1.1      fvdl 		return 0;
    658   1.5  drochner 	}
    659   1.1      fvdl 
    660   1.1      fvdl 	if (mem->am_type != 1)
    661   1.1      fvdl 		return agp_generic_unbind_memory(sc, mem);
    662  1.14       scw 
    663  1.17   hannken 	if (isc->chiptype != CHIP_I810)
    664  1.14       scw 		return EINVAL;
    665   1.1      fvdl 
    666   1.1      fvdl 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
    667   1.1      fvdl 		WRITE4(AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0);
    668  1.13  drochner 	mem->am_is_bound = 0;
    669   1.1      fvdl 	return 0;
    670   1.1      fvdl }
    671  1.24  jmcneill 
    672  1.24  jmcneill static void
    673  1.24  jmcneill agp_i810_powerhook(int why, void *arg)
    674  1.24  jmcneill {
    675  1.24  jmcneill 	struct agp_softc *sc = (struct agp_softc *)arg;
    676  1.24  jmcneill 	struct agp_i810_softc *isc = sc->as_chipc;
    677  1.24  jmcneill 
    678  1.24  jmcneill 	if (why == PWR_RESUME) {
    679  1.24  jmcneill 		pci_conf_restore(sc->as_pc, sc->as_tag, &isc->sc_pciconf);
    680  1.24  jmcneill 		agp_flush_cache();
    681  1.24  jmcneill 	} else if ((why == PWR_STANDBY) || (why == PWR_SUSPEND))
    682  1.24  jmcneill 		pci_conf_capture(sc->as_pc, sc->as_tag, &isc->sc_pciconf);
    683  1.24  jmcneill 
    684  1.24  jmcneill 	return;
    685  1.24  jmcneill }
    686