driver.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 "cpuidle.h"
  14. DEFINE_SPINLOCK(cpuidle_driver_lock);
  15. static void __cpuidle_set_cpu_driver(struct cpuidle_driver *drv, int cpu);
  16. static struct cpuidle_driver * __cpuidle_get_cpu_driver(int cpu);
  17. static void set_power_states(struct cpuidle_driver *drv)
  18. {
  19. int i;
  20. /*
  21. * cpuidle driver should set the drv->power_specified bit
  22. * before registering if the driver provides
  23. * power_usage numbers.
  24. *
  25. * If power_specified is not set,
  26. * we fill in power_usage with decreasing values as the
  27. * cpuidle code has an implicit assumption that state Cn
  28. * uses less power than C(n-1).
  29. *
  30. * With CONFIG_ARCH_HAS_CPU_RELAX, C0 is already assigned
  31. * an power value of -1. So we use -2, -3, etc, for other
  32. * c-states.
  33. */
  34. for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++)
  35. drv->states[i].power_usage = -1 - i;
  36. }
  37. static void __cpuidle_driver_init(struct cpuidle_driver *drv)
  38. {
  39. drv->refcnt = 0;
  40. if (!drv->power_specified)
  41. set_power_states(drv);
  42. }
  43. static int __cpuidle_register_driver(struct cpuidle_driver *drv, int cpu)
  44. {
  45. if (!drv || !drv->state_count)
  46. return -EINVAL;
  47. if (cpuidle_disabled())
  48. return -ENODEV;
  49. if (__cpuidle_get_cpu_driver(cpu))
  50. return -EBUSY;
  51. __cpuidle_driver_init(drv);
  52. __cpuidle_set_cpu_driver(drv, cpu);
  53. return 0;
  54. }
  55. static void __cpuidle_unregister_driver(struct cpuidle_driver *drv, int cpu)
  56. {
  57. if (drv != __cpuidle_get_cpu_driver(cpu))
  58. return;
  59. if (!WARN_ON(drv->refcnt > 0))
  60. __cpuidle_set_cpu_driver(NULL, cpu);
  61. }
  62. #ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS
  63. static DEFINE_PER_CPU(struct cpuidle_driver *, cpuidle_drivers);
  64. static void __cpuidle_set_cpu_driver(struct cpuidle_driver *drv, int cpu)
  65. {
  66. per_cpu(cpuidle_drivers, cpu) = drv;
  67. }
  68. static struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu)
  69. {
  70. return per_cpu(cpuidle_drivers, cpu);
  71. }
  72. static void __cpuidle_unregister_all_cpu_driver(struct cpuidle_driver *drv)
  73. {
  74. int cpu;
  75. for_each_present_cpu(cpu)
  76. __cpuidle_unregister_driver(drv, cpu);
  77. }
  78. static int __cpuidle_register_all_cpu_driver(struct cpuidle_driver *drv)
  79. {
  80. int ret = 0;
  81. int i, cpu;
  82. for_each_present_cpu(cpu) {
  83. ret = __cpuidle_register_driver(drv, cpu);
  84. if (ret)
  85. break;
  86. }
  87. if (ret)
  88. for_each_present_cpu(i) {
  89. if (i == cpu)
  90. break;
  91. __cpuidle_unregister_driver(drv, i);
  92. }
  93. return ret;
  94. }
  95. int cpuidle_register_cpu_driver(struct cpuidle_driver *drv, int cpu)
  96. {
  97. int ret;
  98. spin_lock(&cpuidle_driver_lock);
  99. ret = __cpuidle_register_driver(drv, cpu);
  100. spin_unlock(&cpuidle_driver_lock);
  101. return ret;
  102. }
  103. void cpuidle_unregister_cpu_driver(struct cpuidle_driver *drv, int cpu)
  104. {
  105. spin_lock(&cpuidle_driver_lock);
  106. __cpuidle_unregister_driver(drv, cpu);
  107. spin_unlock(&cpuidle_driver_lock);
  108. }
  109. /**
  110. * cpuidle_register_driver - registers a driver
  111. * @drv: the driver
  112. */
  113. int cpuidle_register_driver(struct cpuidle_driver *drv)
  114. {
  115. int ret;
  116. spin_lock(&cpuidle_driver_lock);
  117. ret = __cpuidle_register_all_cpu_driver(drv);
  118. spin_unlock(&cpuidle_driver_lock);
  119. return ret;
  120. }
  121. EXPORT_SYMBOL_GPL(cpuidle_register_driver);
  122. /**
  123. * cpuidle_unregister_driver - unregisters a driver
  124. * @drv: the driver
  125. */
  126. void cpuidle_unregister_driver(struct cpuidle_driver *drv)
  127. {
  128. spin_lock(&cpuidle_driver_lock);
  129. __cpuidle_unregister_all_cpu_driver(drv);
  130. spin_unlock(&cpuidle_driver_lock);
  131. }
  132. EXPORT_SYMBOL_GPL(cpuidle_unregister_driver);
  133. #else
  134. static struct cpuidle_driver *cpuidle_curr_driver;
  135. static inline void __cpuidle_set_cpu_driver(struct cpuidle_driver *drv, int cpu)
  136. {
  137. cpuidle_curr_driver = drv;
  138. }
  139. static inline struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu)
  140. {
  141. return cpuidle_curr_driver;
  142. }
  143. /**
  144. * cpuidle_register_driver - registers a driver
  145. * @drv: the driver
  146. */
  147. int cpuidle_register_driver(struct cpuidle_driver *drv)
  148. {
  149. int ret, cpu;
  150. cpu = get_cpu();
  151. spin_lock(&cpuidle_driver_lock);
  152. ret = __cpuidle_register_driver(drv, cpu);
  153. spin_unlock(&cpuidle_driver_lock);
  154. put_cpu();
  155. return ret;
  156. }
  157. EXPORT_SYMBOL_GPL(cpuidle_register_driver);
  158. /**
  159. * cpuidle_unregister_driver - unregisters a driver
  160. * @drv: the driver
  161. */
  162. void cpuidle_unregister_driver(struct cpuidle_driver *drv)
  163. {
  164. int cpu;
  165. cpu = get_cpu();
  166. spin_lock(&cpuidle_driver_lock);
  167. __cpuidle_unregister_driver(drv, cpu);
  168. spin_unlock(&cpuidle_driver_lock);
  169. put_cpu();
  170. }
  171. EXPORT_SYMBOL_GPL(cpuidle_unregister_driver);
  172. #endif
  173. /**
  174. * cpuidle_get_driver - return the current driver
  175. */
  176. struct cpuidle_driver *cpuidle_get_driver(void)
  177. {
  178. struct cpuidle_driver *drv;
  179. int cpu;
  180. cpu = get_cpu();
  181. drv = __cpuidle_get_cpu_driver(cpu);
  182. put_cpu();
  183. return drv;
  184. }
  185. EXPORT_SYMBOL_GPL(cpuidle_get_driver);
  186. /**
  187. * cpuidle_get_cpu_driver - return the driver tied with a cpu
  188. */
  189. struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev)
  190. {
  191. if (!dev)
  192. return NULL;
  193. return __cpuidle_get_cpu_driver(dev->cpu);
  194. }
  195. EXPORT_SYMBOL_GPL(cpuidle_get_cpu_driver);
  196. struct cpuidle_driver *cpuidle_driver_ref(void)
  197. {
  198. struct cpuidle_driver *drv;
  199. spin_lock(&cpuidle_driver_lock);
  200. drv = cpuidle_get_driver();
  201. drv->refcnt++;
  202. spin_unlock(&cpuidle_driver_lock);
  203. return drv;
  204. }
  205. void cpuidle_driver_unref(void)
  206. {
  207. struct cpuidle_driver *drv = cpuidle_get_driver();
  208. spin_lock(&cpuidle_driver_lock);
  209. if (drv && !WARN_ON(drv->refcnt <= 0))
  210. drv->refcnt--;
  211. spin_unlock(&cpuidle_driver_lock);
  212. }