blob: a187a39fb86628c958f9fed412de104f4ce8b4a9 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Deepthi Dharwar707827f2011-11-30 02:46:42 +00002/*
Deepthi Dharwar962e7bd2014-01-14 16:26:02 +05303 * cpuidle-pseries - idle state cpuidle driver.
Deepthi Dharwar707827f2011-11-30 02:46:42 +00004 * Adapted from drivers/idle/intel_idle.c and
5 * drivers/acpi/processor_idle.c
6 *
7 */
8
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/moduleparam.h>
13#include <linux/cpuidle.h>
14#include <linux/cpu.h>
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +000015#include <linux/notifier.h>
Deepthi Dharwar707827f2011-11-30 02:46:42 +000016
17#include <asm/paca.h>
18#include <asm/reg.h>
Deepthi Dharwar707827f2011-11-30 02:46:42 +000019#include <asm/machdep.h>
20#include <asm/firmware.h>
Preeti U Murthy3f67d962014-02-12 10:18:45 +053021#include <asm/runlatch.h>
Deepthi Dharwar212bebb2013-08-22 15:23:52 +053022#include <asm/plpar_wrappers.h>
Deepthi Dharwar707827f2011-11-30 02:46:42 +000023
24struct cpuidle_driver pseries_idle_driver = {
Daniel Lezcano1ca80942013-04-03 12:15:22 +000025 .name = "pseries_idle",
26 .owner = THIS_MODULE,
Deepthi Dharwar707827f2011-11-30 02:46:42 +000027};
28
Nicholas Piggin624e46d2017-06-14 23:02:40 +100029static int max_idle_state __read_mostly;
30static struct cpuidle_state *cpuidle_state_table __read_mostly;
31static u64 snooze_timeout __read_mostly;
32static bool snooze_timeout_en __read_mostly;
Deepthi Dharwar707827f2011-11-30 02:46:42 +000033
Daniel Lezcano1ca80942013-04-03 12:15:22 +000034static inline void idle_loop_prolog(unsigned long *in_purr)
Deepthi Dharwar707827f2011-11-30 02:46:42 +000035{
Nicolas Pitred8c6ad32014-01-29 12:45:10 -050036 ppc64_runlatch_off();
Deepthi Dharwar707827f2011-11-30 02:46:42 +000037 *in_purr = mfspr(SPRN_PURR);
38 /*
39 * Indicate to the HV that we are idle. Now would be
40 * a good time to find other work to dispatch.
41 */
42 get_lppaca()->idle = 1;
43}
44
Daniel Lezcano1ca80942013-04-03 12:15:22 +000045static inline void idle_loop_epilog(unsigned long in_purr)
Deepthi Dharwar707827f2011-11-30 02:46:42 +000046{
Anton Blanchard7ffcf8e2013-08-07 02:01:46 +100047 u64 wait_cycles;
48
49 wait_cycles = be64_to_cpu(get_lppaca()->wait_state_cycles);
50 wait_cycles += mfspr(SPRN_PURR) - in_purr;
51 get_lppaca()->wait_state_cycles = cpu_to_be64(wait_cycles);
Deepthi Dharwar707827f2011-11-30 02:46:42 +000052 get_lppaca()->idle = 0;
Nicolas Pitred8c6ad32014-01-29 12:45:10 -050053
54 if (irqs_disabled())
55 local_irq_enable();
56 ppc64_runlatch_on();
Deepthi Dharwar707827f2011-11-30 02:46:42 +000057}
58
59static int snooze_loop(struct cpuidle_device *dev,
60 struct cpuidle_driver *drv,
61 int index)
62{
63 unsigned long in_purr;
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053064 u64 snooze_exit_time;
Deepthi Dharwar707827f2011-11-30 02:46:42 +000065
Nicholas Piggin3fc5ee922017-06-14 23:02:39 +100066 set_thread_flag(TIF_POLLING_NRFLAG);
67
Daniel Lezcano1ca80942013-04-03 12:15:22 +000068 idle_loop_prolog(&in_purr);
Deepthi Dharwar83dac592012-10-03 18:42:26 +000069 local_irq_enable();
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053070 snooze_exit_time = get_tb() + snooze_timeout;
Deepthi Dharwar707827f2011-11-30 02:46:42 +000071
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +053072 while (!need_resched()) {
Deepthi Dharwar83dac592012-10-03 18:42:26 +000073 HMT_low();
74 HMT_very_low();
Nicholas Piggin7ded4292017-06-14 23:02:41 +100075 if (likely(snooze_timeout_en) && get_tb() > snooze_exit_time) {
76 /*
77 * Task has not woken up but we are exiting the polling
78 * loop anyway. Require a barrier after polling is
79 * cleared to order subsequent test of need_resched().
80 */
81 clear_thread_flag(TIF_POLLING_NRFLAG);
82 smp_mb();
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053083 break;
Nicholas Piggin7ded4292017-06-14 23:02:41 +100084 }
Deepthi Dharwar707827f2011-11-30 02:46:42 +000085 }
86
Deepthi Dharwar707827f2011-11-30 02:46:42 +000087 HMT_medium();
Deepthi Dharwar83dac592012-10-03 18:42:26 +000088 clear_thread_flag(TIF_POLLING_NRFLAG);
Deepthi Dharwar83dac592012-10-03 18:42:26 +000089
Daniel Lezcano1ca80942013-04-03 12:15:22 +000090 idle_loop_epilog(in_purr);
91
Deepthi Dharwar707827f2011-11-30 02:46:42 +000092 return index;
93}
94
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +110095static void check_and_cede_processor(void)
96{
97 /*
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +100098 * Ensure our interrupt state is properly tracked,
99 * also checks if no interrupt has occurred while we
100 * were soft-disabled
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100101 */
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +1000102 if (prep_irq_for_idle()) {
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100103 cede_processor();
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +1000104#ifdef CONFIG_TRACE_IRQFLAGS
105 /* Ensure that H_CEDE returns with IRQs on */
106 if (WARN_ON(!(mfmsr() & MSR_EE)))
107 __hard_irq_enable();
108#endif
109 }
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100110}
111
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000112static int dedicated_cede_loop(struct cpuidle_device *dev,
113 struct cpuidle_driver *drv,
114 int index)
115{
116 unsigned long in_purr;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000117
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000118 idle_loop_prolog(&in_purr);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000119 get_lppaca()->donate_dedicated_cpu = 1;
120
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000121 HMT_medium();
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100122 check_and_cede_processor();
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000123
124 get_lppaca()->donate_dedicated_cpu = 0;
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000125
126 idle_loop_epilog(in_purr);
127
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000128 return index;
129}
130
131static int shared_cede_loop(struct cpuidle_device *dev,
132 struct cpuidle_driver *drv,
133 int index)
134{
135 unsigned long in_purr;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000136
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000137 idle_loop_prolog(&in_purr);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000138
139 /*
140 * Yield the processor to the hypervisor. We return if
141 * an external interrupt occurs (which are driven prior
142 * to returning here) or if a prod occurs from another
143 * processor. When returning here, external interrupts
144 * are enabled.
145 */
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100146 check_and_cede_processor();
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000147
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000148 idle_loop_epilog(in_purr);
149
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000150 return index;
151}
152
153/*
154 * States for dedicated partition case.
155 */
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530156static struct cpuidle_state dedicated_states[] = {
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000157 { /* Snooze */
158 .name = "snooze",
159 .desc = "snooze",
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000160 .exit_latency = 0,
161 .target_residency = 0,
162 .enter = &snooze_loop },
163 { /* CEDE */
164 .name = "CEDE",
165 .desc = "CEDE",
Deepthi Dharwar83dac592012-10-03 18:42:26 +0000166 .exit_latency = 10,
167 .target_residency = 100,
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000168 .enter = &dedicated_cede_loop },
169};
170
171/*
172 * States for shared partition case.
173 */
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530174static struct cpuidle_state shared_states[] = {
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000175 { /* Shared Cede */
176 .name = "Shared Cede",
177 .desc = "Shared Cede",
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000178 .exit_latency = 0,
179 .target_residency = 0,
180 .enter = &shared_cede_loop },
181};
182
Sebastian Andrzej Siewior529351f2016-08-18 14:57:25 +0200183static int pseries_cpuidle_cpu_online(unsigned int cpu)
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000184{
Sebastian Andrzej Siewior529351f2016-08-18 14:57:25 +0200185 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000186
Deepthi Dharwar852d8cb2012-07-03 20:07:22 +0000187 if (dev && cpuidle_get_driver()) {
Sebastian Andrzej Siewior529351f2016-08-18 14:57:25 +0200188 cpuidle_pause_and_lock();
189 cpuidle_enable_device(dev);
190 cpuidle_resume_and_unlock();
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000191 }
Sebastian Andrzej Siewior529351f2016-08-18 14:57:25 +0200192 return 0;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000193}
194
Sebastian Andrzej Siewior529351f2016-08-18 14:57:25 +0200195static int pseries_cpuidle_cpu_dead(unsigned int cpu)
196{
197 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
198
199 if (dev && cpuidle_get_driver()) {
200 cpuidle_pause_and_lock();
201 cpuidle_disable_device(dev);
202 cpuidle_resume_and_unlock();
203 }
204 return 0;
205}
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000206
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000207/*
208 * pseries_cpuidle_driver_init()
209 */
210static int pseries_cpuidle_driver_init(void)
211{
212 int idle_state;
213 struct cpuidle_driver *drv = &pseries_idle_driver;
214
215 drv->state_count = 0;
216
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530217 for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
218 /* Is the state not enabled? */
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000219 if (cpuidle_state_table[idle_state].enter == NULL)
220 continue;
221
222 drv->states[drv->state_count] = /* structure copy */
223 cpuidle_state_table[idle_state];
224
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000225 drv->state_count += 1;
226 }
227
228 return 0;
229}
230
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000231/*
232 * pseries_idle_probe()
233 * Choose state table for shared versus dedicated partition
234 */
235static int pseries_idle_probe(void)
236{
237
Deepthi Dharware8bb3e02011-11-30 02:47:03 +0000238 if (cpuidle_disable != IDLE_NO_OVERRIDE)
239 return -ENODEV;
240
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +0530241 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530242 if (lppaca_shared_proc(get_lppaca())) {
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +0530243 cpuidle_state_table = shared_states;
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530244 max_idle_state = ARRAY_SIZE(shared_states);
245 } else {
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +0530246 cpuidle_state_table = dedicated_states;
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530247 max_idle_state = ARRAY_SIZE(dedicated_states);
248 }
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +0530249 } else
250 return -ENODEV;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000251
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +0530252 if (max_idle_state > 1) {
253 snooze_timeout_en = true;
254 snooze_timeout = cpuidle_state_table[1].target_residency *
255 tb_ticks_per_usec;
256 }
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000257 return 0;
258}
259
260static int __init pseries_processor_idle_init(void)
261{
262 int retval;
263
264 retval = pseries_idle_probe();
265 if (retval)
266 return retval;
267
268 pseries_cpuidle_driver_init();
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +0530269 retval = cpuidle_register(&pseries_idle_driver, NULL);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000270 if (retval) {
271 printk(KERN_DEBUG "Registration of pseries driver failed.\n");
272 return retval;
273 }
274
Sebastian Andrzej Siewior529351f2016-08-18 14:57:25 +0200275 retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
276 "cpuidle/pseries:online",
277 pseries_cpuidle_cpu_online, NULL);
278 WARN_ON(retval < 0);
279 retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD,
280 "cpuidle/pseries:DEAD", NULL,
281 pseries_cpuidle_cpu_dead);
282 WARN_ON(retval < 0);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000283 printk(KERN_DEBUG "pseries_idle_driver registered\n");
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000284 return 0;
285}
286
Deepthi Dharwar12431c62014-01-14 16:26:18 +0530287device_initcall(pseries_processor_idle_init);