xref: /linux/arch/sh/kernel/idle.c (revision 273b281fa22c293963ee3e6eec418f5dda2dbc83)
1 /*
2  * The idle loop for all SuperH platforms.
3  *
4  *  Copyright (C) 2002 - 2009  Paul Mundt
5  *
6  * This file is subject to the terms and conditions of the GNU General Public
7  * License.  See the file "COPYING" in the main directory of this archive
8  * for more details.
9  */
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/mm.h>
13 #include <linux/pm.h>
14 #include <linux/tick.h>
15 #include <linux/preempt.h>
16 #include <linux/thread_info.h>
17 #include <linux/irqflags.h>
18 #include <linux/smp.h>
19 #include <asm/pgalloc.h>
20 #include <asm/system.h>
21 #include <asm/atomic.h>
22 
23 static int hlt_counter;
24 void (*pm_idle)(void) = NULL;
25 void (*pm_power_off)(void);
26 EXPORT_SYMBOL(pm_power_off);
27 
28 static int __init nohlt_setup(char *__unused)
29 {
30 	hlt_counter = 1;
31 	return 1;
32 }
33 __setup("nohlt", nohlt_setup);
34 
35 static int __init hlt_setup(char *__unused)
36 {
37 	hlt_counter = 0;
38 	return 1;
39 }
40 __setup("hlt", hlt_setup);
41 
42 static inline int hlt_works(void)
43 {
44 	return !hlt_counter;
45 }
46 
47 /*
48  * On SMP it's slightly faster (but much more power-consuming!)
49  * to poll the ->work.need_resched flag instead of waiting for the
50  * cross-CPU IPI to arrive. Use this option with caution.
51  */
52 static void poll_idle(void)
53 {
54 	local_irq_enable();
55 	while (!need_resched())
56 		cpu_relax();
57 }
58 
59 void default_idle(void)
60 {
61 	if (hlt_works()) {
62 		clear_thread_flag(TIF_POLLING_NRFLAG);
63 		smp_mb__after_clear_bit();
64 
65 		if (!need_resched()) {
66 			local_irq_enable();
67 			cpu_sleep();
68 		} else
69 			local_irq_enable();
70 
71 		set_thread_flag(TIF_POLLING_NRFLAG);
72 	} else
73 		poll_idle();
74 }
75 
76 /*
77  * The idle thread. There's no useful work to be done, so just try to conserve
78  * power and have a low exit latency (ie sit in a loop waiting for somebody to
79  * say that they'd like to reschedule)
80  */
81 void cpu_idle(void)
82 {
83 	unsigned int cpu = smp_processor_id();
84 
85 	set_thread_flag(TIF_POLLING_NRFLAG);
86 
87 	/* endless idle loop with no priority at all */
88 	while (1) {
89 		tick_nohz_stop_sched_tick(1);
90 
91 		while (!need_resched() && cpu_online(cpu)) {
92 			check_pgt_cache();
93 			rmb();
94 
95 			local_irq_disable();
96 			/* Don't trace irqs off for idle */
97 			stop_critical_timings();
98 			pm_idle();
99 			/*
100 			 * Sanity check to ensure that pm_idle() returns
101 			 * with IRQs enabled
102 			 */
103 			WARN_ON(irqs_disabled());
104 			start_critical_timings();
105 		}
106 
107 		tick_nohz_restart_sched_tick();
108 		preempt_enable_no_resched();
109 		schedule();
110 		preempt_disable();
111 	}
112 }
113 
114 void __cpuinit select_idle_routine(void)
115 {
116 	/*
117 	 * If a platform has set its own idle routine, leave it alone.
118 	 */
119 	if (pm_idle)
120 		return;
121 
122 	if (hlt_works())
123 		pm_idle = default_idle;
124 	else
125 		pm_idle = poll_idle;
126 }
127 
128 static void do_nothing(void *unused)
129 {
130 }
131 
132 /*
133  * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
134  * pm_idle and update to new pm_idle value. Required while changing pm_idle
135  * handler on SMP systems.
136  *
137  * Caller must have changed pm_idle to the new value before the call. Old
138  * pm_idle value will not be used by any CPU after the return of this function.
139  */
140 void cpu_idle_wait(void)
141 {
142 	smp_mb();
143 	/* kick all the CPUs so that they exit out of pm_idle */
144 	smp_call_function(do_nothing, NULL, 1);
145 }
146 EXPORT_SYMBOL_GPL(cpu_idle_wait);
147