Merge tag 'sched_ext-for-6.12' of git://git.kernel.org/pub/scm/linux/kernel/git/tj...
[linux-2.6-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>
e1b6705b 19#include <linux/cpumask_types.h>
1498c503 20#include <linux/time64.h>
f721889f 21
161e16a5
UH
22/*
23 * Flags to control the behaviour when attaching a device to its PM domains.
24 *
25 * PD_FLAG_NO_DEV_LINK: As the default behaviour creates a device-link
26 * for every PM domain that gets attached, this
27 * flag can be used to skip that.
28 *
29 * PD_FLAG_DEV_LINK_ON: Add the DL_FLAG_RPM_ACTIVE to power-on the
30 * supplier and its PM domain when creating the
31 * device-links.
32 *
33 */
34#define PD_FLAG_NO_DEV_LINK BIT(0)
35#define PD_FLAG_DEV_LINK_ON BIT(1)
36
37struct dev_pm_domain_attach_data {
38 const char * const *pd_names;
39 const u32 num_pd_names;
40 const u32 pd_flags;
41};
42
43struct dev_pm_domain_list {
44 struct device **pd_devs;
45 struct device_link **pd_links;
46 u32 num_pds;
47};
48
e5089c2c
UH
49/*
50 * Flags to control the behaviour of a genpd.
51 *
52 * These flags may be set in the struct generic_pm_domain's flags field by a
53 * genpd backend driver. The flags must be set before it calls pm_genpd_init(),
54 * which initializes a genpd.
55 *
56 * GENPD_FLAG_PM_CLK: Instructs genpd to use the PM clk framework,
57 * while powering on/off attached devices.
58 *
59 * GENPD_FLAG_IRQ_SAFE: This informs genpd that its backend callbacks,
60 * ->power_on|off(), doesn't sleep. Hence, these
61 * can be invoked from within atomic context, which
62 * enables genpd to power on/off the PM domain,
63 * even when pm_runtime_is_irq_safe() returns true,
64 * for any of its attached devices. Note that, a
65 * genpd having this flag set, requires its
66 * masterdomains to also have it set.
67 *
68 * GENPD_FLAG_ALWAYS_ON: Instructs genpd to always keep the PM domain
69 * powered on.
70 *
71 * GENPD_FLAG_ACTIVE_WAKEUP: Instructs genpd to keep the PM domain powered
72 * on, in case any of its attached devices is used
73 * in the wakeup path to serve system wakeups.
eb594b73
UH
74 *
75 * GENPD_FLAG_CPU_DOMAIN: Instructs genpd that it should expect to get
76 * devices attached, which may belong to CPUs or
77 * possibly have subdomains with CPUs attached.
78 * This flag enables the genpd backend driver to
79 * deploy idle power management support for CPUs
80 * and groups of CPUs. Note that, the backend
81 * driver must then comply with the so called,
82 * last-man-standing algorithm, for the CPUs in the
83 * PM domain.
ed61e18a
LC
84 *
85 * GENPD_FLAG_RPM_ALWAYS_ON: Instructs genpd to always keep the PM domain
86 * powered on except for system suspend.
c79aa080
LI
87 *
88 * GENPD_FLAG_MIN_RESIDENCY: Enable the genpd governor to consider its
89 * components' next wakeup when determining the
90 * optimal idle state.
3dd91515
UH
91 *
92 * GENPD_FLAG_OPP_TABLE_FW: The genpd provider supports performance states,
93 * but its corresponding OPP tables are not
94 * described in DT, but are given directly by FW.
e5089c2c
UH
95 */
96#define GENPD_FLAG_PM_CLK (1U << 0)
97#define GENPD_FLAG_IRQ_SAFE (1U << 1)
98#define GENPD_FLAG_ALWAYS_ON (1U << 2)
99#define GENPD_FLAG_ACTIVE_WAKEUP (1U << 3)
eb594b73 100#define GENPD_FLAG_CPU_DOMAIN (1U << 4)
ed61e18a 101#define GENPD_FLAG_RPM_ALWAYS_ON (1U << 5)
c79aa080 102#define GENPD_FLAG_MIN_RESIDENCY (1U << 6)
3dd91515 103#define GENPD_FLAG_OPP_TABLE_FW (1U << 7)
c11f6f5b 104
17b75eca 105enum gpd_status {
49f618e1
UH
106 GENPD_STATE_ON = 0, /* PM domain is on */
107 GENPD_STATE_OFF, /* PM domain is off */
17b75eca 108};
596ba34b 109
d4f81383
UH
110enum genpd_notication {
111 GENPD_NOTIFY_PRE_OFF = 0,
112 GENPD_NOTIFY_OFF,
113 GENPD_NOTIFY_PRE_ON,
114 GENPD_NOTIFY_ON,
115};
116
f721889f
RW
117struct dev_power_governor {
118 bool (*power_down_ok)(struct dev_pm_domain *domain);
9df3921e 119 bool (*suspend_ok)(struct device *dev);
f721889f
RW
120};
121
d5e4cbfe
RW
122struct gpd_dev_ops {
123 int (*start)(struct device *dev);
124 int (*stop)(struct device *dev);
d5e4cbfe
RW
125};
126
f38d1a6d
UH
127struct genpd_governor_data {
128 s64 max_off_time_ns;
129 bool max_off_time_changed;
130 ktime_t next_wakeup;
1498c503 131 ktime_t next_hrtimer;
f38d1a6d
UH
132 bool cached_power_down_ok;
133 bool cached_power_down_state_idx;
134};
135
fc5cbf0c
AH
136struct genpd_power_state {
137 s64 power_off_latency_ns;
138 s64 power_on_latency_ns;
405f7226 139 s64 residency_ns;
c6a113b5
LI
140 u64 usage;
141 u64 rejected;
0c9b694a 142 struct fwnode_handle *fwnode;
bd40cbb0 143 u64 idle_time;
49a27e27 144 void *data;
fc5cbf0c
AH
145};
146
35241d12 147struct genpd_lock_ops;
1067ae3e 148struct opp_table;
35241d12 149
f721889f 150struct generic_pm_domain {
401ea157 151 struct device dev;
f721889f 152 struct dev_pm_domain domain; /* PM domain operations */
5125bbf3 153 struct list_head gpd_list_node; /* Node in the global PM domains list */
8d87ae48 154 struct list_head parent_links; /* Links with PM domain as a parent */
afb0367a 155 struct list_head child_links; /* Links with PM domain as a child */
f721889f 156 struct list_head dev_list; /* List of devices */
f721889f 157 struct dev_power_governor *gov;
f38d1a6d 158 struct genpd_governor_data *gd; /* Data used by a genpd governor. */
f721889f 159 struct work_struct power_off_work;
de0aa06d
JH
160 struct fwnode_handle *provider; /* Identity of the domain provider */
161 bool has_provider;
d07e9c17 162 const char *name;
c4bb3160 163 atomic_t sd_count; /* Number of subdomains with power "on" */
17b75eca 164 enum gpd_status status; /* Current state of the domain */
596ba34b
RW
165 unsigned int device_count; /* Number of devices */
166 unsigned int suspended_count; /* System suspend device counter */
167 unsigned int prepared_count; /* Suspend counter of prepared devices */
42f6284a 168 unsigned int performance_state; /* Aggregated max performance state */
eb594b73 169 cpumask_var_t cpus; /* A cpumask of the attached CPUs */
a9236a0a 170 bool synced_poweroff; /* A consumer needs a synced poweroff */
f721889f
RW
171 int (*power_off)(struct generic_pm_domain *domain);
172 int (*power_on)(struct generic_pm_domain *domain);
d4f81383 173 struct raw_notifier_head power_notifiers; /* Power on/off notifiers */
1067ae3e 174 struct opp_table *opp_table; /* OPP table of the genpd */
42f6284a
VK
175 int (*set_performance_state)(struct generic_pm_domain *genpd,
176 unsigned int state);
d5e4cbfe 177 struct gpd_dev_ops dev_ops;
95f6454d
UH
178 int (*set_hwmode_dev)(struct generic_pm_domain *domain,
179 struct device *dev, bool enable);
180 bool (*get_hwmode_dev)(struct generic_pm_domain *domain,
181 struct device *dev);
c16561e8
UH
182 int (*attach_dev)(struct generic_pm_domain *domain,
183 struct device *dev);
184 void (*detach_dev)(struct generic_pm_domain *domain,
185 struct device *dev);
c11f6f5b 186 unsigned int flags; /* Bit field of configs for genpd */
59d65b73 187 struct genpd_power_state *states;
49a27e27
UH
188 void (*free_states)(struct genpd_power_state *states,
189 unsigned int state_count);
fc5cbf0c
AH
190 unsigned int state_count; /* number of states */
191 unsigned int state_idx; /* state that genpd will go to when off */
bd40cbb0
UH
192 u64 on_time;
193 u64 accounting_time;
35241d12 194 const struct genpd_lock_ops *lock_ops;
d716f479
LI
195 union {
196 struct mutex mlock;
197 struct {
198 spinlock_t slock;
199 unsigned long lock_flags;
200 };
d7bdb8e6
UH
201 struct {
202 raw_spinlock_t raw_slock;
203 unsigned long raw_lock_flags;
204 };
d716f479 205 };
f721889f
RW
206};
207
596ba34b
RW
208static inline struct generic_pm_domain *pd_to_genpd(struct dev_pm_domain *pd)
209{
210 return container_of(pd, struct generic_pm_domain, domain);
211}
212
5063ce15 213struct gpd_link {
8d87ae48
KC
214 struct generic_pm_domain *parent;
215 struct list_head parent_node;
216 struct generic_pm_domain *child;
217 struct list_head child_node;
18edf49c
VK
218
219 /* Sub-domain's per-master domain performance state */
220 unsigned int performance_state;
221 unsigned int prev_performance_state;
5063ce15
RW
222};
223
b02c999a 224struct gpd_timing_data {
2b1d88cd
UH
225 s64 suspend_latency_ns;
226 s64 resume_latency_ns;
a5bef810 227 s64 effective_constraint_ns;
9c74f2ac 228 ktime_t next_wakeup;
6ff7bb0d 229 bool constraint_changed;
9df3921e 230 bool cached_suspend_ok;
b02c999a
RW
231};
232
00e7c295
UH
233struct pm_domain_data {
234 struct list_head list_node;
235 struct device *dev;
236};
237
cd0ea672
RW
238struct generic_pm_domain_data {
239 struct pm_domain_data base;
66d29d80 240 struct gpd_timing_data *td;
6ff7bb0d 241 struct notifier_block nb;
d4f81383 242 struct notifier_block *power_nb;
f9ccd7c3 243 int cpu;
42f6284a 244 unsigned int performance_state;
c016baf7 245 unsigned int default_pstate;
5937c3ce 246 unsigned int rpm_pstate;
95f6454d 247 bool hw_mode;
a5ea7a0f 248 void *data;
cd0ea672
RW
249};
250
9b4f617b 251#ifdef CONFIG_PM_GENERIC_DOMAINS
cd0ea672
RW
252static inline struct generic_pm_domain_data *to_gpd_data(struct pm_domain_data *pdd)
253{
254 return container_of(pdd, struct generic_pm_domain_data, base);
255}
256
d5e4cbfe
RW
257static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
258{
259 return to_gpd_data(dev->power.subsys_data->domain_data);
260}
261
1a7a6707 262int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev);
924f4486 263int pm_genpd_remove_device(struct device *dev);
781b9d6b 264int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
0d1e16c6 265 struct generic_pm_domain *subdomain);
781b9d6b 266int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
0d1e16c6 267 struct generic_pm_domain *subdomain);
781b9d6b
UH
268int pm_genpd_init(struct generic_pm_domain *genpd,
269 struct dev_power_governor *gov, bool is_off);
270int pm_genpd_remove(struct generic_pm_domain *genpd);
2a56c462 271struct device *dev_to_genpd_dev(struct device *dev);
781b9d6b 272int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state);
d4f81383
UH
273int dev_pm_genpd_add_notifier(struct device *dev, struct notifier_block *nb);
274int dev_pm_genpd_remove_notifier(struct device *dev);
67e3242e 275void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next);
1498c503 276ktime_t dev_pm_genpd_get_next_hrtimer(struct device *dev);
a9236a0a 277void dev_pm_genpd_synced_poweroff(struct device *dev);
95f6454d
UH
278int dev_pm_genpd_set_hwmode(struct device *dev, bool enable);
279bool dev_pm_genpd_get_hwmode(struct device *dev);
b02c999a 280
ae3c511c 281extern struct dev_power_governor simple_qos_governor;
925b44a2 282extern struct dev_power_governor pm_domain_always_on_gov;
e9499968
UH
283#ifdef CONFIG_CPU_IDLE
284extern struct dev_power_governor pm_domain_cpu_gov;
285#endif
f721889f 286#else
b02c999a 287
b642631d
MD
288static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
289{
290 return ERR_PTR(-ENOSYS);
291}
1a7a6707
UH
292static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
293 struct device *dev)
b02c999a
RW
294{
295 return -ENOSYS;
296}
924f4486 297static inline int pm_genpd_remove_device(struct device *dev)
f721889f
RW
298{
299 return -ENOSYS;
300}
301static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
0d1e16c6 302 struct generic_pm_domain *subdomain)
f721889f
RW
303{
304 return -ENOSYS;
305}
306static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
0d1e16c6 307 struct generic_pm_domain *subdomain)
f721889f
RW
308{
309 return -ENOSYS;
310}
7eb231c3
UH
311static inline int pm_genpd_init(struct generic_pm_domain *genpd,
312 struct dev_power_governor *gov, bool is_off)
b02c999a 313{
7eb231c3 314 return -ENOSYS;
b02c999a 315}
3fe57710
JH
316static inline int pm_genpd_remove(struct generic_pm_domain *genpd)
317{
a94ef811 318 return -EOPNOTSUPP;
3fe57710 319}
16d0bf18 320
2a56c462
VK
321static inline struct device *dev_to_genpd_dev(struct device *dev)
322{
323 return ERR_PTR(-EOPNOTSUPP);
324}
325
42f6284a
VK
326static inline int dev_pm_genpd_set_performance_state(struct device *dev,
327 unsigned int state)
328{
a94ef811 329 return -EOPNOTSUPP;
42f6284a
VK
330}
331
d4f81383
UH
332static inline int dev_pm_genpd_add_notifier(struct device *dev,
333 struct notifier_block *nb)
334{
a94ef811 335 return -EOPNOTSUPP;
d4f81383
UH
336}
337
338static inline int dev_pm_genpd_remove_notifier(struct device *dev)
339{
a94ef811 340 return -EOPNOTSUPP;
d4f81383
UH
341}
342
67e3242e
LI
343static inline void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next)
344{ }
345
1498c503
MS
346static inline ktime_t dev_pm_genpd_get_next_hrtimer(struct device *dev)
347{
348 return KTIME_MAX;
349}
a9236a0a
UH
350static inline void dev_pm_genpd_synced_poweroff(struct device *dev)
351{ }
352
95f6454d
UH
353static inline int dev_pm_genpd_set_hwmode(struct device *dev, bool enable)
354{
355 return -EOPNOTSUPP;
356}
357
358static inline bool dev_pm_genpd_get_hwmode(struct device *dev)
359{
360 return false;
361}
362
16d0bf18
GU
363#define simple_qos_governor (*(struct dev_power_governor *)(NULL))
364#define pm_domain_always_on_gov (*(struct dev_power_governor *)(NULL))
17f2ae7f
RW
365#endif
366
77f827de 367#ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
fc519890
UH
368void dev_pm_genpd_suspend(struct device *dev);
369void dev_pm_genpd_resume(struct device *dev);
77f827de 370#else
fc519890
UH
371static inline void dev_pm_genpd_suspend(struct device *dev) {}
372static inline void dev_pm_genpd_resume(struct device *dev) {}
77f827de
RW
373#endif
374
aa42240a
TF
375/* OF PM domain providers */
376struct of_device_id;
377
4d082460 378typedef struct generic_pm_domain *(*genpd_xlate_t)(const struct of_phandle_args *args,
40845524
TR
379 void *data);
380
aa42240a
TF
381struct genpd_onecell_data {
382 struct generic_pm_domain **domains;
383 unsigned int num_domains;
40845524 384 genpd_xlate_t xlate;
aa42240a
TF
385};
386
aa42240a 387#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
892ebdcc
JH
388int of_genpd_add_provider_simple(struct device_node *np,
389 struct generic_pm_domain *genpd);
390int of_genpd_add_provider_onecell(struct device_node *np,
391 struct genpd_onecell_data *data);
aa42240a 392void of_genpd_del_provider(struct device_node *np);
4af6bc16
KK
393int of_genpd_add_device(const struct of_phandle_args *args, struct device *dev);
394int of_genpd_add_subdomain(const struct of_phandle_args *parent_spec,
395 const struct of_phandle_args *subdomain_spec);
396int of_genpd_remove_subdomain(const struct of_phandle_args *parent_spec,
397 const struct of_phandle_args *subdomain_spec);
781b9d6b
UH
398struct generic_pm_domain *of_genpd_remove_last(struct device_node *np);
399int of_genpd_parse_idle_states(struct device_node *dn,
400 struct genpd_power_state **states, int *n);
aa42240a
TF
401
402int genpd_dev_pm_attach(struct device *dev);
3c095f32
UH
403struct device *genpd_dev_pm_attach_by_id(struct device *dev,
404 unsigned int index);
5d6be70a 405struct device *genpd_dev_pm_attach_by_name(struct device *dev,
7416f1f2 406 const char *name);
aa42240a 407#else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
892ebdcc
JH
408static inline int of_genpd_add_provider_simple(struct device_node *np,
409 struct generic_pm_domain *genpd)
aa42240a 410{
a94ef811 411 return -EOPNOTSUPP;
892ebdcc
JH
412}
413
414static inline int of_genpd_add_provider_onecell(struct device_node *np,
415 struct genpd_onecell_data *data)
416{
a94ef811 417 return -EOPNOTSUPP;
aa42240a 418}
aa42240a 419
892ebdcc 420static inline void of_genpd_del_provider(struct device_node *np) {}
aa42240a 421
4af6bc16 422static inline int of_genpd_add_device(const struct of_phandle_args *args,
ec69572b
JH
423 struct device *dev)
424{
425 return -ENODEV;
426}
427
4af6bc16
KK
428static inline int of_genpd_add_subdomain(const struct of_phandle_args *parent_spec,
429 const struct of_phandle_args *subdomain_spec)
ec69572b
JH
430{
431 return -ENODEV;
432}
433
4af6bc16
KK
434static inline int of_genpd_remove_subdomain(const struct of_phandle_args *parent_spec,
435 const struct of_phandle_args *subdomain_spec)
dedd1492
UH
436{
437 return -ENODEV;
438}
439
30f60428
LI
440static inline int of_genpd_parse_idle_states(struct device_node *dn,
441 struct genpd_power_state **states, int *n)
442{
443 return -ENODEV;
444}
445
aa42240a
TF
446static inline int genpd_dev_pm_attach(struct device *dev)
447{
919b7308 448 return 0;
aa42240a 449}
17926551 450
3c095f32
UH
451static inline struct device *genpd_dev_pm_attach_by_id(struct device *dev,
452 unsigned int index)
453{
454 return NULL;
455}
456
5d6be70a 457static inline struct device *genpd_dev_pm_attach_by_name(struct device *dev,
7416f1f2 458 const char *name)
5d6be70a
UH
459{
460 return NULL;
461}
462
17926551
JH
463static inline
464struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
465{
a94ef811 466 return ERR_PTR(-EOPNOTSUPP);
17926551 467}
aa42240a
TF
468#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
469
f48c767c 470#ifdef CONFIG_PM
781b9d6b 471int dev_pm_domain_attach(struct device *dev, bool power_on);
82e12d9e
UH
472struct device *dev_pm_domain_attach_by_id(struct device *dev,
473 unsigned int index);
27dceb81 474struct device *dev_pm_domain_attach_by_name(struct device *dev,
eeb35df0 475 const char *name);
161e16a5
UH
476int dev_pm_domain_attach_list(struct device *dev,
477 const struct dev_pm_domain_attach_data *data,
478 struct dev_pm_domain_list **list);
b6cee654
DA
479int devm_pm_domain_attach_list(struct device *dev,
480 const struct dev_pm_domain_attach_data *data,
481 struct dev_pm_domain_list **list);
781b9d6b 482void dev_pm_domain_detach(struct device *dev, bool power_off);
161e16a5 483void dev_pm_domain_detach_list(struct dev_pm_domain_list *list);
ca765a8c 484int dev_pm_domain_start(struct device *dev);
781b9d6b 485void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
3fbc5c3b 486int dev_pm_domain_set_performance_state(struct device *dev, unsigned int state);
f48c767c
UH
487#else
488static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
489{
919b7308 490 return 0;
f48c767c 491}
82e12d9e
UH
492static inline struct device *dev_pm_domain_attach_by_id(struct device *dev,
493 unsigned int index)
494{
495 return NULL;
496}
27dceb81 497static inline struct device *dev_pm_domain_attach_by_name(struct device *dev,
eeb35df0 498 const char *name)
27dceb81
UH
499{
500 return NULL;
501}
161e16a5
UH
502static inline int dev_pm_domain_attach_list(struct device *dev,
503 const struct dev_pm_domain_attach_data *data,
504 struct dev_pm_domain_list **list)
505{
506 return 0;
507}
b6cee654
DA
508
509static inline int devm_pm_domain_attach_list(struct device *dev,
510 const struct dev_pm_domain_attach_data *data,
511 struct dev_pm_domain_list **list)
512{
513 return 0;
514}
515
f48c767c 516static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
161e16a5 517static inline void dev_pm_domain_detach_list(struct dev_pm_domain_list *list) {}
ca765a8c
UH
518static inline int dev_pm_domain_start(struct device *dev)
519{
520 return 0;
521}
989561de
TV
522static inline void dev_pm_domain_set(struct device *dev,
523 struct dev_pm_domain *pd) {}
3fbc5c3b
UH
524static inline int dev_pm_domain_set_performance_state(struct device *dev,
525 unsigned int state)
526{
527 return 0;
528}
f48c767c
UH
529#endif
530
f721889f 531#endif /* _LINUX_PM_DOMAIN_H */