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$ 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 39 #include <machine/pci_cfgreg.h> 40 #include <pci/pcivar.h> 41 #include "pcib_if.h" 42 43 /* 44 * Hooks for the ACPI CA debugging infrastructure 45 */ 46 #define _COMPONENT ACPI_BUS 47 MODULE_NAME("PCI") 48 49 struct acpi_pcib_softc { 50 device_t ap_dev; 51 ACPI_HANDLE ap_handle; 52 53 int ap_segment; /* analagous to Alpha 'hose' */ 54 int ap_bus; /* bios-assigned bus number */ 55 56 ACPI_BUFFER ap_prt; /* interrupt routing table */ 57 }; 58 59 static int acpi_pcib_probe(device_t bus); 60 static int acpi_pcib_attach(device_t bus); 61 static int acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result); 62 static int acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value); 63 static int acpi_pcib_maxslots(device_t dev); 64 static u_int32_t acpi_pcib_read_config(device_t dev, int bus, int slot, int func, int reg, int bytes); 65 static void acpi_pcib_write_config(device_t dev, int bus, int slot, int func, int reg, 66 u_int32_t data, int bytes); 67 static int acpi_pcib_route_interrupt(device_t pcib, device_t dev, int pin); 68 69 static device_method_t acpi_pcib_methods[] = { 70 /* Device interface */ 71 DEVMETHOD(device_probe, acpi_pcib_probe), 72 DEVMETHOD(device_attach, acpi_pcib_attach), 73 DEVMETHOD(device_shutdown, bus_generic_shutdown), 74 DEVMETHOD(device_suspend, bus_generic_suspend), 75 DEVMETHOD(device_resume, bus_generic_resume), 76 77 /* Bus interface */ 78 DEVMETHOD(bus_print_child, bus_generic_print_child), 79 DEVMETHOD(bus_read_ivar, acpi_pcib_read_ivar), 80 DEVMETHOD(bus_write_ivar, acpi_pcib_write_ivar), 81 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource), 82 DEVMETHOD(bus_release_resource, bus_generic_release_resource), 83 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 84 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 85 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 86 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 87 88 /* pcib interface */ 89 DEVMETHOD(pcib_maxslots, acpi_pcib_maxslots), 90 DEVMETHOD(pcib_read_config, acpi_pcib_read_config), 91 DEVMETHOD(pcib_write_config, acpi_pcib_write_config), 92 DEVMETHOD(pcib_route_interrupt, acpi_pcib_route_interrupt), 93 94 {0, 0} 95 }; 96 97 static driver_t acpi_pcib_driver = { 98 "acpi_pcib", 99 acpi_pcib_methods, 100 sizeof(struct acpi_pcib_softc), 101 }; 102 103 devclass_t acpi_pcib_devclass; 104 DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_driver, acpi_pcib_devclass, 0, 0); 105 106 static int 107 acpi_pcib_probe(device_t dev) 108 { 109 110 if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) && 111 !acpi_disabled("pci") && 112 acpi_MatchHid(dev, "PNP0A03")) { 113 114 /* 115 * Set device description 116 */ 117 device_set_desc(dev, "Host-PCI bridge"); 118 return(0); 119 } 120 return(ENXIO); 121 } 122 123 static int 124 acpi_pcib_attach(device_t dev) 125 { 126 struct acpi_pcib_softc *sc; 127 device_t child; 128 ACPI_STATUS status; 129 int result; 130 131 FUNCTION_TRACE(__func__); 132 133 sc = device_get_softc(dev); 134 sc->ap_dev = dev; 135 sc->ap_handle = acpi_get_handle(dev); 136 137 /* 138 * Don't attach if we're not really there. 139 * 140 * XXX this isn't entirely correct, since we may be a PCI bus 141 * on a hot-plug docking station, etc. 142 */ 143 if (!acpi_DeviceIsPresent(dev)) 144 return_VALUE(ENXIO); 145 146 /* 147 * Get our segment number by evaluating _SEG 148 * It's OK for this to not exist. 149 */ 150 if ((status = acpi_EvaluateInteger(sc->ap_handle, "_SEG", &sc->ap_segment)) != AE_OK) { 151 if (status != AE_NOT_FOUND) { 152 device_printf(dev, "could not evaluate _SEG - %s\n", AcpiFormatException(status)); 153 return_VALUE(ENXIO); 154 } 155 /* if it's not found, assume 0 */ 156 sc->ap_segment = 0; 157 } 158 159 /* 160 * Get our base bus number by evaluating _BBN 161 * If this doesn't exist, we assume we're bus number 0. 162 * 163 * XXX note that it may also not exist in the case where we are 164 * meant to use a private configuration space mechanism for this bus, 165 * so we should dig out our resources and check to see if we have 166 * anything like that. How do we do this? 167 * XXX If we have the requisite information, and if we don't think the 168 * default PCI configuration space handlers can deal with this bus, 169 * we should attach our own handler. 170 * XXX invoke _REG on this for the PCI config space address space? 171 */ 172 if ((status = acpi_EvaluateInteger(sc->ap_handle, "_BBN", &sc->ap_bus)) != AE_OK) { 173 if (status != AE_NOT_FOUND) { 174 device_printf(dev, "could not evaluate _BBN - %s\n", AcpiFormatException(status)); 175 return_VALUE(ENXIO); 176 } 177 /* if it's not found, assume 0 */ 178 sc->ap_bus = 0; 179 } 180 181 /* 182 * Make sure that this bus hasn't already been found. If it has, return silently 183 * (should we complain here?). 184 */ 185 if (devclass_get_device(devclass_find("pci"), sc->ap_bus) != NULL) 186 return_VALUE(0); 187 188 /* 189 * Get the PCI interrupt routing table. 190 */ 191 if ((status = acpi_GetIntoBuffer(sc->ap_handle, AcpiGetIrqRoutingTable, &sc->ap_prt)) != AE_OK) { 192 device_printf(dev, "could not get PCI interrupt routing table - %s\n", AcpiFormatException(status)); 193 /* this is not an error, but it may reduce functionality */ 194 } 195 196 /* 197 * Attach the PCI bus proper. 198 */ 199 if ((child = device_add_child(dev, "pci", sc->ap_bus)) == NULL) { 200 device_printf(device_get_parent(dev), "couldn't attach pci bus"); 201 return_VALUE(ENXIO); 202 } 203 204 /* 205 * Now go scan the bus. 206 * 207 * XXX It would be nice to defer this and count on the nexus getting it 208 * after the first pass, but this does not seem to be reliable. 209 */ 210 result = bus_generic_attach(dev); 211 212 /* 213 * XXX cross-reference our children to attached devices on the child bus 214 * via _ADR, so we can provide power management. 215 */ 216 /* XXX implement */ 217 218 return_VALUE(result); 219 } 220 221 /* 222 * ACPI doesn't tell us how many slots there are, so use the standard 223 * maximum. 224 */ 225 static int 226 acpi_pcib_maxslots(device_t dev) 227 { 228 return(PCI_SLOTMAX); 229 } 230 231 /* 232 * Support for standard PCI bridge ivars. 233 */ 234 static int 235 acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 236 { 237 struct acpi_pcib_softc *sc = device_get_softc(dev); 238 239 switch (which) { 240 case PCIB_IVAR_BUS: 241 *result = sc->ap_bus; 242 return(0); 243 } 244 return(ENOENT); 245 } 246 247 static int 248 acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 249 { 250 struct acpi_pcib_softc *sc = device_get_softc(dev); 251 252 switch (which) { 253 case PCIB_IVAR_BUS: 254 sc->ap_bus = value; 255 return(0); 256 } 257 return(ENOENT); 258 } 259 260 static u_int32_t 261 acpi_pcib_read_config(device_t dev, int bus, int slot, int func, int reg, int bytes) 262 { 263 return(pci_cfgregread(bus, slot, func, reg, bytes)); 264 } 265 266 static void 267 acpi_pcib_write_config(device_t dev, int bus, int slot, int func, int reg, u_int32_t data, int bytes) 268 { 269 pci_cfgregwrite(bus, slot, func, reg, data, bytes); 270 } 271 272 /* 273 * Route an interrupt for a child of the bridge. 274 * 275 * XXX clean up error messages 276 * 277 * XXX this function is somewhat bulky 278 */ 279 static int 280 acpi_pcib_route_interrupt(device_t pcib, device_t dev, int pin) 281 { 282 struct acpi_pcib_softc *sc; 283 PCI_ROUTING_TABLE *prt; 284 ACPI_HANDLE lnkdev; 285 ACPI_BUFFER crsbuf, prsbuf; 286 ACPI_RESOURCE *crsres, *prsres, resbuf; 287 ACPI_DEVICE_INFO devinfo; 288 ACPI_STATUS status; 289 u_int8_t *prtp; 290 device_t *devlist; 291 int devcount; 292 int bus; 293 int interrupt; 294 int i; 295 296 crsbuf.Pointer = NULL; 297 prsbuf.Pointer = NULL; 298 devlist = NULL; 299 interrupt = 255; 300 301 /* ACPI numbers pins 0-3, not 1-4 like the BIOS */ 302 pin--; 303 304 /* find the bridge softc */ 305 if (devclass_get_devices(acpi_pcib_devclass, &devlist, &devcount)) 306 goto out; 307 BUS_READ_IVAR(pcib, pcib, PCIB_IVAR_BUS, (uintptr_t *)&bus); 308 sc = NULL; 309 for (i = 0; i < devcount; i++) { 310 sc = device_get_softc(*(devlist + i)); 311 if (sc->ap_bus == bus) 312 break; 313 sc = NULL; 314 } 315 if (sc == NULL) /* not one of ours */ 316 goto out; 317 prtp = sc->ap_prt.Pointer; 318 if (prtp == NULL) /* didn't get routing table */ 319 goto out; 320 321 /* scan the table looking for this device */ 322 for (;;) { 323 prt = (PCI_ROUTING_TABLE *)prtp; 324 325 if (prt->Length == 0) /* end of table */ 326 goto out; 327 328 /* 329 * Compare the slot number (high word of Address) and pin number 330 * (note that ACPI uses 0 for INTA) to check for a match. 331 * 332 * Note that the low word of the Address field (function number) 333 * is required by the specification to be 0xffff. We don't risk 334 * checking it here. 335 */ 336 if ((((prt->Address & 0xffff0000) >> 16) == pci_get_slot(dev)) && 337 (prt->Pin == pin)) { 338 device_printf(sc->ap_dev, "matched entry for %d.%d.INT%c (source %s)\n", 339 pci_get_bus(dev), pci_get_slot(dev), 'A' + pin, prt->Source); 340 break; 341 } 342 343 /* skip to next entry */ 344 prtp += prt->Length; 345 } 346 347 /* 348 * If source is empty/NULL, the source index is the global IRQ number. 349 */ 350 if ((prt->Source == NULL) || (prt->Source[0] == '\0')) { 351 device_printf(sc->ap_dev, "device is hardwired to IRQ %d\n", prt->SourceIndex); 352 interrupt = prt->SourceIndex; 353 goto out; 354 } 355 356 /* 357 * We have to find the source device (PCI interrupt link device) 358 */ 359 if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, prt->Source, &lnkdev))) { 360 device_printf(sc->ap_dev, "couldn't find PCI interrupt link device %s\n", prt->Source); 361 goto out; 362 } 363 364 /* 365 * Verify that this is a PCI link device, and that it's present. 366 */ 367 if (ACPI_FAILURE(AcpiGetObjectInfo(lnkdev, &devinfo))) { 368 device_printf(sc->ap_dev, "couldn't validate PCI interrupt link device %s\n", prt->Source); 369 goto out; 370 } 371 if (!(devinfo.Valid & ACPI_VALID_HID) || strcmp("PNP0C0F", devinfo.HardwareId)) { 372 device_printf(sc->ap_dev, "PCI interrupt link device %s has wrong _HID (%s)\n", 373 prt->Source, devinfo.HardwareId); 374 goto out; 375 } 376 /* should be 'present' and 'functioning' */ 377 if ((devinfo.CurrentStatus & 0x09) != 0x09) { 378 device_printf(sc->ap_dev, "PCI interrupt link device %s unavailable (CurrentStatus 0x%x)\n", 379 prt->Source, devinfo.CurrentStatus); 380 goto out; 381 } 382 383 /* 384 * Get the current and possible resources for the interrupt link device. 385 */ 386 if (ACPI_FAILURE(status = acpi_GetIntoBuffer(lnkdev, AcpiGetCurrentResources, &crsbuf))) { 387 device_printf(sc->ap_dev, "couldn't get PCI interrupt link device _CRS data - %s\n", 388 AcpiFormatException(status)); 389 goto out; /* this is fatal */ 390 } 391 if ((status = acpi_GetIntoBuffer(lnkdev, AcpiGetPossibleResources, &prsbuf)) != AE_OK) { 392 device_printf(sc->ap_dev, "couldn't get PCI interrupt link device _PRS data - %s\n", 393 AcpiFormatException(status)); 394 /* this is not fatal, since it may be hardwired */ 395 } 396 DEBUG_PRINT(TRACE_RESOURCES, ("got %d bytes for %s._CRS\n", crsbuf.Length, acpi_name(lnkdev))); 397 DEBUG_PRINT(TRACE_RESOURCES, ("got %d bytes for %s._PRS\n", prsbuf.Length, acpi_name(lnkdev))); 398 399 /* 400 * The interrupt may already be routed, so check _CRS first. We don't check the 401 * 'decoding' bit in the _STA result, since there's nothing in the spec that 402 * mandates it be set, however some BIOS' will set it if the decode is active. 403 * 404 * The Source Index points to the particular resource entry we're interested in. 405 */ 406 if (ACPI_FAILURE(acpi_FindIndexedResource(&crsbuf, prt->SourceIndex, &crsres))) { 407 device_printf(sc->ap_dev, "_CRS buffer corrupt, cannot route interrupt\n"); 408 goto out; 409 } 410 411 /* type-check the resource we've got */ 412 if (crsres->Id != ACPI_RSTYPE_IRQ) { /* XXX ACPI_RSTYPE_EXT_IRQ */ 413 device_printf(sc->ap_dev, "_CRS resource entry has unsupported type %d\n", crsres->Id); 414 goto out; 415 } 416 417 /* if there's more than one interrupt, we are confused */ 418 if (crsres->Data.Irq.NumberOfInterrupts > 1) { 419 device_printf(sc->ap_dev, "device has too many interrupts (%d)\n", crsres->Data.Irq.NumberOfInterrupts); 420 goto out; 421 } 422 423 /* 424 * If there's only one interrupt, and it's not zero, then we're already routed. 425 * 426 * Note that we could also check the 'decoding' bit in _STA, but can't depend on 427 * it since it's not part of the spec. 428 * 429 * XXX check ASL examples to see if this is an acceptable set of tests 430 */ 431 if ((crsres->Data.Irq.NumberOfInterrupts == 1) && (crsres->Data.Irq.Interrupts[0] != 0)) { 432 device_printf(sc->ap_dev, "device is routed to IRQ %d\n", crsres->Data.Irq.Interrupts[0]); 433 interrupt = crsres->Data.Irq.Interrupts[0]; 434 goto out; 435 } 436 437 /* 438 * There isn't an interrupt, so we have to look at _PRS to get one. 439 * Get the set of allowed interrupts from the _PRS resource indexed by SourceIndex. 440 */ 441 if (prsbuf.Pointer == NULL) { 442 device_printf(sc->ap_dev, "device has no routed interrupt and no _PRS on PCI interrupt link device\n"); 443 goto out; 444 } 445 if (ACPI_FAILURE(acpi_FindIndexedResource(&prsbuf, prt->SourceIndex, &prsres))) { 446 device_printf(sc->ap_dev, "_PRS buffer corrupt, cannot route interrupt\n"); 447 goto out; 448 } 449 450 /* type-check the resource we've got */ 451 if (prsres->Id != ACPI_RSTYPE_IRQ) { /* XXX ACPI_RSTYPE_EXT_IRQ */ 452 device_printf(sc->ap_dev, "_PRS resource entry has unsupported type %d\n", prsres->Id); 453 goto out; 454 } 455 456 /* there has to be at least one interrupt available */ 457 if (prsres->Data.Irq.NumberOfInterrupts < 1) { 458 device_printf(sc->ap_dev, "device has no interrupts\n"); 459 goto out; 460 } 461 462 /* 463 * Pick an interrupt to use. Note that a more scientific approach than just 464 * taking the first one available would be desirable. 465 * 466 * The PCI BIOS $PIR table offers "preferred PCI interrupts", but ACPI doesn't 467 * seem to offer a similar mechanism, so picking a "good" interrupt here is a 468 * difficult task. 469 * 470 * Build a resource buffer and pass it to AcpiSetCurrentResources to route the 471 * new interrupt. 472 */ 473 device_printf(sc->ap_dev, "possible interrupts:"); 474 for (i = 0; i < prsres->Data.Irq.NumberOfInterrupts; i++) 475 printf(" %d", prsres->Data.Irq.Interrupts[i]); 476 printf("\n"); 477 478 if (crsbuf.Pointer != NULL) /* should never happen */ 479 AcpiOsFree(crsbuf.Pointer); 480 crsbuf.Pointer = NULL; 481 resbuf.Id = ACPI_RSTYPE_IRQ; 482 resbuf.Length = SIZEOF_RESOURCE(ACPI_RESOURCE_IRQ); 483 resbuf.Data.Irq = prsres->Data.Irq; /* structure copy other fields */ 484 resbuf.Data.Irq.NumberOfInterrupts = 1; 485 resbuf.Data.Irq.Interrupts[0] = prsres->Data.Irq.Interrupts[0]; /* just take first... */ 486 if (ACPI_FAILURE(status = acpi_AppendBufferResource(&crsbuf, &resbuf))) { 487 device_printf(sc->ap_dev, "couldn't route interrupt %d via %s, interupt resource build failed - %s\n", 488 prsres->Data.Irq.Interrupts[0], acpi_name(lnkdev), AcpiFormatException(status)); 489 goto out; 490 } 491 if (ACPI_FAILURE(status = AcpiSetCurrentResources(lnkdev, &crsbuf))) { 492 device_printf(sc->ap_dev, "couldn't route interrupt %d via %s - %s\n", 493 prsres->Data.Irq.Interrupts[0], acpi_name(lnkdev), AcpiFormatException(status)); 494 goto out; 495 } 496 497 /* successful, return the interrupt we just routed */ 498 device_printf(sc->ap_dev, "routed interrupt %d via %s\n", 499 prsres->Data.Irq.Interrupts[0], acpi_name(lnkdev)); 500 interrupt = prsres->Data.Irq.Interrupts[0]; 501 502 out: 503 if (devlist != NULL) 504 free(devlist, M_TEMP); 505 if (crsbuf.Pointer != NULL) 506 AcpiOsFree(crsbuf.Pointer); 507 if (prsbuf.Pointer != NULL) 508 AcpiOsFree(prsbuf.Pointer); 509 510 /* XXX APIC_IO interrupt mapping? */ 511 return(interrupt); 512 } 513 514