blob: ad4e10208cdedacdaec754a968eb8c04a4d41283 [file] [log] [blame]
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -08001/*
2 * linux/kernel/power/user.c
3 *
4 * This file provides the user space interface for software suspend/resume.
5 *
6 * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
7 *
8 * This file is released under the GPLv2.
9 *
10 */
11
12#include <linux/suspend.h>
13#include <linux/syscalls.h>
Stefan Seyfried35926952006-12-06 20:34:06 -080014#include <linux/reboot.h>
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080015#include <linux/string.h>
16#include <linux/device.h>
17#include <linux/miscdevice.h>
18#include <linux/mm.h>
19#include <linux/swap.h>
20#include <linux/swapops.h>
21#include <linux/pm.h>
22#include <linux/fs.h>
Rafael J. Wysocki97c78012006-10-11 01:20:45 -070023#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070024#include <linux/cpu.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080025#include <linux/freezer.h>
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080026
27#include <asm/uaccess.h>
28
29#include "power.h"
30
31#define SNAPSHOT_MINOR 231
32
33static struct snapshot_data {
34 struct snapshot_handle handle;
35 int swap;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080036 int mode;
37 char frozen;
38 char ready;
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -080039 char platform_suspend;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080040} snapshot_state;
41
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070042atomic_t snapshot_device_available = ATOMIC_INIT(1);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080043
44static int snapshot_open(struct inode *inode, struct file *filp)
45{
46 struct snapshot_data *data;
47
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070048 if (!atomic_add_unless(&snapshot_device_available, -1, 0))
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080049 return -EBUSY;
50
Rafael J. Wysocki1525a2a2007-05-06 14:50:44 -070051 if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070052 atomic_inc(&snapshot_device_available);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080053 return -ENOSYS;
Rafael J. Wysocki1525a2a2007-05-06 14:50:44 -070054 }
55 if(create_basic_memory_bitmaps()) {
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070056 atomic_inc(&snapshot_device_available);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -070057 return -ENOMEM;
Rafael J. Wysocki1525a2a2007-05-06 14:50:44 -070058 }
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080059 nonseekable_open(inode, filp);
60 data = &snapshot_state;
61 filp->private_data = data;
62 memset(&data->handle, 0, sizeof(struct snapshot_handle));
63 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
Rafael J. Wysocki915bae92006-12-06 20:34:07 -080064 data->swap = swsusp_resume_device ?
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -080065 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080066 data->mode = O_RDONLY;
67 } else {
68 data->swap = -1;
69 data->mode = O_WRONLY;
70 }
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080071 data->frozen = 0;
72 data->ready = 0;
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -080073 data->platform_suspend = 0;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080074
75 return 0;
76}
77
78static int snapshot_release(struct inode *inode, struct file *filp)
79{
80 struct snapshot_data *data;
81
82 swsusp_free();
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -070083 free_basic_memory_bitmaps();
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080084 data = filp->private_data;
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -070085 free_all_swap_pages(data->swap);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080086 if (data->frozen) {
Stephen Hemmingera6d70982006-12-06 20:34:35 -080087 mutex_lock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080088 thaw_processes();
89 enable_nonboot_cpus();
Stephen Hemmingera6d70982006-12-06 20:34:35 -080090 mutex_unlock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080091 }
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070092 atomic_inc(&snapshot_device_available);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080093 return 0;
94}
95
96static ssize_t snapshot_read(struct file *filp, char __user *buf,
97 size_t count, loff_t *offp)
98{
99 struct snapshot_data *data;
100 ssize_t res;
101
102 data = filp->private_data;
103 res = snapshot_read_next(&data->handle, count);
104 if (res > 0) {
105 if (copy_to_user(buf, data_of(data->handle), res))
106 res = -EFAULT;
107 else
108 *offp = data->handle.offset;
109 }
110 return res;
111}
112
113static ssize_t snapshot_write(struct file *filp, const char __user *buf,
114 size_t count, loff_t *offp)
115{
116 struct snapshot_data *data;
117 ssize_t res;
118
119 data = filp->private_data;
120 res = snapshot_write_next(&data->handle, count);
121 if (res > 0) {
122 if (copy_from_user(data_of(data->handle), buf, res))
123 res = -EFAULT;
124 else
125 *offp = data->handle.offset;
126 }
127 return res;
128}
129
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800130static inline int platform_prepare(void)
131{
132 int error = 0;
133
134 if (pm_ops && pm_ops->prepare)
135 error = pm_ops->prepare(PM_SUSPEND_DISK);
136
137 return error;
138}
139
140static inline void platform_finish(void)
141{
142 if (pm_ops && pm_ops->finish)
143 pm_ops->finish(PM_SUSPEND_DISK);
144}
145
146static inline int snapshot_suspend(int platform_suspend)
Rafael J. Wysocki25913052007-02-10 01:43:33 -0800147{
148 int error;
149
150 mutex_lock(&pm_mutex);
151 /* Free memory before shutting down devices. */
152 error = swsusp_shrink_memory();
153 if (error)
154 goto Finish;
155
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800156 if (platform_suspend) {
157 error = platform_prepare();
158 if (error)
159 goto Finish;
160 }
Rafael J. Wysocki25913052007-02-10 01:43:33 -0800161 suspend_console();
162 error = device_suspend(PMSG_FREEZE);
163 if (error)
164 goto Resume_devices;
165
166 error = disable_nonboot_cpus();
167 if (!error) {
168 in_suspend = 1;
169 error = swsusp_suspend();
170 }
171 enable_nonboot_cpus();
172 Resume_devices:
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800173 if (platform_suspend)
174 platform_finish();
175
Rafael J. Wysocki25913052007-02-10 01:43:33 -0800176 device_resume();
177 resume_console();
178 Finish:
179 mutex_unlock(&pm_mutex);
180 return error;
181}
182
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800183static inline int snapshot_restore(int platform_suspend)
Rafael J. Wysocki25913052007-02-10 01:43:33 -0800184{
185 int error;
186
187 mutex_lock(&pm_mutex);
188 pm_prepare_console();
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800189 if (platform_suspend) {
190 error = platform_prepare();
191 if (error)
192 goto Finish;
193 }
Rafael J. Wysocki25913052007-02-10 01:43:33 -0800194 suspend_console();
195 error = device_suspend(PMSG_PRETHAW);
196 if (error)
197 goto Resume_devices;
198
199 error = disable_nonboot_cpus();
200 if (!error)
201 error = swsusp_resume();
202
203 enable_nonboot_cpus();
204 Resume_devices:
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800205 if (platform_suspend)
206 platform_finish();
207
Rafael J. Wysocki25913052007-02-10 01:43:33 -0800208 device_resume();
209 resume_console();
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800210 Finish:
Rafael J. Wysocki25913052007-02-10 01:43:33 -0800211 pm_restore_console();
212 mutex_unlock(&pm_mutex);
213 return error;
214}
215
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800216static int snapshot_ioctl(struct inode *inode, struct file *filp,
217 unsigned int cmd, unsigned long arg)
218{
219 int error = 0;
220 struct snapshot_data *data;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800221 loff_t avail;
222 sector_t offset;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800223
224 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
225 return -ENOTTY;
226 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
227 return -ENOTTY;
228 if (!capable(CAP_SYS_ADMIN))
229 return -EPERM;
230
231 data = filp->private_data;
232
233 switch (cmd) {
234
235 case SNAPSHOT_FREEZE:
236 if (data->frozen)
237 break;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800238 mutex_lock(&pm_mutex);
Rafael J. Wysocki25913052007-02-10 01:43:33 -0800239 if (freeze_processes()) {
240 thaw_processes();
241 error = -EBUSY;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800242 }
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800243 mutex_unlock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800244 if (!error)
245 data->frozen = 1;
246 break;
247
248 case SNAPSHOT_UNFREEZE:
249 if (!data->frozen)
250 break;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800251 mutex_lock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800252 thaw_processes();
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800253 mutex_unlock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800254 data->frozen = 0;
255 break;
256
257 case SNAPSHOT_ATOMIC_SNAPSHOT:
258 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
259 error = -EPERM;
260 break;
261 }
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800262 error = snapshot_suspend(data->platform_suspend);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800263 if (!error)
264 error = put_user(in_suspend, (unsigned int __user *)arg);
265 if (!error)
266 data->ready = 1;
267 break;
268
269 case SNAPSHOT_ATOMIC_RESTORE:
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800270 snapshot_write_finalize(&data->handle);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800271 if (data->mode != O_WRONLY || !data->frozen ||
272 !snapshot_image_loaded(&data->handle)) {
273 error = -EPERM;
274 break;
275 }
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800276 error = snapshot_restore(data->platform_suspend);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800277 break;
278
279 case SNAPSHOT_FREE:
280 swsusp_free();
281 memset(&data->handle, 0, sizeof(struct snapshot_handle));
282 data->ready = 0;
283 break;
284
285 case SNAPSHOT_SET_IMAGE_SIZE:
286 image_size = arg;
287 break;
288
289 case SNAPSHOT_AVAIL_SWAP:
290 avail = count_swap_pages(data->swap, 1);
291 avail <<= PAGE_SHIFT;
292 error = put_user(avail, (loff_t __user *)arg);
293 break;
294
295 case SNAPSHOT_GET_SWAP_PAGE:
296 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
297 error = -ENODEV;
298 break;
299 }
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700300 offset = alloc_swapdev_block(data->swap);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800301 if (offset) {
302 offset <<= PAGE_SHIFT;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800303 error = put_user(offset, (sector_t __user *)arg);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800304 } else {
305 error = -ENOSPC;
306 }
307 break;
308
309 case SNAPSHOT_FREE_SWAP_PAGES:
310 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
311 error = -ENODEV;
312 break;
313 }
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700314 free_all_swap_pages(data->swap);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800315 break;
316
317 case SNAPSHOT_SET_SWAP_FILE:
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700318 if (!swsusp_swap_in_use()) {
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800319 /*
320 * User space encodes device types as two-byte values,
321 * so we need to recode them
322 */
323 if (old_decode_dev(arg)) {
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800324 data->swap = swap_type_of(old_decode_dev(arg),
325 0, NULL);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800326 if (data->swap < 0)
327 error = -ENODEV;
328 } else {
329 data->swap = -1;
330 error = -EINVAL;
331 }
332 } else {
333 error = -EPERM;
334 }
335 break;
336
Luca Tettamanti9b238202006-03-23 03:00:09 -0800337 case SNAPSHOT_S2RAM:
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800338 if (!pm_ops) {
339 error = -ENOSYS;
340 break;
341 }
342
Luca Tettamanti9b238202006-03-23 03:00:09 -0800343 if (!data->frozen) {
344 error = -EPERM;
345 break;
346 }
347
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800348 if (!mutex_trylock(&pm_mutex)) {
Luca Tettamanti9b238202006-03-23 03:00:09 -0800349 error = -EBUSY;
350 break;
351 }
352
353 if (pm_ops->prepare) {
354 error = pm_ops->prepare(PM_SUSPEND_MEM);
355 if (error)
356 goto OutS3;
357 }
358
359 /* Put devices to sleep */
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700360 suspend_console();
Luca Tettamanti9b238202006-03-23 03:00:09 -0800361 error = device_suspend(PMSG_SUSPEND);
362 if (error) {
363 printk(KERN_ERR "Failed to suspend some devices.\n");
364 } else {
Rafael J. Wysocki93c9a7f2007-03-22 00:11:20 -0800365 error = disable_nonboot_cpus();
366 if (!error) {
367 /* Enter S3, system is already frozen */
368 suspend_enter(PM_SUSPEND_MEM);
369 enable_nonboot_cpus();
370 }
Luca Tettamanti9b238202006-03-23 03:00:09 -0800371 /* Wake up devices */
372 device_resume();
373 }
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700374 resume_console();
Luca Tettamanti9b238202006-03-23 03:00:09 -0800375 if (pm_ops->finish)
376 pm_ops->finish(PM_SUSPEND_MEM);
377
Rafael J. Wysocki59a49332006-12-06 20:34:44 -0800378 OutS3:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800379 mutex_unlock(&pm_mutex);
Luca Tettamanti9b238202006-03-23 03:00:09 -0800380 break;
381
Stefan Seyfried35926952006-12-06 20:34:06 -0800382 case SNAPSHOT_PMOPS:
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800383 error = -EINVAL;
384
Stefan Seyfried35926952006-12-06 20:34:06 -0800385 switch (arg) {
386
387 case PMOPS_PREPARE:
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800388 if (pm_ops && pm_ops->enter) {
389 data->platform_suspend = 1;
390 error = 0;
391 } else {
392 error = -ENOSYS;
Stefan Seyfried35926952006-12-06 20:34:06 -0800393 }
394 break;
395
396 case PMOPS_ENTER:
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800397 if (data->platform_suspend) {
398 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
399 error = pm_ops->enter(PM_SUSPEND_DISK);
Rafael J. Wysocki436ce712007-03-27 12:09:13 +0200400 error = 0;
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800401 }
Stefan Seyfried35926952006-12-06 20:34:06 -0800402 break;
403
404 case PMOPS_FINISH:
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800405 if (data->platform_suspend)
406 error = 0;
407
Stefan Seyfried35926952006-12-06 20:34:06 -0800408 break;
409
410 default:
411 printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
Stefan Seyfried35926952006-12-06 20:34:06 -0800412
413 }
414 break;
415
Rafael J. Wysocki37b2ba12006-12-06 20:34:15 -0800416 case SNAPSHOT_SET_SWAP_AREA:
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700417 if (swsusp_swap_in_use()) {
Rafael J. Wysocki37b2ba12006-12-06 20:34:15 -0800418 error = -EPERM;
419 } else {
420 struct resume_swap_area swap_area;
421 dev_t swdev;
422
423 error = copy_from_user(&swap_area, (void __user *)arg,
424 sizeof(struct resume_swap_area));
425 if (error) {
426 error = -EFAULT;
427 break;
428 }
429
430 /*
431 * User space encodes device types as two-byte values,
432 * so we need to recode them
433 */
434 swdev = old_decode_dev(swap_area.dev);
435 if (swdev) {
436 offset = swap_area.offset;
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800437 data->swap = swap_type_of(swdev, offset, NULL);
Rafael J. Wysocki37b2ba12006-12-06 20:34:15 -0800438 if (data->swap < 0)
439 error = -ENODEV;
440 } else {
441 data->swap = -1;
442 error = -EINVAL;
443 }
444 }
445 break;
446
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800447 default:
448 error = -ENOTTY;
449
450 }
451
452 return error;
453}
454
Helge Deller15ad7cd2006-12-06 20:40:36 -0800455static const struct file_operations snapshot_fops = {
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800456 .open = snapshot_open,
457 .release = snapshot_release,
458 .read = snapshot_read,
459 .write = snapshot_write,
460 .llseek = no_llseek,
461 .ioctl = snapshot_ioctl,
462};
463
464static struct miscdevice snapshot_device = {
465 .minor = SNAPSHOT_MINOR,
466 .name = "snapshot",
467 .fops = &snapshot_fops,
468};
469
470static int __init snapshot_device_init(void)
471{
472 return misc_register(&snapshot_device);
473};
474
475device_initcall(snapshot_device_init);