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