1/*
2 * pm_domain.h - Definitions and headers related to device power domains.
3 *
4 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
5 *
6 * This file is released under the GPLv2.
7 */
8
9#ifndef _LINUX_PM_DOMAIN_H
10#define _LINUX_PM_DOMAIN_H
11
12#include <linux/device.h>
13#include <linux/mutex.h>
14#include <linux/pm.h>
15#include <linux/err.h>
16#include <linux/of.h>
17#include <linux/notifier.h>
18#include <linux/spinlock.h>
19
20/*
21 * Flags to control the behaviour of a genpd.
22 *
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.
26 *
27 * GENPD_FLAG_PM_CLK: Instructs genpd to use the PM clk framework,
28 * while powering on/off attached devices.
29 *
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.
38 *
39 * GENPD_FLAG_ALWAYS_ON: Instructs genpd to always keep the PM domain
40 * powered on.
41 *
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.
45 */
46#define GENPD_FLAG_PM_CLK (1U << 0)
47#define GENPD_FLAG_IRQ_SAFE (1U << 1)
48#define GENPD_FLAG_ALWAYS_ON (1U << 2)
49#define GENPD_FLAG_ACTIVE_WAKEUP (1U << 3)
50
51enum gpd_status {
52 GPD_STATE_ACTIVE = 0, /* PM domain is active */
53 GPD_STATE_POWER_OFF, /* PM domain is off */
54};
55
56struct dev_power_governor {
57 bool (*power_down_ok)(struct dev_pm_domain *domain);
58 bool (*suspend_ok)(struct device *dev);
59};
60
61struct gpd_dev_ops {
62 int (*start)(struct device *dev);
63 int (*stop)(struct device *dev);
64};
65
66struct genpd_power_state {
67 s64 power_off_latency_ns;
68 s64 power_on_latency_ns;
69 s64 residency_ns;
70 struct fwnode_handle *fwnode;
71 ktime_t idle_time;
72};
73
74struct genpd_lock_ops;
75struct dev_pm_opp;
76struct opp_table;
77
78struct generic_pm_domain {
79 struct device dev;
80 struct dev_pm_domain domain; /* PM domain operations */
81 struct list_head gpd_list_node; /* Node in the global PM domains list */
82 struct list_head master_links; /* Links with PM domain as a master */
83 struct list_head slave_links; /* Links with PM domain as a slave */
84 struct list_head dev_list; /* List of devices */
85 struct dev_power_governor *gov;
86 struct work_struct power_off_work;
87 struct fwnode_handle *provider; /* Identity of the domain provider */
88 bool has_provider;
89 const char *name;
90 atomic_t sd_count; /* Number of subdomains with power "on" */
91 enum gpd_status status; /* Current state of the domain */
92 unsigned int device_count; /* Number of devices */
93 unsigned int suspended_count; /* System suspend device counter */
94 unsigned int prepared_count; /* Suspend counter of prepared devices */
95 unsigned int performance_state; /* Aggregated max performance state */
96 int (*power_off)(struct generic_pm_domain *domain);
97 int (*power_on)(struct generic_pm_domain *domain);
98 struct opp_table *opp_table; /* OPP table of the genpd */
99 unsigned int (*opp_to_performance_state)(struct generic_pm_domain *genpd,
100 struct dev_pm_opp *opp);
101 int (*set_performance_state)(struct generic_pm_domain *genpd,
102 unsigned int state);
103 struct gpd_dev_ops dev_ops;
104 s64 max_off_time_ns; /* Maximum allowed "suspended" time. */
105 bool max_off_time_changed;
106 bool cached_power_down_ok;
107 int (*attach_dev)(struct generic_pm_domain *domain,
108 struct device *dev);
109 void (*detach_dev)(struct generic_pm_domain *domain,
110 struct device *dev);
111 unsigned int flags; /* Bit field of configs for genpd */
112 struct genpd_power_state *states;
113 unsigned int state_count; /* number of states */
114 unsigned int state_idx; /* state that genpd will go to when off */
115 void *free; /* Free the state that was allocated for default */
116 ktime_t on_time;
117 ktime_t accounting_time;
118 const struct genpd_lock_ops *lock_ops;
119 union {
120 struct mutex mlock;
121 struct {
122 spinlock_t slock;
123 unsigned long lock_flags;
124 };
125 };
126
127};
128
129static inline struct generic_pm_domain *pd_to_genpd(struct dev_pm_domain *pd)
130{
131 return container_of(pd, struct generic_pm_domain, domain);
132}
133
134struct gpd_link {
135 struct generic_pm_domain *master;
136 struct list_head master_node;
137 struct generic_pm_domain *slave;
138 struct list_head slave_node;
139
140 /* Sub-domain's per-master domain performance state */
141 unsigned int performance_state;
142 unsigned int prev_performance_state;
143};
144
145struct gpd_timing_data {
146 s64 suspend_latency_ns;
147 s64 resume_latency_ns;
148 s64 effective_constraint_ns;
149 bool constraint_changed;
150 bool cached_suspend_ok;
151};
152
153struct pm_domain_data {
154 struct list_head list_node;
155 struct device *dev;
156};
157
158struct generic_pm_domain_data {
159 struct pm_domain_data base;
160 struct gpd_timing_data td;
161 struct notifier_block nb;
162 unsigned int performance_state;
163 void *data;
164};
165
166#ifdef CONFIG_PM_GENERIC_DOMAINS
167static inline struct generic_pm_domain_data *to_gpd_data(struct pm_domain_data *pdd)
168{
169 return container_of(pdd, struct generic_pm_domain_data, base);
170}
171
172static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
173{
174 return to_gpd_data(dev->power.subsys_data->domain_data);
175}
176
177int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev);
178int pm_genpd_remove_device(struct device *dev);
179int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
180 struct generic_pm_domain *new_subdomain);
181int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
182 struct generic_pm_domain *target);
183int pm_genpd_init(struct generic_pm_domain *genpd,
184 struct dev_power_governor *gov, bool is_off);
185int pm_genpd_remove(struct generic_pm_domain *genpd);
186int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state);
187
188extern struct dev_power_governor simple_qos_governor;
189extern struct dev_power_governor pm_domain_always_on_gov;
190#else
191
192static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
193{
194 return ERR_PTR(-ENOSYS);
195}
196static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
197 struct device *dev)
198{
199 return -ENOSYS;
200}
201static inline int pm_genpd_remove_device(struct device *dev)
202{
203 return -ENOSYS;
204}
205static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
206 struct generic_pm_domain *new_sd)
207{
208 return -ENOSYS;
209}
210static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
211 struct generic_pm_domain *target)
212{
213 return -ENOSYS;
214}
215static inline int pm_genpd_init(struct generic_pm_domain *genpd,
216 struct dev_power_governor *gov, bool is_off)
217{
218 return -ENOSYS;
219}
220static inline int pm_genpd_remove(struct generic_pm_domain *genpd)
221{
222 return -ENOTSUPP;
223}
224
225static inline int dev_pm_genpd_set_performance_state(struct device *dev,
226 unsigned int state)
227{
228 return -ENOTSUPP;
229}
230
231#define simple_qos_governor (*(struct dev_power_governor *)(NULL))
232#define pm_domain_always_on_gov (*(struct dev_power_governor *)(NULL))
233#endif
234
235#ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
236void pm_genpd_syscore_poweroff(struct device *dev);
237void pm_genpd_syscore_poweron(struct device *dev);
238#else
239static inline void pm_genpd_syscore_poweroff(struct device *dev) {}
240static inline void pm_genpd_syscore_poweron(struct device *dev) {}
241#endif
242
243/* OF PM domain providers */
244struct of_device_id;
245
246typedef struct generic_pm_domain *(*genpd_xlate_t)(struct of_phandle_args *args,
247 void *data);
248
249struct genpd_onecell_data {
250 struct generic_pm_domain **domains;
251 unsigned int num_domains;
252 genpd_xlate_t xlate;
253};
254
255#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
256int of_genpd_add_provider_simple(struct device_node *np,
257 struct generic_pm_domain *genpd);
258int of_genpd_add_provider_onecell(struct device_node *np,
259 struct genpd_onecell_data *data);
260void of_genpd_del_provider(struct device_node *np);
261int of_genpd_add_device(struct of_phandle_args *args, struct device *dev);
262int of_genpd_add_subdomain(struct of_phandle_args *parent,
263 struct of_phandle_args *new_subdomain);
264struct generic_pm_domain *of_genpd_remove_last(struct device_node *np);
265int of_genpd_parse_idle_states(struct device_node *dn,
266 struct genpd_power_state **states, int *n);
267unsigned int pm_genpd_opp_to_performance_state(struct device *genpd_dev,
268 struct dev_pm_opp *opp);
269
270int genpd_dev_pm_attach(struct device *dev);
271struct device *genpd_dev_pm_attach_by_id(struct device *dev,
272 unsigned int index);
273struct device *genpd_dev_pm_attach_by_name(struct device *dev,
274 const char *name);
275#else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
276static inline int of_genpd_add_provider_simple(struct device_node *np,
277 struct generic_pm_domain *genpd)
278{
279 return -ENOTSUPP;
280}
281
282static inline int of_genpd_add_provider_onecell(struct device_node *np,
283 struct genpd_onecell_data *data)
284{
285 return -ENOTSUPP;
286}
287
288static inline void of_genpd_del_provider(struct device_node *np) {}
289
290static inline int of_genpd_add_device(struct of_phandle_args *args,
291 struct device *dev)
292{
293 return -ENODEV;
294}
295
296static inline int of_genpd_add_subdomain(struct of_phandle_args *parent,
297 struct of_phandle_args *new_subdomain)
298{
299 return -ENODEV;
300}
301
302static inline int of_genpd_parse_idle_states(struct device_node *dn,
303 struct genpd_power_state **states, int *n)
304{
305 return -ENODEV;
306}
307
308static inline unsigned int
309pm_genpd_opp_to_performance_state(struct device *genpd_dev,
310 struct dev_pm_opp *opp)
311{
312 return 0;
313}
314
315static inline int genpd_dev_pm_attach(struct device *dev)
316{
317 return 0;
318}
319
320static inline struct device *genpd_dev_pm_attach_by_id(struct device *dev,
321 unsigned int index)
322{
323 return NULL;
324}
325
326static inline struct device *genpd_dev_pm_attach_by_name(struct device *dev,
327 const char *name)
328{
329 return NULL;
330}
331
332static inline
333struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
334{
335 return ERR_PTR(-ENOTSUPP);
336}
337#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
338
339#ifdef CONFIG_PM
340int dev_pm_domain_attach(struct device *dev, bool power_on);
341struct device *dev_pm_domain_attach_by_id(struct device *dev,
342 unsigned int index);
343struct device *dev_pm_domain_attach_by_name(struct device *dev,
344 const char *name);
345void dev_pm_domain_detach(struct device *dev, bool power_off);
346void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
347#else
348static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
349{
350 return 0;
351}
352static inline struct device *dev_pm_domain_attach_by_id(struct device *dev,
353 unsigned int index)
354{
355 return NULL;
356}
357static inline struct device *dev_pm_domain_attach_by_name(struct device *dev,
358 const char *name)
359{
360 return NULL;
361}
362static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
363static inline void dev_pm_domain_set(struct device *dev,
364 struct dev_pm_domain *pd) {}
365#endif
366
367#endif /* _LINUX_PM_DOMAIN_H */
368