blob: 72f70b16d29984b1829b3998c611d4c9af524192 [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 D r i v e r s
7
8 - information for 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 To Do?
231.1 Initialization
241.2 Per-CPU Initialization
251.3 verify
261.4 target or setpolicy?
271.5 target
281.6 setpolicy
292. Frequency Table Helpers
30
31
32
331. What To Do?
34==============
35
36So, you just got a brand-new CPU / chipset with datasheets and want to
37add cpufreq support for this CPU / chipset? Great. Here are some hints
38on what is necessary:
39
40
411.1 Initialization
42------------------
43
44First of all, in an __initcall level 7 (module_init()) or later
45function check whether this kernel runs on the right CPU and the right
46chipset. If so, register a struct cpufreq_driver with the CPUfreq core
47using cpufreq_register_driver()
48
49What shall this struct cpufreq_driver contain?
50
51cpufreq_driver.name - The name of this driver.
52
53cpufreq_driver.owner - THIS_MODULE;
54
55cpufreq_driver.init - A pointer to the per-CPU initialization
56 function.
57
58cpufreq_driver.verify - A pointer to a "verification" function.
59
60cpufreq_driver.setpolicy _or_
61cpufreq_driver.target - See below on the differences.
62
63And optionally
64
65cpufreq_driver.exit - A pointer to a per-CPU cleanup function.
66
67cpufreq_driver.resume - A pointer to a per-CPU resume function
68 which is called with interrupts disabled
69 and _before_ the pre-suspend frequency
70 and/or policy is restored by a call to
71 ->target or ->setpolicy.
72
73cpufreq_driver.attr - A pointer to a NULL-terminated list of
74 "struct freq_attr" which allow to
75 export values to sysfs.
76
77
781.2 Per-CPU Initialization
79--------------------------
80
81Whenever a new CPU is registered with the device model, or after the
82cpufreq driver registers itself, the per-CPU initialization function
83cpufreq_driver.init is called. It takes a struct cpufreq_policy
84*policy as argument. What to do now?
85
86If necessary, activate the CPUfreq support on your CPU.
87
88Then, the driver must fill in the following values:
89
90policy->cpuinfo.min_freq _and_
91policy->cpuinfo.max_freq - the minimum and maximum frequency
92 (in kHz) which is supported by
93 this CPU
94policy->cpuinfo.transition_latency the time it takes on this CPU to
Mark Brownbbe237a2009-11-12 16:06:45 +000095 switch between two frequencies in
96 nanoseconds (if appropriate, else
97 specify CPUFREQ_ETERNAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99policy->cur The current operating frequency of
100 this CPU (if appropriate)
101policy->min,
102policy->max,
103policy->policy and, if necessary,
104policy->governor must contain the "default policy" for
105 this CPU. A few moments later,
106 cpufreq_driver.verify and either
107 cpufreq_driver.setpolicy or
108 cpufreq_driver.target is called with
109 these values.
110
111For setting some of these values, the frequency table helpers might be
112helpful. See the section 2 for more information on them.
113
Viresh Kumar951fc5f2013-01-31 02:03:53 +0000114SMP systems normally have same clock source for a group of cpus. For these the
115.init() would be called only once for the first online cpu. Here the .init()
116routine must initialize policy->cpus with mask of all possible cpus (Online +
117Offline) that share the clock. Then the core would copy this mask onto
118policy->related_cpus and will reset policy->cpus to carry only online cpus.
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
1211.3 verify
122------------
123
124When the user decides a new policy (consisting of
125"policy,governor,min,max") shall be set, this policy must be validated
126so that incompatible values can be corrected. For verifying these
127values, a frequency table helper and/or the
128cpufreq_verify_within_limits(struct cpufreq_policy *policy, unsigned
129int min_freq, unsigned int max_freq) function might be helpful. See
130section 2 for details on frequency table helpers.
131
132You need to make sure that at least one valid frequency (or operating
133range) is within policy->min and policy->max. If necessary, increase
134policy->max first, and only if this is no solution, decrease policy->min.
135
136
1371.4 target or setpolicy?
138----------------------------
139
140Most cpufreq drivers or even most cpu frequency scaling algorithms
141only allow the CPU to be set to one frequency. For these, you use the
142->target call.
143
144Some cpufreq-capable processors switch the frequency between certain
145limits on their own. These shall use the ->setpolicy call
146
147
1481.4. target
149-------------
150
151The target call has three arguments: struct cpufreq_policy *policy,
152unsigned int target_frequency, unsigned int relation.
153
154The CPUfreq driver must set the new frequency when called here. The
155actual frequency must be determined using the following rules:
156
157- keep close to "target_freq"
158- policy->min <= new_freq <= policy->max (THIS MUST BE VALID!!!)
159- if relation==CPUFREQ_REL_L, try to select a new_freq higher than or equal
160 target_freq. ("L for lowest, but no lower than")
161- if relation==CPUFREQ_REL_H, try to select a new_freq lower than or equal
162 target_freq. ("H for highest, but no higher than")
163
Chumbalkar Nagananda51555c02009-05-21 23:29:48 +0000164Here again the frequency table helper might assist you - see section 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165for details.
166
167
1681.5 setpolicy
169---------------
170
171The setpolicy call only takes a struct cpufreq_policy *policy as
172argument. You need to set the lower limit of the in-processor or
173in-chipset dynamic frequency switching to policy->min, the upper limit
174to policy->max, and -if supported- select a performance-oriented
175setting when policy->policy is CPUFREQ_POLICY_PERFORMANCE, and a
176powersaving-oriented setting when CPUFREQ_POLICY_POWERSAVE. Also check
Wanlong Gao25eb6502011-06-13 17:53:53 +0800177the reference implementation in drivers/cpufreq/longrun.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179
180
1812. Frequency Table Helpers
182==========================
183
184As most cpufreq processors only allow for being set to a few specific
185frequencies, a "frequency table" with some functions might assist in
186some work of the processor driver. Such a "frequency table" consists
187of an array of struct cpufreq_freq_table entries, with any value in
188"index" you want to use, and the corresponding frequency in
189"frequency". At the end of the table, you need to add a
190cpufreq_freq_table entry with frequency set to CPUFREQ_TABLE_END. And
191if you want to skip one entry in the table, set the frequency to
192CPUFREQ_ENTRY_INVALID. The entries don't need to be in ascending
193order.
194
195By calling cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
196 struct cpufreq_frequency_table *table);
197the cpuinfo.min_freq and cpuinfo.max_freq values are detected, and
198policy->min and policy->max are set to the same values. This is
199helpful for the per-CPU initialization stage.
200
201int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
202 struct cpufreq_frequency_table *table);
203assures that at least one valid frequency is within policy->min and
204policy->max, and all other criteria are met. This is helpful for the
205->verify call.
206
207int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
208 struct cpufreq_frequency_table *table,
209 unsigned int target_freq,
210 unsigned int relation,
211 unsigned int *index);
212
213is the corresponding frequency table helper for the ->target
214stage. Just pass the values to this function, and the unsigned int
215index returns the number of the frequency table entry which contains
216the frequency the CPU shall be set to. PLEASE NOTE: This is not the
217"index" which is in this cpufreq_table_entry.index, but instead
218cpufreq_table[index]. So, the new frequency is
219cpufreq_table[index].frequency, and the value you stored into the
220frequency table "index" field is
221cpufreq_table[index].index.
222