blob: 152d56cdf017310adb666bd08491905f8517474f [file] [log] [blame]
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001/*
Pavel Machek96bc7ae2005-10-30 14:59:58 -08002 * linux/kernel/power/snapshot.c
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08003 *
Pavel Machek96bc7ae2005-10-30 14:59:58 -08004 * This file provide system snapshot/restore functionality.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08005 *
6 * Copyright (C) 1998-2005 Pavel Machek <pavel@suse.cz>
7 *
8 * This file is released under the GPLv2, and is based on swsusp.c.
9 *
10 */
11
12
13#include <linux/module.h>
14#include <linux/mm.h>
15#include <linux/suspend.h>
16#include <linux/smp_lock.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080017#include <linux/delay.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080018#include <linux/bitops.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080019#include <linux/spinlock.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080020#include <linux/kernel.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080021#include <linux/pm.h>
22#include <linux/device.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080023#include <linux/bootmem.h>
24#include <linux/syscalls.h>
25#include <linux/console.h>
26#include <linux/highmem.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080027
28#include <asm/uaccess.h>
29#include <asm/mmu_context.h>
30#include <asm/pgtable.h>
31#include <asm/tlbflush.h>
32#include <asm/io.h>
33
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080034#include "power.h"
35
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -080036struct pbe *pagedir_nosave;
37unsigned int nr_copy_pages;
38
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080039#ifdef CONFIG_HIGHMEM
40struct highmem_page {
41 char *data;
42 struct page *page;
43 struct highmem_page *next;
44};
45
46static struct highmem_page *highmem_copy;
47
48static int save_highmem_zone(struct zone *zone)
49{
50 unsigned long zone_pfn;
51 mark_free_pages(zone);
52 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
53 struct page *page;
54 struct highmem_page *save;
55 void *kaddr;
56 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
57
58 if (!(pfn%1000))
59 printk(".");
60 if (!pfn_valid(pfn))
61 continue;
62 page = pfn_to_page(pfn);
63 /*
64 * This condition results from rvmalloc() sans vmalloc_32()
65 * and architectural memory reservations. This should be
66 * corrected eventually when the cases giving rise to this
67 * are better understood.
68 */
69 if (PageReserved(page)) {
70 printk("highmem reserved page?!\n");
71 continue;
72 }
73 BUG_ON(PageNosave(page));
74 if (PageNosaveFree(page))
75 continue;
76 save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
77 if (!save)
78 return -ENOMEM;
79 save->next = highmem_copy;
80 save->page = page;
81 save->data = (void *) get_zeroed_page(GFP_ATOMIC);
82 if (!save->data) {
83 kfree(save);
84 return -ENOMEM;
85 }
86 kaddr = kmap_atomic(page, KM_USER0);
87 memcpy(save->data, kaddr, PAGE_SIZE);
88 kunmap_atomic(kaddr, KM_USER0);
89 highmem_copy = save;
90 }
91 return 0;
92}
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080093
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -080094int save_highmem(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080095{
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080096 struct zone *zone;
97 int res = 0;
98
99 pr_debug("swsusp: Saving Highmem\n");
100 for_each_zone (zone) {
101 if (is_highmem(zone))
102 res = save_highmem_zone(zone);
103 if (res)
104 return res;
105 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800106 return 0;
107}
108
109int restore_highmem(void)
110{
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800111 printk("swsusp: Restoring Highmem\n");
112 while (highmem_copy) {
113 struct highmem_page *save = highmem_copy;
114 void *kaddr;
115 highmem_copy = save->next;
116
117 kaddr = kmap_atomic(save->page, KM_USER0);
118 memcpy(kaddr, save->data, PAGE_SIZE);
119 kunmap_atomic(kaddr, KM_USER0);
120 free_page((long) save->data);
121 kfree(save);
122 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800123 return 0;
124}
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800125#endif
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800126
127static int pfn_is_nosave(unsigned long pfn)
128{
129 unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
130 unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
131 return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
132}
133
134/**
135 * saveable - Determine whether a page should be cloned or not.
136 * @pfn: The page
137 *
138 * We save a page if it's Reserved, and not in the range of pages
139 * statically defined as 'unsaveable', or if it isn't reserved, and
140 * isn't part of a free chunk of pages.
141 */
142
Pavel Machekde491862005-10-30 14:59:59 -0800143static int saveable(struct zone *zone, unsigned long *zone_pfn)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800144{
145 unsigned long pfn = *zone_pfn + zone->zone_start_pfn;
Pavel Machekde491862005-10-30 14:59:59 -0800146 struct page *page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800147
148 if (!pfn_valid(pfn))
149 return 0;
150
151 page = pfn_to_page(pfn);
152 BUG_ON(PageReserved(page) && PageNosave(page));
153 if (PageNosave(page))
154 return 0;
155 if (PageReserved(page) && pfn_is_nosave(pfn)) {
156 pr_debug("[nosave pfn 0x%lx]", pfn);
157 return 0;
158 }
159 if (PageNosaveFree(page))
160 return 0;
161
162 return 1;
163}
164
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800165static unsigned count_data_pages(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800166{
167 struct zone *zone;
168 unsigned long zone_pfn;
Pavel Machekdc19d502005-11-07 00:58:40 -0800169 unsigned int n = 0;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800170
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800171 for_each_zone (zone) {
172 if (is_highmem(zone))
173 continue;
174 mark_free_pages(zone);
175 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800176 n += saveable(zone, &zone_pfn);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800177 }
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800178 return n;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800179}
180
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800181static void copy_data_pages(struct pbe *pblist)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800182{
183 struct zone *zone;
184 unsigned long zone_pfn;
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800185 struct pbe *pbe, *p;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800186
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800187 pbe = pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800188 for_each_zone (zone) {
189 if (is_highmem(zone))
190 continue;
191 mark_free_pages(zone);
192 /* This is necessary for swsusp_free() */
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800193 for_each_pb_page (p, pblist)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800194 SetPageNosaveFree(virt_to_page(p));
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800195 for_each_pbe (p, pblist)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800196 SetPageNosaveFree(virt_to_page(p->address));
197 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
198 if (saveable(zone, &zone_pfn)) {
Pavel Machekde491862005-10-30 14:59:59 -0800199 struct page *page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800200 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
201 BUG_ON(!pbe);
202 pbe->orig_address = (unsigned long)page_address(page);
203 /* copy_page is not usable for copying task structs. */
204 memcpy((void *)pbe->address, (void *)pbe->orig_address, PAGE_SIZE);
205 pbe = pbe->next;
206 }
207 }
208 }
209 BUG_ON(pbe);
210}
211
212
213/**
214 * free_pagedir - free pages allocated with alloc_pagedir()
215 */
216
Rafael J. Wysockied14b522005-11-08 21:34:40 -0800217void free_pagedir(struct pbe *pblist)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800218{
219 struct pbe *pbe;
220
221 while (pblist) {
222 pbe = (pblist + PB_PAGE_SKIP)->next;
223 ClearPageNosave(virt_to_page(pblist));
224 ClearPageNosaveFree(virt_to_page(pblist));
225 free_page((unsigned long)pblist);
226 pblist = pbe;
227 }
228}
229
230/**
231 * fill_pb_page - Create a list of PBEs on a given memory page
232 */
233
234static inline void fill_pb_page(struct pbe *pbpage)
235{
236 struct pbe *p;
237
238 p = pbpage;
239 pbpage += PB_PAGE_SKIP;
240 do
241 p->next = p + 1;
242 while (++p < pbpage);
243}
244
245/**
246 * create_pbe_list - Create a list of PBEs on top of a given chain
247 * of memory pages allocated with alloc_pagedir()
248 */
249
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800250static inline void create_pbe_list(struct pbe *pblist, unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800251{
252 struct pbe *pbpage, *p;
Pavel Machekdc19d502005-11-07 00:58:40 -0800253 unsigned int num = PBES_PER_PAGE;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800254
255 for_each_pb_page (pbpage, pblist) {
256 if (num >= nr_pages)
257 break;
258
259 fill_pb_page(pbpage);
260 num += PBES_PER_PAGE;
261 }
262 if (pbpage) {
263 for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
264 p->next = p + 1;
265 p->next = NULL;
266 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800267}
268
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800269/**
270 * @safe_needed - on resume, for storing the PBE list and the image,
271 * we can only use memory pages that do not conflict with the pages
272 * which had been used before suspend.
273 *
274 * The unsafe pages are marked with the PG_nosave_free flag
275 *
276 * Allocated but unusable (ie eaten) memory pages should be marked
277 * so that swsusp_free() can release them
278 */
279
280static inline void *alloc_image_page(gfp_t gfp_mask, int safe_needed)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800281{
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800282 void *res;
283
284 if (safe_needed)
285 do {
286 res = (void *)get_zeroed_page(gfp_mask);
287 if (res && PageNosaveFree(virt_to_page(res)))
288 /* This is for swsusp_free() */
289 SetPageNosave(virt_to_page(res));
290 } while (res && PageNosaveFree(virt_to_page(res)));
291 else
292 res = (void *)get_zeroed_page(gfp_mask);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800293 if (res) {
294 SetPageNosave(virt_to_page(res));
295 SetPageNosaveFree(virt_to_page(res));
296 }
297 return res;
298}
299
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800300unsigned long get_safe_page(gfp_t gfp_mask)
301{
302 return (unsigned long)alloc_image_page(gfp_mask, 1);
303}
304
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800305/**
306 * alloc_pagedir - Allocate the page directory.
307 *
308 * First, determine exactly how many pages we need and
309 * allocate them.
310 *
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
313 * to the next page.
314 *
315 * On each page we set up a list of struct_pbe elements.
316 */
317
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800318struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask, int safe_needed)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800319{
Pavel Machekdc19d502005-11-07 00:58:40 -0800320 unsigned int num;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800321 struct pbe *pblist, *pbe;
322
323 if (!nr_pages)
324 return NULL;
325
326 pr_debug("alloc_pagedir(): nr_pages = %d\n", nr_pages);
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800327 pblist = alloc_image_page(gfp_mask, safe_needed);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800328 /* 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) {
331 pbe += PB_PAGE_SKIP;
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800332 pbe->next = alloc_image_page(gfp_mask, safe_needed);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800333 }
334 if (!pbe) { /* get_zeroed_page() failed */
335 free_pagedir(pblist);
336 pblist = NULL;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800337 } else
338 create_pbe_list(pblist, nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800339 return pblist;
340}
341
342/**
343 * Free pages we allocated for suspend. Suspend pages are alocated
344 * before atomic copy, so we need to free them after resume.
345 */
346
347void swsusp_free(void)
348{
349 struct zone *zone;
350 unsigned long zone_pfn;
351
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)) {
Pavel Machekdc19d502005-11-07 00:58:40 -0800355 struct page *page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800356 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));
361 }
362 }
363 }
364}
365
366
367/**
368 * enough_free_mem - Make sure we enough free memory to snapshot.
369 *
370 * Returns TRUE or FALSE after checking the number of available
371 * free pages.
372 */
373
Pavel Machekdc19d502005-11-07 00:58:40 -0800374static int enough_free_mem(unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800375{
376 pr_debug("swsusp: available memory: %u pages\n", nr_free_pages());
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800377 return nr_free_pages() > (nr_pages + PAGES_FOR_IO +
378 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800379}
380
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800381int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed)
382{
383 struct pbe *p;
384
385 for_each_pbe (p, pblist) {
386 p->address = (unsigned long)alloc_image_page(gfp_mask, safe_needed);
387 if (!p->address)
388 return -ENOMEM;
389 }
390 return 0;
391}
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800392
Pavel Machekdc19d502005-11-07 00:58:40 -0800393static struct pbe *swsusp_alloc(unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800394{
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800395 struct pbe *pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800396
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800397 if (!(pblist = alloc_pagedir(nr_pages, GFP_ATOMIC | __GFP_COLD, 0))) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800398 printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800399 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800400 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800401
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800402 if (alloc_data_pages(pblist, GFP_ATOMIC | __GFP_COLD, 0)) {
403 printk(KERN_ERR "suspend: Allocating image pages failed.\n");
404 swsusp_free();
405 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800406 }
407
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800408 return pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800409}
410
Rafael J. Wysocki2e32a432005-10-30 15:00:00 -0800411asmlinkage int swsusp_save(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800412{
Pavel Machekdc19d502005-11-07 00:58:40 -0800413 unsigned int nr_pages;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800414
415 pr_debug("swsusp: critical section: \n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800416
417 drain_local_pages();
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800418 nr_pages = count_data_pages();
419 printk("swsusp: Need to copy %u pages\n", nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800420
421 pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800422 nr_pages,
423 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE,
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800424 PAGES_FOR_IO, nr_free_pages());
425
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800426 if (!enough_free_mem(nr_pages)) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800427 printk(KERN_ERR "swsusp: Not enough free memory\n");
428 return -ENOMEM;
429 }
430
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800431 pagedir_nosave = swsusp_alloc(nr_pages);
432 if (!pagedir_nosave)
433 return -ENOMEM;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800434
435 /* During allocating of suspend pagedir, new cold pages may appear.
436 * Kill them.
437 */
438 drain_local_pages();
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800439 copy_data_pages(pagedir_nosave);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800440
441 /*
442 * End of critical section. From now on, we can write to memory,
443 * but we should not touch disk. This specially means we must _not_
444 * touch swap space! Except we must write out our image of course.
445 */
446
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800447 nr_copy_pages = nr_pages;
448
449 printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800450 return 0;
451}