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/kernel.h> 35 #include <sys/limits.h> 36 #include <sys/malloc.h> 37 #include <sys/module.h> 38 #include <sys/rman.h> 39 #include <sys/sysctl.h> 40 41 #include <contrib/dev/acpica/include/acpi.h> 42 #include <contrib/dev/acpica/include/accommon.h> 43 44 #include <dev/acpica/acpivar.h> 45 46 #include <machine/pci_cfgreg.h> 47 #include <dev/pci/pcivar.h> 48 #include <dev/pci/pcib_private.h> 49 #include "pcib_if.h" 50 51 #include <dev/acpica/acpi_pcibvar.h> 52 53 /* Hooks for the ACPI CA debugging infrastructure. */ 54 #define _COMPONENT ACPI_BUS 55 ACPI_MODULE_NAME("PCI_ACPI") 56 57 struct acpi_hpcib_softc { 58 device_t ap_dev; 59 ACPI_HANDLE ap_handle; 60 int ap_flags; 61 62 int ap_segment; /* analagous to Alpha 'hose' */ 63 int ap_bus; /* bios-assigned bus number */ 64 65 ACPI_BUFFER ap_prt; /* interrupt routing table */ 66 #ifdef NEW_PCIB 67 struct pcib_host_resources ap_host_res; 68 #endif 69 }; 70 71 static int acpi_pcib_acpi_probe(device_t bus); 72 static int acpi_pcib_acpi_attach(device_t bus); 73 static int acpi_pcib_read_ivar(device_t dev, device_t child, 74 int which, uintptr_t *result); 75 static int acpi_pcib_write_ivar(device_t dev, device_t child, 76 int which, uintptr_t value); 77 static uint32_t acpi_pcib_read_config(device_t dev, u_int bus, 78 u_int slot, u_int func, u_int reg, int bytes); 79 static void acpi_pcib_write_config(device_t dev, u_int bus, 80 u_int slot, u_int func, u_int reg, uint32_t data, 81 int bytes); 82 static int acpi_pcib_acpi_route_interrupt(device_t pcib, 83 device_t dev, int pin); 84 static int acpi_pcib_alloc_msi(device_t pcib, device_t dev, 85 int count, int maxcount, int *irqs); 86 static int acpi_pcib_map_msi(device_t pcib, device_t dev, 87 int irq, uint64_t *addr, uint32_t *data); 88 static int acpi_pcib_alloc_msix(device_t pcib, device_t dev, 89 int *irq); 90 static struct resource *acpi_pcib_acpi_alloc_resource(device_t dev, 91 device_t child, int type, int *rid, 92 u_long start, u_long end, u_long count, 93 u_int flags); 94 #ifdef NEW_PCIB 95 static int acpi_pcib_acpi_adjust_resource(device_t dev, 96 device_t child, int type, struct resource *r, 97 u_long start, u_long end); 98 #endif 99 100 static device_method_t acpi_pcib_acpi_methods[] = { 101 /* Device interface */ 102 DEVMETHOD(device_probe, acpi_pcib_acpi_probe), 103 DEVMETHOD(device_attach, acpi_pcib_acpi_attach), 104 DEVMETHOD(device_shutdown, bus_generic_shutdown), 105 DEVMETHOD(device_suspend, bus_generic_suspend), 106 DEVMETHOD(device_resume, bus_generic_resume), 107 108 /* Bus interface */ 109 DEVMETHOD(bus_read_ivar, acpi_pcib_read_ivar), 110 DEVMETHOD(bus_write_ivar, acpi_pcib_write_ivar), 111 DEVMETHOD(bus_alloc_resource, acpi_pcib_acpi_alloc_resource), 112 #ifdef NEW_PCIB 113 DEVMETHOD(bus_adjust_resource, acpi_pcib_acpi_adjust_resource), 114 #else 115 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource), 116 #endif 117 DEVMETHOD(bus_release_resource, bus_generic_release_resource), 118 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 119 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 120 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 121 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 122 123 /* pcib interface */ 124 DEVMETHOD(pcib_maxslots, pcib_maxslots), 125 DEVMETHOD(pcib_read_config, acpi_pcib_read_config), 126 DEVMETHOD(pcib_write_config, acpi_pcib_write_config), 127 DEVMETHOD(pcib_route_interrupt, acpi_pcib_acpi_route_interrupt), 128 DEVMETHOD(pcib_alloc_msi, acpi_pcib_alloc_msi), 129 DEVMETHOD(pcib_release_msi, pcib_release_msi), 130 DEVMETHOD(pcib_alloc_msix, acpi_pcib_alloc_msix), 131 DEVMETHOD(pcib_release_msix, pcib_release_msix), 132 DEVMETHOD(pcib_map_msi, acpi_pcib_map_msi), 133 DEVMETHOD(pcib_power_for_sleep, acpi_pcib_power_for_sleep), 134 135 DEVMETHOD_END 136 }; 137 138 static devclass_t pcib_devclass; 139 140 DEFINE_CLASS_0(pcib, acpi_pcib_acpi_driver, acpi_pcib_acpi_methods, 141 sizeof(struct acpi_hpcib_softc)); 142 DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_acpi_driver, pcib_devclass, 0, 0); 143 MODULE_DEPEND(acpi_pcib, acpi, 1, 1, 1); 144 145 static int 146 acpi_pcib_acpi_probe(device_t dev) 147 { 148 ACPI_DEVICE_INFO *devinfo; 149 ACPI_HANDLE h; 150 int root; 151 152 if (acpi_disabled("pcib") || (h = acpi_get_handle(dev)) == NULL || 153 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 154 return (ENXIO); 155 root = (devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0; 156 AcpiOsFree(devinfo); 157 if (!root || pci_cfgregopen() == 0) 158 return (ENXIO); 159 160 device_set_desc(dev, "ACPI Host-PCI bridge"); 161 return (0); 162 } 163 164 #ifdef NEW_PCIB 165 static ACPI_STATUS 166 acpi_pcib_producer_handler(ACPI_RESOURCE *res, void *context) 167 { 168 struct acpi_hpcib_softc *sc; 169 UINT64 length, min, max; 170 u_int flags; 171 int error, type; 172 173 sc = context; 174 switch (res->Type) { 175 case ACPI_RESOURCE_TYPE_START_DEPENDENT: 176 case ACPI_RESOURCE_TYPE_END_DEPENDENT: 177 panic("host bridge has depenedent resources"); 178 case ACPI_RESOURCE_TYPE_ADDRESS16: 179 case ACPI_RESOURCE_TYPE_ADDRESS32: 180 case ACPI_RESOURCE_TYPE_ADDRESS64: 181 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64: 182 if (res->Data.Address.ProducerConsumer != ACPI_PRODUCER) 183 break; 184 switch (res->Type) { 185 case ACPI_RESOURCE_TYPE_ADDRESS16: 186 min = res->Data.Address16.Minimum; 187 max = res->Data.Address16.Maximum; 188 length = res->Data.Address16.AddressLength; 189 break; 190 case ACPI_RESOURCE_TYPE_ADDRESS32: 191 min = res->Data.Address32.Minimum; 192 max = res->Data.Address32.Maximum; 193 length = res->Data.Address32.AddressLength; 194 break; 195 case ACPI_RESOURCE_TYPE_ADDRESS64: 196 min = res->Data.Address64.Minimum; 197 max = res->Data.Address64.Maximum; 198 length = res->Data.Address64.AddressLength; 199 break; 200 default: 201 KASSERT(res->Type == 202 ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64, 203 ("should never happen")); 204 min = res->Data.ExtAddress64.Minimum; 205 max = res->Data.ExtAddress64.Maximum; 206 length = res->Data.ExtAddress64.AddressLength; 207 break; 208 } 209 if (length == 0) 210 break; 211 if (min + length - 1 != max && 212 (res->Data.Address.MinAddressFixed != ACPI_ADDRESS_FIXED || 213 res->Data.Address.MaxAddressFixed != ACPI_ADDRESS_FIXED)) 214 break; 215 flags = 0; 216 switch (res->Data.Address.ResourceType) { 217 case ACPI_MEMORY_RANGE: 218 type = SYS_RES_MEMORY; 219 if (res->Type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64) { 220 if (res->Data.Address.Info.Mem.Caching == 221 ACPI_PREFETCHABLE_MEMORY) 222 flags |= RF_PREFETCHABLE; 223 } else { 224 /* 225 * XXX: Parse prefetch flag out of 226 * TypeSpecific. 227 */ 228 } 229 break; 230 case ACPI_IO_RANGE: 231 type = SYS_RES_IOPORT; 232 break; 233 #ifdef PCI_RES_BUS 234 case ACPI_BUS_NUMBER_RANGE: 235 type = PCI_RES_BUS; 236 break; 237 #endif 238 default: 239 return (AE_OK); 240 } 241 242 if (min + length - 1 != max) 243 device_printf(sc->ap_dev, 244 "Length mismatch for %d range: %jx vs %jx\n", type, 245 (uintmax_t)max - min + 1, (uintmax_t)length); 246 #ifdef __i386__ 247 if (min > ULONG_MAX) { 248 device_printf(sc->ap_dev, 249 "Ignoring %d range above 4GB (%#jx-%#jx)\n", 250 type, (uintmax_t)min, (uintmax_t)max); 251 break; 252 } 253 if (max > ULONG_MAX) { 254 device_printf(sc->ap_dev, 255 "Truncating end of %d range above 4GB (%#jx-%#jx)\n", 256 type, (uintmax_t)min, (uintmax_t)max); 257 max = ULONG_MAX; 258 } 259 #endif 260 error = pcib_host_res_decodes(&sc->ap_host_res, type, min, max, 261 flags); 262 if (error) 263 panic("Failed to manage %d range (%#jx-%#jx): %d", 264 type, (uintmax_t)min, (uintmax_t)max, error); 265 break; 266 default: 267 break; 268 } 269 return (AE_OK); 270 } 271 #endif 272 273 static int 274 acpi_pcib_acpi_attach(device_t dev) 275 { 276 struct acpi_hpcib_softc *sc; 277 ACPI_STATUS status; 278 static int bus0_seen = 0; 279 u_int addr, slot, func, busok; 280 uint8_t busno; 281 282 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 283 284 sc = device_get_softc(dev); 285 sc->ap_dev = dev; 286 sc->ap_handle = acpi_get_handle(dev); 287 288 /* 289 * Get our segment number by evaluating _SEG 290 * It's OK for this to not exist. 291 */ 292 status = acpi_GetInteger(sc->ap_handle, "_SEG", &sc->ap_segment); 293 if (ACPI_FAILURE(status)) { 294 if (status != AE_NOT_FOUND) { 295 device_printf(dev, "could not evaluate _SEG - %s\n", 296 AcpiFormatException(status)); 297 return_VALUE (ENXIO); 298 } 299 /* If it's not found, assume 0. */ 300 sc->ap_segment = 0; 301 } 302 303 #ifdef NEW_PCIB 304 /* 305 * Determine which address ranges this bridge decodes and setup 306 * resource managers for those ranges. 307 */ 308 if (pcib_host_res_init(sc->ap_dev, &sc->ap_host_res) != 0) 309 panic("failed to init hostb resources"); 310 if (!acpi_disabled("hostres")) { 311 status = AcpiWalkResources(sc->ap_handle, "_CRS", 312 acpi_pcib_producer_handler, sc); 313 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) 314 device_printf(sc->ap_dev, "failed to parse resources: %s\n", 315 AcpiFormatException(status)); 316 } 317 #endif 318 319 /* 320 * Get our base bus number by evaluating _BBN. 321 * If this doesn't work, we assume we're bus number 0. 322 * 323 * XXX note that it may also not exist in the case where we are 324 * meant to use a private configuration space mechanism for this bus, 325 * so we should dig out our resources and check to see if we have 326 * anything like that. How do we do this? 327 * XXX If we have the requisite information, and if we don't think the 328 * default PCI configuration space handlers can deal with this bus, 329 * we should attach our own handler. 330 * XXX invoke _REG on this for the PCI config space address space? 331 * XXX It seems many BIOS's with multiple Host-PCI bridges do not set 332 * _BBN correctly. They set _BBN to zero for all bridges. Thus, 333 * if _BBN is zero and PCI bus 0 already exists, we try to read our 334 * bus number from the configuration registers at address _ADR. 335 * We only do this for domain/segment 0 in the hopes that this is 336 * only needed for old single-domain machines. 337 */ 338 status = acpi_GetInteger(sc->ap_handle, "_BBN", &sc->ap_bus); 339 if (ACPI_FAILURE(status)) { 340 if (status != AE_NOT_FOUND) { 341 device_printf(dev, "could not evaluate _BBN - %s\n", 342 AcpiFormatException(status)); 343 return_VALUE (ENXIO); 344 } else { 345 /* If it's not found, assume 0. */ 346 sc->ap_bus = 0; 347 } 348 } 349 350 /* 351 * If this is segment 0, the bus is zero, and PCI bus 0 already 352 * exists, read the bus number via PCI config space. 353 */ 354 busok = 1; 355 if (sc->ap_segment == 0 && sc->ap_bus == 0 && bus0_seen) { 356 busok = 0; 357 status = acpi_GetInteger(sc->ap_handle, "_ADR", &addr); 358 if (ACPI_FAILURE(status)) { 359 if (status != AE_NOT_FOUND) { 360 device_printf(dev, "could not evaluate _ADR - %s\n", 361 AcpiFormatException(status)); 362 return_VALUE (ENXIO); 363 } else 364 device_printf(dev, "couldn't find _ADR\n"); 365 } else { 366 /* XXX: We assume bus 0. */ 367 slot = ACPI_ADR_PCI_SLOT(addr); 368 func = ACPI_ADR_PCI_FUNC(addr); 369 if (bootverbose) 370 device_printf(dev, "reading config registers from 0:%d:%d\n", 371 slot, func); 372 if (host_pcib_get_busno(pci_cfgregread, 0, slot, func, &busno) == 0) 373 device_printf(dev, "couldn't read bus number from cfg space\n"); 374 else { 375 sc->ap_bus = busno; 376 busok = 1; 377 } 378 } 379 } 380 381 /* 382 * If nothing else worked, hope that ACPI at least lays out the 383 * host-PCI bridges in order and that as a result our unit number 384 * is actually our bus number. There are several reasons this 385 * might not be true. 386 */ 387 if (busok == 0) { 388 sc->ap_bus = device_get_unit(dev); 389 device_printf(dev, "trying bus number %d\n", sc->ap_bus); 390 } 391 392 /* If this is bus 0 on segment 0, note that it has been seen already. */ 393 if (sc->ap_segment == 0 && sc->ap_bus == 0) 394 bus0_seen = 1; 395 396 return (acpi_pcib_attach(dev, &sc->ap_prt, sc->ap_bus)); 397 } 398 399 /* 400 * Support for standard PCI bridge ivars. 401 */ 402 static int 403 acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 404 { 405 struct acpi_hpcib_softc *sc = device_get_softc(dev); 406 407 switch (which) { 408 case PCIB_IVAR_DOMAIN: 409 *result = sc->ap_segment; 410 return (0); 411 case PCIB_IVAR_BUS: 412 *result = sc->ap_bus; 413 return (0); 414 case ACPI_IVAR_HANDLE: 415 *result = (uintptr_t)sc->ap_handle; 416 return (0); 417 case ACPI_IVAR_FLAGS: 418 *result = (uintptr_t)sc->ap_flags; 419 return (0); 420 } 421 return (ENOENT); 422 } 423 424 static int 425 acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 426 { 427 struct acpi_hpcib_softc *sc = device_get_softc(dev); 428 429 switch (which) { 430 case PCIB_IVAR_DOMAIN: 431 return (EINVAL); 432 case PCIB_IVAR_BUS: 433 sc->ap_bus = value; 434 return (0); 435 case ACPI_IVAR_HANDLE: 436 sc->ap_handle = (ACPI_HANDLE)value; 437 return (0); 438 case ACPI_IVAR_FLAGS: 439 sc->ap_flags = (int)value; 440 return (0); 441 } 442 return (ENOENT); 443 } 444 445 static uint32_t 446 acpi_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func, 447 u_int reg, int bytes) 448 { 449 return (pci_cfgregread(bus, slot, func, reg, bytes)); 450 } 451 452 static void 453 acpi_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func, 454 u_int reg, uint32_t data, int bytes) 455 { 456 pci_cfgregwrite(bus, slot, func, reg, data, bytes); 457 } 458 459 static int 460 acpi_pcib_acpi_route_interrupt(device_t pcib, device_t dev, int pin) 461 { 462 struct acpi_hpcib_softc *sc = device_get_softc(pcib); 463 464 return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt)); 465 } 466 467 static int 468 acpi_pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, 469 int *irqs) 470 { 471 device_t bus; 472 473 bus = device_get_parent(pcib); 474 return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount, 475 irqs)); 476 } 477 478 static int 479 acpi_pcib_alloc_msix(device_t pcib, device_t dev, int *irq) 480 { 481 device_t bus; 482 483 bus = device_get_parent(pcib); 484 return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq)); 485 } 486 487 static int 488 acpi_pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, 489 uint32_t *data) 490 { 491 device_t bus; 492 493 bus = device_get_parent(pcib); 494 return (PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data)); 495 } 496 497 struct resource * 498 acpi_pcib_acpi_alloc_resource(device_t dev, device_t child, int type, int *rid, 499 u_long start, u_long end, u_long count, u_int flags) 500 { 501 #ifdef NEW_PCIB 502 struct acpi_hpcib_softc *sc; 503 struct resource *res; 504 #endif 505 506 #if defined(__i386__) || defined(__amd64__) 507 start = hostb_alloc_start(type, start, end, count); 508 #endif 509 510 #ifdef NEW_PCIB 511 sc = device_get_softc(dev); 512 res = pcib_host_res_alloc(&sc->ap_host_res, child, type, rid, start, end, 513 count, flags); 514 515 /* 516 * XXX: If this is a request for a specific range, assume it is 517 * correct and pass it up to the parent. What we probably want to 518 * do long-term is explicitly trust any firmware-configured 519 * resources during the initial bus scan on boot and then disable 520 * this after that. 521 */ 522 if (res == NULL && start + count - 1 == end) 523 res = bus_generic_alloc_resource(dev, child, type, rid, start, end, 524 count, flags); 525 return (res); 526 #else 527 return (bus_generic_alloc_resource(dev, child, type, rid, start, end, 528 count, flags)); 529 #endif 530 } 531 532 #ifdef NEW_PCIB 533 int 534 acpi_pcib_acpi_adjust_resource(device_t dev, device_t child, int type, 535 struct resource *r, u_long start, u_long end) 536 { 537 struct acpi_hpcib_softc *sc; 538 539 sc = device_get_softc(dev); 540 return (pcib_host_res_adjust(&sc->ap_host_res, child, type, r, start, 541 end)); 542 } 543 #endif 544