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 #define MAX_POWERNV_IDLE_STATES 8
25 struct cpuidle_driver powernv_idle_driver = {
26 .name = "powernv_idle",
30 static int max_idle_state;
31 static struct cpuidle_state *cpuidle_state_table;
32 static u64 snooze_timeout;
33 static bool snooze_timeout_en;
35 static int snooze_loop(struct cpuidle_device *dev,
36 struct cpuidle_driver *drv,
42 set_thread_flag(TIF_POLLING_NRFLAG);
44 snooze_exit_time = get_tb() + snooze_timeout;
46 while (!need_resched()) {
49 if (snooze_timeout_en && get_tb() > snooze_exit_time)
55 clear_thread_flag(TIF_POLLING_NRFLAG);
60 static int nap_loop(struct cpuidle_device *dev,
61 struct cpuidle_driver *drv,
70 /* Register for fastsleep only in oneshot mode of broadcast */
71 #ifdef CONFIG_TICK_ONESHOT
72 static int fastsleep_loop(struct cpuidle_device *dev,
73 struct cpuidle_driver *drv,
76 unsigned long old_lpcr = mfspr(SPRN_LPCR);
77 unsigned long new_lpcr;
79 if (unlikely(system_state < SYSTEM_RUNNING))
83 /* Do not exit powersave upon decrementer as we've setup the timer
86 new_lpcr &= ~LPCR_PECE1;
88 mtspr(SPRN_LPCR, new_lpcr);
91 mtspr(SPRN_LPCR, old_lpcr);
97 * States for dedicated partition case.
99 static struct cpuidle_state powernv_states[MAX_POWERNV_IDLE_STATES] = {
104 .target_residency = 0,
105 .enter = &snooze_loop },
108 static int powernv_cpuidle_add_cpu_notifier(struct notifier_block *n,
109 unsigned long action, void *hcpu)
111 int hotcpu = (unsigned long)hcpu;
112 struct cpuidle_device *dev =
113 per_cpu(cpuidle_devices, hotcpu);
115 if (dev && cpuidle_get_driver()) {
118 case CPU_ONLINE_FROZEN:
119 cpuidle_pause_and_lock();
120 cpuidle_enable_device(dev);
121 cpuidle_resume_and_unlock();
125 case CPU_DEAD_FROZEN:
126 cpuidle_pause_and_lock();
127 cpuidle_disable_device(dev);
128 cpuidle_resume_and_unlock();
138 static struct notifier_block setup_hotplug_notifier = {
139 .notifier_call = powernv_cpuidle_add_cpu_notifier,
143 * powernv_cpuidle_driver_init()
145 static int powernv_cpuidle_driver_init(void)
148 struct cpuidle_driver *drv = &powernv_idle_driver;
150 drv->state_count = 0;
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)
157 drv->states[drv->state_count] = /* structure copy */
158 cpuidle_state_table[idle_state];
160 drv->state_count += 1;
166 static int powernv_add_idle_states(void)
168 struct device_node *power_mgt;
169 int nr_idle_states = 1; /* Snooze */
171 u32 *latency_ns, *residency_ns, *flags;
174 /* Currently we have snooze statically defined */
176 power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
178 pr_warn("opal: PowerMgmt Node not found\n");
182 /* 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");
189 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");
196 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);
200 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-latencies-ns in DT\n");
201 goto out_free_latency;
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);
208 for (i = 0; i < dt_idle_states; i++) {
211 * Cpuidle accepts exit_latency and target_residency in us.
212 * Use default target_residency values if f/w does not expose it.
214 if (flags[i] & OPAL_PM_NAP_ENABLED) {
216 strcpy(powernv_states[nr_idle_states].name, "Nap");
217 strcpy(powernv_states[nr_idle_states].desc, "Nap");
218 powernv_states[nr_idle_states].flags = 0;
219 powernv_states[nr_idle_states].target_residency = 100;
220 powernv_states[nr_idle_states].enter = &nap_loop;
224 * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come
225 * within this config dependency check.
227 #ifdef CONFIG_TICK_ONESHOT
228 if (flags[i] & OPAL_PM_SLEEP_ENABLED ||
229 flags[i] & OPAL_PM_SLEEP_ENABLED_ER1) {
230 /* Add FASTSLEEP state */
231 strcpy(powernv_states[nr_idle_states].name, "FastSleep");
232 strcpy(powernv_states[nr_idle_states].desc, "FastSleep");
233 powernv_states[nr_idle_states].flags = CPUIDLE_FLAG_TIMER_STOP;
234 powernv_states[nr_idle_states].target_residency = 300000;
235 powernv_states[nr_idle_states].enter = &fastsleep_loop;
238 powernv_states[nr_idle_states].exit_latency =
239 ((unsigned int)latency_ns[i]) / 1000;
242 powernv_states[nr_idle_states].target_residency =
243 ((unsigned int)residency_ns[i]) / 1000;
255 return nr_idle_states;
259 * powernv_idle_probe()
260 * Choose state table for shared versus dedicated partition
262 static int powernv_idle_probe(void)
264 if (cpuidle_disable != IDLE_NO_OVERRIDE)
267 if (firmware_has_feature(FW_FEATURE_OPAL)) {
268 cpuidle_state_table = powernv_states;
269 /* Device tree can indicate more idle states */
270 max_idle_state = powernv_add_idle_states();
271 if (max_idle_state > 1) {
272 snooze_timeout_en = true;
273 snooze_timeout = powernv_states[1].target_residency *
282 static int __init powernv_processor_idle_init(void)
286 retval = powernv_idle_probe();
290 powernv_cpuidle_driver_init();
291 retval = cpuidle_register(&powernv_idle_driver, NULL);
293 printk(KERN_DEBUG "Registration of powernv driver failed.\n");
297 register_cpu_notifier(&setup_hotplug_notifier);
298 printk(KERN_DEBUG "powernv_idle_driver registered\n");
302 device_initcall(powernv_processor_idle_init);