blob: cf75f00f7037701bef7b2d7afadf08295032e5a5 [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>
Thomas Gleixner8df3e072016-02-26 18:43:41 +00007#include <linux/cpuhotplug.h>
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -05008#include <linux/tick.h>
9#include <linux/mm.h>
10#include <linux/stackprotector.h>
Rafael J. Wysocki38106312015-02-12 23:33:15 +010011#include <linux/suspend.h>
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -050012
13#include <asm/tlb.h>
14
15#include <trace/events/power.h>
16
Peter Zijlstrae3baac42014-06-04 10:31:18 -070017#include "sched.h"
18
Chris Metcalf6727ad92016-10-07 17:02:55 -070019/* Linker adds these: start and end of __cpuidle functions */
20extern char __cpuidle_text_start[], __cpuidle_text_end[];
21
Rafael J. Wysockifaad3842015-05-10 01:18:03 +020022/**
23 * sched_idle_set_state - Record idle state for the current CPU.
24 * @idle_state: State to record.
25 */
Morten Rasmussen06910642015-01-27 13:48:07 +000026void sched_idle_set_state(struct cpuidle_state *idle_state, int index)
Rafael J. Wysockifaad3842015-05-10 01:18:03 +020027{
28 idle_set_state(this_rq(), idle_state);
Morten Rasmussen06910642015-01-27 13:48:07 +000029 idle_set_state_idx(this_rq(), index);
Rafael J. Wysockifaad3842015-05-10 01:18:03 +020030}
31
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -050032static int __read_mostly cpu_idle_force_poll;
33
34void cpu_idle_poll_ctrl(bool enable)
35{
36 if (enable) {
37 cpu_idle_force_poll++;
38 } else {
39 cpu_idle_force_poll--;
40 WARN_ON_ONCE(cpu_idle_force_poll < 0);
41 }
42}
43
44#ifdef CONFIG_GENERIC_IDLE_POLL_SETUP
45static int __init cpu_idle_poll_setup(char *__unused)
46{
47 cpu_idle_force_poll = 1;
48 return 1;
49}
50__setup("nohlt", cpu_idle_poll_setup);
51
52static int __init cpu_idle_nopoll_setup(char *__unused)
53{
54 cpu_idle_force_poll = 0;
55 return 1;
56}
57__setup("hlt", cpu_idle_nopoll_setup);
58#endif
59
Chris Metcalf6727ad92016-10-07 17:02:55 -070060static noinline int __cpuidle cpu_idle_poll(void)
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -050061{
62 rcu_idle_enter();
63 trace_cpu_idle_rcuidle(0, smp_processor_id());
64 local_irq_enable();
Daniel Bristot de Oliveira9babcd72015-10-08 15:36:06 -030065 stop_critical_timings();
Preeti U Murthyff6f2d22015-01-21 16:27:25 +053066 while (!tif_need_resched() &&
67 (cpu_idle_force_poll || tick_check_broadcast_expired()))
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -050068 cpu_relax();
Daniel Bristot de Oliveira9babcd72015-10-08 15:36:06 -030069 start_critical_timings();
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -050070 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
71 rcu_idle_exit();
72 return 1;
73}
74
75/* Weak implementations for optional arch specific functions */
76void __weak arch_cpu_idle_prepare(void) { }
77void __weak arch_cpu_idle_enter(void) { }
78void __weak arch_cpu_idle_exit(void) { }
79void __weak arch_cpu_idle_dead(void) { }
80void __weak arch_cpu_idle(void)
81{
82 cpu_idle_force_poll = 1;
83 local_irq_enable();
84}
85
Rafael J. Wysocki827a5ae2015-05-10 01:18:46 +020086/**
87 * default_idle_call - Default CPU idle routine.
88 *
89 * To use when the cpuidle framework cannot be used.
90 */
Chris Metcalf6727ad92016-10-07 17:02:55 -070091void __cpuidle default_idle_call(void)
Rafael J. Wysocki82f66322015-05-04 22:53:22 +020092{
Lucas Stach63caae82015-07-20 18:34:50 +020093 if (current_clr_polling_and_test()) {
Rafael J. Wysocki82f66322015-05-04 22:53:22 +020094 local_irq_enable();
Lucas Stach63caae82015-07-20 18:34:50 +020095 } else {
96 stop_critical_timings();
Rafael J. Wysocki82f66322015-05-04 22:53:22 +020097 arch_cpu_idle();
Lucas Stach63caae82015-07-20 18:34:50 +020098 start_critical_timings();
99 }
Rafael J. Wysocki82f66322015-05-04 22:53:22 +0200100}
101
Rafael J. Wysockibcf6ad82015-05-04 22:53:35 +0200102static int call_cpuidle(struct cpuidle_driver *drv, struct cpuidle_device *dev,
103 int next_state)
104{
Rafael J. Wysockibcf6ad82015-05-04 22:53:35 +0200105 /*
106 * The idle task must be scheduled, it is pointless to go to idle, just
107 * update no idle residency and return.
108 */
109 if (current_clr_polling_and_test()) {
110 dev->last_residency = 0;
111 local_irq_enable();
112 return -EBUSY;
113 }
114
Rafael J. Wysockibcf6ad82015-05-04 22:53:35 +0200115 /*
116 * Enter the idle state previously returned by the governor decision.
117 * This function will block until an interrupt occurs and will take
118 * care of re-enabling the local interrupts
119 */
Rafael J. Wysocki827a5ae2015-05-10 01:18:46 +0200120 return cpuidle_enter(drv, dev, next_state);
Rafael J. Wysockibcf6ad82015-05-04 22:53:35 +0200121}
122
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100123/**
124 * cpuidle_idle_call - the main idle function
125 *
126 * NOTE: no locks or semaphores should be used here
Andy Lutomirski82c65d62014-06-04 10:31:16 -0700127 *
128 * On archs that support TIF_POLLING_NRFLAG, is called with polling
129 * set, and it returns with polling set. If it ever stops polling, it
130 * must clear the polling bit.
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100131 */
Rafael J. Wysocki08c373e2014-04-21 01:26:58 +0200132static void cpuidle_idle_call(void)
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100133{
Catalin Marinas9bd616e2016-06-01 18:52:16 +0100134 struct cpuidle_device *dev = cpuidle_get_device();
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100135 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
Peter Zijlstra37352272014-04-11 13:55:48 +0200136 int next_state, entered_state;
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100137
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100138 /*
139 * Check if the idle task must be rescheduled. If it is the
Peter Zijlstrac4441172014-04-11 13:47:16 +0200140 * case, exit the function after re-enabling the local irq.
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100141 */
Peter Zijlstrac4441172014-04-11 13:47:16 +0200142 if (need_resched()) {
Daniel Lezcano8ca3c642014-03-03 08:48:53 +0100143 local_irq_enable();
Rafael J. Wysocki08c373e2014-04-21 01:26:58 +0200144 return;
Daniel Lezcano8ca3c642014-03-03 08:48:53 +0100145 }
146
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100147 /*
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100148 * Tell the RCU framework we are entering an idle section,
149 * so no more rcu read side critical sections and one more
150 * step to the grace period
151 */
Daniel Lezcanoc8cc7d42014-03-03 08:48:52 +0100152 rcu_idle_enter();
153
Rafael J. Wysocki82f66322015-05-04 22:53:22 +0200154 if (cpuidle_not_available(drv, dev)) {
155 default_idle_call();
156 goto exit_idle;
157 }
Rafael J. Wysockief2b22a2015-03-02 22:26:55 +0100158
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100159 /*
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100160 * Suspend-to-idle ("freeze") is a system state in which all user space
161 * has been frozen, all I/O devices have been suspended and the only
162 * activity happens here and in iterrupts (if any). In that case bypass
163 * the cpuidle governor and go stratight for the deepest idle state
164 * available. Possibly also suspend the local tick and the entire
165 * timekeeping to prevent timer interrupts from kicking us out of idle
166 * until a proper wakeup interrupt happens.
167 */
168 if (idle_should_freeze()) {
Rafael J. Wysockief2b22a2015-03-02 22:26:55 +0100169 entered_state = cpuidle_enter_freeze(drv, dev);
Sudeep Holla6f168862016-01-21 11:19:29 +0000170 if (entered_state > 0) {
Rafael J. Wysockief2b22a2015-03-02 22:26:55 +0100171 local_irq_enable();
172 goto exit_idle;
173 }
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100174
Rafael J. Wysockief2b22a2015-03-02 22:26:55 +0100175 next_state = cpuidle_find_deepest_state(drv, dev);
Rafael J. Wysockibcf6ad82015-05-04 22:53:35 +0200176 call_cpuidle(drv, dev, next_state);
Rafael J. Wysockief2b22a2015-03-02 22:26:55 +0100177 } else {
Rafael J. Wysockief2b22a2015-03-02 22:26:55 +0100178 /*
179 * Ask the cpuidle framework to choose a convenient idle state.
180 */
181 next_state = cpuidle_select(drv, dev);
Rafael J. Wysockibcf6ad82015-05-04 22:53:35 +0200182 entered_state = call_cpuidle(drv, dev, next_state);
183 /*
184 * Give the governor an opportunity to reflect on the outcome
185 */
Rafael J. Wysockief2b22a2015-03-02 22:26:55 +0100186 cpuidle_reflect(dev, entered_state);
Rafael J. Wysockibcf6ad82015-05-04 22:53:35 +0200187 }
Peter Zijlstra37352272014-04-11 13:55:48 +0200188
189exit_idle:
Daniel Lezcano8ca3c642014-03-03 08:48:53 +0100190 __current_set_polling();
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100191
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100192 /*
Peter Zijlstra37352272014-04-11 13:55:48 +0200193 * It is up to the idle functions to reenable local interrupts
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100194 */
Daniel Lezcanoc8cc7d42014-03-03 08:48:52 +0100195 if (WARN_ON_ONCE(irqs_disabled()))
196 local_irq_enable();
197
198 rcu_idle_exit();
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100199}
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100200
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500201/*
202 * Generic idle loop implementation
Andy Lutomirski82c65d62014-06-04 10:31:16 -0700203 *
204 * Called with polling cleared.
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500205 */
206static void cpu_idle_loop(void)
207{
Gaurav Jindal (Gaurav Jindal)df55f462016-05-12 10:13:33 +0000208 int cpu = smp_processor_id();
209
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500210 while (1) {
Andy Lutomirski82c65d62014-06-04 10:31:16 -0700211 /*
212 * If the arch has a polling bit, we maintain an invariant:
213 *
214 * Our polling bit is clear if we're not scheduled (i.e. if
215 * rq->curr != rq->idle). This means that, if rq->idle has
216 * the polling bit set, then setting need_resched is
217 * guaranteed to cause the cpu to reschedule.
218 */
219
220 __current_set_polling();
Christoph Lameter0eb77e92016-01-14 15:21:40 -0800221 quiet_vmstat();
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500222 tick_nohz_idle_enter();
223
224 while (!need_resched()) {
225 check_pgt_cache();
226 rmb();
227
Gaurav Jindal (Gaurav Jindal)df55f462016-05-12 10:13:33 +0000228 if (cpu_is_offline(cpu)) {
Thomas Gleixnere69aab12016-02-26 18:43:43 +0000229 cpuhp_report_idle_dead();
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500230 arch_cpu_idle_dead();
Paul E. McKenney528a25b2015-01-28 14:09:43 -0800231 }
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500232
233 local_irq_disable();
234 arch_cpu_idle_enter();
235
236 /*
237 * In poll mode we reenable interrupts and spin.
238 *
239 * Also if we detected in the wakeup from idle
240 * path that the tick broadcast device expired
241 * for us, we don't want to go deep idle as we
242 * know that the IPI is going to arrive right
243 * away
244 */
Daniel Lezcano8ca3c642014-03-03 08:48:53 +0100245 if (cpu_idle_force_poll || tick_check_broadcast_expired())
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500246 cpu_idle_poll();
Daniel Lezcano8ca3c642014-03-03 08:48:53 +0100247 else
248 cpuidle_idle_call();
249
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500250 arch_cpu_idle_exit();
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500251 }
Peter Zijlstra06d50c62014-02-24 18:22:07 +0100252
253 /*
254 * Since we fell out of the loop above, we know
255 * TIF_NEED_RESCHED must be set, propagate it into
256 * PREEMPT_NEED_RESCHED.
257 *
258 * This is required because for polling idle loops we will
259 * not have had an IPI to fold the state for us.
260 */
261 preempt_set_need_resched();
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500262 tick_nohz_idle_exit();
Andy Lutomirski82c65d62014-06-04 10:31:16 -0700263 __current_clr_polling();
264
265 /*
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700266 * We promise to call sched_ttwu_pending and reschedule
267 * if need_resched is set while polling is set. That
268 * means that clearing polling needs to be visible
269 * before doing these things.
Andy Lutomirski82c65d62014-06-04 10:31:16 -0700270 */
271 smp_mb__after_atomic();
272
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700273 sched_ttwu_pending();
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500274 schedule_preempt_disabled();
275 }
276}
277
Chris Metcalf6727ad92016-10-07 17:02:55 -0700278bool cpu_in_idle(unsigned long pc)
279{
280 return pc >= (unsigned long)__cpuidle_text_start &&
281 pc < (unsigned long)__cpuidle_text_end;
282}
283
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500284void cpu_startup_entry(enum cpuhp_state state)
285{
286 /*
287 * This #ifdef needs to die, but it's too late in the cycle to
288 * make this generic (arm and sh have never invoked the canary
289 * init for the non boot cpus!). Will be fixed in 3.11
290 */
291#ifdef CONFIG_X86
292 /*
293 * If we're the non-boot CPU, nothing set the stack canary up
294 * for us. The boot CPU already has it initialized but no harm
295 * in doing it again. This is a good place for updating it, as
296 * we wont ever return from this function (so the invalid
297 * canaries already on the stack wont ever trigger).
298 */
299 boot_init_stack_canary();
300#endif
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500301 arch_cpu_idle_prepare();
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000302 cpuhp_online_idle(state);
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500303 cpu_idle_loop();
304}