[PATCH] m68knommu: ColdFire 532x CPU platform build support
[deliverable/linux.git] / kernel / power / swsusp.c
CommitLineData
1da177e4
LT
1/*
2 * linux/kernel/power/swsusp.c
3 *
96bc7aec 4 * This file provides code to write suspend image to swap and read it back.
1da177e4
LT
5 *
6 * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
25761b6e 7 * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
1da177e4
LT
8 *
9 * This file is released under the GPLv2.
10 *
11 * I'd like to thank the following people for their work:
2e4d5822 12 *
1da177e4
LT
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 *
2e4d5822 17 * Steve Doddi <dirk@loth.demon.co.uk>:
1da177e4
LT
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 *
7088a5c0 33 * Rafael J. Wysocki <rjw@sisk.pl>
61159a31 34 * Reworked the freeing of memory and the handling of swap
7088a5c0 35 *
1da177e4
LT
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
1da177e4
LT
41#include <linux/mm.h>
42#include <linux/suspend.h>
1da177e4 43#include <linux/spinlock.h>
1da177e4
LT
44#include <linux/kernel.h>
45#include <linux/major.h>
46#include <linux/swap.h>
47#include <linux/pm.h>
1da177e4
LT
48#include <linux/swapops.h>
49#include <linux/bootmem.h>
50#include <linux/syscalls.h>
1da177e4 51#include <linux/highmem.h>
1da177e4
LT
52
53#include "power.h"
54
ca0aec0f 55/*
853609b6 56 * Preferred image size in bytes (tunable via /sys/power/image_size).
ca0aec0f 57 * When it is set to N, swsusp will do its best to ensure the image
853609b6 58 * size will not exceed N bytes, but if that is impossible, it will
ca0aec0f
RW
59 * try to create the smallest image possible.
60 */
853609b6 61unsigned long image_size = 500 * 1024 * 1024;
ca0aec0f 62
f577eb30
RW
63int in_suspend __nosavedata = 0;
64
1da177e4 65/**
f577eb30
RW
66 * The following functions are used for tracing the allocated
67 * swap pages, so that they can be freed in case of an error.
7088a5c0 68 *
f577eb30 69 * The functions operate on a linked bitmap structure defined
61159a31 70 * in power.h
1da177e4 71 */
7088a5c0 72
61159a31 73void free_bitmap(struct bitmap_page *bitmap)
1da177e4 74{
f577eb30 75 struct bitmap_page *bp;
1da177e4 76
f577eb30
RW
77 while (bitmap) {
78 bp = bitmap->next;
79 free_page((unsigned long)bitmap);
80 bitmap = bp;
7088a5c0
RW
81 }
82}
83
61159a31 84struct bitmap_page *alloc_bitmap(unsigned int nr_bits)
7088a5c0 85{
f577eb30
RW
86 struct bitmap_page *bitmap, *bp;
87 unsigned int n;
7088a5c0 88
f577eb30 89 if (!nr_bits)
7088a5c0
RW
90 return NULL;
91
f577eb30
RW
92 bitmap = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL);
93 bp = bitmap;
94 for (n = BITMAP_PAGE_BITS; n < nr_bits; n += BITMAP_PAGE_BITS) {
95 bp->next = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL);
96 bp = bp->next;
97 if (!bp) {
98 free_bitmap(bitmap);
7088a5c0
RW
99 return NULL;
100 }
1da177e4 101 }
f577eb30 102 return bitmap;
1da177e4
LT
103}
104
f577eb30 105static int bitmap_set(struct bitmap_page *bitmap, unsigned long bit)
1da177e4 106{
f577eb30
RW
107 unsigned int n;
108
109 n = BITMAP_PAGE_BITS;
110 while (bitmap && n <= bit) {
111 n += BITMAP_PAGE_BITS;
112 bitmap = bitmap->next;
113 }
114 if (!bitmap)
115 return -EINVAL;
116 n -= BITMAP_PAGE_BITS;
117 bit -= n;
118 n = 0;
119 while (bit >= BITS_PER_CHUNK) {
120 bit -= BITS_PER_CHUNK;
121 n++;
7088a5c0 122 }
f577eb30
RW
123 bitmap->chunks[n] |= (1UL << bit);
124 return 0;
7088a5c0 125}
1da177e4 126
61159a31 127unsigned long alloc_swap_page(int swap, struct bitmap_page *bitmap)
7088a5c0 128{
f577eb30
RW
129 unsigned long offset;
130
131 offset = swp_offset(get_swap_page_of_type(swap));
132 if (offset) {
133 if (bitmap_set(bitmap, offset)) {
134 swap_free(swp_entry(swap, offset));
135 offset = 0;
136 }
7088a5c0 137 }
f577eb30 138 return offset;
7088a5c0 139}
1da177e4 140
61159a31 141void free_all_swap_pages(int swap, struct bitmap_page *bitmap)
7088a5c0 142{
f577eb30
RW
143 unsigned int bit, n;
144 unsigned long test;
7088a5c0 145
f577eb30
RW
146 bit = 0;
147 while (bitmap) {
148 for (n = 0; n < BITMAP_PAGE_CHUNKS; n++)
149 for (test = 1UL; test; test <<= 1) {
150 if (bitmap->chunks[n] & test)
151 swap_free(swp_entry(swap, bit));
152 bit++;
153 }
154 bitmap = bitmap->next;
1da177e4 155 }
7088a5c0
RW
156}
157
72a97e08
RW
158/**
159 * swsusp_shrink_memory - Try to free as much memory as needed
160 *
161 * ... but do not OOM-kill anyone
162 *
163 * Notice: all userland should be stopped before it is called, or
164 * livelock is possible.
165 */
166
167#define SHRINK_BITE 10000
d6277db4
RW
168static inline unsigned long __shrink_memory(long tmp)
169{
170 if (tmp > SHRINK_BITE)
171 tmp = SHRINK_BITE;
172 return shrink_all_memory(tmp);
173}
72a97e08
RW
174
175int swsusp_shrink_memory(void)
176{
b3a93a25 177 long size, tmp;
72a97e08
RW
178 struct zone *zone;
179 unsigned long pages = 0;
180 unsigned int i = 0;
181 char *p = "-\\|/";
182
183 printk("Shrinking memory... ");
184 do {
ce4ab001 185 size = 2 * count_special_pages();
b3a93a25
RW
186 size += size / 50 + count_data_pages();
187 size += (size + PBES_PER_PAGE - 1) / PBES_PER_PAGE +
72a97e08 188 PAGES_FOR_IO;
b3a93a25 189 tmp = size;
72a97e08 190 for_each_zone (zone)
a938c356 191 if (!is_highmem(zone) && populated_zone(zone)) {
72a97e08 192 tmp -= zone->free_pages;
a938c356
RW
193 tmp += zone->lowmem_reserve[ZONE_NORMAL];
194 }
72a97e08 195 if (tmp > 0) {
d6277db4 196 tmp = __shrink_memory(tmp);
72a97e08
RW
197 if (!tmp)
198 return -ENOMEM;
199 pages += tmp;
853609b6 200 } else if (size > image_size / PAGE_SIZE) {
d6277db4 201 tmp = __shrink_memory(size - (image_size / PAGE_SIZE));
b3a93a25 202 pages += tmp;
72a97e08 203 }
72a97e08
RW
204 printk("\b%c", p[i++%4]);
205 } while (tmp > 0);
206 printk("\bdone (%lu pages freed)\n", pages);
207
208 return 0;
209}
210
1da177e4
LT
211int swsusp_suspend(void)
212{
213 int error;
0fbeb5a4 214
1da177e4
LT
215 if ((error = arch_prepare_suspend()))
216 return error;
217 local_irq_disable();
218 /* At this point, device_suspend() has been called, but *not*
219 * device_power_down(). We *must* device_power_down() now.
220 * Otherwise, drivers for some devices (e.g. interrupt controllers)
221 * become desynchronized with the actual state of the hardware
222 * at resume time, and evil weirdness ensues.
223 */
224 if ((error = device_power_down(PMSG_FREEZE))) {
99dc7d63 225 printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
0fbeb5a4 226 goto Enable_irqs;
1da177e4 227 }
47b724f3 228
ce4ab001 229 if ((error = save_special_mem())) {
0fbeb5a4
RW
230 printk(KERN_ERR "swsusp: Not enough free pages for highmem\n");
231 goto Restore_highmem;
47b724f3
PM
232 }
233
1da177e4
LT
234 save_processor_state();
235 if ((error = swsusp_arch_suspend()))
99dc7d63 236 printk(KERN_ERR "Error %d suspending\n", error);
1da177e4
LT
237 /* Restore control flow magically appears here */
238 restore_processor_state();
0fbeb5a4 239Restore_highmem:
ce4ab001 240 restore_special_mem();
1da177e4 241 device_power_up();
0fbeb5a4 242Enable_irqs:
1da177e4
LT
243 local_irq_enable();
244 return error;
245}
246
247int swsusp_resume(void)
248{
249 int error;
250 local_irq_disable();
251 if (device_power_down(PMSG_FREEZE))
252 printk(KERN_ERR "Some devices failed to power down, very bad\n");
253 /* We'll ignore saved state, but this gets preempt count (etc) right */
254 save_processor_state();
255 error = swsusp_arch_resume();
256 /* Code below is only ever reached in case of failure. Otherwise
257 * execution continues at place where swsusp_arch_suspend was called
258 */
259 BUG_ON(!error);
2c1b4a5c
RW
260 /* The only reason why swsusp_arch_resume() can fail is memory being
261 * very tight, so we have to free it as soon as we can to avoid
262 * subsequent failures
263 */
264 swsusp_free();
1da177e4 265 restore_processor_state();
ce4ab001 266 restore_special_mem();
8446f1d3 267 touch_softlockup_watchdog();
1da177e4
LT
268 device_power_up();
269 local_irq_enable();
270 return error;
271}
This page took 0.279243 seconds and 5 git commands to generate.