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 static int max_idle_state;
28 static struct cpuidle_state *cpuidle_state_table;
30 static inline void idle_loop_prolog(unsigned long *in_purr)
32 *in_purr = mfspr(SPRN_PURR);
34 * Indicate to the HV that we are idle. Now would be
35 * a good time to find other work to dispatch.
37 get_lppaca()->idle = 1;
40 static inline void idle_loop_epilog(unsigned long in_purr)
44 wait_cycles = be64_to_cpu(get_lppaca()->wait_state_cycles);
45 wait_cycles += mfspr(SPRN_PURR) - in_purr;
46 get_lppaca()->wait_state_cycles = cpu_to_be64(wait_cycles);
47 get_lppaca()->idle = 0;
50 static int snooze_loop(struct cpuidle_device *dev,
51 struct cpuidle_driver *drv,
54 unsigned long in_purr;
56 idle_loop_prolog(&in_purr);
58 set_thread_flag(TIF_POLLING_NRFLAG);
60 while (!need_resched()) {
66 clear_thread_flag(TIF_POLLING_NRFLAG);
69 idle_loop_epilog(in_purr);
74 static void check_and_cede_processor(void)
77 * Ensure our interrupt state is properly tracked,
78 * also checks if no interrupt has occurred while we
81 if (prep_irq_for_idle()) {
83 #ifdef CONFIG_TRACE_IRQFLAGS
84 /* Ensure that H_CEDE returns with IRQs on */
85 if (WARN_ON(!(mfmsr() & MSR_EE)))
91 static int dedicated_cede_loop(struct cpuidle_device *dev,
92 struct cpuidle_driver *drv,
95 unsigned long in_purr;
97 idle_loop_prolog(&in_purr);
98 get_lppaca()->donate_dedicated_cpu = 1;
101 check_and_cede_processor();
103 get_lppaca()->donate_dedicated_cpu = 0;
105 idle_loop_epilog(in_purr);
110 static int shared_cede_loop(struct cpuidle_device *dev,
111 struct cpuidle_driver *drv,
114 unsigned long in_purr;
116 idle_loop_prolog(&in_purr);
119 * Yield the processor to the hypervisor. We return if
120 * an external interrupt occurs (which are driven prior
121 * to returning here) or if a prod occurs from another
122 * processor. When returning here, external interrupts
125 check_and_cede_processor();
127 idle_loop_epilog(in_purr);
133 * States for dedicated partition case.
135 static struct cpuidle_state dedicated_states[] = {
139 .flags = CPUIDLE_FLAG_TIME_VALID,
141 .target_residency = 0,
142 .enter = &snooze_loop },
146 .flags = CPUIDLE_FLAG_TIME_VALID,
148 .target_residency = 100,
149 .enter = &dedicated_cede_loop },
153 * States for shared partition case.
155 static struct cpuidle_state shared_states[] = {
157 .name = "Shared Cede",
158 .desc = "Shared Cede",
159 .flags = CPUIDLE_FLAG_TIME_VALID,
161 .target_residency = 0,
162 .enter = &shared_cede_loop },
165 void update_smt_snooze_delay(int cpu, int residency)
167 struct cpuidle_driver *drv = cpuidle_get_driver();
168 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
170 if (cpuidle_state_table != dedicated_states)
174 /* Disable the Nap state on that cpu */
176 dev->states_usage[1].disable = 1;
179 drv->states[1].target_residency = residency;
182 static int pseries_cpuidle_add_cpu_notifier(struct notifier_block *n,
183 unsigned long action, void *hcpu)
185 int hotcpu = (unsigned long)hcpu;
186 struct cpuidle_device *dev =
187 per_cpu(cpuidle_devices, hotcpu);
189 if (dev && cpuidle_get_driver()) {
192 case CPU_ONLINE_FROZEN:
193 cpuidle_pause_and_lock();
194 cpuidle_enable_device(dev);
195 cpuidle_resume_and_unlock();
199 case CPU_DEAD_FROZEN:
200 cpuidle_pause_and_lock();
201 cpuidle_disable_device(dev);
202 cpuidle_resume_and_unlock();
212 static struct notifier_block setup_hotplug_notifier = {
213 .notifier_call = pseries_cpuidle_add_cpu_notifier,
217 * pseries_cpuidle_driver_init()
219 static int pseries_cpuidle_driver_init(void)
222 struct cpuidle_driver *drv = &pseries_idle_driver;
224 drv->state_count = 0;
226 for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
227 /* Is the state not enabled? */
228 if (cpuidle_state_table[idle_state].enter == NULL)
231 drv->states[drv->state_count] = /* structure copy */
232 cpuidle_state_table[idle_state];
234 drv->state_count += 1;
241 * pseries_idle_probe()
242 * Choose state table for shared versus dedicated partition
244 static int pseries_idle_probe(void)
247 if (cpuidle_disable != IDLE_NO_OVERRIDE)
250 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
251 if (lppaca_shared_proc(get_lppaca())) {
252 cpuidle_state_table = shared_states;
253 max_idle_state = ARRAY_SIZE(shared_states);
255 cpuidle_state_table = dedicated_states;
256 max_idle_state = ARRAY_SIZE(dedicated_states);
264 static int __init pseries_processor_idle_init(void)
268 retval = pseries_idle_probe();
272 pseries_cpuidle_driver_init();
273 retval = cpuidle_register(&pseries_idle_driver, NULL);
275 printk(KERN_DEBUG "Registration of pseries driver failed.\n");
279 register_cpu_notifier(&setup_hotplug_notifier);
280 printk(KERN_DEBUG "pseries_idle_driver registered\n");
284 device_initcall(pseries_processor_idle_init);