]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/acpi/device_pm.c
Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux.git] / drivers / acpi / device_pm.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * drivers/acpi/device_pm.c - ACPI device power management routines.
4  *
5  * Copyright (C) 2012, Intel Corp.
6  * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7  *
8  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *
10  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11  */
12
13 #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>
20
21 #include "internal.h"
22
23 #define _COMPONENT      ACPI_POWER_COMPONENT
24 ACPI_MODULE_NAME("device_pm");
25
26 /**
27  * acpi_power_state_string - String representation of ACPI device power state.
28  * @state: ACPI device power state to return the string representation of.
29  */
30 const char *acpi_power_state_string(int state)
31 {
32         switch (state) {
33         case ACPI_STATE_D0:
34                 return "D0";
35         case ACPI_STATE_D1:
36                 return "D1";
37         case ACPI_STATE_D2:
38                 return "D2";
39         case ACPI_STATE_D3_HOT:
40                 return "D3hot";
41         case ACPI_STATE_D3_COLD:
42                 return "D3cold";
43         default:
44                 return "(unknown)";
45         }
46 }
47
48 static int acpi_dev_pm_explicit_get(struct acpi_device *device, int *state)
49 {
50         unsigned long long psc;
51         acpi_status status;
52
53         status = acpi_evaluate_integer(device->handle, "_PSC", NULL, &psc);
54         if (ACPI_FAILURE(status))
55                 return -ENODEV;
56
57         *state = psc;
58         return 0;
59 }
60
61 /**
62  * acpi_device_get_power - Get power state of an ACPI device.
63  * @device: Device to get the power state of.
64  * @state: Place to store the power state of the device.
65  *
66  * This function does not update the device's power.state field, but it may
67  * update its parent's power.state field (when the parent's power state is
68  * unknown and the device's power state turns out to be D0).
69  *
70  * Also, it does not update power resource reference counters to ensure that
71  * the power state returned by it will be persistent and it may return a power
72  * state shallower than previously set by acpi_device_set_power() for @device
73  * (if that power state depends on any power resources).
74  */
75 int acpi_device_get_power(struct acpi_device *device, int *state)
76 {
77         int result = ACPI_STATE_UNKNOWN;
78         int error;
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         /*
91          * Get the device's power state from power resources settings and _PSC,
92          * if available.
93          */
94         if (device->power.flags.power_resources) {
95                 error = acpi_power_get_inferred_state(device, &result);
96                 if (error)
97                         return error;
98         }
99         if (device->power.flags.explicit_get) {
100                 int psc;
101
102                 error = acpi_dev_pm_explicit_get(device, &psc);
103                 if (error)
104                         return error;
105
106                 /*
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.
110                  *
111                  * For systems predating ACPI 4.0 we assume that D3hot is the
112                  * deepest state that can be supported.
113                  */
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;
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
123          * to be in D0 too, except if ignore_parent is set.
124          */
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;
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
139 static 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
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  */
160 int acpi_device_set_power(struct acpi_device *device, int state)
161 {
162         int target_state = state;
163         int result = 0;
164
165         if (!device || !device->flags.power_manageable
166             || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
167                 return -EINVAL;
168
169         /* Make sure this is a valid target state */
170
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;
177         }
178
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) {
189                 dev_warn(&device->dev, "Power state %s not supported\n",
190                          acpi_power_state_string(state));
191                 return -ENODEV;
192         }
193
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;
201         }
202
203         /*
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.
209          */
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
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                 }
231
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                 }
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
264                 result = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
265         }
266
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)));
277         }
278
279         return result;
280 }
281 EXPORT_SYMBOL(acpi_device_set_power);
282
283 int 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
292         return acpi_device_set_power(device, state);
293 }
294 EXPORT_SYMBOL(acpi_bus_set_power);
295
296 int 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;
305         if (!acpi_device_is_present(device)) {
306                 device->flags.initialized = false;
307                 return -ENXIO;
308         }
309
310         result = acpi_device_get_power(device, &state);
311         if (result)
312                 return result;
313
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;
319
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                 }
333         } else if (state == ACPI_STATE_UNKNOWN) {
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                  */
340                 state = ACPI_STATE_D0;
341         }
342         device->power.state = state;
343         return 0;
344 }
345
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  */
354 int 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 }
365 EXPORT_SYMBOL_GPL(acpi_device_fix_up_power);
366
367 int acpi_device_update_power(struct acpi_device *device, int *state_p)
368 {
369         int state;
370         int result;
371
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
377                 return result;
378         }
379
380         result = acpi_device_get_power(device, &state);
381         if (result)
382                 return result;
383
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) {
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)
402                 *state_p = state;
403
404         return 0;
405 }
406 EXPORT_SYMBOL_GPL(acpi_device_update_power);
407
408 int 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 }
416 EXPORT_SYMBOL_GPL(acpi_bus_update_power);
417
418 bool 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 }
426 EXPORT_SYMBOL(acpi_bus_power_manageable);
427
428 #ifdef CONFIG_PM
429 static DEFINE_MUTEX(acpi_pm_notifier_lock);
430 static DEFINE_MUTEX(acpi_pm_notifier_install_lock);
431
432 void acpi_pm_wakeup_event(struct device *dev)
433 {
434         pm_wakeup_dev_event(dev, 0, acpi_s2idle_wakeup());
435 }
436 EXPORT_SYMBOL_GPL(acpi_pm_wakeup_event);
437
438 static 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
445         acpi_handle_debug(handle, "Wake notify\n");
446
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) {
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);
460                 }
461         }
462
463         mutex_unlock(&acpi_pm_notifier_lock);
464
465         acpi_bus_put_acpi_device(adev);
466 }
467
468 /**
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.
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  */
479 acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev,
480                         void (*func)(struct acpi_device_wakeup_context *context))
481 {
482         acpi_status status = AE_ALREADY_EXISTS;
483
484         if (!dev && !func)
485                 return AE_BAD_PARAMETER;
486
487         mutex_lock(&acpi_pm_notifier_install_lock);
488
489         if (adev->wakeup.flags.notifier_present)
490                 goto out;
491
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;
496
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);
503
504  out:
505         mutex_unlock(&acpi_pm_notifier_install_lock);
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  */
513 acpi_status acpi_remove_pm_notifier(struct acpi_device *adev)
514 {
515         acpi_status status = AE_BAD_PARAMETER;
516
517         mutex_lock(&acpi_pm_notifier_install_lock);
518
519         if (!adev->wakeup.flags.notifier_present)
520                 goto out;
521
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;
527
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);
534
535  out:
536         mutex_unlock(&acpi_pm_notifier_install_lock);
537         return status;
538 }
539
540 bool 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 }
548 EXPORT_SYMBOL(acpi_bus_can_wakeup);
549
550 bool 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
557 /**
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.
564  *
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.
570  *
571  * Callers must ensure that @dev and @adev are valid pointers and that @adev
572  * actually corresponds to @dev before using this function.
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.
577  */
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)
580 {
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;
588
589         /*
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.
594          */
595         d_min = ACPI_STATE_D0;
596         d_max = ACPI_STATE_D3_COLD;
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).
602          */
603         if (target_state > ACPI_STATE_S0) {
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                 }
625
626                 if (status == AE_OK)
627                         has_sxd = true;
628
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;
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) {
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.
650                          */
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;
657
658                         d_max = ret > d_min ? ret : d_min;
659                 } else {
660                         return -ENODATA;
661                 }
662         }
663
664         if (d_min_p)
665                 *d_min_p = d_min;
666
667         if (d_max_p)
668                 *d_max_p = d_max;
669
670         return 0;
671 }
672
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
679  * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is
680  * incorrect, or -ENODATA on ACPI method failure.
681  *
682  * The caller must ensure that @dev is valid before using this function.
683  */
684 int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
685 {
686         struct acpi_device *adev;
687         int ret, d_min, d_max;
688
689         if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD)
690                 return -EINVAL;
691
692         if (d_max_in > ACPI_STATE_D2) {
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)
697                         d_max_in = ACPI_STATE_D2;
698         }
699
700         adev = ACPI_COMPANION(dev);
701         if (!adev) {
702                 dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
703                 return -ENODEV;
704         }
705
706         ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(),
707                                     &d_min, &d_max);
708         if (ret)
709                 return ret;
710
711         if (d_max_in < d_min)
712                 return -EINVAL;
713
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;
718                 }
719         }
720
721         if (d_min_p)
722                 *d_min_p = d_min;
723
724         return d_max;
725 }
726 EXPORT_SYMBOL(acpi_pm_device_sleep_state);
727
728 /**
729  * acpi_pm_notify_work_func - ACPI devices wakeup notification work function.
730  * @context: Device wakeup context.
731  */
732 static void acpi_pm_notify_work_func(struct acpi_device_wakeup_context *context)
733 {
734         struct device *dev = context->dev;
735
736         if (dev) {
737                 pm_wakeup_event(dev, 0);
738                 pm_request_resume(dev);
739         }
740 }
741
742 static DEFINE_MUTEX(acpi_wakeup_lock);
743
744 static int __acpi_device_wakeup_enable(struct acpi_device *adev,
745                                        u32 target_state, int max_count)
746 {
747         struct acpi_device_wakeup *wakeup = &adev->wakeup;
748         acpi_status status;
749         int error = 0;
750
751         mutex_lock(&acpi_wakeup_lock);
752
753         if (wakeup->enable_count >= max_count)
754                 goto out;
755
756         if (wakeup->enable_count > 0)
757                 goto inc;
758
759         error = acpi_enable_wakeup_device_power(adev, target_state);
760         if (error)
761                 goto out;
762
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;
768         }
769
770         acpi_handle_debug(adev->handle, "GPE%2X enabled for wakeup\n",
771                           (unsigned int)wakeup->gpe_number);
772
773 inc:
774         wakeup->enable_count++;
775
776 out:
777         mutex_unlock(&acpi_wakeup_lock);
778         return error;
779 }
780
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  */
793 static 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
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  */
807 static 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
821 out:
822         mutex_unlock(&acpi_wakeup_lock);
823 }
824
825 static int __acpi_pm_set_device_wakeup(struct device *dev, bool enable,
826                                        int max_count)
827 {
828         struct acpi_device *adev;
829         int error;
830
831         adev = ACPI_COMPANION(dev);
832         if (!adev) {
833                 dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
834                 return -ENODEV;
835         }
836
837         if (!acpi_device_can_wakeup(adev))
838                 return -EINVAL;
839
840         if (!enable) {
841                 acpi_device_wakeup_disable(adev);
842                 dev_dbg(dev, "Wakeup disabled by ACPI\n");
843                 return 0;
844         }
845
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");
850
851         return error;
852 }
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  */
859 int acpi_pm_set_device_wakeup(struct device *dev, bool enable)
860 {
861         return __acpi_pm_set_device_wakeup(dev, enable, 1);
862 }
863 EXPORT_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  */
870 int acpi_pm_set_bridge_wakeup(struct device *dev, bool enable)
871 {
872         return __acpi_pm_set_device_wakeup(dev, enable, INT_MAX);
873 }
874 EXPORT_SYMBOL_GPL(acpi_pm_set_bridge_wakeup);
875
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  */
882 static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
883                                  u32 system_state)
884 {
885         int ret, state;
886
887         if (!acpi_device_power_manageable(adev))
888                 return 0;
889
890         ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state);
891         return ret ? ret : acpi_device_set_power(adev, state);
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  */
898 static 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
904 /**
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.
908  *
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.
913  */
914 int acpi_dev_suspend(struct device *dev, bool wakeup)
915 {
916         struct acpi_device *adev = ACPI_COMPANION(dev);
917         u32 target_state = acpi_target_system_state();
918         int error;
919
920         if (!adev)
921                 return 0;
922
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;
929         }
930
931         error = acpi_dev_pm_low_power(dev, adev, target_state);
932         if (error && wakeup)
933                 acpi_device_wakeup_disable(adev);
934
935         return error;
936 }
937 EXPORT_SYMBOL_GPL(acpi_dev_suspend);
938
939 /**
940  * acpi_dev_resume - Put device into the full-power state using ACPI.
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
944  * mechanism.  Set the power state of the device to ACPI D0 and disable wakeup.
945  */
946 int acpi_dev_resume(struct device *dev)
947 {
948         struct acpi_device *adev = ACPI_COMPANION(dev);
949         int error;
950
951         if (!adev)
952                 return 0;
953
954         error = acpi_dev_pm_full_power(adev);
955         acpi_device_wakeup_disable(adev);
956         return error;
957 }
958 EXPORT_SYMBOL_GPL(acpi_dev_resume);
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  */
967 int acpi_subsys_runtime_suspend(struct device *dev)
968 {
969         int ret = pm_generic_runtime_suspend(dev);
970         return ret ? ret : acpi_dev_suspend(dev, true);
971 }
972 EXPORT_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  */
981 int acpi_subsys_runtime_resume(struct device *dev)
982 {
983         int ret = acpi_dev_resume(dev);
984         return ret ? ret : pm_generic_runtime_resume(dev);
985 }
986 EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
987
988 #ifdef CONFIG_PM_SLEEP
989 static 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
994         if (!pm_runtime_suspended(dev) || !adev || (adev->wakeup.flags.valid &&
995             device_may_wakeup(dev) != !!adev->wakeup.prepare_count))
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
1011 /**
1012  * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
1013  * @dev: Device to prepare.
1014  */
1015 int acpi_subsys_prepare(struct device *dev)
1016 {
1017         struct acpi_device *adev = ACPI_COMPANION(dev);
1018
1019         if (dev->driver && dev->driver->pm && dev->driver->pm->prepare) {
1020                 int ret = dev->driver->pm->prepare(dev);
1021
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         }
1028
1029         return !acpi_dev_needs_resume(dev, adev);
1030 }
1031 EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
1032
1033 /**
1034  * acpi_subsys_complete - Finalize device's resume during system resume.
1035  * @dev: Device to handle.
1036  */
1037 void 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          */
1045         if (pm_runtime_suspended(dev) && pm_resume_via_firmware())
1046                 pm_request_resume(dev);
1047 }
1048 EXPORT_SYMBOL_GPL(acpi_subsys_complete);
1049
1050 /**
1051  * acpi_subsys_suspend - Run the device driver's suspend callback.
1052  * @dev: Device to handle.
1053  *
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.
1058  */
1059 int acpi_subsys_suspend(struct device *dev)
1060 {
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
1065         return pm_generic_suspend(dev);
1066 }
1067 EXPORT_SYMBOL_GPL(acpi_subsys_suspend);
1068
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  */
1076 int acpi_subsys_suspend_late(struct device *dev)
1077 {
1078         int ret;
1079
1080         if (dev_pm_smart_suspend_and_suspended(dev))
1081                 return 0;
1082
1083         ret = pm_generic_suspend_late(dev);
1084         return ret ? ret : acpi_dev_suspend(dev, device_may_wakeup(dev));
1085 }
1086 EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
1087
1088 /**
1089  * acpi_subsys_suspend_noirq - Run the device driver's "noirq" suspend callback.
1090  * @dev: Device to suspend.
1091  */
1092 int acpi_subsys_suspend_noirq(struct device *dev)
1093 {
1094         int ret;
1095
1096         if (dev_pm_smart_suspend_and_suspended(dev)) {
1097                 dev->power.may_skip_resume = true;
1098                 return 0;
1099         }
1100
1101         ret = pm_generic_suspend_noirq(dev);
1102         if (ret)
1103                 return ret;
1104
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;
1116 }
1117 EXPORT_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  */
1123 static int acpi_subsys_resume_noirq(struct device *dev)
1124 {
1125         if (dev_pm_may_skip_resume(dev))
1126                 return 0;
1127
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 }
1138
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  */
1147 static int acpi_subsys_resume_early(struct device *dev)
1148 {
1149         int ret = acpi_dev_resume(dev);
1150         return ret ? ret : pm_generic_resume_early(dev);
1151 }
1152
1153 /**
1154  * acpi_subsys_freeze - Run the device driver's freeze callback.
1155  * @dev: Device to handle.
1156  */
1157 int acpi_subsys_freeze(struct device *dev)
1158 {
1159         /*
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.
1166          */
1167         pm_runtime_resume(dev);
1168
1169         return pm_generic_freeze(dev);
1170 }
1171 EXPORT_SYMBOL_GPL(acpi_subsys_freeze);
1172
1173 /**
1174  * acpi_subsys_restore_early - Restore device using ACPI.
1175  * @dev: Device to restore.
1176  */
1177 int acpi_subsys_restore_early(struct device *dev)
1178 {
1179         int ret = acpi_dev_resume(dev);
1180         return ret ? ret : pm_generic_restore_early(dev);
1181 }
1182 EXPORT_SYMBOL_GPL(acpi_subsys_restore_early);
1183
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  */
1193 int 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);
1198
1199         return pm_generic_poweroff(dev);
1200 }
1201 EXPORT_SYMBOL_GPL(acpi_subsys_poweroff);
1202
1203 /**
1204  * acpi_subsys_poweroff_late - Run the device driver's poweroff callback.
1205  * @dev: Device to handle.
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.
1209  */
1210 static int acpi_subsys_poweroff_late(struct device *dev)
1211 {
1212         int ret;
1213
1214         if (dev_pm_smart_suspend_and_suspended(dev))
1215                 return 0;
1216
1217         ret = pm_generic_poweroff_late(dev);
1218         if (ret)
1219                 return ret;
1220
1221         return acpi_dev_suspend(dev, device_may_wakeup(dev));
1222 }
1223
1224 /**
1225  * acpi_subsys_poweroff_noirq - Run the driver's "noirq" poweroff callback.
1226  * @dev: Device to suspend.
1227  */
1228 static int acpi_subsys_poweroff_noirq(struct device *dev)
1229 {
1230         if (dev_pm_smart_suspend_and_suspended(dev))
1231                 return 0;
1232
1233         return pm_generic_poweroff_noirq(dev);
1234 }
1235 #endif /* CONFIG_PM_SLEEP */
1236
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
1255         },
1256 };
1257
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  */
1270 static 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) {
1275                 dev_pm_domain_set(dev, NULL);
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);
1286                         acpi_device_wakeup_disable(adev);
1287                         acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
1288                 }
1289         }
1290 }
1291
1292 /**
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.
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
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.
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  */
1308 int acpi_dev_pm_attach(struct device *dev, bool power_on)
1309 {
1310         struct acpi_device *adev = ACPI_COMPANION(dev);
1311
1312         if (!adev)
1313                 return 0;
1314
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))
1321                 return 0;
1322
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);
1328         }
1329
1330         dev->pm_domain->detach = acpi_dev_pm_detach;
1331         return 1;
1332 }
1333 EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
1334 #endif /* CONFIG_PM */