blob: a97ef6692dad3e6d91ddc336674d813dc0672389 [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 = {
26 .name = "pseries_idle",
27 .owner = THIS_MODULE,
28};
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
36void update_smt_snooze_delay(int snooze)
37{
38 struct cpuidle_driver *drv = cpuidle_get_driver();
39 if (drv)
40 drv->states[0].target_residency = snooze;
41}
42
43static inline void idle_loop_prolog(unsigned long *in_purr, ktime_t *kt_before)
44{
45
46 *kt_before = ktime_get_real();
47 *in_purr = mfspr(SPRN_PURR);
48 /*
49 * Indicate to the HV that we are idle. Now would be
50 * a good time to find other work to dispatch.
51 */
52 get_lppaca()->idle = 1;
53}
54
55static inline s64 idle_loop_epilog(unsigned long in_purr, ktime_t kt_before)
56{
57 get_lppaca()->wait_state_cycles += mfspr(SPRN_PURR) - in_purr;
58 get_lppaca()->idle = 0;
59
60 return ktime_to_us(ktime_sub(ktime_get_real(), kt_before));
61}
62
63static int snooze_loop(struct cpuidle_device *dev,
64 struct cpuidle_driver *drv,
65 int index)
66{
67 unsigned long in_purr;
68 ktime_t kt_before;
69 unsigned long start_snooze;
70 long snooze = drv->states[0].target_residency;
71
72 idle_loop_prolog(&in_purr, &kt_before);
73
74 if (snooze) {
75 start_snooze = get_tb() + snooze * tb_ticks_per_usec;
76 local_irq_enable();
77 set_thread_flag(TIF_POLLING_NRFLAG);
78
79 while ((snooze < 0) || (get_tb() < start_snooze)) {
80 if (need_resched() || cpu_is_offline(dev->cpu))
81 goto out;
82 ppc64_runlatch_off();
83 HMT_low();
84 HMT_very_low();
85 }
86
87 HMT_medium();
88 clear_thread_flag(TIF_POLLING_NRFLAG);
89 smp_mb();
90 local_irq_disable();
91 }
92
93out:
94 HMT_medium();
95 dev->last_residency =
96 (int)idle_loop_epilog(in_purr, kt_before);
97 return index;
98}
99
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100100static void check_and_cede_processor(void)
101{
102 /*
103 * Interrupts are soft-disabled at this point,
104 * but not hard disabled. So an interrupt might have
105 * occurred before entering NAP, and would be potentially
106 * lost (edge events, decrementer events, etc...) unless
107 * we first hard disable then check.
108 */
109 hard_irq_disable();
Anton Blanchard0b17ba72012-06-27 13:13:52 +0000110 if (!lazy_irq_pending())
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100111 cede_processor();
112}
113
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000114static int dedicated_cede_loop(struct cpuidle_device *dev,
115 struct cpuidle_driver *drv,
116 int index)
117{
118 unsigned long in_purr;
119 ktime_t kt_before;
120
121 idle_loop_prolog(&in_purr, &kt_before);
122 get_lppaca()->donate_dedicated_cpu = 1;
123
124 ppc64_runlatch_off();
125 HMT_medium();
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100126 check_and_cede_processor();
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000127
128 get_lppaca()->donate_dedicated_cpu = 0;
129 dev->last_residency =
130 (int)idle_loop_epilog(in_purr, kt_before);
131 return index;
132}
133
134static int shared_cede_loop(struct cpuidle_device *dev,
135 struct cpuidle_driver *drv,
136 int index)
137{
138 unsigned long in_purr;
139 ktime_t kt_before;
140
141 idle_loop_prolog(&in_purr, &kt_before);
142
143 /*
144 * Yield the processor to the hypervisor. We return if
145 * an external interrupt occurs (which are driven prior
146 * to returning here) or if a prod occurs from another
147 * processor. When returning here, external interrupts
148 * are enabled.
149 */
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100150 check_and_cede_processor();
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000151
152 dev->last_residency =
153 (int)idle_loop_epilog(in_purr, kt_before);
154 return index;
155}
156
157/*
158 * States for dedicated partition case.
159 */
160static struct cpuidle_state dedicated_states[MAX_IDLE_STATE_COUNT] = {
161 { /* Snooze */
162 .name = "snooze",
163 .desc = "snooze",
164 .flags = CPUIDLE_FLAG_TIME_VALID,
165 .exit_latency = 0,
166 .target_residency = 0,
167 .enter = &snooze_loop },
168 { /* CEDE */
169 .name = "CEDE",
170 .desc = "CEDE",
171 .flags = CPUIDLE_FLAG_TIME_VALID,
172 .exit_latency = 1,
173 .target_residency = 10,
174 .enter = &dedicated_cede_loop },
175};
176
177/*
178 * States for shared partition case.
179 */
180static struct cpuidle_state shared_states[MAX_IDLE_STATE_COUNT] = {
181 { /* Shared Cede */
182 .name = "Shared Cede",
183 .desc = "Shared Cede",
184 .flags = CPUIDLE_FLAG_TIME_VALID,
185 .exit_latency = 0,
186 .target_residency = 0,
187 .enter = &shared_cede_loop },
188};
189
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000190static int pseries_cpuidle_add_cpu_notifier(struct notifier_block *n,
191 unsigned long action, void *hcpu)
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000192{
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000193 int hotcpu = (unsigned long)hcpu;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000194 struct cpuidle_device *dev =
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000195 per_cpu_ptr(pseries_cpuidle_devices, hotcpu);
196
197 switch (action & 0xf) {
198 case CPU_ONLINE:
199 if (dev && cpuidle_get_driver()) {
200 cpuidle_disable_device(dev);
201 cpuidle_enable_device(dev);
202 }
203 break;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000204 }
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000205 return NOTIFY_OK;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000206}
207
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000208static struct notifier_block setup_hotplug_notifier = {
209 .notifier_call = pseries_cpuidle_add_cpu_notifier,
210};
211
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000212/*
213 * pseries_cpuidle_driver_init()
214 */
215static int pseries_cpuidle_driver_init(void)
216{
217 int idle_state;
218 struct cpuidle_driver *drv = &pseries_idle_driver;
219
220 drv->state_count = 0;
221
222 for (idle_state = 0; idle_state < MAX_IDLE_STATE_COUNT; ++idle_state) {
223
224 if (idle_state > max_idle_state)
225 break;
226
227 /* is the state not enabled? */
228 if (cpuidle_state_table[idle_state].enter == NULL)
229 continue;
230
231 drv->states[drv->state_count] = /* structure copy */
232 cpuidle_state_table[idle_state];
233
234 if (cpuidle_state_table == dedicated_states)
235 drv->states[drv->state_count].target_residency =
236 __get_cpu_var(smt_snooze_delay);
237
238 drv->state_count += 1;
239 }
240
241 return 0;
242}
243
244/* pseries_idle_devices_uninit(void)
245 * unregister cpuidle devices and de-allocate memory
246 */
247static void pseries_idle_devices_uninit(void)
248{
249 int i;
250 struct cpuidle_device *dev;
251
252 for_each_possible_cpu(i) {
253 dev = per_cpu_ptr(pseries_cpuidle_devices, i);
254 cpuidle_unregister_device(dev);
255 }
256
257 free_percpu(pseries_cpuidle_devices);
258 return;
259}
260
261/* pseries_idle_devices_init()
262 * allocate, initialize and register cpuidle device
263 */
264static int pseries_idle_devices_init(void)
265{
266 int i;
267 struct cpuidle_driver *drv = &pseries_idle_driver;
268 struct cpuidle_device *dev;
269
270 pseries_cpuidle_devices = alloc_percpu(struct cpuidle_device);
271 if (pseries_cpuidle_devices == NULL)
272 return -ENOMEM;
273
274 for_each_possible_cpu(i) {
275 dev = per_cpu_ptr(pseries_cpuidle_devices, i);
276 dev->state_count = drv->state_count;
277 dev->cpu = i;
278 if (cpuidle_register_device(dev)) {
279 printk(KERN_DEBUG \
280 "cpuidle_register_device %d failed!\n", i);
281 return -EIO;
282 }
283 }
284
285 return 0;
286}
287
288/*
289 * pseries_idle_probe()
290 * Choose state table for shared versus dedicated partition
291 */
292static int pseries_idle_probe(void)
293{
294
295 if (!firmware_has_feature(FW_FEATURE_SPLPAR))
296 return -ENODEV;
297
Deepthi Dharware8bb3e02011-11-30 02:47:03 +0000298 if (cpuidle_disable != IDLE_NO_OVERRIDE)
299 return -ENODEV;
300
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000301 if (max_idle_state == 0) {
302 printk(KERN_DEBUG "pseries processor idle disabled.\n");
303 return -EPERM;
304 }
305
306 if (get_lppaca()->shared_proc)
307 cpuidle_state_table = shared_states;
308 else
309 cpuidle_state_table = dedicated_states;
310
311 return 0;
312}
313
314static int __init pseries_processor_idle_init(void)
315{
316 int retval;
317
318 retval = pseries_idle_probe();
319 if (retval)
320 return retval;
321
322 pseries_cpuidle_driver_init();
323 retval = cpuidle_register_driver(&pseries_idle_driver);
324 if (retval) {
325 printk(KERN_DEBUG "Registration of pseries driver failed.\n");
326 return retval;
327 }
328
329 retval = pseries_idle_devices_init();
330 if (retval) {
331 pseries_idle_devices_uninit();
332 cpuidle_unregister_driver(&pseries_idle_driver);
333 return retval;
334 }
335
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000336 register_cpu_notifier(&setup_hotplug_notifier);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000337 printk(KERN_DEBUG "pseries_idle_driver registered\n");
338
339 return 0;
340}
341
342static void __exit pseries_processor_idle_exit(void)
343{
344
345 pseries_idle_devices_uninit();
346 cpuidle_unregister_driver(&pseries_idle_driver);
347
348 return;
349}
350
351module_init(pseries_processor_idle_init);
352module_exit(pseries_processor_idle_exit);
353
354MODULE_AUTHOR("Deepthi Dharwar <deepthi@linux.vnet.ibm.com>");
355MODULE_DESCRIPTION("Cpuidle driver for POWER");
356MODULE_LICENSE("GPL");