vgem_basic.c 9.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright © 2016 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 "igt.h"
25
#include "igt_kmod.h"
26
#include "igt_vgem.h"
27 28
#include "igt_debugfs.h"
#include "igt_sysfs.h"
29 30

#include <sys/mman.h>
31
#include <sys/poll.h>
32 33
#include <sys/stat.h>
#include <dirent.h>
34 35 36

IGT_TEST_DESCRIPTION("Basic sanity check of Virtual GEM module (vGEM).");

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
static int __gem_setversion(int fd, drm_set_version_t *sv)
{
	int err;

	err = 0;
	if (igt_ioctl(fd, DRM_IOCTL_SET_VERSION, sv))
		err = -errno;
	errno = 0;

	return err;
}

static void test_setversion(int fd)
{
	drm_set_version_t sv;

	memset(&sv, 0, sizeof(sv));
	sv.drm_di_major = 1; /* must be equal to DRM_IF_MAJOR */
	sv.drm_di_minor = 4; /* must be less than DRM_IF_MINOR */
	sv.drm_dd_major = -1; /* don't care */
	sv.drm_dd_minor = -1; /* don't care */
	igt_assert_eq(__gem_setversion(fd, &sv), 0);

	igt_info("vgem DRM interface v%d.%d, device v%d.%d\n",
		 sv.drm_di_major, sv.drm_di_minor,
		 sv.drm_dd_major, sv.drm_dd_minor);
}

65 66 67 68 69 70
static void test_client(int fd)
{
	close(drm_open_driver(DRIVER_VGEM));
	close(drm_open_driver_render(DRIVER_VGEM));
}

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
static void test_create(int fd)
{
	struct vgem_bo bo;

	bo.width = 0;
	bo.height = 0;
	bo.bpp = 0;
	igt_assert_eq(__vgem_create(fd, &bo), -EINVAL);

	bo.width = 1;
	bo.height = 1;
	bo.bpp = 1;
	vgem_create(fd, &bo);
	igt_assert_eq(bo.size, 4096);
	gem_close(fd, bo.handle);

	bo.width = 1024;
	bo.height = 1024;
	bo.bpp = 8;
	vgem_create(fd, &bo);
	igt_assert_eq(bo.size, 1<<20);
	gem_close(fd, bo.handle);

	bo.width = 1<<15;
	bo.height = 1<<15;
	bo.bpp = 16;
	vgem_create(fd, &bo);
	igt_assert_eq(bo.size, 1<<31);
	gem_close(fd, bo.handle);
}

static void test_mmap(int fd)
{
	struct vgem_bo bo;
	uint32_t *ptr;

	bo.width = 1024;
	bo.height = 1024;
	bo.bpp = 32;
	vgem_create(fd, &bo);

	ptr = vgem_mmap(fd, &bo, PROT_WRITE);
	gem_close(fd, bo.handle);

	for (int page = 0; page < bo.size >> 12; page++)
		ptr[page] = 0;

	munmap(ptr, bo.size);
}

static bool has_prime_import(int fd)
{
	uint64_t value;

	if (drmGetCap(fd, DRM_CAP_PRIME, &value))
		return false;

	return value & DRM_PRIME_CAP_IMPORT;
}

static void test_dmabuf_export(int fd)
{
	struct vgem_bo bo;
	uint32_t handle;
	int other;
	int dmabuf;

	other = drm_open_driver(DRIVER_ANY);
	igt_require(has_prime_import(other));

	bo.width = 1024;
	bo.height = 1;
	bo.bpp = 32;

	vgem_create(fd, &bo);
	dmabuf = prime_handle_to_fd(fd, bo.handle);
	gem_close(fd, bo.handle);

	handle = prime_fd_to_handle(other, dmabuf);
	close(dmabuf);
	gem_close(other, handle);
	close(other);
}

static void test_dmabuf_mmap(int fd)
{
	struct vgem_bo bo;
	uint32_t *ptr;
	int export;

	bo.width = 1024;
	bo.height = 1024;
	bo.bpp = 32;
	vgem_create(fd, &bo);

	export = prime_handle_to_fd_for_mmap(fd, bo.handle);
	ptr = mmap(NULL, bo.size, PROT_WRITE, MAP_SHARED, export, 0);
	close(export);
	igt_assert(ptr != MAP_FAILED);

	for (int page = 0; page < bo.size >> 12; page++)
		ptr[page] = page;
	munmap(ptr, bo.size);

	ptr = vgem_mmap(fd, &bo, PROT_READ);
	gem_close(fd, bo.handle);

	for (int page = 0; page < bo.size >> 12; page++)
		igt_assert_eq(ptr[page], page);
	munmap(ptr, bo.size);
}

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
static bool prime_busy(int fd, bool excl)
{
	struct pollfd pfd = { .fd = fd, .events = excl ? POLLOUT : POLLIN };
	return poll(&pfd, 1, 0) == 0;
}

static void test_dmabuf_fence(int fd)
{
	struct vgem_bo bo;
	int dmabuf;
	uint32_t fence;

	bo.width = 1024;
	bo.height = 1;
	bo.bpp = 32;
	vgem_create(fd, &bo);

	/* export, then fence */

	dmabuf = prime_handle_to_fd(fd, bo.handle);

204
	fence = vgem_fence_attach(fd, &bo, 0);
205 206 207 208 209 210 211
	igt_assert(!prime_busy(dmabuf, false));
	igt_assert(prime_busy(dmabuf, true));

	vgem_fence_signal(fd, fence);
	igt_assert(!prime_busy(dmabuf, false));
	igt_assert(!prime_busy(dmabuf, true));

212
	fence = vgem_fence_attach(fd, &bo, VGEM_FENCE_WRITE);
213 214 215 216 217 218 219
	igt_assert(prime_busy(dmabuf, false));
	igt_assert(prime_busy(dmabuf, true));

	vgem_fence_signal(fd, fence);
	igt_assert(!prime_busy(dmabuf, false));
	igt_assert(!prime_busy(dmabuf, true));

220
	close(dmabuf);
221 222 223 224 225 226 227 228 229 230 231 232 233 234
	gem_close(fd, bo.handle);
}

static void test_dmabuf_fence_before(int fd)
{
	struct vgem_bo bo;
	int dmabuf;
	uint32_t fence;

	bo.width = 1024;
	bo.height = 1;
	bo.bpp = 32;
	vgem_create(fd, &bo);

235
	fence = vgem_fence_attach(fd, &bo, 0);
236 237 238 239 240 241 242 243 244
	dmabuf = prime_handle_to_fd(fd, bo.handle);

	igt_assert(!prime_busy(dmabuf, false));
	igt_assert(prime_busy(dmabuf, true));

	vgem_fence_signal(fd, fence);
	igt_assert(!prime_busy(dmabuf, false));
	igt_assert(!prime_busy(dmabuf, true));

245
	close(dmabuf);
246
	gem_close(fd, bo.handle);
247

248 249
	vgem_create(fd, &bo);

250
	fence = vgem_fence_attach(fd, &bo, VGEM_FENCE_WRITE);
251 252 253 254 255 256 257 258
	dmabuf = prime_handle_to_fd(fd, bo.handle);
	igt_assert(prime_busy(dmabuf, false));
	igt_assert(prime_busy(dmabuf, true));

	vgem_fence_signal(fd, fence);
	igt_assert(!prime_busy(dmabuf, false));
	igt_assert(!prime_busy(dmabuf, true));

259
	close(dmabuf);
260 261 262
	gem_close(fd, bo.handle);
}

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
static void test_sysfs_read(int fd)
{
	int dir = igt_sysfs_open(fd, NULL);
	DIR *dirp = fdopendir(dir);
	struct dirent *de;

	while ((de = readdir(dirp))) {
		struct stat st;

		if (*de->d_name == '.')
			continue;

		if (fstatat(dir, de->d_name, &st, 0))
			continue;

		if (S_ISDIR(st.st_mode))
			continue;

		igt_debug("Reading %s\n", de->d_name);
		igt_set_timeout(1, "vgem sysfs read stalled");
		free(igt_sysfs_get(dir, de->d_name));
		igt_reset_timeout();
	}

	closedir(dirp);
	close(dir);
}

static void test_debugfs_read(int fd)
{
	int dir = igt_debugfs_dir(fd);
	DIR *dirp = fdopendir(dir);
	struct dirent *de;

297
	igt_assert(dirp);
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
	while ((de = readdir(dirp))) {
		struct stat st;

		if (*de->d_name == '.')
			continue;

		if (fstatat(dir, de->d_name, &st, 0))
			continue;

		if (S_ISDIR(st.st_mode))
			continue;

		igt_debug("Reading %s\n", de->d_name);
		igt_set_timeout(1, "vgem debugfs read stalled");
		free(igt_sysfs_get(dir, de->d_name));
		igt_reset_timeout();
	}

	closedir(dirp);
	close(dir);
}

320 321
static int module_unload(void)
{
322
	return igt_kmod_unload("vgem", 0);
323 324 325 326 327 328 329 330
}

static void test_unload(void)
{
	struct vgem_bo bo;
	int vgem, dmabuf;
	uint32_t *ptr;

331 332 333 334
	/* Load and unload vgem just to make sure it exists */
	vgem = __drm_open_driver(DRIVER_VGEM);
	igt_require(vgem != -1);
	close(vgem);
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
	igt_require(module_unload() == 0);

	vgem = __drm_open_driver(DRIVER_VGEM);
	igt_assert(vgem != -1);

	/* The driver should stop the module from unloading */
	igt_assert_f(module_unload() != 0,
		     "open(//dev/vgem) should keep the module alive\n");

	bo.width = 1024;
	bo.height = 1;
	bo.bpp = 32;
	vgem_create(vgem, &bo);
	close(vgem);

	/* Closing the driver should clear all normal references */
	igt_assert_f(module_unload() == 0,
		     "No open(/dev/vgem), should be able to unload\n");

	vgem = __drm_open_driver(DRIVER_VGEM);
	igt_assert(vgem != -1);
	bo.width = 1024;
	bo.height = 1;
	bo.bpp = 32;
	vgem_create(vgem, &bo);
	dmabuf = prime_handle_to_fd(vgem, bo.handle);
	close(vgem);

	/* A dmabuf should prevent module unload. */
	igt_assert_f(module_unload() != 0,
		     "A dmabuf should keep the module alive\n");

	close(dmabuf);
	igt_assert_f(module_unload() == 0,
		     "No open dmabuf, should be able to unload\n");

	vgem = __drm_open_driver(DRIVER_VGEM);
	igt_assert(vgem != -1);
	bo.width = 1024;
	bo.height = 1;
	bo.bpp = 32;
	vgem_create(vgem, &bo);
	dmabuf = prime_handle_to_fd_for_mmap(vgem, bo.handle);
	close(vgem);

	ptr = mmap(NULL, bo.size, PROT_WRITE, MAP_SHARED, dmabuf, 0);
	igt_assert(ptr != MAP_FAILED);
	close(dmabuf);

	/* Although closed, the mmap should keep the dmabuf/module alive */
385 386
	igt_assert_f(module_unload() == 0,
		     "A mmap should not keep the module alive\n");
387 388 389 390 391 392 393 394

	for (int page = 0; page < bo.size >> 12; page++)
		ptr[1024*page + page%1024] = page;

	/* And finally we should have no more uses on the module. */
	munmap(ptr, bo.size);
}

395 396 397 398 399 400 401 402 403 404 405 406 407 408
static bool has_prime_export(int fd)
{
	uint64_t value;

	if (drmGetCap(fd, DRM_CAP_PRIME, &value))
		return false;

	return value & DRM_PRIME_CAP_EXPORT;
}

igt_main
{
	int fd = -1;

409 410 411
	igt_subtest("unload")
		test_unload();

412 413 414 415
	igt_fixture {
		fd = drm_open_driver(DRIVER_VGEM);
	}

416 417 418
	igt_subtest_f("setversion")
		test_setversion(fd);

419 420 421
	igt_subtest_f("second-client")
		test_client(fd);

422 423 424 425 426 427 428 429 430 431 432
	igt_subtest_f("create")
		test_create(fd);

	igt_subtest_f("mmap")
		test_mmap(fd);

	igt_subtest_group {
		igt_fixture {
			igt_require(has_prime_export(fd));
		}

433
		igt_subtest("dmabuf-export")
434 435
			test_dmabuf_export(fd);

436
		igt_subtest("dmabuf-mmap")
437
			test_dmabuf_mmap(fd);
438 439 440 441 442 443 444 445 446 447 448

		igt_subtest_group {
			igt_fixture {
				igt_require(vgem_has_fences(fd));
			}

			igt_subtest("dmabuf-fence")
				test_dmabuf_fence(fd);
			igt_subtest("dmabuf-fence-before")
				test_dmabuf_fence_before(fd);
		}
449 450
	}

451 452 453 454 455
	igt_subtest("sysfs")
		test_sysfs_read(fd);
	igt_subtest("debugfs")
		test_debugfs_read(fd);

456 457 458 459
	igt_fixture {
		close(fd);
	}
}