blob: a197120e169ad313f96de1f08c51ff54c08d61ae [file] [log] [blame]
Deepthi Dharwar707827f2011-11-30 02:46:42 +00001/*
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>
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +000014#include <linux/notifier.h>
Deepthi Dharwar707827f2011-11-30 02:46:42 +000015
16#include <asm/paca.h>
17#include <asm/reg.h>
Deepthi Dharwar707827f2011-11-30 02:46:42 +000018#include <asm/machdep.h>
19#include <asm/firmware.h>
David Howellsae3a197e2012-03-28 18:30:02 +010020#include <asm/runlatch.h>
Deepthi Dharwar707827f2011-11-30 02:46:42 +000021
22#include "plpar_wrappers.h"
23#include "pseries.h"
24
25struct cpuidle_driver pseries_idle_driver = {
Daniel Lezcano1ca80942013-04-03 12:15:22 +000026 .name = "pseries_idle",
27 .owner = THIS_MODULE,
28 .en_core_tk_irqen = 1,
Deepthi Dharwar707827f2011-11-30 02:46:42 +000029};
30
31#define MAX_IDLE_STATE_COUNT 2
32
33static int max_idle_state = MAX_IDLE_STATE_COUNT - 1;
34static struct cpuidle_device __percpu *pseries_cpuidle_devices;
35static struct cpuidle_state *cpuidle_state_table;
36
Daniel Lezcano1ca80942013-04-03 12:15:22 +000037static inline void idle_loop_prolog(unsigned long *in_purr)
Deepthi Dharwar707827f2011-11-30 02:46:42 +000038{
Deepthi Dharwar707827f2011-11-30 02:46:42 +000039 *in_purr = mfspr(SPRN_PURR);
40 /*
41 * Indicate to the HV that we are idle. Now would be
42 * a good time to find other work to dispatch.
43 */
44 get_lppaca()->idle = 1;
45}
46
Daniel Lezcano1ca80942013-04-03 12:15:22 +000047static inline void idle_loop_epilog(unsigned long in_purr)
Deepthi Dharwar707827f2011-11-30 02:46:42 +000048{
49 get_lppaca()->wait_state_cycles += mfspr(SPRN_PURR) - in_purr;
50 get_lppaca()->idle = 0;
Deepthi Dharwar707827f2011-11-30 02:46:42 +000051}
52
53static int snooze_loop(struct cpuidle_device *dev,
54 struct cpuidle_driver *drv,
55 int index)
56{
57 unsigned long in_purr;
Deepthi Dharwar83dac592012-10-03 18:42:26 +000058 int cpu = dev->cpu;
Deepthi Dharwar707827f2011-11-30 02:46:42 +000059
Daniel Lezcano1ca80942013-04-03 12:15:22 +000060 idle_loop_prolog(&in_purr);
Deepthi Dharwar83dac592012-10-03 18:42:26 +000061 local_irq_enable();
62 set_thread_flag(TIF_POLLING_NRFLAG);
Deepthi Dharwar707827f2011-11-30 02:46:42 +000063
Deepthi Dharwar83dac592012-10-03 18:42:26 +000064 while ((!need_resched()) && cpu_online(cpu)) {
65 ppc64_runlatch_off();
66 HMT_low();
67 HMT_very_low();
Deepthi Dharwar707827f2011-11-30 02:46:42 +000068 }
69
Deepthi Dharwar707827f2011-11-30 02:46:42 +000070 HMT_medium();
Deepthi Dharwar83dac592012-10-03 18:42:26 +000071 clear_thread_flag(TIF_POLLING_NRFLAG);
72 smp_mb();
73
Daniel Lezcano1ca80942013-04-03 12:15:22 +000074 idle_loop_epilog(in_purr);
75
Deepthi Dharwar707827f2011-11-30 02:46:42 +000076 return index;
77}
78
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +110079static void check_and_cede_processor(void)
80{
81 /*
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +100082 * Ensure our interrupt state is properly tracked,
83 * also checks if no interrupt has occurred while we
84 * were soft-disabled
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +110085 */
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +100086 if (prep_irq_for_idle()) {
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +110087 cede_processor();
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +100088#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 }
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +110094}
95
Deepthi Dharwar707827f2011-11-30 02:46:42 +000096static int dedicated_cede_loop(struct cpuidle_device *dev,
97 struct cpuidle_driver *drv,
98 int index)
99{
100 unsigned long in_purr;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000101
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000102 idle_loop_prolog(&in_purr);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000103 get_lppaca()->donate_dedicated_cpu = 1;
104
105 ppc64_runlatch_off();
106 HMT_medium();
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100107 check_and_cede_processor();
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000108
109 get_lppaca()->donate_dedicated_cpu = 0;
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000110
111 idle_loop_epilog(in_purr);
112
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000113 return index;
114}
115
116static int shared_cede_loop(struct cpuidle_device *dev,
117 struct cpuidle_driver *drv,
118 int index)
119{
120 unsigned long in_purr;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000121
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000122 idle_loop_prolog(&in_purr);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000123
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 */
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100131 check_and_cede_processor();
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000132
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000133 idle_loop_epilog(in_purr);
134
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000135 return index;
136}
137
138/*
139 * States for dedicated partition case.
140 */
141static 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,
Deepthi Dharwar83dac592012-10-03 18:42:26 +0000153 .exit_latency = 10,
154 .target_residency = 100,
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000155 .enter = &dedicated_cede_loop },
156};
157
158/*
159 * States for shared partition case.
160 */
161static 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
Deepthi Dharwar8ea959a2012-10-03 18:42:18 +0000171void 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)
Deepthi Dharwar83dac592012-10-03 18:42:26 +0000185 drv->states[1].target_residency = residency;
Deepthi Dharwar8ea959a2012-10-03 18:42:18 +0000186}
187
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000188static int pseries_cpuidle_add_cpu_notifier(struct notifier_block *n,
189 unsigned long action, void *hcpu)
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000190{
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000191 int hotcpu = (unsigned long)hcpu;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000192 struct cpuidle_device *dev =
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000193 per_cpu_ptr(pseries_cpuidle_devices, hotcpu);
194
Deepthi Dharwar852d8cb2012-07-03 20:07:22 +0000195 if (dev && cpuidle_get_driver()) {
196 switch (action) {
197 case CPU_ONLINE:
198 case CPU_ONLINE_FROZEN:
199 cpuidle_pause_and_lock();
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000200 cpuidle_enable_device(dev);
Deepthi Dharwar852d8cb2012-07-03 20:07:22 +0000201 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;
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000213 }
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000214 }
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000215 return NOTIFY_OK;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000216}
217
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000218static struct notifier_block setup_hotplug_notifier = {
219 .notifier_call = pseries_cpuidle_add_cpu_notifier,
220};
221
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000222/*
223 * pseries_cpuidle_driver_init()
224 */
225static 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
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000244 drv->state_count += 1;
245 }
246
247 return 0;
248}
249
250/* pseries_idle_devices_uninit(void)
251 * unregister cpuidle devices and de-allocate memory
252 */
253static void pseries_idle_devices_uninit(void)
254{
255 int i;
256 struct cpuidle_device *dev;
257
258 for_each_possible_cpu(i) {
259 dev = per_cpu_ptr(pseries_cpuidle_devices, i);
260 cpuidle_unregister_device(dev);
261 }
262
263 free_percpu(pseries_cpuidle_devices);
264 return;
265}
266
267/* pseries_idle_devices_init()
268 * allocate, initialize and register cpuidle device
269 */
270static int pseries_idle_devices_init(void)
271{
272 int i;
273 struct cpuidle_driver *drv = &pseries_idle_driver;
274 struct cpuidle_device *dev;
275
276 pseries_cpuidle_devices = alloc_percpu(struct cpuidle_device);
277 if (pseries_cpuidle_devices == NULL)
278 return -ENOMEM;
279
280 for_each_possible_cpu(i) {
281 dev = per_cpu_ptr(pseries_cpuidle_devices, i);
282 dev->state_count = drv->state_count;
283 dev->cpu = i;
284 if (cpuidle_register_device(dev)) {
285 printk(KERN_DEBUG \
286 "cpuidle_register_device %d failed!\n", i);
287 return -EIO;
288 }
289 }
290
291 return 0;
292}
293
294/*
295 * pseries_idle_probe()
296 * Choose state table for shared versus dedicated partition
297 */
298static int pseries_idle_probe(void)
299{
300
301 if (!firmware_has_feature(FW_FEATURE_SPLPAR))
302 return -ENODEV;
303
Deepthi Dharware8bb3e02011-11-30 02:47:03 +0000304 if (cpuidle_disable != IDLE_NO_OVERRIDE)
305 return -ENODEV;
306
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000307 if (max_idle_state == 0) {
308 printk(KERN_DEBUG "pseries processor idle disabled.\n");
309 return -EPERM;
310 }
311
312 if (get_lppaca()->shared_proc)
313 cpuidle_state_table = shared_states;
314 else
315 cpuidle_state_table = dedicated_states;
316
317 return 0;
318}
319
320static int __init pseries_processor_idle_init(void)
321{
322 int retval;
323
324 retval = pseries_idle_probe();
325 if (retval)
326 return retval;
327
328 pseries_cpuidle_driver_init();
329 retval = cpuidle_register_driver(&pseries_idle_driver);
330 if (retval) {
331 printk(KERN_DEBUG "Registration of pseries driver failed.\n");
332 return retval;
333 }
334
335 retval = pseries_idle_devices_init();
336 if (retval) {
337 pseries_idle_devices_uninit();
338 cpuidle_unregister_driver(&pseries_idle_driver);
339 return retval;
340 }
341
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000342 register_cpu_notifier(&setup_hotplug_notifier);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000343 printk(KERN_DEBUG "pseries_idle_driver registered\n");
344
345 return 0;
346}
347
348static void __exit pseries_processor_idle_exit(void)
349{
350
Deepthi Dharwar852d8cb2012-07-03 20:07:22 +0000351 unregister_cpu_notifier(&setup_hotplug_notifier);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000352 pseries_idle_devices_uninit();
353 cpuidle_unregister_driver(&pseries_idle_driver);
354
355 return;
356}
357
358module_init(pseries_processor_idle_init);
359module_exit(pseries_processor_idle_exit);
360
361MODULE_AUTHOR("Deepthi Dharwar <deepthi@linux.vnet.ibm.com>");
362MODULE_DESCRIPTION("Cpuidle driver for POWER");
363MODULE_LICENSE("GPL");