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.
28 static void amdgpu_ctx_do_release(struct kref *ref)
30 struct amdgpu_ctx *ctx;
33 ctx = container_of(ref, struct amdgpu_ctx, refcount);
35 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
36 for (j = 0; j < AMDGPU_CTX_MAX_CS_PENDING; ++j)
37 fence_put(ctx->rings[i].fences[j]);
41 int amdgpu_ctx_alloc(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv,
44 struct amdgpu_ctx *ctx;
45 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
48 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
52 mutex_lock(&mgr->lock);
53 r = idr_alloc(&mgr->ctx_handles, ctx, 0, 0, GFP_KERNEL);
55 mutex_unlock(&mgr->lock);
61 memset(ctx, 0, sizeof(*ctx));
62 kref_init(&ctx->refcount);
63 spin_lock_init(&ctx->ring_lock);
64 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
65 ctx->rings[i].sequence = 1;
66 mutex_unlock(&mgr->lock);
71 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;
76 mutex_lock(&mgr->lock);
77 ctx = idr_find(&mgr->ctx_handles, id);
79 idr_remove(&mgr->ctx_handles, id);
80 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
81 mutex_unlock(&mgr->lock);
84 mutex_unlock(&mgr->lock);
88 static int amdgpu_ctx_query(struct amdgpu_device *adev,
89 struct amdgpu_fpriv *fpriv, uint32_t id,
90 union drm_amdgpu_ctx_out *out)
92 struct amdgpu_ctx *ctx;
93 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
94 unsigned reset_counter;
96 mutex_lock(&mgr->lock);
97 ctx = idr_find(&mgr->ctx_handles, id);
99 mutex_unlock(&mgr->lock);
103 /* TODO: these two are always zero */
104 out->state.flags = 0x0;
105 out->state.hangs = 0x0;
107 /* determine if a GPU reset has occured since the last call */
108 reset_counter = atomic_read(&adev->gpu_reset_counter);
109 /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
110 if (ctx->reset_counter == reset_counter)
111 out->state.reset_status = AMDGPU_CTX_NO_RESET;
113 out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
114 ctx->reset_counter = reset_counter;
116 mutex_unlock(&mgr->lock);
120 void amdgpu_ctx_fini(struct amdgpu_fpriv *fpriv)
123 struct amdgpu_ctx *ctx;
125 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
126 idp = &mgr->ctx_handles;
128 idr_for_each_entry(idp,ctx,id) {
129 if (kref_put(&ctx->refcount, amdgpu_ctx_do_release) != 1)
130 DRM_ERROR("ctx %p is still alive\n", ctx);
133 mutex_destroy(&mgr->lock);
136 int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
137 struct drm_file *filp)
142 union drm_amdgpu_ctx *args = data;
143 struct amdgpu_device *adev = dev->dev_private;
144 struct amdgpu_fpriv *fpriv = filp->driver_priv;
147 id = args->in.ctx_id;
149 switch (args->in.op) {
150 case AMDGPU_CTX_OP_ALLOC_CTX:
151 r = amdgpu_ctx_alloc(adev, fpriv, &id);
152 args->out.alloc.ctx_id = id;
154 case AMDGPU_CTX_OP_FREE_CTX:
155 r = amdgpu_ctx_free(adev, fpriv, id);
157 case AMDGPU_CTX_OP_QUERY_STATE:
158 r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
167 struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
169 struct amdgpu_ctx *ctx;
170 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
172 mutex_lock(&mgr->lock);
173 ctx = idr_find(&mgr->ctx_handles, id);
175 kref_get(&ctx->refcount);
176 mutex_unlock(&mgr->lock);
180 int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
185 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
189 uint64_t amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct amdgpu_ring *ring,
192 struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
193 uint64_t seq = cring->sequence;
194 unsigned idx = seq % AMDGPU_CTX_MAX_CS_PENDING;
195 struct fence *other = cring->fences[idx];
199 r = fence_wait_timeout(other, false, MAX_SCHEDULE_TIMEOUT);
201 DRM_ERROR("Error (%ld) waiting for fence!\n", r);
206 spin_lock(&ctx->ring_lock);
207 cring->fences[idx] = fence;
209 spin_unlock(&ctx->ring_lock);
216 struct fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
217 struct amdgpu_ring *ring, uint64_t seq)
219 struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
222 spin_lock(&ctx->ring_lock);
223 if (seq >= cring->sequence) {
224 spin_unlock(&ctx->ring_lock);
225 return ERR_PTR(-EINVAL);
228 if (seq < cring->sequence - AMDGPU_CTX_MAX_CS_PENDING) {
229 spin_unlock(&ctx->ring_lock);
233 fence = fence_get(cring->fences[seq % AMDGPU_CTX_MAX_CS_PENDING]);
234 spin_unlock(&ctx->ring_lock);