* [PATCH v1 1/5] rust: acpi: add `acpi::DeviceId` abstraction
2025-06-04 12:29 [PATCH v1 0/5] rust: Add ACPI match table support for Rust drivers Igor Korotin
@ 2025-06-04 12:29 ` Igor Korotin
2025-06-04 12:29 ` [PATCH v1 2/5] rust: helpers: Add `is_of_node` helper function Igor Korotin
` (2 subsequent siblings)
3 siblings, 0 replies; 7+ messages in thread
From: Igor Korotin @ 2025-06-04 12:29 UTC (permalink / raw)
To: Miguel Ojeda, Alex Gaynor, Rob Herring, Saravana Kannan,
Rafael J . Wysocki, Greg Kroah-Hartman, rust-for-linux,
linux-kernel, linux-acpi, devicetree
Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
Len Brown, Viresh Kumar, Wedson Almeida Filho, Alex Hung,
Tamir Duberstein, FUJITA Tomonori, Xiangfei Ding, Igor Korotin
`acpi::DeviceId` is an abstraction around `struct acpi_device_id`.
This is used by subsequent patches, in particular the i2c driver
abstractions, to create ACPI device ID tables.
Signed-off-by: Igor Korotin <igor.korotin.linux@gmail.com>
---
MAINTAINERS | 1 +
rust/kernel/acpi.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++
rust/kernel/lib.rs | 1 +
3 files changed, 64 insertions(+)
create mode 100644 rust/kernel/acpi.rs
diff --git a/MAINTAINERS b/MAINTAINERS
index b659fb27ab63..5f8dfae08454 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -302,6 +302,7 @@ F: include/linux/acpi.h
F: include/linux/fwnode.h
F: include/linux/fw_table.h
F: lib/fw_table.c
+F: rust/kernel/acpi.rs
F: tools/power/acpi/
ACPI APEI
diff --git a/rust/kernel/acpi.rs b/rust/kernel/acpi.rs
new file mode 100644
index 000000000000..2f526a0b2ed9
--- /dev/null
+++ b/rust/kernel/acpi.rs
@@ -0,0 +1,62 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Advanced Configuration and Power Interface abstractions.
+
+use crate::{bindings, device_id::RawDeviceId, prelude::*};
+
+/// IdTable type for ACPI drivers.
+pub type IdTable<T> = &'static dyn kernel::device_id::IdTable<DeviceId, T>;
+
+/// An ACPI device id.
+#[repr(transparent)]
+#[derive(Clone, Copy)]
+pub struct DeviceId(bindings::acpi_device_id);
+
+// SAFETY:
+// * `DeviceId` is a `#[repr(transparent)` wrapper of `struct acpi_device_id` and does not add
+// additional invariants, so it's safe to transmute to `RawType`.
+// * `DRIVER_DATA_OFFSET` is the offset to the `data` field.
+unsafe impl RawDeviceId for DeviceId {
+ type RawType = bindings::acpi_device_id;
+
+ const DRIVER_DATA_OFFSET: usize = core::mem::offset_of!(bindings::acpi_device_id, driver_data);
+
+ fn index(&self) -> usize {
+ self.0.driver_data as _
+ }
+}
+
+impl DeviceId {
+ const ACPI_ID_LEN: usize = 16;
+
+ /// Create a new device id from an ACPI 'id' string.
+ pub const fn new(id: &'static CStr) -> Self {
+ assert!(id.len() <= Self::ACPI_ID_LEN, "ID exceeds 16 bytes");
+ let src = id.as_bytes_with_nul();
+ // Replace with `bindings::acpi_device_id::default()` once stabilized for `const`.
+ // SAFETY: FFI type is valid to be zero-initialized.
+ let mut acpi: bindings::acpi_device_id = unsafe { core::mem::zeroed() };
+ let mut i = 0;
+ while i < src.len() {
+ acpi.id[i] = src[i];
+ i += 1;
+ }
+
+ Self(acpi)
+ }
+}
+
+/// Create an ACPI `IdTable` with an "alias" for modpost.
+#[macro_export]
+macro_rules! acpi_device_table {
+ ($table_name:ident, $module_table_name:ident, $id_info_type: ty, $table_data: expr) => {
+ const $table_name: $crate::device_id::IdArray<
+ $crate::acpi::DeviceId,
+ $id_info_type,
+ { $table_data.len() },
+ > = $crate::device_id::IdArray::new($table_data);
+
+ $crate::module_device_table!("acpi", $module_table_name, $table_name);
+ };
+}
+
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 6e9287136cac..c2761bc7cfe6 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -51,6 +51,7 @@
pub use ffi;
+pub mod acpi;
pub mod alloc;
#[cfg(CONFIG_BLOCK)]
pub mod block;
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v1 2/5] rust: helpers: Add `is_of_node` helper function
2025-06-04 12:29 [PATCH v1 0/5] rust: Add ACPI match table support for Rust drivers Igor Korotin
2025-06-04 12:29 ` [PATCH v1 1/5] rust: acpi: add `acpi::DeviceId` abstraction Igor Korotin
@ 2025-06-04 12:29 ` Igor Korotin
2025-06-04 12:29 ` [PATCH v1 4/5] rust: platform: Add ACPI match table support to `Driver` trait Igor Korotin
2025-06-04 21:34 ` [PATCH v1 0/5] rust: Add ACPI match table support for Rust drivers Danilo Krummrich
3 siblings, 0 replies; 7+ messages in thread
From: Igor Korotin @ 2025-06-04 12:29 UTC (permalink / raw)
To: Miguel Ojeda, Alex Gaynor, Rob Herring, Saravana Kannan,
Rafael J . Wysocki, Greg Kroah-Hartman, rust-for-linux,
linux-kernel, linux-acpi, devicetree
Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
Len Brown, Viresh Kumar, Wedson Almeida Filho, Alex Hung,
Tamir Duberstein, FUJITA Tomonori, Xiangfei Ding, Igor Korotin
Add a helper function to determine whether a given device appears to be
an Open Firmware (OF) node.
This function will be used in Rust driver abstractions to support both
ACPI and OF matching simultaneously in subsequent patches.
Signed-off-by: Igor Korotin <igor.korotin.linux@gmail.com>
---
MAINTAINERS | 1 +
rust/helpers/helpers.c | 1 +
rust/helpers/of.c | 6 ++++++
3 files changed, 8 insertions(+)
create mode 100644 rust/helpers/of.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 5f8dfae08454..d6cadaa592aa 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -18215,6 +18215,7 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git
F: Documentation/ABI/testing/sysfs-firmware-ofw
F: drivers/of/
F: include/linux/of*.h
+F: rust/helpers/of.c
F: rust/kernel/of.rs
F: scripts/dtc/
F: tools/testing/selftests/dt/
diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c
index 80785b1e7a63..2fc5242aa47a 100644
--- a/rust/helpers/helpers.c
+++ b/rust/helpers/helpers.c
@@ -21,6 +21,7 @@
#include "jump_label.c"
#include "kunit.c"
#include "mutex.c"
+#include "of.c"
#include "page.c"
#include "platform.c"
#include "pci.c"
diff --git a/rust/helpers/of.c b/rust/helpers/of.c
new file mode 100644
index 000000000000..8913eaced716
--- /dev/null
+++ b/rust/helpers/of.c
@@ -0,0 +1,6 @@
+#include <linux/of.h>
+
+bool rust_helper_is_of_node(const struct fwnode_handle *fwnode)
+{
+ return is_of_node(fwnode);
+}
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v1 4/5] rust: platform: Add ACPI match table support to `Driver` trait
2025-06-04 12:29 [PATCH v1 0/5] rust: Add ACPI match table support for Rust drivers Igor Korotin
2025-06-04 12:29 ` [PATCH v1 1/5] rust: acpi: add `acpi::DeviceId` abstraction Igor Korotin
2025-06-04 12:29 ` [PATCH v1 2/5] rust: helpers: Add `is_of_node` helper function Igor Korotin
@ 2025-06-04 12:29 ` Igor Korotin
2025-06-04 21:34 ` [PATCH v1 0/5] rust: Add ACPI match table support for Rust drivers Danilo Krummrich
3 siblings, 0 replies; 7+ messages in thread
From: Igor Korotin @ 2025-06-04 12:29 UTC (permalink / raw)
To: Miguel Ojeda, Alex Gaynor, Rob Herring, Saravana Kannan,
Rafael J . Wysocki, Greg Kroah-Hartman, rust-for-linux,
linux-kernel, linux-acpi, devicetree
Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
Len Brown, Viresh Kumar, Wedson Almeida Filho, Alex Hung,
Tamir Duberstein, FUJITA Tomonori, Xiangfei Ding, Igor Korotin
Extend the `platform::Driver` trait to support ACPI device matching by
adding the `ACPI_ID_TABLE` constant.
This allows Rust platform drivers to define ACPI match tables alongside
their existing OF match tables.
These changes mirror the existing OF support and allow Rust platform
drivers to match devices based on ACPI identifiers.
To avoid breaking compilation, a stub ACPI match table definition is
added to the Rust sample platform driver. Functional support for ACPI
matching in the sample driver will be provided in a follow-up patch.
Signed-off-by: Igor Korotin <igor.korotin.linux@gmail.com>
---
rust/kernel/platform.rs | 14 ++++++++++++--
samples/rust/rust_driver_platform.rs | 3 ++-
2 files changed, 14 insertions(+), 3 deletions(-)
diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs
index 3cc9fe6ccfcf..e6cc878a5a37 100644
--- a/rust/kernel/platform.rs
+++ b/rust/kernel/platform.rs
@@ -39,12 +39,18 @@ unsafe fn register(
None => core::ptr::null(),
};
+ let acpi_table = match T::ACPI_ID_TABLE {
+ Some(table) => table.as_ptr(),
+ None => core::ptr::null(),
+ };
+
// SAFETY: It's safe to set the fields of `struct platform_driver` on initialization.
unsafe {
(*pdrv.get()).driver.name = name.as_char_ptr();
(*pdrv.get()).probe = Some(Self::probe_callback);
(*pdrv.get()).remove = Some(Self::remove_callback);
(*pdrv.get()).driver.of_match_table = of_table;
+ (*pdrv.get()).driver.acpi_match_table = acpi_table;
}
// SAFETY: `pdrv` is guaranteed to be a valid `RegType`.
@@ -98,7 +104,7 @@ fn of_id_table() -> Option<of::IdTable<Self::IdInfo>> {
}
fn acpi_id_table() -> Option<acpi::IdTable<Self::IdInfo>> {
- None
+ T::ACPI_ID_TABLE
}
}
@@ -129,7 +135,7 @@ macro_rules! module_platform_driver {
/// # Example
///
///```
-/// # use kernel::{bindings, c_str, device::Core, of, platform};
+/// # use kernel::{acpi, bindings, c_str, device::Core, of, platform};
///
/// struct MyDriver;
///
@@ -145,6 +151,7 @@ macro_rules! module_platform_driver {
/// impl platform::Driver for MyDriver {
/// type IdInfo = ();
/// const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
+/// const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
///
/// fn probe(
/// _pdev: &platform::Device<Core>,
@@ -165,6 +172,9 @@ pub trait Driver: Send {
/// The table of OF device ids supported by the driver.
const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>>;
+ /// The table of ACPI device ids supported by the driver.
+ const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>>;
+
/// Platform driver probe.
///
/// Called when a new platform device is added or discovered.
diff --git a/samples/rust/rust_driver_platform.rs b/samples/rust/rust_driver_platform.rs
index 8b42b3cfb363..e3992e7a71e9 100644
--- a/samples/rust/rust_driver_platform.rs
+++ b/samples/rust/rust_driver_platform.rs
@@ -2,7 +2,7 @@
//! Rust Platform driver sample.
-use kernel::{c_str, device::Core, of, platform, prelude::*, types::ARef};
+use kernel::{acpi, c_str, device::Core, of, platform, prelude::*, types::ARef};
struct SampleDriver {
pdev: ARef<platform::Device>,
@@ -20,6 +20,7 @@ struct SampleDriver {
impl platform::Driver for SampleDriver {
type IdInfo = Info;
const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
+ const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = None;
fn probe(
pdev: &platform::Device<Core>,
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v1 0/5] rust: Add ACPI match table support for Rust drivers
2025-06-04 12:29 [PATCH v1 0/5] rust: Add ACPI match table support for Rust drivers Igor Korotin
` (2 preceding siblings ...)
2025-06-04 12:29 ` [PATCH v1 4/5] rust: platform: Add ACPI match table support to `Driver` trait Igor Korotin
@ 2025-06-04 21:34 ` Danilo Krummrich
2025-06-05 13:57 ` Igor Korotin
3 siblings, 1 reply; 7+ messages in thread
From: Danilo Krummrich @ 2025-06-04 21:34 UTC (permalink / raw)
To: Igor Korotin
Cc: Miguel Ojeda, Alex Gaynor, Rob Herring, Saravana Kannan,
Rafael J . Wysocki, Greg Kroah-Hartman, rust-for-linux,
linux-kernel, linux-acpi, devicetree, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Len Brown, Viresh Kumar, Wedson Almeida Filho,
Alex Hung, Tamir Duberstein, FUJITA Tomonori, Xiangfei Ding
On Wed, Jun 04, 2025 at 01:29:39PM +0100, Igor Korotin wrote:
> This patch series introduces support for ACPI match tables in Rust
> drivers.
>
> Currently, Rust abstractions support only Open Firmware (OF) device
> matching. This series extends the driver model to support ACPI-based
> matching, enabling Rust drivers to bind to ACPI-described devices.
>
> Changes include:
> - A new `acpi::DeviceId` abstraction for working with
> `struct acpi_device_id`.
> - A helper function `is_of_node()` for determining fwnode types.
> - Updates to the core `Adapter` trait and `platform::Driver` to support
> optional ACPI ID tables.
> - A sample implementation in the Rust platform driver, demonstrating
> multi-bus matching.
>
> This is especially useful for writing drivers that work across platforms
> using both OF and ACPI.
>
> Tested using QEMU with a custom SSDT that creates an ACPI device matching
> the sample Rust platform driver.
Thanks this is great!
Unfortunately, it seems that something went wrong sending this patch series.
Patches 3 and 5 are missing on my end (and on the corresponding lists as well).
Can you please resend?
Also, technically this series is a v2; patch 1 differs from the one you sent
originally -- please include a changelog.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v1 0/5] rust: Add ACPI match table support for Rust drivers
2025-06-04 21:34 ` [PATCH v1 0/5] rust: Add ACPI match table support for Rust drivers Danilo Krummrich
@ 2025-06-05 13:57 ` Igor Korotin
2025-06-05 14:34 ` Danilo Krummrich
0 siblings, 1 reply; 7+ messages in thread
From: Igor Korotin @ 2025-06-05 13:57 UTC (permalink / raw)
To: Danilo Krummrich, Igor Korotin
Cc: Miguel Ojeda, Alex Gaynor, Rob Herring, Saravana Kannan,
Rafael J . Wysocki, Greg Kroah-Hartman, rust-for-linux,
linux-kernel, linux-acpi, devicetree, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Len Brown, Viresh Kumar, Wedson Almeida Filho,
Alex Hung, Tamir Duberstein, FUJITA Tomonori, Xiangfei Ding
> Thanks this is great!
>
> Unfortunately, it seems that something went wrong sending this patch series.
> Patches 3 and 5 are missing on my end (and on the corresponding lists as well).
Yes, I know. Gmail has blocked me from sending any letters. I probably hit
some limit. It will likely unblock me sometime today or tomorrow.
> Also, technically this series is a v2; patch 1 differs from the one you sent
> originally -- please include a changelog.
Because of this Gmail issue, I need to clarify the right course of
action to avoid being blocked again:
Should I resend the whole series as PATCH v2 with a changelog?
Thanks
Igor
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v1 0/5] rust: Add ACPI match table support for Rust drivers
2025-06-05 13:57 ` Igor Korotin
@ 2025-06-05 14:34 ` Danilo Krummrich
0 siblings, 0 replies; 7+ messages in thread
From: Danilo Krummrich @ 2025-06-05 14:34 UTC (permalink / raw)
To: Igor Korotin
Cc: Igor Korotin, Miguel Ojeda, Alex Gaynor, Rob Herring,
Saravana Kannan, Rafael J . Wysocki, Greg Kroah-Hartman,
rust-for-linux, linux-kernel, linux-acpi, devicetree, Boqun Feng,
Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Alice Ryhl, Trevor Gross, Len Brown, Viresh Kumar,
Wedson Almeida Filho, Alex Hung, Tamir Duberstein,
FUJITA Tomonori, Xiangfei Ding
On Thu, Jun 05, 2025 at 02:57:21PM +0100, Igor Korotin wrote:
> Should I resend the whole series as PATCH v2 with a changelog?
Yes, please do so.
^ permalink raw reply [flat|nested] 7+ messages in thread