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