agp_apple.c revision 272461
1/*- 2 * Copyright (c) 2010 Nathan Whitehorn 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: releng/10.1/sys/dev/agp/agp_apple.c 243095 2012-11-15 18:49:17Z eadler $"); 29 30#include <sys/param.h> 31#include <sys/systm.h> 32#include <sys/malloc.h> 33#include <sys/kernel.h> 34#include <sys/module.h> 35#include <sys/bus.h> 36#include <sys/lock.h> 37#include <sys/mutex.h> 38#include <sys/proc.h> 39 40#include <machine/resource.h> 41 42#include <dev/agp/agppriv.h> 43#include <dev/agp/agpreg.h> 44#include <dev/pci/pcivar.h> 45#include <dev/pci/pcireg.h> 46 47#include <vm/vm.h> 48#include <vm/vm_object.h> 49#include <vm/pmap.h> 50 51#define UNIN_AGP_GART_BASE 0x8c 52#define UNIN_AGP_BASE_ADDR 0x90 53#define UNIN_AGP_GART_CONTROL 0x94 54 55#define UNIN_AGP_GART_INVAL 0x00000001 56#define UNIN_AGP_GART_ENABLE 0x00000100 57#define UNIN_AGP_GART_2XRESET 0x00010000 58#define UNIN_AGP_U3_GART_PERFRD 0x00080000 59 60struct agp_apple_softc { 61 struct agp_softc agp; 62 uint32_t aperture; 63 struct agp_gatt *gatt; 64 int u3; 65 int needs_2x_reset; 66}; 67 68static int 69agp_apple_probe(device_t dev) 70{ 71 72 if (resource_disabled("agp", device_get_unit(dev))) 73 return (ENXIO); 74 75 if (pci_get_class(dev) != PCIC_BRIDGE 76 || pci_get_subclass(dev) != PCIS_BRIDGE_HOST) 77 return (ENXIO); 78 79 if (agp_find_caps(dev) == 0) 80 return (ENXIO); 81 82 if (pci_get_class(dev) != PCIC_BRIDGE 83 || pci_get_subclass(dev) != PCIS_BRIDGE_HOST) 84 return (ENXIO); 85 86 switch (pci_get_devid(dev)) { 87 case 0x0020106b: 88 case 0x0027106b: 89 device_set_desc(dev, "Apple UniNorth AGP Bridge"); 90 return (BUS_PROBE_DEFAULT); 91 case 0x002d106b: 92 device_set_desc(dev, "Apple UniNorth 1.5 AGP Bridge"); 93 return (BUS_PROBE_DEFAULT); 94 case 0x0034106b: 95 device_set_desc(dev, "Apple UniNorth 2 AGP Bridge"); 96 return (BUS_PROBE_DEFAULT); 97 case 0x004b106b: 98 case 0x0058106b: 99 case 0x0059106b: 100 device_set_desc(dev, "Apple U3 AGP Bridge"); 101 return (BUS_PROBE_DEFAULT); 102 case 0x0066106b: 103 device_set_desc(dev, "Apple Intrepid AGP Bridge"); 104 return (BUS_PROBE_DEFAULT); 105 } 106 107 return (ENXIO); 108} 109 110static int 111agp_apple_attach(device_t dev) 112{ 113 struct agp_apple_softc *sc = device_get_softc(dev); 114 int error; 115 116 /* Record quirks */ 117 sc->needs_2x_reset = 0; 118 sc->u3 = 0; 119 switch (pci_get_devid(dev)) { 120 case 0x0020106b: 121 case 0x0027106b: 122 sc->needs_2x_reset = 1; 123 break; 124 case 0x004b106b: 125 case 0x0058106b: 126 case 0x0059106b: 127 sc->u3 = 1; 128 break; 129 } 130 131 /* Set the aperture bus address base (must be 0) */ 132 pci_write_config(dev, UNIN_AGP_BASE_ADDR, 0, 4); 133 agp_set_aperture_resource(dev, -1); 134 135 error = agp_generic_attach(dev); 136 if (error) 137 return (error); 138 139 sc->aperture = 256*1024*1024; 140 141 for (sc->aperture = 256*1024*1024; sc->aperture >= 4*1024*1024; 142 sc->aperture /= 2) { 143 sc->gatt = agp_alloc_gatt(dev); 144 if (sc->gatt) 145 break; 146 } 147 if (sc->aperture < 4*1024*1024) { 148 agp_generic_detach(dev); 149 return ENOMEM; 150 } 151 152 /* Install the gatt. */ 153 AGP_SET_APERTURE(dev, sc->aperture); 154 155 /* XXX: U3 scratch page? */ 156 157 /* Enable the aperture and TLB. */ 158 AGP_FLUSH_TLB(dev); 159 160 return (0); 161} 162 163static int 164agp_apple_detach(device_t dev) 165{ 166 struct agp_apple_softc *sc = device_get_softc(dev); 167 168 agp_free_cdev(dev); 169 170 /* Disable the aperture and TLB */ 171 pci_write_config(dev, UNIN_AGP_GART_CONTROL, UNIN_AGP_GART_INVAL, 4); 172 pci_write_config(dev, UNIN_AGP_GART_CONTROL, 0, 4); 173 174 if (sc->needs_2x_reset) { 175 pci_write_config(dev, UNIN_AGP_GART_CONTROL, 176 UNIN_AGP_GART_2XRESET, 4); 177 pci_write_config(dev, UNIN_AGP_GART_CONTROL, 0, 4); 178 } 179 180 AGP_SET_APERTURE(dev, 0); 181 182 agp_free_gatt(sc->gatt); 183 agp_free_res(dev); 184 return 0; 185} 186 187static uint32_t 188agp_apple_get_aperture(device_t dev) 189{ 190 struct agp_apple_softc *sc = device_get_softc(dev); 191 192 return (sc->aperture); 193} 194 195static int 196agp_apple_set_aperture(device_t dev, uint32_t aperture) 197{ 198 struct agp_apple_softc *sc = device_get_softc(dev); 199 200 /* 201 * Check for a multiple of 4 MB and make sure it is within the 202 * programmable range. 203 */ 204 if (aperture % (4*1024*1024) 205 || aperture < 4*1024*1024 206 || aperture > ((sc->u3) ? 512 : 256)*1024*1024) 207 return EINVAL; 208 209 /* The aperture value is a multiple of 4 MB */ 210 aperture /= (4*1024*1024); 211 212 pci_write_config(dev, UNIN_AGP_GART_BASE, 213 (sc->gatt->ag_physical & 0xfffff000) | aperture, 4); 214 215 return (0); 216} 217 218static int 219agp_apple_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical) 220{ 221 struct agp_apple_softc *sc = device_get_softc(dev); 222 223 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) 224 return EINVAL; 225 226 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical; 227 __asm __volatile("dcbst 0,%0; sync" :: 228 "r"(&sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT]) : "memory"); 229 return (0); 230} 231 232static int 233agp_apple_unbind_page(device_t dev, vm_offset_t offset) 234{ 235 struct agp_apple_softc *sc = device_get_softc(dev); 236 237 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) 238 return EINVAL; 239 240 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0; 241 __asm __volatile("dcbst 0,%0; sync" :: 242 "r"(&sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT]) : "memory"); 243 return (0); 244} 245 246static void 247agp_apple_flush_tlb(device_t dev) 248{ 249 struct agp_apple_softc *sc = device_get_softc(dev); 250 uint32_t cntrl = UNIN_AGP_GART_ENABLE; 251 252 if (sc->u3) 253 cntrl |= UNIN_AGP_U3_GART_PERFRD; 254 255 pci_write_config(dev, UNIN_AGP_GART_CONTROL, 256 cntrl | UNIN_AGP_GART_INVAL, 4); 257 pci_write_config(dev, UNIN_AGP_GART_CONTROL, cntrl, 4); 258 259 if (sc->needs_2x_reset) { 260 pci_write_config(dev, UNIN_AGP_GART_CONTROL, 261 cntrl | UNIN_AGP_GART_2XRESET, 4); 262 pci_write_config(dev, UNIN_AGP_GART_CONTROL, cntrl, 4); 263 } 264} 265 266static device_method_t agp_apple_methods[] = { 267 /* Device interface */ 268 DEVMETHOD(device_probe, agp_apple_probe), 269 DEVMETHOD(device_attach, agp_apple_attach), 270 DEVMETHOD(device_detach, agp_apple_detach), 271 DEVMETHOD(device_shutdown, bus_generic_shutdown), 272 DEVMETHOD(device_suspend, bus_generic_suspend), 273 DEVMETHOD(device_resume, bus_generic_resume), 274 275 /* AGP interface */ 276 DEVMETHOD(agp_get_aperture, agp_apple_get_aperture), 277 DEVMETHOD(agp_set_aperture, agp_apple_set_aperture), 278 DEVMETHOD(agp_bind_page, agp_apple_bind_page), 279 DEVMETHOD(agp_unbind_page, agp_apple_unbind_page), 280 DEVMETHOD(agp_flush_tlb, agp_apple_flush_tlb), 281 DEVMETHOD(agp_enable, agp_generic_enable), 282 DEVMETHOD(agp_alloc_memory, agp_generic_alloc_memory), 283 DEVMETHOD(agp_free_memory, agp_generic_free_memory), 284 DEVMETHOD(agp_bind_memory, agp_generic_bind_memory), 285 DEVMETHOD(agp_unbind_memory, agp_generic_unbind_memory), 286 287 { 0, 0 } 288}; 289 290static driver_t agp_apple_driver = { 291 "agp", 292 agp_apple_methods, 293 sizeof(struct agp_apple_softc), 294}; 295 296static devclass_t agp_devclass; 297 298DRIVER_MODULE(agp_apple, hostb, agp_apple_driver, agp_devclass, 0, 0); 299MODULE_DEPEND(agp_apple, agp, 1, 1, 1); 300MODULE_DEPEND(agp_apple, pci, 1, 1, 1); 301