powerpc: Fixup oddity in entry_32.S
[deliverable/linux.git] / arch / powerpc / platforms / pseries / processor_idle.c
CommitLineData
707827f3
DD
1/*
2 * processor_idle - idle state cpuidle driver.
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>
ae3a197e 20#include <asm/runlatch.h>
707827f3
DD
21
22#include "plpar_wrappers.h"
23#include "pseries.h"
24
25struct cpuidle_driver pseries_idle_driver = {
26 .name = "pseries_idle",
27 .owner = THIS_MODULE,
28};
29
30#define MAX_IDLE_STATE_COUNT 2
31
32static int max_idle_state = MAX_IDLE_STATE_COUNT - 1;
33static struct cpuidle_device __percpu *pseries_cpuidle_devices;
34static struct cpuidle_state *cpuidle_state_table;
35
36void update_smt_snooze_delay(int snooze)
37{
38 struct cpuidle_driver *drv = cpuidle_get_driver();
39 if (drv)
40 drv->states[0].target_residency = snooze;
41}
42
43static inline void idle_loop_prolog(unsigned long *in_purr, ktime_t *kt_before)
44{
45
46 *kt_before = ktime_get_real();
47 *in_purr = mfspr(SPRN_PURR);
48 /*
49 * Indicate to the HV that we are idle. Now would be
50 * a good time to find other work to dispatch.
51 */
52 get_lppaca()->idle = 1;
53}
54
55static inline s64 idle_loop_epilog(unsigned long in_purr, ktime_t kt_before)
56{
57 get_lppaca()->wait_state_cycles += mfspr(SPRN_PURR) - in_purr;
58 get_lppaca()->idle = 0;
59
60 return ktime_to_us(ktime_sub(ktime_get_real(), kt_before));
61}
62
63static int snooze_loop(struct cpuidle_device *dev,
64 struct cpuidle_driver *drv,
65 int index)
66{
67 unsigned long in_purr;
68 ktime_t kt_before;
69 unsigned long start_snooze;
70 long snooze = drv->states[0].target_residency;
71
72 idle_loop_prolog(&in_purr, &kt_before);
73
74 if (snooze) {
75 start_snooze = get_tb() + snooze * tb_ticks_per_usec;
76 local_irq_enable();
77 set_thread_flag(TIF_POLLING_NRFLAG);
78
79 while ((snooze < 0) || (get_tb() < start_snooze)) {
80 if (need_resched() || cpu_is_offline(dev->cpu))
81 goto out;
82 ppc64_runlatch_off();
83 HMT_low();
84 HMT_very_low();
85 }
86
87 HMT_medium();
88 clear_thread_flag(TIF_POLLING_NRFLAG);
89 smp_mb();
90 local_irq_disable();
91 }
92
93out:
94 HMT_medium();
95 dev->last_residency =
96 (int)idle_loop_epilog(in_purr, kt_before);
97 return index;
98}
99
7230c564
BH
100static void check_and_cede_processor(void)
101{
102 /*
be2cf20a
BH
103 * Ensure our interrupt state is properly tracked,
104 * also checks if no interrupt has occurred while we
105 * were soft-disabled
7230c564 106 */
be2cf20a 107 if (prep_irq_for_idle()) {
7230c564 108 cede_processor();
be2cf20a
BH
109#ifdef CONFIG_TRACE_IRQFLAGS
110 /* Ensure that H_CEDE returns with IRQs on */
111 if (WARN_ON(!(mfmsr() & MSR_EE)))
112 __hard_irq_enable();
113#endif
114 }
7230c564
BH
115}
116
707827f3
DD
117static int dedicated_cede_loop(struct cpuidle_device *dev,
118 struct cpuidle_driver *drv,
119 int index)
120{
121 unsigned long in_purr;
122 ktime_t kt_before;
123
124 idle_loop_prolog(&in_purr, &kt_before);
125 get_lppaca()->donate_dedicated_cpu = 1;
126
127 ppc64_runlatch_off();
128 HMT_medium();
7230c564 129 check_and_cede_processor();
707827f3
DD
130
131 get_lppaca()->donate_dedicated_cpu = 0;
132 dev->last_residency =
133 (int)idle_loop_epilog(in_purr, kt_before);
134 return index;
135}
136
137static int shared_cede_loop(struct cpuidle_device *dev,
138 struct cpuidle_driver *drv,
139 int index)
140{
141 unsigned long in_purr;
142 ktime_t kt_before;
143
144 idle_loop_prolog(&in_purr, &kt_before);
145
146 /*
147 * Yield the processor to the hypervisor. We return if
148 * an external interrupt occurs (which are driven prior
149 * to returning here) or if a prod occurs from another
150 * processor. When returning here, external interrupts
151 * are enabled.
152 */
7230c564 153 check_and_cede_processor();
707827f3
DD
154
155 dev->last_residency =
156 (int)idle_loop_epilog(in_purr, kt_before);
157 return index;
158}
159
160/*
161 * States for dedicated partition case.
162 */
163static struct cpuidle_state dedicated_states[MAX_IDLE_STATE_COUNT] = {
164 { /* Snooze */
165 .name = "snooze",
166 .desc = "snooze",
167 .flags = CPUIDLE_FLAG_TIME_VALID,
168 .exit_latency = 0,
169 .target_residency = 0,
170 .enter = &snooze_loop },
171 { /* CEDE */
172 .name = "CEDE",
173 .desc = "CEDE",
174 .flags = CPUIDLE_FLAG_TIME_VALID,
175 .exit_latency = 1,
176 .target_residency = 10,
177 .enter = &dedicated_cede_loop },
178};
179
180/*
181 * States for shared partition case.
182 */
183static struct cpuidle_state shared_states[MAX_IDLE_STATE_COUNT] = {
184 { /* Shared Cede */
185 .name = "Shared Cede",
186 .desc = "Shared Cede",
187 .flags = CPUIDLE_FLAG_TIME_VALID,
188 .exit_latency = 0,
189 .target_residency = 0,
190 .enter = &shared_cede_loop },
191};
192
16aaaff6
DD
193static int pseries_cpuidle_add_cpu_notifier(struct notifier_block *n,
194 unsigned long action, void *hcpu)
707827f3 195{
16aaaff6 196 int hotcpu = (unsigned long)hcpu;
707827f3 197 struct cpuidle_device *dev =
16aaaff6
DD
198 per_cpu_ptr(pseries_cpuidle_devices, hotcpu);
199
200 switch (action & 0xf) {
201 case CPU_ONLINE:
202 if (dev && cpuidle_get_driver()) {
203 cpuidle_disable_device(dev);
204 cpuidle_enable_device(dev);
205 }
206 break;
707827f3 207 }
16aaaff6 208 return NOTIFY_OK;
707827f3
DD
209}
210
16aaaff6
DD
211static struct notifier_block setup_hotplug_notifier = {
212 .notifier_call = pseries_cpuidle_add_cpu_notifier,
213};
214
707827f3
DD
215/*
216 * pseries_cpuidle_driver_init()
217 */
218static int pseries_cpuidle_driver_init(void)
219{
220 int idle_state;
221 struct cpuidle_driver *drv = &pseries_idle_driver;
222
223 drv->state_count = 0;
224
225 for (idle_state = 0; idle_state < MAX_IDLE_STATE_COUNT; ++idle_state) {
226
227 if (idle_state > max_idle_state)
228 break;
229
230 /* is the state not enabled? */
231 if (cpuidle_state_table[idle_state].enter == NULL)
232 continue;
233
234 drv->states[drv->state_count] = /* structure copy */
235 cpuidle_state_table[idle_state];
236
237 if (cpuidle_state_table == dedicated_states)
238 drv->states[drv->state_count].target_residency =
239 __get_cpu_var(smt_snooze_delay);
240
241 drv->state_count += 1;
242 }
243
244 return 0;
245}
246
247/* pseries_idle_devices_uninit(void)
248 * unregister cpuidle devices and de-allocate memory
249 */
250static void pseries_idle_devices_uninit(void)
251{
252 int i;
253 struct cpuidle_device *dev;
254
255 for_each_possible_cpu(i) {
256 dev = per_cpu_ptr(pseries_cpuidle_devices, i);
257 cpuidle_unregister_device(dev);
258 }
259
260 free_percpu(pseries_cpuidle_devices);
261 return;
262}
263
264/* pseries_idle_devices_init()
265 * allocate, initialize and register cpuidle device
266 */
267static int pseries_idle_devices_init(void)
268{
269 int i;
270 struct cpuidle_driver *drv = &pseries_idle_driver;
271 struct cpuidle_device *dev;
272
273 pseries_cpuidle_devices = alloc_percpu(struct cpuidle_device);
274 if (pseries_cpuidle_devices == NULL)
275 return -ENOMEM;
276
277 for_each_possible_cpu(i) {
278 dev = per_cpu_ptr(pseries_cpuidle_devices, i);
279 dev->state_count = drv->state_count;
280 dev->cpu = i;
281 if (cpuidle_register_device(dev)) {
282 printk(KERN_DEBUG \
283 "cpuidle_register_device %d failed!\n", i);
284 return -EIO;
285 }
286 }
287
288 return 0;
289}
290
291/*
292 * pseries_idle_probe()
293 * Choose state table for shared versus dedicated partition
294 */
295static int pseries_idle_probe(void)
296{
297
298 if (!firmware_has_feature(FW_FEATURE_SPLPAR))
299 return -ENODEV;
300
e8bb3e00
DD
301 if (cpuidle_disable != IDLE_NO_OVERRIDE)
302 return -ENODEV;
303
707827f3
DD
304 if (max_idle_state == 0) {
305 printk(KERN_DEBUG "pseries processor idle disabled.\n");
306 return -EPERM;
307 }
308
309 if (get_lppaca()->shared_proc)
310 cpuidle_state_table = shared_states;
311 else
312 cpuidle_state_table = dedicated_states;
313
314 return 0;
315}
316
317static int __init pseries_processor_idle_init(void)
318{
319 int retval;
320
321 retval = pseries_idle_probe();
322 if (retval)
323 return retval;
324
325 pseries_cpuidle_driver_init();
326 retval = cpuidle_register_driver(&pseries_idle_driver);
327 if (retval) {
328 printk(KERN_DEBUG "Registration of pseries driver failed.\n");
329 return retval;
330 }
331
332 retval = pseries_idle_devices_init();
333 if (retval) {
334 pseries_idle_devices_uninit();
335 cpuidle_unregister_driver(&pseries_idle_driver);
336 return retval;
337 }
338
16aaaff6 339 register_cpu_notifier(&setup_hotplug_notifier);
707827f3
DD
340 printk(KERN_DEBUG "pseries_idle_driver registered\n");
341
342 return 0;
343}
344
345static void __exit pseries_processor_idle_exit(void)
346{
347
348 pseries_idle_devices_uninit();
349 cpuidle_unregister_driver(&pseries_idle_driver);
350
351 return;
352}
353
354module_init(pseries_processor_idle_init);
355module_exit(pseries_processor_idle_exit);
356
357MODULE_AUTHOR("Deepthi Dharwar <deepthi@linux.vnet.ibm.com>");
358MODULE_DESCRIPTION("Cpuidle driver for POWER");
359MODULE_LICENSE("GPL");
This page took 0.086771 seconds and 5 git commands to generate.