blob: d4864892951b618cd3e2a42e2128078c97f8909c [file] [log] [blame]
Deepthi Dharwar707827f2011-11-30 02:46:42 +00001/*
Deepthi Dharwar962e7bd2014-01-14 16:26:02 +05302 * cpuidle-pseries - idle state cpuidle driver.
Deepthi Dharwar707827f2011-11-30 02:46:42 +00003 * 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>
Deepthi Dharwar212bebb2013-08-22 15:23:52 +053020#include <asm/plpar_wrappers.h>
Deepthi Dharwar707827f2011-11-30 02:46:42 +000021
22struct cpuidle_driver pseries_idle_driver = {
Daniel Lezcano1ca80942013-04-03 12:15:22 +000023 .name = "pseries_idle",
24 .owner = THIS_MODULE,
Deepthi Dharwar707827f2011-11-30 02:46:42 +000025};
26
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +053027static int max_idle_state;
Deepthi Dharwar707827f2011-11-30 02:46:42 +000028static struct cpuidle_state *cpuidle_state_table;
29
Daniel Lezcano1ca80942013-04-03 12:15:22 +000030static inline void idle_loop_prolog(unsigned long *in_purr)
Deepthi Dharwar707827f2011-11-30 02:46:42 +000031{
Nicolas Pitred8c6ad32014-01-29 12:45:10 -050032 ppc64_runlatch_off();
Deepthi Dharwar707827f2011-11-30 02:46:42 +000033 *in_purr = mfspr(SPRN_PURR);
34 /*
35 * Indicate to the HV that we are idle. Now would be
36 * a good time to find other work to dispatch.
37 */
38 get_lppaca()->idle = 1;
39}
40
Daniel Lezcano1ca80942013-04-03 12:15:22 +000041static inline void idle_loop_epilog(unsigned long in_purr)
Deepthi Dharwar707827f2011-11-30 02:46:42 +000042{
Anton Blanchard7ffcf8e2013-08-07 02:01:46 +100043 u64 wait_cycles;
44
45 wait_cycles = be64_to_cpu(get_lppaca()->wait_state_cycles);
46 wait_cycles += mfspr(SPRN_PURR) - in_purr;
47 get_lppaca()->wait_state_cycles = cpu_to_be64(wait_cycles);
Deepthi Dharwar707827f2011-11-30 02:46:42 +000048 get_lppaca()->idle = 0;
Nicolas Pitred8c6ad32014-01-29 12:45:10 -050049
50 if (irqs_disabled())
51 local_irq_enable();
52 ppc64_runlatch_on();
Deepthi Dharwar707827f2011-11-30 02:46:42 +000053}
54
55static int snooze_loop(struct cpuidle_device *dev,
56 struct cpuidle_driver *drv,
57 int index)
58{
59 unsigned long in_purr;
Deepthi Dharwar707827f2011-11-30 02:46:42 +000060
Daniel Lezcano1ca80942013-04-03 12:15:22 +000061 idle_loop_prolog(&in_purr);
Deepthi Dharwar83dac592012-10-03 18:42:26 +000062 local_irq_enable();
63 set_thread_flag(TIF_POLLING_NRFLAG);
Deepthi Dharwar707827f2011-11-30 02:46:42 +000064
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +053065 while (!need_resched()) {
Deepthi Dharwar83dac592012-10-03 18:42:26 +000066 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
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000105 HMT_medium();
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100106 check_and_cede_processor();
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000107
108 get_lppaca()->donate_dedicated_cpu = 0;
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000109
110 idle_loop_epilog(in_purr);
111
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000112 return index;
113}
114
115static int shared_cede_loop(struct cpuidle_device *dev,
116 struct cpuidle_driver *drv,
117 int index)
118{
119 unsigned long in_purr;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000120
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000121 idle_loop_prolog(&in_purr);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000122
123 /*
124 * Yield the processor to the hypervisor. We return if
125 * an external interrupt occurs (which are driven prior
126 * to returning here) or if a prod occurs from another
127 * processor. When returning here, external interrupts
128 * are enabled.
129 */
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100130 check_and_cede_processor();
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000131
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000132 idle_loop_epilog(in_purr);
133
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000134 return index;
135}
136
137/*
138 * States for dedicated partition case.
139 */
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530140static struct cpuidle_state dedicated_states[] = {
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000141 { /* Snooze */
142 .name = "snooze",
143 .desc = "snooze",
144 .flags = CPUIDLE_FLAG_TIME_VALID,
145 .exit_latency = 0,
146 .target_residency = 0,
147 .enter = &snooze_loop },
148 { /* CEDE */
149 .name = "CEDE",
150 .desc = "CEDE",
151 .flags = CPUIDLE_FLAG_TIME_VALID,
Deepthi Dharwar83dac592012-10-03 18:42:26 +0000152 .exit_latency = 10,
153 .target_residency = 100,
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000154 .enter = &dedicated_cede_loop },
155};
156
157/*
158 * States for shared partition case.
159 */
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530160static struct cpuidle_state shared_states[] = {
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000161 { /* Shared Cede */
162 .name = "Shared Cede",
163 .desc = "Shared Cede",
164 .flags = CPUIDLE_FLAG_TIME_VALID,
165 .exit_latency = 0,
166 .target_residency = 0,
167 .enter = &shared_cede_loop },
168};
169
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000170static int pseries_cpuidle_add_cpu_notifier(struct notifier_block *n,
171 unsigned long action, void *hcpu)
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000172{
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000173 int hotcpu = (unsigned long)hcpu;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000174 struct cpuidle_device *dev =
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +0530175 per_cpu(cpuidle_devices, hotcpu);
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000176
Deepthi Dharwar852d8cb2012-07-03 20:07:22 +0000177 if (dev && cpuidle_get_driver()) {
178 switch (action) {
179 case CPU_ONLINE:
180 case CPU_ONLINE_FROZEN:
181 cpuidle_pause_and_lock();
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000182 cpuidle_enable_device(dev);
Deepthi Dharwar852d8cb2012-07-03 20:07:22 +0000183 cpuidle_resume_and_unlock();
184 break;
185
186 case CPU_DEAD:
187 case CPU_DEAD_FROZEN:
188 cpuidle_pause_and_lock();
189 cpuidle_disable_device(dev);
190 cpuidle_resume_and_unlock();
191 break;
192
193 default:
194 return NOTIFY_DONE;
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000195 }
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000196 }
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000197 return NOTIFY_OK;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000198}
199
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000200static struct notifier_block setup_hotplug_notifier = {
201 .notifier_call = pseries_cpuidle_add_cpu_notifier,
202};
203
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000204/*
205 * pseries_cpuidle_driver_init()
206 */
207static int pseries_cpuidle_driver_init(void)
208{
209 int idle_state;
210 struct cpuidle_driver *drv = &pseries_idle_driver;
211
212 drv->state_count = 0;
213
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530214 for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
215 /* Is the state not enabled? */
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000216 if (cpuidle_state_table[idle_state].enter == NULL)
217 continue;
218
219 drv->states[drv->state_count] = /* structure copy */
220 cpuidle_state_table[idle_state];
221
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000222 drv->state_count += 1;
223 }
224
225 return 0;
226}
227
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000228/*
229 * pseries_idle_probe()
230 * Choose state table for shared versus dedicated partition
231 */
232static int pseries_idle_probe(void)
233{
234
Deepthi Dharware8bb3e02011-11-30 02:47:03 +0000235 if (cpuidle_disable != IDLE_NO_OVERRIDE)
236 return -ENODEV;
237
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +0530238 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530239 if (lppaca_shared_proc(get_lppaca())) {
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +0530240 cpuidle_state_table = shared_states;
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530241 max_idle_state = ARRAY_SIZE(shared_states);
242 } else {
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +0530243 cpuidle_state_table = dedicated_states;
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530244 max_idle_state = ARRAY_SIZE(dedicated_states);
245 }
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +0530246 } else
247 return -ENODEV;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000248
249 return 0;
250}
251
252static int __init pseries_processor_idle_init(void)
253{
254 int retval;
255
256 retval = pseries_idle_probe();
257 if (retval)
258 return retval;
259
260 pseries_cpuidle_driver_init();
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +0530261 retval = cpuidle_register(&pseries_idle_driver, NULL);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000262 if (retval) {
263 printk(KERN_DEBUG "Registration of pseries driver failed.\n");
264 return retval;
265 }
266
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000267 register_cpu_notifier(&setup_hotplug_notifier);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000268 printk(KERN_DEBUG "pseries_idle_driver registered\n");
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000269 return 0;
270}
271
Deepthi Dharwar12431c62014-01-14 16:26:18 +0530272device_initcall(pseries_processor_idle_init);