blob: f42d3f737a3326a1a66751ba6589fb4d26b6c925 [file] [log] [blame]
Mark Grossd82b3512008-02-04 22:30:08 -08001/*
2 * This module exposes the interface to kernel space for specifying
3 * QoS dependencies. It provides infrastructure for registration of:
4 *
Mark Grossed771342010-05-06 01:59:26 +02005 * Dependents on a QoS value : register requests
Mark Grossd82b3512008-02-04 22:30:08 -08006 * 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 *
Mark Grossed771342010-05-06 01:59:26 +020017 * There are lists of pm_qos_objects each one wrapping requests, notifiers
Mark Grossd82b3512008-02-04 22:30:08 -080018 *
Mark Grossed771342010-05-06 01:59:26 +020019 * User mode requests on a QOS parameter register themselves to the
Mark Grossd82b3512008-02-04 22:30:08 -080020 * 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
Mark Grossed771342010-05-06 01:59:26 +020023 * 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
Mark Grossd82b3512008-02-04 22:30:08 -080025 * pointer or exits the pm_qos_object will get an opportunity to clean up.
26 *
Richard Hughesbf1db692008-08-05 13:01:35 -070027 * Mark Gross <mgross@linux.intel.com>
Mark Grossd82b3512008-02-04 22:30:08 -080028 */
29
Mark Grossed771342010-05-06 01:59:26 +020030/*#define DEBUG*/
31
Mark Grossd82b3512008-02-04 22:30:08 -080032#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
44#include <linux/uaccess.h>
45
46/*
Mark Grossed771342010-05-06 01:59:26 +020047 * locking rule: all changes to requests or notifiers lists
Mark Grossd82b3512008-02-04 22:30:08 -080048 * or pm_qos_object list and pm_qos_objects need to happen with pm_qos_lock
49 * held, taken with _irqsave. One lock to rule them all
50 */
Mark Grossed771342010-05-06 01:59:26 +020051struct pm_qos_request_list {
Mark Grossd82b3512008-02-04 22:30:08 -080052 struct list_head list;
53 union {
54 s32 value;
55 s32 usec;
56 s32 kbps;
57 };
Mark Grossed771342010-05-06 01:59:26 +020058 int pm_qos_class;
Mark Grossd82b3512008-02-04 22:30:08 -080059};
60
61static s32 max_compare(s32 v1, s32 v2);
62static s32 min_compare(s32 v1, s32 v2);
63
64struct pm_qos_object {
Mark Grossed771342010-05-06 01:59:26 +020065 struct pm_qos_request_list requests;
Mark Grossd82b3512008-02-04 22:30:08 -080066 struct blocking_notifier_head *notifiers;
67 struct miscdevice pm_qos_power_miscdev;
68 char *name;
69 s32 default_value;
John Kacur9d359352008-09-02 14:36:13 -070070 atomic_t target_value;
Mark Grossd82b3512008-02-04 22:30:08 -080071 s32 (*comparitor)(s32, s32);
72};
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 = {
Mark Grossed771342010-05-06 01:59:26 +020077 .requests = {LIST_HEAD_INIT(cpu_dma_pm_qos.requests.list)},
Mark Grossd82b3512008-02-04 22:30:08 -080078 .notifiers = &cpu_dma_lat_notifier,
79 .name = "cpu_dma_latency",
80 .default_value = 2000 * USEC_PER_SEC,
John Kacur9d359352008-09-02 14:36:13 -070081 .target_value = ATOMIC_INIT(2000 * USEC_PER_SEC),
Mark Grossd82b3512008-02-04 22:30:08 -080082 .comparitor = min_compare
83};
84
85static BLOCKING_NOTIFIER_HEAD(network_lat_notifier);
86static struct pm_qos_object network_lat_pm_qos = {
Mark Grossed771342010-05-06 01:59:26 +020087 .requests = {LIST_HEAD_INIT(network_lat_pm_qos.requests.list)},
Mark Grossd82b3512008-02-04 22:30:08 -080088 .notifiers = &network_lat_notifier,
89 .name = "network_latency",
90 .default_value = 2000 * USEC_PER_SEC,
John Kacur9d359352008-09-02 14:36:13 -070091 .target_value = ATOMIC_INIT(2000 * USEC_PER_SEC),
Mark Grossd82b3512008-02-04 22:30:08 -080092 .comparitor = min_compare
93};
94
95
96static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier);
97static struct pm_qos_object network_throughput_pm_qos = {
Mark Grossed771342010-05-06 01:59:26 +020098 .requests = {LIST_HEAD_INIT(network_throughput_pm_qos.requests.list)},
Mark Grossd82b3512008-02-04 22:30:08 -080099 .notifiers = &network_throughput_notifier,
100 .name = "network_throughput",
101 .default_value = 0,
John Kacur9d359352008-09-02 14:36:13 -0700102 .target_value = ATOMIC_INIT(0),
Mark Grossd82b3512008-02-04 22:30:08 -0800103 .comparitor = max_compare
104};
105
106
107static struct pm_qos_object *pm_qos_array[] = {
108 &null_pm_qos,
109 &cpu_dma_pm_qos,
110 &network_lat_pm_qos,
111 &network_throughput_pm_qos
112};
113
114static DEFINE_SPINLOCK(pm_qos_lock);
115
116static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
117 size_t count, loff_t *f_pos);
118static int pm_qos_power_open(struct inode *inode, struct file *filp);
119static int pm_qos_power_release(struct inode *inode, struct file *filp);
120
121static const struct file_operations pm_qos_power_fops = {
122 .write = pm_qos_power_write,
123 .open = pm_qos_power_open,
124 .release = pm_qos_power_release,
125};
126
127/* static helper functions */
128static s32 max_compare(s32 v1, s32 v2)
129{
130 return max(v1, v2);
131}
132
133static s32 min_compare(s32 v1, s32 v2)
134{
135 return min(v1, v2);
136}
137
138
Mark Grossed771342010-05-06 01:59:26 +0200139static void update_target(int pm_qos_class)
Mark Grossd82b3512008-02-04 22:30:08 -0800140{
141 s32 extreme_value;
Mark Grossed771342010-05-06 01:59:26 +0200142 struct pm_qos_request_list *node;
Mark Grossd82b3512008-02-04 22:30:08 -0800143 unsigned long flags;
144 int call_notifier = 0;
145
146 spin_lock_irqsave(&pm_qos_lock, flags);
Mark Grossed771342010-05-06 01:59:26 +0200147 extreme_value = pm_qos_array[pm_qos_class]->default_value;
Mark Grossd82b3512008-02-04 22:30:08 -0800148 list_for_each_entry(node,
Mark Grossed771342010-05-06 01:59:26 +0200149 &pm_qos_array[pm_qos_class]->requests.list, list) {
150 extreme_value = pm_qos_array[pm_qos_class]->comparitor(
Mark Grossd82b3512008-02-04 22:30:08 -0800151 extreme_value, node->value);
152 }
Mark Grossed771342010-05-06 01:59:26 +0200153 if (atomic_read(&pm_qos_array[pm_qos_class]->target_value) !=
154 extreme_value) {
Mark Grossd82b3512008-02-04 22:30:08 -0800155 call_notifier = 1;
Mark Grossed771342010-05-06 01:59:26 +0200156 atomic_set(&pm_qos_array[pm_qos_class]->target_value,
157 extreme_value);
158 pr_debug(KERN_ERR "new target for qos %d is %d\n", pm_qos_class,
159 atomic_read(&pm_qos_array[pm_qos_class]->target_value));
Mark Grossd82b3512008-02-04 22:30:08 -0800160 }
161 spin_unlock_irqrestore(&pm_qos_lock, flags);
162
163 if (call_notifier)
Mark Grossed771342010-05-06 01:59:26 +0200164 blocking_notifier_call_chain(
165 pm_qos_array[pm_qos_class]->notifiers,
166 (unsigned long) extreme_value, NULL);
Mark Grossd82b3512008-02-04 22:30:08 -0800167}
168
169static int register_pm_qos_misc(struct pm_qos_object *qos)
170{
171 qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR;
172 qos->pm_qos_power_miscdev.name = qos->name;
173 qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops;
174
175 return misc_register(&qos->pm_qos_power_miscdev);
176}
177
178static int find_pm_qos_object_by_minor(int minor)
179{
180 int pm_qos_class;
181
182 for (pm_qos_class = 0;
183 pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) {
184 if (minor ==
185 pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor)
186 return pm_qos_class;
187 }
188 return -1;
189}
190
191/**
Mark Grossed771342010-05-06 01:59:26 +0200192 * pm_qos_request - returns current system wide qos expectation
Mark Grossd82b3512008-02-04 22:30:08 -0800193 * @pm_qos_class: identification of which qos value is requested
194 *
195 * This function returns the current target value in an atomic manner.
196 */
Mark Grossed771342010-05-06 01:59:26 +0200197int pm_qos_request(int pm_qos_class)
Mark Grossd82b3512008-02-04 22:30:08 -0800198{
John Kacur9d359352008-09-02 14:36:13 -0700199 return atomic_read(&pm_qos_array[pm_qos_class]->target_value);
Mark Grossd82b3512008-02-04 22:30:08 -0800200}
Mark Grossed771342010-05-06 01:59:26 +0200201EXPORT_SYMBOL_GPL(pm_qos_request);
Mark Grossd82b3512008-02-04 22:30:08 -0800202
203/**
Mark Grossed771342010-05-06 01:59:26 +0200204 * pm_qos_add_request - inserts new qos request into the list
Mark Grossd82b3512008-02-04 22:30:08 -0800205 * @pm_qos_class: identifies which list of qos request to us
Mark Grossd82b3512008-02-04 22:30:08 -0800206 * @value: defines the qos request
207 *
208 * This function inserts a new entry in the pm_qos_class list of requested qos
Richard Hughesbf1db692008-08-05 13:01:35 -0700209 * performance characteristics. It recomputes the aggregate QoS expectations
Mark Grossed771342010-05-06 01:59:26 +0200210 * for the pm_qos_class of parameters, and returns the pm_qos_request list
211 * element as a handle for use in updating and removal. Call needs to save
212 * this handle for later use.
Mark Grossd82b3512008-02-04 22:30:08 -0800213 */
Mark Grossed771342010-05-06 01:59:26 +0200214struct pm_qos_request_list *pm_qos_add_request(int pm_qos_class, s32 value)
Mark Grossd82b3512008-02-04 22:30:08 -0800215{
Mark Grossed771342010-05-06 01:59:26 +0200216 struct pm_qos_request_list *dep;
Mark Grossd82b3512008-02-04 22:30:08 -0800217 unsigned long flags;
218
Mark Grossed771342010-05-06 01:59:26 +0200219 dep = kzalloc(sizeof(struct pm_qos_request_list), GFP_KERNEL);
Mark Grossd82b3512008-02-04 22:30:08 -0800220 if (dep) {
221 if (value == PM_QOS_DEFAULT_VALUE)
222 dep->value = pm_qos_array[pm_qos_class]->default_value;
223 else
224 dep->value = value;
Mark Grossed771342010-05-06 01:59:26 +0200225 dep->pm_qos_class = pm_qos_class;
Mark Grossd82b3512008-02-04 22:30:08 -0800226
227 spin_lock_irqsave(&pm_qos_lock, flags);
228 list_add(&dep->list,
Mark Grossed771342010-05-06 01:59:26 +0200229 &pm_qos_array[pm_qos_class]->requests.list);
Mark Grossd82b3512008-02-04 22:30:08 -0800230 spin_unlock_irqrestore(&pm_qos_lock, flags);
231 update_target(pm_qos_class);
Mark Grossd82b3512008-02-04 22:30:08 -0800232 }
233
Mark Grossed771342010-05-06 01:59:26 +0200234 return dep;
Mark Grossd82b3512008-02-04 22:30:08 -0800235}
Mark Grossed771342010-05-06 01:59:26 +0200236EXPORT_SYMBOL_GPL(pm_qos_add_request);
Mark Grossd82b3512008-02-04 22:30:08 -0800237
238/**
Mark Grossed771342010-05-06 01:59:26 +0200239 * pm_qos_update_request - modifies an existing qos request
240 * @pm_qos_req : handle to list element holding a pm_qos request to use
Mark Grossd82b3512008-02-04 22:30:08 -0800241 * @value: defines the qos request
242 *
Mark Grossed771342010-05-06 01:59:26 +0200243 * Updates an existing qos request for the pm_qos_class of parameters along
Mark Grossd82b3512008-02-04 22:30:08 -0800244 * with updating the target pm_qos_class value.
245 *
Mark Grossed771342010-05-06 01:59:26 +0200246 * Attempts are made to make this code callable on hot code paths.
Mark Grossd82b3512008-02-04 22:30:08 -0800247 */
Mark Grossed771342010-05-06 01:59:26 +0200248void pm_qos_update_request(struct pm_qos_request_list *pm_qos_req,
249 s32 new_value)
Mark Grossd82b3512008-02-04 22:30:08 -0800250{
251 unsigned long flags;
Mark Grossd82b3512008-02-04 22:30:08 -0800252 int pending_update = 0;
Mark Grossed771342010-05-06 01:59:26 +0200253 s32 temp;
Mark Grossd82b3512008-02-04 22:30:08 -0800254
Mark Gross25f3a5a2010-05-17 00:21:03 +0200255 if (pm_qos_req) { /*guard against callers passing in null */
256 spin_lock_irqsave(&pm_qos_lock, flags);
257 if (new_value == PM_QOS_DEFAULT_VALUE)
258 temp = pm_qos_array[pm_qos_req->pm_qos_class]->default_value;
259 else
260 temp = new_value;
Mark Grossed771342010-05-06 01:59:26 +0200261
Mark Gross25f3a5a2010-05-17 00:21:03 +0200262 if (temp != pm_qos_req->value) {
263 pending_update = 1;
264 pm_qos_req->value = temp;
265 }
266 spin_unlock_irqrestore(&pm_qos_lock, flags);
267 if (pending_update)
268 update_target(pm_qos_req->pm_qos_class);
Mark Grossd82b3512008-02-04 22:30:08 -0800269 }
Mark Grossd82b3512008-02-04 22:30:08 -0800270}
Mark Grossed771342010-05-06 01:59:26 +0200271EXPORT_SYMBOL_GPL(pm_qos_update_request);
Mark Grossd82b3512008-02-04 22:30:08 -0800272
273/**
Mark Grossed771342010-05-06 01:59:26 +0200274 * pm_qos_remove_request - modifies an existing qos request
275 * @pm_qos_req: handle to request list element
Mark Grossd82b3512008-02-04 22:30:08 -0800276 *
Mark Grossed771342010-05-06 01:59:26 +0200277 * Will remove pm qos request from the list of requests and
278 * recompute the current target value for the pm_qos_class. Call this
279 * on slow code paths.
Mark Grossd82b3512008-02-04 22:30:08 -0800280 */
Mark Grossed771342010-05-06 01:59:26 +0200281void pm_qos_remove_request(struct pm_qos_request_list *pm_qos_req)
Mark Grossd82b3512008-02-04 22:30:08 -0800282{
283 unsigned long flags;
Mark Grossed771342010-05-06 01:59:26 +0200284 int qos_class;
Mark Grossd82b3512008-02-04 22:30:08 -0800285
Mark Grossed771342010-05-06 01:59:26 +0200286 if (pm_qos_req == NULL)
287 return;
288 /* silent return to keep pcm code cleaner */
289
290 qos_class = pm_qos_req->pm_qos_class;
Mark Grossd82b3512008-02-04 22:30:08 -0800291 spin_lock_irqsave(&pm_qos_lock, flags);
Mark Grossed771342010-05-06 01:59:26 +0200292 list_del(&pm_qos_req->list);
293 kfree(pm_qos_req);
Mark Grossd82b3512008-02-04 22:30:08 -0800294 spin_unlock_irqrestore(&pm_qos_lock, flags);
Mark Grossed771342010-05-06 01:59:26 +0200295 update_target(qos_class);
Mark Grossd82b3512008-02-04 22:30:08 -0800296}
Mark Grossed771342010-05-06 01:59:26 +0200297EXPORT_SYMBOL_GPL(pm_qos_remove_request);
Mark Grossd82b3512008-02-04 22:30:08 -0800298
299/**
300 * pm_qos_add_notifier - sets notification entry for changes to target value
301 * @pm_qos_class: identifies which qos target changes should be notified.
302 * @notifier: notifier block managed by caller.
303 *
304 * will register the notifier into a notification chain that gets called
Richard Hughesbf1db692008-08-05 13:01:35 -0700305 * upon changes to the pm_qos_class target value.
Mark Grossd82b3512008-02-04 22:30:08 -0800306 */
Mark Grossed771342010-05-06 01:59:26 +0200307int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
Mark Grossd82b3512008-02-04 22:30:08 -0800308{
309 int retval;
310
311 retval = blocking_notifier_chain_register(
312 pm_qos_array[pm_qos_class]->notifiers, notifier);
313
314 return retval;
315}
316EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
317
318/**
319 * pm_qos_remove_notifier - deletes notification entry from chain.
320 * @pm_qos_class: identifies which qos target changes are notified.
321 * @notifier: notifier block to be removed.
322 *
323 * will remove the notifier from the notification chain that gets called
Richard Hughesbf1db692008-08-05 13:01:35 -0700324 * upon changes to the pm_qos_class target value.
Mark Grossd82b3512008-02-04 22:30:08 -0800325 */
326int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
327{
328 int retval;
329
330 retval = blocking_notifier_chain_unregister(
331 pm_qos_array[pm_qos_class]->notifiers, notifier);
332
333 return retval;
334}
335EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
336
Mark Grossd82b3512008-02-04 22:30:08 -0800337static int pm_qos_power_open(struct inode *inode, struct file *filp)
338{
Mark Grossd82b3512008-02-04 22:30:08 -0800339 long pm_qos_class;
340
341 pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
342 if (pm_qos_class >= 0) {
Mark Grossed771342010-05-06 01:59:26 +0200343 filp->private_data = (void *) pm_qos_add_request(pm_qos_class,
344 PM_QOS_DEFAULT_VALUE);
345
346 if (filp->private_data)
Mark Grossd82b3512008-02-04 22:30:08 -0800347 return 0;
348 }
Mark Grossd82b3512008-02-04 22:30:08 -0800349 return -EPERM;
350}
351
352static int pm_qos_power_release(struct inode *inode, struct file *filp)
353{
Mark Grossed771342010-05-06 01:59:26 +0200354 struct pm_qos_request_list *req;
Mark Grossd82b3512008-02-04 22:30:08 -0800355
Mark Grossed771342010-05-06 01:59:26 +0200356 req = (struct pm_qos_request_list *)filp->private_data;
357 pm_qos_remove_request(req);
Mark Grossd82b3512008-02-04 22:30:08 -0800358
359 return 0;
360}
361
Mark Grossed771342010-05-06 01:59:26 +0200362
Mark Grossd82b3512008-02-04 22:30:08 -0800363static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
364 size_t count, loff_t *f_pos)
365{
366 s32 value;
Mark Grossed771342010-05-06 01:59:26 +0200367 int x;
368 char ascii_value[11];
369 struct pm_qos_request_list *pm_qos_req;
Mark Grossd82b3512008-02-04 22:30:08 -0800370
Mark Grossed771342010-05-06 01:59:26 +0200371 if (count == sizeof(s32)) {
372 if (copy_from_user(&value, buf, sizeof(s32)))
373 return -EFAULT;
374 } else if (count == 11) { /* len('0x12345678/0') */
375 if (copy_from_user(ascii_value, buf, 11))
376 return -EFAULT;
377 x = sscanf(ascii_value, "%x", &value);
378 if (x != 1)
379 return -EINVAL;
380 pr_debug(KERN_ERR "%s, %d, 0x%x\n", ascii_value, x, value);
381 } else
Mark Grossd82b3512008-02-04 22:30:08 -0800382 return -EINVAL;
Mark Grossd82b3512008-02-04 22:30:08 -0800383
Mark Grossed771342010-05-06 01:59:26 +0200384 pm_qos_req = (struct pm_qos_request_list *)filp->private_data;
385 pm_qos_update_request(pm_qos_req, value);
386
387 return count;
Mark Grossd82b3512008-02-04 22:30:08 -0800388}
389
390
391static int __init pm_qos_power_init(void)
392{
393 int ret = 0;
394
395 ret = register_pm_qos_misc(&cpu_dma_pm_qos);
396 if (ret < 0) {
397 printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n");
398 return ret;
399 }
400 ret = register_pm_qos_misc(&network_lat_pm_qos);
401 if (ret < 0) {
402 printk(KERN_ERR "pm_qos_param: network_latency setup failed\n");
403 return ret;
404 }
405 ret = register_pm_qos_misc(&network_throughput_pm_qos);
406 if (ret < 0)
407 printk(KERN_ERR
408 "pm_qos_param: network_throughput setup failed\n");
409
410 return ret;
411}
412
413late_initcall(pm_qos_power_init);