File Index
Files and directories
Sign in
FILE

rust_dma.rs

File preview with syntax highlighting.

linux-6.19.14/samples/rust/rust_dma.rs RUST
  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::{CoherentAllocation, DataDirection, Device, DmaMask},
 10    page, pci,
 11    prelude::*,
 12    scatterlist::{Owned, SGTable},
 13    sync::aref::ARef,
 14};
 15
 16#[pin_data(PinnedDrop)]
 17struct DmaSampleDriver {
 18    pdev: ARef<pci::Device>,
 19    ca: CoherentAllocation<MyStruct>,
 20    #[pin]
 21    sgt: SGTable<Owned<VVec<u8>>>,
 22}
 23
 24const TEST_VALUES: [(u32, u32); 5] = [
 25    (0xa, 0xb),
 26    (0xc, 0xd),
 27    (0xe, 0xf),
 28    (0xab, 0xba),
 29    (0xcd, 0xef),
 30];
 31
 32struct MyStruct {
 33    h: u32,
 34    b: u32,
 35}
 36
 37impl 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`.
 43unsafe impl kernel::transmute::AsBytes for MyStruct {}
 44// SAFETY: Instances of `MyStruct` have no uninitialized portions.
 45unsafe impl kernel::transmute::FromBytes for MyStruct {}
 46
 47kernel::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
 54impl 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) -> impl PinInit<Self, Error> {
 59        pin_init::pin_init_scope(move || {
 60            dev_info!(pdev.as_ref(), "Probe DMA test driver.\n");
 61
 62            let mask = DmaMask::new::<64>();
 63
 64            // SAFETY: There are no concurrent calls to DMA allocation and mapping primitives.
 65            unsafe { pdev.dma_set_mask_and_coherent(mask)? };
 66
 67            let ca: CoherentAllocation<MyStruct> =
 68                CoherentAllocation::alloc_coherent(pdev.as_ref(), TEST_VALUES.len(), GFP_KERNEL)?;
 69
 70            for (i, value) in TEST_VALUES.into_iter().enumerate() {
 71                kernel::dma_write!(ca[i] = MyStruct::new(value.0, value.1))?;
 72            }
 73
 74            let size = 4 * page::PAGE_SIZE;
 75            let pages = VVec::with_capacity(size, GFP_KERNEL)?;
 76
 77            let sgt = SGTable::new(pdev.as_ref(), pages, DataDirection::ToDevice, GFP_KERNEL);
 78
 79            Ok(try_pin_init!(Self {
 80                pdev: pdev.into(),
 81                ca,
 82                sgt <- sgt,
 83            }))
 84        })
 85    }
 86}
 87
 88#[pinned_drop]
 89impl PinnedDrop for DmaSampleDriver {
 90    fn drop(self: Pin<&mut Self>) {
 91        let dev = self.pdev.as_ref();
 92
 93        dev_info!(dev, "Unload DMA test driver.\n");
 94
 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            assert!(val0.is_ok());
 99            assert!(val1.is_ok());
100
101            if let Ok(val0) = val0 {
102                assert_eq!(val0, value.0);
103            }
104            if let Ok(val1) = val1 {
105                assert_eq!(val1, value.1);
106            }
107        }
108
109        for (i, entry) in self.sgt.iter().enumerate() {
110            dev_info!(dev, "Entry[{}]: DMA address: {:#x}", i, entry.dma_address());
111        }
112    }
113}
114
115kernel::module_pci_driver! {
116    type: DmaSampleDriver,
117    name: "rust_dma",
118    authors: ["Abdiel Janulgue"],
119    description: "Rust DMA test",
120    license: "GPL v2",
121}
~/ Read-only file preview.