dma-api 0.7.1

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
#![cfg(all(test, any(unix, windows)))]

mod test_helpers;

use std::{num::NonZeroUsize, ptr::NonNull};

use dma_api::*;
use test_helpers::{DmaOperation, TrackingDmaOp};

#[test]
fn test_read() {
    let mut dma: DArray<u32> = new_api()
        .array_zero_with_align(10, 0x1000, DmaDirection::FromDevice)
        .unwrap();

    dma.set(0, 1);

    let o = dma.read(0).unwrap();

    assert_eq!(o, 1);
}

#[test]
fn test_write() {
    let mut dma: DArray<u32> = new_api()
        .array_zero_with_align(10, 0x1000, DmaDirection::ToDevice)
        .unwrap();

    dma.set(0, 1);

    let o = dma.read(0).unwrap();

    assert_eq!(o, 1);
}
#[derive(Debug, PartialEq, Eq)]
struct Foo {
    foo: u32,
    bar: u32,
}

#[test]
fn test_modify() {
    let mut dma: DBox<Foo> = new_api()
        .box_zero_with_align(64, DmaDirection::Bidirectional)
        .unwrap();

    dma.modify(|f| f.bar = 1);

    assert_eq!(dma.read(), Foo { foo: 0, bar: 1 });
}

#[test]
fn test_copy() {
    let mut dma = new_api()
        .array_zero_with_align::<u32>(0x40, 0x1000, DmaDirection::Bidirectional)
        .unwrap();

    println!("new dma ok");

    let src = [1u32; 0x40];

    dma.copy_from_slice(&src);

    println!("copy ok");

    for (i, &v) in src.iter().enumerate() {
        assert_eq!(dma.read(i).unwrap(), v);
    }
}

#[test]
fn test_index() {
    let dma = new_api()
        .array_zero_with_align::<u64>(0x40, 0x1000, DmaDirection::Bidirectional)
        .unwrap();

    println!("new dma ok");

    let a = dma.read(0).unwrap();

    assert_eq!(a, 0);
}

#[test]
fn mask_check_rejects_overflow_alloc() {
    static DMA: MaskedDma = MaskedDma;
    let dev = DeviceDma::new(0x0fff, &DMA);

    let err = dev.array_zero_with_align::<u8>(0x1000, 0x1000, DmaDirection::ToDevice);

    assert!(matches!(err, Err(DmaError::DmaMaskNotMatch { .. })));
}

#[test]
fn mask_check_rejects_overflow_map() {
    static DMA: MaskedDma = MaskedDma;
    let dev = DeviceDma::new(0x0fff, &DMA);

    let mut buf = [0u8; 0x1000];

    let err = dev.map_single_array(&buf, 64, DmaDirection::FromDevice);

    assert!(matches!(err, Err(DmaError::DmaMaskNotMatch { .. })));
}

fn new_api() -> DeviceDma {
    static IMPL: Impled = Impled;
    DeviceDma::new(u64::MAX, &IMPL)
}

struct Impled;

impl DmaOp for Impled {
    fn page_size(&self) -> usize {
        0x1000
    }

    unsafe fn map_single(
        &self,
        _dma_mask: u64,
        addr: NonNull<u8>,
        size: NonZeroUsize,
        align: usize,
        _direction: DmaDirection,
    ) -> Result<DmaMapHandle, DmaError> {
        println!(
            "map_single @{:?}, size {:#x}, align: {:#x}",
            addr,
            size.get(),
            align
        );
        let layout = core::alloc::Layout::from_size_align(size.get(), align)?;
        Ok(unsafe { DmaMapHandle::new(addr, (addr.as_ptr() as u64).into(), layout, None) })
    }

    unsafe fn unmap_single(&self, handle: DmaMapHandle) {
        println!(
            "unmap_single @{:?}, size {:#x}",
            handle.as_ptr(),
            handle.size()
        );
    }

    fn flush(&self, addr: std::ptr::NonNull<u8>, size: usize) {
        println!("flush @{:?}, size {size:#x}", addr);
    }

    fn invalidate(&self, addr: std::ptr::NonNull<u8>, size: usize) {
        println!("invalidate @{:?}, size {size:#x}", addr);
    }

    unsafe fn alloc_coherent(
        &self,
        _dma_mask: u64,
        layout: core::alloc::Layout,
    ) -> Option<DmaHandle> {
        println!(
            "alloc_coherent size: {:#x}, align: {:#x}",
            layout.size(),
            layout.align()
        );
        let ptr = unsafe { std::alloc::alloc_zeroed(layout) };
        if ptr.is_null() {
            return None;
        }
        Some(unsafe { DmaHandle::new(NonNull::new(ptr).unwrap(), (ptr as u64).into(), layout) })
    }

    unsafe fn dealloc_coherent(&self, handle: DmaHandle) {
        unsafe { std::alloc::dealloc(handle.as_ptr().as_ptr(), handle.layout()) };
    }
}

struct MaskedDma;

impl DmaOp for MaskedDma {
    fn page_size(&self) -> usize {
        0x1000
    }

    unsafe fn map_single(
        &self,
        _dma_mask: u64,
        addr: NonNull<u8>,
        size: NonZeroUsize,
        align: usize,
        _direction: DmaDirection,
    ) -> Result<DmaMapHandle, DmaError> {
        let layout = core::alloc::Layout::from_size_align(size.get(), align)?;
        Ok(unsafe { DmaMapHandle::new(addr, 0x1000u64.into(), layout, None) })
    }

    unsafe fn unmap_single(&self, _handle: DmaMapHandle) {}

    fn flush(&self, _addr: std::ptr::NonNull<u8>, _size: usize) {}

    fn invalidate(&self, _addr: std::ptr::NonNull<u8>, _size: usize) {}

    unsafe fn alloc_coherent(
        &self,
        _dma_mask: u64,
        layout: core::alloc::Layout,
    ) -> Option<DmaHandle> {
        let ptr = unsafe { std::alloc::alloc_zeroed(layout) };
        if ptr.is_null() {
            return None;
        }
        Some(unsafe { DmaHandle::new(NonNull::new(ptr).unwrap(), 0x1000u64.into(), layout) })
    }

    unsafe fn dealloc_coherent(&self, handle: DmaHandle) {
        unsafe { std::alloc::dealloc(handle.as_ptr().as_ptr(), handle.layout()) };
    }
}

// ============================================================================
// 新增测试: 地址正确性测试
// ============================================================================

#[test]
fn test_single_element_address() {
    let tracker = Box::new(TrackingDmaOp::new(0));
    let tracker: &'static TrackingDmaOp = Box::leak(tracker);
    let dev = DeviceDma::new(u64::MAX, tracker);

    let mut dma: DArray<u32> = dev
        .array_zero_with_align(1, 64, DmaDirection::ToDevice)
        .unwrap();

    tracker.clear();
    dma.set(0, 0x12345678);

    // 验证有且仅有一次 flush 操作,大小为 4 字节 (u32)
    let ops = tracker.get_operations();
    let flush_ops: Vec<_> = ops
        .iter()
        .filter_map(|op| {
            if let DmaOperation::Flush { size, .. } = op {
                Some(*size)
            } else {
                None
            }
        })
        .collect();

    assert_eq!(flush_ops.len(), 1);
    assert_eq!(flush_ops[0], 4); // u32 = 4 bytes
}

#[test]
fn test_multi_element_offset() {
    let tracker = Box::new(TrackingDmaOp::new(0));
    let tracker: &'static TrackingDmaOp = Box::leak(tracker);
    let dev = DeviceDma::new(u64::MAX, tracker);

    let mut dma: DArray<u32> = dev
        .array_zero_with_align(10, 64, DmaDirection::ToDevice)
        .unwrap();
    tracker.clear();

    // 设置 index 0, 1, 2
    dma.set(0, 0);
    dma.set(1, 1);
    dma.set(2, 2);

    // u32 = 4 bytes, 应该有 3 次 flush 操作,每次大小都是 4 字节
    let ops = tracker.get_operations();
    let flush_ops: Vec<_> = ops
        .iter()
        .filter_map(|op| {
            if let DmaOperation::Flush { size, addr } = op {
                Some((*addr, *size))
            } else {
                None
            }
        })
        .collect();

    assert_eq!(flush_ops.len(), 3);

    // 验证每次 flush 的大小都是 4 字节
    for (addr, size) in &flush_ops {
        assert_eq!(*size, 4);
    }

    // 验证地址是递增的,每次增加 4 字节
    assert_eq!(flush_ops[1].0 - flush_ops[0].0, 4); // index 1 - index 0
    assert_eq!(flush_ops[2].0 - flush_ops[1].0, 4); // index 2 - index 1
}

#[test]
fn test_different_type_sizes() {
    let tracker = Box::new(TrackingDmaOp::new(0));
    let tracker: &'static TrackingDmaOp = Box::leak(tracker);
    let dev = DeviceDma::new(u64::MAX, tracker);

    // u8 = 1 byte
    let mut dma_u8: DArray<u8> = dev
        .array_zero_with_align(5, 8, DmaDirection::ToDevice)
        .unwrap();
    tracker.clear();
    dma_u8.set(3, 1);

    let ops = tracker.get_operations();
    if let Some(DmaOperation::Flush { size, .. }) = ops.last() {
        assert_eq!(*size, 1); // u8 = 1 byte
    } else {
        panic!("Expected Flush operation");
    }

    // u64 = 8 bytes
    let mut dma_u64: DArray<u64> = dev
        .array_zero_with_align(5, 8, DmaDirection::ToDevice)
        .unwrap();
    tracker.clear();
    dma_u64.set(2, 2);

    let ops = tracker.get_operations();
    if let Some(DmaOperation::Flush { size, .. }) = ops.last() {
        assert_eq!(*size, 8); // u64 = 8 bytes
    } else {
        panic!("Expected Flush operation");
    }
}

// ============================================================================
// 新增测试: DmaDirection 行为测试
// ============================================================================

#[test]
fn test_direction_to_device() {
    let tracker = Box::new(TrackingDmaOp::new(0x1000));
    let tracker: &'static TrackingDmaOp = Box::leak(tracker);
    let dev = DeviceDma::new(u64::MAX, tracker);
    let mut dma: DArray<u32> = dev
        .array_zero_with_align(10, 64, DmaDirection::ToDevice)
        .unwrap();

    tracker.clear();

    // set 应该 flush
    dma.set(0, 1);
    assert_eq!(tracker.count_flush(), 1);
    assert_eq!(tracker.count_invalidate(), 0);

    // read 不应该 inv
    dma.read(0);
    assert_eq!(tracker.count_flush(), 1); // 没有增加
    assert_eq!(tracker.count_invalidate(), 0); // 没有增加
}

#[test]
fn test_direction_from_device() {
    let tracker = Box::new(TrackingDmaOp::new(0x1000));
    let tracker: &'static TrackingDmaOp = Box::leak(tracker);
    let dev = DeviceDma::new(u64::MAX, tracker);
    let mut dma: DArray<u32> = dev
        .array_zero_with_align(10, 64, DmaDirection::FromDevice)
        .unwrap();

    tracker.clear();

    // read 应该 inv
    dma.read(0);
    assert_eq!(tracker.count_flush(), 0);
    assert_eq!(tracker.count_invalidate(), 1);

    // set 不应该 flush
    tracker.clear();
    dma.set(0, 1);
    assert_eq!(tracker.count_flush(), 0);
    assert_eq!(tracker.count_invalidate(), 0);
}

#[test]
fn test_direction_bidirectional() {
    let tracker = Box::new(TrackingDmaOp::new(0x1000));
    let tracker: &'static TrackingDmaOp = Box::leak(tracker);
    let dev = DeviceDma::new(u64::MAX, tracker);
    let mut dma: DArray<u32> = dev
        .array_zero_with_align(10, 64, DmaDirection::Bidirectional)
        .unwrap();

    tracker.clear();

    // set 应该 flush
    dma.set(0, 1);
    assert_eq!(tracker.count_flush(), 1);

    // read 应该 inv
    dma.read(0);
    assert_eq!(tracker.count_invalidate(), 1);
}

// ============================================================================
// 新增测试: Drop 行为测试
// ============================================================================

// 简单的对齐缓冲区模块
mod align_alloc {
    use std::ptr::NonNull;

    pub struct AlignedBuffer<const ALIGNMENT: usize> {
        ptr: NonNull<u8>,
        _size: usize,
    }

    impl<const ALIGNMENT: usize> AlignedBuffer<ALIGNMENT> {
        pub fn new() -> Self {
            // 分配对齐的内存
            let layout = std::alloc::Layout::from_size_align(0x1000, ALIGNMENT).unwrap();
            let ptr = unsafe { std::alloc::alloc_zeroed(layout) };
            assert!(!ptr.is_null(), "Failed to allocate aligned buffer");

            Self {
                ptr: NonNull::new(ptr).unwrap(),
                _size: 0x1000,
            }
        }

        pub fn as_mut_ptr(&mut self) -> *mut u8 {
            self.ptr.as_ptr()
        }
    }

    impl<const ALIGNMENT: usize> Drop for AlignedBuffer<ALIGNMENT> {
        fn drop(&mut self) {
            let layout = std::alloc::Layout::from_size_align(0x1000, ALIGNMENT).unwrap();
            unsafe { std::alloc::dealloc(self.ptr.as_ptr(), layout) };
        }
    }
}