blob: 95f4c4139fc60a078d651b8164d11b0befcf766b [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,
Alex Deucherf10d9102018-11-27 11:41:27 -050042 [AMDGPU_HW_IP_VCN_JPEG] = 1,
Christian König1b1f2fe2018-08-01 16:00:52 +020043};
44
45static int amdgput_ctx_total_num_entities(void)
46{
47 unsigned i, num_entities = 0;
48
49 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i)
50 num_entities += amdgpu_ctx_num_entities[i];
51
52 return num_entities;
53}
Christian König0d346a12018-07-19 14:22:25 +020054
Andres Rodriguezc2636dc2016-12-22 17:06:50 -050055static int amdgpu_ctx_priority_permit(struct drm_file *filp,
Lucas Stach1b1f42d2017-12-06 17:49:39 +010056 enum drm_sched_priority priority)
Andres Rodriguezc2636dc2016-12-22 17:06:50 -050057{
58 /* NORMAL and below are accessible by everyone */
Lucas Stach1b1f42d2017-12-06 17:49:39 +010059 if (priority <= DRM_SCHED_PRIORITY_NORMAL)
Andres Rodriguezc2636dc2016-12-22 17:06:50 -050060 return 0;
61
62 if (capable(CAP_SYS_NICE))
63 return 0;
64
65 if (drm_is_current_master(filp))
66 return 0;
67
68 return -EACCES;
69}
70
71static int amdgpu_ctx_init(struct amdgpu_device *adev,
Lucas Stach1b1f42d2017-12-06 17:49:39 +010072 enum drm_sched_priority priority,
Andres Rodriguezc2636dc2016-12-22 17:06:50 -050073 struct drm_file *filp,
74 struct amdgpu_ctx *ctx)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040075{
Christian König1b1f2fe2018-08-01 16:00:52 +020076 unsigned num_entities = amdgput_ctx_total_num_entities();
77 unsigned i, j;
Christian König47f38502015-08-04 17:51:05 +020078 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040079
Lucas Stach1b1f42d2017-12-06 17:49:39 +010080 if (priority < 0 || priority >= DRM_SCHED_PRIORITY_MAX)
Andres Rodriguezc2636dc2016-12-22 17:06:50 -050081 return -EINVAL;
82
83 r = amdgpu_ctx_priority_permit(filp, priority);
84 if (r)
85 return r;
86
Alex Deucherd38ceaf2015-04-20 16:55:21 -040087 memset(ctx, 0, sizeof(*ctx));
Chunming Zhou9cb7e5a2015-07-21 13:17:19 +080088 ctx->adev = adev;
Christian König1b1f2fe2018-08-01 16:00:52 +020089
90 ctx->fences = kcalloc(amdgpu_sched_jobs * num_entities,
Chris Wilsonf54d1862016-10-25 13:00:45 +010091 sizeof(struct dma_fence*), GFP_KERNEL);
Chunming Zhou37cd0ca2015-12-10 15:45:11 +080092 if (!ctx->fences)
93 return -ENOMEM;
Chunming Zhou23ca0e42015-07-06 13:42:58 +080094
Christian König1b1f2fe2018-08-01 16:00:52 +020095 ctx->entities[0] = kcalloc(num_entities,
96 sizeof(struct amdgpu_ctx_entity),
97 GFP_KERNEL);
98 if (!ctx->entities[0]) {
99 r = -ENOMEM;
100 goto error_free_fences;
Chunming Zhou37cd0ca2015-12-10 15:45:11 +0800101 }
Nicolai Hähnlece199ad2016-10-04 09:43:30 +0200102
Christian König1b1f2fe2018-08-01 16:00:52 +0200103 for (i = 0; i < num_entities; ++i) {
104 struct amdgpu_ctx_entity *entity = &ctx->entities[0][i];
105
106 entity->sequence = 1;
107 entity->fences = &ctx->fences[amdgpu_sched_jobs * i];
108 }
109 for (i = 1; i < AMDGPU_HW_IP_NUM; ++i)
110 ctx->entities[i] = ctx->entities[i - 1] +
111 amdgpu_ctx_num_entities[i - 1];
112
113 kref_init(&ctx->refcount);
114 spin_lock_init(&ctx->ring_lock);
115 mutex_init(&ctx->lock);
116
Nicolai Hähnlece199ad2016-10-04 09:43:30 +0200117 ctx->reset_counter = atomic_read(&adev->gpu_reset_counter);
Monk Liu668ca1b2017-10-17 14:39:23 +0800118 ctx->reset_counter_query = ctx->reset_counter;
Christian Könige55f2b62017-10-09 15:18:43 +0200119 ctx->vram_lost_counter = atomic_read(&adev->vram_lost_counter);
Andres Rodriguezc23be4a2017-06-06 20:20:38 -0400120 ctx->init_priority = priority;
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100121 ctx->override_priority = DRM_SCHED_PRIORITY_UNSET;
Nicolai Hähnlece199ad2016-10-04 09:43:30 +0200122
Christian König1b1f2fe2018-08-01 16:00:52 +0200123 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
124 struct amdgpu_ring *rings[AMDGPU_MAX_RINGS];
125 struct drm_sched_rq *rqs[AMDGPU_MAX_RINGS];
126 unsigned num_rings;
Christian König20874172016-02-11 09:56:44 +0100127
Christian König1b1f2fe2018-08-01 16:00:52 +0200128 switch (i) {
129 case AMDGPU_HW_IP_GFX:
130 rings[0] = &adev->gfx.gfx_ring[0];
131 num_rings = 1;
132 break;
133 case AMDGPU_HW_IP_COMPUTE:
134 for (j = 0; j < adev->gfx.num_compute_rings; ++j)
135 rings[j] = &adev->gfx.compute_ring[j];
136 num_rings = adev->gfx.num_compute_rings;
137 break;
138 case AMDGPU_HW_IP_DMA:
139 for (j = 0; j < adev->sdma.num_instances; ++j)
140 rings[j] = &adev->sdma.instance[j].ring;
141 num_rings = adev->sdma.num_instances;
142 break;
143 case AMDGPU_HW_IP_UVD:
144 rings[0] = &adev->uvd.inst[0].ring;
145 num_rings = 1;
146 break;
147 case AMDGPU_HW_IP_VCE:
148 rings[0] = &adev->vce.ring[0];
149 num_rings = 1;
150 break;
151 case AMDGPU_HW_IP_UVD_ENC:
152 rings[0] = &adev->uvd.inst[0].ring_enc[0];
153 num_rings = 1;
154 break;
155 case AMDGPU_HW_IP_VCN_DEC:
156 rings[0] = &adev->vcn.ring_dec;
157 num_rings = 1;
158 break;
159 case AMDGPU_HW_IP_VCN_ENC:
160 rings[0] = &adev->vcn.ring_enc[0];
161 num_rings = 1;
162 break;
163 case AMDGPU_HW_IP_VCN_JPEG:
164 rings[0] = &adev->vcn.ring_jpeg;
165 num_rings = 1;
166 break;
Christian König845e6fd2018-07-13 09:12:44 +0200167 }
Christian König1b1f2fe2018-08-01 16:00:52 +0200168
169 for (j = 0; j < num_rings; ++j)
170 rqs[j] = &rings[j]->sched.sched_rq[priority];
171
172 for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j)
173 r = drm_sched_entity_init(&ctx->entities[i][j].entity,
174 rqs, num_rings, &ctx->guilty);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800175 if (r)
Christian König1b1f2fe2018-08-01 16:00:52 +0200176 goto error_cleanup_entities;
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800177 }
178
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400179 return 0;
Huang Rui8ed81472016-10-26 17:07:03 +0800180
Christian König1b1f2fe2018-08-01 16:00:52 +0200181error_cleanup_entities:
182 for (i = 0; i < num_entities; ++i)
183 drm_sched_entity_destroy(&ctx->entities[0][i].entity);
184 kfree(ctx->entities[0]);
185
186error_free_fences:
Huang Rui8ed81472016-10-26 17:07:03 +0800187 kfree(ctx->fences);
188 ctx->fences = NULL;
189 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400190}
191
Emily Deng8ee3a522018-04-16 10:07:02 +0800192static void amdgpu_ctx_fini(struct kref *ref)
Christian König47f38502015-08-04 17:51:05 +0200193{
Emily Deng8ee3a522018-04-16 10:07:02 +0800194 struct amdgpu_ctx *ctx = container_of(ref, struct amdgpu_ctx, refcount);
Christian König1b1f2fe2018-08-01 16:00:52 +0200195 unsigned num_entities = amdgput_ctx_total_num_entities();
Christian König47f38502015-08-04 17:51:05 +0200196 struct amdgpu_device *adev = ctx->adev;
197 unsigned i, j;
198
Dave Airliefe295b22015-11-03 11:07:11 -0500199 if (!adev)
200 return;
201
Christian König1b1f2fe2018-08-01 16:00:52 +0200202 for (i = 0; i < num_entities; ++i)
Chunming Zhou37cd0ca2015-12-10 15:45:11 +0800203 for (j = 0; j < amdgpu_sched_jobs; ++j)
Christian König1b1f2fe2018-08-01 16:00:52 +0200204 dma_fence_put(ctx->entities[0][i].fences[j]);
Chunming Zhou37cd0ca2015-12-10 15:45:11 +0800205 kfree(ctx->fences);
Christian König1b1f2fe2018-08-01 16:00:52 +0200206 kfree(ctx->entities[0]);
Christian König47f38502015-08-04 17:51:05 +0200207
Andrey Grodzovsky0ae94442017-10-10 16:50:17 -0400208 mutex_destroy(&ctx->lock);
Emily Deng8ee3a522018-04-16 10:07:02 +0800209
210 kfree(ctx);
Christian König47f38502015-08-04 17:51:05 +0200211}
212
Christian König0d346a12018-07-19 14:22:25 +0200213int amdgpu_ctx_get_entity(struct amdgpu_ctx *ctx, u32 hw_ip, u32 instance,
214 u32 ring, struct drm_sched_entity **entity)
Christian König869a53d2018-07-16 15:19:20 +0200215{
Christian König1b1f2fe2018-08-01 16:00:52 +0200216 if (hw_ip >= AMDGPU_HW_IP_NUM) {
217 DRM_ERROR("unknown HW IP type: %d\n", hw_ip);
218 return -EINVAL;
219 }
Christian König869a53d2018-07-16 15:19:20 +0200220
221 /* Right now all IPs have only one instance - multiple rings. */
222 if (instance != 0) {
223 DRM_DEBUG("invalid ip instance: %d\n", instance);
224 return -EINVAL;
225 }
226
Christian König1b1f2fe2018-08-01 16:00:52 +0200227 if (ring >= amdgpu_ctx_num_entities[hw_ip]) {
228 DRM_DEBUG("invalid ring: %d %d\n", hw_ip, ring);
Christian König869a53d2018-07-16 15:19:20 +0200229 return -EINVAL;
230 }
231
Christian König1b1f2fe2018-08-01 16:00:52 +0200232 *entity = &ctx->entities[hw_ip][ring].entity;
Christian König869a53d2018-07-16 15:19:20 +0200233 return 0;
234}
235
Christian König47f38502015-08-04 17:51:05 +0200236static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
237 struct amdgpu_fpriv *fpriv,
Andres Rodriguezc2636dc2016-12-22 17:06:50 -0500238 struct drm_file *filp,
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100239 enum drm_sched_priority priority,
Christian König47f38502015-08-04 17:51:05 +0200240 uint32_t *id)
241{
242 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
243 struct amdgpu_ctx *ctx;
244 int r;
245
246 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
247 if (!ctx)
248 return -ENOMEM;
249
250 mutex_lock(&mgr->lock);
251 r = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
252 if (r < 0) {
253 mutex_unlock(&mgr->lock);
254 kfree(ctx);
255 return r;
256 }
Andres Rodriguezc2636dc2016-12-22 17:06:50 -0500257
Christian König47f38502015-08-04 17:51:05 +0200258 *id = (uint32_t)r;
Andres Rodriguezc2636dc2016-12-22 17:06:50 -0500259 r = amdgpu_ctx_init(adev, priority, filp, ctx);
Chunming Zhouc648ed72015-12-10 15:50:02 +0800260 if (r) {
261 idr_remove(&mgr->ctx_handles, *id);
262 *id = 0;
263 kfree(ctx);
264 }
Christian König47f38502015-08-04 17:51:05 +0200265 mutex_unlock(&mgr->lock);
Christian König47f38502015-08-04 17:51:05 +0200266 return r;
267}
268
269static void amdgpu_ctx_do_release(struct kref *ref)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400270{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400271 struct amdgpu_ctx *ctx;
Christian König1b1f2fe2018-08-01 16:00:52 +0200272 unsigned num_entities;
Emily Deng8ee3a522018-04-16 10:07:02 +0800273 u32 i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400274
Christian König47f38502015-08-04 17:51:05 +0200275 ctx = container_of(ref, struct amdgpu_ctx, refcount);
276
Christian König1b1f2fe2018-08-01 16:00:52 +0200277 num_entities = 0;
278 for (i = 0; i < AMDGPU_HW_IP_NUM; i++)
279 num_entities += amdgpu_ctx_num_entities[i];
Andrey Grodzovsky20b6b782018-05-15 14:12:21 -0400280
Christian König1b1f2fe2018-08-01 16:00:52 +0200281 for (i = 0; i < num_entities; i++)
282 drm_sched_entity_destroy(&ctx->entities[0][i].entity);
Christian König47f38502015-08-04 17:51:05 +0200283
Emily Deng8ee3a522018-04-16 10:07:02 +0800284 amdgpu_ctx_fini(ref);
Christian König47f38502015-08-04 17:51:05 +0200285}
286
287static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
288{
289 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
290 struct amdgpu_ctx *ctx;
291
292 mutex_lock(&mgr->lock);
Matthew Wilcoxd3e709e2016-12-22 13:30:22 -0500293 ctx = idr_remove(&mgr->ctx_handles, id);
294 if (ctx)
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800295 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
Christian König47f38502015-08-04 17:51:05 +0200296 mutex_unlock(&mgr->lock);
Matthew Wilcoxd3e709e2016-12-22 13:30:22 -0500297 return ctx ? 0 : -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400298}
299
Marek Olšákd94aed52015-05-05 21:13:49 +0200300static int amdgpu_ctx_query(struct amdgpu_device *adev,
301 struct amdgpu_fpriv *fpriv, uint32_t id,
302 union drm_amdgpu_ctx_out *out)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400303{
304 struct amdgpu_ctx *ctx;
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800305 struct amdgpu_ctx_mgr *mgr;
Marek Olšákd94aed52015-05-05 21:13:49 +0200306 unsigned reset_counter;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400307
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800308 if (!fpriv)
309 return -EINVAL;
310
311 mgr = &fpriv->ctx_mgr;
Marek Olšák0147ee02015-05-05 20:52:00 +0200312 mutex_lock(&mgr->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400313 ctx = idr_find(&mgr->ctx_handles, id);
Marek Olšákd94aed52015-05-05 21:13:49 +0200314 if (!ctx) {
Marek Olšák0147ee02015-05-05 20:52:00 +0200315 mutex_unlock(&mgr->lock);
Marek Olšákd94aed52015-05-05 21:13:49 +0200316 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400317 }
Marek Olšákd94aed52015-05-05 21:13:49 +0200318
319 /* TODO: these two are always zero */
Alex Deucher0b492a42015-08-16 22:48:26 -0400320 out->state.flags = 0x0;
321 out->state.hangs = 0x0;
Marek Olšákd94aed52015-05-05 21:13:49 +0200322
323 /* determine if a GPU reset has occured since the last call */
324 reset_counter = atomic_read(&adev->gpu_reset_counter);
325 /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
Monk Liu668ca1b2017-10-17 14:39:23 +0800326 if (ctx->reset_counter_query == reset_counter)
Marek Olšákd94aed52015-05-05 21:13:49 +0200327 out->state.reset_status = AMDGPU_CTX_NO_RESET;
328 else
329 out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
Monk Liu668ca1b2017-10-17 14:39:23 +0800330 ctx->reset_counter_query = reset_counter;
Marek Olšákd94aed52015-05-05 21:13:49 +0200331
Marek Olšák0147ee02015-05-05 20:52:00 +0200332 mutex_unlock(&mgr->lock);
Marek Olšákd94aed52015-05-05 21:13:49 +0200333 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400334}
335
Monk Liubc1b1bf2017-10-17 14:58:01 +0800336static int amdgpu_ctx_query2(struct amdgpu_device *adev,
337 struct amdgpu_fpriv *fpriv, uint32_t id,
338 union drm_amdgpu_ctx_out *out)
339{
340 struct amdgpu_ctx *ctx;
341 struct amdgpu_ctx_mgr *mgr;
342
343 if (!fpriv)
344 return -EINVAL;
345
346 mgr = &fpriv->ctx_mgr;
347 mutex_lock(&mgr->lock);
348 ctx = idr_find(&mgr->ctx_handles, id);
349 if (!ctx) {
350 mutex_unlock(&mgr->lock);
351 return -EINVAL;
352 }
353
354 out->state.flags = 0x0;
355 out->state.hangs = 0x0;
356
357 if (ctx->reset_counter != atomic_read(&adev->gpu_reset_counter))
358 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RESET;
359
360 if (ctx->vram_lost_counter != atomic_read(&adev->vram_lost_counter))
361 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_VRAMLOST;
362
363 if (atomic_read(&ctx->guilty))
364 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_GUILTY;
365
366 mutex_unlock(&mgr->lock);
367 return 0;
368}
369
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400370int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
Marek Olšákd94aed52015-05-05 21:13:49 +0200371 struct drm_file *filp)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400372{
373 int r;
374 uint32_t id;
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100375 enum drm_sched_priority priority;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400376
377 union drm_amdgpu_ctx *args = data;
378 struct amdgpu_device *adev = dev->dev_private;
379 struct amdgpu_fpriv *fpriv = filp->driver_priv;
380
381 r = 0;
382 id = args->in.ctx_id;
Andres Rodriguezc2636dc2016-12-22 17:06:50 -0500383 priority = amdgpu_to_sched_priority(args->in.priority);
384
Andres Rodriguezb6d8a432017-05-24 17:00:10 -0400385 /* For backwards compatibility reasons, we need to accept
386 * ioctls with garbage in the priority field */
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100387 if (priority == DRM_SCHED_PRIORITY_INVALID)
388 priority = DRM_SCHED_PRIORITY_NORMAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400389
390 switch (args->in.op) {
Christian Königa750b472016-02-11 10:20:53 +0100391 case AMDGPU_CTX_OP_ALLOC_CTX:
Andres Rodriguezc2636dc2016-12-22 17:06:50 -0500392 r = amdgpu_ctx_alloc(adev, fpriv, filp, priority, &id);
Christian Königa750b472016-02-11 10:20:53 +0100393 args->out.alloc.ctx_id = id;
394 break;
395 case AMDGPU_CTX_OP_FREE_CTX:
396 r = amdgpu_ctx_free(fpriv, id);
397 break;
398 case AMDGPU_CTX_OP_QUERY_STATE:
399 r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
400 break;
Monk Liubc1b1bf2017-10-17 14:58:01 +0800401 case AMDGPU_CTX_OP_QUERY_STATE2:
402 r = amdgpu_ctx_query2(adev, fpriv, id, &args->out);
403 break;
Christian Königa750b472016-02-11 10:20:53 +0100404 default:
405 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400406 }
407
408 return r;
409}
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800410
411struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
412{
413 struct amdgpu_ctx *ctx;
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800414 struct amdgpu_ctx_mgr *mgr;
415
416 if (!fpriv)
417 return NULL;
418
419 mgr = &fpriv->ctx_mgr;
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800420
421 mutex_lock(&mgr->lock);
422 ctx = idr_find(&mgr->ctx_handles, id);
423 if (ctx)
424 kref_get(&ctx->refcount);
425 mutex_unlock(&mgr->lock);
426 return ctx;
427}
428
429int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
430{
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800431 if (ctx == NULL)
432 return -EINVAL;
433
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800434 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800435 return 0;
436}
Christian König21c16bf2015-07-07 17:24:49 +0200437
Christian König85eff202018-08-24 14:23:33 +0200438void amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx,
439 struct drm_sched_entity *entity,
440 struct dma_fence *fence, uint64_t* handle)
Christian König21c16bf2015-07-07 17:24:49 +0200441{
Christian König1b1f2fe2018-08-01 16:00:52 +0200442 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
443 uint64_t seq = centity->sequence;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100444 struct dma_fence *other = NULL;
Christian König0d346a12018-07-19 14:22:25 +0200445 unsigned idx = 0;
Christian König21c16bf2015-07-07 17:24:49 +0200446
Chunming Zhou5b011232015-12-10 17:34:33 +0800447 idx = seq & (amdgpu_sched_jobs - 1);
Christian König1b1f2fe2018-08-01 16:00:52 +0200448 other = centity->fences[idx];
Andrey Grodzovsky0ae94442017-10-10 16:50:17 -0400449 if (other)
450 BUG_ON(!dma_fence_is_signaled(other));
Christian König21c16bf2015-07-07 17:24:49 +0200451
Chris Wilsonf54d1862016-10-25 13:00:45 +0100452 dma_fence_get(fence);
Christian König21c16bf2015-07-07 17:24:49 +0200453
454 spin_lock(&ctx->ring_lock);
Christian König1b1f2fe2018-08-01 16:00:52 +0200455 centity->fences[idx] = fence;
456 centity->sequence++;
Christian König21c16bf2015-07-07 17:24:49 +0200457 spin_unlock(&ctx->ring_lock);
458
Chris Wilsonf54d1862016-10-25 13:00:45 +0100459 dma_fence_put(other);
Christian König0d346a12018-07-19 14:22:25 +0200460 if (handle)
461 *handle = seq;
Christian König21c16bf2015-07-07 17:24:49 +0200462}
463
Chris Wilsonf54d1862016-10-25 13:00:45 +0100464struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
Christian König0d346a12018-07-19 14:22:25 +0200465 struct drm_sched_entity *entity,
466 uint64_t seq)
Christian König21c16bf2015-07-07 17:24:49 +0200467{
Christian König1b1f2fe2018-08-01 16:00:52 +0200468 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100469 struct dma_fence *fence;
Christian König21c16bf2015-07-07 17:24:49 +0200470
471 spin_lock(&ctx->ring_lock);
Chunming Zhoub43a9a72015-07-21 15:13:53 +0800472
Monk Liud7b1eeb2017-04-07 18:39:07 +0800473 if (seq == ~0ull)
Christian König1b1f2fe2018-08-01 16:00:52 +0200474 seq = centity->sequence - 1;
Monk Liud7b1eeb2017-04-07 18:39:07 +0800475
Christian König1b1f2fe2018-08-01 16:00:52 +0200476 if (seq >= centity->sequence) {
Christian König21c16bf2015-07-07 17:24:49 +0200477 spin_unlock(&ctx->ring_lock);
478 return ERR_PTR(-EINVAL);
479 }
480
Chunming Zhoub43a9a72015-07-21 15:13:53 +0800481
Christian König1b1f2fe2018-08-01 16:00:52 +0200482 if (seq + amdgpu_sched_jobs < centity->sequence) {
Christian König21c16bf2015-07-07 17:24:49 +0200483 spin_unlock(&ctx->ring_lock);
484 return NULL;
485 }
486
Christian König1b1f2fe2018-08-01 16:00:52 +0200487 fence = dma_fence_get(centity->fences[seq & (amdgpu_sched_jobs - 1)]);
Christian König21c16bf2015-07-07 17:24:49 +0200488 spin_unlock(&ctx->ring_lock);
489
490 return fence;
491}
Christian Königefd4ccb2015-08-04 16:20:31 +0200492
Andres Rodriguezc23be4a2017-06-06 20:20:38 -0400493void amdgpu_ctx_priority_override(struct amdgpu_ctx *ctx,
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100494 enum drm_sched_priority priority)
Andres Rodriguezc23be4a2017-06-06 20:20:38 -0400495{
Christian König1b1f2fe2018-08-01 16:00:52 +0200496 unsigned num_entities = amdgput_ctx_total_num_entities();
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100497 enum drm_sched_priority ctx_prio;
Christian König1b1f2fe2018-08-01 16:00:52 +0200498 unsigned i;
Andres Rodriguezc23be4a2017-06-06 20:20:38 -0400499
500 ctx->override_priority = priority;
501
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100502 ctx_prio = (ctx->override_priority == DRM_SCHED_PRIORITY_UNSET) ?
Andres Rodriguezc23be4a2017-06-06 20:20:38 -0400503 ctx->init_priority : ctx->override_priority;
504
Christian König1b1f2fe2018-08-01 16:00:52 +0200505 for (i = 0; i < num_entities; i++) {
506 struct drm_sched_entity *entity = &ctx->entities[0][i].entity;
Andres Rodriguezc23be4a2017-06-06 20:20:38 -0400507
Christian König7febe4b2018-08-01 16:22:39 +0200508 drm_sched_entity_set_priority(entity, ctx_prio);
Andres Rodriguezc23be4a2017-06-06 20:20:38 -0400509 }
510}
511
Christian König0d346a12018-07-19 14:22:25 +0200512int amdgpu_ctx_wait_prev_fence(struct amdgpu_ctx *ctx,
513 struct drm_sched_entity *entity)
Andrey Grodzovsky0ae94442017-10-10 16:50:17 -0400514{
Christian König1b1f2fe2018-08-01 16:00:52 +0200515 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
516 unsigned idx = centity->sequence & (amdgpu_sched_jobs - 1);
517 struct dma_fence *other = centity->fences[idx];
Andrey Grodzovsky0ae94442017-10-10 16:50:17 -0400518
519 if (other) {
520 signed long r;
Andrey Grodzovsky719a39a2018-04-30 10:04:42 -0400521 r = dma_fence_wait(other, true);
Andrey Grodzovsky0ae94442017-10-10 16:50:17 -0400522 if (r < 0) {
Andrey Grodzovsky719a39a2018-04-30 10:04:42 -0400523 if (r != -ERESTARTSYS)
524 DRM_ERROR("Error (%ld) waiting for fence!\n", r);
525
Andrey Grodzovsky0ae94442017-10-10 16:50:17 -0400526 return r;
527 }
528 }
529
530 return 0;
531}
532
Christian Königefd4ccb2015-08-04 16:20:31 +0200533void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr)
534{
535 mutex_init(&mgr->lock);
536 idr_init(&mgr->ctx_handles);
537}
538
Andrey Grodzovskyc49d8282018-06-05 12:56:26 -0400539void amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr)
Emily Deng8ee3a522018-04-16 10:07:02 +0800540{
Christian König1b1f2fe2018-08-01 16:00:52 +0200541 unsigned num_entities = amdgput_ctx_total_num_entities();
Emily Deng8ee3a522018-04-16 10:07:02 +0800542 struct amdgpu_ctx *ctx;
543 struct idr *idp;
544 uint32_t id, i;
Andrey Grodzovsky48ad3682018-05-30 15:28:52 -0400545 long max_wait = MAX_WAIT_SCHED_ENTITY_Q_EMPTY;
Emily Deng8ee3a522018-04-16 10:07:02 +0800546
547 idp = &mgr->ctx_handles;
548
Andrey Grodzovsky48ad3682018-05-30 15:28:52 -0400549 mutex_lock(&mgr->lock);
Emily Deng8ee3a522018-04-16 10:07:02 +0800550 idr_for_each_entry(idp, ctx, id) {
551
Andrey Grodzovsky48ad3682018-05-30 15:28:52 -0400552 if (!ctx->adev) {
553 mutex_unlock(&mgr->lock);
Emily Deng8ee3a522018-04-16 10:07:02 +0800554 return;
Andrey Grodzovsky48ad3682018-05-30 15:28:52 -0400555 }
Emily Deng8ee3a522018-04-16 10:07:02 +0800556
Christian König1b1f2fe2018-08-01 16:00:52 +0200557 for (i = 0; i < num_entities; i++) {
558 struct drm_sched_entity *entity;
Andrey Grodzovsky20b6b782018-05-15 14:12:21 -0400559
Christian König1b1f2fe2018-08-01 16:00:52 +0200560 entity = &ctx->entities[0][i].entity;
561 max_wait = drm_sched_entity_flush(entity, max_wait);
Andrey Grodzovsky20b6b782018-05-15 14:12:21 -0400562 }
Emily Deng8ee3a522018-04-16 10:07:02 +0800563 }
Andrey Grodzovsky48ad3682018-05-30 15:28:52 -0400564 mutex_unlock(&mgr->lock);
Emily Deng8ee3a522018-04-16 10:07:02 +0800565}
566
Andrey Grodzovskyc49d8282018-06-05 12:56:26 -0400567void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
Emily Deng8ee3a522018-04-16 10:07:02 +0800568{
Christian König1b1f2fe2018-08-01 16:00:52 +0200569 unsigned num_entities = amdgput_ctx_total_num_entities();
Emily Deng8ee3a522018-04-16 10:07:02 +0800570 struct amdgpu_ctx *ctx;
571 struct idr *idp;
572 uint32_t id, i;
573
574 idp = &mgr->ctx_handles;
575
576 idr_for_each_entry(idp, ctx, id) {
577
578 if (!ctx->adev)
579 return;
580
Christian König1b1f2fe2018-08-01 16:00:52 +0200581 if (kref_read(&ctx->refcount) != 1) {
582 DRM_ERROR("ctx %p is still alive\n", ctx);
583 continue;
Andrey Grodzovsky20b6b782018-05-15 14:12:21 -0400584 }
Christian König1b1f2fe2018-08-01 16:00:52 +0200585
586 for (i = 0; i < num_entities; i++)
587 drm_sched_entity_fini(&ctx->entities[0][i].entity);
Emily Deng8ee3a522018-04-16 10:07:02 +0800588 }
589}
590
Christian Königefd4ccb2015-08-04 16:20:31 +0200591void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
592{
593 struct amdgpu_ctx *ctx;
594 struct idr *idp;
595 uint32_t id;
596
Andrey Grodzovskyc49d8282018-06-05 12:56:26 -0400597 amdgpu_ctx_mgr_entity_fini(mgr);
Emily Deng8ee3a522018-04-16 10:07:02 +0800598
Christian Königefd4ccb2015-08-04 16:20:31 +0200599 idp = &mgr->ctx_handles;
600
601 idr_for_each_entry(idp, ctx, id) {
Emily Deng8ee3a522018-04-16 10:07:02 +0800602 if (kref_put(&ctx->refcount, amdgpu_ctx_fini) != 1)
Christian Königefd4ccb2015-08-04 16:20:31 +0200603 DRM_ERROR("ctx %p is still alive\n", ctx);
604 }
605
606 idr_destroy(&mgr->ctx_handles);
607 mutex_destroy(&mgr->lock);
608}