blob: bb56091685d3c10d6a167af30610c4e25c089623 [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{
Deepthi Dharwar707827f2011-11-30 02:46:42 +000032 *in_purr = mfspr(SPRN_PURR);
33 /*
34 * Indicate to the HV that we are idle. Now would be
35 * a good time to find other work to dispatch.
36 */
37 get_lppaca()->idle = 1;
38}
39
Daniel Lezcano1ca80942013-04-03 12:15:22 +000040static inline void idle_loop_epilog(unsigned long in_purr)
Deepthi Dharwar707827f2011-11-30 02:46:42 +000041{
Anton Blanchard7ffcf8e2013-08-07 02:01:46 +100042 u64 wait_cycles;
43
44 wait_cycles = be64_to_cpu(get_lppaca()->wait_state_cycles);
45 wait_cycles += mfspr(SPRN_PURR) - in_purr;
46 get_lppaca()->wait_state_cycles = cpu_to_be64(wait_cycles);
Deepthi Dharwar707827f2011-11-30 02:46:42 +000047 get_lppaca()->idle = 0;
Deepthi Dharwar707827f2011-11-30 02:46:42 +000048}
49
50static int snooze_loop(struct cpuidle_device *dev,
51 struct cpuidle_driver *drv,
52 int index)
53{
54 unsigned long in_purr;
Deepthi Dharwar707827f2011-11-30 02:46:42 +000055
Daniel Lezcano1ca80942013-04-03 12:15:22 +000056 idle_loop_prolog(&in_purr);
Deepthi Dharwar83dac592012-10-03 18:42:26 +000057 local_irq_enable();
58 set_thread_flag(TIF_POLLING_NRFLAG);
Deepthi Dharwar707827f2011-11-30 02:46:42 +000059
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +053060 while (!need_resched()) {
Deepthi Dharwar83dac592012-10-03 18:42:26 +000061 HMT_low();
62 HMT_very_low();
Deepthi Dharwar707827f2011-11-30 02:46:42 +000063 }
64
Deepthi Dharwar707827f2011-11-30 02:46:42 +000065 HMT_medium();
Deepthi Dharwar83dac592012-10-03 18:42:26 +000066 clear_thread_flag(TIF_POLLING_NRFLAG);
67 smp_mb();
68
Daniel Lezcano1ca80942013-04-03 12:15:22 +000069 idle_loop_epilog(in_purr);
70
Deepthi Dharwar707827f2011-11-30 02:46:42 +000071 return index;
72}
73
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +110074static void check_and_cede_processor(void)
75{
76 /*
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +100077 * Ensure our interrupt state is properly tracked,
78 * also checks if no interrupt has occurred while we
79 * were soft-disabled
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +110080 */
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +100081 if (prep_irq_for_idle()) {
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +110082 cede_processor();
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +100083#ifdef CONFIG_TRACE_IRQFLAGS
84 /* Ensure that H_CEDE returns with IRQs on */
85 if (WARN_ON(!(mfmsr() & MSR_EE)))
86 __hard_irq_enable();
87#endif
88 }
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +110089}
90
Deepthi Dharwar707827f2011-11-30 02:46:42 +000091static int dedicated_cede_loop(struct cpuidle_device *dev,
92 struct cpuidle_driver *drv,
93 int index)
94{
95 unsigned long in_purr;
Deepthi Dharwar707827f2011-11-30 02:46:42 +000096
Daniel Lezcano1ca80942013-04-03 12:15:22 +000097 idle_loop_prolog(&in_purr);
Deepthi Dharwar707827f2011-11-30 02:46:42 +000098 get_lppaca()->donate_dedicated_cpu = 1;
99
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000100 HMT_medium();
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100101 check_and_cede_processor();
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000102
103 get_lppaca()->donate_dedicated_cpu = 0;
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000104
105 idle_loop_epilog(in_purr);
106
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000107 return index;
108}
109
110static int shared_cede_loop(struct cpuidle_device *dev,
111 struct cpuidle_driver *drv,
112 int index)
113{
114 unsigned long in_purr;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000115
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000116 idle_loop_prolog(&in_purr);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000117
118 /*
119 * Yield the processor to the hypervisor. We return if
120 * an external interrupt occurs (which are driven prior
121 * to returning here) or if a prod occurs from another
122 * processor. When returning here, external interrupts
123 * are enabled.
124 */
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100125 check_and_cede_processor();
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000126
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000127 idle_loop_epilog(in_purr);
128
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000129 return index;
130}
131
132/*
133 * States for dedicated partition case.
134 */
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530135static struct cpuidle_state dedicated_states[] = {
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000136 { /* Snooze */
137 .name = "snooze",
138 .desc = "snooze",
139 .flags = CPUIDLE_FLAG_TIME_VALID,
140 .exit_latency = 0,
141 .target_residency = 0,
142 .enter = &snooze_loop },
143 { /* CEDE */
144 .name = "CEDE",
145 .desc = "CEDE",
146 .flags = CPUIDLE_FLAG_TIME_VALID,
Deepthi Dharwar83dac592012-10-03 18:42:26 +0000147 .exit_latency = 10,
148 .target_residency = 100,
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000149 .enter = &dedicated_cede_loop },
150};
151
152/*
153 * States for shared partition case.
154 */
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530155static struct cpuidle_state shared_states[] = {
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000156 { /* Shared Cede */
157 .name = "Shared Cede",
158 .desc = "Shared Cede",
159 .flags = CPUIDLE_FLAG_TIME_VALID,
160 .exit_latency = 0,
161 .target_residency = 0,
162 .enter = &shared_cede_loop },
163};
164
Deepthi Dharwar8ea959a2012-10-03 18:42:18 +0000165void update_smt_snooze_delay(int cpu, int residency)
166{
167 struct cpuidle_driver *drv = cpuidle_get_driver();
168 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
169
170 if (cpuidle_state_table != dedicated_states)
171 return;
172
173 if (residency < 0) {
174 /* Disable the Nap state on that cpu */
175 if (dev)
176 dev->states_usage[1].disable = 1;
177 } else
178 if (drv)
Deepthi Dharwar83dac592012-10-03 18:42:26 +0000179 drv->states[1].target_residency = residency;
Deepthi Dharwar8ea959a2012-10-03 18:42:18 +0000180}
181
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000182static int pseries_cpuidle_add_cpu_notifier(struct notifier_block *n,
183 unsigned long action, void *hcpu)
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000184{
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000185 int hotcpu = (unsigned long)hcpu;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000186 struct cpuidle_device *dev =
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +0530187 per_cpu(cpuidle_devices, hotcpu);
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000188
Deepthi Dharwar852d8cb2012-07-03 20:07:22 +0000189 if (dev && cpuidle_get_driver()) {
190 switch (action) {
191 case CPU_ONLINE:
192 case CPU_ONLINE_FROZEN:
193 cpuidle_pause_and_lock();
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000194 cpuidle_enable_device(dev);
Deepthi Dharwar852d8cb2012-07-03 20:07:22 +0000195 cpuidle_resume_and_unlock();
196 break;
197
198 case CPU_DEAD:
199 case CPU_DEAD_FROZEN:
200 cpuidle_pause_and_lock();
201 cpuidle_disable_device(dev);
202 cpuidle_resume_and_unlock();
203 break;
204
205 default:
206 return NOTIFY_DONE;
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000207 }
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000208 }
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000209 return NOTIFY_OK;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000210}
211
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000212static struct notifier_block setup_hotplug_notifier = {
213 .notifier_call = pseries_cpuidle_add_cpu_notifier,
214};
215
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000216/*
217 * pseries_cpuidle_driver_init()
218 */
219static int pseries_cpuidle_driver_init(void)
220{
221 int idle_state;
222 struct cpuidle_driver *drv = &pseries_idle_driver;
223
224 drv->state_count = 0;
225
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530226 for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
227 /* Is the state not enabled? */
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000228 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
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000234 drv->state_count += 1;
235 }
236
237 return 0;
238}
239
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000240/*
241 * pseries_idle_probe()
242 * Choose state table for shared versus dedicated partition
243 */
244static int pseries_idle_probe(void)
245{
246
Deepthi Dharware8bb3e02011-11-30 02:47:03 +0000247 if (cpuidle_disable != IDLE_NO_OVERRIDE)
248 return -ENODEV;
249
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +0530250 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530251 if (lppaca_shared_proc(get_lppaca())) {
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +0530252 cpuidle_state_table = shared_states;
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530253 max_idle_state = ARRAY_SIZE(shared_states);
254 } else {
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +0530255 cpuidle_state_table = dedicated_states;
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530256 max_idle_state = ARRAY_SIZE(dedicated_states);
257 }
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +0530258 } else
259 return -ENODEV;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000260
261 return 0;
262}
263
264static int __init pseries_processor_idle_init(void)
265{
266 int retval;
267
268 retval = pseries_idle_probe();
269 if (retval)
270 return retval;
271
272 pseries_cpuidle_driver_init();
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +0530273 retval = cpuidle_register(&pseries_idle_driver, NULL);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000274 if (retval) {
275 printk(KERN_DEBUG "Registration of pseries driver failed.\n");
276 return retval;
277 }
278
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000279 register_cpu_notifier(&setup_hotplug_notifier);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000280 printk(KERN_DEBUG "pseries_idle_driver registered\n");
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000281 return 0;
282}
283
Deepthi Dharwar12431c62014-01-14 16:26:18 +0530284device_initcall(pseries_processor_idle_init);