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