driver.c 9.7 KB

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