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