Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008 Intel Corporation |
| 3 | * |
| 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: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | * IN THE SOFTWARE. |
| 22 | * |
| 23 | * Authors: |
| 24 | * Eric Anholt <eric@anholt.net> |
| 25 | * |
| 26 | */ |
| 27 | |
Thomas Wood | 804e11f | 2015-08-17 17:57:43 +0100 | [diff] [blame] | 28 | #include "igt.h" |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 29 | #include <stdlib.h> |
| 30 | #include <stdio.h> |
| 31 | #include <string.h> |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 32 | #include <fcntl.h> |
| 33 | #include <inttypes.h> |
| 34 | #include <errno.h> |
| 35 | #include <sys/stat.h> |
Eric Anholt | 0581738 | 2009-03-27 16:04:25 -0700 | [diff] [blame] | 36 | #include <sys/ioctl.h> |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 37 | #include "drm.h" |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 38 | |
Vinay Belgaumkar | 3a01e58 | 2017-09-14 11:09:58 -0700 | [diff] [blame] | 39 | IGT_TEST_DESCRIPTION("Tests for flink - a way to export a gem object by name"); |
| 40 | |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 41 | static void |
| 42 | test_flink(int fd) |
| 43 | { |
| 44 | struct drm_i915_gem_create create; |
| 45 | struct drm_gem_flink flink; |
Daniel Vetter | 8e46c38 | 2013-07-23 22:43:30 +0200 | [diff] [blame] | 46 | struct drm_gem_open open_struct; |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 47 | int ret; |
| 48 | |
Vinay Belgaumkar | 3a01e58 | 2017-09-14 11:09:58 -0700 | [diff] [blame] | 49 | igt_debug("Testing flink and open.\n"); |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 50 | |
| 51 | memset(&create, 0, sizeof(create)); |
| 52 | create.size = 16 * 1024; |
| 53 | ret = ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create); |
Matt Roper | 07be8fe | 2015-03-05 15:01:00 -0800 | [diff] [blame] | 54 | igt_assert_eq(ret, 0); |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 55 | |
| 56 | flink.handle = create.handle; |
| 57 | ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink); |
Matt Roper | 07be8fe | 2015-03-05 15:01:00 -0800 | [diff] [blame] | 58 | igt_assert_eq(ret, 0); |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 59 | |
Daniel Vetter | 8e46c38 | 2013-07-23 22:43:30 +0200 | [diff] [blame] | 60 | open_struct.name = flink.name; |
| 61 | ret = ioctl(fd, DRM_IOCTL_GEM_OPEN, &open_struct); |
Matt Roper | 07be8fe | 2015-03-05 15:01:00 -0800 | [diff] [blame] | 62 | igt_assert_eq(ret, 0); |
Daniel Vetter | 8344095 | 2013-08-13 12:35:58 +0200 | [diff] [blame] | 63 | igt_assert(open_struct.handle != 0); |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static void |
| 67 | test_double_flink(int fd) |
| 68 | { |
| 69 | struct drm_i915_gem_create create; |
| 70 | struct drm_gem_flink flink; |
| 71 | struct drm_gem_flink flink2; |
| 72 | int ret; |
| 73 | |
Vinay Belgaumkar | 3a01e58 | 2017-09-14 11:09:58 -0700 | [diff] [blame] | 74 | igt_debug("Testing repeated flink.\n"); |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 75 | |
| 76 | memset(&create, 0, sizeof(create)); |
| 77 | create.size = 16 * 1024; |
| 78 | ret = ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create); |
Matt Roper | 07be8fe | 2015-03-05 15:01:00 -0800 | [diff] [blame] | 79 | igt_assert_eq(ret, 0); |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 80 | |
| 81 | flink.handle = create.handle; |
| 82 | ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink); |
Matt Roper | 07be8fe | 2015-03-05 15:01:00 -0800 | [diff] [blame] | 83 | igt_assert_eq(ret, 0); |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 84 | |
| 85 | flink2.handle = create.handle; |
| 86 | ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink2); |
Matt Roper | 07be8fe | 2015-03-05 15:01:00 -0800 | [diff] [blame] | 87 | igt_assert_eq(ret, 0); |
Vinay Belgaumkar | 3a01e58 | 2017-09-14 11:09:58 -0700 | [diff] [blame] | 88 | |
| 89 | /* flinks for same gem object share the same name */ |
Daniel Vetter | 8344095 | 2013-08-13 12:35:58 +0200 | [diff] [blame] | 90 | igt_assert(flink2.name == flink.name); |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static void |
| 94 | test_bad_flink(int fd) |
| 95 | { |
| 96 | struct drm_gem_flink flink; |
| 97 | int ret; |
| 98 | |
Vinay Belgaumkar | 3a01e58 | 2017-09-14 11:09:58 -0700 | [diff] [blame] | 99 | igt_debug("Testing error return on bad flink ioctl.\n"); |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 100 | |
| 101 | flink.handle = 0x10101010; |
| 102 | ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink); |
Daniel Vetter | 8344095 | 2013-08-13 12:35:58 +0200 | [diff] [blame] | 103 | igt_assert(ret == -1 && errno == ENOENT); |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static void |
| 107 | test_bad_open(int fd) |
| 108 | { |
Daniel Vetter | 8e46c38 | 2013-07-23 22:43:30 +0200 | [diff] [blame] | 109 | struct drm_gem_open open_struct; |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 110 | int ret; |
| 111 | |
Vinay Belgaumkar | 3a01e58 | 2017-09-14 11:09:58 -0700 | [diff] [blame] | 112 | igt_debug("Testing error return on bad open ioctl.\n"); |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 113 | |
Daniel Vetter | 8e46c38 | 2013-07-23 22:43:30 +0200 | [diff] [blame] | 114 | open_struct.name = 0x10101010; |
| 115 | ret = ioctl(fd, DRM_IOCTL_GEM_OPEN, &open_struct); |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 116 | |
Daniel Vetter | 8344095 | 2013-08-13 12:35:58 +0200 | [diff] [blame] | 117 | igt_assert(ret == -1 && errno == ENOENT); |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Daniel Vetter | 6daae8b | 2012-12-01 13:07:45 +0100 | [diff] [blame] | 120 | static void |
| 121 | test_flink_lifetime(int fd) |
| 122 | { |
| 123 | struct drm_i915_gem_create create; |
| 124 | struct drm_gem_flink flink; |
Daniel Vetter | 8e46c38 | 2013-07-23 22:43:30 +0200 | [diff] [blame] | 125 | struct drm_gem_open open_struct; |
Daniel Vetter | 6daae8b | 2012-12-01 13:07:45 +0100 | [diff] [blame] | 126 | int ret, fd2; |
| 127 | |
Vinay Belgaumkar | 3a01e58 | 2017-09-14 11:09:58 -0700 | [diff] [blame] | 128 | igt_debug("Testing flink lifetime.\n"); |
Daniel Vetter | 6daae8b | 2012-12-01 13:07:45 +0100 | [diff] [blame] | 129 | |
Micah Fedke | c81d293 | 2015-07-22 21:54:02 +0000 | [diff] [blame] | 130 | fd2 = drm_open_driver(DRIVER_INTEL); |
Daniel Vetter | 6daae8b | 2012-12-01 13:07:45 +0100 | [diff] [blame] | 131 | |
| 132 | memset(&create, 0, sizeof(create)); |
| 133 | create.size = 16 * 1024; |
| 134 | ret = ioctl(fd2, DRM_IOCTL_I915_GEM_CREATE, &create); |
Matt Roper | 07be8fe | 2015-03-05 15:01:00 -0800 | [diff] [blame] | 135 | igt_assert_eq(ret, 0); |
Daniel Vetter | 6daae8b | 2012-12-01 13:07:45 +0100 | [diff] [blame] | 136 | |
| 137 | flink.handle = create.handle; |
| 138 | ret = ioctl(fd2, DRM_IOCTL_GEM_FLINK, &flink); |
Matt Roper | 07be8fe | 2015-03-05 15:01:00 -0800 | [diff] [blame] | 139 | igt_assert_eq(ret, 0); |
Daniel Vetter | 6daae8b | 2012-12-01 13:07:45 +0100 | [diff] [blame] | 140 | |
Vinay Belgaumkar | 3a01e58 | 2017-09-14 11:09:58 -0700 | [diff] [blame] | 141 | /* Open a second reference to the gem object with different fd */ |
Daniel Vetter | 8e46c38 | 2013-07-23 22:43:30 +0200 | [diff] [blame] | 142 | open_struct.name = flink.name; |
| 143 | ret = ioctl(fd, DRM_IOCTL_GEM_OPEN, &open_struct); |
Matt Roper | 07be8fe | 2015-03-05 15:01:00 -0800 | [diff] [blame] | 144 | igt_assert_eq(ret, 0); |
Daniel Vetter | 8344095 | 2013-08-13 12:35:58 +0200 | [diff] [blame] | 145 | igt_assert(open_struct.handle != 0); |
Daniel Vetter | 6daae8b | 2012-12-01 13:07:45 +0100 | [diff] [blame] | 146 | |
| 147 | close(fd2); |
Micah Fedke | c81d293 | 2015-07-22 21:54:02 +0000 | [diff] [blame] | 148 | fd2 = drm_open_driver(DRIVER_INTEL); |
Daniel Vetter | 6daae8b | 2012-12-01 13:07:45 +0100 | [diff] [blame] | 149 | |
Vinay Belgaumkar | 3a01e58 | 2017-09-14 11:09:58 -0700 | [diff] [blame] | 150 | /* Flink name remains valid due to the second reference */ |
Daniel Vetter | 8e46c38 | 2013-07-23 22:43:30 +0200 | [diff] [blame] | 151 | open_struct.name = flink.name; |
| 152 | ret = ioctl(fd2, DRM_IOCTL_GEM_OPEN, &open_struct); |
Matt Roper | 07be8fe | 2015-03-05 15:01:00 -0800 | [diff] [blame] | 153 | igt_assert_eq(ret, 0); |
Daniel Vetter | 8344095 | 2013-08-13 12:35:58 +0200 | [diff] [blame] | 154 | igt_assert(open_struct.handle != 0); |
Daniel Vetter | 6daae8b | 2012-12-01 13:07:45 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Daniel Vetter | b3880d3 | 2013-08-14 18:02:46 +0200 | [diff] [blame] | 157 | int fd; |
| 158 | |
Daniel Vetter | 071e9ca | 2013-10-31 16:23:26 +0100 | [diff] [blame] | 159 | igt_main |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 160 | { |
Daniel Vetter | b3880d3 | 2013-08-14 18:02:46 +0200 | [diff] [blame] | 161 | igt_fixture |
Micah Fedke | c81d293 | 2015-07-22 21:54:02 +0000 | [diff] [blame] | 162 | fd = drm_open_driver(DRIVER_INTEL); |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 163 | |
Daniel Vetter | 1caaf0a | 2013-08-12 12:17:35 +0200 | [diff] [blame] | 164 | igt_subtest("basic") |
Daniel Vetter | 2a9e128 | 2012-11-28 12:17:40 +0100 | [diff] [blame] | 165 | test_flink(fd); |
Daniel Vetter | 1caaf0a | 2013-08-12 12:17:35 +0200 | [diff] [blame] | 166 | igt_subtest("double-flink") |
Daniel Vetter | 2a9e128 | 2012-11-28 12:17:40 +0100 | [diff] [blame] | 167 | test_double_flink(fd); |
Daniel Vetter | 1caaf0a | 2013-08-12 12:17:35 +0200 | [diff] [blame] | 168 | igt_subtest("bad-flink") |
Daniel Vetter | 2a9e128 | 2012-11-28 12:17:40 +0100 | [diff] [blame] | 169 | test_bad_flink(fd); |
Daniel Vetter | 1caaf0a | 2013-08-12 12:17:35 +0200 | [diff] [blame] | 170 | igt_subtest("bad-open") |
Daniel Vetter | 2a9e128 | 2012-11-28 12:17:40 +0100 | [diff] [blame] | 171 | test_bad_open(fd); |
Vinay Belgaumkar | 3a01e58 | 2017-09-14 11:09:58 -0700 | [diff] [blame] | 172 | |
| 173 | /* Flink lifetime is limited to that of the gem object it points to */ |
Daniel Vetter | 1caaf0a | 2013-08-12 12:17:35 +0200 | [diff] [blame] | 174 | igt_subtest("flink-lifetime") |
Daniel Vetter | 573d59d | 2012-12-15 01:05:33 +0100 | [diff] [blame] | 175 | test_flink_lifetime(fd); |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 176 | } |