blob: 33e3febaba53f9636405e21a0eeea48411f2de7b [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Mark Grossd82b3512008-02-04 22:30:08 -08002/*
3 * This module exposes the interface to kernel space for specifying
4 * QoS dependencies. It provides infrastructure for registration of:
5 *
Mark Grossed771342010-05-06 01:59:26 +02006 * Dependents on a QoS value : register requests
Mark Grossd82b3512008-02-04 22:30:08 -08007 * Watchers of QoS value : get notified when target QoS value changes
8 *
9 * This QoS design is best effort based. Dependents register their QoS needs.
10 * Watchers register to keep track of the current QoS needs of the system.
11 *
12 * There are 3 basic classes of QoS parameter: latency, timeout, throughput
13 * each have defined units:
14 * latency: usec
15 * timeout: usec <-- currently not used.
16 * throughput: kbs (kilo byte / sec)
17 *
Mark Grossed771342010-05-06 01:59:26 +020018 * There are lists of pm_qos_objects each one wrapping requests, notifiers
Mark Grossd82b3512008-02-04 22:30:08 -080019 *
Mark Grossed771342010-05-06 01:59:26 +020020 * User mode requests on a QOS parameter register themselves to the
Mark Grossd82b3512008-02-04 22:30:08 -080021 * subsystem by opening the device node /dev/... and writing there request to
22 * the node. As long as the process holds a file handle open to the node the
23 * client continues to be accounted for. Upon file release the usermode
Mark Grossed771342010-05-06 01:59:26 +020024 * request is removed and a new qos target is computed. This way when the
25 * request that the application has is cleaned up when closes the file
Mark Grossd82b3512008-02-04 22:30:08 -080026 * pointer or exits the pm_qos_object will get an opportunity to clean up.
27 *
Richard Hughesbf1db692008-08-05 13:01:35 -070028 * Mark Gross <mgross@linux.intel.com>
Mark Grossd82b3512008-02-04 22:30:08 -080029 */
30
Mark Grossed771342010-05-06 01:59:26 +020031/*#define DEBUG*/
32
Jean Pihete8db0be2011-08-25 15:35:03 +020033#include <linux/pm_qos.h>
Mark Grossd82b3512008-02-04 22:30:08 -080034#include <linux/sched.h>
35#include <linux/spinlock.h>
36#include <linux/slab.h>
37#include <linux/time.h>
38#include <linux/fs.h>
39#include <linux/device.h>
40#include <linux/miscdevice.h>
41#include <linux/string.h>
42#include <linux/platform_device.h>
43#include <linux/init.h>
Rafael J. Wysocki0775a602011-05-27 00:05:23 +020044#include <linux/kernel.h>
Nishanth Menonf5f4eda2014-12-05 11:19:08 -060045#include <linux/debugfs.h>
46#include <linux/seq_file.h>
Mark Grossd82b3512008-02-04 22:30:08 -080047
48#include <linux/uaccess.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040049#include <linux/export.h>
Sahara247e9ee2013-06-21 11:12:28 +090050#include <trace/events/power.h>
Mark Grossd82b3512008-02-04 22:30:08 -080051
52/*
Jean Pihetcc749982011-08-25 15:35:12 +020053 * locking rule: all changes to constraints or notifiers lists
Mark Grossd82b3512008-02-04 22:30:08 -080054 * or pm_qos_object list and pm_qos_objects need to happen with pm_qos_lock
55 * held, taken with _irqsave. One lock to rule them all
56 */
Mark Grossd82b3512008-02-04 22:30:08 -080057struct pm_qos_object {
Jean Pihet4e1779b2011-08-25 15:35:27 +020058 struct pm_qos_constraints *constraints;
Mark Grossd82b3512008-02-04 22:30:08 -080059 struct miscdevice pm_qos_power_miscdev;
60 char *name;
Mark Grossd82b3512008-02-04 22:30:08 -080061};
62
James Bottomley5f279842010-07-19 02:00:18 +020063static DEFINE_SPINLOCK(pm_qos_lock);
64
Mark Grossd82b3512008-02-04 22:30:08 -080065static struct pm_qos_object null_pm_qos;
Jean Pihet4e1779b2011-08-25 15:35:27 +020066
Mark Grossd82b3512008-02-04 22:30:08 -080067static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
Jean Pihet4e1779b2011-08-25 15:35:27 +020068static struct pm_qos_constraints cpu_dma_constraints = {
69 .list = PLIST_HEAD_INIT(cpu_dma_constraints.list),
Tim Chen333c5ae2011-02-11 12:49:04 -080070 .target_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE,
71 .default_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE,
Rafael J. Wysocki327adae2014-02-11 00:35:29 +010072 .no_constraint_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE,
James Bottomley5f279842010-07-19 02:00:18 +020073 .type = PM_QOS_MIN,
Jean Pihet4e1779b2011-08-25 15:35:27 +020074 .notifiers = &cpu_dma_lat_notifier,
75};
76static struct pm_qos_object cpu_dma_pm_qos = {
77 .constraints = &cpu_dma_constraints,
Dominik Brodowskia6f05b92011-11-06 21:54:12 +010078 .name = "cpu_dma_latency",
Mark Grossd82b3512008-02-04 22:30:08 -080079};
80
81static BLOCKING_NOTIFIER_HEAD(network_lat_notifier);
Jean Pihet4e1779b2011-08-25 15:35:27 +020082static struct pm_qos_constraints network_lat_constraints = {
83 .list = PLIST_HEAD_INIT(network_lat_constraints.list),
Tim Chen333c5ae2011-02-11 12:49:04 -080084 .target_value = PM_QOS_NETWORK_LAT_DEFAULT_VALUE,
85 .default_value = PM_QOS_NETWORK_LAT_DEFAULT_VALUE,
Rafael J. Wysocki327adae2014-02-11 00:35:29 +010086 .no_constraint_value = PM_QOS_NETWORK_LAT_DEFAULT_VALUE,
Jean Pihet4e1779b2011-08-25 15:35:27 +020087 .type = PM_QOS_MIN,
88 .notifiers = &network_lat_notifier,
89};
90static struct pm_qos_object network_lat_pm_qos = {
91 .constraints = &network_lat_constraints,
92 .name = "network_latency",
Mark Grossd82b3512008-02-04 22:30:08 -080093};
94
95
96static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier);
Jean Pihet4e1779b2011-08-25 15:35:27 +020097static struct pm_qos_constraints network_tput_constraints = {
98 .list = PLIST_HEAD_INIT(network_tput_constraints.list),
Tim Chen333c5ae2011-02-11 12:49:04 -080099 .target_value = PM_QOS_NETWORK_THROUGHPUT_DEFAULT_VALUE,
100 .default_value = PM_QOS_NETWORK_THROUGHPUT_DEFAULT_VALUE,
Rafael J. Wysocki327adae2014-02-11 00:35:29 +0100101 .no_constraint_value = PM_QOS_NETWORK_THROUGHPUT_DEFAULT_VALUE,
James Bottomley5f279842010-07-19 02:00:18 +0200102 .type = PM_QOS_MAX,
Jean Pihet4e1779b2011-08-25 15:35:27 +0200103 .notifiers = &network_throughput_notifier,
104};
105static struct pm_qos_object network_throughput_pm_qos = {
106 .constraints = &network_tput_constraints,
107 .name = "network_throughput",
Mark Grossd82b3512008-02-04 22:30:08 -0800108};
109
110
Tomeu Vizoso7990da72014-09-03 17:49:32 +0200111static BLOCKING_NOTIFIER_HEAD(memory_bandwidth_notifier);
112static struct pm_qos_constraints memory_bw_constraints = {
113 .list = PLIST_HEAD_INIT(memory_bw_constraints.list),
114 .target_value = PM_QOS_MEMORY_BANDWIDTH_DEFAULT_VALUE,
115 .default_value = PM_QOS_MEMORY_BANDWIDTH_DEFAULT_VALUE,
116 .no_constraint_value = PM_QOS_MEMORY_BANDWIDTH_DEFAULT_VALUE,
117 .type = PM_QOS_SUM,
118 .notifiers = &memory_bandwidth_notifier,
119};
120static struct pm_qos_object memory_bandwidth_pm_qos = {
121 .constraints = &memory_bw_constraints,
122 .name = "memory_bandwidth",
123};
124
125
Mark Grossd82b3512008-02-04 22:30:08 -0800126static struct pm_qos_object *pm_qos_array[] = {
127 &null_pm_qos,
128 &cpu_dma_pm_qos,
129 &network_lat_pm_qos,
Tomeu Vizoso7990da72014-09-03 17:49:32 +0200130 &network_throughput_pm_qos,
131 &memory_bandwidth_pm_qos,
Mark Grossd82b3512008-02-04 22:30:08 -0800132};
133
Mark Grossd82b3512008-02-04 22:30:08 -0800134static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
135 size_t count, loff_t *f_pos);
Thomas Renningerf9b9e802011-02-28 22:06:34 +0100136static ssize_t pm_qos_power_read(struct file *filp, char __user *buf,
137 size_t count, loff_t *f_pos);
Mark Grossd82b3512008-02-04 22:30:08 -0800138static int pm_qos_power_open(struct inode *inode, struct file *filp);
139static int pm_qos_power_release(struct inode *inode, struct file *filp);
140
141static const struct file_operations pm_qos_power_fops = {
142 .write = pm_qos_power_write,
Thomas Renningerf9b9e802011-02-28 22:06:34 +0100143 .read = pm_qos_power_read,
Mark Grossd82b3512008-02-04 22:30:08 -0800144 .open = pm_qos_power_open,
145 .release = pm_qos_power_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200146 .llseek = noop_llseek,
Mark Grossd82b3512008-02-04 22:30:08 -0800147};
148
James Bottomley5f279842010-07-19 02:00:18 +0200149/* unlocked internal variant */
Jean Pihetabe98ec2011-08-25 15:35:34 +0200150static inline int pm_qos_get_value(struct pm_qos_constraints *c)
Mark Grossd82b3512008-02-04 22:30:08 -0800151{
Tomeu Vizoso7990da72014-09-03 17:49:32 +0200152 struct plist_node *node;
153 int total_value = 0;
154
Jean Pihetabe98ec2011-08-25 15:35:34 +0200155 if (plist_head_empty(&c->list))
Rafael J. Wysocki327adae2014-02-11 00:35:29 +0100156 return c->no_constraint_value;
James Bottomley5f279842010-07-19 02:00:18 +0200157
Jean Pihetabe98ec2011-08-25 15:35:34 +0200158 switch (c->type) {
James Bottomley5f279842010-07-19 02:00:18 +0200159 case PM_QOS_MIN:
Jean Pihetabe98ec2011-08-25 15:35:34 +0200160 return plist_first(&c->list)->prio;
James Bottomley5f279842010-07-19 02:00:18 +0200161
162 case PM_QOS_MAX:
Jean Pihetabe98ec2011-08-25 15:35:34 +0200163 return plist_last(&c->list)->prio;
James Bottomley5f279842010-07-19 02:00:18 +0200164
Tomeu Vizoso7990da72014-09-03 17:49:32 +0200165 case PM_QOS_SUM:
166 plist_for_each(node, &c->list)
167 total_value += node->prio;
168
169 return total_value;
170
James Bottomley5f279842010-07-19 02:00:18 +0200171 default:
172 /* runtime check for not using enum */
173 BUG();
Luis Gonzalez Fernandezc6a57bf2012-09-07 21:35:21 +0200174 return PM_QOS_DEFAULT_VALUE;
James Bottomley5f279842010-07-19 02:00:18 +0200175 }
Mark Grossd82b3512008-02-04 22:30:08 -0800176}
177
Jean Pihetb66213c2011-08-25 15:35:47 +0200178s32 pm_qos_read_value(struct pm_qos_constraints *c)
Tim Chen333c5ae2011-02-11 12:49:04 -0800179{
Jean Pihetabe98ec2011-08-25 15:35:34 +0200180 return c->target_value;
Tim Chen333c5ae2011-02-11 12:49:04 -0800181}
182
Jean Pihetabe98ec2011-08-25 15:35:34 +0200183static inline void pm_qos_set_value(struct pm_qos_constraints *c, s32 value)
Tim Chen333c5ae2011-02-11 12:49:04 -0800184{
Jean Pihetabe98ec2011-08-25 15:35:34 +0200185 c->target_value = value;
Tim Chen333c5ae2011-02-11 12:49:04 -0800186}
187
Yangtao Li96c69352018-11-06 09:38:06 -0500188static int pm_qos_debug_show(struct seq_file *s, void *unused)
Nishanth Menonf5f4eda2014-12-05 11:19:08 -0600189{
190 struct pm_qos_object *qos = (struct pm_qos_object *)s->private;
191 struct pm_qos_constraints *c;
192 struct pm_qos_request *req;
193 char *type;
194 unsigned long flags;
195 int tot_reqs = 0;
196 int active_reqs = 0;
197
198 if (IS_ERR_OR_NULL(qos)) {
199 pr_err("%s: bad qos param!\n", __func__);
200 return -EINVAL;
201 }
202 c = qos->constraints;
203 if (IS_ERR_OR_NULL(c)) {
204 pr_err("%s: Bad constraints on qos?\n", __func__);
205 return -EINVAL;
206 }
207
208 /* Lock to ensure we have a snapshot */
209 spin_lock_irqsave(&pm_qos_lock, flags);
210 if (plist_head_empty(&c->list)) {
211 seq_puts(s, "Empty!\n");
212 goto out;
213 }
214
215 switch (c->type) {
216 case PM_QOS_MIN:
217 type = "Minimum";
218 break;
219 case PM_QOS_MAX:
220 type = "Maximum";
221 break;
222 case PM_QOS_SUM:
223 type = "Sum";
224 break;
225 default:
226 type = "Unknown";
227 }
228
229 plist_for_each_entry(req, &c->list, node) {
230 char *state = "Default";
231
232 if ((req->node).prio != c->default_value) {
233 active_reqs++;
234 state = "Active";
235 }
236 tot_reqs++;
237 seq_printf(s, "%d: %d: %s\n", tot_reqs,
238 (req->node).prio, state);
239 }
240
241 seq_printf(s, "Type=%s, Value=%d, Requests: active=%d / total=%d\n",
242 type, pm_qos_get_value(c), active_reqs, tot_reqs);
243
244out:
245 spin_unlock_irqrestore(&pm_qos_lock, flags);
246 return 0;
247}
248
Yangtao Li96c69352018-11-06 09:38:06 -0500249DEFINE_SHOW_ATTRIBUTE(pm_qos_debug);
Nishanth Menonf5f4eda2014-12-05 11:19:08 -0600250
Jean Pihetabe98ec2011-08-25 15:35:34 +0200251/**
252 * pm_qos_update_target - manages the constraints list and calls the notifiers
253 * if needed
254 * @c: constraints data struct
255 * @node: request to add to the list, to update or to remove
256 * @action: action to take on the constraints list
257 * @value: value of the request to add or update
258 *
259 * This function returns 1 if the aggregated constraint value has changed, 0
260 * otherwise.
261 */
262int pm_qos_update_target(struct pm_qos_constraints *c, struct plist_node *node,
263 enum pm_qos_req_action action, int value)
Mark Grossd82b3512008-02-04 22:30:08 -0800264{
Mark Grossd82b3512008-02-04 22:30:08 -0800265 unsigned long flags;
Jean Pihetabe98ec2011-08-25 15:35:34 +0200266 int prev_value, curr_value, new_value;
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100267 int ret;
Mark Grossd82b3512008-02-04 22:30:08 -0800268
269 spin_lock_irqsave(&pm_qos_lock, flags);
Jean Pihetabe98ec2011-08-25 15:35:34 +0200270 prev_value = pm_qos_get_value(c);
271 if (value == PM_QOS_DEFAULT_VALUE)
272 new_value = c->default_value;
273 else
274 new_value = value;
275
276 switch (action) {
277 case PM_QOS_REMOVE_REQ:
278 plist_del(node, &c->list);
279 break;
280 case PM_QOS_UPDATE_REQ:
James Bottomley5f279842010-07-19 02:00:18 +0200281 /*
282 * to change the list, we atomically remove, reinit
283 * with new value and add, then see if the extremal
284 * changed
285 */
Jean Pihetabe98ec2011-08-25 15:35:34 +0200286 plist_del(node, &c->list);
Gustavo A. R. Silvafe43e2c2018-03-30 16:18:44 -0500287 /* fall through */
Jean Pihetabe98ec2011-08-25 15:35:34 +0200288 case PM_QOS_ADD_REQ:
289 plist_node_init(node, new_value);
290 plist_add(node, &c->list);
291 break;
292 default:
293 /* no action */
294 ;
Mark Grossd82b3512008-02-04 22:30:08 -0800295 }
Jean Pihetabe98ec2011-08-25 15:35:34 +0200296
297 curr_value = pm_qos_get_value(c);
298 pm_qos_set_value(c, curr_value);
299
Mark Grossd82b3512008-02-04 22:30:08 -0800300 spin_unlock_irqrestore(&pm_qos_lock, flags);
301
Sahara247e9ee2013-06-21 11:12:28 +0900302 trace_pm_qos_update_target(action, prev_value, curr_value);
Jean Pihetabe98ec2011-08-25 15:35:34 +0200303 if (prev_value != curr_value) {
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100304 ret = 1;
305 if (c->notifiers)
306 blocking_notifier_call_chain(c->notifiers,
307 (unsigned long)curr_value,
308 NULL);
Jean Pihetabe98ec2011-08-25 15:35:34 +0200309 } else {
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100310 ret = 0;
Jean Pihetabe98ec2011-08-25 15:35:34 +0200311 }
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100312 return ret;
Mark Grossd82b3512008-02-04 22:30:08 -0800313}
314
Mark Grossd82b3512008-02-04 22:30:08 -0800315/**
Rafael J. Wysocki5efbe422012-10-23 01:07:46 +0200316 * pm_qos_flags_remove_req - Remove device PM QoS flags request.
317 * @pqf: Device PM QoS flags set to remove the request from.
318 * @req: Request to remove from the set.
319 */
320static void pm_qos_flags_remove_req(struct pm_qos_flags *pqf,
321 struct pm_qos_flags_request *req)
322{
323 s32 val = 0;
324
325 list_del(&req->node);
326 list_for_each_entry(req, &pqf->list, node)
327 val |= req->flags;
328
329 pqf->effective_flags = val;
330}
331
332/**
333 * pm_qos_update_flags - Update a set of PM QoS flags.
334 * @pqf: Set of flags to update.
335 * @req: Request to add to the set, to modify, or to remove from the set.
336 * @action: Action to take on the set.
337 * @val: Value of the request to add or modify.
338 *
339 * Update the given set of PM QoS flags and call notifiers if the aggregate
340 * value has changed. Returns 1 if the aggregate constraint value has changed,
341 * 0 otherwise.
342 */
343bool pm_qos_update_flags(struct pm_qos_flags *pqf,
344 struct pm_qos_flags_request *req,
345 enum pm_qos_req_action action, s32 val)
346{
347 unsigned long irqflags;
348 s32 prev_value, curr_value;
349
350 spin_lock_irqsave(&pm_qos_lock, irqflags);
351
352 prev_value = list_empty(&pqf->list) ? 0 : pqf->effective_flags;
353
354 switch (action) {
355 case PM_QOS_REMOVE_REQ:
356 pm_qos_flags_remove_req(pqf, req);
357 break;
358 case PM_QOS_UPDATE_REQ:
359 pm_qos_flags_remove_req(pqf, req);
Gustavo A. R. Silvafe43e2c2018-03-30 16:18:44 -0500360 /* fall through */
Rafael J. Wysocki5efbe422012-10-23 01:07:46 +0200361 case PM_QOS_ADD_REQ:
362 req->flags = val;
363 INIT_LIST_HEAD(&req->node);
364 list_add_tail(&req->node, &pqf->list);
365 pqf->effective_flags |= val;
366 break;
367 default:
368 /* no action */
369 ;
370 }
371
372 curr_value = list_empty(&pqf->list) ? 0 : pqf->effective_flags;
373
374 spin_unlock_irqrestore(&pm_qos_lock, irqflags);
375
Sahara247e9ee2013-06-21 11:12:28 +0900376 trace_pm_qos_update_flags(action, prev_value, curr_value);
Rafael J. Wysocki5efbe422012-10-23 01:07:46 +0200377 return prev_value != curr_value;
378}
379
380/**
Mark Grossed771342010-05-06 01:59:26 +0200381 * pm_qos_request - returns current system wide qos expectation
Mark Grossd82b3512008-02-04 22:30:08 -0800382 * @pm_qos_class: identification of which qos value is requested
383 *
Tim Chen333c5ae2011-02-11 12:49:04 -0800384 * This function returns the current target value.
Mark Grossd82b3512008-02-04 22:30:08 -0800385 */
Mark Grossed771342010-05-06 01:59:26 +0200386int pm_qos_request(int pm_qos_class)
Mark Grossd82b3512008-02-04 22:30:08 -0800387{
Jean Pihetabe98ec2011-08-25 15:35:34 +0200388 return pm_qos_read_value(pm_qos_array[pm_qos_class]->constraints);
Mark Grossd82b3512008-02-04 22:30:08 -0800389}
Mark Grossed771342010-05-06 01:59:26 +0200390EXPORT_SYMBOL_GPL(pm_qos_request);
Mark Grossd82b3512008-02-04 22:30:08 -0800391
Jean Pihetcc749982011-08-25 15:35:12 +0200392int pm_qos_request_active(struct pm_qos_request *req)
James Bottomley82f68252010-07-05 22:53:06 +0200393{
394 return req->pm_qos_class != 0;
395}
396EXPORT_SYMBOL_GPL(pm_qos_request_active);
397
Stephen Boyd40fea92f2013-08-13 14:12:40 -0700398static void __pm_qos_update_request(struct pm_qos_request *req,
399 s32 new_value)
400{
401 trace_pm_qos_update_request(req->pm_qos_class, new_value);
402
403 if (new_value != req->node.prio)
404 pm_qos_update_target(
405 pm_qos_array[req->pm_qos_class]->constraints,
406 &req->node, PM_QOS_UPDATE_REQ, new_value);
407}
408
Mark Grossd82b3512008-02-04 22:30:08 -0800409/**
MyungJoo Hamc4772d12012-03-28 23:31:24 +0200410 * pm_qos_work_fn - the timeout handler of pm_qos_update_request_timeout
411 * @work: work struct for the delayed work (timeout)
412 *
413 * This cancels the timeout request by falling back to the default at timeout.
414 */
415static void pm_qos_work_fn(struct work_struct *work)
416{
417 struct pm_qos_request *req = container_of(to_delayed_work(work),
418 struct pm_qos_request,
419 work);
420
Stephen Boyd40fea92f2013-08-13 14:12:40 -0700421 __pm_qos_update_request(req, PM_QOS_DEFAULT_VALUE);
MyungJoo Hamc4772d12012-03-28 23:31:24 +0200422}
423
424/**
Mark Grossed771342010-05-06 01:59:26 +0200425 * pm_qos_add_request - inserts new qos request into the list
Jean Pihetcc749982011-08-25 15:35:12 +0200426 * @req: pointer to a preallocated handle
Saravana Kannan25cc69e2010-08-26 20:18:43 +0200427 * @pm_qos_class: identifies which list of qos request to use
Mark Grossd82b3512008-02-04 22:30:08 -0800428 * @value: defines the qos request
429 *
430 * This function inserts a new entry in the pm_qos_class list of requested qos
Richard Hughesbf1db692008-08-05 13:01:35 -0700431 * performance characteristics. It recomputes the aggregate QoS expectations
Jean Pihetcc749982011-08-25 15:35:12 +0200432 * for the pm_qos_class of parameters and initializes the pm_qos_request
Saravana Kannan25cc69e2010-08-26 20:18:43 +0200433 * handle. Caller needs to save this handle for later use in updates and
434 * removal.
Mark Grossd82b3512008-02-04 22:30:08 -0800435 */
Saravana Kannan25cc69e2010-08-26 20:18:43 +0200436
Jean Pihetcc749982011-08-25 15:35:12 +0200437void pm_qos_add_request(struct pm_qos_request *req,
James Bottomley82f68252010-07-05 22:53:06 +0200438 int pm_qos_class, s32 value)
Mark Grossd82b3512008-02-04 22:30:08 -0800439{
Jean Pihetabe98ec2011-08-25 15:35:34 +0200440 if (!req) /*guard against callers passing in null */
441 return;
Mark Grossd82b3512008-02-04 22:30:08 -0800442
Jean Pihetcc749982011-08-25 15:35:12 +0200443 if (pm_qos_request_active(req)) {
James Bottomley82f68252010-07-05 22:53:06 +0200444 WARN(1, KERN_ERR "pm_qos_add_request() called for already added request\n");
445 return;
Mark Grossd82b3512008-02-04 22:30:08 -0800446 }
Jean Pihetcc749982011-08-25 15:35:12 +0200447 req->pm_qos_class = pm_qos_class;
MyungJoo Hamc4772d12012-03-28 23:31:24 +0200448 INIT_DELAYED_WORK(&req->work, pm_qos_work_fn);
Saharaae8822b2013-06-21 11:12:29 +0900449 trace_pm_qos_add_request(pm_qos_class, value);
Jean Pihetabe98ec2011-08-25 15:35:34 +0200450 pm_qos_update_target(pm_qos_array[pm_qos_class]->constraints,
451 &req->node, PM_QOS_ADD_REQ, value);
Mark Grossd82b3512008-02-04 22:30:08 -0800452}
Mark Grossed771342010-05-06 01:59:26 +0200453EXPORT_SYMBOL_GPL(pm_qos_add_request);
Mark Grossd82b3512008-02-04 22:30:08 -0800454
455/**
Mark Grossed771342010-05-06 01:59:26 +0200456 * pm_qos_update_request - modifies an existing qos request
Jean Pihetcc749982011-08-25 15:35:12 +0200457 * @req : handle to list element holding a pm_qos request to use
Mark Grossd82b3512008-02-04 22:30:08 -0800458 * @value: defines the qos request
459 *
Mark Grossed771342010-05-06 01:59:26 +0200460 * Updates an existing qos request for the pm_qos_class of parameters along
Mark Grossd82b3512008-02-04 22:30:08 -0800461 * with updating the target pm_qos_class value.
462 *
Mark Grossed771342010-05-06 01:59:26 +0200463 * Attempts are made to make this code callable on hot code paths.
Mark Grossd82b3512008-02-04 22:30:08 -0800464 */
Jean Pihetcc749982011-08-25 15:35:12 +0200465void pm_qos_update_request(struct pm_qos_request *req,
James Bottomley5f279842010-07-19 02:00:18 +0200466 s32 new_value)
Mark Grossd82b3512008-02-04 22:30:08 -0800467{
Jean Pihetcc749982011-08-25 15:35:12 +0200468 if (!req) /*guard against callers passing in null */
James Bottomley5f279842010-07-19 02:00:18 +0200469 return;
Mark Grossed771342010-05-06 01:59:26 +0200470
Jean Pihetcc749982011-08-25 15:35:12 +0200471 if (!pm_qos_request_active(req)) {
James Bottomley82f68252010-07-05 22:53:06 +0200472 WARN(1, KERN_ERR "pm_qos_update_request() called for unknown object\n");
473 return;
474 }
475
Tejun Heoa81f80f2016-09-16 15:49:33 -0400476 cancel_delayed_work_sync(&req->work);
Stephen Boyd40fea92f2013-08-13 14:12:40 -0700477 __pm_qos_update_request(req, new_value);
Mark Grossd82b3512008-02-04 22:30:08 -0800478}
Mark Grossed771342010-05-06 01:59:26 +0200479EXPORT_SYMBOL_GPL(pm_qos_update_request);
Mark Grossd82b3512008-02-04 22:30:08 -0800480
481/**
MyungJoo Hamc4772d12012-03-28 23:31:24 +0200482 * pm_qos_update_request_timeout - modifies an existing qos request temporarily.
483 * @req : handle to list element holding a pm_qos request to use
484 * @new_value: defines the temporal qos request
485 * @timeout_us: the effective duration of this qos request in usecs.
486 *
487 * After timeout_us, this qos request is cancelled automatically.
488 */
489void pm_qos_update_request_timeout(struct pm_qos_request *req, s32 new_value,
490 unsigned long timeout_us)
491{
492 if (!req)
493 return;
494 if (WARN(!pm_qos_request_active(req),
495 "%s called for unknown object.", __func__))
496 return;
497
Tejun Heoed1ac6e2013-01-11 13:37:33 +0100498 cancel_delayed_work_sync(&req->work);
MyungJoo Hamc4772d12012-03-28 23:31:24 +0200499
Saharaae8822b2013-06-21 11:12:29 +0900500 trace_pm_qos_update_request_timeout(req->pm_qos_class,
501 new_value, timeout_us);
MyungJoo Hamc4772d12012-03-28 23:31:24 +0200502 if (new_value != req->node.prio)
503 pm_qos_update_target(
504 pm_qos_array[req->pm_qos_class]->constraints,
505 &req->node, PM_QOS_UPDATE_REQ, new_value);
506
507 schedule_delayed_work(&req->work, usecs_to_jiffies(timeout_us));
508}
509
510/**
Mark Grossed771342010-05-06 01:59:26 +0200511 * pm_qos_remove_request - modifies an existing qos request
Jean Pihetcc749982011-08-25 15:35:12 +0200512 * @req: handle to request list element
Mark Grossd82b3512008-02-04 22:30:08 -0800513 *
Jean Pihetcc749982011-08-25 15:35:12 +0200514 * Will remove pm qos request from the list of constraints and
Mark Grossed771342010-05-06 01:59:26 +0200515 * recompute the current target value for the pm_qos_class. Call this
516 * on slow code paths.
Mark Grossd82b3512008-02-04 22:30:08 -0800517 */
Jean Pihetcc749982011-08-25 15:35:12 +0200518void pm_qos_remove_request(struct pm_qos_request *req)
Mark Grossd82b3512008-02-04 22:30:08 -0800519{
Jean Pihetabe98ec2011-08-25 15:35:34 +0200520 if (!req) /*guard against callers passing in null */
Mark Grossed771342010-05-06 01:59:26 +0200521 return;
522 /* silent return to keep pcm code cleaner */
523
Jean Pihetcc749982011-08-25 15:35:12 +0200524 if (!pm_qos_request_active(req)) {
James Bottomley82f68252010-07-05 22:53:06 +0200525 WARN(1, KERN_ERR "pm_qos_remove_request() called for unknown object\n");
526 return;
527 }
528
Tejun Heoed1ac6e2013-01-11 13:37:33 +0100529 cancel_delayed_work_sync(&req->work);
MyungJoo Hamc4772d12012-03-28 23:31:24 +0200530
Saharaae8822b2013-06-21 11:12:29 +0900531 trace_pm_qos_remove_request(req->pm_qos_class, PM_QOS_DEFAULT_VALUE);
Jean Pihetabe98ec2011-08-25 15:35:34 +0200532 pm_qos_update_target(pm_qos_array[req->pm_qos_class]->constraints,
533 &req->node, PM_QOS_REMOVE_REQ,
534 PM_QOS_DEFAULT_VALUE);
Jean Pihetcc749982011-08-25 15:35:12 +0200535 memset(req, 0, sizeof(*req));
Mark Grossd82b3512008-02-04 22:30:08 -0800536}
Mark Grossed771342010-05-06 01:59:26 +0200537EXPORT_SYMBOL_GPL(pm_qos_remove_request);
Mark Grossd82b3512008-02-04 22:30:08 -0800538
539/**
540 * pm_qos_add_notifier - sets notification entry for changes to target value
541 * @pm_qos_class: identifies which qos target changes should be notified.
542 * @notifier: notifier block managed by caller.
543 *
544 * will register the notifier into a notification chain that gets called
Richard Hughesbf1db692008-08-05 13:01:35 -0700545 * upon changes to the pm_qos_class target value.
Mark Grossd82b3512008-02-04 22:30:08 -0800546 */
Mark Grossed771342010-05-06 01:59:26 +0200547int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
Mark Grossd82b3512008-02-04 22:30:08 -0800548{
549 int retval;
550
551 retval = blocking_notifier_chain_register(
Jean Pihet4e1779b2011-08-25 15:35:27 +0200552 pm_qos_array[pm_qos_class]->constraints->notifiers,
553 notifier);
Mark Grossd82b3512008-02-04 22:30:08 -0800554
555 return retval;
556}
557EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
558
559/**
560 * pm_qos_remove_notifier - deletes notification entry from chain.
561 * @pm_qos_class: identifies which qos target changes are notified.
562 * @notifier: notifier block to be removed.
563 *
564 * will remove the notifier from the notification chain that gets called
Richard Hughesbf1db692008-08-05 13:01:35 -0700565 * upon changes to the pm_qos_class target value.
Mark Grossd82b3512008-02-04 22:30:08 -0800566 */
567int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
568{
569 int retval;
570
571 retval = blocking_notifier_chain_unregister(
Jean Pihet4e1779b2011-08-25 15:35:27 +0200572 pm_qos_array[pm_qos_class]->constraints->notifiers,
573 notifier);
Mark Grossd82b3512008-02-04 22:30:08 -0800574
575 return retval;
576}
577EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
578
Jean Pihet4a31a332011-08-25 15:35:20 +0200579/* User space interface to PM QoS classes via misc devices */
Nishanth Menonf5f4eda2014-12-05 11:19:08 -0600580static int register_pm_qos_misc(struct pm_qos_object *qos, struct dentry *d)
Jean Pihet4a31a332011-08-25 15:35:20 +0200581{
582 qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR;
583 qos->pm_qos_power_miscdev.name = qos->name;
584 qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops;
585
Greg Kroah-Hartman659dc452019-01-22 16:21:47 +0100586 debugfs_create_file(qos->name, S_IRUGO, d, (void *)qos,
587 &pm_qos_debug_fops);
Nishanth Menonf5f4eda2014-12-05 11:19:08 -0600588
Jean Pihet4a31a332011-08-25 15:35:20 +0200589 return misc_register(&qos->pm_qos_power_miscdev);
590}
591
592static int find_pm_qos_object_by_minor(int minor)
593{
594 int pm_qos_class;
595
Saharad24c2a42013-06-20 11:33:57 +0900596 for (pm_qos_class = PM_QOS_CPU_DMA_LATENCY;
Jean Pihet4a31a332011-08-25 15:35:20 +0200597 pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) {
598 if (minor ==
599 pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor)
600 return pm_qos_class;
601 }
602 return -1;
603}
604
Mark Grossd82b3512008-02-04 22:30:08 -0800605static int pm_qos_power_open(struct inode *inode, struct file *filp)
606{
Mark Grossd82b3512008-02-04 22:30:08 -0800607 long pm_qos_class;
608
609 pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
Saharad24c2a42013-06-20 11:33:57 +0900610 if (pm_qos_class >= PM_QOS_CPU_DMA_LATENCY) {
Jean Pihetcc749982011-08-25 15:35:12 +0200611 struct pm_qos_request *req = kzalloc(sizeof(*req), GFP_KERNEL);
James Bottomley82f68252010-07-05 22:53:06 +0200612 if (!req)
613 return -ENOMEM;
614
615 pm_qos_add_request(req, pm_qos_class, PM_QOS_DEFAULT_VALUE);
616 filp->private_data = req;
Mark Grossed771342010-05-06 01:59:26 +0200617
Guennadi Liakhovetski6513fd62011-11-03 10:12:36 +0100618 return 0;
Mark Grossd82b3512008-02-04 22:30:08 -0800619 }
Mark Grossd82b3512008-02-04 22:30:08 -0800620 return -EPERM;
621}
622
623static int pm_qos_power_release(struct inode *inode, struct file *filp)
624{
Jean Pihetcc749982011-08-25 15:35:12 +0200625 struct pm_qos_request *req;
Mark Grossd82b3512008-02-04 22:30:08 -0800626
James Bottomley82f68252010-07-05 22:53:06 +0200627 req = filp->private_data;
Mark Grossed771342010-05-06 01:59:26 +0200628 pm_qos_remove_request(req);
James Bottomley82f68252010-07-05 22:53:06 +0200629 kfree(req);
Mark Grossd82b3512008-02-04 22:30:08 -0800630
631 return 0;
632}
633
Mark Grossed771342010-05-06 01:59:26 +0200634
Thomas Renningerf9b9e802011-02-28 22:06:34 +0100635static ssize_t pm_qos_power_read(struct file *filp, char __user *buf,
636 size_t count, loff_t *f_pos)
637{
638 s32 value;
639 unsigned long flags;
Jean Pihetcc749982011-08-25 15:35:12 +0200640 struct pm_qos_request *req = filp->private_data;
Thomas Renningerf9b9e802011-02-28 22:06:34 +0100641
Jean Pihetcc749982011-08-25 15:35:12 +0200642 if (!req)
Thomas Renningerf9b9e802011-02-28 22:06:34 +0100643 return -EINVAL;
Jean Pihetcc749982011-08-25 15:35:12 +0200644 if (!pm_qos_request_active(req))
Thomas Renningerf9b9e802011-02-28 22:06:34 +0100645 return -EINVAL;
646
Thomas Renningerf9b9e802011-02-28 22:06:34 +0100647 spin_lock_irqsave(&pm_qos_lock, flags);
Jean Pihetabe98ec2011-08-25 15:35:34 +0200648 value = pm_qos_get_value(pm_qos_array[req->pm_qos_class]->constraints);
Thomas Renningerf9b9e802011-02-28 22:06:34 +0100649 spin_unlock_irqrestore(&pm_qos_lock, flags);
650
651 return simple_read_from_buffer(buf, count, f_pos, &value, sizeof(s32));
652}
653
Mark Grossd82b3512008-02-04 22:30:08 -0800654static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
655 size_t count, loff_t *f_pos)
656{
657 s32 value;
Jean Pihetcc749982011-08-25 15:35:12 +0200658 struct pm_qos_request *req;
Mark Grossd82b3512008-02-04 22:30:08 -0800659
Mark Grossed771342010-05-06 01:59:26 +0200660 if (count == sizeof(s32)) {
661 if (copy_from_user(&value, buf, sizeof(s32)))
662 return -EFAULT;
Andy Shevchenkod4f7ecf2013-09-11 17:02:38 +0300663 } else {
Rafael J. Wysocki0775a602011-05-27 00:05:23 +0200664 int ret;
665
Andy Shevchenkod4f7ecf2013-09-11 17:02:38 +0300666 ret = kstrtos32_from_user(buf, count, 16, &value);
667 if (ret)
668 return ret;
Rafael J. Wysocki0775a602011-05-27 00:05:23 +0200669 }
Mark Grossd82b3512008-02-04 22:30:08 -0800670
Jean Pihetcc749982011-08-25 15:35:12 +0200671 req = filp->private_data;
672 pm_qos_update_request(req, value);
Mark Grossed771342010-05-06 01:59:26 +0200673
674 return count;
Mark Grossd82b3512008-02-04 22:30:08 -0800675}
676
677
678static int __init pm_qos_power_init(void)
679{
680 int ret = 0;
Alex Fridd031e1d2012-01-29 20:39:25 +0100681 int i;
Nishanth Menonf5f4eda2014-12-05 11:19:08 -0600682 struct dentry *d;
Mark Grossd82b3512008-02-04 22:30:08 -0800683
Alex Fridd031e1d2012-01-29 20:39:25 +0100684 BUILD_BUG_ON(ARRAY_SIZE(pm_qos_array) != PM_QOS_NUM_CLASSES);
685
Nishanth Menonf5f4eda2014-12-05 11:19:08 -0600686 d = debugfs_create_dir("pm_qos", NULL);
Nishanth Menonf5f4eda2014-12-05 11:19:08 -0600687
Saharad24c2a42013-06-20 11:33:57 +0900688 for (i = PM_QOS_CPU_DMA_LATENCY; i < PM_QOS_NUM_CLASSES; i++) {
Nishanth Menonf5f4eda2014-12-05 11:19:08 -0600689 ret = register_pm_qos_misc(pm_qos_array[i], d);
Alex Fridd031e1d2012-01-29 20:39:25 +0100690 if (ret < 0) {
Joe Perches64ec72a2017-09-27 22:01:34 -0700691 pr_err("%s: %s setup failed\n",
692 __func__, pm_qos_array[i]->name);
Alex Fridd031e1d2012-01-29 20:39:25 +0100693 return ret;
694 }
Mark Grossd82b3512008-02-04 22:30:08 -0800695 }
Mark Grossd82b3512008-02-04 22:30:08 -0800696
697 return ret;
698}
699
700late_initcall(pm_qos_power_init);