idle.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. * return non-zero on failure
  62. */
  63. static int cpuidle_idle_call(void)
  64. {
  65. struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
  66. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  67. int next_state, entered_state, ret;
  68. bool broadcast;
  69. /*
  70. * Check if the idle task must be rescheduled. If it is the
  71. * case, exit the function after re-enabling the local irq and
  72. * set again the polling flag
  73. */
  74. if (current_clr_polling_and_test()) {
  75. local_irq_enable();
  76. __current_set_polling();
  77. return 0;
  78. }
  79. /*
  80. * During the idle period, stop measuring the disabled irqs
  81. * critical sections latencies
  82. */
  83. stop_critical_timings();
  84. /*
  85. * Tell the RCU framework we are entering an idle section,
  86. * so no more rcu read side critical sections and one more
  87. * step to the grace period
  88. */
  89. rcu_idle_enter();
  90. /*
  91. * Check if the cpuidle framework is ready, otherwise fallback
  92. * to the default arch specific idle method
  93. */
  94. ret = cpuidle_enabled(drv, dev);
  95. if (!ret) {
  96. /*
  97. * Ask the governor to choose an idle state it thinks
  98. * it is convenient to go to. There is *always* a
  99. * convenient idle state
  100. */
  101. next_state = cpuidle_select(drv, dev);
  102. /*
  103. * The idle task must be scheduled, it is pointless to
  104. * go to idle, just update no idle residency and get
  105. * out of this function
  106. */
  107. if (current_clr_polling_and_test()) {
  108. dev->last_residency = 0;
  109. entered_state = next_state;
  110. local_irq_enable();
  111. } else {
  112. broadcast = !!(drv->states[next_state].flags &
  113. CPUIDLE_FLAG_TIMER_STOP);
  114. if (broadcast)
  115. /*
  116. * Tell the time framework to switch
  117. * to a broadcast timer because our
  118. * local timer will be shutdown. If a
  119. * local timer is used from another
  120. * cpu as a broadcast timer, this call
  121. * may fail if it is not available
  122. */
  123. ret = clockevents_notify(
  124. CLOCK_EVT_NOTIFY_BROADCAST_ENTER,
  125. &dev->cpu);
  126. if (!ret) {
  127. trace_cpu_idle_rcuidle(next_state, dev->cpu);
  128. /*
  129. * Enter the idle state previously
  130. * returned by the governor
  131. * decision. This function will block
  132. * until an interrupt occurs and will
  133. * take care of re-enabling the local
  134. * interrupts
  135. */
  136. entered_state = cpuidle_enter(drv, dev,
  137. next_state);
  138. trace_cpu_idle_rcuidle(PWR_EVENT_EXIT,
  139. dev->cpu);
  140. if (broadcast)
  141. clockevents_notify(
  142. CLOCK_EVT_NOTIFY_BROADCAST_EXIT,
  143. &dev->cpu);
  144. /*
  145. * Give the governor an opportunity to reflect on the
  146. * outcome
  147. */
  148. cpuidle_reflect(dev, entered_state);
  149. }
  150. }
  151. }
  152. /*
  153. * We can't use the cpuidle framework, let's use the default
  154. * idle routine
  155. */
  156. if (ret)
  157. arch_cpu_idle();
  158. __current_set_polling();
  159. /*
  160. * It is up to the idle functions to enable back the local
  161. * interrupt
  162. */
  163. if (WARN_ON_ONCE(irqs_disabled()))
  164. local_irq_enable();
  165. rcu_idle_exit();
  166. start_critical_timings();
  167. return 0;
  168. }
  169. /*
  170. * Generic idle loop implementation
  171. */
  172. static void cpu_idle_loop(void)
  173. {
  174. while (1) {
  175. tick_nohz_idle_enter();
  176. while (!need_resched()) {
  177. check_pgt_cache();
  178. rmb();
  179. if (cpu_is_offline(smp_processor_id()))
  180. arch_cpu_idle_dead();
  181. local_irq_disable();
  182. arch_cpu_idle_enter();
  183. /*
  184. * In poll mode we reenable interrupts and spin.
  185. *
  186. * Also if we detected in the wakeup from idle
  187. * path that the tick broadcast device expired
  188. * for us, we don't want to go deep idle as we
  189. * know that the IPI is going to arrive right
  190. * away
  191. */
  192. if (cpu_idle_force_poll || tick_check_broadcast_expired())
  193. cpu_idle_poll();
  194. else
  195. cpuidle_idle_call();
  196. arch_cpu_idle_exit();
  197. }
  198. /*
  199. * Since we fell out of the loop above, we know
  200. * TIF_NEED_RESCHED must be set, propagate it into
  201. * PREEMPT_NEED_RESCHED.
  202. *
  203. * This is required because for polling idle loops we will
  204. * not have had an IPI to fold the state for us.
  205. */
  206. preempt_set_need_resched();
  207. tick_nohz_idle_exit();
  208. schedule_preempt_disabled();
  209. }
  210. }
  211. void cpu_startup_entry(enum cpuhp_state state)
  212. {
  213. /*
  214. * This #ifdef needs to die, but it's too late in the cycle to
  215. * make this generic (arm and sh have never invoked the canary
  216. * init for the non boot cpus!). Will be fixed in 3.11
  217. */
  218. #ifdef CONFIG_X86
  219. /*
  220. * If we're the non-boot CPU, nothing set the stack canary up
  221. * for us. The boot CPU already has it initialized but no harm
  222. * in doing it again. This is a good place for updating it, as
  223. * we wont ever return from this function (so the invalid
  224. * canaries already on the stack wont ever trigger).
  225. */
  226. boot_init_stack_canary();
  227. #endif
  228. __current_set_polling();
  229. arch_cpu_idle_prepare();
  230. cpu_idle_loop();
  231. }