]>
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 | * | |
ed77134b | 5 | * Dependents on a QoS value : register requests |
d82b3518 MG |
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 | * | |
ed77134b | 17 | * There are lists of pm_qos_objects each one wrapping requests, notifiers |
d82b3518 | 18 | * |
ed77134b | 19 | * User mode requests on a QOS parameter register themselves to the |
d82b3518 MG |
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 | |
ed77134b MG |
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 | |
d82b3518 MG |
25 | * pointer or exits the pm_qos_object will get an opportunity to clean up. |
26 | * | |
bf1db69f | 27 | * Mark Gross <[email protected]> |
d82b3518 MG |
28 | */ |
29 | ||
ed77134b MG |
30 | /*#define DEBUG*/ |
31 | ||
d82b3518 MG |
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> | |
37 | #include <linux/fs.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 | ||
44 | #include <linux/uaccess.h> | |
45 | ||
46 | /* | |
ed77134b | 47 | * locking rule: all changes to requests or notifiers lists |
d82b3518 MG |
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 | |
50 | */ | |
ed77134b | 51 | struct pm_qos_request_list { |
d82b3518 MG |
52 | struct list_head list; |
53 | union { | |
54 | s32 value; | |
55 | s32 usec; | |
56 | s32 kbps; | |
57 | }; | |
ed77134b | 58 | int pm_qos_class; |
d82b3518 MG |
59 | }; |
60 | ||
61 | static s32 max_compare(s32 v1, s32 v2); | |
62 | static s32 min_compare(s32 v1, s32 v2); | |
63 | ||
64 | struct pm_qos_object { | |
ed77134b | 65 | struct pm_qos_request_list requests; |
d82b3518 MG |
66 | struct blocking_notifier_head *notifiers; |
67 | struct miscdevice pm_qos_power_miscdev; | |
68 | char *name; | |
69 | s32 default_value; | |
9d359357 | 70 | atomic_t target_value; |
d82b3518 MG |
71 | s32 (*comparitor)(s32, s32); |
72 | }; | |
73 | ||
74 | static struct pm_qos_object null_pm_qos; | |
75 | static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier); | |
76 | static struct pm_qos_object cpu_dma_pm_qos = { | |
ed77134b | 77 | .requests = {LIST_HEAD_INIT(cpu_dma_pm_qos.requests.list)}, |
d82b3518 MG |
78 | .notifiers = &cpu_dma_lat_notifier, |
79 | .name = "cpu_dma_latency", | |
80 | .default_value = 2000 * USEC_PER_SEC, | |
9d359357 | 81 | .target_value = ATOMIC_INIT(2000 * USEC_PER_SEC), |
d82b3518 MG |
82 | .comparitor = min_compare |
83 | }; | |
84 | ||
85 | static BLOCKING_NOTIFIER_HEAD(network_lat_notifier); | |
86 | static struct pm_qos_object network_lat_pm_qos = { | |
ed77134b | 87 | .requests = {LIST_HEAD_INIT(network_lat_pm_qos.requests.list)}, |
d82b3518 MG |
88 | .notifiers = &network_lat_notifier, |
89 | .name = "network_latency", | |
90 | .default_value = 2000 * USEC_PER_SEC, | |
9d359357 | 91 | .target_value = ATOMIC_INIT(2000 * USEC_PER_SEC), |
d82b3518 MG |
92 | .comparitor = min_compare |
93 | }; | |
94 | ||
95 | ||
96 | static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier); | |
97 | static struct pm_qos_object network_throughput_pm_qos = { | |
ed77134b | 98 | .requests = {LIST_HEAD_INIT(network_throughput_pm_qos.requests.list)}, |
d82b3518 MG |
99 | .notifiers = &network_throughput_notifier, |
100 | .name = "network_throughput", | |
101 | .default_value = 0, | |
9d359357 | 102 | .target_value = ATOMIC_INIT(0), |
d82b3518 MG |
103 | .comparitor = max_compare |
104 | }; | |
105 | ||
106 | ||
107 | static struct pm_qos_object *pm_qos_array[] = { | |
108 | &null_pm_qos, | |
109 | &cpu_dma_pm_qos, | |
110 | &network_lat_pm_qos, | |
111 | &network_throughput_pm_qos | |
112 | }; | |
113 | ||
114 | static DEFINE_SPINLOCK(pm_qos_lock); | |
115 | ||
116 | static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf, | |
117 | size_t count, loff_t *f_pos); | |
118 | static int pm_qos_power_open(struct inode *inode, struct file *filp); | |
119 | static int pm_qos_power_release(struct inode *inode, struct file *filp); | |
120 | ||
121 | static const struct file_operations pm_qos_power_fops = { | |
122 | .write = pm_qos_power_write, | |
123 | .open = pm_qos_power_open, | |
124 | .release = pm_qos_power_release, | |
125 | }; | |
126 | ||
127 | /* static helper functions */ | |
128 | static s32 max_compare(s32 v1, s32 v2) | |
129 | { | |
130 | return max(v1, v2); | |
131 | } | |
132 | ||
133 | static s32 min_compare(s32 v1, s32 v2) | |
134 | { | |
135 | return min(v1, v2); | |
136 | } | |
137 | ||
138 | ||
ed77134b | 139 | static void update_target(int pm_qos_class) |
d82b3518 MG |
140 | { |
141 | s32 extreme_value; | |
ed77134b | 142 | struct pm_qos_request_list *node; |
d82b3518 MG |
143 | unsigned long flags; |
144 | int call_notifier = 0; | |
145 | ||
146 | spin_lock_irqsave(&pm_qos_lock, flags); | |
ed77134b | 147 | extreme_value = pm_qos_array[pm_qos_class]->default_value; |
d82b3518 | 148 | list_for_each_entry(node, |
ed77134b MG |
149 | &pm_qos_array[pm_qos_class]->requests.list, list) { |
150 | extreme_value = pm_qos_array[pm_qos_class]->comparitor( | |
d82b3518 MG |
151 | extreme_value, node->value); |
152 | } | |
ed77134b MG |
153 | if (atomic_read(&pm_qos_array[pm_qos_class]->target_value) != |
154 | extreme_value) { | |
d82b3518 | 155 | call_notifier = 1; |
ed77134b MG |
156 | atomic_set(&pm_qos_array[pm_qos_class]->target_value, |
157 | extreme_value); | |
158 | pr_debug(KERN_ERR "new target for qos %d is %d\n", pm_qos_class, | |
159 | atomic_read(&pm_qos_array[pm_qos_class]->target_value)); | |
d82b3518 MG |
160 | } |
161 | spin_unlock_irqrestore(&pm_qos_lock, flags); | |
162 | ||
163 | if (call_notifier) | |
ed77134b MG |
164 | blocking_notifier_call_chain( |
165 | pm_qos_array[pm_qos_class]->notifiers, | |
166 | (unsigned long) extreme_value, NULL); | |
d82b3518 MG |
167 | } |
168 | ||
169 | static int register_pm_qos_misc(struct pm_qos_object *qos) | |
170 | { | |
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; | |
174 | ||
175 | return misc_register(&qos->pm_qos_power_miscdev); | |
176 | } | |
177 | ||
178 | static int find_pm_qos_object_by_minor(int minor) | |
179 | { | |
180 | int pm_qos_class; | |
181 | ||
182 | for (pm_qos_class = 0; | |
183 | pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) { | |
184 | if (minor == | |
185 | pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor) | |
186 | return pm_qos_class; | |
187 | } | |
188 | return -1; | |
189 | } | |
190 | ||
191 | /** | |
ed77134b | 192 | * pm_qos_request - returns current system wide qos expectation |
d82b3518 MG |
193 | * @pm_qos_class: identification of which qos value is requested |
194 | * | |
195 | * This function returns the current target value in an atomic manner. | |
196 | */ | |
ed77134b | 197 | int pm_qos_request(int pm_qos_class) |
d82b3518 | 198 | { |
9d359357 | 199 | return atomic_read(&pm_qos_array[pm_qos_class]->target_value); |
d82b3518 | 200 | } |
ed77134b | 201 | EXPORT_SYMBOL_GPL(pm_qos_request); |
d82b3518 MG |
202 | |
203 | /** | |
ed77134b | 204 | * pm_qos_add_request - inserts new qos request into the list |
d82b3518 | 205 | * @pm_qos_class: identifies which list of qos request to us |
d82b3518 MG |
206 | * @value: defines the qos request |
207 | * | |
208 | * This function inserts a new entry in the pm_qos_class list of requested qos | |
bf1db69f | 209 | * performance characteristics. It recomputes the aggregate QoS expectations |
ed77134b MG |
210 | * for the pm_qos_class of parameters, and returns the pm_qos_request list |
211 | * element as a handle for use in updating and removal. Call needs to save | |
212 | * this handle for later use. | |
d82b3518 | 213 | */ |
ed77134b | 214 | struct pm_qos_request_list *pm_qos_add_request(int pm_qos_class, s32 value) |
d82b3518 | 215 | { |
ed77134b | 216 | struct pm_qos_request_list *dep; |
d82b3518 MG |
217 | unsigned long flags; |
218 | ||
ed77134b | 219 | dep = kzalloc(sizeof(struct pm_qos_request_list), GFP_KERNEL); |
d82b3518 MG |
220 | if (dep) { |
221 | if (value == PM_QOS_DEFAULT_VALUE) | |
222 | dep->value = pm_qos_array[pm_qos_class]->default_value; | |
223 | else | |
224 | dep->value = value; | |
ed77134b | 225 | dep->pm_qos_class = pm_qos_class; |
d82b3518 MG |
226 | |
227 | spin_lock_irqsave(&pm_qos_lock, flags); | |
228 | list_add(&dep->list, | |
ed77134b | 229 | &pm_qos_array[pm_qos_class]->requests.list); |
d82b3518 MG |
230 | spin_unlock_irqrestore(&pm_qos_lock, flags); |
231 | update_target(pm_qos_class); | |
d82b3518 MG |
232 | } |
233 | ||
ed77134b | 234 | return dep; |
d82b3518 | 235 | } |
ed77134b | 236 | EXPORT_SYMBOL_GPL(pm_qos_add_request); |
d82b3518 MG |
237 | |
238 | /** | |
ed77134b MG |
239 | * pm_qos_update_request - modifies an existing qos request |
240 | * @pm_qos_req : handle to list element holding a pm_qos request to use | |
d82b3518 MG |
241 | * @value: defines the qos request |
242 | * | |
ed77134b | 243 | * Updates an existing qos request for the pm_qos_class of parameters along |
d82b3518 MG |
244 | * with updating the target pm_qos_class value. |
245 | * | |
ed77134b | 246 | * Attempts are made to make this code callable on hot code paths. |
d82b3518 | 247 | */ |
ed77134b MG |
248 | void pm_qos_update_request(struct pm_qos_request_list *pm_qos_req, |
249 | s32 new_value) | |
d82b3518 MG |
250 | { |
251 | unsigned long flags; | |
d82b3518 | 252 | int pending_update = 0; |
ed77134b | 253 | s32 temp; |
d82b3518 | 254 | |
25f3a5a2 MG |
255 | if (pm_qos_req) { /*guard against callers passing in null */ |
256 | spin_lock_irqsave(&pm_qos_lock, flags); | |
257 | if (new_value == PM_QOS_DEFAULT_VALUE) | |
258 | temp = pm_qos_array[pm_qos_req->pm_qos_class]->default_value; | |
259 | else | |
260 | temp = new_value; | |
261 | ||
262 | if (temp != pm_qos_req->value) { | |
263 | pending_update = 1; | |
264 | pm_qos_req->value = temp; | |
265 | } | |
266 | spin_unlock_irqrestore(&pm_qos_lock, flags); | |
267 | if (pending_update) | |
268 | update_target(pm_qos_req->pm_qos_class); | |
d82b3518 | 269 | } |
d82b3518 | 270 | } |
ed77134b | 271 | EXPORT_SYMBOL_GPL(pm_qos_update_request); |
d82b3518 MG |
272 | |
273 | /** | |
ed77134b MG |
274 | * pm_qos_remove_request - modifies an existing qos request |
275 | * @pm_qos_req: handle to request list element | |
d82b3518 | 276 | * |
ed77134b MG |
277 | * Will remove pm qos request from the list of requests and |
278 | * recompute the current target value for the pm_qos_class. Call this | |
279 | * on slow code paths. | |
d82b3518 | 280 | */ |
ed77134b | 281 | void pm_qos_remove_request(struct pm_qos_request_list *pm_qos_req) |
d82b3518 MG |
282 | { |
283 | unsigned long flags; | |
ed77134b MG |
284 | int qos_class; |
285 | ||
286 | if (pm_qos_req == NULL) | |
287 | return; | |
288 | /* silent return to keep pcm code cleaner */ | |
d82b3518 | 289 | |
ed77134b | 290 | qos_class = pm_qos_req->pm_qos_class; |
d82b3518 | 291 | spin_lock_irqsave(&pm_qos_lock, flags); |
ed77134b MG |
292 | list_del(&pm_qos_req->list); |
293 | kfree(pm_qos_req); | |
d82b3518 | 294 | spin_unlock_irqrestore(&pm_qos_lock, flags); |
ed77134b | 295 | update_target(qos_class); |
d82b3518 | 296 | } |
ed77134b | 297 | EXPORT_SYMBOL_GPL(pm_qos_remove_request); |
d82b3518 MG |
298 | |
299 | /** | |
300 | * pm_qos_add_notifier - sets notification entry for changes to target value | |
301 | * @pm_qos_class: identifies which qos target changes should be notified. | |
302 | * @notifier: notifier block managed by caller. | |
303 | * | |
304 | * will register the notifier into a notification chain that gets called | |
bf1db69f | 305 | * upon changes to the pm_qos_class target value. |
d82b3518 | 306 | */ |
ed77134b | 307 | int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier) |
d82b3518 MG |
308 | { |
309 | int retval; | |
310 | ||
311 | retval = blocking_notifier_chain_register( | |
312 | pm_qos_array[pm_qos_class]->notifiers, notifier); | |
313 | ||
314 | return retval; | |
315 | } | |
316 | EXPORT_SYMBOL_GPL(pm_qos_add_notifier); | |
317 | ||
318 | /** | |
319 | * pm_qos_remove_notifier - deletes notification entry from chain. | |
320 | * @pm_qos_class: identifies which qos target changes are notified. | |
321 | * @notifier: notifier block to be removed. | |
322 | * | |
323 | * will remove the notifier from the notification chain that gets called | |
bf1db69f | 324 | * upon changes to the pm_qos_class target value. |
d82b3518 MG |
325 | */ |
326 | int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier) | |
327 | { | |
328 | int retval; | |
329 | ||
330 | retval = blocking_notifier_chain_unregister( | |
331 | pm_qos_array[pm_qos_class]->notifiers, notifier); | |
332 | ||
333 | return retval; | |
334 | } | |
335 | EXPORT_SYMBOL_GPL(pm_qos_remove_notifier); | |
336 | ||
d82b3518 MG |
337 | static int pm_qos_power_open(struct inode *inode, struct file *filp) |
338 | { | |
d82b3518 MG |
339 | long pm_qos_class; |
340 | ||
341 | pm_qos_class = find_pm_qos_object_by_minor(iminor(inode)); | |
342 | if (pm_qos_class >= 0) { | |
ed77134b MG |
343 | filp->private_data = (void *) pm_qos_add_request(pm_qos_class, |
344 | PM_QOS_DEFAULT_VALUE); | |
345 | ||
346 | if (filp->private_data) | |
d82b3518 MG |
347 | return 0; |
348 | } | |
d82b3518 MG |
349 | return -EPERM; |
350 | } | |
351 | ||
352 | static int pm_qos_power_release(struct inode *inode, struct file *filp) | |
353 | { | |
ed77134b | 354 | struct pm_qos_request_list *req; |
d82b3518 | 355 | |
ed77134b MG |
356 | req = (struct pm_qos_request_list *)filp->private_data; |
357 | pm_qos_remove_request(req); | |
d82b3518 MG |
358 | |
359 | return 0; | |
360 | } | |
361 | ||
ed77134b | 362 | |
d82b3518 MG |
363 | static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf, |
364 | size_t count, loff_t *f_pos) | |
365 | { | |
366 | s32 value; | |
ed77134b MG |
367 | int x; |
368 | char ascii_value[11]; | |
369 | struct pm_qos_request_list *pm_qos_req; | |
370 | ||
371 | if (count == sizeof(s32)) { | |
372 | if (copy_from_user(&value, buf, sizeof(s32))) | |
373 | return -EFAULT; | |
374 | } else if (count == 11) { /* len('0x12345678/0') */ | |
375 | if (copy_from_user(ascii_value, buf, 11)) | |
376 | return -EFAULT; | |
377 | x = sscanf(ascii_value, "%x", &value); | |
378 | if (x != 1) | |
379 | return -EINVAL; | |
380 | pr_debug(KERN_ERR "%s, %d, 0x%x\n", ascii_value, x, value); | |
381 | } else | |
d82b3518 | 382 | return -EINVAL; |
d82b3518 | 383 | |
ed77134b MG |
384 | pm_qos_req = (struct pm_qos_request_list *)filp->private_data; |
385 | pm_qos_update_request(pm_qos_req, value); | |
386 | ||
387 | return count; | |
d82b3518 MG |
388 | } |
389 | ||
390 | ||
391 | static int __init pm_qos_power_init(void) | |
392 | { | |
393 | int ret = 0; | |
394 | ||
395 | ret = register_pm_qos_misc(&cpu_dma_pm_qos); | |
396 | if (ret < 0) { | |
397 | printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n"); | |
398 | return ret; | |
399 | } | |
400 | ret = register_pm_qos_misc(&network_lat_pm_qos); | |
401 | if (ret < 0) { | |
402 | printk(KERN_ERR "pm_qos_param: network_latency setup failed\n"); | |
403 | return ret; | |
404 | } | |
405 | ret = register_pm_qos_misc(&network_throughput_pm_qos); | |
406 | if (ret < 0) | |
407 | printk(KERN_ERR | |
408 | "pm_qos_param: network_throughput setup failed\n"); | |
409 | ||
410 | return ret; | |
411 | } | |
412 | ||
413 | late_initcall(pm_qos_power_init); |