File: dma.rs

package info (click to toggle)
linux 6.17.6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,734,348 kB
  • sloc: ansic: 26,679,111; asm: 271,215; sh: 147,319; python: 75,916; makefile: 57,295; perl: 36,942; xml: 19,562; cpp: 5,899; yacc: 4,909; lex: 2,943; awk: 1,556; sed: 29; ruby: 25
file content (58 lines) | stat: -rw-r--r-- 1,582 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// SPDX-License-Identifier: GPL-2.0

//! Simple DMA object wrapper.

use core::ops::{Deref, DerefMut};

use kernel::device;
use kernel::dma::CoherentAllocation;
use kernel::page::PAGE_SIZE;
use kernel::prelude::*;

pub(crate) struct DmaObject {
    dma: CoherentAllocation<u8>,
}

impl DmaObject {
    pub(crate) fn new(dev: &device::Device<device::Bound>, len: usize) -> Result<Self> {
        let len = core::alloc::Layout::from_size_align(len, PAGE_SIZE)
            .map_err(|_| EINVAL)?
            .pad_to_align()
            .size();
        let dma = CoherentAllocation::alloc_coherent(dev, len, GFP_KERNEL | __GFP_ZERO)?;

        Ok(Self { dma })
    }

    pub(crate) fn from_data(dev: &device::Device<device::Bound>, data: &[u8]) -> Result<Self> {
        Self::new(dev, data.len()).map(|mut dma_obj| {
            // TODO[COHA]: replace with `CoherentAllocation::write()` once available.
            // SAFETY:
            // - `dma_obj`'s size is at least `data.len()`.
            // - We have just created this object and there is no other user at this stage.
            unsafe {
                core::ptr::copy_nonoverlapping(
                    data.as_ptr(),
                    dma_obj.dma.start_ptr_mut(),
                    data.len(),
                );
            }

            dma_obj
        })
    }
}

impl Deref for DmaObject {
    type Target = CoherentAllocation<u8>;

    fn deref(&self) -> &Self::Target {
        &self.dma
    }
}

impl DerefMut for DmaObject {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.dma
    }
}