ACPI: Add missing ACPI_SERIAL_INIT()'s.
[dragonfly.git] / sys / dev / acpica5 / acpi_pcib.c
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  * __FBSDID("$FreeBSD: src/sys/dev/acpica/acpi_pcib.c,v 1.60.8.1 2009/04/15 03:14:26 kensmith Exp $");
27  */
28
29 #include <sys/cdefs.h>
30
31 #include "opt_acpi.h"
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
36 #include <machine/smp.h>
37
38 #include "acpi.h"
39 #include <dev/acpica5/acpivar.h>
40 #include <dev/acpica5/acpi_pcibvar.h>
41
42 #include <bus/pci/pcivar.h>
43 #include <bus/pci/pcireg.h>
44 #include "pcib_if.h"
45
46 /* Hooks for the ACPI CA debugging infrastructure. */
47 #define _COMPONENT      ACPI_BUS
48 ACPI_MODULE_NAME("PCI")
49
50 ACPI_SERIAL_DECL(pcib, "ACPI PCI bus methods");
51
52 /*
53  * For locking, we assume the caller is not concurrent since this is
54  * triggered by newbus methods.
55  */
56
57 struct prt_lookup_request {
58     ACPI_PCI_ROUTING_TABLE *pr_entry;
59     u_int       pr_pin;
60     u_int       pr_slot;
61 };
62
63 typedef void prt_entry_handler(ACPI_PCI_ROUTING_TABLE *entry, void *arg);
64
65 static void     prt_attach_devices(ACPI_PCI_ROUTING_TABLE *entry, void *arg);
66 static void     prt_lookup_device(ACPI_PCI_ROUTING_TABLE *entry, void *arg);
67 static void     prt_walk_table(ACPI_BUFFER *prt, prt_entry_handler *handler,
68                     void *arg);
69
70 #define PCI_INVALID_IRQ 255
71
72 static void
73 prt_walk_table(ACPI_BUFFER *prt, prt_entry_handler *handler, void *arg)
74 {
75     ACPI_PCI_ROUTING_TABLE *entry;
76     char *prtptr;
77
78     /* First check to see if there is a table to walk. */
79     if (prt == NULL || prt->Pointer == NULL)
80         return;
81
82     /* Walk the table executing the handler function for each entry. */
83     prtptr = prt->Pointer;
84     entry = (ACPI_PCI_ROUTING_TABLE *)prtptr;
85     while (entry->Length != 0) {
86         handler(entry, arg);
87         prtptr += entry->Length;
88         entry = (ACPI_PCI_ROUTING_TABLE *)prtptr;
89     }
90 }
91
92 static void
93 prt_attach_devices(ACPI_PCI_ROUTING_TABLE *entry, void *arg)
94 {
95     ACPI_HANDLE handle;
96     device_t child, pcib;
97     int error;
98
99     /* We only care about entries that reference a link device. */
100     if (entry->Source == NULL || entry->Source[0] == '\0')
101         return;
102
103     /*
104      * In practice, we only see SourceIndex's of 0 out in the wild.
105      * When indices != 0 have been found, they've been bugs in the ASL.
106      */
107     if (entry->SourceIndex != 0)
108         return;
109
110     /* Lookup the associated handle and device. */
111     pcib = (device_t)arg;
112     if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, entry->Source, &handle)))
113         return;
114     child = acpi_get_device(handle);
115     if (child == NULL)
116         return;
117
118     /* If the device hasn't been probed yet, force it to do so. */
119     error = device_probe_and_attach(child);
120     if (error != 0) {
121         device_printf(pcib, "failed to force attach of %s\n",
122             acpi_name(handle));
123         return;
124     }
125
126     /* Add a reference for a specific bus/device/pin tuple. */
127     acpi_pci_link_add_reference(child, entry->SourceIndex, pcib,
128         ACPI_ADR_PCI_SLOT(entry->Address), entry->Pin);
129 }
130
131 int
132 acpi_pcib_attach(device_t dev, ACPI_BUFFER *prt, int busno)
133 {
134     device_t                    child;
135     ACPI_STATUS                 status;
136
137     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
138
139     /*
140      * Don't attach if we're not really there.
141      *
142      * XXX: This isn't entirely correct since we may be a PCI bus
143      * on a hot-plug docking station, etc.
144      */
145
146     if (!acpi_DeviceIsPresent(dev))
147         return_VALUE(ENXIO);
148
149     ACPI_SERIAL_INIT(pcib);
150
151     /*
152      * Get the PCI interrupt routing table for this bus.  If we can't
153      * get it, this is not an error but may reduce functionality.  There
154      * are several valid bridges in the field that do not have a _PRT, so
155      * only warn about missing tables if bootverbose is set.
156      */
157     prt->Length = ACPI_ALLOCATE_BUFFER;
158     status = AcpiGetIrqRoutingTable(acpi_get_handle(dev), prt);
159     if (ACPI_FAILURE(status) && (bootverbose || status != AE_NOT_FOUND))
160         device_printf(dev,
161             "could not get PCI interrupt routing table for %s - %s\n",
162             acpi_name(acpi_get_handle(dev)), AcpiFormatException(status));
163
164     /*
165      * Attach the PCI bus proper.
166      */
167     if ((child = device_add_child(dev, "pci", busno)) == NULL) {
168         device_printf(device_get_parent(dev), "couldn't attach pci bus\n");
169         return_VALUE(ENXIO);
170     }
171
172     /*
173      * Now go scan the bus.
174      */
175     prt_walk_table(prt, prt_attach_devices, dev);
176
177     return_VALUE (bus_generic_attach(dev));
178 }
179
180 int
181 acpi_pcib_resume(device_t dev)
182 {
183
184     return (bus_generic_resume(dev));
185 }
186
187 static void
188 prt_lookup_device(ACPI_PCI_ROUTING_TABLE *entry, void *arg)
189 {
190     struct prt_lookup_request *pr;
191
192     pr = (struct prt_lookup_request *)arg;
193     if (pr->pr_entry != NULL)
194         return;
195
196     /*
197      * Compare the slot number (high word of Address) and pin number
198      * (note that ACPI uses 0 for INTA) to check for a match.
199      *
200      * Note that the low word of the Address field (function number)
201      * is required by the specification to be 0xffff.  We don't risk
202      * checking it here.
203      */
204     if (ACPI_ADR_PCI_SLOT(entry->Address) == pr->pr_slot &&
205         entry->Pin == pr->pr_pin)
206             pr->pr_entry = entry;
207 }
208
209 /*
210  * Route an interrupt for a child of the bridge.
211  */
212 int
213 acpi_pcib_route_interrupt(device_t pcib, device_t dev, int pin,
214     ACPI_BUFFER *prtbuf)
215 {
216     ACPI_PCI_ROUTING_TABLE *prt;
217     struct prt_lookup_request pr;
218     ACPI_HANDLE lnkdev;
219     int interrupt;
220
221     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
222
223     interrupt = PCI_INVALID_IRQ;
224
225     /* ACPI numbers pins 0-3, not 1-4 like the BIOS. */
226     pin--;
227
228     ACPI_SERIAL_BEGIN(pcib);
229
230     /* Search for a matching entry in the routing table. */
231     pr.pr_entry = NULL;
232     pr.pr_pin = pin;
233     pr.pr_slot = pci_get_slot(dev);
234     prt_walk_table(prtbuf, prt_lookup_device, &pr);
235     if (pr.pr_entry == NULL) {
236         device_printf(pcib, "no PRT entry for %d.%d.INT%c\n", pci_get_bus(dev),
237             pci_get_slot(dev), 'A' + pin);
238         goto out;
239     }
240     prt = pr.pr_entry;
241
242     if (bootverbose) {
243         device_printf(pcib, "matched entry for %d.%d.INT%c",
244             pci_get_bus(dev), pci_get_slot(dev), 'A' + pin);
245         if (prt->Source != NULL && prt->Source[0] != '\0')
246                 kprintf(" (src %s:%u)", prt->Source, prt->SourceIndex);
247         kprintf("\n");
248     }
249
250     /*
251      * If source is empty/NULL, the source index is a global IRQ number
252      * and it's hard-wired so we're done.
253      *
254      * XXX: If the source index is non-zero, ignore the source device and
255      * assume that this is a hard-wired entry.
256      */
257     if (prt->Source == NULL || prt->Source[0] == '\0' ||
258         prt->SourceIndex != 0) {
259         if (bootverbose)
260             device_printf(pcib, "slot %d INT%c hardwired to IRQ %d\n",
261                 pci_get_slot(dev), 'A' + pin, prt->SourceIndex);
262         if (prt->SourceIndex) {
263             interrupt = prt->SourceIndex;
264 /*      interrupt = BUS_CONFIG_INTR(device_get_parent(dev), dev, interrupt, INTR_TRIGGER_LEVEL,
265                 INTR_POLARITY_LOW);
266 */
267         /* pci_apic_irq() accepts pin number (WHY?) instead of irq number, so we pass it.
268          * Interrupt routing still depends on mptable and is not of any use then.
269          */
270         interrupt = BUS_CONFIG_INTR(device_get_parent(dev), dev, pin, INTR_TRIGGER_LEVEL,
271                 INTR_POLARITY_LOW);
272         } else
273             device_printf(pcib, "error: invalid hard-wired IRQ of 0\n");
274         goto out;
275     }
276
277     /*
278      * We have to find the source device (PCI interrupt link device).
279      */
280     if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, prt->Source, &lnkdev))) {
281         device_printf(pcib, "couldn't find PCI interrupt link device %s\n",
282             prt->Source);
283         goto out;
284     }
285     interrupt = acpi_pci_link_route_interrupt(acpi_get_device(lnkdev),
286         prt->SourceIndex);
287
288     if (bootverbose && PCI_INTERRUPT_VALID(interrupt)) {
289         if (PCI_INTERRUPT_VALID(interrupt))
290             device_printf(pcib, "slot %d INT%c routed to irq %d via %s\n",
291                  pci_get_slot(dev), 'A' + pin, interrupt, acpi_name(lnkdev));
292     }
293
294 out:
295     ACPI_SERIAL_END(pcib);
296
297     return_VALUE (interrupt);
298 }