aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/pm_qos_params.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/pm_qos_params.c')
-rw-r--r--kernel/pm_qos_params.c526
1 files changed, 526 insertions, 0 deletions
diff --git a/kernel/pm_qos_params.c b/kernel/pm_qos_params.c
new file mode 100644
index 00000000000..82da7ac3b1f
--- /dev/null
+++ b/kernel/pm_qos_params.c
@@ -0,0 +1,526 @@
1/*
2 * This module exposes the interface to kernel space for specifying
3 * QoS dependencies. It provides infrastructure for registration of:
4 *
5 * Dependents on a QoS value : register requests
6 * Watchers of QoS value : get notified when target QoS value changes
7 *
8 * This QoS design is best effort based. Dependents register their QoS needs.
9 * Watchers register to keep track of the current QoS needs of the system.
10 *
11 * There are 3 basic classes of QoS parameter: latency, timeout, throughput
12 * each have defined units:
13 * latency: usec
14 * timeout: usec <-- currently not used.
15 * throughput: kbs (kilo byte / sec)
16 *
17 * There are lists of pm_qos_objects each one wrapping requests, notifiers
18 *
19 * User mode requests on a QOS parameter register themselves to the
20 * subsystem by opening the device node /dev/... and writing there request to
21 * the node. As long as the process holds a file handle open to the node the
22 * client continues to be accounted for. Upon file release the usermode
23 * request is removed and a new qos target is computed. This way when the
24 * request that the application has is cleaned up when closes the file
25 * pointer or exits the pm_qos_object will get an opportunity to clean up.
26 *
27 * Mark Gross <mgross@linux.intel.com>
28 */
29
30/*#define DEBUG*/
31
32#include <linux/pm_qos_params.h>
33#include <linux/sched.h>
34#include <linux/spinlock.h>
35#include <linux/slab.h>
36#include <linux/time.h>
37#include <linux/fs.h>
38#include <linux/device.h>
39#include <linux/miscdevice.h>
40#include <linux/string.h>
41#include <linux/platform_device.h>
42#include <linux/init.h>
43#include <linux/kernel.h>
44
45#include <linux/uaccess.h>
46
47/*
48 * locking rule: all changes to requests or notifiers lists
49 * or pm_qos_object list and pm_qos_objects need to happen with pm_qos_lock
50 * held, taken with _irqsave. One lock to rule them all
51 */
52enum pm_qos_type {
53 PM_QOS_MAX, /* return the largest value */
54 PM_QOS_MIN /* return the smallest value */
55};
56
57/*
58 * Note: The lockless read path depends on the CPU accessing
59 * target_value atomically. Atomic access is only guaranteed on all CPU
60 * types linux supports for 32 bit quantites
61 */
62struct pm_qos_object {
63 struct plist_head requests;
64 struct blocking_notifier_head *notifiers;
65 struct miscdevice pm_qos_power_miscdev;
66 char *name;
67 s32 target_value; /* Do not change to 64 bit */
68 s32 default_value;
69 enum pm_qos_type type;
70};
71
72static DEFINE_SPINLOCK(pm_qos_lock);
73
74static struct pm_qos_object null_pm_qos;
75static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
76static struct pm_qos_object cpu_dma_pm_qos = {
77 .requests = PLIST_HEAD_INIT(cpu_dma_pm_qos.requests),
78 .notifiers = &cpu_dma_lat_notifier,
79 .name = "cpu_dma_latency",
80 .target_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE,
81 .default_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE,
82 .type = PM_QOS_MIN,
83};
84
85static BLOCKING_NOTIFIER_HEAD(network_lat_notifier);
86static struct pm_qos_object network_lat_pm_qos = {
87 .requests = PLIST_HEAD_INIT(network_lat_pm_qos.requests),
88 .notifiers = &network_lat_notifier,
89 .name = "network_latency",
90 .target_value = PM_QOS_NETWORK_LAT_DEFAULT_VALUE,
91 .default_value = PM_QOS_NETWORK_LAT_DEFAULT_VALUE,
92 .type = PM_QOS_MIN
93};
94
95
96static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier);
97static struct pm_qos_object network_throughput_pm_qos = {
98 .requests = PLIST_HEAD_INIT(network_throughput_pm_qos.requests),
99 .notifiers = &network_throughput_notifier,
100 .name = "network_throughput",
101 .target_value = PM_QOS_NETWORK_THROUGHPUT_DEFAULT_VALUE,
102 .default_value = PM_QOS_NETWORK_THROUGHPUT_DEFAULT_VALUE,
103 .type = PM_QOS_MAX,
104};
105
106
107static BLOCKING_NOTIFIER_HEAD(min_online_cpus_notifier);
108static struct pm_qos_object min_online_cpus_pm_qos = {
109 .requests = PLIST_HEAD_INIT(min_online_cpus_pm_qos.requests),
110 .notifiers = &min_online_cpus_notifier,
111 .name = "min_online_cpus",
112 .target_value = PM_QOS_MIN_ONLINE_CPUS_DEFAULT_VALUE,
113 .default_value = PM_QOS_MIN_ONLINE_CPUS_DEFAULT_VALUE,
114 .type = PM_QOS_MAX,
115};
116
117
118static BLOCKING_NOTIFIER_HEAD(max_online_cpus_notifier);
119static struct pm_qos_object max_online_cpus_pm_qos = {
120 .requests = PLIST_HEAD_INIT(max_online_cpus_pm_qos.requests),
121 .notifiers = &max_online_cpus_notifier,
122 .name = "max_online_cpus",
123 .target_value = PM_QOS_MAX_ONLINE_CPUS_DEFAULT_VALUE,
124 .default_value = PM_QOS_MAX_ONLINE_CPUS_DEFAULT_VALUE,
125 .type = PM_QOS_MIN,
126};
127
128
129static BLOCKING_NOTIFIER_HEAD(cpu_freq_min_notifier);
130static struct pm_qos_object cpu_freq_min_pm_qos = {
131 .requests = PLIST_HEAD_INIT(cpu_freq_min_pm_qos.requests),
132 .notifiers = &cpu_freq_min_notifier,
133 .name = "cpu_freq_min",
134 .target_value = PM_QOS_CPU_FREQ_MIN_DEFAULT_VALUE,
135 .default_value = PM_QOS_CPU_FREQ_MIN_DEFAULT_VALUE,
136 .type = PM_QOS_MAX,
137};
138
139
140static BLOCKING_NOTIFIER_HEAD(cpu_freq_max_notifier);
141static struct pm_qos_object cpu_freq_max_pm_qos = {
142 .requests = PLIST_HEAD_INIT(cpu_freq_max_pm_qos.requests),
143 .notifiers = &cpu_freq_max_notifier,
144 .name = "cpu_freq_max",
145 .target_value = PM_QOS_CPU_FREQ_MAX_DEFAULT_VALUE,
146 .default_value = PM_QOS_CPU_FREQ_MAX_DEFAULT_VALUE,
147 .type = PM_QOS_MIN,
148};
149
150
151static struct pm_qos_object *pm_qos_array[] = {
152 &null_pm_qos,
153 &cpu_dma_pm_qos,
154 &network_lat_pm_qos,
155 &network_throughput_pm_qos,
156 &min_online_cpus_pm_qos,
157 &max_online_cpus_pm_qos,
158 &cpu_freq_min_pm_qos,
159 &cpu_freq_max_pm_qos
160};
161
162static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
163 size_t count, loff_t *f_pos);
164static ssize_t pm_qos_power_read(struct file *filp, char __user *buf,
165 size_t count, loff_t *f_pos);
166static int pm_qos_power_open(struct inode *inode, struct file *filp);
167static int pm_qos_power_release(struct inode *inode, struct file *filp);
168
169static const struct file_operations pm_qos_power_fops = {
170 .write = pm_qos_power_write,
171 .read = pm_qos_power_read,
172 .open = pm_qos_power_open,
173 .release = pm_qos_power_release,
174 .llseek = noop_llseek,
175};
176
177/* unlocked internal variant */
178static inline int pm_qos_get_value(struct pm_qos_object *o)
179{
180 if (plist_head_empty(&o->requests))
181 return o->default_value;
182
183 switch (o->type) {
184 case PM_QOS_MIN:
185 return plist_first(&o->requests)->prio;
186
187 case PM_QOS_MAX:
188 return plist_last(&o->requests)->prio;
189
190 default:
191 /* runtime check for not using enum */
192 BUG();
193 }
194}
195
196static inline s32 pm_qos_read_value(struct pm_qos_object *o)
197{
198 return o->target_value;
199}
200
201static inline void pm_qos_set_value(struct pm_qos_object *o, s32 value)
202{
203 o->target_value = value;
204}
205
206static void update_target(struct pm_qos_object *o, struct plist_node *node,
207 int del, int value)
208{
209 unsigned long flags;
210 int prev_value, curr_value;
211
212 spin_lock_irqsave(&pm_qos_lock, flags);
213 prev_value = pm_qos_get_value(o);
214 /* PM_QOS_DEFAULT_VALUE is a signal that the value is unchanged */
215 if (value != PM_QOS_DEFAULT_VALUE) {
216 /*
217 * to change the list, we atomically remove, reinit
218 * with new value and add, then see if the extremal
219 * changed
220 */
221 plist_del(node, &o->requests);
222 plist_node_init(node, value);
223 plist_add(node, &o->requests);
224 } else if (del) {
225 plist_del(node, &o->requests);
226 } else {
227 plist_add(node, &o->requests);
228 }
229 curr_value = pm_qos_get_value(o);
230 pm_qos_set_value(o, curr_value);
231 spin_unlock_irqrestore(&pm_qos_lock, flags);
232
233 if (prev_value != curr_value)
234 blocking_notifier_call_chain(o->notifiers,
235 (unsigned long)curr_value,
236 NULL);
237}
238
239static int register_pm_qos_misc(struct pm_qos_object *qos)
240{
241 qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR;
242 qos->pm_qos_power_miscdev.name = qos->name;
243 qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops;
244
245 return misc_register(&qos->pm_qos_power_miscdev);
246}
247
248static int find_pm_qos_object_by_minor(int minor)
249{
250 int pm_qos_class;
251
252 for (pm_qos_class = 0;
253 pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) {
254 if (minor ==
255 pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor)
256 return pm_qos_class;
257 }
258 return -1;
259}
260
261/**
262 * pm_qos_request - returns current system wide qos expectation
263 * @pm_qos_class: identification of which qos value is requested
264 *
265 * This function returns the current target value.
266 */
267int pm_qos_request(int pm_qos_class)
268{
269 return pm_qos_read_value(pm_qos_array[pm_qos_class]);
270}
271EXPORT_SYMBOL_GPL(pm_qos_request);
272
273int pm_qos_request_active(struct pm_qos_request_list *req)
274{
275 return req->pm_qos_class != 0;
276}
277EXPORT_SYMBOL_GPL(pm_qos_request_active);
278
279/**
280 * pm_qos_add_request - inserts new qos request into the list
281 * @dep: pointer to a preallocated handle
282 * @pm_qos_class: identifies which list of qos request to use
283 * @value: defines the qos request
284 *
285 * This function inserts a new entry in the pm_qos_class list of requested qos
286 * performance characteristics. It recomputes the aggregate QoS expectations
287 * for the pm_qos_class of parameters and initializes the pm_qos_request_list
288 * handle. Caller needs to save this handle for later use in updates and
289 * removal.
290 */
291
292void pm_qos_add_request(struct pm_qos_request_list *dep,
293 int pm_qos_class, s32 value)
294{
295 struct pm_qos_object *o = pm_qos_array[pm_qos_class];
296 int new_value;
297
298 if (pm_qos_request_active(dep)) {
299 WARN(1, KERN_ERR "pm_qos_add_request() called for already added request\n");
300 return;
301 }
302 if (value == PM_QOS_DEFAULT_VALUE)
303 new_value = o->default_value;
304 else
305 new_value = value;
306 plist_node_init(&dep->list, new_value);
307 dep->pm_qos_class = pm_qos_class;
308 update_target(o, &dep->list, 0, PM_QOS_DEFAULT_VALUE);
309}
310EXPORT_SYMBOL_GPL(pm_qos_add_request);
311
312/**
313 * pm_qos_update_request - modifies an existing qos request
314 * @pm_qos_req : handle to list element holding a pm_qos request to use
315 * @value: defines the qos request
316 *
317 * Updates an existing qos request for the pm_qos_class of parameters along
318 * with updating the target pm_qos_class value.
319 *
320 * Attempts are made to make this code callable on hot code paths.
321 */
322void pm_qos_update_request(struct pm_qos_request_list *pm_qos_req,
323 s32 new_value)
324{
325 s32 temp;
326 struct pm_qos_object *o;
327
328 if (!pm_qos_req) /*guard against callers passing in null */
329 return;
330
331 if (!pm_qos_request_active(pm_qos_req)) {
332 WARN(1, KERN_ERR "pm_qos_update_request() called for unknown object\n");
333 return;
334 }
335
336 o = pm_qos_array[pm_qos_req->pm_qos_class];
337
338 if (new_value == PM_QOS_DEFAULT_VALUE)
339 temp = o->default_value;
340 else
341 temp = new_value;
342
343 if (temp != pm_qos_req->list.prio)
344 update_target(o, &pm_qos_req->list, 0, temp);
345}
346EXPORT_SYMBOL_GPL(pm_qos_update_request);
347
348/**
349 * pm_qos_remove_request - modifies an existing qos request
350 * @pm_qos_req: handle to request list element
351 *
352 * Will remove pm qos request from the list of requests and
353 * recompute the current target value for the pm_qos_class. Call this
354 * on slow code paths.
355 */
356void pm_qos_remove_request(struct pm_qos_request_list *pm_qos_req)
357{
358 struct pm_qos_object *o;
359
360 if (pm_qos_req == NULL)
361 return;
362 /* silent return to keep pcm code cleaner */
363
364 if (!pm_qos_request_active(pm_qos_req)) {
365 WARN(1, KERN_ERR "pm_qos_remove_request() called for unknown object\n");
366 return;
367 }
368
369 o = pm_qos_array[pm_qos_req->pm_qos_class];
370 update_target(o, &pm_qos_req->list, 1, PM_QOS_DEFAULT_VALUE);
371 memset(pm_qos_req, 0, sizeof(*pm_qos_req));
372}
373EXPORT_SYMBOL_GPL(pm_qos_remove_request);
374
375/**
376 * pm_qos_add_notifier - sets notification entry for changes to target value
377 * @pm_qos_class: identifies which qos target changes should be notified.
378 * @notifier: notifier block managed by caller.
379 *
380 * will register the notifier into a notification chain that gets called
381 * upon changes to the pm_qos_class target value.
382 */
383int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
384{
385 int retval;
386
387 retval = blocking_notifier_chain_register(
388 pm_qos_array[pm_qos_class]->notifiers, notifier);
389
390 return retval;
391}
392EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
393
394/**
395 * pm_qos_remove_notifier - deletes notification entry from chain.
396 * @pm_qos_class: identifies which qos target changes are notified.
397 * @notifier: notifier block to be removed.
398 *
399 * will remove the notifier from the notification chain that gets called
400 * upon changes to the pm_qos_class target value.
401 */
402int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
403{
404 int retval;
405
406 retval = blocking_notifier_chain_unregister(
407 pm_qos_array[pm_qos_class]->notifiers, notifier);
408
409 return retval;
410}
411EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
412
413static int pm_qos_power_open(struct inode *inode, struct file *filp)
414{
415 long pm_qos_class;
416
417 pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
418 if (pm_qos_class >= 0) {
419 struct pm_qos_request_list *req = kzalloc(sizeof(*req), GFP_KERNEL);
420 if (!req)
421 return -ENOMEM;
422
423 pm_qos_add_request(req, pm_qos_class, PM_QOS_DEFAULT_VALUE);
424 filp->private_data = req;
425
426 if (filp->private_data)
427 return 0;
428 }
429 return -EPERM;
430}
431
432static int pm_qos_power_release(struct inode *inode, struct file *filp)
433{
434 struct pm_qos_request_list *req;
435
436 req = filp->private_data;
437 pm_qos_remove_request(req);
438 kfree(req);
439
440 return 0;
441}
442
443
444static ssize_t pm_qos_power_read(struct file *filp, char __user *buf,
445 size_t count, loff_t *f_pos)
446{
447 s32 value;
448 unsigned long flags;
449 struct pm_qos_object *o;
450 struct pm_qos_request_list *pm_qos_req = filp->private_data;
451
452 if (!pm_qos_req)
453 return -EINVAL;
454 if (!pm_qos_request_active(pm_qos_req))
455 return -EINVAL;
456
457 o = pm_qos_array[pm_qos_req->pm_qos_class];
458 spin_lock_irqsave(&pm_qos_lock, flags);
459 value = pm_qos_get_value(o);
460 spin_unlock_irqrestore(&pm_qos_lock, flags);
461
462 return simple_read_from_buffer(buf, count, f_pos, &value, sizeof(s32));
463}
464
465static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
466 size_t count, loff_t *f_pos)
467{
468 s32 value;
469 struct pm_qos_request_list *pm_qos_req;
470
471 if (count == sizeof(s32)) {
472 if (copy_from_user(&value, buf, sizeof(s32)))
473 return -EFAULT;
474 } else if (count <= 11) { /* ASCII perhaps? */
475 char ascii_value[11];
476 unsigned long int ulval;
477 int ret;
478
479 if (copy_from_user(ascii_value, buf, count))
480 return -EFAULT;
481
482 if (count > 10) {
483 if (ascii_value[10] == '\n')
484 ascii_value[10] = '\0';
485 else
486 return -EINVAL;
487 } else {
488 ascii_value[count] = '\0';
489 }
490 ret = strict_strtoul(ascii_value, 16, &ulval);
491 if (ret) {
492 pr_debug("%s, 0x%lx, 0x%x\n", ascii_value, ulval, ret);
493 return -EINVAL;
494 }
495 value = (s32)lower_32_bits(ulval);
496 } else {
497 return -EINVAL;
498 }
499
500 pm_qos_req = filp->private_data;
501 pm_qos_update_request(pm_qos_req, value);
502
503 return count;
504}
505
506
507static int __init pm_qos_power_init(void)
508{
509 int ret = 0;
510 int i;
511
512 BUILD_BUG_ON(ARRAY_SIZE(pm_qos_array) != PM_QOS_NUM_CLASSES);
513
514 for (i = 1; i < PM_QOS_NUM_CLASSES; i++) {
515 ret = register_pm_qos_misc(pm_qos_array[i]);
516 if (ret < 0) {
517 printk(KERN_ERR "pm_qos_param: %s setup failed\n",
518 pm_qos_array[i]->name);
519 return ret;
520 }
521 }
522
523 return ret;
524}
525
526late_initcall(pm_qos_power_init);