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
] = {
93 .flags
= CPUIDLE_FLAG_TIME_VALID
,
95 .target_residency
= 0,
96 .enter
= &snooze_loop
},
99 static int powernv_cpuidle_add_cpu_notifier(struct notifier_block
*n
,
100 unsigned long action
, void *hcpu
)
102 int hotcpu
= (unsigned long)hcpu
;
103 struct cpuidle_device
*dev
=
104 per_cpu(cpuidle_devices
, hotcpu
);
106 if (dev
&& cpuidle_get_driver()) {
109 case CPU_ONLINE_FROZEN
:
110 cpuidle_pause_and_lock();
111 cpuidle_enable_device(dev
);
112 cpuidle_resume_and_unlock();
116 case CPU_DEAD_FROZEN
:
117 cpuidle_pause_and_lock();
118 cpuidle_disable_device(dev
);
119 cpuidle_resume_and_unlock();
129 static struct notifier_block setup_hotplug_notifier
= {
130 .notifier_call
= powernv_cpuidle_add_cpu_notifier
,
134 * powernv_cpuidle_driver_init()
136 static int powernv_cpuidle_driver_init(void)
139 struct cpuidle_driver
*drv
= &powernv_idle_driver
;
141 drv
->state_count
= 0;
143 for (idle_state
= 0; idle_state
< max_idle_state
; ++idle_state
) {
144 /* Is the state not enabled? */
145 if (cpuidle_state_table
[idle_state
].enter
== NULL
)
148 drv
->states
[drv
->state_count
] = /* structure copy */
149 cpuidle_state_table
[idle_state
];
151 drv
->state_count
+= 1;
157 static int powernv_add_idle_states(void)
159 struct device_node
*power_mgt
;
160 int nr_idle_states
= 1; /* Snooze */
162 const __be32
*idle_state_flags
;
163 const __be32
*idle_state_latency
;
164 u32 len_flags
, flags
, latency_ns
;
167 /* Currently we have snooze statically defined */
169 power_mgt
= of_find_node_by_path("/ibm,opal/power-mgt");
171 pr_warn("opal: PowerMgmt Node not found\n");
172 return nr_idle_states
;
175 idle_state_flags
= of_get_property(power_mgt
, "ibm,cpu-idle-state-flags", &len_flags
);
176 if (!idle_state_flags
) {
177 pr_warn("DT-PowerMgmt: missing ibm,cpu-idle-state-flags\n");
178 return nr_idle_states
;
181 idle_state_latency
= of_get_property(power_mgt
,
182 "ibm,cpu-idle-state-latencies-ns", NULL
);
183 if (!idle_state_latency
) {
184 pr_warn("DT-PowerMgmt: missing ibm,cpu-idle-state-latencies-ns\n");
185 return nr_idle_states
;
188 dt_idle_states
= len_flags
/ sizeof(u32
);
190 for (i
= 0; i
< dt_idle_states
; i
++) {
192 flags
= be32_to_cpu(idle_state_flags
[i
]);
194 /* Cpuidle accepts exit_latency in us and we estimate
195 * target residency to be 10x exit_latency
197 latency_ns
= be32_to_cpu(idle_state_latency
[i
]);
198 if (flags
& OPAL_PM_NAP_ENABLED
) {
200 strcpy(powernv_states
[nr_idle_states
].name
, "Nap");
201 strcpy(powernv_states
[nr_idle_states
].desc
, "Nap");
202 powernv_states
[nr_idle_states
].flags
= CPUIDLE_FLAG_TIME_VALID
;
203 powernv_states
[nr_idle_states
].exit_latency
=
204 ((unsigned int)latency_ns
) / 1000;
205 powernv_states
[nr_idle_states
].target_residency
=
206 ((unsigned int)latency_ns
/ 100);
207 powernv_states
[nr_idle_states
].enter
= &nap_loop
;
211 if (flags
& OPAL_PM_SLEEP_ENABLED
||
212 flags
& OPAL_PM_SLEEP_ENABLED_ER1
) {
213 /* Add FASTSLEEP state */
214 strcpy(powernv_states
[nr_idle_states
].name
, "FastSleep");
215 strcpy(powernv_states
[nr_idle_states
].desc
, "FastSleep");
216 powernv_states
[nr_idle_states
].flags
=
217 CPUIDLE_FLAG_TIME_VALID
| CPUIDLE_FLAG_TIMER_STOP
;
218 powernv_states
[nr_idle_states
].exit_latency
=
219 ((unsigned int)latency_ns
) / 1000;
220 powernv_states
[nr_idle_states
].target_residency
=
221 ((unsigned int)latency_ns
/ 100);
222 powernv_states
[nr_idle_states
].enter
= &fastsleep_loop
;
227 return nr_idle_states
;
231 * powernv_idle_probe()
232 * Choose state table for shared versus dedicated partition
234 static int powernv_idle_probe(void)
236 if (cpuidle_disable
!= IDLE_NO_OVERRIDE
)
239 if (firmware_has_feature(FW_FEATURE_OPALv3
)) {
240 cpuidle_state_table
= powernv_states
;
241 /* Device tree can indicate more idle states */
242 max_idle_state
= powernv_add_idle_states();
249 static int __init
powernv_processor_idle_init(void)
253 retval
= powernv_idle_probe();
257 powernv_cpuidle_driver_init();
258 retval
= cpuidle_register(&powernv_idle_driver
, NULL
);
260 printk(KERN_DEBUG
"Registration of powernv driver failed.\n");
264 register_cpu_notifier(&setup_hotplug_notifier
);
265 printk(KERN_DEBUG
"powernv_idle_driver registered\n");
269 device_initcall(powernv_processor_idle_init
);