driver.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. #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. if (s->exit_latency > 0)
  156. s->exit_latency_ns = s->exit_latency * NSEC_PER_USEC;
  157. }
  158. }
  159. /**
  160. * __cpuidle_register_driver: register the driver
  161. * @drv: a valid pointer to a struct cpuidle_driver
  162. *
  163. * Do some sanity checks, initialize the driver, assign the driver to the
  164. * global cpuidle driver variable(s) and set up the broadcast timer if the
  165. * cpuidle driver has some states that shut down the local timer.
  166. *
  167. * Returns 0 on success, a negative error code otherwise:
  168. * * -EINVAL if the driver pointer is NULL or no idle states are available
  169. * * -ENODEV if the cpuidle framework is disabled
  170. * * -EBUSY if the driver is already assigned to the global variable(s)
  171. */
  172. static int __cpuidle_register_driver(struct cpuidle_driver *drv)
  173. {
  174. int ret;
  175. if (!drv || !drv->state_count)
  176. return -EINVAL;
  177. ret = cpuidle_coupled_state_verify(drv);
  178. if (ret)
  179. return ret;
  180. if (cpuidle_disabled())
  181. return -ENODEV;
  182. __cpuidle_driver_init(drv);
  183. ret = __cpuidle_set_driver(drv);
  184. if (ret)
  185. return ret;
  186. if (drv->bctimer)
  187. on_each_cpu_mask(drv->cpumask, cpuidle_setup_broadcast_timer,
  188. (void *)1, 1);
  189. return 0;
  190. }
  191. /**
  192. * __cpuidle_unregister_driver - unregister the driver
  193. * @drv: a valid pointer to a struct cpuidle_driver
  194. *
  195. * Check if the driver is no longer in use, reset the global cpuidle driver
  196. * variable(s) and disable the timer broadcast notification mechanism if it was
  197. * in use.
  198. *
  199. */
  200. static void __cpuidle_unregister_driver(struct cpuidle_driver *drv)
  201. {
  202. if (drv->bctimer) {
  203. drv->bctimer = 0;
  204. on_each_cpu_mask(drv->cpumask, cpuidle_setup_broadcast_timer,
  205. NULL, 1);
  206. }
  207. __cpuidle_unset_driver(drv);
  208. }
  209. /**
  210. * cpuidle_register_driver - registers a driver
  211. * @drv: a pointer to a valid struct cpuidle_driver
  212. *
  213. * Register the driver under a lock to prevent concurrent attempts to
  214. * [un]register the driver from occuring at the same time.
  215. *
  216. * Returns 0 on success, a negative error code (returned by
  217. * __cpuidle_register_driver()) otherwise.
  218. */
  219. int cpuidle_register_driver(struct cpuidle_driver *drv)
  220. {
  221. struct cpuidle_governor *gov;
  222. int ret;
  223. spin_lock(&cpuidle_driver_lock);
  224. ret = __cpuidle_register_driver(drv);
  225. spin_unlock(&cpuidle_driver_lock);
  226. if (!ret && !strlen(param_governor) && drv->governor &&
  227. (cpuidle_get_driver() == drv)) {
  228. mutex_lock(&cpuidle_lock);
  229. gov = cpuidle_find_governor(drv->governor);
  230. if (gov) {
  231. cpuidle_prev_governor = cpuidle_curr_governor;
  232. if (cpuidle_switch_governor(gov) < 0)
  233. cpuidle_prev_governor = NULL;
  234. }
  235. mutex_unlock(&cpuidle_lock);
  236. }
  237. return ret;
  238. }
  239. EXPORT_SYMBOL_GPL(cpuidle_register_driver);
  240. /**
  241. * cpuidle_unregister_driver - unregisters a driver
  242. * @drv: a pointer to a valid struct cpuidle_driver
  243. *
  244. * Unregisters the cpuidle driver under a lock to prevent concurrent attempts
  245. * to [un]register the driver from occuring at the same time. @drv has to
  246. * match the currently registered driver.
  247. */
  248. void cpuidle_unregister_driver(struct cpuidle_driver *drv)
  249. {
  250. bool enabled = (cpuidle_get_driver() == drv);
  251. spin_lock(&cpuidle_driver_lock);
  252. __cpuidle_unregister_driver(drv);
  253. spin_unlock(&cpuidle_driver_lock);
  254. if (!enabled)
  255. return;
  256. mutex_lock(&cpuidle_lock);
  257. if (cpuidle_prev_governor) {
  258. if (!cpuidle_switch_governor(cpuidle_prev_governor))
  259. cpuidle_prev_governor = NULL;
  260. }
  261. mutex_unlock(&cpuidle_lock);
  262. }
  263. EXPORT_SYMBOL_GPL(cpuidle_unregister_driver);
  264. /**
  265. * cpuidle_get_driver - return the driver tied to the current CPU.
  266. *
  267. * Returns a struct cpuidle_driver pointer, or NULL if no driver is registered.
  268. */
  269. struct cpuidle_driver *cpuidle_get_driver(void)
  270. {
  271. struct cpuidle_driver *drv;
  272. int cpu;
  273. cpu = get_cpu();
  274. drv = __cpuidle_get_cpu_driver(cpu);
  275. put_cpu();
  276. return drv;
  277. }
  278. EXPORT_SYMBOL_GPL(cpuidle_get_driver);
  279. /**
  280. * cpuidle_get_cpu_driver - return the driver registered for a CPU.
  281. * @dev: a valid pointer to a struct cpuidle_device
  282. *
  283. * Returns a struct cpuidle_driver pointer, or NULL if no driver is registered
  284. * for the CPU associated with @dev.
  285. */
  286. struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev)
  287. {
  288. if (!dev)
  289. return NULL;
  290. return __cpuidle_get_cpu_driver(dev->cpu);
  291. }
  292. EXPORT_SYMBOL_GPL(cpuidle_get_cpu_driver);
  293. /**
  294. * cpuidle_driver_state_disabled - Disable or enable an idle state
  295. * @drv: cpuidle driver owning the state
  296. * @idx: State index
  297. * @disable: Whether or not to disable the state
  298. */
  299. void cpuidle_driver_state_disabled(struct cpuidle_driver *drv, int idx,
  300. bool disable)
  301. {
  302. unsigned int cpu;
  303. mutex_lock(&cpuidle_lock);
  304. spin_lock(&cpuidle_driver_lock);
  305. if (!drv->cpumask) {
  306. drv->states[idx].flags |= CPUIDLE_FLAG_UNUSABLE;
  307. goto unlock;
  308. }
  309. for_each_cpu(cpu, drv->cpumask) {
  310. struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
  311. if (!dev)
  312. continue;
  313. if (disable)
  314. dev->states_usage[idx].disable |= CPUIDLE_STATE_DISABLED_BY_DRIVER;
  315. else
  316. dev->states_usage[idx].disable &= ~CPUIDLE_STATE_DISABLED_BY_DRIVER;
  317. }
  318. unlock:
  319. spin_unlock(&cpuidle_driver_lock);
  320. mutex_unlock(&cpuidle_lock);
  321. }
  322. EXPORT_SYMBOL_GPL(cpuidle_driver_state_disabled);