xref: /linux-6.15/include/linux/pm_domain.h (revision ca138a7a)
1 /*
2  * pm_domain.h - Definitions and headers related to device power domains.
3  *
4  * Copyright (C) 2011 Rafael J. Wysocki <[email protected]>, Renesas Electronics Corp.
5  *
6  * This file is released under the GPLv2.
7  */
8 
9 #ifndef _LINUX_PM_DOMAIN_H
10 #define _LINUX_PM_DOMAIN_H
11 
12 #include <linux/device.h>
13 #include <linux/mutex.h>
14 #include <linux/pm.h>
15 #include <linux/err.h>
16 #include <linux/of.h>
17 #include <linux/notifier.h>
18 #include <linux/spinlock.h>
19 #include <linux/cpumask.h>
20 
21 /*
22  * Flags to control the behaviour of a genpd.
23  *
24  * These flags may be set in the struct generic_pm_domain's flags field by a
25  * genpd backend driver. The flags must be set before it calls pm_genpd_init(),
26  * which initializes a genpd.
27  *
28  * GENPD_FLAG_PM_CLK:		Instructs genpd to use the PM clk framework,
29  *				while powering on/off attached devices.
30  *
31  * GENPD_FLAG_IRQ_SAFE:		This informs genpd that its backend callbacks,
32  *				->power_on|off(), doesn't sleep. Hence, these
33  *				can be invoked from within atomic context, which
34  *				enables genpd to power on/off the PM domain,
35  *				even when pm_runtime_is_irq_safe() returns true,
36  *				for any of its attached devices. Note that, a
37  *				genpd having this flag set, requires its
38  *				masterdomains to also have it set.
39  *
40  * GENPD_FLAG_ALWAYS_ON:	Instructs genpd to always keep the PM domain
41  *				powered on.
42  *
43  * GENPD_FLAG_ACTIVE_WAKEUP:	Instructs genpd to keep the PM domain powered
44  *				on, in case any of its attached devices is used
45  *				in the wakeup path to serve system wakeups.
46  *
47  * GENPD_FLAG_CPU_DOMAIN:	Instructs genpd that it should expect to get
48  *				devices attached, which may belong to CPUs or
49  *				possibly have subdomains with CPUs attached.
50  *				This flag enables the genpd backend driver to
51  *				deploy idle power management support for CPUs
52  *				and groups of CPUs. Note that, the backend
53  *				driver must then comply with the so called,
54  *				last-man-standing algorithm, for the CPUs in the
55  *				PM domain.
56  */
57 #define GENPD_FLAG_PM_CLK	 (1U << 0)
58 #define GENPD_FLAG_IRQ_SAFE	 (1U << 1)
59 #define GENPD_FLAG_ALWAYS_ON	 (1U << 2)
60 #define GENPD_FLAG_ACTIVE_WAKEUP (1U << 3)
61 #define GENPD_FLAG_CPU_DOMAIN	 (1U << 4)
62 
63 enum gpd_status {
64 	GPD_STATE_ACTIVE = 0,	/* PM domain is active */
65 	GPD_STATE_POWER_OFF,	/* PM domain is off */
66 };
67 
68 struct dev_power_governor {
69 	bool (*power_down_ok)(struct dev_pm_domain *domain);
70 	bool (*suspend_ok)(struct device *dev);
71 };
72 
73 struct gpd_dev_ops {
74 	int (*start)(struct device *dev);
75 	int (*stop)(struct device *dev);
76 };
77 
78 struct genpd_power_state {
79 	s64 power_off_latency_ns;
80 	s64 power_on_latency_ns;
81 	s64 residency_ns;
82 	struct fwnode_handle *fwnode;
83 	ktime_t idle_time;
84 	void *data;
85 };
86 
87 struct genpd_lock_ops;
88 struct dev_pm_opp;
89 struct opp_table;
90 
91 struct generic_pm_domain {
92 	struct device dev;
93 	struct dev_pm_domain domain;	/* PM domain operations */
94 	struct list_head gpd_list_node;	/* Node in the global PM domains list */
95 	struct list_head master_links;	/* Links with PM domain as a master */
96 	struct list_head slave_links;	/* Links with PM domain as a slave */
97 	struct list_head dev_list;	/* List of devices */
98 	struct dev_power_governor *gov;
99 	struct work_struct power_off_work;
100 	struct fwnode_handle *provider;	/* Identity of the domain provider */
101 	bool has_provider;
102 	const char *name;
103 	atomic_t sd_count;	/* Number of subdomains with power "on" */
104 	enum gpd_status status;	/* Current state of the domain */
105 	unsigned int device_count;	/* Number of devices */
106 	unsigned int suspended_count;	/* System suspend device counter */
107 	unsigned int prepared_count;	/* Suspend counter of prepared devices */
108 	unsigned int performance_state;	/* Aggregated max performance state */
109 	cpumask_var_t cpus;		/* A cpumask of the attached CPUs */
110 	int (*power_off)(struct generic_pm_domain *domain);
111 	int (*power_on)(struct generic_pm_domain *domain);
112 	struct opp_table *opp_table;	/* OPP table of the genpd */
113 	unsigned int (*opp_to_performance_state)(struct generic_pm_domain *genpd,
114 						 struct dev_pm_opp *opp);
115 	int (*set_performance_state)(struct generic_pm_domain *genpd,
116 				     unsigned int state);
117 	struct gpd_dev_ops dev_ops;
118 	s64 max_off_time_ns;	/* Maximum allowed "suspended" time. */
119 	bool max_off_time_changed;
120 	bool cached_power_down_ok;
121 	bool cached_power_down_state_idx;
122 	int (*attach_dev)(struct generic_pm_domain *domain,
123 			  struct device *dev);
124 	void (*detach_dev)(struct generic_pm_domain *domain,
125 			   struct device *dev);
126 	unsigned int flags;		/* Bit field of configs for genpd */
127 	struct genpd_power_state *states;
128 	void (*free_states)(struct genpd_power_state *states,
129 			    unsigned int state_count);
130 	unsigned int state_count; /* number of states */
131 	unsigned int state_idx; /* state that genpd will go to when off */
132 	ktime_t on_time;
133 	ktime_t accounting_time;
134 	const struct genpd_lock_ops *lock_ops;
135 	union {
136 		struct mutex mlock;
137 		struct {
138 			spinlock_t slock;
139 			unsigned long lock_flags;
140 		};
141 	};
142 
143 };
144 
145 static inline struct generic_pm_domain *pd_to_genpd(struct dev_pm_domain *pd)
146 {
147 	return container_of(pd, struct generic_pm_domain, domain);
148 }
149 
150 struct gpd_link {
151 	struct generic_pm_domain *master;
152 	struct list_head master_node;
153 	struct generic_pm_domain *slave;
154 	struct list_head slave_node;
155 
156 	/* Sub-domain's per-master domain performance state */
157 	unsigned int performance_state;
158 	unsigned int prev_performance_state;
159 };
160 
161 struct gpd_timing_data {
162 	s64 suspend_latency_ns;
163 	s64 resume_latency_ns;
164 	s64 effective_constraint_ns;
165 	bool constraint_changed;
166 	bool cached_suspend_ok;
167 };
168 
169 struct pm_domain_data {
170 	struct list_head list_node;
171 	struct device *dev;
172 };
173 
174 struct generic_pm_domain_data {
175 	struct pm_domain_data base;
176 	struct gpd_timing_data td;
177 	struct notifier_block nb;
178 	int cpu;
179 	unsigned int performance_state;
180 	void *data;
181 };
182 
183 #ifdef CONFIG_PM_GENERIC_DOMAINS
184 static inline struct generic_pm_domain_data *to_gpd_data(struct pm_domain_data *pdd)
185 {
186 	return container_of(pdd, struct generic_pm_domain_data, base);
187 }
188 
189 static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
190 {
191 	return to_gpd_data(dev->power.subsys_data->domain_data);
192 }
193 
194 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev);
195 int pm_genpd_remove_device(struct device *dev);
196 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
197 			   struct generic_pm_domain *new_subdomain);
198 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
199 			      struct generic_pm_domain *target);
200 int pm_genpd_init(struct generic_pm_domain *genpd,
201 		  struct dev_power_governor *gov, bool is_off);
202 int pm_genpd_remove(struct generic_pm_domain *genpd);
203 int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state);
204 
205 extern struct dev_power_governor simple_qos_governor;
206 extern struct dev_power_governor pm_domain_always_on_gov;
207 #ifdef CONFIG_CPU_IDLE
208 extern struct dev_power_governor pm_domain_cpu_gov;
209 #endif
210 #else
211 
212 static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
213 {
214 	return ERR_PTR(-ENOSYS);
215 }
216 static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
217 				      struct device *dev)
218 {
219 	return -ENOSYS;
220 }
221 static inline int pm_genpd_remove_device(struct device *dev)
222 {
223 	return -ENOSYS;
224 }
225 static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
226 					 struct generic_pm_domain *new_sd)
227 {
228 	return -ENOSYS;
229 }
230 static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
231 					    struct generic_pm_domain *target)
232 {
233 	return -ENOSYS;
234 }
235 static inline int pm_genpd_init(struct generic_pm_domain *genpd,
236 				struct dev_power_governor *gov, bool is_off)
237 {
238 	return -ENOSYS;
239 }
240 static inline int pm_genpd_remove(struct generic_pm_domain *genpd)
241 {
242 	return -ENOTSUPP;
243 }
244 
245 static inline int dev_pm_genpd_set_performance_state(struct device *dev,
246 						     unsigned int state)
247 {
248 	return -ENOTSUPP;
249 }
250 
251 #define simple_qos_governor		(*(struct dev_power_governor *)(NULL))
252 #define pm_domain_always_on_gov		(*(struct dev_power_governor *)(NULL))
253 #endif
254 
255 #ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
256 void pm_genpd_syscore_poweroff(struct device *dev);
257 void pm_genpd_syscore_poweron(struct device *dev);
258 #else
259 static inline void pm_genpd_syscore_poweroff(struct device *dev) {}
260 static inline void pm_genpd_syscore_poweron(struct device *dev) {}
261 #endif
262 
263 /* OF PM domain providers */
264 struct of_device_id;
265 
266 typedef struct generic_pm_domain *(*genpd_xlate_t)(struct of_phandle_args *args,
267 						   void *data);
268 
269 struct genpd_onecell_data {
270 	struct generic_pm_domain **domains;
271 	unsigned int num_domains;
272 	genpd_xlate_t xlate;
273 };
274 
275 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
276 int of_genpd_add_provider_simple(struct device_node *np,
277 				 struct generic_pm_domain *genpd);
278 int of_genpd_add_provider_onecell(struct device_node *np,
279 				  struct genpd_onecell_data *data);
280 void of_genpd_del_provider(struct device_node *np);
281 int of_genpd_add_device(struct of_phandle_args *args, struct device *dev);
282 int of_genpd_add_subdomain(struct of_phandle_args *parent,
283 			   struct of_phandle_args *new_subdomain);
284 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np);
285 int of_genpd_parse_idle_states(struct device_node *dn,
286 			       struct genpd_power_state **states, int *n);
287 unsigned int pm_genpd_opp_to_performance_state(struct device *genpd_dev,
288 					       struct dev_pm_opp *opp);
289 
290 int genpd_dev_pm_attach(struct device *dev);
291 struct device *genpd_dev_pm_attach_by_id(struct device *dev,
292 					 unsigned int index);
293 struct device *genpd_dev_pm_attach_by_name(struct device *dev,
294 					   const char *name);
295 #else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
296 static inline int of_genpd_add_provider_simple(struct device_node *np,
297 					struct generic_pm_domain *genpd)
298 {
299 	return -ENOTSUPP;
300 }
301 
302 static inline int of_genpd_add_provider_onecell(struct device_node *np,
303 					struct genpd_onecell_data *data)
304 {
305 	return -ENOTSUPP;
306 }
307 
308 static inline void of_genpd_del_provider(struct device_node *np) {}
309 
310 static inline int of_genpd_add_device(struct of_phandle_args *args,
311 				      struct device *dev)
312 {
313 	return -ENODEV;
314 }
315 
316 static inline int of_genpd_add_subdomain(struct of_phandle_args *parent,
317 					 struct of_phandle_args *new_subdomain)
318 {
319 	return -ENODEV;
320 }
321 
322 static inline int of_genpd_parse_idle_states(struct device_node *dn,
323 			struct genpd_power_state **states, int *n)
324 {
325 	return -ENODEV;
326 }
327 
328 static inline unsigned int
329 pm_genpd_opp_to_performance_state(struct device *genpd_dev,
330 				  struct dev_pm_opp *opp)
331 {
332 	return 0;
333 }
334 
335 static inline int genpd_dev_pm_attach(struct device *dev)
336 {
337 	return 0;
338 }
339 
340 static inline struct device *genpd_dev_pm_attach_by_id(struct device *dev,
341 						       unsigned int index)
342 {
343 	return NULL;
344 }
345 
346 static inline struct device *genpd_dev_pm_attach_by_name(struct device *dev,
347 							 const char *name)
348 {
349 	return NULL;
350 }
351 
352 static inline
353 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
354 {
355 	return ERR_PTR(-ENOTSUPP);
356 }
357 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
358 
359 #ifdef CONFIG_PM
360 int dev_pm_domain_attach(struct device *dev, bool power_on);
361 struct device *dev_pm_domain_attach_by_id(struct device *dev,
362 					  unsigned int index);
363 struct device *dev_pm_domain_attach_by_name(struct device *dev,
364 					    const char *name);
365 void dev_pm_domain_detach(struct device *dev, bool power_off);
366 void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
367 #else
368 static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
369 {
370 	return 0;
371 }
372 static inline struct device *dev_pm_domain_attach_by_id(struct device *dev,
373 							unsigned int index)
374 {
375 	return NULL;
376 }
377 static inline struct device *dev_pm_domain_attach_by_name(struct device *dev,
378 							  const char *name)
379 {
380 	return NULL;
381 }
382 static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
383 static inline void dev_pm_domain_set(struct device *dev,
384 				     struct dev_pm_domain *pd) {}
385 #endif
386 
387 #endif /* _LINUX_PM_DOMAIN_H */
388