sleep.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/bootmem.h>
  10. #include <linux/memblock.h>
  11. #include <linux/dmi.h>
  12. #include <linux/cpumask.h>
  13. #include <asm/segment.h>
  14. #include <asm/desc.h>
  15. #include <asm/pgtable.h>
  16. #include <asm/cacheflush.h>
  17. #include <asm/realmode.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. * x86_acpi_enter_sleep_state - enter sleep state
  27. * @state: Sleep state to enter.
  28. *
  29. * Wrapper around acpi_enter_sleep_state() to be called by assmebly.
  30. */
  31. acpi_status asmlinkage __visible x86_acpi_enter_sleep_state(u8 state)
  32. {
  33. return acpi_enter_sleep_state(state);
  34. }
  35. /**
  36. * x86_acpi_suspend_lowlevel - save kernel state
  37. *
  38. * Create an identity mapped page table and copy the wakeup routine to
  39. * low memory.
  40. */
  41. int x86_acpi_suspend_lowlevel(void)
  42. {
  43. struct wakeup_header *header =
  44. (struct wakeup_header *) __va(real_mode_header->wakeup_header);
  45. if (header->signature != WAKEUP_HEADER_SIGNATURE) {
  46. printk(KERN_ERR "wakeup header does not match\n");
  47. return -EINVAL;
  48. }
  49. header->video_mode = saved_video_mode;
  50. header->pmode_behavior = 0;
  51. #ifndef CONFIG_64BIT
  52. native_store_gdt((struct desc_ptr *)&header->pmode_gdt);
  53. /*
  54. * We have to check that we can write back the value, and not
  55. * just read it. At least on 90 nm Pentium M (Family 6, Model
  56. * 13), reading an invalid MSR is not guaranteed to trap, see
  57. * Erratum X4 in "Intel Pentium M Processor on 90 nm Process
  58. * with 2-MB L2 Cache and Intel® Processor A100 and A110 on 90
  59. * nm process with 512-KB L2 Cache Specification Update".
  60. */
  61. if (!rdmsr_safe(MSR_EFER,
  62. &header->pmode_efer_low,
  63. &header->pmode_efer_high) &&
  64. !wrmsr_safe(MSR_EFER,
  65. header->pmode_efer_low,
  66. header->pmode_efer_high))
  67. header->pmode_behavior |= (1 << WAKEUP_BEHAVIOR_RESTORE_EFER);
  68. #endif /* !CONFIG_64BIT */
  69. header->pmode_cr0 = read_cr0();
  70. if (__this_cpu_read(cpu_info.cpuid_level) >= 0) {
  71. header->pmode_cr4 = __read_cr4();
  72. header->pmode_behavior |= (1 << WAKEUP_BEHAVIOR_RESTORE_CR4);
  73. }
  74. if (!rdmsr_safe(MSR_IA32_MISC_ENABLE,
  75. &header->pmode_misc_en_low,
  76. &header->pmode_misc_en_high) &&
  77. !wrmsr_safe(MSR_IA32_MISC_ENABLE,
  78. header->pmode_misc_en_low,
  79. header->pmode_misc_en_high))
  80. header->pmode_behavior |=
  81. (1 << WAKEUP_BEHAVIOR_RESTORE_MISC_ENABLE);
  82. header->realmode_flags = acpi_realmode_flags;
  83. header->real_magic = 0x12345678;
  84. #ifndef CONFIG_64BIT
  85. header->pmode_entry = (u32)&wakeup_pmode_return;
  86. header->pmode_cr3 = (u32)__pa_symbol(initial_page_table);
  87. saved_magic = 0x12345678;
  88. #else /* CONFIG_64BIT */
  89. #ifdef CONFIG_SMP
  90. initial_stack = (unsigned long)temp_stack + sizeof(temp_stack);
  91. early_gdt_descr.address =
  92. (unsigned long)get_cpu_gdt_rw(smp_processor_id());
  93. initial_gs = per_cpu_offset(smp_processor_id());
  94. #endif
  95. initial_code = (unsigned long)wakeup_long64;
  96. saved_magic = 0x123456789abcdef0L;
  97. #endif /* CONFIG_64BIT */
  98. /*
  99. * Pause/unpause graph tracing around do_suspend_lowlevel as it has
  100. * inconsistent call/return info after it jumps to the wakeup vector.
  101. */
  102. pause_graph_tracing();
  103. do_suspend_lowlevel();
  104. unpause_graph_tracing();
  105. return 0;
  106. }
  107. static int __init acpi_sleep_setup(char *str)
  108. {
  109. while ((str != NULL) && (*str != '\0')) {
  110. if (strncmp(str, "s3_bios", 7) == 0)
  111. acpi_realmode_flags |= 1;
  112. if (strncmp(str, "s3_mode", 7) == 0)
  113. acpi_realmode_flags |= 2;
  114. if (strncmp(str, "s3_beep", 7) == 0)
  115. acpi_realmode_flags |= 4;
  116. #ifdef CONFIG_HIBERNATION
  117. if (strncmp(str, "s4_nohwsig", 10) == 0)
  118. acpi_no_s4_hw_signature();
  119. #endif
  120. if (strncmp(str, "nonvs", 5) == 0)
  121. acpi_nvs_nosave();
  122. if (strncmp(str, "nonvs_s3", 8) == 0)
  123. acpi_nvs_nosave_s3();
  124. if (strncmp(str, "old_ordering", 12) == 0)
  125. acpi_old_suspend_ordering();
  126. if (strncmp(str, "nobl", 4) == 0)
  127. acpi_sleep_no_blacklist();
  128. str = strchr(str, ',');
  129. if (str != NULL)
  130. str += strspn(str, ", \t");
  131. }
  132. return 1;
  133. }
  134. __setup("acpi_sleep=", acpi_sleep_setup);