]> Pileus Git - ~andy/linux/blob - arch/powerpc/platforms/pseries/processor_idle.c
regulator: extend the fixed dummy voltage regulator to accept voltage
[~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          * Interrupts are soft-disabled at this point,
103          * but not hard disabled. So an interrupt might have
104          * occurred before entering NAP, and would be potentially
105          * lost (edge events, decrementer events, etc...) unless
106          * we first hard disable then check.
107          */
108         hard_irq_disable();
109         if (get_paca()->irq_happened == 0)
110                 cede_processor();
111 }
112
113 static int dedicated_cede_loop(struct cpuidle_device *dev,
114                                 struct cpuidle_driver *drv,
115                                 int index)
116 {
117         unsigned long in_purr;
118         ktime_t kt_before;
119
120         idle_loop_prolog(&in_purr, &kt_before);
121         get_lppaca()->donate_dedicated_cpu = 1;
122
123         ppc64_runlatch_off();
124         HMT_medium();
125         check_and_cede_processor();
126
127         get_lppaca()->donate_dedicated_cpu = 0;
128         dev->last_residency =
129                 (int)idle_loop_epilog(in_purr, kt_before);
130         return index;
131 }
132
133 static int shared_cede_loop(struct cpuidle_device *dev,
134                         struct cpuidle_driver *drv,
135                         int index)
136 {
137         unsigned long in_purr;
138         ktime_t kt_before;
139
140         idle_loop_prolog(&in_purr, &kt_before);
141
142         /*
143          * Yield the processor to the hypervisor.  We return if
144          * an external interrupt occurs (which are driven prior
145          * to returning here) or if a prod occurs from another
146          * processor. When returning here, external interrupts
147          * are enabled.
148          */
149         check_and_cede_processor();
150
151         dev->last_residency =
152                 (int)idle_loop_epilog(in_purr, kt_before);
153         return index;
154 }
155
156 /*
157  * States for dedicated partition case.
158  */
159 static struct cpuidle_state dedicated_states[MAX_IDLE_STATE_COUNT] = {
160         { /* Snooze */
161                 .name = "snooze",
162                 .desc = "snooze",
163                 .flags = CPUIDLE_FLAG_TIME_VALID,
164                 .exit_latency = 0,
165                 .target_residency = 0,
166                 .enter = &snooze_loop },
167         { /* CEDE */
168                 .name = "CEDE",
169                 .desc = "CEDE",
170                 .flags = CPUIDLE_FLAG_TIME_VALID,
171                 .exit_latency = 1,
172                 .target_residency = 10,
173                 .enter = &dedicated_cede_loop },
174 };
175
176 /*
177  * States for shared partition case.
178  */
179 static struct cpuidle_state shared_states[MAX_IDLE_STATE_COUNT] = {
180         { /* Shared Cede */
181                 .name = "Shared Cede",
182                 .desc = "Shared Cede",
183                 .flags = CPUIDLE_FLAG_TIME_VALID,
184                 .exit_latency = 0,
185                 .target_residency = 0,
186                 .enter = &shared_cede_loop },
187 };
188
189 int pseries_notify_cpuidle_add_cpu(int cpu)
190 {
191         struct cpuidle_device *dev =
192                         per_cpu_ptr(pseries_cpuidle_devices, cpu);
193         if (dev && cpuidle_get_driver()) {
194                 cpuidle_disable_device(dev);
195                 cpuidle_enable_device(dev);
196         }
197         return 0;
198 }
199
200 /*
201  * pseries_cpuidle_driver_init()
202  */
203 static int pseries_cpuidle_driver_init(void)
204 {
205         int idle_state;
206         struct cpuidle_driver *drv = &pseries_idle_driver;
207
208         drv->state_count = 0;
209
210         for (idle_state = 0; idle_state < MAX_IDLE_STATE_COUNT; ++idle_state) {
211
212                 if (idle_state > max_idle_state)
213                         break;
214
215                 /* is the state not enabled? */
216                 if (cpuidle_state_table[idle_state].enter == NULL)
217                         continue;
218
219                 drv->states[drv->state_count] = /* structure copy */
220                         cpuidle_state_table[idle_state];
221
222                 if (cpuidle_state_table == dedicated_states)
223                         drv->states[drv->state_count].target_residency =
224                                 __get_cpu_var(smt_snooze_delay);
225
226                 drv->state_count += 1;
227         }
228
229         return 0;
230 }
231
232 /* pseries_idle_devices_uninit(void)
233  * unregister cpuidle devices and de-allocate memory
234  */
235 static void pseries_idle_devices_uninit(void)
236 {
237         int i;
238         struct cpuidle_device *dev;
239
240         for_each_possible_cpu(i) {
241                 dev = per_cpu_ptr(pseries_cpuidle_devices, i);
242                 cpuidle_unregister_device(dev);
243         }
244
245         free_percpu(pseries_cpuidle_devices);
246         return;
247 }
248
249 /* pseries_idle_devices_init()
250  * allocate, initialize and register cpuidle device
251  */
252 static int pseries_idle_devices_init(void)
253 {
254         int i;
255         struct cpuidle_driver *drv = &pseries_idle_driver;
256         struct cpuidle_device *dev;
257
258         pseries_cpuidle_devices = alloc_percpu(struct cpuidle_device);
259         if (pseries_cpuidle_devices == NULL)
260                 return -ENOMEM;
261
262         for_each_possible_cpu(i) {
263                 dev = per_cpu_ptr(pseries_cpuidle_devices, i);
264                 dev->state_count = drv->state_count;
265                 dev->cpu = i;
266                 if (cpuidle_register_device(dev)) {
267                         printk(KERN_DEBUG \
268                                 "cpuidle_register_device %d failed!\n", i);
269                         return -EIO;
270                 }
271         }
272
273         return 0;
274 }
275
276 /*
277  * pseries_idle_probe()
278  * Choose state table for shared versus dedicated partition
279  */
280 static int pseries_idle_probe(void)
281 {
282
283         if (!firmware_has_feature(FW_FEATURE_SPLPAR))
284                 return -ENODEV;
285
286         if (cpuidle_disable != IDLE_NO_OVERRIDE)
287                 return -ENODEV;
288
289         if (max_idle_state == 0) {
290                 printk(KERN_DEBUG "pseries processor idle disabled.\n");
291                 return -EPERM;
292         }
293
294         if (get_lppaca()->shared_proc)
295                 cpuidle_state_table = shared_states;
296         else
297                 cpuidle_state_table = dedicated_states;
298
299         return 0;
300 }
301
302 static int __init pseries_processor_idle_init(void)
303 {
304         int retval;
305
306         retval = pseries_idle_probe();
307         if (retval)
308                 return retval;
309
310         pseries_cpuidle_driver_init();
311         retval = cpuidle_register_driver(&pseries_idle_driver);
312         if (retval) {
313                 printk(KERN_DEBUG "Registration of pseries driver failed.\n");
314                 return retval;
315         }
316
317         retval = pseries_idle_devices_init();
318         if (retval) {
319                 pseries_idle_devices_uninit();
320                 cpuidle_unregister_driver(&pseries_idle_driver);
321                 return retval;
322         }
323
324         printk(KERN_DEBUG "pseries_idle_driver registered\n");
325
326         return 0;
327 }
328
329 static void __exit pseries_processor_idle_exit(void)
330 {
331
332         pseries_idle_devices_uninit();
333         cpuidle_unregister_driver(&pseries_idle_driver);
334
335         return;
336 }
337
338 module_init(pseries_processor_idle_init);
339 module_exit(pseries_processor_idle_exit);
340
341 MODULE_AUTHOR("Deepthi Dharwar <deepthi@linux.vnet.ibm.com>");
342 MODULE_DESCRIPTION("Cpuidle driver for POWER");
343 MODULE_LICENSE("GPL");