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