2 * Copyright 2015 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 * Authors: monk liu <monk.liu@amd.com>
28 static void amdgpu_ctx_do_release(struct kref *ref)
30 struct amdgpu_ctx *ctx;
31 struct amdgpu_ctx_mgr *mgr;
33 ctx = container_of(ref, struct amdgpu_ctx, refcount);
34 mgr = &ctx->fpriv->ctx_mgr;
36 mutex_lock(&mgr->hlock);
37 idr_remove(&mgr->ctx_handles, ctx->id);
38 mutex_unlock(&mgr->hlock);
42 int amdgpu_ctx_alloc(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv, uint32_t *id, uint32_t flags)
45 struct amdgpu_ctx *ctx;
46 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
48 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
52 mutex_lock(&mgr->hlock);
53 r = idr_alloc(&mgr->ctx_handles, ctx, 0, 0, GFP_KERNEL);
55 mutex_unlock(&mgr->hlock);
59 mutex_unlock(&mgr->hlock);
62 memset(ctx, 0, sizeof(*ctx));
65 kref_init(&ctx->refcount);
70 int amdgpu_ctx_free(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv, uint32_t id)
73 struct amdgpu_ctx *ctx;
74 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
77 ctx = idr_find(&mgr->ctx_handles, id);
80 /* if no task is pending on this context, free it */
81 r = kref_put(&ctx->refcount, amdgpu_ctx_do_release);
83 return 0;//context is removed successfully
85 /* context is still in using */
86 kref_get(&ctx->refcount);
93 int amdgpu_ctx_query(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv, uint32_t id, struct amdgpu_ctx_state *state)
95 struct amdgpu_ctx *ctx;
96 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
99 ctx = idr_find(&mgr->ctx_handles, id);
102 /* state should alter with CS activity */
109 void amdgpu_ctx_fini(struct amdgpu_fpriv *fpriv)
112 struct amdgpu_ctx *ctx;
114 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
115 idp = &mgr->ctx_handles;
117 idr_for_each_entry(idp,ctx,id) {
118 if (kref_put(&ctx->refcount, amdgpu_ctx_do_release) != 1)
119 DRM_ERROR("ctx (id=%ul) is still alive\n",ctx->id);
122 mutex_destroy(&mgr->hlock);
125 int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
126 struct drm_file *filp)
131 struct amdgpu_ctx_state state;
133 union drm_amdgpu_ctx *args = data;
134 struct amdgpu_device *adev = dev->dev_private;
135 struct amdgpu_fpriv *fpriv = filp->driver_priv;
138 id = args->in.ctx_id;
139 flags = args->in.flags;
141 switch (args->in.op) {
142 case AMDGPU_CTX_OP_ALLOC_CTX:
143 r = amdgpu_ctx_alloc(adev, fpriv, &id, flags);
144 args->out.alloc.ctx_id = id;
146 case AMDGPU_CTX_OP_FREE_CTX:
147 r = amdgpu_ctx_free(adev, fpriv, id);
149 case AMDGPU_CTX_OP_QUERY_STATE:
150 r = amdgpu_ctx_query(adev, fpriv, id, &state);
152 args->out.state.flags = state.flags;
153 args->out.state.hangs = state.hangs;