blob: d65305b515b1ec8c359532b3f29812178fbbb6ad [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();
Stephen Hemmingera6d70982006-12-06 20:34:35 -080089 mutex_unlock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080090 }
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070091 atomic_inc(&snapshot_device_available);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080092 return 0;
93}
94
95static ssize_t snapshot_read(struct file *filp, char __user *buf,
96 size_t count, loff_t *offp)
97{
98 struct snapshot_data *data;
99 ssize_t res;
100
101 data = filp->private_data;
Rafael J. Wysocki2f41ddd2007-06-16 10:16:03 -0700102 if (!data->ready)
103 return -ENODATA;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800104 res = snapshot_read_next(&data->handle, count);
105 if (res > 0) {
106 if (copy_to_user(buf, data_of(data->handle), res))
107 res = -EFAULT;
108 else
109 *offp = data->handle.offset;
110 }
111 return res;
112}
113
114static ssize_t snapshot_write(struct file *filp, const char __user *buf,
115 size_t count, loff_t *offp)
116{
117 struct snapshot_data *data;
118 ssize_t res;
119
120 data = filp->private_data;
121 res = snapshot_write_next(&data->handle, count);
122 if (res > 0) {
123 if (copy_from_user(data_of(data->handle), buf, res))
124 res = -EFAULT;
125 else
126 *offp = data->handle.offset;
127 }
128 return res;
129}
130
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800131static inline int platform_prepare(void)
132{
133 int error = 0;
134
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700135 if (hibernation_ops)
136 error = hibernation_ops->prepare();
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800137
138 return error;
139}
140
141static inline void platform_finish(void)
142{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700143 if (hibernation_ops)
144 hibernation_ops->finish();
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800145}
146
147static inline int snapshot_suspend(int platform_suspend)
Rafael J. Wysocki25913052007-02-10 01:43:33 -0800148{
149 int error;
150
151 mutex_lock(&pm_mutex);
152 /* Free memory before shutting down devices. */
153 error = swsusp_shrink_memory();
154 if (error)
155 goto Finish;
156
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800157 if (platform_suspend) {
158 error = platform_prepare();
159 if (error)
160 goto Finish;
161 }
Rafael J. Wysocki25913052007-02-10 01:43:33 -0800162 suspend_console();
163 error = device_suspend(PMSG_FREEZE);
164 if (error)
165 goto Resume_devices;
166
167 error = disable_nonboot_cpus();
168 if (!error) {
169 in_suspend = 1;
170 error = swsusp_suspend();
171 }
172 enable_nonboot_cpus();
173 Resume_devices:
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800174 if (platform_suspend)
175 platform_finish();
176
Rafael J. Wysocki25913052007-02-10 01:43:33 -0800177 device_resume();
178 resume_console();
179 Finish:
180 mutex_unlock(&pm_mutex);
181 return error;
182}
183
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800184static inline int snapshot_restore(int platform_suspend)
Rafael J. Wysocki25913052007-02-10 01:43:33 -0800185{
186 int error;
187
188 mutex_lock(&pm_mutex);
189 pm_prepare_console();
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800190 if (platform_suspend) {
191 error = platform_prepare();
192 if (error)
193 goto Finish;
194 }
Rafael J. Wysocki25913052007-02-10 01:43:33 -0800195 suspend_console();
196 error = device_suspend(PMSG_PRETHAW);
197 if (error)
198 goto Resume_devices;
199
200 error = disable_nonboot_cpus();
201 if (!error)
202 error = swsusp_resume();
203
204 enable_nonboot_cpus();
205 Resume_devices:
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800206 if (platform_suspend)
207 platform_finish();
208
Rafael J. Wysocki25913052007-02-10 01:43:33 -0800209 device_resume();
210 resume_console();
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800211 Finish:
Rafael J. Wysocki25913052007-02-10 01:43:33 -0800212 pm_restore_console();
213 mutex_unlock(&pm_mutex);
214 return error;
215}
216
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800217static int snapshot_ioctl(struct inode *inode, struct file *filp,
218 unsigned int cmd, unsigned long arg)
219{
220 int error = 0;
221 struct snapshot_data *data;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800222 loff_t avail;
223 sector_t offset;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800224
225 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
226 return -ENOTTY;
227 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
228 return -ENOTTY;
229 if (!capable(CAP_SYS_ADMIN))
230 return -EPERM;
231
232 data = filp->private_data;
233
234 switch (cmd) {
235
236 case SNAPSHOT_FREEZE:
237 if (data->frozen)
238 break;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800239 mutex_lock(&pm_mutex);
Rafael J. Wysocki25913052007-02-10 01:43:33 -0800240 if (freeze_processes()) {
241 thaw_processes();
242 error = -EBUSY;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800243 }
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800244 mutex_unlock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800245 if (!error)
246 data->frozen = 1;
247 break;
248
249 case SNAPSHOT_UNFREEZE:
Rafael J. Wysocki2f41ddd2007-06-16 10:16:03 -0700250 if (!data->frozen || data->ready)
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800251 break;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800252 mutex_lock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800253 thaw_processes();
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800254 mutex_unlock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800255 data->frozen = 0;
256 break;
257
258 case SNAPSHOT_ATOMIC_SNAPSHOT:
259 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
260 error = -EPERM;
261 break;
262 }
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800263 error = snapshot_suspend(data->platform_suspend);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800264 if (!error)
265 error = put_user(in_suspend, (unsigned int __user *)arg);
266 if (!error)
267 data->ready = 1;
268 break;
269
270 case SNAPSHOT_ATOMIC_RESTORE:
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800271 snapshot_write_finalize(&data->handle);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800272 if (data->mode != O_WRONLY || !data->frozen ||
273 !snapshot_image_loaded(&data->handle)) {
274 error = -EPERM;
275 break;
276 }
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800277 error = snapshot_restore(data->platform_suspend);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800278 break;
279
280 case SNAPSHOT_FREE:
281 swsusp_free();
282 memset(&data->handle, 0, sizeof(struct snapshot_handle));
283 data->ready = 0;
284 break;
285
286 case SNAPSHOT_SET_IMAGE_SIZE:
287 image_size = arg;
288 break;
289
290 case SNAPSHOT_AVAIL_SWAP:
291 avail = count_swap_pages(data->swap, 1);
292 avail <<= PAGE_SHIFT;
293 error = put_user(avail, (loff_t __user *)arg);
294 break;
295
296 case SNAPSHOT_GET_SWAP_PAGE:
297 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
298 error = -ENODEV;
299 break;
300 }
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700301 offset = alloc_swapdev_block(data->swap);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800302 if (offset) {
303 offset <<= PAGE_SHIFT;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800304 error = put_user(offset, (sector_t __user *)arg);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800305 } else {
306 error = -ENOSPC;
307 }
308 break;
309
310 case SNAPSHOT_FREE_SWAP_PAGES:
311 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
312 error = -ENODEV;
313 break;
314 }
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700315 free_all_swap_pages(data->swap);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800316 break;
317
318 case SNAPSHOT_SET_SWAP_FILE:
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700319 if (!swsusp_swap_in_use()) {
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800320 /*
321 * User space encodes device types as two-byte values,
322 * so we need to recode them
323 */
324 if (old_decode_dev(arg)) {
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800325 data->swap = swap_type_of(old_decode_dev(arg),
326 0, NULL);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800327 if (data->swap < 0)
328 error = -ENODEV;
329 } else {
330 data->swap = -1;
331 error = -EINVAL;
332 }
333 } else {
334 error = -EPERM;
335 }
336 break;
337
Luca Tettamanti9b238202006-03-23 03:00:09 -0800338 case SNAPSHOT_S2RAM:
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800339 if (!pm_ops) {
340 error = -ENOSYS;
341 break;
342 }
343
Luca Tettamanti9b238202006-03-23 03:00:09 -0800344 if (!data->frozen) {
345 error = -EPERM;
346 break;
347 }
348
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800349 if (!mutex_trylock(&pm_mutex)) {
Luca Tettamanti9b238202006-03-23 03:00:09 -0800350 error = -EBUSY;
351 break;
352 }
353
354 if (pm_ops->prepare) {
355 error = pm_ops->prepare(PM_SUSPEND_MEM);
356 if (error)
357 goto OutS3;
358 }
359
360 /* Put devices to sleep */
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700361 suspend_console();
Luca Tettamanti9b238202006-03-23 03:00:09 -0800362 error = device_suspend(PMSG_SUSPEND);
363 if (error) {
364 printk(KERN_ERR "Failed to suspend some devices.\n");
365 } else {
Rafael J. Wysocki93c9a7f2007-03-22 00:11:20 -0800366 error = disable_nonboot_cpus();
367 if (!error) {
368 /* Enter S3, system is already frozen */
369 suspend_enter(PM_SUSPEND_MEM);
370 enable_nonboot_cpus();
371 }
Luca Tettamanti9b238202006-03-23 03:00:09 -0800372 /* Wake up devices */
373 device_resume();
374 }
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700375 resume_console();
Luca Tettamanti9b238202006-03-23 03:00:09 -0800376 if (pm_ops->finish)
377 pm_ops->finish(PM_SUSPEND_MEM);
378
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800379 OutS3:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800380 mutex_unlock(&pm_mutex);
Luca Tettamanti9b238202006-03-23 03:00:09 -0800381 break;
382
Stefan Seyfried35926952006-12-06 20:34:06 -0800383 case SNAPSHOT_PMOPS:
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800384 error = -EINVAL;
385
Stefan Seyfried35926952006-12-06 20:34:06 -0800386 switch (arg) {
387
388 case PMOPS_PREPARE:
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700389 if (hibernation_ops) {
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800390 data->platform_suspend = 1;
391 error = 0;
392 } else {
393 error = -ENOSYS;
Stefan Seyfried35926952006-12-06 20:34:06 -0800394 }
395 break;
396
397 case PMOPS_ENTER:
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800398 if (data->platform_suspend) {
399 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700400 error = hibernation_ops->enter();
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);