2 * This module exposes the interface to kernel space for specifying
3 * QoS dependencies. It provides infrastructure for registration of:
5 * Dependents on a QoS value : register requests
6 * Watchers of QoS value : get notified when target QoS value changes
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.
11 * There are 3 basic classes of QoS parameter: latency, timeout, throughput
12 * each have defined units:
14 * timeout: usec <-- currently not used.
15 * throughput: kbs (kilo byte / sec)
17 * There are lists of pm_qos_objects each one wrapping requests, notifiers
19 * User mode requests 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 * 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
25 * pointer or exits the pm_qos_object will get an opportunity to clean up.
27 * Mark Gross <mgross@linux.intel.com>
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>
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>
44 #include <linux/uaccess.h>
47 * locking rule: all changes to requests or notifiers lists
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
52 PM_QOS_MAX, /* return the largest value */
53 PM_QOS_MIN /* return the smallest value */
56 struct pm_qos_object {
57 struct plist_head requests;
58 struct blocking_notifier_head *notifiers;
59 struct miscdevice pm_qos_power_miscdev;
62 enum pm_qos_type type;
65 static DEFINE_SPINLOCK(pm_qos_lock);
67 static struct pm_qos_object null_pm_qos;
68 static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
69 static struct pm_qos_object cpu_dma_pm_qos = {
70 .requests = PLIST_HEAD_INIT(cpu_dma_pm_qos.requests, pm_qos_lock),
71 .notifiers = &cpu_dma_lat_notifier,
72 .name = "cpu_dma_latency",
73 .default_value = 2000 * USEC_PER_SEC,
77 static BLOCKING_NOTIFIER_HEAD(network_lat_notifier);
78 static struct pm_qos_object network_lat_pm_qos = {
79 .requests = PLIST_HEAD_INIT(network_lat_pm_qos.requests, pm_qos_lock),
80 .notifiers = &network_lat_notifier,
81 .name = "network_latency",
82 .default_value = 2000 * USEC_PER_SEC,
87 static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier);
88 static struct pm_qos_object network_throughput_pm_qos = {
89 .requests = PLIST_HEAD_INIT(network_throughput_pm_qos.requests, pm_qos_lock),
90 .notifiers = &network_throughput_notifier,
91 .name = "network_throughput",
97 static struct pm_qos_object *pm_qos_array[] = {
101 &network_throughput_pm_qos
104 static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
105 size_t count, loff_t *f_pos);
106 static int pm_qos_power_open(struct inode *inode, struct file *filp);
107 static int pm_qos_power_release(struct inode *inode, struct file *filp);
109 static 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,
115 /* unlocked internal variant */
116 static inline int pm_qos_get_value(struct pm_qos_object *o)
118 if (plist_head_empty(&o->requests))
119 return o->default_value;
123 return plist_last(&o->requests)->prio;
126 return plist_first(&o->requests)->prio;
129 /* runtime check for not using enum */
134 static void update_target(struct pm_qos_object *o, struct plist_node *node,
138 int prev_value, curr_value;
140 spin_lock_irqsave(&pm_qos_lock, flags);
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) {
145 * to change the list, we atomically remove, reinit
146 * with new value and add, then see if the extremal
149 plist_del(node, &o->requests);
150 plist_node_init(node, value);
151 plist_add(node, &o->requests);
153 plist_del(node, &o->requests);
155 plist_add(node, &o->requests);
157 curr_value = pm_qos_get_value(o);
158 spin_unlock_irqrestore(&pm_qos_lock, flags);
160 if (prev_value != curr_value)
161 blocking_notifier_call_chain(o->notifiers,
162 (unsigned long)curr_value,
166 static int register_pm_qos_misc(struct pm_qos_object *qos)
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;
172 return misc_register(&qos->pm_qos_power_miscdev);
175 static int find_pm_qos_object_by_minor(int minor)
179 for (pm_qos_class = 0;
180 pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) {
182 pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor)
189 * pm_qos_request - returns current system wide qos expectation
190 * @pm_qos_class: identification of which qos value is requested
192 * This function returns the current target value in an atomic manner.
194 int pm_qos_request(int pm_qos_class)
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);
205 EXPORT_SYMBOL_GPL(pm_qos_request);
207 int pm_qos_request_active(struct pm_qos_request_list *req)
209 return req->pm_qos_class != 0;
211 EXPORT_SYMBOL_GPL(pm_qos_request_active);
214 * pm_qos_add_request - inserts new qos request into the list
215 * @dep: pointer to a preallocated handle
216 * @pm_qos_class: identifies which list of qos request to use
217 * @value: defines the qos request
219 * This function inserts a new entry in the pm_qos_class list of requested qos
220 * performance characteristics. It recomputes the aggregate QoS expectations
221 * 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
226 void pm_qos_add_request(struct pm_qos_request_list *dep,
227 int pm_qos_class, s32 value)
229 struct pm_qos_object *o = pm_qos_array[pm_qos_class];
232 if (pm_qos_request_active(dep)) {
233 WARN(1, KERN_ERR "pm_qos_add_request() called for already added request\n");
236 if (value == PM_QOS_DEFAULT_VALUE)
237 new_value = o->default_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);
244 EXPORT_SYMBOL_GPL(pm_qos_add_request);
247 * pm_qos_update_request - modifies an existing qos request
248 * @pm_qos_req : handle to list element holding a pm_qos request to use
249 * @value: defines the qos request
251 * Updates an existing qos request for the pm_qos_class of parameters along
252 * with updating the target pm_qos_class value.
254 * Attempts are made to make this code callable on hot code paths.
256 void pm_qos_update_request(struct pm_qos_request_list *pm_qos_req,
260 struct pm_qos_object *o;
262 if (!pm_qos_req) /*guard against callers passing in null */
265 if (!pm_qos_request_active(pm_qos_req)) {
266 WARN(1, KERN_ERR "pm_qos_update_request() called for unknown object\n");
270 o = pm_qos_array[pm_qos_req->pm_qos_class];
272 if (new_value == PM_QOS_DEFAULT_VALUE)
273 temp = o->default_value;
277 if (temp != pm_qos_req->list.prio)
278 update_target(o, &pm_qos_req->list, 0, temp);
280 EXPORT_SYMBOL_GPL(pm_qos_update_request);
283 * pm_qos_remove_request - modifies an existing qos request
284 * @pm_qos_req: handle to request list element
286 * 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.
290 void pm_qos_remove_request(struct pm_qos_request_list *pm_qos_req)
292 struct pm_qos_object *o;
294 if (pm_qos_req == NULL)
296 /* silent return to keep pcm code cleaner */
298 if (!pm_qos_request_active(pm_qos_req)) {
299 WARN(1, KERN_ERR "pm_qos_remove_request() called for unknown object\n");
303 o = pm_qos_array[pm_qos_req->pm_qos_class];
304 update_target(o, &pm_qos_req->list, 1, PM_QOS_DEFAULT_VALUE);
305 memset(pm_qos_req, 0, sizeof(*pm_qos_req));
307 EXPORT_SYMBOL_GPL(pm_qos_remove_request);
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.
314 * will register the notifier into a notification chain that gets called
315 * upon changes to the pm_qos_class target value.
317 int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
321 retval = blocking_notifier_chain_register(
322 pm_qos_array[pm_qos_class]->notifiers, notifier);
326 EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
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.
333 * will remove the notifier from the notification chain that gets called
334 * upon changes to the pm_qos_class target value.
336 int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
340 retval = blocking_notifier_chain_unregister(
341 pm_qos_array[pm_qos_class]->notifiers, notifier);
345 EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
347 static int pm_qos_power_open(struct inode *inode, struct file *filp)
351 pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
352 if (pm_qos_class >= 0) {
353 struct pm_qos_request_list *req = kzalloc(sizeof(*req), GFP_KERNEL);
357 pm_qos_add_request(req, pm_qos_class, PM_QOS_DEFAULT_VALUE);
358 filp->private_data = req;
360 if (filp->private_data)
366 static int pm_qos_power_release(struct inode *inode, struct file *filp)
368 struct pm_qos_request_list *req;
370 req = filp->private_data;
371 pm_qos_remove_request(req);
378 static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
379 size_t count, loff_t *f_pos)
383 char ascii_value[11];
384 struct pm_qos_request_list *pm_qos_req;
386 if (count == sizeof(s32)) {
387 if (copy_from_user(&value, buf, sizeof(s32)))
389 } else if (count == 11) { /* len('0x12345678/0') */
390 if (copy_from_user(ascii_value, buf, 11))
392 x = sscanf(ascii_value, "%x", &value);
395 pr_debug(KERN_ERR "%s, %d, 0x%x\n", ascii_value, x, value);
399 pm_qos_req = (struct pm_qos_request_list *)filp->private_data;
400 pm_qos_update_request(pm_qos_req, value);
406 static int __init pm_qos_power_init(void)
410 ret = register_pm_qos_misc(&cpu_dma_pm_qos);
412 printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n");
415 ret = register_pm_qos_misc(&network_lat_pm_qos);
417 printk(KERN_ERR "pm_qos_param: network_latency setup failed\n");
420 ret = register_pm_qos_misc(&network_throughput_pm_qos);
423 "pm_qos_param: network_throughput setup failed\n");
428 late_initcall(pm_qos_power_init);