driver.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. static struct cpuidle_driver *cpuidle_curr_driver;
  15. DEFINE_SPINLOCK(cpuidle_driver_lock);
  16. int cpuidle_driver_refcount;
  17. static void __cpuidle_register_driver(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. if (!drv->power_specified) {
  35. for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++)
  36. drv->states[i].power_usage = -1 - i;
  37. }
  38. }
  39. /**
  40. * cpuidle_register_driver - registers a driver
  41. * @drv: the driver
  42. */
  43. int cpuidle_register_driver(struct cpuidle_driver *drv)
  44. {
  45. if (!drv || !drv->state_count)
  46. return -EINVAL;
  47. if (cpuidle_disabled())
  48. return -ENODEV;
  49. spin_lock(&cpuidle_driver_lock);
  50. if (cpuidle_curr_driver) {
  51. spin_unlock(&cpuidle_driver_lock);
  52. return -EBUSY;
  53. }
  54. __cpuidle_register_driver(drv);
  55. cpuidle_curr_driver = drv;
  56. spin_unlock(&cpuidle_driver_lock);
  57. return 0;
  58. }
  59. EXPORT_SYMBOL_GPL(cpuidle_register_driver);
  60. /**
  61. * cpuidle_get_driver - return the current driver
  62. */
  63. struct cpuidle_driver *cpuidle_get_driver(void)
  64. {
  65. return cpuidle_curr_driver;
  66. }
  67. EXPORT_SYMBOL_GPL(cpuidle_get_driver);
  68. /**
  69. * cpuidle_unregister_driver - unregisters a driver
  70. * @drv: the driver
  71. */
  72. void cpuidle_unregister_driver(struct cpuidle_driver *drv)
  73. {
  74. if (drv != cpuidle_curr_driver) {
  75. WARN(1, "invalid cpuidle_unregister_driver(%s)\n",
  76. drv->name);
  77. return;
  78. }
  79. spin_lock(&cpuidle_driver_lock);
  80. if (!WARN_ON(cpuidle_driver_refcount > 0))
  81. cpuidle_curr_driver = NULL;
  82. spin_unlock(&cpuidle_driver_lock);
  83. }
  84. EXPORT_SYMBOL_GPL(cpuidle_unregister_driver);
  85. struct cpuidle_driver *cpuidle_driver_ref(void)
  86. {
  87. struct cpuidle_driver *drv;
  88. spin_lock(&cpuidle_driver_lock);
  89. drv = cpuidle_curr_driver;
  90. cpuidle_driver_refcount++;
  91. spin_unlock(&cpuidle_driver_lock);
  92. return drv;
  93. }
  94. void cpuidle_driver_unref(void)
  95. {
  96. spin_lock(&cpuidle_driver_lock);
  97. if (!WARN_ON(cpuidle_driver_refcount <= 0))
  98. cpuidle_driver_refcount--;
  99. spin_unlock(&cpuidle_driver_lock);
  100. }