Merge tag 'hwmon-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/groeck...
[pandora-kernel.git] / 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 #include <linux/notifier.h>
15
16 #include <asm/paca.h>
17 #include <asm/reg.h>
18 #include <asm/machdep.h>
19 #include <asm/firmware.h>
20 #include <asm/runlatch.h>
21
22 #include "plpar_wrappers.h"
23 #include "pseries.h"
24
25 struct cpuidle_driver pseries_idle_driver = {
26         .name =         "pseries_idle",
27         .owner =        THIS_MODULE,
28 };
29
30 #define MAX_IDLE_STATE_COUNT    2
31
32 static int max_idle_state = MAX_IDLE_STATE_COUNT - 1;
33 static struct cpuidle_device __percpu *pseries_cpuidle_devices;
34 static struct cpuidle_state *cpuidle_state_table;
35
36 void 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
43 static 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
55 static 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
63 static 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
93 out:
94         HMT_medium();
95         dev->last_residency =
96                 (int)idle_loop_epilog(in_purr, kt_before);
97         return index;
98 }
99
100 static void check_and_cede_processor(void)
101 {
102         /*
103          * Ensure our interrupt state is properly tracked,
104          * also checks if no interrupt has occurred while we
105          * were soft-disabled
106          */
107         if (prep_irq_for_idle()) {
108                 cede_processor();
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         }
115 }
116
117 static 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();
129         check_and_cede_processor();
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
137 static 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          */
153         check_and_cede_processor();
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  */
163 static 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  */
183 static 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
193 static int pseries_cpuidle_add_cpu_notifier(struct notifier_block *n,
194                         unsigned long action, void *hcpu)
195 {
196         int hotcpu = (unsigned long)hcpu;
197         struct cpuidle_device *dev =
198                         per_cpu_ptr(pseries_cpuidle_devices, hotcpu);
199
200         if (dev && cpuidle_get_driver()) {
201                 switch (action) {
202                 case CPU_ONLINE:
203                 case CPU_ONLINE_FROZEN:
204                         cpuidle_pause_and_lock();
205                         cpuidle_enable_device(dev);
206                         cpuidle_resume_and_unlock();
207                         break;
208
209                 case CPU_DEAD:
210                 case CPU_DEAD_FROZEN:
211                         cpuidle_pause_and_lock();
212                         cpuidle_disable_device(dev);
213                         cpuidle_resume_and_unlock();
214                         break;
215
216                 default:
217                         return NOTIFY_DONE;
218                 }
219         }
220         return NOTIFY_OK;
221 }
222
223 static struct notifier_block setup_hotplug_notifier = {
224         .notifier_call = pseries_cpuidle_add_cpu_notifier,
225 };
226
227 /*
228  * pseries_cpuidle_driver_init()
229  */
230 static int pseries_cpuidle_driver_init(void)
231 {
232         int idle_state;
233         struct cpuidle_driver *drv = &pseries_idle_driver;
234
235         drv->state_count = 0;
236
237         for (idle_state = 0; idle_state < MAX_IDLE_STATE_COUNT; ++idle_state) {
238
239                 if (idle_state > max_idle_state)
240                         break;
241
242                 /* is the state not enabled? */
243                 if (cpuidle_state_table[idle_state].enter == NULL)
244                         continue;
245
246                 drv->states[drv->state_count] = /* structure copy */
247                         cpuidle_state_table[idle_state];
248
249                 if (cpuidle_state_table == dedicated_states)
250                         drv->states[drv->state_count].target_residency =
251                                 __get_cpu_var(smt_snooze_delay);
252
253                 drv->state_count += 1;
254         }
255
256         return 0;
257 }
258
259 /* pseries_idle_devices_uninit(void)
260  * unregister cpuidle devices and de-allocate memory
261  */
262 static void pseries_idle_devices_uninit(void)
263 {
264         int i;
265         struct cpuidle_device *dev;
266
267         for_each_possible_cpu(i) {
268                 dev = per_cpu_ptr(pseries_cpuidle_devices, i);
269                 cpuidle_unregister_device(dev);
270         }
271
272         free_percpu(pseries_cpuidle_devices);
273         return;
274 }
275
276 /* pseries_idle_devices_init()
277  * allocate, initialize and register cpuidle device
278  */
279 static int pseries_idle_devices_init(void)
280 {
281         int i;
282         struct cpuidle_driver *drv = &pseries_idle_driver;
283         struct cpuidle_device *dev;
284
285         pseries_cpuidle_devices = alloc_percpu(struct cpuidle_device);
286         if (pseries_cpuidle_devices == NULL)
287                 return -ENOMEM;
288
289         for_each_possible_cpu(i) {
290                 dev = per_cpu_ptr(pseries_cpuidle_devices, i);
291                 dev->state_count = drv->state_count;
292                 dev->cpu = i;
293                 if (cpuidle_register_device(dev)) {
294                         printk(KERN_DEBUG \
295                                 "cpuidle_register_device %d failed!\n", i);
296                         return -EIO;
297                 }
298         }
299
300         return 0;
301 }
302
303 /*
304  * pseries_idle_probe()
305  * Choose state table for shared versus dedicated partition
306  */
307 static int pseries_idle_probe(void)
308 {
309
310         if (!firmware_has_feature(FW_FEATURE_SPLPAR))
311                 return -ENODEV;
312
313         if (cpuidle_disable != IDLE_NO_OVERRIDE)
314                 return -ENODEV;
315
316         if (max_idle_state == 0) {
317                 printk(KERN_DEBUG "pseries processor idle disabled.\n");
318                 return -EPERM;
319         }
320
321         if (get_lppaca()->shared_proc)
322                 cpuidle_state_table = shared_states;
323         else
324                 cpuidle_state_table = dedicated_states;
325
326         return 0;
327 }
328
329 static int __init pseries_processor_idle_init(void)
330 {
331         int retval;
332
333         retval = pseries_idle_probe();
334         if (retval)
335                 return retval;
336
337         pseries_cpuidle_driver_init();
338         retval = cpuidle_register_driver(&pseries_idle_driver);
339         if (retval) {
340                 printk(KERN_DEBUG "Registration of pseries driver failed.\n");
341                 return retval;
342         }
343
344         retval = pseries_idle_devices_init();
345         if (retval) {
346                 pseries_idle_devices_uninit();
347                 cpuidle_unregister_driver(&pseries_idle_driver);
348                 return retval;
349         }
350
351         register_cpu_notifier(&setup_hotplug_notifier);
352         printk(KERN_DEBUG "pseries_idle_driver registered\n");
353
354         return 0;
355 }
356
357 static void __exit pseries_processor_idle_exit(void)
358 {
359
360         unregister_cpu_notifier(&setup_hotplug_notifier);
361         pseries_idle_devices_uninit();
362         cpuidle_unregister_driver(&pseries_idle_driver);
363
364         return;
365 }
366
367 module_init(pseries_processor_idle_init);
368 module_exit(pseries_processor_idle_exit);
369
370 MODULE_AUTHOR("Deepthi Dharwar <deepthi@linux.vnet.ibm.com>");
371 MODULE_DESCRIPTION("Cpuidle driver for POWER");
372 MODULE_LICENSE("GPL");