1 /*- 2 * Copyright (c) 2000 Doug Rabson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include "opt_bus.h" 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/malloc.h> 35 #include <sys/kernel.h> 36 #include <sys/bus.h> 37 #include <sys/lock.h> 38 #include <sys/mutex.h> 39 #include <sys/proc.h> 40 41 #include <dev/pci/pcivar.h> 42 #include <dev/pci/pcireg.h> 43 #include <pci/agppriv.h> 44 #include <pci/agpreg.h> 45 46 #include <vm/vm.h> 47 #include <vm/vm_object.h> 48 #include <vm/pmap.h> 49 50 #define REG_GARTCTRL 0 51 #define REG_APSIZE 1 52 #define REG_ATTBASE 2 53 54 struct agp_via_softc { 55 struct agp_softc agp; 56 u_int32_t initial_aperture; /* aperture size at startup */ 57 struct agp_gatt *gatt; 58 int *regs; 59 }; 60 61 static int via_v2_regs[] = { AGP_VIA_GARTCTRL, AGP_VIA_APSIZE, 62 AGP_VIA_ATTBASE }; 63 static int via_v3_regs[] = { AGP3_VIA_GARTCTRL, AGP3_VIA_APSIZE, 64 AGP3_VIA_ATTBASE }; 65 66 static const char* 67 agp_via_match(device_t dev) 68 { 69 if (pci_get_class(dev) != PCIC_BRIDGE 70 || pci_get_subclass(dev) != PCIS_BRIDGE_HOST) 71 return NULL; 72 73 if (agp_find_caps(dev) == 0) 74 return NULL; 75 76 switch (pci_get_devid(dev)) { 77 case 0x03051106: 78 return ("VIA 82C8363 (Apollo KT133A) host to PCI bridge"); 79 case 0x05011106: 80 return ("VIA 8501 (Apollo MVP4) host to PCI bridge"); 81 case 0x05971106: 82 return ("VIA 82C597 (Apollo VP3) host to PCI bridge"); 83 case 0x05981106: 84 return ("VIA 82C598 (Apollo MVP3) host to PCI bridge"); 85 case 0x06051106: 86 return ("VIA 82C694X (Apollo Pro 133A) host to PCI bridge"); 87 case 0x06911106: 88 return ("VIA 82C691 (Apollo Pro) host to PCI bridge"); 89 case 0x31881106: 90 return ("VIA 8385 host to PCI bridge"); 91 }; 92 93 if (pci_get_vendor(dev) == 0x1106) 94 return ("VIA Generic host to PCI bridge"); 95 96 return NULL; 97 } 98 99 static int 100 agp_via_probe(device_t dev) 101 { 102 const char *desc; 103 104 if (resource_disabled("agp", device_get_unit(dev))) 105 return (ENXIO); 106 desc = agp_via_match(dev); 107 if (desc) { 108 device_verbose(dev); 109 device_set_desc(dev, desc); 110 return 0; 111 } 112 113 return ENXIO; 114 } 115 116 static int 117 agp_via_attach(device_t dev) 118 { 119 struct agp_via_softc *sc = device_get_softc(dev); 120 struct agp_gatt *gatt; 121 int error; 122 123 switch (pci_get_devid(dev)) { 124 case 0x31881106: 125 sc->regs = via_v3_regs; 126 break; 127 default: 128 sc->regs = via_v2_regs; 129 break; 130 } 131 132 error = agp_generic_attach(dev); 133 if (error) 134 return error; 135 136 sc->initial_aperture = AGP_GET_APERTURE(dev); 137 138 for (;;) { 139 gatt = agp_alloc_gatt(dev); 140 if (gatt) 141 break; 142 143 /* 144 * Probably contigmalloc failure. Try reducing the 145 * aperture so that the gatt size reduces. 146 */ 147 if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2)) { 148 agp_generic_detach(dev); 149 return ENOMEM; 150 } 151 } 152 sc->gatt = gatt; 153 154 /* Install the gatt. */ 155 pci_write_config(dev, sc->regs[REG_ATTBASE], gatt->ag_physical | 3, 4); 156 157 /* Enable the aperture. */ 158 pci_write_config(dev, sc->regs[REG_GARTCTRL], 0x0f, 4); 159 160 return 0; 161 } 162 163 static int 164 agp_via_detach(device_t dev) 165 { 166 struct agp_via_softc *sc = device_get_softc(dev); 167 int error; 168 169 error = agp_generic_detach(dev); 170 if (error) 171 return error; 172 173 pci_write_config(dev, sc->regs[REG_GARTCTRL], 0, 4); 174 pci_write_config(dev, sc->regs[REG_ATTBASE], 0, 4); 175 AGP_SET_APERTURE(dev, sc->initial_aperture); 176 agp_free_gatt(sc->gatt); 177 178 return 0; 179 } 180 181 static u_int32_t 182 agp_via_get_aperture(device_t dev) 183 { 184 struct agp_via_softc *sc = device_get_softc(dev); 185 u_int32_t apsize; 186 187 apsize = pci_read_config(dev, sc->regs[REG_APSIZE], 1) & 0x1f; 188 189 /* 190 * The size is determined by the number of low bits of 191 * register APBASE which are forced to zero. The low 20 bits 192 * are always forced to zero and each zero bit in the apsize 193 * field just read forces the corresponding bit in the 27:20 194 * to be zero. We calculate the aperture size accordingly. 195 */ 196 return (((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1; 197 } 198 199 static int 200 agp_via_set_aperture(device_t dev, u_int32_t aperture) 201 { 202 struct agp_via_softc *sc = device_get_softc(dev); 203 u_int32_t apsize; 204 205 /* 206 * Reverse the magic from get_aperture. 207 */ 208 apsize = ((aperture - 1) >> 20) ^ 0xff; 209 210 /* 211 * Double check for sanity. 212 */ 213 if ((((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1 != aperture) 214 return EINVAL; 215 216 pci_write_config(dev, sc->regs[REG_APSIZE], apsize, 1); 217 218 return 0; 219 } 220 221 static int 222 agp_via_bind_page(device_t dev, int offset, vm_offset_t physical) 223 { 224 struct agp_via_softc *sc = device_get_softc(dev); 225 226 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) 227 return EINVAL; 228 229 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical; 230 return 0; 231 } 232 233 static int 234 agp_via_unbind_page(device_t dev, int offset) 235 { 236 struct agp_via_softc *sc = device_get_softc(dev); 237 238 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) 239 return EINVAL; 240 241 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0; 242 return 0; 243 } 244 245 static void 246 agp_via_flush_tlb(device_t dev) 247 { 248 struct agp_via_softc *sc = device_get_softc(dev); 249 250 pci_write_config(dev, sc->regs[REG_GARTCTRL], 0x8f, 4); 251 pci_write_config(dev, sc->regs[REG_GARTCTRL], 0x0f, 4); 252 } 253 254 static device_method_t agp_via_methods[] = { 255 /* Device interface */ 256 DEVMETHOD(device_probe, agp_via_probe), 257 DEVMETHOD(device_attach, agp_via_attach), 258 DEVMETHOD(device_detach, agp_via_detach), 259 DEVMETHOD(device_shutdown, bus_generic_shutdown), 260 DEVMETHOD(device_suspend, bus_generic_suspend), 261 DEVMETHOD(device_resume, bus_generic_resume), 262 263 /* AGP interface */ 264 DEVMETHOD(agp_get_aperture, agp_via_get_aperture), 265 DEVMETHOD(agp_set_aperture, agp_via_set_aperture), 266 DEVMETHOD(agp_bind_page, agp_via_bind_page), 267 DEVMETHOD(agp_unbind_page, agp_via_unbind_page), 268 DEVMETHOD(agp_flush_tlb, agp_via_flush_tlb), 269 DEVMETHOD(agp_enable, agp_generic_enable), 270 DEVMETHOD(agp_alloc_memory, agp_generic_alloc_memory), 271 DEVMETHOD(agp_free_memory, agp_generic_free_memory), 272 DEVMETHOD(agp_bind_memory, agp_generic_bind_memory), 273 DEVMETHOD(agp_unbind_memory, agp_generic_unbind_memory), 274 275 { 0, 0 } 276 }; 277 278 static driver_t agp_via_driver = { 279 "agp", 280 agp_via_methods, 281 sizeof(struct agp_via_softc), 282 }; 283 284 static devclass_t agp_devclass; 285 286 DRIVER_MODULE(agp_via, pci, agp_via_driver, agp_devclass, 0, 0); 287 MODULE_DEPEND(agp_via, agp, 1, 1, 1); 288 MODULE_DEPEND(agp_via, pci, 1, 1, 1); 289