PM / OPP: of_property_count_u32_elems() can return errors
[deliverable/linux.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;
e1f60b29
NM
102};
103
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 { \
f78f5b90
PM
151 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
152 !lockdep_is_held(&dev_opp_list_lock), \
b02ded24
DT
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 206
50a3cb04 207 if (IS_ERR_OR_NULL(dev)) {
e1f60b29
NM
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 241 tmp_opp = rcu_dereference(opp);
50a3cb04 242 if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available)
e1f60b29
NM
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 273 tmp_opp = rcu_dereference(opp);
50a3cb04 274 if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available)
e1f60b29
NM
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
4eafbd15
BZ
343/**
344 * dev_pm_opp_get_suspend_opp() - Get suspend opp
345 * @dev: device for which we do this operation
346 *
347 * Return: This function returns pointer to the suspend opp if it is
1b2b90cb 348 * defined and available, otherwise it returns NULL.
4eafbd15
BZ
349 *
350 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
351 * protected pointer. The reason for the same is that the opp pointer which is
352 * returned will remain valid for use with opp_get_{voltage, freq} only while
353 * under the locked area. The pointer returned must be used prior to unlocking
354 * with rcu_read_unlock() to maintain the integrity of the pointer.
355 */
356struct dev_pm_opp *dev_pm_opp_get_suspend_opp(struct device *dev)
357{
358 struct device_opp *dev_opp;
4eafbd15
BZ
359
360 opp_rcu_lockdep_assert();
361
362 dev_opp = _find_device_opp(dev);
1b2b90cb
VK
363 if (IS_ERR(dev_opp) || !dev_opp->suspend_opp ||
364 !dev_opp->suspend_opp->available)
365 return NULL;
4eafbd15 366
1b2b90cb 367 return dev_opp->suspend_opp;
4eafbd15
BZ
368}
369EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp);
370
e1f60b29 371/**
5d4879cd 372 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp list
e1f60b29
NM
373 * @dev: device for which we do this operation
374 *
984f16c8 375 * Return: This function returns the number of available opps if there are any,
e1f60b29
NM
376 * else returns 0 if none or the corresponding error value.
377 *
b4718c02 378 * Locking: This function takes rcu_read_lock().
e1f60b29 379 */
5d4879cd 380int dev_pm_opp_get_opp_count(struct device *dev)
e1f60b29
NM
381{
382 struct device_opp *dev_opp;
47d43ba7 383 struct dev_pm_opp *temp_opp;
e1f60b29
NM
384 int count = 0;
385
b4718c02 386 rcu_read_lock();
b02ded24 387
327854c8 388 dev_opp = _find_device_opp(dev);
e1f60b29 389 if (IS_ERR(dev_opp)) {
b4718c02
DT
390 count = PTR_ERR(dev_opp);
391 dev_err(dev, "%s: device OPP not found (%d)\n",
392 __func__, count);
393 goto out_unlock;
e1f60b29
NM
394 }
395
396 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
397 if (temp_opp->available)
398 count++;
399 }
400
b4718c02
DT
401out_unlock:
402 rcu_read_unlock();
e1f60b29
NM
403 return count;
404}
5d4879cd 405EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
e1f60b29
NM
406
407/**
5d4879cd 408 * dev_pm_opp_find_freq_exact() - search for an exact frequency
e1f60b29
NM
409 * @dev: device for which we do this operation
410 * @freq: frequency to search for
7ae49618 411 * @available: true/false - match for available opp
e1f60b29 412 *
984f16c8
NM
413 * Return: Searches for exact match in the opp list and returns pointer to the
414 * matching opp if found, else returns ERR_PTR in case of error and should
415 * be handled using IS_ERR. Error return values can be:
0779726c
NM
416 * EINVAL: for bad pointer
417 * ERANGE: no match found for search
418 * ENODEV: if device not found in list of registered devices
e1f60b29
NM
419 *
420 * Note: available is a modifier for the search. if available=true, then the
421 * match is for exact matching frequency and is available in the stored OPP
422 * table. if false, the match is for exact frequency which is not available.
423 *
424 * This provides a mechanism to enable an opp which is not available currently
425 * or the opposite as well.
426 *
427 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
428 * protected pointer. The reason for the same is that the opp pointer which is
429 * returned will remain valid for use with opp_get_{voltage, freq} only while
430 * under the locked area. The pointer returned must be used prior to unlocking
431 * with rcu_read_unlock() to maintain the integrity of the pointer.
432 */
47d43ba7
NM
433struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
434 unsigned long freq,
435 bool available)
e1f60b29
NM
436{
437 struct device_opp *dev_opp;
47d43ba7 438 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
e1f60b29 439
b02ded24
DT
440 opp_rcu_lockdep_assert();
441
327854c8 442 dev_opp = _find_device_opp(dev);
e1f60b29
NM
443 if (IS_ERR(dev_opp)) {
444 int r = PTR_ERR(dev_opp);
445 dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
446 return ERR_PTR(r);
447 }
448
449 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
450 if (temp_opp->available == available &&
451 temp_opp->rate == freq) {
452 opp = temp_opp;
453 break;
454 }
455 }
456
457 return opp;
458}
5d4879cd 459EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
e1f60b29
NM
460
461/**
5d4879cd 462 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
e1f60b29
NM
463 * @dev: device for which we do this operation
464 * @freq: Start frequency
465 *
466 * Search for the matching ceil *available* OPP from a starting freq
467 * for a device.
468 *
984f16c8 469 * Return: matching *opp and refreshes *freq accordingly, else returns
0779726c
NM
470 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
471 * values can be:
472 * EINVAL: for bad pointer
473 * ERANGE: no match found for search
474 * ENODEV: if device not found in list of registered devices
e1f60b29
NM
475 *
476 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
477 * protected pointer. The reason for the same is that the opp pointer which is
478 * returned will remain valid for use with opp_get_{voltage, freq} only while
479 * under the locked area. The pointer returned must be used prior to unlocking
480 * with rcu_read_unlock() to maintain the integrity of the pointer.
481 */
47d43ba7
NM
482struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
483 unsigned long *freq)
e1f60b29
NM
484{
485 struct device_opp *dev_opp;
47d43ba7 486 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
e1f60b29 487
b02ded24
DT
488 opp_rcu_lockdep_assert();
489
e1f60b29
NM
490 if (!dev || !freq) {
491 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
492 return ERR_PTR(-EINVAL);
493 }
494
327854c8 495 dev_opp = _find_device_opp(dev);
e1f60b29 496 if (IS_ERR(dev_opp))
0779726c 497 return ERR_CAST(dev_opp);
e1f60b29
NM
498
499 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
500 if (temp_opp->available && temp_opp->rate >= *freq) {
501 opp = temp_opp;
502 *freq = opp->rate;
503 break;
504 }
505 }
506
507 return opp;
508}
5d4879cd 509EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
e1f60b29
NM
510
511/**
5d4879cd 512 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
e1f60b29
NM
513 * @dev: device for which we do this operation
514 * @freq: Start frequency
515 *
516 * Search for the matching floor *available* OPP from a starting freq
517 * for a device.
518 *
984f16c8 519 * Return: matching *opp and refreshes *freq accordingly, else returns
0779726c
NM
520 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
521 * values can be:
522 * EINVAL: for bad pointer
523 * ERANGE: no match found for search
524 * ENODEV: if device not found in list of registered devices
e1f60b29
NM
525 *
526 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
527 * protected pointer. The reason for the same is that the opp pointer which is
528 * returned will remain valid for use with opp_get_{voltage, freq} only while
529 * under the locked area. The pointer returned must be used prior to unlocking
530 * with rcu_read_unlock() to maintain the integrity of the pointer.
531 */
47d43ba7
NM
532struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
533 unsigned long *freq)
e1f60b29
NM
534{
535 struct device_opp *dev_opp;
47d43ba7 536 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
e1f60b29 537
b02ded24
DT
538 opp_rcu_lockdep_assert();
539
e1f60b29
NM
540 if (!dev || !freq) {
541 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
542 return ERR_PTR(-EINVAL);
543 }
544
327854c8 545 dev_opp = _find_device_opp(dev);
e1f60b29 546 if (IS_ERR(dev_opp))
0779726c 547 return ERR_CAST(dev_opp);
e1f60b29
NM
548
549 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
550 if (temp_opp->available) {
551 /* go to the next node, before choosing prev */
552 if (temp_opp->rate > *freq)
553 break;
554 else
555 opp = temp_opp;
556 }
557 }
558 if (!IS_ERR(opp))
559 *freq = opp->rate;
560
561 return opp;
562}
5d4879cd 563EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
e1f60b29 564
06441658
VK
565/* List-dev Helpers */
566static void _kfree_list_dev_rcu(struct rcu_head *head)
567{
568 struct device_list_opp *list_dev;
569
570 list_dev = container_of(head, struct device_list_opp, rcu_head);
571 kfree_rcu(list_dev, rcu_head);
572}
573
574static void _remove_list_dev(struct device_list_opp *list_dev,
575 struct device_opp *dev_opp)
576{
577 list_del(&list_dev->node);
578 call_srcu(&dev_opp->srcu_head.srcu, &list_dev->rcu_head,
579 _kfree_list_dev_rcu);
580}
581
582static struct device_list_opp *_add_list_dev(const struct device *dev,
583 struct device_opp *dev_opp)
584{
585 struct device_list_opp *list_dev;
586
587 list_dev = kzalloc(sizeof(*list_dev), GFP_KERNEL);
588 if (!list_dev)
589 return NULL;
590
591 /* Initialize list-dev */
592 list_dev->dev = dev;
593 list_add_rcu(&list_dev->node, &dev_opp->dev_list);
594
595 return list_dev;
596}
597
984f16c8 598/**
aa5f2f85 599 * _add_device_opp() - Find device OPP table or allocate a new one
984f16c8
NM
600 * @dev: device for which we do this operation
601 *
aa5f2f85
VK
602 * It tries to find an existing table first, if it couldn't find one, it
603 * allocates a new OPP table and returns that.
984f16c8
NM
604 *
605 * Return: valid device_opp pointer if success, else NULL.
606 */
327854c8 607static struct device_opp *_add_device_opp(struct device *dev)
07cce74a
VK
608{
609 struct device_opp *dev_opp;
06441658 610 struct device_list_opp *list_dev;
07cce74a 611
aa5f2f85
VK
612 /* Check for existing list for 'dev' first */
613 dev_opp = _find_device_opp(dev);
614 if (!IS_ERR(dev_opp))
615 return dev_opp;
07cce74a
VK
616
617 /*
618 * Allocate a new device OPP table. In the infrequent case where a new
619 * device is needed to be added, we pay this penalty.
620 */
621 dev_opp = kzalloc(sizeof(*dev_opp), GFP_KERNEL);
622 if (!dev_opp)
623 return NULL;
624
06441658
VK
625 INIT_LIST_HEAD(&dev_opp->dev_list);
626
627 list_dev = _add_list_dev(dev, dev_opp);
628 if (!list_dev) {
629 kfree(dev_opp);
630 return NULL;
631 }
632
07cce74a
VK
633 srcu_init_notifier_head(&dev_opp->srcu_head);
634 INIT_LIST_HEAD(&dev_opp->opp_list);
635
636 /* Secure the device list modification */
637 list_add_rcu(&dev_opp->node, &dev_opp_list);
638 return dev_opp;
639}
640
984f16c8 641/**
737002b5
VK
642 * _kfree_device_rcu() - Free device_opp RCU handler
643 * @head: RCU head
984f16c8 644 */
737002b5 645static void _kfree_device_rcu(struct rcu_head *head)
e1f60b29 646{
737002b5 647 struct device_opp *device_opp = container_of(head, struct device_opp, rcu_head);
6ce4184d 648
737002b5 649 kfree_rcu(device_opp, rcu_head);
e1f60b29 650}
38393409
VK
651
652/**
3bac42ca
VK
653 * _remove_device_opp() - Removes a device OPP table
654 * @dev_opp: device OPP table to be removed.
38393409 655 *
3bac42ca 656 * Removes/frees device OPP table it it doesn't contain any OPPs.
38393409 657 */
3bac42ca 658static void _remove_device_opp(struct device_opp *dev_opp)
38393409 659{
06441658
VK
660 struct device_list_opp *list_dev;
661
3bac42ca
VK
662 if (!list_empty(&dev_opp->opp_list))
663 return;
664
06441658
VK
665 list_dev = list_first_entry(&dev_opp->dev_list, struct device_list_opp,
666 node);
667
668 _remove_list_dev(list_dev, dev_opp);
669
670 /* dev_list must be empty now */
671 WARN_ON(!list_empty(&dev_opp->dev_list));
672
3bac42ca
VK
673 list_del_rcu(&dev_opp->node);
674 call_srcu(&dev_opp->srcu_head.srcu, &dev_opp->rcu_head,
675 _kfree_device_rcu);
38393409 676}
e1f60b29 677
984f16c8
NM
678/**
679 * _kfree_opp_rcu() - Free OPP RCU handler
680 * @head: RCU head
681 */
327854c8 682static void _kfree_opp_rcu(struct rcu_head *head)
129eec55
VK
683{
684 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
685
686 kfree_rcu(opp, rcu_head);
687}
688
984f16c8
NM
689/**
690 * _opp_remove() - Remove an OPP from a table definition
691 * @dev_opp: points back to the device_opp struct this opp belongs to
692 * @opp: pointer to the OPP to remove
23dacf6d 693 * @notify: OPP_EVENT_REMOVE notification should be sent or not
984f16c8
NM
694 *
695 * This function removes an opp definition from the opp list.
696 *
697 * Locking: The internal device_opp and opp structures are RCU protected.
698 * It is assumed that the caller holds required mutex for an RCU updater
699 * strategy.
700 */
327854c8 701static void _opp_remove(struct device_opp *dev_opp,
23dacf6d 702 struct dev_pm_opp *opp, bool notify)
129eec55
VK
703{
704 /*
705 * Notify the changes in the availability of the operable
706 * frequency/voltage list.
707 */
23dacf6d
VK
708 if (notify)
709 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_REMOVE, opp);
129eec55 710 list_del_rcu(&opp->node);
327854c8 711 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
129eec55 712
3bac42ca 713 _remove_device_opp(dev_opp);
129eec55
VK
714}
715
716/**
717 * dev_pm_opp_remove() - Remove an OPP from OPP list
718 * @dev: device for which we do this operation
719 * @freq: OPP to remove with matching 'freq'
720 *
721 * This function removes an opp from the opp list.
984f16c8
NM
722 *
723 * Locking: The internal device_opp and opp structures are RCU protected.
724 * Hence this function internally uses RCU updater strategy with mutex locks
725 * to keep the integrity of the internal data structures. Callers should ensure
726 * that this function is *NOT* called under RCU protection or in contexts where
727 * mutex cannot be locked.
129eec55
VK
728 */
729void dev_pm_opp_remove(struct device *dev, unsigned long freq)
730{
731 struct dev_pm_opp *opp;
732 struct device_opp *dev_opp;
733 bool found = false;
734
735 /* Hold our list modification lock here */
736 mutex_lock(&dev_opp_list_lock);
737
327854c8 738 dev_opp = _find_device_opp(dev);
129eec55
VK
739 if (IS_ERR(dev_opp))
740 goto unlock;
741
742 list_for_each_entry(opp, &dev_opp->opp_list, node) {
743 if (opp->rate == freq) {
744 found = true;
745 break;
746 }
747 }
748
749 if (!found) {
750 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
751 __func__, freq);
752 goto unlock;
753 }
754
23dacf6d 755 _opp_remove(dev_opp, opp, true);
129eec55
VK
756unlock:
757 mutex_unlock(&dev_opp_list_lock);
758}
759EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
760
23dacf6d
VK
761static struct dev_pm_opp *_allocate_opp(struct device *dev,
762 struct device_opp **dev_opp)
e1f60b29 763{
23dacf6d 764 struct dev_pm_opp *opp;
e1f60b29 765
23dacf6d
VK
766 /* allocate new OPP node */
767 opp = kzalloc(sizeof(*opp), GFP_KERNEL);
768 if (!opp)
769 return NULL;
e1f60b29 770
23dacf6d 771 INIT_LIST_HEAD(&opp->node);
e1f60b29 772
23dacf6d
VK
773 *dev_opp = _add_device_opp(dev);
774 if (!*dev_opp) {
775 kfree(opp);
776 return NULL;
777 }
778
779 return opp;
780}
781
06441658
VK
782static int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
783 struct device_opp *dev_opp)
23dacf6d
VK
784{
785 struct dev_pm_opp *opp;
786 struct list_head *head = &dev_opp->opp_list;
787
788 /*
789 * Insert new OPP in order of increasing frequency and discard if
790 * already present.
791 *
792 * Need to use &dev_opp->opp_list in the condition part of the 'for'
793 * loop, don't replace it with head otherwise it will become an infinite
794 * loop.
795 */
796 list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
797 if (new_opp->rate > opp->rate) {
798 head = &opp->node;
799 continue;
800 }
801
802 if (new_opp->rate < opp->rate)
803 break;
804
805 /* Duplicate OPPs */
06441658 806 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
23dacf6d
VK
807 __func__, opp->rate, opp->u_volt, opp->available,
808 new_opp->rate, new_opp->u_volt, new_opp->available);
809
810 return opp->available && new_opp->u_volt == opp->u_volt ?
811 0 : -EEXIST;
812 }
813
814 new_opp->dev_opp = dev_opp;
815 list_add_rcu(&new_opp->node, head);
816
817 return 0;
818}
819
984f16c8
NM
820/**
821 * _opp_add_dynamic() - Allocate a dynamic OPP.
822 * @dev: device for which we do this operation
823 * @freq: Frequency in Hz for this OPP
824 * @u_volt: Voltage in uVolts for this OPP
825 * @dynamic: Dynamically added OPPs.
826 *
827 * This function adds an opp definition to the opp list and returns status.
828 * The opp is made available by default and it can be controlled using
829 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
830 *
831 * NOTE: "dynamic" parameter impacts OPPs added by the of_init_opp_table and
832 * freed by of_free_opp_table.
833 *
834 * Locking: The internal device_opp and opp structures are RCU protected.
835 * Hence this function internally uses RCU updater strategy with mutex locks
836 * to keep the integrity of the internal data structures. Callers should ensure
837 * that this function is *NOT* called under RCU protection or in contexts where
838 * mutex cannot be locked.
839 *
840 * Return:
841 * 0 On success OR
842 * Duplicate OPPs (both freq and volt are same) and opp->available
843 * -EEXIST Freq are same and volt are different OR
844 * Duplicate OPPs (both freq and volt are same) and !opp->available
845 * -ENOMEM Memory allocation failure
846 */
327854c8
NM
847static int _opp_add_dynamic(struct device *dev, unsigned long freq,
848 long u_volt, bool dynamic)
e1f60b29 849{
aa5f2f85 850 struct device_opp *dev_opp;
23dacf6d 851 struct dev_pm_opp *new_opp;
6ce4184d 852 int ret;
e1f60b29 853
e1f60b29
NM
854 /* Hold our list modification lock here */
855 mutex_lock(&dev_opp_list_lock);
856
23dacf6d
VK
857 new_opp = _allocate_opp(dev, &dev_opp);
858 if (!new_opp) {
859 ret = -ENOMEM;
860 goto unlock;
861 }
862
a7470db6 863 /* populate the opp table */
a7470db6
VK
864 new_opp->rate = freq;
865 new_opp->u_volt = u_volt;
866 new_opp->available = true;
23dacf6d 867 new_opp->dynamic = dynamic;
a7470db6 868
06441658 869 ret = _opp_add(dev, new_opp, dev_opp);
23dacf6d 870 if (ret)
6ce4184d 871 goto free_opp;
64ce8545 872
e1f60b29
NM
873 mutex_unlock(&dev_opp_list_lock);
874
03ca370f
MH
875 /*
876 * Notify the changes in the availability of the operable
877 * frequency/voltage list.
878 */
cd1a068a 879 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
e1f60b29 880 return 0;
6ce4184d
VK
881
882free_opp:
23dacf6d
VK
883 _opp_remove(dev_opp, new_opp, false);
884unlock:
6ce4184d 885 mutex_unlock(&dev_opp_list_lock);
6ce4184d 886 return ret;
e1f60b29 887}
38393409 888
27465902
VK
889/* TODO: Support multiple regulators */
890static int opp_get_microvolt(struct dev_pm_opp *opp, struct device *dev)
891{
892 u32 microvolt[3] = {0};
893 int count, ret;
894
680168a5
VK
895 /* Missing property isn't a problem, but an invalid entry is */
896 if (!of_find_property(opp->np, "opp-microvolt", NULL))
27465902
VK
897 return 0;
898
680168a5
VK
899 count = of_property_count_u32_elems(opp->np, "opp-microvolt");
900 if (count < 0) {
901 dev_err(dev, "%s: Invalid opp-microvolt property (%d)\n",
902 __func__, count);
903 return count;
904 }
905
27465902
VK
906 /* There can be one or three elements here */
907 if (count != 1 && count != 3) {
908 dev_err(dev, "%s: Invalid number of elements in opp-microvolt property (%d)\n",
909 __func__, count);
910 return -EINVAL;
911 }
912
913 ret = of_property_read_u32_array(opp->np, "opp-microvolt", microvolt,
914 count);
915 if (ret) {
916 dev_err(dev, "%s: error parsing opp-microvolt: %d\n", __func__,
917 ret);
918 return -EINVAL;
919 }
920
921 opp->u_volt = microvolt[0];
922 opp->u_volt_min = microvolt[1];
923 opp->u_volt_max = microvolt[2];
924
925 return 0;
926}
927
928/**
929 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
930 * @dev: device for which we do this operation
931 * @np: device node
932 *
933 * This function adds an opp definition to the opp list and returns status. The
934 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
935 * removed by dev_pm_opp_remove.
936 *
937 * Locking: The internal device_opp and opp structures are RCU protected.
938 * Hence this function internally uses RCU updater strategy with mutex locks
939 * to keep the integrity of the internal data structures. Callers should ensure
940 * that this function is *NOT* called under RCU protection or in contexts where
941 * mutex cannot be locked.
942 *
943 * Return:
944 * 0 On success OR
945 * Duplicate OPPs (both freq and volt are same) and opp->available
946 * -EEXIST Freq are same and volt are different OR
947 * Duplicate OPPs (both freq and volt are same) and !opp->available
948 * -ENOMEM Memory allocation failure
949 * -EINVAL Failed parsing the OPP node
950 */
951static int _opp_add_static_v2(struct device *dev, struct device_node *np)
952{
953 struct device_opp *dev_opp;
954 struct dev_pm_opp *new_opp;
955 u64 rate;
68fa9f0a 956 u32 val;
27465902
VK
957 int ret;
958
959 /* Hold our list modification lock here */
960 mutex_lock(&dev_opp_list_lock);
961
962 new_opp = _allocate_opp(dev, &dev_opp);
963 if (!new_opp) {
964 ret = -ENOMEM;
965 goto unlock;
966 }
967
968 ret = of_property_read_u64(np, "opp-hz", &rate);
969 if (ret < 0) {
970 dev_err(dev, "%s: opp-hz not found\n", __func__);
971 goto free_opp;
972 }
973
974 /*
975 * Rate is defined as an unsigned long in clk API, and so casting
976 * explicitly to its type. Must be fixed once rate is 64 bit
977 * guaranteed in clk API.
978 */
979 new_opp->rate = (unsigned long)rate;
980 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
981
982 new_opp->np = np;
983 new_opp->dynamic = false;
984 new_opp->available = true;
68fa9f0a
VK
985
986 if (!of_property_read_u32(np, "clock-latency-ns", &val))
987 new_opp->clock_latency_ns = val;
27465902
VK
988
989 ret = opp_get_microvolt(new_opp, dev);
990 if (ret)
991 goto free_opp;
992
68fa9f0a
VK
993 if (!of_property_read_u32(new_opp->np, "opp-microamp", &val))
994 new_opp->u_amp = val;
27465902 995
06441658 996 ret = _opp_add(dev, new_opp, dev_opp);
27465902
VK
997 if (ret)
998 goto free_opp;
999
ad656a6a
VK
1000 /* OPP to select on device suspend */
1001 if (of_property_read_bool(np, "opp-suspend")) {
1002 if (dev_opp->suspend_opp)
1003 dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
1004 __func__, dev_opp->suspend_opp->rate,
1005 new_opp->rate);
1006 else
1007 dev_opp->suspend_opp = new_opp;
1008 }
1009
3ca9bb33
VK
1010 if (new_opp->clock_latency_ns > dev_opp->clock_latency_ns_max)
1011 dev_opp->clock_latency_ns_max = new_opp->clock_latency_ns;
1012
27465902
VK
1013 mutex_unlock(&dev_opp_list_lock);
1014
3ca9bb33 1015 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
27465902 1016 __func__, new_opp->turbo, new_opp->rate, new_opp->u_volt,
3ca9bb33
VK
1017 new_opp->u_volt_min, new_opp->u_volt_max,
1018 new_opp->clock_latency_ns);
27465902
VK
1019
1020 /*
1021 * Notify the changes in the availability of the operable
1022 * frequency/voltage list.
1023 */
1024 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
1025 return 0;
1026
1027free_opp:
1028 _opp_remove(dev_opp, new_opp, false);
1029unlock:
1030 mutex_unlock(&dev_opp_list_lock);
1031 return ret;
1032}
1033
38393409
VK
1034/**
1035 * dev_pm_opp_add() - Add an OPP table from a table definitions
1036 * @dev: device for which we do this operation
1037 * @freq: Frequency in Hz for this OPP
1038 * @u_volt: Voltage in uVolts for this OPP
1039 *
1040 * This function adds an opp definition to the opp list and returns status.
1041 * The opp is made available by default and it can be controlled using
1042 * dev_pm_opp_enable/disable functions.
1043 *
1044 * Locking: The internal device_opp and opp structures are RCU protected.
1045 * Hence this function internally uses RCU updater strategy with mutex locks
1046 * to keep the integrity of the internal data structures. Callers should ensure
1047 * that this function is *NOT* called under RCU protection or in contexts where
1048 * mutex cannot be locked.
1049 *
1050 * Return:
984f16c8 1051 * 0 On success OR
38393409 1052 * Duplicate OPPs (both freq and volt are same) and opp->available
984f16c8 1053 * -EEXIST Freq are same and volt are different OR
38393409 1054 * Duplicate OPPs (both freq and volt are same) and !opp->available
984f16c8 1055 * -ENOMEM Memory allocation failure
38393409
VK
1056 */
1057int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1058{
327854c8 1059 return _opp_add_dynamic(dev, freq, u_volt, true);
38393409 1060}
5d4879cd 1061EXPORT_SYMBOL_GPL(dev_pm_opp_add);
e1f60b29
NM
1062
1063/**
327854c8 1064 * _opp_set_availability() - helper to set the availability of an opp
e1f60b29
NM
1065 * @dev: device for which we do this operation
1066 * @freq: OPP frequency to modify availability
1067 * @availability_req: availability status requested for this opp
1068 *
1069 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
1070 * share a common logic which is isolated here.
1071 *
984f16c8 1072 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1f60b29
NM
1073 * copy operation, returns 0 if no modifcation was done OR modification was
1074 * successful.
1075 *
1076 * Locking: The internal device_opp and opp structures are RCU protected.
1077 * Hence this function internally uses RCU updater strategy with mutex locks to
1078 * keep the integrity of the internal data structures. Callers should ensure
1079 * that this function is *NOT* called under RCU protection or in contexts where
1080 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1081 */
327854c8
NM
1082static int _opp_set_availability(struct device *dev, unsigned long freq,
1083 bool availability_req)
e1f60b29 1084{
29df0ee1 1085 struct device_opp *dev_opp;
47d43ba7 1086 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
e1f60b29
NM
1087 int r = 0;
1088
1089 /* keep the node allocated */
47d43ba7 1090 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
59d84ca8 1091 if (!new_opp)
e1f60b29 1092 return -ENOMEM;
e1f60b29
NM
1093
1094 mutex_lock(&dev_opp_list_lock);
1095
1096 /* Find the device_opp */
327854c8 1097 dev_opp = _find_device_opp(dev);
e1f60b29
NM
1098 if (IS_ERR(dev_opp)) {
1099 r = PTR_ERR(dev_opp);
1100 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1101 goto unlock;
1102 }
1103
1104 /* Do we have the frequency? */
1105 list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) {
1106 if (tmp_opp->rate == freq) {
1107 opp = tmp_opp;
1108 break;
1109 }
1110 }
1111 if (IS_ERR(opp)) {
1112 r = PTR_ERR(opp);
1113 goto unlock;
1114 }
1115
1116 /* Is update really needed? */
1117 if (opp->available == availability_req)
1118 goto unlock;
1119 /* copy the old data over */
1120 *new_opp = *opp;
1121
1122 /* plug in new node */
1123 new_opp->available = availability_req;
1124
1125 list_replace_rcu(&opp->node, &new_opp->node);
1126 mutex_unlock(&dev_opp_list_lock);
327854c8 1127 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
e1f60b29 1128
03ca370f
MH
1129 /* Notify the change of the OPP availability */
1130 if (availability_req)
cd1a068a 1131 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ENABLE,
03ca370f
MH
1132 new_opp);
1133 else
cd1a068a 1134 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_DISABLE,
03ca370f
MH
1135 new_opp);
1136
dde8437d 1137 return 0;
e1f60b29
NM
1138
1139unlock:
1140 mutex_unlock(&dev_opp_list_lock);
e1f60b29
NM
1141 kfree(new_opp);
1142 return r;
1143}
1144
1145/**
5d4879cd 1146 * dev_pm_opp_enable() - Enable a specific OPP
e1f60b29
NM
1147 * @dev: device for which we do this operation
1148 * @freq: OPP frequency to enable
1149 *
1150 * Enables a provided opp. If the operation is valid, this returns 0, else the
1151 * corresponding error value. It is meant to be used for users an OPP available
5d4879cd 1152 * after being temporarily made unavailable with dev_pm_opp_disable.
e1f60b29
NM
1153 *
1154 * Locking: The internal device_opp and opp structures are RCU protected.
1155 * Hence this function indirectly uses RCU and mutex locks to keep the
1156 * integrity of the internal data structures. Callers should ensure that
1157 * this function is *NOT* called under RCU protection or in contexts where
1158 * mutex locking or synchronize_rcu() blocking calls cannot be used.
984f16c8
NM
1159 *
1160 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1161 * copy operation, returns 0 if no modifcation was done OR modification was
1162 * successful.
e1f60b29 1163 */
5d4879cd 1164int dev_pm_opp_enable(struct device *dev, unsigned long freq)
e1f60b29 1165{
327854c8 1166 return _opp_set_availability(dev, freq, true);
e1f60b29 1167}
5d4879cd 1168EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
e1f60b29
NM
1169
1170/**
5d4879cd 1171 * dev_pm_opp_disable() - Disable a specific OPP
e1f60b29
NM
1172 * @dev: device for which we do this operation
1173 * @freq: OPP frequency to disable
1174 *
1175 * Disables a provided opp. If the operation is valid, this returns
1176 * 0, else the corresponding error value. It is meant to be a temporary
1177 * control by users to make this OPP not available until the circumstances are
5d4879cd 1178 * right to make it available again (with a call to dev_pm_opp_enable).
e1f60b29
NM
1179 *
1180 * Locking: The internal device_opp and opp structures are RCU protected.
1181 * Hence this function indirectly uses RCU and mutex locks to keep the
1182 * integrity of the internal data structures. Callers should ensure that
1183 * this function is *NOT* called under RCU protection or in contexts where
1184 * mutex locking or synchronize_rcu() blocking calls cannot be used.
984f16c8
NM
1185 *
1186 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1187 * copy operation, returns 0 if no modifcation was done OR modification was
1188 * successful.
e1f60b29 1189 */
5d4879cd 1190int dev_pm_opp_disable(struct device *dev, unsigned long freq)
e1f60b29 1191{
327854c8 1192 return _opp_set_availability(dev, freq, false);
e1f60b29 1193}
5d4879cd 1194EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
e1f60b29 1195
03ca370f 1196/**
5d4879cd 1197 * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
03ca370f 1198 * @dev: device pointer used to lookup device OPPs.
984f16c8
NM
1199 *
1200 * Return: pointer to notifier head if found, otherwise -ENODEV or
1201 * -EINVAL based on type of error casted as pointer. value must be checked
1202 * with IS_ERR to determine valid pointer or error result.
1203 *
1204 * Locking: This function must be called under rcu_read_lock(). dev_opp is a RCU
1205 * protected pointer. The reason for the same is that the opp pointer which is
1206 * returned will remain valid for use with opp_get_{voltage, freq} only while
1207 * under the locked area. The pointer returned must be used prior to unlocking
1208 * with rcu_read_unlock() to maintain the integrity of the pointer.
03ca370f 1209 */
5d4879cd 1210struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
03ca370f 1211{
327854c8 1212 struct device_opp *dev_opp = _find_device_opp(dev);
03ca370f
MH
1213
1214 if (IS_ERR(dev_opp))
156acb16 1215 return ERR_CAST(dev_opp); /* matching type */
03ca370f 1216
cd1a068a 1217 return &dev_opp->srcu_head;
03ca370f 1218}
4679ec37 1219EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
b496dfbc
SG
1220
1221#ifdef CONFIG_OF
1222/**
737002b5 1223 * of_free_opp_table() - Free OPP table entries created from static DT entries
b496dfbc
SG
1224 * @dev: device pointer used to lookup device OPPs.
1225 *
737002b5 1226 * Free OPPs created using static entries present in DT.
984f16c8
NM
1227 *
1228 * Locking: The internal device_opp and opp structures are RCU protected.
1229 * Hence this function indirectly uses RCU updater strategy with mutex locks
1230 * to keep the integrity of the internal data structures. Callers should ensure
1231 * that this function is *NOT* called under RCU protection or in contexts where
1232 * mutex cannot be locked.
b496dfbc 1233 */
737002b5
VK
1234void of_free_opp_table(struct device *dev)
1235{
1236 struct device_opp *dev_opp;
1237 struct dev_pm_opp *opp, *tmp;
1238
06441658
VK
1239 /* Hold our list modification lock here */
1240 mutex_lock(&dev_opp_list_lock);
1241
737002b5
VK
1242 /* Check for existing list for 'dev' */
1243 dev_opp = _find_device_opp(dev);
1244 if (IS_ERR(dev_opp)) {
1245 int error = PTR_ERR(dev_opp);
1246
1247 if (error != -ENODEV)
1248 WARN(1, "%s: dev_opp: %d\n",
1249 IS_ERR_OR_NULL(dev) ?
1250 "Invalid device" : dev_name(dev),
1251 error);
06441658 1252 goto unlock;
737002b5
VK
1253 }
1254
06441658
VK
1255 /* Find if dev_opp manages a single device */
1256 if (list_is_singular(&dev_opp->dev_list)) {
1257 /* Free static OPPs */
1258 list_for_each_entry_safe(opp, tmp, &dev_opp->opp_list, node) {
1259 if (!opp->dynamic)
1260 _opp_remove(dev_opp, opp, true);
1261 }
1262 } else {
1263 _remove_list_dev(_find_list_dev(dev, dev_opp), dev_opp);
737002b5
VK
1264 }
1265
06441658 1266unlock:
737002b5
VK
1267 mutex_unlock(&dev_opp_list_lock);
1268}
1269EXPORT_SYMBOL_GPL(of_free_opp_table);
1270
8d4d4e98
VK
1271void of_cpumask_free_opp_table(cpumask_var_t cpumask)
1272{
1273 struct device *cpu_dev;
1274 int cpu;
1275
1276 WARN_ON(cpumask_empty(cpumask));
1277
1278 for_each_cpu(cpu, cpumask) {
1279 cpu_dev = get_cpu_device(cpu);
1280 if (!cpu_dev) {
1281 pr_err("%s: failed to get cpu%d device\n", __func__,
1282 cpu);
1283 continue;
1284 }
1285
1286 of_free_opp_table(cpu_dev);
1287 }
1288}
1289EXPORT_SYMBOL_GPL(of_cpumask_free_opp_table);
1290
27465902
VK
1291/* Returns opp descriptor node from its phandle. Caller must do of_node_put() */
1292static struct device_node *
1293_of_get_opp_desc_node_from_prop(struct device *dev, const struct property *prop)
1294{
1295 struct device_node *opp_np;
1296
1297 opp_np = of_find_node_by_phandle(be32_to_cpup(prop->value));
1298 if (!opp_np) {
1299 dev_err(dev, "%s: Prop: %s contains invalid opp desc phandle\n",
1300 __func__, prop->name);
1301 return ERR_PTR(-EINVAL);
1302 }
1303
1304 return opp_np;
1305}
1306
8d4d4e98
VK
1307/* Returns opp descriptor node for a device. Caller must do of_node_put() */
1308static struct device_node *_of_get_opp_desc_node(struct device *dev)
1309{
1310 const struct property *prop;
1311
1312 prop = of_find_property(dev->of_node, "operating-points-v2", NULL);
1313 if (!prop)
1314 return ERR_PTR(-ENODEV);
1315 if (!prop->value)
1316 return ERR_PTR(-ENODATA);
1317
1318 /*
1319 * TODO: Support for multiple OPP tables.
1320 *
1321 * There should be only ONE phandle present in "operating-points-v2"
1322 * property.
1323 */
1324 if (prop->length != sizeof(__be32)) {
1325 dev_err(dev, "%s: Invalid opp desc phandle\n", __func__);
1326 return ERR_PTR(-EINVAL);
1327 }
1328
1329 return _of_get_opp_desc_node_from_prop(dev, prop);
1330}
1331
27465902
VK
1332/* Initializes OPP tables based on new bindings */
1333static int _of_init_opp_table_v2(struct device *dev,
1334 const struct property *prop)
1335{
1336 struct device_node *opp_np, *np;
06441658 1337 struct device_opp *dev_opp;
27465902
VK
1338 int ret = 0, count = 0;
1339
1340 if (!prop->value)
1341 return -ENODATA;
1342
1343 /* Get opp node */
1344 opp_np = _of_get_opp_desc_node_from_prop(dev, prop);
1345 if (IS_ERR(opp_np))
1346 return PTR_ERR(opp_np);
1347
06441658
VK
1348 dev_opp = _managed_opp(opp_np);
1349 if (dev_opp) {
1350 /* OPPs are already managed */
1351 if (!_add_list_dev(dev, dev_opp))
1352 ret = -ENOMEM;
1353 goto put_opp_np;
1354 }
1355
27465902
VK
1356 /* We have opp-list node now, iterate over it and add OPPs */
1357 for_each_available_child_of_node(opp_np, np) {
1358 count++;
1359
1360 ret = _opp_add_static_v2(dev, np);
1361 if (ret) {
1362 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
1363 ret);
1f821ed7 1364 goto free_table;
27465902
VK
1365 }
1366 }
1367
1368 /* There should be one of more OPP defined */
1f821ed7
VK
1369 if (WARN_ON(!count)) {
1370 ret = -ENOENT;
27465902 1371 goto put_opp_np;
1f821ed7 1372 }
27465902 1373
1f821ed7
VK
1374 dev_opp = _find_device_opp(dev);
1375 if (WARN_ON(IS_ERR(dev_opp))) {
1376 ret = PTR_ERR(dev_opp);
1377 goto free_table;
06441658 1378 }
27465902 1379
1f821ed7
VK
1380 dev_opp->np = opp_np;
1381 dev_opp->shared_opp = of_property_read_bool(opp_np, "opp-shared");
1382
1383 of_node_put(opp_np);
1384 return 0;
1385
1386free_table:
1387 of_free_opp_table(dev);
27465902
VK
1388put_opp_np:
1389 of_node_put(opp_np);
1390
1391 return ret;
1392}
1393
1394/* Initializes OPP tables based on old-deprecated bindings */
1395static int _of_init_opp_table_v1(struct device *dev)
b496dfbc
SG
1396{
1397 const struct property *prop;
1398 const __be32 *val;
1399 int nr;
1400
1401 prop = of_find_property(dev->of_node, "operating-points", NULL);
1402 if (!prop)
1403 return -ENODEV;
1404 if (!prop->value)
1405 return -ENODATA;
1406
1407 /*
1408 * Each OPP is a set of tuples consisting of frequency and
1409 * voltage like <freq-kHz vol-uV>.
1410 */
1411 nr = prop->length / sizeof(u32);
1412 if (nr % 2) {
1413 dev_err(dev, "%s: Invalid OPP list\n", __func__);
1414 return -EINVAL;
1415 }
1416
1417 val = prop->value;
1418 while (nr) {
1419 unsigned long freq = be32_to_cpup(val++) * 1000;
1420 unsigned long volt = be32_to_cpup(val++);
1421
327854c8 1422 if (_opp_add_dynamic(dev, freq, volt, false))
b496dfbc
SG
1423 dev_warn(dev, "%s: Failed to add OPP %ld\n",
1424 __func__, freq);
b496dfbc
SG
1425 nr -= 2;
1426 }
1427
1428 return 0;
1429}
129eec55
VK
1430
1431/**
27465902 1432 * of_init_opp_table() - Initialize opp table from device tree
129eec55
VK
1433 * @dev: device pointer used to lookup device OPPs.
1434 *
27465902 1435 * Register the initial OPP table with the OPP library for given device.
984f16c8
NM
1436 *
1437 * Locking: The internal device_opp and opp structures are RCU protected.
1438 * Hence this function indirectly uses RCU updater strategy with mutex locks
1439 * to keep the integrity of the internal data structures. Callers should ensure
1440 * that this function is *NOT* called under RCU protection or in contexts where
1441 * mutex cannot be locked.
27465902
VK
1442 *
1443 * Return:
1444 * 0 On success OR
1445 * Duplicate OPPs (both freq and volt are same) and opp->available
1446 * -EEXIST Freq are same and volt are different OR
1447 * Duplicate OPPs (both freq and volt are same) and !opp->available
1448 * -ENOMEM Memory allocation failure
1449 * -ENODEV when 'operating-points' property is not found or is invalid data
1450 * in device node.
1451 * -ENODATA when empty 'operating-points' property is found
1452 * -EINVAL when invalid entries are found in opp-v2 table
129eec55 1453 */
27465902 1454int of_init_opp_table(struct device *dev)
129eec55 1455{
27465902
VK
1456 const struct property *prop;
1457
1458 /*
1459 * OPPs have two version of bindings now. The older one is deprecated,
1460 * try for the new binding first.
1461 */
1462 prop = of_find_property(dev->of_node, "operating-points-v2", NULL);
1463 if (!prop) {
1464 /*
1465 * Try old-deprecated bindings for backward compatibility with
1466 * older dtbs.
1467 */
1468 return _of_init_opp_table_v1(dev);
1469 }
1470
1471 return _of_init_opp_table_v2(dev, prop);
1472}
74c46c6e 1473EXPORT_SYMBOL_GPL(of_init_opp_table);
8d4d4e98
VK
1474
1475int of_cpumask_init_opp_table(cpumask_var_t cpumask)
1476{
1477 struct device *cpu_dev;
1478 int cpu, ret = 0;
1479
1480 WARN_ON(cpumask_empty(cpumask));
1481
1482 for_each_cpu(cpu, cpumask) {
1483 cpu_dev = get_cpu_device(cpu);
1484 if (!cpu_dev) {
1485 pr_err("%s: failed to get cpu%d device\n", __func__,
1486 cpu);
1487 continue;
1488 }
1489
1490 ret = of_init_opp_table(cpu_dev);
1491 if (ret) {
1492 pr_err("%s: couldn't find opp table for cpu:%d, %d\n",
1493 __func__, cpu, ret);
1494
1495 /* Free all other OPPs */
1496 of_cpumask_free_opp_table(cpumask);
1497 break;
1498 }
1499 }
1500
1501 return ret;
1502}
1503EXPORT_SYMBOL_GPL(of_cpumask_init_opp_table);
1504
1505/* Required only for V1 bindings, as v2 can manage it from DT itself */
1506int set_cpus_sharing_opps(struct device *cpu_dev, cpumask_var_t cpumask)
1507{
1508 struct device_list_opp *list_dev;
2a6127d0 1509 struct device_opp *dev_opp;
8d4d4e98
VK
1510 struct device *dev;
1511 int cpu, ret = 0;
129eec55 1512
8d4d4e98
VK
1513 rcu_read_lock();
1514
1515 dev_opp = _find_device_opp(cpu_dev);
0fe30da2 1516 if (IS_ERR(dev_opp)) {
8d4d4e98
VK
1517 ret = -EINVAL;
1518 goto out_rcu_read_unlock;
0fe30da2 1519 }
129eec55 1520
8d4d4e98
VK
1521 for_each_cpu(cpu, cpumask) {
1522 if (cpu == cpu_dev->id)
1523 continue;
129eec55 1524
8d4d4e98
VK
1525 dev = get_cpu_device(cpu);
1526 if (!dev) {
1527 dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
1528 __func__, cpu);
1529 continue;
1530 }
1531
1532 list_dev = _add_list_dev(dev, dev_opp);
1533 if (!list_dev) {
1534 dev_err(dev, "%s: failed to add list-dev for cpu%d device\n",
1535 __func__, cpu);
1536 continue;
1537 }
129eec55 1538 }
8d4d4e98
VK
1539out_rcu_read_unlock:
1540 rcu_read_unlock();
129eec55 1541
8d4d4e98 1542 return 0;
129eec55 1543}
8d4d4e98
VK
1544EXPORT_SYMBOL_GPL(set_cpus_sharing_opps);
1545
1546/*
1547 * Works only for OPP v2 bindings.
1548 *
1549 * cpumask should be already set to mask of cpu_dev->id.
1550 * Returns -ENOENT if operating-points-v2 bindings aren't supported.
1551 */
1552int of_get_cpus_sharing_opps(struct device *cpu_dev, cpumask_var_t cpumask)
1553{
1554 struct device_node *np, *tmp_np;
1555 struct device *tcpu_dev;
1556 int cpu, ret = 0;
1557
1558 /* Get OPP descriptor node */
1559 np = _of_get_opp_desc_node(cpu_dev);
1560 if (IS_ERR(np)) {
1561 dev_dbg(cpu_dev, "%s: Couldn't find opp node: %ld\n", __func__,
1562 PTR_ERR(np));
1563 return -ENOENT;
1564 }
1565
1566 /* OPPs are shared ? */
1567 if (!of_property_read_bool(np, "opp-shared"))
1568 goto put_cpu_node;
1569
1570 for_each_possible_cpu(cpu) {
1571 if (cpu == cpu_dev->id)
1572 continue;
1573
1574 tcpu_dev = get_cpu_device(cpu);
1575 if (!tcpu_dev) {
1576 dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
1577 __func__, cpu);
1578 ret = -ENODEV;
1579 goto put_cpu_node;
1580 }
1581
1582 /* Get OPP descriptor node */
1583 tmp_np = _of_get_opp_desc_node(tcpu_dev);
1584 if (IS_ERR(tmp_np)) {
1585 dev_err(tcpu_dev, "%s: Couldn't find opp node: %ld\n",
1586 __func__, PTR_ERR(tmp_np));
1587 ret = PTR_ERR(tmp_np);
1588 goto put_cpu_node;
1589 }
1590
1591 /* CPUs are sharing opp node */
1592 if (np == tmp_np)
1593 cpumask_set_cpu(cpu, cpumask);
1594
1595 of_node_put(tmp_np);
1596 }
1597
1598put_cpu_node:
1599 of_node_put(np);
1600 return ret;
1601}
1602EXPORT_SYMBOL_GPL(of_get_cpus_sharing_opps);
b496dfbc 1603#endif
This page took 0.320586 seconds and 5 git commands to generate.