idle.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. }
  55. /*
  56. * Generic idle loop implementation
  57. */
  58. static void cpu_idle_loop(void)
  59. {
  60. while (1) {
  61. tick_nohz_idle_enter();
  62. while (!need_resched()) {
  63. check_pgt_cache();
  64. rmb();
  65. if (cpu_is_offline(smp_processor_id()))
  66. arch_cpu_idle_dead();
  67. local_irq_disable();
  68. arch_cpu_idle_enter();
  69. /*
  70. * In poll mode we reenable interrupts and spin.
  71. *
  72. * Also if we detected in the wakeup from idle
  73. * path that the tick broadcast device expired
  74. * for us, we don't want to go deep idle as we
  75. * know that the IPI is going to arrive right
  76. * away
  77. */
  78. if (cpu_idle_force_poll || tick_check_broadcast_expired()) {
  79. cpu_idle_poll();
  80. } else {
  81. current_clr_polling();
  82. if (!need_resched()) {
  83. stop_critical_timings();
  84. rcu_idle_enter();
  85. arch_cpu_idle();
  86. WARN_ON_ONCE(irqs_disabled());
  87. rcu_idle_exit();
  88. start_critical_timings();
  89. } else {
  90. local_irq_enable();
  91. }
  92. current_set_polling();
  93. }
  94. arch_cpu_idle_exit();
  95. }
  96. tick_nohz_idle_exit();
  97. schedule_preempt_disabled();
  98. }
  99. }
  100. void cpu_startup_entry(enum cpuhp_state state)
  101. {
  102. /*
  103. * This #ifdef needs to die, but it's too late in the cycle to
  104. * make this generic (arm and sh have never invoked the canary
  105. * init for the non boot cpus!). Will be fixed in 3.11
  106. */
  107. #ifdef CONFIG_X86
  108. /*
  109. * If we're the non-boot CPU, nothing set the stack canary up
  110. * for us. The boot CPU already has it initialized but no harm
  111. * in doing it again. This is a good place for updating it, as
  112. * we wont ever return from this function (so the invalid
  113. * canaries already on the stack wont ever trigger).
  114. */
  115. boot_init_stack_canary();
  116. #endif
  117. current_set_polling();
  118. arch_cpu_idle_prepare();
  119. cpu_idle_loop();
  120. }