twizzler_driver/dma/
pool.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
382
383
384
use std::sync::{Arc, Mutex};

use twizzler_abi::{
    marker::BaseType,
    object::{MAX_SIZE, NULLPAGE_SIZE},
    syscall::{BackingType, LifetimeType},
};
use twizzler_object::{CreateSpec, Object};

use super::{Access, DeviceSync, DmaObject, DmaOptions, DmaRegion, DmaSliceRegion, DMA_PAGE_SIZE};

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub(super) struct SplitPageRange {
    start: usize,
    len: usize,
}

pub(super) enum Split {
    Single(SplitPageRange),
    Multiple(SplitPageRange, SplitPageRange),
}

impl SplitPageRange {
    fn new(start: usize, len: usize) -> Self {
        Self { start, len }
    }

    fn split(self, newlen: usize) -> Split {
        let start = self.start;
        let len = self.len;
        if newlen == 0 || newlen == len {
            return Split::Single(Self { start, len });
        }
        Split::Multiple(
            Self { start, len: newlen },
            Self {
                start: start + newlen,
                len: len - newlen,
            },
        )
    }

    fn merge(self, other: Self) -> Self {
        let (first, second) = if self.start < other.start {
            (self, other)
        } else {
            (other, self)
        };
        assert!(first.adjacent_before(&second));

        Self {
            start: first.start,
            len: first.len + second.len,
        }
    }

    fn adjacent_before(&self, other: &Self) -> bool {
        self.start < other.start && self.start + self.len == other.start
    }

    fn len(&self) -> usize {
        self.len
    }

    #[cfg(test)]
    fn start(&self) -> usize {
        self.start
    }

    fn offset(&self) -> usize {
        self.start * DMA_PAGE_SIZE
    }
}

#[cfg(test)]
pub mod tests_split_page_range {
    use super::SplitPageRange;
    use crate::dma::pool::compact_range_list;

    #[test]
    fn spr_split_multiple() {
        let r = SplitPageRange::new(2, 7);
        let split = r.split(4);
        if let super::Split::Multiple(a, b) = split {
            assert_eq!(a.len(), 4);
            assert_eq!(a.start(), 2);
            assert_eq!(b.len(), 3);
            assert_eq!(b.start(), 6);
        } else {
            panic!("split broken");
        }
    }

    #[test]
    fn spr_split_single1() {
        let r = SplitPageRange::new(2, 7);
        let split = r.split(7);
        if let super::Split::Single(r) = split {
            assert_eq!(r.len(), 7);
            assert_eq!(r.start(), 2);
        } else {
            panic!("split broken");
        }
    }

    #[test]
    fn spr_split_single2() {
        let r = SplitPageRange::new(2, 7);
        let split = r.split(0);
        if let super::Split::Single(r) = split {
            assert_eq!(r.len(), 7);
            assert_eq!(r.start(), 2);
        } else {
            panic!("split broken");
        }
    }

    #[test]
    fn spr_merge() {
        let a = SplitPageRange::new(2, 4);
        let b = SplitPageRange::new(6, 3);
        let r = a.merge(b);
        assert_eq!(r.start(), 2);
        assert_eq!(r.len(), 7);
    }

    #[test]
    fn spr_adj() {
        let a = SplitPageRange::new(2, 4);
        let b = SplitPageRange::new(1, 1);
        let c = SplitPageRange::new(6, 4);

        assert!(!a.adjacent_before(&b));
        assert!(b.adjacent_before(&a));
        assert!(!a.adjacent_before(&a));
        assert!(a.adjacent_before(&c));
    }

    #[test]
    fn spr_merge_alg() {
        let a = SplitPageRange::new(2, 4);
        let b = SplitPageRange::new(0, 1);
        let c = SplitPageRange::new(6, 4);
        let x = SplitPageRange::new(2, 8);
        let mut list = vec![a.clone(), b.clone(), c.clone()];
        let single_list = vec![a.clone()];
        let slw: Vec<_> = single_list.windows(2).collect();
        assert!(slw.is_empty());

        compact_range_list(&mut list);

        assert_eq!(list, vec![b, x]);
    }
}

pub(super) struct AllocatableDmaObject {
    dma: DmaObject,
    freelist: Mutex<Vec<SplitPageRange>>,
}

/// A pool for allocating DMA regions that all share a common access type and DMA options.
pub struct DmaPool {
    opts: DmaOptions,
    spec: CreateSpec,
    access: Access,
    objects: Mutex<Vec<Arc<AllocatableDmaObject>>>,
}

/// Possible errors that can arise from a DMA pool allocation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum AllocationError {
    /// The requested region size was too large.
    TooBig,
    /// An internal error occurred.
    InternalError,
}

struct EmptyBase;

impl BaseType for EmptyBase {
    fn init<T>(_t: T) -> Self {
        Self
    }

    fn tags() -> &'static [(
        twizzler_abi::marker::BaseVersion,
        twizzler_abi::marker::BaseTag,
    )] {
        &[]
    }
}

// Merge adjacent regions by sorting, comparing pairs, and merging if they are adjacent.
// Keep going until we cannot merge anymore.
fn compact_range_list(list: &mut Vec<SplitPageRange>) {
    list.sort();
    loop {
        let pairs: Vec<_> = list
            .windows(2)
            .enumerate()
            .filter_map(|(idx, ranges)| {
                if ranges[0].adjacent_before(&ranges[1]) {
                    Some(idx)
                } else {
                    None
                }
            })
            .collect();

        if pairs.is_empty() {
            break;
        }

        // Iterate in reverse to compact from top, so as to not mess up indices.
        for pair in pairs.iter().rev() {
            // Grab the second item first to not mess up indices.
            let second = list.remove(pair + 1);
            let new = list[*pair].clone().merge(second);
            list[*pair] = new;
        }
    }
}

impl AllocatableDmaObject {
    pub(super) fn dma_object(&self) -> &DmaObject {
        &self.dma
    }

    pub(super) fn free(&self, range: SplitPageRange) {
        let mut freelist = self.freelist.lock().unwrap();
        freelist.push(range);

        compact_range_list(&mut freelist);
        // TODO: consider that, if the entire object get free'd, we could delete the object.
    }

    fn allocate(&self, len: usize) -> Option<SplitPageRange> {
        let mut freelist = self.freelist.lock().unwrap();
        let nr_pages = (len - 1) / DMA_PAGE_SIZE + 1;
        let index = freelist.iter().position(|range| range.len() >= nr_pages)?;

        let range = freelist.remove(index);
        Some(match range.split(nr_pages) {
            Split::Single(r) => r,
            Split::Multiple(alloc, extra) => {
                freelist.push(extra);
                alloc
            }
        })
    }

    fn new(spec: &CreateSpec) -> Result<AllocatableDmaObject, AllocationError> {
        Ok(AllocatableDmaObject {
            // TODO: automatic object deletion.
            dma: DmaObject::new::<EmptyBase>(
                Object::create::<EmptyBase>(spec, EmptyBase)
                    .map_err(|_| AllocationError::InternalError)?,
            ),
            freelist: Mutex::new(vec![SplitPageRange::new(
                1,
                (MAX_SIZE - NULLPAGE_SIZE * 2) / DMA_PAGE_SIZE,
            )]),
        })
    }
}

impl DmaPool {
    /// Create a new DmaPool with access and DMA options, where each created underlying Twizzler
    /// object is created using the provided [CreateSpec]. If default (volatile) options are
    /// acceptable for the create spec, use the [crate::dma::DmaPool::default_spec] function.
    pub fn new(spec: CreateSpec, access: Access, opts: DmaOptions) -> Self {
        Self {
            opts,
            spec,
            access,
            objects: Mutex::new(vec![]),
        }
    }

    /// Generate a default [CreateSpec] for use in creating Twizzler DMA objects. By default,
    /// Twizzler objects for DMA are placed in volatile memory with a volatile lifetime.
    pub fn default_spec() -> CreateSpec {
        CreateSpec::new(LifetimeType::Volatile, BackingType::Normal)
    }

    fn new_object(&self) -> Result<Arc<AllocatableDmaObject>, AllocationError> {
        let obj = Arc::new(AllocatableDmaObject::new(&self.spec)?);
        Ok(obj)
    }

    fn do_allocate(
        &self,
        len: usize,
    ) -> Result<(Arc<AllocatableDmaObject>, SplitPageRange), AllocationError> {
        if len > MAX_SIZE - NULLPAGE_SIZE * 2 {
            return Err(AllocationError::TooBig);
        }
        let mut objects = self.objects.lock().unwrap();
        for obj in &*objects {
            if let Some(pagerange) = obj.allocate(len) {
                return Ok((obj.clone(), pagerange));
            }
        }
        let obj = self.new_object()?;
        objects.push(obj);
        drop(objects);
        self.do_allocate(len)
    }

    /// Allocate a new [DmaRegion<T>] from the pool. The region will be initialized with the
    /// provided initial value.
    pub fn allocate<'a, T: DeviceSync>(&'a self, init: T) -> Result<DmaRegion<T>, AllocationError> {
        let len = core::mem::size_of::<T>();
        let (ado, range) = self.do_allocate(len)?;
        let mut reg = DmaRegion::new(
            len,
            self.access,
            self.opts,
            range.offset(),
            Some((ado.clone(), range)),
        );
        reg.fill(init);
        Ok(reg)
    }

    /// Allocate a new [DmaSliceRegion<T>] from the pool. Each entry in the region's slice will
    /// be initialized with the provided initial value.
    pub fn allocate_array<'a, T: DeviceSync + Clone>(
        &'a self,
        count: usize,
        init: T,
    ) -> Result<DmaSliceRegion<T>, AllocationError> {
        let len = core::mem::size_of::<T>() * count;
        let (ado, range) = self.do_allocate(len)?;
        let mut reg = DmaSliceRegion::new(
            len,
            self.access,
            self.opts,
            range.offset(),
            count,
            Some((ado.clone(), range)),
        );
        reg.fill(init);
        Ok(reg)
    }

    /// Allocate a new [DmaSliceRegion<T>] from the pool. Each entry in the region's slice will
    /// be initialized by running the provided closure.
    pub fn allocate_array_with<'a, T: DeviceSync>(
        &'a self,
        count: usize,
        init: impl Fn() -> T,
    ) -> Result<DmaSliceRegion<T>, AllocationError> {
        let len = core::mem::size_of::<T>() * count;
        let (ado, range) = self.do_allocate(len)?;
        let mut reg = DmaSliceRegion::new(
            len,
            self.access,
            self.opts,
            range.offset(),
            count,
            Some((ado.clone(), range)),
        );
        reg.fill_with(init);
        Ok(reg)
    }
}

#[cfg(test)]
mod tests {
    use super::DmaPool;
    use crate::dma::{Access, DmaOptions};

    #[test]
    fn allocate() {
        let pool = DmaPool::new(
            DmaPool::default_spec(),
            Access::BiDirectional,
            DmaOptions::empty(),
        );

        let _res = pool.allocate(u32::MAX).unwrap();
    }
}