idle.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* CPU idle: automatic deep C-states on Nehalem/Westmere and Airmont/Braswell,
  2. * HLT elsewhere.
  3. *
  4. * The default is IDLE_AUTO, which asks power.c for the MWAIT hint of the
  5. * CPU: C6 (0x20) on the Arrandale/Nehalem family, C6N (0x58) on Braswell,
  6. * and -1 (HLT) on the older Atom, where the old USB-legacy SMM hang made
  7. * deep states risky. The explicit c1e/c3/c6 modes remain for testing; the
  8. * c3ap/c6ap variants only apply to the APs.
  9. *
  10. * Arrandale MWAIT state hints (coreboot model_2065x): C1 0x00, C1E 0x01,
  11. * C3 0x10, C6 0x20. Airmont uses the Cherry Trail C6N hint 0x58. The
  12. * MWAIT ECX bit 0 (interrupts break the state) is set so a wake-up never
  13. * waits for the full hint duration.
  14. */
  15. #include "idle.h"
  16. #include "power.h"
  17. #include "sched.h"
  18. #define MWAIT_C1E 0x01
  19. #define MWAIT_C3 0x10
  20. #define MWAIT_C6 0x20
  21. static int has_mwait;
  22. static int has_mwait_ext;
  23. static int current_mode = IDLE_AUTO;
  24. static void cpuid_idle(unsigned int leaf, unsigned int *eax, unsigned int *ebx,
  25. unsigned int *ecx, unsigned int *edx)
  26. {
  27. __asm__ volatile ("cpuid"
  28. : "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx)
  29. : "a"(leaf), "c"(0));
  30. }
  31. void idle_init(void)
  32. {
  33. unsigned int eax, ebx, ecx, edx;
  34. unsigned int max_leaf;
  35. has_mwait = 0;
  36. has_mwait_ext = 0;
  37. current_mode = IDLE_AUTO;
  38. cpuid_idle(0, &max_leaf, &ebx, &ecx, &edx);
  39. if (max_leaf < 1)
  40. return;
  41. cpuid_idle(1, &eax, &ebx, &ecx, &edx);
  42. has_mwait = (ecx & (1u << 3)) ? 1 : 0;
  43. if (has_mwait && max_leaf >= 5) {
  44. cpuid_idle(5, &eax, &ebx, &ecx, &edx);
  45. has_mwait_ext = (ecx & 1u) ? 1 : 0;
  46. }
  47. }
  48. int idle_set_mode(int mode)
  49. {
  50. if (mode == IDLE_AUTO) {
  51. current_mode = IDLE_AUTO;
  52. return 0;
  53. }
  54. if (mode == IDLE_C1) {
  55. current_mode = IDLE_C1;
  56. return 0;
  57. }
  58. if (!has_mwait || !has_mwait_ext)
  59. return -1;
  60. if (mode >= IDLE_C1E && mode <= IDLE_C6_AP) {
  61. current_mode = mode;
  62. return 0;
  63. }
  64. return -1;
  65. }
  66. int idle_mode(void)
  67. {
  68. return current_mode;
  69. }
  70. int idle_mwait_supported(void)
  71. {
  72. return has_mwait && has_mwait_ext;
  73. }
  74. const char *idle_mode_name(int mode)
  75. {
  76. switch (mode) {
  77. case IDLE_C1E:
  78. return "mwait c1e";
  79. case IDLE_C3:
  80. return "mwait c3";
  81. case IDLE_C6:
  82. return "mwait c6";
  83. case IDLE_C3_AP:
  84. return "mwait c3 (APs)";
  85. case IDLE_C6_AP:
  86. return "mwait c6 (APs)";
  87. case IDLE_AUTO:
  88. return "auto";
  89. default:
  90. return "hlt";
  91. }
  92. }
  93. void cpu_idle(void)
  94. {
  95. static unsigned int wake_addr;
  96. int mode = current_mode;
  97. int hint = -1;
  98. if (mode == IDLE_AUTO) {
  99. hint = power_idle_hint();
  100. if (hint >= 0 && (!has_mwait || !has_mwait_ext))
  101. hint = -1;
  102. } else if (mode != IDLE_C1) {
  103. int ap_only = (mode == IDLE_C3_AP || mode == IDLE_C6_AP);
  104. if (!has_mwait || !has_mwait_ext ||
  105. (ap_only && this_cpu()->id == 0)) {
  106. __asm__ volatile ("hlt");
  107. return;
  108. }
  109. if (mode == IDLE_C1E)
  110. hint = MWAIT_C1E;
  111. else if (mode == IDLE_C3 || mode == IDLE_C3_AP)
  112. hint = MWAIT_C3;
  113. else
  114. hint = MWAIT_C6;
  115. }
  116. if (hint >= 0) {
  117. /* MONITOR/MWAIT on a cacheable address; ECX=1 in MWAIT lets
  118. * an interrupt break the state immediately. */
  119. __asm__ volatile ("monitor"
  120. : : "a"(&wake_addr), "c"(0), "d"(0));
  121. __asm__ volatile ("mwait" : : "a"((unsigned int)hint),
  122. "c"(1));
  123. return;
  124. }
  125. __asm__ volatile ("hlt");
  126. }