suspend.c 6.1 KB

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