1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * pm_domain.h - Definitions and headers related to device power domains.
5 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
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
33 * PD_FLAG_REQUIRED_OPP: Assign required_devs for the required OPPs. The
34 * index of the required OPP must correspond to the
35 * index in the array of the pd_names. If pd_names
36 * isn't specified, the index just follows the
37 * index for the attached PM domain.
40 #define PD_FLAG_NO_DEV_LINK BIT(0)
41 #define PD_FLAG_DEV_LINK_ON BIT(1)
42 #define PD_FLAG_REQUIRED_OPP BIT(2)
44 struct dev_pm_domain_attach_data {
45 const char * const *pd_names;
46 const u32 num_pd_names;
50 struct dev_pm_domain_list {
51 struct device **pd_devs;
52 struct device_link **pd_links;
58 * Flags to control the behaviour of a genpd.
60 * These flags may be set in the struct generic_pm_domain's flags field by a
61 * genpd backend driver. The flags must be set before it calls pm_genpd_init(),
62 * which initializes a genpd.
64 * GENPD_FLAG_PM_CLK: Instructs genpd to use the PM clk framework,
65 * while powering on/off attached devices.
67 * GENPD_FLAG_IRQ_SAFE: This informs genpd that its backend callbacks,
68 * ->power_on|off(), doesn't sleep. Hence, these
69 * can be invoked from within atomic context, which
70 * enables genpd to power on/off the PM domain,
71 * even when pm_runtime_is_irq_safe() returns true,
72 * for any of its attached devices. Note that, a
73 * genpd having this flag set, requires its
74 * masterdomains to also have it set.
76 * GENPD_FLAG_ALWAYS_ON: Instructs genpd to always keep the PM domain
79 * GENPD_FLAG_ACTIVE_WAKEUP: Instructs genpd to keep the PM domain powered
80 * on, in case any of its attached devices is used
81 * in the wakeup path to serve system wakeups.
83 * GENPD_FLAG_CPU_DOMAIN: Instructs genpd that it should expect to get
84 * devices attached, which may belong to CPUs or
85 * possibly have subdomains with CPUs attached.
86 * This flag enables the genpd backend driver to
87 * deploy idle power management support for CPUs
88 * and groups of CPUs. Note that, the backend
89 * driver must then comply with the so called,
90 * last-man-standing algorithm, for the CPUs in the
93 * GENPD_FLAG_RPM_ALWAYS_ON: Instructs genpd to always keep the PM domain
94 * powered on except for system suspend.
96 * GENPD_FLAG_MIN_RESIDENCY: Enable the genpd governor to consider its
97 * components' next wakeup when determining the
100 * GENPD_FLAG_OPP_TABLE_FW: The genpd provider supports performance states,
101 * but its corresponding OPP tables are not
102 * described in DT, but are given directly by FW.
104 * GENPD_FLAG_DEV_NAME_FW: Instructs genpd to generate an unique device name
105 * using ida. It is used by genpd providers which
106 * get their genpd-names directly from FW.
108 #define GENPD_FLAG_PM_CLK (1U << 0)
109 #define GENPD_FLAG_IRQ_SAFE (1U << 1)
110 #define GENPD_FLAG_ALWAYS_ON (1U << 2)
111 #define GENPD_FLAG_ACTIVE_WAKEUP (1U << 3)
112 #define GENPD_FLAG_CPU_DOMAIN (1U << 4)
113 #define GENPD_FLAG_RPM_ALWAYS_ON (1U << 5)
114 #define GENPD_FLAG_MIN_RESIDENCY (1U << 6)
115 #define GENPD_FLAG_OPP_TABLE_FW (1U << 7)
116 #define GENPD_FLAG_DEV_NAME_FW (1U << 8)
119 GENPD_STATE_ON = 0, /* PM domain is on */
120 GENPD_STATE_OFF, /* PM domain is off */
123 enum genpd_notication {
124 GENPD_NOTIFY_PRE_OFF = 0,
130 struct dev_power_governor {
131 bool (*power_down_ok)(struct dev_pm_domain *domain);
132 bool (*suspend_ok)(struct device *dev);
136 int (*start)(struct device *dev);
137 int (*stop)(struct device *dev);
140 struct genpd_governor_data {
142 bool max_off_time_changed;
144 ktime_t next_hrtimer;
146 bool reflect_residency;
147 bool cached_power_down_ok;
148 bool cached_power_down_state_idx;
151 struct genpd_power_state {
153 s64 power_off_latency_ns;
154 s64 power_on_latency_ns;
160 struct fwnode_handle *fwnode;
165 struct genpd_lock_ops;
168 struct generic_pm_domain {
170 struct dev_pm_domain domain; /* PM domain operations */
171 struct list_head gpd_list_node; /* Node in the global PM domains list */
172 struct list_head parent_links; /* Links with PM domain as a parent */
173 struct list_head child_links; /* Links with PM domain as a child */
174 struct list_head dev_list; /* List of devices */
175 struct dev_power_governor *gov;
176 struct genpd_governor_data *gd; /* Data used by a genpd governor. */
177 struct work_struct power_off_work;
178 struct fwnode_handle *provider; /* Identity of the domain provider */
181 atomic_t sd_count; /* Number of subdomains with power "on" */
182 enum gpd_status status; /* Current state of the domain */
183 unsigned int device_count; /* Number of devices */
184 unsigned int device_id; /* unique device id */
185 unsigned int suspended_count; /* System suspend device counter */
186 unsigned int prepared_count; /* Suspend counter of prepared devices */
187 unsigned int performance_state; /* Aggregated max performance state */
188 cpumask_var_t cpus; /* A cpumask of the attached CPUs */
189 bool synced_poweroff; /* A consumer needs a synced poweroff */
190 int (*power_off)(struct generic_pm_domain *domain);
191 int (*power_on)(struct generic_pm_domain *domain);
192 struct raw_notifier_head power_notifiers; /* Power on/off notifiers */
193 struct opp_table *opp_table; /* OPP table of the genpd */
194 int (*set_performance_state)(struct generic_pm_domain *genpd,
196 struct gpd_dev_ops dev_ops;
197 int (*set_hwmode_dev)(struct generic_pm_domain *domain,
198 struct device *dev, bool enable);
199 bool (*get_hwmode_dev)(struct generic_pm_domain *domain,
201 int (*attach_dev)(struct generic_pm_domain *domain,
203 void (*detach_dev)(struct generic_pm_domain *domain,
205 unsigned int flags; /* Bit field of configs for genpd */
206 struct genpd_power_state *states;
207 void (*free_states)(struct genpd_power_state *states,
208 unsigned int state_count);
209 unsigned int state_count; /* number of states */
210 unsigned int state_idx; /* state that genpd will go to when off */
213 const struct genpd_lock_ops *lock_ops;
218 unsigned long lock_flags;
221 raw_spinlock_t raw_slock;
222 unsigned long raw_lock_flags;
227 static inline struct generic_pm_domain *pd_to_genpd(struct dev_pm_domain *pd)
229 return container_of(pd, struct generic_pm_domain, domain);
233 struct generic_pm_domain *parent;
234 struct list_head parent_node;
235 struct generic_pm_domain *child;
236 struct list_head child_node;
238 /* Sub-domain's per-master domain performance state */
239 unsigned int performance_state;
240 unsigned int prev_performance_state;
243 struct gpd_timing_data {
244 s64 suspend_latency_ns;
245 s64 resume_latency_ns;
246 s64 effective_constraint_ns;
248 bool constraint_changed;
249 bool cached_suspend_ok;
252 struct pm_domain_data {
253 struct list_head list_node;
257 struct generic_pm_domain_data {
258 struct pm_domain_data base;
259 struct gpd_timing_data *td;
260 struct notifier_block nb;
261 struct notifier_block *power_nb;
263 unsigned int performance_state;
264 unsigned int default_pstate;
265 unsigned int rpm_pstate;
266 unsigned int opp_token;
272 #ifdef CONFIG_PM_GENERIC_DOMAINS
273 static inline struct generic_pm_domain_data *to_gpd_data(struct pm_domain_data *pdd)
275 return container_of(pdd, struct generic_pm_domain_data, base);
278 static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
280 return to_gpd_data(dev->power.subsys_data->domain_data);
283 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev);
284 int pm_genpd_remove_device(struct device *dev);
285 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
286 struct generic_pm_domain *subdomain);
287 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
288 struct generic_pm_domain *subdomain);
289 int pm_genpd_init(struct generic_pm_domain *genpd,
290 struct dev_power_governor *gov, bool is_off);
291 int pm_genpd_remove(struct generic_pm_domain *genpd);
292 void pm_genpd_inc_rejected(struct generic_pm_domain *genpd,
293 unsigned int state_idx);
294 struct device *dev_to_genpd_dev(struct device *dev);
295 int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state);
296 int dev_pm_genpd_add_notifier(struct device *dev, struct notifier_block *nb);
297 int dev_pm_genpd_remove_notifier(struct device *dev);
298 void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next);
299 ktime_t dev_pm_genpd_get_next_hrtimer(struct device *dev);
300 void dev_pm_genpd_synced_poweroff(struct device *dev);
301 int dev_pm_genpd_set_hwmode(struct device *dev, bool enable);
302 bool dev_pm_genpd_get_hwmode(struct device *dev);
303 int dev_pm_genpd_rpm_always_on(struct device *dev, bool on);
305 extern struct dev_power_governor simple_qos_governor;
306 extern struct dev_power_governor pm_domain_always_on_gov;
307 #ifdef CONFIG_CPU_IDLE
308 extern struct dev_power_governor pm_domain_cpu_gov;
312 static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
314 return ERR_PTR(-ENOSYS);
316 static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
321 static inline int pm_genpd_remove_device(struct device *dev)
325 static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
326 struct generic_pm_domain *subdomain)
330 static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
331 struct generic_pm_domain *subdomain)
335 static inline int pm_genpd_init(struct generic_pm_domain *genpd,
336 struct dev_power_governor *gov, bool is_off)
340 static inline int pm_genpd_remove(struct generic_pm_domain *genpd)
345 static inline void pm_genpd_inc_rejected(struct generic_pm_domain *genpd,
346 unsigned int state_idx)
349 static inline struct device *dev_to_genpd_dev(struct device *dev)
351 return ERR_PTR(-EOPNOTSUPP);
354 static inline int dev_pm_genpd_set_performance_state(struct device *dev,
360 static inline int dev_pm_genpd_add_notifier(struct device *dev,
361 struct notifier_block *nb)
366 static inline int dev_pm_genpd_remove_notifier(struct device *dev)
371 static inline void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next)
374 static inline ktime_t dev_pm_genpd_get_next_hrtimer(struct device *dev)
378 static inline void dev_pm_genpd_synced_poweroff(struct device *dev)
381 static inline int dev_pm_genpd_set_hwmode(struct device *dev, bool enable)
386 static inline bool dev_pm_genpd_get_hwmode(struct device *dev)
391 static inline int dev_pm_genpd_rpm_always_on(struct device *dev, bool on)
396 #define simple_qos_governor (*(struct dev_power_governor *)(NULL))
397 #define pm_domain_always_on_gov (*(struct dev_power_governor *)(NULL))
400 #ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
401 void dev_pm_genpd_suspend(struct device *dev);
402 void dev_pm_genpd_resume(struct device *dev);
404 static inline void dev_pm_genpd_suspend(struct device *dev) {}
405 static inline void dev_pm_genpd_resume(struct device *dev) {}
408 /* OF PM domain providers */
411 typedef struct generic_pm_domain *(*genpd_xlate_t)(const struct of_phandle_args *args,
414 struct genpd_onecell_data {
415 struct generic_pm_domain **domains;
416 unsigned int num_domains;
420 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
421 int of_genpd_add_provider_simple(struct device_node *np,
422 struct generic_pm_domain *genpd);
423 int of_genpd_add_provider_onecell(struct device_node *np,
424 struct genpd_onecell_data *data);
425 void of_genpd_del_provider(struct device_node *np);
426 int of_genpd_add_device(const struct of_phandle_args *args, struct device *dev);
427 int of_genpd_add_subdomain(const struct of_phandle_args *parent_spec,
428 const struct of_phandle_args *subdomain_spec);
429 int of_genpd_remove_subdomain(const struct of_phandle_args *parent_spec,
430 const struct of_phandle_args *subdomain_spec);
431 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np);
432 int of_genpd_parse_idle_states(struct device_node *dn,
433 struct genpd_power_state **states, int *n);
435 int genpd_dev_pm_attach(struct device *dev);
436 struct device *genpd_dev_pm_attach_by_id(struct device *dev,
438 struct device *genpd_dev_pm_attach_by_name(struct device *dev,
440 #else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
441 static inline int of_genpd_add_provider_simple(struct device_node *np,
442 struct generic_pm_domain *genpd)
447 static inline int of_genpd_add_provider_onecell(struct device_node *np,
448 struct genpd_onecell_data *data)
453 static inline void of_genpd_del_provider(struct device_node *np) {}
455 static inline int of_genpd_add_device(const struct of_phandle_args *args,
461 static inline int of_genpd_add_subdomain(const struct of_phandle_args *parent_spec,
462 const struct of_phandle_args *subdomain_spec)
467 static inline int of_genpd_remove_subdomain(const struct of_phandle_args *parent_spec,
468 const struct of_phandle_args *subdomain_spec)
473 static inline int of_genpd_parse_idle_states(struct device_node *dn,
474 struct genpd_power_state **states, int *n)
479 static inline int genpd_dev_pm_attach(struct device *dev)
484 static inline struct device *genpd_dev_pm_attach_by_id(struct device *dev,
490 static inline struct device *genpd_dev_pm_attach_by_name(struct device *dev,
497 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
499 return ERR_PTR(-EOPNOTSUPP);
501 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
504 int dev_pm_domain_attach(struct device *dev, bool power_on);
505 struct device *dev_pm_domain_attach_by_id(struct device *dev,
507 struct device *dev_pm_domain_attach_by_name(struct device *dev,
509 int dev_pm_domain_attach_list(struct device *dev,
510 const struct dev_pm_domain_attach_data *data,
511 struct dev_pm_domain_list **list);
512 int devm_pm_domain_attach_list(struct device *dev,
513 const struct dev_pm_domain_attach_data *data,
514 struct dev_pm_domain_list **list);
515 void dev_pm_domain_detach(struct device *dev, bool power_off);
516 void dev_pm_domain_detach_list(struct dev_pm_domain_list *list);
517 int dev_pm_domain_start(struct device *dev);
518 void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
519 int dev_pm_domain_set_performance_state(struct device *dev, unsigned int state);
521 static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
525 static inline struct device *dev_pm_domain_attach_by_id(struct device *dev,
530 static inline struct device *dev_pm_domain_attach_by_name(struct device *dev,
535 static inline int dev_pm_domain_attach_list(struct device *dev,
536 const struct dev_pm_domain_attach_data *data,
537 struct dev_pm_domain_list **list)
542 static inline int devm_pm_domain_attach_list(struct device *dev,
543 const struct dev_pm_domain_attach_data *data,
544 struct dev_pm_domain_list **list)
549 static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
550 static inline void dev_pm_domain_detach_list(struct dev_pm_domain_list *list) {}
551 static inline int dev_pm_domain_start(struct device *dev)
555 static inline void dev_pm_domain_set(struct device *dev,
556 struct dev_pm_domain *pd) {}
557 static inline int dev_pm_domain_set_performance_state(struct device *dev,
564 #endif /* _LINUX_PM_DOMAIN_H */