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