2 * cpuidle-powernv - idle state cpuidle driver.
3 * Adapted from drivers/cpuidle/cpuidle-pseries
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>
14 #include <linux/clockchips.h>
16 #include <linux/slab.h>
18 #include <asm/machdep.h>
19 #include <asm/firmware.h>
21 #include <asm/runlatch.h>
23 struct cpuidle_driver powernv_idle_driver = {
24 .name = "powernv_idle",
28 static int max_idle_state;
29 static struct cpuidle_state *cpuidle_state_table;
30 static u64 snooze_timeout;
31 static bool snooze_timeout_en;
33 static int snooze_loop(struct cpuidle_device *dev,
34 struct cpuidle_driver *drv,
40 set_thread_flag(TIF_POLLING_NRFLAG);
42 snooze_exit_time = get_tb() + snooze_timeout;
44 while (!need_resched()) {
47 if (snooze_timeout_en && get_tb() > snooze_exit_time)
53 clear_thread_flag(TIF_POLLING_NRFLAG);
58 static int nap_loop(struct cpuidle_device *dev,
59 struct cpuidle_driver *drv,
68 /* Register for fastsleep only in oneshot mode of broadcast */
69 #ifdef CONFIG_TICK_ONESHOT
70 static int fastsleep_loop(struct cpuidle_device *dev,
71 struct cpuidle_driver *drv,
74 unsigned long old_lpcr = mfspr(SPRN_LPCR);
75 unsigned long new_lpcr;
77 if (unlikely(system_state < SYSTEM_RUNNING))
81 /* Do not exit powersave upon decrementer as we've setup the timer
84 new_lpcr &= ~LPCR_PECE1;
86 mtspr(SPRN_LPCR, new_lpcr);
89 mtspr(SPRN_LPCR, old_lpcr);
95 * States for dedicated partition case.
97 static struct cpuidle_state powernv_states[CPUIDLE_STATE_MAX] = {
102 .target_residency = 0,
103 .enter = &snooze_loop },
106 static int powernv_cpuidle_add_cpu_notifier(struct notifier_block *n,
107 unsigned long action, void *hcpu)
109 int hotcpu = (unsigned long)hcpu;
110 struct cpuidle_device *dev =
111 per_cpu(cpuidle_devices, hotcpu);
113 if (dev && cpuidle_get_driver()) {
116 case CPU_ONLINE_FROZEN:
117 cpuidle_pause_and_lock();
118 cpuidle_enable_device(dev);
119 cpuidle_resume_and_unlock();
123 case CPU_DEAD_FROZEN:
124 cpuidle_pause_and_lock();
125 cpuidle_disable_device(dev);
126 cpuidle_resume_and_unlock();
136 static struct notifier_block setup_hotplug_notifier = {
137 .notifier_call = powernv_cpuidle_add_cpu_notifier,
141 * powernv_cpuidle_driver_init()
143 static int powernv_cpuidle_driver_init(void)
146 struct cpuidle_driver *drv = &powernv_idle_driver;
148 drv->state_count = 0;
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)
155 drv->states[drv->state_count] = /* structure copy */
156 cpuidle_state_table[idle_state];
158 drv->state_count += 1;
164 static int powernv_add_idle_states(void)
166 struct device_node *power_mgt;
167 int nr_idle_states = 1; /* Snooze */
169 u32 *latency_ns, *residency_ns, *flags;
172 /* Currently we have snooze statically defined */
174 power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
176 pr_warn("opal: PowerMgmt Node not found\n");
180 /* 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");
187 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");
194 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);
198 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-latencies-ns in DT\n");
199 goto out_free_latency;
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);
206 for (i = 0; i < dt_idle_states; i++) {
209 * Cpuidle accepts exit_latency and target_residency in us.
210 * Use default target_residency values if f/w does not expose it.
212 if (flags[i] & OPAL_PM_NAP_ENABLED) {
214 strcpy(powernv_states[nr_idle_states].name, "Nap");
215 strcpy(powernv_states[nr_idle_states].desc, "Nap");
216 powernv_states[nr_idle_states].flags = 0;
217 powernv_states[nr_idle_states].target_residency = 100;
218 powernv_states[nr_idle_states].enter = &nap_loop;
222 * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come
223 * within this config dependency check.
225 #ifdef CONFIG_TICK_ONESHOT
226 if (flags[i] & OPAL_PM_SLEEP_ENABLED ||
227 flags[i] & OPAL_PM_SLEEP_ENABLED_ER1) {
228 /* Add FASTSLEEP state */
229 strcpy(powernv_states[nr_idle_states].name, "FastSleep");
230 strcpy(powernv_states[nr_idle_states].desc, "FastSleep");
231 powernv_states[nr_idle_states].flags = CPUIDLE_FLAG_TIMER_STOP;
232 powernv_states[nr_idle_states].target_residency = 300000;
233 powernv_states[nr_idle_states].enter = &fastsleep_loop;
236 powernv_states[nr_idle_states].exit_latency =
237 ((unsigned int)latency_ns[i]) / 1000;
240 powernv_states[nr_idle_states].target_residency =
241 ((unsigned int)residency_ns[i]) / 1000;
253 return nr_idle_states;
257 * powernv_idle_probe()
258 * Choose state table for shared versus dedicated partition
260 static int powernv_idle_probe(void)
262 if (cpuidle_disable != IDLE_NO_OVERRIDE)
265 if (firmware_has_feature(FW_FEATURE_OPAL)) {
266 cpuidle_state_table = powernv_states;
267 /* Device tree can indicate more idle states */
268 max_idle_state = powernv_add_idle_states();
269 if (max_idle_state > 1) {
270 snooze_timeout_en = true;
271 snooze_timeout = powernv_states[1].target_residency *
280 static int __init powernv_processor_idle_init(void)
284 retval = powernv_idle_probe();
288 powernv_cpuidle_driver_init();
289 retval = cpuidle_register(&powernv_idle_driver, NULL);
291 printk(KERN_DEBUG "Registration of powernv driver failed.\n");
295 register_cpu_notifier(&setup_hotplug_notifier);
296 printk(KERN_DEBUG "powernv_idle_driver registered\n");
300 device_initcall(powernv_processor_idle_init);