xref: /linux-6.15/include/linux/pm_domain.h (revision 151f4e2b)
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  * GENPD_FLAG_RPM_ALWAYS_ON:	Instructs genpd to always keep the PM domain
58  *				powered on except for system suspend.
59  */
60 #define GENPD_FLAG_PM_CLK	 (1U << 0)
61 #define GENPD_FLAG_IRQ_SAFE	 (1U << 1)
62 #define GENPD_FLAG_ALWAYS_ON	 (1U << 2)
63 #define GENPD_FLAG_ACTIVE_WAKEUP (1U << 3)
64 #define GENPD_FLAG_CPU_DOMAIN	 (1U << 4)
65 #define GENPD_FLAG_RPM_ALWAYS_ON (1U << 5)
66 
67 enum gpd_status {
68 	GPD_STATE_ACTIVE = 0,	/* PM domain is active */
69 	GPD_STATE_POWER_OFF,	/* PM domain is off */
70 };
71 
72 struct dev_power_governor {
73 	bool (*power_down_ok)(struct dev_pm_domain *domain);
74 	bool (*suspend_ok)(struct device *dev);
75 };
76 
77 struct gpd_dev_ops {
78 	int (*start)(struct device *dev);
79 	int (*stop)(struct device *dev);
80 };
81 
82 struct genpd_power_state {
83 	s64 power_off_latency_ns;
84 	s64 power_on_latency_ns;
85 	s64 residency_ns;
86 	struct fwnode_handle *fwnode;
87 	ktime_t idle_time;
88 	void *data;
89 };
90 
91 struct genpd_lock_ops;
92 struct dev_pm_opp;
93 struct opp_table;
94 
95 struct generic_pm_domain {
96 	struct device dev;
97 	struct dev_pm_domain domain;	/* PM domain operations */
98 	struct list_head gpd_list_node;	/* Node in the global PM domains list */
99 	struct list_head master_links;	/* Links with PM domain as a master */
100 	struct list_head slave_links;	/* Links with PM domain as a slave */
101 	struct list_head dev_list;	/* List of devices */
102 	struct dev_power_governor *gov;
103 	struct work_struct power_off_work;
104 	struct fwnode_handle *provider;	/* Identity of the domain provider */
105 	bool has_provider;
106 	const char *name;
107 	atomic_t sd_count;	/* Number of subdomains with power "on" */
108 	enum gpd_status status;	/* Current state of the domain */
109 	unsigned int device_count;	/* Number of devices */
110 	unsigned int suspended_count;	/* System suspend device counter */
111 	unsigned int prepared_count;	/* Suspend counter of prepared devices */
112 	unsigned int performance_state;	/* Aggregated max performance state */
113 	cpumask_var_t cpus;		/* A cpumask of the attached CPUs */
114 	int (*power_off)(struct generic_pm_domain *domain);
115 	int (*power_on)(struct generic_pm_domain *domain);
116 	struct opp_table *opp_table;	/* OPP table of the genpd */
117 	unsigned int (*opp_to_performance_state)(struct generic_pm_domain *genpd,
118 						 struct dev_pm_opp *opp);
119 	int (*set_performance_state)(struct generic_pm_domain *genpd,
120 				     unsigned int state);
121 	struct gpd_dev_ops dev_ops;
122 	s64 max_off_time_ns;	/* Maximum allowed "suspended" time. */
123 	bool max_off_time_changed;
124 	bool cached_power_down_ok;
125 	bool cached_power_down_state_idx;
126 	int (*attach_dev)(struct generic_pm_domain *domain,
127 			  struct device *dev);
128 	void (*detach_dev)(struct generic_pm_domain *domain,
129 			   struct device *dev);
130 	unsigned int flags;		/* Bit field of configs for genpd */
131 	struct genpd_power_state *states;
132 	void (*free_states)(struct genpd_power_state *states,
133 			    unsigned int state_count);
134 	unsigned int state_count; /* number of states */
135 	unsigned int state_idx; /* state that genpd will go to when off */
136 	ktime_t on_time;
137 	ktime_t accounting_time;
138 	const struct genpd_lock_ops *lock_ops;
139 	union {
140 		struct mutex mlock;
141 		struct {
142 			spinlock_t slock;
143 			unsigned long lock_flags;
144 		};
145 	};
146 
147 };
148 
149 static inline struct generic_pm_domain *pd_to_genpd(struct dev_pm_domain *pd)
150 {
151 	return container_of(pd, struct generic_pm_domain, domain);
152 }
153 
154 struct gpd_link {
155 	struct generic_pm_domain *master;
156 	struct list_head master_node;
157 	struct generic_pm_domain *slave;
158 	struct list_head slave_node;
159 
160 	/* Sub-domain's per-master domain performance state */
161 	unsigned int performance_state;
162 	unsigned int prev_performance_state;
163 };
164 
165 struct gpd_timing_data {
166 	s64 suspend_latency_ns;
167 	s64 resume_latency_ns;
168 	s64 effective_constraint_ns;
169 	bool constraint_changed;
170 	bool cached_suspend_ok;
171 };
172 
173 struct pm_domain_data {
174 	struct list_head list_node;
175 	struct device *dev;
176 };
177 
178 struct generic_pm_domain_data {
179 	struct pm_domain_data base;
180 	struct gpd_timing_data td;
181 	struct notifier_block nb;
182 	int cpu;
183 	unsigned int performance_state;
184 	void *data;
185 };
186 
187 #ifdef CONFIG_PM_GENERIC_DOMAINS
188 static inline struct generic_pm_domain_data *to_gpd_data(struct pm_domain_data *pdd)
189 {
190 	return container_of(pdd, struct generic_pm_domain_data, base);
191 }
192 
193 static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
194 {
195 	return to_gpd_data(dev->power.subsys_data->domain_data);
196 }
197 
198 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev);
199 int pm_genpd_remove_device(struct device *dev);
200 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
201 			   struct generic_pm_domain *new_subdomain);
202 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
203 			      struct generic_pm_domain *target);
204 int pm_genpd_init(struct generic_pm_domain *genpd,
205 		  struct dev_power_governor *gov, bool is_off);
206 int pm_genpd_remove(struct generic_pm_domain *genpd);
207 int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state);
208 
209 extern struct dev_power_governor simple_qos_governor;
210 extern struct dev_power_governor pm_domain_always_on_gov;
211 #ifdef CONFIG_CPU_IDLE
212 extern struct dev_power_governor pm_domain_cpu_gov;
213 #endif
214 #else
215 
216 static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
217 {
218 	return ERR_PTR(-ENOSYS);
219 }
220 static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
221 				      struct device *dev)
222 {
223 	return -ENOSYS;
224 }
225 static inline int pm_genpd_remove_device(struct device *dev)
226 {
227 	return -ENOSYS;
228 }
229 static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
230 					 struct generic_pm_domain *new_sd)
231 {
232 	return -ENOSYS;
233 }
234 static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
235 					    struct generic_pm_domain *target)
236 {
237 	return -ENOSYS;
238 }
239 static inline int pm_genpd_init(struct generic_pm_domain *genpd,
240 				struct dev_power_governor *gov, bool is_off)
241 {
242 	return -ENOSYS;
243 }
244 static inline int pm_genpd_remove(struct generic_pm_domain *genpd)
245 {
246 	return -ENOTSUPP;
247 }
248 
249 static inline int dev_pm_genpd_set_performance_state(struct device *dev,
250 						     unsigned int state)
251 {
252 	return -ENOTSUPP;
253 }
254 
255 #define simple_qos_governor		(*(struct dev_power_governor *)(NULL))
256 #define pm_domain_always_on_gov		(*(struct dev_power_governor *)(NULL))
257 #endif
258 
259 #ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
260 void pm_genpd_syscore_poweroff(struct device *dev);
261 void pm_genpd_syscore_poweron(struct device *dev);
262 #else
263 static inline void pm_genpd_syscore_poweroff(struct device *dev) {}
264 static inline void pm_genpd_syscore_poweron(struct device *dev) {}
265 #endif
266 
267 /* OF PM domain providers */
268 struct of_device_id;
269 
270 typedef struct generic_pm_domain *(*genpd_xlate_t)(struct of_phandle_args *args,
271 						   void *data);
272 
273 struct genpd_onecell_data {
274 	struct generic_pm_domain **domains;
275 	unsigned int num_domains;
276 	genpd_xlate_t xlate;
277 };
278 
279 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
280 int of_genpd_add_provider_simple(struct device_node *np,
281 				 struct generic_pm_domain *genpd);
282 int of_genpd_add_provider_onecell(struct device_node *np,
283 				  struct genpd_onecell_data *data);
284 void of_genpd_del_provider(struct device_node *np);
285 int of_genpd_add_device(struct of_phandle_args *args, struct device *dev);
286 int of_genpd_add_subdomain(struct of_phandle_args *parent,
287 			   struct of_phandle_args *new_subdomain);
288 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np);
289 int of_genpd_parse_idle_states(struct device_node *dn,
290 			       struct genpd_power_state **states, int *n);
291 unsigned int pm_genpd_opp_to_performance_state(struct device *genpd_dev,
292 					       struct dev_pm_opp *opp);
293 
294 int genpd_dev_pm_attach(struct device *dev);
295 struct device *genpd_dev_pm_attach_by_id(struct device *dev,
296 					 unsigned int index);
297 struct device *genpd_dev_pm_attach_by_name(struct device *dev,
298 					   const char *name);
299 #else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
300 static inline int of_genpd_add_provider_simple(struct device_node *np,
301 					struct generic_pm_domain *genpd)
302 {
303 	return -ENOTSUPP;
304 }
305 
306 static inline int of_genpd_add_provider_onecell(struct device_node *np,
307 					struct genpd_onecell_data *data)
308 {
309 	return -ENOTSUPP;
310 }
311 
312 static inline void of_genpd_del_provider(struct device_node *np) {}
313 
314 static inline int of_genpd_add_device(struct of_phandle_args *args,
315 				      struct device *dev)
316 {
317 	return -ENODEV;
318 }
319 
320 static inline int of_genpd_add_subdomain(struct of_phandle_args *parent,
321 					 struct of_phandle_args *new_subdomain)
322 {
323 	return -ENODEV;
324 }
325 
326 static inline int of_genpd_parse_idle_states(struct device_node *dn,
327 			struct genpd_power_state **states, int *n)
328 {
329 	return -ENODEV;
330 }
331 
332 static inline unsigned int
333 pm_genpd_opp_to_performance_state(struct device *genpd_dev,
334 				  struct dev_pm_opp *opp)
335 {
336 	return 0;
337 }
338 
339 static inline int genpd_dev_pm_attach(struct device *dev)
340 {
341 	return 0;
342 }
343 
344 static inline struct device *genpd_dev_pm_attach_by_id(struct device *dev,
345 						       unsigned int index)
346 {
347 	return NULL;
348 }
349 
350 static inline struct device *genpd_dev_pm_attach_by_name(struct device *dev,
351 							 const char *name)
352 {
353 	return NULL;
354 }
355 
356 static inline
357 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
358 {
359 	return ERR_PTR(-ENOTSUPP);
360 }
361 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
362 
363 #ifdef CONFIG_PM
364 int dev_pm_domain_attach(struct device *dev, bool power_on);
365 struct device *dev_pm_domain_attach_by_id(struct device *dev,
366 					  unsigned int index);
367 struct device *dev_pm_domain_attach_by_name(struct device *dev,
368 					    const char *name);
369 void dev_pm_domain_detach(struct device *dev, bool power_off);
370 void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
371 #else
372 static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
373 {
374 	return 0;
375 }
376 static inline struct device *dev_pm_domain_attach_by_id(struct device *dev,
377 							unsigned int index)
378 {
379 	return NULL;
380 }
381 static inline struct device *dev_pm_domain_attach_by_name(struct device *dev,
382 							  const char *name)
383 {
384 	return NULL;
385 }
386 static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
387 static inline void dev_pm_domain_set(struct device *dev,
388 				     struct dev_pm_domain *pd) {}
389 #endif
390 
391 #endif /* _LINUX_PM_DOMAIN_H */
392