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>
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 struct module;
23 
24 struct cpuidle_device;
25 struct cpuidle_driver;
26 
27 
28 /****************************
29  * CPUIDLE DEVICE INTERFACE *
30  ****************************/
31 
32 struct cpuidle_state_usage {
33 	unsigned long long	disable;
34 	unsigned long long	usage;
35 	unsigned long long	time; /* in US */
36 #ifdef CONFIG_SUSPEND
37 	unsigned long long	s2idle_usage;
38 	unsigned long long	s2idle_time; /* in US */
39 #endif
40 };
41 
42 struct cpuidle_state {
43 	char		name[CPUIDLE_NAME_LEN];
44 	char		desc[CPUIDLE_DESC_LEN];
45 
46 	unsigned int	flags;
47 	unsigned int	exit_latency; /* in US */
48 	int		power_usage; /* in mW */
49 	unsigned int	target_residency; /* in US */
50 	bool		disabled; /* disabled on all CPUs */
51 
52 	int (*enter)	(struct cpuidle_device *dev,
53 			struct cpuidle_driver *drv,
54 			int index);
55 
56 	int (*enter_dead) (struct cpuidle_device *dev, int index);
57 
58 	/*
59 	 * CPUs execute ->enter_s2idle with the local tick or entire timekeeping
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 	 */
63 	void (*enter_s2idle) (struct cpuidle_device *dev,
64 			      struct cpuidle_driver *drv,
65 			      int index);
66 };
67 
68 /* Idle State Flags */
69 #define CPUIDLE_FLAG_NONE       (0x00)
70 #define CPUIDLE_FLAG_POLLING	(0x01) /* polling state */
71 #define CPUIDLE_FLAG_COUPLED	(0x02) /* state applies to multiple cpus */
72 #define CPUIDLE_FLAG_TIMER_STOP (0x04)  /* timer is stopped on this state */
73 
74 #define CPUIDLE_DRIVER_FLAGS_MASK (0xFFFF0000)
75 
76 struct cpuidle_device_kobj;
77 struct cpuidle_state_kobj;
78 struct cpuidle_driver_kobj;
79 
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;
85 	unsigned int		cpu;
86 
87 	int			last_residency;
88 	struct cpuidle_state_usage	states_usage[CPUIDLE_STATE_MAX];
89 	struct cpuidle_state_kobj *kobjs[CPUIDLE_STATE_MAX];
90 	struct cpuidle_driver_kobj *kobj_driver;
91 	struct cpuidle_device_kobj *kobj_dev;
92 	struct list_head 	device_list;
93 
94 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
95 	cpumask_t		coupled_cpus;
96 	struct cpuidle_coupled	*coupled;
97 #endif
98 };
99 
100 DECLARE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
101 DECLARE_PER_CPU(struct cpuidle_device, cpuidle_dev);
102 
103 /**
104  * cpuidle_get_last_residency - retrieves the last state's residency time
105  * @dev: the target CPU
106  */
cpuidle_get_last_residency(struct cpuidle_device * dev)107 static inline int cpuidle_get_last_residency(struct cpuidle_device *dev)
108 {
109 	return dev->last_residency;
110 }
111 
112 
113 /****************************
114  * CPUIDLE DRIVER INTERFACE *
115  ****************************/
116 
117 struct cpuidle_driver {
118 	const char		*name;
119 	struct module 		*owner;
120 	int                     refcnt;
121 
122         /* used by the cpuidle framework to setup the broadcast timer */
123 	unsigned int            bctimer:1;
124 	/* states array must be ordered in decreasing power consumption */
125 	struct cpuidle_state	states[CPUIDLE_STATE_MAX];
126 	int			state_count;
127 	int			safe_state_index;
128 
129 	/* the driver handles the cpus in cpumask */
130 	struct cpumask		*cpumask;
131 };
132 
133 #ifdef CONFIG_CPU_IDLE
134 extern void disable_cpuidle(void);
135 extern bool cpuidle_not_available(struct cpuidle_driver *drv,
136 				  struct cpuidle_device *dev);
137 
138 extern int cpuidle_select(struct cpuidle_driver *drv,
139 			  struct cpuidle_device *dev,
140 			  bool *stop_tick);
141 extern int cpuidle_enter(struct cpuidle_driver *drv,
142 			 struct cpuidle_device *dev, int index);
143 extern void cpuidle_reflect(struct cpuidle_device *dev, int index);
144 
145 extern int cpuidle_register_driver(struct cpuidle_driver *drv);
146 extern struct cpuidle_driver *cpuidle_get_driver(void);
147 extern struct cpuidle_driver *cpuidle_driver_ref(void);
148 extern void cpuidle_driver_unref(void);
149 extern void cpuidle_unregister_driver(struct cpuidle_driver *drv);
150 extern int cpuidle_register_device(struct cpuidle_device *dev);
151 extern void cpuidle_unregister_device(struct cpuidle_device *dev);
152 extern int cpuidle_register(struct cpuidle_driver *drv,
153 			    const struct cpumask *const coupled_cpus);
154 extern void cpuidle_unregister(struct cpuidle_driver *drv);
155 extern void cpuidle_pause_and_lock(void);
156 extern void cpuidle_resume_and_unlock(void);
157 extern void cpuidle_pause(void);
158 extern void cpuidle_resume(void);
159 extern int cpuidle_enable_device(struct cpuidle_device *dev);
160 extern void cpuidle_disable_device(struct cpuidle_device *dev);
161 extern int cpuidle_play_dead(void);
162 
163 extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev);
cpuidle_get_device(void)164 static inline struct cpuidle_device *cpuidle_get_device(void)
165 {return __this_cpu_read(cpuidle_devices); }
166 #else
disable_cpuidle(void)167 static inline void disable_cpuidle(void) { }
cpuidle_not_available(struct cpuidle_driver * drv,struct cpuidle_device * dev)168 static inline bool cpuidle_not_available(struct cpuidle_driver *drv,
169 					 struct cpuidle_device *dev)
170 {return true; }
cpuidle_select(struct cpuidle_driver * drv,struct cpuidle_device * dev,bool * stop_tick)171 static inline int cpuidle_select(struct cpuidle_driver *drv,
172 				 struct cpuidle_device *dev, bool *stop_tick)
173 {return -ENODEV; }
cpuidle_enter(struct cpuidle_driver * drv,struct cpuidle_device * dev,int index)174 static inline int cpuidle_enter(struct cpuidle_driver *drv,
175 				struct cpuidle_device *dev, int index)
176 {return -ENODEV; }
cpuidle_reflect(struct cpuidle_device * dev,int index)177 static inline void cpuidle_reflect(struct cpuidle_device *dev, int index) { }
cpuidle_register_driver(struct cpuidle_driver * drv)178 static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
179 {return -ENODEV; }
cpuidle_get_driver(void)180 static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }
cpuidle_driver_ref(void)181 static inline struct cpuidle_driver *cpuidle_driver_ref(void) {return NULL; }
cpuidle_driver_unref(void)182 static inline void cpuidle_driver_unref(void) {}
cpuidle_unregister_driver(struct cpuidle_driver * drv)183 static inline void cpuidle_unregister_driver(struct cpuidle_driver *drv) { }
cpuidle_register_device(struct cpuidle_device * dev)184 static inline int cpuidle_register_device(struct cpuidle_device *dev)
185 {return -ENODEV; }
cpuidle_unregister_device(struct cpuidle_device * dev)186 static inline void cpuidle_unregister_device(struct cpuidle_device *dev) { }
cpuidle_register(struct cpuidle_driver * drv,const struct cpumask * const coupled_cpus)187 static inline int cpuidle_register(struct cpuidle_driver *drv,
188 				   const struct cpumask *const coupled_cpus)
189 {return -ENODEV; }
cpuidle_unregister(struct cpuidle_driver * drv)190 static inline void cpuidle_unregister(struct cpuidle_driver *drv) { }
cpuidle_pause_and_lock(void)191 static inline void cpuidle_pause_and_lock(void) { }
cpuidle_resume_and_unlock(void)192 static inline void cpuidle_resume_and_unlock(void) { }
cpuidle_pause(void)193 static inline void cpuidle_pause(void) { }
cpuidle_resume(void)194 static inline void cpuidle_resume(void) { }
cpuidle_enable_device(struct cpuidle_device * dev)195 static inline int cpuidle_enable_device(struct cpuidle_device *dev)
196 {return -ENODEV; }
cpuidle_disable_device(struct cpuidle_device * dev)197 static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
cpuidle_play_dead(void)198 static inline int cpuidle_play_dead(void) {return -ENODEV; }
cpuidle_get_cpu_driver(struct cpuidle_device * dev)199 static inline struct cpuidle_driver *cpuidle_get_cpu_driver(
200 	struct cpuidle_device *dev) {return NULL; }
cpuidle_get_device(void)201 static inline struct cpuidle_device *cpuidle_get_device(void) {return NULL; }
202 #endif
203 
204 #ifdef CONFIG_CPU_IDLE
205 extern int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
206 				      struct cpuidle_device *dev);
207 extern int cpuidle_enter_s2idle(struct cpuidle_driver *drv,
208 				struct cpuidle_device *dev);
209 extern void cpuidle_use_deepest_state(bool enable);
210 #else
cpuidle_find_deepest_state(struct cpuidle_driver * drv,struct cpuidle_device * dev)211 static inline int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
212 					     struct cpuidle_device *dev)
213 {return -ENODEV; }
cpuidle_enter_s2idle(struct cpuidle_driver * drv,struct cpuidle_device * dev)214 static inline int cpuidle_enter_s2idle(struct cpuidle_driver *drv,
215 				       struct cpuidle_device *dev)
216 {return -ENODEV; }
cpuidle_use_deepest_state(bool enable)217 static inline void cpuidle_use_deepest_state(bool enable)
218 {
219 }
220 #endif
221 
222 /* kernel/sched/idle.c */
223 extern void sched_idle_set_state(struct cpuidle_state *idle_state);
224 extern void default_idle_call(void);
225 
226 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
227 void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a);
228 #else
cpuidle_coupled_parallel_barrier(struct cpuidle_device * dev,atomic_t * a)229 static inline void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a)
230 {
231 }
232 #endif
233 
234 #if defined(CONFIG_CPU_IDLE) && defined(CONFIG_ARCH_HAS_CPU_RELAX)
235 void cpuidle_poll_state_init(struct cpuidle_driver *drv);
236 #else
cpuidle_poll_state_init(struct cpuidle_driver * drv)237 static inline void cpuidle_poll_state_init(struct cpuidle_driver *drv) {}
238 #endif
239 
240 /******************************
241  * CPUIDLE GOVERNOR INTERFACE *
242  ******************************/
243 
244 struct cpuidle_governor {
245 	char			name[CPUIDLE_NAME_LEN];
246 	struct list_head 	governor_list;
247 	unsigned int		rating;
248 
249 	int  (*enable)		(struct cpuidle_driver *drv,
250 					struct cpuidle_device *dev);
251 	void (*disable)		(struct cpuidle_driver *drv,
252 					struct cpuidle_device *dev);
253 
254 	int  (*select)		(struct cpuidle_driver *drv,
255 					struct cpuidle_device *dev,
256 					bool *stop_tick);
257 	void (*reflect)		(struct cpuidle_device *dev, int index);
258 };
259 
260 #ifdef CONFIG_CPU_IDLE
261 extern int cpuidle_register_governor(struct cpuidle_governor *gov);
262 extern int cpuidle_governor_latency_req(unsigned int cpu);
263 #else
cpuidle_register_governor(struct cpuidle_governor * gov)264 static inline int cpuidle_register_governor(struct cpuidle_governor *gov)
265 {return 0;}
266 #endif
267 
268 #define __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, is_retention) \
269 ({									\
270 	int __ret = 0;							\
271 									\
272 	if (!idx) {							\
273 		cpu_do_idle();						\
274 		return idx;						\
275 	}								\
276 									\
277 	if (!is_retention)						\
278 		__ret =  cpu_pm_enter();				\
279 	if (!__ret) {							\
280 		__ret = low_level_idle_enter(idx);			\
281 		if (!is_retention)					\
282 			cpu_pm_exit();					\
283 	}								\
284 									\
285 	__ret ? -1 : idx;						\
286 })
287 
288 #define CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx)	\
289 	__CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, 0)
290 
291 #define CPU_PM_CPU_IDLE_ENTER_RETENTION(low_level_idle_enter, idx)	\
292 	__CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, 1)
293 
294 #endif /* _LINUX_CPUIDLE_H */
295