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