blob: 3b32336a7803ebc59faab9b908b599dac93b52fc [file] [log] [blame]
Mauro Carvalho Chehab8f9205892020-03-03 14:52:05 +01001.. SPDX-License-Identifier: GPL-2.0
2
3===============================================
4How to Implement a new CPUFreq Processor Driver
5===============================================
6
7Authors:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
9
Mauro Carvalho Chehab8f9205892020-03-03 14:52:05 +010010 - Dominik Brodowski <linux@brodo.de>
11 - Rafael J. Wysocki <rafael.j.wysocki@intel.com>
12 - Viresh Kumar <viresh.kumar@linaro.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Mauro Carvalho Chehab8f9205892020-03-03 14:52:05 +010014.. Contents
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Mauro Carvalho Chehab8f9205892020-03-03 14:52:05 +010016 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
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
Mauro Carvalho Chehab8f9205892020-03-03 14:52:05 +010044What shall this struct cpufreq_driver contain?
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Viresh Kumar7de962c2017-01-06 11:08:05 +053046 .name - The name of this driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Viresh Kumar7de962c2017-01-06 11:08:05 +053048 .init - A pointer to the per-policy initialization function.
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Viresh Kumar7de962c2017-01-06 11:08:05 +053050 .verify - A pointer to a "verification" function.
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Viresh Kumar7de962c2017-01-06 11:08:05 +053052 .setpolicy _or_ .fast_switch _or_ .target _or_ .target_index - See
53 below on the differences.
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55And optionally
56
Viresh Kumar7de962c2017-01-06 11:08:05 +053057 .flags - Hints for the cpufreq core.
Dirk Brandewie367dc4a2014-03-19 08:45:53 -070058
Viresh Kumar7de962c2017-01-06 11:08:05 +053059 .driver_data - cpufreq driver specific data.
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Viresh Kumar7de962c2017-01-06 11:08:05 +053061 .get_intermediate and target_intermediate - Used to switch to stable
62 frequency while changing CPU frequency.
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Viresh Kumar7de962c2017-01-06 11:08:05 +053064 .get - Returns current frequency of the CPU.
65
66 .bios_limit - Returns HW/BIOS max frequency limitations for the CPU.
67
68 .exit - A pointer to a per-policy cleanup function called during
69 CPU_POST_DEAD phase of cpu hotplug process.
70
Viresh Kumar7de962c2017-01-06 11:08:05 +053071 .suspend - A pointer to a per-policy suspend function which is called
72 with interrupts disabled and _after_ the governor is stopped for the
73 policy.
74
75 .resume - A pointer to a per-policy resume function which is called
76 with interrupts disabled and _before_ the governor is started again.
77
Viresh Kumar7de962c2017-01-06 11:08:05 +053078 .attr - A pointer to a NULL-terminated list of "struct freq_attr" which
79 allow to export values to sysfs.
80
81 .boost_enabled - If set, boost frequencies are enabled.
82
83 .set_boost - A pointer to a per-policy function to enable/disable boost
84 frequencies.
Viresh Kumar1c03a2d2014-06-02 22:49:28 +053085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
871.2 Per-CPU Initialization
88--------------------------
89
90Whenever a new CPU is registered with the device model, or after the
Viresh Kumar7de962c2017-01-06 11:08:05 +053091cpufreq driver registers itself, the per-policy initialization function
92cpufreq_driver.init is called if no cpufreq policy existed for the CPU.
93Note that the .init() and .exit() routines are called only once for the
Mauro Carvalho Chehab8f9205892020-03-03 14:52:05 +010094policy and not for each CPU managed by the policy. It takes a ``struct
95cpufreq_policy *policy`` as argument. What to do now?
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97If necessary, activate the CPUfreq support on your CPU.
98
99Then, the driver must fill in the following values:
100
Mauro Carvalho Chehab8f9205892020-03-03 14:52:05 +0100101+-----------------------------------+--------------------------------------+
102|policy->cpuinfo.min_freq _and_ | |
103|policy->cpuinfo.max_freq | the minimum and maximum frequency |
104| | (in kHz) which is supported by |
105| | this CPU |
106+-----------------------------------+--------------------------------------+
107|policy->cpuinfo.transition_latency | the time it takes on this CPU to |
108| | switch between two frequencies in |
109| | nanoseconds (if appropriate, else |
110| | specify CPUFREQ_ETERNAL) |
111+-----------------------------------+--------------------------------------+
112|policy->cur | The current operating frequency of |
113| | this CPU (if appropriate) |
114+-----------------------------------+--------------------------------------+
115|policy->min, | |
116|policy->max, | |
117|policy->policy and, if necessary, | |
118|policy->governor | must contain the "default policy" for|
119| | this CPU. A few moments later, |
120| | cpufreq_driver.verify and either |
121| | cpufreq_driver.setpolicy or |
122| | cpufreq_driver.target/target_index is|
123| | called with these values. |
124+-----------------------------------+--------------------------------------+
125|policy->cpus | Update this with the masks of the |
126| | (online + offline) CPUs that do DVFS |
127| | along with this CPU (i.e. that share|
128| | clock/voltage rails with it). |
129+-----------------------------------+--------------------------------------+
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Viresh Kumareb2f50f2013-04-01 12:57:48 +0000131For setting some of these values (cpuinfo.min[max]_freq, policy->min[max]), the
132frequency table helpers might be helpful. See the section 2 for more information
133on them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135
1361.3 verify
Viresh Kumar7de962c2017-01-06 11:08:05 +0530137----------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139When the user decides a new policy (consisting of
140"policy,governor,min,max") shall be set, this policy must be validated
141so that incompatible values can be corrected. For verifying these
Mauro Carvalho Chehab8f9205892020-03-03 14:52:05 +0100142values cpufreq_verify_within_limits(``struct cpufreq_policy *policy``,
143``unsigned int min_freq``, ``unsigned int max_freq``) function might be helpful.
Viresh Kumar7de962c2017-01-06 11:08:05 +0530144See section 2 for details on frequency table helpers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146You need to make sure that at least one valid frequency (or operating
147range) is within policy->min and policy->max. If necessary, increase
148policy->max first, and only if this is no solution, decrease policy->min.
149
150
Viresh Kumar7de962c2017-01-06 11:08:05 +05301511.4 target or target_index or setpolicy or fast_switch?
152-------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Mauro Carvalho Chehab8f9205892020-03-03 14:52:05 +0100154Most cpufreq drivers or even most cpu frequency scaling algorithms
Viresh Kumar7de962c2017-01-06 11:08:05 +0530155only allow the CPU frequency to be set to predefined fixed values. For
156these, you use the ->target(), ->target_index() or ->fast_switch()
157callbacks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Viresh Kumar7de962c2017-01-06 11:08:05 +0530159Some cpufreq capable processors switch the frequency between certain
160limits on their own. These shall use the ->setpolicy() callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301631.5. target/target_index
Viresh Kumar7de962c2017-01-06 11:08:05 +0530164------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Mauro Carvalho Chehab8f9205892020-03-03 14:52:05 +0100166The target_index call has two arguments: ``struct cpufreq_policy *policy``,
167and ``unsigned int`` index (into the exposed frequency table).
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530168
169The CPUfreq driver must set the new frequency when called here. The
170actual frequency must be determined by freq_table[index].frequency.
171
Viresh Kumar1c03a2d2014-06-02 22:49:28 +0530172It should always restore to earlier frequency (i.e. policy->restore_freq) in
173case of errors, even if we switched to intermediate frequency earlier.
174
Mauro Carvalho Chehab8f9205892020-03-03 14:52:05 +0100175Deprecated
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530176----------
Mauro Carvalho Chehab8f9205892020-03-03 14:52:05 +0100177The target call has three arguments: ``struct cpufreq_policy *policy``,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178unsigned int target_frequency, unsigned int relation.
179
180The CPUfreq driver must set the new frequency when called here. The
181actual frequency must be determined using the following rules:
182
183- keep close to "target_freq"
184- policy->min <= new_freq <= policy->max (THIS MUST BE VALID!!!)
185- if relation==CPUFREQ_REL_L, try to select a new_freq higher than or equal
186 target_freq. ("L for lowest, but no lower than")
187- if relation==CPUFREQ_REL_H, try to select a new_freq lower than or equal
188 target_freq. ("H for highest, but no higher than")
189
Chumbalkar Nagananda51555c02009-05-21 23:29:48 +0000190Here again the frequency table helper might assist you - see section 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191for details.
192
Viresh Kumar7de962c2017-01-06 11:08:05 +05301931.6. fast_switch
194----------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Viresh Kumar7de962c2017-01-06 11:08:05 +0530196This function is used for frequency switching from scheduler's context.
197Not all drivers are expected to implement it, as sleeping from within
198this callback isn't allowed. This callback must be highly optimized to
199do switching as fast as possible.
200
Mauro Carvalho Chehab8f9205892020-03-03 14:52:05 +0100201This function has two arguments: ``struct cpufreq_policy *policy`` and
202``unsigned int target_frequency``.
Viresh Kumar7de962c2017-01-06 11:08:05 +0530203
204
2051.7 setpolicy
206-------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Mauro Carvalho Chehab8f9205892020-03-03 14:52:05 +0100208The setpolicy call only takes a ``struct cpufreq_policy *policy`` as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209argument. You need to set the lower limit of the in-processor or
210in-chipset dynamic frequency switching to policy->min, the upper limit
211to policy->max, and -if supported- select a performance-oriented
212setting when policy->policy is CPUFREQ_POLICY_PERFORMANCE, and a
213powersaving-oriented setting when CPUFREQ_POLICY_POWERSAVE. Also check
Wanlong Gao25eb6502011-06-13 17:53:53 +0800214the reference implementation in drivers/cpufreq/longrun.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Viresh Kumar7de962c2017-01-06 11:08:05 +05302161.8 get_intermediate and target_intermediate
Viresh Kumar1c03a2d2014-06-02 22:49:28 +0530217--------------------------------------------
218
219Only for drivers with target_index() and CPUFREQ_ASYNC_NOTIFICATION unset.
220
221get_intermediate should return a stable intermediate frequency platform wants to
sayli karnik54f5d132017-03-09 11:48:21 +0530222switch to, and target_intermediate() should set CPU to that frequency, before
Viresh Kumar1c03a2d2014-06-02 22:49:28 +0530223jumping to the frequency corresponding to 'index'. Core will take care of
224sending notifications and driver doesn't have to handle them in
225target_intermediate() or target_index().
226
227Drivers can return '0' from get_intermediate() in case they don't wish to switch
228to intermediate frequency for some target frequency. In that case core will
229directly call ->target_index().
230
231NOTE: ->target_index() should restore to policy->restore_freq in case of
232failures as core would send notifications for that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234
2352. Frequency Table Helpers
236==========================
237
238As most cpufreq processors only allow for being set to a few specific
239frequencies, a "frequency table" with some functions might assist in
Viresh Kumar7de962c2017-01-06 11:08:05 +0530240some work of the processor driver. Such a "frequency table" consists of
241an array of struct cpufreq_frequency_table entries, with driver specific
242values in "driver_data", the corresponding frequency in "frequency" and
243flags set. At the end of the table, you need to add a
244cpufreq_frequency_table entry with frequency set to CPUFREQ_TABLE_END.
245And if you want to skip one entry in the table, set the frequency to
246CPUFREQ_ENTRY_INVALID. The entries don't need to be in sorted in any
247particular order, but if they are cpufreq core will do DVFS a bit
248quickly for them as search for best match is faster.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Viresh Kumar2dd0df82018-04-03 15:37:39 +0530250The cpufreq table is verified automatically by the core if the policy contains a
251valid pointer in its policy->freq_table field.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Viresh Kumar7de962c2017-01-06 11:08:05 +0530253cpufreq_frequency_table_verify() assures that at least one valid
254frequency is within policy->min and policy->max, and all other criteria
255are met. This is helpful for the ->verify call.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Viresh Kumar7de962c2017-01-06 11:08:05 +0530257cpufreq_frequency_table_target() is the corresponding frequency table
258helper for the ->target stage. Just pass the values to this function,
259and this function returns the of the frequency table entry which
260contains the frequency the CPU shall be set to.
Stratos Karafotis27e289d2014-04-25 23:15:23 +0300261
262The following macros can be used as iterators over cpufreq_frequency_table:
263
264cpufreq_for_each_entry(pos, table) - iterates over all entries of frequency
265table.
266
Viresh Kumar7de962c2017-01-06 11:08:05 +0530267cpufreq_for_each_valid_entry(pos, table) - iterates over all entries,
Stratos Karafotis27e289d2014-04-25 23:15:23 +0300268excluding CPUFREQ_ENTRY_INVALID frequencies.
Mauro Carvalho Chehab8f9205892020-03-03 14:52:05 +0100269Use arguments "pos" - a ``cpufreq_frequency_table *`` as a loop cursor and
270"table" - the ``cpufreq_frequency_table *`` you want to iterate over.
Stratos Karafotis27e289d2014-04-25 23:15:23 +0300271
Mauro Carvalho Chehab8f9205892020-03-03 14:52:05 +0100272For example::
Stratos Karafotis27e289d2014-04-25 23:15:23 +0300273
274 struct cpufreq_frequency_table *pos, *driver_freq_table;
275
276 cpufreq_for_each_entry(pos, driver_freq_table) {
277 /* Do something with pos */
278 pos->frequency = ...
279 }
Dominik Brodowskiffd81dc2018-01-30 06:42:37 +0100280
281If you need to work with the position of pos within driver_freq_table,
282do not subtract the pointers, as it is quite costly. Instead, use the
283macros cpufreq_for_each_entry_idx() and cpufreq_for_each_valid_entry_idx().