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