]>
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 | ||
14 | #include <linux/kernel.h> | |
15 | #include <linux/errno.h> | |
16 | #include <linux/err.h> | |
e1f60b29 | 17 | #include <linux/slab.h> |
51990e82 | 18 | #include <linux/device.h> |
e1f60b29 NM |
19 | #include <linux/list.h> |
20 | #include <linux/rculist.h> | |
21 | #include <linux/rcupdate.h> | |
e4db1c74 | 22 | #include <linux/pm_opp.h> |
b496dfbc | 23 | #include <linux/of.h> |
80126ce7 | 24 | #include <linux/export.h> |
e1f60b29 NM |
25 | |
26 | /* | |
27 | * Internal data structure organization with the OPP layer library is as | |
28 | * follows: | |
29 | * dev_opp_list (root) | |
30 | * |- device 1 (represents voltage domain 1) | |
31 | * | |- opp 1 (availability, freq, voltage) | |
32 | * | |- opp 2 .. | |
33 | * ... ... | |
34 | * | `- opp n .. | |
35 | * |- device 2 (represents the next voltage domain) | |
36 | * ... | |
37 | * `- device m (represents mth voltage domain) | |
38 | * device 1, 2.. are represented by dev_opp structure while each opp | |
39 | * is represented by the opp structure. | |
40 | */ | |
41 | ||
42 | /** | |
47d43ba7 | 43 | * struct dev_pm_opp - Generic OPP description structure |
e1f60b29 NM |
44 | * @node: opp list node. The nodes are maintained throughout the lifetime |
45 | * of boot. It is expected only an optimal set of OPPs are | |
46 | * added to the library by the SoC framework. | |
47 | * RCU usage: opp list is traversed with RCU locks. node | |
48 | * modification is possible realtime, hence the modifications | |
49 | * are protected by the dev_opp_list_lock for integrity. | |
50 | * IMPORTANT: the opp nodes should be maintained in increasing | |
51 | * order. | |
38393409 | 52 | * @dynamic: not-created from static DT entries. |
e1f60b29 NM |
53 | * @available: true/false - marks if this OPP as available or not |
54 | * @rate: Frequency in hertz | |
55 | * @u_volt: Nominal voltage in microvolts corresponding to this OPP | |
56 | * @dev_opp: points back to the device_opp struct this opp belongs to | |
cd1a068a | 57 | * @rcu_head: RCU callback head used for deferred freeing |
e1f60b29 NM |
58 | * |
59 | * This structure stores the OPP information for a given device. | |
60 | */ | |
47d43ba7 | 61 | struct dev_pm_opp { |
e1f60b29 NM |
62 | struct list_head node; |
63 | ||
64 | bool available; | |
38393409 | 65 | bool dynamic; |
e1f60b29 NM |
66 | unsigned long rate; |
67 | unsigned long u_volt; | |
68 | ||
69 | struct device_opp *dev_opp; | |
cd1a068a | 70 | struct rcu_head rcu_head; |
e1f60b29 NM |
71 | }; |
72 | ||
73 | /** | |
74 | * struct device_opp - Device opp structure | |
75 | * @node: list node - contains the devices with OPPs that | |
76 | * have been registered. Nodes once added are not modified in this | |
77 | * list. | |
78 | * RCU usage: nodes are not modified in the list of device_opp, | |
79 | * however addition is possible and is secured by dev_opp_list_lock | |
80 | * @dev: device pointer | |
cd1a068a | 81 | * @srcu_head: notifier head to notify the OPP availability changes. |
129eec55 | 82 | * @rcu_head: RCU callback head used for deferred freeing |
e1f60b29 NM |
83 | * @opp_list: list of opps |
84 | * | |
85 | * This is an internal data structure maintaining the link to opps attached to | |
86 | * a device. This structure is not meant to be shared to users as it is | |
1c6a662f VK |
87 | * meant for book keeping and private to OPP library. |
88 | * | |
89 | * Because the opp structures can be used from both rcu and srcu readers, we | |
90 | * need to wait for the grace period of both of them before freeing any | |
91 | * resources. And so we have used kfree_rcu() from within call_srcu() handlers. | |
e1f60b29 NM |
92 | */ |
93 | struct device_opp { | |
94 | struct list_head node; | |
95 | ||
96 | struct device *dev; | |
cd1a068a | 97 | struct srcu_notifier_head srcu_head; |
129eec55 | 98 | struct rcu_head rcu_head; |
e1f60b29 NM |
99 | struct list_head opp_list; |
100 | }; | |
101 | ||
102 | /* | |
103 | * The root of the list of all devices. All device_opp structures branch off | |
104 | * from here, with each device_opp containing the list of opp it supports in | |
105 | * various states of availability. | |
106 | */ | |
107 | static LIST_HEAD(dev_opp_list); | |
108 | /* Lock to allow exclusive modification to the device and opp lists */ | |
109 | static DEFINE_MUTEX(dev_opp_list_lock); | |
110 | ||
b02ded24 DT |
111 | #define opp_rcu_lockdep_assert() \ |
112 | do { \ | |
113 | rcu_lockdep_assert(rcu_read_lock_held() || \ | |
114 | lockdep_is_held(&dev_opp_list_lock), \ | |
115 | "Missing rcu_read_lock() or " \ | |
116 | "dev_opp_list_lock protection"); \ | |
117 | } while (0) | |
118 | ||
e1f60b29 | 119 | /** |
327854c8 | 120 | * _find_device_opp() - find device_opp struct using device pointer |
e1f60b29 NM |
121 | * @dev: device pointer used to lookup device OPPs |
122 | * | |
123 | * Search list of device OPPs for one containing matching device. Does a RCU | |
124 | * reader operation to grab the pointer needed. | |
125 | * | |
984f16c8 | 126 | * Return: pointer to 'struct device_opp' if found, otherwise -ENODEV or |
e1f60b29 NM |
127 | * -EINVAL based on type of error. |
128 | * | |
129 | * Locking: This function must be called under rcu_read_lock(). device_opp | |
130 | * is a RCU protected pointer. This means that device_opp is valid as long | |
131 | * as we are under RCU lock. | |
132 | */ | |
327854c8 | 133 | static struct device_opp *_find_device_opp(struct device *dev) |
e1f60b29 NM |
134 | { |
135 | struct device_opp *tmp_dev_opp, *dev_opp = ERR_PTR(-ENODEV); | |
136 | ||
137 | if (unlikely(IS_ERR_OR_NULL(dev))) { | |
138 | pr_err("%s: Invalid parameters\n", __func__); | |
139 | return ERR_PTR(-EINVAL); | |
140 | } | |
141 | ||
142 | list_for_each_entry_rcu(tmp_dev_opp, &dev_opp_list, node) { | |
143 | if (tmp_dev_opp->dev == dev) { | |
144 | dev_opp = tmp_dev_opp; | |
145 | break; | |
146 | } | |
147 | } | |
148 | ||
149 | return dev_opp; | |
150 | } | |
151 | ||
152 | /** | |
5d4879cd | 153 | * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an available opp |
e1f60b29 NM |
154 | * @opp: opp for which voltage has to be returned for |
155 | * | |
984f16c8 | 156 | * Return: voltage in micro volt corresponding to the opp, else |
e1f60b29 NM |
157 | * return 0 |
158 | * | |
159 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu | |
160 | * protected pointer. This means that opp which could have been fetched by | |
161 | * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are | |
162 | * under RCU lock. The pointer returned by the opp_find_freq family must be | |
163 | * used in the same section as the usage of this function with the pointer | |
164 | * prior to unlocking with rcu_read_unlock() to maintain the integrity of the | |
165 | * pointer. | |
166 | */ | |
47d43ba7 | 167 | unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp) |
e1f60b29 | 168 | { |
47d43ba7 | 169 | struct dev_pm_opp *tmp_opp; |
e1f60b29 NM |
170 | unsigned long v = 0; |
171 | ||
04bf1c7f KK |
172 | opp_rcu_lockdep_assert(); |
173 | ||
e1f60b29 NM |
174 | tmp_opp = rcu_dereference(opp); |
175 | if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available) | |
176 | pr_err("%s: Invalid parameters\n", __func__); | |
177 | else | |
178 | v = tmp_opp->u_volt; | |
179 | ||
180 | return v; | |
181 | } | |
5d4879cd | 182 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage); |
e1f60b29 NM |
183 | |
184 | /** | |
5d4879cd | 185 | * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp |
e1f60b29 NM |
186 | * @opp: opp for which frequency has to be returned for |
187 | * | |
984f16c8 | 188 | * Return: frequency in hertz corresponding to the opp, else |
e1f60b29 NM |
189 | * return 0 |
190 | * | |
191 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu | |
192 | * protected pointer. This means that opp which could have been fetched by | |
193 | * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are | |
194 | * under RCU lock. The pointer returned by the opp_find_freq family must be | |
195 | * used in the same section as the usage of this function with the pointer | |
196 | * prior to unlocking with rcu_read_unlock() to maintain the integrity of the | |
197 | * pointer. | |
198 | */ | |
47d43ba7 | 199 | unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp) |
e1f60b29 | 200 | { |
47d43ba7 | 201 | struct dev_pm_opp *tmp_opp; |
e1f60b29 NM |
202 | unsigned long f = 0; |
203 | ||
04bf1c7f KK |
204 | opp_rcu_lockdep_assert(); |
205 | ||
e1f60b29 NM |
206 | tmp_opp = rcu_dereference(opp); |
207 | if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available) | |
208 | pr_err("%s: Invalid parameters\n", __func__); | |
209 | else | |
210 | f = tmp_opp->rate; | |
211 | ||
212 | return f; | |
213 | } | |
5d4879cd | 214 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq); |
e1f60b29 NM |
215 | |
216 | /** | |
5d4879cd | 217 | * dev_pm_opp_get_opp_count() - Get number of opps available in the opp list |
e1f60b29 NM |
218 | * @dev: device for which we do this operation |
219 | * | |
984f16c8 | 220 | * Return: This function returns the number of available opps if there are any, |
e1f60b29 NM |
221 | * else returns 0 if none or the corresponding error value. |
222 | * | |
b4718c02 | 223 | * Locking: This function takes rcu_read_lock(). |
e1f60b29 | 224 | */ |
5d4879cd | 225 | int dev_pm_opp_get_opp_count(struct device *dev) |
e1f60b29 NM |
226 | { |
227 | struct device_opp *dev_opp; | |
47d43ba7 | 228 | struct dev_pm_opp *temp_opp; |
e1f60b29 NM |
229 | int count = 0; |
230 | ||
b4718c02 | 231 | rcu_read_lock(); |
b02ded24 | 232 | |
327854c8 | 233 | dev_opp = _find_device_opp(dev); |
e1f60b29 | 234 | if (IS_ERR(dev_opp)) { |
b4718c02 DT |
235 | count = PTR_ERR(dev_opp); |
236 | dev_err(dev, "%s: device OPP not found (%d)\n", | |
237 | __func__, count); | |
238 | goto out_unlock; | |
e1f60b29 NM |
239 | } |
240 | ||
241 | list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) { | |
242 | if (temp_opp->available) | |
243 | count++; | |
244 | } | |
245 | ||
b4718c02 DT |
246 | out_unlock: |
247 | rcu_read_unlock(); | |
e1f60b29 NM |
248 | return count; |
249 | } | |
5d4879cd | 250 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count); |
e1f60b29 NM |
251 | |
252 | /** | |
5d4879cd | 253 | * dev_pm_opp_find_freq_exact() - search for an exact frequency |
e1f60b29 NM |
254 | * @dev: device for which we do this operation |
255 | * @freq: frequency to search for | |
7ae49618 | 256 | * @available: true/false - match for available opp |
e1f60b29 | 257 | * |
984f16c8 NM |
258 | * Return: Searches for exact match in the opp list and returns pointer to the |
259 | * matching opp if found, else returns ERR_PTR in case of error and should | |
260 | * be handled using IS_ERR. Error return values can be: | |
0779726c NM |
261 | * EINVAL: for bad pointer |
262 | * ERANGE: no match found for search | |
263 | * ENODEV: if device not found in list of registered devices | |
e1f60b29 NM |
264 | * |
265 | * Note: available is a modifier for the search. if available=true, then the | |
266 | * match is for exact matching frequency and is available in the stored OPP | |
267 | * table. if false, the match is for exact frequency which is not available. | |
268 | * | |
269 | * This provides a mechanism to enable an opp which is not available currently | |
270 | * or the opposite as well. | |
271 | * | |
272 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu | |
273 | * protected pointer. The reason for the same is that the opp pointer which is | |
274 | * returned will remain valid for use with opp_get_{voltage, freq} only while | |
275 | * under the locked area. The pointer returned must be used prior to unlocking | |
276 | * with rcu_read_unlock() to maintain the integrity of the pointer. | |
277 | */ | |
47d43ba7 NM |
278 | struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev, |
279 | unsigned long freq, | |
280 | bool available) | |
e1f60b29 NM |
281 | { |
282 | struct device_opp *dev_opp; | |
47d43ba7 | 283 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); |
e1f60b29 | 284 | |
b02ded24 DT |
285 | opp_rcu_lockdep_assert(); |
286 | ||
327854c8 | 287 | dev_opp = _find_device_opp(dev); |
e1f60b29 NM |
288 | if (IS_ERR(dev_opp)) { |
289 | int r = PTR_ERR(dev_opp); | |
290 | dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r); | |
291 | return ERR_PTR(r); | |
292 | } | |
293 | ||
294 | list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) { | |
295 | if (temp_opp->available == available && | |
296 | temp_opp->rate == freq) { | |
297 | opp = temp_opp; | |
298 | break; | |
299 | } | |
300 | } | |
301 | ||
302 | return opp; | |
303 | } | |
5d4879cd | 304 | EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact); |
e1f60b29 NM |
305 | |
306 | /** | |
5d4879cd | 307 | * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq |
e1f60b29 NM |
308 | * @dev: device for which we do this operation |
309 | * @freq: Start frequency | |
310 | * | |
311 | * Search for the matching ceil *available* OPP from a starting freq | |
312 | * for a device. | |
313 | * | |
984f16c8 | 314 | * Return: matching *opp and refreshes *freq accordingly, else returns |
0779726c NM |
315 | * ERR_PTR in case of error and should be handled using IS_ERR. Error return |
316 | * values can be: | |
317 | * EINVAL: for bad pointer | |
318 | * ERANGE: no match found for search | |
319 | * ENODEV: if device not found in list of registered devices | |
e1f60b29 NM |
320 | * |
321 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu | |
322 | * protected pointer. The reason for the same is that the opp pointer which is | |
323 | * returned will remain valid for use with opp_get_{voltage, freq} only while | |
324 | * under the locked area. The pointer returned must be used prior to unlocking | |
325 | * with rcu_read_unlock() to maintain the integrity of the pointer. | |
326 | */ | |
47d43ba7 NM |
327 | struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev, |
328 | unsigned long *freq) | |
e1f60b29 NM |
329 | { |
330 | struct device_opp *dev_opp; | |
47d43ba7 | 331 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); |
e1f60b29 | 332 | |
b02ded24 DT |
333 | opp_rcu_lockdep_assert(); |
334 | ||
e1f60b29 NM |
335 | if (!dev || !freq) { |
336 | dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); | |
337 | return ERR_PTR(-EINVAL); | |
338 | } | |
339 | ||
327854c8 | 340 | dev_opp = _find_device_opp(dev); |
e1f60b29 | 341 | if (IS_ERR(dev_opp)) |
0779726c | 342 | return ERR_CAST(dev_opp); |
e1f60b29 NM |
343 | |
344 | list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) { | |
345 | if (temp_opp->available && temp_opp->rate >= *freq) { | |
346 | opp = temp_opp; | |
347 | *freq = opp->rate; | |
348 | break; | |
349 | } | |
350 | } | |
351 | ||
352 | return opp; | |
353 | } | |
5d4879cd | 354 | EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil); |
e1f60b29 NM |
355 | |
356 | /** | |
5d4879cd | 357 | * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq |
e1f60b29 NM |
358 | * @dev: device for which we do this operation |
359 | * @freq: Start frequency | |
360 | * | |
361 | * Search for the matching floor *available* OPP from a starting freq | |
362 | * for a device. | |
363 | * | |
984f16c8 | 364 | * Return: matching *opp and refreshes *freq accordingly, else returns |
0779726c NM |
365 | * ERR_PTR in case of error and should be handled using IS_ERR. Error return |
366 | * values can be: | |
367 | * EINVAL: for bad pointer | |
368 | * ERANGE: no match found for search | |
369 | * ENODEV: if device not found in list of registered devices | |
e1f60b29 NM |
370 | * |
371 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu | |
372 | * protected pointer. The reason for the same is that the opp pointer which is | |
373 | * returned will remain valid for use with opp_get_{voltage, freq} only while | |
374 | * under the locked area. The pointer returned must be used prior to unlocking | |
375 | * with rcu_read_unlock() to maintain the integrity of the pointer. | |
376 | */ | |
47d43ba7 NM |
377 | struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev, |
378 | unsigned long *freq) | |
e1f60b29 NM |
379 | { |
380 | struct device_opp *dev_opp; | |
47d43ba7 | 381 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); |
e1f60b29 | 382 | |
b02ded24 DT |
383 | opp_rcu_lockdep_assert(); |
384 | ||
e1f60b29 NM |
385 | if (!dev || !freq) { |
386 | dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); | |
387 | return ERR_PTR(-EINVAL); | |
388 | } | |
389 | ||
327854c8 | 390 | dev_opp = _find_device_opp(dev); |
e1f60b29 | 391 | if (IS_ERR(dev_opp)) |
0779726c | 392 | return ERR_CAST(dev_opp); |
e1f60b29 NM |
393 | |
394 | list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) { | |
395 | if (temp_opp->available) { | |
396 | /* go to the next node, before choosing prev */ | |
397 | if (temp_opp->rate > *freq) | |
398 | break; | |
399 | else | |
400 | opp = temp_opp; | |
401 | } | |
402 | } | |
403 | if (!IS_ERR(opp)) | |
404 | *freq = opp->rate; | |
405 | ||
406 | return opp; | |
407 | } | |
5d4879cd | 408 | EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor); |
e1f60b29 | 409 | |
984f16c8 NM |
410 | /** |
411 | * _add_device_opp() - Allocate a new device OPP table | |
412 | * @dev: device for which we do this operation | |
413 | * | |
414 | * New device node which uses OPPs - used when multiple devices with OPP tables | |
415 | * are maintained. | |
416 | * | |
417 | * Return: valid device_opp pointer if success, else NULL. | |
418 | */ | |
327854c8 | 419 | static struct device_opp *_add_device_opp(struct device *dev) |
07cce74a VK |
420 | { |
421 | struct device_opp *dev_opp; | |
422 | ||
423 | /* | |
424 | * Allocate a new device OPP table. In the infrequent case where a new | |
425 | * device is needed to be added, we pay this penalty. | |
426 | */ | |
427 | dev_opp = kzalloc(sizeof(*dev_opp), GFP_KERNEL); | |
428 | if (!dev_opp) | |
429 | return NULL; | |
430 | ||
431 | dev_opp->dev = dev; | |
432 | srcu_init_notifier_head(&dev_opp->srcu_head); | |
433 | INIT_LIST_HEAD(&dev_opp->opp_list); | |
434 | ||
435 | /* Secure the device list modification */ | |
436 | list_add_rcu(&dev_opp->node, &dev_opp_list); | |
437 | return dev_opp; | |
438 | } | |
439 | ||
984f16c8 NM |
440 | /** |
441 | * _opp_add_dynamic() - Allocate a dynamic OPP. | |
442 | * @dev: device for which we do this operation | |
443 | * @freq: Frequency in Hz for this OPP | |
444 | * @u_volt: Voltage in uVolts for this OPP | |
445 | * @dynamic: Dynamically added OPPs. | |
446 | * | |
447 | * This function adds an opp definition to the opp list and returns status. | |
448 | * The opp is made available by default and it can be controlled using | |
449 | * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove. | |
450 | * | |
451 | * NOTE: "dynamic" parameter impacts OPPs added by the of_init_opp_table and | |
452 | * freed by of_free_opp_table. | |
453 | * | |
454 | * Locking: The internal device_opp and opp structures are RCU protected. | |
455 | * Hence this function internally uses RCU updater strategy with mutex locks | |
456 | * to keep the integrity of the internal data structures. Callers should ensure | |
457 | * that this function is *NOT* called under RCU protection or in contexts where | |
458 | * mutex cannot be locked. | |
459 | * | |
460 | * Return: | |
461 | * 0 On success OR | |
462 | * Duplicate OPPs (both freq and volt are same) and opp->available | |
463 | * -EEXIST Freq are same and volt are different OR | |
464 | * Duplicate OPPs (both freq and volt are same) and !opp->available | |
465 | * -ENOMEM Memory allocation failure | |
466 | */ | |
327854c8 NM |
467 | static int _opp_add_dynamic(struct device *dev, unsigned long freq, |
468 | long u_volt, bool dynamic) | |
e1f60b29 NM |
469 | { |
470 | struct device_opp *dev_opp = NULL; | |
47d43ba7 | 471 | struct dev_pm_opp *opp, *new_opp; |
e1f60b29 | 472 | struct list_head *head; |
6ce4184d | 473 | int ret; |
e1f60b29 NM |
474 | |
475 | /* allocate new OPP node */ | |
47d43ba7 | 476 | new_opp = kzalloc(sizeof(*new_opp), GFP_KERNEL); |
e1f60b29 NM |
477 | if (!new_opp) { |
478 | dev_warn(dev, "%s: Unable to create new OPP node\n", __func__); | |
479 | return -ENOMEM; | |
480 | } | |
481 | ||
482 | /* Hold our list modification lock here */ | |
483 | mutex_lock(&dev_opp_list_lock); | |
484 | ||
a7470db6 | 485 | /* populate the opp table */ |
a7470db6 VK |
486 | new_opp->rate = freq; |
487 | new_opp->u_volt = u_volt; | |
488 | new_opp->available = true; | |
38393409 | 489 | new_opp->dynamic = dynamic; |
a7470db6 | 490 | |
e1f60b29 | 491 | /* Check for existing list for 'dev' */ |
327854c8 | 492 | dev_opp = _find_device_opp(dev); |
e1f60b29 | 493 | if (IS_ERR(dev_opp)) { |
327854c8 | 494 | dev_opp = _add_device_opp(dev); |
e1f60b29 | 495 | if (!dev_opp) { |
6ce4184d VK |
496 | ret = -ENOMEM; |
497 | goto free_opp; | |
e1f60b29 NM |
498 | } |
499 | ||
a7470db6 VK |
500 | head = &dev_opp->opp_list; |
501 | goto list_add; | |
e1f60b29 NM |
502 | } |
503 | ||
64ce8545 CK |
504 | /* |
505 | * Insert new OPP in order of increasing frequency | |
506 | * and discard if already present | |
507 | */ | |
e1f60b29 NM |
508 | head = &dev_opp->opp_list; |
509 | list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) { | |
64ce8545 | 510 | if (new_opp->rate <= opp->rate) |
e1f60b29 NM |
511 | break; |
512 | else | |
513 | head = &opp->node; | |
514 | } | |
515 | ||
64ce8545 CK |
516 | /* Duplicate OPPs ? */ |
517 | if (new_opp->rate == opp->rate) { | |
6ce4184d | 518 | ret = opp->available && new_opp->u_volt == opp->u_volt ? |
64ce8545 CK |
519 | 0 : -EEXIST; |
520 | ||
521 | dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n", | |
522 | __func__, opp->rate, opp->u_volt, opp->available, | |
523 | new_opp->rate, new_opp->u_volt, new_opp->available); | |
6ce4184d | 524 | goto free_opp; |
64ce8545 CK |
525 | } |
526 | ||
a7470db6 | 527 | list_add: |
7284a00f | 528 | new_opp->dev_opp = dev_opp; |
e1f60b29 NM |
529 | list_add_rcu(&new_opp->node, head); |
530 | mutex_unlock(&dev_opp_list_lock); | |
531 | ||
03ca370f MH |
532 | /* |
533 | * Notify the changes in the availability of the operable | |
534 | * frequency/voltage list. | |
535 | */ | |
cd1a068a | 536 | srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp); |
e1f60b29 | 537 | return 0; |
6ce4184d VK |
538 | |
539 | free_opp: | |
540 | mutex_unlock(&dev_opp_list_lock); | |
541 | kfree(new_opp); | |
542 | return ret; | |
e1f60b29 | 543 | } |
38393409 VK |
544 | |
545 | /** | |
546 | * dev_pm_opp_add() - Add an OPP table from a table definitions | |
547 | * @dev: device for which we do this operation | |
548 | * @freq: Frequency in Hz for this OPP | |
549 | * @u_volt: Voltage in uVolts for this OPP | |
550 | * | |
551 | * This function adds an opp definition to the opp list and returns status. | |
552 | * The opp is made available by default and it can be controlled using | |
553 | * dev_pm_opp_enable/disable functions. | |
554 | * | |
555 | * Locking: The internal device_opp and opp structures are RCU protected. | |
556 | * Hence this function internally uses RCU updater strategy with mutex locks | |
557 | * to keep the integrity of the internal data structures. Callers should ensure | |
558 | * that this function is *NOT* called under RCU protection or in contexts where | |
559 | * mutex cannot be locked. | |
560 | * | |
561 | * Return: | |
984f16c8 | 562 | * 0 On success OR |
38393409 | 563 | * Duplicate OPPs (both freq and volt are same) and opp->available |
984f16c8 | 564 | * -EEXIST Freq are same and volt are different OR |
38393409 | 565 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
984f16c8 | 566 | * -ENOMEM Memory allocation failure |
38393409 VK |
567 | */ |
568 | int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt) | |
569 | { | |
327854c8 | 570 | return _opp_add_dynamic(dev, freq, u_volt, true); |
38393409 | 571 | } |
5d4879cd | 572 | EXPORT_SYMBOL_GPL(dev_pm_opp_add); |
e1f60b29 | 573 | |
984f16c8 NM |
574 | /** |
575 | * _kfree_opp_rcu() - Free OPP RCU handler | |
576 | * @head: RCU head | |
577 | */ | |
327854c8 | 578 | static void _kfree_opp_rcu(struct rcu_head *head) |
129eec55 VK |
579 | { |
580 | struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head); | |
581 | ||
582 | kfree_rcu(opp, rcu_head); | |
583 | } | |
584 | ||
984f16c8 NM |
585 | /** |
586 | * _kfree_device_rcu() - Free device_opp RCU handler | |
587 | * @head: RCU head | |
588 | */ | |
327854c8 | 589 | static void _kfree_device_rcu(struct rcu_head *head) |
129eec55 VK |
590 | { |
591 | struct device_opp *device_opp = container_of(head, struct device_opp, rcu_head); | |
592 | ||
1c6a662f | 593 | kfree_rcu(device_opp, rcu_head); |
129eec55 VK |
594 | } |
595 | ||
984f16c8 NM |
596 | /** |
597 | * _opp_remove() - Remove an OPP from a table definition | |
598 | * @dev_opp: points back to the device_opp struct this opp belongs to | |
599 | * @opp: pointer to the OPP to remove | |
600 | * | |
601 | * This function removes an opp definition from the opp list. | |
602 | * | |
603 | * Locking: The internal device_opp and opp structures are RCU protected. | |
604 | * It is assumed that the caller holds required mutex for an RCU updater | |
605 | * strategy. | |
606 | */ | |
327854c8 NM |
607 | static void _opp_remove(struct device_opp *dev_opp, |
608 | struct dev_pm_opp *opp) | |
129eec55 VK |
609 | { |
610 | /* | |
611 | * Notify the changes in the availability of the operable | |
612 | * frequency/voltage list. | |
613 | */ | |
614 | srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_REMOVE, opp); | |
615 | list_del_rcu(&opp->node); | |
327854c8 | 616 | call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu); |
129eec55 VK |
617 | |
618 | if (list_empty(&dev_opp->opp_list)) { | |
619 | list_del_rcu(&dev_opp->node); | |
620 | call_srcu(&dev_opp->srcu_head.srcu, &dev_opp->rcu_head, | |
327854c8 | 621 | _kfree_device_rcu); |
129eec55 VK |
622 | } |
623 | } | |
624 | ||
625 | /** | |
626 | * dev_pm_opp_remove() - Remove an OPP from OPP list | |
627 | * @dev: device for which we do this operation | |
628 | * @freq: OPP to remove with matching 'freq' | |
629 | * | |
630 | * This function removes an opp from the opp list. | |
984f16c8 NM |
631 | * |
632 | * Locking: The internal device_opp and opp structures are RCU protected. | |
633 | * Hence this function internally uses RCU updater strategy with mutex locks | |
634 | * to keep the integrity of the internal data structures. Callers should ensure | |
635 | * that this function is *NOT* called under RCU protection or in contexts where | |
636 | * mutex cannot be locked. | |
129eec55 VK |
637 | */ |
638 | void dev_pm_opp_remove(struct device *dev, unsigned long freq) | |
639 | { | |
640 | struct dev_pm_opp *opp; | |
641 | struct device_opp *dev_opp; | |
642 | bool found = false; | |
643 | ||
644 | /* Hold our list modification lock here */ | |
645 | mutex_lock(&dev_opp_list_lock); | |
646 | ||
327854c8 | 647 | dev_opp = _find_device_opp(dev); |
129eec55 VK |
648 | if (IS_ERR(dev_opp)) |
649 | goto unlock; | |
650 | ||
651 | list_for_each_entry(opp, &dev_opp->opp_list, node) { | |
652 | if (opp->rate == freq) { | |
653 | found = true; | |
654 | break; | |
655 | } | |
656 | } | |
657 | ||
658 | if (!found) { | |
659 | dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n", | |
660 | __func__, freq); | |
661 | goto unlock; | |
662 | } | |
663 | ||
327854c8 | 664 | _opp_remove(dev_opp, opp); |
129eec55 VK |
665 | unlock: |
666 | mutex_unlock(&dev_opp_list_lock); | |
667 | } | |
668 | EXPORT_SYMBOL_GPL(dev_pm_opp_remove); | |
669 | ||
e1f60b29 | 670 | /** |
327854c8 | 671 | * _opp_set_availability() - helper to set the availability of an opp |
e1f60b29 NM |
672 | * @dev: device for which we do this operation |
673 | * @freq: OPP frequency to modify availability | |
674 | * @availability_req: availability status requested for this opp | |
675 | * | |
676 | * Set the availability of an OPP with an RCU operation, opp_{enable,disable} | |
677 | * share a common logic which is isolated here. | |
678 | * | |
984f16c8 | 679 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the |
e1f60b29 NM |
680 | * copy operation, returns 0 if no modifcation was done OR modification was |
681 | * successful. | |
682 | * | |
683 | * Locking: The internal device_opp and opp structures are RCU protected. | |
684 | * Hence this function internally uses RCU updater strategy with mutex locks to | |
685 | * keep the integrity of the internal data structures. Callers should ensure | |
686 | * that this function is *NOT* called under RCU protection or in contexts where | |
687 | * mutex locking or synchronize_rcu() blocking calls cannot be used. | |
688 | */ | |
327854c8 NM |
689 | static int _opp_set_availability(struct device *dev, unsigned long freq, |
690 | bool availability_req) | |
e1f60b29 | 691 | { |
29df0ee1 | 692 | struct device_opp *dev_opp; |
47d43ba7 | 693 | struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV); |
e1f60b29 NM |
694 | int r = 0; |
695 | ||
696 | /* keep the node allocated */ | |
47d43ba7 | 697 | new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL); |
e1f60b29 NM |
698 | if (!new_opp) { |
699 | dev_warn(dev, "%s: Unable to create OPP\n", __func__); | |
700 | return -ENOMEM; | |
701 | } | |
702 | ||
703 | mutex_lock(&dev_opp_list_lock); | |
704 | ||
705 | /* Find the device_opp */ | |
327854c8 | 706 | dev_opp = _find_device_opp(dev); |
e1f60b29 NM |
707 | if (IS_ERR(dev_opp)) { |
708 | r = PTR_ERR(dev_opp); | |
709 | dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); | |
710 | goto unlock; | |
711 | } | |
712 | ||
713 | /* Do we have the frequency? */ | |
714 | list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) { | |
715 | if (tmp_opp->rate == freq) { | |
716 | opp = tmp_opp; | |
717 | break; | |
718 | } | |
719 | } | |
720 | if (IS_ERR(opp)) { | |
721 | r = PTR_ERR(opp); | |
722 | goto unlock; | |
723 | } | |
724 | ||
725 | /* Is update really needed? */ | |
726 | if (opp->available == availability_req) | |
727 | goto unlock; | |
728 | /* copy the old data over */ | |
729 | *new_opp = *opp; | |
730 | ||
731 | /* plug in new node */ | |
732 | new_opp->available = availability_req; | |
733 | ||
734 | list_replace_rcu(&opp->node, &new_opp->node); | |
735 | mutex_unlock(&dev_opp_list_lock); | |
327854c8 | 736 | call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu); |
e1f60b29 | 737 | |
03ca370f MH |
738 | /* Notify the change of the OPP availability */ |
739 | if (availability_req) | |
cd1a068a | 740 | srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ENABLE, |
03ca370f MH |
741 | new_opp); |
742 | else | |
cd1a068a | 743 | srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_DISABLE, |
03ca370f MH |
744 | new_opp); |
745 | ||
dde8437d | 746 | return 0; |
e1f60b29 NM |
747 | |
748 | unlock: | |
749 | mutex_unlock(&dev_opp_list_lock); | |
e1f60b29 NM |
750 | kfree(new_opp); |
751 | return r; | |
752 | } | |
753 | ||
754 | /** | |
5d4879cd | 755 | * dev_pm_opp_enable() - Enable a specific OPP |
e1f60b29 NM |
756 | * @dev: device for which we do this operation |
757 | * @freq: OPP frequency to enable | |
758 | * | |
759 | * Enables a provided opp. If the operation is valid, this returns 0, else the | |
760 | * corresponding error value. It is meant to be used for users an OPP available | |
5d4879cd | 761 | * after being temporarily made unavailable with dev_pm_opp_disable. |
e1f60b29 NM |
762 | * |
763 | * Locking: The internal device_opp and opp structures are RCU protected. | |
764 | * Hence this function indirectly uses RCU and mutex locks to keep the | |
765 | * integrity of the internal data structures. Callers should ensure that | |
766 | * this function is *NOT* called under RCU protection or in contexts where | |
767 | * mutex locking or synchronize_rcu() blocking calls cannot be used. | |
984f16c8 NM |
768 | * |
769 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the | |
770 | * copy operation, returns 0 if no modifcation was done OR modification was | |
771 | * successful. | |
e1f60b29 | 772 | */ |
5d4879cd | 773 | int dev_pm_opp_enable(struct device *dev, unsigned long freq) |
e1f60b29 | 774 | { |
327854c8 | 775 | return _opp_set_availability(dev, freq, true); |
e1f60b29 | 776 | } |
5d4879cd | 777 | EXPORT_SYMBOL_GPL(dev_pm_opp_enable); |
e1f60b29 NM |
778 | |
779 | /** | |
5d4879cd | 780 | * dev_pm_opp_disable() - Disable a specific OPP |
e1f60b29 NM |
781 | * @dev: device for which we do this operation |
782 | * @freq: OPP frequency to disable | |
783 | * | |
784 | * Disables a provided opp. If the operation is valid, this returns | |
785 | * 0, else the corresponding error value. It is meant to be a temporary | |
786 | * control by users to make this OPP not available until the circumstances are | |
5d4879cd | 787 | * right to make it available again (with a call to dev_pm_opp_enable). |
e1f60b29 NM |
788 | * |
789 | * Locking: The internal device_opp and opp structures are RCU protected. | |
790 | * Hence this function indirectly uses RCU and mutex locks to keep the | |
791 | * integrity of the internal data structures. Callers should ensure that | |
792 | * this function is *NOT* called under RCU protection or in contexts where | |
793 | * mutex locking or synchronize_rcu() blocking calls cannot be used. | |
984f16c8 NM |
794 | * |
795 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the | |
796 | * copy operation, returns 0 if no modifcation was done OR modification was | |
797 | * successful. | |
e1f60b29 | 798 | */ |
5d4879cd | 799 | int dev_pm_opp_disable(struct device *dev, unsigned long freq) |
e1f60b29 | 800 | { |
327854c8 | 801 | return _opp_set_availability(dev, freq, false); |
e1f60b29 | 802 | } |
5d4879cd | 803 | EXPORT_SYMBOL_GPL(dev_pm_opp_disable); |
e1f60b29 | 804 | |
03ca370f | 805 | /** |
5d4879cd | 806 | * dev_pm_opp_get_notifier() - find notifier_head of the device with opp |
03ca370f | 807 | * @dev: device pointer used to lookup device OPPs. |
984f16c8 NM |
808 | * |
809 | * Return: pointer to notifier head if found, otherwise -ENODEV or | |
810 | * -EINVAL based on type of error casted as pointer. value must be checked | |
811 | * with IS_ERR to determine valid pointer or error result. | |
812 | * | |
813 | * Locking: This function must be called under rcu_read_lock(). dev_opp is a RCU | |
814 | * protected pointer. The reason for the same is that the opp pointer which is | |
815 | * returned will remain valid for use with opp_get_{voltage, freq} only while | |
816 | * under the locked area. The pointer returned must be used prior to unlocking | |
817 | * with rcu_read_unlock() to maintain the integrity of the pointer. | |
03ca370f | 818 | */ |
5d4879cd | 819 | struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev) |
03ca370f | 820 | { |
327854c8 | 821 | struct device_opp *dev_opp = _find_device_opp(dev); |
03ca370f MH |
822 | |
823 | if (IS_ERR(dev_opp)) | |
156acb16 | 824 | return ERR_CAST(dev_opp); /* matching type */ |
03ca370f | 825 | |
cd1a068a | 826 | return &dev_opp->srcu_head; |
03ca370f | 827 | } |
4679ec37 | 828 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier); |
b496dfbc SG |
829 | |
830 | #ifdef CONFIG_OF | |
831 | /** | |
832 | * of_init_opp_table() - Initialize opp table from device tree | |
833 | * @dev: device pointer used to lookup device OPPs. | |
834 | * | |
835 | * Register the initial OPP table with the OPP library for given device. | |
984f16c8 NM |
836 | * |
837 | * Locking: The internal device_opp and opp structures are RCU protected. | |
838 | * Hence this function indirectly uses RCU updater strategy with mutex locks | |
839 | * to keep the integrity of the internal data structures. Callers should ensure | |
840 | * that this function is *NOT* called under RCU protection or in contexts where | |
841 | * mutex cannot be locked. | |
842 | * | |
843 | * Return: | |
844 | * 0 On success OR | |
845 | * Duplicate OPPs (both freq and volt are same) and opp->available | |
846 | * -EEXIST Freq are same and volt are different OR | |
847 | * Duplicate OPPs (both freq and volt are same) and !opp->available | |
848 | * -ENOMEM Memory allocation failure | |
849 | * -ENODEV when 'operating-points' property is not found or is invalid data | |
850 | * in device node. | |
851 | * -ENODATA when empty 'operating-points' property is found | |
b496dfbc SG |
852 | */ |
853 | int of_init_opp_table(struct device *dev) | |
854 | { | |
855 | const struct property *prop; | |
856 | const __be32 *val; | |
857 | int nr; | |
858 | ||
859 | prop = of_find_property(dev->of_node, "operating-points", NULL); | |
860 | if (!prop) | |
861 | return -ENODEV; | |
862 | if (!prop->value) | |
863 | return -ENODATA; | |
864 | ||
865 | /* | |
866 | * Each OPP is a set of tuples consisting of frequency and | |
867 | * voltage like <freq-kHz vol-uV>. | |
868 | */ | |
869 | nr = prop->length / sizeof(u32); | |
870 | if (nr % 2) { | |
871 | dev_err(dev, "%s: Invalid OPP list\n", __func__); | |
872 | return -EINVAL; | |
873 | } | |
874 | ||
875 | val = prop->value; | |
876 | while (nr) { | |
877 | unsigned long freq = be32_to_cpup(val++) * 1000; | |
878 | unsigned long volt = be32_to_cpup(val++); | |
879 | ||
327854c8 | 880 | if (_opp_add_dynamic(dev, freq, volt, false)) |
b496dfbc SG |
881 | dev_warn(dev, "%s: Failed to add OPP %ld\n", |
882 | __func__, freq); | |
b496dfbc SG |
883 | nr -= 2; |
884 | } | |
885 | ||
886 | return 0; | |
887 | } | |
74c46c6e | 888 | EXPORT_SYMBOL_GPL(of_init_opp_table); |
129eec55 VK |
889 | |
890 | /** | |
891 | * of_free_opp_table() - Free OPP table entries created from static DT entries | |
892 | * @dev: device pointer used to lookup device OPPs. | |
893 | * | |
894 | * Free OPPs created using static entries present in DT. | |
984f16c8 NM |
895 | * |
896 | * Locking: The internal device_opp and opp structures are RCU protected. | |
897 | * Hence this function indirectly uses RCU updater strategy with mutex locks | |
898 | * to keep the integrity of the internal data structures. Callers should ensure | |
899 | * that this function is *NOT* called under RCU protection or in contexts where | |
900 | * mutex cannot be locked. | |
129eec55 VK |
901 | */ |
902 | void of_free_opp_table(struct device *dev) | |
903 | { | |
2a6127d0 | 904 | struct device_opp *dev_opp; |
129eec55 VK |
905 | struct dev_pm_opp *opp, *tmp; |
906 | ||
907 | /* Check for existing list for 'dev' */ | |
327854c8 | 908 | dev_opp = _find_device_opp(dev); |
0fe30da2 DT |
909 | if (IS_ERR(dev_opp)) { |
910 | int error = PTR_ERR(dev_opp); | |
911 | if (error != -ENODEV) | |
912 | WARN(1, "%s: dev_opp: %d\n", | |
913 | IS_ERR_OR_NULL(dev) ? | |
914 | "Invalid device" : dev_name(dev), | |
915 | error); | |
129eec55 | 916 | return; |
0fe30da2 | 917 | } |
129eec55 VK |
918 | |
919 | /* Hold our list modification lock here */ | |
920 | mutex_lock(&dev_opp_list_lock); | |
921 | ||
922 | /* Free static OPPs */ | |
923 | list_for_each_entry_safe(opp, tmp, &dev_opp->opp_list, node) { | |
924 | if (!opp->dynamic) | |
327854c8 | 925 | _opp_remove(dev_opp, opp); |
129eec55 VK |
926 | } |
927 | ||
928 | mutex_unlock(&dev_opp_list_lock); | |
929 | } | |
930 | EXPORT_SYMBOL_GPL(of_free_opp_table); | |
b496dfbc | 931 | #endif |