summaryrefslogtreecommitdiff
path: root/tests/vmwgfx/vmw_ref_count.c
blob: 92d49dbd04ad11c626197c6a868fbbeab68afb83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// SPDX-License-Identifier: GPL-2.0 OR MIT
/**********************************************************
 * Copyright 2021-2023 VMware, Inc.
 *
 * 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 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 "igt_vmwgfx.h"

IGT_TEST_DESCRIPTION("Perform tests related to vmwgfx's ref_count codepaths.");

static uint32 data[10] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };

static void write_to_mob(int fd, struct vmw_mob *mob)
{
	void *write_data;

	write_data = vmw_ioctl_mob_map(fd, mob);
	memcpy(write_data, data, sizeof(data));
	vmw_ioctl_mob_unmap(mob);
}

static bool verify_mob_data(int fd, struct vmw_mob *mob)
{
	uint32 *read_data;
	void *readback;
	uint32 i;
	bool data_is_equal = true;

	read_data = malloc(mob->size);

	readback = vmw_ioctl_mob_map(fd, mob);
	memcpy(read_data, readback, sizeof(data));
	vmw_ioctl_mob_unmap(mob);

	for (i = 0; i < ARRAY_SIZE(data); i++) {
		if (read_data[i] != data[i]) {
			data_is_equal = false;
			break;
		}
	}

	free(read_data);
	return data_is_equal;
}

static struct vmw_surface *
create_and_write_shareable_surface(int32 fd, SVGA3dSize surface_size)
{
	struct vmw_mob mob = { 0 };
	struct vmw_surface *surface;

	surface = vmw_ioctl_create_surface_full(
		fd, SVGA3D_SURFACE_HINT_RENDERTARGET, SVGA3D_BUFFER, 0,
		SVGA3D_MS_PATTERN_NONE, SVGA3D_MS_QUALITY_NONE,
		SVGA3D_TEX_FILTER_NONE, 1, 1, surface_size, NULL,
		drm_vmw_surface_flag_shareable);

	mob.handle = surface->base.buffer_handle;
	mob.map_handle = surface->base.buffer_map_handle;
	mob.size = surface->base.buffer_size;

	write_to_mob(fd, &mob);

	return surface;
}

static bool ref_surface_and_check_contents(int32 fd, uint32 surface_handle)
{
	struct vmw_surface surface;
	struct vmw_mob mob = { 0 };

	surface = vmw_ioctl_surface_ref(fd, surface_handle,
					DRM_VMW_HANDLE_LEGACY);

	mob.handle = surface.base.handle;
	mob.size = surface.base.buffer_size;
	mob.map_handle = surface.base.buffer_map_handle;

	return verify_mob_data(fd, &mob);
}

igt_main
{
	int32 fd1, fd2;
	const uint32 size = sizeof(data);
	SVGA3dSize surface_size = { .width = size, .height = 1, .depth = 1 };

	igt_fixture
	{
		fd1 = drm_open_driver_render(DRIVER_VMWGFX);
		fd2 = drm_open_driver_render(DRIVER_VMWGFX);
		igt_require(fd1 != -1);
		igt_require(fd2 != -1);
	}

	igt_describe("Test prime transfers with explicit mobs.");
	igt_subtest("surface_prime_transfer_explicit_mob")
	{
		struct vmw_mob *mob;
		struct vmw_surface *surface;
		int32 surface_fd;
		uint32 surface_handle;

		mob = vmw_ioctl_mob_create(fd1, size);
		surface = vmw_ioctl_create_surface_full(
			fd1, SVGA3D_SURFACE_HINT_RENDERTARGET, SVGA3D_BUFFER, 0,
			SVGA3D_MS_PATTERN_NONE, SVGA3D_MS_QUALITY_NONE,
			SVGA3D_TEX_FILTER_NONE, 1, 1, surface_size, mob,
			drm_vmw_surface_flag_shareable);

		write_to_mob(fd1, mob);

		surface_fd =
			prime_handle_to_fd_for_mmap(fd1, surface->base.handle);

		vmw_ioctl_mob_close_handle(fd1, mob);
		vmw_ioctl_surface_unref(fd1, surface);

		surface_handle = prime_fd_to_handle(fd2, surface_fd);
		close(surface_fd);

		igt_assert(ref_surface_and_check_contents(fd2, surface_handle));
	}

	igt_describe("Test prime transfers with implicit mobs.");
	igt_subtest("surface_prime_transfer_implicit_mob")
	{
		struct vmw_surface *surface;
		int32 surface_fd;
		uint32 surface_handle;

		surface = create_and_write_shareable_surface(fd1, surface_size);

		surface_fd =
			prime_handle_to_fd_for_mmap(fd1, surface->base.handle);

		vmw_ioctl_surface_unref(fd1, surface);

		surface_handle = prime_fd_to_handle(fd2, surface_fd);
		close(surface_fd);

		igt_assert(ref_surface_and_check_contents(fd2, surface_handle));
	}

	igt_describe("Test prime transfers with a fd dup.");
	igt_subtest("surface_prime_transfer_fd_dup")
	{
		int32 surface_fd1, surface_fd2;
		uint32 surface_handle;
		struct vmw_surface *surface;

		surface = create_and_write_shareable_surface(fd1, surface_size);

		surface_fd1 =
			prime_handle_to_fd_for_mmap(fd1, surface->base.handle);
		vmw_ioctl_surface_unref(fd1, surface);

		surface_fd2 = dup(surface_fd1);
		close(surface_fd1);

		surface_handle = prime_fd_to_handle(fd2, surface_fd2);
		close(surface_fd2);

		igt_assert(ref_surface_and_check_contents(fd2, surface_handle));
	}

	igt_describe("Test prime lifetime with 2 surfaces.");
	igt_subtest("surface_prime_transfer_two_surfaces")
	{
		int32 surface_fd;
		uint32 surface_handle1, surface_handle2;
		struct vmw_surface *surface1, *surface2;

		surface1 =
			create_and_write_shareable_surface(fd1, surface_size);
		surface2 =
			create_and_write_shareable_surface(fd1, surface_size);

		surface_fd =
			prime_handle_to_fd_for_mmap(fd1, surface1->base.handle);
		vmw_ioctl_surface_unref(fd1, surface1);

		surface_handle1 = prime_fd_to_handle(fd2, surface_fd);
		close(surface_fd);

		surface_fd =
			prime_handle_to_fd_for_mmap(fd1, surface2->base.handle);
		vmw_ioctl_surface_unref(fd1, surface2);

		surface_handle2 = prime_fd_to_handle(fd2, surface_fd);
		close(surface_fd);

		igt_assert(
			ref_surface_and_check_contents(fd2, surface_handle1));
		igt_assert(
			ref_surface_and_check_contents(fd2, surface_handle2));
	}

	igt_describe("Test prime transfers with multiple handles.");
	igt_subtest("surface_prime_transfer_single_surface_multiple_handle")
	{
		int32 surface_fd;
		uint32 surface_handle_old;
		uint32 surface_handle1, surface_handle2, surface_handle3;
		struct vmw_surface *surface;

		surface = create_and_write_shareable_surface(fd1, surface_size);
		surface_handle_old = surface->base.handle;

		surface_fd =
			prime_handle_to_fd_for_mmap(fd1, surface->base.handle);
		vmw_ioctl_surface_unref(fd1, surface);

		surface_handle1 = prime_fd_to_handle(fd1, surface_fd);
		surface_handle2 = prime_fd_to_handle(fd2, surface_fd);
		surface_handle3 = prime_fd_to_handle(fd2, surface_fd);
		close(surface_fd);

		igt_assert_eq_u32(surface_handle_old, surface_handle1);
		igt_assert_eq_u32(surface_handle2, surface_handle3);

		igt_assert(
			ref_surface_and_check_contents(fd1, surface_handle1));
		igt_assert(
			ref_surface_and_check_contents(fd2, surface_handle2));
	}

	igt_describe("Test repeated unrefs on a mob.");
	igt_subtest("mob_repeated_unref")
	{
		struct vmw_mob *mob;
		int i = 0;

		mob = vmw_ioctl_mob_create(fd1, size);
		write_to_mob(fd1, mob);

		/* Shouldn't crash on multiple invocations */
		for (i = 0; i < 3; i++) {
			struct drm_vmw_handle_close_arg arg = {
				.handle = mob->handle
			};
			drmCommandWrite(fd1, DRM_VMW_HANDLE_CLOSE, &arg,
					      sizeof(arg));
		}
		free(mob);
	}

	igt_describe("Test repeated unrefs on a surface.");
	igt_subtest("surface_repeated_unref")
	{
		struct vmw_surface *surface;
		int i = 0;

		surface = vmw_ioctl_create_surface_full(
			fd1, SVGA3D_SURFACE_HINT_RENDERTARGET, SVGA3D_BUFFER, 0,
			SVGA3D_MS_PATTERN_NONE, SVGA3D_MS_QUALITY_NONE,
			SVGA3D_TEX_FILTER_NONE, 1, 1, surface_size, NULL,
			drm_vmw_surface_flag_shareable);

		/* Shouldn't crash on multiple invocations */
		for (i = 0; i < 3; i++) {
			struct drm_vmw_surface_arg s_arg = {
				.sid = surface->base.handle,
				.handle_type = DRM_VMW_HANDLE_LEGACY
			};
			drmCommandWrite(fd1, DRM_VMW_UNREF_SURFACE, &s_arg,
					sizeof(s_arg));
		}
		free(surface);
	}

	igt_describe("Test unref on a refed surface.");
	igt_subtest("surface_alloc_ref_unref")
	{
		struct vmw_surface *surface;
		struct vmw_surface ref_surface;
		struct vmw_mob readback_mob = { 0 };

		surface = create_and_write_shareable_surface(fd1, surface_size);

		ref_surface = vmw_ioctl_surface_ref(fd1, surface->base.handle,
						    DRM_VMW_HANDLE_LEGACY);

		vmw_ioctl_surface_unref(fd1, surface);

		readback_mob.handle = ref_surface.base.handle;
		readback_mob.size = ref_surface.base.buffer_size;
		readback_mob.map_handle = ref_surface.base.buffer_map_handle;

		igt_assert(verify_mob_data(fd1, &readback_mob));
	}

	igt_fixture
	{
		drm_close_driver(fd1);
		drm_close_driver(fd2);
	}
}