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