blob: 357781e0b791c6e898b6004f947d269be918539b [file] [log] [blame]
Nishanth Menona0dd7b72014-05-05 08:33:50 -05001/*
Viresh Kumar33692dc2015-09-04 13:47:25 +05302 * Generic OPP helper interface for CPU device
Nishanth Menona0dd7b72014-05-05 08:33:50 -05003 *
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 */
Viresh Kumard6d2a522015-10-17 09:45:18 +053013
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053016#include <linux/cpu.h>
Nishanth Menona0dd7b72014-05-05 08:33:50 -050017#include <linux/cpufreq.h>
Nishanth Menona0dd7b72014-05-05 08:33:50 -050018#include <linux/err.h>
19#include <linux/errno.h>
20#include <linux/export.h>
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053021#include <linux/of.h>
Nishanth Menona0dd7b72014-05-05 08:33:50 -050022#include <linux/slab.h>
23
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053024#include "opp.h"
25
Viresh Kumar33692dc2015-09-04 13:47:25 +053026#ifdef CONFIG_CPU_FREQ
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053027
Nishanth Menona0dd7b72014-05-05 08:33:50 -050028/**
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
Viresh Kumar2c2709d2016-02-16 14:17:53 +053034 * opp table is already initialized and ready for usage.
Nishanth Menona0dd7b72014-05-05 08:33:50 -050035 *
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 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +053047 * Locking: The internal opp_table and opp structures are RCU protected.
Nishanth Menona0dd7b72014-05-05 08:33:50 -050048 * 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 */
52int 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
Anand Moond3599922014-09-05 08:38:30 +053068 freq_table = kcalloc((max_opps + 1), sizeof(*freq_table), GFP_ATOMIC);
Nishanth Menona0dd7b72014-05-05 08:33:50 -050069 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;
Bartlomiej Zolnierkiewicz79eea442015-07-29 16:23:08 +053083
84 /* Is Boost/turbo opp ? */
85 if (dev_pm_opp_is_turbo(opp))
86 freq_table[i].flags = CPUFREQ_BOOST_FREQ;
Nishanth Menona0dd7b72014-05-05 08:33:50 -050087 }
88
89 freq_table[i].driver_data = i;
90 freq_table[i].frequency = CPUFREQ_TABLE_END;
91
92 *table = &freq_table[0];
93
94out:
95 rcu_read_unlock();
96 if (ret)
97 kfree(freq_table);
98
99 return ret;
100}
101EXPORT_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 */
110void 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}
119EXPORT_SYMBOL_GPL(dev_pm_opp_free_cpufreq_table);
Viresh Kumar33692dc2015-09-04 13:47:25 +0530120#endif /* CONFIG_CPU_FREQ */
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530121
Sudeep Holla411466c2016-05-03 15:05:04 +0100122static void
123_dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask, bool of)
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530124{
125 struct device *cpu_dev;
126 int cpu;
127
128 WARN_ON(cpumask_empty(cpumask));
129
130 for_each_cpu(cpu, cpumask) {
131 cpu_dev = get_cpu_device(cpu);
132 if (!cpu_dev) {
133 pr_err("%s: failed to get cpu%d device\n", __func__,
134 cpu);
135 continue;
136 }
137
Sudeep Holla411466c2016-05-03 15:05:04 +0100138 if (of)
139 dev_pm_opp_of_remove_table(cpu_dev);
140 else
141 dev_pm_opp_remove_table(cpu_dev);
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530142 }
143}
Sudeep Holla411466c2016-05-03 15:05:04 +0100144
145/**
146 * dev_pm_opp_cpumask_remove_table() - Removes OPP table for @cpumask
147 * @cpumask: cpumask for which OPP table needs to be removed
148 *
149 * This removes the OPP tables for CPUs present in the @cpumask.
150 * This should be used to remove all the OPPs entries associated with
151 * the cpus in @cpumask.
152 *
153 * Locking: The internal opp_table and opp structures are RCU protected.
154 * Hence this function internally uses RCU updater strategy with mutex locks
155 * to keep the integrity of the internal data structures. Callers should ensure
156 * that this function is *NOT* called under RCU protection or in contexts where
157 * mutex cannot be locked.
158 */
159void dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask)
160{
161 _dev_pm_opp_cpumask_remove_table(cpumask, false);
162}
163EXPORT_SYMBOL_GPL(dev_pm_opp_cpumask_remove_table);
164
165#ifdef CONFIG_OF
166/**
167 * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
168 * @cpumask: cpumask for which OPP table needs to be removed
169 *
170 * This removes the OPP tables for CPUs present in the @cpumask.
171 * This should be used only to remove static entries created from DT.
172 *
173 * Locking: The internal opp_table and opp structures are RCU protected.
174 * Hence this function internally uses RCU updater strategy with mutex locks
175 * to keep the integrity of the internal data structures. Callers should ensure
176 * that this function is *NOT* called under RCU protection or in contexts where
177 * mutex cannot be locked.
178 */
179void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
180{
181 _dev_pm_opp_cpumask_remove_table(cpumask, true);
182}
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530183EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
184
Viresh Kumar45ca36a2016-04-21 14:28:54 +0530185/**
186 * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
187 * @cpumask: cpumask for which OPP table needs to be added.
188 *
189 * This adds the OPP tables for CPUs present in the @cpumask.
190 *
191 * Locking: The internal opp_table and opp structures are RCU protected.
192 * Hence this function internally uses RCU updater strategy with mutex locks
193 * to keep the integrity of the internal data structures. Callers should ensure
194 * that this function is *NOT* called under RCU protection or in contexts where
195 * mutex cannot be locked.
196 */
Arnd Bergmannddbb74b2016-04-30 13:33:29 +0200197int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530198{
199 struct device *cpu_dev;
200 int cpu, ret = 0;
201
202 WARN_ON(cpumask_empty(cpumask));
203
204 for_each_cpu(cpu, cpumask) {
205 cpu_dev = get_cpu_device(cpu);
206 if (!cpu_dev) {
207 pr_err("%s: failed to get cpu%d device\n", __func__,
208 cpu);
209 continue;
210 }
211
212 ret = dev_pm_opp_of_add_table(cpu_dev);
213 if (ret) {
214 pr_err("%s: couldn't find opp table for cpu:%d, %d\n",
215 __func__, cpu, ret);
216
217 /* Free all other OPPs */
218 dev_pm_opp_of_cpumask_remove_table(cpumask);
219 break;
220 }
221 }
222
223 return ret;
224}
225EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
226
227/*
228 * Works only for OPP v2 bindings.
229 *
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530230 * Returns -ENOENT if operating-points-v2 bindings aren't supported.
231 */
Viresh Kumar45ca36a2016-04-21 14:28:54 +0530232/**
233 * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
234 * @cpu_dev using operating-points-v2
235 * bindings.
236 *
237 * @cpu_dev: CPU device for which we do this operation
238 * @cpumask: cpumask to update with information of sharing CPUs
239 *
240 * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
241 *
242 * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
243 *
244 * Locking: The internal opp_table and opp structures are RCU protected.
245 * Hence this function internally uses RCU updater strategy with mutex locks
246 * to keep the integrity of the internal data structures. Callers should ensure
247 * that this function is *NOT* called under RCU protection or in contexts where
248 * mutex cannot be locked.
249 */
Arnd Bergmannddbb74b2016-04-30 13:33:29 +0200250int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530251{
252 struct device_node *np, *tmp_np;
253 struct device *tcpu_dev;
254 int cpu, ret = 0;
255
256 /* Get OPP descriptor node */
257 np = _of_get_opp_desc_node(cpu_dev);
258 if (!np) {
Dan Carpentera6eed752015-09-21 19:26:02 +0300259 dev_dbg(cpu_dev, "%s: Couldn't find cpu_dev node.\n", __func__);
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530260 return -ENOENT;
261 }
262
Pi-Cheng Chend9de19b2015-12-28 21:06:17 +0800263 cpumask_set_cpu(cpu_dev->id, cpumask);
264
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530265 /* OPPs are shared ? */
266 if (!of_property_read_bool(np, "opp-shared"))
267 goto put_cpu_node;
268
269 for_each_possible_cpu(cpu) {
270 if (cpu == cpu_dev->id)
271 continue;
272
273 tcpu_dev = get_cpu_device(cpu);
274 if (!tcpu_dev) {
275 dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
276 __func__, cpu);
277 ret = -ENODEV;
278 goto put_cpu_node;
279 }
280
281 /* Get OPP descriptor node */
282 tmp_np = _of_get_opp_desc_node(tcpu_dev);
283 if (!tmp_np) {
Dan Carpentera6eed752015-09-21 19:26:02 +0300284 dev_err(tcpu_dev, "%s: Couldn't find tcpu_dev node.\n",
285 __func__);
286 ret = -ENOENT;
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530287 goto put_cpu_node;
288 }
289
290 /* CPUs are sharing opp node */
291 if (np == tmp_np)
292 cpumask_set_cpu(cpu, cpumask);
293
294 of_node_put(tmp_np);
295 }
296
297put_cpu_node:
298 of_node_put(np);
299 return ret;
300}
301EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);
302#endif
Viresh Kumar2c931042016-04-21 14:28:56 +0530303
304/**
305 * dev_pm_opp_set_sharing_cpus() - Mark OPP table as shared by few CPUs
306 * @cpu_dev: CPU device for which we do this operation
307 * @cpumask: cpumask of the CPUs which share the OPP table with @cpu_dev
308 *
309 * This marks OPP table of the @cpu_dev as shared by the CPUs present in
310 * @cpumask.
311 *
312 * Returns -ENODEV if OPP table isn't already present.
313 *
314 * Locking: The internal opp_table and opp structures are RCU protected.
315 * Hence this function internally uses RCU updater strategy with mutex locks
316 * to keep the integrity of the internal data structures. Callers should ensure
317 * that this function is *NOT* called under RCU protection or in contexts where
318 * mutex cannot be locked.
319 */
Viresh Kumardde370b2016-04-27 08:52:22 +0530320int dev_pm_opp_set_sharing_cpus(struct device *cpu_dev,
Arnd Bergmannddbb74b2016-04-30 13:33:29 +0200321 const struct cpumask *cpumask)
Viresh Kumar2c931042016-04-21 14:28:56 +0530322{
323 struct opp_device *opp_dev;
324 struct opp_table *opp_table;
325 struct device *dev;
326 int cpu, ret = 0;
327
328 mutex_lock(&opp_table_lock);
329
330 opp_table = _find_opp_table(cpu_dev);
331 if (IS_ERR(opp_table)) {
332 ret = PTR_ERR(opp_table);
333 goto unlock;
334 }
335
336 for_each_cpu(cpu, cpumask) {
337 if (cpu == cpu_dev->id)
338 continue;
339
340 dev = get_cpu_device(cpu);
341 if (!dev) {
342 dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
343 __func__, cpu);
344 continue;
345 }
346
347 opp_dev = _add_opp_dev(dev, opp_table);
348 if (!opp_dev) {
349 dev_err(dev, "%s: failed to add opp-dev for cpu%d device\n",
350 __func__, cpu);
351 continue;
352 }
Viresh Kumar46e7a4e2016-04-21 14:28:57 +0530353
354 /* Mark opp-table as multiple CPUs are sharing it now */
355 opp_table->shared_opp = true;
Viresh Kumar2c931042016-04-21 14:28:56 +0530356 }
357unlock:
358 mutex_unlock(&opp_table_lock);
359
360 return ret;
361}
362EXPORT_SYMBOL_GPL(dev_pm_opp_set_sharing_cpus);
Viresh Kumar6f707da2016-04-27 08:52:23 +0530363
364/**
365 * dev_pm_opp_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with @cpu_dev
366 * @cpu_dev: CPU device for which we do this operation
367 * @cpumask: cpumask to update with information of sharing CPUs
368 *
369 * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
370 *
371 * Returns -ENODEV if OPP table isn't already present.
372 *
373 * Locking: The internal opp_table and opp structures are RCU protected.
374 * Hence this function internally uses RCU updater strategy with mutex locks
375 * to keep the integrity of the internal data structures. Callers should ensure
376 * that this function is *NOT* called under RCU protection or in contexts where
377 * mutex cannot be locked.
378 */
Arnd Bergmannddbb74b2016-04-30 13:33:29 +0200379int dev_pm_opp_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
Viresh Kumar6f707da2016-04-27 08:52:23 +0530380{
381 struct opp_device *opp_dev;
382 struct opp_table *opp_table;
383 int ret = 0;
384
385 mutex_lock(&opp_table_lock);
386
387 opp_table = _find_opp_table(cpu_dev);
388 if (IS_ERR(opp_table)) {
389 ret = PTR_ERR(opp_table);
390 goto unlock;
391 }
392
393 cpumask_clear(cpumask);
394
395 if (opp_table->shared_opp) {
396 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
397 cpumask_set_cpu(opp_dev->dev->id, cpumask);
398 } else {
399 cpumask_set_cpu(cpu_dev->id, cpumask);
400 }
401
402unlock:
403 mutex_unlock(&opp_table_lock);
404
405 return ret;
406}
407EXPORT_SYMBOL_GPL(dev_pm_opp_get_sharing_cpus);