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