idle.c 5.9 KB

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