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 struct pm_qos_object {
58 struct pm_qos_constraints *constraints;
59 struct miscdevice pm_qos_power_miscdev;
63 static DEFINE_SPINLOCK(pm_qos_lock);
65 static struct pm_qos_object null_pm_qos;
67 static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
68 static struct pm_qos_constraints cpu_dma_constraints = {
69 .list = PLIST_HEAD_INIT(cpu_dma_constraints.list),
70 .target_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE,
71 .default_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE,
72 .no_constraint_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE,
74 .notifiers = &cpu_dma_lat_notifier,
76 static struct pm_qos_object cpu_dma_pm_qos = {
77 .constraints = &cpu_dma_constraints,
78 .name = "cpu_dma_latency",
81 static struct pm_qos_object *pm_qos_array[] = {
87 * pm_qos_read_value - Return the current effective constraint value.
88 * @c: List of PM QoS constraint requests.
90 s32 pm_qos_read_value(struct pm_qos_constraints *c)
92 return c->target_value;
95 static int pm_qos_get_value(struct pm_qos_constraints *c)
97 if (plist_head_empty(&c->list))
98 return c->no_constraint_value;
102 return plist_first(&c->list)->prio;
105 return plist_last(&c->list)->prio;
108 WARN(1, "Unknown PM QoS type in %s\n", __func__);
109 return PM_QOS_DEFAULT_VALUE;
113 static void pm_qos_set_value(struct pm_qos_constraints *c, s32 value)
115 c->target_value = value;
119 * pm_qos_update_target - Update a list of PM QoS constraint requests.
120 * @c: List of PM QoS requests.
121 * @node: Target list entry.
122 * @action: Action to carry out (add, update or remove).
123 * @value: New request value for the target list entry.
125 * Update the given list of PM QoS constraint requests, @c, by carrying an
126 * @action involving the @node list entry and @value on it.
128 * The recognized values of @action are PM_QOS_ADD_REQ (store @value in @node
129 * and add it to the list), PM_QOS_UPDATE_REQ (remove @node from the list, store
130 * @value in it and add it to the list again), and PM_QOS_REMOVE_REQ (remove
131 * @node from the list, ignore @value).
133 * Return: 1 if the aggregate constraint value has changed, 0 otherwise.
135 int pm_qos_update_target(struct pm_qos_constraints *c, struct plist_node *node,
136 enum pm_qos_req_action action, int value)
138 int prev_value, curr_value, new_value;
141 spin_lock_irqsave(&pm_qos_lock, flags);
143 prev_value = pm_qos_get_value(c);
144 if (value == PM_QOS_DEFAULT_VALUE)
145 new_value = c->default_value;
150 case PM_QOS_REMOVE_REQ:
151 plist_del(node, &c->list);
153 case PM_QOS_UPDATE_REQ:
155 * To change the list, atomically remove, reinit with new value
156 * and add, then see if the aggregate has changed.
158 plist_del(node, &c->list);
161 plist_node_init(node, new_value);
162 plist_add(node, &c->list);
169 curr_value = pm_qos_get_value(c);
170 pm_qos_set_value(c, curr_value);
172 spin_unlock_irqrestore(&pm_qos_lock, flags);
174 trace_pm_qos_update_target(action, prev_value, curr_value);
176 if (prev_value == curr_value)
180 blocking_notifier_call_chain(c->notifiers, curr_value, NULL);
186 * pm_qos_flags_remove_req - Remove device PM QoS flags request.
187 * @pqf: Device PM QoS flags set to remove the request from.
188 * @req: Request to remove from the set.
190 static void pm_qos_flags_remove_req(struct pm_qos_flags *pqf,
191 struct pm_qos_flags_request *req)
195 list_del(&req->node);
196 list_for_each_entry(req, &pqf->list, node)
199 pqf->effective_flags = val;
203 * pm_qos_update_flags - Update a set of PM QoS flags.
204 * @pqf: Set of PM QoS flags to update.
205 * @req: Request to add to the set, to modify, or to remove from the set.
206 * @action: Action to take on the set.
207 * @val: Value of the request to add or modify.
209 * Return: 1 if the aggregate constraint value has changed, 0 otherwise.
211 bool pm_qos_update_flags(struct pm_qos_flags *pqf,
212 struct pm_qos_flags_request *req,
213 enum pm_qos_req_action action, s32 val)
215 unsigned long irqflags;
216 s32 prev_value, curr_value;
218 spin_lock_irqsave(&pm_qos_lock, irqflags);
220 prev_value = list_empty(&pqf->list) ? 0 : pqf->effective_flags;
223 case PM_QOS_REMOVE_REQ:
224 pm_qos_flags_remove_req(pqf, req);
226 case PM_QOS_UPDATE_REQ:
227 pm_qos_flags_remove_req(pqf, req);
231 INIT_LIST_HEAD(&req->node);
232 list_add_tail(&req->node, &pqf->list);
233 pqf->effective_flags |= val;
240 curr_value = list_empty(&pqf->list) ? 0 : pqf->effective_flags;
242 spin_unlock_irqrestore(&pm_qos_lock, irqflags);
244 trace_pm_qos_update_flags(action, prev_value, curr_value);
246 return prev_value != curr_value;
250 * pm_qos_request - returns current system wide qos expectation
251 * @pm_qos_class: identification of which qos value is requested
253 * This function returns the current target value.
255 int pm_qos_request(int pm_qos_class)
257 return pm_qos_read_value(pm_qos_array[pm_qos_class]->constraints);
259 EXPORT_SYMBOL_GPL(pm_qos_request);
261 int pm_qos_request_active(struct pm_qos_request *req)
263 return req->pm_qos_class != 0;
265 EXPORT_SYMBOL_GPL(pm_qos_request_active);
267 static void __pm_qos_update_request(struct pm_qos_request *req,
270 trace_pm_qos_update_request(req->pm_qos_class, new_value);
272 if (new_value != req->node.prio)
273 pm_qos_update_target(
274 pm_qos_array[req->pm_qos_class]->constraints,
275 &req->node, PM_QOS_UPDATE_REQ, new_value);
279 * pm_qos_add_request - inserts new qos request into the list
280 * @req: pointer to a preallocated handle
281 * @pm_qos_class: identifies which list of qos request to use
282 * @value: defines the qos request
284 * This function inserts a new entry in the pm_qos_class list of requested qos
285 * performance characteristics. It recomputes the aggregate QoS expectations
286 * for the pm_qos_class of parameters and initializes the pm_qos_request
287 * handle. Caller needs to save this handle for later use in updates and
291 void pm_qos_add_request(struct pm_qos_request *req,
292 int pm_qos_class, s32 value)
294 if (!req) /*guard against callers passing in null */
297 if (pm_qos_request_active(req)) {
298 WARN(1, KERN_ERR "pm_qos_add_request() called for already added request\n");
301 req->pm_qos_class = pm_qos_class;
302 trace_pm_qos_add_request(pm_qos_class, value);
303 pm_qos_update_target(pm_qos_array[pm_qos_class]->constraints,
304 &req->node, PM_QOS_ADD_REQ, value);
306 EXPORT_SYMBOL_GPL(pm_qos_add_request);
309 * pm_qos_update_request - modifies an existing qos request
310 * @req : handle to list element holding a pm_qos request to use
311 * @value: defines the qos request
313 * Updates an existing qos request for the pm_qos_class of parameters along
314 * with updating the target pm_qos_class value.
316 * Attempts are made to make this code callable on hot code paths.
318 void pm_qos_update_request(struct pm_qos_request *req,
321 if (!req) /*guard against callers passing in null */
324 if (!pm_qos_request_active(req)) {
325 WARN(1, KERN_ERR "pm_qos_update_request() called for unknown object\n");
329 __pm_qos_update_request(req, new_value);
331 EXPORT_SYMBOL_GPL(pm_qos_update_request);
334 * pm_qos_remove_request - modifies an existing qos request
335 * @req: handle to request list element
337 * Will remove pm qos request from the list of constraints and
338 * recompute the current target value for the pm_qos_class. Call this
339 * on slow code paths.
341 void pm_qos_remove_request(struct pm_qos_request *req)
343 if (!req) /*guard against callers passing in null */
345 /* silent return to keep pcm code cleaner */
347 if (!pm_qos_request_active(req)) {
348 WARN(1, KERN_ERR "pm_qos_remove_request() called for unknown object\n");
352 trace_pm_qos_remove_request(req->pm_qos_class, PM_QOS_DEFAULT_VALUE);
353 pm_qos_update_target(pm_qos_array[req->pm_qos_class]->constraints,
354 &req->node, PM_QOS_REMOVE_REQ,
355 PM_QOS_DEFAULT_VALUE);
356 memset(req, 0, sizeof(*req));
358 EXPORT_SYMBOL_GPL(pm_qos_remove_request);
361 * pm_qos_add_notifier - sets notification entry for changes to target value
362 * @pm_qos_class: identifies which qos target changes should be notified.
363 * @notifier: notifier block managed by caller.
365 * will register the notifier into a notification chain that gets called
366 * upon changes to the pm_qos_class target value.
368 int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
372 retval = blocking_notifier_chain_register(
373 pm_qos_array[pm_qos_class]->constraints->notifiers,
378 EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
381 * pm_qos_remove_notifier - deletes notification entry from chain.
382 * @pm_qos_class: identifies which qos target changes are notified.
383 * @notifier: notifier block to be removed.
385 * will remove the notifier from the notification chain that gets called
386 * upon changes to the pm_qos_class target value.
388 int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
392 retval = blocking_notifier_chain_unregister(
393 pm_qos_array[pm_qos_class]->constraints->notifiers,
398 EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
400 /* User space interface to global PM QoS via misc device. */
402 static int pm_qos_power_open(struct inode *inode, struct file *filp)
404 struct pm_qos_request *req;
406 req = kzalloc(sizeof(*req), GFP_KERNEL);
410 pm_qos_add_request(req, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
411 filp->private_data = req;
416 static int pm_qos_power_release(struct inode *inode, struct file *filp)
418 struct pm_qos_request *req = filp->private_data;
420 filp->private_data = NULL;
422 pm_qos_remove_request(req);
428 static ssize_t pm_qos_power_read(struct file *filp, char __user *buf,
429 size_t count, loff_t *f_pos)
431 struct pm_qos_request *req = filp->private_data;
435 if (!req || !pm_qos_request_active(req))
438 spin_lock_irqsave(&pm_qos_lock, flags);
439 value = pm_qos_get_value(pm_qos_array[req->pm_qos_class]->constraints);
440 spin_unlock_irqrestore(&pm_qos_lock, flags);
442 return simple_read_from_buffer(buf, count, f_pos, &value, sizeof(s32));
445 static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
446 size_t count, loff_t *f_pos)
450 if (count == sizeof(s32)) {
451 if (copy_from_user(&value, buf, sizeof(s32)))
456 ret = kstrtos32_from_user(buf, count, 16, &value);
461 pm_qos_update_request(filp->private_data, value);
466 static const struct file_operations pm_qos_power_fops = {
467 .write = pm_qos_power_write,
468 .read = pm_qos_power_read,
469 .open = pm_qos_power_open,
470 .release = pm_qos_power_release,
471 .llseek = noop_llseek,
474 static int register_pm_qos_misc(struct pm_qos_object *qos)
476 qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR;
477 qos->pm_qos_power_miscdev.name = qos->name;
478 qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops;
480 return misc_register(&qos->pm_qos_power_miscdev);
483 static int __init pm_qos_power_init(void)
487 BUILD_BUG_ON(ARRAY_SIZE(pm_qos_array) != PM_QOS_NUM_CLASSES);
489 ret = register_pm_qos_misc(pm_qos_array[PM_QOS_CPU_DMA_LATENCY]);
491 pr_err("%s: %s setup failed\n", __func__,
492 pm_qos_array[PM_QOS_CPU_DMA_LATENCY]->name);
496 late_initcall(pm_qos_power_init);
498 /* Definitions related to the frequency QoS below. */
501 * freq_constraints_init - Initialize frequency QoS constraints.
502 * @qos: Frequency QoS constraints to initialize.
504 void freq_constraints_init(struct freq_constraints *qos)
506 struct pm_qos_constraints *c;
509 plist_head_init(&c->list);
510 c->target_value = FREQ_QOS_MIN_DEFAULT_VALUE;
511 c->default_value = FREQ_QOS_MIN_DEFAULT_VALUE;
512 c->no_constraint_value = FREQ_QOS_MIN_DEFAULT_VALUE;
513 c->type = PM_QOS_MAX;
514 c->notifiers = &qos->min_freq_notifiers;
515 BLOCKING_INIT_NOTIFIER_HEAD(c->notifiers);
518 plist_head_init(&c->list);
519 c->target_value = FREQ_QOS_MAX_DEFAULT_VALUE;
520 c->default_value = FREQ_QOS_MAX_DEFAULT_VALUE;
521 c->no_constraint_value = FREQ_QOS_MAX_DEFAULT_VALUE;
522 c->type = PM_QOS_MIN;
523 c->notifiers = &qos->max_freq_notifiers;
524 BLOCKING_INIT_NOTIFIER_HEAD(c->notifiers);
528 * freq_qos_read_value - Get frequency QoS constraint for a given list.
529 * @qos: Constraints to evaluate.
530 * @type: QoS request type.
532 s32 freq_qos_read_value(struct freq_constraints *qos,
533 enum freq_qos_req_type type)
539 ret = IS_ERR_OR_NULL(qos) ?
540 FREQ_QOS_MIN_DEFAULT_VALUE :
541 pm_qos_read_value(&qos->min_freq);
544 ret = IS_ERR_OR_NULL(qos) ?
545 FREQ_QOS_MAX_DEFAULT_VALUE :
546 pm_qos_read_value(&qos->max_freq);
557 * freq_qos_apply - Add/modify/remove frequency QoS request.
558 * @req: Constraint request to apply.
559 * @action: Action to perform (add/update/remove).
560 * @value: Value to assign to the QoS request.
562 * This is only meant to be called from inside pm_qos, not drivers.
564 int freq_qos_apply(struct freq_qos_request *req,
565 enum pm_qos_req_action action, s32 value)
571 ret = pm_qos_update_target(&req->qos->min_freq, &req->pnode,
575 ret = pm_qos_update_target(&req->qos->max_freq, &req->pnode,
586 * freq_qos_add_request - Insert new frequency QoS request into a given list.
587 * @qos: Constraints to update.
588 * @req: Preallocated request object.
589 * @type: Request type.
590 * @value: Request value.
592 * Insert a new entry into the @qos list of requests, recompute the effective
593 * QoS constraint value for that list and initialize the @req object. The
594 * caller needs to save that object for later use in updates and removal.
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_add_request(struct freq_constraints *qos,
600 struct freq_qos_request *req,
601 enum freq_qos_req_type type, s32 value)
605 if (IS_ERR_OR_NULL(qos) || !req)
608 if (WARN(freq_qos_request_active(req),
609 "%s() called for active request\n", __func__))
614 ret = freq_qos_apply(req, PM_QOS_ADD_REQ, value);
622 EXPORT_SYMBOL_GPL(freq_qos_add_request);
625 * freq_qos_update_request - Modify existing frequency QoS request.
626 * @req: Request to modify.
627 * @new_value: New request value.
629 * Update an existing frequency QoS request along with the effective constraint
630 * value for the list of requests it belongs to.
632 * Return 1 if the effective constraint value has changed, 0 if the effective
633 * constraint value has not changed, or a negative error code on failures.
635 int freq_qos_update_request(struct freq_qos_request *req, s32 new_value)
640 if (WARN(!freq_qos_request_active(req),
641 "%s() called for unknown object\n", __func__))
644 if (req->pnode.prio == new_value)
647 return freq_qos_apply(req, PM_QOS_UPDATE_REQ, new_value);
649 EXPORT_SYMBOL_GPL(freq_qos_update_request);
652 * freq_qos_remove_request - Remove frequency QoS request from its list.
653 * @req: Request to remove.
655 * Remove the given frequency QoS request from the list of constraints it
656 * belongs to and recompute the effective constraint value for that list.
658 * Return 1 if the effective constraint value has changed, 0 if the effective
659 * constraint value has not changed, or a negative error code on failures.
661 int freq_qos_remove_request(struct freq_qos_request *req)
668 if (WARN(!freq_qos_request_active(req),
669 "%s() called for unknown object\n", __func__))
672 ret = freq_qos_apply(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
678 EXPORT_SYMBOL_GPL(freq_qos_remove_request);
681 * freq_qos_add_notifier - Add frequency QoS change notifier.
682 * @qos: List of requests to add the notifier to.
683 * @type: Request type.
684 * @notifier: Notifier block to add.
686 int freq_qos_add_notifier(struct freq_constraints *qos,
687 enum freq_qos_req_type type,
688 struct notifier_block *notifier)
692 if (IS_ERR_OR_NULL(qos) || !notifier)
697 ret = blocking_notifier_chain_register(qos->min_freq.notifiers,
701 ret = blocking_notifier_chain_register(qos->max_freq.notifiers,
711 EXPORT_SYMBOL_GPL(freq_qos_add_notifier);
714 * freq_qos_remove_notifier - Remove frequency QoS change notifier.
715 * @qos: List of requests to remove the notifier from.
716 * @type: Request type.
717 * @notifier: Notifier block to remove.
719 int freq_qos_remove_notifier(struct freq_constraints *qos,
720 enum freq_qos_req_type type,
721 struct notifier_block *notifier)
725 if (IS_ERR_OR_NULL(qos) || !notifier)
730 ret = blocking_notifier_chain_unregister(qos->min_freq.notifiers,
734 ret = blocking_notifier_chain_unregister(qos->max_freq.notifiers,
744 EXPORT_SYMBOL_GPL(freq_qos_remove_notifier);