blob: 14899b1db1e92a09cbc3df78dcfd37b567d6e535 [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,
Deepthi Dharwar707827f2011-11-30 02:46:42 +000028};
29
30#define MAX_IDLE_STATE_COUNT 2
31
32static int max_idle_state = MAX_IDLE_STATE_COUNT - 1;
33static struct cpuidle_device __percpu *pseries_cpuidle_devices;
34static struct cpuidle_state *cpuidle_state_table;
35
Daniel Lezcano1ca80942013-04-03 12:15:22 +000036static inline void idle_loop_prolog(unsigned long *in_purr)
Deepthi Dharwar707827f2011-11-30 02:46:42 +000037{
Deepthi Dharwar707827f2011-11-30 02:46:42 +000038 *in_purr = mfspr(SPRN_PURR);
39 /*
40 * Indicate to the HV that we are idle. Now would be
41 * a good time to find other work to dispatch.
42 */
43 get_lppaca()->idle = 1;
44}
45
Daniel Lezcano1ca80942013-04-03 12:15:22 +000046static inline void idle_loop_epilog(unsigned long in_purr)
Deepthi Dharwar707827f2011-11-30 02:46:42 +000047{
Anton Blanchard7ffcf8e2013-08-07 02:01:46 +100048 u64 wait_cycles;
49
50 wait_cycles = be64_to_cpu(get_lppaca()->wait_state_cycles);
51 wait_cycles += mfspr(SPRN_PURR) - in_purr;
52 get_lppaca()->wait_state_cycles = cpu_to_be64(wait_cycles);
Deepthi Dharwar707827f2011-11-30 02:46:42 +000053 get_lppaca()->idle = 0;
Deepthi Dharwar707827f2011-11-30 02:46:42 +000054}
55
56static int snooze_loop(struct cpuidle_device *dev,
57 struct cpuidle_driver *drv,
58 int index)
59{
60 unsigned long in_purr;
Deepthi Dharwar83dac592012-10-03 18:42:26 +000061 int cpu = dev->cpu;
Deepthi Dharwar707827f2011-11-30 02:46:42 +000062
Daniel Lezcano1ca80942013-04-03 12:15:22 +000063 idle_loop_prolog(&in_purr);
Deepthi Dharwar83dac592012-10-03 18:42:26 +000064 local_irq_enable();
65 set_thread_flag(TIF_POLLING_NRFLAG);
Deepthi Dharwar707827f2011-11-30 02:46:42 +000066
Deepthi Dharwar83dac592012-10-03 18:42:26 +000067 while ((!need_resched()) && cpu_online(cpu)) {
68 ppc64_runlatch_off();
69 HMT_low();
70 HMT_very_low();
Deepthi Dharwar707827f2011-11-30 02:46:42 +000071 }
72
Deepthi Dharwar707827f2011-11-30 02:46:42 +000073 HMT_medium();
Deepthi Dharwar83dac592012-10-03 18:42:26 +000074 clear_thread_flag(TIF_POLLING_NRFLAG);
75 smp_mb();
76
Daniel Lezcano1ca80942013-04-03 12:15:22 +000077 idle_loop_epilog(in_purr);
78
Deepthi Dharwar707827f2011-11-30 02:46:42 +000079 return index;
80}
81
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +110082static void check_and_cede_processor(void)
83{
84 /*
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +100085 * Ensure our interrupt state is properly tracked,
86 * also checks if no interrupt has occurred while we
87 * were soft-disabled
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +110088 */
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +100089 if (prep_irq_for_idle()) {
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +110090 cede_processor();
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +100091#ifdef CONFIG_TRACE_IRQFLAGS
92 /* Ensure that H_CEDE returns with IRQs on */
93 if (WARN_ON(!(mfmsr() & MSR_EE)))
94 __hard_irq_enable();
95#endif
96 }
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +110097}
98
Deepthi Dharwar707827f2011-11-30 02:46:42 +000099static int dedicated_cede_loop(struct cpuidle_device *dev,
100 struct cpuidle_driver *drv,
101 int index)
102{
103 unsigned long in_purr;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000104
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000105 idle_loop_prolog(&in_purr);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000106 get_lppaca()->donate_dedicated_cpu = 1;
107
108 ppc64_runlatch_off();
109 HMT_medium();
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100110 check_and_cede_processor();
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000111
112 get_lppaca()->donate_dedicated_cpu = 0;
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000113
114 idle_loop_epilog(in_purr);
115
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000116 return index;
117}
118
119static int shared_cede_loop(struct cpuidle_device *dev,
120 struct cpuidle_driver *drv,
121 int index)
122{
123 unsigned long in_purr;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000124
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000125 idle_loop_prolog(&in_purr);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000126
127 /*
128 * Yield the processor to the hypervisor. We return if
129 * an external interrupt occurs (which are driven prior
130 * to returning here) or if a prod occurs from another
131 * processor. When returning here, external interrupts
132 * are enabled.
133 */
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100134 check_and_cede_processor();
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000135
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000136 idle_loop_epilog(in_purr);
137
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000138 return index;
139}
140
141/*
142 * States for dedicated partition case.
143 */
144static struct cpuidle_state dedicated_states[MAX_IDLE_STATE_COUNT] = {
145 { /* Snooze */
146 .name = "snooze",
147 .desc = "snooze",
148 .flags = CPUIDLE_FLAG_TIME_VALID,
149 .exit_latency = 0,
150 .target_residency = 0,
151 .enter = &snooze_loop },
152 { /* CEDE */
153 .name = "CEDE",
154 .desc = "CEDE",
155 .flags = CPUIDLE_FLAG_TIME_VALID,
Deepthi Dharwar83dac592012-10-03 18:42:26 +0000156 .exit_latency = 10,
157 .target_residency = 100,
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000158 .enter = &dedicated_cede_loop },
159};
160
161/*
162 * States for shared partition case.
163 */
164static struct cpuidle_state shared_states[MAX_IDLE_STATE_COUNT] = {
165 { /* Shared Cede */
166 .name = "Shared Cede",
167 .desc = "Shared Cede",
168 .flags = CPUIDLE_FLAG_TIME_VALID,
169 .exit_latency = 0,
170 .target_residency = 0,
171 .enter = &shared_cede_loop },
172};
173
Deepthi Dharwar8ea959a2012-10-03 18:42:18 +0000174void update_smt_snooze_delay(int cpu, int residency)
175{
176 struct cpuidle_driver *drv = cpuidle_get_driver();
177 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
178
179 if (cpuidle_state_table != dedicated_states)
180 return;
181
182 if (residency < 0) {
183 /* Disable the Nap state on that cpu */
184 if (dev)
185 dev->states_usage[1].disable = 1;
186 } else
187 if (drv)
Deepthi Dharwar83dac592012-10-03 18:42:26 +0000188 drv->states[1].target_residency = residency;
Deepthi Dharwar8ea959a2012-10-03 18:42:18 +0000189}
190
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000191static int pseries_cpuidle_add_cpu_notifier(struct notifier_block *n,
192 unsigned long action, void *hcpu)
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000193{
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000194 int hotcpu = (unsigned long)hcpu;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000195 struct cpuidle_device *dev =
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000196 per_cpu_ptr(pseries_cpuidle_devices, hotcpu);
197
Deepthi Dharwar852d8cb2012-07-03 20:07:22 +0000198 if (dev && cpuidle_get_driver()) {
199 switch (action) {
200 case CPU_ONLINE:
201 case CPU_ONLINE_FROZEN:
202 cpuidle_pause_and_lock();
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000203 cpuidle_enable_device(dev);
Deepthi Dharwar852d8cb2012-07-03 20:07:22 +0000204 cpuidle_resume_and_unlock();
205 break;
206
207 case CPU_DEAD:
208 case CPU_DEAD_FROZEN:
209 cpuidle_pause_and_lock();
210 cpuidle_disable_device(dev);
211 cpuidle_resume_and_unlock();
212 break;
213
214 default:
215 return NOTIFY_DONE;
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000216 }
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000217 }
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000218 return NOTIFY_OK;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000219}
220
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000221static struct notifier_block setup_hotplug_notifier = {
222 .notifier_call = pseries_cpuidle_add_cpu_notifier,
223};
224
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000225/*
226 * pseries_cpuidle_driver_init()
227 */
228static int pseries_cpuidle_driver_init(void)
229{
230 int idle_state;
231 struct cpuidle_driver *drv = &pseries_idle_driver;
232
233 drv->state_count = 0;
234
235 for (idle_state = 0; idle_state < MAX_IDLE_STATE_COUNT; ++idle_state) {
236
237 if (idle_state > max_idle_state)
238 break;
239
240 /* is the state not enabled? */
241 if (cpuidle_state_table[idle_state].enter == NULL)
242 continue;
243
244 drv->states[drv->state_count] = /* structure copy */
245 cpuidle_state_table[idle_state];
246
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000247 drv->state_count += 1;
248 }
249
250 return 0;
251}
252
253/* pseries_idle_devices_uninit(void)
254 * unregister cpuidle devices and de-allocate memory
255 */
256static void pseries_idle_devices_uninit(void)
257{
258 int i;
259 struct cpuidle_device *dev;
260
261 for_each_possible_cpu(i) {
262 dev = per_cpu_ptr(pseries_cpuidle_devices, i);
263 cpuidle_unregister_device(dev);
264 }
265
266 free_percpu(pseries_cpuidle_devices);
267 return;
268}
269
270/* pseries_idle_devices_init()
271 * allocate, initialize and register cpuidle device
272 */
273static int pseries_idle_devices_init(void)
274{
275 int i;
276 struct cpuidle_driver *drv = &pseries_idle_driver;
277 struct cpuidle_device *dev;
278
279 pseries_cpuidle_devices = alloc_percpu(struct cpuidle_device);
280 if (pseries_cpuidle_devices == NULL)
281 return -ENOMEM;
282
283 for_each_possible_cpu(i) {
284 dev = per_cpu_ptr(pseries_cpuidle_devices, i);
285 dev->state_count = drv->state_count;
286 dev->cpu = i;
287 if (cpuidle_register_device(dev)) {
288 printk(KERN_DEBUG \
289 "cpuidle_register_device %d failed!\n", i);
290 return -EIO;
291 }
292 }
293
294 return 0;
295}
296
297/*
298 * pseries_idle_probe()
299 * Choose state table for shared versus dedicated partition
300 */
301static int pseries_idle_probe(void)
302{
303
304 if (!firmware_has_feature(FW_FEATURE_SPLPAR))
305 return -ENODEV;
306
Deepthi Dharware8bb3e02011-11-30 02:47:03 +0000307 if (cpuidle_disable != IDLE_NO_OVERRIDE)
308 return -ENODEV;
309
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000310 if (max_idle_state == 0) {
311 printk(KERN_DEBUG "pseries processor idle disabled.\n");
312 return -EPERM;
313 }
314
Anton Blanchardf13c13a2013-08-07 02:01:26 +1000315 if (lppaca_shared_proc(get_lppaca()))
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000316 cpuidle_state_table = shared_states;
317 else
318 cpuidle_state_table = dedicated_states;
319
320 return 0;
321}
322
323static int __init pseries_processor_idle_init(void)
324{
325 int retval;
326
327 retval = pseries_idle_probe();
328 if (retval)
329 return retval;
330
331 pseries_cpuidle_driver_init();
332 retval = cpuidle_register_driver(&pseries_idle_driver);
333 if (retval) {
334 printk(KERN_DEBUG "Registration of pseries driver failed.\n");
335 return retval;
336 }
337
338 retval = pseries_idle_devices_init();
339 if (retval) {
340 pseries_idle_devices_uninit();
341 cpuidle_unregister_driver(&pseries_idle_driver);
342 return retval;
343 }
344
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000345 register_cpu_notifier(&setup_hotplug_notifier);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000346 printk(KERN_DEBUG "pseries_idle_driver registered\n");
347
348 return 0;
349}
350
351static void __exit pseries_processor_idle_exit(void)
352{
353
Deepthi Dharwar852d8cb2012-07-03 20:07:22 +0000354 unregister_cpu_notifier(&setup_hotplug_notifier);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000355 pseries_idle_devices_uninit();
356 cpuidle_unregister_driver(&pseries_idle_driver);
357
358 return;
359}
360
361module_init(pseries_processor_idle_init);
362module_exit(pseries_processor_idle_exit);
363
364MODULE_AUTHOR("Deepthi Dharwar <deepthi@linux.vnet.ibm.com>");
365MODULE_DESCRIPTION("Cpuidle driver for POWER");
366MODULE_LICENSE("GPL");