a6bf53e9db17727dbdd1245461b20d99bf34a4e1
[linux-2.6-microblaze.git] / kernel / power / qos.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * This module exposes the interface to kernel space for specifying
4  * QoS dependencies.  It provides infrastructure for registration of:
5  *
6  * Dependents on a QoS value : register requests
7  * 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  *
18  * There are lists of pm_qos_objects each one wrapping requests, notifiers
19  *
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.
27  *
28  * Mark Gross <mgross@linux.intel.com>
29  */
30
31 /*#define DEBUG*/
32
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>
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>
44 #include <linux/kernel.h>
45 #include <linux/debugfs.h>
46 #include <linux/seq_file.h>
47
48 #include <linux/uaccess.h>
49 #include <linux/export.h>
50 #include <trace/events/power.h>
51
52 /*
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
56  */
57 static DEFINE_SPINLOCK(pm_qos_lock);
58
59 /**
60  * pm_qos_read_value - Return the current effective constraint value.
61  * @c: List of PM QoS constraint requests.
62  */
63 s32 pm_qos_read_value(struct pm_qos_constraints *c)
64 {
65         return c->target_value;
66 }
67
68 static int pm_qos_get_value(struct pm_qos_constraints *c)
69 {
70         if (plist_head_empty(&c->list))
71                 return c->no_constraint_value;
72
73         switch (c->type) {
74         case PM_QOS_MIN:
75                 return plist_first(&c->list)->prio;
76
77         case PM_QOS_MAX:
78                 return plist_last(&c->list)->prio;
79
80         default:
81                 WARN(1, "Unknown PM QoS type in %s\n", __func__);
82                 return PM_QOS_DEFAULT_VALUE;
83         }
84 }
85
86 static void pm_qos_set_value(struct pm_qos_constraints *c, s32 value)
87 {
88         c->target_value = value;
89 }
90
91 /**
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.
97  *
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.
100  *
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).
105  *
106  * Return: 1 if the aggregate constraint value has changed, 0  otherwise.
107  */
108 int pm_qos_update_target(struct pm_qos_constraints *c, struct plist_node *node,
109                          enum pm_qos_req_action action, int value)
110 {
111         int prev_value, curr_value, new_value;
112         unsigned long flags;
113
114         spin_lock_irqsave(&pm_qos_lock, flags);
115
116         prev_value = pm_qos_get_value(c);
117         if (value == PM_QOS_DEFAULT_VALUE)
118                 new_value = c->default_value;
119         else
120                 new_value = value;
121
122         switch (action) {
123         case PM_QOS_REMOVE_REQ:
124                 plist_del(node, &c->list);
125                 break;
126         case PM_QOS_UPDATE_REQ:
127                 /*
128                  * To change the list, atomically remove, reinit with new value
129                  * and add, then see if the aggregate has changed.
130                  */
131                 plist_del(node, &c->list);
132                 /* fall through */
133         case PM_QOS_ADD_REQ:
134                 plist_node_init(node, new_value);
135                 plist_add(node, &c->list);
136                 break;
137         default:
138                 /* no action */
139                 ;
140         }
141
142         curr_value = pm_qos_get_value(c);
143         pm_qos_set_value(c, curr_value);
144
145         spin_unlock_irqrestore(&pm_qos_lock, flags);
146
147         trace_pm_qos_update_target(action, prev_value, curr_value);
148
149         if (prev_value == curr_value)
150                 return 0;
151
152         if (c->notifiers)
153                 blocking_notifier_call_chain(c->notifiers, curr_value, NULL);
154
155         return 1;
156 }
157
158 /**
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.
162  */
163 static void pm_qos_flags_remove_req(struct pm_qos_flags *pqf,
164                                     struct pm_qos_flags_request *req)
165 {
166         s32 val = 0;
167
168         list_del(&req->node);
169         list_for_each_entry(req, &pqf->list, node)
170                 val |= req->flags;
171
172         pqf->effective_flags = val;
173 }
174
175 /**
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.
181  *
182  * Return: 1 if the aggregate constraint value has changed, 0 otherwise.
183  */
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)
187 {
188         unsigned long irqflags;
189         s32 prev_value, curr_value;
190
191         spin_lock_irqsave(&pm_qos_lock, irqflags);
192
193         prev_value = list_empty(&pqf->list) ? 0 : pqf->effective_flags;
194
195         switch (action) {
196         case PM_QOS_REMOVE_REQ:
197                 pm_qos_flags_remove_req(pqf, req);
198                 break;
199         case PM_QOS_UPDATE_REQ:
200                 pm_qos_flags_remove_req(pqf, req);
201                 /* fall through */
202         case PM_QOS_ADD_REQ:
203                 req->flags = val;
204                 INIT_LIST_HEAD(&req->node);
205                 list_add_tail(&req->node, &pqf->list);
206                 pqf->effective_flags |= val;
207                 break;
208         default:
209                 /* no action */
210                 ;
211         }
212
213         curr_value = list_empty(&pqf->list) ? 0 : pqf->effective_flags;
214
215         spin_unlock_irqrestore(&pm_qos_lock, irqflags);
216
217         trace_pm_qos_update_flags(action, prev_value, curr_value);
218
219         return prev_value != curr_value;
220 }
221
222 /* Definitions related to the CPU latency QoS. */
223
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,
229         .type = PM_QOS_MIN,
230 };
231
232 /**
233  * pm_qos_request - returns current system wide qos expectation
234  * @pm_qos_class: Ignored.
235  *
236  * This function returns the current target value.
237  */
238 int pm_qos_request(int pm_qos_class)
239 {
240         return pm_qos_read_value(&cpu_latency_constraints);
241 }
242 EXPORT_SYMBOL_GPL(pm_qos_request);
243
244 int pm_qos_request_active(struct pm_qos_request *req)
245 {
246         return req->qos == &cpu_latency_constraints;
247 }
248 EXPORT_SYMBOL_GPL(pm_qos_request_active);
249
250 static void cpu_latency_qos_update(struct pm_qos_request *req,
251                                    enum pm_qos_req_action action, s32 value)
252 {
253         int ret = pm_qos_update_target(req->qos, &req->node, action, value);
254         if (ret > 0)
255                 wake_up_all_idle_cpus();
256 }
257
258 /**
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
263  *
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
268  * removal.
269  */
270 void pm_qos_add_request(struct pm_qos_request *req,
271                         int pm_qos_class, s32 value)
272 {
273         if (!req) /*guard against callers passing in null */
274                 return;
275
276         if (pm_qos_request_active(req)) {
277                 WARN(1, KERN_ERR "pm_qos_add_request() called for already added request\n");
278                 return;
279         }
280
281         trace_pm_qos_add_request(PM_QOS_CPU_DMA_LATENCY, value);
282
283         req->qos = &cpu_latency_constraints;
284         cpu_latency_qos_update(req, PM_QOS_ADD_REQ, value);
285 }
286 EXPORT_SYMBOL_GPL(pm_qos_add_request);
287
288 /**
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
292  *
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.
295  *
296  * Attempts are made to make this code callable on hot code paths.
297  */
298 void pm_qos_update_request(struct pm_qos_request *req, s32 new_value)
299 {
300         if (!req) /*guard against callers passing in null */
301                 return;
302
303         if (!pm_qos_request_active(req)) {
304                 WARN(1, KERN_ERR "pm_qos_update_request() called for unknown object\n");
305                 return;
306         }
307
308         trace_pm_qos_update_request(PM_QOS_CPU_DMA_LATENCY, new_value);
309
310         if (new_value == req->node.prio)
311                 return;
312
313         cpu_latency_qos_update(req, PM_QOS_UPDATE_REQ, new_value);
314 }
315 EXPORT_SYMBOL_GPL(pm_qos_update_request);
316
317 /**
318  * pm_qos_remove_request - modifies an existing qos request
319  * @req: handle to request list element
320  *
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.
324  */
325 void pm_qos_remove_request(struct pm_qos_request *req)
326 {
327         if (!req) /*guard against callers passing in null */
328                 return;
329                 /* silent return to keep pcm code cleaner */
330
331         if (!pm_qos_request_active(req)) {
332                 WARN(1, KERN_ERR "pm_qos_remove_request() called for unknown object\n");
333                 return;
334         }
335
336         trace_pm_qos_remove_request(PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
337
338         cpu_latency_qos_update(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
339         memset(req, 0, sizeof(*req));
340 }
341 EXPORT_SYMBOL_GPL(pm_qos_remove_request);
342
343 /* User space interface to the CPU latency QoS via misc device. */
344
345 static int cpu_latency_qos_open(struct inode *inode, struct file *filp)
346 {
347         struct pm_qos_request *req;
348
349         req = kzalloc(sizeof(*req), GFP_KERNEL);
350         if (!req)
351                 return -ENOMEM;
352
353         pm_qos_add_request(req, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
354         filp->private_data = req;
355
356         return 0;
357 }
358
359 static int cpu_latency_qos_release(struct inode *inode, struct file *filp)
360 {
361         struct pm_qos_request *req = filp->private_data;
362
363         filp->private_data = NULL;
364
365         pm_qos_remove_request(req);
366         kfree(req);
367
368         return 0;
369 }
370
371 static ssize_t cpu_latency_qos_read(struct file *filp, char __user *buf,
372                                     size_t count, loff_t *f_pos)
373 {
374         struct pm_qos_request *req = filp->private_data;
375         unsigned long flags;
376         s32 value;
377
378         if (!req || !pm_qos_request_active(req))
379                 return -EINVAL;
380
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);
384
385         return simple_read_from_buffer(buf, count, f_pos, &value, sizeof(s32));
386 }
387
388 static ssize_t cpu_latency_qos_write(struct file *filp, const char __user *buf,
389                                      size_t count, loff_t *f_pos)
390 {
391         s32 value;
392
393         if (count == sizeof(s32)) {
394                 if (copy_from_user(&value, buf, sizeof(s32)))
395                         return -EFAULT;
396         } else {
397                 int ret;
398
399                 ret = kstrtos32_from_user(buf, count, 16, &value);
400                 if (ret)
401                         return ret;
402         }
403
404         pm_qos_update_request(filp->private_data, value);
405
406         return count;
407 }
408
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,
415 };
416
417 static struct miscdevice cpu_latency_qos_miscdev = {
418         .minor = MISC_DYNAMIC_MINOR,
419         .name = "cpu_dma_latency",
420         .fops = &cpu_latency_qos_fops,
421 };
422
423 static int __init cpu_latency_qos_init(void)
424 {
425         int ret;
426
427         ret = misc_register(&cpu_latency_qos_miscdev);
428         if (ret < 0)
429                 pr_err("%s: %s setup failed\n", __func__,
430                        cpu_latency_qos_miscdev.name);
431
432         return ret;
433 }
434 late_initcall(cpu_latency_qos_init);
435
436 /* Definitions related to the frequency QoS below. */
437
438 /**
439  * freq_constraints_init - Initialize frequency QoS constraints.
440  * @qos: Frequency QoS constraints to initialize.
441  */
442 void freq_constraints_init(struct freq_constraints *qos)
443 {
444         struct pm_qos_constraints *c;
445
446         c = &qos->min_freq;
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);
454
455         c = &qos->max_freq;
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);
463 }
464
465 /**
466  * freq_qos_read_value - Get frequency QoS constraint for a given list.
467  * @qos: Constraints to evaluate.
468  * @type: QoS request type.
469  */
470 s32 freq_qos_read_value(struct freq_constraints *qos,
471                         enum freq_qos_req_type type)
472 {
473         s32 ret;
474
475         switch (type) {
476         case FREQ_QOS_MIN:
477                 ret = IS_ERR_OR_NULL(qos) ?
478                         FREQ_QOS_MIN_DEFAULT_VALUE :
479                         pm_qos_read_value(&qos->min_freq);
480                 break;
481         case FREQ_QOS_MAX:
482                 ret = IS_ERR_OR_NULL(qos) ?
483                         FREQ_QOS_MAX_DEFAULT_VALUE :
484                         pm_qos_read_value(&qos->max_freq);
485                 break;
486         default:
487                 WARN_ON(1);
488                 ret = 0;
489         }
490
491         return ret;
492 }
493
494 /**
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.
499  *
500  * This is only meant to be called from inside pm_qos, not drivers.
501  */
502 int freq_qos_apply(struct freq_qos_request *req,
503                           enum pm_qos_req_action action, s32 value)
504 {
505         int ret;
506
507         switch(req->type) {
508         case FREQ_QOS_MIN:
509                 ret = pm_qos_update_target(&req->qos->min_freq, &req->pnode,
510                                            action, value);
511                 break;
512         case FREQ_QOS_MAX:
513                 ret = pm_qos_update_target(&req->qos->max_freq, &req->pnode,
514                                            action, value);
515                 break;
516         default:
517                 ret = -EINVAL;
518         }
519
520         return ret;
521 }
522
523 /**
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.
529  *
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.
533  *
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.
536  */
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)
540 {
541         int ret;
542
543         if (IS_ERR_OR_NULL(qos) || !req)
544                 return -EINVAL;
545
546         if (WARN(freq_qos_request_active(req),
547                  "%s() called for active request\n", __func__))
548                 return -EINVAL;
549
550         req->qos = qos;
551         req->type = type;
552         ret = freq_qos_apply(req, PM_QOS_ADD_REQ, value);
553         if (ret < 0) {
554                 req->qos = NULL;
555                 req->type = 0;
556         }
557
558         return ret;
559 }
560 EXPORT_SYMBOL_GPL(freq_qos_add_request);
561
562 /**
563  * freq_qos_update_request - Modify existing frequency QoS request.
564  * @req: Request to modify.
565  * @new_value: New request value.
566  *
567  * Update an existing frequency QoS request along with the effective constraint
568  * value for the list of requests it belongs to.
569  *
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.
572  */
573 int freq_qos_update_request(struct freq_qos_request *req, s32 new_value)
574 {
575         if (!req)
576                 return -EINVAL;
577
578         if (WARN(!freq_qos_request_active(req),
579                  "%s() called for unknown object\n", __func__))
580                 return -EINVAL;
581
582         if (req->pnode.prio == new_value)
583                 return 0;
584
585         return freq_qos_apply(req, PM_QOS_UPDATE_REQ, new_value);
586 }
587 EXPORT_SYMBOL_GPL(freq_qos_update_request);
588
589 /**
590  * freq_qos_remove_request - Remove frequency QoS request from its list.
591  * @req: Request to remove.
592  *
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.
595  *
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.
598  */
599 int freq_qos_remove_request(struct freq_qos_request *req)
600 {
601         int ret;
602
603         if (!req)
604                 return -EINVAL;
605
606         if (WARN(!freq_qos_request_active(req),
607                  "%s() called for unknown object\n", __func__))
608                 return -EINVAL;
609
610         ret = freq_qos_apply(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
611         req->qos = NULL;
612         req->type = 0;
613
614         return ret;
615 }
616 EXPORT_SYMBOL_GPL(freq_qos_remove_request);
617
618 /**
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.
623  */
624 int freq_qos_add_notifier(struct freq_constraints *qos,
625                           enum freq_qos_req_type type,
626                           struct notifier_block *notifier)
627 {
628         int ret;
629
630         if (IS_ERR_OR_NULL(qos) || !notifier)
631                 return -EINVAL;
632
633         switch (type) {
634         case FREQ_QOS_MIN:
635                 ret = blocking_notifier_chain_register(qos->min_freq.notifiers,
636                                                        notifier);
637                 break;
638         case FREQ_QOS_MAX:
639                 ret = blocking_notifier_chain_register(qos->max_freq.notifiers,
640                                                        notifier);
641                 break;
642         default:
643                 WARN_ON(1);
644                 ret = -EINVAL;
645         }
646
647         return ret;
648 }
649 EXPORT_SYMBOL_GPL(freq_qos_add_notifier);
650
651 /**
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.
656  */
657 int freq_qos_remove_notifier(struct freq_constraints *qos,
658                              enum freq_qos_req_type type,
659                              struct notifier_block *notifier)
660 {
661         int ret;
662
663         if (IS_ERR_OR_NULL(qos) || !notifier)
664                 return -EINVAL;
665
666         switch (type) {
667         case FREQ_QOS_MIN:
668                 ret = blocking_notifier_chain_unregister(qos->min_freq.notifiers,
669                                                          notifier);
670                 break;
671         case FREQ_QOS_MAX:
672                 ret = blocking_notifier_chain_unregister(qos->max_freq.notifiers,
673                                                          notifier);
674                 break;
675         default:
676                 WARN_ON(1);
677                 ret = -EINVAL;
678         }
679
680         return ret;
681 }
682 EXPORT_SYMBOL_GPL(freq_qos_remove_notifier);