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;
31 struct amdgpu_device *adev;
34 ctx = container_of(ref, struct amdgpu_ctx, refcount);
38 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
39 for (j = 0; j < AMDGPU_CTX_MAX_CS_PENDING; ++j)
40 fence_put(ctx->rings[i].fences[j]);
42 if (amdgpu_enable_scheduler) {
43 for (i = 0; i < adev->num_rings; i++)
44 amd_context_entity_fini(adev->rings[i]->scheduler,
45 &ctx->rings[i].c_entity);
51 static void amdgpu_ctx_init(struct amdgpu_device *adev,
52 struct amdgpu_fpriv *fpriv,
53 struct amdgpu_ctx *ctx,
57 memset(ctx, 0, sizeof(*ctx));
59 kref_init(&ctx->refcount);
60 spin_lock_init(&ctx->ring_lock);
61 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
62 ctx->rings[i].sequence = 1;
65 int amdgpu_ctx_alloc(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv,
68 struct amdgpu_ctx *ctx;
71 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
75 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
76 mutex_lock(&mgr->lock);
77 r = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
79 mutex_unlock(&mgr->lock);
84 amdgpu_ctx_init(adev, fpriv, ctx, *id);
85 mutex_unlock(&mgr->lock);
87 if (adev->kernel_ctx) {
88 DRM_ERROR("kernel cnotext has been created.\n");
92 *id = AMD_KERNEL_CONTEXT_ID;
93 amdgpu_ctx_init(adev, fpriv, ctx, *id);
95 adev->kernel_ctx = ctx;
98 if (amdgpu_enable_scheduler) {
99 /* create context entity for each ring */
100 for (i = 0; i < adev->num_rings; i++) {
101 struct amd_run_queue *rq;
103 rq = &adev->rings[i]->scheduler->sched_rq;
105 rq = &adev->rings[i]->scheduler->kernel_rq;
106 r = amd_context_entity_init(adev->rings[i]->scheduler,
107 &ctx->rings[i].c_entity,
113 if (i < adev->num_rings) {
114 for (j = 0; j < i; j++)
115 amd_context_entity_fini(adev->rings[j]->scheduler,
116 &ctx->rings[j].c_entity);
125 int amdgpu_ctx_free(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv, uint32_t id)
127 struct amdgpu_ctx *ctx;
130 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
131 mutex_lock(&mgr->lock);
132 ctx = idr_find(&mgr->ctx_handles, id);
134 idr_remove(&mgr->ctx_handles, id);
135 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
136 mutex_unlock(&mgr->lock);
139 mutex_unlock(&mgr->lock);
141 ctx = adev->kernel_ctx;
142 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
148 static int amdgpu_ctx_query(struct amdgpu_device *adev,
149 struct amdgpu_fpriv *fpriv, uint32_t id,
150 union drm_amdgpu_ctx_out *out)
152 struct amdgpu_ctx *ctx;
153 struct amdgpu_ctx_mgr *mgr;
154 unsigned reset_counter;
159 mgr = &fpriv->ctx_mgr;
160 mutex_lock(&mgr->lock);
161 ctx = idr_find(&mgr->ctx_handles, id);
163 mutex_unlock(&mgr->lock);
167 /* TODO: these two are always zero */
168 out->state.flags = 0x0;
169 out->state.hangs = 0x0;
171 /* determine if a GPU reset has occured since the last call */
172 reset_counter = atomic_read(&adev->gpu_reset_counter);
173 /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
174 if (ctx->reset_counter == reset_counter)
175 out->state.reset_status = AMDGPU_CTX_NO_RESET;
177 out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
178 ctx->reset_counter = reset_counter;
180 mutex_unlock(&mgr->lock);
184 void amdgpu_ctx_fini(struct amdgpu_fpriv *fpriv)
187 struct amdgpu_ctx *ctx;
189 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
190 idp = &mgr->ctx_handles;
192 idr_for_each_entry(idp,ctx,id) {
193 if (kref_put(&ctx->refcount, amdgpu_ctx_do_release) != 1)
194 DRM_ERROR("ctx %p is still alive\n", ctx);
197 idr_destroy(&mgr->ctx_handles);
198 mutex_destroy(&mgr->lock);
201 int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
202 struct drm_file *filp)
207 union drm_amdgpu_ctx *args = data;
208 struct amdgpu_device *adev = dev->dev_private;
209 struct amdgpu_fpriv *fpriv = filp->driver_priv;
212 id = args->in.ctx_id;
214 switch (args->in.op) {
215 case AMDGPU_CTX_OP_ALLOC_CTX:
216 r = amdgpu_ctx_alloc(adev, fpriv, &id);
217 args->out.alloc.ctx_id = id;
219 case AMDGPU_CTX_OP_FREE_CTX:
220 r = amdgpu_ctx_free(adev, fpriv, id);
222 case AMDGPU_CTX_OP_QUERY_STATE:
223 r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
232 struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
234 struct amdgpu_ctx *ctx;
235 struct amdgpu_ctx_mgr *mgr;
240 mgr = &fpriv->ctx_mgr;
242 mutex_lock(&mgr->lock);
243 ctx = idr_find(&mgr->ctx_handles, id);
245 kref_get(&ctx->refcount);
246 mutex_unlock(&mgr->lock);
250 int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
255 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
259 uint64_t amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct amdgpu_ring *ring,
262 struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
265 struct fence *other = NULL;
267 if (amdgpu_enable_scheduler)
268 seq = atomic64_read(&cring->c_entity.last_queued_v_seq);
270 seq = cring->sequence;
271 idx = seq % AMDGPU_CTX_MAX_CS_PENDING;
272 other = cring->fences[idx];
275 r = fence_wait_timeout(other, false, MAX_SCHEDULE_TIMEOUT);
277 DRM_ERROR("Error (%ld) waiting for fence!\n", r);
282 spin_lock(&ctx->ring_lock);
283 cring->fences[idx] = fence;
284 if (!amdgpu_enable_scheduler)
286 spin_unlock(&ctx->ring_lock);
293 struct fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
294 struct amdgpu_ring *ring, uint64_t seq)
296 struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
301 if (amdgpu_enable_scheduler) {
302 r = amd_sched_wait_emit(&cring->c_entity,
305 AMDGPU_WAIT_IDLE_TIMEOUT_IN_MS);
310 spin_lock(&ctx->ring_lock);
311 if (amdgpu_enable_scheduler)
312 queued_seq = atomic64_read(&cring->c_entity.last_queued_v_seq) + 1;
314 queued_seq = cring->sequence;
316 if (seq >= queued_seq) {
317 spin_unlock(&ctx->ring_lock);
318 return ERR_PTR(-EINVAL);
322 if (seq + AMDGPU_CTX_MAX_CS_PENDING < queued_seq) {
323 spin_unlock(&ctx->ring_lock);
327 fence = fence_get(cring->fences[seq % AMDGPU_CTX_MAX_CS_PENDING]);
328 spin_unlock(&ctx->ring_lock);