Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp907019lqt; Fri, 7 Jun 2024 02:13:37 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWl1mivRwHKXERDm0a++0vh4kV/mmjfIfjHnEQWDtg2lgTf8S4dohI0h4i2XSCqWySrZr9cbXbygxyoYubqAneMd7Dq5rOz9F1uhPCWFQ== X-Google-Smtp-Source: AGHT+IGxJ7hF8noYsfQrQUUyYg2wrVL+my69aJKH69bg7M8vQGr9Xr7hOFneW7vYf3wIJggRIgsl X-Received: by 2002:ac8:53c5:0:b0:440:47d9:d304 with SMTP id d75a77b69052e-44047d9d5edmr7116591cf.5.1717751617216; Fri, 07 Jun 2024 02:13:37 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717751617; cv=pass; d=google.com; s=arc-20160816; b=lVMaMwIFevhswBr7bA/juLdrPQD1w2nJGl+TVOw5yTd10Wu2R+S9PbF5nO6c5SY+Js 6jNaALqoZXBb1BCEQwy8TpSHjZ01OzObsNiiO5+N8ZX9CWSbHAbKFz4Fj6DyZ1W3rPq4 wLEBBfQbqTXOOY6jz4Z0ZMVzkqZhufEFP0ilWDLsI5gmWQst8C/uf5Z5knVlpaw4f/P7 whCmhZrGwCwKtsVBS/gDJXMiv2jpdKDpr+VZ0dqGJ1ggS1QtJyl4fEYqoQRtLkyZBNZR WZqCqxodPro0/lO/TcSubSEsg47LfngVSxUqhymUuUtaaepfqeye0VPpdfi0nNNjHM0t kh0Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=zFgEQc5AxyZLZc/ZKTPUL3s2+RpIEiR6EvwV6W5lrPI=; fh=KGE46j86eDWfaqPCa6dMqy0U/MpSJ73PVH54doDMLgM=; b=IJCoczlvLHOpr8qYIXpx/6/Rrfj0Q555OvCnRbXoompvioqcMKuQ5FzK9XnXLjmqMJ ISGkgF5ZTxthJKjk69vMXWfoRJ3zcdS0sI9a8xyqtqjUFdhXcpp1gtXbtcMB4ptWyY0H V23Px0YZSUwqR9VR7y5E18d/bzC9NNceERACOuCqVHMYNzTpKIIoZenBF7cXr7WRiWAN fkWqzwJVVAhz+IvnnPPZkzbHCJG6tgLfLCdBIUAe//otAcon75mrHnr7LnuEZSJtAJ5/ tVJEpiz3m/rGqTH96Z6rYyz/cOEYF7MZaw+kMKxARRETtzjcdq6lRtcTk/zdaFnlV5jt 2RQg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=J1T8ZkTE; arc=pass (i=1 spf=pass spfdomain=linaro.org dkim=pass dkdomain=linaro.org dmarc=pass fromdomain=linaro.org); spf=pass (google.com: domain of linux-kernel+bounces-205692-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205692-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d75a77b69052e-44038b41558si36767491cf.512.2024.06.07.02.13.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 02:13:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-205692-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=J1T8ZkTE; arc=pass (i=1 spf=pass spfdomain=linaro.org dkim=pass dkdomain=linaro.org dmarc=pass fromdomain=linaro.org); spf=pass (google.com: domain of linux-kernel+bounces-205692-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205692-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id C44DC1C214D1 for ; Fri, 7 Jun 2024 09:13:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3CDDF15CD75; Fri, 7 Jun 2024 09:12:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="J1T8ZkTE" Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (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 3CA8915B996 for ; Fri, 7 Jun 2024 09:12:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717751569; cv=none; b=Tousyt/IJ0HbFOeaRhpfPft7J7s/l64c2LDrBpdQduSBFCmhLMdljA72SYiDR5UrNXBPx/ZRJ3mkiRbgF2r5fHP+DKz3fxXyqqjZS8cb3kgfBSWO6Y0KwhkGi8LsH6Z0ZQ5PI7sLEnoT0rV4JxE4VRX1RX2w9mKFUWCeIFeBFmM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717751569; c=relaxed/simple; bh=yQbu2BzvfcyL06wty0oKxJIOgxPxe0NA+Oypu+rr8gs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=K78G6zRgd+oXUm9j/YhNbdtPRoHZUoFQa6sCJlToJUj7h3MQ3V9tJMSzBONGhPG9mjx+0iyFfYnQS9yclMT1+m7Es1o0Ratvnlf63LuoRoqqVaU3PNSDQpxjshCt7T+smAJ6uRTBMdOOT28FZmiARoCc270G3Zbezze57tXQvRs= 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=J1T8ZkTE; arc=none smtp.client-ip=209.85.214.182 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 Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1f6da06ba24so2968605ad.2 for ; Fri, 07 Jun 2024 02:12:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1717751567; x=1718356367; 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=zFgEQc5AxyZLZc/ZKTPUL3s2+RpIEiR6EvwV6W5lrPI=; b=J1T8ZkTELA5SI9D4hxC0pkjO6v3bd9v7W11iG/AmMtR0EZFLwxbINxtrs+Hk8PtX0I Pdad2urIax4uaRGxOcMbZYIBCj6T7d/R39n1i3VnSbzKXN1XIpU6bEFlDdRV//nleYXH VU6Mdi39oopr/8hCqgsLxYxXQuuJWwApMalmCdT9h0HprKNl4NFvEYPkdRbnxU1patbu 0gXt08cyOu0nhe3QB55r3vJKwgei1ZVVuPV0LF4aX2j3qFxErMIEPgXT1py/K64Xnk76 sqcLcJKMY+ZiATmd6rNoqGJfrSeJfIpCAeXJ7FMsSS/pMO+92FZGsZ5ymMa2tEihrCbm fKbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717751567; x=1718356367; 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=zFgEQc5AxyZLZc/ZKTPUL3s2+RpIEiR6EvwV6W5lrPI=; b=Zxr1TaiLpCOXuSt0Z+W5v4BvSNS1R+IVb7xLDpuoP3M+ox87mMB6WVXJjjTm6oi2RS 3lECip7XEQDC73UKJAOa4UFP8O1g1cbsOajMllM7SYG0zD5s6FWtdzKif1UksDQhXjyq mRoef45U47rJWvOqa4PeslLrPTvRmC9Qg8z5C+zhuyGfyOIoIr2uC6XNW6nqfGaTXDos BrbKurwhC4FRrYyvFG4hun4kjwa2jjja90H4FUfIbOKilrot2cs5F42NgqAC+Fvqn/+O jw1hbJVuGvG4NiAJEZpB0gz+PaG0L476GCB/SAvMSd882n7jhXKLRnCGNUdjrj40lM6u rW2Q== X-Forwarded-Encrypted: i=1; AJvYcCWFNvlJqT8Fqf8pjM4pgRtzzbPUeBK3iKuxjZ46ZeehnThaTFO44wAZwKqM3cTk7Im+kEl/l4O/dnx5L8HOxpfIbirjbfbYVNNK7qpq X-Gm-Message-State: AOJu0YyhhU+XeR0QIMZ5m+J9rhARw/18upfIccSmPdzahRQtgbsoFvAN eXYknCJxSwPbaB3NiECY2d0EjqI0HIgGbBxYNZ/qcGxAHPufJ/eO2vTjJcd/sTE= X-Received: by 2002:a17:903:984:b0:1f6:8235:dbbc with SMTP id d9443c01a7336-1f6d02f4747mr24505395ad.29.1717751567374; Fri, 07 Jun 2024 02:12:47 -0700 (PDT) Received: from localhost ([122.172.82.13]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f6bd7fd6cfsm29198765ad.274.2024.06.07.02.12.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 02:12: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=20Roy=20Baron?= , 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=20Benn=C3=A9e?= , Joakim Bech , Rob Herring , linux-kernel@vger.kernel.org Subject: [RFC PATCH V2 3/8] rust: Extend OPP bindings for the configuration options Date: Fri, 7 Jun 2024 14:42:18 +0530 Message-Id: <37bdd7ee7b74292830dd9977154b08ebcf08a138.1717750631.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.31.1.272.g89b43f80a514 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@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. Signed-off-by: Viresh Kumar --- rust/kernel/opp.rs | 305 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 303 insertions(+), 2 deletions(-) diff --git a/rust/kernel/opp.rs b/rust/kernel/opp.rs index 06f36845047f..ebcb77b52558 100644 --- a/rust/kernel/opp.rs +++ b/rust/kernel/opp.rs @@ -9,11 +9,28 @@ use crate::{ bindings, 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: &Vec) -> Result> { + // 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) +} /// Equivalent to `struct dev_pm_opp_data` in the C Code. #[repr(transparent)] @@ -42,6 +59,290 @@ 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: ARef, + _table: &Table, + _opp: ARef, + _scaling_down: bool, + ) -> Result<()> { + kernel::build_error(VTABLE_DEFAULT_ERROR) + } + + /// Called by the OPP core to configure OPP regulators. + fn config_regulators( + _dev: ARef, + _opp_old: ARef, + _opp_new: ARef, + _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. +pub struct Config { + clk_names: Option>, + prop_name: Option, + regulator_names: Option>, + genpd_names: Option>, + supported_hw: Option>, + required_devs: 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, + genpd_names: None, + supported_hw: None, + required_devs: None, + _data: PhantomData, + } + } + + /// Initializes clock names. + pub fn set_clk_names(mut self, names: Vec) -> 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: Vec) -> 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 genpd names. + pub fn set_genpd_names(mut self, names: Vec) -> Result { + // 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>) -> Result { + // 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) -> 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: ARef) -> 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 (_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.clone(), + // SAFETY: 'opp_table' is guaranteed by the C code to be valid. + &unsafe { Table::from_ptr(opp_table, dev) }, + // SAFETY: 'opp' is guaranteed by the C code to be valid. + unsafe { OPP::from_ptr(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_ptr(old_opp)? }, + // SAFETY: 'new_opp' is guaranteed by the C code to be valid. + unsafe { OPP::from_ptr(new_opp)? }, + regulators, + count, + ) + .map(|_| 0) + }) + } +} + /// Operating performance point (OPP) table. /// /// # Invariants -- 2.31.1.272.g89b43f80a514