Merge branch 'work.regset' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux/fpc-iii.git] / drivers / cpuidle / cpuidle-powernv.c
blob1b299e801f749a57daadb27fba502468f225c2a4
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * cpuidle-powernv - idle state cpuidle driver.
4 * Adapted from drivers/cpuidle/cpuidle-pseries
6 */
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>
14 #include <linux/notifier.h>
15 #include <linux/clockchips.h>
16 #include <linux/of.h>
17 #include <linux/slab.h>
19 #include <asm/machdep.h>
20 #include <asm/firmware.h>
21 #include <asm/opal.h>
22 #include <asm/runlatch.h>
23 #include <asm/cpuidle.h>
26 * Expose only those Hardware idle states via the cpuidle framework
27 * that have latency value below POWERNV_THRESHOLD_LATENCY_NS.
29 #define POWERNV_THRESHOLD_LATENCY_NS 200000
31 static struct cpuidle_driver powernv_idle_driver = {
32 .name = "powernv_idle",
33 .owner = THIS_MODULE,
36 static int max_idle_state __read_mostly;
37 static struct cpuidle_state *cpuidle_state_table __read_mostly;
39 struct stop_psscr_table {
40 u64 val;
41 u64 mask;
44 static struct stop_psscr_table stop_psscr_table[CPUIDLE_STATE_MAX] __read_mostly;
46 static u64 default_snooze_timeout __read_mostly;
47 static bool snooze_timeout_en __read_mostly;
49 static u64 get_snooze_timeout(struct cpuidle_device *dev,
50 struct cpuidle_driver *drv,
51 int index)
53 int i;
55 if (unlikely(!snooze_timeout_en))
56 return default_snooze_timeout;
58 for (i = index + 1; i < drv->state_count; i++) {
59 if (dev->states_usage[i].disable)
60 continue;
62 return drv->states[i].target_residency * tb_ticks_per_usec;
65 return default_snooze_timeout;
68 static int snooze_loop(struct cpuidle_device *dev,
69 struct cpuidle_driver *drv,
70 int index)
72 u64 snooze_exit_time;
74 set_thread_flag(TIF_POLLING_NRFLAG);
76 local_irq_enable();
78 snooze_exit_time = get_tb() + get_snooze_timeout(dev, drv, index);
79 ppc64_runlatch_off();
80 HMT_very_low();
81 while (!need_resched()) {
82 if (likely(snooze_timeout_en) && get_tb() > snooze_exit_time) {
84 * Task has not woken up but we are exiting the polling
85 * loop anyway. Require a barrier after polling is
86 * cleared to order subsequent test of need_resched().
88 clear_thread_flag(TIF_POLLING_NRFLAG);
89 smp_mb();
90 break;
94 HMT_medium();
95 ppc64_runlatch_on();
96 clear_thread_flag(TIF_POLLING_NRFLAG);
98 local_irq_disable();
100 return index;
103 static int nap_loop(struct cpuidle_device *dev,
104 struct cpuidle_driver *drv,
105 int index)
107 power7_idle_type(PNV_THREAD_NAP);
109 return index;
112 /* Register for fastsleep only in oneshot mode of broadcast */
113 #ifdef CONFIG_TICK_ONESHOT
114 static int fastsleep_loop(struct cpuidle_device *dev,
115 struct cpuidle_driver *drv,
116 int index)
118 unsigned long old_lpcr = mfspr(SPRN_LPCR);
119 unsigned long new_lpcr;
121 if (unlikely(system_state < SYSTEM_RUNNING))
122 return index;
124 new_lpcr = old_lpcr;
125 /* Do not exit powersave upon decrementer as we've setup the timer
126 * offload.
128 new_lpcr &= ~LPCR_PECE1;
130 mtspr(SPRN_LPCR, new_lpcr);
132 power7_idle_type(PNV_THREAD_SLEEP);
134 mtspr(SPRN_LPCR, old_lpcr);
136 return index;
138 #endif
140 static int stop_loop(struct cpuidle_device *dev,
141 struct cpuidle_driver *drv,
142 int index)
144 power9_idle_type(stop_psscr_table[index].val,
145 stop_psscr_table[index].mask);
146 return index;
150 * States for dedicated partition case.
152 static struct cpuidle_state powernv_states[CPUIDLE_STATE_MAX] = {
153 { /* Snooze */
154 .name = "snooze",
155 .desc = "snooze",
156 .exit_latency = 0,
157 .target_residency = 0,
158 .enter = snooze_loop },
161 static int powernv_cpuidle_cpu_online(unsigned int cpu)
163 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
165 if (dev && cpuidle_get_driver()) {
166 cpuidle_pause_and_lock();
167 cpuidle_enable_device(dev);
168 cpuidle_resume_and_unlock();
170 return 0;
173 static int powernv_cpuidle_cpu_dead(unsigned int cpu)
175 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
177 if (dev && cpuidle_get_driver()) {
178 cpuidle_pause_and_lock();
179 cpuidle_disable_device(dev);
180 cpuidle_resume_and_unlock();
182 return 0;
186 * powernv_cpuidle_driver_init()
188 static int powernv_cpuidle_driver_init(void)
190 int idle_state;
191 struct cpuidle_driver *drv = &powernv_idle_driver;
193 drv->state_count = 0;
195 for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
196 /* Is the state not enabled? */
197 if (cpuidle_state_table[idle_state].enter == NULL)
198 continue;
200 drv->states[drv->state_count] = /* structure copy */
201 cpuidle_state_table[idle_state];
203 drv->state_count += 1;
207 * On the PowerNV platform cpu_present may be less than cpu_possible in
208 * cases when firmware detects the CPU, but it is not available to the
209 * OS. If CONFIG_HOTPLUG_CPU=n, then such CPUs are not hotplugable at
210 * run time and hence cpu_devices are not created for those CPUs by the
211 * generic topology_init().
213 * drv->cpumask defaults to cpu_possible_mask in
214 * __cpuidle_driver_init(). This breaks cpuidle on PowerNV where
215 * cpu_devices are not created for CPUs in cpu_possible_mask that
216 * cannot be hot-added later at run time.
218 * Trying cpuidle_register_device() on a CPU without a cpu_device is
219 * incorrect, so pass a correct CPU mask to the generic cpuidle driver.
222 drv->cpumask = (struct cpumask *)cpu_present_mask;
224 return 0;
227 static inline void add_powernv_state(int index, const char *name,
228 unsigned int flags,
229 int (*idle_fn)(struct cpuidle_device *,
230 struct cpuidle_driver *,
231 int),
232 unsigned int target_residency,
233 unsigned int exit_latency,
234 u64 psscr_val, u64 psscr_mask)
236 strlcpy(powernv_states[index].name, name, CPUIDLE_NAME_LEN);
237 strlcpy(powernv_states[index].desc, name, CPUIDLE_NAME_LEN);
238 powernv_states[index].flags = flags;
239 powernv_states[index].target_residency = target_residency;
240 powernv_states[index].exit_latency = exit_latency;
241 powernv_states[index].enter = idle_fn;
242 /* For power8 and below psscr_* will be 0 */
243 stop_psscr_table[index].val = psscr_val;
244 stop_psscr_table[index].mask = psscr_mask;
248 * Returns 0 if prop1_len == prop2_len. Else returns -1
250 static inline int validate_dt_prop_sizes(const char *prop1, int prop1_len,
251 const char *prop2, int prop2_len)
253 if (prop1_len == prop2_len)
254 return 0;
256 pr_warn("cpuidle-powernv: array sizes don't match for %s and %s\n",
257 prop1, prop2);
258 return -1;
261 extern u32 pnv_get_supported_cpuidle_states(void);
262 static int powernv_add_idle_states(void)
264 int nr_idle_states = 1; /* Snooze */
265 int dt_idle_states;
266 u32 has_stop_states = 0;
267 int i;
268 u32 supported_flags = pnv_get_supported_cpuidle_states();
271 /* Currently we have snooze statically defined */
272 if (nr_pnv_idle_states <= 0) {
273 pr_warn("cpuidle-powernv : Only Snooze is available\n");
274 goto out;
277 /* TODO: Count only states which are eligible for cpuidle */
278 dt_idle_states = nr_pnv_idle_states;
281 * Since snooze is used as first idle state, max idle states allowed is
282 * CPUIDLE_STATE_MAX -1
284 if (nr_pnv_idle_states > CPUIDLE_STATE_MAX - 1) {
285 pr_warn("cpuidle-powernv: discovered idle states more than allowed");
286 dt_idle_states = CPUIDLE_STATE_MAX - 1;
290 * If the idle states use stop instruction, probe for psscr values
291 * and psscr mask which are necessary to specify required stop level.
293 has_stop_states = (pnv_idle_states[0].flags &
294 (OPAL_PM_STOP_INST_FAST | OPAL_PM_STOP_INST_DEEP));
296 for (i = 0; i < dt_idle_states; i++) {
297 unsigned int exit_latency, target_residency;
298 bool stops_timebase = false;
299 struct pnv_idle_states_t *state = &pnv_idle_states[i];
302 * Skip the platform idle state whose flag isn't in
303 * the supported_cpuidle_states flag mask.
305 if ((state->flags & supported_flags) != state->flags)
306 continue;
308 * If an idle state has exit latency beyond
309 * POWERNV_THRESHOLD_LATENCY_NS then don't use it
310 * in cpu-idle.
312 if (state->latency_ns > POWERNV_THRESHOLD_LATENCY_NS)
313 continue;
315 * Firmware passes residency and latency values in ns.
316 * cpuidle expects it in us.
318 exit_latency = DIV_ROUND_UP(state->latency_ns, 1000);
319 target_residency = DIV_ROUND_UP(state->residency_ns, 1000);
321 if (has_stop_states && !(state->valid))
322 continue;
324 if (state->flags & OPAL_PM_TIMEBASE_STOP)
325 stops_timebase = true;
327 if (state->flags & OPAL_PM_NAP_ENABLED) {
328 /* Add NAP state */
329 add_powernv_state(nr_idle_states, "Nap",
330 CPUIDLE_FLAG_NONE, nap_loop,
331 target_residency, exit_latency, 0, 0);
332 } else if (has_stop_states && !stops_timebase) {
333 add_powernv_state(nr_idle_states, state->name,
334 CPUIDLE_FLAG_NONE, stop_loop,
335 target_residency, exit_latency,
336 state->psscr_val,
337 state->psscr_mask);
341 * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come
342 * within this config dependency check.
344 #ifdef CONFIG_TICK_ONESHOT
345 else if (state->flags & OPAL_PM_SLEEP_ENABLED ||
346 state->flags & OPAL_PM_SLEEP_ENABLED_ER1) {
347 /* Add FASTSLEEP state */
348 add_powernv_state(nr_idle_states, "FastSleep",
349 CPUIDLE_FLAG_TIMER_STOP,
350 fastsleep_loop,
351 target_residency, exit_latency, 0, 0);
352 } else if (has_stop_states && stops_timebase) {
353 add_powernv_state(nr_idle_states, state->name,
354 CPUIDLE_FLAG_TIMER_STOP, stop_loop,
355 target_residency, exit_latency,
356 state->psscr_val,
357 state->psscr_mask);
359 #endif
360 else
361 continue;
362 nr_idle_states++;
364 out:
365 return nr_idle_states;
369 * powernv_idle_probe()
370 * Choose state table for shared versus dedicated partition
372 static int powernv_idle_probe(void)
374 if (cpuidle_disable != IDLE_NO_OVERRIDE)
375 return -ENODEV;
377 if (firmware_has_feature(FW_FEATURE_OPAL)) {
378 cpuidle_state_table = powernv_states;
379 /* Device tree can indicate more idle states */
380 max_idle_state = powernv_add_idle_states();
381 default_snooze_timeout = TICK_USEC * tb_ticks_per_usec;
382 if (max_idle_state > 1)
383 snooze_timeout_en = true;
384 } else
385 return -ENODEV;
387 return 0;
390 static int __init powernv_processor_idle_init(void)
392 int retval;
394 retval = powernv_idle_probe();
395 if (retval)
396 return retval;
398 powernv_cpuidle_driver_init();
399 retval = cpuidle_register(&powernv_idle_driver, NULL);
400 if (retval) {
401 printk(KERN_DEBUG "Registration of powernv driver failed.\n");
402 return retval;
405 retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
406 "cpuidle/powernv:online",
407 powernv_cpuidle_cpu_online, NULL);
408 WARN_ON(retval < 0);
409 retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD,
410 "cpuidle/powernv:dead", NULL,
411 powernv_cpuidle_cpu_dead);
412 WARN_ON(retval < 0);
413 printk(KERN_DEBUG "powernv_idle_driver registered\n");
414 return 0;
417 device_initcall(powernv_processor_idle_init);