Lines Matching +full:cpu +full:- +full:driver
1 // SPDX-License-Identifier: GPL-2.0
3 //! CPU frequency scaling.
9 //! Reference: <https://docs.kernel.org/admin-guide/pm/cpufreq.html>
13 cpu::CpuId,
37 /// Maximum length of CPU frequency driver's name.
43 /// CPU frequency driver flags.
45 /// Driver needs to update internal limits even if frequency remains unchanged.
51 /// Register driver as a thermal cooling device automatically.
54 /// Supports multiple clock domains with per-policy governors in `cpu/cpuN/cpufreq/`.
57 /// Allows post-change notifications outside of the `target()` routine.
60 /// Ensure CPU starts at a valid frequency from the driver's freq-table.
75 /// CPU frequency selection relations.
77 /// CPU frequency selection relations, each optionally marked as "efficient".
89 // Construct from a C-compatible `u32` value.
90 fn new(val: u32) -> Result<Self> { in new()
103 // Convert to a C-compatible `u32` value.
104 fn from(rel: Relation) -> Self { in from()
140 pub unsafe fn from_raw_mut<'a>(ptr: *mut bindings::cpufreq_policy_data) -> &'a mut Self { in from_raw_mut()
150 pub fn as_raw(&self) -> *mut bindings::cpufreq_policy_data { in as_raw()
157 pub fn generic_verify(&self) -> Result { in generic_verify()
180 pub unsafe fn new(index: usize) -> Self { in new()
188 fn from(index: TableIndex) -> Self { in from()
193 /// CPU frequency table.
211 /// fn show_freq(policy: &Policy) -> Result {
234 pub unsafe fn from_raw<'a>(ptr: *const bindings::cpufreq_frequency_table) -> &'a Self { in from_raw()
244 pub fn as_raw(&self) -> *mut bindings::cpufreq_frequency_table { in as_raw()
251 pub fn freq(&self, index: TableIndex) -> Result<Hertz> { in freq()
261 pub fn flags(&self, index: TableIndex) -> u32 { in flags()
269 pub fn data(&self, index: TableIndex) -> u32 { in data()
276 /// CPU frequency table owned and pinned in memory, created from a [`TableBuilder`].
288 fn new(entries: KVec<bindings::cpufreq_frequency_table>) -> Result<Self> { in new()
301 fn as_raw(&self) -> *const bindings::cpufreq_frequency_table { in as_raw()
310 fn deref(&self) -> &Self::Target { in deref()
316 /// CPU frequency table builder.
318 /// This is used by the CPU frequency drivers to build a frequency table dynamically.
322 /// The following example demonstrates how to create a CPU frequency table.
358 pub fn new() -> Self { in new()
365 pub fn add(&mut self, freq: Hertz, flags: u32, driver_data: u32) -> Result { in add()
378 pub fn to_table(mut self) -> Result<TableBox> { in to_table()
386 /// CPU frequency policy.
399 /// The following example demonstrates how to create a CPU frequency table.
410 /// pr_info!("The policy details are: {:?}\n", (policy.cpu(), policy.cur()));
424 pub unsafe fn from_raw<'a>(ptr: *const bindings::cpufreq_policy) -> &'a Self { in from_raw()
439 pub unsafe fn from_raw_mut<'a>(ptr: *mut bindings::cpufreq_policy) -> &'a mut Self { in from_raw_mut()
449 fn as_raw(&self) -> *mut bindings::cpufreq_policy { in as_raw()
455 fn as_ref(&self) -> &bindings::cpufreq_policy { in as_ref()
461 fn as_mut_ref(&mut self) -> &mut bindings::cpufreq_policy { in as_mut_ref()
466 /// Returns the primary CPU for the [`Policy`].
468 pub fn cpu(&self) -> CpuId { in cpu() method
469 // SAFETY: The C API guarantees that `cpu` refers to a valid CPU number. in cpu()
470 unsafe { CpuId::from_u32_unchecked(self.as_ref().cpu) } in cpu()
475 pub fn min(&self) -> Hertz { in min()
481 pub fn set_min(&mut self, min: Hertz) -> &mut Self { in set_min()
488 pub fn max(&self) -> Hertz { in max()
494 pub fn set_max(&mut self, max: Hertz) -> &mut Self { in set_max()
501 pub fn cur(&self) -> Hertz { in cur()
507 pub fn suspend_freq(&self) -> Hertz { in suspend_freq()
513 pub fn set_suspend_freq(&mut self, freq: Hertz) -> &mut Self { in set_suspend_freq()
520 pub fn generic_suspend(&mut self) -> Result { in generic_suspend()
527 pub fn generic_get(&self) -> Result<u32> { in generic_get()
529 Ok(unsafe { bindings::cpufreq_generic_get(u32::from(self.cpu())) }) in generic_get()
542 pub fn cpus(&mut self) -> &mut cpumask::Cpumask { in cpus()
555 pub unsafe fn set_clk(&mut self, dev: &Device, name: Option<&CStr>) -> Result<Clk> { in set_clk()
561 /// Allows / disallows frequency switching code to run on any CPU.
563 pub fn set_dvfs_possible_from_any_cpu(&mut self, val: bool) -> &mut Self { in set_dvfs_possible_from_any_cpu()
570 pub fn fast_switch_possible(&self) -> bool { in fast_switch_possible()
576 pub fn set_fast_switch_possible(&mut self, val: bool) -> &mut Self { in set_fast_switch_possible()
583 pub fn set_transition_latency_ns(&mut self, latency_ns: u32) -> &mut Self { in set_transition_latency_ns()
590 pub fn set_cpuinfo_min_freq(&mut self, min_freq: Hertz) -> &mut Self { in set_cpuinfo_min_freq()
597 pub fn set_cpuinfo_max_freq(&mut self, max_freq: Hertz) -> &mut Self { in set_cpuinfo_max_freq()
605 pub fn set_transition_delay_us(&mut self, transition_delay_us: u32) -> &mut Self { in set_transition_delay_us()
610 /// Returns reference to the CPU frequency [`Table`] for the [`Policy`].
611 pub fn freq_table(&self) -> Result<&Table> { in freq_table()
621 /// Sets the CPU frequency [`Table`] for the [`Policy`].
628 pub unsafe fn set_freq_table(&mut self, table: &Table) -> &mut Self { in set_freq_table()
634 pub fn data<T: ForeignOwnable>(&mut self) -> Option<<T>::Borrowed<'_>> { in data()
643 /// Sets the private data of the [`Policy`] using a foreign-ownable wrapper.
648 fn set_data<T: ForeignOwnable>(&mut self, data: T) -> Result { in set_data()
659 fn clear_data<T: ForeignOwnable>(&mut self) -> Option<T> { in clear_data()
674 /// CPU frequency policy created from a CPU number.
676 /// This struct represents the CPU frequency policy obtained for a specific CPU, providing safe
682 fn from_cpu(cpu: CpuId) -> Result<Self> { in from_cpu()
683 // SAFETY: It is safe to call `cpufreq_cpu_get` for any valid CPU. in from_cpu()
684 let ptr = from_err_ptr(unsafe { bindings::cpufreq_cpu_get(u32::from(cpu)) })?; in from_cpu()
697 fn deref(&self) -> &Self::Target { in deref()
703 fn deref_mut(&mut self) -> &mut Policy { in deref_mut()
715 /// CPU frequency driver.
717 /// Implement this trait to provide a CPU frequency driver and its callbacks.
719 /// Reference: <https://docs.kernel.org/cpu-freq/cpu-drivers.html>
721 pub trait Driver { trait
722 /// Driver's name.
725 /// Driver's flags.
737 /// Driver's `init` callback.
738 fn init(policy: &mut Policy) -> Result<Self::PData>; in init()
740 /// Driver's `exit` callback.
741 fn exit(_policy: &mut Policy, _data: Option<Self::PData>) -> Result { in exit()
745 /// Driver's `online` callback.
746 fn online(_policy: &mut Policy) -> Result { in online()
750 /// Driver's `offline` callback.
751 fn offline(_policy: &mut Policy) -> Result { in offline()
755 /// Driver's `suspend` callback.
756 fn suspend(_policy: &mut Policy) -> Result { in suspend()
760 /// Driver's `resume` callback.
761 fn resume(_policy: &mut Policy) -> Result { in resume()
765 /// Driver's `ready` callback.
770 /// Driver's `verify` callback.
771 fn verify(data: &mut PolicyData) -> Result; in verify()
773 /// Driver's `setpolicy` callback.
774 fn setpolicy(_policy: &mut Policy) -> Result { in setpolicy()
778 /// Driver's `target` callback.
779 fn target(_policy: &mut Policy, _target_freq: u32, _relation: Relation) -> Result { in target()
783 /// Driver's `target_index` callback.
784 fn target_index(_policy: &mut Policy, _index: TableIndex) -> Result { in target_index()
788 /// Driver's `fast_switch` callback.
789 fn fast_switch(_policy: &mut Policy, _target_freq: u32) -> u32 { in fast_switch()
793 /// Driver's `adjust_perf` callback.
798 /// Driver's `get_intermediate` callback.
799 fn get_intermediate(_policy: &mut Policy, _index: TableIndex) -> u32 { in get_intermediate()
803 /// Driver's `target_intermediate` callback.
804 fn target_intermediate(_policy: &mut Policy, _index: TableIndex) -> Result { in target_intermediate()
808 /// Driver's `get` callback.
809 fn get(_policy: &mut Policy) -> Result<u32> { in get()
813 /// Driver's `update_limits` callback.
818 /// Driver's `bios_limit` callback.
819 fn bios_limit(_policy: &mut Policy, _limit: &mut u32) -> Result { in bios_limit()
823 /// Driver's `set_boost` callback.
824 fn set_boost(_policy: &mut Policy, _state: i32) -> Result { in set_boost()
828 /// Driver's `register_em` callback.
834 /// CPU frequency driver Registration.
838 /// The following example demonstrates how to register a cpufreq driver.
855 /// impl cpufreq::Driver for SampleDriver {
856 /// const NAME: &'static CStr = c_str!("cpufreq-sample");
862 /// fn init(policy: &mut cpufreq::Policy) -> Result<Self::PData> {
867 /// fn exit(_policy: &mut cpufreq::Policy, _data: Option<Self::PData>) -> Result {
871 /// fn suspend(policy: &mut cpufreq::Policy) -> Result {
875 /// fn verify(data: &mut cpufreq::PolicyData) -> Result {
879 /// fn target_index(policy: &mut cpufreq::Policy, index: cpufreq::TableIndex) -> Result {
880 /// // Update CPU frequency
884 /// fn get(policy: &mut cpufreq::Policy) -> Result<u32> {
889 /// impl platform::Driver for SampleDriver {
896 /// ) -> Result<Pin<KBox<Self>>> {
903 pub struct Registration<T: Driver>(KBox<UnsafeCell<bindings::cpufreq_driver>>, PhantomData<T>);
907 unsafe impl<T: Driver> Sync for Registration<T> {}
912 unsafe impl<T: Driver> Send for Registration<T> {}
914 impl<T: Driver> Registration<T> {
1018 const fn copy_name(name: &'static CStr) -> [c_char; CPUFREQ_NAME_LEN] { in copy_name()
1033 /// Registers a CPU frequency driver with the cpufreq core.
1034 pub fn new() -> Result<Self> { in new()
1049 pub fn new_foreign_owned(dev: &Device<Bound>) -> Result in new_foreign_owned()
1057 /// CPU frequency driver callbacks.
1058 impl<T: Driver> Registration<T> {
1059 /// Driver's `init` callback.
1063 /// - This function may only be called from the cpufreq C infrastructure.
1064 /// - The pointer arguments must be valid pointers.
1065 unsafe extern "C" fn init_callback(ptr: *mut bindings::cpufreq_policy) -> c_int { in init_callback()
1077 /// Driver's `exit` callback.
1081 /// - This function may only be called from the cpufreq C infrastructure.
1082 /// - The pointer arguments must be valid pointers.
1092 /// Driver's `online` callback.
1096 /// - This function may only be called from the cpufreq C infrastructure.
1097 /// - The pointer arguments must be valid pointers.
1098 unsafe extern "C" fn online_callback(ptr: *mut bindings::cpufreq_policy) -> c_int { in online_callback()
1107 /// Driver's `offline` callback.
1111 /// - This function may only be called from the cpufreq C infrastructure.
1112 /// - The pointer arguments must be valid pointers.
1113 unsafe extern "C" fn offline_callback(ptr: *mut bindings::cpufreq_policy) -> c_int { in offline_callback()
1122 /// Driver's `suspend` callback.
1126 /// - This function may only be called from the cpufreq C infrastructure.
1127 /// - The pointer arguments must be valid pointers.
1128 unsafe extern "C" fn suspend_callback(ptr: *mut bindings::cpufreq_policy) -> c_int { in suspend_callback()
1137 /// Driver's `resume` callback.
1141 /// - This function may only be called from the cpufreq C infrastructure.
1142 /// - The pointer arguments must be valid pointers.
1143 unsafe extern "C" fn resume_callback(ptr: *mut bindings::cpufreq_policy) -> c_int { in resume_callback()
1152 /// Driver's `ready` callback.
1156 /// - This function may only be called from the cpufreq C infrastructure.
1157 /// - The pointer arguments must be valid pointers.
1165 /// Driver's `verify` callback.
1169 /// - This function may only be called from the cpufreq C infrastructure.
1170 /// - The pointer arguments must be valid pointers.
1171 unsafe extern "C" fn verify_callback(ptr: *mut bindings::cpufreq_policy_data) -> c_int { in verify_callback()
1180 /// Driver's `setpolicy` callback.
1184 /// - This function may only be called from the cpufreq C infrastructure.
1185 /// - The pointer arguments must be valid pointers.
1186 unsafe extern "C" fn setpolicy_callback(ptr: *mut bindings::cpufreq_policy) -> c_int { in setpolicy_callback()
1195 /// Driver's `target` callback.
1199 /// - This function may only be called from the cpufreq C infrastructure.
1200 /// - The pointer arguments must be valid pointers.
1205 ) -> c_int { in target_callback()
1214 /// Driver's `target_index` callback.
1218 /// - This function may only be called from the cpufreq C infrastructure.
1219 /// - The pointer arguments must be valid pointers.
1223 ) -> c_int { in target_index_callback()
1237 /// Driver's `fast_switch` callback.
1241 /// - This function may only be called from the cpufreq C infrastructure.
1242 /// - The pointer arguments must be valid pointers.
1246 ) -> c_uint { in fast_switch_callback()
1253 /// Driver's `adjust_perf` callback.
1257 /// - This function may only be called from the cpufreq C infrastructure.
1259 cpu: c_uint, in adjust_perf_callback()
1264 // SAFETY: The C API guarantees that `cpu` refers to a valid CPU number. in adjust_perf_callback()
1265 let cpu_id = unsafe { CpuId::from_u32_unchecked(cpu) }; in adjust_perf_callback()
1272 /// Driver's `get_intermediate` callback.
1276 /// - This function may only be called from the cpufreq C infrastructure.
1277 /// - The pointer arguments must be valid pointers.
1281 ) -> c_uint { in get_intermediate_callback()
1293 /// Driver's `target_intermediate` callback.
1297 /// - This function may only be called from the cpufreq C infrastructure.
1298 /// - The pointer arguments must be valid pointers.
1302 ) -> c_int { in target_intermediate_callback()
1316 /// Driver's `get` callback.
1320 /// - This function may only be called from the cpufreq C infrastructure.
1321 unsafe extern "C" fn get_callback(cpu: c_uint) -> c_uint { in get_callback()
1322 // SAFETY: The C API guarantees that `cpu` refers to a valid CPU number. in get_callback()
1323 let cpu_id = unsafe { CpuId::from_u32_unchecked(cpu) }; in get_callback()
1328 /// Driver's `update_limit` callback.
1332 /// - This function may only be called from the cpufreq C infrastructure.
1333 /// - The pointer arguments must be valid pointers.
1341 /// Driver's `bios_limit` callback.
1345 /// - This function may only be called from the cpufreq C infrastructure.
1346 /// - The pointer arguments must be valid pointers.
1347 unsafe extern "C" fn bios_limit_callback(cpu: c_int, limit: *mut c_uint) -> c_int { in bios_limit_callback()
1348 // SAFETY: The C API guarantees that `cpu` refers to a valid CPU number. in bios_limit_callback()
1349 let cpu_id = unsafe { CpuId::from_i32_unchecked(cpu) }; in bios_limit_callback()
1359 /// Driver's `set_boost` callback.
1363 /// - This function may only be called from the cpufreq C infrastructure.
1364 /// - The pointer arguments must be valid pointers.
1368 ) -> c_int { in set_boost_callback()
1377 /// Driver's `register_em` callback.
1381 /// - This function may only be called from the cpufreq C infrastructure.
1382 /// - The pointer arguments must be valid pointers.
1391 impl<T: Driver> Drop for Registration<T> {