Commit | Line | Data |
---|---|---|
e1f60b29 NM |
1 | /* |
2 | * Generic OPP Interface | |
3 | * | |
4 | * Copyright (C) 2009-2010 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 | */ | |
13 | ||
8d4d4e98 | 14 | #include <linux/cpu.h> |
e1f60b29 NM |
15 | #include <linux/kernel.h> |
16 | #include <linux/errno.h> | |
17 | #include <linux/err.h> | |
e1f60b29 | 18 | #include <linux/slab.h> |
51990e82 | 19 | #include <linux/device.h> |
e1f60b29 NM |
20 | #include <linux/list.h> |
21 | #include <linux/rculist.h> | |
22 | #include <linux/rcupdate.h> | |
e4db1c74 | 23 | #include <linux/pm_opp.h> |
b496dfbc | 24 | #include <linux/of.h> |
80126ce7 | 25 | #include <linux/export.h> |
e1f60b29 NM |
26 | |
27 | /* | |
28 | * Internal data structure organization with the OPP layer library is as | |
29 | * follows: | |
30 | * dev_opp_list (root) | |
31 | * |- device 1 (represents voltage domain 1) | |
32 | * | |- opp 1 (availability, freq, voltage) | |
33 | * | |- opp 2 .. | |
34 | * ... ... | |
35 | * | `- opp n .. | |
36 | * |- device 2 (represents the next voltage domain) | |
37 | * ... | |
38 | * `- device m (represents mth voltage domain) | |
39 | * device 1, 2.. are represented by dev_opp structure while each opp | |
40 | * is represented by the opp structure. | |
41 | */ | |
42 | ||
43 | /** | |
47d43ba7 | 44 | * struct dev_pm_opp - Generic OPP description structure |
e1f60b29 NM |
45 | * @node: opp list node. The nodes are maintained throughout the lifetime |
46 | * of boot. It is expected only an optimal set of OPPs are | |
47 | * added to the library by the SoC framework. | |
48 | * RCU usage: opp list is traversed with RCU locks. node | |
49 | * modification is possible realtime, hence the modifications | |
50 | * are protected by the dev_opp_list_lock for integrity. | |
51 | * IMPORTANT: the opp nodes should be maintained in increasing | |
52 | * order. | |
38393409 | 53 | * @dynamic: not-created from static DT entries. |
e1f60b29 | 54 | * @available: true/false - marks if this OPP as available or not |
27465902 | 55 | * @turbo: true if turbo (boost) OPP |
e1f60b29 | 56 | * @rate: Frequency in hertz |
27465902 VK |
57 | * @u_volt: Target voltage in microvolts corresponding to this OPP |
58 | * @u_volt_min: Minimum voltage in microvolts corresponding to this OPP | |
59 | * @u_volt_max: Maximum voltage in microvolts corresponding to this OPP | |
60 | * @u_amp: Maximum current drawn by the device in microamperes | |
3ca9bb33 VK |
61 | * @clock_latency_ns: Latency (in nanoseconds) of switching to this OPP's |
62 | * frequency from any other OPP's frequency. | |
e1f60b29 | 63 | * @dev_opp: points back to the device_opp struct this opp belongs to |
cd1a068a | 64 | * @rcu_head: RCU callback head used for deferred freeing |
27465902 | 65 | * @np: OPP's device node. |
e1f60b29 NM |
66 | * |
67 | * This structure stores the OPP information for a given device. | |
68 | */ | |
47d43ba7 | 69 | struct dev_pm_opp { |
e1f60b29 NM |
70 | struct list_head node; |
71 | ||
72 | bool available; | |
38393409 | 73 | bool dynamic; |
27465902 | 74 | bool turbo; |
e1f60b29 | 75 | unsigned long rate; |
27465902 | 76 | |
e1f60b29 | 77 | unsigned long u_volt; |
27465902 VK |
78 | unsigned long u_volt_min; |
79 | unsigned long u_volt_max; | |
80 | unsigned long u_amp; | |
3ca9bb33 | 81 | unsigned long clock_latency_ns; |
e1f60b29 NM |
82 | |
83 | struct device_opp *dev_opp; | |
cd1a068a | 84 | struct rcu_head rcu_head; |
27465902 VK |
85 | |
86 | struct device_node *np; | |
e1f60b29 NM |
87 | }; |
88 | ||
06441658 VK |
89 | /** |
90 | * struct device_list_opp - devices managed by 'struct device_opp' | |
91 | * @node: list node | |
92 | * @dev: device to which the struct object belongs | |
93 | * @rcu_head: RCU callback head used for deferred freeing | |
94 | * | |
95 | * This is an internal data structure maintaining the list of devices that are | |
96 | * managed by 'struct device_opp'. | |
97 | */ | |
98 | struct device_list_opp { | |
99 | struct list_head node; | |
100 | const struct device *dev; | |
101 | struct rcu_head rcu_head; | |
102 | }; | |
103 | ||
e1f60b29 NM |
104 | /** |
105 | * struct device_opp - Device opp structure | |
106 | * @node: list node - contains the devices with OPPs that | |
107 | * have been registered. Nodes once added are not modified in this | |
108 | * list. | |
109 | * RCU usage: nodes are not modified in the list of device_opp, | |
110 | * however addition is possible and is secured by dev_opp_list_lock | |
cd1a068a | 111 | * @srcu_head: notifier head to notify the OPP availability changes. |
129eec55 | 112 | * @rcu_head: RCU callback head used for deferred freeing |
06441658 | 113 | * @dev_list: list of devices that share these OPPs |
e1f60b29 | 114 | * @opp_list: list of opps |
06441658 VK |
115 | * @np: struct device_node pointer for opp's DT node. |
116 | * @shared_opp: OPP is shared between multiple devices. | |
e1f60b29 NM |
117 | * |
118 | * This is an internal data structure maintaining the link to opps attached to | |
119 | * a device. This structure is not meant to be shared to users as it is | |
1c6a662f VK |
120 | * meant for book keeping and private to OPP library. |
121 | * | |
122 | * Because the opp structures can be used from both rcu and srcu readers, we | |
123 | * need to wait for the grace period of both of them before freeing any | |
124 | * resources. And so we have used kfree_rcu() from within call_srcu() handlers. | |
e1f60b29 NM |
125 | */ |
126 | struct device_opp { | |
127 | struct list_head node; | |
128 | ||
cd1a068a | 129 | struct srcu_notifier_head srcu_head; |
129eec55 | 130 | struct rcu_head rcu_head; |
06441658 | 131 | struct list_head dev_list; |
e1f60b29 | 132 | struct list_head opp_list; |
3ca9bb33 | 133 | |
06441658 | 134 | struct device_node *np; |
3ca9bb33 | 135 | unsigned long clock_latency_ns_max; |
06441658 | 136 | bool shared_opp; |
ad656a6a | 137 | struct dev_pm_opp *suspend_opp; |
e1f60b29 NM |
138 | }; |
139 | ||
140 | /* | |
141 | * The root of the list of all devices. All device_opp structures branch off | |
142 | * from here, with each device_opp containing the list of opp it supports in | |
143 | * various states of availability. | |
144 | */ | |
145 | static LIST_HEAD(dev_opp_list); | |
146 | /* Lock to allow exclusive modification to the device and opp lists */ | |
147 | static DEFINE_MUTEX(dev_opp_list_lock); | |
148 | ||
b02ded24 DT |
149 | #define opp_rcu_lockdep_assert() \ |
150 | do { \ | |
151 | rcu_lockdep_assert(rcu_read_lock_held() || \ | |
152 | lockdep_is_held(&dev_opp_list_lock), \ | |
153 | "Missing rcu_read_lock() or " \ | |
154 | "dev_opp_list_lock protection"); \ | |
155 | } while (0) | |
156 | ||
06441658 VK |
157 | static struct device_list_opp *_find_list_dev(const struct device *dev, |
158 | struct device_opp *dev_opp) | |
159 | { | |
160 | struct device_list_opp *list_dev; | |
161 | ||
162 | list_for_each_entry(list_dev, &dev_opp->dev_list, node) | |
163 | if (list_dev->dev == dev) | |
164 | return list_dev; | |
165 | ||
166 | return NULL; | |
167 | } | |
168 | ||
169 | static struct device_opp *_managed_opp(const struct device_node *np) | |
170 | { | |
171 | struct device_opp *dev_opp; | |
172 | ||
173 | list_for_each_entry_rcu(dev_opp, &dev_opp_list, node) { | |
174 | if (dev_opp->np == np) { | |
175 | /* | |
176 | * Multiple devices can point to the same OPP table and | |
177 | * so will have same node-pointer, np. | |
178 | * | |
179 | * But the OPPs will be considered as shared only if the | |
180 | * OPP table contains a "opp-shared" property. | |
181 | */ | |
182 | return dev_opp->shared_opp ? dev_opp : NULL; | |
183 | } | |
184 | } | |
185 | ||
186 | return NULL; | |
187 | } | |
188 | ||
e1f60b29 | 189 | /** |
327854c8 | 190 | * _find_device_opp() - find device_opp struct using device pointer |
e1f60b29 NM |
191 | * @dev: device pointer used to lookup device OPPs |
192 | * | |
193 | * Search list of device OPPs for one containing matching device. Does a RCU | |
194 | * reader operation to grab the pointer needed. | |
195 | * | |
984f16c8 | 196 | * Return: pointer to 'struct device_opp' if found, otherwise -ENODEV or |
e1f60b29 NM |
197 | * -EINVAL based on type of error. |
198 | * | |
199 | * Locking: This function must be called under rcu_read_lock(). device_opp | |
200 | * is a RCU protected pointer. This means that device_opp is valid as long | |
201 | * as we are under RCU lock. | |
202 | */ | |
327854c8 | 203 | static struct device_opp *_find_device_opp(struct device *dev) |
e1f60b29 | 204 | { |
06441658 | 205 | struct device_opp *dev_opp; |
e1f60b29 NM |
206 | |
207 | if (unlikely(IS_ERR_OR_NULL(dev))) { | |
208 | pr_err("%s: Invalid parameters\n", __func__); | |
209 | return ERR_PTR(-EINVAL); | |
210 | } | |
211 | ||
06441658 VK |
212 | list_for_each_entry_rcu(dev_opp, &dev_opp_list, node) |
213 | if (_find_list_dev(dev, dev_opp)) | |
214 | return dev_opp; | |
e1f60b29 | 215 | |
06441658 | 216 | return ERR_PTR(-ENODEV); |
e1f60b29 NM |
217 | } |
218 | ||
219 | /** | |
5d4879cd | 220 | * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an available opp |
e1f60b29 NM |
221 | * @opp: opp for which voltage has to be returned for |
222 | * | |
984f16c8 | 223 | * Return: voltage in micro volt corresponding to the opp, else |
e1f60b29 NM |
224 | * return 0 |
225 | * | |
226 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu | |
227 | * protected pointer. This means that opp which could have been fetched by | |
228 | * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are | |
229 | * under RCU lock. The pointer returned by the opp_find_freq family must be | |
230 | * used in the same section as the usage of this function with the pointer | |
231 | * prior to unlocking with rcu_read_unlock() to maintain the integrity of the | |
232 | * pointer. | |
233 | */ | |
47d43ba7 | 234 | unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp) |
e1f60b29 | 235 | { |
47d43ba7 | 236 | struct dev_pm_opp *tmp_opp; |
e1f60b29 NM |
237 | unsigned long v = 0; |
238 | ||
04bf1c7f KK |
239 | opp_rcu_lockdep_assert(); |
240 | ||
e1f60b29 NM |
241 | tmp_opp = rcu_dereference(opp); |
242 | if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available) | |
243 | pr_err("%s: Invalid parameters\n", __func__); | |
244 | else | |
245 | v = tmp_opp->u_volt; | |
246 | ||
247 | return v; | |
248 | } | |
5d4879cd | 249 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage); |
e1f60b29 NM |
250 | |
251 | /** | |
5d4879cd | 252 | * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp |
e1f60b29 NM |
253 | * @opp: opp for which frequency has to be returned for |
254 | * | |
984f16c8 | 255 | * Return: frequency in hertz corresponding to the opp, else |
e1f60b29 NM |
256 | * return 0 |
257 | * | |
258 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu | |
259 | * protected pointer. This means that opp which could have been fetched by | |
260 | * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are | |
261 | * under RCU lock. The pointer returned by the opp_find_freq family must be | |
262 | * used in the same section as the usage of this function with the pointer | |
263 | * prior to unlocking with rcu_read_unlock() to maintain the integrity of the | |
264 | * pointer. | |
265 | */ | |
47d43ba7 | 266 | unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp) |
e1f60b29 | 267 | { |
47d43ba7 | 268 | struct dev_pm_opp *tmp_opp; |
e1f60b29 NM |
269 | unsigned long f = 0; |
270 | ||
04bf1c7f KK |
271 | opp_rcu_lockdep_assert(); |
272 | ||
e1f60b29 NM |
273 | tmp_opp = rcu_dereference(opp); |
274 | if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available) | |
275 | pr_err("%s: Invalid parameters\n", __func__); | |
276 | else | |
277 | f = tmp_opp->rate; | |
278 | ||
279 | return f; | |
280 | } | |
5d4879cd | 281 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq); |
e1f60b29 | 282 | |
3ca9bb33 VK |
283 | /** |
284 | * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds | |
285 | * @dev: device for which we do this operation | |
286 | * | |
287 | * Return: This function returns the max clock latency in nanoseconds. | |
288 | * | |
289 | * Locking: This function takes rcu_read_lock(). | |
290 | */ | |
291 | unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev) | |
292 | { | |
293 | struct device_opp *dev_opp; | |
294 | unsigned long clock_latency_ns; | |
295 | ||
296 | rcu_read_lock(); | |
297 | ||
298 | dev_opp = _find_device_opp(dev); | |
299 | if (IS_ERR(dev_opp)) | |
300 | clock_latency_ns = 0; | |
301 | else | |
302 | clock_latency_ns = dev_opp->clock_latency_ns_max; | |
303 | ||
304 | rcu_read_unlock(); | |
305 | return clock_latency_ns; | |
306 | } | |
307 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency); | |
308 | ||
e1f60b29 | 309 | /** |
5d4879cd | 310 | * dev_pm_opp_get_opp_count() - Get number of opps available in the opp list |
e1f60b29 NM |
311 | * @dev: device for which we do this operation |
312 | * | |
984f16c8 | 313 | * Return: This function returns the number of available opps if there are any, |
e1f60b29 NM |
314 | * else returns 0 if none or the corresponding error value. |
315 | * | |
b4718c02 | 316 | * Locking: This function takes rcu_read_lock(). |
e1f60b29 | 317 | */ |
5d4879cd | 318 | int dev_pm_opp_get_opp_count(struct device *dev) |
e1f60b29 NM |
319 | { |
320 | struct device_opp *dev_opp; | |
47d43ba7 | 321 | struct dev_pm_opp *temp_opp; |
e1f60b29 NM |
322 | int count = 0; |
323 | ||
b4718c02 | 324 | rcu_read_lock(); |
b02ded24 | 325 | |
327854c8 | 326 | dev_opp = _find_device_opp(dev); |
e1f60b29 | 327 | if (IS_ERR(dev_opp)) { |
b4718c02 DT |
328 | count = PTR_ERR(dev_opp); |
329 | dev_err(dev, "%s: device OPP not found (%d)\n", | |
330 | __func__, count); | |
331 | goto out_unlock; | |
e1f60b29 NM |
332 | } |
333 | ||
334 | list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) { | |
335 | if (temp_opp->available) | |
336 | count++; | |
337 | } | |
338 | ||
b4718c02 DT |
339 | out_unlock: |
340 | rcu_read_unlock(); | |
e1f60b29 NM |
341 | return count; |
342 | } | |
5d4879cd | 343 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count); |
e1f60b29 NM |
344 | |
345 | /** | |
5d4879cd | 346 | * dev_pm_opp_find_freq_exact() - search for an exact frequency |
e1f60b29 NM |
347 | * @dev: device for which we do this operation |
348 | * @freq: frequency to search for | |
7ae49618 | 349 | * @available: true/false - match for available opp |
e1f60b29 | 350 | * |
984f16c8 NM |
351 | * Return: Searches for exact match in the opp list and returns pointer to the |
352 | * matching opp if found, else returns ERR_PTR in case of error and should | |
353 | * be handled using IS_ERR. Error return values can be: | |
0779726c NM |
354 | * EINVAL: for bad pointer |
355 | * ERANGE: no match found for search | |
356 | * ENODEV: if device not found in list of registered devices | |
e1f60b29 NM |
357 | * |
358 | * Note: available is a modifier for the search. if available=true, then the | |
359 | * match is for exact matching frequency and is available in the stored OPP | |
360 | * table. if false, the match is for exact frequency which is not available. | |
361 | * | |
362 | * This provides a mechanism to enable an opp which is not available currently | |
363 | * or the opposite as well. | |
364 | * | |
365 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu | |
366 | * protected pointer. The reason for the same is that the opp pointer which is | |
367 | * returned will remain valid for use with opp_get_{voltage, freq} only while | |
368 | * under the locked area. The pointer returned must be used prior to unlocking | |
369 | * with rcu_read_unlock() to maintain the integrity of the pointer. | |
370 | */ | |
47d43ba7 NM |
371 | struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev, |
372 | unsigned long freq, | |
373 | bool available) | |
e1f60b29 NM |
374 | { |
375 | struct device_opp *dev_opp; | |
47d43ba7 | 376 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); |
e1f60b29 | 377 | |
b02ded24 DT |
378 | opp_rcu_lockdep_assert(); |
379 | ||
327854c8 | 380 | dev_opp = _find_device_opp(dev); |
e1f60b29 NM |
381 | if (IS_ERR(dev_opp)) { |
382 | int r = PTR_ERR(dev_opp); | |
383 | dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r); | |
384 | return ERR_PTR(r); | |
385 | } | |
386 | ||
387 | list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) { | |
388 | if (temp_opp->available == available && | |
389 | temp_opp->rate == freq) { | |
390 | opp = temp_opp; | |
391 | break; | |
392 | } | |
393 | } | |
394 | ||
395 | return opp; | |
396 | } | |
5d4879cd | 397 | EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact); |
e1f60b29 NM |
398 | |
399 | /** | |
5d4879cd | 400 | * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq |
e1f60b29 NM |
401 | * @dev: device for which we do this operation |
402 | * @freq: Start frequency | |
403 | * | |
404 | * Search for the matching ceil *available* OPP from a starting freq | |
405 | * for a device. | |
406 | * | |
984f16c8 | 407 | * Return: matching *opp and refreshes *freq accordingly, else returns |
0779726c NM |
408 | * ERR_PTR in case of error and should be handled using IS_ERR. Error return |
409 | * values can be: | |
410 | * EINVAL: for bad pointer | |
411 | * ERANGE: no match found for search | |
412 | * ENODEV: if device not found in list of registered devices | |
e1f60b29 NM |
413 | * |
414 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu | |
415 | * protected pointer. The reason for the same is that the opp pointer which is | |
416 | * returned will remain valid for use with opp_get_{voltage, freq} only while | |
417 | * under the locked area. The pointer returned must be used prior to unlocking | |
418 | * with rcu_read_unlock() to maintain the integrity of the pointer. | |
419 | */ | |
47d43ba7 NM |
420 | struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev, |
421 | unsigned long *freq) | |
e1f60b29 NM |
422 | { |
423 | struct device_opp *dev_opp; | |
47d43ba7 | 424 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); |
e1f60b29 | 425 | |
b02ded24 DT |
426 | opp_rcu_lockdep_assert(); |
427 | ||
e1f60b29 NM |
428 | if (!dev || !freq) { |
429 | dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); | |
430 | return ERR_PTR(-EINVAL); | |
431 | } | |
432 | ||
327854c8 | 433 | dev_opp = _find_device_opp(dev); |
e1f60b29 | 434 | if (IS_ERR(dev_opp)) |
0779726c | 435 | return ERR_CAST(dev_opp); |
e1f60b29 NM |
436 | |
437 | list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) { | |
438 | if (temp_opp->available && temp_opp->rate >= *freq) { | |
439 | opp = temp_opp; | |
440 | *freq = opp->rate; | |
441 | break; | |
442 | } | |
443 | } | |
444 | ||
445 | return opp; | |
446 | } | |
5d4879cd | 447 | EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil); |
e1f60b29 NM |
448 | |
449 | /** | |
5d4879cd | 450 | * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq |
e1f60b29 NM |
451 | * @dev: device for which we do this operation |
452 | * @freq: Start frequency | |
453 | * | |
454 | * Search for the matching floor *available* OPP from a starting freq | |
455 | * for a device. | |
456 | * | |
984f16c8 | 457 | * Return: matching *opp and refreshes *freq accordingly, else returns |
0779726c NM |
458 | * ERR_PTR in case of error and should be handled using IS_ERR. Error return |
459 | * values can be: | |
460 | * EINVAL: for bad pointer | |
461 | * ERANGE: no match found for search | |
462 | * ENODEV: if device not found in list of registered devices | |
e1f60b29 NM |
463 | * |
464 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu | |
465 | * protected pointer. The reason for the same is that the opp pointer which is | |
466 | * returned will remain valid for use with opp_get_{voltage, freq} only while | |
467 | * under the locked area. The pointer returned must be used prior to unlocking | |
468 | * with rcu_read_unlock() to maintain the integrity of the pointer. | |
469 | */ | |
47d43ba7 NM |
470 | struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev, |
471 | unsigned long *freq) | |
e1f60b29 NM |
472 | { |
473 | struct device_opp *dev_opp; | |
47d43ba7 | 474 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); |
e1f60b29 | 475 | |
b02ded24 DT |
476 | opp_rcu_lockdep_assert(); |
477 | ||
e1f60b29 NM |
478 | if (!dev || !freq) { |
479 | dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); | |
480 | return ERR_PTR(-EINVAL); | |
481 | } | |
482 | ||
327854c8 | 483 | dev_opp = _find_device_opp(dev); |
e1f60b29 | 484 | if (IS_ERR(dev_opp)) |
0779726c | 485 | return ERR_CAST(dev_opp); |
e1f60b29 NM |
486 | |
487 | list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) { | |
488 | if (temp_opp->available) { | |
489 | /* go to the next node, before choosing prev */ | |
490 | if (temp_opp->rate > *freq) | |
491 | break; | |
492 | else | |
493 | opp = temp_opp; | |
494 | } | |
495 | } | |
496 | if (!IS_ERR(opp)) | |
497 | *freq = opp->rate; | |
498 | ||
499 | return opp; | |
500 | } | |
5d4879cd | 501 | EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor); |
e1f60b29 | 502 | |
06441658 VK |
503 | /* List-dev Helpers */ |
504 | static void _kfree_list_dev_rcu(struct rcu_head *head) | |
505 | { | |
506 | struct device_list_opp *list_dev; | |
507 | ||
508 | list_dev = container_of(head, struct device_list_opp, rcu_head); | |
509 | kfree_rcu(list_dev, rcu_head); | |
510 | } | |
511 | ||
512 | static void _remove_list_dev(struct device_list_opp *list_dev, | |
513 | struct device_opp *dev_opp) | |
514 | { | |
515 | list_del(&list_dev->node); | |
516 | call_srcu(&dev_opp->srcu_head.srcu, &list_dev->rcu_head, | |
517 | _kfree_list_dev_rcu); | |
518 | } | |
519 | ||
520 | static struct device_list_opp *_add_list_dev(const struct device *dev, | |
521 | struct device_opp *dev_opp) | |
522 | { | |
523 | struct device_list_opp *list_dev; | |
524 | ||
525 | list_dev = kzalloc(sizeof(*list_dev), GFP_KERNEL); | |
526 | if (!list_dev) | |
527 | return NULL; | |
528 | ||
529 | /* Initialize list-dev */ | |
530 | list_dev->dev = dev; | |
531 | list_add_rcu(&list_dev->node, &dev_opp->dev_list); | |
532 | ||
533 | return list_dev; | |
534 | } | |
535 | ||
984f16c8 | 536 | /** |
aa5f2f85 | 537 | * _add_device_opp() - Find device OPP table or allocate a new one |
984f16c8 NM |
538 | * @dev: device for which we do this operation |
539 | * | |
aa5f2f85 VK |
540 | * It tries to find an existing table first, if it couldn't find one, it |
541 | * allocates a new OPP table and returns that. | |
984f16c8 NM |
542 | * |
543 | * Return: valid device_opp pointer if success, else NULL. | |
544 | */ | |
327854c8 | 545 | static struct device_opp *_add_device_opp(struct device *dev) |
07cce74a VK |
546 | { |
547 | struct device_opp *dev_opp; | |
06441658 | 548 | struct device_list_opp *list_dev; |
07cce74a | 549 | |
aa5f2f85 VK |
550 | /* Check for existing list for 'dev' first */ |
551 | dev_opp = _find_device_opp(dev); | |
552 | if (!IS_ERR(dev_opp)) | |
553 | return dev_opp; | |
554 | ||
07cce74a VK |
555 | /* |
556 | * Allocate a new device OPP table. In the infrequent case where a new | |
557 | * device is needed to be added, we pay this penalty. | |
558 | */ | |
559 | dev_opp = kzalloc(sizeof(*dev_opp), GFP_KERNEL); | |
560 | if (!dev_opp) | |
561 | return NULL; | |
562 | ||
06441658 VK |
563 | INIT_LIST_HEAD(&dev_opp->dev_list); |
564 | ||
565 | list_dev = _add_list_dev(dev, dev_opp); | |
566 | if (!list_dev) { | |
567 | kfree(dev_opp); | |
568 | return NULL; | |
569 | } | |
570 | ||
07cce74a VK |
571 | srcu_init_notifier_head(&dev_opp->srcu_head); |
572 | INIT_LIST_HEAD(&dev_opp->opp_list); | |
573 | ||
574 | /* Secure the device list modification */ | |
575 | list_add_rcu(&dev_opp->node, &dev_opp_list); | |
576 | return dev_opp; | |
577 | } | |
578 | ||
737002b5 VK |
579 | /** |
580 | * _kfree_device_rcu() - Free device_opp RCU handler | |
581 | * @head: RCU head | |
582 | */ | |
583 | static void _kfree_device_rcu(struct rcu_head *head) | |
584 | { | |
585 | struct device_opp *device_opp = container_of(head, struct device_opp, rcu_head); | |
586 | ||
587 | kfree_rcu(device_opp, rcu_head); | |
588 | } | |
589 | ||
3bac42ca VK |
590 | /** |
591 | * _remove_device_opp() - Removes a device OPP table | |
592 | * @dev_opp: device OPP table to be removed. | |
593 | * | |
594 | * Removes/frees device OPP table it it doesn't contain any OPPs. | |
595 | */ | |
596 | static void _remove_device_opp(struct device_opp *dev_opp) | |
597 | { | |
06441658 VK |
598 | struct device_list_opp *list_dev; |
599 | ||
3bac42ca VK |
600 | if (!list_empty(&dev_opp->opp_list)) |
601 | return; | |
602 | ||
06441658 VK |
603 | list_dev = list_first_entry(&dev_opp->dev_list, struct device_list_opp, |
604 | node); | |
605 | ||
606 | _remove_list_dev(list_dev, dev_opp); | |
607 | ||
608 | /* dev_list must be empty now */ | |
609 | WARN_ON(!list_empty(&dev_opp->dev_list)); | |
610 | ||
3bac42ca VK |
611 | list_del_rcu(&dev_opp->node); |
612 | call_srcu(&dev_opp->srcu_head.srcu, &dev_opp->rcu_head, | |
613 | _kfree_device_rcu); | |
614 | } | |
615 | ||
737002b5 VK |
616 | /** |
617 | * _kfree_opp_rcu() - Free OPP RCU handler | |
618 | * @head: RCU head | |
619 | */ | |
620 | static void _kfree_opp_rcu(struct rcu_head *head) | |
621 | { | |
622 | struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head); | |
623 | ||
624 | kfree_rcu(opp, rcu_head); | |
625 | } | |
626 | ||
627 | /** | |
628 | * _opp_remove() - Remove an OPP from a table definition | |
629 | * @dev_opp: points back to the device_opp struct this opp belongs to | |
630 | * @opp: pointer to the OPP to remove | |
23dacf6d | 631 | * @notify: OPP_EVENT_REMOVE notification should be sent or not |
737002b5 VK |
632 | * |
633 | * This function removes an opp definition from the opp list. | |
634 | * | |
635 | * Locking: The internal device_opp and opp structures are RCU protected. | |
636 | * It is assumed that the caller holds required mutex for an RCU updater | |
637 | * strategy. | |
638 | */ | |
639 | static void _opp_remove(struct device_opp *dev_opp, | |
23dacf6d | 640 | struct dev_pm_opp *opp, bool notify) |
737002b5 VK |
641 | { |
642 | /* | |
643 | * Notify the changes in the availability of the operable | |
644 | * frequency/voltage list. | |
645 | */ | |
23dacf6d VK |
646 | if (notify) |
647 | srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_REMOVE, opp); | |
737002b5 VK |
648 | list_del_rcu(&opp->node); |
649 | call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu); | |
650 | ||
3bac42ca | 651 | _remove_device_opp(dev_opp); |
737002b5 VK |
652 | } |
653 | ||
654 | /** | |
655 | * dev_pm_opp_remove() - Remove an OPP from OPP list | |
656 | * @dev: device for which we do this operation | |
657 | * @freq: OPP to remove with matching 'freq' | |
658 | * | |
659 | * This function removes an opp from the opp list. | |
660 | * | |
661 | * Locking: The internal device_opp and opp structures are RCU protected. | |
662 | * Hence this function internally uses RCU updater strategy with mutex locks | |
663 | * to keep the integrity of the internal data structures. Callers should ensure | |
664 | * that this function is *NOT* called under RCU protection or in contexts where | |
665 | * mutex cannot be locked. | |
666 | */ | |
667 | void dev_pm_opp_remove(struct device *dev, unsigned long freq) | |
668 | { | |
669 | struct dev_pm_opp *opp; | |
670 | struct device_opp *dev_opp; | |
671 | bool found = false; | |
672 | ||
673 | /* Hold our list modification lock here */ | |
674 | mutex_lock(&dev_opp_list_lock); | |
675 | ||
676 | dev_opp = _find_device_opp(dev); | |
677 | if (IS_ERR(dev_opp)) | |
678 | goto unlock; | |
679 | ||
680 | list_for_each_entry(opp, &dev_opp->opp_list, node) { | |
681 | if (opp->rate == freq) { | |
682 | found = true; | |
683 | break; | |
684 | } | |
685 | } | |
686 | ||
687 | if (!found) { | |
688 | dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n", | |
689 | __func__, freq); | |
690 | goto unlock; | |
691 | } | |
692 | ||
23dacf6d | 693 | _opp_remove(dev_opp, opp, true); |
737002b5 VK |
694 | unlock: |
695 | mutex_unlock(&dev_opp_list_lock); | |
696 | } | |
697 | EXPORT_SYMBOL_GPL(dev_pm_opp_remove); | |
698 | ||
23dacf6d VK |
699 | static struct dev_pm_opp *_allocate_opp(struct device *dev, |
700 | struct device_opp **dev_opp) | |
701 | { | |
702 | struct dev_pm_opp *opp; | |
703 | ||
704 | /* allocate new OPP node */ | |
705 | opp = kzalloc(sizeof(*opp), GFP_KERNEL); | |
706 | if (!opp) | |
707 | return NULL; | |
708 | ||
709 | INIT_LIST_HEAD(&opp->node); | |
710 | ||
711 | *dev_opp = _add_device_opp(dev); | |
712 | if (!*dev_opp) { | |
713 | kfree(opp); | |
714 | return NULL; | |
715 | } | |
716 | ||
717 | return opp; | |
718 | } | |
719 | ||
06441658 VK |
720 | static int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, |
721 | struct device_opp *dev_opp) | |
23dacf6d VK |
722 | { |
723 | struct dev_pm_opp *opp; | |
724 | struct list_head *head = &dev_opp->opp_list; | |
725 | ||
726 | /* | |
727 | * Insert new OPP in order of increasing frequency and discard if | |
728 | * already present. | |
729 | * | |
730 | * Need to use &dev_opp->opp_list in the condition part of the 'for' | |
731 | * loop, don't replace it with head otherwise it will become an infinite | |
732 | * loop. | |
733 | */ | |
734 | list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) { | |
735 | if (new_opp->rate > opp->rate) { | |
736 | head = &opp->node; | |
737 | continue; | |
738 | } | |
739 | ||
740 | if (new_opp->rate < opp->rate) | |
741 | break; | |
742 | ||
743 | /* Duplicate OPPs */ | |
06441658 | 744 | dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n", |
23dacf6d VK |
745 | __func__, opp->rate, opp->u_volt, opp->available, |
746 | new_opp->rate, new_opp->u_volt, new_opp->available); | |
747 | ||
748 | return opp->available && new_opp->u_volt == opp->u_volt ? | |
749 | 0 : -EEXIST; | |
750 | } | |
751 | ||
752 | new_opp->dev_opp = dev_opp; | |
753 | list_add_rcu(&new_opp->node, head); | |
754 | ||
755 | return 0; | |
756 | } | |
757 | ||
984f16c8 NM |
758 | /** |
759 | * _opp_add_dynamic() - Allocate a dynamic OPP. | |
760 | * @dev: device for which we do this operation | |
761 | * @freq: Frequency in Hz for this OPP | |
762 | * @u_volt: Voltage in uVolts for this OPP | |
763 | * @dynamic: Dynamically added OPPs. | |
764 | * | |
765 | * This function adds an opp definition to the opp list and returns status. | |
766 | * The opp is made available by default and it can be controlled using | |
767 | * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove. | |
768 | * | |
769 | * NOTE: "dynamic" parameter impacts OPPs added by the of_init_opp_table and | |
770 | * freed by of_free_opp_table. | |
771 | * | |
772 | * Locking: The internal device_opp and opp structures are RCU protected. | |
773 | * Hence this function internally uses RCU updater strategy with mutex locks | |
774 | * to keep the integrity of the internal data structures. Callers should ensure | |
775 | * that this function is *NOT* called under RCU protection or in contexts where | |
776 | * mutex cannot be locked. | |
777 | * | |
778 | * Return: | |
779 | * 0 On success OR | |
780 | * Duplicate OPPs (both freq and volt are same) and opp->available | |
781 | * -EEXIST Freq are same and volt are different OR | |
782 | * Duplicate OPPs (both freq and volt are same) and !opp->available | |
783 | * -ENOMEM Memory allocation failure | |
784 | */ | |
327854c8 NM |
785 | static int _opp_add_dynamic(struct device *dev, unsigned long freq, |
786 | long u_volt, bool dynamic) | |
e1f60b29 | 787 | { |
aa5f2f85 | 788 | struct device_opp *dev_opp; |
23dacf6d | 789 | struct dev_pm_opp *new_opp; |
6ce4184d | 790 | int ret; |
e1f60b29 | 791 | |
e1f60b29 NM |
792 | /* Hold our list modification lock here */ |
793 | mutex_lock(&dev_opp_list_lock); | |
794 | ||
23dacf6d VK |
795 | new_opp = _allocate_opp(dev, &dev_opp); |
796 | if (!new_opp) { | |
797 | ret = -ENOMEM; | |
798 | goto unlock; | |
799 | } | |
800 | ||
a7470db6 | 801 | /* populate the opp table */ |
a7470db6 VK |
802 | new_opp->rate = freq; |
803 | new_opp->u_volt = u_volt; | |
804 | new_opp->available = true; | |
23dacf6d | 805 | new_opp->dynamic = dynamic; |
a7470db6 | 806 | |
06441658 | 807 | ret = _opp_add(dev, new_opp, dev_opp); |
23dacf6d | 808 | if (ret) |
6ce4184d | 809 | goto free_opp; |
64ce8545 | 810 | |
e1f60b29 NM |
811 | mutex_unlock(&dev_opp_list_lock); |
812 | ||
03ca370f MH |
813 | /* |
814 | * Notify the changes in the availability of the operable | |
815 | * frequency/voltage list. | |
816 | */ | |
cd1a068a | 817 | srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp); |
e1f60b29 | 818 | return 0; |
6ce4184d VK |
819 | |
820 | free_opp: | |
23dacf6d VK |
821 | _opp_remove(dev_opp, new_opp, false); |
822 | unlock: | |
6ce4184d | 823 | mutex_unlock(&dev_opp_list_lock); |
6ce4184d | 824 | return ret; |
e1f60b29 | 825 | } |
38393409 | 826 | |
27465902 VK |
827 | /* TODO: Support multiple regulators */ |
828 | static int opp_get_microvolt(struct dev_pm_opp *opp, struct device *dev) | |
829 | { | |
830 | u32 microvolt[3] = {0}; | |
831 | int count, ret; | |
832 | ||
833 | count = of_property_count_u32_elems(opp->np, "opp-microvolt"); | |
834 | if (!count) | |
835 | return 0; | |
836 | ||
837 | /* There can be one or three elements here */ | |
838 | if (count != 1 && count != 3) { | |
839 | dev_err(dev, "%s: Invalid number of elements in opp-microvolt property (%d)\n", | |
840 | __func__, count); | |
841 | return -EINVAL; | |
842 | } | |
843 | ||
844 | ret = of_property_read_u32_array(opp->np, "opp-microvolt", microvolt, | |
845 | count); | |
846 | if (ret) { | |
847 | dev_err(dev, "%s: error parsing opp-microvolt: %d\n", __func__, | |
848 | ret); | |
849 | return -EINVAL; | |
850 | } | |
851 | ||
852 | opp->u_volt = microvolt[0]; | |
853 | opp->u_volt_min = microvolt[1]; | |
854 | opp->u_volt_max = microvolt[2]; | |
855 | ||
856 | return 0; | |
857 | } | |
858 | ||
859 | /** | |
860 | * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings) | |
861 | * @dev: device for which we do this operation | |
862 | * @np: device node | |
863 | * | |
864 | * This function adds an opp definition to the opp list and returns status. The | |
865 | * opp can be controlled using dev_pm_opp_enable/disable functions and may be | |
866 | * removed by dev_pm_opp_remove. | |
867 | * | |
868 | * Locking: The internal device_opp and opp structures are RCU protected. | |
869 | * Hence this function internally uses RCU updater strategy with mutex locks | |
870 | * to keep the integrity of the internal data structures. Callers should ensure | |
871 | * that this function is *NOT* called under RCU protection or in contexts where | |
872 | * mutex cannot be locked. | |
873 | * | |
874 | * Return: | |
875 | * 0 On success OR | |
876 | * Duplicate OPPs (both freq and volt are same) and opp->available | |
877 | * -EEXIST Freq are same and volt are different OR | |
878 | * Duplicate OPPs (both freq and volt are same) and !opp->available | |
879 | * -ENOMEM Memory allocation failure | |
880 | * -EINVAL Failed parsing the OPP node | |
881 | */ | |
882 | static int _opp_add_static_v2(struct device *dev, struct device_node *np) | |
883 | { | |
884 | struct device_opp *dev_opp; | |
885 | struct dev_pm_opp *new_opp; | |
886 | u64 rate; | |
887 | int ret; | |
888 | ||
889 | /* Hold our list modification lock here */ | |
890 | mutex_lock(&dev_opp_list_lock); | |
891 | ||
892 | new_opp = _allocate_opp(dev, &dev_opp); | |
893 | if (!new_opp) { | |
894 | ret = -ENOMEM; | |
895 | goto unlock; | |
896 | } | |
897 | ||
898 | ret = of_property_read_u64(np, "opp-hz", &rate); | |
899 | if (ret < 0) { | |
900 | dev_err(dev, "%s: opp-hz not found\n", __func__); | |
901 | goto free_opp; | |
902 | } | |
903 | ||
904 | /* | |
905 | * Rate is defined as an unsigned long in clk API, and so casting | |
906 | * explicitly to its type. Must be fixed once rate is 64 bit | |
907 | * guaranteed in clk API. | |
908 | */ | |
909 | new_opp->rate = (unsigned long)rate; | |
910 | new_opp->turbo = of_property_read_bool(np, "turbo-mode"); | |
911 | ||
912 | new_opp->np = np; | |
913 | new_opp->dynamic = false; | |
914 | new_opp->available = true; | |
3ca9bb33 VK |
915 | of_property_read_u32(np, "clock-latency-ns", |
916 | (u32 *)&new_opp->clock_latency_ns); | |
27465902 VK |
917 | |
918 | ret = opp_get_microvolt(new_opp, dev); | |
919 | if (ret) | |
920 | goto free_opp; | |
921 | ||
922 | of_property_read_u32(np, "opp-microamp", (u32 *)&new_opp->u_amp); | |
923 | ||
06441658 | 924 | ret = _opp_add(dev, new_opp, dev_opp); |
27465902 VK |
925 | if (ret) |
926 | goto free_opp; | |
927 | ||
ad656a6a VK |
928 | /* OPP to select on device suspend */ |
929 | if (of_property_read_bool(np, "opp-suspend")) { | |
930 | if (dev_opp->suspend_opp) | |
931 | dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n", | |
932 | __func__, dev_opp->suspend_opp->rate, | |
933 | new_opp->rate); | |
934 | else | |
935 | dev_opp->suspend_opp = new_opp; | |
936 | } | |
937 | ||
3ca9bb33 VK |
938 | if (new_opp->clock_latency_ns > dev_opp->clock_latency_ns_max) |
939 | dev_opp->clock_latency_ns_max = new_opp->clock_latency_ns; | |
940 | ||
27465902 VK |
941 | mutex_unlock(&dev_opp_list_lock); |
942 | ||
3ca9bb33 | 943 | pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n", |
27465902 | 944 | __func__, new_opp->turbo, new_opp->rate, new_opp->u_volt, |
3ca9bb33 VK |
945 | new_opp->u_volt_min, new_opp->u_volt_max, |
946 | new_opp->clock_latency_ns); | |
27465902 VK |
947 | |
948 | /* | |
949 | * Notify the changes in the availability of the operable | |
950 | * frequency/voltage list. | |
951 | */ | |
952 | srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp); | |
953 | return 0; | |
954 | ||
955 | free_opp: | |
956 | _opp_remove(dev_opp, new_opp, false); | |
957 | unlock: | |
958 | mutex_unlock(&dev_opp_list_lock); | |
959 | return ret; | |
960 | } | |
961 | ||
38393409 VK |
962 | /** |
963 | * dev_pm_opp_add() - Add an OPP table from a table definitions | |
964 | * @dev: device for which we do this operation | |
965 | * @freq: Frequency in Hz for this OPP | |
966 | * @u_volt: Voltage in uVolts for this OPP | |
967 | * | |
968 | * This function adds an opp definition to the opp list and returns status. | |
969 | * The opp is made available by default and it can be controlled using | |
970 | * dev_pm_opp_enable/disable functions. | |
971 | * | |
972 | * Locking: The internal device_opp and opp structures are RCU protected. | |
973 | * Hence this function internally uses RCU updater strategy with mutex locks | |
974 | * to keep the integrity of the internal data structures. Callers should ensure | |
975 | * that this function is *NOT* called under RCU protection or in contexts where | |
976 | * mutex cannot be locked. | |
977 | * | |
978 | * Return: | |
984f16c8 | 979 | * 0 On success OR |
38393409 | 980 | * Duplicate OPPs (both freq and volt are same) and opp->available |
984f16c8 | 981 | * -EEXIST Freq are same and volt are different OR |
38393409 | 982 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
984f16c8 | 983 | * -ENOMEM Memory allocation failure |
38393409 VK |
984 | */ |
985 | int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt) | |
986 | { | |
327854c8 | 987 | return _opp_add_dynamic(dev, freq, u_volt, true); |
38393409 | 988 | } |
5d4879cd | 989 | EXPORT_SYMBOL_GPL(dev_pm_opp_add); |
e1f60b29 NM |
990 | |
991 | /** | |
327854c8 | 992 | * _opp_set_availability() - helper to set the availability of an opp |
e1f60b29 NM |
993 | * @dev: device for which we do this operation |
994 | * @freq: OPP frequency to modify availability | |
995 | * @availability_req: availability status requested for this opp | |
996 | * | |
997 | * Set the availability of an OPP with an RCU operation, opp_{enable,disable} | |
998 | * share a common logic which is isolated here. | |
999 | * | |
984f16c8 | 1000 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the |
e1f60b29 NM |
1001 | * copy operation, returns 0 if no modifcation was done OR modification was |
1002 | * successful. | |
1003 | * | |
1004 | * Locking: The internal device_opp and opp structures are RCU protected. | |
1005 | * Hence this function internally uses RCU updater strategy with mutex locks to | |
1006 | * keep the integrity of the internal data structures. Callers should ensure | |
1007 | * that this function is *NOT* called under RCU protection or in contexts where | |
1008 | * mutex locking or synchronize_rcu() blocking calls cannot be used. | |
1009 | */ | |
327854c8 NM |
1010 | static int _opp_set_availability(struct device *dev, unsigned long freq, |
1011 | bool availability_req) | |
e1f60b29 | 1012 | { |
29df0ee1 | 1013 | struct device_opp *dev_opp; |
47d43ba7 | 1014 | struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV); |
e1f60b29 NM |
1015 | int r = 0; |
1016 | ||
1017 | /* keep the node allocated */ | |
47d43ba7 | 1018 | new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL); |
59d84ca8 | 1019 | if (!new_opp) |
e1f60b29 | 1020 | return -ENOMEM; |
e1f60b29 NM |
1021 | |
1022 | mutex_lock(&dev_opp_list_lock); | |
1023 | ||
1024 | /* Find the device_opp */ | |
327854c8 | 1025 | dev_opp = _find_device_opp(dev); |
e1f60b29 NM |
1026 | if (IS_ERR(dev_opp)) { |
1027 | r = PTR_ERR(dev_opp); | |
1028 | dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); | |
1029 | goto unlock; | |
1030 | } | |
1031 | ||
1032 | /* Do we have the frequency? */ | |
1033 | list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) { | |
1034 | if (tmp_opp->rate == freq) { | |
1035 | opp = tmp_opp; | |
1036 | break; | |
1037 | } | |
1038 | } | |
1039 | if (IS_ERR(opp)) { | |
1040 | r = PTR_ERR(opp); | |
1041 | goto unlock; | |
1042 | } | |
1043 | ||
1044 | /* Is update really needed? */ | |
1045 | if (opp->available == availability_req) | |
1046 | goto unlock; | |
1047 | /* copy the old data over */ | |
1048 | *new_opp = *opp; | |
1049 | ||
1050 | /* plug in new node */ | |
1051 | new_opp->available = availability_req; | |
1052 | ||
1053 | list_replace_rcu(&opp->node, &new_opp->node); | |
1054 | mutex_unlock(&dev_opp_list_lock); | |
327854c8 | 1055 | call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu); |
e1f60b29 | 1056 | |
03ca370f MH |
1057 | /* Notify the change of the OPP availability */ |
1058 | if (availability_req) | |
cd1a068a | 1059 | srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ENABLE, |
03ca370f MH |
1060 | new_opp); |
1061 | else | |
cd1a068a | 1062 | srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_DISABLE, |
03ca370f MH |
1063 | new_opp); |
1064 | ||
dde8437d | 1065 | return 0; |
e1f60b29 NM |
1066 | |
1067 | unlock: | |
1068 | mutex_unlock(&dev_opp_list_lock); | |
e1f60b29 NM |
1069 | kfree(new_opp); |
1070 | return r; | |
1071 | } | |
1072 | ||
1073 | /** | |
5d4879cd | 1074 | * dev_pm_opp_enable() - Enable a specific OPP |
e1f60b29 NM |
1075 | * @dev: device for which we do this operation |
1076 | * @freq: OPP frequency to enable | |
1077 | * | |
1078 | * Enables a provided opp. If the operation is valid, this returns 0, else the | |
1079 | * corresponding error value. It is meant to be used for users an OPP available | |
5d4879cd | 1080 | * after being temporarily made unavailable with dev_pm_opp_disable. |
e1f60b29 NM |
1081 | * |
1082 | * Locking: The internal device_opp and opp structures are RCU protected. | |
1083 | * Hence this function indirectly uses RCU and mutex locks to keep the | |
1084 | * integrity of the internal data structures. Callers should ensure that | |
1085 | * this function is *NOT* called under RCU protection or in contexts where | |
1086 | * mutex locking or synchronize_rcu() blocking calls cannot be used. | |
984f16c8 NM |
1087 | * |
1088 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the | |
1089 | * copy operation, returns 0 if no modifcation was done OR modification was | |
1090 | * successful. | |
e1f60b29 | 1091 | */ |
5d4879cd | 1092 | int dev_pm_opp_enable(struct device *dev, unsigned long freq) |
e1f60b29 | 1093 | { |
327854c8 | 1094 | return _opp_set_availability(dev, freq, true); |
e1f60b29 | 1095 | } |
5d4879cd | 1096 | EXPORT_SYMBOL_GPL(dev_pm_opp_enable); |
e1f60b29 NM |
1097 | |
1098 | /** | |
5d4879cd | 1099 | * dev_pm_opp_disable() - Disable a specific OPP |
e1f60b29 NM |
1100 | * @dev: device for which we do this operation |
1101 | * @freq: OPP frequency to disable | |
1102 | * | |
1103 | * Disables a provided opp. If the operation is valid, this returns | |
1104 | * 0, else the corresponding error value. It is meant to be a temporary | |
1105 | * control by users to make this OPP not available until the circumstances are | |
5d4879cd | 1106 | * right to make it available again (with a call to dev_pm_opp_enable). |
e1f60b29 NM |
1107 | * |
1108 | * Locking: The internal device_opp and opp structures are RCU protected. | |
1109 | * Hence this function indirectly uses RCU and mutex locks to keep the | |
1110 | * integrity of the internal data structures. Callers should ensure that | |
1111 | * this function is *NOT* called under RCU protection or in contexts where | |
1112 | * mutex locking or synchronize_rcu() blocking calls cannot be used. | |
984f16c8 NM |
1113 | * |
1114 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the | |
1115 | * copy operation, returns 0 if no modifcation was done OR modification was | |
1116 | * successful. | |
e1f60b29 | 1117 | */ |
5d4879cd | 1118 | int dev_pm_opp_disable(struct device *dev, unsigned long freq) |
e1f60b29 | 1119 | { |
327854c8 | 1120 | return _opp_set_availability(dev, freq, false); |
e1f60b29 | 1121 | } |
5d4879cd | 1122 | EXPORT_SYMBOL_GPL(dev_pm_opp_disable); |
e1f60b29 | 1123 | |
03ca370f | 1124 | /** |
5d4879cd | 1125 | * dev_pm_opp_get_notifier() - find notifier_head of the device with opp |
03ca370f | 1126 | * @dev: device pointer used to lookup device OPPs. |
984f16c8 NM |
1127 | * |
1128 | * Return: pointer to notifier head if found, otherwise -ENODEV or | |
1129 | * -EINVAL based on type of error casted as pointer. value must be checked | |
1130 | * with IS_ERR to determine valid pointer or error result. | |
1131 | * | |
1132 | * Locking: This function must be called under rcu_read_lock(). dev_opp is a RCU | |
1133 | * protected pointer. The reason for the same is that the opp pointer which is | |
1134 | * returned will remain valid for use with opp_get_{voltage, freq} only while | |
1135 | * under the locked area. The pointer returned must be used prior to unlocking | |
1136 | * with rcu_read_unlock() to maintain the integrity of the pointer. | |
03ca370f | 1137 | */ |
5d4879cd | 1138 | struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev) |
03ca370f | 1139 | { |
327854c8 | 1140 | struct device_opp *dev_opp = _find_device_opp(dev); |
03ca370f MH |
1141 | |
1142 | if (IS_ERR(dev_opp)) | |
156acb16 | 1143 | return ERR_CAST(dev_opp); /* matching type */ |
03ca370f | 1144 | |
cd1a068a | 1145 | return &dev_opp->srcu_head; |
03ca370f | 1146 | } |
4679ec37 | 1147 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier); |
b496dfbc SG |
1148 | |
1149 | #ifdef CONFIG_OF | |
737002b5 VK |
1150 | /** |
1151 | * of_free_opp_table() - Free OPP table entries created from static DT entries | |
1152 | * @dev: device pointer used to lookup device OPPs. | |
1153 | * | |
1154 | * Free OPPs created using static entries present in DT. | |
1155 | * | |
1156 | * Locking: The internal device_opp and opp structures are RCU protected. | |
1157 | * Hence this function indirectly uses RCU updater strategy with mutex locks | |
1158 | * to keep the integrity of the internal data structures. Callers should ensure | |
1159 | * that this function is *NOT* called under RCU protection or in contexts where | |
1160 | * mutex cannot be locked. | |
1161 | */ | |
1162 | void of_free_opp_table(struct device *dev) | |
1163 | { | |
1164 | struct device_opp *dev_opp; | |
1165 | struct dev_pm_opp *opp, *tmp; | |
1166 | ||
06441658 VK |
1167 | /* Hold our list modification lock here */ |
1168 | mutex_lock(&dev_opp_list_lock); | |
1169 | ||
737002b5 VK |
1170 | /* Check for existing list for 'dev' */ |
1171 | dev_opp = _find_device_opp(dev); | |
1172 | if (IS_ERR(dev_opp)) { | |
1173 | int error = PTR_ERR(dev_opp); | |
1174 | ||
1175 | if (error != -ENODEV) | |
1176 | WARN(1, "%s: dev_opp: %d\n", | |
1177 | IS_ERR_OR_NULL(dev) ? | |
1178 | "Invalid device" : dev_name(dev), | |
1179 | error); | |
06441658 | 1180 | goto unlock; |
737002b5 VK |
1181 | } |
1182 | ||
06441658 VK |
1183 | /* Find if dev_opp manages a single device */ |
1184 | if (list_is_singular(&dev_opp->dev_list)) { | |
1185 | /* Free static OPPs */ | |
1186 | list_for_each_entry_safe(opp, tmp, &dev_opp->opp_list, node) { | |
1187 | if (!opp->dynamic) | |
1188 | _opp_remove(dev_opp, opp, true); | |
1189 | } | |
1190 | } else { | |
1191 | _remove_list_dev(_find_list_dev(dev, dev_opp), dev_opp); | |
737002b5 VK |
1192 | } |
1193 | ||
06441658 | 1194 | unlock: |
737002b5 VK |
1195 | mutex_unlock(&dev_opp_list_lock); |
1196 | } | |
1197 | EXPORT_SYMBOL_GPL(of_free_opp_table); | |
1198 | ||
8d4d4e98 VK |
1199 | void of_cpumask_free_opp_table(cpumask_var_t cpumask) |
1200 | { | |
1201 | struct device *cpu_dev; | |
1202 | int cpu; | |
1203 | ||
1204 | WARN_ON(cpumask_empty(cpumask)); | |
1205 | ||
1206 | for_each_cpu(cpu, cpumask) { | |
1207 | cpu_dev = get_cpu_device(cpu); | |
1208 | if (!cpu_dev) { | |
1209 | pr_err("%s: failed to get cpu%d device\n", __func__, | |
1210 | cpu); | |
1211 | continue; | |
1212 | } | |
1213 | ||
1214 | of_free_opp_table(cpu_dev); | |
1215 | } | |
1216 | } | |
1217 | EXPORT_SYMBOL_GPL(of_cpumask_free_opp_table); | |
1218 | ||
27465902 VK |
1219 | /* Returns opp descriptor node from its phandle. Caller must do of_node_put() */ |
1220 | static struct device_node * | |
1221 | _of_get_opp_desc_node_from_prop(struct device *dev, const struct property *prop) | |
1222 | { | |
1223 | struct device_node *opp_np; | |
1224 | ||
1225 | opp_np = of_find_node_by_phandle(be32_to_cpup(prop->value)); | |
1226 | if (!opp_np) { | |
1227 | dev_err(dev, "%s: Prop: %s contains invalid opp desc phandle\n", | |
1228 | __func__, prop->name); | |
1229 | return ERR_PTR(-EINVAL); | |
1230 | } | |
1231 | ||
1232 | return opp_np; | |
1233 | } | |
1234 | ||
8d4d4e98 VK |
1235 | /* Returns opp descriptor node for a device. Caller must do of_node_put() */ |
1236 | static struct device_node *_of_get_opp_desc_node(struct device *dev) | |
1237 | { | |
1238 | const struct property *prop; | |
1239 | ||
1240 | prop = of_find_property(dev->of_node, "operating-points-v2", NULL); | |
1241 | if (!prop) | |
1242 | return ERR_PTR(-ENODEV); | |
1243 | if (!prop->value) | |
1244 | return ERR_PTR(-ENODATA); | |
1245 | ||
1246 | /* | |
1247 | * TODO: Support for multiple OPP tables. | |
1248 | * | |
1249 | * There should be only ONE phandle present in "operating-points-v2" | |
1250 | * property. | |
1251 | */ | |
1252 | if (prop->length != sizeof(__be32)) { | |
1253 | dev_err(dev, "%s: Invalid opp desc phandle\n", __func__); | |
1254 | return ERR_PTR(-EINVAL); | |
1255 | } | |
1256 | ||
1257 | return _of_get_opp_desc_node_from_prop(dev, prop); | |
1258 | } | |
1259 | ||
27465902 VK |
1260 | /* Initializes OPP tables based on new bindings */ |
1261 | static int _of_init_opp_table_v2(struct device *dev, | |
1262 | const struct property *prop) | |
1263 | { | |
1264 | struct device_node *opp_np, *np; | |
06441658 | 1265 | struct device_opp *dev_opp; |
27465902 VK |
1266 | int ret = 0, count = 0; |
1267 | ||
1268 | if (!prop->value) | |
1269 | return -ENODATA; | |
1270 | ||
1271 | /* Get opp node */ | |
1272 | opp_np = _of_get_opp_desc_node_from_prop(dev, prop); | |
1273 | if (IS_ERR(opp_np)) | |
1274 | return PTR_ERR(opp_np); | |
1275 | ||
06441658 VK |
1276 | dev_opp = _managed_opp(opp_np); |
1277 | if (dev_opp) { | |
1278 | /* OPPs are already managed */ | |
1279 | if (!_add_list_dev(dev, dev_opp)) | |
1280 | ret = -ENOMEM; | |
1281 | goto put_opp_np; | |
1282 | } | |
1283 | ||
27465902 VK |
1284 | /* We have opp-list node now, iterate over it and add OPPs */ |
1285 | for_each_available_child_of_node(opp_np, np) { | |
1286 | count++; | |
1287 | ||
1288 | ret = _opp_add_static_v2(dev, np); | |
1289 | if (ret) { | |
1290 | dev_err(dev, "%s: Failed to add OPP, %d\n", __func__, | |
1291 | ret); | |
1292 | break; | |
1293 | } | |
1294 | } | |
1295 | ||
1296 | /* There should be one of more OPP defined */ | |
1297 | if (WARN_ON(!count)) | |
1298 | goto put_opp_np; | |
1299 | ||
06441658 VK |
1300 | if (!ret) { |
1301 | if (!dev_opp) { | |
1302 | dev_opp = _find_device_opp(dev); | |
1303 | if (WARN_ON(!dev_opp)) | |
1304 | goto put_opp_np; | |
1305 | } | |
1306 | ||
1307 | dev_opp->np = opp_np; | |
1308 | dev_opp->shared_opp = of_property_read_bool(opp_np, | |
1309 | "opp-shared"); | |
1310 | } else { | |
27465902 | 1311 | of_free_opp_table(dev); |
06441658 | 1312 | } |
27465902 VK |
1313 | |
1314 | put_opp_np: | |
1315 | of_node_put(opp_np); | |
1316 | ||
1317 | return ret; | |
1318 | } | |
1319 | ||
1320 | /* Initializes OPP tables based on old-deprecated bindings */ | |
1321 | static int _of_init_opp_table_v1(struct device *dev) | |
b496dfbc SG |
1322 | { |
1323 | const struct property *prop; | |
1324 | const __be32 *val; | |
1325 | int nr; | |
1326 | ||
1327 | prop = of_find_property(dev->of_node, "operating-points", NULL); | |
1328 | if (!prop) | |
1329 | return -ENODEV; | |
1330 | if (!prop->value) | |
1331 | return -ENODATA; | |
1332 | ||
1333 | /* | |
1334 | * Each OPP is a set of tuples consisting of frequency and | |
1335 | * voltage like <freq-kHz vol-uV>. | |
1336 | */ | |
1337 | nr = prop->length / sizeof(u32); | |
1338 | if (nr % 2) { | |
1339 | dev_err(dev, "%s: Invalid OPP list\n", __func__); | |
1340 | return -EINVAL; | |
1341 | } | |
1342 | ||
1343 | val = prop->value; | |
1344 | while (nr) { | |
1345 | unsigned long freq = be32_to_cpup(val++) * 1000; | |
1346 | unsigned long volt = be32_to_cpup(val++); | |
1347 | ||
327854c8 | 1348 | if (_opp_add_dynamic(dev, freq, volt, false)) |
b496dfbc SG |
1349 | dev_warn(dev, "%s: Failed to add OPP %ld\n", |
1350 | __func__, freq); | |
b496dfbc SG |
1351 | nr -= 2; |
1352 | } | |
1353 | ||
1354 | return 0; | |
1355 | } | |
27465902 VK |
1356 | |
1357 | /** | |
1358 | * of_init_opp_table() - Initialize opp table from device tree | |
1359 | * @dev: device pointer used to lookup device OPPs. | |
1360 | * | |
1361 | * Register the initial OPP table with the OPP library for given device. | |
1362 | * | |
1363 | * Locking: The internal device_opp and opp structures are RCU protected. | |
1364 | * Hence this function indirectly uses RCU updater strategy with mutex locks | |
1365 | * to keep the integrity of the internal data structures. Callers should ensure | |
1366 | * that this function is *NOT* called under RCU protection or in contexts where | |
1367 | * mutex cannot be locked. | |
1368 | * | |
1369 | * Return: | |
1370 | * 0 On success OR | |
1371 | * Duplicate OPPs (both freq and volt are same) and opp->available | |
1372 | * -EEXIST Freq are same and volt are different OR | |
1373 | * Duplicate OPPs (both freq and volt are same) and !opp->available | |
1374 | * -ENOMEM Memory allocation failure | |
1375 | * -ENODEV when 'operating-points' property is not found or is invalid data | |
1376 | * in device node. | |
1377 | * -ENODATA when empty 'operating-points' property is found | |
1378 | * -EINVAL when invalid entries are found in opp-v2 table | |
1379 | */ | |
1380 | int of_init_opp_table(struct device *dev) | |
1381 | { | |
1382 | const struct property *prop; | |
1383 | ||
1384 | /* | |
1385 | * OPPs have two version of bindings now. The older one is deprecated, | |
1386 | * try for the new binding first. | |
1387 | */ | |
1388 | prop = of_find_property(dev->of_node, "operating-points-v2", NULL); | |
1389 | if (!prop) { | |
1390 | /* | |
1391 | * Try old-deprecated bindings for backward compatibility with | |
1392 | * older dtbs. | |
1393 | */ | |
1394 | return _of_init_opp_table_v1(dev); | |
1395 | } | |
1396 | ||
1397 | return _of_init_opp_table_v2(dev, prop); | |
1398 | } | |
74c46c6e | 1399 | EXPORT_SYMBOL_GPL(of_init_opp_table); |
8d4d4e98 VK |
1400 | |
1401 | int of_cpumask_init_opp_table(cpumask_var_t cpumask) | |
1402 | { | |
1403 | struct device *cpu_dev; | |
1404 | int cpu, ret = 0; | |
1405 | ||
1406 | WARN_ON(cpumask_empty(cpumask)); | |
1407 | ||
1408 | for_each_cpu(cpu, cpumask) { | |
1409 | cpu_dev = get_cpu_device(cpu); | |
1410 | if (!cpu_dev) { | |
1411 | pr_err("%s: failed to get cpu%d device\n", __func__, | |
1412 | cpu); | |
1413 | continue; | |
1414 | } | |
1415 | ||
1416 | ret = of_init_opp_table(cpu_dev); | |
1417 | if (ret) { | |
1418 | pr_err("%s: couldn't find opp table for cpu:%d, %d\n", | |
1419 | __func__, cpu, ret); | |
1420 | ||
1421 | /* Free all other OPPs */ | |
1422 | of_cpumask_free_opp_table(cpumask); | |
1423 | break; | |
1424 | } | |
1425 | } | |
1426 | ||
1427 | return ret; | |
1428 | } | |
1429 | EXPORT_SYMBOL_GPL(of_cpumask_init_opp_table); | |
1430 | ||
1431 | /* Required only for V1 bindings, as v2 can manage it from DT itself */ | |
1432 | int set_cpus_sharing_opps(struct device *cpu_dev, cpumask_var_t cpumask) | |
1433 | { | |
1434 | struct device_list_opp *list_dev; | |
1435 | struct device_opp *dev_opp; | |
1436 | struct device *dev; | |
1437 | int cpu, ret = 0; | |
1438 | ||
1439 | rcu_read_lock(); | |
1440 | ||
1441 | dev_opp = _find_device_opp(cpu_dev); | |
1442 | if (IS_ERR(dev_opp)) { | |
1443 | ret = -EINVAL; | |
1444 | goto out_rcu_read_unlock; | |
1445 | } | |
1446 | ||
1447 | for_each_cpu(cpu, cpumask) { | |
1448 | if (cpu == cpu_dev->id) | |
1449 | continue; | |
1450 | ||
1451 | dev = get_cpu_device(cpu); | |
1452 | if (!dev) { | |
1453 | dev_err(cpu_dev, "%s: failed to get cpu%d device\n", | |
1454 | __func__, cpu); | |
1455 | continue; | |
1456 | } | |
1457 | ||
1458 | list_dev = _add_list_dev(dev, dev_opp); | |
1459 | if (!list_dev) { | |
1460 | dev_err(dev, "%s: failed to add list-dev for cpu%d device\n", | |
1461 | __func__, cpu); | |
1462 | continue; | |
1463 | } | |
1464 | } | |
1465 | out_rcu_read_unlock: | |
1466 | rcu_read_unlock(); | |
1467 | ||
1468 | return 0; | |
1469 | } | |
1470 | EXPORT_SYMBOL_GPL(set_cpus_sharing_opps); | |
1471 | ||
1472 | /* | |
1473 | * Works only for OPP v2 bindings. | |
1474 | * | |
1475 | * cpumask should be already set to mask of cpu_dev->id. | |
1476 | * Returns -ENOENT if operating-points-v2 bindings aren't supported. | |
1477 | */ | |
1478 | int of_get_cpus_sharing_opps(struct device *cpu_dev, cpumask_var_t cpumask) | |
1479 | { | |
1480 | struct device_node *np, *tmp_np; | |
1481 | struct device *tcpu_dev; | |
1482 | int cpu, ret = 0; | |
1483 | ||
1484 | /* Get OPP descriptor node */ | |
1485 | np = _of_get_opp_desc_node(cpu_dev); | |
1486 | if (IS_ERR(np)) { | |
1487 | dev_dbg(cpu_dev, "%s: Couldn't find opp node: %ld\n", __func__, | |
1488 | PTR_ERR(np)); | |
1489 | return -ENOENT; | |
1490 | } | |
1491 | ||
1492 | /* OPPs are shared ? */ | |
1493 | if (!of_property_read_bool(np, "opp-shared")) | |
1494 | goto put_cpu_node; | |
1495 | ||
1496 | for_each_possible_cpu(cpu) { | |
1497 | if (cpu == cpu_dev->id) | |
1498 | continue; | |
1499 | ||
1500 | tcpu_dev = get_cpu_device(cpu); | |
1501 | if (!tcpu_dev) { | |
1502 | dev_err(cpu_dev, "%s: failed to get cpu%d device\n", | |
1503 | __func__, cpu); | |
1504 | ret = -ENODEV; | |
1505 | goto put_cpu_node; | |
1506 | } | |
1507 | ||
1508 | /* Get OPP descriptor node */ | |
1509 | tmp_np = _of_get_opp_desc_node(tcpu_dev); | |
1510 | if (IS_ERR(tmp_np)) { | |
1511 | dev_err(tcpu_dev, "%s: Couldn't find opp node: %ld\n", | |
1512 | __func__, PTR_ERR(tmp_np)); | |
1513 | ret = PTR_ERR(tmp_np); | |
1514 | goto put_cpu_node; | |
1515 | } | |
1516 | ||
1517 | /* CPUs are sharing opp node */ | |
1518 | if (np == tmp_np) | |
1519 | cpumask_set_cpu(cpu, cpumask); | |
1520 | ||
1521 | of_node_put(tmp_np); | |
1522 | } | |
1523 | ||
1524 | put_cpu_node: | |
1525 | of_node_put(np); | |
1526 | return ret; | |
1527 | } | |
1528 | EXPORT_SYMBOL_GPL(of_get_cpus_sharing_opps); | |
b496dfbc | 1529 | #endif |