cpuidle.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * cpuidle.h - a generic framework for CPU idle power management
  3. *
  4. * (C) 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. #ifndef _LINUX_CPUIDLE_H
  11. #define _LINUX_CPUIDLE_H
  12. #include <linux/percpu.h>
  13. #include <linux/list.h>
  14. #include <linux/hrtimer.h>
  15. #define CPUIDLE_STATE_MAX 10
  16. #define CPUIDLE_NAME_LEN 16
  17. #define CPUIDLE_DESC_LEN 32
  18. struct module;
  19. struct cpuidle_device;
  20. struct cpuidle_driver;
  21. /****************************
  22. * CPUIDLE DEVICE INTERFACE *
  23. ****************************/
  24. struct cpuidle_state_usage {
  25. unsigned long long disable;
  26. unsigned long long usage;
  27. unsigned long long time; /* in US */
  28. };
  29. struct cpuidle_state {
  30. char name[CPUIDLE_NAME_LEN];
  31. char desc[CPUIDLE_DESC_LEN];
  32. unsigned int flags;
  33. unsigned int exit_latency; /* in US */
  34. int power_usage; /* in mW */
  35. unsigned int target_residency; /* in US */
  36. bool disabled; /* disabled on all CPUs */
  37. int (*enter) (struct cpuidle_device *dev,
  38. struct cpuidle_driver *drv,
  39. int index);
  40. int (*enter_dead) (struct cpuidle_device *dev, int index);
  41. /*
  42. * CPUs execute ->enter_freeze with the local tick or entire timekeeping
  43. * suspended, so it must not re-enable interrupts at any point (even
  44. * temporarily) or attempt to change states of clock event devices.
  45. */
  46. void (*enter_freeze) (struct cpuidle_device *dev,
  47. struct cpuidle_driver *drv,
  48. int index);
  49. };
  50. /* Idle State Flags */
  51. #define CPUIDLE_FLAG_COUPLED (0x02) /* state applies to multiple cpus */
  52. #define CPUIDLE_FLAG_TIMER_STOP (0x04) /* timer is stopped on this state */
  53. #define CPUIDLE_DRIVER_FLAGS_MASK (0xFFFF0000)
  54. struct cpuidle_device_kobj;
  55. struct cpuidle_state_kobj;
  56. struct cpuidle_driver_kobj;
  57. struct cpuidle_device {
  58. unsigned int registered:1;
  59. unsigned int enabled:1;
  60. unsigned int cpu;
  61. int last_residency;
  62. struct cpuidle_state_usage states_usage[CPUIDLE_STATE_MAX];
  63. struct cpuidle_state_kobj *kobjs[CPUIDLE_STATE_MAX];
  64. struct cpuidle_driver_kobj *kobj_driver;
  65. struct cpuidle_device_kobj *kobj_dev;
  66. struct list_head device_list;
  67. #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
  68. int safe_state_index;
  69. cpumask_t coupled_cpus;
  70. struct cpuidle_coupled *coupled;
  71. #endif
  72. };
  73. DECLARE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
  74. DECLARE_PER_CPU(struct cpuidle_device, cpuidle_dev);
  75. /**
  76. * cpuidle_get_last_residency - retrieves the last state's residency time
  77. * @dev: the target CPU
  78. */
  79. static inline int cpuidle_get_last_residency(struct cpuidle_device *dev)
  80. {
  81. return dev->last_residency;
  82. }
  83. /****************************
  84. * CPUIDLE DRIVER INTERFACE *
  85. ****************************/
  86. struct cpuidle_driver {
  87. const char *name;
  88. struct module *owner;
  89. int refcnt;
  90. /* used by the cpuidle framework to setup the broadcast timer */
  91. unsigned int bctimer:1;
  92. /* states array must be ordered in decreasing power consumption */
  93. struct cpuidle_state states[CPUIDLE_STATE_MAX];
  94. int state_count;
  95. int safe_state_index;
  96. /* the driver handles the cpus in cpumask */
  97. struct cpumask *cpumask;
  98. };
  99. #ifdef CONFIG_CPU_IDLE
  100. extern void disable_cpuidle(void);
  101. extern bool cpuidle_not_available(struct cpuidle_driver *drv,
  102. struct cpuidle_device *dev);
  103. extern int cpuidle_select(struct cpuidle_driver *drv,
  104. struct cpuidle_device *dev);
  105. extern int cpuidle_enter(struct cpuidle_driver *drv,
  106. struct cpuidle_device *dev, int index);
  107. extern void cpuidle_reflect(struct cpuidle_device *dev, int index);
  108. extern int cpuidle_register_driver(struct cpuidle_driver *drv);
  109. extern struct cpuidle_driver *cpuidle_get_driver(void);
  110. extern struct cpuidle_driver *cpuidle_driver_ref(void);
  111. extern void cpuidle_driver_unref(void);
  112. extern void cpuidle_unregister_driver(struct cpuidle_driver *drv);
  113. extern int cpuidle_register_device(struct cpuidle_device *dev);
  114. extern void cpuidle_unregister_device(struct cpuidle_device *dev);
  115. extern int cpuidle_register(struct cpuidle_driver *drv,
  116. const struct cpumask *const coupled_cpus);
  117. extern void cpuidle_unregister(struct cpuidle_driver *drv);
  118. extern void cpuidle_pause_and_lock(void);
  119. extern void cpuidle_resume_and_unlock(void);
  120. extern void cpuidle_pause(void);
  121. extern void cpuidle_resume(void);
  122. extern int cpuidle_enable_device(struct cpuidle_device *dev);
  123. extern void cpuidle_disable_device(struct cpuidle_device *dev);
  124. extern int cpuidle_play_dead(void);
  125. extern int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
  126. struct cpuidle_device *dev);
  127. extern int cpuidle_enter_freeze(struct cpuidle_driver *drv,
  128. struct cpuidle_device *dev);
  129. extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev);
  130. #else
  131. static inline void disable_cpuidle(void) { }
  132. static inline bool cpuidle_not_available(struct cpuidle_driver *drv,
  133. struct cpuidle_device *dev)
  134. {return true; }
  135. static inline int cpuidle_select(struct cpuidle_driver *drv,
  136. struct cpuidle_device *dev)
  137. {return -ENODEV; }
  138. static inline int cpuidle_enter(struct cpuidle_driver *drv,
  139. struct cpuidle_device *dev, int index)
  140. {return -ENODEV; }
  141. static inline void cpuidle_reflect(struct cpuidle_device *dev, int index) { }
  142. static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
  143. {return -ENODEV; }
  144. static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }
  145. static inline struct cpuidle_driver *cpuidle_driver_ref(void) {return NULL; }
  146. static inline void cpuidle_driver_unref(void) {}
  147. static inline void cpuidle_unregister_driver(struct cpuidle_driver *drv) { }
  148. static inline int cpuidle_register_device(struct cpuidle_device *dev)
  149. {return -ENODEV; }
  150. static inline void cpuidle_unregister_device(struct cpuidle_device *dev) { }
  151. static inline int cpuidle_register(struct cpuidle_driver *drv,
  152. const struct cpumask *const coupled_cpus)
  153. {return -ENODEV; }
  154. static inline void cpuidle_unregister(struct cpuidle_driver *drv) { }
  155. static inline void cpuidle_pause_and_lock(void) { }
  156. static inline void cpuidle_resume_and_unlock(void) { }
  157. static inline void cpuidle_pause(void) { }
  158. static inline void cpuidle_resume(void) { }
  159. static inline int cpuidle_enable_device(struct cpuidle_device *dev)
  160. {return -ENODEV; }
  161. static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
  162. static inline int cpuidle_play_dead(void) {return -ENODEV; }
  163. static inline int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
  164. struct cpuidle_device *dev)
  165. {return -ENODEV; }
  166. static inline int cpuidle_enter_freeze(struct cpuidle_driver *drv,
  167. struct cpuidle_device *dev)
  168. {return -ENODEV; }
  169. static inline struct cpuidle_driver *cpuidle_get_cpu_driver(
  170. struct cpuidle_device *dev) {return NULL; }
  171. #endif
  172. #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
  173. void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a);
  174. #else
  175. static inline void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a)
  176. {
  177. }
  178. #endif
  179. /******************************
  180. * CPUIDLE GOVERNOR INTERFACE *
  181. ******************************/
  182. struct cpuidle_governor {
  183. char name[CPUIDLE_NAME_LEN];
  184. struct list_head governor_list;
  185. unsigned int rating;
  186. int (*enable) (struct cpuidle_driver *drv,
  187. struct cpuidle_device *dev);
  188. void (*disable) (struct cpuidle_driver *drv,
  189. struct cpuidle_device *dev);
  190. int (*select) (struct cpuidle_driver *drv,
  191. struct cpuidle_device *dev);
  192. void (*reflect) (struct cpuidle_device *dev, int index);
  193. struct module *owner;
  194. };
  195. #ifdef CONFIG_CPU_IDLE
  196. extern int cpuidle_register_governor(struct cpuidle_governor *gov);
  197. #else
  198. static inline int cpuidle_register_governor(struct cpuidle_governor *gov)
  199. {return 0;}
  200. #endif
  201. #ifdef CONFIG_ARCH_HAS_CPU_RELAX
  202. #define CPUIDLE_DRIVER_STATE_START 1
  203. #else
  204. #define CPUIDLE_DRIVER_STATE_START 0
  205. #endif
  206. #endif /* _LINUX_CPUIDLE_H */