1 /*
2 * Copyright 2014 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 */
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <pthread.h>
29 #include <sched.h>
30 #include <sys/ioctl.h>
31 #if HAVE_ALLOCA_H
32 # include <alloca.h>
33 #endif
34
35 #include "xf86drm.h"
36 #include "amdgpu_drm.h"
37 #include "amdgpu_internal.h"
38
39 static int amdgpu_cs_unreference_sem(amdgpu_semaphore_handle sem);
40 static int amdgpu_cs_reset_sem(amdgpu_semaphore_handle sem);
41
42 /**
43 * Create command submission context
44 *
45 * \param dev - \c [in] Device handle. See #amdgpu_device_initialize()
46 * \param priority - \c [in] Context creation flags. See AMDGPU_CTX_PRIORITY_*
47 * \param context - \c [out] GPU Context handle
48 *
49 * \return 0 on success otherwise POSIX Error code
50 */
amdgpu_cs_ctx_create2(amdgpu_device_handle dev,uint32_t priority,amdgpu_context_handle * context)51 drm_public int amdgpu_cs_ctx_create2(amdgpu_device_handle dev,
52 uint32_t priority,
53 amdgpu_context_handle *context)
54 {
55 struct amdgpu_context *gpu_context;
56 union drm_amdgpu_ctx args;
57 int i, j, k;
58 int r;
59
60 if (!dev || !context)
61 return -EINVAL;
62
63 gpu_context = calloc(1, sizeof(struct amdgpu_context));
64 if (!gpu_context)
65 return -ENOMEM;
66
67 gpu_context->dev = dev;
68
69 r = pthread_mutex_init(&gpu_context->sequence_mutex, NULL);
70 if (r)
71 goto error;
72
73 /* Create the context */
74 memset(&args, 0, sizeof(args));
75 args.in.op = AMDGPU_CTX_OP_ALLOC_CTX;
76 args.in.priority = priority;
77
78 r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_CTX, &args, sizeof(args));
79 if (r)
80 goto error;
81
82 gpu_context->id = args.out.alloc.ctx_id;
83 for (i = 0; i < AMDGPU_HW_IP_NUM; i++)
84 for (j = 0; j < AMDGPU_HW_IP_INSTANCE_MAX_COUNT; j++)
85 for (k = 0; k < AMDGPU_CS_MAX_RINGS; k++)
86 list_inithead(&gpu_context->sem_list[i][j][k]);
87 *context = (amdgpu_context_handle)gpu_context;
88
89 return 0;
90
91 error:
92 pthread_mutex_destroy(&gpu_context->sequence_mutex);
93 free(gpu_context);
94 return r;
95 }
96
amdgpu_cs_ctx_create(amdgpu_device_handle dev,amdgpu_context_handle * context)97 drm_public int amdgpu_cs_ctx_create(amdgpu_device_handle dev,
98 amdgpu_context_handle *context)
99 {
100 return amdgpu_cs_ctx_create2(dev, AMDGPU_CTX_PRIORITY_NORMAL, context);
101 }
102
103 /**
104 * Release command submission context
105 *
106 * \param dev - \c [in] amdgpu device handle
107 * \param context - \c [in] amdgpu context handle
108 *
109 * \return 0 on success otherwise POSIX Error code
110 */
amdgpu_cs_ctx_free(amdgpu_context_handle context)111 drm_public int amdgpu_cs_ctx_free(amdgpu_context_handle context)
112 {
113 union drm_amdgpu_ctx args;
114 int i, j, k;
115 int r;
116
117 if (!context)
118 return -EINVAL;
119
120 pthread_mutex_destroy(&context->sequence_mutex);
121
122 /* now deal with kernel side */
123 memset(&args, 0, sizeof(args));
124 args.in.op = AMDGPU_CTX_OP_FREE_CTX;
125 args.in.ctx_id = context->id;
126 r = drmCommandWriteRead(context->dev->fd, DRM_AMDGPU_CTX,
127 &args, sizeof(args));
128 for (i = 0; i < AMDGPU_HW_IP_NUM; i++) {
129 for (j = 0; j < AMDGPU_HW_IP_INSTANCE_MAX_COUNT; j++) {
130 for (k = 0; k < AMDGPU_CS_MAX_RINGS; k++) {
131 amdgpu_semaphore_handle sem;
132 LIST_FOR_EACH_ENTRY(sem, &context->sem_list[i][j][k], list) {
133 list_del(&sem->list);
134 amdgpu_cs_reset_sem(sem);
135 amdgpu_cs_unreference_sem(sem);
136 }
137 }
138 }
139 }
140 free(context);
141
142 return r;
143 }
144
amdgpu_cs_ctx_override_priority(amdgpu_device_handle dev,amdgpu_context_handle context,int master_fd,unsigned priority)145 drm_public int amdgpu_cs_ctx_override_priority(amdgpu_device_handle dev,
146 amdgpu_context_handle context,
147 int master_fd,
148 unsigned priority)
149 {
150 union drm_amdgpu_sched args;
151 int r;
152
153 if (!dev || !context || master_fd < 0)
154 return -EINVAL;
155
156 memset(&args, 0, sizeof(args));
157
158 args.in.op = AMDGPU_SCHED_OP_CONTEXT_PRIORITY_OVERRIDE;
159 args.in.fd = dev->fd;
160 args.in.priority = priority;
161 args.in.ctx_id = context->id;
162
163 r = drmCommandWrite(master_fd, DRM_AMDGPU_SCHED, &args, sizeof(args));
164 if (r)
165 return r;
166
167 return 0;
168 }
169
amdgpu_cs_query_reset_state(amdgpu_context_handle context,uint32_t * state,uint32_t * hangs)170 drm_public int amdgpu_cs_query_reset_state(amdgpu_context_handle context,
171 uint32_t *state, uint32_t *hangs)
172 {
173 union drm_amdgpu_ctx args;
174 int r;
175
176 if (!context)
177 return -EINVAL;
178
179 memset(&args, 0, sizeof(args));
180 args.in.op = AMDGPU_CTX_OP_QUERY_STATE;
181 args.in.ctx_id = context->id;
182 r = drmCommandWriteRead(context->dev->fd, DRM_AMDGPU_CTX,
183 &args, sizeof(args));
184 if (!r) {
185 *state = args.out.state.reset_status;
186 *hangs = args.out.state.hangs;
187 }
188 return r;
189 }
190
amdgpu_cs_query_reset_state2(amdgpu_context_handle context,uint64_t * flags)191 drm_public int amdgpu_cs_query_reset_state2(amdgpu_context_handle context,
192 uint64_t *flags)
193 {
194 union drm_amdgpu_ctx args;
195 int r;
196
197 if (!context)
198 return -EINVAL;
199
200 memset(&args, 0, sizeof(args));
201 args.in.op = AMDGPU_CTX_OP_QUERY_STATE2;
202 args.in.ctx_id = context->id;
203 r = drmCommandWriteRead(context->dev->fd, DRM_AMDGPU_CTX,
204 &args, sizeof(args));
205 if (!r)
206 *flags = args.out.state.flags;
207 return r;
208 }
209
210 /**
211 * Submit command to kernel DRM
212 * \param dev - \c [in] Device handle
213 * \param context - \c [in] GPU Context
214 * \param ibs_request - \c [in] Pointer to submission requests
215 * \param fence - \c [out] return fence for this submission
216 *
217 * \return 0 on success otherwise POSIX Error code
218 * \sa amdgpu_cs_submit()
219 */
amdgpu_cs_submit_one(amdgpu_context_handle context,struct amdgpu_cs_request * ibs_request)220 static int amdgpu_cs_submit_one(amdgpu_context_handle context,
221 struct amdgpu_cs_request *ibs_request)
222 {
223 struct drm_amdgpu_cs_chunk *chunks;
224 struct drm_amdgpu_cs_chunk_data *chunk_data;
225 struct drm_amdgpu_cs_chunk_dep *dependencies = NULL;
226 struct drm_amdgpu_cs_chunk_dep *sem_dependencies = NULL;
227 amdgpu_device_handle dev = context->dev;
228 struct list_head *sem_list;
229 amdgpu_semaphore_handle sem, tmp;
230 uint32_t i, size, num_chunks, bo_list_handle = 0, sem_count = 0;
231 uint64_t seq_no;
232 bool user_fence;
233 int r = 0;
234
235 if (ibs_request->ip_type >= AMDGPU_HW_IP_NUM)
236 return -EINVAL;
237 if (ibs_request->ring >= AMDGPU_CS_MAX_RINGS)
238 return -EINVAL;
239 if (ibs_request->number_of_ibs == 0) {
240 ibs_request->seq_no = AMDGPU_NULL_SUBMIT_SEQ;
241 return 0;
242 }
243 user_fence = (ibs_request->fence_info.handle != NULL);
244
245 size = ibs_request->number_of_ibs + (user_fence ? 2 : 1) + 1;
246
247 chunks = alloca(sizeof(struct drm_amdgpu_cs_chunk) * size);
248
249 size = ibs_request->number_of_ibs + (user_fence ? 1 : 0);
250
251 chunk_data = alloca(sizeof(struct drm_amdgpu_cs_chunk_data) * size);
252
253 if (ibs_request->resources)
254 bo_list_handle = ibs_request->resources->handle;
255 num_chunks = ibs_request->number_of_ibs;
256 /* IB chunks */
257 for (i = 0; i < ibs_request->number_of_ibs; i++) {
258 struct amdgpu_cs_ib_info *ib;
259 chunks[i].chunk_id = AMDGPU_CHUNK_ID_IB;
260 chunks[i].length_dw = sizeof(struct drm_amdgpu_cs_chunk_ib) / 4;
261 chunks[i].chunk_data = (uint64_t)(uintptr_t)&chunk_data[i];
262
263 ib = &ibs_request->ibs[i];
264
265 chunk_data[i].ib_data._pad = 0;
266 chunk_data[i].ib_data.va_start = ib->ib_mc_address;
267 chunk_data[i].ib_data.ib_bytes = ib->size * 4;
268 chunk_data[i].ib_data.ip_type = ibs_request->ip_type;
269 chunk_data[i].ib_data.ip_instance = ibs_request->ip_instance;
270 chunk_data[i].ib_data.ring = ibs_request->ring;
271 chunk_data[i].ib_data.flags = ib->flags;
272 }
273
274 pthread_mutex_lock(&context->sequence_mutex);
275
276 if (user_fence) {
277 i = num_chunks++;
278
279 /* fence chunk */
280 chunks[i].chunk_id = AMDGPU_CHUNK_ID_FENCE;
281 chunks[i].length_dw = sizeof(struct drm_amdgpu_cs_chunk_fence) / 4;
282 chunks[i].chunk_data = (uint64_t)(uintptr_t)&chunk_data[i];
283
284 /* fence bo handle */
285 chunk_data[i].fence_data.handle = ibs_request->fence_info.handle->handle;
286 /* offset */
287 chunk_data[i].fence_data.offset =
288 ibs_request->fence_info.offset * sizeof(uint64_t);
289 }
290
291 if (ibs_request->number_of_dependencies) {
292 dependencies = alloca(sizeof(struct drm_amdgpu_cs_chunk_dep) *
293 ibs_request->number_of_dependencies);
294 if (!dependencies) {
295 r = -ENOMEM;
296 goto error_unlock;
297 }
298
299 for (i = 0; i < ibs_request->number_of_dependencies; ++i) {
300 struct amdgpu_cs_fence *info = &ibs_request->dependencies[i];
301 struct drm_amdgpu_cs_chunk_dep *dep = &dependencies[i];
302 dep->ip_type = info->ip_type;
303 dep->ip_instance = info->ip_instance;
304 dep->ring = info->ring;
305 dep->ctx_id = info->context->id;
306 dep->handle = info->fence;
307 }
308
309 i = num_chunks++;
310
311 /* dependencies chunk */
312 chunks[i].chunk_id = AMDGPU_CHUNK_ID_DEPENDENCIES;
313 chunks[i].length_dw = sizeof(struct drm_amdgpu_cs_chunk_dep) / 4
314 * ibs_request->number_of_dependencies;
315 chunks[i].chunk_data = (uint64_t)(uintptr_t)dependencies;
316 }
317
318 sem_list = &context->sem_list[ibs_request->ip_type][ibs_request->ip_instance][ibs_request->ring];
319 LIST_FOR_EACH_ENTRY(sem, sem_list, list)
320 sem_count++;
321 if (sem_count) {
322 sem_dependencies = alloca(sizeof(struct drm_amdgpu_cs_chunk_dep) * sem_count);
323 if (!sem_dependencies) {
324 r = -ENOMEM;
325 goto error_unlock;
326 }
327 sem_count = 0;
328 LIST_FOR_EACH_ENTRY_SAFE(sem, tmp, sem_list, list) {
329 struct amdgpu_cs_fence *info = &sem->signal_fence;
330 struct drm_amdgpu_cs_chunk_dep *dep = &sem_dependencies[sem_count++];
331 dep->ip_type = info->ip_type;
332 dep->ip_instance = info->ip_instance;
333 dep->ring = info->ring;
334 dep->ctx_id = info->context->id;
335 dep->handle = info->fence;
336
337 list_del(&sem->list);
338 amdgpu_cs_reset_sem(sem);
339 amdgpu_cs_unreference_sem(sem);
340 }
341 i = num_chunks++;
342
343 /* dependencies chunk */
344 chunks[i].chunk_id = AMDGPU_CHUNK_ID_DEPENDENCIES;
345 chunks[i].length_dw = sizeof(struct drm_amdgpu_cs_chunk_dep) / 4 * sem_count;
346 chunks[i].chunk_data = (uint64_t)(uintptr_t)sem_dependencies;
347 }
348
349 r = amdgpu_cs_submit_raw2(dev, context, bo_list_handle, num_chunks,
350 chunks, &seq_no);
351 if (r)
352 goto error_unlock;
353
354 ibs_request->seq_no = seq_no;
355 context->last_seq[ibs_request->ip_type][ibs_request->ip_instance][ibs_request->ring] = ibs_request->seq_no;
356 error_unlock:
357 pthread_mutex_unlock(&context->sequence_mutex);
358 return r;
359 }
360
amdgpu_cs_submit(amdgpu_context_handle context,uint64_t flags,struct amdgpu_cs_request * ibs_request,uint32_t number_of_requests)361 drm_public int amdgpu_cs_submit(amdgpu_context_handle context,
362 uint64_t flags,
363 struct amdgpu_cs_request *ibs_request,
364 uint32_t number_of_requests)
365 {
366 uint32_t i;
367 int r;
368
369 if (!context || !ibs_request)
370 return -EINVAL;
371
372 r = 0;
373 for (i = 0; i < number_of_requests; i++) {
374 r = amdgpu_cs_submit_one(context, ibs_request);
375 if (r)
376 break;
377 ibs_request++;
378 }
379
380 return r;
381 }
382
383 /**
384 * Calculate absolute timeout.
385 *
386 * \param timeout - \c [in] timeout in nanoseconds.
387 *
388 * \return absolute timeout in nanoseconds
389 */
amdgpu_cs_calculate_timeout(uint64_t timeout)390 drm_private uint64_t amdgpu_cs_calculate_timeout(uint64_t timeout)
391 {
392 int r;
393
394 if (timeout != AMDGPU_TIMEOUT_INFINITE) {
395 struct timespec current;
396 uint64_t current_ns;
397 r = clock_gettime(CLOCK_MONOTONIC, ¤t);
398 if (r) {
399 fprintf(stderr, "clock_gettime() returned error (%d)!", errno);
400 return AMDGPU_TIMEOUT_INFINITE;
401 }
402
403 current_ns = ((uint64_t)current.tv_sec) * 1000000000ull;
404 current_ns += current.tv_nsec;
405 timeout += current_ns;
406 if (timeout < current_ns)
407 timeout = AMDGPU_TIMEOUT_INFINITE;
408 }
409 return timeout;
410 }
411
amdgpu_ioctl_wait_cs(amdgpu_context_handle context,unsigned ip,unsigned ip_instance,uint32_t ring,uint64_t handle,uint64_t timeout_ns,uint64_t flags,bool * busy)412 static int amdgpu_ioctl_wait_cs(amdgpu_context_handle context,
413 unsigned ip,
414 unsigned ip_instance,
415 uint32_t ring,
416 uint64_t handle,
417 uint64_t timeout_ns,
418 uint64_t flags,
419 bool *busy)
420 {
421 amdgpu_device_handle dev = context->dev;
422 union drm_amdgpu_wait_cs args;
423 int r;
424
425 memset(&args, 0, sizeof(args));
426 args.in.handle = handle;
427 args.in.ip_type = ip;
428 args.in.ip_instance = ip_instance;
429 args.in.ring = ring;
430 args.in.ctx_id = context->id;
431
432 if (flags & AMDGPU_QUERY_FENCE_TIMEOUT_IS_ABSOLUTE)
433 args.in.timeout = timeout_ns;
434 else
435 args.in.timeout = amdgpu_cs_calculate_timeout(timeout_ns);
436
437 r = drmIoctl(dev->fd, DRM_IOCTL_AMDGPU_WAIT_CS, &args);
438 if (r)
439 return -errno;
440
441 *busy = args.out.status;
442 return 0;
443 }
444
amdgpu_cs_query_fence_status(struct amdgpu_cs_fence * fence,uint64_t timeout_ns,uint64_t flags,uint32_t * expired)445 drm_public int amdgpu_cs_query_fence_status(struct amdgpu_cs_fence *fence,
446 uint64_t timeout_ns,
447 uint64_t flags,
448 uint32_t *expired)
449 {
450 bool busy = true;
451 int r;
452
453 if (!fence || !expired || !fence->context)
454 return -EINVAL;
455 if (fence->ip_type >= AMDGPU_HW_IP_NUM)
456 return -EINVAL;
457 if (fence->ring >= AMDGPU_CS_MAX_RINGS)
458 return -EINVAL;
459 if (fence->fence == AMDGPU_NULL_SUBMIT_SEQ) {
460 *expired = true;
461 return 0;
462 }
463
464 *expired = false;
465
466 r = amdgpu_ioctl_wait_cs(fence->context, fence->ip_type,
467 fence->ip_instance, fence->ring,
468 fence->fence, timeout_ns, flags, &busy);
469
470 if (!r && !busy)
471 *expired = true;
472
473 return r;
474 }
475
amdgpu_ioctl_wait_fences(struct amdgpu_cs_fence * fences,uint32_t fence_count,bool wait_all,uint64_t timeout_ns,uint32_t * status,uint32_t * first)476 static int amdgpu_ioctl_wait_fences(struct amdgpu_cs_fence *fences,
477 uint32_t fence_count,
478 bool wait_all,
479 uint64_t timeout_ns,
480 uint32_t *status,
481 uint32_t *first)
482 {
483 struct drm_amdgpu_fence *drm_fences;
484 amdgpu_device_handle dev = fences[0].context->dev;
485 union drm_amdgpu_wait_fences args;
486 int r;
487 uint32_t i;
488
489 drm_fences = alloca(sizeof(struct drm_amdgpu_fence) * fence_count);
490 for (i = 0; i < fence_count; i++) {
491 drm_fences[i].ctx_id = fences[i].context->id;
492 drm_fences[i].ip_type = fences[i].ip_type;
493 drm_fences[i].ip_instance = fences[i].ip_instance;
494 drm_fences[i].ring = fences[i].ring;
495 drm_fences[i].seq_no = fences[i].fence;
496 }
497
498 memset(&args, 0, sizeof(args));
499 args.in.fences = (uint64_t)(uintptr_t)drm_fences;
500 args.in.fence_count = fence_count;
501 args.in.wait_all = wait_all;
502 args.in.timeout_ns = amdgpu_cs_calculate_timeout(timeout_ns);
503
504 r = drmIoctl(dev->fd, DRM_IOCTL_AMDGPU_WAIT_FENCES, &args);
505 if (r)
506 return -errno;
507
508 *status = args.out.status;
509
510 if (first)
511 *first = args.out.first_signaled;
512
513 return 0;
514 }
515
amdgpu_cs_wait_fences(struct amdgpu_cs_fence * fences,uint32_t fence_count,bool wait_all,uint64_t timeout_ns,uint32_t * status,uint32_t * first)516 drm_public int amdgpu_cs_wait_fences(struct amdgpu_cs_fence *fences,
517 uint32_t fence_count,
518 bool wait_all,
519 uint64_t timeout_ns,
520 uint32_t *status,
521 uint32_t *first)
522 {
523 uint32_t i;
524
525 /* Sanity check */
526 if (!fences || !status || !fence_count)
527 return -EINVAL;
528
529 for (i = 0; i < fence_count; i++) {
530 if (NULL == fences[i].context)
531 return -EINVAL;
532 if (fences[i].ip_type >= AMDGPU_HW_IP_NUM)
533 return -EINVAL;
534 if (fences[i].ring >= AMDGPU_CS_MAX_RINGS)
535 return -EINVAL;
536 }
537
538 *status = 0;
539
540 return amdgpu_ioctl_wait_fences(fences, fence_count, wait_all,
541 timeout_ns, status, first);
542 }
543
amdgpu_cs_create_semaphore(amdgpu_semaphore_handle * sem)544 drm_public int amdgpu_cs_create_semaphore(amdgpu_semaphore_handle *sem)
545 {
546 struct amdgpu_semaphore *gpu_semaphore;
547
548 if (!sem)
549 return -EINVAL;
550
551 gpu_semaphore = calloc(1, sizeof(struct amdgpu_semaphore));
552 if (!gpu_semaphore)
553 return -ENOMEM;
554
555 atomic_set(&gpu_semaphore->refcount, 1);
556 *sem = gpu_semaphore;
557
558 return 0;
559 }
560
amdgpu_cs_signal_semaphore(amdgpu_context_handle ctx,uint32_t ip_type,uint32_t ip_instance,uint32_t ring,amdgpu_semaphore_handle sem)561 drm_public int amdgpu_cs_signal_semaphore(amdgpu_context_handle ctx,
562 uint32_t ip_type,
563 uint32_t ip_instance,
564 uint32_t ring,
565 amdgpu_semaphore_handle sem)
566 {
567 if (!ctx || !sem)
568 return -EINVAL;
569 if (ip_type >= AMDGPU_HW_IP_NUM)
570 return -EINVAL;
571 if (ring >= AMDGPU_CS_MAX_RINGS)
572 return -EINVAL;
573 /* sem has been signaled */
574 if (sem->signal_fence.context)
575 return -EINVAL;
576 pthread_mutex_lock(&ctx->sequence_mutex);
577 sem->signal_fence.context = ctx;
578 sem->signal_fence.ip_type = ip_type;
579 sem->signal_fence.ip_instance = ip_instance;
580 sem->signal_fence.ring = ring;
581 sem->signal_fence.fence = ctx->last_seq[ip_type][ip_instance][ring];
582 update_references(NULL, &sem->refcount);
583 pthread_mutex_unlock(&ctx->sequence_mutex);
584 return 0;
585 }
586
amdgpu_cs_wait_semaphore(amdgpu_context_handle ctx,uint32_t ip_type,uint32_t ip_instance,uint32_t ring,amdgpu_semaphore_handle sem)587 drm_public int amdgpu_cs_wait_semaphore(amdgpu_context_handle ctx,
588 uint32_t ip_type,
589 uint32_t ip_instance,
590 uint32_t ring,
591 amdgpu_semaphore_handle sem)
592 {
593 if (!ctx || !sem)
594 return -EINVAL;
595 if (ip_type >= AMDGPU_HW_IP_NUM)
596 return -EINVAL;
597 if (ring >= AMDGPU_CS_MAX_RINGS)
598 return -EINVAL;
599 /* must signal first */
600 if (!sem->signal_fence.context)
601 return -EINVAL;
602
603 pthread_mutex_lock(&ctx->sequence_mutex);
604 list_add(&sem->list, &ctx->sem_list[ip_type][ip_instance][ring]);
605 pthread_mutex_unlock(&ctx->sequence_mutex);
606 return 0;
607 }
608
amdgpu_cs_reset_sem(amdgpu_semaphore_handle sem)609 static int amdgpu_cs_reset_sem(amdgpu_semaphore_handle sem)
610 {
611 if (!sem || !sem->signal_fence.context)
612 return -EINVAL;
613
614 sem->signal_fence.context = NULL;
615 sem->signal_fence.ip_type = 0;
616 sem->signal_fence.ip_instance = 0;
617 sem->signal_fence.ring = 0;
618 sem->signal_fence.fence = 0;
619
620 return 0;
621 }
622
amdgpu_cs_unreference_sem(amdgpu_semaphore_handle sem)623 static int amdgpu_cs_unreference_sem(amdgpu_semaphore_handle sem)
624 {
625 if (!sem)
626 return -EINVAL;
627
628 if (update_references(&sem->refcount, NULL))
629 free(sem);
630 return 0;
631 }
632
amdgpu_cs_destroy_semaphore(amdgpu_semaphore_handle sem)633 drm_public int amdgpu_cs_destroy_semaphore(amdgpu_semaphore_handle sem)
634 {
635 return amdgpu_cs_unreference_sem(sem);
636 }
637
amdgpu_cs_create_syncobj2(amdgpu_device_handle dev,uint32_t flags,uint32_t * handle)638 drm_public int amdgpu_cs_create_syncobj2(amdgpu_device_handle dev,
639 uint32_t flags,
640 uint32_t *handle)
641 {
642 if (NULL == dev)
643 return -EINVAL;
644
645 return drmSyncobjCreate(dev->fd, flags, handle);
646 }
647
amdgpu_cs_create_syncobj(amdgpu_device_handle dev,uint32_t * handle)648 drm_public int amdgpu_cs_create_syncobj(amdgpu_device_handle dev,
649 uint32_t *handle)
650 {
651 if (NULL == dev)
652 return -EINVAL;
653
654 return drmSyncobjCreate(dev->fd, 0, handle);
655 }
656
amdgpu_cs_destroy_syncobj(amdgpu_device_handle dev,uint32_t handle)657 drm_public int amdgpu_cs_destroy_syncobj(amdgpu_device_handle dev,
658 uint32_t handle)
659 {
660 if (NULL == dev)
661 return -EINVAL;
662
663 return drmSyncobjDestroy(dev->fd, handle);
664 }
665
amdgpu_cs_syncobj_reset(amdgpu_device_handle dev,const uint32_t * syncobjs,uint32_t syncobj_count)666 drm_public int amdgpu_cs_syncobj_reset(amdgpu_device_handle dev,
667 const uint32_t *syncobjs,
668 uint32_t syncobj_count)
669 {
670 if (NULL == dev)
671 return -EINVAL;
672
673 return drmSyncobjReset(dev->fd, syncobjs, syncobj_count);
674 }
675
amdgpu_cs_syncobj_signal(amdgpu_device_handle dev,const uint32_t * syncobjs,uint32_t syncobj_count)676 drm_public int amdgpu_cs_syncobj_signal(amdgpu_device_handle dev,
677 const uint32_t *syncobjs,
678 uint32_t syncobj_count)
679 {
680 if (NULL == dev)
681 return -EINVAL;
682
683 return drmSyncobjSignal(dev->fd, syncobjs, syncobj_count);
684 }
685
amdgpu_cs_syncobj_timeline_signal(amdgpu_device_handle dev,const uint32_t * syncobjs,uint64_t * points,uint32_t syncobj_count)686 drm_public int amdgpu_cs_syncobj_timeline_signal(amdgpu_device_handle dev,
687 const uint32_t *syncobjs,
688 uint64_t *points,
689 uint32_t syncobj_count)
690 {
691 if (NULL == dev)
692 return -EINVAL;
693
694 return drmSyncobjTimelineSignal(dev->fd, syncobjs,
695 points, syncobj_count);
696 }
697
amdgpu_cs_syncobj_wait(amdgpu_device_handle dev,uint32_t * handles,unsigned num_handles,int64_t timeout_nsec,unsigned flags,uint32_t * first_signaled)698 drm_public int amdgpu_cs_syncobj_wait(amdgpu_device_handle dev,
699 uint32_t *handles, unsigned num_handles,
700 int64_t timeout_nsec, unsigned flags,
701 uint32_t *first_signaled)
702 {
703 if (NULL == dev)
704 return -EINVAL;
705
706 return drmSyncobjWait(dev->fd, handles, num_handles, timeout_nsec,
707 flags, first_signaled);
708 }
709
amdgpu_cs_syncobj_timeline_wait(amdgpu_device_handle dev,uint32_t * handles,uint64_t * points,unsigned num_handles,int64_t timeout_nsec,unsigned flags,uint32_t * first_signaled)710 drm_public int amdgpu_cs_syncobj_timeline_wait(amdgpu_device_handle dev,
711 uint32_t *handles, uint64_t *points,
712 unsigned num_handles,
713 int64_t timeout_nsec, unsigned flags,
714 uint32_t *first_signaled)
715 {
716 if (NULL == dev)
717 return -EINVAL;
718
719 return drmSyncobjTimelineWait(dev->fd, handles, points, num_handles,
720 timeout_nsec, flags, first_signaled);
721 }
722
amdgpu_cs_syncobj_query(amdgpu_device_handle dev,uint32_t * handles,uint64_t * points,unsigned num_handles)723 drm_public int amdgpu_cs_syncobj_query(amdgpu_device_handle dev,
724 uint32_t *handles, uint64_t *points,
725 unsigned num_handles)
726 {
727 if (NULL == dev)
728 return -EINVAL;
729
730 return drmSyncobjQuery(dev->fd, handles, points, num_handles);
731 }
732
amdgpu_cs_syncobj_query2(amdgpu_device_handle dev,uint32_t * handles,uint64_t * points,unsigned num_handles,uint32_t flags)733 drm_public int amdgpu_cs_syncobj_query2(amdgpu_device_handle dev,
734 uint32_t *handles, uint64_t *points,
735 unsigned num_handles, uint32_t flags)
736 {
737 if (!dev)
738 return -EINVAL;
739
740 return drmSyncobjQuery2(dev->fd, handles, points, num_handles, flags);
741 }
742
amdgpu_cs_export_syncobj(amdgpu_device_handle dev,uint32_t handle,int * shared_fd)743 drm_public int amdgpu_cs_export_syncobj(amdgpu_device_handle dev,
744 uint32_t handle,
745 int *shared_fd)
746 {
747 if (NULL == dev)
748 return -EINVAL;
749
750 return drmSyncobjHandleToFD(dev->fd, handle, shared_fd);
751 }
752
amdgpu_cs_import_syncobj(amdgpu_device_handle dev,int shared_fd,uint32_t * handle)753 drm_public int amdgpu_cs_import_syncobj(amdgpu_device_handle dev,
754 int shared_fd,
755 uint32_t *handle)
756 {
757 if (NULL == dev)
758 return -EINVAL;
759
760 return drmSyncobjFDToHandle(dev->fd, shared_fd, handle);
761 }
762
amdgpu_cs_syncobj_export_sync_file(amdgpu_device_handle dev,uint32_t syncobj,int * sync_file_fd)763 drm_public int amdgpu_cs_syncobj_export_sync_file(amdgpu_device_handle dev,
764 uint32_t syncobj,
765 int *sync_file_fd)
766 {
767 if (NULL == dev)
768 return -EINVAL;
769
770 return drmSyncobjExportSyncFile(dev->fd, syncobj, sync_file_fd);
771 }
772
amdgpu_cs_syncobj_import_sync_file(amdgpu_device_handle dev,uint32_t syncobj,int sync_file_fd)773 drm_public int amdgpu_cs_syncobj_import_sync_file(amdgpu_device_handle dev,
774 uint32_t syncobj,
775 int sync_file_fd)
776 {
777 if (NULL == dev)
778 return -EINVAL;
779
780 return drmSyncobjImportSyncFile(dev->fd, syncobj, sync_file_fd);
781 }
782
amdgpu_cs_syncobj_export_sync_file2(amdgpu_device_handle dev,uint32_t syncobj,uint64_t point,uint32_t flags,int * sync_file_fd)783 drm_public int amdgpu_cs_syncobj_export_sync_file2(amdgpu_device_handle dev,
784 uint32_t syncobj,
785 uint64_t point,
786 uint32_t flags,
787 int *sync_file_fd)
788 {
789 uint32_t binary_handle;
790 int ret;
791
792 if (NULL == dev)
793 return -EINVAL;
794
795 if (!point)
796 return drmSyncobjExportSyncFile(dev->fd, syncobj, sync_file_fd);
797
798 ret = drmSyncobjCreate(dev->fd, 0, &binary_handle);
799 if (ret)
800 return ret;
801
802 ret = drmSyncobjTransfer(dev->fd, binary_handle, 0,
803 syncobj, point, flags);
804 if (ret)
805 goto out;
806 ret = drmSyncobjExportSyncFile(dev->fd, binary_handle, sync_file_fd);
807 out:
808 drmSyncobjDestroy(dev->fd, binary_handle);
809 return ret;
810 }
811
amdgpu_cs_syncobj_import_sync_file2(amdgpu_device_handle dev,uint32_t syncobj,uint64_t point,int sync_file_fd)812 drm_public int amdgpu_cs_syncobj_import_sync_file2(amdgpu_device_handle dev,
813 uint32_t syncobj,
814 uint64_t point,
815 int sync_file_fd)
816 {
817 uint32_t binary_handle;
818 int ret;
819
820 if (NULL == dev)
821 return -EINVAL;
822
823 if (!point)
824 return drmSyncobjImportSyncFile(dev->fd, syncobj, sync_file_fd);
825
826 ret = drmSyncobjCreate(dev->fd, 0, &binary_handle);
827 if (ret)
828 return ret;
829 ret = drmSyncobjImportSyncFile(dev->fd, binary_handle, sync_file_fd);
830 if (ret)
831 goto out;
832 ret = drmSyncobjTransfer(dev->fd, syncobj, point,
833 binary_handle, 0, 0);
834 out:
835 drmSyncobjDestroy(dev->fd, binary_handle);
836 return ret;
837 }
838
amdgpu_cs_syncobj_transfer(amdgpu_device_handle dev,uint32_t dst_handle,uint64_t dst_point,uint32_t src_handle,uint64_t src_point,uint32_t flags)839 drm_public int amdgpu_cs_syncobj_transfer(amdgpu_device_handle dev,
840 uint32_t dst_handle,
841 uint64_t dst_point,
842 uint32_t src_handle,
843 uint64_t src_point,
844 uint32_t flags)
845 {
846 if (NULL == dev)
847 return -EINVAL;
848
849 return drmSyncobjTransfer(dev->fd,
850 dst_handle, dst_point,
851 src_handle, src_point,
852 flags);
853 }
854
amdgpu_cs_submit_raw(amdgpu_device_handle dev,amdgpu_context_handle context,amdgpu_bo_list_handle bo_list_handle,int num_chunks,struct drm_amdgpu_cs_chunk * chunks,uint64_t * seq_no)855 drm_public int amdgpu_cs_submit_raw(amdgpu_device_handle dev,
856 amdgpu_context_handle context,
857 amdgpu_bo_list_handle bo_list_handle,
858 int num_chunks,
859 struct drm_amdgpu_cs_chunk *chunks,
860 uint64_t *seq_no)
861 {
862 union drm_amdgpu_cs cs;
863 uint64_t *chunk_array;
864 int i, r;
865 if (num_chunks == 0)
866 return -EINVAL;
867
868 memset(&cs, 0, sizeof(cs));
869 chunk_array = alloca(sizeof(uint64_t) * num_chunks);
870 for (i = 0; i < num_chunks; i++)
871 chunk_array[i] = (uint64_t)(uintptr_t)&chunks[i];
872 cs.in.chunks = (uint64_t)(uintptr_t)chunk_array;
873 cs.in.ctx_id = context->id;
874 cs.in.bo_list_handle = bo_list_handle ? bo_list_handle->handle : 0;
875 cs.in.num_chunks = num_chunks;
876 r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_CS,
877 &cs, sizeof(cs));
878 if (r)
879 return r;
880
881 if (seq_no)
882 *seq_no = cs.out.handle;
883 return 0;
884 }
885
amdgpu_cs_submit_raw2(amdgpu_device_handle dev,amdgpu_context_handle context,uint32_t bo_list_handle,int num_chunks,struct drm_amdgpu_cs_chunk * chunks,uint64_t * seq_no)886 drm_public int amdgpu_cs_submit_raw2(amdgpu_device_handle dev,
887 amdgpu_context_handle context,
888 uint32_t bo_list_handle,
889 int num_chunks,
890 struct drm_amdgpu_cs_chunk *chunks,
891 uint64_t *seq_no)
892 {
893 union drm_amdgpu_cs cs;
894 uint64_t *chunk_array;
895 int i, r;
896
897 memset(&cs, 0, sizeof(cs));
898 chunk_array = alloca(sizeof(uint64_t) * num_chunks);
899 for (i = 0; i < num_chunks; i++)
900 chunk_array[i] = (uint64_t)(uintptr_t)&chunks[i];
901 cs.in.chunks = (uint64_t)(uintptr_t)chunk_array;
902 cs.in.ctx_id = context->id;
903 cs.in.bo_list_handle = bo_list_handle;
904 cs.in.num_chunks = num_chunks;
905 r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_CS,
906 &cs, sizeof(cs));
907 if (!r && seq_no)
908 *seq_no = cs.out.handle;
909 return r;
910 }
911
amdgpu_cs_chunk_fence_info_to_data(struct amdgpu_cs_fence_info * fence_info,struct drm_amdgpu_cs_chunk_data * data)912 drm_public void amdgpu_cs_chunk_fence_info_to_data(struct amdgpu_cs_fence_info *fence_info,
913 struct drm_amdgpu_cs_chunk_data *data)
914 {
915 data->fence_data.handle = fence_info->handle->handle;
916 data->fence_data.offset = fence_info->offset * sizeof(uint64_t);
917 }
918
amdgpu_cs_chunk_fence_to_dep(struct amdgpu_cs_fence * fence,struct drm_amdgpu_cs_chunk_dep * dep)919 drm_public void amdgpu_cs_chunk_fence_to_dep(struct amdgpu_cs_fence *fence,
920 struct drm_amdgpu_cs_chunk_dep *dep)
921 {
922 dep->ip_type = fence->ip_type;
923 dep->ip_instance = fence->ip_instance;
924 dep->ring = fence->ring;
925 dep->ctx_id = fence->context->id;
926 dep->handle = fence->fence;
927 }
928
amdgpu_cs_fence_to_handle(amdgpu_device_handle dev,struct amdgpu_cs_fence * fence,uint32_t what,uint32_t * out_handle)929 drm_public int amdgpu_cs_fence_to_handle(amdgpu_device_handle dev,
930 struct amdgpu_cs_fence *fence,
931 uint32_t what,
932 uint32_t *out_handle)
933 {
934 union drm_amdgpu_fence_to_handle fth;
935 int r;
936
937 memset(&fth, 0, sizeof(fth));
938 fth.in.fence.ctx_id = fence->context->id;
939 fth.in.fence.ip_type = fence->ip_type;
940 fth.in.fence.ip_instance = fence->ip_instance;
941 fth.in.fence.ring = fence->ring;
942 fth.in.fence.seq_no = fence->fence;
943 fth.in.what = what;
944
945 r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_FENCE_TO_HANDLE,
946 &fth, sizeof(fth));
947 if (r == 0)
948 *out_handle = fth.out.handle;
949 return r;
950 }
951