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 POWERNV_THRESHOLD_LATENCY_NS 200000
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;
33 static u64 stop_psscr_table[CPUIDLE_STATE_MAX];
35 static u64 default_snooze_timeout;
36 static bool snooze_timeout_en;
38 static u64 get_snooze_timeout(struct cpuidle_device *dev,
39 struct cpuidle_driver *drv,
44 if (unlikely(!snooze_timeout_en))
45 return default_snooze_timeout;
47 for (i = index + 1; i < drv->state_count; i++) {
48 struct cpuidle_state *s = &drv->states[i];
49 struct cpuidle_state_usage *su = &dev->states_usage[i];
51 if (s->disabled || su->disable)
54 return s->target_residency * tb_ticks_per_usec;
57 return default_snooze_timeout;
60 static int snooze_loop(struct cpuidle_device *dev,
61 struct cpuidle_driver *drv,
67 set_thread_flag(TIF_POLLING_NRFLAG);
69 snooze_exit_time = get_tb() + get_snooze_timeout(dev, drv, index);
71 while (!need_resched()) {
74 if (snooze_timeout_en && get_tb() > snooze_exit_time)
80 clear_thread_flag(TIF_POLLING_NRFLAG);
85 static int nap_loop(struct cpuidle_device *dev,
86 struct cpuidle_driver *drv,
95 /* Register for fastsleep only in oneshot mode of broadcast */
96 #ifdef CONFIG_TICK_ONESHOT
97 static int fastsleep_loop(struct cpuidle_device *dev,
98 struct cpuidle_driver *drv,
101 unsigned long old_lpcr = mfspr(SPRN_LPCR);
102 unsigned long new_lpcr;
104 if (unlikely(system_state < SYSTEM_RUNNING))
108 /* Do not exit powersave upon decrementer as we've setup the timer
111 new_lpcr &= ~LPCR_PECE1;
113 mtspr(SPRN_LPCR, new_lpcr);
116 mtspr(SPRN_LPCR, old_lpcr);
122 static int stop_loop(struct cpuidle_device *dev,
123 struct cpuidle_driver *drv,
126 ppc64_runlatch_off();
127 power9_idle_stop(stop_psscr_table[index]);
133 * States for dedicated partition case.
135 static struct cpuidle_state powernv_states[CPUIDLE_STATE_MAX] = {
140 .target_residency = 0,
141 .enter = snooze_loop },
144 static int powernv_cpuidle_cpu_online(unsigned int cpu)
146 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
148 if (dev && cpuidle_get_driver()) {
149 cpuidle_pause_and_lock();
150 cpuidle_enable_device(dev);
151 cpuidle_resume_and_unlock();
156 static int powernv_cpuidle_cpu_dead(unsigned int cpu)
158 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
160 if (dev && cpuidle_get_driver()) {
161 cpuidle_pause_and_lock();
162 cpuidle_disable_device(dev);
163 cpuidle_resume_and_unlock();
169 * powernv_cpuidle_driver_init()
171 static int powernv_cpuidle_driver_init(void)
174 struct cpuidle_driver *drv = &powernv_idle_driver;
176 drv->state_count = 0;
178 for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
179 /* Is the state not enabled? */
180 if (cpuidle_state_table[idle_state].enter == NULL)
183 drv->states[drv->state_count] = /* structure copy */
184 cpuidle_state_table[idle_state];
186 drv->state_count += 1;
190 * On the PowerNV platform cpu_present may be less than cpu_possible in
191 * cases when firmware detects the CPU, but it is not available to the
192 * OS. If CONFIG_HOTPLUG_CPU=n, then such CPUs are not hotplugable at
193 * run time and hence cpu_devices are not created for those CPUs by the
194 * generic topology_init().
196 * drv->cpumask defaults to cpu_possible_mask in
197 * __cpuidle_driver_init(). This breaks cpuidle on PowerNV where
198 * cpu_devices are not created for CPUs in cpu_possible_mask that
199 * cannot be hot-added later at run time.
201 * Trying cpuidle_register_device() on a CPU without a cpu_device is
202 * incorrect, so pass a correct CPU mask to the generic cpuidle driver.
205 drv->cpumask = (struct cpumask *)cpu_present_mask;
210 static int powernv_add_idle_states(void)
212 struct device_node *power_mgt;
213 int nr_idle_states = 1; /* Snooze */
215 u32 latency_ns[CPUIDLE_STATE_MAX];
216 u32 residency_ns[CPUIDLE_STATE_MAX];
217 u32 flags[CPUIDLE_STATE_MAX];
218 u64 psscr_val[CPUIDLE_STATE_MAX];
219 const char *names[CPUIDLE_STATE_MAX];
222 /* Currently we have snooze statically defined */
224 power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
226 pr_warn("opal: PowerMgmt Node not found\n");
230 /* Read values of any property to determine the num of idle states */
231 dt_idle_states = of_property_count_u32_elems(power_mgt, "ibm,cpu-idle-state-flags");
232 if (dt_idle_states < 0) {
233 pr_warn("cpuidle-powernv: no idle states found in the DT\n");
238 * Since snooze is used as first idle state, max idle states allowed is
239 * CPUIDLE_STATE_MAX -1
241 if (dt_idle_states > CPUIDLE_STATE_MAX - 1) {
242 pr_warn("cpuidle-powernv: discovered idle states more than allowed");
243 dt_idle_states = CPUIDLE_STATE_MAX - 1;
246 if (of_property_read_u32_array(power_mgt,
247 "ibm,cpu-idle-state-flags", flags, dt_idle_states)) {
248 pr_warn("cpuidle-powernv : missing ibm,cpu-idle-state-flags in DT\n");
252 if (of_property_read_u32_array(power_mgt,
253 "ibm,cpu-idle-state-latencies-ns", latency_ns,
255 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-latencies-ns in DT\n");
258 if (of_property_read_string_array(power_mgt,
259 "ibm,cpu-idle-state-names", names, dt_idle_states) < 0) {
260 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-names in DT\n");
265 * If the idle states use stop instruction, probe for psscr values
266 * which are necessary to specify required stop level.
268 if (flags[0] & (OPAL_PM_STOP_INST_FAST | OPAL_PM_STOP_INST_DEEP))
269 if (of_property_read_u64_array(power_mgt,
270 "ibm,cpu-idle-state-psscr", psscr_val, dt_idle_states)) {
271 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-states-psscr in DT\n");
275 rc = of_property_read_u32_array(power_mgt,
276 "ibm,cpu-idle-state-residency-ns", residency_ns, dt_idle_states);
278 for (i = 0; i < dt_idle_states; i++) {
280 * If an idle state has exit latency beyond
281 * POWERNV_THRESHOLD_LATENCY_NS then don't use it
284 if (latency_ns[i] > POWERNV_THRESHOLD_LATENCY_NS)
288 * Cpuidle accepts exit_latency and target_residency in us.
289 * Use default target_residency values if f/w does not expose it.
291 if (flags[i] & OPAL_PM_NAP_ENABLED) {
293 strcpy(powernv_states[nr_idle_states].name, "Nap");
294 strcpy(powernv_states[nr_idle_states].desc, "Nap");
295 powernv_states[nr_idle_states].flags = 0;
296 powernv_states[nr_idle_states].target_residency = 100;
297 powernv_states[nr_idle_states].enter = nap_loop;
298 } else if ((flags[i] & OPAL_PM_STOP_INST_FAST) &&
299 !(flags[i] & OPAL_PM_TIMEBASE_STOP)) {
300 strncpy(powernv_states[nr_idle_states].name,
301 names[i], CPUIDLE_NAME_LEN);
302 strncpy(powernv_states[nr_idle_states].desc,
303 names[i], CPUIDLE_NAME_LEN);
304 powernv_states[nr_idle_states].flags = 0;
306 powernv_states[nr_idle_states].enter = stop_loop;
307 stop_psscr_table[nr_idle_states] = psscr_val[i];
311 * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come
312 * within this config dependency check.
314 #ifdef CONFIG_TICK_ONESHOT
315 if (flags[i] & OPAL_PM_SLEEP_ENABLED ||
316 flags[i] & OPAL_PM_SLEEP_ENABLED_ER1) {
317 /* Add FASTSLEEP state */
318 strcpy(powernv_states[nr_idle_states].name, "FastSleep");
319 strcpy(powernv_states[nr_idle_states].desc, "FastSleep");
320 powernv_states[nr_idle_states].flags = CPUIDLE_FLAG_TIMER_STOP;
321 powernv_states[nr_idle_states].target_residency = 300000;
322 powernv_states[nr_idle_states].enter = fastsleep_loop;
323 } else if ((flags[i] & OPAL_PM_STOP_INST_DEEP) &&
324 (flags[i] & OPAL_PM_TIMEBASE_STOP)) {
325 strncpy(powernv_states[nr_idle_states].name,
326 names[i], CPUIDLE_NAME_LEN);
327 strncpy(powernv_states[nr_idle_states].desc,
328 names[i], CPUIDLE_NAME_LEN);
330 powernv_states[nr_idle_states].flags = CPUIDLE_FLAG_TIMER_STOP;
331 powernv_states[nr_idle_states].enter = stop_loop;
332 stop_psscr_table[nr_idle_states] = psscr_val[i];
335 powernv_states[nr_idle_states].exit_latency =
336 ((unsigned int)latency_ns[i]) / 1000;
339 powernv_states[nr_idle_states].target_residency =
340 ((unsigned int)residency_ns[i]) / 1000;
346 return nr_idle_states;
350 * powernv_idle_probe()
351 * Choose state table for shared versus dedicated partition
353 static int powernv_idle_probe(void)
355 if (cpuidle_disable != IDLE_NO_OVERRIDE)
358 if (firmware_has_feature(FW_FEATURE_OPAL)) {
359 cpuidle_state_table = powernv_states;
360 /* Device tree can indicate more idle states */
361 max_idle_state = powernv_add_idle_states();
362 default_snooze_timeout = TICK_USEC * tb_ticks_per_usec;
363 if (max_idle_state > 1)
364 snooze_timeout_en = true;
371 static int __init powernv_processor_idle_init(void)
375 retval = powernv_idle_probe();
379 powernv_cpuidle_driver_init();
380 retval = cpuidle_register(&powernv_idle_driver, NULL);
382 printk(KERN_DEBUG "Registration of powernv driver failed.\n");
386 retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
387 "cpuidle/powernv:online",
388 powernv_cpuidle_cpu_online, NULL);
390 retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD,
391 "cpuidle/powernv:dead", NULL,
392 powernv_cpuidle_cpu_dead);
394 printk(KERN_DEBUG "powernv_idle_driver registered\n");
398 device_initcall(powernv_processor_idle_init);