Commit | Line | Data |
---|---|---|
707827f3 DD |
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> | |
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> | |
ae3a197e | 20 | #include <asm/runlatch.h> |
212bebb4 | 21 | #include <asm/plpar_wrappers.h> |
707827f3 DD |
22 | |
23 | struct cpuidle_driver pseries_idle_driver = { | |
1ca80944 DL |
24 | .name = "pseries_idle", |
25 | .owner = THIS_MODULE, | |
707827f3 DD |
26 | }; |
27 | ||
28 | #define MAX_IDLE_STATE_COUNT 2 | |
29 | ||
30 | static int max_idle_state = MAX_IDLE_STATE_COUNT - 1; | |
707827f3 DD |
31 | static struct cpuidle_state *cpuidle_state_table; |
32 | ||
1ca80944 | 33 | static inline void idle_loop_prolog(unsigned long *in_purr) |
707827f3 | 34 | { |
707827f3 DD |
35 | *in_purr = mfspr(SPRN_PURR); |
36 | /* | |
37 | * Indicate to the HV that we are idle. Now would be | |
38 | * a good time to find other work to dispatch. | |
39 | */ | |
40 | get_lppaca()->idle = 1; | |
41 | } | |
42 | ||
1ca80944 | 43 | static inline void idle_loop_epilog(unsigned long in_purr) |
707827f3 | 44 | { |
7ffcf8ec AB |
45 | u64 wait_cycles; |
46 | ||
47 | wait_cycles = be64_to_cpu(get_lppaca()->wait_state_cycles); | |
48 | wait_cycles += mfspr(SPRN_PURR) - in_purr; | |
49 | get_lppaca()->wait_state_cycles = cpu_to_be64(wait_cycles); | |
707827f3 | 50 | get_lppaca()->idle = 0; |
707827f3 DD |
51 | } |
52 | ||
53 | static int snooze_loop(struct cpuidle_device *dev, | |
54 | struct cpuidle_driver *drv, | |
55 | int index) | |
56 | { | |
57 | unsigned long in_purr; | |
83dac594 | 58 | int cpu = dev->cpu; |
707827f3 | 59 | |
1ca80944 | 60 | idle_loop_prolog(&in_purr); |
83dac594 DD |
61 | local_irq_enable(); |
62 | set_thread_flag(TIF_POLLING_NRFLAG); | |
707827f3 | 63 | |
83dac594 DD |
64 | while ((!need_resched()) && cpu_online(cpu)) { |
65 | ppc64_runlatch_off(); | |
66 | HMT_low(); | |
67 | HMT_very_low(); | |
707827f3 DD |
68 | } |
69 | ||
707827f3 | 70 | HMT_medium(); |
83dac594 DD |
71 | clear_thread_flag(TIF_POLLING_NRFLAG); |
72 | smp_mb(); | |
73 | ||
1ca80944 DL |
74 | idle_loop_epilog(in_purr); |
75 | ||
707827f3 DD |
76 | return index; |
77 | } | |
78 | ||
7230c564 BH |
79 | static void check_and_cede_processor(void) |
80 | { | |
81 | /* | |
be2cf20a BH |
82 | * Ensure our interrupt state is properly tracked, |
83 | * also checks if no interrupt has occurred while we | |
84 | * were soft-disabled | |
7230c564 | 85 | */ |
be2cf20a | 86 | if (prep_irq_for_idle()) { |
7230c564 | 87 | cede_processor(); |
be2cf20a BH |
88 | #ifdef CONFIG_TRACE_IRQFLAGS |
89 | /* Ensure that H_CEDE returns with IRQs on */ | |
90 | if (WARN_ON(!(mfmsr() & MSR_EE))) | |
91 | __hard_irq_enable(); | |
92 | #endif | |
93 | } | |
7230c564 BH |
94 | } |
95 | ||
707827f3 DD |
96 | static int dedicated_cede_loop(struct cpuidle_device *dev, |
97 | struct cpuidle_driver *drv, | |
98 | int index) | |
99 | { | |
100 | unsigned long in_purr; | |
707827f3 | 101 | |
1ca80944 | 102 | idle_loop_prolog(&in_purr); |
707827f3 DD |
103 | get_lppaca()->donate_dedicated_cpu = 1; |
104 | ||
105 | ppc64_runlatch_off(); | |
106 | HMT_medium(); | |
7230c564 | 107 | check_and_cede_processor(); |
707827f3 DD |
108 | |
109 | get_lppaca()->donate_dedicated_cpu = 0; | |
1ca80944 DL |
110 | |
111 | idle_loop_epilog(in_purr); | |
112 | ||
707827f3 DD |
113 | return index; |
114 | } | |
115 | ||
116 | static int shared_cede_loop(struct cpuidle_device *dev, | |
117 | struct cpuidle_driver *drv, | |
118 | int index) | |
119 | { | |
120 | unsigned long in_purr; | |
707827f3 | 121 | |
1ca80944 | 122 | idle_loop_prolog(&in_purr); |
707827f3 DD |
123 | |
124 | /* | |
125 | * Yield the processor to the hypervisor. We return if | |
126 | * an external interrupt occurs (which are driven prior | |
127 | * to returning here) or if a prod occurs from another | |
128 | * processor. When returning here, external interrupts | |
129 | * are enabled. | |
130 | */ | |
7230c564 | 131 | check_and_cede_processor(); |
707827f3 | 132 | |
1ca80944 DL |
133 | idle_loop_epilog(in_purr); |
134 | ||
707827f3 DD |
135 | return index; |
136 | } | |
137 | ||
138 | /* | |
139 | * States for dedicated partition case. | |
140 | */ | |
141 | static struct cpuidle_state dedicated_states[MAX_IDLE_STATE_COUNT] = { | |
142 | { /* Snooze */ | |
143 | .name = "snooze", | |
144 | .desc = "snooze", | |
145 | .flags = CPUIDLE_FLAG_TIME_VALID, | |
146 | .exit_latency = 0, | |
147 | .target_residency = 0, | |
148 | .enter = &snooze_loop }, | |
149 | { /* CEDE */ | |
150 | .name = "CEDE", | |
151 | .desc = "CEDE", | |
152 | .flags = CPUIDLE_FLAG_TIME_VALID, | |
83dac594 DD |
153 | .exit_latency = 10, |
154 | .target_residency = 100, | |
707827f3 DD |
155 | .enter = &dedicated_cede_loop }, |
156 | }; | |
157 | ||
158 | /* | |
159 | * States for shared partition case. | |
160 | */ | |
161 | static struct cpuidle_state shared_states[MAX_IDLE_STATE_COUNT] = { | |
162 | { /* Shared Cede */ | |
163 | .name = "Shared Cede", | |
164 | .desc = "Shared Cede", | |
165 | .flags = CPUIDLE_FLAG_TIME_VALID, | |
166 | .exit_latency = 0, | |
167 | .target_residency = 0, | |
168 | .enter = &shared_cede_loop }, | |
169 | }; | |
170 | ||
8ea959a1 DD |
171 | void update_smt_snooze_delay(int cpu, int residency) |
172 | { | |
173 | struct cpuidle_driver *drv = cpuidle_get_driver(); | |
174 | struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu); | |
175 | ||
176 | if (cpuidle_state_table != dedicated_states) | |
177 | return; | |
178 | ||
179 | if (residency < 0) { | |
180 | /* Disable the Nap state on that cpu */ | |
181 | if (dev) | |
182 | dev->states_usage[1].disable = 1; | |
183 | } else | |
184 | if (drv) | |
83dac594 | 185 | drv->states[1].target_residency = residency; |
8ea959a1 DD |
186 | } |
187 | ||
16aaaff6 DD |
188 | static int pseries_cpuidle_add_cpu_notifier(struct notifier_block *n, |
189 | unsigned long action, void *hcpu) | |
707827f3 | 190 | { |
16aaaff6 | 191 | int hotcpu = (unsigned long)hcpu; |
707827f3 | 192 | struct cpuidle_device *dev = |
7f74dc0f | 193 | per_cpu_ptr(cpuidle_devices, hotcpu); |
16aaaff6 | 194 | |
852d8cb1 DD |
195 | if (dev && cpuidle_get_driver()) { |
196 | switch (action) { | |
197 | case CPU_ONLINE: | |
198 | case CPU_ONLINE_FROZEN: | |
199 | cpuidle_pause_and_lock(); | |
16aaaff6 | 200 | cpuidle_enable_device(dev); |
852d8cb1 DD |
201 | cpuidle_resume_and_unlock(); |
202 | break; | |
203 | ||
204 | case CPU_DEAD: | |
205 | case CPU_DEAD_FROZEN: | |
206 | cpuidle_pause_and_lock(); | |
207 | cpuidle_disable_device(dev); | |
208 | cpuidle_resume_and_unlock(); | |
209 | break; | |
210 | ||
211 | default: | |
212 | return NOTIFY_DONE; | |
16aaaff6 | 213 | } |
707827f3 | 214 | } |
16aaaff6 | 215 | return NOTIFY_OK; |
707827f3 DD |
216 | } |
217 | ||
16aaaff6 DD |
218 | static struct notifier_block setup_hotplug_notifier = { |
219 | .notifier_call = pseries_cpuidle_add_cpu_notifier, | |
220 | }; | |
221 | ||
707827f3 DD |
222 | /* |
223 | * pseries_cpuidle_driver_init() | |
224 | */ | |
225 | static int pseries_cpuidle_driver_init(void) | |
226 | { | |
227 | int idle_state; | |
228 | struct cpuidle_driver *drv = &pseries_idle_driver; | |
229 | ||
230 | drv->state_count = 0; | |
231 | ||
232 | for (idle_state = 0; idle_state < MAX_IDLE_STATE_COUNT; ++idle_state) { | |
233 | ||
234 | if (idle_state > max_idle_state) | |
235 | break; | |
236 | ||
237 | /* is the state not enabled? */ | |
238 | if (cpuidle_state_table[idle_state].enter == NULL) | |
239 | continue; | |
240 | ||
241 | drv->states[drv->state_count] = /* structure copy */ | |
242 | cpuidle_state_table[idle_state]; | |
243 | ||
707827f3 DD |
244 | drv->state_count += 1; |
245 | } | |
246 | ||
247 | return 0; | |
248 | } | |
249 | ||
707827f3 DD |
250 | /* |
251 | * pseries_idle_probe() | |
252 | * Choose state table for shared versus dedicated partition | |
253 | */ | |
254 | static int pseries_idle_probe(void) | |
255 | { | |
256 | ||
257 | if (!firmware_has_feature(FW_FEATURE_SPLPAR)) | |
258 | return -ENODEV; | |
259 | ||
e8bb3e00 DD |
260 | if (cpuidle_disable != IDLE_NO_OVERRIDE) |
261 | return -ENODEV; | |
262 | ||
707827f3 DD |
263 | if (max_idle_state == 0) { |
264 | printk(KERN_DEBUG "pseries processor idle disabled.\n"); | |
265 | return -EPERM; | |
266 | } | |
267 | ||
f13c13a0 | 268 | if (lppaca_shared_proc(get_lppaca())) |
707827f3 DD |
269 | cpuidle_state_table = shared_states; |
270 | else | |
271 | cpuidle_state_table = dedicated_states; | |
272 | ||
273 | return 0; | |
274 | } | |
275 | ||
276 | static int __init pseries_processor_idle_init(void) | |
277 | { | |
278 | int retval; | |
279 | ||
280 | retval = pseries_idle_probe(); | |
281 | if (retval) | |
282 | return retval; | |
283 | ||
284 | pseries_cpuidle_driver_init(); | |
7f74dc0f | 285 | retval = cpuidle_register(&pseries_idle_driver, NULL); |
707827f3 DD |
286 | if (retval) { |
287 | printk(KERN_DEBUG "Registration of pseries driver failed.\n"); | |
288 | return retval; | |
289 | } | |
290 | ||
16aaaff6 | 291 | register_cpu_notifier(&setup_hotplug_notifier); |
707827f3 DD |
292 | printk(KERN_DEBUG "pseries_idle_driver registered\n"); |
293 | ||
294 | return 0; | |
295 | } | |
296 | ||
297 | static void __exit pseries_processor_idle_exit(void) | |
298 | { | |
299 | ||
852d8cb1 | 300 | unregister_cpu_notifier(&setup_hotplug_notifier); |
7f74dc0f | 301 | cpuidle_unregister(&pseries_idle_driver); |
707827f3 DD |
302 | |
303 | return; | |
304 | } | |
305 | ||
306 | module_init(pseries_processor_idle_init); | |
307 | module_exit(pseries_processor_idle_exit); | |
308 | ||
309 | MODULE_AUTHOR("Deepthi Dharwar <deepthi@linux.vnet.ibm.com>"); | |
310 | MODULE_DESCRIPTION("Cpuidle driver for POWER"); | |
311 | MODULE_LICENSE("GPL"); |