suspend.c 6.0 KB

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