dma-api 0.10.0

Trait for DMA alloc and some collections
Documentation
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
use core::{alloc::Layout, marker::PhantomData, ops::Range, ptr::NonNull};

use crate::{
    DeviceDma, DmaAddr, DmaCoherency, DmaDirection, DmaDomainId, DmaError, DmaPod,
    common::{AllocationKind, DmaAllocation},
};

pub struct CoherentArray<T: DmaPod> {
    data: DmaAllocation,
    _phantom: PhantomData<T>,
}

unsafe impl<T: DmaPod + Send> Send for CoherentArray<T> {}
unsafe impl<T: DmaPod + Sync> Sync for CoherentArray<T> {}

impl<T: DmaPod> CoherentArray<T> {
    pub(crate) fn new_zero_with_align(
        os: &DeviceDma,
        len: usize,
        align: usize,
    ) -> Result<Self, DmaError> {
        let layout = array_layout::<T>(len, align)?;
        Ok(Self {
            data: DmaAllocation::new_zero_coherent(os, layout)?,
            _phantom: PhantomData,
        })
    }

    pub(crate) fn new_zero(os: &DeviceDma, len: usize) -> Result<Self, DmaError> {
        Self::new_zero_with_align(os, len, core::mem::align_of::<T>())
    }

    pub fn dma_addr(&self) -> DmaAddr {
        self.data.handle().dma_addr()
    }

    pub fn len(&self) -> usize {
        len_from_bytes::<T>(self.data.handle().size())
    }

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

    pub fn bytes_len(&self) -> usize {
        self.data.handle().size()
    }

    pub fn read_cpu(&self, index: usize) -> Option<T> {
        read_at(self.as_ptr(), self.len(), index)
    }

    pub fn set_cpu(&mut self, index: usize, value: T) {
        write_at(self.as_ptr(), self.len(), index, value);
    }

    pub fn copy_from_slice_cpu(&mut self, src: &[T]) {
        copy_from_slice(self.as_ptr(), self.len(), src);
    }

    pub fn iter_cpu(&self) -> ArrayCpuIter<'_, T, Self> {
        ArrayCpuIter {
            array: self,
            index: 0,
            _phantom: PhantomData,
        }
    }

    pub fn write_with_cpu<R>(&mut self, len: usize, f: impl FnOnce(&mut [T]) -> R) -> R {
        assert!(len <= self.len(), "range out of bounds");
        let data = unsafe { self.as_mut_slice_cpu() };
        f(&mut data[..len])
    }

    pub fn read_with_cpu<R>(&self, len: usize, f: impl FnOnce(&[T]) -> R) -> R {
        assert!(len <= self.len(), "range out of bounds");
        let data = unsafe { core::slice::from_raw_parts(self.as_ptr().as_ptr(), len) };
        f(data)
    }

    pub fn as_ptr(&self) -> NonNull<T> {
        self.data.handle().as_ptr().cast::<T>()
    }

    pub fn as_slice_cpu(&self) -> &[T] {
        unsafe { core::slice::from_raw_parts(self.as_ptr().as_ptr(), self.len()) }
    }

    /// # Safety
    ///
    /// The caller must ensure the device is not concurrently accessing this
    /// memory in a way that races with CPU writes.
    pub unsafe fn as_mut_slice_cpu(&mut self) -> &mut [T] {
        unsafe { core::slice::from_raw_parts_mut(self.as_ptr().as_ptr(), self.len()) }
    }

    pub fn try_release(mut self) -> Result<(), DmaError> {
        self.data.try_release()
    }
}

pub struct ContiguousArray<T: DmaPod> {
    data: DmaAllocation,
    _phantom: PhantomData<T>,
}

unsafe impl<T: DmaPod + Send> Send for ContiguousArray<T> {}
unsafe impl<T: DmaPod + Sync> Sync for ContiguousArray<T> {}

impl<T: DmaPod> ContiguousArray<T> {
    pub(crate) fn new_zero_with_align(
        os: &DeviceDma,
        len: usize,
        align: usize,
        direction: DmaDirection,
    ) -> Result<Self, DmaError> {
        let layout = array_layout::<T>(len, align)?;
        Ok(Self {
            data: DmaAllocation::new_zero_contiguous(os, layout, direction)?,
            _phantom: PhantomData,
        })
    }

    pub(crate) fn new_zero(
        os: &DeviceDma,
        len: usize,
        direction: DmaDirection,
    ) -> Result<Self, DmaError> {
        Self::new_zero_with_align(os, len, core::mem::align_of::<T>(), direction)
    }

    pub fn dma_addr(&self) -> DmaAddr {
        self.data.handle().dma_addr()
    }

    pub fn len(&self) -> usize {
        len_from_bytes::<T>(self.data.handle().size())
    }

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

    pub fn bytes_len(&self) -> usize {
        self.data.handle().size()
    }

    pub fn domain_id(&self) -> DmaDomainId {
        self.data.device.info().domain()
    }

    pub fn coherency(&self) -> DmaCoherency {
        self.data.device.info().coherency()
    }

    pub fn direction(&self) -> DmaDirection {
        match self.data.kind {
            AllocationKind::Contiguous { direction } => direction,
            AllocationKind::Coherent => unreachable!("ContiguousArray cannot hold coherent DMA"),
        }
    }

    pub fn read_cpu(&self, index: usize) -> Option<T> {
        read_at(self.as_ptr(), self.len(), index)
    }

    pub fn set_cpu(&mut self, index: usize, value: T) {
        write_at(self.as_ptr(), self.len(), index, value);
    }

    pub fn copy_from_slice_cpu(&mut self, src: &[T]) {
        copy_from_slice(self.as_ptr(), self.len(), src);
    }

    pub fn iter_cpu(&self) -> ArrayCpuIter<'_, T, Self> {
        ArrayCpuIter {
            array: self,
            index: 0,
            _phantom: PhantomData,
        }
    }

    pub fn prepare_for_device(&self, range: Range<usize>) {
        self.check_range(&range);
        self.data.sync_for_device(range.start, range.len());
    }

    pub fn complete_for_cpu(&self, range: Range<usize>) {
        self.check_range(&range);
        self.data.sync_for_cpu(range.start, range.len());
    }

    pub fn write_for_device<R>(&mut self, len: usize, f: impl FnOnce(&mut [T]) -> R) -> R {
        let ret = self.write_with_cpu(len, f);
        self.prepare_for_device(0..len * core::mem::size_of::<T>());
        ret
    }

    pub fn read_from_device<R>(&self, len: usize, f: impl FnOnce(&[T]) -> R) -> R {
        let size = len * core::mem::size_of::<T>();
        self.complete_for_cpu(0..size);
        self.read_with_cpu(len, f)
    }

    pub fn copy_to_device_from_slice(&mut self, src: &[T]) {
        self.copy_from_slice_cpu(src);
        self.prepare_for_device(0..core::mem::size_of_val(src));
    }

    pub fn copy_from_device_to_slice(&self, dst: &mut [T]) {
        self.read_from_device(dst.len(), |src| dst.copy_from_slice(src));
    }

    pub fn write_with_cpu<R>(&mut self, len: usize, f: impl FnOnce(&mut [T]) -> R) -> R {
        assert!(len <= self.len(), "range out of bounds");
        {
            let data = unsafe { self.as_mut_slice_cpu() };
            f(&mut data[..len])
        }
    }

    pub fn read_with_cpu<R>(&self, len: usize, f: impl FnOnce(&[T]) -> R) -> R {
        assert!(len <= self.len(), "range out of bounds");
        let data = unsafe { core::slice::from_raw_parts(self.as_ptr().as_ptr(), len) };
        f(data)
    }

    pub fn as_ptr(&self) -> NonNull<T> {
        self.data.handle().as_ptr().cast::<T>()
    }

    pub fn as_slice_cpu(&self) -> &[T] {
        unsafe { core::slice::from_raw_parts(self.as_ptr().as_ptr(), self.len()) }
    }

    /// # Safety
    ///
    /// The caller must ensure the device is not concurrently accessing this
    /// memory in a way that races with CPU writes.
    pub unsafe fn as_mut_slice_cpu(&mut self) -> &mut [T] {
        unsafe { core::slice::from_raw_parts_mut(self.as_ptr().as_ptr(), self.len()) }
    }

    fn check_range(&self, range: &Range<usize>) {
        assert!(
            range.start <= range.end && range.end <= self.bytes_len(),
            "range out of bounds, range: {:?}, bytes_len: {}",
            range,
            self.bytes_len()
        );
    }
}

pub trait DmaArrayCpuRead<T: DmaPod> {
    fn len(&self) -> usize;
    fn is_empty(&self) -> bool;
    fn read_cpu(&self, index: usize) -> Option<T>;
}

impl<T: DmaPod> DmaArrayCpuRead<T> for CoherentArray<T> {
    fn len(&self) -> usize {
        CoherentArray::len(self)
    }

    fn is_empty(&self) -> bool {
        CoherentArray::is_empty(self)
    }

    fn read_cpu(&self, index: usize) -> Option<T> {
        CoherentArray::read_cpu(self, index)
    }
}

impl<T: DmaPod> DmaArrayCpuRead<T> for ContiguousArray<T> {
    fn len(&self) -> usize {
        ContiguousArray::len(self)
    }

    fn is_empty(&self) -> bool {
        ContiguousArray::is_empty(self)
    }

    fn read_cpu(&self, index: usize) -> Option<T> {
        ContiguousArray::read_cpu(self, index)
    }
}

pub struct ArrayCpuIter<'a, T: DmaPod, A: DmaArrayCpuRead<T>> {
    array: &'a A,
    index: usize,
    _phantom: PhantomData<T>,
}

impl<'a, T: DmaPod, A: DmaArrayCpuRead<T>> Iterator for ArrayCpuIter<'a, T, A> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        if self.index >= self.array.len() {
            return None;
        }
        let value = self.array.read_cpu(self.index);
        self.index += 1;
        value
    }
}

fn array_layout<T>(len: usize, align: usize) -> Result<Layout, DmaError> {
    let size = len
        .checked_mul(core::mem::size_of::<T>())
        .ok_or(DmaError::LayoutError(
            Layout::from_size_align(usize::MAX, 1).unwrap_err(),
        ))?;
    Ok(Layout::from_size_align(
        size,
        align.max(core::mem::align_of::<T>()),
    )?)
}

fn len_from_bytes<T>(bytes: usize) -> usize {
    if core::mem::size_of::<T>() == 0 {
        0
    } else {
        bytes / core::mem::size_of::<T>()
    }
}

fn read_at<T: DmaPod>(ptr: NonNull<T>, len: usize, index: usize) -> Option<T> {
    if index >= len {
        return None;
    }
    Some(unsafe { ptr.add(index).read() })
}

fn write_at<T: DmaPod>(ptr: NonNull<T>, len: usize, index: usize, value: T) {
    assert!(
        index < len,
        "index out of range, index: {}, len: {}",
        index,
        len
    );
    unsafe { ptr.add(index).write(value) };
}

fn copy_from_slice<T: DmaPod>(ptr: NonNull<T>, len: usize, src: &[T]) {
    assert!(
        src.len() <= len,
        "source slice is larger than DMA array, src len: {}, array len: {}",
        src.len(),
        len
    );
    unsafe {
        ptr.as_ptr()
            .copy_from_nonoverlapping(src.as_ptr(), src.len());
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn array_helper_len_and_layout_rules_hold_for_test() -> bool {
        // len_from_bytes: normal types
        assert!(len_from_bytes::<u8>(100) == 100);
        assert!(len_from_bytes::<u16>(100) == 50);
        assert!(len_from_bytes::<u32>(100) == 25);
        assert!(len_from_bytes::<u64>(100) == 12);

        // array_layout: valid layout succeeds
        let layout = array_layout::<u8>(100, 1);
        assert!(layout.is_ok());
        let l = layout.unwrap();
        assert!(l.size() == 100);

        // array_layout: overflow on size returns error
        let overflow = array_layout::<u8>(usize::MAX, 1);
        assert!(overflow.is_err());

        // array_layout: alignment must be power of 2
        let bad_align = array_layout::<u8>(10, 3); // 3 is not power of 2
        assert!(bad_align.is_err());

        // len_from_bytes: zero bytes returns 0
        assert!(len_from_bytes::<u32>(0) == 0);

        // array_layout: zero length is valid
        let empty = array_layout::<u32>(0, 4);
        assert!(empty.is_ok());
        assert!(empty.unwrap().size() == 0);

        // len_from_bytes: u16 with odd bytes
        assert!(len_from_bytes::<u16>(5) == 2); // 5/2 = 2 (integer division)

        // array_layout: large alignment
        let large_align = array_layout::<u8>(16, 4096); // page-aligned
        assert!(large_align.is_ok());
        assert!(large_align.unwrap().align() == 4096);

        true
    }

    fn array_contiguous_methods_hold_for_test() -> bool {
        // Test ContiguousArray-specific methods that may not be covered
        // These are tested indirectly but we verify the helpers exist
        assert!(array_helper_len_and_layout_rules_hold_for_test());

        // Test len_from_bytes with different types
        assert!(len_from_bytes::<u8>(100) == 100);
        assert!(len_from_bytes::<u16>(100) == 50);
        assert!(len_from_bytes::<u32>(100) == 25);
        assert!(len_from_bytes::<u64>(100) == 12);

        true
    }

    fn array_read_at_write_at_helpers_hold_for_test() -> bool {
        // Test that read_at and write_at helper functions exist
        // These are tested through array operations but we verify basic logic
        assert!(len_from_bytes::<u8>(0) == 0);
        assert!(len_from_bytes::<u32>(0) == 0);

        // Test array_layout with zero size
        let empty = array_layout::<u8>(0, 1);
        assert!(empty.is_ok());
        assert!(empty.unwrap().size() == 0);

        true
    }

    fn array_dma_array_cpu_read_trait_hold_for_test() -> bool {
        // Test DmaArrayCpuRead trait methods exist
        // These are tested through CoherentArray and ContiguousArray but we verify helpers
        assert!(len_from_bytes::<u8>(100) == 100);
        assert!(len_from_bytes::<u16>(50) == 25);

        true
    }

    fn array_layout_edge_cases_comprehensive_hold_for_test() -> bool {
        // Comprehensive edge case tests for array_layout and len_from_bytes

        // len_from_bytes: zero-sized types return 0
        assert!(len_from_bytes::<()>(100) == 0);

        // array_layout: size=1, align=1
        let tiny = array_layout::<u8>(1, 1);
        assert!(tiny.is_ok());
        assert_eq!(tiny.unwrap().size(), 1);

        // array_layout: alignment must be power of 2 (3 is not)
        assert!(array_layout::<u8>(10, 3).is_err());

        // array_layout: alignment of 2 is valid
        assert!(array_layout::<u16>(5, 2).is_ok());

        // len_from_bytes: exact division
        assert_eq!(len_from_bytes::<u32>(40), 10); // 40/4 = 10

        // len_from_bytes: non-exact division truncates
        assert_eq!(len_from_bytes::<u32>(42), 10); // 42/4 = 10 (truncated)

        true
    }

    fn array_copy_from_slice_and_write_at_edge_hold_for_test() -> bool {
        // Test copy_from_slice and write_at logic through helpers

        // len_from_bytes with u8 (size 1, no truncation)
        assert_eq!(len_from_bytes::<u8>(0), 0);
        assert_eq!(len_from_bytes::<u8>(1), 1);
        assert_eq!(len_from_bytes::<u8>(255), 255);

        // len_from_bytes with u16 (size 2)
        assert_eq!(len_from_bytes::<u16>(0), 0);
        assert_eq!(len_from_bytes::<u16>(2), 1);
        assert_eq!(len_from_bytes::<u16>(3), 1); // 3/2 = 1
        assert_eq!(len_from_bytes::<u16>(4), 2);

        // len_from_bytes with u64 (size 8)
        assert_eq!(len_from_bytes::<u64>(0), 0);
        assert_eq!(len_from_bytes::<u64>(7), 0); // 7/8 = 0
        assert_eq!(len_from_bytes::<u64>(8), 1);
        assert_eq!(len_from_bytes::<u64>(15), 1); // 15/8 = 1
        assert_eq!(len_from_bytes::<u64>(16), 2);

        // array_layout: various alignments
        // align=1 always valid for any size
        assert!(array_layout::<u8>(0, 1).is_ok());
        assert!(array_layout::<u8>(1, 1).is_ok());
        assert!(array_layout::<u8>(256, 1).is_ok());

        // align=2: size must be valid
        assert!(array_layout::<u16>(1, 2).is_ok()); // size=2, align=2
        assert!(array_layout::<u16>(100, 2).is_ok()); // size=200, align=2

        // align=4: for u32
        assert!(array_layout::<u32>(10, 4).is_ok()); // size=40, align=4
        assert!(array_layout::<u32>(0, 4).is_ok()); // size=0, align=4

        // align=8: for u64
        assert!(array_layout::<u64>(5, 8).is_ok()); // size=40, align=8

        // Invalid alignments (not power of 2)
        assert!(array_layout::<u8>(10, 3).is_err()); // 3 not power of 2
        assert!(array_layout::<u8>(10, 5).is_err()); // 5 not power of 2
        assert!(array_layout::<u8>(10, 6).is_err()); // 6 not power of 2
        assert!(array_layout::<u8>(10, 7).is_err()); // 7 not power of 2
        assert!(array_layout::<u8>(10, 9).is_err()); // 9 not power of 2

        true
    }

    fn array_layout_overflow_and_size_align_hold_for_test() -> bool {
        // Test array_layout overflow detection and size/align relationships

        // Overflow: usize::MAX * size_of::<T>() overflows
        let overflow_u8 = array_layout::<u8>(usize::MAX, 1);
        assert!(overflow_u8.is_err());

        let overflow_u16 = array_layout::<u16>(usize::MAX / 2 + 1, 2);
        assert!(overflow_u16.is_err());

        let overflow_u32 = array_layout::<u32>(usize::MAX / 4 + 1, 4);
        assert!(overflow_u32.is_err());

        // Valid large sizes (no overflow)
        let large = array_layout::<u8>(1024 * 1024, 4096);
        assert!(large.is_ok());
        let l = large.unwrap();
        assert_eq!(l.size(), 1024 * 1024);
        assert_eq!(l.align(), 4096);

        // Size 0 with any valid alignment
        assert!(array_layout::<u8>(0, 1).is_ok());
        assert!(array_layout::<u8>(0, 2).is_ok());
        assert!(array_layout::<u8>(0, 4).is_ok());
        assert!(array_layout::<u8>(0, 8).is_ok());
        assert!(array_layout::<u8>(0, 16).is_ok());
        assert!(array_layout::<u8>(0, 32).is_ok());
        assert!(array_layout::<u8>(0, 64).is_ok());
        assert!(array_layout::<u8>(0, 128).is_ok());
        assert!(array_layout::<u8>(0, 256).is_ok());
        assert!(array_layout::<u8>(0, 512).is_ok());
        assert!(array_layout::<u8>(0, 1024).is_ok());
        assert!(array_layout::<u8>(0, 2048).is_ok());
        assert!(array_layout::<u8>(0, 4096).is_ok());

        // align max uses max(align, align_of::<T>())
        // For u8 (align 1), requested align is used
        let a1 = array_layout::<u8>(10, 16).unwrap();
        assert_eq!(a1.align(), 16);

        // For u16 (align 2), requested align < 2 should use 2
        let a2 = array_layout::<u16>(10, 1).unwrap();
        assert_eq!(a2.align(), 2); // max(1, 2) = 2

        true
    }

    #[test]
    fn array_helper_len_and_layout_rules_hold() {
        assert!(array_helper_len_and_layout_rules_hold_for_test());
    }

    #[test]
    fn array_contiguous_methods_hold() {
        assert!(array_contiguous_methods_hold_for_test());
    }

    #[test]
    fn array_read_at_write_at_helpers_hold() {
        assert!(array_read_at_write_at_helpers_hold_for_test());
    }

    #[test]
    fn array_dma_array_cpu_read_trait_hold() {
        assert!(array_dma_array_cpu_read_trait_hold_for_test());
    }

    #[test]
    fn array_layout_edge_cases_comprehensive_hold() {
        assert!(array_layout_edge_cases_comprehensive_hold_for_test());
    }

    #[test]
    fn array_copy_from_slice_and_write_at_edge_hold() {
        assert!(array_copy_from_slice_and_write_at_edge_hold_for_test());
    }

    #[test]
    fn array_layout_overflow_and_size_align_hold() {
        assert!(array_layout_overflow_and_size_align_hold_for_test());
    }
}