idle.c 3.1 KB

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