bpf: fix branch pruning logic
[linux/fpc-iii.git] / drivers / cpuidle / cpuidle-powernv.c
blobd5c5a476360fc88d4663483b4731335a0aabbcee
1 /*
2 * cpuidle-powernv - idle state cpuidle driver.
3 * Adapted from drivers/cpuidle/cpuidle-pseries
5 */
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>
15 #include <linux/of.h>
16 #include <linux/slab.h>
18 #include <asm/machdep.h>
19 #include <asm/firmware.h>
20 #include <asm/opal.h>
21 #include <asm/runlatch.h>
23 #define MAX_POWERNV_IDLE_STATES 8
25 struct cpuidle_driver powernv_idle_driver = {
26 .name = "powernv_idle",
27 .owner = THIS_MODULE,
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,
37 int index)
39 u64 snooze_exit_time;
41 local_irq_enable();
42 set_thread_flag(TIF_POLLING_NRFLAG);
44 snooze_exit_time = get_tb() + snooze_timeout;
45 ppc64_runlatch_off();
46 while (!need_resched()) {
47 HMT_low();
48 HMT_very_low();
49 if (snooze_timeout_en && get_tb() > snooze_exit_time)
50 break;
53 HMT_medium();
54 ppc64_runlatch_on();
55 clear_thread_flag(TIF_POLLING_NRFLAG);
56 smp_mb();
57 return index;
60 static int nap_loop(struct cpuidle_device *dev,
61 struct cpuidle_driver *drv,
62 int index)
64 ppc64_runlatch_off();
65 power7_idle();
66 ppc64_runlatch_on();
67 return index;
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,
74 int index)
76 unsigned long old_lpcr = mfspr(SPRN_LPCR);
77 unsigned long new_lpcr;
79 if (unlikely(system_state < SYSTEM_RUNNING))
80 return index;
82 new_lpcr = old_lpcr;
83 /* Do not exit powersave upon decrementer as we've setup the timer
84 * offload.
86 new_lpcr &= ~LPCR_PECE1;
88 mtspr(SPRN_LPCR, new_lpcr);
89 power7_sleep();
91 mtspr(SPRN_LPCR, old_lpcr);
93 return index;
95 #endif
97 * States for dedicated partition case.
99 static struct cpuidle_state powernv_states[MAX_POWERNV_IDLE_STATES] = {
100 { /* Snooze */
101 .name = "snooze",
102 .desc = "snooze",
103 .exit_latency = 0,
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()) {
116 switch (action) {
117 case CPU_ONLINE:
118 case CPU_ONLINE_FROZEN:
119 cpuidle_pause_and_lock();
120 cpuidle_enable_device(dev);
121 cpuidle_resume_and_unlock();
122 break;
124 case CPU_DEAD:
125 case CPU_DEAD_FROZEN:
126 cpuidle_pause_and_lock();
127 cpuidle_disable_device(dev);
128 cpuidle_resume_and_unlock();
129 break;
131 default:
132 return NOTIFY_DONE;
135 return NOTIFY_OK;
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)
147 int idle_state;
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)
155 continue;
157 drv->states[drv->state_count] = /* structure copy */
158 cpuidle_state_table[idle_state];
160 drv->state_count += 1;
164 * On the PowerNV platform cpu_present may be less than cpu_possible in
165 * cases when firmware detects the CPU, but it is not available to the
166 * OS. If CONFIG_HOTPLUG_CPU=n, then such CPUs are not hotplugable at
167 * run time and hence cpu_devices are not created for those CPUs by the
168 * generic topology_init().
170 * drv->cpumask defaults to cpu_possible_mask in
171 * __cpuidle_driver_init(). This breaks cpuidle on PowerNV where
172 * cpu_devices are not created for CPUs in cpu_possible_mask that
173 * cannot be hot-added later at run time.
175 * Trying cpuidle_register_device() on a CPU without a cpu_device is
176 * incorrect, so pass a correct CPU mask to the generic cpuidle driver.
179 drv->cpumask = (struct cpumask *)cpu_present_mask;
181 return 0;
184 static int powernv_add_idle_states(void)
186 struct device_node *power_mgt;
187 int nr_idle_states = 1; /* Snooze */
188 int dt_idle_states;
189 u32 *latency_ns, *residency_ns, *flags;
190 int i, rc;
192 /* Currently we have snooze statically defined */
194 power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
195 if (!power_mgt) {
196 pr_warn("opal: PowerMgmt Node not found\n");
197 goto out;
200 /* Read values of any property to determine the num of idle states */
201 dt_idle_states = of_property_count_u32_elems(power_mgt, "ibm,cpu-idle-state-flags");
202 if (dt_idle_states < 0) {
203 pr_warn("cpuidle-powernv: no idle states found in the DT\n");
204 goto out;
207 flags = kzalloc(sizeof(*flags) * dt_idle_states, GFP_KERNEL);
208 if (of_property_read_u32_array(power_mgt,
209 "ibm,cpu-idle-state-flags", flags, dt_idle_states)) {
210 pr_warn("cpuidle-powernv : missing ibm,cpu-idle-state-flags in DT\n");
211 goto out_free_flags;
214 latency_ns = kzalloc(sizeof(*latency_ns) * dt_idle_states, GFP_KERNEL);
215 rc = of_property_read_u32_array(power_mgt,
216 "ibm,cpu-idle-state-latencies-ns", latency_ns, dt_idle_states);
217 if (rc) {
218 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-latencies-ns in DT\n");
219 goto out_free_latency;
222 residency_ns = kzalloc(sizeof(*residency_ns) * dt_idle_states, GFP_KERNEL);
223 rc = of_property_read_u32_array(power_mgt,
224 "ibm,cpu-idle-state-residency-ns", residency_ns, dt_idle_states);
226 for (i = 0; i < dt_idle_states; i++) {
229 * Cpuidle accepts exit_latency and target_residency in us.
230 * Use default target_residency values if f/w does not expose it.
232 if (flags[i] & OPAL_PM_NAP_ENABLED) {
233 /* Add NAP state */
234 strcpy(powernv_states[nr_idle_states].name, "Nap");
235 strcpy(powernv_states[nr_idle_states].desc, "Nap");
236 powernv_states[nr_idle_states].flags = 0;
237 powernv_states[nr_idle_states].target_residency = 100;
238 powernv_states[nr_idle_states].enter = &nap_loop;
242 * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come
243 * within this config dependency check.
245 #ifdef CONFIG_TICK_ONESHOT
246 if (flags[i] & OPAL_PM_SLEEP_ENABLED ||
247 flags[i] & OPAL_PM_SLEEP_ENABLED_ER1) {
248 /* Add FASTSLEEP state */
249 strcpy(powernv_states[nr_idle_states].name, "FastSleep");
250 strcpy(powernv_states[nr_idle_states].desc, "FastSleep");
251 powernv_states[nr_idle_states].flags = CPUIDLE_FLAG_TIMER_STOP;
252 powernv_states[nr_idle_states].target_residency = 300000;
253 powernv_states[nr_idle_states].enter = &fastsleep_loop;
255 #endif
256 powernv_states[nr_idle_states].exit_latency =
257 ((unsigned int)latency_ns[i]) / 1000;
259 if (!rc) {
260 powernv_states[nr_idle_states].target_residency =
261 ((unsigned int)residency_ns[i]) / 1000;
264 nr_idle_states++;
267 kfree(residency_ns);
268 out_free_latency:
269 kfree(latency_ns);
270 out_free_flags:
271 kfree(flags);
272 out:
273 return nr_idle_states;
277 * powernv_idle_probe()
278 * Choose state table for shared versus dedicated partition
280 static int powernv_idle_probe(void)
282 if (cpuidle_disable != IDLE_NO_OVERRIDE)
283 return -ENODEV;
285 if (firmware_has_feature(FW_FEATURE_OPALv3)) {
286 cpuidle_state_table = powernv_states;
287 /* Device tree can indicate more idle states */
288 max_idle_state = powernv_add_idle_states();
289 if (max_idle_state > 1) {
290 snooze_timeout_en = true;
291 snooze_timeout = powernv_states[1].target_residency *
292 tb_ticks_per_usec;
294 } else
295 return -ENODEV;
297 return 0;
300 static int __init powernv_processor_idle_init(void)
302 int retval;
304 retval = powernv_idle_probe();
305 if (retval)
306 return retval;
308 powernv_cpuidle_driver_init();
309 retval = cpuidle_register(&powernv_idle_driver, NULL);
310 if (retval) {
311 printk(KERN_DEBUG "Registration of powernv driver failed.\n");
312 return retval;
315 register_cpu_notifier(&setup_hotplug_notifier);
316 printk(KERN_DEBUG "powernv_idle_driver registered\n");
317 return 0;
320 device_initcall(powernv_processor_idle_init);