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>
19 #include <asm/runlatch.h>
21 /* Flags and constants used in PowerNV platform */
23 #define MAX_POWERNV_IDLE_STATES 8
24 #define IDLE_USE_INST_NAP 0x00010000 /* Use nap instruction */
25 #define IDLE_USE_INST_SLEEP 0x00020000 /* Use sleep instruction */
27 struct cpuidle_driver powernv_idle_driver
= {
28 .name
= "powernv_idle",
32 static int max_idle_state
;
33 static struct cpuidle_state
*cpuidle_state_table
;
35 static int snooze_loop(struct cpuidle_device
*dev
,
36 struct cpuidle_driver
*drv
,
40 set_thread_flag(TIF_POLLING_NRFLAG
);
43 while (!need_resched()) {
50 clear_thread_flag(TIF_POLLING_NRFLAG
);
55 static int nap_loop(struct cpuidle_device
*dev
,
56 struct cpuidle_driver
*drv
,
65 static int fastsleep_loop(struct cpuidle_device
*dev
,
66 struct cpuidle_driver
*drv
,
69 unsigned long old_lpcr
= mfspr(SPRN_LPCR
);
70 unsigned long new_lpcr
;
72 if (unlikely(system_state
< SYSTEM_RUNNING
))
76 /* Do not exit powersave upon decrementer as we've setup the timer
79 new_lpcr
&= ~LPCR_PECE1
;
81 mtspr(SPRN_LPCR
, new_lpcr
);
84 mtspr(SPRN_LPCR
, old_lpcr
);
90 * States for dedicated partition case.
92 static struct cpuidle_state powernv_states
[MAX_POWERNV_IDLE_STATES
] = {
96 .flags
= CPUIDLE_FLAG_TIME_VALID
,
98 .target_residency
= 0,
99 .enter
= &snooze_loop
},
102 static int powernv_cpuidle_add_cpu_notifier(struct notifier_block
*n
,
103 unsigned long action
, void *hcpu
)
105 int hotcpu
= (unsigned long)hcpu
;
106 struct cpuidle_device
*dev
=
107 per_cpu(cpuidle_devices
, hotcpu
);
109 if (dev
&& cpuidle_get_driver()) {
112 case CPU_ONLINE_FROZEN
:
113 cpuidle_pause_and_lock();
114 cpuidle_enable_device(dev
);
115 cpuidle_resume_and_unlock();
119 case CPU_DEAD_FROZEN
:
120 cpuidle_pause_and_lock();
121 cpuidle_disable_device(dev
);
122 cpuidle_resume_and_unlock();
132 static struct notifier_block setup_hotplug_notifier
= {
133 .notifier_call
= powernv_cpuidle_add_cpu_notifier
,
137 * powernv_cpuidle_driver_init()
139 static int powernv_cpuidle_driver_init(void)
142 struct cpuidle_driver
*drv
= &powernv_idle_driver
;
144 drv
->state_count
= 0;
146 for (idle_state
= 0; idle_state
< max_idle_state
; ++idle_state
) {
147 /* Is the state not enabled? */
148 if (cpuidle_state_table
[idle_state
].enter
== NULL
)
151 drv
->states
[drv
->state_count
] = /* structure copy */
152 cpuidle_state_table
[idle_state
];
154 drv
->state_count
+= 1;
160 static int powernv_add_idle_states(void)
162 struct device_node
*power_mgt
;
163 int nr_idle_states
= 1; /* Snooze */
165 const __be32
*idle_state_flags
;
166 const __be32
*idle_state_latency
;
167 u32 len_flags
, flags
, latency_ns
;
170 /* Currently we have snooze statically defined */
172 power_mgt
= of_find_node_by_path("/ibm,opal/power-mgt");
174 pr_warn("opal: PowerMgmt Node not found\n");
175 return nr_idle_states
;
178 idle_state_flags
= of_get_property(power_mgt
, "ibm,cpu-idle-state-flags", &len_flags
);
179 if (!idle_state_flags
) {
180 pr_warn("DT-PowerMgmt: missing ibm,cpu-idle-state-flags\n");
181 return nr_idle_states
;
184 idle_state_latency
= of_get_property(power_mgt
,
185 "ibm,cpu-idle-state-latencies-ns", NULL
);
186 if (!idle_state_latency
) {
187 pr_warn("DT-PowerMgmt: missing ibm,cpu-idle-state-latencies-ns\n");
188 return nr_idle_states
;
191 dt_idle_states
= len_flags
/ sizeof(u32
);
193 for (i
= 0; i
< dt_idle_states
; i
++) {
195 flags
= be32_to_cpu(idle_state_flags
[i
]);
197 /* Cpuidle accepts exit_latency in us and we estimate
198 * target residency to be 10x exit_latency
200 latency_ns
= be32_to_cpu(idle_state_latency
[i
]);
201 if (flags
& IDLE_USE_INST_NAP
) {
203 strcpy(powernv_states
[nr_idle_states
].name
, "Nap");
204 strcpy(powernv_states
[nr_idle_states
].desc
, "Nap");
205 powernv_states
[nr_idle_states
].flags
= CPUIDLE_FLAG_TIME_VALID
;
206 powernv_states
[nr_idle_states
].exit_latency
=
207 ((unsigned int)latency_ns
) / 1000;
208 powernv_states
[nr_idle_states
].target_residency
=
209 ((unsigned int)latency_ns
/ 100);
210 powernv_states
[nr_idle_states
].enter
= &nap_loop
;
214 if (flags
& IDLE_USE_INST_SLEEP
) {
215 /* Add FASTSLEEP state */
216 strcpy(powernv_states
[nr_idle_states
].name
, "FastSleep");
217 strcpy(powernv_states
[nr_idle_states
].desc
, "FastSleep");
218 powernv_states
[nr_idle_states
].flags
=
219 CPUIDLE_FLAG_TIME_VALID
| CPUIDLE_FLAG_TIMER_STOP
;
220 powernv_states
[nr_idle_states
].exit_latency
=
221 ((unsigned int)latency_ns
) / 1000;
222 powernv_states
[nr_idle_states
].target_residency
=
223 ((unsigned int)latency_ns
/ 100);
224 powernv_states
[nr_idle_states
].enter
= &fastsleep_loop
;
229 return nr_idle_states
;
233 * powernv_idle_probe()
234 * Choose state table for shared versus dedicated partition
236 static int powernv_idle_probe(void)
238 if (cpuidle_disable
!= IDLE_NO_OVERRIDE
)
241 if (firmware_has_feature(FW_FEATURE_OPALv3
)) {
242 cpuidle_state_table
= powernv_states
;
243 /* Device tree can indicate more idle states */
244 max_idle_state
= powernv_add_idle_states();
251 static int __init
powernv_processor_idle_init(void)
255 retval
= powernv_idle_probe();
259 powernv_cpuidle_driver_init();
260 retval
= cpuidle_register(&powernv_idle_driver
, NULL
);
262 printk(KERN_DEBUG
"Registration of powernv driver failed.\n");
266 register_cpu_notifier(&setup_hotplug_notifier
);
267 printk(KERN_DEBUG
"powernv_idle_driver registered\n");
271 device_initcall(powernv_processor_idle_init
);