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