2 * linux/kernel/power/swsusp.c
4 * This file provides code to write suspend image to swap and read it back.
6 * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
7 * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
9 * This file is released under the GPLv2.
11 * I'd like to thank the following people for their work:
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.
17 * Steve Doddi <dirk@loth.demon.co.uk>:
18 * Support the possibility of hardware state restoring.
20 * Raph <grey.havens@earthling.net>:
21 * Support for preserving states of network devices and virtual console
22 * (including X and svgatextmode)
24 * Kurt Garloff <garloff@suse.de>:
25 * Straightened the critical function in order to prevent compilers from
26 * playing tricks with local variables.
28 * Andreas Mohr <a.mohr@mailto.de>
30 * Alex Badea <vampire@go.ro>:
33 * Rafael J. Wysocki <rjw@sisk.pl>
34 * Reworked the freeing of memory and the handling of swap
36 * More state savers are welcome. Especially for the scsi layer...
38 * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt
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>
48 #include <linux/swapops.h>
49 #include <linux/bootmem.h>
50 #include <linux/syscalls.h>
51 #include <linux/highmem.h>
52 #include <linux/time.h>
53 #include <linux/rbtree.h>
59 * Preferred image size in bytes (tunable via /sys/power/image_size).
60 * When it is set to N, swsusp will do its best to ensure the image
61 * size will not exceed N bytes, but if that is impossible, it will
62 * try to create the smallest image possible.
64 unsigned long image_size = 500 * 1024 * 1024;
66 int in_suspend __nosavedata = 0;
69 * The following functions are used for tracing the allocated
70 * swap pages, so that they can be freed in case of an error.
73 struct swsusp_extent {
79 static struct rb_root swsusp_extents = RB_ROOT;
81 static int swsusp_extents_insert(unsigned long swap_offset)
83 struct rb_node **new = &(swsusp_extents.rb_node);
84 struct rb_node *parent = NULL;
85 struct swsusp_extent *ext;
87 /* Figure out where to put the new node */
89 ext = container_of(*new, struct swsusp_extent, node);
91 if (swap_offset < ext->start) {
93 if (swap_offset == ext->start - 1) {
97 new = &((*new)->rb_left);
98 } else if (swap_offset > ext->end) {
100 if (swap_offset == ext->end + 1) {
104 new = &((*new)->rb_right);
106 /* It already is in the tree */
110 /* Add the new node and rebalance the tree. */
111 ext = kzalloc(sizeof(struct swsusp_extent), GFP_KERNEL);
115 ext->start = swap_offset;
116 ext->end = swap_offset;
117 rb_link_node(&ext->node, parent, new);
118 rb_insert_color(&ext->node, &swsusp_extents);
123 * alloc_swapdev_block - allocate a swap page and register that it has
124 * been allocated, so that it can be freed in case of an error.
127 sector_t alloc_swapdev_block(int swap)
129 unsigned long offset;
131 offset = swp_offset(get_swap_page_of_type(swap));
133 if (swsusp_extents_insert(offset))
134 swap_free(swp_entry(swap, offset));
136 return swapdev_block(swap, offset);
142 * free_all_swap_pages - free swap pages allocated for saving image data.
143 * It also frees the extents used to register which swap entres had been
147 void free_all_swap_pages(int swap)
149 struct rb_node *node;
151 while ((node = swsusp_extents.rb_node)) {
152 struct swsusp_extent *ext;
153 unsigned long offset;
155 ext = container_of(node, struct swsusp_extent, node);
156 rb_erase(node, &swsusp_extents);
157 for (offset = ext->start; offset <= ext->end; offset++)
158 swap_free(swp_entry(swap, offset));
164 int swsusp_swap_in_use(void)
166 return (swsusp_extents.rb_node != NULL);
170 * swsusp_show_speed - print the time elapsed between two events represented by
173 * @nr_pages - number of pages processed between @start and @stop
174 * @msg - introductory message to print
177 void swsusp_show_speed(struct timeval *start, struct timeval *stop,
178 unsigned nr_pages, char *msg)
180 s64 elapsed_centisecs64;
185 elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
186 do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
187 centisecs = elapsed_centisecs64;
189 centisecs = 1; /* avoid div-by-zero */
190 k = nr_pages * (PAGE_SIZE / 1024);
191 kps = (k * 100) / centisecs;
192 printk(KERN_INFO "PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
194 centisecs / 100, centisecs % 100,
195 kps / 1000, (kps % 1000) / 10);
199 * swsusp_shrink_memory - Try to free as much memory as needed
201 * ... but do not OOM-kill anyone
203 * Notice: all userland should be stopped before it is called, or
204 * livelock is possible.
207 #define SHRINK_BITE 10000
208 static inline unsigned long __shrink_memory(long tmp)
210 if (tmp > SHRINK_BITE)
212 return shrink_all_memory(tmp);
215 int swsusp_shrink_memory(void)
219 unsigned long pages = 0;
222 struct timeval start, stop;
224 printk(KERN_INFO "PM: Shrinking memory... ");
225 do_gettimeofday(&start);
227 long size, highmem_size;
229 highmem_size = count_highmem_pages();
230 size = count_data_pages() + PAGES_FOR_IO + SPARE_PAGES;
232 size += highmem_size;
233 for_each_populated_zone(zone) {
234 tmp += snapshot_additional_pages(zone);
235 if (is_highmem(zone)) {
237 zone_page_state(zone, NR_FREE_PAGES);
239 tmp -= zone_page_state(zone, NR_FREE_PAGES);
240 tmp += zone->lowmem_reserve[ZONE_NORMAL];
244 if (highmem_size < 0)
249 tmp = __shrink_memory(tmp);
253 } else if (size > image_size / PAGE_SIZE) {
254 tmp = __shrink_memory(size - (image_size / PAGE_SIZE));
257 printk("\b%c", p[i++%4]);
259 do_gettimeofday(&stop);
260 printk("\bdone (%lu pages freed)\n", pages);
261 swsusp_show_speed(&start, &stop, pages, "Freed");
267 * Platforms, like ACPI, may want us to save some memory used by them during
268 * hibernation and to restore the contents of this memory during the subsequent
269 * resume. The code below implements a mechanism allowing us to do that.
273 unsigned long phys_start;
277 struct list_head node;
280 static LIST_HEAD(nvs_list);
283 * hibernate_nvs_register - register platform NVS memory region to save
284 * @start - physical address of the region
285 * @size - size of the region
287 * The NVS region need not be page-aligned (both ends) and we arrange
288 * things so that the data from page-aligned addresses in this region will
289 * be copied into separate RAM pages.
291 int hibernate_nvs_register(unsigned long start, unsigned long size)
293 struct nvs_page *entry, *next;
296 unsigned int nr_bytes;
298 entry = kzalloc(sizeof(struct nvs_page), GFP_KERNEL);
302 list_add_tail(&entry->node, &nvs_list);
303 entry->phys_start = start;
304 nr_bytes = PAGE_SIZE - (start & ~PAGE_MASK);
305 entry->size = (size < nr_bytes) ? size : nr_bytes;
307 start += entry->size;
313 list_for_each_entry_safe(entry, next, &nvs_list, node) {
314 list_del(&entry->node);
321 * hibernate_nvs_free - free data pages allocated for saving NVS regions
323 void hibernate_nvs_free(void)
325 struct nvs_page *entry;
327 list_for_each_entry(entry, &nvs_list, node)
329 free_page((unsigned long)entry->data);
332 iounmap(entry->kaddr);
339 * hibernate_nvs_alloc - allocate memory necessary for saving NVS regions
341 int hibernate_nvs_alloc(void)
343 struct nvs_page *entry;
345 list_for_each_entry(entry, &nvs_list, node) {
346 entry->data = (void *)__get_free_page(GFP_KERNEL);
348 hibernate_nvs_free();
356 * hibernate_nvs_save - save NVS memory regions
358 void hibernate_nvs_save(void)
360 struct nvs_page *entry;
362 printk(KERN_INFO "PM: Saving platform NVS memory\n");
364 list_for_each_entry(entry, &nvs_list, node)
366 entry->kaddr = ioremap(entry->phys_start, entry->size);
367 memcpy(entry->data, entry->kaddr, entry->size);
372 * hibernate_nvs_restore - restore NVS memory regions
374 * This function is going to be called with interrupts disabled, so it
375 * cannot iounmap the virtual addresses used to access the NVS region.
377 void hibernate_nvs_restore(void)
379 struct nvs_page *entry;
381 printk(KERN_INFO "PM: Restoring platform NVS memory\n");
383 list_for_each_entry(entry, &nvs_list, node)
385 memcpy(entry->kaddr, entry->data, entry->size);