blob: 645e541a45f6c9a9667c054bd7f5230ecaef67a3 [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 */
James Bottomley5f279842010-07-19 02:00:18 +020051enum pm_qos_type {
52 PM_QOS_MAX, /* return the largest value */
53 PM_QOS_MIN /* return the smallest value */
54};
Mark Grossd82b3512008-02-04 22:30:08 -080055
56struct pm_qos_object {
James Bottomley5f279842010-07-19 02:00:18 +020057 struct plist_head requests;
Mark Grossd82b3512008-02-04 22:30:08 -080058 struct blocking_notifier_head *notifiers;
59 struct miscdevice pm_qos_power_miscdev;
60 char *name;
61 s32 default_value;
James Bottomley5f279842010-07-19 02:00:18 +020062 enum pm_qos_type type;
Mark Grossd82b3512008-02-04 22:30:08 -080063};
64
James Bottomley5f279842010-07-19 02:00:18 +020065static DEFINE_SPINLOCK(pm_qos_lock);
66
Mark Grossd82b3512008-02-04 22:30:08 -080067static struct pm_qos_object null_pm_qos;
68static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
69static struct pm_qos_object cpu_dma_pm_qos = {
James Bottomley5f279842010-07-19 02:00:18 +020070 .requests = PLIST_HEAD_INIT(cpu_dma_pm_qos.requests, pm_qos_lock),
Mark Grossd82b3512008-02-04 22:30:08 -080071 .notifiers = &cpu_dma_lat_notifier,
72 .name = "cpu_dma_latency",
73 .default_value = 2000 * USEC_PER_SEC,
James Bottomley5f279842010-07-19 02:00:18 +020074 .type = PM_QOS_MIN,
Mark Grossd82b3512008-02-04 22:30:08 -080075};
76
77static BLOCKING_NOTIFIER_HEAD(network_lat_notifier);
78static struct pm_qos_object network_lat_pm_qos = {
James Bottomley5f279842010-07-19 02:00:18 +020079 .requests = PLIST_HEAD_INIT(network_lat_pm_qos.requests, pm_qos_lock),
Mark Grossd82b3512008-02-04 22:30:08 -080080 .notifiers = &network_lat_notifier,
81 .name = "network_latency",
82 .default_value = 2000 * USEC_PER_SEC,
James Bottomley5f279842010-07-19 02:00:18 +020083 .type = PM_QOS_MIN
Mark Grossd82b3512008-02-04 22:30:08 -080084};
85
86
87static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier);
88static struct pm_qos_object network_throughput_pm_qos = {
James Bottomley5f279842010-07-19 02:00:18 +020089 .requests = PLIST_HEAD_INIT(network_throughput_pm_qos.requests, pm_qos_lock),
Mark Grossd82b3512008-02-04 22:30:08 -080090 .notifiers = &network_throughput_notifier,
91 .name = "network_throughput",
92 .default_value = 0,
James Bottomley5f279842010-07-19 02:00:18 +020093 .type = PM_QOS_MAX,
Mark Grossd82b3512008-02-04 22:30:08 -080094};
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
Mark Grossd82b3512008-02-04 22:30:08 -0800104static 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
James Bottomley5f279842010-07-19 02:00:18 +0200115/* unlocked internal variant */
116static inline int pm_qos_get_value(struct pm_qos_object *o)
Mark Grossd82b3512008-02-04 22:30:08 -0800117{
James Bottomley5f279842010-07-19 02:00:18 +0200118 if (plist_head_empty(&o->requests))
119 return o->default_value;
120
121 switch (o->type) {
122 case PM_QOS_MIN:
123 return plist_last(&o->requests)->prio;
124
125 case PM_QOS_MAX:
126 return plist_first(&o->requests)->prio;
127
128 default:
129 /* runtime check for not using enum */
130 BUG();
131 }
Mark Grossd82b3512008-02-04 22:30:08 -0800132}
133
James Bottomley5f279842010-07-19 02:00:18 +0200134static void update_target(struct pm_qos_object *o, struct plist_node *node,
135 int del, int value)
Mark Grossd82b3512008-02-04 22:30:08 -0800136{
Mark Grossd82b3512008-02-04 22:30:08 -0800137 unsigned long flags;
James Bottomley5f279842010-07-19 02:00:18 +0200138 int prev_value, curr_value;
Mark Grossd82b3512008-02-04 22:30:08 -0800139
140 spin_lock_irqsave(&pm_qos_lock, flags);
James Bottomley5f279842010-07-19 02:00:18 +0200141 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);
Mark Grossd82b3512008-02-04 22:30:08 -0800156 }
James Bottomley5f279842010-07-19 02:00:18 +0200157 curr_value = pm_qos_get_value(o);
Mark Grossd82b3512008-02-04 22:30:08 -0800158 spin_unlock_irqrestore(&pm_qos_lock, flags);
159
James Bottomley5f279842010-07-19 02:00:18 +0200160 if (prev_value != curr_value)
161 blocking_notifier_call_chain(o->notifiers,
162 (unsigned long)curr_value,
163 NULL);
Mark Grossd82b3512008-02-04 22:30:08 -0800164}
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/**
Mark Grossed771342010-05-06 01:59:26 +0200189 * pm_qos_request - returns current system wide qos expectation
Mark Grossd82b3512008-02-04 22:30:08 -0800190 * @pm_qos_class: identification of which qos value is requested
191 *
192 * This function returns the current target value in an atomic manner.
193 */
Mark Grossed771342010-05-06 01:59:26 +0200194int pm_qos_request(int pm_qos_class)
Mark Grossd82b3512008-02-04 22:30:08 -0800195{
James Bottomley5f279842010-07-19 02:00:18 +0200196 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;
Mark Grossd82b3512008-02-04 22:30:08 -0800204}
Mark Grossed771342010-05-06 01:59:26 +0200205EXPORT_SYMBOL_GPL(pm_qos_request);
Mark Grossd82b3512008-02-04 22:30:08 -0800206
James Bottomley82f68252010-07-05 22:53:06 +0200207int 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
Mark Grossd82b3512008-02-04 22:30:08 -0800213/**
Mark Grossed771342010-05-06 01:59:26 +0200214 * pm_qos_add_request - inserts new qos request into the list
Saravana Kannan25cc69e2010-08-26 20:18:43 +0200215 * @dep: pointer to a preallocated handle
216 * @pm_qos_class: identifies which list of qos request to use
Mark Grossd82b3512008-02-04 22:30:08 -0800217 * @value: defines the qos request
218 *
219 * This function inserts a new entry in the pm_qos_class list of requested qos
Richard Hughesbf1db692008-08-05 13:01:35 -0700220 * performance characteristics. It recomputes the aggregate QoS expectations
Saravana Kannan25cc69e2010-08-26 20:18:43 +0200221 * for the pm_qos_class of parameters and initializes the pm_qos_request_list
222 * handle. Caller needs to save this handle for later use in updates and
223 * removal.
Mark Grossd82b3512008-02-04 22:30:08 -0800224 */
Saravana Kannan25cc69e2010-08-26 20:18:43 +0200225
James Bottomley82f68252010-07-05 22:53:06 +0200226void pm_qos_add_request(struct pm_qos_request_list *dep,
227 int pm_qos_class, s32 value)
Mark Grossd82b3512008-02-04 22:30:08 -0800228{
James Bottomley82f68252010-07-05 22:53:06 +0200229 struct pm_qos_object *o = pm_qos_array[pm_qos_class];
230 int new_value;
Mark Grossd82b3512008-02-04 22:30:08 -0800231
James Bottomley82f68252010-07-05 22:53:06 +0200232 if (pm_qos_request_active(dep)) {
233 WARN(1, KERN_ERR "pm_qos_add_request() called for already added request\n");
234 return;
Mark Grossd82b3512008-02-04 22:30:08 -0800235 }
James Bottomley82f68252010-07-05 22:53:06 +0200236 if (value == PM_QOS_DEFAULT_VALUE)
237 new_value = o->default_value;
238 else
239 new_value = value;
240 plist_node_init(&dep->list, new_value);
241 dep->pm_qos_class = pm_qos_class;
242 update_target(o, &dep->list, 0, PM_QOS_DEFAULT_VALUE);
Mark Grossd82b3512008-02-04 22:30:08 -0800243}
Mark Grossed771342010-05-06 01:59:26 +0200244EXPORT_SYMBOL_GPL(pm_qos_add_request);
Mark Grossd82b3512008-02-04 22:30:08 -0800245
246/**
Mark Grossed771342010-05-06 01:59:26 +0200247 * pm_qos_update_request - modifies an existing qos request
248 * @pm_qos_req : handle to list element holding a pm_qos request to use
Mark Grossd82b3512008-02-04 22:30:08 -0800249 * @value: defines the qos request
250 *
Mark Grossed771342010-05-06 01:59:26 +0200251 * Updates an existing qos request for the pm_qos_class of parameters along
Mark Grossd82b3512008-02-04 22:30:08 -0800252 * with updating the target pm_qos_class value.
253 *
Mark Grossed771342010-05-06 01:59:26 +0200254 * Attempts are made to make this code callable on hot code paths.
Mark Grossd82b3512008-02-04 22:30:08 -0800255 */
Mark Grossed771342010-05-06 01:59:26 +0200256void pm_qos_update_request(struct pm_qos_request_list *pm_qos_req,
James Bottomley5f279842010-07-19 02:00:18 +0200257 s32 new_value)
Mark Grossd82b3512008-02-04 22:30:08 -0800258{
Mark Grossed771342010-05-06 01:59:26 +0200259 s32 temp;
James Bottomley5f279842010-07-19 02:00:18 +0200260 struct pm_qos_object *o;
Mark Grossd82b3512008-02-04 22:30:08 -0800261
James Bottomley5f279842010-07-19 02:00:18 +0200262 if (!pm_qos_req) /*guard against callers passing in null */
263 return;
Mark Grossed771342010-05-06 01:59:26 +0200264
James Bottomley82f68252010-07-05 22:53:06 +0200265 if (!pm_qos_request_active(pm_qos_req)) {
266 WARN(1, KERN_ERR "pm_qos_update_request() called for unknown object\n");
267 return;
268 }
269
James Bottomley5f279842010-07-19 02:00:18 +0200270 o = pm_qos_array[pm_qos_req->pm_qos_class];
271
272 if (new_value == PM_QOS_DEFAULT_VALUE)
273 temp = o->default_value;
274 else
275 temp = new_value;
276
277 if (temp != pm_qos_req->list.prio)
278 update_target(o, &pm_qos_req->list, 0, temp);
Mark Grossd82b3512008-02-04 22:30:08 -0800279}
Mark Grossed771342010-05-06 01:59:26 +0200280EXPORT_SYMBOL_GPL(pm_qos_update_request);
Mark Grossd82b3512008-02-04 22:30:08 -0800281
282/**
Mark Grossed771342010-05-06 01:59:26 +0200283 * pm_qos_remove_request - modifies an existing qos request
284 * @pm_qos_req: handle to request list element
Mark Grossd82b3512008-02-04 22:30:08 -0800285 *
Mark Grossed771342010-05-06 01:59:26 +0200286 * Will remove pm qos request from the list of requests and
287 * recompute the current target value for the pm_qos_class. Call this
288 * on slow code paths.
Mark Grossd82b3512008-02-04 22:30:08 -0800289 */
Mark Grossed771342010-05-06 01:59:26 +0200290void pm_qos_remove_request(struct pm_qos_request_list *pm_qos_req)
Mark Grossd82b3512008-02-04 22:30:08 -0800291{
James Bottomley5f279842010-07-19 02:00:18 +0200292 struct pm_qos_object *o;
Mark Grossd82b3512008-02-04 22:30:08 -0800293
Mark Grossed771342010-05-06 01:59:26 +0200294 if (pm_qos_req == NULL)
295 return;
296 /* silent return to keep pcm code cleaner */
297
James Bottomley82f68252010-07-05 22:53:06 +0200298 if (!pm_qos_request_active(pm_qos_req)) {
299 WARN(1, KERN_ERR "pm_qos_remove_request() called for unknown object\n");
300 return;
301 }
302
James Bottomley5f279842010-07-19 02:00:18 +0200303 o = pm_qos_array[pm_qos_req->pm_qos_class];
304 update_target(o, &pm_qos_req->list, 1, PM_QOS_DEFAULT_VALUE);
James Bottomley82f68252010-07-05 22:53:06 +0200305 memset(pm_qos_req, 0, sizeof(*pm_qos_req));
Mark Grossd82b3512008-02-04 22:30:08 -0800306}
Mark Grossed771342010-05-06 01:59:26 +0200307EXPORT_SYMBOL_GPL(pm_qos_remove_request);
Mark Grossd82b3512008-02-04 22:30:08 -0800308
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 */
Mark Grossed771342010-05-06 01:59:26 +0200317int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
Mark Grossd82b3512008-02-04 22:30:08 -0800318{
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
Mark Grossd82b3512008-02-04 22:30:08 -0800347static int pm_qos_power_open(struct inode *inode, struct file *filp)
348{
Mark Grossd82b3512008-02-04 22:30:08 -0800349 long pm_qos_class;
350
351 pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
352 if (pm_qos_class >= 0) {
David Alan Gilbertbac1e742010-08-24 20:22:18 +0200353 struct pm_qos_request_list *req = kzalloc(sizeof(*req), GFP_KERNEL);
James Bottomley82f68252010-07-05 22:53:06 +0200354 if (!req)
355 return -ENOMEM;
356
357 pm_qos_add_request(req, pm_qos_class, PM_QOS_DEFAULT_VALUE);
358 filp->private_data = req;
Mark Grossed771342010-05-06 01:59:26 +0200359
360 if (filp->private_data)
Mark Grossd82b3512008-02-04 22:30:08 -0800361 return 0;
362 }
Mark Grossd82b3512008-02-04 22:30:08 -0800363 return -EPERM;
364}
365
366static int pm_qos_power_release(struct inode *inode, struct file *filp)
367{
Mark Grossed771342010-05-06 01:59:26 +0200368 struct pm_qos_request_list *req;
Mark Grossd82b3512008-02-04 22:30:08 -0800369
James Bottomley82f68252010-07-05 22:53:06 +0200370 req = filp->private_data;
Mark Grossed771342010-05-06 01:59:26 +0200371 pm_qos_remove_request(req);
James Bottomley82f68252010-07-05 22:53:06 +0200372 kfree(req);
Mark Grossd82b3512008-02-04 22:30:08 -0800373
374 return 0;
375}
376
Mark Grossed771342010-05-06 01:59:26 +0200377
Mark Grossd82b3512008-02-04 22:30:08 -0800378static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
379 size_t count, loff_t *f_pos)
380{
381 s32 value;
Mark Grossed771342010-05-06 01:59:26 +0200382 int x;
383 char ascii_value[11];
384 struct pm_qos_request_list *pm_qos_req;
Mark Grossd82b3512008-02-04 22:30:08 -0800385
Mark Grossed771342010-05-06 01:59:26 +0200386 if (count == sizeof(s32)) {
387 if (copy_from_user(&value, buf, sizeof(s32)))
388 return -EFAULT;
389 } else if (count == 11) { /* len('0x12345678/0') */
390 if (copy_from_user(ascii_value, buf, 11))
391 return -EFAULT;
mark gross0109c2c2010-09-09 23:20:09 +0200392 if (strlen(ascii_value) != 10)
393 return -EINVAL;
Mark Grossed771342010-05-06 01:59:26 +0200394 x = sscanf(ascii_value, "%x", &value);
395 if (x != 1)
396 return -EINVAL;
mark gross0109c2c2010-09-09 23:20:09 +0200397 pr_debug("%s, %d, 0x%x\n", ascii_value, x, value);
Mark Grossed771342010-05-06 01:59:26 +0200398 } else
Mark Grossd82b3512008-02-04 22:30:08 -0800399 return -EINVAL;
Mark Grossd82b3512008-02-04 22:30:08 -0800400
Mark Grossed771342010-05-06 01:59:26 +0200401 pm_qos_req = (struct pm_qos_request_list *)filp->private_data;
402 pm_qos_update_request(pm_qos_req, value);
403
404 return count;
Mark Grossd82b3512008-02-04 22:30:08 -0800405}
406
407
408static int __init pm_qos_power_init(void)
409{
410 int ret = 0;
411
412 ret = register_pm_qos_misc(&cpu_dma_pm_qos);
413 if (ret < 0) {
414 printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n");
415 return ret;
416 }
417 ret = register_pm_qos_misc(&network_lat_pm_qos);
418 if (ret < 0) {
419 printk(KERN_ERR "pm_qos_param: network_latency setup failed\n");
420 return ret;
421 }
422 ret = register_pm_qos_misc(&network_throughput_pm_qos);
423 if (ret < 0)
424 printk(KERN_ERR
425 "pm_qos_param: network_throughput setup failed\n");
426
427 return ret;
428}
429
430late_initcall(pm_qos_power_init);