cpuidle.h 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * cpuidle.h - a generic framework for CPU idle power management
 *
 * (C) 2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
 *          Shaohua Li <shaohua.li@intel.com>
 *          Adam Belay <abelay@novell.com>
 *
 * This code is licenced under the GPL.
 */

#ifndef _LINUX_CPUIDLE_H
#define _LINUX_CPUIDLE_H

#include <linux/percpu.h>
#include <linux/list.h>
16
#include <linux/hrtimer.h>
17

18
#define CPUIDLE_STATE_MAX	10
19
#define CPUIDLE_NAME_LEN	16
20
#define CPUIDLE_DESC_LEN	32
21

22 23
struct module;

24
struct cpuidle_device;
25
struct cpuidle_driver;
26 27 28 29 30 31


/****************************
 * CPUIDLE DEVICE INTERFACE *
 ****************************/

32 33 34
#define CPUIDLE_STATE_DISABLED_BY_USER		BIT(0)
#define CPUIDLE_STATE_DISABLED_BY_DRIVER	BIT(1)

35
struct cpuidle_state_usage {
36
	unsigned long long	disable;
37
	unsigned long long	usage;
38
	u64			time_ns;
39 40
	unsigned long long	above; /* Number of times it's been too deep */
	unsigned long long	below; /* Number of times it's been too shallow */
41 42 43 44
#ifdef CONFIG_SUSPEND
	unsigned long long	s2idle_usage;
	unsigned long long	s2idle_time; /* in US */
#endif
45 46
};

47 48
struct cpuidle_state {
	char		name[CPUIDLE_NAME_LEN];
49
	char		desc[CPUIDLE_DESC_LEN];
50

51 52
	u64		exit_latency_ns;
	u64		target_residency_ns;
53 54
	unsigned int	flags;
	unsigned int	exit_latency; /* in US */
55
	int		power_usage; /* in mW */
56 57 58
	unsigned int	target_residency; /* in US */

	int (*enter)	(struct cpuidle_device *dev,
59
			struct cpuidle_driver *drv,
60
			int index);
61 62

	int (*enter_dead) (struct cpuidle_device *dev, int index);
63 64

	/*
65
	 * CPUs execute ->enter_s2idle with the local tick or entire timekeeping
66 67
	 * suspended, so it must not re-enable interrupts at any point (even
	 * temporarily) or attempt to change states of clock event devices.
68 69 70
	 *
	 * This callback may point to the same function as ->enter if all of
	 * the above requirements are met by it.
71
	 */
72 73 74
	int (*enter_s2idle)(struct cpuidle_device *dev,
			    struct cpuidle_driver *drv,
			    int index);
75 76 77
};

/* Idle State Flags */
78 79 80 81 82 83 84
#define CPUIDLE_FLAG_NONE       	(0x00)
#define CPUIDLE_FLAG_POLLING		BIT(0) /* polling state */
#define CPUIDLE_FLAG_COUPLED		BIT(1) /* state applies to multiple cpus */
#define CPUIDLE_FLAG_TIMER_STOP 	BIT(2) /* timer is stopped on this state */
#define CPUIDLE_FLAG_UNUSABLE		BIT(3) /* avoid using this state */
#define CPUIDLE_FLAG_OFF		BIT(4) /* disable this state by default */
#define CPUIDLE_FLAG_TLB_FLUSHED	BIT(5) /* idle-state flushes TLBs */
85
#define CPUIDLE_FLAG_RCU_IDLE		BIT(6) /* idle-state takes care of RCU */
86

87
struct cpuidle_device_kobj;
88 89
struct cpuidle_state_kobj;
struct cpuidle_driver_kobj;
90

91
struct cpuidle_device {
92
	unsigned int		registered:1;
93
	unsigned int		enabled:1;
94
	unsigned int		poll_time_limit:1;
95
	unsigned int		cpu;
96
	ktime_t			next_hrtimer;
97

98
	int			last_state_idx;
99
	u64			last_residency_ns;
100
	u64			poll_limit_ns;
101
	u64			forced_idle_latency_limit_ns;
102
	struct cpuidle_state_usage	states_usage[CPUIDLE_STATE_MAX];
103
	struct cpuidle_state_kobj *kobjs[CPUIDLE_STATE_MAX];
D
Daniel Lezcano 已提交
104
	struct cpuidle_driver_kobj *kobj_driver;
105
	struct cpuidle_device_kobj *kobj_dev;
106
	struct list_head 	device_list;
107 108 109 110 111

#ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
	cpumask_t		coupled_cpus;
	struct cpuidle_coupled	*coupled;
#endif
112 113 114
};

DECLARE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
115
DECLARE_PER_CPU(struct cpuidle_device, cpuidle_dev);
116 117 118 119 120 121

/****************************
 * CPUIDLE DRIVER INTERFACE *
 ****************************/

struct cpuidle_driver {
122
	const char		*name;
123
	struct module 		*owner;
124

125 126
        /* used by the cpuidle framework to setup the broadcast timer */
	unsigned int            bctimer:1;
127
	/* states array must be ordered in decreasing power consumption */
128 129 130
	struct cpuidle_state	states[CPUIDLE_STATE_MAX];
	int			state_count;
	int			safe_state_index;
131 132

	/* the driver handles the cpus in cpumask */
V
Viresh Kumar 已提交
133
	struct cpumask		*cpumask;
134 135 136

	/* preferred governor to switch at register time */
	const char		*governor;
137 138 139
};

#ifdef CONFIG_CPU_IDLE
140
extern void disable_cpuidle(void);
141 142
extern bool cpuidle_not_available(struct cpuidle_driver *drv,
				  struct cpuidle_device *dev);
143 144

extern int cpuidle_select(struct cpuidle_driver *drv,
145 146
			  struct cpuidle_device *dev,
			  bool *stop_tick);
147 148 149
extern int cpuidle_enter(struct cpuidle_driver *drv,
			 struct cpuidle_device *dev, int index);
extern void cpuidle_reflect(struct cpuidle_device *dev, int index);
150 151
extern u64 cpuidle_poll_time(struct cpuidle_driver *drv,
			     struct cpuidle_device *dev);
152

153
extern int cpuidle_register_driver(struct cpuidle_driver *drv);
154
extern struct cpuidle_driver *cpuidle_get_driver(void);
155 156
extern void cpuidle_driver_state_disabled(struct cpuidle_driver *drv, int idx,
					bool disable);
157 158 159
extern void cpuidle_unregister_driver(struct cpuidle_driver *drv);
extern int cpuidle_register_device(struct cpuidle_device *dev);
extern void cpuidle_unregister_device(struct cpuidle_device *dev);
160 161 162
extern int cpuidle_register(struct cpuidle_driver *drv,
			    const struct cpumask *const coupled_cpus);
extern void cpuidle_unregister(struct cpuidle_driver *drv);
163 164
extern void cpuidle_pause_and_lock(void);
extern void cpuidle_resume_and_unlock(void);
165 166
extern void cpuidle_pause(void);
extern void cpuidle_resume(void);
167 168
extern int cpuidle_enable_device(struct cpuidle_device *dev);
extern void cpuidle_disable_device(struct cpuidle_device *dev);
169 170
extern int cpuidle_play_dead(void);

D
Daniel Lezcano 已提交
171
extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev);
172 173
static inline struct cpuidle_device *cpuidle_get_device(void)
{return __this_cpu_read(cpuidle_devices); }
174
#else
175
static inline void disable_cpuidle(void) { }
176 177 178
static inline bool cpuidle_not_available(struct cpuidle_driver *drv,
					 struct cpuidle_device *dev)
{return true; }
179
static inline int cpuidle_select(struct cpuidle_driver *drv,
180
				 struct cpuidle_device *dev, bool *stop_tick)
181 182 183 184 185
{return -ENODEV; }
static inline int cpuidle_enter(struct cpuidle_driver *drv,
				struct cpuidle_device *dev, int index)
{return -ENODEV; }
static inline void cpuidle_reflect(struct cpuidle_device *dev, int index) { }
186
static inline u64 cpuidle_poll_time(struct cpuidle_driver *drv,
187 188
			     struct cpuidle_device *dev)
{return 0; }
189
static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
190
{return -ENODEV; }
191
static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }
192 193
static inline void cpuidle_driver_state_disabled(struct cpuidle_driver *drv,
					       int idx, bool disable) { }
194 195
static inline void cpuidle_unregister_driver(struct cpuidle_driver *drv) { }
static inline int cpuidle_register_device(struct cpuidle_device *dev)
196
{return -ENODEV; }
197
static inline void cpuidle_unregister_device(struct cpuidle_device *dev) { }
198 199 200 201
static inline int cpuidle_register(struct cpuidle_driver *drv,
				   const struct cpumask *const coupled_cpus)
{return -ENODEV; }
static inline void cpuidle_unregister(struct cpuidle_driver *drv) { }
202 203
static inline void cpuidle_pause_and_lock(void) { }
static inline void cpuidle_resume_and_unlock(void) { }
204 205
static inline void cpuidle_pause(void) { }
static inline void cpuidle_resume(void) { }
206
static inline int cpuidle_enable_device(struct cpuidle_device *dev)
207
{return -ENODEV; }
208
static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
209
static inline int cpuidle_play_dead(void) {return -ENODEV; }
210 211
static inline struct cpuidle_driver *cpuidle_get_cpu_driver(
	struct cpuidle_device *dev) {return NULL; }
212
static inline struct cpuidle_device *cpuidle_get_device(void) {return NULL; }
213 214
#endif

215
#ifdef CONFIG_CPU_IDLE
216
extern int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
217 218
				      struct cpuidle_device *dev,
				      u64 latency_limit_ns);
219
extern int cpuidle_enter_s2idle(struct cpuidle_driver *drv,
220
				struct cpuidle_device *dev);
221
extern void cpuidle_use_deepest_state(u64 latency_limit_ns);
222
#else
223
static inline int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
224 225
					     struct cpuidle_device *dev,
					     u64 latency_limit_ns)
226
{return -ENODEV; }
227
static inline int cpuidle_enter_s2idle(struct cpuidle_driver *drv,
228 229
				       struct cpuidle_device *dev)
{return -ENODEV; }
230
static inline void cpuidle_use_deepest_state(u64 latency_limit_ns)
231 232
{
}
233 234
#endif

235 236
/* kernel/sched/idle.c */
extern void sched_idle_set_state(struct cpuidle_state *idle_state);
237
extern void default_idle_call(void);
238

239 240
#ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a);
241 242 243 244
#else
static inline void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a)
{
}
245 246
#endif

247
#if defined(CONFIG_CPU_IDLE) && defined(CONFIG_ARCH_HAS_CPU_RELAX)
248
void cpuidle_poll_state_init(struct cpuidle_driver *drv);
249
#else
250
static inline void cpuidle_poll_state_init(struct cpuidle_driver *drv) {}
251 252
#endif

253 254 255 256 257 258 259 260 261
/******************************
 * CPUIDLE GOVERNOR INTERFACE *
 ******************************/

struct cpuidle_governor {
	char			name[CPUIDLE_NAME_LEN];
	struct list_head 	governor_list;
	unsigned int		rating;

262 263 264 265
	int  (*enable)		(struct cpuidle_driver *drv,
					struct cpuidle_device *dev);
	void (*disable)		(struct cpuidle_driver *drv,
					struct cpuidle_device *dev);
266

267
	int  (*select)		(struct cpuidle_driver *drv,
268 269
					struct cpuidle_device *dev,
					bool *stop_tick);
270
	void (*reflect)		(struct cpuidle_device *dev, int index);
271 272 273 274
};

#ifdef CONFIG_CPU_IDLE
extern int cpuidle_register_governor(struct cpuidle_governor *gov);
275
extern s64 cpuidle_governor_latency_req(unsigned int cpu);
276 277 278 279 280
#else
static inline int cpuidle_register_governor(struct cpuidle_governor *gov)
{return 0;}
#endif

281 282 283 284
#define __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter,			\
				idx,					\
				state,					\
				is_retention)				\
285 286 287 288 289 290 291 292 293 294 295
({									\
	int __ret = 0;							\
									\
	if (!idx) {							\
		cpu_do_idle();						\
		return idx;						\
	}								\
									\
	if (!is_retention)						\
		__ret =  cpu_pm_enter();				\
	if (!__ret) {							\
296
		__ret = low_level_idle_enter(state);			\
297 298 299 300 301
		if (!is_retention)					\
			cpu_pm_exit();					\
	}								\
									\
	__ret ? -1 : idx;						\
302 303
})

304
#define CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx)	\
305
	__CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, idx, 0)
306 307

#define CPU_PM_CPU_IDLE_ENTER_RETENTION(low_level_idle_enter, idx)	\
308 309 310 311 312 313 314
	__CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, idx, 1)

#define CPU_PM_CPU_IDLE_ENTER_PARAM(low_level_idle_enter, idx, state)	\
	__CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, state, 0)

#define CPU_PM_CPU_IDLE_ENTER_RETENTION_PARAM(low_level_idle_enter, idx, state)	\
	__CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, state, 1)
315

316
#endif /* _LINUX_CPUIDLE_H */