[PATCH] swsusp: remove unneccessary includes
[safe/jmp/linux-2.6] / kernel / power / snapshot.c
1 /*
2  * linux/kernel/power/snapshot.c
3  *
4  * This file provide system snapshot/restore functionality.
5  *
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>
17 #include <linux/delay.h>
18 #include <linux/bitops.h>
19 #include <linux/spinlock.h>
20 #include <linux/kernel.h>
21 #include <linux/pm.h>
22 #include <linux/device.h>
23 #include <linux/bootmem.h>
24 #include <linux/syscalls.h>
25 #include <linux/console.h>
26 #include <linux/highmem.h>
27
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
34 #include "power.h"
35
36
37 #ifdef CONFIG_HIGHMEM
38 struct highmem_page {
39         char *data;
40         struct page *page;
41         struct highmem_page *next;
42 };
43
44 static struct highmem_page *highmem_copy;
45
46 static int save_highmem_zone(struct zone *zone)
47 {
48         unsigned long zone_pfn;
49         mark_free_pages(zone);
50         for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
51                 struct page *page;
52                 struct highmem_page *save;
53                 void *kaddr;
54                 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
55
56                 if (!(pfn%1000))
57                         printk(".");
58                 if (!pfn_valid(pfn))
59                         continue;
60                 page = pfn_to_page(pfn);
61                 /*
62                  * This condition results from rvmalloc() sans vmalloc_32()
63                  * and architectural memory reservations. This should be
64                  * corrected eventually when the cases giving rise to this
65                  * are better understood.
66                  */
67                 if (PageReserved(page)) {
68                         printk("highmem reserved page?!\n");
69                         continue;
70                 }
71                 BUG_ON(PageNosave(page));
72                 if (PageNosaveFree(page))
73                         continue;
74                 save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
75                 if (!save)
76                         return -ENOMEM;
77                 save->next = highmem_copy;
78                 save->page = page;
79                 save->data = (void *) get_zeroed_page(GFP_ATOMIC);
80                 if (!save->data) {
81                         kfree(save);
82                         return -ENOMEM;
83                 }
84                 kaddr = kmap_atomic(page, KM_USER0);
85                 memcpy(save->data, kaddr, PAGE_SIZE);
86                 kunmap_atomic(kaddr, KM_USER0);
87                 highmem_copy = save;
88         }
89         return 0;
90 }
91 #endif /* CONFIG_HIGHMEM */
92
93
94 static int save_highmem(void)
95 {
96 #ifdef CONFIG_HIGHMEM
97         struct zone *zone;
98         int res = 0;
99
100         pr_debug("swsusp: Saving Highmem\n");
101         for_each_zone (zone) {
102                 if (is_highmem(zone))
103                         res = save_highmem_zone(zone);
104                 if (res)
105                         return res;
106         }
107 #endif
108         return 0;
109 }
110
111 int restore_highmem(void)
112 {
113 #ifdef CONFIG_HIGHMEM
114         printk("swsusp: Restoring Highmem\n");
115         while (highmem_copy) {
116                 struct highmem_page *save = highmem_copy;
117                 void *kaddr;
118                 highmem_copy = save->next;
119
120                 kaddr = kmap_atomic(save->page, KM_USER0);
121                 memcpy(kaddr, save->data, PAGE_SIZE);
122                 kunmap_atomic(kaddr, KM_USER0);
123                 free_page((long) save->data);
124                 kfree(save);
125         }
126 #endif
127         return 0;
128 }
129
130
131 static int pfn_is_nosave(unsigned long pfn)
132 {
133         unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
134         unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
135         return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
136 }
137
138 /**
139  *      saveable - Determine whether a page should be cloned or not.
140  *      @pfn:   The page
141  *
142  *      We save a page if it's Reserved, and not in the range of pages
143  *      statically defined as 'unsaveable', or if it isn't reserved, and
144  *      isn't part of a free chunk of pages.
145  */
146
147 static int saveable(struct zone * zone, unsigned long * zone_pfn)
148 {
149         unsigned long pfn = *zone_pfn + zone->zone_start_pfn;
150         struct page * page;
151
152         if (!pfn_valid(pfn))
153                 return 0;
154
155         page = pfn_to_page(pfn);
156         BUG_ON(PageReserved(page) && PageNosave(page));
157         if (PageNosave(page))
158                 return 0;
159         if (PageReserved(page) && pfn_is_nosave(pfn)) {
160                 pr_debug("[nosave pfn 0x%lx]", pfn);
161                 return 0;
162         }
163         if (PageNosaveFree(page))
164                 return 0;
165
166         return 1;
167 }
168
169 static unsigned count_data_pages(void)
170 {
171         struct zone *zone;
172         unsigned long zone_pfn;
173         unsigned n;
174
175         n = 0;
176         for_each_zone (zone) {
177                 if (is_highmem(zone))
178                         continue;
179                 mark_free_pages(zone);
180                 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
181                         n += saveable(zone, &zone_pfn);
182         }
183         return n;
184 }
185
186 static void copy_data_pages(struct pbe *pblist)
187 {
188         struct zone *zone;
189         unsigned long zone_pfn;
190         struct pbe *pbe, *p;
191
192         pbe = pblist;
193         for_each_zone (zone) {
194                 if (is_highmem(zone))
195                         continue;
196                 mark_free_pages(zone);
197                 /* This is necessary for swsusp_free() */
198                 for_each_pb_page (p, pblist)
199                         SetPageNosaveFree(virt_to_page(p));
200                 for_each_pbe (p, pblist)
201                         SetPageNosaveFree(virt_to_page(p->address));
202                 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
203                         if (saveable(zone, &zone_pfn)) {
204                                 struct page * page;
205                                 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
206                                 BUG_ON(!pbe);
207                                 pbe->orig_address = (unsigned long)page_address(page);
208                                 /* copy_page is not usable for copying task structs. */
209                                 memcpy((void *)pbe->address, (void *)pbe->orig_address, PAGE_SIZE);
210                                 pbe = pbe->next;
211                         }
212                 }
213         }
214         BUG_ON(pbe);
215 }
216
217
218 /**
219  *      free_pagedir - free pages allocated with alloc_pagedir()
220  */
221
222 static void free_pagedir(struct pbe *pblist)
223 {
224         struct pbe *pbe;
225
226         while (pblist) {
227                 pbe = (pblist + PB_PAGE_SKIP)->next;
228                 ClearPageNosave(virt_to_page(pblist));
229                 ClearPageNosaveFree(virt_to_page(pblist));
230                 free_page((unsigned long)pblist);
231                 pblist = pbe;
232         }
233 }
234
235 /**
236  *      fill_pb_page - Create a list of PBEs on a given memory page
237  */
238
239 static inline void fill_pb_page(struct pbe *pbpage)
240 {
241         struct pbe *p;
242
243         p = pbpage;
244         pbpage += PB_PAGE_SKIP;
245         do
246                 p->next = p + 1;
247         while (++p < pbpage);
248 }
249
250 /**
251  *      create_pbe_list - Create a list of PBEs on top of a given chain
252  *      of memory pages allocated with alloc_pagedir()
253  */
254
255 void create_pbe_list(struct pbe *pblist, unsigned nr_pages)
256 {
257         struct pbe *pbpage, *p;
258         unsigned num = PBES_PER_PAGE;
259
260         for_each_pb_page (pbpage, pblist) {
261                 if (num >= nr_pages)
262                         break;
263
264                 fill_pb_page(pbpage);
265                 num += PBES_PER_PAGE;
266         }
267         if (pbpage) {
268                 for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
269                         p->next = p + 1;
270                 p->next = NULL;
271         }
272         pr_debug("create_pbe_list(): initialized %d PBEs\n", num);
273 }
274
275 static void *alloc_image_page(void)
276 {
277         void *res = (void *)get_zeroed_page(GFP_ATOMIC | __GFP_COLD);
278         if (res) {
279                 SetPageNosave(virt_to_page(res));
280                 SetPageNosaveFree(virt_to_page(res));
281         }
282         return res;
283 }
284
285 /**
286  *      alloc_pagedir - Allocate the page directory.
287  *
288  *      First, determine exactly how many pages we need and
289  *      allocate them.
290  *
291  *      We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
292  *      struct pbe elements (pbes) and the last element in the page points
293  *      to the next page.
294  *
295  *      On each page we set up a list of struct_pbe elements.
296  */
297
298 struct pbe * alloc_pagedir(unsigned nr_pages)
299 {
300         unsigned num;
301         struct pbe *pblist, *pbe;
302
303         if (!nr_pages)
304                 return NULL;
305
306         pr_debug("alloc_pagedir(): nr_pages = %d\n", nr_pages);
307         pblist = (struct pbe *)alloc_image_page();
308         /* FIXME: rewrite this ugly loop */
309         for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
310                         pbe = pbe->next, num += PBES_PER_PAGE) {
311                 pbe += PB_PAGE_SKIP;
312                 pbe->next = (struct pbe *)alloc_image_page();
313         }
314         if (!pbe) { /* get_zeroed_page() failed */
315                 free_pagedir(pblist);
316                 pblist = NULL;
317         }
318         return pblist;
319 }
320
321 /**
322  * Free pages we allocated for suspend. Suspend pages are alocated
323  * before atomic copy, so we need to free them after resume.
324  */
325
326 void swsusp_free(void)
327 {
328         struct zone *zone;
329         unsigned long zone_pfn;
330
331         for_each_zone(zone) {
332                 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
333                         if (pfn_valid(zone_pfn + zone->zone_start_pfn)) {
334                                 struct page * page;
335                                 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
336                                 if (PageNosave(page) && PageNosaveFree(page)) {
337                                         ClearPageNosave(page);
338                                         ClearPageNosaveFree(page);
339                                         free_page((long) page_address(page));
340                                 }
341                         }
342         }
343 }
344
345
346 /**
347  *      enough_free_mem - Make sure we enough free memory to snapshot.
348  *
349  *      Returns TRUE or FALSE after checking the number of available
350  *      free pages.
351  */
352
353 static int enough_free_mem(unsigned nr_pages)
354 {
355         pr_debug("swsusp: available memory: %u pages\n", nr_free_pages());
356         return nr_free_pages() > (nr_pages + PAGES_FOR_IO +
357                 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
358 }
359
360
361 static struct pbe *swsusp_alloc(unsigned nr_pages)
362 {
363         struct pbe *pblist, *p;
364
365         if (!(pblist = alloc_pagedir(nr_pages))) {
366                 printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
367                 return NULL;
368         }
369         create_pbe_list(pblist, nr_pages);
370
371         for_each_pbe (p, pblist) {
372                 p->address = (unsigned long)alloc_image_page();
373                 if (!p->address) {
374                         printk(KERN_ERR "suspend: Allocating image pages failed.\n");
375                         swsusp_free();
376                         return NULL;
377                 }
378         }
379
380         return pblist;
381 }
382
383 static int suspend_prepare_image(void)
384 {
385         unsigned nr_pages;
386
387         pr_debug("swsusp: critical section: \n");
388         if (save_highmem()) {
389                 printk(KERN_CRIT "swsusp: Not enough free pages for highmem\n");
390                 restore_highmem();
391                 return -ENOMEM;
392         }
393
394         drain_local_pages();
395         nr_pages = count_data_pages();
396         printk("swsusp: Need to copy %u pages\n", nr_pages);
397
398         pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
399                  nr_pages,
400                  (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE,
401                  PAGES_FOR_IO, nr_free_pages());
402
403         /* This is needed because of the fixed size of swsusp_info */
404         if (MAX_PBES < (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE)
405                 return -ENOSPC;
406
407         if (!enough_free_mem(nr_pages)) {
408                 printk(KERN_ERR "swsusp: Not enough free memory\n");
409                 return -ENOMEM;
410         }
411
412         if (!enough_swap(nr_pages)) {
413                 printk(KERN_ERR "swsusp: Not enough free swap\n");
414                 return -ENOSPC;
415         }
416
417         pagedir_nosave = swsusp_alloc(nr_pages);
418         if (!pagedir_nosave)
419                 return -ENOMEM;
420
421         /* During allocating of suspend pagedir, new cold pages may appear.
422          * Kill them.
423          */
424         drain_local_pages();
425         copy_data_pages(pagedir_nosave);
426
427         /*
428          * End of critical section. From now on, we can write to memory,
429          * but we should not touch disk. This specially means we must _not_
430          * touch swap space! Except we must write out our image of course.
431          */
432
433         nr_copy_pages = nr_pages;
434
435         printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
436         return 0;
437 }
438
439
440 asmlinkage int swsusp_save(void)
441 {
442         return suspend_prepare_image();
443 }