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>
29 * Copyright (c) 2009, Code Aurora Forum. All rights reserved.
32 #include <linux/pm_qos_params.h>
33 #include <linux/sched.h>
34 #include <linux/smp_lock.h>
35 #include <linux/spinlock.h>
36 #include <linux/slab.h>
37 #include <linux/time.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>
45 #include <linux/uaccess.h>
48 * locking rule: all changes to requirements 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
52 static s32
max_compare(s32 v1
, s32 v2
);
53 static s32
min_compare(s32 v1
, s32 v2
);
55 struct pm_qos_power_user
{
57 char name
[sizeof("user_01234567")];
60 static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier
);
61 static struct pm_qos_object cpu_dma_pm_qos
= {
62 .requirements
= {LIST_HEAD_INIT(cpu_dma_pm_qos
.requirements
.list
)},
63 .notifiers
= &cpu_dma_lat_notifier
,
64 .name
= "cpu_dma_latency",
65 .default_value
= 2000 * USEC_PER_SEC
,
66 .target_value
= ATOMIC_INIT(2000 * USEC_PER_SEC
),
67 .comparitor
= min_compare
70 static BLOCKING_NOTIFIER_HEAD(network_lat_notifier
);
71 static struct pm_qos_object network_lat_pm_qos
= {
72 .requirements
= {LIST_HEAD_INIT(network_lat_pm_qos
.requirements
.list
)},
73 .notifiers
= &network_lat_notifier
,
74 .name
= "network_latency",
75 .default_value
= 2000 * USEC_PER_SEC
,
76 .target_value
= ATOMIC_INIT(2000 * USEC_PER_SEC
),
77 .comparitor
= min_compare
81 static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier
);
82 static struct pm_qos_object network_throughput_pm_qos
= {
84 {LIST_HEAD_INIT(network_throughput_pm_qos
.requirements
.list
)},
85 .notifiers
= &network_throughput_notifier
,
86 .name
= "network_throughput",
88 .target_value
= ATOMIC_INIT(0),
89 .comparitor
= max_compare
92 static BLOCKING_NOTIFIER_HEAD(system_bus_freq_notifier
);
93 static struct pm_qos_object system_bus_freq_pm_qos
= {
95 {LIST_HEAD_INIT(system_bus_freq_pm_qos
.requirements
.list
)},
96 .notifiers
= &system_bus_freq_notifier
,
97 .name
= "system_bus_freq",
99 .target_value
= ATOMIC_INIT(0),
100 .comparitor
= max_compare
104 static struct pm_qos_object
*pm_qos_array
[PM_QOS_NUM_CLASSES
] = {
105 [PM_QOS_RESERVED
] = NULL
,
106 [PM_QOS_CPU_DMA_LATENCY
] = &cpu_dma_pm_qos
,
107 [PM_QOS_NETWORK_LATENCY
] = &network_lat_pm_qos
,
108 [PM_QOS_NETWORK_THROUGHPUT
] = &network_throughput_pm_qos
,
109 [PM_QOS_SYSTEM_BUS_FREQ
] = &system_bus_freq_pm_qos
,
112 static DEFINE_SPINLOCK(pm_qos_lock
);
113 static atomic_t pm_qos_user_id
= ATOMIC_INIT(0);
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 pm_qos_class
)
141 struct requirement_list
*node
;
143 int call_notifier
= 0;
145 spin_lock_irqsave(&pm_qos_lock
, flags
);
146 extreme_value
= pm_qos_array
[pm_qos_class
]->default_value
;
147 list_for_each_entry(node
,
148 &pm_qos_array
[pm_qos_class
]->requirements
.list
, list
) {
149 extreme_value
= pm_qos_array
[pm_qos_class
]->comparitor(
150 extreme_value
, node
->value
);
152 if (atomic_read(&pm_qos_array
[pm_qos_class
]->target_value
) !=
155 atomic_set(&pm_qos_array
[pm_qos_class
]->target_value
,
157 pr_debug(KERN_ERR
"new target for qos %d is %d\n",
158 pm_qos_class
, atomic_read(
159 &pm_qos_array
[pm_qos_class
]->target_value
));
161 spin_unlock_irqrestore(&pm_qos_lock
, flags
);
164 blocking_notifier_call_chain(
165 pm_qos_array
[pm_qos_class
]->notifiers
,
166 (unsigned long) extreme_value
, NULL
);
169 static int register_pm_qos_misc(struct pm_qos_object
*qos
)
171 qos
->pm_qos_power_miscdev
.minor
= MISC_DYNAMIC_MINOR
;
172 qos
->pm_qos_power_miscdev
.name
= qos
->name
;
173 qos
->pm_qos_power_miscdev
.fops
= &pm_qos_power_fops
;
175 return misc_register(&qos
->pm_qos_power_miscdev
);
178 static int find_pm_qos_object_by_minor(int minor
)
182 for (pm_qos_class
= 0;
183 pm_qos_class
< PM_QOS_NUM_CLASSES
; pm_qos_class
++) {
184 if (!pm_qos_array
[pm_qos_class
])
187 pm_qos_array
[pm_qos_class
]->pm_qos_power_miscdev
.minor
)
194 * pm_qos_requirement - 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_requirement(int pm_qos_class
)
201 return atomic_read(&pm_qos_array
[pm_qos_class
]->target_value
);
203 EXPORT_SYMBOL_GPL(pm_qos_requirement
);
206 * pm_qos_add_requirement - inserts new qos request into the list
207 * @pm_qos_class: identifies which list of qos request to us
208 * @name: identifies the request
209 * @value: defines the qos request
211 * This function inserts a new entry in the pm_qos_class list of requested qos
212 * performance characteristics. It recomputes the aggregate QoS expectations
213 * for the pm_qos_class of parameters.
215 int pm_qos_add_requirement(int pm_qos_class
, char *name
, s32 value
)
217 struct requirement_list
*dep
;
220 dep
= kzalloc(sizeof(struct requirement_list
), GFP_KERNEL
);
222 if (value
== PM_QOS_DEFAULT_VALUE
)
223 dep
->value
= pm_qos_array
[pm_qos_class
]->default_value
;
226 dep
->name
= kstrdup(name
, GFP_KERNEL
);
230 spin_lock_irqsave(&pm_qos_lock
, flags
);
232 &pm_qos_array
[pm_qos_class
]->requirements
.list
);
233 spin_unlock_irqrestore(&pm_qos_lock
, flags
);
234 update_target(pm_qos_class
);
243 EXPORT_SYMBOL_GPL(pm_qos_add_requirement
);
246 * pm_qos_update_requirement - modifies an existing qos request
247 * @pm_qos_class: identifies which list of qos request to us
248 * @name: identifies the request
249 * @value: defines the qos request
251 * Updates an existing qos requirement for the pm_qos_class of parameters along
252 * with updating the target pm_qos_class value.
254 * If the named request isn't in the list then no change is made.
256 int pm_qos_update_requirement(int pm_qos_class
, char *name
, s32 new_value
)
259 struct requirement_list
*node
;
260 int pending_update
= 0;
262 spin_lock_irqsave(&pm_qos_lock
, flags
);
263 list_for_each_entry(node
,
264 &pm_qos_array
[pm_qos_class
]->requirements
.list
, list
) {
265 if (strcmp(node
->name
, name
) == 0) {
266 if (new_value
== PM_QOS_DEFAULT_VALUE
)
268 pm_qos_array
[pm_qos_class
]->default_value
;
270 node
->value
= new_value
;
275 spin_unlock_irqrestore(&pm_qos_lock
, flags
);
277 update_target(pm_qos_class
);
281 EXPORT_SYMBOL_GPL(pm_qos_update_requirement
);
284 * pm_qos_remove_requirement - modifies an existing qos request
285 * @pm_qos_class: identifies which list of qos request to us
286 * @name: identifies the request
288 * Will remove named qos request from pm_qos_class list of parameters and
289 * recompute the current target value for the pm_qos_class.
291 void pm_qos_remove_requirement(int pm_qos_class
, char *name
)
294 struct requirement_list
*node
;
295 int pending_update
= 0;
297 spin_lock_irqsave(&pm_qos_lock
, flags
);
298 list_for_each_entry(node
,
299 &pm_qos_array
[pm_qos_class
]->requirements
.list
, list
) {
300 if (strcmp(node
->name
, name
) == 0) {
302 list_del(&node
->list
);
308 spin_unlock_irqrestore(&pm_qos_lock
, flags
);
310 update_target(pm_qos_class
);
312 EXPORT_SYMBOL_GPL(pm_qos_remove_requirement
);
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
);
353 static int pm_qos_power_open(struct inode
*inode
, struct file
*filp
)
357 struct pm_qos_power_user
*usr
;
359 usr
= kzalloc(sizeof(struct pm_qos_power_user
), GFP_KERNEL
);
364 pm_qos_class
= find_pm_qos_object_by_minor(iminor(inode
));
365 if (pm_qos_class
< 0) {
371 usr
->pm_qos_class
= pm_qos_class
;
372 snprintf(usr
->name
, sizeof(usr
->name
),
373 "user_%08x", (unsigned)atomic_inc_return(&pm_qos_user_id
));
375 ret
= pm_qos_add_requirement(usr
->pm_qos_class
, usr
->name
,
376 PM_QOS_DEFAULT_VALUE
);
384 filp
->private_data
= usr
;
388 static int pm_qos_power_release(struct inode
*inode
, struct file
*filp
)
390 struct pm_qos_power_user
*usr
;
392 usr
= (struct pm_qos_power_user
*)filp
->private_data
;
393 pm_qos_remove_requirement(usr
->pm_qos_class
, usr
->name
);
395 filp
->private_data
= NULL
;
400 static ssize_t
pm_qos_power_write(struct file
*filp
, const char __user
*buf
,
401 size_t count
, loff_t
*f_pos
)
403 struct pm_qos_power_user
*usr
;
406 usr
= (struct pm_qos_power_user
*)filp
->private_data
;
408 if (count
!= sizeof(s32
))
411 if (get_user(value
, (s32
*)buf
))
414 pm_qos_update_requirement(usr
->pm_qos_class
, usr
->name
, value
);
419 static int __init
pm_qos_power_init(void)
423 ret
= register_pm_qos_misc(&cpu_dma_pm_qos
);
425 printk(KERN_ERR
"pm_qos_param: cpu_dma_latency setup failed\n");
428 ret
= register_pm_qos_misc(&network_lat_pm_qos
);
430 printk(KERN_ERR
"pm_qos_param: network_latency setup failed\n");
433 ret
= register_pm_qos_misc(&network_throughput_pm_qos
);
436 "pm_qos_param: network_throughput setup failed\n");
439 ret
= register_pm_qos_misc(&system_bus_freq_pm_qos
);
442 "pm_qos_param: system_bus_freq setup failed\n");
447 late_initcall(pm_qos_power_init
);