opp.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. Operating Performance Points (OPP) Library
  2. ==========================================
  3. (C) 2009-2010 Nishanth Menon <nm@ti.com>, Texas Instruments Incorporated
  4. Contents
  5. --------
  6. 1. Introduction
  7. 2. Initial OPP List Registration
  8. 3. OPP Search Functions
  9. 4. OPP Availability Control Functions
  10. 5. OPP Data Retrieval Functions
  11. 6. Data Structures
  12. 1. Introduction
  13. ===============
  14. 1.1 What is an Operating Performance Point (OPP)?
  15. Complex SoCs of today consists of a multiple sub-modules working in conjunction.
  16. In an operational system executing varied use cases, not all modules in the SoC
  17. need to function at their highest performing frequency all the time. To
  18. facilitate this, sub-modules in a SoC are grouped into domains, allowing some
  19. domains to run at lower voltage and frequency while other domains run at
  20. voltage/frequency pairs that are higher.
  21. The set of discrete tuples consisting of frequency and voltage pairs that
  22. the device will support per domain are called Operating Performance Points or
  23. OPPs.
  24. As an example:
  25. Let us consider an MPU device which supports the following:
  26. {300MHz at minimum voltage of 1V}, {800MHz at minimum voltage of 1.2V},
  27. {1GHz at minimum voltage of 1.3V}
  28. We can represent these as three OPPs as the following {Hz, uV} tuples:
  29. {300000000, 1000000}
  30. {800000000, 1200000}
  31. {1000000000, 1300000}
  32. 1.2 Operating Performance Points Library
  33. OPP library provides a set of helper functions to organize and query the OPP
  34. information. The library is located in drivers/base/power/opp.c and the header
  35. is located in include/linux/pm_opp.h. OPP library can be enabled by enabling
  36. CONFIG_PM_OPP from power management menuconfig menu. OPP library depends on
  37. CONFIG_PM as certain SoCs such as Texas Instrument's OMAP framework allows to
  38. optionally boot at a certain OPP without needing cpufreq.
  39. Typical usage of the OPP library is as follows:
  40. (users) -> registers a set of default OPPs -> (library)
  41. SoC framework -> modifies on required cases certain OPPs -> OPP layer
  42. -> queries to search/retrieve information ->
  43. OPP layer expects each domain to be represented by a unique device pointer. SoC
  44. framework registers a set of initial OPPs per device with the OPP layer. This
  45. list is expected to be an optimally small number typically around 5 per device.
  46. This initial list contains a set of OPPs that the framework expects to be safely
  47. enabled by default in the system.
  48. Note on OPP Availability:
  49. ------------------------
  50. As the system proceeds to operate, SoC framework may choose to make certain
  51. OPPs available or not available on each device based on various external
  52. factors. Example usage: Thermal management or other exceptional situations where
  53. SoC framework might choose to disable a higher frequency OPP to safely continue
  54. operations until that OPP could be re-enabled if possible.
  55. OPP library facilitates this concept in it's implementation. The following
  56. operational functions operate only on available opps:
  57. opp_find_freq_{ceil, floor}, dev_pm_opp_get_voltage, dev_pm_opp_get_freq, dev_pm_opp_get_opp_count
  58. dev_pm_opp_find_freq_exact is meant to be used to find the opp pointer which can then
  59. be used for dev_pm_opp_enable/disable functions to make an opp available as required.
  60. WARNING: Users of OPP library should refresh their availability count using
  61. get_opp_count if dev_pm_opp_enable/disable functions are invoked for a device, the
  62. exact mechanism to trigger these or the notification mechanism to other
  63. dependent subsystems such as cpufreq are left to the discretion of the SoC
  64. specific framework which uses the OPP library. Similar care needs to be taken
  65. care to refresh the cpufreq table in cases of these operations.
  66. 2. Initial OPP List Registration
  67. ================================
  68. The SoC implementation calls dev_pm_opp_add function iteratively to add OPPs per
  69. device. It is expected that the SoC framework will register the OPP entries
  70. optimally- typical numbers range to be less than 5. The list generated by
  71. registering the OPPs is maintained by OPP library throughout the device
  72. operation. The SoC framework can subsequently control the availability of the
  73. OPPs dynamically using the dev_pm_opp_enable / disable functions.
  74. dev_pm_opp_add - Add a new OPP for a specific domain represented by the device pointer.
  75. The OPP is defined using the frequency and voltage. Once added, the OPP
  76. is assumed to be available and control of it's availability can be done
  77. with the dev_pm_opp_enable/disable functions. OPP library internally stores
  78. and manages this information in the opp struct. This function may be
  79. used by SoC framework to define a optimal list as per the demands of
  80. SoC usage environment.
  81. WARNING: Do not use this function in interrupt context.
  82. Example:
  83. soc_pm_init()
  84. {
  85. /* Do things */
  86. r = dev_pm_opp_add(mpu_dev, 1000000, 900000);
  87. if (!r) {
  88. pr_err("%s: unable to register mpu opp(%d)\n", r);
  89. goto no_cpufreq;
  90. }
  91. /* Do cpufreq things */
  92. no_cpufreq:
  93. /* Do remaining things */
  94. }
  95. 3. OPP Search Functions
  96. =======================
  97. High level framework such as cpufreq operates on frequencies. To map the
  98. frequency back to the corresponding OPP, OPP library provides handy functions
  99. to search the OPP list that OPP library internally manages. These search
  100. functions return the matching pointer representing the opp if a match is
  101. found, else returns error. These errors are expected to be handled by standard
  102. error checks such as IS_ERR() and appropriate actions taken by the caller.
  103. Callers of these functions shall call dev_pm_opp_put() after they have used the
  104. OPP. Otherwise the memory for the OPP will never get freed and result in
  105. memleak.
  106. dev_pm_opp_find_freq_exact - Search for an OPP based on an *exact* frequency and
  107. availability. This function is especially useful to enable an OPP which
  108. is not available by default.
  109. Example: In a case when SoC framework detects a situation where a
  110. higher frequency could be made available, it can use this function to
  111. find the OPP prior to call the dev_pm_opp_enable to actually make it available.
  112. opp = dev_pm_opp_find_freq_exact(dev, 1000000000, false);
  113. dev_pm_opp_put(opp);
  114. /* dont operate on the pointer.. just do a sanity check.. */
  115. if (IS_ERR(opp)) {
  116. pr_err("frequency not disabled!\n");
  117. /* trigger appropriate actions.. */
  118. } else {
  119. dev_pm_opp_enable(dev,1000000000);
  120. }
  121. NOTE: This is the only search function that operates on OPPs which are
  122. not available.
  123. dev_pm_opp_find_freq_floor - Search for an available OPP which is *at most* the
  124. provided frequency. This function is useful while searching for a lesser
  125. match OR operating on OPP information in the order of decreasing
  126. frequency.
  127. Example: To find the highest opp for a device:
  128. freq = ULONG_MAX;
  129. opp = dev_pm_opp_find_freq_floor(dev, &freq);
  130. dev_pm_opp_put(opp);
  131. dev_pm_opp_find_freq_ceil - Search for an available OPP which is *at least* the
  132. provided frequency. This function is useful while searching for a
  133. higher match OR operating on OPP information in the order of increasing
  134. frequency.
  135. Example 1: To find the lowest opp for a device:
  136. freq = 0;
  137. opp = dev_pm_opp_find_freq_ceil(dev, &freq);
  138. dev_pm_opp_put(opp);
  139. Example 2: A simplified implementation of a SoC cpufreq_driver->target:
  140. soc_cpufreq_target(..)
  141. {
  142. /* Do stuff like policy checks etc. */
  143. /* Find the best frequency match for the req */
  144. opp = dev_pm_opp_find_freq_ceil(dev, &freq);
  145. dev_pm_opp_put(opp);
  146. if (!IS_ERR(opp))
  147. soc_switch_to_freq_voltage(freq);
  148. else
  149. /* do something when we can't satisfy the req */
  150. /* do other stuff */
  151. }
  152. 4. OPP Availability Control Functions
  153. =====================================
  154. A default OPP list registered with the OPP library may not cater to all possible
  155. situation. The OPP library provides a set of functions to modify the
  156. availability of a OPP within the OPP list. This allows SoC frameworks to have
  157. fine grained dynamic control of which sets of OPPs are operationally available.
  158. These functions are intended to *temporarily* remove an OPP in conditions such
  159. as thermal considerations (e.g. don't use OPPx until the temperature drops).
  160. WARNING: Do not use these functions in interrupt context.
  161. dev_pm_opp_enable - Make a OPP available for operation.
  162. Example: Lets say that 1GHz OPP is to be made available only if the
  163. SoC temperature is lower than a certain threshold. The SoC framework
  164. implementation might choose to do something as follows:
  165. if (cur_temp < temp_low_thresh) {
  166. /* Enable 1GHz if it was disabled */
  167. opp = dev_pm_opp_find_freq_exact(dev, 1000000000, false);
  168. dev_pm_opp_put(opp);
  169. /* just error check */
  170. if (!IS_ERR(opp))
  171. ret = dev_pm_opp_enable(dev, 1000000000);
  172. else
  173. goto try_something_else;
  174. }
  175. dev_pm_opp_disable - Make an OPP to be not available for operation
  176. Example: Lets say that 1GHz OPP is to be disabled if the temperature
  177. exceeds a threshold value. The SoC framework implementation might
  178. choose to do something as follows:
  179. if (cur_temp > temp_high_thresh) {
  180. /* Disable 1GHz if it was enabled */
  181. opp = dev_pm_opp_find_freq_exact(dev, 1000000000, true);
  182. dev_pm_opp_put(opp);
  183. /* just error check */
  184. if (!IS_ERR(opp))
  185. ret = dev_pm_opp_disable(dev, 1000000000);
  186. else
  187. goto try_something_else;
  188. }
  189. 5. OPP Data Retrieval Functions
  190. ===============================
  191. Since OPP library abstracts away the OPP information, a set of functions to pull
  192. information from the OPP structure is necessary. Once an OPP pointer is
  193. retrieved using the search functions, the following functions can be used by SoC
  194. framework to retrieve the information represented inside the OPP layer.
  195. dev_pm_opp_get_voltage - Retrieve the voltage represented by the opp pointer.
  196. Example: At a cpufreq transition to a different frequency, SoC
  197. framework requires to set the voltage represented by the OPP using
  198. the regulator framework to the Power Management chip providing the
  199. voltage.
  200. soc_switch_to_freq_voltage(freq)
  201. {
  202. /* do things */
  203. opp = dev_pm_opp_find_freq_ceil(dev, &freq);
  204. v = dev_pm_opp_get_voltage(opp);
  205. dev_pm_opp_put(opp);
  206. if (v)
  207. regulator_set_voltage(.., v);
  208. /* do other things */
  209. }
  210. dev_pm_opp_get_freq - Retrieve the freq represented by the opp pointer.
  211. Example: Lets say the SoC framework uses a couple of helper functions
  212. we could pass opp pointers instead of doing additional parameters to
  213. handle quiet a bit of data parameters.
  214. soc_cpufreq_target(..)
  215. {
  216. /* do things.. */
  217. max_freq = ULONG_MAX;
  218. max_opp = dev_pm_opp_find_freq_floor(dev,&max_freq);
  219. requested_opp = dev_pm_opp_find_freq_ceil(dev,&freq);
  220. if (!IS_ERR(max_opp) && !IS_ERR(requested_opp))
  221. r = soc_test_validity(max_opp, requested_opp);
  222. dev_pm_opp_put(max_opp);
  223. dev_pm_opp_put(requested_opp);
  224. /* do other things */
  225. }
  226. soc_test_validity(..)
  227. {
  228. if(dev_pm_opp_get_voltage(max_opp) < dev_pm_opp_get_voltage(requested_opp))
  229. return -EINVAL;
  230. if(dev_pm_opp_get_freq(max_opp) < dev_pm_opp_get_freq(requested_opp))
  231. return -EINVAL;
  232. /* do things.. */
  233. }
  234. dev_pm_opp_get_opp_count - Retrieve the number of available opps for a device
  235. Example: Lets say a co-processor in the SoC needs to know the available
  236. frequencies in a table, the main processor can notify as following:
  237. soc_notify_coproc_available_frequencies()
  238. {
  239. /* Do things */
  240. num_available = dev_pm_opp_get_opp_count(dev);
  241. speeds = kzalloc(sizeof(u32) * num_available, GFP_KERNEL);
  242. /* populate the table in increasing order */
  243. freq = 0;
  244. while (!IS_ERR(opp = dev_pm_opp_find_freq_ceil(dev, &freq))) {
  245. speeds[i] = freq;
  246. freq++;
  247. i++;
  248. dev_pm_opp_put(opp);
  249. }
  250. soc_notify_coproc(AVAILABLE_FREQs, speeds, num_available);
  251. /* Do other things */
  252. }
  253. 6. Data Structures
  254. ==================
  255. Typically an SoC contains multiple voltage domains which are variable. Each
  256. domain is represented by a device pointer. The relationship to OPP can be
  257. represented as follows:
  258. SoC
  259. |- device 1
  260. | |- opp 1 (availability, freq, voltage)
  261. | |- opp 2 ..
  262. ... ...
  263. | `- opp n ..
  264. |- device 2
  265. ...
  266. `- device m
  267. OPP library maintains a internal list that the SoC framework populates and
  268. accessed by various functions as described above. However, the structures
  269. representing the actual OPPs and domains are internal to the OPP library itself
  270. to allow for suitable abstraction reusable across systems.
  271. struct dev_pm_opp - The internal data structure of OPP library which is used to
  272. represent an OPP. In addition to the freq, voltage, availability
  273. information, it also contains internal book keeping information required
  274. for the OPP library to operate on. Pointer to this structure is
  275. provided back to the users such as SoC framework to be used as a
  276. identifier for OPP in the interactions with OPP layer.
  277. WARNING: The struct dev_pm_opp pointer should not be parsed or modified by the
  278. users. The defaults of for an instance is populated by dev_pm_opp_add, but the
  279. availability of the OPP can be modified by dev_pm_opp_enable/disable functions.
  280. struct device - This is used to identify a domain to the OPP layer. The
  281. nature of the device and it's implementation is left to the user of
  282. OPP library such as the SoC framework.
  283. Overall, in a simplistic view, the data structure operations is represented as
  284. following:
  285. Initialization / modification:
  286. +-----+ /- dev_pm_opp_enable
  287. dev_pm_opp_add --> | opp | <-------
  288. | +-----+ \- dev_pm_opp_disable
  289. \-------> domain_info(device)
  290. Search functions:
  291. /-- dev_pm_opp_find_freq_ceil ---\ +-----+
  292. domain_info<---- dev_pm_opp_find_freq_exact -----> | opp |
  293. \-- dev_pm_opp_find_freq_floor ---/ +-----+
  294. Retrieval functions:
  295. +-----+ /- dev_pm_opp_get_voltage
  296. | opp | <---
  297. +-----+ \- dev_pm_opp_get_freq
  298. domain_info <- dev_pm_opp_get_opp_count