PM / OPP: Fix static checker warning (broken 64bit big endian systems)
[linux-2.6-block.git] / drivers / base / power / opp.c
CommitLineData
e1f60b29
NM
1/*
2 * Generic OPP Interface
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
8d4d4e98 14#include <linux/cpu.h>
e1f60b29
NM
15#include <linux/kernel.h>
16#include <linux/errno.h>
17#include <linux/err.h>
e1f60b29 18#include <linux/slab.h>
51990e82 19#include <linux/device.h>
e1f60b29
NM
20#include <linux/list.h>
21#include <linux/rculist.h>
22#include <linux/rcupdate.h>
e4db1c74 23#include <linux/pm_opp.h>
b496dfbc 24#include <linux/of.h>
80126ce7 25#include <linux/export.h>
e1f60b29
NM
26
27/*
28 * Internal data structure organization with the OPP layer library is as
29 * follows:
30 * dev_opp_list (root)
31 * |- device 1 (represents voltage domain 1)
32 * | |- opp 1 (availability, freq, voltage)
33 * | |- opp 2 ..
34 * ... ...
35 * | `- opp n ..
36 * |- device 2 (represents the next voltage domain)
37 * ...
38 * `- device m (represents mth voltage domain)
39 * device 1, 2.. are represented by dev_opp structure while each opp
40 * is represented by the opp structure.
41 */
42
43/**
47d43ba7 44 * struct dev_pm_opp - Generic OPP description structure
e1f60b29
NM
45 * @node: opp list node. The nodes are maintained throughout the lifetime
46 * of boot. It is expected only an optimal set of OPPs are
47 * added to the library by the SoC framework.
48 * RCU usage: opp list is traversed with RCU locks. node
49 * modification is possible realtime, hence the modifications
50 * are protected by the dev_opp_list_lock for integrity.
51 * IMPORTANT: the opp nodes should be maintained in increasing
52 * order.
38393409 53 * @dynamic: not-created from static DT entries.
e1f60b29 54 * @available: true/false - marks if this OPP as available or not
27465902 55 * @turbo: true if turbo (boost) OPP
e1f60b29 56 * @rate: Frequency in hertz
27465902
VK
57 * @u_volt: Target voltage in microvolts corresponding to this OPP
58 * @u_volt_min: Minimum voltage in microvolts corresponding to this OPP
59 * @u_volt_max: Maximum voltage in microvolts corresponding to this OPP
60 * @u_amp: Maximum current drawn by the device in microamperes
3ca9bb33
VK
61 * @clock_latency_ns: Latency (in nanoseconds) of switching to this OPP's
62 * frequency from any other OPP's frequency.
e1f60b29 63 * @dev_opp: points back to the device_opp struct this opp belongs to
cd1a068a 64 * @rcu_head: RCU callback head used for deferred freeing
27465902 65 * @np: OPP's device node.
e1f60b29
NM
66 *
67 * This structure stores the OPP information for a given device.
68 */
47d43ba7 69struct dev_pm_opp {
e1f60b29
NM
70 struct list_head node;
71
72 bool available;
38393409 73 bool dynamic;
27465902 74 bool turbo;
e1f60b29 75 unsigned long rate;
27465902 76
e1f60b29 77 unsigned long u_volt;
27465902
VK
78 unsigned long u_volt_min;
79 unsigned long u_volt_max;
80 unsigned long u_amp;
3ca9bb33 81 unsigned long clock_latency_ns;
e1f60b29
NM
82
83 struct device_opp *dev_opp;
cd1a068a 84 struct rcu_head rcu_head;
27465902
VK
85
86 struct device_node *np;
e1f60b29
NM
87};
88
06441658
VK
89/**
90 * struct device_list_opp - devices managed by 'struct device_opp'
91 * @node: list node
92 * @dev: device to which the struct object belongs
93 * @rcu_head: RCU callback head used for deferred freeing
94 *
95 * This is an internal data structure maintaining the list of devices that are
96 * managed by 'struct device_opp'.
97 */
98struct device_list_opp {
99 struct list_head node;
100 const struct device *dev;
101 struct rcu_head rcu_head;
102};
103
e1f60b29
NM
104/**
105 * struct device_opp - Device opp structure
106 * @node: list node - contains the devices with OPPs that
107 * have been registered. Nodes once added are not modified in this
108 * list.
109 * RCU usage: nodes are not modified in the list of device_opp,
110 * however addition is possible and is secured by dev_opp_list_lock
cd1a068a 111 * @srcu_head: notifier head to notify the OPP availability changes.
129eec55 112 * @rcu_head: RCU callback head used for deferred freeing
06441658 113 * @dev_list: list of devices that share these OPPs
e1f60b29 114 * @opp_list: list of opps
06441658
VK
115 * @np: struct device_node pointer for opp's DT node.
116 * @shared_opp: OPP is shared between multiple devices.
e1f60b29
NM
117 *
118 * This is an internal data structure maintaining the link to opps attached to
119 * a device. This structure is not meant to be shared to users as it is
1c6a662f
VK
120 * meant for book keeping and private to OPP library.
121 *
122 * Because the opp structures can be used from both rcu and srcu readers, we
123 * need to wait for the grace period of both of them before freeing any
124 * resources. And so we have used kfree_rcu() from within call_srcu() handlers.
e1f60b29
NM
125 */
126struct device_opp {
127 struct list_head node;
128
cd1a068a 129 struct srcu_notifier_head srcu_head;
129eec55 130 struct rcu_head rcu_head;
06441658 131 struct list_head dev_list;
e1f60b29 132 struct list_head opp_list;
3ca9bb33 133
06441658 134 struct device_node *np;
3ca9bb33 135 unsigned long clock_latency_ns_max;
06441658 136 bool shared_opp;
ad656a6a 137 struct dev_pm_opp *suspend_opp;
e1f60b29
NM
138};
139
140/*
141 * The root of the list of all devices. All device_opp structures branch off
142 * from here, with each device_opp containing the list of opp it supports in
143 * various states of availability.
144 */
145static LIST_HEAD(dev_opp_list);
146/* Lock to allow exclusive modification to the device and opp lists */
147static DEFINE_MUTEX(dev_opp_list_lock);
148
b02ded24
DT
149#define opp_rcu_lockdep_assert() \
150do { \
151 rcu_lockdep_assert(rcu_read_lock_held() || \
152 lockdep_is_held(&dev_opp_list_lock), \
153 "Missing rcu_read_lock() or " \
154 "dev_opp_list_lock protection"); \
155} while (0)
156
06441658
VK
157static struct device_list_opp *_find_list_dev(const struct device *dev,
158 struct device_opp *dev_opp)
159{
160 struct device_list_opp *list_dev;
161
162 list_for_each_entry(list_dev, &dev_opp->dev_list, node)
163 if (list_dev->dev == dev)
164 return list_dev;
165
166 return NULL;
167}
168
169static struct device_opp *_managed_opp(const struct device_node *np)
170{
171 struct device_opp *dev_opp;
172
173 list_for_each_entry_rcu(dev_opp, &dev_opp_list, node) {
174 if (dev_opp->np == np) {
175 /*
176 * Multiple devices can point to the same OPP table and
177 * so will have same node-pointer, np.
178 *
179 * But the OPPs will be considered as shared only if the
180 * OPP table contains a "opp-shared" property.
181 */
182 return dev_opp->shared_opp ? dev_opp : NULL;
183 }
184 }
185
186 return NULL;
187}
188
e1f60b29 189/**
327854c8 190 * _find_device_opp() - find device_opp struct using device pointer
e1f60b29
NM
191 * @dev: device pointer used to lookup device OPPs
192 *
193 * Search list of device OPPs for one containing matching device. Does a RCU
194 * reader operation to grab the pointer needed.
195 *
984f16c8 196 * Return: pointer to 'struct device_opp' if found, otherwise -ENODEV or
e1f60b29
NM
197 * -EINVAL based on type of error.
198 *
199 * Locking: This function must be called under rcu_read_lock(). device_opp
200 * is a RCU protected pointer. This means that device_opp is valid as long
201 * as we are under RCU lock.
202 */
327854c8 203static struct device_opp *_find_device_opp(struct device *dev)
e1f60b29 204{
06441658 205 struct device_opp *dev_opp;
e1f60b29
NM
206
207 if (unlikely(IS_ERR_OR_NULL(dev))) {
208 pr_err("%s: Invalid parameters\n", __func__);
209 return ERR_PTR(-EINVAL);
210 }
211
06441658
VK
212 list_for_each_entry_rcu(dev_opp, &dev_opp_list, node)
213 if (_find_list_dev(dev, dev_opp))
214 return dev_opp;
e1f60b29 215
06441658 216 return ERR_PTR(-ENODEV);
e1f60b29
NM
217}
218
219/**
5d4879cd 220 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an available opp
e1f60b29
NM
221 * @opp: opp for which voltage has to be returned for
222 *
984f16c8 223 * Return: voltage in micro volt corresponding to the opp, else
e1f60b29
NM
224 * return 0
225 *
226 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
227 * protected pointer. This means that opp which could have been fetched by
228 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
229 * under RCU lock. The pointer returned by the opp_find_freq family must be
230 * used in the same section as the usage of this function with the pointer
231 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
232 * pointer.
233 */
47d43ba7 234unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
e1f60b29 235{
47d43ba7 236 struct dev_pm_opp *tmp_opp;
e1f60b29
NM
237 unsigned long v = 0;
238
04bf1c7f
KK
239 opp_rcu_lockdep_assert();
240
e1f60b29
NM
241 tmp_opp = rcu_dereference(opp);
242 if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
243 pr_err("%s: Invalid parameters\n", __func__);
244 else
245 v = tmp_opp->u_volt;
246
247 return v;
248}
5d4879cd 249EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
e1f60b29
NM
250
251/**
5d4879cd 252 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
e1f60b29
NM
253 * @opp: opp for which frequency has to be returned for
254 *
984f16c8 255 * Return: frequency in hertz corresponding to the opp, else
e1f60b29
NM
256 * return 0
257 *
258 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
259 * protected pointer. This means that opp which could have been fetched by
260 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
261 * under RCU lock. The pointer returned by the opp_find_freq family must be
262 * used in the same section as the usage of this function with the pointer
263 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
264 * pointer.
265 */
47d43ba7 266unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
e1f60b29 267{
47d43ba7 268 struct dev_pm_opp *tmp_opp;
e1f60b29
NM
269 unsigned long f = 0;
270
04bf1c7f
KK
271 opp_rcu_lockdep_assert();
272
e1f60b29
NM
273 tmp_opp = rcu_dereference(opp);
274 if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
275 pr_err("%s: Invalid parameters\n", __func__);
276 else
277 f = tmp_opp->rate;
278
279 return f;
280}
5d4879cd 281EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
e1f60b29 282
19445b25
BZ
283/**
284 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
285 * @opp: opp for which turbo mode is being verified
286 *
287 * Turbo OPPs are not for normal use, and can be enabled (under certain
288 * conditions) for short duration of times to finish high throughput work
289 * quickly. Running on them for longer times may overheat the chip.
290 *
291 * Return: true if opp is turbo opp, else false.
292 *
293 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
294 * protected pointer. This means that opp which could have been fetched by
295 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
296 * under RCU lock. The pointer returned by the opp_find_freq family must be
297 * used in the same section as the usage of this function with the pointer
298 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
299 * pointer.
300 */
301bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
302{
303 struct dev_pm_opp *tmp_opp;
304
305 opp_rcu_lockdep_assert();
306
307 tmp_opp = rcu_dereference(opp);
308 if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available) {
309 pr_err("%s: Invalid parameters\n", __func__);
310 return false;
311 }
312
313 return tmp_opp->turbo;
314}
315EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
316
3ca9bb33
VK
317/**
318 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
319 * @dev: device for which we do this operation
320 *
321 * Return: This function returns the max clock latency in nanoseconds.
322 *
323 * Locking: This function takes rcu_read_lock().
324 */
325unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
326{
327 struct device_opp *dev_opp;
328 unsigned long clock_latency_ns;
329
330 rcu_read_lock();
331
332 dev_opp = _find_device_opp(dev);
333 if (IS_ERR(dev_opp))
334 clock_latency_ns = 0;
335 else
336 clock_latency_ns = dev_opp->clock_latency_ns_max;
337
338 rcu_read_unlock();
339 return clock_latency_ns;
340}
341EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
342
e1f60b29 343/**
5d4879cd 344 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp list
e1f60b29
NM
345 * @dev: device for which we do this operation
346 *
984f16c8 347 * Return: This function returns the number of available opps if there are any,
e1f60b29
NM
348 * else returns 0 if none or the corresponding error value.
349 *
b4718c02 350 * Locking: This function takes rcu_read_lock().
e1f60b29 351 */
5d4879cd 352int dev_pm_opp_get_opp_count(struct device *dev)
e1f60b29
NM
353{
354 struct device_opp *dev_opp;
47d43ba7 355 struct dev_pm_opp *temp_opp;
e1f60b29
NM
356 int count = 0;
357
b4718c02 358 rcu_read_lock();
b02ded24 359
327854c8 360 dev_opp = _find_device_opp(dev);
e1f60b29 361 if (IS_ERR(dev_opp)) {
b4718c02
DT
362 count = PTR_ERR(dev_opp);
363 dev_err(dev, "%s: device OPP not found (%d)\n",
364 __func__, count);
365 goto out_unlock;
e1f60b29
NM
366 }
367
368 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
369 if (temp_opp->available)
370 count++;
371 }
372
b4718c02
DT
373out_unlock:
374 rcu_read_unlock();
e1f60b29
NM
375 return count;
376}
5d4879cd 377EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
e1f60b29
NM
378
379/**
5d4879cd 380 * dev_pm_opp_find_freq_exact() - search for an exact frequency
e1f60b29
NM
381 * @dev: device for which we do this operation
382 * @freq: frequency to search for
7ae49618 383 * @available: true/false - match for available opp
e1f60b29 384 *
984f16c8
NM
385 * Return: Searches for exact match in the opp list and returns pointer to the
386 * matching opp if found, else returns ERR_PTR in case of error and should
387 * be handled using IS_ERR. Error return values can be:
0779726c
NM
388 * EINVAL: for bad pointer
389 * ERANGE: no match found for search
390 * ENODEV: if device not found in list of registered devices
e1f60b29
NM
391 *
392 * Note: available is a modifier for the search. if available=true, then the
393 * match is for exact matching frequency and is available in the stored OPP
394 * table. if false, the match is for exact frequency which is not available.
395 *
396 * This provides a mechanism to enable an opp which is not available currently
397 * or the opposite as well.
398 *
399 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
400 * protected pointer. The reason for the same is that the opp pointer which is
401 * returned will remain valid for use with opp_get_{voltage, freq} only while
402 * under the locked area. The pointer returned must be used prior to unlocking
403 * with rcu_read_unlock() to maintain the integrity of the pointer.
404 */
47d43ba7
NM
405struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
406 unsigned long freq,
407 bool available)
e1f60b29
NM
408{
409 struct device_opp *dev_opp;
47d43ba7 410 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
e1f60b29 411
b02ded24
DT
412 opp_rcu_lockdep_assert();
413
327854c8 414 dev_opp = _find_device_opp(dev);
e1f60b29
NM
415 if (IS_ERR(dev_opp)) {
416 int r = PTR_ERR(dev_opp);
417 dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
418 return ERR_PTR(r);
419 }
420
421 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
422 if (temp_opp->available == available &&
423 temp_opp->rate == freq) {
424 opp = temp_opp;
425 break;
426 }
427 }
428
429 return opp;
430}
5d4879cd 431EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
e1f60b29
NM
432
433/**
5d4879cd 434 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
e1f60b29
NM
435 * @dev: device for which we do this operation
436 * @freq: Start frequency
437 *
438 * Search for the matching ceil *available* OPP from a starting freq
439 * for a device.
440 *
984f16c8 441 * Return: matching *opp and refreshes *freq accordingly, else returns
0779726c
NM
442 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
443 * values can be:
444 * EINVAL: for bad pointer
445 * ERANGE: no match found for search
446 * ENODEV: if device not found in list of registered devices
e1f60b29
NM
447 *
448 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
449 * protected pointer. The reason for the same is that the opp pointer which is
450 * returned will remain valid for use with opp_get_{voltage, freq} only while
451 * under the locked area. The pointer returned must be used prior to unlocking
452 * with rcu_read_unlock() to maintain the integrity of the pointer.
453 */
47d43ba7
NM
454struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
455 unsigned long *freq)
e1f60b29
NM
456{
457 struct device_opp *dev_opp;
47d43ba7 458 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
e1f60b29 459
b02ded24
DT
460 opp_rcu_lockdep_assert();
461
e1f60b29
NM
462 if (!dev || !freq) {
463 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
464 return ERR_PTR(-EINVAL);
465 }
466
327854c8 467 dev_opp = _find_device_opp(dev);
e1f60b29 468 if (IS_ERR(dev_opp))
0779726c 469 return ERR_CAST(dev_opp);
e1f60b29
NM
470
471 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
472 if (temp_opp->available && temp_opp->rate >= *freq) {
473 opp = temp_opp;
474 *freq = opp->rate;
475 break;
476 }
477 }
478
479 return opp;
480}
5d4879cd 481EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
e1f60b29
NM
482
483/**
5d4879cd 484 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
e1f60b29
NM
485 * @dev: device for which we do this operation
486 * @freq: Start frequency
487 *
488 * Search for the matching floor *available* OPP from a starting freq
489 * for a device.
490 *
984f16c8 491 * Return: matching *opp and refreshes *freq accordingly, else returns
0779726c
NM
492 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
493 * values can be:
494 * EINVAL: for bad pointer
495 * ERANGE: no match found for search
496 * ENODEV: if device not found in list of registered devices
e1f60b29
NM
497 *
498 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
499 * protected pointer. The reason for the same is that the opp pointer which is
500 * returned will remain valid for use with opp_get_{voltage, freq} only while
501 * under the locked area. The pointer returned must be used prior to unlocking
502 * with rcu_read_unlock() to maintain the integrity of the pointer.
503 */
47d43ba7
NM
504struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
505 unsigned long *freq)
e1f60b29
NM
506{
507 struct device_opp *dev_opp;
47d43ba7 508 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
e1f60b29 509
b02ded24
DT
510 opp_rcu_lockdep_assert();
511
e1f60b29
NM
512 if (!dev || !freq) {
513 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
514 return ERR_PTR(-EINVAL);
515 }
516
327854c8 517 dev_opp = _find_device_opp(dev);
e1f60b29 518 if (IS_ERR(dev_opp))
0779726c 519 return ERR_CAST(dev_opp);
e1f60b29
NM
520
521 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
522 if (temp_opp->available) {
523 /* go to the next node, before choosing prev */
524 if (temp_opp->rate > *freq)
525 break;
526 else
527 opp = temp_opp;
528 }
529 }
530 if (!IS_ERR(opp))
531 *freq = opp->rate;
532
533 return opp;
534}
5d4879cd 535EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
e1f60b29 536
06441658
VK
537/* List-dev Helpers */
538static void _kfree_list_dev_rcu(struct rcu_head *head)
539{
540 struct device_list_opp *list_dev;
541
542 list_dev = container_of(head, struct device_list_opp, rcu_head);
543 kfree_rcu(list_dev, rcu_head);
544}
545
546static void _remove_list_dev(struct device_list_opp *list_dev,
547 struct device_opp *dev_opp)
548{
549 list_del(&list_dev->node);
550 call_srcu(&dev_opp->srcu_head.srcu, &list_dev->rcu_head,
551 _kfree_list_dev_rcu);
552}
553
554static struct device_list_opp *_add_list_dev(const struct device *dev,
555 struct device_opp *dev_opp)
556{
557 struct device_list_opp *list_dev;
558
559 list_dev = kzalloc(sizeof(*list_dev), GFP_KERNEL);
560 if (!list_dev)
561 return NULL;
562
563 /* Initialize list-dev */
564 list_dev->dev = dev;
565 list_add_rcu(&list_dev->node, &dev_opp->dev_list);
566
567 return list_dev;
568}
569
984f16c8 570/**
aa5f2f85 571 * _add_device_opp() - Find device OPP table or allocate a new one
984f16c8
NM
572 * @dev: device for which we do this operation
573 *
aa5f2f85
VK
574 * It tries to find an existing table first, if it couldn't find one, it
575 * allocates a new OPP table and returns that.
984f16c8
NM
576 *
577 * Return: valid device_opp pointer if success, else NULL.
578 */
327854c8 579static struct device_opp *_add_device_opp(struct device *dev)
07cce74a
VK
580{
581 struct device_opp *dev_opp;
06441658 582 struct device_list_opp *list_dev;
07cce74a 583
aa5f2f85
VK
584 /* Check for existing list for 'dev' first */
585 dev_opp = _find_device_opp(dev);
586 if (!IS_ERR(dev_opp))
587 return dev_opp;
588
07cce74a
VK
589 /*
590 * Allocate a new device OPP table. In the infrequent case where a new
591 * device is needed to be added, we pay this penalty.
592 */
593 dev_opp = kzalloc(sizeof(*dev_opp), GFP_KERNEL);
594 if (!dev_opp)
595 return NULL;
596
06441658
VK
597 INIT_LIST_HEAD(&dev_opp->dev_list);
598
599 list_dev = _add_list_dev(dev, dev_opp);
600 if (!list_dev) {
601 kfree(dev_opp);
602 return NULL;
603 }
604
07cce74a
VK
605 srcu_init_notifier_head(&dev_opp->srcu_head);
606 INIT_LIST_HEAD(&dev_opp->opp_list);
607
608 /* Secure the device list modification */
609 list_add_rcu(&dev_opp->node, &dev_opp_list);
610 return dev_opp;
611}
612
737002b5
VK
613/**
614 * _kfree_device_rcu() - Free device_opp RCU handler
615 * @head: RCU head
616 */
617static void _kfree_device_rcu(struct rcu_head *head)
618{
619 struct device_opp *device_opp = container_of(head, struct device_opp, rcu_head);
620
621 kfree_rcu(device_opp, rcu_head);
622}
623
3bac42ca
VK
624/**
625 * _remove_device_opp() - Removes a device OPP table
626 * @dev_opp: device OPP table to be removed.
627 *
628 * Removes/frees device OPP table it it doesn't contain any OPPs.
629 */
630static void _remove_device_opp(struct device_opp *dev_opp)
631{
06441658
VK
632 struct device_list_opp *list_dev;
633
3bac42ca
VK
634 if (!list_empty(&dev_opp->opp_list))
635 return;
636
06441658
VK
637 list_dev = list_first_entry(&dev_opp->dev_list, struct device_list_opp,
638 node);
639
640 _remove_list_dev(list_dev, dev_opp);
641
642 /* dev_list must be empty now */
643 WARN_ON(!list_empty(&dev_opp->dev_list));
644
3bac42ca
VK
645 list_del_rcu(&dev_opp->node);
646 call_srcu(&dev_opp->srcu_head.srcu, &dev_opp->rcu_head,
647 _kfree_device_rcu);
648}
649
737002b5
VK
650/**
651 * _kfree_opp_rcu() - Free OPP RCU handler
652 * @head: RCU head
653 */
654static void _kfree_opp_rcu(struct rcu_head *head)
655{
656 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
657
658 kfree_rcu(opp, rcu_head);
659}
660
661/**
662 * _opp_remove() - Remove an OPP from a table definition
663 * @dev_opp: points back to the device_opp struct this opp belongs to
664 * @opp: pointer to the OPP to remove
23dacf6d 665 * @notify: OPP_EVENT_REMOVE notification should be sent or not
737002b5
VK
666 *
667 * This function removes an opp definition from the opp list.
668 *
669 * Locking: The internal device_opp and opp structures are RCU protected.
670 * It is assumed that the caller holds required mutex for an RCU updater
671 * strategy.
672 */
673static void _opp_remove(struct device_opp *dev_opp,
23dacf6d 674 struct dev_pm_opp *opp, bool notify)
737002b5
VK
675{
676 /*
677 * Notify the changes in the availability of the operable
678 * frequency/voltage list.
679 */
23dacf6d
VK
680 if (notify)
681 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_REMOVE, opp);
737002b5
VK
682 list_del_rcu(&opp->node);
683 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
684
3bac42ca 685 _remove_device_opp(dev_opp);
737002b5
VK
686}
687
688/**
689 * dev_pm_opp_remove() - Remove an OPP from OPP list
690 * @dev: device for which we do this operation
691 * @freq: OPP to remove with matching 'freq'
692 *
693 * This function removes an opp from the opp list.
694 *
695 * Locking: The internal device_opp and opp structures are RCU protected.
696 * Hence this function internally uses RCU updater strategy with mutex locks
697 * to keep the integrity of the internal data structures. Callers should ensure
698 * that this function is *NOT* called under RCU protection or in contexts where
699 * mutex cannot be locked.
700 */
701void dev_pm_opp_remove(struct device *dev, unsigned long freq)
702{
703 struct dev_pm_opp *opp;
704 struct device_opp *dev_opp;
705 bool found = false;
706
707 /* Hold our list modification lock here */
708 mutex_lock(&dev_opp_list_lock);
709
710 dev_opp = _find_device_opp(dev);
711 if (IS_ERR(dev_opp))
712 goto unlock;
713
714 list_for_each_entry(opp, &dev_opp->opp_list, node) {
715 if (opp->rate == freq) {
716 found = true;
717 break;
718 }
719 }
720
721 if (!found) {
722 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
723 __func__, freq);
724 goto unlock;
725 }
726
23dacf6d 727 _opp_remove(dev_opp, opp, true);
737002b5
VK
728unlock:
729 mutex_unlock(&dev_opp_list_lock);
730}
731EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
732
23dacf6d
VK
733static struct dev_pm_opp *_allocate_opp(struct device *dev,
734 struct device_opp **dev_opp)
735{
736 struct dev_pm_opp *opp;
737
738 /* allocate new OPP node */
739 opp = kzalloc(sizeof(*opp), GFP_KERNEL);
740 if (!opp)
741 return NULL;
742
743 INIT_LIST_HEAD(&opp->node);
744
745 *dev_opp = _add_device_opp(dev);
746 if (!*dev_opp) {
747 kfree(opp);
748 return NULL;
749 }
750
751 return opp;
752}
753
06441658
VK
754static int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
755 struct device_opp *dev_opp)
23dacf6d
VK
756{
757 struct dev_pm_opp *opp;
758 struct list_head *head = &dev_opp->opp_list;
759
760 /*
761 * Insert new OPP in order of increasing frequency and discard if
762 * already present.
763 *
764 * Need to use &dev_opp->opp_list in the condition part of the 'for'
765 * loop, don't replace it with head otherwise it will become an infinite
766 * loop.
767 */
768 list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
769 if (new_opp->rate > opp->rate) {
770 head = &opp->node;
771 continue;
772 }
773
774 if (new_opp->rate < opp->rate)
775 break;
776
777 /* Duplicate OPPs */
06441658 778 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
23dacf6d
VK
779 __func__, opp->rate, opp->u_volt, opp->available,
780 new_opp->rate, new_opp->u_volt, new_opp->available);
781
782 return opp->available && new_opp->u_volt == opp->u_volt ?
783 0 : -EEXIST;
784 }
785
786 new_opp->dev_opp = dev_opp;
787 list_add_rcu(&new_opp->node, head);
788
789 return 0;
790}
791
984f16c8
NM
792/**
793 * _opp_add_dynamic() - Allocate a dynamic OPP.
794 * @dev: device for which we do this operation
795 * @freq: Frequency in Hz for this OPP
796 * @u_volt: Voltage in uVolts for this OPP
797 * @dynamic: Dynamically added OPPs.
798 *
799 * This function adds an opp definition to the opp list and returns status.
800 * The opp is made available by default and it can be controlled using
801 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
802 *
803 * NOTE: "dynamic" parameter impacts OPPs added by the of_init_opp_table and
804 * freed by of_free_opp_table.
805 *
806 * Locking: The internal device_opp and opp structures are RCU protected.
807 * Hence this function internally uses RCU updater strategy with mutex locks
808 * to keep the integrity of the internal data structures. Callers should ensure
809 * that this function is *NOT* called under RCU protection or in contexts where
810 * mutex cannot be locked.
811 *
812 * Return:
813 * 0 On success OR
814 * Duplicate OPPs (both freq and volt are same) and opp->available
815 * -EEXIST Freq are same and volt are different OR
816 * Duplicate OPPs (both freq and volt are same) and !opp->available
817 * -ENOMEM Memory allocation failure
818 */
327854c8
NM
819static int _opp_add_dynamic(struct device *dev, unsigned long freq,
820 long u_volt, bool dynamic)
e1f60b29 821{
aa5f2f85 822 struct device_opp *dev_opp;
23dacf6d 823 struct dev_pm_opp *new_opp;
6ce4184d 824 int ret;
e1f60b29 825
e1f60b29
NM
826 /* Hold our list modification lock here */
827 mutex_lock(&dev_opp_list_lock);
828
23dacf6d
VK
829 new_opp = _allocate_opp(dev, &dev_opp);
830 if (!new_opp) {
831 ret = -ENOMEM;
832 goto unlock;
833 }
834
a7470db6 835 /* populate the opp table */
a7470db6
VK
836 new_opp->rate = freq;
837 new_opp->u_volt = u_volt;
838 new_opp->available = true;
23dacf6d 839 new_opp->dynamic = dynamic;
a7470db6 840
06441658 841 ret = _opp_add(dev, new_opp, dev_opp);
23dacf6d 842 if (ret)
6ce4184d 843 goto free_opp;
64ce8545 844
e1f60b29
NM
845 mutex_unlock(&dev_opp_list_lock);
846
03ca370f
MH
847 /*
848 * Notify the changes in the availability of the operable
849 * frequency/voltage list.
850 */
cd1a068a 851 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
e1f60b29 852 return 0;
6ce4184d
VK
853
854free_opp:
23dacf6d
VK
855 _opp_remove(dev_opp, new_opp, false);
856unlock:
6ce4184d 857 mutex_unlock(&dev_opp_list_lock);
6ce4184d 858 return ret;
e1f60b29 859}
38393409 860
27465902
VK
861/* TODO: Support multiple regulators */
862static int opp_get_microvolt(struct dev_pm_opp *opp, struct device *dev)
863{
864 u32 microvolt[3] = {0};
865 int count, ret;
866
867 count = of_property_count_u32_elems(opp->np, "opp-microvolt");
868 if (!count)
869 return 0;
870
871 /* There can be one or three elements here */
872 if (count != 1 && count != 3) {
873 dev_err(dev, "%s: Invalid number of elements in opp-microvolt property (%d)\n",
874 __func__, count);
875 return -EINVAL;
876 }
877
878 ret = of_property_read_u32_array(opp->np, "opp-microvolt", microvolt,
879 count);
880 if (ret) {
881 dev_err(dev, "%s: error parsing opp-microvolt: %d\n", __func__,
882 ret);
883 return -EINVAL;
884 }
885
886 opp->u_volt = microvolt[0];
887 opp->u_volt_min = microvolt[1];
888 opp->u_volt_max = microvolt[2];
889
890 return 0;
891}
892
893/**
894 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
895 * @dev: device for which we do this operation
896 * @np: device node
897 *
898 * This function adds an opp definition to the opp list and returns status. The
899 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
900 * removed by dev_pm_opp_remove.
901 *
902 * Locking: The internal device_opp and opp structures are RCU protected.
903 * Hence this function internally uses RCU updater strategy with mutex locks
904 * to keep the integrity of the internal data structures. Callers should ensure
905 * that this function is *NOT* called under RCU protection or in contexts where
906 * mutex cannot be locked.
907 *
908 * Return:
909 * 0 On success OR
910 * Duplicate OPPs (both freq and volt are same) and opp->available
911 * -EEXIST Freq are same and volt are different OR
912 * Duplicate OPPs (both freq and volt are same) and !opp->available
913 * -ENOMEM Memory allocation failure
914 * -EINVAL Failed parsing the OPP node
915 */
916static int _opp_add_static_v2(struct device *dev, struct device_node *np)
917{
918 struct device_opp *dev_opp;
919 struct dev_pm_opp *new_opp;
920 u64 rate;
68fa9f0a 921 u32 val;
27465902
VK
922 int ret;
923
924 /* Hold our list modification lock here */
925 mutex_lock(&dev_opp_list_lock);
926
927 new_opp = _allocate_opp(dev, &dev_opp);
928 if (!new_opp) {
929 ret = -ENOMEM;
930 goto unlock;
931 }
932
933 ret = of_property_read_u64(np, "opp-hz", &rate);
934 if (ret < 0) {
935 dev_err(dev, "%s: opp-hz not found\n", __func__);
936 goto free_opp;
937 }
938
939 /*
940 * Rate is defined as an unsigned long in clk API, and so casting
941 * explicitly to its type. Must be fixed once rate is 64 bit
942 * guaranteed in clk API.
943 */
944 new_opp->rate = (unsigned long)rate;
945 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
946
947 new_opp->np = np;
948 new_opp->dynamic = false;
949 new_opp->available = true;
68fa9f0a
VK
950
951 if (!of_property_read_u32(np, "clock-latency-ns", &val))
952 new_opp->clock_latency_ns = val;
27465902
VK
953
954 ret = opp_get_microvolt(new_opp, dev);
955 if (ret)
956 goto free_opp;
957
68fa9f0a
VK
958 if (!of_property_read_u32(new_opp->np, "opp-microamp", &val))
959 new_opp->u_amp = val;
27465902 960
06441658 961 ret = _opp_add(dev, new_opp, dev_opp);
27465902
VK
962 if (ret)
963 goto free_opp;
964
ad656a6a
VK
965 /* OPP to select on device suspend */
966 if (of_property_read_bool(np, "opp-suspend")) {
967 if (dev_opp->suspend_opp)
968 dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
969 __func__, dev_opp->suspend_opp->rate,
970 new_opp->rate);
971 else
972 dev_opp->suspend_opp = new_opp;
973 }
974
3ca9bb33
VK
975 if (new_opp->clock_latency_ns > dev_opp->clock_latency_ns_max)
976 dev_opp->clock_latency_ns_max = new_opp->clock_latency_ns;
977
27465902
VK
978 mutex_unlock(&dev_opp_list_lock);
979
3ca9bb33 980 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
27465902 981 __func__, new_opp->turbo, new_opp->rate, new_opp->u_volt,
3ca9bb33
VK
982 new_opp->u_volt_min, new_opp->u_volt_max,
983 new_opp->clock_latency_ns);
27465902
VK
984
985 /*
986 * Notify the changes in the availability of the operable
987 * frequency/voltage list.
988 */
989 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
990 return 0;
991
992free_opp:
993 _opp_remove(dev_opp, new_opp, false);
994unlock:
995 mutex_unlock(&dev_opp_list_lock);
996 return ret;
997}
998
38393409
VK
999/**
1000 * dev_pm_opp_add() - Add an OPP table from a table definitions
1001 * @dev: device for which we do this operation
1002 * @freq: Frequency in Hz for this OPP
1003 * @u_volt: Voltage in uVolts for this OPP
1004 *
1005 * This function adds an opp definition to the opp list and returns status.
1006 * The opp is made available by default and it can be controlled using
1007 * dev_pm_opp_enable/disable functions.
1008 *
1009 * Locking: The internal device_opp and opp structures are RCU protected.
1010 * Hence this function internally uses RCU updater strategy with mutex locks
1011 * to keep the integrity of the internal data structures. Callers should ensure
1012 * that this function is *NOT* called under RCU protection or in contexts where
1013 * mutex cannot be locked.
1014 *
1015 * Return:
984f16c8 1016 * 0 On success OR
38393409 1017 * Duplicate OPPs (both freq and volt are same) and opp->available
984f16c8 1018 * -EEXIST Freq are same and volt are different OR
38393409 1019 * Duplicate OPPs (both freq and volt are same) and !opp->available
984f16c8 1020 * -ENOMEM Memory allocation failure
38393409
VK
1021 */
1022int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1023{
327854c8 1024 return _opp_add_dynamic(dev, freq, u_volt, true);
38393409 1025}
5d4879cd 1026EXPORT_SYMBOL_GPL(dev_pm_opp_add);
e1f60b29
NM
1027
1028/**
327854c8 1029 * _opp_set_availability() - helper to set the availability of an opp
e1f60b29
NM
1030 * @dev: device for which we do this operation
1031 * @freq: OPP frequency to modify availability
1032 * @availability_req: availability status requested for this opp
1033 *
1034 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
1035 * share a common logic which is isolated here.
1036 *
984f16c8 1037 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1f60b29
NM
1038 * copy operation, returns 0 if no modifcation was done OR modification was
1039 * successful.
1040 *
1041 * Locking: The internal device_opp and opp structures are RCU protected.
1042 * Hence this function internally uses RCU updater strategy with mutex locks to
1043 * keep the integrity of the internal data structures. Callers should ensure
1044 * that this function is *NOT* called under RCU protection or in contexts where
1045 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1046 */
327854c8
NM
1047static int _opp_set_availability(struct device *dev, unsigned long freq,
1048 bool availability_req)
e1f60b29 1049{
29df0ee1 1050 struct device_opp *dev_opp;
47d43ba7 1051 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
e1f60b29
NM
1052 int r = 0;
1053
1054 /* keep the node allocated */
47d43ba7 1055 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
59d84ca8 1056 if (!new_opp)
e1f60b29 1057 return -ENOMEM;
e1f60b29
NM
1058
1059 mutex_lock(&dev_opp_list_lock);
1060
1061 /* Find the device_opp */
327854c8 1062 dev_opp = _find_device_opp(dev);
e1f60b29
NM
1063 if (IS_ERR(dev_opp)) {
1064 r = PTR_ERR(dev_opp);
1065 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1066 goto unlock;
1067 }
1068
1069 /* Do we have the frequency? */
1070 list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) {
1071 if (tmp_opp->rate == freq) {
1072 opp = tmp_opp;
1073 break;
1074 }
1075 }
1076 if (IS_ERR(opp)) {
1077 r = PTR_ERR(opp);
1078 goto unlock;
1079 }
1080
1081 /* Is update really needed? */
1082 if (opp->available == availability_req)
1083 goto unlock;
1084 /* copy the old data over */
1085 *new_opp = *opp;
1086
1087 /* plug in new node */
1088 new_opp->available = availability_req;
1089
1090 list_replace_rcu(&opp->node, &new_opp->node);
1091 mutex_unlock(&dev_opp_list_lock);
327854c8 1092 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
e1f60b29 1093
03ca370f
MH
1094 /* Notify the change of the OPP availability */
1095 if (availability_req)
cd1a068a 1096 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ENABLE,
03ca370f
MH
1097 new_opp);
1098 else
cd1a068a 1099 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_DISABLE,
03ca370f
MH
1100 new_opp);
1101
dde8437d 1102 return 0;
e1f60b29
NM
1103
1104unlock:
1105 mutex_unlock(&dev_opp_list_lock);
e1f60b29
NM
1106 kfree(new_opp);
1107 return r;
1108}
1109
1110/**
5d4879cd 1111 * dev_pm_opp_enable() - Enable a specific OPP
e1f60b29
NM
1112 * @dev: device for which we do this operation
1113 * @freq: OPP frequency to enable
1114 *
1115 * Enables a provided opp. If the operation is valid, this returns 0, else the
1116 * corresponding error value. It is meant to be used for users an OPP available
5d4879cd 1117 * after being temporarily made unavailable with dev_pm_opp_disable.
e1f60b29
NM
1118 *
1119 * Locking: The internal device_opp and opp structures are RCU protected.
1120 * Hence this function indirectly uses RCU and mutex locks to keep the
1121 * integrity of the internal data structures. Callers should ensure that
1122 * this function is *NOT* called under RCU protection or in contexts where
1123 * mutex locking or synchronize_rcu() blocking calls cannot be used.
984f16c8
NM
1124 *
1125 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1126 * copy operation, returns 0 if no modifcation was done OR modification was
1127 * successful.
e1f60b29 1128 */
5d4879cd 1129int dev_pm_opp_enable(struct device *dev, unsigned long freq)
e1f60b29 1130{
327854c8 1131 return _opp_set_availability(dev, freq, true);
e1f60b29 1132}
5d4879cd 1133EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
e1f60b29
NM
1134
1135/**
5d4879cd 1136 * dev_pm_opp_disable() - Disable a specific OPP
e1f60b29
NM
1137 * @dev: device for which we do this operation
1138 * @freq: OPP frequency to disable
1139 *
1140 * Disables a provided opp. If the operation is valid, this returns
1141 * 0, else the corresponding error value. It is meant to be a temporary
1142 * control by users to make this OPP not available until the circumstances are
5d4879cd 1143 * right to make it available again (with a call to dev_pm_opp_enable).
e1f60b29
NM
1144 *
1145 * Locking: The internal device_opp and opp structures are RCU protected.
1146 * Hence this function indirectly uses RCU and mutex locks to keep the
1147 * integrity of the internal data structures. Callers should ensure that
1148 * this function is *NOT* called under RCU protection or in contexts where
1149 * mutex locking or synchronize_rcu() blocking calls cannot be used.
984f16c8
NM
1150 *
1151 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1152 * copy operation, returns 0 if no modifcation was done OR modification was
1153 * successful.
e1f60b29 1154 */
5d4879cd 1155int dev_pm_opp_disable(struct device *dev, unsigned long freq)
e1f60b29 1156{
327854c8 1157 return _opp_set_availability(dev, freq, false);
e1f60b29 1158}
5d4879cd 1159EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
e1f60b29 1160
03ca370f 1161/**
5d4879cd 1162 * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
03ca370f 1163 * @dev: device pointer used to lookup device OPPs.
984f16c8
NM
1164 *
1165 * Return: pointer to notifier head if found, otherwise -ENODEV or
1166 * -EINVAL based on type of error casted as pointer. value must be checked
1167 * with IS_ERR to determine valid pointer or error result.
1168 *
1169 * Locking: This function must be called under rcu_read_lock(). dev_opp is a RCU
1170 * protected pointer. The reason for the same is that the opp pointer which is
1171 * returned will remain valid for use with opp_get_{voltage, freq} only while
1172 * under the locked area. The pointer returned must be used prior to unlocking
1173 * with rcu_read_unlock() to maintain the integrity of the pointer.
03ca370f 1174 */
5d4879cd 1175struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
03ca370f 1176{
327854c8 1177 struct device_opp *dev_opp = _find_device_opp(dev);
03ca370f
MH
1178
1179 if (IS_ERR(dev_opp))
156acb16 1180 return ERR_CAST(dev_opp); /* matching type */
03ca370f 1181
cd1a068a 1182 return &dev_opp->srcu_head;
03ca370f 1183}
4679ec37 1184EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
b496dfbc
SG
1185
1186#ifdef CONFIG_OF
737002b5
VK
1187/**
1188 * of_free_opp_table() - Free OPP table entries created from static DT entries
1189 * @dev: device pointer used to lookup device OPPs.
1190 *
1191 * Free OPPs created using static entries present in DT.
1192 *
1193 * Locking: The internal device_opp and opp structures are RCU protected.
1194 * Hence this function indirectly uses RCU updater strategy with mutex locks
1195 * to keep the integrity of the internal data structures. Callers should ensure
1196 * that this function is *NOT* called under RCU protection or in contexts where
1197 * mutex cannot be locked.
1198 */
1199void of_free_opp_table(struct device *dev)
1200{
1201 struct device_opp *dev_opp;
1202 struct dev_pm_opp *opp, *tmp;
1203
06441658
VK
1204 /* Hold our list modification lock here */
1205 mutex_lock(&dev_opp_list_lock);
1206
737002b5
VK
1207 /* Check for existing list for 'dev' */
1208 dev_opp = _find_device_opp(dev);
1209 if (IS_ERR(dev_opp)) {
1210 int error = PTR_ERR(dev_opp);
1211
1212 if (error != -ENODEV)
1213 WARN(1, "%s: dev_opp: %d\n",
1214 IS_ERR_OR_NULL(dev) ?
1215 "Invalid device" : dev_name(dev),
1216 error);
06441658 1217 goto unlock;
737002b5
VK
1218 }
1219
06441658
VK
1220 /* Find if dev_opp manages a single device */
1221 if (list_is_singular(&dev_opp->dev_list)) {
1222 /* Free static OPPs */
1223 list_for_each_entry_safe(opp, tmp, &dev_opp->opp_list, node) {
1224 if (!opp->dynamic)
1225 _opp_remove(dev_opp, opp, true);
1226 }
1227 } else {
1228 _remove_list_dev(_find_list_dev(dev, dev_opp), dev_opp);
737002b5
VK
1229 }
1230
06441658 1231unlock:
737002b5
VK
1232 mutex_unlock(&dev_opp_list_lock);
1233}
1234EXPORT_SYMBOL_GPL(of_free_opp_table);
1235
8d4d4e98
VK
1236void of_cpumask_free_opp_table(cpumask_var_t cpumask)
1237{
1238 struct device *cpu_dev;
1239 int cpu;
1240
1241 WARN_ON(cpumask_empty(cpumask));
1242
1243 for_each_cpu(cpu, cpumask) {
1244 cpu_dev = get_cpu_device(cpu);
1245 if (!cpu_dev) {
1246 pr_err("%s: failed to get cpu%d device\n", __func__,
1247 cpu);
1248 continue;
1249 }
1250
1251 of_free_opp_table(cpu_dev);
1252 }
1253}
1254EXPORT_SYMBOL_GPL(of_cpumask_free_opp_table);
1255
27465902
VK
1256/* Returns opp descriptor node from its phandle. Caller must do of_node_put() */
1257static struct device_node *
1258_of_get_opp_desc_node_from_prop(struct device *dev, const struct property *prop)
1259{
1260 struct device_node *opp_np;
1261
1262 opp_np = of_find_node_by_phandle(be32_to_cpup(prop->value));
1263 if (!opp_np) {
1264 dev_err(dev, "%s: Prop: %s contains invalid opp desc phandle\n",
1265 __func__, prop->name);
1266 return ERR_PTR(-EINVAL);
1267 }
1268
1269 return opp_np;
1270}
1271
8d4d4e98
VK
1272/* Returns opp descriptor node for a device. Caller must do of_node_put() */
1273static struct device_node *_of_get_opp_desc_node(struct device *dev)
1274{
1275 const struct property *prop;
1276
1277 prop = of_find_property(dev->of_node, "operating-points-v2", NULL);
1278 if (!prop)
1279 return ERR_PTR(-ENODEV);
1280 if (!prop->value)
1281 return ERR_PTR(-ENODATA);
1282
1283 /*
1284 * TODO: Support for multiple OPP tables.
1285 *
1286 * There should be only ONE phandle present in "operating-points-v2"
1287 * property.
1288 */
1289 if (prop->length != sizeof(__be32)) {
1290 dev_err(dev, "%s: Invalid opp desc phandle\n", __func__);
1291 return ERR_PTR(-EINVAL);
1292 }
1293
1294 return _of_get_opp_desc_node_from_prop(dev, prop);
1295}
1296
27465902
VK
1297/* Initializes OPP tables based on new bindings */
1298static int _of_init_opp_table_v2(struct device *dev,
1299 const struct property *prop)
1300{
1301 struct device_node *opp_np, *np;
06441658 1302 struct device_opp *dev_opp;
27465902
VK
1303 int ret = 0, count = 0;
1304
1305 if (!prop->value)
1306 return -ENODATA;
1307
1308 /* Get opp node */
1309 opp_np = _of_get_opp_desc_node_from_prop(dev, prop);
1310 if (IS_ERR(opp_np))
1311 return PTR_ERR(opp_np);
1312
06441658
VK
1313 dev_opp = _managed_opp(opp_np);
1314 if (dev_opp) {
1315 /* OPPs are already managed */
1316 if (!_add_list_dev(dev, dev_opp))
1317 ret = -ENOMEM;
1318 goto put_opp_np;
1319 }
1320
27465902
VK
1321 /* We have opp-list node now, iterate over it and add OPPs */
1322 for_each_available_child_of_node(opp_np, np) {
1323 count++;
1324
1325 ret = _opp_add_static_v2(dev, np);
1326 if (ret) {
1327 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
1328 ret);
1f821ed7 1329 goto free_table;
27465902
VK
1330 }
1331 }
1332
1333 /* There should be one of more OPP defined */
1f821ed7
VK
1334 if (WARN_ON(!count)) {
1335 ret = -ENOENT;
27465902 1336 goto put_opp_np;
1f821ed7 1337 }
27465902 1338
1f821ed7
VK
1339 dev_opp = _find_device_opp(dev);
1340 if (WARN_ON(IS_ERR(dev_opp))) {
1341 ret = PTR_ERR(dev_opp);
1342 goto free_table;
06441658 1343 }
27465902 1344
1f821ed7
VK
1345 dev_opp->np = opp_np;
1346 dev_opp->shared_opp = of_property_read_bool(opp_np, "opp-shared");
1347
1348 of_node_put(opp_np);
1349 return 0;
1350
1351free_table:
1352 of_free_opp_table(dev);
27465902
VK
1353put_opp_np:
1354 of_node_put(opp_np);
1355
1356 return ret;
1357}
1358
1359/* Initializes OPP tables based on old-deprecated bindings */
1360static int _of_init_opp_table_v1(struct device *dev)
b496dfbc
SG
1361{
1362 const struct property *prop;
1363 const __be32 *val;
1364 int nr;
1365
1366 prop = of_find_property(dev->of_node, "operating-points", NULL);
1367 if (!prop)
1368 return -ENODEV;
1369 if (!prop->value)
1370 return -ENODATA;
1371
1372 /*
1373 * Each OPP is a set of tuples consisting of frequency and
1374 * voltage like <freq-kHz vol-uV>.
1375 */
1376 nr = prop->length / sizeof(u32);
1377 if (nr % 2) {
1378 dev_err(dev, "%s: Invalid OPP list\n", __func__);
1379 return -EINVAL;
1380 }
1381
1382 val = prop->value;
1383 while (nr) {
1384 unsigned long freq = be32_to_cpup(val++) * 1000;
1385 unsigned long volt = be32_to_cpup(val++);
1386
327854c8 1387 if (_opp_add_dynamic(dev, freq, volt, false))
b496dfbc
SG
1388 dev_warn(dev, "%s: Failed to add OPP %ld\n",
1389 __func__, freq);
b496dfbc
SG
1390 nr -= 2;
1391 }
1392
1393 return 0;
1394}
27465902
VK
1395
1396/**
1397 * of_init_opp_table() - Initialize opp table from device tree
1398 * @dev: device pointer used to lookup device OPPs.
1399 *
1400 * Register the initial OPP table with the OPP library for given device.
1401 *
1402 * Locking: The internal device_opp and opp structures are RCU protected.
1403 * Hence this function indirectly uses RCU updater strategy with mutex locks
1404 * to keep the integrity of the internal data structures. Callers should ensure
1405 * that this function is *NOT* called under RCU protection or in contexts where
1406 * mutex cannot be locked.
1407 *
1408 * Return:
1409 * 0 On success OR
1410 * Duplicate OPPs (both freq and volt are same) and opp->available
1411 * -EEXIST Freq are same and volt are different OR
1412 * Duplicate OPPs (both freq and volt are same) and !opp->available
1413 * -ENOMEM Memory allocation failure
1414 * -ENODEV when 'operating-points' property is not found or is invalid data
1415 * in device node.
1416 * -ENODATA when empty 'operating-points' property is found
1417 * -EINVAL when invalid entries are found in opp-v2 table
1418 */
1419int of_init_opp_table(struct device *dev)
1420{
1421 const struct property *prop;
1422
1423 /*
1424 * OPPs have two version of bindings now. The older one is deprecated,
1425 * try for the new binding first.
1426 */
1427 prop = of_find_property(dev->of_node, "operating-points-v2", NULL);
1428 if (!prop) {
1429 /*
1430 * Try old-deprecated bindings for backward compatibility with
1431 * older dtbs.
1432 */
1433 return _of_init_opp_table_v1(dev);
1434 }
1435
1436 return _of_init_opp_table_v2(dev, prop);
1437}
74c46c6e 1438EXPORT_SYMBOL_GPL(of_init_opp_table);
8d4d4e98
VK
1439
1440int of_cpumask_init_opp_table(cpumask_var_t cpumask)
1441{
1442 struct device *cpu_dev;
1443 int cpu, ret = 0;
1444
1445 WARN_ON(cpumask_empty(cpumask));
1446
1447 for_each_cpu(cpu, cpumask) {
1448 cpu_dev = get_cpu_device(cpu);
1449 if (!cpu_dev) {
1450 pr_err("%s: failed to get cpu%d device\n", __func__,
1451 cpu);
1452 continue;
1453 }
1454
1455 ret = of_init_opp_table(cpu_dev);
1456 if (ret) {
1457 pr_err("%s: couldn't find opp table for cpu:%d, %d\n",
1458 __func__, cpu, ret);
1459
1460 /* Free all other OPPs */
1461 of_cpumask_free_opp_table(cpumask);
1462 break;
1463 }
1464 }
1465
1466 return ret;
1467}
1468EXPORT_SYMBOL_GPL(of_cpumask_init_opp_table);
1469
1470/* Required only for V1 bindings, as v2 can manage it from DT itself */
1471int set_cpus_sharing_opps(struct device *cpu_dev, cpumask_var_t cpumask)
1472{
1473 struct device_list_opp *list_dev;
1474 struct device_opp *dev_opp;
1475 struct device *dev;
1476 int cpu, ret = 0;
1477
1478 rcu_read_lock();
1479
1480 dev_opp = _find_device_opp(cpu_dev);
1481 if (IS_ERR(dev_opp)) {
1482 ret = -EINVAL;
1483 goto out_rcu_read_unlock;
1484 }
1485
1486 for_each_cpu(cpu, cpumask) {
1487 if (cpu == cpu_dev->id)
1488 continue;
1489
1490 dev = get_cpu_device(cpu);
1491 if (!dev) {
1492 dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
1493 __func__, cpu);
1494 continue;
1495 }
1496
1497 list_dev = _add_list_dev(dev, dev_opp);
1498 if (!list_dev) {
1499 dev_err(dev, "%s: failed to add list-dev for cpu%d device\n",
1500 __func__, cpu);
1501 continue;
1502 }
1503 }
1504out_rcu_read_unlock:
1505 rcu_read_unlock();
1506
1507 return 0;
1508}
1509EXPORT_SYMBOL_GPL(set_cpus_sharing_opps);
1510
1511/*
1512 * Works only for OPP v2 bindings.
1513 *
1514 * cpumask should be already set to mask of cpu_dev->id.
1515 * Returns -ENOENT if operating-points-v2 bindings aren't supported.
1516 */
1517int of_get_cpus_sharing_opps(struct device *cpu_dev, cpumask_var_t cpumask)
1518{
1519 struct device_node *np, *tmp_np;
1520 struct device *tcpu_dev;
1521 int cpu, ret = 0;
1522
1523 /* Get OPP descriptor node */
1524 np = _of_get_opp_desc_node(cpu_dev);
1525 if (IS_ERR(np)) {
1526 dev_dbg(cpu_dev, "%s: Couldn't find opp node: %ld\n", __func__,
1527 PTR_ERR(np));
1528 return -ENOENT;
1529 }
1530
1531 /* OPPs are shared ? */
1532 if (!of_property_read_bool(np, "opp-shared"))
1533 goto put_cpu_node;
1534
1535 for_each_possible_cpu(cpu) {
1536 if (cpu == cpu_dev->id)
1537 continue;
1538
1539 tcpu_dev = get_cpu_device(cpu);
1540 if (!tcpu_dev) {
1541 dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
1542 __func__, cpu);
1543 ret = -ENODEV;
1544 goto put_cpu_node;
1545 }
1546
1547 /* Get OPP descriptor node */
1548 tmp_np = _of_get_opp_desc_node(tcpu_dev);
1549 if (IS_ERR(tmp_np)) {
1550 dev_err(tcpu_dev, "%s: Couldn't find opp node: %ld\n",
1551 __func__, PTR_ERR(tmp_np));
1552 ret = PTR_ERR(tmp_np);
1553 goto put_cpu_node;
1554 }
1555
1556 /* CPUs are sharing opp node */
1557 if (np == tmp_np)
1558 cpumask_set_cpu(cpu, cpumask);
1559
1560 of_node_put(tmp_np);
1561 }
1562
1563put_cpu_node:
1564 of_node_put(np);
1565 return ret;
1566}
1567EXPORT_SYMBOL_GPL(of_get_cpus_sharing_opps);
b496dfbc 1568#endif