blob: c1f2308b41159cb6c5d042c10f1528486c1e772d [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);
Chunming Zhou9cb7e5a2015-07-21 13:17:19 +080059 kfree(ctx);
Christian König47f38502015-08-04 17:51:05 +020060 return r;
Chunming Zhou9cb7e5a2015-07-21 13:17:19 +080061 }
62 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -040063 return 0;
64}
65
Christian König47f38502015-08-04 17:51:05 +020066void amdgpu_ctx_fini(struct amdgpu_ctx *ctx)
67{
68 struct amdgpu_device *adev = ctx->adev;
69 unsigned i, j;
70
Dave Airliefe295b22015-11-03 11:07:11 -050071 if (!adev)
72 return;
73
Christian König47f38502015-08-04 17:51:05 +020074 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
75 for (j = 0; j < AMDGPU_CTX_MAX_CS_PENDING; ++j)
76 fence_put(ctx->rings[i].fences[j]);
77
78 if (amdgpu_enable_scheduler) {
79 for (i = 0; i < adev->num_rings; i++)
Christian König4f839a22015-09-08 20:22:31 +020080 amd_sched_entity_fini(&adev->rings[i]->sched,
Christian König91404fb2015-08-05 18:33:21 +020081 &ctx->rings[i].entity);
Christian König47f38502015-08-04 17:51:05 +020082 }
83}
84
85static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
86 struct amdgpu_fpriv *fpriv,
87 uint32_t *id)
88{
89 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
90 struct amdgpu_ctx *ctx;
91 int r;
92
93 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
94 if (!ctx)
95 return -ENOMEM;
96
97 mutex_lock(&mgr->lock);
98 r = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
99 if (r < 0) {
100 mutex_unlock(&mgr->lock);
101 kfree(ctx);
102 return r;
103 }
104 *id = (uint32_t)r;
Chunming Zhoud033a6d2015-11-05 15:23:09 +0800105 r = amdgpu_ctx_init(adev, AMD_SCHED_PRIORITY_NORMAL, ctx);
Christian König47f38502015-08-04 17:51:05 +0200106 mutex_unlock(&mgr->lock);
107
108 return r;
109}
110
111static void amdgpu_ctx_do_release(struct kref *ref)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400112{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400113 struct amdgpu_ctx *ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400114
Christian König47f38502015-08-04 17:51:05 +0200115 ctx = container_of(ref, struct amdgpu_ctx, refcount);
116
117 amdgpu_ctx_fini(ctx);
118
119 kfree(ctx);
120}
121
122static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
123{
124 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
125 struct amdgpu_ctx *ctx;
126
127 mutex_lock(&mgr->lock);
128 ctx = idr_find(&mgr->ctx_handles, id);
129 if (ctx) {
130 idr_remove(&mgr->ctx_handles, id);
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800131 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
Christian König47f38502015-08-04 17:51:05 +0200132 mutex_unlock(&mgr->lock);
Marek Olšákf11358d2015-05-05 00:56:45 +0200133 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400134 }
Christian König47f38502015-08-04 17:51:05 +0200135 mutex_unlock(&mgr->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400136 return -EINVAL;
137}
138
Marek Olšákd94aed52015-05-05 21:13:49 +0200139static int amdgpu_ctx_query(struct amdgpu_device *adev,
140 struct amdgpu_fpriv *fpriv, uint32_t id,
141 union drm_amdgpu_ctx_out *out)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400142{
143 struct amdgpu_ctx *ctx;
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800144 struct amdgpu_ctx_mgr *mgr;
Marek Olšákd94aed52015-05-05 21:13:49 +0200145 unsigned reset_counter;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400146
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800147 if (!fpriv)
148 return -EINVAL;
149
150 mgr = &fpriv->ctx_mgr;
Marek Olšák0147ee02015-05-05 20:52:00 +0200151 mutex_lock(&mgr->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400152 ctx = idr_find(&mgr->ctx_handles, id);
Marek Olšákd94aed52015-05-05 21:13:49 +0200153 if (!ctx) {
Marek Olšák0147ee02015-05-05 20:52:00 +0200154 mutex_unlock(&mgr->lock);
Marek Olšákd94aed52015-05-05 21:13:49 +0200155 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400156 }
Marek Olšákd94aed52015-05-05 21:13:49 +0200157
158 /* TODO: these two are always zero */
Alex Deucher0b492a42015-08-16 22:48:26 -0400159 out->state.flags = 0x0;
160 out->state.hangs = 0x0;
Marek Olšákd94aed52015-05-05 21:13:49 +0200161
162 /* determine if a GPU reset has occured since the last call */
163 reset_counter = atomic_read(&adev->gpu_reset_counter);
164 /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
165 if (ctx->reset_counter == reset_counter)
166 out->state.reset_status = AMDGPU_CTX_NO_RESET;
167 else
168 out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
169 ctx->reset_counter = reset_counter;
170
Marek Olšák0147ee02015-05-05 20:52:00 +0200171 mutex_unlock(&mgr->lock);
Marek Olšákd94aed52015-05-05 21:13:49 +0200172 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400173}
174
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400175int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
Marek Olšákd94aed52015-05-05 21:13:49 +0200176 struct drm_file *filp)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400177{
178 int r;
179 uint32_t id;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400180
181 union drm_amdgpu_ctx *args = data;
182 struct amdgpu_device *adev = dev->dev_private;
183 struct amdgpu_fpriv *fpriv = filp->driver_priv;
184
185 r = 0;
186 id = args->in.ctx_id;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400187
188 switch (args->in.op) {
189 case AMDGPU_CTX_OP_ALLOC_CTX:
Alex Deucher0b492a42015-08-16 22:48:26 -0400190 r = amdgpu_ctx_alloc(adev, fpriv, &id);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400191 args->out.alloc.ctx_id = id;
192 break;
193 case AMDGPU_CTX_OP_FREE_CTX:
Christian König47f38502015-08-04 17:51:05 +0200194 r = amdgpu_ctx_free(fpriv, id);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400195 break;
196 case AMDGPU_CTX_OP_QUERY_STATE:
Marek Olšákd94aed52015-05-05 21:13:49 +0200197 r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400198 break;
199 default:
200 return -EINVAL;
201 }
202
203 return r;
204}
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800205
206struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
207{
208 struct amdgpu_ctx *ctx;
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800209 struct amdgpu_ctx_mgr *mgr;
210
211 if (!fpriv)
212 return NULL;
213
214 mgr = &fpriv->ctx_mgr;
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800215
216 mutex_lock(&mgr->lock);
217 ctx = idr_find(&mgr->ctx_handles, id);
218 if (ctx)
219 kref_get(&ctx->refcount);
220 mutex_unlock(&mgr->lock);
221 return ctx;
222}
223
224int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
225{
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800226 if (ctx == NULL)
227 return -EINVAL;
228
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800229 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800230 return 0;
231}
Christian König21c16bf2015-07-07 17:24:49 +0200232
233uint64_t amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct amdgpu_ring *ring,
Christian Königce882e62015-08-19 15:00:55 +0200234 struct fence *fence)
Christian König21c16bf2015-07-07 17:24:49 +0200235{
236 struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
Christian Königce882e62015-08-19 15:00:55 +0200237 uint64_t seq = cring->sequence;
Chunming Zhoub43a9a72015-07-21 15:13:53 +0800238 unsigned idx = 0;
239 struct fence *other = NULL;
Christian König21c16bf2015-07-07 17:24:49 +0200240
Chunming Zhoub43a9a72015-07-21 15:13:53 +0800241 idx = seq % AMDGPU_CTX_MAX_CS_PENDING;
242 other = cring->fences[idx];
Christian König21c16bf2015-07-07 17:24:49 +0200243 if (other) {
244 signed long r;
245 r = fence_wait_timeout(other, false, MAX_SCHEDULE_TIMEOUT);
246 if (r < 0)
247 DRM_ERROR("Error (%ld) waiting for fence!\n", r);
248 }
249
250 fence_get(fence);
251
252 spin_lock(&ctx->ring_lock);
253 cring->fences[idx] = fence;
Christian Königce882e62015-08-19 15:00:55 +0200254 cring->sequence++;
Christian König21c16bf2015-07-07 17:24:49 +0200255 spin_unlock(&ctx->ring_lock);
256
257 fence_put(other);
258
259 return seq;
260}
261
262struct fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
263 struct amdgpu_ring *ring, uint64_t seq)
264{
265 struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
266 struct fence *fence;
267
268 spin_lock(&ctx->ring_lock);
Chunming Zhoub43a9a72015-07-21 15:13:53 +0800269
Christian Königce882e62015-08-19 15:00:55 +0200270 if (seq >= cring->sequence) {
Christian König21c16bf2015-07-07 17:24:49 +0200271 spin_unlock(&ctx->ring_lock);
272 return ERR_PTR(-EINVAL);
273 }
274
Chunming Zhoub43a9a72015-07-21 15:13:53 +0800275
Christian Königce882e62015-08-19 15:00:55 +0200276 if (seq + AMDGPU_CTX_MAX_CS_PENDING < cring->sequence) {
Christian König21c16bf2015-07-07 17:24:49 +0200277 spin_unlock(&ctx->ring_lock);
278 return NULL;
279 }
280
281 fence = fence_get(cring->fences[seq % AMDGPU_CTX_MAX_CS_PENDING]);
282 spin_unlock(&ctx->ring_lock);
283
284 return fence;
285}
Christian Königefd4ccb2015-08-04 16:20:31 +0200286
287void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr)
288{
289 mutex_init(&mgr->lock);
290 idr_init(&mgr->ctx_handles);
291}
292
293void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
294{
295 struct amdgpu_ctx *ctx;
296 struct idr *idp;
297 uint32_t id;
298
299 idp = &mgr->ctx_handles;
300
301 idr_for_each_entry(idp, ctx, id) {
302 if (kref_put(&ctx->refcount, amdgpu_ctx_do_release) != 1)
303 DRM_ERROR("ctx %p is still alive\n", ctx);
304 }
305
306 idr_destroy(&mgr->ctx_handles);
307 mutex_destroy(&mgr->lock);
308}