1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * 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
18 * THE 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 /** @file kms_vblank.c
25 *
26 * This is a test of performance of drmWaitVblank.
27 */
28
29 #include "igt.h"
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <inttypes.h>
35 #include <errno.h>
36 #include <time.h>
37 #include <sys/poll.h>
38 #include <sys/stat.h>
39 #include <sys/time.h>
40 #include <sys/wait.h>
41
42 #include <drm.h>
43
44 #include "intel_bufmgr.h"
45
46 IGT_TEST_DESCRIPTION("Test speed of WaitVblank.");
47
48 typedef struct {
49 igt_display_t display;
50 struct igt_fb primary_fb;
51 igt_output_t *output;
52 enum pipe pipe;
53 unsigned int flags;
54 #define IDLE 0x1
55 #define BUSY 0x2
56 #define FORKED 0x4
57 #define NOHANG 0x8
58 #define MODESET 0x10
59 #define DPMS 0x20
60 #define SUSPEND 0x40
61 #define RPM 0x80
62 } data_t;
63
elapsed(const struct timespec * start,const struct timespec * end,int loop)64 static double elapsed(const struct timespec *start,
65 const struct timespec *end,
66 int loop)
67 {
68 return (1e6*(end->tv_sec - start->tv_sec) + (end->tv_nsec - start->tv_nsec)/1000)/loop;
69 }
70
prepare_crtc(data_t * data,int fd,igt_output_t * output)71 static void prepare_crtc(data_t *data, int fd, igt_output_t *output)
72 {
73 drmModeModeInfo *mode;
74 igt_display_t *display = &data->display;
75 igt_plane_t *primary;
76
77 igt_display_reset(display);
78
79 /* select the pipe we want to use */
80 igt_output_set_pipe(output, data->pipe);
81
82 /* create and set the primary plane fb */
83 mode = igt_output_get_mode(output);
84 igt_create_color_fb(fd, mode->hdisplay, mode->vdisplay,
85 DRM_FORMAT_XRGB8888,
86 LOCAL_DRM_FORMAT_MOD_NONE,
87 0.0, 0.0, 0.0,
88 &data->primary_fb);
89
90 primary = igt_output_get_plane_type(output, DRM_PLANE_TYPE_PRIMARY);
91 igt_plane_set_fb(primary, &data->primary_fb);
92
93 igt_display_commit(display);
94
95 igt_wait_for_vblank(fd, data->pipe);
96 }
97
cleanup_crtc(data_t * data,int fd,igt_output_t * output)98 static void cleanup_crtc(data_t *data, int fd, igt_output_t *output)
99 {
100 igt_remove_fb(fd, &data->primary_fb);
101 }
102
wait_vblank(int fd,union drm_wait_vblank * vbl)103 static int wait_vblank(int fd, union drm_wait_vblank *vbl)
104 {
105 int err;
106
107 err = 0;
108 if (igt_ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl))
109 err = -errno;
110
111 return err;
112 }
113
run_test(data_t * data,void (* testfunc)(data_t *,int,int))114 static void run_test(data_t *data, void (*testfunc)(data_t *, int, int))
115 {
116 igt_display_t *display = &data->display;
117 igt_output_t *output = data->output;
118 int fd = display->drm_fd;
119 igt_hang_t hang;
120
121 prepare_crtc(data, fd, output);
122
123 if (data->flags & RPM)
124 igt_require(igt_setup_runtime_pm());
125
126 igt_info("Beginning %s on pipe %s, connector %s\n",
127 igt_subtest_name(), kmstest_pipe_name(data->pipe),
128 igt_output_name(output));
129
130 if (!(data->flags & NOHANG))
131 hang = igt_hang_ring(fd, I915_EXEC_DEFAULT);
132
133 if (data->flags & BUSY) {
134 union drm_wait_vblank vbl;
135
136 memset(&vbl, 0, sizeof(vbl));
137 vbl.request.type =
138 DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT;
139 vbl.request.type |= kmstest_get_vbl_flag(data->pipe);
140 vbl.request.sequence = 120 + 12;
141 igt_assert_eq(wait_vblank(fd, &vbl), 0);
142 }
143
144 if (data->flags & FORKED) {
145 int nchildren = sysconf(_SC_NPROCESSORS_ONLN);
146
147 igt_debug("Spawning %d threads\n", nchildren);
148
149 igt_fork(child, nchildren)
150 testfunc(data, fd, nchildren);
151 igt_waitchildren();
152 } else
153 testfunc(data, fd, 1);
154
155 if (data->flags & BUSY) {
156 struct drm_event_vblank buf;
157 igt_assert_eq(read(fd, &buf, sizeof(buf)), sizeof(buf));
158 }
159
160 igt_assert(poll(&(struct pollfd){fd, POLLIN}, 1, 0) == 0);
161
162 if (!(data->flags & NOHANG))
163 igt_post_hang_ring(fd, hang);
164
165 igt_info("\n%s on pipe %s, connector %s: PASSED\n\n",
166 igt_subtest_name(), kmstest_pipe_name(data->pipe), igt_output_name(output));
167
168 /* cleanup what prepare_crtc() has done */
169 cleanup_crtc(data, fd, output);
170 }
171
crtc_id_subtest(data_t * data,int fd)172 static void crtc_id_subtest(data_t *data, int fd)
173 {
174 igt_display_t *display = &data->display;
175 igt_output_t *output;
176 enum pipe p;
177
178 for_each_pipe_with_valid_output(display, p, output) {
179 struct drm_event_vblank buf;
180 const uint32_t pipe_id_flag = kmstest_get_vbl_flag(p);
181 unsigned crtc_id, expected_crtc_id;
182 uint64_t val;
183 union drm_wait_vblank vbl;
184
185 crtc_id = display->pipes[p].crtc_id;
186 if (drmGetCap(display->drm_fd, DRM_CAP_CRTC_IN_VBLANK_EVENT, &val) == 0)
187 expected_crtc_id = crtc_id;
188 else
189 expected_crtc_id = 0;
190
191 data->pipe = p;
192 prepare_crtc(data, fd, output);
193
194 memset(&vbl, 0, sizeof(vbl));
195 vbl.request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT;
196 vbl.request.type |= pipe_id_flag;
197 vbl.request.sequence = 1;
198 igt_assert_eq(wait_vblank(fd, &vbl), 0);
199
200 igt_assert_eq(read(fd, &buf, sizeof(buf)), sizeof(buf));
201 igt_assert_eq(buf.crtc_id, expected_crtc_id);
202
203 do_or_die(drmModePageFlip(fd, crtc_id,
204 data->primary_fb.fb_id,
205 DRM_MODE_PAGE_FLIP_EVENT, NULL));
206
207 igt_assert_eq(read(fd, &buf, sizeof(buf)), sizeof(buf));
208 igt_assert_eq(buf.crtc_id, expected_crtc_id);
209
210 if (display->is_atomic) {
211 igt_plane_t *primary = igt_output_get_plane(output, 0);
212
213 igt_plane_set_fb(primary, &data->primary_fb);
214 igt_display_commit_atomic(display, DRM_MODE_PAGE_FLIP_EVENT, NULL);
215
216 igt_assert_eq(read(fd, &buf, sizeof(buf)), sizeof(buf));
217 igt_assert_eq(buf.crtc_id, expected_crtc_id);
218 }
219
220 cleanup_crtc(data, fd, output);
221 return;
222 }
223 }
224
accuracy(data_t * data,int fd,int nchildren)225 static void accuracy(data_t *data, int fd, int nchildren)
226 {
227 const uint32_t pipe_id_flag = kmstest_get_vbl_flag(data->pipe);
228 union drm_wait_vblank vbl;
229 unsigned long target;
230 int total = 120 / nchildren;
231 int n;
232
233 memset(&vbl, 0, sizeof(vbl));
234 vbl.request.type = _DRM_VBLANK_RELATIVE;
235 vbl.request.type |= pipe_id_flag;
236 vbl.request.sequence = 1;
237 igt_assert_eq(wait_vblank(fd, &vbl), 0);
238
239 target = vbl.reply.sequence + total;
240 for (n = 0; n < total; n++) {
241 vbl.request.type = _DRM_VBLANK_RELATIVE;
242 vbl.request.type |= pipe_id_flag;
243 vbl.request.sequence = 1;
244 igt_assert_eq(wait_vblank(fd, &vbl), 0);
245
246 vbl.request.type = _DRM_VBLANK_ABSOLUTE | _DRM_VBLANK_EVENT;
247 vbl.request.type |= pipe_id_flag;
248 vbl.request.sequence = target;
249 igt_assert_eq(wait_vblank(fd, &vbl), 0);
250 }
251 vbl.request.type = _DRM_VBLANK_RELATIVE;
252 vbl.request.type |= pipe_id_flag;
253 vbl.request.sequence = 0;
254 igt_assert_eq(wait_vblank(fd, &vbl), 0);
255 igt_assert_eq(vbl.reply.sequence, target);
256
257 for (n = 0; n < total; n++) {
258 struct drm_event_vblank ev;
259 igt_assert_eq(read(fd, &ev, sizeof(ev)), sizeof(ev));
260 igt_assert_eq(ev.sequence, target);
261 }
262 }
263
vblank_query(data_t * data,int fd,int nchildren)264 static void vblank_query(data_t *data, int fd, int nchildren)
265 {
266 const uint32_t pipe_id_flag = kmstest_get_vbl_flag(data->pipe);
267 union drm_wait_vblank vbl;
268 struct timespec start, end;
269 unsigned long sq, count = 0;
270
271 memset(&vbl, 0, sizeof(vbl));
272 vbl.request.type = _DRM_VBLANK_RELATIVE;
273 vbl.request.type |= pipe_id_flag;
274 vbl.request.sequence = 0;
275 igt_assert_eq(wait_vblank(fd, &vbl), 0);
276
277 sq = vbl.reply.sequence;
278
279 clock_gettime(CLOCK_MONOTONIC, &start);
280 do {
281 vbl.request.type = _DRM_VBLANK_RELATIVE;
282 vbl.request.type |= pipe_id_flag;
283 vbl.request.sequence = 0;
284 igt_assert_eq(wait_vblank(fd, &vbl), 0);
285 count++;
286
287 /*
288 * break the loop and fail after 10 seconds to prevent hang.
289 * Ideally, it should take only 2 seconds for 120 vblank in 60 fps.
290 */
291 clock_gettime(CLOCK_MONOTONIC, &end);
292 igt_assert_f(end.tv_sec - start.tv_sec < 10,
293 "VBlank Sequence number increased by only %lu in %lu seconds.\n",
294 vbl.reply.sequence - sq, end.tv_sec - start.tv_sec);
295 } while (vbl.reply.sequence <= (sq + 120));
296 clock_gettime(CLOCK_MONOTONIC, &end);
297
298 igt_info("Time to query current counter (%s): %7.3fµs\n",
299 data->flags & BUSY ? "busy" : "idle", elapsed(&start, &end, count));
300 }
301
vblank_wait(data_t * data,int fd,int nchildren)302 static void vblank_wait(data_t *data, int fd, int nchildren)
303 {
304 const uint32_t pipe_id_flag = kmstest_get_vbl_flag(data->pipe);
305 union drm_wait_vblank vbl;
306 struct timespec start, end;
307 unsigned long sq, count = 0;
308
309 memset(&vbl, 0, sizeof(vbl));
310 vbl.request.type = _DRM_VBLANK_RELATIVE;
311 vbl.request.type |= pipe_id_flag;
312 vbl.request.sequence = 0;
313 igt_assert_eq(wait_vblank(fd, &vbl), 0);
314
315 sq = vbl.reply.sequence;
316
317 clock_gettime(CLOCK_MONOTONIC, &start);
318 do {
319 vbl.request.type = _DRM_VBLANK_RELATIVE;
320 vbl.request.type |= pipe_id_flag;
321 vbl.request.sequence = 1;
322 igt_assert_eq(wait_vblank(fd, &vbl), 0);
323 count++;
324 } while ((vbl.reply.sequence - sq) <= 120);
325 clock_gettime(CLOCK_MONOTONIC, &end);
326
327 igt_info("Time to wait for %ld/%d vblanks (%s): %7.3fµs\n",
328 count, (int)(vbl.reply.sequence - sq),
329 data->flags & BUSY ? "busy" : "idle",
330 elapsed(&start, &end, count));
331 }
332
get_vblank(int fd,enum pipe pipe,unsigned flags)333 static int get_vblank(int fd, enum pipe pipe, unsigned flags)
334 {
335 union drm_wait_vblank vbl;
336
337 memset(&vbl, 0, sizeof(vbl));
338 vbl.request.type = DRM_VBLANK_RELATIVE | kmstest_get_vbl_flag(pipe) | flags;
339 do_or_die(igt_ioctl(fd, DRM_IOCTL_WAIT_VBLANK, &vbl));
340
341 return vbl.reply.sequence;
342 }
343
344 #define VBLANK_ERR 5
345
vblank_ts_cont(data_t * data,int fd,int nchildren)346 static void vblank_ts_cont(data_t *data, int fd, int nchildren)
347 {
348 igt_display_t *display = &data->display;
349 igt_output_t *output = data->output;
350 int seq1, seq2;
351 union drm_wait_vblank vbl;
352 struct timespec start, end;
353 int estimated_vblanks = 0;
354 int vrefresh = igt_output_get_mode(output)->vrefresh;
355 double time_elapsed;
356
357 seq1 = get_vblank(fd, data->pipe, 0);
358 clock_gettime(CLOCK_MONOTONIC, &start);
359
360 if (data->flags & DPMS) {
361 igt_output_set_prop_value(output, IGT_CONNECTOR_DPMS, DRM_MODE_DPMS_OFF);
362 igt_display_commit(display);
363 }
364
365 if (data->flags & MODESET) {
366 igt_output_set_pipe(output, PIPE_NONE);
367 igt_display_commit2(display, display->is_atomic ? COMMIT_ATOMIC : COMMIT_LEGACY);
368 }
369
370 if (data->flags & RPM)
371 igt_require(igt_wait_for_pm_status(IGT_RUNTIME_PM_STATUS_SUSPENDED));
372
373 if (data->flags & SUSPEND)
374 igt_system_suspend_autoresume(SUSPEND_STATE_MEM,
375 SUSPEND_TEST_NONE);
376
377 if (data->flags & (MODESET | DPMS)) {
378 /* Attempting to do a vblank while disabled should return -EINVAL */
379 memset(&vbl, 0, sizeof(vbl));
380 vbl.request.type = _DRM_VBLANK_RELATIVE;
381 vbl.request.type |= kmstest_get_vbl_flag(data->pipe);
382 igt_assert_eq(wait_vblank(fd, &vbl), -EINVAL);
383 }
384
385 if (data->flags & DPMS) {
386 igt_output_set_prop_value(output, IGT_CONNECTOR_DPMS, DRM_MODE_DPMS_ON);
387 igt_display_commit(display);
388 }
389
390 if (data->flags & MODESET) {
391 igt_output_set_pipe(output, data->pipe);
392 igt_display_commit2(display, display->is_atomic ? COMMIT_ATOMIC : COMMIT_LEGACY);
393 }
394
395 seq2 = get_vblank(fd, data->pipe, 0);
396 clock_gettime(CLOCK_MONOTONIC, &end);
397
398 time_elapsed = igt_time_elapsed(&start, &end);
399 estimated_vblanks = (int)(time_elapsed * vrefresh);
400
401 igt_debug("testing ts continuity: Current frame %u, old frame %u\n", seq2, seq1);
402
403 igt_assert_f(seq2 - seq1 >= 0, "elapsed %f(%d vblanks) unexpected vblank seq %u, should be > %u\n", time_elapsed,
404 estimated_vblanks, seq2, seq1);
405 igt_assert_f(seq2 - seq1 <= estimated_vblanks + VBLANK_ERR, "elapsed %f(%d vblanks) unexpected vblank seq %u, should be <= %u\n", time_elapsed,
406 estimated_vblanks, seq2, seq1 + estimated_vblanks);
407 }
408
run_subtests_for_pipe(data_t * data)409 static void run_subtests_for_pipe(data_t *data)
410 {
411 const struct {
412 const char *name;
413 void (*func)(data_t *, int, int);
414 unsigned int valid;
415 } funcs[] = {
416 /*
417 * GPU reset recovery may disable irqs or reset display, so
418 * accuracy tests will fail in the hang case, disable this test.
419 */
420 { "accuracy", accuracy, IDLE | NOHANG },
421 { "query", vblank_query, IDLE | FORKED | BUSY },
422 { "wait", vblank_wait, IDLE | FORKED | BUSY },
423 { "ts-continuation", vblank_ts_cont, IDLE | SUSPEND | MODESET | DPMS | RPM },
424 { }
425 }, *f;
426
427 const struct {
428 const char *name;
429 unsigned int flags;
430 } modes[] = {
431 { "idle", IDLE },
432 { "forked", IDLE | FORKED },
433 { "busy", BUSY },
434 { "forked-busy", BUSY | FORKED },
435 { "dpms-rpm", DPMS | RPM | NOHANG },
436 { "dpms-suspend", DPMS | SUSPEND | NOHANG},
437 { "suspend", SUSPEND | NOHANG },
438 { "modeset", MODESET },
439 { "modeset-rpm", MODESET | RPM | NOHANG},
440 { }
441 }, *m;
442
443 igt_fixture
444 igt_display_require_output_on_pipe(&data->display, data->pipe);
445
446 for (f = funcs; f->name; f++) {
447 for (m = modes; m->name; m++) {
448 if (m->flags & ~(f->valid | NOHANG))
449 continue;
450
451 igt_subtest_f("pipe-%s-%s-%s",
452 kmstest_pipe_name(data->pipe),
453 f->name, m->name) {
454 for_each_valid_output_on_pipe(&data->display, data->pipe, data->output) {
455 data->flags = m->flags | NOHANG;
456 run_test(data, f->func);
457 }
458 }
459
460 /* Skip the -hang version if NOHANG flag is set */
461 if (f->valid & NOHANG || m->flags & NOHANG)
462 continue;
463
464 igt_subtest_f("pipe-%s-%s-%s-hang",
465 kmstest_pipe_name(data->pipe),
466 f->name, m->name) {
467 igt_hang_t hang;
468
469 hang = igt_allow_hang(data->display.drm_fd, 0, 0);
470 for_each_valid_output_on_pipe(&data->display, data->pipe, data->output) {
471 data->flags = m->flags;
472 run_test(data, f->func);
473 }
474 igt_disallow_hang(data->display.drm_fd, hang);
475 }
476 }
477 }
478 }
479
invalid_subtest(data_t * data,int fd)480 static void invalid_subtest(data_t *data, int fd)
481 {
482 union drm_wait_vblank vbl;
483 unsigned long valid_flags;
484
485 igt_display_require_output_on_pipe(&data->display, 0);
486
487 /* First check all is well with a simple query */
488 memset(&vbl, 0, sizeof(vbl));
489 vbl.request.type = _DRM_VBLANK_RELATIVE;
490 igt_assert_eq(wait_vblank(fd, &vbl), 0);
491
492 valid_flags = (_DRM_VBLANK_TYPES_MASK |
493 _DRM_VBLANK_FLAGS_MASK |
494 _DRM_VBLANK_HIGH_CRTC_MASK);
495
496 /* pick some interesting invalid permutations */
497 memset(&vbl, 0, sizeof(vbl));
498 vbl.request.type = _DRM_VBLANK_RELATIVE | ~valid_flags;
499 igt_assert_eq(wait_vblank(fd, &vbl), -EINVAL);
500 for (int bit = 0; bit < 32; bit++) {
501 int err;
502
503 if (valid_flags & (1 << bit))
504 continue;
505
506 memset(&vbl, 0, sizeof(vbl));
507 vbl.request.type = _DRM_VBLANK_RELATIVE | (1 << bit);
508 err = wait_vblank(fd, &vbl);
509 igt_assert_f(err == -EINVAL,
510 "vblank wait with invalid request.type bit %d [0x%08x] did not report -EINVAL, got %d\n",
511 bit, 1 << bit, err);
512 }
513
514 /* check the maximum pipe, nobody should have that many pipes! */
515 memset(&vbl, 0, sizeof(vbl));
516 vbl.request.type = _DRM_VBLANK_RELATIVE;
517 vbl.request.type |= _DRM_VBLANK_SECONDARY;
518 vbl.request.type |= _DRM_VBLANK_FLAGS_MASK;
519 igt_assert_eq(wait_vblank(fd, &vbl), -EINVAL);
520 }
521
522 igt_main
523 {
524 int fd;
525 data_t data;
526
527 igt_skip_on_simulation();
528
529 igt_fixture {
530 fd = drm_open_driver_master(DRIVER_ANY);
531 kmstest_set_vt_graphics_mode();
532 igt_display_require(&data.display, fd);
533 igt_display_require_output(&data.display);
534 }
535
536 igt_subtest("invalid")
537 invalid_subtest(&data, fd);
538
539 igt_subtest("crtc-id")
540 crtc_id_subtest(&data, fd);
541
542 for_each_pipe_static(data.pipe)
543 igt_subtest_group
544 run_subtests_for_pipe(&data);
545 }
546