Identical in principle to DRM_IOCTL_GEM_CHANGE_HANDLE, we need a method
for renaming the driver specific context handles.

We add a new operation (AMDGPU_CTX_OP_CHANGE_HANDLE) to the
DRM_IOCTL_AMDGPU_CTX ioctl.

Source handle is given in the existing input handle field, while the
destination handle needs to be passed in the existing flags field.

Signed-off-by: Tvrtko Ursulin <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 30 +++++++++++++++++++++++++
 include/uapi/drm/amdgpu_drm.h           |  1 +
 2 files changed, 31 insertions(+)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
index 15bf247c38f2..01a74eb6e721 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
@@ -541,6 +541,33 @@ static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, 
uint32_t id)
        return ctx ? 0 : -EINVAL;
 }
 
+static int amdgpu_ctx_change_handle(struct amdgpu_fpriv *fpriv,
+                                   uint32_t old, uint32_t id)
+{
+       struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
+       struct amdgpu_ctx *ctx;
+       int r = -EINVAL;
+
+       if (!old || !id || old == id)
+               return -EINVAL;
+
+       mutex_lock(&mgr->lock);
+
+       ctx = idr_find(&mgr->ctx_handles, old);
+       if (!ctx)
+               goto out;
+
+       r = idr_alloc(&mgr->ctx_handles, ctx, id, id + 1, GFP_KERNEL);
+       if (r < 0)
+               goto out;
+
+       idr_remove(&mgr->ctx_handles, old);
+
+out:
+       mutex_unlock(&mgr->lock);
+       return r;
+}
+
 static int amdgpu_ctx_query(struct amdgpu_device *adev,
                            struct amdgpu_fpriv *fpriv, uint32_t id,
                            union drm_amdgpu_ctx_out *out)
@@ -727,6 +754,9 @@ int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
                        return -EINVAL;
                r = amdgpu_ctx_stable_pstate(adev, fpriv, id, true, 
&stable_pstate);
                break;
+       case AMDGPU_CTX_OP_CHANGE_HANDLE:
+               r = amdgpu_ctx_change_handle(fpriv, id, args->in.flags);
+               break;
        default:
                return -EINVAL;
        }
diff --git a/include/uapi/drm/amdgpu_drm.h b/include/uapi/drm/amdgpu_drm.h
index 15da4d9c44fd..d2a7b4e9daed 100644
--- a/include/uapi/drm/amdgpu_drm.h
+++ b/include/uapi/drm/amdgpu_drm.h
@@ -251,6 +251,7 @@ union drm_amdgpu_bo_list {
 #define AMDGPU_CTX_OP_QUERY_STATE2     4
 #define AMDGPU_CTX_OP_GET_STABLE_PSTATE        5
 #define AMDGPU_CTX_OP_SET_STABLE_PSTATE        6
+#define AMDGPU_CTX_OP_CHANGE_HANDLE    7
 
 /* GPU reset status */
 #define AMDGPU_CTX_NO_RESET            0
-- 
2.54.0

Reply via email to