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>
43 #include <linux/kernel.h>
45 #include <linux/uaccess.h>
48 * locking rule: all changes to requests or notifiers lists
49 * 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
53 PM_QOS_MAX
, /* return the largest value */
54 PM_QOS_MIN
/* return the smallest value */
57 struct pm_qos_object
{
58 struct plist_head requests
;
59 struct blocking_notifier_head
*notifiers
;
60 struct miscdevice pm_qos_power_miscdev
;
63 enum pm_qos_type type
;
66 static DEFINE_SPINLOCK(pm_qos_lock
);
68 static struct pm_qos_object null_pm_qos
;
69 static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier
);
70 static struct pm_qos_object cpu_dma_pm_qos
= {
71 .requests
= PLIST_HEAD_INIT(cpu_dma_pm_qos
.requests
, pm_qos_lock
),
72 .notifiers
= &cpu_dma_lat_notifier
,
73 .name
= "cpu_dma_latency",
74 .default_value
= 2000 * USEC_PER_SEC
,
78 static BLOCKING_NOTIFIER_HEAD(network_lat_notifier
);
79 static struct pm_qos_object network_lat_pm_qos
= {
80 .requests
= PLIST_HEAD_INIT(network_lat_pm_qos
.requests
, pm_qos_lock
),
81 .notifiers
= &network_lat_notifier
,
82 .name
= "network_latency",
83 .default_value
= 2000 * USEC_PER_SEC
,
88 static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier
);
89 static struct pm_qos_object network_throughput_pm_qos
= {
90 .requests
= PLIST_HEAD_INIT(network_throughput_pm_qos
.requests
, pm_qos_lock
),
91 .notifiers
= &network_throughput_notifier
,
92 .name
= "network_throughput",
98 static struct pm_qos_object
*pm_qos_array
[] = {
102 &network_throughput_pm_qos
105 static ssize_t
pm_qos_power_write(struct file
*filp
, const char __user
*buf
,
106 size_t count
, loff_t
*f_pos
);
107 static ssize_t
pm_qos_power_read(struct file
*filp
, char __user
*buf
,
108 size_t count
, loff_t
*f_pos
);
109 static int pm_qos_power_open(struct inode
*inode
, struct file
*filp
);
110 static int pm_qos_power_release(struct inode
*inode
, struct file
*filp
);
112 static const struct file_operations pm_qos_power_fops
= {
113 .write
= pm_qos_power_write
,
114 .read
= pm_qos_power_read
,
115 .open
= pm_qos_power_open
,
116 .release
= pm_qos_power_release
,
117 .llseek
= noop_llseek
,
120 /* unlocked internal variant */
121 static inline int pm_qos_get_value(struct pm_qos_object
*o
)
123 if (plist_head_empty(&o
->requests
))
124 return o
->default_value
;
128 return plist_first(&o
->requests
)->prio
;
131 return plist_last(&o
->requests
)->prio
;
134 /* runtime check for not using enum */
139 static void update_target(struct pm_qos_object
*o
, struct plist_node
*node
,
143 int prev_value
, curr_value
;
145 spin_lock_irqsave(&pm_qos_lock
, flags
);
146 prev_value
= pm_qos_get_value(o
);
147 /* PM_QOS_DEFAULT_VALUE is a signal that the value is unchanged */
148 if (value
!= PM_QOS_DEFAULT_VALUE
) {
150 * to change the list, we atomically remove, reinit
151 * with new value and add, then see if the extremal
154 plist_del(node
, &o
->requests
);
155 plist_node_init(node
, value
);
156 plist_add(node
, &o
->requests
);
158 plist_del(node
, &o
->requests
);
160 plist_add(node
, &o
->requests
);
162 curr_value
= pm_qos_get_value(o
);
163 spin_unlock_irqrestore(&pm_qos_lock
, flags
);
165 if (prev_value
!= curr_value
)
166 blocking_notifier_call_chain(o
->notifiers
,
167 (unsigned long)curr_value
,
171 static int register_pm_qos_misc(struct pm_qos_object
*qos
)
173 qos
->pm_qos_power_miscdev
.minor
= MISC_DYNAMIC_MINOR
;
174 qos
->pm_qos_power_miscdev
.name
= qos
->name
;
175 qos
->pm_qos_power_miscdev
.fops
= &pm_qos_power_fops
;
177 return misc_register(&qos
->pm_qos_power_miscdev
);
180 static int find_pm_qos_object_by_minor(int minor
)
184 for (pm_qos_class
= 0;
185 pm_qos_class
< PM_QOS_NUM_CLASSES
; pm_qos_class
++) {
187 pm_qos_array
[pm_qos_class
]->pm_qos_power_miscdev
.minor
)
194 * pm_qos_request - returns current system wide qos expectation
195 * @pm_qos_class: identification of which qos value is requested
197 * This function returns the current target value in an atomic manner.
199 int pm_qos_request(int pm_qos_class
)
204 spin_lock_irqsave(&pm_qos_lock
, flags
);
205 value
= pm_qos_get_value(pm_qos_array
[pm_qos_class
]);
206 spin_unlock_irqrestore(&pm_qos_lock
, flags
);
210 EXPORT_SYMBOL_GPL(pm_qos_request
);
212 int pm_qos_request_active(struct pm_qos_request_list
*req
)
214 return req
->pm_qos_class
!= 0;
216 EXPORT_SYMBOL_GPL(pm_qos_request_active
);
219 * pm_qos_add_request - inserts new qos request into the list
220 * @dep: pointer to a preallocated handle
221 * @pm_qos_class: identifies which list of qos request to use
222 * @value: defines the qos request
224 * This function inserts a new entry in the pm_qos_class list of requested qos
225 * performance characteristics. It recomputes the aggregate QoS expectations
226 * for the pm_qos_class of parameters and initializes the pm_qos_request_list
227 * handle. Caller needs to save this handle for later use in updates and
231 void pm_qos_add_request(struct pm_qos_request_list
*dep
,
232 int pm_qos_class
, s32 value
)
234 struct pm_qos_object
*o
= pm_qos_array
[pm_qos_class
];
237 if (pm_qos_request_active(dep
)) {
238 WARN(1, KERN_ERR
"pm_qos_add_request() called for already added request\n");
241 if (value
== PM_QOS_DEFAULT_VALUE
)
242 new_value
= o
->default_value
;
245 plist_node_init(&dep
->list
, new_value
);
246 dep
->pm_qos_class
= pm_qos_class
;
247 update_target(o
, &dep
->list
, 0, PM_QOS_DEFAULT_VALUE
);
249 EXPORT_SYMBOL_GPL(pm_qos_add_request
);
252 * pm_qos_update_request - modifies an existing qos request
253 * @pm_qos_req : handle to list element holding a pm_qos request to use
254 * @value: defines the qos request
256 * Updates an existing qos request for the pm_qos_class of parameters along
257 * with updating the target pm_qos_class value.
259 * Attempts are made to make this code callable on hot code paths.
261 void pm_qos_update_request(struct pm_qos_request_list
*pm_qos_req
,
265 struct pm_qos_object
*o
;
267 if (!pm_qos_req
) /*guard against callers passing in null */
270 if (!pm_qos_request_active(pm_qos_req
)) {
271 WARN(1, KERN_ERR
"pm_qos_update_request() called for unknown object\n");
275 o
= pm_qos_array
[pm_qos_req
->pm_qos_class
];
277 if (new_value
== PM_QOS_DEFAULT_VALUE
)
278 temp
= o
->default_value
;
282 if (temp
!= pm_qos_req
->list
.prio
)
283 update_target(o
, &pm_qos_req
->list
, 0, temp
);
285 EXPORT_SYMBOL_GPL(pm_qos_update_request
);
288 * pm_qos_remove_request - modifies an existing qos request
289 * @pm_qos_req: handle to request list element
291 * Will remove pm qos request from the list of requests and
292 * recompute the current target value for the pm_qos_class. Call this
293 * on slow code paths.
295 void pm_qos_remove_request(struct pm_qos_request_list
*pm_qos_req
)
297 struct pm_qos_object
*o
;
299 if (pm_qos_req
== NULL
)
301 /* silent return to keep pcm code cleaner */
303 if (!pm_qos_request_active(pm_qos_req
)) {
304 WARN(1, KERN_ERR
"pm_qos_remove_request() called for unknown object\n");
308 o
= pm_qos_array
[pm_qos_req
->pm_qos_class
];
309 update_target(o
, &pm_qos_req
->list
, 1, PM_QOS_DEFAULT_VALUE
);
310 memset(pm_qos_req
, 0, sizeof(*pm_qos_req
));
312 EXPORT_SYMBOL_GPL(pm_qos_remove_request
);
315 * pm_qos_add_notifier - sets notification entry for changes to target value
316 * @pm_qos_class: identifies which qos target changes should be notified.
317 * @notifier: notifier block managed by caller.
319 * will register the notifier into a notification chain that gets called
320 * upon changes to the pm_qos_class target value.
322 int pm_qos_add_notifier(int pm_qos_class
, struct notifier_block
*notifier
)
326 retval
= blocking_notifier_chain_register(
327 pm_qos_array
[pm_qos_class
]->notifiers
, notifier
);
331 EXPORT_SYMBOL_GPL(pm_qos_add_notifier
);
334 * pm_qos_remove_notifier - deletes notification entry from chain.
335 * @pm_qos_class: identifies which qos target changes are notified.
336 * @notifier: notifier block to be removed.
338 * will remove the notifier from the notification chain that gets called
339 * upon changes to the pm_qos_class target value.
341 int pm_qos_remove_notifier(int pm_qos_class
, struct notifier_block
*notifier
)
345 retval
= blocking_notifier_chain_unregister(
346 pm_qos_array
[pm_qos_class
]->notifiers
, notifier
);
350 EXPORT_SYMBOL_GPL(pm_qos_remove_notifier
);
352 static int pm_qos_power_open(struct inode
*inode
, struct file
*filp
)
356 pm_qos_class
= find_pm_qos_object_by_minor(iminor(inode
));
357 if (pm_qos_class
>= 0) {
358 struct pm_qos_request_list
*req
= kzalloc(sizeof(*req
), GFP_KERNEL
);
362 pm_qos_add_request(req
, pm_qos_class
, PM_QOS_DEFAULT_VALUE
);
363 filp
->private_data
= req
;
365 if (filp
->private_data
)
371 static int pm_qos_power_release(struct inode
*inode
, struct file
*filp
)
373 struct pm_qos_request_list
*req
;
375 req
= filp
->private_data
;
376 pm_qos_remove_request(req
);
383 static ssize_t
pm_qos_power_read(struct file
*filp
, char __user
*buf
,
384 size_t count
, loff_t
*f_pos
)
388 struct pm_qos_object
*o
;
389 struct pm_qos_request_list
*pm_qos_req
= filp
->private_data
;
393 if (!pm_qos_request_active(pm_qos_req
))
396 o
= pm_qos_array
[pm_qos_req
->pm_qos_class
];
397 spin_lock_irqsave(&pm_qos_lock
, flags
);
398 value
= pm_qos_get_value(o
);
399 spin_unlock_irqrestore(&pm_qos_lock
, flags
);
401 return simple_read_from_buffer(buf
, count
, f_pos
, &value
, sizeof(s32
));
404 static ssize_t
pm_qos_power_write(struct file
*filp
, const char __user
*buf
,
405 size_t count
, loff_t
*f_pos
)
408 struct pm_qos_request_list
*pm_qos_req
;
410 if (count
== sizeof(s32
)) {
411 if (copy_from_user(&value
, buf
, sizeof(s32
)))
413 } else if (count
<= 11) { /* ASCII perhaps? */
414 char ascii_value
[11];
415 unsigned long int ulval
;
418 if (copy_from_user(ascii_value
, buf
, count
))
422 if (ascii_value
[10] == '\n')
423 ascii_value
[10] = '\0';
427 ascii_value
[count
] = '\0';
429 ret
= strict_strtoul(ascii_value
, 16, &ulval
);
431 pr_debug("%s, 0x%lx, 0x%x\n", ascii_value
, ulval
, ret
);
434 value
= (s32
)lower_32_bits(ulval
);
439 pm_qos_req
= filp
->private_data
;
440 pm_qos_update_request(pm_qos_req
, value
);
446 static int __init
pm_qos_power_init(void)
450 ret
= register_pm_qos_misc(&cpu_dma_pm_qos
);
452 printk(KERN_ERR
"pm_qos_param: cpu_dma_latency setup failed\n");
455 ret
= register_pm_qos_misc(&network_lat_pm_qos
);
457 printk(KERN_ERR
"pm_qos_param: network_latency setup failed\n");
460 ret
= register_pm_qos_misc(&network_throughput_pm_qos
);
463 "pm_qos_param: network_throughput setup failed\n");
468 late_initcall(pm_qos_power_init
);