blob: 3243eb23e8427f3065fa47b599d386f38be3a997 [file] [log] [blame]
Paul Mundt1da11802008-11-26 15:52:44 +09001/*
2 * The idle loop for all SuperH platforms.
3 *
Paul Mundt2e046b92009-06-19 14:40:51 +09004 * Copyright (C) 2002 - 2009 Paul Mundt
Paul Mundt1da11802008-11-26 15:52:44 +09005 *
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>
Paul Mundt2e046b92009-06-19 14:40:51 +090018#include <linux/smp.h>
Paul Mundt1da11802008-11-26 15:52:44 +090019#include <asm/pgalloc.h>
20#include <asm/system.h>
21#include <asm/atomic.h>
22
23static int hlt_counter;
Paul Mundtf533c3d2009-10-16 17:20:58 +090024void (*pm_idle)(void) = NULL;
Paul Mundt1da11802008-11-26 15:52:44 +090025void (*pm_power_off)(void);
26EXPORT_SYMBOL(pm_power_off);
27
28static int __init nohlt_setup(char *__unused)
29{
30 hlt_counter = 1;
31 return 1;
32}
33__setup("nohlt", nohlt_setup);
34
35static int __init hlt_setup(char *__unused)
36{
37 hlt_counter = 0;
38 return 1;
39}
40__setup("hlt", hlt_setup);
41
Paul Mundtf533c3d2009-10-16 17:20:58 +090042static inline int hlt_works(void)
Paul Mundt1da11802008-11-26 15:52:44 +090043{
Paul Mundtf533c3d2009-10-16 17:20:58 +090044 return !hlt_counter;
Paul Mundt1da11802008-11-26 15:52:44 +090045}
46
Paul Mundtf533c3d2009-10-16 17:20:58 +090047/*
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 */
52static void poll_idle(void)
53{
54 local_irq_enable();
55 while (!need_resched())
56 cpu_relax();
57}
58
59void 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 }
69
70 set_thread_flag(TIF_POLLING_NRFLAG);
71 } else
72 poll_idle();
73}
74
75/*
76 * The idle thread. There's no useful work to be done, so just try to conserve
77 * power and have a low exit latency (ie sit in a loop waiting for somebody to
78 * say that they'd like to reschedule)
79 */
Paul Mundt1da11802008-11-26 15:52:44 +090080void cpu_idle(void)
81{
Paul Mundtf533c3d2009-10-16 17:20:58 +090082 unsigned int cpu = smp_processor_id();
83
Paul Mundt1da11802008-11-26 15:52:44 +090084 set_thread_flag(TIF_POLLING_NRFLAG);
85
86 /* endless idle loop with no priority at all */
87 while (1) {
Paul Mundt1da11802008-11-26 15:52:44 +090088 tick_nohz_stop_sched_tick(1);
Paul Mundt1da11802008-11-26 15:52:44 +090089
Paul Mundtf533c3d2009-10-16 17:20:58 +090090 while (!need_resched() && cpu_online(cpu)) {
Paul Mundt0e6d4982009-10-16 17:27:58 +090091 check_pgt_cache();
92 rmb();
93
Paul Mundtf533c3d2009-10-16 17:20:58 +090094 local_irq_disable();
95 /* Don't trace irqs off for idle */
96 stop_critical_timings();
97 pm_idle();
98 /*
99 * Sanity check to ensure that pm_idle() returns
100 * with IRQs enabled
101 */
102 WARN_ON(irqs_disabled());
103 start_critical_timings();
104 }
105
106 tick_nohz_restart_sched_tick();
Paul Mundt1da11802008-11-26 15:52:44 +0900107 preempt_enable_no_resched();
108 schedule();
109 preempt_disable();
Paul Mundt1da11802008-11-26 15:52:44 +0900110 }
111}
Paul Mundt2e046b92009-06-19 14:40:51 +0900112
Paul Mundtf533c3d2009-10-16 17:20:58 +0900113void __cpuinit select_idle_routine(void)
114{
115 /*
116 * If a platform has set its own idle routine, leave it alone.
117 */
118 if (pm_idle)
119 return;
120
121 if (hlt_works())
122 pm_idle = default_idle;
123 else
124 pm_idle = poll_idle;
125}
126
Paul Mundt2e046b92009-06-19 14:40:51 +0900127static void do_nothing(void *unused)
128{
129}
130
131/*
132 * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
133 * pm_idle and update to new pm_idle value. Required while changing pm_idle
134 * handler on SMP systems.
135 *
136 * Caller must have changed pm_idle to the new value before the call. Old
137 * pm_idle value will not be used by any CPU after the return of this function.
138 */
139void cpu_idle_wait(void)
140{
141 smp_mb();
142 /* kick all the CPUs so that they exit out of pm_idle */
143 smp_call_function(do_nothing, NULL, 1);
144}
145EXPORT_SYMBOL_GPL(cpu_idle_wait);