idle.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <asm/pgalloc.h>
  20. #include <asm/system.h>
  21. #include <asm/atomic.h>
  22. void (*pm_idle)(void) = NULL;
  23. static int hlt_counter;
  24. static int __init nohlt_setup(char *__unused)
  25. {
  26. hlt_counter = 1;
  27. return 1;
  28. }
  29. __setup("nohlt", nohlt_setup);
  30. static int __init hlt_setup(char *__unused)
  31. {
  32. hlt_counter = 0;
  33. return 1;
  34. }
  35. __setup("hlt", hlt_setup);
  36. static inline int hlt_works(void)
  37. {
  38. return !hlt_counter;
  39. }
  40. /*
  41. * On SMP it's slightly faster (but much more power-consuming!)
  42. * to poll the ->work.need_resched flag instead of waiting for the
  43. * cross-CPU IPI to arrive. Use this option with caution.
  44. */
  45. static void poll_idle(void)
  46. {
  47. local_irq_enable();
  48. while (!need_resched())
  49. cpu_relax();
  50. }
  51. void default_idle(void)
  52. {
  53. if (hlt_works()) {
  54. clear_thread_flag(TIF_POLLING_NRFLAG);
  55. smp_mb__after_clear_bit();
  56. set_bl_bit();
  57. if (!need_resched()) {
  58. local_irq_enable();
  59. cpu_sleep();
  60. } else
  61. local_irq_enable();
  62. set_thread_flag(TIF_POLLING_NRFLAG);
  63. clear_bl_bit();
  64. } else
  65. poll_idle();
  66. }
  67. /*
  68. * The idle thread. There's no useful work to be done, so just try to conserve
  69. * power and have a low exit latency (ie sit in a loop waiting for somebody to
  70. * say that they'd like to reschedule)
  71. */
  72. void cpu_idle(void)
  73. {
  74. unsigned int cpu = smp_processor_id();
  75. set_thread_flag(TIF_POLLING_NRFLAG);
  76. /* endless idle loop with no priority at all */
  77. while (1) {
  78. tick_nohz_stop_sched_tick(1);
  79. while (!need_resched() && cpu_online(cpu)) {
  80. check_pgt_cache();
  81. rmb();
  82. local_irq_disable();
  83. /* Don't trace irqs off for idle */
  84. stop_critical_timings();
  85. pm_idle();
  86. /*
  87. * Sanity check to ensure that pm_idle() returns
  88. * with IRQs enabled
  89. */
  90. WARN_ON(irqs_disabled());
  91. start_critical_timings();
  92. }
  93. tick_nohz_restart_sched_tick();
  94. preempt_enable_no_resched();
  95. schedule();
  96. preempt_disable();
  97. }
  98. }
  99. void __init select_idle_routine(void)
  100. {
  101. /*
  102. * If a platform has set its own idle routine, leave it alone.
  103. */
  104. if (pm_idle)
  105. return;
  106. if (hlt_works())
  107. pm_idle = default_idle;
  108. else
  109. pm_idle = poll_idle;
  110. }
  111. static void do_nothing(void *unused)
  112. {
  113. }
  114. void stop_this_cpu(void *unused)
  115. {
  116. local_irq_disable();
  117. cpu_clear(smp_processor_id(), cpu_online_map);
  118. for (;;)
  119. cpu_sleep();
  120. }
  121. /*
  122. * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
  123. * pm_idle and update to new pm_idle value. Required while changing pm_idle
  124. * handler on SMP systems.
  125. *
  126. * Caller must have changed pm_idle to the new value before the call. Old
  127. * pm_idle value will not be used by any CPU after the return of this function.
  128. */
  129. void cpu_idle_wait(void)
  130. {
  131. smp_mb();
  132. /* kick all the CPUs so that they exit out of pm_idle */
  133. smp_call_function(do_nothing, NULL, 1);
  134. }
  135. EXPORT_SYMBOL_GPL(cpu_idle_wait);