1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * pm_domain.h - Definitions and headers related to device power domains.
8 #ifndef _LINUX_PM_DOMAIN_H
9 #define _LINUX_PM_DOMAIN_H
11 #include <linux/device.h>
12 #include <linux/ktime.h>
13 #include <linux/mutex.h>
15 #include <linux/err.h>
17 #include <linux/notifier.h>
18 #include <linux/spinlock.h>
19 #include <linux/cpumask_types.h>
20 #include <linux/time64.h>
23 * Flags to control the behaviour when attaching a device to its PM domains.
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.
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
34 #define PD_FLAG_NO_DEV_LINK BIT(0)
35 #define PD_FLAG_DEV_LINK_ON BIT(1)
37 struct dev_pm_domain_attach_data {
38 const char * const *pd_names;
39 const u32 num_pd_names;
43 struct dev_pm_domain_list {
44 struct device **pd_devs;
45 struct device_link **pd_links;
50 * Flags to control the behaviour of a genpd.
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.
56 * GENPD_FLAG_PM_CLK: Instructs genpd to use the PM clk framework,
57 * while powering on/off attached devices.
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.
68 * GENPD_FLAG_ALWAYS_ON: Instructs genpd to always keep the PM domain
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.
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
85 * GENPD_FLAG_RPM_ALWAYS_ON: Instructs genpd to always keep the PM domain
86 * powered on except for system suspend.
88 * GENPD_FLAG_MIN_RESIDENCY: Enable the genpd governor to consider its
89 * components' next wakeup when determining the
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.
96 #define GENPD_FLAG_PM_CLK (1U << 0)
97 #define GENPD_FLAG_IRQ_SAFE (1U << 1)
98 #define GENPD_FLAG_ALWAYS_ON (1U << 2)
99 #define GENPD_FLAG_ACTIVE_WAKEUP (1U << 3)
100 #define GENPD_FLAG_CPU_DOMAIN (1U << 4)
101 #define GENPD_FLAG_RPM_ALWAYS_ON (1U << 5)
102 #define GENPD_FLAG_MIN_RESIDENCY (1U << 6)
103 #define GENPD_FLAG_OPP_TABLE_FW (1U << 7)
106 GENPD_STATE_ON = 0, /* PM domain is on */
107 GENPD_STATE_OFF, /* PM domain is off */
110 enum genpd_notication {
111 GENPD_NOTIFY_PRE_OFF = 0,
117 struct dev_power_governor {
118 bool (*power_down_ok)(struct dev_pm_domain *domain);
119 bool (*suspend_ok)(struct device *dev);
123 int (*start)(struct device *dev);
124 int (*stop)(struct device *dev);
127 struct genpd_governor_data {
129 bool max_off_time_changed;
131 ktime_t next_hrtimer;
132 bool cached_power_down_ok;
133 bool cached_power_down_state_idx;
136 struct genpd_power_state {
137 s64 power_off_latency_ns;
138 s64 power_on_latency_ns;
142 struct fwnode_handle *fwnode;
147 struct genpd_lock_ops;
150 struct generic_pm_domain {
152 struct dev_pm_domain domain; /* PM domain operations */
153 struct list_head gpd_list_node; /* Node in the global PM domains list */
154 struct list_head parent_links; /* Links with PM domain as a parent */
155 struct list_head child_links; /* Links with PM domain as a child */
156 struct list_head dev_list; /* List of devices */
157 struct dev_power_governor *gov;
158 struct genpd_governor_data *gd; /* Data used by a genpd governor. */
159 struct work_struct power_off_work;
160 struct fwnode_handle *provider; /* Identity of the domain provider */
163 atomic_t sd_count; /* Number of subdomains with power "on" */
164 enum gpd_status status; /* Current state of the domain */
165 unsigned int device_count; /* Number of devices */
166 unsigned int suspended_count; /* System suspend device counter */
167 unsigned int prepared_count; /* Suspend counter of prepared devices */
168 unsigned int performance_state; /* Aggregated max performance state */
169 cpumask_var_t cpus; /* A cpumask of the attached CPUs */
170 bool synced_poweroff; /* A consumer needs a synced poweroff */
171 int (*power_off)(struct generic_pm_domain *domain);
172 int (*power_on)(struct generic_pm_domain *domain);
173 struct raw_notifier_head power_notifiers; /* Power on/off notifiers */
174 struct opp_table *opp_table; /* OPP table of the genpd */
175 int (*set_performance_state)(struct generic_pm_domain *genpd,
177 struct gpd_dev_ops dev_ops;
178 int (*set_hwmode_dev)(struct generic_pm_domain *domain,
179 struct device *dev, bool enable);
180 bool (*get_hwmode_dev)(struct generic_pm_domain *domain,
182 int (*attach_dev)(struct generic_pm_domain *domain,
184 void (*detach_dev)(struct generic_pm_domain *domain,
186 unsigned int flags; /* Bit field of configs for genpd */
187 struct genpd_power_state *states;
188 void (*free_states)(struct genpd_power_state *states,
189 unsigned int state_count);
190 unsigned int state_count; /* number of states */
191 unsigned int state_idx; /* state that genpd will go to when off */
194 const struct genpd_lock_ops *lock_ops;
199 unsigned long lock_flags;
205 static inline struct generic_pm_domain *pd_to_genpd(struct dev_pm_domain *pd)
207 return container_of(pd, struct generic_pm_domain, domain);
211 struct generic_pm_domain *parent;
212 struct list_head parent_node;
213 struct generic_pm_domain *child;
214 struct list_head child_node;
216 /* Sub-domain's per-master domain performance state */
217 unsigned int performance_state;
218 unsigned int prev_performance_state;
221 struct gpd_timing_data {
222 s64 suspend_latency_ns;
223 s64 resume_latency_ns;
224 s64 effective_constraint_ns;
226 bool constraint_changed;
227 bool cached_suspend_ok;
230 struct pm_domain_data {
231 struct list_head list_node;
235 struct generic_pm_domain_data {
236 struct pm_domain_data base;
237 struct gpd_timing_data *td;
238 struct notifier_block nb;
239 struct notifier_block *power_nb;
241 unsigned int performance_state;
242 unsigned int default_pstate;
243 unsigned int rpm_pstate;
248 #ifdef CONFIG_PM_GENERIC_DOMAINS
249 static inline struct generic_pm_domain_data *to_gpd_data(struct pm_domain_data *pdd)
251 return container_of(pdd, struct generic_pm_domain_data, base);
254 static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
256 return to_gpd_data(dev->power.subsys_data->domain_data);
259 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev);
260 int pm_genpd_remove_device(struct device *dev);
261 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
262 struct generic_pm_domain *subdomain);
263 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
264 struct generic_pm_domain *subdomain);
265 int pm_genpd_init(struct generic_pm_domain *genpd,
266 struct dev_power_governor *gov, bool is_off);
267 int pm_genpd_remove(struct generic_pm_domain *genpd);
268 struct device *dev_to_genpd_dev(struct device *dev);
269 int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state);
270 int dev_pm_genpd_add_notifier(struct device *dev, struct notifier_block *nb);
271 int dev_pm_genpd_remove_notifier(struct device *dev);
272 void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next);
273 ktime_t dev_pm_genpd_get_next_hrtimer(struct device *dev);
274 void dev_pm_genpd_synced_poweroff(struct device *dev);
275 int dev_pm_genpd_set_hwmode(struct device *dev, bool enable);
276 bool dev_pm_genpd_get_hwmode(struct device *dev);
278 extern struct dev_power_governor simple_qos_governor;
279 extern struct dev_power_governor pm_domain_always_on_gov;
280 #ifdef CONFIG_CPU_IDLE
281 extern struct dev_power_governor pm_domain_cpu_gov;
285 static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
287 return ERR_PTR(-ENOSYS);
289 static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
294 static inline int pm_genpd_remove_device(struct device *dev)
298 static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
299 struct generic_pm_domain *subdomain)
303 static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
304 struct generic_pm_domain *subdomain)
308 static inline int pm_genpd_init(struct generic_pm_domain *genpd,
309 struct dev_power_governor *gov, bool is_off)
313 static inline int pm_genpd_remove(struct generic_pm_domain *genpd)
318 static inline struct device *dev_to_genpd_dev(struct device *dev)
320 return ERR_PTR(-EOPNOTSUPP);
323 static inline int dev_pm_genpd_set_performance_state(struct device *dev,
329 static inline int dev_pm_genpd_add_notifier(struct device *dev,
330 struct notifier_block *nb)
335 static inline int dev_pm_genpd_remove_notifier(struct device *dev)
340 static inline void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next)
343 static inline ktime_t dev_pm_genpd_get_next_hrtimer(struct device *dev)
347 static inline void dev_pm_genpd_synced_poweroff(struct device *dev)
350 static inline int dev_pm_genpd_set_hwmode(struct device *dev, bool enable)
355 static inline bool dev_pm_genpd_get_hwmode(struct device *dev)
360 #define simple_qos_governor (*(struct dev_power_governor *)(NULL))
361 #define pm_domain_always_on_gov (*(struct dev_power_governor *)(NULL))
364 #ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
365 void dev_pm_genpd_suspend(struct device *dev);
366 void dev_pm_genpd_resume(struct device *dev);
368 static inline void dev_pm_genpd_suspend(struct device *dev) {}
369 static inline void dev_pm_genpd_resume(struct device *dev) {}
372 /* OF PM domain providers */
375 typedef struct generic_pm_domain *(*genpd_xlate_t)(const struct of_phandle_args *args,
378 struct genpd_onecell_data {
379 struct generic_pm_domain **domains;
380 unsigned int num_domains;
384 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
385 int of_genpd_add_provider_simple(struct device_node *np,
386 struct generic_pm_domain *genpd);
387 int of_genpd_add_provider_onecell(struct device_node *np,
388 struct genpd_onecell_data *data);
389 void of_genpd_del_provider(struct device_node *np);
390 int of_genpd_add_device(const struct of_phandle_args *args, struct device *dev);
391 int of_genpd_add_subdomain(const struct of_phandle_args *parent_spec,
392 const struct of_phandle_args *subdomain_spec);
393 int of_genpd_remove_subdomain(const struct of_phandle_args *parent_spec,
394 const struct of_phandle_args *subdomain_spec);
395 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np);
396 int of_genpd_parse_idle_states(struct device_node *dn,
397 struct genpd_power_state **states, int *n);
399 int genpd_dev_pm_attach(struct device *dev);
400 struct device *genpd_dev_pm_attach_by_id(struct device *dev,
402 struct device *genpd_dev_pm_attach_by_name(struct device *dev,
404 #else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
405 static inline int of_genpd_add_provider_simple(struct device_node *np,
406 struct generic_pm_domain *genpd)
411 static inline int of_genpd_add_provider_onecell(struct device_node *np,
412 struct genpd_onecell_data *data)
417 static inline void of_genpd_del_provider(struct device_node *np) {}
419 static inline int of_genpd_add_device(const struct of_phandle_args *args,
425 static inline int of_genpd_add_subdomain(const struct of_phandle_args *parent_spec,
426 const struct of_phandle_args *subdomain_spec)
431 static inline int of_genpd_remove_subdomain(const struct of_phandle_args *parent_spec,
432 const struct of_phandle_args *subdomain_spec)
437 static inline int of_genpd_parse_idle_states(struct device_node *dn,
438 struct genpd_power_state **states, int *n)
443 static inline int genpd_dev_pm_attach(struct device *dev)
448 static inline struct device *genpd_dev_pm_attach_by_id(struct device *dev,
454 static inline struct device *genpd_dev_pm_attach_by_name(struct device *dev,
461 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
463 return ERR_PTR(-EOPNOTSUPP);
465 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
468 int dev_pm_domain_attach(struct device *dev, bool power_on);
469 struct device *dev_pm_domain_attach_by_id(struct device *dev,
471 struct device *dev_pm_domain_attach_by_name(struct device *dev,
473 int dev_pm_domain_attach_list(struct device *dev,
474 const struct dev_pm_domain_attach_data *data,
475 struct dev_pm_domain_list **list);
476 void dev_pm_domain_detach(struct device *dev, bool power_off);
477 void dev_pm_domain_detach_list(struct dev_pm_domain_list *list);
478 int dev_pm_domain_start(struct device *dev);
479 void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
480 int dev_pm_domain_set_performance_state(struct device *dev, unsigned int state);
482 static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
486 static inline struct device *dev_pm_domain_attach_by_id(struct device *dev,
491 static inline struct device *dev_pm_domain_attach_by_name(struct device *dev,
496 static inline int dev_pm_domain_attach_list(struct device *dev,
497 const struct dev_pm_domain_attach_data *data,
498 struct dev_pm_domain_list **list)
502 static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
503 static inline void dev_pm_domain_detach_list(struct dev_pm_domain_list *list) {}
504 static inline int dev_pm_domain_start(struct device *dev)
508 static inline void dev_pm_domain_set(struct device *dev,
509 struct dev_pm_domain *pd) {}
510 static inline int dev_pm_domain_set_performance_state(struct device *dev,
517 #endif /* _LINUX_PM_DOMAIN_H */