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