uwb: Use kcalloc instead of kzalloc to allocate array
[zen-stable.git] / arch / x86 / power / hibernate_64.c
blob460f314d13e54fff4be0c3cd3d9a126a2973d0c7
1 /*
2 * Hibernation support for x86-64
4 * Distribute under GPLv2
6 * Copyright (c) 2007 Rafael J. Wysocki <rjw@sisk.pl>
7 * Copyright (c) 2002 Pavel Machek <pavel@ucw.cz>
8 * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org>
9 */
11 #include <linux/gfp.h>
12 #include <linux/smp.h>
13 #include <linux/suspend.h>
14 #include <asm/proto.h>
15 #include <asm/page.h>
16 #include <asm/pgtable.h>
17 #include <asm/mtrr.h>
18 #include <asm/suspend.h>
20 /* References to section boundaries */
21 extern const void __nosave_begin, __nosave_end;
23 /* Defined in hibernate_asm_64.S */
24 extern int restore_image(void);
27 * Address to jump to in the last phase of restore in order to get to the image
28 * kernel's text (this value is passed in the image header).
30 unsigned long restore_jump_address;
33 * Value of the cr3 register from before the hibernation (this value is passed
34 * in the image header).
36 unsigned long restore_cr3;
38 pgd_t *temp_level4_pgt;
40 void *relocated_restore_code;
42 static int res_phys_pud_init(pud_t *pud, unsigned long address, unsigned long end)
44 long i, j;
46 i = pud_index(address);
47 pud = pud + i;
48 for (; i < PTRS_PER_PUD; pud++, i++) {
49 unsigned long paddr;
50 pmd_t *pmd;
52 paddr = address + i*PUD_SIZE;
53 if (paddr >= end)
54 break;
56 pmd = (pmd_t *)get_safe_page(GFP_ATOMIC);
57 if (!pmd)
58 return -ENOMEM;
59 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
60 for (j = 0; j < PTRS_PER_PMD; pmd++, j++, paddr += PMD_SIZE) {
61 unsigned long pe;
63 if (paddr >= end)
64 break;
65 pe = __PAGE_KERNEL_LARGE_EXEC | paddr;
66 pe &= __supported_pte_mask;
67 set_pmd(pmd, __pmd(pe));
70 return 0;
73 static int set_up_temporary_mappings(void)
75 unsigned long start, end, next;
76 int error;
78 temp_level4_pgt = (pgd_t *)get_safe_page(GFP_ATOMIC);
79 if (!temp_level4_pgt)
80 return -ENOMEM;
82 /* It is safe to reuse the original kernel mapping */
83 set_pgd(temp_level4_pgt + pgd_index(__START_KERNEL_map),
84 init_level4_pgt[pgd_index(__START_KERNEL_map)]);
86 /* Set up the direct mapping from scratch */
87 start = (unsigned long)pfn_to_kaddr(0);
88 end = (unsigned long)pfn_to_kaddr(max_pfn);
90 for (; start < end; start = next) {
91 pud_t *pud = (pud_t *)get_safe_page(GFP_ATOMIC);
92 if (!pud)
93 return -ENOMEM;
94 next = start + PGDIR_SIZE;
95 if (next > end)
96 next = end;
97 if ((error = res_phys_pud_init(pud, __pa(start), __pa(next))))
98 return error;
99 set_pgd(temp_level4_pgt + pgd_index(start),
100 mk_kernel_pgd(__pa(pud)));
102 return 0;
105 int swsusp_arch_resume(void)
107 int error;
109 /* We have got enough memory and from now on we cannot recover */
110 if ((error = set_up_temporary_mappings()))
111 return error;
113 relocated_restore_code = (void *)get_safe_page(GFP_ATOMIC);
114 if (!relocated_restore_code)
115 return -ENOMEM;
116 memcpy(relocated_restore_code, &core_restore_code,
117 &restore_registers - &core_restore_code);
119 restore_image();
120 return 0;
124 * pfn_is_nosave - check if given pfn is in the 'nosave' section
127 int pfn_is_nosave(unsigned long pfn)
129 unsigned long nosave_begin_pfn = __pa_symbol(&__nosave_begin) >> PAGE_SHIFT;
130 unsigned long nosave_end_pfn = PAGE_ALIGN(__pa_symbol(&__nosave_end)) >> PAGE_SHIFT;
131 return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
134 struct restore_data_record {
135 unsigned long jump_address;
136 unsigned long cr3;
137 unsigned long magic;
140 #define RESTORE_MAGIC 0x0123456789ABCDEFUL
143 * arch_hibernation_header_save - populate the architecture specific part
144 * of a hibernation image header
145 * @addr: address to save the data at
147 int arch_hibernation_header_save(void *addr, unsigned int max_size)
149 struct restore_data_record *rdr = addr;
151 if (max_size < sizeof(struct restore_data_record))
152 return -EOVERFLOW;
153 rdr->jump_address = restore_jump_address;
154 rdr->cr3 = restore_cr3;
155 rdr->magic = RESTORE_MAGIC;
156 return 0;
160 * arch_hibernation_header_restore - read the architecture specific data
161 * from the hibernation image header
162 * @addr: address to read the data from
164 int arch_hibernation_header_restore(void *addr)
166 struct restore_data_record *rdr = addr;
168 restore_jump_address = rdr->jump_address;
169 restore_cr3 = rdr->cr3;
170 return (rdr->magic == RESTORE_MAGIC) ? 0 : -EINVAL;