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