Back to home page

OSCL-LXR

 
 

    


0001 .. SPDX-License-Identifier: GPL-2.0
0002 
0003 ===============================================
0004 How to Implement a new CPUFreq Processor Driver
0005 ===============================================
0006 
0007 Authors:
0008 
0009 
0010         - Dominik Brodowski  <linux@brodo.de>
0011         - Rafael J. Wysocki <rafael.j.wysocki@intel.com>
0012         - Viresh Kumar <viresh.kumar@linaro.org>
0013 
0014 .. Contents
0015 
0016    1.   What To Do?
0017    1.1  Initialization
0018    1.2  Per-CPU Initialization
0019    1.3  verify
0020    1.4  target/target_index or setpolicy?
0021    1.5  target/target_index
0022    1.6  setpolicy
0023    1.7  get_intermediate and target_intermediate
0024    2.   Frequency Table Helpers
0025 
0026 
0027 
0028 1. What To Do?
0029 ==============
0030 
0031 So, you just got a brand-new CPU / chipset with datasheets and want to
0032 add cpufreq support for this CPU / chipset? Great. Here are some hints
0033 on what is necessary:
0034 
0035 
0036 1.1 Initialization
0037 ------------------
0038 
0039 First of all, in an __initcall level 7 (module_init()) or later
0040 function check whether this kernel runs on the right CPU and the right
0041 chipset. If so, register a struct cpufreq_driver with the CPUfreq core
0042 using cpufreq_register_driver()
0043 
0044 What shall this struct cpufreq_driver contain?
0045 
0046  .name - The name of this driver.
0047 
0048  .init - A pointer to the per-policy initialization function.
0049 
0050  .verify - A pointer to a "verification" function.
0051 
0052  .setpolicy _or_ .fast_switch _or_ .target _or_ .target_index - See
0053  below on the differences.
0054 
0055 And optionally
0056 
0057  .flags - Hints for the cpufreq core.
0058 
0059  .driver_data - cpufreq driver specific data.
0060 
0061  .get_intermediate and target_intermediate - Used to switch to stable
0062  frequency while changing CPU frequency.
0063 
0064  .get - Returns current frequency of the CPU.
0065 
0066  .bios_limit - Returns HW/BIOS max frequency limitations for the CPU.
0067 
0068  .exit - A pointer to a per-policy cleanup function called during
0069  CPU_POST_DEAD phase of cpu hotplug process.
0070 
0071  .suspend - A pointer to a per-policy suspend function which is called
0072  with interrupts disabled and _after_ the governor is stopped for the
0073  policy.
0074 
0075  .resume - A pointer to a per-policy resume function which is called
0076  with interrupts disabled and _before_ the governor is started again.
0077 
0078  .ready - A pointer to a per-policy ready function which is called after
0079  the policy is fully initialized.
0080 
0081  .attr - A pointer to a NULL-terminated list of "struct freq_attr" which
0082  allow to export values to sysfs.
0083 
0084  .boost_enabled - If set, boost frequencies are enabled.
0085 
0086  .set_boost - A pointer to a per-policy function to enable/disable boost
0087  frequencies.
0088 
0089 
0090 1.2 Per-CPU Initialization
0091 --------------------------
0092 
0093 Whenever a new CPU is registered with the device model, or after the
0094 cpufreq driver registers itself, the per-policy initialization function
0095 cpufreq_driver.init is called if no cpufreq policy existed for the CPU.
0096 Note that the .init() and .exit() routines are called only once for the
0097 policy and not for each CPU managed by the policy. It takes a ``struct
0098 cpufreq_policy *policy`` as argument. What to do now?
0099 
0100 If necessary, activate the CPUfreq support on your CPU.
0101 
0102 Then, the driver must fill in the following values:
0103 
0104 +-----------------------------------+--------------------------------------+
0105 |policy->cpuinfo.min_freq _and_     |                                      |
0106 |policy->cpuinfo.max_freq           | the minimum and maximum frequency    |
0107 |                                   | (in kHz) which is supported by       |
0108 |                                   | this CPU                             |
0109 +-----------------------------------+--------------------------------------+
0110 |policy->cpuinfo.transition_latency | the time it takes on this CPU to     |
0111 |                                   | switch between two frequencies in    |
0112 |                                   | nanoseconds (if appropriate, else    |
0113 |                                   | specify CPUFREQ_ETERNAL)             |
0114 +-----------------------------------+--------------------------------------+
0115 |policy->cur                        | The current operating frequency of   |
0116 |                                   | this CPU (if appropriate)            |
0117 +-----------------------------------+--------------------------------------+
0118 |policy->min,                       |                                      |
0119 |policy->max,                       |                                      |
0120 |policy->policy and, if necessary,  |                                      |
0121 |policy->governor                   | must contain the "default policy" for|
0122 |                                   | this CPU. A few moments later,       |
0123 |                                   | cpufreq_driver.verify and either     |
0124 |                                   | cpufreq_driver.setpolicy or          |
0125 |                                   | cpufreq_driver.target/target_index is|
0126 |                                   | called with these values.            |
0127 +-----------------------------------+--------------------------------------+
0128 |policy->cpus                       | Update this with the masks of the    |
0129 |                                   | (online + offline) CPUs that do DVFS |
0130 |                                   | along with this CPU (i.e.  that share|
0131 |                                   | clock/voltage rails with it).        |
0132 +-----------------------------------+--------------------------------------+
0133 
0134 For setting some of these values (cpuinfo.min[max]_freq, policy->min[max]), the
0135 frequency table helpers might be helpful. See the section 2 for more information
0136 on them.
0137 
0138 
0139 1.3 verify
0140 ----------
0141 
0142 When the user decides a new policy (consisting of
0143 "policy,governor,min,max") shall be set, this policy must be validated
0144 so that incompatible values can be corrected. For verifying these
0145 values cpufreq_verify_within_limits(``struct cpufreq_policy *policy``,
0146 ``unsigned int min_freq``, ``unsigned int max_freq``) function might be helpful.
0147 See section 2 for details on frequency table helpers.
0148 
0149 You need to make sure that at least one valid frequency (or operating
0150 range) is within policy->min and policy->max. If necessary, increase
0151 policy->max first, and only if this is no solution, decrease policy->min.
0152 
0153 
0154 1.4 target or target_index or setpolicy or fast_switch?
0155 -------------------------------------------------------
0156 
0157 Most cpufreq drivers or even most cpu frequency scaling algorithms
0158 only allow the CPU frequency to be set to predefined fixed values. For
0159 these, you use the ->target(), ->target_index() or ->fast_switch()
0160 callbacks.
0161 
0162 Some cpufreq capable processors switch the frequency between certain
0163 limits on their own. These shall use the ->setpolicy() callback.
0164 
0165 
0166 1.5. target/target_index
0167 ------------------------
0168 
0169 The target_index call has two arguments: ``struct cpufreq_policy *policy``,
0170 and ``unsigned int`` index (into the exposed frequency table).
0171 
0172 The CPUfreq driver must set the new frequency when called here. The
0173 actual frequency must be determined by freq_table[index].frequency.
0174 
0175 It should always restore to earlier frequency (i.e. policy->restore_freq) in
0176 case of errors, even if we switched to intermediate frequency earlier.
0177 
0178 Deprecated
0179 ----------
0180 The target call has three arguments: ``struct cpufreq_policy *policy``,
0181 unsigned int target_frequency, unsigned int relation.
0182 
0183 The CPUfreq driver must set the new frequency when called here. The
0184 actual frequency must be determined using the following rules:
0185 
0186 - keep close to "target_freq"
0187 - policy->min <= new_freq <= policy->max (THIS MUST BE VALID!!!)
0188 - if relation==CPUFREQ_REL_L, try to select a new_freq higher than or equal
0189   target_freq. ("L for lowest, but no lower than")
0190 - if relation==CPUFREQ_REL_H, try to select a new_freq lower than or equal
0191   target_freq. ("H for highest, but no higher than")
0192 
0193 Here again the frequency table helper might assist you - see section 2
0194 for details.
0195 
0196 1.6. fast_switch
0197 ----------------
0198 
0199 This function is used for frequency switching from scheduler's context.
0200 Not all drivers are expected to implement it, as sleeping from within
0201 this callback isn't allowed. This callback must be highly optimized to
0202 do switching as fast as possible.
0203 
0204 This function has two arguments: ``struct cpufreq_policy *policy`` and
0205 ``unsigned int target_frequency``.
0206 
0207 
0208 1.7 setpolicy
0209 -------------
0210 
0211 The setpolicy call only takes a ``struct cpufreq_policy *policy`` as
0212 argument. You need to set the lower limit of the in-processor or
0213 in-chipset dynamic frequency switching to policy->min, the upper limit
0214 to policy->max, and -if supported- select a performance-oriented
0215 setting when policy->policy is CPUFREQ_POLICY_PERFORMANCE, and a
0216 powersaving-oriented setting when CPUFREQ_POLICY_POWERSAVE. Also check
0217 the reference implementation in drivers/cpufreq/longrun.c
0218 
0219 1.8 get_intermediate and target_intermediate
0220 --------------------------------------------
0221 
0222 Only for drivers with target_index() and CPUFREQ_ASYNC_NOTIFICATION unset.
0223 
0224 get_intermediate should return a stable intermediate frequency platform wants to
0225 switch to, and target_intermediate() should set CPU to that frequency, before
0226 jumping to the frequency corresponding to 'index'. Core will take care of
0227 sending notifications and driver doesn't have to handle them in
0228 target_intermediate() or target_index().
0229 
0230 Drivers can return '0' from get_intermediate() in case they don't wish to switch
0231 to intermediate frequency for some target frequency. In that case core will
0232 directly call ->target_index().
0233 
0234 NOTE: ->target_index() should restore to policy->restore_freq in case of
0235 failures as core would send notifications for that.
0236 
0237 
0238 2. Frequency Table Helpers
0239 ==========================
0240 
0241 As most cpufreq processors only allow for being set to a few specific
0242 frequencies, a "frequency table" with some functions might assist in
0243 some work of the processor driver. Such a "frequency table" consists of
0244 an array of struct cpufreq_frequency_table entries, with driver specific
0245 values in "driver_data", the corresponding frequency in "frequency" and
0246 flags set. At the end of the table, you need to add a
0247 cpufreq_frequency_table entry with frequency set to CPUFREQ_TABLE_END.
0248 And if you want to skip one entry in the table, set the frequency to
0249 CPUFREQ_ENTRY_INVALID. The entries don't need to be in sorted in any
0250 particular order, but if they are cpufreq core will do DVFS a bit
0251 quickly for them as search for best match is faster.
0252 
0253 The cpufreq table is verified automatically by the core if the policy contains a
0254 valid pointer in its policy->freq_table field.
0255 
0256 cpufreq_frequency_table_verify() assures that at least one valid
0257 frequency is within policy->min and policy->max, and all other criteria
0258 are met. This is helpful for the ->verify call.
0259 
0260 cpufreq_frequency_table_target() is the corresponding frequency table
0261 helper for the ->target stage. Just pass the values to this function,
0262 and this function returns the of the frequency table entry which
0263 contains the frequency the CPU shall be set to.
0264 
0265 The following macros can be used as iterators over cpufreq_frequency_table:
0266 
0267 cpufreq_for_each_entry(pos, table) - iterates over all entries of frequency
0268 table.
0269 
0270 cpufreq_for_each_valid_entry(pos, table) - iterates over all entries,
0271 excluding CPUFREQ_ENTRY_INVALID frequencies.
0272 Use arguments "pos" - a ``cpufreq_frequency_table *`` as a loop cursor and
0273 "table" - the ``cpufreq_frequency_table *`` you want to iterate over.
0274 
0275 For example::
0276 
0277         struct cpufreq_frequency_table *pos, *driver_freq_table;
0278 
0279         cpufreq_for_each_entry(pos, driver_freq_table) {
0280                 /* Do something with pos */
0281                 pos->frequency = ...
0282         }
0283 
0284 If you need to work with the position of pos within driver_freq_table,
0285 do not subtract the pointers, as it is quite costly. Instead, use the
0286 macros cpufreq_for_each_entry_idx() and cpufreq_for_each_valid_entry_idx().