idle.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Generic entry point for the idle threads
  3. */
  4. #include <linux/sched.h>
  5. #include <linux/cpu.h>
  6. #include <linux/cpuidle.h>
  7. #include <linux/tick.h>
  8. #include <linux/mm.h>
  9. #include <linux/stackprotector.h>
  10. #include <asm/tlb.h>
  11. #include <trace/events/power.h>
  12. static int __read_mostly cpu_idle_force_poll;
  13. void cpu_idle_poll_ctrl(bool enable)
  14. {
  15. if (enable) {
  16. cpu_idle_force_poll++;
  17. } else {
  18. cpu_idle_force_poll--;
  19. WARN_ON_ONCE(cpu_idle_force_poll < 0);
  20. }
  21. }
  22. #ifdef CONFIG_GENERIC_IDLE_POLL_SETUP
  23. static int __init cpu_idle_poll_setup(char *__unused)
  24. {
  25. cpu_idle_force_poll = 1;
  26. return 1;
  27. }
  28. __setup("nohlt", cpu_idle_poll_setup);
  29. static int __init cpu_idle_nopoll_setup(char *__unused)
  30. {
  31. cpu_idle_force_poll = 0;
  32. return 1;
  33. }
  34. __setup("hlt", cpu_idle_nopoll_setup);
  35. #endif
  36. static inline int cpu_idle_poll(void)
  37. {
  38. rcu_idle_enter();
  39. trace_cpu_idle_rcuidle(0, smp_processor_id());
  40. local_irq_enable();
  41. while (!tif_need_resched())
  42. cpu_relax();
  43. trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
  44. rcu_idle_exit();
  45. return 1;
  46. }
  47. /* Weak implementations for optional arch specific functions */
  48. void __weak arch_cpu_idle_prepare(void) { }
  49. void __weak arch_cpu_idle_enter(void) { }
  50. void __weak arch_cpu_idle_exit(void) { }
  51. void __weak arch_cpu_idle_dead(void) { }
  52. void __weak arch_cpu_idle(void)
  53. {
  54. cpu_idle_force_poll = 1;
  55. local_irq_enable();
  56. }
  57. /**
  58. * cpuidle_idle_call - the main idle function
  59. *
  60. * NOTE: no locks or semaphores should be used here
  61. */
  62. static void cpuidle_idle_call(void)
  63. {
  64. struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
  65. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  66. int next_state, entered_state;
  67. bool broadcast;
  68. /*
  69. * Check if the idle task must be rescheduled. If it is the
  70. * case, exit the function after re-enabling the local irq.
  71. */
  72. if (need_resched()) {
  73. local_irq_enable();
  74. return;
  75. }
  76. /*
  77. * During the idle period, stop measuring the disabled irqs
  78. * critical sections latencies
  79. */
  80. stop_critical_timings();
  81. /*
  82. * Tell the RCU framework we are entering an idle section,
  83. * so no more rcu read side critical sections and one more
  84. * step to the grace period
  85. */
  86. rcu_idle_enter();
  87. /*
  88. * Ask the cpuidle framework to choose a convenient idle state.
  89. * Fall back to the default arch idle method on errors.
  90. */
  91. next_state = cpuidle_select(drv, dev);
  92. if (next_state < 0) {
  93. use_default:
  94. /*
  95. * We can't use the cpuidle framework, let's use the default
  96. * idle routine.
  97. */
  98. if (current_clr_polling_and_test())
  99. local_irq_enable();
  100. else
  101. arch_cpu_idle();
  102. goto exit_idle;
  103. }
  104. /*
  105. * The idle task must be scheduled, it is pointless to
  106. * go to idle, just update no idle residency and get
  107. * out of this function
  108. */
  109. if (current_clr_polling_and_test()) {
  110. dev->last_residency = 0;
  111. entered_state = next_state;
  112. local_irq_enable();
  113. goto exit_idle;
  114. }
  115. broadcast = !!(drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP);
  116. /*
  117. * Tell the time framework to switch to a broadcast timer
  118. * because our local timer will be shutdown. If a local timer
  119. * is used from another cpu as a broadcast timer, this call may
  120. * fail if it is not available
  121. */
  122. if (broadcast &&
  123. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu))
  124. goto use_default;
  125. trace_cpu_idle_rcuidle(next_state, dev->cpu);
  126. /*
  127. * Enter the idle state previously returned by the governor decision.
  128. * This function will block until an interrupt occurs and will take
  129. * care of re-enabling the local interrupts
  130. */
  131. entered_state = cpuidle_enter(drv, dev, next_state);
  132. trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
  133. if (broadcast)
  134. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);
  135. /*
  136. * Give the governor an opportunity to reflect on the outcome
  137. */
  138. cpuidle_reflect(dev, entered_state);
  139. exit_idle:
  140. __current_set_polling();
  141. /*
  142. * It is up to the idle functions to reenable local interrupts
  143. */
  144. if (WARN_ON_ONCE(irqs_disabled()))
  145. local_irq_enable();
  146. rcu_idle_exit();
  147. start_critical_timings();
  148. }
  149. /*
  150. * Generic idle loop implementation
  151. */
  152. static void cpu_idle_loop(void)
  153. {
  154. while (1) {
  155. tick_nohz_idle_enter();
  156. while (!need_resched()) {
  157. check_pgt_cache();
  158. rmb();
  159. if (cpu_is_offline(smp_processor_id()))
  160. arch_cpu_idle_dead();
  161. local_irq_disable();
  162. arch_cpu_idle_enter();
  163. /*
  164. * In poll mode we reenable interrupts and spin.
  165. *
  166. * Also if we detected in the wakeup from idle
  167. * path that the tick broadcast device expired
  168. * for us, we don't want to go deep idle as we
  169. * know that the IPI is going to arrive right
  170. * away
  171. */
  172. if (cpu_idle_force_poll || tick_check_broadcast_expired())
  173. cpu_idle_poll();
  174. else
  175. cpuidle_idle_call();
  176. arch_cpu_idle_exit();
  177. }
  178. /*
  179. * Since we fell out of the loop above, we know
  180. * TIF_NEED_RESCHED must be set, propagate it into
  181. * PREEMPT_NEED_RESCHED.
  182. *
  183. * This is required because for polling idle loops we will
  184. * not have had an IPI to fold the state for us.
  185. */
  186. preempt_set_need_resched();
  187. tick_nohz_idle_exit();
  188. schedule_preempt_disabled();
  189. }
  190. }
  191. void cpu_startup_entry(enum cpuhp_state state)
  192. {
  193. /*
  194. * This #ifdef needs to die, but it's too late in the cycle to
  195. * make this generic (arm and sh have never invoked the canary
  196. * init for the non boot cpus!). Will be fixed in 3.11
  197. */
  198. #ifdef CONFIG_X86
  199. /*
  200. * If we're the non-boot CPU, nothing set the stack canary up
  201. * for us. The boot CPU already has it initialized but no harm
  202. * in doing it again. This is a good place for updating it, as
  203. * we wont ever return from this function (so the invalid
  204. * canaries already on the stack wont ever trigger).
  205. */
  206. boot_init_stack_canary();
  207. #endif
  208. __current_set_polling();
  209. arch_cpu_idle_prepare();
  210. cpu_idle_loop();
  211. }