blob: 6096c71b182b04d59be7ff97ef770bb8bc6f6e61 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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/suspend.h>
12#include <linux/kobject.h>
13#include <linux/string.h>
14#include <linux/delay.h>
15#include <linux/errno.h>
16#include <linux/init.h>
17#include <linux/pm.h>
Andrew Morton6cc07192006-06-22 14:47:18 -070018#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070019#include <linux/cpu.h>
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -070020#include <linux/resume-trace.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080021#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include "power.h"
24
David Shaohua Li5ae947e2005-03-18 16:27:13 -050025/*This is just an arbitrary number */
26#define FREE_PAGE_NUMBER (100)
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028DECLARE_MUTEX(pm_sem);
29
Pavel Machek123d3c12005-11-29 19:34:37 -080030struct pm_ops *pm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031suspend_disk_method_t pm_disk_mode = PM_DISK_SHUTDOWN;
32
33/**
34 * pm_set_ops - Set the global power method table.
35 * @ops: Pointer to ops structure.
36 */
37
38void pm_set_ops(struct pm_ops * ops)
39{
40 down(&pm_sem);
41 pm_ops = ops;
42 up(&pm_sem);
43}
44
45
46/**
47 * suspend_prepare - Do prep work before entering low-power state.
48 * @state: State we're entering.
49 *
50 * This is common code that is called for each state that we're
51 * entering. Allocate a console, stop all processes, then make sure
52 * the platform can enter the requested state.
53 */
54
55static int suspend_prepare(suspend_state_t state)
56{
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070057 int error;
David Shaohua Li5ae947e2005-03-18 16:27:13 -050058 unsigned int free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 if (!pm_ops || !pm_ops->enter)
61 return -EPERM;
62
63 pm_prepare_console();
64
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070065 error = disable_nonboot_cpus();
66 if (error)
Li Shaohua5a72e042005-06-25 14:55:06 -070067 goto Enable_cpu;
Li Shaohua5a72e042005-06-25 14:55:06 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (freeze_processes()) {
70 error = -EAGAIN;
71 goto Thaw;
72 }
73
David Shaohua Li5ae947e2005-03-18 16:27:13 -050074 if ((free_pages = nr_free_pages()) < FREE_PAGE_NUMBER) {
75 pr_debug("PM: free some memory\n");
76 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
77 if (nr_free_pages() < FREE_PAGE_NUMBER) {
78 error = -ENOMEM;
79 printk(KERN_ERR "PM: No enough memory\n");
80 goto Thaw;
81 }
82 }
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (pm_ops->prepare) {
85 if ((error = pm_ops->prepare(state)))
86 goto Thaw;
87 }
88
Linus Torvalds557240b2006-06-19 18:16:01 -070089 suspend_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if ((error = device_suspend(PMSG_SUSPEND))) {
91 printk(KERN_ERR "Some devices failed to suspend\n");
92 goto Finish;
93 }
94 return 0;
95 Finish:
96 if (pm_ops->finish)
97 pm_ops->finish(state);
98 Thaw:
99 thaw_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700100 Enable_cpu:
101 enable_nonboot_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 pm_restore_console();
103 return error;
104}
105
106
Luca Tettamanti9b238202006-03-23 03:00:09 -0800107int suspend_enter(suspend_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 int error = 0;
110 unsigned long flags;
111
112 local_irq_save(flags);
113
114 if ((error = device_power_down(PMSG_SUSPEND))) {
115 printk(KERN_ERR "Some devices failed to power down\n");
116 goto Done;
117 }
118 error = pm_ops->enter(state);
119 device_power_up();
120 Done:
121 local_irq_restore(flags);
122 return error;
123}
124
125
126/**
127 * suspend_finish - Do final work before exiting suspend sequence.
128 * @state: State we're coming out of.
129 *
130 * Call platform code to clean up, restart processes, and free the
131 * console that we've allocated. This is not called for suspend-to-disk.
132 */
133
134static void suspend_finish(suspend_state_t state)
135{
136 device_resume();
Linus Torvalds557240b2006-06-19 18:16:01 -0700137 resume_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 thaw_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700139 enable_nonboot_cpus();
David Shaohua Li1a384162005-11-23 12:36:00 -0500140 if (pm_ops && pm_ops->finish)
141 pm_ops->finish(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 pm_restore_console();
143}
144
145
146
147
Andreas Mohr3b364b82006-06-25 05:47:56 -0700148static const char * const pm_states[PM_SUSPEND_MAX] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 [PM_SUSPEND_STANDBY] = "standby",
150 [PM_SUSPEND_MEM] = "mem",
Pavel Machek57c4ce32005-09-03 15:57:06 -0700151#ifdef CONFIG_SOFTWARE_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 [PM_SUSPEND_DISK] = "disk",
Pavel Machek57c4ce32005-09-03 15:57:06 -0700153#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154};
155
Pavel Machek123d3c12005-11-29 19:34:37 -0800156static inline int valid_state(suspend_state_t state)
157{
158 /* Suspend-to-disk does not really need low-level support.
159 * It can work with reboot if needed. */
160 if (state == PM_SUSPEND_DISK)
161 return 1;
162
163 if (pm_ops && pm_ops->valid && !pm_ops->valid(state))
164 return 0;
165 return 1;
166}
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169/**
170 * enter_state - Do common work of entering low-power state.
171 * @state: pm_state structure for state we're entering.
172 *
173 * Make sure we're the only ones trying to enter a sleep state. Fail
174 * if someone has beat us to it, since we don't want anything weird to
175 * happen when we wake up.
176 * Then, do the setup for suspend, enter the state, and cleaup (after
177 * we've woken up).
178 */
179
180static int enter_state(suspend_state_t state)
181{
182 int error;
183
Pavel Machek123d3c12005-11-29 19:34:37 -0800184 if (!valid_state(state))
Shaohua Lieb9289e2005-10-30 15:00:01 -0800185 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (down_trylock(&pm_sem))
187 return -EBUSY;
188
189 if (state == PM_SUSPEND_DISK) {
190 error = pm_suspend_disk();
191 goto Unlock;
192 }
193
David Brownell82428b62005-05-09 08:07:00 -0700194 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if ((error = suspend_prepare(state)))
196 goto Unlock;
197
David Brownell82428b62005-05-09 08:07:00 -0700198 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 error = suspend_enter(state);
200
David Brownell82428b62005-05-09 08:07:00 -0700201 pr_debug("PM: Finishing wakeup.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 suspend_finish(state);
203 Unlock:
204 up(&pm_sem);
205 return error;
206}
207
208/*
209 * This is main interface to the outside world. It needs to be
210 * called from process context.
211 */
212int software_suspend(void)
213{
214 return enter_state(PM_SUSPEND_DISK);
215}
216
217
218/**
219 * pm_suspend - Externally visible function for suspending system.
220 * @state: Enumarted value of state to enter.
221 *
222 * Determine whether or not value is within range, get state
223 * structure, and enter (above).
224 */
225
226int pm_suspend(suspend_state_t state)
227{
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500228 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return enter_state(state);
230 return -EINVAL;
231}
232
233
234
235decl_subsys(power,NULL,NULL);
236
237
238/**
239 * state - control system power state.
240 *
241 * show() returns what states are supported, which is hard-coded to
242 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
243 * 'disk' (Suspend-to-Disk).
244 *
245 * store() accepts one of those strings, translates it into the
246 * proper enumerated value, and initiates a suspend transition.
247 */
248
249static ssize_t state_show(struct subsystem * subsys, char * buf)
250{
251 int i;
252 char * s = buf;
253
254 for (i = 0; i < PM_SUSPEND_MAX; i++) {
Pavel Machek123d3c12005-11-29 19:34:37 -0800255 if (pm_states[i] && valid_state(i))
256 s += sprintf(s,"%s ", pm_states[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258 s += sprintf(s,"\n");
259 return (s - buf);
260}
261
262static ssize_t state_store(struct subsystem * subsys, const char * buf, size_t n)
263{
264 suspend_state_t state = PM_SUSPEND_STANDBY;
Andreas Mohr3b364b82006-06-25 05:47:56 -0700265 const char * const *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 char *p;
267 int error;
268 int len;
269
270 p = memchr(buf, '\n', n);
271 len = p ? p - buf : n;
272
273 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
274 if (*s && !strncmp(buf, *s, len))
275 break;
276 }
dean gaudet47bb7892006-04-27 18:39:17 -0700277 if (state < PM_SUSPEND_MAX && *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 error = enter_state(state);
279 else
280 error = -EINVAL;
281 return error ? error : n;
282}
283
284power_attr(state);
285
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700286#ifdef CONFIG_PM_TRACE
287int pm_trace_enabled;
288
289static ssize_t pm_trace_show(struct subsystem * subsys, char * buf)
290{
291 return sprintf(buf, "%d\n", pm_trace_enabled);
292}
293
294static ssize_t
295pm_trace_store(struct subsystem * subsys, const char * buf, size_t n)
296{
297 int val;
298
299 if (sscanf(buf, "%d", &val) == 1) {
300 pm_trace_enabled = !!val;
301 return n;
302 }
303 return -EINVAL;
304}
305
306power_attr(pm_trace);
307
308static struct attribute * g[] = {
309 &state_attr.attr,
310 &pm_trace_attr.attr,
311 NULL,
312};
313#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314static struct attribute * g[] = {
315 &state_attr.attr,
316 NULL,
317};
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700318#endif /* CONFIG_PM_TRACE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320static struct attribute_group attr_group = {
321 .attrs = g,
322};
323
324
325static int __init pm_init(void)
326{
327 int error = subsystem_register(&power_subsys);
328 if (!error)
329 error = sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
330 return error;
331}
332
333core_initcall(pm_init);