v2: with the files this time. Signed-off-by: Ben Widawsky <ben at bwidawsk.net> --- tests/Makefile.am | 8 +++ tests/gem_ctx_bad_destroy.c | 85 +++++++++++++++++++++++ tests/gem_ctx_bad_exec.c | 103 ++++++++++++++++++++++++++++ tests/gem_ctx_basic.c | 158 +++++++++++++++++++++++++++++++++++++++++++ tests/gem_ctx_create.c | 53 +++++++++++++++ tests/gem_ctx_exec.c | 105 ++++++++++++++++++++++++++++ 6 files changed, 512 insertions(+) create mode 100644 tests/gem_ctx_bad_destroy.c create mode 100644 tests/gem_ctx_bad_exec.c create mode 100644 tests/gem_ctx_basic.c create mode 100644 tests/gem_ctx_create.c create mode 100644 tests/gem_ctx_exec.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 9ec07c6..bc33ab0 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -61,6 +61,11 @@ TESTS_progs = \ drm_vma_limiter_gtt \ drm_vma_limiter_cached \ sysfs_rc6_residency \ + gem_ctx_basic \ + gem_ctx_exec \ + gem_ctx_bad_destroy \ + gem_ctx_create \ + gem_ctx_bad_exec \ $(NULL) # IMPORTANT: The ZZ_ tests need to be run last! @@ -113,3 +118,6 @@ AM_CFLAGS += $(CAIRO_CFLAGS) $(LIBUDEV_CFLAGS) $(GLIB_CFLAGS) gem_fence_thrash_CFLAGS = $(AM_CFLAGS) $(THREAD_CFLAGS) gem_fence_thrash_LDADD = $(LDADD) -lpthread + +gem_ctx_basic_CFLAGS = $(AM_CFLAGS) $(THREAD_CFLAGS) +gem_ctx_basic_LDADD = $(LDADD) -lpthread diff --git a/tests/gem_ctx_bad_destroy.c b/tests/gem_ctx_bad_destroy.c new file mode 100644 index 0000000..07407fc --- /dev/null +++ b/tests/gem_ctx_bad_destroy.c @@ -0,0 +1,85 @@ +/* + * Copyright ? 2012 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. + * + * Authors: + * Ben Widawsky <ben at bwidawsk.net> + * + */ + +/* + * Negative test cases for destroy contexts + */ + +#include <stdio.h> +#include <string.h> +#include "i915_drm.h" +#include "drmtest.h" + +static void handle_bad(int ret, int lerrno, int expected) +{ + if (ret != 0 && lerrno != expected) { + fprintf(stderr, "errno was %d, but should have been %d\n", + lerrno, expected); + exit(EXIT_FAILURE); + } else if (ret == 0) { + fprintf(stderr, "Command succeeded, but should have failed\n"); + exit(EXIT_FAILURE); + } +} + +int main(int argc, char *argv[]) +{ + struct drm_i915_gem_context_create create; + struct drm_i915_gem_context_destroy destroy; + int ret, fd; + + fd = drm_open_any(); + + ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &create); + if (ret != 0) { + fprintf(stderr, "%s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + + destroy.ctx_id = create.ctx_id; + /* Make sure a proper destroy works first */ + ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY, &destroy); + assert(ret == 0); + + /* try double destroy */ + ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY, &destroy); + handle_bad(ret, errno, EINVAL); + + /* destroy something random */ + destroy.ctx_id = 2; + ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY, &destroy); + handle_bad(ret, errno, EINVAL); + + /* Try to destroy the default context */ + destroy.ctx_id = 0; + ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY, &create); + handle_bad(ret, errno, EINVAL); + + close(fd); + + exit(EXIT_SUCCESS); +} diff --git a/tests/gem_ctx_bad_exec.c b/tests/gem_ctx_bad_exec.c new file mode 100644 index 0000000..6e050c0 --- /dev/null +++ b/tests/gem_ctx_bad_exec.c @@ -0,0 +1,103 @@ +/* + * Copyright ? 2012 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. + * + * Authors: + * Ben Widawsky <ben at bwidawsk.net> + * + */ + +/* + * Negative test cases: + * test we can't submit contexts to unsupported rings + */ + +#include <unistd.h> +#include <stdlib.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <assert.h> +#include <fcntl.h> +#include <inttypes.h> +#include <errno.h> +#include <sys/stat.h> +#include <sys/ioctl.h> +#include <sys/mman.h> +#include <sys/time.h> +#include "drm.h" +#include "i915_drm.h" +#include "drmtest.h" + +/* Copied from gem_exec_nop.c */ +static int exec(int fd, uint32_t handle, int ring, int ctx_id) +{ + struct drm_i915_gem_execbuffer2 execbuf; + struct drm_i915_gem_exec_object2 gem_exec; + int ret = 0; + + gem_exec.handle = handle; + gem_exec.relocation_count = 0; + gem_exec.relocs_ptr = 0; + gem_exec.alignment = 0; + gem_exec.offset = 0; + gem_exec.flags = 0; + gem_exec.rsvd1 = 0; + gem_exec.rsvd2 = 0; + + execbuf.buffers_ptr = (uintptr_t)&gem_exec; + execbuf.buffer_count = 1; + execbuf.batch_start_offset = 0; + execbuf.batch_len = 8; + execbuf.cliprects_ptr = 0; + execbuf.num_cliprects = 0; + execbuf.DR1 = 0; + execbuf.DR4 = 0; + execbuf.flags = ring; + i915_execbuffer2_set_context_id(execbuf, ctx_id); + execbuf.rsvd2 = 0; + + ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, + &execbuf); + gem_sync(fd, handle); + + return ret; +} + +#define MI_BATCH_BUFFER_END (0xA<<23) +int main(int argc, char *argv[]) +{ + uint32_t handle; + uint32_t batch[2] = {MI_BATCH_BUFFER_END}; + uint32_t ctx_id; + int fd; + fd = drm_open_any(); + + ctx_id = gem_context_create(fd); + + handle = gem_create(fd, 4096); + gem_write(fd, handle, 0, batch, sizeof(batch)); + assert(exec(fd, handle, I915_EXEC_RENDER, ctx_id) == 0); + assert(exec(fd, handle, I915_EXEC_BSD, ctx_id) != 0); + assert(exec(fd, handle, I915_EXEC_BLT, ctx_id) != 0); + + exit(EXIT_SUCCESS); +} diff --git a/tests/gem_ctx_basic.c b/tests/gem_ctx_basic.c new file mode 100644 index 0000000..8492c66 --- /dev/null +++ b/tests/gem_ctx_basic.c @@ -0,0 +1,158 @@ +/* + * Copyright ? 2011 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. + * + * Authors: + * Ben Widawsky <ben at bwidawsk.net> + * + */ + +/* + * This test is useful for finding memory and refcount leaks. + */ + +#include <pthread.h> +#include "rendercopy.h" + +/* options */ +int num_contexts = 10; +int uncontexted = 0; /* test only context create/destroy */ +int multiple_fds = 1; +int iter = 10000; + +/* globals */ +pthread_t *threads; +int devid; +int fd; +int use_rendercpy = 1; + +static void init_buffer(drm_intel_bufmgr *bufmgr, + struct scratch_buf *buf, + uint32_t size) +{ + buf->bo = drm_intel_bo_alloc(bufmgr, "", size, 4096); + buf->size = size; + assert(buf->bo); + buf->tiling = I915_TILING_NONE; + buf->stride = 4096; +} + +static void *work(void *arg) +{ + struct intel_batchbuffer *batch; + drm_intel_context *context; + drm_intel_bufmgr *bufmgr; + int td_fd; + int i; + + if (multiple_fds) + td_fd = fd = drm_open_any(); + else + td_fd = fd; + + assert(td_fd >= 0); + + bufmgr = drm_intel_bufmgr_gem_init(td_fd, 4096); + batch = intel_batchbuffer_alloc(bufmgr, devid); + context = drm_intel_gem_context_create(bufmgr); + + for (i = 0; i < iter; i++) { + struct scratch_buf src, dst; + uint32_t batch_len; + + init_buffer(bufmgr, &src, 4096); + init_buffer(bufmgr, &dst, 4096); + + + if (uncontexted) { + gen6_render_copyfunc(batch, &src, 0, 0, 0, 0, &dst, 0, 0); + } else { + int ret; + ret = drm_intel_bo_subdata(batch->bo, 0, 4096, batch->buffer); + assert(ret == 0); + intel_batchbuffer_flush_with_context(batch, context); + } + } + + drm_intel_gem_context_destroy(context); + intel_batchbuffer_free(batch); + drm_intel_bufmgr_destroy(bufmgr); + + if (multiple_fds) + close(td_fd); + + pthread_exit(NULL); +} + +static void parse(int argc, char *argv[]) +{ + int opt; + while ((opt = getopt(argc, argv, "i:c:n:muh?")) != -1) { + switch (opt) { + case 'i': + iter = atoi(optarg); + break; + case 'c': + num_contexts = atoi(optarg); + break; + case 'm': + multiple_fds = 1; + break; + case 'u': + uncontexted = 1; + break; + case 'h': + case '?': + default: + exit(EXIT_SUCCESS); + break; + } + } +} + +int main(int argc, char *argv[]) +{ + void *ret; + int i; + + fd = drm_open_any(); + devid = intel_get_drm_devid(fd); + + if (!IS_GEN6(devid)) + use_rendercpy = 0; + + parse(argc, argv); + + threads = calloc(num_contexts, sizeof(*threads)); + + for (i = 0; i < num_contexts; i++) + pthread_create(&threads[i], NULL, work, NULL); + + for (i = 0; i < num_contexts; i++) { + pthread_join(threads[i], &ret); + free(ret); + } + + free(threads); + close(fd); + + exit(EXIT_SUCCESS); +} diff --git a/tests/gem_ctx_create.c b/tests/gem_ctx_create.c new file mode 100644 index 0000000..9d0af0a --- /dev/null +++ b/tests/gem_ctx_create.c @@ -0,0 +1,53 @@ +/* + * Copyright ? 2012 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. + * + * Authors: + * Ben Widawsky <ben at bwidawsk.net> + * + */ + +#include <stdio.h> +#include <string.h> +#include "i915_drm.h" +#include "drmtest.h" + +int main(int argc, char *argv[]) +{ + int ret, fd; + struct drm_i915_gem_context_create create; + + create.ctx_id = rand(); + create.pad = rand(); + + fd = drm_open_any(); + + ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &create); + if (ret != 0) { + fprintf(stderr, "%s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + assert(create.ctx_id != 0); + + close(fd); + + exit(EXIT_SUCCESS); +} diff --git a/tests/gem_ctx_exec.c b/tests/gem_ctx_exec.c new file mode 100644 index 0000000..559d659 --- /dev/null +++ b/tests/gem_ctx_exec.c @@ -0,0 +1,105 @@ +/* + * Copyright ? 2012 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. + * + * Authors: + * Ben Widawsky <ben at bwidawsk.net> + * + */ + +/* + * This test covers basic context switch functionality + */ + +#include <unistd.h> +#include <stdlib.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <assert.h> +#include <fcntl.h> +#include <inttypes.h> +#include <errno.h> +#include <sys/stat.h> +#include <sys/ioctl.h> +#include <sys/mman.h> +#include <sys/time.h> +#include "drm.h" +#include "i915_drm.h" +#include "drmtest.h" + +/* Copied from gem_exec_nop.c */ +static int exec(int fd, uint32_t handle, int ring, int ctx_id) +{ + struct drm_i915_gem_execbuffer2 execbuf; + struct drm_i915_gem_exec_object2 gem_exec; + int ret = 0; + + gem_exec.handle = handle; + gem_exec.relocation_count = 0; + gem_exec.relocs_ptr = 0; + gem_exec.alignment = 0; + gem_exec.offset = 0; + gem_exec.flags = 0; + gem_exec.rsvd1 = 0; + gem_exec.rsvd2 = 0; + + execbuf.buffers_ptr = (uintptr_t)&gem_exec; + execbuf.buffer_count = 1; + execbuf.batch_start_offset = 0; + execbuf.batch_len = 8; + execbuf.cliprects_ptr = 0; + execbuf.num_cliprects = 0; + execbuf.DR1 = 0; + execbuf.DR4 = 0; + execbuf.flags = ring; + i915_execbuffer2_set_context_id(execbuf, ctx_id); + execbuf.rsvd2 = 0; + + ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, + &execbuf); + gem_sync(fd, handle); + + return ret; +} + +#define MI_BATCH_BUFFER_END (0xA<<23) +int main(int argc, char *argv[]) +{ + uint32_t handle; + uint32_t batch[2] = {0, MI_BATCH_BUFFER_END}; + uint32_t ctx_id; + int fd; + fd = drm_open_any(); + + ctx_id = gem_context_create(fd); + handle = gem_create(fd, 4096); + + gem_write(fd, handle, 0, batch, sizeof(batch)); + assert(exec(fd, handle, I915_EXEC_RENDER, ctx_id) == 0); + gem_context_destroy(fd, ctx_id); + + ctx_id = gem_context_create(fd); + assert(exec(fd, handle, I915_EXEC_RENDER, ctx_id) == 0); + gem_context_destroy(fd, ctx_id); + + exit(EXIT_SUCCESS); +} -- 1.7.10.4