twizzler_driver/dma/
region.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
use core::{marker::PhantomData, ops::Range};
use std::sync::Arc;

use twizzler_abi::{
    kso::{
        pack_kaction_pin_start_and_len, unpack_kaction_pin_token_and_len, KactionCmd, KactionFlags,
        KactionGenericCmd,
    },
    object::NULLPAGE_SIZE,
    syscall::{sys_kaction, PinnedPage},
};

use super::{
    pin::{PhysInfo, PinError},
    pool::{AllocatableDmaObject, SplitPageRange},
    Access, DeviceSync, DmaObject, DmaOptions, DmaPin, SyncMode,
};
use crate::arch::DMA_PAGE_SIZE;

/// A region of DMA memory, represented in virtual memory as type `T`, with a particular access mode
/// and options.
pub struct DmaRegion<T: DeviceSync> {
    virt: *mut u8,
    backing: Option<(Vec<PhysInfo>, u32)>,
    len: usize,
    access: Access,
    //dma: Option<&'a DmaObject>,
    pool: Option<(Arc<AllocatableDmaObject>, SplitPageRange)>,
    options: DmaOptions,
    offset: usize,
    _pd: PhantomData<T>,
}

/// A region of DMA memory, represented in virtual memory as type `[T; len]`, with a particular
/// access mode and options.
pub struct DmaSliceRegion<T: DeviceSync> {
    region: DmaRegion<T>,
    len: usize,
}

impl<'a, T: DeviceSync> DmaRegion<T> {
    pub(super) fn new(
        len: usize,
        access: Access,
        options: DmaOptions,
        offset: usize,
        pool: Option<(Arc<AllocatableDmaObject>, SplitPageRange)>,
    ) -> Self {
        Self {
            virt: unsafe {
                (pool
                    .as_ref()
                    .unwrap()
                    .0
                    .dma_object()
                    .object()
                    .base_mut_unchecked() as *mut () as *mut u8)
                    .add(offset)
                    .sub(NULLPAGE_SIZE)
            },
            len,
            access,
            options,
            backing: None,
            pool,
            offset,
            _pd: PhantomData,
        }
    }

    pub(super) fn fill(&mut self, init: T) {
        let p = self.virt as *mut T;
        unsafe {
            p.write_volatile(init);
            self.sync(SyncMode::FullCoherence);
        }
    }

    fn dma_object(&self) -> &DmaObject {
        self.pool.as_ref().unwrap().0.dma_object()
    }

    /// Calculate the number of pages this region covers.
    pub fn nr_pages(&self) -> usize {
        (self.len - 1) / DMA_PAGE_SIZE + 1
    }

    fn setup_backing(&mut self) -> Result<(), PinError> {
        if self.backing.is_some() {
            return Ok(());
        }
        let mut pins = Vec::new();
        let len = self.nr_pages();
        pins.resize(len, PinnedPage::new(0));

        // The kaction call here compresses start and len into a u64, and returns the token and len
        // in the return u64. This is all because of the limited registers in a syscall.
        let start = (self.offset / DMA_PAGE_SIZE) as u64;
        let ptr = (&pins).as_ptr() as u64;
        let res = sys_kaction(
            KactionCmd::Generic(KactionGenericCmd::PinPages(0)),
            Some(self.dma_object().object().id()),
            ptr,
            pack_kaction_pin_start_and_len(start, len).ok_or(PinError::InternalError)?,
            KactionFlags::empty(),
        )
        .map_err(|_| PinError::InternalError)?
        .u64()
        .ok_or(PinError::InternalError)?;

        let (token, retlen) =
            unpack_kaction_pin_token_and_len(res).ok_or(PinError::InternalError)?;

        if retlen < len {
            return Err(PinError::Exhausted);
        } else if retlen > len {
            return Err(PinError::InternalError);
        }

        let backing: Result<Vec<_>, _> = pins
            .iter()
            .map(|p| p.physical_address().try_into().map(|pa| PhysInfo::new(pa)))
            .collect();

        self.backing = Some((backing.map_err(|_| PinError::InternalError)?, token));

        Ok(())
    }

    /// Return the number of bytes this region covers.
    pub fn num_bytes(&self) -> usize {
        self.len
    }

    /// Return the access direction of this region.
    pub fn access(&self) -> Access {
        self.access
    }

    // Determines the backing information for region. This includes acquiring physical addresses for
    // the region and holding a pin for the pages.
    pub fn pin(&mut self) -> Result<DmaPin<'_>, PinError> {
        self.setup_backing()?;
        Ok(DmaPin::new(&self.backing.as_ref().unwrap().0))
    }

    // Synchronize the region for cache coherence.
    pub fn sync(&self, sync: SyncMode) {
        crate::arch::sync(self, sync, 0, self.len);
    }

    // Run a closure that takes a reference to the DMA data, ensuring coherence.
    pub fn with<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&T) -> R,
    {
        if !self.options.contains(DmaOptions::UNSAFE_MANUAL_COHERENCE) {
            if self.access() != Access::HostToDevice {
                self.sync(SyncMode::PostDeviceToCpu);
            }
        }
        let data = unsafe { self.get() };
        let ret = f(data);
        ret
    }

    // Run a closure that takes a mutable reference to the DMA data, ensuring coherence.
    pub fn with_mut<F, R>(&mut self, f: F) -> R
    where
        F: FnOnce(&mut T) -> R,
    {
        if !self.options.contains(DmaOptions::UNSAFE_MANUAL_COHERENCE) {
            match self.access() {
                Access::HostToDevice => self.sync(SyncMode::PreCpuToDevice),
                Access::DeviceToHost => self.sync(SyncMode::PostDeviceToCpu),
                Access::BiDirectional => self.sync(SyncMode::FullCoherence),
            }
        }
        let data = unsafe { self.get_mut() };
        let ret = f(data);
        if !self.options.contains(DmaOptions::UNSAFE_MANUAL_COHERENCE) {
            if self.access() != Access::DeviceToHost {
                self.sync(SyncMode::PostCpuToDevice);
            }
        }
        ret
    }

    /// Release any pin created for this region.
    ///
    /// # Safety
    /// Caller must ensure that no device is using the information from any active pins for this
    /// region.
    pub unsafe fn release_pin(&mut self) {
        if let Some((_, token)) = self.backing {
            super::object::release_pin(self.dma_object().object().id(), token);
            self.backing = None;
        }
    }

    /// Get a reference to the DMA memory.
    ///
    /// # Safety
    /// The caller must ensure coherence is applied.
    #[inline]
    pub unsafe fn get(&self) -> &T {
        (self.virt as *const T).as_ref().unwrap()
    }

    /// Get a mutable reference to the DMA memory.
    ///
    /// # Safety
    /// The caller must ensure coherence is applied.
    #[inline]
    pub unsafe fn get_mut(&mut self) -> &mut T {
        (self.virt as *mut T).as_mut().unwrap()
    }
}

impl<'a, T: DeviceSync> DmaSliceRegion<T> {
    pub(super) fn new(
        nrbytes: usize,
        access: Access,
        options: DmaOptions,
        offset: usize,
        len: usize,
        pool: Option<(Arc<AllocatableDmaObject>, SplitPageRange)>,
    ) -> Self {
        Self {
            region: DmaRegion::new(nrbytes, access, options, offset, pool),
            len,
        }
    }

    pub(super) fn fill(&mut self, init: T)
    where
        T: Clone,
    {
        let p = self.region.virt as *mut T;
        for idx in 0..self.len {
            unsafe {
                p.add(idx).write_volatile(init.clone());
            }
        }
        self.sync(0..self.len, SyncMode::FullCoherence);
    }

    pub(super) fn fill_with(&mut self, init: impl Fn() -> T) {
        let p = self.region.virt as *mut T;
        for idx in 0..self.len {
            unsafe {
                p.add(idx).write_volatile(init());
            }
        }
        self.sync(0..self.len, SyncMode::FullCoherence);
    }

    /// Return the number of bytes this region covers.
    pub fn num_bytes(&self) -> usize {
        self.region.len
    }

    #[inline]
    /// Return the access direction of this region.
    pub fn access(&self) -> Access {
        self.region.access()
    }

    /// Return the number of elements in the slice that this region covers.
    pub fn len(&self) -> usize {
        self.len
    }

    // Determines the backing information for region. This includes acquiring physical addresses for
    // the region and holding a pin for the pages.
    #[inline]
    pub fn pin(&mut self) -> Result<DmaPin<'_>, PinError> {
        self.region.pin()
    }

    // Synchronize a subslice of the region for cache coherence.
    pub fn sync(&self, range: Range<usize>, sync: SyncMode) {
        let start = range.start * core::mem::size_of::<T>();
        let len = range.len() * core::mem::size_of::<T>();
        crate::arch::sync(&self.region, sync, start, len);
    }

    // Run a closure that takes a reference to a subslice of the DMA data, ensuring coherence.
    pub fn with<F, R>(&self, range: Range<usize>, f: F) -> R
    where
        F: FnOnce(&[T]) -> R,
    {
        if !self
            .region
            .options
            .contains(DmaOptions::UNSAFE_MANUAL_COHERENCE)
        {
            if self.access() != Access::HostToDevice {
                self.sync(range.clone(), SyncMode::PostDeviceToCpu);
            }
        }
        let data = &unsafe { self.get() }[range];
        let ret = f(data);
        ret
    }

    // Run a closure that takes a mutable reference to a subslice of the DMA data, ensuring
    // coherence.
    pub fn with_mut<F, R>(&mut self, range: Range<usize>, f: F) -> R
    where
        F: FnOnce(&mut [T]) -> R,
    {
        if !self
            .region
            .options
            .contains(DmaOptions::UNSAFE_MANUAL_COHERENCE)
        {
            match self.access() {
                Access::HostToDevice => self.sync(range.clone(), SyncMode::PreCpuToDevice),
                Access::DeviceToHost => self.sync(range.clone(), SyncMode::PostDeviceToCpu),
                Access::BiDirectional => self.sync(range.clone(), SyncMode::FullCoherence),
            }
        }
        let data = &mut unsafe { self.get_mut() }[range.clone()];
        let ret = f(data);
        if !self
            .region
            .options
            .contains(DmaOptions::UNSAFE_MANUAL_COHERENCE)
        {
            if self.access() != Access::DeviceToHost {
                self.sync(range, SyncMode::PostCpuToDevice);
            }
        }
        ret
    }

    /// Release any pin created for this region.
    ///
    /// # Safety
    /// Caller must ensure that no device is using the information from any active pins for this
    /// region.
    #[inline]
    pub unsafe fn release_pin(&mut self) {
        self.region.release_pin()
    }

    /// Get a reference to the DMA memory.
    ///
    /// # Safety
    /// The caller must ensure coherence is applied.
    #[inline]
    pub unsafe fn get(&self) -> &[T] {
        core::slice::from_raw_parts(self.region.virt as *const T, self.len)
    }

    /// Get a mutable reference to the DMA memory.
    ///
    /// # Safety
    /// The caller must ensure coherence is applied.
    #[inline]
    pub unsafe fn get_mut(&mut self) -> &mut [T] {
        core::slice::from_raw_parts_mut(self.region.virt as *mut T, self.len)
    }
}

impl<'a, T: DeviceSync> Drop for DmaRegion<T> {
    fn drop(&mut self) {
        if let Some((_, token)) = self.backing.as_ref() {
            self.dma_object()
                .releasable_pins
                .lock()
                .unwrap()
                .push(*token);
        }

        if let Some((ado, range)) = self.pool.take() {
            ado.free(range);
        }
    }
}