2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
38 #include <dev/acpica/acpivar.h>
39 #include <dev/acpica/acpi_pcibvar.h>
41 #include <dev/pci/pcivar.h>
44 /* Hooks for the ACPI CA debugging infrastructure. */
45 #define _COMPONENT ACPI_BUS
46 ACPI_MODULE_NAME("PCI")
48 ACPI_SERIAL_DECL(pcib, "ACPI PCI bus methods");
51 * For locking, we assume the caller is not concurrent since this is
52 * triggered by newbus methods.
55 struct prt_lookup_request {
56 ACPI_PCI_ROUTING_TABLE *pr_entry;
61 typedef void prt_entry_handler(ACPI_PCI_ROUTING_TABLE *entry, void *arg);
63 static void prt_attach_devices(ACPI_PCI_ROUTING_TABLE *entry, void *arg);
64 static void prt_lookup_device(ACPI_PCI_ROUTING_TABLE *entry, void *arg);
65 static void prt_walk_table(ACPI_BUFFER *prt, prt_entry_handler *handler,
69 prt_walk_table(ACPI_BUFFER *prt, prt_entry_handler *handler, void *arg)
71 ACPI_PCI_ROUTING_TABLE *entry;
74 /* First check to see if there is a table to walk. */
75 if (prt == NULL || prt->Pointer == NULL)
78 /* Walk the table executing the handler function for each entry. */
79 prtptr = prt->Pointer;
80 entry = (ACPI_PCI_ROUTING_TABLE *)prtptr;
81 while (entry->Length != 0) {
83 prtptr += entry->Length;
84 entry = (ACPI_PCI_ROUTING_TABLE *)prtptr;
89 prt_attach_devices(ACPI_PCI_ROUTING_TABLE *entry, void *arg)
95 /* We only care about entries that reference a link device. */
96 if (entry->Source == NULL || entry->Source[0] == '\0')
99 /* Lookup the associated handle and device. */
100 pcib = (device_t)arg;
101 if (ACPI_FAILURE(AcpiGetHandle(acpi_get_handle(pcib), entry->Source,
104 child = acpi_get_device(handle);
108 /* If the device hasn't been probed yet, force it to do so. */
109 error = device_probe_and_attach(child);
111 device_printf((device_t)arg, "failed to force attach of %s\n",
116 /* Add a reference for a specific bus/device/pin tuple. */
117 acpi_pci_link_add_reference(child, entry->SourceIndex, pcib,
118 ACPI_ADR_PCI_SLOT(entry->Address), entry->Pin);
122 acpi_pcib_attach(device_t dev, ACPI_BUFFER *prt, int busno)
127 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
130 * Don't attach if we're not really there.
132 * XXX: This isn't entirely correct since we may be a PCI bus
133 * on a hot-plug docking station, etc.
135 if (!acpi_DeviceIsPresent(dev))
139 * Get the PCI interrupt routing table for this bus. If we can't
140 * get it, this is not an error but may reduce functionality. There
141 * are several valid bridges in the field that do not have a _PRT, so
142 * only warn about missing tables if bootverbose is set.
144 prt->Length = ACPI_ALLOCATE_BUFFER;
145 status = AcpiGetIrqRoutingTable(acpi_get_handle(dev), prt);
146 if (ACPI_FAILURE(status) && (bootverbose || status != AE_NOT_FOUND))
148 "could not get PCI interrupt routing table for %s - %s\n",
149 acpi_name(acpi_get_handle(dev)), AcpiFormatException(status));
152 * Attach the PCI bus proper.
154 if ((child = device_add_child(dev, "pci", busno)) == NULL) {
155 device_printf(device_get_parent(dev), "couldn't attach pci bus\n");
160 * Now go scan the bus.
162 prt_walk_table(prt, prt_attach_devices, dev);
164 return_VALUE (bus_generic_attach(dev));
168 acpi_pcib_resume(device_t dev)
171 return (bus_generic_resume(dev));
175 prt_lookup_device(ACPI_PCI_ROUTING_TABLE *entry, void *arg)
177 struct prt_lookup_request *pr;
179 pr = (struct prt_lookup_request *)arg;
180 if (pr->pr_entry != NULL)
184 * Compare the slot number (high word of Address) and pin number
185 * (note that ACPI uses 0 for INTA) to check for a match.
187 * Note that the low word of the Address field (function number)
188 * is required by the specification to be 0xffff. We don't risk
191 if (ACPI_ADR_PCI_SLOT(entry->Address) == pr->pr_slot &&
192 entry->Pin == pr->pr_pin)
193 pr->pr_entry = entry;
197 * Route an interrupt for a child of the bridge.
200 acpi_pcib_route_interrupt(device_t pcib, device_t dev, int pin,
203 ACPI_PCI_ROUTING_TABLE *prt;
204 struct prt_lookup_request pr;
208 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
210 interrupt = PCI_INVALID_IRQ;
212 /* ACPI numbers pins 0-3, not 1-4 like the BIOS. */
215 ACPI_SERIAL_BEGIN(pcib);
217 /* Search for a matching entry in the routing table. */
220 pr.pr_slot = pci_get_slot(dev);
221 prt_walk_table(prtbuf, prt_lookup_device, &pr);
222 if (pr.pr_entry == NULL)
223 return (PCI_INVALID_IRQ);
227 device_printf(pcib, "matched entry for %d.%d.INT%c",
228 pci_get_bus(dev), pci_get_slot(dev), 'A' + pin);
229 if (prt->Source != NULL && prt->Source[0] != '\0')
230 printf(" (src %s:%u)", prt->Source, prt->SourceIndex);
235 * If source is empty/NULL, the source index is a global IRQ number
236 * and it's hard-wired so we're done.
238 if (prt->Source == NULL || prt->Source[0] == '\0') {
240 device_printf(pcib, "slot %d INT%c hardwired to IRQ %d\n",
241 pci_get_slot(dev), 'A' + pin, prt->SourceIndex);
242 if (prt->SourceIndex)
243 interrupt = prt->SourceIndex;
245 device_printf(pcib, "error: invalid hard-wired IRQ of 0\n");
250 * We have to find the source device (PCI interrupt link device).
252 if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, prt->Source, &lnkdev))) {
253 device_printf(pcib, "couldn't find PCI interrupt link device %s\n",
257 interrupt = acpi_pci_link_route_interrupt(acpi_get_device(lnkdev),
260 if (bootverbose && PCI_INTERRUPT_VALID(interrupt))
261 device_printf(pcib, "slot %d INT%c routed to irq %d via %s\n",
262 pci_get_slot(dev), 'A' + pin, interrupt, acpi_name(lnkdev));
265 ACPI_SERIAL_END(pcib);
267 return_VALUE (interrupt);