driver.c 2.6 KB

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