sleep.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 <[email protected]>
  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 <linux/ftrace.h>
  19. #include "../../realmode/rm/wakeup.h"
  20. #include "sleep.h"
  21. unsigned long acpi_realmode_flags;
  22. #if defined(CONFIG_SMP) && defined(CONFIG_64BIT)
  23. static char temp_stack[4096];
  24. #endif
  25. /**
  26. * acpi_get_wakeup_address - provide physical address for S3 wakeup
  27. *
  28. * Returns the physical address where the kernel should be resumed after the
  29. * system awakes from S3, e.g. for programming into the firmware waking vector.
  30. */
  31. unsigned long acpi_get_wakeup_address(void)
  32. {
  33. return ((unsigned long)(real_mode_header->wakeup_start));
  34. }
  35. /**
  36. * x86_acpi_enter_sleep_state - enter sleep state
  37. * @state: Sleep state to enter.
  38. *
  39. * Wrapper around acpi_enter_sleep_state() to be called by assembly.
  40. */
  41. asmlinkage acpi_status __visible x86_acpi_enter_sleep_state(u8 state)
  42. {
  43. return acpi_enter_sleep_state(state);
  44. }
  45. /**
  46. * x86_acpi_suspend_lowlevel - save kernel state
  47. *
  48. * Create an identity mapped page table and copy the wakeup routine to
  49. * low memory.
  50. */
  51. int x86_acpi_suspend_lowlevel(void)
  52. {
  53. struct wakeup_header *header =
  54. (struct wakeup_header *) __va(real_mode_header->wakeup_header);
  55. if (header->signature != WAKEUP_HEADER_SIGNATURE) {
  56. printk(KERN_ERR "wakeup header does not match\n");
  57. return -EINVAL;
  58. }
  59. header->video_mode = saved_video_mode;
  60. header->pmode_behavior = 0;
  61. #ifndef CONFIG_64BIT
  62. native_store_gdt((struct desc_ptr *)&header->pmode_gdt);
  63. /*
  64. * We have to check that we can write back the value, and not
  65. * just read it. At least on 90 nm Pentium M (Family 6, Model
  66. * 13), reading an invalid MSR is not guaranteed to trap, see
  67. * Erratum X4 in "Intel Pentium M Processor on 90 nm Process
  68. * with 2-MB L2 Cache and Intel® Processor A100 and A110 on 90
  69. * nm process with 512-KB L2 Cache Specification Update".
  70. */
  71. if (!rdmsr_safe(MSR_EFER,
  72. &header->pmode_efer_low,
  73. &header->pmode_efer_high) &&
  74. !wrmsr_safe(MSR_EFER,
  75. header->pmode_efer_low,
  76. header->pmode_efer_high))
  77. header->pmode_behavior |= (1 << WAKEUP_BEHAVIOR_RESTORE_EFER);
  78. #endif /* !CONFIG_64BIT */
  79. header->pmode_cr0 = read_cr0();
  80. if (__this_cpu_read(cpu_info.cpuid_level) >= 0) {
  81. header->pmode_cr4 = __read_cr4();
  82. header->pmode_behavior |= (1 << WAKEUP_BEHAVIOR_RESTORE_CR4);
  83. }
  84. if (!rdmsr_safe(MSR_IA32_MISC_ENABLE,
  85. &header->pmode_misc_en_low,
  86. &header->pmode_misc_en_high) &&
  87. !wrmsr_safe(MSR_IA32_MISC_ENABLE,
  88. header->pmode_misc_en_low,
  89. header->pmode_misc_en_high))
  90. header->pmode_behavior |=
  91. (1 << WAKEUP_BEHAVIOR_RESTORE_MISC_ENABLE);
  92. header->realmode_flags = acpi_realmode_flags;
  93. header->real_magic = 0x12345678;
  94. #ifndef CONFIG_64BIT
  95. header->pmode_entry = (u32)&wakeup_pmode_return;
  96. header->pmode_cr3 = (u32)__pa_symbol(initial_page_table);
  97. saved_magic = 0x12345678;
  98. #else /* CONFIG_64BIT */
  99. #ifdef CONFIG_SMP
  100. initial_stack = (unsigned long)temp_stack + sizeof(temp_stack);
  101. early_gdt_descr.address =
  102. (unsigned long)get_cpu_gdt_rw(smp_processor_id());
  103. initial_gs = per_cpu_offset(smp_processor_id());
  104. #endif
  105. initial_code = (unsigned long)wakeup_long64;
  106. saved_magic = 0x123456789abcdef0L;
  107. #endif /* CONFIG_64BIT */
  108. /*
  109. * Pause/unpause graph tracing around do_suspend_lowlevel as it has
  110. * inconsistent call/return info after it jumps to the wakeup vector.
  111. */
  112. pause_graph_tracing();
  113. do_suspend_lowlevel();
  114. unpause_graph_tracing();
  115. return 0;
  116. }
  117. static int __init acpi_sleep_setup(char *str)
  118. {
  119. while ((str != NULL) && (*str != '\0')) {
  120. if (strncmp(str, "s3_bios", 7) == 0)
  121. acpi_realmode_flags |= 1;
  122. if (strncmp(str, "s3_mode", 7) == 0)
  123. acpi_realmode_flags |= 2;
  124. if (strncmp(str, "s3_beep", 7) == 0)
  125. acpi_realmode_flags |= 4;
  126. #ifdef CONFIG_HIBERNATION
  127. if (strncmp(str, "s4_hwsig", 8) == 0)
  128. acpi_check_s4_hw_signature = 1;
  129. if (strncmp(str, "s4_nohwsig", 10) == 0)
  130. acpi_check_s4_hw_signature = 0;
  131. #endif
  132. if (strncmp(str, "nonvs", 5) == 0)
  133. acpi_nvs_nosave();
  134. if (strncmp(str, "nonvs_s3", 8) == 0)
  135. acpi_nvs_nosave_s3();
  136. if (strncmp(str, "old_ordering", 12) == 0)
  137. acpi_old_suspend_ordering();
  138. if (strncmp(str, "nobl", 4) == 0)
  139. acpi_sleep_no_blacklist();
  140. str = strchr(str, ',');
  141. if (str != NULL)
  142. str += strspn(str, ", \t");
  143. }
  144. return 1;
  145. }
  146. __setup("acpi_sleep=", acpi_sleep_setup);
  147. #if defined(CONFIG_HIBERNATION) && defined(CONFIG_HYPERVISOR_GUEST)
  148. static int __init init_s4_sigcheck(void)
  149. {
  150. /*
  151. * If running on a hypervisor, honour the ACPI specification
  152. * by default and trigger a clean reboot when the hardware
  153. * signature in FACS is changed after hibernation.
  154. */
  155. if (acpi_check_s4_hw_signature == -1 &&
  156. !hypervisor_is_type(X86_HYPER_NATIVE))
  157. acpi_check_s4_hw_signature = 1;
  158. return 0;
  159. }
  160. /* This must happen before acpi_init() which is a subsys initcall */
  161. arch_initcall(init_s4_sigcheck);
  162. #endif