Home | History | Annotate | Line # | Download | only in pci
agp_intel.c revision 1.2.2.3
      1 /*	$NetBSD: agp_intel.c,v 1.2.2.3 2002/01/10 19:56:25 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 Doug Rabson
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  *
     28  *	$FreeBSD: src/sys/pci/agp_intel.c,v 1.4 2001/07/05 21:28:47 jhb Exp $
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: agp_intel.c,v 1.2.2.3 2002/01/10 19:56:25 thorpej Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/malloc.h>
     37 #include <sys/kernel.h>
     38 #include <sys/lock.h>
     39 #include <sys/proc.h>
     40 #include <sys/agpio.h>
     41 #include <sys/device.h>
     42 #include <sys/agpio.h>
     43 
     44 #include <uvm/uvm_extern.h>
     45 
     46 #include <dev/pci/pcivar.h>
     47 #include <dev/pci/pcireg.h>
     48 #include <dev/pci/agpvar.h>
     49 #include <dev/pci/agpreg.h>
     50 
     51 #include <machine/bus.h>
     52 
     53 struct agp_intel_softc {
     54 	u_int32_t	initial_aperture; /* aperture size at startup */
     55 	struct agp_gatt *gatt;
     56 };
     57 
     58 static u_int32_t agp_intel_get_aperture(struct agp_softc *);
     59 static int agp_intel_set_aperture(struct agp_softc *, u_int32_t);
     60 static int agp_intel_bind_page(struct agp_softc *, off_t, bus_addr_t);
     61 static int agp_intel_unbind_page(struct agp_softc *, off_t);
     62 static void agp_intel_flush_tlb(struct agp_softc *);
     63 
     64 struct agp_methods agp_intel_methods = {
     65 	agp_intel_get_aperture,
     66 	agp_intel_set_aperture,
     67 	agp_intel_bind_page,
     68 	agp_intel_unbind_page,
     69 	agp_intel_flush_tlb,
     70 	agp_generic_enable,
     71 	agp_generic_alloc_memory,
     72 	agp_generic_free_memory,
     73 	agp_generic_bind_memory,
     74 	agp_generic_unbind_memory,
     75 };
     76 
     77 int
     78 agp_intel_attach(struct device *parent, struct device *self, void *aux)
     79 {
     80 	struct agp_softc *sc = (struct agp_softc *)self;
     81 	struct pci_attach_args *pa= aux;
     82 	struct agp_intel_softc *isc;
     83 	struct agp_gatt *gatt;
     84 	pcireg_t reg;
     85 
     86 	isc = malloc(sizeof *isc, M_AGP, M_NOWAIT);
     87 	if (isc == NULL) {
     88 		printf(": can't allocate chipset-specific softc\n");
     89 		return ENOMEM;
     90 	}
     91 	memset(isc, 0, sizeof *isc);
     92 
     93 	sc->as_methods = &agp_intel_methods;
     94 	sc->as_chipc = isc;
     95 
     96 	pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP, &sc->as_capoff,
     97 	    NULL);
     98 
     99 	if (agp_map_aperture(pa, sc) != 0) {
    100 		printf(": can't map aperture\n");
    101 		free(isc, M_AGP);
    102 		sc->as_chipc = NULL;
    103 		return ENXIO;
    104 	}
    105 
    106 	isc->initial_aperture = AGP_GET_APERTURE(sc);
    107 
    108 	for (;;) {
    109 		gatt = agp_alloc_gatt(sc);
    110 		if (gatt)
    111 			break;
    112 
    113 		/*
    114 		 * Probably contigmalloc failure. Try reducing the
    115 		 * aperture so that the gatt size reduces.
    116 		 */
    117 		if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) {
    118 			agp_generic_detach(sc);
    119 			printf(": failed to set aperture\n");
    120 			return ENOMEM;
    121 		}
    122 	}
    123 	isc->gatt = gatt;
    124 
    125 	/* Install the gatt. */
    126 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_ATTBASE,
    127 	    gatt->ag_physical);
    128 
    129 	/* Enable things, clear errors etc. */
    130 	/* XXXfvdl get rid of the magic constants */
    131 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_AGPCTRL, 0x2280);
    132 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_INTEL_NBXCFG);
    133 	reg &= ~(1 << 10);
    134 	reg |=  (1 << 9);
    135 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_NBXCFG, reg);
    136 
    137 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_INTEL_STS);
    138 	reg &= ~0x00ff0000;
    139 	reg |= (7 << 16);
    140 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_STS, reg);
    141 
    142 	return 0;
    143 }
    144 
    145 #if 0
    146 static int
    147 agp_intel_detach(struct agp_softc *sc)
    148 {
    149 	int error;
    150 	pcireg_t reg;
    151 	struct agp_intel_softc *isc = sc->as_chipc;
    152 
    153 	error = agp_generic_detach(sc);
    154 	if (error)
    155 		return error;
    156 
    157 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_INTEL_NBXCFG);
    158 	reg &= ~(1 << 9);
    159 	printf("%s: set NBXCFG to %x\n", __FUNCTION__, reg);
    160 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_NBXCFG, reg);
    161 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_ATTBASE, 0);
    162 	AGP_SET_APERTURE(sc, isc->initial_aperture);
    163 	agp_free_gatt(sc, isc->gatt);
    164 
    165 	return 0;
    166 }
    167 #endif
    168 
    169 static u_int32_t
    170 agp_intel_get_aperture(struct agp_softc *sc)
    171 {
    172 	u_int32_t apsize;
    173 
    174 	apsize = pci_conf_read(sc->as_pc, sc->as_tag, AGP_INTEL_APSIZE) & 0x1f;
    175 
    176 	/*
    177 	 * The size is determined by the number of low bits of
    178 	 * register APBASE which are forced to zero. The low 22 bits
    179 	 * are always forced to zero and each zero bit in the apsize
    180 	 * field just read forces the corresponding bit in the 27:22
    181 	 * to be zero. We calculate the aperture size accordingly.
    182 	 */
    183 	return (((apsize ^ 0x1f) << 22) | ((1 << 22) - 1)) + 1;
    184 }
    185 
    186 static int
    187 agp_intel_set_aperture(struct agp_softc *sc, u_int32_t aperture)
    188 {
    189 	u_int32_t apsize;
    190 	pcireg_t reg;
    191 
    192 	/*
    193 	 * Reverse the magic from get_aperture.
    194 	 */
    195 	apsize = ((aperture - 1) >> 22) ^ 0x1f;
    196 
    197 	/*
    198 	 * Double check for sanity.
    199 	 */
    200 	if ((((apsize ^ 0x1f) << 22) | ((1 << 22) - 1)) + 1 != aperture)
    201 		return EINVAL;
    202 
    203 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_INTEL_APSIZE);
    204 	reg = (reg & 0xffffff00) | apsize;
    205 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_APSIZE, reg);
    206 
    207 	return 0;
    208 }
    209 
    210 static int
    211 agp_intel_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
    212 {
    213 	struct agp_intel_softc *isc = sc->as_chipc;
    214 
    215 	if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
    216 		return EINVAL;
    217 
    218 	isc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical | 0x17;
    219 	return 0;
    220 }
    221 
    222 static int
    223 agp_intel_unbind_page(struct agp_softc *sc, off_t offset)
    224 {
    225 	struct agp_intel_softc *isc = sc->as_chipc;
    226 
    227 	if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
    228 		return EINVAL;
    229 
    230 	isc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
    231 	return 0;
    232 }
    233 
    234 static void
    235 agp_intel_flush_tlb(struct agp_softc *sc)
    236 {
    237 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_AGPCTRL, 0x2200);
    238 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_AGPCTRL, 0x2280);
    239 }
    240