idle.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * The idle loop for all SuperH platforms.
  3. *
  4. * Copyright (C) 2002 - 2009 Paul Mundt
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/mm.h>
  13. #include <linux/pm.h>
  14. #include <linux/tick.h>
  15. #include <linux/preempt.h>
  16. #include <linux/thread_info.h>
  17. #include <linux/irqflags.h>
  18. #include <linux/smp.h>
  19. #include <linux/cpuidle.h>
  20. #include <asm/pgalloc.h>
  21. #include <asm/system.h>
  22. #include <linux/atomic.h>
  23. #include <asm/smp.h>
  24. void (*pm_idle)(void);
  25. static int hlt_counter;
  26. static int __init nohlt_setup(char *__unused)
  27. {
  28. hlt_counter = 1;
  29. return 1;
  30. }
  31. __setup("nohlt", nohlt_setup);
  32. static int __init hlt_setup(char *__unused)
  33. {
  34. hlt_counter = 0;
  35. return 1;
  36. }
  37. __setup("hlt", hlt_setup);
  38. static inline int hlt_works(void)
  39. {
  40. return !hlt_counter;
  41. }
  42. /*
  43. * On SMP it's slightly faster (but much more power-consuming!)
  44. * to poll the ->work.need_resched flag instead of waiting for the
  45. * cross-CPU IPI to arrive. Use this option with caution.
  46. */
  47. static void poll_idle(void)
  48. {
  49. local_irq_enable();
  50. while (!need_resched())
  51. cpu_relax();
  52. }
  53. void default_idle(void)
  54. {
  55. if (hlt_works()) {
  56. clear_thread_flag(TIF_POLLING_NRFLAG);
  57. smp_mb__after_clear_bit();
  58. set_bl_bit();
  59. if (!need_resched()) {
  60. local_irq_enable();
  61. cpu_sleep();
  62. } else
  63. local_irq_enable();
  64. set_thread_flag(TIF_POLLING_NRFLAG);
  65. clear_bl_bit();
  66. } else
  67. poll_idle();
  68. }
  69. /*
  70. * The idle thread. There's no useful work to be done, so just try to conserve
  71. * power and have a low exit latency (ie sit in a loop waiting for somebody to
  72. * say that they'd like to reschedule)
  73. */
  74. void cpu_idle(void)
  75. {
  76. unsigned int cpu = smp_processor_id();
  77. set_thread_flag(TIF_POLLING_NRFLAG);
  78. /* endless idle loop with no priority at all */
  79. while (1) {
  80. tick_nohz_idle_enter();
  81. rcu_idle_enter();
  82. while (!need_resched()) {
  83. check_pgt_cache();
  84. rmb();
  85. if (cpu_is_offline(cpu))
  86. play_dead();
  87. local_irq_disable();
  88. /* Don't trace irqs off for idle */
  89. stop_critical_timings();
  90. if (cpuidle_idle_call())
  91. pm_idle();
  92. /*
  93. * Sanity check to ensure that pm_idle() returns
  94. * with IRQs enabled
  95. */
  96. WARN_ON(irqs_disabled());
  97. start_critical_timings();
  98. }
  99. rcu_idle_exit();
  100. tick_nohz_idle_exit();
  101. preempt_enable_no_resched();
  102. schedule();
  103. preempt_disable();
  104. }
  105. }
  106. void __init select_idle_routine(void)
  107. {
  108. /*
  109. * If a platform has set its own idle routine, leave it alone.
  110. */
  111. if (pm_idle)
  112. return;
  113. if (hlt_works())
  114. pm_idle = default_idle;
  115. else
  116. pm_idle = poll_idle;
  117. }
  118. static void do_nothing(void *unused)
  119. {
  120. }
  121. void stop_this_cpu(void *unused)
  122. {
  123. local_irq_disable();
  124. set_cpu_online(smp_processor_id(), false);
  125. for (;;)
  126. cpu_sleep();
  127. }
  128. /*
  129. * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
  130. * pm_idle and update to new pm_idle value. Required while changing pm_idle
  131. * handler on SMP systems.
  132. *
  133. * Caller must have changed pm_idle to the new value before the call. Old
  134. * pm_idle value will not be used by any CPU after the return of this function.
  135. */
  136. void cpu_idle_wait(void)
  137. {
  138. smp_mb();
  139. /* kick all the CPUs so that they exit out of pm_idle */
  140. smp_call_function(do_nothing, NULL, 1);
  141. }
  142. EXPORT_SYMBOL_GPL(cpu_idle_wait);