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 int amdgpu_ctx_alloc(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv,
54 struct amdgpu_ctx *ctx;
55 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
58 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
62 mutex_lock(&mgr->lock);
63 r = idr_alloc(&mgr->ctx_handles, ctx, 0, 0, GFP_KERNEL);
65 mutex_unlock(&mgr->lock);
71 memset(ctx, 0, sizeof(*ctx));
73 kref_init(&ctx->refcount);
74 spin_lock_init(&ctx->ring_lock);
75 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
76 ctx->rings[i].sequence = 1;
77 mutex_unlock(&mgr->lock);
78 if (amdgpu_enable_scheduler) {
79 /* create context entity for each ring */
80 for (i = 0; i < adev->num_rings; i++) {
81 struct amd_run_queue *rq;
83 rq = &adev->rings[i]->scheduler->sched_rq;
85 rq = &adev->rings[i]->scheduler->kernel_rq;
86 r = amd_context_entity_init(adev->rings[i]->scheduler,
87 &ctx->rings[i].c_entity,
93 if (i < adev->num_rings) {
94 for (j = 0; j < i; j++)
95 amd_context_entity_fini(adev->rings[j]->scheduler,
96 &ctx->rings[j].c_entity);
105 int amdgpu_ctx_free(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv, uint32_t id)
107 struct amdgpu_ctx *ctx;
108 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
110 mutex_lock(&mgr->lock);
111 ctx = idr_find(&mgr->ctx_handles, id);
113 idr_remove(&mgr->ctx_handles, id);
114 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
115 mutex_unlock(&mgr->lock);
118 mutex_unlock(&mgr->lock);
122 static int amdgpu_ctx_query(struct amdgpu_device *adev,
123 struct amdgpu_fpriv *fpriv, uint32_t id,
124 union drm_amdgpu_ctx_out *out)
126 struct amdgpu_ctx *ctx;
127 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
128 unsigned reset_counter;
130 mutex_lock(&mgr->lock);
131 ctx = idr_find(&mgr->ctx_handles, id);
133 mutex_unlock(&mgr->lock);
137 /* TODO: these two are always zero */
138 out->state.flags = 0x0;
139 out->state.hangs = 0x0;
141 /* determine if a GPU reset has occured since the last call */
142 reset_counter = atomic_read(&adev->gpu_reset_counter);
143 /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
144 if (ctx->reset_counter == reset_counter)
145 out->state.reset_status = AMDGPU_CTX_NO_RESET;
147 out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
148 ctx->reset_counter = reset_counter;
150 mutex_unlock(&mgr->lock);
154 void amdgpu_ctx_fini(struct amdgpu_fpriv *fpriv)
157 struct amdgpu_ctx *ctx;
159 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
160 idp = &mgr->ctx_handles;
162 idr_for_each_entry(idp,ctx,id) {
163 if (kref_put(&ctx->refcount, amdgpu_ctx_do_release) != 1)
164 DRM_ERROR("ctx %p is still alive\n", ctx);
167 idr_destroy(&mgr->ctx_handles);
168 mutex_destroy(&mgr->lock);
171 int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
172 struct drm_file *filp)
177 union drm_amdgpu_ctx *args = data;
178 struct amdgpu_device *adev = dev->dev_private;
179 struct amdgpu_fpriv *fpriv = filp->driver_priv;
182 id = args->in.ctx_id;
184 switch (args->in.op) {
185 case AMDGPU_CTX_OP_ALLOC_CTX:
186 r = amdgpu_ctx_alloc(adev, fpriv, &id);
187 args->out.alloc.ctx_id = id;
189 case AMDGPU_CTX_OP_FREE_CTX:
190 r = amdgpu_ctx_free(adev, fpriv, id);
192 case AMDGPU_CTX_OP_QUERY_STATE:
193 r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
202 struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
204 struct amdgpu_ctx *ctx;
205 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
207 mutex_lock(&mgr->lock);
208 ctx = idr_find(&mgr->ctx_handles, id);
210 kref_get(&ctx->refcount);
211 mutex_unlock(&mgr->lock);
215 int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
220 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
224 uint64_t amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct amdgpu_ring *ring,
227 struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
228 uint64_t seq = cring->sequence;
229 unsigned idx = seq % AMDGPU_CTX_MAX_CS_PENDING;
230 struct fence *other = cring->fences[idx];
234 r = fence_wait_timeout(other, false, MAX_SCHEDULE_TIMEOUT);
236 DRM_ERROR("Error (%ld) waiting for fence!\n", r);
241 spin_lock(&ctx->ring_lock);
242 cring->fences[idx] = fence;
244 spin_unlock(&ctx->ring_lock);
251 struct fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
252 struct amdgpu_ring *ring, uint64_t seq)
254 struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
257 spin_lock(&ctx->ring_lock);
258 if (seq >= cring->sequence) {
259 spin_unlock(&ctx->ring_lock);
260 return ERR_PTR(-EINVAL);
263 if (seq + AMDGPU_CTX_MAX_CS_PENDING < cring->sequence) {
264 spin_unlock(&ctx->ring_lock);
268 fence = fence_get(cring->fences[seq % AMDGPU_CTX_MAX_CS_PENDING]);
269 spin_unlock(&ctx->ring_lock);