]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/base/power/opp.c
PM / OPP: handle allocation of device_opp in a separate routine
[mirror_ubuntu-jammy-kernel.git] / drivers / base / power / opp.c
CommitLineData
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 61struct 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 */
93struct 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 */
107static LIST_HEAD(dev_opp_list);
108/* Lock to allow exclusive modification to the device and opp lists */
109static DEFINE_MUTEX(dev_opp_list_lock);
110
111/**
112 * find_device_opp() - find device_opp struct using device pointer
113 * @dev: device pointer used to lookup device OPPs
114 *
115 * Search list of device OPPs for one containing matching device. Does a RCU
116 * reader operation to grab the pointer needed.
117 *
118 * Returns pointer to 'struct device_opp' if found, otherwise -ENODEV or
119 * -EINVAL based on type of error.
120 *
121 * Locking: This function must be called under rcu_read_lock(). device_opp
122 * is a RCU protected pointer. This means that device_opp is valid as long
123 * as we are under RCU lock.
124 */
125static struct device_opp *find_device_opp(struct device *dev)
126{
127 struct device_opp *tmp_dev_opp, *dev_opp = ERR_PTR(-ENODEV);
128
129 if (unlikely(IS_ERR_OR_NULL(dev))) {
130 pr_err("%s: Invalid parameters\n", __func__);
131 return ERR_PTR(-EINVAL);
132 }
133
134 list_for_each_entry_rcu(tmp_dev_opp, &dev_opp_list, node) {
135 if (tmp_dev_opp->dev == dev) {
136 dev_opp = tmp_dev_opp;
137 break;
138 }
139 }
140
141 return dev_opp;
142}
143
144/**
5d4879cd 145 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an available opp
e1f60b29
NM
146 * @opp: opp for which voltage has to be returned for
147 *
148 * Return voltage in micro volt corresponding to the opp, else
149 * return 0
150 *
151 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
152 * protected pointer. This means that opp which could have been fetched by
153 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
154 * under RCU lock. The pointer returned by the opp_find_freq family must be
155 * used in the same section as the usage of this function with the pointer
156 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
157 * pointer.
158 */
47d43ba7 159unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
e1f60b29 160{
47d43ba7 161 struct dev_pm_opp *tmp_opp;
e1f60b29
NM
162 unsigned long v = 0;
163
164 tmp_opp = rcu_dereference(opp);
165 if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
166 pr_err("%s: Invalid parameters\n", __func__);
167 else
168 v = tmp_opp->u_volt;
169
170 return v;
171}
5d4879cd 172EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
e1f60b29
NM
173
174/**
5d4879cd 175 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
e1f60b29
NM
176 * @opp: opp for which frequency has to be returned for
177 *
178 * Return frequency in hertz corresponding to the opp, else
179 * return 0
180 *
181 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
182 * protected pointer. This means that opp which could have been fetched by
183 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
184 * under RCU lock. The pointer returned by the opp_find_freq family must be
185 * used in the same section as the usage of this function with the pointer
186 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
187 * pointer.
188 */
47d43ba7 189unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
e1f60b29 190{
47d43ba7 191 struct dev_pm_opp *tmp_opp;
e1f60b29
NM
192 unsigned long f = 0;
193
194 tmp_opp = rcu_dereference(opp);
195 if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
196 pr_err("%s: Invalid parameters\n", __func__);
197 else
198 f = tmp_opp->rate;
199
200 return f;
201}
5d4879cd 202EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
e1f60b29
NM
203
204/**
5d4879cd 205 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp list
e1f60b29
NM
206 * @dev: device for which we do this operation
207 *
208 * This function returns the number of available opps if there are any,
209 * else returns 0 if none or the corresponding error value.
210 *
211 * Locking: This function must be called under rcu_read_lock(). This function
212 * internally references two RCU protected structures: device_opp and opp which
213 * are safe as long as we are under a common RCU locked section.
214 */
5d4879cd 215int dev_pm_opp_get_opp_count(struct device *dev)
e1f60b29
NM
216{
217 struct device_opp *dev_opp;
47d43ba7 218 struct dev_pm_opp *temp_opp;
e1f60b29
NM
219 int count = 0;
220
221 dev_opp = find_device_opp(dev);
222 if (IS_ERR(dev_opp)) {
223 int r = PTR_ERR(dev_opp);
224 dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
225 return r;
226 }
227
228 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
229 if (temp_opp->available)
230 count++;
231 }
232
233 return count;
234}
5d4879cd 235EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
e1f60b29
NM
236
237/**
5d4879cd 238 * dev_pm_opp_find_freq_exact() - search for an exact frequency
e1f60b29
NM
239 * @dev: device for which we do this operation
240 * @freq: frequency to search for
7ae49618 241 * @available: true/false - match for available opp
e1f60b29
NM
242 *
243 * Searches for exact match in the opp list and returns pointer to the matching
244 * opp if found, else returns ERR_PTR in case of error and should be handled
0779726c
NM
245 * using IS_ERR. Error return values can be:
246 * EINVAL: for bad pointer
247 * ERANGE: no match found for search
248 * ENODEV: if device not found in list of registered devices
e1f60b29
NM
249 *
250 * Note: available is a modifier for the search. if available=true, then the
251 * match is for exact matching frequency and is available in the stored OPP
252 * table. if false, the match is for exact frequency which is not available.
253 *
254 * This provides a mechanism to enable an opp which is not available currently
255 * or the opposite as well.
256 *
257 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
258 * protected pointer. The reason for the same is that the opp pointer which is
259 * returned will remain valid for use with opp_get_{voltage, freq} only while
260 * under the locked area. The pointer returned must be used prior to unlocking
261 * with rcu_read_unlock() to maintain the integrity of the pointer.
262 */
47d43ba7
NM
263struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
264 unsigned long freq,
265 bool available)
e1f60b29
NM
266{
267 struct device_opp *dev_opp;
47d43ba7 268 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
e1f60b29
NM
269
270 dev_opp = find_device_opp(dev);
271 if (IS_ERR(dev_opp)) {
272 int r = PTR_ERR(dev_opp);
273 dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
274 return ERR_PTR(r);
275 }
276
277 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
278 if (temp_opp->available == available &&
279 temp_opp->rate == freq) {
280 opp = temp_opp;
281 break;
282 }
283 }
284
285 return opp;
286}
5d4879cd 287EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
e1f60b29
NM
288
289/**
5d4879cd 290 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
e1f60b29
NM
291 * @dev: device for which we do this operation
292 * @freq: Start frequency
293 *
294 * Search for the matching ceil *available* OPP from a starting freq
295 * for a device.
296 *
297 * Returns matching *opp and refreshes *freq accordingly, else returns
0779726c
NM
298 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
299 * values can be:
300 * EINVAL: for bad pointer
301 * ERANGE: no match found for search
302 * ENODEV: if device not found in list of registered devices
e1f60b29
NM
303 *
304 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
305 * protected pointer. The reason for the same is that the opp pointer which is
306 * returned will remain valid for use with opp_get_{voltage, freq} only while
307 * under the locked area. The pointer returned must be used prior to unlocking
308 * with rcu_read_unlock() to maintain the integrity of the pointer.
309 */
47d43ba7
NM
310struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
311 unsigned long *freq)
e1f60b29
NM
312{
313 struct device_opp *dev_opp;
47d43ba7 314 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
e1f60b29
NM
315
316 if (!dev || !freq) {
317 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
318 return ERR_PTR(-EINVAL);
319 }
320
321 dev_opp = find_device_opp(dev);
322 if (IS_ERR(dev_opp))
0779726c 323 return ERR_CAST(dev_opp);
e1f60b29
NM
324
325 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
326 if (temp_opp->available && temp_opp->rate >= *freq) {
327 opp = temp_opp;
328 *freq = opp->rate;
329 break;
330 }
331 }
332
333 return opp;
334}
5d4879cd 335EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
e1f60b29
NM
336
337/**
5d4879cd 338 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
e1f60b29
NM
339 * @dev: device for which we do this operation
340 * @freq: Start frequency
341 *
342 * Search for the matching floor *available* OPP from a starting freq
343 * for a device.
344 *
345 * Returns matching *opp and refreshes *freq accordingly, else returns
0779726c
NM
346 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
347 * values can be:
348 * EINVAL: for bad pointer
349 * ERANGE: no match found for search
350 * ENODEV: if device not found in list of registered devices
e1f60b29
NM
351 *
352 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
353 * protected pointer. The reason for the same is that the opp pointer which is
354 * returned will remain valid for use with opp_get_{voltage, freq} only while
355 * under the locked area. The pointer returned must be used prior to unlocking
356 * with rcu_read_unlock() to maintain the integrity of the pointer.
357 */
47d43ba7
NM
358struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
359 unsigned long *freq)
e1f60b29
NM
360{
361 struct device_opp *dev_opp;
47d43ba7 362 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
e1f60b29
NM
363
364 if (!dev || !freq) {
365 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
366 return ERR_PTR(-EINVAL);
367 }
368
369 dev_opp = find_device_opp(dev);
370 if (IS_ERR(dev_opp))
0779726c 371 return ERR_CAST(dev_opp);
e1f60b29
NM
372
373 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
374 if (temp_opp->available) {
375 /* go to the next node, before choosing prev */
376 if (temp_opp->rate > *freq)
377 break;
378 else
379 opp = temp_opp;
380 }
381 }
382 if (!IS_ERR(opp))
383 *freq = opp->rate;
384
385 return opp;
386}
5d4879cd 387EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
e1f60b29 388
07cce74a
VK
389static struct device_opp *add_device_opp(struct device *dev)
390{
391 struct device_opp *dev_opp;
392
393 /*
394 * Allocate a new device OPP table. In the infrequent case where a new
395 * device is needed to be added, we pay this penalty.
396 */
397 dev_opp = kzalloc(sizeof(*dev_opp), GFP_KERNEL);
398 if (!dev_opp)
399 return NULL;
400
401 dev_opp->dev = dev;
402 srcu_init_notifier_head(&dev_opp->srcu_head);
403 INIT_LIST_HEAD(&dev_opp->opp_list);
404
405 /* Secure the device list modification */
406 list_add_rcu(&dev_opp->node, &dev_opp_list);
407 return dev_opp;
408}
409
38393409
VK
410static int dev_pm_opp_add_dynamic(struct device *dev, unsigned long freq,
411 unsigned long u_volt, bool dynamic)
e1f60b29
NM
412{
413 struct device_opp *dev_opp = NULL;
47d43ba7 414 struct dev_pm_opp *opp, *new_opp;
e1f60b29
NM
415 struct list_head *head;
416
417 /* allocate new OPP node */
47d43ba7 418 new_opp = kzalloc(sizeof(*new_opp), GFP_KERNEL);
e1f60b29
NM
419 if (!new_opp) {
420 dev_warn(dev, "%s: Unable to create new OPP node\n", __func__);
421 return -ENOMEM;
422 }
423
424 /* Hold our list modification lock here */
425 mutex_lock(&dev_opp_list_lock);
426
a7470db6 427 /* populate the opp table */
a7470db6
VK
428 new_opp->rate = freq;
429 new_opp->u_volt = u_volt;
430 new_opp->available = true;
38393409 431 new_opp->dynamic = dynamic;
a7470db6 432
e1f60b29
NM
433 /* Check for existing list for 'dev' */
434 dev_opp = find_device_opp(dev);
435 if (IS_ERR(dev_opp)) {
07cce74a 436 dev_opp = add_device_opp(dev);
e1f60b29
NM
437 if (!dev_opp) {
438 mutex_unlock(&dev_opp_list_lock);
439 kfree(new_opp);
e1f60b29
NM
440 return -ENOMEM;
441 }
442
a7470db6
VK
443 head = &dev_opp->opp_list;
444 goto list_add;
e1f60b29
NM
445 }
446
64ce8545
CK
447 /*
448 * Insert new OPP in order of increasing frequency
449 * and discard if already present
450 */
e1f60b29
NM
451 head = &dev_opp->opp_list;
452 list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
64ce8545 453 if (new_opp->rate <= opp->rate)
e1f60b29
NM
454 break;
455 else
456 head = &opp->node;
457 }
458
64ce8545
CK
459 /* Duplicate OPPs ? */
460 if (new_opp->rate == opp->rate) {
461 int ret = opp->available && new_opp->u_volt == opp->u_volt ?
462 0 : -EEXIST;
463
464 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
465 __func__, opp->rate, opp->u_volt, opp->available,
466 new_opp->rate, new_opp->u_volt, new_opp->available);
467 mutex_unlock(&dev_opp_list_lock);
468 kfree(new_opp);
469 return ret;
470 }
471
a7470db6 472list_add:
7284a00f 473 new_opp->dev_opp = dev_opp;
e1f60b29
NM
474 list_add_rcu(&new_opp->node, head);
475 mutex_unlock(&dev_opp_list_lock);
476
03ca370f
MH
477 /*
478 * Notify the changes in the availability of the operable
479 * frequency/voltage list.
480 */
cd1a068a 481 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
e1f60b29
NM
482 return 0;
483}
38393409
VK
484
485/**
486 * dev_pm_opp_add() - Add an OPP table from a table definitions
487 * @dev: device for which we do this operation
488 * @freq: Frequency in Hz for this OPP
489 * @u_volt: Voltage in uVolts for this OPP
490 *
491 * This function adds an opp definition to the opp list and returns status.
492 * The opp is made available by default and it can be controlled using
493 * dev_pm_opp_enable/disable functions.
494 *
495 * Locking: The internal device_opp and opp structures are RCU protected.
496 * Hence this function internally uses RCU updater strategy with mutex locks
497 * to keep the integrity of the internal data structures. Callers should ensure
498 * that this function is *NOT* called under RCU protection or in contexts where
499 * mutex cannot be locked.
500 *
501 * Return:
502 * 0: On success OR
503 * Duplicate OPPs (both freq and volt are same) and opp->available
504 * -EEXIST: Freq are same and volt are different OR
505 * Duplicate OPPs (both freq and volt are same) and !opp->available
506 * -ENOMEM: Memory allocation failure
507 */
508int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
509{
510 return dev_pm_opp_add_dynamic(dev, freq, u_volt, true);
511}
5d4879cd 512EXPORT_SYMBOL_GPL(dev_pm_opp_add);
e1f60b29 513
129eec55
VK
514static void kfree_opp_rcu(struct rcu_head *head)
515{
516 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
517
518 kfree_rcu(opp, rcu_head);
519}
520
521static void kfree_device_rcu(struct rcu_head *head)
522{
523 struct device_opp *device_opp = container_of(head, struct device_opp, rcu_head);
524
1c6a662f 525 kfree_rcu(device_opp, rcu_head);
129eec55
VK
526}
527
86453b47
VK
528static void __dev_pm_opp_remove(struct device_opp *dev_opp,
529 struct dev_pm_opp *opp)
129eec55
VK
530{
531 /*
532 * Notify the changes in the availability of the operable
533 * frequency/voltage list.
534 */
535 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_REMOVE, opp);
536 list_del_rcu(&opp->node);
537 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, kfree_opp_rcu);
538
539 if (list_empty(&dev_opp->opp_list)) {
540 list_del_rcu(&dev_opp->node);
541 call_srcu(&dev_opp->srcu_head.srcu, &dev_opp->rcu_head,
542 kfree_device_rcu);
543 }
544}
545
546/**
547 * dev_pm_opp_remove() - Remove an OPP from OPP list
548 * @dev: device for which we do this operation
549 * @freq: OPP to remove with matching 'freq'
550 *
551 * This function removes an opp from the opp list.
552 */
553void dev_pm_opp_remove(struct device *dev, unsigned long freq)
554{
555 struct dev_pm_opp *opp;
556 struct device_opp *dev_opp;
557 bool found = false;
558
559 /* Hold our list modification lock here */
560 mutex_lock(&dev_opp_list_lock);
561
562 dev_opp = find_device_opp(dev);
563 if (IS_ERR(dev_opp))
564 goto unlock;
565
566 list_for_each_entry(opp, &dev_opp->opp_list, node) {
567 if (opp->rate == freq) {
568 found = true;
569 break;
570 }
571 }
572
573 if (!found) {
574 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
575 __func__, freq);
576 goto unlock;
577 }
578
579 __dev_pm_opp_remove(dev_opp, opp);
580unlock:
581 mutex_unlock(&dev_opp_list_lock);
582}
583EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
584
e1f60b29
NM
585/**
586 * opp_set_availability() - helper to set the availability of an opp
587 * @dev: device for which we do this operation
588 * @freq: OPP frequency to modify availability
589 * @availability_req: availability status requested for this opp
590 *
591 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
592 * share a common logic which is isolated here.
593 *
594 * Returns -EINVAL for bad pointers, -ENOMEM if no memory available for the
595 * copy operation, returns 0 if no modifcation was done OR modification was
596 * successful.
597 *
598 * Locking: The internal device_opp and opp structures are RCU protected.
599 * Hence this function internally uses RCU updater strategy with mutex locks to
600 * keep the integrity of the internal data structures. Callers should ensure
601 * that this function is *NOT* called under RCU protection or in contexts where
602 * mutex locking or synchronize_rcu() blocking calls cannot be used.
603 */
604static int opp_set_availability(struct device *dev, unsigned long freq,
605 bool availability_req)
606{
29df0ee1 607 struct device_opp *dev_opp;
47d43ba7 608 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
e1f60b29
NM
609 int r = 0;
610
611 /* keep the node allocated */
47d43ba7 612 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
e1f60b29
NM
613 if (!new_opp) {
614 dev_warn(dev, "%s: Unable to create OPP\n", __func__);
615 return -ENOMEM;
616 }
617
618 mutex_lock(&dev_opp_list_lock);
619
620 /* Find the device_opp */
29df0ee1 621 dev_opp = find_device_opp(dev);
e1f60b29
NM
622 if (IS_ERR(dev_opp)) {
623 r = PTR_ERR(dev_opp);
624 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
625 goto unlock;
626 }
627
628 /* Do we have the frequency? */
629 list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) {
630 if (tmp_opp->rate == freq) {
631 opp = tmp_opp;
632 break;
633 }
634 }
635 if (IS_ERR(opp)) {
636 r = PTR_ERR(opp);
637 goto unlock;
638 }
639
640 /* Is update really needed? */
641 if (opp->available == availability_req)
642 goto unlock;
643 /* copy the old data over */
644 *new_opp = *opp;
645
646 /* plug in new node */
647 new_opp->available = availability_req;
648
649 list_replace_rcu(&opp->node, &new_opp->node);
650 mutex_unlock(&dev_opp_list_lock);
b4037aaa 651 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, kfree_opp_rcu);
e1f60b29 652
03ca370f
MH
653 /* Notify the change of the OPP availability */
654 if (availability_req)
cd1a068a 655 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ENABLE,
03ca370f
MH
656 new_opp);
657 else
cd1a068a 658 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_DISABLE,
03ca370f
MH
659 new_opp);
660
dde8437d 661 return 0;
e1f60b29
NM
662
663unlock:
664 mutex_unlock(&dev_opp_list_lock);
e1f60b29
NM
665 kfree(new_opp);
666 return r;
667}
668
669/**
5d4879cd 670 * dev_pm_opp_enable() - Enable a specific OPP
e1f60b29
NM
671 * @dev: device for which we do this operation
672 * @freq: OPP frequency to enable
673 *
674 * Enables a provided opp. If the operation is valid, this returns 0, else the
675 * corresponding error value. It is meant to be used for users an OPP available
5d4879cd 676 * after being temporarily made unavailable with dev_pm_opp_disable.
e1f60b29
NM
677 *
678 * Locking: The internal device_opp and opp structures are RCU protected.
679 * Hence this function indirectly uses RCU and mutex locks to keep the
680 * integrity of the internal data structures. Callers should ensure that
681 * this function is *NOT* called under RCU protection or in contexts where
682 * mutex locking or synchronize_rcu() blocking calls cannot be used.
683 */
5d4879cd 684int dev_pm_opp_enable(struct device *dev, unsigned long freq)
e1f60b29
NM
685{
686 return opp_set_availability(dev, freq, true);
687}
5d4879cd 688EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
e1f60b29
NM
689
690/**
5d4879cd 691 * dev_pm_opp_disable() - Disable a specific OPP
e1f60b29
NM
692 * @dev: device for which we do this operation
693 * @freq: OPP frequency to disable
694 *
695 * Disables a provided opp. If the operation is valid, this returns
696 * 0, else the corresponding error value. It is meant to be a temporary
697 * control by users to make this OPP not available until the circumstances are
5d4879cd 698 * right to make it available again (with a call to dev_pm_opp_enable).
e1f60b29
NM
699 *
700 * Locking: The internal device_opp and opp structures are RCU protected.
701 * Hence this function indirectly uses RCU and mutex locks to keep the
702 * integrity of the internal data structures. Callers should ensure that
703 * this function is *NOT* called under RCU protection or in contexts where
704 * mutex locking or synchronize_rcu() blocking calls cannot be used.
705 */
5d4879cd 706int dev_pm_opp_disable(struct device *dev, unsigned long freq)
e1f60b29
NM
707{
708 return opp_set_availability(dev, freq, false);
709}
5d4879cd 710EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
e1f60b29 711
03ca370f 712/**
5d4879cd 713 * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
03ca370f
MH
714 * @dev: device pointer used to lookup device OPPs.
715 */
5d4879cd 716struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
03ca370f
MH
717{
718 struct device_opp *dev_opp = find_device_opp(dev);
719
720 if (IS_ERR(dev_opp))
156acb16 721 return ERR_CAST(dev_opp); /* matching type */
03ca370f 722
cd1a068a 723 return &dev_opp->srcu_head;
03ca370f 724}
b496dfbc
SG
725
726#ifdef CONFIG_OF
727/**
728 * of_init_opp_table() - Initialize opp table from device tree
729 * @dev: device pointer used to lookup device OPPs.
730 *
731 * Register the initial OPP table with the OPP library for given device.
732 */
733int of_init_opp_table(struct device *dev)
734{
735 const struct property *prop;
736 const __be32 *val;
737 int nr;
738
739 prop = of_find_property(dev->of_node, "operating-points", NULL);
740 if (!prop)
741 return -ENODEV;
742 if (!prop->value)
743 return -ENODATA;
744
745 /*
746 * Each OPP is a set of tuples consisting of frequency and
747 * voltage like <freq-kHz vol-uV>.
748 */
749 nr = prop->length / sizeof(u32);
750 if (nr % 2) {
751 dev_err(dev, "%s: Invalid OPP list\n", __func__);
752 return -EINVAL;
753 }
754
755 val = prop->value;
756 while (nr) {
757 unsigned long freq = be32_to_cpup(val++) * 1000;
758 unsigned long volt = be32_to_cpup(val++);
759
38393409 760 if (dev_pm_opp_add_dynamic(dev, freq, volt, false))
b496dfbc
SG
761 dev_warn(dev, "%s: Failed to add OPP %ld\n",
762 __func__, freq);
b496dfbc
SG
763 nr -= 2;
764 }
765
766 return 0;
767}
74c46c6e 768EXPORT_SYMBOL_GPL(of_init_opp_table);
129eec55
VK
769
770/**
771 * of_free_opp_table() - Free OPP table entries created from static DT entries
772 * @dev: device pointer used to lookup device OPPs.
773 *
774 * Free OPPs created using static entries present in DT.
775 */
776void of_free_opp_table(struct device *dev)
777{
2a6127d0 778 struct device_opp *dev_opp;
129eec55
VK
779 struct dev_pm_opp *opp, *tmp;
780
781 /* Check for existing list for 'dev' */
782 dev_opp = find_device_opp(dev);
783 if (WARN(IS_ERR(dev_opp), "%s: dev_opp: %ld\n", dev_name(dev),
784 PTR_ERR(dev_opp)))
785 return;
786
787 /* Hold our list modification lock here */
788 mutex_lock(&dev_opp_list_lock);
789
790 /* Free static OPPs */
791 list_for_each_entry_safe(opp, tmp, &dev_opp->opp_list, node) {
792 if (!opp->dynamic)
793 __dev_pm_opp_remove(dev_opp, opp);
794 }
795
796 mutex_unlock(&dev_opp_list_lock);
797}
798EXPORT_SYMBOL_GPL(of_free_opp_table);
b496dfbc 799#endif