blob: e9b3853d93ea3754cd1972c765a2bab2c8bc4e83 [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>
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
Nicholas Piggin624e46d2017-06-14 23:02:40 +100028static int max_idle_state __read_mostly;
29static struct cpuidle_state *cpuidle_state_table __read_mostly;
30static u64 snooze_timeout __read_mostly;
31static bool snooze_timeout_en __read_mostly;
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
Nicholas Piggin3fc5ee922017-06-14 23:02:39 +100065 set_thread_flag(TIF_POLLING_NRFLAG);
66
Daniel Lezcano1ca80942013-04-03 12:15:22 +000067 idle_loop_prolog(&in_purr);
Deepthi Dharwar83dac592012-10-03 18:42:26 +000068 local_irq_enable();
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053069 snooze_exit_time = get_tb() + snooze_timeout;
Deepthi Dharwar707827f2011-11-30 02:46:42 +000070
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +053071 while (!need_resched()) {
Deepthi Dharwar83dac592012-10-03 18:42:26 +000072 HMT_low();
73 HMT_very_low();
Nicholas Piggin7ded4292017-06-14 23:02:41 +100074 if (likely(snooze_timeout_en) && get_tb() > snooze_exit_time) {
75 /*
76 * Task has not woken up but we are exiting the polling
77 * loop anyway. Require a barrier after polling is
78 * cleared to order subsequent test of need_resched().
79 */
80 clear_thread_flag(TIF_POLLING_NRFLAG);
81 smp_mb();
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053082 break;
Nicholas Piggin7ded4292017-06-14 23:02:41 +100083 }
Deepthi Dharwar707827f2011-11-30 02:46:42 +000084 }
85
Deepthi Dharwar707827f2011-11-30 02:46:42 +000086 HMT_medium();
Deepthi Dharwar83dac592012-10-03 18:42:26 +000087 clear_thread_flag(TIF_POLLING_NRFLAG);
Deepthi Dharwar83dac592012-10-03 18:42:26 +000088
Daniel Lezcano1ca80942013-04-03 12:15:22 +000089 idle_loop_epilog(in_purr);
90
Deepthi Dharwar707827f2011-11-30 02:46:42 +000091 return index;
92}
93
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +110094static void check_and_cede_processor(void)
95{
96 /*
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +100097 * Ensure our interrupt state is properly tracked,
98 * also checks if no interrupt has occurred while we
99 * were soft-disabled
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100100 */
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +1000101 if (prep_irq_for_idle()) {
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100102 cede_processor();
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +1000103#ifdef CONFIG_TRACE_IRQFLAGS
104 /* Ensure that H_CEDE returns with IRQs on */
105 if (WARN_ON(!(mfmsr() & MSR_EE)))
106 __hard_irq_enable();
107#endif
108 }
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100109}
110
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000111static int dedicated_cede_loop(struct cpuidle_device *dev,
112 struct cpuidle_driver *drv,
113 int index)
114{
115 unsigned long in_purr;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000116
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000117 idle_loop_prolog(&in_purr);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000118 get_lppaca()->donate_dedicated_cpu = 1;
119
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000120 HMT_medium();
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100121 check_and_cede_processor();
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000122
123 get_lppaca()->donate_dedicated_cpu = 0;
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000124
125 idle_loop_epilog(in_purr);
126
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000127 return index;
128}
129
130static int shared_cede_loop(struct cpuidle_device *dev,
131 struct cpuidle_driver *drv,
132 int index)
133{
134 unsigned long in_purr;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000135
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000136 idle_loop_prolog(&in_purr);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000137
138 /*
139 * Yield the processor to the hypervisor. We return if
140 * an external interrupt occurs (which are driven prior
141 * to returning here) or if a prod occurs from another
142 * processor. When returning here, external interrupts
143 * are enabled.
144 */
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100145 check_and_cede_processor();
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000146
Daniel Lezcano1ca80942013-04-03 12:15:22 +0000147 idle_loop_epilog(in_purr);
148
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000149 return index;
150}
151
152/*
153 * States for dedicated partition case.
154 */
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530155static struct cpuidle_state dedicated_states[] = {
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000156 { /* Snooze */
157 .name = "snooze",
158 .desc = "snooze",
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000159 .exit_latency = 0,
160 .target_residency = 0,
161 .enter = &snooze_loop },
162 { /* CEDE */
163 .name = "CEDE",
164 .desc = "CEDE",
Deepthi Dharwar83dac592012-10-03 18:42:26 +0000165 .exit_latency = 10,
166 .target_residency = 100,
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000167 .enter = &dedicated_cede_loop },
168};
169
170/*
171 * States for shared partition case.
172 */
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530173static struct cpuidle_state shared_states[] = {
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000174 { /* Shared Cede */
175 .name = "Shared Cede",
176 .desc = "Shared Cede",
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000177 .exit_latency = 0,
178 .target_residency = 0,
179 .enter = &shared_cede_loop },
180};
181
Sebastian Andrzej Siewior529351f2016-08-18 14:57:25 +0200182static int pseries_cpuidle_cpu_online(unsigned int cpu)
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000183{
Sebastian Andrzej Siewior529351f2016-08-18 14:57:25 +0200184 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000185
Deepthi Dharwar852d8cb2012-07-03 20:07:22 +0000186 if (dev && cpuidle_get_driver()) {
Sebastian Andrzej Siewior529351f2016-08-18 14:57:25 +0200187 cpuidle_pause_and_lock();
188 cpuidle_enable_device(dev);
189 cpuidle_resume_and_unlock();
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000190 }
Sebastian Andrzej Siewior529351f2016-08-18 14:57:25 +0200191 return 0;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000192}
193
Sebastian Andrzej Siewior529351f2016-08-18 14:57:25 +0200194static int pseries_cpuidle_cpu_dead(unsigned int cpu)
195{
196 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
197
198 if (dev && cpuidle_get_driver()) {
199 cpuidle_pause_and_lock();
200 cpuidle_disable_device(dev);
201 cpuidle_resume_and_unlock();
202 }
203 return 0;
204}
Deepthi Dharwar16aaaff2012-05-20 18:34:27 +0000205
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000206/*
207 * pseries_cpuidle_driver_init()
208 */
209static int pseries_cpuidle_driver_init(void)
210{
211 int idle_state;
212 struct cpuidle_driver *drv = &pseries_idle_driver;
213
214 drv->state_count = 0;
215
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530216 for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
217 /* Is the state not enabled? */
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000218 if (cpuidle_state_table[idle_state].enter == NULL)
219 continue;
220
221 drv->states[drv->state_count] = /* structure copy */
222 cpuidle_state_table[idle_state];
223
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000224 drv->state_count += 1;
225 }
226
227 return 0;
228}
229
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000230/*
231 * pseries_idle_probe()
232 * Choose state table for shared versus dedicated partition
233 */
234static int pseries_idle_probe(void)
235{
236
Deepthi Dharware8bb3e02011-11-30 02:47:03 +0000237 if (cpuidle_disable != IDLE_NO_OVERRIDE)
238 return -ENODEV;
239
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +0530240 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530241 if (lppaca_shared_proc(get_lppaca())) {
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +0530242 cpuidle_state_table = shared_states;
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530243 max_idle_state = ARRAY_SIZE(shared_states);
244 } else {
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +0530245 cpuidle_state_table = dedicated_states;
Deepthi Dharwarbf7f61f2014-01-14 16:26:28 +0530246 max_idle_state = ARRAY_SIZE(dedicated_states);
247 }
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +0530248 } else
249 return -ENODEV;
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000250
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +0530251 if (max_idle_state > 1) {
252 snooze_timeout_en = true;
253 snooze_timeout = cpuidle_state_table[1].target_residency *
254 tb_ticks_per_usec;
255 }
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000256 return 0;
257}
258
259static int __init pseries_processor_idle_init(void)
260{
261 int retval;
262
263 retval = pseries_idle_probe();
264 if (retval)
265 return retval;
266
267 pseries_cpuidle_driver_init();
Deepthi Dharwarb69dbba02014-01-14 16:26:09 +0530268 retval = cpuidle_register(&pseries_idle_driver, NULL);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000269 if (retval) {
270 printk(KERN_DEBUG "Registration of pseries driver failed.\n");
271 return retval;
272 }
273
Sebastian Andrzej Siewior529351f2016-08-18 14:57:25 +0200274 retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
275 "cpuidle/pseries:online",
276 pseries_cpuidle_cpu_online, NULL);
277 WARN_ON(retval < 0);
278 retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD,
279 "cpuidle/pseries:DEAD", NULL,
280 pseries_cpuidle_cpu_dead);
281 WARN_ON(retval < 0);
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000282 printk(KERN_DEBUG "pseries_idle_driver registered\n");
Deepthi Dharwar707827f2011-11-30 02:46:42 +0000283 return 0;
284}
285
Deepthi Dharwar12431c62014-01-14 16:26:18 +0530286device_initcall(pseries_processor_idle_init);