On 29.08.25 15:55, Srinivasan Shanmugam wrote:
> Enable userspace to obtain a handle to the kernel-owned MMIO_REMAP
> singleton when AMDGPU_GEM_DOMAIN_MMIO_REMAP is requested via
> amdgpu_gem_create_ioctl().
> 
> Validate the fixed 4K constraint: if PAGE_SIZE > AMDGPU_GPU_PAGE_SIZE
> return -EINVAL; when provided, size and alignment must equal
> AMDGPU_GPU_PAGE_SIZE.
> 
> If the singleton BO is not available, return -ENODEV.
> 
> v2:
> - Drop READ_ONCE() on adev->mmio_remap.bo (use a plain pointer load).
>   The pointer is set `bo = adev->mmio_remap.bo;` ie., The pointer is
>   written once during init and not changed while IOCTLs run. There’s no
>   concurrent writer in this execution path, so a normal read is safe.
>   (Alex)
> 
> v3:
> - Drop early -EINVAL for AMDGPU_GEM_DOMAIN_MMIO_REMAP; let the
>   MMIO_REMAP fast-path handle it and return the singleton handle.
> 
> Cc: Christian König <christian.koe...@amd.com>
> Suggested-by: Alex Deucher <alexander.deuc...@amd.com>
> Signed-off-by: Srinivasan Shanmugam <srinivasan.shanmu...@amd.com>
> ---
>  drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 61 ++++++++++++++++++++++++-
>  1 file changed, 59 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c 
> b/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
> index d3c369742124..7f331c34e581 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
> @@ -424,6 +424,38 @@ const struct drm_gem_object_funcs 
> amdgpu_gem_object_funcs = {
>       .vm_ops = &amdgpu_gem_vm_ops,
>  };
>  
> +/**
> + * amdgpu_gem_get_mmio_remap_handle - Create a GEM handle for the MMIO_REMAP 
> BO
> + * @file_priv: DRM file corresponding to the calling process
> + * @adev: amdgpu device
> + * @handle: returned userspace GEM handle (out)
> + *
> + * Creates a GEM handle to the kernel-owned singleton MMIO_REMAP buffer 
> object
> + * (adev->rmmio_remap.bo). The BO is expected to be allocated during TTM init
> + * when the hardware exposes a remap base and PAGE_SIZE <= 4K.
> + *
> + * drm_gem_handle_create() acquires the handle reference, which will be 
> dropped
> + * by GEM_CLOSE in userspace.
> + *
> + * * Return:
> + * * 0 on success
> + * * -ENODEV if the MMIO_REMAP BO is not available
> + * * A negative errno from drm_gem_handle_create() on failure
> + *
> + */
> +static int amdgpu_gem_get_mmio_remap_handle(struct drm_file *file_priv,
> +                                         struct amdgpu_device *adev,
> +                                         u32 *handle)
> +{
> +     struct amdgpu_bo *bo = adev->rmmio_remap.bo;
> +
> +     if (!bo)
> +             return -ENODEV;

Mhm, ENODEV is probably not the best error code here. EOPNOTSUPP or something 
like this sounds better to me.


> +
> +     /* drm_gem_handle_create() gets the ref; GEM_CLOSE drops it */
> +     return drm_gem_handle_create(file_priv, &bo->tbo.base, handle);
> +}
> +
>  /*
>   * GEM ioctls.
>   */
> @@ -465,8 +497,33 @@ int amdgpu_gem_create_ioctl(struct drm_device *dev, void 
> *data,
>       /* always clear VRAM */
>       flags |= AMDGPU_GEM_CREATE_VRAM_CLEARED;
>  
> -     if (args->in.domains & AMDGPU_GEM_DOMAIN_MMIO_REMAP)
> -             return -EINVAL;
> +     /*
> +      * === MMIO remap (HDP flush) fast-path ===
> +      * If userspace asks for the MMIO_REMAP domain, don't allocate a new BO.
> +      * Return a handle to the singleton BO created at ttm init.
> +      */
> +     if (args->in.domains & AMDGPU_GEM_DOMAIN_MMIO_REMAP) {
> +             /*

> +              * The MMIO remap page is fixed 4K on the GPU side. Do not
> +              * allow use if the system PAGE_SIZE is larger than the GPU
> +              * page size.
> +              */
> +             if (PAGE_SIZE > AMDGPU_GPU_PAGE_SIZE)
> +                     return -EINVAL;

That check doesn't belong here. When the BO can't be created it is simply not 
available.


> +
> +             /* Enforce fixed size/alignment when provided by userspace. */
> +             if (size && size != AMDGPU_GPU_PAGE_SIZE)
> +                     return -EINVAL;
> +             if (args->in.alignment && args->in.alignment != 
> AMDGPU_GPU_PAGE_SIZE)
> +                     return -EINVAL;

Either moves those checks into amdgpu_gem_get_mmio_remap_handle() or completely 
inline amdgpu_gem_get_mmio_remap_handle() here.

Regards,
Christian.

> +
> +             r = amdgpu_gem_get_mmio_remap_handle(filp, adev, &handle);
> +             if (r)
> +                     return r;
> +
> +             args->out.handle = handle;
> +             return 0;
> +     }
>  
>       /* create a gem object to contain this object in */
>       if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS |

Reply via email to