1 // SPDX-License-Identifier: GPL-2.0-only
3 * This module exposes the interface to kernel space for specifying
4 * QoS dependencies. It provides infrastructure for registration of:
6 * Dependents on a QoS value : register requests
7 * Watchers of QoS value : get notified when target QoS value changes
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.
12 * There are 3 basic classes of QoS parameter: latency, timeout, throughput
13 * each have defined units:
15 * timeout: usec <-- currently not used.
16 * throughput: kbs (kilo byte / sec)
18 * There are lists of pm_qos_objects each one wrapping requests, notifiers
20 * User mode requests on a QOS parameter register themselves to the
21 * 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
24 * 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
26 * pointer or exits the pm_qos_object will get an opportunity to clean up.
28 * Mark Gross <mgross@linux.intel.com>
33 #include <linux/pm_qos.h>
34 #include <linux/sched.h>
35 #include <linux/spinlock.h>
36 #include <linux/slab.h>
37 #include <linux/time.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>
44 #include <linux/kernel.h>
45 #include <linux/debugfs.h>
46 #include <linux/seq_file.h>
48 #include <linux/uaccess.h>
49 #include <linux/export.h>
50 #include <trace/events/power.h>
53 * locking rule: all changes to constraints or notifiers lists
54 * 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
57 static DEFINE_SPINLOCK(pm_qos_lock);
60 * pm_qos_read_value - Return the current effective constraint value.
61 * @c: List of PM QoS constraint requests.
63 s32 pm_qos_read_value(struct pm_qos_constraints *c)
65 return c->target_value;
68 static int pm_qos_get_value(struct pm_qos_constraints *c)
70 if (plist_head_empty(&c->list))
71 return c->no_constraint_value;
75 return plist_first(&c->list)->prio;
78 return plist_last(&c->list)->prio;
81 WARN(1, "Unknown PM QoS type in %s\n", __func__);
82 return PM_QOS_DEFAULT_VALUE;
86 static void pm_qos_set_value(struct pm_qos_constraints *c, s32 value)
88 c->target_value = value;
92 * pm_qos_update_target - Update a list of PM QoS constraint requests.
93 * @c: List of PM QoS requests.
94 * @node: Target list entry.
95 * @action: Action to carry out (add, update or remove).
96 * @value: New request value for the target list entry.
98 * Update the given list of PM QoS constraint requests, @c, by carrying an
99 * @action involving the @node list entry and @value on it.
101 * The recognized values of @action are PM_QOS_ADD_REQ (store @value in @node
102 * and add it to the list), PM_QOS_UPDATE_REQ (remove @node from the list, store
103 * @value in it and add it to the list again), and PM_QOS_REMOVE_REQ (remove
104 * @node from the list, ignore @value).
106 * Return: 1 if the aggregate constraint value has changed, 0 otherwise.
108 int pm_qos_update_target(struct pm_qos_constraints *c, struct plist_node *node,
109 enum pm_qos_req_action action, int value)
111 int prev_value, curr_value, new_value;
114 spin_lock_irqsave(&pm_qos_lock, flags);
116 prev_value = pm_qos_get_value(c);
117 if (value == PM_QOS_DEFAULT_VALUE)
118 new_value = c->default_value;
123 case PM_QOS_REMOVE_REQ:
124 plist_del(node, &c->list);
126 case PM_QOS_UPDATE_REQ:
128 * To change the list, atomically remove, reinit with new value
129 * and add, then see if the aggregate has changed.
131 plist_del(node, &c->list);
134 plist_node_init(node, new_value);
135 plist_add(node, &c->list);
142 curr_value = pm_qos_get_value(c);
143 pm_qos_set_value(c, curr_value);
145 spin_unlock_irqrestore(&pm_qos_lock, flags);
147 trace_pm_qos_update_target(action, prev_value, curr_value);
149 if (prev_value == curr_value)
153 blocking_notifier_call_chain(c->notifiers, curr_value, NULL);
159 * pm_qos_flags_remove_req - Remove device PM QoS flags request.
160 * @pqf: Device PM QoS flags set to remove the request from.
161 * @req: Request to remove from the set.
163 static void pm_qos_flags_remove_req(struct pm_qos_flags *pqf,
164 struct pm_qos_flags_request *req)
168 list_del(&req->node);
169 list_for_each_entry(req, &pqf->list, node)
172 pqf->effective_flags = val;
176 * pm_qos_update_flags - Update a set of PM QoS flags.
177 * @pqf: Set of PM QoS flags to update.
178 * @req: Request to add to the set, to modify, or to remove from the set.
179 * @action: Action to take on the set.
180 * @val: Value of the request to add or modify.
182 * Return: 1 if the aggregate constraint value has changed, 0 otherwise.
184 bool pm_qos_update_flags(struct pm_qos_flags *pqf,
185 struct pm_qos_flags_request *req,
186 enum pm_qos_req_action action, s32 val)
188 unsigned long irqflags;
189 s32 prev_value, curr_value;
191 spin_lock_irqsave(&pm_qos_lock, irqflags);
193 prev_value = list_empty(&pqf->list) ? 0 : pqf->effective_flags;
196 case PM_QOS_REMOVE_REQ:
197 pm_qos_flags_remove_req(pqf, req);
199 case PM_QOS_UPDATE_REQ:
200 pm_qos_flags_remove_req(pqf, req);
204 INIT_LIST_HEAD(&req->node);
205 list_add_tail(&req->node, &pqf->list);
206 pqf->effective_flags |= val;
213 curr_value = list_empty(&pqf->list) ? 0 : pqf->effective_flags;
215 spin_unlock_irqrestore(&pm_qos_lock, irqflags);
217 trace_pm_qos_update_flags(action, prev_value, curr_value);
219 return prev_value != curr_value;
222 /* Definitions related to the CPU latency QoS. */
224 static struct pm_qos_constraints cpu_latency_constraints = {
225 .list = PLIST_HEAD_INIT(cpu_latency_constraints.list),
226 .target_value = PM_QOS_CPU_LATENCY_DEFAULT_VALUE,
227 .default_value = PM_QOS_CPU_LATENCY_DEFAULT_VALUE,
228 .no_constraint_value = PM_QOS_CPU_LATENCY_DEFAULT_VALUE,
233 * pm_qos_request - returns current system wide qos expectation
234 * @pm_qos_class: Ignored.
236 * This function returns the current target value.
238 int pm_qos_request(int pm_qos_class)
240 return pm_qos_read_value(&cpu_latency_constraints);
242 EXPORT_SYMBOL_GPL(pm_qos_request);
244 int pm_qos_request_active(struct pm_qos_request *req)
246 return req->qos == &cpu_latency_constraints;
248 EXPORT_SYMBOL_GPL(pm_qos_request_active);
250 static void cpu_latency_qos_update(struct pm_qos_request *req,
251 enum pm_qos_req_action action, s32 value)
253 int ret = pm_qos_update_target(req->qos, &req->node, action, value);
255 wake_up_all_idle_cpus();
259 * pm_qos_add_request - inserts new qos request into the list
260 * @req: pointer to a preallocated handle
261 * @pm_qos_class: Ignored.
262 * @value: defines the qos request
264 * This function inserts a new entry in the PM_QOS_CPU_DMA_LATENCY list of
265 * requested QoS performance characteristics. It recomputes the aggregate QoS
266 * expectations for the PM_QOS_CPU_DMA_LATENCY list and initializes the @req
267 * handle. Caller needs to save this handle for later use in updates and
270 void pm_qos_add_request(struct pm_qos_request *req,
271 int pm_qos_class, s32 value)
273 if (!req) /*guard against callers passing in null */
276 if (pm_qos_request_active(req)) {
277 WARN(1, KERN_ERR "pm_qos_add_request() called for already added request\n");
281 trace_pm_qos_add_request(PM_QOS_CPU_DMA_LATENCY, value);
283 req->qos = &cpu_latency_constraints;
284 cpu_latency_qos_update(req, PM_QOS_ADD_REQ, value);
286 EXPORT_SYMBOL_GPL(pm_qos_add_request);
289 * pm_qos_update_request - modifies an existing qos request
290 * @req : handle to list element holding a pm_qos request to use
291 * @value: defines the qos request
293 * Updates an existing qos request for the PM_QOS_CPU_DMA_LATENCY list along
294 * with updating the target PM_QOS_CPU_DMA_LATENCY value.
296 * Attempts are made to make this code callable on hot code paths.
298 void pm_qos_update_request(struct pm_qos_request *req, s32 new_value)
300 if (!req) /*guard against callers passing in null */
303 if (!pm_qos_request_active(req)) {
304 WARN(1, KERN_ERR "pm_qos_update_request() called for unknown object\n");
308 trace_pm_qos_update_request(PM_QOS_CPU_DMA_LATENCY, new_value);
310 if (new_value == req->node.prio)
313 cpu_latency_qos_update(req, PM_QOS_UPDATE_REQ, new_value);
315 EXPORT_SYMBOL_GPL(pm_qos_update_request);
318 * pm_qos_remove_request - modifies an existing qos request
319 * @req: handle to request list element
321 * Will remove pm qos request from the list of constraints and
322 * recompute the current target value for PM_QOS_CPU_DMA_LATENCY. Call this
323 * on slow code paths.
325 void pm_qos_remove_request(struct pm_qos_request *req)
327 if (!req) /*guard against callers passing in null */
329 /* silent return to keep pcm code cleaner */
331 if (!pm_qos_request_active(req)) {
332 WARN(1, KERN_ERR "pm_qos_remove_request() called for unknown object\n");
336 trace_pm_qos_remove_request(PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
338 cpu_latency_qos_update(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
339 memset(req, 0, sizeof(*req));
341 EXPORT_SYMBOL_GPL(pm_qos_remove_request);
343 /* User space interface to the CPU latency QoS via misc device. */
345 static int cpu_latency_qos_open(struct inode *inode, struct file *filp)
347 struct pm_qos_request *req;
349 req = kzalloc(sizeof(*req), GFP_KERNEL);
353 pm_qos_add_request(req, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
354 filp->private_data = req;
359 static int cpu_latency_qos_release(struct inode *inode, struct file *filp)
361 struct pm_qos_request *req = filp->private_data;
363 filp->private_data = NULL;
365 pm_qos_remove_request(req);
371 static ssize_t cpu_latency_qos_read(struct file *filp, char __user *buf,
372 size_t count, loff_t *f_pos)
374 struct pm_qos_request *req = filp->private_data;
378 if (!req || !pm_qos_request_active(req))
381 spin_lock_irqsave(&pm_qos_lock, flags);
382 value = pm_qos_get_value(&cpu_latency_constraints);
383 spin_unlock_irqrestore(&pm_qos_lock, flags);
385 return simple_read_from_buffer(buf, count, f_pos, &value, sizeof(s32));
388 static ssize_t cpu_latency_qos_write(struct file *filp, const char __user *buf,
389 size_t count, loff_t *f_pos)
393 if (count == sizeof(s32)) {
394 if (copy_from_user(&value, buf, sizeof(s32)))
399 ret = kstrtos32_from_user(buf, count, 16, &value);
404 pm_qos_update_request(filp->private_data, value);
409 static const struct file_operations cpu_latency_qos_fops = {
410 .write = cpu_latency_qos_write,
411 .read = cpu_latency_qos_read,
412 .open = cpu_latency_qos_open,
413 .release = cpu_latency_qos_release,
414 .llseek = noop_llseek,
417 static struct miscdevice cpu_latency_qos_miscdev = {
418 .minor = MISC_DYNAMIC_MINOR,
419 .name = "cpu_dma_latency",
420 .fops = &cpu_latency_qos_fops,
423 static int __init cpu_latency_qos_init(void)
427 ret = misc_register(&cpu_latency_qos_miscdev);
429 pr_err("%s: %s setup failed\n", __func__,
430 cpu_latency_qos_miscdev.name);
434 late_initcall(cpu_latency_qos_init);
436 /* Definitions related to the frequency QoS below. */
439 * freq_constraints_init - Initialize frequency QoS constraints.
440 * @qos: Frequency QoS constraints to initialize.
442 void freq_constraints_init(struct freq_constraints *qos)
444 struct pm_qos_constraints *c;
447 plist_head_init(&c->list);
448 c->target_value = FREQ_QOS_MIN_DEFAULT_VALUE;
449 c->default_value = FREQ_QOS_MIN_DEFAULT_VALUE;
450 c->no_constraint_value = FREQ_QOS_MIN_DEFAULT_VALUE;
451 c->type = PM_QOS_MAX;
452 c->notifiers = &qos->min_freq_notifiers;
453 BLOCKING_INIT_NOTIFIER_HEAD(c->notifiers);
456 plist_head_init(&c->list);
457 c->target_value = FREQ_QOS_MAX_DEFAULT_VALUE;
458 c->default_value = FREQ_QOS_MAX_DEFAULT_VALUE;
459 c->no_constraint_value = FREQ_QOS_MAX_DEFAULT_VALUE;
460 c->type = PM_QOS_MIN;
461 c->notifiers = &qos->max_freq_notifiers;
462 BLOCKING_INIT_NOTIFIER_HEAD(c->notifiers);
466 * freq_qos_read_value - Get frequency QoS constraint for a given list.
467 * @qos: Constraints to evaluate.
468 * @type: QoS request type.
470 s32 freq_qos_read_value(struct freq_constraints *qos,
471 enum freq_qos_req_type type)
477 ret = IS_ERR_OR_NULL(qos) ?
478 FREQ_QOS_MIN_DEFAULT_VALUE :
479 pm_qos_read_value(&qos->min_freq);
482 ret = IS_ERR_OR_NULL(qos) ?
483 FREQ_QOS_MAX_DEFAULT_VALUE :
484 pm_qos_read_value(&qos->max_freq);
495 * freq_qos_apply - Add/modify/remove frequency QoS request.
496 * @req: Constraint request to apply.
497 * @action: Action to perform (add/update/remove).
498 * @value: Value to assign to the QoS request.
500 * This is only meant to be called from inside pm_qos, not drivers.
502 int freq_qos_apply(struct freq_qos_request *req,
503 enum pm_qos_req_action action, s32 value)
509 ret = pm_qos_update_target(&req->qos->min_freq, &req->pnode,
513 ret = pm_qos_update_target(&req->qos->max_freq, &req->pnode,
524 * freq_qos_add_request - Insert new frequency QoS request into a given list.
525 * @qos: Constraints to update.
526 * @req: Preallocated request object.
527 * @type: Request type.
528 * @value: Request value.
530 * Insert a new entry into the @qos list of requests, recompute the effective
531 * QoS constraint value for that list and initialize the @req object. The
532 * caller needs to save that object for later use in updates and removal.
534 * Return 1 if the effective constraint value has changed, 0 if the effective
535 * constraint value has not changed, or a negative error code on failures.
537 int freq_qos_add_request(struct freq_constraints *qos,
538 struct freq_qos_request *req,
539 enum freq_qos_req_type type, s32 value)
543 if (IS_ERR_OR_NULL(qos) || !req)
546 if (WARN(freq_qos_request_active(req),
547 "%s() called for active request\n", __func__))
552 ret = freq_qos_apply(req, PM_QOS_ADD_REQ, value);
560 EXPORT_SYMBOL_GPL(freq_qos_add_request);
563 * freq_qos_update_request - Modify existing frequency QoS request.
564 * @req: Request to modify.
565 * @new_value: New request value.
567 * Update an existing frequency QoS request along with the effective constraint
568 * value for the list of requests it belongs to.
570 * Return 1 if the effective constraint value has changed, 0 if the effective
571 * constraint value has not changed, or a negative error code on failures.
573 int freq_qos_update_request(struct freq_qos_request *req, s32 new_value)
578 if (WARN(!freq_qos_request_active(req),
579 "%s() called for unknown object\n", __func__))
582 if (req->pnode.prio == new_value)
585 return freq_qos_apply(req, PM_QOS_UPDATE_REQ, new_value);
587 EXPORT_SYMBOL_GPL(freq_qos_update_request);
590 * freq_qos_remove_request - Remove frequency QoS request from its list.
591 * @req: Request to remove.
593 * Remove the given frequency QoS request from the list of constraints it
594 * belongs to and recompute the effective constraint value for that list.
596 * Return 1 if the effective constraint value has changed, 0 if the effective
597 * constraint value has not changed, or a negative error code on failures.
599 int freq_qos_remove_request(struct freq_qos_request *req)
606 if (WARN(!freq_qos_request_active(req),
607 "%s() called for unknown object\n", __func__))
610 ret = freq_qos_apply(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
616 EXPORT_SYMBOL_GPL(freq_qos_remove_request);
619 * freq_qos_add_notifier - Add frequency QoS change notifier.
620 * @qos: List of requests to add the notifier to.
621 * @type: Request type.
622 * @notifier: Notifier block to add.
624 int freq_qos_add_notifier(struct freq_constraints *qos,
625 enum freq_qos_req_type type,
626 struct notifier_block *notifier)
630 if (IS_ERR_OR_NULL(qos) || !notifier)
635 ret = blocking_notifier_chain_register(qos->min_freq.notifiers,
639 ret = blocking_notifier_chain_register(qos->max_freq.notifiers,
649 EXPORT_SYMBOL_GPL(freq_qos_add_notifier);
652 * freq_qos_remove_notifier - Remove frequency QoS change notifier.
653 * @qos: List of requests to remove the notifier from.
654 * @type: Request type.
655 * @notifier: Notifier block to remove.
657 int freq_qos_remove_notifier(struct freq_constraints *qos,
658 enum freq_qos_req_type type,
659 struct notifier_block *notifier)
663 if (IS_ERR_OR_NULL(qos) || !notifier)
668 ret = blocking_notifier_chain_unregister(qos->min_freq.notifiers,
672 ret = blocking_notifier_chain_unregister(qos->max_freq.notifiers,
682 EXPORT_SYMBOL_GPL(freq_qos_remove_notifier);