]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - kernel/power/main.c
PM: Rework struct platform_suspend_ops
[mirror_ubuntu-bionic-kernel.git] / kernel / power / main.c
1 /*
2 * kernel/power/main.c - PM subsystem core functionality.
3 *
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
6 *
7 * This file is released under the GPLv2
8 *
9 */
10
11 #include <linux/module.h>
12 #include <linux/suspend.h>
13 #include <linux/kobject.h>
14 #include <linux/string.h>
15 #include <linux/delay.h>
16 #include <linux/errno.h>
17 #include <linux/init.h>
18 #include <linux/console.h>
19 #include <linux/cpu.h>
20 #include <linux/resume-trace.h>
21 #include <linux/freezer.h>
22 #include <linux/vmstat.h>
23
24 #include "power.h"
25
26 BLOCKING_NOTIFIER_HEAD(pm_chain_head);
27
28 DEFINE_MUTEX(pm_mutex);
29
30 #ifdef CONFIG_SUSPEND
31
32 /* This is just an arbitrary number */
33 #define FREE_PAGE_NUMBER (100)
34
35 struct platform_suspend_ops *suspend_ops;
36
37 /**
38 * suspend_set_ops - Set the global suspend method table.
39 * @ops: Pointer to ops structure.
40 */
41
42 void suspend_set_ops(struct platform_suspend_ops *ops)
43 {
44 mutex_lock(&pm_mutex);
45 suspend_ops = ops;
46 mutex_unlock(&pm_mutex);
47 }
48
49 /**
50 * suspend_valid_only_mem - generic memory-only valid callback
51 *
52 * Platform drivers that implement mem suspend only and only need
53 * to check for that in their .valid callback can use this instead
54 * of rolling their own .valid callback.
55 */
56 int suspend_valid_only_mem(suspend_state_t state)
57 {
58 return state == PM_SUSPEND_MEM;
59 }
60
61 /**
62 * suspend_prepare - Do prep work before entering low-power state.
63 *
64 * This is common code that is called for each state that we're entering.
65 * Run suspend notifiers, allocate a console and stop all processes.
66 */
67 static int suspend_prepare(void)
68 {
69 int error;
70 unsigned int free_pages;
71
72 if (!suspend_ops || !suspend_ops->enter)
73 return -EPERM;
74
75 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
76 if (error)
77 goto Finish;
78
79 pm_prepare_console();
80
81 if (freeze_processes()) {
82 error = -EAGAIN;
83 goto Thaw;
84 }
85
86 free_pages = global_page_state(NR_FREE_PAGES);
87 if (free_pages < FREE_PAGE_NUMBER) {
88 pr_debug("PM: free some memory\n");
89 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
90 if (nr_free_pages() < FREE_PAGE_NUMBER) {
91 error = -ENOMEM;
92 printk(KERN_ERR "PM: No enough memory\n");
93 }
94 }
95 if (!error)
96 return 0;
97
98 Thaw:
99 thaw_processes();
100 pm_restore_console();
101 Finish:
102 pm_notifier_call_chain(PM_POST_SUSPEND);
103 return error;
104 }
105
106 /* default implementation */
107 void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
108 {
109 local_irq_disable();
110 }
111
112 /* default implementation */
113 void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
114 {
115 local_irq_enable();
116 }
117
118 /**
119 * suspend_enter - enter the desired system sleep state.
120 * @state: state to enter
121 *
122 * This function should be called after devices have been suspended.
123 */
124 static int suspend_enter(suspend_state_t state)
125 {
126 int error = 0;
127
128 arch_suspend_disable_irqs();
129 BUG_ON(!irqs_disabled());
130
131 if ((error = device_power_down(PMSG_SUSPEND))) {
132 printk(KERN_ERR "Some devices failed to power down\n");
133 goto Done;
134 }
135 error = suspend_ops->enter(state);
136 device_power_up();
137 Done:
138 arch_suspend_enable_irqs();
139 BUG_ON(irqs_disabled());
140 return error;
141 }
142
143 /**
144 * suspend_devices_and_enter - suspend devices and enter the desired system sleep
145 * state.
146 * @state: state to enter
147 */
148 int suspend_devices_and_enter(suspend_state_t state)
149 {
150 int error;
151
152 if (!suspend_ops)
153 return -ENOSYS;
154
155 if (suspend_ops->set_target) {
156 error = suspend_ops->set_target(state);
157 if (error)
158 return error;
159 }
160 suspend_console();
161 error = device_suspend(PMSG_SUSPEND);
162 if (error) {
163 printk(KERN_ERR "Some devices failed to suspend\n");
164 goto Resume_console;
165 }
166 if (suspend_ops->prepare) {
167 error = suspend_ops->prepare();
168 if (error)
169 goto Resume_devices;
170 }
171 error = disable_nonboot_cpus();
172 if (!error)
173 suspend_enter(state);
174
175 enable_nonboot_cpus();
176 if (suspend_ops->finish)
177 suspend_ops->finish();
178 Resume_devices:
179 device_resume();
180 Resume_console:
181 resume_console();
182 return error;
183 }
184
185 /**
186 * suspend_finish - Do final work before exiting suspend sequence.
187 *
188 * Call platform code to clean up, restart processes, and free the
189 * console that we've allocated. This is not called for suspend-to-disk.
190 */
191 static void suspend_finish(void)
192 {
193 thaw_processes();
194 pm_restore_console();
195 pm_notifier_call_chain(PM_POST_SUSPEND);
196 }
197
198
199
200
201 static const char * const pm_states[PM_SUSPEND_MAX] = {
202 [PM_SUSPEND_STANDBY] = "standby",
203 [PM_SUSPEND_MEM] = "mem",
204 };
205
206 static inline int valid_state(suspend_state_t state)
207 {
208 /* All states need lowlevel support and need to be valid
209 * to the lowlevel implementation, no valid callback
210 * implies that none are valid. */
211 if (!suspend_ops || !suspend_ops->valid || !suspend_ops->valid(state))
212 return 0;
213 return 1;
214 }
215
216
217 /**
218 * enter_state - Do common work of entering low-power state.
219 * @state: pm_state structure for state we're entering.
220 *
221 * Make sure we're the only ones trying to enter a sleep state. Fail
222 * if someone has beat us to it, since we don't want anything weird to
223 * happen when we wake up.
224 * Then, do the setup for suspend, enter the state, and cleaup (after
225 * we've woken up).
226 */
227 static int enter_state(suspend_state_t state)
228 {
229 int error;
230
231 if (!valid_state(state))
232 return -ENODEV;
233 if (!mutex_trylock(&pm_mutex))
234 return -EBUSY;
235
236 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
237 if ((error = suspend_prepare()))
238 goto Unlock;
239
240 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
241 error = suspend_devices_and_enter(state);
242
243 pr_debug("PM: Finishing wakeup.\n");
244 suspend_finish();
245 Unlock:
246 mutex_unlock(&pm_mutex);
247 return error;
248 }
249
250
251 /**
252 * pm_suspend - Externally visible function for suspending system.
253 * @state: Enumerated value of state to enter.
254 *
255 * Determine whether or not value is within range, get state
256 * structure, and enter (above).
257 */
258
259 int pm_suspend(suspend_state_t state)
260 {
261 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
262 return enter_state(state);
263 return -EINVAL;
264 }
265
266 EXPORT_SYMBOL(pm_suspend);
267
268 #endif /* CONFIG_SUSPEND */
269
270 decl_subsys(power,NULL,NULL);
271
272
273 /**
274 * state - control system power state.
275 *
276 * show() returns what states are supported, which is hard-coded to
277 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
278 * 'disk' (Suspend-to-Disk).
279 *
280 * store() accepts one of those strings, translates it into the
281 * proper enumerated value, and initiates a suspend transition.
282 */
283
284 static ssize_t state_show(struct kset *kset, char *buf)
285 {
286 char *s = buf;
287 #ifdef CONFIG_SUSPEND
288 int i;
289
290 for (i = 0; i < PM_SUSPEND_MAX; i++) {
291 if (pm_states[i] && valid_state(i))
292 s += sprintf(s,"%s ", pm_states[i]);
293 }
294 #endif
295 #ifdef CONFIG_HIBERNATION
296 s += sprintf(s, "%s\n", "disk");
297 #else
298 if (s != buf)
299 /* convert the last space to a newline */
300 *(s-1) = '\n';
301 #endif
302 return (s - buf);
303 }
304
305 static ssize_t state_store(struct kset *kset, const char *buf, size_t n)
306 {
307 #ifdef CONFIG_SUSPEND
308 suspend_state_t state = PM_SUSPEND_STANDBY;
309 const char * const *s;
310 #endif
311 char *p;
312 int len;
313 int error = -EINVAL;
314
315 p = memchr(buf, '\n', n);
316 len = p ? p - buf : n;
317
318 /* First, check if we are requested to hibernate */
319 if (len == 4 && !strncmp(buf, "disk", len)) {
320 error = hibernate();
321 goto Exit;
322 }
323
324 #ifdef CONFIG_SUSPEND
325 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
326 if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
327 break;
328 }
329 if (state < PM_SUSPEND_MAX && *s)
330 error = enter_state(state);
331 #endif
332
333 Exit:
334 return error ? error : n;
335 }
336
337 power_attr(state);
338
339 #ifdef CONFIG_PM_TRACE
340 int pm_trace_enabled;
341
342 static ssize_t pm_trace_show(struct kset *kset, char *buf)
343 {
344 return sprintf(buf, "%d\n", pm_trace_enabled);
345 }
346
347 static ssize_t
348 pm_trace_store(struct kset *kset, const char *buf, size_t n)
349 {
350 int val;
351
352 if (sscanf(buf, "%d", &val) == 1) {
353 pm_trace_enabled = !!val;
354 return n;
355 }
356 return -EINVAL;
357 }
358
359 power_attr(pm_trace);
360
361 static struct attribute * g[] = {
362 &state_attr.attr,
363 &pm_trace_attr.attr,
364 NULL,
365 };
366 #else
367 static struct attribute * g[] = {
368 &state_attr.attr,
369 NULL,
370 };
371 #endif /* CONFIG_PM_TRACE */
372
373 static struct attribute_group attr_group = {
374 .attrs = g,
375 };
376
377
378 static int __init pm_init(void)
379 {
380 int error = subsystem_register(&power_subsys);
381 if (!error)
382 error = sysfs_create_group(&power_subsys.kobj,&attr_group);
383 return error;
384 }
385
386 core_initcall(pm_init);