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