blob: 854a567811002329e9b39068ad8d4834e9b8cf71 [file] [log] [blame]
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +05301/*
2 * cpuidle-powernv - idle state cpuidle driver.
3 * Adapted from drivers/cpuidle/cpuidle-pseries
4 *
5 */
6
7#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/moduleparam.h>
11#include <linux/cpuidle.h>
12#include <linux/cpu.h>
13#include <linux/notifier.h>
Preeti U Murthy0d948732014-02-26 05:39:06 +053014#include <linux/clockchips.h>
Preeti U Murthy08888392014-02-26 05:39:20 +053015#include <linux/of.h>
Preeti U Murthy92c83ff52015-02-18 06:34:17 +010016#include <linux/slab.h>
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053017
18#include <asm/machdep.h>
19#include <asm/firmware.h>
Shreyas B. Prabhu8eb8ac82014-12-10 00:26:51 +053020#include <asm/opal.h>
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050021#include <asm/runlatch.h>
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053022
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +053023#define POWERNV_THRESHOLD_LATENCY_NS 200000
24
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053025struct cpuidle_driver powernv_idle_driver = {
26 .name = "powernv_idle",
27 .owner = THIS_MODULE,
28};
29
30static int max_idle_state;
31static struct cpuidle_state *cpuidle_state_table;
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +053032
33static u64 stop_psscr_table[CPUIDLE_STATE_MAX];
34
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053035static u64 snooze_timeout;
36static bool snooze_timeout_en;
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053037
38static int snooze_loop(struct cpuidle_device *dev,
39 struct cpuidle_driver *drv,
40 int index)
41{
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053042 u64 snooze_exit_time;
43
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053044 local_irq_enable();
45 set_thread_flag(TIF_POLLING_NRFLAG);
46
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053047 snooze_exit_time = get_tb() + snooze_timeout;
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050048 ppc64_runlatch_off();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053049 while (!need_resched()) {
50 HMT_low();
51 HMT_very_low();
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053052 if (snooze_timeout_en && get_tb() > snooze_exit_time)
53 break;
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053054 }
55
56 HMT_medium();
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050057 ppc64_runlatch_on();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053058 clear_thread_flag(TIF_POLLING_NRFLAG);
59 smp_mb();
60 return index;
61}
62
63static int nap_loop(struct cpuidle_device *dev,
64 struct cpuidle_driver *drv,
65 int index)
66{
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050067 ppc64_runlatch_off();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053068 power7_idle();
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050069 ppc64_runlatch_on();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053070 return index;
71}
72
preeticc5a2f72015-06-24 01:48:01 -050073/* Register for fastsleep only in oneshot mode of broadcast */
74#ifdef CONFIG_TICK_ONESHOT
Preeti U Murthy0d948732014-02-26 05:39:06 +053075static int fastsleep_loop(struct cpuidle_device *dev,
76 struct cpuidle_driver *drv,
77 int index)
78{
79 unsigned long old_lpcr = mfspr(SPRN_LPCR);
80 unsigned long new_lpcr;
81
82 if (unlikely(system_state < SYSTEM_RUNNING))
83 return index;
84
85 new_lpcr = old_lpcr;
Michael Neuling9b6a68d2014-06-11 15:59:27 +100086 /* Do not exit powersave upon decrementer as we've setup the timer
87 * offload.
Preeti U Murthy0d948732014-02-26 05:39:06 +053088 */
Michael Neuling9b6a68d2014-06-11 15:59:27 +100089 new_lpcr &= ~LPCR_PECE1;
Preeti U Murthy0d948732014-02-26 05:39:06 +053090
91 mtspr(SPRN_LPCR, new_lpcr);
92 power7_sleep();
93
94 mtspr(SPRN_LPCR, old_lpcr);
95
96 return index;
97}
preeticc5a2f72015-06-24 01:48:01 -050098#endif
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +053099
100static int stop_loop(struct cpuidle_device *dev,
101 struct cpuidle_driver *drv,
102 int index)
103{
104 ppc64_runlatch_off();
105 power9_idle_stop(stop_psscr_table[index]);
106 ppc64_runlatch_on();
107 return index;
108}
109
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530110/*
111 * States for dedicated partition case.
112 */
Shreyas B. Prabhu169f3fa2016-07-08 11:50:50 +0530113static struct cpuidle_state powernv_states[CPUIDLE_STATE_MAX] = {
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530114 { /* Snooze */
115 .name = "snooze",
116 .desc = "snooze",
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530117 .exit_latency = 0,
118 .target_residency = 0,
Shreyas B. Prabhu957efce2016-07-08 11:50:51 +0530119 .enter = snooze_loop },
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530120};
121
Sebastian Andrzej Siewior10fcca92016-08-24 11:12:59 +0200122static int powernv_cpuidle_cpu_online(unsigned int cpu)
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530123{
Sebastian Andrzej Siewior10fcca92016-08-24 11:12:59 +0200124 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530125
126 if (dev && cpuidle_get_driver()) {
Sebastian Andrzej Siewior10fcca92016-08-24 11:12:59 +0200127 cpuidle_pause_and_lock();
128 cpuidle_enable_device(dev);
129 cpuidle_resume_and_unlock();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530130 }
Sebastian Andrzej Siewior10fcca92016-08-24 11:12:59 +0200131 return 0;
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530132}
133
Sebastian Andrzej Siewior10fcca92016-08-24 11:12:59 +0200134static int powernv_cpuidle_cpu_dead(unsigned int cpu)
135{
136 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
137
138 if (dev && cpuidle_get_driver()) {
139 cpuidle_pause_and_lock();
140 cpuidle_disable_device(dev);
141 cpuidle_resume_and_unlock();
142 }
143 return 0;
144}
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530145
146/*
147 * powernv_cpuidle_driver_init()
148 */
149static int powernv_cpuidle_driver_init(void)
150{
151 int idle_state;
152 struct cpuidle_driver *drv = &powernv_idle_driver;
153
154 drv->state_count = 0;
155
156 for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
157 /* Is the state not enabled? */
158 if (cpuidle_state_table[idle_state].enter == NULL)
159 continue;
160
161 drv->states[drv->state_count] = /* structure copy */
162 cpuidle_state_table[idle_state];
163
164 drv->state_count += 1;
165 }
166
Vaidyanathan Srinivasan9e177132017-03-23 20:52:46 +0530167 /*
168 * On the PowerNV platform cpu_present may be less than cpu_possible in
169 * cases when firmware detects the CPU, but it is not available to the
170 * OS. If CONFIG_HOTPLUG_CPU=n, then such CPUs are not hotplugable at
171 * run time and hence cpu_devices are not created for those CPUs by the
172 * generic topology_init().
173 *
174 * drv->cpumask defaults to cpu_possible_mask in
175 * __cpuidle_driver_init(). This breaks cpuidle on PowerNV where
176 * cpu_devices are not created for CPUs in cpu_possible_mask that
177 * cannot be hot-added later at run time.
178 *
179 * Trying cpuidle_register_device() on a CPU without a cpu_device is
180 * incorrect, so pass a correct CPU mask to the generic cpuidle driver.
181 */
182
183 drv->cpumask = (struct cpumask *)cpu_present_mask;
184
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530185 return 0;
186}
187
Preeti U Murthy08888392014-02-26 05:39:20 +0530188static int powernv_add_idle_states(void)
189{
190 struct device_node *power_mgt;
Preeti U Murthy08888392014-02-26 05:39:20 +0530191 int nr_idle_states = 1; /* Snooze */
192 int dt_idle_states;
Shreyas B. Prabhu957efce2016-07-08 11:50:51 +0530193 u32 latency_ns[CPUIDLE_STATE_MAX];
194 u32 residency_ns[CPUIDLE_STATE_MAX];
195 u32 flags[CPUIDLE_STATE_MAX];
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530196 u64 psscr_val[CPUIDLE_STATE_MAX];
197 const char *names[CPUIDLE_STATE_MAX];
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100198 int i, rc;
Preeti U Murthy08888392014-02-26 05:39:20 +0530199
200 /* Currently we have snooze statically defined */
201
202 power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
203 if (!power_mgt) {
204 pr_warn("opal: PowerMgmt Node not found\n");
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100205 goto out;
Preeti U Murthy08888392014-02-26 05:39:20 +0530206 }
207
Preeti U Murthy70734a72015-02-18 23:24:53 -0600208 /* Read values of any property to determine the num of idle states */
209 dt_idle_states = of_property_count_u32_elems(power_mgt, "ibm,cpu-idle-state-flags");
210 if (dt_idle_states < 0) {
211 pr_warn("cpuidle-powernv: no idle states found in the DT\n");
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100212 goto out;
Preeti U. Murthy74aa51b2014-10-14 13:23:00 +0530213 }
214
Shreyas B. Prabhu957efce2016-07-08 11:50:51 +0530215 /*
216 * Since snooze is used as first idle state, max idle states allowed is
217 * CPUIDLE_STATE_MAX -1
218 */
219 if (dt_idle_states > CPUIDLE_STATE_MAX - 1) {
220 pr_warn("cpuidle-powernv: discovered idle states more than allowed");
221 dt_idle_states = CPUIDLE_STATE_MAX - 1;
222 }
223
Preeti U Murthy70734a72015-02-18 23:24:53 -0600224 if (of_property_read_u32_array(power_mgt,
225 "ibm,cpu-idle-state-flags", flags, dt_idle_states)) {
226 pr_warn("cpuidle-powernv : missing ibm,cpu-idle-state-flags in DT\n");
Shreyas B. Prabhu957efce2016-07-08 11:50:51 +0530227 goto out;
Preeti U Murthy70734a72015-02-18 23:24:53 -0600228 }
Preeti U Murthy08888392014-02-26 05:39:20 +0530229
Shreyas B. Prabhu957efce2016-07-08 11:50:51 +0530230 if (of_property_read_u32_array(power_mgt,
231 "ibm,cpu-idle-state-latencies-ns", latency_ns,
232 dt_idle_states)) {
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100233 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-latencies-ns in DT\n");
Shreyas B. Prabhu957efce2016-07-08 11:50:51 +0530234 goto out;
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100235 }
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530236 if (of_property_read_string_array(power_mgt,
237 "ibm,cpu-idle-state-names", names, dt_idle_states) < 0) {
238 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-names in DT\n");
239 goto out;
240 }
241
242 /*
243 * If the idle states use stop instruction, probe for psscr values
244 * which are necessary to specify required stop level.
245 */
246 if (flags[0] & (OPAL_PM_STOP_INST_FAST | OPAL_PM_STOP_INST_DEEP))
247 if (of_property_read_u64_array(power_mgt,
248 "ibm,cpu-idle-state-psscr", psscr_val, dt_idle_states)) {
249 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-states-psscr in DT\n");
250 goto out;
251 }
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100252
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100253 rc = of_property_read_u32_array(power_mgt,
254 "ibm,cpu-idle-state-residency-ns", residency_ns, dt_idle_states);
255
Preeti U Murthy08888392014-02-26 05:39:20 +0530256 for (i = 0; i < dt_idle_states; i++) {
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530257 /*
258 * If an idle state has exit latency beyond
259 * POWERNV_THRESHOLD_LATENCY_NS then don't use it
260 * in cpu-idle.
261 */
262 if (latency_ns[i] > POWERNV_THRESHOLD_LATENCY_NS)
263 continue;
Preeti U Murthy08888392014-02-26 05:39:20 +0530264
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100265 /*
266 * Cpuidle accepts exit_latency and target_residency in us.
267 * Use default target_residency values if f/w does not expose it.
Preeti U. Murthy74aa51b2014-10-14 13:23:00 +0530268 */
Preeti U Murthy70734a72015-02-18 23:24:53 -0600269 if (flags[i] & OPAL_PM_NAP_ENABLED) {
Preeti U Murthy08888392014-02-26 05:39:20 +0530270 /* Add NAP state */
271 strcpy(powernv_states[nr_idle_states].name, "Nap");
272 strcpy(powernv_states[nr_idle_states].desc, "Nap");
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100273 powernv_states[nr_idle_states].flags = 0;
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100274 powernv_states[nr_idle_states].target_residency = 100;
Shreyas B. Prabhu957efce2016-07-08 11:50:51 +0530275 powernv_states[nr_idle_states].enter = nap_loop;
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530276 } else if ((flags[i] & OPAL_PM_STOP_INST_FAST) &&
277 !(flags[i] & OPAL_PM_TIMEBASE_STOP)) {
278 strncpy(powernv_states[nr_idle_states].name,
279 names[i], CPUIDLE_NAME_LEN);
280 strncpy(powernv_states[nr_idle_states].desc,
281 names[i], CPUIDLE_NAME_LEN);
282 powernv_states[nr_idle_states].flags = 0;
283
284 powernv_states[nr_idle_states].enter = stop_loop;
285 stop_psscr_table[nr_idle_states] = psscr_val[i];
preeticc5a2f72015-06-24 01:48:01 -0500286 }
287
288 /*
289 * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come
290 * within this config dependency check.
291 */
292#ifdef CONFIG_TICK_ONESHOT
293 if (flags[i] & OPAL_PM_SLEEP_ENABLED ||
Preeti U Murthy70734a72015-02-18 23:24:53 -0600294 flags[i] & OPAL_PM_SLEEP_ENABLED_ER1) {
Preeti U Murthy08888392014-02-26 05:39:20 +0530295 /* Add FASTSLEEP state */
296 strcpy(powernv_states[nr_idle_states].name, "FastSleep");
297 strcpy(powernv_states[nr_idle_states].desc, "FastSleep");
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100298 powernv_states[nr_idle_states].flags = CPUIDLE_FLAG_TIMER_STOP;
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100299 powernv_states[nr_idle_states].target_residency = 300000;
Shreyas B. Prabhu957efce2016-07-08 11:50:51 +0530300 powernv_states[nr_idle_states].enter = fastsleep_loop;
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530301 } else if ((flags[i] & OPAL_PM_STOP_INST_DEEP) &&
302 (flags[i] & OPAL_PM_TIMEBASE_STOP)) {
303 strncpy(powernv_states[nr_idle_states].name,
304 names[i], CPUIDLE_NAME_LEN);
305 strncpy(powernv_states[nr_idle_states].desc,
306 names[i], CPUIDLE_NAME_LEN);
307
308 powernv_states[nr_idle_states].flags = CPUIDLE_FLAG_TIMER_STOP;
309 powernv_states[nr_idle_states].enter = stop_loop;
310 stop_psscr_table[nr_idle_states] = psscr_val[i];
Preeti U Murthy08888392014-02-26 05:39:20 +0530311 }
preeticc5a2f72015-06-24 01:48:01 -0500312#endif
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100313 powernv_states[nr_idle_states].exit_latency =
314 ((unsigned int)latency_ns[i]) / 1000;
315
316 if (!rc) {
317 powernv_states[nr_idle_states].target_residency =
318 ((unsigned int)residency_ns[i]) / 1000;
319 }
320
321 nr_idle_states++;
Preeti U Murthy08888392014-02-26 05:39:20 +0530322 }
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100323out:
Preeti U Murthy08888392014-02-26 05:39:20 +0530324 return nr_idle_states;
325}
326
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530327/*
328 * powernv_idle_probe()
329 * Choose state table for shared versus dedicated partition
330 */
331static int powernv_idle_probe(void)
332{
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530333 if (cpuidle_disable != IDLE_NO_OVERRIDE)
334 return -ENODEV;
335
Stewart Smithe4d54f72015-12-09 17:18:20 +1100336 if (firmware_has_feature(FW_FEATURE_OPAL)) {
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530337 cpuidle_state_table = powernv_states;
Preeti U Murthy08888392014-02-26 05:39:20 +0530338 /* Device tree can indicate more idle states */
339 max_idle_state = powernv_add_idle_states();
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +0530340 if (max_idle_state > 1) {
341 snooze_timeout_en = true;
342 snooze_timeout = powernv_states[1].target_residency *
343 tb_ticks_per_usec;
344 }
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530345 } else
346 return -ENODEV;
347
348 return 0;
349}
350
351static int __init powernv_processor_idle_init(void)
352{
353 int retval;
354
355 retval = powernv_idle_probe();
356 if (retval)
357 return retval;
358
359 powernv_cpuidle_driver_init();
360 retval = cpuidle_register(&powernv_idle_driver, NULL);
361 if (retval) {
362 printk(KERN_DEBUG "Registration of powernv driver failed.\n");
363 return retval;
364 }
365
Sebastian Andrzej Siewior10fcca92016-08-24 11:12:59 +0200366 retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
367 "cpuidle/powernv:online",
368 powernv_cpuidle_cpu_online, NULL);
369 WARN_ON(retval < 0);
370 retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD,
371 "cpuidle/powernv:dead", NULL,
372 powernv_cpuidle_cpu_dead);
373 WARN_ON(retval < 0);
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530374 printk(KERN_DEBUG "powernv_idle_driver registered\n");
375 return 0;
376}
377
378device_initcall(powernv_processor_idle_init);