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