Merge branch 'drm-patches' of git://git.kernel.org/pub/scm/linux/kernel/git/airlied...
[deliverable/linux.git] / kernel / power / swsusp.c
1 /*
2 * linux/kernel/power/swsusp.c
3 *
4 * This file provides code to write suspend image to swap and read it back.
5 *
6 * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
7 * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
8 *
9 * This file is released under the GPLv2.
10 *
11 * I'd like to thank the following people for their work:
12 *
13 * Pavel Machek <pavel@ucw.cz>:
14 * Modifications, defectiveness pointing, being with me at the very beginning,
15 * suspend to swap space, stop all tasks. Port to 2.4.18-ac and 2.5.17.
16 *
17 * Steve Doddi <dirk@loth.demon.co.uk>:
18 * Support the possibility of hardware state restoring.
19 *
20 * Raph <grey.havens@earthling.net>:
21 * Support for preserving states of network devices and virtual console
22 * (including X and svgatextmode)
23 *
24 * Kurt Garloff <garloff@suse.de>:
25 * Straightened the critical function in order to prevent compilers from
26 * playing tricks with local variables.
27 *
28 * Andreas Mohr <a.mohr@mailto.de>
29 *
30 * Alex Badea <vampire@go.ro>:
31 * Fixed runaway init
32 *
33 * Rafael J. Wysocki <rjw@sisk.pl>
34 * Reworked the freeing of memory and the handling of swap
35 *
36 * More state savers are welcome. Especially for the scsi layer...
37 *
38 * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt
39 */
40
41 #include <linux/mm.h>
42 #include <linux/suspend.h>
43 #include <linux/spinlock.h>
44 #include <linux/kernel.h>
45 #include <linux/major.h>
46 #include <linux/swap.h>
47 #include <linux/pm.h>
48 #include <linux/swapops.h>
49 #include <linux/bootmem.h>
50 #include <linux/syscalls.h>
51 #include <linux/highmem.h>
52
53 #include "power.h"
54
55 /*
56 * Preferred image size in bytes (tunable via /sys/power/image_size).
57 * When it is set to N, swsusp will do its best to ensure the image
58 * size will not exceed N bytes, but if that is impossible, it will
59 * try to create the smallest image possible.
60 */
61 unsigned long image_size = 500 * 1024 * 1024;
62
63 int in_suspend __nosavedata = 0;
64
65 #ifdef CONFIG_HIGHMEM
66 unsigned int count_highmem_pages(void);
67 int save_highmem(void);
68 int restore_highmem(void);
69 #else
70 static int save_highmem(void) { return 0; }
71 static int restore_highmem(void) { return 0; }
72 static unsigned int count_highmem_pages(void) { return 0; }
73 #endif
74
75 /**
76 * The following functions are used for tracing the allocated
77 * swap pages, so that they can be freed in case of an error.
78 *
79 * The functions operate on a linked bitmap structure defined
80 * in power.h
81 */
82
83 void free_bitmap(struct bitmap_page *bitmap)
84 {
85 struct bitmap_page *bp;
86
87 while (bitmap) {
88 bp = bitmap->next;
89 free_page((unsigned long)bitmap);
90 bitmap = bp;
91 }
92 }
93
94 struct bitmap_page *alloc_bitmap(unsigned int nr_bits)
95 {
96 struct bitmap_page *bitmap, *bp;
97 unsigned int n;
98
99 if (!nr_bits)
100 return NULL;
101
102 bitmap = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL);
103 bp = bitmap;
104 for (n = BITMAP_PAGE_BITS; n < nr_bits; n += BITMAP_PAGE_BITS) {
105 bp->next = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL);
106 bp = bp->next;
107 if (!bp) {
108 free_bitmap(bitmap);
109 return NULL;
110 }
111 }
112 return bitmap;
113 }
114
115 static int bitmap_set(struct bitmap_page *bitmap, unsigned long bit)
116 {
117 unsigned int n;
118
119 n = BITMAP_PAGE_BITS;
120 while (bitmap && n <= bit) {
121 n += BITMAP_PAGE_BITS;
122 bitmap = bitmap->next;
123 }
124 if (!bitmap)
125 return -EINVAL;
126 n -= BITMAP_PAGE_BITS;
127 bit -= n;
128 n = 0;
129 while (bit >= BITS_PER_CHUNK) {
130 bit -= BITS_PER_CHUNK;
131 n++;
132 }
133 bitmap->chunks[n] |= (1UL << bit);
134 return 0;
135 }
136
137 unsigned long alloc_swap_page(int swap, struct bitmap_page *bitmap)
138 {
139 unsigned long offset;
140
141 offset = swp_offset(get_swap_page_of_type(swap));
142 if (offset) {
143 if (bitmap_set(bitmap, offset)) {
144 swap_free(swp_entry(swap, offset));
145 offset = 0;
146 }
147 }
148 return offset;
149 }
150
151 void free_all_swap_pages(int swap, struct bitmap_page *bitmap)
152 {
153 unsigned int bit, n;
154 unsigned long test;
155
156 bit = 0;
157 while (bitmap) {
158 for (n = 0; n < BITMAP_PAGE_CHUNKS; n++)
159 for (test = 1UL; test; test <<= 1) {
160 if (bitmap->chunks[n] & test)
161 swap_free(swp_entry(swap, bit));
162 bit++;
163 }
164 bitmap = bitmap->next;
165 }
166 }
167
168 /**
169 * swsusp_shrink_memory - Try to free as much memory as needed
170 *
171 * ... but do not OOM-kill anyone
172 *
173 * Notice: all userland should be stopped before it is called, or
174 * livelock is possible.
175 */
176
177 #define SHRINK_BITE 10000
178
179 int swsusp_shrink_memory(void)
180 {
181 long size, tmp;
182 struct zone *zone;
183 unsigned long pages = 0;
184 unsigned int i = 0;
185 char *p = "-\\|/";
186
187 printk("Shrinking memory... ");
188 do {
189 size = 2 * count_highmem_pages();
190 size += size / 50 + count_data_pages();
191 size += (size + PBES_PER_PAGE - 1) / PBES_PER_PAGE +
192 PAGES_FOR_IO;
193 tmp = size;
194 for_each_zone (zone)
195 if (!is_highmem(zone))
196 tmp -= zone->free_pages;
197 if (tmp > 0) {
198 tmp = shrink_all_memory(SHRINK_BITE);
199 if (!tmp)
200 return -ENOMEM;
201 pages += tmp;
202 } else if (size > image_size / PAGE_SIZE) {
203 tmp = shrink_all_memory(SHRINK_BITE);
204 pages += tmp;
205 }
206 printk("\b%c", p[i++%4]);
207 } while (tmp > 0);
208 printk("\bdone (%lu pages freed)\n", pages);
209
210 return 0;
211 }
212
213 int swsusp_suspend(void)
214 {
215 int error;
216
217 if ((error = arch_prepare_suspend()))
218 return error;
219 local_irq_disable();
220 /* At this point, device_suspend() has been called, but *not*
221 * device_power_down(). We *must* device_power_down() now.
222 * Otherwise, drivers for some devices (e.g. interrupt controllers)
223 * become desynchronized with the actual state of the hardware
224 * at resume time, and evil weirdness ensues.
225 */
226 if ((error = device_power_down(PMSG_FREEZE))) {
227 printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
228 goto Enable_irqs;
229 }
230
231 if ((error = save_highmem())) {
232 printk(KERN_ERR "swsusp: Not enough free pages for highmem\n");
233 goto Restore_highmem;
234 }
235
236 save_processor_state();
237 if ((error = swsusp_arch_suspend()))
238 printk(KERN_ERR "Error %d suspending\n", error);
239 /* Restore control flow magically appears here */
240 restore_processor_state();
241 Restore_highmem:
242 restore_highmem();
243 device_power_up();
244 Enable_irqs:
245 local_irq_enable();
246 return error;
247 }
248
249 int swsusp_resume(void)
250 {
251 int error;
252 local_irq_disable();
253 if (device_power_down(PMSG_FREEZE))
254 printk(KERN_ERR "Some devices failed to power down, very bad\n");
255 /* We'll ignore saved state, but this gets preempt count (etc) right */
256 save_processor_state();
257 error = swsusp_arch_resume();
258 /* Code below is only ever reached in case of failure. Otherwise
259 * execution continues at place where swsusp_arch_suspend was called
260 */
261 BUG_ON(!error);
262 /* The only reason why swsusp_arch_resume() can fail is memory being
263 * very tight, so we have to free it as soon as we can to avoid
264 * subsequent failures
265 */
266 swsusp_free();
267 restore_processor_state();
268 restore_highmem();
269 touch_softlockup_watchdog();
270 device_power_up();
271 local_irq_enable();
272 return error;
273 }
This page took 0.037224 seconds and 6 git commands to generate.