cpu-drivers.txt 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. 1.3 verify
  78. ------------
  79. When the user decides a new policy (consisting of
  80. "policy,governor,min,max") shall be set, this policy must be validated
  81. so that incompatible values can be corrected. For verifying these
  82. values, a frequency table helper and/or the
  83. cpufreq_verify_within_limits(struct cpufreq_policy *policy, unsigned
  84. int min_freq, unsigned int max_freq) function might be helpful. See
  85. section 2 for details on frequency table helpers.
  86. You need to make sure that at least one valid frequency (or operating
  87. range) is within policy->min and policy->max. If necessary, increase
  88. policy->max first, and only if this is no solution, decrease policy->min.
  89. 1.4 target or setpolicy?
  90. ----------------------------
  91. Most cpufreq drivers or even most cpu frequency scaling algorithms
  92. only allow the CPU to be set to one frequency. For these, you use the
  93. ->target call.
  94. Some cpufreq-capable processors switch the frequency between certain
  95. limits on their own. These shall use the ->setpolicy call
  96. 1.4. target
  97. -------------
  98. The target call has three arguments: struct cpufreq_policy *policy,
  99. unsigned int target_frequency, unsigned int relation.
  100. The CPUfreq driver must set the new frequency when called here. The
  101. actual frequency must be determined using the following rules:
  102. - keep close to "target_freq"
  103. - policy->min <= new_freq <= policy->max (THIS MUST BE VALID!!!)
  104. - if relation==CPUFREQ_REL_L, try to select a new_freq higher than or equal
  105. target_freq. ("L for lowest, but no lower than")
  106. - if relation==CPUFREQ_REL_H, try to select a new_freq lower than or equal
  107. target_freq. ("H for highest, but no higher than")
  108. Here again the frequency table helper might assist you - see section 2
  109. for details.
  110. 1.5 setpolicy
  111. ---------------
  112. The setpolicy call only takes a struct cpufreq_policy *policy as
  113. argument. You need to set the lower limit of the in-processor or
  114. in-chipset dynamic frequency switching to policy->min, the upper limit
  115. to policy->max, and -if supported- select a performance-oriented
  116. setting when policy->policy is CPUFREQ_POLICY_PERFORMANCE, and a
  117. powersaving-oriented setting when CPUFREQ_POLICY_POWERSAVE. Also check
  118. the reference implementation in drivers/cpufreq/longrun.c
  119. 2. Frequency Table Helpers
  120. ==========================
  121. As most cpufreq processors only allow for being set to a few specific
  122. frequencies, a "frequency table" with some functions might assist in
  123. some work of the processor driver. Such a "frequency table" consists
  124. of an array of struct cpufreq_freq_table entries, with any value in
  125. "index" you want to use, and the corresponding frequency in
  126. "frequency". At the end of the table, you need to add a
  127. cpufreq_freq_table entry with frequency set to CPUFREQ_TABLE_END. And
  128. if you want to skip one entry in the table, set the frequency to
  129. CPUFREQ_ENTRY_INVALID. The entries don't need to be in ascending
  130. order.
  131. By calling cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
  132. struct cpufreq_frequency_table *table);
  133. the cpuinfo.min_freq and cpuinfo.max_freq values are detected, and
  134. policy->min and policy->max are set to the same values. This is
  135. helpful for the per-CPU initialization stage.
  136. int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
  137. struct cpufreq_frequency_table *table);
  138. assures that at least one valid frequency is within policy->min and
  139. policy->max, and all other criteria are met. This is helpful for the
  140. ->verify call.
  141. int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
  142. struct cpufreq_frequency_table *table,
  143. unsigned int target_freq,
  144. unsigned int relation,
  145. unsigned int *index);
  146. is the corresponding frequency table helper for the ->target
  147. stage. Just pass the values to this function, and the unsigned int
  148. index returns the number of the frequency table entry which contains
  149. the frequency the CPU shall be set to. PLEASE NOTE: This is not the
  150. "index" which is in this cpufreq_table_entry.index, but instead
  151. cpufreq_table[index]. So, the new frequency is
  152. cpufreq_table[index].frequency, and the value you stored into the
  153. frequency table "index" field is
  154. cpufreq_table[index].index.