xref: /linux-6.15/include/linux/cpuidle.h (revision dc16c8a9)
1 /*
2  * cpuidle.h - a generic framework for CPU idle power management
3  *
4  * (C) 2007 Venkatesh Pallipadi <[email protected]>
5  *          Shaohua Li <[email protected]>
6  *          Adam Belay <[email protected]>
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 #define CPUIDLE_STATE_DISABLED_BY_USER		BIT(0)
33 #define CPUIDLE_STATE_DISABLED_BY_DRIVER	BIT(1)
34 
35 struct cpuidle_state_usage {
36 	unsigned long long	disable;
37 	unsigned long long	usage;
38 	u64			time_ns;
39 	unsigned long long	above; /* Number of times it's been too deep */
40 	unsigned long long	below; /* Number of times it's been too shallow */
41 #ifdef CONFIG_SUSPEND
42 	unsigned long long	s2idle_usage;
43 	unsigned long long	s2idle_time; /* in US */
44 #endif
45 };
46 
47 struct cpuidle_state {
48 	char		name[CPUIDLE_NAME_LEN];
49 	char		desc[CPUIDLE_DESC_LEN];
50 
51 	u64		exit_latency_ns;
52 	u64		target_residency_ns;
53 	unsigned int	flags;
54 	unsigned int	exit_latency; /* in US */
55 	int		power_usage; /* in mW */
56 	unsigned int	target_residency; /* in US */
57 
58 	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 	 * suspended, so it must not re-enable interrupts at any point (even
67 	 * temporarily) or attempt to change states of clock event devices.
68 	 *
69 	 * This callback may point to the same function as ->enter if all of
70 	 * the above requirements are met by it.
71 	 */
72 	int (*enter_s2idle)(struct cpuidle_device *dev,
73 			    struct cpuidle_driver *drv,
74 			    int index);
75 };
76 
77 /* Idle State Flags */
78 #define CPUIDLE_FLAG_NONE       (0x00)
79 #define CPUIDLE_FLAG_POLLING	BIT(0) /* polling state */
80 #define CPUIDLE_FLAG_COUPLED	BIT(1) /* state applies to multiple cpus */
81 #define CPUIDLE_FLAG_TIMER_STOP BIT(2) /* timer is stopped on this state */
82 #define CPUIDLE_FLAG_UNUSABLE	BIT(3) /* avoid using this state */
83 #define CPUIDLE_FLAG_OFF	BIT(4) /* disable this state by default */
84 
85 struct cpuidle_device_kobj;
86 struct cpuidle_state_kobj;
87 struct cpuidle_driver_kobj;
88 
89 struct cpuidle_device {
90 	unsigned int		registered:1;
91 	unsigned int		enabled:1;
92 	unsigned int		poll_time_limit:1;
93 	unsigned int		cpu;
94 	ktime_t			next_hrtimer;
95 
96 	int			last_state_idx;
97 	u64			last_residency_ns;
98 	u64			poll_limit_ns;
99 	u64			forced_idle_latency_limit_ns;
100 	struct cpuidle_state_usage	states_usage[CPUIDLE_STATE_MAX];
101 	struct cpuidle_state_kobj *kobjs[CPUIDLE_STATE_MAX];
102 	struct cpuidle_driver_kobj *kobj_driver;
103 	struct cpuidle_device_kobj *kobj_dev;
104 	struct list_head 	device_list;
105 
106 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
107 	cpumask_t		coupled_cpus;
108 	struct cpuidle_coupled	*coupled;
109 #endif
110 };
111 
112 DECLARE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
113 DECLARE_PER_CPU(struct cpuidle_device, cpuidle_dev);
114 
115 /****************************
116  * CPUIDLE DRIVER INTERFACE *
117  ****************************/
118 
119 struct cpuidle_driver {
120 	const char		*name;
121 	struct module 		*owner;
122 
123         /* used by the cpuidle framework to setup the broadcast timer */
124 	unsigned int            bctimer:1;
125 	/* states array must be ordered in decreasing power consumption */
126 	struct cpuidle_state	states[CPUIDLE_STATE_MAX];
127 	int			state_count;
128 	int			safe_state_index;
129 
130 	/* the driver handles the cpus in cpumask */
131 	struct cpumask		*cpumask;
132 
133 	/* preferred governor to switch at register time */
134 	const char		*governor;
135 };
136 
137 #ifdef CONFIG_CPU_IDLE
138 extern void disable_cpuidle(void);
139 extern bool cpuidle_not_available(struct cpuidle_driver *drv,
140 				  struct cpuidle_device *dev);
141 
142 extern int cpuidle_select(struct cpuidle_driver *drv,
143 			  struct cpuidle_device *dev,
144 			  bool *stop_tick);
145 extern int cpuidle_enter(struct cpuidle_driver *drv,
146 			 struct cpuidle_device *dev, int index);
147 extern void cpuidle_reflect(struct cpuidle_device *dev, int index);
148 extern u64 cpuidle_poll_time(struct cpuidle_driver *drv,
149 			     struct cpuidle_device *dev);
150 
151 extern int cpuidle_register_driver(struct cpuidle_driver *drv);
152 extern struct cpuidle_driver *cpuidle_get_driver(void);
153 extern void cpuidle_driver_state_disabled(struct cpuidle_driver *drv, int idx,
154 					bool disable);
155 extern void cpuidle_unregister_driver(struct cpuidle_driver *drv);
156 extern int cpuidle_register_device(struct cpuidle_device *dev);
157 extern void cpuidle_unregister_device(struct cpuidle_device *dev);
158 extern int cpuidle_register(struct cpuidle_driver *drv,
159 			    const struct cpumask *const coupled_cpus);
160 extern void cpuidle_unregister(struct cpuidle_driver *drv);
161 extern void cpuidle_pause_and_lock(void);
162 extern void cpuidle_resume_and_unlock(void);
163 extern void cpuidle_pause(void);
164 extern void cpuidle_resume(void);
165 extern int cpuidle_enable_device(struct cpuidle_device *dev);
166 extern void cpuidle_disable_device(struct cpuidle_device *dev);
167 extern int cpuidle_play_dead(void);
168 
169 extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev);
170 static inline struct cpuidle_device *cpuidle_get_device(void)
171 {return __this_cpu_read(cpuidle_devices); }
172 #else
173 static inline void disable_cpuidle(void) { }
174 static inline bool cpuidle_not_available(struct cpuidle_driver *drv,
175 					 struct cpuidle_device *dev)
176 {return true; }
177 static inline int cpuidle_select(struct cpuidle_driver *drv,
178 				 struct cpuidle_device *dev, bool *stop_tick)
179 {return -ENODEV; }
180 static inline int cpuidle_enter(struct cpuidle_driver *drv,
181 				struct cpuidle_device *dev, int index)
182 {return -ENODEV; }
183 static inline void cpuidle_reflect(struct cpuidle_device *dev, int index) { }
184 static inline u64 cpuidle_poll_time(struct cpuidle_driver *drv,
185 			     struct cpuidle_device *dev)
186 {return 0; }
187 static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
188 {return -ENODEV; }
189 static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }
190 static inline void cpuidle_driver_state_disabled(struct cpuidle_driver *drv,
191 					       int idx, bool disable) { }
192 static inline void cpuidle_unregister_driver(struct cpuidle_driver *drv) { }
193 static inline int cpuidle_register_device(struct cpuidle_device *dev)
194 {return -ENODEV; }
195 static inline void cpuidle_unregister_device(struct cpuidle_device *dev) { }
196 static inline int cpuidle_register(struct cpuidle_driver *drv,
197 				   const struct cpumask *const coupled_cpus)
198 {return -ENODEV; }
199 static inline void cpuidle_unregister(struct cpuidle_driver *drv) { }
200 static inline void cpuidle_pause_and_lock(void) { }
201 static inline void cpuidle_resume_and_unlock(void) { }
202 static inline void cpuidle_pause(void) { }
203 static inline void cpuidle_resume(void) { }
204 static inline int cpuidle_enable_device(struct cpuidle_device *dev)
205 {return -ENODEV; }
206 static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
207 static inline int cpuidle_play_dead(void) {return -ENODEV; }
208 static inline struct cpuidle_driver *cpuidle_get_cpu_driver(
209 	struct cpuidle_device *dev) {return NULL; }
210 static inline struct cpuidle_device *cpuidle_get_device(void) {return NULL; }
211 #endif
212 
213 #ifdef CONFIG_CPU_IDLE
214 extern int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
215 				      struct cpuidle_device *dev,
216 				      u64 latency_limit_ns);
217 extern int cpuidle_enter_s2idle(struct cpuidle_driver *drv,
218 				struct cpuidle_device *dev);
219 extern void cpuidle_use_deepest_state(u64 latency_limit_ns);
220 #else
221 static inline int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
222 					     struct cpuidle_device *dev,
223 					     u64 latency_limit_ns)
224 {return -ENODEV; }
225 static inline int cpuidle_enter_s2idle(struct cpuidle_driver *drv,
226 				       struct cpuidle_device *dev)
227 {return -ENODEV; }
228 static inline void cpuidle_use_deepest_state(u64 latency_limit_ns)
229 {
230 }
231 #endif
232 
233 /* kernel/sched/idle.c */
234 extern void sched_idle_set_state(struct cpuidle_state *idle_state);
235 extern void default_idle_call(void);
236 
237 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
238 void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a);
239 #else
240 static inline void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a)
241 {
242 }
243 #endif
244 
245 #if defined(CONFIG_CPU_IDLE) && defined(CONFIG_ARCH_HAS_CPU_RELAX)
246 void cpuidle_poll_state_init(struct cpuidle_driver *drv);
247 #else
248 static inline void cpuidle_poll_state_init(struct cpuidle_driver *drv) {}
249 #endif
250 
251 /******************************
252  * CPUIDLE GOVERNOR INTERFACE *
253  ******************************/
254 
255 struct cpuidle_governor {
256 	char			name[CPUIDLE_NAME_LEN];
257 	struct list_head 	governor_list;
258 	unsigned int		rating;
259 
260 	int  (*enable)		(struct cpuidle_driver *drv,
261 					struct cpuidle_device *dev);
262 	void (*disable)		(struct cpuidle_driver *drv,
263 					struct cpuidle_device *dev);
264 
265 	int  (*select)		(struct cpuidle_driver *drv,
266 					struct cpuidle_device *dev,
267 					bool *stop_tick);
268 	void (*reflect)		(struct cpuidle_device *dev, int index);
269 };
270 
271 #ifdef CONFIG_CPU_IDLE
272 extern int cpuidle_register_governor(struct cpuidle_governor *gov);
273 extern s64 cpuidle_governor_latency_req(unsigned int cpu);
274 #else
275 static inline int cpuidle_register_governor(struct cpuidle_governor *gov)
276 {return 0;}
277 #endif
278 
279 #define __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter,			\
280 				idx,					\
281 				state,					\
282 				is_retention)				\
283 ({									\
284 	int __ret = 0;							\
285 									\
286 	if (!idx) {							\
287 		cpu_do_idle();						\
288 		return idx;						\
289 	}								\
290 									\
291 	if (!is_retention)						\
292 		__ret =  cpu_pm_enter();				\
293 	if (!__ret) {							\
294 		__ret = low_level_idle_enter(state);			\
295 		if (!is_retention)					\
296 			cpu_pm_exit();					\
297 	}								\
298 									\
299 	__ret ? -1 : idx;						\
300 })
301 
302 #define CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx)	\
303 	__CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, idx, 0)
304 
305 #define CPU_PM_CPU_IDLE_ENTER_RETENTION(low_level_idle_enter, idx)	\
306 	__CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, idx, 1)
307 
308 #define CPU_PM_CPU_IDLE_ENTER_PARAM(low_level_idle_enter, idx, state)	\
309 	__CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, state, 0)
310 
311 #define CPU_PM_CPU_IDLE_ENTER_RETENTION_PARAM(low_level_idle_enter, idx, state)	\
312 	__CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, state, 1)
313 
314 #endif /* _LINUX_CPUIDLE_H */
315