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