blob: 48b0b8b1bbba1d98d8a472f1090f5d9f61bc782a [file] [log] [blame]
Eric Anholt895a4152009-03-26 18:47:42 -07001/*
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 Wood804e11f2015-08-17 17:57:43 +010028#include "igt.h"
Eric Anholt895a4152009-03-26 18:47:42 -070029#include <stdlib.h>
30#include <stdio.h>
31#include <string.h>
Eric Anholt895a4152009-03-26 18:47:42 -070032#include <fcntl.h>
33#include <inttypes.h>
34#include <errno.h>
35#include <sys/stat.h>
Eric Anholt05817382009-03-27 16:04:25 -070036#include <sys/ioctl.h>
Eric Anholt895a4152009-03-26 18:47:42 -070037#include "drm.h"
Eric Anholt895a4152009-03-26 18:47:42 -070038
Vinay Belgaumkar3a01e582017-09-14 11:09:58 -070039IGT_TEST_DESCRIPTION("Tests for flink - a way to export a gem object by name");
40
Eric Anholt895a4152009-03-26 18:47:42 -070041static void
42test_flink(int fd)
43{
44 struct drm_i915_gem_create create;
45 struct drm_gem_flink flink;
Daniel Vetter8e46c382013-07-23 22:43:30 +020046 struct drm_gem_open open_struct;
Eric Anholt895a4152009-03-26 18:47:42 -070047 int ret;
48
Vinay Belgaumkar3a01e582017-09-14 11:09:58 -070049 igt_debug("Testing flink and open.\n");
Eric Anholt895a4152009-03-26 18:47:42 -070050
51 memset(&create, 0, sizeof(create));
52 create.size = 16 * 1024;
53 ret = ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
Matt Roper07be8fe2015-03-05 15:01:00 -080054 igt_assert_eq(ret, 0);
Eric Anholt895a4152009-03-26 18:47:42 -070055
56 flink.handle = create.handle;
57 ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
Matt Roper07be8fe2015-03-05 15:01:00 -080058 igt_assert_eq(ret, 0);
Eric Anholt895a4152009-03-26 18:47:42 -070059
Daniel Vetter8e46c382013-07-23 22:43:30 +020060 open_struct.name = flink.name;
61 ret = ioctl(fd, DRM_IOCTL_GEM_OPEN, &open_struct);
Matt Roper07be8fe2015-03-05 15:01:00 -080062 igt_assert_eq(ret, 0);
Daniel Vetter83440952013-08-13 12:35:58 +020063 igt_assert(open_struct.handle != 0);
Eric Anholt895a4152009-03-26 18:47:42 -070064}
65
66static void
67test_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 Belgaumkar3a01e582017-09-14 11:09:58 -070074 igt_debug("Testing repeated flink.\n");
Eric Anholt895a4152009-03-26 18:47:42 -070075
76 memset(&create, 0, sizeof(create));
77 create.size = 16 * 1024;
78 ret = ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
Matt Roper07be8fe2015-03-05 15:01:00 -080079 igt_assert_eq(ret, 0);
Eric Anholt895a4152009-03-26 18:47:42 -070080
81 flink.handle = create.handle;
82 ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
Matt Roper07be8fe2015-03-05 15:01:00 -080083 igt_assert_eq(ret, 0);
Eric Anholt895a4152009-03-26 18:47:42 -070084
85 flink2.handle = create.handle;
86 ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink2);
Matt Roper07be8fe2015-03-05 15:01:00 -080087 igt_assert_eq(ret, 0);
Vinay Belgaumkar3a01e582017-09-14 11:09:58 -070088
89 /* flinks for same gem object share the same name */
Daniel Vetter83440952013-08-13 12:35:58 +020090 igt_assert(flink2.name == flink.name);
Eric Anholt895a4152009-03-26 18:47:42 -070091}
92
93static void
94test_bad_flink(int fd)
95{
96 struct drm_gem_flink flink;
97 int ret;
98
Vinay Belgaumkar3a01e582017-09-14 11:09:58 -070099 igt_debug("Testing error return on bad flink ioctl.\n");
Eric Anholt895a4152009-03-26 18:47:42 -0700100
101 flink.handle = 0x10101010;
102 ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
Daniel Vetter83440952013-08-13 12:35:58 +0200103 igt_assert(ret == -1 && errno == ENOENT);
Eric Anholt895a4152009-03-26 18:47:42 -0700104}
105
106static void
107test_bad_open(int fd)
108{
Daniel Vetter8e46c382013-07-23 22:43:30 +0200109 struct drm_gem_open open_struct;
Eric Anholt895a4152009-03-26 18:47:42 -0700110 int ret;
111
Vinay Belgaumkar3a01e582017-09-14 11:09:58 -0700112 igt_debug("Testing error return on bad open ioctl.\n");
Eric Anholt895a4152009-03-26 18:47:42 -0700113
Daniel Vetter8e46c382013-07-23 22:43:30 +0200114 open_struct.name = 0x10101010;
115 ret = ioctl(fd, DRM_IOCTL_GEM_OPEN, &open_struct);
Eric Anholt895a4152009-03-26 18:47:42 -0700116
Daniel Vetter83440952013-08-13 12:35:58 +0200117 igt_assert(ret == -1 && errno == ENOENT);
Eric Anholt895a4152009-03-26 18:47:42 -0700118}
119
Daniel Vetter6daae8b2012-12-01 13:07:45 +0100120static void
121test_flink_lifetime(int fd)
122{
123 struct drm_i915_gem_create create;
124 struct drm_gem_flink flink;
Daniel Vetter8e46c382013-07-23 22:43:30 +0200125 struct drm_gem_open open_struct;
Daniel Vetter6daae8b2012-12-01 13:07:45 +0100126 int ret, fd2;
127
Vinay Belgaumkar3a01e582017-09-14 11:09:58 -0700128 igt_debug("Testing flink lifetime.\n");
Daniel Vetter6daae8b2012-12-01 13:07:45 +0100129
Micah Fedkec81d2932015-07-22 21:54:02 +0000130 fd2 = drm_open_driver(DRIVER_INTEL);
Daniel Vetter6daae8b2012-12-01 13:07:45 +0100131
132 memset(&create, 0, sizeof(create));
133 create.size = 16 * 1024;
134 ret = ioctl(fd2, DRM_IOCTL_I915_GEM_CREATE, &create);
Matt Roper07be8fe2015-03-05 15:01:00 -0800135 igt_assert_eq(ret, 0);
Daniel Vetter6daae8b2012-12-01 13:07:45 +0100136
137 flink.handle = create.handle;
138 ret = ioctl(fd2, DRM_IOCTL_GEM_FLINK, &flink);
Matt Roper07be8fe2015-03-05 15:01:00 -0800139 igt_assert_eq(ret, 0);
Daniel Vetter6daae8b2012-12-01 13:07:45 +0100140
Vinay Belgaumkar3a01e582017-09-14 11:09:58 -0700141 /* Open a second reference to the gem object with different fd */
Daniel Vetter8e46c382013-07-23 22:43:30 +0200142 open_struct.name = flink.name;
143 ret = ioctl(fd, DRM_IOCTL_GEM_OPEN, &open_struct);
Matt Roper07be8fe2015-03-05 15:01:00 -0800144 igt_assert_eq(ret, 0);
Daniel Vetter83440952013-08-13 12:35:58 +0200145 igt_assert(open_struct.handle != 0);
Daniel Vetter6daae8b2012-12-01 13:07:45 +0100146
147 close(fd2);
Micah Fedkec81d2932015-07-22 21:54:02 +0000148 fd2 = drm_open_driver(DRIVER_INTEL);
Daniel Vetter6daae8b2012-12-01 13:07:45 +0100149
Vinay Belgaumkar3a01e582017-09-14 11:09:58 -0700150 /* Flink name remains valid due to the second reference */
Daniel Vetter8e46c382013-07-23 22:43:30 +0200151 open_struct.name = flink.name;
152 ret = ioctl(fd2, DRM_IOCTL_GEM_OPEN, &open_struct);
Matt Roper07be8fe2015-03-05 15:01:00 -0800153 igt_assert_eq(ret, 0);
Daniel Vetter83440952013-08-13 12:35:58 +0200154 igt_assert(open_struct.handle != 0);
Daniel Vetter6daae8b2012-12-01 13:07:45 +0100155}
156
Daniel Vetterb3880d32013-08-14 18:02:46 +0200157int fd;
158
Daniel Vetter071e9ca2013-10-31 16:23:26 +0100159igt_main
Eric Anholt895a4152009-03-26 18:47:42 -0700160{
Daniel Vetterb3880d32013-08-14 18:02:46 +0200161 igt_fixture
Micah Fedkec81d2932015-07-22 21:54:02 +0000162 fd = drm_open_driver(DRIVER_INTEL);
Eric Anholt895a4152009-03-26 18:47:42 -0700163
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200164 igt_subtest("basic")
Daniel Vetter2a9e1282012-11-28 12:17:40 +0100165 test_flink(fd);
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200166 igt_subtest("double-flink")
Daniel Vetter2a9e1282012-11-28 12:17:40 +0100167 test_double_flink(fd);
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200168 igt_subtest("bad-flink")
Daniel Vetter2a9e1282012-11-28 12:17:40 +0100169 test_bad_flink(fd);
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200170 igt_subtest("bad-open")
Daniel Vetter2a9e1282012-11-28 12:17:40 +0100171 test_bad_open(fd);
Vinay Belgaumkar3a01e582017-09-14 11:09:58 -0700172
173 /* Flink lifetime is limited to that of the gem object it points to */
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200174 igt_subtest("flink-lifetime")
Daniel Vetter573d59d2012-12-15 01:05:33 +0100175 test_flink_lifetime(fd);
Eric Anholt895a4152009-03-26 18:47:42 -0700176}