1 /* $NetBSD: agp_via.c,v 1.2 2001/09/15 00:25:00 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_via.c,v 1.3 2001/07/05 21:28:47 jhb Exp $ 29 */ 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/malloc.h> 34 #include <sys/kernel.h> 35 #include <sys/lock.h> 36 #include <sys/proc.h> 37 #include <sys/conf.h> 38 #include <sys/device.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 static u_int32_t agp_via_get_aperture(struct agp_softc *); 51 static int agp_via_set_aperture(struct agp_softc *, u_int32_t); 52 static int agp_via_bind_page(struct agp_softc *, off_t, bus_addr_t); 53 static int agp_via_unbind_page(struct agp_softc *, off_t); 54 static void agp_via_flush_tlb(struct agp_softc *); 55 56 struct agp_methods agp_via_methods = { 57 agp_via_get_aperture, 58 agp_via_set_aperture, 59 agp_via_bind_page, 60 agp_via_unbind_page, 61 agp_via_flush_tlb, 62 agp_generic_enable, 63 agp_generic_alloc_memory, 64 agp_generic_free_memory, 65 agp_generic_bind_memory, 66 agp_generic_unbind_memory, 67 }; 68 69 struct agp_via_softc { 70 u_int32_t initial_aperture; /* aperture size at startup */ 71 struct agp_gatt *gatt; 72 }; 73 74 int 75 agp_via_attach(struct device *parent, struct device *self, void *aux) 76 { 77 struct pci_attach_args *pa = aux; 78 struct agp_softc *sc = (void *)self; 79 struct agp_via_softc *asc; 80 struct agp_gatt *gatt; 81 82 asc = malloc(sizeof *asc, M_AGP, M_NOWAIT); 83 if (asc == NULL) { 84 printf(": can't allocate chipset-specific softc\n"); 85 return ENOMEM; 86 } 87 memset(asc, 0, sizeof *asc); 88 sc->as_chipc = asc; 89 sc->as_methods = &agp_via_methods; 90 pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP, &sc->as_capoff, 91 NULL); 92 93 if (agp_map_aperture(pa, sc) != 0) { 94 printf(": can't map aperture\n"); 95 free(asc, M_AGP); 96 return ENXIO; 97 } 98 99 asc->initial_aperture = AGP_GET_APERTURE(sc); 100 101 for (;;) { 102 gatt = agp_alloc_gatt(sc); 103 if (gatt) 104 break; 105 106 /* 107 * Probably contigmalloc failure. Try reducing the 108 * aperture so that the gatt size reduces. 109 */ 110 if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) { 111 agp_generic_detach(sc); 112 printf(": can't set aperture size\n"); 113 return ENOMEM; 114 } 115 } 116 asc->gatt = gatt; 117 118 /* Install the gatt. */ 119 pci_conf_write(pa->pa_pc, pa->pa_tag, AGP_VIA_ATTBASE, 120 gatt->ag_physical | 3); 121 122 /* Enable the aperture. */ 123 pci_conf_write(pa->pa_pc, pa->pa_tag, AGP_VIA_GARTCTRL, 0x0000000f); 124 125 return 0; 126 } 127 128 #if 0 129 static int 130 agp_via_detach(struct agp_softc *sc) 131 { 132 struct agp_via_softc *asc = sc->as_chipc; 133 int error; 134 135 error = agp_generic_detach(sc); 136 if (error) 137 return error; 138 139 pci_conf_write(sc->as_pc, sc->as_tag, AGP_VIA_GARTCTRL, 0); 140 pci_conf_write(sc->as_pc, sc->as_tag, AGP_VIA_ATTBASE, 0); 141 AGP_SET_APERTURE(sc, asc->initial_aperture); 142 agp_free_gatt(sc, asc->gatt); 143 144 return 0; 145 } 146 #endif 147 148 static u_int32_t 149 agp_via_get_aperture(struct agp_softc *sc) 150 { 151 u_int32_t apsize; 152 153 apsize = pci_conf_read(sc->as_pc, sc->as_tag, AGP_VIA_APSIZE) & 0x1f; 154 155 /* 156 * The size is determined by the number of low bits of 157 * register APBASE which are forced to zero. The low 20 bits 158 * are always forced to zero and each zero bit in the apsize 159 * field just read forces the corresponding bit in the 27:20 160 * to be zero. We calculate the aperture size accordingly. 161 */ 162 return (((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1; 163 } 164 165 static int 166 agp_via_set_aperture(struct agp_softc *sc, u_int32_t aperture) 167 { 168 u_int32_t apsize; 169 pcireg_t reg; 170 171 /* 172 * Reverse the magic from get_aperture. 173 */ 174 apsize = ((aperture - 1) >> 20) ^ 0xff; 175 176 /* 177 * Double check for sanity. 178 */ 179 if ((((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1 != aperture) 180 return EINVAL; 181 182 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_VIA_APSIZE); 183 reg &= ~0xff; 184 reg |= apsize; 185 pci_conf_write(sc->as_pc, sc->as_tag, AGP_VIA_APSIZE, reg); 186 187 return 0; 188 } 189 190 static int 191 agp_via_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical) 192 { 193 struct agp_via_softc *asc = sc->as_chipc; 194 195 if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT)) 196 return EINVAL; 197 198 asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical; 199 return 0; 200 } 201 202 static int 203 agp_via_unbind_page(struct agp_softc *sc, off_t offset) 204 { 205 struct agp_via_softc *asc = sc->as_chipc; 206 207 if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT)) 208 return EINVAL; 209 210 asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0; 211 return 0; 212 } 213 214 static void 215 agp_via_flush_tlb(struct agp_softc *sc) 216 { 217 pci_conf_write(sc->as_pc, sc->as_tag, AGP_VIA_GARTCTRL, 0x8f); 218 pci_conf_write(sc->as_pc, sc->as_tag, AGP_VIA_GARTCTRL, 0x0f); 219 } 220