Skip to content

Commit 9901add

Browse files
xyn-ackojeda
authored andcommitted
samples: rust: add Rust dma test sample driver
Add a simple driver to exercise the basics of the Rust DMA coherent allocator bindings. Suggested-by: Danilo Krummrich <dakr@kernel.org> Signed-off-by: Abdiel Janulgue <abdiel.janulgue@gmail.com> Acked-by: Danilo Krummrich <dakr@kernel.org> Link: https://lore.kernel.org/r/20250317185345.2608976-4-abdiel.janulgue@gmail.com [ Renamed Kconfig symbol and moved it up. Migrated to the new `authors` key in `module!`. Fixed module name in description and typo in commit message. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent ad2907b commit 9901add

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

samples/rust/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ config SAMPLE_RUST_PRINT
4040

4141
If unsure, say N.
4242

43+
config SAMPLE_RUST_DMA
44+
tristate "DMA Test Driver"
45+
depends on PCI
46+
help
47+
This option builds the Rust DMA Test driver sample.
48+
49+
To compile this as a module, choose M here:
50+
the module will be called rust_dma.
51+
52+
If unsure, say N.
53+
4354
config SAMPLE_RUST_DRIVER_PCI
4455
tristate "PCI Driver"
4556
depends on PCI

samples/rust/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ ccflags-y += -I$(src) # needed for trace events
44
obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o
55
obj-$(CONFIG_SAMPLE_RUST_MISC_DEVICE) += rust_misc_device.o
66
obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o
7+
obj-$(CONFIG_SAMPLE_RUST_DMA) += rust_dma.o
78
obj-$(CONFIG_SAMPLE_RUST_DRIVER_PCI) += rust_driver_pci.o
89
obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM) += rust_driver_platform.o
910
obj-$(CONFIG_SAMPLE_RUST_DRIVER_FAUX) += rust_driver_faux.o

samples/rust/rust_dma.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Rust DMA api test (based on QEMU's `pci-testdev`).
4+
//!
5+
//! To make this driver probe, QEMU must be run with `-device pci-testdev`.
6+
7+
use kernel::{bindings, dma::CoherentAllocation, pci, prelude::*};
8+
9+
struct DmaSampleDriver {
10+
pdev: pci::Device,
11+
ca: CoherentAllocation<MyStruct>,
12+
}
13+
14+
const TEST_VALUES: [(u32, u32); 5] = [
15+
(0xa, 0xb),
16+
(0xc, 0xd),
17+
(0xe, 0xf),
18+
(0xab, 0xba),
19+
(0xcd, 0xef),
20+
];
21+
22+
struct MyStruct {
23+
h: u32,
24+
b: u32,
25+
}
26+
27+
impl MyStruct {
28+
fn new(h: u32, b: u32) -> Self {
29+
Self { h, b }
30+
}
31+
}
32+
// SAFETY: All bit patterns are acceptable values for `MyStruct`.
33+
unsafe impl kernel::transmute::AsBytes for MyStruct {}
34+
// SAFETY: Instances of `MyStruct` have no uninitialized portions.
35+
unsafe impl kernel::transmute::FromBytes for MyStruct {}
36+
37+
kernel::pci_device_table!(
38+
PCI_TABLE,
39+
MODULE_PCI_TABLE,
40+
<DmaSampleDriver as pci::Driver>::IdInfo,
41+
[(
42+
pci::DeviceId::from_id(bindings::PCI_VENDOR_ID_REDHAT, 0x5),
43+
()
44+
)]
45+
);
46+
47+
impl pci::Driver for DmaSampleDriver {
48+
type IdInfo = ();
49+
const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
50+
51+
fn probe(pdev: &mut pci::Device, _info: &Self::IdInfo) -> Result<Pin<KBox<Self>>> {
52+
dev_info!(pdev.as_ref(), "Probe DMA test driver.\n");
53+
54+
let ca: CoherentAllocation<MyStruct> =
55+
CoherentAllocation::alloc_coherent(pdev.as_ref(), TEST_VALUES.len(), GFP_KERNEL)?;
56+
57+
|| -> Result {
58+
for (i, value) in TEST_VALUES.into_iter().enumerate() {
59+
kernel::dma_write!(ca[i] = MyStruct::new(value.0, value.1));
60+
}
61+
62+
Ok(())
63+
}()?;
64+
65+
let drvdata = KBox::new(
66+
Self {
67+
pdev: pdev.clone(),
68+
ca,
69+
},
70+
GFP_KERNEL,
71+
)?;
72+
73+
Ok(drvdata.into())
74+
}
75+
}
76+
77+
impl Drop for DmaSampleDriver {
78+
fn drop(&mut self) {
79+
dev_info!(self.pdev.as_ref(), "Unload DMA test driver.\n");
80+
81+
let _ = || -> Result {
82+
for (i, value) in TEST_VALUES.into_iter().enumerate() {
83+
assert_eq!(kernel::dma_read!(self.ca[i].h), value.0);
84+
assert_eq!(kernel::dma_read!(self.ca[i].b), value.1);
85+
}
86+
Ok(())
87+
}();
88+
}
89+
}
90+
91+
kernel::module_pci_driver! {
92+
type: DmaSampleDriver,
93+
name: "rust_dma",
94+
authors: ["Abdiel Janulgue"],
95+
description: "Rust DMA test",
96+
license: "GPL v2",
97+
}

0 commit comments

Comments
 (0)