From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (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 C4B9A226554 for ; Thu, 6 Feb 2025 09:29:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738834171; cv=none; b=r3n62SxrxpgRO5QhE5Sb3JIuBqYEA0q5T+AwcJg46f28MHWScTtkyfpucW4bRBoTj8e7EOgK+S2ZK2/gVxmtXIb1RNhFH94sGGlxxuuwRhOkErvRmX7uVgJf9DRRNzw2tiV12oVCOB8ZEjs9gsqRYfndxP9OGFylMdUqL/9lkmM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738834171; c=relaxed/simple; bh=Btyip5cttvaDpQ47Hobx3+HFANarQIo3DBo0MaIGqeo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=h90f94VQZ8+SRQPmau6TlSShy8TurmKu/lg3oPpiNQFYw7tdXY6TPMg19HMrZdpBVMnuzhYyYiXa/nldVPrAK5jV/XLuTfT3c96IcwcjVMCUDC3n3Cw32HkVjhpLGEYnCKwHQdKdIQ1WQwxpxXzvz3p7Zwi4nkAbK/iEIl3O/GQ= 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=u1LDwLou; arc=none smtp.client-ip=209.85.214.176 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="u1LDwLou" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-21661be2c2dso12919085ad.1 for ; Thu, 06 Feb 2025 01:29:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1738834168; x=1739438968; 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=SCUMLmrIOnRhbVUxbyJm2yfzH22vl8wPDByGbPiWd2g=; b=u1LDwLou7bY0gbt3sI7wKyY873eAsAJyJtEaPltZHeMETsh3CPeRNci3ZrT/HzcI7h bJ+6BfrbvVj/p9RenwbTBpyIJ6RiuYBaW5b+0kXWM1uI4RHERj7UENOH5hhrkc9SbVX3 e6+mX2QiJYzGxrB3qb97uYrFlYEXQN1hVuNnpW2XxyET/nOnRw4gzccCA6eZlUrV9zsn JpOHvs7e7TVjwyRXq8o6yhmeRgVK69xmXiimow6POEwyVN00bdI/GMuupyoKnnO4gNt/ cgBSqKzKH/PeXAT59g6z7+DoiihZzOfgGdMii+PIfhQUOxT+wgnLzxZcgwWIZrzNvyiN qYXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738834168; x=1739438968; 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=SCUMLmrIOnRhbVUxbyJm2yfzH22vl8wPDByGbPiWd2g=; b=X5CNG6rw5jUU2ffWKkgh2OOHX3JLZA1f0Y1nog7tvCMD4ZP8wyMC9QfIsqPDFu2roG 4FtLRCPdWFVllGl0+TED+nms6/WkbZVrFBLeyebEEpUQvpOaZ5xzrKa0d1sIVZV52wyc CEbxhPKbzj8K7+n2nDIE8fOltwNSjutUmsMlsMH7tSnYMASV0fEAs2Hq3dsOth/Lci+/ LfMBM462P9lS9YCNvQHWnA6457JQabMfigvb3tigEntd33zbljjBdMqfY5NrCjbVUPo9 m/ppqFN2bmfflsEzGf1AImQCQuB/mT5PSjcDDUUQ9uaCMYc+zkHf2+D7G3tNqGHB6VWU gXDg== X-Forwarded-Encrypted: i=1; AJvYcCUWDgZHSJeWNkGO3nsz08zm1WvDf3kDx8pdZHaRkK1za+kYG7YTlhtlFHDFuQVXsu9+TD+18mI8ZsBd5l+iQA==@vger.kernel.org X-Gm-Message-State: AOJu0Yx8VJFjPLHrdsDsp8MfmhWsEHUXMNm9+2J5NU7uI0RVNDFVMmWp Nw4LPnFYL61scvN9A3zrX6PcJn/EFwP8QYP3iXVcCOMf0ihxvtot/HJnrpr3FrE= X-Gm-Gg: ASbGncudykN1y/HvdaS2raKVmKOtlgtiUrqcJBBqHmz6k6LDjYLrurt6B4nXwTYmfLv 5goedzHiyygImcpY9VxtWYxB4GICuqMaZwQM+S97TDSuKXEgh6YYchMnywsATcik8oXmi4V/+zB 5MBrfY+Xx2TOjxtDKbST/sMlq9jyeBSPdc8DUrGCWWoSIe1t/9wfEdoxPBRY3Uaozs63jnjrX7j EoNY+4NE4R8lsW3LoKRpIpp4elf2r4IRq6VaeCS7OvDlXp15YYoWGFVsVX+9ht1EoPmz8t3p7/I J9YvpZKiqEb9mwxZ7Q== X-Google-Smtp-Source: AGHT+IHmOhiNOHtcB647gHK2pP1h0sRGNI8hEpCMxo+dj0zatC2leoJLviSo0fMpbheyGPKxBwoovQ== X-Received: by 2002:a05:6a21:8dca:b0:1e1:ae4a:1d42 with SMTP id adf61e73a8af0-1ede88ad419mr13625445637.31.1738834168119; Thu, 06 Feb 2025 01:29:28 -0800 (PST) Received: from localhost ([122.172.84.139]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-ad51aeb8d09sm822696a12.15.2025.02.06.01.29.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Feb 2025 01:29:27 -0800 (PST) From: Viresh Kumar To: "Rafael J. Wysocki" , Miguel Ojeda , Danilo Krummrich , Viresh Kumar , Nishanth Menon , Stephen Boyd , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Cc: Viresh Kumar , linux-pm@vger.kernel.org, Vincent Guittot , rust-for-linux@vger.kernel.org, Manos Pitsidianakis , Erik Schilling , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Joakim Bech , Rob Herring , linux-kernel@vger.kernel.org Subject: [PATCH V8 09/14] rust: Extend OPP bindings for the configuration options Date: Thu, 6 Feb 2025 14:58:30 +0530 Message-Id: X-Mailer: git-send-email 2.31.1.272.g89b43f80a514 In-Reply-To: References: Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit This extends OPP bindings with the bindings for the OPP core configuration options. Reviewed-by: Manos Pitsidianakis Signed-off-by: Viresh Kumar --- rust/kernel/opp.rs | 262 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 260 insertions(+), 2 deletions(-) diff --git a/rust/kernel/opp.rs b/rust/kernel/opp.rs index c0daeadfb188..8028245e94aa 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::{marker::PhantomData, ptr}; + +use macros::vtable; + +// Creates a null-terminated slice of pointers to Cstrings. +fn to_c_str_array(names: &[CString]) -> Result> { + // Allocated a null-terminated vector of pointers. + let mut list = KVec::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,247 @@ 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<()> { + 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<()> { + 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: Its safe to return the configuration token number previously received from the C + // code. + 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 { + clk_names: Option>, + prop_name: Option, + regulator_names: Option>, + supported_hw: Option>, + required_dev: Option>, + required_dev_index: Option, + _data: PhantomData, +} + +impl Config { + /// Creates a new instance of [`Config`]. + pub fn new() -> Self { + Self { + clk_names: None, + prop_name: None, + regulator_names: None, + supported_hw: None, + required_dev: None, + required_dev_index: None, + _data: PhantomData, + } + } + + /// Initializes clock names. + pub fn set_clk_names(mut self, names: KVec) -> Result { + // 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 { + // 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: KVec) -> Result { + // 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 required devices. + pub fn set_required_dev(mut self, dev: ARef, index: u32) -> Result { + // Already configured. + if self.required_dev.is_some() { + return Err(EBUSY); + } + + self.required_dev = Some(dev); + self.required_dev_index = Some(index); + Ok(self) + } + + /// Initializes supported hardware. + pub fn set_supported_hw(mut self, hw: KVec) -> Result { + // 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 { + 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 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_dev, required_dev_index) = match &self.required_dev { + Some(x) => (x.as_raw(), self.required_dev_index.unwrap()), + None => (ptr::null_mut(), 0), + }; + + 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 + }, + supported_hw, + supported_hw_count, + + required_dev, + required_dev_index, + }; + + // 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::get_device(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::get_device(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. /// /// Wraps the kernel's `struct opp_table`. -- 2.31.1.272.g89b43f80a514