driver.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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/cpuidle.h>
  13. #include <linux/cpumask.h>
  14. #include <linux/clockchips.h>
  15. #include "cpuidle.h"
  16. DEFINE_SPINLOCK(cpuidle_driver_lock);
  17. #ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS
  18. static DEFINE_PER_CPU(struct cpuidle_driver *, cpuidle_drivers);
  19. /**
  20. * __cpuidle_get_cpu_driver - return the cpuidle driver tied to a CPU.
  21. * @cpu: the CPU handled by the driver
  22. *
  23. * Returns a pointer to struct cpuidle_driver or NULL if no driver has been
  24. * registered for @cpu.
  25. */
  26. static struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu)
  27. {
  28. return per_cpu(cpuidle_drivers, cpu);
  29. }
  30. /**
  31. * __cpuidle_unset_driver - unset per CPU driver variables.
  32. * @drv: a valid pointer to a struct cpuidle_driver
  33. *
  34. * For each CPU in the driver's CPU mask, unset the registered driver per CPU
  35. * variable. If @drv is different from the registered driver, the corresponding
  36. * variable is not cleared.
  37. */
  38. static inline void __cpuidle_unset_driver(struct cpuidle_driver *drv)
  39. {
  40. int cpu;
  41. for_each_cpu(cpu, drv->cpumask) {
  42. if (drv != __cpuidle_get_cpu_driver(cpu))
  43. continue;
  44. per_cpu(cpuidle_drivers, cpu) = NULL;
  45. }
  46. }
  47. /**
  48. * __cpuidle_set_driver - set per CPU driver variables for the given driver.
  49. * @drv: a valid pointer to a struct cpuidle_driver
  50. *
  51. * For each CPU in the driver's cpumask, unset the registered driver per CPU
  52. * to @drv.
  53. *
  54. * Returns 0 on success, -EBUSY if the CPUs have driver(s) already.
  55. */
  56. static inline int __cpuidle_set_driver(struct cpuidle_driver *drv)
  57. {
  58. int cpu;
  59. for_each_cpu(cpu, drv->cpumask) {
  60. if (__cpuidle_get_cpu_driver(cpu)) {
  61. __cpuidle_unset_driver(drv);
  62. return -EBUSY;
  63. }
  64. per_cpu(cpuidle_drivers, cpu) = drv;
  65. }
  66. return 0;
  67. }
  68. #else
  69. static struct cpuidle_driver *cpuidle_curr_driver;
  70. /**
  71. * __cpuidle_get_cpu_driver - return the global cpuidle driver pointer.
  72. * @cpu: ignored without the multiple driver support
  73. *
  74. * Return a pointer to a struct cpuidle_driver object or NULL if no driver was
  75. * previously registered.
  76. */
  77. static inline struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu)
  78. {
  79. return cpuidle_curr_driver;
  80. }
  81. /**
  82. * __cpuidle_set_driver - assign the global cpuidle driver variable.
  83. * @drv: pointer to a struct cpuidle_driver object
  84. *
  85. * Returns 0 on success, -EBUSY if the driver is already registered.
  86. */
  87. static inline int __cpuidle_set_driver(struct cpuidle_driver *drv)
  88. {
  89. if (cpuidle_curr_driver)
  90. return -EBUSY;
  91. cpuidle_curr_driver = drv;
  92. return 0;
  93. }
  94. /**
  95. * __cpuidle_unset_driver - unset the global cpuidle driver variable.
  96. * @drv: a pointer to a struct cpuidle_driver
  97. *
  98. * Reset the global cpuidle variable to NULL. If @drv does not match the
  99. * registered driver, do nothing.
  100. */
  101. static inline void __cpuidle_unset_driver(struct cpuidle_driver *drv)
  102. {
  103. if (drv == cpuidle_curr_driver)
  104. cpuidle_curr_driver = NULL;
  105. }
  106. #endif
  107. /**
  108. * cpuidle_setup_broadcast_timer - enable/disable the broadcast timer
  109. * @arg: a void pointer used to match the SMP cross call API
  110. *
  111. * @arg is used as a value of type 'long' with one of the two values:
  112. * - CLOCK_EVT_NOTIFY_BROADCAST_ON
  113. * - CLOCK_EVT_NOTIFY_BROADCAST_OFF
  114. *
  115. * Set the broadcast timer notification for the current CPU. This function
  116. * is executed per CPU by an SMP cross call. It not supposed to be called
  117. * directly.
  118. */
  119. static void cpuidle_setup_broadcast_timer(void *arg)
  120. {
  121. int cpu = smp_processor_id();
  122. clockevents_notify((long)(arg), &cpu);
  123. }
  124. /**
  125. * __cpuidle_driver_init - initialize the driver's internal data
  126. * @drv: a valid pointer to a struct cpuidle_driver
  127. */
  128. static void __cpuidle_driver_init(struct cpuidle_driver *drv)
  129. {
  130. int i;
  131. drv->refcnt = 0;
  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. /*
  140. * Look for the timer stop flag in the different states, so that we know
  141. * if the broadcast timer has to be set up. The loop is in the reverse
  142. * order, because usually one of the deeper states have this flag set.
  143. */
  144. for (i = drv->state_count - 1; i >= 0 ; i--) {
  145. if (drv->states[i].flags & CPUIDLE_FLAG_TIMER_STOP) {
  146. drv->bctimer = 1;
  147. break;
  148. }
  149. }
  150. }
  151. /**
  152. * __cpuidle_register_driver: register the driver
  153. * @drv: a valid pointer to a struct cpuidle_driver
  154. *
  155. * Do some sanity checks, initialize the driver, assign the driver to the
  156. * global cpuidle driver variable(s) and set up the broadcast timer if the
  157. * cpuidle driver has some states that shut down the local timer.
  158. *
  159. * Returns 0 on success, a negative error code otherwise:
  160. * * -EINVAL if the driver pointer is NULL or no idle states are available
  161. * * -ENODEV if the cpuidle framework is disabled
  162. * * -EBUSY if the driver is already assigned to the global variable(s)
  163. */
  164. static int __cpuidle_register_driver(struct cpuidle_driver *drv)
  165. {
  166. int ret;
  167. if (!drv || !drv->state_count)
  168. return -EINVAL;
  169. if (cpuidle_disabled())
  170. return -ENODEV;
  171. __cpuidle_driver_init(drv);
  172. ret = __cpuidle_set_driver(drv);
  173. if (ret)
  174. return ret;
  175. if (drv->bctimer)
  176. on_each_cpu_mask(drv->cpumask, cpuidle_setup_broadcast_timer,
  177. (void *)CLOCK_EVT_NOTIFY_BROADCAST_ON, 1);
  178. return 0;
  179. }
  180. /**
  181. * __cpuidle_unregister_driver - unregister the driver
  182. * @drv: a valid pointer to a struct cpuidle_driver
  183. *
  184. * Check if the driver is no longer in use, reset the global cpuidle driver
  185. * variable(s) and disable the timer broadcast notification mechanism if it was
  186. * in use.
  187. *
  188. */
  189. static void __cpuidle_unregister_driver(struct cpuidle_driver *drv)
  190. {
  191. if (WARN_ON(drv->refcnt > 0))
  192. return;
  193. if (drv->bctimer) {
  194. drv->bctimer = 0;
  195. on_each_cpu_mask(drv->cpumask, cpuidle_setup_broadcast_timer,
  196. (void *)CLOCK_EVT_NOTIFY_BROADCAST_OFF, 1);
  197. }
  198. __cpuidle_unset_driver(drv);
  199. }
  200. /**
  201. * cpuidle_register_driver - registers a driver
  202. * @drv: a pointer to a valid struct cpuidle_driver
  203. *
  204. * Register the driver under a lock to prevent concurrent attempts to
  205. * [un]register the driver from occuring at the same time.
  206. *
  207. * Returns 0 on success, a negative error code (returned by
  208. * __cpuidle_register_driver()) otherwise.
  209. */
  210. int cpuidle_register_driver(struct cpuidle_driver *drv)
  211. {
  212. int ret;
  213. spin_lock(&cpuidle_driver_lock);
  214. ret = __cpuidle_register_driver(drv);
  215. spin_unlock(&cpuidle_driver_lock);
  216. return ret;
  217. }
  218. EXPORT_SYMBOL_GPL(cpuidle_register_driver);
  219. /**
  220. * cpuidle_unregister_driver - unregisters a driver
  221. * @drv: a pointer to a valid struct cpuidle_driver
  222. *
  223. * Unregisters the cpuidle driver under a lock to prevent concurrent attempts
  224. * to [un]register the driver from occuring at the same time. @drv has to
  225. * match the currently registered driver.
  226. */
  227. void cpuidle_unregister_driver(struct cpuidle_driver *drv)
  228. {
  229. spin_lock(&cpuidle_driver_lock);
  230. __cpuidle_unregister_driver(drv);
  231. spin_unlock(&cpuidle_driver_lock);
  232. }
  233. EXPORT_SYMBOL_GPL(cpuidle_unregister_driver);
  234. /**
  235. * cpuidle_get_driver - return the driver tied to the current CPU.
  236. *
  237. * Returns a struct cpuidle_driver pointer, or NULL if no driver is registered.
  238. */
  239. struct cpuidle_driver *cpuidle_get_driver(void)
  240. {
  241. struct cpuidle_driver *drv;
  242. int cpu;
  243. cpu = get_cpu();
  244. drv = __cpuidle_get_cpu_driver(cpu);
  245. put_cpu();
  246. return drv;
  247. }
  248. EXPORT_SYMBOL_GPL(cpuidle_get_driver);
  249. /**
  250. * cpuidle_get_cpu_driver - return the driver registered for a CPU.
  251. * @dev: a valid pointer to a struct cpuidle_device
  252. *
  253. * Returns a struct cpuidle_driver pointer, or NULL if no driver is registered
  254. * for the CPU associated with @dev.
  255. */
  256. struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev)
  257. {
  258. if (!dev)
  259. return NULL;
  260. return __cpuidle_get_cpu_driver(dev->cpu);
  261. }
  262. EXPORT_SYMBOL_GPL(cpuidle_get_cpu_driver);
  263. /**
  264. * cpuidle_driver_ref - get a reference to the driver.
  265. *
  266. * Increment the reference counter of the cpuidle driver associated with
  267. * the current CPU.
  268. *
  269. * Returns a pointer to the driver, or NULL if the current CPU has no driver.
  270. */
  271. struct cpuidle_driver *cpuidle_driver_ref(void)
  272. {
  273. struct cpuidle_driver *drv;
  274. spin_lock(&cpuidle_driver_lock);
  275. drv = cpuidle_get_driver();
  276. if (drv)
  277. drv->refcnt++;
  278. spin_unlock(&cpuidle_driver_lock);
  279. return drv;
  280. }
  281. /**
  282. * cpuidle_driver_unref - puts down the refcount for the driver
  283. *
  284. * Decrement the reference counter of the cpuidle driver associated with
  285. * the current CPU.
  286. */
  287. void cpuidle_driver_unref(void)
  288. {
  289. struct cpuidle_driver *drv = cpuidle_get_driver();
  290. spin_lock(&cpuidle_driver_lock);
  291. if (drv && !WARN_ON(drv->refcnt <= 0))
  292. drv->refcnt--;
  293. spin_unlock(&cpuidle_driver_lock);
  294. }