cpu-drivers.txt 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 or setpolicy?
  16. 1.5 target
  17. 1.6 setpolicy
  18. 2. Frequency Table Helpers
  19. 1. What To Do?
  20. ==============
  21. So, you just got a brand-new CPU / chipset with datasheets and want to
  22. add cpufreq support for this CPU / chipset? Great. Here are some hints
  23. on what is necessary:
  24. 1.1 Initialization
  25. ------------------
  26. First of all, in an __initcall level 7 (module_init()) or later
  27. function check whether this kernel runs on the right CPU and the right
  28. chipset. If so, register a struct cpufreq_driver with the CPUfreq core
  29. using cpufreq_register_driver()
  30. What shall this struct cpufreq_driver contain?
  31. cpufreq_driver.name - The name of this driver.
  32. cpufreq_driver.owner - THIS_MODULE;
  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 - See below on the differences.
  38. And optionally
  39. cpufreq_driver.exit - A pointer to a per-CPU cleanup function.
  40. cpufreq_driver.resume - A pointer to a per-CPU resume function
  41. which is called with interrupts disabled
  42. and _before_ the pre-suspend frequency
  43. and/or policy is restored by a call to
  44. ->target or ->setpolicy.
  45. cpufreq_driver.attr - A pointer to a NULL-terminated list of
  46. "struct freq_attr" which allow to
  47. export values to sysfs.
  48. 1.2 Per-CPU Initialization
  49. --------------------------
  50. Whenever a new CPU is registered with the device model, or after the
  51. cpufreq driver registers itself, the per-CPU initialization function
  52. cpufreq_driver.init is called. It takes a struct cpufreq_policy
  53. *policy as argument. What to do now?
  54. If necessary, activate the CPUfreq support on your CPU.
  55. Then, the driver must fill in the following values:
  56. policy->cpuinfo.min_freq _and_
  57. policy->cpuinfo.max_freq - the minimum and maximum frequency
  58. (in kHz) which is supported by
  59. this CPU
  60. policy->cpuinfo.transition_latency the time it takes on this CPU to
  61. switch between two frequencies in
  62. nanoseconds (if appropriate, else
  63. specify CPUFREQ_ETERNAL)
  64. policy->cur The current operating frequency of
  65. this CPU (if appropriate)
  66. policy->min,
  67. policy->max,
  68. policy->policy and, if necessary,
  69. policy->governor must contain the "default policy" for
  70. this CPU. A few moments later,
  71. cpufreq_driver.verify and either
  72. cpufreq_driver.setpolicy or
  73. cpufreq_driver.target is called with
  74. these values.
  75. For setting some of these values, the frequency table helpers might be
  76. helpful. See the section 2 for more information on them.
  77. SMP systems normally have same clock source for a group of cpus. For these the
  78. .init() would be called only once for the first online cpu. Here the .init()
  79. routine must initialize policy->cpus with mask of all possible cpus (Online +
  80. Offline) that share the clock. Then the core would copy this mask onto
  81. policy->related_cpus and will reset policy->cpus to carry only online cpus.
  82. 1.3 verify
  83. ------------
  84. When the user decides a new policy (consisting of
  85. "policy,governor,min,max") shall be set, this policy must be validated
  86. so that incompatible values can be corrected. For verifying these
  87. values, a frequency table helper and/or the
  88. cpufreq_verify_within_limits(struct cpufreq_policy *policy, unsigned
  89. int min_freq, unsigned int max_freq) function might be helpful. See
  90. section 2 for details on frequency table helpers.
  91. You need to make sure that at least one valid frequency (or operating
  92. range) is within policy->min and policy->max. If necessary, increase
  93. policy->max first, and only if this is no solution, decrease policy->min.
  94. 1.4 target or setpolicy?
  95. ----------------------------
  96. Most cpufreq drivers or even most cpu frequency scaling algorithms
  97. only allow the CPU to be set to one frequency. For these, you use the
  98. ->target call.
  99. Some cpufreq-capable processors switch the frequency between certain
  100. limits on their own. These shall use the ->setpolicy call
  101. 1.4. target
  102. -------------
  103. The target call has three arguments: struct cpufreq_policy *policy,
  104. unsigned int target_frequency, unsigned int relation.
  105. The CPUfreq driver must set the new frequency when called here. The
  106. actual frequency must be determined using the following rules:
  107. - keep close to "target_freq"
  108. - policy->min <= new_freq <= policy->max (THIS MUST BE VALID!!!)
  109. - if relation==CPUFREQ_REL_L, try to select a new_freq higher than or equal
  110. target_freq. ("L for lowest, but no lower than")
  111. - if relation==CPUFREQ_REL_H, try to select a new_freq lower than or equal
  112. target_freq. ("H for highest, but no higher than")
  113. Here again the frequency table helper might assist you - see section 2
  114. for details.
  115. 1.5 setpolicy
  116. ---------------
  117. The setpolicy call only takes a struct cpufreq_policy *policy as
  118. argument. You need to set the lower limit of the in-processor or
  119. in-chipset dynamic frequency switching to policy->min, the upper limit
  120. to policy->max, and -if supported- select a performance-oriented
  121. setting when policy->policy is CPUFREQ_POLICY_PERFORMANCE, and a
  122. powersaving-oriented setting when CPUFREQ_POLICY_POWERSAVE. Also check
  123. the reference implementation in drivers/cpufreq/longrun.c
  124. 2. Frequency Table Helpers
  125. ==========================
  126. As most cpufreq processors only allow for being set to a few specific
  127. frequencies, a "frequency table" with some functions might assist in
  128. some work of the processor driver. Such a "frequency table" consists
  129. of an array of struct cpufreq_freq_table entries, with any value in
  130. "index" you want to use, and the corresponding frequency in
  131. "frequency". At the end of the table, you need to add a
  132. cpufreq_freq_table entry with frequency set to CPUFREQ_TABLE_END. And
  133. if you want to skip one entry in the table, set the frequency to
  134. CPUFREQ_ENTRY_INVALID. The entries don't need to be in ascending
  135. order.
  136. By calling cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
  137. struct cpufreq_frequency_table *table);
  138. the cpuinfo.min_freq and cpuinfo.max_freq values are detected, and
  139. policy->min and policy->max are set to the same values. This is
  140. helpful for the per-CPU initialization stage.
  141. int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
  142. struct cpufreq_frequency_table *table);
  143. assures that at least one valid frequency is within policy->min and
  144. policy->max, and all other criteria are met. This is helpful for the
  145. ->verify call.
  146. int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
  147. struct cpufreq_frequency_table *table,
  148. unsigned int target_freq,
  149. unsigned int relation,
  150. unsigned int *index);
  151. is the corresponding frequency table helper for the ->target
  152. stage. Just pass the values to this function, and the unsigned int
  153. index returns the number of the frequency table entry which contains
  154. the frequency the CPU shall be set to. PLEASE NOTE: This is not the
  155. "index" which is in this cpufreq_table_entry.index, but instead
  156. cpufreq_table[index]. So, the new frequency is
  157. cpufreq_table[index].frequency, and the value you stored into the
  158. frequency table "index" field is
  159. cpufreq_table[index].index.