Greg Kroah-Hartman | 5de363b | 2019-04-02 15:32:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Devices PM QoS constraints management |
| 4 | * |
| 5 | * Copyright (C) 2011 Texas Instruments, Inc. |
| 6 | * |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 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. |
Viresh Kumar | d08d1b2 | 2017-02-22 13:58:52 +0530 | [diff] [blame] | 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. |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 19 | * |
| 20 | * Note about the per-device constraint data struct allocation: |
Aisheng Dong | 07a6c71 | 2019-03-06 13:25:25 +0000 | [diff] [blame] | 21 | * . The per-device constraints data struct ptr is stored into the device |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 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. |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 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> |
Paul Gortmaker | 1b6bc32 | 2011-05-27 07:12:15 -0400 | [diff] [blame] | 35 | #include <linux/export.h> |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 36 | #include <linux/pm_runtime.h> |
Rafael J. Wysocki | 37530f2 | 2013-03-04 14:22:57 +0100 | [diff] [blame] | 37 | #include <linux/err.h> |
Sahara | 96d9d0b | 2013-06-21 11:12:30 +0900 | [diff] [blame] | 38 | #include <trace/events/power.h> |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 39 | |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 40 | #include "power.h" |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 41 | |
| 42 | static DEFINE_MUTEX(dev_pm_qos_mtx); |
Rafael J. Wysocki | 0f70306 | 2013-04-02 01:25:24 +0200 | [diff] [blame] | 43 | static DEFINE_MUTEX(dev_pm_qos_sysfs_mtx); |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 44 | |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 45 | /** |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 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 | |
Krzysztof Kozlowski | f90b8ad | 2015-01-09 09:27:58 +0100 | [diff] [blame] | 58 | lockdep_assert_held(&dev->power.lock); |
| 59 | |
Rafael J. Wysocki | 37530f2 | 2013-03-04 14:22:57 +0100 | [diff] [blame] | 60 | if (IS_ERR_OR_NULL(qos)) |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 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 | } |
Lan Tianyu | 6802771 | 2013-01-23 04:26:28 +0800 | [diff] [blame] | 90 | EXPORT_SYMBOL_GPL(dev_pm_qos_flags); |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 91 | |
| 92 | /** |
Viresh Kumar | 8262331 | 2019-07-04 13:06:18 +0530 | [diff] [blame] | 93 | * __dev_pm_qos_resume_latency - Get resume latency constraint for a given device. |
Rafael J. Wysocki | 00dc9ad | 2011-12-01 00:01:31 +0100 | [diff] [blame] | 94 | * @dev: Device to get the PM QoS constraint value for. |
| 95 | * |
| 96 | * This routine must be called with dev->power.lock held. |
| 97 | */ |
Viresh Kumar | 8262331 | 2019-07-04 13:06:18 +0530 | [diff] [blame] | 98 | s32 __dev_pm_qos_resume_latency(struct device *dev) |
Rafael J. Wysocki | 00dc9ad | 2011-12-01 00:01:31 +0100 | [diff] [blame] | 99 | { |
Krzysztof Kozlowski | f90b8ad | 2015-01-09 09:27:58 +0100 | [diff] [blame] | 100 | lockdep_assert_held(&dev->power.lock); |
| 101 | |
Viresh Kumar | 8262331 | 2019-07-04 13:06:18 +0530 | [diff] [blame] | 102 | return dev_pm_qos_raw_resume_latency(dev); |
Rafael J. Wysocki | 00dc9ad | 2011-12-01 00:01:31 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * dev_pm_qos_read_value - Get PM QoS constraint for a given device (locked). |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 107 | * @dev: Device to get the PM QoS constraint value for. |
Viresh Kumar | 2a79ea5 | 2019-07-04 13:06:19 +0530 | [diff] [blame] | 108 | * @type: QoS request type. |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 109 | */ |
Viresh Kumar | 2a79ea5 | 2019-07-04 13:06:19 +0530 | [diff] [blame] | 110 | s32 dev_pm_qos_read_value(struct device *dev, enum dev_pm_qos_req_type type) |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 111 | { |
Viresh Kumar | 2a79ea5 | 2019-07-04 13:06:19 +0530 | [diff] [blame] | 112 | struct dev_pm_qos *qos = dev->power.qos; |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 113 | unsigned long flags; |
Rafael J. Wysocki | 00dc9ad | 2011-12-01 00:01:31 +0100 | [diff] [blame] | 114 | s32 ret; |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 115 | |
| 116 | spin_lock_irqsave(&dev->power.lock, flags); |
Viresh Kumar | 8262331 | 2019-07-04 13:06:18 +0530 | [diff] [blame] | 117 | |
Leonard Crestez | 36a8015 | 2019-11-26 17:17:13 +0200 | [diff] [blame] | 118 | switch (type) { |
| 119 | case DEV_PM_QOS_RESUME_LATENCY: |
Viresh Kumar | 2a79ea5 | 2019-07-04 13:06:19 +0530 | [diff] [blame] | 120 | ret = IS_ERR_OR_NULL(qos) ? PM_QOS_RESUME_LATENCY_NO_CONSTRAINT |
| 121 | : pm_qos_read_value(&qos->resume_latency); |
Leonard Crestez | 36a8015 | 2019-11-26 17:17:13 +0200 | [diff] [blame] | 122 | 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 Kumar | 2a79ea5 | 2019-07-04 13:06:19 +0530 | [diff] [blame] | 132 | WARN_ON(1); |
| 133 | ret = 0; |
| 134 | } |
Viresh Kumar | 8262331 | 2019-07-04 13:06:18 +0530 | [diff] [blame] | 135 | |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 136 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 137 | |
| 138 | return ret; |
| 139 | } |
| 140 | |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 141 | /** |
| 142 | * apply_constraint - Add/modify/remove device PM QoS request. |
| 143 | * @req: Constraint request to apply |
| 144 | * @action: Action to perform (add/update/remove). |
| 145 | * @value: Value to assign to the QoS request. |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame] | 146 | * |
| 147 | * Internal function to update the constraints list using the PM QoS core |
Viresh Kumar | d08d1b2 | 2017-02-22 13:58:52 +0530 | [diff] [blame] | 148 | * code and if needed call the per-device callbacks. |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame] | 149 | */ |
| 150 | static int apply_constraint(struct dev_pm_qos_request *req, |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 151 | enum pm_qos_req_action action, s32 value) |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame] | 152 | { |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 153 | struct dev_pm_qos *qos = req->dev->power.qos; |
| 154 | int ret; |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame] | 155 | |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 156 | switch(req->type) { |
Rafael J. Wysocki | b02f669 | 2014-02-11 00:35:23 +0100 | [diff] [blame] | 157 | case DEV_PM_QOS_RESUME_LATENCY: |
Rafael J. Wysocki | 0759e80 | 2017-11-07 11:33:49 +0100 | [diff] [blame] | 158 | if (WARN_ON(action != PM_QOS_REMOVE_REQ && value < 0)) |
| 159 | value = 0; |
| 160 | |
Rafael J. Wysocki | b02f669 | 2014-02-11 00:35:23 +0100 | [diff] [blame] | 161 | ret = pm_qos_update_target(&qos->resume_latency, |
| 162 | &req->data.pnode, action, value); |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 163 | break; |
Rafael J. Wysocki | 2d984ad | 2014-02-11 00:35:38 +0100 | [diff] [blame] | 164 | case DEV_PM_QOS_LATENCY_TOLERANCE: |
| 165 | ret = pm_qos_update_target(&qos->latency_tolerance, |
| 166 | &req->data.pnode, action, value); |
| 167 | if (ret) { |
| 168 | value = pm_qos_read_value(&qos->latency_tolerance); |
| 169 | req->dev->power.set_latency_tolerance(req->dev, value); |
| 170 | } |
| 171 | break; |
Leonard Crestez | 36a8015 | 2019-11-26 17:17:13 +0200 | [diff] [blame] | 172 | case DEV_PM_QOS_MIN_FREQUENCY: |
| 173 | case DEV_PM_QOS_MAX_FREQUENCY: |
| 174 | ret = freq_qos_apply(&req->data.freq, action, value); |
| 175 | break; |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 176 | case DEV_PM_QOS_FLAGS: |
| 177 | ret = pm_qos_update_flags(&qos->flags, &req->data.flr, |
| 178 | action, value); |
| 179 | break; |
| 180 | default: |
| 181 | ret = -EINVAL; |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | return ret; |
| 185 | } |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 186 | |
| 187 | /* |
| 188 | * dev_pm_qos_constraints_allocate |
| 189 | * @dev: device to allocate data for |
| 190 | * |
| 191 | * Called at the first call to add_request, for constraint data allocation |
| 192 | * Must be called with the dev_pm_qos_mtx mutex held |
| 193 | */ |
| 194 | static int dev_pm_qos_constraints_allocate(struct device *dev) |
| 195 | { |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 196 | struct dev_pm_qos *qos; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 197 | struct pm_qos_constraints *c; |
| 198 | struct blocking_notifier_head *n; |
| 199 | |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 200 | qos = kzalloc(sizeof(*qos), GFP_KERNEL); |
| 201 | if (!qos) |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 202 | return -ENOMEM; |
| 203 | |
Viresh Kumar | 208637b | 2019-07-04 13:06:20 +0530 | [diff] [blame] | 204 | n = kzalloc(3 * sizeof(*n), GFP_KERNEL); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 205 | if (!n) { |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 206 | kfree(qos); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 207 | return -ENOMEM; |
| 208 | } |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 209 | |
Rafael J. Wysocki | b02f669 | 2014-02-11 00:35:23 +0100 | [diff] [blame] | 210 | c = &qos->resume_latency; |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 211 | plist_head_init(&c->list); |
Rafael J. Wysocki | b02f669 | 2014-02-11 00:35:23 +0100 | [diff] [blame] | 212 | c->target_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE; |
| 213 | c->default_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE; |
Rafael J. Wysocki | 0759e80 | 2017-11-07 11:33:49 +0100 | [diff] [blame] | 214 | c->no_constraint_value = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT; |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 215 | c->type = PM_QOS_MIN; |
| 216 | c->notifiers = n; |
Viresh Kumar | 208637b | 2019-07-04 13:06:20 +0530 | [diff] [blame] | 217 | BLOCKING_INIT_NOTIFIER_HEAD(n); |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 218 | |
Rafael J. Wysocki | 2d984ad | 2014-02-11 00:35:38 +0100 | [diff] [blame] | 219 | c = &qos->latency_tolerance; |
| 220 | plist_head_init(&c->list); |
| 221 | c->target_value = PM_QOS_LATENCY_TOLERANCE_DEFAULT_VALUE; |
| 222 | c->default_value = PM_QOS_LATENCY_TOLERANCE_DEFAULT_VALUE; |
| 223 | c->no_constraint_value = PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT; |
| 224 | c->type = PM_QOS_MIN; |
| 225 | |
Leonard Crestez | 36a8015 | 2019-11-26 17:17:13 +0200 | [diff] [blame] | 226 | freq_constraints_init(&qos->freq); |
| 227 | |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 228 | INIT_LIST_HEAD(&qos->flags.list); |
| 229 | |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 230 | spin_lock_irq(&dev->power.lock); |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 231 | dev->power.qos = qos; |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 232 | spin_unlock_irq(&dev->power.lock); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
Rafael J. Wysocki | 37530f2 | 2013-03-04 14:22:57 +0100 | [diff] [blame] | 237 | static void __dev_pm_qos_hide_latency_limit(struct device *dev); |
| 238 | static void __dev_pm_qos_hide_flags(struct device *dev); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 239 | |
| 240 | /** |
| 241 | * dev_pm_qos_constraints_destroy |
| 242 | * @dev: target device |
| 243 | * |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 244 | * Called from the device PM subsystem on device removal under device_pm_lock(). |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 245 | */ |
| 246 | void dev_pm_qos_constraints_destroy(struct device *dev) |
| 247 | { |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 248 | struct dev_pm_qos *qos; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 249 | struct dev_pm_qos_request *req, *tmp; |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 250 | struct pm_qos_constraints *c; |
Rafael J. Wysocki | 35546bd | 2012-11-24 10:10:51 +0100 | [diff] [blame] | 251 | struct pm_qos_flags *f; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 252 | |
Rafael J. Wysocki | 0f70306 | 2013-04-02 01:25:24 +0200 | [diff] [blame] | 253 | mutex_lock(&dev_pm_qos_sysfs_mtx); |
Rafael J. Wysocki | 37530f2 | 2013-03-04 14:22:57 +0100 | [diff] [blame] | 254 | |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 255 | /* |
Rafael J. Wysocki | 35546bd | 2012-11-24 10:10:51 +0100 | [diff] [blame] | 256 | * If the device's PM QoS resume latency limit or PM QoS flags have been |
| 257 | * exposed to user space, they have to be hidden at this point. |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 258 | */ |
Rafael J. Wysocki | b02f669 | 2014-02-11 00:35:23 +0100 | [diff] [blame] | 259 | pm_qos_sysfs_remove_resume_latency(dev); |
Rafael J. Wysocki | 0f70306 | 2013-04-02 01:25:24 +0200 | [diff] [blame] | 260 | pm_qos_sysfs_remove_flags(dev); |
| 261 | |
| 262 | mutex_lock(&dev_pm_qos_mtx); |
| 263 | |
Rafael J. Wysocki | 37530f2 | 2013-03-04 14:22:57 +0100 | [diff] [blame] | 264 | __dev_pm_qos_hide_latency_limit(dev); |
| 265 | __dev_pm_qos_hide_flags(dev); |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 266 | |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 267 | qos = dev->power.qos; |
| 268 | if (!qos) |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 269 | goto out; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 270 | |
Rafael J. Wysocki | 35546bd | 2012-11-24 10:10:51 +0100 | [diff] [blame] | 271 | /* Flush the constraints lists for the device. */ |
Rafael J. Wysocki | b02f669 | 2014-02-11 00:35:23 +0100 | [diff] [blame] | 272 | c = &qos->resume_latency; |
Rafael J. Wysocki | 021c870 | 2012-10-23 01:09:00 +0200 | [diff] [blame] | 273 | plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) { |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 274 | /* |
| 275 | * Update constraints list and call the notification |
| 276 | * callbacks if needed |
| 277 | */ |
| 278 | apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE); |
| 279 | memset(req, 0, sizeof(*req)); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 280 | } |
Viresh Kumar | 208637b | 2019-07-04 13:06:20 +0530 | [diff] [blame] | 281 | |
Rafael J. Wysocki | 2d984ad | 2014-02-11 00:35:38 +0100 | [diff] [blame] | 282 | c = &qos->latency_tolerance; |
| 283 | plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) { |
| 284 | apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE); |
| 285 | memset(req, 0, sizeof(*req)); |
| 286 | } |
Viresh Kumar | 208637b | 2019-07-04 13:06:20 +0530 | [diff] [blame] | 287 | |
Leonard Crestez | 36a8015 | 2019-11-26 17:17:13 +0200 | [diff] [blame] | 288 | c = &qos->freq.min_freq; |
| 289 | plist_for_each_entry_safe(req, tmp, &c->list, data.freq.pnode) { |
| 290 | apply_constraint(req, PM_QOS_REMOVE_REQ, |
| 291 | PM_QOS_MIN_FREQUENCY_DEFAULT_VALUE); |
| 292 | memset(req, 0, sizeof(*req)); |
| 293 | } |
| 294 | |
| 295 | c = &qos->freq.max_freq; |
| 296 | plist_for_each_entry_safe(req, tmp, &c->list, data.freq.pnode) { |
| 297 | apply_constraint(req, PM_QOS_REMOVE_REQ, |
| 298 | PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE); |
| 299 | memset(req, 0, sizeof(*req)); |
| 300 | } |
| 301 | |
Rafael J. Wysocki | 35546bd | 2012-11-24 10:10:51 +0100 | [diff] [blame] | 302 | f = &qos->flags; |
| 303 | list_for_each_entry_safe(req, tmp, &f->list, data.flr.node) { |
| 304 | apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE); |
| 305 | memset(req, 0, sizeof(*req)); |
| 306 | } |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 307 | |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 308 | spin_lock_irq(&dev->power.lock); |
Rafael J. Wysocki | 37530f2 | 2013-03-04 14:22:57 +0100 | [diff] [blame] | 309 | dev->power.qos = ERR_PTR(-ENODEV); |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 310 | spin_unlock_irq(&dev->power.lock); |
| 311 | |
John Keeping | e84b4a8 | 2017-02-16 17:21:50 +0000 | [diff] [blame] | 312 | kfree(qos->resume_latency.notifiers); |
Lan,Tianyu | 9eaee2c | 2012-11-01 22:45:30 +0100 | [diff] [blame] | 313 | kfree(qos); |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 314 | |
| 315 | out: |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 316 | mutex_unlock(&dev_pm_qos_mtx); |
Rafael J. Wysocki | 0f70306 | 2013-04-02 01:25:24 +0200 | [diff] [blame] | 317 | |
| 318 | mutex_unlock(&dev_pm_qos_sysfs_mtx); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 319 | } |
| 320 | |
Jan H. Schönherr | 41ba8bd | 2017-09-05 23:14:29 +0200 | [diff] [blame] | 321 | static bool dev_pm_qos_invalid_req_type(struct device *dev, |
| 322 | enum dev_pm_qos_req_type type) |
Rafael J. Wysocki | 2d984ad | 2014-02-11 00:35:38 +0100 | [diff] [blame] | 323 | { |
Jan H. Schönherr | 41ba8bd | 2017-09-05 23:14:29 +0200 | [diff] [blame] | 324 | return type == DEV_PM_QOS_LATENCY_TOLERANCE && |
| 325 | !dev->power.set_latency_tolerance; |
Rafael J. Wysocki | 2d984ad | 2014-02-11 00:35:38 +0100 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | static int __dev_pm_qos_add_request(struct device *dev, |
| 329 | struct dev_pm_qos_request *req, |
| 330 | enum dev_pm_qos_req_type type, s32 value) |
| 331 | { |
| 332 | int ret = 0; |
| 333 | |
Jan H. Schönherr | 41ba8bd | 2017-09-05 23:14:29 +0200 | [diff] [blame] | 334 | if (!dev || !req || dev_pm_qos_invalid_req_type(dev, type)) |
Rafael J. Wysocki | 2d984ad | 2014-02-11 00:35:38 +0100 | [diff] [blame] | 335 | return -EINVAL; |
| 336 | |
| 337 | if (WARN(dev_pm_qos_request_active(req), |
| 338 | "%s() called for already added request\n", __func__)) |
| 339 | return -EINVAL; |
| 340 | |
| 341 | if (IS_ERR(dev->power.qos)) |
| 342 | ret = -ENODEV; |
| 343 | else if (!dev->power.qos) |
| 344 | ret = dev_pm_qos_constraints_allocate(dev); |
| 345 | |
| 346 | trace_dev_pm_qos_add_request(dev_name(dev), type, value); |
Leonard Crestez | 36a8015 | 2019-11-26 17:17:13 +0200 | [diff] [blame] | 347 | if (ret) |
| 348 | return ret; |
| 349 | |
| 350 | req->dev = dev; |
| 351 | req->type = type; |
| 352 | if (req->type == DEV_PM_QOS_MIN_FREQUENCY) |
| 353 | ret = freq_qos_add_request(&dev->power.qos->freq, |
| 354 | &req->data.freq, |
| 355 | FREQ_QOS_MIN, value); |
| 356 | else if (req->type == DEV_PM_QOS_MAX_FREQUENCY) |
| 357 | ret = freq_qos_add_request(&dev->power.qos->freq, |
| 358 | &req->data.freq, |
| 359 | FREQ_QOS_MAX, value); |
| 360 | else |
Rafael J. Wysocki | 2d984ad | 2014-02-11 00:35:38 +0100 | [diff] [blame] | 361 | ret = apply_constraint(req, PM_QOS_ADD_REQ, value); |
Leonard Crestez | 36a8015 | 2019-11-26 17:17:13 +0200 | [diff] [blame] | 362 | |
Rafael J. Wysocki | 2d984ad | 2014-02-11 00:35:38 +0100 | [diff] [blame] | 363 | return ret; |
| 364 | } |
| 365 | |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 366 | /** |
| 367 | * dev_pm_qos_add_request - inserts new qos request into the list |
| 368 | * @dev: target device for the constraint |
| 369 | * @req: pointer to a preallocated handle |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 370 | * @type: type of the request |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 371 | * @value: defines the qos request |
| 372 | * |
| 373 | * This function inserts a new entry in the device constraints list of |
| 374 | * requested qos performance characteristics. It recomputes the aggregate |
| 375 | * QoS expectations of parameters and initializes the dev_pm_qos_request |
| 376 | * handle. Caller needs to save this handle for later use in updates and |
| 377 | * removal. |
| 378 | * |
| 379 | * Returns 1 if the aggregated constraint value has changed, |
| 380 | * 0 if the aggregated constraint value has not changed, |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 381 | * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory |
| 382 | * to allocate for data structures, -ENODEV if the device has just been removed |
| 383 | * from the system. |
Rafael J. Wysocki | 436ede8 | 2012-11-02 13:10:09 +0100 | [diff] [blame] | 384 | * |
| 385 | * Callers should ensure that the target device is not RPM_SUSPENDED before |
| 386 | * using this function for requests of type DEV_PM_QOS_FLAGS. |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 387 | */ |
| 388 | int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req, |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 389 | enum dev_pm_qos_req_type type, s32 value) |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 390 | { |
Rafael J. Wysocki | 2d984ad | 2014-02-11 00:35:38 +0100 | [diff] [blame] | 391 | int ret; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 392 | |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 393 | mutex_lock(&dev_pm_qos_mtx); |
Rafael J. Wysocki | 2d984ad | 2014-02-11 00:35:38 +0100 | [diff] [blame] | 394 | ret = __dev_pm_qos_add_request(dev, req, type, value); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 395 | mutex_unlock(&dev_pm_qos_mtx); |
| 396 | return ret; |
| 397 | } |
| 398 | EXPORT_SYMBOL_GPL(dev_pm_qos_add_request); |
| 399 | |
| 400 | /** |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 401 | * __dev_pm_qos_update_request - Modify an existing device PM QoS request. |
| 402 | * @req : PM QoS request to modify. |
| 403 | * @new_value: New value to request. |
| 404 | */ |
| 405 | static int __dev_pm_qos_update_request(struct dev_pm_qos_request *req, |
| 406 | s32 new_value) |
| 407 | { |
| 408 | s32 curr_value; |
| 409 | int ret = 0; |
| 410 | |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 411 | if (!req) /*guard against callers passing in null */ |
| 412 | return -EINVAL; |
| 413 | |
| 414 | if (WARN(!dev_pm_qos_request_active(req), |
| 415 | "%s() called for unknown object\n", __func__)) |
| 416 | return -EINVAL; |
| 417 | |
Rafael J. Wysocki | 37530f2 | 2013-03-04 14:22:57 +0100 | [diff] [blame] | 418 | if (IS_ERR_OR_NULL(req->dev->power.qos)) |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 419 | return -ENODEV; |
| 420 | |
| 421 | switch(req->type) { |
Rafael J. Wysocki | b02f669 | 2014-02-11 00:35:23 +0100 | [diff] [blame] | 422 | case DEV_PM_QOS_RESUME_LATENCY: |
Rafael J. Wysocki | 2d984ad | 2014-02-11 00:35:38 +0100 | [diff] [blame] | 423 | case DEV_PM_QOS_LATENCY_TOLERANCE: |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 424 | curr_value = req->data.pnode.prio; |
| 425 | break; |
Leonard Crestez | 36a8015 | 2019-11-26 17:17:13 +0200 | [diff] [blame] | 426 | case DEV_PM_QOS_MIN_FREQUENCY: |
| 427 | case DEV_PM_QOS_MAX_FREQUENCY: |
| 428 | curr_value = req->data.freq.pnode.prio; |
| 429 | break; |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 430 | case DEV_PM_QOS_FLAGS: |
| 431 | curr_value = req->data.flr.flags; |
| 432 | break; |
| 433 | default: |
| 434 | return -EINVAL; |
| 435 | } |
| 436 | |
Sahara | 96d9d0b | 2013-06-21 11:12:30 +0900 | [diff] [blame] | 437 | trace_dev_pm_qos_update_request(dev_name(req->dev), req->type, |
| 438 | new_value); |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 439 | if (curr_value != new_value) |
| 440 | ret = apply_constraint(req, PM_QOS_UPDATE_REQ, new_value); |
| 441 | |
| 442 | return ret; |
| 443 | } |
| 444 | |
| 445 | /** |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 446 | * dev_pm_qos_update_request - modifies an existing qos request |
| 447 | * @req : handle to list element holding a dev_pm_qos request to use |
| 448 | * @new_value: defines the qos request |
| 449 | * |
| 450 | * Updates an existing dev PM qos request along with updating the |
| 451 | * target value. |
| 452 | * |
| 453 | * Attempts are made to make this code callable on hot code paths. |
| 454 | * |
| 455 | * Returns 1 if the aggregated constraint value has changed, |
| 456 | * 0 if the aggregated constraint value has not changed, |
| 457 | * -EINVAL in case of wrong parameters, -ENODEV if the device has been |
| 458 | * removed from the system |
Rafael J. Wysocki | 436ede8 | 2012-11-02 13:10:09 +0100 | [diff] [blame] | 459 | * |
| 460 | * Callers should ensure that the target device is not RPM_SUSPENDED before |
| 461 | * using this function for requests of type DEV_PM_QOS_FLAGS. |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 462 | */ |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 463 | int dev_pm_qos_update_request(struct dev_pm_qos_request *req, s32 new_value) |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 464 | { |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 465 | int ret; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 466 | |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 467 | mutex_lock(&dev_pm_qos_mtx); |
| 468 | ret = __dev_pm_qos_update_request(req, new_value); |
| 469 | mutex_unlock(&dev_pm_qos_mtx); |
| 470 | return ret; |
| 471 | } |
| 472 | EXPORT_SYMBOL_GPL(dev_pm_qos_update_request); |
| 473 | |
| 474 | static int __dev_pm_qos_remove_request(struct dev_pm_qos_request *req) |
| 475 | { |
Rafael J. Wysocki | 37530f2 | 2013-03-04 14:22:57 +0100 | [diff] [blame] | 476 | int ret; |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 477 | |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 478 | if (!req) /*guard against callers passing in null */ |
| 479 | return -EINVAL; |
| 480 | |
Guennadi Liakhovetski | af4c720 | 2011-11-10 00:44:18 +0100 | [diff] [blame] | 481 | if (WARN(!dev_pm_qos_request_active(req), |
| 482 | "%s() called for unknown object\n", __func__)) |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 483 | return -EINVAL; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 484 | |
Rafael J. Wysocki | 37530f2 | 2013-03-04 14:22:57 +0100 | [diff] [blame] | 485 | if (IS_ERR_OR_NULL(req->dev->power.qos)) |
| 486 | return -ENODEV; |
| 487 | |
Sahara | 96d9d0b | 2013-06-21 11:12:30 +0900 | [diff] [blame] | 488 | trace_dev_pm_qos_remove_request(dev_name(req->dev), req->type, |
| 489 | PM_QOS_DEFAULT_VALUE); |
Rafael J. Wysocki | 37530f2 | 2013-03-04 14:22:57 +0100 | [diff] [blame] | 490 | ret = apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE); |
| 491 | memset(req, 0, sizeof(*req)); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 492 | return ret; |
| 493 | } |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 494 | |
| 495 | /** |
| 496 | * dev_pm_qos_remove_request - modifies an existing qos request |
| 497 | * @req: handle to request list element |
| 498 | * |
| 499 | * Will remove pm qos request from the list of constraints and |
| 500 | * recompute the current target value. Call this on slow code paths. |
| 501 | * |
| 502 | * Returns 1 if the aggregated constraint value has changed, |
| 503 | * 0 if the aggregated constraint value has not changed, |
| 504 | * -EINVAL in case of wrong parameters, -ENODEV if the device has been |
| 505 | * removed from the system |
Rafael J. Wysocki | 436ede8 | 2012-11-02 13:10:09 +0100 | [diff] [blame] | 506 | * |
| 507 | * Callers should ensure that the target device is not RPM_SUSPENDED before |
| 508 | * using this function for requests of type DEV_PM_QOS_FLAGS. |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 509 | */ |
| 510 | int dev_pm_qos_remove_request(struct dev_pm_qos_request *req) |
| 511 | { |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 512 | int ret; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 513 | |
| 514 | mutex_lock(&dev_pm_qos_mtx); |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 515 | ret = __dev_pm_qos_remove_request(req); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 516 | mutex_unlock(&dev_pm_qos_mtx); |
| 517 | return ret; |
| 518 | } |
| 519 | EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request); |
| 520 | |
| 521 | /** |
| 522 | * dev_pm_qos_add_notifier - sets notification entry for changes to target value |
| 523 | * of per-device PM QoS constraints |
| 524 | * |
| 525 | * @dev: target device for the constraint |
| 526 | * @notifier: notifier block managed by caller. |
Viresh Kumar | 0b07ee9 | 2019-07-04 13:06:17 +0530 | [diff] [blame] | 527 | * @type: request type. |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 528 | * |
| 529 | * Will register the notifier into a notification chain that gets called |
| 530 | * upon changes to the target value for the device. |
Rafael J. Wysocki | 23e0fc5 | 2012-04-29 22:54:47 +0200 | [diff] [blame] | 531 | * |
| 532 | * If the device's constraints object doesn't exist when this routine is called, |
| 533 | * it will be created (or error code will be returned if that fails). |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 534 | */ |
Viresh Kumar | 0b07ee9 | 2019-07-04 13:06:17 +0530 | [diff] [blame] | 535 | int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier, |
| 536 | enum dev_pm_qos_req_type type) |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 537 | { |
Rafael J. Wysocki | 23e0fc5 | 2012-04-29 22:54:47 +0200 | [diff] [blame] | 538 | int ret = 0; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 539 | |
| 540 | mutex_lock(&dev_pm_qos_mtx); |
| 541 | |
Rafael J. Wysocki | 37530f2 | 2013-03-04 14:22:57 +0100 | [diff] [blame] | 542 | if (IS_ERR(dev->power.qos)) |
| 543 | ret = -ENODEV; |
| 544 | else if (!dev->power.qos) |
| 545 | ret = dev_pm_qos_constraints_allocate(dev); |
Rafael J. Wysocki | 23e0fc5 | 2012-04-29 22:54:47 +0200 | [diff] [blame] | 546 | |
Viresh Kumar | 208637b | 2019-07-04 13:06:20 +0530 | [diff] [blame] | 547 | if (ret) |
| 548 | goto unlock; |
| 549 | |
| 550 | switch (type) { |
| 551 | case DEV_PM_QOS_RESUME_LATENCY: |
Rafael J. Wysocki | b02f669 | 2014-02-11 00:35:23 +0100 | [diff] [blame] | 552 | ret = blocking_notifier_chain_register(dev->power.qos->resume_latency.notifiers, |
| 553 | notifier); |
Viresh Kumar | 208637b | 2019-07-04 13:06:20 +0530 | [diff] [blame] | 554 | break; |
Leonard Crestez | 36a8015 | 2019-11-26 17:17:13 +0200 | [diff] [blame] | 555 | case DEV_PM_QOS_MIN_FREQUENCY: |
| 556 | ret = freq_qos_add_notifier(&dev->power.qos->freq, |
| 557 | FREQ_QOS_MIN, notifier); |
| 558 | break; |
| 559 | case DEV_PM_QOS_MAX_FREQUENCY: |
| 560 | ret = freq_qos_add_notifier(&dev->power.qos->freq, |
| 561 | FREQ_QOS_MAX, notifier); |
| 562 | break; |
Viresh Kumar | 208637b | 2019-07-04 13:06:20 +0530 | [diff] [blame] | 563 | default: |
| 564 | WARN_ON(1); |
| 565 | ret = -EINVAL; |
| 566 | } |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 567 | |
Viresh Kumar | 208637b | 2019-07-04 13:06:20 +0530 | [diff] [blame] | 568 | unlock: |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 569 | mutex_unlock(&dev_pm_qos_mtx); |
Rafael J. Wysocki | 23e0fc5 | 2012-04-29 22:54:47 +0200 | [diff] [blame] | 570 | return ret; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 571 | } |
| 572 | EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier); |
| 573 | |
| 574 | /** |
| 575 | * dev_pm_qos_remove_notifier - deletes notification for changes to target value |
| 576 | * of per-device PM QoS constraints |
| 577 | * |
| 578 | * @dev: target device for the constraint |
| 579 | * @notifier: notifier block to be removed. |
Viresh Kumar | 0b07ee9 | 2019-07-04 13:06:17 +0530 | [diff] [blame] | 580 | * @type: request type. |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 581 | * |
| 582 | * Will remove the notifier from the notification chain that gets called |
| 583 | * upon changes to the target value. |
| 584 | */ |
| 585 | int dev_pm_qos_remove_notifier(struct device *dev, |
Viresh Kumar | 0b07ee9 | 2019-07-04 13:06:17 +0530 | [diff] [blame] | 586 | struct notifier_block *notifier, |
| 587 | enum dev_pm_qos_req_type type) |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 588 | { |
Viresh Kumar | 208637b | 2019-07-04 13:06:20 +0530 | [diff] [blame] | 589 | int ret = 0; |
Viresh Kumar | 0b07ee9 | 2019-07-04 13:06:17 +0530 | [diff] [blame] | 590 | |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 591 | mutex_lock(&dev_pm_qos_mtx); |
| 592 | |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 593 | /* Silently return if the constraints object is not present. */ |
Viresh Kumar | 208637b | 2019-07-04 13:06:20 +0530 | [diff] [blame] | 594 | if (IS_ERR_OR_NULL(dev->power.qos)) |
| 595 | goto unlock; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 596 | |
Viresh Kumar | 208637b | 2019-07-04 13:06:20 +0530 | [diff] [blame] | 597 | switch (type) { |
| 598 | case DEV_PM_QOS_RESUME_LATENCY: |
| 599 | ret = blocking_notifier_chain_unregister(dev->power.qos->resume_latency.notifiers, |
| 600 | notifier); |
| 601 | break; |
Leonard Crestez | 36a8015 | 2019-11-26 17:17:13 +0200 | [diff] [blame] | 602 | case DEV_PM_QOS_MIN_FREQUENCY: |
| 603 | ret = freq_qos_remove_notifier(&dev->power.qos->freq, |
| 604 | FREQ_QOS_MIN, notifier); |
| 605 | break; |
| 606 | case DEV_PM_QOS_MAX_FREQUENCY: |
| 607 | ret = freq_qos_remove_notifier(&dev->power.qos->freq, |
| 608 | FREQ_QOS_MAX, notifier); |
| 609 | break; |
Viresh Kumar | 208637b | 2019-07-04 13:06:20 +0530 | [diff] [blame] | 610 | default: |
| 611 | WARN_ON(1); |
| 612 | ret = -EINVAL; |
| 613 | } |
| 614 | |
| 615 | unlock: |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 616 | mutex_unlock(&dev_pm_qos_mtx); |
Viresh Kumar | 208637b | 2019-07-04 13:06:20 +0530 | [diff] [blame] | 617 | return ret; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 618 | } |
| 619 | EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier); |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame] | 620 | |
| 621 | /** |
Rafael J. Wysocki | 40a5f8b | 2011-12-23 01:23:52 +0100 | [diff] [blame] | 622 | * dev_pm_qos_add_ancestor_request - Add PM QoS request for device's ancestor. |
| 623 | * @dev: Device whose ancestor to add the request for. |
| 624 | * @req: Pointer to the preallocated handle. |
Rafael J. Wysocki | 71d821f | 2014-02-11 00:36:00 +0100 | [diff] [blame] | 625 | * @type: Type of the request. |
Rafael J. Wysocki | 40a5f8b | 2011-12-23 01:23:52 +0100 | [diff] [blame] | 626 | * @value: Constraint latency value. |
| 627 | */ |
| 628 | int dev_pm_qos_add_ancestor_request(struct device *dev, |
Rafael J. Wysocki | 71d821f | 2014-02-11 00:36:00 +0100 | [diff] [blame] | 629 | struct dev_pm_qos_request *req, |
| 630 | enum dev_pm_qos_req_type type, s32 value) |
Rafael J. Wysocki | 40a5f8b | 2011-12-23 01:23:52 +0100 | [diff] [blame] | 631 | { |
| 632 | struct device *ancestor = dev->parent; |
Rafael J. Wysocki | 4ce4780 | 2012-12-18 14:07:49 +0100 | [diff] [blame] | 633 | int ret = -ENODEV; |
Rafael J. Wysocki | 40a5f8b | 2011-12-23 01:23:52 +0100 | [diff] [blame] | 634 | |
Rafael J. Wysocki | 71d821f | 2014-02-11 00:36:00 +0100 | [diff] [blame] | 635 | switch (type) { |
| 636 | case DEV_PM_QOS_RESUME_LATENCY: |
| 637 | while (ancestor && !ancestor->power.ignore_children) |
| 638 | ancestor = ancestor->parent; |
Rafael J. Wysocki | 40a5f8b | 2011-12-23 01:23:52 +0100 | [diff] [blame] | 639 | |
Rafael J. Wysocki | 71d821f | 2014-02-11 00:36:00 +0100 | [diff] [blame] | 640 | break; |
| 641 | case DEV_PM_QOS_LATENCY_TOLERANCE: |
| 642 | while (ancestor && !ancestor->power.set_latency_tolerance) |
| 643 | ancestor = ancestor->parent; |
| 644 | |
| 645 | break; |
| 646 | default: |
| 647 | ancestor = NULL; |
| 648 | } |
Rafael J. Wysocki | 40a5f8b | 2011-12-23 01:23:52 +0100 | [diff] [blame] | 649 | if (ancestor) |
Rafael J. Wysocki | 71d821f | 2014-02-11 00:36:00 +0100 | [diff] [blame] | 650 | ret = dev_pm_qos_add_request(ancestor, req, type, value); |
Rafael J. Wysocki | 40a5f8b | 2011-12-23 01:23:52 +0100 | [diff] [blame] | 651 | |
Rafael J. Wysocki | 4ce4780 | 2012-12-18 14:07:49 +0100 | [diff] [blame] | 652 | if (ret < 0) |
Rafael J. Wysocki | 40a5f8b | 2011-12-23 01:23:52 +0100 | [diff] [blame] | 653 | req->dev = NULL; |
| 654 | |
Rafael J. Wysocki | 4ce4780 | 2012-12-18 14:07:49 +0100 | [diff] [blame] | 655 | return ret; |
Rafael J. Wysocki | 40a5f8b | 2011-12-23 01:23:52 +0100 | [diff] [blame] | 656 | } |
| 657 | EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request); |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 658 | |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 659 | static void __dev_pm_qos_drop_user_request(struct device *dev, |
| 660 | enum dev_pm_qos_req_type type) |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 661 | { |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 662 | struct dev_pm_qos_request *req = NULL; |
| 663 | |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 664 | switch(type) { |
Rafael J. Wysocki | b02f669 | 2014-02-11 00:35:23 +0100 | [diff] [blame] | 665 | case DEV_PM_QOS_RESUME_LATENCY: |
| 666 | req = dev->power.qos->resume_latency_req; |
| 667 | dev->power.qos->resume_latency_req = NULL; |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 668 | break; |
Rafael J. Wysocki | 2d984ad | 2014-02-11 00:35:38 +0100 | [diff] [blame] | 669 | case DEV_PM_QOS_LATENCY_TOLERANCE: |
| 670 | req = dev->power.qos->latency_tolerance_req; |
| 671 | dev->power.qos->latency_tolerance_req = NULL; |
| 672 | break; |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 673 | case DEV_PM_QOS_FLAGS: |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 674 | req = dev->power.qos->flags_req; |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 675 | dev->power.qos->flags_req = NULL; |
| 676 | break; |
Viresh Kumar | 208637b | 2019-07-04 13:06:20 +0530 | [diff] [blame] | 677 | default: |
| 678 | WARN_ON(1); |
| 679 | return; |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 680 | } |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 681 | __dev_pm_qos_remove_request(req); |
| 682 | kfree(req); |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 683 | } |
| 684 | |
Rafael J. Wysocki | 0f70306 | 2013-04-02 01:25:24 +0200 | [diff] [blame] | 685 | static void dev_pm_qos_drop_user_request(struct device *dev, |
| 686 | enum dev_pm_qos_req_type type) |
| 687 | { |
| 688 | mutex_lock(&dev_pm_qos_mtx); |
| 689 | __dev_pm_qos_drop_user_request(dev, type); |
| 690 | mutex_unlock(&dev_pm_qos_mtx); |
| 691 | } |
| 692 | |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 693 | /** |
| 694 | * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space. |
| 695 | * @dev: Device whose PM QoS latency limit is to be exposed to user space. |
| 696 | * @value: Initial value of the latency limit. |
| 697 | */ |
| 698 | int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value) |
| 699 | { |
| 700 | struct dev_pm_qos_request *req; |
| 701 | int ret; |
| 702 | |
| 703 | if (!device_is_registered(dev) || value < 0) |
| 704 | return -EINVAL; |
| 705 | |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 706 | req = kzalloc(sizeof(*req), GFP_KERNEL); |
| 707 | if (!req) |
| 708 | return -ENOMEM; |
| 709 | |
Rafael J. Wysocki | b02f669 | 2014-02-11 00:35:23 +0100 | [diff] [blame] | 710 | ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_RESUME_LATENCY, value); |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 711 | if (ret < 0) { |
| 712 | kfree(req); |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 713 | return ret; |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 714 | } |
| 715 | |
Rafael J. Wysocki | 0f70306 | 2013-04-02 01:25:24 +0200 | [diff] [blame] | 716 | mutex_lock(&dev_pm_qos_sysfs_mtx); |
| 717 | |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 718 | mutex_lock(&dev_pm_qos_mtx); |
| 719 | |
Rafael J. Wysocki | 37530f2 | 2013-03-04 14:22:57 +0100 | [diff] [blame] | 720 | if (IS_ERR_OR_NULL(dev->power.qos)) |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 721 | ret = -ENODEV; |
Rafael J. Wysocki | b02f669 | 2014-02-11 00:35:23 +0100 | [diff] [blame] | 722 | else if (dev->power.qos->resume_latency_req) |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 723 | ret = -EEXIST; |
| 724 | |
| 725 | if (ret < 0) { |
| 726 | __dev_pm_qos_remove_request(req); |
| 727 | kfree(req); |
Rafael J. Wysocki | 0f70306 | 2013-04-02 01:25:24 +0200 | [diff] [blame] | 728 | mutex_unlock(&dev_pm_qos_mtx); |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 729 | goto out; |
| 730 | } |
Rafael J. Wysocki | b02f669 | 2014-02-11 00:35:23 +0100 | [diff] [blame] | 731 | dev->power.qos->resume_latency_req = req; |
Rafael J. Wysocki | 0f70306 | 2013-04-02 01:25:24 +0200 | [diff] [blame] | 732 | |
| 733 | mutex_unlock(&dev_pm_qos_mtx); |
| 734 | |
Rafael J. Wysocki | b02f669 | 2014-02-11 00:35:23 +0100 | [diff] [blame] | 735 | ret = pm_qos_sysfs_add_resume_latency(dev); |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 736 | if (ret) |
Rafael J. Wysocki | b02f669 | 2014-02-11 00:35:23 +0100 | [diff] [blame] | 737 | dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_RESUME_LATENCY); |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 738 | |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 739 | out: |
Rafael J. Wysocki | 0f70306 | 2013-04-02 01:25:24 +0200 | [diff] [blame] | 740 | mutex_unlock(&dev_pm_qos_sysfs_mtx); |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 741 | return ret; |
| 742 | } |
| 743 | EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit); |
| 744 | |
Rafael J. Wysocki | 37530f2 | 2013-03-04 14:22:57 +0100 | [diff] [blame] | 745 | static void __dev_pm_qos_hide_latency_limit(struct device *dev) |
| 746 | { |
Rafael J. Wysocki | b02f669 | 2014-02-11 00:35:23 +0100 | [diff] [blame] | 747 | if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->resume_latency_req) |
| 748 | __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_RESUME_LATENCY); |
Rafael J. Wysocki | 37530f2 | 2013-03-04 14:22:57 +0100 | [diff] [blame] | 749 | } |
| 750 | |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 751 | /** |
| 752 | * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space. |
| 753 | * @dev: Device whose PM QoS latency limit is to be hidden from user space. |
| 754 | */ |
| 755 | void dev_pm_qos_hide_latency_limit(struct device *dev) |
| 756 | { |
Rafael J. Wysocki | 0f70306 | 2013-04-02 01:25:24 +0200 | [diff] [blame] | 757 | mutex_lock(&dev_pm_qos_sysfs_mtx); |
| 758 | |
Rafael J. Wysocki | b02f669 | 2014-02-11 00:35:23 +0100 | [diff] [blame] | 759 | pm_qos_sysfs_remove_resume_latency(dev); |
Rafael J. Wysocki | 0f70306 | 2013-04-02 01:25:24 +0200 | [diff] [blame] | 760 | |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 761 | mutex_lock(&dev_pm_qos_mtx); |
Rafael J. Wysocki | 37530f2 | 2013-03-04 14:22:57 +0100 | [diff] [blame] | 762 | __dev_pm_qos_hide_latency_limit(dev); |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 763 | mutex_unlock(&dev_pm_qos_mtx); |
Rafael J. Wysocki | 0f70306 | 2013-04-02 01:25:24 +0200 | [diff] [blame] | 764 | |
| 765 | mutex_unlock(&dev_pm_qos_sysfs_mtx); |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 766 | } |
| 767 | EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit); |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 768 | |
| 769 | /** |
| 770 | * dev_pm_qos_expose_flags - Expose PM QoS flags of a device to user space. |
| 771 | * @dev: Device whose PM QoS flags are to be exposed to user space. |
| 772 | * @val: Initial values of the flags. |
| 773 | */ |
| 774 | int dev_pm_qos_expose_flags(struct device *dev, s32 val) |
| 775 | { |
| 776 | struct dev_pm_qos_request *req; |
| 777 | int ret; |
| 778 | |
| 779 | if (!device_is_registered(dev)) |
| 780 | return -EINVAL; |
| 781 | |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 782 | req = kzalloc(sizeof(*req), GFP_KERNEL); |
| 783 | if (!req) |
| 784 | return -ENOMEM; |
| 785 | |
| 786 | ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_FLAGS, val); |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 787 | if (ret < 0) { |
| 788 | kfree(req); |
| 789 | return ret; |
| 790 | } |
| 791 | |
| 792 | pm_runtime_get_sync(dev); |
Rafael J. Wysocki | 0f70306 | 2013-04-02 01:25:24 +0200 | [diff] [blame] | 793 | mutex_lock(&dev_pm_qos_sysfs_mtx); |
| 794 | |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 795 | mutex_lock(&dev_pm_qos_mtx); |
| 796 | |
Rafael J. Wysocki | 37530f2 | 2013-03-04 14:22:57 +0100 | [diff] [blame] | 797 | if (IS_ERR_OR_NULL(dev->power.qos)) |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 798 | ret = -ENODEV; |
| 799 | else if (dev->power.qos->flags_req) |
| 800 | ret = -EEXIST; |
| 801 | |
| 802 | if (ret < 0) { |
| 803 | __dev_pm_qos_remove_request(req); |
| 804 | kfree(req); |
Rafael J. Wysocki | 0f70306 | 2013-04-02 01:25:24 +0200 | [diff] [blame] | 805 | mutex_unlock(&dev_pm_qos_mtx); |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 806 | goto out; |
| 807 | } |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 808 | dev->power.qos->flags_req = req; |
Rafael J. Wysocki | 0f70306 | 2013-04-02 01:25:24 +0200 | [diff] [blame] | 809 | |
| 810 | mutex_unlock(&dev_pm_qos_mtx); |
| 811 | |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 812 | ret = pm_qos_sysfs_add_flags(dev); |
| 813 | if (ret) |
Rafael J. Wysocki | 0f70306 | 2013-04-02 01:25:24 +0200 | [diff] [blame] | 814 | dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS); |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 815 | |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 816 | out: |
Rafael J. Wysocki | 0f70306 | 2013-04-02 01:25:24 +0200 | [diff] [blame] | 817 | mutex_unlock(&dev_pm_qos_sysfs_mtx); |
Lan Tianyu | 7e4d684 | 2012-11-08 11:14:08 +0800 | [diff] [blame] | 818 | pm_runtime_put(dev); |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 819 | return ret; |
| 820 | } |
| 821 | EXPORT_SYMBOL_GPL(dev_pm_qos_expose_flags); |
| 822 | |
Rafael J. Wysocki | 37530f2 | 2013-03-04 14:22:57 +0100 | [diff] [blame] | 823 | static void __dev_pm_qos_hide_flags(struct device *dev) |
| 824 | { |
Rafael J. Wysocki | 0f70306 | 2013-04-02 01:25:24 +0200 | [diff] [blame] | 825 | if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->flags_req) |
Rafael J. Wysocki | 37530f2 | 2013-03-04 14:22:57 +0100 | [diff] [blame] | 826 | __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS); |
Rafael J. Wysocki | 37530f2 | 2013-03-04 14:22:57 +0100 | [diff] [blame] | 827 | } |
| 828 | |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 829 | /** |
| 830 | * dev_pm_qos_hide_flags - Hide PM QoS flags of a device from user space. |
| 831 | * @dev: Device whose PM QoS flags are to be hidden from user space. |
| 832 | */ |
| 833 | void dev_pm_qos_hide_flags(struct device *dev) |
| 834 | { |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 835 | pm_runtime_get_sync(dev); |
Rafael J. Wysocki | 0f70306 | 2013-04-02 01:25:24 +0200 | [diff] [blame] | 836 | mutex_lock(&dev_pm_qos_sysfs_mtx); |
| 837 | |
| 838 | pm_qos_sysfs_remove_flags(dev); |
| 839 | |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 840 | mutex_lock(&dev_pm_qos_mtx); |
Rafael J. Wysocki | 37530f2 | 2013-03-04 14:22:57 +0100 | [diff] [blame] | 841 | __dev_pm_qos_hide_flags(dev); |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 842 | mutex_unlock(&dev_pm_qos_mtx); |
Rafael J. Wysocki | 0f70306 | 2013-04-02 01:25:24 +0200 | [diff] [blame] | 843 | |
| 844 | mutex_unlock(&dev_pm_qos_sysfs_mtx); |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 845 | pm_runtime_put(dev); |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 846 | } |
| 847 | EXPORT_SYMBOL_GPL(dev_pm_qos_hide_flags); |
| 848 | |
| 849 | /** |
| 850 | * dev_pm_qos_update_flags - Update PM QoS flags request owned by user space. |
| 851 | * @dev: Device to update the PM QoS flags request for. |
| 852 | * @mask: Flags to set/clear. |
| 853 | * @set: Whether to set or clear the flags (true means set). |
| 854 | */ |
| 855 | int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set) |
| 856 | { |
| 857 | s32 value; |
| 858 | int ret; |
| 859 | |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 860 | pm_runtime_get_sync(dev); |
| 861 | mutex_lock(&dev_pm_qos_mtx); |
| 862 | |
Rafael J. Wysocki | 37530f2 | 2013-03-04 14:22:57 +0100 | [diff] [blame] | 863 | if (IS_ERR_OR_NULL(dev->power.qos) || !dev->power.qos->flags_req) { |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 864 | ret = -EINVAL; |
| 865 | goto out; |
| 866 | } |
| 867 | |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 868 | value = dev_pm_qos_requested_flags(dev); |
| 869 | if (set) |
| 870 | value |= mask; |
| 871 | else |
| 872 | value &= ~mask; |
| 873 | |
| 874 | ret = __dev_pm_qos_update_request(dev->power.qos->flags_req, value); |
| 875 | |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame] | 876 | out: |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 877 | mutex_unlock(&dev_pm_qos_mtx); |
| 878 | pm_runtime_put(dev); |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 879 | return ret; |
| 880 | } |
Rafael J. Wysocki | 2d984ad | 2014-02-11 00:35:38 +0100 | [diff] [blame] | 881 | |
| 882 | /** |
| 883 | * dev_pm_qos_get_user_latency_tolerance - Get user space latency tolerance. |
| 884 | * @dev: Device to obtain the user space latency tolerance for. |
| 885 | */ |
| 886 | s32 dev_pm_qos_get_user_latency_tolerance(struct device *dev) |
| 887 | { |
| 888 | s32 ret; |
| 889 | |
| 890 | mutex_lock(&dev_pm_qos_mtx); |
| 891 | ret = IS_ERR_OR_NULL(dev->power.qos) |
| 892 | || !dev->power.qos->latency_tolerance_req ? |
| 893 | PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT : |
| 894 | dev->power.qos->latency_tolerance_req->data.pnode.prio; |
| 895 | mutex_unlock(&dev_pm_qos_mtx); |
| 896 | return ret; |
| 897 | } |
| 898 | |
| 899 | /** |
| 900 | * dev_pm_qos_update_user_latency_tolerance - Update user space latency tolerance. |
| 901 | * @dev: Device to update the user space latency tolerance for. |
| 902 | * @val: New user space latency tolerance for @dev (negative values disable). |
| 903 | */ |
| 904 | int dev_pm_qos_update_user_latency_tolerance(struct device *dev, s32 val) |
| 905 | { |
| 906 | int ret; |
| 907 | |
| 908 | mutex_lock(&dev_pm_qos_mtx); |
| 909 | |
| 910 | if (IS_ERR_OR_NULL(dev->power.qos) |
| 911 | || !dev->power.qos->latency_tolerance_req) { |
| 912 | struct dev_pm_qos_request *req; |
| 913 | |
| 914 | if (val < 0) { |
Andrew Lutomirski | 80a6f7c | 2016-11-29 17:11:51 -0800 | [diff] [blame] | 915 | if (val == PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT) |
| 916 | ret = 0; |
| 917 | else |
| 918 | ret = -EINVAL; |
Rafael J. Wysocki | 2d984ad | 2014-02-11 00:35:38 +0100 | [diff] [blame] | 919 | goto out; |
| 920 | } |
| 921 | req = kzalloc(sizeof(*req), GFP_KERNEL); |
| 922 | if (!req) { |
| 923 | ret = -ENOMEM; |
| 924 | goto out; |
| 925 | } |
| 926 | ret = __dev_pm_qos_add_request(dev, req, DEV_PM_QOS_LATENCY_TOLERANCE, val); |
| 927 | if (ret < 0) { |
| 928 | kfree(req); |
| 929 | goto out; |
| 930 | } |
| 931 | dev->power.qos->latency_tolerance_req = req; |
| 932 | } else { |
| 933 | if (val < 0) { |
| 934 | __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY_TOLERANCE); |
| 935 | ret = 0; |
| 936 | } else { |
| 937 | ret = __dev_pm_qos_update_request(dev->power.qos->latency_tolerance_req, val); |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | out: |
| 942 | mutex_unlock(&dev_pm_qos_mtx); |
| 943 | return ret; |
| 944 | } |
Andrew Lutomirski | 034e790 | 2016-11-29 17:11:52 -0800 | [diff] [blame] | 945 | EXPORT_SYMBOL_GPL(dev_pm_qos_update_user_latency_tolerance); |
Mika Westerberg | 13b2c4a | 2015-07-27 18:03:56 +0300 | [diff] [blame] | 946 | |
| 947 | /** |
| 948 | * dev_pm_qos_expose_latency_tolerance - Expose latency tolerance to userspace |
| 949 | * @dev: Device whose latency tolerance to expose |
| 950 | */ |
| 951 | int dev_pm_qos_expose_latency_tolerance(struct device *dev) |
| 952 | { |
| 953 | int ret; |
| 954 | |
| 955 | if (!dev->power.set_latency_tolerance) |
| 956 | return -EINVAL; |
| 957 | |
| 958 | mutex_lock(&dev_pm_qos_sysfs_mtx); |
| 959 | ret = pm_qos_sysfs_add_latency_tolerance(dev); |
| 960 | mutex_unlock(&dev_pm_qos_sysfs_mtx); |
| 961 | |
| 962 | return ret; |
| 963 | } |
| 964 | EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_tolerance); |
| 965 | |
| 966 | /** |
| 967 | * dev_pm_qos_hide_latency_tolerance - Hide latency tolerance from userspace |
| 968 | * @dev: Device whose latency tolerance to hide |
| 969 | */ |
| 970 | void dev_pm_qos_hide_latency_tolerance(struct device *dev) |
| 971 | { |
| 972 | mutex_lock(&dev_pm_qos_sysfs_mtx); |
| 973 | pm_qos_sysfs_remove_latency_tolerance(dev); |
| 974 | mutex_unlock(&dev_pm_qos_sysfs_mtx); |
| 975 | |
| 976 | /* Remove the request from user space now */ |
| 977 | pm_runtime_get_sync(dev); |
| 978 | dev_pm_qos_update_user_latency_tolerance(dev, |
| 979 | PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT); |
| 980 | pm_runtime_put(dev); |
| 981 | } |
| 982 | EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_tolerance); |