blob: 454434716f35ccf477c7f7e535a6abc6942e72fe [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>
18
19
20#include "power.h"
21
David Shaohua Li5ae947e2005-03-18 16:27:13 -050022/*This is just an arbitrary number */
23#define FREE_PAGE_NUMBER (100)
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025DECLARE_MUTEX(pm_sem);
26
27struct pm_ops * pm_ops = NULL;
28suspend_disk_method_t pm_disk_mode = PM_DISK_SHUTDOWN;
29
30/**
31 * pm_set_ops - Set the global power method table.
32 * @ops: Pointer to ops structure.
33 */
34
35void pm_set_ops(struct pm_ops * ops)
36{
37 down(&pm_sem);
38 pm_ops = ops;
39 up(&pm_sem);
40}
41
42
43/**
44 * suspend_prepare - Do prep work before entering low-power state.
45 * @state: State we're entering.
46 *
47 * This is common code that is called for each state that we're
48 * entering. Allocate a console, stop all processes, then make sure
49 * the platform can enter the requested state.
50 */
51
52static int suspend_prepare(suspend_state_t state)
53{
54 int error = 0;
David Shaohua Li5ae947e2005-03-18 16:27:13 -050055 unsigned int free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 if (!pm_ops || !pm_ops->enter)
58 return -EPERM;
59
60 pm_prepare_console();
61
62 if (freeze_processes()) {
63 error = -EAGAIN;
64 goto Thaw;
65 }
66
David Shaohua Li5ae947e2005-03-18 16:27:13 -050067 if ((free_pages = nr_free_pages()) < FREE_PAGE_NUMBER) {
68 pr_debug("PM: free some memory\n");
69 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
70 if (nr_free_pages() < FREE_PAGE_NUMBER) {
71 error = -ENOMEM;
72 printk(KERN_ERR "PM: No enough memory\n");
73 goto Thaw;
74 }
75 }
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (pm_ops->prepare) {
78 if ((error = pm_ops->prepare(state)))
79 goto Thaw;
80 }
81
82 if ((error = device_suspend(PMSG_SUSPEND))) {
83 printk(KERN_ERR "Some devices failed to suspend\n");
84 goto Finish;
85 }
86 return 0;
87 Finish:
88 if (pm_ops->finish)
89 pm_ops->finish(state);
90 Thaw:
91 thaw_processes();
92 pm_restore_console();
93 return error;
94}
95
96
97static int suspend_enter(suspend_state_t state)
98{
99 int error = 0;
100 unsigned long flags;
101
102 local_irq_save(flags);
103
104 if ((error = device_power_down(PMSG_SUSPEND))) {
105 printk(KERN_ERR "Some devices failed to power down\n");
106 goto Done;
107 }
108 error = pm_ops->enter(state);
109 device_power_up();
110 Done:
111 local_irq_restore(flags);
112 return error;
113}
114
115
116/**
117 * suspend_finish - Do final work before exiting suspend sequence.
118 * @state: State we're coming out of.
119 *
120 * Call platform code to clean up, restart processes, and free the
121 * console that we've allocated. This is not called for suspend-to-disk.
122 */
123
124static void suspend_finish(suspend_state_t state)
125{
126 device_resume();
127 if (pm_ops && pm_ops->finish)
128 pm_ops->finish(state);
129 thaw_processes();
130 pm_restore_console();
131}
132
133
134
135
136static char * pm_states[] = {
137 [PM_SUSPEND_STANDBY] = "standby",
138 [PM_SUSPEND_MEM] = "mem",
139 [PM_SUSPEND_DISK] = "disk",
140 NULL,
141};
142
143
144/**
145 * enter_state - Do common work of entering low-power state.
146 * @state: pm_state structure for state we're entering.
147 *
148 * Make sure we're the only ones trying to enter a sleep state. Fail
149 * if someone has beat us to it, since we don't want anything weird to
150 * happen when we wake up.
151 * Then, do the setup for suspend, enter the state, and cleaup (after
152 * we've woken up).
153 */
154
155static int enter_state(suspend_state_t state)
156{
157 int error;
158
159 if (down_trylock(&pm_sem))
160 return -EBUSY;
161
162 if (state == PM_SUSPEND_DISK) {
163 error = pm_suspend_disk();
164 goto Unlock;
165 }
166
167 /* Suspend is hard to get right on SMP. */
168 if (num_online_cpus() != 1) {
169 error = -EPERM;
170 goto Unlock;
171 }
172
David Brownell82428b62005-05-09 08:07:00 -0700173 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if ((error = suspend_prepare(state)))
175 goto Unlock;
176
David Brownell82428b62005-05-09 08:07:00 -0700177 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 error = suspend_enter(state);
179
David Brownell82428b62005-05-09 08:07:00 -0700180 pr_debug("PM: Finishing wakeup.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 suspend_finish(state);
182 Unlock:
183 up(&pm_sem);
184 return error;
185}
186
187/*
188 * This is main interface to the outside world. It needs to be
189 * called from process context.
190 */
191int software_suspend(void)
192{
193 return enter_state(PM_SUSPEND_DISK);
194}
195
196
197/**
198 * pm_suspend - Externally visible function for suspending system.
199 * @state: Enumarted value of state to enter.
200 *
201 * Determine whether or not value is within range, get state
202 * structure, and enter (above).
203 */
204
205int pm_suspend(suspend_state_t state)
206{
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500207 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return enter_state(state);
209 return -EINVAL;
210}
211
212
213
214decl_subsys(power,NULL,NULL);
215
216
217/**
218 * state - control system power state.
219 *
220 * show() returns what states are supported, which is hard-coded to
221 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
222 * 'disk' (Suspend-to-Disk).
223 *
224 * store() accepts one of those strings, translates it into the
225 * proper enumerated value, and initiates a suspend transition.
226 */
227
228static ssize_t state_show(struct subsystem * subsys, char * buf)
229{
230 int i;
231 char * s = buf;
232
233 for (i = 0; i < PM_SUSPEND_MAX; i++) {
234 if (pm_states[i])
235 s += sprintf(s,"%s ",pm_states[i]);
236 }
237 s += sprintf(s,"\n");
238 return (s - buf);
239}
240
241static ssize_t state_store(struct subsystem * subsys, const char * buf, size_t n)
242{
243 suspend_state_t state = PM_SUSPEND_STANDBY;
244 char ** s;
245 char *p;
246 int error;
247 int len;
248
249 p = memchr(buf, '\n', n);
250 len = p ? p - buf : n;
251
252 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
253 if (*s && !strncmp(buf, *s, len))
254 break;
255 }
256 if (*s)
257 error = enter_state(state);
258 else
259 error = -EINVAL;
260 return error ? error : n;
261}
262
263power_attr(state);
264
265static struct attribute * g[] = {
266 &state_attr.attr,
267 NULL,
268};
269
270static struct attribute_group attr_group = {
271 .attrs = g,
272};
273
274
275static int __init pm_init(void)
276{
277 int error = subsystem_register(&power_subsys);
278 if (!error)
279 error = sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
280 return error;
281}
282
283core_initcall(pm_init);