blob: 1a8234e706bc7525683f34fa4e9294118987503d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +05302/*
3 * cpuidle-powernv - idle state cpuidle driver.
4 * Adapted from drivers/cpuidle/cpuidle-pseries
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>
14#include <linux/notifier.h>
Preeti U Murthy0d948732014-02-26 05:39:06 +053015#include <linux/clockchips.h>
Preeti U Murthy08888392014-02-26 05:39:20 +053016#include <linux/of.h>
Preeti U Murthy92c83ff52015-02-18 06:34:17 +010017#include <linux/slab.h>
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053018
19#include <asm/machdep.h>
20#include <asm/firmware.h>
Shreyas B. Prabhu8eb8ac82014-12-10 00:26:51 +053021#include <asm/opal.h>
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050022#include <asm/runlatch.h>
Gautham R. Shenoy09206b62017-01-25 14:06:28 +053023#include <asm/cpuidle.h>
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053024
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +053025/*
26 * Expose only those Hardware idle states via the cpuidle framework
27 * that have latency value below POWERNV_THRESHOLD_LATENCY_NS.
28 */
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +053029#define POWERNV_THRESHOLD_LATENCY_NS 200000
30
Andrew Donnellaned613902016-11-22 16:08:06 +110031static struct cpuidle_driver powernv_idle_driver = {
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053032 .name = "powernv_idle",
33 .owner = THIS_MODULE,
34};
35
Nicholas Piggin624e46d2017-06-14 23:02:40 +100036static int max_idle_state __read_mostly;
37static struct cpuidle_state *cpuidle_state_table __read_mostly;
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +053038
Gautham R. Shenoy09206b62017-01-25 14:06:28 +053039struct stop_psscr_table {
40 u64 val;
41 u64 mask;
42};
43
Nicholas Piggin624e46d2017-06-14 23:02:40 +100044static struct stop_psscr_table stop_psscr_table[CPUIDLE_STATE_MAX] __read_mostly;
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +053045
Nicholas Piggin624e46d2017-06-14 23:02:40 +100046static u64 snooze_timeout __read_mostly;
47static bool snooze_timeout_en __read_mostly;
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053048
49static int snooze_loop(struct cpuidle_device *dev,
50 struct cpuidle_driver *drv,
51 int index)
52{
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053053 u64 snooze_exit_time;
54
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053055 set_thread_flag(TIF_POLLING_NRFLAG);
56
Nicholas Piggin3fc5ee922017-06-14 23:02:39 +100057 local_irq_enable();
58
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053059 snooze_exit_time = get_tb() + snooze_timeout;
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050060 ppc64_runlatch_off();
Anton Blanchard26eb48a2017-04-04 07:54:13 +100061 HMT_very_low();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053062 while (!need_resched()) {
Nicholas Piggin7ded4292017-06-14 23:02:41 +100063 if (likely(snooze_timeout_en) && get_tb() > snooze_exit_time) {
64 /*
65 * Task has not woken up but we are exiting the polling
66 * loop anyway. Require a barrier after polling is
67 * cleared to order subsequent test of need_resched().
68 */
69 clear_thread_flag(TIF_POLLING_NRFLAG);
70 smp_mb();
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053071 break;
Nicholas Piggin7ded4292017-06-14 23:02:41 +100072 }
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053073 }
74
75 HMT_medium();
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050076 ppc64_runlatch_on();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053077 clear_thread_flag(TIF_POLLING_NRFLAG);
Nicholas Piggin3fc5ee922017-06-14 23:02:39 +100078
Nicholas Pigginf1343d02017-11-17 02:00:51 +100079 local_irq_disable();
80
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053081 return index;
82}
83
84static int nap_loop(struct cpuidle_device *dev,
85 struct cpuidle_driver *drv,
86 int index)
87{
Nicholas Piggin2201f992017-06-13 23:05:45 +100088 power7_idle_type(PNV_THREAD_NAP);
89
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053090 return index;
91}
92
preeticc5a2f72015-06-24 01:48:01 -050093/* Register for fastsleep only in oneshot mode of broadcast */
94#ifdef CONFIG_TICK_ONESHOT
Preeti U Murthy0d948732014-02-26 05:39:06 +053095static int fastsleep_loop(struct cpuidle_device *dev,
96 struct cpuidle_driver *drv,
97 int index)
98{
99 unsigned long old_lpcr = mfspr(SPRN_LPCR);
100 unsigned long new_lpcr;
101
102 if (unlikely(system_state < SYSTEM_RUNNING))
103 return index;
104
105 new_lpcr = old_lpcr;
Michael Neuling9b6a68d2014-06-11 15:59:27 +1000106 /* Do not exit powersave upon decrementer as we've setup the timer
107 * offload.
Preeti U Murthy0d948732014-02-26 05:39:06 +0530108 */
Michael Neuling9b6a68d2014-06-11 15:59:27 +1000109 new_lpcr &= ~LPCR_PECE1;
Preeti U Murthy0d948732014-02-26 05:39:06 +0530110
111 mtspr(SPRN_LPCR, new_lpcr);
Nicholas Piggin2201f992017-06-13 23:05:45 +1000112
113 power7_idle_type(PNV_THREAD_SLEEP);
Preeti U Murthy0d948732014-02-26 05:39:06 +0530114
115 mtspr(SPRN_LPCR, old_lpcr);
116
117 return index;
118}
preeticc5a2f72015-06-24 01:48:01 -0500119#endif
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530120
121static int stop_loop(struct cpuidle_device *dev,
122 struct cpuidle_driver *drv,
123 int index)
124{
Nicholas Piggin2201f992017-06-13 23:05:45 +1000125 power9_idle_type(stop_psscr_table[index].val,
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530126 stop_psscr_table[index].mask);
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530127 return index;
128}
129
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530130/*
131 * States for dedicated partition case.
132 */
Shreyas B. Prabhu169f3fa2016-07-08 11:50:50 +0530133static struct cpuidle_state powernv_states[CPUIDLE_STATE_MAX] = {
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530134 { /* Snooze */
135 .name = "snooze",
136 .desc = "snooze",
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530137 .exit_latency = 0,
138 .target_residency = 0,
Shreyas B. Prabhu957efce2016-07-08 11:50:51 +0530139 .enter = snooze_loop },
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530140};
141
Sebastian Andrzej Siewior10fcca92016-08-24 11:12:59 +0200142static int powernv_cpuidle_cpu_online(unsigned int cpu)
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530143{
Sebastian Andrzej Siewior10fcca92016-08-24 11:12:59 +0200144 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530145
146 if (dev && cpuidle_get_driver()) {
Sebastian Andrzej Siewior10fcca92016-08-24 11:12:59 +0200147 cpuidle_pause_and_lock();
148 cpuidle_enable_device(dev);
149 cpuidle_resume_and_unlock();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530150 }
Sebastian Andrzej Siewior10fcca92016-08-24 11:12:59 +0200151 return 0;
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530152}
153
Sebastian Andrzej Siewior10fcca92016-08-24 11:12:59 +0200154static int powernv_cpuidle_cpu_dead(unsigned int cpu)
155{
156 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
157
158 if (dev && cpuidle_get_driver()) {
159 cpuidle_pause_and_lock();
160 cpuidle_disable_device(dev);
161 cpuidle_resume_and_unlock();
162 }
163 return 0;
164}
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530165
166/*
167 * powernv_cpuidle_driver_init()
168 */
169static int powernv_cpuidle_driver_init(void)
170{
171 int idle_state;
172 struct cpuidle_driver *drv = &powernv_idle_driver;
173
174 drv->state_count = 0;
175
176 for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
177 /* Is the state not enabled? */
178 if (cpuidle_state_table[idle_state].enter == NULL)
179 continue;
180
181 drv->states[drv->state_count] = /* structure copy */
182 cpuidle_state_table[idle_state];
183
184 drv->state_count += 1;
185 }
186
Vaidyanathan Srinivasan293d2642017-03-23 20:52:46 +0530187 /*
188 * On the PowerNV platform cpu_present may be less than cpu_possible in
189 * cases when firmware detects the CPU, but it is not available to the
190 * OS. If CONFIG_HOTPLUG_CPU=n, then such CPUs are not hotplugable at
191 * run time and hence cpu_devices are not created for those CPUs by the
192 * generic topology_init().
193 *
194 * drv->cpumask defaults to cpu_possible_mask in
195 * __cpuidle_driver_init(). This breaks cpuidle on PowerNV where
196 * cpu_devices are not created for CPUs in cpu_possible_mask that
197 * cannot be hot-added later at run time.
198 *
199 * Trying cpuidle_register_device() on a CPU without a cpu_device is
200 * incorrect, so pass a correct CPU mask to the generic cpuidle driver.
201 */
202
203 drv->cpumask = (struct cpumask *)cpu_present_mask;
204
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530205 return 0;
206}
207
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530208static inline void add_powernv_state(int index, const char *name,
209 unsigned int flags,
210 int (*idle_fn)(struct cpuidle_device *,
211 struct cpuidle_driver *,
212 int),
213 unsigned int target_residency,
214 unsigned int exit_latency,
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530215 u64 psscr_val, u64 psscr_mask)
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530216{
217 strlcpy(powernv_states[index].name, name, CPUIDLE_NAME_LEN);
218 strlcpy(powernv_states[index].desc, name, CPUIDLE_NAME_LEN);
219 powernv_states[index].flags = flags;
220 powernv_states[index].target_residency = target_residency;
221 powernv_states[index].exit_latency = exit_latency;
222 powernv_states[index].enter = idle_fn;
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530223 stop_psscr_table[index].val = psscr_val;
224 stop_psscr_table[index].mask = psscr_mask;
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530225}
226
Gautham R. Shenoyecad4502017-03-15 13:45:53 +0530227/*
228 * Returns 0 if prop1_len == prop2_len. Else returns -1
229 */
230static inline int validate_dt_prop_sizes(const char *prop1, int prop1_len,
231 const char *prop2, int prop2_len)
232{
233 if (prop1_len == prop2_len)
234 return 0;
235
236 pr_warn("cpuidle-powernv: array sizes don't match for %s and %s\n",
237 prop1, prop2);
238 return -1;
239}
240
Gautham R. Shenoy785a12a2017-08-08 14:13:15 +0530241extern u32 pnv_get_supported_cpuidle_states(void);
Preeti U Murthy08888392014-02-26 05:39:20 +0530242static int powernv_add_idle_states(void)
243{
244 struct device_node *power_mgt;
Preeti U Murthy08888392014-02-26 05:39:20 +0530245 int nr_idle_states = 1; /* Snooze */
Gautham R. Shenoyecad4502017-03-15 13:45:53 +0530246 int dt_idle_states, count;
Shreyas B. Prabhu957efce2016-07-08 11:50:51 +0530247 u32 latency_ns[CPUIDLE_STATE_MAX];
248 u32 residency_ns[CPUIDLE_STATE_MAX];
249 u32 flags[CPUIDLE_STATE_MAX];
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530250 u64 psscr_val[CPUIDLE_STATE_MAX];
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530251 u64 psscr_mask[CPUIDLE_STATE_MAX];
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530252 const char *names[CPUIDLE_STATE_MAX];
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530253 u32 has_stop_states = 0;
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100254 int i, rc;
Gautham R. Shenoy785a12a2017-08-08 14:13:15 +0530255 u32 supported_flags = pnv_get_supported_cpuidle_states();
256
Preeti U Murthy08888392014-02-26 05:39:20 +0530257
258 /* Currently we have snooze statically defined */
259
260 power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
261 if (!power_mgt) {
262 pr_warn("opal: PowerMgmt Node not found\n");
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100263 goto out;
Preeti U Murthy08888392014-02-26 05:39:20 +0530264 }
265
Preeti U Murthy70734a72015-02-18 23:24:53 -0600266 /* Read values of any property to determine the num of idle states */
267 dt_idle_states = of_property_count_u32_elems(power_mgt, "ibm,cpu-idle-state-flags");
268 if (dt_idle_states < 0) {
269 pr_warn("cpuidle-powernv: no idle states found in the DT\n");
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100270 goto out;
Preeti U. Murthy74aa51b2014-10-14 13:23:00 +0530271 }
272
Gautham R. Shenoyecad4502017-03-15 13:45:53 +0530273 count = of_property_count_u32_elems(power_mgt,
274 "ibm,cpu-idle-state-latencies-ns");
275
276 if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags", dt_idle_states,
277 "ibm,cpu-idle-state-latencies-ns",
278 count) != 0)
279 goto out;
280
281 count = of_property_count_strings(power_mgt,
282 "ibm,cpu-idle-state-names");
283 if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags", dt_idle_states,
284 "ibm,cpu-idle-state-names",
285 count) != 0)
286 goto out;
287
Shreyas B. Prabhu957efce2016-07-08 11:50:51 +0530288 /*
289 * Since snooze is used as first idle state, max idle states allowed is
290 * CPUIDLE_STATE_MAX -1
291 */
292 if (dt_idle_states > CPUIDLE_STATE_MAX - 1) {
293 pr_warn("cpuidle-powernv: discovered idle states more than allowed");
294 dt_idle_states = CPUIDLE_STATE_MAX - 1;
295 }
296
Preeti U Murthy70734a72015-02-18 23:24:53 -0600297 if (of_property_read_u32_array(power_mgt,
298 "ibm,cpu-idle-state-flags", flags, dt_idle_states)) {
299 pr_warn("cpuidle-powernv : missing ibm,cpu-idle-state-flags in DT\n");
Shreyas B. Prabhu957efce2016-07-08 11:50:51 +0530300 goto out;
Preeti U Murthy70734a72015-02-18 23:24:53 -0600301 }
Preeti U Murthy08888392014-02-26 05:39:20 +0530302
Shreyas B. Prabhu957efce2016-07-08 11:50:51 +0530303 if (of_property_read_u32_array(power_mgt,
304 "ibm,cpu-idle-state-latencies-ns", latency_ns,
305 dt_idle_states)) {
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100306 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-latencies-ns in DT\n");
Shreyas B. Prabhu957efce2016-07-08 11:50:51 +0530307 goto out;
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100308 }
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530309 if (of_property_read_string_array(power_mgt,
310 "ibm,cpu-idle-state-names", names, dt_idle_states) < 0) {
311 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-names in DT\n");
312 goto out;
313 }
314
315 /*
316 * If the idle states use stop instruction, probe for psscr values
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530317 * and psscr mask which are necessary to specify required stop level.
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530318 */
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530319 has_stop_states = (flags[0] &
320 (OPAL_PM_STOP_INST_FAST | OPAL_PM_STOP_INST_DEEP));
321 if (has_stop_states) {
Gautham R. Shenoyecad4502017-03-15 13:45:53 +0530322 count = of_property_count_u64_elems(power_mgt,
323 "ibm,cpu-idle-state-psscr");
324 if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags",
325 dt_idle_states,
326 "ibm,cpu-idle-state-psscr",
327 count) != 0)
328 goto out;
329
330 count = of_property_count_u64_elems(power_mgt,
331 "ibm,cpu-idle-state-psscr-mask");
332 if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags",
333 dt_idle_states,
334 "ibm,cpu-idle-state-psscr-mask",
335 count) != 0)
336 goto out;
337
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530338 if (of_property_read_u64_array(power_mgt,
339 "ibm,cpu-idle-state-psscr", psscr_val, dt_idle_states)) {
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530340 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr in DT\n");
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530341 goto out;
342 }
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100343
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530344 if (of_property_read_u64_array(power_mgt,
345 "ibm,cpu-idle-state-psscr-mask",
346 psscr_mask, dt_idle_states)) {
347 pr_warn("cpuidle-powernv:Missing ibm,cpu-idle-state-psscr-mask in DT\n");
348 goto out;
349 }
350 }
351
Gautham R. Shenoyecad4502017-03-15 13:45:53 +0530352 count = of_property_count_u32_elems(power_mgt,
353 "ibm,cpu-idle-state-residency-ns");
354
355 if (count < 0) {
356 rc = count;
357 } else if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags",
358 dt_idle_states,
359 "ibm,cpu-idle-state-residency-ns",
360 count) != 0) {
361 goto out;
362 } else {
363 rc = of_property_read_u32_array(power_mgt,
364 "ibm,cpu-idle-state-residency-ns",
365 residency_ns, dt_idle_states);
366 }
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100367
Preeti U Murthy08888392014-02-26 05:39:20 +0530368 for (i = 0; i < dt_idle_states; i++) {
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530369 unsigned int exit_latency, target_residency;
Gautham R. Shenoyf9122ee2017-05-16 14:19:48 +0530370 bool stops_timebase = false;
Gautham R. Shenoy785a12a2017-08-08 14:13:15 +0530371
372 /*
373 * Skip the platform idle state whose flag isn't in
374 * the supported_cpuidle_states flag mask.
375 */
376 if ((flags[i] & supported_flags) != flags[i])
377 continue;
Shreyas B. Prabhu3005c592016-07-08 11:50:52 +0530378 /*
379 * If an idle state has exit latency beyond
380 * POWERNV_THRESHOLD_LATENCY_NS then don't use it
381 * in cpu-idle.
382 */
383 if (latency_ns[i] > POWERNV_THRESHOLD_LATENCY_NS)
384 continue;
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530385 /*
386 * Firmware passes residency and latency values in ns.
387 * cpuidle expects it in us.
388 */
Vaidyanathan Srinivasan8d4e10e2017-08-24 00:28:41 +0530389 exit_latency = DIV_ROUND_UP(latency_ns[i], 1000);
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530390 if (!rc)
Vaidyanathan Srinivasan8d4e10e2017-08-24 00:28:41 +0530391 target_residency = DIV_ROUND_UP(residency_ns[i], 1000);
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530392 else
393 target_residency = 0;
Preeti U Murthy08888392014-02-26 05:39:20 +0530394
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530395 if (has_stop_states) {
396 int err = validate_psscr_val_mask(&psscr_val[i],
397 &psscr_mask[i],
398 flags[i]);
399 if (err) {
400 report_invalid_psscr_val(psscr_val[i], err);
401 continue;
402 }
403 }
404
Gautham R. Shenoyf9122ee2017-05-16 14:19:48 +0530405 if (flags[i] & OPAL_PM_TIMEBASE_STOP)
406 stops_timebase = true;
407
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100408 /*
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530409 * For nap and fastsleep, use default target_residency
410 * values if f/w does not expose it.
Preeti U. Murthy74aa51b2014-10-14 13:23:00 +0530411 */
Preeti U Murthy70734a72015-02-18 23:24:53 -0600412 if (flags[i] & OPAL_PM_NAP_ENABLED) {
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530413 if (!rc)
414 target_residency = 100;
Preeti U Murthy08888392014-02-26 05:39:20 +0530415 /* Add NAP state */
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530416 add_powernv_state(nr_idle_states, "Nap",
417 CPUIDLE_FLAG_NONE, nap_loop,
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530418 target_residency, exit_latency, 0, 0);
Gautham R. Shenoyf9122ee2017-05-16 14:19:48 +0530419 } else if (has_stop_states && !stops_timebase) {
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530420 add_powernv_state(nr_idle_states, names[i],
421 CPUIDLE_FLAG_NONE, stop_loop,
422 target_residency, exit_latency,
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530423 psscr_val[i], psscr_mask[i]);
preeticc5a2f72015-06-24 01:48:01 -0500424 }
425
426 /*
427 * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come
428 * within this config dependency check.
429 */
430#ifdef CONFIG_TICK_ONESHOT
Gautham R. Shenoyf9122ee2017-05-16 14:19:48 +0530431 else if (flags[i] & OPAL_PM_SLEEP_ENABLED ||
432 flags[i] & OPAL_PM_SLEEP_ENABLED_ER1) {
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530433 if (!rc)
434 target_residency = 300000;
Preeti U Murthy08888392014-02-26 05:39:20 +0530435 /* Add FASTSLEEP state */
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530436 add_powernv_state(nr_idle_states, "FastSleep",
437 CPUIDLE_FLAG_TIMER_STOP,
438 fastsleep_loop,
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530439 target_residency, exit_latency, 0, 0);
Gautham R. Shenoyf9122ee2017-05-16 14:19:48 +0530440 } else if (has_stop_states && stops_timebase) {
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +0530441 add_powernv_state(nr_idle_states, names[i],
442 CPUIDLE_FLAG_TIMER_STOP, stop_loop,
443 target_residency, exit_latency,
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530444 psscr_val[i], psscr_mask[i]);
Preeti U Murthy08888392014-02-26 05:39:20 +0530445 }
preeticc5a2f72015-06-24 01:48:01 -0500446#endif
Gautham R. Shenoyf9122ee2017-05-16 14:19:48 +0530447 else
448 continue;
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100449 nr_idle_states++;
Preeti U Murthy08888392014-02-26 05:39:20 +0530450 }
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100451out:
Preeti U Murthy08888392014-02-26 05:39:20 +0530452 return nr_idle_states;
453}
454
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530455/*
456 * powernv_idle_probe()
457 * Choose state table for shared versus dedicated partition
458 */
459static int powernv_idle_probe(void)
460{
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530461 if (cpuidle_disable != IDLE_NO_OVERRIDE)
462 return -ENODEV;
463
Stewart Smithe4d54f72015-12-09 17:18:20 +1100464 if (firmware_has_feature(FW_FEATURE_OPAL)) {
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530465 cpuidle_state_table = powernv_states;
Preeti U Murthy08888392014-02-26 05:39:20 +0530466 /* Device tree can indicate more idle states */
467 max_idle_state = powernv_add_idle_states();
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +0530468 if (max_idle_state > 1) {
469 snooze_timeout_en = true;
470 snooze_timeout = powernv_states[1].target_residency *
471 tb_ticks_per_usec;
472 }
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530473 } else
474 return -ENODEV;
475
476 return 0;
477}
478
479static int __init powernv_processor_idle_init(void)
480{
481 int retval;
482
483 retval = powernv_idle_probe();
484 if (retval)
485 return retval;
486
487 powernv_cpuidle_driver_init();
488 retval = cpuidle_register(&powernv_idle_driver, NULL);
489 if (retval) {
490 printk(KERN_DEBUG "Registration of powernv driver failed.\n");
491 return retval;
492 }
493
Sebastian Andrzej Siewior10fcca92016-08-24 11:12:59 +0200494 retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
495 "cpuidle/powernv:online",
496 powernv_cpuidle_cpu_online, NULL);
497 WARN_ON(retval < 0);
498 retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD,
499 "cpuidle/powernv:dead", NULL,
500 powernv_cpuidle_cpu_dead);
501 WARN_ON(retval < 0);
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530502 printk(KERN_DEBUG "powernv_idle_driver registered\n");
503 return 0;
504}
505
506device_initcall(powernv_processor_idle_init);