Skip to contentSkip to conversation

opp: introduce library for device-specific OPPs

PATCH In review Nishanth Menon 8 participants 61 messages Sep 17, 2010 – Sep 20, 2010 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [PATCH 1/4] OMAP: introduce OPP layer for device-specific OPPs

#1 lore raw

PATCH 1/1 opp: introduce library for device-specific OPPs

SOCs have a standard set of tuples consisting of frequency and voltage pairs that the device will support per voltage domain. These are called Operating Performance Points or OPPs. The actual definitions of Operating Performance Points varies over silicon within the same family of devices. For a specific domain, you can have a set of {frequency, voltage} pairs. As the kernel boots and more information is available, a set of these are activated based on the precise nature of device the kernel boots up on. It is interesting to remember that each IP which belongs to a voltage domain may define their own set of OPPs on top of this.

To implement an OPP, some sort of power management support is necessary hence this library enablement depends on CONFIG_PM, however this does not fit into the core power framework as it is an independent library. This is hence introduced under lib allowing all architectures to selectively enable the feature based on thier capabilities.

Contributions include:
Sanjeev Premi for the initial concept:

	http://patchwork.kernel.org/patch/50998/

Kevin Hilman for converting original design to device-based Kevin Hilman and Paul Walmsey for cleaning up many of the function abstractions, improvements and data structure handling Romit Dasgupta for using enums instead of opp pointers Thara Gopinath, Eduardo Valentin and Vishwanath BS for fixes and cleanups.
Linus Walleij for recommending this layer be made generic for usage in other architectures beyond OMAP and ARM.

Discussions and comments from:
http://marc.info/?l=linux-omap&m=126033945313269&w=2
http://marc.info/?l=linux-omap&m=125482970102327&w=2
http://marc.info/?t=125809247500002&r=1&w=2
http://marc.info/?l=linux-omap&m=126025973426007&w=2
http://marc.info/?t=128152609200064&r=1&w=2
incorporated.

Cc Benoit Cousson <[email protected]>
Cc Madhusudhan Chikkature Rajashekar <[email protected]>
Cc Phil Carmody <[email protected]>
Cc Roberto Granados Dorado <[email protected]>
Cc Santosh Shilimkar <[email protected]>
Cc Sergio Alberto Aguirre Rodriguez <[email protected]>
Cc Tero Kristo <[email protected]>
Cc Eduardo Valentin <[email protected]>
Cc Paul Walmsley <[email protected]>
Cc Romit Dasgupta <[email protected]>
Cc Sanjeev Premi <[email protected]>
Cc Thara Gopinath <[email protected]>
Cc Vishwanath BS <[email protected]>
Cc Linus Walleij <[email protected]>

---
minor notes:
a) Code rebased to linus's tree commit 03a7ab0
b) Looping in get-maintainter.pl list and lkml as per thread of discussion
http://marc.info/?t=128152609200064&r=1&w=2

 Documentation/power/00-INDEX |    2 +
 include/linux/opp.h          |  136 +++++++++++++
 kernel/power/Kconfig         |   14 ++
 lib/Makefile                 |    2 +
 lib/opp.c                    |  440 ++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 594 insertions(+), 0 deletions(-)
 create mode 100644 include/linux/opp.h
 create mode 100644 lib/opp.c
Documentation/power/00-INDEX +2 -0
index fb742c2..45e9d4a 100644
@@ -14,6 +14,8 @@ interface.txt
 	- Power management user interface in /sys/power
 notifiers.txt
 	- Registering suspend notifiers in device drivers
+opp.txt
+	- Operating Performance Point library
 pci.txt
 	- How the PCI Subsystem Does Power Management
 pm_qos_interface.txt
include/linux/opp.h new file +136 -0

136 lines not on this page · raw message

kernel/power/Kconfig +14 -0
index ca6066a..634eab6 100644
@@ -242,3 +242,17 @@ config PM_OPS
 	bool
 	depends on PM_SLEEP || PM_RUNTIME
 	default y
+
+config PM_OPP
+	bool "Enable Operating Performance Point(OPP) Layer library"
+	depends on PM
+	---help---
+	  SOCs have a standard set of tuples consisting of frequency and
+	  voltage pairs that the device will support per voltage domain. This
+	  is called Operating Performance Point or OPP. The actual definitions
+	  of OPP varies over silicon within the same family of devices.
+
+	  OPP layer organizes the data internally using device pointers
+	  representing individual voltage domains and provides SOC
+	  implementations a ready to use framework to manage OPPs.
+	  For more information, read <file:Documentation/power/opp.txt>
lib/Makefile +2 -0
index e6a3763..0114fcf 100644
@@ -88,6 +88,8 @@ obj-$(CONFIG_IOMMU_HELPER) += iommu-helper.o
 obj-$(CONFIG_FAULT_INJECTION) += fault-inject.o
 obj-$(CONFIG_CPU_NOTIFIER_ERROR_INJECT) += cpu-notifier-error-inject.o
 
+obj-$(CONFIG_PM_OPP) += opp.o
+
 lib-$(CONFIG_GENERIC_BUG) += bug.o
 
 obj-$(CONFIG_HAVE_ARCH_TRACEHOOK) += syscall.o
lib/opp.c new file +440 -0

440 lines not on this page · raw message

Signature · 1 line
1.6.3.3
#2 lore raw

PATCH 1/1 opp: introduce library for device-specific OPPs

2010/9/17 Nishanth Menon <[email protected]>:

Quoted 5 lines
Documentation/power/00-INDEX +0 -0
index fb742c2..45e9d4a 100644
@@ -14,6 +14,8 @@ interface.txt
Quoted 7 lines

? ? ? ?- Power management user interface in /sys/power ?notifiers.txt
? ? ? ?- Registering suspend notifiers in device drivers +opp.txt
+ ? ? ? - Operating Performance Point library ?pci.txt
? ? ? ?- How the PCI Subsystem Does Power Management

Hm you add the documentation to the index but not the documentation itself (easy slip) would you mind posting it?

Yours,
Linus Walleij

#3 lore raw

PATCH 1/1 opp: introduce library for device-specific OPPs

Linus Walleij had written, on 09/17/2010 08:41 AM, the following:

Quoted 17 lines

2010/9/17 Nishanth Menon <[email protected]>:

Documentation/power/00-INDEX interface.txt · line 20

Quoted 12 lines
Documentation/power/00-INDEX +2 -0
index fb742c2..45e9d4a 100644
@@ -14,6 +14,8 @@ interface.txt
       - Power management user interface in /sys/power
 notifiers.txt
       - Registering suspend notifiers in device drivers
+opp.txt
+       - Operating Performance Point library
 pci.txt
       - How the PCI Subsystem Does Power Management

Hm you add the documentation to the index but not the documentation itself (easy slip) would you mind posting it?

ouch.. Sorry.. I realized that I missed adding MAINTAINER entry as well.. v2 coming up..

Signature · 2 lines
Regards,
Nishanth Menon
#4 lore raw

PATCH 1/1 opp: introduce library for device-specific OPPs

Nishanth Menon had written, on 09/17/2010 10:05 AM, the following:

Quoted 17 lines

Linus Walleij had written, on 09/17/2010 08:41 AM, the following:

Quoted 16 lines

2010/9/17 Nishanth Menon <[email protected]>:

Documentation/power/00-INDEX interface.txt · line 20

Quoted 12 lines
Documentation/power/00-INDEX +2 -0
index fb742c2..45e9d4a 100644
@@ -14,6 +14,8 @@ interface.txt
       - Power management user interface in /sys/power
 notifiers.txt
       - Registering suspend notifiers in device drivers
+opp.txt
+       - Operating Performance Point library
 pci.txt
       - How the PCI Subsystem Does Power Management

Hm you add the documentation to the index but not the documentation itself (easy slip) would you mind posting it?

Quoted 3 lines

ouch.. Sorry.. I realized that I missed adding MAINTAINER entry as well.. v2 coming up..

while the review goes on, I will till end of the day before posting a v2 will collated comments, here is the opp.txt documentation for the same.. apologies on missing in my v1..

OPP Layer
==============
SOCs have a standard set of tuples consisting of frequency and voltage pairs that the device will support per voltage domain. This is called Operating Performance Point or OPP. The actual definitions of OPP varies over silicon within the same family of devices. For a specific domain, you can have a set of {frequency, voltage} pairs. As the kernel boots and more information is available, a set of these are activated based on the precise nature of device the kernel boots up on. It is interesting to remember that each IP which belongs to a voltage domain may define their own set of OPPs on top of this.

OPP layer of its own depends on silicon specific implementation and board specific data to finalize on the final set of OPPs available in a system

OPP layer organizes the data internally using device pointers representing individual voltage domains.

NOTE:
a) the OPP layer implementation expects to be used in multiple contexts based on SOC implementation and recommends locking schemes appropriate to the usage of SOC.
b) All pointer returns are expected to be handled by standard error checks such as IS_ERR() and appropriate actions taken by the caller.
c) Dependency of OPP layer is on CONFIG_PM as certain SOCs such as Texas Instrument's OMAP support have frameworks to optionally boot at a certain opp without needing cpufreq.

Initial list initialization:
---------------------------
The SOC implementation calls the following function to add opp per domain device.

1. opp_add - add a new OPP - NOTE: use struct opp_def and define the custom OPP with OPP_DEF for usage.

Query functions:
----------------
Search for OPPs for various cases:
2. opp_find_freq_exact - exact search function
3. opp_find_freq_floor - round_up search function
4. opp_find_freq_ceil - round_down search function

OPP modifier functions:
----------------------
This allows opp layer users to add customized OPPs or change the table for any need they may have
5. opp_enable - enable a disabled OPP
6. opp_disable - disable an enabled OPP

OPP Data retrieval functions:
----------------------------
The following sets of functions are useful for drivers to retrieve data stored in opp layer for various functions.
7. opp_get_voltage - retrieve voltage for an opp
9. opp_get_freq - get the frequency for an opp
8. opp_get_opp_count - get number of opps enabled for a domain

Cpufreq table generation:
------------------------
9. opp_init_cpufreq_table - this translates the OPP layer's internal OPP arrangement into a table understood and operated upon by the cpufreq layer.

Data Structures:
---------------
struct opp * is a handle structure whose internals are known only to the OPP layer and is meant to hide the complexity away from users of opp layer.

struct opp_def * is the definitions that users can interface with opp layer and is meant to define one OPP for a domain device.

Signature · 2 lines
Regards,
Nishanth Menon
#5 lore raw

PATCH 1/1 opp: introduce library for device-specific OPPs

Quoted 18 lines · Nishanth Menon

Nishanth Menon had written, on 09/17/2010 10:05 AM, the following:

Quoted 17 lines

Linus Walleij had written, on 09/17/2010 08:41 AM, the following:

Quoted 16 lines

2010/9/17 Nishanth Menon <[email protected]>:

Documentation/power/00-INDEX interface.txt · line 20

Quoted 12 lines
Documentation/power/00-INDEX +2 -0
index fb742c2..45e9d4a 100644
@@ -14,6 +14,8 @@ interface.txt
       - Power management user interface in /sys/power
 notifiers.txt
       - Registering suspend notifiers in device drivers
+opp.txt
+       - Operating Performance Point library
 pci.txt
       - How the PCI Subsystem Does Power Management

Hm you add the documentation to the index but not the documentation itself (easy slip) would you mind posting it?

Quoted 7 lines
Quoted 3 lines

ouch.. Sorry.. I realized that I missed adding MAINTAINER entry as well.. v2 coming up..

while the review goes on, I will till end of the day before posting a v2 will collated comments, here is the opp.txt documentation for the same.. apologies on missing in my v1..

OK, I'm not generally familiar with these things, so a couple of questions.

Quoted 10 lines

OPP Layer
==============
SOCs have a standard set of tuples consisting of frequency and voltage pairs that the device will support per voltage domain. This is called Operating Performance Point or OPP. The actual definitions of OPP varies over silicon within the same family of devices. For a specific domain, you can have a set of {frequency, voltage} pairs. As the kernel boots and more information is available, a set of these are activated based on the precise nature of device the kernel boots up on.

Does it mean that the full set of possible OPPs is already known from the hardware configuration and the ones that are actually useable are found during boot?

Quoted 2 lines

It is interesting to remember that each IP which belongs to a voltage domain may define their own set of OPPs on top of this.

What does IP stand for in this context?

Quoted 3 lines

OPP layer of its own depends on silicon specific implementation and board specific data to finalize on the final set of OPPs available in a system

How does the kernel learn about these things? Do they need to be hardcoded or is there any dynamic configuration mechanism available?

Quoted 2 lines

OPP layer organizes the data internally using device pointers representing individual voltage domains.

Can you please describe these data structures in a bit more detail?

Quoted 6 lines

NOTE:
a) the OPP layer implementation expects to be used in multiple contexts based on SOC implementation and recommends locking schemes appropriate to the usage of SOC.
b) All pointer returns are expected to be handled by standard error checks such as IS_ERR() and appropriate actions taken by the caller.

I noticed that in a few places it isn't really necessary to return a pointer. I think you can simply return int in such cases.

Quoted 12 lines

c) Dependency of OPP layer is on CONFIG_PM as certain SOCs such as Texas Instrument's OMAP support have frameworks to optionally boot at a certain opp without needing cpufreq.

Initial list initialization:
---------------------------
The SOC implementation calls the following function to add opp per domain device.

1. opp_add - add a new OPP - NOTE: use struct opp_def and define the custom OPP with OPP_DEF for usage.

Quoted 6 lines

Query functions:
----------------
Search for OPPs for various cases:
2. opp_find_freq_exact - exact search function
3. opp_find_freq_floor - round_up search function
4. opp_find_freq_ceil - round_down search function

What do they do exactly?

Quoted 3 lines

OPP modifier functions:
----------------------
This allows opp layer users to add customized OPPs or change the table

How exactly is that supposed to work?

Quoted 11 lines

for any need they may have
5. opp_enable - enable a disabled OPP
6. opp_disable - disable an enabled OPP

OPP Data retrieval functions:
----------------------------
The following sets of functions are useful for drivers to retrieve data stored in opp layer for various functions.
7. opp_get_voltage - retrieve voltage for an opp
9. opp_get_freq - get the frequency for an opp
8. opp_get_opp_count - get number of opps enabled for a domain

OK, suppose I'm writing a driver that needs to care. What should I use these functions for?

Quoted 6 lines

Cpufreq table generation:
------------------------
9. opp_init_cpufreq_table - this translates the OPP layer's internal OPP arrangement into a table understood and operated upon by the cpufreq layer.

Quoted 8 lines

Data Structures:
---------------
struct opp * is a handle structure whose internals are known only to the OPP layer and is meant to hide the complexity away from users of opp layer.

struct opp_def * is the definitions that users can interface with opp layer and is meant to define one OPP for a domain device.

The data structures require some more description IMHO. They aren't completely intuitive.

Thanks,
Rafael

#6 lore raw

PATCH 1/1 opp: introduce library for device-specific OPPs

Rafael J. Wysocki had written, on 09/17/2010 05:45 PM, the following:

Thanks for your review. few views below..

Quoted 39 lines · Nishanth Menon
Quoted 18 lines

Nishanth Menon had written, on 09/17/2010 10:05 AM, the following:

Quoted 17 lines

Linus Walleij had written, on 09/17/2010 08:41 AM, the following:

Quoted 16 lines

2010/9/17 Nishanth Menon <[email protected]>:

Documentation/power/00-INDEX interface.txt · line 20

Quoted 12 lines
Documentation/power/00-INDEX +2 -0
index fb742c2..45e9d4a 100644
@@ -14,6 +14,8 @@ interface.txt
       - Power management user interface in /sys/power
 notifiers.txt
       - Registering suspend notifiers in device drivers
+opp.txt
+       - Operating Performance Point library
 pci.txt
       - How the PCI Subsystem Does Power Management

Hm you add the documentation to the index but not the documentation itself (easy slip) would you mind posting it?

Quoted 6 lines
Quoted 3 lines

ouch.. Sorry.. I realized that I missed adding MAINTAINER entry as well.. v2 coming up..

while the review goes on, I will till end of the day before posting a v2 will collated comments, here is the opp.txt documentation for the same.. apologies on missing in my v1..

OK, I'm not generally familiar with these things, so a couple of questions.

Quoted 10 lines

OPP Layer
==============
SOCs have a standard set of tuples consisting of frequency and voltage pairs that the device will support per voltage domain. This is called Operating Performance Point or OPP. The actual definitions of OPP varies over silicon within the same family of devices. For a specific domain, you can have a set of {frequency, voltage} pairs. As the kernel boots and more information is available, a set of these are activated based on the precise nature of device the kernel boots up on.

Quoted 3 lines

Does it mean that the full set of possible OPPs is already known from the hardware configuration and the ones that are actually useable are found during boot?

yes. example - https://patchwork.kernel.org/patch/183742/ (based on original patch posted to l-arm, this will need some tweaks with the latest evolution, the concepts remain the same).

For example, consider in the patch above where we have a opp definitions we can choose from omap3430 and 3630,

when using 3630 silicon as a specific, certain boards wish to enable 1GHz, this allows us to do something as follows:
in the generic omap code, we do init of all opps
in the board specific file, we enable 1Ghz

The selection of oppset and actual availability is done on the fly.

Quoted 5 lines
Quoted 3 lines

It is interesting to remember that each IP which belongs to a voltage domain may define their own set of OPPs on top of this.

What does IP stand for in this context?

I had intended domains linked to an IP Block within an SOC - in the case of OMAP as an example we may have scenarios where voltage levels are optimized beyond those defined in the original OPP tables using a TI hardware IP called SmartReflex AVS (ok many SOCs probably are not that complex.. but it is a cool feature to enable improve the power consumption). more:
http://www.powersystemsdesign.com/index.php?option=com_content&view=article&id=168&Itemid=107

So in reality once you enable you'd have a lot more tuples of (Fx, Vnom),(Fx, Vopt1), (Fx, Vopt2).... where (Fx, Vnom) is the original opp definition.

Unfortunately our framework for supporting this is still in churn and probably not ready for upstream yet.

Quoted 7 lines
Quoted 4 lines

OPP layer of its own depends on silicon specific implementation and board specific data to finalize on the final set of OPPs available in a system

How does the kernel learn about these things? Do they need to be hardcoded or is there any dynamic configuration mechanism available?

yes, it is prefered to allow this to be SOC specific - every SOC (including various OMAP families) have their capabilities allowing them to implement the detection and configuration differently.. maybe device trees could be in use in one SOCx, but SOCy might choose to implement a run time detection as demonstrated in the link above.

Quoted 5 lines
Quoted 3 lines

OPP layer organizes the data internally using device pointers representing individual voltage domains.

Can you please describe these data structures in a bit more detail?

probably a dumb question: Is'nt it enough to give detailed verbose information in the struct comment header? why duplicate here as well.. other than giving an overview?

Quoted 10 lines
Quoted 7 lines

NOTE:
a) the OPP layer implementation expects to be used in multiple contexts based on SOC implementation and recommends locking schemes appropriate to the usage of SOC.
b) All pointer returns are expected to be handled by standard error checks such as IS_ERR() and appropriate actions taken by the caller.

I noticed that in a few places it isn't really necessary to return a pointer. I think you can simply return int in such cases.

could you help and point these to me. opp_find_freq_exact, opp_find_freq_ceil, opp_find_freq_floor are the only ones that return pointers and they need to return the pointer to the opp they found to operate on them such as add_freq etc..

Quoted 13 lines
Quoted 13 lines

c) Dependency of OPP layer is on CONFIG_PM as certain SOCs such as Texas Instrument's OMAP support have frameworks to optionally boot at a certain opp without needing cpufreq.

Initial list initialization:
---------------------------
The SOC implementation calls the following function to add opp per domain device.

1. opp_add - add a new OPP - NOTE: use struct opp_def and define the custom OPP with OPP_DEF for usage.

Quoted 8 lines
Quoted 6 lines

Query functions:
----------------
Search for OPPs for various cases:
2. opp_find_freq_exact - exact search function
3. opp_find_freq_floor - round_up search function
4. opp_find_freq_ceil - round_down search function

What do they do exactly?

I assume you are expecting to get a little more verbose data here to the effect:
2. opp_find_freq_exact - function to find an opp with exact match to a provided frequency
3. opp_find_freq_floor - function to find an opp with rounded floor match to a provided frequency
4. opp_find_freq_ceil - function to find an opp with rounded ceil match to a provided frequency

Quoted 6 lines
Quoted 4 lines

OPP modifier functions:
----------------------
This allows opp layer users to add customized OPPs or change the table

How exactly is that supposed to work?

:) This is more from the real way folks who use the OPP on thier platforms face. As an example: OMAP3630 supports 100MHz and 200MHz for the L3 bus driving DDR. some platforms choose not to have 200MHz DDR due to some internal reasons, instead they choose a DDR - for the sake of argument say 166MHz. This is not actually "official OPP" from TI perspective, but these platforms need to do:
init_omap_default_opps()
add_my_custom_166MHz_opp()
disable_the_default_200MHz()

Disclaimer to prevent OMAP guys from flaming me: the values used in my example are hypothetical and is not an official statement from TI to use 166MHz on OMAP3630 ;). But there are folks not using 200MHz and they know the pain if this runtime addition was not possible.

Quoted 13 lines
Quoted 5 lines

for any need they may have
5. opp_enable - enable a disabled OPP
6. opp_disable - disable an enabled OPP

Quoted 7 lines

OPP Data retrieval functions:
----------------------------
The following sets of functions are useful for drivers to retrieve data stored in opp layer for various functions.
7. opp_get_voltage - retrieve voltage for an opp
9. opp_get_freq - get the frequency for an opp
8. opp_get_opp_count - get number of opps enabled for a domain

Quoted 2 lines

OK, suppose I'm writing a driver that needs to care. What should I use these functions for?

opp_get_opp_count gets the number of opps available for that domain - you may choose to setup some sort of array containing some special data on it.
cpufreq SOCs impleemntation might be told to go to a frequency, it makes sense for the implementation to pick up the opp pointer with search function and call opp_get_voltage to get the voltage to scale voltage etc..

Or am I missing your intent here? I did not intend to explain the concept of power management in this documentation..

Quoted 13 lines
Quoted 13 lines

Cpufreq table generation:
------------------------
9. opp_init_cpufreq_table - this translates the OPP layer's internal OPP arrangement into a table understood and operated upon by the cpufreq layer.

Data Structures:
---------------
struct opp * is a handle structure whose internals are known only to the OPP layer and is meant to hide the complexity away from users of opp layer.

Quoted 5 lines
Quoted 2 lines

struct opp_def * is the definitions that users can interface with opp layer and is meant to define one OPP for a domain device.

The data structures require some more description IMHO. They aren't completely intuitive.

ok, a repeat question -> why duplicate the information defined in the structure comment header?

Signature · 2 lines
Regards,
Nishanth Menon
#7 lore raw

[Trimming the CC list]

Quoted 54 lines · Nishanth Menon

Rafael J. Wysocki had written, on 09/17/2010 05:45 PM, the following:

Thanks for your review. few views below..

Quoted 39 lines · Nishanth Menon
Quoted 18 lines

Nishanth Menon had written, on 09/17/2010 10:05 AM, the following:

Quoted 17 lines

Linus Walleij had written, on 09/17/2010 08:41 AM, the following:

Quoted 16 lines

2010/9/17 Nishanth Menon <[email protected]>:

Documentation/power/00-INDEX interface.txt · line 20

Quoted 12 lines
Documentation/power/00-INDEX +2 -0
index fb742c2..45e9d4a 100644
@@ -14,6 +14,8 @@ interface.txt
       - Power management user interface in /sys/power
 notifiers.txt
       - Registering suspend notifiers in device drivers
+opp.txt
+       - Operating Performance Point library
 pci.txt
       - How the PCI Subsystem Does Power Management

Hm you add the documentation to the index but not the documentation itself (easy slip) would you mind posting it?

Quoted 6 lines
Quoted 3 lines

ouch.. Sorry.. I realized that I missed adding MAINTAINER entry as well.. v2 coming up..

while the review goes on, I will till end of the day before posting a v2 will collated comments, here is the opp.txt documentation for the same.. apologies on missing in my v1..

OK, I'm not generally familiar with these things, so a couple of questions.

Quoted 10 lines

OPP Layer
==============
SOCs have a standard set of tuples consisting of frequency and voltage pairs that the device will support per voltage domain. This is called Operating Performance Point or OPP. The actual definitions of OPP varies over silicon within the same family of devices. For a specific domain, you can have a set of {frequency, voltage} pairs. As the kernel boots and more information is available, a set of these are activated based on the precise nature of device the kernel boots up on.

Quoted 3 lines

Does it mean that the full set of possible OPPs is already known from the hardware configuration and the ones that are actually useable are found during boot?

yes. example - https://patchwork.kernel.org/patch/183742/ (based on original patch posted to l-arm, this will need some tweaks with the latest evolution, the concepts remain the same).

For example, consider in the patch above where we have a opp definitions we can choose from omap3430 and 3630,

Quoted 6 lines

when using 3630 silicon as a specific, certain boards wish to enable 1GHz, this allows us to do something as follows:
in the generic omap code, we do init of all opps
in the board specific file, we enable 1Ghz

The selection of oppset and actual availability is done on the fly.

OK, so why don't you simply drop the OPPs you're not going to use from the list in the board-specific file?

...

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

OPP layer organizes the data internally using device pointers representing individual voltage domains.

Can you please describe these data structures in a bit more detail?

probably a dumb question: Is'nt it enough to give detailed verbose information in the struct comment header? why duplicate here as well.. other than giving an overview?

I meant "explain it to me" rather than "explain it in the doc". :-) Sorry for not being clear enough.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

NOTE:
a) the OPP layer implementation expects to be used in multiple contexts based on SOC implementation and recommends locking schemes appropriate to the usage of SOC.
b) All pointer returns are expected to be handled by standard error checks such as IS_ERR() and appropriate actions taken by the caller.

Quoted 6 lines
Quoted 2 lines

I noticed that in a few places it isn't really necessary to return a pointer. I think you can simply return int in such cases.

could you help and point these to me. opp_find_freq_exact, opp_find_freq_ceil, opp_find_freq_floor are the only ones that return pointers and they need to return the pointer to the opp they found to operate on them such as add_freq etc..

Hmm, OK. I must have made a mistake, sorry.

...

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Data Structures:
---------------
struct opp * is a handle structure whose internals are known only to the OPP layer and is meant to hide the complexity away from users of opp layer.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

struct opp_def * is the definitions that users can interface with opp layer and is meant to define one OPP for a domain device.

The data structures require some more description IMHO. They aren't completely intuitive.

ok, a repeat question -> why duplicate the information defined in the structure comment header?

Because it doesn't imply the specific implementation, AFAICS.

Like what the list heads in your structures are used for etc.

Thanks,
Rafael

#8 lore raw
In reply to Nishanth Menon

[Trimming the CC list]

Quoted 54 lines · Nishanth Menon

Rafael J. Wysocki had written, on 09/17/2010 05:45 PM, the following:

Thanks for your review. few views below..

Quoted 39 lines · Nishanth Menon
Quoted 18 lines

Nishanth Menon had written, on 09/17/2010 10:05 AM, the following:

Quoted 17 lines

Linus Walleij had written, on 09/17/2010 08:41 AM, the following:

Quoted 16 lines

2010/9/17 Nishanth Menon <[email protected]>:

Documentation/power/00-INDEX interface.txt · line 20

Quoted 12 lines
Documentation/power/00-INDEX +2 -0
index fb742c2..45e9d4a 100644
@@ -14,6 +14,8 @@ interface.txt
       - Power management user interface in /sys/power
 notifiers.txt
       - Registering suspend notifiers in device drivers
+opp.txt
+       - Operating Performance Point library
 pci.txt
       - How the PCI Subsystem Does Power Management

Hm you add the documentation to the index but not the documentation itself (easy slip) would you mind posting it?

Quoted 6 lines
Quoted 3 lines

ouch.. Sorry.. I realized that I missed adding MAINTAINER entry as well.. v2 coming up..

while the review goes on, I will till end of the day before posting a v2 will collated comments, here is the opp.txt documentation for the same.. apologies on missing in my v1..

OK, I'm not generally familiar with these things, so a couple of questions.

Quoted 10 lines

OPP Layer
==============
SOCs have a standard set of tuples consisting of frequency and voltage pairs that the device will support per voltage domain. This is called Operating Performance Point or OPP. The actual definitions of OPP varies over silicon within the same family of devices. For a specific domain, you can have a set of {frequency, voltage} pairs. As the kernel boots and more information is available, a set of these are activated based on the precise nature of device the kernel boots up on.

Quoted 3 lines

Does it mean that the full set of possible OPPs is already known from the hardware configuration and the ones that are actually useable are found during boot?

yes. example - https://patchwork.kernel.org/patch/183742/ (based on original patch posted to l-arm, this will need some tweaks with the latest evolution, the concepts remain the same).

For example, consider in the patch above where we have a opp definitions we can choose from omap3430 and 3630,

Quoted 6 lines

when using 3630 silicon as a specific, certain boards wish to enable 1GHz, this allows us to do something as follows:
in the generic omap code, we do init of all opps
in the board specific file, we enable 1Ghz

The selection of oppset and actual availability is done on the fly.

OK, so why don't you simply drop the OPPs you're not going to use from the list in the board-specific file?

...

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

OPP layer organizes the data internally using device pointers representing individual voltage domains.

Can you please describe these data structures in a bit more detail?

probably a dumb question: Is'nt it enough to give detailed verbose information in the struct comment header? why duplicate here as well.. other than giving an overview?

I meant "explain it to me" rather than "explain it in the doc". :-) Sorry for not being clear enough.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

NOTE:
a) the OPP layer implementation expects to be used in multiple contexts based on SOC implementation and recommends locking schemes appropriate to the usage of SOC.
b) All pointer returns are expected to be handled by standard error checks such as IS_ERR() and appropriate actions taken by the caller.

Quoted 6 lines
Quoted 2 lines

I noticed that in a few places it isn't really necessary to return a pointer. I think you can simply return int in such cases.

could you help and point these to me. opp_find_freq_exact, opp_find_freq_ceil, opp_find_freq_floor are the only ones that return pointers and they need to return the pointer to the opp they found to operate on them such as add_freq etc..

Hmm, OK. I must have made a mistake, sorry.

...

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Data Structures:
---------------
struct opp * is a handle structure whose internals are known only to the OPP layer and is meant to hide the complexity away from users of opp layer.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

struct opp_def * is the definitions that users can interface with opp layer and is meant to define one OPP for a domain device.

The data structures require some more description IMHO. They aren't completely intuitive.

ok, a repeat question -> why duplicate the information defined in the structure comment header?

Because it doesn't imply the specific implementation, AFAICS.

Like what the list heads in your structures are used for etc.

Thanks,
Rafael

#9 lore raw
In reply to Nishanth Menon

[Trimming the CC list]

Quoted 54 lines · Nishanth Menon

Rafael J. Wysocki had written, on 09/17/2010 05:45 PM, the following:

Thanks for your review. few views below..

Quoted 39 lines · Nishanth Menon
Quoted 18 lines

Nishanth Menon had written, on 09/17/2010 10:05 AM, the following:

Quoted 17 lines

Linus Walleij had written, on 09/17/2010 08:41 AM, the following:

Quoted 16 lines

2010/9/17 Nishanth Menon <[email protected]>:

Documentation/power/00-INDEX interface.txt · line 20

Quoted 12 lines
Documentation/power/00-INDEX +2 -0
index fb742c2..45e9d4a 100644
@@ -14,6 +14,8 @@ interface.txt
       - Power management user interface in /sys/power
 notifiers.txt
       - Registering suspend notifiers in device drivers
+opp.txt
+       - Operating Performance Point library
 pci.txt
       - How the PCI Subsystem Does Power Management

Hm you add the documentation to the index but not the documentation itself (easy slip) would you mind posting it?

Quoted 6 lines
Quoted 3 lines

ouch.. Sorry.. I realized that I missed adding MAINTAINER entry as well.. v2 coming up..

while the review goes on, I will till end of the day before posting a v2 will collated comments, here is the opp.txt documentation for the same.. apologies on missing in my v1..

OK, I'm not generally familiar with these things, so a couple of questions.

Quoted 10 lines

OPP Layer
==============
SOCs have a standard set of tuples consisting of frequency and voltage pairs that the device will support per voltage domain. This is called Operating Performance Point or OPP. The actual definitions of OPP varies over silicon within the same family of devices. For a specific domain, you can have a set of {frequency, voltage} pairs. As the kernel boots and more information is available, a set of these are activated based on the precise nature of device the kernel boots up on.

Quoted 3 lines

Does it mean that the full set of possible OPPs is already known from the hardware configuration and the ones that are actually useable are found during boot?

yes. example - https://patchwork.kernel.org/patch/183742/ (based on original patch posted to l-arm, this will need some tweaks with the latest evolution, the concepts remain the same).

For example, consider in the patch above where we have a opp definitions we can choose from omap3430 and 3630,

Quoted 6 lines

when using 3630 silicon as a specific, certain boards wish to enable 1GHz, this allows us to do something as follows:
in the generic omap code, we do init of all opps
in the board specific file, we enable 1Ghz

The selection of oppset and actual availability is done on the fly.

OK, so why don't you simply drop the OPPs you're not going to use from the list in the board-specific file?

...

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

OPP layer organizes the data internally using device pointers representing individual voltage domains.

Can you please describe these data structures in a bit more detail?

probably a dumb question: Is'nt it enough to give detailed verbose information in the struct comment header? why duplicate here as well.. other than giving an overview?

I meant "explain it to me" rather than "explain it in the doc". :-) Sorry for not being clear enough.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

NOTE:
a) the OPP layer implementation expects to be used in multiple contexts based on SOC implementation and recommends locking schemes appropriate to the usage of SOC.
b) All pointer returns are expected to be handled by standard error checks such as IS_ERR() and appropriate actions taken by the caller.

Quoted 6 lines
Quoted 2 lines

I noticed that in a few places it isn't really necessary to return a pointer. I think you can simply return int in such cases.

could you help and point these to me. opp_find_freq_exact, opp_find_freq_ceil, opp_find_freq_floor are the only ones that return pointers and they need to return the pointer to the opp they found to operate on them such as add_freq etc..

Hmm, OK. I must have made a mistake, sorry.

...

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Data Structures:
---------------
struct opp * is a handle structure whose internals are known only to the OPP layer and is meant to hide the complexity away from users of opp layer.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

struct opp_def * is the definitions that users can interface with opp layer and is meant to define one OPP for a domain device.

The data structures require some more description IMHO. They aren't completely intuitive.

ok, a repeat question -> why duplicate the information defined in the structure comment header?

Because it doesn't imply the specific implementation, AFAICS.

Like what the list heads in your structures are used for etc.

Thanks,
Rafael

#10 lore raw
In reply to Nishanth Menon

Nishanth Menon had written, on 09/17/2010 10:05 AM, the following:

Quoted 17 lines

Linus Walleij had written, on 09/17/2010 08:41 AM, the following:

Quoted 16 lines

2010/9/17 Nishanth Menon <[email protected]>:

Documentation/power/00-INDEX interface.txt · line 20

Quoted 12 lines
Documentation/power/00-INDEX +2 -0
index fb742c2..45e9d4a 100644
@@ -14,6 +14,8 @@ interface.txt
       - Power management user interface in /sys/power
 notifiers.txt
       - Registering suspend notifiers in device drivers
+opp.txt
+       - Operating Performance Point library
 pci.txt
       - How the PCI Subsystem Does Power Management

Hm you add the documentation to the index but not the documentation itself (easy slip) would you mind posting it?

Quoted 3 lines

ouch.. Sorry.. I realized that I missed adding MAINTAINER entry as well.. v2 coming up..

while the review goes on, I will till end of the day before posting a v2 will collated comments, here is the opp.txt documentation for the same.. apologies on missing in my v1..

OPP Layer
==============
SOCs have a standard set of tuples consisting of frequency and voltage pairs that the device will support per voltage domain. This is called Operating Performance Point or OPP. The actual definitions of OPP varies over silicon within the same family of devices. For a specific domain, you can have a set of {frequency, voltage} pairs. As the kernel boots and more information is available, a set of these are activated based on the precise nature of device the kernel boots up on. It is interesting to remember that each IP which belongs to a voltage domain may define their own set of OPPs on top of this.

OPP layer of its own depends on silicon specific implementation and board specific data to finalize on the final set of OPPs available in a system

OPP layer organizes the data internally using device pointers representing individual voltage domains.

NOTE:
a) the OPP layer implementation expects to be used in multiple contexts based on SOC implementation and recommends locking schemes appropriate to the usage of SOC.
b) All pointer returns are expected to be handled by standard error checks such as IS_ERR() and appropriate actions taken by the caller.
c) Dependency of OPP layer is on CONFIG_PM as certain SOCs such as Texas Instrument's OMAP support have frameworks to optionally boot at a certain opp without needing cpufreq.

Initial list initialization:
---------------------------
The SOC implementation calls the following function to add opp per domain device.

1. opp_add - add a new OPP - NOTE: use struct opp_def and define the custom OPP with OPP_DEF for usage.

Query functions:
----------------
Search for OPPs for various cases:
2. opp_find_freq_exact - exact search function
3. opp_find_freq_floor - round_up search function
4. opp_find_freq_ceil - round_down search function

OPP modifier functions:
----------------------
This allows opp layer users to add customized OPPs or change the table for any need they may have
5. opp_enable - enable a disabled OPP
6. opp_disable - disable an enabled OPP

OPP Data retrieval functions:
----------------------------
The following sets of functions are useful for drivers to retrieve data stored in opp layer for various functions.
7. opp_get_voltage - retrieve voltage for an opp
9. opp_get_freq - get the frequency for an opp
8. opp_get_opp_count - get number of opps enabled for a domain

Cpufreq table generation:
------------------------
9. opp_init_cpufreq_table - this translates the OPP layer's internal OPP arrangement into a table understood and operated upon by the cpufreq layer.

Data Structures:
---------------
struct opp * is a handle structure whose internals are known only to the OPP layer and is meant to hide the complexity away from users of opp layer.

struct opp_def * is the definitions that users can interface with opp layer and is meant to define one OPP for a domain device.

Signature · 2 lines
Regards,
Nishanth Menon
#11 lore raw
In reply to Linus Walleij

Linus Walleij had written, on 09/17/2010 08:41 AM, the following:

Quoted 17 lines

2010/9/17 Nishanth Menon <[email protected]>:

Documentation/power/00-INDEX interface.txt · line 20

Quoted 12 lines
Documentation/power/00-INDEX +2 -0
index fb742c2..45e9d4a 100644
@@ -14,6 +14,8 @@ interface.txt
       - Power management user interface in /sys/power
 notifiers.txt
       - Registering suspend notifiers in device drivers
+opp.txt
+       - Operating Performance Point library
 pci.txt
       - How the PCI Subsystem Does Power Management

Hm you add the documentation to the index but not the documentation itself (easy slip) would you mind posting it?

ouch.. Sorry.. I realized that I missed adding MAINTAINER entry as well.. v2 coming up..

Signature · 2 lines
Regards,
Nishanth Menon
#12 lore raw

PATCH 1/1 opp: introduce library for device-specific OPPs

In reply to Nishanth Menon

Hi Nishanth,

Quoted 103 lines

-----Original Message-----
From: Menon, Nishanth
Sent: Thursday, September 16, 2010 8:30 PM
To: linux-arm; lkml
Cc: Len Brown; Pavel Machek; Rafael J. Wysocki; Randy Dunlap; Jesse Barnes; Matthew Garrett; H. Peter Anvin; Andrew Morton; Benjamin Herrenschmidt; Martin K. Petersen; Andi Kleen; linux-pm; linux-doc; linux- omap; Menon, Nishanth; Cousson, Benoit; Chikkature Rajashekar, Madhusudhan; Phil Carmody; Granados Dorado, Roberto; Shilimkar, Santosh; Aguirre, Sergio; Tero Kristo; Eduardo Valentin; Paul Walmsley; Romit Dasgupta; Premi, Sanjeev; Gopinath, Thara; Sripathy, Vishwanath; Linus Walleij; Kevin Hilman
Subject: [PATCH] opp: introduce library for device-specific OPPs

SOCs have a standard set of tuples consisting of frequency and voltage pairs that the device will support per voltage domain. These are called Operating Performance Points or OPPs. The actual definitions of Operating Performance Points varies over silicon within the same family of devices. For a specific domain, you can have a set of {frequency, voltage} pairs. As the kernel boots and more information is available, a set of these are activated based on the precise nature of device the kernel boots up on. It is interesting to remember that each IP which belongs to a voltage domain may define their own set of OPPs on top of this.

To implement an OPP, some sort of power management support is necessary hence this library enablement depends on CONFIG_PM, however this does not fit into the core power framework as it is an independent library. This is hence introduced under lib allowing all architectures to selectively enable the feature based on thier capabilities.

Contributions include:
Sanjeev Premi for the initial concept:

      http://patchwork.kernel.org/patch/50998/

Kevin Hilman for converting original design to device-based Kevin Hilman and Paul Walmsey for cleaning up many of the function abstractions, improvements and data structure handling Romit Dasgupta for using enums instead of opp pointers Thara Gopinath, Eduardo Valentin and Vishwanath BS for fixes and cleanups.
Linus Walleij for recommending this layer be made generic for usage in other architectures beyond OMAP and ARM.

Discussions and comments from:
http://marc.info/?l=linux-omap&m=126033945313269&w=2
http://marc.info/?l=linux-omap&m=125482970102327&w=2
http://marc.info/?t=125809247500002&r=1&w=2
http://marc.info/?l=linux-omap&m=126025973426007&w=2
http://marc.info/?t=128152609200064&r=1&w=2
incorporated.

Cc Benoit Cousson <[email protected]>
Cc Madhusudhan Chikkature Rajashekar <[email protected]>
Cc Phil Carmody <[email protected]>
Cc Roberto Granados Dorado <[email protected]>
Cc Santosh Shilimkar <[email protected]>
Cc Sergio Alberto Aguirre Rodriguez <[email protected]>
Cc Tero Kristo <[email protected]>
Cc Eduardo Valentin <[email protected]>
Cc Paul Walmsley <[email protected]>
Cc Romit Dasgupta <[email protected]>
Cc Sanjeev Premi <[email protected]>
Cc Thara Gopinath <[email protected]>
Cc Vishwanath BS <[email protected]>
Cc Linus Walleij <[email protected]>

---
minor notes:
a) Code rebased to linus's tree commit 03a7ab0
b) Looping in get-maintainter.pl list and lkml as per thread of discussion
http://marc.info/?t=128152609200064&r=1&w=2

 Documentation/power/00-INDEX |    2 +
 include/linux/opp.h          |  136 +++++++++++++
 kernel/power/Kconfig         |   14 ++
 lib/Makefile                 |    2 +
 lib/opp.c                    |  440
++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 594 insertions(+), 0 deletions(-)
 create mode 100644 include/linux/opp.h
 create mode 100644 lib/opp.c
Documentation/power/00-INDEX +2 -0
index fb742c2..45e9d4a 100644
@@ -14,6 +14,8 @@ interface.txt
      - Power management user interface in /sys/power
 notifiers.txt
      - Registering suspend notifiers in device drivers
+opp.txt
+     - Operating Performance Point library
 pci.txt
      - How the PCI Subsystem Does Power Management
 pm_qos_interface.txt
include/linux/opp.h new file +0 -0
new file mode 100644
index 0000000..94a552b

include/linux/opp.h line 8

Quoted 8 lines
include/linux/opp.h new file +8 -0
@@ -0,0 +1,136 @@
+/*
+ * Generic OPP Interface
+ *
+ * Copyright (C) 2009-2010 Texas Instruments Incorporated.
+ *   Nishanth Menon
+ *   Romit Dasgupta <[email protected]>
+ * Copyright (C) 2009 Deep Root Systems, LLC.
+ *   Kevin Hilman

Just curious why only Romit's e-mail address is there, and not Kevin's or yours...

Quoted 221 lines
include/linux/opp.h new file +20 -0
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+#ifndef __ASM_OPP_H
+#define __ASM_OPP_H
+
+#include <linux/err.h>
+#include <linux/cpufreq.h>
+
+/**
+ * struct opp_def - Generic OPP Definition
+ * @freq:    Frequency in hertz corresponding to this OPP
+ * @u_volt:  Nominal voltage in microvolts corresponding to this OPP
+ * @enabled: True/false - is this OPP enabled/disabled by default
+ *
+ * SOCs have a standard set of tuples consisting of frequency and voltage
+ * pairs that the device will support per voltage domain. This is called
+ * Operating Performance Points or OPP. The actual definitions of

Operating
+ * Performance Points varies over silicon within the same family of devices.
+ * For a specific domain, you can have a set of {frequency, voltage} pairs
+ * and this is denoted by an array of opp_def. As the kernel boots and more
+ * information is available, a set of these are activated based on the precise
+ * nature of device the kernel boots up on. It is interesting to remember that
+ * each IP which belongs to a voltage domain may define their own set of OPPs
+ * on top of this - but this is handled by the appropriate driver.
+ */

include/linux/opp.h new file +79 -0
+struct opp_def {
+     unsigned long freq;
+     unsigned long u_volt;
+
+     bool enabled;
+};
+
+/*
+ * Initialization wrapper used to define an OPP.
+ * To point at the end of a terminator of a list of OPPs,
+ * use OPP_DEF(0, 0, 0)
+ */
+#define OPP_DEF(_enabled, _freq, _uv)        \
+{                                            \
+     .enabled        = _enabled,             \
+     .freq           = _freq,                \
+     .u_volt         = _uv,                  \
+}
+
+struct opp;
+
+#ifdef CONFIG_PM
+
+unsigned long opp_get_voltage(const struct opp *opp);
+
+unsigned long opp_get_freq(const struct opp *opp);
+
+int opp_get_opp_count(struct device *dev);
+
+struct opp *opp_find_freq_exact(struct device *dev, unsigned long freq,
+                             bool enabled);
+
+struct opp *opp_find_freq_floor(struct device *dev, unsigned long *freq);
+
+struct opp *opp_find_freq_ceil(struct device *dev, unsigned long *freq);
+
+int opp_add(struct device *dev, const struct opp_def *opp_def);
+
+int opp_enable(struct opp *opp);
+
+int opp_disable(struct opp *opp);
+
+void opp_init_cpufreq_table(struct device *dev,
+                         struct cpufreq_frequency_table **table);
+#else
+static inline unsigned long opp_get_voltage(const struct opp *opp)
+{
+     return 0;
+}
+
+static inline unsigned long opp_get_freq(const struct opp *opp)
+{
+     return 0;
+}
+
+static inline int opp_get_opp_count(struct device *dev)
+{
+     return 0;
+}
+
+static inline struct opp *opp_find_freq_exact(struct device *dev,
+                                  unsigned long freq, bool enabled)
+{
+     return ERR_PTR(-EINVAL);
+}
+
+static inline struct opp *opp_find_freq_floor(struct device *dev,
+                                     unsigned long *freq)
+{
+     return ERR_PTR(-EINVAL);
+}
+
+static inline struct opp *opp_find_freq_ceil(struct device *dev,
+                                     unsigned long *freq)
+{
+     return ERR_PTR(-EINVAL);
+}
+
+static inline int opp_add(struct device *dev, const struct opp_def

*opp_def)

include/linux/opp.h new file +21 -0
+{
+     return ERR_PTR(-EINVAL);
+}
+
+static inline int opp_enable(struct opp *opp)
+{
+     return 0;
+}
+
+static inline int opp_disable(struct opp *opp)
+{
+     return 0;
+}
+
+static inline void opp_init_cpufreq_table(struct device *dev,
+                         struct cpufreq_frequency_table **table)
+{
+}
+
+#endif               /* CONFIG_PM */
+#endif               /* __ASM_OPP_H */
kernel/power/Kconfig +7 -0
index ca6066a..634eab6 100644
@@ -242,3 +242,17 @@ config PM_OPS
      bool
      depends on PM_SLEEP || PM_RUNTIME
      default y
+
+config PM_OPP
+     bool "Enable Operating Performance Point(OPP) Layer library"
+     depends on PM
+     ---help---
+       SOCs have a standard set of tuples consisting of frequency and
+       voltage pairs that the device will support per voltage domain.

This
+ is called Operating Performance Point or OPP. The actual
definitions

kernel/power/Kconfig +6 -0
+       of OPP varies over silicon within the same family of devices.
+
+       OPP layer organizes the data internally using device pointers
+       representing individual voltage domains and provides SOC
+       implementations a ready to use framework to manage OPPs.
+       For more information, read <file:Documentation/power/opp.txt>
lib/Makefile +2 -0
index e6a3763..0114fcf 100644
@@ -88,6 +88,8 @@ obj-$(CONFIG_IOMMU_HELPER) += iommu-helper.o
 obj-$(CONFIG_FAULT_INJECTION) += fault-inject.o
 obj-$(CONFIG_CPU_NOTIFIER_ERROR_INJECT) += cpu-notifier-error-inject.o
 
+obj-$(CONFIG_PM_OPP) += opp.o
+
 lib-$(CONFIG_GENERIC_BUG) += bug.o
 
 obj-$(CONFIG_HAVE_ARCH_TRACEHOOK) += syscall.o
lib/opp.c new file +42 -0
new file mode 100644
index 0000000..650c8c3
@@ -0,0 +1,440 @@
+/*
+ * Generic OPP Interface
+ *
+ * Copyright (C) 2009-2010 Texas Instruments Incorporated.
+ *   Nishanth Menon
+ *   Romit Dasgupta <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/err.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/cpufreq.h>
+#include <linux/list.h>
+#include <linux/opp.h>
+
+/**
+ * struct opp - Generic OPP description structure
+ * @node:    list node
+ * @enabled: true/false - marking this OPP as enabled/disabled
+ * @rate:    Frequency in hertz
+ * @u_volt:  Nominal voltage in microvolts corresponding to this OPP
+ * @dev_opp: contains the device_opp struct
+ *
+ * This structure stores the OPP information for a given domain.
+ */
+struct opp {
+     struct list_head node;
+
+     bool enabled;
+     unsigned long rate;
+     unsigned long u_volt;
+
+     struct device_opp *dev_opp;
+};
+
+/**

lib/opp.c line 52

Quoted 10 lines
lib/opp.c new file +10 -0
+ * struct device_opp - Device opp structure
+ * @node:    list node
+ * @dev:     device handle
+ * @opp_list:        list of opps
+ * @opp_count:       num opps
+ * @enabled_opp_count:       how many opps are actually enabled
+ *
+ * This is an internal datastructure maintaining the link to
+ * opps attached to a domain device. This structure is not
+ * meant to be shared with users as it private to opp layer.

"... as it is private ..."

Quoted 43 lines
lib/opp.c new file +43 -0
+ */
+struct device_opp {
+     struct list_head node;
+
+     struct device *dev;
+
+     struct list_head opp_list;
+     u32 opp_count;
+     u32 enabled_opp_count;
+};
+
+static LIST_HEAD(dev_opp_list);
+
+/**
+ * find_device_opp() - find device_opp struct using device pointer
+ * @dev:     device pointer used to lookup device OPPs
+ *
+ * Search list of device OPPs for one containing matching device.
+ *
+ * Returns pointer to 'struct device_opp' if found, otherwise -ENODEV or
+ * -EINVAL based on type of error.
+ */
+static struct device_opp *find_device_opp(struct device *dev)
+{
+     struct device_opp *tmp_dev_opp, *dev_opp = ERR_PTR(-ENODEV);
+
+     if (unlikely(!dev || IS_ERR(dev))) {
+             pr_err("%s: Invalid parameters being passed\n", __func__);
+             return ERR_PTR(-EINVAL);
+     }
+
+     list_for_each_entry(tmp_dev_opp, &dev_opp_list, node) {
+             if (tmp_dev_opp->dev == dev) {
+                     dev_opp = tmp_dev_opp;
+                     break;
+             }
+     }
+
+     return dev_opp;
+}
+
+/**
+ * opp_get_voltage() - Gets the voltage corresponding to an opp

lib/opp.c line 105

Quoted 10 lines
lib/opp.c new file +10 -0
+ * @opp:     opp for which voltage has to be returned for
+ *
+ * Return voltage in micro volt corresponding to the opp, else
+ * return 0
+ */
+unsigned long opp_get_voltage(const struct opp *opp)
+{
+     if (unlikely(!opp || IS_ERR(opp)) || !opp->enabled) {
+             pr_err("%s: Invalid parameters being passed\n", __func__);
+             return 0;

Shouldn't the case for !opp->enabled just return 0, without error reporting.

IMHO, having an OPP disabled is not really a bug in params, and returning this, will misinform the user.. I think.

What do you think?

Quoted 7 lines
lib/opp.c new file +7 -0
+     }
+
+     return opp->u_volt;
+}
+
+/**
+ * opp_get_freq() - Gets the frequency corresponding to an opp

lib/opp.c line 122

Quoted 10 lines
lib/opp.c new file +10 -0
+ * @opp:     opp for which frequency has to be returned for
+ *
+ * Return frequency in hertz corresponding to the opp, else
+ * return 0
+ */
+unsigned long opp_get_freq(const struct opp *opp)
+{
+     if (unlikely(!opp || IS_ERR(opp)) || !opp->enabled) {
+             pr_err("%s: Invalid parameters being passed\n", __func__);
+             return 0;

Similarly here.

lib/opp.c

Quoted 10 lines
lib/opp.c new file +10 -0
+     }
+
+     return opp->rate;
+}
+
+/**
+ * opp_get_opp_count() - Get number of opps enabled in the opp list
+ * @dev:     device for which we do this operation
+ *
+ * This functions returns the number of opps if there are any OPPs

"This function returns.."

Quoted 23 lines

enabled,
+ * else returns corresponding error value.
+ */

lib/opp.c new file +18 -0
+int opp_get_opp_count(struct device *dev)
+{
+     struct device_opp *dev_opp;
+
+     dev_opp = find_device_opp(dev);
+     if (IS_ERR(dev_opp))
+             return -ENODEV;
+
+     return dev_opp->enabled_opp_count;
+}
+
+/**
+ * opp_find_freq_exact() - search for an exact frequency
+ * @dev:     device for which we do this operation
+ * @freq:    frequency to search for
+ * @enabled: enabled/disabled OPP to search for
+ *
+ * Searches for exact match in the opp list and returns handle to the

matching
+ * opp if found, else returns ERR_PTR in case of error and should be

Quoted 8 lines

handled
+ * using IS_ERR.
+ *
+ * Note: enabled is a modifier for the search. if enabled=true, then the match
+ * is for exact matching frequency and is enabled. if false, the match is for
+ * exact frequency which is disabled.

That enabled var is ignored, which makes this explanation mismatch with the code behavior.

Am I missing something?

Quoted 21 lines

+ */

lib/opp.c new file +20 -0
+struct opp *opp_find_freq_exact(struct device *dev,
+                                  unsigned long freq, bool enabled)
+{
+     struct device_opp *dev_opp;
+     struct opp *temp_opp, *opp = ERR_PTR(-ENODEV);
+
+     dev_opp = find_device_opp(dev);
+     if (IS_ERR(dev_opp))
+             return opp;
+
+     list_for_each_entry(temp_opp, &dev_opp->opp_list, node) {
+             if (temp_opp->enabled && temp_opp->rate == freq) {
+                     opp = temp_opp;
+                     break;
+             }
+     }
+
+     return opp;
+}
+

lib/opp.c line 189

Quoted 10 lines
lib/opp.c new file +10 -0
+/**
+ * opp_find_freq_ceil() - Search for an rounded ceil freq
+ * @dev:     device for which we do this operation
+ * @freq:    Start frequency
+ *
+ * Search for the matching ceil *enabled* OPP from a starting freq
+ * for a domain.
+ *
+ * Returns *opp and *freq is populated with the match, else
+ * returns NULL opp if no match, else returns ERR_PTR in case of error.

By looking at the code below, I don't think returning NULL is a possibility.

Quoted 18 lines

+ *
+ * Example usages:

+ *   * find match/next highest available frequency *
+ *   freq = 350000;
+ *   opp = opp_find_freq_ceil(dev, &freq))
+ *   if (IS_ERR(opp))

+ * pr_err("unable to find a higher frequency\n");
+ * else
+ * pr_info("match freq = %ld\n", freq);
+ *

+ *   * print all supported frequencies in ascending order *
+ *   freq = 0; * Search for the lowest enabled frequency *
+ *   while (!IS_ERR(opp = opp_find_freq_ceil(OPP_MPU, &freq)) {

+ * pr_info("freq = %ld\n", freq);
+ * freq++; * for next higher match *
+ * }
+ */

lib/opp.c new file +1 -0
+struct opp *opp_find_freq_ceil(struct device *dev, unsigned long *freq)

lib/opp.c line 217

Quoted 10 lines
lib/opp.c new file +10 -0
+{
+     struct device_opp *dev_opp;
+     struct opp *temp_opp, *opp = ERR_PTR(-ENODEV);
+
+     dev_opp = find_device_opp(dev);
+     if (IS_ERR(dev_opp))
+             return opp;
+
+     list_for_each_entry(temp_opp, &dev_opp->opp_list, node) {
+             if (temp_opp->enabled && temp_opp->rate >= *freq) {

What if freq contains a NULL pointer?

lib/opp.c line 228

Quoted 11 lines
lib/opp.c new file +11 -0
+                     opp = temp_opp;
+                     *freq = opp->rate;
+                     break;
+             }
+     }
+
+     return opp;
+}
+
+/**
+ * opp_find_freq_floor() - Search for an rounded floor freq

"... Search for a rounded floor ..."

Quoted 8 lines

+ * @dev: device for which we do this operation
+ * @freq: Start frequency
+ *
+ * Search for the matching floor *enabled* OPP from a starting freq
+ * for a domain.
+ *
+ * Returns *opp and *freq is populated with the next match, else
+ * returns NULL opp if no match, else returns ERR_PTR in case of error.

Similar comment about opp not being ever NULL as the function above.

Regards,
Sergio

Quoted 211 lines

+ *
+ * Example usages:

+ *   * find match/next lowest available frequency
+ *   freq = 350000;
+ *   opp = opp_find_freq_floor(dev, &freq)))
+ *   if (IS_ERR(opp))

+ * pr_err ("unable to find a lower frequency\n");
+ * else
+ * pr_info("match freq = %ld\n", freq);
+ *

+ *   * print all supported frequencies in descending order *
+ *   freq = ULONG_MAX; * search highest enabled frequency *
+ *   while (!IS_ERR(opp = opp_find_freq_floor(OPP_MPU, &freq)) {

+ * pr_info("freq = %ld\n", freq);
+ * freq--; * for next lower match *
+ * }
+ */

lib/opp.c new file +35 -0
+struct opp *opp_find_freq_floor(struct device *dev, unsigned long *freq)
+{
+     struct device_opp *dev_opp;
+     struct opp *temp_opp, *opp = ERR_PTR(-ENODEV);
+
+     dev_opp = find_device_opp(dev);
+     if (IS_ERR(dev_opp))
+             return opp;
+
+     list_for_each_entry_reverse(temp_opp, &dev_opp->opp_list, node) {
+             if (temp_opp->enabled && temp_opp->rate <= *freq) {
+                     opp = temp_opp;
+                     *freq = opp->rate;
+                     break;
+             }
+     }
+
+     return opp;
+}
+
+/* wrapper to reuse converting opp_def to opp struct */
+static void opp_populate(struct opp *opp,
+                           const struct opp_def *opp_def)
+{
+     opp->rate = opp_def->freq;
+     opp->enabled = opp_def->enabled;
+     opp->u_volt = opp_def->u_volt;
+}
+
+/**
+ * opp_add()  - Add an OPP table from a table definitions
+ * @dev:     device for which we do this operation
+ * @opp_def: opp_def to describe the OPP which we want to add.
+ *
+ * This function adds an opp definition to the opp list and returns

status.
+ */

lib/opp.c new file +63 -0
+int opp_add(struct device *dev, const struct opp_def *opp_def)
+{
+     struct device_opp *tmp_dev_opp, *dev_opp = NULL;
+     struct opp *opp, *new_opp;
+     struct list_head *head;
+
+     /* Check for existing list for 'dev' */
+     list_for_each_entry(tmp_dev_opp, &dev_opp_list, node) {
+             if (dev == tmp_dev_opp->dev) {
+                     dev_opp = tmp_dev_opp;
+                     break;
+             }
+     }
+
+     if (!dev_opp) {
+             /* Allocate a new device OPP table */
+             dev_opp = kzalloc(sizeof(struct device_opp), GFP_KERNEL);
+             if (!dev_opp) {
+                     pr_warning("%s: unable to allocate device struct\n",
+                             __func__);
+                     return -ENOMEM;
+             }
+
+             dev_opp->dev = dev;
+             INIT_LIST_HEAD(&dev_opp->opp_list);
+
+             list_add(&dev_opp->node, &dev_opp_list);
+     }
+
+     /* allocate new OPP node */
+     new_opp = kzalloc(sizeof(struct opp), GFP_KERNEL);
+     if (!new_opp) {
+             if (list_empty(&dev_opp->opp_list)) {
+                     list_del(&dev_opp->node);
+                     kfree(dev_opp);
+             }
+             pr_warning("%s: unable to allocate new opp node\n",
+                     __func__);
+             return -ENOMEM;
+     }
+     opp_populate(new_opp, opp_def);
+
+     /* Insert new OPP in order of increasing frequency */
+     head = &dev_opp->opp_list;
+     list_for_each_entry_reverse(opp, &dev_opp->opp_list, node) {
+             if (new_opp->rate >= opp->rate) {
+                     head = &opp->node;
+                     break;
+             }
+     }
+     list_add(&new_opp->node, head);
+     dev_opp->opp_count++;
+     if (new_opp->enabled)
+             dev_opp->enabled_opp_count++;
+
+     return 0;
+}
+
+/**
+ * opp_enable() - Enable a specific OPP
+ * @opp:     Pointer to opp
+ *
+ * Enables a provided opp. If the operation is valid, this returns 0,

else the
+ * corresponding error value.
+ *
+ * OPP used here is from the the opp_is_valid/opp_has_freq or other search
+ * functions
+ */

lib/opp.c new file +20 -0
+int opp_enable(struct opp *opp)
+{
+     if (unlikely(!opp || IS_ERR(opp))) {
+             pr_err("%s: Invalid parameters being passed\n", __func__);
+             return -EINVAL;
+     }
+
+     if (!opp->enabled && opp->dev_opp)
+             opp->dev_opp->enabled_opp_count++;
+
+     opp->enabled = true;
+
+     return 0;
+}
+
+/**
+ * opp_disable() - Disable a specific OPP
+ * @opp:     Pointer to opp
+ *
+ * Disables a provided opp. If the operation is valid, this returns 0,

else the
+ * corresponding error value.
+ *
+ * OPP used here is from the the opp_is_valid/opp_has_freq or other search
+ * functions
+ */

lib/opp.c new file +58 -1
+int opp_disable(struct opp *opp)
+{
+     if (unlikely(!opp || IS_ERR(opp))) {
+             pr_err("%s: Invalid parameters being passed\n", __func__);
+             return -EINVAL;
+     }
+
+     if (opp->enabled && opp->dev_opp)
+             opp->dev_opp->enabled_opp_count--;
+
+     opp->enabled = false;
+
+     return 0;
+}
+
+/**
+ * opp_init_cpufreq_table() - create a cpufreq table for a domain
+ * @dev:     device for which we do this operation
+ * @table:   Cpufreq table returned back to caller
+ *
+ * Generate a cpufreq table for a provided domain - this assumes that the
+ * opp list is already initialized and ready for usage
+ */
+void opp_init_cpufreq_table(struct device *dev,
+                         struct cpufreq_frequency_table **table)
+{
+     struct device_opp *dev_opp;
+     struct opp *opp;
+     struct cpufreq_frequency_table *freq_table;
+     int i = 0;
+
+     dev_opp = find_device_opp(dev);
+     if (IS_ERR(dev_opp)) {
+             pr_warning("%s: unable to find device\n", __func__);
+             return;
+     }
+
+     freq_table = kzalloc(sizeof(struct cpufreq_frequency_table) *
+                          (dev_opp->enabled_opp_count + 1), GFP_ATOMIC);
+     if (!freq_table) {
+             pr_warning("%s: failed to allocate frequency table\n",
+                        __func__);
+             return;
+     }
+
+     list_for_each_entry(opp, &dev_opp->opp_list, node) {
+             if (opp->enabled) {
+                     freq_table[i].index = i;
+                     freq_table[i].frequency = opp->rate / 1000;
+                     i++;
+             }
+     }
+
+     freq_table[i].index = i;
+     freq_table[i].frequency = CPUFREQ_TABLE_END;
+
+     *table = &freq_table[0];
+}
--

1.6.3.3

#13 lore raw

PATCH 1/1 opp: introduce library for device-specific OPPs

Aguirre, Sergio had written, on 09/17/2010 09:09 AM, the following:

Quoted 1 line

Hi Nishanth,

thanks for the review.. comments below..

[...]

Quoted 9 lines
Quoted 9 lines
include/linux/opp.h new file +3 -0
new file mode 100644
index 0000000..94a552b
@@ -0,0 +1,136 @@
+/*
+ * Generic OPP Interface
+ *
Quoted 8 lines

include/linux/opp.h line 8

Quoted 5 lines
include/linux/opp.h new file +5 -0
+ * Copyright (C) 2009-2010 Texas Instruments Incorporated.
+ *   Nishanth Menon
+ *   Romit Dasgupta <[email protected]>
+ * Copyright (C) 2009 Deep Root Systems, LLC.
+ *   Kevin Hilman

Just curious why only Romit's e-mail address is there, and not Kevin's or yours...

personal choice I guess, Romit prefered his email id added Kevin and I did not.

[...]

Quoted 8 lines
Quoted 8 lines
lib/opp.c new file +2 -0
new file mode 100644
index 0000000..650c8c3
@@ -0,0 +1,440 @@
+/*
+ * Generic OPP Interface

[...]

Quoted 6 lines
Quoted 6 lines
lib/opp.c new file +6 -0
+
+/**
+ * struct device_opp - Device opp structure
+ * @node:    list node
+ * @dev:     device handle
+ * @opp_list:        list of opps
Quoted 8 lines
Quoted 6 lines

+ * @opp_count: num opps
+ * @enabled_opp_count: how many opps are actually enabled
+ *
+ * This is an internal datastructure maintaining the link to
+ * opps attached to a domain device. This structure is not
+ * meant to be shared with users as it private to opp layer.

"... as it is private ..."

thanks. will fix.
[..]

Quoted 15 lines

lib/opp.c line 105

Quoted 12 lines
lib/opp.c new file +12 -0
+/**
+ * opp_get_voltage() - Gets the voltage corresponding to an opp
+ * @opp:     opp for which voltage has to be returned for
+ *
+ * Return voltage in micro volt corresponding to the opp, else
+ * return 0
+ */
+unsigned long opp_get_voltage(const struct opp *opp)
+{
+     if (unlikely(!opp || IS_ERR(opp)) || !opp->enabled) {
+             pr_err("%s: Invalid parameters being passed\n", __func__);
+             return 0;

Shouldn't the case for !opp->enabled just return 0, without error reporting.

Quoted 3 lines

IMHO, having an OPP disabled is not really a bug in params, and returning this, will misinform the user.. I think.

What do you think?

I think we discussed it a little earlier in the chain
http://marc.info/?t=128152609200064&r=1&w=2
the operations of get_freq and get_voltage dont make sense to be used on a disabled OPP. if we are using it so, we need to report an error to the user and log it for debugging.

The intent is as follows:
a) find_freq_exact is the function to use for finding the opp which is disabled (without the opp pointer, you cant query voltage and frequency anyways).
b) to query this, you need to know the opp frequency anyways, so it makes no usage sense for get_freq or get_voltage to be able to be using a disabled opp.

Do we think it makes sense to add this info to the documentation as well?

Quoted 12 lines
Quoted 12 lines
lib/opp.c new file +11 -0
+     }
+
+     return opp->u_volt;
+}
+
+/**
+ * opp_get_freq() - Gets the frequency corresponding to an opp
+ * @opp:     opp for which frequency has to be returned for
+ *
+ * Return frequency in hertz corresponding to the opp, else
+ * return 0
Quoted 8 lines

lib/opp.c line 122

Quoted 6 lines

+ */

lib/opp.c new file +5 -0
+unsigned long opp_get_freq(const struct opp *opp)
+{
+     if (unlikely(!opp || IS_ERR(opp)) || !opp->enabled) {
+             pr_err("%s: Invalid parameters being passed\n", __func__);
+             return 0;

Similarly here.

commented above.

Quoted 13 lines

lib/opp.c

Quoted 11 lines
lib/opp.c new file +10 -0
+     }
+
+     return opp->rate;
+}
+
+/**
+ * opp_get_opp_count() - Get number of opps enabled in the opp list
+ * @dev:     device for which we do this operation
+ *
+ * This functions returns the number of opps if there are any OPPs

"This function returns.."

Thanks. will fix in v2 post.

Quoted 35 lines
Quoted 24 lines

enabled,
+ * else returns corresponding error value.
+ */

lib/opp.c new file +18 -0
+int opp_get_opp_count(struct device *dev)
+{
+     struct device_opp *dev_opp;
+
+     dev_opp = find_device_opp(dev);
+     if (IS_ERR(dev_opp))
+             return -ENODEV;
+
+     return dev_opp->enabled_opp_count;
+}
+
+/**
+ * opp_find_freq_exact() - search for an exact frequency
+ * @dev:     device for which we do this operation
+ * @freq:    frequency to search for
+ * @enabled: enabled/disabled OPP to search for
+ *
+ * Searches for exact match in the opp list and returns handle to the

matching
+ * opp if found, else returns ERR_PTR in case of error and should be

Quoted 8 lines

handled
+ * using IS_ERR.
+ *
+ * Note: enabled is a modifier for the search. if enabled=true, then the match
+ * is for exact matching frequency and is enabled. if false, the match is for
+ * exact frequency which is disabled.

That enabled var is ignored, which makes this explanation mismatch with the code behavior.

Quoted 1 line

Am I missing something?

ouch.. you are correct.. it is a mistake that got introduced in one of the merges we did.. the matching check
if (temp_opp->enabled && temp_opp->rate == freq) {
should have been
if (temp_opp->enabled == enabled && temp_opp->rate == freq) {

Thanks for catching it.

Quoted 26 lines
Quoted 26 lines

+ */

lib/opp.c new file +24 -0
+struct opp *opp_find_freq_exact(struct device *dev,
+                                  unsigned long freq, bool enabled)
+{
+     struct device_opp *dev_opp;
+     struct opp *temp_opp, *opp = ERR_PTR(-ENODEV);
+
+     dev_opp = find_device_opp(dev);
+     if (IS_ERR(dev_opp))
+             return opp;
+
+     list_for_each_entry(temp_opp, &dev_opp->opp_list, node) {
+             if (temp_opp->enabled && temp_opp->rate == freq) {
+                     opp = temp_opp;
+                     break;
+             }
+     }
+
+     return opp;
+}
+
+/**
+ * opp_find_freq_ceil() - Search for an rounded ceil freq
+ * @dev:     device for which we do this operation
+ * @freq:    Start frequency
Quoted 8 lines
Quoted 6 lines

+ *
+ * Search for the matching ceil *enabled* OPP from a starting freq
+ * for a domain.
+ *
+ * Returns *opp and *freq is populated with the match, else
+ * returns NULL opp if no match, else returns ERR_PTR in case of error.

By looking at the code below, I don't think returning NULL is a possibility.

yet another documentation error.. thanks for catching. ERR_PTR(-ENODEV) is returned instead of NULL..

how about "
Returns *opp and *freq is populated with the match if found, else returns ERR_PTR in case of error and should be handled using IS_ERR.

"

Quoted 23 lines
Quoted 23 lines

+ *
+ * Example usages:

+ *   * find match/next highest available frequency *
+ *   freq = 350000;
+ *   opp = opp_find_freq_ceil(dev, &freq))
+ *   if (IS_ERR(opp))

+ * pr_err("unable to find a higher frequency\n");
+ * else
+ * pr_info("match freq = %ld\n", freq);
+ *

+ *   * print all supported frequencies in ascending order *
+ *   freq = 0; * Search for the lowest enabled frequency *
+ *   while (!IS_ERR(opp = opp_find_freq_ceil(OPP_MPU, &freq)) {

+ * pr_info("freq = %ld\n", freq);
+ * freq++; * for next higher match *
+ * }
+ */

lib/opp.c new file +5 -0
+struct opp *opp_find_freq_ceil(struct device *dev, unsigned long *freq)
+{
+     struct device_opp *dev_opp;
+     struct opp *temp_opp, *opp = ERR_PTR(-ENODEV);
+
Quoted 8 lines

lib/opp.c line 217

Quoted 6 lines
lib/opp.c new file +6 -0
+     dev_opp = find_device_opp(dev);
+     if (IS_ERR(dev_opp))
+             return opp;
+
+     list_for_each_entry(temp_opp, &dev_opp->opp_list, node) {
+             if (temp_opp->enabled && temp_opp->rate >= *freq) {

What if freq contains a NULL pointer?

thanks.. good catch.. should have checked for dev and freq on entry.. added in the to be posted v2

Quoted 14 lines

lib/opp.c line 228

Quoted 12 lines
lib/opp.c new file +11 -0
+                     opp = temp_opp;
+                     *freq = opp->rate;
+                     break;
+             }
+     }
+
+     return opp;
+}
+
+/**
+ * opp_find_freq_floor() - Search for an rounded floor freq

"... Search for a rounded floor ..."

Thanks.. will fix in v2.

Quoted 11 lines
Quoted 9 lines

+ * @dev: device for which we do this operation
+ * @freq: Start frequency
+ *
+ * Search for the matching floor *enabled* OPP from a starting freq
+ * for a domain.
+ *
+ * Returns *opp and *freq is populated with the next match, else
+ * returns NULL opp if no match, else returns ERR_PTR in case of error.

Similar comment about opp not being ever NULL as the function above.

Suggest replacing with:
Returns *opp and *freq is populated with the match if found, else returns ERR_PTR in case of error and should be handled using IS_ERR.

[..]

Signature · 2 lines
Regards,
Nishanth Menon
#14 lore raw

PATCH 1/1 opp: introduce library for device-specific OPPs

Hi Nishanth,

Quoted 13 lines

-----Original Message-----
From: Menon, Nishanth
Sent: Friday, September 17, 2010 10:30 AM
To: Aguirre, Sergio
Cc: linux-arm; lkml; Len Brown; Pavel Machek; Rafael J. Wysocki; Randy Dunlap; Jesse Barnes; Matthew Garrett; H. Peter Anvin; Andrew Morton; Benjamin Herrenschmidt; Martin K. Petersen; Andi Kleen; linux-pm; linux- doc; linux-omap; Cousson, Benoit; Chikkature Rajashekar, Madhusudhan; Phil Carmody; Granados Dorado, Roberto; Shilimkar, Santosh; Tero Kristo; Eduardo Valentin; Paul Walmsley; Romit Dasgupta; Premi, Sanjeev; Gopinath, Thara; Sripathy, Vishwanath; Linus Walleij; Kevin Hilman
Subject: Re: [PATCH] opp: introduce library for device-specific OPPs

Quoted 4 lines

Aguirre, Sergio had written, on 09/17/2010 09:09 AM, the following:

Quoted 1 line

Hi Nishanth,

thanks for the review.. comments below..

You're welcome!

My responses below.

Quoted 18 lines

[...]

Quoted 8 lines
Quoted 8 lines
include/linux/opp.h new file +2 -0
new file mode 100644
index 0000000..94a552b
@@ -0,0 +1,136 @@
+/*
+ * Generic OPP Interface
Quoted 8 lines

include/linux/opp.h line 8

Quoted 6 lines
include/linux/opp.h new file +6 -0
+ *
+ * Copyright (C) 2009-2010 Texas Instruments Incorporated.
+ *   Nishanth Menon
+ *   Romit Dasgupta <[email protected]>
+ * Copyright (C) 2009 Deep Root Systems, LLC.
+ *   Kevin Hilman

Just curious why only Romit's e-mail address is there, and not Kevin's

Quoted 5 lines

or

Quoted 1 line

yours...

personal choice I guess, Romit prefered his email id added Kevin and I did not.

OK. No problem then :)

Quoted 56 lines

[...]

Quoted 8 lines
Quoted 8 lines
lib/opp.c new file +2 -0
new file mode 100644
index 0000000..650c8c3
@@ -0,0 +1,440 @@
+/*
+ * Generic OPP Interface

[...]

Quoted 6 lines
Quoted 6 lines
lib/opp.c new file +6 -0
+
+/**
+ * struct device_opp - Device opp structure
+ * @node:    list node
+ * @dev:     device handle
+ * @opp_list:        list of opps
Quoted 8 lines
Quoted 6 lines

+ * @opp_count: num opps
+ * @enabled_opp_count: how many opps are actually enabled
+ *
+ * This is an internal datastructure maintaining the link to
+ * opps attached to a domain device. This structure is not
+ * meant to be shared with users as it private to opp layer.

"... as it is private ..."

thanks. will fix.
[..]

Quoted 11 lines

lib/opp.c

Quoted 11 lines
lib/opp.c new file +11 -0
+/**
+ * opp_get_voltage() - Gets the voltage corresponding to an opp
+ * @opp:     opp for which voltage has to be returned for
+ *
+ * Return voltage in micro volt corresponding to the opp, else
+ * return 0
+ */
+unsigned long opp_get_voltage(const struct opp *opp)
+{
+     if (unlikely(!opp || IS_ERR(opp)) || !opp->enabled) {
+             pr_err("%s: Invalid parameters being passed\n",

__func__);

Quoted 3 lines
Quoted 1 line

+ return 0;

Shouldn't the case for !opp->enabled just return 0, without error

reporting.

Quoted 2 lines

IMHO, having an OPP disabled is not really a bug in params, and

returning this, will misinform the user.. I think.

Quoted 2 lines

What do you think?

I think we discussed it a little earlier in the chain
http://marc.info/?t=128152609200064&r=1&w=2
the operations of get_freq and get_voltage dont make sense to be used on a disabled OPP. if we are using it so, we need to report an error to the user and log it for debugging.

Quoted 7 lines

The intent is as follows:
a) find_freq_exact is the function to use for finding the opp which is disabled (without the opp pointer, you cant query voltage and frequency anyways).
b) to query this, you need to know the opp frequency anyways, so it makes no usage sense for get_freq or get_voltage to be able to be using a disabled opp.

Ok. I understand now.

Quoted 2 lines

Do we think it makes sense to add this info to the documentation as well?

Yeah, I think that would be good to see explained in the docs.

Quoted 19 lines
Quoted 18 lines
Quoted 8 lines
lib/opp.c new file +6 -0
+     }
+
+     return opp->u_volt;
+}
+
+/**

lib/opp.c

Quoted 10 lines
lib/opp.c new file +10 -0
+ * opp_get_freq() - Gets the frequency corresponding to an opp
+ * @opp:     opp for which frequency has to be returned for
+ *
+ * Return frequency in hertz corresponding to the opp, else
+ * return 0
+ */
+unsigned long opp_get_freq(const struct opp *opp)
+{
+     if (unlikely(!opp || IS_ERR(opp)) || !opp->enabled) {
+             pr_err("%s: Invalid parameters being passed\n",

__func__);

Quoted 4 lines
Quoted 3 lines
Quoted 1 line

+ return 0;

Similarly here.

commented above.

Ok.

Quoted 102 lines
Quoted 14 lines

lib/opp.c

Quoted 12 lines
lib/opp.c new file +10 -0
+     }
+
+     return opp->rate;
+}
+
+/**
+ * opp_get_opp_count() - Get number of opps enabled in the opp list
+ * @dev:     device for which we do this operation
+ *
+ * This functions returns the number of opps if there are any OPPs

"This function returns.."

Thanks. will fix in v2 post.

Quoted 20 lines
Quoted 20 lines

enabled,
+ * else returns corresponding error value.
+ */

lib/opp.c new file +16 -0
+int opp_get_opp_count(struct device *dev)
+{
+     struct device_opp *dev_opp;
+
+     dev_opp = find_device_opp(dev);
+     if (IS_ERR(dev_opp))
+             return -ENODEV;
+
+     return dev_opp->enabled_opp_count;
+}
+
+/**
+ * opp_find_freq_exact() - search for an exact frequency
+ * @dev:     device for which we do this operation
+ * @freq:    frequency to search for
+ * @enabled: enabled/disabled OPP to search for
Quoted 8 lines
Quoted 8 lines

+ *
+ * Searches for exact match in the opp list and returns handle to the matching
+ * opp if found, else returns ERR_PTR in case of error and should be handled
+ * using IS_ERR.
+ *
+ * Note: enabled is a modifier for the search. if enabled=true, then

the

Quoted 2 lines
Quoted 2 lines

match
+ * is for exact matching frequency and is enabled. if false, the match

is

Quoted 4 lines
Quoted 2 lines

for
+ * exact frequency which is disabled.

That enabled var is ignored, which makes this explanation mismatch with

the code behavior.

Quoted 2 lines

Am I missing something?

ouch.. you are correct.. it is a mistake that got introduced in one of the merges we did.. the matching check
if (temp_opp->enabled && temp_opp->rate == freq) {
should have been
if (temp_opp->enabled == enabled && temp_opp->rate == freq) {

Thanks for catching it.

Quoted 24 lines
Quoted 24 lines

+ */

lib/opp.c new file +22 -0
+struct opp *opp_find_freq_exact(struct device *dev,
+                                  unsigned long freq, bool enabled)
+{
+     struct device_opp *dev_opp;
+     struct opp *temp_opp, *opp = ERR_PTR(-ENODEV);
+
+     dev_opp = find_device_opp(dev);
+     if (IS_ERR(dev_opp))
+             return opp;
+
+     list_for_each_entry(temp_opp, &dev_opp->opp_list, node) {
+             if (temp_opp->enabled && temp_opp->rate == freq) {
+                     opp = temp_opp;
+                     break;
+             }
+     }
+
+     return opp;
+}
+
+/**
+ * opp_find_freq_ceil() - Search for an rounded ceil freq
Quoted 8 lines
Quoted 8 lines

+ * @dev: device for which we do this operation
+ * @freq: Start frequency
+ *
+ * Search for the matching ceil *enabled* OPP from a starting freq
+ * for a domain.
+ *
+ * Returns *opp and *freq is populated with the match, else
+ * returns NULL opp if no match, else returns ERR_PTR in case of

error.

Quoted 2 lines

By looking at the code below, I don't think returning NULL is a

possibility.
yet another documentation error.. thanks for catching. ERR_PTR(-ENODEV) is returned instead of NULL..

Quoted 5 lines

how about "
Returns *opp and *freq is populated with the match if found, else returns ERR_PTR in case of error and should be handled using IS_ERR.

"

I'll say:

"Returns matching *opp and refreshes *freq accordingly"

But is a matter of taste of words. :)

Quoted 60 lines
Quoted 11 lines
Quoted 11 lines

+ *
+ * Example usages:

+ *   * find match/next highest available frequency *
+ *   freq = 350000;
+ *   opp = opp_find_freq_ceil(dev, &freq))
+ *   if (IS_ERR(opp))

+ * pr_err("unable to find a higher frequency\n");
+ * else
+ * pr_info("match freq = %ld\n", freq);
+ *

Quoted 8 lines
Quoted 8 lines
+ *   * print all supported frequencies in ascending order *
+ *   freq = 0; * Search for the lowest enabled frequency *
+ *   while (!IS_ERR(opp = opp_find_freq_ceil(OPP_MPU, &freq)) {

+ * pr_info("freq = %ld\n", freq);
+ * freq++; * for next higher match *
+ * }
+ */
+struct opp *opp_find_freq_ceil(struct device *dev, unsigned long

*freq)

Quoted 4 lines
Quoted 4 lines
lib/opp.c new file +4 -0
+{
+     struct device_opp *dev_opp;
+     struct opp *temp_opp, *opp = ERR_PTR(-ENODEV);
+
Quoted 8 lines

lib/opp.c line 217

Quoted 6 lines
lib/opp.c new file +6 -0
+     dev_opp = find_device_opp(dev);
+     if (IS_ERR(dev_opp))
+             return opp;
+
+     list_for_each_entry(temp_opp, &dev_opp->opp_list, node) {
+             if (temp_opp->enabled && temp_opp->rate >= *freq) {

What if freq contains a NULL pointer?

thanks.. good catch.. should have checked for dev and freq on entry.. added in the to be posted v2

Quoted 14 lines

lib/opp.c line 228

Quoted 12 lines
lib/opp.c new file +11 -0
+                     opp = temp_opp;
+                     *freq = opp->rate;
+                     break;
+             }
+     }
+
+     return opp;
+}
+
+/**
+ * opp_find_freq_floor() - Search for an rounded floor freq

"... Search for a rounded floor ..."

Thanks.. will fix in v2.

Quoted 9 lines
Quoted 9 lines

+ * @dev: device for which we do this operation
+ * @freq: Start frequency
+ *
+ * Search for the matching floor *enabled* OPP from a starting freq
+ * for a domain.
+ *
+ * Returns *opp and *freq is populated with the next match, else
+ * returns NULL opp if no match, else returns ERR_PTR in case of

Quoted 7 lines

error.

Quoted 2 lines

Similar comment about opp not being ever NULL as the function above.

Suggest replacing with:
Returns *opp and *freq is populated with the match if found, else returns ERR_PTR in case of error and should be handled using IS_ERR.

Same comment as above.

Regards,
Sergio

Quoted 6 lines

[..]

--
Regards,
Nishanth Menon

#15 lore raw
In reply to Nishanth Menon

Hi Nishanth,

Quoted 13 lines

-----Original Message-----
From: Menon, Nishanth
Sent: Friday, September 17, 2010 10:30 AM
To: Aguirre, Sergio
Cc: linux-arm; lkml; Len Brown; Pavel Machek; Rafael J. Wysocki; Randy Dunlap; Jesse Barnes; Matthew Garrett; H. Peter Anvin; Andrew Morton; Benjamin Herrenschmidt; Martin K. Petersen; Andi Kleen; linux-pm; linux- doc; linux-omap; Cousson, Benoit; Chikkature Rajashekar, Madhusudhan; Phil Carmody; Granados Dorado, Roberto; Shilimkar, Santosh; Tero Kristo; Eduardo Valentin; Paul Walmsley; Romit Dasgupta; Premi, Sanjeev; Gopinath, Thara; Sripathy, Vishwanath; Linus Walleij; Kevin Hilman
Subject: Re: [PATCH] opp: introduce library for device-specific OPPs

Quoted 4 lines

Aguirre, Sergio had written, on 09/17/2010 09:09 AM, the following:

Quoted 1 line

Hi Nishanth,

thanks for the review.. comments below..

You're welcome!

My responses below.

Quoted 18 lines

[...]

Quoted 8 lines
Quoted 8 lines
include/linux/opp.h new file +2 -0
new file mode 100644
index 0000000..94a552b
@@ -0,0 +1,136 @@
+/*
+ * Generic OPP Interface
Quoted 8 lines

include/linux/opp.h line 8

Quoted 6 lines
include/linux/opp.h new file +6 -0
+ *
+ * Copyright (C) 2009-2010 Texas Instruments Incorporated.
+ *   Nishanth Menon
+ *   Romit Dasgupta <[email protected]>
+ * Copyright (C) 2009 Deep Root Systems, LLC.
+ *   Kevin Hilman

Just curious why only Romit's e-mail address is there, and not Kevin's

Quoted 5 lines

or

Quoted 1 line

yours...

personal choice I guess, Romit prefered his email id added Kevin and I did not.

OK. No problem then :)

Quoted 56 lines

[...]

Quoted 8 lines
Quoted 8 lines
lib/opp.c new file +2 -0
new file mode 100644
index 0000000..650c8c3
@@ -0,0 +1,440 @@
+/*
+ * Generic OPP Interface

[...]

Quoted 6 lines
Quoted 6 lines
lib/opp.c new file +6 -0
+
+/**
+ * struct device_opp - Device opp structure
+ * @node:    list node
+ * @dev:     device handle
+ * @opp_list:        list of opps
Quoted 8 lines
Quoted 6 lines

+ * @opp_count: num opps
+ * @enabled_opp_count: how many opps are actually enabled
+ *
+ * This is an internal datastructure maintaining the link to
+ * opps attached to a domain device. This structure is not
+ * meant to be shared with users as it private to opp layer.

"... as it is private ..."

thanks. will fix.
[..]

Quoted 11 lines

lib/opp.c

Quoted 11 lines
lib/opp.c new file +11 -0
+/**
+ * opp_get_voltage() - Gets the voltage corresponding to an opp
+ * @opp:     opp for which voltage has to be returned for
+ *
+ * Return voltage in micro volt corresponding to the opp, else
+ * return 0
+ */
+unsigned long opp_get_voltage(const struct opp *opp)
+{
+     if (unlikely(!opp || IS_ERR(opp)) || !opp->enabled) {
+             pr_err("%s: Invalid parameters being passed\n",

__func__);

Quoted 3 lines
Quoted 1 line

+ return 0;

Shouldn't the case for !opp->enabled just return 0, without error

reporting.

Quoted 2 lines

IMHO, having an OPP disabled is not really a bug in params, and

returning this, will misinform the user.. I think.

Quoted 2 lines

What do you think?

I think we discussed it a little earlier in the chain
http://marc.info/?t=128152609200064&r=1&w=2
the operations of get_freq and get_voltage dont make sense to be used on a disabled OPP. if we are using it so, we need to report an error to the user and log it for debugging.

Quoted 7 lines

The intent is as follows:
a) find_freq_exact is the function to use for finding the opp which is disabled (without the opp pointer, you cant query voltage and frequency anyways).
b) to query this, you need to know the opp frequency anyways, so it makes no usage sense for get_freq or get_voltage to be able to be using a disabled opp.

Ok. I understand now.

Quoted 2 lines

Do we think it makes sense to add this info to the documentation as well?

Yeah, I think that would be good to see explained in the docs.

Quoted 19 lines
Quoted 18 lines
Quoted 8 lines
lib/opp.c new file +6 -0
+     }
+
+     return opp->u_volt;
+}
+
+/**

lib/opp.c

Quoted 10 lines
lib/opp.c new file +10 -0
+ * opp_get_freq() - Gets the frequency corresponding to an opp
+ * @opp:     opp for which frequency has to be returned for
+ *
+ * Return frequency in hertz corresponding to the opp, else
+ * return 0
+ */
+unsigned long opp_get_freq(const struct opp *opp)
+{
+     if (unlikely(!opp || IS_ERR(opp)) || !opp->enabled) {
+             pr_err("%s: Invalid parameters being passed\n",

__func__);

Quoted 4 lines
Quoted 3 lines
Quoted 1 line

+ return 0;

Similarly here.

commented above.

Ok.

Quoted 102 lines
Quoted 14 lines

lib/opp.c

Quoted 12 lines
lib/opp.c new file +10 -0
+     }
+
+     return opp->rate;
+}
+
+/**
+ * opp_get_opp_count() - Get number of opps enabled in the opp list
+ * @dev:     device for which we do this operation
+ *
+ * This functions returns the number of opps if there are any OPPs

"This function returns.."

Thanks. will fix in v2 post.

Quoted 20 lines
Quoted 20 lines

enabled,
+ * else returns corresponding error value.
+ */

lib/opp.c new file +16 -0
+int opp_get_opp_count(struct device *dev)
+{
+     struct device_opp *dev_opp;
+
+     dev_opp = find_device_opp(dev);
+     if (IS_ERR(dev_opp))
+             return -ENODEV;
+
+     return dev_opp->enabled_opp_count;
+}
+
+/**
+ * opp_find_freq_exact() - search for an exact frequency
+ * @dev:     device for which we do this operation
+ * @freq:    frequency to search for
+ * @enabled: enabled/disabled OPP to search for
Quoted 8 lines
Quoted 8 lines

+ *
+ * Searches for exact match in the opp list and returns handle to the matching
+ * opp if found, else returns ERR_PTR in case of error and should be handled
+ * using IS_ERR.
+ *
+ * Note: enabled is a modifier for the search. if enabled=true, then

the

Quoted 2 lines
Quoted 2 lines

match
+ * is for exact matching frequency and is enabled. if false, the match

is

Quoted 4 lines
Quoted 2 lines

for
+ * exact frequency which is disabled.

That enabled var is ignored, which makes this explanation mismatch with

the code behavior.

Quoted 2 lines

Am I missing something?

ouch.. you are correct.. it is a mistake that got introduced in one of the merges we did.. the matching check
if (temp_opp->enabled && temp_opp->rate == freq) {
should have been
if (temp_opp->enabled == enabled && temp_opp->rate == freq) {

Thanks for catching it.

Quoted 24 lines
Quoted 24 lines

+ */

lib/opp.c new file +22 -0
+struct opp *opp_find_freq_exact(struct device *dev,
+                                  unsigned long freq, bool enabled)
+{
+     struct device_opp *dev_opp;
+     struct opp *temp_opp, *opp = ERR_PTR(-ENODEV);
+
+     dev_opp = find_device_opp(dev);
+     if (IS_ERR(dev_opp))
+             return opp;
+
+     list_for_each_entry(temp_opp, &dev_opp->opp_list, node) {
+             if (temp_opp->enabled && temp_opp->rate == freq) {
+                     opp = temp_opp;
+                     break;
+             }
+     }
+
+     return opp;
+}
+
+/**
+ * opp_find_freq_ceil() - Search for an rounded ceil freq
Quoted 8 lines
Quoted 8 lines

+ * @dev: device for which we do this operation
+ * @freq: Start frequency
+ *
+ * Search for the matching ceil *enabled* OPP from a starting freq
+ * for a domain.
+ *
+ * Returns *opp and *freq is populated with the match, else
+ * returns NULL opp if no match, else returns ERR_PTR in case of

error.

Quoted 2 lines

By looking at the code below, I don't think returning NULL is a

possibility.
yet another documentation error.. thanks for catching. ERR_PTR(-ENODEV) is returned instead of NULL..

Quoted 5 lines

how about "
Returns *opp and *freq is populated with the match if found, else returns ERR_PTR in case of error and should be handled using IS_ERR.

"

I'll say:

"Returns matching *opp and refreshes *freq accordingly"

But is a matter of taste of words. :)

Quoted 60 lines
Quoted 11 lines
Quoted 11 lines

+ *
+ * Example usages:

+ *   * find match/next highest available frequency *
+ *   freq = 350000;
+ *   opp = opp_find_freq_ceil(dev, &freq))
+ *   if (IS_ERR(opp))

+ * pr_err("unable to find a higher frequency\n");
+ * else
+ * pr_info("match freq = %ld\n", freq);
+ *

Quoted 8 lines
Quoted 8 lines
+ *   * print all supported frequencies in ascending order *
+ *   freq = 0; * Search for the lowest enabled frequency *
+ *   while (!IS_ERR(opp = opp_find_freq_ceil(OPP_MPU, &freq)) {

+ * pr_info("freq = %ld\n", freq);
+ * freq++; * for next higher match *
+ * }
+ */
+struct opp *opp_find_freq_ceil(struct device *dev, unsigned long

*freq)

Quoted 4 lines
Quoted 4 lines
lib/opp.c new file +4 -0
+{
+     struct device_opp *dev_opp;
+     struct opp *temp_opp, *opp = ERR_PTR(-ENODEV);
+
Quoted 8 lines

lib/opp.c line 217

Quoted 6 lines
lib/opp.c new file +6 -0
+     dev_opp = find_device_opp(dev);
+     if (IS_ERR(dev_opp))
+             return opp;
+
+     list_for_each_entry(temp_opp, &dev_opp->opp_list, node) {
+             if (temp_opp->enabled && temp_opp->rate >= *freq) {

What if freq contains a NULL pointer?

thanks.. good catch.. should have checked for dev and freq on entry.. added in the to be posted v2

Quoted 14 lines

lib/opp.c line 228

Quoted 12 lines
lib/opp.c new file +11 -0
+                     opp = temp_opp;
+                     *freq = opp->rate;
+                     break;
+             }
+     }
+
+     return opp;
+}
+
+/**
+ * opp_find_freq_floor() - Search for an rounded floor freq

"... Search for a rounded floor ..."

Thanks.. will fix in v2.

Quoted 9 lines
Quoted 9 lines

+ * @dev: device for which we do this operation
+ * @freq: Start frequency
+ *
+ * Search for the matching floor *enabled* OPP from a starting freq
+ * for a domain.
+ *
+ * Returns *opp and *freq is populated with the next match, else
+ * returns NULL opp if no match, else returns ERR_PTR in case of

Quoted 7 lines

error.

Quoted 2 lines

Similar comment about opp not being ever NULL as the function above.

Suggest replacing with:
Returns *opp and *freq is populated with the match if found, else returns ERR_PTR in case of error and should be handled using IS_ERR.

Same comment as above.

Regards,
Sergio

Quoted 6 lines

[..]

--
Regards,
Nishanth Menon

#16 lore raw

PATCH 1/1 opp: introduce library for device-specific OPPs

In reply to Nishanth Menon

Nishanth,

<snip>

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 6 lines
include/linux/opp.h new file +0 -0
new file mode 100644
index 0000000..94a552b

include/linux/opp.h line 8

Quoted 8 lines
include/linux/opp.h new file +8 -0
@@ -0,0 +1,136 @@
+/*
+ * Generic OPP Interface
+ *
+ * Copyright (C) 2009-2010 Texas Instruments Incorporated.
+ *   Nishanth Menon
+ *   Romit Dasgupta <[email protected]>
+ * Copyright (C) 2009 Deep Root Systems, LLC.
+ *   Kevin Hilman

Just curious why only Romit's e-mail address is there, and not Kevin's

Quoted 7 lines
Quoted 5 lines

or

Quoted 1 line

yours...

personal choice I guess, Romit prefered his email id added Kevin and I did not.

OK. No problem then :)

Actually, Romit's e-mail address seems to be invalid... Is he still having that address? (OR is he still working in TI? 8-| )

Regards,
Sergio

<snip>

#17 lore raw

PATCH 1/1 opp: introduce library for device-specific OPPs

Aguirre, Sergio had written, on 09/17/2010 11:15 AM, the following:

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 6 lines
include/linux/opp.h new file +0 -0
new file mode 100644
index 0000000..94a552b

include/linux/opp.h line 8

Quoted 8 lines
include/linux/opp.h new file +8 -0
@@ -0,0 +1,136 @@
+/*
+ * Generic OPP Interface
+ *
+ * Copyright (C) 2009-2010 Texas Instruments Incorporated.
+ *   Nishanth Menon
+ *   Romit Dasgupta <[email protected]>
+ * Copyright (C) 2009 Deep Root Systems, LLC.
+ *   Kevin Hilman

Just curious why only Romit's e-mail address is there, and not Kevin's

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines

or

Quoted 1 line

yours...

personal choice I guess, Romit prefered his email id added Kevin and I did not.

OK. No problem then :)

Actually, Romit's e-mail address seems to be invalid... Is he still having that address? (OR is he still working in TI? 8-| )

On an internal thread, I am told to remove the email id - so will remove Romit's email id and cc from v2 patch.

Signature · 2 lines
Regards,
Nishanth Menon
#18 lore raw
In reply to Aguirre, Sergio

Aguirre, Sergio had written, on 09/17/2010 11:15 AM, the following:

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 6 lines
include/linux/opp.h new file +0 -0
new file mode 100644
index 0000000..94a552b

include/linux/opp.h line 8

Quoted 8 lines
include/linux/opp.h new file +8 -0
@@ -0,0 +1,136 @@
+/*
+ * Generic OPP Interface
+ *
+ * Copyright (C) 2009-2010 Texas Instruments Incorporated.
+ *   Nishanth Menon
+ *   Romit Dasgupta <[email protected]>
+ * Copyright (C) 2009 Deep Root Systems, LLC.
+ *   Kevin Hilman

Just curious why only Romit's e-mail address is there, and not Kevin's

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines

or

Quoted 1 line

yours...

personal choice I guess, Romit prefered his email id added Kevin and I did not.

OK. No problem then :)

Actually, Romit's e-mail address seems to be invalid... Is he still having that address? (OR is he still working in TI? 8-| )

On an internal thread, I am told to remove the email id - so will remove Romit's email id and cc from v2 patch.

Signature · 2 lines
Regards,
Nishanth Menon
#19 lore raw
In reply to Nishanth Menon

Nishanth,

<snip>

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 6 lines
include/linux/opp.h new file +0 -0
new file mode 100644
index 0000000..94a552b

include/linux/opp.h line 8

Quoted 8 lines
include/linux/opp.h new file +8 -0
@@ -0,0 +1,136 @@
+/*
+ * Generic OPP Interface
+ *
+ * Copyright (C) 2009-2010 Texas Instruments Incorporated.
+ *   Nishanth Menon
+ *   Romit Dasgupta <[email protected]>
+ * Copyright (C) 2009 Deep Root Systems, LLC.
+ *   Kevin Hilman

Just curious why only Romit's e-mail address is there, and not Kevin's

Quoted 7 lines
Quoted 5 lines

or

Quoted 1 line

yours...

personal choice I guess, Romit prefered his email id added Kevin and I did not.

OK. No problem then :)

Actually, Romit's e-mail address seems to be invalid... Is he still having that address? (OR is he still working in TI? 8-| )

Regards,
Sergio

<snip>

#20 lore raw
In reply to Aguirre, Sergio

Aguirre, Sergio had written, on 09/17/2010 09:09 AM, the following:

Quoted 1 line

Hi Nishanth,

thanks for the review.. comments below..

[...]

Quoted 9 lines
Quoted 9 lines
include/linux/opp.h new file +3 -0
new file mode 100644
index 0000000..94a552b
@@ -0,0 +1,136 @@
+/*
+ * Generic OPP Interface
+ *
Quoted 8 lines

include/linux/opp.h line 8

Quoted 5 lines
include/linux/opp.h new file +5 -0
+ * Copyright (C) 2009-2010 Texas Instruments Incorporated.
+ *   Nishanth Menon
+ *   Romit Dasgupta <[email protected]>
+ * Copyright (C) 2009 Deep Root Systems, LLC.
+ *   Kevin Hilman

Just curious why only Romit's e-mail address is there, and not Kevin's or yours...

personal choice I guess, Romit prefered his email id added Kevin and I did not.

[...]

Quoted 8 lines
Quoted 8 lines
lib/opp.c new file +2 -0
new file mode 100644
index 0000000..650c8c3
@@ -0,0 +1,440 @@
+/*
+ * Generic OPP Interface

[...]

Quoted 6 lines
Quoted 6 lines
lib/opp.c new file +6 -0
+
+/**
+ * struct device_opp - Device opp structure
+ * @node:    list node
+ * @dev:     device handle
+ * @opp_list:        list of opps
Quoted 8 lines
Quoted 6 lines

+ * @opp_count: num opps
+ * @enabled_opp_count: how many opps are actually enabled
+ *
+ * This is an internal datastructure maintaining the link to
+ * opps attached to a domain device. This structure is not
+ * meant to be shared with users as it private to opp layer.

"... as it is private ..."

thanks. will fix.
[..]

Quoted 15 lines

lib/opp.c line 105

Quoted 12 lines
lib/opp.c new file +12 -0
+/**
+ * opp_get_voltage() - Gets the voltage corresponding to an opp
+ * @opp:     opp for which voltage has to be returned for
+ *
+ * Return voltage in micro volt corresponding to the opp, else
+ * return 0
+ */
+unsigned long opp_get_voltage(const struct opp *opp)
+{
+     if (unlikely(!opp || IS_ERR(opp)) || !opp->enabled) {
+             pr_err("%s: Invalid parameters being passed\n", __func__);
+             return 0;

Shouldn't the case for !opp->enabled just return 0, without error reporting.

Quoted 3 lines

IMHO, having an OPP disabled is not really a bug in params, and returning this, will misinform the user.. I think.

What do you think?

I think we discussed it a little earlier in the chain
http://marc.info/?t=128152609200064&r=1&w=2
the operations of get_freq and get_voltage dont make sense to be used on a disabled OPP. if we are using it so, we need to report an error to the user and log it for debugging.

The intent is as follows:
a) find_freq_exact is the function to use for finding the opp which is disabled (without the opp pointer, you cant query voltage and frequency anyways).
b) to query this, you need to know the opp frequency anyways, so it makes no usage sense for get_freq or get_voltage to be able to be using a disabled opp.

Do we think it makes sense to add this info to the documentation as well?

Quoted 12 lines
Quoted 12 lines
lib/opp.c new file +11 -0
+     }
+
+     return opp->u_volt;
+}
+
+/**
+ * opp_get_freq() - Gets the frequency corresponding to an opp
+ * @opp:     opp for which frequency has to be returned for
+ *
+ * Return frequency in hertz corresponding to the opp, else
+ * return 0
Quoted 8 lines

lib/opp.c line 122

Quoted 6 lines

+ */

lib/opp.c new file +5 -0
+unsigned long opp_get_freq(const struct opp *opp)
+{
+     if (unlikely(!opp || IS_ERR(opp)) || !opp->enabled) {
+             pr_err("%s: Invalid parameters being passed\n", __func__);
+             return 0;

Similarly here.

commented above.

Quoted 13 lines

lib/opp.c

Quoted 11 lines
lib/opp.c new file +10 -0
+     }
+
+     return opp->rate;
+}
+
+/**
+ * opp_get_opp_count() - Get number of opps enabled in the opp list
+ * @dev:     device for which we do this operation
+ *
+ * This functions returns the number of opps if there are any OPPs

"This function returns.."

Thanks. will fix in v2 post.

Quoted 35 lines
Quoted 24 lines

enabled,
+ * else returns corresponding error value.
+ */

lib/opp.c new file +18 -0
+int opp_get_opp_count(struct device *dev)
+{
+     struct device_opp *dev_opp;
+
+     dev_opp = find_device_opp(dev);
+     if (IS_ERR(dev_opp))
+             return -ENODEV;
+
+     return dev_opp->enabled_opp_count;
+}
+
+/**
+ * opp_find_freq_exact() - search for an exact frequency
+ * @dev:     device for which we do this operation
+ * @freq:    frequency to search for
+ * @enabled: enabled/disabled OPP to search for
+ *
+ * Searches for exact match in the opp list and returns handle to the

matching
+ * opp if found, else returns ERR_PTR in case of error and should be

Quoted 8 lines

handled
+ * using IS_ERR.
+ *
+ * Note: enabled is a modifier for the search. if enabled=true, then the match
+ * is for exact matching frequency and is enabled. if false, the match is for
+ * exact frequency which is disabled.

That enabled var is ignored, which makes this explanation mismatch with the code behavior.

Quoted 1 line

Am I missing something?

ouch.. you are correct.. it is a mistake that got introduced in one of the merges we did.. the matching check
if (temp_opp->enabled && temp_opp->rate == freq) {
should have been
if (temp_opp->enabled == enabled && temp_opp->rate == freq) {

Thanks for catching it.

Quoted 26 lines
Quoted 26 lines

+ */

lib/opp.c new file +24 -0
+struct opp *opp_find_freq_exact(struct device *dev,
+                                  unsigned long freq, bool enabled)
+{
+     struct device_opp *dev_opp;
+     struct opp *temp_opp, *opp = ERR_PTR(-ENODEV);
+
+     dev_opp = find_device_opp(dev);
+     if (IS_ERR(dev_opp))
+             return opp;
+
+     list_for_each_entry(temp_opp, &dev_opp->opp_list, node) {
+             if (temp_opp->enabled && temp_opp->rate == freq) {
+                     opp = temp_opp;
+                     break;
+             }
+     }
+
+     return opp;
+}
+
+/**
+ * opp_find_freq_ceil() - Search for an rounded ceil freq
+ * @dev:     device for which we do this operation
+ * @freq:    Start frequency
Quoted 8 lines
Quoted 6 lines

+ *
+ * Search for the matching ceil *enabled* OPP from a starting freq
+ * for a domain.
+ *
+ * Returns *opp and *freq is populated with the match, else
+ * returns NULL opp if no match, else returns ERR_PTR in case of error.

By looking at the code below, I don't think returning NULL is a possibility.

yet another documentation error.. thanks for catching. ERR_PTR(-ENODEV) is returned instead of NULL..

how about "
Returns *opp and *freq is populated with the match if found, else returns ERR_PTR in case of error and should be handled using IS_ERR.

"

Quoted 23 lines
Quoted 23 lines

+ *
+ * Example usages:

+ *   * find match/next highest available frequency *
+ *   freq = 350000;
+ *   opp = opp_find_freq_ceil(dev, &freq))
+ *   if (IS_ERR(opp))

+ * pr_err("unable to find a higher frequency\n");
+ * else
+ * pr_info("match freq = %ld\n", freq);
+ *

+ *   * print all supported frequencies in ascending order *
+ *   freq = 0; * Search for the lowest enabled frequency *
+ *   while (!IS_ERR(opp = opp_find_freq_ceil(OPP_MPU, &freq)) {

+ * pr_info("freq = %ld\n", freq);
+ * freq++; * for next higher match *
+ * }
+ */

lib/opp.c new file +5 -0
+struct opp *opp_find_freq_ceil(struct device *dev, unsigned long *freq)
+{
+     struct device_opp *dev_opp;
+     struct opp *temp_opp, *opp = ERR_PTR(-ENODEV);
+
Quoted 8 lines

lib/opp.c line 217

Quoted 6 lines
lib/opp.c new file +6 -0
+     dev_opp = find_device_opp(dev);
+     if (IS_ERR(dev_opp))
+             return opp;
+
+     list_for_each_entry(temp_opp, &dev_opp->opp_list, node) {
+             if (temp_opp->enabled && temp_opp->rate >= *freq) {

What if freq contains a NULL pointer?

thanks.. good catch.. should have checked for dev and freq on entry.. added in the to be posted v2

Quoted 14 lines

lib/opp.c line 228

Quoted 12 lines
lib/opp.c new file +11 -0
+                     opp = temp_opp;
+                     *freq = opp->rate;
+                     break;
+             }
+     }
+
+     return opp;
+}
+
+/**
+ * opp_find_freq_floor() - Search for an rounded floor freq

"... Search for a rounded floor ..."

Thanks.. will fix in v2.

Quoted 11 lines
Quoted 9 lines

+ * @dev: device for which we do this operation
+ * @freq: Start frequency
+ *
+ * Search for the matching floor *enabled* OPP from a starting freq
+ * for a domain.
+ *
+ * Returns *opp and *freq is populated with the next match, else
+ * returns NULL opp if no match, else returns ERR_PTR in case of error.

Similar comment about opp not being ever NULL as the function above.

Suggest replacing with:
Returns *opp and *freq is populated with the match if found, else returns ERR_PTR in case of error and should be handled using IS_ERR.

[..]

Signature · 2 lines
Regards,
Nishanth Menon
#21 lore raw
In reply to Nishanth Menon

Hi Nishanth,

Quoted 103 lines

-----Original Message-----
From: Menon, Nishanth
Sent: Thursday, September 16, 2010 8:30 PM
To: linux-arm; lkml
Cc: Len Brown; Pavel Machek; Rafael J. Wysocki; Randy Dunlap; Jesse Barnes; Matthew Garrett; H. Peter Anvin; Andrew Morton; Benjamin Herrenschmidt; Martin K. Petersen; Andi Kleen; linux-pm; linux-doc; linux- omap; Menon, Nishanth; Cousson, Benoit; Chikkature Rajashekar, Madhusudhan; Phil Carmody; Granados Dorado, Roberto; Shilimkar, Santosh; Aguirre, Sergio; Tero Kristo; Eduardo Valentin; Paul Walmsley; Romit Dasgupta; Premi, Sanjeev; Gopinath, Thara; Sripathy, Vishwanath; Linus Walleij; Kevin Hilman
Subject: [PATCH] opp: introduce library for device-specific OPPs

SOCs have a standard set of tuples consisting of frequency and voltage pairs that the device will support per voltage domain. These are called Operating Performance Points or OPPs. The actual definitions of Operating Performance Points varies over silicon within the same family of devices. For a specific domain, you can have a set of {frequency, voltage} pairs. As the kernel boots and more information is available, a set of these are activated based on the precise nature of device the kernel boots up on. It is interesting to remember that each IP which belongs to a voltage domain may define their own set of OPPs on top of this.

To implement an OPP, some sort of power management support is necessary hence this library enablement depends on CONFIG_PM, however this does not fit into the core power framework as it is an independent library. This is hence introduced under lib allowing all architectures to selectively enable the feature based on thier capabilities.

Contributions include:
Sanjeev Premi for the initial concept:

      http://patchwork.kernel.org/patch/50998/

Kevin Hilman for converting original design to device-based Kevin Hilman and Paul Walmsey for cleaning up many of the function abstractions, improvements and data structure handling Romit Dasgupta for using enums instead of opp pointers Thara Gopinath, Eduardo Valentin and Vishwanath BS for fixes and cleanups.
Linus Walleij for recommending this layer be made generic for usage in other architectures beyond OMAP and ARM.

Discussions and comments from:
http://marc.info/?l=linux-omap&m=126033945313269&w=2
http://marc.info/?l=linux-omap&m=125482970102327&w=2
http://marc.info/?t=125809247500002&r=1&w=2
http://marc.info/?l=linux-omap&m=126025973426007&w=2
http://marc.info/?t=128152609200064&r=1&w=2
incorporated.

Cc Benoit Cousson <[email protected]>
Cc Madhusudhan Chikkature Rajashekar <[email protected]>
Cc Phil Carmody <[email protected]>
Cc Roberto Granados Dorado <[email protected]>
Cc Santosh Shilimkar <[email protected]>
Cc Sergio Alberto Aguirre Rodriguez <[email protected]>
Cc Tero Kristo <[email protected]>
Cc Eduardo Valentin <[email protected]>
Cc Paul Walmsley <[email protected]>
Cc Romit Dasgupta <[email protected]>
Cc Sanjeev Premi <[email protected]>
Cc Thara Gopinath <[email protected]>
Cc Vishwanath BS <[email protected]>
Cc Linus Walleij <[email protected]>

---
minor notes:
a) Code rebased to linus's tree commit 03a7ab0
b) Looping in get-maintainter.pl list and lkml as per thread of discussion
http://marc.info/?t=128152609200064&r=1&w=2

 Documentation/power/00-INDEX |    2 +
 include/linux/opp.h          |  136 +++++++++++++
 kernel/power/Kconfig         |   14 ++
 lib/Makefile                 |    2 +
 lib/opp.c                    |  440
++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 594 insertions(+), 0 deletions(-)
 create mode 100644 include/linux/opp.h
 create mode 100644 lib/opp.c
Documentation/power/00-INDEX +2 -0
index fb742c2..45e9d4a 100644
@@ -14,6 +14,8 @@ interface.txt
      - Power management user interface in /sys/power
 notifiers.txt
      - Registering suspend notifiers in device drivers
+opp.txt
+     - Operating Performance Point library
 pci.txt
      - How the PCI Subsystem Does Power Management
 pm_qos_interface.txt
include/linux/opp.h new file +0 -0
new file mode 100644
index 0000000..94a552b

include/linux/opp.h line 8

Quoted 8 lines
include/linux/opp.h new file +8 -0
@@ -0,0 +1,136 @@
+/*
+ * Generic OPP Interface
+ *
+ * Copyright (C) 2009-2010 Texas Instruments Incorporated.
+ *   Nishanth Menon
+ *   Romit Dasgupta <[email protected]>
+ * Copyright (C) 2009 Deep Root Systems, LLC.
+ *   Kevin Hilman

Just curious why only Romit's e-mail address is there, and not Kevin's or yours...

Quoted 221 lines
include/linux/opp.h new file +20 -0
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+#ifndef __ASM_OPP_H
+#define __ASM_OPP_H
+
+#include <linux/err.h>
+#include <linux/cpufreq.h>
+
+/**
+ * struct opp_def - Generic OPP Definition
+ * @freq:    Frequency in hertz corresponding to this OPP
+ * @u_volt:  Nominal voltage in microvolts corresponding to this OPP
+ * @enabled: True/false - is this OPP enabled/disabled by default
+ *
+ * SOCs have a standard set of tuples consisting of frequency and voltage
+ * pairs that the device will support per voltage domain. This is called
+ * Operating Performance Points or OPP. The actual definitions of

Operating
+ * Performance Points varies over silicon within the same family of devices.
+ * For a specific domain, you can have a set of {frequency, voltage} pairs
+ * and this is denoted by an array of opp_def. As the kernel boots and more
+ * information is available, a set of these are activated based on the precise
+ * nature of device the kernel boots up on. It is interesting to remember that
+ * each IP which belongs to a voltage domain may define their own set of OPPs
+ * on top of this - but this is handled by the appropriate driver.
+ */

include/linux/opp.h new file +79 -0
+struct opp_def {
+     unsigned long freq;
+     unsigned long u_volt;
+
+     bool enabled;
+};
+
+/*
+ * Initialization wrapper used to define an OPP.
+ * To point at the end of a terminator of a list of OPPs,
+ * use OPP_DEF(0, 0, 0)
+ */
+#define OPP_DEF(_enabled, _freq, _uv)        \
+{                                            \
+     .enabled        = _enabled,             \
+     .freq           = _freq,                \
+     .u_volt         = _uv,                  \
+}
+
+struct opp;
+
+#ifdef CONFIG_PM
+
+unsigned long opp_get_voltage(const struct opp *opp);
+
+unsigned long opp_get_freq(const struct opp *opp);
+
+int opp_get_opp_count(struct device *dev);
+
+struct opp *opp_find_freq_exact(struct device *dev, unsigned long freq,
+                             bool enabled);
+
+struct opp *opp_find_freq_floor(struct device *dev, unsigned long *freq);
+
+struct opp *opp_find_freq_ceil(struct device *dev, unsigned long *freq);
+
+int opp_add(struct device *dev, const struct opp_def *opp_def);
+
+int opp_enable(struct opp *opp);
+
+int opp_disable(struct opp *opp);
+
+void opp_init_cpufreq_table(struct device *dev,
+                         struct cpufreq_frequency_table **table);
+#else
+static inline unsigned long opp_get_voltage(const struct opp *opp)
+{
+     return 0;
+}
+
+static inline unsigned long opp_get_freq(const struct opp *opp)
+{
+     return 0;
+}
+
+static inline int opp_get_opp_count(struct device *dev)
+{
+     return 0;
+}
+
+static inline struct opp *opp_find_freq_exact(struct device *dev,
+                                  unsigned long freq, bool enabled)
+{
+     return ERR_PTR(-EINVAL);
+}
+
+static inline struct opp *opp_find_freq_floor(struct device *dev,
+                                     unsigned long *freq)
+{
+     return ERR_PTR(-EINVAL);
+}
+
+static inline struct opp *opp_find_freq_ceil(struct device *dev,
+                                     unsigned long *freq)
+{
+     return ERR_PTR(-EINVAL);
+}
+
+static inline int opp_add(struct device *dev, const struct opp_def

*opp_def)

include/linux/opp.h new file +21 -0
+{
+     return ERR_PTR(-EINVAL);
+}
+
+static inline int opp_enable(struct opp *opp)
+{
+     return 0;
+}
+
+static inline int opp_disable(struct opp *opp)
+{
+     return 0;
+}
+
+static inline void opp_init_cpufreq_table(struct device *dev,
+                         struct cpufreq_frequency_table **table)
+{
+}
+
+#endif               /* CONFIG_PM */
+#endif               /* __ASM_OPP_H */
kernel/power/Kconfig +7 -0
index ca6066a..634eab6 100644
@@ -242,3 +242,17 @@ config PM_OPS
      bool
      depends on PM_SLEEP || PM_RUNTIME
      default y
+
+config PM_OPP
+     bool "Enable Operating Performance Point(OPP) Layer library"
+     depends on PM
+     ---help---
+       SOCs have a standard set of tuples consisting of frequency and
+       voltage pairs that the device will support per voltage domain.

This
+ is called Operating Performance Point or OPP. The actual
definitions

kernel/power/Kconfig +6 -0
+       of OPP varies over silicon within the same family of devices.
+
+       OPP layer organizes the data internally using device pointers
+       representing individual voltage domains and provides SOC
+       implementations a ready to use framework to manage OPPs.
+       For more information, read <file:Documentation/power/opp.txt>
lib/Makefile +2 -0
index e6a3763..0114fcf 100644
@@ -88,6 +88,8 @@ obj-$(CONFIG_IOMMU_HELPER) += iommu-helper.o
 obj-$(CONFIG_FAULT_INJECTION) += fault-inject.o
 obj-$(CONFIG_CPU_NOTIFIER_ERROR_INJECT) += cpu-notifier-error-inject.o
 
+obj-$(CONFIG_PM_OPP) += opp.o
+
 lib-$(CONFIG_GENERIC_BUG) += bug.o
 
 obj-$(CONFIG_HAVE_ARCH_TRACEHOOK) += syscall.o
lib/opp.c new file +42 -0
new file mode 100644
index 0000000..650c8c3
@@ -0,0 +1,440 @@
+/*
+ * Generic OPP Interface
+ *
+ * Copyright (C) 2009-2010 Texas Instruments Incorporated.
+ *   Nishanth Menon
+ *   Romit Dasgupta <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/err.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/cpufreq.h>
+#include <linux/list.h>
+#include <linux/opp.h>
+
+/**
+ * struct opp - Generic OPP description structure
+ * @node:    list node
+ * @enabled: true/false - marking this OPP as enabled/disabled
+ * @rate:    Frequency in hertz
+ * @u_volt:  Nominal voltage in microvolts corresponding to this OPP
+ * @dev_opp: contains the device_opp struct
+ *
+ * This structure stores the OPP information for a given domain.
+ */
+struct opp {
+     struct list_head node;
+
+     bool enabled;
+     unsigned long rate;
+     unsigned long u_volt;
+
+     struct device_opp *dev_opp;
+};
+
+/**

lib/opp.c line 52

Quoted 10 lines
lib/opp.c new file +10 -0
+ * struct device_opp - Device opp structure
+ * @node:    list node
+ * @dev:     device handle
+ * @opp_list:        list of opps
+ * @opp_count:       num opps
+ * @enabled_opp_count:       how many opps are actually enabled
+ *
+ * This is an internal datastructure maintaining the link to
+ * opps attached to a domain device. This structure is not
+ * meant to be shared with users as it private to opp layer.

"... as it is private ..."

Quoted 43 lines
lib/opp.c new file +43 -0
+ */
+struct device_opp {
+     struct list_head node;
+
+     struct device *dev;
+
+     struct list_head opp_list;
+     u32 opp_count;
+     u32 enabled_opp_count;
+};
+
+static LIST_HEAD(dev_opp_list);
+
+/**
+ * find_device_opp() - find device_opp struct using device pointer
+ * @dev:     device pointer used to lookup device OPPs
+ *
+ * Search list of device OPPs for one containing matching device.
+ *
+ * Returns pointer to 'struct device_opp' if found, otherwise -ENODEV or
+ * -EINVAL based on type of error.
+ */
+static struct device_opp *find_device_opp(struct device *dev)
+{
+     struct device_opp *tmp_dev_opp, *dev_opp = ERR_PTR(-ENODEV);
+
+     if (unlikely(!dev || IS_ERR(dev))) {
+             pr_err("%s: Invalid parameters being passed\n", __func__);
+             return ERR_PTR(-EINVAL);
+     }
+
+     list_for_each_entry(tmp_dev_opp, &dev_opp_list, node) {
+             if (tmp_dev_opp->dev == dev) {
+                     dev_opp = tmp_dev_opp;
+                     break;
+             }
+     }
+
+     return dev_opp;
+}
+
+/**
+ * opp_get_voltage() - Gets the voltage corresponding to an opp

lib/opp.c line 105

Quoted 10 lines
lib/opp.c new file +10 -0
+ * @opp:     opp for which voltage has to be returned for
+ *
+ * Return voltage in micro volt corresponding to the opp, else
+ * return 0
+ */
+unsigned long opp_get_voltage(const struct opp *opp)
+{
+     if (unlikely(!opp || IS_ERR(opp)) || !opp->enabled) {
+             pr_err("%s: Invalid parameters being passed\n", __func__);
+             return 0;

Shouldn't the case for !opp->enabled just return 0, without error reporting.

IMHO, having an OPP disabled is not really a bug in params, and returning this, will misinform the user.. I think.

What do you think?

Quoted 7 lines
lib/opp.c new file +7 -0
+     }
+
+     return opp->u_volt;
+}
+
+/**
+ * opp_get_freq() - Gets the frequency corresponding to an opp

lib/opp.c line 122

Quoted 10 lines
lib/opp.c new file +10 -0
+ * @opp:     opp for which frequency has to be returned for
+ *
+ * Return frequency in hertz corresponding to the opp, else
+ * return 0
+ */
+unsigned long opp_get_freq(const struct opp *opp)
+{
+     if (unlikely(!opp || IS_ERR(opp)) || !opp->enabled) {
+             pr_err("%s: Invalid parameters being passed\n", __func__);
+             return 0;

Similarly here.

lib/opp.c

Quoted 10 lines
lib/opp.c new file +10 -0
+     }
+
+     return opp->rate;
+}
+
+/**
+ * opp_get_opp_count() - Get number of opps enabled in the opp list
+ * @dev:     device for which we do this operation
+ *
+ * This functions returns the number of opps if there are any OPPs

"This function returns.."

Quoted 23 lines

enabled,
+ * else returns corresponding error value.
+ */

lib/opp.c new file +18 -0
+int opp_get_opp_count(struct device *dev)
+{
+     struct device_opp *dev_opp;
+
+     dev_opp = find_device_opp(dev);
+     if (IS_ERR(dev_opp))
+             return -ENODEV;
+
+     return dev_opp->enabled_opp_count;
+}
+
+/**
+ * opp_find_freq_exact() - search for an exact frequency
+ * @dev:     device for which we do this operation
+ * @freq:    frequency to search for
+ * @enabled: enabled/disabled OPP to search for
+ *
+ * Searches for exact match in the opp list and returns handle to the

matching
+ * opp if found, else returns ERR_PTR in case of error and should be

Quoted 8 lines

handled
+ * using IS_ERR.
+ *
+ * Note: enabled is a modifier for the search. if enabled=true, then the match
+ * is for exact matching frequency and is enabled. if false, the match is for
+ * exact frequency which is disabled.

That enabled var is ignored, which makes this explanation mismatch with the code behavior.

Am I missing something?

Quoted 21 lines

+ */

lib/opp.c new file +20 -0
+struct opp *opp_find_freq_exact(struct device *dev,
+                                  unsigned long freq, bool enabled)
+{
+     struct device_opp *dev_opp;
+     struct opp *temp_opp, *opp = ERR_PTR(-ENODEV);
+
+     dev_opp = find_device_opp(dev);
+     if (IS_ERR(dev_opp))
+             return opp;
+
+     list_for_each_entry(temp_opp, &dev_opp->opp_list, node) {
+             if (temp_opp->enabled && temp_opp->rate == freq) {
+                     opp = temp_opp;
+                     break;
+             }
+     }
+
+     return opp;
+}
+

lib/opp.c line 189

Quoted 10 lines
lib/opp.c new file +10 -0
+/**
+ * opp_find_freq_ceil() - Search for an rounded ceil freq
+ * @dev:     device for which we do this operation
+ * @freq:    Start frequency
+ *
+ * Search for the matching ceil *enabled* OPP from a starting freq
+ * for a domain.
+ *
+ * Returns *opp and *freq is populated with the match, else
+ * returns NULL opp if no match, else returns ERR_PTR in case of error.

By looking at the code below, I don't think returning NULL is a possibility.

Quoted 18 lines

+ *
+ * Example usages:

+ *   * find match/next highest available frequency *
+ *   freq = 350000;
+ *   opp = opp_find_freq_ceil(dev, &freq))
+ *   if (IS_ERR(opp))

+ * pr_err("unable to find a higher frequency\n");
+ * else
+ * pr_info("match freq = %ld\n", freq);
+ *

+ *   * print all supported frequencies in ascending order *
+ *   freq = 0; * Search for the lowest enabled frequency *
+ *   while (!IS_ERR(opp = opp_find_freq_ceil(OPP_MPU, &freq)) {

+ * pr_info("freq = %ld\n", freq);
+ * freq++; * for next higher match *
+ * }
+ */

lib/opp.c new file +1 -0
+struct opp *opp_find_freq_ceil(struct device *dev, unsigned long *freq)

lib/opp.c line 217

Quoted 10 lines
lib/opp.c new file +10 -0
+{
+     struct device_opp *dev_opp;
+     struct opp *temp_opp, *opp = ERR_PTR(-ENODEV);
+
+     dev_opp = find_device_opp(dev);
+     if (IS_ERR(dev_opp))
+             return opp;
+
+     list_for_each_entry(temp_opp, &dev_opp->opp_list, node) {
+             if (temp_opp->enabled && temp_opp->rate >= *freq) {

What if freq contains a NULL pointer?

lib/opp.c line 228

Quoted 11 lines
lib/opp.c new file +11 -0
+                     opp = temp_opp;
+                     *freq = opp->rate;
+                     break;
+             }
+     }
+
+     return opp;
+}
+
+/**
+ * opp_find_freq_floor() - Search for an rounded floor freq

"... Search for a rounded floor ..."

Quoted 8 lines

+ * @dev: device for which we do this operation
+ * @freq: Start frequency
+ *
+ * Search for the matching floor *enabled* OPP from a starting freq
+ * for a domain.
+ *
+ * Returns *opp and *freq is populated with the next match, else
+ * returns NULL opp if no match, else returns ERR_PTR in case of error.

Similar comment about opp not being ever NULL as the function above.

Regards,
Sergio

Quoted 211 lines

+ *
+ * Example usages:

+ *   * find match/next lowest available frequency
+ *   freq = 350000;
+ *   opp = opp_find_freq_floor(dev, &freq)))
+ *   if (IS_ERR(opp))

+ * pr_err ("unable to find a lower frequency\n");
+ * else
+ * pr_info("match freq = %ld\n", freq);
+ *

+ *   * print all supported frequencies in descending order *
+ *   freq = ULONG_MAX; * search highest enabled frequency *
+ *   while (!IS_ERR(opp = opp_find_freq_floor(OPP_MPU, &freq)) {

+ * pr_info("freq = %ld\n", freq);
+ * freq--; * for next lower match *
+ * }
+ */

lib/opp.c new file +35 -0
+struct opp *opp_find_freq_floor(struct device *dev, unsigned long *freq)
+{
+     struct device_opp *dev_opp;
+     struct opp *temp_opp, *opp = ERR_PTR(-ENODEV);
+
+     dev_opp = find_device_opp(dev);
+     if (IS_ERR(dev_opp))
+             return opp;
+
+     list_for_each_entry_reverse(temp_opp, &dev_opp->opp_list, node) {
+             if (temp_opp->enabled && temp_opp->rate <= *freq) {
+                     opp = temp_opp;
+                     *freq = opp->rate;
+                     break;
+             }
+     }
+
+     return opp;
+}
+
+/* wrapper to reuse converting opp_def to opp struct */
+static void opp_populate(struct opp *opp,
+                           const struct opp_def *opp_def)
+{
+     opp->rate = opp_def->freq;
+     opp->enabled = opp_def->enabled;
+     opp->u_volt = opp_def->u_volt;
+}
+
+/**
+ * opp_add()  - Add an OPP table from a table definitions
+ * @dev:     device for which we do this operation
+ * @opp_def: opp_def to describe the OPP which we want to add.
+ *
+ * This function adds an opp definition to the opp list and returns

status.
+ */

lib/opp.c new file +63 -0
+int opp_add(struct device *dev, const struct opp_def *opp_def)
+{
+     struct device_opp *tmp_dev_opp, *dev_opp = NULL;
+     struct opp *opp, *new_opp;
+     struct list_head *head;
+
+     /* Check for existing list for 'dev' */
+     list_for_each_entry(tmp_dev_opp, &dev_opp_list, node) {
+             if (dev == tmp_dev_opp->dev) {
+                     dev_opp = tmp_dev_opp;
+                     break;
+             }
+     }
+
+     if (!dev_opp) {
+             /* Allocate a new device OPP table */
+             dev_opp = kzalloc(sizeof(struct device_opp), GFP_KERNEL);
+             if (!dev_opp) {
+                     pr_warning("%s: unable to allocate device struct\n",
+                             __func__);
+                     return -ENOMEM;
+             }
+
+             dev_opp->dev = dev;
+             INIT_LIST_HEAD(&dev_opp->opp_list);
+
+             list_add(&dev_opp->node, &dev_opp_list);
+     }
+
+     /* allocate new OPP node */
+     new_opp = kzalloc(sizeof(struct opp), GFP_KERNEL);
+     if (!new_opp) {
+             if (list_empty(&dev_opp->opp_list)) {
+                     list_del(&dev_opp->node);
+                     kfree(dev_opp);
+             }
+             pr_warning("%s: unable to allocate new opp node\n",
+                     __func__);
+             return -ENOMEM;
+     }
+     opp_populate(new_opp, opp_def);
+
+     /* Insert new OPP in order of increasing frequency */
+     head = &dev_opp->opp_list;
+     list_for_each_entry_reverse(opp, &dev_opp->opp_list, node) {
+             if (new_opp->rate >= opp->rate) {
+                     head = &opp->node;
+                     break;
+             }
+     }
+     list_add(&new_opp->node, head);
+     dev_opp->opp_count++;
+     if (new_opp->enabled)
+             dev_opp->enabled_opp_count++;
+
+     return 0;
+}
+
+/**
+ * opp_enable() - Enable a specific OPP
+ * @opp:     Pointer to opp
+ *
+ * Enables a provided opp. If the operation is valid, this returns 0,

else the
+ * corresponding error value.
+ *
+ * OPP used here is from the the opp_is_valid/opp_has_freq or other search
+ * functions
+ */

lib/opp.c new file +20 -0
+int opp_enable(struct opp *opp)
+{
+     if (unlikely(!opp || IS_ERR(opp))) {
+             pr_err("%s: Invalid parameters being passed\n", __func__);
+             return -EINVAL;
+     }
+
+     if (!opp->enabled && opp->dev_opp)
+             opp->dev_opp->enabled_opp_count++;
+
+     opp->enabled = true;
+
+     return 0;
+}
+
+/**
+ * opp_disable() - Disable a specific OPP
+ * @opp:     Pointer to opp
+ *
+ * Disables a provided opp. If the operation is valid, this returns 0,

else the
+ * corresponding error value.
+ *
+ * OPP used here is from the the opp_is_valid/opp_has_freq or other search
+ * functions
+ */

lib/opp.c new file +58 -1
+int opp_disable(struct opp *opp)
+{
+     if (unlikely(!opp || IS_ERR(opp))) {
+             pr_err("%s: Invalid parameters being passed\n", __func__);
+             return -EINVAL;
+     }
+
+     if (opp->enabled && opp->dev_opp)
+             opp->dev_opp->enabled_opp_count--;
+
+     opp->enabled = false;
+
+     return 0;
+}
+
+/**
+ * opp_init_cpufreq_table() - create a cpufreq table for a domain
+ * @dev:     device for which we do this operation
+ * @table:   Cpufreq table returned back to caller
+ *
+ * Generate a cpufreq table for a provided domain - this assumes that the
+ * opp list is already initialized and ready for usage
+ */
+void opp_init_cpufreq_table(struct device *dev,
+                         struct cpufreq_frequency_table **table)
+{
+     struct device_opp *dev_opp;
+     struct opp *opp;
+     struct cpufreq_frequency_table *freq_table;
+     int i = 0;
+
+     dev_opp = find_device_opp(dev);
+     if (IS_ERR(dev_opp)) {
+             pr_warning("%s: unable to find device\n", __func__);
+             return;
+     }
+
+     freq_table = kzalloc(sizeof(struct cpufreq_frequency_table) *
+                          (dev_opp->enabled_opp_count + 1), GFP_ATOMIC);
+     if (!freq_table) {
+             pr_warning("%s: failed to allocate frequency table\n",
+                        __func__);
+             return;
+     }
+
+     list_for_each_entry(opp, &dev_opp->opp_list, node) {
+             if (opp->enabled) {
+                     freq_table[i].index = i;
+                     freq_table[i].frequency = opp->rate / 1000;
+                     i++;
+             }
+     }
+
+     freq_table[i].index = i;
+     freq_table[i].frequency = CPUFREQ_TABLE_END;
+
+     *table = &freq_table[0];
+}
--

1.6.3.3

#22 lore raw
In reply to Nishanth Menon

include/linux/opp.h line 42

Quoted 6 lines · Nishanth Menon
include/linux/opp.h new file +6 -0
+struct opp_def {
+	unsigned long freq;
+	unsigned long u_volt;
+
+	bool enabled;
+};

It might be clearer to use some term other than enabled in the code - when reading I wasn't immediately sure if enabled meant that it was available to be selected or if it was the active operating point. How about 'allowed' (though I'm not 100% happy with that)?

include/linux/opp.h line 118

Quoted 4 lines
include/linux/opp.h new file +4 -0
+static inline int opp_add(struct device *dev, const struct opp_def *opp_def)
+{
+	return ERR_PTR(-EINVAL);
+}

Mismatch with the return type and value here.

Quoted 14 lines
lib/opp.c new file +14 -0
+/**
+ * opp_enable() - Enable a specific OPP
+ * @opp:	Pointer to opp
+ *
+ * Enables a provided opp. If the operation is valid, this returns 0, else the
+ * corresponding error value.
+ *
+ * OPP used here is from the the opp_is_valid/opp_has_freq or other search
+ * functions
+ */
+int opp_enable(struct opp *opp)
+{
+	if (unlikely(!opp || IS_ERR(opp))) {
+		pr_err("%s: Invalid parameters being passed\n", __func__);

lib/opp.c line 371

Quoted 10 lines
lib/opp.c new file +10 -0
+		return -EINVAL;
+	}
+
+	if (!opp->enabled && opp->dev_opp)
+		opp->dev_opp->enabled_opp_count++;
+
+	opp->enabled = true;
+
+	return 0;
+}

When reading the description I'd expected to see some facility to trigger selection of an active operating point in the library (possibly as a separate call since you might have a bunch of operating points being updated in quick succession) but it looks like that needs to be supplied externally at the minute?

#23 lore raw

Mark Brown had written, on 09/17/2010 10:36 AM, the following:

Quoted 13 lines · Nishanth Menon

include/linux/opp.h line 42

Quoted 7 lines
include/linux/opp.h new file +6 -0
+struct opp_def {
+	unsigned long freq;
+	unsigned long u_volt;
+
+	bool enabled;
+};

It might be clearer to use some term other than enabled in the code - when reading I wasn't immediately sure if enabled meant that it was available to be selected or if it was the active operating point. How about 'allowed' (though I'm not 100% happy with that)?

;).. The opp is enabled or disabled if it is populated, it is implicit as being available but not enabled- how about active? this would change the opp_enable/disable functions to opp_activate, opp_deactivate..

Recommendations folks?

Quoted 7 lines

include/linux/opp.h line 118

Quoted 5 lines
include/linux/opp.h new file +4 -0
+static inline int opp_add(struct device *dev, const struct opp_def *opp_def)
+{
+	return ERR_PTR(-EINVAL);
+}

Mismatch with the return type and value here.

/me kicks himself.. ouch.. thanks.. will fix in v2.

Quoted 23 lines
Quoted 23 lines
lib/opp.c new file +22 -0
+/**
+ * opp_enable() - Enable a specific OPP
+ * @opp:	Pointer to opp
+ *
+ * Enables a provided opp. If the operation is valid, this returns 0, else the
+ * corresponding error value.
+ *
+ * OPP used here is from the the opp_is_valid/opp_has_freq or other search
+ * functions
+ */
+int opp_enable(struct opp *opp)
+{
+	if (unlikely(!opp || IS_ERR(opp))) {
+		pr_err("%s: Invalid parameters being passed\n", __func__);
+		return -EINVAL;
+	}
+
+	if (!opp->enabled && opp->dev_opp)
+		opp->dev_opp->enabled_opp_count++;
+
+	opp->enabled = true;
+
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	return 0;
+}

When reading the description I'd expected to see some facility to trigger selection of an active operating point in the library (possibly as a separate call since you might have a bunch of operating points being updated in quick succession) but it looks like that needs to be supplied externally at the minute?

The intent is we use the opp_search* functions to pick up the opp and enable/activate it and disable/deactivate it.

Signature · 2 lines
Regards,
Nishanth Menon
#24 lore raw
Quoted 7 lines · Nishanth Menon

Mark Brown had written, on 09/17/2010 10:36 AM, the following:

Quoted 4 lines

It might be clearer to use some term other than enabled in the code - when reading I wasn't immediately sure if enabled meant that it was available to be selected or if it was the active operating point. How about 'allowed' (though I'm not 100% happy with that)?

Quoted 6 lines

;).. The opp is enabled or disabled if it is populated, it is implicit as being available but not enabled- how about active? this would change the opp_enable/disable functions to opp_activate, opp_deactivate..

Recommendations folks?

The enable/disable thing wasn't so noticable in the API itself, it was in the data structures that I found it confusing - the core has a different idea about what's going on with the system as a whole compared to the decision that an individual device is taking.

Quoted 8 lines
Quoted 5 lines

When reading the description I'd expected to see some facility to trigger selection of an active operating point in the library (possibly as a separate call since you might have a bunch of operating points being updated in quick succession) but it looks like that needs to be supplied externally at the minute?

The intent is we use the opp_search* functions to pick up the opp and enable/activate it and disable/deactivate it.

Sure, I get that bit. What I meant was that I was expecting something that would say that changes had been made to the enabled/disabled sets and that it'd be a good idea to rescan, especially for cases where the devices change their requirements but the OPPs need to be done over a larger block.

#25 lore raw

[trimmed Cc list a bit, as vger bounced my last reply due to header too long]

Quoted 13 lines · Mark Brown
Quoted 11 lines · Nishanth Menon

Mark Brown had written, on 09/17/2010 10:36 AM, the following:

Quoted 4 lines

It might be clearer to use some term other than enabled in the code - when reading I wasn't immediately sure if enabled meant that it was available to be selected or if it was the active operating point. How about 'allowed' (though I'm not 100% happy with that)?

;).. The opp is enabled or disabled if it is populated, it is implicit as being available but not enabled- how about active? this would change the opp_enable/disable functions to opp_activate, opp_deactivate..

Quoted 6 lines
Quoted 1 line

Recommendations folks?

The enable/disable thing wasn't so noticable in the API itself, it was in the data structures that I found it confusing - the core has a different idea about what's going on with the system as a whole compared to the decision that an individual device is taking.

Can you clarify your confusion here? I don't follow the problem you're raising.

The enabled flag in the internal data structure is set to true when opp_enable() is called and set to false when opp_disable() is called. I'm not sure

At least as we're currently using it, this API has know knowlege of what OPP is currently active on the system. IOW, it has no idea what the current frequency/voltage a given device is set to. It's intended more like an OPP database with some convience functions to search through the OPPs and to make OPPs available (or not.) The users of this API (in our case, CPUfreq and voltage scaling code) are the ones which keep track of which OPP is actually in use.

As I write this, I agree with what Phil pointed out; that 'available' is probably the right name for this flag, instead of 'enabled.'

Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

When reading the description I'd expected to see some facility to trigger selection of an active operating point in the library (possibly as a separate call since you might have a bunch of operating points being updated in quick succession) but it looks like that needs to be supplied externally at the minute?

Quoted 8 lines
Quoted 2 lines

The intent is we use the opp_search* functions to pick up the opp and enable/activate it and disable/deactivate it.

Sure, I get that bit. What I meant was that I was expecting something that would say that changes had been made to the enabled/disabled sets and that it'd be a good idea to rescan, especially for cases where the devices change their requirements but the OPPs need to be done over a larger block.

I guess you're thinking of notifiers, so if the list of available OPPs changes, a driver could (re)search to see if a more optimal OPP was available?

Certainly sounds possible, but not sure how useful in practice. OPP change decisions are not very often, so any OPP database updates may not affect a device OPP change currently in progress, but would take effect the next time that device makes an OPP change.

Either way, this is something that could easily be added if it seems useful.

Kevin

#26 lore raw
Quoted 10 lines · Kevin Hilman

[trimmed Cc list a bit, as vger bounced my last reply due to header too long]

Quoted 4 lines · Mark Brown

The enable/disable thing wasn't so noticable in the API itself, it was in the data structures that I found it confusing - the core has a different idea about what's going on with the system as a whole compared to the decision that an individual device is taking.

Can you clarify your confusion here? I don't follow the problem you're raising.

The confusion is between the enable flag meaning that the operating point is on the list that can be chosen from and it being the currently active one. It's clear once you understand what the API does but the current naming makes that harder to grasp.

Quoted 2 lines

As I write this, I agree with what Phil pointed out; that 'available' is probably the right name for this flag, instead of 'enabled.'

Yup, me too.

Quoted 9 lines
Quoted 5 lines

Sure, I get that bit. What I meant was that I was expecting something that would say that changes had been made to the enabled/disabled sets and that it'd be a good idea to rescan, especially for cases where the devices change their requirements but the OPPs need to be done over a larger block.

I guess you're thinking of notifiers, so if the list of available OPPs changes, a driver could (re)search to see if a more optimal OPP was available?

Yup, or at least some suggestion in the API for how this should be done.

Quoted 4 lines

Certainly sounds possible, but not sure how useful in practice. OPP change decisions are not very often, so any OPP database updates may not affect a device OPP change currently in progress, but would take effect the next time that device makes an OPP change.

Like I say, the main use case would be when the device itself is not making the actual operating point decision but is feeding in to a wider block - if the device implements the decision then it really shouldn't need to notify itself about it, though I guess it might be handy.

Quoted 2 lines

Either way, this is something that could easily be added if it seems useful.

My concern there would be divergent idioms for working with the API. It'd seem better to start everyone off down the same path if we can rather than have differences between platforms which make life harder when moving between mulitple platforms.

#27 lore raw
In reply to Kevin Hilman
Quoted 10 lines · Kevin Hilman

[trimmed Cc list a bit, as vger bounced my last reply due to header too long]

Quoted 4 lines · Mark Brown

The enable/disable thing wasn't so noticable in the API itself, it was in the data structures that I found it confusing - the core has a different idea about what's going on with the system as a whole compared to the decision that an individual device is taking.

Can you clarify your confusion here? I don't follow the problem you're raising.

The confusion is between the enable flag meaning that the operating point is on the list that can be chosen from and it being the currently active one. It's clear once you understand what the API does but the current naming makes that harder to grasp.

Quoted 2 lines

As I write this, I agree with what Phil pointed out; that 'available' is probably the right name for this flag, instead of 'enabled.'

Yup, me too.

Quoted 9 lines
Quoted 5 lines

Sure, I get that bit. What I meant was that I was expecting something that would say that changes had been made to the enabled/disabled sets and that it'd be a good idea to rescan, especially for cases where the devices change their requirements but the OPPs need to be done over a larger block.

I guess you're thinking of notifiers, so if the list of available OPPs changes, a driver could (re)search to see if a more optimal OPP was available?

Yup, or at least some suggestion in the API for how this should be done.

Quoted 4 lines

Certainly sounds possible, but not sure how useful in practice. OPP change decisions are not very often, so any OPP database updates may not affect a device OPP change currently in progress, but would take effect the next time that device makes an OPP change.

Like I say, the main use case would be when the device itself is not making the actual operating point decision but is feeding in to a wider block - if the device implements the decision then it really shouldn't need to notify itself about it, though I guess it might be handy.

Quoted 2 lines

Either way, this is something that could easily be added if it seems useful.

My concern there would be divergent idioms for working with the API. It'd seem better to start everyone off down the same path if we can rather than have differences between platforms which make life harder when moving between mulitple platforms.

#28 lore raw
In reply to Mark Brown

[trimmed Cc list a bit, as vger bounced my last reply due to header too long]

Quoted 13 lines · Mark Brown
Quoted 11 lines · Nishanth Menon

Mark Brown had written, on 09/17/2010 10:36 AM, the following:

Quoted 4 lines

It might be clearer to use some term other than enabled in the code - when reading I wasn't immediately sure if enabled meant that it was available to be selected or if it was the active operating point. How about 'allowed' (though I'm not 100% happy with that)?

;).. The opp is enabled or disabled if it is populated, it is implicit as being available but not enabled- how about active? this would change the opp_enable/disable functions to opp_activate, opp_deactivate..

Quoted 6 lines
Quoted 1 line

Recommendations folks?

The enable/disable thing wasn't so noticable in the API itself, it was in the data structures that I found it confusing - the core has a different idea about what's going on with the system as a whole compared to the decision that an individual device is taking.

Can you clarify your confusion here? I don't follow the problem you're raising.

The enabled flag in the internal data structure is set to true when opp_enable() is called and set to false when opp_disable() is called. I'm not sure

At least as we're currently using it, this API has know knowlege of what OPP is currently active on the system. IOW, it has no idea what the current frequency/voltage a given device is set to. It's intended more like an OPP database with some convience functions to search through the OPPs and to make OPPs available (or not.) The users of this API (in our case, CPUfreq and voltage scaling code) are the ones which keep track of which OPP is actually in use.

As I write this, I agree with what Phil pointed out; that 'available' is probably the right name for this flag, instead of 'enabled.'

Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

When reading the description I'd expected to see some facility to trigger selection of an active operating point in the library (possibly as a separate call since you might have a bunch of operating points being updated in quick succession) but it looks like that needs to be supplied externally at the minute?

Quoted 8 lines
Quoted 2 lines

The intent is we use the opp_search* functions to pick up the opp and enable/activate it and disable/deactivate it.

Sure, I get that bit. What I meant was that I was expecting something that would say that changes had been made to the enabled/disabled sets and that it'd be a good idea to rescan, especially for cases where the devices change their requirements but the OPPs need to be done over a larger block.

I guess you're thinking of notifiers, so if the list of available OPPs changes, a driver could (re)search to see if a more optimal OPP was available?

Certainly sounds possible, but not sure how useful in practice. OPP change decisions are not very often, so any OPP database updates may not affect a device OPP change currently in progress, but would take effect the next time that device makes an OPP change.

Either way, this is something that could easily be added if it seems useful.

Kevin

#29 lore raw
In reply to Nishanth Menon
Quoted 7 lines · Nishanth Menon

Mark Brown had written, on 09/17/2010 10:36 AM, the following:

Quoted 4 lines

It might be clearer to use some term other than enabled in the code - when reading I wasn't immediately sure if enabled meant that it was available to be selected or if it was the active operating point. How about 'allowed' (though I'm not 100% happy with that)?

Quoted 6 lines

;).. The opp is enabled or disabled if it is populated, it is implicit as being available but not enabled- how about active? this would change the opp_enable/disable functions to opp_activate, opp_deactivate..

Recommendations folks?

The enable/disable thing wasn't so noticable in the API itself, it was in the data structures that I found it confusing - the core has a different idea about what's going on with the system as a whole compared to the decision that an individual device is taking.

Quoted 8 lines
Quoted 5 lines

When reading the description I'd expected to see some facility to trigger selection of an active operating point in the library (possibly as a separate call since you might have a bunch of operating points being updated in quick succession) but it looks like that needs to be supplied externally at the minute?

The intent is we use the opp_search* functions to pick up the opp and enable/activate it and disable/deactivate it.

Sure, I get that bit. What I meant was that I was expecting something that would say that changes had been made to the enabled/disabled sets and that it'd be a good idea to rescan, especially for cases where the devices change their requirements but the OPPs need to be done over a larger block.

#30 lore raw
In reply to Nishanth Menon
Quoted 14 lines · Nishanth Menon

Mark Brown had written, on 09/17/2010 10:36 AM, the following:

Quoted 13 lines · Nishanth Menon

include/linux/opp.h line 42

Quoted 7 lines
include/linux/opp.h new file +6 -0
+struct opp_def {
+	unsigned long freq;
+	unsigned long u_volt;
+
+	bool enabled;
+};

It might be clearer to use some term other than enabled in the code - when reading I wasn't immediately sure if enabled meant that it was available to be selected or if it was the active operating point. How about 'allowed' (though I'm not 100% happy with that)?

Quoted 3 lines

;).. The opp is enabled or disabled if it is populated, it is implicit as being available but not enabled- how about active? this would change the opp_enable/disable functions to opp_activate, opp_deactivate..

Would that mean that "active" is the one currently in use?

Rafael

#31 lore raw

Rafael J. Wysocki had written, on 09/17/2010 05:22 PM, the following:

Quoted 14 lines · Nishanth Menon
Quoted 13 lines

Mark Brown had written, on 09/17/2010 10:36 AM, the following:

Quoted 12 lines · Nishanth Menon

include/linux/opp.h line 42

Quoted 7 lines
include/linux/opp.h new file +6 -0
+struct opp_def {
+	unsigned long freq;
+	unsigned long u_volt;
+
+	bool enabled;
+};

It might be clearer to use some term other than enabled in the code - when reading I wasn't immediately sure if enabled meant that it was available to be selected or if it was the active operating point. How about 'allowed' (though I'm not 100% happy with that)?

Quoted 5 lines
Quoted 3 lines

;).. The opp is enabled or disabled if it is populated, it is implicit as being available but not enabled- how about active? this would change the opp_enable/disable functions to opp_activate, opp_deactivate..

Would that mean that "active" is the one currently in use?

I like the idea Phil pointed out[1] on using "available" instead.. opp_enable and disable will make the OPP available or not. does this sound better?

[1] http://marc.info/?l=linux-arm-kernel&m=128474217132058&w=2

Signature · 2 lines
Regards,
Nishanth Menon
#32 lore raw
Quoted 19 lines · Nishanth Menon

Rafael J. Wysocki had written, on 09/17/2010 05:22 PM, the following:

Quoted 18 lines · Nishanth Menon
Quoted 16 lines

Mark Brown had written, on 09/17/2010 10:36 AM, the following:

Quoted 12 lines · Nishanth Menon

include/linux/opp.h line 42

Quoted 7 lines
include/linux/opp.h new file +6 -0
+struct opp_def {
+	unsigned long freq;
+	unsigned long u_volt;
+
+	bool enabled;
+};

It might be clearer to use some term other than enabled in the code - when reading I wasn't immediately sure if enabled meant that it was available to be selected or if it was the active operating point. How about 'allowed' (though I'm not 100% happy with that)?

;).. The opp is enabled or disabled if it is populated, it is implicit as being available but not enabled- how about active? this would change the opp_enable/disable functions to opp_activate, opp_deactivate..

Quoted 5 lines
Quoted 1 line

Would that mean that "active" is the one currently in use?

I like the idea Phil pointed out[1] on using "available" instead.. opp_enable and disable will make the OPP available or not. does this sound better?

Yes, it does.

Rafael

#33 lore raw
In reply to Nishanth Menon
Quoted 19 lines · Nishanth Menon

Rafael J. Wysocki had written, on 09/17/2010 05:22 PM, the following:

Quoted 18 lines · Nishanth Menon
Quoted 16 lines

Mark Brown had written, on 09/17/2010 10:36 AM, the following:

Quoted 12 lines · Nishanth Menon

include/linux/opp.h line 42

Quoted 7 lines
include/linux/opp.h new file +6 -0
+struct opp_def {
+	unsigned long freq;
+	unsigned long u_volt;
+
+	bool enabled;
+};

It might be clearer to use some term other than enabled in the code - when reading I wasn't immediately sure if enabled meant that it was available to be selected or if it was the active operating point. How about 'allowed' (though I'm not 100% happy with that)?

;).. The opp is enabled or disabled if it is populated, it is implicit as being available but not enabled- how about active? this would change the opp_enable/disable functions to opp_activate, opp_deactivate..

Quoted 5 lines
Quoted 1 line

Would that mean that "active" is the one currently in use?

I like the idea Phil pointed out[1] on using "available" instead.. opp_enable and disable will make the OPP available or not. does this sound better?

Yes, it does.

Rafael

#34 lore raw
In reply to Rafael J. Wysocki

Rafael J. Wysocki had written, on 09/17/2010 05:22 PM, the following:

Quoted 14 lines · Nishanth Menon
Quoted 13 lines

Mark Brown had written, on 09/17/2010 10:36 AM, the following:

Quoted 12 lines · Nishanth Menon

include/linux/opp.h line 42

Quoted 7 lines
include/linux/opp.h new file +6 -0
+struct opp_def {
+	unsigned long freq;
+	unsigned long u_volt;
+
+	bool enabled;
+};

It might be clearer to use some term other than enabled in the code - when reading I wasn't immediately sure if enabled meant that it was available to be selected or if it was the active operating point. How about 'allowed' (though I'm not 100% happy with that)?

Quoted 5 lines
Quoted 3 lines

;).. The opp is enabled or disabled if it is populated, it is implicit as being available but not enabled- how about active? this would change the opp_enable/disable functions to opp_activate, opp_deactivate..

Would that mean that "active" is the one currently in use?

I like the idea Phil pointed out[1] on using "available" instead.. opp_enable and disable will make the OPP available or not. does this sound better?

[1] http://marc.info/?l=linux-arm-kernel&m=128474217132058&w=2

Signature · 2 lines
Regards,
Nishanth Menon
#35 lore raw
In reply to Nishanth Menon
Quoted 14 lines · Nishanth Menon

Mark Brown had written, on 09/17/2010 10:36 AM, the following:

Quoted 13 lines · Nishanth Menon

include/linux/opp.h line 42

Quoted 7 lines
include/linux/opp.h new file +6 -0
+struct opp_def {
+	unsigned long freq;
+	unsigned long u_volt;
+
+	bool enabled;
+};

It might be clearer to use some term other than enabled in the code - when reading I wasn't immediately sure if enabled meant that it was available to be selected or if it was the active operating point. How about 'allowed' (though I'm not 100% happy with that)?

Quoted 3 lines

;).. The opp is enabled or disabled if it is populated, it is implicit as being available but not enabled- how about active? this would change the opp_enable/disable functions to opp_activate, opp_deactivate..

Would that mean that "active" is the one currently in use?

Rafael

#36 lore raw
In reply to Mark Brown

Mark Brown had written, on 09/17/2010 10:36 AM, the following:

Quoted 13 lines · Nishanth Menon

include/linux/opp.h line 42

Quoted 7 lines
include/linux/opp.h new file +6 -0
+struct opp_def {
+	unsigned long freq;
+	unsigned long u_volt;
+
+	bool enabled;
+};

It might be clearer to use some term other than enabled in the code - when reading I wasn't immediately sure if enabled meant that it was available to be selected or if it was the active operating point. How about 'allowed' (though I'm not 100% happy with that)?

;).. The opp is enabled or disabled if it is populated, it is implicit as being available but not enabled- how about active? this would change the opp_enable/disable functions to opp_activate, opp_deactivate..

Recommendations folks?

Quoted 7 lines

include/linux/opp.h line 118

Quoted 5 lines
include/linux/opp.h new file +4 -0
+static inline int opp_add(struct device *dev, const struct opp_def *opp_def)
+{
+	return ERR_PTR(-EINVAL);
+}

Mismatch with the return type and value here.

/me kicks himself.. ouch.. thanks.. will fix in v2.

Quoted 23 lines
Quoted 23 lines
lib/opp.c new file +22 -0
+/**
+ * opp_enable() - Enable a specific OPP
+ * @opp:	Pointer to opp
+ *
+ * Enables a provided opp. If the operation is valid, this returns 0, else the
+ * corresponding error value.
+ *
+ * OPP used here is from the the opp_is_valid/opp_has_freq or other search
+ * functions
+ */
+int opp_enable(struct opp *opp)
+{
+	if (unlikely(!opp || IS_ERR(opp))) {
+		pr_err("%s: Invalid parameters being passed\n", __func__);
+		return -EINVAL;
+	}
+
+	if (!opp->enabled && opp->dev_opp)
+		opp->dev_opp->enabled_opp_count++;
+
+	opp->enabled = true;
+
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	return 0;
+}

When reading the description I'd expected to see some facility to trigger selection of an active operating point in the library (possibly as a separate call since you might have a bunch of operating points being updated in quick succession) but it looks like that needs to be supplied externally at the minute?

The intent is we use the opp_search* functions to pick up the opp and enable/activate it and disable/deactivate it.

Signature · 2 lines
Regards,
Nishanth Menon
#37 lore raw
In reply to Mark Brown
Quoted 12 lines · ext Mark Brown

include/linux/opp.h line 42

Quoted 6 lines · Nishanth Menon
include/linux/opp.h new file +6 -0
+struct opp_def {
+	unsigned long freq;
+	unsigned long u_volt;
+
+	bool enabled;
+};

It might be clearer to use some term other than enabled in the code - when reading I wasn't immediately sure if enabled meant that it was available to be selected or if it was the active operating point. How

^^^^^^^^^

Quoted 1 line

about 'allowed' (though I'm not 100% happy with that)?

I think your query contains its own answer.

Phil

#38 lore raw
Quoted 9 lines · Phil Carmody
Quoted 3 lines · ext Mark Brown

It might be clearer to use some term other than enabled in the code - when reading I wasn't immediately sure if enabled meant that it was available to be selected or if it was the active operating point. How

^^^^^^^^^

Quoted 1 line

about 'allowed' (though I'm not 100% happy with that)?

I think your query contains its own answer.

Yeah, I didn't pick that because all of them are available in the sense that they could in the future be enabled but I do think it's the best option.

#39 lore raw
In reply to Phil Carmody
Quoted 9 lines · Phil Carmody
Quoted 3 lines · ext Mark Brown

It might be clearer to use some term other than enabled in the code - when reading I wasn't immediately sure if enabled meant that it was available to be selected or if it was the active operating point. How

^^^^^^^^^

Quoted 1 line

about 'allowed' (though I'm not 100% happy with that)?

I think your query contains its own answer.

Yeah, I didn't pick that because all of them are available in the sense that they could in the future be enabled but I do think it's the best option.

#40 lore raw
In reply to Mark Brown
Quoted 12 lines · ext Mark Brown

include/linux/opp.h line 42

Quoted 6 lines · Nishanth Menon
include/linux/opp.h new file +6 -0
+struct opp_def {
+	unsigned long freq;
+	unsigned long u_volt;
+
+	bool enabled;
+};

It might be clearer to use some term other than enabled in the code - when reading I wasn't immediately sure if enabled meant that it was available to be selected or if it was the active operating point. How

^^^^^^^^^

Quoted 1 line

about 'allowed' (though I'm not 100% happy with that)?

I think your query contains its own answer.

Phil

#41 lore raw
In reply to Nishanth Menon

include/linux/opp.h line 42

Quoted 6 lines · Nishanth Menon
include/linux/opp.h new file +6 -0
+struct opp_def {
+	unsigned long freq;
+	unsigned long u_volt;
+
+	bool enabled;
+};

It might be clearer to use some term other than enabled in the code - when reading I wasn't immediately sure if enabled meant that it was available to be selected or if it was the active operating point. How about 'allowed' (though I'm not 100% happy with that)?

include/linux/opp.h line 118

Quoted 4 lines
include/linux/opp.h new file +4 -0
+static inline int opp_add(struct device *dev, const struct opp_def *opp_def)
+{
+	return ERR_PTR(-EINVAL);
+}

Mismatch with the return type and value here.

Quoted 14 lines
lib/opp.c new file +14 -0
+/**
+ * opp_enable() - Enable a specific OPP
+ * @opp:	Pointer to opp
+ *
+ * Enables a provided opp. If the operation is valid, this returns 0, else the
+ * corresponding error value.
+ *
+ * OPP used here is from the the opp_is_valid/opp_has_freq or other search
+ * functions
+ */
+int opp_enable(struct opp *opp)
+{
+	if (unlikely(!opp || IS_ERR(opp))) {
+		pr_err("%s: Invalid parameters being passed\n", __func__);

lib/opp.c line 371

Quoted 10 lines
lib/opp.c new file +10 -0
+		return -EINVAL;
+	}
+
+	if (!opp->enabled && opp->dev_opp)
+		opp->dev_opp->enabled_opp_count++;
+
+	opp->enabled = true;
+
+	return 0;
+}

When reading the description I'd expected to see some facility to trigger selection of an active operating point in the library (possibly as a separate call since you might have a bunch of operating points being updated in quick succession) but it looks like that needs to be supplied externally at the minute?

maintainer
#42 lore raw

PATCH 1/1 opp: introduce library for device-specific OPPs

In reply to Nishanth Menon
Quoted 37 lines · On Thu, 16 Sep 2010 20:29:33 -0500 Nishanth Menon <[email protected]> wrote:

SOCs have a standard set of tuples consisting of frequency and voltage pairs that the device will support per voltage domain. These are called Operating Performance Points or OPPs. The actual definitions of Operating Performance Points varies over silicon within the same family of devices. For a specific domain, you can have a set of {frequency, voltage} pairs. As the kernel boots and more information is available, a set of these are activated based on the precise nature of device the kernel boots up on. It is interesting to remember that each IP which belongs to a voltage domain may define their own set of OPPs on top of this.

To implement an OPP, some sort of power management support is necessary hence this library enablement depends on CONFIG_PM, however this does not fit into the core power framework as it is an independent library. This is hence introduced under lib allowing all architectures to selectively enable the feature based on thier capabilities.

Contributions include:
Sanjeev Premi for the initial concept:

	http://patchwork.kernel.org/patch/50998/

Kevin Hilman for converting original design to device-based Kevin Hilman and Paul Walmsey for cleaning up many of the function abstractions, improvements and data structure handling Romit Dasgupta for using enums instead of opp pointers Thara Gopinath, Eduardo Valentin and Vishwanath BS for fixes and cleanups.
Linus Walleij for recommending this layer be made generic for usage in other architectures beyond OMAP and ARM.

Discussions and comments from:
http://marc.info/?l=linux-omap&m=126033945313269&w=2
http://marc.info/?l=linux-omap&m=125482970102327&w=2
http://marc.info/?t=125809247500002&r=1&w=2
http://marc.info/?l=linux-omap&m=126025973426007&w=2
http://marc.info/?t=128152609200064&r=1&w=2
incorporated.

Quoted 7 lines

...

 Documentation/power/00-INDEX |    2 +
 include/linux/opp.h          |  136 +++++++++++++
 kernel/power/Kconfig         |   14 ++
 lib/Makefile                 |    2 +
 lib/opp.c                    |  440 ++++++++++++++++++++++++++++++++++++++++++

./lib/ is an unusual place to put a driver-like thing such as this. The lib/ directory is mainly for generic kernel-wide support things. I'd suggest that ./drivers/opp/ would be a better place.

Quoted 4 lines

...

include/linux/opp.h new file +1 -0
+/*

include/linux/opp.h line 54

Quoted 10 lines
include/linux/opp.h new file +10 -0
+ * Initialization wrapper used to define an OPP.
+ * To point at the end of a terminator of a list of OPPs,
+ * use OPP_DEF(0, 0, 0)
+ */
+#define OPP_DEF(_enabled, _freq, _uv)	\
+{						\
+	.enabled	= _enabled,		\
+	.freq		= _freq,		\
+	.u_volt		= _uv,			\
+}

OPP_DEF is a somewhat atypical name. OPP_INITIALIZER would be more conventional.

However OPP_DEF has no usage in this patch so perhaps this can be removed?

Quoted 1 line

+static LIST_HEAD(dev_opp_list);

There's no locking for this list. That's OK under some circumstances, but I do think there should be a comment here explaining this apparent
bug: why is no locking needed, what are the lifetime rules for entries on this list.

Also, the _ordering_ of items on this list is significant. It should also be documented.

Quoted 5 lines

...

+/**
+ * opp_get_voltage() - Gets the voltage corresponding to an opp

Usually the () is omitted from function names in kerneldoc comments. It might be OK, or it might produce strange output - I haven't checked.

Quoted 10 lines

...

+/**
+ * opp_find_freq_exact() - search for an exact frequency
+ * @dev: device for which we do this operation
+ * @freq: frequency to search for
+ * @enabled: enabled/disabled OPP to search for
+ *
+ * Searches for exact match in the opp list and returns handle to the matching

s/handle/pointer/

Quoted 34 lines

+ * opp if found, else returns ERR_PTR in case of error and should be handled
+ * using IS_ERR.
+ *
+ * Note: enabled is a modifier for the search. if enabled=true, then the match
+ * is for exact matching frequency and is enabled. if false, the match is for
+ * exact frequency which is disabled.
+ */

...

lib/opp.c new file +24 -0
+int opp_add(struct device *dev, const struct opp_def *opp_def)
+{
+	struct device_opp *tmp_dev_opp, *dev_opp = NULL;
+	struct opp *opp, *new_opp;
+	struct list_head *head;
+
+	/* Check for existing list for 'dev' */
+	list_for_each_entry(tmp_dev_opp, &dev_opp_list, node) {
+		if (dev == tmp_dev_opp->dev) {
+			dev_opp = tmp_dev_opp;
+			break;
+		}
+	}
+
+	if (!dev_opp) {
+		/* Allocate a new device OPP table */
+		dev_opp = kzalloc(sizeof(struct device_opp), GFP_KERNEL);
+		if (!dev_opp) {
+			pr_warning("%s: unable to allocate device struct\n",
+				__func__);
+			return -ENOMEM;
+		}
+
+		dev_opp->dev = dev;

lib/opp.c line 323

Quoted 10 lines
lib/opp.c new file +10 -0
+		INIT_LIST_HEAD(&dev_opp->opp_list);
+
+		list_add(&dev_opp->node, &dev_opp_list);
+	}
+
+	/* allocate new OPP node */
+	new_opp = kzalloc(sizeof(struct opp), GFP_KERNEL);
+	if (!new_opp) {
+		if (list_empty(&dev_opp->opp_list)) {
+			list_del(&dev_opp->node);

It would be neater to move the list_add() down to after the allocation of new_opp and to remove this list_del().

Quoted 10 lines
lib/opp.c new file +10 -0
+			kfree(dev_opp);
+		}
+		pr_warning("%s: unable to allocate new opp node\n",
+			__func__);
+		return -ENOMEM;
+	}
+	opp_populate(new_opp, opp_def);
+
+	/* Insert new OPP in order of increasing frequency */
+	head = &dev_opp->opp_list;

lib/opp.c line 343

Quoted 10 lines
lib/opp.c new file +10 -0
+	list_for_each_entry_reverse(opp, &dev_opp->opp_list, node) {
+		if (new_opp->rate >= opp->rate) {
+			head = &opp->node;
+			break;
+		}
+	}
+	list_add(&new_opp->node, head);
+	dev_opp->opp_count++;
+	if (new_opp->enabled)
+		dev_opp->enabled_opp_count++;

These non-atomic read-modify-write operations on *dev_opp have no locking. What prevents races here?

Quoted 31 lines
lib/opp.c new file +28 -0
+	return 0;
+}
+
 
...
 
+void opp_init_cpufreq_table(struct device *dev,
+			    struct cpufreq_frequency_table **table)
+{
+	struct device_opp *dev_opp;
+	struct opp *opp;
+	struct cpufreq_frequency_table *freq_table;
+	int i = 0;
+
+	dev_opp = find_device_opp(dev);
+	if (IS_ERR(dev_opp)) {
+		pr_warning("%s: unable to find device\n", __func__);
+		return;
+	}
+
+	freq_table = kzalloc(sizeof(struct cpufreq_frequency_table) *
+			     (dev_opp->enabled_opp_count + 1), GFP_ATOMIC);
+	if (!freq_table) {
+		pr_warning("%s: failed to allocate frequency table\n",
+			   __func__);
+		return;
+	}
+
+	list_for_each_entry(opp, &dev_opp->opp_list, node) {
+		if (opp->enabled) {
+			freq_table[i].index = i;

lib/opp.c line 440

Quoted 10 lines
lib/opp.c new file +10 -0
+			freq_table[i].frequency = opp->rate / 1000;
+			i++;
+		}
+	}
+
+	freq_table[i].index = i;
+	freq_table[i].frequency = CPUFREQ_TABLE_END;
+
+	*table = &freq_table[0];
+}

So we're playing with cpufreq internals here but there's no #ifdef CONFIG_CPUFREQ and there's no Kconfig dependency on cpufreq. That needs fixing I think, if only from a reduce-code-bloat perspective.

#43 lore raw

PATCH 1/1 opp: introduce library for device-specific OPPs

Andrew Morton had written, on 09/17/2010 02:19 PM, the following:

Quoted 3 lines

On Thu, 16 Sep 2010 20:29:33 -0500
Nishanth Menon <[email protected]> wrote:

[...]

Quoted 10 lines
Quoted 6 lines
 Documentation/power/00-INDEX |    2 +
 include/linux/opp.h          |  136 +++++++++++++
 kernel/power/Kconfig         |   14 ++
 lib/Makefile                 |    2 +
 lib/opp.c                    |  440 ++++++++++++++++++++++++++++++++++++++++++

./lib/ is an unusual place to put a driver-like thing such as this. The lib/ directory is mainly for generic kernel-wide support things. I'd suggest that ./drivers/opp/ would be a better place.

we had an interesting debate on this topic on the the thread starting
http://marc.info/?l=linux-arm-kernel&m=128465710624421&w=2

It really does not provide any driver like feature here. it is just a bunch of helper functions which any SOC framework can use to build up to implement either:
a) cpufreq implementation with various OPPs
b) bootup in a specific opp in a set of supported OPPs and stay there (e.g. mpurate support on OMAP platforms)

I had considered putting it in drivers/power, but it looks to contain mostly regulator stuff, the other alternative would be to include it in kernel/power or as Kevin H, Linus W and you mention drivers/opp

Personally, I dont have a strong feeling about it as long as it is reusable across SOCs and am hoping to hear some alignment :).

Quoted 17 lines
Quoted 4 lines

...

include/linux/opp.h new file +1 -0
+/*

include/linux/opp.h line 54

Quoted 10 lines
include/linux/opp.h new file +10 -0
+ * Initialization wrapper used to define an OPP.
+ * To point at the end of a terminator of a list of OPPs,
+ * use OPP_DEF(0, 0, 0)
+ */
+#define OPP_DEF(_enabled, _freq, _uv)	\
+{						\
+	.enabled	= _enabled,		\
+	.freq		= _freq,		\
+	.u_volt		= _uv,			\
+}

OPP_DEF is a somewhat atypical name. OPP_INITIALIZER would be more conventional.

Thanks. will incorporate this in v2 of my patchset.

Quoted 3 lines

However OPP_DEF has no usage in this patch so perhaps this can be removed?

there were a few OMAP followon patches from
http://marc.info/?l=linux-omap&m=128152135801634&w=2
which will actually use this for OMAP3 platform.

Quoted 7 lines
Quoted 2 lines

+static LIST_HEAD(dev_opp_list);

There's no locking for this list. That's OK under some circumstances, but I do think there should be a comment here explaining this apparent
bug: why is no locking needed, what are the lifetime rules for entries on this list.

Locking:
arrgh.. my bad.. I had documented it in the missing and later on posted opp.txt http://marc.info/?l=linux-arm-kernel&m=128473931626114&w=2

The OPP layer implementation expects to be used in multiple contexts (including calls from interrupt locked context) based on SOC framework implementation. It is recommended to use appropriate locking schemes within the SOC framework itself.

In terms of the lifetime rules on the nodes in the list:
The list is expected to be maintained once created, entries are expected to be added optimally and not expected to be destroyed, the choice of list implementation was for reducing the complexity of the code itself and not yet meant as a mechanism to dynamically add and delete nodes on the fly.. Essentially, it was intended for the SOC framework to ensure it plugs in the OPP entries optimally and not create a humungous list of all possible OPPs for all families of the vendor SOCs - even though it is possible to use the OPP layer so - it just wont be smart to do so considering list scan latencies on hot paths such as cpufreq transitions or idle transitions.

I should add comments to the effect of the same to the list header and documentation - will do this as part of v2.

Quoted 3 lines

Also, the _ordering_ of items on this list is significant. It should also be documented.

Ack. thanks for catching this. Will do it in v2.

Quoted 10 lines
Quoted 5 lines

...

+/**
+ * opp_get_voltage() - Gets the voltage corresponding to an opp

Usually the () is omitted from function names in kerneldoc comments. It might be OK, or it might produce strange output - I haven't checked.

http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=blob;f=Documentation/kernel-doc-nano-HOWTO.txt;h=3d8a97747f7731c801ca7d3a1483858feeb76b6c;hb=HEAD#l55
I wonder why the example protos use ()

Quoted 11 lines
Quoted 9 lines

...

+/**
+ * opp_find_freq_exact() - search for an exact frequency
+ * @dev: device for which we do this operation
+ * @freq: frequency to search for
+ * @enabled: enabled/disabled OPP to search for
+ *
+ * Searches for exact match in the opp list and returns handle to the matching

s/handle/pointer/

Thanks.. will fix in v2.

Quoted 40 lines
Quoted 40 lines

+ * opp if found, else returns ERR_PTR in case of error and should be handled
+ * using IS_ERR.
+ *
+ * Note: enabled is a modifier for the search. if enabled=true, then the match
+ * is for exact matching frequency and is enabled. if false, the match is for
+ * exact frequency which is disabled.
+ */

...

lib/opp.c new file +29 -0
+int opp_add(struct device *dev, const struct opp_def *opp_def)
+{
+	struct device_opp *tmp_dev_opp, *dev_opp = NULL;
+	struct opp *opp, *new_opp;
+	struct list_head *head;
+
+	/* Check for existing list for 'dev' */
+	list_for_each_entry(tmp_dev_opp, &dev_opp_list, node) {
+		if (dev == tmp_dev_opp->dev) {
+			dev_opp = tmp_dev_opp;
+			break;
+		}
+	}
+
+	if (!dev_opp) {
+		/* Allocate a new device OPP table */
+		dev_opp = kzalloc(sizeof(struct device_opp), GFP_KERNEL);
+		if (!dev_opp) {
+			pr_warning("%s: unable to allocate device struct\n",
+				__func__);
+			return -ENOMEM;
+		}
+
+		dev_opp->dev = dev;
+		INIT_LIST_HEAD(&dev_opp->opp_list);
+
+		list_add(&dev_opp->node, &dev_opp_list);
+	}
+
Quoted 8 lines

lib/opp.c line 323

Quoted 5 lines
lib/opp.c new file +5 -0
+	/* allocate new OPP node */
+	new_opp = kzalloc(sizeof(struct opp), GFP_KERNEL);
+	if (!new_opp) {
+		if (list_empty(&dev_opp->opp_list)) {
+			list_del(&dev_opp->node);

It would be neater to move the list_add() down to after the allocation of new_opp and to remove this list_del().

ack. thanks.. yes it does clean it up. additionally i expanded the static opp_populate which is used just once - removed one function call ;)

Quoted 8 lines

lib/opp.c line 330

Quoted 8 lines
lib/opp.c new file +7 -0
+			kfree(dev_opp);
+		}
+		pr_warning("%s: unable to allocate new opp node\n",
+			__func__);
+		return -ENOMEM;
+	}
+	opp_populate(new_opp, opp_def);
         ^^^^^^^^^^^^
Quoted 8 lines
Quoted 8 lines
lib/opp.c new file +8 -0
+
+	/* Insert new OPP in order of increasing frequency */
+	head = &dev_opp->opp_list;
+	list_for_each_entry_reverse(opp, &dev_opp->opp_list, node) {
+		if (new_opp->rate >= opp->rate) {
+			head = &opp->node;
+			break;
+		}
Quoted 8 lines

lib/opp.c line 343

Quoted 5 lines
lib/opp.c new file +5 -0
+	}
+	list_add(&new_opp->node, head);
+	dev_opp->opp_count++;
+	if (new_opp->enabled)
+		dev_opp->enabled_opp_count++;

These non-atomic read-modify-write operations on *dev_opp have no locking. What prevents races here?

explained above. it is the job of the framework using the OPP Layer library.

Quoted 38 lines
Quoted 38 lines
lib/opp.c new file +34 -0
+	return 0;
+}
+
 
...
 
+void opp_init_cpufreq_table(struct device *dev,
+			    struct cpufreq_frequency_table **table)
+{
+	struct device_opp *dev_opp;
+	struct opp *opp;
+	struct cpufreq_frequency_table *freq_table;
+	int i = 0;
+
+	dev_opp = find_device_opp(dev);
+	if (IS_ERR(dev_opp)) {
+		pr_warning("%s: unable to find device\n", __func__);
+		return;
+	}
+
+	freq_table = kzalloc(sizeof(struct cpufreq_frequency_table) *
+			     (dev_opp->enabled_opp_count + 1), GFP_ATOMIC);
+	if (!freq_table) {
+		pr_warning("%s: failed to allocate frequency table\n",
+			   __func__);
+		return;
+	}
+
+	list_for_each_entry(opp, &dev_opp->opp_list, node) {
+		if (opp->enabled) {
+			freq_table[i].index = i;
+			freq_table[i].frequency = opp->rate / 1000;
+			i++;
+		}
+	}
+
+	freq_table[i].index = i;
Quoted 8 lines

lib/opp.c line 440

Quoted 4 lines
lib/opp.c new file +4 -0
+	freq_table[i].frequency = CPUFREQ_TABLE_END;
+
+	*table = &freq_table[0];
+}

So we're playing with cpufreq internals here but there's no #ifdef CONFIG_CPUFREQ and there's no Kconfig dependency on cpufreq. That needs fixing I think, if only from a reduce-code-bloat perspective.

Thanks and ouch.. Again missing documentation. Apologies.
http://marc.info/?l=linux-arm-kernel&m=128473931626114&w=2
c) Dependency of OPP layer is on CONFIG_PM as certain SOCs such as Texas Instrument's OMAP support have frameworks to optionally boot at a certain opp without needing cpufreq.

This is called "mpurate" bootarg parameter in OMAP framework. I will put this under #ifdef CPUFREQ and provide header coverage for the same appropriately.

Signature · 2 lines
Regards,
Nishanth Menon
#44 lore raw

PATCH 1/1 opp: introduce library for device-specific OPPs

Quoted 4 lines · Nishanth Menon

Andrew Morton had written, on 09/17/2010 02:19 PM, the following:

Quoted 3 lines

On Thu, 16 Sep 2010 20:29:33 -0500
Nishanth Menon <[email protected]> wrote:

[...]

Quoted 43 lines
Quoted 31 lines
Quoted 31 lines
lib/opp.c new file +31 -0
+void opp_init_cpufreq_table(struct device *dev,
+			    struct cpufreq_frequency_table **table)
+{
+	struct device_opp *dev_opp;
+	struct opp *opp;
+	struct cpufreq_frequency_table *freq_table;
+	int i = 0;
+
+	dev_opp = find_device_opp(dev);
+	if (IS_ERR(dev_opp)) {
+		pr_warning("%s: unable to find device\n", __func__);
+		return;
+	}
+
+	freq_table = kzalloc(sizeof(struct cpufreq_frequency_table) *
+			     (dev_opp->enabled_opp_count + 1), GFP_ATOMIC);
+	if (!freq_table) {
+		pr_warning("%s: failed to allocate frequency table\n",
+			   __func__);
+		return;
+	}
+
+	list_for_each_entry(opp, &dev_opp->opp_list, node) {
+		if (opp->enabled) {
+			freq_table[i].index = i;
+			freq_table[i].frequency = opp->rate / 1000;
+			i++;
+		}
+	}
+
+	freq_table[i].index = i;
Quoted 8 lines

lib/opp.c line 440

Quoted 4 lines
lib/opp.c new file +4 -0
+	freq_table[i].frequency = CPUFREQ_TABLE_END;
+
+	*table = &freq_table[0];
+}

So we're playing with cpufreq internals here but there's no #ifdef CONFIG_CPUFREQ and there's no Kconfig dependency on cpufreq. That needs fixing I think, if only from a reduce-code-bloat perspective.

Thanks and ouch.. Again missing documentation. Apologies.
http://marc.info/?l=linux-arm-kernel&m=128473931626114&w=2

Quoted 7 lines

c) Dependency of OPP layer is on CONFIG_PM as certain SOCs such as Texas Instrument's OMAP support have frameworks to optionally boot at a certain opp without needing cpufreq.

This is called "mpurate" bootarg parameter in OMAP framework. I will put this under #ifdef CPUFREQ and provide header coverage for the same appropriately.

The OPP layer in general is dependent on CONFIG_PM, but the snippit above is called only by CPUfreq core when CPUfreq is enabled, so at least that function should be under #ifdef CPUFREQ.

Kevin

#45 lore raw

PATCH 1/1 opp: introduce library for device-specific OPPs

In reply to Nishanth Menon
Quoted 18 lines · Nishanth Menon

Andrew Morton had written, on 09/17/2010 02:19 PM, the following:

Quoted 3 lines

On Thu, 16 Sep 2010 20:29:33 -0500
Nishanth Menon <[email protected]> wrote:

[...]

Quoted 10 lines
Quoted 6 lines
 Documentation/power/00-INDEX |    2 +
 include/linux/opp.h          |  136 +++++++++++++
 kernel/power/Kconfig         |   14 ++
 lib/Makefile                 |    2 +
 lib/opp.c                    |  440 ++++++++++++++++++++++++++++++++++++++++++

./lib/ is an unusual place to put a driver-like thing such as this. The lib/ directory is mainly for generic kernel-wide support things. I'd suggest that ./drivers/opp/ would be a better place.

we had an interesting debate on this topic on the the thread starting
http://marc.info/?l=linux-arm-kernel&m=128465710624421&w=2

Quoted 6 lines

It really does not provide any driver like feature here. it is just a bunch of helper functions which any SOC framework can use to build up to implement either:
a) cpufreq implementation with various OPPs
b) bootup in a specific opp in a set of supported OPPs and stay there (e.g. mpurate support on OMAP platforms)

Generally, some files under drivers/base/power contain helper functions of similar nature. You can put it in there, as far as I'm concerned.

Quoted 24 lines

I had considered putting it in drivers/power, but it looks to contain mostly regulator stuff, the other alternative would be to include it in kernel/power or as Kevin H, Linus W and you mention drivers/opp

Personally, I dont have a strong feeling about it as long as it is reusable across SOCs and am hoping to hear some alignment :).

Quoted 17 lines
Quoted 4 lines

...

include/linux/opp.h new file +1 -0
+/*

include/linux/opp.h line 54

Quoted 10 lines
include/linux/opp.h new file +10 -0
+ * Initialization wrapper used to define an OPP.
+ * To point at the end of a terminator of a list of OPPs,
+ * use OPP_DEF(0, 0, 0)
+ */
+#define OPP_DEF(_enabled, _freq, _uv)	\
+{						\
+	.enabled	= _enabled,		\
+	.freq		= _freq,		\
+	.u_volt		= _uv,			\
+}

OPP_DEF is a somewhat atypical name. OPP_INITIALIZER would be more conventional.

Quoted 8 lines

Thanks. will incorporate this in v2 of my patchset.

Quoted 3 lines

However OPP_DEF has no usage in this patch so perhaps this can be removed?

there were a few OMAP followon patches from
http://marc.info/?l=linux-omap&m=128152135801634&w=2
which will actually use this for OMAP3 platform.

Still, the $subject patch doesn't use it. So perhaps add it in a separate patch?

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

+static LIST_HEAD(dev_opp_list);

There's no locking for this list. That's OK under some circumstances, but I do think there should be a comment here explaining this apparent
bug: why is no locking needed, what are the lifetime rules for entries on this list.

Quoted 8 lines

Locking:
arrgh.. my bad.. I had documented it in the missing and later on posted opp.txt http://marc.info/?l=linux-arm-kernel&m=128473931626114&w=2

The OPP layer implementation expects to be used in multiple contexts (including calls from interrupt locked context) based on SOC framework implementation. It is recommended to use appropriate locking schemes within the SOC framework itself.

That should be stated directly in the kerneldoc comments. The requirement that the caller is supposed to ensure suitable locking is by no means a trivial one.

Apart from this, it might be a good idea to help callers a bit and actually introduce some sort of locking into the framework.

Quoted 11 lines

In terms of the lifetime rules on the nodes in the list:
The list is expected to be maintained once created, entries are expected to be added optimally and not expected to be destroyed, the choice of list implementation was for reducing the complexity of the code itself and not yet meant as a mechanism to dynamically add and delete nodes on the fly.. Essentially, it was intended for the SOC framework to ensure it plugs in the OPP entries optimally and not create a humungous list of all possible OPPs for all families of the vendor SOCs - even though it is possible to use the OPP layer so - it just wont be smart to do so considering list scan latencies on hot paths such as cpufreq transitions or idle transitions.

If the list nodes are not supposed to be added and removed dynamically, it probably would make sense to create data structures containing the "available" OPPs only, once they are known, and simply free the object representing the other ones.

Thanks,
Rafael

#46 lore raw

PATCH 1/1 opp: introduce library for device-specific OPPs

Rafael J. Wysocki had written, on 09/17/2010 06:07 PM, the following:

Quoted 24 lines · Nishanth Menon
Quoted 16 lines

Andrew Morton had written, on 09/17/2010 02:19 PM, the following:

Quoted 3 lines

On Thu, 16 Sep 2010 20:29:33 -0500
Nishanth Menon <[email protected]> wrote:

[...]

Quoted 8 lines
Quoted 5 lines
 Documentation/power/00-INDEX |    2 +
 include/linux/opp.h          |  136 +++++++++++++
 kernel/power/Kconfig         |   14 ++
 lib/Makefile                 |    2 +
 lib/opp.c                    |  440 ++++++++++++++++++++++++++++++++++++++++++

./lib/ is an unusual place to put a driver-like thing such as this. The lib/ directory is mainly for generic kernel-wide support things. I'd suggest that ./drivers/opp/ would be a better place.

we had an interesting debate on this topic on the the thread starting
http://marc.info/?l=linux-arm-kernel&m=128465710624421&w=2

Quoted 6 lines

It really does not provide any driver like feature here. it is just a bunch of helper functions which any SOC framework can use to build up to implement either:
a) cpufreq implementation with various OPPs
b) bootup in a specific opp in a set of supported OPPs and stay there (e.g. mpurate support on OMAP platforms)

Quoted 2 lines

Generally, some files under drivers/base/power contain helper functions of similar nature. You can put it in there, as far as I'm concerned.

Ack. thanks for the suggestion. it looks like a good place to me too. will do it as part of v2.

Quoted 25 lines
Quoted 25 lines

I had considered putting it in drivers/power, but it looks to contain mostly regulator stuff, the other alternative would be to include it in kernel/power or as Kevin H, Linus W and you mention drivers/opp

Personally, I dont have a strong feeling about it as long as it is reusable across SOCs and am hoping to hear some alignment :).

Quoted 15 lines

include/linux/opp.h line 54

Quoted 13 lines

...

include/linux/opp.h new file +11 -0
+/*
+ * Initialization wrapper used to define an OPP.
+ * To point at the end of a terminator of a list of OPPs,
+ * use OPP_DEF(0, 0, 0)
+ */
+#define OPP_DEF(_enabled, _freq, _uv)	\
+{						\
+	.enabled	= _enabled,		\
+	.freq		= _freq,		\
+	.u_volt		= _uv,			\
+}

OPP_DEF is a somewhat atypical name. OPP_INITIALIZER would be more conventional.

Thanks. will incorporate this in v2 of my patchset.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

However OPP_DEF has no usage in this patch so perhaps this can be removed?

there were a few OMAP followon patches from
http://marc.info/?l=linux-omap&m=128152135801634&w=2
which will actually use this for OMAP3 platform.

Still, the $subject patch doesn't use it. So perhaps add it in a separate patch?

was hoping to align on the approach with v2, the follow on OMAP patch
https://patchwork.kernel.org/patch/183742/ will post with needed changes be part of Kevin's thread for OMAP3 if we are ok with it, or Do we want to see it in this series as reference?

Quoted 10 lines
Quoted 10 lines
Quoted 6 lines
Quoted 2 lines

+static LIST_HEAD(dev_opp_list);

There's no locking for this list. That's OK under some circumstances, but I do think there should be a comment here explaining this apparent
bug: why is no locking needed, what are the lifetime rules for entries on this list.

Locking:
arrgh.. my bad.. I had documented it in the missing and later on posted opp.txt http://marc.info/?l=linux-arm-kernel&m=128473931626114&w=2

Quoted 8 lines
Quoted 4 lines

The OPP layer implementation expects to be used in multiple contexts (including calls from interrupt locked context) based on SOC framework implementation. It is recommended to use appropriate locking schemes within the SOC framework itself.

That should be stated directly in the kerneldoc comments. The requirement that the caller is supposed to ensure suitable locking is by no means a trivial one.

Agreed. thanks for pointing this out. I have done this as part of v2 of my patch. will post in a few hrs.

Quoted 3 lines

Apart from this, it might be a good idea to help callers a bit and actually introduce some sort of locking into the framework.

in OMAP implementation we have need to use these apis from irq_locked contexts as well.. lock implementation will prevent thier necessary usage. instead we have implemented lock mechanisms in higher layers and ensured that the opp table does not change once created - the rest of them are query functions - the risk is on opp_enable/disable.

Quoted 13 lines
Quoted 12 lines

In terms of the lifetime rules on the nodes in the list:
The list is expected to be maintained once created, entries are expected to be added optimally and not expected to be destroyed, the choice of list implementation was for reducing the complexity of the code itself and not yet meant as a mechanism to dynamically add and delete nodes on the fly.. Essentially, it was intended for the SOC framework to ensure it plugs in the OPP entries optimally and not create a humongous list of all possible OPPs for all families of the vendor SOCs - even though it is possible to use the OPP layer so - it just wont be smart to do so considering list scan latencies on hot paths such as cpufreq transitions or idle transitions.

Quoted 4 lines

If the list nodes are not supposed to be added and removed dynamically, it probably would make sense to create data structures containing the "available" OPPs only, once they are known, and simply free the object representing the other ones.

I covered the usage in my reply here:
http://marc.info/?l=linux-arm-kernel&m=128476570300466&w=2
but to repeat, the list is dynamic during initialization but remains static after initialization based on SOC framework implementation - this is best implemented with a list (we had started with an original array implementation which evolved to the current list implementation
http://marc.info/?l=linux-omap&m=125912217718770&w=2)

Signature · 2 lines
Regards,
Nishanth Menon
#47 lore raw

[Trimming the CC list.]

Quoted 1 line · Nishanth Menon

Rafael J. Wysocki had written, on 09/17/2010 06:07 PM, the following:

...

Quoted 6 lines
Quoted 3 lines

Apart from this, it might be a good idea to help callers a bit and actually introduce some sort of locking into the framework.

in OMAP implementation we have need to use these apis from irq_locked contexts as well.. lock implementation will prevent thier necessary usage.

Why so? You can use spin_lock_irqsave/spin_unclock_irqrestore to avoid that.

Quoted 3 lines

instead we have implemented lock mechanisms in higher layers and ensured that the opp table does not change once created - the rest of them are query functions - the risk is on opp_enable/disable.

OK, but this way you practically make the users of the framework duplicate some code for the locking purposes. This is kind of suboptimal, and error prone too.

Quoted 16 lines
Quoted 12 lines
Quoted 11 lines

In terms of the lifetime rules on the nodes in the list:
The list is expected to be maintained once created, entries are expected to be added optimally and not expected to be destroyed, the choice of list implementation was for reducing the complexity of the code itself and not yet meant as a mechanism to dynamically add and delete nodes on the fly.. Essentially, it was intended for the SOC framework to ensure it plugs in the OPP entries optimally and not create a humongous list of all possible OPPs for all families of the vendor SOCs - even though it is possible to use the OPP layer so - it just wont be smart to do so considering list scan latencies on hot paths such as cpufreq transitions or idle transitions.

Quoted 4 lines

If the list nodes are not supposed to be added and removed dynamically, it probably would make sense to create data structures containing the "available" OPPs only, once they are known, and simply free the object representing the other ones.

Quoted 7 lines

I covered the usage in my reply here:
http://marc.info/?l=linux-arm-kernel&m=128476570300466&w=2
but to repeat, the list is dynamic during initialization but remains static after initialization based on SOC framework implementation - this is best implemented with a list (we had started with an original array implementation which evolved to the current list implementation
http://marc.info/?l=linux-omap&m=125912217718770&w=2)

Well, my point is, since the _final_ set of OPPs doesn't really change, there's no need to use a list for storing it in principle.

Your current algorithm seems to be:
(1) Create a list of all _possible_ OPPs.
(2) Mark the ones that can actually be used on the given hardware as "available".
(3) Whenever we need to find an OPP to use, browse the entire list.

This isn't optimal, because the OPPs that are not marked as "available" in (2) will never be used, although they _will_ be inspected while browsing the list. So, I think a better algorithm would be:
(1) Create a list of all possible OPPs.
(2) Drop the nonavailable OPPs from the list.
(3) Whenever we need to find an OPP to use, browse the entire list.

But then, it may be better to simply move the list we get in (2) into an array, because the browsing is going to require fewer memory accesses in that case (also, an array would use less memory than the list). So, perhaps, it's better to change the algorithm even further:
(1) Create a list of all possible OPPs.
(2) Drop the nonavailable OPPs from the list.
(3) Move the list we got in (2) into a sorted array.
(4) Whenever we need to find an OPP to use, browse the array (perhaps using binary search).

Thanks,
Rafael

#48 lore raw

"Rafael J. Wysocki" <[email protected]> writes:

[...]

Quoted 27 lines
Quoted 16 lines
Quoted 12 lines
Quoted 11 lines

In terms of the lifetime rules on the nodes in the list:
The list is expected to be maintained once created, entries are expected to be added optimally and not expected to be destroyed, the choice of list implementation was for reducing the complexity of the code itself and not yet meant as a mechanism to dynamically add and delete nodes on the fly.. Essentially, it was intended for the SOC framework to ensure it plugs in the OPP entries optimally and not create a humongous list of all possible OPPs for all families of the vendor SOCs - even though it is possible to use the OPP layer so - it just wont be smart to do so considering list scan latencies on hot paths such as cpufreq transitions or idle transitions.

Quoted 4 lines

If the list nodes are not supposed to be added and removed dynamically, it probably would make sense to create data structures containing the "available" OPPs only, once they are known, and simply free the object representing the other ones.

Quoted 7 lines

I covered the usage in my reply here:
http://marc.info/?l=linux-arm-kernel&m=128476570300466&w=2
but to repeat, the list is dynamic during initialization but remains static after initialization based on SOC framework implementation - this is best implemented with a list (we had started with an original array implementation which evolved to the current list implementation
http://marc.info/?l=linux-omap&m=125912217718770&w=2)

Well, my point is, since the _final_ set of OPPs doesn't really change, there's no need to use a list for storing it in principle.

Quoted 7 lines

Your current algorithm seems to be:
(1) Create a list of all _possible_ OPPs.
(2) Mark the ones that can actually be used on the given hardware as "available".
(3) Whenever we need to find an OPP to use, browse the entire list. This isn't optimal, because the OPPs that are not marked as "available" in (2) will never be used, although they _will_ be inspected while browsing the list.

A little clarificaion about "will never be used" below...

Quoted 5 lines

So, I think a better algorithm would be:
(1) Create a list of all possible OPPs.
(2) Drop the nonavailable OPPs from the list.
(3) Whenever we need to find an OPP to use, browse the entire list.

Quoted 9 lines

But then, it may be better to simply move the list we get in (2) into an array, because the browsing is going to require fewer memory accesses in that case (also, an array would use less memory than the list). So, perhaps, it's better to change the algorithm even further:
(1) Create a list of all possible OPPs.
(2) Drop the nonavailable OPPs from the list.
(3) Move the list we got in (2) into a sorted array.
(4) Whenever we need to find an OPP to use, browse the array (perhaps using binary search).

Just a little clarification on "available." The intended use of this flag was not just a one-time "available on hardware X." It was also intended to be able to add/remove availbale OPPs dynamically at run-time.

More specifically, it's intended for use to *temporarily* remove an OPP from being selected. The production usage of this would primarily for thermal considerations (e.g. don't use OPPx until the temperature drops)

However, for PM development & debug, we also use this to temporarily take a class of OPPs out of the running for test/debug purposes (e.g. driver X runs great at OPPx and OPPy, but not OPPz.) So the ability to temporarily be selective about OPPs at runtime for debug/development is extremely useful.

So, to summarize, "most of the time", all the OPPs that were added (via opp_add()) will be "available". Ones that are !availble will likely only be so temporarily, so I'm not sure that the overhead of keeping a separate structure for the available and !available OPPs is worth it. Especially, since OPP changes are relatively infrequent.

Kevin

#49 lore raw
Quoted 70 lines · Kevin Hilman

"Rafael J. Wysocki" <[email protected]> writes:

[...]

Quoted 27 lines
Quoted 16 lines
Quoted 12 lines
Quoted 11 lines

In terms of the lifetime rules on the nodes in the list:
The list is expected to be maintained once created, entries are expected to be added optimally and not expected to be destroyed, the choice of list implementation was for reducing the complexity of the code itself and not yet meant as a mechanism to dynamically add and delete nodes on the fly.. Essentially, it was intended for the SOC framework to ensure it plugs in the OPP entries optimally and not create a humongous list of all possible OPPs for all families of the vendor SOCs - even though it is possible to use the OPP layer so - it just wont be smart to do so considering list scan latencies on hot paths such as cpufreq transitions or idle transitions.

Quoted 4 lines

If the list nodes are not supposed to be added and removed dynamically, it probably would make sense to create data structures containing the "available" OPPs only, once they are known, and simply free the object representing the other ones.

Quoted 7 lines

I covered the usage in my reply here:
http://marc.info/?l=linux-arm-kernel&m=128476570300466&w=2
but to repeat, the list is dynamic during initialization but remains static after initialization based on SOC framework implementation - this is best implemented with a list (we had started with an original array implementation which evolved to the current list implementation
http://marc.info/?l=linux-omap&m=125912217718770&w=2)

Well, my point is, since the _final_ set of OPPs doesn't really change, there's no need to use a list for storing it in principle.

Quoted 7 lines

Your current algorithm seems to be:
(1) Create a list of all _possible_ OPPs.
(2) Mark the ones that can actually be used on the given hardware as "available".
(3) Whenever we need to find an OPP to use, browse the entire list. This isn't optimal, because the OPPs that are not marked as "available" in (2) will never be used, although they _will_ be inspected while browsing the list.

A little clarificaion about "will never be used" below...

Quoted 5 lines

So, I think a better algorithm would be:
(1) Create a list of all possible OPPs.
(2) Drop the nonavailable OPPs from the list.
(3) Whenever we need to find an OPP to use, browse the entire list.

Quoted 9 lines

But then, it may be better to simply move the list we get in (2) into an array, because the browsing is going to require fewer memory accesses in that case (also, an array would use less memory than the list). So, perhaps, it's better to change the algorithm even further:
(1) Create a list of all possible OPPs.
(2) Drop the nonavailable OPPs from the list.
(3) Move the list we got in (2) into a sorted array.
(4) Whenever we need to find an OPP to use, browse the array (perhaps using binary search).

Just a little clarification on "available." The intended use of this flag was not just a one-time "available on hardware X." It was also intended to be able to add/remove availbale OPPs dynamically at run-time.

More specifically, it's intended for use to *temporarily* remove an OPP from being selected. The production usage of this would primarily for thermal considerations (e.g. don't use OPPx until the temperature drops)

However, for PM development & debug, we also use this to temporarily take a class of OPPs out of the running for test/debug purposes (e.g. driver X runs great at OPPx and OPPy, but not OPPz.) So the ability to temporarily be selective about OPPs at runtime for debug/development is extremely useful.

Quoted 5 lines

So, to summarize, "most of the time", all the OPPs that were added (via opp_add()) will be "available". Ones that are !availble will likely only be so temporarily, so I'm not sure that the overhead of keeping a separate structure for the available and !available OPPs is worth it. Especially, since OPP changes are relatively infrequent.

Well, the Nishanth's description doesn't match this, so thanks for the clarification.

In that case you might consider using a red-black tree for storing the "available" OPPs, so that you can add-remove them dynamically, but you can avoid a linear search through the entire list every time you need to find and available OPP. Since we have standard helpers for handling rbtrees, that shouldn't be a big deal.

Thanks,
Rafael

#50 lore raw
Quoted 71 lines · Rafael J. Wysocki
Quoted 70 lines · Kevin Hilman

"Rafael J. Wysocki" <[email protected]> writes:

[...]

Quoted 27 lines
Quoted 16 lines
Quoted 12 lines
Quoted 11 lines

In terms of the lifetime rules on the nodes in the list:
The list is expected to be maintained once created, entries are expected to be added optimally and not expected to be destroyed, the choice of list implementation was for reducing the complexity of the code itself and not yet meant as a mechanism to dynamically add and delete nodes on the fly.. Essentially, it was intended for the SOC framework to ensure it plugs in the OPP entries optimally and not create a humongous list of all possible OPPs for all families of the vendor SOCs - even though it is possible to use the OPP layer so - it just wont be smart to do so considering list scan latencies on hot paths such as cpufreq transitions or idle transitions.

Quoted 4 lines

If the list nodes are not supposed to be added and removed dynamically, it probably would make sense to create data structures containing the "available" OPPs only, once they are known, and simply free the object representing the other ones.

Quoted 7 lines

I covered the usage in my reply here:
http://marc.info/?l=linux-arm-kernel&m=128476570300466&w=2
but to repeat, the list is dynamic during initialization but remains static after initialization based on SOC framework implementation - this is best implemented with a list (we had started with an original array implementation which evolved to the current list implementation
http://marc.info/?l=linux-omap&m=125912217718770&w=2)

Well, my point is, since the _final_ set of OPPs doesn't really change, there's no need to use a list for storing it in principle.

Quoted 7 lines

Your current algorithm seems to be:
(1) Create a list of all _possible_ OPPs.
(2) Mark the ones that can actually be used on the given hardware as "available".
(3) Whenever we need to find an OPP to use, browse the entire list. This isn't optimal, because the OPPs that are not marked as "available" in (2) will never be used, although they _will_ be inspected while browsing the list.

A little clarificaion about "will never be used" below...

Quoted 5 lines

So, I think a better algorithm would be:
(1) Create a list of all possible OPPs.
(2) Drop the nonavailable OPPs from the list.
(3) Whenever we need to find an OPP to use, browse the entire list.

Quoted 9 lines

But then, it may be better to simply move the list we get in (2) into an array, because the browsing is going to require fewer memory accesses in that case (also, an array would use less memory than the list). So, perhaps, it's better to change the algorithm even further:
(1) Create a list of all possible OPPs.
(2) Drop the nonavailable OPPs from the list.
(3) Move the list we got in (2) into a sorted array.
(4) Whenever we need to find an OPP to use, browse the array (perhaps using binary search).

Just a little clarification on "available." The intended use of this flag was not just a one-time "available on hardware X." It was also intended to be able to add/remove availbale OPPs dynamically at run-time.

More specifically, it's intended for use to *temporarily* remove an OPP from being selected. The production usage of this would primarily for thermal considerations (e.g. don't use OPPx until the temperature drops)

However, for PM development & debug, we also use this to temporarily take a class of OPPs out of the running for test/debug purposes (e.g. driver X runs great at OPPx and OPPy, but not OPPz.) So the ability to temporarily be selective about OPPs at runtime for debug/development is extremely useful.

Quoted 8 lines
Quoted 5 lines

So, to summarize, "most of the time", all the OPPs that were added (via opp_add()) will be "available". Ones that are !availble will likely only be so temporarily, so I'm not sure that the overhead of keeping a separate structure for the available and !available OPPs is worth it. Especially, since OPP changes are relatively infrequent.

Well, the Nishanth's description doesn't match this, so thanks for the clarification.

Agreed, we need to update the doc file to reflect this.

Quoted 5 lines

In that case you might consider using a red-black tree for storing the "available" OPPs, so that you can add-remove them dynamically, but you can avoid a linear search through the entire list every time you need to find and available OPP. Since we have standard helpers for handling rbtrees, that shouldn't be a big deal.

That's a possibility, but do you think rbtrees are worth it for a relatively small number of OPPs for any given device? We're using this to track a list of OPPs for any struct device, so there may be multiple independent OPP lists, but each would have a small number of OPPs.

For example, on OMAP, while the CPU might have a larger number of OPPs (5-6), most devices will have a small number of OPPs (1-3.) I gather this is similar for many of the embedded SoCs available today.

Considering such a small number of OPPs, is the extra complexity of an rbtree worth it?

Kevin

#51 lore raw
Quoted 95 lines · Kevin Hilman
Quoted 71 lines · Rafael J. Wysocki
Quoted 70 lines · Kevin Hilman

"Rafael J. Wysocki" <[email protected]> writes:

[...]

Quoted 27 lines
Quoted 16 lines
Quoted 12 lines
Quoted 11 lines

In terms of the lifetime rules on the nodes in the list:
The list is expected to be maintained once created, entries are expected to be added optimally and not expected to be destroyed, the choice of list implementation was for reducing the complexity of the code itself and not yet meant as a mechanism to dynamically add and delete nodes on the fly.. Essentially, it was intended for the SOC framework to ensure it plugs in the OPP entries optimally and not create a humongous list of all possible OPPs for all families of the vendor SOCs - even though it is possible to use the OPP layer so - it just wont be smart to do so considering list scan latencies on hot paths such as cpufreq transitions or idle transitions.

Quoted 4 lines

If the list nodes are not supposed to be added and removed dynamically, it probably would make sense to create data structures containing the "available" OPPs only, once they are known, and simply free the object representing the other ones.

Quoted 7 lines

I covered the usage in my reply here:
http://marc.info/?l=linux-arm-kernel&m=128476570300466&w=2
but to repeat, the list is dynamic during initialization but remains static after initialization based on SOC framework implementation - this is best implemented with a list (we had started with an original array implementation which evolved to the current list implementation
http://marc.info/?l=linux-omap&m=125912217718770&w=2)

Well, my point is, since the _final_ set of OPPs doesn't really change, there's no need to use a list for storing it in principle.

Quoted 7 lines

Your current algorithm seems to be:
(1) Create a list of all _possible_ OPPs.
(2) Mark the ones that can actually be used on the given hardware as "available".
(3) Whenever we need to find an OPP to use, browse the entire list. This isn't optimal, because the OPPs that are not marked as "available" in (2) will never be used, although they _will_ be inspected while browsing the list.

A little clarificaion about "will never be used" below...

Quoted 5 lines

So, I think a better algorithm would be:
(1) Create a list of all possible OPPs.
(2) Drop the nonavailable OPPs from the list.
(3) Whenever we need to find an OPP to use, browse the entire list.

Quoted 9 lines

But then, it may be better to simply move the list we get in (2) into an array, because the browsing is going to require fewer memory accesses in that case (also, an array would use less memory than the list). So, perhaps, it's better to change the algorithm even further:
(1) Create a list of all possible OPPs.
(2) Drop the nonavailable OPPs from the list.
(3) Move the list we got in (2) into a sorted array.
(4) Whenever we need to find an OPP to use, browse the array (perhaps using binary search).

Just a little clarification on "available." The intended use of this flag was not just a one-time "available on hardware X." It was also intended to be able to add/remove availbale OPPs dynamically at run-time.

More specifically, it's intended for use to *temporarily* remove an OPP from being selected. The production usage of this would primarily for thermal considerations (e.g. don't use OPPx until the temperature drops)

However, for PM development & debug, we also use this to temporarily take a class of OPPs out of the running for test/debug purposes (e.g. driver X runs great at OPPx and OPPy, but not OPPz.) So the ability to temporarily be selective about OPPs at runtime for debug/development is extremely useful.

Quoted 8 lines
Quoted 5 lines

So, to summarize, "most of the time", all the OPPs that were added (via opp_add()) will be "available". Ones that are !availble will likely only be so temporarily, so I'm not sure that the overhead of keeping a separate structure for the available and !available OPPs is worth it. Especially, since OPP changes are relatively infrequent.

Well, the Nishanth's description doesn't match this, so thanks for the clarification.

Agreed, we need to update the doc file to reflect this.

Quoted 5 lines

In that case you might consider using a red-black tree for storing the "available" OPPs, so that you can add-remove them dynamically, but you can avoid a linear search through the entire list every time you need to find and available OPP. Since we have standard helpers for handling rbtrees, that shouldn't be a big deal.

That's a possibility, but do you think rbtrees are worth it for a relatively small number of OPPs for any given device? We're using this to track a list of OPPs for any struct device, so there may be multiple independent OPP lists, but each would have a small number of OPPs.

Quoted 6 lines

For example, on OMAP, while the CPU might have a larger number of OPPs (5-6), most devices will have a small number of OPPs (1-3.) I gather this is similar for many of the embedded SoCs available today.

Considering such a small number of OPPs, is the extra complexity of an rbtree worth it?

OK, probably not. If there's so few of them, I agree that using lists is probably better, but please put the numbers information into the doc too. :-)

Thanks,
Rafael

#52 lore raw
In reply to Kevin Hilman
Quoted 95 lines · Kevin Hilman
Quoted 71 lines · Rafael J. Wysocki
Quoted 70 lines · Kevin Hilman

"Rafael J. Wysocki" <[email protected]> writes:

[...]

Quoted 27 lines
Quoted 16 lines
Quoted 12 lines
Quoted 11 lines

In terms of the lifetime rules on the nodes in the list:
The list is expected to be maintained once created, entries are expected to be added optimally and not expected to be destroyed, the choice of list implementation was for reducing the complexity of the code itself and not yet meant as a mechanism to dynamically add and delete nodes on the fly.. Essentially, it was intended for the SOC framework to ensure it plugs in the OPP entries optimally and not create a humongous list of all possible OPPs for all families of the vendor SOCs - even though it is possible to use the OPP layer so - it just wont be smart to do so considering list scan latencies on hot paths such as cpufreq transitions or idle transitions.

Quoted 4 lines

If the list nodes are not supposed to be added and removed dynamically, it probably would make sense to create data structures containing the "available" OPPs only, once they are known, and simply free the object representing the other ones.

Quoted 7 lines

I covered the usage in my reply here:
http://marc.info/?l=linux-arm-kernel&m=128476570300466&w=2
but to repeat, the list is dynamic during initialization but remains static after initialization based on SOC framework implementation - this is best implemented with a list (we had started with an original array implementation which evolved to the current list implementation
http://marc.info/?l=linux-omap&m=125912217718770&w=2)

Well, my point is, since the _final_ set of OPPs doesn't really change, there's no need to use a list for storing it in principle.

Quoted 7 lines

Your current algorithm seems to be:
(1) Create a list of all _possible_ OPPs.
(2) Mark the ones that can actually be used on the given hardware as "available".
(3) Whenever we need to find an OPP to use, browse the entire list. This isn't optimal, because the OPPs that are not marked as "available" in (2) will never be used, although they _will_ be inspected while browsing the list.

A little clarificaion about "will never be used" below...

Quoted 5 lines

So, I think a better algorithm would be:
(1) Create a list of all possible OPPs.
(2) Drop the nonavailable OPPs from the list.
(3) Whenever we need to find an OPP to use, browse the entire list.

Quoted 9 lines

But then, it may be better to simply move the list we get in (2) into an array, because the browsing is going to require fewer memory accesses in that case (also, an array would use less memory than the list). So, perhaps, it's better to change the algorithm even further:
(1) Create a list of all possible OPPs.
(2) Drop the nonavailable OPPs from the list.
(3) Move the list we got in (2) into a sorted array.
(4) Whenever we need to find an OPP to use, browse the array (perhaps using binary search).

Just a little clarification on "available." The intended use of this flag was not just a one-time "available on hardware X." It was also intended to be able to add/remove availbale OPPs dynamically at run-time.

More specifically, it's intended for use to *temporarily* remove an OPP from being selected. The production usage of this would primarily for thermal considerations (e.g. don't use OPPx until the temperature drops)

However, for PM development & debug, we also use this to temporarily take a class of OPPs out of the running for test/debug purposes (e.g. driver X runs great at OPPx and OPPy, but not OPPz.) So the ability to temporarily be selective about OPPs at runtime for debug/development is extremely useful.

Quoted 8 lines
Quoted 5 lines

So, to summarize, "most of the time", all the OPPs that were added (via opp_add()) will be "available". Ones that are !availble will likely only be so temporarily, so I'm not sure that the overhead of keeping a separate structure for the available and !available OPPs is worth it. Especially, since OPP changes are relatively infrequent.

Well, the Nishanth's description doesn't match this, so thanks for the clarification.

Agreed, we need to update the doc file to reflect this.

Quoted 5 lines

In that case you might consider using a red-black tree for storing the "available" OPPs, so that you can add-remove them dynamically, but you can avoid a linear search through the entire list every time you need to find and available OPP. Since we have standard helpers for handling rbtrees, that shouldn't be a big deal.

That's a possibility, but do you think rbtrees are worth it for a relatively small number of OPPs for any given device? We're using this to track a list of OPPs for any struct device, so there may be multiple independent OPP lists, but each would have a small number of OPPs.

Quoted 6 lines

For example, on OMAP, while the CPU might have a larger number of OPPs (5-6), most devices will have a small number of OPPs (1-3.) I gather this is similar for many of the embedded SoCs available today.

Considering such a small number of OPPs, is the extra complexity of an rbtree worth it?

OK, probably not. If there's so few of them, I agree that using lists is probably better, but please put the numbers information into the doc too. :-)

Thanks,
Rafael

#53 lore raw
In reply to Rafael J. Wysocki
Quoted 71 lines · Rafael J. Wysocki
Quoted 70 lines · Kevin Hilman

"Rafael J. Wysocki" <[email protected]> writes:

[...]

Quoted 27 lines
Quoted 16 lines
Quoted 12 lines
Quoted 11 lines

In terms of the lifetime rules on the nodes in the list:
The list is expected to be maintained once created, entries are expected to be added optimally and not expected to be destroyed, the choice of list implementation was for reducing the complexity of the code itself and not yet meant as a mechanism to dynamically add and delete nodes on the fly.. Essentially, it was intended for the SOC framework to ensure it plugs in the OPP entries optimally and not create a humongous list of all possible OPPs for all families of the vendor SOCs - even though it is possible to use the OPP layer so - it just wont be smart to do so considering list scan latencies on hot paths such as cpufreq transitions or idle transitions.

Quoted 4 lines

If the list nodes are not supposed to be added and removed dynamically, it probably would make sense to create data structures containing the "available" OPPs only, once they are known, and simply free the object representing the other ones.

Quoted 7 lines

I covered the usage in my reply here:
http://marc.info/?l=linux-arm-kernel&m=128476570300466&w=2
but to repeat, the list is dynamic during initialization but remains static after initialization based on SOC framework implementation - this is best implemented with a list (we had started with an original array implementation which evolved to the current list implementation
http://marc.info/?l=linux-omap&m=125912217718770&w=2)

Well, my point is, since the _final_ set of OPPs doesn't really change, there's no need to use a list for storing it in principle.

Quoted 7 lines

Your current algorithm seems to be:
(1) Create a list of all _possible_ OPPs.
(2) Mark the ones that can actually be used on the given hardware as "available".
(3) Whenever we need to find an OPP to use, browse the entire list. This isn't optimal, because the OPPs that are not marked as "available" in (2) will never be used, although they _will_ be inspected while browsing the list.

A little clarificaion about "will never be used" below...

Quoted 5 lines

So, I think a better algorithm would be:
(1) Create a list of all possible OPPs.
(2) Drop the nonavailable OPPs from the list.
(3) Whenever we need to find an OPP to use, browse the entire list.

Quoted 9 lines

But then, it may be better to simply move the list we get in (2) into an array, because the browsing is going to require fewer memory accesses in that case (also, an array would use less memory than the list). So, perhaps, it's better to change the algorithm even further:
(1) Create a list of all possible OPPs.
(2) Drop the nonavailable OPPs from the list.
(3) Move the list we got in (2) into a sorted array.
(4) Whenever we need to find an OPP to use, browse the array (perhaps using binary search).

Just a little clarification on "available." The intended use of this flag was not just a one-time "available on hardware X." It was also intended to be able to add/remove availbale OPPs dynamically at run-time.

More specifically, it's intended for use to *temporarily* remove an OPP from being selected. The production usage of this would primarily for thermal considerations (e.g. don't use OPPx until the temperature drops)

However, for PM development & debug, we also use this to temporarily take a class of OPPs out of the running for test/debug purposes (e.g. driver X runs great at OPPx and OPPy, but not OPPz.) So the ability to temporarily be selective about OPPs at runtime for debug/development is extremely useful.

Quoted 8 lines
Quoted 5 lines

So, to summarize, "most of the time", all the OPPs that were added (via opp_add()) will be "available". Ones that are !availble will likely only be so temporarily, so I'm not sure that the overhead of keeping a separate structure for the available and !available OPPs is worth it. Especially, since OPP changes are relatively infrequent.

Well, the Nishanth's description doesn't match this, so thanks for the clarification.

Agreed, we need to update the doc file to reflect this.

Quoted 5 lines

In that case you might consider using a red-black tree for storing the "available" OPPs, so that you can add-remove them dynamically, but you can avoid a linear search through the entire list every time you need to find and available OPP. Since we have standard helpers for handling rbtrees, that shouldn't be a big deal.

That's a possibility, but do you think rbtrees are worth it for a relatively small number of OPPs for any given device? We're using this to track a list of OPPs for any struct device, so there may be multiple independent OPP lists, but each would have a small number of OPPs.

For example, on OMAP, while the CPU might have a larger number of OPPs (5-6), most devices will have a small number of OPPs (1-3.) I gather this is similar for many of the embedded SoCs available today.

Considering such a small number of OPPs, is the extra complexity of an rbtree worth it?

Kevin

#54 lore raw
In reply to Kevin Hilman
Quoted 70 lines · Kevin Hilman

"Rafael J. Wysocki" <[email protected]> writes:

[...]

Quoted 27 lines
Quoted 16 lines
Quoted 12 lines
Quoted 11 lines

In terms of the lifetime rules on the nodes in the list:
The list is expected to be maintained once created, entries are expected to be added optimally and not expected to be destroyed, the choice of list implementation was for reducing the complexity of the code itself and not yet meant as a mechanism to dynamically add and delete nodes on the fly.. Essentially, it was intended for the SOC framework to ensure it plugs in the OPP entries optimally and not create a humongous list of all possible OPPs for all families of the vendor SOCs - even though it is possible to use the OPP layer so - it just wont be smart to do so considering list scan latencies on hot paths such as cpufreq transitions or idle transitions.

Quoted 4 lines

If the list nodes are not supposed to be added and removed dynamically, it probably would make sense to create data structures containing the "available" OPPs only, once they are known, and simply free the object representing the other ones.

Quoted 7 lines

I covered the usage in my reply here:
http://marc.info/?l=linux-arm-kernel&m=128476570300466&w=2
but to repeat, the list is dynamic during initialization but remains static after initialization based on SOC framework implementation - this is best implemented with a list (we had started with an original array implementation which evolved to the current list implementation
http://marc.info/?l=linux-omap&m=125912217718770&w=2)

Well, my point is, since the _final_ set of OPPs doesn't really change, there's no need to use a list for storing it in principle.

Quoted 7 lines

Your current algorithm seems to be:
(1) Create a list of all _possible_ OPPs.
(2) Mark the ones that can actually be used on the given hardware as "available".
(3) Whenever we need to find an OPP to use, browse the entire list. This isn't optimal, because the OPPs that are not marked as "available" in (2) will never be used, although they _will_ be inspected while browsing the list.

A little clarificaion about "will never be used" below...

Quoted 5 lines

So, I think a better algorithm would be:
(1) Create a list of all possible OPPs.
(2) Drop the nonavailable OPPs from the list.
(3) Whenever we need to find an OPP to use, browse the entire list.

Quoted 9 lines

But then, it may be better to simply move the list we get in (2) into an array, because the browsing is going to require fewer memory accesses in that case (also, an array would use less memory than the list). So, perhaps, it's better to change the algorithm even further:
(1) Create a list of all possible OPPs.
(2) Drop the nonavailable OPPs from the list.
(3) Move the list we got in (2) into a sorted array.
(4) Whenever we need to find an OPP to use, browse the array (perhaps using binary search).

Just a little clarification on "available." The intended use of this flag was not just a one-time "available on hardware X." It was also intended to be able to add/remove availbale OPPs dynamically at run-time.

More specifically, it's intended for use to *temporarily* remove an OPP from being selected. The production usage of this would primarily for thermal considerations (e.g. don't use OPPx until the temperature drops)

However, for PM development & debug, we also use this to temporarily take a class of OPPs out of the running for test/debug purposes (e.g. driver X runs great at OPPx and OPPy, but not OPPz.) So the ability to temporarily be selective about OPPs at runtime for debug/development is extremely useful.

Quoted 5 lines

So, to summarize, "most of the time", all the OPPs that were added (via opp_add()) will be "available". Ones that are !availble will likely only be so temporarily, so I'm not sure that the overhead of keeping a separate structure for the available and !available OPPs is worth it. Especially, since OPP changes are relatively infrequent.

Well, the Nishanth's description doesn't match this, so thanks for the clarification.

In that case you might consider using a red-black tree for storing the "available" OPPs, so that you can add-remove them dynamically, but you can avoid a linear search through the entire list every time you need to find and available OPP. Since we have standard helpers for handling rbtrees, that shouldn't be a big deal.

Thanks,
Rafael

#55 lore raw
In reply to Rafael J. Wysocki

"Rafael J. Wysocki" <[email protected]> writes:

[...]

Quoted 27 lines
Quoted 16 lines
Quoted 12 lines
Quoted 11 lines

In terms of the lifetime rules on the nodes in the list:
The list is expected to be maintained once created, entries are expected to be added optimally and not expected to be destroyed, the choice of list implementation was for reducing the complexity of the code itself and not yet meant as a mechanism to dynamically add and delete nodes on the fly.. Essentially, it was intended for the SOC framework to ensure it plugs in the OPP entries optimally and not create a humongous list of all possible OPPs for all families of the vendor SOCs - even though it is possible to use the OPP layer so - it just wont be smart to do so considering list scan latencies on hot paths such as cpufreq transitions or idle transitions.

Quoted 4 lines

If the list nodes are not supposed to be added and removed dynamically, it probably would make sense to create data structures containing the "available" OPPs only, once they are known, and simply free the object representing the other ones.

Quoted 7 lines

I covered the usage in my reply here:
http://marc.info/?l=linux-arm-kernel&m=128476570300466&w=2
but to repeat, the list is dynamic during initialization but remains static after initialization based on SOC framework implementation - this is best implemented with a list (we had started with an original array implementation which evolved to the current list implementation
http://marc.info/?l=linux-omap&m=125912217718770&w=2)

Well, my point is, since the _final_ set of OPPs doesn't really change, there's no need to use a list for storing it in principle.

Quoted 7 lines

Your current algorithm seems to be:
(1) Create a list of all _possible_ OPPs.
(2) Mark the ones that can actually be used on the given hardware as "available".
(3) Whenever we need to find an OPP to use, browse the entire list. This isn't optimal, because the OPPs that are not marked as "available" in (2) will never be used, although they _will_ be inspected while browsing the list.

A little clarificaion about "will never be used" below...

Quoted 5 lines

So, I think a better algorithm would be:
(1) Create a list of all possible OPPs.
(2) Drop the nonavailable OPPs from the list.
(3) Whenever we need to find an OPP to use, browse the entire list.

Quoted 9 lines

But then, it may be better to simply move the list we get in (2) into an array, because the browsing is going to require fewer memory accesses in that case (also, an array would use less memory than the list). So, perhaps, it's better to change the algorithm even further:
(1) Create a list of all possible OPPs.
(2) Drop the nonavailable OPPs from the list.
(3) Move the list we got in (2) into a sorted array.
(4) Whenever we need to find an OPP to use, browse the array (perhaps using binary search).

Just a little clarification on "available." The intended use of this flag was not just a one-time "available on hardware X." It was also intended to be able to add/remove availbale OPPs dynamically at run-time.

More specifically, it's intended for use to *temporarily* remove an OPP from being selected. The production usage of this would primarily for thermal considerations (e.g. don't use OPPx until the temperature drops)

However, for PM development & debug, we also use this to temporarily take a class of OPPs out of the running for test/debug purposes (e.g. driver X runs great at OPPx and OPPy, but not OPPz.) So the ability to temporarily be selective about OPPs at runtime for debug/development is extremely useful.

So, to summarize, "most of the time", all the OPPs that were added (via opp_add()) will be "available". Ones that are !availble will likely only be so temporarily, so I'm not sure that the overhead of keeping a separate structure for the available and !available OPPs is worth it. Especially, since OPP changes are relatively infrequent.

Kevin

#56 lore raw
In reply to Nishanth Menon

[Trimming the CC list.]

Quoted 1 line · Nishanth Menon

Rafael J. Wysocki had written, on 09/17/2010 06:07 PM, the following:

...

Quoted 6 lines
Quoted 3 lines

Apart from this, it might be a good idea to help callers a bit and actually introduce some sort of locking into the framework.

in OMAP implementation we have need to use these apis from irq_locked contexts as well.. lock implementation will prevent thier necessary usage.

Why so? You can use spin_lock_irqsave/spin_unclock_irqrestore to avoid that.

Quoted 3 lines

instead we have implemented lock mechanisms in higher layers and ensured that the opp table does not change once created - the rest of them are query functions - the risk is on opp_enable/disable.

OK, but this way you practically make the users of the framework duplicate some code for the locking purposes. This is kind of suboptimal, and error prone too.

Quoted 16 lines
Quoted 12 lines
Quoted 11 lines

In terms of the lifetime rules on the nodes in the list:
The list is expected to be maintained once created, entries are expected to be added optimally and not expected to be destroyed, the choice of list implementation was for reducing the complexity of the code itself and not yet meant as a mechanism to dynamically add and delete nodes on the fly.. Essentially, it was intended for the SOC framework to ensure it plugs in the OPP entries optimally and not create a humongous list of all possible OPPs for all families of the vendor SOCs - even though it is possible to use the OPP layer so - it just wont be smart to do so considering list scan latencies on hot paths such as cpufreq transitions or idle transitions.

Quoted 4 lines

If the list nodes are not supposed to be added and removed dynamically, it probably would make sense to create data structures containing the "available" OPPs only, once they are known, and simply free the object representing the other ones.

Quoted 7 lines

I covered the usage in my reply here:
http://marc.info/?l=linux-arm-kernel&m=128476570300466&w=2
but to repeat, the list is dynamic during initialization but remains static after initialization based on SOC framework implementation - this is best implemented with a list (we had started with an original array implementation which evolved to the current list implementation
http://marc.info/?l=linux-omap&m=125912217718770&w=2)

Well, my point is, since the _final_ set of OPPs doesn't really change, there's no need to use a list for storing it in principle.

Your current algorithm seems to be:
(1) Create a list of all _possible_ OPPs.
(2) Mark the ones that can actually be used on the given hardware as "available".
(3) Whenever we need to find an OPP to use, browse the entire list.

This isn't optimal, because the OPPs that are not marked as "available" in (2) will never be used, although they _will_ be inspected while browsing the list. So, I think a better algorithm would be:
(1) Create a list of all possible OPPs.
(2) Drop the nonavailable OPPs from the list.
(3) Whenever we need to find an OPP to use, browse the entire list.

But then, it may be better to simply move the list we get in (2) into an array, because the browsing is going to require fewer memory accesses in that case (also, an array would use less memory than the list). So, perhaps, it's better to change the algorithm even further:
(1) Create a list of all possible OPPs.
(2) Drop the nonavailable OPPs from the list.
(3) Move the list we got in (2) into a sorted array.
(4) Whenever we need to find an OPP to use, browse the array (perhaps using binary search).

Thanks,
Rafael

#57 lore raw

PATCH 1/1 opp: introduce library for device-specific OPPs

In reply to Rafael J. Wysocki
Quoted 7 lines · Rafael J. Wysocki
Quoted 3 lines · Nishanth Menon

there were a few OMAP followon patches from
http://marc.info/?l=linux-omap&m=128152135801634&w=2
which will actually use this for OMAP3 platform.

Still, the $subject patch doesn't use it. So perhaps add it in a separate patch?

By the same argument nothing in this patch causes the code to become used so we may as well not include it at all :)

More seriously, looking at a lot of the questions that people are raising it might make the review a bit easier if the patch were posted in a series along with an initial user or two to provide some context for the expected usage.

#58 lore raw
In reply to Andrew Morton

Andrew Morton had written, on 09/17/2010 02:19 PM, the following:

Quoted 3 lines

On Thu, 16 Sep 2010 20:29:33 -0500
Nishanth Menon <[email protected]> wrote:

[...]

Quoted 10 lines
Quoted 6 lines
 Documentation/power/00-INDEX |    2 +
 include/linux/opp.h          |  136 +++++++++++++
 kernel/power/Kconfig         |   14 ++
 lib/Makefile                 |    2 +
 lib/opp.c                    |  440 ++++++++++++++++++++++++++++++++++++++++++

./lib/ is an unusual place to put a driver-like thing such as this. The lib/ directory is mainly for generic kernel-wide support things. I'd suggest that ./drivers/opp/ would be a better place.

we had an interesting debate on this topic on the the thread starting
http://marc.info/?l=linux-arm-kernel&m=128465710624421&w=2

It really does not provide any driver like feature here. it is just a bunch of helper functions which any SOC framework can use to build up to implement either:
a) cpufreq implementation with various OPPs
b) bootup in a specific opp in a set of supported OPPs and stay there (e.g. mpurate support on OMAP platforms)

I had considered putting it in drivers/power, but it looks to contain mostly regulator stuff, the other alternative would be to include it in kernel/power or as Kevin H, Linus W and you mention drivers/opp

Personally, I dont have a strong feeling about it as long as it is reusable across SOCs and am hoping to hear some alignment :).

Quoted 17 lines
Quoted 4 lines

...

include/linux/opp.h new file +1 -0
+/*

include/linux/opp.h line 54

Quoted 10 lines
include/linux/opp.h new file +10 -0
+ * Initialization wrapper used to define an OPP.
+ * To point at the end of a terminator of a list of OPPs,
+ * use OPP_DEF(0, 0, 0)
+ */
+#define OPP_DEF(_enabled, _freq, _uv)	\
+{						\
+	.enabled	= _enabled,		\
+	.freq		= _freq,		\
+	.u_volt		= _uv,			\
+}

OPP_DEF is a somewhat atypical name. OPP_INITIALIZER would be more conventional.

Thanks. will incorporate this in v2 of my patchset.

Quoted 3 lines

However OPP_DEF has no usage in this patch so perhaps this can be removed?

there were a few OMAP followon patches from
http://marc.info/?l=linux-omap&m=128152135801634&w=2
which will actually use this for OMAP3 platform.

Quoted 7 lines
Quoted 2 lines

+static LIST_HEAD(dev_opp_list);

There's no locking for this list. That's OK under some circumstances, but I do think there should be a comment here explaining this apparent
bug: why is no locking needed, what are the lifetime rules for entries on this list.

Locking:
arrgh.. my bad.. I had documented it in the missing and later on posted opp.txt http://marc.info/?l=linux-arm-kernel&m=128473931626114&w=2

The OPP layer implementation expects to be used in multiple contexts (including calls from interrupt locked context) based on SOC framework implementation. It is recommended to use appropriate locking schemes within the SOC framework itself.

In terms of the lifetime rules on the nodes in the list:
The list is expected to be maintained once created, entries are expected to be added optimally and not expected to be destroyed, the choice of list implementation was for reducing the complexity of the code itself and not yet meant as a mechanism to dynamically add and delete nodes on the fly.. Essentially, it was intended for the SOC framework to ensure it plugs in the OPP entries optimally and not create a humungous list of all possible OPPs for all families of the vendor SOCs - even though it is possible to use the OPP layer so - it just wont be smart to do so considering list scan latencies on hot paths such as cpufreq transitions or idle transitions.

I should add comments to the effect of the same to the list header and documentation - will do this as part of v2.

Quoted 3 lines

Also, the _ordering_ of items on this list is significant. It should also be documented.

Ack. thanks for catching this. Will do it in v2.

Quoted 10 lines
Quoted 5 lines

...

+/**
+ * opp_get_voltage() - Gets the voltage corresponding to an opp

Usually the () is omitted from function names in kerneldoc comments. It might be OK, or it might produce strange output - I haven't checked.

http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=blob;f=Documentation/kernel-doc-nano-HOWTO.txt;h=3d8a97747f7731c801ca7d3a1483858feeb76b6c;hb=HEAD#l55
I wonder why the example protos use ()

Quoted 11 lines
Quoted 9 lines

...

+/**
+ * opp_find_freq_exact() - search for an exact frequency
+ * @dev: device for which we do this operation
+ * @freq: frequency to search for
+ * @enabled: enabled/disabled OPP to search for
+ *
+ * Searches for exact match in the opp list and returns handle to the matching

s/handle/pointer/

Thanks.. will fix in v2.

Quoted 40 lines
Quoted 40 lines

+ * opp if found, else returns ERR_PTR in case of error and should be handled
+ * using IS_ERR.
+ *
+ * Note: enabled is a modifier for the search. if enabled=true, then the match
+ * is for exact matching frequency and is enabled. if false, the match is for
+ * exact frequency which is disabled.
+ */

...

lib/opp.c new file +29 -0
+int opp_add(struct device *dev, const struct opp_def *opp_def)
+{
+	struct device_opp *tmp_dev_opp, *dev_opp = NULL;
+	struct opp *opp, *new_opp;
+	struct list_head *head;
+
+	/* Check for existing list for 'dev' */
+	list_for_each_entry(tmp_dev_opp, &dev_opp_list, node) {
+		if (dev == tmp_dev_opp->dev) {
+			dev_opp = tmp_dev_opp;
+			break;
+		}
+	}
+
+	if (!dev_opp) {
+		/* Allocate a new device OPP table */
+		dev_opp = kzalloc(sizeof(struct device_opp), GFP_KERNEL);
+		if (!dev_opp) {
+			pr_warning("%s: unable to allocate device struct\n",
+				__func__);
+			return -ENOMEM;
+		}
+
+		dev_opp->dev = dev;
+		INIT_LIST_HEAD(&dev_opp->opp_list);
+
+		list_add(&dev_opp->node, &dev_opp_list);
+	}
+
Quoted 8 lines

lib/opp.c line 323

Quoted 5 lines
lib/opp.c new file +5 -0
+	/* allocate new OPP node */
+	new_opp = kzalloc(sizeof(struct opp), GFP_KERNEL);
+	if (!new_opp) {
+		if (list_empty(&dev_opp->opp_list)) {
+			list_del(&dev_opp->node);

It would be neater to move the list_add() down to after the allocation of new_opp and to remove this list_del().

ack. thanks.. yes it does clean it up. additionally i expanded the static opp_populate which is used just once - removed one function call ;)

Quoted 8 lines

lib/opp.c line 330

Quoted 8 lines
lib/opp.c new file +7 -0
+			kfree(dev_opp);
+		}
+		pr_warning("%s: unable to allocate new opp node\n",
+			__func__);
+		return -ENOMEM;
+	}
+	opp_populate(new_opp, opp_def);
         ^^^^^^^^^^^^
Quoted 8 lines
Quoted 8 lines
lib/opp.c new file +8 -0
+
+	/* Insert new OPP in order of increasing frequency */
+	head = &dev_opp->opp_list;
+	list_for_each_entry_reverse(opp, &dev_opp->opp_list, node) {
+		if (new_opp->rate >= opp->rate) {
+			head = &opp->node;
+			break;
+		}
Quoted 8 lines

lib/opp.c line 343

Quoted 5 lines
lib/opp.c new file +5 -0
+	}
+	list_add(&new_opp->node, head);
+	dev_opp->opp_count++;
+	if (new_opp->enabled)
+		dev_opp->enabled_opp_count++;

These non-atomic read-modify-write operations on *dev_opp have no locking. What prevents races here?

explained above. it is the job of the framework using the OPP Layer library.

Quoted 38 lines
Quoted 38 lines
lib/opp.c new file +34 -0
+	return 0;
+}
+
 
...
 
+void opp_init_cpufreq_table(struct device *dev,
+			    struct cpufreq_frequency_table **table)
+{
+	struct device_opp *dev_opp;
+	struct opp *opp;
+	struct cpufreq_frequency_table *freq_table;
+	int i = 0;
+
+	dev_opp = find_device_opp(dev);
+	if (IS_ERR(dev_opp)) {
+		pr_warning("%s: unable to find device\n", __func__);
+		return;
+	}
+
+	freq_table = kzalloc(sizeof(struct cpufreq_frequency_table) *
+			     (dev_opp->enabled_opp_count + 1), GFP_ATOMIC);
+	if (!freq_table) {
+		pr_warning("%s: failed to allocate frequency table\n",
+			   __func__);
+		return;
+	}
+
+	list_for_each_entry(opp, &dev_opp->opp_list, node) {
+		if (opp->enabled) {
+			freq_table[i].index = i;
+			freq_table[i].frequency = opp->rate / 1000;
+			i++;
+		}
+	}
+
+	freq_table[i].index = i;
Quoted 8 lines

lib/opp.c line 440

Quoted 4 lines
lib/opp.c new file +4 -0
+	freq_table[i].frequency = CPUFREQ_TABLE_END;
+
+	*table = &freq_table[0];
+}

So we're playing with cpufreq internals here but there's no #ifdef CONFIG_CPUFREQ and there's no Kconfig dependency on cpufreq. That needs fixing I think, if only from a reduce-code-bloat perspective.

Thanks and ouch.. Again missing documentation. Apologies.
http://marc.info/?l=linux-arm-kernel&m=128473931626114&w=2
c) Dependency of OPP layer is on CONFIG_PM as certain SOCs such as Texas Instrument's OMAP support have frameworks to optionally boot at a certain opp without needing cpufreq.

This is called "mpurate" bootarg parameter in OMAP framework. I will put this under #ifdef CPUFREQ and provide header coverage for the same appropriately.

Signature · 2 lines
Regards,
Nishanth Menon
#59 lore raw

PATCH 1/1 opp: introduce library for device-specific OPPs

In reply to Andrew Morton
Quoted 48 lines · Andrew Morton
Quoted 37 lines · On Thu, 16 Sep 2010 20:29:33 -0500 Nishanth Menon <[email protected]> wrote:

SOCs have a standard set of tuples consisting of frequency and voltage pairs that the device will support per voltage domain. These are called Operating Performance Points or OPPs. The actual definitions of Operating Performance Points varies over silicon within the same family of devices. For a specific domain, you can have a set of {frequency, voltage} pairs. As the kernel boots and more information is available, a set of these are activated based on the precise nature of device the kernel boots up on. It is interesting to remember that each IP which belongs to a voltage domain may define their own set of OPPs on top of this.

To implement an OPP, some sort of power management support is necessary hence this library enablement depends on CONFIG_PM, however this does not fit into the core power framework as it is an independent library. This is hence introduced under lib allowing all architectures to selectively enable the feature based on thier capabilities.

Contributions include:
Sanjeev Premi for the initial concept:

	http://patchwork.kernel.org/patch/50998/

Kevin Hilman for converting original design to device-based Kevin Hilman and Paul Walmsey for cleaning up many of the function abstractions, improvements and data structure handling Romit Dasgupta for using enums instead of opp pointers Thara Gopinath, Eduardo Valentin and Vishwanath BS for fixes and cleanups.
Linus Walleij for recommending this layer be made generic for usage in other architectures beyond OMAP and ARM.

Discussions and comments from:
http://marc.info/?l=linux-omap&m=126033945313269&w=2
http://marc.info/?l=linux-omap&m=125482970102327&w=2
http://marc.info/?t=125809247500002&r=1&w=2
http://marc.info/?l=linux-omap&m=126025973426007&w=2
http://marc.info/?t=128152609200064&r=1&w=2
incorporated.

Quoted 7 lines

...

 Documentation/power/00-INDEX |    2 +
 include/linux/opp.h          |  136 +++++++++++++
 kernel/power/Kconfig         |   14 ++
 lib/Makefile                 |    2 +
 lib/opp.c                    |  440 ++++++++++++++++++++++++++++++++++++++++++
Quoted 3 lines

./lib/ is an unusual place to put a driver-like thing such as this. The lib/ directory is mainly for generic kernel-wide support things. I'd suggest that ./drivers/opp/ would be a better place.

Well, there are a few similar things in drivers/base/power already.

I agree with all of your comments below.

Thanks,
Rafael

Quoted 172 lines

include/linux/opp.h line 54

Quoted 13 lines

...

include/linux/opp.h new file +11 -0
+/*
+ * Initialization wrapper used to define an OPP.
+ * To point at the end of a terminator of a list of OPPs,
+ * use OPP_DEF(0, 0, 0)
+ */
+#define OPP_DEF(_enabled, _freq, _uv)	\
+{						\
+	.enabled	= _enabled,		\
+	.freq		= _freq,		\
+	.u_volt		= _uv,			\
+}

OPP_DEF is a somewhat atypical name. OPP_INITIALIZER would be more conventional.

However OPP_DEF has no usage in this patch so perhaps this can be removed?

Quoted 1 line

+static LIST_HEAD(dev_opp_list);

There's no locking for this list. That's OK under some circumstances, but I do think there should be a comment here explaining this apparent
bug: why is no locking needed, what are the lifetime rules for entries on this list.

Also, the _ordering_ of items on this list is significant. It should also be documented.

Quoted 5 lines

...

+/**
+ * opp_get_voltage() - Gets the voltage corresponding to an opp

Usually the () is omitted from function names in kerneldoc comments. It might be OK, or it might produce strange output - I haven't checked.

Quoted 10 lines

...

+/**
+ * opp_find_freq_exact() - search for an exact frequency
+ * @dev: device for which we do this operation
+ * @freq: frequency to search for
+ * @enabled: enabled/disabled OPP to search for
+ *
+ * Searches for exact match in the opp list and returns handle to the matching

s/handle/pointer/

Quoted 34 lines

+ * opp if found, else returns ERR_PTR in case of error and should be handled
+ * using IS_ERR.
+ *
+ * Note: enabled is a modifier for the search. if enabled=true, then the match
+ * is for exact matching frequency and is enabled. if false, the match is for
+ * exact frequency which is disabled.
+ */

...

lib/opp.c new file +24 -0
+int opp_add(struct device *dev, const struct opp_def *opp_def)
+{
+	struct device_opp *tmp_dev_opp, *dev_opp = NULL;
+	struct opp *opp, *new_opp;
+	struct list_head *head;
+
+	/* Check for existing list for 'dev' */
+	list_for_each_entry(tmp_dev_opp, &dev_opp_list, node) {
+		if (dev == tmp_dev_opp->dev) {
+			dev_opp = tmp_dev_opp;
+			break;
+		}
+	}
+
+	if (!dev_opp) {
+		/* Allocate a new device OPP table */
+		dev_opp = kzalloc(sizeof(struct device_opp), GFP_KERNEL);
+		if (!dev_opp) {
+			pr_warning("%s: unable to allocate device struct\n",
+				__func__);
+			return -ENOMEM;
+		}
+
+		dev_opp->dev = dev;

lib/opp.c line 323

Quoted 10 lines
lib/opp.c new file +10 -0
+		INIT_LIST_HEAD(&dev_opp->opp_list);
+
+		list_add(&dev_opp->node, &dev_opp_list);
+	}
+
+	/* allocate new OPP node */
+	new_opp = kzalloc(sizeof(struct opp), GFP_KERNEL);
+	if (!new_opp) {
+		if (list_empty(&dev_opp->opp_list)) {
+			list_del(&dev_opp->node);

It would be neater to move the list_add() down to after the allocation of new_opp and to remove this list_del().

Quoted 10 lines
lib/opp.c new file +10 -0
+			kfree(dev_opp);
+		}
+		pr_warning("%s: unable to allocate new opp node\n",
+			__func__);
+		return -ENOMEM;
+	}
+	opp_populate(new_opp, opp_def);
+
+	/* Insert new OPP in order of increasing frequency */
+	head = &dev_opp->opp_list;

lib/opp.c line 343

Quoted 10 lines
lib/opp.c new file +10 -0
+	list_for_each_entry_reverse(opp, &dev_opp->opp_list, node) {
+		if (new_opp->rate >= opp->rate) {
+			head = &opp->node;
+			break;
+		}
+	}
+	list_add(&new_opp->node, head);
+	dev_opp->opp_count++;
+	if (new_opp->enabled)
+		dev_opp->enabled_opp_count++;

These non-atomic read-modify-write operations on *dev_opp have no locking. What prevents races here?

Quoted 31 lines
lib/opp.c new file +28 -0
+	return 0;
+}
+
 
...
 
+void opp_init_cpufreq_table(struct device *dev,
+			    struct cpufreq_frequency_table **table)
+{
+	struct device_opp *dev_opp;
+	struct opp *opp;
+	struct cpufreq_frequency_table *freq_table;
+	int i = 0;
+
+	dev_opp = find_device_opp(dev);
+	if (IS_ERR(dev_opp)) {
+		pr_warning("%s: unable to find device\n", __func__);
+		return;
+	}
+
+	freq_table = kzalloc(sizeof(struct cpufreq_frequency_table) *
+			     (dev_opp->enabled_opp_count + 1), GFP_ATOMIC);
+	if (!freq_table) {
+		pr_warning("%s: failed to allocate frequency table\n",
+			   __func__);
+		return;
+	}
+
+	list_for_each_entry(opp, &dev_opp->opp_list, node) {
+		if (opp->enabled) {
+			freq_table[i].index = i;

lib/opp.c line 440

Quoted 10 lines
lib/opp.c new file +10 -0
+			freq_table[i].frequency = opp->rate / 1000;
+			i++;
+		}
+	}
+
+	freq_table[i].index = i;
+	freq_table[i].frequency = CPUFREQ_TABLE_END;
+
+	*table = &freq_table[0];
+}

So we're playing with cpufreq internals here but there's no #ifdef CONFIG_CPUFREQ and there's no Kconfig dependency on cpufreq. That needs fixing I think, if only from a reduce-code-bloat perspective.

maintainer
#60 lore raw
In reply to Nishanth Menon
Quoted 37 lines · On Thu, 16 Sep 2010 20:29:33 -0500 Nishanth Menon <[email protected]> wrote:

SOCs have a standard set of tuples consisting of frequency and voltage pairs that the device will support per voltage domain. These are called Operating Performance Points or OPPs. The actual definitions of Operating Performance Points varies over silicon within the same family of devices. For a specific domain, you can have a set of {frequency, voltage} pairs. As the kernel boots and more information is available, a set of these are activated based on the precise nature of device the kernel boots up on. It is interesting to remember that each IP which belongs to a voltage domain may define their own set of OPPs on top of this.

To implement an OPP, some sort of power management support is necessary hence this library enablement depends on CONFIG_PM, however this does not fit into the core power framework as it is an independent library. This is hence introduced under lib allowing all architectures to selectively enable the feature based on thier capabilities.

Contributions include:
Sanjeev Premi for the initial concept:

	http://patchwork.kernel.org/patch/50998/

Kevin Hilman for converting original design to device-based Kevin Hilman and Paul Walmsey for cleaning up many of the function abstractions, improvements and data structure handling Romit Dasgupta for using enums instead of opp pointers Thara Gopinath, Eduardo Valentin and Vishwanath BS for fixes and cleanups.
Linus Walleij for recommending this layer be made generic for usage in other architectures beyond OMAP and ARM.

Discussions and comments from:
http://marc.info/?l=linux-omap&m=126033945313269&w=2
http://marc.info/?l=linux-omap&m=125482970102327&w=2
http://marc.info/?t=125809247500002&r=1&w=2
http://marc.info/?l=linux-omap&m=126025973426007&w=2
http://marc.info/?t=128152609200064&r=1&w=2
incorporated.

Quoted 7 lines

...

 Documentation/power/00-INDEX |    2 +
 include/linux/opp.h          |  136 +++++++++++++
 kernel/power/Kconfig         |   14 ++
 lib/Makefile                 |    2 +
 lib/opp.c                    |  440 ++++++++++++++++++++++++++++++++++++++++++

./lib/ is an unusual place to put a driver-like thing such as this. The lib/ directory is mainly for generic kernel-wide support things. I'd suggest that ./drivers/opp/ would be a better place.

Quoted 4 lines

...

include/linux/opp.h new file +1 -0
+/*

include/linux/opp.h line 54

Quoted 10 lines
include/linux/opp.h new file +10 -0
+ * Initialization wrapper used to define an OPP.
+ * To point at the end of a terminator of a list of OPPs,
+ * use OPP_DEF(0, 0, 0)
+ */
+#define OPP_DEF(_enabled, _freq, _uv)	\
+{						\
+	.enabled	= _enabled,		\
+	.freq		= _freq,		\
+	.u_volt		= _uv,			\
+}

OPP_DEF is a somewhat atypical name. OPP_INITIALIZER would be more conventional.

However OPP_DEF has no usage in this patch so perhaps this can be removed?

Quoted 1 line

+static LIST_HEAD(dev_opp_list);

There's no locking for this list. That's OK under some circumstances, but I do think there should be a comment here explaining this apparent
bug: why is no locking needed, what are the lifetime rules for entries on this list.

Also, the _ordering_ of items on this list is significant. It should also be documented.

Quoted 5 lines

...

+/**
+ * opp_get_voltage() - Gets the voltage corresponding to an opp

Usually the () is omitted from function names in kerneldoc comments. It might be OK, or it might produce strange output - I haven't checked.

Quoted 10 lines

...

+/**
+ * opp_find_freq_exact() - search for an exact frequency
+ * @dev: device for which we do this operation
+ * @freq: frequency to search for
+ * @enabled: enabled/disabled OPP to search for
+ *
+ * Searches for exact match in the opp list and returns handle to the matching

s/handle/pointer/

Quoted 34 lines

+ * opp if found, else returns ERR_PTR in case of error and should be handled
+ * using IS_ERR.
+ *
+ * Note: enabled is a modifier for the search. if enabled=true, then the match
+ * is for exact matching frequency and is enabled. if false, the match is for
+ * exact frequency which is disabled.
+ */

...

lib/opp.c new file +24 -0
+int opp_add(struct device *dev, const struct opp_def *opp_def)
+{
+	struct device_opp *tmp_dev_opp, *dev_opp = NULL;
+	struct opp *opp, *new_opp;
+	struct list_head *head;
+
+	/* Check for existing list for 'dev' */
+	list_for_each_entry(tmp_dev_opp, &dev_opp_list, node) {
+		if (dev == tmp_dev_opp->dev) {
+			dev_opp = tmp_dev_opp;
+			break;
+		}
+	}
+
+	if (!dev_opp) {
+		/* Allocate a new device OPP table */
+		dev_opp = kzalloc(sizeof(struct device_opp), GFP_KERNEL);
+		if (!dev_opp) {
+			pr_warning("%s: unable to allocate device struct\n",
+				__func__);
+			return -ENOMEM;
+		}
+
+		dev_opp->dev = dev;

lib/opp.c line 323

Quoted 10 lines
lib/opp.c new file +10 -0
+		INIT_LIST_HEAD(&dev_opp->opp_list);
+
+		list_add(&dev_opp->node, &dev_opp_list);
+	}
+
+	/* allocate new OPP node */
+	new_opp = kzalloc(sizeof(struct opp), GFP_KERNEL);
+	if (!new_opp) {
+		if (list_empty(&dev_opp->opp_list)) {
+			list_del(&dev_opp->node);

It would be neater to move the list_add() down to after the allocation of new_opp and to remove this list_del().

Quoted 10 lines
lib/opp.c new file +10 -0
+			kfree(dev_opp);
+		}
+		pr_warning("%s: unable to allocate new opp node\n",
+			__func__);
+		return -ENOMEM;
+	}
+	opp_populate(new_opp, opp_def);
+
+	/* Insert new OPP in order of increasing frequency */
+	head = &dev_opp->opp_list;

lib/opp.c line 343

Quoted 10 lines
lib/opp.c new file +10 -0
+	list_for_each_entry_reverse(opp, &dev_opp->opp_list, node) {
+		if (new_opp->rate >= opp->rate) {
+			head = &opp->node;
+			break;
+		}
+	}
+	list_add(&new_opp->node, head);
+	dev_opp->opp_count++;
+	if (new_opp->enabled)
+		dev_opp->enabled_opp_count++;

These non-atomic read-modify-write operations on *dev_opp have no locking. What prevents races here?

Quoted 31 lines
lib/opp.c new file +28 -0
+	return 0;
+}
+
 
...
 
+void opp_init_cpufreq_table(struct device *dev,
+			    struct cpufreq_frequency_table **table)
+{
+	struct device_opp *dev_opp;
+	struct opp *opp;
+	struct cpufreq_frequency_table *freq_table;
+	int i = 0;
+
+	dev_opp = find_device_opp(dev);
+	if (IS_ERR(dev_opp)) {
+		pr_warning("%s: unable to find device\n", __func__);
+		return;
+	}
+
+	freq_table = kzalloc(sizeof(struct cpufreq_frequency_table) *
+			     (dev_opp->enabled_opp_count + 1), GFP_ATOMIC);
+	if (!freq_table) {
+		pr_warning("%s: failed to allocate frequency table\n",
+			   __func__);
+		return;
+	}
+
+	list_for_each_entry(opp, &dev_opp->opp_list, node) {
+		if (opp->enabled) {
+			freq_table[i].index = i;

lib/opp.c line 440

Quoted 10 lines
lib/opp.c new file +10 -0
+			freq_table[i].frequency = opp->rate / 1000;
+			i++;
+		}
+	}
+
+	freq_table[i].index = i;
+	freq_table[i].frequency = CPUFREQ_TABLE_END;
+
+	*table = &freq_table[0];
+}

So we're playing with cpufreq internals here but there's no #ifdef CONFIG_CPUFREQ and there's no Kconfig dependency on cpufreq. That needs fixing I think, if only from a reduce-code-bloat perspective.

#61 lore raw

PATCH 1/1 opp: introduce library for device-specific OPPs

In reply to a message not in the archive

SOCs have a standard set of tuples consisting of frequency and voltage pairs that the device will support per voltage domain. These are called Operating Performance Points or OPPs. The actual definitions of Operating Performance Points varies over silicon within the same family of devices. For a specific domain, you can have a set of {frequency, voltage} pairs. As the kernel boots and more information is available, a set of these are activated based on the precise nature of device the kernel boots up on. It is interesting to remember that each IP which belongs to a voltage domain may define their own set of OPPs on top of this.

To implement an OPP, some sort of power management support is necessary hence this library enablement depends on CONFIG_PM, however this does not fit into the core power framework as it is an independent library. This is hence introduced under lib allowing all architectures to selectively enable the feature based on thier capabilities.

Contributions include:
Sanjeev Premi for the initial concept:

	http://patchwork.kernel.org/patch/50998/

Kevin Hilman for converting original design to device-based Kevin Hilman and Paul Walmsey for cleaning up many of the function abstractions, improvements and data structure handling Romit Dasgupta for using enums instead of opp pointers Thara Gopinath, Eduardo Valentin and Vishwanath BS for fixes and cleanups.
Linus Walleij for recommending this layer be made generic for usage in other architectures beyond OMAP and ARM.

Discussions and comments from:
http://marc.info/?l=linux-omap&m=126033945313269&w=2
http://marc.info/?l=linux-omap&m=125482970102327&w=2
http://marc.info/?t=125809247500002&r=1&w=2
http://marc.info/?l=linux-omap&m=126025973426007&w=2
http://marc.info/?t=128152609200064&r=1&w=2
incorporated.

Cc Benoit Cousson <[email protected]>
Cc Madhusudhan Chikkature Rajashekar <[email protected]>
Cc Phil Carmody <[email protected]>
Cc Roberto Granados Dorado <[email protected]>
Cc Santosh Shilimkar <[email protected]>
Cc Sergio Alberto Aguirre Rodriguez <[email protected]>
Cc Tero Kristo <[email protected]>
Cc Eduardo Valentin <[email protected]>
Cc Paul Walmsley <[email protected]>
Cc Romit Dasgupta <[email protected]>
Cc Sanjeev Premi <[email protected]>
Cc Thara Gopinath <[email protected]>
Cc Vishwanath BS <[email protected]>
Cc Linus Walleij <[email protected]>

---
minor notes:
a) Code rebased to linus's tree commit 03a7ab0
b) Looping in get-maintainter.pl list and lkml as per thread of discussion
http://marc.info/?t=128152609200064&r=1&w=2

 Documentation/power/00-INDEX |    2 +
 include/linux/opp.h          |  136 +++++++++++++
 kernel/power/Kconfig         |   14 ++
 lib/Makefile                 |    2 +
 lib/opp.c                    |  440 ++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 594 insertions(+), 0 deletions(-)
 create mode 100644 include/linux/opp.h
 create mode 100644 lib/opp.c
Documentation/power/00-INDEX +2 -0
index fb742c2..45e9d4a 100644
@@ -14,6 +14,8 @@ interface.txt
 	- Power management user interface in /sys/power
 notifiers.txt
 	- Registering suspend notifiers in device drivers
+opp.txt
+	- Operating Performance Point library
 pci.txt
 	- How the PCI Subsystem Does Power Management
 pm_qos_interface.txt
include/linux/opp.h new file +136 -0

136 lines not on this page · raw message

kernel/power/Kconfig +14 -0
index ca6066a..634eab6 100644
@@ -242,3 +242,17 @@ config PM_OPS
 	bool
 	depends on PM_SLEEP || PM_RUNTIME
 	default y
+
+config PM_OPP
+	bool "Enable Operating Performance Point(OPP) Layer library"
+	depends on PM
+	---help---
+	  SOCs have a standard set of tuples consisting of frequency and
+	  voltage pairs that the device will support per voltage domain. This
+	  is called Operating Performance Point or OPP. The actual definitions
+	  of OPP varies over silicon within the same family of devices.
+
+	  OPP layer organizes the data internally using device pointers
+	  representing individual voltage domains and provides SOC
+	  implementations a ready to use framework to manage OPPs.
+	  For more information, read <file:Documentation/power/opp.txt>
lib/Makefile +2 -0
index e6a3763..0114fcf 100644
@@ -88,6 +88,8 @@ obj-$(CONFIG_IOMMU_HELPER) += iommu-helper.o
 obj-$(CONFIG_FAULT_INJECTION) += fault-inject.o
 obj-$(CONFIG_CPU_NOTIFIER_ERROR_INJECT) += cpu-notifier-error-inject.o
 
+obj-$(CONFIG_PM_OPP) += opp.o
+
 lib-$(CONFIG_GENERIC_BUG) += bug.o
 
 obj-$(CONFIG_HAVE_ARCH_TRACEHOOK) += syscall.o
lib/opp.c new file +440 -0

440 lines not on this page · raw message

Signature · 1 line
1.6.3.3