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, Device, DmaMask}, 10 pci, 11 prelude::*, 12 types::ARef, 13 }; 14 15 struct DmaSampleDriver { 16 pdev: ARef<pci::Device>, 17 ca: CoherentAllocation<MyStruct>, 18 } 19 20 const TEST_VALUES: [(u32, u32); 5] = [ 21 (0xa, 0xb), 22 (0xc, 0xd), 23 (0xe, 0xf), 24 (0xab, 0xba), 25 (0xcd, 0xef), 26 ]; 27 28 struct MyStruct { 29 h: u32, 30 b: u32, 31 } 32 33 impl MyStruct { 34 fn new(h: u32, b: u32) -> Self { 35 Self { h, b } 36 } 37 } 38 // SAFETY: All bit patterns are acceptable values for `MyStruct`. 39 unsafe impl kernel::transmute::AsBytes for MyStruct {} 40 // SAFETY: Instances of `MyStruct` have no uninitialized portions. 41 unsafe impl kernel::transmute::FromBytes for MyStruct {} 42 43 kernel::pci_device_table!( 44 PCI_TABLE, 45 MODULE_PCI_TABLE, 46 <DmaSampleDriver as pci::Driver>::IdInfo, 47 [(pci::DeviceId::from_id(pci::Vendor::REDHAT, 0x5), ())] 48 ); 49 50 impl pci::Driver for DmaSampleDriver { 51 type IdInfo = (); 52 const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE; 53 54 fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> Result<Pin<KBox<Self>>> { 55 dev_info!(pdev.as_ref(), "Probe DMA test driver.\n"); 56 57 let mask = DmaMask::new::<64>(); 58 59 // SAFETY: There are no concurrent calls to DMA allocation and mapping primitives. 60 unsafe { pdev.dma_set_mask_and_coherent(mask)? }; 61 62 let ca: CoherentAllocation<MyStruct> = 63 CoherentAllocation::alloc_coherent(pdev.as_ref(), TEST_VALUES.len(), GFP_KERNEL)?; 64 65 for (i, value) in TEST_VALUES.into_iter().enumerate() { 66 kernel::dma_write!(ca[i] = MyStruct::new(value.0, value.1))?; 67 } 68 69 let drvdata = KBox::new( 70 Self { 71 pdev: pdev.into(), 72 ca, 73 }, 74 GFP_KERNEL, 75 )?; 76 77 Ok(drvdata.into()) 78 } 79 } 80 81 impl Drop for DmaSampleDriver { 82 fn drop(&mut self) { 83 dev_info!(self.pdev.as_ref(), "Unload DMA test driver.\n"); 84 85 for (i, value) in TEST_VALUES.into_iter().enumerate() { 86 let val0 = kernel::dma_read!(self.ca[i].h); 87 let val1 = kernel::dma_read!(self.ca[i].b); 88 assert!(val0.is_ok()); 89 assert!(val1.is_ok()); 90 91 if let Ok(val0) = val0 { 92 assert_eq!(val0, value.0); 93 } 94 if let Ok(val1) = val1 { 95 assert_eq!(val1, value.1); 96 } 97 } 98 } 99 } 100 101 kernel::module_pci_driver! { 102 type: DmaSampleDriver, 103 name: "rust_dma", 104 authors: ["Abdiel Janulgue"], 105 description: "Rust DMA test", 106 license: "GPL v2", 107 } 108