1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2010 Nathan Whitehorn 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 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 60 struct 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 68 static int 69 agp_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 110 static int 111 agp_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 163 static int 164 agp_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 187 static uint32_t 188 agp_apple_get_aperture(device_t dev) 189 { 190 struct agp_apple_softc *sc = device_get_softc(dev); 191 192 return (sc->aperture); 193 } 194 195 static int 196 agp_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 218 static int 219 agp_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 return (0); 228 } 229 230 static int 231 agp_apple_unbind_page(device_t dev, vm_offset_t offset) 232 { 233 struct agp_apple_softc *sc = device_get_softc(dev); 234 235 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) 236 return EINVAL; 237 238 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0; 239 return (0); 240 } 241 242 static void 243 agp_apple_flush_tlb(device_t dev) 244 { 245 struct agp_apple_softc *sc = device_get_softc(dev); 246 uint32_t cntrl = UNIN_AGP_GART_ENABLE; 247 248 if (sc->u3) 249 cntrl |= UNIN_AGP_U3_GART_PERFRD; 250 251 pci_write_config(dev, UNIN_AGP_GART_CONTROL, 252 cntrl | UNIN_AGP_GART_INVAL, 4); 253 pci_write_config(dev, UNIN_AGP_GART_CONTROL, cntrl, 4); 254 255 if (sc->needs_2x_reset) { 256 pci_write_config(dev, UNIN_AGP_GART_CONTROL, 257 cntrl | UNIN_AGP_GART_2XRESET, 4); 258 pci_write_config(dev, UNIN_AGP_GART_CONTROL, cntrl, 4); 259 } 260 } 261 262 static device_method_t agp_apple_methods[] = { 263 /* Device interface */ 264 DEVMETHOD(device_probe, agp_apple_probe), 265 DEVMETHOD(device_attach, agp_apple_attach), 266 DEVMETHOD(device_detach, agp_apple_detach), 267 DEVMETHOD(device_shutdown, bus_generic_shutdown), 268 DEVMETHOD(device_suspend, bus_generic_suspend), 269 DEVMETHOD(device_resume, bus_generic_resume), 270 271 /* AGP interface */ 272 DEVMETHOD(agp_get_aperture, agp_apple_get_aperture), 273 DEVMETHOD(agp_set_aperture, agp_apple_set_aperture), 274 DEVMETHOD(agp_bind_page, agp_apple_bind_page), 275 DEVMETHOD(agp_unbind_page, agp_apple_unbind_page), 276 DEVMETHOD(agp_flush_tlb, agp_apple_flush_tlb), 277 DEVMETHOD(agp_enable, agp_generic_enable), 278 DEVMETHOD(agp_alloc_memory, agp_generic_alloc_memory), 279 DEVMETHOD(agp_free_memory, agp_generic_free_memory), 280 DEVMETHOD(agp_bind_memory, agp_generic_bind_memory), 281 DEVMETHOD(agp_unbind_memory, agp_generic_unbind_memory), 282 { 0, 0 } 283 }; 284 285 static driver_t agp_apple_driver = { 286 "agp", 287 agp_apple_methods, 288 sizeof(struct agp_apple_softc), 289 }; 290 291 DRIVER_MODULE(agp_apple, hostb, agp_apple_driver, 0, 0); 292 MODULE_DEPEND(agp_apple, agp, 1, 1, 1); 293 MODULE_DEPEND(agp_apple, pci, 1, 1, 1); 294