~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/power/suspend.c

Version: ~ [ linux-5.6-rc1 ] ~ [ linux-5.5.2 ] ~ [ linux-5.4.17 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.102 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.170 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.213 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.213 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.81 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * kernel/power/suspend.c - Suspend to RAM and standby functionality.
  3  *
  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.
  7  *
  8  * This file is released under the GPLv2.
  9  */
 10 
 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/syscalls.h>
 18 #include <linux/gfp.h>
 19 #include <linux/io.h>
 20 #include <linux/kernel.h>
 21 #include <linux/list.h>
 22 #include <linux/mm.h>
 23 #include <linux/slab.h>
 24 #include <linux/export.h>
 25 #include <linux/suspend.h>
 26 #include <linux/syscore_ops.h>
 27 #include <linux/ftrace.h>
 28 #include <trace/events/power.h>
 29 
 30 #include "power.h"
 31 
 32 const char *const pm_states[PM_SUSPEND_MAX] = {
 33         [PM_SUSPEND_FREEZE]     = "freeze",
 34         [PM_SUSPEND_STANDBY]    = "standby",
 35         [PM_SUSPEND_MEM]        = "mem",
 36 };
 37 
 38 static const struct platform_suspend_ops *suspend_ops;
 39 
 40 static bool need_suspend_ops(suspend_state_t state)
 41 {
 42         return !!(state > PM_SUSPEND_FREEZE);
 43 }
 44 
 45 static DECLARE_WAIT_QUEUE_HEAD(suspend_freeze_wait_head);
 46 static bool suspend_freeze_wake;
 47 
 48 static void freeze_begin(void)
 49 {
 50         suspend_freeze_wake = false;
 51 }
 52 
 53 static void freeze_enter(void)
 54 {
 55         wait_event(suspend_freeze_wait_head, suspend_freeze_wake);
 56 }
 57 
 58 void freeze_wake(void)
 59 {
 60         suspend_freeze_wake = true;
 61         wake_up(&suspend_freeze_wait_head);
 62 }
 63 EXPORT_SYMBOL_GPL(freeze_wake);
 64 
 65 /**
 66  * suspend_set_ops - Set the global suspend method table.
 67  * @ops: Suspend operations to use.
 68  */
 69 void suspend_set_ops(const struct platform_suspend_ops *ops)
 70 {
 71         lock_system_sleep();
 72         suspend_ops = ops;
 73         unlock_system_sleep();
 74 }
 75 EXPORT_SYMBOL_GPL(suspend_set_ops);
 76 
 77 bool valid_state(suspend_state_t state)
 78 {
 79         if (state == PM_SUSPEND_FREEZE) {
 80 #ifdef CONFIG_PM_DEBUG
 81                 if (pm_test_level != TEST_NONE &&
 82                     pm_test_level != TEST_FREEZER &&
 83                     pm_test_level != TEST_DEVICES &&
 84                     pm_test_level != TEST_PLATFORM) {
 85                         printk(KERN_WARNING "Unsupported pm_test mode for "
 86                                         "freeze state, please choose "
 87                                         "none/freezer/devices/platform.\n");
 88                         return false;
 89                 }
 90 #endif
 91                         return true;
 92         }
 93         /*
 94          * PM_SUSPEND_STANDBY and PM_SUSPEND_MEMORY states need lowlevel
 95          * support and need to be valid to the lowlevel
 96          * implementation, no valid callback implies that none are valid.
 97          */
 98         return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
 99 }
100 
101 /**
102  * suspend_valid_only_mem - Generic memory-only valid callback.
103  *
104  * Platform drivers that implement mem suspend only and only need to check for
105  * that in their .valid() callback can use this instead of rolling their own
106  * .valid() callback.
107  */
108 int suspend_valid_only_mem(suspend_state_t state)
109 {
110         return state == PM_SUSPEND_MEM;
111 }
112 EXPORT_SYMBOL_GPL(suspend_valid_only_mem);
113 
114 static int suspend_test(int level)
115 {
116 #ifdef CONFIG_PM_DEBUG
117         if (pm_test_level == level) {
118                 printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
119                 mdelay(5000);
120                 return 1;
121         }
122 #endif /* !CONFIG_PM_DEBUG */
123         return 0;
124 }
125 
126 /**
127  * suspend_prepare - Prepare for entering system sleep state.
128  *
129  * Common code run for every system sleep state that can be entered (except for
130  * hibernation).  Run suspend notifiers, allocate the "suspend" console and
131  * freeze processes.
132  */
133 static int suspend_prepare(suspend_state_t state)
134 {
135         int error;
136 
137         if (need_suspend_ops(state) && (!suspend_ops || !suspend_ops->enter))
138                 return -EPERM;
139 
140         pm_prepare_console();
141 
142         error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
143         if (error)
144                 goto Finish;
145 
146         error = suspend_freeze_processes();
147         if (!error)
148                 return 0;
149 
150         suspend_stats.failed_freeze++;
151         dpm_save_failed_step(SUSPEND_FREEZE);
152  Finish:
153         pm_notifier_call_chain(PM_POST_SUSPEND);
154         pm_restore_console();
155         return error;
156 }
157 
158 /* default implementation */
159 void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
160 {
161         local_irq_disable();
162 }
163 
164 /* default implementation */
165 void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
166 {
167         local_irq_enable();
168 }
169 
170 /**
171  * suspend_enter - Make the system enter the given sleep state.
172  * @state: System sleep state to enter.
173  * @wakeup: Returns information that the sleep state should not be re-entered.
174  *
175  * This function should be called after devices have been suspended.
176  */
177 static int suspend_enter(suspend_state_t state, bool *wakeup)
178 {
179         int error;
180 
181         if (need_suspend_ops(state) && suspend_ops->prepare) {
182                 error = suspend_ops->prepare();
183                 if (error)
184                         goto Platform_finish;
185         }
186 
187         error = dpm_suspend_end(PMSG_SUSPEND);
188         if (error) {
189                 printk(KERN_ERR "PM: Some devices failed to power down\n");
190                 goto Platform_finish;
191         }
192 
193         if (need_suspend_ops(state) && suspend_ops->prepare_late) {
194                 error = suspend_ops->prepare_late();
195                 if (error)
196                         goto Platform_wake;
197         }
198 
199         if (suspend_test(TEST_PLATFORM))
200                 goto Platform_wake;
201 
202         /*
203          * PM_SUSPEND_FREEZE equals
204          * frozen processes + suspended devices + idle processors.
205          * Thus we should invoke freeze_enter() soon after
206          * all the devices are suspended.
207          */
208         if (state == PM_SUSPEND_FREEZE) {
209                 freeze_enter();
210                 goto Platform_wake;
211         }
212 
213         error = disable_nonboot_cpus();
214         if (error || suspend_test(TEST_CPUS))
215                 goto Enable_cpus;
216 
217         arch_suspend_disable_irqs();
218         BUG_ON(!irqs_disabled());
219 
220         error = syscore_suspend();
221         if (!error) {
222                 *wakeup = pm_wakeup_pending();
223                 if (!(suspend_test(TEST_CORE) || *wakeup)) {
224                         error = suspend_ops->enter(state);
225                         events_check_enabled = false;
226                 }
227                 syscore_resume();
228         }
229 
230         arch_suspend_enable_irqs();
231         BUG_ON(irqs_disabled());
232 
233  Enable_cpus:
234         enable_nonboot_cpus();
235 
236  Platform_wake:
237         if (need_suspend_ops(state) && suspend_ops->wake)
238                 suspend_ops->wake();
239 
240         dpm_resume_start(PMSG_RESUME);
241 
242  Platform_finish:
243         if (need_suspend_ops(state) && suspend_ops->finish)
244                 suspend_ops->finish();
245 
246         return error;
247 }
248 
249 /**
250  * suspend_devices_and_enter - Suspend devices and enter system sleep state.
251  * @state: System sleep state to enter.
252  */
253 int suspend_devices_and_enter(suspend_state_t state)
254 {
255         int error;
256         bool wakeup = false;
257 
258         if (need_suspend_ops(state) && !suspend_ops)
259                 return -ENOSYS;
260 
261         trace_machine_suspend(state);
262         if (need_suspend_ops(state) && suspend_ops->begin) {
263                 error = suspend_ops->begin(state);
264                 if (error)
265                         goto Close;
266         }
267         suspend_console();
268         ftrace_stop();
269         suspend_test_start();
270         error = dpm_suspend_start(PMSG_SUSPEND);
271         if (error) {
272                 pr_err("PM: Some devices failed to suspend, or early wake event detected\n");
273                 goto Recover_platform;
274         }
275         suspend_test_finish("suspend devices");
276         if (suspend_test(TEST_DEVICES))
277                 goto Recover_platform;
278 
279         do {
280                 error = suspend_enter(state, &wakeup);
281         } while (!error && !wakeup && need_suspend_ops(state)
282                 && suspend_ops->suspend_again && suspend_ops->suspend_again());
283 
284  Resume_devices:
285         suspend_test_start();
286         dpm_resume_end(PMSG_RESUME);
287         suspend_test_finish("resume devices");
288         ftrace_start();
289         resume_console();
290  Close:
291         if (need_suspend_ops(state) && suspend_ops->end)
292                 suspend_ops->end();
293         trace_machine_suspend(PWR_EVENT_EXIT);
294         return error;
295 
296  Recover_platform:
297         if (need_suspend_ops(state) && suspend_ops->recover)
298                 suspend_ops->recover();
299         goto Resume_devices;
300 }
301 
302 /**
303  * suspend_finish - Clean up before finishing the suspend sequence.
304  *
305  * Call platform code to clean up, restart processes, and free the console that
306  * we've allocated. This routine is not called for hibernation.
307  */
308 static void suspend_finish(void)
309 {
310         suspend_thaw_processes();
311         pm_notifier_call_chain(PM_POST_SUSPEND);
312         pm_restore_console();
313 }
314 
315 /**
316  * enter_state - Do common work needed to enter system sleep state.
317  * @state: System sleep state to enter.
318  *
319  * Make sure that no one else is trying to put the system into a sleep state.
320  * Fail if that's not the case.  Otherwise, prepare for system suspend, make the
321  * system enter the given sleep state and clean up after wakeup.
322  */
323 static int enter_state(suspend_state_t state)
324 {
325         int error;
326 
327         if (!valid_state(state))
328                 return -ENODEV;
329 
330         if (!mutex_trylock(&pm_mutex))
331                 return -EBUSY;
332 
333         if (state == PM_SUSPEND_FREEZE)
334                 freeze_begin();
335 
336         printk(KERN_INFO "PM: Syncing filesystems ... ");
337         sys_sync();
338         printk("done.\n");
339 
340         pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
341         error = suspend_prepare(state);
342         if (error)
343                 goto Unlock;
344 
345         if (suspend_test(TEST_FREEZER))
346                 goto Finish;
347 
348         pr_debug("PM: Entering %s sleep\n", pm_states[state]);
349         pm_restrict_gfp_mask();
350         error = suspend_devices_and_enter(state);
351         pm_restore_gfp_mask();
352 
353  Finish:
354         pr_debug("PM: Finishing wakeup.\n");
355         suspend_finish();
356  Unlock:
357         mutex_unlock(&pm_mutex);
358         return error;
359 }
360 
361 /**
362  * pm_suspend - Externally visible function for suspending the system.
363  * @state: System sleep state to enter.
364  *
365  * Check if the value of @state represents one of the supported states,
366  * execute enter_state() and update system suspend statistics.
367  */
368 int pm_suspend(suspend_state_t state)
369 {
370         int error;
371 
372         if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
373                 return -EINVAL;
374 
375         error = enter_state(state);
376         if (error) {
377                 suspend_stats.fail++;
378                 dpm_save_failed_errno(error);
379         } else {
380                 suspend_stats.success++;
381         }
382         return error;
383 }
384 EXPORT_SYMBOL(pm_suspend);
385 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp