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