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 2/8] rust: Extend OPP bindings for the OPP table
Date: Wed, 3 Jul 2024 12:44:27 +0530 [thread overview]
Message-ID: <1cc9665b614b7de2fbe9b5ab9689b43e5c99bae5.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 `struct opp_table`.
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
rust/kernel/opp.rs | 382 ++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 381 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/opp.rs b/rust/kernel/opp.rs
index b26e39a74635..4b31f99acc67 100644
--- a/rust/kernel/opp.rs
+++ b/rust/kernel/opp.rs
@@ -8,8 +8,9 @@
use crate::{
bindings,
+ cpumask::Cpumask,
device::Device,
- error::{code::*, to_result, Result},
+ error::{code::*, from_err_ptr, to_result, Error, Result},
types::{ARef, AlwaysRefCounted, Opaque},
};
@@ -67,6 +68,385 @@ fn freq(&self) -> u64 {
}
}
+/// OPP search types.
+#[derive(Copy, Clone, Debug, Eq, PartialEq)]
+pub enum SearchType {
+ /// Search for exact value.
+ Exact,
+ /// Search for highest value less than equal to value.
+ Floor,
+ /// Search for lowest value greater than equal to value.
+ Ceil,
+}
+
+/// Operating performance point (OPP) table.
+///
+/// # Invariants
+///
+/// The pointer stored in `Self` is non-null and valid for the lifetime of the ARef instance. In
+/// particular, the ARef instance owns an increment on underlying object’s reference count.
+pub struct Table {
+ ptr: *mut bindings::opp_table,
+ dev: ARef<Device>,
+ em: bool,
+ of: bool,
+ cpumask: Option<Cpumask>,
+}
+
+// SAFETY: The fields of `Table` are safe to be used from any thread.
+unsafe impl Send for Table {}
+
+// SAFETY: The fields of `Table` are safe to be referenced from any thread.
+unsafe impl Sync for Table {}
+
+impl Table {
+ /// Creates a new OPP table instance from raw pointer.
+ ///
+ /// # Safety
+ ///
+ /// Callers must ensure that `ptr` is valid and non-null.
+ unsafe fn from_raw_table(ptr: *mut bindings::opp_table, dev: &ARef<Device>) -> Self {
+ // SAFETY: By the safety requirements, ptr is valid and its refcount will be incremented.
+ unsafe { bindings::dev_pm_opp_get_opp_table_ref(ptr) };
+
+ Self {
+ ptr,
+ dev: dev.clone(),
+ em: false,
+ of: false,
+ cpumask: None,
+ }
+ }
+
+ /// Find OPP table from device.
+ pub fn from_dev(dev: &ARef<Device>) -> Result<Self> {
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety
+ // requirements. Refcount of the OPP table is incremented as well.
+ let ptr = from_err_ptr(unsafe { bindings::dev_pm_opp_get_opp_table(dev.as_raw()) })?;
+
+ Ok(Self {
+ ptr,
+ dev: dev.clone(),
+ em: false,
+ of: false,
+ cpumask: None,
+ })
+ }
+
+ /// Add device tree based OPP table for the device.
+ #[cfg(CONFIG_OF)]
+ pub fn from_of(dev: &ARef<Device>, index: i32) -> Result<Self> {
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety
+ // requirements. Refcount of the OPP table is incremented as well.
+ to_result(unsafe { bindings::dev_pm_opp_of_add_table_indexed(dev.as_raw(), index) })?;
+
+ // Fetch the newly created table.
+ let mut table = Self::from_dev(dev)?;
+ table.of = true;
+
+ Ok(table)
+ }
+
+ // Remove device tree based OPP table for the device.
+ #[cfg(CONFIG_OF)]
+ fn remove_of(&self) {
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety
+ // requirements. We took the reference from `from_of` earlier, it is safe to drop the same
+ // now.
+ unsafe { bindings::dev_pm_opp_of_remove_table(self.dev.as_raw()) };
+ }
+
+ /// Add device tree based OPP table for CPU devices.
+ #[cfg(CONFIG_OF)]
+ pub fn from_of_cpumask(dev: &ARef<Device>, cpumask: &Cpumask) -> Result<Self> {
+ // SAFETY: The cpumask is valid and the returned ptr will be owned by the [`Table`] instance.
+ to_result(unsafe { bindings::dev_pm_opp_of_cpumask_add_table(cpumask.as_ptr()) })?;
+
+ // Fetch the newly created table.
+ let mut table = Self::from_dev(dev)?;
+
+ let mut mask = Cpumask::new()?;
+ cpumask.copy(&mut mask);
+ table.cpumask = Some(mask);
+
+ Ok(table)
+ }
+
+ // Remove device tree based OPP table for CPU devices.
+ #[cfg(CONFIG_OF)]
+ fn remove_of_cpumask(&self, cpumask: Cpumask) {
+ // SAFETY: The cpumask is valid and we took the reference from `from_of_cpumask` earlier,
+ // it is safe to drop the same now.
+ unsafe { bindings::dev_pm_opp_of_cpumask_remove_table(cpumask.as_ptr()) };
+ }
+
+ /// Returns the number of OPPs in the table.
+ pub fn opp_count(&self) -> Result<u32> {
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety
+ // requirements.
+ let ret = unsafe { bindings::dev_pm_opp_get_opp_count(self.dev.as_raw()) };
+ if ret < 0 {
+ Err(Error::from_errno(ret))
+ } else {
+ Ok(ret as u32)
+ }
+ }
+
+ /// Returns max clock latency of the OPPs in the table.
+ pub fn max_clock_latency(&self) -> u64 {
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety
+ // requirements.
+ unsafe { bindings::dev_pm_opp_get_max_clock_latency(self.dev.as_raw()) }
+ }
+
+ /// Returns max volt latency of the OPPs in the table.
+ pub fn max_volt_latency(&self) -> u64 {
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety
+ // requirements.
+ unsafe { bindings::dev_pm_opp_get_max_volt_latency(self.dev.as_raw()) }
+ }
+
+ /// Returns max transition latency of the OPPs in the table.
+ pub fn max_transition_latency(&self) -> u64 {
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety
+ // requirements.
+ unsafe { bindings::dev_pm_opp_get_max_transition_latency(self.dev.as_raw()) }
+ }
+
+ /// Returns the suspend OPP.
+ pub fn suspend_freq(&self) -> u64 {
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety
+ // requirements.
+ unsafe { bindings::dev_pm_opp_get_suspend_opp_freq(self.dev.as_raw()) }
+ }
+
+ /// Synchronizes regulators used by the OPP table.
+ pub fn sync_regulators(&self) -> Result<()> {
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety
+ // requirements.
+ to_result(unsafe { bindings::dev_pm_opp_sync_regulators(self.dev.as_raw()) })
+ }
+
+ /// Gets sharing CPUs.
+ pub fn sharing_cpus(dev: &Device, cpumask: &mut Cpumask) -> Result<()> {
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety
+ // requirements.
+ to_result(unsafe {
+ bindings::dev_pm_opp_get_sharing_cpus(dev.as_raw(), cpumask.as_mut_ptr())
+ })
+ }
+
+ /// Sets sharing CPUs.
+ pub fn set_sharing_cpus(&mut self, cpumask: &Cpumask) -> Result<()> {
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety
+ // requirements.
+ to_result(unsafe {
+ bindings::dev_pm_opp_set_sharing_cpus(self.dev.as_raw(), cpumask.as_ptr())
+ })?;
+
+ if let Some(mask) = self.cpumask.as_mut() {
+ // Update the cpumask as this will be used while removing the table.
+ cpumask.copy(mask);
+ }
+
+ Ok(())
+ }
+
+ /// Gets sharing CPUs from Device tree.
+ #[cfg(CONFIG_OF)]
+ pub fn of_sharing_cpus(dev: &Device, cpumask: &mut Cpumask) -> Result<()> {
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety
+ // requirements.
+ to_result(unsafe {
+ bindings::dev_pm_opp_of_get_sharing_cpus(dev.as_raw(), cpumask.as_mut_ptr())
+ })
+ }
+
+ /// Updates the voltage value for an OPP.
+ pub fn adjust_voltage(
+ &self,
+ freq: u64,
+ u_volt: u64,
+ u_volt_min: u64,
+ u_volt_max: u64,
+ ) -> Result<()> {
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety
+ // requirements.
+ to_result(unsafe {
+ bindings::dev_pm_opp_adjust_voltage(
+ self.dev.as_raw(),
+ freq,
+ u_volt,
+ u_volt_min,
+ u_volt_max,
+ )
+ })
+ }
+
+ /// 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
+ // requirements.
+ to_result(unsafe { bindings::dev_pm_opp_set_rate(self.dev.as_raw(), freq) })
+ }
+
+ /// Sets exact OPP.
+ pub fn set_opp(&self, opp: &OPP) -> Result<()> {
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety
+ // requirements.
+ to_result(unsafe { bindings::dev_pm_opp_set_opp(self.dev.as_raw(), opp.as_raw()) })
+ }
+
+ /// Finds OPP based on frequency.
+ pub fn opp_from_freq(
+ &self,
+ mut freq: u64,
+ available: Option<bool>,
+ index: Option<u32>,
+ stype: SearchType,
+ ) -> Result<ARef<OPP>> {
+ let rdev = self.dev.as_raw();
+ let index = index.unwrap_or(0);
+
+ let ptr = from_err_ptr(match stype {
+ SearchType::Exact => {
+ if let Some(available) = available {
+ // SAFETY: The requirements are satisfied by the existence of `Device` and
+ // its safety requirements. The returned ptr will be owned by the new [`OPP`]
+ // instance.
+ unsafe {
+ bindings::dev_pm_opp_find_freq_exact_indexed(rdev, freq, index, available)
+ }
+ } else {
+ return Err(EINVAL);
+ }
+ }
+
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its
+ // safety requirements. The returned ptr will be owned by the new [`OPP`] instance.
+ SearchType::Ceil => unsafe {
+ bindings::dev_pm_opp_find_freq_ceil_indexed(rdev, &mut freq as *mut u64, index)
+ },
+
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its
+ // safety requirements. The returned ptr will be owned by the new [`OPP`] instance.
+ SearchType::Floor => unsafe {
+ bindings::dev_pm_opp_find_freq_floor_indexed(rdev, &mut freq as *mut u64, index)
+ },
+ })?;
+
+ // SAFETY: The `ptr` is guaranteed by the C code to be valid.
+ Ok(unsafe { OPP::from_raw_opp_owned(ptr)?.into() })
+ }
+
+ /// Finds OPP based on level.
+ pub fn opp_from_level(&self, mut level: u32, stype: SearchType) -> Result<ARef<OPP>> {
+ let rdev = self.dev.as_raw();
+
+ let ptr = from_err_ptr(match stype {
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its
+ // safety requirements. The returned ptr will be owned by the new [`OPP`] instance.
+ SearchType::Exact => unsafe { bindings::dev_pm_opp_find_level_exact(rdev, level) },
+
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its
+ // safety requirements. The returned ptr will be owned by the new [`OPP`] instance.
+ SearchType::Ceil => unsafe {
+ bindings::dev_pm_opp_find_level_ceil(rdev, &mut level as *mut u32)
+ },
+
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its
+ // safety requirements. The returned ptr will be owned by the new [`OPP`] instance.
+ SearchType::Floor => unsafe {
+ bindings::dev_pm_opp_find_level_floor(rdev, &mut level as *mut u32)
+ },
+ })?;
+
+ // SAFETY: The `ptr` is guaranteed by the C code to be valid.
+ Ok(unsafe { OPP::from_raw_opp_owned(ptr)?.into() })
+ }
+
+ /// Finds OPP based on bandwidth.
+ pub fn opp_from_bw(&self, mut bw: u32, index: i32, stype: SearchType) -> Result<ARef<OPP>> {
+ let rdev = self.dev.as_raw();
+
+ let ptr = from_err_ptr(match stype {
+ // The OPP core doesn't support this yet.
+ SearchType::Exact => return Err(EINVAL),
+
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its
+ // safety requirements. The returned ptr will be owned by the new [`OPP`] instance.
+ SearchType::Ceil => unsafe {
+ bindings::dev_pm_opp_find_bw_ceil(rdev, &mut bw as *mut u32, index)
+ },
+
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its
+ // safety requirements. The returned ptr will be owned by the new [`OPP`] instance.
+ SearchType::Floor => unsafe {
+ bindings::dev_pm_opp_find_bw_floor(rdev, &mut bw as *mut u32, index)
+ },
+ })?;
+
+ // SAFETY: The `ptr` is guaranteed by the C code to be valid.
+ Ok(unsafe { OPP::from_raw_opp_owned(ptr)?.into() })
+ }
+
+ /// Enable the OPP.
+ pub fn enable_opp(&self, freq: u64) -> Result<()> {
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety
+ // requirements.
+ to_result(unsafe { bindings::dev_pm_opp_enable(self.dev.as_raw(), freq) })
+ }
+
+ /// Disable the OPP.
+ pub fn disable_opp(&self, freq: u64) -> Result<()> {
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety
+ // requirements.
+ to_result(unsafe { bindings::dev_pm_opp_disable(self.dev.as_raw(), freq) })
+ }
+
+ /// Registers with Energy model.
+ #[cfg(CONFIG_OF)]
+ pub fn of_register_em(&mut self, cpumask: &mut Cpumask) -> Result<()> {
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety
+ // requirements.
+ to_result(unsafe {
+ bindings::dev_pm_opp_of_register_em(self.dev.as_raw(), cpumask.as_mut_ptr())
+ })?;
+
+ self.em = true;
+ Ok(())
+ }
+
+ // Unregisters with Energy model.
+ #[cfg(CONFIG_OF)]
+ fn of_unregister_em(&self) {
+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety
+ // requirements. We registered with the EM framework earlier, it is safe to unregister now.
+ unsafe { bindings::em_dev_unregister_perf_domain(self.dev.as_raw()) };
+ }
+}
+
+impl Drop for Table {
+ 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_put_opp_table(self.ptr) };
+
+ #[cfg(CONFIG_OF)]
+ {
+ if self.em {
+ self.of_unregister_em();
+ }
+
+ if self.of {
+ self.remove_of();
+ } else if let Some(cpumask) = self.cpumask.take() {
+ self.remove_of_cpumask(cpumask);
+ }
+ }
+ }
+}
+
/// Operating performance point (OPP).
///
/// # 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 ` Viresh Kumar [this message]
2024-07-03 7:14 ` [RFC PATCH V3 3/8] rust: Extend OPP bindings for the configuration options Viresh Kumar
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=1cc9665b614b7de2fbe9b5ab9689b43e5c99bae5.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).