2 * Copyright 2014 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
34 #include <sys/ioctl.h>
40 #include "amdgpu_drm.h"
41 #include "amdgpu_internal.h"
43 static int amdgpu_cs_unreference_sem(amdgpu_semaphore_handle sem);
44 static int amdgpu_cs_reset_sem(amdgpu_semaphore_handle sem);
47 * Create command submission context
49 * \param dev - \c [in] Device handle. See #amdgpu_device_initialize()
50 * \param priority - \c [in] Context creation flags. See AMDGPU_CTX_PRIORITY_*
51 * \param context - \c [out] GPU Context handle
53 * \return 0 on success otherwise POSIX Error code
55 int amdgpu_cs_ctx_create2(amdgpu_device_handle dev, uint32_t priority,
56 amdgpu_context_handle *context)
58 struct amdgpu_context *gpu_context;
59 union drm_amdgpu_ctx args;
66 gpu_context = calloc(1, sizeof(struct amdgpu_context));
70 gpu_context->dev = dev;
72 r = pthread_mutex_init(&gpu_context->sequence_mutex, NULL);
76 /* Create the context */
77 memset(&args, 0, sizeof(args));
78 args.in.op = AMDGPU_CTX_OP_ALLOC_CTX;
79 args.in.priority = priority;
81 r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_CTX, &args, sizeof(args));
85 gpu_context->id = args.out.alloc.ctx_id;
86 for (i = 0; i < AMDGPU_HW_IP_NUM; i++)
87 for (j = 0; j < AMDGPU_HW_IP_INSTANCE_MAX_COUNT; j++)
88 for (k = 0; k < AMDGPU_CS_MAX_RINGS; k++)
89 list_inithead(&gpu_context->sem_list[i][j][k]);
90 *context = (amdgpu_context_handle)gpu_context;
95 pthread_mutex_destroy(&gpu_context->sequence_mutex);
100 int amdgpu_cs_ctx_create(amdgpu_device_handle dev,
101 amdgpu_context_handle *context)
103 return amdgpu_cs_ctx_create2(dev, AMDGPU_CTX_PRIORITY_NORMAL, context);
107 * Release command submission context
109 * \param dev - \c [in] amdgpu device handle
110 * \param context - \c [in] amdgpu context handle
112 * \return 0 on success otherwise POSIX Error code
114 int amdgpu_cs_ctx_free(amdgpu_context_handle context)
116 union drm_amdgpu_ctx args;
123 pthread_mutex_destroy(&context->sequence_mutex);
125 /* now deal with kernel side */
126 memset(&args, 0, sizeof(args));
127 args.in.op = AMDGPU_CTX_OP_FREE_CTX;
128 args.in.ctx_id = context->id;
129 r = drmCommandWriteRead(context->dev->fd, DRM_AMDGPU_CTX,
130 &args, sizeof(args));
131 for (i = 0; i < AMDGPU_HW_IP_NUM; i++) {
132 for (j = 0; j < AMDGPU_HW_IP_INSTANCE_MAX_COUNT; j++) {
133 for (k = 0; k < AMDGPU_CS_MAX_RINGS; k++) {
134 amdgpu_semaphore_handle sem;
135 LIST_FOR_EACH_ENTRY(sem, &context->sem_list[i][j][k], list) {
136 list_del(&sem->list);
137 amdgpu_cs_reset_sem(sem);
138 amdgpu_cs_unreference_sem(sem);
148 int amdgpu_cs_query_reset_state(amdgpu_context_handle context,
149 uint32_t *state, uint32_t *hangs)
151 union drm_amdgpu_ctx args;
157 memset(&args, 0, sizeof(args));
158 args.in.op = AMDGPU_CTX_OP_QUERY_STATE;
159 args.in.ctx_id = context->id;
160 r = drmCommandWriteRead(context->dev->fd, DRM_AMDGPU_CTX,
161 &args, sizeof(args));
163 *state = args.out.state.reset_status;
164 *hangs = args.out.state.hangs;
170 * Submit command to kernel DRM
171 * \param dev - \c [in] Device handle
172 * \param context - \c [in] GPU Context
173 * \param ibs_request - \c [in] Pointer to submission requests
174 * \param fence - \c [out] return fence for this submission
176 * \return 0 on success otherwise POSIX Error code
177 * \sa amdgpu_cs_submit()
179 static int amdgpu_cs_submit_one(amdgpu_context_handle context,
180 struct amdgpu_cs_request *ibs_request)
182 union drm_amdgpu_cs cs;
183 uint64_t *chunk_array;
184 struct drm_amdgpu_cs_chunk *chunks;
185 struct drm_amdgpu_cs_chunk_data *chunk_data;
186 struct drm_amdgpu_cs_chunk_dep *dependencies = NULL;
187 struct drm_amdgpu_cs_chunk_dep *sem_dependencies = NULL;
188 struct list_head *sem_list;
189 amdgpu_semaphore_handle sem, tmp;
190 uint32_t i, size, sem_count = 0;
194 if (ibs_request->ip_type >= AMDGPU_HW_IP_NUM)
196 if (ibs_request->ring >= AMDGPU_CS_MAX_RINGS)
198 if (ibs_request->number_of_ibs > AMDGPU_CS_MAX_IBS_PER_SUBMIT)
200 if (ibs_request->number_of_ibs == 0) {
201 ibs_request->seq_no = AMDGPU_NULL_SUBMIT_SEQ;
204 user_fence = (ibs_request->fence_info.handle != NULL);
206 size = ibs_request->number_of_ibs + (user_fence ? 2 : 1) + 1;
208 chunk_array = alloca(sizeof(uint64_t) * size);
209 chunks = alloca(sizeof(struct drm_amdgpu_cs_chunk) * size);
211 size = ibs_request->number_of_ibs + (user_fence ? 1 : 0);
213 chunk_data = alloca(sizeof(struct drm_amdgpu_cs_chunk_data) * size);
215 memset(&cs, 0, sizeof(cs));
216 cs.in.chunks = (uint64_t)(uintptr_t)chunk_array;
217 cs.in.ctx_id = context->id;
218 if (ibs_request->resources)
219 cs.in.bo_list_handle = ibs_request->resources->handle;
220 cs.in.num_chunks = ibs_request->number_of_ibs;
222 for (i = 0; i < ibs_request->number_of_ibs; i++) {
223 struct amdgpu_cs_ib_info *ib;
224 chunk_array[i] = (uint64_t)(uintptr_t)&chunks[i];
225 chunks[i].chunk_id = AMDGPU_CHUNK_ID_IB;
226 chunks[i].length_dw = sizeof(struct drm_amdgpu_cs_chunk_ib) / 4;
227 chunks[i].chunk_data = (uint64_t)(uintptr_t)&chunk_data[i];
229 ib = &ibs_request->ibs[i];
231 chunk_data[i].ib_data._pad = 0;
232 chunk_data[i].ib_data.va_start = ib->ib_mc_address;
233 chunk_data[i].ib_data.ib_bytes = ib->size * 4;
234 chunk_data[i].ib_data.ip_type = ibs_request->ip_type;
235 chunk_data[i].ib_data.ip_instance = ibs_request->ip_instance;
236 chunk_data[i].ib_data.ring = ibs_request->ring;
237 chunk_data[i].ib_data.flags = ib->flags;
240 pthread_mutex_lock(&context->sequence_mutex);
243 i = cs.in.num_chunks++;
246 chunk_array[i] = (uint64_t)(uintptr_t)&chunks[i];
247 chunks[i].chunk_id = AMDGPU_CHUNK_ID_FENCE;
248 chunks[i].length_dw = sizeof(struct drm_amdgpu_cs_chunk_fence) / 4;
249 chunks[i].chunk_data = (uint64_t)(uintptr_t)&chunk_data[i];
251 /* fence bo handle */
252 chunk_data[i].fence_data.handle = ibs_request->fence_info.handle->handle;
254 chunk_data[i].fence_data.offset =
255 ibs_request->fence_info.offset * sizeof(uint64_t);
258 if (ibs_request->number_of_dependencies) {
259 dependencies = malloc(sizeof(struct drm_amdgpu_cs_chunk_dep) *
260 ibs_request->number_of_dependencies);
266 for (i = 0; i < ibs_request->number_of_dependencies; ++i) {
267 struct amdgpu_cs_fence *info = &ibs_request->dependencies[i];
268 struct drm_amdgpu_cs_chunk_dep *dep = &dependencies[i];
269 dep->ip_type = info->ip_type;
270 dep->ip_instance = info->ip_instance;
271 dep->ring = info->ring;
272 dep->ctx_id = info->context->id;
273 dep->handle = info->fence;
276 i = cs.in.num_chunks++;
278 /* dependencies chunk */
279 chunk_array[i] = (uint64_t)(uintptr_t)&chunks[i];
280 chunks[i].chunk_id = AMDGPU_CHUNK_ID_DEPENDENCIES;
281 chunks[i].length_dw = sizeof(struct drm_amdgpu_cs_chunk_dep) / 4
282 * ibs_request->number_of_dependencies;
283 chunks[i].chunk_data = (uint64_t)(uintptr_t)dependencies;
286 sem_list = &context->sem_list[ibs_request->ip_type][ibs_request->ip_instance][ibs_request->ring];
287 LIST_FOR_EACH_ENTRY(sem, sem_list, list)
290 sem_dependencies = malloc(sizeof(struct drm_amdgpu_cs_chunk_dep) * sem_count);
291 if (!sem_dependencies) {
296 LIST_FOR_EACH_ENTRY_SAFE(sem, tmp, sem_list, list) {
297 struct amdgpu_cs_fence *info = &sem->signal_fence;
298 struct drm_amdgpu_cs_chunk_dep *dep = &sem_dependencies[sem_count++];
299 dep->ip_type = info->ip_type;
300 dep->ip_instance = info->ip_instance;
301 dep->ring = info->ring;
302 dep->ctx_id = info->context->id;
303 dep->handle = info->fence;
305 list_del(&sem->list);
306 amdgpu_cs_reset_sem(sem);
307 amdgpu_cs_unreference_sem(sem);
309 i = cs.in.num_chunks++;
311 /* dependencies chunk */
312 chunk_array[i] = (uint64_t)(uintptr_t)&chunks[i];
313 chunks[i].chunk_id = AMDGPU_CHUNK_ID_DEPENDENCIES;
314 chunks[i].length_dw = sizeof(struct drm_amdgpu_cs_chunk_dep) / 4 * sem_count;
315 chunks[i].chunk_data = (uint64_t)(uintptr_t)sem_dependencies;
318 r = drmCommandWriteRead(context->dev->fd, DRM_AMDGPU_CS,
323 ibs_request->seq_no = cs.out.handle;
324 context->last_seq[ibs_request->ip_type][ibs_request->ip_instance][ibs_request->ring] = ibs_request->seq_no;
326 pthread_mutex_unlock(&context->sequence_mutex);
328 free(sem_dependencies);
332 int amdgpu_cs_submit(amdgpu_context_handle context,
334 struct amdgpu_cs_request *ibs_request,
335 uint32_t number_of_requests)
340 if (!context || !ibs_request)
344 for (i = 0; i < number_of_requests; i++) {
345 r = amdgpu_cs_submit_one(context, ibs_request);
355 * Calculate absolute timeout.
357 * \param timeout - \c [in] timeout in nanoseconds.
359 * \return absolute timeout in nanoseconds
361 drm_private uint64_t amdgpu_cs_calculate_timeout(uint64_t timeout)
365 if (timeout != AMDGPU_TIMEOUT_INFINITE) {
366 struct timespec current;
368 r = clock_gettime(CLOCK_MONOTONIC, ¤t);
370 fprintf(stderr, "clock_gettime() returned error (%d)!", errno);
371 return AMDGPU_TIMEOUT_INFINITE;
374 current_ns = ((uint64_t)current.tv_sec) * 1000000000ull;
375 current_ns += current.tv_nsec;
376 timeout += current_ns;
377 if (timeout < current_ns)
378 timeout = AMDGPU_TIMEOUT_INFINITE;
383 static int amdgpu_ioctl_wait_cs(amdgpu_context_handle context,
385 unsigned ip_instance,
392 amdgpu_device_handle dev = context->dev;
393 union drm_amdgpu_wait_cs args;
396 memset(&args, 0, sizeof(args));
397 args.in.handle = handle;
398 args.in.ip_type = ip;
399 args.in.ip_instance = ip_instance;
401 args.in.ctx_id = context->id;
403 if (flags & AMDGPU_QUERY_FENCE_TIMEOUT_IS_ABSOLUTE)
404 args.in.timeout = timeout_ns;
406 args.in.timeout = amdgpu_cs_calculate_timeout(timeout_ns);
408 r = drmIoctl(dev->fd, DRM_IOCTL_AMDGPU_WAIT_CS, &args);
412 *busy = args.out.status;
416 int amdgpu_cs_query_fence_status(struct amdgpu_cs_fence *fence,
424 if (!fence || !expired || !fence->context)
426 if (fence->ip_type >= AMDGPU_HW_IP_NUM)
428 if (fence->ring >= AMDGPU_CS_MAX_RINGS)
430 if (fence->fence == AMDGPU_NULL_SUBMIT_SEQ) {
437 r = amdgpu_ioctl_wait_cs(fence->context, fence->ip_type,
438 fence->ip_instance, fence->ring,
439 fence->fence, timeout_ns, flags, &busy);
447 static int amdgpu_ioctl_wait_fences(struct amdgpu_cs_fence *fences,
448 uint32_t fence_count,
454 struct drm_amdgpu_fence *drm_fences;
455 amdgpu_device_handle dev = fences[0].context->dev;
456 union drm_amdgpu_wait_fences args;
460 drm_fences = alloca(sizeof(struct drm_amdgpu_fence) * fence_count);
461 for (i = 0; i < fence_count; i++) {
462 drm_fences[i].ctx_id = fences[i].context->id;
463 drm_fences[i].ip_type = fences[i].ip_type;
464 drm_fences[i].ip_instance = fences[i].ip_instance;
465 drm_fences[i].ring = fences[i].ring;
466 drm_fences[i].seq_no = fences[i].fence;
469 memset(&args, 0, sizeof(args));
470 args.in.fences = (uint64_t)(uintptr_t)drm_fences;
471 args.in.fence_count = fence_count;
472 args.in.wait_all = wait_all;
473 args.in.timeout_ns = amdgpu_cs_calculate_timeout(timeout_ns);
475 r = drmIoctl(dev->fd, DRM_IOCTL_AMDGPU_WAIT_FENCES, &args);
479 *status = args.out.status;
482 *first = args.out.first_signaled;
487 int amdgpu_cs_wait_fences(struct amdgpu_cs_fence *fences,
488 uint32_t fence_count,
497 if (!fences || !status || !fence_count)
500 for (i = 0; i < fence_count; i++) {
501 if (NULL == fences[i].context)
503 if (fences[i].ip_type >= AMDGPU_HW_IP_NUM)
505 if (fences[i].ring >= AMDGPU_CS_MAX_RINGS)
511 return amdgpu_ioctl_wait_fences(fences, fence_count, wait_all,
512 timeout_ns, status, first);
515 int amdgpu_cs_create_semaphore(amdgpu_semaphore_handle *sem)
517 struct amdgpu_semaphore *gpu_semaphore;
522 gpu_semaphore = calloc(1, sizeof(struct amdgpu_semaphore));
526 atomic_set(&gpu_semaphore->refcount, 1);
527 *sem = gpu_semaphore;
532 int amdgpu_cs_signal_semaphore(amdgpu_context_handle ctx,
534 uint32_t ip_instance,
536 amdgpu_semaphore_handle sem)
540 if (ip_type >= AMDGPU_HW_IP_NUM)
542 if (ring >= AMDGPU_CS_MAX_RINGS)
544 /* sem has been signaled */
545 if (sem->signal_fence.context)
547 pthread_mutex_lock(&ctx->sequence_mutex);
548 sem->signal_fence.context = ctx;
549 sem->signal_fence.ip_type = ip_type;
550 sem->signal_fence.ip_instance = ip_instance;
551 sem->signal_fence.ring = ring;
552 sem->signal_fence.fence = ctx->last_seq[ip_type][ip_instance][ring];
553 update_references(NULL, &sem->refcount);
554 pthread_mutex_unlock(&ctx->sequence_mutex);
558 int amdgpu_cs_wait_semaphore(amdgpu_context_handle ctx,
560 uint32_t ip_instance,
562 amdgpu_semaphore_handle sem)
566 if (ip_type >= AMDGPU_HW_IP_NUM)
568 if (ring >= AMDGPU_CS_MAX_RINGS)
570 /* must signal first */
571 if (!sem->signal_fence.context)
574 pthread_mutex_lock(&ctx->sequence_mutex);
575 list_add(&sem->list, &ctx->sem_list[ip_type][ip_instance][ring]);
576 pthread_mutex_unlock(&ctx->sequence_mutex);
580 static int amdgpu_cs_reset_sem(amdgpu_semaphore_handle sem)
582 if (!sem || !sem->signal_fence.context)
585 sem->signal_fence.context = NULL;;
586 sem->signal_fence.ip_type = 0;
587 sem->signal_fence.ip_instance = 0;
588 sem->signal_fence.ring = 0;
589 sem->signal_fence.fence = 0;
594 static int amdgpu_cs_unreference_sem(amdgpu_semaphore_handle sem)
599 if (update_references(&sem->refcount, NULL))
604 int amdgpu_cs_destroy_semaphore(amdgpu_semaphore_handle sem)
606 return amdgpu_cs_unreference_sem(sem);
609 int amdgpu_cs_create_syncobj2(amdgpu_device_handle dev,
616 return drmSyncobjCreate(dev->fd, flags, handle);
619 int amdgpu_cs_create_syncobj(amdgpu_device_handle dev,
625 return drmSyncobjCreate(dev->fd, 0, handle);
628 int amdgpu_cs_destroy_syncobj(amdgpu_device_handle dev,
634 return drmSyncobjDestroy(dev->fd, handle);
637 int amdgpu_cs_syncobj_wait(amdgpu_device_handle dev,
638 uint32_t *handles, unsigned num_handles,
639 int64_t timeout_nsec, unsigned flags,
640 uint32_t *first_signaled)
645 return drmSyncobjWait(dev->fd, handles, num_handles, timeout_nsec,
646 flags, first_signaled);
649 int amdgpu_cs_export_syncobj(amdgpu_device_handle dev,
656 return drmSyncobjHandleToFD(dev->fd, handle, shared_fd);
659 int amdgpu_cs_import_syncobj(amdgpu_device_handle dev,
666 return drmSyncobjFDToHandle(dev->fd, shared_fd, handle);
669 int amdgpu_cs_syncobj_export_sync_file(amdgpu_device_handle dev,
676 return drmSyncobjExportSyncFile(dev->fd, syncobj, sync_file_fd);
679 int amdgpu_cs_syncobj_import_sync_file(amdgpu_device_handle dev,
686 return drmSyncobjImportSyncFile(dev->fd, syncobj, sync_file_fd);
689 int amdgpu_cs_submit_raw(amdgpu_device_handle dev,
690 amdgpu_context_handle context,
691 amdgpu_bo_list_handle bo_list_handle,
693 struct drm_amdgpu_cs_chunk *chunks,
696 union drm_amdgpu_cs cs = {0};
697 uint64_t *chunk_array;
702 chunk_array = alloca(sizeof(uint64_t) * num_chunks);
703 for (i = 0; i < num_chunks; i++)
704 chunk_array[i] = (uint64_t)(uintptr_t)&chunks[i];
705 cs.in.chunks = (uint64_t)(uintptr_t)chunk_array;
706 cs.in.ctx_id = context->id;
707 cs.in.bo_list_handle = bo_list_handle ? bo_list_handle->handle : 0;
708 cs.in.num_chunks = num_chunks;
709 r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_CS,
715 *seq_no = cs.out.handle;
719 void amdgpu_cs_chunk_fence_info_to_data(struct amdgpu_cs_fence_info *fence_info,
720 struct drm_amdgpu_cs_chunk_data *data)
722 data->fence_data.handle = fence_info->handle->handle;
723 data->fence_data.offset = fence_info->offset * sizeof(uint64_t);
726 void amdgpu_cs_chunk_fence_to_dep(struct amdgpu_cs_fence *fence,
727 struct drm_amdgpu_cs_chunk_dep *dep)
729 dep->ip_type = fence->ip_type;
730 dep->ip_instance = fence->ip_instance;
731 dep->ring = fence->ring;
732 dep->ctx_id = fence->context->id;
733 dep->handle = fence->fence;
736 int amdgpu_cs_fence_to_handle(amdgpu_device_handle dev,
737 struct amdgpu_cs_fence *fence,
739 uint32_t *out_handle)
741 union drm_amdgpu_fence_to_handle fth = {0};
744 fth.in.fence.ctx_id = fence->context->id;
745 fth.in.fence.ip_type = fence->ip_type;
746 fth.in.fence.ip_instance = fence->ip_instance;
747 fth.in.fence.ring = fence->ring;
748 fth.in.fence.seq_no = fence->fence;
751 r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_FENCE_TO_HANDLE,
754 *out_handle = fth.out.handle;