CPUFREQ(9)
π CPUFREQ(9) β the kernel dev manual, bestie edition π
okay so this is CPUFREQ(9), a page straight out of the Kernel Developer's Manual, and it's literally all about making your CPU chillax at different speeds. buckle up π¦
β¨ NAME β¨
cpufreq, cpufreq_register, cpufreq_deregister, cpufreq_suspend, cpufreq_resume, cpufreq_get, cpufreq_get_backend, cpufreq_get_state, cpufreq_get_state_index, cpufreq_set, cpufreq_set_all β the whole interface for CPU frequency scaling π₯
πΈ SYNOPSIS πΈ
#include <sys/cpufreq.h>
int
cpufreq_register(struct cpufreq *cf);
void
cpufreq_deregister(void);
void
cpufreq_suspend(struct cpu_info *ci);
void
cpufreq_resume(struct cpu_info *ci);
uint32_t
cpufreq_get(struct cpu_info *ci);
int
cpufreq_get_backend(struct cpufreq *cf);
int
cpufreq_get_state(uint32_t freq, struct cpufreq_state *cfs);
int
cpufreq_get_state_index(uint32_t index, struct cpufreq_state *cfs);
void
cpufreq_set(struct cpu_info *ci, uint32_t freq);
void
cpufreq_set_all(uint32_t freq);
π DESCRIPTION π
The machine-independent cpufreq interface is basically the group chat admin for CPU frequency scaling β it provides the framework, and a machine-dependent backend implementation does the actual scaling. bestie, it's a whole team effort π
Lowkey important: the cpufreq interface is a per-CPU framework. It's implicitly assumed that the frequency can be set independently for all processors in the system. However β and this is the iconic part β cpufreq does not imply any restrictions upon whether this information is utilized by the actual machine-dependent implementation. So yes, it is possible to use cpufreq with frequency scaling implemented via pci(4). In addition, it's assumed that the available frequency levels are shared uniformly by all processors in the system, even when it is possible to control the frequency of individual processors. shared vibes only β¨
ngl you should also know that the cpufreq interface is generally stateless. what does that mean for you? it implies, for instance, that possible caching should be done in the machine-dependent backend. the cpufreq_suspend() and cpufreq_resume() functions are the exceptions to this β those two can be integrated with pmf(9). π«Ά
π¦ FUNCTIONS π¦
cpufreq_register(cf)
The cpufreq_register() function initializes the interface by associating a machine-dependent backend with the framework. Only one backend can be registered β no sharing, no polyamory, one backend only π
. Upon successful completion, cpufreq_register() returns 0 and sets the frequency of all processors to the maximum available level. Note that the registration can be done only after interrupts have been enabled; cf. config_interrupts(9). β οΈ
The following elements in struct cpufreq should be filled prior to the call:
char cf_name[CPUFREQ_NAME_MAX];
struct cpufreq_state cf_state[CPUFREQ_STATE_MAX];
uint32_t cf_state_count;
bool cf_mp;
void *cf_cookie;
xcfunc_t cf_get_freq;
xcfunc_t cf_set_freq;
- The name of the backend should be given in
cf_name. π·οΈ - The
cpufreq_statestructure conveys descriptive information about the frequency states. The following fields can be used for the registration:
uint32_t cfs_freq;
uint32_t cfs_power;
From these, cfs_freq (the clock frequency in MHz) is mandatory, whereas the optional cfs_power can be filled to describe the power consumption (in mW) of each state. The cf_state array must be filled in descending order β that is, the highest frequency should be at the zero index. β
If the backend operates with a simple boolean switch without knowing the clock frequencies, the cfs_freq field should be set to CPUFREQ_STATE_ENABLED or CPUFREQ_STATE_DISABLED. The first constant should precede the latter one in cf_state. π
- The
cf_state_countfield defines the number of states that the backend has filled in thecf_statearray. π’ - The
cf_mpboolean should be set tofalseif it is known that the backend can not handle per-CPU frequency states; changes should always be propagated to all processors in the system. π’ - The
cf_cookiefield is an opaque pointer passed to the backend whencpufreq_get(),cpufreq_set(), orcpufreq_set_all()is called. πͺ - The
cf_get_freqandcf_set_freqare function pointers that should be associated with the machine-dependent functions to get and set a frequency, respectively. Thexcfunc_ttype is part ofxcall(9). When the function pointers are invoked bycpufreq, the first parameter is always thecf_cookieand the second parameter is the frequency, defined asuint32_t *. π
cpufreq_deregister()
Deregisters any possible backend in use. bye bestie π
cpufreq_suspend(ci)
The cpufreq_suspend() can be called when the processor suspends. The function saves the current frequency of ci and sets the minimum available frequency. π΄
cpufreq_resume(ci)
Resumes the frequency of ci that was used before suspend. welcome back queen π
cpufreq_get(ci)
Returns the current frequency of the processor ci. A value zero is returned upon failure. π
cpufreq_get_backend(cf)
Upon successful completion, cpufreq_get_backend() returns 0 and fills cf with the data related to the currently used backend. π―
cpufreq_get_state(freq, cfs)
The cpufreq_get_state() function looks for the given frequency from the array of known frequency states. If freq is not found, the closest match is returned. Upon successful completion, the function returns zero and stores the state information to cfs. π
cpufreq_get_state_index(index, cfs)
Stores the frequency state with the given index to cfs, returning zero upon successful completion. π
cpufreq_set(ci, freq)
The cpufreq_set() function sets the frequency of ci to freq. ποΈ
cpufreq_set_all(freq)
Sets freq for all processors in the system. everyone gets the same treatment, periodt π
β οΈ the serious bit, don't skip it: the three functionscpufreq_get(),cpufreq_set(), andcpufreq_set_all()guarantee that the call will be made incurcpu(9). The interface holds amutex(9)while calling the functions. This, and the use ofxcall(9), implies that no memory can be allocated in the backend during the calls. Nor should the functions be called from interrupt context. fr fr, respect this or suffer π
π§ CODE REFERENCES π§
The cpufreq interface is implemented within sys/kern/subr_cpufreq.c. that's the spot π
π SEE ALSO π
pmf(9)xcall(9)- Venkatesh Pallipadi and Alexey Starikovskiy, The Ondemand Governor. Past, Present, and Future, Intel Open Source Technology Center,
http://www.kernel.org/doc/ols/2006/ols2006v2-pages-223-238.pdf, July, 2006, Proceedings of the Linux Symposium.
π HISTORY π
The cpufreq interface first appeared in NetBSD 6.0. a whole era π
π AUTHORS π
Jukka Ruohonen β¨jruohonen@iki.fiβ© β icon, legend, the moment π
π BUGS π
The interface does not support different "governors" and policies. that's the tea, it's just not a feature rn π
β and that's the page! β this was the Debian build of CPUFREQ(9), dated December 1, 2015 β mwah, go set some frequencies bestie, you got this πβ¨
π¦ where this page comes from
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c) 2011 Jukka Ruohonen <jruohonen.iki.fi>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms