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