Merge tag 'char-misc-5.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregk...
[linux-2.6-microblaze.git] / include / linux / pm_domain.h
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.h>
20
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.
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.
56  *
57  * GENPD_FLAG_RPM_ALWAYS_ON:    Instructs genpd to always keep the PM domain
58  *                              powered on except for system suspend.
59  *
60  * GENPD_FLAG_MIN_RESIDENCY:    Enable the genpd governor to consider its
61  *                              components' next wakeup when determining the
62  *                              optimal idle state.
63  */
64 #define GENPD_FLAG_PM_CLK        (1U << 0)
65 #define GENPD_FLAG_IRQ_SAFE      (1U << 1)
66 #define GENPD_FLAG_ALWAYS_ON     (1U << 2)
67 #define GENPD_FLAG_ACTIVE_WAKEUP (1U << 3)
68 #define GENPD_FLAG_CPU_DOMAIN    (1U << 4)
69 #define GENPD_FLAG_RPM_ALWAYS_ON (1U << 5)
70 #define GENPD_FLAG_MIN_RESIDENCY (1U << 6)
71
72 enum gpd_status {
73         GENPD_STATE_ON = 0,     /* PM domain is on */
74         GENPD_STATE_OFF,        /* PM domain is off */
75 };
76
77 enum genpd_notication {
78         GENPD_NOTIFY_PRE_OFF = 0,
79         GENPD_NOTIFY_OFF,
80         GENPD_NOTIFY_PRE_ON,
81         GENPD_NOTIFY_ON,
82 };
83
84 struct dev_power_governor {
85         bool (*power_down_ok)(struct dev_pm_domain *domain);
86         bool (*suspend_ok)(struct device *dev);
87 };
88
89 struct gpd_dev_ops {
90         int (*start)(struct device *dev);
91         int (*stop)(struct device *dev);
92 };
93
94 struct genpd_governor_data {
95         s64 max_off_time_ns;
96         bool max_off_time_changed;
97         ktime_t next_wakeup;
98         bool cached_power_down_ok;
99         bool cached_power_down_state_idx;
100 };
101
102 struct genpd_power_state {
103         s64 power_off_latency_ns;
104         s64 power_on_latency_ns;
105         s64 residency_ns;
106         u64 usage;
107         u64 rejected;
108         struct fwnode_handle *fwnode;
109         u64 idle_time;
110         void *data;
111 };
112
113 struct genpd_lock_ops;
114 struct dev_pm_opp;
115 struct opp_table;
116
117 struct generic_pm_domain {
118         struct device dev;
119         struct dev_pm_domain domain;    /* PM domain operations */
120         struct list_head gpd_list_node; /* Node in the global PM domains list */
121         struct list_head parent_links;  /* Links with PM domain as a parent */
122         struct list_head child_links;   /* Links with PM domain as a child */
123         struct list_head dev_list;      /* List of devices */
124         struct dev_power_governor *gov;
125         struct genpd_governor_data *gd; /* Data used by a genpd governor. */
126         struct work_struct power_off_work;
127         struct fwnode_handle *provider; /* Identity of the domain provider */
128         bool has_provider;
129         const char *name;
130         atomic_t sd_count;      /* Number of subdomains with power "on" */
131         enum gpd_status status; /* Current state of the domain */
132         unsigned int device_count;      /* Number of devices */
133         unsigned int suspended_count;   /* System suspend device counter */
134         unsigned int prepared_count;    /* Suspend counter of prepared devices */
135         unsigned int performance_state; /* Aggregated max performance state */
136         cpumask_var_t cpus;             /* A cpumask of the attached CPUs */
137         int (*power_off)(struct generic_pm_domain *domain);
138         int (*power_on)(struct generic_pm_domain *domain);
139         struct raw_notifier_head power_notifiers; /* Power on/off notifiers */
140         struct opp_table *opp_table;    /* OPP table of the genpd */
141         unsigned int (*opp_to_performance_state)(struct generic_pm_domain *genpd,
142                                                  struct dev_pm_opp *opp);
143         int (*set_performance_state)(struct generic_pm_domain *genpd,
144                                      unsigned int state);
145         struct gpd_dev_ops dev_ops;
146         int (*attach_dev)(struct generic_pm_domain *domain,
147                           struct device *dev);
148         void (*detach_dev)(struct generic_pm_domain *domain,
149                            struct device *dev);
150         unsigned int flags;             /* Bit field of configs for genpd */
151         struct genpd_power_state *states;
152         void (*free_states)(struct genpd_power_state *states,
153                             unsigned int state_count);
154         unsigned int state_count; /* number of states */
155         unsigned int state_idx; /* state that genpd will go to when off */
156         u64 on_time;
157         u64 accounting_time;
158         const struct genpd_lock_ops *lock_ops;
159         union {
160                 struct mutex mlock;
161                 struct {
162                         spinlock_t slock;
163                         unsigned long lock_flags;
164                 };
165         };
166
167 };
168
169 static inline struct generic_pm_domain *pd_to_genpd(struct dev_pm_domain *pd)
170 {
171         return container_of(pd, struct generic_pm_domain, domain);
172 }
173
174 struct gpd_link {
175         struct generic_pm_domain *parent;
176         struct list_head parent_node;
177         struct generic_pm_domain *child;
178         struct list_head child_node;
179
180         /* Sub-domain's per-master domain performance state */
181         unsigned int performance_state;
182         unsigned int prev_performance_state;
183 };
184
185 struct gpd_timing_data {
186         s64 suspend_latency_ns;
187         s64 resume_latency_ns;
188         s64 effective_constraint_ns;
189         ktime_t next_wakeup;
190         bool constraint_changed;
191         bool cached_suspend_ok;
192 };
193
194 struct pm_domain_data {
195         struct list_head list_node;
196         struct device *dev;
197 };
198
199 struct generic_pm_domain_data {
200         struct pm_domain_data base;
201         struct gpd_timing_data *td;
202         struct notifier_block nb;
203         struct notifier_block *power_nb;
204         int cpu;
205         unsigned int performance_state;
206         unsigned int default_pstate;
207         unsigned int rpm_pstate;
208         void *data;
209 };
210
211 #ifdef CONFIG_PM_GENERIC_DOMAINS
212 static inline struct generic_pm_domain_data *to_gpd_data(struct pm_domain_data *pdd)
213 {
214         return container_of(pdd, struct generic_pm_domain_data, base);
215 }
216
217 static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
218 {
219         return to_gpd_data(dev->power.subsys_data->domain_data);
220 }
221
222 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev);
223 int pm_genpd_remove_device(struct device *dev);
224 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
225                            struct generic_pm_domain *subdomain);
226 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
227                               struct generic_pm_domain *subdomain);
228 int pm_genpd_init(struct generic_pm_domain *genpd,
229                   struct dev_power_governor *gov, bool is_off);
230 int pm_genpd_remove(struct generic_pm_domain *genpd);
231 int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state);
232 int dev_pm_genpd_add_notifier(struct device *dev, struct notifier_block *nb);
233 int dev_pm_genpd_remove_notifier(struct device *dev);
234 void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next);
235
236 extern struct dev_power_governor simple_qos_governor;
237 extern struct dev_power_governor pm_domain_always_on_gov;
238 #ifdef CONFIG_CPU_IDLE
239 extern struct dev_power_governor pm_domain_cpu_gov;
240 #endif
241 #else
242
243 static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
244 {
245         return ERR_PTR(-ENOSYS);
246 }
247 static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
248                                       struct device *dev)
249 {
250         return -ENOSYS;
251 }
252 static inline int pm_genpd_remove_device(struct device *dev)
253 {
254         return -ENOSYS;
255 }
256 static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
257                                          struct generic_pm_domain *subdomain)
258 {
259         return -ENOSYS;
260 }
261 static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
262                                             struct generic_pm_domain *subdomain)
263 {
264         return -ENOSYS;
265 }
266 static inline int pm_genpd_init(struct generic_pm_domain *genpd,
267                                 struct dev_power_governor *gov, bool is_off)
268 {
269         return -ENOSYS;
270 }
271 static inline int pm_genpd_remove(struct generic_pm_domain *genpd)
272 {
273         return -EOPNOTSUPP;
274 }
275
276 static inline int dev_pm_genpd_set_performance_state(struct device *dev,
277                                                      unsigned int state)
278 {
279         return -EOPNOTSUPP;
280 }
281
282 static inline int dev_pm_genpd_add_notifier(struct device *dev,
283                                             struct notifier_block *nb)
284 {
285         return -EOPNOTSUPP;
286 }
287
288 static inline int dev_pm_genpd_remove_notifier(struct device *dev)
289 {
290         return -EOPNOTSUPP;
291 }
292
293 static inline void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next)
294 { }
295
296 #define simple_qos_governor             (*(struct dev_power_governor *)(NULL))
297 #define pm_domain_always_on_gov         (*(struct dev_power_governor *)(NULL))
298 #endif
299
300 #ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
301 void dev_pm_genpd_suspend(struct device *dev);
302 void dev_pm_genpd_resume(struct device *dev);
303 #else
304 static inline void dev_pm_genpd_suspend(struct device *dev) {}
305 static inline void dev_pm_genpd_resume(struct device *dev) {}
306 #endif
307
308 /* OF PM domain providers */
309 struct of_device_id;
310
311 typedef struct generic_pm_domain *(*genpd_xlate_t)(struct of_phandle_args *args,
312                                                    void *data);
313
314 struct genpd_onecell_data {
315         struct generic_pm_domain **domains;
316         unsigned int num_domains;
317         genpd_xlate_t xlate;
318 };
319
320 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
321 int of_genpd_add_provider_simple(struct device_node *np,
322                                  struct generic_pm_domain *genpd);
323 int of_genpd_add_provider_onecell(struct device_node *np,
324                                   struct genpd_onecell_data *data);
325 void of_genpd_del_provider(struct device_node *np);
326 int of_genpd_add_device(struct of_phandle_args *args, struct device *dev);
327 int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
328                            struct of_phandle_args *subdomain_spec);
329 int of_genpd_remove_subdomain(struct of_phandle_args *parent_spec,
330                               struct of_phandle_args *subdomain_spec);
331 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np);
332 int of_genpd_parse_idle_states(struct device_node *dn,
333                                struct genpd_power_state **states, int *n);
334 unsigned int pm_genpd_opp_to_performance_state(struct device *genpd_dev,
335                                                struct dev_pm_opp *opp);
336
337 int genpd_dev_pm_attach(struct device *dev);
338 struct device *genpd_dev_pm_attach_by_id(struct device *dev,
339                                          unsigned int index);
340 struct device *genpd_dev_pm_attach_by_name(struct device *dev,
341                                            const char *name);
342 #else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
343 static inline int of_genpd_add_provider_simple(struct device_node *np,
344                                         struct generic_pm_domain *genpd)
345 {
346         return -EOPNOTSUPP;
347 }
348
349 static inline int of_genpd_add_provider_onecell(struct device_node *np,
350                                         struct genpd_onecell_data *data)
351 {
352         return -EOPNOTSUPP;
353 }
354
355 static inline void of_genpd_del_provider(struct device_node *np) {}
356
357 static inline int of_genpd_add_device(struct of_phandle_args *args,
358                                       struct device *dev)
359 {
360         return -ENODEV;
361 }
362
363 static inline int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
364                                          struct of_phandle_args *subdomain_spec)
365 {
366         return -ENODEV;
367 }
368
369 static inline int of_genpd_remove_subdomain(struct of_phandle_args *parent_spec,
370                                         struct of_phandle_args *subdomain_spec)
371 {
372         return -ENODEV;
373 }
374
375 static inline int of_genpd_parse_idle_states(struct device_node *dn,
376                         struct genpd_power_state **states, int *n)
377 {
378         return -ENODEV;
379 }
380
381 static inline unsigned int
382 pm_genpd_opp_to_performance_state(struct device *genpd_dev,
383                                   struct dev_pm_opp *opp)
384 {
385         return 0;
386 }
387
388 static inline int genpd_dev_pm_attach(struct device *dev)
389 {
390         return 0;
391 }
392
393 static inline struct device *genpd_dev_pm_attach_by_id(struct device *dev,
394                                                        unsigned int index)
395 {
396         return NULL;
397 }
398
399 static inline struct device *genpd_dev_pm_attach_by_name(struct device *dev,
400                                                          const char *name)
401 {
402         return NULL;
403 }
404
405 static inline
406 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
407 {
408         return ERR_PTR(-EOPNOTSUPP);
409 }
410 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
411
412 #ifdef CONFIG_PM
413 int dev_pm_domain_attach(struct device *dev, bool power_on);
414 struct device *dev_pm_domain_attach_by_id(struct device *dev,
415                                           unsigned int index);
416 struct device *dev_pm_domain_attach_by_name(struct device *dev,
417                                             const char *name);
418 void dev_pm_domain_detach(struct device *dev, bool power_off);
419 int dev_pm_domain_start(struct device *dev);
420 void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
421 #else
422 static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
423 {
424         return 0;
425 }
426 static inline struct device *dev_pm_domain_attach_by_id(struct device *dev,
427                                                         unsigned int index)
428 {
429         return NULL;
430 }
431 static inline struct device *dev_pm_domain_attach_by_name(struct device *dev,
432                                                           const char *name)
433 {
434         return NULL;
435 }
436 static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
437 static inline int dev_pm_domain_start(struct device *dev)
438 {
439         return 0;
440 }
441 static inline void dev_pm_domain_set(struct device *dev,
442                                      struct dev_pm_domain *pd) {}
443 #endif
444
445 #endif /* _LINUX_PM_DOMAIN_H */