lkdtm: Add Control Flow Integrity test
[linux/fpc-iii.git] / drivers / acpi / device_pm.c
blob28cffaaf9d82ad9bc392ae0288e2ece506d5bcd7
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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>
21 #include "internal.h"
23 #define _COMPONENT ACPI_POWER_COMPONENT
24 ACPI_MODULE_NAME("device_pm");
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.
30 const char *acpi_power_state_string(int state)
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)";
48 static int acpi_dev_pm_explicit_get(struct acpi_device *device, int *state)
50 unsigned long long psc;
51 acpi_status status;
53 status = acpi_evaluate_integer(device->handle, "_PSC", NULL, &psc);
54 if (ACPI_FAILURE(status))
55 return -ENODEV;
57 *state = psc;
58 return 0;
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.
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).
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).
75 int acpi_device_get_power(struct acpi_device *device, int *state)
77 int result = ACPI_STATE_UNKNOWN;
78 int error;
80 if (!device || !state)
81 return -EINVAL;
83 if (!device->flags.power_manageable) {
84 /* TBD: Non-recursive algorithm for walking up hierarchy. */
85 *state = device->parent ?
86 device->parent->power.state : ACPI_STATE_D0;
87 goto out;
91 * Get the device's power state from power resources settings and _PSC,
92 * if available.
94 if (device->power.flags.power_resources) {
95 error = acpi_power_get_inferred_state(device, &result);
96 if (error)
97 return error;
99 if (device->power.flags.explicit_get) {
100 int psc;
102 error = acpi_dev_pm_explicit_get(device, &psc);
103 if (error)
104 return error;
107 * The power resources settings may indicate a power state
108 * shallower than the actual power state of the device, because
109 * the same power resources may be referenced by other devices.
111 * For systems predating ACPI 4.0 we assume that D3hot is the
112 * deepest state that can be supported.
114 if (psc > result && psc < ACPI_STATE_D3_COLD)
115 result = psc;
116 else if (result == ACPI_STATE_UNKNOWN)
117 result = psc > ACPI_STATE_D2 ? ACPI_STATE_D3_HOT : psc;
121 * If we were unsure about the device parent's power state up to this
122 * point, the fact that the device is in D0 implies that the parent has
123 * to be in D0 too, except if ignore_parent is set.
125 if (!device->power.flags.ignore_parent && device->parent
126 && device->parent->power.state == ACPI_STATE_UNKNOWN
127 && result == ACPI_STATE_D0)
128 device->parent->power.state = ACPI_STATE_D0;
130 *state = result;
132 out:
133 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is %s\n",
134 device->pnp.bus_id, acpi_power_state_string(*state)));
136 return 0;
139 static int acpi_dev_pm_explicit_set(struct acpi_device *adev, int state)
141 if (adev->power.states[state].flags.explicit_set) {
142 char method[5] = { '_', 'P', 'S', '0' + state, '\0' };
143 acpi_status status;
145 status = acpi_evaluate_object(adev->handle, method, NULL, NULL);
146 if (ACPI_FAILURE(status))
147 return -ENODEV;
149 return 0;
153 * acpi_device_set_power - Set power state of an ACPI device.
154 * @device: Device to set the power state of.
155 * @state: New power state to set.
157 * Callers must ensure that the device is power manageable before using this
158 * function.
160 int acpi_device_set_power(struct acpi_device *device, int state)
162 int target_state = state;
163 int result = 0;
165 if (!device || !device->flags.power_manageable
166 || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
167 return -EINVAL;
169 /* Make sure this is a valid target state */
171 /* There is a special case for D0 addressed below. */
172 if (state > ACPI_STATE_D0 && state == device->power.state) {
173 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] already in %s\n",
174 device->pnp.bus_id,
175 acpi_power_state_string(state)));
176 return 0;
179 if (state == ACPI_STATE_D3_COLD) {
181 * For transitions to D3cold we need to execute _PS3 and then
182 * possibly drop references to the power resources in use.
184 state = ACPI_STATE_D3_HOT;
185 /* If _PR3 is not available, use D3hot as the target state. */
186 if (!device->power.states[ACPI_STATE_D3_COLD].flags.valid)
187 target_state = state;
188 } else if (!device->power.states[state].flags.valid) {
189 dev_warn(&device->dev, "Power state %s not supported\n",
190 acpi_power_state_string(state));
191 return -ENODEV;
194 if (!device->power.flags.ignore_parent &&
195 device->parent && (state < device->parent->power.state)) {
196 dev_warn(&device->dev,
197 "Cannot transition to power state %s for parent in %s\n",
198 acpi_power_state_string(state),
199 acpi_power_state_string(device->parent->power.state));
200 return -ENODEV;
204 * Transition Power
205 * ----------------
206 * In accordance with ACPI 6, _PSx is executed before manipulating power
207 * resources, unless the target state is D0, in which case _PS0 is
208 * supposed to be executed after turning the power resources on.
210 if (state > ACPI_STATE_D0) {
212 * According to ACPI 6, devices cannot go from lower-power
213 * (deeper) states to higher-power (shallower) states.
215 if (state < device->power.state) {
216 dev_warn(&device->dev, "Cannot transition from %s to %s\n",
217 acpi_power_state_string(device->power.state),
218 acpi_power_state_string(state));
219 return -ENODEV;
223 * If the device goes from D3hot to D3cold, _PS3 has been
224 * evaluated for it already, so skip it in that case.
226 if (device->power.state < ACPI_STATE_D3_HOT) {
227 result = acpi_dev_pm_explicit_set(device, state);
228 if (result)
229 goto end;
232 if (device->power.flags.power_resources)
233 result = acpi_power_transition(device, target_state);
234 } else {
235 if (device->power.flags.power_resources) {
236 result = acpi_power_transition(device, ACPI_STATE_D0);
237 if (result)
238 goto end;
241 if (device->power.state == ACPI_STATE_D0) {
242 int psc;
244 /* Nothing to do here if _PSC is not present. */
245 if (!device->power.flags.explicit_get)
246 return 0;
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.
259 result = acpi_dev_pm_explicit_get(device, &psc);
260 if (result || psc == ACPI_STATE_D0)
261 return 0;
264 result = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
267 end:
268 if (result) {
269 dev_warn(&device->dev, "Failed to change power state to %s\n",
270 acpi_power_state_string(state));
271 } else {
272 device->power.state = target_state;
273 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
274 "Device [%s] transitioned to %s\n",
275 device->pnp.bus_id,
276 acpi_power_state_string(state)));
279 return result;
281 EXPORT_SYMBOL(acpi_device_set_power);
283 int acpi_bus_set_power(acpi_handle handle, int state)
285 struct acpi_device *device;
286 int result;
288 result = acpi_bus_get_device(handle, &device);
289 if (result)
290 return result;
292 return acpi_device_set_power(device, state);
294 EXPORT_SYMBOL(acpi_bus_set_power);
296 int acpi_bus_init_power(struct acpi_device *device)
298 int state;
299 int result;
301 if (!device)
302 return -EINVAL;
304 device->power.state = ACPI_STATE_UNKNOWN;
305 if (!acpi_device_is_present(device)) {
306 device->flags.initialized = false;
307 return -ENXIO;
310 result = acpi_device_get_power(device, &state);
311 if (result)
312 return result;
314 if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) {
315 /* Reference count the power resources. */
316 result = acpi_power_on_resources(device, state);
317 if (result)
318 return result;
320 if (state == ACPI_STATE_D0) {
322 * If _PSC is not present and the state inferred from
323 * power resources appears to be D0, it still may be
324 * necessary to execute _PS0 at this point, because
325 * another device using the same power resources may
326 * have been put into D0 previously and that's why we
327 * see D0 here.
329 result = acpi_dev_pm_explicit_set(device, state);
330 if (result)
331 return result;
333 } else if (state == ACPI_STATE_UNKNOWN) {
335 * No power resources and missing _PSC? Cross fingers and make
336 * it D0 in hope that this is what the BIOS put the device into.
337 * [We tried to force D0 here by executing _PS0, but that broke
338 * Toshiba P870-303 in a nasty way.]
340 state = ACPI_STATE_D0;
342 device->power.state = state;
343 return 0;
347 * acpi_device_fix_up_power - Force device with missing _PSC into D0.
348 * @device: Device object whose power state is to be fixed up.
350 * Devices without power resources and _PSC, but having _PS0 and _PS3 defined,
351 * are assumed to be put into D0 by the BIOS. However, in some cases that may
352 * not be the case and this function should be used then.
354 int acpi_device_fix_up_power(struct acpi_device *device)
356 int ret = 0;
358 if (!device->power.flags.power_resources
359 && !device->power.flags.explicit_get
360 && device->power.state == ACPI_STATE_D0)
361 ret = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
363 return ret;
365 EXPORT_SYMBOL_GPL(acpi_device_fix_up_power);
367 int acpi_device_update_power(struct acpi_device *device, int *state_p)
369 int state;
370 int result;
372 if (device->power.state == ACPI_STATE_UNKNOWN) {
373 result = acpi_bus_init_power(device);
374 if (!result && state_p)
375 *state_p = device->power.state;
377 return result;
380 result = acpi_device_get_power(device, &state);
381 if (result)
382 return result;
384 if (state == ACPI_STATE_UNKNOWN) {
385 state = ACPI_STATE_D0;
386 result = acpi_device_set_power(device, state);
387 if (result)
388 return result;
389 } else {
390 if (device->power.flags.power_resources) {
392 * We don't need to really switch the state, bu we need
393 * to update the power resources' reference counters.
395 result = acpi_power_transition(device, state);
396 if (result)
397 return result;
399 device->power.state = state;
401 if (state_p)
402 *state_p = state;
404 return 0;
406 EXPORT_SYMBOL_GPL(acpi_device_update_power);
408 int acpi_bus_update_power(acpi_handle handle, int *state_p)
410 struct acpi_device *device;
411 int result;
413 result = acpi_bus_get_device(handle, &device);
414 return result ? result : acpi_device_update_power(device, state_p);
416 EXPORT_SYMBOL_GPL(acpi_bus_update_power);
418 bool acpi_bus_power_manageable(acpi_handle handle)
420 struct acpi_device *device;
421 int result;
423 result = acpi_bus_get_device(handle, &device);
424 return result ? false : device->flags.power_manageable;
426 EXPORT_SYMBOL(acpi_bus_power_manageable);
428 #ifdef CONFIG_PM
429 static DEFINE_MUTEX(acpi_pm_notifier_lock);
430 static DEFINE_MUTEX(acpi_pm_notifier_install_lock);
432 void acpi_pm_wakeup_event(struct device *dev)
434 pm_wakeup_dev_event(dev, 0, acpi_s2idle_wakeup());
436 EXPORT_SYMBOL_GPL(acpi_pm_wakeup_event);
438 static void acpi_pm_notify_handler(acpi_handle handle, u32 val, void *not_used)
440 struct acpi_device *adev;
442 if (val != ACPI_NOTIFY_DEVICE_WAKE)
443 return;
445 acpi_handle_debug(handle, "Wake notify\n");
447 adev = acpi_bus_get_acpi_device(handle);
448 if (!adev)
449 return;
451 mutex_lock(&acpi_pm_notifier_lock);
453 if (adev->wakeup.flags.notifier_present) {
454 pm_wakeup_ws_event(adev->wakeup.ws, 0, acpi_s2idle_wakeup());
455 if (adev->wakeup.context.func) {
456 acpi_handle_debug(handle, "Running %pS for %s\n",
457 adev->wakeup.context.func,
458 dev_name(adev->wakeup.context.dev));
459 adev->wakeup.context.func(&adev->wakeup.context);
463 mutex_unlock(&acpi_pm_notifier_lock);
465 acpi_bus_put_acpi_device(adev);
469 * acpi_add_pm_notifier - Register PM notify handler for given ACPI device.
470 * @adev: ACPI device to add the notify handler for.
471 * @dev: Device to generate a wakeup event for while handling the notification.
472 * @func: Work function to execute when handling the notification.
474 * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of
475 * PM wakeup events. For example, wakeup events may be generated for bridges
476 * if one of the devices below the bridge is signaling wakeup, even if the
477 * bridge itself doesn't have a wakeup GPE associated with it.
479 acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev,
480 void (*func)(struct acpi_device_wakeup_context *context))
482 acpi_status status = AE_ALREADY_EXISTS;
484 if (!dev && !func)
485 return AE_BAD_PARAMETER;
487 mutex_lock(&acpi_pm_notifier_install_lock);
489 if (adev->wakeup.flags.notifier_present)
490 goto out;
492 status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,
493 acpi_pm_notify_handler, NULL);
494 if (ACPI_FAILURE(status))
495 goto out;
497 mutex_lock(&acpi_pm_notifier_lock);
498 adev->wakeup.ws = wakeup_source_register(dev_name(&adev->dev));
499 adev->wakeup.context.dev = dev;
500 adev->wakeup.context.func = func;
501 adev->wakeup.flags.notifier_present = true;
502 mutex_unlock(&acpi_pm_notifier_lock);
504 out:
505 mutex_unlock(&acpi_pm_notifier_install_lock);
506 return status;
510 * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
511 * @adev: ACPI device to remove the notifier from.
513 acpi_status acpi_remove_pm_notifier(struct acpi_device *adev)
515 acpi_status status = AE_BAD_PARAMETER;
517 mutex_lock(&acpi_pm_notifier_install_lock);
519 if (!adev->wakeup.flags.notifier_present)
520 goto out;
522 status = acpi_remove_notify_handler(adev->handle,
523 ACPI_SYSTEM_NOTIFY,
524 acpi_pm_notify_handler);
525 if (ACPI_FAILURE(status))
526 goto out;
528 mutex_lock(&acpi_pm_notifier_lock);
529 adev->wakeup.context.func = NULL;
530 adev->wakeup.context.dev = NULL;
531 wakeup_source_unregister(adev->wakeup.ws);
532 adev->wakeup.flags.notifier_present = false;
533 mutex_unlock(&acpi_pm_notifier_lock);
535 out:
536 mutex_unlock(&acpi_pm_notifier_install_lock);
537 return status;
540 bool acpi_bus_can_wakeup(acpi_handle handle)
542 struct acpi_device *device;
543 int result;
545 result = acpi_bus_get_device(handle, &device);
546 return result ? false : device->wakeup.flags.valid;
548 EXPORT_SYMBOL(acpi_bus_can_wakeup);
550 bool acpi_pm_device_can_wakeup(struct device *dev)
552 struct acpi_device *adev = ACPI_COMPANION(dev);
554 return adev ? acpi_device_can_wakeup(adev) : false;
558 * acpi_dev_pm_get_state - Get preferred power state of ACPI device.
559 * @dev: Device whose preferred target power state to return.
560 * @adev: ACPI device node corresponding to @dev.
561 * @target_state: System state to match the resultant device state.
562 * @d_min_p: Location to store the highest power state available to the device.
563 * @d_max_p: Location to store the lowest power state available to the device.
565 * Find the lowest power (highest number) and highest power (lowest number) ACPI
566 * device power states that the device can be in while the system is in the
567 * state represented by @target_state. Store the integer numbers representing
568 * those stats in the memory locations pointed to by @d_max_p and @d_min_p,
569 * respectively.
571 * Callers must ensure that @dev and @adev are valid pointers and that @adev
572 * actually corresponds to @dev before using this function.
574 * Returns 0 on success or -ENODATA when one of the ACPI methods fails or
575 * returns a value that doesn't make sense. The memory locations pointed to by
576 * @d_max_p and @d_min_p are only modified on success.
578 static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev,
579 u32 target_state, int *d_min_p, int *d_max_p)
581 char method[] = { '_', 'S', '0' + target_state, 'D', '\0' };
582 acpi_handle handle = adev->handle;
583 unsigned long long ret;
584 int d_min, d_max;
585 bool wakeup = false;
586 bool has_sxd = false;
587 acpi_status status;
590 * If the system state is S0, the lowest power state the device can be
591 * in is D3cold, unless the device has _S0W and is supposed to signal
592 * wakeup, in which case the return value of _S0W has to be used as the
593 * lowest power state available to the device.
595 d_min = ACPI_STATE_D0;
596 d_max = ACPI_STATE_D3_COLD;
599 * If present, _SxD methods return the minimum D-state (highest power
600 * state) we can use for the corresponding S-states. Otherwise, the
601 * minimum D-state is D0 (ACPI 3.x).
603 if (target_state > ACPI_STATE_S0) {
605 * We rely on acpi_evaluate_integer() not clobbering the integer
606 * provided if AE_NOT_FOUND is returned.
608 ret = d_min;
609 status = acpi_evaluate_integer(handle, method, NULL, &ret);
610 if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND)
611 || ret > ACPI_STATE_D3_COLD)
612 return -ENODATA;
615 * We need to handle legacy systems where D3hot and D3cold are
616 * the same and 3 is returned in both cases, so fall back to
617 * D3cold if D3hot is not a valid state.
619 if (!adev->power.states[ret].flags.valid) {
620 if (ret == ACPI_STATE_D3_HOT)
621 ret = ACPI_STATE_D3_COLD;
622 else
623 return -ENODATA;
626 if (status == AE_OK)
627 has_sxd = true;
629 d_min = ret;
630 wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
631 && adev->wakeup.sleep_state >= target_state;
632 } else {
633 wakeup = adev->wakeup.flags.valid;
637 * If _PRW says we can wake up the system from the target sleep state,
638 * the D-state returned by _SxD is sufficient for that (we assume a
639 * wakeup-aware driver if wake is set). Still, if _SxW exists
640 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
641 * can wake the system. _S0W may be valid, too.
643 if (wakeup) {
644 method[3] = 'W';
645 status = acpi_evaluate_integer(handle, method, NULL, &ret);
646 if (status == AE_NOT_FOUND) {
647 /* No _SxW. In this case, the ACPI spec says that we
648 * must not go into any power state deeper than the
649 * value returned from _SxD.
651 if (has_sxd && target_state > ACPI_STATE_S0)
652 d_max = d_min;
653 } else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) {
654 /* Fall back to D3cold if ret is not a valid state. */
655 if (!adev->power.states[ret].flags.valid)
656 ret = ACPI_STATE_D3_COLD;
658 d_max = ret > d_min ? ret : d_min;
659 } else {
660 return -ENODATA;
664 if (d_min_p)
665 *d_min_p = d_min;
667 if (d_max_p)
668 *d_max_p = d_max;
670 return 0;
674 * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
675 * @dev: Device whose preferred target power state to return.
676 * @d_min_p: Location to store the upper limit of the allowed states range.
677 * @d_max_in: Deepest low-power state to take into consideration.
678 * Return value: Preferred power state of the device on success, -ENODEV
679 * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is
680 * incorrect, or -ENODATA on ACPI method failure.
682 * The caller must ensure that @dev is valid before using this function.
684 int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
686 struct acpi_device *adev;
687 int ret, d_min, d_max;
689 if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD)
690 return -EINVAL;
692 if (d_max_in > ACPI_STATE_D2) {
693 enum pm_qos_flags_status stat;
695 stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
696 if (stat == PM_QOS_FLAGS_ALL)
697 d_max_in = ACPI_STATE_D2;
700 adev = ACPI_COMPANION(dev);
701 if (!adev) {
702 dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
703 return -ENODEV;
706 ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(),
707 &d_min, &d_max);
708 if (ret)
709 return ret;
711 if (d_max_in < d_min)
712 return -EINVAL;
714 if (d_max > d_max_in) {
715 for (d_max = d_max_in; d_max > d_min; d_max--) {
716 if (adev->power.states[d_max].flags.valid)
717 break;
721 if (d_min_p)
722 *d_min_p = d_min;
724 return d_max;
726 EXPORT_SYMBOL(acpi_pm_device_sleep_state);
729 * acpi_pm_notify_work_func - ACPI devices wakeup notification work function.
730 * @context: Device wakeup context.
732 static void acpi_pm_notify_work_func(struct acpi_device_wakeup_context *context)
734 struct device *dev = context->dev;
736 if (dev) {
737 pm_wakeup_event(dev, 0);
738 pm_request_resume(dev);
742 static DEFINE_MUTEX(acpi_wakeup_lock);
744 static int __acpi_device_wakeup_enable(struct acpi_device *adev,
745 u32 target_state, int max_count)
747 struct acpi_device_wakeup *wakeup = &adev->wakeup;
748 acpi_status status;
749 int error = 0;
751 mutex_lock(&acpi_wakeup_lock);
753 if (wakeup->enable_count >= max_count)
754 goto out;
756 if (wakeup->enable_count > 0)
757 goto inc;
759 error = acpi_enable_wakeup_device_power(adev, target_state);
760 if (error)
761 goto out;
763 status = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
764 if (ACPI_FAILURE(status)) {
765 acpi_disable_wakeup_device_power(adev);
766 error = -EIO;
767 goto out;
770 acpi_handle_debug(adev->handle, "GPE%2X enabled for wakeup\n",
771 (unsigned int)wakeup->gpe_number);
773 inc:
774 wakeup->enable_count++;
776 out:
777 mutex_unlock(&acpi_wakeup_lock);
778 return error;
782 * acpi_device_wakeup_enable - Enable wakeup functionality for device.
783 * @adev: ACPI device to enable wakeup functionality for.
784 * @target_state: State the system is transitioning into.
786 * Enable the GPE associated with @adev so that it can generate wakeup signals
787 * for the device in response to external (remote) events and enable wakeup
788 * power for it.
790 * Callers must ensure that @adev is a valid ACPI device node before executing
791 * this function.
793 static int acpi_device_wakeup_enable(struct acpi_device *adev, u32 target_state)
795 return __acpi_device_wakeup_enable(adev, target_state, 1);
799 * acpi_device_wakeup_disable - Disable wakeup functionality for device.
800 * @adev: ACPI device to disable wakeup functionality for.
802 * Disable the GPE associated with @adev and disable wakeup power for it.
804 * Callers must ensure that @adev is a valid ACPI device node before executing
805 * this function.
807 static void acpi_device_wakeup_disable(struct acpi_device *adev)
809 struct acpi_device_wakeup *wakeup = &adev->wakeup;
811 mutex_lock(&acpi_wakeup_lock);
813 if (!wakeup->enable_count)
814 goto out;
816 acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
817 acpi_disable_wakeup_device_power(adev);
819 wakeup->enable_count--;
821 out:
822 mutex_unlock(&acpi_wakeup_lock);
825 static int __acpi_pm_set_device_wakeup(struct device *dev, bool enable,
826 int max_count)
828 struct acpi_device *adev;
829 int error;
831 adev = ACPI_COMPANION(dev);
832 if (!adev) {
833 dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
834 return -ENODEV;
837 if (!acpi_device_can_wakeup(adev))
838 return -EINVAL;
840 if (!enable) {
841 acpi_device_wakeup_disable(adev);
842 dev_dbg(dev, "Wakeup disabled by ACPI\n");
843 return 0;
846 error = __acpi_device_wakeup_enable(adev, acpi_target_system_state(),
847 max_count);
848 if (!error)
849 dev_dbg(dev, "Wakeup enabled by ACPI\n");
851 return error;
855 * acpi_pm_set_device_wakeup - Enable/disable remote wakeup for given device.
856 * @dev: Device to enable/disable to generate wakeup events.
857 * @enable: Whether to enable or disable the wakeup functionality.
859 int acpi_pm_set_device_wakeup(struct device *dev, bool enable)
861 return __acpi_pm_set_device_wakeup(dev, enable, 1);
863 EXPORT_SYMBOL_GPL(acpi_pm_set_device_wakeup);
866 * acpi_pm_set_bridge_wakeup - Enable/disable remote wakeup for given bridge.
867 * @dev: Bridge device to enable/disable to generate wakeup events.
868 * @enable: Whether to enable or disable the wakeup functionality.
870 int acpi_pm_set_bridge_wakeup(struct device *dev, bool enable)
872 return __acpi_pm_set_device_wakeup(dev, enable, INT_MAX);
874 EXPORT_SYMBOL_GPL(acpi_pm_set_bridge_wakeup);
877 * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
878 * @dev: Device to put into a low-power state.
879 * @adev: ACPI device node corresponding to @dev.
880 * @system_state: System state to choose the device state for.
882 static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
883 u32 system_state)
885 int ret, state;
887 if (!acpi_device_power_manageable(adev))
888 return 0;
890 ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state);
891 return ret ? ret : acpi_device_set_power(adev, state);
895 * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
896 * @adev: ACPI device node to put into the full-power state.
898 static int acpi_dev_pm_full_power(struct acpi_device *adev)
900 return acpi_device_power_manageable(adev) ?
901 acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
905 * acpi_dev_suspend - Put device into a low-power state using ACPI.
906 * @dev: Device to put into a low-power state.
907 * @wakeup: Whether or not to enable wakeup for the device.
909 * Put the given device into a low-power state using the standard ACPI
910 * mechanism. Set up remote wakeup if desired, choose the state to put the
911 * device into (this checks if remote wakeup is expected to work too), and set
912 * the power state of the device.
914 int acpi_dev_suspend(struct device *dev, bool wakeup)
916 struct acpi_device *adev = ACPI_COMPANION(dev);
917 u32 target_state = acpi_target_system_state();
918 int error;
920 if (!adev)
921 return 0;
923 if (wakeup && acpi_device_can_wakeup(adev)) {
924 error = acpi_device_wakeup_enable(adev, target_state);
925 if (error)
926 return -EAGAIN;
927 } else {
928 wakeup = false;
931 error = acpi_dev_pm_low_power(dev, adev, target_state);
932 if (error && wakeup)
933 acpi_device_wakeup_disable(adev);
935 return error;
937 EXPORT_SYMBOL_GPL(acpi_dev_suspend);
940 * acpi_dev_resume - Put device into the full-power state using ACPI.
941 * @dev: Device to put into the full-power state.
943 * Put the given device into the full-power state using the standard ACPI
944 * mechanism. Set the power state of the device to ACPI D0 and disable wakeup.
946 int acpi_dev_resume(struct device *dev)
948 struct acpi_device *adev = ACPI_COMPANION(dev);
949 int error;
951 if (!adev)
952 return 0;
954 error = acpi_dev_pm_full_power(adev);
955 acpi_device_wakeup_disable(adev);
956 return error;
958 EXPORT_SYMBOL_GPL(acpi_dev_resume);
961 * acpi_subsys_runtime_suspend - Suspend device using ACPI.
962 * @dev: Device to suspend.
964 * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
965 * it into a runtime low-power state.
967 int acpi_subsys_runtime_suspend(struct device *dev)
969 int ret = pm_generic_runtime_suspend(dev);
970 return ret ? ret : acpi_dev_suspend(dev, true);
972 EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
975 * acpi_subsys_runtime_resume - Resume device using ACPI.
976 * @dev: Device to Resume.
978 * Use ACPI to put the given device into the full-power state and carry out the
979 * generic runtime resume procedure for it.
981 int acpi_subsys_runtime_resume(struct device *dev)
983 int ret = acpi_dev_resume(dev);
984 return ret ? ret : pm_generic_runtime_resume(dev);
986 EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
988 #ifdef CONFIG_PM_SLEEP
989 static bool acpi_dev_needs_resume(struct device *dev, struct acpi_device *adev)
991 u32 sys_target = acpi_target_system_state();
992 int ret, state;
994 if (!pm_runtime_suspended(dev) || !adev || (adev->wakeup.flags.valid &&
995 device_may_wakeup(dev) != !!adev->wakeup.prepare_count))
996 return true;
998 if (sys_target == ACPI_STATE_S0)
999 return false;
1001 if (adev->power.flags.dsw_present)
1002 return true;
1004 ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state);
1005 if (ret)
1006 return true;
1008 return state != adev->power.state;
1012 * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
1013 * @dev: Device to prepare.
1015 int acpi_subsys_prepare(struct device *dev)
1017 struct acpi_device *adev = ACPI_COMPANION(dev);
1019 if (dev->driver && dev->driver->pm && dev->driver->pm->prepare) {
1020 int ret = dev->driver->pm->prepare(dev);
1022 if (ret < 0)
1023 return ret;
1025 if (!ret && dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_PREPARE))
1026 return 0;
1029 return !acpi_dev_needs_resume(dev, adev);
1031 EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
1034 * acpi_subsys_complete - Finalize device's resume during system resume.
1035 * @dev: Device to handle.
1037 void acpi_subsys_complete(struct device *dev)
1039 pm_generic_complete(dev);
1041 * If the device had been runtime-suspended before the system went into
1042 * the sleep state it is going out of and it has never been resumed till
1043 * now, resume it in case the firmware powered it up.
1045 if (pm_runtime_suspended(dev) && pm_resume_via_firmware())
1046 pm_request_resume(dev);
1048 EXPORT_SYMBOL_GPL(acpi_subsys_complete);
1051 * acpi_subsys_suspend - Run the device driver's suspend callback.
1052 * @dev: Device to handle.
1054 * Follow PCI and resume devices from runtime suspend before running their
1055 * system suspend callbacks, unless the driver can cope with runtime-suspended
1056 * devices during system suspend and there are no ACPI-specific reasons for
1057 * resuming them.
1059 int acpi_subsys_suspend(struct device *dev)
1061 if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
1062 acpi_dev_needs_resume(dev, ACPI_COMPANION(dev)))
1063 pm_runtime_resume(dev);
1065 return pm_generic_suspend(dev);
1067 EXPORT_SYMBOL_GPL(acpi_subsys_suspend);
1070 * acpi_subsys_suspend_late - Suspend device using ACPI.
1071 * @dev: Device to suspend.
1073 * Carry out the generic late suspend procedure for @dev and use ACPI to put
1074 * it into a low-power state during system transition into a sleep state.
1076 int acpi_subsys_suspend_late(struct device *dev)
1078 int ret;
1080 if (dev_pm_smart_suspend_and_suspended(dev))
1081 return 0;
1083 ret = pm_generic_suspend_late(dev);
1084 return ret ? ret : acpi_dev_suspend(dev, device_may_wakeup(dev));
1086 EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
1089 * acpi_subsys_suspend_noirq - Run the device driver's "noirq" suspend callback.
1090 * @dev: Device to suspend.
1092 int acpi_subsys_suspend_noirq(struct device *dev)
1094 int ret;
1096 if (dev_pm_smart_suspend_and_suspended(dev)) {
1097 dev->power.may_skip_resume = true;
1098 return 0;
1101 ret = pm_generic_suspend_noirq(dev);
1102 if (ret)
1103 return ret;
1106 * If the target system sleep state is suspend-to-idle, it is sufficient
1107 * to check whether or not the device's wakeup settings are good for
1108 * runtime PM. Otherwise, the pm_resume_via_firmware() check will cause
1109 * acpi_subsys_complete() to take care of fixing up the device's state
1110 * anyway, if need be.
1112 dev->power.may_skip_resume = device_may_wakeup(dev) ||
1113 !device_can_wakeup(dev);
1115 return 0;
1117 EXPORT_SYMBOL_GPL(acpi_subsys_suspend_noirq);
1120 * acpi_subsys_resume_noirq - Run the device driver's "noirq" resume callback.
1121 * @dev: Device to handle.
1123 static int acpi_subsys_resume_noirq(struct device *dev)
1125 if (dev_pm_may_skip_resume(dev))
1126 return 0;
1129 * Devices with DPM_FLAG_SMART_SUSPEND may be left in runtime suspend
1130 * during system suspend, so update their runtime PM status to "active"
1131 * as they will be put into D0 going forward.
1133 if (dev_pm_smart_suspend_and_suspended(dev))
1134 pm_runtime_set_active(dev);
1136 return pm_generic_resume_noirq(dev);
1140 * acpi_subsys_resume_early - Resume device using ACPI.
1141 * @dev: Device to Resume.
1143 * Use ACPI to put the given device into the full-power state and carry out the
1144 * generic early resume procedure for it during system transition into the
1145 * working state.
1147 static int acpi_subsys_resume_early(struct device *dev)
1149 int ret = acpi_dev_resume(dev);
1150 return ret ? ret : pm_generic_resume_early(dev);
1154 * acpi_subsys_freeze - Run the device driver's freeze callback.
1155 * @dev: Device to handle.
1157 int acpi_subsys_freeze(struct device *dev)
1160 * Resume all runtime-suspended devices before creating a snapshot
1161 * image of system memory, because the restore kernel generally cannot
1162 * be expected to always handle them consistently and they need to be
1163 * put into the runtime-active metastate during system resume anyway,
1164 * so it is better to ensure that the state saved in the image will be
1165 * always consistent with that.
1167 pm_runtime_resume(dev);
1169 return pm_generic_freeze(dev);
1171 EXPORT_SYMBOL_GPL(acpi_subsys_freeze);
1174 * acpi_subsys_restore_early - Restore device using ACPI.
1175 * @dev: Device to restore.
1177 int acpi_subsys_restore_early(struct device *dev)
1179 int ret = acpi_dev_resume(dev);
1180 return ret ? ret : pm_generic_restore_early(dev);
1182 EXPORT_SYMBOL_GPL(acpi_subsys_restore_early);
1185 * acpi_subsys_poweroff - Run the device driver's poweroff callback.
1186 * @dev: Device to handle.
1188 * Follow PCI and resume devices from runtime suspend before running their
1189 * system poweroff callbacks, unless the driver can cope with runtime-suspended
1190 * devices during system suspend and there are no ACPI-specific reasons for
1191 * resuming them.
1193 int acpi_subsys_poweroff(struct device *dev)
1195 if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
1196 acpi_dev_needs_resume(dev, ACPI_COMPANION(dev)))
1197 pm_runtime_resume(dev);
1199 return pm_generic_poweroff(dev);
1201 EXPORT_SYMBOL_GPL(acpi_subsys_poweroff);
1204 * acpi_subsys_poweroff_late - Run the device driver's poweroff callback.
1205 * @dev: Device to handle.
1207 * Carry out the generic late poweroff procedure for @dev and use ACPI to put
1208 * it into a low-power state during system transition into a sleep state.
1210 static int acpi_subsys_poweroff_late(struct device *dev)
1212 int ret;
1214 if (dev_pm_smart_suspend_and_suspended(dev))
1215 return 0;
1217 ret = pm_generic_poweroff_late(dev);
1218 if (ret)
1219 return ret;
1221 return acpi_dev_suspend(dev, device_may_wakeup(dev));
1225 * acpi_subsys_poweroff_noirq - Run the driver's "noirq" poweroff callback.
1226 * @dev: Device to suspend.
1228 static int acpi_subsys_poweroff_noirq(struct device *dev)
1230 if (dev_pm_smart_suspend_and_suspended(dev))
1231 return 0;
1233 return pm_generic_poweroff_noirq(dev);
1235 #endif /* CONFIG_PM_SLEEP */
1237 static struct dev_pm_domain acpi_general_pm_domain = {
1238 .ops = {
1239 .runtime_suspend = acpi_subsys_runtime_suspend,
1240 .runtime_resume = acpi_subsys_runtime_resume,
1241 #ifdef CONFIG_PM_SLEEP
1242 .prepare = acpi_subsys_prepare,
1243 .complete = acpi_subsys_complete,
1244 .suspend = acpi_subsys_suspend,
1245 .suspend_late = acpi_subsys_suspend_late,
1246 .suspend_noirq = acpi_subsys_suspend_noirq,
1247 .resume_noirq = acpi_subsys_resume_noirq,
1248 .resume_early = acpi_subsys_resume_early,
1249 .freeze = acpi_subsys_freeze,
1250 .poweroff = acpi_subsys_poweroff,
1251 .poweroff_late = acpi_subsys_poweroff_late,
1252 .poweroff_noirq = acpi_subsys_poweroff_noirq,
1253 .restore_early = acpi_subsys_restore_early,
1254 #endif
1259 * acpi_dev_pm_detach - Remove ACPI power management from the device.
1260 * @dev: Device to take care of.
1261 * @power_off: Whether or not to try to remove power from the device.
1263 * Remove the device from the general ACPI PM domain and remove its wakeup
1264 * notifier. If @power_off is set, additionally remove power from the device if
1265 * possible.
1267 * Callers must ensure proper synchronization of this function with power
1268 * management callbacks.
1270 static void acpi_dev_pm_detach(struct device *dev, bool power_off)
1272 struct acpi_device *adev = ACPI_COMPANION(dev);
1274 if (adev && dev->pm_domain == &acpi_general_pm_domain) {
1275 dev_pm_domain_set(dev, NULL);
1276 acpi_remove_pm_notifier(adev);
1277 if (power_off) {
1279 * If the device's PM QoS resume latency limit or flags
1280 * have been exposed to user space, they have to be
1281 * hidden at this point, so that they don't affect the
1282 * choice of the low-power state to put the device into.
1284 dev_pm_qos_hide_latency_limit(dev);
1285 dev_pm_qos_hide_flags(dev);
1286 acpi_device_wakeup_disable(adev);
1287 acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
1293 * acpi_dev_pm_attach - Prepare device for ACPI power management.
1294 * @dev: Device to prepare.
1295 * @power_on: Whether or not to power on the device.
1297 * If @dev has a valid ACPI handle that has a valid struct acpi_device object
1298 * attached to it, install a wakeup notification handler for the device and
1299 * add it to the general ACPI PM domain. If @power_on is set, the device will
1300 * be put into the ACPI D0 state before the function returns.
1302 * This assumes that the @dev's bus type uses generic power management callbacks
1303 * (or doesn't use any power management callbacks at all).
1305 * Callers must ensure proper synchronization of this function with power
1306 * management callbacks.
1308 int acpi_dev_pm_attach(struct device *dev, bool power_on)
1310 struct acpi_device *adev = ACPI_COMPANION(dev);
1312 if (!adev)
1313 return 0;
1316 * Only attach the power domain to the first device if the
1317 * companion is shared by multiple. This is to prevent doing power
1318 * management twice.
1320 if (!acpi_device_is_first_physical_node(adev, dev))
1321 return 0;
1323 acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func);
1324 dev_pm_domain_set(dev, &acpi_general_pm_domain);
1325 if (power_on) {
1326 acpi_dev_pm_full_power(adev);
1327 acpi_device_wakeup_disable(adev);
1330 dev->pm_domain->detach = acpi_dev_pm_detach;
1331 return 1;
1333 EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
1334 #endif /* CONFIG_PM */