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