2024-06-07 09:18:52

by Viresh Kumar

[permalink] [raw]
Subject: [RFC PATCH V2 0/8] Rust bindings for cpufreq and OPP core + sample driver

Hello,

This RFC adds initial rust bindings for two subsystems, cpufreq and operating
performance points (OPP). The bindings are provided for most of the interface
these subsystems expose.

This series also provides a sample cpufreq driver rcpufreq-dt, which is a
duplicate of the merged cpufreq-dt driver (A generic platform agnostic device
tree based cpufreq driver) used on most of the ARM platforms.

This is tested with the help of QEMU for now and frequency transitions and
configurations work as expected. No performance measurement is done as of now
with this.

These patches (along with few other dependencies) are pushed here for anyone to
give them a try:

git://git.kernel.org/pub/scm/linux/kernel/git/vireshk/linux.git rust/cpufreq-dt


This depends on basic bindings for few other modules: device/driver, platform
driver, OF, clk, and cpumask. I am not looking to upstream a full fledged
support for them yet.

Based staging/rust-device from the Rust tree (which is based over v6.10-rc1).

V1->V2:
- Create and use separate bindings for OF, clk, cpumask, etc (not included in
this patchset but pushed to the above branch). This helped removing direct
calls from the driver.
- Fix wrong usage of Pinning + Vec.
- Use Token for OPP Config.
- Use Opaque, transparent and Aref for few structures.
- Broken down into smaller patches to make it easy for reviewers.
- Based over staging/rust-device.

Thanks.

Viresh Kumar (8):
rust: Add initial bindings for OPP framework
rust: Extend OPP bindings for the OPP table
rust: Extend OPP bindings for the configuration options
rust: Add initial bindings for cpufreq framework
rust: Extend cpufreq bindings for policy and driver ops
rust: Extend cpufreq bindings for driver registration
rust: Extend OPP bindings with CPU frequency table
cpufreq: Add Rust based cpufreq-dt driver

drivers/cpufreq/Kconfig | 12 +
drivers/cpufreq/Makefile | 1 +
drivers/cpufreq/rcpufreq_dt.rs | 229 +++++++
rust/bindings/bindings_helper.h | 2 +
rust/helpers.c | 15 +
rust/kernel/cpufreq.rs | 1070 +++++++++++++++++++++++++++++++
rust/kernel/lib.rs | 4 +
rust/kernel/opp.rs | 886 +++++++++++++++++++++++++
8 files changed, 2219 insertions(+)
create mode 100644 drivers/cpufreq/rcpufreq_dt.rs
create mode 100644 rust/kernel/cpufreq.rs
create mode 100644 rust/kernel/opp.rs

--
2.31.1.272.g89b43f80a514



2024-06-07 09:22:52

by Viresh Kumar

[permalink] [raw]
Subject: [RFC PATCH V2 8/8] cpufreq: Add Rust based cpufreq-dt driver

This commit adds a Rust based cpufreq-dt driver, which covers most of
the functionality of the existing C based driver. Only a handful of
things are left, like fetching platform data from cpufreq-dt-platdev.c.

This is tested with the help of QEMU for now and switching of
frequencies work as expected.

Signed-off-by: Viresh Kumar <[email protected]>
---
drivers/cpufreq/Kconfig | 12 ++
drivers/cpufreq/Makefile | 1 +
drivers/cpufreq/rcpufreq_dt.rs | 229 +++++++++++++++++++++++++++++++++
3 files changed, 242 insertions(+)
create mode 100644 drivers/cpufreq/rcpufreq_dt.rs

diff --git a/drivers/cpufreq/Kconfig b/drivers/cpufreq/Kconfig
index 94e55c40970a..eb9359bd3c5c 100644
--- a/drivers/cpufreq/Kconfig
+++ b/drivers/cpufreq/Kconfig
@@ -217,6 +217,18 @@ config CPUFREQ_DT

If in doubt, say N.

+config CPUFREQ_DT_RUST
+ tristate "Rust based Generic DT based cpufreq driver"
+ depends on HAVE_CLK && OF && RUST
+ select CPUFREQ_DT_PLATDEV
+ select PM_OPP
+ help
+ This adds a Rust based generic DT based cpufreq driver for frequency
+ management. It supports both uniprocessor (UP) and symmetric
+ multiprocessor (SMP) systems.
+
+ If in doubt, say N.
+
config CPUFREQ_DT_PLATDEV
tristate "Generic DT based cpufreq platdev driver"
depends on OF
diff --git a/drivers/cpufreq/Makefile b/drivers/cpufreq/Makefile
index 8d141c71b016..4981d908b803 100644
--- a/drivers/cpufreq/Makefile
+++ b/drivers/cpufreq/Makefile
@@ -15,6 +15,7 @@ obj-$(CONFIG_CPU_FREQ_GOV_COMMON) += cpufreq_governor.o
obj-$(CONFIG_CPU_FREQ_GOV_ATTR_SET) += cpufreq_governor_attr_set.o

obj-$(CONFIG_CPUFREQ_DT) += cpufreq-dt.o
+obj-$(CONFIG_CPUFREQ_DT_RUST) += rcpufreq_dt.o
obj-$(CONFIG_CPUFREQ_DT_PLATDEV) += cpufreq-dt-platdev.o

# Traces
diff --git a/drivers/cpufreq/rcpufreq_dt.rs b/drivers/cpufreq/rcpufreq_dt.rs
new file mode 100644
index 000000000000..b016771e6180
--- /dev/null
+++ b/drivers/cpufreq/rcpufreq_dt.rs
@@ -0,0 +1,229 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Rust based implementation of the cpufreq-dt driver.
+
+use core::format_args;
+
+use kernel::{
+ b_str, c_str, clk, cpufreq, define_of_id_table,
+ device::{self, Device},
+ error::code::*,
+ fmt,
+ macros::vtable,
+ module_platform_driver, of, opp, platform,
+ prelude::*,
+ str::CString,
+ sync::Arc,
+ types::ARef,
+};
+
+// Finds exact supply name from the OF node.
+fn find_supply_name_exact(np: &ARef<of::DeviceNode>, name: &str) -> Option<CString> {
+ let name_cstr = CString::try_from_fmt(fmt!("{}-supply", name)).ok()?;
+
+ np.find_property(&name_cstr).ok()?;
+ CString::try_from_fmt(fmt!("{}", name)).ok()
+}
+
+// Finds supply name for the CPU from DT.
+fn find_supply_names(dev: ARef<Device>, cpu: u32) -> Option<Vec<CString>> {
+ let np = of::DeviceNode::from_dev(dev).ok()?;
+
+ // Try "cpu0" for older DTs.
+ let name = match cpu {
+ 0 => find_supply_name_exact(&np, "cpu0"),
+ _ => None,
+ }
+ .or(find_supply_name_exact(&np, "cpu"))?;
+
+ let mut list = Vec::with_capacity(1, GFP_KERNEL).ok()?;
+ list.push(name, GFP_KERNEL).ok()?;
+
+ Some(list)
+}
+
+// Represents the cpufreq dt device.
+struct CPUFreqDTDevice {
+ opp_table: opp::Table,
+ freq_table: opp::FreqTable,
+ #[allow(dead_code)]
+ token: Option<opp::ConfigToken>,
+ #[allow(dead_code)]
+ clk: clk::Clk,
+}
+
+#[vtable]
+impl opp::ConfigOps for CPUFreqDTDevice {}
+
+#[vtable]
+impl cpufreq::DriverOps for CPUFreqDTDevice {
+ type Data = ();
+ type PData = Arc<Self>;
+
+ fn init(policy: &mut cpufreq::Policy) -> Result<Self::PData> {
+ let cpu = policy.cpu();
+ let dev = Device::from_cpu(cpu)?;
+
+ policy.set_cpus(cpu);
+
+ let token = match find_supply_names(dev.clone(), cpu) {
+ Some(names) => Some(
+ opp::Config::<Self>::new()
+ .set_regulator_names(names)?
+ .set(dev.clone())?,
+ ),
+ _ => None,
+ };
+
+ // Get OPP-sharing information from "operating-points-v2" bindings.
+ let fallback = match opp::Table::of_sharing_cpus(dev.clone(), policy.cpus()) {
+ Ok(_) => false,
+ Err(e) => {
+ if e != ENOENT {
+ return Err(e);
+ }
+
+ // "operating-points-v2" not supported. If the platform hasn't
+ // set sharing CPUs, fallback to all CPUs share the `Policy`
+ // for backward compatibility.
+ opp::Table::sharing_cpus(dev.clone(), policy.cpus()).is_err()
+ }
+ };
+
+ // Initialize OPP tables for all policy cpus.
+ //
+ // For platforms not using "operating-points-v2" bindings, we do this
+ // before updating policy cpus. Otherwise, we will end up creating
+ // duplicate OPPs for the CPUs.
+ //
+ // OPPs might be populated at runtime, don't fail for error here unless
+ // it is -EPROBE_DEFER.
+ let mut opp_table = match opp::Table::from_of_cpumask(dev.clone(), policy.cpus()) {
+ Ok(table) => table,
+ Err(e) => {
+ if e == EPROBE_DEFER {
+ return Err(e);
+ }
+
+ // The table is added dynamically ?
+ opp::Table::from_dev(dev.clone())?
+ }
+ };
+
+ // The OPP table must be initialized, statically or dynamically, by this point.
+ opp_table.opp_count()?;
+
+ // Set sharing cpus for fallback scenario.
+ if fallback {
+ policy.set_all_cpus();
+ opp_table.set_sharing_cpus(policy.cpus())?;
+ }
+
+ let mut transition_latency = opp_table.max_transition_latency() as u32;
+ if transition_latency == 0 {
+ transition_latency = cpufreq::ETERNAL_LATENCY;
+ }
+
+ let freq_table = opp_table.to_cpufreq_table()?;
+ let clk = policy
+ .set_freq_table(freq_table.table())
+ .set_dvfs_possible_from_any_cpu()
+ .set_suspend_freq((opp_table.suspend_freq() / 1000) as u32)
+ .set_transition_latency(transition_latency)
+ .set_clk(dev, None)?;
+
+ Ok(Arc::new(
+ CPUFreqDTDevice {
+ opp_table,
+ token,
+ freq_table,
+ clk,
+ },
+ GFP_KERNEL,
+ )?)
+ }
+
+ fn exit(_policy: &mut cpufreq::Policy, _data: Option<Self::PData>) -> Result<()> {
+ Ok(())
+ }
+
+ fn online(_policy: &mut cpufreq::Policy) -> Result<()> {
+ // We did light-weight tear down earlier, nothing to do here.
+ Ok(())
+ }
+
+ fn offline(_policy: &mut cpufreq::Policy) -> Result<()> {
+ // Preserve policy->data and don't free resources on light-weight
+ // tear down.
+ Ok(())
+ }
+
+ fn suspend(policy: &mut cpufreq::Policy) -> Result<()> {
+ policy.generic_suspend()
+ }
+
+ fn verify(data: &mut cpufreq::PolicyData) -> Result<()> {
+ data.generic_verify()
+ }
+
+ fn target_index(policy: &mut cpufreq::Policy, index: u32) -> Result<()> {
+ let data = match policy.data::<Self::PData>() {
+ Some(data) => data,
+ None => return Err(ENOENT),
+ };
+
+ let freq = data.freq_table.freq(index.try_into().unwrap())? as u64;
+ data.opp_table.set_rate(freq * 1000)
+ }
+
+ fn get(policy: &mut cpufreq::Policy) -> Result<u32> {
+ policy.generic_get()
+ }
+
+ fn set_boost(_policy: &mut cpufreq::Policy, _state: i32) -> Result<()> {
+ Ok(())
+ }
+
+ fn register_em(policy: &mut cpufreq::Policy) {
+ policy.register_em_opp()
+ }
+}
+
+type DeviceData = device::Data<cpufreq::Registration<CPUFreqDTDevice>, ()>;
+
+struct CPUFreqDTDriver;
+
+impl platform::Driver for CPUFreqDTDriver {
+ type Data = Arc<DeviceData>;
+
+ define_of_id_table! {(), [
+ (of::DeviceId(b_str!("operating-points-v2")), None),
+ ]}
+
+ fn probe(_dev: &mut platform::Device, _id_info: Option<&Self::IdInfo>) -> Result<Self::Data> {
+ let data = Arc::<DeviceData>::from(kernel::new_device_data!(
+ cpufreq::Registration::new(),
+ (),
+ "CPUFreqDT::Registration"
+ )?);
+ let flags = cpufreq::flags::NEED_INITIAL_FREQ_CHECK | cpufreq::flags::IS_COOLING_DEV;
+ let boost = true;
+
+ data.registrations()
+ .ok_or(ENXIO)?
+ .as_pinned_mut()
+ .register(c_str!("cpufreq-dt"), (), flags, boost)?;
+
+ pr_info!("CPUFreq DT driver registered\n");
+
+ Ok(data)
+ }
+}
+
+module_platform_driver! {
+ type: CPUFreqDTDriver,
+ name: "cpufreq_dt",
+ author: "Viresh Kumar <[email protected]>",
+ description: "Generic CPUFreq DT driver",
+ license: "GPL v2",
+}
--
2.31.1.272.g89b43f80a514


2024-06-07 09:23:35

by Viresh Kumar

[permalink] [raw]
Subject: [RFC PATCH V2 6/8] rust: Extend cpufreq bindings for driver registration

This extends the cpufreq bindings with bindings for registering a
driver.

Signed-off-by: Viresh Kumar <[email protected]>
---
rust/kernel/cpufreq.rs | 492 ++++++++++++++++++++++++++++++++++++++++-
1 file changed, 490 insertions(+), 2 deletions(-)

diff --git a/rust/kernel/cpufreq.rs b/rust/kernel/cpufreq.rs
index d5679272e40b..76eba63b6716 100644
--- a/rust/kernel/cpufreq.rs
+++ b/rust/kernel/cpufreq.rs
@@ -9,14 +9,16 @@
use crate::{
bindings, clk, cpumask,
device::Device,
- error::{code::*, from_err_ptr, to_result, Result, VTABLE_DEFAULT_ERROR},
+ error::{code::*, from_err_ptr, from_result, to_result, Result, VTABLE_DEFAULT_ERROR},
prelude::*,
types::{ARef, ForeignOwnable},
};

use core::{
+ cell::UnsafeCell,
+ marker::{PhantomData, PhantomPinned},
pin::Pin,
- ptr::self,
+ ptr::{self, addr_of_mut},
};

use macros::vtable;
@@ -580,3 +582,489 @@ fn register_em(_policy: &mut Policy) {
kernel::build_error(VTABLE_DEFAULT_ERROR)
}
}
+
+/// Registration of a cpufreq driver.
+pub struct Registration<T: DriverOps> {
+ registered: bool,
+ drv: UnsafeCell<bindings::cpufreq_driver>,
+ _p: PhantomData<T>,
+ _pin: PhantomPinned,
+}
+
+// SAFETY: `Registration` doesn't offer any methods or access to fields when shared between threads
+// or CPUs, so it is safe to share it.
+unsafe impl<T: DriverOps> Sync for Registration<T> {}
+
+// SAFETY: Registration with and unregistration from the cpufreq subsystem can happen from any thread.
+// Additionally, `T::Data` (which is dropped during unregistration) is `Send`, so it is okay to move
+// `Registration` to different threads.
+#[allow(clippy::non_send_fields_in_send_ty)]
+unsafe impl<T: DriverOps> Send for Registration<T> {}
+
+impl<T: DriverOps> Default for Registration<T> {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl<T: DriverOps> Registration<T> {
+ /// Creates new [`Registration`] but does not register it yet.
+ ///
+ /// It is allowed to move.
+ pub fn new() -> Self {
+ Self {
+ registered: false,
+ drv: UnsafeCell::new(bindings::cpufreq_driver::default()),
+ _pin: PhantomPinned,
+ _p: PhantomData,
+ }
+ }
+
+ /// Registers a cpufreq driver with the rest of the kernel.
+ pub fn register(
+ self: Pin<&mut Self>,
+ name: &'static CStr,
+ data: T::Data,
+ flags: u16,
+ boost: bool,
+ ) -> Result {
+ // SAFETY: We never move out of `this`.
+ let this = unsafe { self.get_unchecked_mut() };
+
+ if this.registered {
+ return Err(EINVAL);
+ }
+
+ let drv = this.drv.get_mut();
+
+ // Account for the trailing null character.
+ let len = name.len() + 1;
+ if len > drv.name.len() {
+ return Err(EINVAL);
+ };
+
+ // SAFETY: `name` is a valid Cstr, and we are copying it to an array of equal or larger
+ // size.
+ let name = unsafe { &*(name.as_bytes_with_nul() as *const [u8] as *const [i8]) };
+ drv.name[..len].copy_from_slice(name);
+
+ drv.boost_enabled = boost;
+ drv.flags = flags;
+
+ // Allocate an array of 3 pointers to be passed to the C code.
+ let mut attr = Box::new([ptr::null_mut(); 3], GFP_KERNEL)?;
+ let mut next = 0;
+
+ // SAFETY: The C code returns a valid pointer here, which is again passed to the C code in
+ // an array.
+ attr[next] =
+ unsafe { addr_of_mut!(bindings::cpufreq_freq_attr_scaling_available_freqs) as *mut _ };
+ next += 1;
+
+ if boost {
+ // SAFETY: The C code returns a valid pointer here, which is again passed to the C code
+ // in an array.
+ attr[next] =
+ unsafe { addr_of_mut!(bindings::cpufreq_freq_attr_scaling_boost_freqs) as *mut _ };
+ next += 1;
+ }
+ attr[next] = ptr::null_mut();
+
+ // Pass the ownership of the memory block to the C code. This will be freed when
+ // the [`Registration`] object goes out of scope.
+ drv.attr = Box::leak(attr) as *mut _;
+
+ // Initialize mandatory callbacks.
+ drv.init = Some(Self::init_callback);
+ drv.verify = Some(Self::verify_callback);
+
+ // Initialize optional callbacks.
+ drv.setpolicy = if T::HAS_SETPOLICY {
+ Some(Self::setpolicy_callback)
+ } else {
+ None
+ };
+ drv.target = if T::HAS_TARGET {
+ Some(Self::target_callback)
+ } else {
+ None
+ };
+ drv.target_index = if T::HAS_TARGET_INDEX {
+ Some(Self::target_index_callback)
+ } else {
+ None
+ };
+ drv.fast_switch = if T::HAS_FAST_SWITCH {
+ Some(Self::fast_switch_callback)
+ } else {
+ None
+ };
+ drv.adjust_perf = if T::HAS_ADJUST_PERF {
+ Some(Self::adjust_perf_callback)
+ } else {
+ None
+ };
+ drv.get_intermediate = if T::HAS_GET_INTERMEDIATE {
+ Some(Self::get_intermediate_callback)
+ } else {
+ None
+ };
+ drv.target_intermediate = if T::HAS_TARGET_INTERMEDIATE {
+ Some(Self::target_intermediate_callback)
+ } else {
+ None
+ };
+ drv.get = if T::HAS_GET {
+ Some(Self::get_callback)
+ } else {
+ None
+ };
+ drv.update_limits = if T::HAS_UPDATE_LIMITS {
+ Some(Self::update_limits_callback)
+ } else {
+ None
+ };
+ drv.bios_limit = if T::HAS_BIOS_LIMIT {
+ Some(Self::bios_limit_callback)
+ } else {
+ None
+ };
+ drv.online = if T::HAS_ONLINE {
+ Some(Self::online_callback)
+ } else {
+ None
+ };
+ drv.offline = if T::HAS_OFFLINE {
+ Some(Self::offline_callback)
+ } else {
+ None
+ };
+ drv.exit = if T::HAS_EXIT {
+ Some(Self::exit_callback)
+ } else {
+ None
+ };
+ drv.suspend = if T::HAS_SUSPEND {
+ Some(Self::suspend_callback)
+ } else {
+ None
+ };
+ drv.resume = if T::HAS_RESUME {
+ Some(Self::resume_callback)
+ } else {
+ None
+ };
+ drv.ready = if T::HAS_READY {
+ Some(Self::ready_callback)
+ } else {
+ None
+ };
+ drv.set_boost = if T::HAS_SET_BOOST {
+ Some(Self::set_boost_callback)
+ } else {
+ None
+ };
+ drv.register_em = if T::HAS_REGISTER_EM {
+ Some(Self::register_em_callback)
+ } else {
+ None
+ };
+
+ // Set driver data before registering the driver, as the cpufreq core may call few
+ // callbacks before `cpufreq_register_driver()` returns.
+ this.set_data(data)?;
+
+ // SAFETY: It is safe to register the driver with the cpufreq core in the C code.
+ to_result(unsafe { bindings::cpufreq_register_driver(this.drv.get_mut()) })?;
+
+ this.registered = true;
+ Ok(())
+ }
+
+ /// Returns the previous set data for a cpufreq driver.
+ pub fn data<D: ForeignOwnable>() -> Option<<D>::Borrowed<'static>> {
+ // SAFETY: The driver data is earlier set by us from [`set_data()`].
+ let data = unsafe { bindings::cpufreq_get_driver_data() };
+ if data.is_null() {
+ None
+ } else {
+ // SAFETY: The driver data is earlier set by us from [`set_data()`].
+ Some(unsafe { D::borrow(data) })
+ }
+ }
+
+ // Sets the data for a cpufreq driver.
+ fn set_data(&mut self, data: T::Data) -> Result<()> {
+ let drv = self.drv.get_mut();
+
+ if drv.driver_data.is_null() {
+ // Pass the ownership of the data to the foreign interface.
+ drv.driver_data = <T::Data as ForeignOwnable>::into_foreign(data) as _;
+ Ok(())
+ } else {
+ Err(EBUSY)
+ }
+ }
+
+ // Clears and returns the data for a cpufreq driver.
+ fn clear_data(&mut self) -> Option<T::Data> {
+ let drv = self.drv.get_mut();
+
+ if drv.driver_data.is_null() {
+ None
+ } else {
+ // SAFETY: By the type invariants, we know that `self` owns a reference, so it is safe to
+ // relinquish it now.
+ let data = Some(unsafe { <T::Data as ForeignOwnable>::from_foreign(drv.driver_data) });
+ drv.driver_data = ptr::null_mut();
+ data
+ }
+ }
+}
+
+// cpufreq driver callbacks.
+impl<T: DriverOps> Registration<T> {
+ // Policy's init callback.
+ extern "C" fn init_callback(ptr: *mut bindings::cpufreq_policy) -> core::ffi::c_int {
+ from_result(|| {
+ // SAFETY: `ptr` is valid by the contract with the C code. `policy` is alive only for the
+ // duration of this call, so it is guaranteed to remain alive for the lifetime of
+ // `ptr`.
+ let mut policy = unsafe { Policy::from_ptr(ptr) };
+
+ let data = T::init(&mut policy)?;
+ policy.set_data(data)?;
+ Ok(0)
+ })
+ }
+
+ // Policy's exit callback.
+ extern "C" fn exit_callback(ptr: *mut bindings::cpufreq_policy) -> core::ffi::c_int {
+ from_result(|| {
+ // SAFETY: `ptr` is valid by the contract with the C code. `policy` is alive only for the
+ // duration of this call, so it is guaranteed to remain alive for the lifetime of
+ // `ptr`.
+ let mut policy = unsafe { Policy::from_ptr(ptr) };
+
+ let data = policy.clear_data();
+ T::exit(&mut policy, data).map(|_| 0)
+ })
+ }
+
+ // Policy's online callback.
+ extern "C" fn online_callback(ptr: *mut bindings::cpufreq_policy) -> core::ffi::c_int {
+ from_result(|| {
+ // SAFETY: `ptr` is valid by the contract with the C code. `policy` is alive only for the
+ // duration of this call, so it is guaranteed to remain alive for the lifetime of
+ // `ptr`.
+ let mut policy = unsafe { Policy::from_ptr(ptr) };
+ T::online(&mut policy).map(|_| 0)
+ })
+ }
+
+ // Policy's offline callback.
+ extern "C" fn offline_callback(ptr: *mut bindings::cpufreq_policy) -> core::ffi::c_int {
+ from_result(|| {
+ // SAFETY: `ptr` is valid by the contract with the C code. `policy` is alive only for the
+ // duration of this call, so it is guaranteed to remain alive for the lifetime of
+ // `ptr`.
+ let mut policy = unsafe { Policy::from_ptr(ptr) };
+ T::offline(&mut policy).map(|_| 0)
+ })
+ }
+
+ // Policy's suspend callback.
+ extern "C" fn suspend_callback(ptr: *mut bindings::cpufreq_policy) -> core::ffi::c_int {
+ from_result(|| {
+ // SAFETY: `ptr` is valid by the contract with the C code. `policy` is alive only for the
+ // duration of this call, so it is guaranteed to remain alive for the lifetime of
+ // `ptr`.
+ let mut policy = unsafe { Policy::from_ptr(ptr) };
+ T::suspend(&mut policy).map(|_| 0)
+ })
+ }
+
+ // Policy's resume callback.
+ extern "C" fn resume_callback(ptr: *mut bindings::cpufreq_policy) -> core::ffi::c_int {
+ from_result(|| {
+ // SAFETY: `ptr` is valid by the contract with the C code. `policy` is alive only for the
+ // duration of this call, so it is guaranteed to remain alive for the lifetime of
+ // `ptr`.
+ let mut policy = unsafe { Policy::from_ptr(ptr) };
+ T::resume(&mut policy).map(|_| 0)
+ })
+ }
+
+ // Policy's ready callback.
+ extern "C" fn ready_callback(ptr: *mut bindings::cpufreq_policy) {
+ // SAFETY: `ptr` is valid by the contract with the C code. `policy` is alive only for the
+ // duration of this call, so it is guaranteed to remain alive for the lifetime of
+ // `ptr`.
+ let mut policy = unsafe { Policy::from_ptr(ptr) };
+ T::ready(&mut policy);
+ }
+
+ // Policy's verify callback.
+ extern "C" fn verify_callback(ptr: *mut bindings::cpufreq_policy_data) -> core::ffi::c_int {
+ from_result(|| {
+ // SAFETY: `ptr` is valid by the contract with the C code. `policy` is alive only for the
+ // duration of this call, so it is guaranteed to remain alive for the lifetime of
+ // `ptr`.
+ let mut data = unsafe { PolicyData::from_ptr(ptr) };
+ T::verify(&mut data).map(|_| 0)
+ })
+ }
+
+ // Policy's setpolicy callback.
+ extern "C" fn setpolicy_callback(ptr: *mut bindings::cpufreq_policy) -> core::ffi::c_int {
+ from_result(|| {
+ // SAFETY: `ptr` is valid by the contract with the C code. `policy` is alive only for the
+ // duration of this call, so it is guaranteed to remain alive for the lifetime of
+ // `ptr`.
+ let mut policy = unsafe { Policy::from_ptr(ptr) };
+ T::setpolicy(&mut policy).map(|_| 0)
+ })
+ }
+
+ // Policy's target callback.
+ extern "C" fn target_callback(
+ ptr: *mut bindings::cpufreq_policy,
+ target_freq: u32,
+ relation: u32,
+ ) -> core::ffi::c_int {
+ from_result(|| {
+ // SAFETY: `ptr` is valid by the contract with the C code. `policy` is alive only for the
+ // duration of this call, so it is guaranteed to remain alive for the lifetime of
+ // `ptr`.
+ let mut policy = unsafe { Policy::from_ptr(ptr) };
+ T::target(&mut policy, target_freq, Relation::new(relation)?).map(|_| 0)
+ })
+ }
+
+ // Policy's target_index callback.
+ extern "C" fn target_index_callback(
+ ptr: *mut bindings::cpufreq_policy,
+ index: u32,
+ ) -> core::ffi::c_int {
+ from_result(|| {
+ // SAFETY: `ptr` is valid by the contract with the C code. `policy` is alive only for the
+ // duration of this call, so it is guaranteed to remain alive for the lifetime of
+ // `ptr`.
+ let mut policy = unsafe { Policy::from_ptr(ptr) };
+ T::target_index(&mut policy, index).map(|_| 0)
+ })
+ }
+
+ // Policy's fast_switch callback.
+ extern "C" fn fast_switch_callback(
+ ptr: *mut bindings::cpufreq_policy,
+ target_freq: u32,
+ ) -> core::ffi::c_uint {
+ // SAFETY: `ptr` is valid by the contract with the C code. `policy` is alive only for the
+ // duration of this call, so it is guaranteed to remain alive for the lifetime of
+ // `ptr`.
+ let mut policy = unsafe { Policy::from_ptr(ptr) };
+ T::fast_switch(&mut policy, target_freq)
+ }
+
+ // Policy's adjust_perf callback.
+ extern "C" fn adjust_perf_callback(cpu: u32, min_perf: u64, target_perf: u64, capacity: u64) {
+ if let Some(mut policy) = Policy::from_cpu(cpu).ok() {
+ T::adjust_perf(&mut policy, min_perf, target_perf, capacity);
+ }
+ }
+
+ // Policy's get_intermediate callback.
+ extern "C" fn get_intermediate_callback(
+ ptr: *mut bindings::cpufreq_policy,
+ index: u32,
+ ) -> core::ffi::c_uint {
+ // SAFETY: `ptr` is valid by the contract with the C code. `policy` is alive only for the
+ // duration of this call, so it is guaranteed to remain alive for the lifetime of
+ // `ptr`.
+ let mut policy = unsafe { Policy::from_ptr(ptr) };
+ T::get_intermediate(&mut policy, index)
+ }
+
+ // Policy's target_intermediate callback.
+ extern "C" fn target_intermediate_callback(
+ ptr: *mut bindings::cpufreq_policy,
+ index: u32,
+ ) -> core::ffi::c_int {
+ from_result(|| {
+ // SAFETY: `ptr` is valid by the contract with the C code. `policy` is alive only for the
+ // duration of this call, so it is guaranteed to remain alive for the lifetime of
+ // `ptr`.
+ let mut policy = unsafe { Policy::from_ptr(ptr) };
+ T::target_intermediate(&mut policy, index).map(|_| 0)
+ })
+ }
+
+ // Policy's get callback.
+ extern "C" fn get_callback(cpu: u32) -> core::ffi::c_uint {
+ // SAFETY: Get the policy for a CPU.
+ Policy::from_cpu(cpu).map_or(0, |mut policy| T::get(&mut policy).map_or(0, |f| f))
+ }
+
+ // Policy's update_limit callback.
+ extern "C" fn update_limits_callback(cpu: u32) {
+ // SAFETY: Get the policy for a CPU.
+ if let Some(mut policy) = Policy::from_cpu(cpu).ok() {
+ T::update_limits(&mut policy);
+ }
+ }
+
+ // Policy's bios_limit callback.
+ extern "C" fn bios_limit_callback(cpu: i32, limit: *mut u32) -> core::ffi::c_int {
+ from_result(|| {
+ let mut policy = Policy::from_cpu(cpu as u32)?;
+
+ // SAFETY: The pointer is guaranteed by the C code to be valid.
+ T::bios_limit(&mut policy, &mut (unsafe { *limit })).map(|_| 0)
+ })
+ }
+
+ // Policy's set_boost callback.
+ extern "C" fn set_boost_callback(
+ ptr: *mut bindings::cpufreq_policy,
+ state: i32,
+ ) -> core::ffi::c_int {
+ from_result(|| {
+ // SAFETY: `ptr` is valid by the contract with the C code. `policy` is alive only for the
+ // duration of this call, so it is guaranteed to remain alive for the lifetime of
+ // `ptr`.
+ let mut policy = unsafe { Policy::from_ptr(ptr) };
+ T::set_boost(&mut policy, state).map(|_| 0)
+ })
+ }
+
+ // Policy's register_em callback.
+ extern "C" fn register_em_callback(ptr: *mut bindings::cpufreq_policy) {
+ // SAFETY: `ptr` is valid by the contract with the C code. `policy` is alive only for the
+ // duration of this call, so it is guaranteed to remain alive for the lifetime of
+ // `ptr`.
+ let mut policy = unsafe { Policy::from_ptr(ptr) };
+ T::register_em(&mut policy);
+ }
+}
+
+impl<T: DriverOps> Drop for Registration<T> {
+ // Removes the registration from the kernel if it has completed successfully before.
+ fn drop(&mut self) {
+ let drv = self.drv.get_mut();
+
+ if self.registered {
+ // SAFETY: The driver was earlier registered from `register()`.
+ unsafe { bindings::cpufreq_unregister_driver(drv) };
+ }
+
+ // Free the previously leaked memory to the C code.
+ if !drv.attr.is_null() {
+ // SAFETY: The pointer was earlier initialized from the result of `Box::leak`.
+ unsafe { drop(Box::from_raw(drv.attr)) };
+ }
+
+ // Free data
+ drop(self.clear_data());
+ }
+}
--
2.31.1.272.g89b43f80a514


2024-06-07 09:23:53

by Viresh Kumar

[permalink] [raw]
Subject: [RFC PATCH V2 7/8] rust: Extend OPP bindings with CPU frequency table

This commit adds bindings for CPUFreq core related API.

Signed-off-by: Viresh Kumar <[email protected]>
---
rust/kernel/opp.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 60 insertions(+), 1 deletion(-)

diff --git a/rust/kernel/opp.rs b/rust/kernel/opp.rs
index ebcb77b52558..4833848f79b1 100644
--- a/rust/kernel/opp.rs
+++ b/rust/kernel/opp.rs
@@ -15,7 +15,10 @@
types::{ARef, AlwaysRefCounted, Opaque},
};

-use core::{ffi::c_char, marker::PhantomData, ptr};
+#[cfg(CONFIG_CPU_FREQ)]
+use crate::cpufreq;
+
+use core::{ffi::c_char, marker::PhantomData, ops::Deref, ptr};

use macros::vtable;

@@ -343,6 +346,56 @@ extern "C" fn config_regulators(
}
}

+/// CPU Frequency table created from OPP entries.
+#[cfg(CONFIG_CPU_FREQ)]
+pub struct FreqTable {
+ dev: ARef<Device>,
+ table: cpufreq::Table,
+}
+
+#[cfg(CONFIG_CPU_FREQ)]
+impl FreqTable {
+ /// Creates new instance of [`FreqTable`] from raw pointer.
+ fn new(table: &Table) -> Result<Self> {
+ let mut ptr: *mut bindings::cpufreq_frequency_table = ptr::null_mut();
+
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety
+ // requirements.
+ to_result(unsafe {
+ bindings::dev_pm_opp_init_cpufreq_table(table.dev.as_raw(), &mut ptr)
+ })?;
+ Ok(Self {
+ dev: table.dev.clone(),
+ // SAFETY: The `ptr` is guaranteed by the C code to be valid.
+ table: unsafe { cpufreq::Table::from_raw(ptr) },
+ })
+ }
+
+ /// Returns reference to the underlying [`cpufreq::Table`].
+ pub fn table(&self) -> &cpufreq::Table {
+ &self.table
+ }
+}
+
+#[cfg(CONFIG_CPU_FREQ)]
+impl Deref for FreqTable {
+ type Target = cpufreq::Table;
+
+ #[inline]
+ fn deref(&self) -> &Self::Target {
+ &self.table
+ }
+}
+
+#[cfg(CONFIG_CPU_FREQ)]
+impl Drop for FreqTable {
+ fn drop(&mut self) {
+ // SAFETY: By the type invariants, we know that `self` owns a reference, so it is safe to
+ // relinquish it now.
+ unsafe { bindings::dev_pm_opp_free_cpufreq_table(self.dev.as_raw(), &mut self.as_ptr()) };
+ }
+}
+
/// Operating performance point (OPP) table.
///
/// # Invariants
@@ -538,6 +591,12 @@ pub fn adjust_voltage(
})
}

+ /// Create cpufreq table from OPP table.
+ #[cfg(CONFIG_CPU_FREQ)]
+ pub fn to_cpufreq_table(&mut self) -> Result<FreqTable> {
+ FreqTable::new(self)
+ }
+
/// Sets a matching OPP based on frequency.
pub fn set_rate(&self, freq: u64) -> Result<()> {
// SAFETY: The requirements are satisfied by the existence of `Device` and its safety
--
2.31.1.272.g89b43f80a514