1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/acpi/device_pm.c - ACPI device power management routines.
5 * Copyright (C) 2012, Intel Corp.
6 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13 #include <linux/acpi.h>
14 #include <linux/export.h>
15 #include <linux/mutex.h>
16 #include <linux/pm_qos.h>
17 #include <linux/pm_domain.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/suspend.h>
24 #define _COMPONENT ACPI_POWER_COMPONENT
25 ACPI_MODULE_NAME("device_pm");
28 * acpi_power_state_string - String representation of ACPI device power state.
29 * @state: ACPI device power state to return the string representation of.
31 const char *acpi_power_state_string(int state)
40 case ACPI_STATE_D3_HOT:
42 case ACPI_STATE_D3_COLD:
49 static int acpi_dev_pm_explicit_get(struct acpi_device *device, int *state)
51 unsigned long long psc;
54 status = acpi_evaluate_integer(device->handle, "_PSC", NULL, &psc);
55 if (ACPI_FAILURE(status))
63 * acpi_device_get_power - Get power state of an ACPI device.
64 * @device: Device to get the power state of.
65 * @state: Place to store the power state of the device.
67 * This function does not update the device's power.state field, but it may
68 * update its parent's power.state field (when the parent's power state is
69 * unknown and the device's power state turns out to be D0).
71 * Also, it does not update power resource reference counters to ensure that
72 * the power state returned by it will be persistent and it may return a power
73 * state shallower than previously set by acpi_device_set_power() for @device
74 * (if that power state depends on any power resources).
76 int acpi_device_get_power(struct acpi_device *device, int *state)
78 int result = ACPI_STATE_UNKNOWN;
81 if (!device || !state)
84 if (!device->flags.power_manageable) {
85 /* TBD: Non-recursive algorithm for walking up hierarchy. */
86 *state = device->parent ?
87 device->parent->power.state : ACPI_STATE_D0;
92 * Get the device's power state from power resources settings and _PSC,
95 if (device->power.flags.power_resources) {
96 error = acpi_power_get_inferred_state(device, &result);
100 if (device->power.flags.explicit_get) {
103 error = acpi_dev_pm_explicit_get(device, &psc);
108 * The power resources settings may indicate a power state
109 * shallower than the actual power state of the device, because
110 * the same power resources may be referenced by other devices.
112 * For systems predating ACPI 4.0 we assume that D3hot is the
113 * deepest state that can be supported.
115 if (psc > result && psc < ACPI_STATE_D3_COLD)
117 else if (result == ACPI_STATE_UNKNOWN)
118 result = psc > ACPI_STATE_D2 ? ACPI_STATE_D3_HOT : psc;
122 * If we were unsure about the device parent's power state up to this
123 * point, the fact that the device is in D0 implies that the parent has
124 * to be in D0 too, except if ignore_parent is set.
126 if (!device->power.flags.ignore_parent && device->parent
127 && device->parent->power.state == ACPI_STATE_UNKNOWN
128 && result == ACPI_STATE_D0)
129 device->parent->power.state = ACPI_STATE_D0;
134 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is %s\n",
135 device->pnp.bus_id, acpi_power_state_string(*state)));
140 static int acpi_dev_pm_explicit_set(struct acpi_device *adev, int state)
142 if (adev->power.states[state].flags.explicit_set) {
143 char method[5] = { '_', 'P', 'S', '0' + state, '\0' };
146 status = acpi_evaluate_object(adev->handle, method, NULL, NULL);
147 if (ACPI_FAILURE(status))
154 * acpi_device_set_power - Set power state of an ACPI device.
155 * @device: Device to set the power state of.
156 * @state: New power state to set.
158 * Callers must ensure that the device is power manageable before using this
161 int acpi_device_set_power(struct acpi_device *device, int state)
163 int target_state = state;
166 if (!device || !device->flags.power_manageable
167 || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
170 acpi_handle_debug(device->handle, "Power state change: %s -> %s\n",
171 acpi_power_state_string(device->power.state),
172 acpi_power_state_string(state));
174 /* Make sure this is a valid target state */
176 /* There is a special case for D0 addressed below. */
177 if (state > ACPI_STATE_D0 && state == device->power.state) {
178 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] already in %s\n",
180 acpi_power_state_string(state)));
184 if (state == ACPI_STATE_D3_COLD) {
186 * For transitions to D3cold we need to execute _PS3 and then
187 * possibly drop references to the power resources in use.
189 state = ACPI_STATE_D3_HOT;
190 /* If D3cold is not supported, use D3hot as the target state. */
191 if (!device->power.states[ACPI_STATE_D3_COLD].flags.valid)
192 target_state = state;
193 } else if (!device->power.states[state].flags.valid) {
194 dev_warn(&device->dev, "Power state %s not supported\n",
195 acpi_power_state_string(state));
199 if (!device->power.flags.ignore_parent &&
200 device->parent && (state < device->parent->power.state)) {
201 dev_warn(&device->dev,
202 "Cannot transition to power state %s for parent in %s\n",
203 acpi_power_state_string(state),
204 acpi_power_state_string(device->parent->power.state));
211 * In accordance with ACPI 6, _PSx is executed before manipulating power
212 * resources, unless the target state is D0, in which case _PS0 is
213 * supposed to be executed after turning the power resources on.
215 if (state > ACPI_STATE_D0) {
217 * According to ACPI 6, devices cannot go from lower-power
218 * (deeper) states to higher-power (shallower) states.
220 if (state < device->power.state) {
221 dev_warn(&device->dev, "Cannot transition from %s to %s\n",
222 acpi_power_state_string(device->power.state),
223 acpi_power_state_string(state));
228 * If the device goes from D3hot to D3cold, _PS3 has been
229 * evaluated for it already, so skip it in that case.
231 if (device->power.state < ACPI_STATE_D3_HOT) {
232 result = acpi_dev_pm_explicit_set(device, state);
237 if (device->power.flags.power_resources)
238 result = acpi_power_transition(device, target_state);
240 int cur_state = device->power.state;
242 if (device->power.flags.power_resources) {
243 result = acpi_power_transition(device, ACPI_STATE_D0);
248 if (cur_state == ACPI_STATE_D0) {
251 /* Nothing to do here if _PSC is not present. */
252 if (!device->power.flags.explicit_get)
256 * The power state of the device was set to D0 last
257 * time, but that might have happened before a
258 * system-wide transition involving the platform
259 * firmware, so it may be necessary to evaluate _PS0
260 * for the device here. However, use extra care here
261 * and evaluate _PSC to check the device's current power
262 * state, and only invoke _PS0 if the evaluation of _PSC
263 * is successful and it returns a power state different
266 result = acpi_dev_pm_explicit_get(device, &psc);
267 if (result || psc == ACPI_STATE_D0)
271 result = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
276 dev_warn(&device->dev, "Failed to change power state to %s\n",
277 acpi_power_state_string(target_state));
279 device->power.state = target_state;
280 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
281 "Device [%s] transitioned to %s\n",
283 acpi_power_state_string(target_state)));
288 EXPORT_SYMBOL(acpi_device_set_power);
290 int acpi_bus_set_power(acpi_handle handle, int state)
292 struct acpi_device *device;
295 result = acpi_bus_get_device(handle, &device);
299 return acpi_device_set_power(device, state);
301 EXPORT_SYMBOL(acpi_bus_set_power);
303 int acpi_bus_init_power(struct acpi_device *device)
311 device->power.state = ACPI_STATE_UNKNOWN;
312 if (!acpi_device_is_present(device)) {
313 device->flags.initialized = false;
317 result = acpi_device_get_power(device, &state);
321 if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) {
322 /* Reference count the power resources. */
323 result = acpi_power_on_resources(device, state);
327 if (state == ACPI_STATE_D0) {
329 * If _PSC is not present and the state inferred from
330 * power resources appears to be D0, it still may be
331 * necessary to execute _PS0 at this point, because
332 * another device using the same power resources may
333 * have been put into D0 previously and that's why we
336 result = acpi_dev_pm_explicit_set(device, state);
340 } else if (state == ACPI_STATE_UNKNOWN) {
342 * No power resources and missing _PSC? Cross fingers and make
343 * it D0 in hope that this is what the BIOS put the device into.
344 * [We tried to force D0 here by executing _PS0, but that broke
345 * Toshiba P870-303 in a nasty way.]
347 state = ACPI_STATE_D0;
349 device->power.state = state;
354 * acpi_device_fix_up_power - Force device with missing _PSC into D0.
355 * @device: Device object whose power state is to be fixed up.
357 * Devices without power resources and _PSC, but having _PS0 and _PS3 defined,
358 * are assumed to be put into D0 by the BIOS. However, in some cases that may
359 * not be the case and this function should be used then.
361 int acpi_device_fix_up_power(struct acpi_device *device)
365 if (!device->power.flags.power_resources
366 && !device->power.flags.explicit_get
367 && device->power.state == ACPI_STATE_D0)
368 ret = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
372 EXPORT_SYMBOL_GPL(acpi_device_fix_up_power);
374 int acpi_device_update_power(struct acpi_device *device, int *state_p)
379 if (device->power.state == ACPI_STATE_UNKNOWN) {
380 result = acpi_bus_init_power(device);
381 if (!result && state_p)
382 *state_p = device->power.state;
387 result = acpi_device_get_power(device, &state);
391 if (state == ACPI_STATE_UNKNOWN) {
392 state = ACPI_STATE_D0;
393 result = acpi_device_set_power(device, state);
397 if (device->power.flags.power_resources) {
399 * We don't need to really switch the state, bu we need
400 * to update the power resources' reference counters.
402 result = acpi_power_transition(device, state);
406 device->power.state = state;
413 EXPORT_SYMBOL_GPL(acpi_device_update_power);
415 int acpi_bus_update_power(acpi_handle handle, int *state_p)
417 struct acpi_device *device;
420 result = acpi_bus_get_device(handle, &device);
421 return result ? result : acpi_device_update_power(device, state_p);
423 EXPORT_SYMBOL_GPL(acpi_bus_update_power);
425 bool acpi_bus_power_manageable(acpi_handle handle)
427 struct acpi_device *device;
430 result = acpi_bus_get_device(handle, &device);
431 return result ? false : device->flags.power_manageable;
433 EXPORT_SYMBOL(acpi_bus_power_manageable);
436 static DEFINE_MUTEX(acpi_pm_notifier_lock);
437 static DEFINE_MUTEX(acpi_pm_notifier_install_lock);
439 void acpi_pm_wakeup_event(struct device *dev)
441 pm_wakeup_dev_event(dev, 0, acpi_s2idle_wakeup());
443 EXPORT_SYMBOL_GPL(acpi_pm_wakeup_event);
445 static void acpi_pm_notify_handler(acpi_handle handle, u32 val, void *not_used)
447 struct acpi_device *adev;
449 if (val != ACPI_NOTIFY_DEVICE_WAKE)
452 acpi_handle_debug(handle, "Wake notify\n");
454 adev = acpi_bus_get_acpi_device(handle);
458 mutex_lock(&acpi_pm_notifier_lock);
460 if (adev->wakeup.flags.notifier_present) {
461 pm_wakeup_ws_event(adev->wakeup.ws, 0, acpi_s2idle_wakeup());
462 if (adev->wakeup.context.func) {
463 acpi_handle_debug(handle, "Running %pS for %s\n",
464 adev->wakeup.context.func,
465 dev_name(adev->wakeup.context.dev));
466 adev->wakeup.context.func(&adev->wakeup.context);
470 mutex_unlock(&acpi_pm_notifier_lock);
472 acpi_bus_put_acpi_device(adev);
476 * acpi_add_pm_notifier - Register PM notify handler for given ACPI device.
477 * @adev: ACPI device to add the notify handler for.
478 * @dev: Device to generate a wakeup event for while handling the notification.
479 * @func: Work function to execute when handling the notification.
481 * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of
482 * PM wakeup events. For example, wakeup events may be generated for bridges
483 * if one of the devices below the bridge is signaling wakeup, even if the
484 * bridge itself doesn't have a wakeup GPE associated with it.
486 acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev,
487 void (*func)(struct acpi_device_wakeup_context *context))
489 acpi_status status = AE_ALREADY_EXISTS;
492 return AE_BAD_PARAMETER;
494 mutex_lock(&acpi_pm_notifier_install_lock);
496 if (adev->wakeup.flags.notifier_present)
499 status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,
500 acpi_pm_notify_handler, NULL);
501 if (ACPI_FAILURE(status))
504 mutex_lock(&acpi_pm_notifier_lock);
505 adev->wakeup.ws = wakeup_source_register(&adev->dev,
506 dev_name(&adev->dev));
507 adev->wakeup.context.dev = dev;
508 adev->wakeup.context.func = func;
509 adev->wakeup.flags.notifier_present = true;
510 mutex_unlock(&acpi_pm_notifier_lock);
513 mutex_unlock(&acpi_pm_notifier_install_lock);
518 * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
519 * @adev: ACPI device to remove the notifier from.
521 acpi_status acpi_remove_pm_notifier(struct acpi_device *adev)
523 acpi_status status = AE_BAD_PARAMETER;
525 mutex_lock(&acpi_pm_notifier_install_lock);
527 if (!adev->wakeup.flags.notifier_present)
530 status = acpi_remove_notify_handler(adev->handle,
532 acpi_pm_notify_handler);
533 if (ACPI_FAILURE(status))
536 mutex_lock(&acpi_pm_notifier_lock);
537 adev->wakeup.context.func = NULL;
538 adev->wakeup.context.dev = NULL;
539 wakeup_source_unregister(adev->wakeup.ws);
540 adev->wakeup.flags.notifier_present = false;
541 mutex_unlock(&acpi_pm_notifier_lock);
544 mutex_unlock(&acpi_pm_notifier_install_lock);
548 bool acpi_bus_can_wakeup(acpi_handle handle)
550 struct acpi_device *device;
553 result = acpi_bus_get_device(handle, &device);
554 return result ? false : device->wakeup.flags.valid;
556 EXPORT_SYMBOL(acpi_bus_can_wakeup);
558 bool acpi_pm_device_can_wakeup(struct device *dev)
560 struct acpi_device *adev = ACPI_COMPANION(dev);
562 return adev ? acpi_device_can_wakeup(adev) : false;
566 * acpi_dev_pm_get_state - Get preferred power state of ACPI device.
567 * @dev: Device whose preferred target power state to return.
568 * @adev: ACPI device node corresponding to @dev.
569 * @target_state: System state to match the resultant device state.
570 * @d_min_p: Location to store the highest power state available to the device.
571 * @d_max_p: Location to store the lowest power state available to the device.
573 * Find the lowest power (highest number) and highest power (lowest number) ACPI
574 * device power states that the device can be in while the system is in the
575 * state represented by @target_state. Store the integer numbers representing
576 * those stats in the memory locations pointed to by @d_max_p and @d_min_p,
579 * Callers must ensure that @dev and @adev are valid pointers and that @adev
580 * actually corresponds to @dev before using this function.
582 * Returns 0 on success or -ENODATA when one of the ACPI methods fails or
583 * returns a value that doesn't make sense. The memory locations pointed to by
584 * @d_max_p and @d_min_p are only modified on success.
586 static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev,
587 u32 target_state, int *d_min_p, int *d_max_p)
589 char method[] = { '_', 'S', '0' + target_state, 'D', '\0' };
590 acpi_handle handle = adev->handle;
591 unsigned long long ret;
594 bool has_sxd = false;
598 * If the system state is S0, the lowest power state the device can be
599 * in is D3cold, unless the device has _S0W and is supposed to signal
600 * wakeup, in which case the return value of _S0W has to be used as the
601 * lowest power state available to the device.
603 d_min = ACPI_STATE_D0;
604 d_max = ACPI_STATE_D3_COLD;
607 * If present, _SxD methods return the minimum D-state (highest power
608 * state) we can use for the corresponding S-states. Otherwise, the
609 * minimum D-state is D0 (ACPI 3.x).
611 if (target_state > ACPI_STATE_S0) {
613 * We rely on acpi_evaluate_integer() not clobbering the integer
614 * provided if AE_NOT_FOUND is returned.
617 status = acpi_evaluate_integer(handle, method, NULL, &ret);
618 if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND)
619 || ret > ACPI_STATE_D3_COLD)
623 * We need to handle legacy systems where D3hot and D3cold are
624 * the same and 3 is returned in both cases, so fall back to
625 * D3cold if D3hot is not a valid state.
627 if (!adev->power.states[ret].flags.valid) {
628 if (ret == ACPI_STATE_D3_HOT)
629 ret = ACPI_STATE_D3_COLD;
638 wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
639 && adev->wakeup.sleep_state >= target_state;
641 wakeup = adev->wakeup.flags.valid;
645 * If _PRW says we can wake up the system from the target sleep state,
646 * the D-state returned by _SxD is sufficient for that (we assume a
647 * wakeup-aware driver if wake is set). Still, if _SxW exists
648 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
649 * can wake the system. _S0W may be valid, too.
653 status = acpi_evaluate_integer(handle, method, NULL, &ret);
654 if (status == AE_NOT_FOUND) {
655 /* No _SxW. In this case, the ACPI spec says that we
656 * must not go into any power state deeper than the
657 * value returned from _SxD.
659 if (has_sxd && target_state > ACPI_STATE_S0)
661 } else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) {
662 /* Fall back to D3cold if ret is not a valid state. */
663 if (!adev->power.states[ret].flags.valid)
664 ret = ACPI_STATE_D3_COLD;
666 d_max = ret > d_min ? ret : d_min;
682 * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
683 * @dev: Device whose preferred target power state to return.
684 * @d_min_p: Location to store the upper limit of the allowed states range.
685 * @d_max_in: Deepest low-power state to take into consideration.
686 * Return value: Preferred power state of the device on success, -ENODEV
687 * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is
688 * incorrect, or -ENODATA on ACPI method failure.
690 * The caller must ensure that @dev is valid before using this function.
692 int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
694 struct acpi_device *adev;
695 int ret, d_min, d_max;
697 if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD)
700 if (d_max_in > ACPI_STATE_D2) {
701 enum pm_qos_flags_status stat;
703 stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
704 if (stat == PM_QOS_FLAGS_ALL)
705 d_max_in = ACPI_STATE_D2;
708 adev = ACPI_COMPANION(dev);
710 dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
714 ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(),
719 if (d_max_in < d_min)
722 if (d_max > d_max_in) {
723 for (d_max = d_max_in; d_max > d_min; d_max--) {
724 if (adev->power.states[d_max].flags.valid)
734 EXPORT_SYMBOL(acpi_pm_device_sleep_state);
737 * acpi_pm_notify_work_func - ACPI devices wakeup notification work function.
738 * @context: Device wakeup context.
740 static void acpi_pm_notify_work_func(struct acpi_device_wakeup_context *context)
742 struct device *dev = context->dev;
745 pm_wakeup_event(dev, 0);
746 pm_request_resume(dev);
750 static DEFINE_MUTEX(acpi_wakeup_lock);
752 static int __acpi_device_wakeup_enable(struct acpi_device *adev,
755 struct acpi_device_wakeup *wakeup = &adev->wakeup;
759 mutex_lock(&acpi_wakeup_lock);
761 if (wakeup->enable_count >= INT_MAX) {
762 acpi_handle_info(adev->handle, "Wakeup enable count out of bounds!\n");
765 if (wakeup->enable_count > 0)
768 error = acpi_enable_wakeup_device_power(adev, target_state);
772 status = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
773 if (ACPI_FAILURE(status)) {
774 acpi_disable_wakeup_device_power(adev);
779 acpi_handle_debug(adev->handle, "GPE%2X enabled for wakeup\n",
780 (unsigned int)wakeup->gpe_number);
783 wakeup->enable_count++;
786 mutex_unlock(&acpi_wakeup_lock);
791 * acpi_device_wakeup_enable - Enable wakeup functionality for device.
792 * @adev: ACPI device to enable wakeup functionality for.
793 * @target_state: State the system is transitioning into.
795 * Enable the GPE associated with @adev so that it can generate wakeup signals
796 * for the device in response to external (remote) events and enable wakeup
799 * Callers must ensure that @adev is a valid ACPI device node before executing
802 static int acpi_device_wakeup_enable(struct acpi_device *adev, u32 target_state)
804 return __acpi_device_wakeup_enable(adev, target_state);
808 * acpi_device_wakeup_disable - Disable wakeup functionality for device.
809 * @adev: ACPI device to disable wakeup functionality for.
811 * Disable the GPE associated with @adev and disable wakeup power for it.
813 * Callers must ensure that @adev is a valid ACPI device node before executing
816 static void acpi_device_wakeup_disable(struct acpi_device *adev)
818 struct acpi_device_wakeup *wakeup = &adev->wakeup;
820 mutex_lock(&acpi_wakeup_lock);
822 if (!wakeup->enable_count)
825 acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
826 acpi_disable_wakeup_device_power(adev);
828 wakeup->enable_count--;
831 mutex_unlock(&acpi_wakeup_lock);
835 * acpi_pm_set_device_wakeup - Enable/disable remote wakeup for given device.
836 * @dev: Device to enable/disable to generate wakeup events.
837 * @enable: Whether to enable or disable the wakeup functionality.
839 int acpi_pm_set_device_wakeup(struct device *dev, bool enable)
841 struct acpi_device *adev;
844 adev = ACPI_COMPANION(dev);
846 dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
850 if (!acpi_device_can_wakeup(adev))
854 acpi_device_wakeup_disable(adev);
855 dev_dbg(dev, "Wakeup disabled by ACPI\n");
859 error = __acpi_device_wakeup_enable(adev, acpi_target_system_state());
861 dev_dbg(dev, "Wakeup enabled by ACPI\n");
865 EXPORT_SYMBOL_GPL(acpi_pm_set_device_wakeup);
868 * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
869 * @dev: Device to put into a low-power state.
870 * @adev: ACPI device node corresponding to @dev.
871 * @system_state: System state to choose the device state for.
873 static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
878 if (!acpi_device_power_manageable(adev))
881 ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state);
882 return ret ? ret : acpi_device_set_power(adev, state);
886 * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
887 * @adev: ACPI device node to put into the full-power state.
889 static int acpi_dev_pm_full_power(struct acpi_device *adev)
891 return acpi_device_power_manageable(adev) ?
892 acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
896 * acpi_dev_suspend - Put device into a low-power state using ACPI.
897 * @dev: Device to put into a low-power state.
898 * @wakeup: Whether or not to enable wakeup for the device.
900 * Put the given device into a low-power state using the standard ACPI
901 * mechanism. Set up remote wakeup if desired, choose the state to put the
902 * device into (this checks if remote wakeup is expected to work too), and set
903 * the power state of the device.
905 int acpi_dev_suspend(struct device *dev, bool wakeup)
907 struct acpi_device *adev = ACPI_COMPANION(dev);
908 u32 target_state = acpi_target_system_state();
914 if (wakeup && acpi_device_can_wakeup(adev)) {
915 error = acpi_device_wakeup_enable(adev, target_state);
922 error = acpi_dev_pm_low_power(dev, adev, target_state);
924 acpi_device_wakeup_disable(adev);
928 EXPORT_SYMBOL_GPL(acpi_dev_suspend);
931 * acpi_dev_resume - Put device into the full-power state using ACPI.
932 * @dev: Device to put into the full-power state.
934 * Put the given device into the full-power state using the standard ACPI
935 * mechanism. Set the power state of the device to ACPI D0 and disable wakeup.
937 int acpi_dev_resume(struct device *dev)
939 struct acpi_device *adev = ACPI_COMPANION(dev);
945 error = acpi_dev_pm_full_power(adev);
946 acpi_device_wakeup_disable(adev);
949 EXPORT_SYMBOL_GPL(acpi_dev_resume);
952 * acpi_subsys_runtime_suspend - Suspend device using ACPI.
953 * @dev: Device to suspend.
955 * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
956 * it into a runtime low-power state.
958 int acpi_subsys_runtime_suspend(struct device *dev)
960 int ret = pm_generic_runtime_suspend(dev);
961 return ret ? ret : acpi_dev_suspend(dev, true);
963 EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
966 * acpi_subsys_runtime_resume - Resume device using ACPI.
967 * @dev: Device to Resume.
969 * Use ACPI to put the given device into the full-power state and carry out the
970 * generic runtime resume procedure for it.
972 int acpi_subsys_runtime_resume(struct device *dev)
974 int ret = acpi_dev_resume(dev);
975 return ret ? ret : pm_generic_runtime_resume(dev);
977 EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
979 #ifdef CONFIG_PM_SLEEP
980 static bool acpi_dev_needs_resume(struct device *dev, struct acpi_device *adev)
982 u32 sys_target = acpi_target_system_state();
985 if (!pm_runtime_suspended(dev) || !adev || (adev->wakeup.flags.valid &&
986 device_may_wakeup(dev) != !!adev->wakeup.prepare_count))
989 if (sys_target == ACPI_STATE_S0)
992 if (adev->power.flags.dsw_present)
995 ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state);
999 return state != adev->power.state;
1003 * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
1004 * @dev: Device to prepare.
1006 int acpi_subsys_prepare(struct device *dev)
1008 struct acpi_device *adev = ACPI_COMPANION(dev);
1010 if (dev->driver && dev->driver->pm && dev->driver->pm->prepare) {
1011 int ret = dev->driver->pm->prepare(dev);
1016 if (!ret && dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_PREPARE))
1020 return !acpi_dev_needs_resume(dev, adev);
1022 EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
1025 * acpi_subsys_complete - Finalize device's resume during system resume.
1026 * @dev: Device to handle.
1028 void acpi_subsys_complete(struct device *dev)
1030 pm_generic_complete(dev);
1032 * If the device had been runtime-suspended before the system went into
1033 * the sleep state it is going out of and it has never been resumed till
1034 * now, resume it in case the firmware powered it up.
1036 if (pm_runtime_suspended(dev) && pm_resume_via_firmware())
1037 pm_request_resume(dev);
1039 EXPORT_SYMBOL_GPL(acpi_subsys_complete);
1042 * acpi_subsys_suspend - Run the device driver's suspend callback.
1043 * @dev: Device to handle.
1045 * Follow PCI and resume devices from runtime suspend before running their
1046 * system suspend callbacks, unless the driver can cope with runtime-suspended
1047 * devices during system suspend and there are no ACPI-specific reasons for
1050 int acpi_subsys_suspend(struct device *dev)
1052 if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
1053 acpi_dev_needs_resume(dev, ACPI_COMPANION(dev)))
1054 pm_runtime_resume(dev);
1056 return pm_generic_suspend(dev);
1058 EXPORT_SYMBOL_GPL(acpi_subsys_suspend);
1061 * acpi_subsys_suspend_late - Suspend device using ACPI.
1062 * @dev: Device to suspend.
1064 * Carry out the generic late suspend procedure for @dev and use ACPI to put
1065 * it into a low-power state during system transition into a sleep state.
1067 int acpi_subsys_suspend_late(struct device *dev)
1071 if (dev_pm_skip_suspend(dev))
1074 ret = pm_generic_suspend_late(dev);
1075 return ret ? ret : acpi_dev_suspend(dev, device_may_wakeup(dev));
1077 EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
1080 * acpi_subsys_suspend_noirq - Run the device driver's "noirq" suspend callback.
1081 * @dev: Device to suspend.
1083 int acpi_subsys_suspend_noirq(struct device *dev)
1087 if (dev_pm_skip_suspend(dev))
1090 ret = pm_generic_suspend_noirq(dev);
1095 * If the target system sleep state is suspend-to-idle, it is sufficient
1096 * to check whether or not the device's wakeup settings are good for
1097 * runtime PM. Otherwise, the pm_resume_via_firmware() check will cause
1098 * acpi_subsys_complete() to take care of fixing up the device's state
1099 * anyway, if need be.
1101 if (device_can_wakeup(dev) && !device_may_wakeup(dev))
1102 dev->power.may_skip_resume = false;
1106 EXPORT_SYMBOL_GPL(acpi_subsys_suspend_noirq);
1109 * acpi_subsys_resume_noirq - Run the device driver's "noirq" resume callback.
1110 * @dev: Device to handle.
1112 static int acpi_subsys_resume_noirq(struct device *dev)
1114 if (dev_pm_skip_resume(dev))
1117 return pm_generic_resume_noirq(dev);
1121 * acpi_subsys_resume_early - Resume device using ACPI.
1122 * @dev: Device to Resume.
1124 * Use ACPI to put the given device into the full-power state and carry out the
1125 * generic early resume procedure for it during system transition into the
1128 static int acpi_subsys_resume_early(struct device *dev)
1132 if (dev_pm_skip_resume(dev))
1135 ret = acpi_dev_resume(dev);
1136 return ret ? ret : pm_generic_resume_early(dev);
1140 * acpi_subsys_freeze - Run the device driver's freeze callback.
1141 * @dev: Device to handle.
1143 int acpi_subsys_freeze(struct device *dev)
1146 * Resume all runtime-suspended devices before creating a snapshot
1147 * image of system memory, because the restore kernel generally cannot
1148 * be expected to always handle them consistently and they need to be
1149 * put into the runtime-active metastate during system resume anyway,
1150 * so it is better to ensure that the state saved in the image will be
1151 * always consistent with that.
1153 pm_runtime_resume(dev);
1155 return pm_generic_freeze(dev);
1157 EXPORT_SYMBOL_GPL(acpi_subsys_freeze);
1160 * acpi_subsys_restore_early - Restore device using ACPI.
1161 * @dev: Device to restore.
1163 int acpi_subsys_restore_early(struct device *dev)
1165 int ret = acpi_dev_resume(dev);
1166 return ret ? ret : pm_generic_restore_early(dev);
1168 EXPORT_SYMBOL_GPL(acpi_subsys_restore_early);
1171 * acpi_subsys_poweroff - Run the device driver's poweroff callback.
1172 * @dev: Device to handle.
1174 * Follow PCI and resume devices from runtime suspend before running their
1175 * system poweroff callbacks, unless the driver can cope with runtime-suspended
1176 * devices during system suspend and there are no ACPI-specific reasons for
1179 int acpi_subsys_poweroff(struct device *dev)
1181 if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
1182 acpi_dev_needs_resume(dev, ACPI_COMPANION(dev)))
1183 pm_runtime_resume(dev);
1185 return pm_generic_poweroff(dev);
1187 EXPORT_SYMBOL_GPL(acpi_subsys_poweroff);
1190 * acpi_subsys_poweroff_late - Run the device driver's poweroff callback.
1191 * @dev: Device to handle.
1193 * Carry out the generic late poweroff procedure for @dev and use ACPI to put
1194 * it into a low-power state during system transition into a sleep state.
1196 static int acpi_subsys_poweroff_late(struct device *dev)
1200 if (dev_pm_skip_suspend(dev))
1203 ret = pm_generic_poweroff_late(dev);
1207 return acpi_dev_suspend(dev, device_may_wakeup(dev));
1211 * acpi_subsys_poweroff_noirq - Run the driver's "noirq" poweroff callback.
1212 * @dev: Device to suspend.
1214 static int acpi_subsys_poweroff_noirq(struct device *dev)
1216 if (dev_pm_skip_suspend(dev))
1219 return pm_generic_poweroff_noirq(dev);
1221 #endif /* CONFIG_PM_SLEEP */
1223 static struct dev_pm_domain acpi_general_pm_domain = {
1225 .runtime_suspend = acpi_subsys_runtime_suspend,
1226 .runtime_resume = acpi_subsys_runtime_resume,
1227 #ifdef CONFIG_PM_SLEEP
1228 .prepare = acpi_subsys_prepare,
1229 .complete = acpi_subsys_complete,
1230 .suspend = acpi_subsys_suspend,
1231 .suspend_late = acpi_subsys_suspend_late,
1232 .suspend_noirq = acpi_subsys_suspend_noirq,
1233 .resume_noirq = acpi_subsys_resume_noirq,
1234 .resume_early = acpi_subsys_resume_early,
1235 .freeze = acpi_subsys_freeze,
1236 .poweroff = acpi_subsys_poweroff,
1237 .poweroff_late = acpi_subsys_poweroff_late,
1238 .poweroff_noirq = acpi_subsys_poweroff_noirq,
1239 .restore_early = acpi_subsys_restore_early,
1245 * acpi_dev_pm_detach - Remove ACPI power management from the device.
1246 * @dev: Device to take care of.
1247 * @power_off: Whether or not to try to remove power from the device.
1249 * Remove the device from the general ACPI PM domain and remove its wakeup
1250 * notifier. If @power_off is set, additionally remove power from the device if
1253 * Callers must ensure proper synchronization of this function with power
1254 * management callbacks.
1256 static void acpi_dev_pm_detach(struct device *dev, bool power_off)
1258 struct acpi_device *adev = ACPI_COMPANION(dev);
1260 if (adev && dev->pm_domain == &acpi_general_pm_domain) {
1261 dev_pm_domain_set(dev, NULL);
1262 acpi_remove_pm_notifier(adev);
1265 * If the device's PM QoS resume latency limit or flags
1266 * have been exposed to user space, they have to be
1267 * hidden at this point, so that they don't affect the
1268 * choice of the low-power state to put the device into.
1270 dev_pm_qos_hide_latency_limit(dev);
1271 dev_pm_qos_hide_flags(dev);
1272 acpi_device_wakeup_disable(adev);
1273 acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
1279 * acpi_dev_pm_attach - Prepare device for ACPI power management.
1280 * @dev: Device to prepare.
1281 * @power_on: Whether or not to power on the device.
1283 * If @dev has a valid ACPI handle that has a valid struct acpi_device object
1284 * attached to it, install a wakeup notification handler for the device and
1285 * add it to the general ACPI PM domain. If @power_on is set, the device will
1286 * be put into the ACPI D0 state before the function returns.
1288 * This assumes that the @dev's bus type uses generic power management callbacks
1289 * (or doesn't use any power management callbacks at all).
1291 * Callers must ensure proper synchronization of this function with power
1292 * management callbacks.
1294 int acpi_dev_pm_attach(struct device *dev, bool power_on)
1297 * Skip devices whose ACPI companions match the device IDs below,
1298 * because they require special power management handling incompatible
1299 * with the generic ACPI PM domain.
1301 static const struct acpi_device_id special_pm_ids[] = {
1302 ACPI_FAN_DEVICE_IDS,
1305 struct acpi_device *adev = ACPI_COMPANION(dev);
1307 if (!adev || !acpi_match_device_ids(adev, special_pm_ids))
1311 * Only attach the power domain to the first device if the
1312 * companion is shared by multiple. This is to prevent doing power
1315 if (!acpi_device_is_first_physical_node(adev, dev))
1318 acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func);
1319 dev_pm_domain_set(dev, &acpi_general_pm_domain);
1321 acpi_dev_pm_full_power(adev);
1322 acpi_device_wakeup_disable(adev);
1325 dev->pm_domain->detach = acpi_dev_pm_detach;
1328 EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
1331 * acpi_storage_d3 - Check if D3 should be used in the suspend path
1332 * @dev: Device to check
1334 * Return %true if the platform firmware wants @dev to be programmed
1335 * into D3hot or D3cold (if supported) in the suspend path, or %false
1336 * when there is no specific preference. On some platforms, if this
1337 * hint is ignored, @dev may remain unresponsive after suspending the
1338 * platform as a whole.
1340 * Although the property has storage in the name it actually is
1341 * applied to the PCIe slot and plugging in a non-storage device the
1342 * same platform restrictions will likely apply.
1344 bool acpi_storage_d3(struct device *dev)
1346 struct acpi_device *adev = ACPI_COMPANION(dev);
1351 if (fwnode_property_read_u8(acpi_fwnode_handle(adev), "StorageD3Enable",
1356 EXPORT_SYMBOL_GPL(acpi_storage_d3);
1358 #endif /* CONFIG_PM */