1
2
3
4
5
6
7
8
9
10
11
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/slab.h>
22
23#include "opp.h"
24
25#ifdef CONFIG_CPU_FREQ
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46int dev_pm_opp_init_cpufreq_table(struct device *dev,
47 struct cpufreq_frequency_table **table)
48{
49 struct dev_pm_opp *opp;
50 struct cpufreq_frequency_table *freq_table = NULL;
51 int i, max_opps, ret = 0;
52 unsigned long rate;
53
54 max_opps = dev_pm_opp_get_opp_count(dev);
55 if (max_opps <= 0)
56 return max_opps ? max_opps : -ENODATA;
57
58 freq_table = kcalloc((max_opps + 1), sizeof(*freq_table), GFP_KERNEL);
59 if (!freq_table)
60 return -ENOMEM;
61
62 for (i = 0, rate = 0; i < max_opps; i++, rate++) {
63
64 opp = dev_pm_opp_find_freq_ceil(dev, &rate);
65 if (IS_ERR(opp)) {
66 ret = PTR_ERR(opp);
67 goto out;
68 }
69 freq_table[i].driver_data = i;
70 freq_table[i].frequency = rate / 1000;
71
72
73 if (dev_pm_opp_is_turbo(opp))
74 freq_table[i].flags = CPUFREQ_BOOST_FREQ;
75
76 dev_pm_opp_put(opp);
77 }
78
79 freq_table[i].driver_data = i;
80 freq_table[i].frequency = CPUFREQ_TABLE_END;
81
82 *table = &freq_table[0];
83
84out:
85 if (ret)
86 kfree(freq_table);
87
88 return ret;
89}
90EXPORT_SYMBOL_GPL(dev_pm_opp_init_cpufreq_table);
91
92
93
94
95
96
97
98
99void dev_pm_opp_free_cpufreq_table(struct device *dev,
100 struct cpufreq_frequency_table **table)
101{
102 if (!table)
103 return;
104
105 kfree(*table);
106 *table = NULL;
107}
108EXPORT_SYMBOL_GPL(dev_pm_opp_free_cpufreq_table);
109#endif
110
111void _dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask,
112 int last_cpu)
113{
114 struct device *cpu_dev;
115 int cpu;
116
117 WARN_ON(cpumask_empty(cpumask));
118
119 for_each_cpu(cpu, cpumask) {
120 if (cpu == last_cpu)
121 break;
122
123 cpu_dev = get_cpu_device(cpu);
124 if (!cpu_dev) {
125 pr_err("%s: failed to get cpu%d device\n", __func__,
126 cpu);
127 continue;
128 }
129
130 _dev_pm_opp_find_and_remove_table(cpu_dev);
131 }
132}
133
134
135
136
137
138
139
140
141
142void dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask)
143{
144 _dev_pm_opp_cpumask_remove_table(cpumask, -1);
145}
146EXPORT_SYMBOL_GPL(dev_pm_opp_cpumask_remove_table);
147
148
149
150
151
152
153
154
155
156
157
158int dev_pm_opp_set_sharing_cpus(struct device *cpu_dev,
159 const struct cpumask *cpumask)
160{
161 struct opp_device *opp_dev;
162 struct opp_table *opp_table;
163 struct device *dev;
164 int cpu, ret = 0;
165
166 opp_table = _find_opp_table(cpu_dev);
167 if (IS_ERR(opp_table))
168 return PTR_ERR(opp_table);
169
170 for_each_cpu(cpu, cpumask) {
171 if (cpu == cpu_dev->id)
172 continue;
173
174 dev = get_cpu_device(cpu);
175 if (!dev) {
176 dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
177 __func__, cpu);
178 continue;
179 }
180
181 opp_dev = _add_opp_dev(dev, opp_table);
182 if (!opp_dev) {
183 dev_err(dev, "%s: failed to add opp-dev for cpu%d device\n",
184 __func__, cpu);
185 continue;
186 }
187
188
189 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
190 }
191
192 dev_pm_opp_put_opp_table(opp_table);
193
194 return ret;
195}
196EXPORT_SYMBOL_GPL(dev_pm_opp_set_sharing_cpus);
197
198
199
200
201
202
203
204
205
206
207
208int dev_pm_opp_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
209{
210 struct opp_device *opp_dev;
211 struct opp_table *opp_table;
212 int ret = 0;
213
214 opp_table = _find_opp_table(cpu_dev);
215 if (IS_ERR(opp_table))
216 return PTR_ERR(opp_table);
217
218 if (opp_table->shared_opp == OPP_TABLE_ACCESS_UNKNOWN) {
219 ret = -EINVAL;
220 goto put_opp_table;
221 }
222
223 cpumask_clear(cpumask);
224
225 if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {
226 mutex_lock(&opp_table->lock);
227 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
228 cpumask_set_cpu(opp_dev->dev->id, cpumask);
229 mutex_unlock(&opp_table->lock);
230 } else {
231 cpumask_set_cpu(cpu_dev->id, cpumask);
232 }
233
234put_opp_table:
235 dev_pm_opp_put_opp_table(opp_table);
236
237 return ret;
238}
239EXPORT_SYMBOL_GPL(dev_pm_opp_get_sharing_cpus);
240