1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * drivers/acpi/device_pm.c - ACPI device power management routines. 4 * 5 * Copyright (C) 2012, Intel Corp. 6 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com> 7 * 8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 9 * 10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 11 */ 12 13 #define pr_fmt(fmt) "PM: " fmt 14 15 #include <linux/acpi.h> 16 #include <linux/export.h> 17 #include <linux/mutex.h> 18 #include <linux/pm_qos.h> 19 #include <linux/pm_domain.h> 20 #include <linux/pm_runtime.h> 21 #include <linux/suspend.h> 22 23 #include "fan.h" 24 #include "internal.h" 25 26 /** 27 * acpi_power_state_string - String representation of ACPI device power state. 28 * @state: ACPI device power state to return the string representation of. 29 */ 30 const char *acpi_power_state_string(int state) 31 { 32 switch (state) { 33 case ACPI_STATE_D0: 34 return "D0"; 35 case ACPI_STATE_D1: 36 return "D1"; 37 case ACPI_STATE_D2: 38 return "D2"; 39 case ACPI_STATE_D3_HOT: 40 return "D3hot"; 41 case ACPI_STATE_D3_COLD: 42 return "D3cold"; 43 default: 44 return "(unknown)"; 45 } 46 } 47 48 static int acpi_dev_pm_explicit_get(struct acpi_device *device, int *state) 49 { 50 unsigned long long psc; 51 acpi_status status; 52 53 status = acpi_evaluate_integer(device->handle, "_PSC", NULL, &psc); 54 if (ACPI_FAILURE(status)) 55 return -ENODEV; 56 57 *state = psc; 58 return 0; 59 } 60 61 /** 62 * acpi_device_get_power - Get power state of an ACPI device. 63 * @device: Device to get the power state of. 64 * @state: Place to store the power state of the device. 65 * 66 * This function does not update the device's power.state field, but it may 67 * update its parent's power.state field (when the parent's power state is 68 * unknown and the device's power state turns out to be D0). 69 * 70 * Also, it does not update power resource reference counters to ensure that 71 * the power state returned by it will be persistent and it may return a power 72 * state shallower than previously set by acpi_device_set_power() for @device 73 * (if that power state depends on any power resources). 74 */ 75 int acpi_device_get_power(struct acpi_device *device, int *state) 76 { 77 int result = ACPI_STATE_UNKNOWN; 78 struct acpi_device *parent; 79 int error; 80 81 if (!device || !state) 82 return -EINVAL; 83 84 parent = acpi_dev_parent(device); 85 86 if (!device->flags.power_manageable) { 87 /* TBD: Non-recursive algorithm for walking up hierarchy. */ 88 *state = parent ? parent->power.state : ACPI_STATE_D0; 89 goto out; 90 } 91 92 /* 93 * Get the device's power state from power resources settings and _PSC, 94 * if available. 95 */ 96 if (device->power.flags.power_resources) { 97 error = acpi_power_get_inferred_state(device, &result); 98 if (error) 99 return error; 100 } 101 if (device->power.flags.explicit_get) { 102 int psc; 103 104 error = acpi_dev_pm_explicit_get(device, &psc); 105 if (error) 106 return error; 107 108 /* 109 * The power resources settings may indicate a power state 110 * shallower than the actual power state of the device, because 111 * the same power resources may be referenced by other devices. 112 * 113 * For systems predating ACPI 4.0 we assume that D3hot is the 114 * deepest state that can be supported. 115 */ 116 if (psc > result && psc < ACPI_STATE_D3_COLD) 117 result = psc; 118 else if (result == ACPI_STATE_UNKNOWN) 119 result = psc > ACPI_STATE_D2 ? ACPI_STATE_D3_HOT : psc; 120 } 121 122 /* 123 * If we were unsure about the device parent's power state up to this 124 * point, the fact that the device is in D0 implies that the parent has 125 * to be in D0 too, except if ignore_parent is set. 126 */ 127 if (!device->power.flags.ignore_parent && parent && 128 parent->power.state == ACPI_STATE_UNKNOWN && 129 result == ACPI_STATE_D0) 130 parent->power.state = ACPI_STATE_D0; 131 132 *state = result; 133 134 out: 135 acpi_handle_debug(device->handle, "Power state: %s\n", 136 acpi_power_state_string(*state)); 137 138 return 0; 139 } 140 141 static int acpi_dev_pm_explicit_set(struct acpi_device *adev, int state) 142 { 143 if (adev->power.states[state].flags.explicit_set) { 144 char method[5] = { '_', 'P', 'S', '0' + state, '\0' }; 145 acpi_status status; 146 147 status = acpi_evaluate_object(adev->handle, method, NULL, NULL); 148 if (ACPI_FAILURE(status)) 149 return -ENODEV; 150 } 151 return 0; 152 } 153 154 /** 155 * acpi_device_set_power - Set power state of an ACPI device. 156 * @device: Device to set the power state of. 157 * @state: New power state to set. 158 * 159 * Callers must ensure that the device is power manageable before using this 160 * function. 161 */ 162 int acpi_device_set_power(struct acpi_device *device, int state) 163 { 164 int target_state = state; 165 int result = 0; 166 167 if (!device || !device->flags.power_manageable 168 || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD)) 169 return -EINVAL; 170 171 acpi_handle_debug(device->handle, "Power state change: %s -> %s\n", 172 acpi_power_state_string(device->power.state), 173 acpi_power_state_string(state)); 174 175 /* Make sure this is a valid target state */ 176 177 /* There is a special case for D0 addressed below. */ 178 if (state > ACPI_STATE_D0 && state == device->power.state) 179 goto no_change; 180 181 if (state == ACPI_STATE_D3_COLD) { 182 /* 183 * For transitions to D3cold we need to execute _PS3 and then 184 * possibly drop references to the power resources in use. 185 */ 186 state = ACPI_STATE_D3_HOT; 187 /* If D3cold is not supported, use D3hot as the target state. */ 188 if (!device->power.states[ACPI_STATE_D3_COLD].flags.valid) 189 target_state = state; 190 } else if (!device->power.states[state].flags.valid) { 191 acpi_handle_debug(device->handle, "Power state %s not supported\n", 192 acpi_power_state_string(state)); 193 return -ENODEV; 194 } 195 196 if (!device->power.flags.ignore_parent) { 197 struct acpi_device *parent; 198 199 parent = acpi_dev_parent(device); 200 if (parent && state < parent->power.state) { 201 acpi_handle_debug(device->handle, 202 "Cannot transition to %s for parent in %s\n", 203 acpi_power_state_string(state), 204 acpi_power_state_string(parent->power.state)); 205 return -ENODEV; 206 } 207 } 208 209 /* 210 * Transition Power 211 * ---------------- 212 * In accordance with ACPI 6, _PSx is executed before manipulating power 213 * resources, unless the target state is D0, in which case _PS0 is 214 * supposed to be executed after turning the power resources on. 215 */ 216 if (state > ACPI_STATE_D0) { 217 /* 218 * According to ACPI 6, devices cannot go from lower-power 219 * (deeper) states to higher-power (shallower) states. 220 */ 221 if (state < device->power.state) { 222 acpi_handle_debug(device->handle, 223 "Cannot transition from %s to %s\n", 224 acpi_power_state_string(device->power.state), 225 acpi_power_state_string(state)); 226 return -ENODEV; 227 } 228 229 /* 230 * If the device goes from D3hot to D3cold, _PS3 has been 231 * evaluated for it already, so skip it in that case. 232 */ 233 if (device->power.state < ACPI_STATE_D3_HOT) { 234 result = acpi_dev_pm_explicit_set(device, state); 235 if (result) 236 goto end; 237 } 238 239 if (device->power.flags.power_resources) 240 result = acpi_power_transition(device, target_state); 241 } else { 242 int cur_state = device->power.state; 243 244 if (device->power.flags.power_resources) { 245 result = acpi_power_transition(device, ACPI_STATE_D0); 246 if (result) 247 goto end; 248 } 249 250 if (cur_state == ACPI_STATE_D0) { 251 int psc; 252 253 /* Nothing to do here if _PSC is not present. */ 254 if (!device->power.flags.explicit_get) 255 goto no_change; 256 257 /* 258 * The power state of the device was set to D0 last 259 * time, but that might have happened before a 260 * system-wide transition involving the platform 261 * firmware, so it may be necessary to evaluate _PS0 262 * for the device here. However, use extra care here 263 * and evaluate _PSC to check the device's current power 264 * state, and only invoke _PS0 if the evaluation of _PSC 265 * is successful and it returns a power state different 266 * from D0. 267 */ 268 result = acpi_dev_pm_explicit_get(device, &psc); 269 if (result || psc == ACPI_STATE_D0) 270 goto no_change; 271 } 272 273 result = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0); 274 } 275 276 end: 277 if (result) { 278 acpi_handle_debug(device->handle, 279 "Failed to change power state to %s\n", 280 acpi_power_state_string(target_state)); 281 } else { 282 device->power.state = target_state; 283 acpi_handle_debug(device->handle, "Power state changed to %s\n", 284 acpi_power_state_string(target_state)); 285 } 286 287 return result; 288 289 no_change: 290 acpi_handle_debug(device->handle, "Already in %s\n", 291 acpi_power_state_string(state)); 292 return 0; 293 } 294 EXPORT_SYMBOL(acpi_device_set_power); 295 296 int acpi_bus_set_power(acpi_handle handle, int state) 297 { 298 struct acpi_device *device = acpi_fetch_acpi_dev(handle); 299 300 if (device) 301 return acpi_device_set_power(device, state); 302 303 return -ENODEV; 304 } 305 EXPORT_SYMBOL(acpi_bus_set_power); 306 307 int acpi_bus_init_power(struct acpi_device *device) 308 { 309 int state; 310 int result; 311 312 if (!device) 313 return -EINVAL; 314 315 device->power.state = ACPI_STATE_UNKNOWN; 316 if (!acpi_device_is_present(device)) { 317 device->flags.initialized = false; 318 return -ENXIO; 319 } 320 321 result = acpi_device_get_power(device, &state); 322 if (result) 323 return result; 324 325 if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) { 326 /* Reference count the power resources. */ 327 result = acpi_power_on_resources(device, state); 328 if (result) 329 return result; 330 331 if (state == ACPI_STATE_D0) { 332 /* 333 * If _PSC is not present and the state inferred from 334 * power resources appears to be D0, it still may be 335 * necessary to execute _PS0 at this point, because 336 * another device using the same power resources may 337 * have been put into D0 previously and that's why we 338 * see D0 here. 339 */ 340 result = acpi_dev_pm_explicit_set(device, state); 341 if (result) 342 return result; 343 } 344 } else if (state == ACPI_STATE_UNKNOWN) { 345 /* 346 * No power resources and missing _PSC? Cross fingers and make 347 * it D0 in hope that this is what the BIOS put the device into. 348 * [We tried to force D0 here by executing _PS0, but that broke 349 * Toshiba P870-303 in a nasty way.] 350 */ 351 state = ACPI_STATE_D0; 352 } 353 device->power.state = state; 354 return 0; 355 } 356 357 /** 358 * acpi_device_fix_up_power - Force device with missing _PSC into D0. 359 * @device: Device object whose power state is to be fixed up. 360 * 361 * Devices without power resources and _PSC, but having _PS0 and _PS3 defined, 362 * are assumed to be put into D0 by the BIOS. However, in some cases that may 363 * not be the case and this function should be used then. 364 */ 365 int acpi_device_fix_up_power(struct acpi_device *device) 366 { 367 int ret = 0; 368 369 if (!device->power.flags.power_resources 370 && !device->power.flags.explicit_get 371 && device->power.state == ACPI_STATE_D0) 372 ret = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0); 373 374 return ret; 375 } 376 EXPORT_SYMBOL_GPL(acpi_device_fix_up_power); 377 378 static int fix_up_power_if_applicable(struct acpi_device *adev, void *not_used) 379 { 380 if (adev->status.present && adev->status.enabled) 381 acpi_device_fix_up_power(adev); 382 383 return 0; 384 } 385 386 /** 387 * acpi_device_fix_up_power_extended - Force device and its children into D0. 388 * @adev: Parent device object whose power state is to be fixed up. 389 * 390 * Call acpi_device_fix_up_power() for @adev and its children so long as they 391 * are reported as present and enabled. 392 */ 393 void acpi_device_fix_up_power_extended(struct acpi_device *adev) 394 { 395 acpi_device_fix_up_power(adev); 396 acpi_dev_for_each_child(adev, fix_up_power_if_applicable, NULL); 397 } 398 EXPORT_SYMBOL_GPL(acpi_device_fix_up_power_extended); 399 400 /** 401 * acpi_device_fix_up_power_children - Force a device's children into D0. 402 * @adev: Parent device object whose children's power state is to be fixed up. 403 * 404 * Call acpi_device_fix_up_power() for @adev's children so long as they 405 * are reported as present and enabled. 406 */ 407 void acpi_device_fix_up_power_children(struct acpi_device *adev) 408 { 409 acpi_dev_for_each_child(adev, fix_up_power_if_applicable, NULL); 410 } 411 EXPORT_SYMBOL_GPL(acpi_device_fix_up_power_children); 412 413 int acpi_device_update_power(struct acpi_device *device, int *state_p) 414 { 415 int state; 416 int result; 417 418 if (device->power.state == ACPI_STATE_UNKNOWN) { 419 result = acpi_bus_init_power(device); 420 if (!result && state_p) 421 *state_p = device->power.state; 422 423 return result; 424 } 425 426 result = acpi_device_get_power(device, &state); 427 if (result) 428 return result; 429 430 if (state == ACPI_STATE_UNKNOWN) { 431 state = ACPI_STATE_D0; 432 result = acpi_device_set_power(device, state); 433 if (result) 434 return result; 435 } else { 436 if (device->power.flags.power_resources) { 437 /* 438 * We don't need to really switch the state, bu we need 439 * to update the power resources' reference counters. 440 */ 441 result = acpi_power_transition(device, state); 442 if (result) 443 return result; 444 } 445 device->power.state = state; 446 } 447 if (state_p) 448 *state_p = state; 449 450 return 0; 451 } 452 EXPORT_SYMBOL_GPL(acpi_device_update_power); 453 454 int acpi_bus_update_power(acpi_handle handle, int *state_p) 455 { 456 struct acpi_device *device = acpi_fetch_acpi_dev(handle); 457 458 if (device) 459 return acpi_device_update_power(device, state_p); 460 461 return -ENODEV; 462 } 463 EXPORT_SYMBOL_GPL(acpi_bus_update_power); 464 465 bool acpi_bus_power_manageable(acpi_handle handle) 466 { 467 struct acpi_device *device = acpi_fetch_acpi_dev(handle); 468 469 return device && device->flags.power_manageable; 470 } 471 EXPORT_SYMBOL(acpi_bus_power_manageable); 472 473 static int acpi_power_up_if_adr_present(struct acpi_device *adev, void *not_used) 474 { 475 if (!(adev->flags.power_manageable && adev->pnp.type.bus_address)) 476 return 0; 477 478 acpi_handle_debug(adev->handle, "Power state: %s\n", 479 acpi_power_state_string(adev->power.state)); 480 481 if (adev->power.state == ACPI_STATE_D3_COLD) 482 return acpi_device_set_power(adev, ACPI_STATE_D0); 483 484 return 0; 485 } 486 487 /** 488 * acpi_dev_power_up_children_with_adr - Power up childres with valid _ADR 489 * @adev: Parent ACPI device object. 490 * 491 * Change the power states of the direct children of @adev that are in D3cold 492 * and hold valid _ADR objects to D0 in order to allow bus (e.g. PCI) 493 * enumeration code to access them. 494 */ 495 void acpi_dev_power_up_children_with_adr(struct acpi_device *adev) 496 { 497 acpi_dev_for_each_child(adev, acpi_power_up_if_adr_present, NULL); 498 } 499 500 /** 501 * acpi_dev_power_state_for_wake - Deepest power state for wakeup signaling 502 * @adev: ACPI companion of the target device. 503 * 504 * Evaluate _S0W for @adev and return the value produced by it or return 505 * ACPI_STATE_UNKNOWN on errors (including _S0W not present). 506 */ 507 u8 acpi_dev_power_state_for_wake(struct acpi_device *adev) 508 { 509 unsigned long long state; 510 acpi_status status; 511 512 status = acpi_evaluate_integer(adev->handle, "_S0W", NULL, &state); 513 if (ACPI_FAILURE(status)) 514 return ACPI_STATE_UNKNOWN; 515 516 return state; 517 } 518 519 #ifdef CONFIG_PM 520 static DEFINE_MUTEX(acpi_pm_notifier_lock); 521 static DEFINE_MUTEX(acpi_pm_notifier_install_lock); 522 523 void acpi_pm_wakeup_event(struct device *dev) 524 { 525 pm_wakeup_dev_event(dev, 0, acpi_s2idle_wakeup()); 526 } 527 EXPORT_SYMBOL_GPL(acpi_pm_wakeup_event); 528 529 static void acpi_pm_notify_handler(acpi_handle handle, u32 val, void *not_used) 530 { 531 struct acpi_device *adev; 532 533 if (val != ACPI_NOTIFY_DEVICE_WAKE) 534 return; 535 536 acpi_handle_debug(handle, "Wake notify\n"); 537 538 adev = acpi_get_acpi_dev(handle); 539 if (!adev) 540 return; 541 542 mutex_lock(&acpi_pm_notifier_lock); 543 544 if (adev->wakeup.flags.notifier_present) { 545 pm_wakeup_ws_event(adev->wakeup.ws, 0, acpi_s2idle_wakeup()); 546 if (adev->wakeup.context.func) { 547 acpi_handle_debug(handle, "Running %pS for %s\n", 548 adev->wakeup.context.func, 549 dev_name(adev->wakeup.context.dev)); 550 adev->wakeup.context.func(&adev->wakeup.context); 551 } 552 } 553 554 mutex_unlock(&acpi_pm_notifier_lock); 555 556 acpi_put_acpi_dev(adev); 557 } 558 559 /** 560 * acpi_add_pm_notifier - Register PM notify handler for given ACPI device. 561 * @adev: ACPI device to add the notify handler for. 562 * @dev: Device to generate a wakeup event for while handling the notification. 563 * @func: Work function to execute when handling the notification. 564 * 565 * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of 566 * PM wakeup events. For example, wakeup events may be generated for bridges 567 * if one of the devices below the bridge is signaling wakeup, even if the 568 * bridge itself doesn't have a wakeup GPE associated with it. 569 */ 570 acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev, 571 void (*func)(struct acpi_device_wakeup_context *context)) 572 { 573 acpi_status status = AE_ALREADY_EXISTS; 574 575 if (!dev && !func) 576 return AE_BAD_PARAMETER; 577 578 mutex_lock(&acpi_pm_notifier_install_lock); 579 580 if (adev->wakeup.flags.notifier_present) 581 goto out; 582 583 status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY, 584 acpi_pm_notify_handler, NULL); 585 if (ACPI_FAILURE(status)) 586 goto out; 587 588 mutex_lock(&acpi_pm_notifier_lock); 589 adev->wakeup.ws = wakeup_source_register(dev, dev_name(&adev->dev)); 590 adev->wakeup.context.dev = dev; 591 adev->wakeup.context.func = func; 592 adev->wakeup.flags.notifier_present = true; 593 mutex_unlock(&acpi_pm_notifier_lock); 594 595 out: 596 mutex_unlock(&acpi_pm_notifier_install_lock); 597 return status; 598 } 599 600 /** 601 * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device. 602 * @adev: ACPI device to remove the notifier from. 603 */ 604 acpi_status acpi_remove_pm_notifier(struct acpi_device *adev) 605 { 606 acpi_status status = AE_BAD_PARAMETER; 607 608 mutex_lock(&acpi_pm_notifier_install_lock); 609 610 if (!adev->wakeup.flags.notifier_present) 611 goto out; 612 613 status = acpi_remove_notify_handler(adev->handle, 614 ACPI_SYSTEM_NOTIFY, 615 acpi_pm_notify_handler); 616 if (ACPI_FAILURE(status)) 617 goto out; 618 619 mutex_lock(&acpi_pm_notifier_lock); 620 adev->wakeup.context.func = NULL; 621 adev->wakeup.context.dev = NULL; 622 wakeup_source_unregister(adev->wakeup.ws); 623 adev->wakeup.flags.notifier_present = false; 624 mutex_unlock(&acpi_pm_notifier_lock); 625 626 out: 627 mutex_unlock(&acpi_pm_notifier_install_lock); 628 return status; 629 } 630 631 bool acpi_bus_can_wakeup(acpi_handle handle) 632 { 633 struct acpi_device *device = acpi_fetch_acpi_dev(handle); 634 635 return device && device->wakeup.flags.valid; 636 } 637 EXPORT_SYMBOL(acpi_bus_can_wakeup); 638 639 bool acpi_pm_device_can_wakeup(struct device *dev) 640 { 641 struct acpi_device *adev = ACPI_COMPANION(dev); 642 643 return adev ? acpi_device_can_wakeup(adev) : false; 644 } 645 646 /** 647 * acpi_dev_pm_get_state - Get preferred power state of ACPI device. 648 * @dev: Device whose preferred target power state to return. 649 * @adev: ACPI device node corresponding to @dev. 650 * @target_state: System state to match the resultant device state. 651 * @d_min_p: Location to store the highest power state available to the device. 652 * @d_max_p: Location to store the lowest power state available to the device. 653 * 654 * Find the lowest power (highest number) and highest power (lowest number) ACPI 655 * device power states that the device can be in while the system is in the 656 * state represented by @target_state. Store the integer numbers representing 657 * those stats in the memory locations pointed to by @d_max_p and @d_min_p, 658 * respectively. 659 * 660 * Callers must ensure that @dev and @adev are valid pointers and that @adev 661 * actually corresponds to @dev before using this function. 662 * 663 * Returns 0 on success or -ENODATA when one of the ACPI methods fails or 664 * returns a value that doesn't make sense. The memory locations pointed to by 665 * @d_max_p and @d_min_p are only modified on success. 666 */ 667 static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev, 668 u32 target_state, int *d_min_p, int *d_max_p) 669 { 670 char method[] = { '_', 'S', '0' + target_state, 'D', '\0' }; 671 acpi_handle handle = adev->handle; 672 unsigned long long ret; 673 int d_min, d_max; 674 bool wakeup = false; 675 bool has_sxd = false; 676 acpi_status status; 677 678 /* 679 * If the system state is S0, the lowest power state the device can be 680 * in is D3cold, unless the device has _S0W and is supposed to signal 681 * wakeup, in which case the return value of _S0W has to be used as the 682 * lowest power state available to the device. 683 */ 684 d_min = ACPI_STATE_D0; 685 d_max = ACPI_STATE_D3_COLD; 686 687 /* 688 * If present, _SxD methods return the minimum D-state (highest power 689 * state) we can use for the corresponding S-states. Otherwise, the 690 * minimum D-state is D0 (ACPI 3.x). 691 */ 692 if (target_state > ACPI_STATE_S0) { 693 /* 694 * We rely on acpi_evaluate_integer() not clobbering the integer 695 * provided if AE_NOT_FOUND is returned. 696 */ 697 ret = d_min; 698 status = acpi_evaluate_integer(handle, method, NULL, &ret); 699 if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND) 700 || ret > ACPI_STATE_D3_COLD) 701 return -ENODATA; 702 703 /* 704 * We need to handle legacy systems where D3hot and D3cold are 705 * the same and 3 is returned in both cases, so fall back to 706 * D3cold if D3hot is not a valid state. 707 */ 708 if (!adev->power.states[ret].flags.valid) { 709 if (ret == ACPI_STATE_D3_HOT) 710 ret = ACPI_STATE_D3_COLD; 711 else 712 return -ENODATA; 713 } 714 715 if (status == AE_OK) 716 has_sxd = true; 717 718 d_min = ret; 719 wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid 720 && adev->wakeup.sleep_state >= target_state; 721 } else if (device_may_wakeup(dev) && dev->power.wakeirq) { 722 /* 723 * The ACPI subsystem doesn't manage the wake bit for IRQs 724 * defined with ExclusiveAndWake and SharedAndWake. Instead we 725 * expect them to be managed via the PM subsystem. Drivers 726 * should call dev_pm_set_wake_irq to register an IRQ as a wake 727 * source. 728 * 729 * If a device has a wake IRQ attached we need to check the 730 * _S0W method to get the correct wake D-state. Otherwise we 731 * end up putting the device into D3Cold which will more than 732 * likely disable wake functionality. 733 */ 734 wakeup = true; 735 } else { 736 /* ACPI GPE is specified in _PRW. */ 737 wakeup = adev->wakeup.flags.valid; 738 } 739 740 /* 741 * If _PRW says we can wake up the system from the target sleep state, 742 * the D-state returned by _SxD is sufficient for that (we assume a 743 * wakeup-aware driver if wake is set). Still, if _SxW exists 744 * (ACPI 3.x), it should return the maximum (lowest power) D-state that 745 * can wake the system. _S0W may be valid, too. 746 */ 747 if (wakeup) { 748 method[3] = 'W'; 749 status = acpi_evaluate_integer(handle, method, NULL, &ret); 750 if (status == AE_NOT_FOUND) { 751 /* No _SxW. In this case, the ACPI spec says that we 752 * must not go into any power state deeper than the 753 * value returned from _SxD. 754 */ 755 if (has_sxd && target_state > ACPI_STATE_S0) 756 d_max = d_min; 757 } else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) { 758 /* Fall back to D3cold if ret is not a valid state. */ 759 if (!adev->power.states[ret].flags.valid) 760 ret = ACPI_STATE_D3_COLD; 761 762 d_max = ret > d_min ? ret : d_min; 763 } else { 764 return -ENODATA; 765 } 766 } 767 768 if (d_min_p) 769 *d_min_p = d_min; 770 771 if (d_max_p) 772 *d_max_p = d_max; 773 774 return 0; 775 } 776 777 /** 778 * acpi_pm_device_sleep_state - Get preferred power state of ACPI device. 779 * @dev: Device whose preferred target power state to return. 780 * @d_min_p: Location to store the upper limit of the allowed states range. 781 * @d_max_in: Deepest low-power state to take into consideration. 782 * Return value: Preferred power state of the device on success, -ENODEV 783 * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is 784 * incorrect, or -ENODATA on ACPI method failure. 785 * 786 * The caller must ensure that @dev is valid before using this function. 787 */ 788 int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in) 789 { 790 struct acpi_device *adev; 791 int ret, d_min, d_max; 792 793 if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD) 794 return -EINVAL; 795 796 if (d_max_in > ACPI_STATE_D2) { 797 enum pm_qos_flags_status stat; 798 799 stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF); 800 if (stat == PM_QOS_FLAGS_ALL) 801 d_max_in = ACPI_STATE_D2; 802 } 803 804 adev = ACPI_COMPANION(dev); 805 if (!adev) { 806 dev_dbg(dev, "ACPI companion missing in %s!\n", __func__); 807 return -ENODEV; 808 } 809 810 ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(), 811 &d_min, &d_max); 812 if (ret) 813 return ret; 814 815 if (d_max_in < d_min) 816 return -EINVAL; 817 818 if (d_max > d_max_in) { 819 for (d_max = d_max_in; d_max > d_min; d_max--) { 820 if (adev->power.states[d_max].flags.valid) 821 break; 822 } 823 } 824 825 if (d_min_p) 826 *d_min_p = d_min; 827 828 return d_max; 829 } 830 EXPORT_SYMBOL(acpi_pm_device_sleep_state); 831 832 /** 833 * acpi_pm_notify_work_func - ACPI devices wakeup notification work function. 834 * @context: Device wakeup context. 835 */ 836 static void acpi_pm_notify_work_func(struct acpi_device_wakeup_context *context) 837 { 838 struct device *dev = context->dev; 839 840 if (dev) { 841 pm_wakeup_event(dev, 0); 842 pm_request_resume(dev); 843 } 844 } 845 846 static DEFINE_MUTEX(acpi_wakeup_lock); 847 848 static int __acpi_device_wakeup_enable(struct acpi_device *adev, 849 u32 target_state) 850 { 851 struct acpi_device_wakeup *wakeup = &adev->wakeup; 852 acpi_status status; 853 int error = 0; 854 855 mutex_lock(&acpi_wakeup_lock); 856 857 /* 858 * If the device wakeup power is already enabled, disable it and enable 859 * it again in case it depends on the configuration of subordinate 860 * devices and the conditions have changed since it was enabled last 861 * time. 862 */ 863 if (wakeup->enable_count > 0) 864 acpi_disable_wakeup_device_power(adev); 865 866 error = acpi_enable_wakeup_device_power(adev, target_state); 867 if (error) { 868 if (wakeup->enable_count > 0) { 869 acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number); 870 wakeup->enable_count = 0; 871 } 872 goto out; 873 } 874 875 if (wakeup->enable_count > 0) 876 goto inc; 877 878 status = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number); 879 if (ACPI_FAILURE(status)) { 880 acpi_disable_wakeup_device_power(adev); 881 error = -EIO; 882 goto out; 883 } 884 885 acpi_handle_debug(adev->handle, "GPE%2X enabled for wakeup\n", 886 (unsigned int)wakeup->gpe_number); 887 888 inc: 889 if (wakeup->enable_count < INT_MAX) 890 wakeup->enable_count++; 891 else 892 acpi_handle_info(adev->handle, "Wakeup enable count out of bounds!\n"); 893 894 out: 895 mutex_unlock(&acpi_wakeup_lock); 896 return error; 897 } 898 899 /** 900 * acpi_device_wakeup_enable - Enable wakeup functionality for device. 901 * @adev: ACPI device to enable wakeup functionality for. 902 * @target_state: State the system is transitioning into. 903 * 904 * Enable the GPE associated with @adev so that it can generate wakeup signals 905 * for the device in response to external (remote) events and enable wakeup 906 * power for it. 907 * 908 * Callers must ensure that @adev is a valid ACPI device node before executing 909 * this function. 910 */ 911 static int acpi_device_wakeup_enable(struct acpi_device *adev, u32 target_state) 912 { 913 return __acpi_device_wakeup_enable(adev, target_state); 914 } 915 916 /** 917 * acpi_device_wakeup_disable - Disable wakeup functionality for device. 918 * @adev: ACPI device to disable wakeup functionality for. 919 * 920 * Disable the GPE associated with @adev and disable wakeup power for it. 921 * 922 * Callers must ensure that @adev is a valid ACPI device node before executing 923 * this function. 924 */ 925 static void acpi_device_wakeup_disable(struct acpi_device *adev) 926 { 927 struct acpi_device_wakeup *wakeup = &adev->wakeup; 928 929 mutex_lock(&acpi_wakeup_lock); 930 931 if (!wakeup->enable_count) 932 goto out; 933 934 acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number); 935 acpi_disable_wakeup_device_power(adev); 936 937 wakeup->enable_count--; 938 939 out: 940 mutex_unlock(&acpi_wakeup_lock); 941 } 942 943 /** 944 * acpi_pm_set_device_wakeup - Enable/disable remote wakeup for given device. 945 * @dev: Device to enable/disable to generate wakeup events. 946 * @enable: Whether to enable or disable the wakeup functionality. 947 */ 948 int acpi_pm_set_device_wakeup(struct device *dev, bool enable) 949 { 950 struct acpi_device *adev; 951 int error; 952 953 adev = ACPI_COMPANION(dev); 954 if (!adev) { 955 dev_dbg(dev, "ACPI companion missing in %s!\n", __func__); 956 return -ENODEV; 957 } 958 959 if (!acpi_device_can_wakeup(adev)) 960 return -EINVAL; 961 962 if (!enable) { 963 acpi_device_wakeup_disable(adev); 964 dev_dbg(dev, "Wakeup disabled by ACPI\n"); 965 return 0; 966 } 967 968 error = __acpi_device_wakeup_enable(adev, acpi_target_system_state()); 969 if (!error) 970 dev_dbg(dev, "Wakeup enabled by ACPI\n"); 971 972 return error; 973 } 974 EXPORT_SYMBOL_GPL(acpi_pm_set_device_wakeup); 975 976 /** 977 * acpi_dev_pm_low_power - Put ACPI device into a low-power state. 978 * @dev: Device to put into a low-power state. 979 * @adev: ACPI device node corresponding to @dev. 980 * @system_state: System state to choose the device state for. 981 */ 982 static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev, 983 u32 system_state) 984 { 985 int ret, state; 986 987 if (!acpi_device_power_manageable(adev)) 988 return 0; 989 990 ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state); 991 return ret ? ret : acpi_device_set_power(adev, state); 992 } 993 994 /** 995 * acpi_dev_pm_full_power - Put ACPI device into the full-power state. 996 * @adev: ACPI device node to put into the full-power state. 997 */ 998 static int acpi_dev_pm_full_power(struct acpi_device *adev) 999 { 1000 return acpi_device_power_manageable(adev) ? 1001 acpi_device_set_power(adev, ACPI_STATE_D0) : 0; 1002 } 1003 1004 /** 1005 * acpi_dev_suspend - Put device into a low-power state using ACPI. 1006 * @dev: Device to put into a low-power state. 1007 * @wakeup: Whether or not to enable wakeup for the device. 1008 * 1009 * Put the given device into a low-power state using the standard ACPI 1010 * mechanism. Set up remote wakeup if desired, choose the state to put the 1011 * device into (this checks if remote wakeup is expected to work too), and set 1012 * the power state of the device. 1013 */ 1014 int acpi_dev_suspend(struct device *dev, bool wakeup) 1015 { 1016 struct acpi_device *adev = ACPI_COMPANION(dev); 1017 u32 target_state = acpi_target_system_state(); 1018 int error; 1019 1020 if (!adev) 1021 return 0; 1022 1023 if (wakeup && acpi_device_can_wakeup(adev)) { 1024 error = acpi_device_wakeup_enable(adev, target_state); 1025 if (error) 1026 return -EAGAIN; 1027 } else { 1028 wakeup = false; 1029 } 1030 1031 error = acpi_dev_pm_low_power(dev, adev, target_state); 1032 if (error && wakeup) 1033 acpi_device_wakeup_disable(adev); 1034 1035 return error; 1036 } 1037 EXPORT_SYMBOL_GPL(acpi_dev_suspend); 1038 1039 /** 1040 * acpi_dev_resume - Put device into the full-power state using ACPI. 1041 * @dev: Device to put into the full-power state. 1042 * 1043 * Put the given device into the full-power state using the standard ACPI 1044 * mechanism. Set the power state of the device to ACPI D0 and disable wakeup. 1045 */ 1046 int acpi_dev_resume(struct device *dev) 1047 { 1048 struct acpi_device *adev = ACPI_COMPANION(dev); 1049 int error; 1050 1051 if (!adev) 1052 return 0; 1053 1054 error = acpi_dev_pm_full_power(adev); 1055 acpi_device_wakeup_disable(adev); 1056 return error; 1057 } 1058 EXPORT_SYMBOL_GPL(acpi_dev_resume); 1059 1060 /** 1061 * acpi_subsys_runtime_suspend - Suspend device using ACPI. 1062 * @dev: Device to suspend. 1063 * 1064 * Carry out the generic runtime suspend procedure for @dev and use ACPI to put 1065 * it into a runtime low-power state. 1066 */ 1067 int acpi_subsys_runtime_suspend(struct device *dev) 1068 { 1069 int ret = pm_generic_runtime_suspend(dev); 1070 1071 return ret ? ret : acpi_dev_suspend(dev, true); 1072 } 1073 EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend); 1074 1075 /** 1076 * acpi_subsys_runtime_resume - Resume device using ACPI. 1077 * @dev: Device to Resume. 1078 * 1079 * Use ACPI to put the given device into the full-power state and carry out the 1080 * generic runtime resume procedure for it. 1081 */ 1082 int acpi_subsys_runtime_resume(struct device *dev) 1083 { 1084 int ret = acpi_dev_resume(dev); 1085 1086 return ret ? ret : pm_generic_runtime_resume(dev); 1087 } 1088 EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume); 1089 1090 #ifdef CONFIG_PM_SLEEP 1091 static bool acpi_dev_needs_resume(struct device *dev, struct acpi_device *adev) 1092 { 1093 u32 sys_target = acpi_target_system_state(); 1094 int ret, state; 1095 1096 if (!pm_runtime_suspended(dev) || !adev || (adev->wakeup.flags.valid && 1097 device_may_wakeup(dev) != !!adev->wakeup.prepare_count)) 1098 return true; 1099 1100 if (sys_target == ACPI_STATE_S0) 1101 return false; 1102 1103 if (adev->power.flags.dsw_present) 1104 return true; 1105 1106 ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state); 1107 if (ret) 1108 return true; 1109 1110 return state != adev->power.state; 1111 } 1112 1113 /** 1114 * acpi_subsys_prepare - Prepare device for system transition to a sleep state. 1115 * @dev: Device to prepare. 1116 */ 1117 int acpi_subsys_prepare(struct device *dev) 1118 { 1119 struct acpi_device *adev = ACPI_COMPANION(dev); 1120 1121 dev_pm_set_strict_midlayer(dev, true); 1122 1123 if (dev->driver && dev->driver->pm && dev->driver->pm->prepare) { 1124 int ret = dev->driver->pm->prepare(dev); 1125 1126 if (ret < 0) 1127 return ret; 1128 1129 if (!ret && dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_PREPARE)) 1130 return 0; 1131 } 1132 1133 return !acpi_dev_needs_resume(dev, adev); 1134 } 1135 EXPORT_SYMBOL_GPL(acpi_subsys_prepare); 1136 1137 /** 1138 * acpi_subsys_complete - Finalize device's resume during system resume. 1139 * @dev: Device to handle. 1140 */ 1141 void acpi_subsys_complete(struct device *dev) 1142 { 1143 pm_generic_complete(dev); 1144 /* 1145 * If the device had been runtime-suspended before the system went into 1146 * the sleep state it is going out of and it has never been resumed till 1147 * now, resume it in case the firmware powered it up. 1148 */ 1149 if (pm_runtime_suspended(dev) && pm_resume_via_firmware()) 1150 pm_request_resume(dev); 1151 1152 dev_pm_set_strict_midlayer(dev, false); 1153 } 1154 EXPORT_SYMBOL_GPL(acpi_subsys_complete); 1155 1156 /** 1157 * acpi_subsys_suspend - Run the device driver's suspend callback. 1158 * @dev: Device to handle. 1159 * 1160 * Follow PCI and resume devices from runtime suspend before running their 1161 * system suspend callbacks, unless the driver can cope with runtime-suspended 1162 * devices during system suspend and there are no ACPI-specific reasons for 1163 * resuming them. 1164 */ 1165 int acpi_subsys_suspend(struct device *dev) 1166 { 1167 if (!dev_pm_smart_suspend(dev) || 1168 acpi_dev_needs_resume(dev, ACPI_COMPANION(dev))) 1169 pm_runtime_resume(dev); 1170 1171 return pm_generic_suspend(dev); 1172 } 1173 EXPORT_SYMBOL_GPL(acpi_subsys_suspend); 1174 1175 /** 1176 * acpi_subsys_suspend_late - Suspend device using ACPI. 1177 * @dev: Device to suspend. 1178 * 1179 * Carry out the generic late suspend procedure for @dev and use ACPI to put 1180 * it into a low-power state during system transition into a sleep state. 1181 */ 1182 int acpi_subsys_suspend_late(struct device *dev) 1183 { 1184 int ret; 1185 1186 if (dev_pm_skip_suspend(dev)) 1187 return 0; 1188 1189 ret = pm_generic_suspend_late(dev); 1190 return ret ? ret : acpi_dev_suspend(dev, device_may_wakeup(dev)); 1191 } 1192 EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late); 1193 1194 /** 1195 * acpi_subsys_suspend_noirq - Run the device driver's "noirq" suspend callback. 1196 * @dev: Device to suspend. 1197 */ 1198 int acpi_subsys_suspend_noirq(struct device *dev) 1199 { 1200 int ret; 1201 1202 if (dev_pm_skip_suspend(dev)) 1203 return 0; 1204 1205 ret = pm_generic_suspend_noirq(dev); 1206 if (ret) 1207 return ret; 1208 1209 /* 1210 * If the target system sleep state is suspend-to-idle, it is sufficient 1211 * to check whether or not the device's wakeup settings are good for 1212 * runtime PM. Otherwise, the pm_resume_via_firmware() check will cause 1213 * acpi_subsys_complete() to take care of fixing up the device's state 1214 * anyway, if need be. 1215 */ 1216 if (device_can_wakeup(dev) && !device_may_wakeup(dev)) 1217 dev->power.may_skip_resume = false; 1218 1219 return 0; 1220 } 1221 EXPORT_SYMBOL_GPL(acpi_subsys_suspend_noirq); 1222 1223 /** 1224 * acpi_subsys_resume_noirq - Run the device driver's "noirq" resume callback. 1225 * @dev: Device to handle. 1226 */ 1227 static int acpi_subsys_resume_noirq(struct device *dev) 1228 { 1229 if (dev_pm_skip_resume(dev)) 1230 return 0; 1231 1232 return pm_generic_resume_noirq(dev); 1233 } 1234 1235 /** 1236 * acpi_subsys_resume_early - Resume device using ACPI. 1237 * @dev: Device to Resume. 1238 * 1239 * Use ACPI to put the given device into the full-power state and carry out the 1240 * generic early resume procedure for it during system transition into the 1241 * working state, but only do that if device either defines early resume 1242 * handler, or does not define power operations at all. Otherwise powering up 1243 * of the device is postponed to the normal resume phase. 1244 */ 1245 static int acpi_subsys_resume_early(struct device *dev) 1246 { 1247 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1248 int ret; 1249 1250 if (dev_pm_skip_resume(dev)) 1251 return 0; 1252 1253 if (pm && !pm->resume_early) { 1254 dev_dbg(dev, "Postponing ACPI PM to normal resume stage\n"); 1255 return 0; 1256 } 1257 1258 ret = acpi_dev_resume(dev); 1259 return ret ? ret : pm_generic_resume_early(dev); 1260 } 1261 1262 /** 1263 * acpi_subsys_resume - Resume device using ACPI. 1264 * @dev: Device to Resume. 1265 * 1266 * Use ACPI to put the given device into the full-power state if it has not been 1267 * powered up during early resume phase, and carry out the generic resume 1268 * procedure for it during system transition into the working state. 1269 */ 1270 static int acpi_subsys_resume(struct device *dev) 1271 { 1272 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1273 int ret = 0; 1274 1275 if (!dev_pm_skip_resume(dev) && pm && !pm->resume_early) { 1276 dev_dbg(dev, "Applying postponed ACPI PM\n"); 1277 ret = acpi_dev_resume(dev); 1278 } 1279 1280 return ret ? ret : pm_generic_resume(dev); 1281 } 1282 1283 /** 1284 * acpi_subsys_freeze - Run the device driver's freeze callback. 1285 * @dev: Device to handle. 1286 */ 1287 int acpi_subsys_freeze(struct device *dev) 1288 { 1289 /* 1290 * Resume all runtime-suspended devices before creating a snapshot 1291 * image of system memory, because the restore kernel generally cannot 1292 * be expected to always handle them consistently and they need to be 1293 * put into the runtime-active metastate during system resume anyway, 1294 * so it is better to ensure that the state saved in the image will be 1295 * always consistent with that. 1296 */ 1297 pm_runtime_resume(dev); 1298 1299 return pm_generic_freeze(dev); 1300 } 1301 EXPORT_SYMBOL_GPL(acpi_subsys_freeze); 1302 1303 /** 1304 * acpi_subsys_restore_early - Restore device using ACPI. 1305 * @dev: Device to restore. 1306 */ 1307 int acpi_subsys_restore_early(struct device *dev) 1308 { 1309 int ret = acpi_dev_resume(dev); 1310 1311 return ret ? ret : pm_generic_restore_early(dev); 1312 } 1313 EXPORT_SYMBOL_GPL(acpi_subsys_restore_early); 1314 1315 /** 1316 * acpi_subsys_poweroff - Run the device driver's poweroff callback. 1317 * @dev: Device to handle. 1318 * 1319 * Follow PCI and resume devices from runtime suspend before running their 1320 * system poweroff callbacks, unless the driver can cope with runtime-suspended 1321 * devices during system suspend and there are no ACPI-specific reasons for 1322 * resuming them. 1323 */ 1324 int acpi_subsys_poweroff(struct device *dev) 1325 { 1326 if (!dev_pm_smart_suspend(dev) || 1327 acpi_dev_needs_resume(dev, ACPI_COMPANION(dev))) 1328 pm_runtime_resume(dev); 1329 1330 return pm_generic_poweroff(dev); 1331 } 1332 EXPORT_SYMBOL_GPL(acpi_subsys_poweroff); 1333 1334 /** 1335 * acpi_subsys_poweroff_late - Run the device driver's poweroff callback. 1336 * @dev: Device to handle. 1337 * 1338 * Carry out the generic late poweroff procedure for @dev and use ACPI to put 1339 * it into a low-power state during system transition into a sleep state. 1340 */ 1341 static int acpi_subsys_poweroff_late(struct device *dev) 1342 { 1343 int ret; 1344 1345 if (dev_pm_skip_suspend(dev)) 1346 return 0; 1347 1348 ret = pm_generic_poweroff_late(dev); 1349 if (ret) 1350 return ret; 1351 1352 return acpi_dev_suspend(dev, device_may_wakeup(dev)); 1353 } 1354 1355 /** 1356 * acpi_subsys_poweroff_noirq - Run the driver's "noirq" poweroff callback. 1357 * @dev: Device to suspend. 1358 */ 1359 static int acpi_subsys_poweroff_noirq(struct device *dev) 1360 { 1361 if (dev_pm_skip_suspend(dev)) 1362 return 0; 1363 1364 return pm_generic_poweroff_noirq(dev); 1365 } 1366 #endif /* CONFIG_PM_SLEEP */ 1367 1368 static void acpi_dev_pm_detach(struct device *dev, bool power_off); 1369 1370 static struct dev_pm_domain acpi_general_pm_domain = { 1371 .ops = { 1372 .runtime_suspend = acpi_subsys_runtime_suspend, 1373 .runtime_resume = acpi_subsys_runtime_resume, 1374 #ifdef CONFIG_PM_SLEEP 1375 .prepare = acpi_subsys_prepare, 1376 .complete = acpi_subsys_complete, 1377 .suspend = acpi_subsys_suspend, 1378 .resume = acpi_subsys_resume, 1379 .suspend_late = acpi_subsys_suspend_late, 1380 .suspend_noirq = acpi_subsys_suspend_noirq, 1381 .resume_noirq = acpi_subsys_resume_noirq, 1382 .resume_early = acpi_subsys_resume_early, 1383 .freeze = acpi_subsys_freeze, 1384 .poweroff = acpi_subsys_poweroff, 1385 .poweroff_late = acpi_subsys_poweroff_late, 1386 .poweroff_noirq = acpi_subsys_poweroff_noirq, 1387 .restore_early = acpi_subsys_restore_early, 1388 #endif 1389 }, 1390 .detach = acpi_dev_pm_detach, 1391 }; 1392 1393 /** 1394 * acpi_dev_pm_detach - Remove ACPI power management from the device. 1395 * @dev: Device to take care of. 1396 * @power_off: Whether or not to try to remove power from the device. 1397 * 1398 * Remove the device from the general ACPI PM domain and remove its wakeup 1399 * notifier. If @power_off is set, additionally remove power from the device if 1400 * possible. 1401 * 1402 * Callers must ensure proper synchronization of this function with power 1403 * management callbacks. 1404 */ 1405 static void acpi_dev_pm_detach(struct device *dev, bool power_off) 1406 { 1407 struct acpi_device *adev = ACPI_COMPANION(dev); 1408 1409 if (adev && dev->pm_domain == &acpi_general_pm_domain) { 1410 dev_pm_domain_set(dev, NULL); 1411 acpi_remove_pm_notifier(adev); 1412 if (power_off) { 1413 /* 1414 * If the device's PM QoS resume latency limit or flags 1415 * have been exposed to user space, they have to be 1416 * hidden at this point, so that they don't affect the 1417 * choice of the low-power state to put the device into. 1418 */ 1419 dev_pm_qos_hide_latency_limit(dev); 1420 dev_pm_qos_hide_flags(dev); 1421 acpi_device_wakeup_disable(adev); 1422 acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0); 1423 } 1424 } 1425 } 1426 1427 /** 1428 * acpi_dev_pm_attach - Prepare device for ACPI power management. 1429 * @dev: Device to prepare. 1430 * @power_on: Whether or not to power on the device. 1431 * 1432 * If @dev has a valid ACPI handle that has a valid struct acpi_device object 1433 * attached to it, install a wakeup notification handler for the device and 1434 * add it to the general ACPI PM domain. If @power_on is set, the device will 1435 * be put into the ACPI D0 state before the function returns. 1436 * 1437 * This assumes that the @dev's bus type uses generic power management callbacks 1438 * (or doesn't use any power management callbacks at all). 1439 * 1440 * Callers must ensure proper synchronization of this function with power 1441 * management callbacks. 1442 */ 1443 int acpi_dev_pm_attach(struct device *dev, bool power_on) 1444 { 1445 /* 1446 * Skip devices whose ACPI companions match the device IDs below, 1447 * because they require special power management handling incompatible 1448 * with the generic ACPI PM domain. 1449 */ 1450 static const struct acpi_device_id special_pm_ids[] = { 1451 ACPI_FAN_DEVICE_IDS, 1452 {} 1453 }; 1454 struct acpi_device *adev = ACPI_COMPANION(dev); 1455 1456 if (!adev || !acpi_match_device_ids(adev, special_pm_ids)) 1457 return 0; 1458 1459 /* 1460 * Skip devices whose ACPI companions don't support power management and 1461 * don't have a wakeup GPE. 1462 */ 1463 if (!acpi_device_power_manageable(adev) && !acpi_device_can_wakeup(adev)) { 1464 dev_dbg(dev, "No ACPI power management or wakeup GPE\n"); 1465 return 0; 1466 } 1467 1468 /* 1469 * Only attach the power domain to the first device if the 1470 * companion is shared by multiple. This is to prevent doing power 1471 * management twice. 1472 */ 1473 if (!acpi_device_is_first_physical_node(adev, dev)) 1474 return 0; 1475 1476 acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func); 1477 dev_pm_domain_set(dev, &acpi_general_pm_domain); 1478 if (power_on) { 1479 acpi_dev_pm_full_power(adev); 1480 acpi_device_wakeup_disable(adev); 1481 } 1482 1483 return 1; 1484 } 1485 EXPORT_SYMBOL_GPL(acpi_dev_pm_attach); 1486 1487 /** 1488 * acpi_storage_d3 - Check if D3 should be used in the suspend path 1489 * @dev: Device to check 1490 * 1491 * Return %true if the platform firmware wants @dev to be programmed 1492 * into D3hot or D3cold (if supported) in the suspend path, or %false 1493 * when there is no specific preference. On some platforms, if this 1494 * hint is ignored, @dev may remain unresponsive after suspending the 1495 * platform as a whole. 1496 * 1497 * Although the property has storage in the name it actually is 1498 * applied to the PCIe slot and plugging in a non-storage device the 1499 * same platform restrictions will likely apply. 1500 */ 1501 bool acpi_storage_d3(struct device *dev) 1502 { 1503 struct acpi_device *adev = ACPI_COMPANION(dev); 1504 u8 val; 1505 1506 if (force_storage_d3()) 1507 return true; 1508 1509 if (!adev) 1510 return false; 1511 if (fwnode_property_read_u8(acpi_fwnode_handle(adev), "StorageD3Enable", 1512 &val)) 1513 return false; 1514 return val == 1; 1515 } 1516 EXPORT_SYMBOL_GPL(acpi_storage_d3); 1517 1518 /** 1519 * acpi_dev_state_d0 - Tell if the device is in D0 power state 1520 * @dev: Physical device the ACPI power state of which to check 1521 * 1522 * On a system without ACPI, return true. On a system with ACPI, return true if 1523 * the current ACPI power state of the device is D0, or false otherwise. 1524 * 1525 * Note that the power state of a device is not well-defined after it has been 1526 * passed to acpi_device_set_power() and before that function returns, so it is 1527 * not valid to ask for the ACPI power state of the device in that time frame. 1528 * 1529 * This function is intended to be used in a driver's probe or remove 1530 * function. See Documentation/firmware-guide/acpi/non-d0-probe.rst for 1531 * more information. 1532 */ 1533 bool acpi_dev_state_d0(struct device *dev) 1534 { 1535 struct acpi_device *adev = ACPI_COMPANION(dev); 1536 1537 if (!adev) 1538 return true; 1539 1540 return adev->power.state == ACPI_STATE_D0; 1541 } 1542 EXPORT_SYMBOL_GPL(acpi_dev_state_d0); 1543 1544 #endif /* CONFIG_PM */ 1545