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