1 /*- 2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org> 3 * Copyright (c) 2000, Michael Smith <msmith@freebsd.org> 4 * Copyright (c) 2000, BSDi 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_acpi.h" 33 #include "opt_iommu.h" 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/bus.h> 38 #include <sys/kernel.h> 39 #include <sys/malloc.h> 40 #include <sys/module.h> 41 #include <sys/sbuf.h> 42 #include <sys/taskqueue.h> 43 #include <sys/tree.h> 44 45 #include <contrib/dev/acpica/include/acpi.h> 46 #include <contrib/dev/acpica/include/accommon.h> 47 48 #include <dev/acpica/acpivar.h> 49 #include <dev/acpica/acpi_pcivar.h> 50 51 #include <sys/pciio.h> 52 #include <dev/pci/pcireg.h> 53 #include <dev/pci/pcivar.h> 54 #include <dev/pci/pci_private.h> 55 56 #include <dev/iommu/iommu.h> 57 58 #include "pcib_if.h" 59 #include "pci_if.h" 60 61 /* Hooks for the ACPI CA debugging infrastructure. */ 62 #define _COMPONENT ACPI_BUS 63 ACPI_MODULE_NAME("PCI") 64 65 struct acpi_pci_devinfo { 66 struct pci_devinfo ap_dinfo; 67 ACPI_HANDLE ap_handle; 68 int ap_flags; 69 }; 70 71 ACPI_SERIAL_DECL(pci_powerstate, "ACPI PCI power methods"); 72 73 /* Be sure that ACPI and PCI power states are equivalent. */ 74 CTASSERT(ACPI_STATE_D0 == PCI_POWERSTATE_D0); 75 CTASSERT(ACPI_STATE_D1 == PCI_POWERSTATE_D1); 76 CTASSERT(ACPI_STATE_D2 == PCI_POWERSTATE_D2); 77 CTASSERT(ACPI_STATE_D3 == PCI_POWERSTATE_D3); 78 79 static struct pci_devinfo *acpi_pci_alloc_devinfo(device_t dev); 80 static int acpi_pci_attach(device_t dev); 81 static void acpi_pci_child_deleted(device_t dev, device_t child); 82 static int acpi_pci_child_location_method(device_t cbdev, 83 device_t child, struct sbuf *sb); 84 static int acpi_pci_detach(device_t dev); 85 static int acpi_pci_probe(device_t dev); 86 static int acpi_pci_read_ivar(device_t dev, device_t child, int which, 87 uintptr_t *result); 88 static int acpi_pci_write_ivar(device_t dev, device_t child, int which, 89 uintptr_t value); 90 static ACPI_STATUS acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, 91 void *context, void **status); 92 static int acpi_pci_set_powerstate_method(device_t dev, device_t child, 93 int state); 94 static void acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child); 95 static bus_dma_tag_t acpi_pci_get_dma_tag(device_t bus, device_t child); 96 97 static device_method_t acpi_pci_methods[] = { 98 /* Device interface */ 99 DEVMETHOD(device_probe, acpi_pci_probe), 100 DEVMETHOD(device_attach, acpi_pci_attach), 101 DEVMETHOD(device_detach, acpi_pci_detach), 102 103 /* Bus interface */ 104 DEVMETHOD(bus_read_ivar, acpi_pci_read_ivar), 105 DEVMETHOD(bus_write_ivar, acpi_pci_write_ivar), 106 DEVMETHOD(bus_child_deleted, acpi_pci_child_deleted), 107 DEVMETHOD(bus_child_location, acpi_pci_child_location_method), 108 DEVMETHOD(bus_get_cpus, acpi_get_cpus), 109 DEVMETHOD(bus_get_dma_tag, acpi_pci_get_dma_tag), 110 DEVMETHOD(bus_get_domain, acpi_get_domain), 111 112 /* PCI interface */ 113 DEVMETHOD(pci_alloc_devinfo, acpi_pci_alloc_devinfo), 114 DEVMETHOD(pci_child_added, acpi_pci_child_added), 115 DEVMETHOD(pci_set_powerstate, acpi_pci_set_powerstate_method), 116 117 DEVMETHOD_END 118 }; 119 120 static devclass_t pci_devclass; 121 122 DEFINE_CLASS_1(pci, acpi_pci_driver, acpi_pci_methods, sizeof(struct pci_softc), 123 pci_driver); 124 DRIVER_MODULE(acpi_pci, pcib, acpi_pci_driver, pci_devclass, 0, 0); 125 MODULE_DEPEND(acpi_pci, acpi, 1, 1, 1); 126 MODULE_DEPEND(acpi_pci, pci, 1, 1, 1); 127 MODULE_VERSION(acpi_pci, 1); 128 129 static struct pci_devinfo * 130 acpi_pci_alloc_devinfo(device_t dev) 131 { 132 struct acpi_pci_devinfo *dinfo; 133 134 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO); 135 return (&dinfo->ap_dinfo); 136 } 137 138 static int 139 acpi_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 140 { 141 struct acpi_pci_devinfo *dinfo; 142 143 dinfo = device_get_ivars(child); 144 switch (which) { 145 case ACPI_IVAR_HANDLE: 146 *result = (uintptr_t)dinfo->ap_handle; 147 return (0); 148 case ACPI_IVAR_FLAGS: 149 *result = (uintptr_t)dinfo->ap_flags; 150 return (0); 151 } 152 return (pci_read_ivar(dev, child, which, result)); 153 } 154 155 static int 156 acpi_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 157 { 158 struct acpi_pci_devinfo *dinfo; 159 160 dinfo = device_get_ivars(child); 161 switch (which) { 162 case ACPI_IVAR_HANDLE: 163 dinfo->ap_handle = (ACPI_HANDLE)value; 164 return (0); 165 case ACPI_IVAR_FLAGS: 166 dinfo->ap_flags = (int)value; 167 return (0); 168 } 169 return (pci_write_ivar(dev, child, which, value)); 170 } 171 172 static void 173 acpi_pci_child_deleted(device_t dev, device_t child) 174 { 175 struct acpi_pci_devinfo *dinfo = device_get_ivars(child); 176 177 if (acpi_get_device(dinfo->ap_handle) == child) 178 AcpiDetachData(dinfo->ap_handle, acpi_fake_objhandler); 179 pci_child_deleted(dev, child); 180 } 181 182 static int 183 acpi_pci_child_location_method(device_t cbdev, device_t child, struct sbuf *sb) 184 { 185 struct acpi_pci_devinfo *dinfo = device_get_ivars(child); 186 int pxm; 187 188 pci_child_location_method(cbdev, child, sb); 189 190 if (dinfo->ap_handle) { 191 sbuf_printf(sb, " handle=%s", acpi_name(dinfo->ap_handle)); 192 if (ACPI_SUCCESS(acpi_GetInteger(dinfo->ap_handle, "_PXM", &pxm))) { 193 sbuf_printf(sb, " _PXM=%d", pxm); 194 } 195 } 196 return (0); 197 } 198 199 /* 200 * PCI power manangement 201 */ 202 static int 203 acpi_pci_set_powerstate_method(device_t dev, device_t child, int state) 204 { 205 ACPI_HANDLE h; 206 ACPI_STATUS status; 207 int old_state, error; 208 209 error = 0; 210 if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3) 211 return (EINVAL); 212 213 /* 214 * We set the state using PCI Power Management outside of setting 215 * the ACPI state. This means that when powering down a device, we 216 * first shut it down using PCI, and then using ACPI, which lets ACPI 217 * try to power down any Power Resources that are now no longer used. 218 * When powering up a device, we let ACPI set the state first so that 219 * it can enable any needed Power Resources before changing the PCI 220 * power state. 221 */ 222 ACPI_SERIAL_BEGIN(pci_powerstate); 223 old_state = pci_get_powerstate(child); 224 if (old_state < state && pci_do_power_suspend) { 225 error = pci_set_powerstate_method(dev, child, state); 226 if (error) 227 goto out; 228 } 229 h = acpi_get_handle(child); 230 status = acpi_pwr_switch_consumer(h, state); 231 if (ACPI_SUCCESS(status)) { 232 if (bootverbose) 233 device_printf(dev, "set ACPI power state D%d on %s\n", 234 state, acpi_name(h)); 235 } else if (status != AE_NOT_FOUND) 236 device_printf(dev, 237 "failed to set ACPI power state D%d on %s: %s\n", 238 state, acpi_name(h), AcpiFormatException(status)); 239 if (old_state > state && pci_do_power_resume) 240 error = pci_set_powerstate_method(dev, child, state); 241 242 out: 243 ACPI_SERIAL_END(pci_powerstate); 244 return (error); 245 } 246 247 static void 248 acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child) 249 { 250 ACPI_STATUS status; 251 device_t child; 252 253 /* 254 * Occasionally a PCI device may show up as an ACPI device 255 * with a _HID. (For example, the TabletPC TC1000 has a 256 * second PCI-ISA bridge that has a _HID for an 257 * acpi_sysresource device.) In that case, leave ACPI-CA's 258 * device data pointing at the ACPI-enumerated device. 259 */ 260 child = acpi_get_device(handle); 261 if (child != NULL) { 262 KASSERT(device_get_parent(child) == 263 devclass_get_device(devclass_find("acpi"), 0), 264 ("%s: child (%s)'s parent is not acpi0", __func__, 265 acpi_name(handle))); 266 return; 267 } 268 269 /* 270 * Update ACPI-CA to use the PCI enumerated device_t for this handle. 271 */ 272 status = AcpiAttachData(handle, acpi_fake_objhandler, pci_child); 273 if (ACPI_FAILURE(status)) 274 printf("WARNING: Unable to attach object data to %s - %s\n", 275 acpi_name(handle), AcpiFormatException(status)); 276 } 277 278 static ACPI_STATUS 279 acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, void *context, 280 void **status) 281 { 282 struct acpi_pci_devinfo *dinfo; 283 device_t child; 284 int func, slot; 285 UINT32 address; 286 287 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 288 289 child = context; 290 if (ACPI_FAILURE(acpi_GetInteger(handle, "_ADR", &address))) 291 return_ACPI_STATUS (AE_OK); 292 slot = ACPI_ADR_PCI_SLOT(address); 293 func = ACPI_ADR_PCI_FUNC(address); 294 dinfo = device_get_ivars(child); 295 if (dinfo->ap_dinfo.cfg.func == func && 296 dinfo->ap_dinfo.cfg.slot == slot) { 297 dinfo->ap_handle = handle; 298 acpi_pci_update_device(handle, child); 299 return_ACPI_STATUS (AE_CTRL_TERMINATE); 300 } 301 return_ACPI_STATUS (AE_OK); 302 } 303 304 void 305 acpi_pci_child_added(device_t dev, device_t child) 306 { 307 308 /* 309 * PCI devices are added via the bus scan in the normal PCI 310 * bus driver. As each device is added, the 311 * acpi_pci_child_added() callback walks the ACPI namespace 312 * under the bridge driver to save ACPI handles to all the 313 * devices that appear in the ACPI namespace as immediate 314 * descendants of the bridge. 315 * 316 * XXX: Sometimes PCI devices show up in the ACPI namespace that 317 * pci_add_children() doesn't find. We currently just ignore 318 * these devices. 319 */ 320 AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1, 321 acpi_pci_save_handle, NULL, child, NULL); 322 } 323 324 static int 325 acpi_pci_probe(device_t dev) 326 { 327 328 if (acpi_get_handle(dev) == NULL) 329 return (ENXIO); 330 device_set_desc(dev, "ACPI PCI bus"); 331 return (BUS_PROBE_DEFAULT); 332 } 333 334 static void 335 acpi_pci_bus_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context) 336 { 337 device_t dev; 338 339 dev = context; 340 341 switch (notify) { 342 case ACPI_NOTIFY_BUS_CHECK: 343 bus_topo_lock(); 344 BUS_RESCAN(dev); 345 bus_topo_unlock(); 346 break; 347 default: 348 device_printf(dev, "unknown notify %#x\n", notify); 349 break; 350 } 351 } 352 353 static void 354 acpi_pci_device_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context) 355 { 356 device_t child, dev; 357 ACPI_STATUS status; 358 int error; 359 360 dev = context; 361 362 switch (notify) { 363 case ACPI_NOTIFY_DEVICE_CHECK: 364 bus_topo_lock(); 365 BUS_RESCAN(dev); 366 bus_topo_unlock(); 367 break; 368 case ACPI_NOTIFY_EJECT_REQUEST: 369 child = acpi_get_device(h); 370 if (child == NULL) { 371 device_printf(dev, "no device to eject for %s\n", 372 acpi_name(h)); 373 return; 374 } 375 bus_topo_lock(); 376 error = device_detach(child); 377 if (error) { 378 bus_topo_unlock(); 379 device_printf(dev, "failed to detach %s: %d\n", 380 device_get_nameunit(child), error); 381 return; 382 } 383 status = acpi_SetInteger(h, "_EJ0", 1); 384 if (ACPI_FAILURE(status)) { 385 bus_topo_unlock(); 386 device_printf(dev, "failed to eject %s: %s\n", 387 acpi_name(h), AcpiFormatException(status)); 388 return; 389 } 390 BUS_RESCAN(dev); 391 bus_topo_unlock(); 392 break; 393 default: 394 device_printf(dev, "unknown notify %#x for %s\n", notify, 395 acpi_name(h)); 396 break; 397 } 398 } 399 400 static ACPI_STATUS 401 acpi_pci_install_device_notify_handler(ACPI_HANDLE handle, UINT32 level, 402 void *context, void **status) 403 { 404 ACPI_HANDLE h; 405 406 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 407 408 if (ACPI_FAILURE(AcpiGetHandle(handle, "_EJ0", &h))) 409 return_ACPI_STATUS (AE_OK); 410 411 AcpiInstallNotifyHandler(handle, ACPI_SYSTEM_NOTIFY, 412 acpi_pci_device_notify_handler, context); 413 return_ACPI_STATUS (AE_OK); 414 } 415 416 static int 417 acpi_pci_attach(device_t dev) 418 { 419 int error; 420 421 error = pci_attach(dev); 422 if (error) 423 return (error); 424 AcpiInstallNotifyHandler(acpi_get_handle(dev), ACPI_SYSTEM_NOTIFY, 425 acpi_pci_bus_notify_handler, dev); 426 AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1, 427 acpi_pci_install_device_notify_handler, NULL, dev, NULL); 428 429 return (0); 430 } 431 432 static ACPI_STATUS 433 acpi_pci_remove_notify_handler(ACPI_HANDLE handle, UINT32 level, void *context, 434 void **status) 435 { 436 ACPI_HANDLE h; 437 438 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 439 440 if (ACPI_FAILURE(AcpiGetHandle(handle, "_EJ0", &h))) 441 return_ACPI_STATUS (AE_OK); 442 443 AcpiRemoveNotifyHandler(handle, ACPI_SYSTEM_NOTIFY, 444 acpi_pci_device_notify_handler); 445 return_ACPI_STATUS (AE_OK); 446 } 447 448 static int 449 acpi_pci_detach(device_t dev) 450 { 451 452 AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1, 453 acpi_pci_remove_notify_handler, NULL, dev, NULL); 454 AcpiRemoveNotifyHandler(acpi_get_handle(dev), ACPI_SYSTEM_NOTIFY, 455 acpi_pci_bus_notify_handler); 456 return (pci_detach(dev)); 457 } 458 459 #ifdef IOMMU 460 static bus_dma_tag_t 461 acpi_pci_get_dma_tag(device_t bus, device_t child) 462 { 463 bus_dma_tag_t tag; 464 465 if (device_get_parent(child) == bus) { 466 /* try iommu and return if it works */ 467 tag = iommu_get_dma_tag(bus, child); 468 } else 469 tag = NULL; 470 if (tag == NULL) 471 tag = pci_get_dma_tag(bus, child); 472 return (tag); 473 } 474 #else 475 static bus_dma_tag_t 476 acpi_pci_get_dma_tag(device_t bus, device_t child) 477 { 478 479 return (pci_get_dma_tag(bus, child)); 480 } 481 #endif 482