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 unsigned 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 n
+= saveable(zone
, &zone_pfn
);
207 static void copy_data_pages(struct pbe
*pblist
)
210 unsigned long zone_pfn
;
214 for_each_zone (zone
) {
215 if (is_highmem(zone
))
217 mark_free_pages(zone
);
218 /* This is necessary for swsusp_free() */
219 for_each_pb_page (p
, pblist
)
220 SetPageNosaveFree(virt_to_page(p
));
221 for_each_pbe (p
, pblist
)
222 SetPageNosaveFree(virt_to_page(p
->address
));
223 for (zone_pfn
= 0; zone_pfn
< zone
->spanned_pages
; ++zone_pfn
) {
224 if (saveable(zone
, &zone_pfn
)) {
226 page
= pfn_to_page(zone_pfn
+ zone
->zone_start_pfn
);
228 pbe
->orig_address
= (unsigned long)page_address(page
);
229 /* copy_page is not usable for copying task structs. */
230 memcpy((void *)pbe
->address
, (void *)pbe
->orig_address
, PAGE_SIZE
);
240 * free_pagedir - free pages allocated with alloc_pagedir()
243 static void free_pagedir(struct pbe
*pblist
)
248 pbe
= (pblist
+ PB_PAGE_SKIP
)->next
;
249 ClearPageNosave(virt_to_page(pblist
));
250 ClearPageNosaveFree(virt_to_page(pblist
));
251 free_page((unsigned long)pblist
);
257 * fill_pb_page - Create a list of PBEs on a given memory page
260 static inline void fill_pb_page(struct pbe
*pbpage
)
265 pbpage
+= PB_PAGE_SKIP
;
268 while (++p
< pbpage
);
272 * create_pbe_list - Create a list of PBEs on top of a given chain
273 * of memory pages allocated with alloc_pagedir()
276 void create_pbe_list(struct pbe
*pblist
, unsigned nr_pages
)
278 struct pbe
*pbpage
, *p
;
279 unsigned num
= PBES_PER_PAGE
;
281 for_each_pb_page (pbpage
, pblist
) {
285 fill_pb_page(pbpage
);
286 num
+= PBES_PER_PAGE
;
289 for (num
-= PBES_PER_PAGE
- 1, p
= pbpage
; num
< nr_pages
; p
++, num
++)
293 pr_debug("create_pbe_list(): initialized %d PBEs\n", num
);
296 static void *alloc_image_page(void)
298 void *res
= (void *)get_zeroed_page(GFP_ATOMIC
| __GFP_COLD
);
300 SetPageNosave(virt_to_page(res
));
301 SetPageNosaveFree(virt_to_page(res
));
307 * alloc_pagedir - Allocate the page directory.
309 * First, determine exactly how many pages we need and
312 * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
313 * struct pbe elements (pbes) and the last element in the page points
316 * On each page we set up a list of struct_pbe elements.
319 struct pbe
* alloc_pagedir(unsigned nr_pages
)
322 struct pbe
*pblist
, *pbe
;
327 pr_debug("alloc_pagedir(): nr_pages = %d\n", nr_pages
);
328 pblist
= (struct pbe
*)alloc_image_page();
329 /* FIXME: rewrite this ugly loop */
330 for (pbe
= pblist
, num
= PBES_PER_PAGE
; pbe
&& num
< nr_pages
;
331 pbe
= pbe
->next
, num
+= PBES_PER_PAGE
) {
333 pbe
->next
= (struct pbe
*)alloc_image_page();
335 if (!pbe
) { /* get_zeroed_page() failed */
336 free_pagedir(pblist
);
343 * Free pages we allocated for suspend. Suspend pages are alocated
344 * before atomic copy, so we need to free them after resume.
347 void swsusp_free(void)
350 unsigned long zone_pfn
;
352 for_each_zone(zone
) {
353 for (zone_pfn
= 0; zone_pfn
< zone
->spanned_pages
; ++zone_pfn
)
354 if (pfn_valid(zone_pfn
+ zone
->zone_start_pfn
)) {
356 page
= pfn_to_page(zone_pfn
+ zone
->zone_start_pfn
);
357 if (PageNosave(page
) && PageNosaveFree(page
)) {
358 ClearPageNosave(page
);
359 ClearPageNosaveFree(page
);
360 free_page((long) page_address(page
));
368 * enough_free_mem - Make sure we enough free memory to snapshot.
370 * Returns TRUE or FALSE after checking the number of available
374 static int enough_free_mem(unsigned nr_pages
)
376 pr_debug("swsusp: available memory: %u pages\n", nr_free_pages());
377 return nr_free_pages() > (nr_pages
+ PAGES_FOR_IO
+
378 (nr_pages
+ PBES_PER_PAGE
- 1) / PBES_PER_PAGE
);
382 static struct pbe
*swsusp_alloc(unsigned nr_pages
)
384 struct pbe
*pblist
, *p
;
386 if (!(pblist
= alloc_pagedir(nr_pages
))) {
387 printk(KERN_ERR
"suspend: Allocating pagedir failed.\n");
390 create_pbe_list(pblist
, nr_pages
);
392 for_each_pbe (p
, pblist
) {
393 p
->address
= (unsigned long)alloc_image_page();
395 printk(KERN_ERR
"suspend: Allocating image pages failed.\n");
404 static int suspend_prepare_image(void)
408 pr_debug("swsusp: critical section: \n");
409 if (save_highmem()) {
410 printk(KERN_CRIT
"swsusp: Not enough free pages for highmem\n");
416 nr_pages
= count_data_pages();
417 printk("swsusp: Need to copy %u pages\n", nr_pages
);
419 pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
421 (nr_pages
+ PBES_PER_PAGE
- 1) / PBES_PER_PAGE
,
422 PAGES_FOR_IO
, nr_free_pages());
424 /* This is needed because of the fixed size of swsusp_info */
425 if (MAX_PBES
< (nr_pages
+ PBES_PER_PAGE
- 1) / PBES_PER_PAGE
)
428 if (!enough_free_mem(nr_pages
)) {
429 printk(KERN_ERR
"swsusp: Not enough free memory\n");
433 if (!enough_swap(nr_pages
)) {
434 printk(KERN_ERR
"swsusp: Not enough free swap\n");
438 pagedir_nosave
= swsusp_alloc(nr_pages
);
442 /* During allocating of suspend pagedir, new cold pages may appear.
446 copy_data_pages(pagedir_nosave
);
449 * End of critical section. From now on, we can write to memory,
450 * but we should not touch disk. This specially means we must _not_
451 * touch swap space! Except we must write out our image of course.
454 nr_copy_pages
= nr_pages
;
456 printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages
);
461 asmlinkage
int swsusp_save(void)
463 return suspend_prepare_image();