From: Viresh Kumar <viresh.kumar@linaro.org>
To: "Rafael J. Wysocki" <rafael@kernel.org>,
"Miguel Ojeda" <miguel.ojeda.sandonis@gmail.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Wedson Almeida Filho" <wedsonaf@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@samsung.com>,
"Alice Ryhl" <aliceryhl@google.com>
Cc: "Viresh Kumar" <viresh.kumar@linaro.org>,
linux-pm@vger.kernel.org,
"Vincent Guittot" <vincent.guittot@linaro.org>,
"Stephen Boyd" <sboyd@kernel.org>, "Nishanth Menon" <nm@ti.com>,
rust-for-linux@vger.kernel.org,
"Manos Pitsidianakis" <manos.pitsidianakis@linaro.org>,
"Erik Schilling" <erik.schilling@linaro.org>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Joakim Bech" <joakim.bech@linaro.org>,
"Rob Herring" <robh@kernel.org>,
linux-kernel@vger.kernel.org
Subject: [RFC PATCH V3 3/8] rust: Extend OPP bindings for the configuration options
Date: Wed, 3 Jul 2024 12:44:28 +0530 [thread overview]
Message-ID: <6d2d8f98dbed555167b6134202ae2e6d121f7c78.1719990273.git.viresh.kumar@linaro.org> (raw)
In-Reply-To: <cover.1719990273.git.viresh.kumar@linaro.org>
This extends OPP bindings with the bindings for the OPP core
configuration options.
Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
rust/kernel/opp.rs | 301 ++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 299 insertions(+), 2 deletions(-)
diff --git a/rust/kernel/opp.rs b/rust/kernel/opp.rs
index 4b31f99acc67..92c4ac6cb89c 100644
--- a/rust/kernel/opp.rs
+++ b/rust/kernel/opp.rs
@@ -10,11 +10,28 @@
bindings,
cpumask::Cpumask,
device::Device,
- error::{code::*, from_err_ptr, to_result, Error, Result},
+ error::{code::*, from_err_ptr, from_result, to_result, Error, Result, VTABLE_DEFAULT_ERROR},
+ prelude::*,
+ str::CString,
types::{ARef, AlwaysRefCounted, Opaque},
};
-use core::ptr;
+use core::{ffi::c_char, marker::PhantomData, ptr};
+
+use macros::vtable;
+
+// Creates a null-terminated slice of pointers to Cstrings.
+fn to_c_str_array(names: &[CString]) -> Result<Vec<*const c_char>> {
+ // Allocated a null-terminated vector of pointers.
+ let mut list = Vec::with_capacity(names.len() + 1, GFP_KERNEL)?;
+
+ for name in names.iter() {
+ list.push(name.as_ptr() as _, GFP_KERNEL)?;
+ }
+
+ list.push(ptr::null(), GFP_KERNEL)?;
+ Ok(list)
+}
/// Dynamically created Operating performance point (OPP).
pub struct Token {
@@ -79,6 +96,286 @@ pub enum SearchType {
Ceil,
}
+/// Implement this trait to provide OPP Configuration callbacks.
+#[vtable]
+pub trait ConfigOps {
+ /// Called by the OPP core to configure OPP clks.
+ fn config_clks(_dev: &Device, _table: &Table, _opp: &OPP, _scaling_down: bool) -> Result<()> {
+ kernel::build_error(VTABLE_DEFAULT_ERROR)
+ }
+
+ /// Called by the OPP core to configure OPP regulators.
+ fn config_regulators(
+ _dev: &Device,
+ _opp_old: &OPP,
+ _opp_new: &OPP,
+ _data: *mut *mut bindings::regulator,
+ _count: u32,
+ ) -> Result<()> {
+ kernel::build_error(VTABLE_DEFAULT_ERROR)
+ }
+}
+
+/// Config token returned by the C code.
+pub struct ConfigToken(i32);
+
+impl Drop for ConfigToken {
+ 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_clear_config(self.0) };
+ }
+}
+
+/// Equivalent to `struct dev_pm_opp_config` in the C Code.
+#[derive(Default)]
+pub struct Config<T: ConfigOps> {
+ clk_names: Option<Vec<CString>>,
+ prop_name: Option<CString>,
+ regulator_names: Option<Vec<CString>>,
+ genpd_names: Option<Vec<CString>>,
+ supported_hw: Option<Vec<u32>>,
+ required_devs: Option<Vec<ARef<Device>>>,
+ _data: PhantomData<T>,
+}
+
+impl<T: ConfigOps> Config<T> {
+ /// Creates a new instance of [`Config`].
+ pub fn new() -> Self {
+ Self {
+ clk_names: None,
+ prop_name: None,
+ regulator_names: None,
+ genpd_names: None,
+ supported_hw: None,
+ required_devs: None,
+ _data: PhantomData,
+ }
+ }
+
+ /// Initializes clock names.
+ pub fn set_clk_names(mut self, names: Vec<CString>) -> Result<Self> {
+ // Already configured.
+ if self.clk_names.is_some() {
+ return Err(EBUSY);
+ }
+
+ if names.is_empty() {
+ return Err(EINVAL);
+ }
+
+ self.clk_names = Some(names);
+ Ok(self)
+ }
+
+ /// Initializes property name.
+ pub fn set_prop_name(mut self, name: CString) -> Result<Self> {
+ // Already configured.
+ if self.prop_name.is_some() {
+ return Err(EBUSY);
+ }
+
+ self.prop_name = Some(name);
+ Ok(self)
+ }
+
+ /// Initializes regulator names.
+ pub fn set_regulator_names(mut self, names: Vec<CString>) -> Result<Self> {
+ // Already configured.
+ if self.regulator_names.is_some() {
+ return Err(EBUSY);
+ }
+
+ if names.is_empty() {
+ return Err(EINVAL);
+ }
+
+ self.regulator_names = Some(names);
+
+ Ok(self)
+ }
+
+ /// Initializes genpd names.
+ pub fn set_genpd_names(mut self, names: Vec<CString>) -> Result<Self> {
+ // Already configured. Only one of genpd or required devs can be configured.
+ if self.genpd_names.is_some() || self.required_devs.is_some() {
+ return Err(EBUSY);
+ }
+
+ if names.is_empty() {
+ return Err(EINVAL);
+ }
+
+ self.genpd_names = Some(names);
+ Ok(self)
+ }
+
+ /// Initializes required devices.
+ pub fn set_required_devs(mut self, devs: Vec<ARef<Device>>) -> Result<Self> {
+ // Already configured. Only one of genpd or required devs can be configured.
+ if self.genpd_names.is_some() || self.required_devs.is_some() {
+ return Err(EBUSY);
+ }
+
+ if devs.is_empty() {
+ return Err(EINVAL);
+ }
+
+ self.required_devs = Some(devs);
+ Ok(self)
+ }
+
+ /// Initializes supported hardware.
+ pub fn set_supported_hw(mut self, hw: Vec<u32>) -> Result<Self> {
+ // Already configured.
+ if self.supported_hw.is_some() {
+ return Err(EBUSY);
+ }
+
+ if hw.is_empty() {
+ return Err(EINVAL);
+ }
+
+ self.supported_hw = Some(hw);
+ Ok(self)
+ }
+
+ /// Sets the configuration with the OPP core.
+ pub fn set(self, dev: &Device) -> Result<ConfigToken> {
+ let (_clk_list, clk_names) = match &self.clk_names {
+ Some(x) => {
+ let list = to_c_str_array(x)?;
+ let ptr = list.as_ptr();
+ (Some(list), ptr)
+ }
+ None => (None, ptr::null()),
+ };
+
+ let (_regulator_list, regulator_names) = match &self.regulator_names {
+ Some(x) => {
+ let list = to_c_str_array(x)?;
+ let ptr = list.as_ptr();
+ (Some(list), ptr)
+ }
+ None => (None, ptr::null()),
+ };
+
+ let (_genpd_list, genpd_names) = match &self.genpd_names {
+ Some(x) => {
+ let list = to_c_str_array(x)?;
+ let ptr = list.as_ptr();
+ (Some(list), ptr)
+ }
+ None => (None, ptr::null()),
+ };
+
+ let prop_name = match &self.prop_name {
+ Some(x) => x.as_char_ptr(),
+ None => ptr::null(),
+ };
+
+ let (supported_hw, supported_hw_count) = match &self.supported_hw {
+ Some(x) => (x.as_ptr(), x.len() as u32),
+ None => (ptr::null(), 0),
+ };
+
+ let (_required_devs_list, required_devs) = match &self.required_devs {
+ Some(x) => {
+ // Create a non-NULL-terminated vectorof pointers.
+ let mut list = Vec::with_capacity(x.len(), GFP_KERNEL)?;
+
+ for dev in x.iter() {
+ list.push(dev.as_raw(), GFP_KERNEL)?;
+ }
+
+ let ptr = list.as_mut_ptr();
+ (Some(list), ptr)
+ }
+ None => (None, ptr::null_mut()),
+ };
+
+ let mut config = bindings::dev_pm_opp_config {
+ clk_names,
+ config_clks: if T::HAS_CONFIG_CLKS {
+ Some(Self::config_clks)
+ } else {
+ None
+ },
+ prop_name,
+ regulator_names,
+ config_regulators: if T::HAS_CONFIG_REGULATORS {
+ Some(Self::config_regulators)
+ } else {
+ None
+ },
+ genpd_names,
+ supported_hw,
+ supported_hw_count,
+
+ // Don't need to support virt_devs for now.
+ virt_devs: ptr::null_mut(),
+ required_devs,
+ };
+
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety
+ // requirements. The OPP core guarantees to not use fields of `config`, after this call has
+ // returned and so we don't need to save a copy of them for future use
+ let ret = unsafe { bindings::dev_pm_opp_set_config(dev.as_raw(), &mut config) };
+ if ret < 0 {
+ Err(Error::from_errno(ret))
+ } else {
+ Ok(ConfigToken(ret))
+ }
+ }
+
+ // Config's config_clks callback.
+ extern "C" fn config_clks(
+ dev: *mut bindings::device,
+ opp_table: *mut bindings::opp_table,
+ opp: *mut bindings::dev_pm_opp,
+ _data: *mut core::ffi::c_void,
+ scaling_down: bool,
+ ) -> core::ffi::c_int {
+ from_result(|| {
+ // SAFETY: 'dev' is guaranteed by the C code to be valid.
+ let dev = unsafe { Device::from_raw(dev) };
+ T::config_clks(
+ &dev,
+ // SAFETY: 'opp_table' is guaranteed by the C code to be valid.
+ &unsafe { Table::from_raw_table(opp_table, &dev) },
+ // SAFETY: 'opp' is guaranteed by the C code to be valid.
+ unsafe { OPP::from_raw_opp(opp)? },
+ scaling_down,
+ )
+ .map(|_| 0)
+ })
+ }
+
+ // Config's config_regulators callback.
+ extern "C" fn config_regulators(
+ dev: *mut bindings::device,
+ old_opp: *mut bindings::dev_pm_opp,
+ new_opp: *mut bindings::dev_pm_opp,
+ regulators: *mut *mut bindings::regulator,
+ count: core::ffi::c_uint,
+ ) -> core::ffi::c_int {
+ from_result(|| {
+ // SAFETY: 'dev' is guaranteed by the C code to be valid.
+ let dev = unsafe { Device::from_raw(dev) };
+ T::config_regulators(
+ &dev,
+ // SAFETY: 'old_opp' is guaranteed by the C code to be valid.
+ unsafe { OPP::from_raw_opp(old_opp)? },
+ // SAFETY: 'new_opp' is guaranteed by the C code to be valid.
+ unsafe { OPP::from_raw_opp(new_opp)? },
+ regulators,
+ count,
+ )
+ .map(|_| 0)
+ })
+ }
+}
+
/// Operating performance point (OPP) table.
///
/// # Invariants
--
2.31.1.272.g89b43f80a514
next prev parent reply other threads:[~2024-07-03 7:30 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-03 7:14 [RFC PATCH V3 0/8] Rust bindings for cpufreq and OPP core + sample driver Viresh Kumar
2024-07-03 7:14 ` [RFC PATCH V3 1/8] rust: Add initial bindings for OPP framework Viresh Kumar
2024-07-03 15:34 ` Boqun Feng
2024-07-05 11:02 ` Viresh Kumar
2024-07-05 18:19 ` Boqun Feng
2024-07-09 11:02 ` Viresh Kumar
2024-07-09 17:45 ` Boqun Feng
2024-07-10 7:36 ` Viresh Kumar
2024-07-10 11:06 ` Viresh Kumar
2024-07-10 21:58 ` Boqun Feng
2024-07-03 7:14 ` [RFC PATCH V3 2/8] rust: Extend OPP bindings for the OPP table Viresh Kumar
2024-07-03 7:14 ` Viresh Kumar [this message]
2024-07-03 7:14 ` [RFC PATCH V3 4/8] rust: Add initial bindings for cpufreq framework Viresh Kumar
2024-07-03 7:14 ` [RFC PATCH V3 5/8] rust: Extend cpufreq bindings for policy and driver ops Viresh Kumar
2024-07-03 7:14 ` [RFC PATCH V3 6/8] rust: Extend cpufreq bindings for driver registration Viresh Kumar
2024-07-05 11:39 ` Danilo Krummrich
2024-07-05 11:43 ` Danilo Krummrich
2024-07-03 7:14 ` [RFC PATCH V3 7/8] rust: Extend OPP bindings with CPU frequency table Viresh Kumar
2024-07-03 7:14 ` [RFC PATCH V3 8/8] cpufreq: Add Rust based cpufreq-dt driver Viresh Kumar
2024-07-05 11:32 ` Danilo Krummrich
2024-07-10 8:56 ` Viresh Kumar
2024-07-10 15:28 ` Danilo Krummrich
2024-07-11 6:06 ` Viresh Kumar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6d2d8f98dbed555167b6134202ae2e6d121f7c78.1719990273.git.viresh.kumar@linaro.org \
--to=viresh.kumar@linaro.org \
--cc=a.hindborg@samsung.com \
--cc=alex.bennee@linaro.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=erik.schilling@linaro.org \
--cc=gary@garyguo.net \
--cc=joakim.bech@linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=manos.pitsidianakis@linaro.org \
--cc=miguel.ojeda.sandonis@gmail.com \
--cc=nm@ti.com \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=robh@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=sboyd@kernel.org \
--cc=vincent.guittot@linaro.org \
--cc=wedsonaf@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).