cpuidle.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. cpumask_t coupled_cpus;
  69. struct cpuidle_coupled *coupled;
  70. #endif
  71. };
  72. DECLARE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
  73. DECLARE_PER_CPU(struct cpuidle_device, cpuidle_dev);
  74. /**
  75. * cpuidle_get_last_residency - retrieves the last state's residency time
  76. * @dev: the target CPU
  77. */
  78. static inline int cpuidle_get_last_residency(struct cpuidle_device *dev)
  79. {
  80. return dev->last_residency;
  81. }
  82. /****************************
  83. * CPUIDLE DRIVER INTERFACE *
  84. ****************************/
  85. struct cpuidle_driver {
  86. const char *name;
  87. struct module *owner;
  88. int refcnt;
  89. /* used by the cpuidle framework to setup the broadcast timer */
  90. unsigned int bctimer:1;
  91. /* states array must be ordered in decreasing power consumption */
  92. struct cpuidle_state states[CPUIDLE_STATE_MAX];
  93. int state_count;
  94. int safe_state_index;
  95. /* the driver handles the cpus in cpumask */
  96. struct cpumask *cpumask;
  97. };
  98. #ifdef CONFIG_CPU_IDLE
  99. extern void disable_cpuidle(void);
  100. extern bool cpuidle_not_available(struct cpuidle_driver *drv,
  101. struct cpuidle_device *dev);
  102. extern int cpuidle_select(struct cpuidle_driver *drv,
  103. struct cpuidle_device *dev);
  104. extern int cpuidle_enter(struct cpuidle_driver *drv,
  105. struct cpuidle_device *dev, int index);
  106. extern void cpuidle_reflect(struct cpuidle_device *dev, int index);
  107. extern int cpuidle_register_driver(struct cpuidle_driver *drv);
  108. extern struct cpuidle_driver *cpuidle_get_driver(void);
  109. extern struct cpuidle_driver *cpuidle_driver_ref(void);
  110. extern void cpuidle_driver_unref(void);
  111. extern void cpuidle_unregister_driver(struct cpuidle_driver *drv);
  112. extern int cpuidle_register_device(struct cpuidle_device *dev);
  113. extern void cpuidle_unregister_device(struct cpuidle_device *dev);
  114. extern int cpuidle_register(struct cpuidle_driver *drv,
  115. const struct cpumask *const coupled_cpus);
  116. extern void cpuidle_unregister(struct cpuidle_driver *drv);
  117. extern void cpuidle_pause_and_lock(void);
  118. extern void cpuidle_resume_and_unlock(void);
  119. extern void cpuidle_pause(void);
  120. extern void cpuidle_resume(void);
  121. extern int cpuidle_enable_device(struct cpuidle_device *dev);
  122. extern void cpuidle_disable_device(struct cpuidle_device *dev);
  123. extern int cpuidle_play_dead(void);
  124. extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev);
  125. static inline struct cpuidle_device *cpuidle_get_device(void)
  126. {return __this_cpu_read(cpuidle_devices); }
  127. #else
  128. static inline void disable_cpuidle(void) { }
  129. static inline bool cpuidle_not_available(struct cpuidle_driver *drv,
  130. struct cpuidle_device *dev)
  131. {return true; }
  132. static inline int cpuidle_select(struct cpuidle_driver *drv,
  133. struct cpuidle_device *dev)
  134. {return -ENODEV; }
  135. static inline int cpuidle_enter(struct cpuidle_driver *drv,
  136. struct cpuidle_device *dev, int index)
  137. {return -ENODEV; }
  138. static inline void cpuidle_reflect(struct cpuidle_device *dev, int index) { }
  139. static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
  140. {return -ENODEV; }
  141. static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }
  142. static inline struct cpuidle_driver *cpuidle_driver_ref(void) {return NULL; }
  143. static inline void cpuidle_driver_unref(void) {}
  144. static inline void cpuidle_unregister_driver(struct cpuidle_driver *drv) { }
  145. static inline int cpuidle_register_device(struct cpuidle_device *dev)
  146. {return -ENODEV; }
  147. static inline void cpuidle_unregister_device(struct cpuidle_device *dev) { }
  148. static inline int cpuidle_register(struct cpuidle_driver *drv,
  149. const struct cpumask *const coupled_cpus)
  150. {return -ENODEV; }
  151. static inline void cpuidle_unregister(struct cpuidle_driver *drv) { }
  152. static inline void cpuidle_pause_and_lock(void) { }
  153. static inline void cpuidle_resume_and_unlock(void) { }
  154. static inline void cpuidle_pause(void) { }
  155. static inline void cpuidle_resume(void) { }
  156. static inline int cpuidle_enable_device(struct cpuidle_device *dev)
  157. {return -ENODEV; }
  158. static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
  159. static inline int cpuidle_play_dead(void) {return -ENODEV; }
  160. static inline struct cpuidle_driver *cpuidle_get_cpu_driver(
  161. struct cpuidle_device *dev) {return NULL; }
  162. static inline struct cpuidle_device *cpuidle_get_device(void) {return NULL; }
  163. #endif
  164. #if defined(CONFIG_CPU_IDLE) && defined(CONFIG_SUSPEND)
  165. extern int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
  166. struct cpuidle_device *dev);
  167. extern int cpuidle_enter_freeze(struct cpuidle_driver *drv,
  168. struct cpuidle_device *dev);
  169. #else
  170. static inline int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
  171. struct cpuidle_device *dev)
  172. {return -ENODEV; }
  173. static inline int cpuidle_enter_freeze(struct cpuidle_driver *drv,
  174. struct cpuidle_device *dev)
  175. {return -ENODEV; }
  176. #endif
  177. /* kernel/sched/idle.c */
  178. extern void sched_idle_set_state(struct cpuidle_state *idle_state);
  179. extern void default_idle_call(void);
  180. #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
  181. void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a);
  182. #else
  183. static inline void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a)
  184. {
  185. }
  186. #endif
  187. /******************************
  188. * CPUIDLE GOVERNOR INTERFACE *
  189. ******************************/
  190. struct cpuidle_governor {
  191. char name[CPUIDLE_NAME_LEN];
  192. struct list_head governor_list;
  193. unsigned int rating;
  194. int (*enable) (struct cpuidle_driver *drv,
  195. struct cpuidle_device *dev);
  196. void (*disable) (struct cpuidle_driver *drv,
  197. struct cpuidle_device *dev);
  198. int (*select) (struct cpuidle_driver *drv,
  199. struct cpuidle_device *dev);
  200. void (*reflect) (struct cpuidle_device *dev, int index);
  201. struct module *owner;
  202. };
  203. #ifdef CONFIG_CPU_IDLE
  204. extern int cpuidle_register_governor(struct cpuidle_governor *gov);
  205. #else
  206. static inline int cpuidle_register_governor(struct cpuidle_governor *gov)
  207. {return 0;}
  208. #endif
  209. #ifdef CONFIG_ARCH_HAS_CPU_RELAX
  210. #define CPUIDLE_DRIVER_STATE_START 1
  211. #else
  212. #define CPUIDLE_DRIVER_STATE_START 0
  213. #endif
  214. #endif /* _LINUX_CPUIDLE_H */