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