Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
4cf30348 | 11 | #include <linux/module.h> |
1da177e4 LT |
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/pm.h> | |
6cc07191 | 19 | #include <linux/console.h> |
e3920fb4 | 20 | #include <linux/cpu.h> |
c5c6ba4e | 21 | #include <linux/resume-trace.h> |
7dfb7103 | 22 | #include <linux/freezer.h> |
96177299 | 23 | #include <linux/vmstat.h> |
1da177e4 LT |
24 | |
25 | #include "power.h" | |
26 | ||
5ae947ec DSL |
27 | /*This is just an arbitrary number */ |
28 | #define FREE_PAGE_NUMBER (100) | |
29 | ||
a6d70980 | 30 | DEFINE_MUTEX(pm_mutex); |
1da177e4 | 31 | |
123d3c13 | 32 | struct pm_ops *pm_ops; |
9185cfa9 | 33 | suspend_disk_method_t pm_disk_mode = PM_DISK_PLATFORM; |
1da177e4 LT |
34 | |
35 | /** | |
36 | * pm_set_ops - Set the global power method table. | |
37 | * @ops: Pointer to ops structure. | |
38 | */ | |
39 | ||
40 | void pm_set_ops(struct pm_ops * ops) | |
41 | { | |
a6d70980 | 42 | mutex_lock(&pm_mutex); |
1da177e4 | 43 | pm_ops = ops; |
a6d70980 | 44 | mutex_unlock(&pm_mutex); |
1da177e4 LT |
45 | } |
46 | ||
47 | ||
48 | /** | |
49 | * suspend_prepare - Do prep work before entering low-power state. | |
50 | * @state: State we're entering. | |
51 | * | |
52 | * This is common code that is called for each state that we're | |
53 | * entering. Allocate a console, stop all processes, then make sure | |
54 | * the platform can enter the requested state. | |
55 | */ | |
56 | ||
57 | static int suspend_prepare(suspend_state_t state) | |
58 | { | |
e3920fb4 | 59 | int error; |
5ae947ec | 60 | unsigned int free_pages; |
1da177e4 LT |
61 | |
62 | if (!pm_ops || !pm_ops->enter) | |
63 | return -EPERM; | |
64 | ||
65 | pm_prepare_console(); | |
66 | ||
e3920fb4 RW |
67 | error = disable_nonboot_cpus(); |
68 | if (error) | |
5a72e04d | 69 | goto Enable_cpu; |
5a72e04d | 70 | |
1da177e4 LT |
71 | if (freeze_processes()) { |
72 | error = -EAGAIN; | |
73 | goto Thaw; | |
74 | } | |
75 | ||
96177299 CL |
76 | if ((free_pages = global_page_state(NR_FREE_PAGES)) |
77 | < FREE_PAGE_NUMBER) { | |
5ae947ec DSL |
78 | pr_debug("PM: free some memory\n"); |
79 | shrink_all_memory(FREE_PAGE_NUMBER - free_pages); | |
80 | if (nr_free_pages() < FREE_PAGE_NUMBER) { | |
81 | error = -ENOMEM; | |
82 | printk(KERN_ERR "PM: No enough memory\n"); | |
83 | goto Thaw; | |
84 | } | |
85 | } | |
86 | ||
1da177e4 LT |
87 | if (pm_ops->prepare) { |
88 | if ((error = pm_ops->prepare(state))) | |
89 | goto Thaw; | |
90 | } | |
91 | ||
557240b4 | 92 | suspend_console(); |
1da177e4 LT |
93 | if ((error = device_suspend(PMSG_SUSPEND))) { |
94 | printk(KERN_ERR "Some devices failed to suspend\n"); | |
95 | goto Finish; | |
96 | } | |
97 | return 0; | |
98 | Finish: | |
99 | if (pm_ops->finish) | |
100 | pm_ops->finish(state); | |
101 | Thaw: | |
102 | thaw_processes(); | |
5a72e04d LS |
103 | Enable_cpu: |
104 | enable_nonboot_cpus(); | |
1da177e4 LT |
105 | pm_restore_console(); |
106 | return error; | |
107 | } | |
108 | ||
109 | ||
9b238205 | 110 | int suspend_enter(suspend_state_t state) |
1da177e4 LT |
111 | { |
112 | int error = 0; | |
113 | unsigned long flags; | |
114 | ||
115 | local_irq_save(flags); | |
116 | ||
117 | if ((error = device_power_down(PMSG_SUSPEND))) { | |
118 | printk(KERN_ERR "Some devices failed to power down\n"); | |
119 | goto Done; | |
120 | } | |
121 | error = pm_ops->enter(state); | |
122 | device_power_up(); | |
123 | Done: | |
124 | local_irq_restore(flags); | |
125 | return error; | |
126 | } | |
127 | ||
128 | ||
129 | /** | |
130 | * suspend_finish - Do final work before exiting suspend sequence. | |
131 | * @state: State we're coming out of. | |
132 | * | |
133 | * Call platform code to clean up, restart processes, and free the | |
134 | * console that we've allocated. This is not called for suspend-to-disk. | |
135 | */ | |
136 | ||
137 | static void suspend_finish(suspend_state_t state) | |
138 | { | |
139 | device_resume(); | |
557240b4 | 140 | resume_console(); |
1da177e4 | 141 | thaw_processes(); |
5a72e04d | 142 | enable_nonboot_cpus(); |
1a38416c DSL |
143 | if (pm_ops && pm_ops->finish) |
144 | pm_ops->finish(state); | |
1da177e4 LT |
145 | pm_restore_console(); |
146 | } | |
147 | ||
148 | ||
149 | ||
150 | ||
3b364b8d | 151 | static const char * const pm_states[PM_SUSPEND_MAX] = { |
1da177e4 LT |
152 | [PM_SUSPEND_STANDBY] = "standby", |
153 | [PM_SUSPEND_MEM] = "mem", | |
57c4ce3c | 154 | #ifdef CONFIG_SOFTWARE_SUSPEND |
1da177e4 | 155 | [PM_SUSPEND_DISK] = "disk", |
57c4ce3c | 156 | #endif |
1da177e4 LT |
157 | }; |
158 | ||
123d3c13 PM |
159 | static inline int valid_state(suspend_state_t state) |
160 | { | |
161 | /* Suspend-to-disk does not really need low-level support. | |
162 | * It can work with reboot if needed. */ | |
163 | if (state == PM_SUSPEND_DISK) | |
164 | return 1; | |
165 | ||
166 | if (pm_ops && pm_ops->valid && !pm_ops->valid(state)) | |
167 | return 0; | |
168 | return 1; | |
169 | } | |
170 | ||
1da177e4 LT |
171 | |
172 | /** | |
173 | * enter_state - Do common work of entering low-power state. | |
174 | * @state: pm_state structure for state we're entering. | |
175 | * | |
176 | * Make sure we're the only ones trying to enter a sleep state. Fail | |
177 | * if someone has beat us to it, since we don't want anything weird to | |
178 | * happen when we wake up. | |
179 | * Then, do the setup for suspend, enter the state, and cleaup (after | |
180 | * we've woken up). | |
181 | */ | |
182 | ||
183 | static int enter_state(suspend_state_t state) | |
184 | { | |
185 | int error; | |
186 | ||
123d3c13 | 187 | if (!valid_state(state)) |
eb9289eb | 188 | return -ENODEV; |
a6d70980 | 189 | if (!mutex_trylock(&pm_mutex)) |
1da177e4 LT |
190 | return -EBUSY; |
191 | ||
192 | if (state == PM_SUSPEND_DISK) { | |
193 | error = pm_suspend_disk(); | |
194 | goto Unlock; | |
195 | } | |
196 | ||
82428b62 | 197 | pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]); |
1da177e4 LT |
198 | if ((error = suspend_prepare(state))) |
199 | goto Unlock; | |
200 | ||
82428b62 | 201 | pr_debug("PM: Entering %s sleep\n", pm_states[state]); |
1da177e4 LT |
202 | error = suspend_enter(state); |
203 | ||
82428b62 | 204 | pr_debug("PM: Finishing wakeup.\n"); |
1da177e4 LT |
205 | suspend_finish(state); |
206 | Unlock: | |
a6d70980 | 207 | mutex_unlock(&pm_mutex); |
1da177e4 LT |
208 | return error; |
209 | } | |
210 | ||
211 | /* | |
212 | * This is main interface to the outside world. It needs to be | |
213 | * called from process context. | |
214 | */ | |
215 | int software_suspend(void) | |
216 | { | |
217 | return enter_state(PM_SUSPEND_DISK); | |
218 | } | |
219 | ||
220 | ||
221 | /** | |
222 | * pm_suspend - Externally visible function for suspending system. | |
223 | * @state: Enumarted value of state to enter. | |
224 | * | |
225 | * Determine whether or not value is within range, get state | |
226 | * structure, and enter (above). | |
227 | */ | |
228 | ||
229 | int pm_suspend(suspend_state_t state) | |
230 | { | |
e2a5b420 | 231 | if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX) |
1da177e4 LT |
232 | return enter_state(state); |
233 | return -EINVAL; | |
234 | } | |
235 | ||
4cf30348 | 236 | EXPORT_SYMBOL(pm_suspend); |
1da177e4 LT |
237 | |
238 | decl_subsys(power,NULL,NULL); | |
239 | ||
240 | ||
241 | /** | |
242 | * state - control system power state. | |
243 | * | |
244 | * show() returns what states are supported, which is hard-coded to | |
245 | * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and | |
246 | * 'disk' (Suspend-to-Disk). | |
247 | * | |
248 | * store() accepts one of those strings, translates it into the | |
249 | * proper enumerated value, and initiates a suspend transition. | |
250 | */ | |
251 | ||
252 | static ssize_t state_show(struct subsystem * subsys, char * buf) | |
253 | { | |
254 | int i; | |
255 | char * s = buf; | |
256 | ||
257 | for (i = 0; i < PM_SUSPEND_MAX; i++) { | |
123d3c13 PM |
258 | if (pm_states[i] && valid_state(i)) |
259 | s += sprintf(s,"%s ", pm_states[i]); | |
1da177e4 LT |
260 | } |
261 | s += sprintf(s,"\n"); | |
262 | return (s - buf); | |
263 | } | |
264 | ||
265 | static ssize_t state_store(struct subsystem * subsys, const char * buf, size_t n) | |
266 | { | |
267 | suspend_state_t state = PM_SUSPEND_STANDBY; | |
3b364b8d | 268 | const char * const *s; |
1da177e4 LT |
269 | char *p; |
270 | int error; | |
271 | int len; | |
272 | ||
273 | p = memchr(buf, '\n', n); | |
274 | len = p ? p - buf : n; | |
275 | ||
276 | for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) { | |
277 | if (*s && !strncmp(buf, *s, len)) | |
278 | break; | |
279 | } | |
47bb7899 | 280 | if (state < PM_SUSPEND_MAX && *s) |
1da177e4 LT |
281 | error = enter_state(state); |
282 | else | |
283 | error = -EINVAL; | |
284 | return error ? error : n; | |
285 | } | |
286 | ||
287 | power_attr(state); | |
288 | ||
c5c6ba4e RW |
289 | #ifdef CONFIG_PM_TRACE |
290 | int pm_trace_enabled; | |
291 | ||
292 | static ssize_t pm_trace_show(struct subsystem * subsys, char * buf) | |
293 | { | |
294 | return sprintf(buf, "%d\n", pm_trace_enabled); | |
295 | } | |
296 | ||
297 | static ssize_t | |
298 | pm_trace_store(struct subsystem * subsys, const char * buf, size_t n) | |
299 | { | |
300 | int val; | |
301 | ||
302 | if (sscanf(buf, "%d", &val) == 1) { | |
303 | pm_trace_enabled = !!val; | |
304 | return n; | |
305 | } | |
306 | return -EINVAL; | |
307 | } | |
308 | ||
309 | power_attr(pm_trace); | |
310 | ||
311 | static struct attribute * g[] = { | |
312 | &state_attr.attr, | |
313 | &pm_trace_attr.attr, | |
314 | NULL, | |
315 | }; | |
316 | #else | |
1da177e4 LT |
317 | static struct attribute * g[] = { |
318 | &state_attr.attr, | |
319 | NULL, | |
320 | }; | |
c5c6ba4e | 321 | #endif /* CONFIG_PM_TRACE */ |
1da177e4 LT |
322 | |
323 | static struct attribute_group attr_group = { | |
324 | .attrs = g, | |
325 | }; | |
326 | ||
327 | ||
328 | static int __init pm_init(void) | |
329 | { | |
330 | int error = subsystem_register(&power_subsys); | |
331 | if (!error) | |
332 | error = sysfs_create_group(&power_subsys.kset.kobj,&attr_group); | |
333 | return error; | |
334 | } | |
335 | ||
336 | core_initcall(pm_init); |