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