PM / OPP: Add missing doc style comments
[linux-2.6-block.git] / drivers / base / power / opp / cpu.c
1 /*
2  * Generic OPP helper interface for CPU device
3  *
4  * Copyright (C) 2009-2014 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/cpufreq.h>
18 #include <linux/err.h>
19 #include <linux/errno.h>
20 #include <linux/export.h>
21 #include <linux/of.h>
22 #include <linux/slab.h>
23
24 #include "opp.h"
25
26 #ifdef CONFIG_CPU_FREQ
27
28 /**
29  * dev_pm_opp_init_cpufreq_table() - create a cpufreq table for a device
30  * @dev:        device for which we do this operation
31  * @table:      Cpufreq table returned back to caller
32  *
33  * Generate a cpufreq table for a provided device- this assumes that the
34  * opp table is already initialized and ready for usage.
35  *
36  * This function allocates required memory for the cpufreq table. It is
37  * expected that the caller does the required maintenance such as freeing
38  * the table as required.
39  *
40  * Returns -EINVAL for bad pointers, -ENODEV if the device is not found, -ENOMEM
41  * if no memory available for the operation (table is not populated), returns 0
42  * if successful and table is populated.
43  *
44  * WARNING: It is  important for the callers to ensure refreshing their copy of
45  * the table if any of the mentioned functions have been invoked in the interim.
46  *
47  * Locking: The internal opp_table and opp structures are RCU protected.
48  * Since we just use the regular accessor functions to access the internal data
49  * structures, we use RCU read lock inside this function. As a result, users of
50  * this function DONOT need to use explicit locks for invoking.
51  */
52 int dev_pm_opp_init_cpufreq_table(struct device *dev,
53                                   struct cpufreq_frequency_table **table)
54 {
55         struct dev_pm_opp *opp;
56         struct cpufreq_frequency_table *freq_table = NULL;
57         int i, max_opps, ret = 0;
58         unsigned long rate;
59
60         rcu_read_lock();
61
62         max_opps = dev_pm_opp_get_opp_count(dev);
63         if (max_opps <= 0) {
64                 ret = max_opps ? max_opps : -ENODATA;
65                 goto out;
66         }
67
68         freq_table = kcalloc((max_opps + 1), sizeof(*freq_table), GFP_ATOMIC);
69         if (!freq_table) {
70                 ret = -ENOMEM;
71                 goto out;
72         }
73
74         for (i = 0, rate = 0; i < max_opps; i++, rate++) {
75                 /* find next rate */
76                 opp = dev_pm_opp_find_freq_ceil(dev, &rate);
77                 if (IS_ERR(opp)) {
78                         ret = PTR_ERR(opp);
79                         goto out;
80                 }
81                 freq_table[i].driver_data = i;
82                 freq_table[i].frequency = rate / 1000;
83
84                 /* Is Boost/turbo opp ? */
85                 if (dev_pm_opp_is_turbo(opp))
86                         freq_table[i].flags = CPUFREQ_BOOST_FREQ;
87         }
88
89         freq_table[i].driver_data = i;
90         freq_table[i].frequency = CPUFREQ_TABLE_END;
91
92         *table = &freq_table[0];
93
94 out:
95         rcu_read_unlock();
96         if (ret)
97                 kfree(freq_table);
98
99         return ret;
100 }
101 EXPORT_SYMBOL_GPL(dev_pm_opp_init_cpufreq_table);
102
103 /**
104  * dev_pm_opp_free_cpufreq_table() - free the cpufreq table
105  * @dev:        device for which we do this operation
106  * @table:      table to free
107  *
108  * Free up the table allocated by dev_pm_opp_init_cpufreq_table
109  */
110 void dev_pm_opp_free_cpufreq_table(struct device *dev,
111                                    struct cpufreq_frequency_table **table)
112 {
113         if (!table)
114                 return;
115
116         kfree(*table);
117         *table = NULL;
118 }
119 EXPORT_SYMBOL_GPL(dev_pm_opp_free_cpufreq_table);
120 #endif  /* CONFIG_CPU_FREQ */
121
122 /**
123  * dev_pm_opp_set_sharing_cpus() - Mark OPP table as shared by few CPUs
124  * @cpu_dev:    CPU device for which we do this operation
125  * @cpumask:    cpumask of the CPUs which share the OPP table with @cpu_dev
126  *
127  * This marks OPP table of the @cpu_dev as shared by the CPUs present in
128  * @cpumask.
129  *
130  * Returns -ENODEV if OPP table isn't already present.
131  *
132  * Locking: The internal opp_table and opp structures are RCU protected.
133  * Hence this function internally uses RCU updater strategy with mutex locks
134  * to keep the integrity of the internal data structures. Callers should ensure
135  * that this function is *NOT* called under RCU protection or in contexts where
136  * mutex cannot be locked.
137  */
138 int dev_pm_opp_set_sharing_cpus(struct device *cpu_dev, cpumask_var_t cpumask)
139 {
140         struct opp_device *opp_dev;
141         struct opp_table *opp_table;
142         struct device *dev;
143         int cpu, ret = 0;
144
145         mutex_lock(&opp_table_lock);
146
147         opp_table = _find_opp_table(cpu_dev);
148         if (IS_ERR(opp_table)) {
149                 ret = PTR_ERR(opp_table);
150                 goto unlock;
151         }
152
153         for_each_cpu(cpu, cpumask) {
154                 if (cpu == cpu_dev->id)
155                         continue;
156
157                 dev = get_cpu_device(cpu);
158                 if (!dev) {
159                         dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
160                                 __func__, cpu);
161                         continue;
162                 }
163
164                 opp_dev = _add_opp_dev(dev, opp_table);
165                 if (!opp_dev) {
166                         dev_err(dev, "%s: failed to add opp-dev for cpu%d device\n",
167                                 __func__, cpu);
168                         continue;
169                 }
170         }
171 unlock:
172         mutex_unlock(&opp_table_lock);
173
174         return ret;
175 }
176 EXPORT_SYMBOL_GPL(dev_pm_opp_set_sharing_cpus);
177
178 #ifdef CONFIG_OF
179 /**
180  * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
181  * @cpumask:    cpumask for which OPP table needs to be removed
182  *
183  * This removes the OPP tables for CPUs present in the @cpumask.
184  *
185  * Locking: The internal opp_table and opp structures are RCU protected.
186  * Hence this function internally uses RCU updater strategy with mutex locks
187  * to keep the integrity of the internal data structures. Callers should ensure
188  * that this function is *NOT* called under RCU protection or in contexts where
189  * mutex cannot be locked.
190  */
191 void dev_pm_opp_of_cpumask_remove_table(cpumask_var_t cpumask)
192 {
193         struct device *cpu_dev;
194         int cpu;
195
196         WARN_ON(cpumask_empty(cpumask));
197
198         for_each_cpu(cpu, cpumask) {
199                 cpu_dev = get_cpu_device(cpu);
200                 if (!cpu_dev) {
201                         pr_err("%s: failed to get cpu%d device\n", __func__,
202                                cpu);
203                         continue;
204                 }
205
206                 dev_pm_opp_of_remove_table(cpu_dev);
207         }
208 }
209 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
210
211 /**
212  * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
213  * @cpumask:    cpumask for which OPP table needs to be added.
214  *
215  * This adds the OPP tables for CPUs present in the @cpumask.
216  *
217  * Locking: The internal opp_table and opp structures are RCU protected.
218  * Hence this function internally uses RCU updater strategy with mutex locks
219  * to keep the integrity of the internal data structures. Callers should ensure
220  * that this function is *NOT* called under RCU protection or in contexts where
221  * mutex cannot be locked.
222  */
223 int dev_pm_opp_of_cpumask_add_table(cpumask_var_t cpumask)
224 {
225         struct device *cpu_dev;
226         int cpu, ret = 0;
227
228         WARN_ON(cpumask_empty(cpumask));
229
230         for_each_cpu(cpu, cpumask) {
231                 cpu_dev = get_cpu_device(cpu);
232                 if (!cpu_dev) {
233                         pr_err("%s: failed to get cpu%d device\n", __func__,
234                                cpu);
235                         continue;
236                 }
237
238                 ret = dev_pm_opp_of_add_table(cpu_dev);
239                 if (ret) {
240                         pr_err("%s: couldn't find opp table for cpu:%d, %d\n",
241                                __func__, cpu, ret);
242
243                         /* Free all other OPPs */
244                         dev_pm_opp_of_cpumask_remove_table(cpumask);
245                         break;
246                 }
247         }
248
249         return ret;
250 }
251 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
252
253 /*
254  * Works only for OPP v2 bindings.
255  *
256  * Returns -ENOENT if operating-points-v2 bindings aren't supported.
257  */
258 /**
259  * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
260  *                                    @cpu_dev using operating-points-v2
261  *                                    bindings.
262  *
263  * @cpu_dev:    CPU device for which we do this operation
264  * @cpumask:    cpumask to update with information of sharing CPUs
265  *
266  * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
267  *
268  * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
269  *
270  * Locking: The internal opp_table and opp structures are RCU protected.
271  * Hence this function internally uses RCU updater strategy with mutex locks
272  * to keep the integrity of the internal data structures. Callers should ensure
273  * that this function is *NOT* called under RCU protection or in contexts where
274  * mutex cannot be locked.
275  */
276 int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, cpumask_var_t cpumask)
277 {
278         struct device_node *np, *tmp_np;
279         struct device *tcpu_dev;
280         int cpu, ret = 0;
281
282         /* Get OPP descriptor node */
283         np = _of_get_opp_desc_node(cpu_dev);
284         if (!np) {
285                 dev_dbg(cpu_dev, "%s: Couldn't find cpu_dev node.\n", __func__);
286                 return -ENOENT;
287         }
288
289         cpumask_set_cpu(cpu_dev->id, cpumask);
290
291         /* OPPs are shared ? */
292         if (!of_property_read_bool(np, "opp-shared"))
293                 goto put_cpu_node;
294
295         for_each_possible_cpu(cpu) {
296                 if (cpu == cpu_dev->id)
297                         continue;
298
299                 tcpu_dev = get_cpu_device(cpu);
300                 if (!tcpu_dev) {
301                         dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
302                                 __func__, cpu);
303                         ret = -ENODEV;
304                         goto put_cpu_node;
305                 }
306
307                 /* Get OPP descriptor node */
308                 tmp_np = _of_get_opp_desc_node(tcpu_dev);
309                 if (!tmp_np) {
310                         dev_err(tcpu_dev, "%s: Couldn't find tcpu_dev node.\n",
311                                 __func__);
312                         ret = -ENOENT;
313                         goto put_cpu_node;
314                 }
315
316                 /* CPUs are sharing opp node */
317                 if (np == tmp_np)
318                         cpumask_set_cpu(cpu, cpumask);
319
320                 of_node_put(tmp_np);
321         }
322
323 put_cpu_node:
324         of_node_put(np);
325         return ret;
326 }
327 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);
328 #endif