governors.txt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 F r e q G o v e r n o r s
  4. - information for users and 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 is a CPUFreq Governor?
  12. 2. Governors In the Linux Kernel
  13. 2.1 Performance
  14. 2.2 Powersave
  15. 2.3 Userspace
  16. 3. The Governor Interface in the CPUfreq Core
  17. 1. What Is A CPUFreq Governor?
  18. ==============================
  19. Most cpufreq drivers (in fact, all except one, longrun) or even most
  20. cpu frequency scaling algorithms only offer the CPU to be set to one
  21. frequency. In order to offer dynamic frequency scaling, the cpufreq
  22. core must be able to tell these drivers of a "target frequency". So
  23. these specific drivers will be transformed to offer a "->target"
  24. call instead of the existing "->setpolicy" call. For "longrun", all
  25. stays the same, though.
  26. How to decide what frequency within the CPUfreq policy should be used?
  27. That's done using "cpufreq governors". Two are already in this patch
  28. -- they're the already existing "powersave" and "performance" which
  29. set the frequency statically to the lowest or highest frequency,
  30. respectively. At least two more such governors will be ready for
  31. addition in the near future, but likely many more as there are various
  32. different theories and models about dynamic frequency scaling
  33. around. Using such a generic interface as cpufreq offers to scaling
  34. governors, these can be tested extensively, and the best one can be
  35. selected for each specific use.
  36. Basically, it's the following flow graph:
  37. CPU can be set to switch independetly | CPU can only be set
  38. within specific "limits" | to specific frequencies
  39. "CPUfreq policy"
  40. consists of frequency limits (policy->{min,max})
  41. and CPUfreq governor to be used
  42. / \
  43. / \
  44. / the cpufreq governor decides
  45. / (dynamically or statically)
  46. / what target_freq to set within
  47. / the limits of policy->{min,max}
  48. / \
  49. / \
  50. Using the ->setpolicy call, Using the ->target call,
  51. the limits and the the frequency closest
  52. "policy" is set. to target_freq is set.
  53. It is assured that it
  54. is within policy->{min,max}
  55. 2. Governors In the Linux Kernel
  56. ================================
  57. 2.1 Performance
  58. ---------------
  59. The CPUfreq governor "performance" sets the CPU statically to the
  60. highest frequency within the borders of scaling_min_freq and
  61. scaling_max_freq.
  62. 2.1 Powersave
  63. -------------
  64. The CPUfreq governor "powersave" sets the CPU statically to the
  65. lowest frequency within the borders of scaling_min_freq and
  66. scaling_max_freq.
  67. 2.2 Userspace
  68. -------------
  69. The CPUfreq governor "userspace" allows the user, or any userspace
  70. program running with UID "root", to set the CPU to a specific frequency
  71. by making a sysfs file "scaling_setspeed" available in the CPU-device
  72. directory.
  73. 3. The Governor Interface in the CPUfreq Core
  74. =============================================
  75. A new governor must register itself with the CPUfreq core using
  76. "cpufreq_register_governor". The struct cpufreq_governor, which has to
  77. be passed to that function, must contain the following values:
  78. governor->name - A unique name for this governor
  79. governor->governor - The governor callback function
  80. governor->owner - .THIS_MODULE for the governor module (if
  81. appropriate)
  82. The governor->governor callback is called with the current (or to-be-set)
  83. cpufreq_policy struct for that CPU, and an unsigned int event. The
  84. following events are currently defined:
  85. CPUFREQ_GOV_START: This governor shall start its duty for the CPU
  86. policy->cpu
  87. CPUFREQ_GOV_STOP: This governor shall end its duty for the CPU
  88. policy->cpu
  89. CPUFREQ_GOV_LIMITS: The limits for CPU policy->cpu have changed to
  90. policy->min and policy->max.
  91. If you need other "events" externally of your driver, _only_ use the
  92. cpufreq_governor_l(unsigned int cpu, unsigned int event) call to the
  93. CPUfreq core to ensure proper locking.
  94. The CPUfreq governor may call the CPU processor driver using one of
  95. these two functions:
  96. int cpufreq_driver_target(struct cpufreq_policy *policy,
  97. unsigned int target_freq,
  98. unsigned int relation);
  99. int __cpufreq_driver_target(struct cpufreq_policy *policy,
  100. unsigned int target_freq,
  101. unsigned int relation);
  102. target_freq must be within policy->min and policy->max, of course.
  103. What's the difference between these two functions? When your governor
  104. still is in a direct code path of a call to governor->governor, the
  105. per-CPU cpufreq lock is still held in the cpufreq core, and there's
  106. no need to lock it again (in fact, this would cause a deadlock). So
  107. use __cpufreq_driver_target only in these cases. In all other cases
  108. (for example, when there's a "daemonized" function that wakes up
  109. every second), use cpufreq_driver_target to lock the cpufreq per-CPU
  110. lock before the command is passed to the cpufreq processor driver.