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/mutex.h>
14 #include <linux/err.h>
16 #include <linux/notifier.h>
17 #include <linux/spinlock.h>
18 #include <linux/cpumask.h>
21 * Flags to control the behaviour of a genpd.
23 * These flags may be set in the struct generic_pm_domain's flags field by a
24 * genpd backend driver. The flags must be set before it calls pm_genpd_init(),
25 * which initializes a genpd.
27 * GENPD_FLAG_PM_CLK: Instructs genpd to use the PM clk framework,
28 * while powering on/off attached devices.
30 * GENPD_FLAG_IRQ_SAFE: This informs genpd that its backend callbacks,
31 * ->power_on|off(), doesn't sleep. Hence, these
32 * can be invoked from within atomic context, which
33 * enables genpd to power on/off the PM domain,
34 * even when pm_runtime_is_irq_safe() returns true,
35 * for any of its attached devices. Note that, a
36 * genpd having this flag set, requires its
37 * masterdomains to also have it set.
39 * GENPD_FLAG_ALWAYS_ON: Instructs genpd to always keep the PM domain
42 * GENPD_FLAG_ACTIVE_WAKEUP: Instructs genpd to keep the PM domain powered
43 * on, in case any of its attached devices is used
44 * in the wakeup path to serve system wakeups.
46 * GENPD_FLAG_CPU_DOMAIN: Instructs genpd that it should expect to get
47 * devices attached, which may belong to CPUs or
48 * possibly have subdomains with CPUs attached.
49 * This flag enables the genpd backend driver to
50 * deploy idle power management support for CPUs
51 * and groups of CPUs. Note that, the backend
52 * driver must then comply with the so called,
53 * last-man-standing algorithm, for the CPUs in the
56 * GENPD_FLAG_RPM_ALWAYS_ON: Instructs genpd to always keep the PM domain
57 * powered on except for system suspend.
59 #define GENPD_FLAG_PM_CLK (1U << 0)
60 #define GENPD_FLAG_IRQ_SAFE (1U << 1)
61 #define GENPD_FLAG_ALWAYS_ON (1U << 2)
62 #define GENPD_FLAG_ACTIVE_WAKEUP (1U << 3)
63 #define GENPD_FLAG_CPU_DOMAIN (1U << 4)
64 #define GENPD_FLAG_RPM_ALWAYS_ON (1U << 5)
67 GPD_STATE_ACTIVE
= 0, /* PM domain is active */
68 GPD_STATE_POWER_OFF
, /* PM domain is off */
71 struct dev_power_governor
{
72 bool (*power_down_ok
)(struct dev_pm_domain
*domain
);
73 bool (*suspend_ok
)(struct device
*dev
);
77 int (*start
)(struct device
*dev
);
78 int (*stop
)(struct device
*dev
);
81 struct genpd_power_state
{
82 s64 power_off_latency_ns
;
83 s64 power_on_latency_ns
;
85 struct fwnode_handle
*fwnode
;
90 struct genpd_lock_ops
;
94 struct generic_pm_domain
{
96 struct dev_pm_domain domain
; /* PM domain operations */
97 struct list_head gpd_list_node
; /* Node in the global PM domains list */
98 struct list_head master_links
; /* Links with PM domain as a master */
99 struct list_head slave_links
; /* Links with PM domain as a slave */
100 struct list_head dev_list
; /* List of devices */
101 struct dev_power_governor
*gov
;
102 struct work_struct power_off_work
;
103 struct fwnode_handle
*provider
; /* Identity of the domain provider */
106 atomic_t sd_count
; /* Number of subdomains with power "on" */
107 enum gpd_status status
; /* Current state of the domain */
108 unsigned int device_count
; /* Number of devices */
109 unsigned int suspended_count
; /* System suspend device counter */
110 unsigned int prepared_count
; /* Suspend counter of prepared devices */
111 unsigned int performance_state
; /* Aggregated max performance state */
112 cpumask_var_t cpus
; /* A cpumask of the attached CPUs */
113 int (*power_off
)(struct generic_pm_domain
*domain
);
114 int (*power_on
)(struct generic_pm_domain
*domain
);
115 struct opp_table
*opp_table
; /* OPP table of the genpd */
116 unsigned int (*opp_to_performance_state
)(struct generic_pm_domain
*genpd
,
117 struct dev_pm_opp
*opp
);
118 int (*set_performance_state
)(struct generic_pm_domain
*genpd
,
120 struct gpd_dev_ops dev_ops
;
121 s64 max_off_time_ns
; /* Maximum allowed "suspended" time. */
122 bool max_off_time_changed
;
123 bool cached_power_down_ok
;
124 bool cached_power_down_state_idx
;
125 int (*attach_dev
)(struct generic_pm_domain
*domain
,
127 void (*detach_dev
)(struct generic_pm_domain
*domain
,
129 unsigned int flags
; /* Bit field of configs for genpd */
130 struct genpd_power_state
*states
;
131 void (*free_states
)(struct genpd_power_state
*states
,
132 unsigned int state_count
);
133 unsigned int state_count
; /* number of states */
134 unsigned int state_idx
; /* state that genpd will go to when off */
136 ktime_t accounting_time
;
137 const struct genpd_lock_ops
*lock_ops
;
142 unsigned long lock_flags
;
148 static inline struct generic_pm_domain
*pd_to_genpd(struct dev_pm_domain
*pd
)
150 return container_of(pd
, struct generic_pm_domain
, domain
);
154 struct generic_pm_domain
*master
;
155 struct list_head master_node
;
156 struct generic_pm_domain
*slave
;
157 struct list_head slave_node
;
159 /* Sub-domain's per-master domain performance state */
160 unsigned int performance_state
;
161 unsigned int prev_performance_state
;
164 struct gpd_timing_data
{
165 s64 suspend_latency_ns
;
166 s64 resume_latency_ns
;
167 s64 effective_constraint_ns
;
168 bool constraint_changed
;
169 bool cached_suspend_ok
;
172 struct pm_domain_data
{
173 struct list_head list_node
;
177 struct generic_pm_domain_data
{
178 struct pm_domain_data base
;
179 struct gpd_timing_data td
;
180 struct notifier_block nb
;
182 unsigned int performance_state
;
186 #ifdef CONFIG_PM_GENERIC_DOMAINS
187 static inline struct generic_pm_domain_data
*to_gpd_data(struct pm_domain_data
*pdd
)
189 return container_of(pdd
, struct generic_pm_domain_data
, base
);
192 static inline struct generic_pm_domain_data
*dev_gpd_data(struct device
*dev
)
194 return to_gpd_data(dev
->power
.subsys_data
->domain_data
);
197 int pm_genpd_add_device(struct generic_pm_domain
*genpd
, struct device
*dev
);
198 int pm_genpd_remove_device(struct device
*dev
);
199 int pm_genpd_add_subdomain(struct generic_pm_domain
*genpd
,
200 struct generic_pm_domain
*new_subdomain
);
201 int pm_genpd_remove_subdomain(struct generic_pm_domain
*genpd
,
202 struct generic_pm_domain
*target
);
203 int pm_genpd_init(struct generic_pm_domain
*genpd
,
204 struct dev_power_governor
*gov
, bool is_off
);
205 int pm_genpd_remove(struct generic_pm_domain
*genpd
);
206 int dev_pm_genpd_set_performance_state(struct device
*dev
, unsigned int state
);
208 extern struct dev_power_governor simple_qos_governor
;
209 extern struct dev_power_governor pm_domain_always_on_gov
;
210 #ifdef CONFIG_CPU_IDLE
211 extern struct dev_power_governor pm_domain_cpu_gov
;
215 static inline struct generic_pm_domain_data
*dev_gpd_data(struct device
*dev
)
217 return ERR_PTR(-ENOSYS
);
219 static inline int pm_genpd_add_device(struct generic_pm_domain
*genpd
,
224 static inline int pm_genpd_remove_device(struct device
*dev
)
228 static inline int pm_genpd_add_subdomain(struct generic_pm_domain
*genpd
,
229 struct generic_pm_domain
*new_sd
)
233 static inline int pm_genpd_remove_subdomain(struct generic_pm_domain
*genpd
,
234 struct generic_pm_domain
*target
)
238 static inline int pm_genpd_init(struct generic_pm_domain
*genpd
,
239 struct dev_power_governor
*gov
, bool is_off
)
243 static inline int pm_genpd_remove(struct generic_pm_domain
*genpd
)
248 static inline int dev_pm_genpd_set_performance_state(struct device
*dev
,
254 #define simple_qos_governor (*(struct dev_power_governor *)(NULL))
255 #define pm_domain_always_on_gov (*(struct dev_power_governor *)(NULL))
258 #ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
259 void pm_genpd_syscore_poweroff(struct device
*dev
);
260 void pm_genpd_syscore_poweron(struct device
*dev
);
262 static inline void pm_genpd_syscore_poweroff(struct device
*dev
) {}
263 static inline void pm_genpd_syscore_poweron(struct device
*dev
) {}
266 /* OF PM domain providers */
269 typedef struct generic_pm_domain
*(*genpd_xlate_t
)(struct of_phandle_args
*args
,
272 struct genpd_onecell_data
{
273 struct generic_pm_domain
**domains
;
274 unsigned int num_domains
;
278 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
279 int of_genpd_add_provider_simple(struct device_node
*np
,
280 struct generic_pm_domain
*genpd
);
281 int of_genpd_add_provider_onecell(struct device_node
*np
,
282 struct genpd_onecell_data
*data
);
283 void of_genpd_del_provider(struct device_node
*np
);
284 int of_genpd_add_device(struct of_phandle_args
*args
, struct device
*dev
);
285 int of_genpd_add_subdomain(struct of_phandle_args
*parent
,
286 struct of_phandle_args
*new_subdomain
);
287 struct generic_pm_domain
*of_genpd_remove_last(struct device_node
*np
);
288 int of_genpd_parse_idle_states(struct device_node
*dn
,
289 struct genpd_power_state
**states
, int *n
);
290 unsigned int pm_genpd_opp_to_performance_state(struct device
*genpd_dev
,
291 struct dev_pm_opp
*opp
);
293 int genpd_dev_pm_attach(struct device
*dev
);
294 struct device
*genpd_dev_pm_attach_by_id(struct device
*dev
,
296 struct device
*genpd_dev_pm_attach_by_name(struct device
*dev
,
298 #else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
299 static inline int of_genpd_add_provider_simple(struct device_node
*np
,
300 struct generic_pm_domain
*genpd
)
305 static inline int of_genpd_add_provider_onecell(struct device_node
*np
,
306 struct genpd_onecell_data
*data
)
311 static inline void of_genpd_del_provider(struct device_node
*np
) {}
313 static inline int of_genpd_add_device(struct of_phandle_args
*args
,
319 static inline int of_genpd_add_subdomain(struct of_phandle_args
*parent
,
320 struct of_phandle_args
*new_subdomain
)
325 static inline int of_genpd_parse_idle_states(struct device_node
*dn
,
326 struct genpd_power_state
**states
, int *n
)
331 static inline unsigned int
332 pm_genpd_opp_to_performance_state(struct device
*genpd_dev
,
333 struct dev_pm_opp
*opp
)
338 static inline int genpd_dev_pm_attach(struct device
*dev
)
343 static inline struct device
*genpd_dev_pm_attach_by_id(struct device
*dev
,
349 static inline struct device
*genpd_dev_pm_attach_by_name(struct device
*dev
,
356 struct generic_pm_domain
*of_genpd_remove_last(struct device_node
*np
)
358 return ERR_PTR(-ENOTSUPP
);
360 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
363 int dev_pm_domain_attach(struct device
*dev
, bool power_on
);
364 struct device
*dev_pm_domain_attach_by_id(struct device
*dev
,
366 struct device
*dev_pm_domain_attach_by_name(struct device
*dev
,
368 void dev_pm_domain_detach(struct device
*dev
, bool power_off
);
369 void dev_pm_domain_set(struct device
*dev
, struct dev_pm_domain
*pd
);
371 static inline int dev_pm_domain_attach(struct device
*dev
, bool power_on
)
375 static inline struct device
*dev_pm_domain_attach_by_id(struct device
*dev
,
380 static inline struct device
*dev_pm_domain_attach_by_name(struct device
*dev
,
385 static inline void dev_pm_domain_detach(struct device
*dev
, bool power_off
) {}
386 static inline void dev_pm_domain_set(struct device
*dev
,
387 struct dev_pm_domain
*pd
) {}
390 #endif /* _LINUX_PM_DOMAIN_H */