xref: /openbmc/linux/drivers/cpuidle/cpuidle-powernv.c (revision 4f139972b489f8bc2c821aa25ac65018d92af3f7)
1 /*
2  *  cpuidle-powernv - idle state cpuidle driver.
3  *  Adapted from drivers/cpuidle/cpuidle-pseries
4  *
5  */
6 
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>
17 
18 #include <asm/machdep.h>
19 #include <asm/firmware.h>
20 #include <asm/opal.h>
21 #include <asm/runlatch.h>
22 #include <asm/cpuidle.h>
23 
24 /*
25  * Expose only those Hardware idle states via the cpuidle framework
26  * that have latency value below POWERNV_THRESHOLD_LATENCY_NS.
27  */
28 #define POWERNV_THRESHOLD_LATENCY_NS 200000
29 
30 static struct cpuidle_driver powernv_idle_driver = {
31 	.name             = "powernv_idle",
32 	.owner            = THIS_MODULE,
33 };
34 
35 static int max_idle_state;
36 static struct cpuidle_state *cpuidle_state_table;
37 
38 struct stop_psscr_table {
39 	u64 val;
40 	u64 mask;
41 };
42 
43 static struct stop_psscr_table stop_psscr_table[CPUIDLE_STATE_MAX];
44 
45 static u64 snooze_timeout;
46 static bool snooze_timeout_en;
47 
48 static int snooze_loop(struct cpuidle_device *dev,
49 			struct cpuidle_driver *drv,
50 			int index)
51 {
52 	u64 snooze_exit_time;
53 
54 	local_irq_enable();
55 	set_thread_flag(TIF_POLLING_NRFLAG);
56 
57 	snooze_exit_time = get_tb() + snooze_timeout;
58 	ppc64_runlatch_off();
59 	while (!need_resched()) {
60 		HMT_low();
61 		HMT_very_low();
62 		if (snooze_timeout_en && get_tb() > snooze_exit_time)
63 			break;
64 	}
65 
66 	HMT_medium();
67 	ppc64_runlatch_on();
68 	clear_thread_flag(TIF_POLLING_NRFLAG);
69 	smp_mb();
70 	return index;
71 }
72 
73 static int nap_loop(struct cpuidle_device *dev,
74 			struct cpuidle_driver *drv,
75 			int index)
76 {
77 	ppc64_runlatch_off();
78 	power7_idle();
79 	ppc64_runlatch_on();
80 	return index;
81 }
82 
83 /* Register for fastsleep only in oneshot mode of broadcast */
84 #ifdef CONFIG_TICK_ONESHOT
85 static int fastsleep_loop(struct cpuidle_device *dev,
86 				struct cpuidle_driver *drv,
87 				int index)
88 {
89 	unsigned long old_lpcr = mfspr(SPRN_LPCR);
90 	unsigned long new_lpcr;
91 
92 	if (unlikely(system_state < SYSTEM_RUNNING))
93 		return index;
94 
95 	new_lpcr = old_lpcr;
96 	/* Do not exit powersave upon decrementer as we've setup the timer
97 	 * offload.
98 	 */
99 	new_lpcr &= ~LPCR_PECE1;
100 
101 	mtspr(SPRN_LPCR, new_lpcr);
102 	power7_sleep();
103 
104 	mtspr(SPRN_LPCR, old_lpcr);
105 
106 	return index;
107 }
108 #endif
109 
110 static int stop_loop(struct cpuidle_device *dev,
111 		     struct cpuidle_driver *drv,
112 		     int index)
113 {
114 	ppc64_runlatch_off();
115 	power9_idle_stop(stop_psscr_table[index].val,
116 			 stop_psscr_table[index].mask);
117 	ppc64_runlatch_on();
118 	return index;
119 }
120 
121 /*
122  * States for dedicated partition case.
123  */
124 static struct cpuidle_state powernv_states[CPUIDLE_STATE_MAX] = {
125 	{ /* Snooze */
126 		.name = "snooze",
127 		.desc = "snooze",
128 		.exit_latency = 0,
129 		.target_residency = 0,
130 		.enter = snooze_loop },
131 };
132 
133 static int powernv_cpuidle_cpu_online(unsigned int cpu)
134 {
135 	struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
136 
137 	if (dev && cpuidle_get_driver()) {
138 		cpuidle_pause_and_lock();
139 		cpuidle_enable_device(dev);
140 		cpuidle_resume_and_unlock();
141 	}
142 	return 0;
143 }
144 
145 static int powernv_cpuidle_cpu_dead(unsigned int cpu)
146 {
147 	struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
148 
149 	if (dev && cpuidle_get_driver()) {
150 		cpuidle_pause_and_lock();
151 		cpuidle_disable_device(dev);
152 		cpuidle_resume_and_unlock();
153 	}
154 	return 0;
155 }
156 
157 /*
158  * powernv_cpuidle_driver_init()
159  */
160 static int powernv_cpuidle_driver_init(void)
161 {
162 	int idle_state;
163 	struct cpuidle_driver *drv = &powernv_idle_driver;
164 
165 	drv->state_count = 0;
166 
167 	for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
168 		/* Is the state not enabled? */
169 		if (cpuidle_state_table[idle_state].enter == NULL)
170 			continue;
171 
172 		drv->states[drv->state_count] =	/* structure copy */
173 			cpuidle_state_table[idle_state];
174 
175 		drv->state_count += 1;
176 	}
177 
178 	/*
179 	 * On the PowerNV platform cpu_present may be less than cpu_possible in
180 	 * cases when firmware detects the CPU, but it is not available to the
181 	 * OS.  If CONFIG_HOTPLUG_CPU=n, then such CPUs are not hotplugable at
182 	 * run time and hence cpu_devices are not created for those CPUs by the
183 	 * generic topology_init().
184 	 *
185 	 * drv->cpumask defaults to cpu_possible_mask in
186 	 * __cpuidle_driver_init().  This breaks cpuidle on PowerNV where
187 	 * cpu_devices are not created for CPUs in cpu_possible_mask that
188 	 * cannot be hot-added later at run time.
189 	 *
190 	 * Trying cpuidle_register_device() on a CPU without a cpu_device is
191 	 * incorrect, so pass a correct CPU mask to the generic cpuidle driver.
192 	 */
193 
194 	drv->cpumask = (struct cpumask *)cpu_present_mask;
195 
196 	return 0;
197 }
198 
199 static inline void add_powernv_state(int index, const char *name,
200 				     unsigned int flags,
201 				     int (*idle_fn)(struct cpuidle_device *,
202 						    struct cpuidle_driver *,
203 						    int),
204 				     unsigned int target_residency,
205 				     unsigned int exit_latency,
206 				     u64 psscr_val, u64 psscr_mask)
207 {
208 	strlcpy(powernv_states[index].name, name, CPUIDLE_NAME_LEN);
209 	strlcpy(powernv_states[index].desc, name, CPUIDLE_NAME_LEN);
210 	powernv_states[index].flags = flags;
211 	powernv_states[index].target_residency = target_residency;
212 	powernv_states[index].exit_latency = exit_latency;
213 	powernv_states[index].enter = idle_fn;
214 	stop_psscr_table[index].val = psscr_val;
215 	stop_psscr_table[index].mask = psscr_mask;
216 }
217 
218 static int powernv_add_idle_states(void)
219 {
220 	struct device_node *power_mgt;
221 	int nr_idle_states = 1; /* Snooze */
222 	int dt_idle_states;
223 	u32 latency_ns[CPUIDLE_STATE_MAX];
224 	u32 residency_ns[CPUIDLE_STATE_MAX];
225 	u32 flags[CPUIDLE_STATE_MAX];
226 	u64 psscr_val[CPUIDLE_STATE_MAX];
227 	u64 psscr_mask[CPUIDLE_STATE_MAX];
228 	const char *names[CPUIDLE_STATE_MAX];
229 	u32 has_stop_states = 0;
230 	int i, rc;
231 
232 	/* Currently we have snooze statically defined */
233 
234 	power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
235 	if (!power_mgt) {
236 		pr_warn("opal: PowerMgmt Node not found\n");
237 		goto out;
238 	}
239 
240 	/* Read values of any property to determine the num of idle states */
241 	dt_idle_states = of_property_count_u32_elems(power_mgt, "ibm,cpu-idle-state-flags");
242 	if (dt_idle_states < 0) {
243 		pr_warn("cpuidle-powernv: no idle states found in the DT\n");
244 		goto out;
245 	}
246 
247 	/*
248 	 * Since snooze is used as first idle state, max idle states allowed is
249 	 * CPUIDLE_STATE_MAX -1
250 	 */
251 	if (dt_idle_states > CPUIDLE_STATE_MAX - 1) {
252 		pr_warn("cpuidle-powernv: discovered idle states more than allowed");
253 		dt_idle_states = CPUIDLE_STATE_MAX - 1;
254 	}
255 
256 	if (of_property_read_u32_array(power_mgt,
257 			"ibm,cpu-idle-state-flags", flags, dt_idle_states)) {
258 		pr_warn("cpuidle-powernv : missing ibm,cpu-idle-state-flags in DT\n");
259 		goto out;
260 	}
261 
262 	if (of_property_read_u32_array(power_mgt,
263 		"ibm,cpu-idle-state-latencies-ns", latency_ns,
264 		dt_idle_states)) {
265 		pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-latencies-ns in DT\n");
266 		goto out;
267 	}
268 	if (of_property_read_string_array(power_mgt,
269 		"ibm,cpu-idle-state-names", names, dt_idle_states) < 0) {
270 		pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-names in DT\n");
271 		goto out;
272 	}
273 
274 	/*
275 	 * If the idle states use stop instruction, probe for psscr values
276 	 * and psscr mask which are necessary to specify required stop level.
277 	 */
278 	has_stop_states = (flags[0] &
279 			   (OPAL_PM_STOP_INST_FAST | OPAL_PM_STOP_INST_DEEP));
280 	if (has_stop_states) {
281 		if (of_property_read_u64_array(power_mgt,
282 		    "ibm,cpu-idle-state-psscr", psscr_val, dt_idle_states)) {
283 			pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr in DT\n");
284 			goto out;
285 		}
286 
287 		if (of_property_read_u64_array(power_mgt,
288 					       "ibm,cpu-idle-state-psscr-mask",
289 						psscr_mask, dt_idle_states)) {
290 			pr_warn("cpuidle-powernv:Missing ibm,cpu-idle-state-psscr-mask in DT\n");
291 			goto out;
292 		}
293 	}
294 
295 	rc = of_property_read_u32_array(power_mgt,
296 		"ibm,cpu-idle-state-residency-ns", residency_ns, dt_idle_states);
297 
298 	for (i = 0; i < dt_idle_states; i++) {
299 		unsigned int exit_latency, target_residency;
300 		/*
301 		 * If an idle state has exit latency beyond
302 		 * POWERNV_THRESHOLD_LATENCY_NS then don't use it
303 		 * in cpu-idle.
304 		 */
305 		if (latency_ns[i] > POWERNV_THRESHOLD_LATENCY_NS)
306 			continue;
307 		/*
308 		 * Firmware passes residency and latency values in ns.
309 		 * cpuidle expects it in us.
310 		 */
311 		exit_latency = latency_ns[i] / 1000;
312 		if (!rc)
313 			target_residency = residency_ns[i] / 1000;
314 		else
315 			target_residency = 0;
316 
317 		if (has_stop_states) {
318 			int err = validate_psscr_val_mask(&psscr_val[i],
319 							  &psscr_mask[i],
320 							  flags[i]);
321 			if (err) {
322 				report_invalid_psscr_val(psscr_val[i], err);
323 				continue;
324 			}
325 		}
326 
327 		/*
328 		 * For nap and fastsleep, use default target_residency
329 		 * values if f/w does not expose it.
330 		 */
331 		if (flags[i] & OPAL_PM_NAP_ENABLED) {
332 			if (!rc)
333 				target_residency = 100;
334 			/* Add NAP state */
335 			add_powernv_state(nr_idle_states, "Nap",
336 					  CPUIDLE_FLAG_NONE, nap_loop,
337 					  target_residency, exit_latency, 0, 0);
338 		} else if ((flags[i] & OPAL_PM_STOP_INST_FAST) &&
339 				!(flags[i] & OPAL_PM_TIMEBASE_STOP)) {
340 			add_powernv_state(nr_idle_states, names[i],
341 					  CPUIDLE_FLAG_NONE, stop_loop,
342 					  target_residency, exit_latency,
343 					  psscr_val[i], psscr_mask[i]);
344 		}
345 
346 		/*
347 		 * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come
348 		 * within this config dependency check.
349 		 */
350 #ifdef CONFIG_TICK_ONESHOT
351 		if (flags[i] & OPAL_PM_SLEEP_ENABLED ||
352 			flags[i] & OPAL_PM_SLEEP_ENABLED_ER1) {
353 			if (!rc)
354 				target_residency = 300000;
355 			/* Add FASTSLEEP state */
356 			add_powernv_state(nr_idle_states, "FastSleep",
357 					  CPUIDLE_FLAG_TIMER_STOP,
358 					  fastsleep_loop,
359 					  target_residency, exit_latency, 0, 0);
360 		} else if ((flags[i] & OPAL_PM_STOP_INST_DEEP) &&
361 				(flags[i] & OPAL_PM_TIMEBASE_STOP)) {
362 			add_powernv_state(nr_idle_states, names[i],
363 					  CPUIDLE_FLAG_TIMER_STOP, stop_loop,
364 					  target_residency, exit_latency,
365 					  psscr_val[i], psscr_mask[i]);
366 		}
367 #endif
368 		nr_idle_states++;
369 	}
370 out:
371 	return nr_idle_states;
372 }
373 
374 /*
375  * powernv_idle_probe()
376  * Choose state table for shared versus dedicated partition
377  */
378 static int powernv_idle_probe(void)
379 {
380 	if (cpuidle_disable != IDLE_NO_OVERRIDE)
381 		return -ENODEV;
382 
383 	if (firmware_has_feature(FW_FEATURE_OPAL)) {
384 		cpuidle_state_table = powernv_states;
385 		/* Device tree can indicate more idle states */
386 		max_idle_state = powernv_add_idle_states();
387 		if (max_idle_state > 1) {
388 			snooze_timeout_en = true;
389 			snooze_timeout = powernv_states[1].target_residency *
390 					 tb_ticks_per_usec;
391 		}
392  	} else
393  		return -ENODEV;
394 
395 	return 0;
396 }
397 
398 static int __init powernv_processor_idle_init(void)
399 {
400 	int retval;
401 
402 	retval = powernv_idle_probe();
403 	if (retval)
404 		return retval;
405 
406 	powernv_cpuidle_driver_init();
407 	retval = cpuidle_register(&powernv_idle_driver, NULL);
408 	if (retval) {
409 		printk(KERN_DEBUG "Registration of powernv driver failed.\n");
410 		return retval;
411 	}
412 
413 	retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
414 					   "cpuidle/powernv:online",
415 					   powernv_cpuidle_cpu_online, NULL);
416 	WARN_ON(retval < 0);
417 	retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD,
418 					   "cpuidle/powernv:dead", NULL,
419 					   powernv_cpuidle_cpu_dead);
420 	WARN_ON(retval < 0);
421 	printk(KERN_DEBUG "powernv_idle_driver registered\n");
422 	return 0;
423 }
424 
425 device_initcall(powernv_processor_idle_init);
426