idle.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Generic entry point for the idle threads
  3. */
  4. #include <linux/sched.h>
  5. #include <linux/cpu.h>
  6. #include <linux/tick.h>
  7. #include <linux/mm.h>
  8. #include <asm/tlb.h>
  9. #include <trace/events/power.h>
  10. static int __read_mostly cpu_idle_force_poll;
  11. void cpu_idle_poll_ctrl(bool enable)
  12. {
  13. if (enable) {
  14. cpu_idle_force_poll++;
  15. } else {
  16. cpu_idle_force_poll--;
  17. WARN_ON_ONCE(cpu_idle_force_poll < 0);
  18. }
  19. }
  20. #ifdef CONFIG_GENERIC_IDLE_POLL_SETUP
  21. static int __init cpu_idle_poll_setup(char *__unused)
  22. {
  23. cpu_idle_force_poll = 1;
  24. return 1;
  25. }
  26. __setup("nohlt", cpu_idle_poll_setup);
  27. static int __init cpu_idle_nopoll_setup(char *__unused)
  28. {
  29. cpu_idle_force_poll = 0;
  30. return 1;
  31. }
  32. __setup("hlt", cpu_idle_nopoll_setup);
  33. #endif
  34. static inline int cpu_idle_poll(void)
  35. {
  36. trace_cpu_idle_rcuidle(0, smp_processor_id());
  37. local_irq_enable();
  38. while (!need_resched())
  39. cpu_relax();
  40. trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
  41. return 1;
  42. }
  43. /* Weak implementations for optional arch specific functions */
  44. void __weak arch_cpu_idle_prepare(void) { }
  45. void __weak arch_cpu_idle_enter(void) { }
  46. void __weak arch_cpu_idle_exit(void) { }
  47. void __weak arch_cpu_idle_dead(void) { }
  48. void __weak arch_cpu_idle(void)
  49. {
  50. cpu_idle_force_poll = 1;
  51. }
  52. /*
  53. * Generic idle loop implementation
  54. */
  55. static void cpu_idle_loop(void)
  56. {
  57. while (1) {
  58. tick_nohz_idle_enter();
  59. while (!need_resched()) {
  60. check_pgt_cache();
  61. rmb();
  62. if (cpu_is_offline(smp_processor_id()))
  63. arch_cpu_idle_dead();
  64. local_irq_disable();
  65. arch_cpu_idle_enter();
  66. /*
  67. * In poll mode we reenable interrupts and spin.
  68. *
  69. * Also if we detected in the wakeup from idle
  70. * path that the tick broadcast device expired
  71. * for us, we don't want to go deep idle as we
  72. * know that the IPI is going to arrive right
  73. * away
  74. */
  75. if (cpu_idle_force_poll || tick_check_broadcast_expired()) {
  76. cpu_idle_poll();
  77. } else {
  78. current_clr_polling();
  79. if (!need_resched()) {
  80. stop_critical_timings();
  81. rcu_idle_enter();
  82. arch_cpu_idle();
  83. WARN_ON_ONCE(irqs_disabled());
  84. rcu_idle_exit();
  85. start_critical_timings();
  86. } else {
  87. local_irq_enable();
  88. }
  89. current_set_polling();
  90. }
  91. arch_cpu_idle_exit();
  92. }
  93. tick_nohz_idle_exit();
  94. schedule_preempt_disabled();
  95. }
  96. }
  97. void cpu_startup_entry(enum cpuhp_state state)
  98. {
  99. current_set_polling();
  100. arch_cpu_idle_prepare();
  101. cpu_idle_loop();
  102. }