dma_api/
lib.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
#![cfg_attr(not(test), no_std)]
#![doc = include_str!("../README.md")]

extern crate alloc;

use core::ptr::NonNull;

mod dma;

pub use dma::r#box::DBox;
pub use dma::slice::{DSlice, DSliceMut};
pub use dma::vec::DVec;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
    ToDevice,
    FromDevice,
    Bidirectional,
}

pub trait Impl {
    /// map virt address to physical address
    fn map(addr: NonNull<u8>, size: usize, direction: Direction) -> u64;
    /// unmap virt address
    fn unmap(addr: NonNull<u8>, size: usize);
    /// write cache back to memory
    fn flush(addr: NonNull<u8>, size: usize);
    /// invalidate cache
    fn invalidate(addr: NonNull<u8>, size: usize);
}

extern "Rust" {
    fn __dma_api_map(addr: NonNull<u8>, size: usize, direction: Direction) -> u64;
    fn __dma_api_unmap(addr: NonNull<u8>, size: usize);
    fn __dma_api_flush(addr: NonNull<u8>, size: usize);
    fn __dma_api_invalidate(addr: NonNull<u8>, size: usize);
}

fn map(addr: NonNull<u8>, size: usize, direction: Direction) -> u64 {
    unsafe { __dma_api_map(addr, size, direction) }
}

fn unmap(addr: NonNull<u8>, size: usize) {
    unsafe { __dma_api_unmap(addr, size) }
}

fn flush(addr: NonNull<u8>, size: usize) {
    unsafe { __dma_api_flush(addr, size) }
}

fn invalidate(addr: NonNull<u8>, size: usize) {
    unsafe { __dma_api_invalidate(addr, size) }
}

#[macro_export]
macro_rules! set_impl {
    ($t: ty) => {
        #[no_mangle]
        fn __dma_api_map(
            addr: core::ptr::NonNull<u8>,
            size: usize,
            direction: $crate::Direction,
        ) -> u64 {
            <$t as $crate::Impl>::map(addr, size, direction)
        }
        #[no_mangle]
        fn __dma_api_unmap(addr: core::ptr::NonNull<u8>, size: usize) {
            <$t as $crate::Impl>::unmap(addr, size)
        }
        #[no_mangle]
        fn __dma_api_flush(addr: core::ptr::NonNull<u8>, size: usize) {
            <$t as $crate::Impl>::flush(addr, size)
        }
        #[no_mangle]
        fn __dma_api_invalidate(addr: core::ptr::NonNull<u8>, size: usize) {
            <$t as $crate::Impl>::invalidate(addr, size)
        }
    };
}