blob: 1ed5874bcee09afef28c505574bbd2d213abd785 [file] [log] [blame]
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001/*
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>
Paul Gortmaker313162d2012-01-30 11:46:54 -050013#include <linux/mutex.h>
14#include <linux/pm.h>
Guennadi Liakhovetski4f042cd2011-12-01 00:05:31 +010015#include <linux/err.h>
Thomas Abrahamc8aa1302012-01-27 15:22:07 +090016#include <linux/of.h>
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020017#include <linux/notifier.h>
Lina Iyerd716f472016-10-14 10:47:55 -070018#include <linux/spinlock.h>
Rafael J. Wysockif7218892011-07-01 22:12:45 +020019
Ulf Hanssone5089c22018-10-03 16:38:16 +020020/*
21 * Flags to control the behaviour of a genpd.
22 *
23 * These flags may be set in the struct generic_pm_domain's flags field by a
24 * genpd backend driver. The flags must be set before it calls pm_genpd_init(),
25 * which initializes a genpd.
26 *
27 * GENPD_FLAG_PM_CLK: Instructs genpd to use the PM clk framework,
28 * while powering on/off attached devices.
29 *
30 * GENPD_FLAG_IRQ_SAFE: This informs genpd that its backend callbacks,
31 * ->power_on|off(), doesn't sleep. Hence, these
32 * can be invoked from within atomic context, which
33 * enables genpd to power on/off the PM domain,
34 * even when pm_runtime_is_irq_safe() returns true,
35 * for any of its attached devices. Note that, a
36 * genpd having this flag set, requires its
37 * masterdomains to also have it set.
38 *
39 * GENPD_FLAG_ALWAYS_ON: Instructs genpd to always keep the PM domain
40 * powered on.
41 *
42 * GENPD_FLAG_ACTIVE_WAKEUP: Instructs genpd to keep the PM domain powered
43 * on, in case any of its attached devices is used
44 * in the wakeup path to serve system wakeups.
45 */
46#define GENPD_FLAG_PM_CLK (1U << 0)
47#define GENPD_FLAG_IRQ_SAFE (1U << 1)
48#define GENPD_FLAG_ALWAYS_ON (1U << 2)
49#define GENPD_FLAG_ACTIVE_WAKEUP (1U << 3)
Ulf Hanssonc11f6f52014-12-01 12:50:21 +010050
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020051enum gpd_status {
52 GPD_STATE_ACTIVE = 0, /* PM domain is active */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020053 GPD_STATE_POWER_OFF, /* PM domain is off */
54};
Rafael J. Wysocki596ba342011-07-01 22:13:19 +020055
Rafael J. Wysockif7218892011-07-01 22:12:45 +020056struct dev_power_governor {
57 bool (*power_down_ok)(struct dev_pm_domain *domain);
Ulf Hansson9df39212016-03-31 11:21:25 +020058 bool (*suspend_ok)(struct device *dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +020059};
60
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010061struct gpd_dev_ops {
62 int (*start)(struct device *dev);
63 int (*stop)(struct device *dev);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010064};
65
Axel Haslamfc5cbf02016-02-15 11:10:51 +010066struct genpd_power_state {
67 s64 power_off_latency_ns;
68 s64 power_on_latency_ns;
Lina Iyer405f7222016-10-14 10:47:50 -070069 s64 residency_ns;
Lina Iyer0c9b6942016-10-14 10:47:52 -070070 struct fwnode_handle *fwnode;
Thara Gopinathafece3a2017-07-14 13:10:15 -040071 ktime_t idle_time;
Axel Haslamfc5cbf02016-02-15 11:10:51 +010072};
73
Lina Iyer35241d12016-10-14 10:47:54 -070074struct genpd_lock_ops;
Viresh Kumar6e417662017-11-29 15:21:51 +053075struct dev_pm_opp;
Viresh Kumar1067ae32018-11-02 11:18:08 +053076struct opp_table;
Lina Iyer35241d12016-10-14 10:47:54 -070077
Rafael J. Wysockif7218892011-07-01 22:12:45 +020078struct generic_pm_domain {
Viresh Kumar401ea152017-03-17 11:26:19 +053079 struct device dev;
Rafael J. Wysockif7218892011-07-01 22:12:45 +020080 struct dev_pm_domain domain; /* PM domain operations */
Rafael J. Wysocki5125bbf382011-07-13 12:31:52 +020081 struct list_head gpd_list_node; /* Node in the global PM domains list */
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +020082 struct list_head master_links; /* Links with PM domain as a master */
83 struct list_head slave_links; /* Links with PM domain as a slave */
Rafael J. Wysockif7218892011-07-01 22:12:45 +020084 struct list_head dev_list; /* List of devices */
Rafael J. Wysockif7218892011-07-01 22:12:45 +020085 struct dev_power_governor *gov;
86 struct work_struct power_off_work;
Jon Hunterde0aa06d2016-09-12 12:01:12 +010087 struct fwnode_handle *provider; /* Identity of the domain provider */
88 bool has_provider;
Geert Uytterhoevend07e9c12014-08-29 15:13:21 +020089 const char *name;
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +020090 atomic_t sd_count; /* Number of subdomains with power "on" */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020091 enum gpd_status status; /* Current state of the domain */
Rafael J. Wysocki596ba342011-07-01 22:13:19 +020092 unsigned int device_count; /* Number of devices */
93 unsigned int suspended_count; /* System suspend device counter */
94 unsigned int prepared_count; /* Suspend counter of prepared devices */
Viresh Kumar42f62842017-10-12 15:07:23 +053095 unsigned int performance_state; /* Aggregated max performance state */
Rafael J. Wysockif7218892011-07-01 22:12:45 +020096 int (*power_off)(struct generic_pm_domain *domain);
97 int (*power_on)(struct generic_pm_domain *domain);
Viresh Kumar1067ae32018-11-02 11:18:08 +053098 struct opp_table *opp_table; /* OPP table of the genpd */
Viresh Kumar6e417662017-11-29 15:21:51 +053099 unsigned int (*opp_to_performance_state)(struct generic_pm_domain *genpd,
100 struct dev_pm_opp *opp);
Viresh Kumar42f62842017-10-12 15:07:23 +0530101 int (*set_performance_state)(struct generic_pm_domain *genpd,
102 unsigned int state);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100103 struct gpd_dev_ops dev_ops;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100104 s64 max_off_time_ns; /* Maximum allowed "suspended" time. */
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200105 bool max_off_time_changed;
106 bool cached_power_down_ok;
Ulf Hanssonc16561e2014-11-06 00:37:08 +0100107 int (*attach_dev)(struct generic_pm_domain *domain,
108 struct device *dev);
109 void (*detach_dev)(struct generic_pm_domain *domain,
110 struct device *dev);
Ulf Hanssonc11f6f52014-12-01 12:50:21 +0100111 unsigned int flags; /* Bit field of configs for genpd */
Lina Iyer59d65b72016-10-14 10:47:49 -0700112 struct genpd_power_state *states;
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100113 unsigned int state_count; /* number of states */
114 unsigned int state_idx; /* state that genpd will go to when off */
Lina Iyer59d65b72016-10-14 10:47:49 -0700115 void *free; /* Free the state that was allocated for default */
Thara Gopinathafece3a2017-07-14 13:10:15 -0400116 ktime_t on_time;
117 ktime_t accounting_time;
Lina Iyer35241d12016-10-14 10:47:54 -0700118 const struct genpd_lock_ops *lock_ops;
Lina Iyerd716f472016-10-14 10:47:55 -0700119 union {
120 struct mutex mlock;
121 struct {
122 spinlock_t slock;
123 unsigned long lock_flags;
124 };
125 };
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100126
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200127};
128
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200129static inline struct generic_pm_domain *pd_to_genpd(struct dev_pm_domain *pd)
130{
131 return container_of(pd, struct generic_pm_domain, domain);
132}
133
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200134struct gpd_link {
135 struct generic_pm_domain *master;
136 struct list_head master_node;
137 struct generic_pm_domain *slave;
138 struct list_head slave_node;
Viresh Kumar18edf492018-11-02 14:40:19 +0530139
140 /* Sub-domain's per-master domain performance state */
141 unsigned int performance_state;
142 unsigned int prev_performance_state;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200143};
144
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100145struct gpd_timing_data {
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200146 s64 suspend_latency_ns;
147 s64 resume_latency_ns;
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +0200148 s64 effective_constraint_ns;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200149 bool constraint_changed;
Ulf Hansson9df39212016-03-31 11:21:25 +0200150 bool cached_suspend_ok;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100151};
152
Ulf Hansson00e7c292014-11-14 08:41:32 +0100153struct pm_domain_data {
154 struct list_head list_node;
155 struct device *dev;
156};
157
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200158struct generic_pm_domain_data {
159 struct pm_domain_data base;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100160 struct gpd_timing_data td;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200161 struct notifier_block nb;
Viresh Kumar42f62842017-10-12 15:07:23 +0530162 unsigned int performance_state;
Dave Gerlacha5ea7a02017-04-04 08:51:29 -0700163 void *data;
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200164};
165
Guennadi Liakhovetski9b4f617b2012-02-04 22:26:49 +0100166#ifdef CONFIG_PM_GENERIC_DOMAINS
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200167static inline struct generic_pm_domain_data *to_gpd_data(struct pm_domain_data *pdd)
168{
169 return container_of(pdd, struct generic_pm_domain_data, base);
170}
171
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100172static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
173{
174 return to_gpd_data(dev->power.subsys_data->domain_data);
175}
176
Ulf Hansson1a7a6702018-05-29 12:04:14 +0200177int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev);
Ulf Hansson924f4482018-05-29 12:04:15 +0200178int pm_genpd_remove_device(struct device *dev);
Ulf Hansson781b9d62018-05-29 12:04:13 +0200179int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
180 struct generic_pm_domain *new_subdomain);
181int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
182 struct generic_pm_domain *target);
183int pm_genpd_init(struct generic_pm_domain *genpd,
184 struct dev_power_governor *gov, bool is_off);
185int pm_genpd_remove(struct generic_pm_domain *genpd);
186int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state);
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100187
Ulf Hanssonae3c5112014-09-03 12:52:31 +0200188extern struct dev_power_governor simple_qos_governor;
Mark Brown925b44a2011-12-08 23:27:28 +0100189extern struct dev_power_governor pm_domain_always_on_gov;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200190#else
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100191
Magnus Dammb6426312012-02-25 22:14:18 +0100192static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
193{
194 return ERR_PTR(-ENOSYS);
195}
Ulf Hansson1a7a6702018-05-29 12:04:14 +0200196static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
197 struct device *dev)
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100198{
199 return -ENOSYS;
200}
Ulf Hansson924f4482018-05-29 12:04:15 +0200201static inline int pm_genpd_remove_device(struct device *dev)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200202{
203 return -ENOSYS;
204}
205static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
206 struct generic_pm_domain *new_sd)
207{
208 return -ENOSYS;
209}
210static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
211 struct generic_pm_domain *target)
212{
213 return -ENOSYS;
214}
Ulf Hansson7eb231c2016-06-17 12:27:52 +0200215static inline int pm_genpd_init(struct generic_pm_domain *genpd,
216 struct dev_power_governor *gov, bool is_off)
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100217{
Ulf Hansson7eb231c2016-06-17 12:27:52 +0200218 return -ENOSYS;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100219}
Jon Hunter3fe57712016-09-12 12:01:13 +0100220static inline int pm_genpd_remove(struct generic_pm_domain *genpd)
221{
222 return -ENOTSUPP;
223}
Geert Uytterhoeven16d0bf12017-02-08 19:37:55 +0100224
Viresh Kumar42f62842017-10-12 15:07:23 +0530225static inline int dev_pm_genpd_set_performance_state(struct device *dev,
226 unsigned int state)
227{
228 return -ENOTSUPP;
229}
230
Geert Uytterhoeven16d0bf12017-02-08 19:37:55 +0100231#define simple_qos_governor (*(struct dev_power_governor *)(NULL))
232#define pm_domain_always_on_gov (*(struct dev_power_governor *)(NULL))
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200233#endif
234
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200235#ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
Ulf Hansson781b9d62018-05-29 12:04:13 +0200236void pm_genpd_syscore_poweroff(struct device *dev);
237void pm_genpd_syscore_poweron(struct device *dev);
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200238#else
Ulf Hanssond47e6462014-09-03 12:52:24 +0200239static inline void pm_genpd_syscore_poweroff(struct device *dev) {}
240static inline void pm_genpd_syscore_poweron(struct device *dev) {}
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200241#endif
242
Tomasz Figaaa422402014-09-19 20:27:36 +0200243/* OF PM domain providers */
244struct of_device_id;
245
Thierry Reding40845522017-03-29 18:34:50 +0200246typedef struct generic_pm_domain *(*genpd_xlate_t)(struct of_phandle_args *args,
247 void *data);
248
Tomasz Figaaa422402014-09-19 20:27:36 +0200249struct genpd_onecell_data {
250 struct generic_pm_domain **domains;
251 unsigned int num_domains;
Thierry Reding40845522017-03-29 18:34:50 +0200252 genpd_xlate_t xlate;
Tomasz Figaaa422402014-09-19 20:27:36 +0200253};
254
Tomasz Figaaa422402014-09-19 20:27:36 +0200255#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
Jon Hunter892ebdcc2016-09-12 12:01:09 +0100256int of_genpd_add_provider_simple(struct device_node *np,
257 struct generic_pm_domain *genpd);
258int of_genpd_add_provider_onecell(struct device_node *np,
259 struct genpd_onecell_data *data);
Tomasz Figaaa422402014-09-19 20:27:36 +0200260void of_genpd_del_provider(struct device_node *np);
Ulf Hansson781b9d62018-05-29 12:04:13 +0200261int of_genpd_add_device(struct of_phandle_args *args, struct device *dev);
262int of_genpd_add_subdomain(struct of_phandle_args *parent,
263 struct of_phandle_args *new_subdomain);
264struct generic_pm_domain *of_genpd_remove_last(struct device_node *np);
265int of_genpd_parse_idle_states(struct device_node *dn,
266 struct genpd_power_state **states, int *n);
Viresh Kumare38f89d2018-06-13 20:22:04 +0530267unsigned int pm_genpd_opp_to_performance_state(struct device *genpd_dev,
268 struct dev_pm_opp *opp);
Tomasz Figaaa422402014-09-19 20:27:36 +0200269
270int genpd_dev_pm_attach(struct device *dev);
Ulf Hansson3c095f32018-05-31 12:59:58 +0200271struct device *genpd_dev_pm_attach_by_id(struct device *dev,
272 unsigned int index);
Ulf Hansson5d6be702018-06-29 13:04:31 +0200273struct device *genpd_dev_pm_attach_by_name(struct device *dev,
Douglas Anderson7416f1f2019-02-14 10:12:48 -0800274 const char *name);
Tomasz Figaaa422402014-09-19 20:27:36 +0200275#else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
Jon Hunter892ebdcc2016-09-12 12:01:09 +0100276static inline int of_genpd_add_provider_simple(struct device_node *np,
277 struct generic_pm_domain *genpd)
Tomasz Figaaa422402014-09-19 20:27:36 +0200278{
Jon Hunter892ebdcc2016-09-12 12:01:09 +0100279 return -ENOTSUPP;
Tomasz Figaaa422402014-09-19 20:27:36 +0200280}
Tomasz Figaaa422402014-09-19 20:27:36 +0200281
Jon Hunter892ebdcc2016-09-12 12:01:09 +0100282static inline int of_genpd_add_provider_onecell(struct device_node *np,
283 struct genpd_onecell_data *data)
284{
285 return -ENOTSUPP;
286}
287
288static inline void of_genpd_del_provider(struct device_node *np) {}
Tomasz Figaaa422402014-09-19 20:27:36 +0200289
Jon Hunterec695722016-09-12 12:01:05 +0100290static inline int of_genpd_add_device(struct of_phandle_args *args,
291 struct device *dev)
292{
293 return -ENODEV;
294}
295
296static inline int of_genpd_add_subdomain(struct of_phandle_args *parent,
297 struct of_phandle_args *new_subdomain)
298{
299 return -ENODEV;
300}
301
Lina Iyer30f60422016-10-14 10:47:51 -0700302static inline int of_genpd_parse_idle_states(struct device_node *dn,
303 struct genpd_power_state **states, int *n)
304{
305 return -ENODEV;
306}
307
Viresh Kumar6e417662017-11-29 15:21:51 +0530308static inline unsigned int
Viresh Kumare38f89d2018-06-13 20:22:04 +0530309pm_genpd_opp_to_performance_state(struct device *genpd_dev,
310 struct dev_pm_opp *opp)
311{
312 return 0;
313}
314
Tomasz Figaaa422402014-09-19 20:27:36 +0200315static inline int genpd_dev_pm_attach(struct device *dev)
316{
Ulf Hansson919b7302018-05-09 12:17:52 +0200317 return 0;
Tomasz Figaaa422402014-09-19 20:27:36 +0200318}
Jon Hunter17926552016-09-12 12:01:14 +0100319
Ulf Hansson3c095f32018-05-31 12:59:58 +0200320static inline struct device *genpd_dev_pm_attach_by_id(struct device *dev,
321 unsigned int index)
322{
323 return NULL;
324}
325
Ulf Hansson5d6be702018-06-29 13:04:31 +0200326static inline struct device *genpd_dev_pm_attach_by_name(struct device *dev,
Douglas Anderson7416f1f2019-02-14 10:12:48 -0800327 const char *name)
Ulf Hansson5d6be702018-06-29 13:04:31 +0200328{
329 return NULL;
330}
331
Jon Hunter17926552016-09-12 12:01:14 +0100332static inline
333struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
334{
335 return ERR_PTR(-ENOTSUPP);
336}
Tomasz Figaaa422402014-09-19 20:27:36 +0200337#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
338
Ulf Hanssonf48c7672014-09-29 13:58:47 +0200339#ifdef CONFIG_PM
Ulf Hansson781b9d62018-05-29 12:04:13 +0200340int dev_pm_domain_attach(struct device *dev, bool power_on);
Ulf Hansson82e12d92018-05-31 12:59:59 +0200341struct device *dev_pm_domain_attach_by_id(struct device *dev,
342 unsigned int index);
Ulf Hansson27dceb82018-06-29 13:04:32 +0200343struct device *dev_pm_domain_attach_by_name(struct device *dev,
Douglas Andersoneeb35df2019-02-14 10:12:49 -0800344 const char *name);
Ulf Hansson781b9d62018-05-29 12:04:13 +0200345void dev_pm_domain_detach(struct device *dev, bool power_off);
346void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
Ulf Hanssonf48c7672014-09-29 13:58:47 +0200347#else
348static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
349{
Ulf Hansson919b7302018-05-09 12:17:52 +0200350 return 0;
Ulf Hanssonf48c7672014-09-29 13:58:47 +0200351}
Ulf Hansson82e12d92018-05-31 12:59:59 +0200352static inline struct device *dev_pm_domain_attach_by_id(struct device *dev,
353 unsigned int index)
354{
355 return NULL;
356}
Ulf Hansson27dceb82018-06-29 13:04:32 +0200357static inline struct device *dev_pm_domain_attach_by_name(struct device *dev,
Douglas Andersoneeb35df2019-02-14 10:12:49 -0800358 const char *name)
Ulf Hansson27dceb82018-06-29 13:04:32 +0200359{
360 return NULL;
361}
Ulf Hanssonf48c7672014-09-29 13:58:47 +0200362static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100363static inline void dev_pm_domain_set(struct device *dev,
364 struct dev_pm_domain *pd) {}
Ulf Hanssonf48c7672014-09-29 13:58:47 +0200365#endif
366
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200367#endif /* _LINUX_PM_DOMAIN_H */