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 */
57 * Note: The lockless read path depends on the CPU accessing
58 * target_value atomically. Atomic access is only guaranteed on all CPU
59 * types linux supports for 32 bit quantites
61 struct pm_qos_object {
62 struct plist_head requests;
63 struct blocking_notifier_head *notifiers;
64 struct miscdevice pm_qos_power_miscdev;
66 s32 target_value; /* Do not change to 64 bit */
68 enum pm_qos_type type;
71 static DEFINE_SPINLOCK(pm_qos_lock);
73 static struct pm_qos_object null_pm_qos;
74 static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
75 static struct pm_qos_object cpu_dma_pm_qos = {
76 .requests = PLIST_HEAD_INIT(cpu_dma_pm_qos.requests, pm_qos_lock),
77 .notifiers = &cpu_dma_lat_notifier,
78 .name = "cpu_dma_latency",
79 .target_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE,
80 .default_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE,
84 static BLOCKING_NOTIFIER_HEAD(network_lat_notifier);
85 static struct pm_qos_object network_lat_pm_qos = {
86 .requests = PLIST_HEAD_INIT(network_lat_pm_qos.requests, pm_qos_lock),
87 .notifiers = &network_lat_notifier,
88 .name = "network_latency",
89 .target_value = PM_QOS_NETWORK_LAT_DEFAULT_VALUE,
90 .default_value = PM_QOS_NETWORK_LAT_DEFAULT_VALUE,
95 static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier);
96 static struct pm_qos_object network_throughput_pm_qos = {
97 .requests = PLIST_HEAD_INIT(network_throughput_pm_qos.requests, pm_qos_lock),
98 .notifiers = &network_throughput_notifier,
99 .name = "network_throughput",
100 .target_value = PM_QOS_NETWORK_THROUGHPUT_DEFAULT_VALUE,
101 .default_value = PM_QOS_NETWORK_THROUGHPUT_DEFAULT_VALUE,
106 static struct pm_qos_object *pm_qos_array[] = {
110 &network_throughput_pm_qos
113 static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
114 size_t count, loff_t *f_pos);
115 static int pm_qos_power_open(struct inode *inode, struct file *filp);
116 static int pm_qos_power_release(struct inode *inode, struct file *filp);
118 static const struct file_operations pm_qos_power_fops = {
119 .write = pm_qos_power_write,
120 .open = pm_qos_power_open,
121 .release = pm_qos_power_release,
122 .llseek = noop_llseek,
125 /* unlocked internal variant */
126 static inline int pm_qos_get_value(struct pm_qos_object *o)
128 if (plist_head_empty(&o->requests))
129 return o->default_value;
133 return plist_first(&o->requests)->prio;
136 return plist_last(&o->requests)->prio;
139 /* runtime check for not using enum */
144 static inline s32 pm_qos_read_value(struct pm_qos_object *o)
146 return o->target_value;
149 static inline void pm_qos_set_value(struct pm_qos_object *o, s32 value)
151 o->target_value = value;
154 static void update_target(struct pm_qos_object *o, struct plist_node *node,
158 int prev_value, curr_value;
160 spin_lock_irqsave(&pm_qos_lock, flags);
161 prev_value = pm_qos_get_value(o);
162 /* PM_QOS_DEFAULT_VALUE is a signal that the value is unchanged */
163 if (value != PM_QOS_DEFAULT_VALUE) {
165 * to change the list, we atomically remove, reinit
166 * with new value and add, then see if the extremal
169 plist_del(node, &o->requests);
170 plist_node_init(node, value);
171 plist_add(node, &o->requests);
173 plist_del(node, &o->requests);
175 plist_add(node, &o->requests);
177 curr_value = pm_qos_get_value(o);
178 pm_qos_set_value(o, curr_value);
179 spin_unlock_irqrestore(&pm_qos_lock, flags);
181 if (prev_value != curr_value)
182 blocking_notifier_call_chain(o->notifiers,
183 (unsigned long)curr_value,
187 static int register_pm_qos_misc(struct pm_qos_object *qos)
189 qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR;
190 qos->pm_qos_power_miscdev.name = qos->name;
191 qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops;
193 return misc_register(&qos->pm_qos_power_miscdev);
196 static int find_pm_qos_object_by_minor(int minor)
200 for (pm_qos_class = 0;
201 pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) {
203 pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor)
210 * pm_qos_request - returns current system wide qos expectation
211 * @pm_qos_class: identification of which qos value is requested
213 * This function returns the current target value.
215 int pm_qos_request(int pm_qos_class)
217 return pm_qos_read_value(pm_qos_array[pm_qos_class]);
219 EXPORT_SYMBOL_GPL(pm_qos_request);
221 int pm_qos_request_active(struct pm_qos_request_list *req)
223 return req->pm_qos_class != 0;
225 EXPORT_SYMBOL_GPL(pm_qos_request_active);
228 * pm_qos_add_request - inserts new qos request into the list
229 * @dep: pointer to a preallocated handle
230 * @pm_qos_class: identifies which list of qos request to use
231 * @value: defines the qos request
233 * This function inserts a new entry in the pm_qos_class list of requested qos
234 * performance characteristics. It recomputes the aggregate QoS expectations
235 * for the pm_qos_class of parameters and initializes the pm_qos_request_list
236 * handle. Caller needs to save this handle for later use in updates and
240 void pm_qos_add_request(struct pm_qos_request_list *dep,
241 int pm_qos_class, s32 value)
243 struct pm_qos_object *o = pm_qos_array[pm_qos_class];
246 if (pm_qos_request_active(dep)) {
247 WARN(1, KERN_ERR "pm_qos_add_request() called for already added request\n");
250 if (value == PM_QOS_DEFAULT_VALUE)
251 new_value = o->default_value;
254 plist_node_init(&dep->list, new_value);
255 dep->pm_qos_class = pm_qos_class;
256 update_target(o, &dep->list, 0, PM_QOS_DEFAULT_VALUE);
258 EXPORT_SYMBOL_GPL(pm_qos_add_request);
261 * pm_qos_update_request - modifies an existing qos request
262 * @pm_qos_req : handle to list element holding a pm_qos request to use
263 * @value: defines the qos request
265 * Updates an existing qos request for the pm_qos_class of parameters along
266 * with updating the target pm_qos_class value.
268 * Attempts are made to make this code callable on hot code paths.
270 void pm_qos_update_request(struct pm_qos_request_list *pm_qos_req,
274 struct pm_qos_object *o;
276 if (!pm_qos_req) /*guard against callers passing in null */
279 if (!pm_qos_request_active(pm_qos_req)) {
280 WARN(1, KERN_ERR "pm_qos_update_request() called for unknown object\n");
284 o = pm_qos_array[pm_qos_req->pm_qos_class];
286 if (new_value == PM_QOS_DEFAULT_VALUE)
287 temp = o->default_value;
291 if (temp != pm_qos_req->list.prio)
292 update_target(o, &pm_qos_req->list, 0, temp);
294 EXPORT_SYMBOL_GPL(pm_qos_update_request);
297 * pm_qos_remove_request - modifies an existing qos request
298 * @pm_qos_req: handle to request list element
300 * Will remove pm qos request from the list of requests and
301 * recompute the current target value for the pm_qos_class. Call this
302 * on slow code paths.
304 void pm_qos_remove_request(struct pm_qos_request_list *pm_qos_req)
306 struct pm_qos_object *o;
308 if (pm_qos_req == NULL)
310 /* silent return to keep pcm code cleaner */
312 if (!pm_qos_request_active(pm_qos_req)) {
313 WARN(1, KERN_ERR "pm_qos_remove_request() called for unknown object\n");
317 o = pm_qos_array[pm_qos_req->pm_qos_class];
318 update_target(o, &pm_qos_req->list, 1, PM_QOS_DEFAULT_VALUE);
319 memset(pm_qos_req, 0, sizeof(*pm_qos_req));
321 EXPORT_SYMBOL_GPL(pm_qos_remove_request);
324 * pm_qos_add_notifier - sets notification entry for changes to target value
325 * @pm_qos_class: identifies which qos target changes should be notified.
326 * @notifier: notifier block managed by caller.
328 * will register the notifier into a notification chain that gets called
329 * upon changes to the pm_qos_class target value.
331 int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
335 retval = blocking_notifier_chain_register(
336 pm_qos_array[pm_qos_class]->notifiers, notifier);
340 EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
343 * pm_qos_remove_notifier - deletes notification entry from chain.
344 * @pm_qos_class: identifies which qos target changes are notified.
345 * @notifier: notifier block to be removed.
347 * will remove the notifier from the notification chain that gets called
348 * upon changes to the pm_qos_class target value.
350 int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
354 retval = blocking_notifier_chain_unregister(
355 pm_qos_array[pm_qos_class]->notifiers, notifier);
359 EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
361 static int pm_qos_power_open(struct inode *inode, struct file *filp)
365 pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
366 if (pm_qos_class >= 0) {
367 struct pm_qos_request_list *req = kzalloc(sizeof(*req), GFP_KERNEL);
371 pm_qos_add_request(req, pm_qos_class, PM_QOS_DEFAULT_VALUE);
372 filp->private_data = req;
374 if (filp->private_data)
380 static int pm_qos_power_release(struct inode *inode, struct file *filp)
382 struct pm_qos_request_list *req;
384 req = filp->private_data;
385 pm_qos_remove_request(req);
392 static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
393 size_t count, loff_t *f_pos)
397 char ascii_value[11];
398 struct pm_qos_request_list *pm_qos_req;
400 if (count == sizeof(s32)) {
401 if (copy_from_user(&value, buf, sizeof(s32)))
403 } else if (count == 11) { /* len('0x12345678/0') */
404 if (copy_from_user(ascii_value, buf, 11))
406 if (strlen(ascii_value) != 10)
408 x = sscanf(ascii_value, "%x", &value);
411 pr_debug("%s, %d, 0x%x\n", ascii_value, x, value);
415 pm_qos_req = filp->private_data;
416 pm_qos_update_request(pm_qos_req, value);
422 static int __init pm_qos_power_init(void)
426 ret = register_pm_qos_misc(&cpu_dma_pm_qos);
428 printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n");
431 ret = register_pm_qos_misc(&network_lat_pm_qos);
433 printk(KERN_ERR "pm_qos_param: network_latency setup failed\n");
436 ret = register_pm_qos_misc(&network_throughput_pm_qos);
439 "pm_qos_param: network_throughput setup failed\n");
444 late_initcall(pm_qos_power_init);