blob: 34083c9ac976ecf7e26e1a08566e7eb18c5f04c2 [file] [log] [blame]
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -05001/*
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
11#include <asm/tlb.h>
12
13#include <trace/events/power.h>
14
15static int __read_mostly cpu_idle_force_poll;
16
17void cpu_idle_poll_ctrl(bool enable)
18{
19 if (enable) {
20 cpu_idle_force_poll++;
21 } else {
22 cpu_idle_force_poll--;
23 WARN_ON_ONCE(cpu_idle_force_poll < 0);
24 }
25}
26
27#ifdef CONFIG_GENERIC_IDLE_POLL_SETUP
28static int __init cpu_idle_poll_setup(char *__unused)
29{
30 cpu_idle_force_poll = 1;
31 return 1;
32}
33__setup("nohlt", cpu_idle_poll_setup);
34
35static int __init cpu_idle_nopoll_setup(char *__unused)
36{
37 cpu_idle_force_poll = 0;
38 return 1;
39}
40__setup("hlt", cpu_idle_nopoll_setup);
41#endif
42
43static inline int cpu_idle_poll(void)
44{
45 rcu_idle_enter();
46 trace_cpu_idle_rcuidle(0, smp_processor_id());
47 local_irq_enable();
48 while (!tif_need_resched())
49 cpu_relax();
50 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
51 rcu_idle_exit();
52 return 1;
53}
54
55/* Weak implementations for optional arch specific functions */
56void __weak arch_cpu_idle_prepare(void) { }
57void __weak arch_cpu_idle_enter(void) { }
58void __weak arch_cpu_idle_exit(void) { }
59void __weak arch_cpu_idle_dead(void) { }
60void __weak arch_cpu_idle(void)
61{
62 cpu_idle_force_poll = 1;
63 local_irq_enable();
64}
65
Daniel Lezcano30cdd692014-03-03 08:48:51 +010066/**
67 * cpuidle_idle_call - the main idle function
68 *
69 * NOTE: no locks or semaphores should be used here
Daniel Lezcano30cdd692014-03-03 08:48:51 +010070 */
Rafael J. Wysocki08c373e2014-04-21 01:26:58 +020071static void cpuidle_idle_call(void)
Daniel Lezcano30cdd692014-03-03 08:48:51 +010072{
73 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
74 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
Peter Zijlstra37352272014-04-11 13:55:48 +020075 int next_state, entered_state;
Daniel Lezcano30cdd692014-03-03 08:48:51 +010076 bool broadcast;
77
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +010078 /*
79 * Check if the idle task must be rescheduled. If it is the
Peter Zijlstrac4441172014-04-11 13:47:16 +020080 * case, exit the function after re-enabling the local irq.
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +010081 */
Peter Zijlstrac4441172014-04-11 13:47:16 +020082 if (need_resched()) {
Daniel Lezcano8ca3c642014-03-03 08:48:53 +010083 local_irq_enable();
Rafael J. Wysocki08c373e2014-04-21 01:26:58 +020084 return;
Daniel Lezcano8ca3c642014-03-03 08:48:53 +010085 }
86
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +010087 /*
88 * During the idle period, stop measuring the disabled irqs
89 * critical sections latencies
90 */
Daniel Lezcanoc8cc7d42014-03-03 08:48:52 +010091 stop_critical_timings();
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +010092
93 /*
94 * Tell the RCU framework we are entering an idle section,
95 * so no more rcu read side critical sections and one more
96 * step to the grace period
97 */
Daniel Lezcanoc8cc7d42014-03-03 08:48:52 +010098 rcu_idle_enter();
99
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100100 /*
101 * Check if the cpuidle framework is ready, otherwise fallback
102 * to the default arch specific idle method
103 */
Peter Zijlstra37352272014-04-11 13:55:48 +0200104 if (cpuidle_enabled(drv, dev)) {
105use_default:
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100106 /*
Peter Zijlstra37352272014-04-11 13:55:48 +0200107 * We can't use the cpuidle framework, let's use the default
108 * idle routine.
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100109 */
Peter Zijlstra37352272014-04-11 13:55:48 +0200110 if (current_clr_polling_and_test())
Daniel Lezcano8ca3c642014-03-03 08:48:53 +0100111 local_irq_enable();
Peter Zijlstra37352272014-04-11 13:55:48 +0200112 else
113 arch_cpu_idle();
Daniel Lezcano8ca3c642014-03-03 08:48:53 +0100114
Peter Zijlstra37352272014-04-11 13:55:48 +0200115 goto exit_idle;
Daniel Lezcano8ca3c642014-03-03 08:48:53 +0100116 }
117
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100118 /*
Peter Zijlstra37352272014-04-11 13:55:48 +0200119 * Ask the governor to choose an idle state it thinks
120 * it is convenient to go to. There is *always* a
121 * convenient idle state
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100122 */
Peter Zijlstra37352272014-04-11 13:55:48 +0200123 next_state = cpuidle_select(drv, dev);
124
125 /*
126 * The idle task must be scheduled, it is pointless to
127 * go to idle, just update no idle residency and get
128 * out of this function
129 */
130 if (current_clr_polling_and_test()) {
131 dev->last_residency = 0;
132 entered_state = next_state;
133 local_irq_enable();
134 goto exit_idle;
Peter Zijlstrac4441172014-04-11 13:47:16 +0200135 }
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100136
Peter Zijlstra37352272014-04-11 13:55:48 +0200137 broadcast = !!(drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP);
138
139 /*
140 * Tell the time framework to switch to a broadcast timer
141 * because our local timer will be shutdown. If a local timer
142 * is used from another cpu as a broadcast timer, this call may
143 * fail if it is not available
144 */
145 if (broadcast &&
146 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu))
147 goto use_default;
148
149 trace_cpu_idle_rcuidle(next_state, dev->cpu);
150
151 /*
152 * Enter the idle state previously returned by the governor decision.
153 * This function will block until an interrupt occurs and will take
154 * care of re-enabling the local interrupts
155 */
156 entered_state = cpuidle_enter(drv, dev, next_state);
157
158 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
159
160 if (broadcast)
161 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);
162
163 /*
164 * Give the governor an opportunity to reflect on the outcome
165 */
166 cpuidle_reflect(dev, entered_state);
167
168exit_idle:
Daniel Lezcano8ca3c642014-03-03 08:48:53 +0100169 __current_set_polling();
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100170
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100171 /*
Peter Zijlstra37352272014-04-11 13:55:48 +0200172 * It is up to the idle functions to reenable local interrupts
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100173 */
Daniel Lezcanoc8cc7d42014-03-03 08:48:52 +0100174 if (WARN_ON_ONCE(irqs_disabled()))
175 local_irq_enable();
176
177 rcu_idle_exit();
178 start_critical_timings();
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100179}
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100180
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500181/*
182 * Generic idle loop implementation
183 */
184static void cpu_idle_loop(void)
185{
186 while (1) {
187 tick_nohz_idle_enter();
188
189 while (!need_resched()) {
190 check_pgt_cache();
191 rmb();
192
193 if (cpu_is_offline(smp_processor_id()))
194 arch_cpu_idle_dead();
195
196 local_irq_disable();
197 arch_cpu_idle_enter();
198
199 /*
200 * In poll mode we reenable interrupts and spin.
201 *
202 * Also if we detected in the wakeup from idle
203 * path that the tick broadcast device expired
204 * for us, we don't want to go deep idle as we
205 * know that the IPI is going to arrive right
206 * away
207 */
Daniel Lezcano8ca3c642014-03-03 08:48:53 +0100208 if (cpu_idle_force_poll || tick_check_broadcast_expired())
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500209 cpu_idle_poll();
Daniel Lezcano8ca3c642014-03-03 08:48:53 +0100210 else
211 cpuidle_idle_call();
212
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500213 arch_cpu_idle_exit();
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500214 }
Peter Zijlstra06d50c62014-02-24 18:22:07 +0100215
216 /*
217 * Since we fell out of the loop above, we know
218 * TIF_NEED_RESCHED must be set, propagate it into
219 * PREEMPT_NEED_RESCHED.
220 *
221 * This is required because for polling idle loops we will
222 * not have had an IPI to fold the state for us.
223 */
224 preempt_set_need_resched();
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500225 tick_nohz_idle_exit();
226 schedule_preempt_disabled();
227 }
228}
229
230void cpu_startup_entry(enum cpuhp_state state)
231{
232 /*
233 * This #ifdef needs to die, but it's too late in the cycle to
234 * make this generic (arm and sh have never invoked the canary
235 * init for the non boot cpus!). Will be fixed in 3.11
236 */
237#ifdef CONFIG_X86
238 /*
239 * If we're the non-boot CPU, nothing set the stack canary up
240 * for us. The boot CPU already has it initialized but no harm
241 * in doing it again. This is a good place for updating it, as
242 * we wont ever return from this function (so the invalid
243 * canaries already on the stack wont ever trigger).
244 */
245 boot_init_stack_canary();
246#endif
247 __current_set_polling();
248 arch_cpu_idle_prepare();
249 cpu_idle_loop();
250}