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