blob: 15e341634536f202442205f93e663e08b2dae0df [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2015 Advanced Micro Devices, Inc.
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 *
22 * Authors: monk liu <monk.liu@amd.com>
23 */
24
25#include <drm/drmP.h>
26#include "amdgpu.h"
27
Chunming Zhoud033a6d2015-11-05 15:23:09 +080028int amdgpu_ctx_init(struct amdgpu_device *adev, enum amd_sched_priority pri,
Christian König47f38502015-08-04 17:51:05 +020029 struct amdgpu_ctx *ctx)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040030{
Christian König21c16bf2015-07-07 17:24:49 +020031 unsigned i, j;
Christian König47f38502015-08-04 17:51:05 +020032 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040033
Alex Deucherd38ceaf2015-04-20 16:55:21 -040034 memset(ctx, 0, sizeof(*ctx));
Chunming Zhou9cb7e5a2015-07-21 13:17:19 +080035 ctx->adev = adev;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040036 kref_init(&ctx->refcount);
Christian König21c16bf2015-07-07 17:24:49 +020037 spin_lock_init(&ctx->ring_lock);
38 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
39 ctx->rings[i].sequence = 1;
Chunming Zhou23ca0e42015-07-06 13:42:58 +080040
Chunming Zhou9cb7e5a2015-07-21 13:17:19 +080041 if (amdgpu_enable_scheduler) {
42 /* create context entity for each ring */
43 for (i = 0; i < adev->num_rings; i++) {
Christian König432a4ff2015-08-12 11:46:04 +020044 struct amd_sched_rq *rq;
Chunming Zhoud033a6d2015-11-05 15:23:09 +080045 if (pri >= AMD_SCHED_MAX_PRIORITY)
46 return -EINVAL;
47 rq = &adev->rings[i]->sched.sched_rq[pri];
Christian König4f839a22015-09-08 20:22:31 +020048 r = amd_sched_entity_init(&adev->rings[i]->sched,
Christian König91404fb2015-08-05 18:33:21 +020049 &ctx->rings[i].entity,
50 rq, amdgpu_sched_jobs);
Chunming Zhou9cb7e5a2015-07-21 13:17:19 +080051 if (r)
52 break;
53 }
54
55 if (i < adev->num_rings) {
56 for (j = 0; j < i; j++)
Christian König4f839a22015-09-08 20:22:31 +020057 amd_sched_entity_fini(&adev->rings[j]->sched,
Christian König91404fb2015-08-05 18:33:21 +020058 &ctx->rings[j].entity);
Christian König47f38502015-08-04 17:51:05 +020059 return r;
Chunming Zhou9cb7e5a2015-07-21 13:17:19 +080060 }
61 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -040062 return 0;
63}
64
Christian König47f38502015-08-04 17:51:05 +020065void amdgpu_ctx_fini(struct amdgpu_ctx *ctx)
66{
67 struct amdgpu_device *adev = ctx->adev;
68 unsigned i, j;
69
Dave Airliefe295b22015-11-03 11:07:11 -050070 if (!adev)
71 return;
72
Christian König47f38502015-08-04 17:51:05 +020073 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
74 for (j = 0; j < AMDGPU_CTX_MAX_CS_PENDING; ++j)
75 fence_put(ctx->rings[i].fences[j]);
76
77 if (amdgpu_enable_scheduler) {
78 for (i = 0; i < adev->num_rings; i++)
Christian König4f839a22015-09-08 20:22:31 +020079 amd_sched_entity_fini(&adev->rings[i]->sched,
Christian König91404fb2015-08-05 18:33:21 +020080 &ctx->rings[i].entity);
Christian König47f38502015-08-04 17:51:05 +020081 }
82}
83
84static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
85 struct amdgpu_fpriv *fpriv,
86 uint32_t *id)
87{
88 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
89 struct amdgpu_ctx *ctx;
90 int r;
91
92 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
93 if (!ctx)
94 return -ENOMEM;
95
96 mutex_lock(&mgr->lock);
97 r = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
98 if (r < 0) {
99 mutex_unlock(&mgr->lock);
100 kfree(ctx);
101 return r;
102 }
103 *id = (uint32_t)r;
Chunming Zhoud033a6d2015-11-05 15:23:09 +0800104 r = amdgpu_ctx_init(adev, AMD_SCHED_PRIORITY_NORMAL, ctx);
Chunming Zhouc648ed72015-12-10 15:50:02 +0800105 if (r) {
106 idr_remove(&mgr->ctx_handles, *id);
107 *id = 0;
108 kfree(ctx);
109 }
Christian König47f38502015-08-04 17:51:05 +0200110 mutex_unlock(&mgr->lock);
Christian König47f38502015-08-04 17:51:05 +0200111 return r;
112}
113
114static void amdgpu_ctx_do_release(struct kref *ref)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400115{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400116 struct amdgpu_ctx *ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400117
Christian König47f38502015-08-04 17:51:05 +0200118 ctx = container_of(ref, struct amdgpu_ctx, refcount);
119
120 amdgpu_ctx_fini(ctx);
121
122 kfree(ctx);
123}
124
125static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
126{
127 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
128 struct amdgpu_ctx *ctx;
129
130 mutex_lock(&mgr->lock);
131 ctx = idr_find(&mgr->ctx_handles, id);
132 if (ctx) {
133 idr_remove(&mgr->ctx_handles, id);
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800134 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
Christian König47f38502015-08-04 17:51:05 +0200135 mutex_unlock(&mgr->lock);
Marek Olšákf11358d2015-05-05 00:56:45 +0200136 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400137 }
Christian König47f38502015-08-04 17:51:05 +0200138 mutex_unlock(&mgr->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400139 return -EINVAL;
140}
141
Marek Olšákd94aed52015-05-05 21:13:49 +0200142static int amdgpu_ctx_query(struct amdgpu_device *adev,
143 struct amdgpu_fpriv *fpriv, uint32_t id,
144 union drm_amdgpu_ctx_out *out)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400145{
146 struct amdgpu_ctx *ctx;
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800147 struct amdgpu_ctx_mgr *mgr;
Marek Olšákd94aed52015-05-05 21:13:49 +0200148 unsigned reset_counter;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400149
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800150 if (!fpriv)
151 return -EINVAL;
152
153 mgr = &fpriv->ctx_mgr;
Marek Olšák0147ee02015-05-05 20:52:00 +0200154 mutex_lock(&mgr->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400155 ctx = idr_find(&mgr->ctx_handles, id);
Marek Olšákd94aed52015-05-05 21:13:49 +0200156 if (!ctx) {
Marek Olšák0147ee02015-05-05 20:52:00 +0200157 mutex_unlock(&mgr->lock);
Marek Olšákd94aed52015-05-05 21:13:49 +0200158 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400159 }
Marek Olšákd94aed52015-05-05 21:13:49 +0200160
161 /* TODO: these two are always zero */
Alex Deucher0b492a42015-08-16 22:48:26 -0400162 out->state.flags = 0x0;
163 out->state.hangs = 0x0;
Marek Olšákd94aed52015-05-05 21:13:49 +0200164
165 /* determine if a GPU reset has occured since the last call */
166 reset_counter = atomic_read(&adev->gpu_reset_counter);
167 /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
168 if (ctx->reset_counter == reset_counter)
169 out->state.reset_status = AMDGPU_CTX_NO_RESET;
170 else
171 out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
172 ctx->reset_counter = reset_counter;
173
Marek Olšák0147ee02015-05-05 20:52:00 +0200174 mutex_unlock(&mgr->lock);
Marek Olšákd94aed52015-05-05 21:13:49 +0200175 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400176}
177
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400178int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
Marek Olšákd94aed52015-05-05 21:13:49 +0200179 struct drm_file *filp)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400180{
181 int r;
182 uint32_t id;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400183
184 union drm_amdgpu_ctx *args = data;
185 struct amdgpu_device *adev = dev->dev_private;
186 struct amdgpu_fpriv *fpriv = filp->driver_priv;
187
188 r = 0;
189 id = args->in.ctx_id;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400190
191 switch (args->in.op) {
192 case AMDGPU_CTX_OP_ALLOC_CTX:
Alex Deucher0b492a42015-08-16 22:48:26 -0400193 r = amdgpu_ctx_alloc(adev, fpriv, &id);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400194 args->out.alloc.ctx_id = id;
195 break;
196 case AMDGPU_CTX_OP_FREE_CTX:
Christian König47f38502015-08-04 17:51:05 +0200197 r = amdgpu_ctx_free(fpriv, id);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400198 break;
199 case AMDGPU_CTX_OP_QUERY_STATE:
Marek Olšákd94aed52015-05-05 21:13:49 +0200200 r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400201 break;
202 default:
203 return -EINVAL;
204 }
205
206 return r;
207}
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800208
209struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
210{
211 struct amdgpu_ctx *ctx;
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800212 struct amdgpu_ctx_mgr *mgr;
213
214 if (!fpriv)
215 return NULL;
216
217 mgr = &fpriv->ctx_mgr;
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800218
219 mutex_lock(&mgr->lock);
220 ctx = idr_find(&mgr->ctx_handles, id);
221 if (ctx)
222 kref_get(&ctx->refcount);
223 mutex_unlock(&mgr->lock);
224 return ctx;
225}
226
227int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
228{
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800229 if (ctx == NULL)
230 return -EINVAL;
231
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800232 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800233 return 0;
234}
Christian König21c16bf2015-07-07 17:24:49 +0200235
236uint64_t amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct amdgpu_ring *ring,
Christian Königce882e62015-08-19 15:00:55 +0200237 struct fence *fence)
Christian König21c16bf2015-07-07 17:24:49 +0200238{
239 struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
Christian Königce882e62015-08-19 15:00:55 +0200240 uint64_t seq = cring->sequence;
Chunming Zhoub43a9a72015-07-21 15:13:53 +0800241 unsigned idx = 0;
242 struct fence *other = NULL;
Christian König21c16bf2015-07-07 17:24:49 +0200243
Chunming Zhoub43a9a72015-07-21 15:13:53 +0800244 idx = seq % AMDGPU_CTX_MAX_CS_PENDING;
245 other = cring->fences[idx];
Christian König21c16bf2015-07-07 17:24:49 +0200246 if (other) {
247 signed long r;
248 r = fence_wait_timeout(other, false, MAX_SCHEDULE_TIMEOUT);
249 if (r < 0)
250 DRM_ERROR("Error (%ld) waiting for fence!\n", r);
251 }
252
253 fence_get(fence);
254
255 spin_lock(&ctx->ring_lock);
256 cring->fences[idx] = fence;
Christian Königce882e62015-08-19 15:00:55 +0200257 cring->sequence++;
Christian König21c16bf2015-07-07 17:24:49 +0200258 spin_unlock(&ctx->ring_lock);
259
260 fence_put(other);
261
262 return seq;
263}
264
265struct fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
266 struct amdgpu_ring *ring, uint64_t seq)
267{
268 struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
269 struct fence *fence;
270
271 spin_lock(&ctx->ring_lock);
Chunming Zhoub43a9a72015-07-21 15:13:53 +0800272
Christian Königce882e62015-08-19 15:00:55 +0200273 if (seq >= cring->sequence) {
Christian König21c16bf2015-07-07 17:24:49 +0200274 spin_unlock(&ctx->ring_lock);
275 return ERR_PTR(-EINVAL);
276 }
277
Chunming Zhoub43a9a72015-07-21 15:13:53 +0800278
Christian Königce882e62015-08-19 15:00:55 +0200279 if (seq + AMDGPU_CTX_MAX_CS_PENDING < cring->sequence) {
Christian König21c16bf2015-07-07 17:24:49 +0200280 spin_unlock(&ctx->ring_lock);
281 return NULL;
282 }
283
284 fence = fence_get(cring->fences[seq % AMDGPU_CTX_MAX_CS_PENDING]);
285 spin_unlock(&ctx->ring_lock);
286
287 return fence;
288}
Christian Königefd4ccb2015-08-04 16:20:31 +0200289
290void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr)
291{
292 mutex_init(&mgr->lock);
293 idr_init(&mgr->ctx_handles);
294}
295
296void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
297{
298 struct amdgpu_ctx *ctx;
299 struct idr *idp;
300 uint32_t id;
301
302 idp = &mgr->ctx_handles;
303
304 idr_for_each_entry(idp, ctx, id) {
305 if (kref_put(&ctx->refcount, amdgpu_ctx_do_release) != 1)
306 DRM_ERROR("ctx %p is still alive\n", ctx);
307 }
308
309 idr_destroy(&mgr->ctx_handles);
310 mutex_destroy(&mgr->lock);
311}