ioctl_wrappers.c 35.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 25 26 27 28
/*
 * Copyright © 2007, 2011, 2013, 2014 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:
 *    Eric Anholt <eric@anholt.net>
 *    Daniel Vetter <daniel.vetter@ffwll.ch>
 *
 */

29
#ifdef HAVE_LIBGEN_H
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <libgen.h>
#endif
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <string.h>
#include <sys/mman.h>
#include <signal.h>
#include <pciaccess.h>
#include <getopt.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <sys/utsname.h>
#include <termios.h>
48
#include <errno.h>
49 50 51

#include "drmtest.h"
#include "i915_drm.h"
52
#include "intel_batchbuffer.h"
53
#include "intel_chipset.h"
54
#include "intel_io.h"
55
#include "igt_debugfs.h"
56
#include "igt_sysfs.h"
57 58
#include "config.h"

59 60 61 62 63 64
#ifdef HAVE_VALGRIND
#include <valgrind/valgrind.h>
#include <valgrind/memcheck.h>

#define VG(x) x
#else
65
#define VG(x) do {} while (0)
66 67
#endif

68 69
#include "ioctl_wrappers.h"

70 71 72 73
/**
 * SECTION:ioctl_wrappers
 * @short_description: ioctl wrappers and related functions
 * @title: ioctl wrappers
74
 * @include: igt.h
75
 *
76 77 78 79 80 81 82 83 84 85 86 87 88
 * This helper library contains simple functions to wrap the raw drm/i915 kernel
 * ioctls. The normal versions never pass any error codes to the caller and use
 * igt_assert() to check for error conditions instead. For some ioctls raw
 * wrappers which do pass on error codes are available. These raw wrappers have
 * a __ prefix.
 *
 * For wrappers which check for feature bits there can also be two versions: The
 * normal one simply returns a boolean to the caller. But when skipping the
 * testcase entirely is the right action then it's better to use igt_skip()
 * directly in the wrapper. Such functions have _require_ in their name to
 * distinguish them.
 */

89 90 91
int (*igt_ioctl)(int fd, unsigned long request, void *arg) = drmIoctl;


92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/**
 * gem_handle_to_libdrm_bo:
 * @bufmgr: libdrm buffer manager instance
 * @fd: open i915 drm file descriptor
 * @name: buffer name in libdrm
 * @handle: gem buffer object handle
 *
 * This helper function imports a raw gem buffer handle into the libdrm buffer
 * manager.
 *
 * Returns: The imported libdrm buffer manager object.
 */
drm_intel_bo *
gem_handle_to_libdrm_bo(drm_intel_bufmgr *bufmgr, int fd, const char *name, uint32_t handle)
{
	struct drm_gem_flink flink;
	int ret;
	drm_intel_bo *bo;

Chris Wilson's avatar
Chris Wilson committed
111
	memset(&flink, 0, sizeof(handle));
112 113 114
	flink.handle = handle;
	ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
	igt_assert(ret == 0);
115
	errno = 0;
116 117 118 119 120 121 122

	bo = drm_intel_bo_gem_create_from_name(bufmgr, name, flink.name);
	igt_assert(bo);

	return bo;
}

123 124 125 126 127 128 129 130 131 132 133 134 135
static int
__gem_get_tiling(int fd, struct drm_i915_gem_get_tiling *arg)
{
	int err;

	err = 0;
	if (igt_ioctl(fd, DRM_IOCTL_I915_GEM_GET_TILING, arg))
		err = -errno;
	errno = 0;

	return err;
}

136 137 138 139 140 141 142 143
/**
 * gem_get_tiling:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 * @tiling: (out) tiling mode of the gem buffer
 * @swizzle: (out) bit 6 swizzle mode
 *
 * This wraps the GET_TILING ioctl.
144 145
 *
 * Returns whether the actual physical tiling matches the reported tiling.
146
 */
147
bool
148 149 150 151 152 153 154
gem_get_tiling(int fd, uint32_t handle, uint32_t *tiling, uint32_t *swizzle)
{
	struct drm_i915_gem_get_tiling get_tiling;

	memset(&get_tiling, 0, sizeof(get_tiling));
	get_tiling.handle = handle;

155
	igt_assert_eq(__gem_get_tiling(fd, &get_tiling), 0);
156 157 158

	*tiling = get_tiling.tiling_mode;
	*swizzle = get_tiling.swizzle_mode;
159 160

	return get_tiling.phys_swizzle_mode == get_tiling.swizzle_mode;
161 162
}

163 164 165 166 167
int __gem_set_tiling(int fd, uint32_t handle, uint32_t tiling, uint32_t stride)
{
	struct drm_i915_gem_set_tiling st;
	int ret;

168
	/* The kernel doesn't know about these tiling modes, expects NONE */
169
	if (tiling == I915_TILING_Yf || tiling == I915_TILING_Ys)
170
		tiling = I915_TILING_NONE;
171

172 173 174 175 176 177 178 179 180 181 182
	memset(&st, 0, sizeof(st));
	do {
		st.handle = handle;
		st.tiling_mode = tiling;
		st.stride = tiling ? stride : 0;

		ret = ioctl(fd, DRM_IOCTL_I915_GEM_SET_TILING, &st);
	} while (ret == -1 && (errno == EINTR || errno == EAGAIN));
	if (ret != 0)
		return -errno;

183
	errno = 0;
184 185 186 187
	igt_assert(st.tiling_mode == tiling);
	return 0;
}

188 189 190 191 192 193 194 195 196
/**
 * gem_set_tiling:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 * @tiling: tiling mode bits
 * @stride: stride of the buffer when using a tiled mode, otherwise must be 0
 *
 * This wraps the SET_TILING ioctl.
 */
197 198 199 200 201
void gem_set_tiling(int fd, uint32_t handle, uint32_t tiling, uint32_t stride)
{
	igt_assert(__gem_set_tiling(fd, handle, tiling, stride) == 0);
}

202
int __gem_set_caching(int fd, uint32_t handle, uint32_t caching)
203
{
204
	struct drm_i915_gem_caching arg;
205 206 207 208 209 210 211
	int err;

	memset(&arg, 0, sizeof(arg));
	arg.handle = handle;
	arg.caching = caching;

	err = 0;
212
	if (igt_ioctl(fd, DRM_IOCTL_I915_GEM_SET_CACHING, &arg))
213
		err = -errno;
214 215

	errno = 0;
216 217 218
	return err;
}

219 220 221 222 223 224 225 226 227 228 229 230
/**
 * gem_set_caching:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 * @caching: caching mode bits
 *
 * This wraps the SET_CACHING ioctl. Note that this function internally calls
 * igt_require() when SET_CACHING isn't available, hence automatically skips the
 * test. Therefore always extract test logic which uses this into its own
 * subtest.
 */
void gem_set_caching(int fd, uint32_t handle, uint32_t caching)
231
{
232
	igt_require(__gem_set_caching(fd, handle, caching) == 0);
233 234
}

235 236 237 238 239 240 241 242 243
/**
 * gem_get_caching:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 *
 * This wraps the GET_CACHING ioctl.
 *
 * Returns: The current caching mode bits.
 */
244 245
uint32_t gem_get_caching(int fd, uint32_t handle)
{
246
	struct drm_i915_gem_caching arg;
247 248
	int ret;

249
	memset(&arg, 0, sizeof(arg));
250
	arg.handle = handle;
251
	ret = ioctl(fd, DRM_IOCTL_I915_GEM_GET_CACHING, &arg);
252
	igt_assert(ret == 0);
253
	errno = 0;
254 255 256 257

	return arg.caching;
}

258 259 260 261 262 263 264 265 266
/**
 * gem_open:
 * @fd: open i915 drm file descriptor
 * @name: flink buffer name
 *
 * This wraps the GEM_OPEN ioctl, which is used to import an flink name.
 *
 * Returns: gem file-private buffer handle of the open object.
 */
267 268 269 270 271
uint32_t gem_open(int fd, uint32_t name)
{
	struct drm_gem_open open_struct;
	int ret;

Chris Wilson's avatar
Chris Wilson committed
272
	memset(&open_struct, 0, sizeof(open_struct));
273 274 275 276
	open_struct.name = name;
	ret = ioctl(fd, DRM_IOCTL_GEM_OPEN, &open_struct);
	igt_assert(ret == 0);
	igt_assert(open_struct.handle != 0);
277
	errno = 0;
278 279 280 281

	return open_struct.handle;
}

282 283 284 285 286 287 288 289 290 291 292
/**
 * gem_flink:
 * @fd: open i915 drm file descriptor
 * @handle: file-private gem buffer object handle
 *
 * This wraps the GEM_FLINK ioctl, which is used to export a gem buffer object
 * into the device-global flink namespace. See gem_open() for opening such a
 * buffer name on a different i915 drm file descriptor.
 *
 * Returns: The created flink buffer name.
 */
293 294 295 296 297
uint32_t gem_flink(int fd, uint32_t handle)
{
	struct drm_gem_flink flink;
	int ret;

Chris Wilson's avatar
Chris Wilson committed
298
	memset(&flink, 0, sizeof(flink));
299 300 301
	flink.handle = handle;
	ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
	igt_assert(ret == 0);
302
	errno = 0;
303 304 305 306

	return flink.name;
}

307 308 309 310 311 312 313 314
/**
 * gem_close:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 *
 * This wraps the GEM_CLOSE ioctl, which to release a file-private gem buffer
 * handle.
 */
315 316 317 318
void gem_close(int fd, uint32_t handle)
{
	struct drm_gem_close close_bo;

319 320
	igt_assert_neq(handle, 0);

Chris Wilson's avatar
Chris Wilson committed
321
	memset(&close_bo, 0, sizeof(close_bo));
322 323 324 325
	close_bo.handle = handle;
	do_ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close_bo);
}

326
int __gem_write(int fd, uint32_t handle, uint64_t offset, const void *buf, uint64_t length)
327 328 329 330 331 332 333 334
{
	struct drm_i915_gem_pwrite gem_pwrite;
	int err;

	memset(&gem_pwrite, 0, sizeof(gem_pwrite));
	gem_pwrite.handle = handle;
	gem_pwrite.offset = offset;
	gem_pwrite.size = length;
335
	gem_pwrite.data_ptr = to_user_pointer(buf);
336 337 338 339 340 341 342

	err = 0;
	if (drmIoctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &gem_pwrite))
		err = -errno;
	return err;
}

343 344 345 346 347 348 349 350 351 352 353
/**
 * gem_write:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 * @offset: offset within the buffer of the subrange
 * @buf: pointer to the data to write into the buffer
 * @length: size of the subrange
 *
 * This wraps the PWRITE ioctl, which is to upload a linear data to a subrange
 * of a gem buffer object.
 */
354
void gem_write(int fd, uint32_t handle, uint64_t offset, const void *buf, uint64_t length)
355
{
356
	igt_assert_eq(__gem_write(fd, handle, offset, buf, length), 0);
357 358
}

359 360 361 362 363 364 365 366 367
static int __gem_read(int fd, uint32_t handle, uint64_t offset, void *buf, uint64_t length)
{
	struct drm_i915_gem_pread gem_pread;
	int err;

	memset(&gem_pread, 0, sizeof(gem_pread));
	gem_pread.handle = handle;
	gem_pread.offset = offset;
	gem_pread.size = length;
368
	gem_pread.data_ptr = to_user_pointer(buf);
369 370 371 372 373 374

	err = 0;
	if (drmIoctl(fd, DRM_IOCTL_I915_GEM_PREAD, &gem_pread))
		err = -errno;
	return err;
}
375 376 377 378 379 380 381 382 383 384 385
/**
 * gem_read:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 * @offset: offset within the buffer of the subrange
 * @buf: pointer to the data to read into
 * @length: size of the subrange
 *
 * This wraps the PREAD ioctl, which is to download a linear data to a subrange
 * of a gem buffer object.
 */
386
void gem_read(int fd, uint32_t handle, uint64_t offset, void *buf, uint64_t length)
387
{
388
	igt_assert_eq(__gem_read(fd, handle, offset, buf, length), 0);
389 390
}

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
int __gem_set_domain(int fd, uint32_t handle, uint32_t read, uint32_t write)
{
	struct drm_i915_gem_set_domain set_domain;
	int err;

	memset(&set_domain, 0, sizeof(set_domain));
	set_domain.handle = handle;
	set_domain.read_domains = read;
	set_domain.write_domain = write;

	err = 0;
	if (igt_ioctl(fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain))
		err = -errno;

	return err;
}

408 409 410 411
/**
 * gem_set_domain:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
412 413
 * @read: gem domain bits for read access
 * @write: gem domain bit for write access
414 415 416 417 418 419
 *
 * This wraps the SET_DOMAIN ioctl, which is used to control the coherency of
 * the gem buffer object between the cpu and gtt mappings. It is also use to
 * synchronize with outstanding rendering in general, but for that use-case
 * please have a look at gem_sync().
 */
420
void gem_set_domain(int fd, uint32_t handle, uint32_t read, uint32_t write)
421
{
422
	igt_assert_eq(__gem_set_domain(fd, handle, read, write), 0);
423 424
}

425
/**
426
 * __gem_wait:
427 428
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
429
 * @timeout_ns: [in] time to wait, [out] remaining time (in nanoseconds)
430
 *
431 432 433
 * This functions waits for outstanding rendering to complete, upto
 * the timeout_ns. If no timeout_ns is provided, the wait is indefinite and
 * only returns upon an error or when the rendering is complete.
434
 */
435
int gem_wait(int fd, uint32_t handle, int64_t *timeout_ns)
436
{
437
	struct drm_i915_gem_wait wait;
438
	int ret;
439 440 441

	memset(&wait, 0, sizeof(wait));
	wait.bo_handle = handle;
442 443
	wait.timeout_ns = timeout_ns ? *timeout_ns : -1;
	wait.flags = 0;
444

445
	ret = 0;
446
	if (igt_ioctl(fd, DRM_IOCTL_I915_GEM_WAIT, &wait))
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
		ret = -errno;

	if (timeout_ns)
		*timeout_ns = wait.timeout_ns;

	return ret;
}

/**
 * gem_sync:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 *
 * This functions waits for outstanding rendering to complete.
 */
void gem_sync(int fd, uint32_t handle)
{
	if (gem_wait(fd, handle, NULL))
		gem_set_domain(fd, handle,
			       I915_GEM_DOMAIN_GTT,
			       I915_GEM_DOMAIN_GTT);
	errno = 0;
469 470
}

471

472 473 474 475 476 477 478 479
bool gem_create__has_stolen_support(int fd)
{
	static int has_stolen_support = -1;
	struct drm_i915_getparam gp;
	int val = -1;

	if (has_stolen_support < 0) {
		memset(&gp, 0, sizeof(gp));
480
		gp.param = 38; /* CREATE_VERSION */
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
		gp.value = &val;

		/* Do we have the extended gem_create_ioctl? */
		ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
		has_stolen_support = val >= 2;
	}

	return has_stolen_support;
}

struct local_i915_gem_create_v2 {
	uint64_t size;
	uint32_t handle;
	uint32_t pad;
#define I915_CREATE_PLACEMENT_STOLEN (1<<0)
	uint32_t flags;
};

#define LOCAL_IOCTL_I915_GEM_CREATE       DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_CREATE, struct local_i915_gem_create_v2)
uint32_t __gem_create_stolen(int fd, uint64_t size)
{
	struct local_i915_gem_create_v2 create;
	int ret;

	memset(&create, 0, sizeof(create));
	create.handle = 0;
	create.size = size;
	create.flags = I915_CREATE_PLACEMENT_STOLEN;
509
	ret = igt_ioctl(fd, LOCAL_IOCTL_I915_GEM_CREATE, &create);
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542

	if (ret < 0)
		return 0;

	errno = 0;
	return create.handle;
}

/**
 * gem_create_stolen:
 * @fd: open i915 drm file descriptor
 * @size: desired size of the buffer
 *
 * This wraps the new GEM_CREATE ioctl, which allocates a new gem buffer
 * object of @size and placement in stolen memory region.
 *
 * Returns: The file-private handle of the created buffer object
 */

uint32_t gem_create_stolen(int fd, uint64_t size)
{
	struct local_i915_gem_create_v2 create;

	memset(&create, 0, sizeof(create));
	create.handle = 0;
	create.size = size;
	create.flags = I915_CREATE_PLACEMENT_STOLEN;
	do_ioctl(fd, LOCAL_IOCTL_I915_GEM_CREATE, &create);
	igt_assert(create.handle);

	return create.handle;
}

543
int __gem_create(int fd, uint64_t size, uint32_t *handle)
544
{
545 546 547 548 549
	struct drm_i915_gem_create create = {
		.size = size,
	};
	int err = 0;

550
	if (igt_ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) == 0) {
551
		*handle = create.handle;
552
	} else {
553
		err = -errno;
554 555
		igt_assume(err != 0);
	}
556 557

	errno = 0;
558
	return err;
559 560
}

561 562 563 564 565 566 567 568 569 570
/**
 * gem_create:
 * @fd: open i915 drm file descriptor
 * @size: desired size of the buffer
 *
 * This wraps the GEM_CREATE ioctl, which allocates a new gem buffer object of
 * @size.
 *
 * Returns: The file-private handle of the created buffer object
 */
571
uint32_t gem_create(int fd, uint64_t size)
572
{
573
	uint32_t handle;
574

575
	igt_assert_eq(__gem_create(fd, size, &handle), 0);
576

577
	return handle;
578 579
}

580 581 582 583 584 585 586 587 588 589 590
/**
 * __gem_execbuf:
 * @fd: open i915 drm file descriptor
 * @execbuf: execbuffer data structure
 *
 * This wraps the EXECBUFFER2 ioctl, which submits a batchbuffer for the gpu to
 * run. This is allowed to fail, with -errno returned.
 */
int __gem_execbuf(int fd, struct drm_i915_gem_execbuffer2 *execbuf)
{
	int err = 0;
591
	if (igt_ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, execbuf)) {
592
		err = -errno;
593 594
		igt_assume(err != 0);
	}
595
	errno = 0;
596 597 598
	return err;
}

599 600 601 602 603 604 605 606
/**
 * gem_execbuf:
 * @fd: open i915 drm file descriptor
 * @execbuf: execbuffer data structure
 *
 * This wraps the EXECBUFFER2 ioctl, which submits a batchbuffer for the gpu to
 * run.
 */
607 608
void gem_execbuf(int fd, struct drm_i915_gem_execbuffer2 *execbuf)
{
609
	igt_assert_eq(__gem_execbuf(fd, execbuf), 0);
610 611
}

612 613 614 615 616 617 618 619 620 621 622
/**
 * __gem_execbuf_wr:
 * @fd: open i915 drm file descriptor
 * @execbuf: execbuffer data structure
 *
 * This wraps the EXECBUFFER2_WR ioctl, which submits a batchbuffer for the gpu to
 * run. This is allowed to fail, with -errno returned.
 */
int __gem_execbuf_wr(int fd, struct drm_i915_gem_execbuffer2 *execbuf)
{
	int err = 0;
623
	if (igt_ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2_WR, execbuf)) {
624
		err = -errno;
625 626
		igt_assume(err != 0);
	}
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
	errno = 0;
	return err;
}

/**
 * gem_execbuf_wr:
 * @fd: open i915 drm file descriptor
 * @execbuf: execbuffer data structure
 *
 * This wraps the EXECBUFFER2_WR ioctl, which submits a batchbuffer for the gpu to
 * run.
 */
void gem_execbuf_wr(int fd, struct drm_i915_gem_execbuffer2 *execbuf)
{
	igt_assert_eq(__gem_execbuf_wr(fd, execbuf), 0);
}

644 645 646 647 648 649
/**
 * gem_madvise:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 * @state: desired madvise state
 *
650
 * This wraps the MADVISE ioctl, which is used in libdrm to implement
651 652 653 654 655
 * opportunistic buffer object caching. Objects in the cache are set to DONTNEED
 * (internally in the kernel tracked as purgeable objects). When such a cached
 * object is in need again it must be set back to WILLNEED before first use.
 *
 * Returns: When setting the madvise state to WILLNEED this returns whether the
656
 * backing storage was still available or not.
657
 */
658 659 660 661
int gem_madvise(int fd, uint32_t handle, int state)
{
	struct drm_i915_gem_madvise madv;

Chris Wilson's avatar
Chris Wilson committed
662
	memset(&madv, 0, sizeof(madv));
663 664 665 666 667 668 669 670
	madv.handle = handle;
	madv.madv = state;
	madv.retained = 1;
	do_ioctl(fd, DRM_IOCTL_I915_GEM_MADVISE, &madv);

	return madv.retained;
}

671
int __gem_userptr(int fd, void *ptr, uint64_t size, int read_only, uint32_t flags, uint32_t *handle)
672
{
673
	struct drm_i915_gem_userptr userptr;
674 675

	memset(&userptr, 0, sizeof(userptr));
676
	userptr.user_ptr = to_user_pointer(ptr);
677 678 679
	userptr.user_size = size;
	userptr.flags = flags;
	if (read_only)
680
		userptr.flags |= I915_USERPTR_READ_ONLY;
681

682
	if (igt_ioctl(fd, DRM_IOCTL_I915_GEM_USERPTR, &userptr))
683
		return -errno;
684

685 686
	*handle = userptr.handle;
	return 0;
687 688 689 690 691 692 693 694 695 696 697 698 699
}

/**
 * gem_userptr:
 * @fd: open i915 drm file descriptor
 * @ptr: userptr pointer to be passed
 * @size: desired size of the buffer
 * @read_only: specify whether userptr is opened read only
 * @flags: other userptr flags
 * @handle: returned handle for the object
 *
 * Returns userptr handle for the GEM object.
 */
700
void gem_userptr(int fd, void *ptr, uint64_t size, int read_only, uint32_t flags, uint32_t *handle)
701 702 703 704
{
	igt_assert_eq(__gem_userptr(fd, ptr, size, read_only, flags, handle), 0);
}

705 706 707 708 709
/**
 * gem_sw_finish:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 *
710
 * This wraps the SW_FINISH ioctl, which is used to flush out frontbuffer
711 712 713
 * rendering done through the direct cpu memory mappings. Shipping userspace
 * does _not_ call this after frontbuffer rendering through gtt memory mappings.
 */
714 715 716 717
void gem_sw_finish(int fd, uint32_t handle)
{
	struct drm_i915_gem_sw_finish finish;

Chris Wilson's avatar
Chris Wilson committed
718
	memset(&finish, 0, sizeof(finish));
719 720 721 722 723
	finish.handle = handle;

	do_ioctl(fd, DRM_IOCTL_I915_GEM_SW_FINISH, &finish);
}

724 725 726 727 728
/**
 * gem_bo_busy:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 *
729
 * This wraps the BUSY ioctl, which tells whether a buffer object is still
730 731 732 733
 * actively used by the gpu in a execbuffer.
 *
 * Returns: The busy state of the buffer object.
 */
734 735 736 737
bool gem_bo_busy(int fd, uint32_t handle)
{
	struct drm_i915_gem_busy busy;

Chris Wilson's avatar
Chris Wilson committed
738
	memset(&busy, 0, sizeof(busy));
739 740 741 742 743 744 745 746 747
	busy.handle = handle;

	do_ioctl(fd, DRM_IOCTL_I915_GEM_BUSY, &busy);

	return !!busy.busy;
}


/* feature test helpers */
748 749

/**
750
 * gem_gtt_type:
751 752
 * @fd: open i915 drm file descriptor
 *
753 754 755
 * Feature test macro to check what type of gtt is being used by the kernel:
 * 0 - global gtt
 * 1 - aliasing ppgtt
756
 * 2 - full ppgtt
757
 *
758
 * Returns: Type of gtt being used.
759
 */
760
static int gem_gtt_type(int fd)
761 762
{
	struct drm_i915_getparam gp;
Chris Wilson's avatar
Chris Wilson committed
763
	int val = 0;
764

Chris Wilson's avatar
Chris Wilson committed
765
	memset(&gp, 0, sizeof(gp));
766
	gp.param = I915_PARAM_HAS_ALIASING_PPGTT;
767 768 769 770 771
	gp.value = &val;

	if (ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp, sizeof(gp)))
		return 0;

772
	errno = 0;
773 774 775
	return val;
}

776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
/**
 * gem_uses_ppgtt:
 * @fd: open i915 drm file descriptor
 *
 * Feature test macro to check whether the kernel internally uses ppgtt to
 * execute batches. Note that this is also true when we're using full ppgtt.
 *
 * Returns: Whether batches are run through ppgtt.
 */
bool gem_uses_ppgtt(int fd)
{
	return gem_gtt_type(fd) > 0;
}

/**
 * gem_uses_full_ppgtt:
 * @fd: open i915 drm file descriptor
 *
 * Feature test macro to check whether the kernel internally uses full
 * per-process gtt to execute batches. Note that this is also true when we're
 * using full 64b ppgtt.
 *
 * Returns: Whether batches are run through full ppgtt.
 */
bool gem_uses_full_ppgtt(int fd)
{
	return gem_gtt_type(fd) > 1;
}

805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
/**
 * gem_gpu_reset_type:
 * @fd: open i915 drm file descriptor
 *
 * Query whether reset-engine (2), global-reset (1) or reset-disable (0)
 * is available.
 *
 * Returns: GPU reset type available
 */
int gem_gpu_reset_type(int fd)
{
	struct drm_i915_getparam gp;
	int gpu_reset_type = -1;

	memset(&gp, 0, sizeof(gp));
	gp.param = I915_PARAM_HAS_GPU_RESET;
	gp.value = &gpu_reset_type;
	drmIoctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);

	return gpu_reset_type;
}

/**
 * gem_gpu_reset_enabled:
 * @fd: open i915 drm file descriptor
 *
 * Feature test macro to check whether the kernel internally uses hangchecks
 * and can reset the GPU upon hang detection. Note that this is also true when
 * reset-engine (the lightweight, single engine reset) is available.
 *
 * Returns: Whether the driver will detect hangs and perform a reset.
 */
bool gem_gpu_reset_enabled(int fd)
{
	return gem_gpu_reset_type(fd) > 0;
}

/**
 * gem_engine_reset_enabled:
 * @fd: open i915 drm file descriptor
 *
 * Feature test macro to check whether the kernel internally uses hangchecks
 * and can reset individual engines upon hang detection.
 *
 * Returns: Whether the driver will detect hangs and perform an engine reset.
 */
bool gem_engine_reset_enabled(int fd)
{
	return gem_gpu_reset_type(fd) > 1;
}

856
/**
857
 * gem_available_fences:
858 859 860
 * @fd: open i915 drm file descriptor
 *
 * Feature test macro to query the kernel for the number of available fences
861
 * usable in a batchbuffer. Only relevant for pre-gen4.
862 863 864
 *
 * Returns: The number of available fences.
 */
865 866
int gem_available_fences(int fd)
{
Chris Wilson's avatar
Chris Wilson committed
867
	static int num_fences = -1;
868

Chris Wilson's avatar
Chris Wilson committed
869 870
	if (num_fences < 0) {
		struct drm_i915_getparam gp;
871

Chris Wilson's avatar
Chris Wilson committed
872 873 874
		memset(&gp, 0, sizeof(gp));
		gp.param = I915_PARAM_NUM_FENCES_AVAIL;
		gp.value = &num_fences;
875

Chris Wilson's avatar
Chris Wilson committed
876 877 878 879 880 881
		num_fences = 0;
		ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp, sizeof(gp));
		errno = 0;
	}

	return num_fences;
882 883
}

884 885
bool gem_has_llc(int fd)
{
Chris Wilson's avatar
Chris Wilson committed
886
	static int has_llc = -1;
887

Chris Wilson's avatar
Chris Wilson committed
888 889
	if (has_llc < 0) {
		struct drm_i915_getparam gp;
890

Chris Wilson's avatar
Chris Wilson committed
891 892 893
		memset(&gp, 0, sizeof(gp));
		gp.param = I915_PARAM_HAS_LLC;
		gp.value = &has_llc;
894

Chris Wilson's avatar
Chris Wilson committed
895 896 897 898 899 900
		has_llc = 0;
		ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp, sizeof(gp));
		errno = 0;
	}

	return has_llc;
901 902
}

903
static bool has_param(int fd, int param)
904 905
{
	drm_i915_getparam_t gp;
Chris Wilson's avatar
Chris Wilson committed
906
	int tmp = 0;
907

Chris Wilson's avatar
Chris Wilson committed
908
	memset(&gp, 0, sizeof(gp));
909 910 911
	gp.value = &tmp;
	gp.param = param;

912
	if (igt_ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp))
913
		return false;
914 915 916

	errno = 0;
	return tmp > 0;
917 918
}

919 920 921 922
/**
 * gem_has_bsd:
 * @fd: open i915 drm file descriptor
 *
923
 * Feature test macro to query whether the BSD ring is available.
924 925 926
 *
 * Note that recent Bspec calls this the VCS ring for Video Command Submission.
 *
927
 * Returns: Whether the BSD ring is available or not.
928
 */
929 930
bool gem_has_bsd(int fd)
{
Chris Wilson's avatar
Chris Wilson committed
931 932
	static int has_bsd = -1;
	if (has_bsd < 0)
933
		has_bsd = has_param(fd, I915_PARAM_HAS_BSD);
Chris Wilson's avatar
Chris Wilson committed
934
	return has_bsd;
935 936
}

937 938 939 940
/**
 * gem_has_blt:
 * @fd: open i915 drm file descriptor
 *
941
 * Feature test macro to query whether the blitter ring is available.
942 943 944
 *
 * Note that recent Bspec calls this the BCS ring for Blitter Command Submission.
 *
945
 * Returns: Whether the blitter ring is available or not.
946
 */
947 948
bool gem_has_blt(int fd)
{
Chris Wilson's avatar
Chris Wilson committed
949 950
	static int has_blt = -1;
	if (has_blt < 0)
951
		has_blt =  has_param(fd, I915_PARAM_HAS_BLT);
Chris Wilson's avatar
Chris Wilson committed
952
	return has_blt;
953 954
}

955 956 957 958
/**
 * gem_has_vebox:
 * @fd: open i915 drm file descriptor
 *
959
 * Feature test macro to query whether the vebox ring is available.
960 961 962 963
 *
 * Note that recent Bspec calls this the VECS ring for Video Enhancement Command
 * Submission.
 *
964
 * Returns: Whether the vebox ring is available or not.
965
 */
966 967
bool gem_has_vebox(int fd)
{
Chris Wilson's avatar
Chris Wilson committed
968 969
	static int has_vebox = -1;
	if (has_vebox < 0)
970
		has_vebox =  has_param(fd, I915_PARAM_HAS_VEBOX);
Chris Wilson's avatar
Chris Wilson committed
971
	return has_vebox;
972 973
}

974
#define I915_PARAM_HAS_BSD2 31
975 976 977 978
/**
 * gem_has_bsd2:
 * @fd: open i915 drm file descriptor
 *
979
 * Feature test macro to query whether the BSD2 ring is available.
980 981 982 983 984 985 986
 *
 * Note that recent Bspec calls this the VCS ring for Video Command Submission.
 *
 * Returns: Whether the BSD ring is avaible or not.
 */
bool gem_has_bsd2(int fd)
{
Chris Wilson's avatar
Chris Wilson committed
987 988
	static int has_bsd2 = -1;
	if (has_bsd2 < 0)
989
		has_bsd2 = has_param(fd, I915_PARAM_HAS_BSD2);
Chris Wilson's avatar
Chris Wilson committed
990
	return has_bsd2;
991
}
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037

struct local_i915_gem_get_aperture {
	__u64 aper_size;
	__u64 aper_available_size;
	__u64 version;
	__u64 map_total_size;
	__u64 stolen_total_size;
};
#define DRM_I915_GEM_GET_APERTURE	0x23
#define LOCAL_IOCTL_I915_GEM_GET_APERTURE DRM_IOR  (DRM_COMMAND_BASE + DRM_I915_GEM_GET_APERTURE, struct local_i915_gem_get_aperture)
/**
 * gem_total_mappable_size:
 * @fd: open i915 drm file descriptor
 *
 * Feature test macro to query the kernel for the total mappable size.
 *
 * Returns: Total mappable address space size.
 */
uint64_t gem_total_mappable_size(int fd)
{
	struct local_i915_gem_get_aperture aperture;

	memset(&aperture, 0, sizeof(aperture));
	do_ioctl(fd, LOCAL_IOCTL_I915_GEM_GET_APERTURE, &aperture);

	return aperture.map_total_size;
}

/**
 * gem_total_stolen_size:
 * @fd: open i915 drm file descriptor
 *
 * Feature test macro to query the kernel for the total stolen size.
 *
 * Returns: Total stolen memory.
 */
uint64_t gem_total_stolen_size(int fd)
{
	struct local_i915_gem_get_aperture aperture;

	memset(&aperture, 0, sizeof(aperture));
	do_ioctl(fd, LOCAL_IOCTL_I915_GEM_GET_APERTURE, &aperture);

	return aperture.stolen_total_size;
}

1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
/**
 * gem_available_aperture_size:
 * @fd: open i915 drm file descriptor
 *
 * Feature test macro to query the kernel for the available gpu aperture size
 * usable in a batchbuffer.
 *
 * Returns: The available gtt address space size.
 */
uint64_t gem_available_aperture_size(int fd)
{
	struct drm_i915_gem_get_aperture aperture;

	memset(&aperture, 0, sizeof(aperture));
	aperture.aper_size = 256*1024*1024;
	do_ioctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture);

	return aperture.aper_available_size;
}
1057

1058 1059 1060 1061 1062 1063 1064 1065
/**
 * gem_aperture_size:
 * @fd: open i915 drm file descriptor
 *
 * Feature test macro to query the kernel for the total gpu aperture size.
 *
 * Returns: The total gtt address space size.
 */
1066 1067
uint64_t gem_aperture_size(int fd)
{
Chris Wilson's avatar
Chris Wilson committed
1068
	static uint64_t aperture_size = 0;
1069

Chris Wilson's avatar
Chris Wilson committed
1070
	if (aperture_size == 0) {
1071
		struct drm_i915_gem_context_param p;
1072 1073 1074

		memset(&p, 0, sizeof(p));
		p.param = 0x3;
1075
		if (__gem_context_get_param(fd, &p) == 0) {
1076 1077 1078
			aperture_size = p.value;
		} else {
			struct drm_i915_gem_get_aperture aperture;
Chris Wilson's avatar
Chris Wilson committed
1079

1080 1081
			memset(&aperture, 0, sizeof(aperture));
			aperture.aper_size = 256*1024*1024;
Chris Wilson's avatar
Chris Wilson committed
1082

1083 1084 1085
			do_ioctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture);
			aperture_size =  aperture.aper_size;
		}
Chris Wilson's avatar
Chris Wilson committed
1086
	}
Chris Wilson's avatar
Chris Wilson committed
1087

Chris Wilson's avatar
Chris Wilson committed
1088
	return aperture_size;
1089 1090
}

1091
/**
1092
 * gem_mappable_aperture_size:
1093 1094
 *
 * Feature test macro to query the kernel for the mappable gpu aperture size.
1095
 * This is the area available for GTT memory mappings.
1096 1097 1098
 *
 * Returns: The mappable gtt address space size.
 */
1099 1100
uint64_t gem_mappable_aperture_size(void)
{
Chris Wilson's avatar
Chris Wilson committed
1101
	struct pci_device *pci_dev = intel_get_pci_device();
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
	int bar;

	if (intel_gen(pci_dev->device_id) < 3)
		bar = 0;
	else
		bar = 2;

	return pci_dev->regions[bar].size;
}

1112 1113
/**
 * gem_global_aperture_size:
1114
 * @fd: open i915 drm file descriptor
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
 *
 * Feature test macro to query the kernel for the global gpu aperture size.
 * This is the area available for the kernel to perform address translations.
 *
 * Returns: The mappable gtt address space size.
 */
uint64_t gem_global_aperture_size(int fd)
{
	struct drm_i915_gem_get_aperture aperture;

	memset(&aperture, 0, sizeof(aperture));
	aperture.aper_size = 256*1024*1024;
	do_ioctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture);

	return aperture.aper_size;
}

1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
/**
 * gem_has_softpin:
 * @fd: open i915 drm file descriptor
 *
 * Feature test macro to query whether the softpinning functionality is
 * supported.
 *
 * Returns: Whether softpin support is available
 */
bool gem_has_softpin(int fd)
{
	static int has_softpin = -1;

	if (has_softpin < 0) {
		struct drm_i915_getparam gp;

		memset(&gp, 0, sizeof(gp));
1149
		gp.param = I915_PARAM_HAS_EXEC_SOFTPIN;
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
		gp.value = &has_softpin;

		has_softpin = 0;
		ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp, sizeof(gp));
		errno = 0;
	}

	return has_softpin;
}

1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
/**
 * gem_has_exec_fence:
 * @fd: open i915 drm file descriptor
 *
 * Feature test macro to query whether in/out fence support in execbuffer is
 * available.
 *
 * Returns: Whether fence support is available
 */
bool gem_has_exec_fence(int fd)
{
	static int has_exec_fence = -1;

	if (has_exec_fence < 0) {
		struct drm_i915_getparam gp;

		memset(&gp, 0, sizeof(gp));
1177
		gp.param = I915_PARAM_HAS_EXEC_FENCE;
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
		gp.value = &has_exec_fence;

		has_exec_fence = 0;
		ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp, sizeof(gp));
		errno = 0;
	}

	return has_exec_fence;
}

1188 1189 1190 1191 1192 1193 1194
/**
 * gem_require_caching:
 * @fd: open i915 drm file descriptor
 *
 * Feature test macro to query whether buffer object caching control is
 * available. Automatically skips through igt_require() if not.
 */
1195 1196
void gem_require_caching(int fd)
{
1197
	uint32_t handle;
1198

1199 1200 1201
	handle = gem_create(fd, 4096);
	gem_set_caching(fd, handle, 0);
	gem_close(fd, handle);
1202

1203
	errno = 0;
1204 1205
}

1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
static void reset_device(int fd)
{
	int dir;

	dir = igt_debugfs_dir(fd);
	igt_require(dir >= 0);

	if (ioctl(fd, DRM_IOCTL_I915_GEM_THROTTLE)) {
		igt_info("Found wedged device, trying to reset and continue\n");
		igt_sysfs_set(dir, "i915_wedged", "-1");
	}
	igt_sysfs_set(dir, "i915_next_seqno", "1");

	close(dir);
}

1222 1223
void igt_require_gem(int fd)
{
1224
	char path[256];
1225 1226
	int err;

1227 1228
	igt_require_intel(fd);

1229 1230
	/*
	 * We only want to use the throttle-ioctl for its -EIO reporting
1231 1232 1233 1234 1235 1236 1237
	 * of a wedged device, not for actually waiting on outstanding
	 * requests! So create a new drm_file for the device that is clean.
	 */
	snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
	fd = open(path, O_RDWR);
	igt_assert_lte(0, fd);

1238 1239 1240 1241 1242 1243
	/*
	 * Reset the global seqno at the start of each test. This ensures that
	 * the test will not wrap unless it explicitly sets up seqno wrapping
	 * itself, which avoids accidentally hanging when setting up long
	 * sequences of batches.
	 */
1244
	reset_device(fd);
1245

1246 1247 1248
	err = 0;
	if (ioctl(fd, DRM_IOCTL_I915_GEM_THROTTLE))
		err = -errno;
1249

1250
	close(fd);
1251 1252 1253 1254

	igt_require_f(err == 0, "Unresponsive i915/GEM device\n");
}

1255
bool gem_has_ring(int fd, unsigned ring)
1256 1257 1258 1259
{
	struct drm_i915_gem_execbuffer2 execbuf;
	struct drm_i915_gem_exec_object2 exec;

1260 1261 1262 1263 1264 1265
	/* silly ABI, the kernel thinks everyone who has BSD also has BSD2 */
	if ((ring & ~(3<<13)) == I915_EXEC_BSD) {
		if (ring & (3 << 13) && !gem_has_bsd2(fd))
			return false;
	}

1266 1267
	memset(&exec, 0, sizeof(exec));
	memset(&execbuf, 0, sizeof(execbuf));
1268
	execbuf.buffers_ptr = to_user_pointer(&exec);
1269 1270
	execbuf.buffer_count = 1;
	execbuf.flags = ring;
1271
	return __gem_execbuf(fd, &execbuf) == -ENOENT;
1272 1273
}

1274 1275 1276
/**
 * gem_require_ring:
 * @fd: open i915 drm file descriptor
1277
 * @ring: ring flag bit as used in gem_execbuf()
1278 1279
 *
 * Feature test macro to query whether a specific ring is available.
1280 1281
 * This automagically skips if the ring isn't available by
 * calling igt_require().
1282
 */
1283
void gem_require_ring(int fd, unsigned ring)
1284
{
1285
	igt_require(gem_has_ring(fd, ring));
1286 1287
}

1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
/**
 * gem_has_mocs_registers:
 * @fd: open i915 drm file descriptor
 *
 * Feature test macro to query whether the device has MOCS registers.
 * These exist gen 9+.
 */
bool gem_has_mocs_registers(int fd)
{
	return intel_gen(intel_get_drm_devid(fd)) >= 9;
}

/**
 * gem_require_mocs_registers:
 * @fd: open i915 drm file descriptor
 *
 * Feature test macro to query whether the device has MOCS registers.
 * These exist gen 9+.
 */
void gem_require_mocs_registers(int fd)
{
	igt_require(gem_has_mocs_registers(fd));
}

1312
/* prime */
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324

/**
 * prime_handle_to_fd:
 * @fd: open i915 drm file descriptor
 * @handle: file-private gem buffer object handle
 *
 * This wraps the PRIME_HANDLE_TO_FD ioctl, which is used to export a gem buffer
 * object into a global (i.e. potentially cross-device) dma-buf file-descriptor
 * handle.
 *
 * Returns: The created dma-buf fd handle.
 */
1325 1326 1327 1328
int prime_handle_to_fd(int fd, uint32_t handle)
{
	struct drm_prime_handle args;

Chris Wilson's avatar
Chris Wilson committed
1329
	memset(&args, 0, sizeof(args));
1330 1331 1332 1333 1334 1335 1336 1337 1338
	args.handle = handle;
	args.flags = DRM_CLOEXEC;
	args.fd = -1;

	do_ioctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);

	return args.fd;
}

1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
/**
 * prime_handle_to_fd_for_mmap:
 * @fd: open i915 drm file descriptor
 * @handle: file-private gem buffer object handle
 *
 * Same as prime_handle_to_fd above but with DRM_RDWR capabilities, which can
 * be useful for writing into the mmap'ed dma-buf file-descriptor.
 *
 * Returns: The created dma-buf fd handle or -1 if the ioctl fails.
 */
int prime_handle_to_fd_for_mmap(int fd, uint32_t handle)
{
	struct drm_prime_handle args;

	memset(&args, 0, sizeof(args));
	args.handle = handle;
	args.flags = DRM_CLOEXEC | DRM_RDWR;
	args.fd = -1;

1358
	if (igt_ioctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args) != 0)
1359 1360 1361 1362 1363
		return -1;

	return args.fd;
}

1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
/**
 * prime_fd_to_handle:
 * @fd: open i915 drm file descriptor
 * @dma_buf_fd: dma-buf fd handle
 *
 * This wraps the PRIME_FD_TO_HANDLE ioctl, which is used to import a dma-buf
 * file-descriptor into a gem buffer object.
 *
 * Returns: The created gem buffer object handle.
 */
1374 1375 1376 1377
uint32_t prime_fd_to_handle(int fd, int dma_buf_fd)
{
	struct drm_prime_handle args;

Chris Wilson's avatar
Chris Wilson committed
1378
	memset(&args, 0, sizeof(args));
1379 1380 1381 1382 1383 1384 1385 1386 1387
	args.fd = dma_buf_fd;
	args.flags = 0;
	args.handle = 0;

	do_ioctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);

	return args.handle;
}

1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
/**
 * prime_get_size:
 * @dma_buf_fd: dma-buf fd handle
 *
 * This wraps the lseek() protocol used to query the invariant size of a
 * dma-buf.  Not all kernels support this, which is check with igt_require() and
 * so will result in automagic test skipping.
 *
 * Returns: The lifetime-invariant size of the dma-buf object.
 */
1398 1399 1400
off_t prime_get_size(int dma_buf_fd)
{
	off_t ret;
Chris Wilson's avatar
Chris Wilson committed
1401

1402 1403 1404
	ret = lseek(dma_buf_fd, 0, SEEK_END);
	igt_assert(ret >= 0 || errno == ESPIPE);
	igt_require(ret >= 0);
1405
	errno = 0;
1406 1407 1408

	return ret;
}
1409

1410 1411 1412
/**
 * prime_sync_start
 * @dma_buf_fd: dma-buf fd handle
1413 1414 1415
 * @write: read/write or read-only access
 *
 * Must be called before starting CPU mmap access to a dma-buf.
1416
 */
1417
void prime_sync_start(int dma_buf_fd, bool write)
1418 1419 1420 1421
{
	struct local_dma_buf_sync sync_start;

	memset(&sync_start, 0, sizeof(sync_start));
1422 1423 1424 1425
	sync_start.flags = LOCAL_DMA_BUF_SYNC_START;
	sync_start.flags |= LOCAL_DMA_BUF_SYNC_READ;
	if (write)
		sync_start.flags |= LOCAL_DMA_BUF_SYNC_WRITE;
1426 1427 1428 1429 1430 1431
	do_ioctl(dma_buf_fd, LOCAL_DMA_BUF_IOCTL_SYNC, &sync_start);
}

/**
 * prime_sync_end
 * @dma_buf_fd: dma-buf fd handle
1432 1433 1434
 * @write: read/write or read-only access
 *
 * Must be called after finishing CPU mmap access to a dma-buf.
1435
 */
1436
void prime_sync_end(int dma_buf_fd, bool write)
1437 1438 1439 1440
{
	struct local_dma_buf_sync sync_end;

	memset(&sync_end, 0, sizeof(sync_end));
1441 1442 1443 1444
	sync_end.flags = LOCAL_DMA_BUF_SYNC_END;
	sync_end.flags |= LOCAL_DMA_BUF_SYNC_READ;
	if (write)
		sync_end.flags |= LOCAL_DMA_BUF_SYNC_WRITE;
1445 1446 1447
	do_ioctl(dma_buf_fd, LOCAL_DMA_BUF_IOCTL_SYNC, &sync_end);
}

1448
bool igt_has_fb_modifiers(int fd)
1449 1450 1451 1452 1453 1454 1455
{
	static bool has_modifiers, cap_modifiers_tested;

	if (!cap_modifiers_tested) {
		uint64_t cap_modifiers;
		int ret;

1456
		ret = drmGetCap(fd, DRM_CAP_ADDFB2_MODIFIERS, &cap_modifiers);
1457
		igt_assert(ret == 0 || errno == EINVAL || errno == EOPNOTSUPP);
1458 1459 1460 1461
		has_modifiers = ret == 0 && cap_modifiers == 1;
		cap_modifiers_tested = true;
	}

1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
	return has_modifiers;
}

/**
 * igt_require_fb_modifiers:
 * @fd: Open DRM file descriptor.
 *
 * Requires presence of DRM_CAP_ADDFB2_MODIFIERS.
 */
void igt_require_fb_modifiers(int fd)
{
	igt_require(igt_has_fb_modifiers(fd));
1474
}
1475

1476 1477 1478 1479
int __kms_addfb(int fd, uint32_t handle,
		uint32_t width, uint32_t height,
		uint32_t pixel_format, uint64_t modifier,
		uint32_t strides[4], uint32_t offsets[4],
1480
		int num_planes, uint32_t flags, uint32_t *buf_id)
1481
{
1482
	struct drm_mode_fb_cmd2 f;
1483
	int ret, i;
1484

1485 1486
	if (flags & DRM_MODE_FB_MODIFIERS)
		igt_require_fb_modifiers(fd);
1487 1488 1489 1490 1491 1492 1493 1494

	memset(&f, 0, sizeof(f));

	f.width  = width;
	f.height = height;
	f.pixel_format = pixel_format;
	f.flags = flags;

1495
	for (i = 0; i < num_planes; i++) {
1496 1497
		f.handles[i] = handle;
		f.modifier[i] = modifier;
1498
		f.pitches[i] = strides[i];
1499 1500 1501
		f.offsets[i] = offsets[i];
	}

1502
	ret = igt_ioctl(fd, DRM_IOCTL_MODE_ADDFB2, &f);
1503 1504 1505 1506 1507

	*buf_id = f.fb_id;

	return ret < 0 ? -errno : ret;
}