blob: fec65f01c0315ec726a464e49ca9a1c47910ca58 [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
Christian König47f38502015-08-04 17:51:05 +020028int amdgpu_ctx_init(struct amdgpu_device *adev, bool kernel,
29 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;
Christian König47f38502015-08-04 17:51:05 +020045 if (kernel)
Christian König4f839a22015-09-08 20:22:31 +020046 rq = &adev->rings[i]->sched.kernel_rq;
Christian König47f38502015-08-04 17:51:05 +020047 else
Christian König4f839a22015-09-08 20:22:31 +020048 rq = &adev->rings[i]->sched.sched_rq;
49 r = amd_sched_entity_init(&adev->rings[i]->sched,
Christian König91404fb2015-08-05 18:33:21 +020050 &ctx->rings[i].entity,
51 rq, amdgpu_sched_jobs);
Chunming Zhou9cb7e5a2015-07-21 13:17:19 +080052 if (r)
53 break;
54 }
55
56 if (i < adev->num_rings) {
57 for (j = 0; j < i; j++)
Christian König4f839a22015-09-08 20:22:31 +020058 amd_sched_entity_fini(&adev->rings[j]->sched,
Christian König91404fb2015-08-05 18:33:21 +020059 &ctx->rings[j].entity);
Chunming Zhou9cb7e5a2015-07-21 13:17:19 +080060 kfree(ctx);
Christian König47f38502015-08-04 17:51:05 +020061 return r;
Chunming Zhou9cb7e5a2015-07-21 13:17:19 +080062 }
63 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -040064 return 0;
65}
66
Christian König47f38502015-08-04 17:51:05 +020067void amdgpu_ctx_fini(struct amdgpu_ctx *ctx)
68{
69 struct amdgpu_device *adev = ctx->adev;
70 unsigned i, j;
71
Dave Airliefe295b22015-11-03 11:07:11 -050072 if (!adev)
73 return;
74
Christian König47f38502015-08-04 17:51:05 +020075 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
76 for (j = 0; j < AMDGPU_CTX_MAX_CS_PENDING; ++j)
77 fence_put(ctx->rings[i].fences[j]);
78
79 if (amdgpu_enable_scheduler) {
80 for (i = 0; i < adev->num_rings; i++)
Christian König4f839a22015-09-08 20:22:31 +020081 amd_sched_entity_fini(&adev->rings[i]->sched,
Christian König91404fb2015-08-05 18:33:21 +020082 &ctx->rings[i].entity);
Christian König47f38502015-08-04 17:51:05 +020083 }
84}
85
86static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
87 struct amdgpu_fpriv *fpriv,
88 uint32_t *id)
89{
90 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
91 struct amdgpu_ctx *ctx;
92 int r;
93
94 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
95 if (!ctx)
96 return -ENOMEM;
97
98 mutex_lock(&mgr->lock);
99 r = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
100 if (r < 0) {
101 mutex_unlock(&mgr->lock);
102 kfree(ctx);
103 return r;
104 }
105 *id = (uint32_t)r;
106 r = amdgpu_ctx_init(adev, false, ctx);
107 mutex_unlock(&mgr->lock);
108
109 return r;
110}
111
112static void amdgpu_ctx_do_release(struct kref *ref)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400113{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400114 struct amdgpu_ctx *ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400115
Christian König47f38502015-08-04 17:51:05 +0200116 ctx = container_of(ref, struct amdgpu_ctx, refcount);
117
118 amdgpu_ctx_fini(ctx);
119
120 kfree(ctx);
121}
122
123static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
124{
125 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
126 struct amdgpu_ctx *ctx;
127
128 mutex_lock(&mgr->lock);
129 ctx = idr_find(&mgr->ctx_handles, id);
130 if (ctx) {
131 idr_remove(&mgr->ctx_handles, id);
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800132 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
Christian König47f38502015-08-04 17:51:05 +0200133 mutex_unlock(&mgr->lock);
Marek Olšákf11358d2015-05-05 00:56:45 +0200134 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400135 }
Christian König47f38502015-08-04 17:51:05 +0200136 mutex_unlock(&mgr->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400137 return -EINVAL;
138}
139
Marek Olšákd94aed52015-05-05 21:13:49 +0200140static int amdgpu_ctx_query(struct amdgpu_device *adev,
141 struct amdgpu_fpriv *fpriv, uint32_t id,
142 union drm_amdgpu_ctx_out *out)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400143{
144 struct amdgpu_ctx *ctx;
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800145 struct amdgpu_ctx_mgr *mgr;
Marek Olšákd94aed52015-05-05 21:13:49 +0200146 unsigned reset_counter;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400147
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800148 if (!fpriv)
149 return -EINVAL;
150
151 mgr = &fpriv->ctx_mgr;
Marek Olšák0147ee02015-05-05 20:52:00 +0200152 mutex_lock(&mgr->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400153 ctx = idr_find(&mgr->ctx_handles, id);
Marek Olšákd94aed52015-05-05 21:13:49 +0200154 if (!ctx) {
Marek Olšák0147ee02015-05-05 20:52:00 +0200155 mutex_unlock(&mgr->lock);
Marek Olšákd94aed52015-05-05 21:13:49 +0200156 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400157 }
Marek Olšákd94aed52015-05-05 21:13:49 +0200158
159 /* TODO: these two are always zero */
Alex Deucher0b492a42015-08-16 22:48:26 -0400160 out->state.flags = 0x0;
161 out->state.hangs = 0x0;
Marek Olšákd94aed52015-05-05 21:13:49 +0200162
163 /* determine if a GPU reset has occured since the last call */
164 reset_counter = atomic_read(&adev->gpu_reset_counter);
165 /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
166 if (ctx->reset_counter == reset_counter)
167 out->state.reset_status = AMDGPU_CTX_NO_RESET;
168 else
169 out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
170 ctx->reset_counter = reset_counter;
171
Marek Olšák0147ee02015-05-05 20:52:00 +0200172 mutex_unlock(&mgr->lock);
Marek Olšákd94aed52015-05-05 21:13:49 +0200173 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400174}
175
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400176int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
Marek Olšákd94aed52015-05-05 21:13:49 +0200177 struct drm_file *filp)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400178{
179 int r;
180 uint32_t id;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400181
182 union drm_amdgpu_ctx *args = data;
183 struct amdgpu_device *adev = dev->dev_private;
184 struct amdgpu_fpriv *fpriv = filp->driver_priv;
185
186 r = 0;
187 id = args->in.ctx_id;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400188
189 switch (args->in.op) {
190 case AMDGPU_CTX_OP_ALLOC_CTX:
Alex Deucher0b492a42015-08-16 22:48:26 -0400191 r = amdgpu_ctx_alloc(adev, fpriv, &id);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400192 args->out.alloc.ctx_id = id;
193 break;
194 case AMDGPU_CTX_OP_FREE_CTX:
Christian König47f38502015-08-04 17:51:05 +0200195 r = amdgpu_ctx_free(fpriv, id);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400196 break;
197 case AMDGPU_CTX_OP_QUERY_STATE:
Marek Olšákd94aed52015-05-05 21:13:49 +0200198 r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400199 break;
200 default:
201 return -EINVAL;
202 }
203
204 return r;
205}
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800206
207struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
208{
209 struct amdgpu_ctx *ctx;
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800210 struct amdgpu_ctx_mgr *mgr;
211
212 if (!fpriv)
213 return NULL;
214
215 mgr = &fpriv->ctx_mgr;
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800216
217 mutex_lock(&mgr->lock);
218 ctx = idr_find(&mgr->ctx_handles, id);
219 if (ctx)
220 kref_get(&ctx->refcount);
221 mutex_unlock(&mgr->lock);
222 return ctx;
223}
224
225int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
226{
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800227 if (ctx == NULL)
228 return -EINVAL;
229
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800230 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800231 return 0;
232}
Christian König21c16bf2015-07-07 17:24:49 +0200233
234uint64_t amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct amdgpu_ring *ring,
Christian Königce882e62015-08-19 15:00:55 +0200235 struct fence *fence)
Christian König21c16bf2015-07-07 17:24:49 +0200236{
237 struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
Christian Königce882e62015-08-19 15:00:55 +0200238 uint64_t seq = cring->sequence;
Chunming Zhoub43a9a72015-07-21 15:13:53 +0800239 unsigned idx = 0;
240 struct fence *other = NULL;
Christian König21c16bf2015-07-07 17:24:49 +0200241
Chunming Zhoub43a9a72015-07-21 15:13:53 +0800242 idx = seq % AMDGPU_CTX_MAX_CS_PENDING;
243 other = cring->fences[idx];
Christian König21c16bf2015-07-07 17:24:49 +0200244 if (other) {
245 signed long r;
246 r = fence_wait_timeout(other, false, MAX_SCHEDULE_TIMEOUT);
247 if (r < 0)
248 DRM_ERROR("Error (%ld) waiting for fence!\n", r);
249 }
250
251 fence_get(fence);
252
253 spin_lock(&ctx->ring_lock);
254 cring->fences[idx] = fence;
Christian Königce882e62015-08-19 15:00:55 +0200255 cring->sequence++;
Christian König21c16bf2015-07-07 17:24:49 +0200256 spin_unlock(&ctx->ring_lock);
257
258 fence_put(other);
259
260 return seq;
261}
262
263struct fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
264 struct amdgpu_ring *ring, uint64_t seq)
265{
266 struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
267 struct fence *fence;
268
269 spin_lock(&ctx->ring_lock);
Chunming Zhoub43a9a72015-07-21 15:13:53 +0800270
Christian Königce882e62015-08-19 15:00:55 +0200271 if (seq >= cring->sequence) {
Christian König21c16bf2015-07-07 17:24:49 +0200272 spin_unlock(&ctx->ring_lock);
273 return ERR_PTR(-EINVAL);
274 }
275
Chunming Zhoub43a9a72015-07-21 15:13:53 +0800276
Christian Königce882e62015-08-19 15:00:55 +0200277 if (seq + AMDGPU_CTX_MAX_CS_PENDING < cring->sequence) {
Christian König21c16bf2015-07-07 17:24:49 +0200278 spin_unlock(&ctx->ring_lock);
279 return NULL;
280 }
281
282 fence = fence_get(cring->fences[seq % AMDGPU_CTX_MAX_CS_PENDING]);
283 spin_unlock(&ctx->ring_lock);
284
285 return fence;
286}
Christian Königefd4ccb2015-08-04 16:20:31 +0200287
288void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr)
289{
290 mutex_init(&mgr->lock);
291 idr_init(&mgr->ctx_handles);
292}
293
294void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
295{
296 struct amdgpu_ctx *ctx;
297 struct idr *idp;
298 uint32_t id;
299
300 idp = &mgr->ctx_handles;
301
302 idr_for_each_entry(idp, ctx, id) {
303 if (kref_put(&ctx->refcount, amdgpu_ctx_do_release) != 1)
304 DRM_ERROR("ctx %p is still alive\n", ctx);
305 }
306
307 idr_destroy(&mgr->ctx_handles);
308 mutex_destroy(&mgr->lock);
309}