1 /* SPDX-License-Identifier: GPL-2.0-only */
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.
6 */
7
8 #ifndef _LINUX_PM_DOMAIN_H
9 #define _LINUX_PM_DOMAIN_H
10
11 #include <linux/device.h>
12 #include <linux/ktime.h>
13 #include <linux/mutex.h>
14 #include <linux/pm.h>
15 #include <linux/err.h>
16 #include <linux/of.h>
17 #include <linux/notifier.h>
18 #include <linux/spinlock.h>
19 #include <linux/cpumask_types.h>
20 #include <linux/time64.h>
21
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
37 struct dev_pm_domain_attach_data {
38 const char * const *pd_names;
39 const u32 num_pd_names;
40 const u32 pd_flags;
41 };
42
43 struct dev_pm_domain_list {
44 struct device **pd_devs;
45 struct device_link **pd_links;
46 u32 num_pds;
47 };
48
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.
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.
84 *
85 * GENPD_FLAG_RPM_ALWAYS_ON: Instructs genpd to always keep the PM domain
86 * powered on except for system suspend.
87 *
88 * GENPD_FLAG_MIN_RESIDENCY: Enable the genpd governor to consider its
89 * components' next wakeup when determining the
90 * optimal idle state.
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.
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)
100 #define GENPD_FLAG_CPU_DOMAIN (1U << 4)
101 #define GENPD_FLAG_RPM_ALWAYS_ON (1U << 5)
102 #define GENPD_FLAG_MIN_RESIDENCY (1U << 6)
103 #define GENPD_FLAG_OPP_TABLE_FW (1U << 7)
104
105 enum gpd_status {
106 GENPD_STATE_ON = 0, /* PM domain is on */
107 GENPD_STATE_OFF, /* PM domain is off */
108 };
109
110 enum genpd_notication {
111 GENPD_NOTIFY_PRE_OFF = 0,
112 GENPD_NOTIFY_OFF,
113 GENPD_NOTIFY_PRE_ON,
114 GENPD_NOTIFY_ON,
115 };
116
117 struct dev_power_governor {
118 bool (*power_down_ok)(struct dev_pm_domain *domain);
119 bool (*suspend_ok)(struct device *dev);
120 };
121
122 struct gpd_dev_ops {
123 int (*start)(struct device *dev);
124 int (*stop)(struct device *dev);
125 };
126
127 struct genpd_governor_data {
128 s64 max_off_time_ns;
129 bool max_off_time_changed;
130 ktime_t next_wakeup;
131 ktime_t next_hrtimer;
132 bool cached_power_down_ok;
133 bool cached_power_down_state_idx;
134 };
135
136 struct genpd_power_state {
137 s64 power_off_latency_ns;
138 s64 power_on_latency_ns;
139 s64 residency_ns;
140 u64 usage;
141 u64 rejected;
142 struct fwnode_handle *fwnode;
143 u64 idle_time;
144 void *data;
145 };
146
147 struct genpd_lock_ops;
148 struct opp_table;
149
150 struct generic_pm_domain {
151 struct device dev;
152 struct dev_pm_domain domain; /* PM domain operations */
153 struct list_head gpd_list_node; /* Node in the global PM domains list */
154 struct list_head parent_links; /* Links with PM domain as a parent */
155 struct list_head child_links; /* Links with PM domain as a child */
156 struct list_head dev_list; /* List of devices */
157 struct dev_power_governor *gov;
158 struct genpd_governor_data *gd; /* Data used by a genpd governor. */
159 struct work_struct power_off_work;
160 struct fwnode_handle *provider; /* Identity of the domain provider */
161 bool has_provider;
162 const char *name;
163 atomic_t sd_count; /* Number of subdomains with power "on" */
164 enum gpd_status status; /* Current state of the domain */
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 */
168 unsigned int performance_state; /* Aggregated max performance state */
169 cpumask_var_t cpus; /* A cpumask of the attached CPUs */
170 bool synced_poweroff; /* A consumer needs a synced poweroff */
171 int (*power_off)(struct generic_pm_domain *domain);
172 int (*power_on)(struct generic_pm_domain *domain);
173 struct raw_notifier_head power_notifiers; /* Power on/off notifiers */
174 struct opp_table *opp_table; /* OPP table of the genpd */
175 int (*set_performance_state)(struct generic_pm_domain *genpd,
176 unsigned int state);
177 struct gpd_dev_ops dev_ops;
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);
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);
186 unsigned int flags; /* Bit field of configs for genpd */
187 struct genpd_power_state *states;
188 void (*free_states)(struct genpd_power_state *states,
189 unsigned int state_count);
190 unsigned int state_count; /* number of states */
191 unsigned int state_idx; /* state that genpd will go to when off */
192 u64 on_time;
193 u64 accounting_time;
194 const struct genpd_lock_ops *lock_ops;
195 union {
196 struct mutex mlock;
197 struct {
198 spinlock_t slock;
199 unsigned long lock_flags;
200 };
201 struct {
202 raw_spinlock_t raw_slock;
203 unsigned long raw_lock_flags;
204 };
205 };
206 };
207
pd_to_genpd(struct dev_pm_domain * pd)208 static 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
213 struct gpd_link {
214 struct generic_pm_domain *parent;
215 struct list_head parent_node;
216 struct generic_pm_domain *child;
217 struct list_head child_node;
218
219 /* Sub-domain's per-master domain performance state */
220 unsigned int performance_state;
221 unsigned int prev_performance_state;
222 };
223
224 struct gpd_timing_data {
225 s64 suspend_latency_ns;
226 s64 resume_latency_ns;
227 s64 effective_constraint_ns;
228 ktime_t next_wakeup;
229 bool constraint_changed;
230 bool cached_suspend_ok;
231 };
232
233 struct pm_domain_data {
234 struct list_head list_node;
235 struct device *dev;
236 };
237
238 struct generic_pm_domain_data {
239 struct pm_domain_data base;
240 struct gpd_timing_data *td;
241 struct notifier_block nb;
242 struct notifier_block *power_nb;
243 int cpu;
244 unsigned int performance_state;
245 unsigned int default_pstate;
246 unsigned int rpm_pstate;
247 bool hw_mode;
248 void *data;
249 };
250
251 #ifdef CONFIG_PM_GENERIC_DOMAINS
to_gpd_data(struct pm_domain_data * pdd)252 static 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
dev_gpd_data(struct device * dev)257 static 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
262 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev);
263 int pm_genpd_remove_device(struct device *dev);
264 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
265 struct generic_pm_domain *subdomain);
266 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
267 struct generic_pm_domain *subdomain);
268 int pm_genpd_init(struct generic_pm_domain *genpd,
269 struct dev_power_governor *gov, bool is_off);
270 int pm_genpd_remove(struct generic_pm_domain *genpd);
271 struct device *dev_to_genpd_dev(struct device *dev);
272 int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state);
273 int dev_pm_genpd_add_notifier(struct device *dev, struct notifier_block *nb);
274 int dev_pm_genpd_remove_notifier(struct device *dev);
275 void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next);
276 ktime_t dev_pm_genpd_get_next_hrtimer(struct device *dev);
277 void dev_pm_genpd_synced_poweroff(struct device *dev);
278 int dev_pm_genpd_set_hwmode(struct device *dev, bool enable);
279 bool dev_pm_genpd_get_hwmode(struct device *dev);
280
281 extern struct dev_power_governor simple_qos_governor;
282 extern struct dev_power_governor pm_domain_always_on_gov;
283 #ifdef CONFIG_CPU_IDLE
284 extern struct dev_power_governor pm_domain_cpu_gov;
285 #endif
286 #else
287
dev_gpd_data(struct device * dev)288 static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
289 {
290 return ERR_PTR(-ENOSYS);
291 }
pm_genpd_add_device(struct generic_pm_domain * genpd,struct device * dev)292 static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
293 struct device *dev)
294 {
295 return -ENOSYS;
296 }
pm_genpd_remove_device(struct device * dev)297 static inline int pm_genpd_remove_device(struct device *dev)
298 {
299 return -ENOSYS;
300 }
pm_genpd_add_subdomain(struct generic_pm_domain * genpd,struct generic_pm_domain * subdomain)301 static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
302 struct generic_pm_domain *subdomain)
303 {
304 return -ENOSYS;
305 }
pm_genpd_remove_subdomain(struct generic_pm_domain * genpd,struct generic_pm_domain * subdomain)306 static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
307 struct generic_pm_domain *subdomain)
308 {
309 return -ENOSYS;
310 }
pm_genpd_init(struct generic_pm_domain * genpd,struct dev_power_governor * gov,bool is_off)311 static inline int pm_genpd_init(struct generic_pm_domain *genpd,
312 struct dev_power_governor *gov, bool is_off)
313 {
314 return -ENOSYS;
315 }
pm_genpd_remove(struct generic_pm_domain * genpd)316 static inline int pm_genpd_remove(struct generic_pm_domain *genpd)
317 {
318 return -EOPNOTSUPP;
319 }
320
dev_to_genpd_dev(struct device * dev)321 static inline struct device *dev_to_genpd_dev(struct device *dev)
322 {
323 return ERR_PTR(-EOPNOTSUPP);
324 }
325
dev_pm_genpd_set_performance_state(struct device * dev,unsigned int state)326 static inline int dev_pm_genpd_set_performance_state(struct device *dev,
327 unsigned int state)
328 {
329 return -EOPNOTSUPP;
330 }
331
dev_pm_genpd_add_notifier(struct device * dev,struct notifier_block * nb)332 static inline int dev_pm_genpd_add_notifier(struct device *dev,
333 struct notifier_block *nb)
334 {
335 return -EOPNOTSUPP;
336 }
337
dev_pm_genpd_remove_notifier(struct device * dev)338 static inline int dev_pm_genpd_remove_notifier(struct device *dev)
339 {
340 return -EOPNOTSUPP;
341 }
342
dev_pm_genpd_set_next_wakeup(struct device * dev,ktime_t next)343 static inline void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next)
344 { }
345
dev_pm_genpd_get_next_hrtimer(struct device * dev)346 static inline ktime_t dev_pm_genpd_get_next_hrtimer(struct device *dev)
347 {
348 return KTIME_MAX;
349 }
dev_pm_genpd_synced_poweroff(struct device * dev)350 static inline void dev_pm_genpd_synced_poweroff(struct device *dev)
351 { }
352
dev_pm_genpd_set_hwmode(struct device * dev,bool enable)353 static inline int dev_pm_genpd_set_hwmode(struct device *dev, bool enable)
354 {
355 return -EOPNOTSUPP;
356 }
357
dev_pm_genpd_get_hwmode(struct device * dev)358 static inline bool dev_pm_genpd_get_hwmode(struct device *dev)
359 {
360 return false;
361 }
362
363 #define simple_qos_governor (*(struct dev_power_governor *)(NULL))
364 #define pm_domain_always_on_gov (*(struct dev_power_governor *)(NULL))
365 #endif
366
367 #ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
368 void dev_pm_genpd_suspend(struct device *dev);
369 void dev_pm_genpd_resume(struct device *dev);
370 #else
dev_pm_genpd_suspend(struct device * dev)371 static inline void dev_pm_genpd_suspend(struct device *dev) {}
dev_pm_genpd_resume(struct device * dev)372 static inline void dev_pm_genpd_resume(struct device *dev) {}
373 #endif
374
375 /* OF PM domain providers */
376 struct of_device_id;
377
378 typedef struct generic_pm_domain *(*genpd_xlate_t)(const struct of_phandle_args *args,
379 void *data);
380
381 struct genpd_onecell_data {
382 struct generic_pm_domain **domains;
383 unsigned int num_domains;
384 genpd_xlate_t xlate;
385 };
386
387 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
388 int of_genpd_add_provider_simple(struct device_node *np,
389 struct generic_pm_domain *genpd);
390 int of_genpd_add_provider_onecell(struct device_node *np,
391 struct genpd_onecell_data *data);
392 void of_genpd_del_provider(struct device_node *np);
393 int of_genpd_add_device(const struct of_phandle_args *args, struct device *dev);
394 int of_genpd_add_subdomain(const struct of_phandle_args *parent_spec,
395 const struct of_phandle_args *subdomain_spec);
396 int of_genpd_remove_subdomain(const struct of_phandle_args *parent_spec,
397 const struct of_phandle_args *subdomain_spec);
398 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np);
399 int of_genpd_parse_idle_states(struct device_node *dn,
400 struct genpd_power_state **states, int *n);
401
402 int genpd_dev_pm_attach(struct device *dev);
403 struct device *genpd_dev_pm_attach_by_id(struct device *dev,
404 unsigned int index);
405 struct device *genpd_dev_pm_attach_by_name(struct device *dev,
406 const char *name);
407 #else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
of_genpd_add_provider_simple(struct device_node * np,struct generic_pm_domain * genpd)408 static inline int of_genpd_add_provider_simple(struct device_node *np,
409 struct generic_pm_domain *genpd)
410 {
411 return -EOPNOTSUPP;
412 }
413
of_genpd_add_provider_onecell(struct device_node * np,struct genpd_onecell_data * data)414 static inline int of_genpd_add_provider_onecell(struct device_node *np,
415 struct genpd_onecell_data *data)
416 {
417 return -EOPNOTSUPP;
418 }
419
of_genpd_del_provider(struct device_node * np)420 static inline void of_genpd_del_provider(struct device_node *np) {}
421
of_genpd_add_device(const struct of_phandle_args * args,struct device * dev)422 static inline int of_genpd_add_device(const struct of_phandle_args *args,
423 struct device *dev)
424 {
425 return -ENODEV;
426 }
427
of_genpd_add_subdomain(const struct of_phandle_args * parent_spec,const struct of_phandle_args * subdomain_spec)428 static inline int of_genpd_add_subdomain(const struct of_phandle_args *parent_spec,
429 const struct of_phandle_args *subdomain_spec)
430 {
431 return -ENODEV;
432 }
433
of_genpd_remove_subdomain(const struct of_phandle_args * parent_spec,const struct of_phandle_args * subdomain_spec)434 static inline int of_genpd_remove_subdomain(const struct of_phandle_args *parent_spec,
435 const struct of_phandle_args *subdomain_spec)
436 {
437 return -ENODEV;
438 }
439
of_genpd_parse_idle_states(struct device_node * dn,struct genpd_power_state ** states,int * n)440 static 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
genpd_dev_pm_attach(struct device * dev)446 static inline int genpd_dev_pm_attach(struct device *dev)
447 {
448 return 0;
449 }
450
genpd_dev_pm_attach_by_id(struct device * dev,unsigned int index)451 static inline struct device *genpd_dev_pm_attach_by_id(struct device *dev,
452 unsigned int index)
453 {
454 return NULL;
455 }
456
genpd_dev_pm_attach_by_name(struct device * dev,const char * name)457 static inline struct device *genpd_dev_pm_attach_by_name(struct device *dev,
458 const char *name)
459 {
460 return NULL;
461 }
462
463 static inline
of_genpd_remove_last(struct device_node * np)464 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
465 {
466 return ERR_PTR(-EOPNOTSUPP);
467 }
468 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
469
470 #ifdef CONFIG_PM
471 int dev_pm_domain_attach(struct device *dev, bool power_on);
472 struct device *dev_pm_domain_attach_by_id(struct device *dev,
473 unsigned int index);
474 struct device *dev_pm_domain_attach_by_name(struct device *dev,
475 const char *name);
476 int dev_pm_domain_attach_list(struct device *dev,
477 const struct dev_pm_domain_attach_data *data,
478 struct dev_pm_domain_list **list);
479 int devm_pm_domain_attach_list(struct device *dev,
480 const struct dev_pm_domain_attach_data *data,
481 struct dev_pm_domain_list **list);
482 void dev_pm_domain_detach(struct device *dev, bool power_off);
483 void dev_pm_domain_detach_list(struct dev_pm_domain_list *list);
484 int dev_pm_domain_start(struct device *dev);
485 void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
486 int dev_pm_domain_set_performance_state(struct device *dev, unsigned int state);
487 #else
dev_pm_domain_attach(struct device * dev,bool power_on)488 static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
489 {
490 return 0;
491 }
dev_pm_domain_attach_by_id(struct device * dev,unsigned int index)492 static inline struct device *dev_pm_domain_attach_by_id(struct device *dev,
493 unsigned int index)
494 {
495 return NULL;
496 }
dev_pm_domain_attach_by_name(struct device * dev,const char * name)497 static inline struct device *dev_pm_domain_attach_by_name(struct device *dev,
498 const char *name)
499 {
500 return NULL;
501 }
dev_pm_domain_attach_list(struct device * dev,const struct dev_pm_domain_attach_data * data,struct dev_pm_domain_list ** list)502 static 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 }
508
devm_pm_domain_attach_list(struct device * dev,const struct dev_pm_domain_attach_data * data,struct dev_pm_domain_list ** list)509 static 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
dev_pm_domain_detach(struct device * dev,bool power_off)516 static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
dev_pm_domain_detach_list(struct dev_pm_domain_list * list)517 static inline void dev_pm_domain_detach_list(struct dev_pm_domain_list *list) {}
dev_pm_domain_start(struct device * dev)518 static inline int dev_pm_domain_start(struct device *dev)
519 {
520 return 0;
521 }
dev_pm_domain_set(struct device * dev,struct dev_pm_domain * pd)522 static inline void dev_pm_domain_set(struct device *dev,
523 struct dev_pm_domain *pd) {}
dev_pm_domain_set_performance_state(struct device * dev,unsigned int state)524 static inline int dev_pm_domain_set_performance_state(struct device *dev,
525 unsigned int state)
526 {
527 return 0;
528 }
529 #endif
530
531 #endif /* _LINUX_PM_DOMAIN_H */
532