2 * cpuidle-pseries - idle state cpuidle driver.
3 * Adapted from drivers/idle/intel_idle.c and
4 * drivers/acpi/processor_idle.c
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>
18 #include <asm/machdep.h>
19 #include <asm/firmware.h>
20 #include <asm/plpar_wrappers.h>
22 struct cpuidle_driver pseries_idle_driver
= {
23 .name
= "pseries_idle",
27 #define MAX_IDLE_STATE_COUNT 2
29 static int max_idle_state
= MAX_IDLE_STATE_COUNT
- 1;
30 static struct cpuidle_state
*cpuidle_state_table
;
32 static inline void idle_loop_prolog(unsigned long *in_purr
)
34 *in_purr
= mfspr(SPRN_PURR
);
36 * Indicate to the HV that we are idle. Now would be
37 * a good time to find other work to dispatch.
39 get_lppaca()->idle
= 1;
42 static inline void idle_loop_epilog(unsigned long in_purr
)
46 wait_cycles
= be64_to_cpu(get_lppaca()->wait_state_cycles
);
47 wait_cycles
+= mfspr(SPRN_PURR
) - in_purr
;
48 get_lppaca()->wait_state_cycles
= cpu_to_be64(wait_cycles
);
49 get_lppaca()->idle
= 0;
52 static int snooze_loop(struct cpuidle_device
*dev
,
53 struct cpuidle_driver
*drv
,
56 unsigned long in_purr
;
58 idle_loop_prolog(&in_purr
);
60 set_thread_flag(TIF_POLLING_NRFLAG
);
62 while (!need_resched()) {
68 clear_thread_flag(TIF_POLLING_NRFLAG
);
71 idle_loop_epilog(in_purr
);
76 static void check_and_cede_processor(void)
79 * Ensure our interrupt state is properly tracked,
80 * also checks if no interrupt has occurred while we
83 if (prep_irq_for_idle()) {
85 #ifdef CONFIG_TRACE_IRQFLAGS
86 /* Ensure that H_CEDE returns with IRQs on */
87 if (WARN_ON(!(mfmsr() & MSR_EE
)))
93 static int dedicated_cede_loop(struct cpuidle_device
*dev
,
94 struct cpuidle_driver
*drv
,
97 unsigned long in_purr
;
99 idle_loop_prolog(&in_purr
);
100 get_lppaca()->donate_dedicated_cpu
= 1;
103 check_and_cede_processor();
105 get_lppaca()->donate_dedicated_cpu
= 0;
107 idle_loop_epilog(in_purr
);
112 static int shared_cede_loop(struct cpuidle_device
*dev
,
113 struct cpuidle_driver
*drv
,
116 unsigned long in_purr
;
118 idle_loop_prolog(&in_purr
);
121 * Yield the processor to the hypervisor. We return if
122 * an external interrupt occurs (which are driven prior
123 * to returning here) or if a prod occurs from another
124 * processor. When returning here, external interrupts
127 check_and_cede_processor();
129 idle_loop_epilog(in_purr
);
135 * States for dedicated partition case.
137 static struct cpuidle_state dedicated_states
[MAX_IDLE_STATE_COUNT
] = {
141 .flags
= CPUIDLE_FLAG_TIME_VALID
,
143 .target_residency
= 0,
144 .enter
= &snooze_loop
},
148 .flags
= CPUIDLE_FLAG_TIME_VALID
,
150 .target_residency
= 100,
151 .enter
= &dedicated_cede_loop
},
155 * States for shared partition case.
157 static struct cpuidle_state shared_states
[MAX_IDLE_STATE_COUNT
] = {
159 .name
= "Shared Cede",
160 .desc
= "Shared Cede",
161 .flags
= CPUIDLE_FLAG_TIME_VALID
,
163 .target_residency
= 0,
164 .enter
= &shared_cede_loop
},
167 void update_smt_snooze_delay(int cpu
, int residency
)
169 struct cpuidle_driver
*drv
= cpuidle_get_driver();
170 struct cpuidle_device
*dev
= per_cpu(cpuidle_devices
, cpu
);
172 if (cpuidle_state_table
!= dedicated_states
)
176 /* Disable the Nap state on that cpu */
178 dev
->states_usage
[1].disable
= 1;
181 drv
->states
[1].target_residency
= residency
;
184 static int pseries_cpuidle_add_cpu_notifier(struct notifier_block
*n
,
185 unsigned long action
, void *hcpu
)
187 int hotcpu
= (unsigned long)hcpu
;
188 struct cpuidle_device
*dev
=
189 per_cpu(cpuidle_devices
, hotcpu
);
191 if (dev
&& cpuidle_get_driver()) {
194 case CPU_ONLINE_FROZEN
:
195 cpuidle_pause_and_lock();
196 cpuidle_enable_device(dev
);
197 cpuidle_resume_and_unlock();
201 case CPU_DEAD_FROZEN
:
202 cpuidle_pause_and_lock();
203 cpuidle_disable_device(dev
);
204 cpuidle_resume_and_unlock();
214 static struct notifier_block setup_hotplug_notifier
= {
215 .notifier_call
= pseries_cpuidle_add_cpu_notifier
,
219 * pseries_cpuidle_driver_init()
221 static int pseries_cpuidle_driver_init(void)
224 struct cpuidle_driver
*drv
= &pseries_idle_driver
;
226 drv
->state_count
= 0;
228 for (idle_state
= 0; idle_state
< MAX_IDLE_STATE_COUNT
; ++idle_state
) {
230 if (idle_state
> max_idle_state
)
233 /* is the state not enabled? */
234 if (cpuidle_state_table
[idle_state
].enter
== NULL
)
237 drv
->states
[drv
->state_count
] = /* structure copy */
238 cpuidle_state_table
[idle_state
];
240 drv
->state_count
+= 1;
247 * pseries_idle_probe()
248 * Choose state table for shared versus dedicated partition
250 static int pseries_idle_probe(void)
253 if (cpuidle_disable
!= IDLE_NO_OVERRIDE
)
256 if (max_idle_state
== 0) {
257 printk(KERN_DEBUG
"pseries processor idle disabled.\n");
261 if (firmware_has_feature(FW_FEATURE_SPLPAR
)) {
262 if (lppaca_shared_proc(get_lppaca()))
263 cpuidle_state_table
= shared_states
;
265 cpuidle_state_table
= dedicated_states
;
272 static int __init
pseries_processor_idle_init(void)
276 retval
= pseries_idle_probe();
280 pseries_cpuidle_driver_init();
281 retval
= cpuidle_register(&pseries_idle_driver
, NULL
);
283 printk(KERN_DEBUG
"Registration of pseries driver failed.\n");
287 register_cpu_notifier(&setup_hotplug_notifier
);
288 printk(KERN_DEBUG
"pseries_idle_driver registered\n");
292 static void __exit
pseries_processor_idle_exit(void)
295 unregister_cpu_notifier(&setup_hotplug_notifier
);
296 cpuidle_unregister(&pseries_idle_driver
);
300 module_init(pseries_processor_idle_init
);
301 module_exit(pseries_processor_idle_exit
);
303 MODULE_AUTHOR("Deepthi Dharwar <deepthi@linux.vnet.ibm.com>");
304 MODULE_DESCRIPTION("Cpuidle driver for POWER");
305 MODULE_LICENSE("GPL");