blob: 31aa0390c777d186652fe1b5dc3bfb4aafbaa338 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/power/swsusp.c
3 *
Pavel Machek96bc7ae2005-10-30 14:59:58 -08004 * This file provides code to write suspend image to swap and read it back.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08007 * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This file is released under the GPLv2.
10 *
11 * I'd like to thank the following people for their work:
Pavel Machek2e4d5822005-06-25 14:55:12 -070012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * 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 *
Pavel Machek2e4d5822005-06-25 14:55:12 -070017 * Steve Doddi <dirk@loth.demon.co.uk>:
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * 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 *
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -080033 * Rafael J. Wysocki <rjw@sisk.pl>
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080034 * Reworked the freeing of memory and the handling of swap
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -080035 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * More state savers are welcome. Especially for the scsi layer...
37 *
38 * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt
39 */
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/mm.h>
42#include <linux/suspend.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/kernel.h>
45#include <linux/major.h>
46#include <linux/swap.h>
47#include <linux/pm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/swapops.h>
49#include <linux/bootmem.h>
50#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/highmem.h>
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -080052#include <linux/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#include "power.h"
55
Rafael J. Wysockica0aec02006-01-06 00:15:56 -080056/*
Rafael J. Wysocki853609b2006-02-01 03:05:07 -080057 * Preferred image size in bytes (tunable via /sys/power/image_size).
Rafael J. Wysockica0aec02006-01-06 00:15:56 -080058 * When it is set to N, swsusp will do its best to ensure the image
Rafael J. Wysocki853609b2006-02-01 03:05:07 -080059 * size will not exceed N bytes, but if that is impossible, it will
Rafael J. Wysockica0aec02006-01-06 00:15:56 -080060 * try to create the smallest image possible.
61 */
Rafael J. Wysocki853609b2006-02-01 03:05:07 -080062unsigned long image_size = 500 * 1024 * 1024;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -080063
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080064int in_suspend __nosavedata = 0;
65
Linus Torvalds34480972006-06-25 18:41:00 -070066#ifdef CONFIG_HIGHMEM
67unsigned int count_highmem_pages(void);
Linus Torvalds34480972006-06-25 18:41:00 -070068int restore_highmem(void);
69#else
Linus Torvalds34480972006-06-25 18:41:00 -070070static inline int restore_highmem(void) { return 0; }
71static inline unsigned int count_highmem_pages(void) { return 0; }
72#endif
73
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080074/**
75 * The following functions are used for tracing the allocated
76 * swap pages, so that they can be freed in case of an error.
77 *
78 * The functions operate on a linked bitmap structure defined
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080079 * in power.h
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080080 */
81
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080082void free_bitmap(struct bitmap_page *bitmap)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080083{
84 struct bitmap_page *bp;
85
86 while (bitmap) {
87 bp = bitmap->next;
88 free_page((unsigned long)bitmap);
89 bitmap = bp;
90 }
91}
92
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080093struct bitmap_page *alloc_bitmap(unsigned int nr_bits)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080094{
95 struct bitmap_page *bitmap, *bp;
96 unsigned int n;
97
98 if (!nr_bits)
99 return NULL;
100
101 bitmap = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL);
102 bp = bitmap;
103 for (n = BITMAP_PAGE_BITS; n < nr_bits; n += BITMAP_PAGE_BITS) {
104 bp->next = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL);
105 bp = bp->next;
106 if (!bp) {
107 free_bitmap(bitmap);
108 return NULL;
109 }
110 }
111 return bitmap;
112}
113
114static int bitmap_set(struct bitmap_page *bitmap, unsigned long bit)
115{
116 unsigned int n;
117
118 n = BITMAP_PAGE_BITS;
119 while (bitmap && n <= bit) {
120 n += BITMAP_PAGE_BITS;
121 bitmap = bitmap->next;
122 }
123 if (!bitmap)
124 return -EINVAL;
125 n -= BITMAP_PAGE_BITS;
126 bit -= n;
127 n = 0;
128 while (bit >= BITS_PER_CHUNK) {
129 bit -= BITS_PER_CHUNK;
130 n++;
131 }
132 bitmap->chunks[n] |= (1UL << bit);
133 return 0;
134}
135
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800136sector_t alloc_swapdev_block(int swap, struct bitmap_page *bitmap)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800137{
138 unsigned long offset;
139
140 offset = swp_offset(get_swap_page_of_type(swap));
141 if (offset) {
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800142 if (bitmap_set(bitmap, offset))
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800143 swap_free(swp_entry(swap, offset));
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800144 else
145 return swapdev_block(swap, offset);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800146 }
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800147 return 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800148}
149
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800150void free_all_swap_pages(int swap, struct bitmap_page *bitmap)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800151{
152 unsigned int bit, n;
153 unsigned long test;
154
155 bit = 0;
156 while (bitmap) {
157 for (n = 0; n < BITMAP_PAGE_CHUNKS; n++)
158 for (test = 1UL; test; test <<= 1) {
159 if (bitmap->chunks[n] & test)
160 swap_free(swp_entry(swap, bit));
161 bit++;
162 }
163 bitmap = bitmap->next;
164 }
165}
166
167/**
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800168 * swsusp_show_speed - print the time elapsed between two events represented by
169 * @start and @stop
170 *
171 * @nr_pages - number of pages processed between @start and @stop
172 * @msg - introductory message to print
173 */
174
175void swsusp_show_speed(struct timeval *start, struct timeval *stop,
176 unsigned nr_pages, char *msg)
177{
178 s64 elapsed_centisecs64;
179 int centisecs;
180 int k;
181 int kps;
182
183 elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
184 do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
185 centisecs = elapsed_centisecs64;
186 if (centisecs == 0)
187 centisecs = 1; /* avoid div-by-zero */
188 k = nr_pages * (PAGE_SIZE / 1024);
189 kps = (k * 100) / centisecs;
190 printk("%s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n", msg, k,
191 centisecs / 100, centisecs % 100,
192 kps / 1000, (kps % 1000) / 10);
193}
194
195/**
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800196 * swsusp_shrink_memory - Try to free as much memory as needed
197 *
198 * ... but do not OOM-kill anyone
199 *
200 * Notice: all userland should be stopped before it is called, or
201 * livelock is possible.
202 */
203
204#define SHRINK_BITE 10000
Rafael J. Wysockid6277db2006-06-23 02:03:18 -0700205static inline unsigned long __shrink_memory(long tmp)
206{
207 if (tmp > SHRINK_BITE)
208 tmp = SHRINK_BITE;
209 return shrink_all_memory(tmp);
210}
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800211
212int swsusp_shrink_memory(void)
213{
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800214 long tmp;
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800215 struct zone *zone;
216 unsigned long pages = 0;
217 unsigned int i = 0;
218 char *p = "-\\|/";
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800219 struct timeval start, stop;
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800220
221 printk("Shrinking memory... ");
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800222 do_gettimeofday(&start);
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800223 do {
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800224 long size, highmem_size;
225
226 highmem_size = count_highmem_pages();
227 size = count_data_pages() + PAGES_FOR_IO;
Rafael J. Wysockib3a93a22006-01-06 00:15:22 -0800228 tmp = size;
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800229 size += highmem_size;
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800230 for_each_zone (zone)
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800231 if (populated_zone(zone)) {
232 if (is_highmem(zone)) {
233 highmem_size -= zone->free_pages;
234 } else {
235 tmp -= zone->free_pages;
236 tmp += zone->lowmem_reserve[ZONE_NORMAL];
237 tmp += snapshot_additional_pages(zone);
238 }
Rafael J. Wysockia938c352006-06-23 02:04:46 -0700239 }
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800240
241 if (highmem_size < 0)
242 highmem_size = 0;
243
244 tmp += highmem_size;
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800245 if (tmp > 0) {
Rafael J. Wysockid6277db2006-06-23 02:03:18 -0700246 tmp = __shrink_memory(tmp);
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800247 if (!tmp)
248 return -ENOMEM;
249 pages += tmp;
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800250 } else if (size > image_size / PAGE_SIZE) {
Rafael J. Wysockid6277db2006-06-23 02:03:18 -0700251 tmp = __shrink_memory(size - (image_size / PAGE_SIZE));
Rafael J. Wysockib3a93a22006-01-06 00:15:22 -0800252 pages += tmp;
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800253 }
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800254 printk("\b%c", p[i++%4]);
255 } while (tmp > 0);
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800256 do_gettimeofday(&stop);
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800257 printk("\bdone (%lu pages freed)\n", pages);
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800258 swsusp_show_speed(&start, &stop, pages, "Freed");
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800259
260 return 0;
261}
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263int swsusp_suspend(void)
264{
265 int error;
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if ((error = arch_prepare_suspend()))
268 return error;
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 local_irq_disable();
271 /* At this point, device_suspend() has been called, but *not*
272 * device_power_down(). We *must* device_power_down() now.
273 * Otherwise, drivers for some devices (e.g. interrupt controllers)
274 * become desynchronized with the actual state of the hardware
275 * at resume time, and evil weirdness ensues.
276 */
277 if ((error = device_power_down(PMSG_FREEZE))) {
Pavel Machek99dc7d62005-09-03 15:57:05 -0700278 printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800279 goto Enable_irqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
Pavel Machek47b724f2005-07-07 17:56:44 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 save_processor_state();
283 if ((error = swsusp_arch_suspend()))
Pavel Machek99dc7d62005-09-03 15:57:05 -0700284 printk(KERN_ERR "Error %d suspending\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /* Restore control flow magically appears here */
286 restore_processor_state();
David Brownellf1cc0a82006-08-14 23:11:08 -0700287 /* NOTE: device_power_up() is just a resume() for devices
288 * that suspended with irqs off ... no overall powerup.
289 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 device_power_up();
Rafael J. Wysocki59a49332006-12-06 20:34:44 -0800291 Enable_irqs:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 local_irq_enable();
293 return error;
294}
295
296int swsusp_resume(void)
297{
298 int error;
David Brownellf1cc0a82006-08-14 23:11:08 -0700299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 local_irq_disable();
David Brownellf1cc0a82006-08-14 23:11:08 -0700301 /* NOTE: device_power_down() is just a suspend() with irqs off;
302 * it has no special "power things down" semantics
303 */
304 if (device_power_down(PMSG_PRETHAW))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 printk(KERN_ERR "Some devices failed to power down, very bad\n");
306 /* We'll ignore saved state, but this gets preempt count (etc) right */
307 save_processor_state();
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800308 error = restore_highmem();
309 if (!error) {
310 error = swsusp_arch_resume();
311 /* The code below is only ever reached in case of a failure.
312 * Otherwise execution continues at place where
313 * swsusp_arch_suspend() was called
314 */
315 BUG_ON(!error);
316 /* This call to restore_highmem() undos the previous one */
317 restore_highmem();
318 }
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800319 /* The only reason why swsusp_arch_resume() can fail is memory being
320 * very tight, so we have to free it as soon as we can to avoid
321 * subsequent failures
322 */
323 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 restore_processor_state();
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700325 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 device_power_up();
327 local_irq_enable();
328 return error;
329}