2 * Generic OPP Interface
4 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
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.
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/cpufreq.h>
20 #include <linux/list.h>
21 #include <linux/rculist.h>
22 #include <linux/rcupdate.h>
23 #include <linux/opp.h>
26 * Internal data structure organization with the OPP layer library is as
29 * |- device 1 (represents voltage domain 1)
30 * | |- opp 1 (availability, freq, voltage)
34 * |- device 2 (represents the next voltage domain)
36 * `- device m (represents mth voltage domain)
37 * device 1, 2.. are represented by dev_opp structure while each opp
38 * is represented by the opp structure.
42 * struct opp - Generic OPP description structure
43 * @node: opp list node. The nodes are maintained throughout the lifetime
44 * of boot. It is expected only an optimal set of OPPs are
45 * added to the library by the SoC framework.
46 * RCU usage: opp list is traversed with RCU locks. node
47 * modification is possible realtime, hence the modifications
48 * are protected by the dev_opp_list_lock for integrity.
49 * IMPORTANT: the opp nodes should be maintained in increasing
51 * @available: true/false - marks if this OPP as available or not
52 * @rate: Frequency in hertz
53 * @u_volt: Nominal voltage in microvolts corresponding to this OPP
54 * @dev_opp: points back to the device_opp struct this opp belongs to
56 * This structure stores the OPP information for a given device.
59 struct list_head node
;
65 struct device_opp
*dev_opp
;
69 * struct device_opp - Device opp structure
70 * @node: list node - contains the devices with OPPs that
71 * have been registered. Nodes once added are not modified in this
73 * RCU usage: nodes are not modified in the list of device_opp,
74 * however addition is possible and is secured by dev_opp_list_lock
75 * @dev: device pointer
76 * @head: notifier head to notify the OPP availability changes.
77 * @opp_list: list of opps
79 * This is an internal data structure maintaining the link to opps attached to
80 * a device. This structure is not meant to be shared to users as it is
81 * meant for book keeping and private to OPP library
84 struct list_head node
;
87 struct srcu_notifier_head head
;
88 struct list_head opp_list
;
92 * The root of the list of all devices. All device_opp structures branch off
93 * from here, with each device_opp containing the list of opp it supports in
94 * various states of availability.
96 static LIST_HEAD(dev_opp_list
);
97 /* Lock to allow exclusive modification to the device and opp lists */
98 static DEFINE_MUTEX(dev_opp_list_lock
);
101 * find_device_opp() - find device_opp struct using device pointer
102 * @dev: device pointer used to lookup device OPPs
104 * Search list of device OPPs for one containing matching device. Does a RCU
105 * reader operation to grab the pointer needed.
107 * Returns pointer to 'struct device_opp' if found, otherwise -ENODEV or
108 * -EINVAL based on type of error.
110 * Locking: This function must be called under rcu_read_lock(). device_opp
111 * is a RCU protected pointer. This means that device_opp is valid as long
112 * as we are under RCU lock.
114 static struct device_opp
*find_device_opp(struct device
*dev
)
116 struct device_opp
*tmp_dev_opp
, *dev_opp
= ERR_PTR(-ENODEV
);
118 if (unlikely(IS_ERR_OR_NULL(dev
))) {
119 pr_err("%s: Invalid parameters\n", __func__
);
120 return ERR_PTR(-EINVAL
);
123 list_for_each_entry_rcu(tmp_dev_opp
, &dev_opp_list
, node
) {
124 if (tmp_dev_opp
->dev
== dev
) {
125 dev_opp
= tmp_dev_opp
;
134 * opp_get_voltage() - Gets the voltage corresponding to an available opp
135 * @opp: opp for which voltage has to be returned for
137 * Return voltage in micro volt corresponding to the opp, else
140 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
141 * protected pointer. This means that opp which could have been fetched by
142 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
143 * under RCU lock. The pointer returned by the opp_find_freq family must be
144 * used in the same section as the usage of this function with the pointer
145 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
148 unsigned long opp_get_voltage(struct opp
*opp
)
153 tmp_opp
= rcu_dereference(opp
);
154 if (unlikely(IS_ERR_OR_NULL(tmp_opp
)) || !tmp_opp
->available
)
155 pr_err("%s: Invalid parameters\n", __func__
);
163 * opp_get_freq() - Gets the frequency corresponding to an available opp
164 * @opp: opp for which frequency has to be returned for
166 * Return frequency in hertz corresponding to the opp, else
169 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
170 * protected pointer. This means that opp which could have been fetched by
171 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
172 * under RCU lock. The pointer returned by the opp_find_freq family must be
173 * used in the same section as the usage of this function with the pointer
174 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
177 unsigned long opp_get_freq(struct opp
*opp
)
182 tmp_opp
= rcu_dereference(opp
);
183 if (unlikely(IS_ERR_OR_NULL(tmp_opp
)) || !tmp_opp
->available
)
184 pr_err("%s: Invalid parameters\n", __func__
);
192 * opp_get_opp_count() - Get number of opps available in the opp list
193 * @dev: device for which we do this operation
195 * This function returns the number of available opps if there are any,
196 * else returns 0 if none or the corresponding error value.
198 * Locking: This function must be called under rcu_read_lock(). This function
199 * internally references two RCU protected structures: device_opp and opp which
200 * are safe as long as we are under a common RCU locked section.
202 int opp_get_opp_count(struct device
*dev
)
204 struct device_opp
*dev_opp
;
205 struct opp
*temp_opp
;
208 dev_opp
= find_device_opp(dev
);
209 if (IS_ERR(dev_opp
)) {
210 int r
= PTR_ERR(dev_opp
);
211 dev_err(dev
, "%s: device OPP not found (%d)\n", __func__
, r
);
215 list_for_each_entry_rcu(temp_opp
, &dev_opp
->opp_list
, node
) {
216 if (temp_opp
->available
)
224 * opp_find_freq_exact() - search for an exact frequency
225 * @dev: device for which we do this operation
226 * @freq: frequency to search for
227 * @available: true/false - match for available opp
229 * Searches for exact match in the opp list and returns pointer to the matching
230 * opp if found, else returns ERR_PTR in case of error and should be handled
233 * Note: available is a modifier for the search. if available=true, then the
234 * match is for exact matching frequency and is available in the stored OPP
235 * table. if false, the match is for exact frequency which is not available.
237 * This provides a mechanism to enable an opp which is not available currently
238 * or the opposite as well.
240 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
241 * protected pointer. The reason for the same is that the opp pointer which is
242 * returned will remain valid for use with opp_get_{voltage, freq} only while
243 * under the locked area. The pointer returned must be used prior to unlocking
244 * with rcu_read_unlock() to maintain the integrity of the pointer.
246 struct opp
*opp_find_freq_exact(struct device
*dev
, unsigned long freq
,
249 struct device_opp
*dev_opp
;
250 struct opp
*temp_opp
, *opp
= ERR_PTR(-ENODEV
);
252 dev_opp
= find_device_opp(dev
);
253 if (IS_ERR(dev_opp
)) {
254 int r
= PTR_ERR(dev_opp
);
255 dev_err(dev
, "%s: device OPP not found (%d)\n", __func__
, r
);
259 list_for_each_entry_rcu(temp_opp
, &dev_opp
->opp_list
, node
) {
260 if (temp_opp
->available
== available
&&
261 temp_opp
->rate
== freq
) {
271 * opp_find_freq_ceil() - Search for an rounded ceil freq
272 * @dev: device for which we do this operation
273 * @freq: Start frequency
275 * Search for the matching ceil *available* OPP from a starting freq
278 * Returns matching *opp and refreshes *freq accordingly, else returns
279 * ERR_PTR in case of error and should be handled using IS_ERR.
281 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
282 * protected pointer. The reason for the same is that the opp pointer which is
283 * returned will remain valid for use with opp_get_{voltage, freq} only while
284 * under the locked area. The pointer returned must be used prior to unlocking
285 * with rcu_read_unlock() to maintain the integrity of the pointer.
287 struct opp
*opp_find_freq_ceil(struct device
*dev
, unsigned long *freq
)
289 struct device_opp
*dev_opp
;
290 struct opp
*temp_opp
, *opp
= ERR_PTR(-ENODEV
);
293 dev_err(dev
, "%s: Invalid argument freq=%p\n", __func__
, freq
);
294 return ERR_PTR(-EINVAL
);
297 dev_opp
= find_device_opp(dev
);
301 list_for_each_entry_rcu(temp_opp
, &dev_opp
->opp_list
, node
) {
302 if (temp_opp
->available
&& temp_opp
->rate
>= *freq
) {
313 * opp_find_freq_floor() - Search for a rounded floor freq
314 * @dev: device for which we do this operation
315 * @freq: Start frequency
317 * Search for the matching floor *available* OPP from a starting freq
320 * Returns matching *opp and refreshes *freq accordingly, else returns
321 * ERR_PTR in case of error and should be handled using IS_ERR.
323 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
324 * protected pointer. The reason for the same is that the opp pointer which is
325 * returned will remain valid for use with opp_get_{voltage, freq} only while
326 * under the locked area. The pointer returned must be used prior to unlocking
327 * with rcu_read_unlock() to maintain the integrity of the pointer.
329 struct opp
*opp_find_freq_floor(struct device
*dev
, unsigned long *freq
)
331 struct device_opp
*dev_opp
;
332 struct opp
*temp_opp
, *opp
= ERR_PTR(-ENODEV
);
335 dev_err(dev
, "%s: Invalid argument freq=%p\n", __func__
, freq
);
336 return ERR_PTR(-EINVAL
);
339 dev_opp
= find_device_opp(dev
);
343 list_for_each_entry_rcu(temp_opp
, &dev_opp
->opp_list
, node
) {
344 if (temp_opp
->available
) {
345 /* go to the next node, before choosing prev */
346 if (temp_opp
->rate
> *freq
)
359 * opp_add() - Add an OPP table from a table definitions
360 * @dev: device for which we do this operation
361 * @freq: Frequency in Hz for this OPP
362 * @u_volt: Voltage in uVolts for this OPP
364 * This function adds an opp definition to the opp list and returns status.
365 * The opp is made available by default and it can be controlled using
366 * opp_enable/disable functions.
368 * Locking: The internal device_opp and opp structures are RCU protected.
369 * Hence this function internally uses RCU updater strategy with mutex locks
370 * to keep the integrity of the internal data structures. Callers should ensure
371 * that this function is *NOT* called under RCU protection or in contexts where
372 * mutex cannot be locked.
374 int opp_add(struct device
*dev
, unsigned long freq
, unsigned long u_volt
)
376 struct device_opp
*dev_opp
= NULL
;
377 struct opp
*opp
, *new_opp
;
378 struct list_head
*head
;
380 /* allocate new OPP node */
381 new_opp
= kzalloc(sizeof(struct opp
), GFP_KERNEL
);
383 dev_warn(dev
, "%s: Unable to create new OPP node\n", __func__
);
387 /* Hold our list modification lock here */
388 mutex_lock(&dev_opp_list_lock
);
390 /* Check for existing list for 'dev' */
391 dev_opp
= find_device_opp(dev
);
392 if (IS_ERR(dev_opp
)) {
394 * Allocate a new device OPP table. In the infrequent case
395 * where a new device is needed to be added, we pay this
398 dev_opp
= kzalloc(sizeof(struct device_opp
), GFP_KERNEL
);
400 mutex_unlock(&dev_opp_list_lock
);
403 "%s: Unable to create device OPP structure\n",
409 srcu_init_notifier_head(&dev_opp
->head
);
410 INIT_LIST_HEAD(&dev_opp
->opp_list
);
412 /* Secure the device list modification */
413 list_add_rcu(&dev_opp
->node
, &dev_opp_list
);
416 /* populate the opp table */
417 new_opp
->dev_opp
= dev_opp
;
418 new_opp
->rate
= freq
;
419 new_opp
->u_volt
= u_volt
;
420 new_opp
->available
= true;
422 /* Insert new OPP in order of increasing frequency */
423 head
= &dev_opp
->opp_list
;
424 list_for_each_entry_rcu(opp
, &dev_opp
->opp_list
, node
) {
425 if (new_opp
->rate
< opp
->rate
)
431 list_add_rcu(&new_opp
->node
, head
);
432 mutex_unlock(&dev_opp_list_lock
);
435 * Notify the changes in the availability of the operable
436 * frequency/voltage list.
438 srcu_notifier_call_chain(&dev_opp
->head
, OPP_EVENT_ADD
, new_opp
);
443 * opp_set_availability() - helper to set the availability of an opp
444 * @dev: device for which we do this operation
445 * @freq: OPP frequency to modify availability
446 * @availability_req: availability status requested for this opp
448 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
449 * share a common logic which is isolated here.
451 * Returns -EINVAL for bad pointers, -ENOMEM if no memory available for the
452 * copy operation, returns 0 if no modifcation was done OR modification was
455 * Locking: The internal device_opp and opp structures are RCU protected.
456 * Hence this function internally uses RCU updater strategy with mutex locks to
457 * keep the integrity of the internal data structures. Callers should ensure
458 * that this function is *NOT* called under RCU protection or in contexts where
459 * mutex locking or synchronize_rcu() blocking calls cannot be used.
461 static int opp_set_availability(struct device
*dev
, unsigned long freq
,
462 bool availability_req
)
464 struct device_opp
*tmp_dev_opp
, *dev_opp
= ERR_PTR(-ENODEV
);
465 struct opp
*new_opp
, *tmp_opp
, *opp
= ERR_PTR(-ENODEV
);
468 /* keep the node allocated */
469 new_opp
= kmalloc(sizeof(struct opp
), GFP_KERNEL
);
471 dev_warn(dev
, "%s: Unable to create OPP\n", __func__
);
475 mutex_lock(&dev_opp_list_lock
);
477 /* Find the device_opp */
478 list_for_each_entry(tmp_dev_opp
, &dev_opp_list
, node
) {
479 if (dev
== tmp_dev_opp
->dev
) {
480 dev_opp
= tmp_dev_opp
;
484 if (IS_ERR(dev_opp
)) {
485 r
= PTR_ERR(dev_opp
);
486 dev_warn(dev
, "%s: Device OPP not found (%d)\n", __func__
, r
);
490 /* Do we have the frequency? */
491 list_for_each_entry(tmp_opp
, &dev_opp
->opp_list
, node
) {
492 if (tmp_opp
->rate
== freq
) {
502 /* Is update really needed? */
503 if (opp
->available
== availability_req
)
505 /* copy the old data over */
508 /* plug in new node */
509 new_opp
->available
= availability_req
;
511 list_replace_rcu(&opp
->node
, &new_opp
->node
);
512 mutex_unlock(&dev_opp_list_lock
);
515 /* Notify the change of the OPP availability */
516 if (availability_req
)
517 srcu_notifier_call_chain(&dev_opp
->head
, OPP_EVENT_ENABLE
,
520 srcu_notifier_call_chain(&dev_opp
->head
, OPP_EVENT_DISABLE
,
523 /* clean up old opp */
528 mutex_unlock(&dev_opp_list_lock
);
535 * opp_enable() - Enable a specific OPP
536 * @dev: device for which we do this operation
537 * @freq: OPP frequency to enable
539 * Enables a provided opp. If the operation is valid, this returns 0, else the
540 * corresponding error value. It is meant to be used for users an OPP available
541 * after being temporarily made unavailable with opp_disable.
543 * Locking: The internal device_opp and opp structures are RCU protected.
544 * Hence this function indirectly uses RCU and mutex locks to keep the
545 * integrity of the internal data structures. Callers should ensure that
546 * this function is *NOT* called under RCU protection or in contexts where
547 * mutex locking or synchronize_rcu() blocking calls cannot be used.
549 int opp_enable(struct device
*dev
, unsigned long freq
)
551 return opp_set_availability(dev
, freq
, true);
555 * opp_disable() - Disable a specific OPP
556 * @dev: device for which we do this operation
557 * @freq: OPP frequency to disable
559 * Disables a provided opp. If the operation is valid, this returns
560 * 0, else the corresponding error value. It is meant to be a temporary
561 * control by users to make this OPP not available until the circumstances are
562 * right to make it available again (with a call to opp_enable).
564 * Locking: The internal device_opp and opp structures are RCU protected.
565 * Hence this function indirectly uses RCU and mutex locks to keep the
566 * integrity of the internal data structures. Callers should ensure that
567 * this function is *NOT* called under RCU protection or in contexts where
568 * mutex locking or synchronize_rcu() blocking calls cannot be used.
570 int opp_disable(struct device
*dev
, unsigned long freq
)
572 return opp_set_availability(dev
, freq
, false);
575 #ifdef CONFIG_CPU_FREQ
577 * opp_init_cpufreq_table() - create a cpufreq table for a device
578 * @dev: device for which we do this operation
579 * @table: Cpufreq table returned back to caller
581 * Generate a cpufreq table for a provided device- this assumes that the
582 * opp list is already initialized and ready for usage.
584 * This function allocates required memory for the cpufreq table. It is
585 * expected that the caller does the required maintenance such as freeing
586 * the table as required.
588 * Returns -EINVAL for bad pointers, -ENODEV if the device is not found, -ENOMEM
589 * if no memory available for the operation (table is not populated), returns 0
590 * if successful and table is populated.
592 * WARNING: It is important for the callers to ensure refreshing their copy of
593 * the table if any of the mentioned functions have been invoked in the interim.
595 * Locking: The internal device_opp and opp structures are RCU protected.
596 * To simplify the logic, we pretend we are updater and hold relevant mutex here
597 * Callers should ensure that this function is *NOT* called under RCU protection
598 * or in contexts where mutex locking cannot be used.
600 int opp_init_cpufreq_table(struct device
*dev
,
601 struct cpufreq_frequency_table
**table
)
603 struct device_opp
*dev_opp
;
605 struct cpufreq_frequency_table
*freq_table
;
608 /* Pretend as if I am an updater */
609 mutex_lock(&dev_opp_list_lock
);
611 dev_opp
= find_device_opp(dev
);
612 if (IS_ERR(dev_opp
)) {
613 int r
= PTR_ERR(dev_opp
);
614 mutex_unlock(&dev_opp_list_lock
);
615 dev_err(dev
, "%s: Device OPP not found (%d)\n", __func__
, r
);
619 freq_table
= kzalloc(sizeof(struct cpufreq_frequency_table
) *
620 (opp_get_opp_count(dev
) + 1), GFP_KERNEL
);
622 mutex_unlock(&dev_opp_list_lock
);
623 dev_warn(dev
, "%s: Unable to allocate frequency table\n",
628 list_for_each_entry(opp
, &dev_opp
->opp_list
, node
) {
629 if (opp
->available
) {
630 freq_table
[i
].index
= i
;
631 freq_table
[i
].frequency
= opp
->rate
/ 1000;
635 mutex_unlock(&dev_opp_list_lock
);
637 freq_table
[i
].index
= i
;
638 freq_table
[i
].frequency
= CPUFREQ_TABLE_END
;
640 *table
= &freq_table
[0];
646 * opp_free_cpufreq_table() - free the cpufreq table
647 * @dev: device for which we do this operation
648 * @table: table to free
650 * Free up the table allocated by opp_init_cpufreq_table
652 void opp_free_cpufreq_table(struct device
*dev
,
653 struct cpufreq_frequency_table
**table
)
661 #endif /* CONFIG_CPU_FREQ */
664 * opp_get_notifier() - find notifier_head of the device with opp
665 * @dev: device pointer used to lookup device OPPs.
667 struct srcu_notifier_head
*opp_get_notifier(struct device
*dev
)
669 struct device_opp
*dev_opp
= find_device_opp(dev
);
672 return ERR_PTR(PTR_ERR(dev_opp
)); /* matching type */
674 return &dev_opp
->head
;