Skip to content

Commit 5444799

Browse files
author
Danilo Krummrich
committed
samples: rust: dma: add sample code for SGTable
Add sample code for allocating and mapping a scatter-gather table (`SGTable`). Reviewed-by: Alexandre Courbot <acourbot@nvidia.com> Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com> Reviewed-by: Lyude Paul <lyude@redhat.com> Co-developed-by: Abdiel Janulgue <abdiel.janulgue@gmail.com> Signed-off-by: Abdiel Janulgue <abdiel.janulgue@gmail.com> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Link: https://lore.kernel.org/r/20250828133323.53311-5-dakr@kernel.org Signed-off-by: Danilo Krummrich <dakr@kernel.org>
1 parent 05aa6fb commit 5444799

1 file changed

Lines changed: 26 additions & 9 deletions

File tree

samples/rust/rust_dma.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@
77
use kernel::{
88
bindings,
99
device::Core,
10-
dma::{CoherentAllocation, Device, DmaMask},
11-
pci,
10+
dma::{CoherentAllocation, DataDirection, Device, DmaMask},
11+
page, pci,
1212
prelude::*,
13+
scatterlist::{Owned, SGTable},
1314
types::ARef,
1415
};
1516

17+
#[pin_data(PinnedDrop)]
1618
struct DmaSampleDriver {
1719
pdev: ARef<pci::Device>,
1820
ca: CoherentAllocation<MyStruct>,
21+
#[pin]
22+
sgt: SGTable<Owned<VVec<u8>>>,
1923
}
2024

2125
const TEST_VALUES: [(u32, u32); 5] = [
@@ -70,21 +74,30 @@ impl pci::Driver for DmaSampleDriver {
7074
kernel::dma_write!(ca[i] = MyStruct::new(value.0, value.1))?;
7175
}
7276

73-
let drvdata = KBox::new(
74-
Self {
77+
let size = 4 * page::PAGE_SIZE;
78+
let pages = VVec::with_capacity(size, GFP_KERNEL)?;
79+
80+
let sgt = SGTable::new(pdev.as_ref(), pages, DataDirection::ToDevice, GFP_KERNEL);
81+
82+
let drvdata = KBox::pin_init(
83+
try_pin_init!(Self {
7584
pdev: pdev.into(),
7685
ca,
77-
},
86+
sgt <- sgt,
87+
}),
7888
GFP_KERNEL,
7989
)?;
8090

81-
Ok(drvdata.into())
91+
Ok(drvdata)
8292
}
8393
}
8494

85-
impl Drop for DmaSampleDriver {
86-
fn drop(&mut self) {
87-
dev_info!(self.pdev.as_ref(), "Unload DMA test driver.\n");
95+
#[pinned_drop]
96+
impl PinnedDrop for DmaSampleDriver {
97+
fn drop(self: Pin<&mut Self>) {
98+
let dev = self.pdev.as_ref();
99+
100+
dev_info!(dev, "Unload DMA test driver.\n");
88101

89102
for (i, value) in TEST_VALUES.into_iter().enumerate() {
90103
let val0 = kernel::dma_read!(self.ca[i].h);
@@ -99,6 +112,10 @@ impl Drop for DmaSampleDriver {
99112
assert_eq!(val1, value.1);
100113
}
101114
}
115+
116+
for (i, entry) in self.sgt.iter().enumerate() {
117+
dev_info!(dev, "Entry[{}]: DMA address: {:#x}", i, entry.dma_address());
118+
}
102119
}
103120
}
104121

0 commit comments

Comments
 (0)