driver.c 4.4 KB

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