s390/hibernate: add early resume function
[deliverable/linux.git] / arch / s390 / kernel / suspend.c
1 /*
2 * Suspend support specific for s390.
3 *
4 * Copyright IBM Corp. 2009
5 *
6 * Author(s): Hans-Joachim Picht <hans@linux.vnet.ibm.com>
7 */
8
9 #include <linux/pfn.h>
10 #include <linux/suspend.h>
11 #include <linux/mm.h>
12 #include <asm/ctl_reg.h>
13 #include <asm/ipl.h>
14 #include <asm/cio.h>
15
16 /*
17 * References to section boundaries
18 */
19 extern const void __nosave_begin, __nosave_end;
20
21 /*
22 * The restore of the saved pages in an hibernation image will set
23 * the change and referenced bits in the storage key for each page.
24 * Overindication of the referenced bits after an hibernation cycle
25 * does not cause any harm but the overindication of the change bits
26 * would cause trouble.
27 * Use the ARCH_SAVE_PAGE_KEYS hooks to save the storage key of each
28 * page to the most significant byte of the associated page frame
29 * number in the hibernation image.
30 */
31
32 /*
33 * Key storage is allocated as a linked list of pages.
34 * The size of the keys array is (PAGE_SIZE - sizeof(long))
35 */
36 struct page_key_data {
37 struct page_key_data *next;
38 unsigned char data[];
39 };
40
41 #define PAGE_KEY_DATA_SIZE (PAGE_SIZE - sizeof(struct page_key_data *))
42
43 static struct page_key_data *page_key_data;
44 static struct page_key_data *page_key_rp, *page_key_wp;
45 static unsigned long page_key_rx, page_key_wx;
46 unsigned long suspend_zero_pages;
47
48 /*
49 * For each page in the hibernation image one additional byte is
50 * stored in the most significant byte of the page frame number.
51 * On suspend no additional memory is required but on resume the
52 * keys need to be memorized until the page data has been restored.
53 * Only then can the storage keys be set to their old state.
54 */
55 unsigned long page_key_additional_pages(unsigned long pages)
56 {
57 return DIV_ROUND_UP(pages, PAGE_KEY_DATA_SIZE);
58 }
59
60 /*
61 * Free page_key_data list of arrays.
62 */
63 void page_key_free(void)
64 {
65 struct page_key_data *pkd;
66
67 while (page_key_data) {
68 pkd = page_key_data;
69 page_key_data = pkd->next;
70 free_page((unsigned long) pkd);
71 }
72 }
73
74 /*
75 * Allocate page_key_data list of arrays with enough room to store
76 * one byte for each page in the hibernation image.
77 */
78 int page_key_alloc(unsigned long pages)
79 {
80 struct page_key_data *pk;
81 unsigned long size;
82
83 size = DIV_ROUND_UP(pages, PAGE_KEY_DATA_SIZE);
84 while (size--) {
85 pk = (struct page_key_data *) get_zeroed_page(GFP_KERNEL);
86 if (!pk) {
87 page_key_free();
88 return -ENOMEM;
89 }
90 pk->next = page_key_data;
91 page_key_data = pk;
92 }
93 page_key_rp = page_key_wp = page_key_data;
94 page_key_rx = page_key_wx = 0;
95 return 0;
96 }
97
98 /*
99 * Save the storage key into the upper 8 bits of the page frame number.
100 */
101 void page_key_read(unsigned long *pfn)
102 {
103 unsigned long addr;
104
105 addr = (unsigned long) page_address(pfn_to_page(*pfn));
106 *(unsigned char *) pfn = (unsigned char) page_get_storage_key(addr);
107 }
108
109 /*
110 * Extract the storage key from the upper 8 bits of the page frame number
111 * and store it in the page_key_data list of arrays.
112 */
113 void page_key_memorize(unsigned long *pfn)
114 {
115 page_key_wp->data[page_key_wx] = *(unsigned char *) pfn;
116 *(unsigned char *) pfn = 0;
117 if (++page_key_wx < PAGE_KEY_DATA_SIZE)
118 return;
119 page_key_wp = page_key_wp->next;
120 page_key_wx = 0;
121 }
122
123 /*
124 * Get the next key from the page_key_data list of arrays and set the
125 * storage key of the page referred by @address. If @address refers to
126 * a "safe" page the swsusp_arch_resume code will transfer the storage
127 * key from the buffer page to the original page.
128 */
129 void page_key_write(void *address)
130 {
131 page_set_storage_key((unsigned long) address,
132 page_key_rp->data[page_key_rx], 0);
133 if (++page_key_rx >= PAGE_KEY_DATA_SIZE)
134 return;
135 page_key_rp = page_key_rp->next;
136 page_key_rx = 0;
137 }
138
139 int pfn_is_nosave(unsigned long pfn)
140 {
141 unsigned long nosave_begin_pfn = PFN_DOWN(__pa(&__nosave_begin));
142 unsigned long nosave_end_pfn = PFN_DOWN(__pa(&__nosave_end));
143
144 /* Always save lowcore pages (LC protection might be enabled). */
145 if (pfn <= LC_PAGES)
146 return 0;
147 if (pfn >= nosave_begin_pfn && pfn < nosave_end_pfn)
148 return 1;
149 /* Skip memory holes and read-only pages (NSS, DCSS, ...). */
150 if (tprot(PFN_PHYS(pfn)))
151 return 1;
152 return 0;
153 }
154
155 /*
156 * PM notifier callback for suspend
157 */
158 static int suspend_pm_cb(struct notifier_block *nb, unsigned long action,
159 void *ptr)
160 {
161 switch (action) {
162 case PM_SUSPEND_PREPARE:
163 case PM_HIBERNATION_PREPARE:
164 suspend_zero_pages = __get_free_pages(GFP_KERNEL, LC_ORDER);
165 if (!suspend_zero_pages)
166 return NOTIFY_BAD;
167 break;
168 case PM_POST_SUSPEND:
169 case PM_POST_HIBERNATION:
170 free_pages(suspend_zero_pages, LC_ORDER);
171 break;
172 default:
173 return NOTIFY_DONE;
174 }
175 return NOTIFY_OK;
176 }
177
178 static int __init suspend_pm_init(void)
179 {
180 pm_notifier(suspend_pm_cb, 0);
181 return 0;
182 }
183 arch_initcall(suspend_pm_init);
184
185 void save_processor_state(void)
186 {
187 /* swsusp_arch_suspend() actually saves all cpu register contents.
188 * Machine checks must be disabled since swsusp_arch_suspend() stores
189 * register contents to their lowcore save areas. That's the same
190 * place where register contents on machine checks would be saved.
191 * To avoid register corruption disable machine checks.
192 * We must also disable machine checks in the new psw mask for
193 * program checks, since swsusp_arch_suspend() may generate program
194 * checks. Disabling machine checks for all other new psw masks is
195 * just paranoia.
196 */
197 local_mcck_disable();
198 /* Disable lowcore protection */
199 __ctl_clear_bit(0,28);
200 S390_lowcore.external_new_psw.mask &= ~PSW_MASK_MCHECK;
201 S390_lowcore.svc_new_psw.mask &= ~PSW_MASK_MCHECK;
202 S390_lowcore.io_new_psw.mask &= ~PSW_MASK_MCHECK;
203 S390_lowcore.program_new_psw.mask &= ~PSW_MASK_MCHECK;
204 }
205
206 void restore_processor_state(void)
207 {
208 S390_lowcore.external_new_psw.mask |= PSW_MASK_MCHECK;
209 S390_lowcore.svc_new_psw.mask |= PSW_MASK_MCHECK;
210 S390_lowcore.io_new_psw.mask |= PSW_MASK_MCHECK;
211 S390_lowcore.program_new_psw.mask |= PSW_MASK_MCHECK;
212 /* Enable lowcore protection */
213 __ctl_set_bit(0,28);
214 local_mcck_enable();
215 }
216
217 /* Called at the end of swsusp_arch_resume */
218 void s390_early_resume(void)
219 {
220 lgr_info_log();
221 channel_subsystem_reinit();
222 }
This page took 0.035277 seconds and 5 git commands to generate.