1214603Snwhitehorn/*- 2214603Snwhitehorn * Copyright (c) 2010 Nathan Whitehorn 3214603Snwhitehorn * All rights reserved. 4214603Snwhitehorn * 5214603Snwhitehorn * Redistribution and use in source and binary forms, with or without 6214603Snwhitehorn * modification, are permitted provided that the following conditions 7214603Snwhitehorn * are met: 8214603Snwhitehorn * 1. Redistributions of source code must retain the above copyright 9214603Snwhitehorn * notice, this list of conditions and the following disclaimer. 10214603Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright 11214603Snwhitehorn * notice, this list of conditions and the following disclaimer in the 12214603Snwhitehorn * documentation and/or other materials provided with the distribution. 13214603Snwhitehorn * 14214603Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15214603Snwhitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16214603Snwhitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17214603Snwhitehorn * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18214603Snwhitehorn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19214603Snwhitehorn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20214603Snwhitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21214603Snwhitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22214603Snwhitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23214603Snwhitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24214603Snwhitehorn * SUCH DAMAGE. 25214603Snwhitehorn */ 26214603Snwhitehorn 27214603Snwhitehorn#include <sys/cdefs.h> 28214603Snwhitehorn__FBSDID("$FreeBSD$"); 29214603Snwhitehorn 30214603Snwhitehorn#include "opt_bus.h" 31214603Snwhitehorn 32214603Snwhitehorn#include <sys/param.h> 33214603Snwhitehorn#include <sys/systm.h> 34214603Snwhitehorn#include <sys/malloc.h> 35214603Snwhitehorn#include <sys/kernel.h> 36214603Snwhitehorn#include <sys/module.h> 37214603Snwhitehorn#include <sys/bus.h> 38214603Snwhitehorn#include <sys/lock.h> 39214603Snwhitehorn#include <sys/mutex.h> 40214603Snwhitehorn#include <sys/proc.h> 41214603Snwhitehorn 42214603Snwhitehorn#include <machine/resource.h> 43214603Snwhitehorn 44214603Snwhitehorn#include <dev/agp/agppriv.h> 45214603Snwhitehorn#include <dev/agp/agpreg.h> 46214603Snwhitehorn#include <dev/pci/pcivar.h> 47214603Snwhitehorn#include <dev/pci/pcireg.h> 48214603Snwhitehorn 49214603Snwhitehorn#include <vm/vm.h> 50214603Snwhitehorn#include <vm/vm_object.h> 51214603Snwhitehorn#include <vm/pmap.h> 52214603Snwhitehorn 53214603Snwhitehorn#define UNIN_AGP_GART_BASE 0x8c 54214603Snwhitehorn#define UNIN_AGP_BASE_ADDR 0x90 55214603Snwhitehorn#define UNIN_AGP_GART_CONTROL 0x94 56214603Snwhitehorn 57214603Snwhitehorn#define UNIN_AGP_GART_INVAL 0x00000001 58214603Snwhitehorn#define UNIN_AGP_GART_ENABLE 0x00000100 59214603Snwhitehorn#define UNIN_AGP_GART_2XRESET 0x00010000 60214603Snwhitehorn#define UNIN_AGP_U3_GART_PERFRD 0x00080000 61214603Snwhitehorn 62214603Snwhitehornstruct agp_apple_softc { 63214603Snwhitehorn struct agp_softc agp; 64214603Snwhitehorn uint32_t aperture; 65214603Snwhitehorn struct agp_gatt *gatt; 66214603Snwhitehorn int u3; 67214603Snwhitehorn int needs_2x_reset; 68214603Snwhitehorn}; 69214603Snwhitehorn 70214603Snwhitehornstatic int 71214603Snwhitehornagp_apple_probe(device_t dev) 72214603Snwhitehorn{ 73214603Snwhitehorn 74214603Snwhitehorn if (resource_disabled("agp", device_get_unit(dev))) 75214603Snwhitehorn return (ENXIO); 76214603Snwhitehorn 77214603Snwhitehorn if (pci_get_class(dev) != PCIC_BRIDGE 78214603Snwhitehorn || pci_get_subclass(dev) != PCIS_BRIDGE_HOST) 79214603Snwhitehorn return (ENXIO); 80214603Snwhitehorn 81214603Snwhitehorn if (agp_find_caps(dev) == 0) 82214603Snwhitehorn return (ENXIO); 83214603Snwhitehorn 84214603Snwhitehorn if (pci_get_class(dev) != PCIC_BRIDGE 85214603Snwhitehorn || pci_get_subclass(dev) != PCIS_BRIDGE_HOST) 86214603Snwhitehorn return (ENXIO); 87214603Snwhitehorn 88214603Snwhitehorn switch (pci_get_devid(dev)) { 89214603Snwhitehorn case 0x0020106b: 90214603Snwhitehorn case 0x0027106b: 91214603Snwhitehorn device_set_desc(dev, "Apple UniNorth AGP Bridge"); 92214603Snwhitehorn return (BUS_PROBE_DEFAULT); 93214603Snwhitehorn case 0x002d106b: 94214603Snwhitehorn device_set_desc(dev, "Apple UniNorth 1.5 AGP Bridge"); 95214603Snwhitehorn return (BUS_PROBE_DEFAULT); 96214603Snwhitehorn case 0x0034106b: 97214603Snwhitehorn device_set_desc(dev, "Apple UniNorth 2 AGP Bridge"); 98214603Snwhitehorn return (BUS_PROBE_DEFAULT); 99214603Snwhitehorn case 0x004b106b: 100214603Snwhitehorn case 0x0058106b: 101214603Snwhitehorn case 0x0059106b: 102214603Snwhitehorn device_set_desc(dev, "Apple U3 AGP Bridge"); 103214603Snwhitehorn return (BUS_PROBE_DEFAULT); 104214603Snwhitehorn case 0x0066106b: 105214603Snwhitehorn device_set_desc(dev, "Apple Intrepid AGP Bridge"); 106214603Snwhitehorn return (BUS_PROBE_DEFAULT); 107214603Snwhitehorn } 108214603Snwhitehorn 109214603Snwhitehorn return (ENXIO); 110214603Snwhitehorn} 111214603Snwhitehorn 112214603Snwhitehornstatic int 113214603Snwhitehornagp_apple_attach(device_t dev) 114214603Snwhitehorn{ 115214603Snwhitehorn struct agp_apple_softc *sc = device_get_softc(dev); 116214603Snwhitehorn int error; 117214603Snwhitehorn 118214603Snwhitehorn /* Record quirks */ 119214603Snwhitehorn sc->needs_2x_reset = 0; 120214603Snwhitehorn sc->u3 = 0; 121214603Snwhitehorn switch (pci_get_devid(dev)) { 122214603Snwhitehorn case 0x0020106b: 123214603Snwhitehorn case 0x0027106b: 124214603Snwhitehorn sc->needs_2x_reset = 1; 125214603Snwhitehorn break; 126214603Snwhitehorn case 0x004b106b: 127214603Snwhitehorn case 0x0058106b: 128214603Snwhitehorn case 0x0059106b: 129214603Snwhitehorn sc->u3 = 1; 130214603Snwhitehorn break; 131214603Snwhitehorn } 132214603Snwhitehorn 133214603Snwhitehorn /* Set the aperture bus address base (must be 0) */ 134214603Snwhitehorn pci_write_config(dev, UNIN_AGP_BASE_ADDR, 0, 4); 135214603Snwhitehorn agp_set_aperture_resource(dev, -1); 136214603Snwhitehorn 137214603Snwhitehorn error = agp_generic_attach(dev); 138214603Snwhitehorn if (error) 139214603Snwhitehorn return (error); 140214603Snwhitehorn 141214603Snwhitehorn sc->aperture = 256*1024*1024; 142214603Snwhitehorn 143214603Snwhitehorn for (sc->aperture = 256*1024*1024; sc->aperture >= 4*1024*1024; 144214603Snwhitehorn sc->aperture /= 2) { 145214603Snwhitehorn sc->gatt = agp_alloc_gatt(dev); 146214603Snwhitehorn if (sc->gatt) 147214603Snwhitehorn break; 148214603Snwhitehorn } 149214603Snwhitehorn if (sc->aperture < 4*1024*1024) { 150214603Snwhitehorn agp_generic_detach(dev); 151214603Snwhitehorn return ENOMEM; 152214603Snwhitehorn } 153214603Snwhitehorn 154214603Snwhitehorn /* Install the gatt. */ 155214603Snwhitehorn AGP_SET_APERTURE(dev, sc->aperture); 156214603Snwhitehorn 157214603Snwhitehorn /* XXX: U3 scratch page? */ 158214603Snwhitehorn 159214603Snwhitehorn /* Enable the aperture and TLB. */ 160214603Snwhitehorn AGP_FLUSH_TLB(dev); 161214603Snwhitehorn 162214603Snwhitehorn return (0); 163214603Snwhitehorn} 164214603Snwhitehorn 165214603Snwhitehornstatic int 166214603Snwhitehornagp_apple_detach(device_t dev) 167214603Snwhitehorn{ 168214603Snwhitehorn struct agp_apple_softc *sc = device_get_softc(dev); 169214603Snwhitehorn 170214603Snwhitehorn agp_free_cdev(dev); 171214603Snwhitehorn 172214603Snwhitehorn /* Disable the aperture and TLB */ 173214603Snwhitehorn pci_write_config(dev, UNIN_AGP_GART_CONTROL, UNIN_AGP_GART_INVAL, 4); 174214603Snwhitehorn pci_write_config(dev, UNIN_AGP_GART_CONTROL, 0, 4); 175214603Snwhitehorn 176214603Snwhitehorn if (sc->needs_2x_reset) { 177214603Snwhitehorn pci_write_config(dev, UNIN_AGP_GART_CONTROL, 178214603Snwhitehorn UNIN_AGP_GART_2XRESET, 4); 179214603Snwhitehorn pci_write_config(dev, UNIN_AGP_GART_CONTROL, 0, 4); 180214603Snwhitehorn } 181214603Snwhitehorn 182214603Snwhitehorn AGP_SET_APERTURE(dev, 0); 183214603Snwhitehorn 184214603Snwhitehorn agp_free_gatt(sc->gatt); 185214603Snwhitehorn agp_free_res(dev); 186214603Snwhitehorn return 0; 187214603Snwhitehorn} 188214603Snwhitehorn 189214603Snwhitehornstatic uint32_t 190214603Snwhitehornagp_apple_get_aperture(device_t dev) 191214603Snwhitehorn{ 192214603Snwhitehorn struct agp_apple_softc *sc = device_get_softc(dev); 193214603Snwhitehorn 194214603Snwhitehorn return (sc->aperture); 195214603Snwhitehorn} 196214603Snwhitehorn 197214603Snwhitehornstatic int 198214603Snwhitehornagp_apple_set_aperture(device_t dev, uint32_t aperture) 199214603Snwhitehorn{ 200214603Snwhitehorn struct agp_apple_softc *sc = device_get_softc(dev); 201214603Snwhitehorn 202214603Snwhitehorn /* 203214603Snwhitehorn * Check for a multiple of 4 MB and make sure it is within the 204214603Snwhitehorn * programmable range. 205214603Snwhitehorn */ 206214603Snwhitehorn if (aperture % (4*1024*1024) 207214603Snwhitehorn || aperture < 4*1024*1024 208214603Snwhitehorn || aperture > ((sc->u3) ? 512 : 256)*1024*1024) 209214603Snwhitehorn return EINVAL; 210214603Snwhitehorn 211214603Snwhitehorn /* The aperture value is a multiple of 4 MB */ 212214603Snwhitehorn aperture /= (4*1024*1024); 213214603Snwhitehorn 214214603Snwhitehorn pci_write_config(dev, UNIN_AGP_GART_BASE, 215214603Snwhitehorn (sc->gatt->ag_physical & 0xfffff000) | aperture, 4); 216214603Snwhitehorn 217214603Snwhitehorn return (0); 218214603Snwhitehorn} 219214603Snwhitehorn 220214603Snwhitehornstatic int 221214603Snwhitehornagp_apple_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical) 222214603Snwhitehorn{ 223214603Snwhitehorn struct agp_apple_softc *sc = device_get_softc(dev); 224214603Snwhitehorn 225214603Snwhitehorn if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) 226214603Snwhitehorn return EINVAL; 227214603Snwhitehorn 228214603Snwhitehorn sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical; 229214603Snwhitehorn __asm __volatile("dcbst 0,%0; sync" :: 230214603Snwhitehorn "r"(&sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT]) : "memory"); 231214603Snwhitehorn return (0); 232214603Snwhitehorn} 233214603Snwhitehorn 234214603Snwhitehornstatic int 235214603Snwhitehornagp_apple_unbind_page(device_t dev, vm_offset_t offset) 236214603Snwhitehorn{ 237214603Snwhitehorn struct agp_apple_softc *sc = device_get_softc(dev); 238214603Snwhitehorn 239214603Snwhitehorn if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) 240214603Snwhitehorn return EINVAL; 241214603Snwhitehorn 242214603Snwhitehorn sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0; 243214603Snwhitehorn __asm __volatile("dcbst 0,%0; sync" :: 244214603Snwhitehorn "r"(&sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT]) : "memory"); 245214603Snwhitehorn return (0); 246214603Snwhitehorn} 247214603Snwhitehorn 248214603Snwhitehornstatic void 249214603Snwhitehornagp_apple_flush_tlb(device_t dev) 250214603Snwhitehorn{ 251214603Snwhitehorn struct agp_apple_softc *sc = device_get_softc(dev); 252214603Snwhitehorn uint32_t cntrl = UNIN_AGP_GART_ENABLE; 253214603Snwhitehorn 254214603Snwhitehorn if (sc->u3) 255214603Snwhitehorn cntrl |= UNIN_AGP_U3_GART_PERFRD; 256214603Snwhitehorn 257214603Snwhitehorn pci_write_config(dev, UNIN_AGP_GART_CONTROL, 258214603Snwhitehorn cntrl | UNIN_AGP_GART_INVAL, 4); 259214603Snwhitehorn pci_write_config(dev, UNIN_AGP_GART_CONTROL, cntrl, 4); 260214603Snwhitehorn 261214603Snwhitehorn if (sc->needs_2x_reset) { 262214603Snwhitehorn pci_write_config(dev, UNIN_AGP_GART_CONTROL, 263214603Snwhitehorn cntrl | UNIN_AGP_GART_2XRESET, 4); 264214603Snwhitehorn pci_write_config(dev, UNIN_AGP_GART_CONTROL, cntrl, 4); 265214603Snwhitehorn } 266214603Snwhitehorn} 267214603Snwhitehorn 268214603Snwhitehornstatic device_method_t agp_apple_methods[] = { 269214603Snwhitehorn /* Device interface */ 270214603Snwhitehorn DEVMETHOD(device_probe, agp_apple_probe), 271214603Snwhitehorn DEVMETHOD(device_attach, agp_apple_attach), 272214603Snwhitehorn DEVMETHOD(device_detach, agp_apple_detach), 273214603Snwhitehorn DEVMETHOD(device_shutdown, bus_generic_shutdown), 274214603Snwhitehorn DEVMETHOD(device_suspend, bus_generic_suspend), 275214603Snwhitehorn DEVMETHOD(device_resume, bus_generic_resume), 276214603Snwhitehorn 277214603Snwhitehorn /* AGP interface */ 278214603Snwhitehorn DEVMETHOD(agp_get_aperture, agp_apple_get_aperture), 279214603Snwhitehorn DEVMETHOD(agp_set_aperture, agp_apple_set_aperture), 280214603Snwhitehorn DEVMETHOD(agp_bind_page, agp_apple_bind_page), 281214603Snwhitehorn DEVMETHOD(agp_unbind_page, agp_apple_unbind_page), 282214603Snwhitehorn DEVMETHOD(agp_flush_tlb, agp_apple_flush_tlb), 283214603Snwhitehorn DEVMETHOD(agp_enable, agp_generic_enable), 284214603Snwhitehorn DEVMETHOD(agp_alloc_memory, agp_generic_alloc_memory), 285214603Snwhitehorn DEVMETHOD(agp_free_memory, agp_generic_free_memory), 286214603Snwhitehorn DEVMETHOD(agp_bind_memory, agp_generic_bind_memory), 287214603Snwhitehorn DEVMETHOD(agp_unbind_memory, agp_generic_unbind_memory), 288214603Snwhitehorn 289214603Snwhitehorn { 0, 0 } 290214603Snwhitehorn}; 291214603Snwhitehorn 292214603Snwhitehornstatic driver_t agp_apple_driver = { 293214603Snwhitehorn "agp", 294214603Snwhitehorn agp_apple_methods, 295214603Snwhitehorn sizeof(struct agp_apple_softc), 296214603Snwhitehorn}; 297214603Snwhitehorn 298214603Snwhitehornstatic devclass_t agp_devclass; 299214603Snwhitehorn 300214603SnwhitehornDRIVER_MODULE(agp_apple, hostb, agp_apple_driver, agp_devclass, 0, 0); 301214603SnwhitehornMODULE_DEPEND(agp_apple, agp, 1, 1, 1); 302214603SnwhitehornMODULE_DEPEND(agp_apple, pci, 1, 1, 1); 303