Skip to content

Commit 23b71c1

Browse files
Lyudejannau
authored andcommitted
rust: Add dma_buf stub bindings
In order to implement the gem export callback, we need a type to represent struct dma_buf. So - this commit introduces a set of stub bindings for dma_buf. These bindings provide a ref-counted DmaBuf object, but don't currently implement any functionality for using the DmaBuf. Signed-off-by: Lyude Paul <lyude@redhat.com>
1 parent cc8e784 commit 23b71c1

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

rust/kernel/dma_buf.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! DMA buffer API
4+
//!
5+
//! C header: [`include/linux/dma-buf.h`](srctree/include/linux/dma-buf.h)
6+
7+
use bindings;
8+
use kernel::types::*;
9+
10+
/// A DMA buffer object.
11+
///
12+
/// # Invariants
13+
///
14+
/// The data layout of this type is equivalent to that of `struct dma_buf`.
15+
#[repr(transparent)]
16+
pub struct DmaBuf(Opaque<bindings::dma_buf>);
17+
18+
// SAFETY: `struct dma_buf` is thread-safe
19+
unsafe impl Send for DmaBuf {}
20+
// SAFETY: `struct dma_buf` is thread-safe
21+
unsafe impl Sync for DmaBuf {}
22+
23+
impl DmaBuf {
24+
/// Convert from a `*mut bindings::dma_buf` to a [`DmaBuf`].
25+
///
26+
/// # Safety
27+
///
28+
/// The caller guarantees that `self_ptr` points to a valid initialized `struct dma_buf` for the
29+
/// duration of the lifetime of `'a`, and promises to not violate rust's data aliasing rules
30+
/// using the reference provided by this function.
31+
pub(crate) unsafe fn as_ref<'a>(self_ptr: *mut bindings::dma_buf) -> &'a Self {
32+
// SAFETY: Our data layout is equivalent to `dma_buf` .
33+
unsafe { &*self_ptr.cast() }
34+
}
35+
36+
pub(crate) fn as_raw(&self) -> *mut bindings::dma_buf {
37+
self.0.get()
38+
}
39+
}

rust/kernel/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ pub mod device;
100100
pub mod device_id;
101101
pub mod devres;
102102
pub mod dma;
103+
pub mod dma_buf;
104+
#[cfg(CONFIG_DMA_SHARED_BUFFER)]
105+
pub mod dma_fence;
103106
pub mod driver;
104107
#[cfg(CONFIG_DRM = "y")]
105108
pub mod drm;

0 commit comments

Comments
 (0)