Linux Audio

Check our new training course

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