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