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