On Tue, Oct 27, 2026 at 12:41:01AM +0800, Ke Sun wrote:
> From: Alvin Sun <[email protected]>
>
> Implement BO_CREATE and BO_MMAP_OFFSET so userspace can allocate GPU
> buffers and obtain a DRM mmap offset for the generic mmap path.
> BO_CREATE page-aligns the requested size; BO_MMAP_OFFSET rejects
> NO_MMAP objects, which must never be CPU-mapped.
>
> Signed-off-by: Alvin Sun <[email protected]>
> ---
> drivers/gpu/drm/tyr/driver.rs | 2 ++
> drivers/gpu/drm/tyr/file.rs | 73
> +++++++++++++++++++++++++++++++++++++++++++
> drivers/gpu/drm/tyr/gem.rs | 18 +++++++++--
> 3 files changed, 90 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/gpu/drm/tyr/driver.rs b/drivers/gpu/drm/tyr/driver.rs
> index c0f17bc4ce2c9..f4f31b24180b4 100644
> --- a/drivers/gpu/drm/tyr/driver.rs
> +++ b/drivers/gpu/drm/tyr/driver.rs
> @@ -227,6 +227,8 @@ impl drm::Driver for TyrDrmDriver {
> (PANTHOR_VM_DESTROY, drm_panthor_vm_destroy, ioctl::RENDER_ALLOW,
> TyrDrmFileData::vm_destroy),
> (PANTHOR_VM_BIND, drm_panthor_vm_bind, ioctl::RENDER_ALLOW,
> TyrDrmFileData::vm_bind),
> (PANTHOR_VM_GET_STATE, drm_panthor_vm_get_state,
> ioctl::RENDER_ALLOW, TyrDrmFileData::vm_get_state),
> + (PANTHOR_BO_CREATE, drm_panthor_bo_create, ioctl::RENDER_ALLOW,
> TyrDrmFileData::bo_create),
> + (PANTHOR_BO_MMAP_OFFSET, drm_panthor_bo_mmap_offset,
> ioctl::RENDER_ALLOW, TyrDrmFileData::bo_mmap_offset),
> }
> }
>
> diff --git a/drivers/gpu/drm/tyr/file.rs b/drivers/gpu/drm/tyr/file.rs
> index 22a74b4abf3de..9ffb42f0d8851 100644
> --- a/drivers/gpu/drm/tyr/file.rs
> +++ b/drivers/gpu/drm/tyr/file.rs
> @@ -250,6 +250,79 @@ pub(crate) fn vm_get_state(
> Ok(0)
> })
> }
> +
> + pub(crate) fn bo_create(
> + ddev: &TyrDrmDevice<Registered>,
> + _reg_data: &TyrDrmRegistrationData<'_>,
> + bocreate: &mut uapi::drm_panthor_bo_create,
> + file: &TyrDrmFile,
> + ) -> Result<u32> {
> + if bocreate.size == 0
> + || bocreate.pad != 0
> + || bocreate.flags &
> !uapi::drm_panthor_bo_flags_DRM_PANTHOR_BO_NO_MMAP != 0
> + {
> + dev_dbg!(
> + ddev.as_ref(),
> + "Invalid BO_CREATE params: size={}, pad={}, flags={:#x}\n",
> + bocreate.size,
> + bocreate.pad,
> + bocreate.flags
> + );
> + return Err(EINVAL);
> + }
Panthor checks that both BO_WB_MMAP and BO_NO_MMAP are not
set at the same time, should we do the same here?
Also if only BO_WB_MMAP is set, shouldn't we return EOPNOTSUPP?
> +
> + if bocreate.exclusive_vm_id != 0 {
> + dev_dbg!(ddev.as_ref(), "BO_CREATE: exclusive_vm_id not
> supported\n");
> + return Err(EINVAL);
Return EOPNOTSUPP because it's a valid request we just don't support it.
> + }
> +
> + let size = usize::try_from(bocreate.size).map_err(|_| {
> + dev_dbg!(
> + ddev.as_ref(),
> + "BO_CREATE size {:#x} too large\n",
> + bocreate.size
> + );
> + EINVAL
> + })?;
> + let bo = crate::gem::new_object(ddev, size, bocreate.flags)?;
Use file.inner_with() to access the platform device with the bound
lifetime and then pass it to the new_object so that you can
avoid accessing it later with an unsafe block. Something like:
let bo = file.inner_with(|pfile| {
crate::gem::new_object(
ddev,
pfile.reg.pdev.as_ref(),
size,
bocreate.flags,
)
})?;
> + bocreate.handle = bo.create_handle(file)?;
> + bocreate.size = bo.size() as u64;
use u64::from_safe_cast(bo.size())
> +
> + Ok(0)
> + }
> +
> + pub(crate) fn bo_mmap_offset(
> + ddev: &TyrDrmDevice<Registered>,
> + _reg_data: &TyrDrmRegistrationData<'_>,
> + bommap: &mut uapi::drm_panthor_bo_mmap_offset,
> + file: &TyrDrmFile,
> + ) -> Result<u32> {
> + if bommap.pad != 0 {
> + dev_dbg!(
> + ddev.as_ref(),
> + "BO mmap offset pad not zero: {}\n",
> + bommap.pad
> + );
> + return Err(EINVAL);
> + }
> +
> + let bo = crate::gem::lookup_handle(file,
> bommap.handle).inspect_err(|_| {
> + dev_dbg!(ddev.as_ref(), "Invalid BO mmap handle: {}\n",
> bommap.handle);
> + })?;
> + if bo.flags() & uapi::drm_panthor_bo_flags_DRM_PANTHOR_BO_NO_MMAP !=
> 0 {
> + dev_dbg!(ddev.as_ref(), "BO mmap offset on NO_MMAP object\n");
> + return Err(EPERM);
> + }
> + bommap.offset = bo.create_mmap_offset().inspect_err(|_| {
> + dev_err!(
> + ddev.as_ref(),
> + "Failed to create mmap offset for handle {}\n",
> + bommap.handle
> + );
> + })?;
> +
> + Ok(0)
> + }
> }
>
> fn vm_bind_exec_op(vm: &Vm<'_>, file: &TyrDrmFile, op: &VmBindOp) -> Result {
> diff --git a/drivers/gpu/drm/tyr/gem.rs b/drivers/gpu/drm/tyr/gem.rs
> index 60f6363995a5a..14f3e182b98e2 100644
> --- a/drivers/gpu/drm/tyr/gem.rs
> +++ b/drivers/gpu/drm/tyr/gem.rs
> @@ -38,6 +38,13 @@ pub(crate) struct BoData {
> flags: u32,
> }
>
> +impl BoData {
> + /// Returns the flags the BO was created with.
> + pub(crate) fn flags(&self) -> u32 {
> + self.flags
> + }
> +}
> +
> /// Provides a way to pass arguments when creating BoData
> /// as required by the gem::DriverObject trait.
> pub(crate) struct BoCreateArgs {
> @@ -57,7 +64,6 @@ fn new(_dev: &TyrDrmDevice, _size: usize, args:
> BoCreateArgs) -> impl PinInit<Se
> pub(crate) type Bo = gem::shmem::Object<BoData>;
>
> /// Create a new GEM buffer object.
> -#[expect(dead_code)]
> pub(crate) fn new_object(ddev: &TyrDrmDevice, size: usize, flags: u32) ->
> Result<ARef<Bo>> {
> if size == 0 {
> return Err(EINVAL);
> @@ -65,7 +71,7 @@ pub(crate) fn new_object(ddev: &TyrDrmDevice, size: usize,
> flags: u32) -> Result
>
> let aligned_size =
> size.checked_next_multiple_of(PAGE_SIZE).ok_or(EINVAL)?;
>
> - Bo::new(
> + let bo = Bo::new(
> ddev,
> aligned_size,
> shmem::ObjectConfig {
> @@ -73,7 +79,13 @@ pub(crate) fn new_object(ddev: &TyrDrmDevice, size: usize,
> flags: u32) -> Result
> parent_resv_obj: None,
> },
> BoCreateArgs { flags },
> - )
> + )?;
> +
> + // SAFETY: `ddev` is bound for the duration of this call.
> + let dev = unsafe { ddev.as_ref().as_ref().as_bound() };
You can remove this unsafe block by accessing the bound device through
file.inner_with()
> + bo.sg_table(dev)?;
> +
> + Ok(bo)
> }
>
> /// Look up a GEM object by handle for a DRM file.
>
> --
> 2.43.0
>