drmtest.c 36.5 KB
Newer Older
1
/*
2
 * Copyright © 2007, 2011, 2013 Intel Corporation
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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>
25
 *    Daniel Vetter <daniel.vetter@ffwll.ch>
26 27 28
 *
 */

Ben Widawsky's avatar
Ben Widawsky committed
29 30
#define _GNU_SOURCE
#include <stdio.h>
31 32 33
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
34
#include <string.h>
Daniel Vetter's avatar
Daniel Vetter committed
35
#include <sys/mman.h>
36
#include <signal.h>
37
#include <pciaccess.h>
Daniel Vetter's avatar
Daniel Vetter committed
38
#include <math.h>
39
#include <getopt.h>
40
#include <stdlib.h>
41
#include <linux/kd.h>
42
#include <unistd.h>
43
#include <sys/wait.h>
Imre Deak's avatar
Imre Deak committed
44
#include "drm_fourcc.h"
Daniel Vetter's avatar
Daniel Vetter committed
45

46
#include "drmtest.h"
47 48
#include "i915_drm.h"
#include "intel_chipset.h"
49
#include "intel_gpu_tools.h"
50

51 52 53
/* This file contains a bunch of wrapper functions to directly use gem ioctls.
 * Mostly useful to write kernel tests. */

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
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;

	flink.handle = handle;
	ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
	assert(ret == 0);

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

	return bo;
}

71 72 73 74 75 76 77 78 79 80 81 82 83 84
static int
is_intel(int fd)
{
	struct drm_i915_getparam gp;
	int devid;

	gp.param = I915_PARAM_CHIPSET_ID;
	gp.value = &devid;

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

	return IS_INTEL(devid);
}
85

86 87 88 89 90 91 92 93 94 95 96 97 98 99
bool gem_uses_aliasing_ppgtt(int fd)
{
	struct drm_i915_getparam gp;
	int val;

	gp.param = 18; /* HAS_ALIASING_PPGTT */
	gp.value = &val;

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

	return val;
}

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
int gem_available_fences(int fd)
{
	struct drm_i915_getparam gp;
	int val;

	gp.param = I915_PARAM_NUM_FENCES_AVAIL;
	gp.value = &val;

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

	return val;
}


115
#define LOCAL_I915_EXEC_VEBOX	(4 << 0)
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
/* Ensure the gpu is idle by launching a nop execbuf and stalling for it. */
void gem_quiescent_gpu(int fd)
{
	uint32_t batch[2] = {MI_BATCH_BUFFER_END, 0};
	uint32_t handle;
	struct drm_i915_gem_execbuffer2 execbuf;
	struct drm_i915_gem_exec_object2 gem_exec[1];

	handle = gem_create(fd, 4096);
	gem_write(fd, handle, 0, batch, sizeof(batch));

	gem_exec[0].handle = handle;
	gem_exec[0].relocation_count = 0;
	gem_exec[0].relocs_ptr = 0;
	gem_exec[0].alignment = 0;
	gem_exec[0].offset = 0;
	gem_exec[0].flags = 0;
	gem_exec[0].rsvd1 = 0;
	gem_exec[0].rsvd2 = 0;

	execbuf.buffers_ptr = (uintptr_t)gem_exec;
	execbuf.buffer_count = 1;
	execbuf.batch_start_offset = 0;
	execbuf.batch_len = 8;
	execbuf.cliprects_ptr = 0;
	execbuf.num_cliprects = 0;
	execbuf.DR1 = 0;
	execbuf.DR4 = 0;
	execbuf.flags = 0;
145
	i915_execbuffer2_set_context_id(execbuf, 0);
146 147
	execbuf.rsvd2 = 0;

148
	do_ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
149

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	if (gem_has_blt(fd)) {
		execbuf.flags = I915_EXEC_BLT;
		do_ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
	}

	if (gem_has_bsd(fd)) {
		execbuf.flags = I915_EXEC_BSD;
		do_ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
	}

	if (gem_has_vebox(fd)) {
		execbuf.flags = LOCAL_I915_EXEC_VEBOX;
		do_ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
	}

165 166 167
	gem_sync(fd, handle);
}

Ben Widawsky's avatar
Ben Widawsky committed
168
static bool is_master(int fd)
169
{
Ben Widawsky's avatar
Ben Widawsky committed
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
	drm_client_t client;
	int ret;

	/* Check that we're the only opener and authed. */
	client.idx = 0;
	ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
	assert (ret == 0);
	if (!client.auth) {
		return 0;
	}
	client.idx = 1;
	ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
	if (ret != -1 || errno != EINVAL) {
		return 0;
	}
	return 1;
}

/**
 * drm_get_card() - get an intel card number for use in /dev or /sys
 *
 * @master: -1 not a master, 0 don't care, 1 is the master
 *
 * returns -1 on error
 */
int drm_get_card(int master)
{
	char *name;
198 199 200
	int i, fd;

	for (i = 0; i < 16; i++) {
Ben Widawsky's avatar
Ben Widawsky committed
201 202 203 204 205
		int ret;

		ret = asprintf(&name, "/dev/dri/card%u", i);
		if (ret == -1)
			return -1;
206
		fd = open(name, O_RDWR);
Ben Widawsky's avatar
Ben Widawsky committed
207 208
		free(name);

209 210 211
		if (fd == -1)
			continue;

Ben Widawsky's avatar
Ben Widawsky committed
212
		if (is_intel(fd) && master == 0) {
Daniel Vetter's avatar
Daniel Vetter committed
213
			close(fd);
Ben Widawsky's avatar
Ben Widawsky committed
214 215 216 217 218 219 220 221 222 223 224
			break;
		}

		if (master == 1 && is_master(fd)) {
			close(fd);
			break;
		}

		if (master == -1 && !is_master(fd)) {
			close(fd);
			break;
225
		}
226 227

		close(fd);
228
	}
Ben Widawsky's avatar
Ben Widawsky committed
229 230

	return i;
231 232
}

Ben Widawsky's avatar
Ben Widawsky committed
233
/** Open the first DRM device we can find, searching up to 16 device nodes */
234
static int __drm_open_any(void)
Ben Widawsky's avatar
Ben Widawsky committed
235 236 237 238 239 240 241 242 243 244 245
{
	char *name;
	int ret, fd;

	ret = asprintf(&name, "/dev/dri/card%d", drm_get_card(0));
	if (ret == -1)
		return -1;

	fd = open(name, O_RDWR);
	free(name);

246 247 248 249
	if (!is_intel(fd)) {
		close(fd);
		fd = -1;
	}
Ben Widawsky's avatar
Ben Widawsky committed
250 251 252

	return fd;
}
253

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
static void quiescent_gpu_at_exit(int sig)
{
	int fd;

	fd = __drm_open_any();
	if (fd >= 0) {
		gem_quiescent_gpu(fd);
		close(fd);
	}
}

int drm_open_any(void)
{
	static int open_count;
	int fd = __drm_open_any();

270
	if (fd < -1) {
271 272
		fprintf(stderr, "Failed to open any drm device.%s\n",
			geteuid() ? "Retry as root?" : "");
273 274 275 276
		return fd;
	}

	if (__sync_fetch_and_add(&open_count, 1))
277 278 279
		return fd;

	gem_quiescent_gpu(fd);
Daniel Vetter's avatar
Daniel Vetter committed
280
	igt_install_exit_handler(quiescent_gpu_at_exit);
281 282 283 284

	return fd;
}

285 286 287 288 289
/**
 * Open the first DRM device we can find where we end up being the master.
 */
int drm_open_any_master(void)
{
Ben Widawsky's avatar
Ben Widawsky committed
290 291
	char *name;
	int ret, fd;
292

Ben Widawsky's avatar
Ben Widawsky committed
293 294 295
	ret = asprintf(&name, "/dev/dri/card%d", drm_get_card(1));
	if (ret == -1)
		return -1;
296

Ben Widawsky's avatar
Ben Widawsky committed
297 298 299 300
	fd = open(name, O_RDWR);
	free(name);
	if (fd == -1)
		fprintf(stderr, "Couldn't find an un-controlled DRM device\n");
301

Ben Widawsky's avatar
Ben Widawsky committed
302
	assert(is_intel(fd));
303

Ben Widawsky's avatar
Ben Widawsky committed
304
	return fd;
305
}
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

void gem_set_tiling(int fd, uint32_t handle, int tiling, int stride)
{
	struct drm_i915_gem_set_tiling st;
	int ret;

	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));
	assert(ret == 0);
	assert(st.tiling_mode == tiling);
}

324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
bool gem_has_enable_ring(int fd,int param)
{
	drm_i915_getparam_t gp;
	int ret, tmp;
	memset(&gp, 0, sizeof(gp));

	gp.value = &tmp;
	gp.param = param;

	ret = drmIoctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);

	if ((ret == 0) && (*gp.value > 0))
		return true;
	else
		return false;
}

bool gem_has_bsd(int fd)
{

	return gem_has_enable_ring(fd,I915_PARAM_HAS_BSD);
}

bool gem_has_blt(int fd)
{

	return gem_has_enable_ring(fd,I915_PARAM_HAS_BLT);
}

353
#define LOCAL_I915_PARAM_HAS_VEBOX 22
354
bool gem_has_vebox(int fd)
355 356
{

357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
	return gem_has_enable_ring(fd,LOCAL_I915_PARAM_HAS_VEBOX);
}

int gem_get_num_rings(int fd)
{
	int num_rings = 1;	/* render ring is always available */

	if (gem_has_bsd(fd))
		num_rings++;
	else
		goto skip;

	if (gem_has_blt(fd))
		num_rings++;
	else
		goto skip;

	if (gem_has_vebox(fd))
		num_rings++;
	else
		goto skip;
378 379


380 381
skip:
	return num_rings;
382 383
}

Daniel Vetter's avatar
Daniel Vetter committed
384
struct local_drm_i915_gem_caching {
385
	uint32_t handle;
Daniel Vetter's avatar
Daniel Vetter committed
386
	uint32_t caching;
387 388 389 390 391
};

#define LOCAL_DRM_I915_GEM_SET_CACHEING    0x2f
#define LOCAL_DRM_I915_GEM_GET_CACHEING    0x30
#define LOCAL_DRM_IOCTL_I915_GEM_SET_CACHEING \
Daniel Vetter's avatar
Daniel Vetter committed
392
	DRM_IOW(DRM_COMMAND_BASE + LOCAL_DRM_I915_GEM_SET_CACHEING, struct local_drm_i915_gem_caching)
393
#define LOCAL_DRM_IOCTL_I915_GEM_GET_CACHEING \
Daniel Vetter's avatar
Daniel Vetter committed
394
	DRM_IOWR(DRM_COMMAND_BASE + LOCAL_DRM_I915_GEM_GET_CACHEING, struct local_drm_i915_gem_caching)
395

396
void gem_check_caching(int fd)
397
{
Daniel Vetter's avatar
Daniel Vetter committed
398
	struct local_drm_i915_gem_caching arg;
399 400 401
	int ret;

	arg.handle = gem_create(fd, 4096);
402
	assert(arg.handle != 0);
403

Daniel Vetter's avatar
Daniel Vetter committed
404
	arg.caching = 0;
405 406 407
	ret = ioctl(fd, LOCAL_DRM_IOCTL_I915_GEM_SET_CACHEING, &arg);
	gem_close(fd, arg.handle);

408
	if (ret != 0) {
Daniel Vetter's avatar
Daniel Vetter committed
409
		if (!igt_only_list_subtests())
410
			printf("no set_caching support detected\n");
Daniel Vetter's avatar
Daniel Vetter committed
411
		igt_skip();
412
	}
413 414
}

415
void gem_set_caching(int fd, uint32_t handle, int caching)
416
{
Daniel Vetter's avatar
Daniel Vetter committed
417
	struct local_drm_i915_gem_caching arg;
418 419 420
	int ret;

	arg.handle = handle;
Daniel Vetter's avatar
Daniel Vetter committed
421
	arg.caching = caching;
422
	ret = ioctl(fd, LOCAL_DRM_IOCTL_I915_GEM_SET_CACHEING, &arg);
423 424

	if (ret != 0 && (errno == ENOTTY || errno == EINVAL))
Daniel Vetter's avatar
Daniel Vetter committed
425
		igt_skip();
426 427
	else
		assert(ret == 0);
428 429
}

Daniel Vetter's avatar
Daniel Vetter committed
430
uint32_t gem_get_caching(int fd, uint32_t handle)
431
{
Daniel Vetter's avatar
Daniel Vetter committed
432
	struct local_drm_i915_gem_caching arg;
433 434 435
	int ret;

	arg.handle = handle;
Daniel Vetter's avatar
Daniel Vetter committed
436
	arg.caching = 0;
437 438 439
	ret = ioctl(fd, LOCAL_DRM_IOCTL_I915_GEM_GET_CACHEING, &arg);
	assert(ret == 0);

Daniel Vetter's avatar
Daniel Vetter committed
440
	return arg.caching;
441 442
}

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
uint32_t gem_open(int fd, uint32_t name)
{
	struct drm_gem_open open_struct;
	int ret;

	open_struct.name = name;
	ret = ioctl(fd, DRM_IOCTL_GEM_OPEN, &open_struct);
	assert(ret == 0);
	assert(open_struct.handle != 0);

	return open_struct.handle;
}

uint32_t gem_flink(int fd, uint32_t handle)
{
	struct drm_gem_flink flink;
	int ret;

	flink.handle = handle;
	ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
	assert(ret == 0);

	return flink.name;
}

Daniel Vetter's avatar
Daniel Vetter committed
468 469 470
void gem_close(int fd, uint32_t handle)
{
	struct drm_gem_close close_bo;
471

Daniel Vetter's avatar
Daniel Vetter committed
472
	close_bo.handle = handle;
473
	do_ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close_bo);
Daniel Vetter's avatar
Daniel Vetter committed
474
}
Daniel Vetter's avatar
Daniel Vetter committed
475 476 477 478 479 480 481 482 483

void gem_write(int fd, uint32_t handle, uint32_t offset, const void *buf, uint32_t size)
{
	struct drm_i915_gem_pwrite gem_pwrite;

	gem_pwrite.handle = handle;
	gem_pwrite.offset = offset;
	gem_pwrite.size = size;
	gem_pwrite.data_ptr = (uintptr_t)buf;
484
	do_ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &gem_pwrite);
Daniel Vetter's avatar
Daniel Vetter committed
485 486
}

Daniel Vetter's avatar
Daniel Vetter committed
487 488 489 490 491 492 493 494
void gem_read(int fd, uint32_t handle, uint32_t offset, void *buf, uint32_t length)
{
	struct drm_i915_gem_pread gem_pread;

	gem_pread.handle = handle;
	gem_pread.offset = offset;
	gem_pread.size = length;
	gem_pread.data_ptr = (uintptr_t)buf;
495
	do_ioctl(fd, DRM_IOCTL_I915_GEM_PREAD, &gem_pread);
Daniel Vetter's avatar
Daniel Vetter committed
496 497
}

498 499 500 501 502 503 504 505 506
void gem_set_domain(int fd, uint32_t handle,
		    uint32_t read_domains, uint32_t write_domain)
{
	struct drm_i915_gem_set_domain set_domain;

	set_domain.handle = handle;
	set_domain.read_domains = read_domains;
	set_domain.write_domain = write_domain;

507
	do_ioctl(fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
508 509 510 511 512 513 514
}

void gem_sync(int fd, uint32_t handle)
{
	gem_set_domain(fd, handle, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
}

515 516 517 518 519 520
uint32_t gem_create(int fd, int size)
{
	struct drm_i915_gem_create create;

	create.handle = 0;
	create.size = size;
521
	do_ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
522 523 524 525 526
	assert(create.handle);

	return create.handle;
}

527
void *gem_mmap__gtt(int fd, uint32_t handle, int size, int prot)
Daniel Vetter's avatar
Daniel Vetter committed
528 529 530 531 532 533 534 535
{
	struct drm_i915_gem_mmap_gtt mmap_arg;
	void *ptr;

	mmap_arg.handle = handle;
	if (drmIoctl(fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &mmap_arg))
		return NULL;

536
	ptr = mmap64(0, size, prot, MAP_SHARED, fd, mmap_arg.offset);
Daniel Vetter's avatar
Daniel Vetter committed
537 538 539 540 541 542
	if (ptr == MAP_FAILED)
		ptr = NULL;

	return ptr;
}

543 544 545 546 547 548 549 550 551 552 553 554 555
void *gem_mmap__cpu(int fd, uint32_t handle, int size, int prot)
{
	struct drm_i915_gem_mmap mmap_arg;

	mmap_arg.handle = handle;
	mmap_arg.offset = 0;
	mmap_arg.size = size;
	if (drmIoctl(fd, DRM_IOCTL_I915_GEM_MMAP, &mmap_arg))
		return NULL;

	return (void *)(uintptr_t)mmap_arg.addr_ptr;
}

556 557 558 559 560
uint64_t gem_aperture_size(int fd)
{
	struct drm_i915_gem_get_aperture aperture;

	aperture.aper_size = 256*1024*1024;
561
	do_ioctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture);
562 563 564
	return aperture.aper_size;
}

565 566 567 568 569 570 571 572 573 574 575 576 577 578
uint64_t gem_mappable_aperture_size(void)
{
	struct pci_device *pci_dev;
	int bar;
	pci_dev = intel_get_pci_device();

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

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

579 580 581 582 583 584 585
int gem_madvise(int fd, uint32_t handle, int state)
{
	struct drm_i915_gem_madvise madv;

	madv.handle = handle;
	madv.madv = state;
	madv.retained = 1;
586
	do_ioctl(fd, DRM_IOCTL_I915_GEM_MADVISE, &madv);
587 588 589 590

	return madv.retained;
}

591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
/* prime */
int prime_handle_to_fd(int fd, uint32_t handle)
{
	struct drm_prime_handle args;

	args.handle = handle;
	args.flags = DRM_CLOEXEC;
	args.fd = -1;

	do_ioctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);

	return args.fd;
}

uint32_t prime_fd_to_handle(int fd, int dma_buf_fd)
{
	struct drm_prime_handle args;

	args.fd = dma_buf_fd;
	args.flags = 0;
	args.handle = 0;

	do_ioctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);

	return args.handle;
}

618
/* signal interrupt helpers */
619 620
static pid_t signal_helper = -1;
long long int sig_stat;
621
static void __attribute__((noreturn)) signal_helper_process(pid_t pid)
622 623 624 625 626 627 628 629 630 631 632 633 634 635
{
	/* Interrupt the parent process at 500Hz, just to be annoying */
	while (1) {
		usleep(1000 * 1000 / 500);
		if (kill(pid, SIGUSR1)) /* Parent has died, so must we. */
			exit(0);
	}
}

static void sig_handler(int i)
{
	sig_stat++;
}

636 637
static void signal_helper_exit_handler(int sig)
{
Daniel Vetter's avatar
Daniel Vetter committed
638
	igt_stop_signal_helper();
639 640
}

Daniel Vetter's avatar
Daniel Vetter committed
641
void igt_fork_signal_helper(void)
642 643
{
	pid_t pid;
644
	sighandler_t oldsig;
645

Daniel Vetter's avatar
Daniel Vetter committed
646
	igt_install_exit_handler(signal_helper_exit_handler);
647

648
	signal(SIGUSR1, sig_handler);
649
	oldsig = signal(SIGQUIT, SIG_DFL);
650
	pid = fork();
651
	signal(SIGQUIT, oldsig);
652 653 654 655 656 657 658 659
	if (pid == 0) {
		signal_helper_process(getppid());
		return;
	}

	signal_helper = pid;
}

Daniel Vetter's avatar
Daniel Vetter committed
660
void igt_stop_signal_helper(void)
661
{
662 663 664
	int exitcode;

	if (signal_helper != -1) {
665
		kill(signal_helper, SIGQUIT);
666 667
		wait(&exitcode);
	}
668 669

	if (sig_stat)
670
		fprintf(stdout, "signal handler called %llu times\n", sig_stat);
671

672
	sig_stat = 0;
673 674
	signal_helper = -1;
}
675

676 677 678
/* subtests helpers */
static bool list_subtests = false;
static char *run_single_subtest = NULL;
679 680 681
static bool in_subtest = false;
static bool test_with_subtests = false;
static bool skip_subtests_henceforth = false;
682

Daniel Vetter's avatar
Daniel Vetter committed
683
void igt_subtest_init(int argc, char **argv)
684 685 686 687 688 689 690 691
{
	int c, option_index = 0;
	static struct option long_options[] = {
		{"list-subtests", 0, 0, 'l'},
		{"run-subtest", 1, 0, 'r'},
		{NULL, 0, 0, 0,}
	};

692 693
	test_with_subtests = true;

694 695
	/* supress getopt errors about unknown options */
	opterr = 0;
696 697
	/* restrict the option parsing to long option names to avoid collisions
	 * with options the test declares */
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
	while((c = getopt_long(argc, argv, "",
			       long_options, &option_index)) != -1) {
		switch(c) {
		case 'l':
			list_subtests = true;
			goto out;
		case 'r':
			run_single_subtest = strdup(optarg);
			goto out;
		}
	}

out:
	/* reset opt parsing */
	optind = 1;
}

/*
 * Note: Testcases which use these helpers MUST NOT output anything to stdout
Daniel Vetter's avatar
Daniel Vetter committed
717
 * outside of places protected by igt_run_subtest checks - the piglit
718 719
 * runner adds every line to the subtest list.
 */
Daniel Vetter's avatar
Daniel Vetter committed
720
bool __igt_run_subtest(const char *subtest_name)
721
{
722 723
	assert(in_subtest == false);

724 725 726 727 728
	if (list_subtests) {
		printf("%s\n", subtest_name);
		return false;
	}

729 730 731
	if (skip_subtests_henceforth)
		return false;

732
	if (!run_single_subtest) {
733
		return in_subtest = true;
734 735
	} else {
		if (strcmp(subtest_name, run_single_subtest) == 0)
736
			return in_subtest = true;
737 738 739 740 741

		return false;
	}
}

Daniel Vetter's avatar
Daniel Vetter committed
742
bool igt_only_list_subtests(void)
743 744 745 746
{
	return list_subtests;
}

747 748 749
static bool skipped_one = false;
static bool succeeded_one = false;
static bool failed_one = false;
Daniel Vetter's avatar
Daniel Vetter committed
750
static int igt_exitcode;
751 752 753 754 755

static void exit_subtest(void) __attribute__((noreturn));
static void exit_subtest(void)
{
	in_subtest = false;
Daniel Vetter's avatar
Daniel Vetter committed
756
	longjmp(igt_subtest_jmpbuf, 1);
757 758
}

Daniel Vetter's avatar
Daniel Vetter committed
759
void igt_skip(void)
760 761 762 763 764 765 766 767 768 769
{
	skipped_one = true;
	if (in_subtest)
		exit_subtest();
	else if (test_with_subtests)
		skip_subtests_henceforth = true;
	else
		exit(77);
}

Daniel Vetter's avatar
Daniel Vetter committed
770
void igt_success(void)
771 772 773 774 775 776
{
	succeeded_one = true;
	if (in_subtest)
		exit_subtest();
}

Daniel Vetter's avatar
Daniel Vetter committed
777
void igt_fail(int exitcode)
778 779 780 781
{
	assert(exitcode != 0 && exitcode != 77);

	if (!failed_one)
Daniel Vetter's avatar
Daniel Vetter committed
782
		igt_exitcode = exitcode;
783 784 785 786 787 788 789 790 791 792 793

	failed_one = true;

	if (in_subtest)
		exit_subtest();
	else {
		assert(!test_with_subtests);
		exit(exitcode);
	}
}

794
void igt_exit(void)
795
{
Daniel Vetter's avatar
Daniel Vetter committed
796
	if (igt_only_list_subtests())
797 798 799 800 801 802
		return 0;

	/* Calling this without calling one of the above is a failure */
	assert(skipped_one || succeeded_one || failed_one);

	if (failed_one)
803
		exit(igt_exitcode);
804
	else if (succeeded_one)
805
		exit(0);
806
	else
807
		exit(77);
808 809
}

810
static bool env_set(const char *env_var, bool default_value)
811 812 813 814 815
{
	char *val;

	val = getenv(env_var);
	if (!val)
816
		return default_value;
817 818 819 820

	return atoi(val) != 0;
}

Daniel Vetter's avatar
Daniel Vetter committed
821
bool igt_run_in_simulation(void)
822
{
823
	static int simulation = -1;
824

825
	if (simulation == -1)
826
		simulation = env_set("INTEL_SIMULATION", false);
827

828
	return simulation;
829 830
}

831 832
/* Skip the test when running on simulation (and that's relevant only when
 * we're not in the mode where we list the subtests) */
Daniel Vetter's avatar
Daniel Vetter committed
833
void igt_skip_on_simulation(void)
834
{
Daniel Vetter's avatar
Daniel Vetter committed
835
	if (igt_only_list_subtests())
836 837
		return;

Daniel Vetter's avatar
Daniel Vetter committed
838
	if (igt_run_in_simulation())
839 840 841
		exit(77);
}

842
/* other helpers */
Daniel Vetter's avatar
Daniel Vetter committed
843
void igt_exchange_int(void *array, unsigned i, unsigned j)
Daniel Vetter's avatar
Daniel Vetter committed
844 845 846 847 848 849 850 851 852
{
	int *int_arr, tmp;
	int_arr = array;

	tmp = int_arr[i];
	int_arr[i] = int_arr[j];
	int_arr[j] = tmp;
}

Daniel Vetter's avatar
Daniel Vetter committed
853
void igt_permute_array(void *array, unsigned size,
854 855 856 857 858 859 860 861 862 863 864 865 866 867
			   void (*exchange_func)(void *array,
						 unsigned i,
						 unsigned j))
{
	int i;

	for (i = size - 1; i > 1; i--) {
		/* yes, not perfectly uniform, who cares */
		long l = random() % (i +1);
		if (i != l)
			exchange_func(array, i, l);
	}
}

Daniel Vetter's avatar
Daniel Vetter committed
868
void igt_progress(const char *header, uint64_t i, uint64_t total)
869
{
870
	int divider = 200;
871

872 873 874
	if (!isatty(fileno(stderr)))
		return;

875
	if (i+1 >= total) {
876
		fprintf(stderr, "\r%s100%%\n", header);
877 878 879
		return;
	}

880
	if (total / 200 == 0)
881
		divider = 1;
882

883
	/* only bother updating about every 0.5% */
884
	if (i % (total / divider) == 0 || i+1 >= total) {
885 886 887 888 889
		fprintf(stderr, "\r%s%3llu%%", header,
			(long long unsigned) i * 100 / total);
	}
}

890 891 892 893
/* mappable aperture trasher helper */
drm_intel_bo **trash_bos;
int num_trash_bos;

Daniel Vetter's avatar
Daniel Vetter committed
894
void igt_init_aperture_trashers(drm_intel_bufmgr *bufmgr)
895 896 897 898 899 900 901 902 903 904 905 906
{
	int i;

	num_trash_bos = gem_mappable_aperture_size() / (1024*1024);

	trash_bos = malloc(num_trash_bos * sizeof(drm_intel_bo *));
	assert(trash_bos);

	for (i = 0; i < num_trash_bos; i++)
		trash_bos[i] = drm_intel_bo_alloc(bufmgr, "trash bo", 1024*1024, 4096);
}

Daniel Vetter's avatar
Daniel Vetter committed
907
void igt_trash_aperture(void)
908 909 910 911 912 913 914 915 916 917 918 919
{
	int i;
	uint8_t *gtt_ptr;

	for (i = 0; i < num_trash_bos; i++) {
		drm_intel_gem_bo_map_gtt(trash_bos[i]);
		gtt_ptr = trash_bos[i]->virtual;
		*gtt_ptr = 0;
		drm_intel_gem_bo_unmap_gtt(trash_bos[i]);
	}
}

Daniel Vetter's avatar
Daniel Vetter committed
920
void igt_cleanup_aperture_trashers(void)
921 922 923 924 925 926 927 928
{
	int i;

	for (i = 0; i < num_trash_bos; i++)
		drm_intel_bo_unreference(trash_bos[i]);

	free(trash_bos);
}
Daniel Vetter's avatar
Daniel Vetter committed
929 930

/* helpers to create nice-looking framebuffers */
Imre Deak's avatar
Imre Deak committed
931 932 933
static int create_bo_for_fb(int fd, int width, int height, int bpp,
			    bool tiled, uint32_t *gem_handle_ret,
			    unsigned *size_ret, unsigned *stride_ret)
Daniel Vetter's avatar
Daniel Vetter committed
934 935
{
	struct drm_i915_gem_set_tiling set_tiling;
Imre Deak's avatar
Imre Deak committed
936
	uint32_t gem_handle;
Daniel Vetter's avatar
Daniel Vetter committed
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
	int size;
	unsigned stride;

	if (tiled) {
		int v;

		/* Round the tiling up to the next power-of-two and the
		 * region up to the next pot fence size so that this works
		 * on all generations.
		 *
		 * This can still fail if the framebuffer is too large to
		 * be tiled. But then that failure is expected.
		 */

		v = width * bpp / 8;
		for (stride = 512; stride < v; stride *= 2)
			;

		v = stride * height;
		for (size = 1024*1024; size < v; size *= 2)
			;
	} else {
		/* Scan-out has a 64 byte alignment restriction */
		stride = (width * (bpp / 8) + 63) & ~63;
		size = stride * height;
	}

Imre Deak's avatar
Imre Deak committed
964
	gem_handle = gem_create(fd, size);
Daniel Vetter's avatar
Daniel Vetter committed
965 966

	if (tiled) {
Imre Deak's avatar
Imre Deak committed
967
		set_tiling.handle = gem_handle;
Daniel Vetter's avatar
Daniel Vetter committed
968 969 970 971 972
		set_tiling.tiling_mode = I915_TILING_X;
		set_tiling.stride = stride;
		if (ioctl(fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling)) {
			fprintf(stderr, "set tiling failed: %s (stride=%d, size=%d)\n",
				strerror(errno), stride, size);
Imre Deak's avatar
Imre Deak committed
973
			return -1;
Daniel Vetter's avatar
Daniel Vetter committed
974 975 976
		}
	}

Imre Deak's avatar
Imre Deak committed
977 978 979
	*stride_ret = stride;
	*size_ret = size;
	*gem_handle_ret = gem_handle;
Daniel Vetter's avatar
Daniel Vetter committed
980

Imre Deak's avatar
Imre Deak committed
981
	return 0;
Daniel Vetter's avatar
Daniel Vetter committed
982 983
}

Imre Deak's avatar
Imre Deak committed
984 985
void
kmstest_paint_color_gradient(cairo_t *cr, int x, int y, int w, int h,
Daniel Vetter's avatar
Daniel Vetter committed
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
		     int r, int g, int b)
{
	cairo_pattern_t *pat;

	pat = cairo_pattern_create_linear(x, y, x + w, y + h);
	cairo_pattern_add_color_stop_rgba(pat, 1, 0, 0, 0, 1);
	cairo_pattern_add_color_stop_rgba(pat, 0, r, g, b, 1);

	cairo_rectangle(cr, x, y, w, h);
	cairo_set_source(cr, pat);
	cairo_fill(cr);
	cairo_pattern_destroy(pat);
}

static void
paint_test_patterns(cairo_t *cr, int width, int height)
{
	double gr_height, gr_width;
	int x, y;

	y = height * 0.10;
	gr_width = width * 0.75;
	gr_height = height * 0.08;
	x = (width / 2) - (gr_width / 2);

Imre Deak's avatar
Imre Deak committed
1011
	kmstest_paint_color_gradient(cr, x, y, gr_width, gr_height, 1, 0, 0);
Daniel Vetter's avatar
Daniel Vetter committed
1012 1013

	y += gr_height;
Imre Deak's avatar
Imre Deak committed
1014
	kmstest_paint_color_gradient(cr, x, y, gr_width, gr_height, 0, 1, 0);
Daniel Vetter's avatar
Daniel Vetter committed
1015 1016

	y += gr_height;
Imre Deak's avatar
Imre Deak committed
1017
	kmstest_paint_color_gradient(cr, x, y, gr_width, gr_height, 0, 0, 1);
Daniel Vetter's avatar
Daniel Vetter committed
1018 1019

	y += gr_height;
Imre Deak's avatar
Imre Deak committed
1020
	kmstest_paint_color_gradient(cr, x, y, gr_width, gr_height, 1, 1, 1);
Daniel Vetter's avatar
Daniel Vetter committed
1021 1022
}

Imre Deak's avatar
Imre Deak committed
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
int kmstest_cairo_printf_line(cairo_t *cr, enum kmstest_text_align align,
				double yspacing, const char *fmt, ...)
{
	double x, y, xofs, yofs;
	cairo_text_extents_t extents;
	char *text;
	va_list ap;
	int ret;

	va_start(ap, fmt);
	ret = vasprintf(&text, fmt, ap);
	assert(ret >= 0);
	va_end(ap);

	cairo_text_extents(cr, text, &extents);

	xofs = yofs = 0;
	if (align & align_right)
		xofs = -extents.width;
	else if (align & align_hcenter)
		xofs = -extents.width / 2;

	if (align & align_top)
		yofs = extents.height;
	else if (align & align_vcenter)
		yofs = extents.height / 2;

	cairo_get_current_point(cr, &x, &y);
	if (xofs || yofs)
		cairo_rel_move_to(cr, xofs, yofs);

	cairo_text_path(cr, text);
	cairo_set_source_rgb(cr, 0, 0, 0);
	cairo_stroke_preserve(cr);
	cairo_set_source_rgb(cr, 1, 1, 1);
	cairo_fill(cr);

	cairo_move_to(cr, x, y + extents.height + yspacing);

	free(text);

	return extents.width;
}

Daniel Vetter's avatar
Daniel Vetter committed
1067
static void
1068
paint_marker(cairo_t *cr, int x, int y)
Daniel Vetter's avatar
Daniel Vetter committed
1069
{
1070
	enum kmstest_text_align align;
Daniel Vetter's avatar
Daniel Vetter committed
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
	int xoff, yoff;

	cairo_move_to(cr, x, y - 20);
	cairo_line_to(cr, x, y + 20);
	cairo_move_to(cr, x - 20, y);
	cairo_line_to(cr, x + 20, y);
	cairo_new_sub_path(cr);
	cairo_arc(cr, x, y, 10, 0, M_PI * 2);
	cairo_set_line_width(cr, 4);
	cairo_set_source_rgb(cr, 0, 0, 0);
	cairo_stroke_preserve(cr);
	cairo_set_source_rgb(cr, 1, 1, 1);
	cairo_set_line_width(cr, 2);
	cairo_stroke(cr);

1086 1087 1088 1089 1090 1091
	xoff = x ? -20 : 20;
	align = x ? align_right : align_left;

	yoff = y ? -20 : 20;
	align |= y ? align_bottom : align_top;

Daniel Vetter's avatar
Daniel Vetter committed
1092
	cairo_move_to(cr, x + xoff, y + yoff);
1093 1094
	cairo_set_font_size(cr, 18);
	kmstest_cairo_printf_line(cr, align, 0, "(%d, %d)", x, y);
Daniel Vetter's avatar
Daniel Vetter committed
1095 1096
}

Imre Deak's avatar
Imre Deak committed
1097
void kmstest_paint_test_pattern(cairo_t *cr, int width, int height)
Daniel Vetter's avatar
Daniel Vetter committed
1098 1099 1100 1101 1102 1103
{
	paint_test_patterns(cr, width, height);

	cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);

	/* Paint corner markers */
1104 1105 1106 1107
	paint_marker(cr, 0, 0);
	paint_marker(cr, width, 0);
	paint_marker(cr, 0, height);
	paint_marker(cr, width, height);
Daniel Vetter's avatar
Daniel Vetter committed
1108

Imre Deak's avatar
Imre Deak committed
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
	assert(!cairo_status(cr));
}

#define DF(did, cid, _bpp, _depth)	\
	{ DRM_FORMAT_##did, CAIRO_FORMAT_##cid, # did, _bpp, _depth }
static struct format_desc_struct {
	uint32_t drm_id;
	cairo_format_t cairo_id;
	const char *name;
	int bpp;
	int depth;
} format_desc[] = {
	DF(RGB565,	RGB16_565,	16, 16),
	DF(RGB888,	INVALID,	24, 24),
	DF(XRGB8888,	RGB24,		32, 24),
	DF(XRGB2101010,	RGB30,		32, 30),
	DF(ARGB8888,	ARGB32,		32, 32),
};
#undef DF

#define for_each_format(f)	\
	for (f = format_desc; f - format_desc < ARRAY_SIZE(format_desc); f++)
Daniel Vetter's avatar
Daniel Vetter committed
1131

Imre Deak's avatar
Imre Deak committed
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
static uint32_t bpp_depth_to_drm_format(int bpp, int depth)
{
	struct format_desc_struct *f;

	for_each_format(f)
		if (f->bpp == bpp && f->depth == depth)
			return f->drm_id;

	abort();
}
Daniel Vetter's avatar
Daniel Vetter committed
1142

Imre Deak's avatar
Imre Deak committed
1143 1144 1145 1146 1147
/* Return fb_id on success, 0 on error */
unsigned int kmstest_create_fb(int fd, int width, int height, int bpp,
			       int depth, bool tiled, struct kmstest_fb *fb)
{
	memset(fb, 0, sizeof(*fb));
Daniel Vetter's avatar
Daniel Vetter committed
1148

Imre Deak's avatar
Imre Deak committed
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
	if (create_bo_for_fb(fd, width, height, bpp, tiled, &fb->gem_handle,
			       &fb->size, &fb->stride) < 0)
		return 0;

	if (drmModeAddFB(fd, width, height, depth, bpp, fb->stride,
			       fb->gem_handle, &fb->fb_id) < 0) {
		gem_close(fd, fb->gem_handle);

		return 0;
	}

	fb->width = width;
	fb->height = height;
	fb->drm_format = bpp_depth_to_drm_format(bpp, depth);

	return fb->fb_id;
}

Imre Deak's avatar
Imre Deak committed
1167
uint32_t drm_format_to_bpp(uint32_t drm_format)
Imre Deak's avatar
Imre Deak committed
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
{
	struct format_desc_struct *f;

	for_each_format(f)
		if (f->drm_id == drm_format)
			return f->bpp;

	abort();
}

unsigned int kmstest_create_fb2(int fd, int width, int height, uint32_t format,
			        bool tiled, struct kmstest_fb *fb)
{
	uint32_t handles[4];
	uint32_t pitches[4];
	uint32_t offsets[4];
	uint32_t fb_id;
	int bpp;
	int ret;

	memset(fb, 0, sizeof(*fb));

	bpp = drm_format_to_bpp(format);
	ret = create_bo_for_fb(fd, width, height, bpp, tiled, &fb->gem_handle,
			      &fb->size, &fb->stride);
	if (ret < 0)
		return ret;

	memset(handles, 0, sizeof(handles));
	handles[0] = fb->gem_handle;
	memset(pitches, 0, sizeof(pitches));
	pitches[0] = fb->stride;
	memset(offsets, 0, sizeof(offsets));
	if (drmModeAddFB2(fd, width, height, format, handles, pitches,
			  offsets, &fb_id, 0) < 0) {
		gem_close(fd, fb->gem_handle);

		return 0;
	}

	fb->width = width;
	fb->height = height;
	fb->drm_format = format;
	fb->fb_id = fb_id;

	return fb_id;
}

Imre Deak's avatar
Imre Deak committed
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
static cairo_format_t drm_format_to_cairo(uint32_t drm_format)
{
	struct format_desc_struct *f;

	for_each_format(f)
		if (f->drm_id == drm_format)
			return f->cairo_id;

	abort();
}

static cairo_t *create_cairo_ctx(int fd, struct kmstest_fb *fb)
{
	cairo_t *cr;
	cairo_surface_t *surface;
	cairo_format_t cformat;
	void *fb_ptr;

	cformat = drm_format_to_cairo(fb->drm_format);
	fb_ptr = gem_mmap(fd, fb->gem_handle, fb->size, PROT_READ | PROT_WRITE);
	surface = cairo_image_surface_create_for_data((unsigned char *)fb_ptr,
						   cformat, fb->width,
						   fb->height, fb->stride);
	assert(surface);
	cr = cairo_create(surface);
Daniel Vetter's avatar
Daniel Vetter committed
1241 1242
	cairo_surface_destroy(surface);

Imre Deak's avatar
Imre Deak committed
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
	return cr;
}

cairo_t *kmstest_get_cairo_ctx(int fd, struct kmstest_fb *fb)
{

	if (!fb->cairo_ctx)
		fb->cairo_ctx = create_cairo_ctx(fd, fb);

	gem_set_domain(fd, fb->gem_handle, I915_GEM_DOMAIN_CPU,
		       I915_GEM_DOMAIN_CPU);
Daniel Vetter's avatar
Daniel Vetter committed
1254

Imre Deak's avatar
Imre Deak committed
1255
	return fb->cairo_ctx;
Daniel Vetter's avatar
Daniel Vetter committed
1256
}
Daniel Vetter's avatar
Daniel Vetter committed
1257

Imre Deak's avatar
Imre Deak committed
1258
void kmstest_remove_fb(int fd, struct kmstest_fb *fb)
1259
{
Imre Deak's avatar
Imre Deak committed
1260 1261 1262 1263
	if (fb->cairo_ctx)
		cairo_destroy(fb->cairo_ctx);
	do_or_die(drmModeRmFB(fd, fb->fb_id));
	gem_close(fd, fb->gem_handle);
1264 1265
}

Imre Deak's avatar
Imre Deak committed
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
const char *kmstest_format_str(uint32_t drm_format)
{
	struct format_desc_struct *f;

	for_each_format(f)
		if (f->drm_id == drm_format)
			return f->name;

	return "invalid";
}

const char *kmstest_pipe_str(int pipe)
{
	const char *str[] = { "A", "B", "C" };

	if (pipe > 2)
		return "invalid";

	return str[pipe];
}

void kmstest_get_all_formats(const uint32_t **formats, int *format_count)
{
	static uint32_t *drm_formats;

	if (!drm_formats) {
		struct format_desc_struct *f;
		uint32_t *format;

		drm_formats = calloc(ARRAY_SIZE(format_desc),
				     sizeof(*drm_formats));
		format = &drm_formats[0];
		for_each_format(f)
			*format++