xref: /freebsd-src/sys/dev/acpica/acpi_pcib_pci.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
12ccfc932SJohn Baldwin /*-
22ccfc932SJohn Baldwin  * Copyright (c) 2000 Michael Smith
32ccfc932SJohn Baldwin  * Copyright (c) 2000 BSDi
42ccfc932SJohn Baldwin  * All rights reserved.
52ccfc932SJohn Baldwin  *
62ccfc932SJohn Baldwin  * Redistribution and use in source and binary forms, with or without
72ccfc932SJohn Baldwin  * modification, are permitted provided that the following conditions
82ccfc932SJohn Baldwin  * are met:
92ccfc932SJohn Baldwin  * 1. Redistributions of source code must retain the above copyright
102ccfc932SJohn Baldwin  *    notice, this list of conditions and the following disclaimer.
112ccfc932SJohn Baldwin  * 2. Redistributions in binary form must reproduce the above copyright
122ccfc932SJohn Baldwin  *    notice, this list of conditions and the following disclaimer in the
132ccfc932SJohn Baldwin  *    documentation and/or other materials provided with the distribution.
142ccfc932SJohn Baldwin  *
152ccfc932SJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
162ccfc932SJohn Baldwin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
172ccfc932SJohn Baldwin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
182ccfc932SJohn Baldwin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
192ccfc932SJohn Baldwin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
202ccfc932SJohn Baldwin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
212ccfc932SJohn Baldwin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
222ccfc932SJohn Baldwin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
232ccfc932SJohn Baldwin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
242ccfc932SJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
252ccfc932SJohn Baldwin  * SUCH DAMAGE.
262ccfc932SJohn Baldwin  */
27aad970f1SDavid E. O'Brien 
28aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
292ccfc932SJohn Baldwin #include "opt_acpi.h"
302ccfc932SJohn Baldwin 
312ccfc932SJohn Baldwin #include <sys/param.h>
322ccfc932SJohn Baldwin #include <sys/bus.h>
332ccfc932SJohn Baldwin #include <sys/kernel.h>
345acd0218SNate Lawson #include <sys/malloc.h>
35fe12f24bSPoul-Henning Kamp #include <sys/module.h>
3683c41143SJohn Baldwin #include <sys/rman.h>
372ccfc932SJohn Baldwin 
38129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h>
39129d3046SJung-uk Kim #include <contrib/dev/acpica/include/accommon.h>
40129d3046SJung-uk Kim 
412ccfc932SJohn Baldwin #include <dev/acpica/acpivar.h>
422ccfc932SJohn Baldwin #include <dev/acpica/acpi_pcibvar.h>
432ccfc932SJohn Baldwin 
442ccfc932SJohn Baldwin #include <machine/pci_cfgreg.h>
45cace7a2aSWarner Losh #include <dev/pci/pcivar.h>
46cace7a2aSWarner Losh #include <dev/pci/pcireg.h>
47cace7a2aSWarner Losh #include <dev/pci/pcib_private.h>
482ccfc932SJohn Baldwin #include "pcib_if.h"
492ccfc932SJohn Baldwin 
505acd0218SNate Lawson /* Hooks for the ACPI CA debugging infrastructure. */
512ccfc932SJohn Baldwin #define _COMPONENT	ACPI_BUS
522ccfc932SJohn Baldwin ACPI_MODULE_NAME("PCI_PCI")
532ccfc932SJohn Baldwin 
542ccfc932SJohn Baldwin struct acpi_pcib_softc {
552ccfc932SJohn Baldwin     struct pcib_softc	ap_pcibsc;
562ccfc932SJohn Baldwin     ACPI_HANDLE		ap_handle;
572ccfc932SJohn Baldwin     ACPI_BUFFER		ap_prt;		/* interrupt routing table */
582ccfc932SJohn Baldwin };
592ccfc932SJohn Baldwin 
602ccfc932SJohn Baldwin struct acpi_pcib_lookup_info {
612ccfc932SJohn Baldwin     UINT32		address;
622ccfc932SJohn Baldwin     ACPI_HANDLE		handle;
632ccfc932SJohn Baldwin };
642ccfc932SJohn Baldwin 
652ccfc932SJohn Baldwin static int		acpi_pcib_pci_probe(device_t bus);
662ccfc932SJohn Baldwin static int		acpi_pcib_pci_attach(device_t bus);
676f33eaa5SJohn Baldwin static int		acpi_pcib_pci_detach(device_t bus);
685acd0218SNate Lawson static int		acpi_pcib_read_ivar(device_t dev, device_t child,
695acd0218SNate Lawson 			    int which, uintptr_t *result);
702ccfc932SJohn Baldwin static int		acpi_pcib_pci_route_interrupt(device_t pcib,
712ccfc932SJohn Baldwin 			    device_t dev, int pin);
722ccfc932SJohn Baldwin 
732ccfc932SJohn Baldwin static device_method_t acpi_pcib_pci_methods[] = {
742ccfc932SJohn Baldwin     /* Device interface */
752ccfc932SJohn Baldwin     DEVMETHOD(device_probe,		acpi_pcib_pci_probe),
762ccfc932SJohn Baldwin     DEVMETHOD(device_attach,		acpi_pcib_pci_attach),
776f33eaa5SJohn Baldwin     DEVMETHOD(device_detach,		acpi_pcib_pci_detach),
782ccfc932SJohn Baldwin 
792ccfc932SJohn Baldwin     /* Bus interface */
802ccfc932SJohn Baldwin     DEVMETHOD(bus_read_ivar,		acpi_pcib_read_ivar),
818d791e5aSJohn Baldwin     DEVMETHOD(bus_get_cpus,		acpi_pcib_get_cpus),
822ccfc932SJohn Baldwin 
832ccfc932SJohn Baldwin     /* pcib interface */
842ccfc932SJohn Baldwin     DEVMETHOD(pcib_route_interrupt,	acpi_pcib_pci_route_interrupt),
8562508c53SJohn Baldwin     DEVMETHOD(pcib_power_for_sleep,	acpi_pcib_power_for_sleep),
862ccfc932SJohn Baldwin 
8761bfd867SSofian Brabez     DEVMETHOD_END
882ccfc932SJohn Baldwin };
892ccfc932SJohn Baldwin 
907d23a9b3SJohn Baldwin DEFINE_CLASS_1(pcib, acpi_pcib_pci_driver, acpi_pcib_pci_methods,
917d23a9b3SJohn Baldwin     sizeof(struct acpi_pcib_softc), pcib_driver);
92*916a5d8aSJohn Baldwin DRIVER_MODULE(acpi_pcib, pci, acpi_pcib_pci_driver, 0, 0);
9364278df5SNate Lawson MODULE_DEPEND(acpi_pcib, acpi, 1, 1, 1);
942ccfc932SJohn Baldwin 
952ccfc932SJohn Baldwin static int
acpi_pcib_pci_probe(device_t dev)962ccfc932SJohn Baldwin acpi_pcib_pci_probe(device_t dev)
972ccfc932SJohn Baldwin {
982ccfc932SJohn Baldwin 
995acd0218SNate Lawson     if (pci_get_class(dev) != PCIC_BRIDGE ||
1005acd0218SNate Lawson 	pci_get_subclass(dev) != PCIS_BRIDGE_PCI ||
1012ccfc932SJohn Baldwin 	acpi_disabled("pci"))
1022ccfc932SJohn Baldwin 	return (ENXIO);
1032ccfc932SJohn Baldwin     if (acpi_get_handle(dev) == NULL)
1042ccfc932SJohn Baldwin 	return (ENXIO);
1055acd0218SNate Lawson     if (pci_cfgregopen() == 0)
1065e0ca577SMitsuru IWASAKI 	return (ENXIO);
1075e0ca577SMitsuru IWASAKI 
1082ccfc932SJohn Baldwin     device_set_desc(dev, "ACPI PCI-PCI bridge");
1092ccfc932SJohn Baldwin     return (-1000);
1102ccfc932SJohn Baldwin }
1112ccfc932SJohn Baldwin 
1122ccfc932SJohn Baldwin static int
acpi_pcib_pci_attach(device_t dev)1132ccfc932SJohn Baldwin acpi_pcib_pci_attach(device_t dev)
1142ccfc932SJohn Baldwin {
1152ccfc932SJohn Baldwin     struct acpi_pcib_softc *sc;
1162ccfc932SJohn Baldwin 
1172ccfc932SJohn Baldwin     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1182ccfc932SJohn Baldwin 
1192ccfc932SJohn Baldwin     pcib_attach_common(dev);
1202ccfc932SJohn Baldwin     sc = device_get_softc(dev);
1212ccfc932SJohn Baldwin     sc->ap_handle = acpi_get_handle(dev);
12267e7d085SJohn Baldwin     acpi_pcib_fetch_prt(dev, &sc->ap_prt);
12367e7d085SJohn Baldwin 
12467e7d085SJohn Baldwin     return (pcib_attach_child(dev));
1252ccfc932SJohn Baldwin }
1262ccfc932SJohn Baldwin 
1272ccfc932SJohn Baldwin static int
acpi_pcib_pci_detach(device_t dev)1286f33eaa5SJohn Baldwin acpi_pcib_pci_detach(device_t dev)
1296f33eaa5SJohn Baldwin {
1306f33eaa5SJohn Baldwin     struct acpi_pcib_softc *sc;
1316f33eaa5SJohn Baldwin     int error;
1326f33eaa5SJohn Baldwin 
1336f33eaa5SJohn Baldwin     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1346f33eaa5SJohn Baldwin 
1356f33eaa5SJohn Baldwin     sc = device_get_softc(dev);
1366f33eaa5SJohn Baldwin     error = pcib_detach(dev);
1376f33eaa5SJohn Baldwin     if (error == 0)
1386f33eaa5SJohn Baldwin 	    AcpiOsFree(sc->ap_prt.Pointer);
1396f33eaa5SJohn Baldwin     return (error);
1406f33eaa5SJohn Baldwin }
1416f33eaa5SJohn Baldwin 
1426f33eaa5SJohn Baldwin static int
acpi_pcib_read_ivar(device_t dev,device_t child,int which,uintptr_t * result)1432ccfc932SJohn Baldwin acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1442ccfc932SJohn Baldwin {
1452ccfc932SJohn Baldwin     struct acpi_pcib_softc *sc = device_get_softc(dev);
1462ccfc932SJohn Baldwin 
1472ccfc932SJohn Baldwin     switch (which) {
1482ccfc932SJohn Baldwin     case ACPI_IVAR_HANDLE:
1492ccfc932SJohn Baldwin 	*result = (uintptr_t)sc->ap_handle;
1502ccfc932SJohn Baldwin 	return (0);
1512ccfc932SJohn Baldwin     }
1522ccfc932SJohn Baldwin     return (pcib_read_ivar(dev, child, which, result));
1532ccfc932SJohn Baldwin }
1542ccfc932SJohn Baldwin 
1552ccfc932SJohn Baldwin static int
acpi_pcib_pci_route_interrupt(device_t pcib,device_t dev,int pin)1562ccfc932SJohn Baldwin acpi_pcib_pci_route_interrupt(device_t pcib, device_t dev, int pin)
1572ccfc932SJohn Baldwin {
1582ccfc932SJohn Baldwin     struct acpi_pcib_softc *sc;
1592ccfc932SJohn Baldwin 
1602ccfc932SJohn Baldwin     sc = device_get_softc(pcib);
161fb0ac543SJohn Baldwin 
162fb0ac543SJohn Baldwin     /*
163fb0ac543SJohn Baldwin      * If we don't have a _PRT, fall back to the swizzle method
164fb0ac543SJohn Baldwin      * for routing interrupts.
165fb0ac543SJohn Baldwin      */
166fb0ac543SJohn Baldwin     if (sc->ap_prt.Pointer == NULL)
167fb0ac543SJohn Baldwin 	return (pcib_route_interrupt(pcib, dev, pin));
168fb0ac543SJohn Baldwin     else
1695e1ba6d4SJohn Baldwin 	return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt));
1702ccfc932SJohn Baldwin }
171