cpuidle.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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/kobject.h>
  15. #include <linux/completion.h>
  16. #include <linux/hrtimer.h>
  17. #define CPUIDLE_STATE_MAX 8
  18. #define CPUIDLE_NAME_LEN 16
  19. #define CPUIDLE_DESC_LEN 32
  20. struct module;
  21. struct cpuidle_device;
  22. struct cpuidle_driver;
  23. /****************************
  24. * CPUIDLE DEVICE INTERFACE *
  25. ****************************/
  26. struct cpuidle_state_usage {
  27. void *driver_data;
  28. unsigned long long disable;
  29. unsigned long long usage;
  30. unsigned long long time; /* in US */
  31. };
  32. struct cpuidle_state {
  33. char name[CPUIDLE_NAME_LEN];
  34. char desc[CPUIDLE_DESC_LEN];
  35. unsigned int flags;
  36. unsigned int exit_latency; /* in US */
  37. int power_usage; /* in mW */
  38. unsigned int target_residency; /* in US */
  39. bool disabled; /* disabled on all CPUs */
  40. int (*enter) (struct cpuidle_device *dev,
  41. struct cpuidle_driver *drv,
  42. int index);
  43. int (*enter_dead) (struct cpuidle_device *dev, int index);
  44. };
  45. /* Idle State Flags */
  46. #define CPUIDLE_FLAG_TIME_VALID (0x01) /* is residency time measurable? */
  47. #define CPUIDLE_FLAG_COUPLED (0x02) /* state applies to multiple cpus */
  48. #define CPUIDLE_DRIVER_FLAGS_MASK (0xFFFF0000)
  49. /**
  50. * cpuidle_get_statedata - retrieves private driver state data
  51. * @st_usage: the state usage statistics
  52. */
  53. static inline void *cpuidle_get_statedata(struct cpuidle_state_usage *st_usage)
  54. {
  55. return st_usage->driver_data;
  56. }
  57. /**
  58. * cpuidle_set_statedata - stores private driver state data
  59. * @st_usage: the state usage statistics
  60. * @data: the private data
  61. */
  62. static inline void
  63. cpuidle_set_statedata(struct cpuidle_state_usage *st_usage, void *data)
  64. {
  65. st_usage->driver_data = data;
  66. }
  67. struct cpuidle_device {
  68. unsigned int registered:1;
  69. unsigned int enabled:1;
  70. unsigned int cpu;
  71. int last_residency;
  72. int state_count;
  73. struct cpuidle_state_usage states_usage[CPUIDLE_STATE_MAX];
  74. struct cpuidle_state_kobj *kobjs[CPUIDLE_STATE_MAX];
  75. struct cpuidle_driver_kobj *kobj_driver;
  76. struct list_head device_list;
  77. struct kobject kobj;
  78. struct completion kobj_unregister;
  79. #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
  80. int safe_state_index;
  81. cpumask_t coupled_cpus;
  82. struct cpuidle_coupled *coupled;
  83. #endif
  84. };
  85. DECLARE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
  86. /**
  87. * cpuidle_get_last_residency - retrieves the last state's residency time
  88. * @dev: the target CPU
  89. *
  90. * NOTE: this value is invalid if CPUIDLE_FLAG_TIME_VALID isn't set
  91. */
  92. static inline int cpuidle_get_last_residency(struct cpuidle_device *dev)
  93. {
  94. return dev->last_residency;
  95. }
  96. /****************************
  97. * CPUIDLE DRIVER INTERFACE *
  98. ****************************/
  99. struct cpuidle_driver {
  100. const char *name;
  101. struct module *owner;
  102. int refcnt;
  103. /* set to 1 to use the core cpuidle time keeping (for all states). */
  104. unsigned int en_core_tk_irqen:1;
  105. /* states array must be ordered in decreasing power consumption */
  106. struct cpuidle_state states[CPUIDLE_STATE_MAX];
  107. int state_count;
  108. int safe_state_index;
  109. };
  110. #ifdef CONFIG_CPU_IDLE
  111. extern void disable_cpuidle(void);
  112. extern int cpuidle_idle_call(void);
  113. extern int cpuidle_register_driver(struct cpuidle_driver *drv);
  114. extern struct cpuidle_driver *cpuidle_get_driver(void);
  115. extern struct cpuidle_driver *cpuidle_driver_ref(void);
  116. extern void cpuidle_driver_unref(void);
  117. extern void cpuidle_unregister_driver(struct cpuidle_driver *drv);
  118. extern int cpuidle_register_device(struct cpuidle_device *dev);
  119. extern void cpuidle_unregister_device(struct cpuidle_device *dev);
  120. extern void cpuidle_pause_and_lock(void);
  121. extern void cpuidle_resume_and_unlock(void);
  122. extern void cpuidle_pause(void);
  123. extern void cpuidle_resume(void);
  124. extern int cpuidle_enable_device(struct cpuidle_device *dev);
  125. extern void cpuidle_disable_device(struct cpuidle_device *dev);
  126. extern int cpuidle_wrap_enter(struct cpuidle_device *dev,
  127. struct cpuidle_driver *drv, int index,
  128. int (*enter)(struct cpuidle_device *dev,
  129. struct cpuidle_driver *drv, int index));
  130. extern int cpuidle_play_dead(void);
  131. extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev);
  132. extern int cpuidle_register_cpu_driver(struct cpuidle_driver *drv, int cpu);
  133. extern void cpuidle_unregister_cpu_driver(struct cpuidle_driver *drv, int cpu);
  134. #else
  135. static inline void disable_cpuidle(void) { }
  136. static inline int cpuidle_idle_call(void) { return -ENODEV; }
  137. static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
  138. {return -ENODEV; }
  139. static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }
  140. static inline struct cpuidle_driver *cpuidle_driver_ref(void) {return NULL; }
  141. static inline void cpuidle_driver_unref(void) {}
  142. static inline void cpuidle_unregister_driver(struct cpuidle_driver *drv) { }
  143. static inline int cpuidle_register_device(struct cpuidle_device *dev)
  144. {return -ENODEV; }
  145. static inline void cpuidle_unregister_device(struct cpuidle_device *dev) { }
  146. static inline void cpuidle_pause_and_lock(void) { }
  147. static inline void cpuidle_resume_and_unlock(void) { }
  148. static inline void cpuidle_pause(void) { }
  149. static inline void cpuidle_resume(void) { }
  150. static inline int cpuidle_enable_device(struct cpuidle_device *dev)
  151. {return -ENODEV; }
  152. static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
  153. static inline int cpuidle_wrap_enter(struct cpuidle_device *dev,
  154. struct cpuidle_driver *drv, int index,
  155. int (*enter)(struct cpuidle_device *dev,
  156. struct cpuidle_driver *drv, int index))
  157. { return -ENODEV; }
  158. static inline int cpuidle_play_dead(void) {return -ENODEV; }
  159. #endif
  160. #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
  161. void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a);
  162. #else
  163. static inline void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a)
  164. {
  165. }
  166. #endif
  167. /******************************
  168. * CPUIDLE GOVERNOR INTERFACE *
  169. ******************************/
  170. struct cpuidle_governor {
  171. char name[CPUIDLE_NAME_LEN];
  172. struct list_head governor_list;
  173. unsigned int rating;
  174. int (*enable) (struct cpuidle_driver *drv,
  175. struct cpuidle_device *dev);
  176. void (*disable) (struct cpuidle_driver *drv,
  177. struct cpuidle_device *dev);
  178. int (*select) (struct cpuidle_driver *drv,
  179. struct cpuidle_device *dev);
  180. void (*reflect) (struct cpuidle_device *dev, int index);
  181. struct module *owner;
  182. };
  183. #ifdef CONFIG_CPU_IDLE
  184. extern int cpuidle_register_governor(struct cpuidle_governor *gov);
  185. extern void cpuidle_unregister_governor(struct cpuidle_governor *gov);
  186. #else
  187. static inline int cpuidle_register_governor(struct cpuidle_governor *gov)
  188. {return 0;}
  189. static inline void cpuidle_unregister_governor(struct cpuidle_governor *gov) { }
  190. #endif
  191. #ifdef CONFIG_ARCH_HAS_CPU_RELAX
  192. #define CPUIDLE_DRIVER_STATE_START 1
  193. #else
  194. #define CPUIDLE_DRIVER_STATE_START 0
  195. #endif
  196. #endif /* _LINUX_CPUIDLE_H */