From nobody Mon Nov 25 14:54:36 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EFE84216DE1; Fri, 25 Oct 2024 21:06:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729890369; cv=none; b=HMdKQA1oZxKw3xUQfBzlGjI42ATucvQWDzXpqjLEzbdpQjjV/W0CFsrydrNx10HrIHYv1OURgJRNQ/abktq/Nq8AYytFepbBrCbioxKA77oOeraY/Ura6hls4+vIecier+r96CnaYXU5U6Cgx2s319oYPfDWPVNs8el8LMjBI1M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729890369; c=relaxed/simple; bh=yv3Bmr21rRk5eXwZrMp1GWmeP54UQKU8npZAxjm6M6k=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=LeSLv7SXa6tnXL4zkot9fP3I9AB+2BPzmkJGn9NLmI2R4TAtNWY5St5AC9uC3nKwQsUd64ALkP1RozUlcO5M/G1oozNUckAEhVAE3VzGZwdlumMRXC4gCFxXCpKWHDXJ/2zmv8yrbiW0hb0BcB2r5/Bwj0fwuylzmoIx0LeMnMA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=siwXzZBs; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="siwXzZBs" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 77994C4CEC3; Fri, 25 Oct 2024 21:06:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1729890368; bh=yv3Bmr21rRk5eXwZrMp1GWmeP54UQKU8npZAxjm6M6k=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=siwXzZBspz5wDqs9RD5M93sgU+Madl9XI6od1OLT7rIGIGeAJTwA5pJ1kXvGP66kQ gFUHspHSL55CRKrRN++V6PREplLXM0yEH2DytEkIp7TBrLM9kzrdK7DcHLyFYzCrX4 XW94C+peSxBZHZfbFLul6VXJPQ9cKmTkXaZnsS2ulWG4yK1tIyuEb/o6BksERXMg69 gPBnI6zYy0gC7grWubljJ1Y+vg7ZGGPNg9uaSBekATBaVM7d5GN1HPSj5mtrhVB3hS zA6ukvOWrgknz9WPkljFbGHadmx9VVrE8+xO2C2EcCtCJgCunErB9CT0GDrbOSD8kF trypy5yS+Xe+Q== From: "Rob Herring (Arm)" Date: Fri, 25 Oct 2024 16:05:46 -0500 Subject: [PATCH RFC 1/3] of: unittest: Add a platform device node for rust platform driver sample Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241025-rust-platform-dev-v1-1-0df8dcf7c20b@kernel.org> References: <20241025-rust-platform-dev-v1-0-0df8dcf7c20b@kernel.org> In-Reply-To: <20241025-rust-platform-dev-v1-0-0df8dcf7c20b@kernel.org> To: Saravana Kannan , Greg Kroah-Hartman , "Rafael J. Wysocki" , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Dirk Behme Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org X-Mailer: b4 0.15-dev Signed-off-by: Rob Herring (Arm) Reviewed-by: Dirk Behme Tested-by: Dirk Behme --- drivers/of/unittest-data/tests-platform.dtsi | 5 +++++ samples/rust/rust_driver_platform.rs | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/drivers/of/unittest-data/tests-platform.dtsi b/drivers/of/unit= test-data/tests-platform.dtsi index fa39611071b3..2caaf1c10ee6 100644 --- a/drivers/of/unittest-data/tests-platform.dtsi +++ b/drivers/of/unittest-data/tests-platform.dtsi @@ -33,6 +33,11 @@ dev@100 { reg =3D <0x100>; }; }; + + test-device@2 { + compatible =3D "test,rust-device"; + reg =3D <0x2>; + }; }; }; }; diff --git a/samples/rust/rust_driver_platform.rs b/samples/rust/rust_drive= r_platform.rs index 55caaaa4f216..5cf4a8f86c13 100644 --- a/samples/rust/rust_driver_platform.rs +++ b/samples/rust/rust_driver_platform.rs @@ -15,7 +15,7 @@ struct SampleDriver { MODULE_OF_TABLE, ::IdInfo, [( - of::DeviceId::new(c_str!("redhat,rust-sample-platform-driver")), + of::DeviceId::new(c_str!("test,rust-device")), Info(42) )] ); --=20 2.45.2 From nobody Mon Nov 25 14:54:36 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 907A621C2CB; Fri, 25 Oct 2024 21:06:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729890369; cv=none; b=VpysDHUzUQLESubNIs3NFAZq114MSrc0+4MVojlLfiWfi20lsd1w6WAorxjh6UliaLHHHbcreuj+EHL3D7ToTrZCnmWLMat74RGJ/eFz8IV2xToReynYyE2bJMwR8Ev579jC9YQTWbTaXpXWEIJ0EmnTgAOtQm8Jp5k/njMsOaI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729890369; c=relaxed/simple; bh=MTv3LxsYyRYwz6lTAAdJU1iMPfscHo0zUbDJdr9/tPE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nQ65oF98wDFpjd4cM5sf3SXJ473X6I0FFijNojs48b54UhyCDh9XbueMBkpuK/a1HJGzT26lBUCjf5qAfN9xiPTEnivXv3KeE3jfrPtcannXR/qvv6Xwlv0ZnOp5A6lRk4GmT1zk/eqZDYDTWxVU4Kh6jvz4k+m3dEDIVzRUEt0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Xc39/tj7; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Xc39/tj7" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5659AC4CEC3; Fri, 25 Oct 2024 21:06:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1729890369; bh=MTv3LxsYyRYwz6lTAAdJU1iMPfscHo0zUbDJdr9/tPE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Xc39/tj7V0/xyo2Ny5kW6277FYTDGBXRrQgo3QM9930swS359mf8Pt1jFmdGHFNzL xC+ly3PfrC0lROCuN+EhJ73RETsRdosFuDmtZcB63ISlnHQ2yiCEC9OV/0UcM5Femd U//kP3o3289YzFDwo5Q2IoNW02VZH4awhZvTJRxjJUSrehMZNbB/4uV5AH71dUwq2f fU3JFXpTUEcJXYku5WREtweixVnpWAGPWnP4xHfGdWhOq+2mLqZyoelKTzzoyqanZC K69MEacxBRUR8cxPfOR79gA0SXU/TVQU8sx5O5GAK9TYjXpcTLvRc3/hRdj9fy+nGl 232arYqmMWSeA== From: "Rob Herring (Arm)" Date: Fri, 25 Oct 2024 16:05:47 -0500 Subject: [PATCH RFC 2/3] rust: Add bindings for device properties Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241025-rust-platform-dev-v1-2-0df8dcf7c20b@kernel.org> References: <20241025-rust-platform-dev-v1-0-0df8dcf7c20b@kernel.org> In-Reply-To: <20241025-rust-platform-dev-v1-0-0df8dcf7c20b@kernel.org> To: Saravana Kannan , Greg Kroah-Hartman , "Rafael J. Wysocki" , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Dirk Behme Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org X-Mailer: b4 0.15-dev The device property API is a firmware agnostic API for reading properties from firmware (DT/ACPI) devices nodes and swnodes. While the C API takes a pointer to a caller allocated variable/buffer, the rust API is designed to return a value and can be used in struct initialization. Rust generics are also utilized to support different sizes of properties (e.g. u8, u16, u32). Signed-off-by: Rob Herring (Arm) Tested-by: Dirk Behme --- Not sure if we need the KVec variant, but I kept it as that was my first pass attempt. Most callers are filling in some value in a driver data struct. Sometimes the number of elements is not known, so the caller calls to get the array size, allocs the correct size buffer, and then reads the property again to fill in the buffer. I have not implemented a wrapper for device_property_read_string(_array) because that API is problematic for dynamic DT nodes. The API just returns pointer(s) into the raw DT data. We probably need to return a copy of the string(s) instead for rust. After property accessors, next up is child node accessors/iterators. --- rust/bindings/bindings_helper.h | 1 + rust/kernel/device.rs | 145 ++++++++++++++++++++++++++++++++++++= +++- 2 files changed, 145 insertions(+), 1 deletion(-) diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helpe= r.h index 217c776615b9..65717cc20a23 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index 0c28b1e6b004..bb66a28df890 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -5,10 +5,14 @@ //! C header: [`include/linux/device.h`](srctree/include/linux/device.h) =20 use crate::{ + alloc::KVec, bindings, + error::{to_result, Result}, + prelude::*, + str::CStr, types::{ARef, Opaque}, }; -use core::{fmt, ptr}; +use core::{fmt, mem::size_of, ptr}; =20 #[cfg(CONFIG_PRINTK)] use crate::c_str; @@ -189,6 +193,145 @@ unsafe fn printk(&self, klevel: &[u8], msg: fmt::Argu= ments<'_>) { ) }; } + + /// Returns if a firmware property `name` is true or false + pub fn property_read_bool(&self, name: &CStr) -> bool { + unsafe { bindings::device_property_present(self.as_raw(), name.as_= ptr() as *const i8) } + } + + /// Returns if a firmware string property `name` has match for `match_= str` + pub fn property_match_string(&self, name: &CStr, match_str: &CStr) -> = Result { + let ret =3D unsafe { + bindings::device_property_match_string( + self.as_raw(), + name.as_ptr() as *const i8, + match_str.as_ptr() as *const i8, + ) + }; + to_result(ret)?; + Ok(ret as usize) + } + + /// Returns firmware property `name` scalar value + /// + /// Valid types are i8, u8, i16, u16, i32, u32, i64, u64 + pub fn property_read(&self, name: &CStr) -> Result { + let mut val: [T; 1] =3D unsafe { core::mem::zeroed() }; + + Self::_property_read_array(&self, name, &mut val)?; + Ok(val[0]) + } + + /// Returns firmware property `name` array values + /// + /// Valid types are i8, u8, i16, u16, i32, u32, i64, u64 + pub fn property_read_array(&self, name: &CStr) -> R= esult<[T; N]> { + let mut val: [T; N] =3D unsafe { core::mem::zeroed() }; + + Self::_property_read_array(self, name, &mut val)?; + Ok(val) + } + + fn _property_read_array(&self, name: &CStr, val: &mut [T]) -> Resul= t { + match size_of::() { + 1 =3D> to_result(unsafe { + bindings::device_property_read_u8_array( + self.as_raw(), + name.as_ptr() as *const i8, + val.as_ptr() as *mut u8, + val.len(), + ) + })?, + 2 =3D> to_result(unsafe { + bindings::device_property_read_u16_array( + self.as_raw(), + name.as_ptr() as *const i8, + val.as_ptr() as *mut u16, + val.len(), + ) + })?, + 4 =3D> to_result(unsafe { + bindings::device_property_read_u32_array( + self.as_raw(), + name.as_ptr() as *const i8, + val.as_ptr() as *mut u32, + val.len(), + ) + })?, + 8 =3D> to_result(unsafe { + bindings::device_property_read_u64_array( + self.as_raw(), + name.as_ptr() as *const i8, + val.as_ptr() as *mut u64, + val.len(), + ) + })?, + _ =3D> return Err(EINVAL), + } + Ok(()) + } + + pub fn property_read_array_vec(&self, name: &CStr, len: usize) -> R= esult> { + let mut val: KVec =3D KVec::with_capacity(len, GFP_KERNEL)?; + + // SAFETY: len always matches capacity + unsafe { val.set_len(len) } + Self::_property_read_array::(&self, name, val.as_mut_slice())?; + Ok(val) + } + + /// Returns array length for firmware property `name` + /// + /// Valid types are i8, u8, i16, u16, i32, u32, i64, u64 + pub fn property_count_elem(&self, name: &CStr) -> Result { + let ret; + + match size_of::() { + 1 =3D> { + ret =3D unsafe { + bindings::device_property_read_u8_array( + self.as_raw(), + name.as_ptr() as *const i8, + ptr::null_mut(), + 0, + ) + } + } + 2 =3D> { + ret =3D unsafe { + bindings::device_property_read_u16_array( + self.as_raw(), + name.as_ptr() as *const i8, + ptr::null_mut(), + 0, + ) + } + } + 4 =3D> { + ret =3D unsafe { + bindings::device_property_read_u32_array( + self.as_raw(), + name.as_ptr() as *const i8, + ptr::null_mut(), + 0, + ) + } + } + 8 =3D> { + ret =3D unsafe { + bindings::device_property_read_u64_array( + self.as_raw(), + name.as_ptr() as *const i8, + ptr::null_mut(), + 0, + ) + } + } + _ =3D> return Err(EINVAL), + } + to_result(ret)?; + Ok(ret.try_into().unwrap()) + } } =20 // SAFETY: Instances of `Device` are always reference-counted. --=20 2.45.2 From nobody Mon Nov 25 14:54:36 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B945E21C2F1; Fri, 25 Oct 2024 21:06:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729890370; cv=none; b=IYXSzQsSSbVuHDU7Ks7Ill4/VVO3vxkYZ/9upXebFOB6ReB+T0Al6Ejh5gh8QRqO46aYS+euXufJqSkPGVFe5GP0NrfsTHgjRq8oXgO3drR6+qw16RjszCZSz6Go1C5wWfRpd5Z5X58ENoLfiAu2DG10yw5Y5wu6tuGPjtjDfYM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729890370; c=relaxed/simple; bh=SbDhiQQsJeI/5H0yQes94SuNKiHafq1al/d5HAKaqTs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=TnzBjUhFOYBssvUXJhpDVOX7abP80wF9KegfvxZkp+OoFVwtvdaa3HOkUaHSi7sABbMngng+WGJz2M30acTHqRyGDpajXKFRLH4s72LL803eOjADo+k1wwAsMOZ6h6l2AyG9Z80Wxw31rNunWRKD6aMEp41O5IY9w25ZuvjgdvE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dHPY7FII; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="dHPY7FII" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 36BFBC4CEC3; Fri, 25 Oct 2024 21:06:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1729890370; bh=SbDhiQQsJeI/5H0yQes94SuNKiHafq1al/d5HAKaqTs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=dHPY7FIIJaavcX+kyElBfHkBhuIvWKd2QEGKyFTdJDg/q0rtBR0yVJAkFpSZqw+uN VflU2EaCO5jVBwOxb1apowsxT2BFY5vDDY01ArWcXPBbXHgQQ7tLW6OZckUaFdPj07 hmgqK2eap9ILI4fFF+LTsoT2YyrIrSokJM+uVm34qyqT/WU8ROTdUc2N9+7wdau3Ul ocyrFupWE+4AthipacC2YQIw8JwS4J4mwonhW1Wuta6lW53w3SYK3D7/r0yUyMXbd5 cIi6d6U1JCPMLhYPGzKwHr5vnXKBASRwu0OdDG9Ui5ZlSEaJKmXmVvGhtN2/LolMG9 ie5v6kYu+TkHw== From: "Rob Herring (Arm)" Date: Fri, 25 Oct 2024 16:05:48 -0500 Subject: [PATCH RFC 3/3] samples: rust: platform: Add property read examples Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241025-rust-platform-dev-v1-3-0df8dcf7c20b@kernel.org> References: <20241025-rust-platform-dev-v1-0-0df8dcf7c20b@kernel.org> In-Reply-To: <20241025-rust-platform-dev-v1-0-0df8dcf7c20b@kernel.org> To: Saravana Kannan , Greg Kroah-Hartman , "Rafael J. Wysocki" , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Dirk Behme Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org X-Mailer: b4 0.15-dev Add some example usage of the device property read methods for DT/ACPI/swnode properties. Signed-off-by: Rob Herring (Arm) Tested-by: Dirk Behme --- drivers/of/unittest-data/tests-platform.dtsi | 3 +++ samples/rust/rust_driver_platform.rs | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/drivers/of/unittest-data/tests-platform.dtsi b/drivers/of/unit= test-data/tests-platform.dtsi index 2caaf1c10ee6..a5369b9343b8 100644 --- a/drivers/of/unittest-data/tests-platform.dtsi +++ b/drivers/of/unittest-data/tests-platform.dtsi @@ -37,6 +37,9 @@ dev@100 { test-device@2 { compatible =3D "test,rust-device"; reg =3D <0x2>; + + test,u32-prop =3D <0xdeadbeef>; + test,i16-array =3D /bits/ 16 <1 2 (-3) (-4)>; }; }; }; diff --git a/samples/rust/rust_driver_platform.rs b/samples/rust/rust_drive= r_platform.rs index 5cf4a8f86c13..95c290806862 100644 --- a/samples/rust/rust_driver_platform.rs +++ b/samples/rust/rust_driver_platform.rs @@ -41,6 +41,28 @@ fn probe(pdev: &mut platform::Device, info: Option<&Self= ::IdInfo>) -> Result(c_str!("test,u32-prop"))?; + let prop: u32 =3D dev.property_read(c_str!("test,u32-prop"))?; + dev_info!(dev, "'test,u32-prop' is {:#x}\n", prop); + + let prop: [i16; 4] =3D dev.property_read_array(c_str!("test,i16-ar= ray"))?; + dev_info!(dev, "'test,i16-array' is {:?}\n", prop); + dev_info!( + dev, + "'test,i16-array' length is {}\n", + dev.property_count_elem::(c_str!("test,i16-array")) + .unwrap() + ); + let drvdata =3D KBox::new(Self { pdev: pdev.clone() }, GFP_KERNEL)= ?; =20 Ok(drvdata.into()) --=20 2.45.2