suspend.c 5.8 KB

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