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.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  * GENPD_FLAG_OPP_TABLE_FW:	The genpd provider supports performance states,
66  *				but its corresponding OPP tables are not
67  *				described in DT, but are given directly by FW.
68  */
69 #define GENPD_FLAG_PM_CLK	 (1U << 0)
70 #define GENPD_FLAG_IRQ_SAFE	 (1U << 1)
71 #define GENPD_FLAG_ALWAYS_ON	 (1U << 2)
72 #define GENPD_FLAG_ACTIVE_WAKEUP (1U << 3)
73 #define GENPD_FLAG_CPU_DOMAIN	 (1U << 4)
74 #define GENPD_FLAG_RPM_ALWAYS_ON (1U << 5)
75 #define GENPD_FLAG_MIN_RESIDENCY (1U << 6)
76 #define GENPD_FLAG_OPP_TABLE_FW	 (1U << 7)
77 
78 enum gpd_status {
79 	GENPD_STATE_ON = 0,	/* PM domain is on */
80 	GENPD_STATE_OFF,	/* PM domain is off */
81 };
82 
83 enum genpd_notication {
84 	GENPD_NOTIFY_PRE_OFF = 0,
85 	GENPD_NOTIFY_OFF,
86 	GENPD_NOTIFY_PRE_ON,
87 	GENPD_NOTIFY_ON,
88 };
89 
90 struct dev_power_governor {
91 	bool (*power_down_ok)(struct dev_pm_domain *domain);
92 	bool (*suspend_ok)(struct device *dev);
93 };
94 
95 struct gpd_dev_ops {
96 	int (*start)(struct device *dev);
97 	int (*stop)(struct device *dev);
98 };
99 
100 struct genpd_governor_data {
101 	s64 max_off_time_ns;
102 	bool max_off_time_changed;
103 	ktime_t next_wakeup;
104 	ktime_t next_hrtimer;
105 	bool cached_power_down_ok;
106 	bool cached_power_down_state_idx;
107 };
108 
109 struct genpd_power_state {
110 	s64 power_off_latency_ns;
111 	s64 power_on_latency_ns;
112 	s64 residency_ns;
113 	u64 usage;
114 	u64 rejected;
115 	struct fwnode_handle *fwnode;
116 	u64 idle_time;
117 	void *data;
118 };
119 
120 struct genpd_lock_ops;
121 struct opp_table;
122 
123 struct generic_pm_domain {
124 	struct device dev;
125 	struct dev_pm_domain domain;	/* PM domain operations */
126 	struct list_head gpd_list_node;	/* Node in the global PM domains list */
127 	struct list_head parent_links;	/* Links with PM domain as a parent */
128 	struct list_head child_links;	/* Links with PM domain as a child */
129 	struct list_head dev_list;	/* List of devices */
130 	struct dev_power_governor *gov;
131 	struct genpd_governor_data *gd;	/* Data used by a genpd governor. */
132 	struct work_struct power_off_work;
133 	struct fwnode_handle *provider;	/* Identity of the domain provider */
134 	bool has_provider;
135 	const char *name;
136 	atomic_t sd_count;	/* Number of subdomains with power "on" */
137 	enum gpd_status status;	/* Current state of the domain */
138 	unsigned int device_count;	/* Number of devices */
139 	unsigned int suspended_count;	/* System suspend device counter */
140 	unsigned int prepared_count;	/* Suspend counter of prepared devices */
141 	unsigned int performance_state;	/* Aggregated max performance state */
142 	cpumask_var_t cpus;		/* A cpumask of the attached CPUs */
143 	bool synced_poweroff;		/* A consumer needs a synced poweroff */
144 	int (*power_off)(struct generic_pm_domain *domain);
145 	int (*power_on)(struct generic_pm_domain *domain);
146 	struct raw_notifier_head power_notifiers; /* Power on/off notifiers */
147 	struct opp_table *opp_table;	/* OPP table of the genpd */
148 	int (*set_performance_state)(struct generic_pm_domain *genpd,
149 				     unsigned int state);
150 	struct gpd_dev_ops dev_ops;
151 	int (*attach_dev)(struct generic_pm_domain *domain,
152 			  struct device *dev);
153 	void (*detach_dev)(struct generic_pm_domain *domain,
154 			   struct device *dev);
155 	unsigned int flags;		/* Bit field of configs for genpd */
156 	struct genpd_power_state *states;
157 	void (*free_states)(struct genpd_power_state *states,
158 			    unsigned int state_count);
159 	unsigned int state_count; /* number of states */
160 	unsigned int state_idx; /* state that genpd will go to when off */
161 	u64 on_time;
162 	u64 accounting_time;
163 	const struct genpd_lock_ops *lock_ops;
164 	union {
165 		struct mutex mlock;
166 		struct {
167 			spinlock_t slock;
168 			unsigned long lock_flags;
169 		};
170 	};
171 
172 };
173 
pd_to_genpd(struct dev_pm_domain * pd)174 static inline struct generic_pm_domain *pd_to_genpd(struct dev_pm_domain *pd)
175 {
176 	return container_of(pd, struct generic_pm_domain, domain);
177 }
178 
179 struct gpd_link {
180 	struct generic_pm_domain *parent;
181 	struct list_head parent_node;
182 	struct generic_pm_domain *child;
183 	struct list_head child_node;
184 
185 	/* Sub-domain's per-master domain performance state */
186 	unsigned int performance_state;
187 	unsigned int prev_performance_state;
188 };
189 
190 struct gpd_timing_data {
191 	s64 suspend_latency_ns;
192 	s64 resume_latency_ns;
193 	s64 effective_constraint_ns;
194 	ktime_t	next_wakeup;
195 	bool constraint_changed;
196 	bool cached_suspend_ok;
197 };
198 
199 struct pm_domain_data {
200 	struct list_head list_node;
201 	struct device *dev;
202 };
203 
204 struct generic_pm_domain_data {
205 	struct pm_domain_data base;
206 	struct gpd_timing_data *td;
207 	struct notifier_block nb;
208 	struct notifier_block *power_nb;
209 	int cpu;
210 	unsigned int performance_state;
211 	unsigned int default_pstate;
212 	unsigned int rpm_pstate;
213 	void *data;
214 };
215 
216 #ifdef CONFIG_PM_GENERIC_DOMAINS
to_gpd_data(struct pm_domain_data * pdd)217 static inline struct generic_pm_domain_data *to_gpd_data(struct pm_domain_data *pdd)
218 {
219 	return container_of(pdd, struct generic_pm_domain_data, base);
220 }
221 
dev_gpd_data(struct device * dev)222 static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
223 {
224 	return to_gpd_data(dev->power.subsys_data->domain_data);
225 }
226 
227 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev);
228 int pm_genpd_remove_device(struct device *dev);
229 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
230 			   struct generic_pm_domain *subdomain);
231 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
232 			      struct generic_pm_domain *subdomain);
233 int pm_genpd_init(struct generic_pm_domain *genpd,
234 		  struct dev_power_governor *gov, bool is_off);
235 int pm_genpd_remove(struct generic_pm_domain *genpd);
236 int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state);
237 int dev_pm_genpd_add_notifier(struct device *dev, struct notifier_block *nb);
238 int dev_pm_genpd_remove_notifier(struct device *dev);
239 void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next);
240 ktime_t dev_pm_genpd_get_next_hrtimer(struct device *dev);
241 void dev_pm_genpd_synced_poweroff(struct device *dev);
242 
243 extern struct dev_power_governor simple_qos_governor;
244 extern struct dev_power_governor pm_domain_always_on_gov;
245 #ifdef CONFIG_CPU_IDLE
246 extern struct dev_power_governor pm_domain_cpu_gov;
247 #endif
248 #else
249 
dev_gpd_data(struct device * dev)250 static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
251 {
252 	return ERR_PTR(-ENOSYS);
253 }
pm_genpd_add_device(struct generic_pm_domain * genpd,struct device * dev)254 static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
255 				      struct device *dev)
256 {
257 	return -ENOSYS;
258 }
pm_genpd_remove_device(struct device * dev)259 static inline int pm_genpd_remove_device(struct device *dev)
260 {
261 	return -ENOSYS;
262 }
pm_genpd_add_subdomain(struct generic_pm_domain * genpd,struct generic_pm_domain * subdomain)263 static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
264 					 struct generic_pm_domain *subdomain)
265 {
266 	return -ENOSYS;
267 }
pm_genpd_remove_subdomain(struct generic_pm_domain * genpd,struct generic_pm_domain * subdomain)268 static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
269 					    struct generic_pm_domain *subdomain)
270 {
271 	return -ENOSYS;
272 }
pm_genpd_init(struct generic_pm_domain * genpd,struct dev_power_governor * gov,bool is_off)273 static inline int pm_genpd_init(struct generic_pm_domain *genpd,
274 				struct dev_power_governor *gov, bool is_off)
275 {
276 	return -ENOSYS;
277 }
pm_genpd_remove(struct generic_pm_domain * genpd)278 static inline int pm_genpd_remove(struct generic_pm_domain *genpd)
279 {
280 	return -EOPNOTSUPP;
281 }
282 
dev_pm_genpd_set_performance_state(struct device * dev,unsigned int state)283 static inline int dev_pm_genpd_set_performance_state(struct device *dev,
284 						     unsigned int state)
285 {
286 	return -EOPNOTSUPP;
287 }
288 
dev_pm_genpd_add_notifier(struct device * dev,struct notifier_block * nb)289 static inline int dev_pm_genpd_add_notifier(struct device *dev,
290 					    struct notifier_block *nb)
291 {
292 	return -EOPNOTSUPP;
293 }
294 
dev_pm_genpd_remove_notifier(struct device * dev)295 static inline int dev_pm_genpd_remove_notifier(struct device *dev)
296 {
297 	return -EOPNOTSUPP;
298 }
299 
dev_pm_genpd_set_next_wakeup(struct device * dev,ktime_t next)300 static inline void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next)
301 { }
302 
dev_pm_genpd_get_next_hrtimer(struct device * dev)303 static inline ktime_t dev_pm_genpd_get_next_hrtimer(struct device *dev)
304 {
305 	return KTIME_MAX;
306 }
dev_pm_genpd_synced_poweroff(struct device * dev)307 static inline void dev_pm_genpd_synced_poweroff(struct device *dev)
308 { }
309 
310 #define simple_qos_governor		(*(struct dev_power_governor *)(NULL))
311 #define pm_domain_always_on_gov		(*(struct dev_power_governor *)(NULL))
312 #endif
313 
314 #ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
315 void dev_pm_genpd_suspend(struct device *dev);
316 void dev_pm_genpd_resume(struct device *dev);
317 #else
dev_pm_genpd_suspend(struct device * dev)318 static inline void dev_pm_genpd_suspend(struct device *dev) {}
dev_pm_genpd_resume(struct device * dev)319 static inline void dev_pm_genpd_resume(struct device *dev) {}
320 #endif
321 
322 /* OF PM domain providers */
323 struct of_device_id;
324 
325 typedef struct generic_pm_domain *(*genpd_xlate_t)(struct of_phandle_args *args,
326 						   void *data);
327 
328 struct genpd_onecell_data {
329 	struct generic_pm_domain **domains;
330 	unsigned int num_domains;
331 	genpd_xlate_t xlate;
332 };
333 
334 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
335 int of_genpd_add_provider_simple(struct device_node *np,
336 				 struct generic_pm_domain *genpd);
337 int of_genpd_add_provider_onecell(struct device_node *np,
338 				  struct genpd_onecell_data *data);
339 void of_genpd_del_provider(struct device_node *np);
340 int of_genpd_add_device(struct of_phandle_args *args, struct device *dev);
341 int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
342 			   struct of_phandle_args *subdomain_spec);
343 int of_genpd_remove_subdomain(struct of_phandle_args *parent_spec,
344 			      struct of_phandle_args *subdomain_spec);
345 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np);
346 int of_genpd_parse_idle_states(struct device_node *dn,
347 			       struct genpd_power_state **states, int *n);
348 
349 int genpd_dev_pm_attach(struct device *dev);
350 struct device *genpd_dev_pm_attach_by_id(struct device *dev,
351 					 unsigned int index);
352 struct device *genpd_dev_pm_attach_by_name(struct device *dev,
353 					   const char *name);
354 #else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
of_genpd_add_provider_simple(struct device_node * np,struct generic_pm_domain * genpd)355 static inline int of_genpd_add_provider_simple(struct device_node *np,
356 					struct generic_pm_domain *genpd)
357 {
358 	return -EOPNOTSUPP;
359 }
360 
of_genpd_add_provider_onecell(struct device_node * np,struct genpd_onecell_data * data)361 static inline int of_genpd_add_provider_onecell(struct device_node *np,
362 					struct genpd_onecell_data *data)
363 {
364 	return -EOPNOTSUPP;
365 }
366 
of_genpd_del_provider(struct device_node * np)367 static inline void of_genpd_del_provider(struct device_node *np) {}
368 
of_genpd_add_device(struct of_phandle_args * args,struct device * dev)369 static inline int of_genpd_add_device(struct of_phandle_args *args,
370 				      struct device *dev)
371 {
372 	return -ENODEV;
373 }
374 
of_genpd_add_subdomain(struct of_phandle_args * parent_spec,struct of_phandle_args * subdomain_spec)375 static inline int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
376 					 struct of_phandle_args *subdomain_spec)
377 {
378 	return -ENODEV;
379 }
380 
of_genpd_remove_subdomain(struct of_phandle_args * parent_spec,struct of_phandle_args * subdomain_spec)381 static inline int of_genpd_remove_subdomain(struct of_phandle_args *parent_spec,
382 					struct of_phandle_args *subdomain_spec)
383 {
384 	return -ENODEV;
385 }
386 
of_genpd_parse_idle_states(struct device_node * dn,struct genpd_power_state ** states,int * n)387 static inline int of_genpd_parse_idle_states(struct device_node *dn,
388 			struct genpd_power_state **states, int *n)
389 {
390 	return -ENODEV;
391 }
392 
genpd_dev_pm_attach(struct device * dev)393 static inline int genpd_dev_pm_attach(struct device *dev)
394 {
395 	return 0;
396 }
397 
genpd_dev_pm_attach_by_id(struct device * dev,unsigned int index)398 static inline struct device *genpd_dev_pm_attach_by_id(struct device *dev,
399 						       unsigned int index)
400 {
401 	return NULL;
402 }
403 
genpd_dev_pm_attach_by_name(struct device * dev,const char * name)404 static inline struct device *genpd_dev_pm_attach_by_name(struct device *dev,
405 							 const char *name)
406 {
407 	return NULL;
408 }
409 
410 static inline
of_genpd_remove_last(struct device_node * np)411 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
412 {
413 	return ERR_PTR(-EOPNOTSUPP);
414 }
415 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
416 
417 #ifdef CONFIG_PM
418 int dev_pm_domain_attach(struct device *dev, bool power_on);
419 struct device *dev_pm_domain_attach_by_id(struct device *dev,
420 					  unsigned int index);
421 struct device *dev_pm_domain_attach_by_name(struct device *dev,
422 					    const char *name);
423 void dev_pm_domain_detach(struct device *dev, bool power_off);
424 int dev_pm_domain_start(struct device *dev);
425 void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
426 int dev_pm_domain_set_performance_state(struct device *dev, unsigned int state);
427 #else
dev_pm_domain_attach(struct device * dev,bool power_on)428 static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
429 {
430 	return 0;
431 }
dev_pm_domain_attach_by_id(struct device * dev,unsigned int index)432 static inline struct device *dev_pm_domain_attach_by_id(struct device *dev,
433 							unsigned int index)
434 {
435 	return NULL;
436 }
dev_pm_domain_attach_by_name(struct device * dev,const char * name)437 static inline struct device *dev_pm_domain_attach_by_name(struct device *dev,
438 							  const char *name)
439 {
440 	return NULL;
441 }
dev_pm_domain_detach(struct device * dev,bool power_off)442 static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
dev_pm_domain_start(struct device * dev)443 static inline int dev_pm_domain_start(struct device *dev)
444 {
445 	return 0;
446 }
dev_pm_domain_set(struct device * dev,struct dev_pm_domain * pd)447 static inline void dev_pm_domain_set(struct device *dev,
448 				     struct dev_pm_domain *pd) {}
dev_pm_domain_set_performance_state(struct device * dev,unsigned int state)449 static inline int dev_pm_domain_set_performance_state(struct device *dev,
450 						      unsigned int state)
451 {
452 	return 0;
453 }
454 #endif
455 
456 #endif /* _LINUX_PM_DOMAIN_H */
457