PM: domains: Skip another warning in irq_safe_dev_in_sleep_domain()
[linux-block.git] / include / linux / pm_domain.h
CommitLineData
55716d26 1/* SPDX-License-Identifier: GPL-2.0-only */
f721889f
RW
2/*
3 * pm_domain.h - Definitions and headers related to device power domains.
4 *
5 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
f721889f
RW
6 */
7
8#ifndef _LINUX_PM_DOMAIN_H
9#define _LINUX_PM_DOMAIN_H
10
11#include <linux/device.h>
67e3242e 12#include <linux/ktime.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.
c79aa080
LI
59 *
60 * GENPD_FLAG_MIN_RESIDENCY: Enable the genpd governor to consider its
61 * components' next wakeup when determining the
62 * optimal idle state.
e5089c2c
UH
63 */
64#define GENPD_FLAG_PM_CLK (1U << 0)
65#define GENPD_FLAG_IRQ_SAFE (1U << 1)
66#define GENPD_FLAG_ALWAYS_ON (1U << 2)
67#define GENPD_FLAG_ACTIVE_WAKEUP (1U << 3)
eb594b73 68#define GENPD_FLAG_CPU_DOMAIN (1U << 4)
ed61e18a 69#define GENPD_FLAG_RPM_ALWAYS_ON (1U << 5)
c79aa080 70#define GENPD_FLAG_MIN_RESIDENCY (1U << 6)
c11f6f5b 71
17b75eca 72enum gpd_status {
49f618e1
UH
73 GENPD_STATE_ON = 0, /* PM domain is on */
74 GENPD_STATE_OFF, /* PM domain is off */
17b75eca 75};
596ba34b 76
d4f81383
UH
77enum genpd_notication {
78 GENPD_NOTIFY_PRE_OFF = 0,
79 GENPD_NOTIFY_OFF,
80 GENPD_NOTIFY_PRE_ON,
81 GENPD_NOTIFY_ON,
82};
83
f721889f
RW
84struct dev_power_governor {
85 bool (*power_down_ok)(struct dev_pm_domain *domain);
9df3921e 86 bool (*suspend_ok)(struct device *dev);
f721889f
RW
87};
88
d5e4cbfe
RW
89struct gpd_dev_ops {
90 int (*start)(struct device *dev);
91 int (*stop)(struct device *dev);
d5e4cbfe
RW
92};
93
fc5cbf0c
AH
94struct genpd_power_state {
95 s64 power_off_latency_ns;
96 s64 power_on_latency_ns;
405f7226 97 s64 residency_ns;
c6a113b5
LI
98 u64 usage;
99 u64 rejected;
0c9b694a 100 struct fwnode_handle *fwnode;
bd40cbb0 101 u64 idle_time;
49a27e27 102 void *data;
fc5cbf0c
AH
103};
104
35241d12 105struct genpd_lock_ops;
6e41766a 106struct dev_pm_opp;
1067ae3e 107struct opp_table;
35241d12 108
f721889f 109struct generic_pm_domain {
401ea157 110 struct device dev;
f721889f 111 struct dev_pm_domain domain; /* PM domain operations */
5125bbf3 112 struct list_head gpd_list_node; /* Node in the global PM domains list */
8d87ae48 113 struct list_head parent_links; /* Links with PM domain as a parent */
afb0367a 114 struct list_head child_links; /* Links with PM domain as a child */
f721889f 115 struct list_head dev_list; /* List of devices */
f721889f
RW
116 struct dev_power_governor *gov;
117 struct work_struct power_off_work;
de0aa06d
JH
118 struct fwnode_handle *provider; /* Identity of the domain provider */
119 bool has_provider;
d07e9c17 120 const char *name;
c4bb3160 121 atomic_t sd_count; /* Number of subdomains with power "on" */
17b75eca 122 enum gpd_status status; /* Current state of the domain */
596ba34b
RW
123 unsigned int device_count; /* Number of devices */
124 unsigned int suspended_count; /* System suspend device counter */
125 unsigned int prepared_count; /* Suspend counter of prepared devices */
42f6284a 126 unsigned int performance_state; /* Aggregated max performance state */
eb594b73 127 cpumask_var_t cpus; /* A cpumask of the attached CPUs */
f721889f
RW
128 int (*power_off)(struct generic_pm_domain *domain);
129 int (*power_on)(struct generic_pm_domain *domain);
d4f81383 130 struct raw_notifier_head power_notifiers; /* Power on/off notifiers */
1067ae3e 131 struct opp_table *opp_table; /* OPP table of the genpd */
6e41766a
VK
132 unsigned int (*opp_to_performance_state)(struct generic_pm_domain *genpd,
133 struct dev_pm_opp *opp);
42f6284a
VK
134 int (*set_performance_state)(struct generic_pm_domain *genpd,
135 unsigned int state);
d5e4cbfe 136 struct gpd_dev_ops dev_ops;
221e9b58 137 s64 max_off_time_ns; /* Maximum allowed "suspended" time. */
c79aa080 138 ktime_t next_wakeup; /* Maintained by the domain governor */
6ff7bb0d
RW
139 bool max_off_time_changed;
140 bool cached_power_down_ok;
e9499968 141 bool cached_power_down_state_idx;
c16561e8
UH
142 int (*attach_dev)(struct generic_pm_domain *domain,
143 struct device *dev);
144 void (*detach_dev)(struct generic_pm_domain *domain,
145 struct device *dev);
c11f6f5b 146 unsigned int flags; /* Bit field of configs for genpd */
59d65b73 147 struct genpd_power_state *states;
49a27e27
UH
148 void (*free_states)(struct genpd_power_state *states,
149 unsigned int state_count);
fc5cbf0c
AH
150 unsigned int state_count; /* number of states */
151 unsigned int state_idx; /* state that genpd will go to when off */
bd40cbb0
UH
152 u64 on_time;
153 u64 accounting_time;
35241d12 154 const struct genpd_lock_ops *lock_ops;
d716f479
LI
155 union {
156 struct mutex mlock;
157 struct {
158 spinlock_t slock;
159 unsigned long lock_flags;
160 };
161 };
fc5cbf0c 162
f721889f
RW
163};
164
596ba34b
RW
165static inline struct generic_pm_domain *pd_to_genpd(struct dev_pm_domain *pd)
166{
167 return container_of(pd, struct generic_pm_domain, domain);
168}
169
5063ce15 170struct gpd_link {
8d87ae48
KC
171 struct generic_pm_domain *parent;
172 struct list_head parent_node;
173 struct generic_pm_domain *child;
174 struct list_head child_node;
18edf49c
VK
175
176 /* Sub-domain's per-master domain performance state */
177 unsigned int performance_state;
178 unsigned int prev_performance_state;
5063ce15
RW
179};
180
b02c999a 181struct gpd_timing_data {
2b1d88cd
UH
182 s64 suspend_latency_ns;
183 s64 resume_latency_ns;
a5bef810 184 s64 effective_constraint_ns;
6ff7bb0d 185 bool constraint_changed;
9df3921e 186 bool cached_suspend_ok;
b02c999a
RW
187};
188
00e7c295
UH
189struct pm_domain_data {
190 struct list_head list_node;
191 struct device *dev;
192};
193
cd0ea672
RW
194struct generic_pm_domain_data {
195 struct pm_domain_data base;
b02c999a 196 struct gpd_timing_data td;
6ff7bb0d 197 struct notifier_block nb;
d4f81383 198 struct notifier_block *power_nb;
f9ccd7c3 199 int cpu;
42f6284a 200 unsigned int performance_state;
c016baf7 201 unsigned int default_pstate;
5937c3ce 202 unsigned int rpm_pstate;
67e3242e 203 ktime_t next_wakeup;
a5ea7a0f 204 void *data;
cd0ea672
RW
205};
206
9b4f617b 207#ifdef CONFIG_PM_GENERIC_DOMAINS
cd0ea672
RW
208static inline struct generic_pm_domain_data *to_gpd_data(struct pm_domain_data *pdd)
209{
210 return container_of(pdd, struct generic_pm_domain_data, base);
211}
212
d5e4cbfe
RW
213static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
214{
215 return to_gpd_data(dev->power.subsys_data->domain_data);
216}
217
1a7a6707 218int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev);
924f4486 219int pm_genpd_remove_device(struct device *dev);
781b9d6b 220int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
0d1e16c6 221 struct generic_pm_domain *subdomain);
781b9d6b 222int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
0d1e16c6 223 struct generic_pm_domain *subdomain);
781b9d6b
UH
224int pm_genpd_init(struct generic_pm_domain *genpd,
225 struct dev_power_governor *gov, bool is_off);
226int pm_genpd_remove(struct generic_pm_domain *genpd);
227int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state);
d4f81383
UH
228int dev_pm_genpd_add_notifier(struct device *dev, struct notifier_block *nb);
229int dev_pm_genpd_remove_notifier(struct device *dev);
67e3242e 230void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next);
b02c999a 231
ae3c511c 232extern struct dev_power_governor simple_qos_governor;
925b44a2 233extern struct dev_power_governor pm_domain_always_on_gov;
e9499968
UH
234#ifdef CONFIG_CPU_IDLE
235extern struct dev_power_governor pm_domain_cpu_gov;
236#endif
f721889f 237#else
b02c999a 238
b642631d
MD
239static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
240{
241 return ERR_PTR(-ENOSYS);
242}
1a7a6707
UH
243static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
244 struct device *dev)
b02c999a
RW
245{
246 return -ENOSYS;
247}
924f4486 248static inline int pm_genpd_remove_device(struct device *dev)
f721889f
RW
249{
250 return -ENOSYS;
251}
252static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
0d1e16c6 253 struct generic_pm_domain *subdomain)
f721889f
RW
254{
255 return -ENOSYS;
256}
257static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
0d1e16c6 258 struct generic_pm_domain *subdomain)
f721889f
RW
259{
260 return -ENOSYS;
261}
7eb231c3
UH
262static inline int pm_genpd_init(struct generic_pm_domain *genpd,
263 struct dev_power_governor *gov, bool is_off)
b02c999a 264{
7eb231c3 265 return -ENOSYS;
b02c999a 266}
3fe57710
JH
267static inline int pm_genpd_remove(struct generic_pm_domain *genpd)
268{
a94ef811 269 return -EOPNOTSUPP;
3fe57710 270}
16d0bf18 271
42f6284a
VK
272static inline int dev_pm_genpd_set_performance_state(struct device *dev,
273 unsigned int state)
274{
a94ef811 275 return -EOPNOTSUPP;
42f6284a
VK
276}
277
d4f81383
UH
278static inline int dev_pm_genpd_add_notifier(struct device *dev,
279 struct notifier_block *nb)
280{
a94ef811 281 return -EOPNOTSUPP;
d4f81383
UH
282}
283
284static inline int dev_pm_genpd_remove_notifier(struct device *dev)
285{
a94ef811 286 return -EOPNOTSUPP;
d4f81383
UH
287}
288
67e3242e
LI
289static inline void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next)
290{ }
291
16d0bf18
GU
292#define simple_qos_governor (*(struct dev_power_governor *)(NULL))
293#define pm_domain_always_on_gov (*(struct dev_power_governor *)(NULL))
17f2ae7f
RW
294#endif
295
77f827de 296#ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
fc519890
UH
297void dev_pm_genpd_suspend(struct device *dev);
298void dev_pm_genpd_resume(struct device *dev);
77f827de 299#else
fc519890
UH
300static inline void dev_pm_genpd_suspend(struct device *dev) {}
301static inline void dev_pm_genpd_resume(struct device *dev) {}
77f827de
RW
302#endif
303
aa42240a
TF
304/* OF PM domain providers */
305struct of_device_id;
306
40845524
TR
307typedef struct generic_pm_domain *(*genpd_xlate_t)(struct of_phandle_args *args,
308 void *data);
309
aa42240a
TF
310struct genpd_onecell_data {
311 struct generic_pm_domain **domains;
312 unsigned int num_domains;
40845524 313 genpd_xlate_t xlate;
aa42240a
TF
314};
315
aa42240a 316#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
892ebdcc
JH
317int of_genpd_add_provider_simple(struct device_node *np,
318 struct generic_pm_domain *genpd);
319int of_genpd_add_provider_onecell(struct device_node *np,
320 struct genpd_onecell_data *data);
aa42240a 321void of_genpd_del_provider(struct device_node *np);
781b9d6b 322int of_genpd_add_device(struct of_phandle_args *args, struct device *dev);
0d1e16c6
UH
323int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
324 struct of_phandle_args *subdomain_spec);
dedd1492
UH
325int of_genpd_remove_subdomain(struct of_phandle_args *parent_spec,
326 struct of_phandle_args *subdomain_spec);
781b9d6b
UH
327struct generic_pm_domain *of_genpd_remove_last(struct device_node *np);
328int of_genpd_parse_idle_states(struct device_node *dn,
329 struct genpd_power_state **states, int *n);
e38f89d3
VK
330unsigned int pm_genpd_opp_to_performance_state(struct device *genpd_dev,
331 struct dev_pm_opp *opp);
aa42240a
TF
332
333int genpd_dev_pm_attach(struct device *dev);
3c095f32
UH
334struct device *genpd_dev_pm_attach_by_id(struct device *dev,
335 unsigned int index);
5d6be70a 336struct device *genpd_dev_pm_attach_by_name(struct device *dev,
7416f1f2 337 const char *name);
aa42240a 338#else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
892ebdcc
JH
339static inline int of_genpd_add_provider_simple(struct device_node *np,
340 struct generic_pm_domain *genpd)
aa42240a 341{
a94ef811 342 return -EOPNOTSUPP;
892ebdcc
JH
343}
344
345static inline int of_genpd_add_provider_onecell(struct device_node *np,
346 struct genpd_onecell_data *data)
347{
a94ef811 348 return -EOPNOTSUPP;
aa42240a 349}
aa42240a 350
892ebdcc 351static inline void of_genpd_del_provider(struct device_node *np) {}
aa42240a 352
ec69572b
JH
353static inline int of_genpd_add_device(struct of_phandle_args *args,
354 struct device *dev)
355{
356 return -ENODEV;
357}
358
0d1e16c6
UH
359static inline int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
360 struct of_phandle_args *subdomain_spec)
ec69572b
JH
361{
362 return -ENODEV;
363}
364
dedd1492
UH
365static inline int of_genpd_remove_subdomain(struct of_phandle_args *parent_spec,
366 struct of_phandle_args *subdomain_spec)
367{
368 return -ENODEV;
369}
370
30f60428
LI
371static inline int of_genpd_parse_idle_states(struct device_node *dn,
372 struct genpd_power_state **states, int *n)
373{
374 return -ENODEV;
375}
376
e38f89d3
VK
377static inline unsigned int
378pm_genpd_opp_to_performance_state(struct device *genpd_dev,
379 struct dev_pm_opp *opp)
380{
381 return 0;
382}
383
aa42240a
TF
384static inline int genpd_dev_pm_attach(struct device *dev)
385{
919b7308 386 return 0;
aa42240a 387}
17926551 388
3c095f32
UH
389static inline struct device *genpd_dev_pm_attach_by_id(struct device *dev,
390 unsigned int index)
391{
392 return NULL;
393}
394
5d6be70a 395static inline struct device *genpd_dev_pm_attach_by_name(struct device *dev,
7416f1f2 396 const char *name)
5d6be70a
UH
397{
398 return NULL;
399}
400
17926551
JH
401static inline
402struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
403{
a94ef811 404 return ERR_PTR(-EOPNOTSUPP);
17926551 405}
aa42240a
TF
406#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
407
f48c767c 408#ifdef CONFIG_PM
781b9d6b 409int dev_pm_domain_attach(struct device *dev, bool power_on);
82e12d9e
UH
410struct device *dev_pm_domain_attach_by_id(struct device *dev,
411 unsigned int index);
27dceb81 412struct device *dev_pm_domain_attach_by_name(struct device *dev,
eeb35df0 413 const char *name);
781b9d6b 414void dev_pm_domain_detach(struct device *dev, bool power_off);
ca765a8c 415int dev_pm_domain_start(struct device *dev);
781b9d6b 416void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
f48c767c
UH
417#else
418static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
419{
919b7308 420 return 0;
f48c767c 421}
82e12d9e
UH
422static inline struct device *dev_pm_domain_attach_by_id(struct device *dev,
423 unsigned int index)
424{
425 return NULL;
426}
27dceb81 427static inline struct device *dev_pm_domain_attach_by_name(struct device *dev,
eeb35df0 428 const char *name)
27dceb81
UH
429{
430 return NULL;
431}
f48c767c 432static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
ca765a8c
UH
433static inline int dev_pm_domain_start(struct device *dev)
434{
435 return 0;
436}
989561de
TV
437static inline void dev_pm_domain_set(struct device *dev,
438 struct dev_pm_domain *pd) {}
f48c767c
UH
439#endif
440
f721889f 441#endif /* _LINUX_PM_DOMAIN_H */