From 1f09b85d125111c44e8a53024aa54077f15ab66f Mon Sep 17 00:00:00 2001 From: Jason Ekstrand Date: Mon, 28 Aug 2017 14:10:12 -0700 Subject: lib: Add some syncobj helpers (v2) [airlied: added some initial gtk-doc for the helper library.] Reviewed-by: Dave Airlie Signed-off-by: Jason Ekstrand --- .../intel-gpu-tools/intel-gpu-tools-docs.xml | 1 + lib/Makefile.sources | 2 + lib/igt_syncobj.c | 288 +++++++++++++++++++++ lib/igt_syncobj.h | 71 +++++ 4 files changed, 362 insertions(+) create mode 100644 lib/igt_syncobj.c create mode 100644 lib/igt_syncobj.h diff --git a/docs/reference/intel-gpu-tools/intel-gpu-tools-docs.xml b/docs/reference/intel-gpu-tools/intel-gpu-tools-docs.xml index 0c34e4a5..04a0485c 100644 --- a/docs/reference/intel-gpu-tools/intel-gpu-tools-docs.xml +++ b/docs/reference/intel-gpu-tools/intel-gpu-tools-docs.xml @@ -34,6 +34,7 @@ + diff --git a/lib/Makefile.sources b/lib/Makefile.sources index e851f62b..965e230e 100644 --- a/lib/Makefile.sources +++ b/lib/Makefile.sources @@ -84,6 +84,8 @@ lib_source_list = \ uwildmat/uwildmat.c \ igt_kmod.c \ igt_kmod.h \ + igt_syncobj.c \ + igt_syncobj.h \ $(NULL) .PHONY: version.h.tmp diff --git a/lib/igt_syncobj.c b/lib/igt_syncobj.c new file mode 100644 index 00000000..d9114ca8 --- /dev/null +++ b/lib/igt_syncobj.c @@ -0,0 +1,288 @@ +/* + * Copyright © 2017 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include +#include + +#include "igt.h" +#include "igt_syncobj.h" + +/** + * SECTION:igt_syncobj + * @short_description: Library with syncobj helpers + * @title: syncobj + * @include: igt_syncobj.h + * + * This library contains helpers for sync object tests. + */ + +static int +__syncobj_create(int fd, uint32_t *handle, uint32_t flags) +{ + struct drm_syncobj_create create = { 0 }; + int err = 0; + + create.flags = flags; + if (drmIoctl(fd, DRM_IOCTL_SYNCOBJ_CREATE, &create)) + err = -errno; + *handle = create.handle; + return err; +} + +/** + * syncobj_create + * @fd: The DRM file descriptor + * @flags: Flags to pass syncobj create + * + * Create a syncobj with the flags. + * + * Returns: A newly created syncobj + */ +uint32_t +syncobj_create(int fd, uint32_t flags) +{ + uint32_t handle; + igt_assert_eq(__syncobj_create(fd, &handle, flags), 0); + igt_assert(handle); + return handle; +} + +static int +__syncobj_destroy(int fd, uint32_t handle) +{ + struct drm_syncobj_destroy destroy = { 0 }; + int err = 0; + + destroy.handle = handle; + if (drmIoctl(fd, DRM_IOCTL_SYNCOBJ_DESTROY, &destroy)) + err = -errno; + return err; +} + +/** + * syncobj_destroy: + * @fd: The DRM file descriptor + * @handle: The handle to the syncobj to destroy + * Destroy a syncobj. + */ +void +syncobj_destroy(int fd, uint32_t handle) +{ + igt_assert_eq(__syncobj_destroy(fd, handle), 0); +} + +int +__syncobj_handle_to_fd(int fd, struct drm_syncobj_handle *args) +{ + int err = 0; + if (drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, args)) + err = -errno; + return err; +} + +/** + * syncobj_handle_to_fd: + * @fd: The DRM file descriptor + * @handle: Handle to syncobj + * @flags: Flags to handle to fd ioctl. + * + * Convert a syncobj handle to an fd using the flags. + * + * Returns: a file descriptor (either syncobj or sync_file. + */ +int +syncobj_handle_to_fd(int fd, uint32_t handle, uint32_t flags) +{ + struct drm_syncobj_handle args = { 0 }; + args.handle = handle; + args.flags = flags; + igt_assert_eq(__syncobj_handle_to_fd(fd, &args), 0); + igt_assert(args.fd >= 0); + return args.fd; +} + +int +__syncobj_fd_to_handle(int fd, struct drm_syncobj_handle *args) +{ + int err = 0; + if (drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, args)) + err = -errno; + return err; +} + +/** + * syncobj_fd_to_handle: + * @fd: The DRM file descriptor + * @syncobj_fd: syncobj fd to convert + * @flags: Flags to the syncobj fd to handle ioctl. + * + * Convert a syncobj fd a syncobj handle using the flags. + * + * Returns: a syncobj handle. + */ +uint32_t +syncobj_fd_to_handle(int fd, int syncobj_fd, uint32_t flags) +{ + struct drm_syncobj_handle args = { 0 }; + args.fd = syncobj_fd; + args.flags = flags; + igt_assert_eq(__syncobj_fd_to_handle(fd, &args), 0); + igt_assert(args.handle > 0); + return args.handle; +} + +/** + * syncobj_import_sync_file: + * @fd: The DRM file descriptor + * @handle: Handle to the syncobt to import file into + * @sync_file: The sync_file fd to import state from. + * + * Import a sync_file fd into a syncobj handle. + */ +void +syncobj_import_sync_file(int fd, uint32_t handle, int sync_file) +{ + struct drm_syncobj_handle args = { 0 }; + args.handle = handle; + args.fd = sync_file; + args.flags = DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE; + igt_assert_eq(__syncobj_fd_to_handle(fd, &args), 0); +} + +int +__syncobj_wait(int fd, struct local_syncobj_wait *args) +{ + int err = 0; + if (drmIoctl(fd, LOCAL_IOCTL_SYNCOBJ_WAIT, args)) + err = -errno; + return err; +} + +int +syncobj_wait_err(int fd, uint32_t *handles, uint32_t count, + uint64_t abs_timeout_nsec, uint32_t flags) +{ + struct local_syncobj_wait wait; + + wait.handles = to_user_pointer(handles); + wait.timeout_nsec = abs_timeout_nsec; + wait.count_handles = count; + wait.flags = flags; + wait.first_signaled = 0; + wait.pad = 0; + + return __syncobj_wait(fd, &wait); +} + +/** + * syncobj_wait: + * @fd: The DRM file descriptor + * @handles: List of syncobj handles to wait for. + * @count: Count of handles + * @abs_timeout_nsec: Absolute wait timeout in nanoseconds. + * @flags: Wait ioctl flags. + * @first_signaled: Returned handle for first signaled syncobj. + * + * Waits in the kernel for any/all the requested syncobjs + * using the timeout and flags. + * Returns: bool value - false = timedout, true = signaled + */ +bool +syncobj_wait(int fd, uint32_t *handles, uint32_t count, + uint64_t abs_timeout_nsec, uint32_t flags, + uint32_t *first_signaled) +{ + struct local_syncobj_wait wait; + int ret; + + wait.handles = to_user_pointer(handles); + wait.timeout_nsec = abs_timeout_nsec; + wait.count_handles = count; + wait.flags = flags; + wait.first_signaled = 0; + wait.pad = 0; + + ret = __syncobj_wait(fd, &wait); + if (ret == ETIME) + return false; + + igt_assert_eq(ret, 0); + if (first_signaled) + *first_signaled = wait.first_signaled; + + return true; +} + +static int +__syncobj_reset(int fd, uint32_t *handles, uint32_t count) +{ + struct local_syncobj_array array = { 0 }; + int err = 0; + + array.handles = to_user_pointer(handles); + array.count_handles = count; + if (drmIoctl(fd, LOCAL_IOCTL_SYNCOBJ_RESET, &array)) + err = -errno; + return err; +} + +/** + * syncobj_reset: + * @fd: The DRM file descriptor. + * @handles: Array of syncobj handles to reset + * @count: Count of syncobj handles. + * + * Reset state of a set of syncobjs. + */ +void +syncobj_reset(int fd, uint32_t *handles, uint32_t count) +{ + igt_assert_eq(__syncobj_reset(fd, handles, count), 0); +} + +static int +__syncobj_signal(int fd, uint32_t *handles, uint32_t count) +{ + struct local_syncobj_array array = { 0 }; + int err = 0; + + array.handles = to_user_pointer(handles); + array.count_handles = count; + if (drmIoctl(fd, LOCAL_IOCTL_SYNCOBJ_SIGNAL, &array)) + err = -errno; + return err; +} + +/** + * syncobj_signal: + * @fd: The DRM file descriptor. + * @handles: Array of syncobj handles to signal + * @count: Count of syncobj handles. + * + * Signal a set of syncobjs. + */ +void +syncobj_signal(int fd, uint32_t *handles, uint32_t count) +{ + igt_assert_eq(__syncobj_signal(fd, handles, count), 0); +} diff --git a/lib/igt_syncobj.h b/lib/igt_syncobj.h new file mode 100644 index 00000000..44d1378d --- /dev/null +++ b/lib/igt_syncobj.h @@ -0,0 +1,71 @@ +/* + * Copyright © 2017 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#ifndef IGT_SYNCOBJ_H +#define IGT_SYNCOBJ_H + +#include +#include +#include + +#define LOCAL_SYNCOBJ_CREATE_SIGNALED (1 << 0) + +#define LOCAL_SYNCOBJ_WAIT_FLAGS_WAIT_ALL (1 << 0) +#define LOCAL_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT (1 << 1) +struct local_syncobj_wait { + __u64 handles; + /* absolute timeout */ + __s64 timeout_nsec; + __u32 count_handles; + __u32 flags; + __u32 first_signaled; /* only valid when not waiting all */ + __u32 pad; +}; + +struct local_syncobj_array { + __u64 handles; + __u32 count_handles; + __u32 pad; +}; + +#define LOCAL_IOCTL_SYNCOBJ_WAIT DRM_IOWR(0xC3, struct local_syncobj_wait) +#define LOCAL_IOCTL_SYNCOBJ_RESET DRM_IOWR(0xC4, struct local_syncobj_array) +#define LOCAL_IOCTL_SYNCOBJ_SIGNAL DRM_IOWR(0xC5, struct local_syncobj_array) + +uint32_t syncobj_create(int fd, uint32_t flags); +void syncobj_destroy(int fd, uint32_t handle); +int __syncobj_handle_to_fd(int fd, struct drm_syncobj_handle *args); +int __syncobj_fd_to_handle(int fd, struct drm_syncobj_handle *args); +int syncobj_handle_to_fd(int fd, uint32_t handle, uint32_t flags); +uint32_t syncobj_fd_to_handle(int fd, int syncobj_fd, uint32_t flags); +void syncobj_import_sync_file(int fd, uint32_t handle, int sync_file); +int __syncobj_wait(int fd, struct local_syncobj_wait *args); +int syncobj_wait_err(int fd, uint32_t *handles, uint32_t count, + uint64_t abs_timeout_nsec, uint32_t flags); +bool syncobj_wait(int fd, uint32_t *handles, uint32_t count, + uint64_t abs_timeout_nsec, uint32_t flags, + uint32_t *first_signaled); +void syncobj_reset(int fd, uint32_t *handles, uint32_t count); +void syncobj_signal(int fd, uint32_t *handles, uint32_t count); + +#endif /* IGT_SYNCOBJ_H */ -- cgit v1.2.3