Home | History | Annotate | Line # | Download | only in pci
agp_sis.c revision 1.14
      1 /*	$NetBSD: agp_sis.c,v 1.14 2010/11/13 13:52:05 uebayasi 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_sis.c,v 1.3 2001/07/05 21:28:47 jhb Exp $
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: agp_sis.c,v 1.14 2010/11/13 13:52:05 uebayasi 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/proc.h>
     39 #include <sys/conf.h>
     40 #include <sys/device.h>
     41 #include <sys/agpio.h>
     42 
     43 #include <dev/pci/pcivar.h>
     44 #include <dev/pci/pcireg.h>
     45 #include <dev/pci/agpvar.h>
     46 #include <dev/pci/agpreg.h>
     47 
     48 #include <sys/bus.h>
     49 
     50 struct agp_sis_softc {
     51 	u_int32_t	initial_aperture; /* aperture size at startup */
     52 	struct agp_gatt *gatt;
     53 };
     54 
     55 static u_int32_t agp_sis_get_aperture(struct agp_softc *);
     56 static int agp_sis_set_aperture(struct agp_softc *, u_int32_t);
     57 static int agp_sis_bind_page(struct agp_softc *, off_t, bus_addr_t);
     58 static int agp_sis_unbind_page(struct agp_softc *, off_t);
     59 static void agp_sis_flush_tlb(struct agp_softc *);
     60 
     61 static struct agp_methods agp_sis_methods = {
     62 	agp_sis_get_aperture,
     63 	agp_sis_set_aperture,
     64 	agp_sis_bind_page,
     65 	agp_sis_unbind_page,
     66 	agp_sis_flush_tlb,
     67 	agp_generic_enable,
     68 	agp_generic_alloc_memory,
     69 	agp_generic_free_memory,
     70 	agp_generic_bind_memory,
     71 	agp_generic_unbind_memory,
     72 };
     73 
     74 int
     75 agp_sis_attach(device_t parent, device_t self, void *aux)
     76 {
     77 	struct agp_softc *sc = device_private(self);
     78 	struct pci_attach_args *pa = aux;
     79 	struct agp_sis_softc *ssc;
     80 	struct agp_gatt *gatt;
     81 	pcireg_t reg;
     82 
     83 	ssc = malloc(sizeof *ssc, M_AGP, M_NOWAIT);
     84 	if (ssc == NULL) {
     85 		aprint_error(": can't allocate chipset-specific softc\n");
     86 		return ENOMEM;
     87 	}
     88 	sc->as_methods = &agp_sis_methods;
     89 	sc->as_chipc = ssc;
     90 	pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP, &sc->as_capoff,
     91 	    NULL);
     92 
     93 	if (agp_map_aperture(pa, sc, AGP_APBASE) != 0) {
     94 		aprint_error(": can't map aperture\n");
     95 		free(ssc, M_AGP);
     96 		return ENXIO;
     97 	}
     98 
     99 	ssc->initial_aperture = AGP_GET_APERTURE(sc);
    100 
    101 	for (;;) {
    102 		gatt = agp_alloc_gatt(sc);
    103 		if (gatt)
    104 			break;
    105 
    106 		/*
    107 		 * Probably contigmalloc failure. Try reducing the
    108 		 * aperture so that the gatt size reduces.
    109 		 */
    110 		if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) {
    111 			agp_generic_detach(sc);
    112 			aprint_error(": failed to set aperture\n");
    113 			return ENOMEM;
    114 		}
    115 	}
    116 	ssc->gatt = gatt;
    117 
    118 	/* Install the gatt. */
    119 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_SIS_ATTBASE,
    120 	    gatt->ag_physical);
    121 
    122 	/* Enable the aperture and auto-tlb-inval */
    123 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_SIS_WINCTRL);
    124 	reg |= (0x05 << 24) | 3;
    125 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_SIS_WINCTRL, reg);
    126 
    127 	return 0;
    128 }
    129 
    130 #if 0
    131 static int
    132 agp_sis_detach(struct agp_softc *sc)
    133 {
    134 	struct agp_sis_softc *ssc = sc->as_chipc;
    135 	pcireg_t reg;
    136 	int error;
    137 
    138 	error = agp_generic_detach(sc);
    139 	if (error)
    140 		return error;
    141 
    142 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_SIS_WINCTRL);
    143 	reg &= ~3;
    144 	reg &= 0x00ffffff;
    145 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_SIS_WINCTRL, reg);
    146 
    147 	/* Put the aperture back the way it started. */
    148 	AGP_SET_APERTURE(sc, ssc->initial_aperture);
    149 
    150 	agp_free_gatt(sc, ssc->gatt);
    151 	return 0;
    152 }
    153 #endif
    154 
    155 static u_int32_t
    156 agp_sis_get_aperture(struct agp_softc *sc)
    157 {
    158 	int gws;
    159 
    160 	/*
    161 	 * The aperture size is equal to 4M<<gws.
    162 	 */
    163 	gws = (pci_conf_read(sc->as_pc, sc->as_tag, AGP_SIS_WINCTRL)&0x70) >> 4;
    164 	return (4*1024*1024) << gws;
    165 }
    166 
    167 static int
    168 agp_sis_set_aperture(struct agp_softc *sc, u_int32_t aperture)
    169 {
    170 	int gws;
    171 	pcireg_t reg;
    172 
    173 	/*
    174 	 * Check for a power of two and make sure its within the
    175 	 * programmable range.
    176 	 */
    177 	if (aperture & (aperture - 1)
    178 	    || aperture < 4*1024*1024
    179 	    || aperture > 256*1024*1024)
    180 		return EINVAL;
    181 
    182 	gws = ffs(aperture / 4*1024*1024) - 1;
    183 
    184 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_SIS_WINCTRL);
    185 	reg &= ~0x00000070;
    186 	reg |= gws << 4;
    187 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_SIS_WINCTRL, reg);
    188 
    189 	return 0;
    190 }
    191 
    192 static int
    193 agp_sis_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
    194 {
    195 	struct agp_sis_softc *ssc = sc->as_chipc;
    196 
    197 	if (offset < 0 || offset >= (ssc->gatt->ag_entries << AGP_PAGE_SHIFT))
    198 		return EINVAL;
    199 
    200 	ssc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical;
    201 	return 0;
    202 }
    203 
    204 static int
    205 agp_sis_unbind_page(struct agp_softc *sc, off_t offset)
    206 {
    207 	struct agp_sis_softc *ssc = sc->as_chipc;
    208 
    209 	if (offset < 0 || offset >= (ssc->gatt->ag_entries << AGP_PAGE_SHIFT))
    210 		return EINVAL;
    211 
    212 	ssc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
    213 	return 0;
    214 }
    215 
    216 static void
    217 agp_sis_flush_tlb(struct agp_softc *sc)
    218 {
    219 	pcireg_t reg;
    220 
    221 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_SIS_TLBFLUSH);
    222 	reg &= 0xffffff00;
    223 	reg |= 0x02;
    224 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_SIS_TLBFLUSH, reg);
    225 }
    226