idle.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. rcu_idle_enter();
  37. trace_cpu_idle_rcuidle(0, smp_processor_id());
  38. local_irq_enable();
  39. while (!need_resched())
  40. cpu_relax();
  41. trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
  42. rcu_idle_exit();
  43. return 1;
  44. }
  45. /* Weak implementations for optional arch specific functions */
  46. void __weak arch_cpu_idle_prepare(void) { }
  47. void __weak arch_cpu_idle_enter(void) { }
  48. void __weak arch_cpu_idle_exit(void) { }
  49. void __weak arch_cpu_idle_dead(void) { }
  50. void __weak arch_cpu_idle(void)
  51. {
  52. cpu_idle_force_poll = 1;
  53. }
  54. /*
  55. * Generic idle loop implementation
  56. */
  57. static void cpu_idle_loop(void)
  58. {
  59. while (1) {
  60. tick_nohz_idle_enter();
  61. while (!need_resched()) {
  62. check_pgt_cache();
  63. rmb();
  64. if (cpu_is_offline(smp_processor_id()))
  65. arch_cpu_idle_dead();
  66. local_irq_disable();
  67. arch_cpu_idle_enter();
  68. /*
  69. * In poll mode we reenable interrupts and spin.
  70. *
  71. * Also if we detected in the wakeup from idle
  72. * path that the tick broadcast device expired
  73. * for us, we don't want to go deep idle as we
  74. * know that the IPI is going to arrive right
  75. * away
  76. */
  77. if (cpu_idle_force_poll || tick_check_broadcast_expired()) {
  78. cpu_idle_poll();
  79. } else {
  80. current_clr_polling();
  81. if (!need_resched()) {
  82. stop_critical_timings();
  83. rcu_idle_enter();
  84. arch_cpu_idle();
  85. WARN_ON_ONCE(irqs_disabled());
  86. rcu_idle_exit();
  87. start_critical_timings();
  88. } else {
  89. local_irq_enable();
  90. }
  91. current_set_polling();
  92. }
  93. arch_cpu_idle_exit();
  94. }
  95. tick_nohz_idle_exit();
  96. schedule_preempt_disabled();
  97. }
  98. }
  99. void cpu_startup_entry(enum cpuhp_state state)
  100. {
  101. current_set_polling();
  102. arch_cpu_idle_prepare();
  103. cpu_idle_loop();
  104. }