]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/base/power/qos.c
Merge remote-tracking branch 'signal/for-next'
[karo-tx-linux.git] / drivers / base / power / qos.c
1 /*
2  * Devices PM QoS constraints management
3  *
4  * Copyright (C) 2011 Texas Instruments, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  *
11  * This module exposes the interface to kernel space for specifying
12  * per-device PM QoS dependencies. It provides infrastructure for registration
13  * of:
14  *
15  * Dependents on a QoS value : register requests
16  * Watchers of QoS value : get notified when target QoS value changes
17  *
18  * This QoS design is best effort based. Dependents register their QoS needs.
19  * Watchers register to keep track of the current QoS needs of the system.
20  * Watchers can register different types of notification callbacks:
21  *  . a per-device notification callback using the dev_pm_qos_*_notifier API.
22  *    The notification chain data is stored in the per-device constraint
23  *    data struct.
24  *  . a system-wide notification callback using the dev_pm_qos_*_global_notifier
25  *    API. The notification chain data is stored in a static variable.
26  *
27  * Note about the per-device constraint data struct allocation:
28  * . The per-device constraints data struct ptr is tored into the device
29  *    dev_pm_info.
30  * . To minimize the data usage by the per-device constraints, the data struct
31  *   is only allocated at the first call to dev_pm_qos_add_request.
32  * . The data is later free'd when the device is removed from the system.
33  *  . A global mutex protects the constraints users from the data being
34  *     allocated and free'd.
35  */
36
37 #include <linux/pm_qos.h>
38 #include <linux/spinlock.h>
39 #include <linux/slab.h>
40 #include <linux/device.h>
41 #include <linux/mutex.h>
42 #include <linux/export.h>
43 #include <linux/pm_runtime.h>
44
45 #include "power.h"
46
47 static DEFINE_MUTEX(dev_pm_qos_mtx);
48
49 static BLOCKING_NOTIFIER_HEAD(dev_pm_notifiers);
50
51 /**
52  * __dev_pm_qos_flags - Check PM QoS flags for a given device.
53  * @dev: Device to check the PM QoS flags for.
54  * @mask: Flags to check against.
55  *
56  * This routine must be called with dev->power.lock held.
57  */
58 enum pm_qos_flags_status __dev_pm_qos_flags(struct device *dev, s32 mask)
59 {
60         struct dev_pm_qos *qos = dev->power.qos;
61         struct pm_qos_flags *pqf;
62         s32 val;
63
64         if (!qos)
65                 return PM_QOS_FLAGS_UNDEFINED;
66
67         pqf = &qos->flags;
68         if (list_empty(&pqf->list))
69                 return PM_QOS_FLAGS_UNDEFINED;
70
71         val = pqf->effective_flags & mask;
72         if (val)
73                 return (val == mask) ? PM_QOS_FLAGS_ALL : PM_QOS_FLAGS_SOME;
74
75         return PM_QOS_FLAGS_NONE;
76 }
77
78 /**
79  * dev_pm_qos_flags - Check PM QoS flags for a given device (locked).
80  * @dev: Device to check the PM QoS flags for.
81  * @mask: Flags to check against.
82  */
83 enum pm_qos_flags_status dev_pm_qos_flags(struct device *dev, s32 mask)
84 {
85         unsigned long irqflags;
86         enum pm_qos_flags_status ret;
87
88         spin_lock_irqsave(&dev->power.lock, irqflags);
89         ret = __dev_pm_qos_flags(dev, mask);
90         spin_unlock_irqrestore(&dev->power.lock, irqflags);
91
92         return ret;
93 }
94
95 /**
96  * __dev_pm_qos_read_value - Get PM QoS constraint for a given device.
97  * @dev: Device to get the PM QoS constraint value for.
98  *
99  * This routine must be called with dev->power.lock held.
100  */
101 s32 __dev_pm_qos_read_value(struct device *dev)
102 {
103         return dev->power.qos ? pm_qos_read_value(&dev->power.qos->latency) : 0;
104 }
105
106 /**
107  * dev_pm_qos_read_value - Get PM QoS constraint for a given device (locked).
108  * @dev: Device to get the PM QoS constraint value for.
109  */
110 s32 dev_pm_qos_read_value(struct device *dev)
111 {
112         unsigned long flags;
113         s32 ret;
114
115         spin_lock_irqsave(&dev->power.lock, flags);
116         ret = __dev_pm_qos_read_value(dev);
117         spin_unlock_irqrestore(&dev->power.lock, flags);
118
119         return ret;
120 }
121
122 /**
123  * apply_constraint - Add/modify/remove device PM QoS request.
124  * @req: Constraint request to apply
125  * @action: Action to perform (add/update/remove).
126  * @value: Value to assign to the QoS request.
127  *
128  * Internal function to update the constraints list using the PM QoS core
129  * code and if needed call the per-device and the global notification
130  * callbacks
131  */
132 static int apply_constraint(struct dev_pm_qos_request *req,
133                             enum pm_qos_req_action action, s32 value)
134 {
135         struct dev_pm_qos *qos = req->dev->power.qos;
136         int ret;
137
138         switch(req->type) {
139         case DEV_PM_QOS_LATENCY:
140                 ret = pm_qos_update_target(&qos->latency, &req->data.pnode,
141                                            action, value);
142                 if (ret) {
143                         value = pm_qos_read_value(&qos->latency);
144                         blocking_notifier_call_chain(&dev_pm_notifiers,
145                                                      (unsigned long)value,
146                                                      req);
147                 }
148                 break;
149         case DEV_PM_QOS_FLAGS:
150                 ret = pm_qos_update_flags(&qos->flags, &req->data.flr,
151                                           action, value);
152                 break;
153         default:
154                 ret = -EINVAL;
155         }
156
157         return ret;
158 }
159
160 /*
161  * dev_pm_qos_constraints_allocate
162  * @dev: device to allocate data for
163  *
164  * Called at the first call to add_request, for constraint data allocation
165  * Must be called with the dev_pm_qos_mtx mutex held
166  */
167 static int dev_pm_qos_constraints_allocate(struct device *dev)
168 {
169         struct dev_pm_qos *qos;
170         struct pm_qos_constraints *c;
171         struct blocking_notifier_head *n;
172
173         qos = kzalloc(sizeof(*qos), GFP_KERNEL);
174         if (!qos)
175                 return -ENOMEM;
176
177         n = kzalloc(sizeof(*n), GFP_KERNEL);
178         if (!n) {
179                 kfree(qos);
180                 return -ENOMEM;
181         }
182         BLOCKING_INIT_NOTIFIER_HEAD(n);
183
184         c = &qos->latency;
185         plist_head_init(&c->list);
186         c->target_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
187         c->default_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
188         c->type = PM_QOS_MIN;
189         c->notifiers = n;
190
191         INIT_LIST_HEAD(&qos->flags.list);
192
193         spin_lock_irq(&dev->power.lock);
194         dev->power.qos = qos;
195         spin_unlock_irq(&dev->power.lock);
196
197         return 0;
198 }
199
200 /**
201  * dev_pm_qos_constraints_init - Initalize device's PM QoS constraints pointer.
202  * @dev: target device
203  *
204  * Called from the device PM subsystem during device insertion under
205  * device_pm_lock().
206  */
207 void dev_pm_qos_constraints_init(struct device *dev)
208 {
209         mutex_lock(&dev_pm_qos_mtx);
210         dev->power.qos = NULL;
211         dev->power.power_state = PMSG_ON;
212         mutex_unlock(&dev_pm_qos_mtx);
213 }
214
215 /**
216  * dev_pm_qos_constraints_destroy
217  * @dev: target device
218  *
219  * Called from the device PM subsystem on device removal under device_pm_lock().
220  */
221 void dev_pm_qos_constraints_destroy(struct device *dev)
222 {
223         struct dev_pm_qos *qos;
224         struct dev_pm_qos_request *req, *tmp;
225         struct pm_qos_constraints *c;
226
227         /*
228          * If the device's PM QoS resume latency limit has been exposed to user
229          * space, it has to be hidden at this point.
230          */
231         dev_pm_qos_hide_latency_limit(dev);
232
233         mutex_lock(&dev_pm_qos_mtx);
234
235         dev->power.power_state = PMSG_INVALID;
236         qos = dev->power.qos;
237         if (!qos)
238                 goto out;
239
240         c = &qos->latency;
241         /* Flush the constraints list for the device */
242         plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
243                 /*
244                  * Update constraints list and call the notification
245                  * callbacks if needed
246                  */
247                 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
248                 memset(req, 0, sizeof(*req));
249         }
250
251         spin_lock_irq(&dev->power.lock);
252         dev->power.qos = NULL;
253         spin_unlock_irq(&dev->power.lock);
254
255         kfree(c->notifiers);
256         kfree(c);
257
258  out:
259         mutex_unlock(&dev_pm_qos_mtx);
260 }
261
262 /**
263  * dev_pm_qos_add_request - inserts new qos request into the list
264  * @dev: target device for the constraint
265  * @req: pointer to a preallocated handle
266  * @type: type of the request
267  * @value: defines the qos request
268  *
269  * This function inserts a new entry in the device constraints list of
270  * requested qos performance characteristics. It recomputes the aggregate
271  * QoS expectations of parameters and initializes the dev_pm_qos_request
272  * handle.  Caller needs to save this handle for later use in updates and
273  * removal.
274  *
275  * Returns 1 if the aggregated constraint value has changed,
276  * 0 if the aggregated constraint value has not changed,
277  * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory
278  * to allocate for data structures, -ENODEV if the device has just been removed
279  * from the system.
280  */
281 int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
282                            enum dev_pm_qos_req_type type, s32 value)
283 {
284         int ret = 0;
285
286         if (!dev || !req) /*guard against callers passing in null */
287                 return -EINVAL;
288
289         if (WARN(dev_pm_qos_request_active(req),
290                  "%s() called for already added request\n", __func__))
291                 return -EINVAL;
292
293         req->dev = dev;
294
295         mutex_lock(&dev_pm_qos_mtx);
296
297         if (!dev->power.qos) {
298                 if (dev->power.power_state.event == PM_EVENT_INVALID) {
299                         /* The device has been removed from the system. */
300                         req->dev = NULL;
301                         ret = -ENODEV;
302                         goto out;
303                 } else {
304                         /*
305                          * Allocate the constraints data on the first call to
306                          * add_request, i.e. only if the data is not already
307                          * allocated and if the device has not been removed.
308                          */
309                         ret = dev_pm_qos_constraints_allocate(dev);
310                 }
311         }
312
313         if (!ret) {
314                 req->type = type;
315                 ret = apply_constraint(req, PM_QOS_ADD_REQ, value);
316         }
317
318  out:
319         mutex_unlock(&dev_pm_qos_mtx);
320
321         return ret;
322 }
323 EXPORT_SYMBOL_GPL(dev_pm_qos_add_request);
324
325 /**
326  * __dev_pm_qos_update_request - Modify an existing device PM QoS request.
327  * @req : PM QoS request to modify.
328  * @new_value: New value to request.
329  */
330 static int __dev_pm_qos_update_request(struct dev_pm_qos_request *req,
331                                        s32 new_value)
332 {
333         s32 curr_value;
334         int ret = 0;
335
336         if (!req->dev->power.qos)
337                 return -ENODEV;
338
339         switch(req->type) {
340         case DEV_PM_QOS_LATENCY:
341                 curr_value = req->data.pnode.prio;
342                 break;
343         case DEV_PM_QOS_FLAGS:
344                 curr_value = req->data.flr.flags;
345                 break;
346         default:
347                 return -EINVAL;
348         }
349
350         if (curr_value != new_value)
351                 ret = apply_constraint(req, PM_QOS_UPDATE_REQ, new_value);
352
353         return ret;
354 }
355
356 /**
357  * dev_pm_qos_update_request - modifies an existing qos request
358  * @req : handle to list element holding a dev_pm_qos request to use
359  * @new_value: defines the qos request
360  *
361  * Updates an existing dev PM qos request along with updating the
362  * target value.
363  *
364  * Attempts are made to make this code callable on hot code paths.
365  *
366  * Returns 1 if the aggregated constraint value has changed,
367  * 0 if the aggregated constraint value has not changed,
368  * -EINVAL in case of wrong parameters, -ENODEV if the device has been
369  * removed from the system
370  */
371 int dev_pm_qos_update_request(struct dev_pm_qos_request *req, s32 new_value)
372 {
373         int ret;
374
375         if (!req) /*guard against callers passing in null */
376                 return -EINVAL;
377
378         if (WARN(!dev_pm_qos_request_active(req),
379                  "%s() called for unknown object\n", __func__))
380                 return -EINVAL;
381
382         mutex_lock(&dev_pm_qos_mtx);
383         __dev_pm_qos_update_request(req, new_value);
384         mutex_unlock(&dev_pm_qos_mtx);
385
386         return ret;
387 }
388 EXPORT_SYMBOL_GPL(dev_pm_qos_update_request);
389
390 /**
391  * dev_pm_qos_remove_request - modifies an existing qos request
392  * @req: handle to request list element
393  *
394  * Will remove pm qos request from the list of constraints and
395  * recompute the current target value. Call this on slow code paths.
396  *
397  * Returns 1 if the aggregated constraint value has changed,
398  * 0 if the aggregated constraint value has not changed,
399  * -EINVAL in case of wrong parameters, -ENODEV if the device has been
400  * removed from the system
401  */
402 int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
403 {
404         int ret = 0;
405
406         if (!req) /*guard against callers passing in null */
407                 return -EINVAL;
408
409         if (WARN(!dev_pm_qos_request_active(req),
410                  "%s() called for unknown object\n", __func__))
411                 return -EINVAL;
412
413         mutex_lock(&dev_pm_qos_mtx);
414
415         if (req->dev->power.qos) {
416                 ret = apply_constraint(req, PM_QOS_REMOVE_REQ,
417                                        PM_QOS_DEFAULT_VALUE);
418                 memset(req, 0, sizeof(*req));
419         } else {
420                 /* Return if the device has been removed */
421                 ret = -ENODEV;
422         }
423
424         mutex_unlock(&dev_pm_qos_mtx);
425         return ret;
426 }
427 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request);
428
429 /**
430  * dev_pm_qos_add_notifier - sets notification entry for changes to target value
431  * of per-device PM QoS constraints
432  *
433  * @dev: target device for the constraint
434  * @notifier: notifier block managed by caller.
435  *
436  * Will register the notifier into a notification chain that gets called
437  * upon changes to the target value for the device.
438  *
439  * If the device's constraints object doesn't exist when this routine is called,
440  * it will be created (or error code will be returned if that fails).
441  */
442 int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier)
443 {
444         int ret = 0;
445
446         mutex_lock(&dev_pm_qos_mtx);
447
448         if (!dev->power.qos)
449                 ret = dev->power.power_state.event != PM_EVENT_INVALID ?
450                         dev_pm_qos_constraints_allocate(dev) : -ENODEV;
451
452         if (!ret)
453                 ret = blocking_notifier_chain_register(
454                                 dev->power.qos->latency.notifiers, notifier);
455
456         mutex_unlock(&dev_pm_qos_mtx);
457         return ret;
458 }
459 EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier);
460
461 /**
462  * dev_pm_qos_remove_notifier - deletes notification for changes to target value
463  * of per-device PM QoS constraints
464  *
465  * @dev: target device for the constraint
466  * @notifier: notifier block to be removed.
467  *
468  * Will remove the notifier from the notification chain that gets called
469  * upon changes to the target value.
470  */
471 int dev_pm_qos_remove_notifier(struct device *dev,
472                                struct notifier_block *notifier)
473 {
474         int retval = 0;
475
476         mutex_lock(&dev_pm_qos_mtx);
477
478         /* Silently return if the constraints object is not present. */
479         if (dev->power.qos)
480                 retval = blocking_notifier_chain_unregister(
481                                 dev->power.qos->latency.notifiers,
482                                 notifier);
483
484         mutex_unlock(&dev_pm_qos_mtx);
485         return retval;
486 }
487 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier);
488
489 /**
490  * dev_pm_qos_add_global_notifier - sets notification entry for changes to
491  * target value of the PM QoS constraints for any device
492  *
493  * @notifier: notifier block managed by caller.
494  *
495  * Will register the notifier into a notification chain that gets called
496  * upon changes to the target value for any device.
497  */
498 int dev_pm_qos_add_global_notifier(struct notifier_block *notifier)
499 {
500         return blocking_notifier_chain_register(&dev_pm_notifiers, notifier);
501 }
502 EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier);
503
504 /**
505  * dev_pm_qos_remove_global_notifier - deletes notification for changes to
506  * target value of PM QoS constraints for any device
507  *
508  * @notifier: notifier block to be removed.
509  *
510  * Will remove the notifier from the notification chain that gets called
511  * upon changes to the target value for any device.
512  */
513 int dev_pm_qos_remove_global_notifier(struct notifier_block *notifier)
514 {
515         return blocking_notifier_chain_unregister(&dev_pm_notifiers, notifier);
516 }
517 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier);
518
519 /**
520  * dev_pm_qos_add_ancestor_request - Add PM QoS request for device's ancestor.
521  * @dev: Device whose ancestor to add the request for.
522  * @req: Pointer to the preallocated handle.
523  * @value: Constraint latency value.
524  */
525 int dev_pm_qos_add_ancestor_request(struct device *dev,
526                                     struct dev_pm_qos_request *req, s32 value)
527 {
528         struct device *ancestor = dev->parent;
529         int error = -ENODEV;
530
531         while (ancestor && !ancestor->power.ignore_children)
532                 ancestor = ancestor->parent;
533
534         if (ancestor)
535                 error = dev_pm_qos_add_request(ancestor, req,
536                                                DEV_PM_QOS_LATENCY, value);
537
538         if (error)
539                 req->dev = NULL;
540
541         return error;
542 }
543 EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request);
544
545 #ifdef CONFIG_PM_RUNTIME
546 static void __dev_pm_qos_drop_user_request(struct device *dev,
547                                            enum dev_pm_qos_req_type type)
548 {
549         switch(type) {
550         case DEV_PM_QOS_LATENCY:
551                 dev_pm_qos_remove_request(dev->power.qos->latency_req);
552                 dev->power.qos->latency_req = NULL;
553                 break;
554         case DEV_PM_QOS_FLAGS:
555                 dev_pm_qos_remove_request(dev->power.qos->flags_req);
556                 dev->power.qos->flags_req = NULL;
557                 break;
558         }
559 }
560
561 /**
562  * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space.
563  * @dev: Device whose PM QoS latency limit is to be exposed to user space.
564  * @value: Initial value of the latency limit.
565  */
566 int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value)
567 {
568         struct dev_pm_qos_request *req;
569         int ret;
570
571         if (!device_is_registered(dev) || value < 0)
572                 return -EINVAL;
573
574         if (dev->power.qos && dev->power.qos->latency_req)
575                 return -EEXIST;
576
577         req = kzalloc(sizeof(*req), GFP_KERNEL);
578         if (!req)
579                 return -ENOMEM;
580
581         ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_LATENCY, value);
582         if (ret < 0)
583                 return ret;
584
585         dev->power.qos->latency_req = req;
586         ret = pm_qos_sysfs_add_latency(dev);
587         if (ret)
588                 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY);
589
590         return ret;
591 }
592 EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit);
593
594 /**
595  * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space.
596  * @dev: Device whose PM QoS latency limit is to be hidden from user space.
597  */
598 void dev_pm_qos_hide_latency_limit(struct device *dev)
599 {
600         if (dev->power.qos && dev->power.qos->latency_req) {
601                 pm_qos_sysfs_remove_latency(dev);
602                 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY);
603         }
604 }
605 EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit);
606
607 /**
608  * dev_pm_qos_expose_flags - Expose PM QoS flags of a device to user space.
609  * @dev: Device whose PM QoS flags are to be exposed to user space.
610  * @val: Initial values of the flags.
611  */
612 int dev_pm_qos_expose_flags(struct device *dev, s32 val)
613 {
614         struct dev_pm_qos_request *req;
615         int ret;
616
617         if (!device_is_registered(dev))
618                 return -EINVAL;
619
620         if (dev->power.qos && dev->power.qos->flags_req)
621                 return -EEXIST;
622
623         req = kzalloc(sizeof(*req), GFP_KERNEL);
624         if (!req)
625                 return -ENOMEM;
626
627         ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_FLAGS, val);
628         if (ret < 0)
629                 return ret;
630
631         dev->power.qos->flags_req = req;
632         ret = pm_qos_sysfs_add_flags(dev);
633         if (ret)
634                 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
635
636         return ret;
637 }
638 EXPORT_SYMBOL_GPL(dev_pm_qos_expose_flags);
639
640 /**
641  * dev_pm_qos_hide_flags - Hide PM QoS flags of a device from user space.
642  * @dev: Device whose PM QoS flags are to be hidden from user space.
643  */
644 void dev_pm_qos_hide_flags(struct device *dev)
645 {
646         if (dev->power.qos && dev->power.qos->flags_req) {
647                 pm_qos_sysfs_remove_flags(dev);
648                 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
649         }
650 }
651 EXPORT_SYMBOL_GPL(dev_pm_qos_hide_flags);
652
653 /**
654  * dev_pm_qos_update_flags - Update PM QoS flags request owned by user space.
655  * @dev: Device to update the PM QoS flags request for.
656  * @mask: Flags to set/clear.
657  * @set: Whether to set or clear the flags (true means set).
658  */
659 int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set)
660 {
661         s32 value;
662         int ret;
663
664         if (!dev->power.qos || !dev->power.qos->flags_req)
665                 return -EINVAL;
666
667         pm_runtime_get_sync(dev);
668         mutex_lock(&dev_pm_qos_mtx);
669
670         value = dev_pm_qos_requested_flags(dev);
671         if (set)
672                 value |= mask;
673         else
674                 value &= ~mask;
675
676         ret = __dev_pm_qos_update_request(dev->power.qos->flags_req, value);
677
678         mutex_unlock(&dev_pm_qos_mtx);
679         pm_runtime_put(dev);
680
681         return ret;
682 }
683 #endif /* CONFIG_PM_RUNTIME */