suspend.c 5.1 KB

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