blob: 6a6e6e7846b3a0391ca5a9c90e1cf3f6fd107662 [file] [log] [blame]
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301/*
2 * Generic OPP OF helpers
3 *
4 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
5 * Nishanth Menon
6 * Romit Dasgupta
7 * Kevin Hilman
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
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.h>
Viresh Kumardfbe4672016-12-01 16:28:19 +053020#include <linux/slab.h>
Viresh Kumarf47b72a2016-05-05 16:20:33 +053021#include <linux/export.h>
22
23#include "opp.h"
24
25static struct opp_table *_managed_opp(const struct device_node *np)
26{
Viresh Kumarb83c1892017-01-23 10:11:45 +053027 struct opp_table *opp_table, *managed_table = NULL;
28
29 mutex_lock(&opp_table_lock);
Viresh Kumarf47b72a2016-05-05 16:20:33 +053030
31 list_for_each_entry_rcu(opp_table, &opp_tables, node) {
32 if (opp_table->np == np) {
33 /*
34 * Multiple devices can point to the same OPP table and
35 * so will have same node-pointer, np.
36 *
37 * But the OPPs will be considered as shared only if the
38 * OPP table contains a "opp-shared" property.
39 */
Viresh Kumarb83c1892017-01-23 10:11:45 +053040 if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {
41 _get_opp_table_kref(opp_table);
42 managed_table = opp_table;
43 }
Viresh Kumar79ee2e82016-06-16 19:03:11 +053044
Viresh Kumarb83c1892017-01-23 10:11:45 +053045 break;
Viresh Kumarf47b72a2016-05-05 16:20:33 +053046 }
47 }
48
Viresh Kumarb83c1892017-01-23 10:11:45 +053049 mutex_unlock(&opp_table_lock);
50
51 return managed_table;
Viresh Kumarf47b72a2016-05-05 16:20:33 +053052}
53
54void _of_init_opp_table(struct opp_table *opp_table, struct device *dev)
55{
56 struct device_node *np;
57
58 /*
59 * Only required for backward compatibility with v1 bindings, but isn't
60 * harmful for other cases. And so we do it unconditionally.
61 */
62 np = of_node_get(dev->of_node);
63 if (np) {
64 u32 val;
65
66 if (!of_property_read_u32(np, "clock-latency", &val))
67 opp_table->clock_latency_ns_max = val;
68 of_property_read_u32(np, "voltage-tolerance",
69 &opp_table->voltage_tolerance_v1);
70 of_node_put(np);
71 }
72}
73
74static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
75 struct device_node *np)
76{
77 unsigned int count = opp_table->supported_hw_count;
78 u32 version;
79 int ret;
80
Dave Gerlacha4ee4542016-09-23 15:07:47 -050081 if (!opp_table->supported_hw) {
82 /*
83 * In the case that no supported_hw has been set by the
84 * platform but there is an opp-supported-hw value set for
85 * an OPP then the OPP should not be enabled as there is
86 * no way to see if the hardware supports it.
87 */
88 if (of_find_property(np, "opp-supported-hw", NULL))
89 return false;
90 else
91 return true;
92 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +053093
94 while (count--) {
95 ret = of_property_read_u32_index(np, "opp-supported-hw", count,
96 &version);
97 if (ret) {
98 dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
99 __func__, count, ret);
100 return false;
101 }
102
103 /* Both of these are bitwise masks of the versions */
104 if (!(version & opp_table->supported_hw[count]))
105 return false;
106 }
107
108 return true;
109}
110
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530111static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
112 struct opp_table *opp_table)
113{
Viresh Kumardfbe4672016-12-01 16:28:19 +0530114 u32 *microvolt, *microamp = NULL;
115 int supplies, vcount, icount, ret, i, j;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530116 struct property *prop = NULL;
117 char name[NAME_MAX];
118
Viresh Kumardfbe4672016-12-01 16:28:19 +0530119 supplies = opp_table->regulator_count ? opp_table->regulator_count : 1;
120
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530121 /* Search for "opp-microvolt-<name>" */
122 if (opp_table->prop_name) {
123 snprintf(name, sizeof(name), "opp-microvolt-%s",
124 opp_table->prop_name);
125 prop = of_find_property(opp->np, name, NULL);
126 }
127
128 if (!prop) {
129 /* Search for "opp-microvolt" */
130 sprintf(name, "opp-microvolt");
131 prop = of_find_property(opp->np, name, NULL);
132
133 /* Missing property isn't a problem, but an invalid entry is */
134 if (!prop)
135 return 0;
136 }
137
Viresh Kumardfbe4672016-12-01 16:28:19 +0530138 vcount = of_property_count_u32_elems(opp->np, name);
139 if (vcount < 0) {
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530140 dev_err(dev, "%s: Invalid %s property (%d)\n",
Viresh Kumardfbe4672016-12-01 16:28:19 +0530141 __func__, name, vcount);
142 return vcount;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530143 }
144
Viresh Kumardfbe4672016-12-01 16:28:19 +0530145 /* There can be one or three elements per supply */
146 if (vcount != supplies && vcount != supplies * 3) {
147 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
148 __func__, name, vcount, supplies);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530149 return -EINVAL;
150 }
151
Viresh Kumardfbe4672016-12-01 16:28:19 +0530152 microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL);
153 if (!microvolt)
154 return -ENOMEM;
155
156 ret = of_property_read_u32_array(opp->np, name, microvolt, vcount);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530157 if (ret) {
158 dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530159 ret = -EINVAL;
160 goto free_microvolt;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530161 }
162
163 /* Search for "opp-microamp-<name>" */
164 prop = NULL;
165 if (opp_table->prop_name) {
166 snprintf(name, sizeof(name), "opp-microamp-%s",
167 opp_table->prop_name);
168 prop = of_find_property(opp->np, name, NULL);
169 }
170
171 if (!prop) {
172 /* Search for "opp-microamp" */
173 sprintf(name, "opp-microamp");
174 prop = of_find_property(opp->np, name, NULL);
175 }
176
Viresh Kumardfbe4672016-12-01 16:28:19 +0530177 if (prop) {
178 icount = of_property_count_u32_elems(opp->np, name);
179 if (icount < 0) {
180 dev_err(dev, "%s: Invalid %s property (%d)\n", __func__,
181 name, icount);
182 ret = icount;
183 goto free_microvolt;
184 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530185
Viresh Kumardfbe4672016-12-01 16:28:19 +0530186 if (icount != supplies) {
187 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
188 __func__, name, icount, supplies);
189 ret = -EINVAL;
190 goto free_microvolt;
191 }
192
193 microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL);
194 if (!microamp) {
195 ret = -EINVAL;
196 goto free_microvolt;
197 }
198
199 ret = of_property_read_u32_array(opp->np, name, microamp,
200 icount);
201 if (ret) {
202 dev_err(dev, "%s: error parsing %s: %d\n", __func__,
203 name, ret);
204 ret = -EINVAL;
205 goto free_microamp;
206 }
207 }
208
209 for (i = 0, j = 0; i < supplies; i++) {
210 opp->supplies[i].u_volt = microvolt[j++];
211
212 if (vcount == supplies) {
213 opp->supplies[i].u_volt_min = opp->supplies[i].u_volt;
214 opp->supplies[i].u_volt_max = opp->supplies[i].u_volt;
215 } else {
216 opp->supplies[i].u_volt_min = microvolt[j++];
217 opp->supplies[i].u_volt_max = microvolt[j++];
218 }
219
220 if (microamp)
221 opp->supplies[i].u_amp = microamp[i];
222 }
223
224free_microamp:
225 kfree(microamp);
226free_microvolt:
227 kfree(microvolt);
228
229 return ret;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530230}
231
232/**
233 * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
234 * entries
235 * @dev: device pointer used to lookup OPP table.
236 *
237 * Free OPPs created using static entries present in DT.
238 *
239 * Locking: The internal opp_table and opp structures are RCU protected.
240 * Hence this function indirectly uses RCU updater strategy with mutex locks
241 * to keep the integrity of the internal data structures. Callers should ensure
242 * that this function is *NOT* called under RCU protection or in contexts where
243 * mutex cannot be locked.
244 */
245void dev_pm_opp_of_remove_table(struct device *dev)
246{
Viresh Kumar9274c892017-01-02 14:41:00 +0530247 _dev_pm_opp_find_and_remove_table(dev, false);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530248}
249EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
250
251/* Returns opp descriptor node for a device, caller must do of_node_put() */
Masahiro Yamada4bee77d2016-10-20 15:44:53 +0900252static struct device_node *_of_get_opp_desc_node(struct device *dev)
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530253{
254 /*
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530255 * There should be only ONE phandle present in "operating-points-v2"
256 * property.
257 */
258
259 return of_parse_phandle(dev->of_node, "operating-points-v2", 0);
260}
261
262/**
263 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530264 * @opp_table: OPP table
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530265 * @dev: device for which we do this operation
266 * @np: device node
267 *
268 * This function adds an opp definition to the opp table and returns status. The
269 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
270 * removed by dev_pm_opp_remove.
271 *
272 * Locking: The internal opp_table and opp structures are RCU protected.
273 * Hence this function internally uses RCU updater strategy with mutex locks
274 * to keep the integrity of the internal data structures. Callers should ensure
275 * that this function is *NOT* called under RCU protection or in contexts where
276 * mutex cannot be locked.
277 *
278 * Return:
279 * 0 On success OR
280 * Duplicate OPPs (both freq and volt are same) and opp->available
281 * -EEXIST Freq are same and volt are different OR
282 * Duplicate OPPs (both freq and volt are same) and !opp->available
283 * -ENOMEM Memory allocation failure
284 * -EINVAL Failed parsing the OPP node
285 */
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530286static int _opp_add_static_v2(struct opp_table *opp_table, struct device *dev,
287 struct device_node *np)
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530288{
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530289 struct dev_pm_opp *new_opp;
290 u64 rate;
291 u32 val;
292 int ret;
293
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530294 new_opp = _opp_allocate(opp_table);
295 if (!new_opp)
296 return -ENOMEM;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530297
298 ret = of_property_read_u64(np, "opp-hz", &rate);
299 if (ret < 0) {
300 dev_err(dev, "%s: opp-hz not found\n", __func__);
301 goto free_opp;
302 }
303
304 /* Check if the OPP supports hardware's hierarchy of versions or not */
305 if (!_opp_is_supported(dev, opp_table, np)) {
306 dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate);
307 goto free_opp;
308 }
309
310 /*
311 * Rate is defined as an unsigned long in clk API, and so casting
312 * explicitly to its type. Must be fixed once rate is 64 bit
313 * guaranteed in clk API.
314 */
315 new_opp->rate = (unsigned long)rate;
316 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
317
318 new_opp->np = np;
319 new_opp->dynamic = false;
320 new_opp->available = true;
321
322 if (!of_property_read_u32(np, "clock-latency-ns", &val))
323 new_opp->clock_latency_ns = val;
324
325 ret = opp_parse_supplies(new_opp, dev, opp_table);
326 if (ret)
327 goto free_opp;
328
329 ret = _opp_add(dev, new_opp, opp_table);
Viresh Kumar7f8538e2017-01-02 14:40:55 +0530330 if (ret) {
331 /* Don't return error for duplicate OPPs */
332 if (ret == -EBUSY)
333 ret = 0;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530334 goto free_opp;
Viresh Kumar7f8538e2017-01-02 14:40:55 +0530335 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530336
337 /* OPP to select on device suspend */
338 if (of_property_read_bool(np, "opp-suspend")) {
339 if (opp_table->suspend_opp) {
340 dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
341 __func__, opp_table->suspend_opp->rate,
342 new_opp->rate);
343 } else {
344 new_opp->suspend = true;
345 opp_table->suspend_opp = new_opp;
346 }
347 }
348
349 if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
350 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
351
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530352 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
Viresh Kumar0f0fe7e2016-12-01 16:28:17 +0530353 __func__, new_opp->turbo, new_opp->rate,
Viresh Kumardfbe4672016-12-01 16:28:19 +0530354 new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
355 new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530356
357 /*
358 * Notify the changes in the availability of the operable
359 * frequency/voltage list.
360 */
361 srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_ADD, new_opp);
362 return 0;
363
364free_opp:
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530365 _opp_free(new_opp);
366
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530367 return ret;
368}
369
370/* Initializes OPP tables based on new bindings */
371static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np)
372{
373 struct device_node *np;
374 struct opp_table *opp_table;
375 int ret = 0, count = 0;
376
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530377 opp_table = _managed_opp(opp_np);
378 if (opp_table) {
379 /* OPPs are already managed */
380 if (!_add_opp_dev(dev, opp_table))
381 ret = -ENOMEM;
Viresh Kumarb83c1892017-01-23 10:11:45 +0530382 goto put_opp_table;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530383 }
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530384
Viresh Kumarb83c1892017-01-23 10:11:45 +0530385 opp_table = dev_pm_opp_get_opp_table(dev);
386 if (!opp_table)
387 return -ENOMEM;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530388
389 /* We have opp-table node now, iterate over it and add OPPs */
390 for_each_available_child_of_node(opp_np, np) {
391 count++;
392
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530393 ret = _opp_add_static_v2(opp_table, dev, np);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530394 if (ret) {
395 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
396 ret);
Viresh Kumarb83c1892017-01-23 10:11:45 +0530397 _dev_pm_opp_remove_table(opp_table, dev, false);
398 goto put_opp_table;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530399 }
400 }
401
402 /* There should be one of more OPP defined */
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530403 if (WARN_ON(!count)) {
404 ret = -ENOENT;
Viresh Kumarb83c1892017-01-23 10:11:45 +0530405 goto put_opp_table;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530406 }
407
408 opp_table->np = opp_np;
Viresh Kumar79ee2e82016-06-16 19:03:11 +0530409 if (of_property_read_bool(opp_np, "opp-shared"))
410 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
411 else
412 opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530413
Viresh Kumarb83c1892017-01-23 10:11:45 +0530414put_opp_table:
415 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530416
417 return ret;
418}
419
420/* Initializes OPP tables based on old-deprecated bindings */
421static int _of_add_opp_table_v1(struct device *dev)
422{
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530423 struct opp_table *opp_table;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530424 const struct property *prop;
425 const __be32 *val;
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530426 int nr, ret = 0;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530427
428 prop = of_find_property(dev->of_node, "operating-points", NULL);
429 if (!prop)
430 return -ENODEV;
431 if (!prop->value)
432 return -ENODATA;
433
434 /*
435 * Each OPP is a set of tuples consisting of frequency and
436 * voltage like <freq-kHz vol-uV>.
437 */
438 nr = prop->length / sizeof(u32);
439 if (nr % 2) {
440 dev_err(dev, "%s: Invalid OPP table\n", __func__);
441 return -EINVAL;
442 }
443
Viresh Kumarb83c1892017-01-23 10:11:45 +0530444 opp_table = dev_pm_opp_get_opp_table(dev);
445 if (!opp_table)
446 return -ENOMEM;
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530447
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530448 val = prop->value;
449 while (nr) {
450 unsigned long freq = be32_to_cpup(val++) * 1000;
451 unsigned long volt = be32_to_cpup(val++);
452
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530453 ret = _opp_add_v1(opp_table, dev, freq, volt, false);
Viresh Kumar04a86a82017-01-02 14:40:58 +0530454 if (ret) {
455 dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
456 __func__, freq, ret);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530457 _dev_pm_opp_remove_table(opp_table, dev, false);
458 break;
Viresh Kumar04a86a82017-01-02 14:40:58 +0530459 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530460 nr -= 2;
461 }
462
Viresh Kumarb83c1892017-01-23 10:11:45 +0530463 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530464 return ret;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530465}
466
467/**
468 * dev_pm_opp_of_add_table() - Initialize opp table from device tree
469 * @dev: device pointer used to lookup OPP table.
470 *
471 * Register the initial OPP table with the OPP library for given device.
472 *
473 * Locking: The internal opp_table and opp structures are RCU protected.
474 * Hence this function indirectly uses RCU updater strategy with mutex locks
475 * to keep the integrity of the internal data structures. Callers should ensure
476 * that this function is *NOT* called under RCU protection or in contexts where
477 * mutex cannot be locked.
478 *
479 * Return:
480 * 0 On success OR
481 * Duplicate OPPs (both freq and volt are same) and opp->available
482 * -EEXIST Freq are same and volt are different OR
483 * Duplicate OPPs (both freq and volt are same) and !opp->available
484 * -ENOMEM Memory allocation failure
485 * -ENODEV when 'operating-points' property is not found or is invalid data
486 * in device node.
487 * -ENODATA when empty 'operating-points' property is found
488 * -EINVAL when invalid entries are found in opp-v2 table
489 */
490int dev_pm_opp_of_add_table(struct device *dev)
491{
492 struct device_node *opp_np;
493 int ret;
494
495 /*
496 * OPPs have two version of bindings now. The older one is deprecated,
497 * try for the new binding first.
498 */
499 opp_np = _of_get_opp_desc_node(dev);
500 if (!opp_np) {
501 /*
502 * Try old-deprecated bindings for backward compatibility with
503 * older dtbs.
504 */
505 return _of_add_opp_table_v1(dev);
506 }
507
508 ret = _of_add_opp_table_v2(dev, opp_np);
509 of_node_put(opp_np);
510
511 return ret;
512}
513EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
514
515/* CPU device specific helpers */
516
517/**
518 * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
519 * @cpumask: cpumask for which OPP table needs to be removed
520 *
521 * This removes the OPP tables for CPUs present in the @cpumask.
522 * This should be used only to remove static entries created from DT.
523 *
524 * Locking: The internal opp_table and opp structures are RCU protected.
525 * Hence this function internally uses RCU updater strategy with mutex locks
526 * to keep the integrity of the internal data structures. Callers should ensure
527 * that this function is *NOT* called under RCU protection or in contexts where
528 * mutex cannot be locked.
529 */
530void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
531{
532 _dev_pm_opp_cpumask_remove_table(cpumask, true);
533}
534EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
535
536/**
537 * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
538 * @cpumask: cpumask for which OPP table needs to be added.
539 *
540 * This adds the OPP tables for CPUs present in the @cpumask.
541 *
542 * Locking: The internal opp_table and opp structures are RCU protected.
543 * Hence this function internally uses RCU updater strategy with mutex locks
544 * to keep the integrity of the internal data structures. Callers should ensure
545 * that this function is *NOT* called under RCU protection or in contexts where
546 * mutex cannot be locked.
547 */
548int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
549{
550 struct device *cpu_dev;
551 int cpu, ret = 0;
552
553 WARN_ON(cpumask_empty(cpumask));
554
555 for_each_cpu(cpu, cpumask) {
556 cpu_dev = get_cpu_device(cpu);
557 if (!cpu_dev) {
558 pr_err("%s: failed to get cpu%d device\n", __func__,
559 cpu);
560 continue;
561 }
562
563 ret = dev_pm_opp_of_add_table(cpu_dev);
564 if (ret) {
565 pr_err("%s: couldn't find opp table for cpu:%d, %d\n",
566 __func__, cpu, ret);
567
568 /* Free all other OPPs */
569 dev_pm_opp_of_cpumask_remove_table(cpumask);
570 break;
571 }
572 }
573
574 return ret;
575}
576EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
577
578/*
579 * Works only for OPP v2 bindings.
580 *
581 * Returns -ENOENT if operating-points-v2 bindings aren't supported.
582 */
583/**
584 * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
585 * @cpu_dev using operating-points-v2
586 * bindings.
587 *
588 * @cpu_dev: CPU device for which we do this operation
589 * @cpumask: cpumask to update with information of sharing CPUs
590 *
591 * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
592 *
593 * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
594 *
595 * Locking: The internal opp_table and opp structures are RCU protected.
596 * Hence this function internally uses RCU updater strategy with mutex locks
597 * to keep the integrity of the internal data structures. Callers should ensure
598 * that this function is *NOT* called under RCU protection or in contexts where
599 * mutex cannot be locked.
600 */
601int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
602 struct cpumask *cpumask)
603{
604 struct device_node *np, *tmp_np;
605 struct device *tcpu_dev;
606 int cpu, ret = 0;
607
608 /* Get OPP descriptor node */
609 np = _of_get_opp_desc_node(cpu_dev);
610 if (!np) {
Masahiro Yamada349aa922016-10-20 16:12:49 +0900611 dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530612 return -ENOENT;
613 }
614
615 cpumask_set_cpu(cpu_dev->id, cpumask);
616
617 /* OPPs are shared ? */
618 if (!of_property_read_bool(np, "opp-shared"))
619 goto put_cpu_node;
620
621 for_each_possible_cpu(cpu) {
622 if (cpu == cpu_dev->id)
623 continue;
624
625 tcpu_dev = get_cpu_device(cpu);
626 if (!tcpu_dev) {
627 dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
628 __func__, cpu);
629 ret = -ENODEV;
630 goto put_cpu_node;
631 }
632
633 /* Get OPP descriptor node */
634 tmp_np = _of_get_opp_desc_node(tcpu_dev);
635 if (!tmp_np) {
Masahiro Yamada349aa922016-10-20 16:12:49 +0900636 dev_err(tcpu_dev, "%s: Couldn't find opp node.\n",
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530637 __func__);
638 ret = -ENOENT;
639 goto put_cpu_node;
640 }
641
642 /* CPUs are sharing opp node */
643 if (np == tmp_np)
644 cpumask_set_cpu(cpu, cpumask);
645
646 of_node_put(tmp_np);
647 }
648
649put_cpu_node:
650 of_node_put(np);
651 return ret;
652}
653EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);