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