cpuidle/pseries: Convert to hotplug state machine
[linux-2.6-block.git] / drivers / cpuidle / cpuidle-pseries.c
CommitLineData
707827f3 1/*
962e7bd4 2 * cpuidle-pseries - idle state cpuidle driver.
707827f3
DD
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>
16aaaff6 14#include <linux/notifier.h>
707827f3
DD
15
16#include <asm/paca.h>
17#include <asm/reg.h>
707827f3
DD
18#include <asm/machdep.h>
19#include <asm/firmware.h>
3f67d962 20#include <asm/runlatch.h>
212bebb4 21#include <asm/plpar_wrappers.h>
707827f3
DD
22
23struct cpuidle_driver pseries_idle_driver = {
1ca80944
DL
24 .name = "pseries_idle",
25 .owner = THIS_MODULE,
707827f3
DD
26};
27
bf7f61f2 28static int max_idle_state;
707827f3 29static struct cpuidle_state *cpuidle_state_table;
78eaa10f
SB
30static u64 snooze_timeout;
31static bool snooze_timeout_en;
707827f3 32
1ca80944 33static inline void idle_loop_prolog(unsigned long *in_purr)
707827f3 34{
d8c6ad31 35 ppc64_runlatch_off();
707827f3
DD
36 *in_purr = mfspr(SPRN_PURR);
37 /*
38 * Indicate to the HV that we are idle. Now would be
39 * a good time to find other work to dispatch.
40 */
41 get_lppaca()->idle = 1;
42}
43
1ca80944 44static inline void idle_loop_epilog(unsigned long in_purr)
707827f3 45{
7ffcf8ec
AB
46 u64 wait_cycles;
47
48 wait_cycles = be64_to_cpu(get_lppaca()->wait_state_cycles);
49 wait_cycles += mfspr(SPRN_PURR) - in_purr;
50 get_lppaca()->wait_state_cycles = cpu_to_be64(wait_cycles);
707827f3 51 get_lppaca()->idle = 0;
d8c6ad31
NP
52
53 if (irqs_disabled())
54 local_irq_enable();
55 ppc64_runlatch_on();
707827f3
DD
56}
57
58static int snooze_loop(struct cpuidle_device *dev,
59 struct cpuidle_driver *drv,
60 int index)
61{
62 unsigned long in_purr;
78eaa10f 63 u64 snooze_exit_time;
707827f3 64
1ca80944 65 idle_loop_prolog(&in_purr);
83dac594
DD
66 local_irq_enable();
67 set_thread_flag(TIF_POLLING_NRFLAG);
78eaa10f 68 snooze_exit_time = get_tb() + snooze_timeout;
707827f3 69
b69dbba0 70 while (!need_resched()) {
83dac594
DD
71 HMT_low();
72 HMT_very_low();
78eaa10f
SB
73 if (snooze_timeout_en && get_tb() > snooze_exit_time)
74 break;
707827f3
DD
75 }
76
707827f3 77 HMT_medium();
83dac594
DD
78 clear_thread_flag(TIF_POLLING_NRFLAG);
79 smp_mb();
80
1ca80944
DL
81 idle_loop_epilog(in_purr);
82
707827f3
DD
83 return index;
84}
85
7230c564
BH
86static void check_and_cede_processor(void)
87{
88 /*
be2cf20a
BH
89 * Ensure our interrupt state is properly tracked,
90 * also checks if no interrupt has occurred while we
91 * were soft-disabled
7230c564 92 */
be2cf20a 93 if (prep_irq_for_idle()) {
7230c564 94 cede_processor();
be2cf20a
BH
95#ifdef CONFIG_TRACE_IRQFLAGS
96 /* Ensure that H_CEDE returns with IRQs on */
97 if (WARN_ON(!(mfmsr() & MSR_EE)))
98 __hard_irq_enable();
99#endif
100 }
7230c564
BH
101}
102
707827f3
DD
103static int dedicated_cede_loop(struct cpuidle_device *dev,
104 struct cpuidle_driver *drv,
105 int index)
106{
107 unsigned long in_purr;
707827f3 108
1ca80944 109 idle_loop_prolog(&in_purr);
707827f3
DD
110 get_lppaca()->donate_dedicated_cpu = 1;
111
707827f3 112 HMT_medium();
7230c564 113 check_and_cede_processor();
707827f3
DD
114
115 get_lppaca()->donate_dedicated_cpu = 0;
1ca80944
DL
116
117 idle_loop_epilog(in_purr);
118
707827f3
DD
119 return index;
120}
121
122static int shared_cede_loop(struct cpuidle_device *dev,
123 struct cpuidle_driver *drv,
124 int index)
125{
126 unsigned long in_purr;
707827f3 127
1ca80944 128 idle_loop_prolog(&in_purr);
707827f3
DD
129
130 /*
131 * Yield the processor to the hypervisor. We return if
132 * an external interrupt occurs (which are driven prior
133 * to returning here) or if a prod occurs from another
134 * processor. When returning here, external interrupts
135 * are enabled.
136 */
7230c564 137 check_and_cede_processor();
707827f3 138
1ca80944
DL
139 idle_loop_epilog(in_purr);
140
707827f3
DD
141 return index;
142}
143
144/*
145 * States for dedicated partition case.
146 */
bf7f61f2 147static struct cpuidle_state dedicated_states[] = {
707827f3
DD
148 { /* Snooze */
149 .name = "snooze",
150 .desc = "snooze",
707827f3
DD
151 .exit_latency = 0,
152 .target_residency = 0,
153 .enter = &snooze_loop },
154 { /* CEDE */
155 .name = "CEDE",
156 .desc = "CEDE",
83dac594
DD
157 .exit_latency = 10,
158 .target_residency = 100,
707827f3
DD
159 .enter = &dedicated_cede_loop },
160};
161
162/*
163 * States for shared partition case.
164 */
bf7f61f2 165static struct cpuidle_state shared_states[] = {
707827f3
DD
166 { /* Shared Cede */
167 .name = "Shared Cede",
168 .desc = "Shared Cede",
707827f3
DD
169 .exit_latency = 0,
170 .target_residency = 0,
171 .enter = &shared_cede_loop },
172};
173
529351fd 174static int pseries_cpuidle_cpu_online(unsigned int cpu)
707827f3 175{
529351fd 176 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
16aaaff6 177
852d8cb1 178 if (dev && cpuidle_get_driver()) {
529351fd
SAS
179 cpuidle_pause_and_lock();
180 cpuidle_enable_device(dev);
181 cpuidle_resume_and_unlock();
182 }
183 return 0;
184}
852d8cb1 185
529351fd
SAS
186static int pseries_cpuidle_cpu_dead(unsigned int cpu)
187{
188 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
852d8cb1 189
529351fd
SAS
190 if (dev && cpuidle_get_driver()) {
191 cpuidle_pause_and_lock();
192 cpuidle_disable_device(dev);
193 cpuidle_resume_and_unlock();
707827f3 194 }
529351fd 195 return 0;
707827f3
DD
196}
197
198/*
199 * pseries_cpuidle_driver_init()
200 */
201static int pseries_cpuidle_driver_init(void)
202{
203 int idle_state;
204 struct cpuidle_driver *drv = &pseries_idle_driver;
205
206 drv->state_count = 0;
207
bf7f61f2
DD
208 for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
209 /* Is the state not enabled? */
707827f3
DD
210 if (cpuidle_state_table[idle_state].enter == NULL)
211 continue;
212
213 drv->states[drv->state_count] = /* structure copy */
214 cpuidle_state_table[idle_state];
215
707827f3
DD
216 drv->state_count += 1;
217 }
218
219 return 0;
220}
221
707827f3
DD
222/*
223 * pseries_idle_probe()
224 * Choose state table for shared versus dedicated partition
225 */
226static int pseries_idle_probe(void)
227{
228
e8bb3e00
DD
229 if (cpuidle_disable != IDLE_NO_OVERRIDE)
230 return -ENODEV;
231
b69dbba0 232 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
bf7f61f2 233 if (lppaca_shared_proc(get_lppaca())) {
b69dbba0 234 cpuidle_state_table = shared_states;
bf7f61f2
DD
235 max_idle_state = ARRAY_SIZE(shared_states);
236 } else {
b69dbba0 237 cpuidle_state_table = dedicated_states;
bf7f61f2
DD
238 max_idle_state = ARRAY_SIZE(dedicated_states);
239 }
b69dbba0
DD
240 } else
241 return -ENODEV;
707827f3 242
78eaa10f
SB
243 if (max_idle_state > 1) {
244 snooze_timeout_en = true;
245 snooze_timeout = cpuidle_state_table[1].target_residency *
246 tb_ticks_per_usec;
247 }
707827f3
DD
248 return 0;
249}
250
251static int __init pseries_processor_idle_init(void)
252{
253 int retval;
254
255 retval = pseries_idle_probe();
256 if (retval)
257 return retval;
258
259 pseries_cpuidle_driver_init();
b69dbba0 260 retval = cpuidle_register(&pseries_idle_driver, NULL);
707827f3
DD
261 if (retval) {
262 printk(KERN_DEBUG "Registration of pseries driver failed.\n");
263 return retval;
264 }
265
529351fd
SAS
266 retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
267 "cpuidle/pseries:online",
268 pseries_cpuidle_cpu_online, NULL);
269 WARN_ON(retval < 0);
270 retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD,
271 "cpuidle/pseries:DEAD", NULL,
272 pseries_cpuidle_cpu_dead);
273 WARN_ON(retval < 0);
707827f3 274 printk(KERN_DEBUG "pseries_idle_driver registered\n");
707827f3
DD
275 return 0;
276}
277
12431c64 278device_initcall(pseries_processor_idle_init);