xref: /freebsd/sys/dev/acpica/acpi_pcib_acpi.c (revision e3aa81b84d547e81938b7953edfbdbc30e976b4f)
115e32d5dSMike Smith /*-
215e32d5dSMike Smith  * Copyright (c) 2000 Michael Smith
315e32d5dSMike Smith  * Copyright (c) 2000 BSDi
415e32d5dSMike Smith  * All rights reserved.
515e32d5dSMike Smith  *
615e32d5dSMike Smith  * Redistribution and use in source and binary forms, with or without
715e32d5dSMike Smith  * modification, are permitted provided that the following conditions
815e32d5dSMike Smith  * are met:
915e32d5dSMike Smith  * 1. Redistributions of source code must retain the above copyright
1015e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer.
1115e32d5dSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
1215e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer in the
1315e32d5dSMike Smith  *    documentation and/or other materials provided with the distribution.
1415e32d5dSMike Smith  *
1515e32d5dSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1615e32d5dSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1715e32d5dSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1815e32d5dSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1915e32d5dSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2015e32d5dSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2115e32d5dSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2215e32d5dSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2315e32d5dSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2415e32d5dSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2515e32d5dSMike Smith  * SUCH DAMAGE.
2615e32d5dSMike Smith  *
2715e32d5dSMike Smith  *	$FreeBSD$
2815e32d5dSMike Smith  */
2915e32d5dSMike Smith #include "opt_acpi.h"
3015e32d5dSMike Smith #include <sys/param.h>
3115e32d5dSMike Smith #include <sys/bus.h>
324fa387b6SMike Smith #include <sys/kernel.h>
33e3aa81b8SNate Lawson #include <sys/malloc.h>
3415e32d5dSMike Smith 
3515e32d5dSMike Smith #include "acpi.h"
3615e32d5dSMike Smith #include <dev/acpica/acpivar.h>
3715e32d5dSMike Smith 
3815e32d5dSMike Smith #include <machine/pci_cfgreg.h>
39cace7a2aSWarner Losh #include <dev/pci/pcivar.h>
40cace7a2aSWarner Losh #include <dev/pci/pcib_private.h>
4115e32d5dSMike Smith #include "pcib_if.h"
4215e32d5dSMike Smith 
432ccfc932SJohn Baldwin #include <dev/acpica/acpi_pcibvar.h>
442ccfc932SJohn Baldwin 
45e3aa81b8SNate Lawson /* Hooks for the ACPI CA debugging infrastructure. */
46e71b6381SMike Smith #define _COMPONENT	ACPI_BUS
472ccfc932SJohn Baldwin ACPI_MODULE_NAME("PCI_ACPI")
480ae55423SMike Smith 
492ccfc932SJohn Baldwin struct acpi_hpcib_softc {
5015e32d5dSMike Smith     device_t		ap_dev;
5115e32d5dSMike Smith     ACPI_HANDLE		ap_handle;
5215e32d5dSMike Smith 
5315e32d5dSMike Smith     int			ap_segment;	/* analagous to Alpha 'hose' */
5415e32d5dSMike Smith     int			ap_bus;		/* bios-assigned bus number */
554fa387b6SMike Smith 
564fa387b6SMike Smith     ACPI_BUFFER		ap_prt;		/* interrupt routing table */
5715e32d5dSMike Smith };
5815e32d5dSMike Smith 
592ccfc932SJohn Baldwin static int		acpi_pcib_acpi_probe(device_t bus);
602ccfc932SJohn Baldwin static int		acpi_pcib_acpi_attach(device_t bus);
61ba835e3fSMitsuru IWASAKI static int		acpi_pcib_acpi_resume(device_t bus);
62e3aa81b8SNate Lawson static int		acpi_pcib_read_ivar(device_t dev, device_t child,
63e3aa81b8SNate Lawson 			    int which, uintptr_t *result);
64e3aa81b8SNate Lawson static int		acpi_pcib_write_ivar(device_t dev, device_t child,
65e3aa81b8SNate Lawson 			    int which, uintptr_t value);
66e3aa81b8SNate Lawson static uint32_t		acpi_pcib_read_config(device_t dev, int bus, int slot,
67e3aa81b8SNate Lawson 			    int func, int reg, int bytes);
68e3aa81b8SNate Lawson static void		acpi_pcib_write_config(device_t dev, int bus, int slot,
69e3aa81b8SNate Lawson 			    int func, int reg, uint32_t data, int bytes);
702ccfc932SJohn Baldwin static int		acpi_pcib_acpi_route_interrupt(device_t pcib,
712ccfc932SJohn Baldwin 			    device_t dev, int pin);
72cd8b53edSWarner Losh static struct resource *acpi_pcib_acpi_alloc_resource(device_t dev,
73cd8b53edSWarner Losh   			    device_t child, int type, int *rid,
74cd8b53edSWarner Losh 			    u_long start, u_long end, u_long count,
75cd8b53edSWarner Losh 			    u_int flags);
7615e32d5dSMike Smith 
772ccfc932SJohn Baldwin static device_method_t acpi_pcib_acpi_methods[] = {
7815e32d5dSMike Smith     /* Device interface */
792ccfc932SJohn Baldwin     DEVMETHOD(device_probe,		acpi_pcib_acpi_probe),
802ccfc932SJohn Baldwin     DEVMETHOD(device_attach,		acpi_pcib_acpi_attach),
8115e32d5dSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
8215e32d5dSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
83ba835e3fSMitsuru IWASAKI     DEVMETHOD(device_resume,		acpi_pcib_acpi_resume),
8415e32d5dSMike Smith 
8515e32d5dSMike Smith     /* Bus interface */
8615e32d5dSMike Smith     DEVMETHOD(bus_print_child,		bus_generic_print_child),
8715e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_pcib_read_ivar),
8815e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_pcib_write_ivar),
89cd8b53edSWarner Losh     DEVMETHOD(bus_alloc_resource,	acpi_pcib_acpi_alloc_resource),
9015e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
9115e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
9215e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource, 	bus_generic_deactivate_resource),
9315e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
9415e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
9515e32d5dSMike Smith 
9615e32d5dSMike Smith     /* pcib interface */
972ccfc932SJohn Baldwin     DEVMETHOD(pcib_maxslots,		pcib_maxslots),
9815e32d5dSMike Smith     DEVMETHOD(pcib_read_config,		acpi_pcib_read_config),
9915e32d5dSMike Smith     DEVMETHOD(pcib_write_config,	acpi_pcib_write_config),
1002ccfc932SJohn Baldwin     DEVMETHOD(pcib_route_interrupt,	acpi_pcib_acpi_route_interrupt),
10115e32d5dSMike Smith 
10215e32d5dSMike Smith     {0, 0}
10315e32d5dSMike Smith };
10415e32d5dSMike Smith 
1052ccfc932SJohn Baldwin static driver_t acpi_pcib_acpi_driver = {
1062ccfc932SJohn Baldwin     "pcib",
1072ccfc932SJohn Baldwin     acpi_pcib_acpi_methods,
1082ccfc932SJohn Baldwin     sizeof(struct acpi_hpcib_softc),
10915e32d5dSMike Smith };
11015e32d5dSMike Smith 
1112ccfc932SJohn Baldwin DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_acpi_driver, pcib_devclass, 0, 0);
11264278df5SNate Lawson MODULE_DEPEND(acpi_pcib, acpi, 1, 1, 1);
11315e32d5dSMike Smith 
11415e32d5dSMike Smith static int
1152ccfc932SJohn Baldwin acpi_pcib_acpi_probe(device_t dev)
11615e32d5dSMike Smith {
11715e32d5dSMike Smith 
118e3aa81b8SNate Lawson     if (acpi_get_type(dev) == ACPI_TYPE_DEVICE && !acpi_disabled("pci") &&
11915e32d5dSMike Smith 	acpi_MatchHid(dev, "PNP0A03")) {
12015e32d5dSMike Smith 
121e3aa81b8SNate Lawson 	if (pci_cfgregopen() == 0)
1225e0ca577SMitsuru IWASAKI 		return (ENXIO);
1232ccfc932SJohn Baldwin 	device_set_desc(dev, "ACPI Host-PCI bridge");
12415e32d5dSMike Smith 	return (0);
12515e32d5dSMike Smith     }
12615e32d5dSMike Smith     return (ENXIO);
12715e32d5dSMike Smith }
12815e32d5dSMike Smith 
12915e32d5dSMike Smith static int
1302ccfc932SJohn Baldwin acpi_pcib_acpi_attach(device_t dev)
13115e32d5dSMike Smith {
1322ccfc932SJohn Baldwin     struct acpi_hpcib_softc	*sc;
13315e32d5dSMike Smith     ACPI_STATUS			status;
1348b149b51SJohn Baldwin     u_int addr, slot, func, busok;
1359debb532SJohn Baldwin     uint8_t busno;
1360ae55423SMike Smith 
137b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
13815e32d5dSMike Smith 
13915e32d5dSMike Smith     sc = device_get_softc(dev);
14015e32d5dSMike Smith     sc->ap_dev = dev;
14115e32d5dSMike Smith     sc->ap_handle = acpi_get_handle(dev);
14215e32d5dSMike Smith 
14315e32d5dSMike Smith     /*
1449debb532SJohn Baldwin      * Get our base bus number by evaluating _BBN.
145c1b1f787SMike Smith      * If this doesn't work, we assume we're bus number 0.
14615e32d5dSMike Smith      *
14715e32d5dSMike Smith      * XXX note that it may also not exist in the case where we are
14815e32d5dSMike Smith      *     meant to use a private configuration space mechanism for this bus,
14915e32d5dSMike Smith      *     so we should dig out our resources and check to see if we have
15015e32d5dSMike Smith      *     anything like that.  How do we do this?
151042283a6SMike Smith      * XXX If we have the requisite information, and if we don't think the
152042283a6SMike Smith      *     default PCI configuration space handlers can deal with this bus,
153042283a6SMike Smith      *     we should attach our own handler.
154042283a6SMike Smith      * XXX invoke _REG on this for the PCI config space address space?
1559debb532SJohn Baldwin      * XXX It seems many BIOS's with multiple Host-PCI bridges do not set
1569debb532SJohn Baldwin      *     _BBN correctly.  They set _BBN to zero for all bridges.  Thus,
1579debb532SJohn Baldwin      *     if _BBN is zero and pcib0 already exists, we try to read our
1589debb532SJohn Baldwin      *     bus number from the configuration registers at address _ADR.
15915e32d5dSMike Smith      */
160c310653eSNate Lawson     status = acpi_GetInteger(sc->ap_handle, "_BBN", &sc->ap_bus);
1619debb532SJohn Baldwin     if (ACPI_FAILURE(status)) {
16215e32d5dSMike Smith 	if (status != AE_NOT_FOUND) {
1639debb532SJohn Baldwin 	    device_printf(dev, "could not evaluate _BBN - %s\n",
1649debb532SJohn Baldwin 		AcpiFormatException(status));
165c1b1f787SMike Smith 	    return_VALUE (ENXIO);
166c1b1f787SMike Smith 	} else {
167e3aa81b8SNate Lawson 	    /* If it's not found, assume 0. */
16815e32d5dSMike Smith 	    sc->ap_bus = 0;
16915e32d5dSMike Smith 	}
1708745ec57SJohn Baldwin     }
1719debb532SJohn Baldwin 
1729debb532SJohn Baldwin     /*
1739debb532SJohn Baldwin      * If the bus is zero and pcib0 already exists, read the bus number
1749debb532SJohn Baldwin      * via PCI config space.
1759debb532SJohn Baldwin      */
1769debb532SJohn Baldwin     busok = 1;
1779debb532SJohn Baldwin     if (sc->ap_bus == 0 && devclass_get_device(pcib_devclass, 0) != dev) {
1788745ec57SJohn Baldwin 	busok = 0;
179c310653eSNate Lawson 	status = acpi_GetInteger(sc->ap_handle, "_ADR", &addr);
1809debb532SJohn Baldwin 	if (ACPI_FAILURE(status)) {
1819debb532SJohn Baldwin 	    if (status != AE_NOT_FOUND) {
1829debb532SJohn Baldwin 		device_printf(dev, "could not evaluate _ADR - %s\n",
1839debb532SJohn Baldwin 		    AcpiFormatException(status));
1849debb532SJohn Baldwin 		return_VALUE (ENXIO);
1858745ec57SJohn Baldwin 	    } else
186e3aa81b8SNate Lawson 		device_printf(dev, "couldn't find _ADR\n");
1879debb532SJohn Baldwin 	} else {
1889debb532SJohn Baldwin 	    /* XXX: We assume bus 0. */
1899debb532SJohn Baldwin 	    slot = addr >> 16;
1909debb532SJohn Baldwin 	    func = addr & 0xffff;
1919debb532SJohn Baldwin 	    if (bootverbose)
1929debb532SJohn Baldwin 		device_printf(dev, "reading config registers from 0:%d:%d\n",
1939debb532SJohn Baldwin 		    slot, func);
1948745ec57SJohn Baldwin 	    if (host_pcib_get_busno(pci_cfgregread, 0, slot, func, &busno) == 0)
195e3aa81b8SNate Lawson 		device_printf(dev, "couldn't read bus number from cfg space\n");
1968745ec57SJohn Baldwin 	    else {
1979debb532SJohn Baldwin 		sc->ap_bus = busno;
1988745ec57SJohn Baldwin 		busok = 1;
1999debb532SJohn Baldwin 	    }
2009debb532SJohn Baldwin 	}
2019debb532SJohn Baldwin     }
2029debb532SJohn Baldwin 
2039debb532SJohn Baldwin     /*
2049debb532SJohn Baldwin      * If nothing else worked, hope that ACPI at least lays out the
2059debb532SJohn Baldwin      * host-PCI bridges in order and that as a result our unit number
2069debb532SJohn Baldwin      * is actually our bus number.  There are several reasons this
2079debb532SJohn Baldwin      * might not be true.
2089debb532SJohn Baldwin      */
2099debb532SJohn Baldwin     if (busok == 0) {
2109debb532SJohn Baldwin 	sc->ap_bus = device_get_unit(dev);
2119debb532SJohn Baldwin 	device_printf(dev, "trying bus number %d\n", sc->ap_bus);
212c1b1f787SMike Smith     }
21315e32d5dSMike Smith 
21415e32d5dSMike Smith     /*
2152ccfc932SJohn Baldwin      * Get our segment number by evaluating _SEG
2162ccfc932SJohn Baldwin      * It's OK for this to not exist.
217042283a6SMike Smith      */
218e3aa81b8SNate Lawson     status = acpi_GetInteger(sc->ap_handle, "_SEG", &sc->ap_segment);
219e3aa81b8SNate Lawson     if (ACPI_FAILURE(status)) {
2202ccfc932SJohn Baldwin 	if (status != AE_NOT_FOUND) {
221e3aa81b8SNate Lawson 	    device_printf(dev, "could not evaluate _SEG - %s\n",
222e3aa81b8SNate Lawson 		AcpiFormatException(status));
2230ae55423SMike Smith 	    return_VALUE (ENXIO);
22415e32d5dSMike Smith 	}
225e3aa81b8SNate Lawson 	/* If it's not found, assume 0. */
2262ccfc932SJohn Baldwin 	sc->ap_segment = 0;
22715e32d5dSMike Smith     }
22815e32d5dSMike Smith 
2292ccfc932SJohn Baldwin     return (acpi_pcib_attach(dev, &sc->ap_prt, sc->ap_bus));
23015e32d5dSMike Smith }
23115e32d5dSMike Smith 
232ba835e3fSMitsuru IWASAKI static int
233ba835e3fSMitsuru IWASAKI acpi_pcib_acpi_resume(device_t dev)
234ba835e3fSMitsuru IWASAKI {
235ba835e3fSMitsuru IWASAKI     struct acpi_hpcib_softc	*sc = device_get_softc(dev);
236ba835e3fSMitsuru IWASAKI 
237ba835e3fSMitsuru IWASAKI     return (acpi_pcib_resume(dev, &sc->ap_prt, sc->ap_bus));
238ba835e3fSMitsuru IWASAKI }
239ba835e3fSMitsuru IWASAKI 
2404fa387b6SMike Smith /*
2414fa387b6SMike Smith  * Support for standard PCI bridge ivars.
2424fa387b6SMike Smith  */
24315e32d5dSMike Smith static int
24415e32d5dSMike Smith acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
24515e32d5dSMike Smith {
2462ccfc932SJohn Baldwin     struct acpi_hpcib_softc	*sc = device_get_softc(dev);
24715e32d5dSMike Smith 
24815e32d5dSMike Smith     switch (which) {
24915e32d5dSMike Smith     case PCIB_IVAR_BUS:
25015e32d5dSMike Smith 	*result = sc->ap_bus;
25115e32d5dSMike Smith 	return (0);
2522ccfc932SJohn Baldwin     case ACPI_IVAR_HANDLE:
2532ccfc932SJohn Baldwin 	*result = (uintptr_t)sc->ap_handle;
2542ccfc932SJohn Baldwin 	return (0);
25515e32d5dSMike Smith     }
25615e32d5dSMike Smith     return (ENOENT);
25715e32d5dSMike Smith }
25815e32d5dSMike Smith 
25915e32d5dSMike Smith static int
26015e32d5dSMike Smith acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
26115e32d5dSMike Smith {
2622ccfc932SJohn Baldwin     struct acpi_hpcib_softc 	*sc = device_get_softc(dev);
26315e32d5dSMike Smith 
26415e32d5dSMike Smith     switch (which) {
26515e32d5dSMike Smith     case PCIB_IVAR_BUS:
26615e32d5dSMike Smith 	sc->ap_bus = value;
26715e32d5dSMike Smith 	return (0);
26815e32d5dSMike Smith     }
26915e32d5dSMike Smith     return (ENOENT);
27015e32d5dSMike Smith }
27115e32d5dSMike Smith 
272e3aa81b8SNate Lawson static uint32_t
273e3aa81b8SNate Lawson acpi_pcib_read_config(device_t dev, int bus, int slot, int func, int reg,
274e3aa81b8SNate Lawson     int bytes)
27515e32d5dSMike Smith {
27615e32d5dSMike Smith     return (pci_cfgregread(bus, slot, func, reg, bytes));
27715e32d5dSMike Smith }
27815e32d5dSMike Smith 
27915e32d5dSMike Smith static void
280e3aa81b8SNate Lawson acpi_pcib_write_config(device_t dev, int bus, int slot, int func, int reg,
281e3aa81b8SNate Lawson     uint32_t data, int bytes)
28215e32d5dSMike Smith {
28315e32d5dSMike Smith     pci_cfgregwrite(bus, slot, func, reg, data, bytes);
28415e32d5dSMike Smith }
28515e32d5dSMike Smith 
28615e32d5dSMike Smith static int
2872ccfc932SJohn Baldwin acpi_pcib_acpi_route_interrupt(device_t pcib, device_t dev, int pin)
28815e32d5dSMike Smith {
2892ccfc932SJohn Baldwin     struct acpi_hpcib_softc *sc;
2904fa387b6SMike Smith 
291e3aa81b8SNate Lawson     /* Find the bridge softc. */
2922ccfc932SJohn Baldwin     sc = device_get_softc(pcib);
2932ccfc932SJohn Baldwin     return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt));
29415e32d5dSMike Smith }
295cd8b53edSWarner Losh 
296cd8b53edSWarner Losh struct resource *
297cd8b53edSWarner Losh acpi_pcib_acpi_alloc_resource(device_t dev, device_t child, int type, int *rid,
298cd8b53edSWarner Losh     u_long start, u_long end, u_long count, u_int flags)
299cd8b53edSWarner Losh {
300cd8b53edSWarner Losh     /*
301cd8b53edSWarner Losh      * If no memory preference is given, use upper 256MB slot most
302cd8b53edSWarner Losh      * bioses use for their memory window.  Typically other bridges
303cd8b53edSWarner Losh      * before us get in the way to assert their preferences on memory.
304cd8b53edSWarner Losh      * Hardcoding like this sucks, so a more MD/MI way needs to be
305cd8b53edSWarner Losh      * found to do it.
306cd8b53edSWarner Losh      */
307cd8b53edSWarner Losh     if (type == SYS_RES_MEMORY && start == 0UL && end == ~0UL)
308cd8b53edSWarner Losh 	start = 0xf0000000;
309cd8b53edSWarner Losh     return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
310cd8b53edSWarner Losh 	count, flags));
311cd8b53edSWarner Losh }
312