2 * Copyright © 2014 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
32 #include <sys/ioctl.h>
36 #include "libdrm_macros.h"
38 #include "amdgpu_drm.h"
39 #include "amdgpu_internal.h"
40 #include "util_math.h"
42 static void amdgpu_close_kms_handle(amdgpu_device_handle dev,
45 struct drm_gem_close args = {};
48 drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &args);
51 int amdgpu_bo_alloc(amdgpu_device_handle dev,
52 struct amdgpu_bo_alloc_request *alloc_buffer,
53 amdgpu_bo_handle *buf_handle)
56 union drm_amdgpu_gem_create args;
57 unsigned heap = alloc_buffer->preferred_heap;
60 /* It's an error if the heap is not specified */
61 if (!(heap & (AMDGPU_GEM_DOMAIN_GTT | AMDGPU_GEM_DOMAIN_VRAM)))
64 bo = calloc(1, sizeof(struct amdgpu_bo));
68 atomic_set(&bo->refcount, 1);
70 bo->alloc_size = alloc_buffer->alloc_size;
72 memset(&args, 0, sizeof(args));
73 args.in.bo_size = alloc_buffer->alloc_size;
74 args.in.alignment = alloc_buffer->phys_alignment;
76 /* Set the placement. */
77 args.in.domains = heap;
78 args.in.domain_flags = alloc_buffer->flags;
80 /* Allocate the buffer with the preferred heap. */
81 r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_GEM_CREATE,
88 bo->handle = args.out.handle;
90 pthread_mutex_lock(&bo->dev->bo_table_mutex);
91 r = handle_table_insert(&bo->dev->bo_handles, bo->handle, bo);
92 pthread_mutex_unlock(&bo->dev->bo_table_mutex);
94 pthread_mutex_init(&bo->cpu_access_mutex, NULL);
104 int amdgpu_bo_set_metadata(amdgpu_bo_handle bo,
105 struct amdgpu_bo_metadata *info)
107 struct drm_amdgpu_gem_metadata args = {};
109 args.handle = bo->handle;
110 args.op = AMDGPU_GEM_METADATA_OP_SET_METADATA;
111 args.data.flags = info->flags;
112 args.data.tiling_info = info->tiling_info;
114 if (info->size_metadata > sizeof(args.data.data))
117 if (info->size_metadata) {
118 args.data.data_size_bytes = info->size_metadata;
119 memcpy(args.data.data, info->umd_metadata, info->size_metadata);
122 return drmCommandWriteRead(bo->dev->fd,
123 DRM_AMDGPU_GEM_METADATA,
124 &args, sizeof(args));
127 int amdgpu_bo_query_info(amdgpu_bo_handle bo,
128 struct amdgpu_bo_info *info)
130 struct drm_amdgpu_gem_metadata metadata = {};
131 struct drm_amdgpu_gem_create_in bo_info = {};
132 struct drm_amdgpu_gem_op gem_op = {};
135 /* Validate the BO passed in */
139 /* Query metadata. */
140 metadata.handle = bo->handle;
141 metadata.op = AMDGPU_GEM_METADATA_OP_GET_METADATA;
143 r = drmCommandWriteRead(bo->dev->fd, DRM_AMDGPU_GEM_METADATA,
144 &metadata, sizeof(metadata));
148 if (metadata.data.data_size_bytes >
149 sizeof(info->metadata.umd_metadata))
152 /* Query buffer info. */
153 gem_op.handle = bo->handle;
154 gem_op.op = AMDGPU_GEM_OP_GET_GEM_CREATE_INFO;
155 gem_op.value = (uintptr_t)&bo_info;
157 r = drmCommandWriteRead(bo->dev->fd, DRM_AMDGPU_GEM_OP,
158 &gem_op, sizeof(gem_op));
162 memset(info, 0, sizeof(*info));
163 info->alloc_size = bo_info.bo_size;
164 info->phys_alignment = bo_info.alignment;
165 info->preferred_heap = bo_info.domains;
166 info->alloc_flags = bo_info.domain_flags;
167 info->metadata.flags = metadata.data.flags;
168 info->metadata.tiling_info = metadata.data.tiling_info;
170 info->metadata.size_metadata = metadata.data.data_size_bytes;
171 if (metadata.data.data_size_bytes > 0)
172 memcpy(info->metadata.umd_metadata, metadata.data.data,
173 metadata.data.data_size_bytes);
178 static int amdgpu_bo_export_flink(amdgpu_bo_handle bo)
180 struct drm_gem_flink flink;
191 if (bo->dev->flink_fd != bo->dev->fd) {
192 r = drmPrimeHandleToFD(bo->dev->fd, bo->handle, DRM_CLOEXEC,
195 r = drmPrimeFDToHandle(bo->dev->flink_fd, dma_fd, &handle);
200 fd = bo->dev->flink_fd;
202 memset(&flink, 0, sizeof(flink));
203 flink.handle = handle;
205 r = drmIoctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
209 bo->flink_name = flink.name;
211 if (bo->dev->flink_fd != bo->dev->fd) {
212 struct drm_gem_close args = {};
213 args.handle = handle;
214 drmIoctl(bo->dev->flink_fd, DRM_IOCTL_GEM_CLOSE, &args);
217 pthread_mutex_lock(&bo->dev->bo_table_mutex);
218 r = handle_table_insert(&bo->dev->bo_flink_names, bo->flink_name, bo);
219 pthread_mutex_unlock(&bo->dev->bo_table_mutex);
224 int amdgpu_bo_export(amdgpu_bo_handle bo,
225 enum amdgpu_bo_handle_type type,
226 uint32_t *shared_handle)
231 case amdgpu_bo_handle_type_gem_flink_name:
232 r = amdgpu_bo_export_flink(bo);
236 *shared_handle = bo->flink_name;
239 case amdgpu_bo_handle_type_kms:
240 case amdgpu_bo_handle_type_kms_noimport:
241 *shared_handle = bo->handle;
244 case amdgpu_bo_handle_type_dma_buf_fd:
245 return drmPrimeHandleToFD(bo->dev->fd, bo->handle,
246 DRM_CLOEXEC | DRM_RDWR,
247 (int*)shared_handle);
252 int amdgpu_bo_import(amdgpu_device_handle dev,
253 enum amdgpu_bo_handle_type type,
254 uint32_t shared_handle,
255 struct amdgpu_bo_import_result *output)
257 struct drm_gem_open open_arg = {};
258 struct drm_gem_close close_arg = {};
259 struct amdgpu_bo *bo = NULL;
262 uint64_t dma_buf_size = 0;
264 /* We must maintain a list of pairs <handle, bo>, so that we always
265 * return the same amdgpu_bo instance for the same handle. */
266 pthread_mutex_lock(&dev->bo_table_mutex);
268 /* Convert a DMA buf handle to a KMS handle now. */
269 if (type == amdgpu_bo_handle_type_dma_buf_fd) {
273 /* Get a KMS handle. */
274 r = drmPrimeFDToHandle(dev->fd, shared_handle, &handle);
276 pthread_mutex_unlock(&dev->bo_table_mutex);
280 /* Query the buffer size. */
281 size = lseek(shared_handle, 0, SEEK_END);
282 if (size == (off_t)-1) {
283 pthread_mutex_unlock(&dev->bo_table_mutex);
284 amdgpu_close_kms_handle(dev, handle);
287 lseek(shared_handle, 0, SEEK_SET);
290 shared_handle = handle;
293 /* If we have already created a buffer with this handle, find it. */
295 case amdgpu_bo_handle_type_gem_flink_name:
296 bo = handle_table_lookup(&dev->bo_flink_names, shared_handle);
299 case amdgpu_bo_handle_type_dma_buf_fd:
300 bo = handle_table_lookup(&dev->bo_handles, shared_handle);
303 case amdgpu_bo_handle_type_kms:
304 case amdgpu_bo_handle_type_kms_noimport:
305 /* Importing a KMS handle in not allowed. */
306 pthread_mutex_unlock(&dev->bo_table_mutex);
310 pthread_mutex_unlock(&dev->bo_table_mutex);
315 /* The buffer already exists, just bump the refcount. */
316 atomic_inc(&bo->refcount);
317 pthread_mutex_unlock(&dev->bo_table_mutex);
319 output->buf_handle = bo;
320 output->alloc_size = bo->alloc_size;
324 bo = calloc(1, sizeof(struct amdgpu_bo));
326 pthread_mutex_unlock(&dev->bo_table_mutex);
327 if (type == amdgpu_bo_handle_type_dma_buf_fd) {
328 amdgpu_close_kms_handle(dev, shared_handle);
333 /* Open the handle. */
335 case amdgpu_bo_handle_type_gem_flink_name:
336 open_arg.name = shared_handle;
337 r = drmIoctl(dev->flink_fd, DRM_IOCTL_GEM_OPEN, &open_arg);
340 pthread_mutex_unlock(&dev->bo_table_mutex);
344 bo->handle = open_arg.handle;
345 if (dev->flink_fd != dev->fd) {
346 close_arg.handle = open_arg.handle;
347 r = drmPrimeHandleToFD(dev->flink_fd, bo->handle, DRM_CLOEXEC, &dma_fd);
350 drmIoctl(dev->flink_fd, DRM_IOCTL_GEM_CLOSE,
352 pthread_mutex_unlock(&dev->bo_table_mutex);
355 r = drmPrimeFDToHandle(dev->fd, dma_fd, &bo->handle );
358 drmIoctl(dev->flink_fd, DRM_IOCTL_GEM_CLOSE, &close_arg);
362 pthread_mutex_unlock(&dev->bo_table_mutex);
366 bo->flink_name = shared_handle;
367 bo->alloc_size = open_arg.size;
368 r = handle_table_insert(&dev->bo_flink_names, shared_handle,
371 pthread_mutex_unlock(&dev->bo_table_mutex);
377 case amdgpu_bo_handle_type_dma_buf_fd:
378 bo->handle = shared_handle;
379 bo->alloc_size = dma_buf_size;
382 case amdgpu_bo_handle_type_kms:
383 case amdgpu_bo_handle_type_kms_noimport:
384 assert(0); /* unreachable */
388 atomic_set(&bo->refcount, 1);
390 pthread_mutex_init(&bo->cpu_access_mutex, NULL);
392 handle_table_insert(&dev->bo_handles, bo->handle, bo);
393 pthread_mutex_unlock(&dev->bo_table_mutex);
395 output->buf_handle = bo;
396 output->alloc_size = bo->alloc_size;
400 int amdgpu_bo_free(amdgpu_bo_handle buf_handle)
402 struct amdgpu_device *dev;
403 struct amdgpu_bo *bo = buf_handle;
407 pthread_mutex_lock(&dev->bo_table_mutex);
409 if (update_references(&bo->refcount, NULL)) {
410 /* Remove the buffer from the hash tables. */
411 handle_table_remove(&dev->bo_handles, bo->handle);
414 handle_table_remove(&dev->bo_flink_names,
417 /* Release CPU access. */
418 if (bo->cpu_map_count > 0) {
419 bo->cpu_map_count = 1;
420 amdgpu_bo_cpu_unmap(bo);
423 amdgpu_close_kms_handle(dev, bo->handle);
424 pthread_mutex_destroy(&bo->cpu_access_mutex);
428 pthread_mutex_unlock(&dev->bo_table_mutex);
432 int amdgpu_bo_cpu_map(amdgpu_bo_handle bo, void **cpu)
434 union drm_amdgpu_gem_mmap args;
438 pthread_mutex_lock(&bo->cpu_access_mutex);
442 assert(bo->cpu_map_count > 0);
445 pthread_mutex_unlock(&bo->cpu_access_mutex);
449 assert(bo->cpu_map_count == 0);
451 memset(&args, 0, sizeof(args));
453 /* Query the buffer address (args.addr_ptr).
454 * The kernel driver ignores the offset and size parameters. */
455 args.in.handle = bo->handle;
457 r = drmCommandWriteRead(bo->dev->fd, DRM_AMDGPU_GEM_MMAP, &args,
460 pthread_mutex_unlock(&bo->cpu_access_mutex);
464 /* Map the buffer. */
465 ptr = drm_mmap(NULL, bo->alloc_size, PROT_READ | PROT_WRITE, MAP_SHARED,
466 bo->dev->fd, args.out.addr_ptr);
467 if (ptr == MAP_FAILED) {
468 pthread_mutex_unlock(&bo->cpu_access_mutex);
473 bo->cpu_map_count = 1;
474 pthread_mutex_unlock(&bo->cpu_access_mutex);
480 int amdgpu_bo_cpu_unmap(amdgpu_bo_handle bo)
484 pthread_mutex_lock(&bo->cpu_access_mutex);
485 assert(bo->cpu_map_count >= 0);
487 if (bo->cpu_map_count == 0) {
489 pthread_mutex_unlock(&bo->cpu_access_mutex);
494 if (bo->cpu_map_count > 0) {
495 /* mapped multiple times */
496 pthread_mutex_unlock(&bo->cpu_access_mutex);
500 r = drm_munmap(bo->cpu_ptr, bo->alloc_size) == 0 ? 0 : -errno;
502 pthread_mutex_unlock(&bo->cpu_access_mutex);
506 int amdgpu_query_buffer_size_alignment(amdgpu_device_handle dev,
507 struct amdgpu_buffer_size_alignments *info)
509 info->size_local = dev->dev_info.pte_fragment_size;
510 info->size_remote = dev->dev_info.gart_page_size;
514 int amdgpu_bo_wait_for_idle(amdgpu_bo_handle bo,
518 union drm_amdgpu_gem_wait_idle args;
521 memset(&args, 0, sizeof(args));
522 args.in.handle = bo->handle;
523 args.in.timeout = amdgpu_cs_calculate_timeout(timeout_ns);
525 r = drmCommandWriteRead(bo->dev->fd, DRM_AMDGPU_GEM_WAIT_IDLE,
526 &args, sizeof(args));
529 *busy = args.out.status;
532 fprintf(stderr, "amdgpu: GEM_WAIT_IDLE failed with %i\n", r);
537 int amdgpu_find_bo_by_cpu_mapping(amdgpu_device_handle dev,
540 amdgpu_bo_handle *buf_handle,
541 uint64_t *offset_in_bo)
544 struct amdgpu_bo *bo;
546 if (cpu == NULL || size == 0)
550 * Workaround for a buggy application which tries to import previously
551 * exposed CPU pointers. If we find a real world use case we should
552 * improve that by asking the kernel for the right handle.
554 pthread_mutex_lock(&dev->bo_table_mutex);
555 for (i = 0; i < dev->bo_handles.max_key; i++) {
556 bo = handle_table_lookup(&dev->bo_handles, i);
557 if (!bo || !bo->cpu_ptr || size > bo->alloc_size)
559 if (cpu >= bo->cpu_ptr &&
560 cpu < (void*)((uintptr_t)bo->cpu_ptr + bo->alloc_size))
564 if (i < dev->bo_handles.max_key) {
565 atomic_inc(&bo->refcount);
567 *offset_in_bo = (uintptr_t)cpu - (uintptr_t)bo->cpu_ptr;
572 pthread_mutex_unlock(&dev->bo_table_mutex);
577 int amdgpu_create_bo_from_user_mem(amdgpu_device_handle dev,
580 amdgpu_bo_handle *buf_handle)
583 struct amdgpu_bo *bo;
584 struct drm_amdgpu_gem_userptr args;
586 args.addr = (uintptr_t)cpu;
587 args.flags = AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_REGISTER |
588 AMDGPU_GEM_USERPTR_VALIDATE;
590 r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_GEM_USERPTR,
591 &args, sizeof(args));
595 bo = calloc(1, sizeof(struct amdgpu_bo));
599 atomic_set(&bo->refcount, 1);
601 bo->alloc_size = size;
602 bo->handle = args.handle;
604 pthread_mutex_lock(&bo->dev->bo_table_mutex);
605 r = handle_table_insert(&bo->dev->bo_handles, bo->handle, bo);
606 pthread_mutex_unlock(&bo->dev->bo_table_mutex);
608 pthread_mutex_init(&bo->cpu_access_mutex, NULL);
618 int amdgpu_bo_list_create(amdgpu_device_handle dev,
619 uint32_t number_of_resources,
620 amdgpu_bo_handle *resources,
621 uint8_t *resource_prios,
622 amdgpu_bo_list_handle *result)
624 struct drm_amdgpu_bo_list_entry *list;
625 union drm_amdgpu_bo_list args;
629 if (!number_of_resources)
632 /* overflow check for multiplication */
633 if (number_of_resources > UINT32_MAX / sizeof(struct drm_amdgpu_bo_list_entry))
636 list = malloc(number_of_resources * sizeof(struct drm_amdgpu_bo_list_entry));
640 *result = malloc(sizeof(struct amdgpu_bo_list));
646 memset(&args, 0, sizeof(args));
647 args.in.operation = AMDGPU_BO_LIST_OP_CREATE;
648 args.in.bo_number = number_of_resources;
649 args.in.bo_info_size = sizeof(struct drm_amdgpu_bo_list_entry);
650 args.in.bo_info_ptr = (uint64_t)(uintptr_t)list;
652 for (i = 0; i < number_of_resources; i++) {
653 list[i].bo_handle = resources[i]->handle;
655 list[i].bo_priority = resource_prios[i];
657 list[i].bo_priority = 0;
660 r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_BO_LIST,
661 &args, sizeof(args));
668 (*result)->dev = dev;
669 (*result)->handle = args.out.list_handle;
673 int amdgpu_bo_list_destroy(amdgpu_bo_list_handle list)
675 union drm_amdgpu_bo_list args;
678 memset(&args, 0, sizeof(args));
679 args.in.operation = AMDGPU_BO_LIST_OP_DESTROY;
680 args.in.list_handle = list->handle;
682 r = drmCommandWriteRead(list->dev->fd, DRM_AMDGPU_BO_LIST,
683 &args, sizeof(args));
691 int amdgpu_bo_list_update(amdgpu_bo_list_handle handle,
692 uint32_t number_of_resources,
693 amdgpu_bo_handle *resources,
694 uint8_t *resource_prios)
696 struct drm_amdgpu_bo_list_entry *list;
697 union drm_amdgpu_bo_list args;
701 if (!number_of_resources)
704 /* overflow check for multiplication */
705 if (number_of_resources > UINT32_MAX / sizeof(struct drm_amdgpu_bo_list_entry))
708 list = malloc(number_of_resources * sizeof(struct drm_amdgpu_bo_list_entry));
712 args.in.operation = AMDGPU_BO_LIST_OP_UPDATE;
713 args.in.list_handle = handle->handle;
714 args.in.bo_number = number_of_resources;
715 args.in.bo_info_size = sizeof(struct drm_amdgpu_bo_list_entry);
716 args.in.bo_info_ptr = (uintptr_t)list;
718 for (i = 0; i < number_of_resources; i++) {
719 list[i].bo_handle = resources[i]->handle;
721 list[i].bo_priority = resource_prios[i];
723 list[i].bo_priority = 0;
726 r = drmCommandWriteRead(handle->dev->fd, DRM_AMDGPU_BO_LIST,
727 &args, sizeof(args));
732 int amdgpu_bo_va_op(amdgpu_bo_handle bo,
739 amdgpu_device_handle dev = bo->dev;
741 size = ALIGN(size, getpagesize());
743 return amdgpu_bo_va_op_raw(dev, bo, offset, size, addr,
744 AMDGPU_VM_PAGE_READABLE |
745 AMDGPU_VM_PAGE_WRITEABLE |
746 AMDGPU_VM_PAGE_EXECUTABLE, ops);
749 int amdgpu_bo_va_op_raw(amdgpu_device_handle dev,
757 struct drm_amdgpu_gem_va va;
760 if (ops != AMDGPU_VA_OP_MAP && ops != AMDGPU_VA_OP_UNMAP &&
761 ops != AMDGPU_VA_OP_REPLACE && ops != AMDGPU_VA_OP_CLEAR)
764 memset(&va, 0, sizeof(va));
765 va.handle = bo ? bo->handle : 0;
768 va.va_address = addr;
769 va.offset_in_bo = offset;
772 r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_GEM_VA, &va, sizeof(va));