blob: 0489db3a1f60632a3e282b29133a10e80772808c [file] [log] [blame]
Greg Kroah-Hartman5de363b2019-04-02 15:32:01 +02001// SPDX-License-Identifier: GPL-2.0
Jean Pihet91ff4cb2011-08-25 15:35:41 +02002/*
3 * Devices PM QoS constraints management
4 *
5 * Copyright (C) 2011 Texas Instruments, Inc.
6 *
Jean Pihet91ff4cb2011-08-25 15:35:41 +02007 * 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.
Viresh Kumard08d1b22017-02-22 13:58:52 +053016 * 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.
Jean Pihet91ff4cb2011-08-25 15:35:41 +020019 *
20 * Note about the per-device constraint data struct allocation:
Aisheng Dong07a6c712019-03-06 13:25:25 +000021 * . The per-device constraints data struct ptr is stored into the device
Jean Pihet91ff4cb2011-08-25 15:35:41 +020022 * 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.
Jean Pihet91ff4cb2011-08-25 15:35:41 +020026 * . 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>
Paul Gortmaker1b6bc322011-05-27 07:12:15 -040035#include <linux/export.h>
Rafael J. Wysockie39473d2012-10-24 02:08:18 +020036#include <linux/pm_runtime.h>
Rafael J. Wysocki37530f22013-03-04 14:22:57 +010037#include <linux/err.h>
Sahara96d9d0b2013-06-21 11:12:30 +090038#include <trace/events/power.h>
Jean Pihet91ff4cb2011-08-25 15:35:41 +020039
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +010040#include "power.h"
Jean Pihet91ff4cb2011-08-25 15:35:41 +020041
42static DEFINE_MUTEX(dev_pm_qos_mtx);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +020043static DEFINE_MUTEX(dev_pm_qos_sysfs_mtx);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +020044
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +020045/**
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +020046 * __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 */
52enum 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
Krzysztof Kozlowskif90b8ad2015-01-09 09:27:58 +010058 lockdep_assert_held(&dev->power.lock);
59
Rafael J. Wysocki37530f22013-03-04 14:22:57 +010060 if (IS_ERR_OR_NULL(qos))
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +020061 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 */
79enum 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}
Lan Tianyu68027712013-01-23 04:26:28 +080090EXPORT_SYMBOL_GPL(dev_pm_qos_flags);
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +020091
92/**
Viresh Kumar82623312019-07-04 13:06:18 +053093 * __dev_pm_qos_resume_latency - Get resume latency constraint for a given device.
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +010094 * @dev: Device to get the PM QoS constraint value for.
95 *
96 * This routine must be called with dev->power.lock held.
97 */
Viresh Kumar82623312019-07-04 13:06:18 +053098s32 __dev_pm_qos_resume_latency(struct device *dev)
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +010099{
Krzysztof Kozlowskif90b8ad2015-01-09 09:27:58 +0100100 lockdep_assert_held(&dev->power.lock);
101
Viresh Kumar82623312019-07-04 13:06:18 +0530102 return dev_pm_qos_raw_resume_latency(dev);
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100103}
104
105/**
106 * dev_pm_qos_read_value - Get PM QoS constraint for a given device (locked).
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200107 * @dev: Device to get the PM QoS constraint value for.
Viresh Kumar2a79ea52019-07-04 13:06:19 +0530108 * @type: QoS request type.
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200109 */
Viresh Kumar2a79ea52019-07-04 13:06:19 +0530110s32 dev_pm_qos_read_value(struct device *dev, enum dev_pm_qos_req_type type)
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200111{
Viresh Kumar2a79ea52019-07-04 13:06:19 +0530112 struct dev_pm_qos *qos = dev->power.qos;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200113 unsigned long flags;
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100114 s32 ret;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200115
116 spin_lock_irqsave(&dev->power.lock, flags);
Viresh Kumar82623312019-07-04 13:06:18 +0530117
Leonard Crestez36a80152019-11-26 17:17:13 +0200118 switch (type) {
119 case DEV_PM_QOS_RESUME_LATENCY:
Viresh Kumar2a79ea52019-07-04 13:06:19 +0530120 ret = IS_ERR_OR_NULL(qos) ? PM_QOS_RESUME_LATENCY_NO_CONSTRAINT
121 : pm_qos_read_value(&qos->resume_latency);
Leonard Crestez36a80152019-11-26 17:17:13 +0200122 break;
123 case DEV_PM_QOS_MIN_FREQUENCY:
124 ret = IS_ERR_OR_NULL(qos) ? PM_QOS_MIN_FREQUENCY_DEFAULT_VALUE
125 : freq_qos_read_value(&qos->freq, FREQ_QOS_MIN);
126 break;
127 case DEV_PM_QOS_MAX_FREQUENCY:
128 ret = IS_ERR_OR_NULL(qos) ? PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE
129 : freq_qos_read_value(&qos->freq, FREQ_QOS_MAX);
130 break;
131 default:
Viresh Kumar2a79ea52019-07-04 13:06:19 +0530132 WARN_ON(1);
133 ret = 0;
134 }
Viresh Kumar82623312019-07-04 13:06:18 +0530135
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200136 spin_unlock_irqrestore(&dev->power.lock, flags);
137
138 return ret;
139}
Mark Salyzyned418392020-08-19 05:29:01 -0700140EXPORT_SYMBOL_GPL(dev_pm_qos_read_value);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200141
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200142/**
143 * apply_constraint - Add/modify/remove device PM QoS request.
144 * @req: Constraint request to apply
145 * @action: Action to perform (add/update/remove).
146 * @value: Value to assign to the QoS request.
Jean Pihetb66213c2011-08-25 15:35:47 +0200147 *
148 * Internal function to update the constraints list using the PM QoS core
Viresh Kumard08d1b22017-02-22 13:58:52 +0530149 * code and if needed call the per-device callbacks.
Jean Pihetb66213c2011-08-25 15:35:47 +0200150 */
151static int apply_constraint(struct dev_pm_qos_request *req,
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200152 enum pm_qos_req_action action, s32 value)
Jean Pihetb66213c2011-08-25 15:35:47 +0200153{
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200154 struct dev_pm_qos *qos = req->dev->power.qos;
155 int ret;
Jean Pihetb66213c2011-08-25 15:35:47 +0200156
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200157 switch(req->type) {
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100158 case DEV_PM_QOS_RESUME_LATENCY:
Rafael J. Wysocki0759e802017-11-07 11:33:49 +0100159 if (WARN_ON(action != PM_QOS_REMOVE_REQ && value < 0))
160 value = 0;
161
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100162 ret = pm_qos_update_target(&qos->resume_latency,
163 &req->data.pnode, action, value);
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200164 break;
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100165 case DEV_PM_QOS_LATENCY_TOLERANCE:
166 ret = pm_qos_update_target(&qos->latency_tolerance,
167 &req->data.pnode, action, value);
168 if (ret) {
169 value = pm_qos_read_value(&qos->latency_tolerance);
170 req->dev->power.set_latency_tolerance(req->dev, value);
171 }
172 break;
Leonard Crestez36a80152019-11-26 17:17:13 +0200173 case DEV_PM_QOS_MIN_FREQUENCY:
174 case DEV_PM_QOS_MAX_FREQUENCY:
175 ret = freq_qos_apply(&req->data.freq, action, value);
176 break;
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200177 case DEV_PM_QOS_FLAGS:
178 ret = pm_qos_update_flags(&qos->flags, &req->data.flr,
179 action, value);
180 break;
181 default:
182 ret = -EINVAL;
Jean Pihetb66213c2011-08-25 15:35:47 +0200183 }
184
185 return ret;
186}
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200187
188/*
189 * dev_pm_qos_constraints_allocate
190 * @dev: device to allocate data for
191 *
192 * Called at the first call to add_request, for constraint data allocation
193 * Must be called with the dev_pm_qos_mtx mutex held
194 */
195static int dev_pm_qos_constraints_allocate(struct device *dev)
196{
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200197 struct dev_pm_qos *qos;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200198 struct pm_qos_constraints *c;
199 struct blocking_notifier_head *n;
200
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200201 qos = kzalloc(sizeof(*qos), GFP_KERNEL);
202 if (!qos)
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200203 return -ENOMEM;
204
Viresh Kumar208637b2019-07-04 13:06:20 +0530205 n = kzalloc(3 * sizeof(*n), GFP_KERNEL);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200206 if (!n) {
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200207 kfree(qos);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200208 return -ENOMEM;
209 }
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200210
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100211 c = &qos->resume_latency;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200212 plist_head_init(&c->list);
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100213 c->target_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE;
214 c->default_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE;
Rafael J. Wysocki0759e802017-11-07 11:33:49 +0100215 c->no_constraint_value = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200216 c->type = PM_QOS_MIN;
217 c->notifiers = n;
Viresh Kumar208637b2019-07-04 13:06:20 +0530218 BLOCKING_INIT_NOTIFIER_HEAD(n);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200219
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100220 c = &qos->latency_tolerance;
221 plist_head_init(&c->list);
222 c->target_value = PM_QOS_LATENCY_TOLERANCE_DEFAULT_VALUE;
223 c->default_value = PM_QOS_LATENCY_TOLERANCE_DEFAULT_VALUE;
224 c->no_constraint_value = PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT;
225 c->type = PM_QOS_MIN;
226
Leonard Crestez36a80152019-11-26 17:17:13 +0200227 freq_constraints_init(&qos->freq);
228
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200229 INIT_LIST_HEAD(&qos->flags.list);
230
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200231 spin_lock_irq(&dev->power.lock);
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200232 dev->power.qos = qos;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200233 spin_unlock_irq(&dev->power.lock);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200234
235 return 0;
236}
237
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100238static void __dev_pm_qos_hide_latency_limit(struct device *dev);
239static void __dev_pm_qos_hide_flags(struct device *dev);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200240
241/**
242 * dev_pm_qos_constraints_destroy
243 * @dev: target device
244 *
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200245 * Called from the device PM subsystem on device removal under device_pm_lock().
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200246 */
247void dev_pm_qos_constraints_destroy(struct device *dev)
248{
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200249 struct dev_pm_qos *qos;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200250 struct dev_pm_qos_request *req, *tmp;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200251 struct pm_qos_constraints *c;
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100252 struct pm_qos_flags *f;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200253
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200254 mutex_lock(&dev_pm_qos_sysfs_mtx);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100255
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100256 /*
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100257 * If the device's PM QoS resume latency limit or PM QoS flags have been
258 * exposed to user space, they have to be hidden at this point.
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100259 */
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100260 pm_qos_sysfs_remove_resume_latency(dev);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200261 pm_qos_sysfs_remove_flags(dev);
262
263 mutex_lock(&dev_pm_qos_mtx);
264
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100265 __dev_pm_qos_hide_latency_limit(dev);
266 __dev_pm_qos_hide_flags(dev);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100267
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200268 qos = dev->power.qos;
269 if (!qos)
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200270 goto out;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200271
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100272 /* Flush the constraints lists for the device. */
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100273 c = &qos->resume_latency;
Rafael J. Wysocki021c8702012-10-23 01:09:00 +0200274 plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200275 /*
276 * Update constraints list and call the notification
277 * callbacks if needed
278 */
279 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
280 memset(req, 0, sizeof(*req));
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200281 }
Viresh Kumar208637b2019-07-04 13:06:20 +0530282
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100283 c = &qos->latency_tolerance;
284 plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
285 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
286 memset(req, 0, sizeof(*req));
287 }
Viresh Kumar208637b2019-07-04 13:06:20 +0530288
Leonard Crestez36a80152019-11-26 17:17:13 +0200289 c = &qos->freq.min_freq;
290 plist_for_each_entry_safe(req, tmp, &c->list, data.freq.pnode) {
291 apply_constraint(req, PM_QOS_REMOVE_REQ,
292 PM_QOS_MIN_FREQUENCY_DEFAULT_VALUE);
293 memset(req, 0, sizeof(*req));
294 }
295
296 c = &qos->freq.max_freq;
297 plist_for_each_entry_safe(req, tmp, &c->list, data.freq.pnode) {
298 apply_constraint(req, PM_QOS_REMOVE_REQ,
299 PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
300 memset(req, 0, sizeof(*req));
301 }
302
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100303 f = &qos->flags;
304 list_for_each_entry_safe(req, tmp, &f->list, data.flr.node) {
305 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
306 memset(req, 0, sizeof(*req));
307 }
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200308
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200309 spin_lock_irq(&dev->power.lock);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100310 dev->power.qos = ERR_PTR(-ENODEV);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200311 spin_unlock_irq(&dev->power.lock);
312
John Keepinge84b4a82017-02-16 17:21:50 +0000313 kfree(qos->resume_latency.notifiers);
Lan,Tianyu9eaee2c2012-11-01 22:45:30 +0100314 kfree(qos);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200315
316 out:
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200317 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200318
319 mutex_unlock(&dev_pm_qos_sysfs_mtx);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200320}
321
Jan H. Schönherr41ba8bd2017-09-05 23:14:29 +0200322static bool dev_pm_qos_invalid_req_type(struct device *dev,
323 enum dev_pm_qos_req_type type)
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100324{
Jan H. Schönherr41ba8bd2017-09-05 23:14:29 +0200325 return type == DEV_PM_QOS_LATENCY_TOLERANCE &&
326 !dev->power.set_latency_tolerance;
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100327}
328
329static int __dev_pm_qos_add_request(struct device *dev,
330 struct dev_pm_qos_request *req,
331 enum dev_pm_qos_req_type type, s32 value)
332{
333 int ret = 0;
334
Jan H. Schönherr41ba8bd2017-09-05 23:14:29 +0200335 if (!dev || !req || dev_pm_qos_invalid_req_type(dev, type))
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100336 return -EINVAL;
337
338 if (WARN(dev_pm_qos_request_active(req),
339 "%s() called for already added request\n", __func__))
340 return -EINVAL;
341
342 if (IS_ERR(dev->power.qos))
343 ret = -ENODEV;
344 else if (!dev->power.qos)
345 ret = dev_pm_qos_constraints_allocate(dev);
346
347 trace_dev_pm_qos_add_request(dev_name(dev), type, value);
Leonard Crestez36a80152019-11-26 17:17:13 +0200348 if (ret)
349 return ret;
350
351 req->dev = dev;
352 req->type = type;
353 if (req->type == DEV_PM_QOS_MIN_FREQUENCY)
354 ret = freq_qos_add_request(&dev->power.qos->freq,
355 &req->data.freq,
356 FREQ_QOS_MIN, value);
357 else if (req->type == DEV_PM_QOS_MAX_FREQUENCY)
358 ret = freq_qos_add_request(&dev->power.qos->freq,
359 &req->data.freq,
360 FREQ_QOS_MAX, value);
361 else
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100362 ret = apply_constraint(req, PM_QOS_ADD_REQ, value);
Leonard Crestez36a80152019-11-26 17:17:13 +0200363
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100364 return ret;
365}
366
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200367/**
368 * dev_pm_qos_add_request - inserts new qos request into the list
369 * @dev: target device for the constraint
370 * @req: pointer to a preallocated handle
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200371 * @type: type of the request
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200372 * @value: defines the qos request
373 *
374 * This function inserts a new entry in the device constraints list of
375 * requested qos performance characteristics. It recomputes the aggregate
376 * QoS expectations of parameters and initializes the dev_pm_qos_request
377 * handle. Caller needs to save this handle for later use in updates and
378 * removal.
379 *
380 * Returns 1 if the aggregated constraint value has changed,
381 * 0 if the aggregated constraint value has not changed,
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200382 * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory
383 * to allocate for data structures, -ENODEV if the device has just been removed
384 * from the system.
Rafael J. Wysocki436ede82012-11-02 13:10:09 +0100385 *
386 * Callers should ensure that the target device is not RPM_SUSPENDED before
387 * using this function for requests of type DEV_PM_QOS_FLAGS.
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200388 */
389int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200390 enum dev_pm_qos_req_type type, s32 value)
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200391{
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100392 int ret;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200393
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200394 mutex_lock(&dev_pm_qos_mtx);
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100395 ret = __dev_pm_qos_add_request(dev, req, type, value);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200396 mutex_unlock(&dev_pm_qos_mtx);
397 return ret;
398}
399EXPORT_SYMBOL_GPL(dev_pm_qos_add_request);
400
401/**
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200402 * __dev_pm_qos_update_request - Modify an existing device PM QoS request.
403 * @req : PM QoS request to modify.
404 * @new_value: New value to request.
405 */
406static int __dev_pm_qos_update_request(struct dev_pm_qos_request *req,
407 s32 new_value)
408{
409 s32 curr_value;
410 int ret = 0;
411
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100412 if (!req) /*guard against callers passing in null */
413 return -EINVAL;
414
415 if (WARN(!dev_pm_qos_request_active(req),
416 "%s() called for unknown object\n", __func__))
417 return -EINVAL;
418
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100419 if (IS_ERR_OR_NULL(req->dev->power.qos))
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200420 return -ENODEV;
421
422 switch(req->type) {
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100423 case DEV_PM_QOS_RESUME_LATENCY:
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100424 case DEV_PM_QOS_LATENCY_TOLERANCE:
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200425 curr_value = req->data.pnode.prio;
426 break;
Leonard Crestez36a80152019-11-26 17:17:13 +0200427 case DEV_PM_QOS_MIN_FREQUENCY:
428 case DEV_PM_QOS_MAX_FREQUENCY:
429 curr_value = req->data.freq.pnode.prio;
430 break;
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200431 case DEV_PM_QOS_FLAGS:
432 curr_value = req->data.flr.flags;
433 break;
434 default:
435 return -EINVAL;
436 }
437
Sahara96d9d0b2013-06-21 11:12:30 +0900438 trace_dev_pm_qos_update_request(dev_name(req->dev), req->type,
439 new_value);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200440 if (curr_value != new_value)
441 ret = apply_constraint(req, PM_QOS_UPDATE_REQ, new_value);
442
443 return ret;
444}
445
446/**
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200447 * dev_pm_qos_update_request - modifies an existing qos request
448 * @req : handle to list element holding a dev_pm_qos request to use
449 * @new_value: defines the qos request
450 *
451 * Updates an existing dev PM qos request along with updating the
452 * target value.
453 *
454 * Attempts are made to make this code callable on hot code paths.
455 *
456 * Returns 1 if the aggregated constraint value has changed,
457 * 0 if the aggregated constraint value has not changed,
458 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
459 * removed from the system
Rafael J. Wysocki436ede82012-11-02 13:10:09 +0100460 *
461 * Callers should ensure that the target device is not RPM_SUSPENDED before
462 * using this function for requests of type DEV_PM_QOS_FLAGS.
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200463 */
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200464int dev_pm_qos_update_request(struct dev_pm_qos_request *req, s32 new_value)
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200465{
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200466 int ret;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200467
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100468 mutex_lock(&dev_pm_qos_mtx);
469 ret = __dev_pm_qos_update_request(req, new_value);
470 mutex_unlock(&dev_pm_qos_mtx);
471 return ret;
472}
473EXPORT_SYMBOL_GPL(dev_pm_qos_update_request);
474
475static int __dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
476{
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100477 int ret;
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100478
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200479 if (!req) /*guard against callers passing in null */
480 return -EINVAL;
481
Guennadi Liakhovetskiaf4c7202011-11-10 00:44:18 +0100482 if (WARN(!dev_pm_qos_request_active(req),
483 "%s() called for unknown object\n", __func__))
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200484 return -EINVAL;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200485
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100486 if (IS_ERR_OR_NULL(req->dev->power.qos))
487 return -ENODEV;
488
Sahara96d9d0b2013-06-21 11:12:30 +0900489 trace_dev_pm_qos_remove_request(dev_name(req->dev), req->type,
490 PM_QOS_DEFAULT_VALUE);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100491 ret = apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
492 memset(req, 0, sizeof(*req));
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200493 return ret;
494}
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200495
496/**
497 * dev_pm_qos_remove_request - modifies an existing qos request
498 * @req: handle to request list element
499 *
500 * Will remove pm qos request from the list of constraints and
501 * recompute the current target value. Call this on slow code paths.
502 *
503 * Returns 1 if the aggregated constraint value has changed,
504 * 0 if the aggregated constraint value has not changed,
505 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
506 * removed from the system
Rafael J. Wysocki436ede82012-11-02 13:10:09 +0100507 *
508 * Callers should ensure that the target device is not RPM_SUSPENDED before
509 * using this function for requests of type DEV_PM_QOS_FLAGS.
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200510 */
511int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
512{
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100513 int ret;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200514
515 mutex_lock(&dev_pm_qos_mtx);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100516 ret = __dev_pm_qos_remove_request(req);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200517 mutex_unlock(&dev_pm_qos_mtx);
518 return ret;
519}
520EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request);
521
522/**
523 * dev_pm_qos_add_notifier - sets notification entry for changes to target value
524 * of per-device PM QoS constraints
525 *
526 * @dev: target device for the constraint
527 * @notifier: notifier block managed by caller.
Viresh Kumar0b07ee92019-07-04 13:06:17 +0530528 * @type: request type.
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200529 *
530 * Will register the notifier into a notification chain that gets called
531 * upon changes to the target value for the device.
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200532 *
533 * If the device's constraints object doesn't exist when this routine is called,
534 * it will be created (or error code will be returned if that fails).
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200535 */
Viresh Kumar0b07ee92019-07-04 13:06:17 +0530536int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier,
537 enum dev_pm_qos_req_type type)
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200538{
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200539 int ret = 0;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200540
541 mutex_lock(&dev_pm_qos_mtx);
542
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100543 if (IS_ERR(dev->power.qos))
544 ret = -ENODEV;
545 else if (!dev->power.qos)
546 ret = dev_pm_qos_constraints_allocate(dev);
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200547
Viresh Kumar208637b2019-07-04 13:06:20 +0530548 if (ret)
549 goto unlock;
550
551 switch (type) {
552 case DEV_PM_QOS_RESUME_LATENCY:
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100553 ret = blocking_notifier_chain_register(dev->power.qos->resume_latency.notifiers,
554 notifier);
Viresh Kumar208637b2019-07-04 13:06:20 +0530555 break;
Leonard Crestez36a80152019-11-26 17:17:13 +0200556 case DEV_PM_QOS_MIN_FREQUENCY:
557 ret = freq_qos_add_notifier(&dev->power.qos->freq,
558 FREQ_QOS_MIN, notifier);
559 break;
560 case DEV_PM_QOS_MAX_FREQUENCY:
561 ret = freq_qos_add_notifier(&dev->power.qos->freq,
562 FREQ_QOS_MAX, notifier);
563 break;
Viresh Kumar208637b2019-07-04 13:06:20 +0530564 default:
565 WARN_ON(1);
566 ret = -EINVAL;
567 }
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200568
Viresh Kumar208637b2019-07-04 13:06:20 +0530569unlock:
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200570 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200571 return ret;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200572}
573EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier);
574
575/**
576 * dev_pm_qos_remove_notifier - deletes notification for changes to target value
577 * of per-device PM QoS constraints
578 *
579 * @dev: target device for the constraint
580 * @notifier: notifier block to be removed.
Viresh Kumar0b07ee92019-07-04 13:06:17 +0530581 * @type: request type.
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200582 *
583 * Will remove the notifier from the notification chain that gets called
584 * upon changes to the target value.
585 */
586int dev_pm_qos_remove_notifier(struct device *dev,
Viresh Kumar0b07ee92019-07-04 13:06:17 +0530587 struct notifier_block *notifier,
588 enum dev_pm_qos_req_type type)
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200589{
Viresh Kumar208637b2019-07-04 13:06:20 +0530590 int ret = 0;
Viresh Kumar0b07ee92019-07-04 13:06:17 +0530591
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200592 mutex_lock(&dev_pm_qos_mtx);
593
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200594 /* Silently return if the constraints object is not present. */
Viresh Kumar208637b2019-07-04 13:06:20 +0530595 if (IS_ERR_OR_NULL(dev->power.qos))
596 goto unlock;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200597
Viresh Kumar208637b2019-07-04 13:06:20 +0530598 switch (type) {
599 case DEV_PM_QOS_RESUME_LATENCY:
600 ret = blocking_notifier_chain_unregister(dev->power.qos->resume_latency.notifiers,
601 notifier);
602 break;
Leonard Crestez36a80152019-11-26 17:17:13 +0200603 case DEV_PM_QOS_MIN_FREQUENCY:
604 ret = freq_qos_remove_notifier(&dev->power.qos->freq,
605 FREQ_QOS_MIN, notifier);
606 break;
607 case DEV_PM_QOS_MAX_FREQUENCY:
608 ret = freq_qos_remove_notifier(&dev->power.qos->freq,
609 FREQ_QOS_MAX, notifier);
610 break;
Viresh Kumar208637b2019-07-04 13:06:20 +0530611 default:
612 WARN_ON(1);
613 ret = -EINVAL;
614 }
615
616unlock:
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200617 mutex_unlock(&dev_pm_qos_mtx);
Viresh Kumar208637b2019-07-04 13:06:20 +0530618 return ret;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200619}
620EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier);
Jean Pihetb66213c2011-08-25 15:35:47 +0200621
622/**
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100623 * dev_pm_qos_add_ancestor_request - Add PM QoS request for device's ancestor.
624 * @dev: Device whose ancestor to add the request for.
625 * @req: Pointer to the preallocated handle.
Rafael J. Wysocki71d821f2014-02-11 00:36:00 +0100626 * @type: Type of the request.
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100627 * @value: Constraint latency value.
628 */
629int dev_pm_qos_add_ancestor_request(struct device *dev,
Rafael J. Wysocki71d821f2014-02-11 00:36:00 +0100630 struct dev_pm_qos_request *req,
631 enum dev_pm_qos_req_type type, s32 value)
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100632{
633 struct device *ancestor = dev->parent;
Rafael J. Wysocki4ce47802012-12-18 14:07:49 +0100634 int ret = -ENODEV;
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100635
Rafael J. Wysocki71d821f2014-02-11 00:36:00 +0100636 switch (type) {
637 case DEV_PM_QOS_RESUME_LATENCY:
638 while (ancestor && !ancestor->power.ignore_children)
639 ancestor = ancestor->parent;
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100640
Rafael J. Wysocki71d821f2014-02-11 00:36:00 +0100641 break;
642 case DEV_PM_QOS_LATENCY_TOLERANCE:
643 while (ancestor && !ancestor->power.set_latency_tolerance)
644 ancestor = ancestor->parent;
645
646 break;
647 default:
648 ancestor = NULL;
649 }
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100650 if (ancestor)
Rafael J. Wysocki71d821f2014-02-11 00:36:00 +0100651 ret = dev_pm_qos_add_request(ancestor, req, type, value);
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100652
Rafael J. Wysocki4ce47802012-12-18 14:07:49 +0100653 if (ret < 0)
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100654 req->dev = NULL;
655
Rafael J. Wysocki4ce47802012-12-18 14:07:49 +0100656 return ret;
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100657}
658EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100659
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200660static void __dev_pm_qos_drop_user_request(struct device *dev,
661 enum dev_pm_qos_req_type type)
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100662{
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100663 struct dev_pm_qos_request *req = NULL;
664
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200665 switch(type) {
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100666 case DEV_PM_QOS_RESUME_LATENCY:
667 req = dev->power.qos->resume_latency_req;
668 dev->power.qos->resume_latency_req = NULL;
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200669 break;
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100670 case DEV_PM_QOS_LATENCY_TOLERANCE:
671 req = dev->power.qos->latency_tolerance_req;
672 dev->power.qos->latency_tolerance_req = NULL;
673 break;
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200674 case DEV_PM_QOS_FLAGS:
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100675 req = dev->power.qos->flags_req;
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200676 dev->power.qos->flags_req = NULL;
677 break;
Viresh Kumar208637b2019-07-04 13:06:20 +0530678 default:
679 WARN_ON(1);
680 return;
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200681 }
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100682 __dev_pm_qos_remove_request(req);
683 kfree(req);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100684}
685
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200686static void dev_pm_qos_drop_user_request(struct device *dev,
687 enum dev_pm_qos_req_type type)
688{
689 mutex_lock(&dev_pm_qos_mtx);
690 __dev_pm_qos_drop_user_request(dev, type);
691 mutex_unlock(&dev_pm_qos_mtx);
692}
693
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100694/**
695 * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space.
696 * @dev: Device whose PM QoS latency limit is to be exposed to user space.
697 * @value: Initial value of the latency limit.
698 */
699int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value)
700{
701 struct dev_pm_qos_request *req;
702 int ret;
703
704 if (!device_is_registered(dev) || value < 0)
705 return -EINVAL;
706
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100707 req = kzalloc(sizeof(*req), GFP_KERNEL);
708 if (!req)
709 return -ENOMEM;
710
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100711 ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_RESUME_LATENCY, value);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100712 if (ret < 0) {
713 kfree(req);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100714 return ret;
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100715 }
716
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200717 mutex_lock(&dev_pm_qos_sysfs_mtx);
718
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100719 mutex_lock(&dev_pm_qos_mtx);
720
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100721 if (IS_ERR_OR_NULL(dev->power.qos))
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100722 ret = -ENODEV;
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100723 else if (dev->power.qos->resume_latency_req)
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100724 ret = -EEXIST;
725
726 if (ret < 0) {
727 __dev_pm_qos_remove_request(req);
728 kfree(req);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200729 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100730 goto out;
731 }
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100732 dev->power.qos->resume_latency_req = req;
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200733
734 mutex_unlock(&dev_pm_qos_mtx);
735
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100736 ret = pm_qos_sysfs_add_resume_latency(dev);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100737 if (ret)
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100738 dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_RESUME_LATENCY);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100739
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100740 out:
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200741 mutex_unlock(&dev_pm_qos_sysfs_mtx);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100742 return ret;
743}
744EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit);
745
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100746static void __dev_pm_qos_hide_latency_limit(struct device *dev)
747{
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100748 if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->resume_latency_req)
749 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_RESUME_LATENCY);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100750}
751
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100752/**
753 * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space.
754 * @dev: Device whose PM QoS latency limit is to be hidden from user space.
755 */
756void dev_pm_qos_hide_latency_limit(struct device *dev)
757{
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200758 mutex_lock(&dev_pm_qos_sysfs_mtx);
759
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100760 pm_qos_sysfs_remove_resume_latency(dev);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200761
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100762 mutex_lock(&dev_pm_qos_mtx);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100763 __dev_pm_qos_hide_latency_limit(dev);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100764 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200765
766 mutex_unlock(&dev_pm_qos_sysfs_mtx);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100767}
768EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200769
770/**
771 * dev_pm_qos_expose_flags - Expose PM QoS flags of a device to user space.
772 * @dev: Device whose PM QoS flags are to be exposed to user space.
773 * @val: Initial values of the flags.
774 */
775int dev_pm_qos_expose_flags(struct device *dev, s32 val)
776{
777 struct dev_pm_qos_request *req;
778 int ret;
779
780 if (!device_is_registered(dev))
781 return -EINVAL;
782
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200783 req = kzalloc(sizeof(*req), GFP_KERNEL);
784 if (!req)
785 return -ENOMEM;
786
787 ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_FLAGS, val);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100788 if (ret < 0) {
789 kfree(req);
790 return ret;
791 }
792
793 pm_runtime_get_sync(dev);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200794 mutex_lock(&dev_pm_qos_sysfs_mtx);
795
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100796 mutex_lock(&dev_pm_qos_mtx);
797
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100798 if (IS_ERR_OR_NULL(dev->power.qos))
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100799 ret = -ENODEV;
800 else if (dev->power.qos->flags_req)
801 ret = -EEXIST;
802
803 if (ret < 0) {
804 __dev_pm_qos_remove_request(req);
805 kfree(req);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200806 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100807 goto out;
808 }
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200809 dev->power.qos->flags_req = req;
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200810
811 mutex_unlock(&dev_pm_qos_mtx);
812
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200813 ret = pm_qos_sysfs_add_flags(dev);
814 if (ret)
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200815 dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200816
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100817 out:
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200818 mutex_unlock(&dev_pm_qos_sysfs_mtx);
Lan Tianyu7e4d6842012-11-08 11:14:08 +0800819 pm_runtime_put(dev);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200820 return ret;
821}
822EXPORT_SYMBOL_GPL(dev_pm_qos_expose_flags);
823
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100824static void __dev_pm_qos_hide_flags(struct device *dev)
825{
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200826 if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->flags_req)
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100827 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100828}
829
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200830/**
831 * dev_pm_qos_hide_flags - Hide PM QoS flags of a device from user space.
832 * @dev: Device whose PM QoS flags are to be hidden from user space.
833 */
834void dev_pm_qos_hide_flags(struct device *dev)
835{
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100836 pm_runtime_get_sync(dev);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200837 mutex_lock(&dev_pm_qos_sysfs_mtx);
838
839 pm_qos_sysfs_remove_flags(dev);
840
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100841 mutex_lock(&dev_pm_qos_mtx);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100842 __dev_pm_qos_hide_flags(dev);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100843 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200844
845 mutex_unlock(&dev_pm_qos_sysfs_mtx);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100846 pm_runtime_put(dev);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200847}
848EXPORT_SYMBOL_GPL(dev_pm_qos_hide_flags);
849
850/**
851 * dev_pm_qos_update_flags - Update PM QoS flags request owned by user space.
852 * @dev: Device to update the PM QoS flags request for.
853 * @mask: Flags to set/clear.
854 * @set: Whether to set or clear the flags (true means set).
855 */
856int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set)
857{
858 s32 value;
859 int ret;
860
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200861 pm_runtime_get_sync(dev);
862 mutex_lock(&dev_pm_qos_mtx);
863
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100864 if (IS_ERR_OR_NULL(dev->power.qos) || !dev->power.qos->flags_req) {
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100865 ret = -EINVAL;
866 goto out;
867 }
868
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200869 value = dev_pm_qos_requested_flags(dev);
870 if (set)
871 value |= mask;
872 else
873 value &= ~mask;
874
875 ret = __dev_pm_qos_update_request(dev->power.qos->flags_req, value);
876
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100877 out:
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200878 mutex_unlock(&dev_pm_qos_mtx);
879 pm_runtime_put(dev);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200880 return ret;
881}
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100882
883/**
884 * dev_pm_qos_get_user_latency_tolerance - Get user space latency tolerance.
885 * @dev: Device to obtain the user space latency tolerance for.
886 */
887s32 dev_pm_qos_get_user_latency_tolerance(struct device *dev)
888{
889 s32 ret;
890
891 mutex_lock(&dev_pm_qos_mtx);
892 ret = IS_ERR_OR_NULL(dev->power.qos)
893 || !dev->power.qos->latency_tolerance_req ?
894 PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT :
895 dev->power.qos->latency_tolerance_req->data.pnode.prio;
896 mutex_unlock(&dev_pm_qos_mtx);
897 return ret;
898}
899
900/**
901 * dev_pm_qos_update_user_latency_tolerance - Update user space latency tolerance.
902 * @dev: Device to update the user space latency tolerance for.
903 * @val: New user space latency tolerance for @dev (negative values disable).
904 */
905int dev_pm_qos_update_user_latency_tolerance(struct device *dev, s32 val)
906{
907 int ret;
908
909 mutex_lock(&dev_pm_qos_mtx);
910
911 if (IS_ERR_OR_NULL(dev->power.qos)
912 || !dev->power.qos->latency_tolerance_req) {
913 struct dev_pm_qos_request *req;
914
915 if (val < 0) {
Andrew Lutomirski80a6f7c2016-11-29 17:11:51 -0800916 if (val == PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT)
917 ret = 0;
918 else
919 ret = -EINVAL;
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100920 goto out;
921 }
922 req = kzalloc(sizeof(*req), GFP_KERNEL);
923 if (!req) {
924 ret = -ENOMEM;
925 goto out;
926 }
927 ret = __dev_pm_qos_add_request(dev, req, DEV_PM_QOS_LATENCY_TOLERANCE, val);
928 if (ret < 0) {
929 kfree(req);
930 goto out;
931 }
932 dev->power.qos->latency_tolerance_req = req;
933 } else {
934 if (val < 0) {
935 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY_TOLERANCE);
936 ret = 0;
937 } else {
938 ret = __dev_pm_qos_update_request(dev->power.qos->latency_tolerance_req, val);
939 }
940 }
941
942 out:
943 mutex_unlock(&dev_pm_qos_mtx);
944 return ret;
945}
Andrew Lutomirski034e7902016-11-29 17:11:52 -0800946EXPORT_SYMBOL_GPL(dev_pm_qos_update_user_latency_tolerance);
Mika Westerberg13b2c4a2015-07-27 18:03:56 +0300947
948/**
949 * dev_pm_qos_expose_latency_tolerance - Expose latency tolerance to userspace
950 * @dev: Device whose latency tolerance to expose
951 */
952int dev_pm_qos_expose_latency_tolerance(struct device *dev)
953{
954 int ret;
955
956 if (!dev->power.set_latency_tolerance)
957 return -EINVAL;
958
959 mutex_lock(&dev_pm_qos_sysfs_mtx);
960 ret = pm_qos_sysfs_add_latency_tolerance(dev);
961 mutex_unlock(&dev_pm_qos_sysfs_mtx);
962
963 return ret;
964}
965EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_tolerance);
966
967/**
968 * dev_pm_qos_hide_latency_tolerance - Hide latency tolerance from userspace
969 * @dev: Device whose latency tolerance to hide
970 */
971void dev_pm_qos_hide_latency_tolerance(struct device *dev)
972{
973 mutex_lock(&dev_pm_qos_sysfs_mtx);
974 pm_qos_sysfs_remove_latency_tolerance(dev);
975 mutex_unlock(&dev_pm_qos_sysfs_mtx);
976
977 /* Remove the request from user space now */
978 pm_runtime_get_sync(dev);
979 dev_pm_qos_update_user_latency_tolerance(dev,
980 PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT);
981 pm_runtime_put(dev);
982}
983EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_tolerance);