1 /*- 2 * Copyright (c) 1997, Stefan Esser <se@kfreebsd.org> 3 * Copyright (c) 2000, Michael Smith <msmith@kfreebsd.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 * $FreeBSD: src/sys/dev/acpica/acpi_pci.c,v 1.31.2.1.6.1 2009/04/15 03:14:26 kensmith Exp $ 29 */ 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/bus.h> 34 #include <sys/kernel.h> 35 #include <sys/malloc.h> 36 #include <sys/module.h> 37 38 #include "acpi.h" 39 #include "acpivar.h" 40 41 #include <sys/pciio.h> 42 #include <bus/pci/pcireg.h> 43 #include <bus/pci/pcivar.h> 44 #include <bus/pci/pci_private.h> 45 46 #include "pcib_if.h" 47 #include "pci_if.h" 48 49 /* Hooks for the ACPI CA debugging infrastructure. */ 50 #define _COMPONENT ACPI_BUS 51 ACPI_MODULE_NAME("PCI") 52 53 struct acpi_pci_devinfo { 54 struct pci_devinfo ap_dinfo; 55 ACPI_HANDLE ap_handle; 56 int ap_flags; 57 }; 58 59 ACPI_SERIAL_DECL(pci_powerstate, "ACPI PCI power methods"); 60 61 /* Be sure that ACPI and PCI power states are equivalent. */ 62 CTASSERT(ACPI_STATE_D0 == PCI_POWERSTATE_D0); 63 CTASSERT(ACPI_STATE_D1 == PCI_POWERSTATE_D1); 64 CTASSERT(ACPI_STATE_D2 == PCI_POWERSTATE_D2); 65 CTASSERT(ACPI_STATE_D3 == PCI_POWERSTATE_D3); 66 67 static int acpi_pci_attach(device_t dev); 68 static int acpi_pci_suspend(device_t dev); 69 static int acpi_pci_resume(device_t dev); 70 static int acpi_pci_child_location_str_method(device_t cbdev, 71 device_t child, char *buf, size_t buflen); 72 static int acpi_pci_probe(device_t dev); 73 static int acpi_pci_read_ivar(device_t dev, device_t child, int which, 74 uintptr_t *result); 75 static int acpi_pci_write_ivar(device_t dev, device_t child, int which, 76 uintptr_t value); 77 static ACPI_STATUS acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, 78 void *context, void **status); 79 static int acpi_pci_set_powerstate_method(device_t dev, device_t child, 80 int state); 81 static void acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child); 82 83 static device_method_t acpi_pci_methods[] = { 84 /* Device interface */ 85 DEVMETHOD(device_probe, acpi_pci_probe), 86 DEVMETHOD(device_attach, acpi_pci_attach), 87 DEVMETHOD(device_suspend, acpi_pci_suspend), 88 DEVMETHOD(device_resume, acpi_pci_resume), 89 90 /* Bus interface */ 91 DEVMETHOD(bus_read_ivar, acpi_pci_read_ivar), 92 DEVMETHOD(bus_write_ivar, acpi_pci_write_ivar), 93 DEVMETHOD(bus_child_location_str, acpi_pci_child_location_str_method), 94 95 /* PCI interface */ 96 DEVMETHOD(pci_set_powerstate, acpi_pci_set_powerstate_method), 97 98 DEVMETHOD_END 99 }; 100 101 static devclass_t pci_devclass; 102 103 DEFINE_CLASS_1(pci, acpi_pci_driver, acpi_pci_methods, 0, pci_driver); 104 DRIVER_MODULE(acpi_pci, pcib, acpi_pci_driver, pci_devclass, NULL, NULL); 105 MODULE_DEPEND(acpi_pci, acpi, 1, 1, 1); 106 MODULE_DEPEND(acpi_pci, pci, 1, 1, 1); 107 MODULE_VERSION(acpi_pci, 1); 108 109 static int 110 acpi_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 111 { 112 struct acpi_pci_devinfo *dinfo; 113 114 dinfo = device_get_ivars(child); 115 switch (which) { 116 case ACPI_IVAR_HANDLE: 117 *result = (uintptr_t)dinfo->ap_handle; 118 return (0); 119 case ACPI_IVAR_FLAGS: 120 *result = (uintptr_t)dinfo->ap_flags; 121 return (0); 122 } 123 return (pci_read_ivar(dev, child, which, result)); 124 } 125 126 static int 127 acpi_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 128 { 129 struct acpi_pci_devinfo *dinfo; 130 131 dinfo = device_get_ivars(child); 132 switch (which) { 133 case ACPI_IVAR_HANDLE: 134 dinfo->ap_handle = (ACPI_HANDLE)value; 135 return (0); 136 case ACPI_IVAR_FLAGS: 137 dinfo->ap_flags = (int)value; 138 return (0); 139 } 140 return (pci_write_ivar(dev, child, which, value)); 141 } 142 143 static int 144 acpi_pci_child_location_str_method(device_t cbdev, device_t child, char *buf, 145 size_t buflen) 146 { 147 struct acpi_pci_devinfo *dinfo = device_get_ivars(child); 148 149 pci_child_location_str_method(cbdev, child, buf, buflen); 150 151 if (dinfo->ap_handle) { 152 strlcat(buf, " handle=", buflen); 153 strlcat(buf, acpi_name(dinfo->ap_handle), buflen); 154 } 155 return (0); 156 } 157 158 /* 159 * PCI power manangement 160 */ 161 static int 162 acpi_pci_set_powerstate_method(device_t dev, device_t child, int state) 163 { 164 ACPI_HANDLE h; 165 ACPI_STATUS status; 166 int old_state, error; 167 168 error = 0; 169 if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3) 170 return (EINVAL); 171 172 /* 173 * We set the state using PCI Power Management outside of setting 174 * the ACPI state. This means that when powering down a device, we 175 * first shut it down using PCI, and then using ACPI, which lets ACPI 176 * try to power down any Power Resources that are now no longer used. 177 * When powering up a device, we let ACPI set the state first so that 178 * it can enable any needed Power Resources before changing the PCI 179 * power state. 180 */ 181 ACPI_SERIAL_BEGIN(pci_powerstate); 182 old_state = pci_get_powerstate(child); 183 if (old_state < state) { 184 error = pci_set_powerstate_method(dev, child, state); 185 if (error) 186 goto out; 187 } 188 h = acpi_get_handle(child); 189 status = acpi_pwr_switch_consumer(h, state); 190 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) 191 device_printf(dev, 192 "Failed to set ACPI power state D%d on %s: %s\n", 193 state, acpi_name(h), AcpiFormatException(status)); 194 if (old_state > state) 195 error = pci_set_powerstate_method(dev, child, state); 196 197 out: 198 ACPI_SERIAL_END(pci_powerstate); 199 return (error); 200 } 201 202 static void 203 acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child) 204 { 205 ACPI_STATUS status; 206 device_t child; 207 208 /* 209 * Lookup and remove the unused device that acpi0 creates when it walks 210 * the namespace creating devices. 211 */ 212 child = acpi_get_device(handle); 213 if (child != NULL) { 214 if (device_is_alive(child)) { 215 /* 216 * The TabletPC TC1000 has a second PCI-ISA bridge 217 * that has a _HID for an acpi_sysresource device. 218 * In that case, leave ACPI-CA's device data pointing 219 * at the ACPI-enumerated device. 220 */ 221 device_printf(child, 222 "Conflicts with PCI device %d:%d:%d\n", 223 pci_get_bus(pci_child), pci_get_slot(pci_child), 224 pci_get_function(pci_child)); 225 return; 226 } 227 KASSERT(device_get_parent(child) == 228 devclass_get_device(devclass_find("acpi"), 0), 229 ("%s: child (%s)'s parent is not acpi0", __func__, 230 acpi_name(handle))); 231 device_delete_child(device_get_parent(child), child); 232 } 233 234 /* 235 * Update ACPI-CA to use the PCI enumerated device_t for this handle. 236 */ 237 status = AcpiDetachData(handle, acpi_fake_objhandler); 238 if (ACPI_FAILURE(status)) 239 kprintf("WARNING: Unable to detach object data from %s - %s\n", 240 acpi_name(handle), AcpiFormatException(status)); 241 status = AcpiAttachData(handle, acpi_fake_objhandler, pci_child); 242 if (ACPI_FAILURE(status)) 243 kprintf("WARNING: Unable to attach object data to %s - %s\n", 244 acpi_name(handle), AcpiFormatException(status)); 245 } 246 247 static ACPI_STATUS 248 acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, void *context, 249 void **status) 250 { 251 struct acpi_pci_devinfo *dinfo; 252 device_t *devlist; 253 int devcount, i, func, slot; 254 UINT32 address; 255 256 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 257 258 if (ACPI_FAILURE(acpi_GetInteger(handle, "_ADR", &address))) 259 return_ACPI_STATUS (AE_OK); 260 slot = ACPI_ADR_PCI_SLOT(address); 261 func = ACPI_ADR_PCI_FUNC(address); 262 if (device_get_children((device_t)context, &devlist, &devcount) != 0) 263 return_ACPI_STATUS (AE_OK); 264 for (i = 0; i < devcount; i++) { 265 dinfo = device_get_ivars(devlist[i]); 266 if (dinfo->ap_dinfo.cfg.func == func && 267 dinfo->ap_dinfo.cfg.slot == slot) { 268 dinfo->ap_handle = handle; 269 acpi_pci_update_device(handle, devlist[i]); 270 break; 271 } 272 } 273 kfree(devlist, M_TEMP); 274 return_ACPI_STATUS (AE_OK); 275 } 276 277 static int 278 acpi_pci_probe(device_t dev) 279 { 280 281 if (pcib_get_bus(dev) < 0) 282 return (ENXIO); 283 if (acpi_get_handle(dev) == NULL) 284 return (ENXIO); 285 device_set_desc(dev, "ACPI PCI bus"); 286 return (0); 287 } 288 289 static int 290 acpi_pci_attach(device_t dev) 291 { 292 int busno, domain; 293 294 /* 295 * Since there can be multiple independantly numbered PCI 296 * busses on systems with multiple PCI domains, we can't use 297 * the unit number to decide which bus we are probing. We ask 298 * the parent pcib what our domain and bus numbers are. 299 */ 300 busno = pcib_get_bus(dev); 301 domain = pcib_get_domain(dev); 302 if (bootverbose) { 303 device_printf(dev, "domain=%d, physical bus=%d\n", 304 domain, busno); 305 } 306 307 ACPI_SERIAL_INIT(pci_powerstate); 308 309 /* 310 * First, PCI devices are added as in the normal PCI bus driver. 311 * Afterwards, the ACPI namespace under the bridge driver is 312 * walked to save ACPI handles to all the devices that appear in 313 * the ACPI namespace as immediate 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 pci_add_children(dev, domain, busno, sizeof(struct acpi_pci_devinfo)); 320 AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1, 321 acpi_pci_save_handle, NULL, dev, NULL); 322 323 return (bus_generic_attach(dev)); 324 } 325 326 int 327 acpi_pci_suspend(device_t dev) 328 { 329 int dstate, error, i, numdevs; 330 device_t acpi_dev, child, *devlist; 331 struct pci_devinfo *dinfo; 332 333 acpi_dev = devclass_get_device(devclass_find("acpi"), 0); 334 device_get_children(dev, &devlist, &numdevs); 335 336 /* 337 * Save the PCI configuration space for each child and set the 338 * device in the appropriate power state for this sleep state. 339 */ 340 for (i = 0; i < numdevs; i++) { 341 child = devlist[i]; 342 dinfo = (struct pci_devinfo *)device_get_ivars(child); 343 pci_cfg_save(child, dinfo, 0); 344 } 345 346 /* 347 * Suspend devices before potentially powering them down. 348 */ 349 error = bus_generic_suspend(dev); 350 if (error) { 351 kfree(devlist, M_TEMP); 352 return (error); 353 } 354 355 /* 356 * Always set the device to D3. If ACPI suggests a different 357 * power state, use it instead. If ACPI is not present, the 358 * firmware is responsible for managing device power. Skip 359 * children who aren't attached since they are powered down 360 * separately. Only manage type 0 devices for now. 361 */ 362 for (i = 0; acpi_dev && i < numdevs; i++) { 363 child = devlist[i]; 364 dinfo = (struct pci_devinfo *)device_get_ivars(child); 365 if (device_is_attached(child) && dinfo->cfg.hdrtype == 0) { 366 dstate = PCI_POWERSTATE_D3; 367 ACPI_PWR_FOR_SLEEP(acpi_dev, child, &dstate); 368 pci_set_powerstate(child, dstate); 369 } 370 } 371 372 kfree(devlist, M_TEMP); 373 return (0); 374 } 375 376 int 377 acpi_pci_resume(device_t dev) 378 { 379 int i, numdevs; 380 device_t acpi_dev, child, *devlist; 381 struct pci_devinfo *dinfo; 382 383 acpi_dev = devclass_get_device(devclass_find("acpi"), 0); 384 device_get_children(dev, &devlist, &numdevs); 385 386 /* 387 * Set each child to D0 and restore its PCI configuration space. 388 */ 389 for (i = 0; i < numdevs; i++) { 390 /* 391 * Notify ACPI we're going to D0 but ignore the result. If 392 * ACPI is not present, the firmware is responsible for 393 * managing device power. Only manage type 0 devices for now. 394 */ 395 child = devlist[i]; 396 dinfo = (struct pci_devinfo *) device_get_ivars(child); 397 if (acpi_dev && device_is_attached(child) && 398 dinfo->cfg.hdrtype == 0) { 399 ACPI_PWR_FOR_SLEEP(acpi_dev, child, NULL); 400 pci_set_powerstate(child, PCI_POWERSTATE_D0); 401 } 402 403 /* 404 * Now the device is powered up, restore its config space. 405 */ 406 pci_cfg_restore(child, dinfo); 407 } 408 409 kfree(devlist, M_TEMP); 410 return (bus_generic_resume(dev)); 411 } 412