agp_amd64.c revision 141885
1272343Sngie/*- 2272343Sngie * Copyright (c) 2004 Jung-uk Kim 3272343Sngie * All rights reserved. 4272343Sngie * 5272343Sngie * Redistribution and use in source and binary forms, with or without 6272343Sngie * modification, are permitted provided that the following conditions 7272343Sngie * are met: 8272343Sngie * 1. Redistributions of source code must retain the above copyright 9291764Sngie * notice, this list of conditions and the following disclaimer. 10291764Sngie * 2. Redistributions in binary form must reproduce the above copyright 11291764Sngie * notice, this list of conditions and the following disclaimer in the 12291764Sngie * documentation and/or other materials provided with the distribution. 13291764Sngie * 14291764Sngie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15291764Sngie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16272343Sngie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17272343Sngie * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18272343Sngie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19272343Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20272343Sngie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21272343Sngie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22272343Sngie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23272343Sngie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24272343Sngie * SUCH DAMAGE. 25272343Sngie */ 26272343Sngie 27272343Sngie#include <sys/cdefs.h> 28272343Sngie__FBSDID("$FreeBSD: head/sys/dev/agp/agp_amd64.c 141885 2005-02-14 07:30:04Z anholt $"); 29272343Sngie 30272343Sngie#include "opt_bus.h" 31272343Sngie 32272343Sngie#include <sys/param.h> 33272343Sngie#include <sys/systm.h> 34272343Sngie#include <sys/malloc.h> 35272343Sngie#include <sys/kernel.h> 36272343Sngie#include <sys/module.h> 37272343Sngie#include <sys/bus.h> 38272343Sngie#include <sys/lock.h> 39272343Sngie#include <sys/mutex.h> 40272343Sngie#include <sys/proc.h> 41272343Sngie 42272343Sngie#include <dev/pci/pcivar.h> 43272343Sngie#include <dev/pci/pcireg.h> 44272343Sngie#include <pci/agppriv.h> 45272343Sngie#include <pci/agpreg.h> 46272343Sngie 47272343Sngie#include <vm/vm.h> 48272343Sngie#include <vm/vm_object.h> 49272343Sngie#include <vm/pmap.h> 50272343Sngie#include <machine/bus.h> 51272343Sngie#include <machine/resource.h> 52272343Sngie#include <sys/rman.h> 53272343Sngie 54272343Sngie/* XXX */ 55272343Sngieextern void pci_cfgregwrite(int, int, int, int, uint32_t, int); 56272343Sngieextern uint32_t pci_cfgregread(int, int, int, int, int); 57272343Sngie 58272343SngieMALLOC_DECLARE(M_AGP); 59272343Sngie 60272343Sngie#define AMD64_MAX_MCTRL 8 61272343Sngie 62272343Sngiestruct agp_amd64_softc { 63272343Sngie struct agp_softc agp; 64272343Sngie uint32_t initial_aperture; /* aperture size at startup */ 65272343Sngie struct agp_gatt *gatt; 66272343Sngie int mctrl[AMD64_MAX_MCTRL]; 67272343Sngie int n_mctrl; 68272343Sngie}; 69272343Sngie 70272343Sngiestatic const char* 71272343Sngieagp_amd64_match(device_t dev) 72272343Sngie{ 73272343Sngie if (pci_get_class(dev) != PCIC_BRIDGE 74272343Sngie || pci_get_subclass(dev) != PCIS_BRIDGE_HOST) 75272343Sngie return NULL; 76272343Sngie 77272343Sngie if (agp_find_caps(dev) == 0) 78272343Sngie return NULL; 79272343Sngie 80272343Sngie switch (pci_get_devid(dev)) { 81272343Sngie case 0x74541022: 82272343Sngie return ("AMD 8151 AGP graphics tunnel"); 83272343Sngie case 0x07551039: 84272343Sngie return ("SiS 755 host to AGP bridge"); 85272343Sngie case 0x02041106: 86272343Sngie return ("VIA 8380 host to PCI bridge"); 87272343Sngie case 0x02821106: 88272343Sngie return ("VIA K8T800Pro host to PCI bridge"); 89272343Sngie case 0x31881106: 90272343Sngie return ("VIA 8385 host to PCI bridge"); 91272343Sngie }; 92272343Sngie 93272343Sngie return NULL; 94272343Sngie} 95272343Sngie 96272343Sngiestatic int 97272343Sngieagp_amd64_probe(device_t dev) 98272343Sngie{ 99272343Sngie const char *desc; 100272343Sngie 101272343Sngie if (resource_disabled("agp", device_get_unit(dev))) 102272343Sngie return ENXIO; 103272343Sngie if ((desc = agp_amd64_match(dev))) { 104272343Sngie device_verbose(dev); 105272343Sngie device_set_desc(dev, desc); 106272343Sngie return 0; 107272343Sngie } 108272343Sngie 109272343Sngie return ENXIO; 110272343Sngie} 111272343Sngie 112272343Sngiestatic int 113272343Sngieagp_amd64_attach(device_t dev) 114272343Sngie{ 115272343Sngie struct agp_amd64_softc *sc = device_get_softc(dev); 116272343Sngie struct agp_gatt *gatt; 117272343Sngie int i, n, error; 118272343Sngie 119272343Sngie for (i = 0, n = 0; i < PCI_SLOTMAX && n < AMD64_MAX_MCTRL; i++) 120272343Sngie if (pci_cfgregread(0, i, 3, 0, 4) == 0x11031022) { 121291764Sngie sc->mctrl[n] = i; 122291764Sngie n++; 123291764Sngie } 124291764Sngie 125272343Sngie if (n == 0) 126272343Sngie return ENXIO; 127272343Sngie 128291764Sngie sc->n_mctrl = n; 129291764Sngie 130291764Sngie if (bootverbose) 131272343Sngie printf("AMD64: %d Misc. Control unit(s) found.\n", sc->n_mctrl); 132291764Sngie 133272343Sngie if ((error = agp_generic_attach(dev))) 134272343Sngie return error; 135272343Sngie 136272343Sngie sc->initial_aperture = AGP_GET_APERTURE(dev); 137272343Sngie 138291764Sngie for (;;) { 139291764Sngie gatt = agp_alloc_gatt(dev); 140291764Sngie if (gatt) 141272343Sngie break; 142291764Sngie 143272343Sngie /* 144272343Sngie * Probably contigmalloc failure. Try reducing the 145272343Sngie * aperture so that the gatt size reduces. 146272343Sngie */ 147272343Sngie if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2)) { 148272343Sngie agp_generic_detach(dev); 149272343Sngie return ENOMEM; 150272343Sngie } 151272343Sngie } 152272343Sngie sc->gatt = gatt; 153272343Sngie 154272343Sngie /* Install the gatt and enable aperture. */ 155272343Sngie for (i = 0; i < sc->n_mctrl; i++) { 156272343Sngie pci_cfgregwrite(0, sc->mctrl[i], 3, AGP_AMD64_ATTBASE, 157 (uint32_t)(gatt->ag_physical >> 8) & AGP_AMD64_ATTBASE_MASK, 158 4); 159 pci_cfgregwrite(0, sc->mctrl[i], 3, AGP_AMD64_APCTRL, 160 (pci_cfgregread(0, sc->mctrl[i], 3, AGP_AMD64_APCTRL, 4) | 161 AGP_AMD64_APCTRL_GARTEN) & 162 ~(AGP_AMD64_APCTRL_DISGARTCPU | AGP_AMD64_APCTRL_DISGARTIO), 163 4); 164 } 165 166 agp_flush_cache(); 167 168 return 0; 169} 170 171static int 172agp_amd64_detach(device_t dev) 173{ 174 struct agp_amd64_softc *sc = device_get_softc(dev); 175 int i, error; 176 177 if ((error = agp_generic_detach(dev))) 178 return error; 179 180 for (i = 0; i < sc->n_mctrl; i++) 181 pci_cfgregwrite(0, sc->mctrl[i], 3, AGP_AMD64_APCTRL, 182 pci_cfgregread(0, sc->mctrl[i], 3, AGP_AMD64_APCTRL, 4) & 183 ~AGP_AMD64_APCTRL_GARTEN, 4); 184 185 AGP_SET_APERTURE(dev, sc->initial_aperture); 186 agp_free_gatt(sc->gatt); 187 188 return 0; 189} 190 191static uint32_t agp_amd64_table[] = { 192 0x02000000, /* 32 MB */ 193 0x04000000, /* 64 MB */ 194 0x08000000, /* 128 MB */ 195 0x10000000, /* 256 MB */ 196 0x20000000, /* 512 MB */ 197 0x40000000, /* 1024 MB */ 198 0x80000000, /* 2048 MB */ 199}; 200 201#define AGP_AMD64_TABLE_SIZE \ 202 (sizeof(agp_amd64_table) / sizeof(agp_amd64_table[0])) 203 204static uint32_t 205agp_amd64_get_aperture(device_t dev) 206{ 207 struct agp_amd64_softc *sc = device_get_softc(dev); 208 uint32_t i; 209 210 i = (pci_cfgregread(0, sc->mctrl[0], 3, AGP_AMD64_APCTRL, 4) & 211 AGP_AMD64_APCTRL_SIZE_MASK) >> 1; 212 213 if (i >= AGP_AMD64_TABLE_SIZE) 214 return 0; 215 216 return (agp_amd64_table[i]); 217} 218 219static int 220agp_amd64_set_aperture(device_t dev, uint32_t aperture) 221{ 222 struct agp_amd64_softc *sc = device_get_softc(dev); 223 uint32_t i; 224 int j; 225 226 for (i = 0; i < AGP_AMD64_TABLE_SIZE; i++) 227 if (agp_amd64_table[i] == aperture) 228 break; 229 if (i == AGP_AMD64_TABLE_SIZE) 230 return EINVAL; 231 232 for (j = 0; j < sc->n_mctrl; j++) 233 pci_cfgregwrite(0, sc->mctrl[j], 3, AGP_AMD64_APCTRL, 234 (pci_cfgregread(0, sc->mctrl[j], 3, AGP_AMD64_APCTRL, 4) & 235 ~(AGP_AMD64_APCTRL_SIZE_MASK)) | (i << 1), 4); 236 237 return 0; 238} 239 240static int 241agp_amd64_bind_page(device_t dev, int offset, vm_offset_t physical) 242{ 243 struct agp_amd64_softc *sc = device_get_softc(dev); 244 245 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) 246 return EINVAL; 247 248 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical; 249 return 0; 250} 251 252static int 253agp_amd64_unbind_page(device_t dev, int offset) 254{ 255 struct agp_amd64_softc *sc = device_get_softc(dev); 256 257 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) 258 return EINVAL; 259 260 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0; 261 return 0; 262} 263 264static void 265agp_amd64_flush_tlb(device_t dev) 266{ 267 struct agp_amd64_softc *sc = device_get_softc(dev); 268 int i; 269 270 for (i = 0; i < sc->n_mctrl; i++) 271 pci_cfgregwrite(0, sc->mctrl[i], 3, AGP_AMD64_CACHECTRL, 272 pci_cfgregread(0, sc->mctrl[i], 3, AGP_AMD64_CACHECTRL, 4) | 273 AGP_AMD64_CACHECTRL_INVGART, 4); 274} 275 276static device_method_t agp_amd64_methods[] = { 277 /* Device interface */ 278 DEVMETHOD(device_probe, agp_amd64_probe), 279 DEVMETHOD(device_attach, agp_amd64_attach), 280 DEVMETHOD(device_detach, agp_amd64_detach), 281 DEVMETHOD(device_shutdown, bus_generic_shutdown), 282 DEVMETHOD(device_suspend, bus_generic_suspend), 283 DEVMETHOD(device_resume, bus_generic_resume), 284 285 /* AGP interface */ 286 DEVMETHOD(agp_get_aperture, agp_amd64_get_aperture), 287 DEVMETHOD(agp_set_aperture, agp_amd64_set_aperture), 288 DEVMETHOD(agp_bind_page, agp_amd64_bind_page), 289 DEVMETHOD(agp_unbind_page, agp_amd64_unbind_page), 290 DEVMETHOD(agp_flush_tlb, agp_amd64_flush_tlb), 291 DEVMETHOD(agp_enable, agp_generic_enable), 292 DEVMETHOD(agp_alloc_memory, agp_generic_alloc_memory), 293 DEVMETHOD(agp_free_memory, agp_generic_free_memory), 294 DEVMETHOD(agp_bind_memory, agp_generic_bind_memory), 295 DEVMETHOD(agp_unbind_memory, agp_generic_unbind_memory), 296 297 { 0, 0 } 298}; 299 300static driver_t agp_amd64_driver = { 301 "agp", 302 agp_amd64_methods, 303 sizeof(struct agp_amd64_softc), 304}; 305 306static devclass_t agp_devclass; 307 308DRIVER_MODULE(agp_amd64, pci, agp_amd64_driver, agp_devclass, 0, 0); 309MODULE_DEPEND(agp_amd64, agp, 1, 1, 1); 310MODULE_DEPEND(agp_amd64, pci, 1, 1, 1); 311