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