suspend.c 6.4 KB

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