2 * Copyright 2012 Intel Corporation
3 * Jesse Barnes <jesse.barnes@intel.com>
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
35 #include <sys/ioctl.h>
39 #include "testdisplay.h"
40 #include "intel_bufmgr.h"
41 #include "intel_batchbuffer.h"
42 #include "intel_gpu_tools.h"
44 #define TEST_DPMS (1 << 0)
45 #define TEST_WITH_DUMMY_LOAD (1 << 1)
46 #define TEST_PAN (1 << 2)
47 #define TEST_MODESET (1 << 3)
48 #define TEST_CHECK_TS (1 << 4)
50 drmModeRes *resources;
52 static drm_intel_bufmgr *bufmgr;
53 struct intel_batchbuffer *batch;
68 drmModeEncoder *encoder;
69 drmModeConnector *connector;
73 unsigned int current_fb_id;
74 unsigned int fb_ids[2];
75 struct kmstest_fb fb_info[2];
76 struct timeval last_flip_received;
77 struct timeval last_flip_ts;
80 static void emit_dummy_load(struct test_output *o)
83 drm_intel_bo *dummy_bo, *target_bo, *tmp_bo;
84 struct kmstest_fb *fb_info = &o->fb_info[o->current_fb_id];
85 unsigned pitch = fb_info->stride;
87 limit = intel_gen(devid) < 6 ? 500 : 5000;
89 dummy_bo = drm_intel_bo_alloc(bufmgr, "dummy_bo", fb_info->size, 4096);
91 target_bo = gem_handle_to_libdrm_bo(bufmgr, drm_fd, "imported", fb_info->gem_handle);
94 for (i = 0; i < limit; i++) {
96 OUT_BATCH(XY_SRC_COPY_BLT_CMD |
97 XY_SRC_COPY_BLT_WRITE_ALPHA |
98 XY_SRC_COPY_BLT_WRITE_RGB);
99 OUT_BATCH((3 << 24) | /* 32 bits */
100 (0xcc << 16) | /* copy ROP */
102 OUT_BATCH(0 << 16 | 0);
103 OUT_BATCH((o->mode.vdisplay) << 16 | (o->mode.hdisplay));
104 OUT_RELOC_FENCED(dummy_bo, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER, 0);
105 OUT_BATCH(0 << 16 | 0);
107 OUT_RELOC_FENCED(target_bo, I915_GEM_DOMAIN_RENDER, 0, 0);
110 if (IS_GEN6(devid) || IS_GEN7(devid)) {
112 OUT_BATCH(XY_SETUP_CLIP_BLT_CMD);
119 dummy_bo = target_bo;
122 intel_batchbuffer_flush(batch);
124 drm_intel_bo_unreference(dummy_bo);
125 drm_intel_bo_unreference(target_bo);
128 static int set_dpms(struct test_output *o, int mode)
132 for (i = 0; i < o->connector->count_props; i++) {
133 struct drm_mode_get_property prop;
135 prop.prop_id = o->connector->props[i];
136 prop.count_values = 0;
137 prop.count_enum_blobs = 0;
138 if (drmIoctl(drm_fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
141 if (strcmp(prop.name, "DPMS"))
148 fprintf(stderr, "DPMS property not found on %d\n", o->id);
153 return drmModeConnectorSetProperty(drm_fd, o->id, dpms, mode);
157 analog_tv_connector(struct test_output *o)
159 uint32_t connector_type = o->connector->connector_type;
161 return connector_type == DRM_MODE_CONNECTOR_TV ||
162 connector_type == DRM_MODE_CONNECTOR_9PinDIN ||
163 connector_type == DRM_MODE_CONNECTOR_SVIDEO ||
164 connector_type == DRM_MODE_CONNECTOR_Composite;
167 static void page_flip_handler(int fd, unsigned int frame, unsigned int sec,
168 unsigned int usec, void *data)
170 struct test_output *o = data;
171 unsigned int new_fb_id;
172 struct timeval now, diff, pageflip_ts;
173 double usec_interflip;
175 pageflip_ts.tv_sec = sec;
176 pageflip_ts.tv_usec = usec;
178 gettimeofday(&now, NULL);
180 timersub(&pageflip_ts, &now, &diff);
182 if (diff.tv_sec > 0 || (diff.tv_sec > 0 && diff.tv_usec > 2000)) {
183 fprintf(stderr, "pageflip timestamp delayed for too long: %is, %iusec\n",
184 (int) diff.tv_sec, (int) diff.tv_usec);
188 if (!timercmp(&o->last_flip_received, &pageflip_ts, <)) {
189 fprintf(stderr, "pageflip ts before the pageflip was issued!\n");
193 if (o->count > 1 && o->flags & TEST_CHECK_TS && !analog_tv_connector(o)) {
194 timersub(&pageflip_ts, &o->last_flip_ts, &diff);
195 usec_interflip = 1.0 / ((double) o->mode.vrefresh) * 1000.0 * 1000.0;
197 if (fabs((((double) diff.tv_usec) - usec_interflip) / usec_interflip) > 0.005) {
198 fprintf(stderr, "inter-flip timestamp jitter: %is, %ius\n",
199 (int) diff.tv_sec, (int) diff.tv_usec);
200 /* atm this is way too easy to hit, thanks to the hpd
201 * poll helper :( hence make it non-fatal for now */
208 o->current_fb_id = !o->current_fb_id;
209 new_fb_id = o->fb_ids[o->current_fb_id];
211 if (o->flags & TEST_WITH_DUMMY_LOAD)
214 printf("."); fflush(stdout);
215 if (o->flags & TEST_MODESET) {
216 if (drmModeSetCrtc(drm_fd, o->crtc,
217 o->fb_ids[o->current_fb_id],
219 &o->id, 1, &o->mode)) {
220 fprintf(stderr, "failed to restore output mode: %s\n",
226 if (o->flags & TEST_DPMS)
227 do_or_die(set_dpms(o, DRM_MODE_DPMS_ON));
229 do_or_die(drmModePageFlip(drm_fd, o->crtc, new_fb_id,
230 DRM_MODE_PAGE_FLIP_EVENT, o));
232 if (o->flags & TEST_DPMS)
233 do_or_die(set_dpms(o, DRM_MODE_DPMS_OFF));
235 if (o->flags & TEST_MODESET) {
236 if (drmModeSetCrtc(drm_fd, o->crtc,
240 fprintf(stderr, "failed to disable output: %s\n",
246 o->last_flip_received = now;
247 o->last_flip_ts = pageflip_ts;
250 static void connector_find_preferred_mode(struct test_output *o, int crtc_id)
252 drmModeConnector *connector;
253 drmModeEncoder *encoder = NULL;
256 /* First, find the connector & mode */
259 connector = drmModeGetConnector(drm_fd, o->id);
262 if (connector->connection != DRM_MODE_CONNECTED) {
263 drmModeFreeConnector(connector);
267 if (!connector->count_modes) {
268 fprintf(stderr, "connector %d has no modes\n", o->id);
269 drmModeFreeConnector(connector);
273 if (connector->connector_id != o->id) {
274 fprintf(stderr, "connector id doesn't match (%d != %d)\n",
275 connector->connector_id, o->id);
276 drmModeFreeConnector(connector);
280 for (j = 0; j < connector->count_modes; j++) {
281 o->mode = connector->modes[j];
282 if (o->mode.type & DRM_MODE_TYPE_PREFERRED) {
288 if (!o->mode_valid) {
289 if (connector->count_modes > 0) {
290 /* use the first mode as test mode */
291 o->mode = connector->modes[0];
295 fprintf(stderr, "failed to find any modes on connector %d\n",
301 /* Now get the encoder */
302 for (i = 0; i < connector->count_encoders; i++) {
303 encoder = drmModeGetEncoder(drm_fd, connector->encoders[i]);
306 fprintf(stderr, "could not get encoder %i: %s\n",
307 resources->encoders[i], strerror(errno));
308 drmModeFreeEncoder(encoder);
315 o->encoder = encoder;
317 if (i == resources->count_encoders) {
318 fprintf(stderr, "failed to find encoder\n");
323 /* Find first CRTC not in use */
324 for (i = 0; i < resources->count_crtcs; i++) {
325 if (resources->crtcs[i] != crtc_id)
327 if (resources->crtcs[i] &&
328 (o->encoder->possible_crtcs & (1<<i))) {
329 o->crtc = resources->crtcs[i];
335 fprintf(stderr, "could not find requested crtc %d\n", crtc_id);
340 o->connector = connector;
344 paint_flip_mode(cairo_t *cr, int width, int height, void *priv)
346 bool odd_frame = (bool) priv;
349 cairo_rectangle(cr, width/4, height/2, width/4, height/8);
351 cairo_rectangle(cr, width/2, height/2, width/4, height/8);
353 cairo_set_source_rgb(cr, 1, 1, 1);
358 fb_is_bound(struct test_output *o, int fb)
360 struct drm_mode_crtc mode;
362 mode.crtc_id = o->crtc;
363 if (drmIoctl(drm_fd, DRM_IOCTL_MODE_GETCRTC, &mode))
366 return mode.mode_valid && mode.fb_id == fb;
369 static void flip_mode(struct test_output *o, int crtc, int duration)
372 int bpp = 32, depth = 24;
373 drmEventContext evctx;
377 connector_find_preferred_mode(o, crtc);
381 fprintf(stdout, "Beginning page flipping on crtc %d, connector %d\n",
384 width = o->mode.hdisplay;
385 height = o->mode.vdisplay;
387 if (o->flags & TEST_PAN)
390 o->fb_ids[0] = kmstest_create_fb(drm_fd, width, height, bpp, depth,
391 false, &o->fb_info[0],
392 paint_flip_mode, (void *)false);
393 o->fb_ids[1] = kmstest_create_fb(drm_fd, width, height, bpp, depth,
394 false, &o->fb_info[1],
395 paint_flip_mode, (void *)true);
397 if (!o->fb_ids[0] || !o->fb_ids[1]) {
398 fprintf(stderr, "failed to create fbs\n");
402 kmstest_dump_mode(&o->mode);
403 if (drmModeSetCrtc(drm_fd, o->crtc, o->fb_ids[0], 0, 0,
404 &o->id, 1, &o->mode)) {
405 fprintf(stderr, "failed to set mode (%dx%d@%dHz): %s\n",
406 width, height, o->mode.vrefresh,
410 assert(fb_is_bound(o, o->fb_ids[0]));
412 /* quiescent the hw a bit so ensure we don't miss a single frame */
413 if (o->flags & TEST_CHECK_TS)
416 if (drmModePageFlip(drm_fd, o->crtc, o->fb_ids[1],
417 DRM_MODE_PAGE_FLIP_EVENT, o)) {
418 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
421 o->current_fb_id = 1;
422 o->count = 1; /* for the uncounted tail */
424 memset(&evctx, 0, sizeof evctx);
425 evctx.version = DRM_EVENT_CONTEXT_VERSION;
426 evctx.vblank_handler = NULL;
427 evctx.page_flip_handler = page_flip_handler;
429 gettimeofday(&end, NULL);
430 gettimeofday(&o->last_flip_received, NULL);
431 end.tv_sec += duration;
434 struct timeval now, timeout = { .tv_sec = 3, .tv_usec = 0 };
437 /* make timeout lax with the dummy load */
438 if (o->flags & TEST_WITH_DUMMY_LOAD)
439 timeout.tv_sec *= 10;
443 FD_SET(drm_fd, &fds);
444 ret = select(drm_fd + 1, &fds, NULL, NULL, &timeout);
447 fprintf(stderr, "select timed out or error (ret %d)\n",
450 } else if (FD_ISSET(0, &fds)) {
451 fprintf(stderr, "no fds active, breaking\n");
455 gettimeofday(&now, NULL);
456 if (now.tv_sec > end.tv_sec ||
457 (now.tv_sec == end.tv_sec && now.tv_usec >= end.tv_usec)) {
461 /* pan before the flip completes */
462 if (o->flags & TEST_PAN) {
463 int x_ofs = o->count * 10 > o->mode.hdisplay ? o->mode.hdisplay :
466 if (drmModeSetCrtc(drm_fd, o->crtc, o->fb_ids[o->current_fb_id],
468 &o->id, 1, &o->mode)) {
469 fprintf(stderr, "failed to pan (%dx%d@%dHz): %s\n",
470 width, height, o->mode.vrefresh,
476 drmHandleEvent(drm_fd, &evctx);
479 /* and drain the event queue */
480 evctx.page_flip_handler = NULL;
481 drmHandleEvent(drm_fd, &evctx);
483 /* Verify we drop no frames, but only if it's not a TV encoder, since
484 * those use some funny fake timings behind userspace's back. */
485 if (o->flags & TEST_CHECK_TS && !analog_tv_connector(o)) {
490 gettimeofday(&now, NULL);
492 us = duration * 1000 * 1000;
493 us += (now.tv_sec - end.tv_sec) * 1000 * 1000;
494 us += now.tv_usec - end.tv_usec;
496 expected = us * o->mode.vrefresh / (1000 * 1000);
497 if (o->count < expected * 99/100) {
498 fprintf(stderr, "dropped frames, expected %d, counted %d, encoder type %d\n",
499 expected, o->count, o->encoder->encoder_type);
504 fprintf(stdout, "\npage flipping on crtc %d, connector %d: PASSED\n",
507 drmModeFreeEncoder(o->encoder);
508 drmModeFreeConnector(o->connector);
511 static int run_test(int duration, int flags)
513 struct test_output o;
516 resources = drmModeGetResources(drm_fd);
518 fprintf(stderr, "drmModeGetResources failed: %s\n",
523 /* Find any connected displays */
524 for (c = 0; c < resources->count_connectors; c++) {
525 memset(&o, 0, sizeof(o));
526 o.id = resources->connectors[c];
528 for (i = 0; i < resources->count_crtcs; i++)
529 flip_mode(&o, resources->crtcs[i], duration);
532 drmModeFreeResources(resources);
536 int main(int argc, char **argv)
543 { 15, TEST_CHECK_TS , "plain flip" },
544 { 30, TEST_DPMS, "flip vs dpms" },
545 { 30, TEST_DPMS | TEST_WITH_DUMMY_LOAD, "delayed flip vs. dpms" },
546 { 5, TEST_PAN, "flip vs panning" },
547 { 30, TEST_PAN | TEST_WITH_DUMMY_LOAD, "delayed flip vs panning" },
548 { 30, TEST_MODESET, "flip vs modeset" },
549 { 30, TEST_MODESET | TEST_WITH_DUMMY_LOAD, "delayed flip vs modeset" },
553 drm_fd = drm_open_any();
555 bufmgr = drm_intel_bufmgr_gem_init(drm_fd, 4096);
556 devid = intel_get_drm_devid(drm_fd);
557 batch = intel_batchbuffer_alloc(bufmgr, devid);
559 for (i = 0; i < sizeof(tests) / sizeof (tests[0]); i++) {
560 printf("running testcase: %s\n", tests[i].name);
561 run_test(tests[i].duration, tests[i].flags);