governors.txt 5.6 KB

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