1
2
3
4
5
6
7
8
9
10
11
12
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16#include <linux/cpu.h>
17#include <linux/errno.h>
18#include <linux/device.h>
19#include <linux/of_device.h>
20#include <linux/pm_domain.h>
21#include <linux/slab.h>
22#include <linux/export.h>
23#include <linux/energy_model.h>
24
25#include "opp.h"
26
27
28
29
30
31static struct device_node *_opp_of_get_opp_desc_node(struct device_node *np,
32 int index)
33{
34
35 return of_parse_phandle(np, "operating-points-v2", index);
36}
37
38
39struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev)
40{
41 return _opp_of_get_opp_desc_node(dev->of_node, 0);
42}
43EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node);
44
45struct opp_table *_managed_opp(struct device *dev, int index)
46{
47 struct opp_table *opp_table, *managed_table = NULL;
48 struct device_node *np;
49
50 np = _opp_of_get_opp_desc_node(dev->of_node, index);
51 if (!np)
52 return NULL;
53
54 list_for_each_entry(opp_table, &opp_tables, node) {
55 if (opp_table->np == np) {
56
57
58
59
60
61
62
63 if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {
64 _get_opp_table_kref(opp_table);
65 managed_table = opp_table;
66 }
67
68 break;
69 }
70 }
71
72 of_node_put(np);
73
74 return managed_table;
75}
76
77
78static struct dev_pm_opp *_find_opp_of_np(struct opp_table *opp_table,
79 struct device_node *opp_np)
80{
81 struct dev_pm_opp *opp;
82
83 lockdep_assert_held(&opp_table_lock);
84
85 mutex_lock(&opp_table->lock);
86
87 list_for_each_entry(opp, &opp_table->opp_list, node) {
88 if (opp->np == opp_np) {
89 dev_pm_opp_get(opp);
90 mutex_unlock(&opp_table->lock);
91 return opp;
92 }
93 }
94
95 mutex_unlock(&opp_table->lock);
96
97 return NULL;
98}
99
100static struct device_node *of_parse_required_opp(struct device_node *np,
101 int index)
102{
103 struct device_node *required_np;
104
105 required_np = of_parse_phandle(np, "required-opps", index);
106 if (unlikely(!required_np)) {
107 pr_err("%s: Unable to parse required-opps: %pOF, index: %d\n",
108 __func__, np, index);
109 }
110
111 return required_np;
112}
113
114
115static struct opp_table *_find_table_of_opp_np(struct device_node *opp_np)
116{
117 struct opp_table *opp_table;
118 struct device_node *opp_table_np;
119
120 lockdep_assert_held(&opp_table_lock);
121
122 opp_table_np = of_get_parent(opp_np);
123 if (!opp_table_np)
124 goto err;
125
126
127 of_node_put(opp_table_np);
128
129 list_for_each_entry(opp_table, &opp_tables, node) {
130 if (opp_table_np == opp_table->np) {
131 _get_opp_table_kref(opp_table);
132 return opp_table;
133 }
134 }
135
136err:
137 return ERR_PTR(-ENODEV);
138}
139
140
141static void _opp_table_free_required_tables(struct opp_table *opp_table)
142{
143 struct opp_table **required_opp_tables = opp_table->required_opp_tables;
144 int i;
145
146 if (!required_opp_tables)
147 return;
148
149 for (i = 0; i < opp_table->required_opp_count; i++) {
150 if (IS_ERR_OR_NULL(required_opp_tables[i]))
151 break;
152
153 dev_pm_opp_put_opp_table(required_opp_tables[i]);
154 }
155
156 kfree(required_opp_tables);
157
158 opp_table->required_opp_count = 0;
159 opp_table->required_opp_tables = NULL;
160}
161
162
163
164
165
166static void _opp_table_alloc_required_tables(struct opp_table *opp_table,
167 struct device *dev,
168 struct device_node *opp_np)
169{
170 struct opp_table **required_opp_tables;
171 struct device_node *required_np, *np;
172 int count, i;
173
174
175 np = of_get_next_available_child(opp_np, NULL);
176 if (!np) {
177 dev_warn(dev, "Empty OPP table\n");
178
179 return;
180 }
181
182 count = of_count_phandle_with_args(np, "required-opps", NULL);
183 if (!count)
184 goto put_np;
185
186 required_opp_tables = kcalloc(count, sizeof(*required_opp_tables),
187 GFP_KERNEL);
188 if (!required_opp_tables)
189 goto put_np;
190
191 opp_table->required_opp_tables = required_opp_tables;
192 opp_table->required_opp_count = count;
193
194 for (i = 0; i < count; i++) {
195 required_np = of_parse_required_opp(np, i);
196 if (!required_np)
197 goto free_required_tables;
198
199 required_opp_tables[i] = _find_table_of_opp_np(required_np);
200 of_node_put(required_np);
201
202 if (IS_ERR(required_opp_tables[i]))
203 goto free_required_tables;
204
205
206
207
208
209
210 if (!required_opp_tables[i]->is_genpd) {
211 dev_err(dev, "required-opp doesn't belong to genpd: %pOF\n",
212 required_np);
213 goto free_required_tables;
214 }
215 }
216
217 goto put_np;
218
219free_required_tables:
220 _opp_table_free_required_tables(opp_table);
221put_np:
222 of_node_put(np);
223}
224
225void _of_init_opp_table(struct opp_table *opp_table, struct device *dev,
226 int index)
227{
228 struct device_node *np, *opp_np;
229 u32 val;
230
231
232
233
234
235 np = of_node_get(dev->of_node);
236 if (!np)
237 return;
238
239 if (!of_property_read_u32(np, "clock-latency", &val))
240 opp_table->clock_latency_ns_max = val;
241 of_property_read_u32(np, "voltage-tolerance",
242 &opp_table->voltage_tolerance_v1);
243
244 if (of_find_property(np, "#power-domain-cells", NULL))
245 opp_table->is_genpd = true;
246
247
248 opp_np = _opp_of_get_opp_desc_node(np, index);
249 of_node_put(np);
250
251 if (!opp_np)
252 return;
253
254 if (of_property_read_bool(opp_np, "opp-shared"))
255 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
256 else
257 opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
258
259 opp_table->np = opp_np;
260
261 _opp_table_alloc_required_tables(opp_table, dev, opp_np);
262 of_node_put(opp_np);
263}
264
265void _of_clear_opp_table(struct opp_table *opp_table)
266{
267 _opp_table_free_required_tables(opp_table);
268}
269
270
271
272
273
274void _of_opp_free_required_opps(struct opp_table *opp_table,
275 struct dev_pm_opp *opp)
276{
277 struct dev_pm_opp **required_opps = opp->required_opps;
278 int i;
279
280 if (!required_opps)
281 return;
282
283 for (i = 0; i < opp_table->required_opp_count; i++) {
284 if (!required_opps[i])
285 break;
286
287
288 dev_pm_opp_put(required_opps[i]);
289 }
290
291 kfree(required_opps);
292 opp->required_opps = NULL;
293}
294
295
296static int _of_opp_alloc_required_opps(struct opp_table *opp_table,
297 struct dev_pm_opp *opp)
298{
299 struct dev_pm_opp **required_opps;
300 struct opp_table *required_table;
301 struct device_node *np;
302 int i, ret, count = opp_table->required_opp_count;
303
304 if (!count)
305 return 0;
306
307 required_opps = kcalloc(count, sizeof(*required_opps), GFP_KERNEL);
308 if (!required_opps)
309 return -ENOMEM;
310
311 opp->required_opps = required_opps;
312
313 for (i = 0; i < count; i++) {
314 required_table = opp_table->required_opp_tables[i];
315
316 np = of_parse_required_opp(opp->np, i);
317 if (unlikely(!np)) {
318 ret = -ENODEV;
319 goto free_required_opps;
320 }
321
322 required_opps[i] = _find_opp_of_np(required_table, np);
323 of_node_put(np);
324
325 if (!required_opps[i]) {
326 pr_err("%s: Unable to find required OPP node: %pOF (%d)\n",
327 __func__, opp->np, i);
328 ret = -ENODEV;
329 goto free_required_opps;
330 }
331 }
332
333 return 0;
334
335free_required_opps:
336 _of_opp_free_required_opps(opp_table, opp);
337
338 return ret;
339}
340
341static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
342 struct device_node *np)
343{
344 unsigned int count = opp_table->supported_hw_count;
345 u32 version;
346 int ret;
347
348 if (!opp_table->supported_hw) {
349
350
351
352
353
354
355 if (of_find_property(np, "opp-supported-hw", NULL))
356 return false;
357 else
358 return true;
359 }
360
361 while (count--) {
362 ret = of_property_read_u32_index(np, "opp-supported-hw", count,
363 &version);
364 if (ret) {
365 dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
366 __func__, count, ret);
367 return false;
368 }
369
370
371 if (!(version & opp_table->supported_hw[count]))
372 return false;
373 }
374
375 return true;
376}
377
378static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
379 struct opp_table *opp_table)
380{
381 u32 *microvolt, *microamp = NULL;
382 int supplies = opp_table->regulator_count, vcount, icount, ret, i, j;
383 struct property *prop = NULL;
384 char name[NAME_MAX];
385
386
387 if (opp_table->prop_name) {
388 snprintf(name, sizeof(name), "opp-microvolt-%s",
389 opp_table->prop_name);
390 prop = of_find_property(opp->np, name, NULL);
391 }
392
393 if (!prop) {
394
395 sprintf(name, "opp-microvolt");
396 prop = of_find_property(opp->np, name, NULL);
397
398
399 if (!prop) {
400 if (unlikely(supplies == -1)) {
401
402 opp_table->regulator_count = 0;
403 return 0;
404 }
405
406 if (!supplies)
407 return 0;
408
409 dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n",
410 __func__);
411 return -EINVAL;
412 }
413 }
414
415 if (unlikely(supplies == -1)) {
416
417 supplies = opp_table->regulator_count = 1;
418 } else if (unlikely(!supplies)) {
419 dev_err(dev, "%s: opp-microvolt wasn't expected\n", __func__);
420 return -EINVAL;
421 }
422
423 vcount = of_property_count_u32_elems(opp->np, name);
424 if (vcount < 0) {
425 dev_err(dev, "%s: Invalid %s property (%d)\n",
426 __func__, name, vcount);
427 return vcount;
428 }
429
430
431 if (vcount != supplies && vcount != supplies * 3) {
432 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
433 __func__, name, vcount, supplies);
434 return -EINVAL;
435 }
436
437 microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL);
438 if (!microvolt)
439 return -ENOMEM;
440
441 ret = of_property_read_u32_array(opp->np, name, microvolt, vcount);
442 if (ret) {
443 dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
444 ret = -EINVAL;
445 goto free_microvolt;
446 }
447
448
449 prop = NULL;
450 if (opp_table->prop_name) {
451 snprintf(name, sizeof(name), "opp-microamp-%s",
452 opp_table->prop_name);
453 prop = of_find_property(opp->np, name, NULL);
454 }
455
456 if (!prop) {
457
458 sprintf(name, "opp-microamp");
459 prop = of_find_property(opp->np, name, NULL);
460 }
461
462 if (prop) {
463 icount = of_property_count_u32_elems(opp->np, name);
464 if (icount < 0) {
465 dev_err(dev, "%s: Invalid %s property (%d)\n", __func__,
466 name, icount);
467 ret = icount;
468 goto free_microvolt;
469 }
470
471 if (icount != supplies) {
472 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
473 __func__, name, icount, supplies);
474 ret = -EINVAL;
475 goto free_microvolt;
476 }
477
478 microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL);
479 if (!microamp) {
480 ret = -EINVAL;
481 goto free_microvolt;
482 }
483
484 ret = of_property_read_u32_array(opp->np, name, microamp,
485 icount);
486 if (ret) {
487 dev_err(dev, "%s: error parsing %s: %d\n", __func__,
488 name, ret);
489 ret = -EINVAL;
490 goto free_microamp;
491 }
492 }
493
494 for (i = 0, j = 0; i < supplies; i++) {
495 opp->supplies[i].u_volt = microvolt[j++];
496
497 if (vcount == supplies) {
498 opp->supplies[i].u_volt_min = opp->supplies[i].u_volt;
499 opp->supplies[i].u_volt_max = opp->supplies[i].u_volt;
500 } else {
501 opp->supplies[i].u_volt_min = microvolt[j++];
502 opp->supplies[i].u_volt_max = microvolt[j++];
503 }
504
505 if (microamp)
506 opp->supplies[i].u_amp = microamp[i];
507 }
508
509free_microamp:
510 kfree(microamp);
511free_microvolt:
512 kfree(microvolt);
513
514 return ret;
515}
516
517
518
519
520
521
522
523
524void dev_pm_opp_of_remove_table(struct device *dev)
525{
526 _dev_pm_opp_find_and_remove_table(dev);
527}
528EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554static struct dev_pm_opp *_opp_add_static_v2(struct opp_table *opp_table,
555 struct device *dev, struct device_node *np)
556{
557 struct dev_pm_opp *new_opp;
558 u64 rate = 0;
559 u32 val;
560 int ret;
561 bool rate_not_available = false;
562
563 new_opp = _opp_allocate(opp_table);
564 if (!new_opp)
565 return ERR_PTR(-ENOMEM);
566
567 ret = of_property_read_u64(np, "opp-hz", &rate);
568 if (ret < 0) {
569
570 if (!opp_table->is_genpd) {
571 dev_err(dev, "%s: opp-hz not found\n", __func__);
572 goto free_opp;
573 }
574
575 rate_not_available = true;
576 } else {
577
578
579
580
581
582 new_opp->rate = (unsigned long)rate;
583 }
584
585 of_property_read_u32(np, "opp-level", &new_opp->level);
586
587
588 if (!_opp_is_supported(dev, opp_table, np)) {
589 dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate);
590 goto free_opp;
591 }
592
593 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
594
595 new_opp->np = np;
596 new_opp->dynamic = false;
597 new_opp->available = true;
598
599 ret = _of_opp_alloc_required_opps(opp_table, new_opp);
600 if (ret)
601 goto free_opp;
602
603 if (!of_property_read_u32(np, "clock-latency-ns", &val))
604 new_opp->clock_latency_ns = val;
605
606 ret = opp_parse_supplies(new_opp, dev, opp_table);
607 if (ret)
608 goto free_required_opps;
609
610 if (opp_table->is_genpd)
611 new_opp->pstate = pm_genpd_opp_to_performance_state(dev, new_opp);
612
613 ret = _opp_add(dev, new_opp, opp_table, rate_not_available);
614 if (ret) {
615
616 if (ret == -EBUSY)
617 ret = 0;
618 goto free_required_opps;
619 }
620
621
622 if (of_property_read_bool(np, "opp-suspend")) {
623 if (opp_table->suspend_opp) {
624 dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
625 __func__, opp_table->suspend_opp->rate,
626 new_opp->rate);
627 } else {
628 new_opp->suspend = true;
629 opp_table->suspend_opp = new_opp;
630 }
631 }
632
633 if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
634 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
635
636 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
637 __func__, new_opp->turbo, new_opp->rate,
638 new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
639 new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns);
640
641
642
643
644
645 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
646 return new_opp;
647
648free_required_opps:
649 _of_opp_free_required_opps(opp_table, new_opp);
650free_opp:
651 _opp_free(new_opp);
652
653 return ERR_PTR(ret);
654}
655
656
657static int _of_add_opp_table_v2(struct device *dev, struct opp_table *opp_table)
658{
659 struct device_node *np;
660 int ret, count = 0, pstate_count = 0;
661 struct dev_pm_opp *opp;
662
663
664 if (opp_table->parsed_static_opps) {
665 kref_get(&opp_table->list_kref);
666 return 0;
667 }
668
669 kref_init(&opp_table->list_kref);
670
671
672 for_each_available_child_of_node(opp_table->np, np) {
673 opp = _opp_add_static_v2(opp_table, dev, np);
674 if (IS_ERR(opp)) {
675 ret = PTR_ERR(opp);
676 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
677 ret);
678 of_node_put(np);
679 goto put_list_kref;
680 } else if (opp) {
681 count++;
682 }
683 }
684
685
686 if (WARN_ON(!count)) {
687 ret = -ENOENT;
688 goto put_list_kref;
689 }
690
691 list_for_each_entry(opp, &opp_table->opp_list, node)
692 pstate_count += !!opp->pstate;
693
694
695 if (pstate_count && pstate_count != count) {
696 dev_err(dev, "Not all nodes have performance state set (%d: %d)\n",
697 count, pstate_count);
698 ret = -ENOENT;
699 goto put_list_kref;
700 }
701
702 if (pstate_count)
703 opp_table->genpd_performance_state = true;
704
705 opp_table->parsed_static_opps = true;
706
707 return 0;
708
709put_list_kref:
710 _put_opp_list_kref(opp_table);
711
712 return ret;
713}
714
715
716static int _of_add_opp_table_v1(struct device *dev, struct opp_table *opp_table)
717{
718 const struct property *prop;
719 const __be32 *val;
720 int nr, ret = 0;
721
722 prop = of_find_property(dev->of_node, "operating-points", NULL);
723 if (!prop)
724 return -ENODEV;
725 if (!prop->value)
726 return -ENODATA;
727
728
729
730
731
732 nr = prop->length / sizeof(u32);
733 if (nr % 2) {
734 dev_err(dev, "%s: Invalid OPP table\n", __func__);
735 return -EINVAL;
736 }
737
738 kref_init(&opp_table->list_kref);
739
740 val = prop->value;
741 while (nr) {
742 unsigned long freq = be32_to_cpup(val++) * 1000;
743 unsigned long volt = be32_to_cpup(val++);
744
745 ret = _opp_add_v1(opp_table, dev, freq, volt, false);
746 if (ret) {
747 dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
748 __func__, freq, ret);
749 _put_opp_list_kref(opp_table);
750 return ret;
751 }
752 nr -= 2;
753 }
754
755 return ret;
756}
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775int dev_pm_opp_of_add_table(struct device *dev)
776{
777 struct opp_table *opp_table;
778 int ret;
779
780 opp_table = dev_pm_opp_get_opp_table_indexed(dev, 0);
781 if (!opp_table)
782 return -ENOMEM;
783
784
785
786
787
788 if (opp_table->np)
789 ret = _of_add_opp_table_v2(dev, opp_table);
790 else
791 ret = _of_add_opp_table_v1(dev, opp_table);
792
793 if (ret)
794 dev_pm_opp_put_opp_table(opp_table);
795
796 return ret;
797}
798EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819int dev_pm_opp_of_add_table_indexed(struct device *dev, int index)
820{
821 struct opp_table *opp_table;
822 int ret, count;
823
824 if (index) {
825
826
827
828
829 count = of_count_phandle_with_args(dev->of_node,
830 "operating-points-v2", NULL);
831 if (count == 1)
832 index = 0;
833 }
834
835 opp_table = dev_pm_opp_get_opp_table_indexed(dev, index);
836 if (!opp_table)
837 return -ENOMEM;
838
839 ret = _of_add_opp_table_v2(dev, opp_table);
840 if (ret)
841 dev_pm_opp_put_opp_table(opp_table);
842
843 return ret;
844}
845EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed);
846
847
848
849
850
851
852
853
854
855
856void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
857{
858 _dev_pm_opp_cpumask_remove_table(cpumask, -1);
859}
860EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
861
862
863
864
865
866
867
868int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
869{
870 struct device *cpu_dev;
871 int cpu, ret;
872
873 if (WARN_ON(cpumask_empty(cpumask)))
874 return -ENODEV;
875
876 for_each_cpu(cpu, cpumask) {
877 cpu_dev = get_cpu_device(cpu);
878 if (!cpu_dev) {
879 pr_err("%s: failed to get cpu%d device\n", __func__,
880 cpu);
881 ret = -ENODEV;
882 goto remove_table;
883 }
884
885 ret = dev_pm_opp_of_add_table(cpu_dev);
886 if (ret) {
887
888
889
890
891 pr_debug("%s: couldn't find opp table for cpu:%d, %d\n",
892 __func__, cpu, ret);
893
894 goto remove_table;
895 }
896 }
897
898 return 0;
899
900remove_table:
901
902 _dev_pm_opp_cpumask_remove_table(cpumask, cpu);
903
904 return ret;
905}
906EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
926 struct cpumask *cpumask)
927{
928 struct device_node *np, *tmp_np, *cpu_np;
929 int cpu, ret = 0;
930
931
932 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
933 if (!np) {
934 dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
935 return -ENOENT;
936 }
937
938 cpumask_set_cpu(cpu_dev->id, cpumask);
939
940
941 if (!of_property_read_bool(np, "opp-shared"))
942 goto put_cpu_node;
943
944 for_each_possible_cpu(cpu) {
945 if (cpu == cpu_dev->id)
946 continue;
947
948 cpu_np = of_cpu_device_node_get(cpu);
949 if (!cpu_np) {
950 dev_err(cpu_dev, "%s: failed to get cpu%d node\n",
951 __func__, cpu);
952 ret = -ENOENT;
953 goto put_cpu_node;
954 }
955
956
957 tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0);
958 of_node_put(cpu_np);
959 if (!tmp_np) {
960 pr_err("%pOF: Couldn't find opp node\n", cpu_np);
961 ret = -ENOENT;
962 goto put_cpu_node;
963 }
964
965
966 if (np == tmp_np)
967 cpumask_set_cpu(cpu, cpumask);
968
969 of_node_put(tmp_np);
970 }
971
972put_cpu_node:
973 of_node_put(np);
974 return ret;
975}
976EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);
977
978
979
980
981
982
983
984
985
986
987
988
989int of_get_required_opp_performance_state(struct device_node *np, int index)
990{
991 struct dev_pm_opp *opp;
992 struct device_node *required_np;
993 struct opp_table *opp_table;
994 int pstate = -EINVAL;
995
996 required_np = of_parse_required_opp(np, index);
997 if (!required_np)
998 return -EINVAL;
999
1000 opp_table = _find_table_of_opp_np(required_np);
1001 if (IS_ERR(opp_table)) {
1002 pr_err("%s: Failed to find required OPP table %pOF: %ld\n",
1003 __func__, np, PTR_ERR(opp_table));
1004 goto put_required_np;
1005 }
1006
1007 opp = _find_opp_of_np(opp_table, required_np);
1008 if (opp) {
1009 pstate = opp->pstate;
1010 dev_pm_opp_put(opp);
1011 }
1012
1013 dev_pm_opp_put_opp_table(opp_table);
1014
1015put_required_np:
1016 of_node_put(required_np);
1017
1018 return pstate;
1019}
1020EXPORT_SYMBOL_GPL(of_get_required_opp_performance_state);
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp)
1031{
1032 if (IS_ERR_OR_NULL(opp)) {
1033 pr_err("%s: Invalid parameters\n", __func__);
1034 return NULL;
1035 }
1036
1037 return of_node_get(opp->np);
1038}
1039EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node);
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053static int __maybe_unused _get_cpu_power(unsigned long *mW, unsigned long *kHz,
1054 int cpu)
1055{
1056 struct device *cpu_dev;
1057 struct dev_pm_opp *opp;
1058 struct device_node *np;
1059 unsigned long mV, Hz;
1060 u32 cap;
1061 u64 tmp;
1062 int ret;
1063
1064 cpu_dev = get_cpu_device(cpu);
1065 if (!cpu_dev)
1066 return -ENODEV;
1067
1068 np = of_node_get(cpu_dev->of_node);
1069 if (!np)
1070 return -EINVAL;
1071
1072 ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
1073 of_node_put(np);
1074 if (ret)
1075 return -EINVAL;
1076
1077 Hz = *kHz * 1000;
1078 opp = dev_pm_opp_find_freq_ceil(cpu_dev, &Hz);
1079 if (IS_ERR(opp))
1080 return -EINVAL;
1081
1082 mV = dev_pm_opp_get_voltage(opp) / 1000;
1083 dev_pm_opp_put(opp);
1084 if (!mV)
1085 return -EINVAL;
1086
1087 tmp = (u64)cap * mV * mV * (Hz / 1000000);
1088 do_div(tmp, 1000000000);
1089
1090 *mW = (unsigned long)tmp;
1091 *kHz = Hz / 1000;
1092
1093 return 0;
1094}
1095
1096
1097
1098
1099
1100
1101
1102
1103void dev_pm_opp_of_register_em(struct cpumask *cpus)
1104{
1105 struct em_data_callback em_cb = EM_DATA_CB(_get_cpu_power);
1106 int ret, nr_opp, cpu = cpumask_first(cpus);
1107 struct device *cpu_dev;
1108 struct device_node *np;
1109 u32 cap;
1110
1111 cpu_dev = get_cpu_device(cpu);
1112 if (!cpu_dev)
1113 return;
1114
1115 nr_opp = dev_pm_opp_get_opp_count(cpu_dev);
1116 if (nr_opp <= 0)
1117 return;
1118
1119 np = of_node_get(cpu_dev->of_node);
1120 if (!np)
1121 return;
1122
1123
1124
1125
1126
1127
1128
1129
1130 ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
1131 of_node_put(np);
1132 if (ret || !cap)
1133 return;
1134
1135 em_register_perf_domain(cpus, nr_opp, &em_cb);
1136}
1137EXPORT_SYMBOL_GPL(dev_pm_opp_of_register_em);
1138