Disintegrate asm/system.h for PowerPC
[linux-2.6.git] / arch / powerpc / platforms / pseries / processor_idle.c
blob41a34bc4a9a2903e3d9bb747176cfde009f16435
1 /*
2 * processor_idle - idle state cpuidle driver.
3 * Adapted from drivers/idle/intel_idle.c and
4 * drivers/acpi/processor_idle.c
6 */
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>
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>
21 #include "plpar_wrappers.h"
22 #include "pseries.h"
24 struct cpuidle_driver pseries_idle_driver = {
25 .name = "pseries_idle",
26 .owner = THIS_MODULE,
29 #define MAX_IDLE_STATE_COUNT 2
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;
35 void update_smt_snooze_delay(int snooze)
37 struct cpuidle_driver *drv = cpuidle_get_driver();
38 if (drv)
39 drv->states[0].target_residency = snooze;
42 static inline void idle_loop_prolog(unsigned long *in_purr, ktime_t *kt_before)
45 *kt_before = ktime_get_real();
46 *in_purr = mfspr(SPRN_PURR);
48 * Indicate to the HV that we are idle. Now would be
49 * a good time to find other work to dispatch.
51 get_lppaca()->idle = 1;
54 static inline s64 idle_loop_epilog(unsigned long in_purr, ktime_t kt_before)
56 get_lppaca()->wait_state_cycles += mfspr(SPRN_PURR) - in_purr;
57 get_lppaca()->idle = 0;
59 return ktime_to_us(ktime_sub(ktime_get_real(), kt_before));
62 static int snooze_loop(struct cpuidle_device *dev,
63 struct cpuidle_driver *drv,
64 int index)
66 unsigned long in_purr;
67 ktime_t kt_before;
68 unsigned long start_snooze;
69 long snooze = drv->states[0].target_residency;
71 idle_loop_prolog(&in_purr, &kt_before);
73 if (snooze) {
74 start_snooze = get_tb() + snooze * tb_ticks_per_usec;
75 local_irq_enable();
76 set_thread_flag(TIF_POLLING_NRFLAG);
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();
86 HMT_medium();
87 clear_thread_flag(TIF_POLLING_NRFLAG);
88 smp_mb();
89 local_irq_disable();
92 out:
93 HMT_medium();
94 dev->last_residency =
95 (int)idle_loop_epilog(in_purr, kt_before);
96 return index;
99 static void check_and_cede_processor(void)
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.
108 hard_irq_disable();
109 if (get_paca()->irq_happened == 0)
110 cede_processor();
113 static int dedicated_cede_loop(struct cpuidle_device *dev,
114 struct cpuidle_driver *drv,
115 int index)
117 unsigned long in_purr;
118 ktime_t kt_before;
120 idle_loop_prolog(&in_purr, &kt_before);
121 get_lppaca()->donate_dedicated_cpu = 1;
123 ppc64_runlatch_off();
124 HMT_medium();
125 check_and_cede_processor();
127 get_lppaca()->donate_dedicated_cpu = 0;
128 dev->last_residency =
129 (int)idle_loop_epilog(in_purr, kt_before);
130 return index;
133 static int shared_cede_loop(struct cpuidle_device *dev,
134 struct cpuidle_driver *drv,
135 int index)
137 unsigned long in_purr;
138 ktime_t kt_before;
140 idle_loop_prolog(&in_purr, &kt_before);
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.
149 check_and_cede_processor();
151 dev->last_residency =
152 (int)idle_loop_epilog(in_purr, kt_before);
153 return index;
157 * States for dedicated partition case.
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 },
177 * States for shared partition case.
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 },
189 int pseries_notify_cpuidle_add_cpu(int cpu)
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);
197 return 0;
201 * pseries_cpuidle_driver_init()
203 static int pseries_cpuidle_driver_init(void)
205 int idle_state;
206 struct cpuidle_driver *drv = &pseries_idle_driver;
208 drv->state_count = 0;
210 for (idle_state = 0; idle_state < MAX_IDLE_STATE_COUNT; ++idle_state) {
212 if (idle_state > max_idle_state)
213 break;
215 /* is the state not enabled? */
216 if (cpuidle_state_table[idle_state].enter == NULL)
217 continue;
219 drv->states[drv->state_count] = /* structure copy */
220 cpuidle_state_table[idle_state];
222 if (cpuidle_state_table == dedicated_states)
223 drv->states[drv->state_count].target_residency =
224 __get_cpu_var(smt_snooze_delay);
226 drv->state_count += 1;
229 return 0;
232 /* pseries_idle_devices_uninit(void)
233 * unregister cpuidle devices and de-allocate memory
235 static void pseries_idle_devices_uninit(void)
237 int i;
238 struct cpuidle_device *dev;
240 for_each_possible_cpu(i) {
241 dev = per_cpu_ptr(pseries_cpuidle_devices, i);
242 cpuidle_unregister_device(dev);
245 free_percpu(pseries_cpuidle_devices);
246 return;
249 /* pseries_idle_devices_init()
250 * allocate, initialize and register cpuidle device
252 static int pseries_idle_devices_init(void)
254 int i;
255 struct cpuidle_driver *drv = &pseries_idle_driver;
256 struct cpuidle_device *dev;
258 pseries_cpuidle_devices = alloc_percpu(struct cpuidle_device);
259 if (pseries_cpuidle_devices == NULL)
260 return -ENOMEM;
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;
273 return 0;
277 * pseries_idle_probe()
278 * Choose state table for shared versus dedicated partition
280 static int pseries_idle_probe(void)
283 if (!firmware_has_feature(FW_FEATURE_SPLPAR))
284 return -ENODEV;
286 if (cpuidle_disable != IDLE_NO_OVERRIDE)
287 return -ENODEV;
289 if (max_idle_state == 0) {
290 printk(KERN_DEBUG "pseries processor idle disabled.\n");
291 return -EPERM;
294 if (get_lppaca()->shared_proc)
295 cpuidle_state_table = shared_states;
296 else
297 cpuidle_state_table = dedicated_states;
299 return 0;
302 static int __init pseries_processor_idle_init(void)
304 int retval;
306 retval = pseries_idle_probe();
307 if (retval)
308 return retval;
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;
317 retval = pseries_idle_devices_init();
318 if (retval) {
319 pseries_idle_devices_uninit();
320 cpuidle_unregister_driver(&pseries_idle_driver);
321 return retval;
324 printk(KERN_DEBUG "pseries_idle_driver registered\n");
326 return 0;
329 static void __exit pseries_processor_idle_exit(void)
332 pseries_idle_devices_uninit();
333 cpuidle_unregister_driver(&pseries_idle_driver);
335 return;
338 module_init(pseries_processor_idle_init);
339 module_exit(pseries_processor_idle_exit);
341 MODULE_AUTHOR("Deepthi Dharwar <deepthi@linux.vnet.ibm.com>");
342 MODULE_DESCRIPTION("Cpuidle driver for POWER");
343 MODULE_LICENSE("GPL");