1/*
2 * kernel/power/disk.c - Suspend-to-disk support.
3 *
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
6 * Copyright (c) 2004 Pavel Machek <pavel@suse.cz>
7 *
8 * This file is released under the GPLv2.
9 *
10 */
11
12#include <linux/suspend.h>
13#include <linux/syscalls.h>
14#include <linux/reboot.h>
15#include <linux/string.h>
16#include <linux/device.h>
17#include <linux/delay.h>
18#include <linux/fs.h>
19#include <linux/mount.h>
20#include <linux/pm.h>
21#include <linux/console.h>
22#include <linux/cpu.h>
23#include <linux/freezer.h>
24
25#include "power.h"
26
27
28static int noresume = 0;
29char resume_file[256] = CONFIG_PM_STD_PARTITION;
30dev_t swsusp_resume_device;
31sector_t swsusp_resume_block;
32
33enum {
34	HIBERNATION_INVALID,
35	HIBERNATION_PLATFORM,
36	HIBERNATION_TEST,
37	HIBERNATION_TESTPROC,
38	HIBERNATION_SHUTDOWN,
39	HIBERNATION_REBOOT,
40	/* keep last */
41	__HIBERNATION_AFTER_LAST
42};
43#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
44#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
45
46static int hibernation_mode = HIBERNATION_SHUTDOWN;
47
48struct hibernation_ops *hibernation_ops;
49
50/**
51 * hibernation_set_ops - set the global hibernate operations
52 * @ops: the hibernation operations to use in subsequent hibernation transitions
53 */
54
55void hibernation_set_ops(struct hibernation_ops *ops)
56{
57	if (ops && !(ops->prepare && ops->enter && ops->finish)) {
58		WARN_ON(1);
59		return;
60	}
61	mutex_lock(&pm_mutex);
62	hibernation_ops = ops;
63	if (ops)
64		hibernation_mode = HIBERNATION_PLATFORM;
65	else if (hibernation_mode == HIBERNATION_PLATFORM)
66		hibernation_mode = HIBERNATION_SHUTDOWN;
67
68	mutex_unlock(&pm_mutex);
69}
70
71
72/**
73 *	platform_prepare - prepare the machine for hibernation using the
74 *	platform driver if so configured and return an error code if it fails
75 */
76
77static int platform_prepare(void)
78{
79	return (hibernation_mode == HIBERNATION_PLATFORM && hibernation_ops) ?
80		hibernation_ops->prepare() : 0;
81}
82
83/**
84 *	platform_finish - switch the machine to the normal mode of operation
85 *	using the platform driver (must be called after platform_prepare())
86 */
87
88static void platform_finish(void)
89{
90	if (hibernation_mode == HIBERNATION_PLATFORM && hibernation_ops)
91		hibernation_ops->finish();
92}
93
94/**
95 *	power_down - Shut the machine down for hibernation.
96 *
97 *	Use the platform driver, if configured so; otherwise try
98 *	to power off or reboot.
99 */
100
101static void power_down(void)
102{
103	switch (hibernation_mode) {
104	case HIBERNATION_TEST:
105	case HIBERNATION_TESTPROC:
106		break;
107	case HIBERNATION_SHUTDOWN:
108		kernel_power_off();
109		break;
110	case HIBERNATION_REBOOT:
111		kernel_restart(NULL);
112		break;
113	case HIBERNATION_PLATFORM:
114		if (hibernation_ops) {
115			kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
116			hibernation_ops->enter();
117			break;
118		}
119	}
120	kernel_halt();
121	/*
122	 * Valid image is on the disk, if we continue we risk serious data
123	 * corruption after resume.
124	 */
125	printk(KERN_CRIT "Please power me down manually\n");
126	while(1);
127}
128
129static void unprepare_processes(void)
130{
131	thaw_processes();
132	pm_restore_console();
133}
134
135static int prepare_processes(void)
136{
137	int error = 0;
138
139	pm_prepare_console();
140	if (freeze_processes()) {
141		error = -EBUSY;
142		unprepare_processes();
143	}
144	return error;
145}
146
147/**
148 *	hibernate - The granpappy of the built-in hibernation management
149 */
150
151int hibernate(void)
152{
153	int error;
154
155	/* The snapshot device should not be opened while we're running */
156	if (!atomic_add_unless(&snapshot_device_available, -1, 0))
157		return -EBUSY;
158
159	/* Allocate memory management structures */
160	error = create_basic_memory_bitmaps();
161	if (error)
162		goto Exit;
163
164	error = prepare_processes();
165	if (error)
166		goto Finish;
167
168	mutex_lock(&pm_mutex);
169	if (hibernation_mode == HIBERNATION_TESTPROC) {
170		printk("swsusp debug: Waiting for 5 seconds.\n");
171		mdelay(5000);
172		goto Thaw;
173	}
174
175	/* Free memory before shutting down devices. */
176	error = swsusp_shrink_memory();
177	if (error)
178		goto Thaw;
179
180	error = platform_prepare();
181	if (error)
182		goto Thaw;
183
184	suspend_console();
185	error = device_suspend(PMSG_FREEZE);
186	if (error) {
187		printk(KERN_ERR "PM: Some devices failed to suspend\n");
188		goto Resume_devices;
189	}
190	error = disable_nonboot_cpus();
191	if (error)
192		goto Enable_cpus;
193
194	if (hibernation_mode == HIBERNATION_TEST) {
195		printk("swsusp debug: Waiting for 5 seconds.\n");
196		mdelay(5000);
197		goto Enable_cpus;
198	}
199
200	pr_debug("PM: snapshotting memory.\n");
201	in_suspend = 1;
202	error = swsusp_suspend();
203	if (error)
204		goto Enable_cpus;
205
206	if (in_suspend) {
207		enable_nonboot_cpus();
208		platform_finish();
209		device_resume();
210		resume_console();
211		pr_debug("PM: writing image.\n");
212		error = swsusp_write();
213		if (!error)
214			power_down();
215		else {
216			swsusp_free();
217			goto Thaw;
218		}
219	} else {
220		pr_debug("PM: Image restored successfully.\n");
221	}
222
223	swsusp_free();
224 Enable_cpus:
225	enable_nonboot_cpus();
226 Resume_devices:
227	platform_finish();
228	device_resume();
229	resume_console();
230 Thaw:
231	mutex_unlock(&pm_mutex);
232	unprepare_processes();
233 Finish:
234	free_basic_memory_bitmaps();
235 Exit:
236	atomic_inc(&snapshot_device_available);
237	return error;
238}
239
240
241/**
242 *	software_resume - Resume from a saved image.
243 *
244 *	Called as a late_initcall (so all devices are discovered and
245 *	initialized), we call swsusp to see if we have a saved image or not.
246 *	If so, we quiesce devices, the restore the saved image. We will
247 *	return above (in hibernate() ) if everything goes well.
248 *	Otherwise, we fail gracefully and return to the normally
249 *	scheduled program.
250 *
251 */
252
253static int software_resume(void)
254{
255	int error;
256
257	mutex_lock(&pm_mutex);
258	if (!swsusp_resume_device) {
259		if (!strlen(resume_file)) {
260			mutex_unlock(&pm_mutex);
261			return -ENOENT;
262		}
263		swsusp_resume_device = name_to_dev_t(resume_file);
264		pr_debug("swsusp: Resume From Partition %s\n", resume_file);
265	} else {
266		pr_debug("swsusp: Resume From Partition %d:%d\n",
267			 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
268	}
269
270	if (noresume) {
271		mutex_unlock(&pm_mutex);
272		return 0;
273	}
274
275	pr_debug("PM: Checking swsusp image.\n");
276	error = swsusp_check();
277	if (error)
278		goto Unlock;
279
280	/* The snapshot device should not be opened while we're running */
281	if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
282		error = -EBUSY;
283		goto Unlock;
284	}
285
286	error = create_basic_memory_bitmaps();
287	if (error)
288		goto Finish;
289
290	pr_debug("PM: Preparing processes for restore.\n");
291	error = prepare_processes();
292	if (error) {
293		swsusp_close();
294		goto Done;
295	}
296
297	pr_debug("PM: Reading swsusp image.\n");
298
299	error = swsusp_read();
300	if (error) {
301		swsusp_free();
302		goto Thaw;
303	}
304
305	pr_debug("PM: Preparing devices for restore.\n");
306
307	suspend_console();
308	error = device_suspend(PMSG_PRETHAW);
309	if (error)
310		goto Free;
311
312	error = disable_nonboot_cpus();
313	if (!error)
314		swsusp_resume();
315
316	enable_nonboot_cpus();
317 Free:
318	swsusp_free();
319	device_resume();
320	resume_console();
321 Thaw:
322	printk(KERN_ERR "PM: Restore failed, recovering.\n");
323	unprepare_processes();
324 Done:
325	free_basic_memory_bitmaps();
326 Finish:
327	atomic_inc(&snapshot_device_available);
328	/* For success case, the suspend path will release the lock */
329 Unlock:
330	mutex_unlock(&pm_mutex);
331	pr_debug("PM: Resume from disk failed.\n");
332	return 0;
333}
334
335late_initcall(software_resume);
336
337
338static const char * const hibernation_modes[] = {
339	[HIBERNATION_PLATFORM]	= "platform",
340	[HIBERNATION_SHUTDOWN]	= "shutdown",
341	[HIBERNATION_REBOOT]	= "reboot",
342	[HIBERNATION_TEST]	= "test",
343	[HIBERNATION_TESTPROC]	= "testproc",
344};
345
346/**
347 *	disk - Control hibernation mode
348 *
349 *	Suspend-to-disk can be handled in several ways. We have a few options
350 *	for putting the system to sleep - using the platform driver (e.g. ACPI
351 *	or other hibernation_ops), powering off the system or rebooting the
352 *	system (for testing) as well as the two test modes.
353 *
354 *	The system can support 'platform', and that is known a priori (and
355 *	encoded by the presence of hibernation_ops). However, the user may
356 *	choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
357 *	test modes, 'test' or 'testproc'.
358 *
359 *	show() will display what the mode is currently set to.
360 *	store() will accept one of
361 *
362 *	'platform'
363 *	'shutdown'
364 *	'reboot'
365 *	'test'
366 *	'testproc'
367 *
368 *	It will only change to 'platform' if the system
369 *	supports it (as determined by having hibernation_ops).
370 */
371
372static ssize_t disk_show(struct kset *kset, char *buf)
373{
374	int i;
375	char *start = buf;
376
377	for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
378		if (!hibernation_modes[i])
379			continue;
380		switch (i) {
381		case HIBERNATION_SHUTDOWN:
382		case HIBERNATION_REBOOT:
383		case HIBERNATION_TEST:
384		case HIBERNATION_TESTPROC:
385			break;
386		case HIBERNATION_PLATFORM:
387			if (hibernation_ops)
388				break;
389			/* not a valid mode, continue with loop */
390			continue;
391		}
392		if (i == hibernation_mode)
393			buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
394		else
395			buf += sprintf(buf, "%s ", hibernation_modes[i]);
396	}
397	buf += sprintf(buf, "\n");
398	return buf-start;
399}
400
401
402static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
403{
404	int error = 0;
405	int i;
406	int len;
407	char *p;
408	int mode = HIBERNATION_INVALID;
409
410	p = memchr(buf, '\n', n);
411	len = p ? p - buf : n;
412
413	mutex_lock(&pm_mutex);
414	for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
415		if (len == strlen(hibernation_modes[i])
416		    && !strncmp(buf, hibernation_modes[i], len)) {
417			mode = i;
418			break;
419		}
420	}
421	if (mode != HIBERNATION_INVALID) {
422		switch (mode) {
423		case HIBERNATION_SHUTDOWN:
424		case HIBERNATION_REBOOT:
425		case HIBERNATION_TEST:
426		case HIBERNATION_TESTPROC:
427			hibernation_mode = mode;
428			break;
429		case HIBERNATION_PLATFORM:
430			if (hibernation_ops)
431				hibernation_mode = mode;
432			else
433				error = -EINVAL;
434		}
435	} else
436		error = -EINVAL;
437
438	if (!error)
439		pr_debug("PM: suspend-to-disk mode set to '%s'\n",
440			 hibernation_modes[mode]);
441	mutex_unlock(&pm_mutex);
442	return error ? error : n;
443}
444
445power_attr(disk);
446
447static ssize_t resume_show(struct kset *kset, char *buf)
448{
449	return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
450		       MINOR(swsusp_resume_device));
451}
452
453static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
454{
455	unsigned int maj, min;
456	dev_t res;
457	int ret = -EINVAL;
458
459	if (sscanf(buf, "%u:%u", &maj, &min) != 2)
460		goto out;
461
462	res = MKDEV(maj,min);
463	if (maj != MAJOR(res) || min != MINOR(res))
464		goto out;
465
466	mutex_lock(&pm_mutex);
467	swsusp_resume_device = res;
468	mutex_unlock(&pm_mutex);
469	printk("Attempting manual resume\n");
470	noresume = 0;
471	software_resume();
472	ret = n;
473 out:
474	return ret;
475}
476
477power_attr(resume);
478
479static ssize_t image_size_show(struct kset *kset, char *buf)
480{
481	return sprintf(buf, "%lu\n", image_size);
482}
483
484static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
485{
486	unsigned long size;
487
488	if (sscanf(buf, "%lu", &size) == 1) {
489		image_size = size;
490		return n;
491	}
492
493	return -EINVAL;
494}
495
496power_attr(image_size);
497
498static struct attribute * g[] = {
499	&disk_attr.attr,
500	&resume_attr.attr,
501	&image_size_attr.attr,
502	NULL,
503};
504
505
506static struct attribute_group attr_group = {
507	.attrs = g,
508};
509
510
511static int __init pm_disk_init(void)
512{
513	return sysfs_create_group(&power_subsys.kobj, &attr_group);
514}
515
516core_initcall(pm_disk_init);
517
518
519static int __init resume_setup(char *str)
520{
521	if (noresume)
522		return 1;
523
524	strncpy( resume_file, str, 255 );
525	return 1;
526}
527
528static int __init resume_offset_setup(char *str)
529{
530	unsigned long long offset;
531
532	if (noresume)
533		return 1;
534
535	if (sscanf(str, "%llu", &offset) == 1)
536		swsusp_resume_block = offset;
537
538	return 1;
539}
540
541static int __init noresume_setup(char *str)
542{
543	noresume = 1;
544	return 1;
545}
546
547__setup("noresume", noresume_setup);
548__setup("resume_offset=", resume_offset_setup);
549__setup("resume=", resume_setup);
550