sleep.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * sleep.c - x86-specific ACPI sleep support.
  4. *
  5. * Copyright (C) 2001-2003 Patrick Mochel
  6. * Copyright (C) 2001-2003 Pavel Machek <pavel@ucw.cz>
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/memblock.h>
  10. #include <linux/dmi.h>
  11. #include <linux/cpumask.h>
  12. #include <linux/pgtable.h>
  13. #include <asm/segment.h>
  14. #include <asm/desc.h>
  15. #include <asm/cacheflush.h>
  16. #include <asm/realmode.h>
  17. #include <asm/hypervisor.h>
  18. #include <asm/smp.h>
  19. #include <linux/ftrace.h>
  20. #include "../../realmode/rm/wakeup.h"
  21. #include "sleep.h"
  22. unsigned long acpi_realmode_flags;
  23. #if defined(CONFIG_SMP) && defined(CONFIG_64BIT)
  24. static char temp_stack[4096];
  25. #endif
  26. /**
  27. * acpi_get_wakeup_address - provide physical address for S3 wakeup
  28. *
  29. * Returns the physical address where the kernel should be resumed after the
  30. * system awakes from S3, e.g. for programming into the firmware waking vector.
  31. */
  32. unsigned long acpi_get_wakeup_address(void)
  33. {
  34. return ((unsigned long)(real_mode_header->wakeup_start));
  35. }
  36. /**
  37. * x86_acpi_enter_sleep_state - enter sleep state
  38. * @state: Sleep state to enter.
  39. *
  40. * Wrapper around acpi_enter_sleep_state() to be called by assembly.
  41. */
  42. asmlinkage acpi_status __visible x86_acpi_enter_sleep_state(u8 state)
  43. {
  44. return acpi_enter_sleep_state(state);
  45. }
  46. /**
  47. * x86_acpi_suspend_lowlevel - save kernel state
  48. *
  49. * Create an identity mapped page table and copy the wakeup routine to
  50. * low memory.
  51. */
  52. int x86_acpi_suspend_lowlevel(void)
  53. {
  54. struct wakeup_header *header =
  55. (struct wakeup_header *) __va(real_mode_header->wakeup_header);
  56. if (header->signature != WAKEUP_HEADER_SIGNATURE) {
  57. printk(KERN_ERR "wakeup header does not match\n");
  58. return -EINVAL;
  59. }
  60. header->video_mode = saved_video_mode;
  61. header->pmode_behavior = 0;
  62. #ifndef CONFIG_64BIT
  63. native_store_gdt((struct desc_ptr *)&header->pmode_gdt);
  64. /*
  65. * We have to check that we can write back the value, and not
  66. * just read it. At least on 90 nm Pentium M (Family 6, Model
  67. * 13), reading an invalid MSR is not guaranteed to trap, see
  68. * Erratum X4 in "Intel Pentium M Processor on 90 nm Process
  69. * with 2-MB L2 Cache and Intel® Processor A100 and A110 on 90
  70. * nm process with 512-KB L2 Cache Specification Update".
  71. */
  72. if (!rdmsr_safe(MSR_EFER,
  73. &header->pmode_efer_low,
  74. &header->pmode_efer_high) &&
  75. !wrmsr_safe(MSR_EFER,
  76. header->pmode_efer_low,
  77. header->pmode_efer_high))
  78. header->pmode_behavior |= (1 << WAKEUP_BEHAVIOR_RESTORE_EFER);
  79. #endif /* !CONFIG_64BIT */
  80. header->pmode_cr0 = read_cr0();
  81. if (__this_cpu_read(cpu_info.cpuid_level) >= 0) {
  82. header->pmode_cr4 = __read_cr4();
  83. header->pmode_behavior |= (1 << WAKEUP_BEHAVIOR_RESTORE_CR4);
  84. }
  85. if (!rdmsr_safe(MSR_IA32_MISC_ENABLE,
  86. &header->pmode_misc_en_low,
  87. &header->pmode_misc_en_high) &&
  88. !wrmsr_safe(MSR_IA32_MISC_ENABLE,
  89. header->pmode_misc_en_low,
  90. header->pmode_misc_en_high))
  91. header->pmode_behavior |=
  92. (1 << WAKEUP_BEHAVIOR_RESTORE_MISC_ENABLE);
  93. header->realmode_flags = acpi_realmode_flags;
  94. header->real_magic = 0x12345678;
  95. #ifndef CONFIG_64BIT
  96. header->pmode_entry = (u32)&wakeup_pmode_return;
  97. header->pmode_cr3 = (u32)__pa_symbol(initial_page_table);
  98. saved_magic = 0x12345678;
  99. #else /* CONFIG_64BIT */
  100. #ifdef CONFIG_SMP
  101. /*
  102. * As each CPU starts up, it will find its own stack pointer
  103. * from its current_task->thread.sp. Typically that will be
  104. * the idle thread for a newly-started AP, or even the boot
  105. * CPU which will find it set to &init_task in the static
  106. * per-cpu data.
  107. *
  108. * Make the resuming CPU use the temporary stack at startup
  109. * by setting current->thread.sp to point to that. The true
  110. * %rsp will be restored with the rest of the CPU context,
  111. * by do_suspend_lowlevel(). And unwinders don't care about
  112. * the abuse of ->thread.sp because it's a dead variable
  113. * while the thread is running on the CPU anyway; the true
  114. * value is in the actual %rsp register.
  115. */
  116. current->thread.sp = (unsigned long)temp_stack + sizeof(temp_stack);
  117. /*
  118. * Ensure the CPU knows which one it is when it comes back, if
  119. * it isn't in parallel mode and expected to work that out for
  120. * itself.
  121. */
  122. if (!(smpboot_control & STARTUP_PARALLEL_MASK))
  123. smpboot_control = smp_processor_id();
  124. #endif
  125. initial_code = (unsigned long)wakeup_long64;
  126. saved_magic = 0x123456789abcdef0L;
  127. #endif /* CONFIG_64BIT */
  128. /*
  129. * Pause/unpause graph tracing around do_suspend_lowlevel as it has
  130. * inconsistent call/return info after it jumps to the wakeup vector.
  131. */
  132. pause_graph_tracing();
  133. do_suspend_lowlevel();
  134. unpause_graph_tracing();
  135. return 0;
  136. }
  137. static int __init acpi_sleep_setup(char *str)
  138. {
  139. while ((str != NULL) && (*str != '\0')) {
  140. if (strncmp(str, "s3_bios", 7) == 0)
  141. acpi_realmode_flags |= 1;
  142. if (strncmp(str, "s3_mode", 7) == 0)
  143. acpi_realmode_flags |= 2;
  144. if (strncmp(str, "s3_beep", 7) == 0)
  145. acpi_realmode_flags |= 4;
  146. #ifdef CONFIG_HIBERNATION
  147. if (strncmp(str, "s4_hwsig", 8) == 0)
  148. acpi_check_s4_hw_signature = 1;
  149. if (strncmp(str, "s4_nohwsig", 10) == 0)
  150. acpi_check_s4_hw_signature = 0;
  151. #endif
  152. if (strncmp(str, "nonvs", 5) == 0)
  153. acpi_nvs_nosave();
  154. if (strncmp(str, "nonvs_s3", 8) == 0)
  155. acpi_nvs_nosave_s3();
  156. if (strncmp(str, "old_ordering", 12) == 0)
  157. acpi_old_suspend_ordering();
  158. if (strncmp(str, "nobl", 4) == 0)
  159. acpi_sleep_no_blacklist();
  160. str = strchr(str, ',');
  161. if (str != NULL)
  162. str += strspn(str, ", \t");
  163. }
  164. return 1;
  165. }
  166. __setup("acpi_sleep=", acpi_sleep_setup);
  167. #if defined(CONFIG_HIBERNATION) && defined(CONFIG_HYPERVISOR_GUEST)
  168. static int __init init_s4_sigcheck(void)
  169. {
  170. /*
  171. * If running on a hypervisor, honour the ACPI specification
  172. * by default and trigger a clean reboot when the hardware
  173. * signature in FACS is changed after hibernation.
  174. */
  175. if (acpi_check_s4_hw_signature == -1 &&
  176. !hypervisor_is_type(X86_HYPER_NATIVE))
  177. acpi_check_s4_hw_signature = 1;
  178. return 0;
  179. }
  180. /* This must happen before acpi_init() which is a subsys initcall */
  181. arch_initcall(init_s4_sigcheck);
  182. #endif