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