driver.c 9.8 KB

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