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<'bound> = Self; 62 const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE; 63 64 fn probe<'bound>( 65 pdev: &'bound pci::Device<Core<'_>>, 66 _info: &'bound Self::IdInfo, 67 ) -> impl PinInit<Self, Error> + 'bound { 68 pin_init::pin_init_scope(move || { 69 dev_info!(pdev, "Probe DMA test driver.\n"); 70 71 let mask = DmaMask::new::<64>(); 72 73 // SAFETY: There are no concurrent calls to DMA allocation and mapping primitives. 74 unsafe { pdev.dma_set_mask_and_coherent(mask)? }; 75 76 let ca: Coherent<[MyStruct]> = 77 Coherent::zeroed_slice(pdev.as_ref(), TEST_VALUES.len(), GFP_KERNEL)?; 78 79 for (i, value) in TEST_VALUES.into_iter().enumerate() { 80 kernel::dma_write!(ca, [try: i], MyStruct::new(value.0, value.1)); 81 } 82 83 let size = 4 * page::PAGE_SIZE; 84 let pages = VVec::with_capacity(size, GFP_KERNEL)?; 85 86 let sgt = SGTable::new(pdev.as_ref(), pages, DataDirection::ToDevice, GFP_KERNEL); 87 88 Ok(try_pin_init!(Self { 89 pdev: pdev.into(), 90 ca, 91 sgt <- sgt, 92 })) 93 }) 94 } 95 } 96 97 impl DmaSampleDriver { 98 fn check_dma(&self) { 99 for (i, value) in TEST_VALUES.into_iter().enumerate() { 100 let val0 = kernel::dma_read!(self.ca, [panic: i].h); 101 let val1 = kernel::dma_read!(self.ca, [panic: i].b); 102 103 assert_eq!(val0, value.0); 104 assert_eq!(val1, value.1); 105 } 106 } 107 } 108 109 #[pinned_drop] 110 impl PinnedDrop for DmaSampleDriver { 111 fn drop(self: Pin<&mut Self>) { 112 dev_info!(self.pdev, "Unload DMA test driver.\n"); 113 114 self.check_dma(); 115 116 for (i, entry) in self.sgt.iter().enumerate() { 117 dev_info!( 118 self.pdev, 119 "Entry[{}]: DMA address: {:#x}", 120 i, 121 entry.dma_address(), 122 ); 123 } 124 } 125 } 126 127 kernel::module_pci_driver! { 128 type: DmaSampleDriver, 129 name: "rust_dma", 130 authors: ["Abdiel Janulgue"], 131 description: "Rust DMA test", 132 license: "GPL v2", 133 } 134