blob: 933fae74c3379cba1fd7d066a2ab7f6daead76a3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001 CPU frequency and voltage scaling code in the Linux(TM) kernel
2
3
4 L i n u x C P U F r e q
5
6 C P U F r e q G o v e r n o r s
7
8 - information for users and developers -
9
10
11 Dominik Brodowski <linux@brodo.de>
Nico Golde594dd2c2005-06-25 14:58:33 -070012 some additions and corrections by Nico Golde <nico@ngolde.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14
15
16 Clock scaling allows you to change the clock speed of the CPUs on the
17 fly. This is a nice method to save battery power, because the lower
18 the clock speed, the less power the CPU consumes.
19
20
21Contents:
22---------
231. What is a CPUFreq Governor?
24
252. Governors In the Linux Kernel
262.1 Performance
272.2 Powersave
282.3 Userspace
Nico Golde594dd2c2005-06-25 14:58:33 -0700292.4 Ondemand
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
313. The Governor Interface in the CPUfreq Core
32
33
34
351. What Is A CPUFreq Governor?
36==============================
37
38Most cpufreq drivers (in fact, all except one, longrun) or even most
39cpu frequency scaling algorithms only offer the CPU to be set to one
40frequency. In order to offer dynamic frequency scaling, the cpufreq
41core must be able to tell these drivers of a "target frequency". So
42these specific drivers will be transformed to offer a "->target"
43call instead of the existing "->setpolicy" call. For "longrun", all
44stays the same, though.
45
46How to decide what frequency within the CPUfreq policy should be used?
47That's done using "cpufreq governors". Two are already in this patch
48-- they're the already existing "powersave" and "performance" which
49set the frequency statically to the lowest or highest frequency,
50respectively. At least two more such governors will be ready for
51addition in the near future, but likely many more as there are various
52different theories and models about dynamic frequency scaling
53around. Using such a generic interface as cpufreq offers to scaling
54governors, these can be tested extensively, and the best one can be
55selected for each specific use.
56
57Basically, it's the following flow graph:
58
59CPU can be set to switch independetly | CPU can only be set
60 within specific "limits" | to specific frequencies
61
62 "CPUfreq policy"
63 consists of frequency limits (policy->{min,max})
64 and CPUfreq governor to be used
65 / \
66 / \
67 / the cpufreq governor decides
68 / (dynamically or statically)
69 / what target_freq to set within
70 / the limits of policy->{min,max}
71 / \
72 / \
73 Using the ->setpolicy call, Using the ->target call,
74 the limits and the the frequency closest
75 "policy" is set. to target_freq is set.
76 It is assured that it
77 is within policy->{min,max}
78
79
802. Governors In the Linux Kernel
81================================
82
832.1 Performance
84---------------
85
86The CPUfreq governor "performance" sets the CPU statically to the
87highest frequency within the borders of scaling_min_freq and
88scaling_max_freq.
89
90
Nico Golde594dd2c2005-06-25 14:58:33 -0700912.2 Powersave
Linus Torvalds1da177e2005-04-16 15:20:36 -070092-------------
93
94The CPUfreq governor "powersave" sets the CPU statically to the
95lowest frequency within the borders of scaling_min_freq and
96scaling_max_freq.
97
98
Nico Golde594dd2c2005-06-25 14:58:33 -0700992.3 Userspace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100-------------
101
102The CPUfreq governor "userspace" allows the user, or any userspace
103program running with UID "root", to set the CPU to a specific frequency
104by making a sysfs file "scaling_setspeed" available in the CPU-device
105directory.
106
107
Nico Golde594dd2c2005-06-25 14:58:33 -07001082.4 Ondemand
109------------
110
111The CPUfreq govenor "ondemand" sets the CPU depending on the
112current usage. To do this the CPU must have the capability to
113switch the frequency very fast.
114
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
1173. The Governor Interface in the CPUfreq Core
118=============================================
119
120A new governor must register itself with the CPUfreq core using
121"cpufreq_register_governor". The struct cpufreq_governor, which has to
122be passed to that function, must contain the following values:
123
124governor->name - A unique name for this governor
125governor->governor - The governor callback function
126governor->owner - .THIS_MODULE for the governor module (if
127 appropriate)
128
129The governor->governor callback is called with the current (or to-be-set)
130cpufreq_policy struct for that CPU, and an unsigned int event. The
131following events are currently defined:
132
133CPUFREQ_GOV_START: This governor shall start its duty for the CPU
134 policy->cpu
135CPUFREQ_GOV_STOP: This governor shall end its duty for the CPU
136 policy->cpu
137CPUFREQ_GOV_LIMITS: The limits for CPU policy->cpu have changed to
138 policy->min and policy->max.
139
140If you need other "events" externally of your driver, _only_ use the
141cpufreq_governor_l(unsigned int cpu, unsigned int event) call to the
142CPUfreq core to ensure proper locking.
143
144
145The CPUfreq governor may call the CPU processor driver using one of
146these two functions:
147
148int cpufreq_driver_target(struct cpufreq_policy *policy,
149 unsigned int target_freq,
150 unsigned int relation);
151
152int __cpufreq_driver_target(struct cpufreq_policy *policy,
153 unsigned int target_freq,
154 unsigned int relation);
155
156target_freq must be within policy->min and policy->max, of course.
157What's the difference between these two functions? When your governor
158still is in a direct code path of a call to governor->governor, the
159per-CPU cpufreq lock is still held in the cpufreq core, and there's
160no need to lock it again (in fact, this would cause a deadlock). So
161use __cpufreq_driver_target only in these cases. In all other cases
162(for example, when there's a "daemonized" function that wakes up
163every second), use cpufreq_driver_target to lock the cpufreq per-CPU
164lock before the command is passed to the cpufreq processor driver.
165