pm_domain.h 11.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * pm_domain.h - Definitions and headers related to device power domains.
 *
 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
 *
 * This file is released under the GPLv2.
 */

#ifndef _LINUX_PM_DOMAIN_H
#define _LINUX_PM_DOMAIN_H

#include <linux/device.h>
13 14
#include <linux/mutex.h>
#include <linux/pm.h>
15
#include <linux/err.h>
T
Thomas Abraham 已提交
16
#include <linux/of.h>
17
#include <linux/notifier.h>
18
#include <linux/spinlock.h>
19
#include <linux/cpumask.h>
20

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/*
 * Flags to control the behaviour of a genpd.
 *
 * These flags may be set in the struct generic_pm_domain's flags field by a
 * genpd backend driver. The flags must be set before it calls pm_genpd_init(),
 * which initializes a genpd.
 *
 * GENPD_FLAG_PM_CLK:		Instructs genpd to use the PM clk framework,
 *				while powering on/off attached devices.
 *
 * GENPD_FLAG_IRQ_SAFE:		This informs genpd that its backend callbacks,
 *				->power_on|off(), doesn't sleep. Hence, these
 *				can be invoked from within atomic context, which
 *				enables genpd to power on/off the PM domain,
 *				even when pm_runtime_is_irq_safe() returns true,
 *				for any of its attached devices. Note that, a
 *				genpd having this flag set, requires its
 *				masterdomains to also have it set.
 *
 * GENPD_FLAG_ALWAYS_ON:	Instructs genpd to always keep the PM domain
 *				powered on.
 *
 * GENPD_FLAG_ACTIVE_WAKEUP:	Instructs genpd to keep the PM domain powered
 *				on, in case any of its attached devices is used
 *				in the wakeup path to serve system wakeups.
46 47 48 49 50 51 52 53 54 55
 *
 * GENPD_FLAG_CPU_DOMAIN:	Instructs genpd that it should expect to get
 *				devices attached, which may belong to CPUs or
 *				possibly have subdomains with CPUs attached.
 *				This flag enables the genpd backend driver to
 *				deploy idle power management support for CPUs
 *				and groups of CPUs. Note that, the backend
 *				driver must then comply with the so called,
 *				last-man-standing algorithm, for the CPUs in the
 *				PM domain.
56 57 58 59 60
 */
#define GENPD_FLAG_PM_CLK	 (1U << 0)
#define GENPD_FLAG_IRQ_SAFE	 (1U << 1)
#define GENPD_FLAG_ALWAYS_ON	 (1U << 2)
#define GENPD_FLAG_ACTIVE_WAKEUP (1U << 3)
61
#define GENPD_FLAG_CPU_DOMAIN	 (1U << 4)
62

63 64 65 66
enum gpd_status {
	GPD_STATE_ACTIVE = 0,	/* PM domain is active */
	GPD_STATE_POWER_OFF,	/* PM domain is off */
};
67

68 69
struct dev_power_governor {
	bool (*power_down_ok)(struct dev_pm_domain *domain);
70
	bool (*suspend_ok)(struct device *dev);
71 72
};

73 74 75 76 77
struct gpd_dev_ops {
	int (*start)(struct device *dev);
	int (*stop)(struct device *dev);
};

78 79 80
struct genpd_power_state {
	s64 power_off_latency_ns;
	s64 power_on_latency_ns;
81
	s64 residency_ns;
82
	struct fwnode_handle *fwnode;
83
	ktime_t idle_time;
84
	void *data;
85 86
};

L
Lina Iyer 已提交
87
struct genpd_lock_ops;
88
struct dev_pm_opp;
89
struct opp_table;
L
Lina Iyer 已提交
90

91
struct generic_pm_domain {
92
	struct device dev;
93
	struct dev_pm_domain domain;	/* PM domain operations */
94
	struct list_head gpd_list_node;	/* Node in the global PM domains list */
95 96
	struct list_head master_links;	/* Links with PM domain as a master */
	struct list_head slave_links;	/* Links with PM domain as a slave */
97 98 99
	struct list_head dev_list;	/* List of devices */
	struct dev_power_governor *gov;
	struct work_struct power_off_work;
100 101
	struct fwnode_handle *provider;	/* Identity of the domain provider */
	bool has_provider;
102
	const char *name;
103
	atomic_t sd_count;	/* Number of subdomains with power "on" */
104
	enum gpd_status status;	/* Current state of the domain */
105 106 107
	unsigned int device_count;	/* Number of devices */
	unsigned int suspended_count;	/* System suspend device counter */
	unsigned int prepared_count;	/* Suspend counter of prepared devices */
108
	unsigned int performance_state;	/* Aggregated max performance state */
109
	cpumask_var_t cpus;		/* A cpumask of the attached CPUs */
110 111
	int (*power_off)(struct generic_pm_domain *domain);
	int (*power_on)(struct generic_pm_domain *domain);
112
	struct opp_table *opp_table;	/* OPP table of the genpd */
113 114
	unsigned int (*opp_to_performance_state)(struct generic_pm_domain *genpd,
						 struct dev_pm_opp *opp);
115 116
	int (*set_performance_state)(struct generic_pm_domain *genpd,
				     unsigned int state);
117
	struct gpd_dev_ops dev_ops;
118
	s64 max_off_time_ns;	/* Maximum allowed "suspended" time. */
119 120
	bool max_off_time_changed;
	bool cached_power_down_ok;
121
	bool cached_power_down_state_idx;
122 123 124 125
	int (*attach_dev)(struct generic_pm_domain *domain,
			  struct device *dev);
	void (*detach_dev)(struct generic_pm_domain *domain,
			   struct device *dev);
126
	unsigned int flags;		/* Bit field of configs for genpd */
127
	struct genpd_power_state *states;
128 129
	void (*free_states)(struct genpd_power_state *states,
			    unsigned int state_count);
130 131
	unsigned int state_count; /* number of states */
	unsigned int state_idx; /* state that genpd will go to when off */
132 133
	ktime_t on_time;
	ktime_t accounting_time;
L
Lina Iyer 已提交
134
	const struct genpd_lock_ops *lock_ops;
135 136 137 138 139 140 141
	union {
		struct mutex mlock;
		struct {
			spinlock_t slock;
			unsigned long lock_flags;
		};
	};
142

143 144
};

145 146 147 148 149
static inline struct generic_pm_domain *pd_to_genpd(struct dev_pm_domain *pd)
{
	return container_of(pd, struct generic_pm_domain, domain);
}

150 151 152 153 154
struct gpd_link {
	struct generic_pm_domain *master;
	struct list_head master_node;
	struct generic_pm_domain *slave;
	struct list_head slave_node;
155 156 157 158

	/* Sub-domain's per-master domain performance state */
	unsigned int performance_state;
	unsigned int prev_performance_state;
159 160
};

161
struct gpd_timing_data {
162 163
	s64 suspend_latency_ns;
	s64 resume_latency_ns;
164
	s64 effective_constraint_ns;
165
	bool constraint_changed;
166
	bool cached_suspend_ok;
167 168
};

169 170 171 172 173
struct pm_domain_data {
	struct list_head list_node;
	struct device *dev;
};

174 175
struct generic_pm_domain_data {
	struct pm_domain_data base;
176
	struct gpd_timing_data td;
177
	struct notifier_block nb;
178
	unsigned int performance_state;
179
	void *data;
180 181
};

182
#ifdef CONFIG_PM_GENERIC_DOMAINS
183 184 185 186 187
static inline struct generic_pm_domain_data *to_gpd_data(struct pm_domain_data *pdd)
{
	return container_of(pdd, struct generic_pm_domain_data, base);
}

188 189 190 191 192
static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
{
	return to_gpd_data(dev->power.subsys_data->domain_data);
}

193
int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev);
194
int pm_genpd_remove_device(struct device *dev);
195 196 197 198 199 200 201 202
int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
			   struct generic_pm_domain *new_subdomain);
int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
			      struct generic_pm_domain *target);
int pm_genpd_init(struct generic_pm_domain *genpd,
		  struct dev_power_governor *gov, bool is_off);
int pm_genpd_remove(struct generic_pm_domain *genpd);
int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state);
203

204
extern struct dev_power_governor simple_qos_governor;
205
extern struct dev_power_governor pm_domain_always_on_gov;
206 207 208
#ifdef CONFIG_CPU_IDLE
extern struct dev_power_governor pm_domain_cpu_gov;
#endif
209
#else
210

211 212 213 214
static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
{
	return ERR_PTR(-ENOSYS);
}
215 216
static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
				      struct device *dev)
217 218 219
{
	return -ENOSYS;
}
220
static inline int pm_genpd_remove_device(struct device *dev)
221 222 223 224 225 226 227 228 229 230 231 232 233
{
	return -ENOSYS;
}
static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
					 struct generic_pm_domain *new_sd)
{
	return -ENOSYS;
}
static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
					    struct generic_pm_domain *target)
{
	return -ENOSYS;
}
234 235
static inline int pm_genpd_init(struct generic_pm_domain *genpd,
				struct dev_power_governor *gov, bool is_off)
236
{
237
	return -ENOSYS;
238
}
239 240 241 242
static inline int pm_genpd_remove(struct generic_pm_domain *genpd)
{
	return -ENOTSUPP;
}
243

244 245 246 247 248 249
static inline int dev_pm_genpd_set_performance_state(struct device *dev,
						     unsigned int state)
{
	return -ENOTSUPP;
}

250 251
#define simple_qos_governor		(*(struct dev_power_governor *)(NULL))
#define pm_domain_always_on_gov		(*(struct dev_power_governor *)(NULL))
252 253
#endif

254
#ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
255 256
void pm_genpd_syscore_poweroff(struct device *dev);
void pm_genpd_syscore_poweron(struct device *dev);
257
#else
258 259
static inline void pm_genpd_syscore_poweroff(struct device *dev) {}
static inline void pm_genpd_syscore_poweron(struct device *dev) {}
260 261
#endif

262 263 264
/* OF PM domain providers */
struct of_device_id;

265 266 267
typedef struct generic_pm_domain *(*genpd_xlate_t)(struct of_phandle_args *args,
						   void *data);

268 269 270
struct genpd_onecell_data {
	struct generic_pm_domain **domains;
	unsigned int num_domains;
271
	genpd_xlate_t xlate;
272 273 274
};

#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
275 276 277 278
int of_genpd_add_provider_simple(struct device_node *np,
				 struct generic_pm_domain *genpd);
int of_genpd_add_provider_onecell(struct device_node *np,
				  struct genpd_onecell_data *data);
279
void of_genpd_del_provider(struct device_node *np);
280 281 282 283 284 285
int of_genpd_add_device(struct of_phandle_args *args, struct device *dev);
int of_genpd_add_subdomain(struct of_phandle_args *parent,
			   struct of_phandle_args *new_subdomain);
struct generic_pm_domain *of_genpd_remove_last(struct device_node *np);
int of_genpd_parse_idle_states(struct device_node *dn,
			       struct genpd_power_state **states, int *n);
286 287
unsigned int pm_genpd_opp_to_performance_state(struct device *genpd_dev,
					       struct dev_pm_opp *opp);
288 289

int genpd_dev_pm_attach(struct device *dev);
290 291
struct device *genpd_dev_pm_attach_by_id(struct device *dev,
					 unsigned int index);
292
struct device *genpd_dev_pm_attach_by_name(struct device *dev,
293
					   const char *name);
294
#else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
295 296
static inline int of_genpd_add_provider_simple(struct device_node *np,
					struct generic_pm_domain *genpd)
297
{
298 299 300 301 302 303 304
	return -ENOTSUPP;
}

static inline int of_genpd_add_provider_onecell(struct device_node *np,
					struct genpd_onecell_data *data)
{
	return -ENOTSUPP;
305 306
}

307
static inline void of_genpd_del_provider(struct device_node *np) {}
308

309 310 311 312 313 314 315 316 317 318 319 320
static inline int of_genpd_add_device(struct of_phandle_args *args,
				      struct device *dev)
{
	return -ENODEV;
}

static inline int of_genpd_add_subdomain(struct of_phandle_args *parent,
					 struct of_phandle_args *new_subdomain)
{
	return -ENODEV;
}

321 322 323 324 325 326
static inline int of_genpd_parse_idle_states(struct device_node *dn,
			struct genpd_power_state **states, int *n)
{
	return -ENODEV;
}

327 328 329 330 331 332 333
static inline unsigned int
pm_genpd_opp_to_performance_state(struct device *genpd_dev,
				  struct dev_pm_opp *opp)
{
	return 0;
}

334 335
static inline int genpd_dev_pm_attach(struct device *dev)
{
336
	return 0;
337
}
338

339 340 341 342 343 344
static inline struct device *genpd_dev_pm_attach_by_id(struct device *dev,
						       unsigned int index)
{
	return NULL;
}

345
static inline struct device *genpd_dev_pm_attach_by_name(struct device *dev,
346
							 const char *name)
347 348 349 350
{
	return NULL;
}

351 352 353 354 355
static inline
struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
{
	return ERR_PTR(-ENOTSUPP);
}
356 357
#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */

358
#ifdef CONFIG_PM
359
int dev_pm_domain_attach(struct device *dev, bool power_on);
360 361
struct device *dev_pm_domain_attach_by_id(struct device *dev,
					  unsigned int index);
362
struct device *dev_pm_domain_attach_by_name(struct device *dev,
363
					    const char *name);
364 365
void dev_pm_domain_detach(struct device *dev, bool power_off);
void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
366 367 368
#else
static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
{
369
	return 0;
370
}
371 372 373 374 375
static inline struct device *dev_pm_domain_attach_by_id(struct device *dev,
							unsigned int index)
{
	return NULL;
}
376
static inline struct device *dev_pm_domain_attach_by_name(struct device *dev,
377
							  const char *name)
378 379 380
{
	return NULL;
}
381
static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
382 383
static inline void dev_pm_domain_set(struct device *dev,
				     struct dev_pm_domain *pd) {}
384 385
#endif

386
#endif /* _LINUX_PM_DOMAIN_H */