xref: /linux-6.15/include/linux/cpuidle.h (revision b898cc70)
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/module.h>
17 #include <linux/kobject.h>
18 #include <linux/completion.h>
19 
20 #define CPUIDLE_STATE_MAX	8
21 #define CPUIDLE_NAME_LEN	16
22 #define CPUIDLE_DESC_LEN	32
23 
24 struct cpuidle_device;
25 
26 
27 /****************************
28  * CPUIDLE DEVICE INTERFACE *
29  ****************************/
30 
31 struct cpuidle_state {
32 	char		name[CPUIDLE_NAME_LEN];
33 	char		desc[CPUIDLE_DESC_LEN];
34 	void		*driver_data;
35 
36 	unsigned int	flags;
37 	unsigned int	exit_latency; /* in US */
38 	unsigned int	power_usage; /* in mW */
39 	unsigned int	target_residency; /* in US */
40 
41 	unsigned long long	usage;
42 	unsigned long long	time; /* in US */
43 
44 	int (*enter)	(struct cpuidle_device *dev,
45 			 struct cpuidle_state *state);
46 };
47 
48 /* Idle State Flags */
49 #define CPUIDLE_FLAG_TIME_VALID	(0x01) /* is residency time measurable? */
50 #define CPUIDLE_FLAG_CHECK_BM	(0x02) /* BM activity will exit state */
51 #define CPUIDLE_FLAG_POLL	(0x10) /* no latency, no savings */
52 #define CPUIDLE_FLAG_SHALLOW	(0x20) /* low latency, minimal savings */
53 #define CPUIDLE_FLAG_BALANCED	(0x40) /* medium latency, moderate savings */
54 #define CPUIDLE_FLAG_DEEP	(0x80) /* high latency, large savings */
55 
56 #define CPUIDLE_DRIVER_FLAGS_MASK (0xFFFF0000)
57 
58 /**
59  * cpuidle_get_statedata - retrieves private driver state data
60  * @state: the state
61  */
62 static inline void * cpuidle_get_statedata(struct cpuidle_state *state)
63 {
64 	return state->driver_data;
65 }
66 
67 /**
68  * cpuidle_set_statedata - stores private driver state data
69  * @state: the state
70  * @data: the private data
71  */
72 static inline void
73 cpuidle_set_statedata(struct cpuidle_state *state, void *data)
74 {
75 	state->driver_data = data;
76 }
77 
78 struct cpuidle_state_kobj {
79 	struct cpuidle_state *state;
80 	struct completion kobj_unregister;
81 	struct kobject kobj;
82 };
83 
84 struct cpuidle_device {
85 	unsigned int		registered:1;
86 	unsigned int		enabled:1;
87 	unsigned int		cpu;
88 
89 	int			last_residency;
90 	int			state_count;
91 	struct cpuidle_state	states[CPUIDLE_STATE_MAX];
92 	struct cpuidle_state_kobj *kobjs[CPUIDLE_STATE_MAX];
93 	struct cpuidle_state	*last_state;
94 
95 	struct list_head 	device_list;
96 	struct kobject		kobj;
97 	struct completion	kobj_unregister;
98 	void			*governor_data;
99 	struct cpuidle_state	*safe_state;
100 };
101 
102 DECLARE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
103 
104 /**
105  * cpuidle_get_last_residency - retrieves the last state's residency time
106  * @dev: the target CPU
107  *
108  * NOTE: this value is invalid if CPUIDLE_FLAG_TIME_VALID isn't set
109  */
110 static inline int cpuidle_get_last_residency(struct cpuidle_device *dev)
111 {
112 	return dev->last_residency;
113 }
114 
115 
116 /****************************
117  * CPUIDLE DRIVER INTERFACE *
118  ****************************/
119 
120 struct cpuidle_driver {
121 	char			name[CPUIDLE_NAME_LEN];
122 	struct module 		*owner;
123 };
124 
125 #ifdef CONFIG_CPU_IDLE
126 
127 extern int cpuidle_register_driver(struct cpuidle_driver *drv);
128 struct cpuidle_driver *cpuidle_get_driver(void);
129 extern void cpuidle_unregister_driver(struct cpuidle_driver *drv);
130 extern int cpuidle_register_device(struct cpuidle_device *dev);
131 extern void cpuidle_unregister_device(struct cpuidle_device *dev);
132 
133 extern void cpuidle_pause_and_lock(void);
134 extern void cpuidle_resume_and_unlock(void);
135 extern int cpuidle_enable_device(struct cpuidle_device *dev);
136 extern void cpuidle_disable_device(struct cpuidle_device *dev);
137 
138 #else
139 
140 static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
141 {return -ENODEV; }
142 static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }
143 static inline void cpuidle_unregister_driver(struct cpuidle_driver *drv) { }
144 static inline int cpuidle_register_device(struct cpuidle_device *dev)
145 {return -ENODEV; }
146 static inline void cpuidle_unregister_device(struct cpuidle_device *dev) { }
147 
148 static inline void cpuidle_pause_and_lock(void) { }
149 static inline void cpuidle_resume_and_unlock(void) { }
150 static inline int cpuidle_enable_device(struct cpuidle_device *dev)
151 {return -ENODEV; }
152 static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
153 
154 #endif
155 
156 /******************************
157  * CPUIDLE GOVERNOR INTERFACE *
158  ******************************/
159 
160 struct cpuidle_governor {
161 	char			name[CPUIDLE_NAME_LEN];
162 	struct list_head 	governor_list;
163 	unsigned int		rating;
164 
165 	int  (*enable)		(struct cpuidle_device *dev);
166 	void (*disable)		(struct cpuidle_device *dev);
167 
168 	int  (*select)		(struct cpuidle_device *dev);
169 	void (*reflect)		(struct cpuidle_device *dev);
170 
171 	struct module 		*owner;
172 };
173 
174 #ifdef CONFIG_CPU_IDLE
175 
176 extern int cpuidle_register_governor(struct cpuidle_governor *gov);
177 extern void cpuidle_unregister_governor(struct cpuidle_governor *gov);
178 
179 #else
180 
181 static inline int cpuidle_register_governor(struct cpuidle_governor *gov)
182 {return 0;}
183 static inline void cpuidle_unregister_governor(struct cpuidle_governor *gov) { }
184 
185 #endif
186 
187 #ifdef CONFIG_ARCH_HAS_CPU_RELAX
188 #define CPUIDLE_DRIVER_STATE_START	1
189 #else
190 #define CPUIDLE_DRIVER_STATE_START	0
191 #endif
192 
193 #endif /* _LINUX_CPUIDLE_H */
194