driver.c 10 KB

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