]> Pileus Git - ~andy/linux/blob - kernel/cpu/idle.c
Merge branch 'for-next-merge' of git://git.kernel.org/pub/scm/linux/kernel/git/nab...
[~andy/linux] / kernel / cpu / idle.c
1 /*
2  * Generic entry point for the idle threads
3  */
4 #include <linux/sched.h>
5 #include <linux/cpu.h>
6 #include <linux/tick.h>
7 #include <linux/mm.h>
8
9 #include <asm/tlb.h>
10
11 #include <trace/events/power.h>
12
13 static int __read_mostly cpu_idle_force_poll;
14
15 void cpu_idle_poll_ctrl(bool enable)
16 {
17         if (enable) {
18                 cpu_idle_force_poll++;
19         } else {
20                 cpu_idle_force_poll--;
21                 WARN_ON_ONCE(cpu_idle_force_poll < 0);
22         }
23 }
24
25 #ifdef CONFIG_GENERIC_IDLE_POLL_SETUP
26 static int __init cpu_idle_poll_setup(char *__unused)
27 {
28         cpu_idle_force_poll = 1;
29         return 1;
30 }
31 __setup("nohlt", cpu_idle_poll_setup);
32
33 static int __init cpu_idle_nopoll_setup(char *__unused)
34 {
35         cpu_idle_force_poll = 0;
36         return 1;
37 }
38 __setup("hlt", cpu_idle_nopoll_setup);
39 #endif
40
41 static inline int cpu_idle_poll(void)
42 {
43         trace_cpu_idle_rcuidle(0, smp_processor_id());
44         local_irq_enable();
45         while (!need_resched())
46                 cpu_relax();
47         trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
48         return 1;
49 }
50
51 /* Weak implementations for optional arch specific functions */
52 void __weak arch_cpu_idle_prepare(void) { }
53 void __weak arch_cpu_idle_enter(void) { }
54 void __weak arch_cpu_idle_exit(void) { }
55 void __weak arch_cpu_idle_dead(void) { }
56 void __weak arch_cpu_idle(void)
57 {
58         cpu_idle_force_poll = 1;
59 }
60
61 /*
62  * Generic idle loop implementation
63  */
64 static void cpu_idle_loop(void)
65 {
66         while (1) {
67                 tick_nohz_idle_enter();
68
69                 while (!need_resched()) {
70                         check_pgt_cache();
71                         rmb();
72
73                         if (cpu_is_offline(smp_processor_id()))
74                                 arch_cpu_idle_dead();
75
76                         local_irq_disable();
77                         arch_cpu_idle_enter();
78
79                         /*
80                          * In poll mode we reenable interrupts and spin.
81                          *
82                          * Also if we detected in the wakeup from idle
83                          * path that the tick broadcast device expired
84                          * for us, we don't want to go deep idle as we
85                          * know that the IPI is going to arrive right
86                          * away
87                          */
88                         if (cpu_idle_force_poll || tick_check_broadcast_expired()) {
89                                 cpu_idle_poll();
90                         } else {
91                                 current_clr_polling();
92                                 if (!need_resched()) {
93                                         stop_critical_timings();
94                                         rcu_idle_enter();
95                                         arch_cpu_idle();
96                                         WARN_ON_ONCE(irqs_disabled());
97                                         rcu_idle_exit();
98                                         start_critical_timings();
99                                 } else {
100                                         local_irq_enable();
101                                 }
102                                 current_set_polling();
103                         }
104                         arch_cpu_idle_exit();
105                 }
106                 tick_nohz_idle_exit();
107                 schedule_preempt_disabled();
108         }
109 }
110
111 void cpu_startup_entry(enum cpuhp_state state)
112 {
113         current_set_polling();
114         arch_cpu_idle_prepare();
115         cpu_idle_loop();
116 }