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