Commit | Line | Data |
---|---|---|
a0dd7b79 | 1 | /* |
33692dc3 | 2 | * Generic OPP helper interface for CPU device |
a0dd7b79 NM |
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 | */ | |
d6d2a528 VK |
13 | |
14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
15 | ||
f59d3ee8 | 16 | #include <linux/cpu.h> |
a0dd7b79 | 17 | #include <linux/cpufreq.h> |
a0dd7b79 NM |
18 | #include <linux/err.h> |
19 | #include <linux/errno.h> | |
20 | #include <linux/export.h> | |
a0dd7b79 NM |
21 | #include <linux/slab.h> |
22 | ||
f59d3ee8 VK |
23 | #include "opp.h" |
24 | ||
33692dc3 | 25 | #ifdef CONFIG_CPU_FREQ |
f59d3ee8 | 26 | |
a0dd7b79 NM |
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 | |
31 | * | |
32 | * Generate a cpufreq table for a provided device- this assumes that the | |
2c2709dc | 33 | * opp table is already initialized and ready for usage. |
a0dd7b79 NM |
34 | * |
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. | |
38 | * | |
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. | |
42 | * | |
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. | |
a0dd7b79 NM |
45 | */ |
46 | int 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 | ||
a0dd7b79 | 54 | max_opps = dev_pm_opp_get_opp_count(dev); |
8a31d9d9 VK |
55 | if (max_opps <= 0) |
56 | return max_opps ? max_opps : -ENODATA; | |
a0dd7b79 | 57 | |
4a823c0b | 58 | freq_table = kcalloc((max_opps + 1), sizeof(*freq_table), GFP_KERNEL); |
8a31d9d9 VK |
59 | if (!freq_table) |
60 | return -ENOMEM; | |
a0dd7b79 NM |
61 | |
62 | for (i = 0, rate = 0; i < max_opps; i++, rate++) { | |
63 | /* find next rate */ | |
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; | |
79eea44a BZ |
71 | |
72 | /* Is Boost/turbo opp ? */ | |
73 | if (dev_pm_opp_is_turbo(opp)) | |
74 | freq_table[i].flags = CPUFREQ_BOOST_FREQ; | |
8a31d9d9 VK |
75 | |
76 | dev_pm_opp_put(opp); | |
a0dd7b79 NM |
77 | } |
78 | ||
79 | freq_table[i].driver_data = i; | |
80 | freq_table[i].frequency = CPUFREQ_TABLE_END; | |
81 | ||
82 | *table = &freq_table[0]; | |
83 | ||
84 | out: | |
a0dd7b79 NM |
85 | if (ret) |
86 | kfree(freq_table); | |
87 | ||
88 | return ret; | |
89 | } | |
90 | EXPORT_SYMBOL_GPL(dev_pm_opp_init_cpufreq_table); | |
91 | ||
92 | /** | |
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 | |
96 | * | |
97 | * Free up the table allocated by dev_pm_opp_init_cpufreq_table | |
98 | */ | |
99 | void 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 | } | |
108 | EXPORT_SYMBOL_GPL(dev_pm_opp_free_cpufreq_table); | |
33692dc3 | 109 | #endif /* CONFIG_CPU_FREQ */ |
f59d3ee8 | 110 | |
404b1369 VK |
111 | void _dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask, bool of, |
112 | int last_cpu) | |
f59d3ee8 VK |
113 | { |
114 | struct device *cpu_dev; | |
115 | int cpu; | |
116 | ||
117 | WARN_ON(cpumask_empty(cpumask)); | |
118 | ||
119 | for_each_cpu(cpu, cpumask) { | |
404b1369 VK |
120 | if (cpu == last_cpu) |
121 | break; | |
122 | ||
f59d3ee8 VK |
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 | ||
411466c5 SH |
130 | if (of) |
131 | dev_pm_opp_of_remove_table(cpu_dev); | |
132 | else | |
133 | dev_pm_opp_remove_table(cpu_dev); | |
f59d3ee8 VK |
134 | } |
135 | } | |
411466c5 SH |
136 | |
137 | /** | |
138 | * dev_pm_opp_cpumask_remove_table() - Removes OPP table for @cpumask | |
139 | * @cpumask: cpumask for which OPP table needs to be removed | |
140 | * | |
141 | * This removes the OPP tables for CPUs present in the @cpumask. | |
142 | * This should be used to remove all the OPPs entries associated with | |
143 | * the cpus in @cpumask. | |
411466c5 SH |
144 | */ |
145 | void dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask) | |
146 | { | |
404b1369 | 147 | _dev_pm_opp_cpumask_remove_table(cpumask, false, -1); |
411466c5 SH |
148 | } |
149 | EXPORT_SYMBOL_GPL(dev_pm_opp_cpumask_remove_table); | |
150 | ||
2c93104f VK |
151 | /** |
152 | * dev_pm_opp_set_sharing_cpus() - Mark OPP table as shared by few CPUs | |
153 | * @cpu_dev: CPU device for which we do this operation | |
154 | * @cpumask: cpumask of the CPUs which share the OPP table with @cpu_dev | |
155 | * | |
156 | * This marks OPP table of the @cpu_dev as shared by the CPUs present in | |
157 | * @cpumask. | |
158 | * | |
159 | * Returns -ENODEV if OPP table isn't already present. | |
2c93104f | 160 | */ |
dde370b2 | 161 | int dev_pm_opp_set_sharing_cpus(struct device *cpu_dev, |
ddbb74bc | 162 | const struct cpumask *cpumask) |
2c93104f VK |
163 | { |
164 | struct opp_device *opp_dev; | |
165 | struct opp_table *opp_table; | |
166 | struct device *dev; | |
167 | int cpu, ret = 0; | |
168 | ||
2c93104f | 169 | opp_table = _find_opp_table(cpu_dev); |
5b650b38 VK |
170 | if (IS_ERR(opp_table)) |
171 | return PTR_ERR(opp_table); | |
2c93104f VK |
172 | |
173 | for_each_cpu(cpu, cpumask) { | |
174 | if (cpu == cpu_dev->id) | |
175 | continue; | |
176 | ||
177 | dev = get_cpu_device(cpu); | |
178 | if (!dev) { | |
179 | dev_err(cpu_dev, "%s: failed to get cpu%d device\n", | |
180 | __func__, cpu); | |
181 | continue; | |
182 | } | |
183 | ||
184 | opp_dev = _add_opp_dev(dev, opp_table); | |
185 | if (!opp_dev) { | |
186 | dev_err(dev, "%s: failed to add opp-dev for cpu%d device\n", | |
187 | __func__, cpu); | |
188 | continue; | |
189 | } | |
46e7a4e1 VK |
190 | |
191 | /* Mark opp-table as multiple CPUs are sharing it now */ | |
79ee2e8f | 192 | opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED; |
2c93104f | 193 | } |
5b650b38 VK |
194 | |
195 | dev_pm_opp_put_opp_table(opp_table); | |
2c93104f VK |
196 | |
197 | return ret; | |
198 | } | |
199 | EXPORT_SYMBOL_GPL(dev_pm_opp_set_sharing_cpus); | |
6f707daa VK |
200 | |
201 | /** | |
202 | * dev_pm_opp_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with @cpu_dev | |
203 | * @cpu_dev: CPU device for which we do this operation | |
204 | * @cpumask: cpumask to update with information of sharing CPUs | |
205 | * | |
206 | * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev. | |
207 | * | |
79ee2e8f VK |
208 | * Returns -ENODEV if OPP table isn't already present and -EINVAL if the OPP |
209 | * table's status is access-unknown. | |
6f707daa | 210 | */ |
ddbb74bc | 211 | int dev_pm_opp_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask) |
6f707daa VK |
212 | { |
213 | struct opp_device *opp_dev; | |
214 | struct opp_table *opp_table; | |
215 | int ret = 0; | |
216 | ||
6f707daa | 217 | opp_table = _find_opp_table(cpu_dev); |
5b650b38 VK |
218 | if (IS_ERR(opp_table)) |
219 | return PTR_ERR(opp_table); | |
6f707daa | 220 | |
79ee2e8f VK |
221 | if (opp_table->shared_opp == OPP_TABLE_ACCESS_UNKNOWN) { |
222 | ret = -EINVAL; | |
5b650b38 | 223 | goto put_opp_table; |
79ee2e8f VK |
224 | } |
225 | ||
6f707daa VK |
226 | cpumask_clear(cpumask); |
227 | ||
79ee2e8f | 228 | if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) { |
6f707daa VK |
229 | list_for_each_entry(opp_dev, &opp_table->dev_list, node) |
230 | cpumask_set_cpu(opp_dev->dev->id, cpumask); | |
231 | } else { | |
232 | cpumask_set_cpu(cpu_dev->id, cpumask); | |
233 | } | |
234 | ||
5b650b38 VK |
235 | put_opp_table: |
236 | dev_pm_opp_put_opp_table(opp_table); | |
6f707daa VK |
237 | |
238 | return ret; | |
239 | } | |
240 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_sharing_cpus); |