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