]> nv-tegra.nvidia Code Review - linux-3.10.git/blob - kernel/power/user.c
09468ec61124ff8184d23684e45343a8e81d216a
[linux-3.10.git] / kernel / power / user.c
1 /*
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>
14 #include <linux/reboot.h>
15 #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>
23 #include <linux/console.h>
24 #include <linux/cpu.h>
25 #include <linux/freezer.h>
26
27 #include <asm/uaccess.h>
28
29 #include "power.h"
30
31 #define SNAPSHOT_MINOR  231
32
33 static struct snapshot_data {
34         struct snapshot_handle handle;
35         int swap;
36         int mode;
37         char frozen;
38         char ready;
39         char platform_suspend;
40 } snapshot_state;
41
42 atomic_t snapshot_device_available = ATOMIC_INIT(1);
43
44 static int snapshot_open(struct inode *inode, struct file *filp)
45 {
46         struct snapshot_data *data;
47
48         if (!atomic_add_unless(&snapshot_device_available, -1, 0))
49                 return -EBUSY;
50
51         if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
52                 atomic_inc(&snapshot_device_available);
53                 return -ENOSYS;
54         }
55         if(create_basic_memory_bitmaps()) {
56                 atomic_inc(&snapshot_device_available);
57                 return -ENOMEM;
58         }
59         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) {
64                 data->swap = swsusp_resume_device ?
65                         swap_type_of(swsusp_resume_device, 0, NULL) : -1;
66                 data->mode = O_RDONLY;
67         } else {
68                 data->swap = -1;
69                 data->mode = O_WRONLY;
70         }
71         data->frozen = 0;
72         data->ready = 0;
73         data->platform_suspend = 0;
74
75         return 0;
76 }
77
78 static int snapshot_release(struct inode *inode, struct file *filp)
79 {
80         struct snapshot_data *data;
81
82         swsusp_free();
83         free_basic_memory_bitmaps();
84         data = filp->private_data;
85         free_all_swap_pages(data->swap);
86         if (data->frozen) {
87                 mutex_lock(&pm_mutex);
88                 thaw_processes();
89                 mutex_unlock(&pm_mutex);
90         }
91         atomic_inc(&snapshot_device_available);
92         return 0;
93 }
94
95 static 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;
102         if (!data->ready)
103                 return -ENODATA;
104         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
114 static 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
131 static inline int platform_prepare(void)
132 {
133         int error = 0;
134
135         if (hibernation_ops)
136                 error = hibernation_ops->prepare();
137
138         return error;
139 }
140
141 static inline void platform_finish(void)
142 {
143         if (hibernation_ops)
144                 hibernation_ops->finish();
145 }
146
147 static inline int snapshot_suspend(int platform_suspend)
148 {
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
157         if (platform_suspend) {
158                 error = platform_prepare();
159                 if (error)
160                         goto Finish;
161         }
162         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:
174         if (platform_suspend)
175                 platform_finish();
176
177         device_resume();
178         resume_console();
179  Finish:
180         mutex_unlock(&pm_mutex);
181         return error;
182 }
183
184 static inline int snapshot_restore(void)
185 {
186         int error;
187
188         mutex_lock(&pm_mutex);
189         pm_prepare_console();
190         suspend_console();
191         error = device_suspend(PMSG_PRETHAW);
192         if (error)
193                 goto Finish;
194
195         error = disable_nonboot_cpus();
196         if (!error)
197                 error = swsusp_resume();
198
199         enable_nonboot_cpus();
200  Finish:
201         device_resume();
202         resume_console();
203         pm_restore_console();
204         mutex_unlock(&pm_mutex);
205         return error;
206 }
207
208 static int snapshot_ioctl(struct inode *inode, struct file *filp,
209                           unsigned int cmd, unsigned long arg)
210 {
211         int error = 0;
212         struct snapshot_data *data;
213         loff_t avail;
214         sector_t offset;
215
216         if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
217                 return -ENOTTY;
218         if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
219                 return -ENOTTY;
220         if (!capable(CAP_SYS_ADMIN))
221                 return -EPERM;
222
223         data = filp->private_data;
224
225         switch (cmd) {
226
227         case SNAPSHOT_FREEZE:
228                 if (data->frozen)
229                         break;
230                 mutex_lock(&pm_mutex);
231                 if (freeze_processes()) {
232                         thaw_processes();
233                         error = -EBUSY;
234                 }
235                 mutex_unlock(&pm_mutex);
236                 if (!error)
237                         data->frozen = 1;
238                 break;
239
240         case SNAPSHOT_UNFREEZE:
241                 if (!data->frozen || data->ready)
242                         break;
243                 mutex_lock(&pm_mutex);
244                 thaw_processes();
245                 mutex_unlock(&pm_mutex);
246                 data->frozen = 0;
247                 break;
248
249         case SNAPSHOT_ATOMIC_SNAPSHOT:
250                 if (data->mode != O_RDONLY || !data->frozen  || data->ready) {
251                         error = -EPERM;
252                         break;
253                 }
254                 error = snapshot_suspend(data->platform_suspend);
255                 if (!error)
256                         error = put_user(in_suspend, (unsigned int __user *)arg);
257                 if (!error)
258                         data->ready = 1;
259                 break;
260
261         case SNAPSHOT_ATOMIC_RESTORE:
262                 snapshot_write_finalize(&data->handle);
263                 if (data->mode != O_WRONLY || !data->frozen ||
264                     !snapshot_image_loaded(&data->handle)) {
265                         error = -EPERM;
266                         break;
267                 }
268                 error = snapshot_restore();
269                 break;
270
271         case SNAPSHOT_FREE:
272                 swsusp_free();
273                 memset(&data->handle, 0, sizeof(struct snapshot_handle));
274                 data->ready = 0;
275                 break;
276
277         case SNAPSHOT_SET_IMAGE_SIZE:
278                 image_size = arg;
279                 break;
280
281         case SNAPSHOT_AVAIL_SWAP:
282                 avail = count_swap_pages(data->swap, 1);
283                 avail <<= PAGE_SHIFT;
284                 error = put_user(avail, (loff_t __user *)arg);
285                 break;
286
287         case SNAPSHOT_GET_SWAP_PAGE:
288                 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
289                         error = -ENODEV;
290                         break;
291                 }
292                 offset = alloc_swapdev_block(data->swap);
293                 if (offset) {
294                         offset <<= PAGE_SHIFT;
295                         error = put_user(offset, (sector_t __user *)arg);
296                 } else {
297                         error = -ENOSPC;
298                 }
299                 break;
300
301         case SNAPSHOT_FREE_SWAP_PAGES:
302                 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
303                         error = -ENODEV;
304                         break;
305                 }
306                 free_all_swap_pages(data->swap);
307                 break;
308
309         case SNAPSHOT_SET_SWAP_FILE:
310                 if (!swsusp_swap_in_use()) {
311                         /*
312                          * User space encodes device types as two-byte values,
313                          * so we need to recode them
314                          */
315                         if (old_decode_dev(arg)) {
316                                 data->swap = swap_type_of(old_decode_dev(arg),
317                                                         0, NULL);
318                                 if (data->swap < 0)
319                                         error = -ENODEV;
320                         } else {
321                                 data->swap = -1;
322                                 error = -EINVAL;
323                         }
324                 } else {
325                         error = -EPERM;
326                 }
327                 break;
328
329         case SNAPSHOT_S2RAM:
330                 if (!pm_ops) {
331                         error = -ENOSYS;
332                         break;
333                 }
334
335                 if (!data->frozen) {
336                         error = -EPERM;
337                         break;
338                 }
339
340                 if (!mutex_trylock(&pm_mutex)) {
341                         error = -EBUSY;
342                         break;
343                 }
344
345                 if (pm_ops->prepare) {
346                         error = pm_ops->prepare(PM_SUSPEND_MEM);
347                         if (error)
348                                 goto OutS3;
349                 }
350
351                 /* Put devices to sleep */
352                 suspend_console();
353                 error = device_suspend(PMSG_SUSPEND);
354                 if (error) {
355                         printk(KERN_ERR "Failed to suspend some devices.\n");
356                 } else {
357                         error = disable_nonboot_cpus();
358                         if (!error) {
359                                 /* Enter S3, system is already frozen */
360                                 suspend_enter(PM_SUSPEND_MEM);
361                                 enable_nonboot_cpus();
362                         }
363                         /* Wake up devices */
364                         device_resume();
365                 }
366                 resume_console();
367                 if (pm_ops->finish)
368                         pm_ops->finish(PM_SUSPEND_MEM);
369
370  OutS3:
371                 mutex_unlock(&pm_mutex);
372                 break;
373
374         case SNAPSHOT_PMOPS:
375                 error = -EINVAL;
376
377                 switch (arg) {
378
379                 case PMOPS_PREPARE:
380                         if (hibernation_ops) {
381                                 data->platform_suspend = 1;
382                                 error = 0;
383                         } else {
384                                 error = -ENOSYS;
385                         }
386                         break;
387
388                 case PMOPS_ENTER:
389                         if (data->platform_suspend) {
390                                 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
391                                 error = hibernation_ops->enter();
392                         }
393                         break;
394
395                 case PMOPS_FINISH:
396                         if (data->platform_suspend)
397                                 error = 0;
398
399                         break;
400
401                 default:
402                         printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
403
404                 }
405                 break;
406
407         case SNAPSHOT_SET_SWAP_AREA:
408                 if (swsusp_swap_in_use()) {
409                         error = -EPERM;
410                 } else {
411                         struct resume_swap_area swap_area;
412                         dev_t swdev;
413
414                         error = copy_from_user(&swap_area, (void __user *)arg,
415                                         sizeof(struct resume_swap_area));
416                         if (error) {
417                                 error = -EFAULT;
418                                 break;
419                         }
420
421                         /*
422                          * User space encodes device types as two-byte values,
423                          * so we need to recode them
424                          */
425                         swdev = old_decode_dev(swap_area.dev);
426                         if (swdev) {
427                                 offset = swap_area.offset;
428                                 data->swap = swap_type_of(swdev, offset, NULL);
429                                 if (data->swap < 0)
430                                         error = -ENODEV;
431                         } else {
432                                 data->swap = -1;
433                                 error = -EINVAL;
434                         }
435                 }
436                 break;
437
438         default:
439                 error = -ENOTTY;
440
441         }
442
443         return error;
444 }
445
446 static const struct file_operations snapshot_fops = {
447         .open = snapshot_open,
448         .release = snapshot_release,
449         .read = snapshot_read,
450         .write = snapshot_write,
451         .llseek = no_llseek,
452         .ioctl = snapshot_ioctl,
453 };
454
455 static struct miscdevice snapshot_device = {
456         .minor = SNAPSHOT_MINOR,
457         .name = "snapshot",
458         .fops = &snapshot_fops,
459 };
460
461 static int __init snapshot_device_init(void)
462 {
463         return misc_register(&snapshot_device);
464 };
465
466 device_initcall(snapshot_device_init);