]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/pm_qos_params.c
kernel/pm_qos_params.c: Remove unnecessary casts of private_data
[net-next-2.6.git] / kernel / pm_qos_params.c
CommitLineData
d82b3518
MG
1/*
2 * This module exposes the interface to kernel space for specifying
3 * QoS dependencies. It provides infrastructure for registration of:
4 *
ed77134b 5 * Dependents on a QoS value : register requests
d82b3518
MG
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 *
ed77134b 17 * There are lists of pm_qos_objects each one wrapping requests, notifiers
d82b3518 18 *
ed77134b 19 * User mode requests on a QOS parameter register themselves to the
d82b3518
MG
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
ed77134b
MG
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
d82b3518
MG
25 * pointer or exits the pm_qos_object will get an opportunity to clean up.
26 *
bf1db69f 27 * Mark Gross <mgross@linux.intel.com>
d82b3518
MG
28 */
29
ed77134b
MG
30/*#define DEBUG*/
31
d82b3518
MG
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
44#include <linux/uaccess.h>
45
46/*
ed77134b 47 * locking rule: all changes to requests or notifiers lists
d82b3518
MG
48 * 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 */
5f279845
JB
51enum pm_qos_type {
52 PM_QOS_MAX, /* return the largest value */
53 PM_QOS_MIN /* return the smallest value */
54};
d82b3518
MG
55
56struct pm_qos_object {
5f279845 57 struct plist_head requests;
d82b3518
MG
58 struct blocking_notifier_head *notifiers;
59 struct miscdevice pm_qos_power_miscdev;
60 char *name;
61 s32 default_value;
5f279845 62 enum pm_qos_type type;
d82b3518
MG
63};
64
5f279845
JB
65static DEFINE_SPINLOCK(pm_qos_lock);
66
d82b3518
MG
67static struct pm_qos_object null_pm_qos;
68static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
69static struct pm_qos_object cpu_dma_pm_qos = {
5f279845 70 .requests = PLIST_HEAD_INIT(cpu_dma_pm_qos.requests, pm_qos_lock),
d82b3518
MG
71 .notifiers = &cpu_dma_lat_notifier,
72 .name = "cpu_dma_latency",
73 .default_value = 2000 * USEC_PER_SEC,
5f279845 74 .type = PM_QOS_MIN,
d82b3518
MG
75};
76
77static BLOCKING_NOTIFIER_HEAD(network_lat_notifier);
78static struct pm_qos_object network_lat_pm_qos = {
5f279845 79 .requests = PLIST_HEAD_INIT(network_lat_pm_qos.requests, pm_qos_lock),
d82b3518
MG
80 .notifiers = &network_lat_notifier,
81 .name = "network_latency",
82 .default_value = 2000 * USEC_PER_SEC,
5f279845 83 .type = PM_QOS_MIN
d82b3518
MG
84};
85
86
87static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier);
88static struct pm_qos_object network_throughput_pm_qos = {
5f279845 89 .requests = PLIST_HEAD_INIT(network_throughput_pm_qos.requests, pm_qos_lock),
d82b3518
MG
90 .notifiers = &network_throughput_notifier,
91 .name = "network_throughput",
92 .default_value = 0,
5f279845 93 .type = PM_QOS_MAX,
d82b3518
MG
94};
95
96
97static struct pm_qos_object *pm_qos_array[] = {
98 &null_pm_qos,
99 &cpu_dma_pm_qos,
100 &network_lat_pm_qos,
101 &network_throughput_pm_qos
102};
103
d82b3518
MG
104static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
105 size_t count, loff_t *f_pos);
106static int pm_qos_power_open(struct inode *inode, struct file *filp);
107static int pm_qos_power_release(struct inode *inode, struct file *filp);
108
109static const struct file_operations pm_qos_power_fops = {
110 .write = pm_qos_power_write,
111 .open = pm_qos_power_open,
112 .release = pm_qos_power_release,
113};
114
5f279845
JB
115/* unlocked internal variant */
116static inline int pm_qos_get_value(struct pm_qos_object *o)
d82b3518 117{
5f279845
JB
118 if (plist_head_empty(&o->requests))
119 return o->default_value;
d82b3518 120
5f279845
JB
121 switch (o->type) {
122 case PM_QOS_MIN:
123 return plist_last(&o->requests)->prio;
d82b3518 124
5f279845
JB
125 case PM_QOS_MAX:
126 return plist_first(&o->requests)->prio;
d82b3518 127
5f279845
JB
128 default:
129 /* runtime check for not using enum */
130 BUG();
131 }
132}
133
134static void update_target(struct pm_qos_object *o, struct plist_node *node,
135 int del, int value)
d82b3518 136{
d82b3518 137 unsigned long flags;
5f279845 138 int prev_value, curr_value;
d82b3518
MG
139
140 spin_lock_irqsave(&pm_qos_lock, flags);
5f279845
JB
141 prev_value = pm_qos_get_value(o);
142 /* PM_QOS_DEFAULT_VALUE is a signal that the value is unchanged */
143 if (value != PM_QOS_DEFAULT_VALUE) {
144 /*
145 * to change the list, we atomically remove, reinit
146 * with new value and add, then see if the extremal
147 * changed
148 */
149 plist_del(node, &o->requests);
150 plist_node_init(node, value);
151 plist_add(node, &o->requests);
152 } else if (del) {
153 plist_del(node, &o->requests);
154 } else {
155 plist_add(node, &o->requests);
d82b3518 156 }
5f279845 157 curr_value = pm_qos_get_value(o);
d82b3518
MG
158 spin_unlock_irqrestore(&pm_qos_lock, flags);
159
5f279845
JB
160 if (prev_value != curr_value)
161 blocking_notifier_call_chain(o->notifiers,
162 (unsigned long)curr_value,
163 NULL);
d82b3518
MG
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/**
ed77134b 189 * pm_qos_request - returns current system wide qos expectation
d82b3518
MG
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 */
ed77134b 194int pm_qos_request(int pm_qos_class)
d82b3518 195{
5f279845
JB
196 unsigned long flags;
197 int value;
198
199 spin_lock_irqsave(&pm_qos_lock, flags);
200 value = pm_qos_get_value(pm_qos_array[pm_qos_class]);
201 spin_unlock_irqrestore(&pm_qos_lock, flags);
202
203 return value;
d82b3518 204}
ed77134b 205EXPORT_SYMBOL_GPL(pm_qos_request);
d82b3518 206
82f68251
JB
207int pm_qos_request_active(struct pm_qos_request_list *req)
208{
209 return req->pm_qos_class != 0;
210}
211EXPORT_SYMBOL_GPL(pm_qos_request_active);
212
d82b3518 213/**
ed77134b 214 * pm_qos_add_request - inserts new qos request into the list
d82b3518 215 * @pm_qos_class: identifies which list of qos request to us
d82b3518
MG
216 * @value: defines the qos request
217 *
218 * This function inserts a new entry in the pm_qos_class list of requested qos
bf1db69f 219 * performance characteristics. It recomputes the aggregate QoS expectations
ed77134b
MG
220 * for the pm_qos_class of parameters, and returns the pm_qos_request list
221 * element as a handle for use in updating and removal. Call needs to save
222 * this handle for later use.
d82b3518 223 */
82f68251
JB
224void pm_qos_add_request(struct pm_qos_request_list *dep,
225 int pm_qos_class, s32 value)
d82b3518 226{
82f68251
JB
227 struct pm_qos_object *o = pm_qos_array[pm_qos_class];
228 int new_value;
d82b3518 229
82f68251
JB
230 if (pm_qos_request_active(dep)) {
231 WARN(1, KERN_ERR "pm_qos_add_request() called for already added request\n");
232 return;
233 }
234 if (value == PM_QOS_DEFAULT_VALUE)
235 new_value = o->default_value;
236 else
237 new_value = value;
238 plist_node_init(&dep->list, new_value);
239 dep->pm_qos_class = pm_qos_class;
240 update_target(o, &dep->list, 0, PM_QOS_DEFAULT_VALUE);
d82b3518 241}
ed77134b 242EXPORT_SYMBOL_GPL(pm_qos_add_request);
d82b3518
MG
243
244/**
ed77134b
MG
245 * pm_qos_update_request - modifies an existing qos request
246 * @pm_qos_req : handle to list element holding a pm_qos request to use
d82b3518
MG
247 * @value: defines the qos request
248 *
ed77134b 249 * Updates an existing qos request for the pm_qos_class of parameters along
d82b3518
MG
250 * with updating the target pm_qos_class value.
251 *
ed77134b 252 * Attempts are made to make this code callable on hot code paths.
d82b3518 253 */
ed77134b 254void pm_qos_update_request(struct pm_qos_request_list *pm_qos_req,
5f279845 255 s32 new_value)
d82b3518 256{
ed77134b 257 s32 temp;
5f279845 258 struct pm_qos_object *o;
d82b3518 259
5f279845
JB
260 if (!pm_qos_req) /*guard against callers passing in null */
261 return;
262
82f68251
JB
263 if (!pm_qos_request_active(pm_qos_req)) {
264 WARN(1, KERN_ERR "pm_qos_update_request() called for unknown object\n");
265 return;
266 }
267
5f279845
JB
268 o = pm_qos_array[pm_qos_req->pm_qos_class];
269
270 if (new_value == PM_QOS_DEFAULT_VALUE)
271 temp = o->default_value;
272 else
273 temp = new_value;
274
275 if (temp != pm_qos_req->list.prio)
276 update_target(o, &pm_qos_req->list, 0, temp);
d82b3518 277}
ed77134b 278EXPORT_SYMBOL_GPL(pm_qos_update_request);
d82b3518
MG
279
280/**
ed77134b
MG
281 * pm_qos_remove_request - modifies an existing qos request
282 * @pm_qos_req: handle to request list element
d82b3518 283 *
ed77134b
MG
284 * Will remove pm qos request from the list of requests and
285 * recompute the current target value for the pm_qos_class. Call this
286 * on slow code paths.
d82b3518 287 */
ed77134b 288void pm_qos_remove_request(struct pm_qos_request_list *pm_qos_req)
d82b3518 289{
5f279845 290 struct pm_qos_object *o;
ed77134b
MG
291
292 if (pm_qos_req == NULL)
293 return;
294 /* silent return to keep pcm code cleaner */
d82b3518 295
82f68251
JB
296 if (!pm_qos_request_active(pm_qos_req)) {
297 WARN(1, KERN_ERR "pm_qos_remove_request() called for unknown object\n");
298 return;
299 }
300
5f279845
JB
301 o = pm_qos_array[pm_qos_req->pm_qos_class];
302 update_target(o, &pm_qos_req->list, 1, PM_QOS_DEFAULT_VALUE);
82f68251 303 memset(pm_qos_req, 0, sizeof(*pm_qos_req));
d82b3518 304}
ed77134b 305EXPORT_SYMBOL_GPL(pm_qos_remove_request);
d82b3518
MG
306
307/**
308 * pm_qos_add_notifier - sets notification entry for changes to target value
309 * @pm_qos_class: identifies which qos target changes should be notified.
310 * @notifier: notifier block managed by caller.
311 *
312 * will register the notifier into a notification chain that gets called
bf1db69f 313 * upon changes to the pm_qos_class target value.
d82b3518 314 */
ed77134b 315int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
d82b3518
MG
316{
317 int retval;
318
319 retval = blocking_notifier_chain_register(
320 pm_qos_array[pm_qos_class]->notifiers, notifier);
321
322 return retval;
323}
324EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
325
326/**
327 * pm_qos_remove_notifier - deletes notification entry from chain.
328 * @pm_qos_class: identifies which qos target changes are notified.
329 * @notifier: notifier block to be removed.
330 *
331 * will remove the notifier from the notification chain that gets called
bf1db69f 332 * upon changes to the pm_qos_class target value.
d82b3518
MG
333 */
334int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
335{
336 int retval;
337
338 retval = blocking_notifier_chain_unregister(
339 pm_qos_array[pm_qos_class]->notifiers, notifier);
340
341 return retval;
342}
343EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
344
d82b3518
MG
345static int pm_qos_power_open(struct inode *inode, struct file *filp)
346{
d82b3518
MG
347 long pm_qos_class;
348
349 pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
350 if (pm_qos_class >= 0) {
82f68251
JB
351 struct pm_qos_request_list *req = kzalloc(GFP_KERNEL, sizeof(*req));
352 if (!req)
353 return -ENOMEM;
354
355 pm_qos_add_request(req, pm_qos_class, PM_QOS_DEFAULT_VALUE);
356 filp->private_data = req;
ed77134b
MG
357
358 if (filp->private_data)
d82b3518
MG
359 return 0;
360 }
d82b3518
MG
361 return -EPERM;
362}
363
364static int pm_qos_power_release(struct inode *inode, struct file *filp)
365{
ed77134b 366 struct pm_qos_request_list *req;
d82b3518 367
82f68251 368 req = filp->private_data;
ed77134b 369 pm_qos_remove_request(req);
82f68251 370 kfree(req);
d82b3518
MG
371
372 return 0;
373}
374
ed77134b 375
d82b3518
MG
376static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
377 size_t count, loff_t *f_pos)
378{
379 s32 value;
ed77134b
MG
380 int x;
381 char ascii_value[11];
382 struct pm_qos_request_list *pm_qos_req;
383
384 if (count == sizeof(s32)) {
385 if (copy_from_user(&value, buf, sizeof(s32)))
386 return -EFAULT;
387 } else if (count == 11) { /* len('0x12345678/0') */
388 if (copy_from_user(ascii_value, buf, 11))
389 return -EFAULT;
390 x = sscanf(ascii_value, "%x", &value);
391 if (x != 1)
392 return -EINVAL;
393 pr_debug(KERN_ERR "%s, %d, 0x%x\n", ascii_value, x, value);
394 } else
d82b3518 395 return -EINVAL;
d82b3518 396
99a51792 397 pm_qos_req = filp->private_data;
ed77134b
MG
398 pm_qos_update_request(pm_qos_req, value);
399
400 return count;
d82b3518
MG
401}
402
403
404static int __init pm_qos_power_init(void)
405{
406 int ret = 0;
407
408 ret = register_pm_qos_misc(&cpu_dma_pm_qos);
409 if (ret < 0) {
410 printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n");
411 return ret;
412 }
413 ret = register_pm_qos_misc(&network_lat_pm_qos);
414 if (ret < 0) {
415 printk(KERN_ERR "pm_qos_param: network_latency setup failed\n");
416 return ret;
417 }
418 ret = register_pm_qos_misc(&network_throughput_pm_qos);
419 if (ret < 0)
420 printk(KERN_ERR
421 "pm_qos_param: network_throughput setup failed\n");
422
423 return ret;
424}
425
426late_initcall(pm_qos_power_init);