1 /*
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>
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 
20 /* Defines used for the flags field in the struct generic_pm_domain */
21 #define GENPD_FLAG_PM_CLK	 (1U << 0) /* PM domain uses PM clk */
22 #define GENPD_FLAG_IRQ_SAFE	 (1U << 1) /* PM domain operates in atomic */
23 #define GENPD_FLAG_ALWAYS_ON	 (1U << 2) /* PM domain is always powered on */
24 #define GENPD_FLAG_ACTIVE_WAKEUP (1U << 3) /* Keep devices active if wakeup */
25 
26 enum gpd_status {
27 	GPD_STATE_ACTIVE = 0,	/* PM domain is active */
28 	GPD_STATE_POWER_OFF,	/* PM domain is off */
29 };
30 
31 struct dev_power_governor {
32 	bool (*power_down_ok)(struct dev_pm_domain *domain);
33 	bool (*suspend_ok)(struct device *dev);
34 };
35 
36 struct gpd_dev_ops {
37 	int (*start)(struct device *dev);
38 	int (*stop)(struct device *dev);
39 };
40 
41 struct genpd_power_state {
42 	s64 power_off_latency_ns;
43 	s64 power_on_latency_ns;
44 	s64 residency_ns;
45 	struct fwnode_handle *fwnode;
46 	ktime_t idle_time;
47 };
48 
49 struct genpd_lock_ops;
50 struct dev_pm_opp;
51 
52 struct generic_pm_domain {
53 	struct device dev;
54 	struct dev_pm_domain domain;	/* PM domain operations */
55 	struct list_head gpd_list_node;	/* Node in the global PM domains list */
56 	struct list_head master_links;	/* Links with PM domain as a master */
57 	struct list_head slave_links;	/* Links with PM domain as a slave */
58 	struct list_head dev_list;	/* List of devices */
59 	struct dev_power_governor *gov;
60 	struct work_struct power_off_work;
61 	struct fwnode_handle *provider;	/* Identity of the domain provider */
62 	bool has_provider;
63 	const char *name;
64 	atomic_t sd_count;	/* Number of subdomains with power "on" */
65 	enum gpd_status status;	/* Current state of the domain */
66 	unsigned int device_count;	/* Number of devices */
67 	unsigned int suspended_count;	/* System suspend device counter */
68 	unsigned int prepared_count;	/* Suspend counter of prepared devices */
69 	unsigned int performance_state;	/* Aggregated max performance state */
70 	int (*power_off)(struct generic_pm_domain *domain);
71 	int (*power_on)(struct generic_pm_domain *domain);
72 	unsigned int (*opp_to_performance_state)(struct generic_pm_domain *genpd,
73 						 struct dev_pm_opp *opp);
74 	int (*set_performance_state)(struct generic_pm_domain *genpd,
75 				     unsigned int state);
76 	struct gpd_dev_ops dev_ops;
77 	s64 max_off_time_ns;	/* Maximum allowed "suspended" time. */
78 	bool max_off_time_changed;
79 	bool cached_power_down_ok;
80 	int (*attach_dev)(struct generic_pm_domain *domain,
81 			  struct device *dev);
82 	void (*detach_dev)(struct generic_pm_domain *domain,
83 			   struct device *dev);
84 	unsigned int flags;		/* Bit field of configs for genpd */
85 	struct genpd_power_state *states;
86 	unsigned int state_count; /* number of states */
87 	unsigned int state_idx; /* state that genpd will go to when off */
88 	void *free; /* Free the state that was allocated for default */
89 	ktime_t on_time;
90 	ktime_t accounting_time;
91 	const struct genpd_lock_ops *lock_ops;
92 	union {
93 		struct mutex mlock;
94 		struct {
95 			spinlock_t slock;
96 			unsigned long lock_flags;
97 		};
98 	};
99 
100 };
101 
pd_to_genpd(struct dev_pm_domain * pd)102 static inline struct generic_pm_domain *pd_to_genpd(struct dev_pm_domain *pd)
103 {
104 	return container_of(pd, struct generic_pm_domain, domain);
105 }
106 
107 struct gpd_link {
108 	struct generic_pm_domain *master;
109 	struct list_head master_node;
110 	struct generic_pm_domain *slave;
111 	struct list_head slave_node;
112 };
113 
114 struct gpd_timing_data {
115 	s64 suspend_latency_ns;
116 	s64 resume_latency_ns;
117 	s64 effective_constraint_ns;
118 	bool constraint_changed;
119 	bool cached_suspend_ok;
120 };
121 
122 struct pm_domain_data {
123 	struct list_head list_node;
124 	struct device *dev;
125 };
126 
127 struct generic_pm_domain_data {
128 	struct pm_domain_data base;
129 	struct gpd_timing_data td;
130 	struct notifier_block nb;
131 	unsigned int performance_state;
132 	void *data;
133 };
134 
135 #ifdef CONFIG_PM_GENERIC_DOMAINS
to_gpd_data(struct pm_domain_data * pdd)136 static inline struct generic_pm_domain_data *to_gpd_data(struct pm_domain_data *pdd)
137 {
138 	return container_of(pdd, struct generic_pm_domain_data, base);
139 }
140 
dev_gpd_data(struct device * dev)141 static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
142 {
143 	return to_gpd_data(dev->power.subsys_data->domain_data);
144 }
145 
146 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev);
147 int pm_genpd_remove_device(struct device *dev);
148 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
149 			   struct generic_pm_domain *new_subdomain);
150 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
151 			      struct generic_pm_domain *target);
152 int pm_genpd_init(struct generic_pm_domain *genpd,
153 		  struct dev_power_governor *gov, bool is_off);
154 int pm_genpd_remove(struct generic_pm_domain *genpd);
155 int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state);
156 
157 extern struct dev_power_governor simple_qos_governor;
158 extern struct dev_power_governor pm_domain_always_on_gov;
159 #else
160 
dev_gpd_data(struct device * dev)161 static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
162 {
163 	return ERR_PTR(-ENOSYS);
164 }
pm_genpd_add_device(struct generic_pm_domain * genpd,struct device * dev)165 static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
166 				      struct device *dev)
167 {
168 	return -ENOSYS;
169 }
pm_genpd_remove_device(struct device * dev)170 static inline int pm_genpd_remove_device(struct device *dev)
171 {
172 	return -ENOSYS;
173 }
pm_genpd_add_subdomain(struct generic_pm_domain * genpd,struct generic_pm_domain * new_sd)174 static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
175 					 struct generic_pm_domain *new_sd)
176 {
177 	return -ENOSYS;
178 }
pm_genpd_remove_subdomain(struct generic_pm_domain * genpd,struct generic_pm_domain * target)179 static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
180 					    struct generic_pm_domain *target)
181 {
182 	return -ENOSYS;
183 }
pm_genpd_init(struct generic_pm_domain * genpd,struct dev_power_governor * gov,bool is_off)184 static inline int pm_genpd_init(struct generic_pm_domain *genpd,
185 				struct dev_power_governor *gov, bool is_off)
186 {
187 	return -ENOSYS;
188 }
pm_genpd_remove(struct generic_pm_domain * genpd)189 static inline int pm_genpd_remove(struct generic_pm_domain *genpd)
190 {
191 	return -ENOTSUPP;
192 }
193 
dev_pm_genpd_set_performance_state(struct device * dev,unsigned int state)194 static inline int dev_pm_genpd_set_performance_state(struct device *dev,
195 						     unsigned int state)
196 {
197 	return -ENOTSUPP;
198 }
199 
200 #define simple_qos_governor		(*(struct dev_power_governor *)(NULL))
201 #define pm_domain_always_on_gov		(*(struct dev_power_governor *)(NULL))
202 #endif
203 
204 #ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
205 void pm_genpd_syscore_poweroff(struct device *dev);
206 void pm_genpd_syscore_poweron(struct device *dev);
207 #else
pm_genpd_syscore_poweroff(struct device * dev)208 static inline void pm_genpd_syscore_poweroff(struct device *dev) {}
pm_genpd_syscore_poweron(struct device * dev)209 static inline void pm_genpd_syscore_poweron(struct device *dev) {}
210 #endif
211 
212 /* OF PM domain providers */
213 struct of_device_id;
214 
215 typedef struct generic_pm_domain *(*genpd_xlate_t)(struct of_phandle_args *args,
216 						   void *data);
217 
218 struct genpd_onecell_data {
219 	struct generic_pm_domain **domains;
220 	unsigned int num_domains;
221 	genpd_xlate_t xlate;
222 };
223 
224 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
225 int of_genpd_add_provider_simple(struct device_node *np,
226 				 struct generic_pm_domain *genpd);
227 int of_genpd_add_provider_onecell(struct device_node *np,
228 				  struct genpd_onecell_data *data);
229 void of_genpd_del_provider(struct device_node *np);
230 int of_genpd_add_device(struct of_phandle_args *args, struct device *dev);
231 int of_genpd_add_subdomain(struct of_phandle_args *parent,
232 			   struct of_phandle_args *new_subdomain);
233 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np);
234 int of_genpd_parse_idle_states(struct device_node *dn,
235 			       struct genpd_power_state **states, int *n);
236 unsigned int of_genpd_opp_to_performance_state(struct device *dev,
237 				struct device_node *np);
238 
239 int genpd_dev_pm_attach(struct device *dev);
240 struct device *genpd_dev_pm_attach_by_id(struct device *dev,
241 					 unsigned int index);
242 struct device *genpd_dev_pm_attach_by_name(struct device *dev,
243 					   char *name);
244 #else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
of_genpd_add_provider_simple(struct device_node * np,struct generic_pm_domain * genpd)245 static inline int of_genpd_add_provider_simple(struct device_node *np,
246 					struct generic_pm_domain *genpd)
247 {
248 	return -ENOTSUPP;
249 }
250 
of_genpd_add_provider_onecell(struct device_node * np,struct genpd_onecell_data * data)251 static inline int of_genpd_add_provider_onecell(struct device_node *np,
252 					struct genpd_onecell_data *data)
253 {
254 	return -ENOTSUPP;
255 }
256 
of_genpd_del_provider(struct device_node * np)257 static inline void of_genpd_del_provider(struct device_node *np) {}
258 
of_genpd_add_device(struct of_phandle_args * args,struct device * dev)259 static inline int of_genpd_add_device(struct of_phandle_args *args,
260 				      struct device *dev)
261 {
262 	return -ENODEV;
263 }
264 
of_genpd_add_subdomain(struct of_phandle_args * parent,struct of_phandle_args * new_subdomain)265 static inline int of_genpd_add_subdomain(struct of_phandle_args *parent,
266 					 struct of_phandle_args *new_subdomain)
267 {
268 	return -ENODEV;
269 }
270 
of_genpd_parse_idle_states(struct device_node * dn,struct genpd_power_state ** states,int * n)271 static inline int of_genpd_parse_idle_states(struct device_node *dn,
272 			struct genpd_power_state **states, int *n)
273 {
274 	return -ENODEV;
275 }
276 
277 static inline unsigned int
of_genpd_opp_to_performance_state(struct device * dev,struct device_node * np)278 of_genpd_opp_to_performance_state(struct device *dev,
279 				  struct device_node *np)
280 {
281 	return 0;
282 }
283 
genpd_dev_pm_attach(struct device * dev)284 static inline int genpd_dev_pm_attach(struct device *dev)
285 {
286 	return 0;
287 }
288 
genpd_dev_pm_attach_by_id(struct device * dev,unsigned int index)289 static inline struct device *genpd_dev_pm_attach_by_id(struct device *dev,
290 						       unsigned int index)
291 {
292 	return NULL;
293 }
294 
genpd_dev_pm_attach_by_name(struct device * dev,char * name)295 static inline struct device *genpd_dev_pm_attach_by_name(struct device *dev,
296 							 char *name)
297 {
298 	return NULL;
299 }
300 
301 static inline
of_genpd_remove_last(struct device_node * np)302 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
303 {
304 	return ERR_PTR(-ENOTSUPP);
305 }
306 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
307 
308 #ifdef CONFIG_PM
309 int dev_pm_domain_attach(struct device *dev, bool power_on);
310 struct device *dev_pm_domain_attach_by_id(struct device *dev,
311 					  unsigned int index);
312 struct device *dev_pm_domain_attach_by_name(struct device *dev,
313 					    char *name);
314 void dev_pm_domain_detach(struct device *dev, bool power_off);
315 void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
316 #else
dev_pm_domain_attach(struct device * dev,bool power_on)317 static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
318 {
319 	return 0;
320 }
dev_pm_domain_attach_by_id(struct device * dev,unsigned int index)321 static inline struct device *dev_pm_domain_attach_by_id(struct device *dev,
322 							unsigned int index)
323 {
324 	return NULL;
325 }
dev_pm_domain_attach_by_name(struct device * dev,char * name)326 static inline struct device *dev_pm_domain_attach_by_name(struct device *dev,
327 							  char *name)
328 {
329 	return NULL;
330 }
dev_pm_domain_detach(struct device * dev,bool power_off)331 static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
dev_pm_domain_set(struct device * dev,struct dev_pm_domain * pd)332 static inline void dev_pm_domain_set(struct device *dev,
333 				     struct dev_pm_domain *pd) {}
334 #endif
335 
336 #endif /* _LINUX_PM_DOMAIN_H */
337