General description of the CPUFreq core and CPUFreq notifiers

Authors:

1. General Information

The CPUFreq core code is located in drivers/cpufreq/cpufreq.c. Thiscpufreq code offers a standardized interface for the CPUFreqarchitecture drivers (those pieces of code that do actualfrequency transitions), as well as to “notifiers”. These are devicedrivers or other part of the kernel that need to be informed ofpolicy changes (ex. thermal modules like ACPI) or of allfrequency changes (ex. timing code) or even need to force certainspeed limits (like LCD drivers on ARM architecture). Additionally, thekernel “constant” loops_per_jiffy is updated on frequency changeshere.

Reference counting of the cpufreq policies is done by cpufreq_cpu_getand cpufreq_cpu_put, which make sure that the cpufreq driver iscorrectly registered with the core, and will not be unloaded untilcpufreq_put_cpu is called. That also ensures that the respective cpufreqpolicy doesn’t get freed while being used.

2. CPUFreq notifiers

CPUFreq notifiers conform to the standard kernel notifier interface.See linux/include/linux/notifier.h for details on notifiers.

There are two different CPUFreq notifiers - policy notifiers andtransition notifiers.

2.1 CPUFreq policy notifiers

These are notified when a new policy is created or removed.

The phase is specified in the second argument to the notifier. The phase isCPUFREQ_CREATE_POLICY when the policy is first created and it isCPUFREQ_REMOVE_POLICY when the policy is removed.

The third argument, avoid*pointer, points to a struct cpufreq_policyconsisting of several values, including min, max (the lower and upperfrequencies (in kHz) of the new policy).

2.2 CPUFreq transition notifiers

These are notified twice for each online CPU in the policy, when theCPUfreq driver switches the CPU core frequency and this change has noany external implications.

The second argument specifies the phase - CPUFREQ_PRECHANGE orCPUFREQ_POSTCHANGE.

The third argument is a struct cpufreq_freqs with the followingvalues:

cpunumber of the affected CPU
oldold frequency
newnew frequency
flagsflags of the cpufreq driver

3. CPUFreq Table Generation with Operating Performance Point (OPP)

For details about OPP, see Documentation/power/opp.rst

dev_pm_opp_init_cpufreq_table -

This function provides a ready to use conversion routine to translatethe OPP layer’s internal information about the available frequenciesinto a format readily providable to cpufreq.

Warning

Do not use this function in interrupt context.

Example:

soc_pm_init(){       /* Do things */       r = dev_pm_opp_init_cpufreq_table(dev, &freq_table);       if (!r)               policy->freq_table = freq_table;       /* Do other things */}

Note

This function is available only if CONFIG_CPU_FREQ is enabled inaddition to CONFIG_PM_OPP.

dev_pm_opp_free_cpufreq_table
Free up the table allocated by dev_pm_opp_init_cpufreq_table