xref: /freebsd/sys/dev/acpica/acpi_pcib_pci.c (revision 3416500aef140042c64bc149cb1ec6620483bc44)
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "opt_acpi.h"
32 
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/rman.h>
39 
40 #include <contrib/dev/acpica/include/acpi.h>
41 #include <contrib/dev/acpica/include/accommon.h>
42 
43 #include <dev/acpica/acpivar.h>
44 #include <dev/acpica/acpi_pcibvar.h>
45 
46 #include <machine/pci_cfgreg.h>
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pcireg.h>
49 #include <dev/pci/pcib_private.h>
50 #include "pcib_if.h"
51 
52 /* Hooks for the ACPI CA debugging infrastructure. */
53 #define _COMPONENT	ACPI_BUS
54 ACPI_MODULE_NAME("PCI_PCI")
55 
56 struct acpi_pcib_softc {
57     struct pcib_softc	ap_pcibsc;
58     ACPI_HANDLE		ap_handle;
59     ACPI_BUFFER		ap_prt;		/* interrupt routing table */
60 };
61 
62 struct acpi_pcib_lookup_info {
63     UINT32		address;
64     ACPI_HANDLE		handle;
65 };
66 
67 static int		acpi_pcib_pci_probe(device_t bus);
68 static int		acpi_pcib_pci_attach(device_t bus);
69 static int		acpi_pcib_pci_detach(device_t bus);
70 static int		acpi_pcib_read_ivar(device_t dev, device_t child,
71 			    int which, uintptr_t *result);
72 static int		acpi_pcib_pci_route_interrupt(device_t pcib,
73 			    device_t dev, int pin);
74 
75 static device_method_t acpi_pcib_pci_methods[] = {
76     /* Device interface */
77     DEVMETHOD(device_probe,		acpi_pcib_pci_probe),
78     DEVMETHOD(device_attach,		acpi_pcib_pci_attach),
79     DEVMETHOD(device_detach,		acpi_pcib_pci_detach),
80 
81     /* Bus interface */
82     DEVMETHOD(bus_read_ivar,		acpi_pcib_read_ivar),
83     DEVMETHOD(bus_get_cpus,		acpi_pcib_get_cpus),
84 
85     /* pcib interface */
86     DEVMETHOD(pcib_route_interrupt,	acpi_pcib_pci_route_interrupt),
87     DEVMETHOD(pcib_power_for_sleep,	acpi_pcib_power_for_sleep),
88 
89     DEVMETHOD_END
90 };
91 
92 static devclass_t pcib_devclass;
93 
94 DEFINE_CLASS_1(pcib, acpi_pcib_pci_driver, acpi_pcib_pci_methods,
95     sizeof(struct acpi_pcib_softc), pcib_driver);
96 DRIVER_MODULE(acpi_pcib, pci, acpi_pcib_pci_driver, pcib_devclass, 0, 0);
97 MODULE_DEPEND(acpi_pcib, acpi, 1, 1, 1);
98 
99 static int
100 acpi_pcib_pci_probe(device_t dev)
101 {
102 
103     if (pci_get_class(dev) != PCIC_BRIDGE ||
104 	pci_get_subclass(dev) != PCIS_BRIDGE_PCI ||
105 	acpi_disabled("pci"))
106 	return (ENXIO);
107     if (acpi_get_handle(dev) == NULL)
108 	return (ENXIO);
109     if (pci_cfgregopen() == 0)
110 	return (ENXIO);
111 
112     device_set_desc(dev, "ACPI PCI-PCI bridge");
113     return (-1000);
114 }
115 
116 static int
117 acpi_pcib_pci_attach(device_t dev)
118 {
119     struct acpi_pcib_softc *sc;
120 
121     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
122 
123     pcib_attach_common(dev);
124     sc = device_get_softc(dev);
125     sc->ap_handle = acpi_get_handle(dev);
126     acpi_pcib_fetch_prt(dev, &sc->ap_prt);
127 
128     return (pcib_attach_child(dev));
129 }
130 
131 static int
132 acpi_pcib_pci_detach(device_t dev)
133 {
134     struct acpi_pcib_softc *sc;
135     int error;
136 
137     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
138 
139     sc = device_get_softc(dev);
140     error = pcib_detach(dev);
141     if (error == 0)
142 	    AcpiOsFree(sc->ap_prt.Pointer);
143     return (error);
144 }
145 
146 static int
147 acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
148 {
149     struct acpi_pcib_softc *sc = device_get_softc(dev);
150 
151     switch (which) {
152     case ACPI_IVAR_HANDLE:
153 	*result = (uintptr_t)sc->ap_handle;
154 	return (0);
155     }
156     return (pcib_read_ivar(dev, child, which, result));
157 }
158 
159 static int
160 acpi_pcib_pci_route_interrupt(device_t pcib, device_t dev, int pin)
161 {
162     struct acpi_pcib_softc *sc;
163 
164     sc = device_get_softc(pcib);
165 
166     /*
167      * If we don't have a _PRT, fall back to the swizzle method
168      * for routing interrupts.
169      */
170     if (sc->ap_prt.Pointer == NULL)
171 	return (pcib_route_interrupt(pcib, dev, pin));
172     else
173 	return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt));
174 }
175