Merge branches 'pm-cpufreq' and 'pm-cpuidle'
[linux-2.6-block.git] / drivers / cpuidle / cpuidle-pseries.c
CommitLineData
707827f3 1/*
962e7bd4 2 * cpuidle-pseries - idle state cpuidle driver.
707827f3
DD
3 * Adapted from drivers/idle/intel_idle.c and
4 * drivers/acpi/processor_idle.c
5 *
6 */
7
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/moduleparam.h>
12#include <linux/cpuidle.h>
13#include <linux/cpu.h>
16aaaff6 14#include <linux/notifier.h>
707827f3
DD
15
16#include <asm/paca.h>
17#include <asm/reg.h>
707827f3
DD
18#include <asm/machdep.h>
19#include <asm/firmware.h>
3f67d962 20#include <asm/runlatch.h>
212bebb4 21#include <asm/plpar_wrappers.h>
707827f3
DD
22
23struct cpuidle_driver pseries_idle_driver = {
1ca80944
DL
24 .name = "pseries_idle",
25 .owner = THIS_MODULE,
707827f3
DD
26};
27
bf7f61f2 28static int max_idle_state;
707827f3
DD
29static struct cpuidle_state *cpuidle_state_table;
30
1ca80944 31static inline void idle_loop_prolog(unsigned long *in_purr)
707827f3 32{
d8c6ad31 33 ppc64_runlatch_off();
707827f3
DD
34 *in_purr = mfspr(SPRN_PURR);
35 /*
36 * Indicate to the HV that we are idle. Now would be
37 * a good time to find other work to dispatch.
38 */
39 get_lppaca()->idle = 1;
40}
41
1ca80944 42static inline void idle_loop_epilog(unsigned long in_purr)
707827f3 43{
7ffcf8ec
AB
44 u64 wait_cycles;
45
46 wait_cycles = be64_to_cpu(get_lppaca()->wait_state_cycles);
47 wait_cycles += mfspr(SPRN_PURR) - in_purr;
48 get_lppaca()->wait_state_cycles = cpu_to_be64(wait_cycles);
707827f3 49 get_lppaca()->idle = 0;
d8c6ad31
NP
50
51 if (irqs_disabled())
52 local_irq_enable();
53 ppc64_runlatch_on();
707827f3
DD
54}
55
56static int snooze_loop(struct cpuidle_device *dev,
57 struct cpuidle_driver *drv,
58 int index)
59{
60 unsigned long in_purr;
707827f3 61
1ca80944 62 idle_loop_prolog(&in_purr);
83dac594
DD
63 local_irq_enable();
64 set_thread_flag(TIF_POLLING_NRFLAG);
707827f3 65
b69dbba0 66 while (!need_resched()) {
83dac594
DD
67 HMT_low();
68 HMT_very_low();
707827f3
DD
69 }
70
707827f3 71 HMT_medium();
83dac594
DD
72 clear_thread_flag(TIF_POLLING_NRFLAG);
73 smp_mb();
74
1ca80944
DL
75 idle_loop_epilog(in_purr);
76
707827f3
DD
77 return index;
78}
79
7230c564
BH
80static void check_and_cede_processor(void)
81{
82 /*
be2cf20a
BH
83 * Ensure our interrupt state is properly tracked,
84 * also checks if no interrupt has occurred while we
85 * were soft-disabled
7230c564 86 */
be2cf20a 87 if (prep_irq_for_idle()) {
7230c564 88 cede_processor();
be2cf20a
BH
89#ifdef CONFIG_TRACE_IRQFLAGS
90 /* Ensure that H_CEDE returns with IRQs on */
91 if (WARN_ON(!(mfmsr() & MSR_EE)))
92 __hard_irq_enable();
93#endif
94 }
7230c564
BH
95}
96
707827f3
DD
97static int dedicated_cede_loop(struct cpuidle_device *dev,
98 struct cpuidle_driver *drv,
99 int index)
100{
101 unsigned long in_purr;
707827f3 102
1ca80944 103 idle_loop_prolog(&in_purr);
707827f3
DD
104 get_lppaca()->donate_dedicated_cpu = 1;
105
707827f3 106 HMT_medium();
7230c564 107 check_and_cede_processor();
707827f3
DD
108
109 get_lppaca()->donate_dedicated_cpu = 0;
1ca80944
DL
110
111 idle_loop_epilog(in_purr);
112
707827f3
DD
113 return index;
114}
115
116static int shared_cede_loop(struct cpuidle_device *dev,
117 struct cpuidle_driver *drv,
118 int index)
119{
120 unsigned long in_purr;
707827f3 121
1ca80944 122 idle_loop_prolog(&in_purr);
707827f3
DD
123
124 /*
125 * Yield the processor to the hypervisor. We return if
126 * an external interrupt occurs (which are driven prior
127 * to returning here) or if a prod occurs from another
128 * processor. When returning here, external interrupts
129 * are enabled.
130 */
7230c564 131 check_and_cede_processor();
707827f3 132
1ca80944
DL
133 idle_loop_epilog(in_purr);
134
707827f3
DD
135 return index;
136}
137
138/*
139 * States for dedicated partition case.
140 */
bf7f61f2 141static struct cpuidle_state dedicated_states[] = {
707827f3
DD
142 { /* Snooze */
143 .name = "snooze",
144 .desc = "snooze",
707827f3
DD
145 .exit_latency = 0,
146 .target_residency = 0,
147 .enter = &snooze_loop },
148 { /* CEDE */
149 .name = "CEDE",
150 .desc = "CEDE",
83dac594
DD
151 .exit_latency = 10,
152 .target_residency = 100,
707827f3
DD
153 .enter = &dedicated_cede_loop },
154};
155
156/*
157 * States for shared partition case.
158 */
bf7f61f2 159static struct cpuidle_state shared_states[] = {
707827f3
DD
160 { /* Shared Cede */
161 .name = "Shared Cede",
162 .desc = "Shared Cede",
707827f3
DD
163 .exit_latency = 0,
164 .target_residency = 0,
165 .enter = &shared_cede_loop },
166};
167
16aaaff6
DD
168static int pseries_cpuidle_add_cpu_notifier(struct notifier_block *n,
169 unsigned long action, void *hcpu)
707827f3 170{
16aaaff6 171 int hotcpu = (unsigned long)hcpu;
707827f3 172 struct cpuidle_device *dev =
b69dbba0 173 per_cpu(cpuidle_devices, hotcpu);
16aaaff6 174
852d8cb1
DD
175 if (dev && cpuidle_get_driver()) {
176 switch (action) {
177 case CPU_ONLINE:
178 case CPU_ONLINE_FROZEN:
179 cpuidle_pause_and_lock();
16aaaff6 180 cpuidle_enable_device(dev);
852d8cb1
DD
181 cpuidle_resume_and_unlock();
182 break;
183
184 case CPU_DEAD:
185 case CPU_DEAD_FROZEN:
186 cpuidle_pause_and_lock();
187 cpuidle_disable_device(dev);
188 cpuidle_resume_and_unlock();
189 break;
190
191 default:
192 return NOTIFY_DONE;
16aaaff6 193 }
707827f3 194 }
16aaaff6 195 return NOTIFY_OK;
707827f3
DD
196}
197
16aaaff6
DD
198static struct notifier_block setup_hotplug_notifier = {
199 .notifier_call = pseries_cpuidle_add_cpu_notifier,
200};
201
707827f3
DD
202/*
203 * pseries_cpuidle_driver_init()
204 */
205static int pseries_cpuidle_driver_init(void)
206{
207 int idle_state;
208 struct cpuidle_driver *drv = &pseries_idle_driver;
209
210 drv->state_count = 0;
211
bf7f61f2
DD
212 for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
213 /* Is the state not enabled? */
707827f3
DD
214 if (cpuidle_state_table[idle_state].enter == NULL)
215 continue;
216
217 drv->states[drv->state_count] = /* structure copy */
218 cpuidle_state_table[idle_state];
219
707827f3
DD
220 drv->state_count += 1;
221 }
222
223 return 0;
224}
225
707827f3
DD
226/*
227 * pseries_idle_probe()
228 * Choose state table for shared versus dedicated partition
229 */
230static int pseries_idle_probe(void)
231{
232
e8bb3e00
DD
233 if (cpuidle_disable != IDLE_NO_OVERRIDE)
234 return -ENODEV;
235
b69dbba0 236 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
bf7f61f2 237 if (lppaca_shared_proc(get_lppaca())) {
b69dbba0 238 cpuidle_state_table = shared_states;
bf7f61f2
DD
239 max_idle_state = ARRAY_SIZE(shared_states);
240 } else {
b69dbba0 241 cpuidle_state_table = dedicated_states;
bf7f61f2
DD
242 max_idle_state = ARRAY_SIZE(dedicated_states);
243 }
b69dbba0
DD
244 } else
245 return -ENODEV;
707827f3
DD
246
247 return 0;
248}
249
250static int __init pseries_processor_idle_init(void)
251{
252 int retval;
253
254 retval = pseries_idle_probe();
255 if (retval)
256 return retval;
257
258 pseries_cpuidle_driver_init();
b69dbba0 259 retval = cpuidle_register(&pseries_idle_driver, NULL);
707827f3
DD
260 if (retval) {
261 printk(KERN_DEBUG "Registration of pseries driver failed.\n");
262 return retval;
263 }
264
16aaaff6 265 register_cpu_notifier(&setup_hotplug_notifier);
707827f3 266 printk(KERN_DEBUG "pseries_idle_driver registered\n");
707827f3
DD
267 return 0;
268}
269
12431c64 270device_initcall(pseries_processor_idle_init);