driver.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. * For each CPU in the driver's cpumask, unset the registered driver per CPU
  55. * to @drv.
  56. *
  57. * Returns 0 on success, -EBUSY if the CPUs have driver(s) 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. if (__cpuidle_get_cpu_driver(cpu)) {
  64. __cpuidle_unset_driver(drv);
  65. return -EBUSY;
  66. }
  67. per_cpu(cpuidle_drivers, cpu) = drv;
  68. }
  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. drv->refcnt = 0;
  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. /*
  142. * Look for the timer stop flag in the different states, so that we know
  143. * if the broadcast timer has to be set up. The loop is in the reverse
  144. * order, because usually one of the deeper states have this flag set.
  145. */
  146. for (i = drv->state_count - 1; i >= 0 ; i--) {
  147. if (drv->states[i].flags & CPUIDLE_FLAG_TIMER_STOP) {
  148. drv->bctimer = 1;
  149. break;
  150. }
  151. }
  152. }
  153. #ifdef CONFIG_ARCH_HAS_CPU_RELAX
  154. static int __cpuidle poll_idle(struct cpuidle_device *dev,
  155. struct cpuidle_driver *drv, int index)
  156. {
  157. local_irq_enable();
  158. if (!current_set_polling_and_test()) {
  159. while (!need_resched())
  160. cpu_relax();
  161. }
  162. current_clr_polling();
  163. return index;
  164. }
  165. static void poll_idle_init(struct cpuidle_driver *drv)
  166. {
  167. struct cpuidle_state *state = &drv->states[0];
  168. snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
  169. snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
  170. state->exit_latency = 0;
  171. state->target_residency = 0;
  172. state->power_usage = -1;
  173. state->enter = poll_idle;
  174. state->disabled = false;
  175. }
  176. #else
  177. static void poll_idle_init(struct cpuidle_driver *drv) {}
  178. #endif /* !CONFIG_ARCH_HAS_CPU_RELAX */
  179. /**
  180. * __cpuidle_register_driver: register the driver
  181. * @drv: a valid pointer to a struct cpuidle_driver
  182. *
  183. * Do some sanity checks, initialize the driver, assign the driver to the
  184. * global cpuidle driver variable(s) and set up the broadcast timer if the
  185. * cpuidle driver has some states that shut down the local timer.
  186. *
  187. * Returns 0 on success, a negative error code otherwise:
  188. * * -EINVAL if the driver pointer is NULL or no idle states are available
  189. * * -ENODEV if the cpuidle framework is disabled
  190. * * -EBUSY if the driver is already assigned to the global variable(s)
  191. */
  192. static int __cpuidle_register_driver(struct cpuidle_driver *drv)
  193. {
  194. int ret;
  195. if (!drv || !drv->state_count)
  196. return -EINVAL;
  197. ret = cpuidle_coupled_state_verify(drv);
  198. if (ret)
  199. return ret;
  200. if (cpuidle_disabled())
  201. return -ENODEV;
  202. __cpuidle_driver_init(drv);
  203. ret = __cpuidle_set_driver(drv);
  204. if (ret)
  205. return ret;
  206. if (drv->bctimer)
  207. on_each_cpu_mask(drv->cpumask, cpuidle_setup_broadcast_timer,
  208. (void *)1, 1);
  209. poll_idle_init(drv);
  210. return 0;
  211. }
  212. /**
  213. * __cpuidle_unregister_driver - unregister the driver
  214. * @drv: a valid pointer to a struct cpuidle_driver
  215. *
  216. * Check if the driver is no longer in use, reset the global cpuidle driver
  217. * variable(s) and disable the timer broadcast notification mechanism if it was
  218. * in use.
  219. *
  220. */
  221. static void __cpuidle_unregister_driver(struct cpuidle_driver *drv)
  222. {
  223. if (WARN_ON(drv->refcnt > 0))
  224. return;
  225. if (drv->bctimer) {
  226. drv->bctimer = 0;
  227. on_each_cpu_mask(drv->cpumask, cpuidle_setup_broadcast_timer,
  228. NULL, 1);
  229. }
  230. __cpuidle_unset_driver(drv);
  231. }
  232. /**
  233. * cpuidle_register_driver - registers a driver
  234. * @drv: a pointer to a valid struct cpuidle_driver
  235. *
  236. * Register the driver under a lock to prevent concurrent attempts to
  237. * [un]register the driver from occuring at the same time.
  238. *
  239. * Returns 0 on success, a negative error code (returned by
  240. * __cpuidle_register_driver()) otherwise.
  241. */
  242. int cpuidle_register_driver(struct cpuidle_driver *drv)
  243. {
  244. int ret;
  245. spin_lock(&cpuidle_driver_lock);
  246. ret = __cpuidle_register_driver(drv);
  247. spin_unlock(&cpuidle_driver_lock);
  248. return ret;
  249. }
  250. EXPORT_SYMBOL_GPL(cpuidle_register_driver);
  251. /**
  252. * cpuidle_unregister_driver - unregisters a driver
  253. * @drv: a pointer to a valid struct cpuidle_driver
  254. *
  255. * Unregisters the cpuidle driver under a lock to prevent concurrent attempts
  256. * to [un]register the driver from occuring at the same time. @drv has to
  257. * match the currently registered driver.
  258. */
  259. void cpuidle_unregister_driver(struct cpuidle_driver *drv)
  260. {
  261. spin_lock(&cpuidle_driver_lock);
  262. __cpuidle_unregister_driver(drv);
  263. spin_unlock(&cpuidle_driver_lock);
  264. }
  265. EXPORT_SYMBOL_GPL(cpuidle_unregister_driver);
  266. /**
  267. * cpuidle_get_driver - return the driver tied to the current CPU.
  268. *
  269. * Returns a struct cpuidle_driver pointer, or NULL if no driver is registered.
  270. */
  271. struct cpuidle_driver *cpuidle_get_driver(void)
  272. {
  273. struct cpuidle_driver *drv;
  274. int cpu;
  275. cpu = get_cpu();
  276. drv = __cpuidle_get_cpu_driver(cpu);
  277. put_cpu();
  278. return drv;
  279. }
  280. EXPORT_SYMBOL_GPL(cpuidle_get_driver);
  281. /**
  282. * cpuidle_get_cpu_driver - return the driver registered for a CPU.
  283. * @dev: a valid pointer to a struct cpuidle_device
  284. *
  285. * Returns a struct cpuidle_driver pointer, or NULL if no driver is registered
  286. * for the CPU associated with @dev.
  287. */
  288. struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev)
  289. {
  290. if (!dev)
  291. return NULL;
  292. return __cpuidle_get_cpu_driver(dev->cpu);
  293. }
  294. EXPORT_SYMBOL_GPL(cpuidle_get_cpu_driver);
  295. /**
  296. * cpuidle_driver_ref - get a reference to the driver.
  297. *
  298. * Increment the reference counter of the cpuidle driver associated with
  299. * the current CPU.
  300. *
  301. * Returns a pointer to the driver, or NULL if the current CPU has no driver.
  302. */
  303. struct cpuidle_driver *cpuidle_driver_ref(void)
  304. {
  305. struct cpuidle_driver *drv;
  306. spin_lock(&cpuidle_driver_lock);
  307. drv = cpuidle_get_driver();
  308. if (drv)
  309. drv->refcnt++;
  310. spin_unlock(&cpuidle_driver_lock);
  311. return drv;
  312. }
  313. /**
  314. * cpuidle_driver_unref - puts down the refcount for the driver
  315. *
  316. * Decrement the reference counter of the cpuidle driver associated with
  317. * the current CPU.
  318. */
  319. void cpuidle_driver_unref(void)
  320. {
  321. struct cpuidle_driver *drv;
  322. spin_lock(&cpuidle_driver_lock);
  323. drv = cpuidle_get_driver();
  324. if (drv && !WARN_ON(drv->refcnt <= 0))
  325. drv->refcnt--;
  326. spin_unlock(&cpuidle_driver_lock);
  327. }