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