xref: /openbmc/linux/Documentation/cpu-freq/cpu-drivers.rst (revision 4f774c4a65bf3987d1a95c966e884f38c8a942af)
18f920589SMauro Carvalho Chehab.. SPDX-License-Identifier: GPL-2.0
28f920589SMauro Carvalho Chehab
38f920589SMauro Carvalho Chehab===============================================
48f920589SMauro Carvalho ChehabHow to Implement a new CPUFreq Processor Driver
58f920589SMauro Carvalho Chehab===============================================
68f920589SMauro Carvalho Chehab
78f920589SMauro Carvalho ChehabAuthors:
88f920589SMauro Carvalho Chehab
98f920589SMauro Carvalho Chehab
108f920589SMauro Carvalho Chehab	- Dominik Brodowski  <linux@brodo.de>
118f920589SMauro Carvalho Chehab	- Rafael J. Wysocki <rafael.j.wysocki@intel.com>
128f920589SMauro Carvalho Chehab	- Viresh Kumar <viresh.kumar@linaro.org>
138f920589SMauro Carvalho Chehab
148f920589SMauro Carvalho Chehab.. Contents
158f920589SMauro Carvalho Chehab
168f920589SMauro Carvalho Chehab   1.   What To Do?
178f920589SMauro Carvalho Chehab   1.1  Initialization
188f920589SMauro Carvalho Chehab   1.2  Per-CPU Initialization
198f920589SMauro Carvalho Chehab   1.3  verify
208f920589SMauro Carvalho Chehab   1.4  target/target_index or setpolicy?
218f920589SMauro Carvalho Chehab   1.5  target/target_index
228f920589SMauro Carvalho Chehab   1.6  setpolicy
238f920589SMauro Carvalho Chehab   1.7  get_intermediate and target_intermediate
248f920589SMauro Carvalho Chehab   2.   Frequency Table Helpers
258f920589SMauro Carvalho Chehab
268f920589SMauro Carvalho Chehab
278f920589SMauro Carvalho Chehab
288f920589SMauro Carvalho Chehab1. What To Do?
298f920589SMauro Carvalho Chehab==============
308f920589SMauro Carvalho Chehab
318f920589SMauro Carvalho ChehabSo, you just got a brand-new CPU / chipset with datasheets and want to
328f920589SMauro Carvalho Chehabadd cpufreq support for this CPU / chipset? Great. Here are some hints
338f920589SMauro Carvalho Chehabon what is necessary:
348f920589SMauro Carvalho Chehab
358f920589SMauro Carvalho Chehab
368f920589SMauro Carvalho Chehab1.1 Initialization
378f920589SMauro Carvalho Chehab------------------
388f920589SMauro Carvalho Chehab
398f920589SMauro Carvalho ChehabFirst of all, in an __initcall level 7 (module_init()) or later
408f920589SMauro Carvalho Chehabfunction check whether this kernel runs on the right CPU and the right
418f920589SMauro Carvalho Chehabchipset. If so, register a struct cpufreq_driver with the CPUfreq core
428f920589SMauro Carvalho Chehabusing cpufreq_register_driver()
438f920589SMauro Carvalho Chehab
448f920589SMauro Carvalho ChehabWhat shall this struct cpufreq_driver contain?
458f920589SMauro Carvalho Chehab
468f920589SMauro Carvalho Chehab .name - The name of this driver.
478f920589SMauro Carvalho Chehab
488f920589SMauro Carvalho Chehab .init - A pointer to the per-policy initialization function.
498f920589SMauro Carvalho Chehab
508f920589SMauro Carvalho Chehab .verify - A pointer to a "verification" function.
518f920589SMauro Carvalho Chehab
528f920589SMauro Carvalho Chehab .setpolicy _or_ .fast_switch _or_ .target _or_ .target_index - See
538f920589SMauro Carvalho Chehab below on the differences.
548f920589SMauro Carvalho Chehab
558f920589SMauro Carvalho ChehabAnd optionally
568f920589SMauro Carvalho Chehab
578f920589SMauro Carvalho Chehab .flags - Hints for the cpufreq core.
588f920589SMauro Carvalho Chehab
598f920589SMauro Carvalho Chehab .driver_data - cpufreq driver specific data.
608f920589SMauro Carvalho Chehab
618f920589SMauro Carvalho Chehab .get_intermediate and target_intermediate - Used to switch to stable
628f920589SMauro Carvalho Chehab frequency while changing CPU frequency.
638f920589SMauro Carvalho Chehab
648f920589SMauro Carvalho Chehab .get - Returns current frequency of the CPU.
658f920589SMauro Carvalho Chehab
668f920589SMauro Carvalho Chehab .bios_limit - Returns HW/BIOS max frequency limitations for the CPU.
678f920589SMauro Carvalho Chehab
688f920589SMauro Carvalho Chehab .exit - A pointer to a per-policy cleanup function called during
698f920589SMauro Carvalho Chehab CPU_POST_DEAD phase of cpu hotplug process.
708f920589SMauro Carvalho Chehab
718f920589SMauro Carvalho Chehab .suspend - A pointer to a per-policy suspend function which is called
728f920589SMauro Carvalho Chehab with interrupts disabled and _after_ the governor is stopped for the
738f920589SMauro Carvalho Chehab policy.
748f920589SMauro Carvalho Chehab
758f920589SMauro Carvalho Chehab .resume - A pointer to a per-policy resume function which is called
768f920589SMauro Carvalho Chehab with interrupts disabled and _before_ the governor is started again.
778f920589SMauro Carvalho Chehab
78*4f774c4aSBjorn Andersson .ready - A pointer to a per-policy ready function which is called after
79*4f774c4aSBjorn Andersson the policy is fully initialized.
80*4f774c4aSBjorn Andersson
818f920589SMauro Carvalho Chehab .attr - A pointer to a NULL-terminated list of "struct freq_attr" which
828f920589SMauro Carvalho Chehab allow to export values to sysfs.
838f920589SMauro Carvalho Chehab
848f920589SMauro Carvalho Chehab .boost_enabled - If set, boost frequencies are enabled.
858f920589SMauro Carvalho Chehab
868f920589SMauro Carvalho Chehab .set_boost - A pointer to a per-policy function to enable/disable boost
878f920589SMauro Carvalho Chehab frequencies.
888f920589SMauro Carvalho Chehab
898f920589SMauro Carvalho Chehab
908f920589SMauro Carvalho Chehab1.2 Per-CPU Initialization
918f920589SMauro Carvalho Chehab--------------------------
928f920589SMauro Carvalho Chehab
938f920589SMauro Carvalho ChehabWhenever a new CPU is registered with the device model, or after the
948f920589SMauro Carvalho Chehabcpufreq driver registers itself, the per-policy initialization function
958f920589SMauro Carvalho Chehabcpufreq_driver.init is called if no cpufreq policy existed for the CPU.
968f920589SMauro Carvalho ChehabNote that the .init() and .exit() routines are called only once for the
978f920589SMauro Carvalho Chehabpolicy and not for each CPU managed by the policy. It takes a ``struct
988f920589SMauro Carvalho Chehabcpufreq_policy *policy`` as argument. What to do now?
998f920589SMauro Carvalho Chehab
1008f920589SMauro Carvalho ChehabIf necessary, activate the CPUfreq support on your CPU.
1018f920589SMauro Carvalho Chehab
1028f920589SMauro Carvalho ChehabThen, the driver must fill in the following values:
1038f920589SMauro Carvalho Chehab
1048f920589SMauro Carvalho Chehab+-----------------------------------+--------------------------------------+
1058f920589SMauro Carvalho Chehab|policy->cpuinfo.min_freq _and_	    |					   |
1068f920589SMauro Carvalho Chehab|policy->cpuinfo.max_freq	    | the minimum and maximum frequency	   |
1078f920589SMauro Carvalho Chehab|				    | (in kHz) which is supported by	   |
1088f920589SMauro Carvalho Chehab|				    | this CPU				   |
1098f920589SMauro Carvalho Chehab+-----------------------------------+--------------------------------------+
1108f920589SMauro Carvalho Chehab|policy->cpuinfo.transition_latency | the time it takes on this CPU to	   |
1118f920589SMauro Carvalho Chehab|				    | switch between two frequencies in	   |
1128f920589SMauro Carvalho Chehab|				    | nanoseconds (if appropriate, else	   |
1138f920589SMauro Carvalho Chehab|				    | specify CPUFREQ_ETERNAL)		   |
1148f920589SMauro Carvalho Chehab+-----------------------------------+--------------------------------------+
1158f920589SMauro Carvalho Chehab|policy->cur			    | The current operating frequency of   |
1168f920589SMauro Carvalho Chehab|				    | this CPU (if appropriate)		   |
1178f920589SMauro Carvalho Chehab+-----------------------------------+--------------------------------------+
1188f920589SMauro Carvalho Chehab|policy->min,			    |					   |
1198f920589SMauro Carvalho Chehab|policy->max,			    |					   |
1208f920589SMauro Carvalho Chehab|policy->policy and, if necessary,  |					   |
1218f920589SMauro Carvalho Chehab|policy->governor		    | must contain the "default policy" for|
1228f920589SMauro Carvalho Chehab|				    | this CPU. A few moments later,       |
1238f920589SMauro Carvalho Chehab|				    | cpufreq_driver.verify and either     |
1248f920589SMauro Carvalho Chehab|				    | cpufreq_driver.setpolicy or          |
1258f920589SMauro Carvalho Chehab|				    | cpufreq_driver.target/target_index is|
1268f920589SMauro Carvalho Chehab|				    | called with these values.		   |
1278f920589SMauro Carvalho Chehab+-----------------------------------+--------------------------------------+
1288f920589SMauro Carvalho Chehab|policy->cpus			    | Update this with the masks of the	   |
1298f920589SMauro Carvalho Chehab|				    | (online + offline) CPUs that do DVFS |
1308f920589SMauro Carvalho Chehab|				    | along with this CPU (i.e.  that share|
1318f920589SMauro Carvalho Chehab|				    | clock/voltage rails with it).	   |
1328f920589SMauro Carvalho Chehab+-----------------------------------+--------------------------------------+
1338f920589SMauro Carvalho Chehab
1348f920589SMauro Carvalho ChehabFor setting some of these values (cpuinfo.min[max]_freq, policy->min[max]), the
1358f920589SMauro Carvalho Chehabfrequency table helpers might be helpful. See the section 2 for more information
1368f920589SMauro Carvalho Chehabon them.
1378f920589SMauro Carvalho Chehab
1388f920589SMauro Carvalho Chehab
1398f920589SMauro Carvalho Chehab1.3 verify
1408f920589SMauro Carvalho Chehab----------
1418f920589SMauro Carvalho Chehab
1428f920589SMauro Carvalho ChehabWhen the user decides a new policy (consisting of
1438f920589SMauro Carvalho Chehab"policy,governor,min,max") shall be set, this policy must be validated
1448f920589SMauro Carvalho Chehabso that incompatible values can be corrected. For verifying these
1458f920589SMauro Carvalho Chehabvalues cpufreq_verify_within_limits(``struct cpufreq_policy *policy``,
1468f920589SMauro Carvalho Chehab``unsigned int min_freq``, ``unsigned int max_freq``) function might be helpful.
1478f920589SMauro Carvalho ChehabSee section 2 for details on frequency table helpers.
1488f920589SMauro Carvalho Chehab
1498f920589SMauro Carvalho ChehabYou need to make sure that at least one valid frequency (or operating
1508f920589SMauro Carvalho Chehabrange) is within policy->min and policy->max. If necessary, increase
1518f920589SMauro Carvalho Chehabpolicy->max first, and only if this is no solution, decrease policy->min.
1528f920589SMauro Carvalho Chehab
1538f920589SMauro Carvalho Chehab
1548f920589SMauro Carvalho Chehab1.4 target or target_index or setpolicy or fast_switch?
1558f920589SMauro Carvalho Chehab-------------------------------------------------------
1568f920589SMauro Carvalho Chehab
1578f920589SMauro Carvalho ChehabMost cpufreq drivers or even most cpu frequency scaling algorithms
1588f920589SMauro Carvalho Chehabonly allow the CPU frequency to be set to predefined fixed values. For
1598f920589SMauro Carvalho Chehabthese, you use the ->target(), ->target_index() or ->fast_switch()
1608f920589SMauro Carvalho Chehabcallbacks.
1618f920589SMauro Carvalho Chehab
1628f920589SMauro Carvalho ChehabSome cpufreq capable processors switch the frequency between certain
1638f920589SMauro Carvalho Chehablimits on their own. These shall use the ->setpolicy() callback.
1648f920589SMauro Carvalho Chehab
1658f920589SMauro Carvalho Chehab
1668f920589SMauro Carvalho Chehab1.5. target/target_index
1678f920589SMauro Carvalho Chehab------------------------
1688f920589SMauro Carvalho Chehab
1698f920589SMauro Carvalho ChehabThe target_index call has two arguments: ``struct cpufreq_policy *policy``,
1708f920589SMauro Carvalho Chehaband ``unsigned int`` index (into the exposed frequency table).
1718f920589SMauro Carvalho Chehab
1728f920589SMauro Carvalho ChehabThe CPUfreq driver must set the new frequency when called here. The
1738f920589SMauro Carvalho Chehabactual frequency must be determined by freq_table[index].frequency.
1748f920589SMauro Carvalho Chehab
1758f920589SMauro Carvalho ChehabIt should always restore to earlier frequency (i.e. policy->restore_freq) in
1768f920589SMauro Carvalho Chehabcase of errors, even if we switched to intermediate frequency earlier.
1778f920589SMauro Carvalho Chehab
1788f920589SMauro Carvalho ChehabDeprecated
1798f920589SMauro Carvalho Chehab----------
1808f920589SMauro Carvalho ChehabThe target call has three arguments: ``struct cpufreq_policy *policy``,
1818f920589SMauro Carvalho Chehabunsigned int target_frequency, unsigned int relation.
1828f920589SMauro Carvalho Chehab
1838f920589SMauro Carvalho ChehabThe CPUfreq driver must set the new frequency when called here. The
1848f920589SMauro Carvalho Chehabactual frequency must be determined using the following rules:
1858f920589SMauro Carvalho Chehab
1868f920589SMauro Carvalho Chehab- keep close to "target_freq"
1878f920589SMauro Carvalho Chehab- policy->min <= new_freq <= policy->max (THIS MUST BE VALID!!!)
1888f920589SMauro Carvalho Chehab- if relation==CPUFREQ_REL_L, try to select a new_freq higher than or equal
1898f920589SMauro Carvalho Chehab  target_freq. ("L for lowest, but no lower than")
1908f920589SMauro Carvalho Chehab- if relation==CPUFREQ_REL_H, try to select a new_freq lower than or equal
1918f920589SMauro Carvalho Chehab  target_freq. ("H for highest, but no higher than")
1928f920589SMauro Carvalho Chehab
1938f920589SMauro Carvalho ChehabHere again the frequency table helper might assist you - see section 2
1948f920589SMauro Carvalho Chehabfor details.
1958f920589SMauro Carvalho Chehab
1968f920589SMauro Carvalho Chehab1.6. fast_switch
1978f920589SMauro Carvalho Chehab----------------
1988f920589SMauro Carvalho Chehab
1998f920589SMauro Carvalho ChehabThis function is used for frequency switching from scheduler's context.
2008f920589SMauro Carvalho ChehabNot all drivers are expected to implement it, as sleeping from within
2018f920589SMauro Carvalho Chehabthis callback isn't allowed. This callback must be highly optimized to
2028f920589SMauro Carvalho Chehabdo switching as fast as possible.
2038f920589SMauro Carvalho Chehab
2048f920589SMauro Carvalho ChehabThis function has two arguments: ``struct cpufreq_policy *policy`` and
2058f920589SMauro Carvalho Chehab``unsigned int target_frequency``.
2068f920589SMauro Carvalho Chehab
2078f920589SMauro Carvalho Chehab
2088f920589SMauro Carvalho Chehab1.7 setpolicy
2098f920589SMauro Carvalho Chehab-------------
2108f920589SMauro Carvalho Chehab
2118f920589SMauro Carvalho ChehabThe setpolicy call only takes a ``struct cpufreq_policy *policy`` as
2128f920589SMauro Carvalho Chehabargument. You need to set the lower limit of the in-processor or
2138f920589SMauro Carvalho Chehabin-chipset dynamic frequency switching to policy->min, the upper limit
2148f920589SMauro Carvalho Chehabto policy->max, and -if supported- select a performance-oriented
2158f920589SMauro Carvalho Chehabsetting when policy->policy is CPUFREQ_POLICY_PERFORMANCE, and a
2168f920589SMauro Carvalho Chehabpowersaving-oriented setting when CPUFREQ_POLICY_POWERSAVE. Also check
2178f920589SMauro Carvalho Chehabthe reference implementation in drivers/cpufreq/longrun.c
2188f920589SMauro Carvalho Chehab
2198f920589SMauro Carvalho Chehab1.8 get_intermediate and target_intermediate
2208f920589SMauro Carvalho Chehab--------------------------------------------
2218f920589SMauro Carvalho Chehab
2228f920589SMauro Carvalho ChehabOnly for drivers with target_index() and CPUFREQ_ASYNC_NOTIFICATION unset.
2238f920589SMauro Carvalho Chehab
2248f920589SMauro Carvalho Chehabget_intermediate should return a stable intermediate frequency platform wants to
2258f920589SMauro Carvalho Chehabswitch to, and target_intermediate() should set CPU to that frequency, before
2268f920589SMauro Carvalho Chehabjumping to the frequency corresponding to 'index'. Core will take care of
2278f920589SMauro Carvalho Chehabsending notifications and driver doesn't have to handle them in
2288f920589SMauro Carvalho Chehabtarget_intermediate() or target_index().
2298f920589SMauro Carvalho Chehab
2308f920589SMauro Carvalho ChehabDrivers can return '0' from get_intermediate() in case they don't wish to switch
2318f920589SMauro Carvalho Chehabto intermediate frequency for some target frequency. In that case core will
2328f920589SMauro Carvalho Chehabdirectly call ->target_index().
2338f920589SMauro Carvalho Chehab
2348f920589SMauro Carvalho ChehabNOTE: ->target_index() should restore to policy->restore_freq in case of
2358f920589SMauro Carvalho Chehabfailures as core would send notifications for that.
2368f920589SMauro Carvalho Chehab
2378f920589SMauro Carvalho Chehab
2388f920589SMauro Carvalho Chehab2. Frequency Table Helpers
2398f920589SMauro Carvalho Chehab==========================
2408f920589SMauro Carvalho Chehab
2418f920589SMauro Carvalho ChehabAs most cpufreq processors only allow for being set to a few specific
2428f920589SMauro Carvalho Chehabfrequencies, a "frequency table" with some functions might assist in
2438f920589SMauro Carvalho Chehabsome work of the processor driver. Such a "frequency table" consists of
2448f920589SMauro Carvalho Chehaban array of struct cpufreq_frequency_table entries, with driver specific
2458f920589SMauro Carvalho Chehabvalues in "driver_data", the corresponding frequency in "frequency" and
2468f920589SMauro Carvalho Chehabflags set. At the end of the table, you need to add a
2478f920589SMauro Carvalho Chehabcpufreq_frequency_table entry with frequency set to CPUFREQ_TABLE_END.
2488f920589SMauro Carvalho ChehabAnd if you want to skip one entry in the table, set the frequency to
2498f920589SMauro Carvalho ChehabCPUFREQ_ENTRY_INVALID. The entries don't need to be in sorted in any
2508f920589SMauro Carvalho Chehabparticular order, but if they are cpufreq core will do DVFS a bit
2518f920589SMauro Carvalho Chehabquickly for them as search for best match is faster.
2528f920589SMauro Carvalho Chehab
2538f920589SMauro Carvalho ChehabThe cpufreq table is verified automatically by the core if the policy contains a
2548f920589SMauro Carvalho Chehabvalid pointer in its policy->freq_table field.
2558f920589SMauro Carvalho Chehab
2568f920589SMauro Carvalho Chehabcpufreq_frequency_table_verify() assures that at least one valid
2578f920589SMauro Carvalho Chehabfrequency is within policy->min and policy->max, and all other criteria
2588f920589SMauro Carvalho Chehabare met. This is helpful for the ->verify call.
2598f920589SMauro Carvalho Chehab
2608f920589SMauro Carvalho Chehabcpufreq_frequency_table_target() is the corresponding frequency table
2618f920589SMauro Carvalho Chehabhelper for the ->target stage. Just pass the values to this function,
2628f920589SMauro Carvalho Chehaband this function returns the of the frequency table entry which
2638f920589SMauro Carvalho Chehabcontains the frequency the CPU shall be set to.
2648f920589SMauro Carvalho Chehab
2658f920589SMauro Carvalho ChehabThe following macros can be used as iterators over cpufreq_frequency_table:
2668f920589SMauro Carvalho Chehab
2678f920589SMauro Carvalho Chehabcpufreq_for_each_entry(pos, table) - iterates over all entries of frequency
2688f920589SMauro Carvalho Chehabtable.
2698f920589SMauro Carvalho Chehab
2708f920589SMauro Carvalho Chehabcpufreq_for_each_valid_entry(pos, table) - iterates over all entries,
2718f920589SMauro Carvalho Chehabexcluding CPUFREQ_ENTRY_INVALID frequencies.
2728f920589SMauro Carvalho ChehabUse arguments "pos" - a ``cpufreq_frequency_table *`` as a loop cursor and
2738f920589SMauro Carvalho Chehab"table" - the ``cpufreq_frequency_table *`` you want to iterate over.
2748f920589SMauro Carvalho Chehab
2758f920589SMauro Carvalho ChehabFor example::
2768f920589SMauro Carvalho Chehab
2778f920589SMauro Carvalho Chehab	struct cpufreq_frequency_table *pos, *driver_freq_table;
2788f920589SMauro Carvalho Chehab
2798f920589SMauro Carvalho Chehab	cpufreq_for_each_entry(pos, driver_freq_table) {
2808f920589SMauro Carvalho Chehab		/* Do something with pos */
2818f920589SMauro Carvalho Chehab		pos->frequency = ...
2828f920589SMauro Carvalho Chehab	}
2838f920589SMauro Carvalho Chehab
2848f920589SMauro Carvalho ChehabIf you need to work with the position of pos within driver_freq_table,
2858f920589SMauro Carvalho Chehabdo not subtract the pointers, as it is quite costly. Instead, use the
2868f920589SMauro Carvalho Chehabmacros cpufreq_for_each_entry_idx() and cpufreq_for_each_valid_entry_idx().
287