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