blob: db8e51d7f392ec19c9902279259073c178322f05 [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>
James Bottomley5f279842010-07-19 02:00:18 +020033#include <linux/plist.h>
Mark Grossd82b3512008-02-04 22:30:08 -080034#include <linux/sched.h>
35#include <linux/spinlock.h>
36#include <linux/slab.h>
37#include <linux/time.h>
38#include <linux/fs.h>
39#include <linux/device.h>
40#include <linux/miscdevice.h>
41#include <linux/string.h>
42#include <linux/platform_device.h>
43#include <linux/init.h>
44
45#include <linux/uaccess.h>
46
47/*
Mark Grossed771342010-05-06 01:59:26 +020048 * locking rule: all changes to requests or notifiers lists
Mark Grossd82b3512008-02-04 22:30:08 -080049 * 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 */
Mark Grossed771342010-05-06 01:59:26 +020052struct pm_qos_request_list {
James Bottomley5f279842010-07-19 02:00:18 +020053 struct plist_node list;
Mark Grossed771342010-05-06 01:59:26 +020054 int pm_qos_class;
Mark Grossd82b3512008-02-04 22:30:08 -080055};
56
James Bottomley5f279842010-07-19 02:00:18 +020057enum pm_qos_type {
58 PM_QOS_MAX, /* return the largest value */
59 PM_QOS_MIN /* return the smallest value */
60};
Mark Grossd82b3512008-02-04 22:30:08 -080061
62struct pm_qos_object {
James Bottomley5f279842010-07-19 02:00:18 +020063 struct plist_head requests;
Mark Grossd82b3512008-02-04 22:30:08 -080064 struct blocking_notifier_head *notifiers;
65 struct miscdevice pm_qos_power_miscdev;
66 char *name;
67 s32 default_value;
James Bottomley5f279842010-07-19 02:00:18 +020068 enum pm_qos_type type;
Mark Grossd82b3512008-02-04 22:30:08 -080069};
70
James Bottomley5f279842010-07-19 02:00:18 +020071static DEFINE_SPINLOCK(pm_qos_lock);
72
Mark Grossd82b3512008-02-04 22:30:08 -080073static struct pm_qos_object null_pm_qos;
74static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
75static struct pm_qos_object cpu_dma_pm_qos = {
James Bottomley5f279842010-07-19 02:00:18 +020076 .requests = PLIST_HEAD_INIT(cpu_dma_pm_qos.requests, pm_qos_lock),
Mark Grossd82b3512008-02-04 22:30:08 -080077 .notifiers = &cpu_dma_lat_notifier,
78 .name = "cpu_dma_latency",
79 .default_value = 2000 * USEC_PER_SEC,
James Bottomley5f279842010-07-19 02:00:18 +020080 .type = PM_QOS_MIN,
Mark Grossd82b3512008-02-04 22:30:08 -080081};
82
83static BLOCKING_NOTIFIER_HEAD(network_lat_notifier);
84static struct pm_qos_object network_lat_pm_qos = {
James Bottomley5f279842010-07-19 02:00:18 +020085 .requests = PLIST_HEAD_INIT(network_lat_pm_qos.requests, pm_qos_lock),
Mark Grossd82b3512008-02-04 22:30:08 -080086 .notifiers = &network_lat_notifier,
87 .name = "network_latency",
88 .default_value = 2000 * USEC_PER_SEC,
James Bottomley5f279842010-07-19 02:00:18 +020089 .type = PM_QOS_MIN
Mark Grossd82b3512008-02-04 22:30:08 -080090};
91
92
93static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier);
94static struct pm_qos_object network_throughput_pm_qos = {
James Bottomley5f279842010-07-19 02:00:18 +020095 .requests = PLIST_HEAD_INIT(network_throughput_pm_qos.requests, pm_qos_lock),
Mark Grossd82b3512008-02-04 22:30:08 -080096 .notifiers = &network_throughput_notifier,
97 .name = "network_throughput",
98 .default_value = 0,
James Bottomley5f279842010-07-19 02:00:18 +020099 .type = PM_QOS_MAX,
Mark Grossd82b3512008-02-04 22:30:08 -0800100};
101
102
103static struct pm_qos_object *pm_qos_array[] = {
104 &null_pm_qos,
105 &cpu_dma_pm_qos,
106 &network_lat_pm_qos,
107 &network_throughput_pm_qos
108};
109
Mark Grossd82b3512008-02-04 22:30:08 -0800110static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
111 size_t count, loff_t *f_pos);
112static int pm_qos_power_open(struct inode *inode, struct file *filp);
113static int pm_qos_power_release(struct inode *inode, struct file *filp);
114
115static const struct file_operations pm_qos_power_fops = {
116 .write = pm_qos_power_write,
117 .open = pm_qos_power_open,
118 .release = pm_qos_power_release,
119};
120
James Bottomley5f279842010-07-19 02:00:18 +0200121/* unlocked internal variant */
122static inline int pm_qos_get_value(struct pm_qos_object *o)
Mark Grossd82b3512008-02-04 22:30:08 -0800123{
James Bottomley5f279842010-07-19 02:00:18 +0200124 if (plist_head_empty(&o->requests))
125 return o->default_value;
126
127 switch (o->type) {
128 case PM_QOS_MIN:
129 return plist_last(&o->requests)->prio;
130
131 case PM_QOS_MAX:
132 return plist_first(&o->requests)->prio;
133
134 default:
135 /* runtime check for not using enum */
136 BUG();
137 }
Mark Grossd82b3512008-02-04 22:30:08 -0800138}
139
James Bottomley5f279842010-07-19 02:00:18 +0200140static void update_target(struct pm_qos_object *o, struct plist_node *node,
141 int del, int value)
Mark Grossd82b3512008-02-04 22:30:08 -0800142{
Mark Grossd82b3512008-02-04 22:30:08 -0800143 unsigned long flags;
James Bottomley5f279842010-07-19 02:00:18 +0200144 int prev_value, curr_value;
Mark Grossd82b3512008-02-04 22:30:08 -0800145
146 spin_lock_irqsave(&pm_qos_lock, flags);
James Bottomley5f279842010-07-19 02:00:18 +0200147 prev_value = pm_qos_get_value(o);
148 /* PM_QOS_DEFAULT_VALUE is a signal that the value is unchanged */
149 if (value != PM_QOS_DEFAULT_VALUE) {
150 /*
151 * to change the list, we atomically remove, reinit
152 * with new value and add, then see if the extremal
153 * changed
154 */
155 plist_del(node, &o->requests);
156 plist_node_init(node, value);
157 plist_add(node, &o->requests);
158 } else if (del) {
159 plist_del(node, &o->requests);
160 } else {
161 plist_add(node, &o->requests);
Mark Grossd82b3512008-02-04 22:30:08 -0800162 }
James Bottomley5f279842010-07-19 02:00:18 +0200163 curr_value = pm_qos_get_value(o);
Mark Grossd82b3512008-02-04 22:30:08 -0800164 spin_unlock_irqrestore(&pm_qos_lock, flags);
165
James Bottomley5f279842010-07-19 02:00:18 +0200166 if (prev_value != curr_value)
167 blocking_notifier_call_chain(o->notifiers,
168 (unsigned long)curr_value,
169 NULL);
Mark Grossd82b3512008-02-04 22:30:08 -0800170}
171
172static int register_pm_qos_misc(struct pm_qos_object *qos)
173{
174 qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR;
175 qos->pm_qos_power_miscdev.name = qos->name;
176 qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops;
177
178 return misc_register(&qos->pm_qos_power_miscdev);
179}
180
181static int find_pm_qos_object_by_minor(int minor)
182{
183 int pm_qos_class;
184
185 for (pm_qos_class = 0;
186 pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) {
187 if (minor ==
188 pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor)
189 return pm_qos_class;
190 }
191 return -1;
192}
193
194/**
Mark Grossed771342010-05-06 01:59:26 +0200195 * pm_qos_request - returns current system wide qos expectation
Mark Grossd82b3512008-02-04 22:30:08 -0800196 * @pm_qos_class: identification of which qos value is requested
197 *
198 * This function returns the current target value in an atomic manner.
199 */
Mark Grossed771342010-05-06 01:59:26 +0200200int pm_qos_request(int pm_qos_class)
Mark Grossd82b3512008-02-04 22:30:08 -0800201{
James Bottomley5f279842010-07-19 02:00:18 +0200202 unsigned long flags;
203 int value;
204
205 spin_lock_irqsave(&pm_qos_lock, flags);
206 value = pm_qos_get_value(pm_qos_array[pm_qos_class]);
207 spin_unlock_irqrestore(&pm_qos_lock, flags);
208
209 return value;
Mark Grossd82b3512008-02-04 22:30:08 -0800210}
Mark Grossed771342010-05-06 01:59:26 +0200211EXPORT_SYMBOL_GPL(pm_qos_request);
Mark Grossd82b3512008-02-04 22:30:08 -0800212
213/**
Mark Grossed771342010-05-06 01:59:26 +0200214 * pm_qos_add_request - inserts new qos request into the list
Mark Grossd82b3512008-02-04 22:30:08 -0800215 * @pm_qos_class: identifies which list of qos request to us
Mark Grossd82b3512008-02-04 22:30:08 -0800216 * @value: defines the qos request
217 *
218 * This function inserts a new entry in the pm_qos_class list of requested qos
Richard Hughesbf1db692008-08-05 13:01:35 -0700219 * performance characteristics. It recomputes the aggregate QoS expectations
Mark Grossed771342010-05-06 01:59:26 +0200220 * 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.
Mark Grossd82b3512008-02-04 22:30:08 -0800223 */
Mark Grossed771342010-05-06 01:59:26 +0200224struct pm_qos_request_list *pm_qos_add_request(int pm_qos_class, s32 value)
Mark Grossd82b3512008-02-04 22:30:08 -0800225{
Mark Grossed771342010-05-06 01:59:26 +0200226 struct pm_qos_request_list *dep;
Mark Grossd82b3512008-02-04 22:30:08 -0800227
Mark Grossed771342010-05-06 01:59:26 +0200228 dep = kzalloc(sizeof(struct pm_qos_request_list), GFP_KERNEL);
Mark Grossd82b3512008-02-04 22:30:08 -0800229 if (dep) {
James Bottomley5f279842010-07-19 02:00:18 +0200230 struct pm_qos_object *o = pm_qos_array[pm_qos_class];
231 int new_value;
Mark Grossd82b3512008-02-04 22:30:08 -0800232
James Bottomley5f279842010-07-19 02:00:18 +0200233 if (value == PM_QOS_DEFAULT_VALUE)
234 new_value = o->default_value;
235 else
236 new_value = value;
237 plist_node_init(&dep->list, new_value);
238 dep->pm_qos_class = pm_qos_class;
239 update_target(o, &dep->list, 0, PM_QOS_DEFAULT_VALUE);
Mark Grossd82b3512008-02-04 22:30:08 -0800240 }
241
Mark Grossed771342010-05-06 01:59:26 +0200242 return dep;
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 Bottomley5f279842010-07-19 02:00:18 +0200265 o = pm_qos_array[pm_qos_req->pm_qos_class];
266
267 if (new_value == PM_QOS_DEFAULT_VALUE)
268 temp = o->default_value;
269 else
270 temp = new_value;
271
272 if (temp != pm_qos_req->list.prio)
273 update_target(o, &pm_qos_req->list, 0, temp);
Mark Grossd82b3512008-02-04 22:30:08 -0800274}
Mark Grossed771342010-05-06 01:59:26 +0200275EXPORT_SYMBOL_GPL(pm_qos_update_request);
Mark Grossd82b3512008-02-04 22:30:08 -0800276
277/**
Mark Grossed771342010-05-06 01:59:26 +0200278 * pm_qos_remove_request - modifies an existing qos request
279 * @pm_qos_req: handle to request list element
Mark Grossd82b3512008-02-04 22:30:08 -0800280 *
Mark Grossed771342010-05-06 01:59:26 +0200281 * Will remove pm qos request from the list of requests and
282 * recompute the current target value for the pm_qos_class. Call this
283 * on slow code paths.
Mark Grossd82b3512008-02-04 22:30:08 -0800284 */
Mark Grossed771342010-05-06 01:59:26 +0200285void pm_qos_remove_request(struct pm_qos_request_list *pm_qos_req)
Mark Grossd82b3512008-02-04 22:30:08 -0800286{
James Bottomley5f279842010-07-19 02:00:18 +0200287 struct pm_qos_object *o;
Mark Grossd82b3512008-02-04 22:30:08 -0800288
Mark Grossed771342010-05-06 01:59:26 +0200289 if (pm_qos_req == NULL)
290 return;
291 /* silent return to keep pcm code cleaner */
292
James Bottomley5f279842010-07-19 02:00:18 +0200293 o = pm_qos_array[pm_qos_req->pm_qos_class];
294 update_target(o, &pm_qos_req->list, 1, PM_QOS_DEFAULT_VALUE);
Mark Grossed771342010-05-06 01:59:26 +0200295 kfree(pm_qos_req);
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);