Linux Audio

Check our new training course

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