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