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