Commit | Line | Data |
---|---|---|
d82b3518 MG |
1 | /* |
2 | * This module exposes the interface to kernel space for specifying | |
3 | * QoS dependencies. It provides infrastructure for registration of: | |
4 | * | |
5 | * Dependents on a QoS value : register requirements | |
6 | * Watchers of QoS value : get notified when target QoS value changes | |
7 | * | |
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. | |
10 | * | |
11 | * There are 3 basic classes of QoS parameter: latency, timeout, throughput | |
12 | * each have defined units: | |
13 | * latency: usec | |
14 | * timeout: usec <-- currently not used. | |
15 | * throughput: kbs (kilo byte / sec) | |
16 | * | |
17 | * There are lists of pm_qos_objects each one wrapping requirements, notifiers | |
18 | * | |
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. | |
26 | * | |
bf1db69f | 27 | * Mark Gross <mgross@linux.intel.com> |
d82b3518 MG |
28 | */ |
29 | ||
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> | |
35 | #include <linux/fs.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> | |
41 | ||
42 | #include <linux/uaccess.h> | |
43 | ||
44 | /* | |
9d359357 | 45 | * locking rule: all changes to requirements or notifiers lists |
d82b3518 MG |
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 | |
48 | */ | |
49 | struct requirement_list { | |
50 | struct list_head list; | |
51 | union { | |
52 | s32 value; | |
53 | s32 usec; | |
54 | s32 kbps; | |
55 | }; | |
56 | char *name; | |
57 | }; | |
58 | ||
59 | static s32 max_compare(s32 v1, s32 v2); | |
60 | static s32 min_compare(s32 v1, s32 v2); | |
61 | ||
62 | struct pm_qos_object { | |
63 | struct requirement_list requirements; | |
64 | struct blocking_notifier_head *notifiers; | |
65 | struct miscdevice pm_qos_power_miscdev; | |
66 | char *name; | |
67 | s32 default_value; | |
9d359357 | 68 | atomic_t target_value; |
d82b3518 MG |
69 | s32 (*comparitor)(s32, s32); |
70 | }; | |
71 | ||
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, | |
9d359357 | 79 | .target_value = ATOMIC_INIT(2000 * USEC_PER_SEC), |
d82b3518 MG |
80 | .comparitor = min_compare |
81 | }; | |
82 | ||
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, | |
9d359357 | 89 | .target_value = ATOMIC_INIT(2000 * USEC_PER_SEC), |
d82b3518 MG |
90 | .comparitor = min_compare |
91 | }; | |
92 | ||
93 | ||
94 | static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier); | |
95 | static struct pm_qos_object network_throughput_pm_qos = { | |
96 | .requirements = | |
97 | {LIST_HEAD_INIT(network_throughput_pm_qos.requirements.list)}, | |
98 | .notifiers = &network_throughput_notifier, | |
99 | .name = "network_throughput", | |
100 | .default_value = 0, | |
9d359357 | 101 | .target_value = ATOMIC_INIT(0), |
d82b3518 MG |
102 | .comparitor = max_compare |
103 | }; | |
104 | ||
105 | ||
106 | static struct pm_qos_object *pm_qos_array[] = { | |
107 | &null_pm_qos, | |
108 | &cpu_dma_pm_qos, | |
109 | &network_lat_pm_qos, | |
110 | &network_throughput_pm_qos | |
111 | }; | |
112 | ||
113 | static DEFINE_SPINLOCK(pm_qos_lock); | |
114 | ||
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); | |
119 | ||
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, | |
124 | }; | |
125 | ||
126 | /* static helper functions */ | |
127 | static s32 max_compare(s32 v1, s32 v2) | |
128 | { | |
129 | return max(v1, v2); | |
130 | } | |
131 | ||
132 | static s32 min_compare(s32 v1, s32 v2) | |
133 | { | |
134 | return min(v1, v2); | |
135 | } | |
136 | ||
137 | ||
138 | static void update_target(int target) | |
139 | { | |
140 | s32 extreme_value; | |
141 | struct requirement_list *node; | |
142 | unsigned long flags; | |
143 | int call_notifier = 0; | |
144 | ||
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); | |
151 | } | |
9d359357 | 152 | if (atomic_read(&pm_qos_array[target]->target_value) != extreme_value) { |
d82b3518 | 153 | call_notifier = 1; |
9d359357 | 154 | atomic_set(&pm_qos_array[target]->target_value, extreme_value); |
d82b3518 | 155 | pr_debug(KERN_ERR "new target for qos %d is %d\n", target, |
9d359357 | 156 | atomic_read(&pm_qos_array[target]->target_value)); |
d82b3518 MG |
157 | } |
158 | spin_unlock_irqrestore(&pm_qos_lock, flags); | |
159 | ||
160 | if (call_notifier) | |
161 | blocking_notifier_call_chain(pm_qos_array[target]->notifiers, | |
162 | (unsigned long) extreme_value, NULL); | |
163 | } | |
164 | ||
165 | static int register_pm_qos_misc(struct pm_qos_object *qos) | |
166 | { | |
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; | |
170 | ||
171 | return misc_register(&qos->pm_qos_power_miscdev); | |
172 | } | |
173 | ||
174 | static int find_pm_qos_object_by_minor(int minor) | |
175 | { | |
176 | int pm_qos_class; | |
177 | ||
178 | for (pm_qos_class = 0; | |
179 | pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) { | |
180 | if (minor == | |
181 | pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor) | |
182 | return pm_qos_class; | |
183 | } | |
184 | return -1; | |
185 | } | |
186 | ||
187 | /** | |
188 | * pm_qos_requirement - returns current system wide qos expectation | |
189 | * @pm_qos_class: identification of which qos value is requested | |
190 | * | |
191 | * This function returns the current target value in an atomic manner. | |
192 | */ | |
193 | int pm_qos_requirement(int pm_qos_class) | |
194 | { | |
9d359357 | 195 | return atomic_read(&pm_qos_array[pm_qos_class]->target_value); |
d82b3518 MG |
196 | } |
197 | EXPORT_SYMBOL_GPL(pm_qos_requirement); | |
198 | ||
199 | /** | |
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 | |
204 | * | |
205 | * This function inserts a new entry in the pm_qos_class list of requested qos | |
bf1db69f RH |
206 | * performance characteristics. It recomputes the aggregate QoS expectations |
207 | * for the pm_qos_class of parameters. | |
d82b3518 MG |
208 | */ |
209 | int pm_qos_add_requirement(int pm_qos_class, char *name, s32 value) | |
210 | { | |
211 | struct requirement_list *dep; | |
212 | unsigned long flags; | |
213 | ||
214 | dep = kzalloc(sizeof(struct requirement_list), GFP_KERNEL); | |
215 | if (dep) { | |
216 | if (value == PM_QOS_DEFAULT_VALUE) | |
217 | dep->value = pm_qos_array[pm_qos_class]->default_value; | |
218 | else | |
219 | dep->value = value; | |
220 | dep->name = kstrdup(name, GFP_KERNEL); | |
221 | if (!dep->name) | |
222 | goto cleanup; | |
223 | ||
224 | spin_lock_irqsave(&pm_qos_lock, flags); | |
225 | list_add(&dep->list, | |
226 | &pm_qos_array[pm_qos_class]->requirements.list); | |
227 | spin_unlock_irqrestore(&pm_qos_lock, flags); | |
228 | update_target(pm_qos_class); | |
229 | ||
230 | return 0; | |
231 | } | |
232 | ||
233 | cleanup: | |
234 | kfree(dep); | |
235 | return -ENOMEM; | |
236 | } | |
237 | EXPORT_SYMBOL_GPL(pm_qos_add_requirement); | |
238 | ||
239 | /** | |
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 | |
244 | * | |
bf1db69f | 245 | * Updates an existing qos requirement for the pm_qos_class of parameters along |
d82b3518 MG |
246 | * with updating the target pm_qos_class value. |
247 | * | |
bf1db69f | 248 | * If the named request isn't in the list then no change is made. |
d82b3518 MG |
249 | */ |
250 | int pm_qos_update_requirement(int pm_qos_class, char *name, s32 new_value) | |
251 | { | |
252 | unsigned long flags; | |
253 | struct requirement_list *node; | |
254 | int pending_update = 0; | |
255 | ||
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) | |
261 | node->value = | |
262 | pm_qos_array[pm_qos_class]->default_value; | |
263 | else | |
264 | node->value = new_value; | |
265 | pending_update = 1; | |
266 | break; | |
267 | } | |
268 | } | |
269 | spin_unlock_irqrestore(&pm_qos_lock, flags); | |
270 | if (pending_update) | |
271 | update_target(pm_qos_class); | |
272 | ||
273 | return 0; | |
274 | } | |
275 | EXPORT_SYMBOL_GPL(pm_qos_update_requirement); | |
276 | ||
277 | /** | |
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 | |
281 | * | |
bf1db69f | 282 | * Will remove named qos request from pm_qos_class list of parameters and |
d82b3518 MG |
283 | * recompute the current target value for the pm_qos_class. |
284 | */ | |
285 | void pm_qos_remove_requirement(int pm_qos_class, char *name) | |
286 | { | |
287 | unsigned long flags; | |
288 | struct requirement_list *node; | |
289 | int pending_update = 0; | |
290 | ||
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) { | |
295 | kfree(node->name); | |
296 | list_del(&node->list); | |
297 | kfree(node); | |
298 | pending_update = 1; | |
299 | break; | |
300 | } | |
301 | } | |
302 | spin_unlock_irqrestore(&pm_qos_lock, flags); | |
303 | if (pending_update) | |
304 | update_target(pm_qos_class); | |
305 | } | |
306 | EXPORT_SYMBOL_GPL(pm_qos_remove_requirement); | |
307 | ||
308 | /** | |
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. | |
312 | * | |
313 | * will register the notifier into a notification chain that gets called | |
bf1db69f | 314 | * upon changes to the pm_qos_class target value. |
d82b3518 MG |
315 | */ |
316 | int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier) | |
317 | { | |
318 | int retval; | |
319 | ||
320 | retval = blocking_notifier_chain_register( | |
321 | pm_qos_array[pm_qos_class]->notifiers, notifier); | |
322 | ||
323 | return retval; | |
324 | } | |
325 | EXPORT_SYMBOL_GPL(pm_qos_add_notifier); | |
326 | ||
327 | /** | |
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. | |
331 | * | |
332 | * will remove the notifier from the notification chain that gets called | |
bf1db69f | 333 | * upon changes to the pm_qos_class target value. |
d82b3518 MG |
334 | */ |
335 | int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier) | |
336 | { | |
337 | int retval; | |
338 | ||
339 | retval = blocking_notifier_chain_unregister( | |
340 | pm_qos_array[pm_qos_class]->notifiers, notifier); | |
341 | ||
342 | return retval; | |
343 | } | |
344 | EXPORT_SYMBOL_GPL(pm_qos_remove_notifier); | |
345 | ||
1a6deaea | 346 | #define PID_NAME_LEN 32 |
d82b3518 MG |
347 | |
348 | static int pm_qos_power_open(struct inode *inode, struct file *filp) | |
349 | { | |
350 | int ret; | |
351 | long pm_qos_class; | |
1a6deaea | 352 | char name[PID_NAME_LEN]; |
d82b3518 MG |
353 | |
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; | |
1a6deaea | 357 | snprintf(name, PID_NAME_LEN, "process_%d", current->pid); |
d82b3518 MG |
358 | ret = pm_qos_add_requirement(pm_qos_class, name, |
359 | PM_QOS_DEFAULT_VALUE); | |
e6fe07a0 | 360 | if (ret >= 0) |
d82b3518 MG |
361 | return 0; |
362 | } | |
d82b3518 MG |
363 | return -EPERM; |
364 | } | |
365 | ||
366 | static int pm_qos_power_release(struct inode *inode, struct file *filp) | |
367 | { | |
368 | int pm_qos_class; | |
1a6deaea | 369 | char name[PID_NAME_LEN]; |
d82b3518 MG |
370 | |
371 | pm_qos_class = (long)filp->private_data; | |
1a6deaea | 372 | snprintf(name, PID_NAME_LEN, "process_%d", current->pid); |
d82b3518 MG |
373 | pm_qos_remove_requirement(pm_qos_class, name); |
374 | ||
375 | return 0; | |
376 | } | |
377 | ||
378 | static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf, | |
379 | size_t count, loff_t *f_pos) | |
380 | { | |
381 | s32 value; | |
382 | int pm_qos_class; | |
1a6deaea | 383 | char name[PID_NAME_LEN]; |
d82b3518 MG |
384 | |
385 | pm_qos_class = (long)filp->private_data; | |
386 | if (count != sizeof(s32)) | |
387 | return -EINVAL; | |
388 | if (copy_from_user(&value, buf, sizeof(s32))) | |
389 | return -EFAULT; | |
1a6deaea | 390 | snprintf(name, PID_NAME_LEN, "process_%d", current->pid); |
d82b3518 MG |
391 | pm_qos_update_requirement(pm_qos_class, name, value); |
392 | ||
393 | return sizeof(s32); | |
394 | } | |
395 | ||
396 | ||
397 | static int __init pm_qos_power_init(void) | |
398 | { | |
399 | int ret = 0; | |
400 | ||
401 | ret = register_pm_qos_misc(&cpu_dma_pm_qos); | |
402 | if (ret < 0) { | |
403 | printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n"); | |
404 | return ret; | |
405 | } | |
406 | ret = register_pm_qos_misc(&network_lat_pm_qos); | |
407 | if (ret < 0) { | |
408 | printk(KERN_ERR "pm_qos_param: network_latency setup failed\n"); | |
409 | return ret; | |
410 | } | |
411 | ret = register_pm_qos_misc(&network_throughput_pm_qos); | |
412 | if (ret < 0) | |
413 | printk(KERN_ERR | |
414 | "pm_qos_param: network_throughput setup failed\n"); | |
415 | ||
416 | return ret; | |
417 | } | |
418 | ||
419 | late_initcall(pm_qos_power_init); |