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