idle.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 <linux/stackprotector.h>
  9. #include <asm/tlb.h>
  10. #include <trace/events/power.h>
  11. static int __read_mostly cpu_idle_force_poll;
  12. void cpu_idle_poll_ctrl(bool enable)
  13. {
  14. if (enable) {
  15. cpu_idle_force_poll++;
  16. } else {
  17. cpu_idle_force_poll--;
  18. WARN_ON_ONCE(cpu_idle_force_poll < 0);
  19. }
  20. }
  21. #ifdef CONFIG_GENERIC_IDLE_POLL_SETUP
  22. static int __init cpu_idle_poll_setup(char *__unused)
  23. {
  24. cpu_idle_force_poll = 1;
  25. return 1;
  26. }
  27. __setup("nohlt", cpu_idle_poll_setup);
  28. static int __init cpu_idle_nopoll_setup(char *__unused)
  29. {
  30. cpu_idle_force_poll = 0;
  31. return 1;
  32. }
  33. __setup("hlt", cpu_idle_nopoll_setup);
  34. #endif
  35. static inline int cpu_idle_poll(void)
  36. {
  37. rcu_idle_enter();
  38. trace_cpu_idle_rcuidle(0, smp_processor_id());
  39. local_irq_enable();
  40. while (!need_resched())
  41. cpu_relax();
  42. trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
  43. rcu_idle_exit();
  44. return 1;
  45. }
  46. /* Weak implementations for optional arch specific functions */
  47. void __weak arch_cpu_idle_prepare(void) { }
  48. void __weak arch_cpu_idle_enter(void) { }
  49. void __weak arch_cpu_idle_exit(void) { }
  50. void __weak arch_cpu_idle_dead(void) { }
  51. void __weak arch_cpu_idle(void)
  52. {
  53. cpu_idle_force_poll = 1;
  54. local_irq_enable();
  55. }
  56. /*
  57. * Generic idle loop implementation
  58. */
  59. static void cpu_idle_loop(void)
  60. {
  61. while (1) {
  62. tick_nohz_idle_enter();
  63. while (!need_resched()) {
  64. check_pgt_cache();
  65. rmb();
  66. if (cpu_is_offline(smp_processor_id()))
  67. arch_cpu_idle_dead();
  68. local_irq_disable();
  69. arch_cpu_idle_enter();
  70. /*
  71. * In poll mode we reenable interrupts and spin.
  72. *
  73. * Also if we detected in the wakeup from idle
  74. * path that the tick broadcast device expired
  75. * for us, we don't want to go deep idle as we
  76. * know that the IPI is going to arrive right
  77. * away
  78. */
  79. if (cpu_idle_force_poll || tick_check_broadcast_expired()) {
  80. cpu_idle_poll();
  81. } else {
  82. current_clr_polling();
  83. if (!need_resched()) {
  84. stop_critical_timings();
  85. rcu_idle_enter();
  86. arch_cpu_idle();
  87. WARN_ON_ONCE(irqs_disabled());
  88. rcu_idle_exit();
  89. start_critical_timings();
  90. } else {
  91. local_irq_enable();
  92. }
  93. current_set_polling();
  94. }
  95. arch_cpu_idle_exit();
  96. }
  97. tick_nohz_idle_exit();
  98. schedule_preempt_disabled();
  99. }
  100. }
  101. void cpu_startup_entry(enum cpuhp_state state)
  102. {
  103. /*
  104. * This #ifdef needs to die, but it's too late in the cycle to
  105. * make this generic (arm and sh have never invoked the canary
  106. * init for the non boot cpus!). Will be fixed in 3.11
  107. */
  108. #ifdef CONFIG_X86
  109. /*
  110. * If we're the non-boot CPU, nothing set the stack canary up
  111. * for us. The boot CPU already has it initialized but no harm
  112. * in doing it again. This is a good place for updating it, as
  113. * we wont ever return from this function (so the invalid
  114. * canaries already on the stack wont ever trigger).
  115. */
  116. boot_init_stack_canary();
  117. #endif
  118. current_set_polling();
  119. arch_cpu_idle_prepare();
  120. cpu_idle_loop();
  121. }