1 /*
2  * Copyright 2012 Intel Corporation
3  *   Jesse Barnes <jesse.barnes@intel.com>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "config.h"
25 
26 #include "igt.h"
27 
28 #if defined(USE_CAIRO_PIXMAN)
29 #include <cairo.h>
30 #endif
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <math.h>
34 #include <stdint.h>
35 #include <unistd.h>
36 #include <sys/poll.h>
37 #include <sys/time.h>
38 #include <sys/ioctl.h>
39 #ifdef HAVE_LINUX_KD_H
40 #include <linux/kd.h>
41 #elif HAVE_SYS_KD_H
42 #include <sys/kd.h>
43 #endif
44 #include <time.h>
45 #include <pthread.h>
46 
47 #include "igt_stats.h"
48 
49 #define TEST_DPMS		(1 << 0)
50 
51 #define TEST_PAN		(1 << 3)
52 #define TEST_MODESET		(1 << 4)
53 #define TEST_CHECK_TS		(1 << 5)
54 #define TEST_EBUSY		(1 << 6)
55 #define TEST_EINVAL		(1 << 7)
56 #define TEST_FLIP		(1 << 8)
57 #define TEST_VBLANK		(1 << 9)
58 #define TEST_VBLANK_BLOCK	(1 << 10)
59 #define TEST_VBLANK_ABSOLUTE	(1 << 11)
60 #define TEST_VBLANK_EXPIRED_SEQ	(1 << 12)
61 #define TEST_FB_RECREATE	(1 << 13)
62 #define TEST_RMFB		(1 << 14)
63 #define TEST_HANG		(1 << 15)
64 #define TEST_NOEVENT		(1 << 16)
65 
66 #define TEST_SINGLE_BUFFER	(1 << 18)
67 #define TEST_DPMS_OFF		(1 << 19)
68 #define TEST_NO_2X_OUTPUT	(1 << 20)
69 #define TEST_DPMS_OFF_OTHERS	(1 << 21)
70 #define TEST_ENOENT		(1 << 22)
71 #define TEST_FENCE_STRESS	(1 << 23)
72 #define TEST_VBLANK_RACE	(1 << 24)
73 #define TEST_SUSPEND		(1 << 26)
74 #define TEST_BO_TOOBIG		(1 << 28)
75 
76 #define TEST_NO_VBLANK		(1 << 29)
77 #define TEST_BASIC		(1 << 30)
78 
79 #define EVENT_FLIP		(1 << 0)
80 #define EVENT_VBLANK		(1 << 1)
81 
82 #define RUN_TEST		1
83 #define RUN_PAIR		2
84 
85 #ifndef DRM_CAP_TIMESTAMP_MONOTONIC
86 #define DRM_CAP_TIMESTAMP_MONOTONIC 6
87 #endif
88 
89 drmModeRes *resources;
90 int drm_fd;
91 static drm_intel_bufmgr *bufmgr;
92 struct intel_batchbuffer *batch;
93 uint32_t devid;
94 int test_time = 3;
95 static bool monotonic_timestamp;
96 static pthread_t vblank_wait_thread;
97 
98 static drmModeConnector *last_connector;
99 
100 uint32_t *fb_ptr;
101 
102 struct type_name {
103 	int type;
104 	const char *name;
105 };
106 
107 struct event_state {
108 	const char *name;
109 
110 	/*
111 	 * Event data for the last event that has already passed our check.
112 	 * Updated using the below current_* vars in update_state().
113 	 */
114 	struct timeval last_ts;			/* kernel reported timestamp */
115 	struct timeval last_received_ts;	/* the moment we received it */
116 	unsigned int last_seq;			/* kernel reported seq. num */
117 
118 	/*
119 	 * Event data for for the current event that we just received and
120 	 * going to check for validity. Set in event_handler().
121 	 */
122 	struct timeval current_ts;		/* kernel reported timestamp */
123 	struct timeval current_received_ts;	/* the moment we received it */
124 	unsigned int current_seq;		/* kernel reported seq. num */
125 
126 	int count;				/* # of events of this type */
127 
128 	/* Step between the current and next 'target' sequence number. */
129 	int seq_step;
130 };
131 
vblank_dependence(int flags)132 static bool vblank_dependence(int flags)
133 {
134 	int vblank_flags = TEST_VBLANK | TEST_VBLANK_BLOCK |
135 			   TEST_VBLANK_ABSOLUTE | TEST_VBLANK_EXPIRED_SEQ |
136 			   TEST_CHECK_TS | TEST_VBLANK_RACE | TEST_EBUSY;
137 
138 	if (flags & vblank_flags)
139 		return true;
140 
141 	return false;
142 }
143 
timeval_float(const struct timeval * tv)144 static float timeval_float(const struct timeval *tv)
145 {
146 	return tv->tv_sec + tv->tv_usec / 1000000.0f;
147 }
148 
dump_event_state(const struct event_state * es)149 static void dump_event_state(const struct event_state *es)
150 {
151 	igt_debug("name = %s\n"
152 		  "last_ts = %.06f\n"
153 		  "last_received_ts = %.06f\n"
154 		  "last_seq = %u\n"
155 		  "current_ts = %.06f\n"
156 		  "current_received_ts = %.06f\n"
157 		  "current_seq = %u\n"
158 		  "count = %u\n"
159 		  "seq_step = %d\n",
160 		  es->name,
161 		  timeval_float(&es->last_ts),
162 		  timeval_float(&es->last_received_ts),
163 		  es->last_seq,
164 		  timeval_float(&es->current_ts),
165 		  timeval_float(&es->current_received_ts),
166 		  es->current_seq,
167 		  es->count,
168 		  es->seq_step);
169 }
170 
171 struct test_output {
172 	int mode_valid;
173 	drmModeModeInfo kmode[4];
174 	drmModeEncoder *kencoder[4];
175 	drmModeConnector *kconnector[4];
176 	uint32_t _connector[4];
177 	uint32_t _crtc[4];
178 	int _pipe[4];
179 	int count; /* 1:1 mapping between crtc:connector */
180 	int flags;
181 	int pipe; /* primary pipe for vblank */
182 	unsigned int current_fb_id;
183 	unsigned int fb_width;
184 	unsigned int fb_height;
185 	unsigned int fb_ids[3];
186 	int bpp, depth;
187 	struct igt_fb fb_info[3];
188 
189 	struct event_state flip_state;
190 	struct event_state vblank_state;
191 	/* Overall step between each round */
192 	int seq_step;
193 	unsigned int pending_events;
194 	int flip_count;
195 
196 	double vblank_interval;
197 };
198 
199 
gettime_us(void)200 static unsigned long gettime_us(void)
201 {
202 	struct timespec ts;
203 
204 	clock_gettime(CLOCK_MONOTONIC, &ts);
205 
206 	return ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
207 }
208 
emit_fence_stress(struct test_output * o)209 static void emit_fence_stress(struct test_output *o)
210 {
211 	const int num_fences = gem_available_fences(drm_fd);
212 	struct igt_fb *fb_info = &o->fb_info[o->current_fb_id];
213 	struct drm_i915_gem_execbuffer2 execbuf;
214 	struct drm_i915_gem_exec_object2 *exec;
215 	uint32_t buf[2] = { MI_BATCH_BUFFER_END, 0 };
216 	drm_intel_bo **bo;
217 	int i;
218 
219 	igt_require(bufmgr);
220 
221 	bo = calloc(sizeof(*bo), num_fences);
222 	exec = calloc(sizeof(*exec), num_fences+1);
223 	for (i = 0; i < num_fences - 1; i++) {
224 		uint32_t tiling = I915_TILING_X;
225 		unsigned long pitch = 0;
226 		bo[i] = drm_intel_bo_alloc_tiled(bufmgr,
227 						 "X tiled bo", 1024, 1024, 4,
228 						 &tiling, &pitch, 0);
229 		exec[i].handle = bo[i]->handle;
230 		exec[i].flags = EXEC_OBJECT_NEEDS_FENCE;
231 	}
232 	exec[i].handle = fb_info->gem_handle;
233 	exec[i].flags = EXEC_OBJECT_NEEDS_FENCE;
234 	exec[++i].handle = gem_create(drm_fd, 4096);
235 	gem_write(drm_fd, exec[i].handle, 0, buf, sizeof(buf));
236 
237 	memset(&execbuf, 0, sizeof(execbuf));
238 	execbuf.buffers_ptr = (uintptr_t)exec;
239 	execbuf.buffer_count = i + 1;
240 	execbuf.batch_len = sizeof(buf);
241 	if (HAS_BLT_RING(intel_get_drm_devid(drm_fd)))
242 		execbuf.flags = I915_EXEC_BLT;
243 
244 	gem_execbuf(drm_fd, &execbuf);
245 
246 	gem_close(drm_fd, exec[i].handle);
247 	for (i = 0; i < num_fences - 1; i++)
248 		drm_intel_bo_unreference(bo[i]);
249 	free(bo);
250 	free(exec);
251 }
252 
dpms_off_other_outputs(struct test_output * o)253 static void dpms_off_other_outputs(struct test_output *o)
254 {
255 	int i, n;
256 	drmModeConnector *connector;
257 	uint32_t connector_id;
258 
259 	for (i = 0; i < resources->count_connectors; i++) {
260 		connector_id = resources->connectors[i];
261 
262 		for (n = 0; n < o->count; n++) {
263 			if (connector_id == o->kconnector[n]->connector_id)
264 				goto next;
265 		}
266 
267 		connector = drmModeGetConnectorCurrent(drm_fd, connector_id);
268 
269 		kmstest_set_connector_dpms(drm_fd, connector,  DRM_MODE_DPMS_ON);
270 		kmstest_set_connector_dpms(drm_fd, connector,  DRM_MODE_DPMS_OFF);
271 
272 		drmModeFreeConnector(connector);
273 next:
274 		;
275 	}
276 }
277 
set_dpms(struct test_output * o,int mode)278 static void set_dpms(struct test_output *o, int mode)
279 {
280 	for (int n = 0; n < o->count; n++)
281 		kmstest_set_connector_dpms(drm_fd, o->kconnector[n], mode);
282 }
283 
set_flag(unsigned int * v,unsigned int flag)284 static void set_flag(unsigned int *v, unsigned int flag)
285 {
286 	igt_assert(!(*v & flag));
287 	*v |= flag;
288 }
289 
clear_flag(unsigned int * v,unsigned int flag)290 static void clear_flag(unsigned int *v, unsigned int flag)
291 {
292 	igt_assert(*v & flag);
293 	*v &= ~flag;
294 }
295 
do_page_flip(struct test_output * o,uint32_t fb_id,bool event)296 static int do_page_flip(struct test_output *o, uint32_t fb_id, bool event)
297 {
298 	int n, ret = 0;
299 
300 	o->flip_count = 0;
301 
302 	for (n = 0; ret == 0 && n < o->count; n++)
303 		ret = drmModePageFlip(drm_fd, o->_crtc[n], fb_id,
304 				      event ? DRM_MODE_PAGE_FLIP_EVENT : 0,
305 				      event ? (void *)((unsigned long)o | (n==0)) : NULL);
306 
307 	if (ret == 0 && event)
308 		set_flag(&o->pending_events, EVENT_FLIP);
309 
310 	return ret;
311 }
312 
313 struct vblank_reply {
314 	unsigned int sequence;
315 	struct timeval ts;
316 };
317 
__wait_for_vblank(unsigned int flags,int crtc_idx,int target_seq,unsigned long ret_data,struct vblank_reply * reply)318 static int __wait_for_vblank(unsigned int flags, int crtc_idx,
319 			      int target_seq, unsigned long ret_data,
320 			      struct vblank_reply *reply)
321 {
322 	drmVBlank wait_vbl;
323 	int ret;
324 	uint32_t pipe_id_flag;
325 	bool event = !(flags & TEST_VBLANK_BLOCK);
326 
327 	memset(&wait_vbl, 0, sizeof(wait_vbl));
328 	pipe_id_flag = kmstest_get_vbl_flag(crtc_idx);
329 
330 	wait_vbl.request.type = pipe_id_flag;
331 	if (flags & TEST_VBLANK_ABSOLUTE)
332 		wait_vbl.request.type |= DRM_VBLANK_ABSOLUTE;
333 	else
334 		wait_vbl.request.type |= DRM_VBLANK_RELATIVE;
335 	if (event) {
336 		wait_vbl.request.type |= DRM_VBLANK_EVENT;
337 		wait_vbl.request.signal = ret_data;
338 	}
339 	wait_vbl.request.sequence = target_seq;
340 
341 	ret = drmWaitVBlank(drm_fd, &wait_vbl);
342 
343 	if (ret == 0) {
344 		reply->ts.tv_sec = wait_vbl.reply.tval_sec;
345 		reply->ts.tv_usec = wait_vbl.reply.tval_usec;
346 		reply->sequence = wait_vbl.reply.sequence;
347 	} else
348 		ret = -errno;
349 
350 	return ret;
351 }
352 
do_wait_for_vblank(struct test_output * o,int pipe_id,int target_seq,struct vblank_reply * reply)353 static int do_wait_for_vblank(struct test_output *o, int pipe_id,
354 			      int target_seq, struct vblank_reply *reply)
355 {
356 	int ret;
357 	unsigned flags = o->flags;
358 
359 	/* Absolute waits only works once we have a frame counter. */
360 	if (!(o->vblank_state.count > 0))
361 		flags &= ~TEST_VBLANK_ABSOLUTE;
362 
363 	ret = __wait_for_vblank(flags, pipe_id, target_seq, (unsigned long)o,
364 				reply);
365 	if (ret == 0 && !(o->flags & TEST_VBLANK_BLOCK))
366 		set_flag(&o->pending_events, EVENT_VBLANK);
367 
368 	return ret;
369 }
370 
371 static bool
analog_tv_connector(const struct test_output * o)372 analog_tv_connector(const struct test_output *o)
373 {
374 	uint32_t connector_type = o->kconnector[0]->connector_type;
375 
376 	return connector_type == DRM_MODE_CONNECTOR_TV ||
377 		connector_type == DRM_MODE_CONNECTOR_9PinDIN ||
378 		connector_type == DRM_MODE_CONNECTOR_SVIDEO ||
379 		connector_type == DRM_MODE_CONNECTOR_Composite;
380 }
381 
event_handler(struct event_state * es,unsigned int frame,unsigned int sec,unsigned int usec)382 static void event_handler(struct event_state *es, unsigned int frame,
383 			  unsigned int sec, unsigned int usec)
384 {
385 	struct timeval now;
386 
387 	if (monotonic_timestamp) {
388 		struct timespec ts;
389 
390 		clock_gettime(CLOCK_MONOTONIC, &ts);
391 		now.tv_sec = ts.tv_sec;
392 		now.tv_usec = ts.tv_nsec / 1000;
393 	} else {
394 		gettimeofday(&now, NULL);
395 	}
396 	es->current_received_ts = now;
397 
398 	es->current_ts.tv_sec = sec;
399 	es->current_ts.tv_usec = usec;
400 	es->current_seq = frame;
401 }
402 
page_flip_handler(int fd,unsigned int frame,unsigned int sec,unsigned int usec,void * data)403 static void page_flip_handler(int fd, unsigned int frame, unsigned int sec,
404 			      unsigned int usec, void *data)
405 {
406 	int primary = (unsigned long)data & 1;
407 	struct test_output *o = (void *)((unsigned long)data & ~ 1);
408 
409 	if (++o->flip_count == o->count)
410 		clear_flag(&o->pending_events, EVENT_FLIP);
411 	if (primary)
412 		event_handler(&o->flip_state, frame, sec, usec);
413 }
414 
mode_frame_time(const struct test_output * o)415 static double mode_frame_time(const struct test_output *o)
416 {
417 	return 1000.0 * o->kmode[0].htotal * o->kmode[0].vtotal / o->kmode[0].clock;
418 }
419 
actual_frame_time(const struct test_output * o)420 static double actual_frame_time(const struct test_output *o)
421 {
422 	igt_assert(o->flags & TEST_CHECK_TS);
423 	return o->vblank_interval;
424 }
425 
vblank_wait_thread_func(void * data)426 static void *vblank_wait_thread_func(void *data)
427 {
428 	struct test_output *o = data;
429 	struct vblank_reply reply;
430 	int i;
431 
432 	for (i = 0; i < 32; i++) {
433 		unsigned long start = gettime_us();
434 		__wait_for_vblank(TEST_VBLANK_BLOCK, o->pipe, 20, (unsigned long)o, &reply);
435 		if (gettime_us() - start > 2 * mode_frame_time(o))
436 			return (void*)1;
437 	}
438 
439 	return 0;
440 }
441 
spawn_vblank_wait_thread(struct test_output * o)442 static void spawn_vblank_wait_thread(struct test_output *o)
443 {
444 	igt_assert(pthread_create(&vblank_wait_thread, NULL,
445 				  vblank_wait_thread_func, o) == 0);
446 }
447 
join_vblank_wait_thread(void)448 static void join_vblank_wait_thread(void)
449 {
450 	igt_assert(pthread_join(vblank_wait_thread, NULL) == 0);
451 }
452 
fixup_premature_vblank_ts(struct test_output * o,struct event_state * es)453 static void fixup_premature_vblank_ts(struct test_output *o,
454 				      struct event_state *es)
455 {
456 	/*
457 	 * In case a power off event preempts the completion of a
458 	 * wait-for-vblank event the kernel will return a wf-vblank event with
459 	 * a zeroed-out timestamp. In order that check_state() doesn't
460 	 * complain replace this ts with a valid ts. As we can't calculate the
461 	 * exact timestamp, just use the time we received the event.
462 	 */
463 	struct timeval tv;
464 
465 	if (!(o->flags & (TEST_DPMS | TEST_MODESET)))
466 		return;
467 
468 	if (o->vblank_state.current_ts.tv_sec != 0 ||
469 	    o->vblank_state.current_ts.tv_usec != 0)
470 		return;
471 
472 	tv.tv_sec = 0;
473 	tv.tv_usec = 1;
474 	timersub(&es->current_received_ts, &tv, &es->current_ts);
475 }
476 
vblank_handler(int fd,unsigned int frame,unsigned int sec,unsigned int usec,void * data)477 static void vblank_handler(int fd, unsigned int frame, unsigned int sec,
478 			      unsigned int usec, void *data)
479 {
480 	struct test_output *o = data;
481 
482 	clear_flag(&o->pending_events, EVENT_VBLANK);
483 	event_handler(&o->vblank_state, frame, sec, usec);
484 	fixup_premature_vblank_ts(o, &o->vblank_state);
485 }
486 
check_state(const struct test_output * o,const struct event_state * es)487 static void check_state(const struct test_output *o, const struct event_state *es)
488 {
489 	struct timeval diff;
490 
491 	dump_event_state(es);
492 
493 	timersub(&es->current_ts, &es->current_received_ts, &diff);
494 	if (!analog_tv_connector(o)) {
495 		igt_assert_f(diff.tv_sec < 0 || (diff.tv_sec == 0 && diff.tv_usec <= 2000),
496 			     "%s ts delayed for too long: %.06f\n",
497 			     es->name, timeval_float(&diff));
498 	}
499 
500 	if (es->count == 0)
501 		return;
502 
503 	timersub(&es->current_ts, &es->last_received_ts, &diff);
504 	igt_assert_f(timercmp(&es->last_received_ts, &es->current_ts, <),
505 		     "%s ts before the %s was issued!\n"
506 		     "timerdiff %.06f\n",
507 		     es->name, es->name, timeval_float(&diff));
508 
509 	/* check only valid if no modeset happens in between, that increments by
510 	 * (1 << 23) on each step. This bounding matches the one in
511 	 * DRM_IOCTL_WAIT_VBLANK. */
512 	if (!(o->flags & (TEST_DPMS | TEST_MODESET | TEST_NO_VBLANK)))
513 		igt_assert_f(es->current_seq - (es->last_seq + o->seq_step) <= 1UL << 23,
514 			     "unexpected %s seq %u, should be >= %u\n",
515 			     es->name, es->current_seq, es->last_seq + o->seq_step);
516 
517 	if (o->flags & TEST_CHECK_TS) {
518 		double elapsed, expected;
519 
520 		timersub(&es->current_ts, &es->last_ts, &diff);
521 		elapsed = 1e6*diff.tv_sec + diff.tv_usec;
522 		expected = (es->current_seq - es->last_seq) * actual_frame_time(o);
523 
524 		igt_debug("%s ts/seq: last %.06f/%u, current %.06f/%u: elapsed=%.1fus expected=%.1fus +- %.1fus, error %.1f%%\n",
525 			  es->name, timeval_float(&es->last_ts), es->last_seq,
526 			  timeval_float(&es->current_ts), es->current_seq,
527 			  elapsed, expected, expected * 0.005,
528 			  fabs((elapsed - expected) / expected) * 100);
529 
530 		igt_assert_f(fabs((elapsed - expected) / expected) <= 0.005,
531 			     "inconsistent %s ts/seq: last %.06f/%u, current %.06f/%u: elapsed=%.1fus expected=%.1fus\n",
532 			     es->name, timeval_float(&es->last_ts), es->last_seq,
533 			     timeval_float(&es->current_ts), es->current_seq,
534 			     elapsed, expected);
535 
536 		igt_assert_f(es->current_seq == es->last_seq + o->seq_step,
537 			     "unexpected %s seq %u, expected %u\n",
538 			     es->name, es->current_seq,
539 			     es->last_seq + o->seq_step);
540 	}
541 }
542 
check_state_correlation(struct test_output * o,struct event_state * es1,struct event_state * es2)543 static void check_state_correlation(struct test_output *o,
544 				    struct event_state *es1,
545 				    struct event_state *es2)
546 {
547 	struct timeval tv_diff;
548 	double ftime;
549 	double usec_diff;
550 	int seq_diff;
551 
552 	if (es1->count == 0 || es2->count == 0)
553 		return;
554 
555 	timersub(&es2->current_ts, &es1->current_ts, &tv_diff);
556 	usec_diff = tv_diff.tv_sec * USEC_PER_SEC + tv_diff.tv_usec;
557 
558 	seq_diff = es2->current_seq - es1->current_seq;
559 	ftime = mode_frame_time(o);
560 	usec_diff -= seq_diff * ftime;
561 
562 	igt_assert_f(fabs(usec_diff) / ftime <= 0.005,
563 		     "timestamp mismatch between %s and %s (diff %.6f sec)\n",
564 		     es1->name, es2->name, usec_diff / USEC_PER_SEC);
565 }
566 
check_all_state(struct test_output * o,unsigned int completed_events)567 static void check_all_state(struct test_output *o,
568 			    unsigned int completed_events)
569 {
570 	bool flip, vblank;
571 
572 	flip = completed_events & EVENT_FLIP;
573 	vblank = completed_events & EVENT_VBLANK;
574 
575 	if (flip)
576 		check_state(o, &o->flip_state);
577 	if (vblank)
578 		check_state(o, &o->vblank_state);
579 
580 	/* FIXME: Correlation check is broken. */
581 	if (flip && vblank && 0)
582 		check_state_correlation(o, &o->flip_state, &o->vblank_state);
583 }
584 
recreate_fb(struct test_output * o)585 static void recreate_fb(struct test_output *o)
586 {
587 	drmModeFBPtr r;
588 	struct igt_fb *fb_info = &o->fb_info[o->current_fb_id];
589 	uint32_t new_fb_id;
590 
591 	/* Call rmfb/getfb/addfb to ensure those don't introduce stalls */
592 	r = drmModeGetFB(drm_fd, fb_info->fb_id);
593 	igt_assert(r);
594 
595 	do_or_die(drmModeAddFB(drm_fd, o->fb_width, o->fb_height, o->depth,
596 			       o->bpp, fb_info->strides[0],
597 			       r->handle, &new_fb_id));
598 
599 	gem_close(drm_fd, r->handle);
600 	drmFree(r);
601 	do_or_die(drmModeRmFB(drm_fd, fb_info->fb_id));
602 
603 	o->fb_ids[o->current_fb_id] = new_fb_id;
604 	o->fb_info[o->current_fb_id].fb_id = new_fb_id;
605 }
606 
hang_gpu(int fd)607 static igt_hang_t hang_gpu(int fd)
608 {
609 #if defined(USE_INTEL)
610 	return igt_hang_ring(fd, I915_EXEC_DEFAULT);
611 #else
612 	igt_hang_t ret = {};
613 	return ret;
614 #endif
615 }
616 
unhang_gpu(int fd,igt_hang_t hang)617 static void unhang_gpu(int fd, igt_hang_t hang)
618 {
619 #if defined(USE_INTEL)
620 	igt_post_hang_ring(fd, hang);
621 #endif
622 }
623 
is_wedged(int fd)624 static bool is_wedged(int fd)
625 {
626 	if (drmIoctl(fd, DRM_IOCTL_I915_GEM_THROTTLE, 0) == 0)
627 		return false;
628 
629 	return errno == EIO;
630 }
631 
set_mode(struct test_output * o,uint32_t fb,int x,int y)632 static int set_mode(struct test_output *o, uint32_t fb, int x, int y)
633 {
634 	int n, ret;
635 
636 	for (n = o->count - 1; n >= 0; n--) {
637 		uint32_t buffer_id = fb, x_crtc = x, y_crtc = y;
638 		uint32_t *conn = &o->_connector[n];
639 		int count = 1;
640 		drmModeModeInfoPtr mode = &o->kmode[n];
641 
642 		if (fb == 0) {
643 			buffer_id = x_crtc = y_crtc = count = 0;
644 			conn = NULL; mode = NULL;
645 		}
646 
647 		ret = drmModeSetCrtc(drm_fd, o->_crtc[n],
648 				     buffer_id, x_crtc, y_crtc,
649 				     conn, count, mode);
650 		if (ret)
651 			return ret;
652 	}
653 
654 	return 0;
655 }
656 
657 /* Return mask of completed events. */
run_test_step(struct test_output * o)658 static unsigned int run_test_step(struct test_output *o)
659 {
660 	unsigned int new_fb_id;
661 	/* for funny reasons page_flip returns -EBUSY on disabled crtcs ... */
662 	int expected_einval = o->flags & TEST_MODESET ? -EBUSY : -EINVAL;
663 	unsigned int completed_events = 0;
664 	bool do_flip;
665 	bool do_vblank;
666 	struct vblank_reply vbl_reply;
667 	unsigned int target_seq;
668 	igt_hang_t hang;
669 
670 	target_seq = o->vblank_state.seq_step;
671 	/* Absolute waits only works once we have a frame counter. */
672 	if (o->flags & TEST_VBLANK_ABSOLUTE && o->vblank_state.count > 0)
673 		target_seq += o->vblank_state.last_seq;
674 
675 	/*
676 	 * It's possible that we don't have a pending flip here, in case both
677 	 * wf-vblank and flip were scheduled and the wf-vblank event was
678 	 * delivered earlier. The same applies to vblank events w.r.t flip.
679 	 */
680 	do_flip = (o->flags & TEST_FLIP) && !(o->pending_events & EVENT_FLIP);
681 	do_vblank = (o->flags & TEST_VBLANK) &&
682 		    !(o->pending_events & EVENT_VBLANK);
683 
684 	if (o->flags & TEST_DPMS_OFF_OTHERS)
685 		dpms_off_other_outputs(o);
686 
687 	if (!(o->flags & TEST_SINGLE_BUFFER))
688 		o->current_fb_id = !o->current_fb_id;
689 
690 	if (o->flags & TEST_FB_RECREATE)
691 		recreate_fb(o);
692 	new_fb_id = o->fb_ids[o->current_fb_id];
693 
694 	if ((o->flags & TEST_VBLANK_EXPIRED_SEQ) &&
695 	    !(o->pending_events & EVENT_VBLANK) && o->flip_state.count > 0) {
696 		struct vblank_reply reply;
697 		unsigned int exp_seq;
698 		unsigned long start, end;
699 
700 		exp_seq = o->flip_state.current_seq;
701 		start = gettime_us();
702 		do_or_die(__wait_for_vblank(TEST_VBLANK_ABSOLUTE |
703 					    TEST_VBLANK_BLOCK, o->pipe, exp_seq,
704 					    0, &reply));
705 		end = gettime_us();
706 		igt_debug("Vblank took %luus\n", end - start);
707 		igt_assert(end - start < 500);
708 		igt_assert_eq(reply.sequence, exp_seq);
709 		igt_assert(timercmp(&reply.ts, &o->flip_state.last_ts, ==));
710 	}
711 
712 	if (o->flags & TEST_ENOENT) {
713 		/* hope that fb 0xfffffff0 does not exist */
714 		igt_assert_eq(do_page_flip(o, 0xfffffff0, false), -ENOENT);
715 		igt_assert_eq(set_mode(o, 0xfffffff0, 0, 0), -ENOENT);
716 	}
717 
718 	if (do_flip && (o->flags & TEST_EINVAL) && o->flip_state.count > 0)
719 		igt_assert_eq(do_page_flip(o, new_fb_id, false), expected_einval);
720 
721 	if (do_vblank && (o->flags & TEST_EINVAL) && o->vblank_state.count > 0)
722 		igt_assert_eq(do_wait_for_vblank(o, o->pipe, target_seq, &vbl_reply), -EINVAL);
723 
724 	if (o->flags & TEST_VBLANK_RACE) {
725 		spawn_vblank_wait_thread(o);
726 
727 		if (o->flags & TEST_MODESET)
728 			igt_assert_f(set_mode(o, 0 /* no fb */, 0, 0) == 0,
729 				     "failed to disable output: %s\n",
730 				     strerror(errno));
731 	}
732 
733 	if (o->flags & TEST_DPMS_OFF)
734 		set_dpms(o, DRM_MODE_DPMS_OFF);
735 
736 	if (o->flags & TEST_MODESET)
737 		igt_assert(set_mode(o, o->fb_ids[o->current_fb_id], 0, 0) == 0);
738 
739 	if (o->flags & TEST_DPMS)
740 		set_dpms(o, DRM_MODE_DPMS_ON);
741 
742 	if (o->flags & TEST_VBLANK_RACE) {
743 		struct vblank_reply reply;
744 		unsigned long start, end;
745 
746 		/* modeset/DPMS is done, vblank wait should work normally now */
747 		start = gettime_us();
748 		igt_assert(__wait_for_vblank(TEST_VBLANK_BLOCK, o->pipe, 2, 0, &reply) == 0);
749 		end = gettime_us();
750 		/*
751 		 * we waited for two vblanks, so verify that
752 		 * we were blocked for ~1-2 frames.
753 		 */
754 		igt_assert_f(end - start > 0.9 * mode_frame_time(o) &&
755 			     end - start < 2.1 * mode_frame_time(o),
756 			     "wait for two vblanks took %lu usec (frame time %f usec)\n",
757 			     end - start, mode_frame_time(o));
758 		join_vblank_wait_thread();
759 	}
760 
761 	igt_print_activity();
762 
763 	memset(&hang, 0, sizeof(hang));
764 	if (do_flip && (o->flags & TEST_HANG))
765 		hang = hang_gpu(drm_fd);
766 
767 	/* try to make sure we can issue two flips during the same frame */
768 	if (do_flip && (o->flags & TEST_EBUSY)) {
769 		struct vblank_reply reply;
770 		igt_assert(__wait_for_vblank(TEST_VBLANK_BLOCK, o->pipe, 1, 0, &reply) == 0);
771 	}
772 
773 	if (do_flip)
774 		do_or_die(do_page_flip(o, new_fb_id, !(o->flags & TEST_NOEVENT)));
775 
776 	if (o->flags & TEST_FENCE_STRESS)
777 		emit_fence_stress(o);
778 
779 	if (do_vblank) {
780 		do_or_die(do_wait_for_vblank(o, o->pipe, target_seq,
781 					     &vbl_reply));
782 		if (o->flags & TEST_VBLANK_BLOCK) {
783 			event_handler(&o->vblank_state, vbl_reply.sequence,
784 				      vbl_reply.ts.tv_sec,
785 				      vbl_reply.ts.tv_usec);
786 			completed_events = EVENT_VBLANK;
787 		}
788 	}
789 
790 	if (do_flip && (o->flags & TEST_EBUSY))
791 		igt_assert_eq(do_page_flip(o, new_fb_id, false), -EBUSY);
792 
793 	if (do_flip && (o->flags & TEST_RMFB))
794 		recreate_fb(o);
795 
796 	/* pan before the flip completes */
797 	if (o->flags & TEST_PAN) {
798 		int count = do_flip ?
799 			o->flip_state.count : o->vblank_state.count;
800 		int width = o->fb_width - o->kmode[0].hdisplay;
801 		int x_ofs = count * 10 % (2 * width);
802 		if (x_ofs >= width)
803 			x_ofs = 2 * width - x_ofs;
804 
805 		/* Make sure DSPSURF changes value */
806 		if (o->flags & TEST_HANG)
807 			o->current_fb_id = !o->current_fb_id;
808 
809 		igt_assert_f(set_mode(o, o->fb_ids[o->current_fb_id], x_ofs, 0) == 0,
810 			     "failed to pan (%dx%d@%dHz)+%d: %s\n",
811 			     o->kmode[0].hdisplay, o->kmode[0].vdisplay, o->kmode[0].vrefresh,
812 			     x_ofs, strerror(errno));
813 	}
814 
815 	if (o->flags & TEST_DPMS)
816 		set_dpms(o, DRM_MODE_DPMS_OFF);
817 
818 	if (o->flags & TEST_MODESET && !(o->flags & TEST_RMFB) && !(o->flags & TEST_VBLANK_RACE))
819 		igt_assert_f(set_mode(o, 0 /* no fb */, 0, 0) == 0,
820 			     "failed to disable output: %s\n",
821 			     strerror(errno));
822 
823 	if (o->flags & TEST_SUSPEND)
824 		igt_system_suspend_autoresume(SUSPEND_STATE_MEM,
825 					      SUSPEND_TEST_NONE);
826 
827 	if (do_vblank && (o->flags & TEST_EINVAL) && o->vblank_state.count > 0)
828 		igt_assert(do_wait_for_vblank(o, o->pipe, target_seq, &vbl_reply)
829 			   == -EINVAL);
830 
831 	if (do_flip && (o->flags & TEST_EINVAL))
832 		igt_assert(do_page_flip(o, new_fb_id, false) == expected_einval);
833 
834 	unhang_gpu(drm_fd, hang);
835 
836 	return completed_events;
837 }
838 
update_state(struct event_state * es)839 static void update_state(struct event_state *es)
840 {
841 	es->last_received_ts = es->current_received_ts;
842 	es->last_ts = es->current_ts;
843 	es->last_seq = es->current_seq;
844 	es->count++;
845 }
846 
update_all_state(struct test_output * o,unsigned int completed_events)847 static void update_all_state(struct test_output *o,
848 			     unsigned int completed_events)
849 {
850 	if (completed_events & EVENT_FLIP)
851 		update_state(&o->flip_state);
852 
853 	if (completed_events & EVENT_VBLANK)
854 		update_state(&o->vblank_state);
855 }
856 
connector_find_preferred_mode(uint32_t connector_id,int crtc_idx,struct test_output * o)857 static void connector_find_preferred_mode(uint32_t connector_id, int crtc_idx,
858 					  struct test_output *o)
859 {
860 	struct kmstest_connector_config config;
861 
862 	if (!kmstest_get_connector_config(drm_fd, connector_id, 1 << crtc_idx,
863 					  &config)) {
864 		o->mode_valid = 0;
865 		return;
866 	}
867 
868 	o->pipe = config.pipe;
869 	o->kconnector[0] = config.connector;
870 	o->kencoder[0] = config.encoder;
871 	o->_crtc[0] = config.crtc->crtc_id;
872 	o->_pipe[0] = config.pipe;
873 	o->kmode[0] = config.default_mode;
874 	o->mode_valid = 1;
875 
876 	o->fb_width = o->kmode[0].hdisplay;
877 	o->fb_height = o->kmode[0].vdisplay;
878 
879 	drmModeFreeCrtc(config.crtc);
880 }
881 
mode_compatible(const drmModeModeInfo * a,const drmModeModeInfo * b)882 static bool mode_compatible(const drmModeModeInfo *a, const drmModeModeInfo *b)
883 {
884 	int d_refresh;
885 
886 	if (a->hdisplay != b->hdisplay)
887 		return false;
888 
889 	if (a->vdisplay != b->vdisplay)
890 		return false;
891 
892 	d_refresh = a->vrefresh - b->vrefresh;
893 	if (d_refresh < -1 || d_refresh > 1)
894 		return false;
895 
896 	return true;
897 }
898 
connector_find_compatible_mode(int crtc_idx0,int crtc_idx1,struct test_output * o)899 static void connector_find_compatible_mode(int crtc_idx0, int crtc_idx1,
900 					   struct test_output *o)
901 {
902 	struct kmstest_connector_config config[2];
903 	drmModeModeInfo *mode[2];
904 	int n, m;
905 
906 	if (!kmstest_get_connector_config(drm_fd, o->_connector[0],
907 					  1 << crtc_idx0, &config[0]))
908 		return;
909 
910 	if (!kmstest_get_connector_config(drm_fd, o->_connector[1],
911 					  1 << crtc_idx1, &config[1])) {
912 		kmstest_free_connector_config(&config[0]);
913 		return;
914 	}
915 
916 	mode[0] = &config[0].default_mode;
917 	mode[1] = &config[1].default_mode;
918 	if (!mode_compatible(mode[0], mode[1])) {
919 		for (n = 0; n < config[0].connector->count_modes; n++) {
920 			mode[0] = &config[0].connector->modes[n];
921 			for (m = 0; m < config[1].connector->count_modes; m++) {
922 				mode[1] = &config[1].connector->modes[m];
923 				if (mode_compatible(mode[0], mode[1]))
924 					goto found;
925 			}
926 		}
927 
928 		/* hope for the best! */
929 		mode[1] = mode[0] = &config[0].default_mode;
930 	}
931 
932 found:
933 	o->pipe = config[0].pipe;
934 	o->fb_width = mode[0]->hdisplay;
935 	o->fb_height = mode[0]->vdisplay;
936 	o->mode_valid = 1;
937 
938 	o->kconnector[0] = config[0].connector;
939 	o->kencoder[0] = config[0].encoder;
940 	o->_crtc[0] = config[0].crtc->crtc_id;
941 	o->_pipe[0] = config[0].pipe;
942 	o->kmode[0] = *mode[0];
943 
944 	o->kconnector[1] = config[1].connector;
945 	o->kencoder[1] = config[1].encoder;
946 	o->_crtc[1] = config[1].crtc->crtc_id;
947 	o->_pipe[1] = config[1].pipe;
948 	o->kmode[1] = *mode[1];
949 
950 	drmModeFreeCrtc(config[0].crtc);
951 	drmModeFreeCrtc(config[1].crtc);
952 }
953 
paint_flip_mode(struct igt_fb * fb,bool odd_frame)954 static void paint_flip_mode(struct igt_fb *fb, bool odd_frame)
955 {
956 	/* TODO (b/145293089) resolve Cairo/Pixman dependencies */
957 #if defined(USE_CAIRO_PIXMAN)
958 	cairo_t *cr = igt_get_cairo_ctx(drm_fd, fb);
959 	int width = fb->width;
960 	int height = fb->height;
961 
962 	igt_paint_test_pattern(cr, width, height);
963 
964 	if (odd_frame)
965 		cairo_rectangle(cr, width/4, height/2, width/4, height/8);
966 	else
967 		cairo_rectangle(cr, width/2, height/2, width/4, height/8);
968 
969 	cairo_set_source_rgb(cr, 1, 1, 1);
970 	cairo_fill(cr);
971 
972 	igt_put_cairo_ctx(drm_fd, fb, cr);
973 #endif
974 }
975 
fb_is_bound(struct test_output * o,int fb)976 static bool fb_is_bound(struct test_output *o, int fb)
977 {
978 	int n;
979 
980 	for (n = 0; n < o->count; n++) {
981 		struct drm_mode_crtc mode = {
982 			.crtc_id = o->_crtc[n]
983 		};
984 
985 		if (drmIoctl(drm_fd, DRM_IOCTL_MODE_GETCRTC, &mode))
986 			return false;
987 
988 		if (!mode.mode_valid || mode.fb_id != fb)
989 			return false;
990 	}
991 
992 	return true;
993 }
994 
check_final_state(const struct test_output * o,const struct event_state * es,unsigned int elapsed)995 static void check_final_state(const struct test_output *o,
996 			      const struct event_state *es,
997 			      unsigned int elapsed)
998 {
999 	igt_assert_f(es->count > 0,
1000 		     "no %s event received\n", es->name);
1001 
1002 	/* Verify we drop no frames, but only if it's not a TV encoder, since
1003 	 * those use some funny fake timings behind userspace's back. */
1004 	if (o->flags & TEST_CHECK_TS) {
1005 		int count = es->count * o->seq_step;
1006 		unsigned int min = actual_frame_time(o) * (count - 1);
1007 		unsigned int max = actual_frame_time(o) * (count + 1);
1008 
1009 		igt_debug("expected %d, counted %d, encoder type %d\n",
1010 			  (int)(elapsed / actual_frame_time(o)), count,
1011 			  o->kencoder[0]->encoder_type);
1012 		igt_assert_f(elapsed >= min && elapsed <= max,
1013 			     "dropped frames, expected %d, counted %d, encoder type %d\n",
1014 			     (int)(elapsed / actual_frame_time(o)), count,
1015 			     o->kencoder[0]->encoder_type);
1016 	}
1017 }
1018 
1019 /*
1020  * Wait until at least one pending event completes. Return mask of completed
1021  * events.
1022  */
wait_for_events(struct test_output * o)1023 static unsigned int wait_for_events(struct test_output *o)
1024 {
1025 	drmEventContext evctx;
1026 	struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1027 	fd_set fds;
1028 	unsigned int event_mask;
1029 	int ret;
1030 
1031 	event_mask = o->pending_events;
1032 	igt_assert(event_mask);
1033 
1034 	memset(&evctx, 0, sizeof evctx);
1035 	evctx.version = 2;
1036 	evctx.vblank_handler = vblank_handler;
1037 	evctx.page_flip_handler = page_flip_handler;
1038 
1039 	FD_ZERO(&fds);
1040 	FD_SET(drm_fd, &fds);
1041 	do {
1042 		do {
1043 			ret = select(drm_fd + 1, &fds, NULL, NULL, &timeout);
1044 		} while (ret < 0 && errno == EINTR);
1045 
1046 		igt_assert_f(ret >= 0,
1047 			     "select error (errno %i)\n", errno);
1048 		igt_assert_f(ret > 0,
1049 			     "select timed out or error (ret %d)\n", ret);
1050 		igt_assert_f(!FD_ISSET(0, &fds),
1051 			     "no fds active, breaking\n");
1052 
1053 		do_or_die(drmHandleEvent(drm_fd, &evctx));
1054 	} while (o->pending_events);
1055 
1056 	event_mask ^= o->pending_events;
1057 	igt_assert(event_mask);
1058 
1059 	return event_mask;
1060 }
1061 
1062 /* Returned the elapsed time in us */
event_loop(struct test_output * o,unsigned duration_ms)1063 static unsigned event_loop(struct test_output *o, unsigned duration_ms)
1064 {
1065 	unsigned long start, end;
1066 	int count = 0;
1067 
1068 	start = gettime_us();
1069 
1070 	while (1) {
1071 		unsigned int completed_events;
1072 
1073 		completed_events = run_test_step(o);
1074 		if (o->pending_events)
1075 			completed_events |= wait_for_events(o);
1076 		check_all_state(o, completed_events);
1077 		update_all_state(o, completed_events);
1078 
1079 		if (count && (gettime_us() - start) / 1000 >= duration_ms)
1080 			break;
1081 
1082 		count++;
1083 	}
1084 
1085 	end = gettime_us();
1086 
1087 	/* Flush any remaining events */
1088 	if (o->pending_events)
1089 		wait_for_events(o);
1090 
1091 	return end - start;
1092 }
1093 
free_test_output(struct test_output * o)1094 static void free_test_output(struct test_output *o)
1095 {
1096 	int i;
1097 
1098 	for (i = 0; i < o->count; i++) {
1099 		drmModeFreeEncoder(o->kencoder[i]);
1100 		drmModeFreeConnector(o->kconnector[i]);
1101 	}
1102 }
1103 
calibrate_ts(struct test_output * o,int crtc_idx)1104 static void calibrate_ts(struct test_output *o, int crtc_idx)
1105 {
1106 #define CALIBRATE_TS_STEPS 16
1107 	drmVBlank wait;
1108 	igt_stats_t stats;
1109 	uint32_t last_seq;
1110 	uint64_t last_timestamp;
1111 	double expected;
1112 	double mean;
1113 	double stddev;
1114 	int n;
1115 
1116 	memset(&wait, 0, sizeof(wait));
1117 	wait.request.type = kmstest_get_vbl_flag(crtc_idx);
1118 	wait.request.type |= DRM_VBLANK_RELATIVE | DRM_VBLANK_NEXTONMISS;
1119 	do_or_die(drmWaitVBlank(drm_fd, &wait));
1120 
1121 	last_seq = wait.reply.sequence;
1122 	last_timestamp = wait.reply.tval_sec;
1123 	last_timestamp *= 1000000;
1124 	last_timestamp += wait.reply.tval_usec;
1125 
1126 	memset(&wait, 0, sizeof(wait));
1127 	wait.request.type = kmstest_get_vbl_flag(crtc_idx);
1128 	wait.request.type |= DRM_VBLANK_ABSOLUTE | DRM_VBLANK_EVENT;
1129 	wait.request.sequence = last_seq;
1130 	for (n = 0; n < CALIBRATE_TS_STEPS; n++) {
1131 		drmVBlank check = {};
1132 
1133 		++wait.request.sequence;
1134 		do_or_die(drmWaitVBlank(drm_fd, &wait));
1135 
1136 		/* Double check that haven't already missed the vblank */
1137 		check.request.type = kmstest_get_vbl_flag(crtc_idx);
1138 		check.request.type |= DRM_VBLANK_RELATIVE;
1139 		do_or_die(drmWaitVBlank(drm_fd, &check));
1140 
1141 		igt_assert(!igt_vblank_after(check.reply.sequence, wait.request.sequence));
1142 	}
1143 
1144 	igt_stats_init_with_size(&stats, CALIBRATE_TS_STEPS);
1145 	for (n = 0; n < CALIBRATE_TS_STEPS; n++) {
1146 		struct drm_event_vblank ev;
1147 		uint64_t now;
1148 		int poll_ret;
1149 
1150 		while (1) {
1151 			/*
1152 			 * In case of the interruptible tests, this poll may
1153 			 * be interrupted with -EINTR, handle this by restarting
1154 			 * until we poll timeout or success.
1155 			 */
1156 			poll_ret = poll(&(struct pollfd){drm_fd, POLLIN}, 1, 1000);
1157 
1158 			if (poll_ret == 1)
1159 				break;
1160 
1161 			igt_assert_neq(poll_ret, 0);
1162 			igt_assert_eq(errno, EINTR);
1163 		}
1164 		igt_assert(read(drm_fd, &ev, sizeof(ev)) == sizeof(ev));
1165 		igt_assert_eq(ev.sequence, last_seq + 1);
1166 
1167 		now = ev.tv_sec;
1168 		now *= 1000000;
1169 		now += ev.tv_usec;
1170 
1171 		igt_stats_push(&stats, now - last_timestamp);
1172 
1173 		last_timestamp = now;
1174 		last_seq = ev.sequence;
1175 	}
1176 
1177 	expected = mode_frame_time(o);
1178 
1179 	mean = igt_stats_get_mean(&stats);
1180 	stddev = igt_stats_get_std_deviation(&stats);
1181 
1182 	igt_info("Expected frametime: %.0fus; measured %.1fus +- %.3fus accuracy %.2f%%\n",
1183 		 expected, mean, stddev, 100 * 3 * stddev / mean);
1184 	/* 99.7% samples within 0.5% of the mean */
1185 	igt_assert(3 * stddev / mean < 0.005);
1186 	/* 84% samples within 0.5% of the expected value.
1187 	 * See comments in check_timings() in kms_setmode.c
1188 	 */
1189 	if (fabs(mean - expected) > 2*stddev) {
1190 		igt_info("vblank interval differs from modeline! expected %.1fus, measured %1.fus +- %.3fus, difference %.1fus (%.1f sigma)\n",
1191 			 expected, mean, stddev,
1192 			 fabs(mean - expected), fabs(mean - expected) / stddev);
1193 	}
1194 
1195 	o->vblank_interval = mean;
1196 }
1197 
run_test_on_crtc_set(struct test_output * o,int * crtc_idxs,int crtc_count,int duration_ms)1198 static void run_test_on_crtc_set(struct test_output *o, int *crtc_idxs,
1199 				 int crtc_count, int duration_ms)
1200 {
1201 	char test_name[128];
1202 	unsigned elapsed;
1203 	unsigned bo_size = 0;
1204 	uint64_t tiling;
1205 	int i;
1206 	bool vblank = true;
1207 
1208 	switch (crtc_count) {
1209 	case RUN_TEST:
1210 		connector_find_preferred_mode(o->_connector[0], crtc_idxs[0], o);
1211 		if (!o->mode_valid)
1212 			return;
1213 		snprintf(test_name, sizeof(test_name),
1214 			 "%s on pipe %s, connector %s-%d",
1215 			 igt_subtest_name(),
1216 			 kmstest_pipe_name(o->_pipe[0]),
1217 			 kmstest_connector_type_str(o->kconnector[0]->connector_type),
1218 			 o->kconnector[0]->connector_type_id);
1219 		break;
1220 	case RUN_PAIR:
1221 		connector_find_compatible_mode(crtc_idxs[0], crtc_idxs[1], o);
1222 		if (!o->mode_valid)
1223 			return;
1224 		snprintf(test_name, sizeof(test_name),
1225 			 "%s on pipe %s:%s, connector %s-%d:%s-%d",
1226 			 igt_subtest_name(),
1227 			 kmstest_pipe_name(o->_pipe[0]),
1228 			 kmstest_pipe_name(o->_pipe[1]),
1229 			 kmstest_connector_type_str(o->kconnector[0]->connector_type),
1230 			 o->kconnector[0]->connector_type_id,
1231 			 kmstest_connector_type_str(o->kconnector[1]->connector_type),
1232 			 o->kconnector[1]->connector_type_id);
1233 		break;
1234 	default:
1235 		igt_assert(0);
1236 	}
1237 
1238 	igt_assert_eq(o->count, crtc_count);
1239 
1240 	last_connector = o->kconnector[0];
1241 
1242 	igt_info("Beginning %s\n", test_name);
1243 
1244 	if (o->flags & TEST_PAN)
1245 		o->fb_width *= 2;
1246 
1247 	tiling = LOCAL_DRM_FORMAT_MOD_NONE;
1248 	if (o->flags & TEST_FENCE_STRESS)
1249 		tiling = LOCAL_I915_FORMAT_MOD_X_TILED;
1250 
1251 	/* 256 MB is usually the maximum mappable aperture,
1252 	 * (make it 4x times that to ensure failure) */
1253 	if (o->flags & TEST_BO_TOOBIG) {
1254 		bo_size = 4*gem_mappable_aperture_size();
1255 		igt_require(bo_size < gem_global_aperture_size(drm_fd));
1256 	}
1257 
1258 	o->fb_ids[0] = igt_create_fb(drm_fd, o->fb_width, o->fb_height,
1259 					 igt_bpp_depth_to_drm_format(o->bpp, o->depth),
1260 					 tiling, &o->fb_info[0]);
1261 	o->fb_ids[1] = igt_create_fb_with_bo_size(drm_fd, o->fb_width, o->fb_height,
1262 						  igt_bpp_depth_to_drm_format(o->bpp, o->depth),
1263 						  tiling, IGT_COLOR_YCBCR_BT709,
1264 						  IGT_COLOR_YCBCR_LIMITED_RANGE,
1265 						  &o->fb_info[1], bo_size, 0);
1266 
1267 	igt_assert(o->fb_ids[0]);
1268 	igt_assert(o->fb_ids[1]);
1269 
1270 	paint_flip_mode(&o->fb_info[0], false);
1271 	if (!(o->flags & TEST_BO_TOOBIG))
1272 		paint_flip_mode(&o->fb_info[1], true);
1273 	if (o->fb_ids[2])
1274 		paint_flip_mode(&o->fb_info[2], true);
1275 
1276 	for (i = 0; i < o->count; i++)
1277 		kmstest_dump_mode(&o->kmode[i]);
1278 
1279 	kmstest_unset_all_crtcs(drm_fd, resources);
1280 
1281 	if (set_mode(o, o->fb_ids[0], 0, 0)) {
1282 		/* We may fail to apply the mode if there are hidden
1283 		 * constraints, such as bandwidth on the third pipe.
1284 		 */
1285 		igt_assert_f(crtc_count > 1 || crtc_idxs[0] < 2,
1286 			     "set_mode may only fail on the 3rd pipe or in multiple crtc tests\n");
1287 		igt_info("\n%s: SKIPPED\n\n", test_name);
1288 		goto out;
1289 	}
1290 	igt_assert(fb_is_bound(o, o->fb_ids[0]));
1291 
1292 	vblank = kms_has_vblank(drm_fd);
1293 	if (!vblank) {
1294 		if (vblank_dependence(o->flags))
1295 			igt_require_f(vblank, "There is no VBlank\n");
1296 		else
1297 			o->flags |= TEST_NO_VBLANK;
1298 	}
1299 
1300 	/* quiescent the hw a bit so ensure we don't miss a single frame */
1301 	if (o->flags & TEST_CHECK_TS)
1302 		calibrate_ts(o, crtc_idxs[0]);
1303 
1304 	if (o->flags & TEST_BO_TOOBIG) {
1305 		int err = do_page_flip(o, o->fb_ids[1], true);
1306 		igt_assert(err == 0 || err == -E2BIG);
1307 		if (err)
1308 			goto out;
1309 	} else {
1310 		igt_assert_eq(do_page_flip(o, o->fb_ids[1], true), 0);
1311 	}
1312 	wait_for_events(o);
1313 
1314 	o->current_fb_id = 1;
1315 
1316 	if (o->flags & TEST_FLIP)
1317 		o->flip_state.seq_step = 1;
1318 	else
1319 		o->flip_state.seq_step = 0;
1320 	if (o->flags & TEST_VBLANK)
1321 		o->vblank_state.seq_step = 10;
1322 	else
1323 		o->vblank_state.seq_step = 0;
1324 
1325 	/* We run the vblank and flip actions in parallel by default. */
1326 	o->seq_step = max(o->vblank_state.seq_step, o->flip_state.seq_step);
1327 
1328 	elapsed = event_loop(o, duration_ms);
1329 
1330 	if (o->flags & TEST_FLIP && !(o->flags & TEST_NOEVENT))
1331 		check_final_state(o, &o->flip_state, elapsed);
1332 	if (o->flags & TEST_VBLANK)
1333 		check_final_state(o, &o->vblank_state, elapsed);
1334 
1335 	igt_info("\n%s: PASSED\n\n", test_name);
1336 
1337 out:
1338 	igt_remove_fb(drm_fd, &o->fb_info[2]);
1339 	igt_remove_fb(drm_fd, &o->fb_info[1]);
1340 	igt_remove_fb(drm_fd, &o->fb_info[0]);
1341 
1342 	last_connector = NULL;
1343 
1344 	free_test_output(o);
1345 }
1346 
run_test(int duration,int flags)1347 static int run_test(int duration, int flags)
1348 {
1349 	struct test_output o;
1350 	int i, n, modes = 0;
1351 
1352 	igt_require((flags & TEST_HANG) == 0 || !is_wedged(drm_fd));
1353 
1354 	resources = drmModeGetResources(drm_fd);
1355 	igt_require(resources);
1356 
1357 	/* Count output configurations to scale test runtime. */
1358 	for (i = 0; i < resources->count_connectors; i++) {
1359 		for (n = 0; n < resources->count_crtcs; n++) {
1360 			memset(&o, 0, sizeof(o));
1361 			o.count = 1;
1362 			o._connector[0] = resources->connectors[i];
1363 			o.flags = flags;
1364 			o.flip_state.name = "flip";
1365 			o.vblank_state.name = "vblank";
1366 			o.bpp = 32;
1367 			o.depth = 24;
1368 
1369 			connector_find_preferred_mode(o._connector[0], n, &o);
1370 			if (o.mode_valid)
1371 				modes++;
1372 
1373 			free_test_output(&o);
1374 		}
1375 	}
1376 
1377 	igt_require(modes);
1378 	duration = duration * 1000 / modes;
1379 	duration = max(500, duration);
1380 
1381 	/* Find any connected displays */
1382 	for (i = 0; i < resources->count_connectors; i++) {
1383 		for (n = 0; n < resources->count_crtcs; n++) {
1384 			int crtc_idx;
1385 
1386 			memset(&o, 0, sizeof(o));
1387 			o.count = 1;
1388 			o._connector[0] = resources->connectors[i];
1389 			o.flags = flags;
1390 			o.flip_state.name = "flip";
1391 			o.vblank_state.name = "vblank";
1392 			o.bpp = 32;
1393 			o.depth = 24;
1394 
1395 			crtc_idx = n;
1396 			run_test_on_crtc_set(&o, &crtc_idx, RUN_TEST, duration);
1397 		}
1398 	}
1399 
1400 	drmModeFreeResources(resources);
1401 	return 1;
1402 }
1403 
run_pair(int duration,int flags)1404 static int run_pair(int duration, int flags)
1405 {
1406 	struct test_output o;
1407 	int i, j, m, n, modes = 0;
1408 
1409 	igt_require((flags & TEST_HANG) == 0 || !is_wedged(drm_fd));
1410 
1411 	resources = drmModeGetResources(drm_fd);
1412 	igt_require(resources);
1413 
1414 	/* Find a pair of connected displays */
1415 	for (i = 0; i < resources->count_connectors; i++) {
1416 		for (n = 0; n < resources->count_crtcs; n++) {
1417 			for (j = i + 1; j < resources->count_connectors; j++) {
1418 				for (m = n + 1; m < resources->count_crtcs; m++) {
1419 					memset(&o, 0, sizeof(o));
1420 					o.count = 2;
1421 					o._connector[0] = resources->connectors[i];
1422 					o._connector[1] = resources->connectors[j];
1423 					o.flags = flags;
1424 					o.flip_state.name = "flip";
1425 					o.vblank_state.name = "vblank";
1426 					o.bpp = 32;
1427 					o.depth = 24;
1428 
1429 					connector_find_compatible_mode(n, m, &o);
1430 					if (o.mode_valid)
1431 						modes++;
1432 
1433 					free_test_output(&o);
1434 				}
1435 			}
1436 		}
1437 	}
1438 
1439 	/* If we have fewer than 2 connected outputs then we won't have any
1440 	 * configuration at all. So skip in that case. */
1441 	igt_require_f(modes, "At least two displays required\n");
1442 	duration = duration * 1000 / modes;
1443 	duration = max(duration, 500);
1444 
1445 	/* Find a pair of connected displays */
1446 	for (i = 0; i < resources->count_connectors; i++) {
1447 		for (n = 0; n < resources->count_crtcs; n++) {
1448 			for (j = i + 1; j < resources->count_connectors; j++) {
1449 				for (m = n + 1; m < resources->count_crtcs; m++) {
1450 					int crtc_idxs[2];
1451 
1452 					memset(&o, 0, sizeof(o));
1453 					o.count = 2;
1454 					o._connector[0] = resources->connectors[i];
1455 					o._connector[1] = resources->connectors[j];
1456 					o.flags = flags;
1457 					o.flip_state.name = "flip";
1458 					o.vblank_state.name = "vblank";
1459 					o.bpp = 32;
1460 					o.depth = 24;
1461 
1462 					crtc_idxs[0] = n;
1463 					crtc_idxs[1] = m;
1464 
1465 					run_test_on_crtc_set(&o, crtc_idxs,
1466 							     RUN_PAIR,
1467 							     duration);
1468 				}
1469 			}
1470 		}
1471 	}
1472 
1473 	drmModeFreeResources(resources);
1474 	return 1;
1475 }
1476 
get_timestamp_format(void)1477 static void get_timestamp_format(void)
1478 {
1479 	uint64_t cap_mono;
1480 	int ret;
1481 
1482 	ret = drmGetCap(drm_fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap_mono);
1483 	igt_assert(ret == 0 || errno == EINVAL);
1484 	monotonic_timestamp = ret == 0 && cap_mono == 1;
1485 	igt_info("Using %s timestamps\n",
1486 		 monotonic_timestamp ? "monotonic" : "real");
1487 }
1488 
kms_flip_exit_handler(int sig)1489 static void kms_flip_exit_handler(int sig)
1490 {
1491 	igt_fixture {
1492 		if (last_connector)
1493 			kmstest_set_connector_dpms(drm_fd, last_connector, DRM_MODE_DPMS_ON);
1494 	}
1495 }
1496 
test_nonblocking_read(int in)1497 static void test_nonblocking_read(int in)
1498 {
1499 	char buffer[1024];
1500 	int fd = dup(in);
1501 	int ret;
1502 
1503 	ret = -1;
1504 	if (fd != -1)
1505 		ret = fcntl(fd, F_GETFL);
1506 	if (ret != -1) {
1507 		ret |= O_NONBLOCK;
1508 		ret = fcntl(fd, F_SETFL, ret);
1509 	}
1510 	igt_require(ret != -1);
1511 
1512 	igt_set_timeout(5, "Nonblocking DRM fd reading");
1513 	ret = read(fd, buffer, sizeof(buffer));
1514 	igt_reset_timeout();
1515 
1516 	igt_assert_eq(ret, -1);
1517 	igt_assert_eq(errno, EAGAIN);
1518 
1519 	close(fd);
1520 }
1521 
1522 igt_main
1523 {
1524 	struct {
1525 		int duration;
1526 		int flags;
1527 		const char *name;
1528 	} tests[] = {
1529 		{ 30, TEST_VBLANK | TEST_CHECK_TS, "wf_vblank-ts-check" },
1530 		{ 30, TEST_VBLANK | TEST_VBLANK_BLOCK | TEST_CHECK_TS,
1531 					"blocking-wf_vblank" },
1532 		{ 30,  TEST_VBLANK | TEST_VBLANK_ABSOLUTE,
1533 					"absolute-wf_vblank" },
1534 		{ 30,  TEST_VBLANK | TEST_VBLANK_BLOCK | TEST_VBLANK_ABSOLUTE,
1535 					"blocking-absolute-wf_vblank" },
1536 		{ 10, TEST_FLIP | TEST_BASIC, "plain-flip" },
1537 		{ 1, TEST_FLIP | TEST_EBUSY, "busy-flip" },
1538 		{ 30, TEST_FLIP | TEST_FENCE_STRESS , "flip-vs-fences" },
1539 		{ 30, TEST_FLIP | TEST_CHECK_TS, "plain-flip-ts-check" },
1540 		{ 30, TEST_FLIP | TEST_CHECK_TS | TEST_FB_RECREATE,
1541 			"plain-flip-fb-recreate" },
1542 		{ 30, TEST_FLIP | TEST_RMFB | TEST_MODESET , "flip-vs-rmfb" },
1543 		{ 20, TEST_FLIP | TEST_DPMS | TEST_EINVAL | TEST_BASIC, "flip-vs-dpms" },
1544 		{ 30,  TEST_FLIP | TEST_PAN, "flip-vs-panning" },
1545 		{ 20, TEST_FLIP | TEST_MODESET | TEST_EINVAL | TEST_BASIC, "flip-vs-modeset" },
1546 		{ 30,  TEST_FLIP | TEST_VBLANK_EXPIRED_SEQ,
1547 					"flip-vs-expired-vblank" },
1548 
1549 		{ 30, TEST_FLIP | TEST_VBLANK | TEST_VBLANK_ABSOLUTE |
1550 		      TEST_CHECK_TS, "flip-vs-absolute-wf_vblank" },
1551 		{ 10, TEST_FLIP | TEST_VBLANK | TEST_CHECK_TS | TEST_BASIC,
1552 					"flip-vs-wf_vblank" },
1553 		{ 30, TEST_FLIP | TEST_VBLANK | TEST_VBLANK_BLOCK |
1554 			TEST_CHECK_TS, "flip-vs-blocking-wf-vblank" },
1555 		{ 30, TEST_FLIP | TEST_MODESET | TEST_HANG | TEST_NOEVENT, "flip-vs-modeset-vs-hang" },
1556 		{ 30, TEST_FLIP | TEST_PAN | TEST_HANG, "flip-vs-panning-vs-hang" },
1557 
1558 		{ 1, TEST_DPMS_OFF | TEST_MODESET | TEST_FLIP,
1559 					"flip-vs-dpms-off-vs-modeset" },
1560 		{ 1, TEST_DPMS_OFF | TEST_MODESET | TEST_FLIP | TEST_SINGLE_BUFFER,
1561 					"single-buffer-flip-vs-dpms-off-vs-modeset" },
1562 		{ 30, TEST_FLIP | TEST_NO_2X_OUTPUT | TEST_DPMS_OFF_OTHERS , "dpms-off-confusion" },
1563 		{ 0, TEST_ENOENT | TEST_NOEVENT, "nonexisting-fb" },
1564 		{ 10, TEST_DPMS_OFF | TEST_DPMS | TEST_VBLANK_RACE, "dpms-vs-vblank-race" },
1565 		{ 10, TEST_MODESET | TEST_VBLANK_RACE, "modeset-vs-vblank-race" },
1566 		{ 0, TEST_BO_TOOBIG | TEST_NO_2X_OUTPUT, "bo-too-big" },
1567 		{ 10, TEST_FLIP | TEST_SUSPEND, "flip-vs-suspend" },
1568 	};
1569 	int i;
1570 
1571 	igt_fixture {
1572 		drm_fd = drm_open_driver_master(DRIVER_ANY);
1573 
1574 		igt_enable_connectors(drm_fd);
1575 
1576 		kmstest_set_vt_graphics_mode();
1577 		igt_install_exit_handler(kms_flip_exit_handler);
1578 		get_timestamp_format();
1579 
1580 #if defined(USE_INTEL)
1581 		if (is_i915_device(drm_fd)) {
1582 			bufmgr = drm_intel_bufmgr_gem_init(drm_fd, 4096);
1583 			if (bufmgr) {
1584 				devid = intel_get_drm_devid(drm_fd);
1585 				batch = intel_batchbuffer_alloc(bufmgr, devid);
1586 			}
1587 		}
1588 #endif
1589 	}
1590 
1591 	igt_subtest("nonblocking-read")
1592 		test_nonblocking_read(drm_fd);
1593 
1594 	for (i = 0; i < sizeof(tests) / sizeof (tests[0]); i++) {
1595 		igt_subtest_f("%s%s",
1596 			      tests[i].flags & TEST_BASIC ? "basic-" : "",
1597 			      tests[i].name)
1598 			run_test(tests[i].duration, tests[i].flags);
1599 
1600 		if (tests[i].flags & TEST_NO_2X_OUTPUT)
1601 			continue;
1602 
1603 		igt_subtest_f( "2x-%s", tests[i].name)
1604 			run_pair(tests[i].duration, tests[i].flags);
1605 	}
1606 
1607 	igt_fork_signal_helper();
1608 	for (i = 0; i < sizeof(tests) / sizeof (tests[0]); i++) {
1609 		/* relative blocking vblank waits that get constantly interrupt
1610 		 * take forver. So don't do them. */
1611 		if ((tests[i].flags & TEST_VBLANK_BLOCK) &&
1612 		    !(tests[i].flags & TEST_VBLANK_ABSOLUTE))
1613 			continue;
1614 
1615 		/*
1616 		 * -EBUSY needs to complete in a single vblank, skip them for
1617 		 * interruptible tests
1618 		 */
1619 		if (tests[i].flags & TEST_EBUSY)
1620 			continue;
1621 
1622 		igt_subtest_f( "%s-interruptible", tests[i].name)
1623 			run_test(tests[i].duration, tests[i].flags);
1624 
1625 		if (tests[i].flags & TEST_NO_2X_OUTPUT)
1626 			continue;
1627 
1628 		igt_subtest_f( "2x-%s-interruptible", tests[i].name)
1629 			run_pair(tests[i].duration, tests[i].flags);
1630 	}
1631 	igt_stop_signal_helper();
1632 }
1633