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