blob: 07135e009d8b9ce7590c3b442d610c790a1ff206 [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;
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053030static u64 snooze_timeout;
31static bool snooze_timeout_en;
Deepthi Dharwar707827f2011-11-30 02:46:42 +000032
Daniel Lezcano1ca80942013-04-03 12:15:22 +000033static inline void idle_loop_prolog(unsigned long *in_purr)
Deepthi Dharwar707827f2011-11-30 02:46:42 +000034{
Nicolas Pitred8c6ad32014-01-29 12:45:10 -050035 ppc64_runlatch_off();
Deepthi Dharwar707827f2011-11-30 02:46:42 +000036 *in_purr = mfspr(SPRN_PURR);
37 /*
38 * Indicate to the HV that we are idle. Now would be
39 * a good time to find other work to dispatch.
40 */
41 get_lppaca()->idle = 1;
42}
43
Daniel Lezcano1ca80942013-04-03 12:15:22 +000044static inline void idle_loop_epilog(unsigned long in_purr)
Deepthi Dharwar707827f2011-11-30 02:46:42 +000045{
Anton Blanchard7ffcf8e2013-08-07 02:01:46 +100046 u64 wait_cycles;
47
48 wait_cycles = be64_to_cpu(get_lppaca()->wait_state_cycles);
49 wait_cycles += mfspr(SPRN_PURR) - in_purr;
50 get_lppaca()->wait_state_cycles = cpu_to_be64(wait_cycles);
Deepthi Dharwar707827f2011-11-30 02:46:42 +000051 get_lppaca()->idle = 0;
Nicolas Pitred8c6ad32014-01-29 12:45:10 -050052
53 if (irqs_disabled())
54 local_irq_enable();
55 ppc64_runlatch_on();
Deepthi Dharwar707827f2011-11-30 02:46:42 +000056}
57
58static int snooze_loop(struct cpuidle_device *dev,
59 struct cpuidle_driver *drv,
60 int index)
61{
62 unsigned long in_purr;
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053063 u64 snooze_exit_time;
Deepthi Dharwar707827f2011-11-30 02:46:42 +000064
Daniel Lezcano1ca80942013-04-03 12:15:22 +000065 idle_loop_prolog(&in_purr);
Deepthi Dharwar83dac592012-10-03 18:42:26 +000066 local_irq_enable();
67 set_thread_flag(TIF_POLLING_NRFLAG);
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053068 snooze_exit_time = get_tb() + snooze_timeout;
Deepthi Dharwar707827f2011-11-30 02:46:42 +000069
Deepthi Dharwarb69dbba2014-01-14 16:26:09 +053070 while (!need_resched()) {
Deepthi Dharwar83dac592012-10-03 18:42:26 +000071 HMT_low();
72 HMT_very_low();
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053073 if (snooze_timeout_en && get_tb() > snooze_exit_time)
74 break;
Deepthi Dharwar707827f2011-11-30 02:46:42 +000075 }
76
Deepthi Dharwar707827f2011-11-30 02:46:42 +000077 HMT_medium();
Deepthi Dharwar83dac592012-10-03 18:42:26 +000078 clear_thread_flag(TIF_POLLING_NRFLAG);
79 smp_mb();
80
Daniel Lezcano1ca80942013-04-03 12:15:22 +000081 idle_loop_epilog(in_purr);
82
Deepthi Dharwar707827f2011-11-30 02:46:42 +000083 return index;
84}
85
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +110086static void check_and_cede_processor(void)
87{
88 /*
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +100089 * Ensure our interrupt state is properly tracked,
90 * also checks if no interrupt has occurred while we
91 * were soft-disabled
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +110092 */
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +100093 if (prep_irq_for_idle()) {
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +110094 cede_processor();
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +100095#ifdef CONFIG_TRACE_IRQFLAGS
96 /* Ensure that H_CEDE returns with IRQs on */
97 if (WARN_ON(!(mfmsr() & MSR_EE)))
98 __hard_irq_enable();
99#endif
100 }
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100101}
102
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000103static int dedicated_cede_loop(struct cpuidle_device *dev,
104 struct cpuidle_driver *drv,
105 int index)
106{
107 unsigned long in_purr;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000108
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000109 idle_loop_prolog(&in_purr);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000110 get_lppaca()->donate_dedicated_cpu = 1;
111
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000112 HMT_medium();
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100113 check_and_cede_processor();
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000114
115 get_lppaca()->donate_dedicated_cpu = 0;
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000116
117 idle_loop_epilog(in_purr);
118
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000119 return index;
120}
121
122static int shared_cede_loop(struct cpuidle_device *dev,
123 struct cpuidle_driver *drv,
124 int index)
125{
126 unsigned long in_purr;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000127
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000128 idle_loop_prolog(&in_purr);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000129
130 /*
131 * Yield the processor to the hypervisor. We return if
132 * an external interrupt occurs (which are driven prior
133 * to returning here) or if a prod occurs from another
134 * processor. When returning here, external interrupts
135 * are enabled.
136 */
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100137 check_and_cede_processor();
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000138
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000139 idle_loop_epilog(in_purr);
140
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000141 return index;
142}
143
144/*
145 * States for dedicated partition case.
146 */
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530147static struct cpuidle_state dedicated_states[] = {
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000148 { /* Snooze */
149 .name = "snooze",
150 .desc = "snooze",
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000151 .exit_latency = 0,
152 .target_residency = 0,
153 .enter = &snooze_loop },
154 { /* CEDE */
155 .name = "CEDE",
156 .desc = "CEDE",
Deepthi Dharwar83dac592012-10-03 18:42:26 +0000157 .exit_latency = 10,
158 .target_residency = 100,
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000159 .enter = &dedicated_cede_loop },
160};
161
162/*
163 * States for shared partition case.
164 */
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530165static struct cpuidle_state shared_states[] = {
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000166 { /* Shared Cede */
167 .name = "Shared Cede",
168 .desc = "Shared Cede",
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000169 .exit_latency = 0,
170 .target_residency = 0,
171 .enter = &shared_cede_loop },
172};
173
Deepthi Dharwar16aaaff62012-05-20 18:34:27 +0000174static int pseries_cpuidle_add_cpu_notifier(struct notifier_block *n,
175 unsigned long action, void *hcpu)
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000176{
Deepthi Dharwar16aaaff62012-05-20 18:34:27 +0000177 int hotcpu = (unsigned long)hcpu;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000178 struct cpuidle_device *dev =
Deepthi Dharwarb69dbba2014-01-14 16:26:09 +0530179 per_cpu(cpuidle_devices, hotcpu);
Deepthi Dharwar16aaaff62012-05-20 18:34:27 +0000180
Deepthi Dharwar852d8cb2012-07-03 20:07:22 +0000181 if (dev && cpuidle_get_driver()) {
182 switch (action) {
183 case CPU_ONLINE:
184 case CPU_ONLINE_FROZEN:
185 cpuidle_pause_and_lock();
Deepthi Dharwar16aaaff62012-05-20 18:34:27 +0000186 cpuidle_enable_device(dev);
Deepthi Dharwar852d8cb2012-07-03 20:07:22 +0000187 cpuidle_resume_and_unlock();
188 break;
189
190 case CPU_DEAD:
191 case CPU_DEAD_FROZEN:
192 cpuidle_pause_and_lock();
193 cpuidle_disable_device(dev);
194 cpuidle_resume_and_unlock();
195 break;
196
197 default:
198 return NOTIFY_DONE;
Deepthi Dharwar16aaaff62012-05-20 18:34:27 +0000199 }
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000200 }
Deepthi Dharwar16aaaff62012-05-20 18:34:27 +0000201 return NOTIFY_OK;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000202}
203
Deepthi Dharwar16aaaff62012-05-20 18:34:27 +0000204static struct notifier_block setup_hotplug_notifier = {
205 .notifier_call = pseries_cpuidle_add_cpu_notifier,
206};
207
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000208/*
209 * pseries_cpuidle_driver_init()
210 */
211static int pseries_cpuidle_driver_init(void)
212{
213 int idle_state;
214 struct cpuidle_driver *drv = &pseries_idle_driver;
215
216 drv->state_count = 0;
217
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530218 for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
219 /* Is the state not enabled? */
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000220 if (cpuidle_state_table[idle_state].enter == NULL)
221 continue;
222
223 drv->states[drv->state_count] = /* structure copy */
224 cpuidle_state_table[idle_state];
225
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000226 drv->state_count += 1;
227 }
228
229 return 0;
230}
231
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000232/*
233 * pseries_idle_probe()
234 * Choose state table for shared versus dedicated partition
235 */
236static int pseries_idle_probe(void)
237{
238
Deepthi Dharware8bb3e02011-11-30 02:47:03 +0000239 if (cpuidle_disable != IDLE_NO_OVERRIDE)
240 return -ENODEV;
241
Deepthi Dharwarb69dbba2014-01-14 16:26:09 +0530242 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530243 if (lppaca_shared_proc(get_lppaca())) {
Deepthi Dharwarb69dbba2014-01-14 16:26:09 +0530244 cpuidle_state_table = shared_states;
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530245 max_idle_state = ARRAY_SIZE(shared_states);
246 } else {
Deepthi Dharwarb69dbba2014-01-14 16:26:09 +0530247 cpuidle_state_table = dedicated_states;
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530248 max_idle_state = ARRAY_SIZE(dedicated_states);
249 }
Deepthi Dharwarb69dbba2014-01-14 16:26:09 +0530250 } else
251 return -ENODEV;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000252
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +0530253 if (max_idle_state > 1) {
254 snooze_timeout_en = true;
255 snooze_timeout = cpuidle_state_table[1].target_residency *
256 tb_ticks_per_usec;
257 }
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000258 return 0;
259}
260
261static int __init pseries_processor_idle_init(void)
262{
263 int retval;
264
265 retval = pseries_idle_probe();
266 if (retval)
267 return retval;
268
269 pseries_cpuidle_driver_init();
Deepthi Dharwarb69dbba2014-01-14 16:26:09 +0530270 retval = cpuidle_register(&pseries_idle_driver, NULL);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000271 if (retval) {
272 printk(KERN_DEBUG "Registration of pseries driver failed.\n");
273 return retval;
274 }
275
Deepthi Dharwar16aaaff62012-05-20 18:34:27 +0000276 register_cpu_notifier(&setup_hotplug_notifier);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000277 printk(KERN_DEBUG "pseries_idle_driver registered\n");
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000278 return 0;
279}
280
Deepthi Dharwar12431c62014-01-14 16:26:18 +0530281device_initcall(pseries_processor_idle_init);