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 34#include <sys/param.h> 35#include <sys/systm.h> 36#include <sys/bus.h> 37#include <sys/kernel.h> 38#include <sys/malloc.h> 39#include <sys/module.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#include <dev/acpica/acpi_pcivar.h> 46 47#include <sys/pciio.h> 48#include <dev/pci/pcireg.h> 49#include <dev/pci/pcivar.h> 50#include <dev/pci/pci_private.h> 51 52#include "pcib_if.h" 53#include "pci_if.h" 54 55/* Hooks for the ACPI CA debugging infrastructure. */ 56#define _COMPONENT ACPI_BUS 57ACPI_MODULE_NAME("PCI") 58 59struct acpi_pci_devinfo { 60 struct pci_devinfo ap_dinfo; 61 ACPI_HANDLE ap_handle; 62 int ap_flags; 63}; 64 65ACPI_SERIAL_DECL(pci_powerstate, "ACPI PCI power methods"); 66 67/* Be sure that ACPI and PCI power states are equivalent. */ 68CTASSERT(ACPI_STATE_D0 == PCI_POWERSTATE_D0); 69CTASSERT(ACPI_STATE_D1 == PCI_POWERSTATE_D1); 70CTASSERT(ACPI_STATE_D2 == PCI_POWERSTATE_D2); 71CTASSERT(ACPI_STATE_D3 == PCI_POWERSTATE_D3); 72 73static struct pci_devinfo *acpi_pci_alloc_devinfo(device_t dev); 74static int acpi_pci_attach(device_t dev); 75static void acpi_pci_child_deleted(device_t dev, device_t child); 76static int acpi_pci_child_location_str_method(device_t cbdev, 77 device_t child, char *buf, size_t buflen); 78static int acpi_pci_detach(device_t dev); 79static int acpi_pci_probe(device_t dev); 80static int acpi_pci_read_ivar(device_t dev, device_t child, int which, 81 uintptr_t *result); 82static int acpi_pci_write_ivar(device_t dev, device_t child, int which, 83 uintptr_t value); 84static ACPI_STATUS acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, 85 void *context, void **status); 86static int acpi_pci_set_powerstate_method(device_t dev, device_t child, 87 int state); 88static void acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child); 89static bus_dma_tag_t acpi_pci_get_dma_tag(device_t bus, device_t child); 90 91static device_method_t acpi_pci_methods[] = { 92 /* Device interface */ 93 DEVMETHOD(device_probe, acpi_pci_probe), 94 DEVMETHOD(device_attach, acpi_pci_attach), 95 DEVMETHOD(device_detach, acpi_pci_detach), 96 97 /* Bus interface */ 98 DEVMETHOD(bus_read_ivar, acpi_pci_read_ivar), 99 DEVMETHOD(bus_write_ivar, acpi_pci_write_ivar), 100 DEVMETHOD(bus_child_deleted, acpi_pci_child_deleted), 101 DEVMETHOD(bus_child_location_str, acpi_pci_child_location_str_method), 102 DEVMETHOD(bus_get_cpus, acpi_get_cpus), 103 DEVMETHOD(bus_get_dma_tag, acpi_pci_get_dma_tag), 104 DEVMETHOD(bus_get_domain, acpi_get_domain), 105 106 /* PCI interface */ 107 DEVMETHOD(pci_alloc_devinfo, acpi_pci_alloc_devinfo), 108 DEVMETHOD(pci_child_added, acpi_pci_child_added), 109 DEVMETHOD(pci_set_powerstate, acpi_pci_set_powerstate_method), 110 111 DEVMETHOD_END 112}; 113 114static devclass_t pci_devclass; 115 116DEFINE_CLASS_1(pci, acpi_pci_driver, acpi_pci_methods, sizeof(struct pci_softc), 117 pci_driver); 118DRIVER_MODULE(acpi_pci, pcib, acpi_pci_driver, pci_devclass, 0, 0); 119MODULE_DEPEND(acpi_pci, acpi, 1, 1, 1); 120MODULE_DEPEND(acpi_pci, pci, 1, 1, 1); 121MODULE_VERSION(acpi_pci, 1); 122 123static struct pci_devinfo * 124acpi_pci_alloc_devinfo(device_t dev) 125{ 126 struct acpi_pci_devinfo *dinfo; 127 128 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO); 129 return (&dinfo->ap_dinfo); 130} 131 132static int 133acpi_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 134{ 135 struct acpi_pci_devinfo *dinfo; 136 137 dinfo = device_get_ivars(child); 138 switch (which) { 139 case ACPI_IVAR_HANDLE: 140 *result = (uintptr_t)dinfo->ap_handle; 141 return (0); 142 case ACPI_IVAR_FLAGS: 143 *result = (uintptr_t)dinfo->ap_flags; 144 return (0); 145 } 146 return (pci_read_ivar(dev, child, which, result)); 147} 148 149static int 150acpi_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 151{ 152 struct acpi_pci_devinfo *dinfo; 153 154 dinfo = device_get_ivars(child); 155 switch (which) { 156 case ACPI_IVAR_HANDLE: 157 dinfo->ap_handle = (ACPI_HANDLE)value; 158 return (0); 159 case ACPI_IVAR_FLAGS: 160 dinfo->ap_flags = (int)value; 161 return (0); 162 } 163 return (pci_write_ivar(dev, child, which, value)); 164} 165 166static void 167acpi_pci_child_deleted(device_t dev, device_t child) 168{ 169 struct acpi_pci_devinfo *dinfo = device_get_ivars(child); 170 171 if (acpi_get_device(dinfo->ap_handle) == child) 172 AcpiDetachData(dinfo->ap_handle, acpi_fake_objhandler); 173 pci_child_deleted(dev, child); 174} 175 176static int 177acpi_pci_child_location_str_method(device_t cbdev, device_t child, char *buf, 178 size_t buflen) 179{ 180 struct acpi_pci_devinfo *dinfo = device_get_ivars(child); 181 int pxm; 182 char buf2[32]; 183 184 pci_child_location_str_method(cbdev, child, buf, buflen); 185 186 if (dinfo->ap_handle) { 187 strlcat(buf, " handle=", buflen); 188 strlcat(buf, acpi_name(dinfo->ap_handle), buflen); 189 190 if (ACPI_SUCCESS(acpi_GetInteger(dinfo->ap_handle, "_PXM", &pxm))) { 191 snprintf(buf2, 32, " _PXM=%d", pxm); 192 strlcat(buf, buf2, buflen); 193 } 194 } 195 return (0); 196} 197 198/* 199 * PCI power manangement 200 */ 201static int 202acpi_pci_set_powerstate_method(device_t dev, device_t child, int state) 203{ 204 ACPI_HANDLE h; 205 ACPI_STATUS status; 206 int old_state, error; 207 208 error = 0; 209 if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3) 210 return (EINVAL); 211 212 /* 213 * We set the state using PCI Power Management outside of setting 214 * the ACPI state. This means that when powering down a device, we 215 * first shut it down using PCI, and then using ACPI, which lets ACPI 216 * try to power down any Power Resources that are now no longer used. 217 * When powering up a device, we let ACPI set the state first so that 218 * it can enable any needed Power Resources before changing the PCI 219 * power state. 220 */ 221 ACPI_SERIAL_BEGIN(pci_powerstate); 222 old_state = pci_get_powerstate(child); 223 if (old_state < state && pci_do_power_suspend) { 224 error = pci_set_powerstate_method(dev, child, state); 225 if (error) 226 goto out; 227 } 228 h = acpi_get_handle(child); 229 status = acpi_pwr_switch_consumer(h, state); 230 if (ACPI_SUCCESS(status)) { 231 if (bootverbose) 232 device_printf(dev, "set ACPI power state D%d on %s\n", 233 state, acpi_name(h)); 234 } else if (status != AE_NOT_FOUND) 235 device_printf(dev, 236 "failed to set ACPI power state D%d on %s: %s\n", 237 state, acpi_name(h), AcpiFormatException(status)); 238 if (old_state > state && pci_do_power_resume) 239 error = pci_set_powerstate_method(dev, child, state); 240 241out: 242 ACPI_SERIAL_END(pci_powerstate); 243 return (error); 244} 245 246static void 247acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child) 248{ 249 ACPI_STATUS status; 250 device_t child; 251 252 /* 253 * Occasionally a PCI device may show up as an ACPI device 254 * with a _HID. (For example, the TabletPC TC1000 has a 255 * second PCI-ISA bridge that has a _HID for an 256 * acpi_sysresource device.) In that case, leave ACPI-CA's 257 * device data pointing at the ACPI-enumerated device. 258 */ 259 child = acpi_get_device(handle); 260 if (child != NULL) { 261 KASSERT(device_get_parent(child) == 262 devclass_get_device(devclass_find("acpi"), 0), 263 ("%s: child (%s)'s parent is not acpi0", __func__, 264 acpi_name(handle))); 265 return; 266 } 267 268 /* 269 * Update ACPI-CA to use the PCI enumerated device_t for this handle. 270 */ 271 status = AcpiAttachData(handle, acpi_fake_objhandler, pci_child); 272 if (ACPI_FAILURE(status)) 273 printf("WARNING: Unable to attach object data to %s - %s\n", 274 acpi_name(handle), AcpiFormatException(status)); 275} 276 277static ACPI_STATUS 278acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, void *context, 279 void **status) 280{ 281 struct acpi_pci_devinfo *dinfo; 282 device_t child; 283 int func, slot; 284 UINT32 address; 285 286 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 287 288 child = context; 289 if (ACPI_FAILURE(acpi_GetInteger(handle, "_ADR", &address))) 290 return_ACPI_STATUS (AE_OK); 291 slot = ACPI_ADR_PCI_SLOT(address); 292 func = ACPI_ADR_PCI_FUNC(address); 293 dinfo = device_get_ivars(child); 294 if (dinfo->ap_dinfo.cfg.func == func && 295 dinfo->ap_dinfo.cfg.slot == slot) { 296 dinfo->ap_handle = handle; 297 acpi_pci_update_device(handle, child); 298 return_ACPI_STATUS (AE_CTRL_TERMINATE); 299 } 300 return_ACPI_STATUS (AE_OK); 301} 302 303void 304acpi_pci_child_added(device_t dev, device_t child) 305{ 306 307 /* 308 * PCI devices are added via the bus scan in the normal PCI 309 * bus driver. As each device is added, the 310 * acpi_pci_child_added() callback walks the ACPI namespace 311 * under the bridge driver to save ACPI handles to all the 312 * devices that appear in the ACPI namespace as immediate 313 * descendants of the bridge. 314 * 315 * XXX: Sometimes PCI devices show up in the ACPI namespace that 316 * pci_add_children() doesn't find. We currently just ignore 317 * these devices. 318 */ 319 AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1, 320 acpi_pci_save_handle, NULL, child, NULL); 321} 322 323static int 324acpi_pci_probe(device_t dev) 325{ 326 327 if (acpi_get_handle(dev) == NULL) 328 return (ENXIO); 329 device_set_desc(dev, "ACPI PCI bus"); 330 return (BUS_PROBE_DEFAULT); 331} 332 333static void 334acpi_pci_device_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context) 335{ 336 device_t child, dev; 337 ACPI_STATUS status; 338 int error; 339 340 dev = context; 341 342 switch (notify) { 343 case ACPI_NOTIFY_DEVICE_CHECK: 344 mtx_lock(&Giant); 345 BUS_RESCAN(dev); 346 mtx_unlock(&Giant); 347 break; 348 case ACPI_NOTIFY_EJECT_REQUEST: 349 child = acpi_get_device(h); 350 if (child == NULL) { 351 device_printf(dev, "no device to eject for %s\n", 352 acpi_name(h)); 353 return; 354 } 355 mtx_lock(&Giant); 356 error = device_detach(child); 357 if (error) { 358 mtx_unlock(&Giant); 359 device_printf(dev, "failed to detach %s: %d\n", 360 device_get_nameunit(child), error); 361 return; 362 } 363 status = acpi_SetInteger(h, "_EJ0", 1); 364 if (ACPI_FAILURE(status)) { 365 mtx_unlock(&Giant); 366 device_printf(dev, "failed to eject %s: %s\n", 367 acpi_name(h), AcpiFormatException(status)); 368 return; 369 } 370 BUS_RESCAN(dev); 371 mtx_unlock(&Giant); 372 break; 373 default: 374 device_printf(dev, "unknown notify %#x for %s\n", notify, 375 acpi_name(h)); 376 break; 377 } 378} 379 380static ACPI_STATUS 381acpi_pci_install_device_notify_handler(ACPI_HANDLE handle, UINT32 level, 382 void *context, void **status) 383{ 384 ACPI_HANDLE h; 385 386 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 387 388 if (ACPI_FAILURE(AcpiGetHandle(handle, "_EJ0", &h))) 389 return_ACPI_STATUS (AE_OK); 390 391 AcpiInstallNotifyHandler(handle, ACPI_SYSTEM_NOTIFY, 392 acpi_pci_device_notify_handler, context); 393 return_ACPI_STATUS (AE_OK); 394} 395 396static int 397acpi_pci_attach(device_t dev) 398{ 399 int error; 400 401 error = pci_attach(dev); 402 if (error) 403 return (error); 404 AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1, 405 acpi_pci_install_device_notify_handler, NULL, dev, NULL); 406 407 return (0); 408} 409 410static ACPI_STATUS 411acpi_pci_remove_notify_handler(ACPI_HANDLE handle, UINT32 level, void *context, 412 void **status) 413{ 414 ACPI_HANDLE h; 415 416 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 417 418 if (ACPI_FAILURE(AcpiGetHandle(handle, "_EJ0", &h))) 419 return_ACPI_STATUS (AE_OK); 420 421 AcpiRemoveNotifyHandler(handle, ACPI_SYSTEM_NOTIFY, 422 acpi_pci_device_notify_handler); 423 return_ACPI_STATUS (AE_OK); 424} 425 426static int 427acpi_pci_detach(device_t dev) 428{ 429 430 AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1, 431 acpi_pci_remove_notify_handler, NULL, dev, NULL); 432 return (pci_detach(dev)); 433} 434 435#ifdef ACPI_DMAR 436bus_dma_tag_t dmar_get_dma_tag(device_t dev, device_t child); 437static bus_dma_tag_t 438acpi_pci_get_dma_tag(device_t bus, device_t child) 439{ 440 bus_dma_tag_t tag; 441 442 if (device_get_parent(child) == bus) { 443 /* try dmar and return if it works */ 444 tag = dmar_get_dma_tag(bus, child); 445 } else 446 tag = NULL; 447 if (tag == NULL) 448 tag = pci_get_dma_tag(bus, child); 449 return (tag); 450} 451#else 452static bus_dma_tag_t 453acpi_pci_get_dma_tag(device_t bus, device_t child) 454{ 455 456 return (pci_get_dma_tag(bus, child)); 457} 458#endif 459 460