idle.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* CPU idle: HLT (C1) by default; optional MONITOR/MWAIT C2/C3.
  2. *
  3. * MWAIT used to be left off entirely on the ASUS AT3IONT-I (Atom 330 +
  4. * NVIDIA MCP79): a USB-legacy SMM event (the first key press) hung the
  5. * machine in C2. The UHCI/OHCI drivers now disable that legacy SMI
  6. * emulation (PCI config 0xC0), so C2 is exposed through `idle c2` for a
  7. * runtime test. HLT stays the default until the board has been tested;
  8. * `idle` with no argument shows the current mode.
  9. */
  10. #include "idle.h"
  11. #include "sched.h"
  12. static int has_mwait;
  13. static int has_mwait_ext;
  14. static int current_mode = IDLE_C1;
  15. static unsigned int mwait_hint = 0x10;
  16. static void cpuid_idle(unsigned int leaf, unsigned int *eax, unsigned int *ebx,
  17. unsigned int *ecx, unsigned int *edx)
  18. {
  19. __asm__ volatile ("cpuid"
  20. : "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx)
  21. : "a"(leaf), "c"(0));
  22. }
  23. void idle_init(void)
  24. {
  25. unsigned int eax, ebx, ecx, edx;
  26. unsigned int max_leaf;
  27. has_mwait = 0;
  28. has_mwait_ext = 0;
  29. current_mode = IDLE_C1;
  30. cpuid_idle(0, &max_leaf, &ebx, &ecx, &edx);
  31. if (max_leaf < 1)
  32. return;
  33. cpuid_idle(1, &eax, &ebx, &ecx, &edx);
  34. has_mwait = (ecx & (1u << 3)) ? 1 : 0;
  35. if (has_mwait && max_leaf >= 5) {
  36. cpuid_idle(5, &eax, &ebx, &ecx, &edx);
  37. has_mwait_ext = (ecx & 1u) ? 1 : 0;
  38. }
  39. }
  40. int idle_set_mode(int mode)
  41. {
  42. if (mode == IDLE_C1) {
  43. current_mode = IDLE_C1;
  44. return 0;
  45. }
  46. if (!has_mwait || !has_mwait_ext)
  47. return -1;
  48. if (mode == IDLE_C2) {
  49. mwait_hint = 0x10;
  50. current_mode = IDLE_C2;
  51. return 0;
  52. }
  53. if (mode == IDLE_C3) {
  54. mwait_hint = 0x20;
  55. current_mode = IDLE_C3;
  56. return 0;
  57. }
  58. if (mode == IDLE_C2_AP) {
  59. mwait_hint = 0x10;
  60. current_mode = IDLE_C2_AP;
  61. return 0;
  62. }
  63. if (mode == IDLE_C3_AP) {
  64. mwait_hint = 0x20;
  65. current_mode = IDLE_C3_AP;
  66. return 0;
  67. }
  68. return -1;
  69. }
  70. int idle_mode(void)
  71. {
  72. return current_mode;
  73. }
  74. int idle_mwait_supported(void)
  75. {
  76. return has_mwait && has_mwait_ext;
  77. }
  78. const char *idle_mode_name(int mode)
  79. {
  80. switch (mode) {
  81. case IDLE_C2:
  82. return "mwait c2";
  83. case IDLE_C3:
  84. return "mwait c3";
  85. case IDLE_C2_AP:
  86. return "mwait c2 (APs)";
  87. case IDLE_C3_AP:
  88. return "mwait c3 (APs)";
  89. default:
  90. return "hlt";
  91. }
  92. }
  93. void cpu_idle(void)
  94. {
  95. int ap_only = (current_mode == IDLE_C2_AP ||
  96. current_mode == IDLE_C3_AP);
  97. if (current_mode != IDLE_C1 && has_mwait && has_mwait_ext &&
  98. (!ap_only || this_cpu()->id != 0)) {
  99. volatile unsigned int dummy = 0;
  100. __asm__ volatile ("monitor" : : "a"(&dummy), "c"(0), "d"(0));
  101. __asm__ volatile ("mwait" : : "a"(mwait_hint), "c"(1));
  102. return;
  103. }
  104. __asm__ volatile ("hlt");
  105. }