blob: bb9e2b6f3ecc33b5e9fde738d6c1405a4a2d642e [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 Dharwar16aaaff62012-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>
Preeti U Murthy3f67d962014-02-12 10:18:45 +053020#include <asm/runlatch.h>
Deepthi Dharwar212bebb2013-08-22 15:23:52 +053021#include <asm/plpar_wrappers.h>
Deepthi Dharwar707827f2011-11-30 02:46:42 +000022
23struct cpuidle_driver pseries_idle_driver = {
Daniel Lezcano1ca80942013-04-03 12:15:22 +000024 .name = "pseries_idle",
25 .owner = THIS_MODULE,
Deepthi Dharwar707827f2011-11-30 02:46:42 +000026};
27
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +053028static int max_idle_state;
Deepthi Dharwar707827f2011-11-30 02:46:42 +000029static struct cpuidle_state *cpuidle_state_table;
30
Daniel Lezcano1ca80942013-04-03 12:15:22 +000031static inline void idle_loop_prolog(unsigned long *in_purr)
Deepthi Dharwar707827f2011-11-30 02:46:42 +000032{
Nicolas Pitred8c6ad32014-01-29 12:45:10 -050033 ppc64_runlatch_off();
Deepthi Dharwar707827f2011-11-30 02:46:42 +000034 *in_purr = mfspr(SPRN_PURR);
35 /*
36 * Indicate to the HV that we are idle. Now would be
37 * a good time to find other work to dispatch.
38 */
39 get_lppaca()->idle = 1;
40}
41
Daniel Lezcano1ca80942013-04-03 12:15:22 +000042static inline void idle_loop_epilog(unsigned long in_purr)
Deepthi Dharwar707827f2011-11-30 02:46:42 +000043{
Anton Blanchard7ffcf8e2013-08-07 02:01:46 +100044 u64 wait_cycles;
45
46 wait_cycles = be64_to_cpu(get_lppaca()->wait_state_cycles);
47 wait_cycles += mfspr(SPRN_PURR) - in_purr;
48 get_lppaca()->wait_state_cycles = cpu_to_be64(wait_cycles);
Deepthi Dharwar707827f2011-11-30 02:46:42 +000049 get_lppaca()->idle = 0;
Nicolas Pitred8c6ad32014-01-29 12:45:10 -050050
51 if (irqs_disabled())
52 local_irq_enable();
53 ppc64_runlatch_on();
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 Dharwar707827f2011-11-30 02:46:42 +000061
Daniel Lezcano1ca80942013-04-03 12:15:22 +000062 idle_loop_prolog(&in_purr);
Deepthi Dharwar83dac592012-10-03 18:42:26 +000063 local_irq_enable();
64 set_thread_flag(TIF_POLLING_NRFLAG);
Deepthi Dharwar707827f2011-11-30 02:46:42 +000065
Deepthi Dharwarb69dbba2014-01-14 16:26:09 +053066 while (!need_resched()) {
Deepthi Dharwar83dac592012-10-03 18:42:26 +000067 HMT_low();
68 HMT_very_low();
Deepthi Dharwar707827f2011-11-30 02:46:42 +000069 }
70
Deepthi Dharwar707827f2011-11-30 02:46:42 +000071 HMT_medium();
Deepthi Dharwar83dac592012-10-03 18:42:26 +000072 clear_thread_flag(TIF_POLLING_NRFLAG);
73 smp_mb();
74
Daniel Lezcano1ca80942013-04-03 12:15:22 +000075 idle_loop_epilog(in_purr);
76
Deepthi Dharwar707827f2011-11-30 02:46:42 +000077 return index;
78}
79
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +110080static void check_and_cede_processor(void)
81{
82 /*
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +100083 * Ensure our interrupt state is properly tracked,
84 * also checks if no interrupt has occurred while we
85 * were soft-disabled
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +110086 */
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +100087 if (prep_irq_for_idle()) {
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +110088 cede_processor();
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +100089#ifdef CONFIG_TRACE_IRQFLAGS
90 /* Ensure that H_CEDE returns with IRQs on */
91 if (WARN_ON(!(mfmsr() & MSR_EE)))
92 __hard_irq_enable();
93#endif
94 }
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +110095}
96
Deepthi Dharwar707827f2011-11-30 02:46:42 +000097static int dedicated_cede_loop(struct cpuidle_device *dev,
98 struct cpuidle_driver *drv,
99 int index)
100{
101 unsigned long in_purr;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000102
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000103 idle_loop_prolog(&in_purr);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000104 get_lppaca()->donate_dedicated_cpu = 1;
105
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000106 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 */
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530141static struct cpuidle_state dedicated_states[] = {
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000142 { /* Snooze */
143 .name = "snooze",
144 .desc = "snooze",
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000145 .exit_latency = 0,
146 .target_residency = 0,
147 .enter = &snooze_loop },
148 { /* CEDE */
149 .name = "CEDE",
150 .desc = "CEDE",
Deepthi Dharwar83dac592012-10-03 18:42:26 +0000151 .exit_latency = 10,
152 .target_residency = 100,
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000153 .enter = &dedicated_cede_loop },
154};
155
156/*
157 * States for shared partition case.
158 */
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530159static struct cpuidle_state shared_states[] = {
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000160 { /* Shared Cede */
161 .name = "Shared Cede",
162 .desc = "Shared Cede",
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000163 .exit_latency = 0,
164 .target_residency = 0,
165 .enter = &shared_cede_loop },
166};
167
Deepthi Dharwar16aaaff62012-05-20 18:34:27 +0000168static int pseries_cpuidle_add_cpu_notifier(struct notifier_block *n,
169 unsigned long action, void *hcpu)
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000170{
Deepthi Dharwar16aaaff62012-05-20 18:34:27 +0000171 int hotcpu = (unsigned long)hcpu;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000172 struct cpuidle_device *dev =
Deepthi Dharwarb69dbba2014-01-14 16:26:09 +0530173 per_cpu(cpuidle_devices, hotcpu);
Deepthi Dharwar16aaaff62012-05-20 18:34:27 +0000174
Deepthi Dharwar852d8cb2012-07-03 20:07:22 +0000175 if (dev && cpuidle_get_driver()) {
176 switch (action) {
177 case CPU_ONLINE:
178 case CPU_ONLINE_FROZEN:
179 cpuidle_pause_and_lock();
Deepthi Dharwar16aaaff62012-05-20 18:34:27 +0000180 cpuidle_enable_device(dev);
Deepthi Dharwar852d8cb2012-07-03 20:07:22 +0000181 cpuidle_resume_and_unlock();
182 break;
183
184 case CPU_DEAD:
185 case CPU_DEAD_FROZEN:
186 cpuidle_pause_and_lock();
187 cpuidle_disable_device(dev);
188 cpuidle_resume_and_unlock();
189 break;
190
191 default:
192 return NOTIFY_DONE;
Deepthi Dharwar16aaaff62012-05-20 18:34:27 +0000193 }
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000194 }
Deepthi Dharwar16aaaff62012-05-20 18:34:27 +0000195 return NOTIFY_OK;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000196}
197
Deepthi Dharwar16aaaff62012-05-20 18:34:27 +0000198static struct notifier_block setup_hotplug_notifier = {
199 .notifier_call = pseries_cpuidle_add_cpu_notifier,
200};
201
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000202/*
203 * pseries_cpuidle_driver_init()
204 */
205static int pseries_cpuidle_driver_init(void)
206{
207 int idle_state;
208 struct cpuidle_driver *drv = &pseries_idle_driver;
209
210 drv->state_count = 0;
211
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530212 for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
213 /* Is the state not enabled? */
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000214 if (cpuidle_state_table[idle_state].enter == NULL)
215 continue;
216
217 drv->states[drv->state_count] = /* structure copy */
218 cpuidle_state_table[idle_state];
219
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000220 drv->state_count += 1;
221 }
222
223 return 0;
224}
225
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000226/*
227 * pseries_idle_probe()
228 * Choose state table for shared versus dedicated partition
229 */
230static int pseries_idle_probe(void)
231{
232
Deepthi Dharware8bb3e02011-11-30 02:47:03 +0000233 if (cpuidle_disable != IDLE_NO_OVERRIDE)
234 return -ENODEV;
235
Deepthi Dharwarb69dbba2014-01-14 16:26:09 +0530236 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530237 if (lppaca_shared_proc(get_lppaca())) {
Deepthi Dharwarb69dbba2014-01-14 16:26:09 +0530238 cpuidle_state_table = shared_states;
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530239 max_idle_state = ARRAY_SIZE(shared_states);
240 } else {
Deepthi Dharwarb69dbba2014-01-14 16:26:09 +0530241 cpuidle_state_table = dedicated_states;
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530242 max_idle_state = ARRAY_SIZE(dedicated_states);
243 }
Deepthi Dharwarb69dbba2014-01-14 16:26:09 +0530244 } else
245 return -ENODEV;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000246
247 return 0;
248}
249
250static int __init pseries_processor_idle_init(void)
251{
252 int retval;
253
254 retval = pseries_idle_probe();
255 if (retval)
256 return retval;
257
258 pseries_cpuidle_driver_init();
Deepthi Dharwarb69dbba2014-01-14 16:26:09 +0530259 retval = cpuidle_register(&pseries_idle_driver, NULL);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000260 if (retval) {
261 printk(KERN_DEBUG "Registration of pseries driver failed.\n");
262 return retval;
263 }
264
Deepthi Dharwar16aaaff62012-05-20 18:34:27 +0000265 register_cpu_notifier(&setup_hotplug_notifier);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000266 printk(KERN_DEBUG "pseries_idle_driver registered\n");
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000267 return 0;
268}
269
Deepthi Dharwar12431c62014-01-14 16:26:18 +0530270device_initcall(pseries_processor_idle_init);