cpufreq: Remove the ->stop_cpu() driver callback
[linux-2.6-block.git] / Documentation / cpu-freq / cpu-drivers.rst
CommitLineData
8f920589 1.. SPDX-License-Identifier: GPL-2.0
1da177e4 2
8f920589
MCC
3===============================================
4How to Implement a new CPUFreq Processor Driver
5===============================================
1da177e4 6
8f920589 7Authors:
1da177e4 8
1da177e4 9
8f920589
MCC
10 - Dominik Brodowski <linux@brodo.de>
11 - Rafael J. Wysocki <rafael.j.wysocki@intel.com>
12 - Viresh Kumar <viresh.kumar@linaro.org>
1da177e4 13
8f920589 14.. Contents
1da177e4 15
8f920589
MCC
16 1. What To Do?
17 1.1 Initialization
18 1.2 Per-CPU Initialization
19 1.3 verify
20 1.4 target/target_index or setpolicy?
21 1.5 target/target_index
22 1.6 setpolicy
23 1.7 get_intermediate and target_intermediate
24 2. Frequency Table Helpers
1da177e4
LT
25
26
27
281. What To Do?
29==============
30
31So, you just got a brand-new CPU / chipset with datasheets and want to
32add cpufreq support for this CPU / chipset? Great. Here are some hints
33on what is necessary:
34
35
361.1 Initialization
37------------------
38
39First of all, in an __initcall level 7 (module_init()) or later
40function check whether this kernel runs on the right CPU and the right
41chipset. If so, register a struct cpufreq_driver with the CPUfreq core
42using cpufreq_register_driver()
43
8f920589 44What shall this struct cpufreq_driver contain?
1da177e4 45
7de962c0 46 .name - The name of this driver.
1da177e4 47
7de962c0 48 .init - A pointer to the per-policy initialization function.
1da177e4 49
7de962c0 50 .verify - A pointer to a "verification" function.
1da177e4 51
7de962c0
VK
52 .setpolicy _or_ .fast_switch _or_ .target _or_ .target_index - See
53 below on the differences.
1da177e4
LT
54
55And optionally
56
7de962c0 57 .flags - Hints for the cpufreq core.
367dc4aa 58
7de962c0 59 .driver_data - cpufreq driver specific data.
1da177e4 60
7de962c0
VK
61 .resolve_freq - Returns the most appropriate frequency for a target
62 frequency. Doesn't change the frequency though.
1da177e4 63
7de962c0
VK
64 .get_intermediate and target_intermediate - Used to switch to stable
65 frequency while changing CPU frequency.
1da177e4 66
7de962c0
VK
67 .get - Returns current frequency of the CPU.
68
69 .bios_limit - Returns HW/BIOS max frequency limitations for the CPU.
70
71 .exit - A pointer to a per-policy cleanup function called during
72 CPU_POST_DEAD phase of cpu hotplug process.
7de962c0
VK
73
74 .suspend - A pointer to a per-policy suspend function which is called
75 with interrupts disabled and _after_ the governor is stopped for the
76 policy.
77
78 .resume - A pointer to a per-policy resume function which is called
79 with interrupts disabled and _before_ the governor is started again.
80
81 .ready - A pointer to a per-policy ready function which is called after
82 the policy is fully initialized.
83
84 .attr - A pointer to a NULL-terminated list of "struct freq_attr" which
85 allow to export values to sysfs.
86
87 .boost_enabled - If set, boost frequencies are enabled.
88
89 .set_boost - A pointer to a per-policy function to enable/disable boost
90 frequencies.
1c03a2d0 91
1da177e4
LT
92
931.2 Per-CPU Initialization
94--------------------------
95
96Whenever a new CPU is registered with the device model, or after the
7de962c0
VK
97cpufreq driver registers itself, the per-policy initialization function
98cpufreq_driver.init is called if no cpufreq policy existed for the CPU.
99Note that the .init() and .exit() routines are called only once for the
8f920589
MCC
100policy and not for each CPU managed by the policy. It takes a ``struct
101cpufreq_policy *policy`` as argument. What to do now?
1da177e4
LT
102
103If necessary, activate the CPUfreq support on your CPU.
104
105Then, the driver must fill in the following values:
106
8f920589
MCC
107+-----------------------------------+--------------------------------------+
108|policy->cpuinfo.min_freq _and_ | |
109|policy->cpuinfo.max_freq | the minimum and maximum frequency |
110| | (in kHz) which is supported by |
111| | this CPU |
112+-----------------------------------+--------------------------------------+
113|policy->cpuinfo.transition_latency | the time it takes on this CPU to |
114| | switch between two frequencies in |
115| | nanoseconds (if appropriate, else |
116| | specify CPUFREQ_ETERNAL) |
117+-----------------------------------+--------------------------------------+
118|policy->cur | The current operating frequency of |
119| | this CPU (if appropriate) |
120+-----------------------------------+--------------------------------------+
121|policy->min, | |
122|policy->max, | |
123|policy->policy and, if necessary, | |
124|policy->governor | must contain the "default policy" for|
125| | this CPU. A few moments later, |
126| | cpufreq_driver.verify and either |
127| | cpufreq_driver.setpolicy or |
128| | cpufreq_driver.target/target_index is|
129| | called with these values. |
130+-----------------------------------+--------------------------------------+
131|policy->cpus | Update this with the masks of the |
132| | (online + offline) CPUs that do DVFS |
133| | along with this CPU (i.e. that share|
134| | clock/voltage rails with it). |
135+-----------------------------------+--------------------------------------+
1da177e4 136
eb2f50ff
VK
137For setting some of these values (cpuinfo.min[max]_freq, policy->min[max]), the
138frequency table helpers might be helpful. See the section 2 for more information
139on them.
1da177e4
LT
140
141
1421.3 verify
7de962c0 143----------
1da177e4
LT
144
145When the user decides a new policy (consisting of
146"policy,governor,min,max") shall be set, this policy must be validated
147so that incompatible values can be corrected. For verifying these
8f920589
MCC
148values cpufreq_verify_within_limits(``struct cpufreq_policy *policy``,
149``unsigned int min_freq``, ``unsigned int max_freq``) function might be helpful.
7de962c0 150See section 2 for details on frequency table helpers.
1da177e4
LT
151
152You need to make sure that at least one valid frequency (or operating
153range) is within policy->min and policy->max. If necessary, increase
154policy->max first, and only if this is no solution, decrease policy->min.
155
156
7de962c0
VK
1571.4 target or target_index or setpolicy or fast_switch?
158-------------------------------------------------------
1da177e4 159
8f920589 160Most cpufreq drivers or even most cpu frequency scaling algorithms
7de962c0
VK
161only allow the CPU frequency to be set to predefined fixed values. For
162these, you use the ->target(), ->target_index() or ->fast_switch()
163callbacks.
1da177e4 164
7de962c0
VK
165Some cpufreq capable processors switch the frequency between certain
166limits on their own. These shall use the ->setpolicy() callback.
1da177e4
LT
167
168
1c03a2d0 1691.5. target/target_index
7de962c0 170------------------------
1da177e4 171
8f920589
MCC
172The target_index call has two arguments: ``struct cpufreq_policy *policy``,
173and ``unsigned int`` index (into the exposed frequency table).
9c0ebcf7
VK
174
175The CPUfreq driver must set the new frequency when called here. The
176actual frequency must be determined by freq_table[index].frequency.
177
1c03a2d0
VK
178It should always restore to earlier frequency (i.e. policy->restore_freq) in
179case of errors, even if we switched to intermediate frequency earlier.
180
8f920589 181Deprecated
9c0ebcf7 182----------
8f920589 183The target call has three arguments: ``struct cpufreq_policy *policy``,
1da177e4
LT
184unsigned int target_frequency, unsigned int relation.
185
186The CPUfreq driver must set the new frequency when called here. The
187actual frequency must be determined using the following rules:
188
189- keep close to "target_freq"
190- policy->min <= new_freq <= policy->max (THIS MUST BE VALID!!!)
191- if relation==CPUFREQ_REL_L, try to select a new_freq higher than or equal
192 target_freq. ("L for lowest, but no lower than")
193- if relation==CPUFREQ_REL_H, try to select a new_freq lower than or equal
194 target_freq. ("H for highest, but no higher than")
195
51555c0e 196Here again the frequency table helper might assist you - see section 2
1da177e4
LT
197for details.
198
7de962c0
VK
1991.6. fast_switch
200----------------
1da177e4 201
7de962c0
VK
202This function is used for frequency switching from scheduler's context.
203Not all drivers are expected to implement it, as sleeping from within
204this callback isn't allowed. This callback must be highly optimized to
205do switching as fast as possible.
206
8f920589
MCC
207This function has two arguments: ``struct cpufreq_policy *policy`` and
208``unsigned int target_frequency``.
7de962c0
VK
209
210
2111.7 setpolicy
212-------------
1da177e4 213
8f920589 214The setpolicy call only takes a ``struct cpufreq_policy *policy`` as
1da177e4
LT
215argument. You need to set the lower limit of the in-processor or
216in-chipset dynamic frequency switching to policy->min, the upper limit
217to policy->max, and -if supported- select a performance-oriented
218setting when policy->policy is CPUFREQ_POLICY_PERFORMANCE, and a
219powersaving-oriented setting when CPUFREQ_POLICY_POWERSAVE. Also check
25eb650a 220the reference implementation in drivers/cpufreq/longrun.c
1da177e4 221
7de962c0 2221.8 get_intermediate and target_intermediate
1c03a2d0
VK
223--------------------------------------------
224
225Only for drivers with target_index() and CPUFREQ_ASYNC_NOTIFICATION unset.
226
227get_intermediate should return a stable intermediate frequency platform wants to
54f5d13b 228switch to, and target_intermediate() should set CPU to that frequency, before
1c03a2d0
VK
229jumping to the frequency corresponding to 'index'. Core will take care of
230sending notifications and driver doesn't have to handle them in
231target_intermediate() or target_index().
232
233Drivers can return '0' from get_intermediate() in case they don't wish to switch
234to intermediate frequency for some target frequency. In that case core will
235directly call ->target_index().
236
237NOTE: ->target_index() should restore to policy->restore_freq in case of
238failures as core would send notifications for that.
1da177e4
LT
239
240
2412. Frequency Table Helpers
242==========================
243
244As most cpufreq processors only allow for being set to a few specific
245frequencies, a "frequency table" with some functions might assist in
7de962c0
VK
246some work of the processor driver. Such a "frequency table" consists of
247an array of struct cpufreq_frequency_table entries, with driver specific
248values in "driver_data", the corresponding frequency in "frequency" and
249flags set. At the end of the table, you need to add a
250cpufreq_frequency_table entry with frequency set to CPUFREQ_TABLE_END.
251And if you want to skip one entry in the table, set the frequency to
252CPUFREQ_ENTRY_INVALID. The entries don't need to be in sorted in any
253particular order, but if they are cpufreq core will do DVFS a bit
254quickly for them as search for best match is faster.
255
2dd0df84
VK
256The cpufreq table is verified automatically by the core if the policy contains a
257valid pointer in its policy->freq_table field.
7de962c0
VK
258
259cpufreq_frequency_table_verify() assures that at least one valid
260frequency is within policy->min and policy->max, and all other criteria
261are met. This is helpful for the ->verify call.
262
263cpufreq_frequency_table_target() is the corresponding frequency table
264helper for the ->target stage. Just pass the values to this function,
265and this function returns the of the frequency table entry which
266contains the frequency the CPU shall be set to.
27e289dc
SK
267
268The following macros can be used as iterators over cpufreq_frequency_table:
269
270cpufreq_for_each_entry(pos, table) - iterates over all entries of frequency
271table.
272
7de962c0 273cpufreq_for_each_valid_entry(pos, table) - iterates over all entries,
27e289dc 274excluding CPUFREQ_ENTRY_INVALID frequencies.
8f920589
MCC
275Use arguments "pos" - a ``cpufreq_frequency_table *`` as a loop cursor and
276"table" - the ``cpufreq_frequency_table *`` you want to iterate over.
27e289dc 277
8f920589 278For example::
27e289dc
SK
279
280 struct cpufreq_frequency_table *pos, *driver_freq_table;
281
282 cpufreq_for_each_entry(pos, driver_freq_table) {
283 /* Do something with pos */
284 pos->frequency = ...
285 }
ffd81dcf
DB
286
287If you need to work with the position of pos within driver_freq_table,
288do not subtract the pointers, as it is quite costly. Instead, use the
289macros cpufreq_for_each_entry_idx() and cpufreq_for_each_valid_entry_idx().