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