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>
17 #include <asm/machdep.h>
18 #include <asm/firmware.h>
20 #include <asm/runlatch.h>
22 #define MAX_POWERNV_IDLE_STATES 8
24 struct cpuidle_driver powernv_idle_driver
= {
25 .name
= "powernv_idle",
29 static int max_idle_state
;
30 static struct cpuidle_state
*cpuidle_state_table
;
32 static int snooze_loop(struct cpuidle_device
*dev
,
33 struct cpuidle_driver
*drv
,
37 set_thread_flag(TIF_POLLING_NRFLAG
);
40 while (!need_resched()) {
47 clear_thread_flag(TIF_POLLING_NRFLAG
);
52 static int nap_loop(struct cpuidle_device
*dev
,
53 struct cpuidle_driver
*drv
,
62 static int fastsleep_loop(struct cpuidle_device
*dev
,
63 struct cpuidle_driver
*drv
,
66 unsigned long old_lpcr
= mfspr(SPRN_LPCR
);
67 unsigned long new_lpcr
;
69 if (unlikely(system_state
< SYSTEM_RUNNING
))
73 /* Do not exit powersave upon decrementer as we've setup the timer
76 new_lpcr
&= ~LPCR_PECE1
;
78 mtspr(SPRN_LPCR
, new_lpcr
);
81 mtspr(SPRN_LPCR
, old_lpcr
);
87 * States for dedicated partition case.
89 static struct cpuidle_state powernv_states
[MAX_POWERNV_IDLE_STATES
] = {
94 .target_residency
= 0,
95 .enter
= &snooze_loop
},
98 static int powernv_cpuidle_add_cpu_notifier(struct notifier_block
*n
,
99 unsigned long action
, void *hcpu
)
101 int hotcpu
= (unsigned long)hcpu
;
102 struct cpuidle_device
*dev
=
103 per_cpu(cpuidle_devices
, hotcpu
);
105 if (dev
&& cpuidle_get_driver()) {
108 case CPU_ONLINE_FROZEN
:
109 cpuidle_pause_and_lock();
110 cpuidle_enable_device(dev
);
111 cpuidle_resume_and_unlock();
115 case CPU_DEAD_FROZEN
:
116 cpuidle_pause_and_lock();
117 cpuidle_disable_device(dev
);
118 cpuidle_resume_and_unlock();
128 static struct notifier_block setup_hotplug_notifier
= {
129 .notifier_call
= powernv_cpuidle_add_cpu_notifier
,
133 * powernv_cpuidle_driver_init()
135 static int powernv_cpuidle_driver_init(void)
138 struct cpuidle_driver
*drv
= &powernv_idle_driver
;
140 drv
->state_count
= 0;
142 for (idle_state
= 0; idle_state
< max_idle_state
; ++idle_state
) {
143 /* Is the state not enabled? */
144 if (cpuidle_state_table
[idle_state
].enter
== NULL
)
147 drv
->states
[drv
->state_count
] = /* structure copy */
148 cpuidle_state_table
[idle_state
];
150 drv
->state_count
+= 1;
156 static int powernv_add_idle_states(void)
158 struct device_node
*power_mgt
;
159 int nr_idle_states
= 1; /* Snooze */
161 const __be32
*idle_state_flags
;
162 const __be32
*idle_state_latency
;
163 u32 len_flags
, flags
, latency_ns
;
166 /* Currently we have snooze statically defined */
168 power_mgt
= of_find_node_by_path("/ibm,opal/power-mgt");
170 pr_warn("opal: PowerMgmt Node not found\n");
171 return nr_idle_states
;
174 idle_state_flags
= of_get_property(power_mgt
, "ibm,cpu-idle-state-flags", &len_flags
);
175 if (!idle_state_flags
) {
176 pr_warn("DT-PowerMgmt: missing ibm,cpu-idle-state-flags\n");
177 return nr_idle_states
;
180 idle_state_latency
= of_get_property(power_mgt
,
181 "ibm,cpu-idle-state-latencies-ns", NULL
);
182 if (!idle_state_latency
) {
183 pr_warn("DT-PowerMgmt: missing ibm,cpu-idle-state-latencies-ns\n");
184 return nr_idle_states
;
187 dt_idle_states
= len_flags
/ sizeof(u32
);
189 for (i
= 0; i
< dt_idle_states
; i
++) {
191 flags
= be32_to_cpu(idle_state_flags
[i
]);
193 /* Cpuidle accepts exit_latency in us and we estimate
194 * target residency to be 10x exit_latency
196 latency_ns
= be32_to_cpu(idle_state_latency
[i
]);
197 if (flags
& OPAL_PM_NAP_ENABLED
) {
199 strcpy(powernv_states
[nr_idle_states
].name
, "Nap");
200 strcpy(powernv_states
[nr_idle_states
].desc
, "Nap");
201 powernv_states
[nr_idle_states
].flags
= 0;
202 powernv_states
[nr_idle_states
].exit_latency
=
203 ((unsigned int)latency_ns
) / 1000;
204 powernv_states
[nr_idle_states
].target_residency
=
205 ((unsigned int)latency_ns
/ 100);
206 powernv_states
[nr_idle_states
].enter
= &nap_loop
;
210 if (flags
& OPAL_PM_SLEEP_ENABLED
||
211 flags
& OPAL_PM_SLEEP_ENABLED_ER1
) {
212 /* Add FASTSLEEP state */
213 strcpy(powernv_states
[nr_idle_states
].name
, "FastSleep");
214 strcpy(powernv_states
[nr_idle_states
].desc
, "FastSleep");
215 powernv_states
[nr_idle_states
].flags
= CPUIDLE_FLAG_TIMER_STOP
;
216 powernv_states
[nr_idle_states
].exit_latency
=
217 ((unsigned int)latency_ns
) / 1000;
218 powernv_states
[nr_idle_states
].target_residency
=
219 ((unsigned int)latency_ns
/ 100);
220 powernv_states
[nr_idle_states
].enter
= &fastsleep_loop
;
225 return nr_idle_states
;
229 * powernv_idle_probe()
230 * Choose state table for shared versus dedicated partition
232 static int powernv_idle_probe(void)
234 if (cpuidle_disable
!= IDLE_NO_OVERRIDE
)
237 if (firmware_has_feature(FW_FEATURE_OPALv3
)) {
238 cpuidle_state_table
= powernv_states
;
239 /* Device tree can indicate more idle states */
240 max_idle_state
= powernv_add_idle_states();
247 static int __init
powernv_processor_idle_init(void)
251 retval
= powernv_idle_probe();
255 powernv_cpuidle_driver_init();
256 retval
= cpuidle_register(&powernv_idle_driver
, NULL
);
258 printk(KERN_DEBUG
"Registration of powernv driver failed.\n");
262 register_cpu_notifier(&setup_hotplug_notifier
);
263 printk(KERN_DEBUG
"powernv_idle_driver registered\n");
267 device_initcall(powernv_processor_idle_init
);