blob: b85481acd0ca49fdb6625faaae44293cc213d3ea [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>
12
13
14
15 Clock scaling allows you to change the clock speed of the CPUs on the
16 fly. This is a nice method to save battery power, because the lower
17 the clock speed, the less power the CPU consumes.
18
19
20Contents:
21---------
221. What is a CPUFreq Governor?
23
242. Governors In the Linux Kernel
252.1 Performance
262.2 Powersave
272.3 Userspace
28
293. The Governor Interface in the CPUfreq Core
30
31
32
331. What Is A CPUFreq Governor?
34==============================
35
36Most cpufreq drivers (in fact, all except one, longrun) or even most
37cpu frequency scaling algorithms only offer the CPU to be set to one
38frequency. In order to offer dynamic frequency scaling, the cpufreq
39core must be able to tell these drivers of a "target frequency". So
40these specific drivers will be transformed to offer a "->target"
41call instead of the existing "->setpolicy" call. For "longrun", all
42stays the same, though.
43
44How to decide what frequency within the CPUfreq policy should be used?
45That's done using "cpufreq governors". Two are already in this patch
46-- they're the already existing "powersave" and "performance" which
47set the frequency statically to the lowest or highest frequency,
48respectively. At least two more such governors will be ready for
49addition in the near future, but likely many more as there are various
50different theories and models about dynamic frequency scaling
51around. Using such a generic interface as cpufreq offers to scaling
52governors, these can be tested extensively, and the best one can be
53selected for each specific use.
54
55Basically, it's the following flow graph:
56
57CPU can be set to switch independetly | CPU can only be set
58 within specific "limits" | to specific frequencies
59
60 "CPUfreq policy"
61 consists of frequency limits (policy->{min,max})
62 and CPUfreq governor to be used
63 / \
64 / \
65 / the cpufreq governor decides
66 / (dynamically or statically)
67 / what target_freq to set within
68 / the limits of policy->{min,max}
69 / \
70 / \
71 Using the ->setpolicy call, Using the ->target call,
72 the limits and the the frequency closest
73 "policy" is set. to target_freq is set.
74 It is assured that it
75 is within policy->{min,max}
76
77
782. Governors In the Linux Kernel
79================================
80
812.1 Performance
82---------------
83
84The CPUfreq governor "performance" sets the CPU statically to the
85highest frequency within the borders of scaling_min_freq and
86scaling_max_freq.
87
88
892.1 Powersave
90-------------
91
92The CPUfreq governor "powersave" sets the CPU statically to the
93lowest frequency within the borders of scaling_min_freq and
94scaling_max_freq.
95
96
972.2 Userspace
98-------------
99
100The CPUfreq governor "userspace" allows the user, or any userspace
101program running with UID "root", to set the CPU to a specific frequency
102by making a sysfs file "scaling_setspeed" available in the CPU-device
103directory.
104
105
106
1073. The Governor Interface in the CPUfreq Core
108=============================================
109
110A new governor must register itself with the CPUfreq core using
111"cpufreq_register_governor". The struct cpufreq_governor, which has to
112be passed to that function, must contain the following values:
113
114governor->name - A unique name for this governor
115governor->governor - The governor callback function
116governor->owner - .THIS_MODULE for the governor module (if
117 appropriate)
118
119The governor->governor callback is called with the current (or to-be-set)
120cpufreq_policy struct for that CPU, and an unsigned int event. The
121following events are currently defined:
122
123CPUFREQ_GOV_START: This governor shall start its duty for the CPU
124 policy->cpu
125CPUFREQ_GOV_STOP: This governor shall end its duty for the CPU
126 policy->cpu
127CPUFREQ_GOV_LIMITS: The limits for CPU policy->cpu have changed to
128 policy->min and policy->max.
129
130If you need other "events" externally of your driver, _only_ use the
131cpufreq_governor_l(unsigned int cpu, unsigned int event) call to the
132CPUfreq core to ensure proper locking.
133
134
135The CPUfreq governor may call the CPU processor driver using one of
136these two functions:
137
138int cpufreq_driver_target(struct cpufreq_policy *policy,
139 unsigned int target_freq,
140 unsigned int relation);
141
142int __cpufreq_driver_target(struct cpufreq_policy *policy,
143 unsigned int target_freq,
144 unsigned int relation);
145
146target_freq must be within policy->min and policy->max, of course.
147What's the difference between these two functions? When your governor
148still is in a direct code path of a call to governor->governor, the
149per-CPU cpufreq lock is still held in the cpufreq core, and there's
150no need to lock it again (in fact, this would cause a deadlock). So
151use __cpufreq_driver_target only in these cases. In all other cases
152(for example, when there's a "daemonized" function that wakes up
153every second), use cpufreq_driver_target to lock the cpufreq per-CPU
154lock before the command is passed to the cpufreq processor driver.
155