cpu-drivers.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. CPU frequency and voltage scaling code in the Linux(TM) kernel
  2. L i n u x C P U F r e q
  3. C P U D r i v e r s
  4. - information for developers -
  5. Dominik Brodowski <linux@brodo.de>
  6. Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  7. Viresh Kumar <viresh.kumar@linaro.org>
  8. Clock scaling allows you to change the clock speed of the CPUs on the
  9. fly. This is a nice method to save battery power, because the lower
  10. the clock speed, the less power the CPU consumes.
  11. Contents:
  12. ---------
  13. 1. What To Do?
  14. 1.1 Initialization
  15. 1.2 Per-CPU Initialization
  16. 1.3 verify
  17. 1.4 target/target_index or setpolicy?
  18. 1.5 target/target_index
  19. 1.6 setpolicy
  20. 1.7 get_intermediate and target_intermediate
  21. 2. Frequency Table Helpers
  22. 1. What To Do?
  23. ==============
  24. So, you just got a brand-new CPU / chipset with datasheets and want to
  25. add cpufreq support for this CPU / chipset? Great. Here are some hints
  26. on what is necessary:
  27. 1.1 Initialization
  28. ------------------
  29. First of all, in an __initcall level 7 (module_init()) or later
  30. function check whether this kernel runs on the right CPU and the right
  31. chipset. If so, register a struct cpufreq_driver with the CPUfreq core
  32. using cpufreq_register_driver()
  33. What shall this struct cpufreq_driver contain?
  34. .name - The name of this driver.
  35. .init - A pointer to the per-policy initialization function.
  36. .verify - A pointer to a "verification" function.
  37. .setpolicy _or_ .fast_switch _or_ .target _or_ .target_index - See
  38. below on the differences.
  39. And optionally
  40. .flags - Hints for the cpufreq core.
  41. .driver_data - cpufreq driver specific data.
  42. .resolve_freq - Returns the most appropriate frequency for a target
  43. frequency. Doesn't change the frequency though.
  44. .get_intermediate and target_intermediate - Used to switch to stable
  45. frequency while changing CPU frequency.
  46. .get - Returns current frequency of the CPU.
  47. .bios_limit - Returns HW/BIOS max frequency limitations for the CPU.
  48. .exit - A pointer to a per-policy cleanup function called during
  49. CPU_POST_DEAD phase of cpu hotplug process.
  50. .stop_cpu - A pointer to a per-policy stop function called during
  51. CPU_DOWN_PREPARE phase of cpu hotplug process.
  52. .suspend - A pointer to a per-policy suspend function which is called
  53. with interrupts disabled and _after_ the governor is stopped for the
  54. policy.
  55. .resume - A pointer to a per-policy resume function which is called
  56. with interrupts disabled and _before_ the governor is started again.
  57. .ready - A pointer to a per-policy ready function which is called after
  58. the policy is fully initialized.
  59. .attr - A pointer to a NULL-terminated list of "struct freq_attr" which
  60. allow to export values to sysfs.
  61. .boost_enabled - If set, boost frequencies are enabled.
  62. .set_boost - A pointer to a per-policy function to enable/disable boost
  63. frequencies.
  64. 1.2 Per-CPU Initialization
  65. --------------------------
  66. Whenever a new CPU is registered with the device model, or after the
  67. cpufreq driver registers itself, the per-policy initialization function
  68. cpufreq_driver.init is called if no cpufreq policy existed for the CPU.
  69. Note that the .init() and .exit() routines are called only once for the
  70. policy and not for each CPU managed by the policy. It takes a struct
  71. cpufreq_policy *policy as argument. What to do now?
  72. If necessary, activate the CPUfreq support on your CPU.
  73. Then, the driver must fill in the following values:
  74. policy->cpuinfo.min_freq _and_
  75. policy->cpuinfo.max_freq - the minimum and maximum frequency
  76. (in kHz) which is supported by
  77. this CPU
  78. policy->cpuinfo.transition_latency the time it takes on this CPU to
  79. switch between two frequencies in
  80. nanoseconds (if appropriate, else
  81. specify CPUFREQ_ETERNAL)
  82. policy->cur The current operating frequency of
  83. this CPU (if appropriate)
  84. policy->min,
  85. policy->max,
  86. policy->policy and, if necessary,
  87. policy->governor must contain the "default policy" for
  88. this CPU. A few moments later,
  89. cpufreq_driver.verify and either
  90. cpufreq_driver.setpolicy or
  91. cpufreq_driver.target/target_index is called
  92. with these values.
  93. policy->cpus Update this with the masks of the
  94. (online + offline) CPUs that do DVFS
  95. along with this CPU (i.e. that share
  96. clock/voltage rails with it).
  97. For setting some of these values (cpuinfo.min[max]_freq, policy->min[max]), the
  98. frequency table helpers might be helpful. See the section 2 for more information
  99. on them.
  100. 1.3 verify
  101. ----------
  102. When the user decides a new policy (consisting of
  103. "policy,governor,min,max") shall be set, this policy must be validated
  104. so that incompatible values can be corrected. For verifying these
  105. values cpufreq_verify_within_limits(struct cpufreq_policy *policy,
  106. unsigned int min_freq, unsigned int max_freq) function might be helpful.
  107. See section 2 for details on frequency table helpers.
  108. You need to make sure that at least one valid frequency (or operating
  109. range) is within policy->min and policy->max. If necessary, increase
  110. policy->max first, and only if this is no solution, decrease policy->min.
  111. 1.4 target or target_index or setpolicy or fast_switch?
  112. -------------------------------------------------------
  113. Most cpufreq drivers or even most cpu frequency scaling algorithms
  114. only allow the CPU frequency to be set to predefined fixed values. For
  115. these, you use the ->target(), ->target_index() or ->fast_switch()
  116. callbacks.
  117. Some cpufreq capable processors switch the frequency between certain
  118. limits on their own. These shall use the ->setpolicy() callback.
  119. 1.5. target/target_index
  120. ------------------------
  121. The target_index call has two arguments: struct cpufreq_policy *policy,
  122. and unsigned int index (into the exposed frequency table).
  123. The CPUfreq driver must set the new frequency when called here. The
  124. actual frequency must be determined by freq_table[index].frequency.
  125. It should always restore to earlier frequency (i.e. policy->restore_freq) in
  126. case of errors, even if we switched to intermediate frequency earlier.
  127. Deprecated:
  128. ----------
  129. The target call has three arguments: struct cpufreq_policy *policy,
  130. unsigned int target_frequency, unsigned int relation.
  131. The CPUfreq driver must set the new frequency when called here. The
  132. actual frequency must be determined using the following rules:
  133. - keep close to "target_freq"
  134. - policy->min <= new_freq <= policy->max (THIS MUST BE VALID!!!)
  135. - if relation==CPUFREQ_REL_L, try to select a new_freq higher than or equal
  136. target_freq. ("L for lowest, but no lower than")
  137. - if relation==CPUFREQ_REL_H, try to select a new_freq lower than or equal
  138. target_freq. ("H for highest, but no higher than")
  139. Here again the frequency table helper might assist you - see section 2
  140. for details.
  141. 1.6. fast_switch
  142. ----------------
  143. This function is used for frequency switching from scheduler's context.
  144. Not all drivers are expected to implement it, as sleeping from within
  145. this callback isn't allowed. This callback must be highly optimized to
  146. do switching as fast as possible.
  147. This function has two arguments: struct cpufreq_policy *policy and
  148. unsigned int target_frequency.
  149. 1.7 setpolicy
  150. -------------
  151. The setpolicy call only takes a struct cpufreq_policy *policy as
  152. argument. You need to set the lower limit of the in-processor or
  153. in-chipset dynamic frequency switching to policy->min, the upper limit
  154. to policy->max, and -if supported- select a performance-oriented
  155. setting when policy->policy is CPUFREQ_POLICY_PERFORMANCE, and a
  156. powersaving-oriented setting when CPUFREQ_POLICY_POWERSAVE. Also check
  157. the reference implementation in drivers/cpufreq/longrun.c
  158. 1.8 get_intermediate and target_intermediate
  159. --------------------------------------------
  160. Only for drivers with target_index() and CPUFREQ_ASYNC_NOTIFICATION unset.
  161. get_intermediate should return a stable intermediate frequency platform wants to
  162. switch to, and target_intermediate() should set CPU to that frequency, before
  163. jumping to the frequency corresponding to 'index'. Core will take care of
  164. sending notifications and driver doesn't have to handle them in
  165. target_intermediate() or target_index().
  166. Drivers can return '0' from get_intermediate() in case they don't wish to switch
  167. to intermediate frequency for some target frequency. In that case core will
  168. directly call ->target_index().
  169. NOTE: ->target_index() should restore to policy->restore_freq in case of
  170. failures as core would send notifications for that.
  171. 2. Frequency Table Helpers
  172. ==========================
  173. As most cpufreq processors only allow for being set to a few specific
  174. frequencies, a "frequency table" with some functions might assist in
  175. some work of the processor driver. Such a "frequency table" consists of
  176. an array of struct cpufreq_frequency_table entries, with driver specific
  177. values in "driver_data", the corresponding frequency in "frequency" and
  178. flags set. At the end of the table, you need to add a
  179. cpufreq_frequency_table entry with frequency set to CPUFREQ_TABLE_END.
  180. And if you want to skip one entry in the table, set the frequency to
  181. CPUFREQ_ENTRY_INVALID. The entries don't need to be in sorted in any
  182. particular order, but if they are cpufreq core will do DVFS a bit
  183. quickly for them as search for best match is faster.
  184. The cpufreq table is verified automatically by the core if the policy contains a
  185. valid pointer in its policy->freq_table field.
  186. cpufreq_frequency_table_verify() assures that at least one valid
  187. frequency is within policy->min and policy->max, and all other criteria
  188. are met. This is helpful for the ->verify call.
  189. cpufreq_frequency_table_target() is the corresponding frequency table
  190. helper for the ->target stage. Just pass the values to this function,
  191. and this function returns the of the frequency table entry which
  192. contains the frequency the CPU shall be set to.
  193. The following macros can be used as iterators over cpufreq_frequency_table:
  194. cpufreq_for_each_entry(pos, table) - iterates over all entries of frequency
  195. table.
  196. cpufreq_for_each_valid_entry(pos, table) - iterates over all entries,
  197. excluding CPUFREQ_ENTRY_INVALID frequencies.
  198. Use arguments "pos" - a cpufreq_frequency_table * as a loop cursor and
  199. "table" - the cpufreq_frequency_table * you want to iterate over.
  200. For example:
  201. struct cpufreq_frequency_table *pos, *driver_freq_table;
  202. cpufreq_for_each_entry(pos, driver_freq_table) {
  203. /* Do something with pos */
  204. pos->frequency = ...
  205. }
  206. If you need to work with the position of pos within driver_freq_table,
  207. do not subtract the pointers, as it is quite costly. Instead, use the
  208. macros cpufreq_for_each_entry_idx() and cpufreq_for_each_valid_entry_idx().