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

source code of linux/include/linux/pm_domain.h