blob: 62752899b1a1f32dcee8d547599e1cad2708e521 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#include "power.h"
54
Rafael J. Wysockica0aec02006-01-06 00:15:56 -080055/*
Rafael J. Wysocki853609b2006-02-01 03:05:07 -080056 * Preferred image size in bytes (tunable via /sys/power/image_size).
Rafael J. Wysockica0aec02006-01-06 00:15:56 -080057 * When it is set to N, swsusp will do its best to ensure the image
Rafael J. Wysocki853609b2006-02-01 03:05:07 -080058 * size will not exceed N bytes, but if that is impossible, it will
Rafael J. Wysockica0aec02006-01-06 00:15:56 -080059 * try to create the smallest image possible.
60 */
Rafael J. Wysocki853609b2006-02-01 03:05:07 -080061unsigned long image_size = 500 * 1024 * 1024;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -080062
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080063int in_suspend __nosavedata = 0;
64
Linus Torvalds34480972006-06-25 18:41:00 -070065#ifdef CONFIG_HIGHMEM
66unsigned int count_highmem_pages(void);
67int save_highmem(void);
68int restore_highmem(void);
69#else
70static inline int save_highmem(void) { return 0; }
71static inline int restore_highmem(void) { return 0; }
72static inline unsigned int count_highmem_pages(void) { return 0; }
73#endif
74
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080075/**
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
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080080 * in power.h
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080081 */
82
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080083void free_bitmap(struct bitmap_page *bitmap)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080084{
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
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080094struct bitmap_page *alloc_bitmap(unsigned int nr_bits)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080095{
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
115static 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
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800137unsigned long alloc_swap_page(int swap, struct bitmap_page *bitmap)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800138{
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
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800151void free_all_swap_pages(int swap, struct bitmap_page *bitmap)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800152{
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/**
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800169 * 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
Rafael J. Wysockid6277db2006-06-23 02:03:18 -0700178static inline unsigned long __shrink_memory(long tmp)
179{
180 if (tmp > SHRINK_BITE)
181 tmp = SHRINK_BITE;
182 return shrink_all_memory(tmp);
183}
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800184
185int swsusp_shrink_memory(void)
186{
Rafael J. Wysockib3a93a22006-01-06 00:15:22 -0800187 long size, tmp;
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800188 struct zone *zone;
189 unsigned long pages = 0;
190 unsigned int i = 0;
191 char *p = "-\\|/";
192
193 printk("Shrinking memory... ");
194 do {
Linus Torvalds34480972006-06-25 18:41:00 -0700195 size = 2 * count_highmem_pages();
Rafael J. Wysockib3a93a22006-01-06 00:15:22 -0800196 size += size / 50 + count_data_pages();
197 size += (size + PBES_PER_PAGE - 1) / PBES_PER_PAGE +
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800198 PAGES_FOR_IO;
Rafael J. Wysockib3a93a22006-01-06 00:15:22 -0800199 tmp = size;
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800200 for_each_zone (zone)
Rafael J. Wysockia938c352006-06-23 02:04:46 -0700201 if (!is_highmem(zone) && populated_zone(zone)) {
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800202 tmp -= zone->free_pages;
Rafael J. Wysockia938c352006-06-23 02:04:46 -0700203 tmp += zone->lowmem_reserve[ZONE_NORMAL];
204 }
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800205 if (tmp > 0) {
Rafael J. Wysockid6277db2006-06-23 02:03:18 -0700206 tmp = __shrink_memory(tmp);
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800207 if (!tmp)
208 return -ENOMEM;
209 pages += tmp;
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800210 } else if (size > image_size / PAGE_SIZE) {
Rafael J. Wysockid6277db2006-06-23 02:03:18 -0700211 tmp = __shrink_memory(size - (image_size / PAGE_SIZE));
Rafael J. Wysockib3a93a22006-01-06 00:15:22 -0800212 pages += tmp;
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800213 }
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800214 printk("\b%c", p[i++%4]);
215 } while (tmp > 0);
216 printk("\bdone (%lu pages freed)\n", pages);
217
218 return 0;
219}
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221int swsusp_suspend(void)
222{
223 int error;
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if ((error = arch_prepare_suspend()))
226 return error;
227 local_irq_disable();
228 /* At this point, device_suspend() has been called, but *not*
229 * device_power_down(). We *must* device_power_down() now.
230 * Otherwise, drivers for some devices (e.g. interrupt controllers)
231 * become desynchronized with the actual state of the hardware
232 * at resume time, and evil weirdness ensues.
233 */
234 if ((error = device_power_down(PMSG_FREEZE))) {
Pavel Machek99dc7d62005-09-03 15:57:05 -0700235 printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800236 goto Enable_irqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
Pavel Machek47b724f2005-07-07 17:56:44 -0700238
Linus Torvalds34480972006-06-25 18:41:00 -0700239 if ((error = save_highmem())) {
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800240 printk(KERN_ERR "swsusp: Not enough free pages for highmem\n");
241 goto Restore_highmem;
Pavel Machek47b724f2005-07-07 17:56:44 -0700242 }
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 save_processor_state();
245 if ((error = swsusp_arch_suspend()))
Pavel Machek99dc7d62005-09-03 15:57:05 -0700246 printk(KERN_ERR "Error %d suspending\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /* Restore control flow magically appears here */
248 restore_processor_state();
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800249Restore_highmem:
Linus Torvalds34480972006-06-25 18:41:00 -0700250 restore_highmem();
David Brownellf1cc0a82006-08-14 23:11:08 -0700251 /* NOTE: device_power_up() is just a resume() for devices
252 * that suspended with irqs off ... no overall powerup.
253 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 device_power_up();
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800255Enable_irqs:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 local_irq_enable();
257 return error;
258}
259
260int swsusp_resume(void)
261{
262 int error;
David Brownellf1cc0a82006-08-14 23:11:08 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 local_irq_disable();
David Brownellf1cc0a82006-08-14 23:11:08 -0700265 /* NOTE: device_power_down() is just a suspend() with irqs off;
266 * it has no special "power things down" semantics
267 */
268 if (device_power_down(PMSG_PRETHAW))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 printk(KERN_ERR "Some devices failed to power down, very bad\n");
270 /* We'll ignore saved state, but this gets preempt count (etc) right */
271 save_processor_state();
272 error = swsusp_arch_resume();
273 /* Code below is only ever reached in case of failure. Otherwise
274 * execution continues at place where swsusp_arch_suspend was called
275 */
276 BUG_ON(!error);
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800277 /* The only reason why swsusp_arch_resume() can fail is memory being
278 * very tight, so we have to free it as soon as we can to avoid
279 * subsequent failures
280 */
281 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 restore_processor_state();
Linus Torvalds34480972006-06-25 18:41:00 -0700283 restore_highmem();
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700284 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 device_power_up();
286 local_irq_enable();
287 return error;
288}