driver.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. static void __cpuidle_set_cpu_driver(struct cpuidle_driver *drv, int cpu);
  18. static struct cpuidle_driver * __cpuidle_get_cpu_driver(int cpu);
  19. static void cpuidle_setup_broadcast_timer(void *arg)
  20. {
  21. int cpu = smp_processor_id();
  22. clockevents_notify((long)(arg), &cpu);
  23. }
  24. static void __cpuidle_driver_init(struct cpuidle_driver *drv, int cpu)
  25. {
  26. int i;
  27. drv->refcnt = 0;
  28. for (i = drv->state_count - 1; i >= 0 ; i--) {
  29. if (!(drv->states[i].flags & CPUIDLE_FLAG_TIMER_STOP))
  30. continue;
  31. drv->bctimer = 1;
  32. on_each_cpu_mask(get_cpu_mask(cpu), cpuidle_setup_broadcast_timer,
  33. (void *)CLOCK_EVT_NOTIFY_BROADCAST_ON, 1);
  34. break;
  35. }
  36. }
  37. static int __cpuidle_register_driver(struct cpuidle_driver *drv, int cpu)
  38. {
  39. if (!drv || !drv->state_count)
  40. return -EINVAL;
  41. if (cpuidle_disabled())
  42. return -ENODEV;
  43. if (__cpuidle_get_cpu_driver(cpu))
  44. return -EBUSY;
  45. __cpuidle_driver_init(drv, cpu);
  46. __cpuidle_set_cpu_driver(drv, cpu);
  47. return 0;
  48. }
  49. static void __cpuidle_unregister_driver(struct cpuidle_driver *drv, int cpu)
  50. {
  51. if (drv != __cpuidle_get_cpu_driver(cpu))
  52. return;
  53. if (!WARN_ON(drv->refcnt > 0))
  54. __cpuidle_set_cpu_driver(NULL, cpu);
  55. if (drv->bctimer) {
  56. drv->bctimer = 0;
  57. on_each_cpu_mask(get_cpu_mask(cpu), cpuidle_setup_broadcast_timer,
  58. (void *)CLOCK_EVT_NOTIFY_BROADCAST_OFF, 1);
  59. }
  60. }
  61. #ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS
  62. static DEFINE_PER_CPU(struct cpuidle_driver *, cpuidle_drivers);
  63. static void __cpuidle_set_cpu_driver(struct cpuidle_driver *drv, int cpu)
  64. {
  65. per_cpu(cpuidle_drivers, cpu) = drv;
  66. }
  67. static struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu)
  68. {
  69. return per_cpu(cpuidle_drivers, cpu);
  70. }
  71. static void __cpuidle_unregister_all_cpu_driver(struct cpuidle_driver *drv)
  72. {
  73. int cpu;
  74. for_each_present_cpu(cpu)
  75. __cpuidle_unregister_driver(drv, cpu);
  76. }
  77. static int __cpuidle_register_all_cpu_driver(struct cpuidle_driver *drv)
  78. {
  79. int ret = 0;
  80. int i, cpu;
  81. for_each_present_cpu(cpu) {
  82. ret = __cpuidle_register_driver(drv, cpu);
  83. if (ret)
  84. break;
  85. }
  86. if (ret)
  87. for_each_present_cpu(i) {
  88. if (i == cpu)
  89. break;
  90. __cpuidle_unregister_driver(drv, i);
  91. }
  92. return ret;
  93. }
  94. int cpuidle_register_cpu_driver(struct cpuidle_driver *drv, int cpu)
  95. {
  96. int ret;
  97. spin_lock(&cpuidle_driver_lock);
  98. ret = __cpuidle_register_driver(drv, cpu);
  99. spin_unlock(&cpuidle_driver_lock);
  100. return ret;
  101. }
  102. void cpuidle_unregister_cpu_driver(struct cpuidle_driver *drv, int cpu)
  103. {
  104. spin_lock(&cpuidle_driver_lock);
  105. __cpuidle_unregister_driver(drv, cpu);
  106. spin_unlock(&cpuidle_driver_lock);
  107. }
  108. /**
  109. * cpuidle_register_driver - registers a driver
  110. * @drv: the driver
  111. */
  112. int cpuidle_register_driver(struct cpuidle_driver *drv)
  113. {
  114. int ret;
  115. spin_lock(&cpuidle_driver_lock);
  116. ret = __cpuidle_register_all_cpu_driver(drv);
  117. spin_unlock(&cpuidle_driver_lock);
  118. return ret;
  119. }
  120. EXPORT_SYMBOL_GPL(cpuidle_register_driver);
  121. /**
  122. * cpuidle_unregister_driver - unregisters a driver
  123. * @drv: the driver
  124. */
  125. void cpuidle_unregister_driver(struct cpuidle_driver *drv)
  126. {
  127. spin_lock(&cpuidle_driver_lock);
  128. __cpuidle_unregister_all_cpu_driver(drv);
  129. spin_unlock(&cpuidle_driver_lock);
  130. }
  131. EXPORT_SYMBOL_GPL(cpuidle_unregister_driver);
  132. #else
  133. static struct cpuidle_driver *cpuidle_curr_driver;
  134. static inline void __cpuidle_set_cpu_driver(struct cpuidle_driver *drv, int cpu)
  135. {
  136. cpuidle_curr_driver = drv;
  137. }
  138. static inline struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu)
  139. {
  140. return cpuidle_curr_driver;
  141. }
  142. /**
  143. * cpuidle_register_driver - registers a driver
  144. * @drv: the driver
  145. */
  146. int cpuidle_register_driver(struct cpuidle_driver *drv)
  147. {
  148. int ret, cpu;
  149. cpu = get_cpu();
  150. spin_lock(&cpuidle_driver_lock);
  151. ret = __cpuidle_register_driver(drv, cpu);
  152. spin_unlock(&cpuidle_driver_lock);
  153. put_cpu();
  154. return ret;
  155. }
  156. EXPORT_SYMBOL_GPL(cpuidle_register_driver);
  157. /**
  158. * cpuidle_unregister_driver - unregisters a driver
  159. * @drv: the driver
  160. */
  161. void cpuidle_unregister_driver(struct cpuidle_driver *drv)
  162. {
  163. int cpu;
  164. cpu = get_cpu();
  165. spin_lock(&cpuidle_driver_lock);
  166. __cpuidle_unregister_driver(drv, cpu);
  167. spin_unlock(&cpuidle_driver_lock);
  168. put_cpu();
  169. }
  170. EXPORT_SYMBOL_GPL(cpuidle_unregister_driver);
  171. #endif
  172. /**
  173. * cpuidle_get_driver - return the current driver
  174. */
  175. struct cpuidle_driver *cpuidle_get_driver(void)
  176. {
  177. struct cpuidle_driver *drv;
  178. int cpu;
  179. cpu = get_cpu();
  180. drv = __cpuidle_get_cpu_driver(cpu);
  181. put_cpu();
  182. return drv;
  183. }
  184. EXPORT_SYMBOL_GPL(cpuidle_get_driver);
  185. /**
  186. * cpuidle_get_cpu_driver - return the driver tied with a cpu
  187. */
  188. struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev)
  189. {
  190. if (!dev)
  191. return NULL;
  192. return __cpuidle_get_cpu_driver(dev->cpu);
  193. }
  194. EXPORT_SYMBOL_GPL(cpuidle_get_cpu_driver);
  195. struct cpuidle_driver *cpuidle_driver_ref(void)
  196. {
  197. struct cpuidle_driver *drv;
  198. spin_lock(&cpuidle_driver_lock);
  199. drv = cpuidle_get_driver();
  200. drv->refcnt++;
  201. spin_unlock(&cpuidle_driver_lock);
  202. return drv;
  203. }
  204. void cpuidle_driver_unref(void)
  205. {
  206. struct cpuidle_driver *drv = cpuidle_get_driver();
  207. spin_lock(&cpuidle_driver_lock);
  208. if (drv && !WARN_ON(drv->refcnt <= 0))
  209. drv->refcnt--;
  210. spin_unlock(&cpuidle_driver_lock);
  211. }