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