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 * PD_FLAG_REQUIRED_OPP: Assign required_devs for the required OPPs. The
34 * index of the required OPP must correspond to the
35 * index in the array of the pd_names. If pd_names
36 * isn't specified, the index just follows the
37 * index for the attached PM domain.
38 *
39 * PD_FLAG_ATTACH_POWER_ON: Power on the domain during attach.
40 *
41 * PD_FLAG_DETACH_POWER_OFF: Power off the domain during detach.
42 *
43 */
44#define PD_FLAG_NO_DEV_LINK BIT(0)
45#define PD_FLAG_DEV_LINK_ON BIT(1)
46#define PD_FLAG_REQUIRED_OPP BIT(2)
47#define PD_FLAG_ATTACH_POWER_ON BIT(3)
48#define PD_FLAG_DETACH_POWER_OFF BIT(4)
49
50struct dev_pm_domain_attach_data {
51 const char * const *pd_names;
52 const u32 num_pd_names;
53 const u32 pd_flags;
54};
55
56struct dev_pm_domain_list {
57 struct device **pd_devs;
58 struct device_link **pd_links;
59 u32 *opp_tokens;
60 u32 num_pds;
61};
62
63/*
64 * Flags to control the behaviour of a genpd.
65 *
66 * These flags may be set in the struct generic_pm_domain's flags field by a
67 * genpd backend driver. The flags must be set before it calls pm_genpd_init(),
68 * which initializes a genpd.
69 *
70 * GENPD_FLAG_PM_CLK: Instructs genpd to use the PM clk framework,
71 * while powering on/off attached devices.
72 *
73 * GENPD_FLAG_IRQ_SAFE: This informs genpd that its backend callbacks,
74 * ->power_on|off(), doesn't sleep. Hence, these
75 * can be invoked from within atomic context, which
76 * enables genpd to power on/off the PM domain,
77 * even when pm_runtime_is_irq_safe() returns true,
78 * for any of its attached devices. Note that, a
79 * genpd having this flag set, requires its
80 * masterdomains to also have it set.
81 *
82 * GENPD_FLAG_ALWAYS_ON: Instructs genpd to always keep the PM domain
83 * powered on.
84 *
85 * GENPD_FLAG_ACTIVE_WAKEUP: Instructs genpd to keep the PM domain powered
86 * on, in case any of its attached devices is used
87 * in the wakeup path to serve system wakeups.
88 *
89 * GENPD_FLAG_CPU_DOMAIN: Instructs genpd that it should expect to get
90 * devices attached, which may belong to CPUs or
91 * possibly have subdomains with CPUs attached.
92 * This flag enables the genpd backend driver to
93 * deploy idle power management support for CPUs
94 * and groups of CPUs. Note that, the backend
95 * driver must then comply with the so called,
96 * last-man-standing algorithm, for the CPUs in the
97 * PM domain.
98 *
99 * GENPD_FLAG_RPM_ALWAYS_ON: Instructs genpd to always keep the PM domain
100 * powered on except for system suspend.
101 *
102 * GENPD_FLAG_MIN_RESIDENCY: Enable the genpd governor to consider its
103 * components' next wakeup when determining the
104 * optimal idle state.
105 *
106 * GENPD_FLAG_OPP_TABLE_FW: The genpd provider supports performance states,
107 * but its corresponding OPP tables are not
108 * described in DT, but are given directly by FW.
109 *
110 * GENPD_FLAG_DEV_NAME_FW: Instructs genpd to generate an unique device name
111 * using ida. It is used by genpd providers which
112 * get their genpd-names directly from FW.
113 *
114 * GENPD_FLAG_NO_SYNC_STATE: The ->sync_state() support is implemented in a
115 * genpd provider specific way, likely through a
116 * parent device node. This flag makes genpd to
117 * skip its internal support for this.
118 *
119 * GENPD_FLAG_NO_STAY_ON: For genpd OF providers a powered-on PM domain at
120 * initialization is prevented from being
121 * powered-off until the ->sync_state() callback is
122 * invoked. This flag informs genpd to allow a
123 * power-off without waiting for ->sync_state().
124 */
125#define GENPD_FLAG_PM_CLK (1U << 0)
126#define GENPD_FLAG_IRQ_SAFE (1U << 1)
127#define GENPD_FLAG_ALWAYS_ON (1U << 2)
128#define GENPD_FLAG_ACTIVE_WAKEUP (1U << 3)
129#define GENPD_FLAG_CPU_DOMAIN (1U << 4)
130#define GENPD_FLAG_RPM_ALWAYS_ON (1U << 5)
131#define GENPD_FLAG_MIN_RESIDENCY (1U << 6)
132#define GENPD_FLAG_OPP_TABLE_FW (1U << 7)
133#define GENPD_FLAG_DEV_NAME_FW (1U << 8)
134#define GENPD_FLAG_NO_SYNC_STATE (1U << 9)
135#define GENPD_FLAG_NO_STAY_ON (1U << 10)
136
137enum gpd_status {
138 GENPD_STATE_ON = 0, /* PM domain is on */
139 GENPD_STATE_OFF, /* PM domain is off */
140};
141
142enum genpd_notication {
143 GENPD_NOTIFY_PRE_OFF = 0,
144 GENPD_NOTIFY_OFF,
145 GENPD_NOTIFY_PRE_ON,
146 GENPD_NOTIFY_ON,
147};
148
149enum genpd_sync_state {
150 GENPD_SYNC_STATE_OFF = 0,
151 GENPD_SYNC_STATE_SIMPLE,
152 GENPD_SYNC_STATE_ONECELL,
153};
154
155struct dev_power_governor {
156 bool (*power_down_ok)(struct dev_pm_domain *domain);
157 bool (*suspend_ok)(struct device *dev);
158};
159
160struct gpd_dev_ops {
161 int (*start)(struct device *dev);
162 int (*stop)(struct device *dev);
163};
164
165struct genpd_governor_data {
166 s64 max_off_time_ns;
167 bool max_off_time_changed;
168 ktime_t next_wakeup;
169 ktime_t next_hrtimer;
170 ktime_t last_enter;
171 bool reflect_residency;
172 bool cached_power_down_ok;
173 bool cached_power_down_state_idx;
174};
175
176struct genpd_power_state {
177 const char *name;
178 s64 power_off_latency_ns;
179 s64 power_on_latency_ns;
180 s64 residency_ns;
181 u64 usage;
182 u64 rejected;
183 u64 above;
184 u64 below;
185 struct fwnode_handle *fwnode;
186 u64 idle_time;
187 void *data;
188};
189
190struct genpd_lock_ops;
191struct opp_table;
192
193struct generic_pm_domain {
194 struct device dev;
195 struct dev_pm_domain domain; /* PM domain operations */
196 struct list_head gpd_list_node; /* Node in the global PM domains list */
197 struct list_head parent_links; /* Links with PM domain as a parent */
198 struct list_head child_links; /* Links with PM domain as a child */
199 struct list_head dev_list; /* List of devices */
200 struct dev_power_governor *gov;
201 struct genpd_governor_data *gd; /* Data used by a genpd governor. */
202 struct work_struct power_off_work;
203 struct fwnode_handle *provider; /* Identity of the domain provider */
204 bool has_provider;
205 const char *name;
206 atomic_t sd_count; /* Number of subdomains with power "on" */
207 enum gpd_status status; /* Current state of the domain */
208 unsigned int device_count; /* Number of devices */
209 unsigned int device_id; /* unique device id */
210 unsigned int suspended_count; /* System suspend device counter */
211 unsigned int prepared_count; /* Suspend counter of prepared devices */
212 unsigned int performance_state; /* Aggregated max performance state */
213 cpumask_var_t cpus; /* A cpumask of the attached CPUs */
214 bool synced_poweroff; /* A consumer needs a synced poweroff */
215 bool stay_on; /* Stay powered-on during boot. */
216 enum genpd_sync_state sync_state; /* How sync_state is managed. */
217 int (*power_off)(struct generic_pm_domain *domain);
218 int (*power_on)(struct generic_pm_domain *domain);
219 struct raw_notifier_head power_notifiers; /* Power on/off notifiers */
220 struct opp_table *opp_table; /* OPP table of the genpd */
221 int (*set_performance_state)(struct generic_pm_domain *genpd,
222 unsigned int state);
223 struct gpd_dev_ops dev_ops;
224 int (*set_hwmode_dev)(struct generic_pm_domain *domain,
225 struct device *dev, bool enable);
226 bool (*get_hwmode_dev)(struct generic_pm_domain *domain,
227 struct device *dev);
228 int (*attach_dev)(struct generic_pm_domain *domain,
229 struct device *dev);
230 void (*detach_dev)(struct generic_pm_domain *domain,
231 struct device *dev);
232 unsigned int flags; /* Bit field of configs for genpd */
233 struct genpd_power_state *states;
234 void (*free_states)(struct genpd_power_state *states,
235 unsigned int state_count);
236 unsigned int state_count; /* number of states */
237 unsigned int state_idx; /* state that genpd will go to when off */
238 u64 on_time;
239 u64 accounting_time;
240 const struct genpd_lock_ops *lock_ops;
241 union {
242 struct mutex mlock;
243 struct {
244 spinlock_t slock;
245 unsigned long lock_flags;
246 };
247 struct {
248 raw_spinlock_t raw_slock;
249 unsigned long raw_lock_flags;
250 };
251 };
252};
253
254static inline struct generic_pm_domain *pd_to_genpd(struct dev_pm_domain *pd)
255{
256 return container_of(pd, struct generic_pm_domain, domain);
257}
258
259struct gpd_link {
260 struct generic_pm_domain *parent;
261 struct list_head parent_node;
262 struct generic_pm_domain *child;
263 struct list_head child_node;
264
265 /* Sub-domain's per-master domain performance state */
266 unsigned int performance_state;
267 unsigned int prev_performance_state;
268};
269
270struct gpd_timing_data {
271 s64 suspend_latency_ns;
272 s64 resume_latency_ns;
273 s64 effective_constraint_ns;
274 ktime_t next_wakeup;
275 bool constraint_changed;
276 bool cached_suspend_ok;
277};
278
279struct pm_domain_data {
280 struct list_head list_node;
281 struct device *dev;
282};
283
284struct generic_pm_domain_data {
285 struct pm_domain_data base;
286 struct gpd_timing_data *td;
287 struct notifier_block nb;
288 struct notifier_block *power_nb;
289 int cpu;
290 unsigned int performance_state;
291 unsigned int default_pstate;
292 unsigned int rpm_pstate;
293 unsigned int opp_token;
294 bool hw_mode;
295 bool rpm_always_on;
296 void *data;
297};
298
299#ifdef CONFIG_PM_GENERIC_DOMAINS
300static inline struct generic_pm_domain_data *to_gpd_data(struct pm_domain_data *pdd)
301{
302 return container_of(pdd, struct generic_pm_domain_data, base);
303}
304
305static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
306{
307 return to_gpd_data(dev->power.subsys_data->domain_data);
308}
309
310int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev);
311int pm_genpd_remove_device(struct device *dev);
312int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
313 struct generic_pm_domain *subdomain);
314int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
315 struct generic_pm_domain *subdomain);
316int pm_genpd_init(struct generic_pm_domain *genpd,
317 struct dev_power_governor *gov, bool is_off);
318int pm_genpd_remove(struct generic_pm_domain *genpd);
319void pm_genpd_inc_rejected(struct generic_pm_domain *genpd,
320 unsigned int state_idx);
321struct device *dev_to_genpd_dev(struct device *dev);
322int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state);
323int dev_pm_genpd_add_notifier(struct device *dev, struct notifier_block *nb);
324int dev_pm_genpd_remove_notifier(struct device *dev);
325void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next);
326ktime_t dev_pm_genpd_get_next_hrtimer(struct device *dev);
327void dev_pm_genpd_synced_poweroff(struct device *dev);
328int dev_pm_genpd_set_hwmode(struct device *dev, bool enable);
329bool dev_pm_genpd_get_hwmode(struct device *dev);
330int dev_pm_genpd_rpm_always_on(struct device *dev, bool on);
331bool dev_pm_genpd_is_on(struct device *dev);
332
333extern struct dev_power_governor simple_qos_governor;
334extern struct dev_power_governor pm_domain_always_on_gov;
335#ifdef CONFIG_CPU_IDLE
336extern struct dev_power_governor pm_domain_cpu_gov;
337#endif
338#else
339
340static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
341{
342 return ERR_PTR(error: -ENOSYS);
343}
344static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
345 struct device *dev)
346{
347 return -ENOSYS;
348}
349static inline int pm_genpd_remove_device(struct device *dev)
350{
351 return -ENOSYS;
352}
353static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
354 struct generic_pm_domain *subdomain)
355{
356 return -ENOSYS;
357}
358static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
359 struct generic_pm_domain *subdomain)
360{
361 return -ENOSYS;
362}
363static inline int pm_genpd_init(struct generic_pm_domain *genpd,
364 struct dev_power_governor *gov, bool is_off)
365{
366 return -ENOSYS;
367}
368static inline int pm_genpd_remove(struct generic_pm_domain *genpd)
369{
370 return -EOPNOTSUPP;
371}
372
373static inline void pm_genpd_inc_rejected(struct generic_pm_domain *genpd,
374 unsigned int state_idx)
375{ }
376
377static inline struct device *dev_to_genpd_dev(struct device *dev)
378{
379 return ERR_PTR(error: -EOPNOTSUPP);
380}
381
382static inline int dev_pm_genpd_set_performance_state(struct device *dev,
383 unsigned int state)
384{
385 return -EOPNOTSUPP;
386}
387
388static inline int dev_pm_genpd_add_notifier(struct device *dev,
389 struct notifier_block *nb)
390{
391 return -EOPNOTSUPP;
392}
393
394static inline int dev_pm_genpd_remove_notifier(struct device *dev)
395{
396 return -EOPNOTSUPP;
397}
398
399static inline void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next)
400{ }
401
402static inline ktime_t dev_pm_genpd_get_next_hrtimer(struct device *dev)
403{
404 return KTIME_MAX;
405}
406static inline void dev_pm_genpd_synced_poweroff(struct device *dev)
407{ }
408
409static inline int dev_pm_genpd_set_hwmode(struct device *dev, bool enable)
410{
411 return -EOPNOTSUPP;
412}
413
414static inline bool dev_pm_genpd_get_hwmode(struct device *dev)
415{
416 return false;
417}
418
419static inline int dev_pm_genpd_rpm_always_on(struct device *dev, bool on)
420{
421 return -EOPNOTSUPP;
422}
423
424static inline bool dev_pm_genpd_is_on(struct device *dev)
425{
426 return false;
427}
428
429#define simple_qos_governor (*(struct dev_power_governor *)(NULL))
430#define pm_domain_always_on_gov (*(struct dev_power_governor *)(NULL))
431#endif
432
433#ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
434void dev_pm_genpd_suspend(struct device *dev);
435void dev_pm_genpd_resume(struct device *dev);
436#else
437static inline void dev_pm_genpd_suspend(struct device *dev) {}
438static inline void dev_pm_genpd_resume(struct device *dev) {}
439#endif
440
441/* OF PM domain providers */
442struct of_device_id;
443
444typedef struct generic_pm_domain *(*genpd_xlate_t)(const struct of_phandle_args *args,
445 void *data);
446
447struct genpd_onecell_data {
448 struct generic_pm_domain **domains;
449 unsigned int num_domains;
450 genpd_xlate_t xlate;
451};
452
453#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
454int of_genpd_add_provider_simple(struct device_node *np,
455 struct generic_pm_domain *genpd);
456int of_genpd_add_provider_onecell(struct device_node *np,
457 struct genpd_onecell_data *data);
458void of_genpd_del_provider(struct device_node *np);
459int of_genpd_add_device(const struct of_phandle_args *args, struct device *dev);
460int of_genpd_add_subdomain(const struct of_phandle_args *parent_spec,
461 const struct of_phandle_args *subdomain_spec);
462int of_genpd_remove_subdomain(const struct of_phandle_args *parent_spec,
463 const struct of_phandle_args *subdomain_spec);
464struct generic_pm_domain *of_genpd_remove_last(struct device_node *np);
465int of_genpd_parse_idle_states(struct device_node *dn,
466 struct genpd_power_state **states, int *n);
467void of_genpd_sync_state(struct device_node *np);
468
469int genpd_dev_pm_attach(struct device *dev);
470struct device *genpd_dev_pm_attach_by_id(struct device *dev,
471 unsigned int index);
472struct device *genpd_dev_pm_attach_by_name(struct device *dev,
473 const char *name);
474#else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
475static inline int of_genpd_add_provider_simple(struct device_node *np,
476 struct generic_pm_domain *genpd)
477{
478 return -EOPNOTSUPP;
479}
480
481static inline int of_genpd_add_provider_onecell(struct device_node *np,
482 struct genpd_onecell_data *data)
483{
484 return -EOPNOTSUPP;
485}
486
487static inline void of_genpd_del_provider(struct device_node *np) {}
488
489static inline int of_genpd_add_device(const struct of_phandle_args *args,
490 struct device *dev)
491{
492 return -ENODEV;
493}
494
495static inline int of_genpd_add_subdomain(const struct of_phandle_args *parent_spec,
496 const struct of_phandle_args *subdomain_spec)
497{
498 return -ENODEV;
499}
500
501static inline int of_genpd_remove_subdomain(const struct of_phandle_args *parent_spec,
502 const struct of_phandle_args *subdomain_spec)
503{
504 return -ENODEV;
505}
506
507static inline int of_genpd_parse_idle_states(struct device_node *dn,
508 struct genpd_power_state **states, int *n)
509{
510 return -ENODEV;
511}
512
513static inline void of_genpd_sync_state(struct device_node *np) {}
514
515static inline int genpd_dev_pm_attach(struct device *dev)
516{
517 return 0;
518}
519
520static inline struct device *genpd_dev_pm_attach_by_id(struct device *dev,
521 unsigned int index)
522{
523 return NULL;
524}
525
526static inline struct device *genpd_dev_pm_attach_by_name(struct device *dev,
527 const char *name)
528{
529 return NULL;
530}
531
532static inline
533struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
534{
535 return ERR_PTR(error: -EOPNOTSUPP);
536}
537#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
538
539#ifdef CONFIG_PM
540int dev_pm_domain_attach(struct device *dev, u32 flags);
541struct device *dev_pm_domain_attach_by_id(struct device *dev,
542 unsigned int index);
543struct device *dev_pm_domain_attach_by_name(struct device *dev,
544 const char *name);
545int dev_pm_domain_attach_list(struct device *dev,
546 const struct dev_pm_domain_attach_data *data,
547 struct dev_pm_domain_list **list);
548int devm_pm_domain_attach_list(struct device *dev,
549 const struct dev_pm_domain_attach_data *data,
550 struct dev_pm_domain_list **list);
551void dev_pm_domain_detach(struct device *dev, bool power_off);
552void dev_pm_domain_detach_list(struct dev_pm_domain_list *list);
553int dev_pm_domain_start(struct device *dev);
554void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
555int dev_pm_domain_set_performance_state(struct device *dev, unsigned int state);
556#else
557static inline int dev_pm_domain_attach(struct device *dev, u32 flags)
558{
559 return 0;
560}
561static inline struct device *dev_pm_domain_attach_by_id(struct device *dev,
562 unsigned int index)
563{
564 return NULL;
565}
566static inline struct device *dev_pm_domain_attach_by_name(struct device *dev,
567 const char *name)
568{
569 return NULL;
570}
571static inline int dev_pm_domain_attach_list(struct device *dev,
572 const struct dev_pm_domain_attach_data *data,
573 struct dev_pm_domain_list **list)
574{
575 return 0;
576}
577
578static inline int devm_pm_domain_attach_list(struct device *dev,
579 const struct dev_pm_domain_attach_data *data,
580 struct dev_pm_domain_list **list)
581{
582 return 0;
583}
584
585static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
586static inline void dev_pm_domain_detach_list(struct dev_pm_domain_list *list) {}
587static inline int dev_pm_domain_start(struct device *dev)
588{
589 return 0;
590}
591static inline void dev_pm_domain_set(struct device *dev,
592 struct dev_pm_domain *pd) {}
593static inline int dev_pm_domain_set_performance_state(struct device *dev,
594 unsigned int state)
595{
596 return 0;
597}
598#endif
599
600#endif /* _LINUX_PM_DOMAIN_H */
601