rust: Add ACPI match table support for Rust drivers
Subsystems (3)
v7 cover rust: Add ACPI match table support for Rust drivers
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.
- 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.
This series is based on three patches by Danilo Krummrich, which introduce general improvements to the Rust platform driver. These patches re included unchanged at the beginning of the series for completeness, as they are required for the ACPI integration.
Danilo Krummrich (3):
rust: device: implement FwNode::is_compatible()
samples: rust: platform: don't call as_ref() repeatedly
samples: rust: platform: conditionally call Self::properties_parse()
Igor Korotin (6):
rust: acpi: add acpi::DeviceId abstraction
rust: driver: Consolidate Adapter::of_id_info methods using #[cfg]
rust: driver: Add ACPI id table support to Adapter trait
rust: platform: Set OF_ID_TABLE default to None in Driver trait
rust: platform: Add ACPI match table support to Driver trait
samples: rust: add ACPI match table example to platform driver
Changelog
---------
v7:
- Rebased onto the driver-core-next branch
- Incorporated three of Danilo’s upstream patches as a base
- Switched from assert! to build_assert! in acpi::DeviceId::new
for compile-time checks
- Renamed the sample ACPI HID to TST0001
- Moved the ACPI test instructions into the module’s top-level
documentation comment
- Link to v6: https://lore.kernel.org/rust-for-linux/[email protected]/
v6:
- Moved set Driver::OF_ID_TABLE default to None to a separate commit
- Removed out of scope change related to cpufreq driver.
- Link to v5: https://lore.kernel.org/rust-for-linux/[email protected]/
v5:
- Got rid of unnecessary consolidation of Adapter::acpi_id_info methods.
Instead, firstly made consolidation of Adapter::of_id_info, then Adapter::acpi_id_info is added using the same pattern.
- Set Adapter::OF_ID_TABLE and Adapter::ACPI_ID_TABLE as None by
default.
- Removed Adapter::OF_ID_TABLE/Adapter::ACPI_ID_TABLE initialization
example due to irrelevance.
- Removed extra of dependency and Adapter::OF_ID_TABLE initialization
in cpufreq driver.
- Link to v4: https://lore.kernel.org/rust-for-linux/[email protected]/
v4:
- Fixed code example for trait Adapter in platform.rs
- Fixed driver implementation example in rust_driver_platform.rs and moved
it to trait Adapter in platform.rs per Danilo Krummrich's suggestion.
- Consolidated Adapter::of_id_info and Adapter::acpi_id_info methods using
#[cfg] per Benno Lossin's suggestion.
- Link to v3: https://lore.kernel.org/rust-for-linux/[email protected]/
v3:
- Removed fwnode type check in Adapter::id_info per Greg's and Danilo's
comments
- Removed is_of_node rust helper, due to unnecessity.
- Fixed example code in rust_driver_platform.rs per Danilo's comment
- Added an instruction of testing ACPI using QEMU with a custom SSDT
- Fixed minor code formatting issues.
- Link to v2: https://lore.kernel.org/rust-for-linux/[email protected]/
v2:
- Removed misleading comment in acpi::DeviceID implementation.
- Removed unnecessary casting in acpi::DeviceID::new.
- Moved pub mod acpi to correct alphabetical position in rust/kernel/lib.rs.
- Link to v1: https://lore.kernel.org/rust-for-linux/[email protected]/
MAINTAINERS | 1 + rust/bindings/bindings_helper.h | 1 + rust/helpers/property.c | 6 ++ rust/kernel/acpi.rs | 60 ++++++++++++++++++++ rust/kernel/device/property.rs | 9 +++ rust/kernel/driver.rs | 81 ++++++++++++++++++++------- rust/kernel/lib.rs | 1 + rust/kernel/platform.rs | 29 +++++++++- samples/rust/rust_driver_platform.rs | 83 ++++++++++++++++++++++++++-- 9 files changed, 244 insertions(+), 27 deletions(-) create mode 100644 rust/kernel/acpi.rs
base-commit: 2a1ea59de83bf367215e2a4dd9bf8bbd061349b3
Signature · 1 line
2.43.0
PATCH v7 1/9 rust: device: implement FwNode::is_compatible()
From: Danilo Krummrich <[email protected]>
Implement FwNode::is_compatible() to check whether a given match string is compatible with a FwNode.
--- rust/helpers/property.c | 6 ++++++ rust/kernel/device/property.rs | 9 +++++++++ 2 files changed, 15 insertions(+)
rust/helpers/property.c +6 -0
@@ -6,3 +6,9 @@ void rust_helper_fwnode_handle_put(struct fwnode_handle *fwnode) {fwnode_handle_put (fwnode); } + +bool rust_helper_fwnode_device_is_compatible (const struct fwnode_handle *fwnode, +const char *match) +{ +return fwnode_device_is_compatible (fwnode, match); +}
rust/kernel/device/property.rs +9 -0
@@ -93,6 +93,15 @@ pub fn property_present(&self, name: &CStr) -> bool {unsafe { bindings::fwnode_property_present (self.as_raw ().cast_const (), name.as_char_ptr ()) } } +/// Return `true` if this [`FwNode`] is compatible with `match_str`, `false` otherwise. +pub fn is_compatible (&self, match_str: &CStr ) ->bool { +// SAFETY: +// - By the invariant of `CStr`, `name.as_char_ptr()` is valid and null-terminated. +// - The type invariant of `Self` guarantees that `self.as_raw() is a pointer to a valid +// `struct fwnode_handle`. +unsafe { bindings::fwnode_device_is_compatible (self.as_raw (), match_str.as_char_ptr ()) } + } +/// Returns firmware property `name` boolean value. pub fn property_read_bool (&self, name: &CStr ) ->bool {// SAFETY:
Signature · 1 line
2.43.0
PATCH v7 2/9 samples: rust: platform: don't call as_ref() repeatedly
In reply to Igor KorotinFrom: Danilo Krummrich <[email protected]>
In SampleDriver::probe() don't call pdev.as_ref() repeatedly, instead introduce a dedicated &Device.
--- samples/rust/rust_driver_platform.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
samples/rust/rust_driver_platform.rs +5 -3
@@ -32,13 +32,15 @@ fn probe( pdev: &platform::Device<Core>, info: Option<&Self::IdInfo>, ) -> Result<Pin<KBox<Self>>> { -dev_dbg! (pdev.as_ref (),"Probe Rust Platform driver sample.\n" ); +let dev = pdev.as_ref (); + +dev_dbg! (dev,"Probe Rust Platform driver sample.\n" );if let Some (info) = info { -dev_info! (pdev.as_ref (),"Probed with info: '{}'.\n" , info.0 ); +dev_info! (dev,"Probed with info: '{}'.\n" , info.0 ); } -Self ::properties_parse (pdev.as_ref ())?; +Self ::properties_parse (dev)?;let drvdata =KBox ::new (Self { pdev: pdev.into () },GFP_KERNEL )?;
Signature · 1 line
2.43.0
PATCH v7 3/9 samples: rust: platform: conditionally call Self::properties_parse()
In reply to Igor KorotinFrom: Danilo Krummrich <[email protected]>
Only call Self::properties_parse() when the device is compatible with "test,rust-device".
Once we add ACPI support, we don't want the ACPI device to fail probing in Self::properties_parse().
--- samples/rust/rust_driver_platform.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
samples/rust/rust_driver_platform.rs +6 -1
@@ -40,7 +40,12 @@ fn probe(dev_info! (dev,"Probed with info: '{}'.\n" , info.0 ); } -Self ::properties_parse (dev)?; +if dev + .fwnode () + .is_some_and (|node| node.is_compatible (c_str! ("test,rust-device" ))) + { +Self ::properties_parse (dev)?; + }let drvdata =KBox ::new (Self { pdev: pdev.into () },GFP_KERNEL )?;
Signature · 1 line
2.43.0
Re: [PATCH v7 3/9] samples: rust: platform: conditionally call Self::properties_parse()
Quoted 9 lines · Igor Korotin
From: Danilo Krummrich <[email protected]>
Only call
Self::properties_parse()when the device is compatible with "test,rust-device".Once we add ACPI support, we don't want the ACPI device to fail probing in
Self::properties_parse().Signed-off-by Danilo Krummrich <[email protected]>
This needs your S-o-b as well since you sent the patch.
Quoted 9 lines
--- samples/rust/rust_driver_platform.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)samples/rust/rust_driver_platform.rs +0
-0
samples/rust/rust_driver_platform.rs fn probe( · line 45
Quoted 7 lines
samples/rust/rust_driver_platform.rs +3
-1@@ -40,7 +40,12 @@ fn probe(dev_info! (dev,"Probed with info: '{}'.\n" , info.0 ); } -Self ::properties_parse (dev)?; + if dev + .fwnode() + .is_some_and(|node| node.is_compatible(c_str!("test,rust-device")))
I think you should be checking just is this ACPI or DT rather than compatible. It's kind of an anti-pattern to test compatible in probe. The reason is we've already matched to a compatible and have match data to use, so we don't need to do it again. It becomes quite messy when there are numerous possible compatibles.
In C, we just do 'if (dev->of_node)' to check for DT or not.
It is just a sample, but I'm sure folks will copy it.
Rob
Danilo Krummrich
maintainerRe: [PATCH v7 3/9] samples: rust: platform: conditionally call Self::properties_parse()
Quoted 25 lines · Rob Herring
Quoted 9 lines · Igor Korotin
From: Danilo Krummrich <[email protected]>
Only call
Self::properties_parse()when the device is compatible with "test,rust-device".Once we add ACPI support, we don't want the ACPI device to fail probing in
Self::properties_parse().Signed-off-by Danilo Krummrich <[email protected]>This needs your S-o-b as well since you sent the patch.
Quoted 12 lines
--- samples/rust/rust_driver_platform.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)samples/rust/rust_driver_platform.rs +0
-0@@ -40,7 +40,12 @@ fn probe(dev_info! (dev,"Probed with info: '{}'.\n" , info.0 ); }
Quoted 10 lines
samples/rust/rust_driver_platform.rs fn probe( · line 45
Quoted 4 lines
samples/rust/rust_driver_platform.rs +3
-1-Self ::properties_parse (dev)?; + if dev + .fwnode() + .is_some_and(|node| node.is_compatible(c_str!("test,rust-device")))I think you should be checking just is this ACPI or DT rather than compatible. It's kind of an anti-pattern to test compatible in probe. The reason is we've already matched to a compatible and have match data to use, so we don't need to do it again. It becomes quite messy when there are numerous possible compatibles.
Yeah, that was my first approach; here's the patch from a few days ago [1].
The reason why I decided against this, was that all the properties we check in Self::properties_parse() in a fallible way *only* apply to the device with this compatible string.
But I don't mind if we replace it with [1] either.
[1] https://git.kernel.org/pub/scm/linux/kernel/git/dakr/linux.git/commit/?h=rust/is_of_node
Danilo Krummrich
maintainerRe: [PATCH v7 3/9] samples: rust: platform: conditionally call Self::properties_parse()
Quoted 37 lines · Danilo Krummrich
Quoted 25 lines · Rob Herring
Quoted 9 lines · Igor Korotin
From: Danilo Krummrich <[email protected]>
Only call
Self::properties_parse()when the device is compatible with "test,rust-device".Once we add ACPI support, we don't want the ACPI device to fail probing in
Self::properties_parse().Signed-off-by Danilo Krummrich <[email protected]>This needs your S-o-b as well since you sent the patch.
Quoted 12 lines
--- samples/rust/rust_driver_platform.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)samples/rust/rust_driver_platform.rs +0
-0@@ -40,7 +40,12 @@ fn probe(dev_info! (dev,"Probed with info: '{}'.\n" , info.0 ); }Quoted 10 lines
samples/rust/rust_driver_platform.rs fn probe( · line 45
Quoted 4 lines
samples/rust/rust_driver_platform.rs +3
-1-Self ::properties_parse (dev)?; + if dev + .fwnode() + .is_some_and(|node| node.is_compatible(c_str!("test,rust-device")))I think you should be checking just is this ACPI or DT rather than compatible. It's kind of an anti-pattern to test compatible in probe. The reason is we've already matched to a compatible and have match data to use, so we don't need to do it again. It becomes quite messy when there are numerous possible compatibles.
Quoted 7 lines
Yeah, that was my first approach; here's the patch from a few days ago [1].
The reason why I decided against this, was that all the properties we check in
Self::properties_parse()in a fallible way *only* apply to the device with this compatible string.But I don't mind if we replace it with [1] either.
As mentioned, I don't mind either, so let's change it up.
@Igor, can you please pick up the patch in [1] and at the same time drop the patch introducing FwNode::is_compatible() and replace node.is_compatible() with node.is_of_node() in this one?
Please also remember to add your SoB to the patches not authored by yourself.
Thanks,
Danilo
Re: [PATCH v7 3/9] samples: rust: platform: conditionally call Self::properties_parse()
Quoted 44 lines · Danilo Krummrich
Quoted 43 lines · Danilo Krummrich
Quoted 25 lines · Rob Herring
Quoted 9 lines · Igor Korotin
From: Danilo Krummrich <[email protected]>
Only call
Self::properties_parse()when the device is compatible with "test,rust-device".Once we add ACPI support, we don't want the ACPI device to fail probing in
Self::properties_parse().Signed-off-by Danilo Krummrich <[email protected]>This needs your S-o-b as well since you sent the patch.
Quoted 12 lines
--- samples/rust/rust_driver_platform.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)samples/rust/rust_driver_platform.rs +0
-0@@ -40,7 +40,12 @@ fn probe(dev_info! (dev,"Probed with info: '{}'.\n" , info.0 ); }Quoted 10 lines
samples/rust/rust_driver_platform.rs fn probe( · line 45
Quoted 4 lines
samples/rust/rust_driver_platform.rs +3
-1-Self ::properties_parse (dev)?; + if dev + .fwnode() + .is_some_and(|node| node.is_compatible(c_str!("test,rust-device")))I think you should be checking just is this ACPI or DT rather than compatible. It's kind of an anti-pattern to test compatible in probe. The reason is we've already matched to a compatible and have match data to use, so we don't need to do it again. It becomes quite messy when there are numerous possible compatibles.
Yeah, that was my first approach; here's the patch from a few days ago [1].
The reason why I decided against this, was that all the properties we check in
Self::properties_parse()in a fallible way *only* apply to the device with this compatible string.
Quoted 7 lines
Quoted 1 line
But I don't mind if we replace it with [1] either.
As mentioned, I don't mind either, so let's change it up.
@Igor, can you please pick up the patch in [1] and at the same time drop the patch introducing
FwNode::is_compatible()and replacenode.is_compatible()withnode.is_of_node()in this one?
Just to make sure that we're on the same page, I replace this:
if dev.fwnode().is_some_and(|node|
node.is_compatible(c_str!("test,rust-device")))
with:
if dev.fwnode().is_some_and(|node| node.is_of_node())
And reworded the comment as follows:
samples: rust: platform: conditionally call Self::properties_parse()
Only call Self::properties_parse() when the device is OF node
Once we add ACPI support, we don't want the ACPI device to fail probing in Self::properties_parse().
Thanks
Igor
Danilo Krummrich
maintainerRe: [PATCH v7 3/9] samples: rust: platform: conditionally call Self::properties_parse()
Quoted 9 lines · Igor Korotin
Just to make sure that we're on the same page, I replace this:
if dev.fwnode().is_some_and(|node|node.is_compatible(c_str!("test,rust-device")))
with:
if dev.fwnode().is_some_and(|node| node.is_of_node())
Quoted 8 lines
And reworded the comment as follows:
samples: rust: platform: conditionally call
Self::properties_parse()Only call
Self::properties_parse()when the device is OF nodeOnce we add ACPI support, we don't want the ACPI device to fail probing in
Self::properties_parse().
LGTM!
PATCH v7 4/9 rust: acpi: add `acpi::DeviceId` abstraction
In reply to Igor Korotinacpi::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.
--- MAINTAINERS | 1 + rust/kernel/acpi.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + 3 files changed, 62 insertions(+) create mode 100644 rust/kernel/acpi.rs
MAINTAINERS +1 -0
@@ -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
rust/kernel/acpi.rs new file +60 -0
60 lines not on this page · raw message
rust/kernel/lib.rs +1 -0
@@ -51,6 +51,7 @@pub use ffi; +pub mod acpi;pub mod alloc;#[cfg( CONFIG_AUXILIARY_BUS )] pub mod auxiliary;
Signature · 1 line
2.43.0
PATCH v7 5/9 rust: driver: Consolidate `Adapter::of_id_info` methods using `#[cfg]`
In reply to Igor KorotinRefactor the of_id_info methods in the Adapter trait to reduce duplication. Previously, the method had two versions selected via #[cfg(...)] and #[cfg(not(...))]. This change merges them into a single method by using #[cfg] blocks within the method body.
--- rust/kernel/driver.rs | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-)
rust/kernel/driver.rs +21 -19
@@ -147,30 +147,32 @@ pub trait Adapter {/// Returns the driver's private data from the matching entry in the [`of::IdTable`], if any. /// /// If this returns `None`, it means there is no match with an entry in the [`of::IdTable`]. -#[cfg( CONFIG_OF )] fn of_id_info (dev: &device::Device ) ->Option <&'staticSelf ::IdInfo > { -let table =Self ::of_id_table ()?; +#[cfg(not( CONFIG_OF ))] + { +let _ = dev; +return None ; + } -// SAFETY: -// - `table` has static lifetime, hence it's valid for read, -// - `dev` is guaranteed to be valid while it's alive, and so is `pdev.as_ref().as_raw()`. -let raw_id =unsafe { bindings::of_match_device (table.as_ptr (), dev.as_raw ()) }; +#[cfg( CONFIG_OF )] + { +let table =Self ::of_id_table ()?; -if raw_id.is_null () { -None - }else { -// SAFETY: `DeviceId` is a `#[repr(transparent)` wrapper of `struct of_device_id` and -// does not add additional invariants, so it's safe to transmute. -let id =unsafe { &*raw_id.cast ::<of::DeviceId >() }; +// SAFETY: +// - `table` has static lifetime, hence it's valid for read, +// - `dev` is guaranteed to be valid while it's alive, and so is `pdev.as_ref().as_raw()`. +let raw_id =unsafe { bindings::of_match_device (table.as_ptr (), dev.as_raw ()) }; -Some (table.info (<of::DeviceId as crate ::device_id::RawDeviceId >::index (id))) - } - } +if raw_id.is_null () { +None + }else { +// SAFETY: `DeviceId` is a `#[repr(transparent)` wrapper of `struct of_device_id` and +// does not add additional invariants, so it's safe to transmute. +let id =unsafe { &*raw_id.cast ::<of::DeviceId >() }; -#[cfg(not( CONFIG_OF ))] -#[allow(missing_docs)] -fn of_id_info (_dev: &device::Device ) ->Option <&'staticSelf ::IdInfo > { -None +Some (table.info (<of::DeviceId as crate ::device_id::RawDeviceId >::index (id))) + } + } }/// Returns the driver's private data from the matching entry of any of the ID tables, if any.
Signature · 1 line
2.43.0
PATCH v7 6/9 rust: driver: Add ACPI id table support to Adapter trait
In reply to Igor KorotinExtend the Adapter trait to support ACPI device identification.
This mirrors the existing Open Firmware (OF) support (of_id_table) and enables Rust drivers to match and retrieve ACPI-specific device data when CONFIG_ACPI is enabled.
To avoid breaking compilation, a stub implementation of acpi_id_table() is added to the Platform adapter; the full implementation will be provided in a subsequent patch.
--- rust/bindings/bindings_helper.h | 1 + rust/kernel/driver.rs | 41 ++++++++++++++++++++++++++++++++- rust/kernel/platform.rs | 6 ++++- 3 files changed, 46 insertions(+), 2 deletions(-)
rust/bindings/bindings_helper.h +1 -0
@@ -28,6 +28,7 @@*/ #include <linux/hrtimer_types.h> +#include <linux/acpi.h> #include <drm/drm_device.h> #include <drm/drm_drv.h> #include <drm/drm_file.h>
rust/kernel/driver.rs +40 -1
59 lines not on this page · raw message
rust/kernel/platform.rs +5 -1
@@ -5,7 +5,7 @@//! C header: [`include/linux/platform_device.h`](srctree/include/linux/platform_device.h) use crate ::{ - bindings, container_of, device, driver, + acpi, bindings, container_of, device, driver, error::{to_result,Result }, of, prelude::*,
@@ -94,6 +94,10 @@ impl<T: Driver + 'static> driver::Adapter for Adapter<T> {fn of_id_table () ->Option <of::IdTable <Self ::IdInfo >> {T ::OF_ID_TABLE } + +fn acpi_id_table () ->Option <acpi::IdTable <Self ::IdInfo >> { +None + } }/// Declares a kernel module that exposes a single platform driver.
Signature · 1 line
2.43.0
PATCH v7 7/9 rust: platform: Set `OF_ID_TABLE` default to `None` in `Driver` trait
In reply to Igor KorotinProvide a default value of None for Driver::OF_ID_TABLE to simplify driver implementations.
Drivers that do not require OpenFirmware matching no longer need to import the of module or define the constant explicitly.
This reduces unnecessary boilerplate and avoids pulling in unused dependencies.
--- rust/kernel/platform.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/platform.rs +1 -1
@@ -162,7 +162,7 @@ pub trait Driver: Send {type IdInfo : 'static;/// The table of OF device ids supported by the driver. -const OF_ID_TABLE :Option <of::IdTable <Self ::IdInfo >>; +const OF_ID_TABLE :Option <of::IdTable <Self ::IdInfo >> =None ;/// Platform driver probe. ///
Signature · 1 line
2.43.0
PATCH v7 8/9 rust: platform: Add ACPI match table support to `Driver` trait
In reply to Igor KorotinExtend 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.
--- rust/kernel/platform.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-)
rust/kernel/platform.rs +21 -2
@@ -37,12 +37,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`.
@@ -96,7 +102,7 @@ fn of_id_table() -> Option<of::IdTable<Self::IdInfo>> { }fn acpi_id_table () ->Option <acpi::IdTable <Self ::IdInfo >> { -None +T ::ACPI_ID_TABLE } }
@@ -127,7 +133,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; ///
@@ -140,9 +146,19 @@ macro_rules! module_platform_driver {/// ] /// ); /// +/// kernel::acpi_device_table!( +/// ACPI_TABLE, +/// MODULE_ACPI_TABLE, +/// <MyDriver as platform::Driver>::IdInfo, +/// [ +/// (acpi::DeviceId::new(b"TST0001"), ()) +/// ] +/// ); +/// /// 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>,
@@ -164,6 +180,9 @@ pub trait Driver: Send {/// The table of OF device ids supported by the driver. const OF_ID_TABLE :Option <of::IdTable <Self ::IdInfo >> =None ; +/// The table of ACPI device ids supported by the driver. +const ACPI_ID_TABLE :Option <acpi::IdTable <Self ::IdInfo >> =None ; +/// Platform driver probe. /// /// Called when a new platform device is added or discovered.
Signature · 1 line
2.43.0
PATCH v7 9/9 samples: rust: add ACPI match table example to platform driver
In reply to Igor KorotinExtend the Rust sample platform driver to probe using device/driver name matching, OF ID table matching, or ACPI ID table matching.
--- samples/rust/rust_driver_platform.rs | 70 +++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-)
samples/rust/rust_driver_platform.rs +69 -1
@@ -2,8 +2,68 @@//! Rust Platform driver sample. +//! ACPI match table test +//! +//! This demonstrates how to test an ACPI-based Rust platform driver using QEMU +//! with a custom SSDT. +//! +//! Steps: +//! +//! 1. **Create an SSDT source file** (`ssdt.dsl`) with the following content: +//! +//! ```asl +//! DefinitionBlock ("", "SSDT", 2, "TEST", "VIRTACPI", 0x00000001) +//! { +//! Scope (\_SB) +//! { +//! Device (T432) +//! { +//! Name (_HID, "TST0001") // ACPI hardware ID to match +//! Name (_UID, 1) +//! Name (_STA, 0x0F) // Device present, enabled +//! Name (_CRS, ResourceTemplate () +//! { +//! Memory32Fixed (ReadWrite, 0xFED00000, 0x1000) +//! }) +//! } +//! } +//! } +//! ``` +//! +//! 2. **Compile the table**: +//! +//! ```sh +//! iasl -tc ssdt.dsl +//! ``` +//! +//! This generates `ssdt.aml` +//! +//! 3. **Run QEMU** with the compiled AML file: +//! +//! ```sh +//! qemu-system-x86_64 -m 512M \ +//! -enable-kvm \ +//! -kernel path/to/bzImage \ +//! -append "root=/dev/sda console=ttyS0" \ +//! -hda rootfs.img \ +//! -serial stdio \ +//! -acpitable file=ssdt.aml +//! ``` +//! +//! Requirements: +//! - The `rust_driver_platform` must be present either: +//! - built directly into the kernel (`bzImage`), or +//! - available as a `.ko` file and loadable from `rootfs.img` +//! +//! 4. **Verify it worked** by checking `dmesg`: +//! +//! ``` +//! rust_driver_platform TST0001:00: Probed with info: '0'. +//! ``` +//! +use kernel::{ - c_str, + acpi, c_str, device::{self,Core }, of, platform, prelude::*,
@@ -24,9 +84,17 @@ struct SampleDriver { [(of::DeviceId ::new (c_str! ("test,rust-device" )),Info (42 ))] ); +kernel::acpi_device_table!( +ACPI_TABLE , +MODULE_ACPI_TABLE , + <SampleDriver as platform::Driver >::IdInfo , + [(acpi::DeviceId ::new (b"TST0001" ),Info (0 ))] +); +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 >> =Some (&ACPI_TABLE );fn probe ( pdev: &platform::Device<Core>,
Signature · 1 line
2.43.0