poplar/ddk/
dma.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
use crate::memory_object::MappedMemoryObject;
use alloc::sync::Arc;
use core::{
    alloc::{Allocator, Layout},
    mem,
    ptr::{self, NonNull},
    sync::atomic::{AtomicBool, Ordering},
};
use linked_list_allocator::LockedHeap;

pub struct DmaPool {
    memory: MappedMemoryObject,
    // TODO: in the future, something like a slab allocator would probably be better (no need to
    // waste space inside the physically-mapped region, and likely to map many of the same size)
    allocator: Arc<LockedHeap>,
}

impl DmaPool {
    pub fn new(memory: MappedMemoryObject) -> DmaPool {
        let allocator = Arc::new(unsafe { LockedHeap::new(memory.ptr() as *mut u8, memory.inner.size) });
        DmaPool { memory, allocator }
    }

    pub fn create<T>(&self, value: T) -> Result<DmaObject<T>, ()> {
        let ptr = self.allocator.allocate(Layout::new::<T>()).map_err(|_| ())?.cast::<T>();
        unsafe {
            ptr::write(ptr.as_ptr(), value);
        }
        Ok(DmaObject {
            ptr,
            phys: self.memory.virt_to_phys(ptr.as_ptr() as usize).unwrap(),
            allocator: self.allocator.clone(),
            token: AtomicBool::new(false),
        })
    }

    pub fn create_array<T>(&self, length: usize, value: T) -> Result<DmaArray<T>, ()>
    where
        T: Copy,
    {
        let ptr = self.allocator.allocate(Layout::array::<T>(length).unwrap()).map_err(|_| ())?.cast::<T>();
        for i in 0..length {
            unsafe {
                ptr::write(ptr.as_ptr().add(i), value);
            }
        }
        Ok(DmaArray {
            ptr,
            length,
            phys: self.memory.virt_to_phys(ptr.as_ptr() as usize).unwrap(),
            allocator: self.allocator.clone(),
            token: AtomicBool::new(false),
        })
    }

    pub fn create_buffer(&self, length: usize) -> Result<DmaBuffer, ()> {
        let ptr: NonNull<[u8]> = self.allocator.allocate(Layout::array::<u8>(length).unwrap()).map_err(|_| ())?;
        let slice = unsafe { ptr.as_uninit_slice_mut() };
        for i in 0..length {
            slice[i].write(0x00);
        }

        Ok(DmaBuffer {
            ptr,
            length,
            phys: self.memory.virt_to_phys(ptr.cast::<u8>().as_ptr() as usize).unwrap(),
            allocator: self.allocator.clone(),
            token: AtomicBool::new(false),
        })
    }
}

pub struct DmaObject<T> {
    pub ptr: NonNull<T>,
    pub phys: usize,
    allocator: Arc<LockedHeap>,
    token: AtomicBool,
}

impl<T> DmaObject<T> {
    pub fn token(&mut self) -> Result<DmaToken, ()> {
        if let Ok(_) = self.token.compare_exchange(false, true, Ordering::Acquire, Ordering::Acquire) {
            Ok(DmaToken {
                ptr: self.ptr.cast(),
                length: mem::size_of::<T>(),
                phys: self.phys,
                token: unsafe { NonNull::new_unchecked(&mut self.token as *mut AtomicBool) },
            })
        } else {
            return Err(());
        }
    }

    pub fn read(&self) -> &T {
        assert!(!self.token_held(), "DmaObject accessed while token held!");
        unsafe { self.ptr.as_ref() }
    }

    pub fn write(&mut self) -> &mut T {
        assert!(!self.token_held(), "DmaObject accessed while token held!");
        unsafe { self.ptr.as_mut() }
    }

    fn token_held(&self) -> bool {
        self.token.load(Ordering::Acquire)
    }
}

unsafe impl<T> Send for DmaObject<T> {}
unsafe impl<T> Sync for DmaObject<T> {}

impl<T> Drop for DmaObject<T> {
    fn drop(&mut self) {
        assert!(!self.token_held(), "DmaObject dropped while token held!");
        unsafe { self.allocator.deallocate(self.ptr.cast(), Layout::new::<T>()) }
    }
}

pub struct DmaArray<T> {
    pub ptr: NonNull<T>,
    pub length: usize,
    pub phys: usize,
    allocator: Arc<LockedHeap>,
    token: AtomicBool,
}

impl<T> DmaArray<T> {
    pub fn token(&mut self) -> Result<DmaToken, ()> {
        if let Ok(_) = self.token.compare_exchange(false, true, Ordering::Acquire, Ordering::Acquire) {
            Ok(DmaToken {
                ptr: self.ptr.cast(),
                length: self.length,
                phys: self.phys,
                token: unsafe { NonNull::new_unchecked(&mut self.token as *mut AtomicBool) },
            })
        } else {
            return Err(());
        }
    }

    pub fn write(&mut self, index: usize, value: T) {
        assert!(!self.token_held(), "DmaArray accessed while token held!");
        assert!(index < self.length);
        unsafe {
            ptr::write(self.ptr.as_ptr().add(index), value);
        }
    }

    pub fn read(&self, index: usize) -> &T {
        assert!(!self.token_held(), "DmaArray accessed while token held!");
        assert!(index < self.length);
        unsafe { &*self.ptr.as_ptr().add(index) }
    }

    pub fn phys_of_element(&self, index: usize) -> usize {
        self.phys + index * mem::size_of::<T>()
    }

    fn token_held(&self) -> bool {
        self.token.load(Ordering::Acquire)
    }
}

unsafe impl<T> Send for DmaArray<T> {}
unsafe impl<T> Sync for DmaArray<T> {}

impl<T> Drop for DmaArray<T> {
    fn drop(&mut self) {
        assert!(!self.token_held(), "DmaArray dropped while token held!");
        unsafe { self.allocator.deallocate(self.ptr.cast(), Layout::array::<T>(self.length).unwrap()) }
    }
}

pub struct DmaBuffer {
    pub ptr: NonNull<[u8]>,
    pub length: usize,
    pub phys: usize,
    allocator: Arc<LockedHeap>,
    token: AtomicBool,
}

impl DmaBuffer {
    pub fn token(&mut self) -> Result<DmaToken, ()> {
        if let Ok(_) = self.token.compare_exchange(false, true, Ordering::Acquire, Ordering::Acquire) {
            Ok(DmaToken {
                ptr: self.ptr.cast(),
                length: self.length,
                phys: self.phys,
                token: unsafe { NonNull::new_unchecked(&mut self.token as *mut AtomicBool) },
            })
        } else {
            return Err(());
        }
    }

    pub fn read(&self) -> &[u8] {
        assert!(!self.token_held(), "DmaBuffer accessed while token held!");
        unsafe { self.ptr.as_ref() }
    }

    pub fn write(&mut self) -> &mut [u8] {
        assert!(!self.token_held(), "DmaBuffer accessed while token held!");
        unsafe { self.ptr.as_mut() }
    }

    pub unsafe fn at<T>(&self, offset: usize) -> &T {
        assert!(!self.token_held(), "DmaBuffer accessed while token held!");
        assert!((offset + mem::size_of::<T>()) <= self.length);
        unsafe { &*(self.ptr.byte_add(offset).cast::<T>().as_ptr()) }
    }

    fn token_held(&self) -> bool {
        self.token.load(Ordering::Acquire)
    }
}

unsafe impl Send for DmaBuffer {}
unsafe impl Sync for DmaBuffer {}

impl Drop for DmaBuffer {
    fn drop(&mut self) {
        assert!(!self.token_held(), "DmaBuffer dropped while token held!");
        unsafe { self.allocator.deallocate(self.ptr.cast(), Layout::array::<u8>(self.length).unwrap()) }
    }
}

/// A `DmaToken` refers to an underlying `DmaObject`, `DmaArray`, or `DmaBuffer` while waiting for
/// a DMA transaction to complete. It allows a DMA type to be 'locked' while hardware is accessing
/// it (preventing accesses through the DMA type and erroring if it is dropped while the token is
/// held), and so helps to enforce correct use of DMA memory.
///
/// It also erases the overlying DMA type, and so allows users to operate on any form of DMAable memory.
pub struct DmaToken {
    pub ptr: NonNull<u8>,
    pub length: usize,
    pub phys: usize,
    token: NonNull<AtomicBool>,
}

unsafe impl Send for DmaToken {}
unsafe impl Sync for DmaToken {}

impl Drop for DmaToken {
    fn drop(&mut self) {
        unsafe {
            self.token.as_ref().store(false, Ordering::Release);
        }
    }
}