dma_api/dma/
vec.rs

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use alloc::vec::Vec;
use core::{
    alloc::Layout,
    mem::size_of,
    ops::{Deref, Index},
};

use crate::{unmap, Direction};

use super::DCommon;

pub struct DVec<T> {
    inner: DCommon<T>,
}

impl<T> DVec<T> {
    const T_SIZE: usize = size_of::<T>();

    pub fn zeros(len: usize, align: usize, direction: Direction) -> Option<Self> {
        let size = len * size_of::<T>();
        let layout = Layout::from_size_align(size, align).unwrap();

        Some(Self {
            inner: DCommon::zeros(layout, direction)?,
        })
    }

    pub fn from_vec(value: Vec<T>, direction: Direction) -> Self {
        Self {
            inner: DCommon::from_vec(value, direction),
        }
    }

    pub fn to_vec(mut self) -> Vec<T> {
        unsafe {
            self.inner
                .preper_read(self.inner.addr.cast(), self.inner.layout.size());
            unmap(self.inner.addr.cast(), self.inner.layout.size());
            let len = self.len();

            self.inner.layout = Layout::from_size_align_unchecked(0, 0x1000);
            Vec::from_raw_parts(self.inner.addr.as_ptr(), len, len)
        }
    }

    pub fn len(&self) -> usize {
        self.inner.layout.size() / size_of::<T>()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn bus_addr(&self) -> u64 {
        self.inner.bus_addr
    }

    pub fn get(&self, index: usize) -> Option<T> {
        if index >= self.len() {
            return None;
        }

        unsafe {
            let ptr = self.inner.addr.add(index);

            self.inner.preper_read(ptr.cast(), Self::T_SIZE);

            Some(ptr.read_volatile())
        }
    }

    pub fn set(&mut self, index: usize, value: T) {
        assert!(index < self.len());

        unsafe {
            let ptr = self.inner.addr.add(index);

            ptr.write_volatile(value);

            self.inner.confirm_write(ptr.cast(), Self::T_SIZE);
        }
    }

    fn as_slice_mut(&mut self) -> &mut [T] {
        unsafe { core::slice::from_raw_parts_mut(self.inner.addr.as_ptr(), self.len()) }
    }
}

impl<T> Index<usize> for DVec<T> {
    type Output = T;

    fn index(&self, index: usize) -> &Self::Output {
        assert!(index < self.len());

        let ptr = unsafe { self.inner.addr.add(index) };

        self.inner.preper_read(ptr.cast(), Self::T_SIZE);

        unsafe { &*ptr.as_ptr() }
    }
}

impl<T: Copy> DVec<T> {
    pub fn copy_from_slice(&mut self, src: &[T]) {
        assert!(src.len() <= self.len());

        self.as_slice_mut().copy_from_slice(src);

        self.inner
            .confirm_write(self.inner.addr.cast(), Self::T_SIZE * src.len());
    }
}

impl<T> Deref for DVec<T> {
    type Target = [T];

    fn deref(&self) -> &Self::Target {
        self.inner
            .preper_read(self.inner.addr.cast(), Self::T_SIZE * self.len());
        unsafe { core::slice::from_raw_parts(self.inner.addr.as_ptr(), self.len()) }
    }
}