Debian Codegraph @

samples/rust/rust_dma.rs

raw
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::{
8 device::Core,
9 dma::{CoherentAllocation, DataDirection, Device, DmaMask},
10 page, pci,
11 prelude::*,
12 scatterlist::{Owned, SGTable},
13 sync::aref::ARef,
14 };
15
16 #[pin_data(PinnedDrop)]
17 struct DmaSampleDriver {
18 pdev: ARef<pci::Device>,
19 ca: CoherentAllocation<MyStruct>,
20 #[pin]
21 sgt: SGTable<Owned<VVec<u8>>>,
22 }
23
24 const TEST_VALUES: [(u32, u32); 5] = [
25 (0xa, 0xb),
26 (0xc, 0xd),
27 (0xe, 0xf),
28 (0xab, 0xba),
29 (0xcd, 0xef),
30 ];
31
32 struct MyStruct {
33 h: u32,
34 b: u32,
35 }
36
37 impl MyStruct {
38 fn new(h: u32, b: u32) -> Self {
39 Self { h, b }
40 }
41 }
42 // SAFETY: All bit patterns are acceptable values for `MyStruct`.
43 unsafe impl kernel::transmute::AsBytes for MyStruct {}
44 // SAFETY: Instances of `MyStruct` have no uninitialized portions.
45 unsafe impl kernel::transmute::FromBytes for MyStruct {}
46
47 kernel::pci_device_table!(
48 PCI_TABLE,
49 MODULE_PCI_TABLE,
50 <DmaSampleDriver as pci::Driver>::IdInfo,
51 [(pci::DeviceId::from_id(pci::Vendor::REDHAT, 0x5), ())]
52 );
53
54 impl pci::Driver for DmaSampleDriver {
55 type IdInfo = ();
56 const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
57
58 fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> impl PinInit<Self, Error> {
59 pin_init::pin_init_scope(move || {
60 dev_info!(pdev, "Probe DMA test driver.\n");
61
62 let mask = DmaMask::new::<64>();
63
64 // SAFETY: There are no concurrent calls to DMA allocation and mapping primitives.
65 unsafe { pdev.dma_set_mask_and_coherent(mask)? };
66
67 let ca: CoherentAllocation<MyStruct> =
68 CoherentAllocation::alloc_coherent(pdev.as_ref(), TEST_VALUES.len(), GFP_KERNEL)?;
69
70 for (i, value) in TEST_VALUES.into_iter().enumerate() {
71 kernel::dma_write!(ca, [i]?, MyStruct::new(value.0, value.1));
72 }
73
74 let size = 4 * page::PAGE_SIZE;
75 let pages = VVec::with_capacity(size, GFP_KERNEL)?;
76
77 let sgt = SGTable::new(pdev.as_ref(), pages, DataDirection::ToDevice, GFP_KERNEL);
78
79 Ok(try_pin_init!(Self {
80 pdev: pdev.into(),
81 ca,
82 sgt <- sgt,
83 }))
84 })
85 }
86 }
87
88 impl DmaSampleDriver {
89 fn check_dma(&self) -> Result {
90 for (i, value) in TEST_VALUES.into_iter().enumerate() {
91 let val0 = kernel::dma_read!(self.ca, [i]?.h);
92 let val1 = kernel::dma_read!(self.ca, [i]?.b);
93
94 assert_eq!(val0, value.0);
95 assert_eq!(val1, value.1);
96 }
97
98 Ok(())
99 }
100 }
101
102 #[pinned_drop]
103 impl PinnedDrop for DmaSampleDriver {
104 fn drop(self: Pin<&mut Self>) {
105 dev_info!(self.pdev, "Unload DMA test driver.\n");
106
107 assert!(self.check_dma().is_ok());
108
109 for (i, entry) in self.sgt.iter().enumerate() {
110 dev_info!(
111 self.pdev,
112 "Entry[{}]: DMA address: {:#x}",
113 i,
114 entry.dma_address(),
115 );
116 }
117 }
118 }
119
120 kernel::module_pci_driver! {
121 type: DmaSampleDriver,
122 name: "rust_dma",
123 authors: ["Abdiel Janulgue"],
124 description: "Rust DMA test",
125 license: "GPL v2",
126 }