blob: 406508d4ce742d1cffb375dfdd1bee911b6adf76 [file] [log] [blame]
Paul Mundt1da11802008-11-26 15:52:44 +09001/*
2 * The idle loop for all SuperH platforms.
3 *
Paul Mundt2e046b92009-06-19 14:40:51 +09004 * Copyright (C) 2002 - 2009 Paul Mundt
Paul Mundt1da11802008-11-26 15:52:44 +09005 *
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>
Paul Mundt2e046b92009-06-19 14:40:51 +090018#include <linux/smp.h>
Len Browna0bfa132011-04-01 19:34:59 -040019#include <linux/cpuidle.h>
Paul Mundt1da11802008-11-26 15:52:44 +090020#include <asm/pgalloc.h>
21#include <asm/system.h>
Arun Sharma600634972011-07-26 16:09:06 -070022#include <linux/atomic.h>
Paul Mundt763142d2010-04-26 19:08:55 +090023#include <asm/smp.h>
Paul Mundt1da11802008-11-26 15:52:44 +090024
Paul Mundtc66d3fc2011-08-08 16:30:11 +090025void (*pm_idle)(void);
Paul Mundtfbb82b02010-01-20 16:42:52 +090026
27static int hlt_counter;
Paul Mundt1da11802008-11-26 15:52:44 +090028
29static int __init nohlt_setup(char *__unused)
30{
31 hlt_counter = 1;
32 return 1;
33}
34__setup("nohlt", nohlt_setup);
35
36static int __init hlt_setup(char *__unused)
37{
38 hlt_counter = 0;
39 return 1;
40}
41__setup("hlt", hlt_setup);
42
Paul Mundtf533c3d2009-10-16 17:20:58 +090043static inline int hlt_works(void)
Paul Mundt1da11802008-11-26 15:52:44 +090044{
Paul Mundtf533c3d2009-10-16 17:20:58 +090045 return !hlt_counter;
Paul Mundt1da11802008-11-26 15:52:44 +090046}
47
Paul Mundtf533c3d2009-10-16 17:20:58 +090048/*
49 * On SMP it's slightly faster (but much more power-consuming!)
50 * to poll the ->work.need_resched flag instead of waiting for the
51 * cross-CPU IPI to arrive. Use this option with caution.
52 */
53static void poll_idle(void)
54{
55 local_irq_enable();
56 while (!need_resched())
57 cpu_relax();
58}
59
60void default_idle(void)
61{
62 if (hlt_works()) {
63 clear_thread_flag(TIF_POLLING_NRFLAG);
64 smp_mb__after_clear_bit();
65
Paul Mundt73a38b82009-12-18 14:40:56 +090066 set_bl_bit();
Paul Mundtf533c3d2009-10-16 17:20:58 +090067 if (!need_resched()) {
68 local_irq_enable();
69 cpu_sleep();
Paul Mundt9dbe00a2009-10-16 17:55:59 +090070 } else
71 local_irq_enable();
Paul Mundtf533c3d2009-10-16 17:20:58 +090072
73 set_thread_flag(TIF_POLLING_NRFLAG);
Paul Mundt73a38b82009-12-18 14:40:56 +090074 clear_bl_bit();
Paul Mundtf533c3d2009-10-16 17:20:58 +090075 } else
76 poll_idle();
77}
78
79/*
80 * The idle thread. There's no useful work to be done, so just try to conserve
81 * power and have a low exit latency (ie sit in a loop waiting for somebody to
82 * say that they'd like to reschedule)
83 */
Paul Mundt1da11802008-11-26 15:52:44 +090084void cpu_idle(void)
85{
Paul Mundtf533c3d2009-10-16 17:20:58 +090086 unsigned int cpu = smp_processor_id();
87
Paul Mundt1da11802008-11-26 15:52:44 +090088 set_thread_flag(TIF_POLLING_NRFLAG);
89
90 /* endless idle loop with no priority at all */
91 while (1) {
Frederic Weisbecker1268fbc2011-11-17 18:48:14 +010092 tick_nohz_idle_enter();
93 rcu_idle_enter();
Paul Mundt1da11802008-11-26 15:52:44 +090094
Paul Mundt763142d2010-04-26 19:08:55 +090095 while (!need_resched()) {
Paul Mundt0e6d4982009-10-16 17:27:58 +090096 check_pgt_cache();
97 rmb();
98
Paul Mundt763142d2010-04-26 19:08:55 +090099 if (cpu_is_offline(cpu))
100 play_dead();
101
Paul Mundtf533c3d2009-10-16 17:20:58 +0900102 local_irq_disable();
103 /* Don't trace irqs off for idle */
104 stop_critical_timings();
David Browncbc158d2011-08-04 09:24:31 -0700105 if (cpuidle_idle_call())
Len Browna0bfa132011-04-01 19:34:59 -0400106 pm_idle();
Paul Mundtf533c3d2009-10-16 17:20:58 +0900107 /*
108 * Sanity check to ensure that pm_idle() returns
109 * with IRQs enabled
110 */
111 WARN_ON(irqs_disabled());
112 start_critical_timings();
113 }
114
Frederic Weisbecker1268fbc2011-11-17 18:48:14 +0100115 rcu_idle_exit();
116 tick_nohz_idle_exit();
Paul Mundt1da11802008-11-26 15:52:44 +0900117 preempt_enable_no_resched();
118 schedule();
119 preempt_disable();
Paul Mundt1da11802008-11-26 15:52:44 +0900120 }
121}
Paul Mundt2e046b92009-06-19 14:40:51 +0900122
Paul Mundt90851c42010-03-23 17:06:47 +0900123void __init select_idle_routine(void)
Paul Mundtf533c3d2009-10-16 17:20:58 +0900124{
125 /*
126 * If a platform has set its own idle routine, leave it alone.
127 */
128 if (pm_idle)
129 return;
130
131 if (hlt_works())
132 pm_idle = default_idle;
133 else
134 pm_idle = poll_idle;
135}
136
Paul Mundt2e046b92009-06-19 14:40:51 +0900137static void do_nothing(void *unused)
138{
139}
140
Paul Mundtfbb82b02010-01-20 16:42:52 +0900141void stop_this_cpu(void *unused)
142{
143 local_irq_disable();
Paul Mundtf0ccf272010-04-26 18:39:50 +0900144 set_cpu_online(smp_processor_id(), false);
Paul Mundtfbb82b02010-01-20 16:42:52 +0900145
146 for (;;)
147 cpu_sleep();
148}
149
Paul Mundt2e046b92009-06-19 14:40:51 +0900150/*
151 * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
152 * pm_idle and update to new pm_idle value. Required while changing pm_idle
153 * handler on SMP systems.
154 *
155 * Caller must have changed pm_idle to the new value before the call. Old
156 * pm_idle value will not be used by any CPU after the return of this function.
157 */
158void cpu_idle_wait(void)
159{
160 smp_mb();
161 /* kick all the CPUs so that they exit out of pm_idle */
162 smp_call_function(do_nothing, NULL, 1);
163}
164EXPORT_SYMBOL_GPL(cpu_idle_wait);