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.h>
20 #include <linux/time64.h>
23 * Flags to control the behaviour of a genpd.
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.
29 * GENPD_FLAG_PM_CLK: Instructs genpd to use the PM clk framework,
30 * while powering on/off attached devices.
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.
41 * GENPD_FLAG_ALWAYS_ON: Instructs genpd to always keep the PM domain
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.
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
58 * GENPD_FLAG_RPM_ALWAYS_ON: Instructs genpd to always keep the PM domain
59 * powered on except for system suspend.
61 * GENPD_FLAG_MIN_RESIDENCY: Enable the genpd governor to consider its
62 * components' next wakeup when determining the
65 #define GENPD_FLAG_PM_CLK (1U << 0)
66 #define GENPD_FLAG_IRQ_SAFE (1U << 1)
67 #define GENPD_FLAG_ALWAYS_ON (1U << 2)
68 #define GENPD_FLAG_ACTIVE_WAKEUP (1U << 3)
69 #define GENPD_FLAG_CPU_DOMAIN (1U << 4)
70 #define GENPD_FLAG_RPM_ALWAYS_ON (1U << 5)
71 #define GENPD_FLAG_MIN_RESIDENCY (1U << 6)
74 GENPD_STATE_ON = 0, /* PM domain is on */
75 GENPD_STATE_OFF, /* PM domain is off */
78 enum genpd_notication {
79 GENPD_NOTIFY_PRE_OFF = 0,
85 struct dev_power_governor {
86 bool (*power_down_ok)(struct dev_pm_domain *domain);
87 bool (*suspend_ok)(struct device *dev);
91 int (*start)(struct device *dev);
92 int (*stop)(struct device *dev);
95 struct genpd_governor_data {
97 bool max_off_time_changed;
100 bool cached_power_down_ok;
101 bool cached_power_down_state_idx;
104 struct genpd_power_state {
105 s64 power_off_latency_ns;
106 s64 power_on_latency_ns;
110 struct fwnode_handle *fwnode;
115 struct genpd_lock_ops;
119 struct generic_pm_domain {
121 struct dev_pm_domain domain; /* PM domain operations */
122 struct list_head gpd_list_node; /* Node in the global PM domains list */
123 struct list_head parent_links; /* Links with PM domain as a parent */
124 struct list_head child_links; /* Links with PM domain as a child */
125 struct list_head dev_list; /* List of devices */
126 struct dev_power_governor *gov;
127 struct genpd_governor_data *gd; /* Data used by a genpd governor. */
128 struct work_struct power_off_work;
129 struct fwnode_handle *provider; /* Identity of the domain provider */
132 atomic_t sd_count; /* Number of subdomains with power "on" */
133 enum gpd_status status; /* Current state of the domain */
134 unsigned int device_count; /* Number of devices */
135 unsigned int suspended_count; /* System suspend device counter */
136 unsigned int prepared_count; /* Suspend counter of prepared devices */
137 unsigned int performance_state; /* Aggregated max performance state */
138 cpumask_var_t cpus; /* A cpumask of the attached CPUs */
139 bool synced_poweroff; /* A consumer needs a synced poweroff */
140 int (*power_off)(struct generic_pm_domain *domain);
141 int (*power_on)(struct generic_pm_domain *domain);
142 struct raw_notifier_head power_notifiers; /* Power on/off notifiers */
143 struct opp_table *opp_table; /* OPP table of the genpd */
144 unsigned int (*opp_to_performance_state)(struct generic_pm_domain *genpd,
145 struct dev_pm_opp *opp);
146 int (*set_performance_state)(struct generic_pm_domain *genpd,
148 struct gpd_dev_ops dev_ops;
149 int (*attach_dev)(struct generic_pm_domain *domain,
151 void (*detach_dev)(struct generic_pm_domain *domain,
153 unsigned int flags; /* Bit field of configs for genpd */
154 struct genpd_power_state *states;
155 void (*free_states)(struct genpd_power_state *states,
156 unsigned int state_count);
157 unsigned int state_count; /* number of states */
158 unsigned int state_idx; /* state that genpd will go to when off */
161 const struct genpd_lock_ops *lock_ops;
166 unsigned long lock_flags;
172 static inline struct generic_pm_domain *pd_to_genpd(struct dev_pm_domain *pd)
174 return container_of(pd, struct generic_pm_domain, domain);
178 struct generic_pm_domain *parent;
179 struct list_head parent_node;
180 struct generic_pm_domain *child;
181 struct list_head child_node;
183 /* Sub-domain's per-master domain performance state */
184 unsigned int performance_state;
185 unsigned int prev_performance_state;
188 struct gpd_timing_data {
189 s64 suspend_latency_ns;
190 s64 resume_latency_ns;
191 s64 effective_constraint_ns;
193 bool constraint_changed;
194 bool cached_suspend_ok;
197 struct pm_domain_data {
198 struct list_head list_node;
202 struct generic_pm_domain_data {
203 struct pm_domain_data base;
204 struct gpd_timing_data *td;
205 struct notifier_block nb;
206 struct notifier_block *power_nb;
208 unsigned int performance_state;
209 unsigned int default_pstate;
210 unsigned int rpm_pstate;
214 #ifdef CONFIG_PM_GENERIC_DOMAINS
215 static inline struct generic_pm_domain_data *to_gpd_data(struct pm_domain_data *pdd)
217 return container_of(pdd, struct generic_pm_domain_data, base);
220 static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
222 return to_gpd_data(dev->power.subsys_data->domain_data);
225 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev);
226 int pm_genpd_remove_device(struct device *dev);
227 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
228 struct generic_pm_domain *subdomain);
229 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
230 struct generic_pm_domain *subdomain);
231 int pm_genpd_init(struct generic_pm_domain *genpd,
232 struct dev_power_governor *gov, bool is_off);
233 int pm_genpd_remove(struct generic_pm_domain *genpd);
234 int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state);
235 int dev_pm_genpd_add_notifier(struct device *dev, struct notifier_block *nb);
236 int dev_pm_genpd_remove_notifier(struct device *dev);
237 void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next);
238 ktime_t dev_pm_genpd_get_next_hrtimer(struct device *dev);
239 void dev_pm_genpd_synced_poweroff(struct device *dev);
241 extern struct dev_power_governor simple_qos_governor;
242 extern struct dev_power_governor pm_domain_always_on_gov;
243 #ifdef CONFIG_CPU_IDLE
244 extern struct dev_power_governor pm_domain_cpu_gov;
248 static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
250 return ERR_PTR(-ENOSYS);
252 static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
257 static inline int pm_genpd_remove_device(struct device *dev)
261 static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
262 struct generic_pm_domain *subdomain)
266 static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
267 struct generic_pm_domain *subdomain)
271 static inline int pm_genpd_init(struct generic_pm_domain *genpd,
272 struct dev_power_governor *gov, bool is_off)
276 static inline int pm_genpd_remove(struct generic_pm_domain *genpd)
281 static inline int dev_pm_genpd_set_performance_state(struct device *dev,
287 static inline int dev_pm_genpd_add_notifier(struct device *dev,
288 struct notifier_block *nb)
293 static inline int dev_pm_genpd_remove_notifier(struct device *dev)
298 static inline void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next)
301 static inline ktime_t dev_pm_genpd_get_next_hrtimer(struct device *dev)
305 static inline void dev_pm_genpd_synced_poweroff(struct device *dev)
308 #define simple_qos_governor (*(struct dev_power_governor *)(NULL))
309 #define pm_domain_always_on_gov (*(struct dev_power_governor *)(NULL))
312 #ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
313 void dev_pm_genpd_suspend(struct device *dev);
314 void dev_pm_genpd_resume(struct device *dev);
316 static inline void dev_pm_genpd_suspend(struct device *dev) {}
317 static inline void dev_pm_genpd_resume(struct device *dev) {}
320 /* OF PM domain providers */
323 typedef struct generic_pm_domain *(*genpd_xlate_t)(struct of_phandle_args *args,
326 struct genpd_onecell_data {
327 struct generic_pm_domain **domains;
328 unsigned int num_domains;
332 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
333 int of_genpd_add_provider_simple(struct device_node *np,
334 struct generic_pm_domain *genpd);
335 int of_genpd_add_provider_onecell(struct device_node *np,
336 struct genpd_onecell_data *data);
337 void of_genpd_del_provider(struct device_node *np);
338 int of_genpd_add_device(struct of_phandle_args *args, struct device *dev);
339 int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
340 struct of_phandle_args *subdomain_spec);
341 int of_genpd_remove_subdomain(struct of_phandle_args *parent_spec,
342 struct of_phandle_args *subdomain_spec);
343 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np);
344 int of_genpd_parse_idle_states(struct device_node *dn,
345 struct genpd_power_state **states, int *n);
346 unsigned int pm_genpd_opp_to_performance_state(struct device *genpd_dev,
347 struct dev_pm_opp *opp);
349 int genpd_dev_pm_attach(struct device *dev);
350 struct device *genpd_dev_pm_attach_by_id(struct device *dev,
352 struct device *genpd_dev_pm_attach_by_name(struct device *dev,
354 #else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
355 static inline int of_genpd_add_provider_simple(struct device_node *np,
356 struct generic_pm_domain *genpd)
361 static inline int of_genpd_add_provider_onecell(struct device_node *np,
362 struct genpd_onecell_data *data)
367 static inline void of_genpd_del_provider(struct device_node *np) {}
369 static inline int of_genpd_add_device(struct of_phandle_args *args,
375 static inline int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
376 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)
387 static inline int of_genpd_parse_idle_states(struct device_node *dn,
388 struct genpd_power_state **states, int *n)
393 static inline unsigned int
394 pm_genpd_opp_to_performance_state(struct device *genpd_dev,
395 struct dev_pm_opp *opp)
400 static inline int genpd_dev_pm_attach(struct device *dev)
405 static inline struct device *genpd_dev_pm_attach_by_id(struct device *dev,
411 static inline struct device *genpd_dev_pm_attach_by_name(struct device *dev,
418 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
420 return ERR_PTR(-EOPNOTSUPP);
422 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
425 int dev_pm_domain_attach(struct device *dev, bool power_on);
426 struct device *dev_pm_domain_attach_by_id(struct device *dev,
428 struct device *dev_pm_domain_attach_by_name(struct device *dev,
430 void dev_pm_domain_detach(struct device *dev, bool power_off);
431 int dev_pm_domain_start(struct device *dev);
432 void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
434 static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
438 static inline struct device *dev_pm_domain_attach_by_id(struct device *dev,
443 static inline struct device *dev_pm_domain_attach_by_name(struct device *dev,
448 static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
449 static inline int dev_pm_domain_start(struct device *dev)
453 static inline void dev_pm_domain_set(struct device *dev,
454 struct dev_pm_domain *pd) {}
457 #endif /* _LINUX_PM_DOMAIN_H */