Skip to content

Commit 46f5f1c

Browse files
hoshinolinajannau
authored andcommitted
rust: drm/gpuvm: Add GpuVaFlags support
Signed-off-by: Asahi Lina <lina@asahilina.net>
1 parent 435479d commit 46f5f1c

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

rust/kernel/drm/gpuvm.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,66 @@ use core::ops::{Deref, DerefMut, Range};
2626
use core::ptr::NonNull;
2727
use pin_init;
2828

29+
/// GpuVaFlags to be used for a GpuVa.
30+
///
31+
/// They can be combined with the operators `|`, `&`, and `!`.
32+
#[derive(Clone, Copy, PartialEq, Default)]
33+
pub struct GpuVaFlags(u32);
34+
35+
impl GpuVaFlags {
36+
/// No GpuVaFlags (zero)
37+
pub const NONE: GpuVaFlags = GpuVaFlags(0);
38+
39+
/// The backing GEM is invalidated.
40+
pub const INVALIDATED: GpuVaFlags = GpuVaFlags(bindings::drm_gpuva_flags_DRM_GPUVA_INVALIDATED);
41+
42+
/// The GpuVa is a sparse mapping.
43+
pub const SPARSE: GpuVaFlags = GpuVaFlags(bindings::drm_gpuva_flags_DRM_GPUVA_SPARSE);
44+
45+
/// The GpuVa is a repeat mapping.
46+
pub const REPEAT: GpuVaFlags = GpuVaFlags(bindings::drm_gpuva_flags_DRM_GPUVA_REPEAT);
47+
48+
/// Construct a driver-specific GpuVaFlag.
49+
///
50+
/// The argument must be a flag index in the range [0..28].
51+
pub const fn user_flag(index: u32) -> GpuVaFlags {
52+
let flags = bindings::drm_gpuva_flags_DRM_GPUVA_USERBITS << index;
53+
assert!(flags != 0);
54+
GpuVaFlags(flags)
55+
}
56+
57+
/// Get the raw representation of this flag.
58+
pub(crate) fn as_raw(self) -> u32 {
59+
self.0
60+
}
61+
62+
/// Check whether `flags` is contained in `self`.
63+
pub fn contains(self, flags: GpuVaFlags) -> bool {
64+
(self & flags) == flags
65+
}
66+
}
67+
68+
impl core::ops::BitOr for GpuVaFlags {
69+
type Output = Self;
70+
fn bitor(self, rhs: Self) -> Self::Output {
71+
Self(self.0 | rhs.0)
72+
}
73+
}
74+
75+
impl core::ops::BitAnd for GpuVaFlags {
76+
type Output = Self;
77+
fn bitand(self, rhs: Self) -> Self::Output {
78+
Self(self.0 & rhs.0)
79+
}
80+
}
81+
82+
impl core::ops::Not for GpuVaFlags {
83+
type Output = Self;
84+
fn not(self) -> Self::Output {
85+
Self(!self.0)
86+
}
87+
}
88+
2989
/// Trait that must be implemented by DRM drivers to represent a DRM GpuVm (a GPU address space).
3090
pub trait DriverGpuVm: Sized {
3191
/// The parent `Driver` implementation for this `DriverGpuVm`.
@@ -91,6 +151,9 @@ impl<T: DriverGpuVm> OpMap<T> {
91151
pub fn offset(&self) -> u64 {
92152
self.0.gem.offset
93153
}
154+
pub fn flags(&self) -> GpuVaFlags {
155+
GpuVaFlags(self.0.flags)
156+
}
94157
pub fn object(&self) -> &<<T::Driver as drm::Driver>::Object as BaseDriverObject>::Object {
95158
let p = unsafe {
96159
<<<T::Driver as drm::Driver>::Object as BaseDriverObject>::Object as IntoGEMObject>::from_raw(self.0.gem.obj)
@@ -202,6 +265,9 @@ impl<T: DriverGpuVm> GpuVa<T> {
202265
pub fn offset(&self) -> u64 {
203266
self.gpuva.gem.offset
204267
}
268+
pub fn flags(&self) -> GpuVaFlags {
269+
GpuVaFlags(self.gpuva.flags)
270+
}
205271
}
206272

207273
/// A base GpuVm BO.
@@ -563,6 +629,7 @@ impl<T: DriverGpuVm> LockedGpuVm<'_, '_, T> {
563629
req_addr: u64,
564630
req_range: u64,
565631
req_offset: u64,
632+
flags: GpuVaFlags,
566633
) -> Result {
567634
let obj = self.obj.ok_or(EINVAL)?;
568635
let mut ctx = StepContext {
@@ -578,6 +645,7 @@ impl<T: DriverGpuVm> LockedGpuVm<'_, '_, T> {
578645
req_range,
579646
obj.as_raw() as *const _ as *mut _,
580647
req_offset,
648+
flags.as_raw(),
581649
)
582650
})
583651
}

0 commit comments

Comments
 (0)