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