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