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 | ||
4202735e | 32 | struct cpuidle_state_usage { |
dc7fd275 | 33 | unsigned long long disable; |
4202735e DD |
34 | unsigned long long usage; |
35 | unsigned long long time; /* in US */ | |
64bdff69 RW |
36 | #ifdef CONFIG_SUSPEND |
37 | unsigned long long s2idle_usage; | |
38 | unsigned long long s2idle_time; /* in US */ | |
39 | #endif | |
4202735e DD |
40 | }; |
41 | ||
4f86d3a8 LB |
42 | struct cpuidle_state { |
43 | char name[CPUIDLE_NAME_LEN]; | |
4fcb2fcd | 44 | char desc[CPUIDLE_DESC_LEN]; |
4f86d3a8 LB |
45 | |
46 | unsigned int flags; | |
47 | unsigned int exit_latency; /* in US */ | |
02401c06 | 48 | int power_usage; /* in mW */ |
4f86d3a8 | 49 | unsigned int target_residency; /* in US */ |
cbc9ef02 | 50 | bool disabled; /* disabled on all CPUs */ |
4f86d3a8 | 51 | |
4f86d3a8 | 52 | int (*enter) (struct cpuidle_device *dev, |
46bcfad7 | 53 | struct cpuidle_driver *drv, |
e978aa7d | 54 | int index); |
1a022e3f BO |
55 | |
56 | int (*enter_dead) (struct cpuidle_device *dev, int index); | |
124cf911 RW |
57 | |
58 | /* | |
28ba086e | 59 | * CPUs execute ->enter_s2idle with the local tick or entire timekeeping |
124cf911 RW |
60 | * suspended, so it must not re-enable interrupts at any point (even |
61 | * temporarily) or attempt to change states of clock event devices. | |
62 | */ | |
28ba086e | 63 | void (*enter_s2idle) (struct cpuidle_device *dev, |
124cf911 RW |
64 | struct cpuidle_driver *drv, |
65 | int index); | |
4f86d3a8 LB |
66 | }; |
67 | ||
68 | /* Idle State Flags */ | |
9e9fc6f0 | 69 | #define CPUIDLE_FLAG_NONE (0x00) |
dc2251bf | 70 | #define CPUIDLE_FLAG_POLLING (0x01) /* polling state */ |
4126c019 | 71 | #define CPUIDLE_FLAG_COUPLED (0x02) /* state applies to multiple cpus */ |
b60e6a0e | 72 | #define CPUIDLE_FLAG_TIMER_STOP (0x04) /* timer is stopped on this state */ |
4f86d3a8 LB |
73 | |
74 | #define CPUIDLE_DRIVER_FLAGS_MASK (0xFFFF0000) | |
75 | ||
728ce22b | 76 | struct cpuidle_device_kobj; |
1a706438 DL |
77 | struct cpuidle_state_kobj; |
78 | struct cpuidle_driver_kobj; | |
728ce22b | 79 | |
4f86d3a8 | 80 | struct cpuidle_device { |
dcb84f33 | 81 | unsigned int registered:1; |
b5556a67 | 82 | unsigned int enabled:1; |
bb8313b6 | 83 | unsigned int use_deepest_state:1; |
5f26bdce | 84 | unsigned int poll_time_limit:1; |
4f86d3a8 LB |
85 | unsigned int cpu; |
86 | ||
87 | int last_residency; | |
4202735e | 88 | struct cpuidle_state_usage states_usage[CPUIDLE_STATE_MAX]; |
4f86d3a8 | 89 | struct cpuidle_state_kobj *kobjs[CPUIDLE_STATE_MAX]; |
bf4d1b5d | 90 | struct cpuidle_driver_kobj *kobj_driver; |
728ce22b | 91 | struct cpuidle_device_kobj *kobj_dev; |
4f86d3a8 | 92 | struct list_head device_list; |
4126c019 CC |
93 | |
94 | #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED | |
4126c019 CC |
95 | cpumask_t coupled_cpus; |
96 | struct cpuidle_coupled *coupled; | |
97 | #endif | |
4f86d3a8 LB |
98 | }; |
99 | ||
100 | DECLARE_PER_CPU(struct cpuidle_device *, cpuidle_devices); | |
f08dbf8a | 101 | DECLARE_PER_CPU(struct cpuidle_device, cpuidle_dev); |
4f86d3a8 | 102 | |
4f86d3a8 LB |
103 | /**************************** |
104 | * CPUIDLE DRIVER INTERFACE * | |
105 | ****************************/ | |
106 | ||
107 | struct cpuidle_driver { | |
db70b044 | 108 | const char *name; |
4f86d3a8 | 109 | struct module *owner; |
42f67f2a | 110 | int refcnt; |
46bcfad7 | 111 | |
a06df062 DL |
112 | /* used by the cpuidle framework to setup the broadcast timer */ |
113 | unsigned int bctimer:1; | |
8aef33a7 | 114 | /* states array must be ordered in decreasing power consumption */ |
46bcfad7 DD |
115 | struct cpuidle_state states[CPUIDLE_STATE_MAX]; |
116 | int state_count; | |
117 | int safe_state_index; | |
82467a5a DL |
118 | |
119 | /* the driver handles the cpus in cpumask */ | |
6587fca2 | 120 | struct cpumask *cpumask; |
4f86d3a8 LB |
121 | }; |
122 | ||
123 | #ifdef CONFIG_CPU_IDLE | |
d91ee586 | 124 | extern void disable_cpuidle(void); |
ef2b22ac RW |
125 | extern bool cpuidle_not_available(struct cpuidle_driver *drv, |
126 | struct cpuidle_device *dev); | |
907e30f1 | 127 | |
907e30f1 | 128 | extern int cpuidle_select(struct cpuidle_driver *drv, |
45f1ff59 RW |
129 | struct cpuidle_device *dev, |
130 | bool *stop_tick); | |
907e30f1 DL |
131 | extern int cpuidle_enter(struct cpuidle_driver *drv, |
132 | struct cpuidle_device *dev, int index); | |
133 | extern void cpuidle_reflect(struct cpuidle_device *dev, int index); | |
134 | ||
4f86d3a8 | 135 | extern int cpuidle_register_driver(struct cpuidle_driver *drv); |
6e797a07 RW |
136 | extern struct cpuidle_driver *cpuidle_get_driver(void); |
137 | extern struct cpuidle_driver *cpuidle_driver_ref(void); | |
138 | extern void cpuidle_driver_unref(void); | |
4f86d3a8 LB |
139 | extern void cpuidle_unregister_driver(struct cpuidle_driver *drv); |
140 | extern int cpuidle_register_device(struct cpuidle_device *dev); | |
141 | extern void cpuidle_unregister_device(struct cpuidle_device *dev); | |
4c637b21 DL |
142 | extern int cpuidle_register(struct cpuidle_driver *drv, |
143 | const struct cpumask *const coupled_cpus); | |
144 | extern void cpuidle_unregister(struct cpuidle_driver *drv); | |
4f86d3a8 LB |
145 | extern void cpuidle_pause_and_lock(void); |
146 | extern void cpuidle_resume_and_unlock(void); | |
8651f97b PM |
147 | extern void cpuidle_pause(void); |
148 | extern void cpuidle_resume(void); | |
4f86d3a8 LB |
149 | extern int cpuidle_enable_device(struct cpuidle_device *dev); |
150 | extern void cpuidle_disable_device(struct cpuidle_device *dev); | |
1a022e3f BO |
151 | extern int cpuidle_play_dead(void); |
152 | ||
bf4d1b5d | 153 | extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev); |
9bd616e3 CM |
154 | static inline struct cpuidle_device *cpuidle_get_device(void) |
155 | {return __this_cpu_read(cpuidle_devices); } | |
4f86d3a8 | 156 | #else |
d91ee586 | 157 | static inline void disable_cpuidle(void) { } |
ef2b22ac RW |
158 | static inline bool cpuidle_not_available(struct cpuidle_driver *drv, |
159 | struct cpuidle_device *dev) | |
160 | {return true; } | |
907e30f1 | 161 | static inline int cpuidle_select(struct cpuidle_driver *drv, |
45f1ff59 | 162 | struct cpuidle_device *dev, bool *stop_tick) |
907e30f1 DL |
163 | {return -ENODEV; } |
164 | static inline int cpuidle_enter(struct cpuidle_driver *drv, | |
165 | struct cpuidle_device *dev, int index) | |
166 | {return -ENODEV; } | |
167 | static inline void cpuidle_reflect(struct cpuidle_device *dev, int index) { } | |
4f86d3a8 | 168 | static inline int cpuidle_register_driver(struct cpuidle_driver *drv) |
6b2c676b | 169 | {return -ENODEV; } |
752138df | 170 | static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; } |
6e797a07 RW |
171 | static inline struct cpuidle_driver *cpuidle_driver_ref(void) {return NULL; } |
172 | static inline void cpuidle_driver_unref(void) {} | |
4f86d3a8 LB |
173 | static inline void cpuidle_unregister_driver(struct cpuidle_driver *drv) { } |
174 | static inline int cpuidle_register_device(struct cpuidle_device *dev) | |
6b2c676b | 175 | {return -ENODEV; } |
4f86d3a8 | 176 | static inline void cpuidle_unregister_device(struct cpuidle_device *dev) { } |
4c637b21 DL |
177 | static inline int cpuidle_register(struct cpuidle_driver *drv, |
178 | const struct cpumask *const coupled_cpus) | |
179 | {return -ENODEV; } | |
180 | static inline void cpuidle_unregister(struct cpuidle_driver *drv) { } | |
4f86d3a8 LB |
181 | static inline void cpuidle_pause_and_lock(void) { } |
182 | static inline void cpuidle_resume_and_unlock(void) { } | |
8651f97b PM |
183 | static inline void cpuidle_pause(void) { } |
184 | static inline void cpuidle_resume(void) { } | |
4f86d3a8 | 185 | static inline int cpuidle_enable_device(struct cpuidle_device *dev) |
6b2c676b | 186 | {return -ENODEV; } |
4f86d3a8 | 187 | static inline void cpuidle_disable_device(struct cpuidle_device *dev) { } |
1a022e3f | 188 | static inline int cpuidle_play_dead(void) {return -ENODEV; } |
87e9b9f1 RW |
189 | static inline struct cpuidle_driver *cpuidle_get_cpu_driver( |
190 | struct cpuidle_device *dev) {return NULL; } | |
9bd616e3 | 191 | static inline struct cpuidle_device *cpuidle_get_device(void) {return NULL; } |
87e9b9f1 RW |
192 | #endif |
193 | ||
bb8313b6 | 194 | #ifdef CONFIG_CPU_IDLE |
87e9b9f1 RW |
195 | extern int cpuidle_find_deepest_state(struct cpuidle_driver *drv, |
196 | struct cpuidle_device *dev); | |
28ba086e | 197 | extern int cpuidle_enter_s2idle(struct cpuidle_driver *drv, |
87e9b9f1 | 198 | struct cpuidle_device *dev); |
bb8313b6 | 199 | extern void cpuidle_use_deepest_state(bool enable); |
87e9b9f1 | 200 | #else |
ef2b22ac RW |
201 | static inline int cpuidle_find_deepest_state(struct cpuidle_driver *drv, |
202 | struct cpuidle_device *dev) | |
203 | {return -ENODEV; } | |
28ba086e | 204 | static inline int cpuidle_enter_s2idle(struct cpuidle_driver *drv, |
ef2b22ac RW |
205 | struct cpuidle_device *dev) |
206 | {return -ENODEV; } | |
bb8313b6 JP |
207 | static inline void cpuidle_use_deepest_state(bool enable) |
208 | { | |
209 | } | |
4f86d3a8 LB |
210 | #endif |
211 | ||
faad3849 RW |
212 | /* kernel/sched/idle.c */ |
213 | extern void sched_idle_set_state(struct cpuidle_state *idle_state); | |
827a5aef | 214 | extern void default_idle_call(void); |
faad3849 | 215 | |
20ff51a3 CC |
216 | #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED |
217 | void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a); | |
c7a9b09b AB |
218 | #else |
219 | static inline void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a) | |
220 | { | |
221 | } | |
20ff51a3 CC |
222 | #endif |
223 | ||
d7212cfb | 224 | #if defined(CONFIG_CPU_IDLE) && defined(CONFIG_ARCH_HAS_CPU_RELAX) |
1b39e3f8 | 225 | void cpuidle_poll_state_init(struct cpuidle_driver *drv); |
34c2f65b | 226 | #else |
1b39e3f8 | 227 | static inline void cpuidle_poll_state_init(struct cpuidle_driver *drv) {} |
34c2f65b RW |
228 | #endif |
229 | ||
4f86d3a8 LB |
230 | /****************************** |
231 | * CPUIDLE GOVERNOR INTERFACE * | |
232 | ******************************/ | |
233 | ||
234 | struct cpuidle_governor { | |
235 | char name[CPUIDLE_NAME_LEN]; | |
236 | struct list_head governor_list; | |
237 | unsigned int rating; | |
238 | ||
46bcfad7 DD |
239 | int (*enable) (struct cpuidle_driver *drv, |
240 | struct cpuidle_device *dev); | |
241 | void (*disable) (struct cpuidle_driver *drv, | |
242 | struct cpuidle_device *dev); | |
4f86d3a8 | 243 | |
46bcfad7 | 244 | int (*select) (struct cpuidle_driver *drv, |
45f1ff59 RW |
245 | struct cpuidle_device *dev, |
246 | bool *stop_tick); | |
e978aa7d | 247 | void (*reflect) (struct cpuidle_device *dev, int index); |
4f86d3a8 LB |
248 | }; |
249 | ||
250 | #ifdef CONFIG_CPU_IDLE | |
4f86d3a8 | 251 | extern int cpuidle_register_governor(struct cpuidle_governor *gov); |
0fc784fb | 252 | extern int cpuidle_governor_latency_req(unsigned int cpu); |
4f86d3a8 | 253 | #else |
4f86d3a8 LB |
254 | static inline int cpuidle_register_governor(struct cpuidle_governor *gov) |
255 | {return 0;} | |
4f86d3a8 LB |
256 | #endif |
257 | ||
db50a74d PP |
258 | #define __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, is_retention) \ |
259 | ({ \ | |
260 | int __ret = 0; \ | |
261 | \ | |
262 | if (!idx) { \ | |
263 | cpu_do_idle(); \ | |
264 | return idx; \ | |
265 | } \ | |
266 | \ | |
267 | if (!is_retention) \ | |
268 | __ret = cpu_pm_enter(); \ | |
269 | if (!__ret) { \ | |
270 | __ret = low_level_idle_enter(idx); \ | |
271 | if (!is_retention) \ | |
272 | cpu_pm_exit(); \ | |
273 | } \ | |
274 | \ | |
275 | __ret ? -1 : idx; \ | |
220276e0 SH |
276 | }) |
277 | ||
db50a74d PP |
278 | #define CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx) \ |
279 | __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, 0) | |
280 | ||
281 | #define CPU_PM_CPU_IDLE_ENTER_RETENTION(low_level_idle_enter, idx) \ | |
282 | __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, 1) | |
283 | ||
4f86d3a8 | 284 | #endif /* _LINUX_CPUIDLE_H */ |