cpuidle.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 use_deepest_state:1;
  61. unsigned int cpu;
  62. int last_residency;
  63. struct cpuidle_state_usage states_usage[CPUIDLE_STATE_MAX];
  64. struct cpuidle_state_kobj *kobjs[CPUIDLE_STATE_MAX];
  65. struct cpuidle_driver_kobj *kobj_driver;
  66. struct cpuidle_device_kobj *kobj_dev;
  67. struct list_head device_list;
  68. #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
  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 struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev);
  126. static inline struct cpuidle_device *cpuidle_get_device(void)
  127. {return __this_cpu_read(cpuidle_devices); }
  128. #else
  129. static inline void disable_cpuidle(void) { }
  130. static inline bool cpuidle_not_available(struct cpuidle_driver *drv,
  131. struct cpuidle_device *dev)
  132. {return true; }
  133. static inline int cpuidle_select(struct cpuidle_driver *drv,
  134. struct cpuidle_device *dev)
  135. {return -ENODEV; }
  136. static inline int cpuidle_enter(struct cpuidle_driver *drv,
  137. struct cpuidle_device *dev, int index)
  138. {return -ENODEV; }
  139. static inline void cpuidle_reflect(struct cpuidle_device *dev, int index) { }
  140. static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
  141. {return -ENODEV; }
  142. static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }
  143. static inline struct cpuidle_driver *cpuidle_driver_ref(void) {return NULL; }
  144. static inline void cpuidle_driver_unref(void) {}
  145. static inline void cpuidle_unregister_driver(struct cpuidle_driver *drv) { }
  146. static inline int cpuidle_register_device(struct cpuidle_device *dev)
  147. {return -ENODEV; }
  148. static inline void cpuidle_unregister_device(struct cpuidle_device *dev) { }
  149. static inline int cpuidle_register(struct cpuidle_driver *drv,
  150. const struct cpumask *const coupled_cpus)
  151. {return -ENODEV; }
  152. static inline void cpuidle_unregister(struct cpuidle_driver *drv) { }
  153. static inline void cpuidle_pause_and_lock(void) { }
  154. static inline void cpuidle_resume_and_unlock(void) { }
  155. static inline void cpuidle_pause(void) { }
  156. static inline void cpuidle_resume(void) { }
  157. static inline int cpuidle_enable_device(struct cpuidle_device *dev)
  158. {return -ENODEV; }
  159. static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
  160. static inline int cpuidle_play_dead(void) {return -ENODEV; }
  161. static inline struct cpuidle_driver *cpuidle_get_cpu_driver(
  162. struct cpuidle_device *dev) {return NULL; }
  163. static inline struct cpuidle_device *cpuidle_get_device(void) {return NULL; }
  164. #endif
  165. #ifdef CONFIG_CPU_IDLE
  166. extern int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
  167. struct cpuidle_device *dev);
  168. extern int cpuidle_enter_freeze(struct cpuidle_driver *drv,
  169. struct cpuidle_device *dev);
  170. extern void cpuidle_use_deepest_state(bool enable);
  171. #else
  172. static inline int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
  173. struct cpuidle_device *dev)
  174. {return -ENODEV; }
  175. static inline int cpuidle_enter_freeze(struct cpuidle_driver *drv,
  176. struct cpuidle_device *dev)
  177. {return -ENODEV; }
  178. static inline void cpuidle_use_deepest_state(bool enable)
  179. {
  180. }
  181. #endif
  182. /* kernel/sched/idle.c */
  183. extern void sched_idle_set_state(struct cpuidle_state *idle_state);
  184. extern void default_idle_call(void);
  185. #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
  186. void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a);
  187. #else
  188. static inline void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a)
  189. {
  190. }
  191. #endif
  192. /******************************
  193. * CPUIDLE GOVERNOR INTERFACE *
  194. ******************************/
  195. struct cpuidle_governor {
  196. char name[CPUIDLE_NAME_LEN];
  197. struct list_head governor_list;
  198. unsigned int rating;
  199. int (*enable) (struct cpuidle_driver *drv,
  200. struct cpuidle_device *dev);
  201. void (*disable) (struct cpuidle_driver *drv,
  202. struct cpuidle_device *dev);
  203. int (*select) (struct cpuidle_driver *drv,
  204. struct cpuidle_device *dev);
  205. void (*reflect) (struct cpuidle_device *dev, int index);
  206. };
  207. #ifdef CONFIG_CPU_IDLE
  208. extern int cpuidle_register_governor(struct cpuidle_governor *gov);
  209. #else
  210. static inline int cpuidle_register_governor(struct cpuidle_governor *gov)
  211. {return 0;}
  212. #endif
  213. #ifdef CONFIG_ARCH_HAS_CPU_RELAX
  214. #define CPUIDLE_DRIVER_STATE_START 1
  215. #else
  216. #define CPUIDLE_DRIVER_STATE_START 0
  217. #endif
  218. #define CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx) \
  219. ({ \
  220. int __ret; \
  221. \
  222. if (!idx) { \
  223. cpu_do_idle(); \
  224. return idx; \
  225. } \
  226. \
  227. __ret = cpu_pm_enter(); \
  228. if (!__ret) { \
  229. __ret = low_level_idle_enter(idx); \
  230. cpu_pm_exit(); \
  231. } \
  232. \
  233. __ret ? -1 : idx; \
  234. })
  235. #endif /* _LINUX_CPUIDLE_H */