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