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