2 * cpuidle.h - a generic framework for CPU idle power management
4 * (C) 2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
5 * Shaohua Li <shaohua.li@intel.com>
6 * Adam Belay <abelay@novell.com>
8 * This code is licenced under the GPL.
11 #ifndef _LINUX_CPUIDLE_H
12 #define _LINUX_CPUIDLE_H
14 #include <linux/percpu.h>
15 #include <linux/list.h>
16 #include <linux/hrtimer.h>
18 #define CPUIDLE_STATE_MAX 10
19 #define CPUIDLE_NAME_LEN 16
20 #define CPUIDLE_DESC_LEN 32
24 struct cpuidle_device;
25 struct cpuidle_driver;
28 /****************************
29 * CPUIDLE DEVICE INTERFACE *
30 ****************************/
32 struct cpuidle_state_usage {
33 unsigned long long disable;
34 unsigned long long usage;
35 unsigned long long time; /* in US */
37 unsigned long long s2idle_usage;
38 unsigned long long s2idle_time; /* in US */
42 struct cpuidle_state {
43 char name[CPUIDLE_NAME_LEN];
44 char desc[CPUIDLE_DESC_LEN];
47 unsigned int exit_latency; /* in US */
48 int power_usage; /* in mW */
49 unsigned int target_residency; /* in US */
50 bool disabled; /* disabled on all CPUs */
52 int (*enter) (struct cpuidle_device *dev,
53 struct cpuidle_driver *drv,
56 int (*enter_dead) (struct cpuidle_device *dev, int index);
59 * CPUs execute ->enter_s2idle with the local tick or entire timekeeping
60 * suspended, so it must not re-enable interrupts at any point (even
61 * temporarily) or attempt to change states of clock event devices.
63 void (*enter_s2idle) (struct cpuidle_device *dev,
64 struct cpuidle_driver *drv,
68 /* Idle State Flags */
69 #define CPUIDLE_FLAG_NONE (0x00)
70 #define CPUIDLE_FLAG_POLLING (0x01) /* polling state */
71 #define CPUIDLE_FLAG_COUPLED (0x02) /* state applies to multiple cpus */
72 #define CPUIDLE_FLAG_TIMER_STOP (0x04) /* timer is stopped on this state */
74 #define CPUIDLE_DRIVER_FLAGS_MASK (0xFFFF0000)
76 struct cpuidle_device_kobj;
77 struct cpuidle_state_kobj;
78 struct cpuidle_driver_kobj;
80 struct cpuidle_device {
81 unsigned int registered:1;
82 unsigned int enabled:1;
83 unsigned int use_deepest_state:1;
84 unsigned int poll_time_limit:1;
88 struct cpuidle_state_usage states_usage[CPUIDLE_STATE_MAX];
89 struct cpuidle_state_kobj *kobjs[CPUIDLE_STATE_MAX];
90 struct cpuidle_driver_kobj *kobj_driver;
91 struct cpuidle_device_kobj *kobj_dev;
92 struct list_head device_list;
94 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
95 cpumask_t coupled_cpus;
96 struct cpuidle_coupled *coupled;
100 DECLARE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
101 DECLARE_PER_CPU(struct cpuidle_device, cpuidle_dev);
104 * cpuidle_get_last_residency - retrieves the last state's residency time
105 * @dev: the target CPU
107 static inline int cpuidle_get_last_residency(struct cpuidle_device *dev)
109 return dev->last_residency;
113 /****************************
114 * CPUIDLE DRIVER INTERFACE *
115 ****************************/
117 struct cpuidle_driver {
119 struct module *owner;
122 /* used by the cpuidle framework to setup the broadcast timer */
123 unsigned int bctimer:1;
124 /* states array must be ordered in decreasing power consumption */
125 struct cpuidle_state states[CPUIDLE_STATE_MAX];
127 int safe_state_index;
129 /* the driver handles the cpus in cpumask */
130 struct cpumask *cpumask;
133 #ifdef CONFIG_CPU_IDLE
134 extern void disable_cpuidle(void);
135 extern bool cpuidle_not_available(struct cpuidle_driver *drv,
136 struct cpuidle_device *dev);
138 extern int cpuidle_select(struct cpuidle_driver *drv,
139 struct cpuidle_device *dev,
141 extern int cpuidle_enter(struct cpuidle_driver *drv,
142 struct cpuidle_device *dev, int index);
143 extern void cpuidle_reflect(struct cpuidle_device *dev, int index);
145 extern int cpuidle_register_driver(struct cpuidle_driver *drv);
146 extern struct cpuidle_driver *cpuidle_get_driver(void);
147 extern struct cpuidle_driver *cpuidle_driver_ref(void);
148 extern void cpuidle_driver_unref(void);
149 extern void cpuidle_unregister_driver(struct cpuidle_driver *drv);
150 extern int cpuidle_register_device(struct cpuidle_device *dev);
151 extern void cpuidle_unregister_device(struct cpuidle_device *dev);
152 extern int cpuidle_register(struct cpuidle_driver *drv,
153 const struct cpumask *const coupled_cpus);
154 extern void cpuidle_unregister(struct cpuidle_driver *drv);
155 extern void cpuidle_pause_and_lock(void);
156 extern void cpuidle_resume_and_unlock(void);
157 extern void cpuidle_pause(void);
158 extern void cpuidle_resume(void);
159 extern int cpuidle_enable_device(struct cpuidle_device *dev);
160 extern void cpuidle_disable_device(struct cpuidle_device *dev);
161 extern int cpuidle_play_dead(void);
163 extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev);
164 static inline struct cpuidle_device *cpuidle_get_device(void)
165 {return __this_cpu_read(cpuidle_devices); }
167 static inline void disable_cpuidle(void) { }
168 static inline bool cpuidle_not_available(struct cpuidle_driver *drv,
169 struct cpuidle_device *dev)
171 static inline int cpuidle_select(struct cpuidle_driver *drv,
172 struct cpuidle_device *dev, bool *stop_tick)
174 static inline int cpuidle_enter(struct cpuidle_driver *drv,
175 struct cpuidle_device *dev, int index)
177 static inline void cpuidle_reflect(struct cpuidle_device *dev, int index) { }
178 static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
180 static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }
181 static inline struct cpuidle_driver *cpuidle_driver_ref(void) {return NULL; }
182 static inline void cpuidle_driver_unref(void) {}
183 static inline void cpuidle_unregister_driver(struct cpuidle_driver *drv) { }
184 static inline int cpuidle_register_device(struct cpuidle_device *dev)
186 static inline void cpuidle_unregister_device(struct cpuidle_device *dev) { }
187 static inline int cpuidle_register(struct cpuidle_driver *drv,
188 const struct cpumask *const coupled_cpus)
190 static inline void cpuidle_unregister(struct cpuidle_driver *drv) { }
191 static inline void cpuidle_pause_and_lock(void) { }
192 static inline void cpuidle_resume_and_unlock(void) { }
193 static inline void cpuidle_pause(void) { }
194 static inline void cpuidle_resume(void) { }
195 static inline int cpuidle_enable_device(struct cpuidle_device *dev)
197 static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
198 static inline int cpuidle_play_dead(void) {return -ENODEV; }
199 static inline struct cpuidle_driver *cpuidle_get_cpu_driver(
200 struct cpuidle_device *dev) {return NULL; }
201 static inline struct cpuidle_device *cpuidle_get_device(void) {return NULL; }
204 #ifdef CONFIG_CPU_IDLE
205 extern int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
206 struct cpuidle_device *dev);
207 extern int cpuidle_enter_s2idle(struct cpuidle_driver *drv,
208 struct cpuidle_device *dev);
209 extern void cpuidle_use_deepest_state(bool enable);
211 static inline int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
212 struct cpuidle_device *dev)
214 static inline int cpuidle_enter_s2idle(struct cpuidle_driver *drv,
215 struct cpuidle_device *dev)
217 static inline void cpuidle_use_deepest_state(bool enable)
222 /* kernel/sched/idle.c */
223 extern void sched_idle_set_state(struct cpuidle_state *idle_state);
224 extern void default_idle_call(void);
226 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
227 void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a);
229 static inline void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a)
234 #if defined(CONFIG_CPU_IDLE) && defined(CONFIG_ARCH_HAS_CPU_RELAX)
235 void cpuidle_poll_state_init(struct cpuidle_driver *drv);
237 static inline void cpuidle_poll_state_init(struct cpuidle_driver *drv) {}
240 /******************************
241 * CPUIDLE GOVERNOR INTERFACE *
242 ******************************/
244 struct cpuidle_governor {
245 char name[CPUIDLE_NAME_LEN];
246 struct list_head governor_list;
249 int (*enable) (struct cpuidle_driver *drv,
250 struct cpuidle_device *dev);
251 void (*disable) (struct cpuidle_driver *drv,
252 struct cpuidle_device *dev);
254 int (*select) (struct cpuidle_driver *drv,
255 struct cpuidle_device *dev,
257 void (*reflect) (struct cpuidle_device *dev, int index);
260 #ifdef CONFIG_CPU_IDLE
261 extern int cpuidle_register_governor(struct cpuidle_governor *gov);
262 extern int cpuidle_governor_latency_req(unsigned int cpu);
264 static inline int cpuidle_register_governor(struct cpuidle_governor *gov)
268 #define __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, is_retention) \
278 __ret = cpu_pm_enter(); \
280 __ret = low_level_idle_enter(idx); \
288 #define CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx) \
289 __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, 0)
291 #define CPU_PM_CPU_IDLE_ENTER_RETENTION(low_level_idle_enter, idx) \
292 __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, 1)
294 #endif /* _LINUX_CPUIDLE_H */