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 */
36 unsigned long long above
; /* Number of times it's been too deep */
37 unsigned long long below
; /* Number of times it's been too shallow */
39 unsigned long long s2idle_usage
;
40 unsigned long long s2idle_time
; /* in US */
44 struct cpuidle_state
{
45 char name
[CPUIDLE_NAME_LEN
];
46 char desc
[CPUIDLE_DESC_LEN
];
49 unsigned int exit_latency
; /* in US */
50 int power_usage
; /* in mW */
51 unsigned int target_residency
; /* in US */
52 bool disabled
; /* disabled on all CPUs */
54 int (*enter
) (struct cpuidle_device
*dev
,
55 struct cpuidle_driver
*drv
,
58 int (*enter_dead
) (struct cpuidle_device
*dev
, int index
);
61 * CPUs execute ->enter_s2idle with the local tick or entire timekeeping
62 * suspended, so it must not re-enable interrupts at any point (even
63 * temporarily) or attempt to change states of clock event devices.
65 void (*enter_s2idle
) (struct cpuidle_device
*dev
,
66 struct cpuidle_driver
*drv
,
70 /* Idle State Flags */
71 #define CPUIDLE_FLAG_NONE (0x00)
72 #define CPUIDLE_FLAG_POLLING BIT(0) /* polling state */
73 #define CPUIDLE_FLAG_COUPLED BIT(1) /* state applies to multiple cpus */
74 #define CPUIDLE_FLAG_TIMER_STOP BIT(2) /* timer is stopped on this state */
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;
89 struct cpuidle_state_usage states_usage
[CPUIDLE_STATE_MAX
];
90 struct cpuidle_state_kobj
*kobjs
[CPUIDLE_STATE_MAX
];
91 struct cpuidle_driver_kobj
*kobj_driver
;
92 struct cpuidle_device_kobj
*kobj_dev
;
93 struct list_head device_list
;
95 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
96 cpumask_t coupled_cpus
;
97 struct cpuidle_coupled
*coupled
;
101 DECLARE_PER_CPU(struct cpuidle_device
*, cpuidle_devices
);
102 DECLARE_PER_CPU(struct cpuidle_device
, cpuidle_dev
);
104 /****************************
105 * CPUIDLE DRIVER INTERFACE *
106 ****************************/
108 struct cpuidle_driver
{
110 struct module
*owner
;
113 /* used by the cpuidle framework to setup the broadcast timer */
114 unsigned int bctimer
:1;
115 /* states array must be ordered in decreasing power consumption */
116 struct cpuidle_state states
[CPUIDLE_STATE_MAX
];
118 int safe_state_index
;
120 /* the driver handles the cpus in cpumask */
121 struct cpumask
*cpumask
;
124 #ifdef CONFIG_CPU_IDLE
125 extern void disable_cpuidle(void);
126 extern bool cpuidle_not_available(struct cpuidle_driver
*drv
,
127 struct cpuidle_device
*dev
);
129 extern int cpuidle_select(struct cpuidle_driver
*drv
,
130 struct cpuidle_device
*dev
,
132 extern int cpuidle_enter(struct cpuidle_driver
*drv
,
133 struct cpuidle_device
*dev
, int index
);
134 extern void cpuidle_reflect(struct cpuidle_device
*dev
, int index
);
136 extern int cpuidle_register_driver(struct cpuidle_driver
*drv
);
137 extern struct cpuidle_driver
*cpuidle_get_driver(void);
138 extern struct cpuidle_driver
*cpuidle_driver_ref(void);
139 extern void cpuidle_driver_unref(void);
140 extern void cpuidle_unregister_driver(struct cpuidle_driver
*drv
);
141 extern int cpuidle_register_device(struct cpuidle_device
*dev
);
142 extern void cpuidle_unregister_device(struct cpuidle_device
*dev
);
143 extern int cpuidle_register(struct cpuidle_driver
*drv
,
144 const struct cpumask
*const coupled_cpus
);
145 extern void cpuidle_unregister(struct cpuidle_driver
*drv
);
146 extern void cpuidle_pause_and_lock(void);
147 extern void cpuidle_resume_and_unlock(void);
148 extern void cpuidle_pause(void);
149 extern void cpuidle_resume(void);
150 extern int cpuidle_enable_device(struct cpuidle_device
*dev
);
151 extern void cpuidle_disable_device(struct cpuidle_device
*dev
);
152 extern int cpuidle_play_dead(void);
154 extern struct cpuidle_driver
*cpuidle_get_cpu_driver(struct cpuidle_device
*dev
);
155 static inline struct cpuidle_device
*cpuidle_get_device(void)
156 {return __this_cpu_read(cpuidle_devices
); }
158 static inline void disable_cpuidle(void) { }
159 static inline bool cpuidle_not_available(struct cpuidle_driver
*drv
,
160 struct cpuidle_device
*dev
)
162 static inline int cpuidle_select(struct cpuidle_driver
*drv
,
163 struct cpuidle_device
*dev
, bool *stop_tick
)
165 static inline int cpuidle_enter(struct cpuidle_driver
*drv
,
166 struct cpuidle_device
*dev
, int index
)
168 static inline void cpuidle_reflect(struct cpuidle_device
*dev
, int index
) { }
169 static inline int cpuidle_register_driver(struct cpuidle_driver
*drv
)
171 static inline struct cpuidle_driver
*cpuidle_get_driver(void) {return NULL
; }
172 static inline struct cpuidle_driver
*cpuidle_driver_ref(void) {return NULL
; }
173 static inline void cpuidle_driver_unref(void) {}
174 static inline void cpuidle_unregister_driver(struct cpuidle_driver
*drv
) { }
175 static inline int cpuidle_register_device(struct cpuidle_device
*dev
)
177 static inline void cpuidle_unregister_device(struct cpuidle_device
*dev
) { }
178 static inline int cpuidle_register(struct cpuidle_driver
*drv
,
179 const struct cpumask
*const coupled_cpus
)
181 static inline void cpuidle_unregister(struct cpuidle_driver
*drv
) { }
182 static inline void cpuidle_pause_and_lock(void) { }
183 static inline void cpuidle_resume_and_unlock(void) { }
184 static inline void cpuidle_pause(void) { }
185 static inline void cpuidle_resume(void) { }
186 static inline int cpuidle_enable_device(struct cpuidle_device
*dev
)
188 static inline void cpuidle_disable_device(struct cpuidle_device
*dev
) { }
189 static inline int cpuidle_play_dead(void) {return -ENODEV
; }
190 static inline struct cpuidle_driver
*cpuidle_get_cpu_driver(
191 struct cpuidle_device
*dev
) {return NULL
; }
192 static inline struct cpuidle_device
*cpuidle_get_device(void) {return NULL
; }
195 #ifdef CONFIG_CPU_IDLE
196 extern int cpuidle_find_deepest_state(struct cpuidle_driver
*drv
,
197 struct cpuidle_device
*dev
);
198 extern int cpuidle_enter_s2idle(struct cpuidle_driver
*drv
,
199 struct cpuidle_device
*dev
);
200 extern void cpuidle_use_deepest_state(bool enable
);
202 static inline int cpuidle_find_deepest_state(struct cpuidle_driver
*drv
,
203 struct cpuidle_device
*dev
)
205 static inline int cpuidle_enter_s2idle(struct cpuidle_driver
*drv
,
206 struct cpuidle_device
*dev
)
208 static inline void cpuidle_use_deepest_state(bool enable
)
213 /* kernel/sched/idle.c */
214 extern void sched_idle_set_state(struct cpuidle_state
*idle_state
);
215 extern void default_idle_call(void);
217 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
218 void cpuidle_coupled_parallel_barrier(struct cpuidle_device
*dev
, atomic_t
*a
);
220 static inline void cpuidle_coupled_parallel_barrier(struct cpuidle_device
*dev
, atomic_t
*a
)
225 #if defined(CONFIG_CPU_IDLE) && defined(CONFIG_ARCH_HAS_CPU_RELAX)
226 void cpuidle_poll_state_init(struct cpuidle_driver
*drv
);
228 static inline void cpuidle_poll_state_init(struct cpuidle_driver
*drv
) {}
231 /******************************
232 * CPUIDLE GOVERNOR INTERFACE *
233 ******************************/
235 struct cpuidle_governor
{
236 char name
[CPUIDLE_NAME_LEN
];
237 struct list_head governor_list
;
240 int (*enable
) (struct cpuidle_driver
*drv
,
241 struct cpuidle_device
*dev
);
242 void (*disable
) (struct cpuidle_driver
*drv
,
243 struct cpuidle_device
*dev
);
245 int (*select
) (struct cpuidle_driver
*drv
,
246 struct cpuidle_device
*dev
,
248 void (*reflect
) (struct cpuidle_device
*dev
, int index
);
251 #ifdef CONFIG_CPU_IDLE
252 extern int cpuidle_register_governor(struct cpuidle_governor
*gov
);
253 extern int cpuidle_governor_latency_req(unsigned int cpu
);
255 static inline int cpuidle_register_governor(struct cpuidle_governor
*gov
)
259 #define __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, is_retention) \
269 __ret = cpu_pm_enter(); \
271 __ret = low_level_idle_enter(idx); \
279 #define CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx) \
280 __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, 0)
282 #define CPU_PM_CPU_IDLE_ENTER_RETENTION(low_level_idle_enter, idx) \
283 __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, 1)
285 #endif /* _LINUX_CPUIDLE_H */