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
use std::os::fd::BorrowedFd;

use rustix::{
    io::Errno,
    ioctl::{ioctl, Setter, WriteOpcode},
};

use crate::BufferError;

const DMA_BUF_BASE: u8 = b'b';
const DMA_BUF_IOCTL_SYNC: u8 = 0;

const DMA_BUF_SYNC_READ: u64 = 1 << 0;
const DMA_BUF_SYNC_WRITE: u64 = 1 << 1;
const DMA_BUF_SYNC_START: u64 = 0 << 2;
const DMA_BUF_SYNC_END: u64 = 1 << 2;

#[derive(Default)]
#[repr(C)]
struct dma_buf_sync {
    flags: u64,
}

fn dma_buf_sync_ioctl(fd: BorrowedFd<'_>, flags: u64) -> Result<(), Errno> {
    type Opcode = WriteOpcode<DMA_BUF_BASE, DMA_BUF_IOCTL_SYNC, dma_buf_sync>;

    let sync = dma_buf_sync { flags };

    // SAFETY: This function is unsafe because the opcode has to be valid, and the value type must
    // match. We have checked those, so we're good.
    let ioctl_type = unsafe { Setter::<Opcode, dma_buf_sync>::new(sync) };

    // SAFETY: This function is unsafe because the driver isn't guaranteed to implement the ioctl,
    // and to implement it properly. We don't have much of a choice and still have to trust the
    // kernel there.
    unsafe { ioctl(fd, ioctl_type) }
}

fn dma_buf_sync(fd: BorrowedFd<'_>, flags: u64) -> Result<(), BufferError> {
    dma_buf_sync_ioctl(fd, flags).map_err(|e| BufferError::FdAccess {
        reason: e.to_string(),
        source: std::io::Error::from(e),
    })
}

pub(crate) fn dma_buf_begin_cpu_read_access(fd: BorrowedFd<'_>) -> Result<(), BufferError> {
    dma_buf_sync(fd, DMA_BUF_SYNC_START | DMA_BUF_SYNC_READ)
}

pub(crate) fn dma_buf_begin_cpu_readwrite_access(fd: BorrowedFd<'_>) -> Result<(), BufferError> {
    dma_buf_sync(
        fd,
        DMA_BUF_SYNC_START | DMA_BUF_SYNC_WRITE | DMA_BUF_SYNC_READ,
    )
}

pub(crate) fn dma_buf_begin_cpu_write_access(fd: BorrowedFd<'_>) -> Result<(), BufferError> {
    dma_buf_sync(fd, DMA_BUF_SYNC_START | DMA_BUF_SYNC_WRITE)
}

pub(crate) fn dma_buf_end_cpu_read_access(fd: BorrowedFd<'_>) -> Result<(), BufferError> {
    dma_buf_sync(fd, DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ)
}

pub(crate) fn dma_buf_end_cpu_readwrite_access(fd: BorrowedFd<'_>) -> Result<(), BufferError> {
    dma_buf_sync(
        fd,
        DMA_BUF_SYNC_END | DMA_BUF_SYNC_WRITE | DMA_BUF_SYNC_READ,
    )
}

pub(crate) fn dma_buf_end_cpu_write_access(fd: BorrowedFd<'_>) -> Result<(), BufferError> {
    dma_buf_sync(fd, DMA_BUF_SYNC_END | DMA_BUF_SYNC_WRITE)
}