Merge tag 'dma-mapping-5.3-1' of git://git.infradead.org/users/hch/dma-mapping
[linux-2.6-block.git] / drivers / acpi / device_pm.c
CommitLineData
1802d0be 1// SPDX-License-Identifier: GPL-2.0-only
ec2cd81c
RW
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 *
ec2cd81c
RW
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 */
12
7b199811 13#include <linux/acpi.h>
86b3832c 14#include <linux/export.h>
ec2cd81c 15#include <linux/mutex.h>
86b3832c 16#include <linux/pm_qos.h>
989561de 17#include <linux/pm_domain.h>
cd7bd02d 18#include <linux/pm_runtime.h>
33e4f80e 19#include <linux/suspend.h>
ec2cd81c 20
9ce4e607
RW
21#include "internal.h"
22
23#define _COMPONENT ACPI_POWER_COMPONENT
24ACPI_MODULE_NAME("device_pm");
ec2cd81c 25
9ce4e607
RW
26/**
27 * acpi_power_state_string - String representation of ACPI device power state.
28 * @state: ACPI device power state to return the string representation of.
29 */
30const char *acpi_power_state_string(int state)
31{
32 switch (state) {
33 case ACPI_STATE_D0:
34 return "D0";
35 case ACPI_STATE_D1:
36 return "D1";
37 case ACPI_STATE_D2:
38 return "D2";
39 case ACPI_STATE_D3_HOT:
40 return "D3hot";
41 case ACPI_STATE_D3_COLD:
898fee4f 42 return "D3cold";
9ce4e607
RW
43 default:
44 return "(unknown)";
45 }
46}
47
f850a48a
RW
48static int acpi_dev_pm_explicit_get(struct acpi_device *device, int *state)
49{
50 unsigned long long psc;
51 acpi_status status;
52
53 status = acpi_evaluate_integer(device->handle, "_PSC", NULL, &psc);
54 if (ACPI_FAILURE(status))
55 return -ENODEV;
56
57 *state = psc;
58 return 0;
59}
60
9ce4e607
RW
61/**
62 * acpi_device_get_power - Get power state of an ACPI device.
63 * @device: Device to get the power state of.
64 * @state: Place to store the power state of the device.
65 *
66 * This function does not update the device's power.state field, but it may
67 * update its parent's power.state field (when the parent's power state is
68 * unknown and the device's power state turns out to be D0).
9ed411c0
RW
69 *
70 * Also, it does not update power resource reference counters to ensure that
71 * the power state returned by it will be persistent and it may return a power
72 * state shallower than previously set by acpi_device_set_power() for @device
73 * (if that power state depends on any power resources).
9ce4e607
RW
74 */
75int acpi_device_get_power(struct acpi_device *device, int *state)
76{
77 int result = ACPI_STATE_UNKNOWN;
f850a48a 78 int error;
9ce4e607
RW
79
80 if (!device || !state)
81 return -EINVAL;
82
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;
88 }
89
90 /*
75eb2d13
RW
91 * Get the device's power state from power resources settings and _PSC,
92 * if available.
9ce4e607 93 */
75eb2d13 94 if (device->power.flags.power_resources) {
f850a48a 95 error = acpi_power_get_inferred_state(device, &result);
75eb2d13
RW
96 if (error)
97 return error;
98 }
9ce4e607 99 if (device->power.flags.explicit_get) {
f850a48a 100 int psc;
75eb2d13 101
f850a48a
RW
102 error = acpi_dev_pm_explicit_get(device, &psc);
103 if (error)
104 return error;
9ce4e607 105
75eb2d13
RW
106 /*
107 * The power resources settings may indicate a power state
20dacb71
RW
108 * shallower than the actual power state of the device, because
109 * the same power resources may be referenced by other devices.
75eb2d13 110 *
20dacb71
RW
111 * For systems predating ACPI 4.0 we assume that D3hot is the
112 * deepest state that can be supported.
75eb2d13
RW
113 */
114 if (psc > result && psc < ACPI_STATE_D3_COLD)
115 result = psc;
116 else if (result == ACPI_STATE_UNKNOWN)
20dacb71 117 result = psc > ACPI_STATE_D2 ? ACPI_STATE_D3_HOT : psc;
9ce4e607
RW
118 }
119
120 /*
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
644f17ad 123 * to be in D0 too, except if ignore_parent is set.
9ce4e607 124 */
644f17ad
MW
125 if (!device->power.flags.ignore_parent && device->parent
126 && device->parent->power.state == ACPI_STATE_UNKNOWN
9ce4e607
RW
127 && result == ACPI_STATE_D0)
128 device->parent->power.state = ACPI_STATE_D0;
129
130 *state = result;
131
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)));
135
136 return 0;
137}
138
9c0f45e3
RW
139static int acpi_dev_pm_explicit_set(struct acpi_device *adev, int state)
140{
141 if (adev->power.states[state].flags.explicit_set) {
142 char method[5] = { '_', 'P', 'S', '0' + state, '\0' };
143 acpi_status status;
144
145 status = acpi_evaluate_object(adev->handle, method, NULL, NULL);
146 if (ACPI_FAILURE(status))
147 return -ENODEV;
148 }
149 return 0;
150}
151
9ce4e607
RW
152/**
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.
156 *
157 * Callers must ensure that the device is power manageable before using this
158 * function.
159 */
160int acpi_device_set_power(struct acpi_device *device, int state)
161{
20dacb71 162 int target_state = state;
9ce4e607 163 int result = 0;
9ce4e607 164
2c7d132a
RW
165 if (!device || !device->flags.power_manageable
166 || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
9ce4e607
RW
167 return -EINVAL;
168
169 /* Make sure this is a valid target state */
170
f850a48a
RW
171 /* There is a special case for D0 addressed below. */
172 if (state > ACPI_STATE_D0 && state == device->power.state) {
b69137a7
RW
173 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] already in %s\n",
174 device->pnp.bus_id,
9ce4e607
RW
175 acpi_power_state_string(state)));
176 return 0;
177 }
178
20dacb71
RW
179 if (state == ACPI_STATE_D3_COLD) {
180 /*
181 * For transitions to D3cold we need to execute _PS3 and then
182 * possibly drop references to the power resources in use.
183 */
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) {
b69137a7
RW
189 dev_warn(&device->dev, "Power state %s not supported\n",
190 acpi_power_state_string(state));
9ce4e607
RW
191 return -ENODEV;
192 }
20dacb71 193
644f17ad
MW
194 if (!device->power.flags.ignore_parent &&
195 device->parent && (state < device->parent->power.state)) {
b69137a7 196 dev_warn(&device->dev,
593298e6
AL
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));
9ce4e607
RW
200 return -ENODEV;
201 }
202
9ce4e607
RW
203 /*
204 * Transition Power
205 * ----------------
20dacb71
RW
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.
9ce4e607 209 */
20dacb71
RW
210 if (state > ACPI_STATE_D0) {
211 /*
212 * According to ACPI 6, devices cannot go from lower-power
213 * (deeper) states to higher-power (shallower) states.
214 */
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;
220 }
221
21ba2379
RW
222 /*
223 * If the device goes from D3hot to D3cold, _PS3 has been
224 * evaluated for it already, so skip it in that case.
225 */
226 if (device->power.state < ACPI_STATE_D3_HOT) {
227 result = acpi_dev_pm_explicit_set(device, state);
228 if (result)
229 goto end;
230 }
9ce4e607 231
20dacb71
RW
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;
239 }
f850a48a
RW
240
241 if (device->power.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 return 0;
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 return 0;
262 }
263
20dacb71 264 result = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
e5656271 265 }
9ce4e607 266
e78adb75
RW
267 end:
268 if (result) {
b69137a7
RW
269 dev_warn(&device->dev, "Failed to change power state to %s\n",
270 acpi_power_state_string(state));
e78adb75 271 } else {
71b65445 272 device->power.state = target_state;
9ce4e607
RW
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)));
277 }
278
279 return result;
280}
281EXPORT_SYMBOL(acpi_device_set_power);
282
283int acpi_bus_set_power(acpi_handle handle, int state)
284{
285 struct acpi_device *device;
286 int result;
287
288 result = acpi_bus_get_device(handle, &device);
289 if (result)
290 return result;
291
9ce4e607
RW
292 return acpi_device_set_power(device, state);
293}
294EXPORT_SYMBOL(acpi_bus_set_power);
295
296int acpi_bus_init_power(struct acpi_device *device)
297{
298 int state;
299 int result;
300
301 if (!device)
302 return -EINVAL;
303
304 device->power.state = ACPI_STATE_UNKNOWN;
cde1f95f
SA
305 if (!acpi_device_is_present(device)) {
306 device->flags.initialized = false;
1b1f3e16 307 return -ENXIO;
cde1f95f 308 }
9ce4e607
RW
309
310 result = acpi_device_get_power(device, &state);
311 if (result)
312 return result;
313
a2367807 314 if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) {
20dacb71 315 /* Reference count the power resources. */
9ce4e607 316 result = acpi_power_on_resources(device, state);
a2367807
RW
317 if (result)
318 return result;
9ce4e607 319
20dacb71
RW
320 if (state == ACPI_STATE_D0) {
321 /*
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.
328 */
329 result = acpi_dev_pm_explicit_set(device, state);
330 if (result)
331 return result;
332 }
b3785492 333 } else if (state == ACPI_STATE_UNKNOWN) {
7cd8407d
RW
334 /*
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.]
339 */
b3785492 340 state = ACPI_STATE_D0;
a2367807
RW
341 }
342 device->power.state = state;
343 return 0;
9ce4e607
RW
344}
345
b9e95fc6
RW
346/**
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.
349 *
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.
353 */
354int acpi_device_fix_up_power(struct acpi_device *device)
355{
356 int ret = 0;
357
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);
362
363 return ret;
364}
78a898d0 365EXPORT_SYMBOL_GPL(acpi_device_fix_up_power);
b9e95fc6 366
202317a5 367int acpi_device_update_power(struct acpi_device *device, int *state_p)
9ce4e607 368{
9ce4e607
RW
369 int state;
370 int result;
371
202317a5
RW
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;
376
9ce4e607 377 return result;
202317a5 378 }
9ce4e607
RW
379
380 result = acpi_device_get_power(device, &state);
381 if (result)
382 return result;
383
91bdad0b 384 if (state == ACPI_STATE_UNKNOWN) {
511d5c42 385 state = ACPI_STATE_D0;
91bdad0b
RW
386 result = acpi_device_set_power(device, state);
387 if (result)
388 return result;
389 } else {
390 if (device->power.flags.power_resources) {
391 /*
392 * We don't need to really switch the state, bu we need
393 * to update the power resources' reference counters.
394 */
395 result = acpi_power_transition(device, state);
396 if (result)
397 return result;
398 }
399 device->power.state = state;
400 }
401 if (state_p)
9ce4e607
RW
402 *state_p = state;
403
91bdad0b 404 return 0;
9ce4e607 405}
2bb3a2bf 406EXPORT_SYMBOL_GPL(acpi_device_update_power);
202317a5
RW
407
408int acpi_bus_update_power(acpi_handle handle, int *state_p)
409{
410 struct acpi_device *device;
411 int result;
412
413 result = acpi_bus_get_device(handle, &device);
414 return result ? result : acpi_device_update_power(device, state_p);
415}
9ce4e607
RW
416EXPORT_SYMBOL_GPL(acpi_bus_update_power);
417
418bool acpi_bus_power_manageable(acpi_handle handle)
419{
420 struct acpi_device *device;
421 int result;
422
423 result = acpi_bus_get_device(handle, &device);
424 return result ? false : device->flags.power_manageable;
425}
426EXPORT_SYMBOL(acpi_bus_power_manageable);
427
ec4602a9
RW
428#ifdef CONFIG_PM
429static DEFINE_MUTEX(acpi_pm_notifier_lock);
ff165679 430static DEFINE_MUTEX(acpi_pm_notifier_install_lock);
ec4602a9 431
33e4f80e
RW
432void acpi_pm_wakeup_event(struct device *dev)
433{
434 pm_wakeup_dev_event(dev, 0, acpi_s2idle_wakeup());
435}
436EXPORT_SYMBOL_GPL(acpi_pm_wakeup_event);
437
c072530f
RW
438static void acpi_pm_notify_handler(acpi_handle handle, u32 val, void *not_used)
439{
440 struct acpi_device *adev;
441
442 if (val != ACPI_NOTIFY_DEVICE_WAKE)
443 return;
444
020a6375
RW
445 acpi_handle_debug(handle, "Wake notify\n");
446
c072530f
RW
447 adev = acpi_bus_get_acpi_device(handle);
448 if (!adev)
449 return;
450
451 mutex_lock(&acpi_pm_notifier_lock);
452
453 if (adev->wakeup.flags.notifier_present) {
33e4f80e 454 pm_wakeup_ws_event(adev->wakeup.ws, 0, acpi_s2idle_wakeup());
020a6375 455 if (adev->wakeup.context.func) {
d75f773c 456 acpi_handle_debug(handle, "Running %pS for %s\n",
020a6375
RW
457 adev->wakeup.context.func,
458 dev_name(adev->wakeup.context.dev));
64fd1c70 459 adev->wakeup.context.func(&adev->wakeup.context);
020a6375 460 }
c072530f
RW
461 }
462
463 mutex_unlock(&acpi_pm_notifier_lock);
464
465 acpi_bus_put_acpi_device(adev);
466}
467
ec4602a9 468/**
c072530f
RW
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.
64fd1c70 472 * @func: Work function to execute when handling the notification.
ec4602a9
RW
473 *
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.
478 */
c072530f 479acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev,
64fd1c70 480 void (*func)(struct acpi_device_wakeup_context *context))
ec4602a9
RW
481{
482 acpi_status status = AE_ALREADY_EXISTS;
483
64fd1c70 484 if (!dev && !func)
c072530f
RW
485 return AE_BAD_PARAMETER;
486
ff165679 487 mutex_lock(&acpi_pm_notifier_install_lock);
ec4602a9
RW
488
489 if (adev->wakeup.flags.notifier_present)
490 goto out;
491
c072530f
RW
492 status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,
493 acpi_pm_notify_handler, NULL);
ec4602a9
RW
494 if (ACPI_FAILURE(status))
495 goto out;
496
ff165679
VS
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;
ec4602a9 501 adev->wakeup.flags.notifier_present = true;
ff165679 502 mutex_unlock(&acpi_pm_notifier_lock);
ec4602a9
RW
503
504 out:
ff165679 505 mutex_unlock(&acpi_pm_notifier_install_lock);
ec4602a9
RW
506 return status;
507}
508
509/**
510 * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
511 * @adev: ACPI device to remove the notifier from.
512 */
c072530f 513acpi_status acpi_remove_pm_notifier(struct acpi_device *adev)
ec4602a9
RW
514{
515 acpi_status status = AE_BAD_PARAMETER;
516
ff165679 517 mutex_lock(&acpi_pm_notifier_install_lock);
ec4602a9
RW
518
519 if (!adev->wakeup.flags.notifier_present)
520 goto out;
521
522 status = acpi_remove_notify_handler(adev->handle,
523 ACPI_SYSTEM_NOTIFY,
c072530f 524 acpi_pm_notify_handler);
ec4602a9
RW
525 if (ACPI_FAILURE(status))
526 goto out;
527
ff165679 528 mutex_lock(&acpi_pm_notifier_lock);
64fd1c70 529 adev->wakeup.context.func = NULL;
c072530f
RW
530 adev->wakeup.context.dev = NULL;
531 wakeup_source_unregister(adev->wakeup.ws);
ec4602a9 532 adev->wakeup.flags.notifier_present = false;
ff165679 533 mutex_unlock(&acpi_pm_notifier_lock);
ec4602a9
RW
534
535 out:
ff165679 536 mutex_unlock(&acpi_pm_notifier_install_lock);
ec4602a9
RW
537 return status;
538}
539
9ce4e607
RW
540bool acpi_bus_can_wakeup(acpi_handle handle)
541{
542 struct acpi_device *device;
543 int result;
544
545 result = acpi_bus_get_device(handle, &device);
546 return result ? false : device->wakeup.flags.valid;
547}
548EXPORT_SYMBOL(acpi_bus_can_wakeup);
549
8370c2dc
RW
550bool acpi_pm_device_can_wakeup(struct device *dev)
551{
552 struct acpi_device *adev = ACPI_COMPANION(dev);
553
554 return adev ? acpi_device_can_wakeup(adev) : false;
555}
556
86b3832c 557/**
b25c77ef 558 * acpi_dev_pm_get_state - Get preferred power state of ACPI device.
86b3832c
RW
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.
fa1675b5
RW
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.
86b3832c 564 *
fa1675b5
RW
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.
86b3832c
RW
570 *
571 * Callers must ensure that @dev and @adev are valid pointers and that @adev
572 * actually corresponds to @dev before using this function.
fa1675b5
RW
573 *
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.
86b3832c 577 */
b25c77ef 578static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev,
fa1675b5 579 u32 target_state, int *d_min_p, int *d_max_p)
86b3832c 580{
fa1675b5
RW
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;
86b3832c 585 bool wakeup = false;
bf8c6184 586 bool has_sxd = false;
fa1675b5 587 acpi_status status;
86b3832c 588
86b3832c 589 /*
fa1675b5
RW
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.
86b3832c
RW
594 */
595 d_min = ACPI_STATE_D0;
4c164ae7 596 d_max = ACPI_STATE_D3_COLD;
86b3832c
RW
597
598 /*
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).
86b3832c
RW
602 */
603 if (target_state > ACPI_STATE_S0) {
fa1675b5
RW
604 /*
605 * We rely on acpi_evaluate_integer() not clobbering the integer
606 * provided if AE_NOT_FOUND is returned.
607 */
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;
613
614 /*
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.
618 */
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;
624 }
bf8c6184
DD
625
626 if (status == AE_OK)
627 has_sxd = true;
628
fa1675b5 629 d_min = ret;
86b3832c
RW
630 wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
631 && adev->wakeup.sleep_state >= target_state;
20f97caf 632 } else {
86b3832c
RW
633 wakeup = adev->wakeup.flags.valid;
634 }
635
636 /*
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.
642 */
643 if (wakeup) {
fa1675b5
RW
644 method[3] = 'W';
645 status = acpi_evaluate_integer(handle, method, NULL, &ret);
646 if (status == AE_NOT_FOUND) {
bf8c6184
DD
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.
650 */
651 if (has_sxd && target_state > ACPI_STATE_S0)
86b3832c 652 d_max = d_min;
fa1675b5
RW
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;
657
658 d_max = ret > d_min ? ret : d_min;
659 } else {
660 return -ENODATA;
86b3832c
RW
661 }
662 }
663
86b3832c
RW
664 if (d_min_p)
665 *d_min_p = d_min;
fa1675b5
RW
666
667 if (d_max_p)
668 *d_max_p = d_max;
669
670 return 0;
86b3832c 671}
cd7bd02d 672
a6ae7594
RW
673/**
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
fa1675b5
RW
679 * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is
680 * incorrect, or -ENODATA on ACPI method failure.
a6ae7594
RW
681 *
682 * The caller must ensure that @dev is valid before using this function.
683 */
684int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
685{
a6ae7594 686 struct acpi_device *adev;
9b5c7a5a 687 int ret, d_min, d_max;
fa1675b5
RW
688
689 if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD)
690 return -EINVAL;
691
20dacb71 692 if (d_max_in > ACPI_STATE_D2) {
fa1675b5
RW
693 enum pm_qos_flags_status stat;
694
695 stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
696 if (stat == PM_QOS_FLAGS_ALL)
20dacb71 697 d_max_in = ACPI_STATE_D2;
fa1675b5 698 }
a6ae7594 699
17653a3e
RW
700 adev = ACPI_COMPANION(dev);
701 if (!adev) {
702 dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
a6ae7594
RW
703 return -ENODEV;
704 }
705
fa1675b5 706 ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(),
9b5c7a5a 707 &d_min, &d_max);
fa1675b5
RW
708 if (ret)
709 return ret;
710
9b5c7a5a 711 if (d_max_in < d_min)
fa1675b5
RW
712 return -EINVAL;
713
714 if (d_max > d_max_in) {
9b5c7a5a 715 for (d_max = d_max_in; d_max > d_min; d_max--) {
fa1675b5
RW
716 if (adev->power.states[d_max].flags.valid)
717 break;
718 }
719 }
9b5c7a5a
RW
720
721 if (d_min_p)
722 *d_min_p = d_min;
723
fa1675b5 724 return d_max;
a6ae7594
RW
725}
726EXPORT_SYMBOL(acpi_pm_device_sleep_state);
727
e5cc8ef3 728/**
c072530f 729 * acpi_pm_notify_work_func - ACPI devices wakeup notification work function.
64fd1c70 730 * @context: Device wakeup context.
e5cc8ef3 731 */
64fd1c70 732static void acpi_pm_notify_work_func(struct acpi_device_wakeup_context *context)
e5cc8ef3 733{
64fd1c70 734 struct device *dev = context->dev;
e5cc8ef3 735
c072530f 736 if (dev) {
e5cc8ef3 737 pm_wakeup_event(dev, 0);
64fd1c70 738 pm_request_resume(dev);
e5cc8ef3
RW
739 }
740}
741
99d8845e
RW
742static DEFINE_MUTEX(acpi_wakeup_lock);
743
1ba51a7c
RW
744static int __acpi_device_wakeup_enable(struct acpi_device *adev,
745 u32 target_state, int max_count)
dee8370c
RW
746{
747 struct acpi_device_wakeup *wakeup = &adev->wakeup;
99d8845e
RW
748 acpi_status status;
749 int error = 0;
dee8370c 750
99d8845e 751 mutex_lock(&acpi_wakeup_lock);
dee8370c 752
1ba51a7c 753 if (wakeup->enable_count >= max_count)
99d8845e 754 goto out;
235d81a6 755
1ba51a7c
RW
756 if (wakeup->enable_count > 0)
757 goto inc;
758
99d8845e
RW
759 error = acpi_enable_wakeup_device_power(adev, target_state);
760 if (error)
761 goto out;
dee8370c 762
99d8845e
RW
763 status = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
764 if (ACPI_FAILURE(status)) {
dee8370c 765 acpi_disable_wakeup_device_power(adev);
99d8845e
RW
766 error = -EIO;
767 goto out;
dee8370c 768 }
99d8845e 769
fbc9418f
RW
770 acpi_handle_debug(adev->handle, "GPE%2X enabled for wakeup\n",
771 (unsigned int)wakeup->gpe_number);
772
1ba51a7c 773inc:
99d8845e
RW
774 wakeup->enable_count++;
775
776out:
777 mutex_unlock(&acpi_wakeup_lock);
778 return error;
779}
780
1ba51a7c
RW
781/**
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.
785 *
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.
789 *
790 * Callers must ensure that @adev is a valid ACPI device node before executing
791 * this function.
792 */
793static int acpi_device_wakeup_enable(struct acpi_device *adev, u32 target_state)
794{
795 return __acpi_device_wakeup_enable(adev, target_state, 1);
796}
797
99d8845e
RW
798/**
799 * acpi_device_wakeup_disable - Disable wakeup functionality for device.
800 * @adev: ACPI device to disable wakeup functionality for.
801 *
802 * Disable the GPE associated with @adev and disable wakeup power for it.
803 *
804 * Callers must ensure that @adev is a valid ACPI device node before executing
805 * this function.
806 */
807static void acpi_device_wakeup_disable(struct acpi_device *adev)
808{
809 struct acpi_device_wakeup *wakeup = &adev->wakeup;
810
811 mutex_lock(&acpi_wakeup_lock);
812
813 if (!wakeup->enable_count)
814 goto out;
815
816 acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
817 acpi_disable_wakeup_device_power(adev);
818
819 wakeup->enable_count--;
820
821out:
822 mutex_unlock(&acpi_wakeup_lock);
dee8370c
RW
823}
824
1ba51a7c
RW
825static int __acpi_pm_set_device_wakeup(struct device *dev, bool enable,
826 int max_count)
a6ae7594 827{
a6ae7594
RW
828 struct acpi_device *adev;
829 int error;
830
17653a3e
RW
831 adev = ACPI_COMPANION(dev);
832 if (!adev) {
833 dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
a6ae7594
RW
834 return -ENODEV;
835 }
836
4d183d04
RW
837 if (!acpi_device_can_wakeup(adev))
838 return -EINVAL;
839
99d8845e
RW
840 if (!enable) {
841 acpi_device_wakeup_disable(adev);
842 dev_dbg(dev, "Wakeup disabled by ACPI\n");
843 return 0;
844 }
845
1ba51a7c
RW
846 error = __acpi_device_wakeup_enable(adev, acpi_target_system_state(),
847 max_count);
a6ae7594 848 if (!error)
99d8845e 849 dev_dbg(dev, "Wakeup enabled by ACPI\n");
a6ae7594
RW
850
851 return error;
852}
1ba51a7c
RW
853
854/**
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.
858 */
859int acpi_pm_set_device_wakeup(struct device *dev, bool enable)
860{
861 return __acpi_pm_set_device_wakeup(dev, enable, 1);
862}
863EXPORT_SYMBOL_GPL(acpi_pm_set_device_wakeup);
864
865/**
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.
869 */
870int acpi_pm_set_bridge_wakeup(struct device *dev, bool enable)
871{
872 return __acpi_pm_set_device_wakeup(dev, enable, INT_MAX);
873}
874EXPORT_SYMBOL_GPL(acpi_pm_set_bridge_wakeup);
e5cc8ef3 875
e5cc8ef3
RW
876/**
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.
881 */
882static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
883 u32 system_state)
884{
fa1675b5 885 int ret, state;
e5cc8ef3
RW
886
887 if (!acpi_device_power_manageable(adev))
888 return 0;
889
fa1675b5
RW
890 ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state);
891 return ret ? ret : acpi_device_set_power(adev, state);
e5cc8ef3
RW
892}
893
894/**
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.
897 */
898static int acpi_dev_pm_full_power(struct acpi_device *adev)
899{
900 return acpi_device_power_manageable(adev) ?
901 acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
902}
903
e5cc8ef3 904/**
cbe25ce3 905 * acpi_dev_suspend - Put device into a low-power state using ACPI.
e5cc8ef3 906 * @dev: Device to put into a low-power state.
cbe25ce3 907 * @wakeup: Whether or not to enable wakeup for the device.
e5cc8ef3 908 *
cbe25ce3 909 * Put the given device into a low-power state using the standard ACPI
e5cc8ef3
RW
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.
913 */
cbe25ce3 914int acpi_dev_suspend(struct device *dev, bool wakeup)
e5cc8ef3 915{
79c0373f 916 struct acpi_device *adev = ACPI_COMPANION(dev);
cbe25ce3 917 u32 target_state = acpi_target_system_state();
e5cc8ef3
RW
918 int error;
919
920 if (!adev)
921 return 0;
922
cbe25ce3
RW
923 if (wakeup && acpi_device_can_wakeup(adev)) {
924 error = acpi_device_wakeup_enable(adev, target_state);
99d8845e
RW
925 if (error)
926 return -EAGAIN;
cbe25ce3
RW
927 } else {
928 wakeup = false;
99d8845e 929 }
e5cc8ef3 930
cbe25ce3
RW
931 error = acpi_dev_pm_low_power(dev, adev, target_state);
932 if (error && wakeup)
99d8845e 933 acpi_device_wakeup_disable(adev);
e5cc8ef3
RW
934
935 return error;
936}
cbe25ce3 937EXPORT_SYMBOL_GPL(acpi_dev_suspend);
e5cc8ef3
RW
938
939/**
63705c40 940 * acpi_dev_resume - Put device into the full-power state using ACPI.
e5cc8ef3
RW
941 * @dev: Device to put into the full-power state.
942 *
943 * Put the given device into the full-power state using the standard ACPI
63705c40 944 * mechanism. Set the power state of the device to ACPI D0 and disable wakeup.
e5cc8ef3 945 */
63705c40 946int acpi_dev_resume(struct device *dev)
e5cc8ef3 947{
79c0373f 948 struct acpi_device *adev = ACPI_COMPANION(dev);
e5cc8ef3
RW
949 int error;
950
951 if (!adev)
952 return 0;
953
954 error = acpi_dev_pm_full_power(adev);
99d8845e 955 acpi_device_wakeup_disable(adev);
e5cc8ef3
RW
956 return error;
957}
63705c40 958EXPORT_SYMBOL_GPL(acpi_dev_resume);
e5cc8ef3
RW
959
960/**
961 * acpi_subsys_runtime_suspend - Suspend device using ACPI.
962 * @dev: Device to suspend.
963 *
964 * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
965 * it into a runtime low-power state.
966 */
967int acpi_subsys_runtime_suspend(struct device *dev)
968{
969 int ret = pm_generic_runtime_suspend(dev);
cbe25ce3 970 return ret ? ret : acpi_dev_suspend(dev, true);
e5cc8ef3
RW
971}
972EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
973
974/**
975 * acpi_subsys_runtime_resume - Resume device using ACPI.
976 * @dev: Device to Resume.
977 *
978 * Use ACPI to put the given device into the full-power state and carry out the
979 * generic runtime resume procedure for it.
980 */
981int acpi_subsys_runtime_resume(struct device *dev)
982{
63705c40 983 int ret = acpi_dev_resume(dev);
e5cc8ef3
RW
984 return ret ? ret : pm_generic_runtime_resume(dev);
985}
986EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
e5cc8ef3
RW
987
988#ifdef CONFIG_PM_SLEEP
c2ebf788
UH
989static bool acpi_dev_needs_resume(struct device *dev, struct acpi_device *adev)
990{
991 u32 sys_target = acpi_target_system_state();
992 int ret, state;
993
9a51c6b1
RW
994 if (!pm_runtime_suspended(dev) || !adev || (adev->wakeup.flags.valid &&
995 device_may_wakeup(dev) != !!adev->wakeup.prepare_count))
c2ebf788
UH
996 return true;
997
998 if (sys_target == ACPI_STATE_S0)
999 return false;
1000
1001 if (adev->power.flags.dsw_present)
1002 return true;
1003
1004 ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state);
1005 if (ret)
1006 return true;
1007
1008 return state != adev->power.state;
1009}
1010
e5cc8ef3
RW
1011/**
1012 * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
1013 * @dev: Device to prepare.
1014 */
1015int acpi_subsys_prepare(struct device *dev)
1016{
f25c0ae2 1017 struct acpi_device *adev = ACPI_COMPANION(dev);
f25c0ae2 1018
08810a41
RW
1019 if (dev->driver && dev->driver->pm && dev->driver->pm->prepare) {
1020 int ret = dev->driver->pm->prepare(dev);
f25c0ae2 1021
08810a41
RW
1022 if (ret < 0)
1023 return ret;
1024
1025 if (!ret && dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_PREPARE))
1026 return 0;
1027 }
f25c0ae2 1028
c2ebf788 1029 return !acpi_dev_needs_resume(dev, adev);
e5cc8ef3
RW
1030}
1031EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
1032
e4da817d
UH
1033/**
1034 * acpi_subsys_complete - Finalize device's resume during system resume.
1035 * @dev: Device to handle.
1036 */
1037void acpi_subsys_complete(struct device *dev)
1038{
1039 pm_generic_complete(dev);
1040 /*
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.
1044 */
db68daff 1045 if (pm_runtime_suspended(dev) && pm_resume_via_firmware())
e4da817d
UH
1046 pm_request_resume(dev);
1047}
1048EXPORT_SYMBOL_GPL(acpi_subsys_complete);
1049
92858c47
RW
1050/**
1051 * acpi_subsys_suspend - Run the device driver's suspend callback.
1052 * @dev: Device to handle.
1053 *
05087360
RW
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.
92858c47
RW
1058 */
1059int acpi_subsys_suspend(struct device *dev)
1060{
05087360
RW
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);
1064
92858c47
RW
1065 return pm_generic_suspend(dev);
1066}
4cf563c5 1067EXPORT_SYMBOL_GPL(acpi_subsys_suspend);
92858c47 1068
e5cc8ef3
RW
1069/**
1070 * acpi_subsys_suspend_late - Suspend device using ACPI.
1071 * @dev: Device to suspend.
1072 *
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.
1075 */
1076int acpi_subsys_suspend_late(struct device *dev)
1077{
05087360
RW
1078 int ret;
1079
1080 if (dev_pm_smart_suspend_and_suspended(dev))
1081 return 0;
1082
1083 ret = pm_generic_suspend_late(dev);
cbe25ce3 1084 return ret ? ret : acpi_dev_suspend(dev, device_may_wakeup(dev));
e5cc8ef3
RW
1085}
1086EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
1087
05087360
RW
1088/**
1089 * acpi_subsys_suspend_noirq - Run the device driver's "noirq" suspend callback.
1090 * @dev: Device to suspend.
1091 */
1092int acpi_subsys_suspend_noirq(struct device *dev)
1093{
db68daff
RW
1094 int ret;
1095
1096 if (dev_pm_smart_suspend_and_suspended(dev)) {
1097 dev->power.may_skip_resume = true;
05087360 1098 return 0;
db68daff
RW
1099 }
1100
1101 ret = pm_generic_suspend_noirq(dev);
1102 if (ret)
1103 return ret;
05087360 1104
db68daff
RW
1105 /*
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.
1111 */
1112 dev->power.may_skip_resume = device_may_wakeup(dev) ||
1113 !device_can_wakeup(dev);
1114
1115 return 0;
05087360
RW
1116}
1117EXPORT_SYMBOL_GPL(acpi_subsys_suspend_noirq);
1118
1119/**
1120 * acpi_subsys_resume_noirq - Run the device driver's "noirq" resume callback.
1121 * @dev: Device to handle.
1122 */
3cd7957e 1123static int acpi_subsys_resume_noirq(struct device *dev)
05087360 1124{
db68daff
RW
1125 if (dev_pm_may_skip_resume(dev))
1126 return 0;
1127
05087360
RW
1128 /*
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.
1132 */
1133 if (dev_pm_smart_suspend_and_suspended(dev))
1134 pm_runtime_set_active(dev);
1135
1136 return pm_generic_resume_noirq(dev);
1137}
05087360 1138
e5cc8ef3
RW
1139/**
1140 * acpi_subsys_resume_early - Resume device using ACPI.
1141 * @dev: Device to Resume.
1142 *
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.
1146 */
3cd7957e 1147static int acpi_subsys_resume_early(struct device *dev)
e5cc8ef3 1148{
63705c40 1149 int ret = acpi_dev_resume(dev);
e5cc8ef3
RW
1150 return ret ? ret : pm_generic_resume_early(dev);
1151}
92858c47
RW
1152
1153/**
1154 * acpi_subsys_freeze - Run the device driver's freeze callback.
1155 * @dev: Device to handle.
1156 */
1157int acpi_subsys_freeze(struct device *dev)
1158{
1159 /*
501debd4
RW
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.
92858c47 1166 */
501debd4 1167 pm_runtime_resume(dev);
05087360 1168
92858c47
RW
1169 return pm_generic_freeze(dev);
1170}
4cf563c5 1171EXPORT_SYMBOL_GPL(acpi_subsys_freeze);
92858c47 1172
05087360 1173/**
3cd7957e
RW
1174 * acpi_subsys_restore_early - Restore device using ACPI.
1175 * @dev: Device to restore.
05087360 1176 */
3cd7957e 1177int acpi_subsys_restore_early(struct device *dev)
05087360 1178{
3cd7957e
RW
1179 int ret = acpi_dev_resume(dev);
1180 return ret ? ret : pm_generic_restore_early(dev);
05087360 1181}
3cd7957e 1182EXPORT_SYMBOL_GPL(acpi_subsys_restore_early);
05087360 1183
c95b7595
RW
1184/**
1185 * acpi_subsys_poweroff - Run the device driver's poweroff callback.
1186 * @dev: Device to handle.
1187 *
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.
1192 */
1193int acpi_subsys_poweroff(struct device *dev)
1194{
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);
05087360 1198
c95b7595 1199 return pm_generic_poweroff(dev);
05087360 1200}
c95b7595 1201EXPORT_SYMBOL_GPL(acpi_subsys_poweroff);
05087360
RW
1202
1203/**
c95b7595 1204 * acpi_subsys_poweroff_late - Run the device driver's poweroff callback.
05087360 1205 * @dev: Device to handle.
c95b7595
RW
1206 *
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.
05087360 1209 */
c95b7595 1210static int acpi_subsys_poweroff_late(struct device *dev)
05087360 1211{
c95b7595 1212 int ret;
05087360
RW
1213
1214 if (dev_pm_smart_suspend_and_suspended(dev))
1215 return 0;
1216
c95b7595
RW
1217 ret = pm_generic_poweroff_late(dev);
1218 if (ret)
1219 return ret;
1220
1221 return acpi_dev_suspend(dev, device_may_wakeup(dev));
05087360 1222}
05087360
RW
1223
1224/**
c95b7595
RW
1225 * acpi_subsys_poweroff_noirq - Run the driver's "noirq" poweroff callback.
1226 * @dev: Device to suspend.
05087360 1227 */
c95b7595 1228static int acpi_subsys_poweroff_noirq(struct device *dev)
05087360 1229{
c95b7595 1230 if (dev_pm_smart_suspend_and_suspended(dev))
05087360 1231 return 0;
05087360 1232
c95b7595 1233 return pm_generic_poweroff_noirq(dev);
05087360 1234}
e5cc8ef3
RW
1235#endif /* CONFIG_PM_SLEEP */
1236
1237static struct dev_pm_domain acpi_general_pm_domain = {
1238 .ops = {
e5cc8ef3
RW
1239 .runtime_suspend = acpi_subsys_runtime_suspend,
1240 .runtime_resume = acpi_subsys_runtime_resume,
e5cc8ef3
RW
1241#ifdef CONFIG_PM_SLEEP
1242 .prepare = acpi_subsys_prepare,
e4da817d 1243 .complete = acpi_subsys_complete,
92858c47 1244 .suspend = acpi_subsys_suspend,
e5cc8ef3 1245 .suspend_late = acpi_subsys_suspend_late,
05087360
RW
1246 .suspend_noirq = acpi_subsys_suspend_noirq,
1247 .resume_noirq = acpi_subsys_resume_noirq,
e5cc8ef3 1248 .resume_early = acpi_subsys_resume_early,
92858c47 1249 .freeze = acpi_subsys_freeze,
c95b7595
RW
1250 .poweroff = acpi_subsys_poweroff,
1251 .poweroff_late = acpi_subsys_poweroff_late,
1252 .poweroff_noirq = acpi_subsys_poweroff_noirq,
3cd7957e 1253 .restore_early = acpi_subsys_restore_early,
e5cc8ef3
RW
1254#endif
1255 },
1256};
1257
91d66cd2
UH
1258/**
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.
1262 *
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.
1266 *
1267 * Callers must ensure proper synchronization of this function with power
1268 * management callbacks.
1269 */
1270static void acpi_dev_pm_detach(struct device *dev, bool power_off)
1271{
1272 struct acpi_device *adev = ACPI_COMPANION(dev);
1273
1274 if (adev && dev->pm_domain == &acpi_general_pm_domain) {
989561de 1275 dev_pm_domain_set(dev, NULL);
91d66cd2
UH
1276 acpi_remove_pm_notifier(adev);
1277 if (power_off) {
1278 /*
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.
1283 */
1284 dev_pm_qos_hide_latency_limit(dev);
1285 dev_pm_qos_hide_flags(dev);
99d8845e 1286 acpi_device_wakeup_disable(adev);
91d66cd2
UH
1287 acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
1288 }
1289 }
1290}
1291
e5cc8ef3
RW
1292/**
1293 * acpi_dev_pm_attach - Prepare device for ACPI power management.
1294 * @dev: Device to prepare.
b88ce2a4 1295 * @power_on: Whether or not to power on the device.
e5cc8ef3
RW
1296 *
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
b88ce2a4
RW
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.
e5cc8ef3
RW
1301 *
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).
1304 *
1305 * Callers must ensure proper synchronization of this function with power
1306 * management callbacks.
1307 */
b88ce2a4 1308int acpi_dev_pm_attach(struct device *dev, bool power_on)
e5cc8ef3 1309{
79c0373f 1310 struct acpi_device *adev = ACPI_COMPANION(dev);
e5cc8ef3
RW
1311
1312 if (!adev)
919b7308 1313 return 0;
e5cc8ef3 1314
712e960f
MW
1315 /*
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.
1319 */
1320 if (!acpi_device_is_first_physical_node(adev, dev))
919b7308 1321 return 0;
712e960f 1322
c072530f 1323 acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func);
989561de 1324 dev_pm_domain_set(dev, &acpi_general_pm_domain);
b88ce2a4
RW
1325 if (power_on) {
1326 acpi_dev_pm_full_power(adev);
99d8845e 1327 acpi_device_wakeup_disable(adev);
b88ce2a4 1328 }
86f1e15f
UH
1329
1330 dev->pm_domain->detach = acpi_dev_pm_detach;
919b7308 1331 return 1;
e5cc8ef3
RW
1332}
1333EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
ec4602a9 1334#endif /* CONFIG_PM */