Skip to content

Commit 5744564

Browse files
hoshinolinajannau
authored andcommitted
rust: drm: syncobj: Add DRM Sync Object abstraction
DRM Sync Objects are a container for a DMA fence, and can be waited on signaled, exported, and imported from userspace. Add a Rust abstraction so Rust DRM drivers can support this functionality. Signed-off-by: Asahi Lina <lina@asahilina.net>
1 parent 6706c83 commit 5744564

5 files changed

Lines changed: 108 additions & 0 deletions

File tree

rust/bindings/bindings_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <drm/drm_gem.h>
3636
#include <drm/drm_gem_shmem_helper.h>
3737
#include <drm/drm_ioctl.h>
38+
#include <drm/drm_syncobj.h>
3839
#include <kunit/test.h>
3940
#include <linux/auxiliary_bus.h>
4041
#include <linux/bitmap.h>

rust/helpers/drm_syncobj.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <drm/drm_syncobj.h>
4+
5+
#ifdef CONFIG_DRM
6+
7+
void rust_helper_drm_syncobj_get(struct drm_syncobj *obj)
8+
{
9+
drm_syncobj_get(obj);
10+
}
11+
12+
void rust_helper_drm_syncobj_put(struct drm_syncobj *obj)
13+
{
14+
drm_syncobj_put(obj);
15+
}
16+
17+
struct dma_fence *rust_helper_drm_syncobj_fence_get(struct drm_syncobj *syncobj)
18+
{
19+
return drm_syncobj_fence_get(syncobj);
20+
}
21+
22+
#endif

rust/helpers/helpers.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "dma-mapping.c"
3030
#include "dma-resv.c"
3131
#include "drm.c"
32+
#include "drm_syncobj.c"
3233
#include "err.c"
3334
#include "irq.c"
3435
#include "fs.c"

rust/kernel/drm/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub mod file;
88
pub mod gem;
99
pub mod ioctl;
1010
pub mod mm;
11+
pub mod syncobj;
1112

1213
pub use self::device::Device;
1314
pub use self::driver::Driver;

rust/kernel/drm/syncobj.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// SPDX-License-Identifier: GPL-2.0 OR MIT
2+
3+
//! DRM Sync Objects
4+
//!
5+
//! C header: [`include/drm/drm_syncobj.h`](../../../../include/drm/drm_syncobj.h)
6+
7+
use crate::{bindings, dma_fence::*, drm, error::Result, prelude::*};
8+
9+
/// A DRM Sync Object
10+
///
11+
/// # Invariants
12+
/// ptr is a valid pointer to a drm_syncobj and we own a reference to it.
13+
pub struct SyncObj {
14+
ptr: *mut bindings::drm_syncobj,
15+
}
16+
17+
impl SyncObj {
18+
/// Looks up a sync object by its handle for a given `File`.
19+
pub fn lookup_handle<T: drm::file::DriverFile>(
20+
file: &drm::File<T>,
21+
handle: u32,
22+
) -> Result<SyncObj> {
23+
// SAFETY: The arguments are all valid per the type invariants.
24+
let ptr = unsafe { bindings::drm_syncobj_find(file.as_raw() as *mut _, handle) };
25+
26+
if ptr.is_null() {
27+
Err(ENOENT)
28+
} else {
29+
Ok(SyncObj { ptr })
30+
}
31+
}
32+
33+
/// Returns the DMA fence associated with this sync object, if any.
34+
pub fn fence_get(&self) -> Option<Fence> {
35+
// SAFETY: self.ptr is always valid
36+
let fence = unsafe { bindings::drm_syncobj_fence_get(self.ptr) };
37+
if fence.is_null() {
38+
None
39+
} else {
40+
// SAFETY: The pointer is non-NULL and drm_syncobj_fence_get acquired an
41+
// additional reference.
42+
Some(unsafe { Fence::from_raw(fence) })
43+
}
44+
}
45+
46+
/// Replaces the DMA fence with a new one, or removes it if fence is None.
47+
pub fn replace_fence(&self, fence: Option<&Fence>) {
48+
// SAFETY: All arguments should be valid per the respective type invariants.
49+
unsafe {
50+
bindings::drm_syncobj_replace_fence(
51+
self.ptr,
52+
fence.map_or(core::ptr::null_mut(), |a| a.raw()),
53+
)
54+
};
55+
}
56+
57+
/// Adds a new timeline point to the syncobj.
58+
pub fn add_point(&self, chain: FenceChain, fence: &Fence, point: u64) {
59+
// SAFETY: All arguments should be valid per the respective type invariants.
60+
// This takes over the FenceChain ownership.
61+
unsafe { bindings::drm_syncobj_add_point(self.ptr, chain.into_raw(), fence.raw(), point) };
62+
}
63+
}
64+
65+
impl Drop for SyncObj {
66+
fn drop(&mut self) {
67+
// SAFETY: We own a reference to this syncobj.
68+
unsafe { bindings::drm_syncobj_put(self.ptr) };
69+
}
70+
}
71+
72+
impl Clone for SyncObj {
73+
fn clone(&self) -> Self {
74+
// SAFETY: `ptr` is valid per the type invariant and we own a reference to it.
75+
unsafe { bindings::drm_syncobj_get(self.ptr) };
76+
SyncObj { ptr: self.ptr }
77+
}
78+
}
79+
80+
// SAFETY: drm_syncobj operations are internally locked.
81+
unsafe impl Sync for SyncObj {}
82+
// SAFETY: drm_syncobj operations are internally locked.
83+
unsafe impl Send for SyncObj {}

0 commit comments

Comments
 (0)