blob: dfdec524d1b7dc9de49380d6d4552dd71add9a7a [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 *
5 * Dependents on a QoS value : register requirements
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 requirements, notifiers
18 *
19 * User mode requirements 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 * requirement is removed and a new qos target is computed. This way when the
24 * requirement 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 *
Richard Hughesbf1db692008-08-05 13:01:35 -070027 * Mark Gross <mgross@linux.intel.com>
Mark Grossd82b3512008-02-04 22:30:08 -080028 */
29
30#include <linux/pm_qos_params.h>
31#include <linux/sched.h>
Arnd Bergmanndb26e642008-05-20 19:16:33 +020032#include <linux/smp_lock.h>
Mark Grossd82b3512008-02-04 22:30:08 -080033#include <linux/spinlock.h>
34#include <linux/slab.h>
35#include <linux/time.h>
36#include <linux/fs.h>
37#include <linux/device.h>
38#include <linux/miscdevice.h>
39#include <linux/string.h>
40#include <linux/platform_device.h>
41#include <linux/init.h>
42
43#include <linux/uaccess.h>
44
45/*
John Kacur9d359352008-09-02 14:36:13 -070046 * locking rule: all changes to requirements or notifiers lists
Mark Grossd82b3512008-02-04 22:30:08 -080047 * or pm_qos_object list and pm_qos_objects need to happen with pm_qos_lock
48 * held, taken with _irqsave. One lock to rule them all
49 */
50struct requirement_list {
51 struct list_head list;
52 union {
53 s32 value;
54 s32 usec;
55 s32 kbps;
56 };
57 char *name;
58};
59
60static s32 max_compare(s32 v1, s32 v2);
61static s32 min_compare(s32 v1, s32 v2);
62
63struct pm_qos_object {
64 struct requirement_list requirements;
65 struct blocking_notifier_head *notifiers;
66 struct miscdevice pm_qos_power_miscdev;
67 char *name;
68 s32 default_value;
John Kacur9d359352008-09-02 14:36:13 -070069 atomic_t target_value;
Mark Grossd82b3512008-02-04 22:30:08 -080070 s32 (*comparitor)(s32, s32);
71};
72
73static struct pm_qos_object null_pm_qos;
74static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
75static struct pm_qos_object cpu_dma_pm_qos = {
76 .requirements = {LIST_HEAD_INIT(cpu_dma_pm_qos.requirements.list)},
77 .notifiers = &cpu_dma_lat_notifier,
78 .name = "cpu_dma_latency",
79 .default_value = 2000 * USEC_PER_SEC,
John Kacur9d359352008-09-02 14:36:13 -070080 .target_value = ATOMIC_INIT(2000 * USEC_PER_SEC),
Mark Grossd82b3512008-02-04 22:30:08 -080081 .comparitor = min_compare
82};
83
84static BLOCKING_NOTIFIER_HEAD(network_lat_notifier);
85static struct pm_qos_object network_lat_pm_qos = {
86 .requirements = {LIST_HEAD_INIT(network_lat_pm_qos.requirements.list)},
87 .notifiers = &network_lat_notifier,
88 .name = "network_latency",
89 .default_value = 2000 * USEC_PER_SEC,
John Kacur9d359352008-09-02 14:36:13 -070090 .target_value = ATOMIC_INIT(2000 * USEC_PER_SEC),
Mark Grossd82b3512008-02-04 22:30:08 -080091 .comparitor = min_compare
92};
93
94
95static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier);
96static struct pm_qos_object network_throughput_pm_qos = {
97 .requirements =
98 {LIST_HEAD_INIT(network_throughput_pm_qos.requirements.list)},
99 .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
139static void update_target(int target)
140{
141 s32 extreme_value;
142 struct requirement_list *node;
143 unsigned long flags;
144 int call_notifier = 0;
145
146 spin_lock_irqsave(&pm_qos_lock, flags);
147 extreme_value = pm_qos_array[target]->default_value;
148 list_for_each_entry(node,
149 &pm_qos_array[target]->requirements.list, list) {
150 extreme_value = pm_qos_array[target]->comparitor(
151 extreme_value, node->value);
152 }
John Kacur9d359352008-09-02 14:36:13 -0700153 if (atomic_read(&pm_qos_array[target]->target_value) != extreme_value) {
Mark Grossd82b3512008-02-04 22:30:08 -0800154 call_notifier = 1;
John Kacur9d359352008-09-02 14:36:13 -0700155 atomic_set(&pm_qos_array[target]->target_value, extreme_value);
Mark Grossd82b3512008-02-04 22:30:08 -0800156 pr_debug(KERN_ERR "new target for qos %d is %d\n", target,
John Kacur9d359352008-09-02 14:36:13 -0700157 atomic_read(&pm_qos_array[target]->target_value));
Mark Grossd82b3512008-02-04 22:30:08 -0800158 }
159 spin_unlock_irqrestore(&pm_qos_lock, flags);
160
161 if (call_notifier)
162 blocking_notifier_call_chain(pm_qos_array[target]->notifiers,
163 (unsigned long) extreme_value, NULL);
164}
165
166static int register_pm_qos_misc(struct pm_qos_object *qos)
167{
168 qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR;
169 qos->pm_qos_power_miscdev.name = qos->name;
170 qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops;
171
172 return misc_register(&qos->pm_qos_power_miscdev);
173}
174
175static int find_pm_qos_object_by_minor(int minor)
176{
177 int pm_qos_class;
178
179 for (pm_qos_class = 0;
180 pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) {
181 if (minor ==
182 pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor)
183 return pm_qos_class;
184 }
185 return -1;
186}
187
188/**
189 * pm_qos_requirement - returns current system wide qos expectation
190 * @pm_qos_class: identification of which qos value is requested
191 *
192 * This function returns the current target value in an atomic manner.
193 */
194int pm_qos_requirement(int pm_qos_class)
195{
John Kacur9d359352008-09-02 14:36:13 -0700196 return atomic_read(&pm_qos_array[pm_qos_class]->target_value);
Mark Grossd82b3512008-02-04 22:30:08 -0800197}
198EXPORT_SYMBOL_GPL(pm_qos_requirement);
199
200/**
201 * pm_qos_add_requirement - inserts new qos request into the list
202 * @pm_qos_class: identifies which list of qos request to us
203 * @name: identifies the request
204 * @value: defines the qos request
205 *
206 * This function inserts a new entry in the pm_qos_class list of requested qos
Richard Hughesbf1db692008-08-05 13:01:35 -0700207 * performance characteristics. It recomputes the aggregate QoS expectations
208 * for the pm_qos_class of parameters.
Mark Grossd82b3512008-02-04 22:30:08 -0800209 */
210int pm_qos_add_requirement(int pm_qos_class, char *name, s32 value)
211{
212 struct requirement_list *dep;
213 unsigned long flags;
214
215 dep = kzalloc(sizeof(struct requirement_list), GFP_KERNEL);
216 if (dep) {
217 if (value == PM_QOS_DEFAULT_VALUE)
218 dep->value = pm_qos_array[pm_qos_class]->default_value;
219 else
220 dep->value = value;
221 dep->name = kstrdup(name, GFP_KERNEL);
222 if (!dep->name)
223 goto cleanup;
224
225 spin_lock_irqsave(&pm_qos_lock, flags);
226 list_add(&dep->list,
227 &pm_qos_array[pm_qos_class]->requirements.list);
228 spin_unlock_irqrestore(&pm_qos_lock, flags);
229 update_target(pm_qos_class);
230
231 return 0;
232 }
233
234cleanup:
235 kfree(dep);
236 return -ENOMEM;
237}
238EXPORT_SYMBOL_GPL(pm_qos_add_requirement);
239
240/**
241 * pm_qos_update_requirement - modifies an existing qos request
242 * @pm_qos_class: identifies which list of qos request to us
243 * @name: identifies the request
244 * @value: defines the qos request
245 *
Richard Hughesbf1db692008-08-05 13:01:35 -0700246 * Updates an existing qos requirement for the pm_qos_class of parameters along
Mark Grossd82b3512008-02-04 22:30:08 -0800247 * with updating the target pm_qos_class value.
248 *
Richard Hughesbf1db692008-08-05 13:01:35 -0700249 * If the named request isn't in the list then no change is made.
Mark Grossd82b3512008-02-04 22:30:08 -0800250 */
251int pm_qos_update_requirement(int pm_qos_class, char *name, s32 new_value)
252{
253 unsigned long flags;
254 struct requirement_list *node;
255 int pending_update = 0;
256
257 spin_lock_irqsave(&pm_qos_lock, flags);
258 list_for_each_entry(node,
259 &pm_qos_array[pm_qos_class]->requirements.list, list) {
260 if (strcmp(node->name, name) == 0) {
261 if (new_value == PM_QOS_DEFAULT_VALUE)
262 node->value =
263 pm_qos_array[pm_qos_class]->default_value;
264 else
265 node->value = new_value;
266 pending_update = 1;
267 break;
268 }
269 }
270 spin_unlock_irqrestore(&pm_qos_lock, flags);
271 if (pending_update)
272 update_target(pm_qos_class);
273
274 return 0;
275}
276EXPORT_SYMBOL_GPL(pm_qos_update_requirement);
277
278/**
279 * pm_qos_remove_requirement - modifies an existing qos request
280 * @pm_qos_class: identifies which list of qos request to us
281 * @name: identifies the request
282 *
Richard Hughesbf1db692008-08-05 13:01:35 -0700283 * Will remove named qos request from pm_qos_class list of parameters and
Mark Grossd82b3512008-02-04 22:30:08 -0800284 * recompute the current target value for the pm_qos_class.
285 */
286void pm_qos_remove_requirement(int pm_qos_class, char *name)
287{
288 unsigned long flags;
289 struct requirement_list *node;
290 int pending_update = 0;
291
292 spin_lock_irqsave(&pm_qos_lock, flags);
293 list_for_each_entry(node,
294 &pm_qos_array[pm_qos_class]->requirements.list, list) {
295 if (strcmp(node->name, name) == 0) {
296 kfree(node->name);
297 list_del(&node->list);
298 kfree(node);
299 pending_update = 1;
300 break;
301 }
302 }
303 spin_unlock_irqrestore(&pm_qos_lock, flags);
304 if (pending_update)
305 update_target(pm_qos_class);
306}
307EXPORT_SYMBOL_GPL(pm_qos_remove_requirement);
308
309/**
310 * pm_qos_add_notifier - sets notification entry for changes to target value
311 * @pm_qos_class: identifies which qos target changes should be notified.
312 * @notifier: notifier block managed by caller.
313 *
314 * will register the notifier into a notification chain that gets called
Richard Hughesbf1db692008-08-05 13:01:35 -0700315 * upon changes to the pm_qos_class target value.
Mark Grossd82b3512008-02-04 22:30:08 -0800316 */
317 int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
318{
319 int retval;
320
321 retval = blocking_notifier_chain_register(
322 pm_qos_array[pm_qos_class]->notifiers, notifier);
323
324 return retval;
325}
326EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
327
328/**
329 * pm_qos_remove_notifier - deletes notification entry from chain.
330 * @pm_qos_class: identifies which qos target changes are notified.
331 * @notifier: notifier block to be removed.
332 *
333 * will remove the notifier from the notification chain that gets called
Richard Hughesbf1db692008-08-05 13:01:35 -0700334 * upon changes to the pm_qos_class target value.
Mark Grossd82b3512008-02-04 22:30:08 -0800335 */
336int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
337{
338 int retval;
339
340 retval = blocking_notifier_chain_unregister(
341 pm_qos_array[pm_qos_class]->notifiers, notifier);
342
343 return retval;
344}
345EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
346
347#define PID_NAME_LEN sizeof("process_1234567890")
348static char name[PID_NAME_LEN];
349
350static int pm_qos_power_open(struct inode *inode, struct file *filp)
351{
352 int ret;
353 long pm_qos_class;
354
Arnd Bergmanndb26e642008-05-20 19:16:33 +0200355 lock_kernel();
Mark Grossd82b3512008-02-04 22:30:08 -0800356 pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
357 if (pm_qos_class >= 0) {
358 filp->private_data = (void *)pm_qos_class;
359 sprintf(name, "process_%d", current->pid);
360 ret = pm_qos_add_requirement(pm_qos_class, name,
361 PM_QOS_DEFAULT_VALUE);
Arnd Bergmanndb26e642008-05-20 19:16:33 +0200362 if (ret >= 0) {
363 unlock_kernel();
Mark Grossd82b3512008-02-04 22:30:08 -0800364 return 0;
Arnd Bergmanndb26e642008-05-20 19:16:33 +0200365 }
Mark Grossd82b3512008-02-04 22:30:08 -0800366 }
Arnd Bergmanndb26e642008-05-20 19:16:33 +0200367 unlock_kernel();
Mark Grossd82b3512008-02-04 22:30:08 -0800368
369 return -EPERM;
370}
371
372static int pm_qos_power_release(struct inode *inode, struct file *filp)
373{
374 int pm_qos_class;
375
376 pm_qos_class = (long)filp->private_data;
377 sprintf(name, "process_%d", current->pid);
378 pm_qos_remove_requirement(pm_qos_class, name);
379
380 return 0;
381}
382
383static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
384 size_t count, loff_t *f_pos)
385{
386 s32 value;
387 int pm_qos_class;
388
389 pm_qos_class = (long)filp->private_data;
390 if (count != sizeof(s32))
391 return -EINVAL;
392 if (copy_from_user(&value, buf, sizeof(s32)))
393 return -EFAULT;
394 sprintf(name, "process_%d", current->pid);
395 pm_qos_update_requirement(pm_qos_class, name, value);
396
397 return sizeof(s32);
398}
399
400
401static int __init pm_qos_power_init(void)
402{
403 int ret = 0;
404
405 ret = register_pm_qos_misc(&cpu_dma_pm_qos);
406 if (ret < 0) {
407 printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n");
408 return ret;
409 }
410 ret = register_pm_qos_misc(&network_lat_pm_qos);
411 if (ret < 0) {
412 printk(KERN_ERR "pm_qos_param: network_latency setup failed\n");
413 return ret;
414 }
415 ret = register_pm_qos_misc(&network_throughput_pm_qos);
416 if (ret < 0)
417 printk(KERN_ERR
418 "pm_qos_param: network_throughput setup failed\n");
419
420 return ret;
421}
422
423late_initcall(pm_qos_power_init);