2 * pm_domain.h - Definitions and headers related to device power domains.
6 * This file is released under the GPLv2.
9 #ifndef _LINUX_PM_DOMAIN_H
10 #define _LINUX_PM_DOMAIN_H
12 #include <linux/device.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.h>
22 * Flags to control the behaviour of a genpd.
24 * These flags may be set in the struct generic_pm_domain's flags field by a
25 * genpd backend driver. The flags must be set before it calls pm_genpd_init(),
26 * which initializes a genpd.
28 * GENPD_FLAG_PM_CLK: Instructs genpd to use the PM clk framework,
29 * while powering on/off attached devices.
31 * GENPD_FLAG_IRQ_SAFE: This informs genpd that its backend callbacks,
32 * ->power_on|off(), doesn't sleep. Hence, these
33 * can be invoked from within atomic context, which
34 * enables genpd to power on/off the PM domain,
35 * even when pm_runtime_is_irq_safe() returns true,
36 * for any of its attached devices. Note that, a
37 * genpd having this flag set, requires its
38 * masterdomains to also have it set.
40 * GENPD_FLAG_ALWAYS_ON: Instructs genpd to always keep the PM domain
43 * GENPD_FLAG_ACTIVE_WAKEUP: Instructs genpd to keep the PM domain powered
44 * on, in case any of its attached devices is used
45 * in the wakeup path to serve system wakeups.
47 * GENPD_FLAG_CPU_DOMAIN: Instructs genpd that it should expect to get
48 * devices attached, which may belong to CPUs or
49 * possibly have subdomains with CPUs attached.
50 * This flag enables the genpd backend driver to
51 * deploy idle power management support for CPUs
52 * and groups of CPUs. Note that, the backend
53 * driver must then comply with the so called,
54 * last-man-standing algorithm, for the CPUs in the
57 * GENPD_FLAG_RPM_ALWAYS_ON: Instructs genpd to always keep the PM domain
58 * powered on except for system suspend.
60 #define GENPD_FLAG_PM_CLK (1U << 0)
61 #define GENPD_FLAG_IRQ_SAFE (1U << 1)
62 #define GENPD_FLAG_ALWAYS_ON (1U << 2)
63 #define GENPD_FLAG_ACTIVE_WAKEUP (1U << 3)
64 #define GENPD_FLAG_CPU_DOMAIN (1U << 4)
65 #define GENPD_FLAG_RPM_ALWAYS_ON (1U << 5)
68 GPD_STATE_ACTIVE = 0, /* PM domain is active */
69 GPD_STATE_POWER_OFF, /* PM domain is off */
72 struct dev_power_governor {
73 bool (*power_down_ok)(struct dev_pm_domain *domain);
74 bool (*suspend_ok)(struct device *dev);
78 int (*start)(struct device *dev);
79 int (*stop)(struct device *dev);
82 struct genpd_power_state {
83 s64 power_off_latency_ns;
84 s64 power_on_latency_ns;
86 struct fwnode_handle *fwnode;
91 struct genpd_lock_ops;
95 struct generic_pm_domain {
97 struct dev_pm_domain domain; /* PM domain operations */
98 struct list_head gpd_list_node; /* Node in the global PM domains list */
99 struct list_head master_links; /* Links with PM domain as a master */
100 struct list_head slave_links; /* Links with PM domain as a slave */
101 struct list_head dev_list; /* List of devices */
102 struct dev_power_governor *gov;
103 struct work_struct power_off_work;
104 struct fwnode_handle *provider; /* Identity of the domain provider */
107 atomic_t sd_count; /* Number of subdomains with power "on" */
108 enum gpd_status status; /* Current state of the domain */
109 unsigned int device_count; /* Number of devices */
110 unsigned int suspended_count; /* System suspend device counter */
111 unsigned int prepared_count; /* Suspend counter of prepared devices */
112 unsigned int performance_state; /* Aggregated max performance state */
113 cpumask_var_t cpus; /* A cpumask of the attached CPUs */
114 int (*power_off)(struct generic_pm_domain *domain);
115 int (*power_on)(struct generic_pm_domain *domain);
116 struct opp_table *opp_table; /* OPP table of the genpd */
117 unsigned int (*opp_to_performance_state)(struct generic_pm_domain *genpd,
118 struct dev_pm_opp *opp);
119 int (*set_performance_state)(struct generic_pm_domain *genpd,
121 struct gpd_dev_ops dev_ops;
122 s64 max_off_time_ns; /* Maximum allowed "suspended" time. */
123 bool max_off_time_changed;
124 bool cached_power_down_ok;
125 bool cached_power_down_state_idx;
126 int (*attach_dev)(struct generic_pm_domain *domain,
128 void (*detach_dev)(struct generic_pm_domain *domain,
130 unsigned int flags; /* Bit field of configs for genpd */
131 struct genpd_power_state *states;
132 void (*free_states)(struct genpd_power_state *states,
133 unsigned int state_count);
134 unsigned int state_count; /* number of states */
135 unsigned int state_idx; /* state that genpd will go to when off */
137 ktime_t accounting_time;
138 const struct genpd_lock_ops *lock_ops;
143 unsigned long lock_flags;
149 static inline struct generic_pm_domain *pd_to_genpd(struct dev_pm_domain *pd)
151 return container_of(pd, struct generic_pm_domain, domain);
155 struct generic_pm_domain *master;
156 struct list_head master_node;
157 struct generic_pm_domain *slave;
158 struct list_head slave_node;
160 /* Sub-domain's per-master domain performance state */
161 unsigned int performance_state;
162 unsigned int prev_performance_state;
165 struct gpd_timing_data {
166 s64 suspend_latency_ns;
167 s64 resume_latency_ns;
168 s64 effective_constraint_ns;
169 bool constraint_changed;
170 bool cached_suspend_ok;
173 struct pm_domain_data {
174 struct list_head list_node;
178 struct generic_pm_domain_data {
179 struct pm_domain_data base;
180 struct gpd_timing_data td;
181 struct notifier_block nb;
183 unsigned int performance_state;
187 #ifdef CONFIG_PM_GENERIC_DOMAINS
188 static inline struct generic_pm_domain_data *to_gpd_data(struct pm_domain_data *pdd)
190 return container_of(pdd, struct generic_pm_domain_data, base);
193 static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
195 return to_gpd_data(dev->power.subsys_data->domain_data);
198 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev);
199 int pm_genpd_remove_device(struct device *dev);
200 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
201 struct generic_pm_domain *new_subdomain);
202 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
203 struct generic_pm_domain *target);
204 int pm_genpd_init(struct generic_pm_domain *genpd,
205 struct dev_power_governor *gov, bool is_off);
206 int pm_genpd_remove(struct generic_pm_domain *genpd);
207 int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state);
209 extern struct dev_power_governor simple_qos_governor;
210 extern struct dev_power_governor pm_domain_always_on_gov;
211 #ifdef CONFIG_CPU_IDLE
212 extern struct dev_power_governor pm_domain_cpu_gov;
216 static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
218 return ERR_PTR(-ENOSYS);
220 static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
225 static inline int pm_genpd_remove_device(struct device *dev)
229 static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
230 struct generic_pm_domain *new_sd)
234 static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
235 struct generic_pm_domain *target)
239 static inline int pm_genpd_init(struct generic_pm_domain *genpd,
240 struct dev_power_governor *gov, bool is_off)
244 static inline int pm_genpd_remove(struct generic_pm_domain *genpd)
249 static inline int dev_pm_genpd_set_performance_state(struct device *dev,
255 #define simple_qos_governor (*(struct dev_power_governor *)(NULL))
256 #define pm_domain_always_on_gov (*(struct dev_power_governor *)(NULL))
259 #ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
260 void pm_genpd_syscore_poweroff(struct device *dev);
261 void pm_genpd_syscore_poweron(struct device *dev);
263 static inline void pm_genpd_syscore_poweroff(struct device *dev) {}
264 static inline void pm_genpd_syscore_poweron(struct device *dev) {}
267 /* OF PM domain providers */
270 typedef struct generic_pm_domain *(*genpd_xlate_t)(struct of_phandle_args *args,
273 struct genpd_onecell_data {
274 struct generic_pm_domain **domains;
275 unsigned int num_domains;
279 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
280 int of_genpd_add_provider_simple(struct device_node *np,
281 struct generic_pm_domain *genpd);
282 int of_genpd_add_provider_onecell(struct device_node *np,
283 struct genpd_onecell_data *data);
284 void of_genpd_del_provider(struct device_node *np);
285 int of_genpd_add_device(struct of_phandle_args *args, struct device *dev);
286 int of_genpd_add_subdomain(struct of_phandle_args *parent,
287 struct of_phandle_args *new_subdomain);
288 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np);
289 int of_genpd_parse_idle_states(struct device_node *dn,
290 struct genpd_power_state **states, int *n);
291 unsigned int pm_genpd_opp_to_performance_state(struct device *genpd_dev,
292 struct dev_pm_opp *opp);
294 int genpd_dev_pm_attach(struct device *dev);
295 struct device *genpd_dev_pm_attach_by_id(struct device *dev,
297 struct device *genpd_dev_pm_attach_by_name(struct device *dev,
299 #else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
300 static inline int of_genpd_add_provider_simple(struct device_node *np,
301 struct generic_pm_domain *genpd)
306 static inline int of_genpd_add_provider_onecell(struct device_node *np,
307 struct genpd_onecell_data *data)
312 static inline void of_genpd_del_provider(struct device_node *np) {}
314 static inline int of_genpd_add_device(struct of_phandle_args *args,
320 static inline int of_genpd_add_subdomain(struct of_phandle_args *parent,
321 struct of_phandle_args *new_subdomain)
326 static inline int of_genpd_parse_idle_states(struct device_node *dn,
327 struct genpd_power_state **states, int *n)
332 static inline unsigned int
333 pm_genpd_opp_to_performance_state(struct device *genpd_dev,
334 struct dev_pm_opp *opp)
339 static inline int genpd_dev_pm_attach(struct device *dev)
344 static inline struct device *genpd_dev_pm_attach_by_id(struct device *dev,
350 static inline struct device *genpd_dev_pm_attach_by_name(struct device *dev,
357 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
359 return ERR_PTR(-ENOTSUPP);
361 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
364 int dev_pm_domain_attach(struct device *dev, bool power_on);
365 struct device *dev_pm_domain_attach_by_id(struct device *dev,
367 struct device *dev_pm_domain_attach_by_name(struct device *dev,
369 void dev_pm_domain_detach(struct device *dev, bool power_off);
370 void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
372 static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
376 static inline struct device *dev_pm_domain_attach_by_id(struct device *dev,
381 static inline struct device *dev_pm_domain_attach_by_name(struct device *dev,
386 static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
387 static inline void dev_pm_domain_set(struct device *dev,
388 struct dev_pm_domain *pd) {}
391 #endif /* _LINUX_PM_DOMAIN_H */