watchdog: xilinx: Make of_device_id array const
[linux/fpc-iii.git] / kernel / power / suspend.c
blob963e6d0f050bd8ac2c62a0fee2838118e2993943
1 /*
2 * kernel/power/suspend.c - Suspend to RAM and standby functionality.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
6 * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
8 * This file is released under the GPLv2.
9 */
11 #include <linux/string.h>
12 #include <linux/delay.h>
13 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/console.h>
16 #include <linux/cpu.h>
17 #include <linux/cpuidle.h>
18 #include <linux/syscalls.h>
19 #include <linux/gfp.h>
20 #include <linux/io.h>
21 #include <linux/kernel.h>
22 #include <linux/list.h>
23 #include <linux/mm.h>
24 #include <linux/slab.h>
25 #include <linux/export.h>
26 #include <linux/suspend.h>
27 #include <linux/syscore_ops.h>
28 #include <linux/ftrace.h>
29 #include <trace/events/power.h>
30 #include <linux/compiler.h>
32 #include "power.h"
34 struct pm_sleep_state pm_states[PM_SUSPEND_MAX] = {
35 [PM_SUSPEND_FREEZE] = { .label = "freeze", .state = PM_SUSPEND_FREEZE },
36 [PM_SUSPEND_STANDBY] = { .label = "standby", },
37 [PM_SUSPEND_MEM] = { .label = "mem", },
40 static const struct platform_suspend_ops *suspend_ops;
41 static const struct platform_freeze_ops *freeze_ops;
43 static bool need_suspend_ops(suspend_state_t state)
45 return state > PM_SUSPEND_FREEZE;
48 static DECLARE_WAIT_QUEUE_HEAD(suspend_freeze_wait_head);
49 static bool suspend_freeze_wake;
51 void freeze_set_ops(const struct platform_freeze_ops *ops)
53 lock_system_sleep();
54 freeze_ops = ops;
55 unlock_system_sleep();
58 static void freeze_begin(void)
60 suspend_freeze_wake = false;
63 static void freeze_enter(void)
65 cpuidle_use_deepest_state(true);
66 cpuidle_resume();
67 wait_event(suspend_freeze_wait_head, suspend_freeze_wake);
68 cpuidle_pause();
69 cpuidle_use_deepest_state(false);
72 void freeze_wake(void)
74 suspend_freeze_wake = true;
75 wake_up(&suspend_freeze_wait_head);
77 EXPORT_SYMBOL_GPL(freeze_wake);
79 static bool valid_state(suspend_state_t state)
82 * PM_SUSPEND_STANDBY and PM_SUSPEND_MEM states need low level
83 * support and need to be valid to the low level
84 * implementation, no valid callback implies that none are valid.
86 return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
90 * If this is set, the "mem" label always corresponds to the deepest sleep state
91 * available, the "standby" label corresponds to the second deepest sleep state
92 * available (if any), and the "freeze" label corresponds to the remaining
93 * available sleep state (if there is one).
95 static bool relative_states;
97 static int __init sleep_states_setup(char *str)
99 relative_states = !strncmp(str, "1", 1);
100 if (relative_states) {
101 pm_states[PM_SUSPEND_MEM].state = PM_SUSPEND_FREEZE;
102 pm_states[PM_SUSPEND_FREEZE].state = 0;
104 return 1;
107 __setup("relative_sleep_states=", sleep_states_setup);
110 * suspend_set_ops - Set the global suspend method table.
111 * @ops: Suspend operations to use.
113 void suspend_set_ops(const struct platform_suspend_ops *ops)
115 suspend_state_t i;
116 int j = PM_SUSPEND_MAX - 1;
118 lock_system_sleep();
120 suspend_ops = ops;
121 for (i = PM_SUSPEND_MEM; i >= PM_SUSPEND_STANDBY; i--)
122 if (valid_state(i))
123 pm_states[j--].state = i;
124 else if (!relative_states)
125 pm_states[j--].state = 0;
127 pm_states[j--].state = PM_SUSPEND_FREEZE;
128 while (j >= PM_SUSPEND_MIN)
129 pm_states[j--].state = 0;
131 unlock_system_sleep();
133 EXPORT_SYMBOL_GPL(suspend_set_ops);
136 * suspend_valid_only_mem - Generic memory-only valid callback.
138 * Platform drivers that implement mem suspend only and only need to check for
139 * that in their .valid() callback can use this instead of rolling their own
140 * .valid() callback.
142 int suspend_valid_only_mem(suspend_state_t state)
144 return state == PM_SUSPEND_MEM;
146 EXPORT_SYMBOL_GPL(suspend_valid_only_mem);
148 static int suspend_test(int level)
150 #ifdef CONFIG_PM_DEBUG
151 if (pm_test_level == level) {
152 printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
153 mdelay(5000);
154 return 1;
156 #endif /* !CONFIG_PM_DEBUG */
157 return 0;
161 * suspend_prepare - Prepare for entering system sleep state.
163 * Common code run for every system sleep state that can be entered (except for
164 * hibernation). Run suspend notifiers, allocate the "suspend" console and
165 * freeze processes.
167 static int suspend_prepare(suspend_state_t state)
169 int error;
171 if (need_suspend_ops(state) && (!suspend_ops || !suspend_ops->enter))
172 return -EPERM;
174 pm_prepare_console();
176 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
177 if (error)
178 goto Finish;
180 error = suspend_freeze_processes();
181 if (!error)
182 return 0;
184 suspend_stats.failed_freeze++;
185 dpm_save_failed_step(SUSPEND_FREEZE);
186 Finish:
187 pm_notifier_call_chain(PM_POST_SUSPEND);
188 pm_restore_console();
189 return error;
192 /* default implementation */
193 void __weak arch_suspend_disable_irqs(void)
195 local_irq_disable();
198 /* default implementation */
199 void __weak arch_suspend_enable_irqs(void)
201 local_irq_enable();
205 * suspend_enter - Make the system enter the given sleep state.
206 * @state: System sleep state to enter.
207 * @wakeup: Returns information that the sleep state should not be re-entered.
209 * This function should be called after devices have been suspended.
211 static int suspend_enter(suspend_state_t state, bool *wakeup)
213 int error;
215 if (need_suspend_ops(state) && suspend_ops->prepare) {
216 error = suspend_ops->prepare();
217 if (error)
218 goto Platform_finish;
221 error = dpm_suspend_end(PMSG_SUSPEND);
222 if (error) {
223 printk(KERN_ERR "PM: Some devices failed to power down\n");
224 goto Platform_finish;
227 if (need_suspend_ops(state) && suspend_ops->prepare_late) {
228 error = suspend_ops->prepare_late();
229 if (error)
230 goto Platform_wake;
233 if (suspend_test(TEST_PLATFORM))
234 goto Platform_wake;
237 * PM_SUSPEND_FREEZE equals
238 * frozen processes + suspended devices + idle processors.
239 * Thus we should invoke freeze_enter() soon after
240 * all the devices are suspended.
242 if (state == PM_SUSPEND_FREEZE) {
243 freeze_enter();
244 goto Platform_wake;
247 ftrace_stop();
248 error = disable_nonboot_cpus();
249 if (error || suspend_test(TEST_CPUS))
250 goto Enable_cpus;
252 arch_suspend_disable_irqs();
253 BUG_ON(!irqs_disabled());
255 error = syscore_suspend();
256 if (!error) {
257 *wakeup = pm_wakeup_pending();
258 if (!(suspend_test(TEST_CORE) || *wakeup)) {
259 error = suspend_ops->enter(state);
260 events_check_enabled = false;
262 syscore_resume();
265 arch_suspend_enable_irqs();
266 BUG_ON(irqs_disabled());
268 Enable_cpus:
269 enable_nonboot_cpus();
270 ftrace_start();
272 Platform_wake:
273 if (need_suspend_ops(state) && suspend_ops->wake)
274 suspend_ops->wake();
276 dpm_resume_start(PMSG_RESUME);
278 Platform_finish:
279 if (need_suspend_ops(state) && suspend_ops->finish)
280 suspend_ops->finish();
282 return error;
286 * suspend_devices_and_enter - Suspend devices and enter system sleep state.
287 * @state: System sleep state to enter.
289 int suspend_devices_and_enter(suspend_state_t state)
291 int error;
292 bool wakeup = false;
294 if (need_suspend_ops(state) && !suspend_ops)
295 return -ENOSYS;
297 trace_machine_suspend(state);
298 if (need_suspend_ops(state) && suspend_ops->begin) {
299 error = suspend_ops->begin(state);
300 if (error)
301 goto Close;
302 } else if (state == PM_SUSPEND_FREEZE && freeze_ops->begin) {
303 error = freeze_ops->begin();
304 if (error)
305 goto Close;
307 suspend_console();
308 suspend_test_start();
309 error = dpm_suspend_start(PMSG_SUSPEND);
310 if (error) {
311 pr_err("PM: Some devices failed to suspend, or early wake event detected\n");
312 goto Recover_platform;
314 suspend_test_finish("suspend devices");
315 if (suspend_test(TEST_DEVICES))
316 goto Recover_platform;
318 do {
319 error = suspend_enter(state, &wakeup);
320 } while (!error && !wakeup && need_suspend_ops(state)
321 && suspend_ops->suspend_again && suspend_ops->suspend_again());
323 Resume_devices:
324 suspend_test_start();
325 dpm_resume_end(PMSG_RESUME);
326 suspend_test_finish("resume devices");
327 resume_console();
328 Close:
329 if (need_suspend_ops(state) && suspend_ops->end)
330 suspend_ops->end();
331 else if (state == PM_SUSPEND_FREEZE && freeze_ops->end)
332 freeze_ops->end();
334 trace_machine_suspend(PWR_EVENT_EXIT);
335 return error;
337 Recover_platform:
338 if (need_suspend_ops(state) && suspend_ops->recover)
339 suspend_ops->recover();
340 goto Resume_devices;
344 * suspend_finish - Clean up before finishing the suspend sequence.
346 * Call platform code to clean up, restart processes, and free the console that
347 * we've allocated. This routine is not called for hibernation.
349 static void suspend_finish(void)
351 suspend_thaw_processes();
352 pm_notifier_call_chain(PM_POST_SUSPEND);
353 pm_restore_console();
357 * enter_state - Do common work needed to enter system sleep state.
358 * @state: System sleep state to enter.
360 * Make sure that no one else is trying to put the system into a sleep state.
361 * Fail if that's not the case. Otherwise, prepare for system suspend, make the
362 * system enter the given sleep state and clean up after wakeup.
364 static int enter_state(suspend_state_t state)
366 int error;
368 if (state == PM_SUSPEND_FREEZE) {
369 #ifdef CONFIG_PM_DEBUG
370 if (pm_test_level != TEST_NONE && pm_test_level <= TEST_CPUS) {
371 pr_warning("PM: Unsupported test mode for freeze state,"
372 "please choose none/freezer/devices/platform.\n");
373 return -EAGAIN;
375 #endif
376 } else if (!valid_state(state)) {
377 return -EINVAL;
379 if (!mutex_trylock(&pm_mutex))
380 return -EBUSY;
382 if (state == PM_SUSPEND_FREEZE)
383 freeze_begin();
385 printk(KERN_INFO "PM: Syncing filesystems ... ");
386 sys_sync();
387 printk("done.\n");
389 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state].label);
390 error = suspend_prepare(state);
391 if (error)
392 goto Unlock;
394 if (suspend_test(TEST_FREEZER))
395 goto Finish;
397 pr_debug("PM: Entering %s sleep\n", pm_states[state].label);
398 pm_restrict_gfp_mask();
399 error = suspend_devices_and_enter(state);
400 pm_restore_gfp_mask();
402 Finish:
403 pr_debug("PM: Finishing wakeup.\n");
404 suspend_finish();
405 Unlock:
406 mutex_unlock(&pm_mutex);
407 return error;
411 * pm_suspend - Externally visible function for suspending the system.
412 * @state: System sleep state to enter.
414 * Check if the value of @state represents one of the supported states,
415 * execute enter_state() and update system suspend statistics.
417 int pm_suspend(suspend_state_t state)
419 int error;
421 if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
422 return -EINVAL;
424 error = enter_state(state);
425 if (error) {
426 suspend_stats.fail++;
427 dpm_save_failed_errno(error);
428 } else {
429 suspend_stats.success++;
431 return error;
433 EXPORT_SYMBOL(pm_suspend);