blob: 3092aeed716acd44ecb03ed84638b9e5edd2a919 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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#if 0
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 */
57struct pm_qos_object {
58 struct pm_qos_constraints *constraints;
59 struct miscdevice pm_qos_power_miscdev;
60 char *name;
61};
62#endif
63
64static DEFINE_SPINLOCK(pm_qos_lock);
65
66static struct pm_qos_object null_pm_qos;
67
68static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
69static struct pm_qos_constraints cpu_dma_constraints = {
70 .list = PLIST_HEAD_INIT(cpu_dma_constraints.list),
71 .target_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE,
72 .default_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE,
73 .no_constraint_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE,
74 .type = PM_QOS_MIN,
75 .notifiers = &cpu_dma_lat_notifier,
76};
77static struct pm_qos_object cpu_dma_pm_qos = {
78 .constraints = &cpu_dma_constraints,
79 .name = "cpu_dma_latency",
80};
81
82static BLOCKING_NOTIFIER_HEAD(cpu_num_min_notifier);
83static struct pm_qos_constraints cpu_num_min_constraints = {
84 .list = PLIST_HEAD_INIT(cpu_num_min_constraints.list),
85 .notifiers = &cpu_num_min_notifier,
86 .default_value = PM_QOS_DEFAULT_VALUE,
87 .target_value = PM_QOS_DEFAULT_VALUE,
88 .no_constraint_value = PM_QOS_DEFAULT_VALUE,
89 .type = PM_QOS_MAX,
90};
91
92static struct pm_qos_object cpu_num_min_pm_qos = {
93 .constraints = &cpu_num_min_constraints,
94 .name = "cpu_num_min",
95};
96
97static BLOCKING_NOTIFIER_HEAD(cpu_num_max_notifier);
98static struct pm_qos_constraints cpu_num_max_constraints = {
99 .list = PLIST_HEAD_INIT(cpu_num_max_constraints.list),
100 .notifiers = &cpu_num_max_notifier,
101 .default_value = INT_MAX,
102 .target_value = PM_QOS_DEFAULT_VALUE,
103 .no_constraint_value = PM_QOS_DEFAULT_VALUE,
104 .type = PM_QOS_MIN,
105};
106
107static struct pm_qos_object cpu_num_max_pm_qos = {
108 .constraints = &cpu_num_max_constraints,
109 .name = "cpu_num_max",
110};
111
112static BLOCKING_NOTIFIER_HEAD(cpu_freq_min_notifier);
113static struct pm_qos_constraints cpu_freq_min_constraints = {
114 .list = PLIST_HEAD_INIT(cpu_freq_min_constraints.list),
115 .notifiers = &cpu_freq_min_notifier,
116 .default_value = PM_QOS_DEFAULT_VALUE,
117 .target_value = PM_QOS_DEFAULT_VALUE,
118 .no_constraint_value = PM_QOS_DEFAULT_VALUE,
119 .type = PM_QOS_MAX,
120};
121
122static struct pm_qos_object cpu_freq_min_pm_qos = {
123 .constraints = &cpu_freq_min_constraints,
124 .name = "cpu_freq_min",
125};
126
127static BLOCKING_NOTIFIER_HEAD(cpu_freq_max_notifier);
128static struct pm_qos_constraints cpu_freq_max_constraints = {
129 .list = PLIST_HEAD_INIT(cpu_freq_max_constraints.list),
130 .notifiers = &cpu_freq_max_notifier,
131 .default_value = INT_MAX,
132 .target_value = PM_QOS_DEFAULT_VALUE,
133 .no_constraint_value = PM_QOS_DEFAULT_VALUE,
134 .type = PM_QOS_MIN,
135};
136
137static struct pm_qos_object cpu_freq_max_pm_qos = {
138 .constraints = &cpu_freq_max_constraints,
139 .name = "cpu_freq_max",
140};
141
142static BLOCKING_NOTIFIER_HEAD(ddr_devfreq_min_notifier);
143static struct pm_qos_constraints ddr_devfreq_min_constraints = {
144 .list = PLIST_HEAD_INIT(ddr_devfreq_min_constraints.list),
145 .target_value = PM_QOS_DEFAULT_VALUE,
146 .default_value = 0,
147 .no_constraint_value = PM_QOS_DEFAULT_VALUE,
148 .type = PM_QOS_MAX,
149 .notifiers = &ddr_devfreq_min_notifier,
150};
151static struct pm_qos_object ddr_devfreq_min_pm_qos = {
152 .constraints = &ddr_devfreq_min_constraints,
153 .name = "ddr_devfreq_min",
154};
155
156static BLOCKING_NOTIFIER_HEAD(ddr_devfreq_max_notifier);
157static struct pm_qos_constraints ddr_devfreq_max_constraints = {
158 .list = PLIST_HEAD_INIT(ddr_devfreq_max_constraints.list),
159 .target_value = PM_QOS_DEFAULT_VALUE,
160 .default_value = INT_MAX,
161 .no_constraint_value = PM_QOS_DEFAULT_VALUE,
162 .type = PM_QOS_MIN,
163 .notifiers = &ddr_devfreq_max_notifier,
164};
165static struct pm_qos_object ddr_devfreq_max_pm_qos = {
166 .constraints = &ddr_devfreq_max_constraints,
167 .name = "ddr_devfreq_max",
168};
169
170
171
172static BLOCKING_NOTIFIER_HEAD(axi_min_notifier);
173static struct pm_qos_constraints axi_min_constraints = {
174 .list = PLIST_HEAD_INIT(axi_min_constraints.list),
175 .notifiers = &axi_min_notifier,
176 .default_value = PM_QOS_DEFAULT_VALUE,
177 .target_value = PM_QOS_DEFAULT_VALUE,
178 .no_constraint_value = PM_QOS_DEFAULT_VALUE,
179 .type = PM_QOS_MAX,
180};
181
182static struct pm_qos_object axi_min_pm_qos = {
183 .constraints = &axi_min_constraints,
184 .name = "axi_min",
185};
186
187static BLOCKING_NOTIFIER_HEAD(cpuidle_block_notifier);
188static struct pm_qos_constraints cpuidle_block_constraints = {
189 .list = PLIST_HEAD_INIT(cpuidle_block_constraints.list),
190 .target_value = PM_QOS_CPUIDLE_BLOCK_DEFAULT_VALUE,
191 .default_value = PM_QOS_CPUIDLE_BLOCK_DEFAULT_VALUE,
192 .no_constraint_value = PM_QOS_CPUIDLE_BLOCK_DEFAULT_VALUE,
193 .type = PM_QOS_MIN,
194 .notifiers = &cpuidle_block_notifier,
195};
196static struct pm_qos_object cpuidle_block_pm_qos = {
197 .constraints = &cpuidle_block_constraints,
198 .name = "cpuidle_block",
199};
200
201struct pm_qos_object *pm_qos_array[] = {
202 &null_pm_qos,
203 &cpu_dma_pm_qos,
204 &cpu_freq_min_pm_qos,
205 &cpu_freq_max_pm_qos,
206 &ddr_devfreq_min_pm_qos,
207 &ddr_devfreq_max_pm_qos,
208 &axi_min_pm_qos,
209 &cpuidle_block_pm_qos,
210 &cpu_num_min_pm_qos,
211 &cpu_num_max_pm_qos,
212};
213
214static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
215 size_t count, loff_t *f_pos);
216static ssize_t pm_qos_power_read(struct file *filp, char __user *buf,
217 size_t count, loff_t *f_pos);
218static int pm_qos_power_open(struct inode *inode, struct file *filp);
219static int pm_qos_power_release(struct inode *inode, struct file *filp);
220
221static const struct file_operations pm_qos_power_fops = {
222 .write = pm_qos_power_write,
223 .read = pm_qos_power_read,
224 .open = pm_qos_power_open,
225 .release = pm_qos_power_release,
226 .llseek = noop_llseek,
227};
228
229/* unlocked internal variant */
230static inline int pm_qos_get_value(struct pm_qos_constraints *c)
231{
232 struct plist_node *node;
233 int total_value = 0;
234
235 if (plist_head_empty(&c->list))
236 return c->no_constraint_value;
237
238 switch (c->type) {
239 case PM_QOS_MIN:
240 return plist_first(&c->list)->prio;
241
242 case PM_QOS_MAX:
243 return plist_last(&c->list)->prio;
244
245 case PM_QOS_SUM:
246 plist_for_each(node, &c->list)
247 total_value += node->prio;
248
249 return total_value;
250
251 default:
252 /* runtime check for not using enum */
253 BUG();
254 return PM_QOS_DEFAULT_VALUE;
255 }
256}
257
258s32 pm_qos_read_value(struct pm_qos_constraints *c)
259{
260 return c->target_value;
261}
262
263static inline void pm_qos_set_value(struct pm_qos_constraints *c, s32 value)
264{
265 c->target_value = value;
266}
267
268static int pm_qos_debug_show(struct seq_file *s, void *unused)
269{
270 struct pm_qos_object *qos = (struct pm_qos_object *)s->private;
271 struct pm_qos_constraints *c;
272 struct pm_qos_request *req;
273 char *type;
274 unsigned long flags;
275 int tot_reqs = 0;
276 int active_reqs = 0;
277
278 if (IS_ERR_OR_NULL(qos)) {
279 pr_err("%s: bad qos param!\n", __func__);
280 return -EINVAL;
281 }
282 c = qos->constraints;
283 if (IS_ERR_OR_NULL(c)) {
284 pr_err("%s: Bad constraints on qos?\n", __func__);
285 return -EINVAL;
286 }
287
288 /* Lock to ensure we have a snapshot */
289 spin_lock_irqsave(&pm_qos_lock, flags);
290 if (plist_head_empty(&c->list)) {
291 seq_puts(s, "Empty!\n");
292 goto out;
293 }
294
295 switch (c->type) {
296 case PM_QOS_MIN:
297 type = "Minimum";
298 break;
299 case PM_QOS_MAX:
300 type = "Maximum";
301 break;
302 case PM_QOS_SUM:
303 type = "Sum";
304 break;
305 default:
306 type = "Unknown";
307 }
308
309 plist_for_each_entry(req, &c->list, node) {
310 char *state = "Default";
311
312 if ((req->node).prio != c->default_value) {
313 active_reqs++;
314 state = "Active";
315 }
316 tot_reqs++;
317 seq_printf(s, "%d: %d: %s\n", tot_reqs,
318 (req->node).prio, state);
319 }
320
321 seq_printf(s, "Type=%s, Value=%d, Requests: active=%d / total=%d\n",
322 type, pm_qos_get_value(c), active_reqs, tot_reqs);
323
324out:
325 spin_unlock_irqrestore(&pm_qos_lock, flags);
326 return 0;
327}
328
329DEFINE_SHOW_ATTRIBUTE(pm_qos_debug);
330
331/**
332 * pm_qos_update_target - manages the constraints list and calls the notifiers
333 * if needed
334 * @c: constraints data struct
335 * @node: request to add to the list, to update or to remove
336 * @action: action to take on the constraints list
337 * @value: value of the request to add or update
338 *
339 * This function returns 1 if the aggregated constraint value has changed, 0
340 * otherwise.
341 */
342int pm_qos_update_target(struct pm_qos_constraints *c, struct plist_node *node,
343 enum pm_qos_req_action action, int value)
344{
345 unsigned long flags;
346 int prev_value, curr_value, new_value;
347 int ret;
348
349 spin_lock_irqsave(&pm_qos_lock, flags);
350 prev_value = pm_qos_get_value(c);
351 if (value == PM_QOS_DEFAULT_VALUE)
352 new_value = c->default_value;
353 else
354 new_value = value;
355
356 switch (action) {
357 case PM_QOS_REMOVE_REQ:
358 plist_del(node, &c->list);
359 break;
360 case PM_QOS_UPDATE_REQ:
361 /*
362 * to change the list, we atomically remove, reinit
363 * with new value and add, then see if the extremal
364 * changed
365 */
366 plist_del(node, &c->list);
367 /* fall through */
368 case PM_QOS_ADD_REQ:
369 plist_node_init(node, new_value);
370 plist_add(node, &c->list);
371 break;
372 default:
373 /* no action */
374 ;
375 }
376
377 curr_value = pm_qos_get_value(c);
378 pm_qos_set_value(c, curr_value);
379
380 spin_unlock_irqrestore(&pm_qos_lock, flags);
381
382 trace_pm_qos_update_target(action, prev_value, curr_value);
383 if (prev_value != curr_value) {
384 ret = 1;
385 if (c->notifiers)
386 blocking_notifier_call_chain(c->notifiers,
387 (unsigned long)curr_value,
388 NULL);
389 } else {
390 ret = 0;
391 }
392 return ret;
393}
394
395/**
396 * pm_qos_flags_remove_req - Remove device PM QoS flags request.
397 * @pqf: Device PM QoS flags set to remove the request from.
398 * @req: Request to remove from the set.
399 */
400static void pm_qos_flags_remove_req(struct pm_qos_flags *pqf,
401 struct pm_qos_flags_request *req)
402{
403 s32 val = 0;
404
405 list_del(&req->node);
406 list_for_each_entry(req, &pqf->list, node)
407 val |= req->flags;
408
409 pqf->effective_flags = val;
410}
411
412/**
413 * pm_qos_update_flags - Update a set of PM QoS flags.
414 * @pqf: Set of flags to update.
415 * @req: Request to add to the set, to modify, or to remove from the set.
416 * @action: Action to take on the set.
417 * @val: Value of the request to add or modify.
418 *
419 * Update the given set of PM QoS flags and call notifiers if the aggregate
420 * value has changed. Returns 1 if the aggregate constraint value has changed,
421 * 0 otherwise.
422 */
423bool pm_qos_update_flags(struct pm_qos_flags *pqf,
424 struct pm_qos_flags_request *req,
425 enum pm_qos_req_action action, s32 val)
426{
427 unsigned long irqflags;
428 s32 prev_value, curr_value;
429
430 spin_lock_irqsave(&pm_qos_lock, irqflags);
431
432 prev_value = list_empty(&pqf->list) ? 0 : pqf->effective_flags;
433
434 switch (action) {
435 case PM_QOS_REMOVE_REQ:
436 pm_qos_flags_remove_req(pqf, req);
437 break;
438 case PM_QOS_UPDATE_REQ:
439 pm_qos_flags_remove_req(pqf, req);
440 /* fall through */
441 case PM_QOS_ADD_REQ:
442 req->flags = val;
443 INIT_LIST_HEAD(&req->node);
444 list_add_tail(&req->node, &pqf->list);
445 pqf->effective_flags |= val;
446 break;
447 default:
448 /* no action */
449 ;
450 }
451
452 curr_value = list_empty(&pqf->list) ? 0 : pqf->effective_flags;
453
454 spin_unlock_irqrestore(&pm_qos_lock, irqflags);
455
456 trace_pm_qos_update_flags(action, prev_value, curr_value);
457 return prev_value != curr_value;
458}
459
460/**
461 * pm_qos_request - returns current system wide qos expectation
462 * @pm_qos_class: identification of which qos value is requested
463 *
464 * This function returns the current target value.
465 */
466int pm_qos_request(int pm_qos_class)
467{
468 return pm_qos_read_value(pm_qos_array[pm_qos_class]->constraints);
469}
470EXPORT_SYMBOL_GPL(pm_qos_request);
471
472int pm_qos_request_active(struct pm_qos_request *req)
473{
474 return req->pm_qos_class != 0;
475}
476EXPORT_SYMBOL_GPL(pm_qos_request_active);
477
478static void __pm_qos_update_request(struct pm_qos_request *req,
479 s32 new_value)
480{
481 trace_pm_qos_update_request(req->pm_qos_class, new_value);
482
483 if (new_value != req->node.prio)
484 pm_qos_update_target(
485 pm_qos_array[req->pm_qos_class]->constraints,
486 &req->node, PM_QOS_UPDATE_REQ, new_value);
487}
488
489/**
490 * pm_qos_work_fn - the timeout handler of pm_qos_update_request_timeout
491 * @work: work struct for the delayed work (timeout)
492 *
493 * This cancels the timeout request by falling back to the default at timeout.
494 */
495static void pm_qos_work_fn(struct work_struct *work)
496{
497 struct pm_qos_request *req = container_of(to_delayed_work(work),
498 struct pm_qos_request,
499 work);
500
501 __pm_qos_update_request(req, PM_QOS_DEFAULT_VALUE);
502}
503
504/**
505 * pm_qos_add_request - inserts new qos request into the list
506 * @req: pointer to a preallocated handle
507 * @pm_qos_class: identifies which list of qos request to use
508 * @value: defines the qos request
509 *
510 * This function inserts a new entry in the pm_qos_class list of requested qos
511 * performance characteristics. It recomputes the aggregate QoS expectations
512 * for the pm_qos_class of parameters and initializes the pm_qos_request
513 * handle. Caller needs to save this handle for later use in updates and
514 * removal.
515 */
516
517void pm_qos_add_request(struct pm_qos_request *req,
518 int pm_qos_class, s32 value)
519{
520 if (!req) /*guard against callers passing in null */
521 return;
522
523 if (pm_qos_request_active(req)) {
524 WARN(1, KERN_ERR "pm_qos_add_request() called for already added request\n");
525 return;
526 }
527 req->pm_qos_class = pm_qos_class;
528 INIT_DELAYED_WORK(&req->work, pm_qos_work_fn);
529 trace_pm_qos_add_request(pm_qos_class, value);
530 pm_qos_update_target(pm_qos_array[pm_qos_class]->constraints,
531 &req->node, PM_QOS_ADD_REQ, value);
532}
533EXPORT_SYMBOL_GPL(pm_qos_add_request);
534
535/**
536 * pm_qos_update_request - modifies an existing qos request
537 * @req : handle to list element holding a pm_qos request to use
538 * @value: defines the qos request
539 *
540 * Updates an existing qos request for the pm_qos_class of parameters along
541 * with updating the target pm_qos_class value.
542 *
543 * Attempts are made to make this code callable on hot code paths.
544 */
545void pm_qos_update_request(struct pm_qos_request *req,
546 s32 new_value)
547{
548 if (!req) /*guard against callers passing in null */
549 return;
550
551 if (!pm_qos_request_active(req)) {
552 WARN(1, KERN_ERR "pm_qos_update_request() called for unknown object\n");
553 return;
554 }
555 /* note: add if check to avoid unneccesary timer cancelling */
556 if (delayed_work_pending(&req->work))
557 cancel_delayed_work_sync(&req->work);
558 __pm_qos_update_request(req, new_value);
559}
560EXPORT_SYMBOL_GPL(pm_qos_update_request);
561
562/**
563 * pm_qos_update_request_timeout - modifies an existing qos request temporarily.
564 * @req : handle to list element holding a pm_qos request to use
565 * @new_value: defines the temporal qos request
566 * @timeout_us: the effective duration of this qos request in usecs.
567 *
568 * After timeout_us, this qos request is cancelled automatically.
569 */
570void pm_qos_update_request_timeout(struct pm_qos_request *req, s32 new_value,
571 unsigned long timeout_us)
572{
573 if (!req)
574 return;
575 if (WARN(!pm_qos_request_active(req),
576 "%s called for unknown object.", __func__))
577 return;
578
579 cancel_delayed_work_sync(&req->work);
580
581 trace_pm_qos_update_request_timeout(req->pm_qos_class,
582 new_value, timeout_us);
583 if (new_value != req->node.prio)
584 pm_qos_update_target(
585 pm_qos_array[req->pm_qos_class]->constraints,
586 &req->node, PM_QOS_UPDATE_REQ, new_value);
587
588 schedule_delayed_work(&req->work, usecs_to_jiffies(timeout_us));
589}
590
591/**
592 * pm_qos_remove_request - modifies an existing qos request
593 * @req: handle to request list element
594 *
595 * Will remove pm qos request from the list of constraints and
596 * recompute the current target value for the pm_qos_class. Call this
597 * on slow code paths.
598 */
599void pm_qos_remove_request(struct pm_qos_request *req)
600{
601 if (!req) /*guard against callers passing in null */
602 return;
603 /* silent return to keep pcm code cleaner */
604
605 if (!pm_qos_request_active(req)) {
606 WARN(1, KERN_ERR "pm_qos_remove_request() called for unknown object\n");
607 return;
608 }
609
610 cancel_delayed_work_sync(&req->work);
611
612 trace_pm_qos_remove_request(req->pm_qos_class, PM_QOS_DEFAULT_VALUE);
613 pm_qos_update_target(pm_qos_array[req->pm_qos_class]->constraints,
614 &req->node, PM_QOS_REMOVE_REQ,
615 PM_QOS_DEFAULT_VALUE);
616 memset(req, 0, sizeof(*req));
617}
618EXPORT_SYMBOL_GPL(pm_qos_remove_request);
619
620/**
621 * pm_qos_add_notifier - sets notification entry for changes to target value
622 * @pm_qos_class: identifies which qos target changes should be notified.
623 * @notifier: notifier block managed by caller.
624 *
625 * will register the notifier into a notification chain that gets called
626 * upon changes to the pm_qos_class target value.
627 */
628int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
629{
630 int retval;
631
632 retval = blocking_notifier_chain_register(
633 pm_qos_array[pm_qos_class]->constraints->notifiers,
634 notifier);
635
636 return retval;
637}
638EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
639
640/**
641 * pm_qos_remove_notifier - deletes notification entry from chain.
642 * @pm_qos_class: identifies which qos target changes are notified.
643 * @notifier: notifier block to be removed.
644 *
645 * will remove the notifier from the notification chain that gets called
646 * upon changes to the pm_qos_class target value.
647 */
648int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
649{
650 int retval;
651
652 retval = blocking_notifier_chain_unregister(
653 pm_qos_array[pm_qos_class]->constraints->notifiers,
654 notifier);
655
656 return retval;
657}
658EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
659
660/* User space interface to PM QoS classes via misc devices */
661static int register_pm_qos_misc(struct pm_qos_object *qos, struct dentry *d)
662{
663 qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR;
664 qos->pm_qos_power_miscdev.name = qos->name;
665 qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops;
666
667 debugfs_create_file(qos->name, S_IRUGO, d, (void *)qos,
668 &pm_qos_debug_fops);
669
670 return misc_register(&qos->pm_qos_power_miscdev);
671}
672
673static int find_pm_qos_object_by_minor(int minor)
674{
675 int pm_qos_class;
676
677 for (pm_qos_class = PM_QOS_CPU_DMA_LATENCY;
678 pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) {
679 if (minor ==
680 pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor)
681 return pm_qos_class;
682 }
683 return -1;
684}
685
686static int pm_qos_power_open(struct inode *inode, struct file *filp)
687{
688 long pm_qos_class;
689
690 pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
691 if (pm_qos_class >= PM_QOS_CPU_DMA_LATENCY) {
692 struct pm_qos_request *req = kzalloc(sizeof(*req), GFP_KERNEL);
693 if (!req)
694 return -ENOMEM;
695
696 pm_qos_add_request(req, pm_qos_class, PM_QOS_DEFAULT_VALUE);
697 filp->private_data = req;
698
699 return 0;
700 }
701 return -EPERM;
702}
703
704static int pm_qos_power_release(struct inode *inode, struct file *filp)
705{
706 struct pm_qos_request *req;
707
708 req = filp->private_data;
709 pm_qos_remove_request(req);
710 kfree(req);
711
712 return 0;
713}
714
715
716static ssize_t pm_qos_power_read(struct file *filp, char __user *buf,
717 size_t count, loff_t *f_pos)
718{
719 s32 value;
720 unsigned long flags;
721 struct pm_qos_request *req = filp->private_data;
722
723 if (!req)
724 return -EINVAL;
725 if (!pm_qos_request_active(req))
726 return -EINVAL;
727
728 spin_lock_irqsave(&pm_qos_lock, flags);
729 value = pm_qos_get_value(pm_qos_array[req->pm_qos_class]->constraints);
730 spin_unlock_irqrestore(&pm_qos_lock, flags);
731
732 return simple_read_from_buffer(buf, count, f_pos, &value, sizeof(s32));
733}
734
735static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
736 size_t count, loff_t *f_pos)
737{
738 s32 value;
739 struct pm_qos_request *req;
740
741 if (count == sizeof(s32)) {
742 if (copy_from_user(&value, buf, sizeof(s32)))
743 return -EFAULT;
744 } else {
745 int ret;
746
747 ret = kstrtos32_from_user(buf, count, 16, &value);
748 if (ret)
749 return ret;
750 }
751
752 req = filp->private_data;
753 pm_qos_update_request(req, value);
754
755 return count;
756}
757
758static struct dentry *cpuidle_block_dentry;
759
760/**
761 * cpuidle_block_show - Print information of devices blocking LPM.
762 * @m: seq_file to print the statistics into.
763 */
764
765static int cpuidle_block_show(struct seq_file *m, void *unused)
766{
767 unsigned long flags;
768 struct pm_qos_object *o;
769 struct list_head *list;
770 struct plist_node *node;
771 struct pm_qos_request *req;
772 s32 target_value = PM_QOS_CPUIDLE_BLOCK_DEFAULT_VALUE;
773
774 o = pm_qos_array[PM_QOS_CPUIDLE_BLOCK];
775 list = &o->constraints->list.node_list;
776
777 rcu_read_lock();
778 spin_lock_irqsave(&pm_qos_lock, flags);
779
780 target_value = pm_qos_read_value(o->constraints);
781 if (target_value != PM_QOS_CPUIDLE_BLOCK_DEFAULT_VALUE) {
782 seq_printf(m, "Can't enter state equals to or deeper than state %d\n",
783 target_value);
784 seq_printf(m, "*****Blocking devices*****\n");
785 } else
786 seq_printf(m, "SOC can enter all states\n");
787
788 list_for_each_entry(node, list, node_list) {
789 req = container_of(node, struct pm_qos_request, node);
790 if (node->prio != PM_QOS_CPUIDLE_BLOCK_DEFAULT_VALUE)
791 seq_printf(m, "state %d:\t%s\n", node->prio, req->name);
792 }
793
794 spin_unlock_irqrestore(&pm_qos_lock, flags);
795 rcu_read_unlock();
796
797 return 0;
798}
799
800static int cpuidle_block_open(struct inode *inode, struct file *file)
801{
802 return single_open(file, cpuidle_block_show, NULL);
803}
804
805const struct file_operations cpuidle_block_stats_fops = {
806 .owner = THIS_MODULE,
807 .open = cpuidle_block_open,
808 .read = seq_read,
809 .llseek = seq_lseek,
810 .release = single_release,
811};
812
813static int __init cpuidle_block_debugfs_init(void)
814{
815 cpuidle_block_dentry = debugfs_create_file("cpuidle_block_devices",
816 S_IRUGO, NULL, NULL, &cpuidle_block_stats_fops);
817 return 0;
818}
819
820postcore_initcall(cpuidle_block_debugfs_init);
821
822static int __init pm_qos_power_init(void)
823{
824 int ret = 0;
825 int i;
826 struct dentry *d;
827
828 BUILD_BUG_ON(ARRAY_SIZE(pm_qos_array) != PM_QOS_NUM_CLASSES);
829
830 d = debugfs_create_dir("pm_qos", NULL);
831
832 for (i = PM_QOS_CPU_DMA_LATENCY; i < PM_QOS_NUM_CLASSES; i++) {
833 ret = register_pm_qos_misc(pm_qos_array[i], d);
834 if (ret < 0) {
835 pr_err("%s: %s setup failed\n",
836 __func__, pm_qos_array[i]->name);
837 return ret;
838 }
839 }
840
841 return ret;
842}
843
844late_initcall(pm_qos_power_init);
845
846/* Definitions related to the frequency QoS below. */
847
848/**
849 * freq_constraints_init - Initialize frequency QoS constraints.
850 * @qos: Frequency QoS constraints to initialize.
851 */
852void freq_constraints_init(struct freq_constraints *qos)
853{
854 struct pm_qos_constraints *c;
855
856 c = &qos->min_freq;
857 plist_head_init(&c->list);
858 c->target_value = FREQ_QOS_MIN_DEFAULT_VALUE;
859 c->default_value = FREQ_QOS_MIN_DEFAULT_VALUE;
860 c->no_constraint_value = FREQ_QOS_MIN_DEFAULT_VALUE;
861 c->type = PM_QOS_MAX;
862 c->notifiers = &qos->min_freq_notifiers;
863 BLOCKING_INIT_NOTIFIER_HEAD(c->notifiers);
864
865 c = &qos->max_freq;
866 plist_head_init(&c->list);
867 c->target_value = FREQ_QOS_MAX_DEFAULT_VALUE;
868 c->default_value = FREQ_QOS_MAX_DEFAULT_VALUE;
869 c->no_constraint_value = FREQ_QOS_MAX_DEFAULT_VALUE;
870 c->type = PM_QOS_MIN;
871 c->notifiers = &qos->max_freq_notifiers;
872 BLOCKING_INIT_NOTIFIER_HEAD(c->notifiers);
873}
874
875/**
876 * freq_qos_read_value - Get frequency QoS constraint for a given list.
877 * @qos: Constraints to evaluate.
878 * @type: QoS request type.
879 */
880s32 freq_qos_read_value(struct freq_constraints *qos,
881 enum freq_qos_req_type type)
882{
883 s32 ret;
884
885 switch (type) {
886 case FREQ_QOS_MIN:
887 ret = IS_ERR_OR_NULL(qos) ?
888 FREQ_QOS_MIN_DEFAULT_VALUE :
889 pm_qos_read_value(&qos->min_freq);
890 break;
891 case FREQ_QOS_MAX:
892 ret = IS_ERR_OR_NULL(qos) ?
893 FREQ_QOS_MAX_DEFAULT_VALUE :
894 pm_qos_read_value(&qos->max_freq);
895 break;
896 default:
897 WARN_ON(1);
898 ret = 0;
899 }
900
901 return ret;
902}
903
904/**
905 * freq_qos_apply - Add/modify/remove frequency QoS request.
906 * @req: Constraint request to apply.
907 * @action: Action to perform (add/update/remove).
908 * @value: Value to assign to the QoS request.
909 *
910 * This is only meant to be called from inside pm_qos, not drivers.
911 */
912int freq_qos_apply(struct freq_qos_request *req,
913 enum pm_qos_req_action action, s32 value)
914{
915 int ret;
916
917 switch(req->type) {
918 case FREQ_QOS_MIN:
919 ret = pm_qos_update_target(&req->qos->min_freq, &req->pnode,
920 action, value);
921 break;
922 case FREQ_QOS_MAX:
923 ret = pm_qos_update_target(&req->qos->max_freq, &req->pnode,
924 action, value);
925 break;
926 default:
927 ret = -EINVAL;
928 }
929
930 return ret;
931}
932
933/**
934 * freq_qos_add_request - Insert new frequency QoS request into a given list.
935 * @qos: Constraints to update.
936 * @req: Preallocated request object.
937 * @type: Request type.
938 * @value: Request value.
939 *
940 * Insert a new entry into the @qos list of requests, recompute the effective
941 * QoS constraint value for that list and initialize the @req object. The
942 * caller needs to save that object for later use in updates and removal.
943 *
944 * Return 1 if the effective constraint value has changed, 0 if the effective
945 * constraint value has not changed, or a negative error code on failures.
946 */
947int freq_qos_add_request(struct freq_constraints *qos,
948 struct freq_qos_request *req,
949 enum freq_qos_req_type type, s32 value)
950{
951 int ret;
952
953 if (IS_ERR_OR_NULL(qos) || !req)
954 return -EINVAL;
955
956 if (WARN(freq_qos_request_active(req),
957 "%s() called for active request\n", __func__))
958 return -EINVAL;
959
960 req->qos = qos;
961 req->type = type;
962 ret = freq_qos_apply(req, PM_QOS_ADD_REQ, value);
963 if (ret < 0) {
964 req->qos = NULL;
965 req->type = 0;
966 }
967
968 return ret;
969}
970EXPORT_SYMBOL_GPL(freq_qos_add_request);
971
972/**
973 * freq_qos_update_request - Modify existing frequency QoS request.
974 * @req: Request to modify.
975 * @new_value: New request value.
976 *
977 * Update an existing frequency QoS request along with the effective constraint
978 * value for the list of requests it belongs to.
979 *
980 * Return 1 if the effective constraint value has changed, 0 if the effective
981 * constraint value has not changed, or a negative error code on failures.
982 */
983int freq_qos_update_request(struct freq_qos_request *req, s32 new_value)
984{
985 if (!req)
986 return -EINVAL;
987
988 if (WARN(!freq_qos_request_active(req),
989 "%s() called for unknown object\n", __func__))
990 return -EINVAL;
991
992 if (req->pnode.prio == new_value)
993 return 0;
994
995 return freq_qos_apply(req, PM_QOS_UPDATE_REQ, new_value);
996}
997EXPORT_SYMBOL_GPL(freq_qos_update_request);
998
999/**
1000 * freq_qos_remove_request - Remove frequency QoS request from its list.
1001 * @req: Request to remove.
1002 *
1003 * Remove the given frequency QoS request from the list of constraints it
1004 * belongs to and recompute the effective constraint value for that list.
1005 *
1006 * Return 1 if the effective constraint value has changed, 0 if the effective
1007 * constraint value has not changed, or a negative error code on failures.
1008 */
1009int freq_qos_remove_request(struct freq_qos_request *req)
1010{
1011 int ret;
1012
1013 if (!req)
1014 return -EINVAL;
1015
1016 if (WARN(!freq_qos_request_active(req),
1017 "%s() called for unknown object\n", __func__))
1018 return -EINVAL;
1019
1020 ret = freq_qos_apply(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
1021 req->qos = NULL;
1022 req->type = 0;
1023
1024 return ret;
1025}
1026EXPORT_SYMBOL_GPL(freq_qos_remove_request);
1027
1028/**
1029 * freq_qos_add_notifier - Add frequency QoS change notifier.
1030 * @qos: List of requests to add the notifier to.
1031 * @type: Request type.
1032 * @notifier: Notifier block to add.
1033 */
1034int freq_qos_add_notifier(struct freq_constraints *qos,
1035 enum freq_qos_req_type type,
1036 struct notifier_block *notifier)
1037{
1038 int ret;
1039
1040 if (IS_ERR_OR_NULL(qos) || !notifier)
1041 return -EINVAL;
1042
1043 switch (type) {
1044 case FREQ_QOS_MIN:
1045 ret = blocking_notifier_chain_register(qos->min_freq.notifiers,
1046 notifier);
1047 break;
1048 case FREQ_QOS_MAX:
1049 ret = blocking_notifier_chain_register(qos->max_freq.notifiers,
1050 notifier);
1051 break;
1052 default:
1053 WARN_ON(1);
1054 ret = -EINVAL;
1055 }
1056
1057 return ret;
1058}
1059EXPORT_SYMBOL_GPL(freq_qos_add_notifier);
1060
1061/**
1062 * freq_qos_remove_notifier - Remove frequency QoS change notifier.
1063 * @qos: List of requests to remove the notifier from.
1064 * @type: Request type.
1065 * @notifier: Notifier block to remove.
1066 */
1067int freq_qos_remove_notifier(struct freq_constraints *qos,
1068 enum freq_qos_req_type type,
1069 struct notifier_block *notifier)
1070{
1071 int ret;
1072
1073 if (IS_ERR_OR_NULL(qos) || !notifier)
1074 return -EINVAL;
1075
1076 switch (type) {
1077 case FREQ_QOS_MIN:
1078 ret = blocking_notifier_chain_unregister(qos->min_freq.notifiers,
1079 notifier);
1080 break;
1081 case FREQ_QOS_MAX:
1082 ret = blocking_notifier_chain_unregister(qos->max_freq.notifiers,
1083 notifier);
1084 break;
1085 default:
1086 WARN_ON(1);
1087 ret = -EINVAL;
1088 }
1089
1090 return ret;
1091}
1092EXPORT_SYMBOL_GPL(freq_qos_remove_notifier);