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.
26 #include <drm/drm_auth.h>
29 static int amdgpu_ctx_priority_permit(struct drm_file *filp,
30 enum amd_sched_priority priority)
32 /* NORMAL and below are accessible by everyone */
33 if (priority <= AMD_SCHED_PRIORITY_NORMAL)
36 if (capable(CAP_SYS_NICE))
39 if (drm_is_current_master(filp))
45 static int amdgpu_ctx_init(struct amdgpu_device *adev,
46 enum amd_sched_priority priority,
47 struct drm_file *filp,
48 struct amdgpu_ctx *ctx)
53 if (priority < 0 || priority >= AMD_SCHED_PRIORITY_MAX)
56 r = amdgpu_ctx_priority_permit(filp, priority);
60 memset(ctx, 0, sizeof(*ctx));
62 kref_init(&ctx->refcount);
63 spin_lock_init(&ctx->ring_lock);
64 ctx->fences = kcalloc(amdgpu_sched_jobs * AMDGPU_MAX_RINGS,
65 sizeof(struct dma_fence*), GFP_KERNEL);
69 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
70 ctx->rings[i].sequence = 1;
71 ctx->rings[i].fences = &ctx->fences[amdgpu_sched_jobs * i];
74 ctx->reset_counter = atomic_read(&adev->gpu_reset_counter);
76 /* create context entity for each ring */
77 for (i = 0; i < adev->num_rings; i++) {
78 struct amdgpu_ring *ring = adev->rings[i];
79 struct amd_sched_rq *rq;
81 rq = &ring->sched.sched_rq[priority];
83 if (ring == &adev->gfx.kiq.ring)
86 r = amd_sched_entity_init(&ring->sched, &ctx->rings[i].entity,
87 rq, amdgpu_sched_jobs);
92 r = amdgpu_queue_mgr_init(adev, &ctx->queue_mgr);
99 for (j = 0; j < i; j++)
100 amd_sched_entity_fini(&adev->rings[j]->sched,
101 &ctx->rings[j].entity);
107 static void amdgpu_ctx_fini(struct amdgpu_ctx *ctx)
109 struct amdgpu_device *adev = ctx->adev;
115 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
116 for (j = 0; j < amdgpu_sched_jobs; ++j)
117 dma_fence_put(ctx->rings[i].fences[j]);
121 for (i = 0; i < adev->num_rings; i++)
122 amd_sched_entity_fini(&adev->rings[i]->sched,
123 &ctx->rings[i].entity);
125 amdgpu_queue_mgr_fini(adev, &ctx->queue_mgr);
128 static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
129 struct amdgpu_fpriv *fpriv,
130 struct drm_file *filp,
131 enum amd_sched_priority priority,
134 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
135 struct amdgpu_ctx *ctx;
138 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
142 mutex_lock(&mgr->lock);
143 r = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
145 mutex_unlock(&mgr->lock);
151 r = amdgpu_ctx_init(adev, priority, filp, ctx);
153 idr_remove(&mgr->ctx_handles, *id);
157 mutex_unlock(&mgr->lock);
161 static void amdgpu_ctx_do_release(struct kref *ref)
163 struct amdgpu_ctx *ctx;
165 ctx = container_of(ref, struct amdgpu_ctx, refcount);
167 amdgpu_ctx_fini(ctx);
172 static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
174 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
175 struct amdgpu_ctx *ctx;
177 mutex_lock(&mgr->lock);
178 ctx = idr_remove(&mgr->ctx_handles, id);
180 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
181 mutex_unlock(&mgr->lock);
182 return ctx ? 0 : -EINVAL;
185 static int amdgpu_ctx_query(struct amdgpu_device *adev,
186 struct amdgpu_fpriv *fpriv, uint32_t id,
187 union drm_amdgpu_ctx_out *out)
189 struct amdgpu_ctx *ctx;
190 struct amdgpu_ctx_mgr *mgr;
191 unsigned reset_counter;
196 mgr = &fpriv->ctx_mgr;
197 mutex_lock(&mgr->lock);
198 ctx = idr_find(&mgr->ctx_handles, id);
200 mutex_unlock(&mgr->lock);
204 /* TODO: these two are always zero */
205 out->state.flags = 0x0;
206 out->state.hangs = 0x0;
208 /* determine if a GPU reset has occured since the last call */
209 reset_counter = atomic_read(&adev->gpu_reset_counter);
210 /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
211 if (ctx->reset_counter == reset_counter)
212 out->state.reset_status = AMDGPU_CTX_NO_RESET;
214 out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
215 ctx->reset_counter = reset_counter;
217 mutex_unlock(&mgr->lock);
221 static enum amd_sched_priority amdgpu_to_sched_priority(int amdgpu_priority)
223 switch (amdgpu_priority) {
224 case AMDGPU_CTX_PRIORITY_HIGH_HW:
225 return AMD_SCHED_PRIORITY_HIGH_HW;
226 case AMDGPU_CTX_PRIORITY_HIGH_SW:
227 return AMD_SCHED_PRIORITY_HIGH_SW;
228 case AMDGPU_CTX_PRIORITY_NORMAL:
229 return AMD_SCHED_PRIORITY_NORMAL;
230 case AMDGPU_CTX_PRIORITY_LOW_SW:
231 case AMDGPU_CTX_PRIORITY_LOW_HW:
232 return AMD_SCHED_PRIORITY_LOW;
233 case AMDGPU_CTX_PRIORITY_UNSET:
234 return AMD_SCHED_PRIORITY_UNSET;
236 WARN(1, "Invalid context priority %d\n", amdgpu_priority);
237 return AMD_SCHED_PRIORITY_INVALID;
241 int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
242 struct drm_file *filp)
246 enum amd_sched_priority priority;
248 union drm_amdgpu_ctx *args = data;
249 struct amdgpu_device *adev = dev->dev_private;
250 struct amdgpu_fpriv *fpriv = filp->driver_priv;
253 id = args->in.ctx_id;
254 priority = amdgpu_to_sched_priority(args->in.priority);
256 /* For backwards compatibility reasons, we need to accept
257 * ioctls with garbage in the priority field */
258 if (priority == AMD_SCHED_PRIORITY_INVALID)
259 priority = AMD_SCHED_PRIORITY_NORMAL;
261 switch (args->in.op) {
262 case AMDGPU_CTX_OP_ALLOC_CTX:
263 r = amdgpu_ctx_alloc(adev, fpriv, filp, priority, &id);
264 args->out.alloc.ctx_id = id;
266 case AMDGPU_CTX_OP_FREE_CTX:
267 r = amdgpu_ctx_free(fpriv, id);
269 case AMDGPU_CTX_OP_QUERY_STATE:
270 r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
279 struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
281 struct amdgpu_ctx *ctx;
282 struct amdgpu_ctx_mgr *mgr;
287 mgr = &fpriv->ctx_mgr;
289 mutex_lock(&mgr->lock);
290 ctx = idr_find(&mgr->ctx_handles, id);
292 kref_get(&ctx->refcount);
293 mutex_unlock(&mgr->lock);
297 int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
302 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
306 int amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct amdgpu_ring *ring,
307 struct dma_fence *fence, uint64_t* handler)
309 struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
310 uint64_t seq = cring->sequence;
312 struct dma_fence *other = NULL;
314 idx = seq & (amdgpu_sched_jobs - 1);
315 other = cring->fences[idx];
318 r = dma_fence_wait_timeout(other, true, MAX_SCHEDULE_TIMEOUT);
323 dma_fence_get(fence);
325 spin_lock(&ctx->ring_lock);
326 cring->fences[idx] = fence;
328 spin_unlock(&ctx->ring_lock);
330 dma_fence_put(other);
337 struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
338 struct amdgpu_ring *ring, uint64_t seq)
340 struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
341 struct dma_fence *fence;
343 spin_lock(&ctx->ring_lock);
346 seq = ctx->rings[ring->idx].sequence - 1;
348 if (seq >= cring->sequence) {
349 spin_unlock(&ctx->ring_lock);
350 return ERR_PTR(-EINVAL);
354 if (seq + amdgpu_sched_jobs < cring->sequence) {
355 spin_unlock(&ctx->ring_lock);
359 fence = dma_fence_get(cring->fences[seq & (amdgpu_sched_jobs - 1)]);
360 spin_unlock(&ctx->ring_lock);
365 void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr)
367 mutex_init(&mgr->lock);
368 idr_init(&mgr->ctx_handles);
371 void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
373 struct amdgpu_ctx *ctx;
377 idp = &mgr->ctx_handles;
379 idr_for_each_entry(idp, ctx, id) {
380 if (kref_put(&ctx->refcount, amdgpu_ctx_do_release) != 1)
381 DRM_ERROR("ctx %p is still alive\n", ctx);
384 idr_destroy(&mgr->ctx_handles);
385 mutex_destroy(&mgr->lock);