150fd2a5bSNathan Whitehorn /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
450fd2a5bSNathan Whitehorn * Copyright (c) 2010 Nathan Whitehorn
550fd2a5bSNathan Whitehorn * All rights reserved.
650fd2a5bSNathan Whitehorn *
750fd2a5bSNathan Whitehorn * Redistribution and use in source and binary forms, with or without
850fd2a5bSNathan Whitehorn * modification, are permitted provided that the following conditions
950fd2a5bSNathan Whitehorn * are met:
1050fd2a5bSNathan Whitehorn * 1. Redistributions of source code must retain the above copyright
1150fd2a5bSNathan Whitehorn * notice, this list of conditions and the following disclaimer.
1250fd2a5bSNathan Whitehorn * 2. Redistributions in binary form must reproduce the above copyright
1350fd2a5bSNathan Whitehorn * notice, this list of conditions and the following disclaimer in the
1450fd2a5bSNathan Whitehorn * documentation and/or other materials provided with the distribution.
1550fd2a5bSNathan Whitehorn *
1650fd2a5bSNathan Whitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1750fd2a5bSNathan Whitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1850fd2a5bSNathan Whitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1950fd2a5bSNathan Whitehorn * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2050fd2a5bSNathan Whitehorn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2150fd2a5bSNathan Whitehorn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2250fd2a5bSNathan Whitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2350fd2a5bSNathan Whitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2450fd2a5bSNathan Whitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2550fd2a5bSNathan Whitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2650fd2a5bSNathan Whitehorn * SUCH DAMAGE.
2750fd2a5bSNathan Whitehorn */
2850fd2a5bSNathan Whitehorn
2950fd2a5bSNathan Whitehorn #include <sys/param.h>
3050fd2a5bSNathan Whitehorn #include <sys/systm.h>
3150fd2a5bSNathan Whitehorn #include <sys/malloc.h>
3250fd2a5bSNathan Whitehorn #include <sys/kernel.h>
3350fd2a5bSNathan Whitehorn #include <sys/module.h>
3450fd2a5bSNathan Whitehorn #include <sys/bus.h>
3550fd2a5bSNathan Whitehorn #include <sys/lock.h>
3650fd2a5bSNathan Whitehorn #include <sys/mutex.h>
3750fd2a5bSNathan Whitehorn #include <sys/proc.h>
3850fd2a5bSNathan Whitehorn
3950fd2a5bSNathan Whitehorn #include <machine/resource.h>
4050fd2a5bSNathan Whitehorn
4150fd2a5bSNathan Whitehorn #include <dev/agp/agppriv.h>
4250fd2a5bSNathan Whitehorn #include <dev/agp/agpreg.h>
4350fd2a5bSNathan Whitehorn #include <dev/pci/pcivar.h>
4450fd2a5bSNathan Whitehorn #include <dev/pci/pcireg.h>
4550fd2a5bSNathan Whitehorn
4650fd2a5bSNathan Whitehorn #include <vm/vm.h>
4750fd2a5bSNathan Whitehorn #include <vm/vm_object.h>
4850fd2a5bSNathan Whitehorn #include <vm/pmap.h>
4950fd2a5bSNathan Whitehorn
5050fd2a5bSNathan Whitehorn #define UNIN_AGP_GART_BASE 0x8c
5150fd2a5bSNathan Whitehorn #define UNIN_AGP_BASE_ADDR 0x90
5250fd2a5bSNathan Whitehorn #define UNIN_AGP_GART_CONTROL 0x94
5350fd2a5bSNathan Whitehorn
5450fd2a5bSNathan Whitehorn #define UNIN_AGP_GART_INVAL 0x00000001
5550fd2a5bSNathan Whitehorn #define UNIN_AGP_GART_ENABLE 0x00000100
5650fd2a5bSNathan Whitehorn #define UNIN_AGP_GART_2XRESET 0x00010000
5750fd2a5bSNathan Whitehorn #define UNIN_AGP_U3_GART_PERFRD 0x00080000
5850fd2a5bSNathan Whitehorn
5950fd2a5bSNathan Whitehorn struct agp_apple_softc {
6050fd2a5bSNathan Whitehorn struct agp_softc agp;
6150fd2a5bSNathan Whitehorn uint32_t aperture;
6250fd2a5bSNathan Whitehorn struct agp_gatt *gatt;
6350fd2a5bSNathan Whitehorn int u3;
6450fd2a5bSNathan Whitehorn int needs_2x_reset;
6550fd2a5bSNathan Whitehorn };
6650fd2a5bSNathan Whitehorn
6750fd2a5bSNathan Whitehorn static int
agp_apple_probe(device_t dev)6850fd2a5bSNathan Whitehorn agp_apple_probe(device_t dev)
6950fd2a5bSNathan Whitehorn {
7050fd2a5bSNathan Whitehorn
71a8de37b0SEitan Adler if (resource_disabled("agp", device_get_unit(dev)))
72a8de37b0SEitan Adler return (ENXIO);
73a8de37b0SEitan Adler
7450fd2a5bSNathan Whitehorn if (pci_get_class(dev) != PCIC_BRIDGE
7550fd2a5bSNathan Whitehorn || pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
7650fd2a5bSNathan Whitehorn return (ENXIO);
7750fd2a5bSNathan Whitehorn
7850fd2a5bSNathan Whitehorn if (agp_find_caps(dev) == 0)
7950fd2a5bSNathan Whitehorn return (ENXIO);
8050fd2a5bSNathan Whitehorn
8150fd2a5bSNathan Whitehorn if (pci_get_class(dev) != PCIC_BRIDGE
8250fd2a5bSNathan Whitehorn || pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
8350fd2a5bSNathan Whitehorn return (ENXIO);
8450fd2a5bSNathan Whitehorn
8550fd2a5bSNathan Whitehorn switch (pci_get_devid(dev)) {
8650fd2a5bSNathan Whitehorn case 0x0020106b:
8750fd2a5bSNathan Whitehorn case 0x0027106b:
8850fd2a5bSNathan Whitehorn device_set_desc(dev, "Apple UniNorth AGP Bridge");
8950fd2a5bSNathan Whitehorn return (BUS_PROBE_DEFAULT);
9050fd2a5bSNathan Whitehorn case 0x002d106b:
9150fd2a5bSNathan Whitehorn device_set_desc(dev, "Apple UniNorth 1.5 AGP Bridge");
9250fd2a5bSNathan Whitehorn return (BUS_PROBE_DEFAULT);
9350fd2a5bSNathan Whitehorn case 0x0034106b:
9450fd2a5bSNathan Whitehorn device_set_desc(dev, "Apple UniNorth 2 AGP Bridge");
9550fd2a5bSNathan Whitehorn return (BUS_PROBE_DEFAULT);
9650fd2a5bSNathan Whitehorn case 0x004b106b:
9750fd2a5bSNathan Whitehorn case 0x0058106b:
9850fd2a5bSNathan Whitehorn case 0x0059106b:
9950fd2a5bSNathan Whitehorn device_set_desc(dev, "Apple U3 AGP Bridge");
10050fd2a5bSNathan Whitehorn return (BUS_PROBE_DEFAULT);
10150fd2a5bSNathan Whitehorn case 0x0066106b:
10250fd2a5bSNathan Whitehorn device_set_desc(dev, "Apple Intrepid AGP Bridge");
10350fd2a5bSNathan Whitehorn return (BUS_PROBE_DEFAULT);
10450fd2a5bSNathan Whitehorn }
10550fd2a5bSNathan Whitehorn
10650fd2a5bSNathan Whitehorn return (ENXIO);
10750fd2a5bSNathan Whitehorn }
10850fd2a5bSNathan Whitehorn
10950fd2a5bSNathan Whitehorn static int
agp_apple_attach(device_t dev)11050fd2a5bSNathan Whitehorn agp_apple_attach(device_t dev)
11150fd2a5bSNathan Whitehorn {
11250fd2a5bSNathan Whitehorn struct agp_apple_softc *sc = device_get_softc(dev);
11350fd2a5bSNathan Whitehorn int error;
11450fd2a5bSNathan Whitehorn
11550fd2a5bSNathan Whitehorn /* Record quirks */
11650fd2a5bSNathan Whitehorn sc->needs_2x_reset = 0;
11750fd2a5bSNathan Whitehorn sc->u3 = 0;
11850fd2a5bSNathan Whitehorn switch (pci_get_devid(dev)) {
11950fd2a5bSNathan Whitehorn case 0x0020106b:
12050fd2a5bSNathan Whitehorn case 0x0027106b:
12150fd2a5bSNathan Whitehorn sc->needs_2x_reset = 1;
12250fd2a5bSNathan Whitehorn break;
12350fd2a5bSNathan Whitehorn case 0x004b106b:
12450fd2a5bSNathan Whitehorn case 0x0058106b:
12550fd2a5bSNathan Whitehorn case 0x0059106b:
12650fd2a5bSNathan Whitehorn sc->u3 = 1;
12750fd2a5bSNathan Whitehorn break;
12850fd2a5bSNathan Whitehorn }
12950fd2a5bSNathan Whitehorn
13050fd2a5bSNathan Whitehorn /* Set the aperture bus address base (must be 0) */
13150fd2a5bSNathan Whitehorn pci_write_config(dev, UNIN_AGP_BASE_ADDR, 0, 4);
13250fd2a5bSNathan Whitehorn agp_set_aperture_resource(dev, -1);
13350fd2a5bSNathan Whitehorn
13450fd2a5bSNathan Whitehorn error = agp_generic_attach(dev);
13550fd2a5bSNathan Whitehorn if (error)
13650fd2a5bSNathan Whitehorn return (error);
13750fd2a5bSNathan Whitehorn
13850fd2a5bSNathan Whitehorn sc->aperture = 256*1024*1024;
13950fd2a5bSNathan Whitehorn
14050fd2a5bSNathan Whitehorn for (sc->aperture = 256*1024*1024; sc->aperture >= 4*1024*1024;
14150fd2a5bSNathan Whitehorn sc->aperture /= 2) {
14250fd2a5bSNathan Whitehorn sc->gatt = agp_alloc_gatt(dev);
14350fd2a5bSNathan Whitehorn if (sc->gatt)
14450fd2a5bSNathan Whitehorn break;
14550fd2a5bSNathan Whitehorn }
14650fd2a5bSNathan Whitehorn if (sc->aperture < 4*1024*1024) {
14750fd2a5bSNathan Whitehorn agp_generic_detach(dev);
14850fd2a5bSNathan Whitehorn return ENOMEM;
14950fd2a5bSNathan Whitehorn }
15050fd2a5bSNathan Whitehorn
15150fd2a5bSNathan Whitehorn /* Install the gatt. */
15250fd2a5bSNathan Whitehorn AGP_SET_APERTURE(dev, sc->aperture);
15350fd2a5bSNathan Whitehorn
15450fd2a5bSNathan Whitehorn /* XXX: U3 scratch page? */
15550fd2a5bSNathan Whitehorn
15650fd2a5bSNathan Whitehorn /* Enable the aperture and TLB. */
15750fd2a5bSNathan Whitehorn AGP_FLUSH_TLB(dev);
15850fd2a5bSNathan Whitehorn
15950fd2a5bSNathan Whitehorn return (0);
16050fd2a5bSNathan Whitehorn }
16150fd2a5bSNathan Whitehorn
16250fd2a5bSNathan Whitehorn static int
agp_apple_detach(device_t dev)16350fd2a5bSNathan Whitehorn agp_apple_detach(device_t dev)
16450fd2a5bSNathan Whitehorn {
16550fd2a5bSNathan Whitehorn struct agp_apple_softc *sc = device_get_softc(dev);
16650fd2a5bSNathan Whitehorn
16750fd2a5bSNathan Whitehorn agp_free_cdev(dev);
16850fd2a5bSNathan Whitehorn
16950fd2a5bSNathan Whitehorn /* Disable the aperture and TLB */
17050fd2a5bSNathan Whitehorn pci_write_config(dev, UNIN_AGP_GART_CONTROL, UNIN_AGP_GART_INVAL, 4);
17150fd2a5bSNathan Whitehorn pci_write_config(dev, UNIN_AGP_GART_CONTROL, 0, 4);
17250fd2a5bSNathan Whitehorn
17350fd2a5bSNathan Whitehorn if (sc->needs_2x_reset) {
17450fd2a5bSNathan Whitehorn pci_write_config(dev, UNIN_AGP_GART_CONTROL,
17550fd2a5bSNathan Whitehorn UNIN_AGP_GART_2XRESET, 4);
17650fd2a5bSNathan Whitehorn pci_write_config(dev, UNIN_AGP_GART_CONTROL, 0, 4);
17750fd2a5bSNathan Whitehorn }
17850fd2a5bSNathan Whitehorn
17950fd2a5bSNathan Whitehorn AGP_SET_APERTURE(dev, 0);
18050fd2a5bSNathan Whitehorn
18150fd2a5bSNathan Whitehorn agp_free_gatt(sc->gatt);
18250fd2a5bSNathan Whitehorn agp_free_res(dev);
18350fd2a5bSNathan Whitehorn return 0;
18450fd2a5bSNathan Whitehorn }
18550fd2a5bSNathan Whitehorn
18650fd2a5bSNathan Whitehorn static uint32_t
agp_apple_get_aperture(device_t dev)18750fd2a5bSNathan Whitehorn agp_apple_get_aperture(device_t dev)
18850fd2a5bSNathan Whitehorn {
18950fd2a5bSNathan Whitehorn struct agp_apple_softc *sc = device_get_softc(dev);
19050fd2a5bSNathan Whitehorn
19150fd2a5bSNathan Whitehorn return (sc->aperture);
19250fd2a5bSNathan Whitehorn }
19350fd2a5bSNathan Whitehorn
19450fd2a5bSNathan Whitehorn static int
agp_apple_set_aperture(device_t dev,uint32_t aperture)19550fd2a5bSNathan Whitehorn agp_apple_set_aperture(device_t dev, uint32_t aperture)
19650fd2a5bSNathan Whitehorn {
19750fd2a5bSNathan Whitehorn struct agp_apple_softc *sc = device_get_softc(dev);
19850fd2a5bSNathan Whitehorn
19950fd2a5bSNathan Whitehorn /*
20050fd2a5bSNathan Whitehorn * Check for a multiple of 4 MB and make sure it is within the
20150fd2a5bSNathan Whitehorn * programmable range.
20250fd2a5bSNathan Whitehorn */
20350fd2a5bSNathan Whitehorn if (aperture % (4*1024*1024)
20450fd2a5bSNathan Whitehorn || aperture < 4*1024*1024
20550fd2a5bSNathan Whitehorn || aperture > ((sc->u3) ? 512 : 256)*1024*1024)
20650fd2a5bSNathan Whitehorn return EINVAL;
20750fd2a5bSNathan Whitehorn
20850fd2a5bSNathan Whitehorn /* The aperture value is a multiple of 4 MB */
20950fd2a5bSNathan Whitehorn aperture /= (4*1024*1024);
21050fd2a5bSNathan Whitehorn
21150fd2a5bSNathan Whitehorn pci_write_config(dev, UNIN_AGP_GART_BASE,
21250fd2a5bSNathan Whitehorn (sc->gatt->ag_physical & 0xfffff000) | aperture, 4);
21350fd2a5bSNathan Whitehorn
21450fd2a5bSNathan Whitehorn return (0);
21550fd2a5bSNathan Whitehorn }
21650fd2a5bSNathan Whitehorn
21750fd2a5bSNathan Whitehorn static int
agp_apple_bind_page(device_t dev,vm_offset_t offset,vm_offset_t physical)21850fd2a5bSNathan Whitehorn agp_apple_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical)
21950fd2a5bSNathan Whitehorn {
22050fd2a5bSNathan Whitehorn struct agp_apple_softc *sc = device_get_softc(dev);
22150fd2a5bSNathan Whitehorn
22250fd2a5bSNathan Whitehorn if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
22350fd2a5bSNathan Whitehorn return EINVAL;
22450fd2a5bSNathan Whitehorn
22550fd2a5bSNathan Whitehorn sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical;
22650fd2a5bSNathan Whitehorn return (0);
22750fd2a5bSNathan Whitehorn }
22850fd2a5bSNathan Whitehorn
22950fd2a5bSNathan Whitehorn static int
agp_apple_unbind_page(device_t dev,vm_offset_t offset)23050fd2a5bSNathan Whitehorn agp_apple_unbind_page(device_t dev, vm_offset_t offset)
23150fd2a5bSNathan Whitehorn {
23250fd2a5bSNathan Whitehorn struct agp_apple_softc *sc = device_get_softc(dev);
23350fd2a5bSNathan Whitehorn
23450fd2a5bSNathan Whitehorn if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
23550fd2a5bSNathan Whitehorn return EINVAL;
23650fd2a5bSNathan Whitehorn
23750fd2a5bSNathan Whitehorn sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
23850fd2a5bSNathan Whitehorn return (0);
23950fd2a5bSNathan Whitehorn }
24050fd2a5bSNathan Whitehorn
24150fd2a5bSNathan Whitehorn static void
agp_apple_flush_tlb(device_t dev)24250fd2a5bSNathan Whitehorn agp_apple_flush_tlb(device_t dev)
24350fd2a5bSNathan Whitehorn {
24450fd2a5bSNathan Whitehorn struct agp_apple_softc *sc = device_get_softc(dev);
24550fd2a5bSNathan Whitehorn uint32_t cntrl = UNIN_AGP_GART_ENABLE;
24650fd2a5bSNathan Whitehorn
24750fd2a5bSNathan Whitehorn if (sc->u3)
24850fd2a5bSNathan Whitehorn cntrl |= UNIN_AGP_U3_GART_PERFRD;
24950fd2a5bSNathan Whitehorn
25050fd2a5bSNathan Whitehorn pci_write_config(dev, UNIN_AGP_GART_CONTROL,
25150fd2a5bSNathan Whitehorn cntrl | UNIN_AGP_GART_INVAL, 4);
25250fd2a5bSNathan Whitehorn pci_write_config(dev, UNIN_AGP_GART_CONTROL, cntrl, 4);
25350fd2a5bSNathan Whitehorn
25450fd2a5bSNathan Whitehorn if (sc->needs_2x_reset) {
25550fd2a5bSNathan Whitehorn pci_write_config(dev, UNIN_AGP_GART_CONTROL,
25650fd2a5bSNathan Whitehorn cntrl | UNIN_AGP_GART_2XRESET, 4);
25750fd2a5bSNathan Whitehorn pci_write_config(dev, UNIN_AGP_GART_CONTROL, cntrl, 4);
25850fd2a5bSNathan Whitehorn }
25950fd2a5bSNathan Whitehorn }
26050fd2a5bSNathan Whitehorn
26150fd2a5bSNathan Whitehorn static device_method_t agp_apple_methods[] = {
26250fd2a5bSNathan Whitehorn /* Device interface */
26350fd2a5bSNathan Whitehorn DEVMETHOD(device_probe, agp_apple_probe),
26450fd2a5bSNathan Whitehorn DEVMETHOD(device_attach, agp_apple_attach),
26550fd2a5bSNathan Whitehorn DEVMETHOD(device_detach, agp_apple_detach),
26650fd2a5bSNathan Whitehorn DEVMETHOD(device_shutdown, bus_generic_shutdown),
26750fd2a5bSNathan Whitehorn DEVMETHOD(device_suspend, bus_generic_suspend),
26850fd2a5bSNathan Whitehorn DEVMETHOD(device_resume, bus_generic_resume),
26950fd2a5bSNathan Whitehorn
27050fd2a5bSNathan Whitehorn /* AGP interface */
27150fd2a5bSNathan Whitehorn DEVMETHOD(agp_get_aperture, agp_apple_get_aperture),
27250fd2a5bSNathan Whitehorn DEVMETHOD(agp_set_aperture, agp_apple_set_aperture),
27350fd2a5bSNathan Whitehorn DEVMETHOD(agp_bind_page, agp_apple_bind_page),
27450fd2a5bSNathan Whitehorn DEVMETHOD(agp_unbind_page, agp_apple_unbind_page),
27550fd2a5bSNathan Whitehorn DEVMETHOD(agp_flush_tlb, agp_apple_flush_tlb),
27650fd2a5bSNathan Whitehorn DEVMETHOD(agp_enable, agp_generic_enable),
27750fd2a5bSNathan Whitehorn DEVMETHOD(agp_alloc_memory, agp_generic_alloc_memory),
27850fd2a5bSNathan Whitehorn DEVMETHOD(agp_free_memory, agp_generic_free_memory),
27950fd2a5bSNathan Whitehorn DEVMETHOD(agp_bind_memory, agp_generic_bind_memory),
28050fd2a5bSNathan Whitehorn DEVMETHOD(agp_unbind_memory, agp_generic_unbind_memory),
28150fd2a5bSNathan Whitehorn { 0, 0 }
28250fd2a5bSNathan Whitehorn };
28350fd2a5bSNathan Whitehorn
28450fd2a5bSNathan Whitehorn static driver_t agp_apple_driver = {
28550fd2a5bSNathan Whitehorn "agp",
28650fd2a5bSNathan Whitehorn agp_apple_methods,
28750fd2a5bSNathan Whitehorn sizeof(struct agp_apple_softc),
28850fd2a5bSNathan Whitehorn };
28950fd2a5bSNathan Whitehorn
29074f84981SJohn Baldwin DRIVER_MODULE(agp_apple, hostb, agp_apple_driver, 0, 0);
29150fd2a5bSNathan Whitehorn MODULE_DEPEND(agp_apple, agp, 1, 1, 1);
29250fd2a5bSNathan Whitehorn MODULE_DEPEND(agp_apple, pci, 1, 1, 1);
293