driver.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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/clockchips.h>
  16. #include "cpuidle.h"
  17. DEFINE_SPINLOCK(cpuidle_driver_lock);
  18. #ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS
  19. static DEFINE_PER_CPU(struct cpuidle_driver *, cpuidle_drivers);
  20. /**
  21. * __cpuidle_get_cpu_driver - return the cpuidle driver tied to a CPU.
  22. * @cpu: the CPU handled by the driver
  23. *
  24. * Returns a pointer to struct cpuidle_driver or NULL if no driver has been
  25. * registered for @cpu.
  26. */
  27. static struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu)
  28. {
  29. return per_cpu(cpuidle_drivers, cpu);
  30. }
  31. /**
  32. * __cpuidle_unset_driver - unset per CPU driver variables.
  33. * @drv: a valid pointer to a struct cpuidle_driver
  34. *
  35. * For each CPU in the driver's CPU mask, unset the registered driver per CPU
  36. * variable. If @drv is different from the registered driver, the corresponding
  37. * variable is not cleared.
  38. */
  39. static inline void __cpuidle_unset_driver(struct cpuidle_driver *drv)
  40. {
  41. int cpu;
  42. for_each_cpu(cpu, drv->cpumask) {
  43. if (drv != __cpuidle_get_cpu_driver(cpu))
  44. continue;
  45. per_cpu(cpuidle_drivers, cpu) = NULL;
  46. }
  47. }
  48. /**
  49. * __cpuidle_set_driver - set per CPU driver variables for the given driver.
  50. * @drv: a valid pointer to a struct cpuidle_driver
  51. *
  52. * For each CPU in the driver's cpumask, unset the registered driver per CPU
  53. * to @drv.
  54. *
  55. * Returns 0 on success, -EBUSY if the CPUs have driver(s) already.
  56. */
  57. static inline int __cpuidle_set_driver(struct cpuidle_driver *drv)
  58. {
  59. int cpu;
  60. for_each_cpu(cpu, drv->cpumask) {
  61. if (__cpuidle_get_cpu_driver(cpu)) {
  62. __cpuidle_unset_driver(drv);
  63. return -EBUSY;
  64. }
  65. per_cpu(cpuidle_drivers, cpu) = drv;
  66. }
  67. return 0;
  68. }
  69. #else
  70. static struct cpuidle_driver *cpuidle_curr_driver;
  71. /**
  72. * __cpuidle_get_cpu_driver - return the global cpuidle driver pointer.
  73. * @cpu: ignored without the multiple driver support
  74. *
  75. * Return a pointer to a struct cpuidle_driver object or NULL if no driver was
  76. * previously registered.
  77. */
  78. static inline struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu)
  79. {
  80. return cpuidle_curr_driver;
  81. }
  82. /**
  83. * __cpuidle_set_driver - assign the global cpuidle driver variable.
  84. * @drv: pointer to a struct cpuidle_driver object
  85. *
  86. * Returns 0 on success, -EBUSY if the driver is already registered.
  87. */
  88. static inline int __cpuidle_set_driver(struct cpuidle_driver *drv)
  89. {
  90. if (cpuidle_curr_driver)
  91. return -EBUSY;
  92. cpuidle_curr_driver = drv;
  93. return 0;
  94. }
  95. /**
  96. * __cpuidle_unset_driver - unset the global cpuidle driver variable.
  97. * @drv: a pointer to a struct cpuidle_driver
  98. *
  99. * Reset the global cpuidle variable to NULL. If @drv does not match the
  100. * registered driver, do nothing.
  101. */
  102. static inline void __cpuidle_unset_driver(struct cpuidle_driver *drv)
  103. {
  104. if (drv == cpuidle_curr_driver)
  105. cpuidle_curr_driver = NULL;
  106. }
  107. #endif
  108. /**
  109. * cpuidle_setup_broadcast_timer - enable/disable the broadcast timer
  110. * @arg: a void pointer used to match the SMP cross call API
  111. *
  112. * @arg is used as a value of type 'long' with one of the two values:
  113. * - CLOCK_EVT_NOTIFY_BROADCAST_ON
  114. * - CLOCK_EVT_NOTIFY_BROADCAST_OFF
  115. *
  116. * Set the broadcast timer notification for the current CPU. This function
  117. * is executed per CPU by an SMP cross call. It not supposed to be called
  118. * directly.
  119. */
  120. static void cpuidle_setup_broadcast_timer(void *arg)
  121. {
  122. int cpu = smp_processor_id();
  123. clockevents_notify((long)(arg), &cpu);
  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 poll_idle(struct cpuidle_device *dev,
  154. struct cpuidle_driver *drv, int index)
  155. {
  156. ktime_t t1, t2;
  157. s64 diff;
  158. t1 = ktime_get();
  159. local_irq_enable();
  160. if (!current_set_polling_and_test()) {
  161. while (!need_resched())
  162. cpu_relax();
  163. }
  164. current_clr_polling();
  165. t2 = ktime_get();
  166. diff = ktime_to_us(ktime_sub(t2, t1));
  167. if (diff > INT_MAX)
  168. diff = INT_MAX;
  169. dev->last_residency = (int) diff;
  170. return index;
  171. }
  172. static void poll_idle_init(struct cpuidle_driver *drv)
  173. {
  174. struct cpuidle_state *state = &drv->states[0];
  175. snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
  176. snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
  177. state->exit_latency = 0;
  178. state->target_residency = 0;
  179. state->power_usage = -1;
  180. state->flags = CPUIDLE_FLAG_TIME_VALID;
  181. state->enter = poll_idle;
  182. state->disabled = false;
  183. }
  184. #else
  185. static void poll_idle_init(struct cpuidle_driver *drv) {}
  186. #endif /* !CONFIG_ARCH_HAS_CPU_RELAX */
  187. /**
  188. * __cpuidle_register_driver: register the driver
  189. * @drv: a valid pointer to a struct cpuidle_driver
  190. *
  191. * Do some sanity checks, initialize the driver, assign the driver to the
  192. * global cpuidle driver variable(s) and set up the broadcast timer if the
  193. * cpuidle driver has some states that shut down the local timer.
  194. *
  195. * Returns 0 on success, a negative error code otherwise:
  196. * * -EINVAL if the driver pointer is NULL or no idle states are available
  197. * * -ENODEV if the cpuidle framework is disabled
  198. * * -EBUSY if the driver is already assigned to the global variable(s)
  199. */
  200. static int __cpuidle_register_driver(struct cpuidle_driver *drv)
  201. {
  202. int ret;
  203. if (!drv || !drv->state_count)
  204. return -EINVAL;
  205. if (cpuidle_disabled())
  206. return -ENODEV;
  207. __cpuidle_driver_init(drv);
  208. ret = __cpuidle_set_driver(drv);
  209. if (ret)
  210. return ret;
  211. if (drv->bctimer)
  212. on_each_cpu_mask(drv->cpumask, cpuidle_setup_broadcast_timer,
  213. (void *)CLOCK_EVT_NOTIFY_BROADCAST_ON, 1);
  214. poll_idle_init(drv);
  215. return 0;
  216. }
  217. /**
  218. * __cpuidle_unregister_driver - unregister the driver
  219. * @drv: a valid pointer to a struct cpuidle_driver
  220. *
  221. * Check if the driver is no longer in use, reset the global cpuidle driver
  222. * variable(s) and disable the timer broadcast notification mechanism if it was
  223. * in use.
  224. *
  225. */
  226. static void __cpuidle_unregister_driver(struct cpuidle_driver *drv)
  227. {
  228. if (WARN_ON(drv->refcnt > 0))
  229. return;
  230. if (drv->bctimer) {
  231. drv->bctimer = 0;
  232. on_each_cpu_mask(drv->cpumask, cpuidle_setup_broadcast_timer,
  233. (void *)CLOCK_EVT_NOTIFY_BROADCAST_OFF, 1);
  234. }
  235. __cpuidle_unset_driver(drv);
  236. }
  237. /**
  238. * cpuidle_register_driver - registers a driver
  239. * @drv: a pointer to a valid struct cpuidle_driver
  240. *
  241. * Register the driver under a lock to prevent concurrent attempts to
  242. * [un]register the driver from occuring at the same time.
  243. *
  244. * Returns 0 on success, a negative error code (returned by
  245. * __cpuidle_register_driver()) otherwise.
  246. */
  247. int cpuidle_register_driver(struct cpuidle_driver *drv)
  248. {
  249. int ret;
  250. spin_lock(&cpuidle_driver_lock);
  251. ret = __cpuidle_register_driver(drv);
  252. spin_unlock(&cpuidle_driver_lock);
  253. return ret;
  254. }
  255. EXPORT_SYMBOL_GPL(cpuidle_register_driver);
  256. /**
  257. * cpuidle_unregister_driver - unregisters a driver
  258. * @drv: a pointer to a valid struct cpuidle_driver
  259. *
  260. * Unregisters the cpuidle driver under a lock to prevent concurrent attempts
  261. * to [un]register the driver from occuring at the same time. @drv has to
  262. * match the currently registered driver.
  263. */
  264. void cpuidle_unregister_driver(struct cpuidle_driver *drv)
  265. {
  266. spin_lock(&cpuidle_driver_lock);
  267. __cpuidle_unregister_driver(drv);
  268. spin_unlock(&cpuidle_driver_lock);
  269. }
  270. EXPORT_SYMBOL_GPL(cpuidle_unregister_driver);
  271. /**
  272. * cpuidle_get_driver - return the driver tied to the current CPU.
  273. *
  274. * Returns a struct cpuidle_driver pointer, or NULL if no driver is registered.
  275. */
  276. struct cpuidle_driver *cpuidle_get_driver(void)
  277. {
  278. struct cpuidle_driver *drv;
  279. int cpu;
  280. cpu = get_cpu();
  281. drv = __cpuidle_get_cpu_driver(cpu);
  282. put_cpu();
  283. return drv;
  284. }
  285. EXPORT_SYMBOL_GPL(cpuidle_get_driver);
  286. /**
  287. * cpuidle_get_cpu_driver - return the driver registered for a CPU.
  288. * @dev: a valid pointer to a struct cpuidle_device
  289. *
  290. * Returns a struct cpuidle_driver pointer, or NULL if no driver is registered
  291. * for the CPU associated with @dev.
  292. */
  293. struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev)
  294. {
  295. if (!dev)
  296. return NULL;
  297. return __cpuidle_get_cpu_driver(dev->cpu);
  298. }
  299. EXPORT_SYMBOL_GPL(cpuidle_get_cpu_driver);
  300. /**
  301. * cpuidle_driver_ref - get a reference to the driver.
  302. *
  303. * Increment the reference counter of the cpuidle driver associated with
  304. * the current CPU.
  305. *
  306. * Returns a pointer to the driver, or NULL if the current CPU has no driver.
  307. */
  308. struct cpuidle_driver *cpuidle_driver_ref(void)
  309. {
  310. struct cpuidle_driver *drv;
  311. spin_lock(&cpuidle_driver_lock);
  312. drv = cpuidle_get_driver();
  313. if (drv)
  314. drv->refcnt++;
  315. spin_unlock(&cpuidle_driver_lock);
  316. return drv;
  317. }
  318. /**
  319. * cpuidle_driver_unref - puts down the refcount for the driver
  320. *
  321. * Decrement the reference counter of the cpuidle driver associated with
  322. * the current CPU.
  323. */
  324. void cpuidle_driver_unref(void)
  325. {
  326. struct cpuidle_driver *drv;
  327. spin_lock(&cpuidle_driver_lock);
  328. drv = cpuidle_get_driver();
  329. if (drv && !WARN_ON(drv->refcnt <= 0))
  330. drv->refcnt--;
  331. spin_unlock(&cpuidle_driver_lock);
  332. }