pandora: defconfig: update
[pandora-kernel.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/ipl.h>
13 #include <asm/sections.h>
14 #include <asm/system.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
47 /*
48  * For each page in the hibernation image one additional byte is
49  * stored in the most significant byte of the page frame number.
50  * On suspend no additional memory is required but on resume the
51  * keys need to be memorized until the page data has been restored.
52  * Only then can the storage keys be set to their old state.
53  */
54 unsigned long page_key_additional_pages(unsigned long pages)
55 {
56         return DIV_ROUND_UP(pages, PAGE_KEY_DATA_SIZE);
57 }
58
59 /*
60  * Free page_key_data list of arrays.
61  */
62 void page_key_free(void)
63 {
64         struct page_key_data *pkd;
65
66         while (page_key_data) {
67                 pkd = page_key_data;
68                 page_key_data = pkd->next;
69                 free_page((unsigned long) pkd);
70         }
71 }
72
73 /*
74  * Allocate page_key_data list of arrays with enough room to store
75  * one byte for each page in the hibernation image.
76  */
77 int page_key_alloc(unsigned long pages)
78 {
79         struct page_key_data *pk;
80         unsigned long size;
81
82         size = DIV_ROUND_UP(pages, PAGE_KEY_DATA_SIZE);
83         while (size--) {
84                 pk = (struct page_key_data *) get_zeroed_page(GFP_KERNEL);
85                 if (!pk) {
86                         page_key_free();
87                         return -ENOMEM;
88                 }
89                 pk->next = page_key_data;
90                 page_key_data = pk;
91         }
92         page_key_rp = page_key_wp = page_key_data;
93         page_key_rx = page_key_wx = 0;
94         return 0;
95 }
96
97 /*
98  * Save the storage key into the upper 8 bits of the page frame number.
99  */
100 void page_key_read(unsigned long *pfn)
101 {
102         unsigned long addr;
103
104         addr = (unsigned long) page_address(pfn_to_page(*pfn));
105         *(unsigned char *) pfn = (unsigned char) page_get_storage_key(addr);
106 }
107
108 /*
109  * Extract the storage key from the upper 8 bits of the page frame number
110  * and store it in the page_key_data list of arrays.
111  */
112 void page_key_memorize(unsigned long *pfn)
113 {
114         page_key_wp->data[page_key_wx] = *(unsigned char *) pfn;
115         *(unsigned char *) pfn = 0;
116         if (++page_key_wx < PAGE_KEY_DATA_SIZE)
117                 return;
118         page_key_wp = page_key_wp->next;
119         page_key_wx = 0;
120 }
121
122 /*
123  * Get the next key from the page_key_data list of arrays and set the
124  * storage key of the page referred by @address. If @address refers to
125  * a "safe" page the swsusp_arch_resume code will transfer the storage
126  * key from the buffer page to the original page.
127  */
128 void page_key_write(void *address)
129 {
130         page_set_storage_key((unsigned long) address,
131                              page_key_rp->data[page_key_rx], 0);
132         if (++page_key_rx >= PAGE_KEY_DATA_SIZE)
133                 return;
134         page_key_rp = page_key_rp->next;
135         page_key_rx = 0;
136 }
137
138 int pfn_is_nosave(unsigned long pfn)
139 {
140         unsigned long nosave_begin_pfn = PFN_DOWN(__pa(&__nosave_begin));
141         unsigned long nosave_end_pfn = PFN_DOWN(__pa(&__nosave_end));
142         unsigned long eshared_pfn = PFN_DOWN(__pa(&_eshared)) - 1;
143         unsigned long stext_pfn = PFN_DOWN(__pa(&_stext));
144
145         /* Always save lowcore pages (LC protection might be enabled). */
146         if (pfn <= LC_PAGES)
147                 return 0;
148         if (pfn >= nosave_begin_pfn && pfn < nosave_end_pfn)
149                 return 1;
150         /* Skip memory holes and read-only pages (NSS, DCSS, ...). */
151         if (pfn >= stext_pfn && pfn <= eshared_pfn)
152                 return ipl_info.type == IPL_TYPE_NSS ? 1 : 0;
153         if (tprot(PFN_PHYS(pfn)))
154                 return 1;
155         return 0;
156 }
157
158 void save_processor_state(void)
159 {
160         /* swsusp_arch_suspend() actually saves all cpu register contents.
161          * Machine checks must be disabled since swsusp_arch_suspend() stores
162          * register contents to their lowcore save areas. That's the same
163          * place where register contents on machine checks would be saved.
164          * To avoid register corruption disable machine checks.
165          * We must also disable machine checks in the new psw mask for
166          * program checks, since swsusp_arch_suspend() may generate program
167          * checks. Disabling machine checks for all other new psw masks is
168          * just paranoia.
169          */
170         local_mcck_disable();
171         /* Disable lowcore protection */
172         __ctl_clear_bit(0,28);
173         S390_lowcore.external_new_psw.mask &= ~PSW_MASK_MCHECK;
174         S390_lowcore.svc_new_psw.mask &= ~PSW_MASK_MCHECK;
175         S390_lowcore.io_new_psw.mask &= ~PSW_MASK_MCHECK;
176         S390_lowcore.program_new_psw.mask &= ~PSW_MASK_MCHECK;
177 }
178
179 void restore_processor_state(void)
180 {
181         S390_lowcore.external_new_psw.mask |= PSW_MASK_MCHECK;
182         S390_lowcore.svc_new_psw.mask |= PSW_MASK_MCHECK;
183         S390_lowcore.io_new_psw.mask |= PSW_MASK_MCHECK;
184         S390_lowcore.program_new_psw.mask |= PSW_MASK_MCHECK;
185         /* Enable lowcore protection */
186         __ctl_set_bit(0,28);
187         local_mcck_enable();
188 }