treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 336
[linux-2.6-block.git] / include / linux / pm_domain.h
CommitLineData
f721889f
RW
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>
313162d0
PG
13#include <linux/mutex.h>
14#include <linux/pm.h>
4f042cda 15#include <linux/err.h>
c8aa130b 16#include <linux/of.h>
6ff7bb0d 17#include <linux/notifier.h>
d716f479 18#include <linux/spinlock.h>
eb594b73 19#include <linux/cpumask.h>
f721889f 20
e5089c2c
UH
21/*
22 * Flags to control the behaviour of a genpd.
23 *
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.
27 *
28 * GENPD_FLAG_PM_CLK: Instructs genpd to use the PM clk framework,
29 * while powering on/off attached devices.
30 *
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.
39 *
40 * GENPD_FLAG_ALWAYS_ON: Instructs genpd to always keep the PM domain
41 * powered on.
42 *
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.
eb594b73
UH
46 *
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
55 * PM domain.
ed61e18a
LC
56 *
57 * GENPD_FLAG_RPM_ALWAYS_ON: Instructs genpd to always keep the PM domain
58 * powered on except for system suspend.
e5089c2c
UH
59 */
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)
eb594b73 64#define GENPD_FLAG_CPU_DOMAIN (1U << 4)
ed61e18a 65#define GENPD_FLAG_RPM_ALWAYS_ON (1U << 5)
c11f6f5b 66
17b75eca
RW
67enum gpd_status {
68 GPD_STATE_ACTIVE = 0, /* PM domain is active */
17b75eca
RW
69 GPD_STATE_POWER_OFF, /* PM domain is off */
70};
596ba34b 71
f721889f
RW
72struct dev_power_governor {
73 bool (*power_down_ok)(struct dev_pm_domain *domain);
9df3921e 74 bool (*suspend_ok)(struct device *dev);
f721889f
RW
75};
76
d5e4cbfe
RW
77struct gpd_dev_ops {
78 int (*start)(struct device *dev);
79 int (*stop)(struct device *dev);
d5e4cbfe
RW
80};
81
fc5cbf0c
AH
82struct genpd_power_state {
83 s64 power_off_latency_ns;
84 s64 power_on_latency_ns;
405f7226 85 s64 residency_ns;
0c9b694a 86 struct fwnode_handle *fwnode;
afece3ab 87 ktime_t idle_time;
49a27e27 88 void *data;
fc5cbf0c
AH
89};
90
35241d12 91struct genpd_lock_ops;
6e41766a 92struct dev_pm_opp;
1067ae3e 93struct opp_table;
35241d12 94
f721889f 95struct generic_pm_domain {
401ea157 96 struct device dev;
f721889f 97 struct dev_pm_domain domain; /* PM domain operations */
5125bbf3 98 struct list_head gpd_list_node; /* Node in the global PM domains list */
5063ce15
RW
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 */
f721889f 101 struct list_head dev_list; /* List of devices */
f721889f
RW
102 struct dev_power_governor *gov;
103 struct work_struct power_off_work;
de0aa06d
JH
104 struct fwnode_handle *provider; /* Identity of the domain provider */
105 bool has_provider;
d07e9c17 106 const char *name;
c4bb3160 107 atomic_t sd_count; /* Number of subdomains with power "on" */
17b75eca 108 enum gpd_status status; /* Current state of the domain */
596ba34b
RW
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 */
42f6284a 112 unsigned int performance_state; /* Aggregated max performance state */
eb594b73 113 cpumask_var_t cpus; /* A cpumask of the attached CPUs */
f721889f
RW
114 int (*power_off)(struct generic_pm_domain *domain);
115 int (*power_on)(struct generic_pm_domain *domain);
1067ae3e 116 struct opp_table *opp_table; /* OPP table of the genpd */
6e41766a
VK
117 unsigned int (*opp_to_performance_state)(struct generic_pm_domain *genpd,
118 struct dev_pm_opp *opp);
42f6284a
VK
119 int (*set_performance_state)(struct generic_pm_domain *genpd,
120 unsigned int state);
d5e4cbfe 121 struct gpd_dev_ops dev_ops;
221e9b58 122 s64 max_off_time_ns; /* Maximum allowed "suspended" time. */
6ff7bb0d
RW
123 bool max_off_time_changed;
124 bool cached_power_down_ok;
e9499968 125 bool cached_power_down_state_idx;
c16561e8
UH
126 int (*attach_dev)(struct generic_pm_domain *domain,
127 struct device *dev);
128 void (*detach_dev)(struct generic_pm_domain *domain,
129 struct device *dev);
c11f6f5b 130 unsigned int flags; /* Bit field of configs for genpd */
59d65b73 131 struct genpd_power_state *states;
49a27e27
UH
132 void (*free_states)(struct genpd_power_state *states,
133 unsigned int state_count);
fc5cbf0c
AH
134 unsigned int state_count; /* number of states */
135 unsigned int state_idx; /* state that genpd will go to when off */
afece3ab
TG
136 ktime_t on_time;
137 ktime_t accounting_time;
35241d12 138 const struct genpd_lock_ops *lock_ops;
d716f479
LI
139 union {
140 struct mutex mlock;
141 struct {
142 spinlock_t slock;
143 unsigned long lock_flags;
144 };
145 };
fc5cbf0c 146
f721889f
RW
147};
148
596ba34b
RW
149static inline struct generic_pm_domain *pd_to_genpd(struct dev_pm_domain *pd)
150{
151 return container_of(pd, struct generic_pm_domain, domain);
152}
153
5063ce15
RW
154struct gpd_link {
155 struct generic_pm_domain *master;
156 struct list_head master_node;
157 struct generic_pm_domain *slave;
158 struct list_head slave_node;
18edf49c
VK
159
160 /* Sub-domain's per-master domain performance state */
161 unsigned int performance_state;
162 unsigned int prev_performance_state;
5063ce15
RW
163};
164
b02c999a 165struct gpd_timing_data {
2b1d88cd
UH
166 s64 suspend_latency_ns;
167 s64 resume_latency_ns;
a5bef810 168 s64 effective_constraint_ns;
6ff7bb0d 169 bool constraint_changed;
9df3921e 170 bool cached_suspend_ok;
b02c999a
RW
171};
172
00e7c295
UH
173struct pm_domain_data {
174 struct list_head list_node;
175 struct device *dev;
176};
177
cd0ea672
RW
178struct generic_pm_domain_data {
179 struct pm_domain_data base;
b02c999a 180 struct gpd_timing_data td;
6ff7bb0d 181 struct notifier_block nb;
f9ccd7c3 182 int cpu;
42f6284a 183 unsigned int performance_state;
a5ea7a0f 184 void *data;
cd0ea672
RW
185};
186
9b4f617b 187#ifdef CONFIG_PM_GENERIC_DOMAINS
cd0ea672
RW
188static inline struct generic_pm_domain_data *to_gpd_data(struct pm_domain_data *pdd)
189{
190 return container_of(pdd, struct generic_pm_domain_data, base);
191}
192
d5e4cbfe
RW
193static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
194{
195 return to_gpd_data(dev->power.subsys_data->domain_data);
196}
197
1a7a6707 198int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev);
924f4486 199int pm_genpd_remove_device(struct device *dev);
781b9d6b
UH
200int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
201 struct generic_pm_domain *new_subdomain);
202int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
203 struct generic_pm_domain *target);
204int pm_genpd_init(struct generic_pm_domain *genpd,
205 struct dev_power_governor *gov, bool is_off);
206int pm_genpd_remove(struct generic_pm_domain *genpd);
207int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state);
b02c999a 208
ae3c511c 209extern struct dev_power_governor simple_qos_governor;
925b44a2 210extern struct dev_power_governor pm_domain_always_on_gov;
e9499968
UH
211#ifdef CONFIG_CPU_IDLE
212extern struct dev_power_governor pm_domain_cpu_gov;
213#endif
f721889f 214#else
b02c999a 215
b642631d
MD
216static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
217{
218 return ERR_PTR(-ENOSYS);
219}
1a7a6707
UH
220static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
221 struct device *dev)
b02c999a
RW
222{
223 return -ENOSYS;
224}
924f4486 225static inline int pm_genpd_remove_device(struct device *dev)
f721889f
RW
226{
227 return -ENOSYS;
228}
229static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
230 struct generic_pm_domain *new_sd)
231{
232 return -ENOSYS;
233}
234static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
235 struct generic_pm_domain *target)
236{
237 return -ENOSYS;
238}
7eb231c3
UH
239static inline int pm_genpd_init(struct generic_pm_domain *genpd,
240 struct dev_power_governor *gov, bool is_off)
b02c999a 241{
7eb231c3 242 return -ENOSYS;
b02c999a 243}
3fe57710
JH
244static inline int pm_genpd_remove(struct generic_pm_domain *genpd)
245{
246 return -ENOTSUPP;
247}
16d0bf18 248
42f6284a
VK
249static inline int dev_pm_genpd_set_performance_state(struct device *dev,
250 unsigned int state)
251{
252 return -ENOTSUPP;
253}
254
16d0bf18
GU
255#define simple_qos_governor (*(struct dev_power_governor *)(NULL))
256#define pm_domain_always_on_gov (*(struct dev_power_governor *)(NULL))
17f2ae7f
RW
257#endif
258
77f827de 259#ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
781b9d6b
UH
260void pm_genpd_syscore_poweroff(struct device *dev);
261void pm_genpd_syscore_poweron(struct device *dev);
77f827de 262#else
d47e6464
UH
263static inline void pm_genpd_syscore_poweroff(struct device *dev) {}
264static inline void pm_genpd_syscore_poweron(struct device *dev) {}
77f827de
RW
265#endif
266
aa42240a
TF
267/* OF PM domain providers */
268struct of_device_id;
269
40845524
TR
270typedef struct generic_pm_domain *(*genpd_xlate_t)(struct of_phandle_args *args,
271 void *data);
272
aa42240a
TF
273struct genpd_onecell_data {
274 struct generic_pm_domain **domains;
275 unsigned int num_domains;
40845524 276 genpd_xlate_t xlate;
aa42240a
TF
277};
278
aa42240a 279#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
892ebdcc
JH
280int of_genpd_add_provider_simple(struct device_node *np,
281 struct generic_pm_domain *genpd);
282int of_genpd_add_provider_onecell(struct device_node *np,
283 struct genpd_onecell_data *data);
aa42240a 284void of_genpd_del_provider(struct device_node *np);
781b9d6b
UH
285int of_genpd_add_device(struct of_phandle_args *args, struct device *dev);
286int of_genpd_add_subdomain(struct of_phandle_args *parent,
287 struct of_phandle_args *new_subdomain);
288struct generic_pm_domain *of_genpd_remove_last(struct device_node *np);
289int of_genpd_parse_idle_states(struct device_node *dn,
290 struct genpd_power_state **states, int *n);
e38f89d3
VK
291unsigned int pm_genpd_opp_to_performance_state(struct device *genpd_dev,
292 struct dev_pm_opp *opp);
aa42240a
TF
293
294int genpd_dev_pm_attach(struct device *dev);
3c095f32
UH
295struct device *genpd_dev_pm_attach_by_id(struct device *dev,
296 unsigned int index);
5d6be70a 297struct device *genpd_dev_pm_attach_by_name(struct device *dev,
7416f1f2 298 const char *name);
aa42240a 299#else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
892ebdcc
JH
300static inline int of_genpd_add_provider_simple(struct device_node *np,
301 struct generic_pm_domain *genpd)
aa42240a 302{
892ebdcc
JH
303 return -ENOTSUPP;
304}
305
306static inline int of_genpd_add_provider_onecell(struct device_node *np,
307 struct genpd_onecell_data *data)
308{
309 return -ENOTSUPP;
aa42240a 310}
aa42240a 311
892ebdcc 312static inline void of_genpd_del_provider(struct device_node *np) {}
aa42240a 313
ec69572b
JH
314static inline int of_genpd_add_device(struct of_phandle_args *args,
315 struct device *dev)
316{
317 return -ENODEV;
318}
319
320static inline int of_genpd_add_subdomain(struct of_phandle_args *parent,
321 struct of_phandle_args *new_subdomain)
322{
323 return -ENODEV;
324}
325
30f60428
LI
326static inline int of_genpd_parse_idle_states(struct device_node *dn,
327 struct genpd_power_state **states, int *n)
328{
329 return -ENODEV;
330}
331
e38f89d3
VK
332static inline unsigned int
333pm_genpd_opp_to_performance_state(struct device *genpd_dev,
334 struct dev_pm_opp *opp)
335{
336 return 0;
337}
338
aa42240a
TF
339static inline int genpd_dev_pm_attach(struct device *dev)
340{
919b7308 341 return 0;
aa42240a 342}
17926551 343
3c095f32
UH
344static inline struct device *genpd_dev_pm_attach_by_id(struct device *dev,
345 unsigned int index)
346{
347 return NULL;
348}
349
5d6be70a 350static inline struct device *genpd_dev_pm_attach_by_name(struct device *dev,
7416f1f2 351 const char *name)
5d6be70a
UH
352{
353 return NULL;
354}
355
17926551
JH
356static inline
357struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
358{
359 return ERR_PTR(-ENOTSUPP);
360}
aa42240a
TF
361#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
362
f48c767c 363#ifdef CONFIG_PM
781b9d6b 364int dev_pm_domain_attach(struct device *dev, bool power_on);
82e12d9e
UH
365struct device *dev_pm_domain_attach_by_id(struct device *dev,
366 unsigned int index);
27dceb81 367struct device *dev_pm_domain_attach_by_name(struct device *dev,
eeb35df0 368 const char *name);
781b9d6b
UH
369void dev_pm_domain_detach(struct device *dev, bool power_off);
370void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
f48c767c
UH
371#else
372static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
373{
919b7308 374 return 0;
f48c767c 375}
82e12d9e
UH
376static inline struct device *dev_pm_domain_attach_by_id(struct device *dev,
377 unsigned int index)
378{
379 return NULL;
380}
27dceb81 381static inline struct device *dev_pm_domain_attach_by_name(struct device *dev,
eeb35df0 382 const char *name)
27dceb81
UH
383{
384 return NULL;
385}
f48c767c 386static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
989561de
TV
387static inline void dev_pm_domain_set(struct device *dev,
388 struct dev_pm_domain *pd) {}
f48c767c
UH
389#endif
390
f721889f 391#endif /* _LINUX_PM_DOMAIN_H */