1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Generic OPP OF helpers
4 *
5 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
6 * Nishanth Menon
7 * Romit Dasgupta
8 * Kevin Hilman
9 */
10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13 #include <linux/cpu.h>
14 #include <linux/errno.h>
15 #include <linux/device.h>
16 #include <linux/of_device.h>
17 #include <linux/pm_domain.h>
18 #include <linux/slab.h>
19 #include <linux/export.h>
20 #include <linux/energy_model.h>
21
22 #include "opp.h"
23
24 /*
25 * Returns opp descriptor node for a device node, caller must
26 * do of_node_put().
27 */
_opp_of_get_opp_desc_node(struct device_node * np,int index)28 static struct device_node *_opp_of_get_opp_desc_node(struct device_node *np,
29 int index)
30 {
31 /* "operating-points-v2" can be an array for power domain providers */
32 return of_parse_phandle(np, "operating-points-v2", index);
33 }
34
35 /* Returns opp descriptor node for a device, caller must do of_node_put() */
dev_pm_opp_of_get_opp_desc_node(struct device * dev)36 struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev)
37 {
38 return _opp_of_get_opp_desc_node(dev->of_node, 0);
39 }
40 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node);
41
_managed_opp(struct device * dev,int index)42 struct opp_table *_managed_opp(struct device *dev, int index)
43 {
44 struct opp_table *opp_table, *managed_table = NULL;
45 struct device_node *np;
46
47 np = _opp_of_get_opp_desc_node(dev->of_node, index);
48 if (!np)
49 return NULL;
50
51 list_for_each_entry(opp_table, &opp_tables, node) {
52 if (opp_table->np == np) {
53 /*
54 * Multiple devices can point to the same OPP table and
55 * so will have same node-pointer, np.
56 *
57 * But the OPPs will be considered as shared only if the
58 * OPP table contains a "opp-shared" property.
59 */
60 if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {
61 _get_opp_table_kref(opp_table);
62 managed_table = opp_table;
63 }
64
65 break;
66 }
67 }
68
69 of_node_put(np);
70
71 return managed_table;
72 }
73
74 /* The caller must call dev_pm_opp_put() after the OPP is used */
_find_opp_of_np(struct opp_table * opp_table,struct device_node * opp_np)75 static struct dev_pm_opp *_find_opp_of_np(struct opp_table *opp_table,
76 struct device_node *opp_np)
77 {
78 struct dev_pm_opp *opp;
79
80 mutex_lock(&opp_table->lock);
81
82 list_for_each_entry(opp, &opp_table->opp_list, node) {
83 if (opp->np == opp_np) {
84 dev_pm_opp_get(opp);
85 mutex_unlock(&opp_table->lock);
86 return opp;
87 }
88 }
89
90 mutex_unlock(&opp_table->lock);
91
92 return NULL;
93 }
94
of_parse_required_opp(struct device_node * np,int index)95 static struct device_node *of_parse_required_opp(struct device_node *np,
96 int index)
97 {
98 return of_parse_phandle(np, "required-opps", index);
99 }
100
101 /* The caller must call dev_pm_opp_put_opp_table() after the table is used */
_find_table_of_opp_np(struct device_node * opp_np)102 static struct opp_table *_find_table_of_opp_np(struct device_node *opp_np)
103 {
104 struct opp_table *opp_table;
105 struct device_node *opp_table_np;
106
107 lockdep_assert_held(&opp_table_lock);
108
109 opp_table_np = of_get_parent(opp_np);
110 if (!opp_table_np)
111 goto err;
112
113 /* It is safe to put the node now as all we need now is its address */
114 of_node_put(opp_table_np);
115
116 list_for_each_entry(opp_table, &opp_tables, node) {
117 if (opp_table_np == opp_table->np) {
118 _get_opp_table_kref(opp_table);
119 return opp_table;
120 }
121 }
122
123 err:
124 return ERR_PTR(-ENODEV);
125 }
126
127 /* Free resources previously acquired by _opp_table_alloc_required_tables() */
_opp_table_free_required_tables(struct opp_table * opp_table)128 static void _opp_table_free_required_tables(struct opp_table *opp_table)
129 {
130 struct opp_table **required_opp_tables = opp_table->required_opp_tables;
131 int i;
132
133 if (!required_opp_tables)
134 return;
135
136 for (i = 0; i < opp_table->required_opp_count; i++) {
137 if (IS_ERR_OR_NULL(required_opp_tables[i]))
138 break;
139
140 dev_pm_opp_put_opp_table(required_opp_tables[i]);
141 }
142
143 kfree(required_opp_tables);
144
145 opp_table->required_opp_count = 0;
146 opp_table->required_opp_tables = NULL;
147 }
148
149 /*
150 * Populate all devices and opp tables which are part of "required-opps" list.
151 * Checking only the first OPP node should be enough.
152 */
_opp_table_alloc_required_tables(struct opp_table * opp_table,struct device * dev,struct device_node * opp_np)153 static void _opp_table_alloc_required_tables(struct opp_table *opp_table,
154 struct device *dev,
155 struct device_node *opp_np)
156 {
157 struct opp_table **required_opp_tables;
158 struct device_node *required_np, *np;
159 int count, i;
160
161 /* Traversing the first OPP node is all we need */
162 np = of_get_next_available_child(opp_np, NULL);
163 if (!np) {
164 dev_err(dev, "Empty OPP table\n");
165 return;
166 }
167
168 count = of_count_phandle_with_args(np, "required-opps", NULL);
169 if (!count)
170 goto put_np;
171
172 required_opp_tables = kcalloc(count, sizeof(*required_opp_tables),
173 GFP_KERNEL);
174 if (!required_opp_tables)
175 goto put_np;
176
177 opp_table->required_opp_tables = required_opp_tables;
178 opp_table->required_opp_count = count;
179
180 for (i = 0; i < count; i++) {
181 required_np = of_parse_required_opp(np, i);
182 if (!required_np)
183 goto free_required_tables;
184
185 required_opp_tables[i] = _find_table_of_opp_np(required_np);
186 of_node_put(required_np);
187
188 if (IS_ERR(required_opp_tables[i]))
189 goto free_required_tables;
190
191 /*
192 * We only support genpd's OPPs in the "required-opps" for now,
193 * as we don't know how much about other cases. Error out if the
194 * required OPP doesn't belong to a genpd.
195 */
196 if (!required_opp_tables[i]->is_genpd) {
197 dev_err(dev, "required-opp doesn't belong to genpd: %pOF\n",
198 required_np);
199 goto free_required_tables;
200 }
201 }
202
203 goto put_np;
204
205 free_required_tables:
206 _opp_table_free_required_tables(opp_table);
207 put_np:
208 of_node_put(np);
209 }
210
_of_init_opp_table(struct opp_table * opp_table,struct device * dev,int index)211 void _of_init_opp_table(struct opp_table *opp_table, struct device *dev,
212 int index)
213 {
214 struct device_node *np, *opp_np;
215 u32 val;
216
217 /*
218 * Only required for backward compatibility with v1 bindings, but isn't
219 * harmful for other cases. And so we do it unconditionally.
220 */
221 np = of_node_get(dev->of_node);
222 if (!np)
223 return;
224
225 if (!of_property_read_u32(np, "clock-latency", &val))
226 opp_table->clock_latency_ns_max = val;
227 of_property_read_u32(np, "voltage-tolerance",
228 &opp_table->voltage_tolerance_v1);
229
230 if (of_find_property(np, "#power-domain-cells", NULL))
231 opp_table->is_genpd = true;
232
233 /* Get OPP table node */
234 opp_np = _opp_of_get_opp_desc_node(np, index);
235 of_node_put(np);
236
237 if (!opp_np)
238 return;
239
240 if (of_property_read_bool(opp_np, "opp-shared"))
241 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
242 else
243 opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
244
245 opp_table->np = opp_np;
246
247 _opp_table_alloc_required_tables(opp_table, dev, opp_np);
248 of_node_put(opp_np);
249 }
250
_of_clear_opp_table(struct opp_table * opp_table)251 void _of_clear_opp_table(struct opp_table *opp_table)
252 {
253 _opp_table_free_required_tables(opp_table);
254 }
255
256 /*
257 * Release all resources previously acquired with a call to
258 * _of_opp_alloc_required_opps().
259 */
_of_opp_free_required_opps(struct opp_table * opp_table,struct dev_pm_opp * opp)260 void _of_opp_free_required_opps(struct opp_table *opp_table,
261 struct dev_pm_opp *opp)
262 {
263 struct dev_pm_opp **required_opps = opp->required_opps;
264 int i;
265
266 if (!required_opps)
267 return;
268
269 for (i = 0; i < opp_table->required_opp_count; i++) {
270 if (!required_opps[i])
271 break;
272
273 /* Put the reference back */
274 dev_pm_opp_put(required_opps[i]);
275 }
276
277 kfree(required_opps);
278 opp->required_opps = NULL;
279 }
280
281 /* Populate all required OPPs which are part of "required-opps" list */
_of_opp_alloc_required_opps(struct opp_table * opp_table,struct dev_pm_opp * opp)282 static int _of_opp_alloc_required_opps(struct opp_table *opp_table,
283 struct dev_pm_opp *opp)
284 {
285 struct dev_pm_opp **required_opps;
286 struct opp_table *required_table;
287 struct device_node *np;
288 int i, ret, count = opp_table->required_opp_count;
289
290 if (!count)
291 return 0;
292
293 required_opps = kcalloc(count, sizeof(*required_opps), GFP_KERNEL);
294 if (!required_opps)
295 return -ENOMEM;
296
297 opp->required_opps = required_opps;
298
299 for (i = 0; i < count; i++) {
300 required_table = opp_table->required_opp_tables[i];
301
302 np = of_parse_required_opp(opp->np, i);
303 if (unlikely(!np)) {
304 ret = -ENODEV;
305 goto free_required_opps;
306 }
307
308 required_opps[i] = _find_opp_of_np(required_table, np);
309 of_node_put(np);
310
311 if (!required_opps[i]) {
312 pr_err("%s: Unable to find required OPP node: %pOF (%d)\n",
313 __func__, opp->np, i);
314 ret = -ENODEV;
315 goto free_required_opps;
316 }
317 }
318
319 return 0;
320
321 free_required_opps:
322 _of_opp_free_required_opps(opp_table, opp);
323
324 return ret;
325 }
326
_bandwidth_supported(struct device * dev,struct opp_table * opp_table)327 static int _bandwidth_supported(struct device *dev, struct opp_table *opp_table)
328 {
329 struct device_node *np, *opp_np;
330 struct property *prop;
331
332 if (!opp_table) {
333 np = of_node_get(dev->of_node);
334 if (!np)
335 return -ENODEV;
336
337 opp_np = _opp_of_get_opp_desc_node(np, 0);
338 of_node_put(np);
339 } else {
340 opp_np = of_node_get(opp_table->np);
341 }
342
343 /* Lets not fail in case we are parsing opp-v1 bindings */
344 if (!opp_np)
345 return 0;
346
347 /* Checking only first OPP is sufficient */
348 np = of_get_next_available_child(opp_np, NULL);
349 of_node_put(opp_np);
350 if (!np) {
351 dev_err(dev, "OPP table empty\n");
352 return -EINVAL;
353 }
354
355 prop = of_find_property(np, "opp-peak-kBps", NULL);
356 of_node_put(np);
357
358 if (!prop || !prop->length)
359 return 0;
360
361 return 1;
362 }
363
dev_pm_opp_of_find_icc_paths(struct device * dev,struct opp_table * opp_table)364 int dev_pm_opp_of_find_icc_paths(struct device *dev,
365 struct opp_table *opp_table)
366 {
367 struct device_node *np;
368 int ret, i, count, num_paths;
369 struct icc_path **paths;
370
371 ret = _bandwidth_supported(dev, opp_table);
372 if (ret <= 0)
373 return ret;
374
375 ret = 0;
376
377 np = of_node_get(dev->of_node);
378 if (!np)
379 return 0;
380
381 count = of_count_phandle_with_args(np, "interconnects",
382 "#interconnect-cells");
383 of_node_put(np);
384 if (count < 0)
385 return 0;
386
387 /* two phandles when #interconnect-cells = <1> */
388 if (count % 2) {
389 dev_err(dev, "%s: Invalid interconnects values\n", __func__);
390 return -EINVAL;
391 }
392
393 num_paths = count / 2;
394 paths = kcalloc(num_paths, sizeof(*paths), GFP_KERNEL);
395 if (!paths)
396 return -ENOMEM;
397
398 for (i = 0; i < num_paths; i++) {
399 paths[i] = of_icc_get_by_index(dev, i);
400 if (IS_ERR(paths[i])) {
401 ret = PTR_ERR(paths[i]);
402 if (ret != -EPROBE_DEFER) {
403 dev_err(dev, "%s: Unable to get path%d: %d\n",
404 __func__, i, ret);
405 }
406 goto err;
407 }
408 }
409
410 if (opp_table) {
411 opp_table->paths = paths;
412 opp_table->path_count = num_paths;
413 return 0;
414 }
415
416 err:
417 while (i--)
418 icc_put(paths[i]);
419
420 kfree(paths);
421
422 return ret;
423 }
424 EXPORT_SYMBOL_GPL(dev_pm_opp_of_find_icc_paths);
425
_opp_is_supported(struct device * dev,struct opp_table * opp_table,struct device_node * np)426 static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
427 struct device_node *np)
428 {
429 unsigned int levels = opp_table->supported_hw_count;
430 int count, versions, ret, i, j;
431 u32 val;
432
433 if (!opp_table->supported_hw) {
434 /*
435 * In the case that no supported_hw has been set by the
436 * platform but there is an opp-supported-hw value set for
437 * an OPP then the OPP should not be enabled as there is
438 * no way to see if the hardware supports it.
439 */
440 if (of_find_property(np, "opp-supported-hw", NULL))
441 return false;
442 else
443 return true;
444 }
445
446 count = of_property_count_u32_elems(np, "opp-supported-hw");
447 if (count <= 0 || count % levels) {
448 dev_err(dev, "%s: Invalid opp-supported-hw property (%d)\n",
449 __func__, count);
450 return false;
451 }
452
453 versions = count / levels;
454
455 /* All levels in at least one of the versions should match */
456 for (i = 0; i < versions; i++) {
457 bool supported = true;
458
459 for (j = 0; j < levels; j++) {
460 ret = of_property_read_u32_index(np, "opp-supported-hw",
461 i * levels + j, &val);
462 if (ret) {
463 dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
464 __func__, i * levels + j, ret);
465 return false;
466 }
467
468 /* Check if the level is supported */
469 if (!(val & opp_table->supported_hw[j])) {
470 supported = false;
471 break;
472 }
473 }
474
475 if (supported)
476 return true;
477 }
478
479 return false;
480 }
481
opp_parse_supplies(struct dev_pm_opp * opp,struct device * dev,struct opp_table * opp_table)482 static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
483 struct opp_table *opp_table)
484 {
485 u32 *microvolt, *microamp = NULL;
486 int supplies = opp_table->regulator_count, vcount, icount, ret, i, j;
487 struct property *prop = NULL;
488 char name[NAME_MAX];
489
490 /* Search for "opp-microvolt-<name>" */
491 if (opp_table->prop_name) {
492 snprintf(name, sizeof(name), "opp-microvolt-%s",
493 opp_table->prop_name);
494 prop = of_find_property(opp->np, name, NULL);
495 }
496
497 if (!prop) {
498 /* Search for "opp-microvolt" */
499 sprintf(name, "opp-microvolt");
500 prop = of_find_property(opp->np, name, NULL);
501
502 /* Missing property isn't a problem, but an invalid entry is */
503 if (!prop) {
504 if (unlikely(supplies == -1)) {
505 /* Initialize regulator_count */
506 opp_table->regulator_count = 0;
507 return 0;
508 }
509
510 if (!supplies)
511 return 0;
512
513 dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n",
514 __func__);
515 return -EINVAL;
516 }
517 }
518
519 if (unlikely(supplies == -1)) {
520 /* Initialize regulator_count */
521 supplies = opp_table->regulator_count = 1;
522 } else if (unlikely(!supplies)) {
523 dev_err(dev, "%s: opp-microvolt wasn't expected\n", __func__);
524 return -EINVAL;
525 }
526
527 vcount = of_property_count_u32_elems(opp->np, name);
528 if (vcount < 0) {
529 dev_err(dev, "%s: Invalid %s property (%d)\n",
530 __func__, name, vcount);
531 return vcount;
532 }
533
534 /* There can be one or three elements per supply */
535 if (vcount != supplies && vcount != supplies * 3) {
536 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
537 __func__, name, vcount, supplies);
538 return -EINVAL;
539 }
540
541 microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL);
542 if (!microvolt)
543 return -ENOMEM;
544
545 ret = of_property_read_u32_array(opp->np, name, microvolt, vcount);
546 if (ret) {
547 dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
548 ret = -EINVAL;
549 goto free_microvolt;
550 }
551
552 /* Search for "opp-microamp-<name>" */
553 prop = NULL;
554 if (opp_table->prop_name) {
555 snprintf(name, sizeof(name), "opp-microamp-%s",
556 opp_table->prop_name);
557 prop = of_find_property(opp->np, name, NULL);
558 }
559
560 if (!prop) {
561 /* Search for "opp-microamp" */
562 sprintf(name, "opp-microamp");
563 prop = of_find_property(opp->np, name, NULL);
564 }
565
566 if (prop) {
567 icount = of_property_count_u32_elems(opp->np, name);
568 if (icount < 0) {
569 dev_err(dev, "%s: Invalid %s property (%d)\n", __func__,
570 name, icount);
571 ret = icount;
572 goto free_microvolt;
573 }
574
575 if (icount != supplies) {
576 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
577 __func__, name, icount, supplies);
578 ret = -EINVAL;
579 goto free_microvolt;
580 }
581
582 microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL);
583 if (!microamp) {
584 ret = -EINVAL;
585 goto free_microvolt;
586 }
587
588 ret = of_property_read_u32_array(opp->np, name, microamp,
589 icount);
590 if (ret) {
591 dev_err(dev, "%s: error parsing %s: %d\n", __func__,
592 name, ret);
593 ret = -EINVAL;
594 goto free_microamp;
595 }
596 }
597
598 for (i = 0, j = 0; i < supplies; i++) {
599 opp->supplies[i].u_volt = microvolt[j++];
600
601 if (vcount == supplies) {
602 opp->supplies[i].u_volt_min = opp->supplies[i].u_volt;
603 opp->supplies[i].u_volt_max = opp->supplies[i].u_volt;
604 } else {
605 opp->supplies[i].u_volt_min = microvolt[j++];
606 opp->supplies[i].u_volt_max = microvolt[j++];
607 }
608
609 if (microamp)
610 opp->supplies[i].u_amp = microamp[i];
611 }
612
613 free_microamp:
614 kfree(microamp);
615 free_microvolt:
616 kfree(microvolt);
617
618 return ret;
619 }
620
621 /**
622 * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
623 * entries
624 * @dev: device pointer used to lookup OPP table.
625 *
626 * Free OPPs created using static entries present in DT.
627 */
dev_pm_opp_of_remove_table(struct device * dev)628 void dev_pm_opp_of_remove_table(struct device *dev)
629 {
630 dev_pm_opp_remove_table(dev);
631 }
632 EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
633
_read_bw(struct dev_pm_opp * new_opp,struct opp_table * table,struct device_node * np,bool peak)634 static int _read_bw(struct dev_pm_opp *new_opp, struct opp_table *table,
635 struct device_node *np, bool peak)
636 {
637 const char *name = peak ? "opp-peak-kBps" : "opp-avg-kBps";
638 struct property *prop;
639 int i, count, ret;
640 u32 *bw;
641
642 prop = of_find_property(np, name, NULL);
643 if (!prop)
644 return -ENODEV;
645
646 count = prop->length / sizeof(u32);
647 if (table->path_count != count) {
648 pr_err("%s: Mismatch between %s and paths (%d %d)\n",
649 __func__, name, count, table->path_count);
650 return -EINVAL;
651 }
652
653 bw = kmalloc_array(count, sizeof(*bw), GFP_KERNEL);
654 if (!bw)
655 return -ENOMEM;
656
657 ret = of_property_read_u32_array(np, name, bw, count);
658 if (ret) {
659 pr_err("%s: Error parsing %s: %d\n", __func__, name, ret);
660 goto out;
661 }
662
663 for (i = 0; i < count; i++) {
664 if (peak)
665 new_opp->bandwidth[i].peak = kBps_to_icc(bw[i]);
666 else
667 new_opp->bandwidth[i].avg = kBps_to_icc(bw[i]);
668 }
669
670 out:
671 kfree(bw);
672 return ret;
673 }
674
_read_opp_key(struct dev_pm_opp * new_opp,struct opp_table * table,struct device_node * np,bool * rate_not_available)675 static int _read_opp_key(struct dev_pm_opp *new_opp, struct opp_table *table,
676 struct device_node *np, bool *rate_not_available)
677 {
678 bool found = false;
679 u64 rate;
680 int ret;
681
682 ret = of_property_read_u64(np, "opp-hz", &rate);
683 if (!ret) {
684 /*
685 * Rate is defined as an unsigned long in clk API, and so
686 * casting explicitly to its type. Must be fixed once rate is 64
687 * bit guaranteed in clk API.
688 */
689 new_opp->rate = (unsigned long)rate;
690 found = true;
691 }
692 *rate_not_available = !!ret;
693
694 /*
695 * Bandwidth consists of peak and average (optional) values:
696 * opp-peak-kBps = <path1_value path2_value>;
697 * opp-avg-kBps = <path1_value path2_value>;
698 */
699 ret = _read_bw(new_opp, table, np, true);
700 if (!ret) {
701 found = true;
702 ret = _read_bw(new_opp, table, np, false);
703 }
704
705 /* The properties were found but we failed to parse them */
706 if (ret && ret != -ENODEV)
707 return ret;
708
709 if (!of_property_read_u32(np, "opp-level", &new_opp->level))
710 found = true;
711
712 if (found)
713 return 0;
714
715 return ret;
716 }
717
718 /**
719 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
720 * @opp_table: OPP table
721 * @dev: device for which we do this operation
722 * @np: device node
723 *
724 * This function adds an opp definition to the opp table and returns status. The
725 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
726 * removed by dev_pm_opp_remove.
727 *
728 * Return:
729 * Valid OPP pointer:
730 * On success
731 * NULL:
732 * Duplicate OPPs (both freq and volt are same) and opp->available
733 * OR if the OPP is not supported by hardware.
734 * ERR_PTR(-EEXIST):
735 * Freq are same and volt are different OR
736 * Duplicate OPPs (both freq and volt are same) and !opp->available
737 * ERR_PTR(-ENOMEM):
738 * Memory allocation failure
739 * ERR_PTR(-EINVAL):
740 * Failed parsing the OPP node
741 */
_opp_add_static_v2(struct opp_table * opp_table,struct device * dev,struct device_node * np)742 static struct dev_pm_opp *_opp_add_static_v2(struct opp_table *opp_table,
743 struct device *dev, struct device_node *np)
744 {
745 struct dev_pm_opp *new_opp;
746 u32 val;
747 int ret;
748 bool rate_not_available = false;
749
750 new_opp = _opp_allocate(opp_table);
751 if (!new_opp)
752 return ERR_PTR(-ENOMEM);
753
754 ret = _read_opp_key(new_opp, opp_table, np, &rate_not_available);
755 if (ret < 0 && !opp_table->is_genpd) {
756 dev_err(dev, "%s: opp key field not found\n", __func__);
757 goto free_opp;
758 }
759
760 /* Check if the OPP supports hardware's hierarchy of versions or not */
761 if (!_opp_is_supported(dev, opp_table, np)) {
762 dev_dbg(dev, "OPP not supported by hardware: %lu\n",
763 new_opp->rate);
764 goto free_opp;
765 }
766
767 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
768
769 new_opp->np = np;
770 new_opp->dynamic = false;
771 new_opp->available = true;
772
773 ret = _of_opp_alloc_required_opps(opp_table, new_opp);
774 if (ret)
775 goto free_opp;
776
777 if (!of_property_read_u32(np, "clock-latency-ns", &val))
778 new_opp->clock_latency_ns = val;
779
780 ret = opp_parse_supplies(new_opp, dev, opp_table);
781 if (ret)
782 goto free_required_opps;
783
784 if (opp_table->is_genpd)
785 new_opp->pstate = pm_genpd_opp_to_performance_state(dev, new_opp);
786
787 ret = _opp_add(dev, new_opp, opp_table, rate_not_available);
788 if (ret) {
789 /* Don't return error for duplicate OPPs */
790 if (ret == -EBUSY)
791 ret = 0;
792 goto free_required_opps;
793 }
794
795 /* OPP to select on device suspend */
796 if (of_property_read_bool(np, "opp-suspend")) {
797 if (opp_table->suspend_opp) {
798 /* Pick the OPP with higher rate as suspend OPP */
799 if (new_opp->rate > opp_table->suspend_opp->rate) {
800 opp_table->suspend_opp->suspend = false;
801 new_opp->suspend = true;
802 opp_table->suspend_opp = new_opp;
803 }
804 } else {
805 new_opp->suspend = true;
806 opp_table->suspend_opp = new_opp;
807 }
808 }
809
810 if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
811 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
812
813 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
814 __func__, new_opp->turbo, new_opp->rate,
815 new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
816 new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns);
817
818 /*
819 * Notify the changes in the availability of the operable
820 * frequency/voltage list.
821 */
822 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
823 return new_opp;
824
825 free_required_opps:
826 _of_opp_free_required_opps(opp_table, new_opp);
827 free_opp:
828 _opp_free(new_opp);
829
830 return ret ? ERR_PTR(ret) : NULL;
831 }
832
833 /* Initializes OPP tables based on new bindings */
_of_add_opp_table_v2(struct device * dev,struct opp_table * opp_table)834 static int _of_add_opp_table_v2(struct device *dev, struct opp_table *opp_table)
835 {
836 struct device_node *np;
837 int ret, count = 0;
838 struct dev_pm_opp *opp;
839
840 /* OPP table is already initialized for the device */
841 mutex_lock(&opp_table->lock);
842 if (opp_table->parsed_static_opps) {
843 opp_table->parsed_static_opps++;
844 mutex_unlock(&opp_table->lock);
845 return 0;
846 }
847
848 opp_table->parsed_static_opps = 1;
849 mutex_unlock(&opp_table->lock);
850
851 /* We have opp-table node now, iterate over it and add OPPs */
852 for_each_available_child_of_node(opp_table->np, np) {
853 opp = _opp_add_static_v2(opp_table, dev, np);
854 if (IS_ERR(opp)) {
855 ret = PTR_ERR(opp);
856 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
857 ret);
858 of_node_put(np);
859 goto remove_static_opp;
860 } else if (opp) {
861 count++;
862 }
863 }
864
865 /* There should be one or more OPPs defined */
866 if (!count) {
867 dev_err(dev, "%s: no supported OPPs", __func__);
868 ret = -ENOENT;
869 goto remove_static_opp;
870 }
871
872 list_for_each_entry(opp, &opp_table->opp_list, node) {
873 /* Any non-zero performance state would enable the feature */
874 if (opp->pstate) {
875 opp_table->genpd_performance_state = true;
876 break;
877 }
878 }
879
880 return 0;
881
882 remove_static_opp:
883 _opp_remove_all_static(opp_table);
884
885 return ret;
886 }
887
888 /* Initializes OPP tables based on old-deprecated bindings */
_of_add_opp_table_v1(struct device * dev,struct opp_table * opp_table)889 static int _of_add_opp_table_v1(struct device *dev, struct opp_table *opp_table)
890 {
891 const struct property *prop;
892 const __be32 *val;
893 int nr, ret = 0;
894
895 mutex_lock(&opp_table->lock);
896 if (opp_table->parsed_static_opps) {
897 opp_table->parsed_static_opps++;
898 mutex_unlock(&opp_table->lock);
899 return 0;
900 }
901
902 opp_table->parsed_static_opps = 1;
903 mutex_unlock(&opp_table->lock);
904
905 prop = of_find_property(dev->of_node, "operating-points", NULL);
906 if (!prop) {
907 ret = -ENODEV;
908 goto remove_static_opp;
909 }
910 if (!prop->value) {
911 ret = -ENODATA;
912 goto remove_static_opp;
913 }
914
915 /*
916 * Each OPP is a set of tuples consisting of frequency and
917 * voltage like <freq-kHz vol-uV>.
918 */
919 nr = prop->length / sizeof(u32);
920 if (nr % 2) {
921 dev_err(dev, "%s: Invalid OPP table\n", __func__);
922 ret = -EINVAL;
923 goto remove_static_opp;
924 }
925
926 val = prop->value;
927 while (nr) {
928 unsigned long freq = be32_to_cpup(val++) * 1000;
929 unsigned long volt = be32_to_cpup(val++);
930
931 ret = _opp_add_v1(opp_table, dev, freq, volt, false);
932 if (ret) {
933 dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
934 __func__, freq, ret);
935 goto remove_static_opp;
936 }
937 nr -= 2;
938 }
939
940 return 0;
941
942 remove_static_opp:
943 _opp_remove_all_static(opp_table);
944
945 return ret;
946 }
947
948 /**
949 * dev_pm_opp_of_add_table() - Initialize opp table from device tree
950 * @dev: device pointer used to lookup OPP table.
951 *
952 * Register the initial OPP table with the OPP library for given device.
953 *
954 * Return:
955 * 0 On success OR
956 * Duplicate OPPs (both freq and volt are same) and opp->available
957 * -EEXIST Freq are same and volt are different OR
958 * Duplicate OPPs (both freq and volt are same) and !opp->available
959 * -ENOMEM Memory allocation failure
960 * -ENODEV when 'operating-points' property is not found or is invalid data
961 * in device node.
962 * -ENODATA when empty 'operating-points' property is found
963 * -EINVAL when invalid entries are found in opp-v2 table
964 */
dev_pm_opp_of_add_table(struct device * dev)965 int dev_pm_opp_of_add_table(struct device *dev)
966 {
967 struct opp_table *opp_table;
968 int ret;
969
970 opp_table = dev_pm_opp_get_opp_table_indexed(dev, 0);
971 if (IS_ERR(opp_table))
972 return PTR_ERR(opp_table);
973
974 /*
975 * OPPs have two version of bindings now. Also try the old (v1)
976 * bindings for backward compatibility with older dtbs.
977 */
978 if (opp_table->np)
979 ret = _of_add_opp_table_v2(dev, opp_table);
980 else
981 ret = _of_add_opp_table_v1(dev, opp_table);
982
983 if (ret)
984 dev_pm_opp_put_opp_table(opp_table);
985
986 return ret;
987 }
988 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
989
990 /**
991 * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree
992 * @dev: device pointer used to lookup OPP table.
993 * @index: Index number.
994 *
995 * Register the initial OPP table with the OPP library for given device only
996 * using the "operating-points-v2" property.
997 *
998 * Return:
999 * 0 On success OR
1000 * Duplicate OPPs (both freq and volt are same) and opp->available
1001 * -EEXIST Freq are same and volt are different OR
1002 * Duplicate OPPs (both freq and volt are same) and !opp->available
1003 * -ENOMEM Memory allocation failure
1004 * -ENODEV when 'operating-points' property is not found or is invalid data
1005 * in device node.
1006 * -ENODATA when empty 'operating-points' property is found
1007 * -EINVAL when invalid entries are found in opp-v2 table
1008 */
dev_pm_opp_of_add_table_indexed(struct device * dev,int index)1009 int dev_pm_opp_of_add_table_indexed(struct device *dev, int index)
1010 {
1011 struct opp_table *opp_table;
1012 int ret, count;
1013
1014 if (index) {
1015 /*
1016 * If only one phandle is present, then the same OPP table
1017 * applies for all index requests.
1018 */
1019 count = of_count_phandle_with_args(dev->of_node,
1020 "operating-points-v2", NULL);
1021 if (count == 1)
1022 index = 0;
1023 }
1024
1025 opp_table = dev_pm_opp_get_opp_table_indexed(dev, index);
1026 if (IS_ERR(opp_table))
1027 return PTR_ERR(opp_table);
1028
1029 ret = _of_add_opp_table_v2(dev, opp_table);
1030 if (ret)
1031 dev_pm_opp_put_opp_table(opp_table);
1032
1033 return ret;
1034 }
1035 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed);
1036
1037 /* CPU device specific helpers */
1038
1039 /**
1040 * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
1041 * @cpumask: cpumask for which OPP table needs to be removed
1042 *
1043 * This removes the OPP tables for CPUs present in the @cpumask.
1044 * This should be used only to remove static entries created from DT.
1045 */
dev_pm_opp_of_cpumask_remove_table(const struct cpumask * cpumask)1046 void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
1047 {
1048 _dev_pm_opp_cpumask_remove_table(cpumask, -1);
1049 }
1050 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
1051
1052 /**
1053 * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
1054 * @cpumask: cpumask for which OPP table needs to be added.
1055 *
1056 * This adds the OPP tables for CPUs present in the @cpumask.
1057 */
dev_pm_opp_of_cpumask_add_table(const struct cpumask * cpumask)1058 int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
1059 {
1060 struct device *cpu_dev;
1061 int cpu, ret;
1062
1063 if (WARN_ON(cpumask_empty(cpumask)))
1064 return -ENODEV;
1065
1066 for_each_cpu(cpu, cpumask) {
1067 cpu_dev = get_cpu_device(cpu);
1068 if (!cpu_dev) {
1069 pr_err("%s: failed to get cpu%d device\n", __func__,
1070 cpu);
1071 ret = -ENODEV;
1072 goto remove_table;
1073 }
1074
1075 ret = dev_pm_opp_of_add_table(cpu_dev);
1076 if (ret) {
1077 /*
1078 * OPP may get registered dynamically, don't print error
1079 * message here.
1080 */
1081 pr_debug("%s: couldn't find opp table for cpu:%d, %d\n",
1082 __func__, cpu, ret);
1083
1084 goto remove_table;
1085 }
1086 }
1087
1088 return 0;
1089
1090 remove_table:
1091 /* Free all other OPPs */
1092 _dev_pm_opp_cpumask_remove_table(cpumask, cpu);
1093
1094 return ret;
1095 }
1096 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
1097
1098 /*
1099 * Works only for OPP v2 bindings.
1100 *
1101 * Returns -ENOENT if operating-points-v2 bindings aren't supported.
1102 */
1103 /**
1104 * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
1105 * @cpu_dev using operating-points-v2
1106 * bindings.
1107 *
1108 * @cpu_dev: CPU device for which we do this operation
1109 * @cpumask: cpumask to update with information of sharing CPUs
1110 *
1111 * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
1112 *
1113 * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
1114 */
dev_pm_opp_of_get_sharing_cpus(struct device * cpu_dev,struct cpumask * cpumask)1115 int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
1116 struct cpumask *cpumask)
1117 {
1118 struct device_node *np, *tmp_np, *cpu_np;
1119 int cpu, ret = 0;
1120
1121 /* Get OPP descriptor node */
1122 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
1123 if (!np) {
1124 dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
1125 return -ENOENT;
1126 }
1127
1128 cpumask_set_cpu(cpu_dev->id, cpumask);
1129
1130 /* OPPs are shared ? */
1131 if (!of_property_read_bool(np, "opp-shared"))
1132 goto put_cpu_node;
1133
1134 for_each_possible_cpu(cpu) {
1135 if (cpu == cpu_dev->id)
1136 continue;
1137
1138 cpu_np = of_cpu_device_node_get(cpu);
1139 if (!cpu_np) {
1140 dev_err(cpu_dev, "%s: failed to get cpu%d node\n",
1141 __func__, cpu);
1142 ret = -ENOENT;
1143 goto put_cpu_node;
1144 }
1145
1146 /* Get OPP descriptor node */
1147 tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0);
1148 of_node_put(cpu_np);
1149 if (!tmp_np) {
1150 pr_err("%pOF: Couldn't find opp node\n", cpu_np);
1151 ret = -ENOENT;
1152 goto put_cpu_node;
1153 }
1154
1155 /* CPUs are sharing opp node */
1156 if (np == tmp_np)
1157 cpumask_set_cpu(cpu, cpumask);
1158
1159 of_node_put(tmp_np);
1160 }
1161
1162 put_cpu_node:
1163 of_node_put(np);
1164 return ret;
1165 }
1166 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);
1167
1168 /**
1169 * of_get_required_opp_performance_state() - Search for required OPP and return its performance state.
1170 * @np: Node that contains the "required-opps" property.
1171 * @index: Index of the phandle to parse.
1172 *
1173 * Returns the performance state of the OPP pointed out by the "required-opps"
1174 * property at @index in @np.
1175 *
1176 * Return: Zero or positive performance state on success, otherwise negative
1177 * value on errors.
1178 */
of_get_required_opp_performance_state(struct device_node * np,int index)1179 int of_get_required_opp_performance_state(struct device_node *np, int index)
1180 {
1181 struct dev_pm_opp *opp;
1182 struct device_node *required_np;
1183 struct opp_table *opp_table;
1184 int pstate = -EINVAL;
1185
1186 required_np = of_parse_required_opp(np, index);
1187 if (!required_np)
1188 return -ENODEV;
1189
1190 opp_table = _find_table_of_opp_np(required_np);
1191 if (IS_ERR(opp_table)) {
1192 pr_err("%s: Failed to find required OPP table %pOF: %ld\n",
1193 __func__, np, PTR_ERR(opp_table));
1194 goto put_required_np;
1195 }
1196
1197 opp = _find_opp_of_np(opp_table, required_np);
1198 if (opp) {
1199 pstate = opp->pstate;
1200 dev_pm_opp_put(opp);
1201 }
1202
1203 dev_pm_opp_put_opp_table(opp_table);
1204
1205 put_required_np:
1206 of_node_put(required_np);
1207
1208 return pstate;
1209 }
1210 EXPORT_SYMBOL_GPL(of_get_required_opp_performance_state);
1211
1212 /**
1213 * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp
1214 * @opp: opp for which DT node has to be returned for
1215 *
1216 * Return: DT node corresponding to the opp, else 0 on success.
1217 *
1218 * The caller needs to put the node with of_node_put() after using it.
1219 */
dev_pm_opp_get_of_node(struct dev_pm_opp * opp)1220 struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp)
1221 {
1222 if (IS_ERR_OR_NULL(opp)) {
1223 pr_err("%s: Invalid parameters\n", __func__);
1224 return NULL;
1225 }
1226
1227 return of_node_get(opp->np);
1228 }
1229 EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node);
1230
1231 /*
1232 * Callback function provided to the Energy Model framework upon registration.
1233 * This computes the power estimated by @dev at @kHz if it is the frequency
1234 * of an existing OPP, or at the frequency of the first OPP above @kHz otherwise
1235 * (see dev_pm_opp_find_freq_ceil()). This function updates @kHz to the ceiled
1236 * frequency and @mW to the associated power. The power is estimated as
1237 * P = C * V^2 * f with C being the device's capacitance and V and f
1238 * respectively the voltage and frequency of the OPP.
1239 *
1240 * Returns -EINVAL if the power calculation failed because of missing
1241 * parameters, 0 otherwise.
1242 */
_get_power(unsigned long * mW,unsigned long * kHz,struct device * dev)1243 static int __maybe_unused _get_power(unsigned long *mW, unsigned long *kHz,
1244 struct device *dev)
1245 {
1246 struct dev_pm_opp *opp;
1247 struct device_node *np;
1248 unsigned long mV, Hz;
1249 u32 cap;
1250 u64 tmp;
1251 int ret;
1252
1253 np = of_node_get(dev->of_node);
1254 if (!np)
1255 return -EINVAL;
1256
1257 ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
1258 of_node_put(np);
1259 if (ret)
1260 return -EINVAL;
1261
1262 Hz = *kHz * 1000;
1263 opp = dev_pm_opp_find_freq_ceil(dev, &Hz);
1264 if (IS_ERR(opp))
1265 return -EINVAL;
1266
1267 mV = dev_pm_opp_get_voltage(opp) / 1000;
1268 dev_pm_opp_put(opp);
1269 if (!mV)
1270 return -EINVAL;
1271
1272 tmp = (u64)cap * mV * mV * (Hz / 1000000);
1273 do_div(tmp, 1000000000);
1274
1275 *mW = (unsigned long)tmp;
1276 *kHz = Hz / 1000;
1277
1278 return 0;
1279 }
1280
1281 /**
1282 * dev_pm_opp_of_register_em() - Attempt to register an Energy Model
1283 * @dev : Device for which an Energy Model has to be registered
1284 * @cpus : CPUs for which an Energy Model has to be registered. For
1285 * other type of devices it should be set to NULL.
1286 *
1287 * This checks whether the "dynamic-power-coefficient" devicetree property has
1288 * been specified, and tries to register an Energy Model with it if it has.
1289 * Having this property means the voltages are known for OPPs and the EM
1290 * might be calculated.
1291 */
dev_pm_opp_of_register_em(struct device * dev,struct cpumask * cpus)1292 int dev_pm_opp_of_register_em(struct device *dev, struct cpumask *cpus)
1293 {
1294 struct em_data_callback em_cb = EM_DATA_CB(_get_power);
1295 struct device_node *np;
1296 int ret, nr_opp;
1297 u32 cap;
1298
1299 if (IS_ERR_OR_NULL(dev)) {
1300 ret = -EINVAL;
1301 goto failed;
1302 }
1303
1304 nr_opp = dev_pm_opp_get_opp_count(dev);
1305 if (nr_opp <= 0) {
1306 ret = -EINVAL;
1307 goto failed;
1308 }
1309
1310 np = of_node_get(dev->of_node);
1311 if (!np) {
1312 ret = -EINVAL;
1313 goto failed;
1314 }
1315
1316 /*
1317 * Register an EM only if the 'dynamic-power-coefficient' property is
1318 * set in devicetree. It is assumed the voltage values are known if that
1319 * property is set since it is useless otherwise. If voltages are not
1320 * known, just let the EM registration fail with an error to alert the
1321 * user about the inconsistent configuration.
1322 */
1323 ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
1324 of_node_put(np);
1325 if (ret || !cap) {
1326 dev_dbg(dev, "Couldn't find proper 'dynamic-power-coefficient' in DT\n");
1327 ret = -EINVAL;
1328 goto failed;
1329 }
1330
1331 ret = em_dev_register_perf_domain(dev, nr_opp, &em_cb, cpus, true);
1332 if (ret)
1333 goto failed;
1334
1335 return 0;
1336
1337 failed:
1338 dev_dbg(dev, "Couldn't register Energy Model %d\n", ret);
1339 return ret;
1340 }
1341 EXPORT_SYMBOL_GPL(dev_pm_opp_of_register_em);
1342