1/*- 2 * Copyright (c) 2004 Jung-uk Kim 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>
| 1/*- 2 * Copyright (c) 2004 Jung-uk Kim 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>
|
85 case 0x02041106: 86 return ("VIA 8380 host to PCI bridge"); 87 case 0x02821106: 88 return ("VIA K8T800Pro host to PCI bridge"); 89 case 0x31881106: 90 return ("VIA 8385 host to PCI bridge"); 91 }; 92 93 return NULL; 94} 95 96static int 97agp_amd64_probe(device_t dev) 98{ 99 const char *desc; 100 101 if (resource_disabled("agp", device_get_unit(dev))) 102 return ENXIO; 103 if ((desc = agp_amd64_match(dev))) { 104 device_verbose(dev); 105 device_set_desc(dev, desc); 106 return BUS_PROBE_DEFAULT; 107 } 108 109 return ENXIO; 110} 111 112static int 113agp_amd64_attach(device_t dev) 114{ 115 struct agp_amd64_softc *sc = device_get_softc(dev); 116 struct agp_gatt *gatt; 117 int i, n, error; 118 119 for (i = 0, n = 0; i < PCI_SLOTMAX && n < AMD64_MAX_MCTRL; i++) 120 if (pci_cfgregread(0, i, 3, 0, 4) == 0x11031022) { 121 sc->mctrl[n] = i; 122 n++; 123 } 124 125 if (n == 0) 126 return ENXIO; 127 128 sc->n_mctrl = n; 129 130 if (bootverbose) 131 printf("AMD64: %d Misc. Control unit(s) found.\n", sc->n_mctrl); 132 133 if ((error = agp_generic_attach(dev))) 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 and enable aperture. */ 155 for (i = 0; i < sc->n_mctrl; i++) { 156 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);
| 87 case 0x02041106: 88 return ("VIA 8380 host to PCI bridge"); 89 case 0x02821106: 90 return ("VIA K8T800Pro host to PCI bridge"); 91 case 0x31881106: 92 return ("VIA 8385 host to PCI bridge"); 93 }; 94 95 return NULL; 96} 97 98static int 99agp_amd64_probe(device_t dev) 100{ 101 const char *desc; 102 103 if (resource_disabled("agp", device_get_unit(dev))) 104 return ENXIO; 105 if ((desc = agp_amd64_match(dev))) { 106 device_verbose(dev); 107 device_set_desc(dev, desc); 108 return BUS_PROBE_DEFAULT; 109 } 110 111 return ENXIO; 112} 113 114static int 115agp_amd64_attach(device_t dev) 116{ 117 struct agp_amd64_softc *sc = device_get_softc(dev); 118 struct agp_gatt *gatt; 119 int i, n, error; 120 121 for (i = 0, n = 0; i < PCI_SLOTMAX && n < AMD64_MAX_MCTRL; i++) 122 if (pci_cfgregread(0, i, 3, 0, 4) == 0x11031022) { 123 sc->mctrl[n] = i; 124 n++; 125 } 126 127 if (n == 0) 128 return ENXIO; 129 130 sc->n_mctrl = n; 131 132 if (bootverbose) 133 printf("AMD64: %d Misc. Control unit(s) found.\n", sc->n_mctrl); 134 135 if ((error = agp_generic_attach(dev))) 136 return error; 137 138 sc->initial_aperture = AGP_GET_APERTURE(dev); 139 140 for (;;) { 141 gatt = agp_alloc_gatt(dev); 142 if (gatt) 143 break; 144 145 /* 146 * Probably contigmalloc failure. Try reducing the 147 * aperture so that the gatt size reduces. 148 */ 149 if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2)) { 150 agp_generic_detach(dev); 151 return ENOMEM; 152 } 153 } 154 sc->gatt = gatt; 155 156 /* Install the gatt and enable aperture. */ 157 for (i = 0; i < sc->n_mctrl; i++) { 158 pci_cfgregwrite(0, sc->mctrl[i], 3, AGP_AMD64_ATTBASE, 159 (uint32_t)(gatt->ag_physical >> 8) & AGP_AMD64_ATTBASE_MASK, 160 4); 161 pci_cfgregwrite(0, sc->mctrl[i], 3, AGP_AMD64_APCTRL, 162 (pci_cfgregread(0, sc->mctrl[i], 3, AGP_AMD64_APCTRL, 4) | 163 AGP_AMD64_APCTRL_GARTEN) & 164 ~(AGP_AMD64_APCTRL_DISGARTCPU | AGP_AMD64_APCTRL_DISGARTIO), 165 4); 166 } 167 168 agp_flush_cache(); 169 170 return 0; 171} 172 173static int 174agp_amd64_detach(device_t dev) 175{ 176 struct agp_amd64_softc *sc = device_get_softc(dev); 177 int i, error; 178 179 if ((error = agp_generic_detach(dev))) 180 return error; 181 182 for (i = 0; i < sc->n_mctrl; i++) 183 pci_cfgregwrite(0, sc->mctrl[i], 3, AGP_AMD64_APCTRL, 184 pci_cfgregread(0, sc->mctrl[i], 3, AGP_AMD64_APCTRL, 4) & 185 ~AGP_AMD64_APCTRL_GARTEN, 4); 186 187 AGP_SET_APERTURE(dev, sc->initial_aperture); 188 agp_free_gatt(sc->gatt); 189 190 return 0; 191} 192 193static uint32_t agp_amd64_table[] = { 194 0x02000000, /* 32 MB */ 195 0x04000000, /* 64 MB */ 196 0x08000000, /* 128 MB */ 197 0x10000000, /* 256 MB */ 198 0x20000000, /* 512 MB */ 199 0x40000000, /* 1024 MB */ 200 0x80000000, /* 2048 MB */ 201}; 202 203#define AGP_AMD64_TABLE_SIZE \ 204 (sizeof(agp_amd64_table) / sizeof(agp_amd64_table[0])) 205 206static uint32_t 207agp_amd64_get_aperture(device_t dev) 208{ 209 struct agp_amd64_softc *sc = device_get_softc(dev); 210 uint32_t i; 211 212 i = (pci_cfgregread(0, sc->mctrl[0], 3, AGP_AMD64_APCTRL, 4) & 213 AGP_AMD64_APCTRL_SIZE_MASK) >> 1; 214 215 if (i >= AGP_AMD64_TABLE_SIZE) 216 return 0; 217 218 return (agp_amd64_table[i]); 219} 220 221static int 222agp_amd64_set_aperture(device_t dev, uint32_t aperture) 223{ 224 struct agp_amd64_softc *sc = device_get_softc(dev); 225 uint32_t i; 226 int j; 227 228 for (i = 0; i < AGP_AMD64_TABLE_SIZE; i++) 229 if (agp_amd64_table[i] == aperture) 230 break; 231 if (i == AGP_AMD64_TABLE_SIZE) 232 return EINVAL; 233 234 for (j = 0; j < sc->n_mctrl; j++) 235 pci_cfgregwrite(0, sc->mctrl[j], 3, AGP_AMD64_APCTRL, 236 (pci_cfgregread(0, sc->mctrl[j], 3, AGP_AMD64_APCTRL, 4) & 237 ~(AGP_AMD64_APCTRL_SIZE_MASK)) | (i << 1), 4); 238 239 return 0; 240} 241 242static int 243agp_amd64_bind_page(device_t dev, int offset, vm_offset_t physical) 244{ 245 struct agp_amd64_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_amd64_unbind_page(device_t dev, int offset) 256{ 257 struct agp_amd64_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_amd64_flush_tlb(device_t dev) 268{ 269 struct agp_amd64_softc *sc = device_get_softc(dev); 270 int i; 271 272 for (i = 0; i < sc->n_mctrl; i++) 273 pci_cfgregwrite(0, sc->mctrl[i], 3, AGP_AMD64_CACHECTRL, 274 pci_cfgregread(0, sc->mctrl[i], 3, AGP_AMD64_CACHECTRL, 4) | 275 AGP_AMD64_CACHECTRL_INVGART, 4); 276} 277 278static device_method_t agp_amd64_methods[] = { 279 /* Device interface */ 280 DEVMETHOD(device_probe, agp_amd64_probe), 281 DEVMETHOD(device_attach, agp_amd64_attach), 282 DEVMETHOD(device_detach, agp_amd64_detach), 283 DEVMETHOD(device_shutdown, bus_generic_shutdown), 284 DEVMETHOD(device_suspend, bus_generic_suspend), 285 DEVMETHOD(device_resume, bus_generic_resume), 286 287 /* AGP interface */ 288 DEVMETHOD(agp_get_aperture, agp_amd64_get_aperture), 289 DEVMETHOD(agp_set_aperture, agp_amd64_set_aperture), 290 DEVMETHOD(agp_bind_page, agp_amd64_bind_page), 291 DEVMETHOD(agp_unbind_page, agp_amd64_unbind_page), 292 DEVMETHOD(agp_flush_tlb, agp_amd64_flush_tlb), 293 DEVMETHOD(agp_enable, agp_generic_enable), 294 DEVMETHOD(agp_alloc_memory, agp_generic_alloc_memory), 295 DEVMETHOD(agp_free_memory, agp_generic_free_memory), 296 DEVMETHOD(agp_bind_memory, agp_generic_bind_memory), 297 DEVMETHOD(agp_unbind_memory, agp_generic_unbind_memory), 298 299 { 0, 0 } 300}; 301 302static driver_t agp_amd64_driver = { 303 "agp", 304 agp_amd64_methods, 305 sizeof(struct agp_amd64_softc), 306}; 307 308static devclass_t agp_devclass; 309 310DRIVER_MODULE(agp_amd64, pci, agp_amd64_driver, agp_devclass, 0, 0); 311MODULE_DEPEND(agp_amd64, agp, 1, 1, 1); 312MODULE_DEPEND(agp_amd64, pci, 1, 1, 1);
|