blob: e12dc30d8864e007fa5b70cc2969a1f00c9cf2b2 [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
Preeti U Murthy08888392014-02-26 05:39:20 +053023#define MAX_POWERNV_IDLE_STATES 8
Preeti U Murthy08888392014-02-26 05:39:20 +053024
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;
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053032static u64 snooze_timeout;
33static bool snooze_timeout_en;
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053034
35static int snooze_loop(struct cpuidle_device *dev,
36 struct cpuidle_driver *drv,
37 int index)
38{
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053039 u64 snooze_exit_time;
40
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053041 local_irq_enable();
42 set_thread_flag(TIF_POLLING_NRFLAG);
43
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053044 snooze_exit_time = get_tb() + snooze_timeout;
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050045 ppc64_runlatch_off();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053046 while (!need_resched()) {
47 HMT_low();
48 HMT_very_low();
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +053049 if (snooze_timeout_en && get_tb() > snooze_exit_time)
50 break;
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053051 }
52
53 HMT_medium();
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050054 ppc64_runlatch_on();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053055 clear_thread_flag(TIF_POLLING_NRFLAG);
56 smp_mb();
57 return index;
58}
59
60static int nap_loop(struct cpuidle_device *dev,
61 struct cpuidle_driver *drv,
62 int index)
63{
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050064 ppc64_runlatch_off();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053065 power7_idle();
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050066 ppc64_runlatch_on();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053067 return index;
68}
69
preeticc5a2f72015-06-24 01:48:01 -050070/* Register for fastsleep only in oneshot mode of broadcast */
71#ifdef CONFIG_TICK_ONESHOT
Preeti U Murthy0d948732014-02-26 05:39:06 +053072static int fastsleep_loop(struct cpuidle_device *dev,
73 struct cpuidle_driver *drv,
74 int index)
75{
76 unsigned long old_lpcr = mfspr(SPRN_LPCR);
77 unsigned long new_lpcr;
78
79 if (unlikely(system_state < SYSTEM_RUNNING))
80 return index;
81
82 new_lpcr = old_lpcr;
Michael Neuling9b6a68d2014-06-11 15:59:27 +100083 /* Do not exit powersave upon decrementer as we've setup the timer
84 * offload.
Preeti U Murthy0d948732014-02-26 05:39:06 +053085 */
Michael Neuling9b6a68d2014-06-11 15:59:27 +100086 new_lpcr &= ~LPCR_PECE1;
Preeti U Murthy0d948732014-02-26 05:39:06 +053087
88 mtspr(SPRN_LPCR, new_lpcr);
89 power7_sleep();
90
91 mtspr(SPRN_LPCR, old_lpcr);
92
93 return index;
94}
preeticc5a2f72015-06-24 01:48:01 -050095#endif
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053096/*
97 * States for dedicated partition case.
98 */
Preeti U Murthy08888392014-02-26 05:39:20 +053099static struct cpuidle_state powernv_states[MAX_POWERNV_IDLE_STATES] = {
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530100 { /* Snooze */
101 .name = "snooze",
102 .desc = "snooze",
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530103 .exit_latency = 0,
104 .target_residency = 0,
105 .enter = &snooze_loop },
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530106};
107
108static int powernv_cpuidle_add_cpu_notifier(struct notifier_block *n,
109 unsigned long action, void *hcpu)
110{
111 int hotcpu = (unsigned long)hcpu;
112 struct cpuidle_device *dev =
113 per_cpu(cpuidle_devices, hotcpu);
114
115 if (dev && cpuidle_get_driver()) {
116 switch (action) {
117 case CPU_ONLINE:
118 case CPU_ONLINE_FROZEN:
119 cpuidle_pause_and_lock();
120 cpuidle_enable_device(dev);
121 cpuidle_resume_and_unlock();
122 break;
123
124 case CPU_DEAD:
125 case CPU_DEAD_FROZEN:
126 cpuidle_pause_and_lock();
127 cpuidle_disable_device(dev);
128 cpuidle_resume_and_unlock();
129 break;
130
131 default:
132 return NOTIFY_DONE;
133 }
134 }
135 return NOTIFY_OK;
136}
137
138static struct notifier_block setup_hotplug_notifier = {
139 .notifier_call = powernv_cpuidle_add_cpu_notifier,
140};
141
142/*
143 * powernv_cpuidle_driver_init()
144 */
145static int powernv_cpuidle_driver_init(void)
146{
147 int idle_state;
148 struct cpuidle_driver *drv = &powernv_idle_driver;
149
150 drv->state_count = 0;
151
152 for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
153 /* Is the state not enabled? */
154 if (cpuidle_state_table[idle_state].enter == NULL)
155 continue;
156
157 drv->states[drv->state_count] = /* structure copy */
158 cpuidle_state_table[idle_state];
159
160 drv->state_count += 1;
161 }
162
163 return 0;
164}
165
Preeti U Murthy08888392014-02-26 05:39:20 +0530166static int powernv_add_idle_states(void)
167{
168 struct device_node *power_mgt;
Preeti U Murthy08888392014-02-26 05:39:20 +0530169 int nr_idle_states = 1; /* Snooze */
170 int dt_idle_states;
Preeti U Murthy70734a72015-02-18 23:24:53 -0600171 u32 *latency_ns, *residency_ns, *flags;
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100172 int i, rc;
Preeti U Murthy08888392014-02-26 05:39:20 +0530173
174 /* Currently we have snooze statically defined */
175
176 power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
177 if (!power_mgt) {
178 pr_warn("opal: PowerMgmt Node not found\n");
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100179 goto out;
Preeti U Murthy08888392014-02-26 05:39:20 +0530180 }
181
Preeti U Murthy70734a72015-02-18 23:24:53 -0600182 /* Read values of any property to determine the num of idle states */
183 dt_idle_states = of_property_count_u32_elems(power_mgt, "ibm,cpu-idle-state-flags");
184 if (dt_idle_states < 0) {
185 pr_warn("cpuidle-powernv: no idle states found in the DT\n");
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100186 goto out;
Preeti U. Murthy74aa51b2014-10-14 13:23:00 +0530187 }
188
Preeti U Murthy70734a72015-02-18 23:24:53 -0600189 flags = kzalloc(sizeof(*flags) * dt_idle_states, GFP_KERNEL);
190 if (of_property_read_u32_array(power_mgt,
191 "ibm,cpu-idle-state-flags", flags, dt_idle_states)) {
192 pr_warn("cpuidle-powernv : missing ibm,cpu-idle-state-flags in DT\n");
193 goto out_free_flags;
194 }
Preeti U Murthy08888392014-02-26 05:39:20 +0530195
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100196 latency_ns = kzalloc(sizeof(*latency_ns) * dt_idle_states, GFP_KERNEL);
197 rc = of_property_read_u32_array(power_mgt,
198 "ibm,cpu-idle-state-latencies-ns", latency_ns, dt_idle_states);
199 if (rc) {
200 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-latencies-ns in DT\n");
201 goto out_free_latency;
202 }
203
204 residency_ns = kzalloc(sizeof(*residency_ns) * dt_idle_states, GFP_KERNEL);
205 rc = of_property_read_u32_array(power_mgt,
206 "ibm,cpu-idle-state-residency-ns", residency_ns, dt_idle_states);
207
Preeti U Murthy08888392014-02-26 05:39:20 +0530208 for (i = 0; i < dt_idle_states; i++) {
209
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100210 /*
211 * Cpuidle accepts exit_latency and target_residency in us.
212 * Use default target_residency values if f/w does not expose it.
Preeti U. Murthy74aa51b2014-10-14 13:23:00 +0530213 */
Preeti U Murthy70734a72015-02-18 23:24:53 -0600214 if (flags[i] & OPAL_PM_NAP_ENABLED) {
Preeti U Murthy08888392014-02-26 05:39:20 +0530215 /* Add NAP state */
216 strcpy(powernv_states[nr_idle_states].name, "Nap");
217 strcpy(powernv_states[nr_idle_states].desc, "Nap");
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100218 powernv_states[nr_idle_states].flags = 0;
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100219 powernv_states[nr_idle_states].target_residency = 100;
Preeti U Murthy08888392014-02-26 05:39:20 +0530220 powernv_states[nr_idle_states].enter = &nap_loop;
preeticc5a2f72015-06-24 01:48:01 -0500221 }
222
223 /*
224 * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come
225 * within this config dependency check.
226 */
227#ifdef CONFIG_TICK_ONESHOT
228 if (flags[i] & OPAL_PM_SLEEP_ENABLED ||
Preeti U Murthy70734a72015-02-18 23:24:53 -0600229 flags[i] & OPAL_PM_SLEEP_ENABLED_ER1) {
Preeti U Murthy08888392014-02-26 05:39:20 +0530230 /* Add FASTSLEEP state */
231 strcpy(powernv_states[nr_idle_states].name, "FastSleep");
232 strcpy(powernv_states[nr_idle_states].desc, "FastSleep");
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100233 powernv_states[nr_idle_states].flags = CPUIDLE_FLAG_TIMER_STOP;
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100234 powernv_states[nr_idle_states].target_residency = 300000;
Preeti U Murthy08888392014-02-26 05:39:20 +0530235 powernv_states[nr_idle_states].enter = &fastsleep_loop;
Preeti U Murthy08888392014-02-26 05:39:20 +0530236 }
preeticc5a2f72015-06-24 01:48:01 -0500237#endif
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100238 powernv_states[nr_idle_states].exit_latency =
239 ((unsigned int)latency_ns[i]) / 1000;
240
241 if (!rc) {
242 powernv_states[nr_idle_states].target_residency =
243 ((unsigned int)residency_ns[i]) / 1000;
244 }
245
246 nr_idle_states++;
Preeti U Murthy08888392014-02-26 05:39:20 +0530247 }
248
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100249 kfree(residency_ns);
250out_free_latency:
251 kfree(latency_ns);
Preeti U Murthy70734a72015-02-18 23:24:53 -0600252out_free_flags:
253 kfree(flags);
Preeti U Murthy92c83ff52015-02-18 06:34:17 +0100254out:
Preeti U Murthy08888392014-02-26 05:39:20 +0530255 return nr_idle_states;
256}
257
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530258/*
259 * powernv_idle_probe()
260 * Choose state table for shared versus dedicated partition
261 */
262static int powernv_idle_probe(void)
263{
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530264 if (cpuidle_disable != IDLE_NO_OVERRIDE)
265 return -ENODEV;
266
Stewart Smithe4d54f72015-12-09 17:18:20 +1100267 if (firmware_has_feature(FW_FEATURE_OPAL)) {
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530268 cpuidle_state_table = powernv_states;
Preeti U Murthy08888392014-02-26 05:39:20 +0530269 /* Device tree can indicate more idle states */
270 max_idle_state = powernv_add_idle_states();
Shilpasri G Bhat78eaa102015-06-18 16:53:11 +0530271 if (max_idle_state > 1) {
272 snooze_timeout_en = true;
273 snooze_timeout = powernv_states[1].target_residency *
274 tb_ticks_per_usec;
275 }
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530276 } else
277 return -ENODEV;
278
279 return 0;
280}
281
282static int __init powernv_processor_idle_init(void)
283{
284 int retval;
285
286 retval = powernv_idle_probe();
287 if (retval)
288 return retval;
289
290 powernv_cpuidle_driver_init();
291 retval = cpuidle_register(&powernv_idle_driver, NULL);
292 if (retval) {
293 printk(KERN_DEBUG "Registration of powernv driver failed.\n");
294 return retval;
295 }
296
297 register_cpu_notifier(&setup_hotplug_notifier);
298 printk(KERN_DEBUG "powernv_idle_driver registered\n");
299 return 0;
300}
301
302device_initcall(powernv_processor_idle_init);