Merge tag 'iwlwifi-for-kalle-2016-02-15' of https://git.kernel.org/pub/scm/linux...
[deliverable/linux.git] / kernel / sched / idle.c
CommitLineData
cf37b6b4
NP
1/*
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>
38106313 10#include <linux/suspend.h>
cf37b6b4
NP
11
12#include <asm/tlb.h>
13
14#include <trace/events/power.h>
15
e3baac47
PZ
16#include "sched.h"
17
faad3849
RW
18/**
19 * sched_idle_set_state - Record idle state for the current CPU.
20 * @idle_state: State to record.
21 */
22void sched_idle_set_state(struct cpuidle_state *idle_state)
23{
24 idle_set_state(this_rq(), idle_state);
25}
26
cf37b6b4
NP
27static int __read_mostly cpu_idle_force_poll;
28
29void cpu_idle_poll_ctrl(bool enable)
30{
31 if (enable) {
32 cpu_idle_force_poll++;
33 } else {
34 cpu_idle_force_poll--;
35 WARN_ON_ONCE(cpu_idle_force_poll < 0);
36 }
37}
38
39#ifdef CONFIG_GENERIC_IDLE_POLL_SETUP
40static int __init cpu_idle_poll_setup(char *__unused)
41{
42 cpu_idle_force_poll = 1;
43 return 1;
44}
45__setup("nohlt", cpu_idle_poll_setup);
46
47static int __init cpu_idle_nopoll_setup(char *__unused)
48{
49 cpu_idle_force_poll = 0;
50 return 1;
51}
52__setup("hlt", cpu_idle_nopoll_setup);
53#endif
54
55static inline int cpu_idle_poll(void)
56{
57 rcu_idle_enter();
58 trace_cpu_idle_rcuidle(0, smp_processor_id());
59 local_irq_enable();
9babcd79 60 stop_critical_timings();
ff6f2d29
PM
61 while (!tif_need_resched() &&
62 (cpu_idle_force_poll || tick_check_broadcast_expired()))
cf37b6b4 63 cpu_relax();
9babcd79 64 start_critical_timings();
cf37b6b4
NP
65 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
66 rcu_idle_exit();
67 return 1;
68}
69
70/* Weak implementations for optional arch specific functions */
71void __weak arch_cpu_idle_prepare(void) { }
72void __weak arch_cpu_idle_enter(void) { }
73void __weak arch_cpu_idle_exit(void) { }
74void __weak arch_cpu_idle_dead(void) { }
75void __weak arch_cpu_idle(void)
76{
77 cpu_idle_force_poll = 1;
78 local_irq_enable();
79}
80
827a5aef
RW
81/**
82 * default_idle_call - Default CPU idle routine.
83 *
84 * To use when the cpuidle framework cannot be used.
85 */
86void default_idle_call(void)
82f66327 87{
63caae84 88 if (current_clr_polling_and_test()) {
82f66327 89 local_irq_enable();
63caae84
LS
90 } else {
91 stop_critical_timings();
82f66327 92 arch_cpu_idle();
63caae84
LS
93 start_critical_timings();
94 }
82f66327
RW
95}
96
bcf6ad8a
RW
97static int call_cpuidle(struct cpuidle_driver *drv, struct cpuidle_device *dev,
98 int next_state)
99{
bcf6ad8a
RW
100 /* Fall back to the default arch idle method on errors. */
101 if (next_state < 0) {
102 default_idle_call();
103 return next_state;
104 }
105
106 /*
107 * The idle task must be scheduled, it is pointless to go to idle, just
108 * update no idle residency and return.
109 */
110 if (current_clr_polling_and_test()) {
111 dev->last_residency = 0;
112 local_irq_enable();
113 return -EBUSY;
114 }
115
bcf6ad8a
RW
116 /*
117 * Enter the idle state previously returned by the governor decision.
118 * This function will block until an interrupt occurs and will take
119 * care of re-enabling the local interrupts
120 */
827a5aef 121 return cpuidle_enter(drv, dev, next_state);
bcf6ad8a
RW
122}
123
30cdd69e
DL
124/**
125 * cpuidle_idle_call - the main idle function
126 *
127 * NOTE: no locks or semaphores should be used here
82c65d60
AL
128 *
129 * On archs that support TIF_POLLING_NRFLAG, is called with polling
130 * set, and it returns with polling set. If it ever stops polling, it
131 * must clear the polling bit.
30cdd69e 132 */
08c373e5 133static void cpuidle_idle_call(void)
30cdd69e
DL
134{
135 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
136 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
37352273 137 int next_state, entered_state;
30cdd69e 138
a1d028bd
DL
139 /*
140 * Check if the idle task must be rescheduled. If it is the
c444117f 141 * case, exit the function after re-enabling the local irq.
a1d028bd 142 */
c444117f 143 if (need_resched()) {
8ca3c642 144 local_irq_enable();
08c373e5 145 return;
8ca3c642
DL
146 }
147
a1d028bd
DL
148 /*
149 * Tell the RCU framework we are entering an idle section,
150 * so no more rcu read side critical sections and one more
151 * step to the grace period
152 */
c8cc7d4d
DL
153 rcu_idle_enter();
154
82f66327
RW
155 if (cpuidle_not_available(drv, dev)) {
156 default_idle_call();
157 goto exit_idle;
158 }
ef2b22ac 159
38106313
RW
160 /*
161 * Suspend-to-idle ("freeze") is a system state in which all user space
162 * has been frozen, all I/O devices have been suspended and the only
163 * activity happens here and in iterrupts (if any). In that case bypass
164 * the cpuidle governor and go stratight for the deepest idle state
165 * available. Possibly also suspend the local tick and the entire
166 * timekeeping to prevent timer interrupts from kicking us out of idle
167 * until a proper wakeup interrupt happens.
168 */
169 if (idle_should_freeze()) {
ef2b22ac
RW
170 entered_state = cpuidle_enter_freeze(drv, dev);
171 if (entered_state >= 0) {
172 local_irq_enable();
173 goto exit_idle;
174 }
175
ef2b22ac 176 next_state = cpuidle_find_deepest_state(drv, dev);
bcf6ad8a 177 call_cpuidle(drv, dev, next_state);
ef2b22ac 178 } else {
ef2b22ac
RW
179 /*
180 * Ask the cpuidle framework to choose a convenient idle state.
181 */
182 next_state = cpuidle_select(drv, dev);
bcf6ad8a
RW
183 entered_state = call_cpuidle(drv, dev, next_state);
184 /*
185 * Give the governor an opportunity to reflect on the outcome
186 */
ef2b22ac 187 cpuidle_reflect(dev, entered_state);
bcf6ad8a 188 }
37352273
PZ
189
190exit_idle:
8ca3c642 191 __current_set_polling();
30cdd69e 192
a1d028bd 193 /*
37352273 194 * It is up to the idle functions to reenable local interrupts
a1d028bd 195 */
c8cc7d4d
DL
196 if (WARN_ON_ONCE(irqs_disabled()))
197 local_irq_enable();
198
199 rcu_idle_exit();
30cdd69e 200}
30cdd69e 201
528a25b0
PM
202DEFINE_PER_CPU(bool, cpu_dead_idle);
203
cf37b6b4
NP
204/*
205 * Generic idle loop implementation
82c65d60
AL
206 *
207 * Called with polling cleared.
cf37b6b4
NP
208 */
209static void cpu_idle_loop(void)
210{
211 while (1) {
82c65d60
AL
212 /*
213 * If the arch has a polling bit, we maintain an invariant:
214 *
215 * Our polling bit is clear if we're not scheduled (i.e. if
216 * rq->curr != rq->idle). This means that, if rq->idle has
217 * the polling bit set, then setting need_resched is
218 * guaranteed to cause the cpu to reschedule.
219 */
220
221 __current_set_polling();
0eb77e98 222 quiet_vmstat();
cf37b6b4
NP
223 tick_nohz_idle_enter();
224
225 while (!need_resched()) {
226 check_pgt_cache();
227 rmb();
228
528a25b0 229 if (cpu_is_offline(smp_processor_id())) {
88428cc5
PM
230 rcu_cpu_notify(NULL, CPU_DYING_IDLE,
231 (void *)(long)smp_processor_id());
528a25b0
PM
232 smp_mb(); /* all activity before dead. */
233 this_cpu_write(cpu_dead_idle, true);
cf37b6b4 234 arch_cpu_idle_dead();
528a25b0 235 }
cf37b6b4
NP
236
237 local_irq_disable();
238 arch_cpu_idle_enter();
239
240 /*
241 * In poll mode we reenable interrupts and spin.
242 *
243 * Also if we detected in the wakeup from idle
244 * path that the tick broadcast device expired
245 * for us, we don't want to go deep idle as we
246 * know that the IPI is going to arrive right
247 * away
248 */
8ca3c642 249 if (cpu_idle_force_poll || tick_check_broadcast_expired())
cf37b6b4 250 cpu_idle_poll();
8ca3c642
DL
251 else
252 cpuidle_idle_call();
253
cf37b6b4 254 arch_cpu_idle_exit();
cf37b6b4 255 }
06d50c65
PZ
256
257 /*
258 * Since we fell out of the loop above, we know
259 * TIF_NEED_RESCHED must be set, propagate it into
260 * PREEMPT_NEED_RESCHED.
261 *
262 * This is required because for polling idle loops we will
263 * not have had an IPI to fold the state for us.
264 */
265 preempt_set_need_resched();
cf37b6b4 266 tick_nohz_idle_exit();
82c65d60
AL
267 __current_clr_polling();
268
269 /*
e3baac47
PZ
270 * We promise to call sched_ttwu_pending and reschedule
271 * if need_resched is set while polling is set. That
272 * means that clearing polling needs to be visible
273 * before doing these things.
82c65d60
AL
274 */
275 smp_mb__after_atomic();
276
e3baac47 277 sched_ttwu_pending();
cf37b6b4
NP
278 schedule_preempt_disabled();
279 }
280}
281
282void cpu_startup_entry(enum cpuhp_state state)
283{
284 /*
285 * This #ifdef needs to die, but it's too late in the cycle to
286 * make this generic (arm and sh have never invoked the canary
287 * init for the non boot cpus!). Will be fixed in 3.11
288 */
289#ifdef CONFIG_X86
290 /*
291 * If we're the non-boot CPU, nothing set the stack canary up
292 * for us. The boot CPU already has it initialized but no harm
293 * in doing it again. This is a good place for updating it, as
294 * we wont ever return from this function (so the invalid
295 * canaries already on the stack wont ever trigger).
296 */
297 boot_init_stack_canary();
298#endif
cf37b6b4
NP
299 arch_cpu_idle_prepare();
300 cpu_idle_loop();
301}
This page took 0.144245 seconds and 5 git commands to generate.