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 requirements
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 requirements, notifiers
19 * User mode requirements 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 * requirement is removed and a new qos target is computed. This way when the
24 * requirement 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>
30 #include <linux/pm_qos_params.h>
31 #include <linux/sched.h>
32 #include <linux/spinlock.h>
33 #include <linux/slab.h>
34 #include <linux/time.h>
36 #include <linux/device.h>
37 #include <linux/miscdevice.h>
38 #include <linux/string.h>
39 #include <linux/platform_device.h>
40 #include <linux/init.h>
42 #include <linux/uaccess.h>
45 * locking rule: all changes to requirements or notifiers lists
46 * or pm_qos_object list and pm_qos_objects need to happen with pm_qos_lock
47 * held, taken with _irqsave. One lock to rule them all
49 struct requirement_list
{
50 struct list_head list
;
59 static s32
max_compare(s32 v1
, s32 v2
);
60 static s32
min_compare(s32 v1
, s32 v2
);
62 struct pm_qos_object
{
63 struct requirement_list requirements
;
64 struct blocking_notifier_head
*notifiers
;
65 struct miscdevice pm_qos_power_miscdev
;
68 atomic_t target_value
;
69 s32 (*comparitor
)(s32
, s32
);
72 static struct pm_qos_object null_pm_qos
;
73 static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier
);
74 static struct pm_qos_object cpu_dma_pm_qos
= {
75 .requirements
= {LIST_HEAD_INIT(cpu_dma_pm_qos
.requirements
.list
)},
76 .notifiers
= &cpu_dma_lat_notifier
,
77 .name
= "cpu_dma_latency",
78 .default_value
= 2000 * USEC_PER_SEC
,
79 .target_value
= ATOMIC_INIT(2000 * USEC_PER_SEC
),
80 .comparitor
= min_compare
83 static BLOCKING_NOTIFIER_HEAD(network_lat_notifier
);
84 static struct pm_qos_object network_lat_pm_qos
= {
85 .requirements
= {LIST_HEAD_INIT(network_lat_pm_qos
.requirements
.list
)},
86 .notifiers
= &network_lat_notifier
,
87 .name
= "network_latency",
88 .default_value
= 2000 * USEC_PER_SEC
,
89 .target_value
= ATOMIC_INIT(2000 * USEC_PER_SEC
),
90 .comparitor
= min_compare
94 static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier
);
95 static struct pm_qos_object network_throughput_pm_qos
= {
97 {LIST_HEAD_INIT(network_throughput_pm_qos
.requirements
.list
)},
98 .notifiers
= &network_throughput_notifier
,
99 .name
= "network_throughput",
101 .target_value
= ATOMIC_INIT(0),
102 .comparitor
= max_compare
106 static struct pm_qos_object
*pm_qos_array
[] = {
110 &network_throughput_pm_qos
113 static DEFINE_SPINLOCK(pm_qos_lock
);
115 static ssize_t
pm_qos_power_write(struct file
*filp
, const char __user
*buf
,
116 size_t count
, loff_t
*f_pos
);
117 static int pm_qos_power_open(struct inode
*inode
, struct file
*filp
);
118 static int pm_qos_power_release(struct inode
*inode
, struct file
*filp
);
120 static const struct file_operations pm_qos_power_fops
= {
121 .write
= pm_qos_power_write
,
122 .open
= pm_qos_power_open
,
123 .release
= pm_qos_power_release
,
126 /* static helper functions */
127 static s32
max_compare(s32 v1
, s32 v2
)
132 static s32
min_compare(s32 v1
, s32 v2
)
138 static void update_target(int target
)
141 struct requirement_list
*node
;
143 int call_notifier
= 0;
145 spin_lock_irqsave(&pm_qos_lock
, flags
);
146 extreme_value
= pm_qos_array
[target
]->default_value
;
147 list_for_each_entry(node
,
148 &pm_qos_array
[target
]->requirements
.list
, list
) {
149 extreme_value
= pm_qos_array
[target
]->comparitor(
150 extreme_value
, node
->value
);
152 if (atomic_read(&pm_qos_array
[target
]->target_value
) != extreme_value
) {
154 atomic_set(&pm_qos_array
[target
]->target_value
, extreme_value
);
155 pr_debug(KERN_ERR
"new target for qos %d is %d\n", target
,
156 atomic_read(&pm_qos_array
[target
]->target_value
));
158 spin_unlock_irqrestore(&pm_qos_lock
, flags
);
161 blocking_notifier_call_chain(pm_qos_array
[target
]->notifiers
,
162 (unsigned long) extreme_value
, NULL
);
165 static int register_pm_qos_misc(struct pm_qos_object
*qos
)
167 qos
->pm_qos_power_miscdev
.minor
= MISC_DYNAMIC_MINOR
;
168 qos
->pm_qos_power_miscdev
.name
= qos
->name
;
169 qos
->pm_qos_power_miscdev
.fops
= &pm_qos_power_fops
;
171 return misc_register(&qos
->pm_qos_power_miscdev
);
174 static int find_pm_qos_object_by_minor(int minor
)
178 for (pm_qos_class
= 0;
179 pm_qos_class
< PM_QOS_NUM_CLASSES
; pm_qos_class
++) {
181 pm_qos_array
[pm_qos_class
]->pm_qos_power_miscdev
.minor
)
188 * pm_qos_requirement - returns current system wide qos expectation
189 * @pm_qos_class: identification of which qos value is requested
191 * This function returns the current target value in an atomic manner.
193 int pm_qos_requirement(int pm_qos_class
)
195 return atomic_read(&pm_qos_array
[pm_qos_class
]->target_value
);
197 EXPORT_SYMBOL_GPL(pm_qos_requirement
);
200 * pm_qos_add_requirement - inserts new qos request into the list
201 * @pm_qos_class: identifies which list of qos request to us
202 * @name: identifies the request
203 * @value: defines the qos request
205 * This function inserts a new entry in the pm_qos_class list of requested qos
206 * performance characteristics. It recomputes the aggregate QoS expectations
207 * for the pm_qos_class of parameters.
209 int pm_qos_add_requirement(int pm_qos_class
, char *name
, s32 value
)
211 struct requirement_list
*dep
;
214 dep
= kzalloc(sizeof(struct requirement_list
), GFP_KERNEL
);
216 if (value
== PM_QOS_DEFAULT_VALUE
)
217 dep
->value
= pm_qos_array
[pm_qos_class
]->default_value
;
220 dep
->name
= kstrdup(name
, GFP_KERNEL
);
224 spin_lock_irqsave(&pm_qos_lock
, flags
);
226 &pm_qos_array
[pm_qos_class
]->requirements
.list
);
227 spin_unlock_irqrestore(&pm_qos_lock
, flags
);
228 update_target(pm_qos_class
);
237 EXPORT_SYMBOL_GPL(pm_qos_add_requirement
);
240 * pm_qos_update_requirement - modifies an existing qos request
241 * @pm_qos_class: identifies which list of qos request to us
242 * @name: identifies the request
243 * @value: defines the qos request
245 * Updates an existing qos requirement for the pm_qos_class of parameters along
246 * with updating the target pm_qos_class value.
248 * If the named request isn't in the list then no change is made.
250 int pm_qos_update_requirement(int pm_qos_class
, char *name
, s32 new_value
)
253 struct requirement_list
*node
;
254 int pending_update
= 0;
256 spin_lock_irqsave(&pm_qos_lock
, flags
);
257 list_for_each_entry(node
,
258 &pm_qos_array
[pm_qos_class
]->requirements
.list
, list
) {
259 if (strcmp(node
->name
, name
) == 0) {
260 if (new_value
== PM_QOS_DEFAULT_VALUE
)
262 pm_qos_array
[pm_qos_class
]->default_value
;
264 node
->value
= new_value
;
269 spin_unlock_irqrestore(&pm_qos_lock
, flags
);
271 update_target(pm_qos_class
);
275 EXPORT_SYMBOL_GPL(pm_qos_update_requirement
);
278 * pm_qos_remove_requirement - modifies an existing qos request
279 * @pm_qos_class: identifies which list of qos request to us
280 * @name: identifies the request
282 * Will remove named qos request from pm_qos_class list of parameters and
283 * recompute the current target value for the pm_qos_class.
285 void pm_qos_remove_requirement(int pm_qos_class
, char *name
)
288 struct requirement_list
*node
;
289 int pending_update
= 0;
291 spin_lock_irqsave(&pm_qos_lock
, flags
);
292 list_for_each_entry(node
,
293 &pm_qos_array
[pm_qos_class
]->requirements
.list
, list
) {
294 if (strcmp(node
->name
, name
) == 0) {
296 list_del(&node
->list
);
302 spin_unlock_irqrestore(&pm_qos_lock
, flags
);
304 update_target(pm_qos_class
);
306 EXPORT_SYMBOL_GPL(pm_qos_remove_requirement
);
309 * pm_qos_add_notifier - sets notification entry for changes to target value
310 * @pm_qos_class: identifies which qos target changes should be notified.
311 * @notifier: notifier block managed by caller.
313 * will register the notifier into a notification chain that gets called
314 * upon changes to the pm_qos_class target value.
316 int pm_qos_add_notifier(int pm_qos_class
, struct notifier_block
*notifier
)
320 retval
= blocking_notifier_chain_register(
321 pm_qos_array
[pm_qos_class
]->notifiers
, notifier
);
325 EXPORT_SYMBOL_GPL(pm_qos_add_notifier
);
328 * pm_qos_remove_notifier - deletes notification entry from chain.
329 * @pm_qos_class: identifies which qos target changes are notified.
330 * @notifier: notifier block to be removed.
332 * will remove the notifier from the notification chain that gets called
333 * upon changes to the pm_qos_class target value.
335 int pm_qos_remove_notifier(int pm_qos_class
, struct notifier_block
*notifier
)
339 retval
= blocking_notifier_chain_unregister(
340 pm_qos_array
[pm_qos_class
]->notifiers
, notifier
);
344 EXPORT_SYMBOL_GPL(pm_qos_remove_notifier
);
346 #define PID_NAME_LEN 32
348 static int pm_qos_power_open(struct inode
*inode
, struct file
*filp
)
352 char name
[PID_NAME_LEN
];
354 pm_qos_class
= find_pm_qos_object_by_minor(iminor(inode
));
355 if (pm_qos_class
>= 0) {
356 filp
->private_data
= (void *)pm_qos_class
;
357 snprintf(name
, PID_NAME_LEN
, "process_%d", current
->pid
);
358 ret
= pm_qos_add_requirement(pm_qos_class
, name
,
359 PM_QOS_DEFAULT_VALUE
);
366 static int pm_qos_power_release(struct inode
*inode
, struct file
*filp
)
369 char name
[PID_NAME_LEN
];
371 pm_qos_class
= (long)filp
->private_data
;
372 snprintf(name
, PID_NAME_LEN
, "process_%d", current
->pid
);
373 pm_qos_remove_requirement(pm_qos_class
, name
);
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 name
[PID_NAME_LEN
];
385 pm_qos_class
= (long)filp
->private_data
;
386 if (count
!= sizeof(s32
))
388 if (copy_from_user(&value
, buf
, sizeof(s32
)))
390 snprintf(name
, PID_NAME_LEN
, "process_%d", current
->pid
);
391 pm_qos_update_requirement(pm_qos_class
, name
, value
);
397 static int __init
pm_qos_power_init(void)
401 ret
= register_pm_qos_misc(&cpu_dma_pm_qos
);
403 printk(KERN_ERR
"pm_qos_param: cpu_dma_latency setup failed\n");
406 ret
= register_pm_qos_misc(&network_lat_pm_qos
);
408 printk(KERN_ERR
"pm_qos_param: network_latency setup failed\n");
411 ret
= register_pm_qos_misc(&network_throughput_pm_qos
);
414 "pm_qos_param: network_throughput setup failed\n");
419 late_initcall(pm_qos_power_init
);