opp.txt 14 KB

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