]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/cpuidle/cpuidle-pseries.c
License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[mirror_ubuntu-bionic-kernel.git] / drivers / cpuidle / cpuidle-pseries.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
707827f3 2/*
962e7bd4 3 * cpuidle-pseries - idle state cpuidle driver.
707827f3
DD
4 * 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>
16aaaff6 15#include <linux/notifier.h>
707827f3
DD
16
17#include <asm/paca.h>
18#include <asm/reg.h>
707827f3
DD
19#include <asm/machdep.h>
20#include <asm/firmware.h>
3f67d962 21#include <asm/runlatch.h>
212bebb4 22#include <asm/plpar_wrappers.h>
707827f3
DD
23
24struct cpuidle_driver pseries_idle_driver = {
1ca80944
DL
25 .name = "pseries_idle",
26 .owner = THIS_MODULE,
707827f3
DD
27};
28
624e46d0
NP
29static 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;
707827f3 33
1ca80944 34static inline void idle_loop_prolog(unsigned long *in_purr)
707827f3 35{
d8c6ad31 36 ppc64_runlatch_off();
707827f3
DD
37 *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
1ca80944 45static inline void idle_loop_epilog(unsigned long in_purr)
707827f3 46{
7ffcf8ec
AB
47 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);
707827f3 52 get_lppaca()->idle = 0;
d8c6ad31
NP
53
54 if (irqs_disabled())
55 local_irq_enable();
56 ppc64_runlatch_on();
707827f3
DD
57}
58
59static int snooze_loop(struct cpuidle_device *dev,
60 struct cpuidle_driver *drv,
61 int index)
62{
63 unsigned long in_purr;
78eaa10f 64 u64 snooze_exit_time;
707827f3 65
3fc5ee92
NP
66 set_thread_flag(TIF_POLLING_NRFLAG);
67
1ca80944 68 idle_loop_prolog(&in_purr);
83dac594 69 local_irq_enable();
78eaa10f 70 snooze_exit_time = get_tb() + snooze_timeout;
707827f3 71
b69dbba0 72 while (!need_resched()) {
83dac594
DD
73 HMT_low();
74 HMT_very_low();
7ded4291
NP
75 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();
78eaa10f 83 break;
7ded4291 84 }
707827f3
DD
85 }
86
707827f3 87 HMT_medium();
83dac594 88 clear_thread_flag(TIF_POLLING_NRFLAG);
83dac594 89
1ca80944
DL
90 idle_loop_epilog(in_purr);
91
707827f3
DD
92 return index;
93}
94
7230c564
BH
95static void check_and_cede_processor(void)
96{
97 /*
be2cf20a
BH
98 * Ensure our interrupt state is properly tracked,
99 * also checks if no interrupt has occurred while we
100 * were soft-disabled
7230c564 101 */
be2cf20a 102 if (prep_irq_for_idle()) {
7230c564 103 cede_processor();
be2cf20a
BH
104#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 }
7230c564
BH
110}
111
707827f3
DD
112static int dedicated_cede_loop(struct cpuidle_device *dev,
113 struct cpuidle_driver *drv,
114 int index)
115{
116 unsigned long in_purr;
707827f3 117
1ca80944 118 idle_loop_prolog(&in_purr);
707827f3
DD
119 get_lppaca()->donate_dedicated_cpu = 1;
120
707827f3 121 HMT_medium();
7230c564 122 check_and_cede_processor();
707827f3
DD
123
124 get_lppaca()->donate_dedicated_cpu = 0;
1ca80944
DL
125
126 idle_loop_epilog(in_purr);
127
707827f3
DD
128 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;
707827f3 136
1ca80944 137 idle_loop_prolog(&in_purr);
707827f3
DD
138
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 */
7230c564 146 check_and_cede_processor();
707827f3 147
1ca80944
DL
148 idle_loop_epilog(in_purr);
149
707827f3
DD
150 return index;
151}
152
153/*
154 * States for dedicated partition case.
155 */
bf7f61f2 156static struct cpuidle_state dedicated_states[] = {
707827f3
DD
157 { /* Snooze */
158 .name = "snooze",
159 .desc = "snooze",
707827f3
DD
160 .exit_latency = 0,
161 .target_residency = 0,
162 .enter = &snooze_loop },
163 { /* CEDE */
164 .name = "CEDE",
165 .desc = "CEDE",
83dac594
DD
166 .exit_latency = 10,
167 .target_residency = 100,
707827f3
DD
168 .enter = &dedicated_cede_loop },
169};
170
171/*
172 * States for shared partition case.
173 */
bf7f61f2 174static struct cpuidle_state shared_states[] = {
707827f3
DD
175 { /* Shared Cede */
176 .name = "Shared Cede",
177 .desc = "Shared Cede",
707827f3
DD
178 .exit_latency = 0,
179 .target_residency = 0,
180 .enter = &shared_cede_loop },
181};
182
529351fd 183static int pseries_cpuidle_cpu_online(unsigned int cpu)
707827f3 184{
529351fd 185 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
16aaaff6 186
852d8cb1 187 if (dev && cpuidle_get_driver()) {
529351fd
SAS
188 cpuidle_pause_and_lock();
189 cpuidle_enable_device(dev);
190 cpuidle_resume_and_unlock();
191 }
192 return 0;
193}
852d8cb1 194
529351fd
SAS
195static int pseries_cpuidle_cpu_dead(unsigned int cpu)
196{
197 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
852d8cb1 198
529351fd
SAS
199 if (dev && cpuidle_get_driver()) {
200 cpuidle_pause_and_lock();
201 cpuidle_disable_device(dev);
202 cpuidle_resume_and_unlock();
707827f3 203 }
529351fd 204 return 0;
707827f3
DD
205}
206
207/*
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
bf7f61f2
DD
217 for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
218 /* Is the state not enabled? */
707827f3
DD
219 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
707827f3
DD
225 drv->state_count += 1;
226 }
227
228 return 0;
229}
230
707827f3
DD
231/*
232 * pseries_idle_probe()
233 * Choose state table for shared versus dedicated partition
234 */
235static int pseries_idle_probe(void)
236{
237
e8bb3e00
DD
238 if (cpuidle_disable != IDLE_NO_OVERRIDE)
239 return -ENODEV;
240
b69dbba0 241 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
bf7f61f2 242 if (lppaca_shared_proc(get_lppaca())) {
b69dbba0 243 cpuidle_state_table = shared_states;
bf7f61f2
DD
244 max_idle_state = ARRAY_SIZE(shared_states);
245 } else {
b69dbba0 246 cpuidle_state_table = dedicated_states;
bf7f61f2
DD
247 max_idle_state = ARRAY_SIZE(dedicated_states);
248 }
b69dbba0
DD
249 } else
250 return -ENODEV;
707827f3 251
78eaa10f
SB
252 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 }
707827f3
DD
257 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();
b69dbba0 269 retval = cpuidle_register(&pseries_idle_driver, NULL);
707827f3
DD
270 if (retval) {
271 printk(KERN_DEBUG "Registration of pseries driver failed.\n");
272 return retval;
273 }
274
529351fd
SAS
275 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);
707827f3 283 printk(KERN_DEBUG "pseries_idle_driver registered\n");
707827f3
DD
284 return 0;
285}
286
12431c64 287device_initcall(pseries_processor_idle_init);