blob: b3cf1def9deec0e6eee02e26d5e3cf1bd660b1e8 [file] [log] [blame]
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -03001==========================================
Nishanth Menon4b875812013-02-26 23:53:02 +00002Operating Performance Points (OPP) Library
3==========================================
Nishanth Menone1f60b22010-10-13 00:13:10 +02004
5(C) 2009-2010 Nishanth Menon <nm@ti.com>, Texas Instruments Incorporated
6
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -03007.. Contents
8
9 1. Introduction
10 2. Initial OPP List Registration
11 3. OPP Search Functions
12 4. OPP Availability Control Functions
13 5. OPP Data Retrieval Functions
14 6. Data Structures
Nishanth Menone1f60b22010-10-13 00:13:10 +020015
161. Introduction
17===============
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -030018
Nishanth Menon4b875812013-02-26 23:53:02 +0000191.1 What is an Operating Performance Point (OPP)?
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -030020-------------------------------------------------
Nishanth Menon4b875812013-02-26 23:53:02 +000021
Nishanth Menone1f60b22010-10-13 00:13:10 +020022Complex SoCs of today consists of a multiple sub-modules working in conjunction.
23In an operational system executing varied use cases, not all modules in the SoC
24need to function at their highest performing frequency all the time. To
25facilitate this, sub-modules in a SoC are grouped into domains, allowing some
Nishanth Menon4b875812013-02-26 23:53:02 +000026domains to run at lower voltage and frequency while other domains run at
27voltage/frequency pairs that are higher.
28
29The set of discrete tuples consisting of frequency and voltage pairs that
Nishanth Menone1f60b22010-10-13 00:13:10 +020030the device will support per domain are called Operating Performance Points or
31OPPs.
32
Nishanth Menon4b875812013-02-26 23:53:02 +000033As an example:
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -030034
Nishanth Menon4b875812013-02-26 23:53:02 +000035Let us consider an MPU device which supports the following:
36{300MHz at minimum voltage of 1V}, {800MHz at minimum voltage of 1.2V},
37{1GHz at minimum voltage of 1.3V}
38
39We can represent these as three OPPs as the following {Hz, uV} tuples:
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -030040
41- {300000000, 1000000}
42- {800000000, 1200000}
43- {1000000000, 1300000}
Nishanth Menon4b875812013-02-26 23:53:02 +000044
451.2 Operating Performance Points Library
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -030046----------------------------------------
Nishanth Menon4b875812013-02-26 23:53:02 +000047
Nishanth Menone1f60b22010-10-13 00:13:10 +020048OPP library provides a set of helper functions to organize and query the OPP
49information. The library is located in drivers/base/power/opp.c and the header
Nishanth Menone4db1c72013-09-19 16:03:52 -050050is located in include/linux/pm_opp.h. OPP library can be enabled by enabling
Nishanth Menone1f60b22010-10-13 00:13:10 +020051CONFIG_PM_OPP from power management menuconfig menu. OPP library depends on
52CONFIG_PM as certain SoCs such as Texas Instrument's OMAP framework allows to
53optionally boot at a certain OPP without needing cpufreq.
54
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -030055Typical usage of the OPP library is as follows::
56
57 (users) -> registers a set of default OPPs -> (library)
58 SoC framework -> modifies on required cases certain OPPs -> OPP layer
Nishanth Menone1f60b22010-10-13 00:13:10 +020059 -> queries to search/retrieve information ->
60
Nishanth Menone1f60b22010-10-13 00:13:10 +020061OPP layer expects each domain to be represented by a unique device pointer. SoC
62framework registers a set of initial OPPs per device with the OPP layer. This
63list is expected to be an optimally small number typically around 5 per device.
64This initial list contains a set of OPPs that the framework expects to be safely
65enabled by default in the system.
66
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -030067Note on OPP Availability
68^^^^^^^^^^^^^^^^^^^^^^^^
69
Nishanth Menone1f60b22010-10-13 00:13:10 +020070As the system proceeds to operate, SoC framework may choose to make certain
71OPPs available or not available on each device based on various external
72factors. Example usage: Thermal management or other exceptional situations where
73SoC framework might choose to disable a higher frequency OPP to safely continue
74operations until that OPP could be re-enabled if possible.
75
76OPP library facilitates this concept in it's implementation. The following
77operational functions operate only on available opps:
Nishanth Menon5d4879c2013-09-19 16:03:50 -050078opp_find_freq_{ceil, floor}, dev_pm_opp_get_voltage, dev_pm_opp_get_freq, dev_pm_opp_get_opp_count
Nishanth Menone1f60b22010-10-13 00:13:10 +020079
Nishanth Menon5d4879c2013-09-19 16:03:50 -050080dev_pm_opp_find_freq_exact is meant to be used to find the opp pointer which can then
81be used for dev_pm_opp_enable/disable functions to make an opp available as required.
Nishanth Menone1f60b22010-10-13 00:13:10 +020082
83WARNING: Users of OPP library should refresh their availability count using
Nishanth Menon5d4879c2013-09-19 16:03:50 -050084get_opp_count if dev_pm_opp_enable/disable functions are invoked for a device, the
Nishanth Menone1f60b22010-10-13 00:13:10 +020085exact mechanism to trigger these or the notification mechanism to other
86dependent subsystems such as cpufreq are left to the discretion of the SoC
87specific framework which uses the OPP library. Similar care needs to be taken
88care to refresh the cpufreq table in cases of these operations.
89
Nishanth Menone1f60b22010-10-13 00:13:10 +0200902. Initial OPP List Registration
91================================
Nishanth Menon5d4879c2013-09-19 16:03:50 -050092The SoC implementation calls dev_pm_opp_add function iteratively to add OPPs per
Nishanth Menone1f60b22010-10-13 00:13:10 +020093device. It is expected that the SoC framework will register the OPP entries
94optimally- typical numbers range to be less than 5. The list generated by
95registering the OPPs is maintained by OPP library throughout the device
96operation. The SoC framework can subsequently control the availability of the
Nishanth Menon5d4879c2013-09-19 16:03:50 -050097OPPs dynamically using the dev_pm_opp_enable / disable functions.
Nishanth Menone1f60b22010-10-13 00:13:10 +020098
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -030099dev_pm_opp_add
100 Add a new OPP for a specific domain represented by the device pointer.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200101 The OPP is defined using the frequency and voltage. Once added, the OPP
102 is assumed to be available and control of it's availability can be done
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500103 with the dev_pm_opp_enable/disable functions. OPP library internally stores
Nishanth Menone1f60b22010-10-13 00:13:10 +0200104 and manages this information in the opp struct. This function may be
105 used by SoC framework to define a optimal list as per the demands of
106 SoC usage environment.
107
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300108 WARNING:
109 Do not use this function in interrupt context.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200110
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300111 Example::
112
Nishanth Menone1f60b22010-10-13 00:13:10 +0200113 soc_pm_init()
114 {
115 /* Do things */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500116 r = dev_pm_opp_add(mpu_dev, 1000000, 900000);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200117 if (!r) {
118 pr_err("%s: unable to register mpu opp(%d)\n", r);
119 goto no_cpufreq;
120 }
121 /* Do cpufreq things */
122 no_cpufreq:
123 /* Do remaining things */
124 }
125
1263. OPP Search Functions
127=======================
128High level framework such as cpufreq operates on frequencies. To map the
129frequency back to the corresponding OPP, OPP library provides handy functions
130to search the OPP list that OPP library internally manages. These search
131functions return the matching pointer representing the opp if a match is
132found, else returns error. These errors are expected to be handled by standard
133error checks such as IS_ERR() and appropriate actions taken by the caller.
134
Viresh Kumar6185dea2017-01-23 10:11:52 +0530135Callers of these functions shall call dev_pm_opp_put() after they have used the
136OPP. Otherwise the memory for the OPP will never get freed and result in
137memleak.
138
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300139dev_pm_opp_find_freq_exact
140 Search for an OPP based on an *exact* frequency and
Nishanth Menone1f60b22010-10-13 00:13:10 +0200141 availability. This function is especially useful to enable an OPP which
142 is not available by default.
143 Example: In a case when SoC framework detects a situation where a
144 higher frequency could be made available, it can use this function to
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300145 find the OPP prior to call the dev_pm_opp_enable to actually make
146 it available::
147
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500148 opp = dev_pm_opp_find_freq_exact(dev, 1000000000, false);
Viresh Kumar6185dea2017-01-23 10:11:52 +0530149 dev_pm_opp_put(opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200150 /* dont operate on the pointer.. just do a sanity check.. */
151 if (IS_ERR(opp)) {
152 pr_err("frequency not disabled!\n");
153 /* trigger appropriate actions.. */
154 } else {
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500155 dev_pm_opp_enable(dev,1000000000);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200156 }
157
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300158 NOTE:
159 This is the only search function that operates on OPPs which are
160 not available.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200161
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300162dev_pm_opp_find_freq_floor
163 Search for an available OPP which is *at most* the
Nishanth Menone1f60b22010-10-13 00:13:10 +0200164 provided frequency. This function is useful while searching for a lesser
165 match OR operating on OPP information in the order of decreasing
166 frequency.
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300167 Example: To find the highest opp for a device::
168
Nishanth Menone1f60b22010-10-13 00:13:10 +0200169 freq = ULONG_MAX;
Viresh Kumar6185dea2017-01-23 10:11:52 +0530170 opp = dev_pm_opp_find_freq_floor(dev, &freq);
171 dev_pm_opp_put(opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200172
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300173dev_pm_opp_find_freq_ceil
174 Search for an available OPP which is *at least* the
Nishanth Menone1f60b22010-10-13 00:13:10 +0200175 provided frequency. This function is useful while searching for a
176 higher match OR operating on OPP information in the order of increasing
177 frequency.
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300178 Example 1: To find the lowest opp for a device::
179
Nishanth Menone1f60b22010-10-13 00:13:10 +0200180 freq = 0;
Viresh Kumar6185dea2017-01-23 10:11:52 +0530181 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
182 dev_pm_opp_put(opp);
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300183
184 Example 2: A simplified implementation of a SoC cpufreq_driver->target::
185
Nishanth Menone1f60b22010-10-13 00:13:10 +0200186 soc_cpufreq_target(..)
187 {
188 /* Do stuff like policy checks etc. */
189 /* Find the best frequency match for the req */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500190 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
Viresh Kumar6185dea2017-01-23 10:11:52 +0530191 dev_pm_opp_put(opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200192 if (!IS_ERR(opp))
193 soc_switch_to_freq_voltage(freq);
194 else
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300195 /* do something when we can't satisfy the req */
Nishanth Menone1f60b22010-10-13 00:13:10 +0200196 /* do other stuff */
197 }
198
1994. OPP Availability Control Functions
200=====================================
201A default OPP list registered with the OPP library may not cater to all possible
202situation. The OPP library provides a set of functions to modify the
203availability of a OPP within the OPP list. This allows SoC frameworks to have
204fine grained dynamic control of which sets of OPPs are operationally available.
205These functions are intended to *temporarily* remove an OPP in conditions such
206as thermal considerations (e.g. don't use OPPx until the temperature drops).
207
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300208WARNING:
209 Do not use these functions in interrupt context.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200210
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300211dev_pm_opp_enable
212 Make a OPP available for operation.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200213 Example: Lets say that 1GHz OPP is to be made available only if the
214 SoC temperature is lower than a certain threshold. The SoC framework
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300215 implementation might choose to do something as follows::
216
Nishanth Menone1f60b22010-10-13 00:13:10 +0200217 if (cur_temp < temp_low_thresh) {
218 /* Enable 1GHz if it was disabled */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500219 opp = dev_pm_opp_find_freq_exact(dev, 1000000000, false);
Viresh Kumar6185dea2017-01-23 10:11:52 +0530220 dev_pm_opp_put(opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200221 /* just error check */
222 if (!IS_ERR(opp))
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500223 ret = dev_pm_opp_enable(dev, 1000000000);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200224 else
225 goto try_something_else;
226 }
227
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300228dev_pm_opp_disable
229 Make an OPP to be not available for operation
Nishanth Menone1f60b22010-10-13 00:13:10 +0200230 Example: Lets say that 1GHz OPP is to be disabled if the temperature
231 exceeds a threshold value. The SoC framework implementation might
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300232 choose to do something as follows::
233
Nishanth Menone1f60b22010-10-13 00:13:10 +0200234 if (cur_temp > temp_high_thresh) {
235 /* Disable 1GHz if it was enabled */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500236 opp = dev_pm_opp_find_freq_exact(dev, 1000000000, true);
Viresh Kumar6185dea2017-01-23 10:11:52 +0530237 dev_pm_opp_put(opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200238 /* just error check */
239 if (!IS_ERR(opp))
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500240 ret = dev_pm_opp_disable(dev, 1000000000);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200241 else
242 goto try_something_else;
243 }
244
2455. OPP Data Retrieval Functions
246===============================
247Since OPP library abstracts away the OPP information, a set of functions to pull
248information from the OPP structure is necessary. Once an OPP pointer is
249retrieved using the search functions, the following functions can be used by SoC
250framework to retrieve the information represented inside the OPP layer.
251
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300252dev_pm_opp_get_voltage
253 Retrieve the voltage represented by the opp pointer.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200254 Example: At a cpufreq transition to a different frequency, SoC
255 framework requires to set the voltage represented by the OPP using
256 the regulator framework to the Power Management chip providing the
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300257 voltage::
258
Nishanth Menone1f60b22010-10-13 00:13:10 +0200259 soc_switch_to_freq_voltage(freq)
260 {
261 /* do things */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500262 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
263 v = dev_pm_opp_get_voltage(opp);
Viresh Kumar6185dea2017-01-23 10:11:52 +0530264 dev_pm_opp_put(opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200265 if (v)
266 regulator_set_voltage(.., v);
267 /* do other things */
268 }
269
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300270dev_pm_opp_get_freq
271 Retrieve the freq represented by the opp pointer.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200272 Example: Lets say the SoC framework uses a couple of helper functions
273 we could pass opp pointers instead of doing additional parameters to
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300274 handle quiet a bit of data parameters::
275
Nishanth Menone1f60b22010-10-13 00:13:10 +0200276 soc_cpufreq_target(..)
277 {
278 /* do things.. */
279 max_freq = ULONG_MAX;
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500280 max_opp = dev_pm_opp_find_freq_floor(dev,&max_freq);
281 requested_opp = dev_pm_opp_find_freq_ceil(dev,&freq);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200282 if (!IS_ERR(max_opp) && !IS_ERR(requested_opp))
283 r = soc_test_validity(max_opp, requested_opp);
Viresh Kumar6185dea2017-01-23 10:11:52 +0530284 dev_pm_opp_put(max_opp);
285 dev_pm_opp_put(requested_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200286 /* do other things */
287 }
288 soc_test_validity(..)
289 {
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500290 if(dev_pm_opp_get_voltage(max_opp) < dev_pm_opp_get_voltage(requested_opp))
Nishanth Menone1f60b22010-10-13 00:13:10 +0200291 return -EINVAL;
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500292 if(dev_pm_opp_get_freq(max_opp) < dev_pm_opp_get_freq(requested_opp))
Nishanth Menone1f60b22010-10-13 00:13:10 +0200293 return -EINVAL;
294 /* do things.. */
295 }
296
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300297dev_pm_opp_get_opp_count
298 Retrieve the number of available opps for a device
Nishanth Menone1f60b22010-10-13 00:13:10 +0200299 Example: Lets say a co-processor in the SoC needs to know the available
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300300 frequencies in a table, the main processor can notify as following::
301
Nishanth Menone1f60b22010-10-13 00:13:10 +0200302 soc_notify_coproc_available_frequencies()
303 {
304 /* Do things */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500305 num_available = dev_pm_opp_get_opp_count(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200306 speeds = kzalloc(sizeof(u32) * num_available, GFP_KERNEL);
307 /* populate the table in increasing order */
308 freq = 0;
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500309 while (!IS_ERR(opp = dev_pm_opp_find_freq_ceil(dev, &freq))) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200310 speeds[i] = freq;
311 freq++;
312 i++;
Viresh Kumar6185dea2017-01-23 10:11:52 +0530313 dev_pm_opp_put(opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200314 }
Nishanth Menone1f60b22010-10-13 00:13:10 +0200315
316 soc_notify_coproc(AVAILABLE_FREQs, speeds, num_available);
317 /* Do other things */
318 }
319
Nishanth Menona0dd7b72014-05-05 08:33:50 -05003206. Data Structures
Nishanth Menone1f60b22010-10-13 00:13:10 +0200321==================
322Typically an SoC contains multiple voltage domains which are variable. Each
323domain is represented by a device pointer. The relationship to OPP can be
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300324represented as follows::
325
326 SoC
327 |- device 1
328 | |- opp 1 (availability, freq, voltage)
329 | |- opp 2 ..
330 ... ...
331 | `- opp n ..
332 |- device 2
333 ...
334 `- device m
Nishanth Menone1f60b22010-10-13 00:13:10 +0200335
336OPP library maintains a internal list that the SoC framework populates and
337accessed by various functions as described above. However, the structures
338representing the actual OPPs and domains are internal to the OPP library itself
339to allow for suitable abstraction reusable across systems.
340
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300341struct dev_pm_opp
342 The internal data structure of OPP library which is used to
Nishanth Menone1f60b22010-10-13 00:13:10 +0200343 represent an OPP. In addition to the freq, voltage, availability
344 information, it also contains internal book keeping information required
345 for the OPP library to operate on. Pointer to this structure is
346 provided back to the users such as SoC framework to be used as a
347 identifier for OPP in the interactions with OPP layer.
348
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300349 WARNING:
350 The struct dev_pm_opp pointer should not be parsed or modified by the
351 users. The defaults of for an instance is populated by
352 dev_pm_opp_add, but the availability of the OPP can be modified
353 by dev_pm_opp_enable/disable functions.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200354
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300355struct device
356 This is used to identify a domain to the OPP layer. The
Nishanth Menone1f60b22010-10-13 00:13:10 +0200357 nature of the device and it's implementation is left to the user of
358 OPP library such as the SoC framework.
359
360Overall, in a simplistic view, the data structure operations is represented as
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300361following::
Nishanth Menone1f60b22010-10-13 00:13:10 +0200362
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300363 Initialization / modification:
364 +-----+ /- dev_pm_opp_enable
365 dev_pm_opp_add --> | opp | <-------
366 | +-----+ \- dev_pm_opp_disable
367 \-------> domain_info(device)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200368
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300369 Search functions:
370 /-- dev_pm_opp_find_freq_ceil ---\ +-----+
371 domain_info<---- dev_pm_opp_find_freq_exact -----> | opp |
372 \-- dev_pm_opp_find_freq_floor ---/ +-----+
Nishanth Menone1f60b22010-10-13 00:13:10 +0200373
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300374 Retrieval functions:
375 +-----+ /- dev_pm_opp_get_voltage
376 | opp | <---
377 +-----+ \- dev_pm_opp_get_freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200378
Mauro Carvalho Chehab151f4e22019-06-13 07:10:36 -0300379 domain_info <- dev_pm_opp_get_opp_count