From patchwork Wed Jul 3 07:14:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 13721484 Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 90F80136E38 for ; Wed, 3 Jul 2024 07:30:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719991849; cv=none; b=tWGhJXkQYolTy+bNEI7KUiMwI7B0aNbmG11qTbstOfoOu8t/j+t2iTGUFN66nI44CRSCOXAqBf3hCCbhF4mzHVdFngK52pZKakvtak6o15ZQosZOa4vttVuAWFD9y/YS+gtwZSZjxU7gD+vb2Jg3HbN8rjIV3yA8fPCaDzwEy48= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719991849; c=relaxed/simple; bh=GK2g/4QhvhMTx4QYRZDVe7tqIhXsVJ2LLJpMAfTNCdw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=aSW+K0arVpxykO+U2GhkFGg5EMdAoO+5tf+zgl3ZZWQRY17BiMW+0M5KAktwvyIiw+ik5z/k7Ive/tcyuiFdZRrDu7X4j75UYKDU19qTEweaS5ptRp5Wsabm7BZct301+fk240TsoPpk7J+arzsckbmd1qxexJafVBTT08ZYMgs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=mOv0Rz9b; arc=none smtp.client-ip=209.85.215.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="mOv0Rz9b" Received: by mail-pg1-f169.google.com with SMTP id 41be03b00d2f7-7178727da84so2929165a12.0 for ; Wed, 03 Jul 2024 00:30:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1719991847; x=1720596647; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=LmIqXvmGNOMRdgx5QTBNAc1d2UsMur4Ln/Lj5x2EkKQ=; b=mOv0Rz9bS4fwcOXiFCtfO38Q/lfPlOXTTJUOeaggRJZ1RVhW5f3KnMwQig4Hybs7if 48/WUCrLhy5/9UGFrd8idvM9mhltEujw//Cv3I+fmE7jGsKIDG6JA3mKDsifjgfT33YE kq0wlLc57trDhrTCknxA8WUjUMJqV/lZIbcpdoFfhWgQnD5yW/YLyyyj8r2Xq2bKduLc g9idxUwbsmSPpJfnv1H036P9/ezbeVX8H07NG1YoWNTzh8iSX7bJ03vCwaBc7sSqyBkG /0+TYmzMDjXdScsqgypJXjVIv0GCTtGzy4pS6RaUQF8JNci56G72tnQ8SkxB2sA2QInT xfTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719991847; x=1720596647; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LmIqXvmGNOMRdgx5QTBNAc1d2UsMur4Ln/Lj5x2EkKQ=; b=QXe/N+QAdLSPJCGnX/MYRv6BYl7ygl5U+nFVVydzZ1I2/DLY4KgHgGjcw7h8yF4sa8 l5aw9mqi7yoRa71YvGKtwtdae7EtjhHr7hxPjWDwTCw6rgBOAP9yHdgSjZVXgUftxNeX MH8hU0ZxQP43YyDuMaVNXBHiwKSsuHO3pU5Ty6eoSHcNnPiYZHjk79CFBtmWdbvTSaL3 +Iopddg6IqwmvbbeVsjuJVRXkeWrEC/yTyKJs/0vVoTcVMYxpKGIvnsnkrfYftm+orTr B900VO2P7YLSw4Bu84kvib1NCp/dEw7t49qz9sibfhGbxmIL5zmwKFk76MEuWwacMqwN njmg== X-Forwarded-Encrypted: i=1; AJvYcCXIOeltgvIJNf+7o4TJ/KreOSbJIVcnjlFBvRwN++9y/RZ5bzzzPgnBPU3quFHzcPb5BXFTuw384UHz6zAjCtbMkTTBHP3kw3g= X-Gm-Message-State: AOJu0YxDXS6s29QPhk3i6hVa88ALFyJ3e7EL5ISerOXeaQ5KjtsupUZA hI9eddq6v15jZpgw1WX8Y3PpGAUUofVcpV17NUuw7utyuOCzyEUZvoisFYgl2+4= X-Google-Smtp-Source: AGHT+IE+NygpQCuADLF9esQ+aGRiDiKaDEVs/1KtnrCfBn2nuONTakqGy5ihyncFzkVA+na251+Xrw== X-Received: by 2002:a05:6a20:3948:b0:1be:cdce:9fb7 with SMTP id adf61e73a8af0-1bef613f429mr11746387637.19.1719991846646; Wed, 03 Jul 2024 00:30:46 -0700 (PDT) Received: from localhost ([122.172.82.13]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10d1fb1sm95810335ad.36.2024.07.03.00.30.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jul 2024 00:30:46 -0700 (PDT) From: Viresh Kumar To: "Rafael J. Wysocki" , Miguel Ojeda , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl Cc: Viresh Kumar , linux-pm@vger.kernel.org, Vincent Guittot , Stephen Boyd , Nishanth Menon , rust-for-linux@vger.kernel.org, Manos Pitsidianakis , Erik Schilling , =?utf-8?q?Alex_Benn=C3=A9e?= , Joakim Bech , Rob Herring , linux-kernel@vger.kernel.org Subject: [RFC PATCH V3 1/8] rust: Add initial bindings for OPP framework Date: Wed, 3 Jul 2024 12:44:26 +0530 Message-Id: X-Mailer: git-send-email 2.31.1.272.g89b43f80a514 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This commit adds initial Rust bindings for the Operating performance points (OPP) core. This adds bindings for `struct dev_pm_opp` and `struct dev_pm_opp_data` to begin with. Reviewed-by: Manos Pitsidianakis Signed-off-by: Viresh Kumar --- rust/bindings/bindings_helper.h | 1 + rust/kernel/lib.rs | 2 + rust/kernel/opp.rs | 182 ++++++++++++++++++++++++++++++++ 3 files changed, 185 insertions(+) create mode 100644 rust/kernel/opp.rs diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index d8b54b9fa4d0..1bf8e053c8f4 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 3bf1089b87a3..e309d7774cbd 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -45,6 +45,8 @@ #[cfg(CONFIG_NET)] pub mod net; pub mod of; +#[cfg(CONFIG_PM_OPP)] +pub mod opp; pub mod platform; pub mod prelude; pub mod print; diff --git a/rust/kernel/opp.rs b/rust/kernel/opp.rs new file mode 100644 index 000000000000..b26e39a74635 --- /dev/null +++ b/rust/kernel/opp.rs @@ -0,0 +1,182 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Operating performance points. +//! +//! This module provides bindings for interacting with the OPP subsystem. +//! +//! C header: [`include/linux/pm_opp.h`](srctree/include/linux/pm_opp.h) + +use crate::{ + bindings, + device::Device, + error::{code::*, to_result, Result}, + types::{ARef, AlwaysRefCounted, Opaque}, +}; + +use core::ptr; + +/// Dynamically created Operating performance point (OPP). +pub struct Token { + dev: ARef, + freq: u64, +} + +impl Token { + /// Adds an OPP dynamically. + pub fn new(dev: &ARef, mut data: Data) -> Result { + // SAFETY: The requirements are satisfied by the existence of `Device` and its safety + // requirements. + to_result(unsafe { bindings::dev_pm_opp_add_dynamic(dev.as_raw(), &mut data.0) })?; + Ok(Self { + dev: dev.clone(), + freq: data.freq(), + }) + } +} + +impl Drop for Token { + fn drop(&mut self) { + // SAFETY: The requirements are satisfied by the existence of `Device` and its safety + // requirements. + unsafe { bindings::dev_pm_opp_remove(self.dev.as_raw(), self.freq) }; + } +} + +/// Equivalent to `struct dev_pm_opp_data` in the C Code. +#[repr(transparent)] +pub struct Data(bindings::dev_pm_opp_data); + +impl Data { + /// Creates new instance of [`Data`]. + pub fn new(freq: u64, u_volt: u64, level: u32, turbo: bool) -> Self { + Self(bindings::dev_pm_opp_data { + turbo, + freq, + u_volt, + level, + }) + } + + /// Adds an OPP dynamically. The OPP is freed once the [`Token`] gets freed. + pub fn add_opp(self, dev: &ARef) -> Result { + Token::new(dev, self) + } + + fn freq(&self) -> u64 { + self.0.freq + } +} + +/// Operating performance point (OPP). +/// +/// # 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. +#[repr(transparent)] +pub struct OPP(Opaque); + +// SAFETY: `OPP` only holds a pointer to a C OPP, which is safe to be used from any thread. +unsafe impl Send for OPP {} + +// SAFETY: `OPP` only holds a pointer to a C OPP, references to which are safe to be used from any +// thread. +unsafe impl Sync for OPP {} + +// SAFETY: The type invariants guarantee that [`OPP`] is always refcounted. +unsafe impl AlwaysRefCounted for OPP { + fn inc_ref(&self) { + // SAFETY: The existence of a shared reference means that the refcount is nonzero. + unsafe { bindings::dev_pm_opp_get(self.0.get()) }; + } + + unsafe fn dec_ref(obj: ptr::NonNull) { + // SAFETY: The safety requirements guarantee that the refcount is nonzero. + unsafe { bindings::dev_pm_opp_put(obj.cast().as_ptr()) } + } +} + +impl OPP { + /// Creates a reference to a [`OPP`] from a valid pointer. + /// + /// # Safety + /// + /// The caller must ensure that `ptr` is valid and remains valid for the lifetime of the + /// returned [`OPP`] reference. + pub unsafe fn from_raw_opp_owned<'a>(ptr: *mut bindings::dev_pm_opp) -> Result<&'a Self> { + // SAFETY: The caller guarantees that the pointer is not dangling + // and stays valid for the duration of 'a. The cast is okay because + // `OPP` is `repr(transparent)`. + Ok(unsafe { &*ptr.cast() }) + } + + /// Creates a reference to a [`OPP`] from a valid pointer. + /// + /// # Safety + /// + /// The caller must ensure that `ptr` is valid and remains valid for the lifetime of the + /// returned [`OPP`] reference. + pub unsafe fn from_raw_opp<'a>(ptr: *mut bindings::dev_pm_opp) -> Result<&'a Self> { + let opp = unsafe { Self::from_raw_opp_owned(ptr) }?; + + // Take an extra reference to the OPP since the caller didn't take it. + opp.inc_ref(); + Ok(opp) + } + + #[inline] + fn as_raw(&self) -> *mut bindings::dev_pm_opp { + self.0.get() + } + + /// Returns the frequency of an OPP. + pub fn freq(&self, index: Option) -> u64 { + let index = index.unwrap_or(0); + + // SAFETY: By the type invariants, we know that `self` owns a reference, so it is safe to + // use it. + unsafe { bindings::dev_pm_opp_get_freq_indexed(self.as_raw(), index) } + } + + /// Returns the voltage of an OPP. + pub fn voltage(&self) -> u64 { + // SAFETY: By the type invariants, we know that `self` owns a reference, so it is safe to + // use it. + unsafe { bindings::dev_pm_opp_get_voltage(self.as_raw()) } + } + + /// Returns the level of an OPP. + pub fn level(&self) -> u32 { + // SAFETY: By the type invariants, we know that `self` owns a reference, so it is safe to + // use it. + unsafe { bindings::dev_pm_opp_get_level(self.as_raw()) } + } + + /// Returns the power of an OPP. + pub fn power(&self) -> u64 { + // SAFETY: By the type invariants, we know that `self` owns a reference, so it is safe to + // use it. + unsafe { bindings::dev_pm_opp_get_power(self.as_raw()) } + } + + /// Returns the required pstate of an OPP. + pub fn required_pstate(&self, index: u32) -> u32 { + // SAFETY: By the type invariants, we know that `self` owns a reference, so it is safe to + // use it. + unsafe { bindings::dev_pm_opp_get_required_pstate(self.as_raw(), index) } + } + + /// Returns true if the OPP is turbo. + pub fn is_turbo(&self) -> bool { + // SAFETY: By the type invariants, we know that `self` owns a reference, so it is safe to + // use it. + unsafe { bindings::dev_pm_opp_is_turbo(self.as_raw()) } + } +} + +impl Drop for OPP { + fn drop(&mut self) { + // SAFETY: The safety requirements guarantee that the refcount is nonzero. + unsafe { bindings::dev_pm_opp_put(self.as_raw()) } + } +}