blob: 3a763a823416e403f1eb4212dcf5b08bdc2ac207 [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
23struct cpuidle_driver powernv_idle_driver = {
24 .name = "powernv_idle",
25 .owner = THIS_MODULE,
26};
27
28static int max_idle_state;
29static struct cpuidle_state *cpuidle_state_table;
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053030static u64 snooze_timeout;
31static bool snooze_timeout_en;
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053032
33static int snooze_loop(struct cpuidle_device *dev,
34 struct cpuidle_driver *drv,
35 int index)
36{
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053037 u64 snooze_exit_time;
38
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053039 local_irq_enable();
40 set_thread_flag(TIF_POLLING_NRFLAG);
41
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053042 snooze_exit_time = get_tb() + snooze_timeout;
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050043 ppc64_runlatch_off();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053044 while (!need_resched()) {
45 HMT_low();
46 HMT_very_low();
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053047 if (snooze_timeout_en && get_tb() > snooze_exit_time)
48 break;
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053049 }
50
51 HMT_medium();
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050052 ppc64_runlatch_on();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053053 clear_thread_flag(TIF_POLLING_NRFLAG);
54 smp_mb();
55 return index;
56}
57
58static int nap_loop(struct cpuidle_device *dev,
59 struct cpuidle_driver *drv,
60 int index)
61{
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050062 ppc64_runlatch_off();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053063 power7_idle();
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050064 ppc64_runlatch_on();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053065 return index;
66}
67
preeticc5a2f72015-06-24 01:48:01 -050068/* Register for fastsleep only in oneshot mode of broadcast */
69#ifdef CONFIG_TICK_ONESHOT
Preeti U Murthy0d948732014-02-26 05:39:06 +053070static int fastsleep_loop(struct cpuidle_device *dev,
71 struct cpuidle_driver *drv,
72 int index)
73{
74 unsigned long old_lpcr = mfspr(SPRN_LPCR);
75 unsigned long new_lpcr;
76
77 if (unlikely(system_state < SYSTEM_RUNNING))
78 return index;
79
80 new_lpcr = old_lpcr;
Michael Neuling9b6a68d2014-06-11 15:59:27 +100081 /* Do not exit powersave upon decrementer as we've setup the timer
82 * offload.
Preeti U Murthy0d948732014-02-26 05:39:06 +053083 */
Michael Neuling9b6a68d2014-06-11 15:59:27 +100084 new_lpcr &= ~LPCR_PECE1;
Preeti U Murthy0d948732014-02-26 05:39:06 +053085
86 mtspr(SPRN_LPCR, new_lpcr);
87 power7_sleep();
88
89 mtspr(SPRN_LPCR, old_lpcr);
90
91 return index;
92}
preeticc5a2f72015-06-24 01:48:01 -050093#endif
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053094/*
95 * States for dedicated partition case.
96 */
Shreyas B. Prabhu169f3fa2016-07-08 11:50:50 +053097static struct cpuidle_state powernv_states[CPUIDLE_STATE_MAX] = {
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053098 { /* Snooze */
99 .name = "snooze",
100 .desc = "snooze",
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530101 .exit_latency = 0,
102 .target_residency = 0,
103 .enter = &snooze_loop },
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530104};
105
106static int powernv_cpuidle_add_cpu_notifier(struct notifier_block *n,
107 unsigned long action, void *hcpu)
108{
109 int hotcpu = (unsigned long)hcpu;
110 struct cpuidle_device *dev =
111 per_cpu(cpuidle_devices, hotcpu);
112
113 if (dev && cpuidle_get_driver()) {
114 switch (action) {
115 case CPU_ONLINE:
116 case CPU_ONLINE_FROZEN:
117 cpuidle_pause_and_lock();
118 cpuidle_enable_device(dev);
119 cpuidle_resume_and_unlock();
120 break;
121
122 case CPU_DEAD:
123 case CPU_DEAD_FROZEN:
124 cpuidle_pause_and_lock();
125 cpuidle_disable_device(dev);
126 cpuidle_resume_and_unlock();
127 break;
128
129 default:
130 return NOTIFY_DONE;
131 }
132 }
133 return NOTIFY_OK;
134}
135
136static struct notifier_block setup_hotplug_notifier = {
137 .notifier_call = powernv_cpuidle_add_cpu_notifier,
138};
139
140/*
141 * powernv_cpuidle_driver_init()
142 */
143static int powernv_cpuidle_driver_init(void)
144{
145 int idle_state;
146 struct cpuidle_driver *drv = &powernv_idle_driver;
147
148 drv->state_count = 0;
149
150 for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
151 /* Is the state not enabled? */
152 if (cpuidle_state_table[idle_state].enter == NULL)
153 continue;
154
155 drv->states[drv->state_count] = /* structure copy */
156 cpuidle_state_table[idle_state];
157
158 drv->state_count += 1;
159 }
160
161 return 0;
162}
163
Preeti U Murthy08888392014-02-26 05:39:20 +0530164static int powernv_add_idle_states(void)
165{
166 struct device_node *power_mgt;
Preeti U Murthy08888392014-02-26 05:39:20 +0530167 int nr_idle_states = 1; /* Snooze */
168 int dt_idle_states;
Preeti U Murthy70734a72015-02-18 23:24:53 -0600169 u32 *latency_ns, *residency_ns, *flags;
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100170 int i, rc;
Preeti U Murthy08888392014-02-26 05:39:20 +0530171
172 /* Currently we have snooze statically defined */
173
174 power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
175 if (!power_mgt) {
176 pr_warn("opal: PowerMgmt Node not found\n");
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100177 goto out;
Preeti U Murthy08888392014-02-26 05:39:20 +0530178 }
179
Preeti U Murthy70734a72015-02-18 23:24:53 -0600180 /* Read values of any property to determine the num of idle states */
181 dt_idle_states = of_property_count_u32_elems(power_mgt, "ibm,cpu-idle-state-flags");
182 if (dt_idle_states < 0) {
183 pr_warn("cpuidle-powernv: no idle states found in the DT\n");
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100184 goto out;
Preeti U. Murthy74aa51b2014-10-14 13:23:00 +0530185 }
186
Preeti U Murthy70734a72015-02-18 23:24:53 -0600187 flags = kzalloc(sizeof(*flags) * dt_idle_states, GFP_KERNEL);
188 if (of_property_read_u32_array(power_mgt,
189 "ibm,cpu-idle-state-flags", flags, dt_idle_states)) {
190 pr_warn("cpuidle-powernv : missing ibm,cpu-idle-state-flags in DT\n");
191 goto out_free_flags;
192 }
Preeti U Murthy08888392014-02-26 05:39:20 +0530193
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100194 latency_ns = kzalloc(sizeof(*latency_ns) * dt_idle_states, GFP_KERNEL);
195 rc = of_property_read_u32_array(power_mgt,
196 "ibm,cpu-idle-state-latencies-ns", latency_ns, dt_idle_states);
197 if (rc) {
198 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-latencies-ns in DT\n");
199 goto out_free_latency;
200 }
201
202 residency_ns = kzalloc(sizeof(*residency_ns) * dt_idle_states, GFP_KERNEL);
203 rc = of_property_read_u32_array(power_mgt,
204 "ibm,cpu-idle-state-residency-ns", residency_ns, dt_idle_states);
205
Preeti U Murthy08888392014-02-26 05:39:20 +0530206 for (i = 0; i < dt_idle_states; i++) {
207
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100208 /*
209 * Cpuidle accepts exit_latency and target_residency in us.
210 * Use default target_residency values if f/w does not expose it.
Preeti U. Murthy74aa51b2014-10-14 13:23:00 +0530211 */
Preeti U Murthy70734a72015-02-18 23:24:53 -0600212 if (flags[i] & OPAL_PM_NAP_ENABLED) {
Preeti U Murthy08888392014-02-26 05:39:20 +0530213 /* Add NAP state */
214 strcpy(powernv_states[nr_idle_states].name, "Nap");
215 strcpy(powernv_states[nr_idle_states].desc, "Nap");
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100216 powernv_states[nr_idle_states].flags = 0;
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100217 powernv_states[nr_idle_states].target_residency = 100;
Preeti U Murthy08888392014-02-26 05:39:20 +0530218 powernv_states[nr_idle_states].enter = &nap_loop;
preeticc5a2f72015-06-24 01:48:01 -0500219 }
220
221 /*
222 * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come
223 * within this config dependency check.
224 */
225#ifdef CONFIG_TICK_ONESHOT
226 if (flags[i] & OPAL_PM_SLEEP_ENABLED ||
Preeti U Murthy70734a72015-02-18 23:24:53 -0600227 flags[i] & OPAL_PM_SLEEP_ENABLED_ER1) {
Preeti U Murthy08888392014-02-26 05:39:20 +0530228 /* Add FASTSLEEP state */
229 strcpy(powernv_states[nr_idle_states].name, "FastSleep");
230 strcpy(powernv_states[nr_idle_states].desc, "FastSleep");
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100231 powernv_states[nr_idle_states].flags = CPUIDLE_FLAG_TIMER_STOP;
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100232 powernv_states[nr_idle_states].target_residency = 300000;
Preeti U Murthy08888392014-02-26 05:39:20 +0530233 powernv_states[nr_idle_states].enter = &fastsleep_loop;
Preeti U Murthy08888392014-02-26 05:39:20 +0530234 }
preeticc5a2f72015-06-24 01:48:01 -0500235#endif
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100236 powernv_states[nr_idle_states].exit_latency =
237 ((unsigned int)latency_ns[i]) / 1000;
238
239 if (!rc) {
240 powernv_states[nr_idle_states].target_residency =
241 ((unsigned int)residency_ns[i]) / 1000;
242 }
243
244 nr_idle_states++;
Preeti U Murthy08888392014-02-26 05:39:20 +0530245 }
246
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100247 kfree(residency_ns);
248out_free_latency:
249 kfree(latency_ns);
Preeti U Murthy70734a72015-02-18 23:24:53 -0600250out_free_flags:
251 kfree(flags);
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100252out:
Preeti U Murthy08888392014-02-26 05:39:20 +0530253 return nr_idle_states;
254}
255
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530256/*
257 * powernv_idle_probe()
258 * Choose state table for shared versus dedicated partition
259 */
260static int powernv_idle_probe(void)
261{
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530262 if (cpuidle_disable != IDLE_NO_OVERRIDE)
263 return -ENODEV;
264
Stewart Smithe4d54f72015-12-09 17:18:20 +1100265 if (firmware_has_feature(FW_FEATURE_OPAL)) {
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530266 cpuidle_state_table = powernv_states;
Preeti U Murthy08888392014-02-26 05:39:20 +0530267 /* Device tree can indicate more idle states */
268 max_idle_state = powernv_add_idle_states();
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +0530269 if (max_idle_state > 1) {
270 snooze_timeout_en = true;
271 snooze_timeout = powernv_states[1].target_residency *
272 tb_ticks_per_usec;
273 }
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530274 } else
275 return -ENODEV;
276
277 return 0;
278}
279
280static int __init powernv_processor_idle_init(void)
281{
282 int retval;
283
284 retval = powernv_idle_probe();
285 if (retval)
286 return retval;
287
288 powernv_cpuidle_driver_init();
289 retval = cpuidle_register(&powernv_idle_driver, NULL);
290 if (retval) {
291 printk(KERN_DEBUG "Registration of powernv driver failed.\n");
292 return retval;
293 }
294
295 register_cpu_notifier(&setup_hotplug_notifier);
296 printk(KERN_DEBUG "powernv_idle_driver registered\n");
297 return 0;
298}
299
300device_initcall(powernv_processor_idle_init);