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/param.h> 30 #include <sys/systm.h> 31 #include <sys/malloc.h> 32 #include <sys/kernel.h> 33 #include <sys/module.h> 34 #include <sys/bus.h> 35 #include <sys/lock.h> 36 #include <sys/mutex.h> 37 #include <sys/proc.h> 38 39 #include <machine/resource.h> 40 41 #include <dev/agp/agppriv.h> 42 #include <dev/agp/agpreg.h> 43 #include <dev/pci/pcivar.h> 44 #include <dev/pci/pcireg.h> 45 46 #include <vm/vm.h> 47 #include <vm/vm_object.h> 48 #include <vm/pmap.h> 49 50 #define UNIN_AGP_GART_BASE 0x8c 51 #define UNIN_AGP_BASE_ADDR 0x90 52 #define UNIN_AGP_GART_CONTROL 0x94 53 54 #define UNIN_AGP_GART_INVAL 0x00000001 55 #define UNIN_AGP_GART_ENABLE 0x00000100 56 #define UNIN_AGP_GART_2XRESET 0x00010000 57 #define UNIN_AGP_U3_GART_PERFRD 0x00080000 58 59 struct agp_apple_softc { 60 struct agp_softc agp; 61 uint32_t aperture; 62 struct agp_gatt *gatt; 63 int u3; 64 int needs_2x_reset; 65 }; 66 67 static int 68 agp_apple_probe(device_t dev) 69 { 70 71 if (resource_disabled("agp", device_get_unit(dev))) 72 return (ENXIO); 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 109 static int 110 agp_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 162 static int 163 agp_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 186 static uint32_t 187 agp_apple_get_aperture(device_t dev) 188 { 189 struct agp_apple_softc *sc = device_get_softc(dev); 190 191 return (sc->aperture); 192 } 193 194 static int 195 agp_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 217 static int 218 agp_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 return (0); 227 } 228 229 static int 230 agp_apple_unbind_page(device_t dev, vm_offset_t offset) 231 { 232 struct agp_apple_softc *sc = device_get_softc(dev); 233 234 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) 235 return EINVAL; 236 237 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0; 238 return (0); 239 } 240 241 static void 242 agp_apple_flush_tlb(device_t dev) 243 { 244 struct agp_apple_softc *sc = device_get_softc(dev); 245 uint32_t cntrl = UNIN_AGP_GART_ENABLE; 246 247 if (sc->u3) 248 cntrl |= UNIN_AGP_U3_GART_PERFRD; 249 250 pci_write_config(dev, UNIN_AGP_GART_CONTROL, 251 cntrl | UNIN_AGP_GART_INVAL, 4); 252 pci_write_config(dev, UNIN_AGP_GART_CONTROL, cntrl, 4); 253 254 if (sc->needs_2x_reset) { 255 pci_write_config(dev, UNIN_AGP_GART_CONTROL, 256 cntrl | UNIN_AGP_GART_2XRESET, 4); 257 pci_write_config(dev, UNIN_AGP_GART_CONTROL, cntrl, 4); 258 } 259 } 260 261 static device_method_t agp_apple_methods[] = { 262 /* Device interface */ 263 DEVMETHOD(device_probe, agp_apple_probe), 264 DEVMETHOD(device_attach, agp_apple_attach), 265 DEVMETHOD(device_detach, agp_apple_detach), 266 DEVMETHOD(device_shutdown, bus_generic_shutdown), 267 DEVMETHOD(device_suspend, bus_generic_suspend), 268 DEVMETHOD(device_resume, bus_generic_resume), 269 270 /* AGP interface */ 271 DEVMETHOD(agp_get_aperture, agp_apple_get_aperture), 272 DEVMETHOD(agp_set_aperture, agp_apple_set_aperture), 273 DEVMETHOD(agp_bind_page, agp_apple_bind_page), 274 DEVMETHOD(agp_unbind_page, agp_apple_unbind_page), 275 DEVMETHOD(agp_flush_tlb, agp_apple_flush_tlb), 276 DEVMETHOD(agp_enable, agp_generic_enable), 277 DEVMETHOD(agp_alloc_memory, agp_generic_alloc_memory), 278 DEVMETHOD(agp_free_memory, agp_generic_free_memory), 279 DEVMETHOD(agp_bind_memory, agp_generic_bind_memory), 280 DEVMETHOD(agp_unbind_memory, agp_generic_unbind_memory), 281 { 0, 0 } 282 }; 283 284 static driver_t agp_apple_driver = { 285 "agp", 286 agp_apple_methods, 287 sizeof(struct agp_apple_softc), 288 }; 289 290 DRIVER_MODULE(agp_apple, hostb, agp_apple_driver, 0, 0); 291 MODULE_DEPEND(agp_apple, agp, 1, 1, 1); 292 MODULE_DEPEND(agp_apple, pci, 1, 1, 1); 293