opp.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. WARNING on OPP List locking mechanism:
  67. -------------------------------------------------
  68. OPP library uses RCU for exclusivity. RCU allows the query functions to operate
  69. in multiple contexts and this synchronization mechanism is optimal for a read
  70. intensive operations on data structure as the OPP library caters to.
  71. To ensure that the data retrieved are sane, the users such as SoC framework
  72. should ensure that the section of code operating on OPP queries are locked
  73. using RCU read locks. The opp_find_freq_{exact,ceil,floor},
  74. opp_get_{voltage, freq, opp_count} fall into this category.
  75. opp_{add,enable,disable} are updaters which use mutex and implement it's own
  76. RCU locking mechanisms. These functions should *NOT* be called under RCU locks
  77. and other contexts that prevent blocking functions in RCU or mutex operations
  78. from working.
  79. 2. Initial OPP List Registration
  80. ================================
  81. The SoC implementation calls dev_pm_opp_add function iteratively to add OPPs per
  82. device. It is expected that the SoC framework will register the OPP entries
  83. optimally- typical numbers range to be less than 5. The list generated by
  84. registering the OPPs is maintained by OPP library throughout the device
  85. operation. The SoC framework can subsequently control the availability of the
  86. OPPs dynamically using the dev_pm_opp_enable / disable functions.
  87. dev_pm_opp_add - Add a new OPP for a specific domain represented by the device pointer.
  88. The OPP is defined using the frequency and voltage. Once added, the OPP
  89. is assumed to be available and control of it's availability can be done
  90. with the dev_pm_opp_enable/disable functions. OPP library internally stores
  91. and manages this information in the opp struct. This function may be
  92. used by SoC framework to define a optimal list as per the demands of
  93. SoC usage environment.
  94. WARNING: Do not use this function in interrupt context.
  95. Example:
  96. soc_pm_init()
  97. {
  98. /* Do things */
  99. r = dev_pm_opp_add(mpu_dev, 1000000, 900000);
  100. if (!r) {
  101. pr_err("%s: unable to register mpu opp(%d)\n", r);
  102. goto no_cpufreq;
  103. }
  104. /* Do cpufreq things */
  105. no_cpufreq:
  106. /* Do remaining things */
  107. }
  108. 3. OPP Search Functions
  109. =======================
  110. High level framework such as cpufreq operates on frequencies. To map the
  111. frequency back to the corresponding OPP, OPP library provides handy functions
  112. to search the OPP list that OPP library internally manages. These search
  113. functions return the matching pointer representing the opp if a match is
  114. found, else returns error. These errors are expected to be handled by standard
  115. error checks such as IS_ERR() and appropriate actions taken by the caller.
  116. dev_pm_opp_find_freq_exact - Search for an OPP based on an *exact* frequency and
  117. availability. This function is especially useful to enable an OPP which
  118. is not available by default.
  119. Example: In a case when SoC framework detects a situation where a
  120. higher frequency could be made available, it can use this function to
  121. find the OPP prior to call the dev_pm_opp_enable to actually make it available.
  122. rcu_read_lock();
  123. opp = dev_pm_opp_find_freq_exact(dev, 1000000000, false);
  124. rcu_read_unlock();
  125. /* dont operate on the pointer.. just do a sanity check.. */
  126. if (IS_ERR(opp)) {
  127. pr_err("frequency not disabled!\n");
  128. /* trigger appropriate actions.. */
  129. } else {
  130. dev_pm_opp_enable(dev,1000000000);
  131. }
  132. NOTE: This is the only search function that operates on OPPs which are
  133. not available.
  134. dev_pm_opp_find_freq_floor - Search for an available OPP which is *at most* the
  135. provided frequency. This function is useful while searching for a lesser
  136. match OR operating on OPP information in the order of decreasing
  137. frequency.
  138. Example: To find the highest opp for a device:
  139. freq = ULONG_MAX;
  140. rcu_read_lock();
  141. dev_pm_opp_find_freq_floor(dev, &freq);
  142. rcu_read_unlock();
  143. dev_pm_opp_find_freq_ceil - Search for an available OPP which is *at least* the
  144. provided frequency. This function is useful while searching for a
  145. higher match OR operating on OPP information in the order of increasing
  146. frequency.
  147. Example 1: To find the lowest opp for a device:
  148. freq = 0;
  149. rcu_read_lock();
  150. dev_pm_opp_find_freq_ceil(dev, &freq);
  151. rcu_read_unlock();
  152. Example 2: A simplified implementation of a SoC cpufreq_driver->target:
  153. soc_cpufreq_target(..)
  154. {
  155. /* Do stuff like policy checks etc. */
  156. /* Find the best frequency match for the req */
  157. rcu_read_lock();
  158. opp = dev_pm_opp_find_freq_ceil(dev, &freq);
  159. rcu_read_unlock();
  160. if (!IS_ERR(opp))
  161. soc_switch_to_freq_voltage(freq);
  162. else
  163. /* do something when we can't satisfy the req */
  164. /* do other stuff */
  165. }
  166. 4. OPP Availability Control Functions
  167. =====================================
  168. A default OPP list registered with the OPP library may not cater to all possible
  169. situation. The OPP library provides a set of functions to modify the
  170. availability of a OPP within the OPP list. This allows SoC frameworks to have
  171. fine grained dynamic control of which sets of OPPs are operationally available.
  172. These functions are intended to *temporarily* remove an OPP in conditions such
  173. as thermal considerations (e.g. don't use OPPx until the temperature drops).
  174. WARNING: Do not use these functions in interrupt context.
  175. dev_pm_opp_enable - Make a OPP available for operation.
  176. Example: Lets say that 1GHz OPP is to be made available only if the
  177. SoC temperature is lower than a certain threshold. The SoC framework
  178. implementation might choose to do something as follows:
  179. if (cur_temp < temp_low_thresh) {
  180. /* Enable 1GHz if it was disabled */
  181. rcu_read_lock();
  182. opp = dev_pm_opp_find_freq_exact(dev, 1000000000, false);
  183. rcu_read_unlock();
  184. /* just error check */
  185. if (!IS_ERR(opp))
  186. ret = dev_pm_opp_enable(dev, 1000000000);
  187. else
  188. goto try_something_else;
  189. }
  190. dev_pm_opp_disable - Make an OPP to be not available for operation
  191. Example: Lets say that 1GHz OPP is to be disabled if the temperature
  192. exceeds a threshold value. The SoC framework implementation might
  193. choose to do something as follows:
  194. if (cur_temp > temp_high_thresh) {
  195. /* Disable 1GHz if it was enabled */
  196. rcu_read_lock();
  197. opp = dev_pm_opp_find_freq_exact(dev, 1000000000, true);
  198. rcu_read_unlock();
  199. /* just error check */
  200. if (!IS_ERR(opp))
  201. ret = dev_pm_opp_disable(dev, 1000000000);
  202. else
  203. goto try_something_else;
  204. }
  205. 5. OPP Data Retrieval Functions
  206. ===============================
  207. Since OPP library abstracts away the OPP information, a set of functions to pull
  208. information from the OPP structure is necessary. Once an OPP pointer is
  209. retrieved using the search functions, the following functions can be used by SoC
  210. framework to retrieve the information represented inside the OPP layer.
  211. dev_pm_opp_get_voltage - Retrieve the voltage represented by the opp pointer.
  212. Example: At a cpufreq transition to a different frequency, SoC
  213. framework requires to set the voltage represented by the OPP using
  214. the regulator framework to the Power Management chip providing the
  215. voltage.
  216. soc_switch_to_freq_voltage(freq)
  217. {
  218. /* do things */
  219. rcu_read_lock();
  220. opp = dev_pm_opp_find_freq_ceil(dev, &freq);
  221. v = dev_pm_opp_get_voltage(opp);
  222. rcu_read_unlock();
  223. if (v)
  224. regulator_set_voltage(.., v);
  225. /* do other things */
  226. }
  227. dev_pm_opp_get_freq - Retrieve the freq represented by the opp pointer.
  228. Example: Lets say the SoC framework uses a couple of helper functions
  229. we could pass opp pointers instead of doing additional parameters to
  230. handle quiet a bit of data parameters.
  231. soc_cpufreq_target(..)
  232. {
  233. /* do things.. */
  234. max_freq = ULONG_MAX;
  235. rcu_read_lock();
  236. max_opp = dev_pm_opp_find_freq_floor(dev,&max_freq);
  237. requested_opp = dev_pm_opp_find_freq_ceil(dev,&freq);
  238. if (!IS_ERR(max_opp) && !IS_ERR(requested_opp))
  239. r = soc_test_validity(max_opp, requested_opp);
  240. rcu_read_unlock();
  241. /* do other things */
  242. }
  243. soc_test_validity(..)
  244. {
  245. if(dev_pm_opp_get_voltage(max_opp) < dev_pm_opp_get_voltage(requested_opp))
  246. return -EINVAL;
  247. if(dev_pm_opp_get_freq(max_opp) < dev_pm_opp_get_freq(requested_opp))
  248. return -EINVAL;
  249. /* do things.. */
  250. }
  251. dev_pm_opp_get_opp_count - Retrieve the number of available opps for a device
  252. Example: Lets say a co-processor in the SoC needs to know the available
  253. frequencies in a table, the main processor can notify as following:
  254. soc_notify_coproc_available_frequencies()
  255. {
  256. /* Do things */
  257. rcu_read_lock();
  258. num_available = dev_pm_opp_get_opp_count(dev);
  259. speeds = kzalloc(sizeof(u32) * num_available, GFP_KERNEL);
  260. /* populate the table in increasing order */
  261. freq = 0;
  262. while (!IS_ERR(opp = dev_pm_opp_find_freq_ceil(dev, &freq))) {
  263. speeds[i] = freq;
  264. freq++;
  265. i++;
  266. }
  267. rcu_read_unlock();
  268. soc_notify_coproc(AVAILABLE_FREQs, speeds, num_available);
  269. /* Do other things */
  270. }
  271. 6. Data Structures
  272. ==================
  273. Typically an SoC contains multiple voltage domains which are variable. Each
  274. domain is represented by a device pointer. The relationship to OPP can be
  275. represented as follows:
  276. SoC
  277. |- device 1
  278. | |- opp 1 (availability, freq, voltage)
  279. | |- opp 2 ..
  280. ... ...
  281. | `- opp n ..
  282. |- device 2
  283. ...
  284. `- device m
  285. OPP library maintains a internal list that the SoC framework populates and
  286. accessed by various functions as described above. However, the structures
  287. representing the actual OPPs and domains are internal to the OPP library itself
  288. to allow for suitable abstraction reusable across systems.
  289. struct dev_pm_opp - The internal data structure of OPP library which is used to
  290. represent an OPP. In addition to the freq, voltage, availability
  291. information, it also contains internal book keeping information required
  292. for the OPP library to operate on. Pointer to this structure is
  293. provided back to the users such as SoC framework to be used as a
  294. identifier for OPP in the interactions with OPP layer.
  295. WARNING: The struct dev_pm_opp pointer should not be parsed or modified by the
  296. users. The defaults of for an instance is populated by dev_pm_opp_add, but the
  297. availability of the OPP can be modified by dev_pm_opp_enable/disable functions.
  298. struct device - This is used to identify a domain to the OPP layer. The
  299. nature of the device and it's implementation is left to the user of
  300. OPP library such as the SoC framework.
  301. Overall, in a simplistic view, the data structure operations is represented as
  302. following:
  303. Initialization / modification:
  304. +-----+ /- dev_pm_opp_enable
  305. dev_pm_opp_add --> | opp | <-------
  306. | +-----+ \- dev_pm_opp_disable
  307. \-------> domain_info(device)
  308. Search functions:
  309. /-- dev_pm_opp_find_freq_ceil ---\ +-----+
  310. domain_info<---- dev_pm_opp_find_freq_exact -----> | opp |
  311. \-- dev_pm_opp_find_freq_floor ---/ +-----+
  312. Retrieval functions:
  313. +-----+ /- dev_pm_opp_get_voltage
  314. | opp | <---
  315. +-----+ \- dev_pm_opp_get_freq
  316. domain_info <- dev_pm_opp_get_opp_count