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