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