xref: /linux/drivers/cpuidle/driver.c (revision 3ef975893041b9f826475298c802b5c046d0ba16)
1 /*
2  * driver.c - driver support
3  *
4  * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
5  *               Shaohua Li <shaohua.li@intel.com>
6  *               Adam Belay <abelay@novell.com>
7  *
8  * This code is licenced under the GPL.
9  */
10 
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 
13 #include <linux/mutex.h>
14 #include <linux/module.h>
15 #include <linux/sched.h>
16 #include <linux/sched/idle.h>
17 #include <linux/cpuidle.h>
18 #include <linux/cpumask.h>
19 #include <linux/tick.h>
20 #include <linux/cpu.h>
21 #include <linux/math64.h>
22 
23 #include "cpuidle.h"
24 
25 DEFINE_SPINLOCK(cpuidle_driver_lock);
26 
27 #ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS
28 
29 static DEFINE_PER_CPU(struct cpuidle_driver *, cpuidle_drivers);
30 
31 /**
32  * __cpuidle_get_cpu_driver - return the cpuidle driver tied to a CPU.
33  * @cpu: the CPU handled by the driver
34  *
35  * Returns a pointer to struct cpuidle_driver or NULL if no driver has been
36  * registered for @cpu.
37  */
38 static struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu)
39 {
40 	return per_cpu(cpuidle_drivers, cpu);
41 }
42 
43 /**
44  * __cpuidle_unset_driver - unset per CPU driver variables.
45  * @drv: a valid pointer to a struct cpuidle_driver
46  *
47  * For each CPU in the driver's CPU mask, unset the registered driver per CPU
48  * variable. If @drv is different from the registered driver, the corresponding
49  * variable is not cleared.
50  */
51 static inline void __cpuidle_unset_driver(struct cpuidle_driver *drv)
52 {
53 	int cpu;
54 
55 	for_each_cpu(cpu, drv->cpumask) {
56 
57 		if (drv != __cpuidle_get_cpu_driver(cpu))
58 			continue;
59 
60 		per_cpu(cpuidle_drivers, cpu) = NULL;
61 	}
62 }
63 
64 /**
65  * __cpuidle_set_driver - set per CPU driver variables for the given driver.
66  * @drv: a valid pointer to a struct cpuidle_driver
67  *
68  * Returns 0 on success, -EBUSY if any CPU in the cpumask have a driver
69  * different from drv already.
70  */
71 static inline int __cpuidle_set_driver(struct cpuidle_driver *drv)
72 {
73 	int cpu;
74 
75 	for_each_cpu(cpu, drv->cpumask) {
76 		struct cpuidle_driver *old_drv;
77 
78 		old_drv = __cpuidle_get_cpu_driver(cpu);
79 		if (old_drv && old_drv != drv)
80 			return -EBUSY;
81 	}
82 
83 	for_each_cpu(cpu, drv->cpumask)
84 		per_cpu(cpuidle_drivers, cpu) = drv;
85 
86 	return 0;
87 }
88 
89 #else
90 
91 static struct cpuidle_driver *cpuidle_curr_driver;
92 
93 /**
94  * __cpuidle_get_cpu_driver - return the global cpuidle driver pointer.
95  * @cpu: ignored without the multiple driver support
96  *
97  * Return a pointer to a struct cpuidle_driver object or NULL if no driver was
98  * previously registered.
99  */
100 static inline struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu)
101 {
102 	return cpuidle_curr_driver;
103 }
104 
105 /**
106  * __cpuidle_set_driver - assign the global cpuidle driver variable.
107  * @drv: pointer to a struct cpuidle_driver object
108  *
109  * Returns 0 on success, -EBUSY if the driver is already registered.
110  */
111 static inline int __cpuidle_set_driver(struct cpuidle_driver *drv)
112 {
113 	if (cpuidle_curr_driver)
114 		return -EBUSY;
115 
116 	cpuidle_curr_driver = drv;
117 
118 	return 0;
119 }
120 
121 /**
122  * __cpuidle_unset_driver - unset the global cpuidle driver variable.
123  * @drv: a pointer to a struct cpuidle_driver
124  *
125  * Reset the global cpuidle variable to NULL.  If @drv does not match the
126  * registered driver, do nothing.
127  */
128 static inline void __cpuidle_unset_driver(struct cpuidle_driver *drv)
129 {
130 	if (drv == cpuidle_curr_driver)
131 		cpuidle_curr_driver = NULL;
132 }
133 
134 #endif
135 
136 /**
137  * cpuidle_setup_broadcast_timer - enable/disable the broadcast timer on a cpu
138  * @arg: a void pointer used to match the SMP cross call API
139  *
140  * If @arg is NULL broadcast is disabled otherwise enabled
141  *
142  * This function is executed per CPU by an SMP cross call.  It's not
143  * supposed to be called directly.
144  */
145 static void cpuidle_setup_broadcast_timer(void *arg)
146 {
147 	if (arg)
148 		tick_broadcast_enable();
149 	else
150 		tick_broadcast_disable();
151 }
152 
153 /**
154  * __cpuidle_driver_init - initialize the driver's internal data
155  * @drv: a valid pointer to a struct cpuidle_driver
156  */
157 static void __cpuidle_driver_init(struct cpuidle_driver *drv)
158 {
159 	int i;
160 
161 	/*
162 	 * Use all possible CPUs as the default, because if the kernel boots
163 	 * with some CPUs offline and then we online one of them, the CPU
164 	 * notifier has to know which driver to assign.
165 	 */
166 	if (!drv->cpumask)
167 		drv->cpumask = (struct cpumask *)cpu_possible_mask;
168 
169 	for (i = 0; i < drv->state_count; i++) {
170 		struct cpuidle_state *s = &drv->states[i];
171 
172 		/*
173 		 * Look for the timer stop flag in the different states and if
174 		 * it is found, indicate that the broadcast timer has to be set
175 		 * up.
176 		 */
177 		if (s->flags & CPUIDLE_FLAG_TIMER_STOP)
178 			drv->bctimer = 1;
179 
180 		/*
181 		 * The core will use the target residency and exit latency
182 		 * values in nanoseconds, but allow drivers to provide them in
183 		 * microseconds too.
184 		 */
185 		if (s->target_residency > 0)
186 			s->target_residency_ns = s->target_residency * NSEC_PER_USEC;
187 		else if (s->target_residency_ns < 0)
188 			s->target_residency_ns = 0;
189 		else
190 			s->target_residency = div_u64(s->target_residency_ns, NSEC_PER_USEC);
191 
192 		if (s->exit_latency > 0)
193 			s->exit_latency_ns = mul_u32_u32(s->exit_latency, NSEC_PER_USEC);
194 		else if (s->exit_latency_ns < 0)
195 			s->exit_latency_ns =  0;
196 		else
197 			s->exit_latency = div_u64(s->exit_latency_ns, NSEC_PER_USEC);
198 	}
199 }
200 
201 /**
202  * __cpuidle_register_driver: register the driver
203  * @drv: a valid pointer to a struct cpuidle_driver
204  *
205  * Do some sanity checks, initialize the driver, assign the driver to the
206  * global cpuidle driver variable(s) and set up the broadcast timer if the
207  * cpuidle driver has some states that shut down the local timer.
208  *
209  * Returns 0 on success, a negative error code otherwise:
210  *  * -EINVAL if the driver pointer is NULL or no idle states are available
211  *  * -ENODEV if the cpuidle framework is disabled
212  *  * -EBUSY if the driver is already assigned to the global variable(s)
213  */
214 static int __cpuidle_register_driver(struct cpuidle_driver *drv)
215 {
216 	int ret;
217 
218 	if (!drv || !drv->state_count)
219 		return -EINVAL;
220 
221 	ret = cpuidle_coupled_state_verify(drv);
222 	if (ret)
223 		return ret;
224 
225 	if (cpuidle_disabled())
226 		return -ENODEV;
227 
228 	__cpuidle_driver_init(drv);
229 
230 	ret = __cpuidle_set_driver(drv);
231 	if (ret)
232 		return ret;
233 
234 	if (drv->bctimer)
235 		on_each_cpu_mask(drv->cpumask, cpuidle_setup_broadcast_timer,
236 				 (void *)1, 1);
237 
238 	return 0;
239 }
240 
241 /**
242  * __cpuidle_unregister_driver - unregister the driver
243  * @drv: a valid pointer to a struct cpuidle_driver
244  *
245  * Check if the driver is no longer in use, reset the global cpuidle driver
246  * variable(s) and disable the timer broadcast notification mechanism if it was
247  * in use.
248  *
249  */
250 static void __cpuidle_unregister_driver(struct cpuidle_driver *drv)
251 {
252 	if (drv->bctimer) {
253 		drv->bctimer = 0;
254 		on_each_cpu_mask(drv->cpumask, cpuidle_setup_broadcast_timer,
255 				 NULL, 1);
256 	}
257 
258 	__cpuidle_unset_driver(drv);
259 }
260 
261 /**
262  * cpuidle_register_driver - registers a driver
263  * @drv: a pointer to a valid struct cpuidle_driver
264  *
265  * Register the driver under a lock to prevent concurrent attempts to
266  * [un]register the driver from occurring at the same time.
267  *
268  * Returns 0 on success, a negative error code (returned by
269  * __cpuidle_register_driver()) otherwise.
270  */
271 int cpuidle_register_driver(struct cpuidle_driver *drv)
272 {
273 	struct cpuidle_governor *gov;
274 	int ret;
275 
276 	spin_lock(&cpuidle_driver_lock);
277 	ret = __cpuidle_register_driver(drv);
278 	spin_unlock(&cpuidle_driver_lock);
279 
280 	if (!ret && !strlen(param_governor) && drv->governor &&
281 	    (cpuidle_get_driver() == drv)) {
282 		mutex_lock(&cpuidle_lock);
283 		gov = cpuidle_find_governor(drv->governor);
284 		if (gov) {
285 			cpuidle_prev_governor = cpuidle_curr_governor;
286 			if (cpuidle_switch_governor(gov) < 0)
287 				cpuidle_prev_governor = NULL;
288 		}
289 		mutex_unlock(&cpuidle_lock);
290 	}
291 
292 	return ret;
293 }
294 EXPORT_SYMBOL_GPL(cpuidle_register_driver);
295 
296 /**
297  * cpuidle_unregister_driver - unregisters a driver
298  * @drv: a pointer to a valid struct cpuidle_driver
299  *
300  * Unregisters the cpuidle driver under a lock to prevent concurrent attempts
301  * to [un]register the driver from occurring at the same time.  @drv has to
302  * match the currently registered driver.
303  */
304 void cpuidle_unregister_driver(struct cpuidle_driver *drv)
305 {
306 	bool enabled = (cpuidle_get_driver() == drv);
307 
308 	spin_lock(&cpuidle_driver_lock);
309 	__cpuidle_unregister_driver(drv);
310 	spin_unlock(&cpuidle_driver_lock);
311 
312 	if (!enabled)
313 		return;
314 
315 	mutex_lock(&cpuidle_lock);
316 	if (cpuidle_prev_governor) {
317 		if (!cpuidle_switch_governor(cpuidle_prev_governor))
318 			cpuidle_prev_governor = NULL;
319 	}
320 	mutex_unlock(&cpuidle_lock);
321 }
322 EXPORT_SYMBOL_GPL(cpuidle_unregister_driver);
323 
324 /**
325  * cpuidle_get_driver - return the driver tied to the current CPU.
326  *
327  * Returns a struct cpuidle_driver pointer, or NULL if no driver is registered.
328  */
329 struct cpuidle_driver *cpuidle_get_driver(void)
330 {
331 	struct cpuidle_driver *drv;
332 	int cpu;
333 
334 	cpu = get_cpu();
335 	drv = __cpuidle_get_cpu_driver(cpu);
336 	put_cpu();
337 
338 	return drv;
339 }
340 EXPORT_SYMBOL_GPL(cpuidle_get_driver);
341 
342 /**
343  * cpuidle_get_cpu_driver - return the driver registered for a CPU.
344  * @dev: a valid pointer to a struct cpuidle_device
345  *
346  * Returns a struct cpuidle_driver pointer, or NULL if no driver is registered
347  * for the CPU associated with @dev.
348  */
349 struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev)
350 {
351 	if (!dev)
352 		return NULL;
353 
354 	return __cpuidle_get_cpu_driver(dev->cpu);
355 }
356 EXPORT_SYMBOL_GPL(cpuidle_get_cpu_driver);
357 
358 /**
359  * cpuidle_driver_state_disabled - Disable or enable an idle state
360  * @drv: cpuidle driver owning the state
361  * @idx: State index
362  * @disable: Whether or not to disable the state
363  */
364 void cpuidle_driver_state_disabled(struct cpuidle_driver *drv, int idx,
365 				 bool disable)
366 {
367 	unsigned int cpu;
368 
369 	mutex_lock(&cpuidle_lock);
370 
371 	spin_lock(&cpuidle_driver_lock);
372 
373 	if (!drv->cpumask) {
374 		drv->states[idx].flags |= CPUIDLE_FLAG_UNUSABLE;
375 		goto unlock;
376 	}
377 
378 	for_each_cpu(cpu, drv->cpumask) {
379 		struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
380 
381 		if (!dev)
382 			continue;
383 
384 		if (disable)
385 			dev->states_usage[idx].disable |= CPUIDLE_STATE_DISABLED_BY_DRIVER;
386 		else
387 			dev->states_usage[idx].disable &= ~CPUIDLE_STATE_DISABLED_BY_DRIVER;
388 	}
389 
390 unlock:
391 	spin_unlock(&cpuidle_driver_lock);
392 
393 	mutex_unlock(&cpuidle_lock);
394 }
395