cpuidle.h 10.1 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 68
	 * suspended, so it must not re-enable interrupts at any point (even
	 * temporarily) or attempt to change states of clock event devices.
	 */
69
	void (*enter_s2idle) (struct cpuidle_device *dev,
70 71
			      struct cpuidle_driver *drv,
			      int index);
72 73 74
};

/* Idle State Flags */
75
#define CPUIDLE_FLAG_NONE       (0x00)
76 77 78
#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 */
79
#define CPUIDLE_FLAG_UNUSABLE	BIT(3) /* avoid using this state */
80
#define CPUIDLE_FLAG_OFF	BIT(4) /* disable this state by default */
81

82
struct cpuidle_device_kobj;
83 84
struct cpuidle_state_kobj;
struct cpuidle_driver_kobj;
85

86
struct cpuidle_device {
87
	unsigned int		registered:1;
88
	unsigned int		enabled:1;
89
	unsigned int		poll_time_limit:1;
90
	unsigned int		cpu;
91
	ktime_t			next_hrtimer;
92

93
	int			last_state_idx;
94
	u64			last_residency_ns;
95
	u64			poll_limit_ns;
96
	u64			forced_idle_latency_limit_ns;
97
	struct cpuidle_state_usage	states_usage[CPUIDLE_STATE_MAX];
98
	struct cpuidle_state_kobj *kobjs[CPUIDLE_STATE_MAX];
D
Daniel Lezcano 已提交
99
	struct cpuidle_driver_kobj *kobj_driver;
100
	struct cpuidle_device_kobj *kobj_dev;
101
	struct list_head 	device_list;
102 103 104 105 106

#ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
	cpumask_t		coupled_cpus;
	struct cpuidle_coupled	*coupled;
#endif
107 108 109
};

DECLARE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
110
DECLARE_PER_CPU(struct cpuidle_device, cpuidle_dev);
111 112 113 114 115 116

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

struct cpuidle_driver {
117
	const char		*name;
118
	struct module 		*owner;
119

120 121
        /* used by the cpuidle framework to setup the broadcast timer */
	unsigned int            bctimer:1;
122
	/* states array must be ordered in decreasing power consumption */
123 124 125
	struct cpuidle_state	states[CPUIDLE_STATE_MAX];
	int			state_count;
	int			safe_state_index;
126 127

	/* the driver handles the cpus in cpumask */
V
Viresh Kumar 已提交
128
	struct cpumask		*cpumask;
129 130 131

	/* preferred governor to switch at register time */
	const char		*governor;
132 133 134
};

#ifdef CONFIG_CPU_IDLE
135
extern void disable_cpuidle(void);
136 137
extern bool cpuidle_not_available(struct cpuidle_driver *drv,
				  struct cpuidle_device *dev);
138 139

extern int cpuidle_select(struct cpuidle_driver *drv,
140 141
			  struct cpuidle_device *dev,
			  bool *stop_tick);
142 143 144
extern int cpuidle_enter(struct cpuidle_driver *drv,
			 struct cpuidle_device *dev, int index);
extern void cpuidle_reflect(struct cpuidle_device *dev, int index);
145 146
extern u64 cpuidle_poll_time(struct cpuidle_driver *drv,
			     struct cpuidle_device *dev);
147

148
extern int cpuidle_register_driver(struct cpuidle_driver *drv);
149
extern struct cpuidle_driver *cpuidle_get_driver(void);
150 151
extern void cpuidle_driver_state_disabled(struct cpuidle_driver *drv, int idx,
					bool disable);
152 153 154
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);
155 156 157
extern int cpuidle_register(struct cpuidle_driver *drv,
			    const struct cpumask *const coupled_cpus);
extern void cpuidle_unregister(struct cpuidle_driver *drv);
158 159
extern void cpuidle_pause_and_lock(void);
extern void cpuidle_resume_and_unlock(void);
160 161
extern void cpuidle_pause(void);
extern void cpuidle_resume(void);
162 163
extern int cpuidle_enable_device(struct cpuidle_device *dev);
extern void cpuidle_disable_device(struct cpuidle_device *dev);
164 165
extern int cpuidle_play_dead(void);

D
Daniel Lezcano 已提交
166
extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev);
167 168
static inline struct cpuidle_device *cpuidle_get_device(void)
{return __this_cpu_read(cpuidle_devices); }
169
#else
170
static inline void disable_cpuidle(void) { }
171 172 173
static inline bool cpuidle_not_available(struct cpuidle_driver *drv,
					 struct cpuidle_device *dev)
{return true; }
174
static inline int cpuidle_select(struct cpuidle_driver *drv,
175
				 struct cpuidle_device *dev, bool *stop_tick)
176 177 178 179 180
{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) { }
181
static inline u64 cpuidle_poll_time(struct cpuidle_driver *drv,
182 183
			     struct cpuidle_device *dev)
{return 0; }
184
static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
185
{return -ENODEV; }
186
static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }
187 188
static inline void cpuidle_driver_state_disabled(struct cpuidle_driver *drv,
					       int idx, bool disable) { }
189 190
static inline void cpuidle_unregister_driver(struct cpuidle_driver *drv) { }
static inline int cpuidle_register_device(struct cpuidle_device *dev)
191
{return -ENODEV; }
192
static inline void cpuidle_unregister_device(struct cpuidle_device *dev) { }
193 194 195 196
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) { }
197 198
static inline void cpuidle_pause_and_lock(void) { }
static inline void cpuidle_resume_and_unlock(void) { }
199 200
static inline void cpuidle_pause(void) { }
static inline void cpuidle_resume(void) { }
201
static inline int cpuidle_enable_device(struct cpuidle_device *dev)
202
{return -ENODEV; }
203
static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
204
static inline int cpuidle_play_dead(void) {return -ENODEV; }
205 206
static inline struct cpuidle_driver *cpuidle_get_cpu_driver(
	struct cpuidle_device *dev) {return NULL; }
207
static inline struct cpuidle_device *cpuidle_get_device(void) {return NULL; }
208 209
#endif

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

230 231
/* kernel/sched/idle.c */
extern void sched_idle_set_state(struct cpuidle_state *idle_state);
232
extern void default_idle_call(void);
233

234 235
#ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a);
236 237 238 239
#else
static inline void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a)
{
}
240 241
#endif

242
#if defined(CONFIG_CPU_IDLE) && defined(CONFIG_ARCH_HAS_CPU_RELAX)
243
void cpuidle_poll_state_init(struct cpuidle_driver *drv);
244
#else
245
static inline void cpuidle_poll_state_init(struct cpuidle_driver *drv) {}
246 247
#endif

248 249 250 251 252 253 254 255 256
/******************************
 * CPUIDLE GOVERNOR INTERFACE *
 ******************************/

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

257 258 259 260
	int  (*enable)		(struct cpuidle_driver *drv,
					struct cpuidle_device *dev);
	void (*disable)		(struct cpuidle_driver *drv,
					struct cpuidle_device *dev);
261

262
	int  (*select)		(struct cpuidle_driver *drv,
263 264
					struct cpuidle_device *dev,
					bool *stop_tick);
265
	void (*reflect)		(struct cpuidle_device *dev, int index);
266 267 268 269
};

#ifdef CONFIG_CPU_IDLE
extern int cpuidle_register_governor(struct cpuidle_governor *gov);
270
extern s64 cpuidle_governor_latency_req(unsigned int cpu);
271 272 273 274 275
#else
static inline int cpuidle_register_governor(struct cpuidle_governor *gov)
{return 0;}
#endif

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

299
#define CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx)	\
300
	__CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, idx, 0)
301 302

#define CPU_PM_CPU_IDLE_ENTER_RETENTION(low_level_idle_enter, idx)	\
303 304 305 306 307 308 309
	__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)
310

311
#endif /* _LINUX_CPUIDLE_H */