Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * kernel/power/autosleep.c
4 *
5 * Opportunistic sleep support.
6 *
7 * Copyright (C) 2012 Rafael J. Wysocki <rjw@sisk.pl>
8 */
9
10#include <linux/device.h>
11#include <linux/mutex.h>
12#include <linux/pm_wakeup.h>
13
14#include "power.h"
15
16static suspend_state_t autosleep_state;
17static struct workqueue_struct *autosleep_wq;
18/*
19 * Note: it is only safe to mutex_lock(&autosleep_lock) if a wakeup_source
20 * is active, otherwise a deadlock with try_to_suspend() is possible.
21 * Alternatively mutex_lock_interruptible() can be used. This will then fail
22 * if an auto_sleep cycle tries to freeze processes.
23 */
24static DEFINE_MUTEX(autosleep_lock);
25static struct wakeup_source *autosleep_ws;
26
27static void try_to_suspend(struct work_struct *work)
28{
29 unsigned int initial_count, final_count;
30
31 if (!pm_get_wakeup_count(&initial_count, true))
32 goto out;
33
34 mutex_lock(&autosleep_lock);
35
36 if (!pm_save_wakeup_count(initial_count) ||
37 system_state != SYSTEM_RUNNING) {
38 mutex_unlock(&autosleep_lock);
39 goto out;
40 }
41
42 if (autosleep_state == PM_SUSPEND_ON) {
43 mutex_unlock(&autosleep_lock);
44 return;
45 }
46 if (autosleep_state >= PM_SUSPEND_MAX)
47 hibernate();
48 else
49 pm_suspend(autosleep_state);
50
51 mutex_unlock(&autosleep_lock);
52
53 if (!pm_get_wakeup_count(&final_count, false))
54 goto out;
55
56 /*
57 * If the wakeup occured for an unknown reason, wait to prevent the
58 * system from trying to suspend and waking up in a tight loop.
59 */
60 if (final_count == initial_count)
61 schedule_timeout_uninterruptible(HZ / 2);
62
63 out:
64 queue_up_suspend_work();
65}
66
67static DECLARE_WORK(suspend_work, try_to_suspend);
68
69void queue_up_suspend_work(void)
70{
71 if (autosleep_state > PM_SUSPEND_ON)
72 queue_work(autosleep_wq, &suspend_work);
73}
74
75suspend_state_t pm_autosleep_state(void)
76{
77 return autosleep_state;
78}
79
80int pm_autosleep_lock(void)
81{
82 return mutex_lock_interruptible(&autosleep_lock);
83}
84
85void pm_autosleep_unlock(void)
86{
87 mutex_unlock(&autosleep_lock);
88}
89
90int pm_autosleep_set_state(suspend_state_t state)
91{
92
93#ifndef CONFIG_HIBERNATION
94 if (state >= PM_SUSPEND_MAX)
95 return -EINVAL;
96#endif
97
98 __pm_stay_awake(autosleep_ws);
99
100 mutex_lock(&autosleep_lock);
101
102 autosleep_state = state;
103
104 __pm_relax(autosleep_ws);
105
106 if (state > PM_SUSPEND_ON) {
107 pm_wakep_autosleep_enabled(true);
108 queue_up_suspend_work();
109 } else {
110 pm_wakep_autosleep_enabled(false);
111 }
112
113 mutex_unlock(&autosleep_lock);
114 return 0;
115}
116
117int __init pm_autosleep_init(void)
118{
119 autosleep_ws = wakeup_source_register(NULL, "autosleep");
120 if (!autosleep_ws)
121 return -ENOMEM;
122
123 autosleep_wq = alloc_ordered_workqueue("autosleep", 0);
124 if (autosleep_wq)
125 return 0;
126
127 wakeup_source_unregister(autosleep_ws);
128 return -ENOMEM;
129}
1/*
2 * kernel/power/autosleep.c
3 *
4 * Opportunistic sleep support.
5 *
6 * Copyright (C) 2012 Rafael J. Wysocki <rjw@sisk.pl>
7 */
8
9#include <linux/device.h>
10#include <linux/mutex.h>
11#include <linux/pm_wakeup.h>
12
13#include "power.h"
14
15static suspend_state_t autosleep_state;
16static struct workqueue_struct *autosleep_wq;
17/*
18 * Note: it is only safe to mutex_lock(&autosleep_lock) if a wakeup_source
19 * is active, otherwise a deadlock with try_to_suspend() is possible.
20 * Alternatively mutex_lock_interruptible() can be used. This will then fail
21 * if an auto_sleep cycle tries to freeze processes.
22 */
23static DEFINE_MUTEX(autosleep_lock);
24static struct wakeup_source *autosleep_ws;
25
26static void try_to_suspend(struct work_struct *work)
27{
28 unsigned int initial_count, final_count;
29
30 if (!pm_get_wakeup_count(&initial_count, true))
31 goto out;
32
33 mutex_lock(&autosleep_lock);
34
35 if (!pm_save_wakeup_count(initial_count)) {
36 mutex_unlock(&autosleep_lock);
37 goto out;
38 }
39
40 if (autosleep_state == PM_SUSPEND_ON) {
41 mutex_unlock(&autosleep_lock);
42 return;
43 }
44 if (autosleep_state >= PM_SUSPEND_MAX)
45 hibernate();
46 else
47 pm_suspend(autosleep_state);
48
49 mutex_unlock(&autosleep_lock);
50
51 if (!pm_get_wakeup_count(&final_count, false))
52 goto out;
53
54 /*
55 * If the wakeup occured for an unknown reason, wait to prevent the
56 * system from trying to suspend and waking up in a tight loop.
57 */
58 if (final_count == initial_count)
59 schedule_timeout_uninterruptible(HZ / 2);
60
61 out:
62 queue_up_suspend_work();
63}
64
65static DECLARE_WORK(suspend_work, try_to_suspend);
66
67void queue_up_suspend_work(void)
68{
69 if (!work_pending(&suspend_work) && autosleep_state > PM_SUSPEND_ON)
70 queue_work(autosleep_wq, &suspend_work);
71}
72
73suspend_state_t pm_autosleep_state(void)
74{
75 return autosleep_state;
76}
77
78int pm_autosleep_lock(void)
79{
80 return mutex_lock_interruptible(&autosleep_lock);
81}
82
83void pm_autosleep_unlock(void)
84{
85 mutex_unlock(&autosleep_lock);
86}
87
88int pm_autosleep_set_state(suspend_state_t state)
89{
90
91#ifndef CONFIG_HIBERNATION
92 if (state >= PM_SUSPEND_MAX)
93 return -EINVAL;
94#endif
95
96 __pm_stay_awake(autosleep_ws);
97
98 mutex_lock(&autosleep_lock);
99
100 autosleep_state = state;
101
102 __pm_relax(autosleep_ws);
103
104 if (state > PM_SUSPEND_ON) {
105 pm_wakep_autosleep_enabled(true);
106 queue_up_suspend_work();
107 } else {
108 pm_wakep_autosleep_enabled(false);
109 }
110
111 mutex_unlock(&autosleep_lock);
112 return 0;
113}
114
115int __init pm_autosleep_init(void)
116{
117 autosleep_ws = wakeup_source_register("autosleep");
118 if (!autosleep_ws)
119 return -ENOMEM;
120
121 autosleep_wq = alloc_ordered_workqueue("autosleep", 0);
122 if (autosleep_wq)
123 return 0;
124
125 wakeup_source_unregister(autosleep_ws);
126 return -ENOMEM;
127}