0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0012
0013 #include <linux/cpu.h>
0014 #include <linux/errno.h>
0015 #include <linux/device.h>
0016 #include <linux/of_device.h>
0017 #include <linux/pm_domain.h>
0018 #include <linux/slab.h>
0019 #include <linux/export.h>
0020 #include <linux/energy_model.h>
0021
0022 #include "opp.h"
0023
0024
0025
0026
0027
0028 static struct device_node *_opp_of_get_opp_desc_node(struct device_node *np,
0029 int index)
0030 {
0031
0032 return of_parse_phandle(np, "operating-points-v2", index);
0033 }
0034
0035
0036 struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev)
0037 {
0038 return _opp_of_get_opp_desc_node(dev->of_node, 0);
0039 }
0040 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node);
0041
0042 struct opp_table *_managed_opp(struct device *dev, int index)
0043 {
0044 struct opp_table *opp_table, *managed_table = NULL;
0045 struct device_node *np;
0046
0047 np = _opp_of_get_opp_desc_node(dev->of_node, index);
0048 if (!np)
0049 return NULL;
0050
0051 list_for_each_entry(opp_table, &opp_tables, node) {
0052 if (opp_table->np == np) {
0053
0054
0055
0056
0057
0058
0059
0060 if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {
0061 _get_opp_table_kref(opp_table);
0062 managed_table = opp_table;
0063 }
0064
0065 break;
0066 }
0067 }
0068
0069 of_node_put(np);
0070
0071 return managed_table;
0072 }
0073
0074
0075 static struct dev_pm_opp *_find_opp_of_np(struct opp_table *opp_table,
0076 struct device_node *opp_np)
0077 {
0078 struct dev_pm_opp *opp;
0079
0080 mutex_lock(&opp_table->lock);
0081
0082 list_for_each_entry(opp, &opp_table->opp_list, node) {
0083 if (opp->np == opp_np) {
0084 dev_pm_opp_get(opp);
0085 mutex_unlock(&opp_table->lock);
0086 return opp;
0087 }
0088 }
0089
0090 mutex_unlock(&opp_table->lock);
0091
0092 return NULL;
0093 }
0094
0095 static struct device_node *of_parse_required_opp(struct device_node *np,
0096 int index)
0097 {
0098 return of_parse_phandle(np, "required-opps", index);
0099 }
0100
0101
0102 static struct opp_table *_find_table_of_opp_np(struct device_node *opp_np)
0103 {
0104 struct opp_table *opp_table;
0105 struct device_node *opp_table_np;
0106
0107 opp_table_np = of_get_parent(opp_np);
0108 if (!opp_table_np)
0109 goto err;
0110
0111
0112 of_node_put(opp_table_np);
0113
0114 mutex_lock(&opp_table_lock);
0115 list_for_each_entry(opp_table, &opp_tables, node) {
0116 if (opp_table_np == opp_table->np) {
0117 _get_opp_table_kref(opp_table);
0118 mutex_unlock(&opp_table_lock);
0119 return opp_table;
0120 }
0121 }
0122 mutex_unlock(&opp_table_lock);
0123
0124 err:
0125 return ERR_PTR(-ENODEV);
0126 }
0127
0128
0129 static void _opp_table_free_required_tables(struct opp_table *opp_table)
0130 {
0131 struct opp_table **required_opp_tables = opp_table->required_opp_tables;
0132 int i;
0133
0134 if (!required_opp_tables)
0135 return;
0136
0137 for (i = 0; i < opp_table->required_opp_count; i++) {
0138 if (IS_ERR_OR_NULL(required_opp_tables[i]))
0139 continue;
0140
0141 dev_pm_opp_put_opp_table(required_opp_tables[i]);
0142 }
0143
0144 kfree(required_opp_tables);
0145
0146 opp_table->required_opp_count = 0;
0147 opp_table->required_opp_tables = NULL;
0148 list_del(&opp_table->lazy);
0149 }
0150
0151
0152
0153
0154
0155 static void _opp_table_alloc_required_tables(struct opp_table *opp_table,
0156 struct device *dev,
0157 struct device_node *opp_np)
0158 {
0159 struct opp_table **required_opp_tables;
0160 struct device_node *required_np, *np;
0161 bool lazy = false;
0162 int count, i;
0163
0164
0165 np = of_get_next_available_child(opp_np, NULL);
0166 if (!np) {
0167 dev_warn(dev, "Empty OPP table\n");
0168
0169 return;
0170 }
0171
0172 count = of_count_phandle_with_args(np, "required-opps", NULL);
0173 if (count <= 0)
0174 goto put_np;
0175
0176 required_opp_tables = kcalloc(count, sizeof(*required_opp_tables),
0177 GFP_KERNEL);
0178 if (!required_opp_tables)
0179 goto put_np;
0180
0181 opp_table->required_opp_tables = required_opp_tables;
0182 opp_table->required_opp_count = count;
0183
0184 for (i = 0; i < count; i++) {
0185 required_np = of_parse_required_opp(np, i);
0186 if (!required_np)
0187 goto free_required_tables;
0188
0189 required_opp_tables[i] = _find_table_of_opp_np(required_np);
0190 of_node_put(required_np);
0191
0192 if (IS_ERR(required_opp_tables[i]))
0193 lazy = true;
0194 }
0195
0196
0197 if (lazy)
0198 list_add(&opp_table->lazy, &lazy_opp_tables);
0199
0200 goto put_np;
0201
0202 free_required_tables:
0203 _opp_table_free_required_tables(opp_table);
0204 put_np:
0205 of_node_put(np);
0206 }
0207
0208 void _of_init_opp_table(struct opp_table *opp_table, struct device *dev,
0209 int index)
0210 {
0211 struct device_node *np, *opp_np;
0212 u32 val;
0213
0214
0215
0216
0217
0218 np = of_node_get(dev->of_node);
0219 if (!np)
0220 return;
0221
0222 if (!of_property_read_u32(np, "clock-latency", &val))
0223 opp_table->clock_latency_ns_max = val;
0224 of_property_read_u32(np, "voltage-tolerance",
0225 &opp_table->voltage_tolerance_v1);
0226
0227 if (of_find_property(np, "#power-domain-cells", NULL))
0228 opp_table->is_genpd = true;
0229
0230
0231 opp_np = _opp_of_get_opp_desc_node(np, index);
0232 of_node_put(np);
0233
0234 if (!opp_np)
0235 return;
0236
0237 if (of_property_read_bool(opp_np, "opp-shared"))
0238 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
0239 else
0240 opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
0241
0242 opp_table->np = opp_np;
0243
0244 _opp_table_alloc_required_tables(opp_table, dev, opp_np);
0245 }
0246
0247 void _of_clear_opp_table(struct opp_table *opp_table)
0248 {
0249 _opp_table_free_required_tables(opp_table);
0250 of_node_put(opp_table->np);
0251 }
0252
0253
0254
0255
0256
0257 static void _of_opp_free_required_opps(struct opp_table *opp_table,
0258 struct dev_pm_opp *opp)
0259 {
0260 struct dev_pm_opp **required_opps = opp->required_opps;
0261 int i;
0262
0263 if (!required_opps)
0264 return;
0265
0266 for (i = 0; i < opp_table->required_opp_count; i++) {
0267 if (!required_opps[i])
0268 continue;
0269
0270
0271 dev_pm_opp_put(required_opps[i]);
0272 }
0273
0274 opp->required_opps = NULL;
0275 kfree(required_opps);
0276 }
0277
0278 void _of_clear_opp(struct opp_table *opp_table, struct dev_pm_opp *opp)
0279 {
0280 _of_opp_free_required_opps(opp_table, opp);
0281 of_node_put(opp->np);
0282 }
0283
0284
0285 static int _of_opp_alloc_required_opps(struct opp_table *opp_table,
0286 struct dev_pm_opp *opp)
0287 {
0288 struct dev_pm_opp **required_opps;
0289 struct opp_table *required_table;
0290 struct device_node *np;
0291 int i, ret, count = opp_table->required_opp_count;
0292
0293 if (!count)
0294 return 0;
0295
0296 required_opps = kcalloc(count, sizeof(*required_opps), GFP_KERNEL);
0297 if (!required_opps)
0298 return -ENOMEM;
0299
0300 opp->required_opps = required_opps;
0301
0302 for (i = 0; i < count; i++) {
0303 required_table = opp_table->required_opp_tables[i];
0304
0305
0306 if (IS_ERR_OR_NULL(required_table))
0307 continue;
0308
0309 np = of_parse_required_opp(opp->np, i);
0310 if (unlikely(!np)) {
0311 ret = -ENODEV;
0312 goto free_required_opps;
0313 }
0314
0315 required_opps[i] = _find_opp_of_np(required_table, np);
0316 of_node_put(np);
0317
0318 if (!required_opps[i]) {
0319 pr_err("%s: Unable to find required OPP node: %pOF (%d)\n",
0320 __func__, opp->np, i);
0321 ret = -ENODEV;
0322 goto free_required_opps;
0323 }
0324 }
0325
0326 return 0;
0327
0328 free_required_opps:
0329 _of_opp_free_required_opps(opp_table, opp);
0330
0331 return ret;
0332 }
0333
0334
0335 static int lazy_link_required_opps(struct opp_table *opp_table,
0336 struct opp_table *new_table, int index)
0337 {
0338 struct device_node *required_np;
0339 struct dev_pm_opp *opp;
0340
0341 list_for_each_entry(opp, &opp_table->opp_list, node) {
0342 required_np = of_parse_required_opp(opp->np, index);
0343 if (unlikely(!required_np))
0344 return -ENODEV;
0345
0346 opp->required_opps[index] = _find_opp_of_np(new_table, required_np);
0347 of_node_put(required_np);
0348
0349 if (!opp->required_opps[index]) {
0350 pr_err("%s: Unable to find required OPP node: %pOF (%d)\n",
0351 __func__, opp->np, index);
0352 return -ENODEV;
0353 }
0354 }
0355
0356 return 0;
0357 }
0358
0359
0360 static void lazy_link_required_opp_table(struct opp_table *new_table)
0361 {
0362 struct opp_table *opp_table, *temp, **required_opp_tables;
0363 struct device_node *required_np, *opp_np, *required_table_np;
0364 struct dev_pm_opp *opp;
0365 int i, ret;
0366
0367 mutex_lock(&opp_table_lock);
0368
0369 list_for_each_entry_safe(opp_table, temp, &lazy_opp_tables, lazy) {
0370 bool lazy = false;
0371
0372
0373 opp_np = of_get_next_available_child(opp_table->np, NULL);
0374
0375 for (i = 0; i < opp_table->required_opp_count; i++) {
0376 required_opp_tables = opp_table->required_opp_tables;
0377
0378
0379 if (!IS_ERR(required_opp_tables[i]))
0380 continue;
0381
0382
0383 required_np = of_parse_required_opp(opp_np, i);
0384 required_table_np = of_get_parent(required_np);
0385
0386 of_node_put(required_table_np);
0387 of_node_put(required_np);
0388
0389
0390
0391
0392
0393 if (required_table_np != new_table->np) {
0394 lazy = true;
0395 continue;
0396 }
0397
0398 required_opp_tables[i] = new_table;
0399 _get_opp_table_kref(new_table);
0400
0401
0402 ret = lazy_link_required_opps(opp_table, new_table, i);
0403 if (ret) {
0404
0405 lazy = false;
0406 break;
0407 }
0408 }
0409
0410 of_node_put(opp_np);
0411
0412
0413 if (!lazy) {
0414 list_del_init(&opp_table->lazy);
0415
0416 list_for_each_entry(opp, &opp_table->opp_list, node)
0417 _required_opps_available(opp, opp_table->required_opp_count);
0418 }
0419 }
0420
0421 mutex_unlock(&opp_table_lock);
0422 }
0423
0424 static int _bandwidth_supported(struct device *dev, struct opp_table *opp_table)
0425 {
0426 struct device_node *np, *opp_np;
0427 struct property *prop;
0428
0429 if (!opp_table) {
0430 np = of_node_get(dev->of_node);
0431 if (!np)
0432 return -ENODEV;
0433
0434 opp_np = _opp_of_get_opp_desc_node(np, 0);
0435 of_node_put(np);
0436 } else {
0437 opp_np = of_node_get(opp_table->np);
0438 }
0439
0440
0441 if (!opp_np)
0442 return 0;
0443
0444
0445 np = of_get_next_available_child(opp_np, NULL);
0446 of_node_put(opp_np);
0447 if (!np) {
0448 dev_err(dev, "OPP table empty\n");
0449 return -EINVAL;
0450 }
0451
0452 prop = of_find_property(np, "opp-peak-kBps", NULL);
0453 of_node_put(np);
0454
0455 if (!prop || !prop->length)
0456 return 0;
0457
0458 return 1;
0459 }
0460
0461 int dev_pm_opp_of_find_icc_paths(struct device *dev,
0462 struct opp_table *opp_table)
0463 {
0464 struct device_node *np;
0465 int ret, i, count, num_paths;
0466 struct icc_path **paths;
0467
0468 ret = _bandwidth_supported(dev, opp_table);
0469 if (ret == -EINVAL)
0470 return 0;
0471 else if (ret <= 0)
0472 return ret;
0473
0474 ret = 0;
0475
0476 np = of_node_get(dev->of_node);
0477 if (!np)
0478 return 0;
0479
0480 count = of_count_phandle_with_args(np, "interconnects",
0481 "#interconnect-cells");
0482 of_node_put(np);
0483 if (count < 0)
0484 return 0;
0485
0486
0487 if (count % 2) {
0488 dev_err(dev, "%s: Invalid interconnects values\n", __func__);
0489 return -EINVAL;
0490 }
0491
0492 num_paths = count / 2;
0493 paths = kcalloc(num_paths, sizeof(*paths), GFP_KERNEL);
0494 if (!paths)
0495 return -ENOMEM;
0496
0497 for (i = 0; i < num_paths; i++) {
0498 paths[i] = of_icc_get_by_index(dev, i);
0499 if (IS_ERR(paths[i])) {
0500 ret = PTR_ERR(paths[i]);
0501 if (ret != -EPROBE_DEFER) {
0502 dev_err(dev, "%s: Unable to get path%d: %d\n",
0503 __func__, i, ret);
0504 }
0505 goto err;
0506 }
0507 }
0508
0509 if (opp_table) {
0510 opp_table->paths = paths;
0511 opp_table->path_count = num_paths;
0512 return 0;
0513 }
0514
0515 err:
0516 while (i--)
0517 icc_put(paths[i]);
0518
0519 kfree(paths);
0520
0521 return ret;
0522 }
0523 EXPORT_SYMBOL_GPL(dev_pm_opp_of_find_icc_paths);
0524
0525 static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
0526 struct device_node *np)
0527 {
0528 unsigned int levels = opp_table->supported_hw_count;
0529 int count, versions, ret, i, j;
0530 u32 val;
0531
0532 if (!opp_table->supported_hw) {
0533
0534
0535
0536
0537
0538
0539 if (of_find_property(np, "opp-supported-hw", NULL))
0540 return false;
0541 else
0542 return true;
0543 }
0544
0545 count = of_property_count_u32_elems(np, "opp-supported-hw");
0546 if (count <= 0 || count % levels) {
0547 dev_err(dev, "%s: Invalid opp-supported-hw property (%d)\n",
0548 __func__, count);
0549 return false;
0550 }
0551
0552 versions = count / levels;
0553
0554
0555 for (i = 0; i < versions; i++) {
0556 bool supported = true;
0557
0558 for (j = 0; j < levels; j++) {
0559 ret = of_property_read_u32_index(np, "opp-supported-hw",
0560 i * levels + j, &val);
0561 if (ret) {
0562 dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
0563 __func__, i * levels + j, ret);
0564 return false;
0565 }
0566
0567
0568 if (!(val & opp_table->supported_hw[j])) {
0569 supported = false;
0570 break;
0571 }
0572 }
0573
0574 if (supported)
0575 return true;
0576 }
0577
0578 return false;
0579 }
0580
0581 static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
0582 struct opp_table *opp_table)
0583 {
0584 u32 *microvolt, *microamp = NULL, *microwatt = NULL;
0585 int supplies = opp_table->regulator_count;
0586 int vcount, icount, pcount, ret, i, j;
0587 struct property *prop = NULL;
0588 char name[NAME_MAX];
0589
0590
0591 if (opp_table->prop_name) {
0592 snprintf(name, sizeof(name), "opp-microvolt-%s",
0593 opp_table->prop_name);
0594 prop = of_find_property(opp->np, name, NULL);
0595 }
0596
0597 if (!prop) {
0598
0599 sprintf(name, "opp-microvolt");
0600 prop = of_find_property(opp->np, name, NULL);
0601
0602
0603 if (!prop) {
0604 if (unlikely(supplies == -1)) {
0605
0606 opp_table->regulator_count = 0;
0607 return 0;
0608 }
0609
0610 if (!supplies)
0611 return 0;
0612
0613 dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n",
0614 __func__);
0615 return -EINVAL;
0616 }
0617 }
0618
0619 if (unlikely(supplies == -1)) {
0620
0621 supplies = opp_table->regulator_count = 1;
0622 } else if (unlikely(!supplies)) {
0623 dev_err(dev, "%s: opp-microvolt wasn't expected\n", __func__);
0624 return -EINVAL;
0625 }
0626
0627 vcount = of_property_count_u32_elems(opp->np, name);
0628 if (vcount < 0) {
0629 dev_err(dev, "%s: Invalid %s property (%d)\n",
0630 __func__, name, vcount);
0631 return vcount;
0632 }
0633
0634
0635 if (vcount != supplies && vcount != supplies * 3) {
0636 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
0637 __func__, name, vcount, supplies);
0638 return -EINVAL;
0639 }
0640
0641 microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL);
0642 if (!microvolt)
0643 return -ENOMEM;
0644
0645 ret = of_property_read_u32_array(opp->np, name, microvolt, vcount);
0646 if (ret) {
0647 dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
0648 ret = -EINVAL;
0649 goto free_microvolt;
0650 }
0651
0652
0653 prop = NULL;
0654 if (opp_table->prop_name) {
0655 snprintf(name, sizeof(name), "opp-microamp-%s",
0656 opp_table->prop_name);
0657 prop = of_find_property(opp->np, name, NULL);
0658 }
0659
0660 if (!prop) {
0661
0662 sprintf(name, "opp-microamp");
0663 prop = of_find_property(opp->np, name, NULL);
0664 }
0665
0666 if (prop) {
0667 icount = of_property_count_u32_elems(opp->np, name);
0668 if (icount < 0) {
0669 dev_err(dev, "%s: Invalid %s property (%d)\n", __func__,
0670 name, icount);
0671 ret = icount;
0672 goto free_microvolt;
0673 }
0674
0675 if (icount != supplies) {
0676 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
0677 __func__, name, icount, supplies);
0678 ret = -EINVAL;
0679 goto free_microvolt;
0680 }
0681
0682 microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL);
0683 if (!microamp) {
0684 ret = -EINVAL;
0685 goto free_microvolt;
0686 }
0687
0688 ret = of_property_read_u32_array(opp->np, name, microamp,
0689 icount);
0690 if (ret) {
0691 dev_err(dev, "%s: error parsing %s: %d\n", __func__,
0692 name, ret);
0693 ret = -EINVAL;
0694 goto free_microamp;
0695 }
0696 }
0697
0698
0699 sprintf(name, "opp-microwatt");
0700 prop = of_find_property(opp->np, name, NULL);
0701
0702 if (prop) {
0703 pcount = of_property_count_u32_elems(opp->np, name);
0704 if (pcount < 0) {
0705 dev_err(dev, "%s: Invalid %s property (%d)\n", __func__,
0706 name, pcount);
0707 ret = pcount;
0708 goto free_microamp;
0709 }
0710
0711 if (pcount != supplies) {
0712 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
0713 __func__, name, pcount, supplies);
0714 ret = -EINVAL;
0715 goto free_microamp;
0716 }
0717
0718 microwatt = kmalloc_array(pcount, sizeof(*microwatt),
0719 GFP_KERNEL);
0720 if (!microwatt) {
0721 ret = -EINVAL;
0722 goto free_microamp;
0723 }
0724
0725 ret = of_property_read_u32_array(opp->np, name, microwatt,
0726 pcount);
0727 if (ret) {
0728 dev_err(dev, "%s: error parsing %s: %d\n", __func__,
0729 name, ret);
0730 ret = -EINVAL;
0731 goto free_microwatt;
0732 }
0733 }
0734
0735 for (i = 0, j = 0; i < supplies; i++) {
0736 opp->supplies[i].u_volt = microvolt[j++];
0737
0738 if (vcount == supplies) {
0739 opp->supplies[i].u_volt_min = opp->supplies[i].u_volt;
0740 opp->supplies[i].u_volt_max = opp->supplies[i].u_volt;
0741 } else {
0742 opp->supplies[i].u_volt_min = microvolt[j++];
0743 opp->supplies[i].u_volt_max = microvolt[j++];
0744 }
0745
0746 if (microamp)
0747 opp->supplies[i].u_amp = microamp[i];
0748
0749 if (microwatt)
0750 opp->supplies[i].u_watt = microwatt[i];
0751 }
0752
0753 free_microwatt:
0754 kfree(microwatt);
0755 free_microamp:
0756 kfree(microamp);
0757 free_microvolt:
0758 kfree(microvolt);
0759
0760 return ret;
0761 }
0762
0763
0764
0765
0766
0767
0768
0769
0770 void dev_pm_opp_of_remove_table(struct device *dev)
0771 {
0772 dev_pm_opp_remove_table(dev);
0773 }
0774 EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
0775
0776 static int _read_rate(struct dev_pm_opp *new_opp, struct opp_table *opp_table,
0777 struct device_node *np)
0778 {
0779 struct property *prop;
0780 int i, count, ret;
0781 u64 *rates;
0782
0783 prop = of_find_property(np, "opp-hz", NULL);
0784 if (!prop)
0785 return -ENODEV;
0786
0787 count = prop->length / sizeof(u64);
0788 if (opp_table->clk_count != count) {
0789 pr_err("%s: Count mismatch between opp-hz and clk_count (%d %d)\n",
0790 __func__, count, opp_table->clk_count);
0791 return -EINVAL;
0792 }
0793
0794 rates = kmalloc_array(count, sizeof(*rates), GFP_KERNEL);
0795 if (!rates)
0796 return -ENOMEM;
0797
0798 ret = of_property_read_u64_array(np, "opp-hz", rates, count);
0799 if (ret) {
0800 pr_err("%s: Error parsing opp-hz: %d\n", __func__, ret);
0801 } else {
0802
0803
0804
0805
0806
0807 for (i = 0; i < count; i++) {
0808 new_opp->rates[i] = (unsigned long)rates[i];
0809
0810
0811 WARN_ON(new_opp->rates[i] != rates[i]);
0812 }
0813 }
0814
0815 kfree(rates);
0816
0817 return ret;
0818 }
0819
0820 static int _read_bw(struct dev_pm_opp *new_opp, struct opp_table *opp_table,
0821 struct device_node *np, bool peak)
0822 {
0823 const char *name = peak ? "opp-peak-kBps" : "opp-avg-kBps";
0824 struct property *prop;
0825 int i, count, ret;
0826 u32 *bw;
0827
0828 prop = of_find_property(np, name, NULL);
0829 if (!prop)
0830 return -ENODEV;
0831
0832 count = prop->length / sizeof(u32);
0833 if (opp_table->path_count != count) {
0834 pr_err("%s: Mismatch between %s and paths (%d %d)\n",
0835 __func__, name, count, opp_table->path_count);
0836 return -EINVAL;
0837 }
0838
0839 bw = kmalloc_array(count, sizeof(*bw), GFP_KERNEL);
0840 if (!bw)
0841 return -ENOMEM;
0842
0843 ret = of_property_read_u32_array(np, name, bw, count);
0844 if (ret) {
0845 pr_err("%s: Error parsing %s: %d\n", __func__, name, ret);
0846 goto out;
0847 }
0848
0849 for (i = 0; i < count; i++) {
0850 if (peak)
0851 new_opp->bandwidth[i].peak = kBps_to_icc(bw[i]);
0852 else
0853 new_opp->bandwidth[i].avg = kBps_to_icc(bw[i]);
0854 }
0855
0856 out:
0857 kfree(bw);
0858 return ret;
0859 }
0860
0861 static int _read_opp_key(struct dev_pm_opp *new_opp,
0862 struct opp_table *opp_table, struct device_node *np)
0863 {
0864 bool found = false;
0865 int ret;
0866
0867 ret = _read_rate(new_opp, opp_table, np);
0868 if (!ret)
0869 found = true;
0870 else if (ret != -ENODEV)
0871 return ret;
0872
0873
0874
0875
0876
0877
0878 ret = _read_bw(new_opp, opp_table, np, true);
0879 if (!ret) {
0880 found = true;
0881 ret = _read_bw(new_opp, opp_table, np, false);
0882 }
0883
0884
0885 if (ret && ret != -ENODEV)
0886 return ret;
0887
0888 if (!of_property_read_u32(np, "opp-level", &new_opp->level))
0889 found = true;
0890
0891 if (found)
0892 return 0;
0893
0894 return ret;
0895 }
0896
0897
0898
0899
0900
0901
0902
0903
0904
0905
0906
0907
0908
0909
0910
0911
0912
0913
0914
0915
0916
0917
0918
0919
0920
0921 static struct dev_pm_opp *_opp_add_static_v2(struct opp_table *opp_table,
0922 struct device *dev, struct device_node *np)
0923 {
0924 struct dev_pm_opp *new_opp;
0925 u32 val;
0926 int ret;
0927
0928 new_opp = _opp_allocate(opp_table);
0929 if (!new_opp)
0930 return ERR_PTR(-ENOMEM);
0931
0932 ret = _read_opp_key(new_opp, opp_table, np);
0933 if (ret < 0) {
0934 dev_err(dev, "%s: opp key field not found\n", __func__);
0935 goto free_opp;
0936 }
0937
0938
0939 if (!_opp_is_supported(dev, opp_table, np)) {
0940 dev_dbg(dev, "OPP not supported by hardware: %s\n",
0941 of_node_full_name(np));
0942 goto free_opp;
0943 }
0944
0945 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
0946
0947 new_opp->np = of_node_get(np);
0948 new_opp->dynamic = false;
0949 new_opp->available = true;
0950
0951 ret = _of_opp_alloc_required_opps(opp_table, new_opp);
0952 if (ret)
0953 goto free_opp;
0954
0955 if (!of_property_read_u32(np, "clock-latency-ns", &val))
0956 new_opp->clock_latency_ns = val;
0957
0958 ret = opp_parse_supplies(new_opp, dev, opp_table);
0959 if (ret)
0960 goto free_required_opps;
0961
0962 if (opp_table->is_genpd)
0963 new_opp->pstate = pm_genpd_opp_to_performance_state(dev, new_opp);
0964
0965 ret = _opp_add(dev, new_opp, opp_table);
0966 if (ret) {
0967
0968 if (ret == -EBUSY)
0969 ret = 0;
0970 goto free_required_opps;
0971 }
0972
0973
0974 if (of_property_read_bool(np, "opp-suspend")) {
0975 if (opp_table->suspend_opp) {
0976
0977 if (_opp_compare_key(opp_table, new_opp, opp_table->suspend_opp) == 1) {
0978 opp_table->suspend_opp->suspend = false;
0979 new_opp->suspend = true;
0980 opp_table->suspend_opp = new_opp;
0981 }
0982 } else {
0983 new_opp->suspend = true;
0984 opp_table->suspend_opp = new_opp;
0985 }
0986 }
0987
0988 if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
0989 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
0990
0991 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu level:%u\n",
0992 __func__, new_opp->turbo, new_opp->rates[0],
0993 new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
0994 new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns,
0995 new_opp->level);
0996
0997
0998
0999
1000
1001 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
1002 return new_opp;
1003
1004 free_required_opps:
1005 _of_opp_free_required_opps(opp_table, new_opp);
1006 free_opp:
1007 _opp_free(new_opp);
1008
1009 return ret ? ERR_PTR(ret) : NULL;
1010 }
1011
1012
1013 static int _of_add_opp_table_v2(struct device *dev, struct opp_table *opp_table)
1014 {
1015 struct device_node *np;
1016 int ret, count = 0;
1017 struct dev_pm_opp *opp;
1018
1019
1020 mutex_lock(&opp_table->lock);
1021 if (opp_table->parsed_static_opps) {
1022 opp_table->parsed_static_opps++;
1023 mutex_unlock(&opp_table->lock);
1024 return 0;
1025 }
1026
1027 opp_table->parsed_static_opps = 1;
1028 mutex_unlock(&opp_table->lock);
1029
1030
1031 for_each_available_child_of_node(opp_table->np, np) {
1032 opp = _opp_add_static_v2(opp_table, dev, np);
1033 if (IS_ERR(opp)) {
1034 ret = PTR_ERR(opp);
1035 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
1036 ret);
1037 of_node_put(np);
1038 goto remove_static_opp;
1039 } else if (opp) {
1040 count++;
1041 }
1042 }
1043
1044
1045 if (!count) {
1046 dev_err(dev, "%s: no supported OPPs", __func__);
1047 ret = -ENOENT;
1048 goto remove_static_opp;
1049 }
1050
1051 list_for_each_entry(opp, &opp_table->opp_list, node) {
1052
1053 if (opp->pstate) {
1054 opp_table->genpd_performance_state = true;
1055 break;
1056 }
1057 }
1058
1059 lazy_link_required_opp_table(opp_table);
1060
1061 return 0;
1062
1063 remove_static_opp:
1064 _opp_remove_all_static(opp_table);
1065
1066 return ret;
1067 }
1068
1069
1070 static int _of_add_opp_table_v1(struct device *dev, struct opp_table *opp_table)
1071 {
1072 const struct property *prop;
1073 const __be32 *val;
1074 int nr, ret = 0;
1075
1076 mutex_lock(&opp_table->lock);
1077 if (opp_table->parsed_static_opps) {
1078 opp_table->parsed_static_opps++;
1079 mutex_unlock(&opp_table->lock);
1080 return 0;
1081 }
1082
1083 opp_table->parsed_static_opps = 1;
1084 mutex_unlock(&opp_table->lock);
1085
1086 prop = of_find_property(dev->of_node, "operating-points", NULL);
1087 if (!prop) {
1088 ret = -ENODEV;
1089 goto remove_static_opp;
1090 }
1091 if (!prop->value) {
1092 ret = -ENODATA;
1093 goto remove_static_opp;
1094 }
1095
1096
1097
1098
1099
1100 nr = prop->length / sizeof(u32);
1101 if (nr % 2) {
1102 dev_err(dev, "%s: Invalid OPP table\n", __func__);
1103 ret = -EINVAL;
1104 goto remove_static_opp;
1105 }
1106
1107 val = prop->value;
1108 while (nr) {
1109 unsigned long freq = be32_to_cpup(val++) * 1000;
1110 unsigned long volt = be32_to_cpup(val++);
1111
1112 ret = _opp_add_v1(opp_table, dev, freq, volt, false);
1113 if (ret) {
1114 dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
1115 __func__, freq, ret);
1116 goto remove_static_opp;
1117 }
1118 nr -= 2;
1119 }
1120
1121 return 0;
1122
1123 remove_static_opp:
1124 _opp_remove_all_static(opp_table);
1125
1126 return ret;
1127 }
1128
1129 static int _of_add_table_indexed(struct device *dev, int index)
1130 {
1131 struct opp_table *opp_table;
1132 int ret, count;
1133
1134 if (index) {
1135
1136
1137
1138
1139 count = of_count_phandle_with_args(dev->of_node,
1140 "operating-points-v2", NULL);
1141 if (count == 1)
1142 index = 0;
1143 }
1144
1145 opp_table = _add_opp_table_indexed(dev, index, true);
1146 if (IS_ERR(opp_table))
1147 return PTR_ERR(opp_table);
1148
1149
1150
1151
1152
1153 if (opp_table->np)
1154 ret = _of_add_opp_table_v2(dev, opp_table);
1155 else
1156 ret = _of_add_opp_table_v1(dev, opp_table);
1157
1158 if (ret)
1159 dev_pm_opp_put_opp_table(opp_table);
1160
1161 return ret;
1162 }
1163
1164 static void devm_pm_opp_of_table_release(void *data)
1165 {
1166 dev_pm_opp_of_remove_table(data);
1167 }
1168
1169 static int _devm_of_add_table_indexed(struct device *dev, int index)
1170 {
1171 int ret;
1172
1173 ret = _of_add_table_indexed(dev, index);
1174 if (ret)
1175 return ret;
1176
1177 return devm_add_action_or_reset(dev, devm_pm_opp_of_table_release, dev);
1178 }
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199 int devm_pm_opp_of_add_table(struct device *dev)
1200 {
1201 return _devm_of_add_table_indexed(dev, 0);
1202 }
1203 EXPORT_SYMBOL_GPL(devm_pm_opp_of_add_table);
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222 int dev_pm_opp_of_add_table(struct device *dev)
1223 {
1224 return _of_add_table_indexed(dev, 0);
1225 }
1226 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238 int dev_pm_opp_of_add_table_indexed(struct device *dev, int index)
1239 {
1240 return _of_add_table_indexed(dev, index);
1241 }
1242 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed);
1243
1244
1245
1246
1247
1248
1249
1250
1251 int devm_pm_opp_of_add_table_indexed(struct device *dev, int index)
1252 {
1253 return _devm_of_add_table_indexed(dev, index);
1254 }
1255 EXPORT_SYMBOL_GPL(devm_pm_opp_of_add_table_indexed);
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266 void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
1267 {
1268 _dev_pm_opp_cpumask_remove_table(cpumask, -1);
1269 }
1270 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
1271
1272
1273
1274
1275
1276
1277
1278 int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
1279 {
1280 struct device *cpu_dev;
1281 int cpu, ret;
1282
1283 if (WARN_ON(cpumask_empty(cpumask)))
1284 return -ENODEV;
1285
1286 for_each_cpu(cpu, cpumask) {
1287 cpu_dev = get_cpu_device(cpu);
1288 if (!cpu_dev) {
1289 pr_err("%s: failed to get cpu%d device\n", __func__,
1290 cpu);
1291 ret = -ENODEV;
1292 goto remove_table;
1293 }
1294
1295 ret = dev_pm_opp_of_add_table(cpu_dev);
1296 if (ret) {
1297
1298
1299
1300
1301 pr_debug("%s: couldn't find opp table for cpu:%d, %d\n",
1302 __func__, cpu, ret);
1303
1304 goto remove_table;
1305 }
1306 }
1307
1308 return 0;
1309
1310 remove_table:
1311
1312 _dev_pm_opp_cpumask_remove_table(cpumask, cpu);
1313
1314 return ret;
1315 }
1316 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335 int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
1336 struct cpumask *cpumask)
1337 {
1338 struct device_node *np, *tmp_np, *cpu_np;
1339 int cpu, ret = 0;
1340
1341
1342 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
1343 if (!np) {
1344 dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
1345 return -ENOENT;
1346 }
1347
1348 cpumask_set_cpu(cpu_dev->id, cpumask);
1349
1350
1351 if (!of_property_read_bool(np, "opp-shared"))
1352 goto put_cpu_node;
1353
1354 for_each_possible_cpu(cpu) {
1355 if (cpu == cpu_dev->id)
1356 continue;
1357
1358 cpu_np = of_cpu_device_node_get(cpu);
1359 if (!cpu_np) {
1360 dev_err(cpu_dev, "%s: failed to get cpu%d node\n",
1361 __func__, cpu);
1362 ret = -ENOENT;
1363 goto put_cpu_node;
1364 }
1365
1366
1367 tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0);
1368 of_node_put(cpu_np);
1369 if (!tmp_np) {
1370 pr_err("%pOF: Couldn't find opp node\n", cpu_np);
1371 ret = -ENOENT;
1372 goto put_cpu_node;
1373 }
1374
1375
1376 if (np == tmp_np)
1377 cpumask_set_cpu(cpu, cpumask);
1378
1379 of_node_put(tmp_np);
1380 }
1381
1382 put_cpu_node:
1383 of_node_put(np);
1384 return ret;
1385 }
1386 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399 int of_get_required_opp_performance_state(struct device_node *np, int index)
1400 {
1401 struct dev_pm_opp *opp;
1402 struct device_node *required_np;
1403 struct opp_table *opp_table;
1404 int pstate = -EINVAL;
1405
1406 required_np = of_parse_required_opp(np, index);
1407 if (!required_np)
1408 return -ENODEV;
1409
1410 opp_table = _find_table_of_opp_np(required_np);
1411 if (IS_ERR(opp_table)) {
1412 pr_err("%s: Failed to find required OPP table %pOF: %ld\n",
1413 __func__, np, PTR_ERR(opp_table));
1414 goto put_required_np;
1415 }
1416
1417 opp = _find_opp_of_np(opp_table, required_np);
1418 if (opp) {
1419 pstate = opp->pstate;
1420 dev_pm_opp_put(opp);
1421 }
1422
1423 dev_pm_opp_put_opp_table(opp_table);
1424
1425 put_required_np:
1426 of_node_put(required_np);
1427
1428 return pstate;
1429 }
1430 EXPORT_SYMBOL_GPL(of_get_required_opp_performance_state);
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440 struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp)
1441 {
1442 if (IS_ERR_OR_NULL(opp)) {
1443 pr_err("%s: Invalid parameters\n", __func__);
1444 return NULL;
1445 }
1446
1447 return of_node_get(opp->np);
1448 }
1449 EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node);
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460 static int __maybe_unused
1461 _get_dt_power(struct device *dev, unsigned long *uW, unsigned long *kHz)
1462 {
1463 struct dev_pm_opp *opp;
1464 unsigned long opp_freq, opp_power;
1465
1466
1467 opp_freq = *kHz * 1000;
1468 opp = dev_pm_opp_find_freq_ceil(dev, &opp_freq);
1469 if (IS_ERR(opp))
1470 return -EINVAL;
1471
1472 opp_power = dev_pm_opp_get_power(opp);
1473 dev_pm_opp_put(opp);
1474 if (!opp_power)
1475 return -EINVAL;
1476
1477 *kHz = opp_freq / 1000;
1478 *uW = opp_power;
1479
1480 return 0;
1481 }
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495 static int __maybe_unused _get_power(struct device *dev, unsigned long *uW,
1496 unsigned long *kHz)
1497 {
1498 struct dev_pm_opp *opp;
1499 struct device_node *np;
1500 unsigned long mV, Hz;
1501 u32 cap;
1502 u64 tmp;
1503 int ret;
1504
1505 np = of_node_get(dev->of_node);
1506 if (!np)
1507 return -EINVAL;
1508
1509 ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
1510 of_node_put(np);
1511 if (ret)
1512 return -EINVAL;
1513
1514 Hz = *kHz * 1000;
1515 opp = dev_pm_opp_find_freq_ceil(dev, &Hz);
1516 if (IS_ERR(opp))
1517 return -EINVAL;
1518
1519 mV = dev_pm_opp_get_voltage(opp) / 1000;
1520 dev_pm_opp_put(opp);
1521 if (!mV)
1522 return -EINVAL;
1523
1524 tmp = (u64)cap * mV * mV * (Hz / 1000000);
1525
1526 do_div(tmp, 1000000);
1527
1528 *uW = (unsigned long)tmp;
1529 *kHz = Hz / 1000;
1530
1531 return 0;
1532 }
1533
1534 static bool _of_has_opp_microwatt_property(struct device *dev)
1535 {
1536 unsigned long power, freq = 0;
1537 struct dev_pm_opp *opp;
1538
1539
1540 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
1541 if (IS_ERR(opp))
1542 return false;
1543
1544 power = dev_pm_opp_get_power(opp);
1545 dev_pm_opp_put(opp);
1546 if (!power)
1547 return false;
1548
1549 return true;
1550 }
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563 int dev_pm_opp_of_register_em(struct device *dev, struct cpumask *cpus)
1564 {
1565 struct em_data_callback em_cb;
1566 struct device_node *np;
1567 int ret, nr_opp;
1568 u32 cap;
1569
1570 if (IS_ERR_OR_NULL(dev)) {
1571 ret = -EINVAL;
1572 goto failed;
1573 }
1574
1575 nr_opp = dev_pm_opp_get_opp_count(dev);
1576 if (nr_opp <= 0) {
1577 ret = -EINVAL;
1578 goto failed;
1579 }
1580
1581
1582 if (_of_has_opp_microwatt_property(dev)) {
1583 EM_SET_ACTIVE_POWER_CB(em_cb, _get_dt_power);
1584 goto register_em;
1585 }
1586
1587 np = of_node_get(dev->of_node);
1588 if (!np) {
1589 ret = -EINVAL;
1590 goto failed;
1591 }
1592
1593
1594
1595
1596
1597
1598
1599
1600 ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
1601 of_node_put(np);
1602 if (ret || !cap) {
1603 dev_dbg(dev, "Couldn't find proper 'dynamic-power-coefficient' in DT\n");
1604 ret = -EINVAL;
1605 goto failed;
1606 }
1607
1608 EM_SET_ACTIVE_POWER_CB(em_cb, _get_power);
1609
1610 register_em:
1611 ret = em_dev_register_perf_domain(dev, nr_opp, &em_cb, cpus, true);
1612 if (ret)
1613 goto failed;
1614
1615 return 0;
1616
1617 failed:
1618 dev_dbg(dev, "Couldn't register Energy Model %d\n", ret);
1619 return ret;
1620 }
1621 EXPORT_SYMBOL_GPL(dev_pm_opp_of_register_em);