kms_vblank.c 9.61 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 © 2015 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.
 */

/** @file kms_vblank.c
 *
 * This is a test of performance of drmWaitVblank.
 */

Thomas Wood's avatar
Thomas Wood committed
29
#include "igt.h"
30 31 32 33 34 35 36
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <inttypes.h>
#include <errno.h>
#include <time.h>
37
#include <sys/poll.h>
38 39 40 41 42 43 44 45 46 47
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>

#include <drm.h>

#include "intel_bufmgr.h"

IGT_TEST_DESCRIPTION("Test speed of WaitVblank.");

48 49 50 51 52
typedef struct {
	igt_display_t display;
	struct igt_fb primary_fb;
	igt_output_t *output;
	enum pipe pipe;
53 54 55 56
	unsigned int flags;
#define IDLE 1
#define BUSY 2
#define FORKED 4
57 58
} data_t;

59 60 61 62 63 64 65
static double elapsed(const struct timespec *start,
		      const struct timespec *end,
		      int loop)
{
	return (1e6*(end->tv_sec - start->tv_sec) + (end->tv_nsec - start->tv_nsec)/1000)/loop;
}

66
static void prepare_crtc(data_t *data, int fd, igt_output_t *output)
67
{
68 69 70 71 72 73
	drmModeModeInfo *mode;
	igt_display_t *display = &data->display;
	igt_plane_t *primary;

	/* select the pipe we want to use */
	igt_output_set_pipe(output, data->pipe);
74

75 76 77 78 79 80 81 82
	/* create and set the primary plane fb */
	mode = igt_output_get_mode(output);
	igt_create_color_fb(fd, mode->hdisplay, mode->vdisplay,
			    DRM_FORMAT_XRGB8888,
			    LOCAL_DRM_FORMAT_MOD_NONE,
			    0.0, 0.0, 0.0,
			    &data->primary_fb);

83
	primary = igt_output_get_plane_type(output, DRM_PLANE_TYPE_PRIMARY);
84 85 86 87 88 89 90 91 92 93 94 95 96 97
	igt_plane_set_fb(primary, &data->primary_fb);

	igt_display_commit(display);

	igt_wait_for_vblank(fd, data->pipe);
}

static void cleanup_crtc(data_t *data, int fd, igt_output_t *output)
{
	igt_display_t *display = &data->display;
	igt_plane_t *primary;

	igt_remove_fb(fd, &data->primary_fb);

98
	primary = igt_output_get_plane_type(output, DRM_PLANE_TYPE_PRIMARY);
99 100 101 102 103 104
	igt_plane_set_fb(primary, NULL);

	igt_output_set_pipe(output, PIPE_ANY);
	igt_display_commit(display);
}

105
static int wait_vblank(int fd, union drm_wait_vblank *vbl)
106
{
107 108 109 110 111 112 113 114 115 116 117 118 119
	int err;

	err = 0;
	if (igt_ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl))
		err = -errno;

	return err;
}

static void run_test(data_t *data, int fd, void (*testfunc)(data_t *, int, int))
{
	int nchildren =
		data->flags & FORKED ? sysconf(_SC_NPROCESSORS_ONLN) : 1;
120 121 122 123
	igt_display_t *display = &data->display;
	igt_output_t *output;
	enum pipe p;

124 125 126
	for_each_pipe_with_valid_output(display, p, output) {
		data->pipe = p;
		prepare_crtc(data, fd, output);
127

128 129 130 131 132 133 134 135
		igt_info("Beginning %s on pipe %s, connector %s (%d threads)\n",
			 igt_subtest_name(),
			 kmstest_pipe_name(data->pipe),
			 igt_output_name(output),
			 nchildren);

		if (data->flags & BUSY) {
			union drm_wait_vblank vbl;
136

137 138 139 140 141 142 143
			memset(&vbl, 0, sizeof(vbl));
			vbl.request.type =
				DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT;
			vbl.request.type |= kmstest_get_vbl_flag(data->pipe);
			vbl.request.sequence = 120 + 12;
			igt_assert_eq(wait_vblank(fd, &vbl), 0);
		}
144

145 146 147 148 149 150 151 152 153 154
		igt_fork(child, nchildren)
			testfunc(data, fd, nchildren);
		igt_waitchildren();

		if (data->flags & BUSY) {
			struct drm_event_vblank buf;
			igt_assert_eq(read(fd, &buf, sizeof(buf)), sizeof(buf));
		}

		igt_assert(poll(&(struct pollfd){fd, POLLIN}, 1, 0) == 0);
155

156
		igt_info("\n%s on pipe %s, connector %s: PASSED\n\n",
157 158 159
			 igt_subtest_name(),
			 kmstest_pipe_name(data->pipe),
			 igt_output_name(output));
160

161 162
		/* cleanup what prepare_crtc() has done */
		cleanup_crtc(data, fd, output);
163
	}
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
}

static void crtc_id_subtest(data_t *data, int fd)
{
	igt_display_t *display = &data->display;
	igt_output_t *output;
	enum pipe p;

	for_each_pipe_with_valid_output(display, p, output) {
		struct drm_event_vblank buf;
		const uint32_t pipe_id_flag = kmstest_get_vbl_flag(p);
		unsigned crtc_id, expected_crtc_id;
		uint64_t val;
		union drm_wait_vblank vbl;

		crtc_id = display->pipes[p].crtc_id;
		if (drmGetCap(display->drm_fd, DRM_CAP_CRTC_IN_VBLANK_EVENT, &val) == 0)
			expected_crtc_id = crtc_id;
		else
			expected_crtc_id = 0;

		data->pipe = p;
		prepare_crtc(data, fd, output);

		memset(&vbl, 0, sizeof(vbl));
		vbl.request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT;
		vbl.request.type |= pipe_id_flag;
		vbl.request.sequence = 1;
		igt_assert_eq(wait_vblank(fd, &vbl), 0);

		igt_assert_eq(read(fd, &buf, sizeof(buf)), sizeof(buf));
		igt_assert_eq(buf.crtc_id, expected_crtc_id);

		do_or_die(drmModePageFlip(fd, crtc_id,
					  data->primary_fb.fb_id,
					  DRM_MODE_PAGE_FLIP_EVENT, NULL));

		igt_assert_eq(read(fd, &buf, sizeof(buf)), sizeof(buf));
		igt_assert_eq(buf.crtc_id, expected_crtc_id);

		if (display->is_atomic) {
			igt_plane_t *primary = igt_output_get_plane(output, 0);

			igt_plane_set_fb(primary, &data->primary_fb);
			igt_display_commit_atomic(display, DRM_MODE_PAGE_FLIP_EVENT, NULL);
209

210 211 212 213 214 215 216
			igt_assert_eq(read(fd, &buf, sizeof(buf)), sizeof(buf));
			igt_assert_eq(buf.crtc_id, expected_crtc_id);
		}

		cleanup_crtc(data, fd, output);
		return;
	}
217 218
}

219
static void accuracy(data_t *data, int fd, int nchildren)
220
{
221
	const uint32_t pipe_id_flag = kmstest_get_vbl_flag(data->pipe);
222 223
	union drm_wait_vblank vbl;
	unsigned long target;
224
	int total = 120 / nchildren;
225 226 227 228
	int n;

	memset(&vbl, 0, sizeof(vbl));
	vbl.request.type = DRM_VBLANK_RELATIVE;
229
	vbl.request.type |= pipe_id_flag;
230
	vbl.request.sequence = 1;
231
	igt_assert_eq(wait_vblank(fd, &vbl), 0);
232

233 234
	target = vbl.reply.sequence + total;
	for (n = 0; n < total; n++) {
235
		vbl.request.type = DRM_VBLANK_RELATIVE;
236
		vbl.request.type |= pipe_id_flag;
237
		vbl.request.sequence = 1;
238
		igt_assert_eq(wait_vblank(fd, &vbl), 0);
239 240

		vbl.request.type = DRM_VBLANK_ABSOLUTE | DRM_VBLANK_EVENT;
241
		vbl.request.type |= pipe_id_flag;
242
		vbl.request.sequence = target;
243
		igt_assert_eq(wait_vblank(fd, &vbl), 0);
244 245
	}
	vbl.request.type = DRM_VBLANK_RELATIVE;
246
	vbl.request.type |= pipe_id_flag;
247
	vbl.request.sequence = 0;
248
	igt_assert_eq(wait_vblank(fd, &vbl), 0);
249 250
	igt_assert_eq(vbl.reply.sequence, target);

251
	for (n = 0; n < total; n++) {
252 253 254 255 256 257
		struct drm_event_vblank ev;
		igt_assert_eq(read(fd, &ev, sizeof(ev)), sizeof(ev));
		igt_assert_eq(ev.sequence, target);
	}
}

258
static void vblank_query(data_t *data, int fd, int nchildren)
259
{
260
	const uint32_t pipe_id_flag = kmstest_get_vbl_flag(data->pipe);
261 262 263 264 265 266
	union drm_wait_vblank vbl;
	struct timespec start, end;
	unsigned long sq, count = 0;

	memset(&vbl, 0, sizeof(vbl));
	vbl.request.type = DRM_VBLANK_RELATIVE;
267
	vbl.request.type |= pipe_id_flag;
268
	vbl.request.sequence = 0;
269
	igt_assert_eq(wait_vblank(fd, &vbl), 0);
270 271 272 273 274 275

	sq = vbl.reply.sequence;

	clock_gettime(CLOCK_MONOTONIC, &start);
	do {
		vbl.request.type = DRM_VBLANK_RELATIVE;
276
		vbl.request.type |= pipe_id_flag;
277
		vbl.request.sequence = 0;
278
		igt_assert_eq(wait_vblank(fd, &vbl), 0);
279
		count++;
280
	} while ((vbl.reply.sequence - sq) <= 120);
281 282 283
	clock_gettime(CLOCK_MONOTONIC, &end);

	igt_info("Time to query current counter (%s):		%7.3fµs\n",
284
		 data->flags & BUSY ? "busy" : "idle", elapsed(&start, &end, count));
285 286
}

287
static void vblank_wait(data_t *data, int fd, int nchildren)
288
{
289
	const uint32_t pipe_id_flag = kmstest_get_vbl_flag(data->pipe);
290 291 292 293 294 295
	union drm_wait_vblank vbl;
	struct timespec start, end;
	unsigned long sq, count = 0;

	memset(&vbl, 0, sizeof(vbl));
	vbl.request.type = DRM_VBLANK_RELATIVE;
296
	vbl.request.type |= pipe_id_flag;
297
	vbl.request.sequence = 0;
298
	igt_assert_eq(wait_vblank(fd, &vbl), 0);
299 300 301 302 303 304

	sq = vbl.reply.sequence;

	clock_gettime(CLOCK_MONOTONIC, &start);
	do {
		vbl.request.type = DRM_VBLANK_RELATIVE;
305
		vbl.request.type |= pipe_id_flag;
306
		vbl.request.sequence = 1;
307
		igt_assert_eq(wait_vblank(fd, &vbl), 0);
308
		count++;
309
	} while ((vbl.reply.sequence - sq) <= 120);
310 311 312 313
	clock_gettime(CLOCK_MONOTONIC, &end);

	igt_info("Time to wait for %ld/%d vblanks (%s):		%7.3fµs\n",
		 count, (int)(vbl.reply.sequence - sq),
314
		 data->flags & BUSY ? "busy" : "idle",
315
		 elapsed(&start, &end, count));
316 317 318 319 320
}

igt_main
{
	int fd;
321
	data_t data;
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
	const struct {
		const char *name;
		void (*func)(data_t *, int, int);
		unsigned int valid;
	} funcs[] = {
		{ "accuracy", accuracy, IDLE },
		{ "query", vblank_query, IDLE | FORKED | BUSY },
		{ "wait", vblank_wait, IDLE | FORKED | BUSY },
		{ }
	}, *f;
	const struct {
		const char *name;
		unsigned int flags;
	} modes[] = {
		{ "idle", IDLE },
		{ "forked", IDLE | FORKED },
		{ "busy", BUSY },
		{ "forked-busy", BUSY | FORKED },
		{ }
	}, *m;
342 343 344 345

	igt_skip_on_simulation();

	igt_fixture {
Tomeu Vizoso's avatar
Tomeu Vizoso committed
346
		fd = drm_open_driver(DRIVER_ANY);
347 348
		kmstest_set_vt_graphics_mode();
		igt_display_init(&data.display, fd);
349
		igt_display_require_output(&data.display);
350 351
	}

352 353 354
	igt_subtest("crtc-id")
		crtc_id_subtest(&data, fd);

355 356 357 358
	for (f = funcs; f->name; f++) {
		for (m = modes; m->name; m++) {
			if (m->flags & ~f->valid)
				continue;
359

360 361 362 363 364
			igt_subtest_f("%s-%s", f->name, m->name) {
				data.flags = m->flags;
				run_test(&data, fd, f->func);
			}
		}
365
	}
366
}