idle.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* CPU idle: HLT (C1) by default; optional MONITOR/MWAIT (C2).
  2. *
  3. * NOTE: MWAIT is intentionally disabled. On the ASUS AT3IONT-I (Atom 330 +
  4. * NVIDIA MCP79) a USB-legacy SMM event (the first key press) hangs the
  5. * machine when the CPU idles via MWAIT C2. HLT is the path QEMU always used
  6. * (its default CPU has no MONITOR/MWAIT) and what every BIOS's legacy-USB
  7. * emulation expects, since DOS idles with HLT.
  8. */
  9. #include "idle.h"
  10. #define IDLE_USE_MWAIT 0
  11. #if IDLE_USE_MWAIT
  12. static int has_mwait = 0;
  13. static int has_mwait_ext = 0;
  14. static unsigned int mwait_cstate_hint = 0x00;
  15. static void cpuid_idle(unsigned int leaf, unsigned int *eax, unsigned int *ebx,
  16. unsigned int *ecx, unsigned int *edx)
  17. {
  18. __asm__ volatile ("cpuid"
  19. : "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx)
  20. : "a"(leaf), "c"(0));
  21. }
  22. #endif
  23. void idle_init(void)
  24. {
  25. #if IDLE_USE_MWAIT
  26. unsigned int eax, ebx, ecx, edx;
  27. unsigned int max_leaf;
  28. cpuid_idle(0, &max_leaf, &ebx, &ecx, &edx);
  29. if (max_leaf < 1)
  30. return;
  31. cpuid_idle(1, &eax, &ebx, &ecx, &edx);
  32. has_mwait = (ecx & (1u << 3)) ? 1 : 0;
  33. if (has_mwait && max_leaf >= 5) {
  34. cpuid_idle(5, &eax, &ebx, &ecx, &edx);
  35. has_mwait_ext = (ecx & 1u) ? 1 : 0;
  36. }
  37. mwait_cstate_hint = 0x10;
  38. #endif
  39. }
  40. void cpu_idle(void)
  41. {
  42. #if IDLE_USE_MWAIT
  43. if (has_mwait && has_mwait_ext) {
  44. volatile unsigned int dummy = 0;
  45. __asm__ volatile (
  46. "monitor"
  47. :
  48. : "a"(&dummy), "c"(0), "d"(0)
  49. );
  50. __asm__ volatile (
  51. "mwait"
  52. :
  53. : "a"(mwait_cstate_hint), "c"(1)
  54. );
  55. return;
  56. }
  57. #endif
  58. __asm__ volatile ("hlt");
  59. }