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 int amdgpu_close_kms_handle(int fd, uint32_t handle)
44 struct drm_gem_close args = {};
47 return drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &args);
50 static int amdgpu_bo_create(amdgpu_device_handle dev,
53 amdgpu_bo_handle *buf_handle)
58 bo = calloc(1, sizeof(struct amdgpu_bo));
62 r = handle_table_insert(&dev->bo_handles, handle, bo);
68 atomic_set(&bo->refcount, 1);
70 bo->alloc_size = size;
72 pthread_mutex_init(&bo->cpu_access_mutex, NULL);
78 drm_public int amdgpu_bo_alloc(amdgpu_device_handle dev,
79 struct amdgpu_bo_alloc_request *alloc_buffer,
80 amdgpu_bo_handle *buf_handle)
82 union drm_amdgpu_gem_create args;
85 memset(&args, 0, sizeof(args));
86 args.in.bo_size = alloc_buffer->alloc_size;
87 args.in.alignment = alloc_buffer->phys_alignment;
89 /* Set the placement. */
90 args.in.domains = alloc_buffer->preferred_heap;
91 args.in.domain_flags = alloc_buffer->flags;
93 /* Allocate the buffer with the preferred heap. */
94 r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_GEM_CREATE,
99 pthread_mutex_lock(&dev->bo_table_mutex);
100 r = amdgpu_bo_create(dev, alloc_buffer->alloc_size, args.out.handle,
102 pthread_mutex_unlock(&dev->bo_table_mutex);
104 amdgpu_close_kms_handle(dev->fd, args.out.handle);
111 drm_public int amdgpu_bo_set_metadata(amdgpu_bo_handle bo,
112 struct amdgpu_bo_metadata *info)
114 struct drm_amdgpu_gem_metadata args = {};
116 args.handle = bo->handle;
117 args.op = AMDGPU_GEM_METADATA_OP_SET_METADATA;
118 args.data.flags = info->flags;
119 args.data.tiling_info = info->tiling_info;
121 if (info->size_metadata > sizeof(args.data.data))
124 if (info->size_metadata) {
125 args.data.data_size_bytes = info->size_metadata;
126 memcpy(args.data.data, info->umd_metadata, info->size_metadata);
129 return drmCommandWriteRead(bo->dev->fd,
130 DRM_AMDGPU_GEM_METADATA,
131 &args, sizeof(args));
134 drm_public int amdgpu_bo_query_info(amdgpu_bo_handle bo,
135 struct amdgpu_bo_info *info)
137 struct drm_amdgpu_gem_metadata metadata = {};
138 struct drm_amdgpu_gem_create_in bo_info = {};
139 struct drm_amdgpu_gem_op gem_op = {};
142 /* Validate the BO passed in */
146 /* Query metadata. */
147 metadata.handle = bo->handle;
148 metadata.op = AMDGPU_GEM_METADATA_OP_GET_METADATA;
150 r = drmCommandWriteRead(bo->dev->fd, DRM_AMDGPU_GEM_METADATA,
151 &metadata, sizeof(metadata));
155 if (metadata.data.data_size_bytes >
156 sizeof(info->metadata.umd_metadata))
159 /* Query buffer info. */
160 gem_op.handle = bo->handle;
161 gem_op.op = AMDGPU_GEM_OP_GET_GEM_CREATE_INFO;
162 gem_op.value = (uintptr_t)&bo_info;
164 r = drmCommandWriteRead(bo->dev->fd, DRM_AMDGPU_GEM_OP,
165 &gem_op, sizeof(gem_op));
169 memset(info, 0, sizeof(*info));
170 info->alloc_size = bo_info.bo_size;
171 info->phys_alignment = bo_info.alignment;
172 info->preferred_heap = bo_info.domains;
173 info->alloc_flags = bo_info.domain_flags;
174 info->metadata.flags = metadata.data.flags;
175 info->metadata.tiling_info = metadata.data.tiling_info;
177 info->metadata.size_metadata = metadata.data.data_size_bytes;
178 if (metadata.data.data_size_bytes > 0)
179 memcpy(info->metadata.umd_metadata, metadata.data.data,
180 metadata.data.data_size_bytes);
185 static int amdgpu_bo_export_flink(amdgpu_bo_handle bo)
187 struct drm_gem_flink flink;
198 if (bo->dev->flink_fd != bo->dev->fd) {
199 r = drmPrimeHandleToFD(bo->dev->fd, bo->handle, DRM_CLOEXEC,
202 r = drmPrimeFDToHandle(bo->dev->flink_fd, dma_fd, &handle);
207 fd = bo->dev->flink_fd;
209 memset(&flink, 0, sizeof(flink));
210 flink.handle = handle;
212 r = drmIoctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
216 bo->flink_name = flink.name;
218 if (bo->dev->flink_fd != bo->dev->fd)
219 amdgpu_close_kms_handle(bo->dev->flink_fd, handle);
221 pthread_mutex_lock(&bo->dev->bo_table_mutex);
222 r = handle_table_insert(&bo->dev->bo_flink_names, bo->flink_name, bo);
223 pthread_mutex_unlock(&bo->dev->bo_table_mutex);
228 drm_public int amdgpu_bo_export(amdgpu_bo_handle bo,
229 enum amdgpu_bo_handle_type type,
230 uint32_t *shared_handle)
235 case amdgpu_bo_handle_type_gem_flink_name:
236 r = amdgpu_bo_export_flink(bo);
240 *shared_handle = bo->flink_name;
243 case amdgpu_bo_handle_type_kms:
244 case amdgpu_bo_handle_type_kms_noimport:
245 *shared_handle = bo->handle;
248 case amdgpu_bo_handle_type_dma_buf_fd:
249 return drmPrimeHandleToFD(bo->dev->fd, bo->handle,
250 DRM_CLOEXEC | DRM_RDWR,
251 (int*)shared_handle);
256 drm_public int amdgpu_bo_import(amdgpu_device_handle dev,
257 enum amdgpu_bo_handle_type type,
258 uint32_t shared_handle,
259 struct amdgpu_bo_import_result *output)
261 struct drm_gem_open open_arg = {};
262 struct amdgpu_bo *bo = NULL;
263 uint32_t handle = 0, flink_name = 0;
264 uint64_t alloc_size = 0;
267 uint64_t dma_buf_size = 0;
269 /* We must maintain a list of pairs <handle, bo>, so that we always
270 * return the same amdgpu_bo instance for the same handle. */
271 pthread_mutex_lock(&dev->bo_table_mutex);
273 /* Convert a DMA buf handle to a KMS handle now. */
274 if (type == amdgpu_bo_handle_type_dma_buf_fd) {
277 /* Get a KMS handle. */
278 r = drmPrimeFDToHandle(dev->fd, shared_handle, &handle);
282 /* Query the buffer size. */
283 size = lseek(shared_handle, 0, SEEK_END);
284 if (size == (off_t)-1) {
288 lseek(shared_handle, 0, SEEK_SET);
291 shared_handle = handle;
294 /* If we have already created a buffer with this handle, find it. */
296 case amdgpu_bo_handle_type_gem_flink_name:
297 bo = handle_table_lookup(&dev->bo_flink_names, shared_handle);
300 case amdgpu_bo_handle_type_dma_buf_fd:
301 bo = handle_table_lookup(&dev->bo_handles, shared_handle);
304 case amdgpu_bo_handle_type_kms:
305 case amdgpu_bo_handle_type_kms_noimport:
306 /* Importing a KMS handle in not allowed. */
316 /* The buffer already exists, just bump the refcount. */
317 atomic_inc(&bo->refcount);
318 pthread_mutex_unlock(&dev->bo_table_mutex);
320 output->buf_handle = bo;
321 output->alloc_size = bo->alloc_size;
325 /* Open the handle. */
327 case amdgpu_bo_handle_type_gem_flink_name:
328 open_arg.name = shared_handle;
329 r = drmIoctl(dev->flink_fd, DRM_IOCTL_GEM_OPEN, &open_arg);
333 flink_name = shared_handle;
334 handle = open_arg.handle;
335 alloc_size = open_arg.size;
336 if (dev->flink_fd != dev->fd) {
337 r = drmPrimeHandleToFD(dev->flink_fd, handle,
338 DRM_CLOEXEC, &dma_fd);
341 r = drmPrimeFDToHandle(dev->fd, dma_fd, &handle);
345 r = amdgpu_close_kms_handle(dev->flink_fd,
353 case amdgpu_bo_handle_type_dma_buf_fd:
354 handle = shared_handle;
355 alloc_size = dma_buf_size;
358 case amdgpu_bo_handle_type_kms:
359 case amdgpu_bo_handle_type_kms_noimport:
360 assert(0); /* unreachable */
364 r = amdgpu_bo_create(dev, alloc_size, handle, &bo);
369 bo->flink_name = flink_name;
370 r = handle_table_insert(&dev->bo_flink_names, flink_name,
377 output->buf_handle = bo;
378 output->alloc_size = bo->alloc_size;
379 pthread_mutex_unlock(&dev->bo_table_mutex);
383 if (flink_name && open_arg.handle)
384 amdgpu_close_kms_handle(dev->flink_fd, open_arg.handle);
389 amdgpu_close_kms_handle(dev->fd, handle);
391 pthread_mutex_unlock(&dev->bo_table_mutex);
395 drm_public int amdgpu_bo_free(amdgpu_bo_handle buf_handle)
397 struct amdgpu_device *dev;
398 struct amdgpu_bo *bo = buf_handle;
402 pthread_mutex_lock(&dev->bo_table_mutex);
404 if (update_references(&bo->refcount, NULL)) {
405 /* Remove the buffer from the hash tables. */
406 handle_table_remove(&dev->bo_handles, bo->handle);
409 handle_table_remove(&dev->bo_flink_names,
412 /* Release CPU access. */
413 if (bo->cpu_map_count > 0) {
414 bo->cpu_map_count = 1;
415 amdgpu_bo_cpu_unmap(bo);
418 amdgpu_close_kms_handle(dev->fd, bo->handle);
419 pthread_mutex_destroy(&bo->cpu_access_mutex);
423 pthread_mutex_unlock(&dev->bo_table_mutex);
428 drm_public void amdgpu_bo_inc_ref(amdgpu_bo_handle bo)
430 atomic_inc(&bo->refcount);
433 drm_public int amdgpu_bo_cpu_map(amdgpu_bo_handle bo, void **cpu)
435 union drm_amdgpu_gem_mmap args;
439 pthread_mutex_lock(&bo->cpu_access_mutex);
443 assert(bo->cpu_map_count > 0);
446 pthread_mutex_unlock(&bo->cpu_access_mutex);
450 assert(bo->cpu_map_count == 0);
452 memset(&args, 0, sizeof(args));
454 /* Query the buffer address (args.addr_ptr).
455 * The kernel driver ignores the offset and size parameters. */
456 args.in.handle = bo->handle;
458 r = drmCommandWriteRead(bo->dev->fd, DRM_AMDGPU_GEM_MMAP, &args,
461 pthread_mutex_unlock(&bo->cpu_access_mutex);
465 /* Map the buffer. */
466 ptr = drm_mmap(NULL, bo->alloc_size, PROT_READ | PROT_WRITE, MAP_SHARED,
467 bo->dev->fd, args.out.addr_ptr);
468 if (ptr == MAP_FAILED) {
469 pthread_mutex_unlock(&bo->cpu_access_mutex);
474 bo->cpu_map_count = 1;
475 pthread_mutex_unlock(&bo->cpu_access_mutex);
481 drm_public int amdgpu_bo_cpu_unmap(amdgpu_bo_handle bo)
485 pthread_mutex_lock(&bo->cpu_access_mutex);
486 assert(bo->cpu_map_count >= 0);
488 if (bo->cpu_map_count == 0) {
490 pthread_mutex_unlock(&bo->cpu_access_mutex);
495 if (bo->cpu_map_count > 0) {
496 /* mapped multiple times */
497 pthread_mutex_unlock(&bo->cpu_access_mutex);
501 r = drm_munmap(bo->cpu_ptr, bo->alloc_size) == 0 ? 0 : -errno;
503 pthread_mutex_unlock(&bo->cpu_access_mutex);
507 drm_public int amdgpu_query_buffer_size_alignment(amdgpu_device_handle dev,
508 struct amdgpu_buffer_size_alignments *info)
510 info->size_local = dev->dev_info.pte_fragment_size;
511 info->size_remote = dev->dev_info.gart_page_size;
515 drm_public int amdgpu_bo_wait_for_idle(amdgpu_bo_handle bo,
519 union drm_amdgpu_gem_wait_idle args;
522 memset(&args, 0, sizeof(args));
523 args.in.handle = bo->handle;
524 args.in.timeout = amdgpu_cs_calculate_timeout(timeout_ns);
526 r = drmCommandWriteRead(bo->dev->fd, DRM_AMDGPU_GEM_WAIT_IDLE,
527 &args, sizeof(args));
530 *busy = args.out.status;
533 fprintf(stderr, "amdgpu: GEM_WAIT_IDLE failed with %i\n", r);
538 drm_public int amdgpu_find_bo_by_cpu_mapping(amdgpu_device_handle dev,
541 amdgpu_bo_handle *buf_handle,
542 uint64_t *offset_in_bo)
544 struct amdgpu_bo *bo;
548 if (cpu == NULL || size == 0)
552 * Workaround for a buggy application which tries to import previously
553 * exposed CPU pointers. If we find a real world use case we should
554 * improve that by asking the kernel for the right handle.
556 pthread_mutex_lock(&dev->bo_table_mutex);
557 for (i = 0; i < dev->bo_handles.max_key; i++) {
558 bo = handle_table_lookup(&dev->bo_handles, i);
559 if (!bo || !bo->cpu_ptr || size > bo->alloc_size)
561 if (cpu >= bo->cpu_ptr &&
562 cpu < (void*)((uintptr_t)bo->cpu_ptr + bo->alloc_size))
566 if (i < dev->bo_handles.max_key) {
567 atomic_inc(&bo->refcount);
569 *offset_in_bo = (uintptr_t)cpu - (uintptr_t)bo->cpu_ptr;
575 pthread_mutex_unlock(&dev->bo_table_mutex);
580 drm_public int amdgpu_create_bo_from_user_mem(amdgpu_device_handle dev,
583 amdgpu_bo_handle *buf_handle)
586 struct drm_amdgpu_gem_userptr args;
588 args.addr = (uintptr_t)cpu;
589 args.flags = AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_REGISTER |
590 AMDGPU_GEM_USERPTR_VALIDATE;
592 r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_GEM_USERPTR,
593 &args, sizeof(args));
597 pthread_mutex_lock(&dev->bo_table_mutex);
598 r = amdgpu_bo_create(dev, size, args.handle, buf_handle);
599 pthread_mutex_unlock(&dev->bo_table_mutex);
601 amdgpu_close_kms_handle(dev->fd, args.handle);
608 drm_public int amdgpu_bo_list_create_raw(amdgpu_device_handle dev,
609 uint32_t number_of_buffers,
610 struct drm_amdgpu_bo_list_entry *buffers,
613 union drm_amdgpu_bo_list args;
616 memset(&args, 0, sizeof(args));
617 args.in.operation = AMDGPU_BO_LIST_OP_CREATE;
618 args.in.bo_number = number_of_buffers;
619 args.in.bo_info_size = sizeof(struct drm_amdgpu_bo_list_entry);
620 args.in.bo_info_ptr = (uint64_t)(uintptr_t)buffers;
622 r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_BO_LIST,
623 &args, sizeof(args));
625 *result = args.out.list_handle;
629 drm_public int amdgpu_bo_list_destroy_raw(amdgpu_device_handle dev,
632 union drm_amdgpu_bo_list args;
634 memset(&args, 0, sizeof(args));
635 args.in.operation = AMDGPU_BO_LIST_OP_DESTROY;
636 args.in.list_handle = bo_list;
638 return drmCommandWriteRead(dev->fd, DRM_AMDGPU_BO_LIST,
639 &args, sizeof(args));
642 drm_public int amdgpu_bo_list_create(amdgpu_device_handle dev,
643 uint32_t number_of_resources,
644 amdgpu_bo_handle *resources,
645 uint8_t *resource_prios,
646 amdgpu_bo_list_handle *result)
648 struct drm_amdgpu_bo_list_entry *list;
649 union drm_amdgpu_bo_list args;
653 if (!number_of_resources)
656 /* overflow check for multiplication */
657 if (number_of_resources > UINT32_MAX / sizeof(struct drm_amdgpu_bo_list_entry))
660 list = malloc(number_of_resources * sizeof(struct drm_amdgpu_bo_list_entry));
664 *result = malloc(sizeof(struct amdgpu_bo_list));
670 memset(&args, 0, sizeof(args));
671 args.in.operation = AMDGPU_BO_LIST_OP_CREATE;
672 args.in.bo_number = number_of_resources;
673 args.in.bo_info_size = sizeof(struct drm_amdgpu_bo_list_entry);
674 args.in.bo_info_ptr = (uint64_t)(uintptr_t)list;
676 for (i = 0; i < number_of_resources; i++) {
677 list[i].bo_handle = resources[i]->handle;
679 list[i].bo_priority = resource_prios[i];
681 list[i].bo_priority = 0;
684 r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_BO_LIST,
685 &args, sizeof(args));
692 (*result)->dev = dev;
693 (*result)->handle = args.out.list_handle;
697 drm_public int amdgpu_bo_list_destroy(amdgpu_bo_list_handle list)
699 union drm_amdgpu_bo_list args;
702 memset(&args, 0, sizeof(args));
703 args.in.operation = AMDGPU_BO_LIST_OP_DESTROY;
704 args.in.list_handle = list->handle;
706 r = drmCommandWriteRead(list->dev->fd, DRM_AMDGPU_BO_LIST,
707 &args, sizeof(args));
715 drm_public int amdgpu_bo_list_update(amdgpu_bo_list_handle handle,
716 uint32_t number_of_resources,
717 amdgpu_bo_handle *resources,
718 uint8_t *resource_prios)
720 struct drm_amdgpu_bo_list_entry *list;
721 union drm_amdgpu_bo_list args;
725 if (!number_of_resources)
728 /* overflow check for multiplication */
729 if (number_of_resources > UINT32_MAX / sizeof(struct drm_amdgpu_bo_list_entry))
732 list = malloc(number_of_resources * sizeof(struct drm_amdgpu_bo_list_entry));
736 args.in.operation = AMDGPU_BO_LIST_OP_UPDATE;
737 args.in.list_handle = handle->handle;
738 args.in.bo_number = number_of_resources;
739 args.in.bo_info_size = sizeof(struct drm_amdgpu_bo_list_entry);
740 args.in.bo_info_ptr = (uintptr_t)list;
742 for (i = 0; i < number_of_resources; i++) {
743 list[i].bo_handle = resources[i]->handle;
745 list[i].bo_priority = resource_prios[i];
747 list[i].bo_priority = 0;
750 r = drmCommandWriteRead(handle->dev->fd, DRM_AMDGPU_BO_LIST,
751 &args, sizeof(args));
756 drm_public int amdgpu_bo_va_op(amdgpu_bo_handle bo,
763 amdgpu_device_handle dev = bo->dev;
765 size = ALIGN(size, getpagesize());
767 return amdgpu_bo_va_op_raw(dev, bo, offset, size, addr,
768 AMDGPU_VM_PAGE_READABLE |
769 AMDGPU_VM_PAGE_WRITEABLE |
770 AMDGPU_VM_PAGE_EXECUTABLE, ops);
773 drm_public int amdgpu_bo_va_op_raw(amdgpu_device_handle dev,
781 struct drm_amdgpu_gem_va va;
784 if (ops != AMDGPU_VA_OP_MAP && ops != AMDGPU_VA_OP_UNMAP &&
785 ops != AMDGPU_VA_OP_REPLACE && ops != AMDGPU_VA_OP_CLEAR)
788 memset(&va, 0, sizeof(va));
789 va.handle = bo ? bo->handle : 0;
792 va.va_address = addr;
793 va.offset_in_bo = offset;
796 r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_GEM_VA, &va, sizeof(va));