summaryrefslogtreecommitdiff
path: root/include/linux/pm_qos.h
blob: c5e4f6ff7ac62661a851419b99dd1a508119c5a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#ifndef _LINUX_PM_QOS_H
#define _LINUX_PM_QOS_H
/* interface for the pm_qos_power infrastructure of the linux kernel.
 *
 * Mark Gross <mgross@linux.intel.com>
 *
 * Support added for bounded constraints by Sai Gurrappadi
 * <sgurrappad@nvidia.com>
 *
 * Copyright (c) 2013-2014, NVIDIA CORPORATION. All rights reserved.
 */
#include <linux/plist.h>
#include <linux/notifier.h>
#include <linux/miscdevice.h>
#include <linux/device.h>
#include <linux/workqueue.h>

enum {
	PM_QOS_RESERVED = 0,
	PM_QOS_CPU_DMA_LATENCY,
	PM_QOS_NETWORK_LATENCY,
	PM_QOS_NETWORK_THROUGHPUT,
	PM_QOS_MIN_ONLINE_CPUS,
	PM_QOS_MAX_ONLINE_CPUS,
	PM_QOS_CPU_FREQ_MIN,
	PM_QOS_CPU_FREQ_MAX,
	PM_QOS_GPU_FREQ_MIN,
	PM_QOS_GPU_FREQ_MAX,
	PM_QOS_EMC_FREQ_MIN,

	/* insert new class ID */

	PM_QOS_NUM_CLASSES,
};

/**
 * enum pm_qos_bounded_classes - Class ID's for bounded constraints
 *
 * Each class wraps around a corresponding min and max pm qos node
 * and binds the two constraints in one.
 */
enum pm_qos_bounded_classes {
	PM_QOS_RESERVED_BOUNDS = 0,
	PM_QOS_CPU_FREQ_BOUNDS,	/* requests should be in KHz to not exceed s32*/
	PM_QOS_GPU_FREQ_BOUNDS,	/* requests should be in KHz to not exceed s32*/
	PM_QOS_ONLINE_CPUS_BOUNDS,
	/* insert new bounded class ids here */
	PM_QOS_NUM_BOUNDED_CLASSES,
};

enum pm_qos_flags_status {
	PM_QOS_FLAGS_UNDEFINED = -1,
	PM_QOS_FLAGS_NONE,
	PM_QOS_FLAGS_SOME,
	PM_QOS_FLAGS_ALL,
};

#define PM_QOS_DEFAULT_VALUE -1

#define PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE	(2000 * USEC_PER_SEC)
#define PM_QOS_NETWORK_LAT_DEFAULT_VALUE	(2000 * USEC_PER_SEC)
#define PM_QOS_NETWORK_THROUGHPUT_DEFAULT_VALUE	0
#define PM_QOS_MIN_ONLINE_CPUS_DEFAULT_VALUE	0
#define PM_QOS_MAX_ONLINE_CPUS_DEFAULT_VALUE	INT_MAX
#define PM_QOS_CPU_FREQ_MIN_DEFAULT_VALUE	0
#define PM_QOS_CPU_FREQ_MAX_DEFAULT_VALUE	INT_MAX
#define PM_QOS_GPU_FREQ_MIN_DEFAULT_VALUE	0
#define PM_QOS_GPU_FREQ_MAX_DEFAULT_VALUE	INT_MAX
#define PM_QOS_EMC_FREQ_MIN_DEFAULT_VALUE	0
#define PM_QOS_EMC_FREQ_MAX_DEFAULT_VALUE	INT_MAX
#define PM_QOS_DEV_LAT_DEFAULT_VALUE		0

#define PM_QOS_FLAG_NO_POWER_OFF	(1 << 0)
#define PM_QOS_FLAG_REMOTE_WAKEUP	(1 << 1)

struct pm_qos_request {
	struct plist_node node;
	int pm_qos_class;
	int priority;
	struct delayed_work work; /* for pm_qos_update_request_timeout */
};

struct pm_qos_flags_request {
	struct list_head node;
	s32 flags;	/* Do not change to 64 bit */
};

enum dev_pm_qos_req_type {
	DEV_PM_QOS_LATENCY = 1,
	DEV_PM_QOS_FLAGS,
};

struct dev_pm_qos_request {
	enum dev_pm_qos_req_type type;
	union {
		struct plist_node pnode;
		struct pm_qos_flags_request flr;
	} data;
	struct device *dev;
	struct delayed_work work; /* for pm_qos_update_request_timeout */
};

enum pm_qos_type {
	PM_QOS_UNITIALIZED,
	PM_QOS_MAX,		/* return the largest value */
	PM_QOS_MIN		/* return the smallest value */
};

/**
 * enum pm_qos_bound_priority - priority value of the given bound request
 *
 * Kernel space clients can request any priority level; priorities
 * PM_QOS_PRIO_TRUSTED and higher should be used when the client wants to
 * ensure that no userspace client can override it's request
 *
 * Userspace clients can request any priority as high as PM_QOS_PRIO_UNTRUSTED
 * Default userspace request which don't have any priority specified will have
 * PM_QOS_PRIO_DEFAULT_UNTRUSTED priority.
 *
 * General rule of thumb - request as low (numerically larger) a priority
 * as you can
 */
enum pm_qos_bound_priority {
	/* kernel clients */
	PM_QOS_PRIO_HIGHEST = 0,
	PM_QOS_PRIO_TRUSTED = 10,
	/* userspace clients */
	PM_QOS_PRIO_UNTRUSTED,
	PM_QOS_PRIO_DEFAULT_UNTRUSTED = 50,
	PM_QOS_NUM_PRIO = 100,
};

/**
 * struct pm_qos_prio - priority node for bounded constraints
 * @max_list: List of upper bound requests
 * @min_list: list of lower bound requests
 * @node: node to queue in the list of priorities
 */
struct pm_qos_prio {
	struct plist_head max_list;
	struct plist_head min_list;
	struct plist_node node;
};

/**
 * struct pm_qos_bounded_constraint - binds two pm_qos_constraints
 * @prio_list: list of priorities
 * @max_class: Class id of the upper bound
 * @min_class: Class id of the lower bound
 * @min_wins: Pick min if min > max
 *
 * Requests are added at their corresponding priority level. Target
 * values for a bounded constraint will be the intersection of all the
 * (min, max) ranges specified by each priority level. If the intersection
 * is null at any priority level, the higher priority level's requests are
 * honoured.
 */
struct pm_qos_bounded_constraint {
	struct plist_head prio_list;
	int max_class;
	int min_class;
	bool min_wins;
};

/*
 * Note: The lockless read path depends on the CPU accessing target_value
 * or effective_flags atomically.  Atomic access is only guaranteed on all CPU
 * types linux supports for 32 bit quantites
 */
struct pm_qos_constraints {
	struct plist_head list;
	s32 target_value;	/* Do not change to 64 bit */
	s32 default_value;
	enum pm_qos_type type;
	struct blocking_notifier_head *notifiers;
	int parent_class;
};

struct pm_qos_flags {
	struct list_head list;
	s32 effective_flags;	/* Do not change to 64 bit */
	struct blocking_notifier_head *notifiers;
};

struct dev_pm_qos {
	struct pm_qos_constraints latency;
	struct pm_qos_flags flags;
	struct dev_pm_qos_request *latency_req;
	struct dev_pm_qos_request *flags_req;
};

/* Action requested to pm_qos_update_target */
enum pm_qos_req_action {
	PM_QOS_ADD_REQ,		/* Add a new request */
	PM_QOS_UPDATE_REQ,	/* Update an existing request */
	PM_QOS_REMOVE_REQ	/* Remove an existing request */
};

static inline int dev_pm_qos_request_active(struct dev_pm_qos_request *req)
{
	return req->dev != NULL;
}

int pm_qos_update_target(struct pm_qos_constraints *c, struct plist_node *node,
			 enum pm_qos_req_action action, int value);
bool pm_qos_update_flags(struct pm_qos_flags *pqf,
			 struct pm_qos_flags_request *req,
			 enum pm_qos_req_action action, s32 val);
void pm_qos_add_request(struct pm_qos_request *req, int pm_qos_class,
			s32 value);
void pm_qos_update_request(struct pm_qos_request *req,
			   s32 new_value);
void pm_qos_update_request_timeout(struct pm_qos_request *req,
				   s32 new_value, unsigned long timeout_us);
void pm_qos_remove_request(struct pm_qos_request *req);

/* Interface for bounded constraints */
void pm_qos_add_min_bound_req(struct pm_qos_request *req, int priority,
			      int pm_qos_bounded_class, s32 val);
void pm_qos_add_max_bound_req(struct pm_qos_request *req, int priority,
			      int pm_qos_bounded_class, s32 val);
void pm_qos_update_bounded_req(struct pm_qos_request *req, int priority,
			       s32 val);
void pm_qos_update_bounded_req_timeout(struct pm_qos_request *req,
				       unsigned long timeout_us);
void pm_qos_remove_bounded_req(struct pm_qos_request *req);
void pm_qos_add_min_notifier(int pm_qos_bounded_class,
			     struct notifier_block *notifer);
void pm_qos_add_max_notifier(int pm_qos_bounded_class,
			     struct notifier_block *notifier);
void pm_qos_remove_min_notifier(int pm_qos_bounded_class,
				struct notifier_block *notifier);
void pm_qos_remove_max_notifier(int pm_qos_bounded_class,
				struct notifier_block *notifier);
s32 pm_qos_read_min_bound(int pm_qos_bounded_class);
s32 pm_qos_read_max_bound(int pm_qos_bounded_class);

int pm_qos_request(int pm_qos_class);
int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier);
int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier);
int pm_qos_request_active(struct pm_qos_request *req);
s32 pm_qos_read_value(struct pm_qos_constraints *c);

#ifdef CONFIG_PM
enum pm_qos_flags_status __dev_pm_qos_flags(struct device *dev, s32 mask);
enum pm_qos_flags_status dev_pm_qos_flags(struct device *dev, s32 mask);
s32 __dev_pm_qos_read_value(struct device *dev);
s32 dev_pm_qos_read_value(struct device *dev);
int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
			   enum dev_pm_qos_req_type type, s32 value);
int dev_pm_qos_update_request(struct dev_pm_qos_request *req, s32 new_value);
int dev_pm_qos_update_request_timeout(struct dev_pm_qos_request *req,
				      s32 new_value,
				      unsigned long timeout_us);
int dev_pm_qos_remove_request(struct dev_pm_qos_request *req);
int dev_pm_qos_add_notifier(struct device *dev,
			    struct notifier_block *notifier);
int dev_pm_qos_remove_notifier(struct device *dev,
			       struct notifier_block *notifier);
int dev_pm_qos_add_global_notifier(struct notifier_block *notifier);
int dev_pm_qos_remove_global_notifier(struct notifier_block *notifier);
void dev_pm_qos_constraints_init(struct device *dev);
void dev_pm_qos_constraints_destroy(struct device *dev);
int dev_pm_qos_add_ancestor_request(struct device *dev,
				    struct dev_pm_qos_request *req, s32 value);
#else
static inline enum pm_qos_flags_status __dev_pm_qos_flags(struct device *dev,
							  s32 mask)
			{ return PM_QOS_FLAGS_UNDEFINED; }
static inline enum pm_qos_flags_status dev_pm_qos_flags(struct device *dev,
							s32 mask)
			{ return PM_QOS_FLAGS_UNDEFINED; }
static inline s32 __dev_pm_qos_read_value(struct device *dev)
			{ return 0; }
static inline s32 dev_pm_qos_read_value(struct device *dev)
			{ return 0; }
static inline int dev_pm_qos_add_request(struct device *dev,
					 struct dev_pm_qos_request *req,
					 enum dev_pm_qos_req_type type,
					 s32 value)
			{ return 0; }
static inline int dev_pm_qos_update_request(struct dev_pm_qos_request *req,
					    s32 new_value)
			{ return 0; }
static inline int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
			{ return 0; }
static inline int dev_pm_qos_add_notifier(struct device *dev,
					  struct notifier_block *notifier)
			{ return 0; }
static inline int dev_pm_qos_remove_notifier(struct device *dev,
					     struct notifier_block *notifier)
			{ return 0; }
static inline int dev_pm_qos_add_global_notifier(
					struct notifier_block *notifier)
			{ return 0; }
static inline int dev_pm_qos_remove_global_notifier(
					struct notifier_block *notifier)
			{ return 0; }
static inline void dev_pm_qos_constraints_init(struct device *dev)
{
	dev->power.power_state = PMSG_ON;
}
static inline void dev_pm_qos_constraints_destroy(struct device *dev)
{
	dev->power.power_state = PMSG_INVALID;
}
static inline int dev_pm_qos_add_ancestor_request(struct device *dev,
				    struct dev_pm_qos_request *req, s32 value)
			{ return 0; }
#endif

#ifdef CONFIG_PM_RUNTIME
int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value);
void dev_pm_qos_hide_latency_limit(struct device *dev);
int dev_pm_qos_expose_flags(struct device *dev, s32 value);
void dev_pm_qos_hide_flags(struct device *dev);
int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set);

static inline s32 dev_pm_qos_requested_latency(struct device *dev)
{
	return dev->power.qos->latency_req->data.pnode.prio;
}

static inline s32 dev_pm_qos_requested_flags(struct device *dev)
{
	return dev->power.qos->flags_req->data.flr.flags;
}
#else
static inline int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value)
			{ return 0; }
static inline void dev_pm_qos_hide_latency_limit(struct device *dev) {}
static inline int dev_pm_qos_expose_flags(struct device *dev, s32 value)
			{ return 0; }
static inline void dev_pm_qos_hide_flags(struct device *dev) {}
static inline int dev_pm_qos_update_flags(struct device *dev, s32 m, bool set)
			{ return 0; }

static inline s32 dev_pm_qos_requested_latency(struct device *dev) { return 0; }
static inline s32 dev_pm_qos_requested_flags(struct device *dev) { return 0; }
#endif

#endif