]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/base/power/qos.c
Merge branches 'pm-core', 'pm-qos', 'pm-domains' and 'pm-opp'
[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 a per-device notification callback using the
21  * dev_pm_qos_*_notifier API. The notification chain data is stored in the
22  * per-device constraint data struct.
23  *
24  * Note about the per-device constraint data struct allocation:
25  * . The per-device constraints data struct ptr is tored into the device
26  *    dev_pm_info.
27  * . To minimize the data usage by the per-device constraints, the data struct
28  *   is only allocated at the first call to dev_pm_qos_add_request.
29  * . The data is later free'd when the device is removed from the system.
30  *  . A global mutex protects the constraints users from the data being
31  *     allocated and free'd.
32  */
33
34 #include <linux/pm_qos.h>
35 #include <linux/spinlock.h>
36 #include <linux/slab.h>
37 #include <linux/device.h>
38 #include <linux/mutex.h>
39 #include <linux/export.h>
40 #include <linux/pm_runtime.h>
41 #include <linux/err.h>
42 #include <trace/events/power.h>
43
44 #include "power.h"
45
46 static DEFINE_MUTEX(dev_pm_qos_mtx);
47 static DEFINE_MUTEX(dev_pm_qos_sysfs_mtx);
48
49 /**
50  * __dev_pm_qos_flags - Check PM QoS flags for a given device.
51  * @dev: Device to check the PM QoS flags for.
52  * @mask: Flags to check against.
53  *
54  * This routine must be called with dev->power.lock held.
55  */
56 enum pm_qos_flags_status __dev_pm_qos_flags(struct device *dev, s32 mask)
57 {
58         struct dev_pm_qos *qos = dev->power.qos;
59         struct pm_qos_flags *pqf;
60         s32 val;
61
62         lockdep_assert_held(&dev->power.lock);
63
64         if (IS_ERR_OR_NULL(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 EXPORT_SYMBOL_GPL(dev_pm_qos_flags);
95
96 /**
97  * __dev_pm_qos_read_value - Get PM QoS constraint for a given device.
98  * @dev: Device to get the PM QoS constraint value for.
99  *
100  * This routine must be called with dev->power.lock held.
101  */
102 s32 __dev_pm_qos_read_value(struct device *dev)
103 {
104         lockdep_assert_held(&dev->power.lock);
105
106         return IS_ERR_OR_NULL(dev->power.qos) ?
107                 0 : pm_qos_read_value(&dev->power.qos->resume_latency);
108 }
109
110 /**
111  * dev_pm_qos_read_value - Get PM QoS constraint for a given device (locked).
112  * @dev: Device to get the PM QoS constraint value for.
113  */
114 s32 dev_pm_qos_read_value(struct device *dev)
115 {
116         unsigned long flags;
117         s32 ret;
118
119         spin_lock_irqsave(&dev->power.lock, flags);
120         ret = __dev_pm_qos_read_value(dev);
121         spin_unlock_irqrestore(&dev->power.lock, flags);
122
123         return ret;
124 }
125
126 /**
127  * apply_constraint - Add/modify/remove device PM QoS request.
128  * @req: Constraint request to apply
129  * @action: Action to perform (add/update/remove).
130  * @value: Value to assign to the QoS request.
131  *
132  * Internal function to update the constraints list using the PM QoS core
133  * code and if needed call the per-device callbacks.
134  */
135 static int apply_constraint(struct dev_pm_qos_request *req,
136                             enum pm_qos_req_action action, s32 value)
137 {
138         struct dev_pm_qos *qos = req->dev->power.qos;
139         int ret;
140
141         switch(req->type) {
142         case DEV_PM_QOS_RESUME_LATENCY:
143                 ret = pm_qos_update_target(&qos->resume_latency,
144                                            &req->data.pnode, action, value);
145                 break;
146         case DEV_PM_QOS_LATENCY_TOLERANCE:
147                 ret = pm_qos_update_target(&qos->latency_tolerance,
148                                            &req->data.pnode, action, value);
149                 if (ret) {
150                         value = pm_qos_read_value(&qos->latency_tolerance);
151                         req->dev->power.set_latency_tolerance(req->dev, value);
152                 }
153                 break;
154         case DEV_PM_QOS_FLAGS:
155                 ret = pm_qos_update_flags(&qos->flags, &req->data.flr,
156                                           action, value);
157                 break;
158         default:
159                 ret = -EINVAL;
160         }
161
162         return ret;
163 }
164
165 /*
166  * dev_pm_qos_constraints_allocate
167  * @dev: device to allocate data for
168  *
169  * Called at the first call to add_request, for constraint data allocation
170  * Must be called with the dev_pm_qos_mtx mutex held
171  */
172 static int dev_pm_qos_constraints_allocate(struct device *dev)
173 {
174         struct dev_pm_qos *qos;
175         struct pm_qos_constraints *c;
176         struct blocking_notifier_head *n;
177
178         qos = kzalloc(sizeof(*qos), GFP_KERNEL);
179         if (!qos)
180                 return -ENOMEM;
181
182         n = kzalloc(sizeof(*n), GFP_KERNEL);
183         if (!n) {
184                 kfree(qos);
185                 return -ENOMEM;
186         }
187         BLOCKING_INIT_NOTIFIER_HEAD(n);
188
189         c = &qos->resume_latency;
190         plist_head_init(&c->list);
191         c->target_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE;
192         c->default_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE;
193         c->no_constraint_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE;
194         c->type = PM_QOS_MIN;
195         c->notifiers = n;
196
197         c = &qos->latency_tolerance;
198         plist_head_init(&c->list);
199         c->target_value = PM_QOS_LATENCY_TOLERANCE_DEFAULT_VALUE;
200         c->default_value = PM_QOS_LATENCY_TOLERANCE_DEFAULT_VALUE;
201         c->no_constraint_value = PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT;
202         c->type = PM_QOS_MIN;
203
204         INIT_LIST_HEAD(&qos->flags.list);
205
206         spin_lock_irq(&dev->power.lock);
207         dev->power.qos = qos;
208         spin_unlock_irq(&dev->power.lock);
209
210         return 0;
211 }
212
213 static void __dev_pm_qos_hide_latency_limit(struct device *dev);
214 static void __dev_pm_qos_hide_flags(struct device *dev);
215
216 /**
217  * dev_pm_qos_constraints_destroy
218  * @dev: target device
219  *
220  * Called from the device PM subsystem on device removal under device_pm_lock().
221  */
222 void dev_pm_qos_constraints_destroy(struct device *dev)
223 {
224         struct dev_pm_qos *qos;
225         struct dev_pm_qos_request *req, *tmp;
226         struct pm_qos_constraints *c;
227         struct pm_qos_flags *f;
228
229         mutex_lock(&dev_pm_qos_sysfs_mtx);
230
231         /*
232          * If the device's PM QoS resume latency limit or PM QoS flags have been
233          * exposed to user space, they have to be hidden at this point.
234          */
235         pm_qos_sysfs_remove_resume_latency(dev);
236         pm_qos_sysfs_remove_flags(dev);
237
238         mutex_lock(&dev_pm_qos_mtx);
239
240         __dev_pm_qos_hide_latency_limit(dev);
241         __dev_pm_qos_hide_flags(dev);
242
243         qos = dev->power.qos;
244         if (!qos)
245                 goto out;
246
247         /* Flush the constraints lists for the device. */
248         c = &qos->resume_latency;
249         plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
250                 /*
251                  * Update constraints list and call the notification
252                  * callbacks if needed
253                  */
254                 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
255                 memset(req, 0, sizeof(*req));
256         }
257         c = &qos->latency_tolerance;
258         plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
259                 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
260                 memset(req, 0, sizeof(*req));
261         }
262         f = &qos->flags;
263         list_for_each_entry_safe(req, tmp, &f->list, data.flr.node) {
264                 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
265                 memset(req, 0, sizeof(*req));
266         }
267
268         spin_lock_irq(&dev->power.lock);
269         dev->power.qos = ERR_PTR(-ENODEV);
270         spin_unlock_irq(&dev->power.lock);
271
272         kfree(qos->resume_latency.notifiers);
273         kfree(qos);
274
275  out:
276         mutex_unlock(&dev_pm_qos_mtx);
277
278         mutex_unlock(&dev_pm_qos_sysfs_mtx);
279 }
280
281 static bool dev_pm_qos_invalid_request(struct device *dev,
282                                        struct dev_pm_qos_request *req)
283 {
284         return !req || (req->type == DEV_PM_QOS_LATENCY_TOLERANCE
285                         && !dev->power.set_latency_tolerance);
286 }
287
288 static int __dev_pm_qos_add_request(struct device *dev,
289                                     struct dev_pm_qos_request *req,
290                                     enum dev_pm_qos_req_type type, s32 value)
291 {
292         int ret = 0;
293
294         if (!dev || dev_pm_qos_invalid_request(dev, req))
295                 return -EINVAL;
296
297         if (WARN(dev_pm_qos_request_active(req),
298                  "%s() called for already added request\n", __func__))
299                 return -EINVAL;
300
301         if (IS_ERR(dev->power.qos))
302                 ret = -ENODEV;
303         else if (!dev->power.qos)
304                 ret = dev_pm_qos_constraints_allocate(dev);
305
306         trace_dev_pm_qos_add_request(dev_name(dev), type, value);
307         if (!ret) {
308                 req->dev = dev;
309                 req->type = type;
310                 ret = apply_constraint(req, PM_QOS_ADD_REQ, value);
311         }
312         return ret;
313 }
314
315 /**
316  * dev_pm_qos_add_request - inserts new qos request into the list
317  * @dev: target device for the constraint
318  * @req: pointer to a preallocated handle
319  * @type: type of the request
320  * @value: defines the qos request
321  *
322  * This function inserts a new entry in the device constraints list of
323  * requested qos performance characteristics. It recomputes the aggregate
324  * QoS expectations of parameters and initializes the dev_pm_qos_request
325  * handle.  Caller needs to save this handle for later use in updates and
326  * removal.
327  *
328  * Returns 1 if the aggregated constraint value has changed,
329  * 0 if the aggregated constraint value has not changed,
330  * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory
331  * to allocate for data structures, -ENODEV if the device has just been removed
332  * from the system.
333  *
334  * Callers should ensure that the target device is not RPM_SUSPENDED before
335  * using this function for requests of type DEV_PM_QOS_FLAGS.
336  */
337 int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
338                            enum dev_pm_qos_req_type type, s32 value)
339 {
340         int ret;
341
342         mutex_lock(&dev_pm_qos_mtx);
343         ret = __dev_pm_qos_add_request(dev, req, type, value);
344         mutex_unlock(&dev_pm_qos_mtx);
345         return ret;
346 }
347 EXPORT_SYMBOL_GPL(dev_pm_qos_add_request);
348
349 /**
350  * __dev_pm_qos_update_request - Modify an existing device PM QoS request.
351  * @req : PM QoS request to modify.
352  * @new_value: New value to request.
353  */
354 static int __dev_pm_qos_update_request(struct dev_pm_qos_request *req,
355                                        s32 new_value)
356 {
357         s32 curr_value;
358         int ret = 0;
359
360         if (!req) /*guard against callers passing in null */
361                 return -EINVAL;
362
363         if (WARN(!dev_pm_qos_request_active(req),
364                  "%s() called for unknown object\n", __func__))
365                 return -EINVAL;
366
367         if (IS_ERR_OR_NULL(req->dev->power.qos))
368                 return -ENODEV;
369
370         switch(req->type) {
371         case DEV_PM_QOS_RESUME_LATENCY:
372         case DEV_PM_QOS_LATENCY_TOLERANCE:
373                 curr_value = req->data.pnode.prio;
374                 break;
375         case DEV_PM_QOS_FLAGS:
376                 curr_value = req->data.flr.flags;
377                 break;
378         default:
379                 return -EINVAL;
380         }
381
382         trace_dev_pm_qos_update_request(dev_name(req->dev), req->type,
383                                         new_value);
384         if (curr_value != new_value)
385                 ret = apply_constraint(req, PM_QOS_UPDATE_REQ, new_value);
386
387         return ret;
388 }
389
390 /**
391  * dev_pm_qos_update_request - modifies an existing qos request
392  * @req : handle to list element holding a dev_pm_qos request to use
393  * @new_value: defines the qos request
394  *
395  * Updates an existing dev PM qos request along with updating the
396  * target value.
397  *
398  * Attempts are made to make this code callable on hot code paths.
399  *
400  * Returns 1 if the aggregated constraint value has changed,
401  * 0 if the aggregated constraint value has not changed,
402  * -EINVAL in case of wrong parameters, -ENODEV if the device has been
403  * removed from the system
404  *
405  * Callers should ensure that the target device is not RPM_SUSPENDED before
406  * using this function for requests of type DEV_PM_QOS_FLAGS.
407  */
408 int dev_pm_qos_update_request(struct dev_pm_qos_request *req, s32 new_value)
409 {
410         int ret;
411
412         mutex_lock(&dev_pm_qos_mtx);
413         ret = __dev_pm_qos_update_request(req, new_value);
414         mutex_unlock(&dev_pm_qos_mtx);
415         return ret;
416 }
417 EXPORT_SYMBOL_GPL(dev_pm_qos_update_request);
418
419 static int __dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
420 {
421         int ret;
422
423         if (!req) /*guard against callers passing in null */
424                 return -EINVAL;
425
426         if (WARN(!dev_pm_qos_request_active(req),
427                  "%s() called for unknown object\n", __func__))
428                 return -EINVAL;
429
430         if (IS_ERR_OR_NULL(req->dev->power.qos))
431                 return -ENODEV;
432
433         trace_dev_pm_qos_remove_request(dev_name(req->dev), req->type,
434                                         PM_QOS_DEFAULT_VALUE);
435         ret = apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
436         memset(req, 0, sizeof(*req));
437         return ret;
438 }
439
440 /**
441  * dev_pm_qos_remove_request - modifies an existing qos request
442  * @req: handle to request list element
443  *
444  * Will remove pm qos request from the list of constraints and
445  * recompute the current target value. Call this on slow code paths.
446  *
447  * Returns 1 if the aggregated constraint value has changed,
448  * 0 if the aggregated constraint value has not changed,
449  * -EINVAL in case of wrong parameters, -ENODEV if the device has been
450  * removed from the system
451  *
452  * Callers should ensure that the target device is not RPM_SUSPENDED before
453  * using this function for requests of type DEV_PM_QOS_FLAGS.
454  */
455 int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
456 {
457         int ret;
458
459         mutex_lock(&dev_pm_qos_mtx);
460         ret = __dev_pm_qos_remove_request(req);
461         mutex_unlock(&dev_pm_qos_mtx);
462         return ret;
463 }
464 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request);
465
466 /**
467  * dev_pm_qos_add_notifier - sets notification entry for changes to target value
468  * of per-device PM QoS constraints
469  *
470  * @dev: target device for the constraint
471  * @notifier: notifier block managed by caller.
472  *
473  * Will register the notifier into a notification chain that gets called
474  * upon changes to the target value for the device.
475  *
476  * If the device's constraints object doesn't exist when this routine is called,
477  * it will be created (or error code will be returned if that fails).
478  */
479 int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier)
480 {
481         int ret = 0;
482
483         mutex_lock(&dev_pm_qos_mtx);
484
485         if (IS_ERR(dev->power.qos))
486                 ret = -ENODEV;
487         else if (!dev->power.qos)
488                 ret = dev_pm_qos_constraints_allocate(dev);
489
490         if (!ret)
491                 ret = blocking_notifier_chain_register(dev->power.qos->resume_latency.notifiers,
492                                                        notifier);
493
494         mutex_unlock(&dev_pm_qos_mtx);
495         return ret;
496 }
497 EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier);
498
499 /**
500  * dev_pm_qos_remove_notifier - deletes notification for changes to target value
501  * of per-device PM QoS constraints
502  *
503  * @dev: target device for the constraint
504  * @notifier: notifier block to be removed.
505  *
506  * Will remove the notifier from the notification chain that gets called
507  * upon changes to the target value.
508  */
509 int dev_pm_qos_remove_notifier(struct device *dev,
510                                struct notifier_block *notifier)
511 {
512         int retval = 0;
513
514         mutex_lock(&dev_pm_qos_mtx);
515
516         /* Silently return if the constraints object is not present. */
517         if (!IS_ERR_OR_NULL(dev->power.qos))
518                 retval = blocking_notifier_chain_unregister(dev->power.qos->resume_latency.notifiers,
519                                                             notifier);
520
521         mutex_unlock(&dev_pm_qos_mtx);
522         return retval;
523 }
524 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier);
525
526 /**
527  * dev_pm_qos_add_ancestor_request - Add PM QoS request for device's ancestor.
528  * @dev: Device whose ancestor to add the request for.
529  * @req: Pointer to the preallocated handle.
530  * @type: Type of the request.
531  * @value: Constraint latency value.
532  */
533 int dev_pm_qos_add_ancestor_request(struct device *dev,
534                                     struct dev_pm_qos_request *req,
535                                     enum dev_pm_qos_req_type type, s32 value)
536 {
537         struct device *ancestor = dev->parent;
538         int ret = -ENODEV;
539
540         switch (type) {
541         case DEV_PM_QOS_RESUME_LATENCY:
542                 while (ancestor && !ancestor->power.ignore_children)
543                         ancestor = ancestor->parent;
544
545                 break;
546         case DEV_PM_QOS_LATENCY_TOLERANCE:
547                 while (ancestor && !ancestor->power.set_latency_tolerance)
548                         ancestor = ancestor->parent;
549
550                 break;
551         default:
552                 ancestor = NULL;
553         }
554         if (ancestor)
555                 ret = dev_pm_qos_add_request(ancestor, req, type, value);
556
557         if (ret < 0)
558                 req->dev = NULL;
559
560         return ret;
561 }
562 EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request);
563
564 static void __dev_pm_qos_drop_user_request(struct device *dev,
565                                            enum dev_pm_qos_req_type type)
566 {
567         struct dev_pm_qos_request *req = NULL;
568
569         switch(type) {
570         case DEV_PM_QOS_RESUME_LATENCY:
571                 req = dev->power.qos->resume_latency_req;
572                 dev->power.qos->resume_latency_req = NULL;
573                 break;
574         case DEV_PM_QOS_LATENCY_TOLERANCE:
575                 req = dev->power.qos->latency_tolerance_req;
576                 dev->power.qos->latency_tolerance_req = NULL;
577                 break;
578         case DEV_PM_QOS_FLAGS:
579                 req = dev->power.qos->flags_req;
580                 dev->power.qos->flags_req = NULL;
581                 break;
582         }
583         __dev_pm_qos_remove_request(req);
584         kfree(req);
585 }
586
587 static void dev_pm_qos_drop_user_request(struct device *dev,
588                                          enum dev_pm_qos_req_type type)
589 {
590         mutex_lock(&dev_pm_qos_mtx);
591         __dev_pm_qos_drop_user_request(dev, type);
592         mutex_unlock(&dev_pm_qos_mtx);
593 }
594
595 /**
596  * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space.
597  * @dev: Device whose PM QoS latency limit is to be exposed to user space.
598  * @value: Initial value of the latency limit.
599  */
600 int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value)
601 {
602         struct dev_pm_qos_request *req;
603         int ret;
604
605         if (!device_is_registered(dev) || value < 0)
606                 return -EINVAL;
607
608         req = kzalloc(sizeof(*req), GFP_KERNEL);
609         if (!req)
610                 return -ENOMEM;
611
612         ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_RESUME_LATENCY, value);
613         if (ret < 0) {
614                 kfree(req);
615                 return ret;
616         }
617
618         mutex_lock(&dev_pm_qos_sysfs_mtx);
619
620         mutex_lock(&dev_pm_qos_mtx);
621
622         if (IS_ERR_OR_NULL(dev->power.qos))
623                 ret = -ENODEV;
624         else if (dev->power.qos->resume_latency_req)
625                 ret = -EEXIST;
626
627         if (ret < 0) {
628                 __dev_pm_qos_remove_request(req);
629                 kfree(req);
630                 mutex_unlock(&dev_pm_qos_mtx);
631                 goto out;
632         }
633         dev->power.qos->resume_latency_req = req;
634
635         mutex_unlock(&dev_pm_qos_mtx);
636
637         ret = pm_qos_sysfs_add_resume_latency(dev);
638         if (ret)
639                 dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_RESUME_LATENCY);
640
641  out:
642         mutex_unlock(&dev_pm_qos_sysfs_mtx);
643         return ret;
644 }
645 EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit);
646
647 static void __dev_pm_qos_hide_latency_limit(struct device *dev)
648 {
649         if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->resume_latency_req)
650                 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_RESUME_LATENCY);
651 }
652
653 /**
654  * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space.
655  * @dev: Device whose PM QoS latency limit is to be hidden from user space.
656  */
657 void dev_pm_qos_hide_latency_limit(struct device *dev)
658 {
659         mutex_lock(&dev_pm_qos_sysfs_mtx);
660
661         pm_qos_sysfs_remove_resume_latency(dev);
662
663         mutex_lock(&dev_pm_qos_mtx);
664         __dev_pm_qos_hide_latency_limit(dev);
665         mutex_unlock(&dev_pm_qos_mtx);
666
667         mutex_unlock(&dev_pm_qos_sysfs_mtx);
668 }
669 EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit);
670
671 /**
672  * dev_pm_qos_expose_flags - Expose PM QoS flags of a device to user space.
673  * @dev: Device whose PM QoS flags are to be exposed to user space.
674  * @val: Initial values of the flags.
675  */
676 int dev_pm_qos_expose_flags(struct device *dev, s32 val)
677 {
678         struct dev_pm_qos_request *req;
679         int ret;
680
681         if (!device_is_registered(dev))
682                 return -EINVAL;
683
684         req = kzalloc(sizeof(*req), GFP_KERNEL);
685         if (!req)
686                 return -ENOMEM;
687
688         ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_FLAGS, val);
689         if (ret < 0) {
690                 kfree(req);
691                 return ret;
692         }
693
694         pm_runtime_get_sync(dev);
695         mutex_lock(&dev_pm_qos_sysfs_mtx);
696
697         mutex_lock(&dev_pm_qos_mtx);
698
699         if (IS_ERR_OR_NULL(dev->power.qos))
700                 ret = -ENODEV;
701         else if (dev->power.qos->flags_req)
702                 ret = -EEXIST;
703
704         if (ret < 0) {
705                 __dev_pm_qos_remove_request(req);
706                 kfree(req);
707                 mutex_unlock(&dev_pm_qos_mtx);
708                 goto out;
709         }
710         dev->power.qos->flags_req = req;
711
712         mutex_unlock(&dev_pm_qos_mtx);
713
714         ret = pm_qos_sysfs_add_flags(dev);
715         if (ret)
716                 dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
717
718  out:
719         mutex_unlock(&dev_pm_qos_sysfs_mtx);
720         pm_runtime_put(dev);
721         return ret;
722 }
723 EXPORT_SYMBOL_GPL(dev_pm_qos_expose_flags);
724
725 static void __dev_pm_qos_hide_flags(struct device *dev)
726 {
727         if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->flags_req)
728                 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
729 }
730
731 /**
732  * dev_pm_qos_hide_flags - Hide PM QoS flags of a device from user space.
733  * @dev: Device whose PM QoS flags are to be hidden from user space.
734  */
735 void dev_pm_qos_hide_flags(struct device *dev)
736 {
737         pm_runtime_get_sync(dev);
738         mutex_lock(&dev_pm_qos_sysfs_mtx);
739
740         pm_qos_sysfs_remove_flags(dev);
741
742         mutex_lock(&dev_pm_qos_mtx);
743         __dev_pm_qos_hide_flags(dev);
744         mutex_unlock(&dev_pm_qos_mtx);
745
746         mutex_unlock(&dev_pm_qos_sysfs_mtx);
747         pm_runtime_put(dev);
748 }
749 EXPORT_SYMBOL_GPL(dev_pm_qos_hide_flags);
750
751 /**
752  * dev_pm_qos_update_flags - Update PM QoS flags request owned by user space.
753  * @dev: Device to update the PM QoS flags request for.
754  * @mask: Flags to set/clear.
755  * @set: Whether to set or clear the flags (true means set).
756  */
757 int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set)
758 {
759         s32 value;
760         int ret;
761
762         pm_runtime_get_sync(dev);
763         mutex_lock(&dev_pm_qos_mtx);
764
765         if (IS_ERR_OR_NULL(dev->power.qos) || !dev->power.qos->flags_req) {
766                 ret = -EINVAL;
767                 goto out;
768         }
769
770         value = dev_pm_qos_requested_flags(dev);
771         if (set)
772                 value |= mask;
773         else
774                 value &= ~mask;
775
776         ret = __dev_pm_qos_update_request(dev->power.qos->flags_req, value);
777
778  out:
779         mutex_unlock(&dev_pm_qos_mtx);
780         pm_runtime_put(dev);
781         return ret;
782 }
783
784 /**
785  * dev_pm_qos_get_user_latency_tolerance - Get user space latency tolerance.
786  * @dev: Device to obtain the user space latency tolerance for.
787  */
788 s32 dev_pm_qos_get_user_latency_tolerance(struct device *dev)
789 {
790         s32 ret;
791
792         mutex_lock(&dev_pm_qos_mtx);
793         ret = IS_ERR_OR_NULL(dev->power.qos)
794                 || !dev->power.qos->latency_tolerance_req ?
795                         PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT :
796                         dev->power.qos->latency_tolerance_req->data.pnode.prio;
797         mutex_unlock(&dev_pm_qos_mtx);
798         return ret;
799 }
800
801 /**
802  * dev_pm_qos_update_user_latency_tolerance - Update user space latency tolerance.
803  * @dev: Device to update the user space latency tolerance for.
804  * @val: New user space latency tolerance for @dev (negative values disable).
805  */
806 int dev_pm_qos_update_user_latency_tolerance(struct device *dev, s32 val)
807 {
808         int ret;
809
810         mutex_lock(&dev_pm_qos_mtx);
811
812         if (IS_ERR_OR_NULL(dev->power.qos)
813             || !dev->power.qos->latency_tolerance_req) {
814                 struct dev_pm_qos_request *req;
815
816                 if (val < 0) {
817                         if (val == PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT)
818                                 ret = 0;
819                         else
820                                 ret = -EINVAL;
821                         goto out;
822                 }
823                 req = kzalloc(sizeof(*req), GFP_KERNEL);
824                 if (!req) {
825                         ret = -ENOMEM;
826                         goto out;
827                 }
828                 ret = __dev_pm_qos_add_request(dev, req, DEV_PM_QOS_LATENCY_TOLERANCE, val);
829                 if (ret < 0) {
830                         kfree(req);
831                         goto out;
832                 }
833                 dev->power.qos->latency_tolerance_req = req;
834         } else {
835                 if (val < 0) {
836                         __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY_TOLERANCE);
837                         ret = 0;
838                 } else {
839                         ret = __dev_pm_qos_update_request(dev->power.qos->latency_tolerance_req, val);
840                 }
841         }
842
843  out:
844         mutex_unlock(&dev_pm_qos_mtx);
845         return ret;
846 }
847 EXPORT_SYMBOL_GPL(dev_pm_qos_update_user_latency_tolerance);
848
849 /**
850  * dev_pm_qos_expose_latency_tolerance - Expose latency tolerance to userspace
851  * @dev: Device whose latency tolerance to expose
852  */
853 int dev_pm_qos_expose_latency_tolerance(struct device *dev)
854 {
855         int ret;
856
857         if (!dev->power.set_latency_tolerance)
858                 return -EINVAL;
859
860         mutex_lock(&dev_pm_qos_sysfs_mtx);
861         ret = pm_qos_sysfs_add_latency_tolerance(dev);
862         mutex_unlock(&dev_pm_qos_sysfs_mtx);
863
864         return ret;
865 }
866 EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_tolerance);
867
868 /**
869  * dev_pm_qos_hide_latency_tolerance - Hide latency tolerance from userspace
870  * @dev: Device whose latency tolerance to hide
871  */
872 void dev_pm_qos_hide_latency_tolerance(struct device *dev)
873 {
874         mutex_lock(&dev_pm_qos_sysfs_mtx);
875         pm_qos_sysfs_remove_latency_tolerance(dev);
876         mutex_unlock(&dev_pm_qos_sysfs_mtx);
877
878         /* Remove the request from user space now */
879         pm_runtime_get_sync(dev);
880         dev_pm_qos_update_user_latency_tolerance(dev,
881                 PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT);
882         pm_runtime_put(dev);
883 }
884 EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_tolerance);