blob: 987b7f2564634cce2ecec7ad2c5195e1d59dfb07 [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>
Andres Rodriguezc2636dc2016-12-22 17:06:50 -050026#include <drm/drm_auth.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040027#include "amdgpu.h"
Andres Rodriguez52c6a622017-06-26 16:17:13 -040028#include "amdgpu_sched.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040029
Christian König1b1f2fe2018-08-01 16:00:52 +020030#define to_amdgpu_ctx_entity(e) \
31 container_of((e), struct amdgpu_ctx_entity, entity)
32
33const unsigned int amdgpu_ctx_num_entities[AMDGPU_HW_IP_NUM] = {
34 [AMDGPU_HW_IP_GFX] = 1,
35 [AMDGPU_HW_IP_COMPUTE] = 4,
36 [AMDGPU_HW_IP_DMA] = 2,
37 [AMDGPU_HW_IP_UVD] = 1,
38 [AMDGPU_HW_IP_VCE] = 1,
39 [AMDGPU_HW_IP_UVD_ENC] = 1,
40 [AMDGPU_HW_IP_VCN_DEC] = 1,
41 [AMDGPU_HW_IP_VCN_ENC] = 1,
42};
43
44static int amdgput_ctx_total_num_entities(void)
45{
46 unsigned i, num_entities = 0;
47
48 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i)
49 num_entities += amdgpu_ctx_num_entities[i];
50
51 return num_entities;
52}
Christian König0d346a12018-07-19 14:22:25 +020053
Andres Rodriguezc2636dc2016-12-22 17:06:50 -050054static int amdgpu_ctx_priority_permit(struct drm_file *filp,
Lucas Stach1b1f42d2017-12-06 17:49:39 +010055 enum drm_sched_priority priority)
Andres Rodriguezc2636dc2016-12-22 17:06:50 -050056{
57 /* NORMAL and below are accessible by everyone */
Lucas Stach1b1f42d2017-12-06 17:49:39 +010058 if (priority <= DRM_SCHED_PRIORITY_NORMAL)
Andres Rodriguezc2636dc2016-12-22 17:06:50 -050059 return 0;
60
61 if (capable(CAP_SYS_NICE))
62 return 0;
63
64 if (drm_is_current_master(filp))
65 return 0;
66
67 return -EACCES;
68}
69
70static int amdgpu_ctx_init(struct amdgpu_device *adev,
Lucas Stach1b1f42d2017-12-06 17:49:39 +010071 enum drm_sched_priority priority,
Andres Rodriguezc2636dc2016-12-22 17:06:50 -050072 struct drm_file *filp,
73 struct amdgpu_ctx *ctx)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040074{
Christian König1b1f2fe2018-08-01 16:00:52 +020075 unsigned num_entities = amdgput_ctx_total_num_entities();
76 unsigned i, j;
Christian König47f38502015-08-04 17:51:05 +020077 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040078
Lucas Stach1b1f42d2017-12-06 17:49:39 +010079 if (priority < 0 || priority >= DRM_SCHED_PRIORITY_MAX)
Andres Rodriguezc2636dc2016-12-22 17:06:50 -050080 return -EINVAL;
81
82 r = amdgpu_ctx_priority_permit(filp, priority);
83 if (r)
84 return r;
85
Alex Deucherd38ceaf2015-04-20 16:55:21 -040086 memset(ctx, 0, sizeof(*ctx));
Chunming Zhou9cb7e5a2015-07-21 13:17:19 +080087 ctx->adev = adev;
Christian König1b1f2fe2018-08-01 16:00:52 +020088
89 ctx->fences = kcalloc(amdgpu_sched_jobs * num_entities,
Chris Wilsonf54d1862016-10-25 13:00:45 +010090 sizeof(struct dma_fence*), GFP_KERNEL);
Chunming Zhou37cd0ca2015-12-10 15:45:11 +080091 if (!ctx->fences)
92 return -ENOMEM;
Chunming Zhou23ca0e42015-07-06 13:42:58 +080093
Christian König1b1f2fe2018-08-01 16:00:52 +020094 ctx->entities[0] = kcalloc(num_entities,
95 sizeof(struct amdgpu_ctx_entity),
96 GFP_KERNEL);
97 if (!ctx->entities[0]) {
98 r = -ENOMEM;
99 goto error_free_fences;
Chunming Zhou37cd0ca2015-12-10 15:45:11 +0800100 }
Nicolai Hähnlece199ad2016-10-04 09:43:30 +0200101
Christian König1b1f2fe2018-08-01 16:00:52 +0200102 for (i = 0; i < num_entities; ++i) {
103 struct amdgpu_ctx_entity *entity = &ctx->entities[0][i];
104
105 entity->sequence = 1;
106 entity->fences = &ctx->fences[amdgpu_sched_jobs * i];
107 }
108 for (i = 1; i < AMDGPU_HW_IP_NUM; ++i)
109 ctx->entities[i] = ctx->entities[i - 1] +
110 amdgpu_ctx_num_entities[i - 1];
111
112 kref_init(&ctx->refcount);
113 spin_lock_init(&ctx->ring_lock);
114 mutex_init(&ctx->lock);
115
Nicolai Hähnlece199ad2016-10-04 09:43:30 +0200116 ctx->reset_counter = atomic_read(&adev->gpu_reset_counter);
Monk Liu668ca1b2017-10-17 14:39:23 +0800117 ctx->reset_counter_query = ctx->reset_counter;
Christian Könige55f2b62017-10-09 15:18:43 +0200118 ctx->vram_lost_counter = atomic_read(&adev->vram_lost_counter);
Andres Rodriguezc23be4a2017-06-06 20:20:38 -0400119 ctx->init_priority = priority;
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100120 ctx->override_priority = DRM_SCHED_PRIORITY_UNSET;
Nicolai Hähnlece199ad2016-10-04 09:43:30 +0200121
Christian König1b1f2fe2018-08-01 16:00:52 +0200122 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
123 struct amdgpu_ring *rings[AMDGPU_MAX_RINGS];
124 struct drm_sched_rq *rqs[AMDGPU_MAX_RINGS];
125 unsigned num_rings;
Christian König20874172016-02-11 09:56:44 +0100126
Christian König1b1f2fe2018-08-01 16:00:52 +0200127 switch (i) {
128 case AMDGPU_HW_IP_GFX:
129 rings[0] = &adev->gfx.gfx_ring[0];
130 num_rings = 1;
131 break;
132 case AMDGPU_HW_IP_COMPUTE:
133 for (j = 0; j < adev->gfx.num_compute_rings; ++j)
134 rings[j] = &adev->gfx.compute_ring[j];
135 num_rings = adev->gfx.num_compute_rings;
136 break;
137 case AMDGPU_HW_IP_DMA:
138 for (j = 0; j < adev->sdma.num_instances; ++j)
139 rings[j] = &adev->sdma.instance[j].ring;
140 num_rings = adev->sdma.num_instances;
141 break;
142 case AMDGPU_HW_IP_UVD:
143 rings[0] = &adev->uvd.inst[0].ring;
144 num_rings = 1;
145 break;
146 case AMDGPU_HW_IP_VCE:
147 rings[0] = &adev->vce.ring[0];
148 num_rings = 1;
149 break;
150 case AMDGPU_HW_IP_UVD_ENC:
151 rings[0] = &adev->uvd.inst[0].ring_enc[0];
152 num_rings = 1;
153 break;
154 case AMDGPU_HW_IP_VCN_DEC:
155 rings[0] = &adev->vcn.ring_dec;
156 num_rings = 1;
157 break;
158 case AMDGPU_HW_IP_VCN_ENC:
159 rings[0] = &adev->vcn.ring_enc[0];
160 num_rings = 1;
161 break;
162 case AMDGPU_HW_IP_VCN_JPEG:
163 rings[0] = &adev->vcn.ring_jpeg;
164 num_rings = 1;
165 break;
Christian König845e6fd2018-07-13 09:12:44 +0200166 }
Christian König1b1f2fe2018-08-01 16:00:52 +0200167
168 for (j = 0; j < num_rings; ++j)
169 rqs[j] = &rings[j]->sched.sched_rq[priority];
170
171 for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j)
172 r = drm_sched_entity_init(&ctx->entities[i][j].entity,
173 rqs, num_rings, &ctx->guilty);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800174 if (r)
Christian König1b1f2fe2018-08-01 16:00:52 +0200175 goto error_cleanup_entities;
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800176 }
177
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400178 return 0;
Huang Rui8ed81472016-10-26 17:07:03 +0800179
Christian König1b1f2fe2018-08-01 16:00:52 +0200180error_cleanup_entities:
181 for (i = 0; i < num_entities; ++i)
182 drm_sched_entity_destroy(&ctx->entities[0][i].entity);
183 kfree(ctx->entities[0]);
184
185error_free_fences:
Huang Rui8ed81472016-10-26 17:07:03 +0800186 kfree(ctx->fences);
187 ctx->fences = NULL;
188 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400189}
190
Emily Deng8ee3a522018-04-16 10:07:02 +0800191static void amdgpu_ctx_fini(struct kref *ref)
Christian König47f38502015-08-04 17:51:05 +0200192{
Emily Deng8ee3a522018-04-16 10:07:02 +0800193 struct amdgpu_ctx *ctx = container_of(ref, struct amdgpu_ctx, refcount);
Christian König1b1f2fe2018-08-01 16:00:52 +0200194 unsigned num_entities = amdgput_ctx_total_num_entities();
Christian König47f38502015-08-04 17:51:05 +0200195 struct amdgpu_device *adev = ctx->adev;
196 unsigned i, j;
197
Dave Airliefe295b22015-11-03 11:07:11 -0500198 if (!adev)
199 return;
200
Christian König1b1f2fe2018-08-01 16:00:52 +0200201 for (i = 0; i < num_entities; ++i)
Chunming Zhou37cd0ca2015-12-10 15:45:11 +0800202 for (j = 0; j < amdgpu_sched_jobs; ++j)
Christian König1b1f2fe2018-08-01 16:00:52 +0200203 dma_fence_put(ctx->entities[0][i].fences[j]);
Chunming Zhou37cd0ca2015-12-10 15:45:11 +0800204 kfree(ctx->fences);
Christian König1b1f2fe2018-08-01 16:00:52 +0200205 kfree(ctx->entities[0]);
Christian König47f38502015-08-04 17:51:05 +0200206
Andrey Grodzovsky0ae94442017-10-10 16:50:17 -0400207 mutex_destroy(&ctx->lock);
Emily Deng8ee3a522018-04-16 10:07:02 +0800208
209 kfree(ctx);
Christian König47f38502015-08-04 17:51:05 +0200210}
211
Christian König0d346a12018-07-19 14:22:25 +0200212int amdgpu_ctx_get_entity(struct amdgpu_ctx *ctx, u32 hw_ip, u32 instance,
213 u32 ring, struct drm_sched_entity **entity)
Christian König869a53d2018-07-16 15:19:20 +0200214{
Christian König1b1f2fe2018-08-01 16:00:52 +0200215 if (hw_ip >= AMDGPU_HW_IP_NUM) {
216 DRM_ERROR("unknown HW IP type: %d\n", hw_ip);
217 return -EINVAL;
218 }
Christian König869a53d2018-07-16 15:19:20 +0200219
220 /* Right now all IPs have only one instance - multiple rings. */
221 if (instance != 0) {
222 DRM_DEBUG("invalid ip instance: %d\n", instance);
223 return -EINVAL;
224 }
225
Christian König1b1f2fe2018-08-01 16:00:52 +0200226 if (ring >= amdgpu_ctx_num_entities[hw_ip]) {
227 DRM_DEBUG("invalid ring: %d %d\n", hw_ip, ring);
Christian König869a53d2018-07-16 15:19:20 +0200228 return -EINVAL;
229 }
230
Christian König1b1f2fe2018-08-01 16:00:52 +0200231 *entity = &ctx->entities[hw_ip][ring].entity;
Christian König869a53d2018-07-16 15:19:20 +0200232 return 0;
233}
234
Christian König47f38502015-08-04 17:51:05 +0200235static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
236 struct amdgpu_fpriv *fpriv,
Andres Rodriguezc2636dc2016-12-22 17:06:50 -0500237 struct drm_file *filp,
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100238 enum drm_sched_priority priority,
Christian König47f38502015-08-04 17:51:05 +0200239 uint32_t *id)
240{
241 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
242 struct amdgpu_ctx *ctx;
243 int r;
244
245 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
246 if (!ctx)
247 return -ENOMEM;
248
249 mutex_lock(&mgr->lock);
250 r = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
251 if (r < 0) {
252 mutex_unlock(&mgr->lock);
253 kfree(ctx);
254 return r;
255 }
Andres Rodriguezc2636dc2016-12-22 17:06:50 -0500256
Christian König47f38502015-08-04 17:51:05 +0200257 *id = (uint32_t)r;
Andres Rodriguezc2636dc2016-12-22 17:06:50 -0500258 r = amdgpu_ctx_init(adev, priority, filp, ctx);
Chunming Zhouc648ed72015-12-10 15:50:02 +0800259 if (r) {
260 idr_remove(&mgr->ctx_handles, *id);
261 *id = 0;
262 kfree(ctx);
263 }
Christian König47f38502015-08-04 17:51:05 +0200264 mutex_unlock(&mgr->lock);
Christian König47f38502015-08-04 17:51:05 +0200265 return r;
266}
267
268static void amdgpu_ctx_do_release(struct kref *ref)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400269{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400270 struct amdgpu_ctx *ctx;
Christian König1b1f2fe2018-08-01 16:00:52 +0200271 unsigned num_entities;
Emily Deng8ee3a522018-04-16 10:07:02 +0800272 u32 i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400273
Christian König47f38502015-08-04 17:51:05 +0200274 ctx = container_of(ref, struct amdgpu_ctx, refcount);
275
Christian König1b1f2fe2018-08-01 16:00:52 +0200276 num_entities = 0;
277 for (i = 0; i < AMDGPU_HW_IP_NUM; i++)
278 num_entities += amdgpu_ctx_num_entities[i];
Andrey Grodzovsky20b6b782018-05-15 14:12:21 -0400279
Christian König1b1f2fe2018-08-01 16:00:52 +0200280 for (i = 0; i < num_entities; i++)
281 drm_sched_entity_destroy(&ctx->entities[0][i].entity);
Christian König47f38502015-08-04 17:51:05 +0200282
Emily Deng8ee3a522018-04-16 10:07:02 +0800283 amdgpu_ctx_fini(ref);
Christian König47f38502015-08-04 17:51:05 +0200284}
285
286static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
287{
288 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
289 struct amdgpu_ctx *ctx;
290
291 mutex_lock(&mgr->lock);
Matthew Wilcoxd3e709e2016-12-22 13:30:22 -0500292 ctx = idr_remove(&mgr->ctx_handles, id);
293 if (ctx)
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800294 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
Christian König47f38502015-08-04 17:51:05 +0200295 mutex_unlock(&mgr->lock);
Matthew Wilcoxd3e709e2016-12-22 13:30:22 -0500296 return ctx ? 0 : -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400297}
298
Marek Olšákd94aed52015-05-05 21:13:49 +0200299static int amdgpu_ctx_query(struct amdgpu_device *adev,
300 struct amdgpu_fpriv *fpriv, uint32_t id,
301 union drm_amdgpu_ctx_out *out)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400302{
303 struct amdgpu_ctx *ctx;
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800304 struct amdgpu_ctx_mgr *mgr;
Marek Olšákd94aed52015-05-05 21:13:49 +0200305 unsigned reset_counter;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400306
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800307 if (!fpriv)
308 return -EINVAL;
309
310 mgr = &fpriv->ctx_mgr;
Marek Olšák0147ee02015-05-05 20:52:00 +0200311 mutex_lock(&mgr->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400312 ctx = idr_find(&mgr->ctx_handles, id);
Marek Olšákd94aed52015-05-05 21:13:49 +0200313 if (!ctx) {
Marek Olšák0147ee02015-05-05 20:52:00 +0200314 mutex_unlock(&mgr->lock);
Marek Olšákd94aed52015-05-05 21:13:49 +0200315 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400316 }
Marek Olšákd94aed52015-05-05 21:13:49 +0200317
318 /* TODO: these two are always zero */
Alex Deucher0b492a42015-08-16 22:48:26 -0400319 out->state.flags = 0x0;
320 out->state.hangs = 0x0;
Marek Olšákd94aed52015-05-05 21:13:49 +0200321
322 /* determine if a GPU reset has occured since the last call */
323 reset_counter = atomic_read(&adev->gpu_reset_counter);
324 /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
Monk Liu668ca1b2017-10-17 14:39:23 +0800325 if (ctx->reset_counter_query == reset_counter)
Marek Olšákd94aed52015-05-05 21:13:49 +0200326 out->state.reset_status = AMDGPU_CTX_NO_RESET;
327 else
328 out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
Monk Liu668ca1b2017-10-17 14:39:23 +0800329 ctx->reset_counter_query = reset_counter;
Marek Olšákd94aed52015-05-05 21:13:49 +0200330
Marek Olšák0147ee02015-05-05 20:52:00 +0200331 mutex_unlock(&mgr->lock);
Marek Olšákd94aed52015-05-05 21:13:49 +0200332 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400333}
334
Monk Liubc1b1bf2017-10-17 14:58:01 +0800335static int amdgpu_ctx_query2(struct amdgpu_device *adev,
336 struct amdgpu_fpriv *fpriv, uint32_t id,
337 union drm_amdgpu_ctx_out *out)
338{
339 struct amdgpu_ctx *ctx;
340 struct amdgpu_ctx_mgr *mgr;
341
342 if (!fpriv)
343 return -EINVAL;
344
345 mgr = &fpriv->ctx_mgr;
346 mutex_lock(&mgr->lock);
347 ctx = idr_find(&mgr->ctx_handles, id);
348 if (!ctx) {
349 mutex_unlock(&mgr->lock);
350 return -EINVAL;
351 }
352
353 out->state.flags = 0x0;
354 out->state.hangs = 0x0;
355
356 if (ctx->reset_counter != atomic_read(&adev->gpu_reset_counter))
357 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RESET;
358
359 if (ctx->vram_lost_counter != atomic_read(&adev->vram_lost_counter))
360 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_VRAMLOST;
361
362 if (atomic_read(&ctx->guilty))
363 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_GUILTY;
364
365 mutex_unlock(&mgr->lock);
366 return 0;
367}
368
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400369int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
Marek Olšákd94aed52015-05-05 21:13:49 +0200370 struct drm_file *filp)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400371{
372 int r;
373 uint32_t id;
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100374 enum drm_sched_priority priority;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400375
376 union drm_amdgpu_ctx *args = data;
377 struct amdgpu_device *adev = dev->dev_private;
378 struct amdgpu_fpriv *fpriv = filp->driver_priv;
379
380 r = 0;
381 id = args->in.ctx_id;
Andres Rodriguezc2636dc2016-12-22 17:06:50 -0500382 priority = amdgpu_to_sched_priority(args->in.priority);
383
Andres Rodriguezb6d8a432017-05-24 17:00:10 -0400384 /* For backwards compatibility reasons, we need to accept
385 * ioctls with garbage in the priority field */
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100386 if (priority == DRM_SCHED_PRIORITY_INVALID)
387 priority = DRM_SCHED_PRIORITY_NORMAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400388
389 switch (args->in.op) {
Christian Königa750b472016-02-11 10:20:53 +0100390 case AMDGPU_CTX_OP_ALLOC_CTX:
Andres Rodriguezc2636dc2016-12-22 17:06:50 -0500391 r = amdgpu_ctx_alloc(adev, fpriv, filp, priority, &id);
Christian Königa750b472016-02-11 10:20:53 +0100392 args->out.alloc.ctx_id = id;
393 break;
394 case AMDGPU_CTX_OP_FREE_CTX:
395 r = amdgpu_ctx_free(fpriv, id);
396 break;
397 case AMDGPU_CTX_OP_QUERY_STATE:
398 r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
399 break;
Monk Liubc1b1bf2017-10-17 14:58:01 +0800400 case AMDGPU_CTX_OP_QUERY_STATE2:
401 r = amdgpu_ctx_query2(adev, fpriv, id, &args->out);
402 break;
Christian Königa750b472016-02-11 10:20:53 +0100403 default:
404 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400405 }
406
407 return r;
408}
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800409
410struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
411{
412 struct amdgpu_ctx *ctx;
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800413 struct amdgpu_ctx_mgr *mgr;
414
415 if (!fpriv)
416 return NULL;
417
418 mgr = &fpriv->ctx_mgr;
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800419
420 mutex_lock(&mgr->lock);
421 ctx = idr_find(&mgr->ctx_handles, id);
422 if (ctx)
423 kref_get(&ctx->refcount);
424 mutex_unlock(&mgr->lock);
425 return ctx;
426}
427
428int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
429{
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800430 if (ctx == NULL)
431 return -EINVAL;
432
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800433 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800434 return 0;
435}
Christian König21c16bf2015-07-07 17:24:49 +0200436
Christian König0d346a12018-07-19 14:22:25 +0200437int amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx,
438 struct drm_sched_entity *entity,
439 struct dma_fence *fence, uint64_t* handle)
Christian König21c16bf2015-07-07 17:24:49 +0200440{
Christian König1b1f2fe2018-08-01 16:00:52 +0200441 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
442 uint64_t seq = centity->sequence;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100443 struct dma_fence *other = NULL;
Christian König0d346a12018-07-19 14:22:25 +0200444 unsigned idx = 0;
Christian König21c16bf2015-07-07 17:24:49 +0200445
Chunming Zhou5b011232015-12-10 17:34:33 +0800446 idx = seq & (amdgpu_sched_jobs - 1);
Christian König1b1f2fe2018-08-01 16:00:52 +0200447 other = centity->fences[idx];
Andrey Grodzovsky0ae94442017-10-10 16:50:17 -0400448 if (other)
449 BUG_ON(!dma_fence_is_signaled(other));
Christian König21c16bf2015-07-07 17:24:49 +0200450
Chris Wilsonf54d1862016-10-25 13:00:45 +0100451 dma_fence_get(fence);
Christian König21c16bf2015-07-07 17:24:49 +0200452
453 spin_lock(&ctx->ring_lock);
Christian König1b1f2fe2018-08-01 16:00:52 +0200454 centity->fences[idx] = fence;
455 centity->sequence++;
Christian König21c16bf2015-07-07 17:24:49 +0200456 spin_unlock(&ctx->ring_lock);
457
Chris Wilsonf54d1862016-10-25 13:00:45 +0100458 dma_fence_put(other);
Christian König0d346a12018-07-19 14:22:25 +0200459 if (handle)
460 *handle = seq;
Christian König21c16bf2015-07-07 17:24:49 +0200461
Monk Liueb01abc2017-09-15 13:40:31 +0800462 return 0;
Christian König21c16bf2015-07-07 17:24:49 +0200463}
464
Chris Wilsonf54d1862016-10-25 13:00:45 +0100465struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
Christian König0d346a12018-07-19 14:22:25 +0200466 struct drm_sched_entity *entity,
467 uint64_t seq)
Christian König21c16bf2015-07-07 17:24:49 +0200468{
Christian König1b1f2fe2018-08-01 16:00:52 +0200469 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100470 struct dma_fence *fence;
Christian König21c16bf2015-07-07 17:24:49 +0200471
472 spin_lock(&ctx->ring_lock);
Chunming Zhoub43a9a72015-07-21 15:13:53 +0800473
Monk Liud7b1eeb2017-04-07 18:39:07 +0800474 if (seq == ~0ull)
Christian König1b1f2fe2018-08-01 16:00:52 +0200475 seq = centity->sequence - 1;
Monk Liud7b1eeb2017-04-07 18:39:07 +0800476
Christian König1b1f2fe2018-08-01 16:00:52 +0200477 if (seq >= centity->sequence) {
Christian König21c16bf2015-07-07 17:24:49 +0200478 spin_unlock(&ctx->ring_lock);
479 return ERR_PTR(-EINVAL);
480 }
481
Chunming Zhoub43a9a72015-07-21 15:13:53 +0800482
Christian König1b1f2fe2018-08-01 16:00:52 +0200483 if (seq + amdgpu_sched_jobs < centity->sequence) {
Christian König21c16bf2015-07-07 17:24:49 +0200484 spin_unlock(&ctx->ring_lock);
485 return NULL;
486 }
487
Christian König1b1f2fe2018-08-01 16:00:52 +0200488 fence = dma_fence_get(centity->fences[seq & (amdgpu_sched_jobs - 1)]);
Christian König21c16bf2015-07-07 17:24:49 +0200489 spin_unlock(&ctx->ring_lock);
490
491 return fence;
492}
Christian Königefd4ccb2015-08-04 16:20:31 +0200493
Andres Rodriguezc23be4a2017-06-06 20:20:38 -0400494void amdgpu_ctx_priority_override(struct amdgpu_ctx *ctx,
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100495 enum drm_sched_priority priority)
Andres Rodriguezc23be4a2017-06-06 20:20:38 -0400496{
Christian König1b1f2fe2018-08-01 16:00:52 +0200497 unsigned num_entities = amdgput_ctx_total_num_entities();
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100498 enum drm_sched_priority ctx_prio;
Christian König1b1f2fe2018-08-01 16:00:52 +0200499 unsigned i;
Andres Rodriguezc23be4a2017-06-06 20:20:38 -0400500
501 ctx->override_priority = priority;
502
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100503 ctx_prio = (ctx->override_priority == DRM_SCHED_PRIORITY_UNSET) ?
Andres Rodriguezc23be4a2017-06-06 20:20:38 -0400504 ctx->init_priority : ctx->override_priority;
505
Christian König1b1f2fe2018-08-01 16:00:52 +0200506 for (i = 0; i < num_entities; i++) {
507 struct drm_sched_entity *entity = &ctx->entities[0][i].entity;
Andres Rodriguezc23be4a2017-06-06 20:20:38 -0400508
Christian König7febe4b2018-08-01 16:22:39 +0200509 drm_sched_entity_set_priority(entity, ctx_prio);
Andres Rodriguezc23be4a2017-06-06 20:20:38 -0400510 }
511}
512
Christian König0d346a12018-07-19 14:22:25 +0200513int amdgpu_ctx_wait_prev_fence(struct amdgpu_ctx *ctx,
514 struct drm_sched_entity *entity)
Andrey Grodzovsky0ae94442017-10-10 16:50:17 -0400515{
Christian König1b1f2fe2018-08-01 16:00:52 +0200516 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
517 unsigned idx = centity->sequence & (amdgpu_sched_jobs - 1);
518 struct dma_fence *other = centity->fences[idx];
Andrey Grodzovsky0ae94442017-10-10 16:50:17 -0400519
520 if (other) {
521 signed long r;
Andrey Grodzovsky719a39a2018-04-30 10:04:42 -0400522 r = dma_fence_wait(other, true);
Andrey Grodzovsky0ae94442017-10-10 16:50:17 -0400523 if (r < 0) {
Andrey Grodzovsky719a39a2018-04-30 10:04:42 -0400524 if (r != -ERESTARTSYS)
525 DRM_ERROR("Error (%ld) waiting for fence!\n", r);
526
Andrey Grodzovsky0ae94442017-10-10 16:50:17 -0400527 return r;
528 }
529 }
530
531 return 0;
532}
533
Christian Königefd4ccb2015-08-04 16:20:31 +0200534void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr)
535{
536 mutex_init(&mgr->lock);
537 idr_init(&mgr->ctx_handles);
538}
539
Andrey Grodzovskyc49d8282018-06-05 12:56:26 -0400540void amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr)
Emily Deng8ee3a522018-04-16 10:07:02 +0800541{
Christian König1b1f2fe2018-08-01 16:00:52 +0200542 unsigned num_entities = amdgput_ctx_total_num_entities();
Emily Deng8ee3a522018-04-16 10:07:02 +0800543 struct amdgpu_ctx *ctx;
544 struct idr *idp;
545 uint32_t id, i;
Andrey Grodzovsky48ad3682018-05-30 15:28:52 -0400546 long max_wait = MAX_WAIT_SCHED_ENTITY_Q_EMPTY;
Emily Deng8ee3a522018-04-16 10:07:02 +0800547
548 idp = &mgr->ctx_handles;
549
Andrey Grodzovsky48ad3682018-05-30 15:28:52 -0400550 mutex_lock(&mgr->lock);
Emily Deng8ee3a522018-04-16 10:07:02 +0800551 idr_for_each_entry(idp, ctx, id) {
552
Andrey Grodzovsky48ad3682018-05-30 15:28:52 -0400553 if (!ctx->adev) {
554 mutex_unlock(&mgr->lock);
Emily Deng8ee3a522018-04-16 10:07:02 +0800555 return;
Andrey Grodzovsky48ad3682018-05-30 15:28:52 -0400556 }
Emily Deng8ee3a522018-04-16 10:07:02 +0800557
Christian König1b1f2fe2018-08-01 16:00:52 +0200558 for (i = 0; i < num_entities; i++) {
559 struct drm_sched_entity *entity;
Andrey Grodzovsky20b6b782018-05-15 14:12:21 -0400560
Christian König1b1f2fe2018-08-01 16:00:52 +0200561 entity = &ctx->entities[0][i].entity;
562 max_wait = drm_sched_entity_flush(entity, max_wait);
Andrey Grodzovsky20b6b782018-05-15 14:12:21 -0400563 }
Emily Deng8ee3a522018-04-16 10:07:02 +0800564 }
Andrey Grodzovsky48ad3682018-05-30 15:28:52 -0400565 mutex_unlock(&mgr->lock);
Emily Deng8ee3a522018-04-16 10:07:02 +0800566}
567
Andrey Grodzovskyc49d8282018-06-05 12:56:26 -0400568void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
Emily Deng8ee3a522018-04-16 10:07:02 +0800569{
Christian König1b1f2fe2018-08-01 16:00:52 +0200570 unsigned num_entities = amdgput_ctx_total_num_entities();
Emily Deng8ee3a522018-04-16 10:07:02 +0800571 struct amdgpu_ctx *ctx;
572 struct idr *idp;
573 uint32_t id, i;
574
575 idp = &mgr->ctx_handles;
576
577 idr_for_each_entry(idp, ctx, id) {
578
579 if (!ctx->adev)
580 return;
581
Christian König1b1f2fe2018-08-01 16:00:52 +0200582 if (kref_read(&ctx->refcount) != 1) {
583 DRM_ERROR("ctx %p is still alive\n", ctx);
584 continue;
Andrey Grodzovsky20b6b782018-05-15 14:12:21 -0400585 }
Christian König1b1f2fe2018-08-01 16:00:52 +0200586
587 for (i = 0; i < num_entities; i++)
588 drm_sched_entity_fini(&ctx->entities[0][i].entity);
Emily Deng8ee3a522018-04-16 10:07:02 +0800589 }
590}
591
Christian Königefd4ccb2015-08-04 16:20:31 +0200592void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
593{
594 struct amdgpu_ctx *ctx;
595 struct idr *idp;
596 uint32_t id;
597
Andrey Grodzovskyc49d8282018-06-05 12:56:26 -0400598 amdgpu_ctx_mgr_entity_fini(mgr);
Emily Deng8ee3a522018-04-16 10:07:02 +0800599
Christian Königefd4ccb2015-08-04 16:20:31 +0200600 idp = &mgr->ctx_handles;
601
602 idr_for_each_entry(idp, ctx, id) {
Emily Deng8ee3a522018-04-16 10:07:02 +0800603 if (kref_put(&ctx->refcount, amdgpu_ctx_fini) != 1)
Christian Königefd4ccb2015-08-04 16:20:31 +0200604 DRM_ERROR("ctx %p is still alive\n", ctx);
605 }
606
607 idr_destroy(&mgr->ctx_handles);
608 mutex_destroy(&mgr->lock);
609}