Home | History | Annotate | Line # | Download | only in pci
agp_via.c revision 1.12
      1  1.12  jmcneill /*	$NetBSD: agp_via.c,v 1.12 2007/03/27 00:34:16 jmcneill 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_via.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.12  jmcneill __KERNEL_RCSID(0, "$NetBSD: agp_via.c,v 1.12 2007/03/27 00:34:16 jmcneill 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/conf.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.12  jmcneill #include <dev/pci/pcidevs.h>
     51   1.1      fvdl 
     52   1.1      fvdl #include <machine/bus.h>
     53   1.1      fvdl 
     54   1.1      fvdl static u_int32_t agp_via_get_aperture(struct agp_softc *);
     55   1.1      fvdl static int agp_via_set_aperture(struct agp_softc *, u_int32_t);
     56   1.1      fvdl static int agp_via_bind_page(struct agp_softc *, off_t, bus_addr_t);
     57   1.1      fvdl static int agp_via_unbind_page(struct agp_softc *, off_t);
     58   1.1      fvdl static void agp_via_flush_tlb(struct agp_softc *);
     59   1.1      fvdl 
     60   1.7   thorpej static struct agp_methods agp_via_methods = {
     61   1.1      fvdl 	agp_via_get_aperture,
     62   1.1      fvdl 	agp_via_set_aperture,
     63   1.1      fvdl 	agp_via_bind_page,
     64   1.1      fvdl 	agp_via_unbind_page,
     65   1.1      fvdl 	agp_via_flush_tlb,
     66   1.1      fvdl 	agp_generic_enable,
     67   1.1      fvdl 	agp_generic_alloc_memory,
     68   1.1      fvdl 	agp_generic_free_memory,
     69   1.1      fvdl 	agp_generic_bind_memory,
     70   1.1      fvdl 	agp_generic_unbind_memory,
     71   1.1      fvdl };
     72   1.1      fvdl 
     73   1.1      fvdl struct agp_via_softc {
     74   1.1      fvdl 	u_int32_t	initial_aperture; /* aperture size at startup */
     75   1.1      fvdl 	struct agp_gatt *gatt;
     76  1.12  jmcneill 	int		*regs;
     77  1.12  jmcneill };
     78  1.12  jmcneill 
     79  1.12  jmcneill #define REG_GARTCTRL	0
     80  1.12  jmcneill #define REG_APSIZE	1
     81  1.12  jmcneill #define REG_ATTBASE	2
     82  1.12  jmcneill 
     83  1.12  jmcneill static int via_v2_regs[] =
     84  1.12  jmcneill 	{ AGP_VIA_GARTCTRL, AGP_VIA_APSIZE, AGP_VIA_ATTBASE };
     85  1.12  jmcneill static int via_v3_regs[] =
     86  1.12  jmcneill 	{ AGP3_VIA_GARTCTRL, AGP3_VIA_APSIZE, AGP3_VIA_ATTBASE };
     87  1.12  jmcneill 
     88  1.12  jmcneill static pci_product_id_t via_v3_devices[] = {
     89  1.12  jmcneill 	0x0314,
     90  1.12  jmcneill 	0xffff,
     91   1.1      fvdl };
     92   1.1      fvdl 
     93   1.1      fvdl int
     94  1.11  christos agp_via_attach(struct device *parent, struct device *self, void *aux)
     95   1.1      fvdl {
     96   1.1      fvdl 	struct pci_attach_args *pa = aux;
     97   1.1      fvdl 	struct agp_softc *sc = (void *)self;
     98   1.1      fvdl 	struct agp_via_softc *asc;
     99   1.1      fvdl 	struct agp_gatt *gatt;
    100  1.12  jmcneill 	pcireg_t agpsel;
    101  1.12  jmcneill 	int i;
    102   1.1      fvdl 
    103   1.4   tsutsui 	asc = malloc(sizeof *asc, M_AGP, M_NOWAIT|M_ZERO);
    104   1.1      fvdl 	if (asc == NULL) {
    105   1.5   thorpej 		aprint_error(": can't allocate chipset-specific softc\n");
    106   1.1      fvdl 		return ENOMEM;
    107   1.1      fvdl 	}
    108   1.1      fvdl 	sc->as_chipc = asc;
    109   1.1      fvdl 	sc->as_methods = &agp_via_methods;
    110   1.1      fvdl 	pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP, &sc->as_capoff,
    111   1.1      fvdl 	    NULL);
    112   1.1      fvdl 
    113  1.12  jmcneill 	asc->regs = via_v2_regs;
    114  1.12  jmcneill 	for (i = 0; via_v3_devices[i] != 0xffff; i++) {
    115  1.12  jmcneill 		if (PCI_PRODUCT(pa->pa_id) == via_v3_devices[i]) {
    116  1.12  jmcneill 			agpsel = pci_conf_read(pa->pa_pc, pa->pa_tag,
    117  1.12  jmcneill 			    AGP_VIA_AGPSEL);
    118  1.12  jmcneill 			if ((agpsel & (1 << 1)) == 0) {
    119  1.12  jmcneill 				asc->regs = via_v3_regs;
    120  1.12  jmcneill 				printf(" (v3)");
    121  1.12  jmcneill 			} else {
    122  1.12  jmcneill 				asc->regs = via_v2_regs;
    123  1.12  jmcneill 				printf(" (v2 compat mode)");
    124  1.12  jmcneill 			}
    125  1.12  jmcneill 			break;
    126  1.12  jmcneill 		}
    127  1.12  jmcneill 	}
    128  1.12  jmcneill 
    129   1.9  christos 	if (agp_map_aperture(pa, sc, AGP_APBASE) != 0) {
    130   1.5   thorpej 		aprint_error(": can't map aperture\n");
    131   1.1      fvdl 		free(asc, M_AGP);
    132   1.1      fvdl 		return ENXIO;
    133   1.1      fvdl 	}
    134   1.1      fvdl 
    135   1.1      fvdl 	asc->initial_aperture = AGP_GET_APERTURE(sc);
    136   1.1      fvdl 
    137   1.1      fvdl 	for (;;) {
    138   1.1      fvdl 		gatt = agp_alloc_gatt(sc);
    139   1.1      fvdl 		if (gatt)
    140   1.1      fvdl 			break;
    141   1.1      fvdl 
    142   1.1      fvdl 		/*
    143   1.1      fvdl 		 * Probably contigmalloc failure. Try reducing the
    144   1.1      fvdl 		 * aperture so that the gatt size reduces.
    145   1.1      fvdl 		 */
    146   1.1      fvdl 		if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) {
    147   1.1      fvdl 			agp_generic_detach(sc);
    148   1.5   thorpej 			aprint_error(": can't set aperture size\n");
    149   1.1      fvdl 			return ENOMEM;
    150   1.1      fvdl 		}
    151   1.1      fvdl 	}
    152   1.1      fvdl 	asc->gatt = gatt;
    153   1.1      fvdl 
    154  1.12  jmcneill 	if (asc->regs == via_v2_regs) {
    155  1.12  jmcneill 		/* Install the gatt. */
    156  1.12  jmcneill 		pci_conf_write(pa->pa_pc, pa->pa_tag, asc->regs[REG_ATTBASE],
    157  1.12  jmcneill 				 gatt->ag_physical | 3);
    158  1.12  jmcneill 		/* Enable the aperture. */
    159  1.12  jmcneill 		pci_conf_write(pa->pa_pc, pa->pa_tag, asc->regs[REG_GARTCTRL],
    160  1.12  jmcneill 				 0x0000000f);
    161  1.12  jmcneill 	} else {
    162  1.12  jmcneill 		pcireg_t gartctrl;
    163  1.12  jmcneill 		/* Install the gatt. */
    164  1.12  jmcneill 		pci_conf_write(pa->pa_pc, pa->pa_tag, asc->regs[REG_ATTBASE],
    165  1.12  jmcneill 				 gatt->ag_physical);
    166  1.12  jmcneill 		/* Enable the aperture. */
    167  1.12  jmcneill 		gartctrl = pci_conf_read(pa->pa_pc, pa->pa_tag,
    168  1.12  jmcneill 				 asc->regs[REG_ATTBASE]);
    169  1.12  jmcneill 		pci_conf_write(pa->pa_pc, pa->pa_tag, asc->regs[REG_GARTCTRL],
    170  1.12  jmcneill 				 gartctrl | (3 << 7));
    171  1.12  jmcneill 	}
    172   1.1      fvdl 
    173   1.1      fvdl 	return 0;
    174   1.1      fvdl }
    175   1.1      fvdl 
    176   1.1      fvdl #if 0
    177   1.1      fvdl static int
    178   1.1      fvdl agp_via_detach(struct agp_softc *sc)
    179   1.1      fvdl {
    180   1.1      fvdl 	struct agp_via_softc *asc = sc->as_chipc;
    181   1.1      fvdl 	int error;
    182   1.1      fvdl 
    183   1.1      fvdl 	error = agp_generic_detach(sc);
    184   1.1      fvdl 	if (error)
    185   1.1      fvdl 		return error;
    186   1.1      fvdl 
    187  1.12  jmcneill 	pci_conf_write(sc->as_pc, sc->as_tag, asc->regs[REG_GARTCTRL], 0);
    188  1.12  jmcneill 	pci_conf_write(sc->as_pc, sc->as_tag, asc->regs[REG_ATTBASE], 0);
    189   1.1      fvdl 	AGP_SET_APERTURE(sc, asc->initial_aperture);
    190   1.1      fvdl 	agp_free_gatt(sc, asc->gatt);
    191   1.1      fvdl 
    192   1.1      fvdl 	return 0;
    193   1.1      fvdl }
    194   1.1      fvdl #endif
    195   1.1      fvdl 
    196   1.1      fvdl static u_int32_t
    197   1.1      fvdl agp_via_get_aperture(struct agp_softc *sc)
    198   1.1      fvdl {
    199  1.12  jmcneill 	struct agp_via_softc *asc = sc->as_chipc;
    200   1.1      fvdl 	u_int32_t apsize;
    201   1.1      fvdl 
    202  1.12  jmcneill 	apsize = pci_conf_read(sc->as_pc, sc->as_tag, asc->regs[REG_APSIZE])
    203  1.12  jmcneill 				& 0x1f;
    204   1.1      fvdl 
    205   1.1      fvdl 	/*
    206   1.1      fvdl 	 * The size is determined by the number of low bits of
    207   1.1      fvdl 	 * register APBASE which are forced to zero. The low 20 bits
    208   1.1      fvdl 	 * are always forced to zero and each zero bit in the apsize
    209   1.1      fvdl 	 * field just read forces the corresponding bit in the 27:20
    210   1.1      fvdl 	 * to be zero. We calculate the aperture size accordingly.
    211   1.1      fvdl 	 */
    212   1.1      fvdl 	return (((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1;
    213   1.1      fvdl }
    214   1.1      fvdl 
    215   1.1      fvdl static int
    216   1.1      fvdl agp_via_set_aperture(struct agp_softc *sc, u_int32_t aperture)
    217   1.1      fvdl {
    218  1.12  jmcneill 	struct agp_via_softc *asc = sc->as_chipc;
    219   1.1      fvdl 	u_int32_t apsize;
    220   1.1      fvdl 	pcireg_t reg;
    221   1.1      fvdl 
    222   1.1      fvdl 	/*
    223   1.1      fvdl 	 * Reverse the magic from get_aperture.
    224   1.1      fvdl 	 */
    225   1.1      fvdl 	apsize = ((aperture - 1) >> 20) ^ 0xff;
    226   1.1      fvdl 
    227   1.1      fvdl 	/*
    228   1.1      fvdl 	 * Double check for sanity.
    229   1.1      fvdl 	 */
    230   1.1      fvdl 	if ((((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1 != aperture)
    231   1.1      fvdl 		return EINVAL;
    232   1.1      fvdl 
    233  1.12  jmcneill 	reg = pci_conf_read(sc->as_pc, sc->as_tag, asc->regs[REG_APSIZE]);
    234   1.1      fvdl 	reg &= ~0xff;
    235   1.1      fvdl 	reg |= apsize;
    236  1.12  jmcneill 	pci_conf_write(sc->as_pc, sc->as_tag, asc->regs[REG_APSIZE], reg);
    237   1.1      fvdl 
    238   1.1      fvdl 	return 0;
    239   1.1      fvdl }
    240   1.1      fvdl 
    241   1.1      fvdl static int
    242   1.1      fvdl agp_via_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
    243   1.1      fvdl {
    244   1.1      fvdl 	struct agp_via_softc *asc = sc->as_chipc;
    245   1.1      fvdl 
    246   1.1      fvdl 	if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
    247   1.1      fvdl 		return EINVAL;
    248   1.1      fvdl 
    249   1.1      fvdl 	asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical;
    250   1.1      fvdl 	return 0;
    251   1.1      fvdl }
    252   1.1      fvdl 
    253   1.1      fvdl static int
    254   1.1      fvdl agp_via_unbind_page(struct agp_softc *sc, off_t offset)
    255   1.1      fvdl {
    256   1.1      fvdl 	struct agp_via_softc *asc = sc->as_chipc;
    257   1.1      fvdl 
    258   1.1      fvdl 	if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
    259   1.1      fvdl 		return EINVAL;
    260   1.1      fvdl 
    261   1.1      fvdl 	asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
    262   1.1      fvdl 	return 0;
    263   1.1      fvdl }
    264   1.1      fvdl 
    265   1.1      fvdl static void
    266   1.1      fvdl agp_via_flush_tlb(struct agp_softc *sc)
    267   1.1      fvdl {
    268  1.12  jmcneill 	struct agp_via_softc *asc = sc->as_chipc;
    269  1.12  jmcneill 	pcireg_t gartctrl;
    270  1.12  jmcneill 
    271  1.12  jmcneill 	if (asc->regs == via_v2_regs) {
    272  1.12  jmcneill 		pci_conf_write(sc->as_pc, sc->as_tag, asc->regs[REG_GARTCTRL],
    273  1.12  jmcneill 				0x8f);
    274  1.12  jmcneill 		pci_conf_write(sc->as_pc, sc->as_tag, asc->regs[REG_GARTCTRL],
    275  1.12  jmcneill 				0x0f);
    276  1.12  jmcneill 	} else {
    277  1.12  jmcneill 		gartctrl = pci_conf_read(sc->as_pc, sc->as_tag,
    278  1.12  jmcneill 					 asc->regs[REG_GARTCTRL]);
    279  1.12  jmcneill 		pci_conf_write(sc->as_pc, sc->as_tag, asc->regs[REG_GARTCTRL],
    280  1.12  jmcneill 			       gartctrl & ~(1 << 7));
    281  1.12  jmcneill 		pci_conf_write(sc->as_pc, sc->as_tag, asc->regs[REG_GARTCTRL],
    282  1.12  jmcneill 			       gartctrl);
    283  1.12  jmcneill 	}
    284   1.1      fvdl }
    285