mmc: rtsx_pci: Enable MMC_CAP_ERASE to allow erase/discard/trim requests
[linux/fpc-iii.git] / drivers / base / power / opp / cpu.c
blob8c3434bdb26dee8c23a637798b18a3963666ee70
1 /*
2 * Generic OPP helper interface for CPU device
4 * Copyright (C) 2009-2014 Texas Instruments Incorporated.
5 * Nishanth Menon
6 * Romit Dasgupta
7 * Kevin Hilman
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>
23 #include "opp.h"
25 #ifdef CONFIG_CPU_FREQ
27 /**
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 * Locking: The internal opp_table and opp structures are RCU protected.
47 * Since we just use the regular accessor functions to access the internal data
48 * structures, we use RCU read lock inside this function. As a result, users of
49 * this function DONOT need to use explicit locks for invoking.
51 int dev_pm_opp_init_cpufreq_table(struct device *dev,
52 struct cpufreq_frequency_table **table)
54 struct dev_pm_opp *opp;
55 struct cpufreq_frequency_table *freq_table = NULL;
56 int i, max_opps, ret = 0;
57 unsigned long rate;
59 rcu_read_lock();
61 max_opps = dev_pm_opp_get_opp_count(dev);
62 if (max_opps <= 0) {
63 ret = max_opps ? max_opps : -ENODATA;
64 goto out;
67 freq_table = kcalloc((max_opps + 1), sizeof(*freq_table), GFP_ATOMIC);
68 if (!freq_table) {
69 ret = -ENOMEM;
70 goto out;
73 for (i = 0, rate = 0; i < max_opps; i++, rate++) {
74 /* find next rate */
75 opp = dev_pm_opp_find_freq_ceil(dev, &rate);
76 if (IS_ERR(opp)) {
77 ret = PTR_ERR(opp);
78 goto out;
80 freq_table[i].driver_data = i;
81 freq_table[i].frequency = rate / 1000;
83 /* Is Boost/turbo opp ? */
84 if (dev_pm_opp_is_turbo(opp))
85 freq_table[i].flags = CPUFREQ_BOOST_FREQ;
88 freq_table[i].driver_data = i;
89 freq_table[i].frequency = CPUFREQ_TABLE_END;
91 *table = &freq_table[0];
93 out:
94 rcu_read_unlock();
95 if (ret)
96 kfree(freq_table);
98 return ret;
100 EXPORT_SYMBOL_GPL(dev_pm_opp_init_cpufreq_table);
103 * dev_pm_opp_free_cpufreq_table() - free the cpufreq table
104 * @dev: device for which we do this operation
105 * @table: table to free
107 * Free up the table allocated by dev_pm_opp_init_cpufreq_table
109 void dev_pm_opp_free_cpufreq_table(struct device *dev,
110 struct cpufreq_frequency_table **table)
112 if (!table)
113 return;
115 kfree(*table);
116 *table = NULL;
118 EXPORT_SYMBOL_GPL(dev_pm_opp_free_cpufreq_table);
119 #endif /* CONFIG_CPU_FREQ */
121 void _dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask, bool of)
123 struct device *cpu_dev;
124 int cpu;
126 WARN_ON(cpumask_empty(cpumask));
128 for_each_cpu(cpu, cpumask) {
129 cpu_dev = get_cpu_device(cpu);
130 if (!cpu_dev) {
131 pr_err("%s: failed to get cpu%d device\n", __func__,
132 cpu);
133 continue;
136 if (of)
137 dev_pm_opp_of_remove_table(cpu_dev);
138 else
139 dev_pm_opp_remove_table(cpu_dev);
144 * dev_pm_opp_cpumask_remove_table() - Removes OPP table for @cpumask
145 * @cpumask: cpumask for which OPP table needs to be removed
147 * This removes the OPP tables for CPUs present in the @cpumask.
148 * This should be used to remove all the OPPs entries associated with
149 * the cpus in @cpumask.
151 * Locking: The internal opp_table and opp structures are RCU protected.
152 * Hence this function internally uses RCU updater strategy with mutex locks
153 * to keep the integrity of the internal data structures. Callers should ensure
154 * that this function is *NOT* called under RCU protection or in contexts where
155 * mutex cannot be locked.
157 void dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask)
159 _dev_pm_opp_cpumask_remove_table(cpumask, false);
161 EXPORT_SYMBOL_GPL(dev_pm_opp_cpumask_remove_table);
164 * dev_pm_opp_set_sharing_cpus() - Mark OPP table as shared by few CPUs
165 * @cpu_dev: CPU device for which we do this operation
166 * @cpumask: cpumask of the CPUs which share the OPP table with @cpu_dev
168 * This marks OPP table of the @cpu_dev as shared by the CPUs present in
169 * @cpumask.
171 * Returns -ENODEV if OPP table isn't already present.
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.
179 int dev_pm_opp_set_sharing_cpus(struct device *cpu_dev,
180 const struct cpumask *cpumask)
182 struct opp_device *opp_dev;
183 struct opp_table *opp_table;
184 struct device *dev;
185 int cpu, ret = 0;
187 mutex_lock(&opp_table_lock);
189 opp_table = _find_opp_table(cpu_dev);
190 if (IS_ERR(opp_table)) {
191 ret = PTR_ERR(opp_table);
192 goto unlock;
195 for_each_cpu(cpu, cpumask) {
196 if (cpu == cpu_dev->id)
197 continue;
199 dev = get_cpu_device(cpu);
200 if (!dev) {
201 dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
202 __func__, cpu);
203 continue;
206 opp_dev = _add_opp_dev(dev, opp_table);
207 if (!opp_dev) {
208 dev_err(dev, "%s: failed to add opp-dev for cpu%d device\n",
209 __func__, cpu);
210 continue;
213 /* Mark opp-table as multiple CPUs are sharing it now */
214 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
216 unlock:
217 mutex_unlock(&opp_table_lock);
219 return ret;
221 EXPORT_SYMBOL_GPL(dev_pm_opp_set_sharing_cpus);
224 * dev_pm_opp_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with @cpu_dev
225 * @cpu_dev: CPU device for which we do this operation
226 * @cpumask: cpumask to update with information of sharing CPUs
228 * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
230 * Returns -ENODEV if OPP table isn't already present and -EINVAL if the OPP
231 * table's status is access-unknown.
233 * Locking: The internal opp_table and opp structures are RCU protected.
234 * Hence this function internally 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.
239 int dev_pm_opp_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
241 struct opp_device *opp_dev;
242 struct opp_table *opp_table;
243 int ret = 0;
245 mutex_lock(&opp_table_lock);
247 opp_table = _find_opp_table(cpu_dev);
248 if (IS_ERR(opp_table)) {
249 ret = PTR_ERR(opp_table);
250 goto unlock;
253 if (opp_table->shared_opp == OPP_TABLE_ACCESS_UNKNOWN) {
254 ret = -EINVAL;
255 goto unlock;
258 cpumask_clear(cpumask);
260 if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {
261 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
262 cpumask_set_cpu(opp_dev->dev->id, cpumask);
263 } else {
264 cpumask_set_cpu(cpu_dev->id, cpumask);
267 unlock:
268 mutex_unlock(&opp_table_lock);
270 return ret;
272 EXPORT_SYMBOL_GPL(dev_pm_opp_get_sharing_cpus);