Home | History | Annotate | Line # | Download | only in pci
agp_ali.c revision 1.15
      1  1.15     freza /*	$NetBSD: agp_ali.c,v 1.15 2008/06/09 06:49:54 freza 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_ali.c,v 1.3 2001/07/05 21:28:46 jhb Exp $
     29   1.1      fvdl  */
     30   1.3     lukem 
     31   1.3     lukem #include <sys/cdefs.h>
     32  1.15     freza __KERNEL_RCSID(0, "$NetBSD: agp_ali.c,v 1.15 2008/06/09 06:49:54 freza Exp $");
     33   1.1      fvdl 
     34   1.1      fvdl #include <sys/param.h>
     35   1.1      fvdl #include <sys/kernel.h>
     36   1.1      fvdl #include <sys/malloc.h>
     37   1.1      fvdl #include <sys/systm.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 <uvm/uvm_extern.h>
     44   1.1      fvdl 
     45   1.1      fvdl #include <dev/pci/pcivar.h>
     46   1.1      fvdl #include <dev/pci/pcireg.h>
     47   1.1      fvdl #include <dev/pci/agpvar.h>
     48   1.1      fvdl #include <dev/pci/agpreg.h>
     49   1.1      fvdl 
     50  1.12        ad #include <sys/bus.h>
     51   1.1      fvdl 
     52   1.1      fvdl struct agp_ali_softc {
     53   1.1      fvdl 	u_int32_t	initial_aperture; /* aperture size at startup */
     54   1.1      fvdl 	struct agp_gatt *gatt;
     55   1.1      fvdl };
     56   1.1      fvdl 
     57   1.1      fvdl static u_int32_t agp_ali_get_aperture(struct agp_softc *);
     58   1.1      fvdl static int agp_ali_set_aperture(struct agp_softc *sc, u_int32_t);
     59   1.1      fvdl static int agp_ali_bind_page(struct agp_softc *, off_t, bus_addr_t);
     60   1.1      fvdl static int agp_ali_unbind_page(struct agp_softc *, off_t);
     61   1.1      fvdl static void agp_ali_flush_tlb(struct agp_softc *);
     62   1.1      fvdl 
     63   1.1      fvdl 
     64   1.7   thorpej static struct agp_methods agp_ali_methods = {
     65   1.1      fvdl 	agp_ali_get_aperture,
     66   1.1      fvdl 	agp_ali_set_aperture,
     67   1.1      fvdl 	agp_ali_bind_page,
     68   1.1      fvdl 	agp_ali_unbind_page,
     69   1.1      fvdl 	agp_ali_flush_tlb,
     70   1.1      fvdl 	agp_generic_enable,
     71   1.1      fvdl 	agp_generic_alloc_memory,
     72   1.1      fvdl 	agp_generic_free_memory,
     73   1.1      fvdl 	agp_generic_bind_memory,
     74   1.1      fvdl 	agp_generic_unbind_memory,
     75   1.1      fvdl };
     76   1.1      fvdl 
     77   1.1      fvdl int
     78  1.15     freza agp_ali_attach(device_t parent, device_t self, void *aux)
     79   1.1      fvdl {
     80  1.15     freza 	struct agp_softc *sc = device_private(self);
     81   1.1      fvdl 	struct agp_ali_softc *asc;
     82   1.1      fvdl 	struct pci_attach_args *pa = aux;
     83   1.1      fvdl 	struct agp_gatt *gatt;
     84   1.1      fvdl 	pcireg_t reg;
     85   1.1      fvdl 
     86   1.1      fvdl 	asc = malloc(sizeof *asc, M_AGP, M_NOWAIT);
     87   1.1      fvdl 	if (asc == NULL) {
     88   1.4   thorpej 		aprint_error(": failed to allocate softc\n");
     89   1.1      fvdl 		return ENOMEM;
     90   1.1      fvdl 	}
     91   1.1      fvdl 	sc->as_chipc = asc;
     92   1.1      fvdl 	sc->as_methods = &agp_ali_methods;
     93   1.1      fvdl 
     94   1.9  christos 	if (agp_map_aperture(pa, sc, AGP_APBASE) != 0) {
     95   1.4   thorpej 		aprint_error(": failed to map aperture\n");
     96   1.1      fvdl 		free(asc, M_AGP);
     97   1.1      fvdl 		return ENXIO;
     98   1.1      fvdl 	}
     99   1.1      fvdl 
    100   1.1      fvdl 	pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP, &sc->as_capoff,
    101   1.1      fvdl 	    NULL);
    102   1.1      fvdl 
    103   1.1      fvdl 	asc->initial_aperture = agp_ali_get_aperture(sc);
    104   1.1      fvdl 
    105   1.1      fvdl 	for (;;) {
    106   1.1      fvdl 		gatt = agp_alloc_gatt(sc);
    107   1.1      fvdl 		if (gatt != NULL)
    108   1.1      fvdl 			break;
    109   1.1      fvdl 
    110   1.1      fvdl 		/*
    111   1.1      fvdl 		 * Probably contigmalloc failure. Try reducing the
    112   1.1      fvdl 		 * aperture so that the gatt size reduces.
    113   1.1      fvdl 		 */
    114   1.1      fvdl 		if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) {
    115   1.1      fvdl 			agp_generic_detach(sc);
    116   1.4   thorpej 			aprint_error(": failed to set aperture\n");
    117   1.1      fvdl 			return ENOMEM;
    118   1.1      fvdl 		}
    119   1.1      fvdl 	}
    120   1.1      fvdl 	asc->gatt = gatt;
    121   1.1      fvdl 
    122   1.1      fvdl 	/* Install the gatt. */
    123   1.1      fvdl 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AGP_ALI_ATTBASE);
    124   1.5  christos 	reg = (reg & 0xfff) | (gatt->ag_physical & ~0xfff);
    125   1.1      fvdl 	pci_conf_write(pa->pa_pc, pa->pa_tag, AGP_ALI_ATTBASE, reg);
    126   1.6     perry 
    127   1.1      fvdl 	/* Enable the TLB. */
    128   1.1      fvdl 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AGP_ALI_TLBCTRL);
    129   1.1      fvdl 	reg = (reg & ~0xff) | 0x10;
    130   1.1      fvdl 	pci_conf_write(pa->pa_pc, pa->pa_tag, AGP_ALI_TLBCTRL, reg);
    131   1.1      fvdl 
    132   1.1      fvdl 	return 0;
    133   1.1      fvdl }
    134   1.1      fvdl 
    135   1.1      fvdl #if 0
    136   1.1      fvdl static int
    137   1.1      fvdl agp_ali_detach(struct agp_softc *sc)
    138   1.1      fvdl {
    139   1.1      fvdl 	int error;
    140   1.1      fvdl 	pcireg_t reg;
    141   1.1      fvdl 	struct agp_ali_softc *asc = sc->as_chipc;
    142   1.1      fvdl 
    143   1.1      fvdl 	error = agp_generic_detach(sc);
    144   1.1      fvdl 	if (error)
    145   1.1      fvdl 		return error;
    146   1.1      fvdl 
    147   1.1      fvdl 	/* Disable the TLB.. */
    148   1.1      fvdl 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_ALI_TLBCTRL);
    149   1.1      fvdl 	reg &= ~0xff;
    150   1.1      fvdl 	reg |= 0x90;
    151   1.1      fvdl 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_ALI_TLBCTRL, reg);
    152   1.1      fvdl 
    153   1.1      fvdl 	/* Put the aperture back the way it started. */
    154   1.1      fvdl 	AGP_SET_APERTURE(sc, asc->initial_aperture);
    155   1.1      fvdl 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_ALI_ATTBASE);
    156   1.5  christos 	reg &= 0xf;
    157   1.1      fvdl 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_ALI_ATTBASE, reg);
    158   1.1      fvdl 
    159   1.1      fvdl 	agp_free_gatt(sc, asc->gatt);
    160   1.1      fvdl 	return 0;
    161   1.1      fvdl }
    162   1.1      fvdl #endif
    163   1.1      fvdl 
    164   1.1      fvdl #define M 1024*1024
    165   1.1      fvdl 
    166   1.7   thorpej static const u_int32_t agp_ali_table[] = {
    167   1.1      fvdl 	0,			/* 0 - invalid */
    168   1.1      fvdl 	1,			/* 1 - invalid */
    169   1.1      fvdl 	2,			/* 2 - invalid */
    170  1.14     njoly 	4*M,			/* 3 */
    171  1.14     njoly 	8*M,			/* 4 */
    172  1.14     njoly 	0,			/* 5 - Reserved */
    173  1.14     njoly 	16*M,			/* 6 */
    174  1.14     njoly 	32*M,			/* 7 */
    175  1.14     njoly 	64*M,			/* 8 */
    176  1.14     njoly 	128*M,			/* 9 */
    177  1.14     njoly 	256*M,			/* 10 */
    178   1.1      fvdl };
    179   1.1      fvdl #define agp_ali_table_size (sizeof(agp_ali_table) / sizeof(agp_ali_table[0]))
    180   1.1      fvdl 
    181   1.1      fvdl static u_int32_t
    182   1.1      fvdl agp_ali_get_aperture(struct agp_softc *sc)
    183   1.1      fvdl {
    184   1.1      fvdl 	int i;
    185   1.1      fvdl 
    186   1.1      fvdl 	/*
    187   1.1      fvdl 	 * The aperture size is derived from the low bits of attbase.
    188   1.1      fvdl 	 * I'm not sure this is correct..
    189   1.1      fvdl 	 */
    190   1.5  christos 	i = (int)pci_conf_read(sc->as_pc, sc->as_tag, AGP_ALI_ATTBASE) & 0xf;
    191   1.1      fvdl 	if (i >= agp_ali_table_size)
    192   1.1      fvdl 		return 0;
    193   1.1      fvdl 	return agp_ali_table[i];
    194   1.1      fvdl }
    195   1.1      fvdl 
    196   1.1      fvdl static int
    197   1.1      fvdl agp_ali_set_aperture(struct agp_softc *sc, u_int32_t aperture)
    198   1.1      fvdl {
    199   1.1      fvdl 	int i;
    200   1.1      fvdl 	pcireg_t reg;
    201   1.1      fvdl 
    202  1.14     njoly 	if (aperture & (aperture - 1) || aperture < 1*M)
    203  1.14     njoly 		return EINVAL;
    204  1.14     njoly 
    205   1.1      fvdl 	for (i = 0; i < agp_ali_table_size; i++)
    206   1.1      fvdl 		if (agp_ali_table[i] == aperture)
    207   1.1      fvdl 			break;
    208   1.1      fvdl 	if (i == agp_ali_table_size)
    209   1.1      fvdl 		return EINVAL;
    210   1.1      fvdl 
    211   1.1      fvdl 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_ALI_ATTBASE);
    212   1.5  christos 	reg &= ~0xf;
    213   1.1      fvdl 	reg |= i;
    214   1.1      fvdl 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_ALI_ATTBASE, reg);
    215   1.1      fvdl 	return 0;
    216   1.1      fvdl }
    217   1.1      fvdl 
    218   1.1      fvdl static int
    219   1.1      fvdl agp_ali_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
    220   1.1      fvdl {
    221   1.1      fvdl 	struct agp_ali_softc *asc = sc->as_chipc;
    222   1.1      fvdl 
    223   1.1      fvdl 	if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
    224   1.1      fvdl 		return EINVAL;
    225   1.1      fvdl 
    226   1.1      fvdl 	asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical;
    227   1.1      fvdl 	return 0;
    228   1.1      fvdl }
    229   1.1      fvdl 
    230   1.1      fvdl static int
    231   1.1      fvdl agp_ali_unbind_page(struct agp_softc *sc, off_t offset)
    232   1.1      fvdl {
    233   1.1      fvdl 	struct agp_ali_softc *asc = sc->as_chipc;
    234   1.1      fvdl 
    235   1.1      fvdl 	if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
    236   1.1      fvdl 		return EINVAL;
    237   1.1      fvdl 
    238   1.1      fvdl 	asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
    239   1.1      fvdl 	return 0;
    240   1.1      fvdl }
    241   1.1      fvdl 
    242   1.1      fvdl static void
    243   1.1      fvdl agp_ali_flush_tlb(struct agp_softc *sc)
    244   1.1      fvdl {
    245   1.1      fvdl 	pcireg_t reg;
    246   1.1      fvdl 
    247   1.1      fvdl 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_ALI_TLBCTRL);
    248   1.1      fvdl 	reg &= ~0xff;
    249   1.1      fvdl 	reg |= 0x90;
    250   1.1      fvdl 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_ALI_TLBCTRL, reg);
    251   1.1      fvdl 	reg &= ~0xff;
    252   1.1      fvdl 	reg |= 0x10;
    253   1.1      fvdl 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_ALI_TLBCTRL, reg);
    254   1.1      fvdl }
    255