agp_apple.c revision 267654
1219820Sjeff/*- 2219820Sjeff * Copyright (c) 2010 Nathan Whitehorn 3219820Sjeff * All rights reserved. 4219820Sjeff * 5219820Sjeff * Redistribution and use in source and binary forms, with or without 6219820Sjeff * modification, are permitted provided that the following conditions 7219820Sjeff * are met: 8219820Sjeff * 1. Redistributions of source code must retain the above copyright 9219820Sjeff * notice, this list of conditions and the following disclaimer. 10219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright 11219820Sjeff * notice, this list of conditions and the following disclaimer in the 12219820Sjeff * documentation and/or other materials provided with the distribution. 13219820Sjeff * 14219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15219820Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16219820Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17219820Sjeff * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18219820Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19219820Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20219820Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21219820Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22219820Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23219820Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24219820Sjeff * SUCH DAMAGE. 25219820Sjeff */ 26219820Sjeff 27219820Sjeff#include <sys/cdefs.h> 28219820Sjeff__FBSDID("$FreeBSD: releng/9.3/sys/dev/agp/agp_apple.c 214603 2010-10-31 18:27:05Z nwhitehorn $"); 29219820Sjeff 30219820Sjeff#include "opt_bus.h" 31219820Sjeff 32219820Sjeff#include <sys/param.h> 33219820Sjeff#include <sys/systm.h> 34219820Sjeff#include <sys/malloc.h> 35219820Sjeff#include <sys/kernel.h> 36219820Sjeff#include <sys/module.h> 37219820Sjeff#include <sys/bus.h> 38219820Sjeff#include <sys/lock.h> 39219820Sjeff#include <sys/mutex.h> 40219820Sjeff#include <sys/proc.h> 41219820Sjeff 42219820Sjeff#include <machine/resource.h> 43219820Sjeff 44219820Sjeff#include <dev/agp/agppriv.h> 45219820Sjeff#include <dev/agp/agpreg.h> 46219820Sjeff#include <dev/pci/pcivar.h> 47219820Sjeff#include <dev/pci/pcireg.h> 48219820Sjeff 49219820Sjeff#include <vm/vm.h> 50219820Sjeff#include <vm/vm_object.h> 51219820Sjeff#include <vm/pmap.h> 52219820Sjeff 53219820Sjeff#define UNIN_AGP_GART_BASE 0x8c 54219820Sjeff#define UNIN_AGP_BASE_ADDR 0x90 55219820Sjeff#define UNIN_AGP_GART_CONTROL 0x94 56219820Sjeff 57219820Sjeff#define UNIN_AGP_GART_INVAL 0x00000001 58219820Sjeff#define UNIN_AGP_GART_ENABLE 0x00000100 59219820Sjeff#define UNIN_AGP_GART_2XRESET 0x00010000 60219820Sjeff#define UNIN_AGP_U3_GART_PERFRD 0x00080000 61219820Sjeff 62219820Sjeffstruct agp_apple_softc { 63219820Sjeff struct agp_softc agp; 64219820Sjeff uint32_t aperture; 65219820Sjeff struct agp_gatt *gatt; 66219820Sjeff int u3; 67219820Sjeff int needs_2x_reset; 68219820Sjeff}; 69219820Sjeff 70219820Sjeffstatic int 71219820Sjeffagp_apple_probe(device_t dev) 72219820Sjeff{ 73 74 if (resource_disabled("agp", device_get_unit(dev))) 75 return (ENXIO); 76 77 if (pci_get_class(dev) != PCIC_BRIDGE 78 || pci_get_subclass(dev) != PCIS_BRIDGE_HOST) 79 return (ENXIO); 80 81 if (agp_find_caps(dev) == 0) 82 return (ENXIO); 83 84 if (pci_get_class(dev) != PCIC_BRIDGE 85 || pci_get_subclass(dev) != PCIS_BRIDGE_HOST) 86 return (ENXIO); 87 88 switch (pci_get_devid(dev)) { 89 case 0x0020106b: 90 case 0x0027106b: 91 device_set_desc(dev, "Apple UniNorth AGP Bridge"); 92 return (BUS_PROBE_DEFAULT); 93 case 0x002d106b: 94 device_set_desc(dev, "Apple UniNorth 1.5 AGP Bridge"); 95 return (BUS_PROBE_DEFAULT); 96 case 0x0034106b: 97 device_set_desc(dev, "Apple UniNorth 2 AGP Bridge"); 98 return (BUS_PROBE_DEFAULT); 99 case 0x004b106b: 100 case 0x0058106b: 101 case 0x0059106b: 102 device_set_desc(dev, "Apple U3 AGP Bridge"); 103 return (BUS_PROBE_DEFAULT); 104 case 0x0066106b: 105 device_set_desc(dev, "Apple Intrepid AGP Bridge"); 106 return (BUS_PROBE_DEFAULT); 107 } 108 109 return (ENXIO); 110} 111 112static int 113agp_apple_attach(device_t dev) 114{ 115 struct agp_apple_softc *sc = device_get_softc(dev); 116 int error; 117 118 /* Record quirks */ 119 sc->needs_2x_reset = 0; 120 sc->u3 = 0; 121 switch (pci_get_devid(dev)) { 122 case 0x0020106b: 123 case 0x0027106b: 124 sc->needs_2x_reset = 1; 125 break; 126 case 0x004b106b: 127 case 0x0058106b: 128 case 0x0059106b: 129 sc->u3 = 1; 130 break; 131 } 132 133 /* Set the aperture bus address base (must be 0) */ 134 pci_write_config(dev, UNIN_AGP_BASE_ADDR, 0, 4); 135 agp_set_aperture_resource(dev, -1); 136 137 error = agp_generic_attach(dev); 138 if (error) 139 return (error); 140 141 sc->aperture = 256*1024*1024; 142 143 for (sc->aperture = 256*1024*1024; sc->aperture >= 4*1024*1024; 144 sc->aperture /= 2) { 145 sc->gatt = agp_alloc_gatt(dev); 146 if (sc->gatt) 147 break; 148 } 149 if (sc->aperture < 4*1024*1024) { 150 agp_generic_detach(dev); 151 return ENOMEM; 152 } 153 154 /* Install the gatt. */ 155 AGP_SET_APERTURE(dev, sc->aperture); 156 157 /* XXX: U3 scratch page? */ 158 159 /* Enable the aperture and TLB. */ 160 AGP_FLUSH_TLB(dev); 161 162 return (0); 163} 164 165static int 166agp_apple_detach(device_t dev) 167{ 168 struct agp_apple_softc *sc = device_get_softc(dev); 169 170 agp_free_cdev(dev); 171 172 /* Disable the aperture and TLB */ 173 pci_write_config(dev, UNIN_AGP_GART_CONTROL, UNIN_AGP_GART_INVAL, 4); 174 pci_write_config(dev, UNIN_AGP_GART_CONTROL, 0, 4); 175 176 if (sc->needs_2x_reset) { 177 pci_write_config(dev, UNIN_AGP_GART_CONTROL, 178 UNIN_AGP_GART_2XRESET, 4); 179 pci_write_config(dev, UNIN_AGP_GART_CONTROL, 0, 4); 180 } 181 182 AGP_SET_APERTURE(dev, 0); 183 184 agp_free_gatt(sc->gatt); 185 agp_free_res(dev); 186 return 0; 187} 188 189static uint32_t 190agp_apple_get_aperture(device_t dev) 191{ 192 struct agp_apple_softc *sc = device_get_softc(dev); 193 194 return (sc->aperture); 195} 196 197static int 198agp_apple_set_aperture(device_t dev, uint32_t aperture) 199{ 200 struct agp_apple_softc *sc = device_get_softc(dev); 201 202 /* 203 * Check for a multiple of 4 MB and make sure it is within the 204 * programmable range. 205 */ 206 if (aperture % (4*1024*1024) 207 || aperture < 4*1024*1024 208 || aperture > ((sc->u3) ? 512 : 256)*1024*1024) 209 return EINVAL; 210 211 /* The aperture value is a multiple of 4 MB */ 212 aperture /= (4*1024*1024); 213 214 pci_write_config(dev, UNIN_AGP_GART_BASE, 215 (sc->gatt->ag_physical & 0xfffff000) | aperture, 4); 216 217 return (0); 218} 219 220static int 221agp_apple_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical) 222{ 223 struct agp_apple_softc *sc = device_get_softc(dev); 224 225 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) 226 return EINVAL; 227 228 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical; 229 __asm __volatile("dcbst 0,%0; sync" :: 230 "r"(&sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT]) : "memory"); 231 return (0); 232} 233 234static int 235agp_apple_unbind_page(device_t dev, vm_offset_t offset) 236{ 237 struct agp_apple_softc *sc = device_get_softc(dev); 238 239 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) 240 return EINVAL; 241 242 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0; 243 __asm __volatile("dcbst 0,%0; sync" :: 244 "r"(&sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT]) : "memory"); 245 return (0); 246} 247 248static void 249agp_apple_flush_tlb(device_t dev) 250{ 251 struct agp_apple_softc *sc = device_get_softc(dev); 252 uint32_t cntrl = UNIN_AGP_GART_ENABLE; 253 254 if (sc->u3) 255 cntrl |= UNIN_AGP_U3_GART_PERFRD; 256 257 pci_write_config(dev, UNIN_AGP_GART_CONTROL, 258 cntrl | UNIN_AGP_GART_INVAL, 4); 259 pci_write_config(dev, UNIN_AGP_GART_CONTROL, cntrl, 4); 260 261 if (sc->needs_2x_reset) { 262 pci_write_config(dev, UNIN_AGP_GART_CONTROL, 263 cntrl | UNIN_AGP_GART_2XRESET, 4); 264 pci_write_config(dev, UNIN_AGP_GART_CONTROL, cntrl, 4); 265 } 266} 267 268static device_method_t agp_apple_methods[] = { 269 /* Device interface */ 270 DEVMETHOD(device_probe, agp_apple_probe), 271 DEVMETHOD(device_attach, agp_apple_attach), 272 DEVMETHOD(device_detach, agp_apple_detach), 273 DEVMETHOD(device_shutdown, bus_generic_shutdown), 274 DEVMETHOD(device_suspend, bus_generic_suspend), 275 DEVMETHOD(device_resume, bus_generic_resume), 276 277 /* AGP interface */ 278 DEVMETHOD(agp_get_aperture, agp_apple_get_aperture), 279 DEVMETHOD(agp_set_aperture, agp_apple_set_aperture), 280 DEVMETHOD(agp_bind_page, agp_apple_bind_page), 281 DEVMETHOD(agp_unbind_page, agp_apple_unbind_page), 282 DEVMETHOD(agp_flush_tlb, agp_apple_flush_tlb), 283 DEVMETHOD(agp_enable, agp_generic_enable), 284 DEVMETHOD(agp_alloc_memory, agp_generic_alloc_memory), 285 DEVMETHOD(agp_free_memory, agp_generic_free_memory), 286 DEVMETHOD(agp_bind_memory, agp_generic_bind_memory), 287 DEVMETHOD(agp_unbind_memory, agp_generic_unbind_memory), 288 289 { 0, 0 } 290}; 291 292static driver_t agp_apple_driver = { 293 "agp", 294 agp_apple_methods, 295 sizeof(struct agp_apple_softc), 296}; 297 298static devclass_t agp_devclass; 299 300DRIVER_MODULE(agp_apple, hostb, agp_apple_driver, agp_devclass, 0, 0); 301MODULE_DEPEND(agp_apple, agp, 1, 1, 1); 302MODULE_DEPEND(agp_apple, pci, 1, 1, 1); 303