cpuidle.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. #include <linux/android_kabi.h>
  16. #define CPUIDLE_STATE_MAX 10
  17. #define CPUIDLE_NAME_LEN 16
  18. #define CPUIDLE_DESC_LEN 32
  19. struct module;
  20. struct cpuidle_device;
  21. struct cpuidle_driver;
  22. /****************************
  23. * CPUIDLE DEVICE INTERFACE *
  24. ****************************/
  25. #define CPUIDLE_STATE_DISABLED_BY_USER BIT(0)
  26. #define CPUIDLE_STATE_DISABLED_BY_DRIVER BIT(1)
  27. struct cpuidle_state_usage {
  28. unsigned long long disable;
  29. unsigned long long usage;
  30. u64 time_ns;
  31. unsigned long long above; /* Number of times it's been too deep */
  32. unsigned long long below; /* Number of times it's been too shallow */
  33. unsigned long long rejected; /* Number of times idle entry was rejected */
  34. #ifdef CONFIG_SUSPEND
  35. unsigned long long s2idle_usage;
  36. unsigned long long s2idle_time; /* in US */
  37. #endif
  38. };
  39. struct cpuidle_state {
  40. char name[CPUIDLE_NAME_LEN];
  41. char desc[CPUIDLE_DESC_LEN];
  42. u64 exit_latency_ns;
  43. u64 target_residency_ns;
  44. unsigned int flags;
  45. unsigned int exit_latency; /* in US */
  46. int power_usage; /* in mW */
  47. unsigned int target_residency; /* in US */
  48. int (*enter) (struct cpuidle_device *dev,
  49. struct cpuidle_driver *drv,
  50. int index);
  51. int (*enter_dead) (struct cpuidle_device *dev, int index);
  52. /*
  53. * CPUs execute ->enter_s2idle with the local tick or entire timekeeping
  54. * suspended, so it must not re-enable interrupts at any point (even
  55. * temporarily) or attempt to change states of clock event devices.
  56. *
  57. * This callback may point to the same function as ->enter if all of
  58. * the above requirements are met by it.
  59. */
  60. int (*enter_s2idle)(struct cpuidle_device *dev,
  61. struct cpuidle_driver *drv,
  62. int index);
  63. };
  64. /* Idle State Flags */
  65. #define CPUIDLE_FLAG_NONE (0x00)
  66. #define CPUIDLE_FLAG_POLLING BIT(0) /* polling state */
  67. #define CPUIDLE_FLAG_COUPLED BIT(1) /* state applies to multiple cpus */
  68. #define CPUIDLE_FLAG_TIMER_STOP BIT(2) /* timer is stopped on this state */
  69. #define CPUIDLE_FLAG_UNUSABLE BIT(3) /* avoid using this state */
  70. #define CPUIDLE_FLAG_OFF BIT(4) /* disable this state by default */
  71. #define CPUIDLE_FLAG_TLB_FLUSHED BIT(5) /* idle-state flushes TLBs */
  72. #define CPUIDLE_FLAG_RCU_IDLE BIT(6) /* idle-state takes care of RCU */
  73. struct cpuidle_device_kobj;
  74. struct cpuidle_state_kobj;
  75. struct cpuidle_driver_kobj;
  76. struct cpuidle_device {
  77. unsigned int registered:1;
  78. unsigned int enabled:1;
  79. unsigned int poll_time_limit:1;
  80. unsigned int cpu;
  81. ktime_t next_hrtimer;
  82. int last_state_idx;
  83. u64 last_residency_ns;
  84. u64 poll_limit_ns;
  85. u64 forced_idle_latency_limit_ns;
  86. struct cpuidle_state_usage states_usage[CPUIDLE_STATE_MAX];
  87. struct cpuidle_state_kobj *kobjs[CPUIDLE_STATE_MAX];
  88. struct cpuidle_driver_kobj *kobj_driver;
  89. struct cpuidle_device_kobj *kobj_dev;
  90. struct list_head device_list;
  91. #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
  92. cpumask_t coupled_cpus;
  93. struct cpuidle_coupled *coupled;
  94. #endif
  95. ANDROID_KABI_RESERVE(1);
  96. };
  97. DECLARE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
  98. DECLARE_PER_CPU(struct cpuidle_device, cpuidle_dev);
  99. /****************************
  100. * CPUIDLE DRIVER INTERFACE *
  101. ****************************/
  102. struct cpuidle_driver {
  103. const char *name;
  104. struct module *owner;
  105. /* used by the cpuidle framework to setup the broadcast timer */
  106. unsigned int bctimer:1;
  107. /* states array must be ordered in decreasing power consumption */
  108. struct cpuidle_state states[CPUIDLE_STATE_MAX];
  109. int state_count;
  110. int safe_state_index;
  111. /* the driver handles the cpus in cpumask */
  112. struct cpumask *cpumask;
  113. /* preferred governor to switch at register time */
  114. const char *governor;
  115. ANDROID_KABI_RESERVE(1);
  116. };
  117. #ifdef CONFIG_CPU_IDLE
  118. extern void disable_cpuidle(void);
  119. extern bool cpuidle_not_available(struct cpuidle_driver *drv,
  120. struct cpuidle_device *dev);
  121. extern int cpuidle_select(struct cpuidle_driver *drv,
  122. struct cpuidle_device *dev,
  123. bool *stop_tick);
  124. extern int cpuidle_enter(struct cpuidle_driver *drv,
  125. struct cpuidle_device *dev, int index);
  126. extern void cpuidle_reflect(struct cpuidle_device *dev, int index);
  127. extern u64 cpuidle_poll_time(struct cpuidle_driver *drv,
  128. struct cpuidle_device *dev);
  129. extern int cpuidle_register_driver(struct cpuidle_driver *drv);
  130. extern struct cpuidle_driver *cpuidle_get_driver(void);
  131. extern void cpuidle_driver_state_disabled(struct cpuidle_driver *drv, int idx,
  132. bool disable);
  133. extern void cpuidle_unregister_driver(struct cpuidle_driver *drv);
  134. extern int cpuidle_register_device(struct cpuidle_device *dev);
  135. extern void cpuidle_unregister_device(struct cpuidle_device *dev);
  136. extern int cpuidle_register(struct cpuidle_driver *drv,
  137. const struct cpumask *const coupled_cpus);
  138. extern void cpuidle_unregister(struct cpuidle_driver *drv);
  139. extern void cpuidle_pause_and_lock(void);
  140. extern void cpuidle_resume_and_unlock(void);
  141. extern void cpuidle_pause(void);
  142. extern void cpuidle_resume(void);
  143. extern int cpuidle_enable_device(struct cpuidle_device *dev);
  144. extern void cpuidle_disable_device(struct cpuidle_device *dev);
  145. extern int cpuidle_play_dead(void);
  146. extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev);
  147. static inline struct cpuidle_device *cpuidle_get_device(void)
  148. {return __this_cpu_read(cpuidle_devices); }
  149. #else
  150. static inline void disable_cpuidle(void) { }
  151. static inline bool cpuidle_not_available(struct cpuidle_driver *drv,
  152. struct cpuidle_device *dev)
  153. {return true; }
  154. static inline int cpuidle_select(struct cpuidle_driver *drv,
  155. struct cpuidle_device *dev, bool *stop_tick)
  156. {return -ENODEV; }
  157. static inline int cpuidle_enter(struct cpuidle_driver *drv,
  158. struct cpuidle_device *dev, int index)
  159. {return -ENODEV; }
  160. static inline void cpuidle_reflect(struct cpuidle_device *dev, int index) { }
  161. static inline u64 cpuidle_poll_time(struct cpuidle_driver *drv,
  162. struct cpuidle_device *dev)
  163. {return 0; }
  164. static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
  165. {return -ENODEV; }
  166. static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }
  167. static inline void cpuidle_driver_state_disabled(struct cpuidle_driver *drv,
  168. int idx, bool disable) { }
  169. static inline void cpuidle_unregister_driver(struct cpuidle_driver *drv) { }
  170. static inline int cpuidle_register_device(struct cpuidle_device *dev)
  171. {return -ENODEV; }
  172. static inline void cpuidle_unregister_device(struct cpuidle_device *dev) { }
  173. static inline int cpuidle_register(struct cpuidle_driver *drv,
  174. const struct cpumask *const coupled_cpus)
  175. {return -ENODEV; }
  176. static inline void cpuidle_unregister(struct cpuidle_driver *drv) { }
  177. static inline void cpuidle_pause_and_lock(void) { }
  178. static inline void cpuidle_resume_and_unlock(void) { }
  179. static inline void cpuidle_pause(void) { }
  180. static inline void cpuidle_resume(void) { }
  181. static inline int cpuidle_enable_device(struct cpuidle_device *dev)
  182. {return -ENODEV; }
  183. static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
  184. static inline int cpuidle_play_dead(void) {return -ENODEV; }
  185. static inline struct cpuidle_driver *cpuidle_get_cpu_driver(
  186. struct cpuidle_device *dev) {return NULL; }
  187. static inline struct cpuidle_device *cpuidle_get_device(void) {return NULL; }
  188. #endif
  189. #ifdef CONFIG_CPU_IDLE
  190. extern int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
  191. struct cpuidle_device *dev,
  192. u64 latency_limit_ns);
  193. extern int cpuidle_enter_s2idle(struct cpuidle_driver *drv,
  194. struct cpuidle_device *dev);
  195. extern void cpuidle_use_deepest_state(u64 latency_limit_ns);
  196. #else
  197. static inline int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
  198. struct cpuidle_device *dev,
  199. u64 latency_limit_ns)
  200. {return -ENODEV; }
  201. static inline int cpuidle_enter_s2idle(struct cpuidle_driver *drv,
  202. struct cpuidle_device *dev)
  203. {return -ENODEV; }
  204. static inline void cpuidle_use_deepest_state(u64 latency_limit_ns)
  205. {
  206. }
  207. #endif
  208. /* kernel/sched/idle.c */
  209. extern void sched_idle_set_state(struct cpuidle_state *idle_state);
  210. extern void default_idle_call(void);
  211. #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
  212. void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a);
  213. #else
  214. static inline void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a)
  215. {
  216. }
  217. #endif
  218. #if defined(CONFIG_CPU_IDLE) && defined(CONFIG_ARCH_HAS_CPU_RELAX)
  219. void cpuidle_poll_state_init(struct cpuidle_driver *drv);
  220. #else
  221. static inline void cpuidle_poll_state_init(struct cpuidle_driver *drv) {}
  222. #endif
  223. /******************************
  224. * CPUIDLE GOVERNOR INTERFACE *
  225. ******************************/
  226. struct cpuidle_governor {
  227. char name[CPUIDLE_NAME_LEN];
  228. struct list_head governor_list;
  229. unsigned int rating;
  230. int (*enable) (struct cpuidle_driver *drv,
  231. struct cpuidle_device *dev);
  232. void (*disable) (struct cpuidle_driver *drv,
  233. struct cpuidle_device *dev);
  234. int (*select) (struct cpuidle_driver *drv,
  235. struct cpuidle_device *dev,
  236. bool *stop_tick);
  237. void (*reflect) (struct cpuidle_device *dev, int index);
  238. };
  239. extern int cpuidle_register_governor(struct cpuidle_governor *gov);
  240. extern s64 cpuidle_governor_latency_req(unsigned int cpu);
  241. #define __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, \
  242. idx, \
  243. state, \
  244. is_retention) \
  245. ({ \
  246. int __ret = 0; \
  247. \
  248. if (!idx) { \
  249. cpu_do_idle(); \
  250. return idx; \
  251. } \
  252. \
  253. if (!is_retention) \
  254. __ret = cpu_pm_enter(); \
  255. if (!__ret) { \
  256. __ret = low_level_idle_enter(state); \
  257. if (!is_retention) \
  258. cpu_pm_exit(); \
  259. } \
  260. \
  261. __ret ? -1 : idx; \
  262. })
  263. #define CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx) \
  264. __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, idx, 0)
  265. #define CPU_PM_CPU_IDLE_ENTER_RETENTION(low_level_idle_enter, idx) \
  266. __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, idx, 1)
  267. #define CPU_PM_CPU_IDLE_ENTER_PARAM(low_level_idle_enter, idx, state) \
  268. __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, state, 0)
  269. #define CPU_PM_CPU_IDLE_ENTER_RETENTION_PARAM(low_level_idle_enter, idx, state) \
  270. __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, state, 1)
  271. #endif /* _LINUX_CPUIDLE_H */