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