cpu-drivers.txt 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. Clock scaling allows you to change the clock speed of the CPUs on the
  7. fly. This is a nice method to save battery power, because the lower
  8. the clock speed, the less power the CPU consumes.
  9. Contents:
  10. ---------
  11. 1. What To Do?
  12. 1.1 Initialization
  13. 1.2 Per-CPU Initialization
  14. 1.3 verify
  15. 1.4 target/target_index or setpolicy?
  16. 1.5 target/target_index
  17. 1.6 setpolicy
  18. 1.7 get_intermediate and target_intermediate
  19. 2. Frequency Table Helpers
  20. 1. What To Do?
  21. ==============
  22. So, you just got a brand-new CPU / chipset with datasheets and want to
  23. add cpufreq support for this CPU / chipset? Great. Here are some hints
  24. on what is necessary:
  25. 1.1 Initialization
  26. ------------------
  27. First of all, in an __initcall level 7 (module_init()) or later
  28. function check whether this kernel runs on the right CPU and the right
  29. chipset. If so, register a struct cpufreq_driver with the CPUfreq core
  30. using cpufreq_register_driver()
  31. What shall this struct cpufreq_driver contain?
  32. cpufreq_driver.name - The name of this driver.
  33. cpufreq_driver.init - A pointer to the per-CPU initialization
  34. function.
  35. cpufreq_driver.verify - A pointer to a "verification" function.
  36. cpufreq_driver.setpolicy _or_
  37. cpufreq_driver.target/
  38. target_index - See below on the differences.
  39. And optionally
  40. cpufreq_driver.exit - A pointer to a per-CPU cleanup
  41. function called during CPU_POST_DEAD
  42. phase of cpu hotplug process.
  43. cpufreq_driver.stop_cpu - A pointer to a per-CPU stop function
  44. called during CPU_DOWN_PREPARE phase of
  45. cpu hotplug process.
  46. cpufreq_driver.resume - A pointer to a per-CPU resume function
  47. which is called with interrupts disabled
  48. and _before_ the pre-suspend frequency
  49. and/or policy is restored by a call to
  50. ->target/target_index or ->setpolicy.
  51. cpufreq_driver.attr - A pointer to a NULL-terminated list of
  52. "struct freq_attr" which allow to
  53. export values to sysfs.
  54. cpufreq_driver.get_intermediate
  55. and target_intermediate Used to switch to stable frequency while
  56. changing CPU frequency.
  57. 1.2 Per-CPU Initialization
  58. --------------------------
  59. Whenever a new CPU is registered with the device model, or after the
  60. cpufreq driver registers itself, the per-CPU initialization function
  61. cpufreq_driver.init is called. It takes a struct cpufreq_policy
  62. *policy as argument. What to do now?
  63. If necessary, activate the CPUfreq support on your CPU.
  64. Then, the driver must fill in the following values:
  65. policy->cpuinfo.min_freq _and_
  66. policy->cpuinfo.max_freq - the minimum and maximum frequency
  67. (in kHz) which is supported by
  68. this CPU
  69. policy->cpuinfo.transition_latency the time it takes on this CPU to
  70. switch between two frequencies in
  71. nanoseconds (if appropriate, else
  72. specify CPUFREQ_ETERNAL)
  73. policy->cur The current operating frequency of
  74. this CPU (if appropriate)
  75. policy->min,
  76. policy->max,
  77. policy->policy and, if necessary,
  78. policy->governor must contain the "default policy" for
  79. this CPU. A few moments later,
  80. cpufreq_driver.verify and either
  81. cpufreq_driver.setpolicy or
  82. cpufreq_driver.target/target_index is called
  83. with these values.
  84. For setting some of these values (cpuinfo.min[max]_freq, policy->min[max]), the
  85. frequency table helpers might be helpful. See the section 2 for more information
  86. on them.
  87. SMP systems normally have same clock source for a group of cpus. For these the
  88. .init() would be called only once for the first online cpu. Here the .init()
  89. routine must initialize policy->cpus with mask of all possible cpus (Online +
  90. Offline) that share the clock. Then the core would copy this mask onto
  91. policy->related_cpus and will reset policy->cpus to carry only online cpus.
  92. 1.3 verify
  93. ------------
  94. When the user decides a new policy (consisting of
  95. "policy,governor,min,max") shall be set, this policy must be validated
  96. so that incompatible values can be corrected. For verifying these
  97. values, a frequency table helper and/or the
  98. cpufreq_verify_within_limits(struct cpufreq_policy *policy, unsigned
  99. int min_freq, unsigned int max_freq) function might be helpful. See
  100. section 2 for details on frequency table helpers.
  101. You need to make sure that at least one valid frequency (or operating
  102. range) is within policy->min and policy->max. If necessary, increase
  103. policy->max first, and only if this is no solution, decrease policy->min.
  104. 1.4 target/target_index or setpolicy?
  105. ----------------------------
  106. Most cpufreq drivers or even most cpu frequency scaling algorithms
  107. only allow the CPU to be set to one frequency. For these, you use the
  108. ->target/target_index call.
  109. Some cpufreq-capable processors switch the frequency between certain
  110. limits on their own. These shall use the ->setpolicy call
  111. 1.5. target/target_index
  112. -------------
  113. The target_index call has two arguments: struct cpufreq_policy *policy,
  114. and unsigned int index (into the exposed frequency table).
  115. The CPUfreq driver must set the new frequency when called here. The
  116. actual frequency must be determined by freq_table[index].frequency.
  117. It should always restore to earlier frequency (i.e. policy->restore_freq) in
  118. case of errors, even if we switched to intermediate frequency earlier.
  119. Deprecated:
  120. ----------
  121. The target call has three arguments: struct cpufreq_policy *policy,
  122. unsigned int target_frequency, unsigned int relation.
  123. The CPUfreq driver must set the new frequency when called here. The
  124. actual frequency must be determined using the following rules:
  125. - keep close to "target_freq"
  126. - policy->min <= new_freq <= policy->max (THIS MUST BE VALID!!!)
  127. - if relation==CPUFREQ_REL_L, try to select a new_freq higher than or equal
  128. target_freq. ("L for lowest, but no lower than")
  129. - if relation==CPUFREQ_REL_H, try to select a new_freq lower than or equal
  130. target_freq. ("H for highest, but no higher than")
  131. Here again the frequency table helper might assist you - see section 2
  132. for details.
  133. 1.6 setpolicy
  134. ---------------
  135. The setpolicy call only takes a struct cpufreq_policy *policy as
  136. argument. You need to set the lower limit of the in-processor or
  137. in-chipset dynamic frequency switching to policy->min, the upper limit
  138. to policy->max, and -if supported- select a performance-oriented
  139. setting when policy->policy is CPUFREQ_POLICY_PERFORMANCE, and a
  140. powersaving-oriented setting when CPUFREQ_POLICY_POWERSAVE. Also check
  141. the reference implementation in drivers/cpufreq/longrun.c
  142. 1.7 get_intermediate and target_intermediate
  143. --------------------------------------------
  144. Only for drivers with target_index() and CPUFREQ_ASYNC_NOTIFICATION unset.
  145. get_intermediate should return a stable intermediate frequency platform wants to
  146. switch to, and target_intermediate() should set CPU to to that frequency, before
  147. jumping to the frequency corresponding to 'index'. Core will take care of
  148. sending notifications and driver doesn't have to handle them in
  149. target_intermediate() or target_index().
  150. Drivers can return '0' from get_intermediate() in case they don't wish to switch
  151. to intermediate frequency for some target frequency. In that case core will
  152. directly call ->target_index().
  153. NOTE: ->target_index() should restore to policy->restore_freq in case of
  154. failures as core would send notifications for that.
  155. 2. Frequency Table Helpers
  156. ==========================
  157. As most cpufreq processors only allow for being set to a few specific
  158. frequencies, a "frequency table" with some functions might assist in
  159. some work of the processor driver. Such a "frequency table" consists
  160. of an array of struct cpufreq_frequency_table entries, with any value in
  161. "driver_data" you want to use, and the corresponding frequency in
  162. "frequency". At the end of the table, you need to add a
  163. cpufreq_frequency_table entry with frequency set to CPUFREQ_TABLE_END. And
  164. if you want to skip one entry in the table, set the frequency to
  165. CPUFREQ_ENTRY_INVALID. The entries don't need to be in ascending
  166. order.
  167. By calling cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
  168. struct cpufreq_frequency_table *table);
  169. the cpuinfo.min_freq and cpuinfo.max_freq values are detected, and
  170. policy->min and policy->max are set to the same values. This is
  171. helpful for the per-CPU initialization stage.
  172. int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
  173. struct cpufreq_frequency_table *table);
  174. assures that at least one valid frequency is within policy->min and
  175. policy->max, and all other criteria are met. This is helpful for the
  176. ->verify call.
  177. int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
  178. struct cpufreq_frequency_table *table,
  179. unsigned int target_freq,
  180. unsigned int relation,
  181. unsigned int *index);
  182. is the corresponding frequency table helper for the ->target
  183. stage. Just pass the values to this function, and the unsigned int
  184. index returns the number of the frequency table entry which contains
  185. the frequency the CPU shall be set to.
  186. The following macros can be used as iterators over cpufreq_frequency_table:
  187. cpufreq_for_each_entry(pos, table) - iterates over all entries of frequency
  188. table.
  189. cpufreq-for_each_valid_entry(pos, table) - iterates over all entries,
  190. excluding CPUFREQ_ENTRY_INVALID frequencies.
  191. Use arguments "pos" - a cpufreq_frequency_table * as a loop cursor and
  192. "table" - the cpufreq_frequency_table * you want to iterate over.
  193. For example:
  194. struct cpufreq_frequency_table *pos, *driver_freq_table;
  195. cpufreq_for_each_entry(pos, driver_freq_table) {
  196. /* Do something with pos */
  197. pos->frequency = ...
  198. }