blob: 370593006f5f76db29a0433912966cb49820346d [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>
Gautham R. Shenoy09206b62017-01-25 14:06:28 +053022#include <asm/cpuidle.h>
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053023
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +053024/*
25 * Expose only those Hardware idle states via the cpuidle framework
26 * that have latency value below POWERNV_THRESHOLD_LATENCY_NS.
27 */
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +053028#define POWERNV_THRESHOLD_LATENCY_NS 200000
29
Andrew Donnellaned613902016-11-22 16:08:06 +110030static struct cpuidle_driver powernv_idle_driver = {
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053031 .name = "powernv_idle",
32 .owner = THIS_MODULE,
33};
34
35static int max_idle_state;
36static struct cpuidle_state *cpuidle_state_table;
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +053037
Gautham R. Shenoy09206b62017-01-25 14:06:28 +053038struct stop_psscr_table {
39 u64 val;
40 u64 mask;
41};
42
43static struct stop_psscr_table stop_psscr_table[CPUIDLE_STATE_MAX];
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +053044
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053045static u64 snooze_timeout;
46static bool snooze_timeout_en;
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053047
48static int snooze_loop(struct cpuidle_device *dev,
49 struct cpuidle_driver *drv,
50 int index)
51{
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053052 u64 snooze_exit_time;
53
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053054 local_irq_enable();
55 set_thread_flag(TIF_POLLING_NRFLAG);
56
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053057 snooze_exit_time = get_tb() + snooze_timeout;
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050058 ppc64_runlatch_off();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053059 while (!need_resched()) {
60 HMT_low();
61 HMT_very_low();
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053062 if (snooze_timeout_en && get_tb() > snooze_exit_time)
63 break;
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053064 }
65
66 HMT_medium();
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050067 ppc64_runlatch_on();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053068 clear_thread_flag(TIF_POLLING_NRFLAG);
69 smp_mb();
70 return index;
71}
72
73static int nap_loop(struct cpuidle_device *dev,
74 struct cpuidle_driver *drv,
75 int index)
76{
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050077 ppc64_runlatch_off();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053078 power7_idle();
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050079 ppc64_runlatch_on();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053080 return index;
81}
82
preeticc5a2f72015-06-24 01:48:01 -050083/* Register for fastsleep only in oneshot mode of broadcast */
84#ifdef CONFIG_TICK_ONESHOT
Preeti U Murthy0d948732014-02-26 05:39:06 +053085static int fastsleep_loop(struct cpuidle_device *dev,
86 struct cpuidle_driver *drv,
87 int index)
88{
89 unsigned long old_lpcr = mfspr(SPRN_LPCR);
90 unsigned long new_lpcr;
91
92 if (unlikely(system_state < SYSTEM_RUNNING))
93 return index;
94
95 new_lpcr = old_lpcr;
Michael Neuling9b6a68d2014-06-11 15:59:27 +100096 /* Do not exit powersave upon decrementer as we've setup the timer
97 * offload.
Preeti U Murthy0d948732014-02-26 05:39:06 +053098 */
Michael Neuling9b6a68d2014-06-11 15:59:27 +100099 new_lpcr &= ~LPCR_PECE1;
Preeti U Murthy0d948732014-02-26 05:39:06 +0530100
101 mtspr(SPRN_LPCR, new_lpcr);
102 power7_sleep();
103
104 mtspr(SPRN_LPCR, old_lpcr);
105
106 return index;
107}
preeticc5a2f72015-06-24 01:48:01 -0500108#endif
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530109
110static int stop_loop(struct cpuidle_device *dev,
111 struct cpuidle_driver *drv,
112 int index)
113{
114 ppc64_runlatch_off();
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530115 power9_idle_stop(stop_psscr_table[index].val,
116 stop_psscr_table[index].mask);
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530117 ppc64_runlatch_on();
118 return index;
119}
120
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530121/*
122 * States for dedicated partition case.
123 */
Shreyas B. Prabhu169f3fa2016-07-08 11:50:50 +0530124static struct cpuidle_state powernv_states[CPUIDLE_STATE_MAX] = {
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530125 { /* Snooze */
126 .name = "snooze",
127 .desc = "snooze",
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530128 .exit_latency = 0,
129 .target_residency = 0,
Shreyas B. Prabhu957efce2016-07-08 11:50:51 +0530130 .enter = snooze_loop },
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530131};
132
Sebastian Andrzej Siewior10fcca92016-08-24 11:12:59 +0200133static int powernv_cpuidle_cpu_online(unsigned int cpu)
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530134{
Sebastian Andrzej Siewior10fcca92016-08-24 11:12:59 +0200135 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530136
137 if (dev && cpuidle_get_driver()) {
Sebastian Andrzej Siewior10fcca92016-08-24 11:12:59 +0200138 cpuidle_pause_and_lock();
139 cpuidle_enable_device(dev);
140 cpuidle_resume_and_unlock();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530141 }
Sebastian Andrzej Siewior10fcca92016-08-24 11:12:59 +0200142 return 0;
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530143}
144
Sebastian Andrzej Siewior10fcca92016-08-24 11:12:59 +0200145static int powernv_cpuidle_cpu_dead(unsigned int cpu)
146{
147 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
148
149 if (dev && cpuidle_get_driver()) {
150 cpuidle_pause_and_lock();
151 cpuidle_disable_device(dev);
152 cpuidle_resume_and_unlock();
153 }
154 return 0;
155}
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530156
157/*
158 * powernv_cpuidle_driver_init()
159 */
160static int powernv_cpuidle_driver_init(void)
161{
162 int idle_state;
163 struct cpuidle_driver *drv = &powernv_idle_driver;
164
165 drv->state_count = 0;
166
167 for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
168 /* Is the state not enabled? */
169 if (cpuidle_state_table[idle_state].enter == NULL)
170 continue;
171
172 drv->states[drv->state_count] = /* structure copy */
173 cpuidle_state_table[idle_state];
174
175 drv->state_count += 1;
176 }
177
178 return 0;
179}
180
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530181static inline void add_powernv_state(int index, const char *name,
182 unsigned int flags,
183 int (*idle_fn)(struct cpuidle_device *,
184 struct cpuidle_driver *,
185 int),
186 unsigned int target_residency,
187 unsigned int exit_latency,
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530188 u64 psscr_val, u64 psscr_mask)
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530189{
190 strlcpy(powernv_states[index].name, name, CPUIDLE_NAME_LEN);
191 strlcpy(powernv_states[index].desc, name, CPUIDLE_NAME_LEN);
192 powernv_states[index].flags = flags;
193 powernv_states[index].target_residency = target_residency;
194 powernv_states[index].exit_latency = exit_latency;
195 powernv_states[index].enter = idle_fn;
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530196 stop_psscr_table[index].val = psscr_val;
197 stop_psscr_table[index].mask = psscr_mask;
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530198}
199
Preeti U Murthy08888392014-02-26 05:39:20 +0530200static int powernv_add_idle_states(void)
201{
202 struct device_node *power_mgt;
Preeti U Murthy08888392014-02-26 05:39:20 +0530203 int nr_idle_states = 1; /* Snooze */
204 int dt_idle_states;
Shreyas B. Prabhu957efce2016-07-08 11:50:51 +0530205 u32 latency_ns[CPUIDLE_STATE_MAX];
206 u32 residency_ns[CPUIDLE_STATE_MAX];
207 u32 flags[CPUIDLE_STATE_MAX];
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530208 u64 psscr_val[CPUIDLE_STATE_MAX];
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530209 u64 psscr_mask[CPUIDLE_STATE_MAX];
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530210 const char *names[CPUIDLE_STATE_MAX];
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530211 u32 has_stop_states = 0;
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100212 int i, rc;
Preeti U Murthy08888392014-02-26 05:39:20 +0530213
214 /* Currently we have snooze statically defined */
215
216 power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
217 if (!power_mgt) {
218 pr_warn("opal: PowerMgmt Node not found\n");
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100219 goto out;
Preeti U Murthy08888392014-02-26 05:39:20 +0530220 }
221
Preeti U Murthy70734a72015-02-18 23:24:53 -0600222 /* Read values of any property to determine the num of idle states */
223 dt_idle_states = of_property_count_u32_elems(power_mgt, "ibm,cpu-idle-state-flags");
224 if (dt_idle_states < 0) {
225 pr_warn("cpuidle-powernv: no idle states found in the DT\n");
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100226 goto out;
Preeti U. Murthy74aa51b2014-10-14 13:23:00 +0530227 }
228
Shreyas B. Prabhu957efce2016-07-08 11:50:51 +0530229 /*
230 * Since snooze is used as first idle state, max idle states allowed is
231 * CPUIDLE_STATE_MAX -1
232 */
233 if (dt_idle_states > CPUIDLE_STATE_MAX - 1) {
234 pr_warn("cpuidle-powernv: discovered idle states more than allowed");
235 dt_idle_states = CPUIDLE_STATE_MAX - 1;
236 }
237
Preeti U Murthy70734a72015-02-18 23:24:53 -0600238 if (of_property_read_u32_array(power_mgt,
239 "ibm,cpu-idle-state-flags", flags, dt_idle_states)) {
240 pr_warn("cpuidle-powernv : missing ibm,cpu-idle-state-flags in DT\n");
Shreyas B. Prabhu957efce2016-07-08 11:50:51 +0530241 goto out;
Preeti U Murthy70734a72015-02-18 23:24:53 -0600242 }
Preeti U Murthy08888392014-02-26 05:39:20 +0530243
Shreyas B. Prabhu957efce2016-07-08 11:50:51 +0530244 if (of_property_read_u32_array(power_mgt,
245 "ibm,cpu-idle-state-latencies-ns", latency_ns,
246 dt_idle_states)) {
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100247 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-latencies-ns in DT\n");
Shreyas B. Prabhu957efce2016-07-08 11:50:51 +0530248 goto out;
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100249 }
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530250 if (of_property_read_string_array(power_mgt,
251 "ibm,cpu-idle-state-names", names, dt_idle_states) < 0) {
252 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-names in DT\n");
253 goto out;
254 }
255
256 /*
257 * If the idle states use stop instruction, probe for psscr values
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530258 * and psscr mask which are necessary to specify required stop level.
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530259 */
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530260 has_stop_states = (flags[0] &
261 (OPAL_PM_STOP_INST_FAST | OPAL_PM_STOP_INST_DEEP));
262 if (has_stop_states) {
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530263 if (of_property_read_u64_array(power_mgt,
264 "ibm,cpu-idle-state-psscr", psscr_val, dt_idle_states)) {
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530265 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr in DT\n");
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530266 goto out;
267 }
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100268
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530269 if (of_property_read_u64_array(power_mgt,
270 "ibm,cpu-idle-state-psscr-mask",
271 psscr_mask, dt_idle_states)) {
272 pr_warn("cpuidle-powernv:Missing ibm,cpu-idle-state-psscr-mask in DT\n");
273 goto out;
274 }
275 }
276
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100277 rc = of_property_read_u32_array(power_mgt,
278 "ibm,cpu-idle-state-residency-ns", residency_ns, dt_idle_states);
279
Preeti U Murthy08888392014-02-26 05:39:20 +0530280 for (i = 0; i < dt_idle_states; i++) {
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530281 unsigned int exit_latency, target_residency;
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530282 /*
283 * If an idle state has exit latency beyond
284 * POWERNV_THRESHOLD_LATENCY_NS then don't use it
285 * in cpu-idle.
286 */
287 if (latency_ns[i] > POWERNV_THRESHOLD_LATENCY_NS)
288 continue;
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530289 /*
290 * Firmware passes residency and latency values in ns.
291 * cpuidle expects it in us.
292 */
293 exit_latency = latency_ns[i] / 1000;
294 if (!rc)
295 target_residency = residency_ns[i] / 1000;
296 else
297 target_residency = 0;
Preeti U Murthy08888392014-02-26 05:39:20 +0530298
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530299 if (has_stop_states) {
300 int err = validate_psscr_val_mask(&psscr_val[i],
301 &psscr_mask[i],
302 flags[i]);
303 if (err) {
304 report_invalid_psscr_val(psscr_val[i], err);
305 continue;
306 }
307 }
308
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100309 /*
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530310 * For nap and fastsleep, use default target_residency
311 * values if f/w does not expose it.
Preeti U. Murthy74aa51b2014-10-14 13:23:00 +0530312 */
Preeti U Murthy70734a72015-02-18 23:24:53 -0600313 if (flags[i] & OPAL_PM_NAP_ENABLED) {
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530314 if (!rc)
315 target_residency = 100;
Preeti U Murthy08888392014-02-26 05:39:20 +0530316 /* Add NAP state */
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530317 add_powernv_state(nr_idle_states, "Nap",
318 CPUIDLE_FLAG_NONE, nap_loop,
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530319 target_residency, exit_latency, 0, 0);
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530320 } else if ((flags[i] & OPAL_PM_STOP_INST_FAST) &&
321 !(flags[i] & OPAL_PM_TIMEBASE_STOP)) {
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530322 add_powernv_state(nr_idle_states, names[i],
323 CPUIDLE_FLAG_NONE, stop_loop,
324 target_residency, exit_latency,
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530325 psscr_val[i], psscr_mask[i]);
preeticc5a2f72015-06-24 01:48:01 -0500326 }
327
328 /*
329 * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come
330 * within this config dependency check.
331 */
332#ifdef CONFIG_TICK_ONESHOT
333 if (flags[i] & OPAL_PM_SLEEP_ENABLED ||
Preeti U Murthy70734a72015-02-18 23:24:53 -0600334 flags[i] & OPAL_PM_SLEEP_ENABLED_ER1) {
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530335 if (!rc)
336 target_residency = 300000;
Preeti U Murthy08888392014-02-26 05:39:20 +0530337 /* Add FASTSLEEP state */
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530338 add_powernv_state(nr_idle_states, "FastSleep",
339 CPUIDLE_FLAG_TIMER_STOP,
340 fastsleep_loop,
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530341 target_residency, exit_latency, 0, 0);
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530342 } else if ((flags[i] & OPAL_PM_STOP_INST_DEEP) &&
343 (flags[i] & OPAL_PM_TIMEBASE_STOP)) {
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530344 add_powernv_state(nr_idle_states, names[i],
345 CPUIDLE_FLAG_TIMER_STOP, stop_loop,
346 target_residency, exit_latency,
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530347 psscr_val[i], psscr_mask[i]);
Preeti U Murthy08888392014-02-26 05:39:20 +0530348 }
preeticc5a2f72015-06-24 01:48:01 -0500349#endif
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100350 nr_idle_states++;
Preeti U Murthy08888392014-02-26 05:39:20 +0530351 }
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100352out:
Preeti U Murthy08888392014-02-26 05:39:20 +0530353 return nr_idle_states;
354}
355
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530356/*
357 * powernv_idle_probe()
358 * Choose state table for shared versus dedicated partition
359 */
360static int powernv_idle_probe(void)
361{
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530362 if (cpuidle_disable != IDLE_NO_OVERRIDE)
363 return -ENODEV;
364
Stewart Smithe4d54f72015-12-09 17:18:20 +1100365 if (firmware_has_feature(FW_FEATURE_OPAL)) {
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530366 cpuidle_state_table = powernv_states;
Preeti U Murthy08888392014-02-26 05:39:20 +0530367 /* Device tree can indicate more idle states */
368 max_idle_state = powernv_add_idle_states();
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +0530369 if (max_idle_state > 1) {
370 snooze_timeout_en = true;
371 snooze_timeout = powernv_states[1].target_residency *
372 tb_ticks_per_usec;
373 }
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530374 } else
375 return -ENODEV;
376
377 return 0;
378}
379
380static int __init powernv_processor_idle_init(void)
381{
382 int retval;
383
384 retval = powernv_idle_probe();
385 if (retval)
386 return retval;
387
388 powernv_cpuidle_driver_init();
389 retval = cpuidle_register(&powernv_idle_driver, NULL);
390 if (retval) {
391 printk(KERN_DEBUG "Registration of powernv driver failed.\n");
392 return retval;
393 }
394
Sebastian Andrzej Siewior10fcca92016-08-24 11:12:59 +0200395 retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
396 "cpuidle/powernv:online",
397 powernv_cpuidle_cpu_online, NULL);
398 WARN_ON(retval < 0);
399 retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD,
400 "cpuidle/powernv:dead", NULL,
401 powernv_cpuidle_cpu_dead);
402 WARN_ON(retval < 0);
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530403 printk(KERN_DEBUG "powernv_idle_driver registered\n");
404 return 0;
405}
406
407device_initcall(powernv_processor_idle_init);