2 * Copyright (c) 2010 Nathan Whitehorn
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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>
39 #include <sys/mutex.h>
42 #include <machine/resource.h>
44 #include <dev/agp/agppriv.h>
45 #include <dev/agp/agpreg.h>
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcireg.h>
50 #include <vm/vm_object.h>
53 #define UNIN_AGP_GART_BASE 0x8c
54 #define UNIN_AGP_BASE_ADDR 0x90
55 #define UNIN_AGP_GART_CONTROL 0x94
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
62 struct agp_apple_softc {
65 struct agp_gatt *gatt;
71 agp_apple_probe(device_t dev)
74 if (resource_disabled("agp", device_get_unit(dev)))
77 if (pci_get_class(dev) != PCIC_BRIDGE
78 || pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
81 if (agp_find_caps(dev) == 0)
84 if (pci_get_class(dev) != PCIC_BRIDGE
85 || pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
88 switch (pci_get_devid(dev)) {
91 device_set_desc(dev, "Apple UniNorth AGP Bridge");
92 return (BUS_PROBE_DEFAULT);
94 device_set_desc(dev, "Apple UniNorth 1.5 AGP Bridge");
95 return (BUS_PROBE_DEFAULT);
97 device_set_desc(dev, "Apple UniNorth 2 AGP Bridge");
98 return (BUS_PROBE_DEFAULT);
102 device_set_desc(dev, "Apple U3 AGP Bridge");
103 return (BUS_PROBE_DEFAULT);
105 device_set_desc(dev, "Apple Intrepid AGP Bridge");
106 return (BUS_PROBE_DEFAULT);
113 agp_apple_attach(device_t dev)
115 struct agp_apple_softc *sc = device_get_softc(dev);
119 sc->needs_2x_reset = 0;
121 switch (pci_get_devid(dev)) {
124 sc->needs_2x_reset = 1;
133 /* Set the aperture bus address base (must be 0) */
134 pci_write_config(dev, UNIN_AGP_BASE_ADDR, 0, 4);
135 agp_set_aperture_resource(dev, -1);
137 error = agp_generic_attach(dev);
141 sc->aperture = 256*1024*1024;
143 for (sc->aperture = 256*1024*1024; sc->aperture >= 4*1024*1024;
145 sc->gatt = agp_alloc_gatt(dev);
149 if (sc->aperture < 4*1024*1024) {
150 agp_generic_detach(dev);
154 /* Install the gatt. */
155 AGP_SET_APERTURE(dev, sc->aperture);
157 /* XXX: U3 scratch page? */
159 /* Enable the aperture and TLB. */
166 agp_apple_detach(device_t dev)
168 struct agp_apple_softc *sc = device_get_softc(dev);
172 /* Disable the aperture and TLB */
173 pci_write_config(dev, UNIN_AGP_GART_CONTROL, UNIN_AGP_GART_INVAL, 4);
174 pci_write_config(dev, UNIN_AGP_GART_CONTROL, 0, 4);
176 if (sc->needs_2x_reset) {
177 pci_write_config(dev, UNIN_AGP_GART_CONTROL,
178 UNIN_AGP_GART_2XRESET, 4);
179 pci_write_config(dev, UNIN_AGP_GART_CONTROL, 0, 4);
182 AGP_SET_APERTURE(dev, 0);
184 agp_free_gatt(sc->gatt);
190 agp_apple_get_aperture(device_t dev)
192 struct agp_apple_softc *sc = device_get_softc(dev);
194 return (sc->aperture);
198 agp_apple_set_aperture(device_t dev, uint32_t aperture)
200 struct agp_apple_softc *sc = device_get_softc(dev);
203 * Check for a multiple of 4 MB and make sure it is within the
204 * programmable range.
206 if (aperture % (4*1024*1024)
207 || aperture < 4*1024*1024
208 || aperture > ((sc->u3) ? 512 : 256)*1024*1024)
211 /* The aperture value is a multiple of 4 MB */
212 aperture /= (4*1024*1024);
214 pci_write_config(dev, UNIN_AGP_GART_BASE,
215 (sc->gatt->ag_physical & 0xfffff000) | aperture, 4);
221 agp_apple_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical)
223 struct agp_apple_softc *sc = device_get_softc(dev);
225 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
228 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical;
229 __asm __volatile("dcbst 0,%0; sync" ::
230 "r"(&sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT]) : "memory");
235 agp_apple_unbind_page(device_t dev, vm_offset_t offset)
237 struct agp_apple_softc *sc = device_get_softc(dev);
239 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
242 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
243 __asm __volatile("dcbst 0,%0; sync" ::
244 "r"(&sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT]) : "memory");
249 agp_apple_flush_tlb(device_t dev)
251 struct agp_apple_softc *sc = device_get_softc(dev);
252 uint32_t cntrl = UNIN_AGP_GART_ENABLE;
255 cntrl |= UNIN_AGP_U3_GART_PERFRD;
257 pci_write_config(dev, UNIN_AGP_GART_CONTROL,
258 cntrl | UNIN_AGP_GART_INVAL, 4);
259 pci_write_config(dev, UNIN_AGP_GART_CONTROL, cntrl, 4);
261 if (sc->needs_2x_reset) {
262 pci_write_config(dev, UNIN_AGP_GART_CONTROL,
263 cntrl | UNIN_AGP_GART_2XRESET, 4);
264 pci_write_config(dev, UNIN_AGP_GART_CONTROL, cntrl, 4);
268 static device_method_t agp_apple_methods[] = {
269 /* Device interface */
270 DEVMETHOD(device_probe, agp_apple_probe),
271 DEVMETHOD(device_attach, agp_apple_attach),
272 DEVMETHOD(device_detach, agp_apple_detach),
273 DEVMETHOD(device_shutdown, bus_generic_shutdown),
274 DEVMETHOD(device_suspend, bus_generic_suspend),
275 DEVMETHOD(device_resume, bus_generic_resume),
278 DEVMETHOD(agp_get_aperture, agp_apple_get_aperture),
279 DEVMETHOD(agp_set_aperture, agp_apple_set_aperture),
280 DEVMETHOD(agp_bind_page, agp_apple_bind_page),
281 DEVMETHOD(agp_unbind_page, agp_apple_unbind_page),
282 DEVMETHOD(agp_flush_tlb, agp_apple_flush_tlb),
283 DEVMETHOD(agp_enable, agp_generic_enable),
284 DEVMETHOD(agp_alloc_memory, agp_generic_alloc_memory),
285 DEVMETHOD(agp_free_memory, agp_generic_free_memory),
286 DEVMETHOD(agp_bind_memory, agp_generic_bind_memory),
287 DEVMETHOD(agp_unbind_memory, agp_generic_unbind_memory),
292 static driver_t agp_apple_driver = {
295 sizeof(struct agp_apple_softc),
298 static devclass_t agp_devclass;
300 DRIVER_MODULE(agp_apple, hostb, agp_apple_driver, agp_devclass, 0, 0);
301 MODULE_DEPEND(agp_apple, agp, 1, 1, 1);
302 MODULE_DEPEND(agp_apple, pci, 1, 1, 1);