agp_via.c revision 133852
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: head/sys/dev/agp/agp_via.c 133852 2004-08-16 12:25:48Z obrien $"); 29 30#include "opt_bus.h" 31#include "opt_agp.h" 32 33#include <sys/param.h> 34#include <sys/systm.h> 35#include <sys/malloc.h> 36#include <sys/kernel.h> 37#include <sys/module.h> 38#include <sys/bus.h> 39#include <sys/lock.h> 40#include <sys/mutex.h> 41#include <sys/proc.h> 42 43#include <dev/pci/pcivar.h> 44#include <dev/pci/pcireg.h> 45#include <pci/agppriv.h> 46#include <pci/agpreg.h> 47 48#include <vm/vm.h> 49#include <vm/vm_object.h> 50#include <vm/pmap.h> 51 52#define REG_GARTCTRL 0 53#define REG_APSIZE 1 54#define REG_ATTBASE 2 55 56struct agp_via_softc { 57 struct agp_softc agp; 58 u_int32_t initial_aperture; /* aperture size at startup */ 59 struct agp_gatt *gatt; 60 int *regs; 61}; 62 63static int via_v2_regs[] = { AGP_VIA_GARTCTRL, AGP_VIA_APSIZE, 64 AGP_VIA_ATTBASE }; 65static int via_v3_regs[] = { AGP3_VIA_GARTCTRL, AGP3_VIA_APSIZE, 66 AGP3_VIA_ATTBASE }; 67 68static const char* 69agp_via_match(device_t dev) 70{ 71 if (pci_get_class(dev) != PCIC_BRIDGE 72 || pci_get_subclass(dev) != PCIS_BRIDGE_HOST) 73 return NULL; 74 75 if (agp_find_caps(dev) == 0) 76 return NULL; 77 78 switch (pci_get_devid(dev)) { 79 case 0x03051106: 80 return ("VIA 82C8363 (Apollo KT133A) host to PCI bridge"); 81 case 0x05011106: 82 return ("VIA 8501 (Apollo MVP4) host to PCI bridge"); 83 case 0x05971106: 84 return ("VIA 82C597 (Apollo VP3) host to PCI bridge"); 85 case 0x05981106: 86 return ("VIA 82C598 (Apollo MVP3) host to PCI bridge"); 87 case 0x06051106: 88 return ("VIA 82C694X (Apollo Pro 133A) host to PCI bridge"); 89 case 0x06911106: 90 return ("VIA 82C691 (Apollo Pro) host to PCI bridge"); 91 case 0x31881106: 92#if defined(__amd64__) || defined(AGP_AMD64_GART) 93 return NULL; 94#else 95 return ("VIA 8385 host to PCI bridge"); 96#endif 97 case 0x31891106: 98 return ("VIA 8377 (Apollo KT400/KT400A/KT600) host to PCI bridge"); 99 }; 100 101 if (pci_get_vendor(dev) == 0x1106) 102 return ("VIA Generic host to PCI bridge"); 103 104 return NULL; 105} 106 107static int 108agp_via_probe(device_t dev) 109{ 110 const char *desc; 111 112 if (resource_disabled("agp", device_get_unit(dev))) 113 return (ENXIO); 114 desc = agp_via_match(dev); 115 if (desc) { 116 device_verbose(dev); 117 device_set_desc(dev, desc); 118 return 0; 119 } 120 121 return ENXIO; 122} 123 124static int 125agp_via_attach(device_t dev) 126{ 127 struct agp_via_softc *sc = device_get_softc(dev); 128 struct agp_gatt *gatt; 129 int error; 130 u_int32_t agpsel; 131 132 switch (pci_get_devid(dev)) { 133#ifdef AGP_NO_AMD64_GART 134 case 0x31881106: 135#endif 136 case 0x31891106: 137 /* The newer VIA chipsets will select the AGP version based on 138 * what AGP versions the card supports. We still have to 139 * program it using the v2 registers if it has chosen to use 140 * compatibility mode. 141 */ 142 agpsel = pci_read_config(dev, AGP_VIA_AGPSEL, 1); 143 if ((agpsel & (1 << 1)) == 0) 144 sc->regs = via_v3_regs; 145 else 146 sc->regs = via_v2_regs; 147 break; 148 default: 149 sc->regs = via_v2_regs; 150 break; 151 } 152 153 error = agp_generic_attach(dev); 154 if (error) 155 return error; 156 157 sc->initial_aperture = AGP_GET_APERTURE(dev); 158 159 for (;;) { 160 gatt = agp_alloc_gatt(dev); 161 if (gatt) 162 break; 163 164 /* 165 * Probably contigmalloc failure. Try reducing the 166 * aperture so that the gatt size reduces. 167 */ 168 if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2)) { 169 agp_generic_detach(dev); 170 return ENOMEM; 171 } 172 } 173 sc->gatt = gatt; 174 175 /* Install the gatt. */ 176 pci_write_config(dev, sc->regs[REG_ATTBASE], gatt->ag_physical | 3, 4); 177 178 /* Enable the aperture. */ 179 pci_write_config(dev, sc->regs[REG_GARTCTRL], 0x0f, 4); 180 181 return 0; 182} 183 184static int 185agp_via_detach(device_t dev) 186{ 187 struct agp_via_softc *sc = device_get_softc(dev); 188 int error; 189 190 error = agp_generic_detach(dev); 191 if (error) 192 return error; 193 194 pci_write_config(dev, sc->regs[REG_GARTCTRL], 0, 4); 195 pci_write_config(dev, sc->regs[REG_ATTBASE], 0, 4); 196 AGP_SET_APERTURE(dev, sc->initial_aperture); 197 agp_free_gatt(sc->gatt); 198 199 return 0; 200} 201 202static u_int32_t 203agp_via_get_aperture(device_t dev) 204{ 205 struct agp_via_softc *sc = device_get_softc(dev); 206 u_int32_t apsize; 207 208 apsize = pci_read_config(dev, sc->regs[REG_APSIZE], 1) & 0x1f; 209 210 /* 211 * The size is determined by the number of low bits of 212 * register APBASE which are forced to zero. The low 20 bits 213 * are always forced to zero and each zero bit in the apsize 214 * field just read forces the corresponding bit in the 27:20 215 * to be zero. We calculate the aperture size accordingly. 216 */ 217 return (((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1; 218} 219 220static int 221agp_via_set_aperture(device_t dev, u_int32_t aperture) 222{ 223 struct agp_via_softc *sc = device_get_softc(dev); 224 u_int32_t apsize; 225 226 /* 227 * Reverse the magic from get_aperture. 228 */ 229 apsize = ((aperture - 1) >> 20) ^ 0xff; 230 231 /* 232 * Double check for sanity. 233 */ 234 if ((((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1 != aperture) 235 return EINVAL; 236 237 pci_write_config(dev, sc->regs[REG_APSIZE], apsize, 1); 238 239 return 0; 240} 241 242static int 243agp_via_bind_page(device_t dev, int offset, vm_offset_t physical) 244{ 245 struct agp_via_softc *sc = device_get_softc(dev); 246 247 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) 248 return EINVAL; 249 250 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical; 251 return 0; 252} 253 254static int 255agp_via_unbind_page(device_t dev, int offset) 256{ 257 struct agp_via_softc *sc = device_get_softc(dev); 258 259 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) 260 return EINVAL; 261 262 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0; 263 return 0; 264} 265 266static void 267agp_via_flush_tlb(device_t dev) 268{ 269 struct agp_via_softc *sc = device_get_softc(dev); 270 271 pci_write_config(dev, sc->regs[REG_GARTCTRL], 0x8f, 4); 272 pci_write_config(dev, sc->regs[REG_GARTCTRL], 0x0f, 4); 273} 274 275static device_method_t agp_via_methods[] = { 276 /* Device interface */ 277 DEVMETHOD(device_probe, agp_via_probe), 278 DEVMETHOD(device_attach, agp_via_attach), 279 DEVMETHOD(device_detach, agp_via_detach), 280 DEVMETHOD(device_shutdown, bus_generic_shutdown), 281 DEVMETHOD(device_suspend, bus_generic_suspend), 282 DEVMETHOD(device_resume, bus_generic_resume), 283 284 /* AGP interface */ 285 DEVMETHOD(agp_get_aperture, agp_via_get_aperture), 286 DEVMETHOD(agp_set_aperture, agp_via_set_aperture), 287 DEVMETHOD(agp_bind_page, agp_via_bind_page), 288 DEVMETHOD(agp_unbind_page, agp_via_unbind_page), 289 DEVMETHOD(agp_flush_tlb, agp_via_flush_tlb), 290 DEVMETHOD(agp_enable, agp_generic_enable), 291 DEVMETHOD(agp_alloc_memory, agp_generic_alloc_memory), 292 DEVMETHOD(agp_free_memory, agp_generic_free_memory), 293 DEVMETHOD(agp_bind_memory, agp_generic_bind_memory), 294 DEVMETHOD(agp_unbind_memory, agp_generic_unbind_memory), 295 296 { 0, 0 } 297}; 298 299static driver_t agp_via_driver = { 300 "agp", 301 agp_via_methods, 302 sizeof(struct agp_via_softc), 303}; 304 305static devclass_t agp_devclass; 306 307DRIVER_MODULE(agp_via, pci, agp_via_driver, agp_devclass, 0, 0); 308MODULE_DEPEND(agp_via, agp, 1, 1, 1); 309MODULE_DEPEND(agp_via, pci, 1, 1, 1); 310