Skip to content

Commit 75eb07d

Browse files
hoshinolinamarcan
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 <[email protected]>
1 parent da2ab13 commit 75eb07d

4 files changed

Lines changed: 98 additions & 0 deletions

File tree

rust/bindings/bindings_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <drm/drm_gem.h>
1313
#include <drm/drm_gem_shmem_helper.h>
1414
#include <drm/drm_ioctl.h>
15+
#include <drm/drm_syncobj.h>
1516
#include <linux/delay.h>
1617
#include <linux/device.h>
1718
#include <linux/dma-fence.h>

rust/helpers.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include <drm/drm_gem.h>
2222
#include <drm/drm_gem_shmem_helper.h>
23+
#include <drm/drm_syncobj.h>
2324
#include <linux/bug.h>
2425
#include <linux/build_bug.h>
2526
#include <linux/device.h>
@@ -492,6 +493,24 @@ __u64 rust_helper_drm_vma_node_offset_addr(struct drm_vma_offset_node *node)
492493
}
493494
EXPORT_SYMBOL_GPL(rust_helper_drm_vma_node_offset_addr);
494495

496+
void rust_helper_drm_syncobj_get(struct drm_syncobj *obj)
497+
{
498+
drm_syncobj_get(obj);
499+
}
500+
EXPORT_SYMBOL_GPL(rust_helper_drm_syncobj_get);
501+
502+
void rust_helper_drm_syncobj_put(struct drm_syncobj *obj)
503+
{
504+
drm_syncobj_put(obj);
505+
}
506+
EXPORT_SYMBOL_GPL(rust_helper_drm_syncobj_put);
507+
508+
struct dma_fence *rust_helper_drm_syncobj_fence_get(struct drm_syncobj *syncobj)
509+
{
510+
return drm_syncobj_fence_get(syncobj);
511+
}
512+
EXPORT_SYMBOL_GPL(rust_helper_drm_syncobj_fence_get);
513+
495514
#ifdef CONFIG_DRM_GEM_SHMEM_HELPER
496515

497516
void rust_helper_drm_gem_shmem_object_free(struct drm_gem_object *obj)

rust/kernel/drm/mod.rs

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

rust/kernel/drm/syncobj.rs

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

0 commit comments

Comments
 (0)