acpi_pcib.c revision 127156
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: head/sys/dev/acpica/acpi_pcib.c 127156 2004-03-18 02:33:58Z njl $"); 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 39#include <dev/acpica/acpivar.h> 40#include <dev/acpica/acpi_pcibvar.h> 41 42#include <machine/pci_cfgreg.h> 43#include <dev/pci/pcivar.h> 44#include <dev/pci/pcib_private.h> 45#include "pcib_if.h" 46 47/* 48 * Hooks for the ACPI CA debugging infrastructure 49 */ 50#define _COMPONENT ACPI_BUS 51ACPI_MODULE_NAME("PCI") 52 53int 54acpi_pcib_attach(device_t dev, ACPI_BUFFER *prt, int busno) 55{ 56 device_t child; 57 ACPI_STATUS status; 58 59 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 60 61 /* 62 * Don't attach if we're not really there. 63 * 64 * XXX: This isn't entirely correct since we may be a PCI bus 65 * on a hot-plug docking station, etc. 66 */ 67 if (!acpi_DeviceIsPresent(dev)) 68 return_VALUE(ENXIO); 69 70 /* 71 * Get the PCI interrupt routing table for this bus. 72 */ 73 prt->Length = ACPI_ALLOCATE_BUFFER; 74 status = AcpiGetIrqRoutingTable(acpi_get_handle(dev), prt); 75 if (ACPI_FAILURE(status)) 76 /* This is not an error, but it may reduce functionality. */ 77 device_printf(dev, 78 "could not get PCI interrupt routing table for %s - %s\n", 79 acpi_name(acpi_get_handle(dev)), AcpiFormatException(status)); 80 81 /* 82 * Attach the PCI bus proper. 83 */ 84 if ((child = device_add_child(dev, "pci", busno)) == NULL) { 85 device_printf(device_get_parent(dev), "couldn't attach pci bus\n"); 86 return_VALUE(ENXIO); 87 } 88 89 /* 90 * Now go scan the bus. 91 */ 92 acpi_pci_link_config(dev, prt, busno); 93 return_VALUE(bus_generic_attach(dev)); 94} 95 96int 97acpi_pcib_resume(device_t dev, ACPI_BUFFER *prt, int busno) 98{ 99 acpi_pci_link_resume(dev, prt, busno); 100 return (bus_generic_resume(dev)); 101} 102 103/* 104 * Route an interrupt for a child of the bridge. 105 * 106 * XXX clean up error messages 107 * 108 * XXX this function is somewhat bulky 109 */ 110int 111acpi_pcib_route_interrupt(device_t pcib, device_t dev, int pin, 112 ACPI_BUFFER *prtbuf) 113{ 114 ACPI_PCI_ROUTING_TABLE *prt; 115 ACPI_HANDLE lnkdev; 116 ACPI_BUFFER crsbuf, prsbuf, buf; 117 ACPI_RESOURCE *crsres, *prsres, resbuf; 118 ACPI_DEVICE_INFO *devinfo; 119 ACPI_STATUS status; 120 UINT32 NumberOfInterrupts; 121 UINT32 *Interrupts; 122 u_int8_t *prtp; 123 int interrupt; 124 int i; 125 126 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 127 128 buf.Pointer = NULL; 129 crsbuf.Pointer = NULL; 130 prsbuf.Pointer = NULL; 131 interrupt = 255; 132 133 /* ACPI numbers pins 0-3, not 1-4 like the BIOS */ 134 pin--; 135 136 prtp = prtbuf->Pointer; 137 if (prtp == NULL) /* didn't get routing table */ 138 goto out; 139 140 /* scan the table looking for this device */ 141 for (;;) { 142 prt = (ACPI_PCI_ROUTING_TABLE *)prtp; 143 144 if (prt->Length == 0) /* end of table */ 145 goto out; 146 147 /* 148 * Compare the slot number (high word of Address) and pin number 149 * (note that ACPI uses 0 for INTA) to check for a match. 150 * 151 * Note that the low word of the Address field (function number) 152 * is required by the specification to be 0xffff. We don't risk 153 * checking it here. 154 */ 155 if ((((prt->Address & 0xffff0000) >> 16) == pci_get_slot(dev)) && 156 (prt->Pin == pin)) { 157 if (bootverbose) 158 device_printf(pcib, "matched entry for %d.%d.INT%c (source %s)\n", 159 pci_get_bus(dev), pci_get_slot(dev), 'A' + pin, prt->Source); 160 break; 161 } 162 163 /* skip to next entry */ 164 prtp += prt->Length; 165 } 166 167 /* 168 * If source is empty/NULL, the source index is the global IRQ number. 169 */ 170 if ((prt->Source == NULL) || (prt->Source[0] == '\0')) { 171 if (bootverbose) 172 device_printf(pcib, "device is hardwired to IRQ %d\n", 173 prt->SourceIndex); 174 interrupt = prt->SourceIndex; 175 goto out; 176 } 177 178 /* 179 * We have to find the source device (PCI interrupt link device) 180 */ 181 if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, prt->Source, &lnkdev))) { 182 device_printf(pcib, "couldn't find PCI interrupt link device %s\n", 183 prt->Source); 184 goto out; 185 } 186 187 /* 188 * Verify that this is a PCI link device, and that it's present. 189 */ 190 buf.Length = ACPI_ALLOCATE_BUFFER; 191 if (ACPI_FAILURE(AcpiGetObjectInfo(lnkdev, &buf))) { 192 device_printf(pcib, "couldn't validate PCI interrupt link device %s\n", 193 prt->Source); 194 goto out; 195 } 196 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 197 if ((devinfo->Valid & ACPI_VALID_HID) == 0 || 198 strcmp("PNP0C0F", devinfo->HardwareId.Value) != 0) { 199 200 device_printf(pcib, "PCI interrupt link device %s has wrong _HID (%s)\n", 201 prt->Source, devinfo->HardwareId.Value); 202 goto out; 203 } 204 if ((devinfo->Valid & ACPI_VALID_STA) != 0 && 205 (devinfo->CurrentStatus & 0x9) != 0x9) { 206 207 device_printf(pcib, "PCI interrupt link device %s not present\n", 208 prt->Source); 209 goto out; 210 } 211 212 /* 213 * Get the current and possible resources for the interrupt link device. 214 */ 215 crsbuf.Length = ACPI_ALLOCATE_BUFFER; 216 if (ACPI_FAILURE(status = AcpiGetCurrentResources(lnkdev, &crsbuf))) { 217 device_printf(pcib, "couldn't get PCI interrupt link device _CRS data - %s\n", 218 AcpiFormatException(status)); 219 goto out; /* this is fatal */ 220 } 221 prsbuf.Length = ACPI_ALLOCATE_BUFFER; 222 if (ACPI_FAILURE(status = AcpiGetPossibleResources(lnkdev, &prsbuf))) { 223 device_printf(pcib, "couldn't get PCI interrupt link device _PRS data - %s\n", 224 AcpiFormatException(status)); 225 /* this is not fatal, since it may be hardwired */ 226 } 227 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "got %ld bytes for %s._CRS\n", 228 (long)crsbuf.Length, acpi_name(lnkdev))); 229 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "got %ld bytes for %s._PRS\n", 230 (long)prsbuf.Length, acpi_name(lnkdev))); 231 232 /* 233 * The interrupt may already be routed, so check _CRS first. We don't check the 234 * 'decoding' bit in the _STA result, since there's nothing in the spec that 235 * mandates it be set, however some BIOS' will set it if the decode is active. 236 * 237 * The Source Index points to the particular resource entry we're interested in. 238 */ 239 if (ACPI_FAILURE(acpi_FindIndexedResource(&crsbuf, prt->SourceIndex, &crsres))) { 240 device_printf(pcib, "_CRS buffer corrupt, cannot route interrupt\n"); 241 goto out; 242 } 243 244 /* type-check the resource we've got */ 245 if (crsres->Id != ACPI_RSTYPE_IRQ && crsres->Id != ACPI_RSTYPE_EXT_IRQ) { 246 device_printf(pcib, "_CRS resource entry has unsupported type %d\n", 247 crsres->Id); 248 goto out; 249 } 250 251 /* set variables based on resource type */ 252 if (crsres->Id == ACPI_RSTYPE_IRQ) { 253 NumberOfInterrupts = crsres->Data.Irq.NumberOfInterrupts; 254 Interrupts = crsres->Data.Irq.Interrupts; 255 } else { 256 NumberOfInterrupts = crsres->Data.ExtendedIrq.NumberOfInterrupts; 257 Interrupts = crsres->Data.ExtendedIrq.Interrupts; 258 } 259 260 /* if there's more than one interrupt, we are confused */ 261 if (NumberOfInterrupts > 1) { 262 device_printf(pcib, "device has too many interrupts (%d)\n", 263 NumberOfInterrupts); 264 goto out; 265 } 266 267 /* 268 * If there's only one interrupt, and it's not zero, then we're already routed. 269 * 270 * Note that we could also check the 'decoding' bit in _STA, but can't depend on 271 * it since it's not part of the spec. 272 * 273 * XXX check ASL examples to see if this is an acceptable set of tests 274 */ 275 if ((NumberOfInterrupts == 1) && (Interrupts[0] != 0)) { 276 device_printf(pcib, "slot %d INT%c is routed to irq %d\n", 277 pci_get_slot(dev), 'A' + pin, Interrupts[0]); 278 interrupt = Interrupts[0]; 279 goto out; 280 } 281 282 /* 283 * There isn't an interrupt, so we have to look at _PRS to get one. 284 * Get the set of allowed interrupts from the _PRS resource indexed by SourceIndex. 285 */ 286 if (prsbuf.Pointer == NULL) { 287 device_printf(pcib, "device has no routed interrupt and no _PRS on PCI interrupt link device\n"); 288 goto out; 289 } 290 291 /* 292 * Search through the _PRS resources, looking for an IRQ or extended 293 * IRQ resource. Skip dependent function resources for now. In the 294 * future, we might use these for priority but this is good enough for 295 * now until BIOS vendors actually mean something by using them. 296 */ 297 for (i = prt->SourceIndex; ; i++) { 298 if (ACPI_FAILURE(acpi_FindIndexedResource(&prsbuf, i, &prsres))) { 299 device_printf(pcib, "_PRS lacks IRQ resource, routing failed\n"); 300 goto out; 301 } 302 switch (prsres->Id) { 303 case ACPI_RSTYPE_IRQ: 304 case ACPI_RSTYPE_EXT_IRQ: 305 break; 306 case ACPI_RSTYPE_START_DPF: 307 continue; 308 default: 309 device_printf(pcib, "_PRS has invalid type %d\n", prsres->Id); 310 goto out; 311 } 312 } 313 314 /* set variables based on resource type */ 315 if (prsres->Id == ACPI_RSTYPE_IRQ) { 316 NumberOfInterrupts = prsres->Data.Irq.NumberOfInterrupts; 317 Interrupts = prsres->Data.Irq.Interrupts; 318 } else { 319 NumberOfInterrupts = prsres->Data.ExtendedIrq.NumberOfInterrupts; 320 Interrupts = prsres->Data.ExtendedIrq.Interrupts; 321 } 322 323 /* there has to be at least one interrupt available */ 324 if (NumberOfInterrupts < 1) { 325 device_printf(pcib, "device has no interrupts\n"); 326 goto out; 327 } 328 329 /* 330 * Pick an interrupt to use. Note that a more scientific approach than just 331 * taking the first one available would be desirable. 332 * 333 * The PCI BIOS $PIR table offers "preferred PCI interrupts", but ACPI doesn't 334 * seem to offer a similar mechanism, so picking a "good" interrupt here is a 335 * difficult task. 336 * 337 * Build a resource buffer and pass it to AcpiSetCurrentResources to route the 338 * new interrupt. 339 */ 340 device_printf(pcib, "possible interrupts:"); 341 for (i = 0; i < NumberOfInterrupts; i++) 342 printf(" %d", Interrupts[i]); 343 printf("\n"); 344 345 if (crsbuf.Pointer != NULL) /* should never happen */ 346 AcpiOsFree(crsbuf.Pointer); 347 crsbuf.Pointer = NULL; 348 if (prsres->Id == ACPI_RSTYPE_IRQ) { 349 resbuf.Id = ACPI_RSTYPE_IRQ; 350 resbuf.Length = ACPI_SIZEOF_RESOURCE(ACPI_RESOURCE_IRQ); 351 resbuf.Data.Irq = prsres->Data.Irq; /* structure copy other fields */ 352 resbuf.Data.Irq.NumberOfInterrupts = 1; 353 resbuf.Data.Irq.Interrupts[0] = Interrupts[0]; /* just take first... */ 354 } else { 355 resbuf.Id = ACPI_RSTYPE_EXT_IRQ; 356 resbuf.Length = ACPI_SIZEOF_RESOURCE(ACPI_RESOURCE_IRQ); 357 resbuf.Data.ExtendedIrq = prsres->Data.ExtendedIrq; /* structure copy other fields */ 358 resbuf.Data.ExtendedIrq.NumberOfInterrupts = 1; 359 resbuf.Data.ExtendedIrq.Interrupts[0] = Interrupts[0]; /* just take first... */ 360 } 361 if (ACPI_FAILURE(status = acpi_AppendBufferResource(&crsbuf, &resbuf))) { 362 device_printf(pcib, "couldn't route interrupt %d via %s, interrupt resource build failed - %s\n", 363 Interrupts[0], acpi_name(lnkdev), AcpiFormatException(status)); 364 goto out; 365 } 366 if (ACPI_FAILURE(status = AcpiSetCurrentResources(lnkdev, &crsbuf))) { 367 device_printf(pcib, "couldn't route interrupt %d via %s - %s\n", 368 Interrupts[0], acpi_name(lnkdev), AcpiFormatException(status)); 369 goto out; 370 } 371 372 /* successful, return the interrupt we just routed */ 373 device_printf(pcib, "slot %d INT%c routed to irq %d via %s\n", 374 pci_get_slot(dev), 'A' + pin, Interrupts[0], acpi_name(lnkdev)); 375 interrupt = Interrupts[0]; 376 377 out: 378 if (crsbuf.Pointer != NULL) 379 AcpiOsFree(crsbuf.Pointer); 380 if (prsbuf.Pointer != NULL) 381 AcpiOsFree(prsbuf.Pointer); 382 if (buf.Pointer != NULL) 383 AcpiOsFree(buf.Pointer); 384 385 /* XXX APIC_IO interrupt mapping? */ 386 return_VALUE(interrupt); 387} 388