2 * Generic OPP helper interface for CPU device
4 * Copyright (C) 2009-2014 Texas Instruments Incorporated.
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.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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/slab.h>
25 #ifdef CONFIG_CPU_FREQ
28 * dev_pm_opp_init_cpufreq_table() - create a cpufreq table for a device
29 * @dev: device for which we do this operation
30 * @table: Cpufreq table returned back to caller
32 * Generate a cpufreq table for a provided device- this assumes that the
33 * opp table is already initialized and ready for usage.
35 * This function allocates required memory for the cpufreq table. It is
36 * expected that the caller does the required maintenance such as freeing
37 * the table as required.
39 * Returns -EINVAL for bad pointers, -ENODEV if the device is not found, -ENOMEM
40 * if no memory available for the operation (table is not populated), returns 0
41 * if successful and table is populated.
43 * WARNING: It is important for the callers to ensure refreshing their copy of
44 * the table if any of the mentioned functions have been invoked in the interim.
46 int dev_pm_opp_init_cpufreq_table(struct device *dev,
47 struct cpufreq_frequency_table **table)
49 struct dev_pm_opp *opp;
50 struct cpufreq_frequency_table *freq_table = NULL;
51 int i, max_opps, ret = 0;
54 max_opps = dev_pm_opp_get_opp_count(dev);
56 return max_opps ? max_opps : -ENODATA;
58 freq_table = kcalloc((max_opps + 1), sizeof(*freq_table), GFP_ATOMIC);
62 for (i = 0, rate = 0; i < max_opps; i++, rate++) {
64 opp = dev_pm_opp_find_freq_ceil(dev, &rate);
69 freq_table[i].driver_data = i;
70 freq_table[i].frequency = rate / 1000;
72 /* Is Boost/turbo opp ? */
73 if (dev_pm_opp_is_turbo(opp))
74 freq_table[i].flags = CPUFREQ_BOOST_FREQ;
79 freq_table[i].driver_data = i;
80 freq_table[i].frequency = CPUFREQ_TABLE_END;
82 *table = &freq_table[0];
90 EXPORT_SYMBOL_GPL(dev_pm_opp_init_cpufreq_table);
93 * dev_pm_opp_free_cpufreq_table() - free the cpufreq table
94 * @dev: device for which we do this operation
95 * @table: table to free
97 * Free up the table allocated by dev_pm_opp_init_cpufreq_table
99 void dev_pm_opp_free_cpufreq_table(struct device *dev,
100 struct cpufreq_frequency_table **table)
108 EXPORT_SYMBOL_GPL(dev_pm_opp_free_cpufreq_table);
109 #endif /* CONFIG_CPU_FREQ */
111 void _dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask, bool of)
113 struct device *cpu_dev;
116 WARN_ON(cpumask_empty(cpumask));
118 for_each_cpu(cpu, cpumask) {
119 cpu_dev = get_cpu_device(cpu);
121 pr_err("%s: failed to get cpu%d device\n", __func__,
127 dev_pm_opp_of_remove_table(cpu_dev);
129 dev_pm_opp_remove_table(cpu_dev);
134 * dev_pm_opp_cpumask_remove_table() - Removes OPP table for @cpumask
135 * @cpumask: cpumask for which OPP table needs to be removed
137 * This removes the OPP tables for CPUs present in the @cpumask.
138 * This should be used to remove all the OPPs entries associated with
139 * the cpus in @cpumask.
141 * Locking: The internal opp_table and opp structures are RCU protected.
142 * Hence this function internally uses RCU updater strategy with mutex locks
143 * to keep the integrity of the internal data structures. Callers should ensure
144 * that this function is *NOT* called under RCU protection or in contexts where
145 * mutex cannot be locked.
147 void dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask)
149 _dev_pm_opp_cpumask_remove_table(cpumask, false);
151 EXPORT_SYMBOL_GPL(dev_pm_opp_cpumask_remove_table);
154 * dev_pm_opp_set_sharing_cpus() - Mark OPP table as shared by few CPUs
155 * @cpu_dev: CPU device for which we do this operation
156 * @cpumask: cpumask of the CPUs which share the OPP table with @cpu_dev
158 * This marks OPP table of the @cpu_dev as shared by the CPUs present in
161 * Returns -ENODEV if OPP table isn't already present.
163 * Locking: The internal opp_table and opp structures are RCU protected.
164 * Hence this function internally uses RCU updater strategy with mutex locks
165 * to keep the integrity of the internal data structures. Callers should ensure
166 * that this function is *NOT* called under RCU protection or in contexts where
167 * mutex cannot be locked.
169 int dev_pm_opp_set_sharing_cpus(struct device *cpu_dev,
170 const struct cpumask *cpumask)
172 struct opp_device *opp_dev;
173 struct opp_table *opp_table;
177 opp_table = _find_opp_table(cpu_dev);
178 if (IS_ERR(opp_table))
179 return PTR_ERR(opp_table);
181 for_each_cpu(cpu, cpumask) {
182 if (cpu == cpu_dev->id)
185 dev = get_cpu_device(cpu);
187 dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
192 opp_dev = _add_opp_dev(dev, opp_table);
194 dev_err(dev, "%s: failed to add opp-dev for cpu%d device\n",
199 /* Mark opp-table as multiple CPUs are sharing it now */
200 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
203 dev_pm_opp_put_opp_table(opp_table);
207 EXPORT_SYMBOL_GPL(dev_pm_opp_set_sharing_cpus);
210 * dev_pm_opp_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with @cpu_dev
211 * @cpu_dev: CPU device for which we do this operation
212 * @cpumask: cpumask to update with information of sharing CPUs
214 * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
216 * Returns -ENODEV if OPP table isn't already present and -EINVAL if the OPP
217 * table's status is access-unknown.
219 * Locking: The internal opp_table and opp structures are RCU protected.
220 * Hence this function internally uses RCU updater strategy with mutex locks
221 * to keep the integrity of the internal data structures. Callers should ensure
222 * that this function is *NOT* called under RCU protection or in contexts where
223 * mutex cannot be locked.
225 int dev_pm_opp_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
227 struct opp_device *opp_dev;
228 struct opp_table *opp_table;
231 opp_table = _find_opp_table(cpu_dev);
232 if (IS_ERR(opp_table))
233 return PTR_ERR(opp_table);
235 if (opp_table->shared_opp == OPP_TABLE_ACCESS_UNKNOWN) {
240 cpumask_clear(cpumask);
242 if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {
243 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
244 cpumask_set_cpu(opp_dev->dev->id, cpumask);
246 cpumask_set_cpu(cpu_dev->id, cpumask);
250 dev_pm_opp_put_opp_table(opp_table);
254 EXPORT_SYMBOL_GPL(dev_pm_opp_get_sharing_cpus);