blob: fe4b24bf33cafe3980c10cfe1d1a2939c09202c6 [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
Andy Lutomirski82c65d62014-06-04 10:31:16 -070070 *
71 * On archs that support TIF_POLLING_NRFLAG, is called with polling
72 * set, and it returns with polling set. If it ever stops polling, it
73 * must clear the polling bit.
Daniel Lezcano30cdd692014-03-03 08:48:51 +010074 */
Rafael J. Wysocki08c373e2014-04-21 01:26:58 +020075static void cpuidle_idle_call(void)
Daniel Lezcano30cdd692014-03-03 08:48:51 +010076{
77 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
78 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
Peter Zijlstra37352272014-04-11 13:55:48 +020079 int next_state, entered_state;
Daniel Lezcano30cdd692014-03-03 08:48:51 +010080 bool broadcast;
81
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +010082 /*
83 * Check if the idle task must be rescheduled. If it is the
Peter Zijlstrac4441172014-04-11 13:47:16 +020084 * case, exit the function after re-enabling the local irq.
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +010085 */
Peter Zijlstrac4441172014-04-11 13:47:16 +020086 if (need_resched()) {
Daniel Lezcano8ca3c642014-03-03 08:48:53 +010087 local_irq_enable();
Rafael J. Wysocki08c373e2014-04-21 01:26:58 +020088 return;
Daniel Lezcano8ca3c642014-03-03 08:48:53 +010089 }
90
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +010091 /*
92 * During the idle period, stop measuring the disabled irqs
93 * critical sections latencies
94 */
Daniel Lezcanoc8cc7d42014-03-03 08:48:52 +010095 stop_critical_timings();
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +010096
97 /*
98 * Tell the RCU framework we are entering an idle section,
99 * so no more rcu read side critical sections and one more
100 * step to the grace period
101 */
Daniel Lezcanoc8cc7d42014-03-03 08:48:52 +0100102 rcu_idle_enter();
103
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100104 /*
Rafael J. Wysocki52c324f2014-05-01 00:13:47 +0200105 * Ask the cpuidle framework to choose a convenient idle state.
Ingo Molnarec6e7f42014-05-22 10:37:06 +0200106 * Fall back to the default arch idle method on errors.
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100107 */
Rafael J. Wysocki52c324f2014-05-01 00:13:47 +0200108 next_state = cpuidle_select(drv, dev);
Ingo Molnarec6e7f42014-05-22 10:37:06 +0200109 if (next_state < 0) {
Peter Zijlstra37352272014-04-11 13:55:48 +0200110use_default:
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100111 /*
Peter Zijlstra37352272014-04-11 13:55:48 +0200112 * We can't use the cpuidle framework, let's use the default
113 * idle routine.
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100114 */
Peter Zijlstra37352272014-04-11 13:55:48 +0200115 if (current_clr_polling_and_test())
Daniel Lezcano8ca3c642014-03-03 08:48:53 +0100116 local_irq_enable();
Peter Zijlstra37352272014-04-11 13:55:48 +0200117 else
118 arch_cpu_idle();
Daniel Lezcano8ca3c642014-03-03 08:48:53 +0100119
Peter Zijlstra37352272014-04-11 13:55:48 +0200120 goto exit_idle;
Daniel Lezcano8ca3c642014-03-03 08:48:53 +0100121 }
122
Peter Zijlstra37352272014-04-11 13:55:48 +0200123
124 /*
125 * The idle task must be scheduled, it is pointless to
126 * go to idle, just update no idle residency and get
127 * out of this function
128 */
129 if (current_clr_polling_and_test()) {
130 dev->last_residency = 0;
131 entered_state = next_state;
132 local_irq_enable();
133 goto exit_idle;
Peter Zijlstrac4441172014-04-11 13:47:16 +0200134 }
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100135
Peter Zijlstra37352272014-04-11 13:55:48 +0200136 broadcast = !!(drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP);
137
138 /*
139 * Tell the time framework to switch to a broadcast timer
140 * because our local timer will be shutdown. If a local timer
141 * is used from another cpu as a broadcast timer, this call may
142 * fail if it is not available
143 */
144 if (broadcast &&
145 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu))
146 goto use_default;
147
148 trace_cpu_idle_rcuidle(next_state, dev->cpu);
149
150 /*
151 * Enter the idle state previously returned by the governor decision.
152 * This function will block until an interrupt occurs and will take
153 * care of re-enabling the local interrupts
154 */
155 entered_state = cpuidle_enter(drv, dev, next_state);
156
157 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
158
159 if (broadcast)
160 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);
161
162 /*
163 * Give the governor an opportunity to reflect on the outcome
164 */
165 cpuidle_reflect(dev, entered_state);
166
167exit_idle:
Daniel Lezcano8ca3c642014-03-03 08:48:53 +0100168 __current_set_polling();
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100169
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100170 /*
Peter Zijlstra37352272014-04-11 13:55:48 +0200171 * It is up to the idle functions to reenable local interrupts
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100172 */
Daniel Lezcanoc8cc7d42014-03-03 08:48:52 +0100173 if (WARN_ON_ONCE(irqs_disabled()))
174 local_irq_enable();
175
176 rcu_idle_exit();
177 start_critical_timings();
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100178}
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100179
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500180/*
181 * Generic idle loop implementation
Andy Lutomirski82c65d62014-06-04 10:31:16 -0700182 *
183 * Called with polling cleared.
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500184 */
185static void cpu_idle_loop(void)
186{
187 while (1) {
Andy Lutomirski82c65d62014-06-04 10:31:16 -0700188 /*
189 * If the arch has a polling bit, we maintain an invariant:
190 *
191 * Our polling bit is clear if we're not scheduled (i.e. if
192 * rq->curr != rq->idle). This means that, if rq->idle has
193 * the polling bit set, then setting need_resched is
194 * guaranteed to cause the cpu to reschedule.
195 */
196
197 __current_set_polling();
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500198 tick_nohz_idle_enter();
199
200 while (!need_resched()) {
201 check_pgt_cache();
202 rmb();
203
204 if (cpu_is_offline(smp_processor_id()))
205 arch_cpu_idle_dead();
206
207 local_irq_disable();
208 arch_cpu_idle_enter();
209
210 /*
211 * In poll mode we reenable interrupts and spin.
212 *
213 * Also if we detected in the wakeup from idle
214 * path that the tick broadcast device expired
215 * for us, we don't want to go deep idle as we
216 * know that the IPI is going to arrive right
217 * away
218 */
Daniel Lezcano8ca3c642014-03-03 08:48:53 +0100219 if (cpu_idle_force_poll || tick_check_broadcast_expired())
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500220 cpu_idle_poll();
Daniel Lezcano8ca3c642014-03-03 08:48:53 +0100221 else
222 cpuidle_idle_call();
223
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500224 arch_cpu_idle_exit();
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500225 }
Peter Zijlstra06d50c62014-02-24 18:22:07 +0100226
227 /*
228 * Since we fell out of the loop above, we know
229 * TIF_NEED_RESCHED must be set, propagate it into
230 * PREEMPT_NEED_RESCHED.
231 *
232 * This is required because for polling idle loops we will
233 * not have had an IPI to fold the state for us.
234 */
235 preempt_set_need_resched();
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500236 tick_nohz_idle_exit();
Andy Lutomirski82c65d62014-06-04 10:31:16 -0700237 __current_clr_polling();
238
239 /*
240 * We promise to reschedule if need_resched is set while
241 * polling is set. That means that clearing polling
242 * needs to be visible before rescheduling.
243 */
244 smp_mb__after_atomic();
245
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500246 schedule_preempt_disabled();
247 }
248}
249
250void cpu_startup_entry(enum cpuhp_state state)
251{
252 /*
253 * This #ifdef needs to die, but it's too late in the cycle to
254 * make this generic (arm and sh have never invoked the canary
255 * init for the non boot cpus!). Will be fixed in 3.11
256 */
257#ifdef CONFIG_X86
258 /*
259 * If we're the non-boot CPU, nothing set the stack canary up
260 * for us. The boot CPU already has it initialized but no harm
261 * in doing it again. This is a good place for updating it, as
262 * we wont ever return from this function (so the invalid
263 * canaries already on the stack wont ever trigger).
264 */
265 boot_init_stack_canary();
266#endif
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500267 arch_cpu_idle_prepare();
268 cpu_idle_loop();
269}