2 * linux/kernel/power/swsusp.c
4 * This file is to realize architecture-independent
5 * machine suspend feature using pretty near only high-level routines
7 * Copyright (C) 1998-2005 Pavel Machek <pavel@suse.cz>
9 * This file is released under the GPLv2, and is based on swsusp.c.
14 #include <linux/module.h>
16 #include <linux/suspend.h>
17 #include <linux/smp_lock.h>
18 #include <linux/file.h>
19 #include <linux/utsname.h>
20 #include <linux/version.h>
21 #include <linux/delay.h>
22 #include <linux/reboot.h>
23 #include <linux/bitops.h>
24 #include <linux/vt_kern.h>
25 #include <linux/kbd_kern.h>
26 #include <linux/keyboard.h>
27 #include <linux/spinlock.h>
28 #include <linux/genhd.h>
29 #include <linux/kernel.h>
30 #include <linux/major.h>
31 #include <linux/swap.h>
33 #include <linux/device.h>
34 #include <linux/buffer_head.h>
35 #include <linux/swapops.h>
36 #include <linux/bootmem.h>
37 #include <linux/syscalls.h>
38 #include <linux/console.h>
39 #include <linux/highmem.h>
40 #include <linux/bio.h>
41 #include <linux/mount.h>
43 #include <asm/uaccess.h>
44 #include <asm/mmu_context.h>
45 #include <asm/pgtable.h>
46 #include <asm/tlbflush.h>
49 #include <linux/random.h>
50 #include <linux/crypto.h>
51 #include <asm/scatterlist.h>
62 struct highmem_page *next;
65 static struct highmem_page *highmem_copy;
67 static int save_highmem_zone(struct zone *zone)
69 unsigned long zone_pfn;
70 mark_free_pages(zone);
71 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
73 struct highmem_page *save;
75 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
81 page = pfn_to_page(pfn);
83 * This condition results from rvmalloc() sans vmalloc_32()
84 * and architectural memory reservations. This should be
85 * corrected eventually when the cases giving rise to this
86 * are better understood.
88 if (PageReserved(page)) {
89 printk("highmem reserved page?!\n");
92 BUG_ON(PageNosave(page));
93 if (PageNosaveFree(page))
95 save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
98 save->next = highmem_copy;
100 save->data = (void *) get_zeroed_page(GFP_ATOMIC);
105 kaddr = kmap_atomic(page, KM_USER0);
106 memcpy(save->data, kaddr, PAGE_SIZE);
107 kunmap_atomic(kaddr, KM_USER0);
112 #endif /* CONFIG_HIGHMEM */
115 static int save_highmem(void)
117 #ifdef CONFIG_HIGHMEM
121 pr_debug("swsusp: Saving Highmem\n");
122 for_each_zone (zone) {
123 if (is_highmem(zone))
124 res = save_highmem_zone(zone);
132 int restore_highmem(void)
134 #ifdef CONFIG_HIGHMEM
135 printk("swsusp: Restoring Highmem\n");
136 while (highmem_copy) {
137 struct highmem_page *save = highmem_copy;
139 highmem_copy = save->next;
141 kaddr = kmap_atomic(save->page, KM_USER0);
142 memcpy(kaddr, save->data, PAGE_SIZE);
143 kunmap_atomic(kaddr, KM_USER0);
144 free_page((long) save->data);
152 static int pfn_is_nosave(unsigned long pfn)
154 unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
155 unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
156 return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
160 * saveable - Determine whether a page should be cloned or not.
163 * We save a page if it's Reserved, and not in the range of pages
164 * statically defined as 'unsaveable', or if it isn't reserved, and
165 * isn't part of a free chunk of pages.
168 static int saveable(struct zone * zone, unsigned long * zone_pfn)
170 unsigned long pfn = *zone_pfn + zone->zone_start_pfn;
176 page = pfn_to_page(pfn);
177 BUG_ON(PageReserved(page) && PageNosave(page));
178 if (PageNosave(page))
180 if (PageReserved(page) && pfn_is_nosave(pfn)) {
181 pr_debug("[nosave pfn 0x%lx]", pfn);
184 if (PageNosaveFree(page))
190 static void count_data_pages(void)
193 unsigned long zone_pfn;
197 for_each_zone (zone) {
198 if (is_highmem(zone))
200 mark_free_pages(zone);
201 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
202 nr_copy_pages += saveable(zone, &zone_pfn);
206 static void copy_data_pages(void)
209 unsigned long zone_pfn;
210 struct pbe *pbe = pagedir_nosave, *p;
212 pr_debug("copy_data_pages(): pages to copy: %d\n", nr_copy_pages);
213 for_each_zone (zone) {
214 if (is_highmem(zone))
216 mark_free_pages(zone);
217 /* This is necessary for swsusp_free() */
218 for_each_pb_page (p, pagedir_nosave)
219 SetPageNosaveFree(virt_to_page(p));
220 for_each_pbe(p, pagedir_nosave)
221 SetPageNosaveFree(virt_to_page(p->address));
222 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
223 if (saveable(zone, &zone_pfn)) {
225 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
227 pbe->orig_address = (unsigned long)page_address(page);
228 /* copy_page is not usable for copying task structs. */
229 memcpy((void *)pbe->address, (void *)pbe->orig_address, PAGE_SIZE);
239 * free_pagedir - free pages allocated with alloc_pagedir()
242 void free_pagedir(struct pbe *pblist)
247 pbe = (pblist + PB_PAGE_SKIP)->next;
248 ClearPageNosave(virt_to_page(pblist));
249 ClearPageNosaveFree(virt_to_page(pblist));
250 free_page((unsigned long)pblist);
256 * fill_pb_page - Create a list of PBEs on a given memory page
259 static inline void fill_pb_page(struct pbe *pbpage)
264 pbpage += PB_PAGE_SKIP;
267 while (++p < pbpage);
271 * create_pbe_list - Create a list of PBEs on top of a given chain
272 * of memory pages allocated with alloc_pagedir()
275 void create_pbe_list(struct pbe *pblist, unsigned nr_pages)
277 struct pbe *pbpage, *p;
278 unsigned num = PBES_PER_PAGE;
280 for_each_pb_page (pbpage, pblist) {
284 fill_pb_page(pbpage);
285 num += PBES_PER_PAGE;
288 for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
292 pr_debug("create_pbe_list(): initialized %d PBEs\n", num);
295 static void *alloc_image_page(void)
297 void *res = (void *)get_zeroed_page(GFP_ATOMIC | __GFP_COLD);
299 SetPageNosave(virt_to_page(res));
300 SetPageNosaveFree(virt_to_page(res));
306 * alloc_pagedir - Allocate the page directory.
308 * First, determine exactly how many pages we need and
311 * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
312 * struct pbe elements (pbes) and the last element in the page points
315 * On each page we set up a list of struct_pbe elements.
318 struct pbe * alloc_pagedir(unsigned nr_pages)
321 struct pbe *pblist, *pbe;
326 pr_debug("alloc_pagedir(): nr_pages = %d\n", nr_pages);
327 pblist = (struct pbe *)alloc_image_page();
328 /* FIXME: rewrite this ugly loop */
329 for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
330 pbe = pbe->next, num += PBES_PER_PAGE) {
332 pbe->next = (struct pbe *)alloc_image_page();
334 if (!pbe) { /* get_zeroed_page() failed */
335 free_pagedir(pblist);
342 * Free pages we allocated for suspend. Suspend pages are alocated
343 * before atomic copy, so we need to free them after resume.
346 void swsusp_free(void)
349 unsigned long zone_pfn;
351 for_each_zone(zone) {
352 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
353 if (pfn_valid(zone_pfn + zone->zone_start_pfn)) {
355 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
356 if (PageNosave(page) && PageNosaveFree(page)) {
357 ClearPageNosave(page);
358 ClearPageNosaveFree(page);
359 free_page((long) page_address(page));
367 * enough_free_mem - Make sure we enough free memory to snapshot.
369 * Returns TRUE or FALSE after checking the number of available
373 static int enough_free_mem(void)
375 pr_debug("swsusp: available memory: %u pages\n", nr_free_pages());
376 return nr_free_pages() > (nr_copy_pages + PAGES_FOR_IO +
377 nr_copy_pages/PBES_PER_PAGE + !!(nr_copy_pages%PBES_PER_PAGE));
381 static int swsusp_alloc(void)
385 pagedir_nosave = NULL;
387 if (MAX_PBES < nr_copy_pages / PBES_PER_PAGE +
388 !!(nr_copy_pages % PBES_PER_PAGE))
391 if (!(pagedir_save = alloc_pagedir(nr_copy_pages))) {
392 printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
395 create_pbe_list(pagedir_save, nr_copy_pages);
396 pagedir_nosave = pagedir_save;
398 for_each_pbe (p, pagedir_save) {
399 p->address = (unsigned long)alloc_image_page();
401 printk(KERN_ERR "suspend: Allocating image pages failed.\n");
410 static int suspend_prepare_image(void)
414 pr_debug("swsusp: critical section: \n");
415 if (save_highmem()) {
416 printk(KERN_CRIT "swsusp: Not enough free pages for highmem\n");
423 printk("swsusp: Need to copy %u pages\n", nr_copy_pages);
425 pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
427 nr_copy_pages/PBES_PER_PAGE + !!(nr_copy_pages%PBES_PER_PAGE),
428 PAGES_FOR_IO, nr_free_pages());
430 if (!enough_free_mem()) {
431 printk(KERN_ERR "swsusp: Not enough free memory\n");
435 if (!enough_swap()) {
436 printk(KERN_ERR "swsusp: Not enough free swap\n");
440 error = swsusp_alloc();
444 /* During allocating of suspend pagedir, new cold pages may appear.
451 * End of critical section. From now on, we can write to memory,
452 * but we should not touch disk. This specially means we must _not_
453 * touch swap space! Except we must write out our image of course.
456 printk("swsusp: critical section/: done (%d pages copied)\n", nr_copy_pages );
461 asmlinkage int swsusp_save(void)
463 return suspend_prepare_image();