opp.txt 15 KB

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