kms_fbc_crc: Add a CRC based FBC test
[platform/upstream/intel-gpu-tools.git] / tests / kms_fbc_crc.c
1 /*
2  * Copyright © 2013 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
25 #include <errno.h>
26 #include <stdbool.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include <glib.h>
31
32 #include "drm_fourcc.h"
33
34 #include "drmtest.h"
35 #include "igt_debugfs.h"
36 #include "igt_kms.h"
37 #include "rendercopy.h"
38
39 enum test_mode {
40         TEST_PAGE_FLIP,
41         TEST_MMAP_CPU,
42         TEST_MMAP_GTT,
43         TEST_BLT,
44         TEST_RENDER,
45         TEST_CONTEXT,
46         TEST_PAGE_FLIP_AND_MMAP_CPU,
47         TEST_PAGE_FLIP_AND_MMAP_GTT,
48         TEST_PAGE_FLIP_AND_BLT,
49         TEST_PAGE_FLIP_AND_RENDER,
50         TEST_PAGE_FLIP_AND_CONTEXT,
51 };
52
53 typedef struct {
54         struct kmstest_connector_config config;
55         drmModeModeInfo mode;
56         struct kmstest_fb fb[2];
57 } connector_t;
58
59 typedef struct {
60         int drm_fd;
61         igt_debugfs_t debugfs;
62         drmModeRes *resources;
63         FILE *ctl;
64         igt_crc_t ref_crc[2];
65         igt_pipe_crc_t **pipe_crc;
66         drm_intel_bufmgr *bufmgr;
67         drm_intel_context *ctx[2];
68         uint32_t devid;
69         uint32_t handle[2];
70         uint32_t crtc_id;
71         uint32_t crtc_idx;
72         uint32_t fb_id[2];
73 } data_t;
74
75 static const char *test_mode_str(enum test_mode mode)
76 {
77         static const char * const test_modes[] = {
78                 [TEST_PAGE_FLIP] = "page_flip",
79                 [TEST_MMAP_CPU] = "mmap_cpu",
80                 [TEST_MMAP_GTT] = "mmap_gtt",
81                 [TEST_BLT] = "blt",
82                 [TEST_RENDER] = "render",
83                 [TEST_CONTEXT] = "context",
84                 [TEST_PAGE_FLIP_AND_MMAP_CPU] = "page_flip_and_mmap_cpu",
85                 [TEST_PAGE_FLIP_AND_MMAP_GTT] = "page_flip_and_mmap_gtt",
86                 [TEST_PAGE_FLIP_AND_BLT] = "page_flip_and_blt",
87                 [TEST_PAGE_FLIP_AND_RENDER] = "page_flip_and_render",
88                 [TEST_PAGE_FLIP_AND_CONTEXT] = "page_flip_and_context",
89         };
90
91         return test_modes[mode];
92 }
93
94 static uint32_t create_fb(data_t *data,
95                           int w, int h,
96                           double r, double g, double b,
97                           struct kmstest_fb *fb)
98 {
99         uint32_t fb_id;
100         cairo_t *cr;
101
102         fb_id = kmstest_create_fb2(data->drm_fd, w, h,
103                                    DRM_FORMAT_XRGB8888, true, fb);
104         igt_assert(fb_id);
105
106         cr = kmstest_get_cairo_ctx(data->drm_fd, fb);
107         kmstest_paint_color(cr, 0, 0, w, h, r, g, b);
108         igt_assert(cairo_status(cr) == 0);
109
110         return fb_id;
111 }
112
113 static bool
114 connector_set_mode(data_t *data, connector_t *connector,
115                    drmModeModeInfo *mode, uint32_t fb_id)
116 {
117         struct kmstest_connector_config *config = &connector->config;
118         int ret;
119
120 #if 0
121         fprintf(stdout, "Using pipe %c, %dx%d\n", pipe_name(config->pipe),
122                 mode->hdisplay, mode->vdisplay);
123 #endif
124
125         ret = drmModeSetCrtc(data->drm_fd,
126                              config->crtc->crtc_id,
127                              fb_id,
128                              0, 0, /* x, y */
129                              &config->connector->connector_id,
130                              1,
131                              mode);
132         igt_assert(ret == 0);
133
134         return 0;
135 }
136
137 static void display_init(data_t *data)
138 {
139         data->resources = drmModeGetResources(data->drm_fd);
140         igt_assert(data->resources);
141
142         data->pipe_crc = calloc(data->resources->count_crtcs, sizeof(data->pipe_crc[0]));
143 }
144
145 static void display_fini(data_t *data)
146 {
147         free(data->pipe_crc);
148 }
149
150 static void fill_blt(data_t *data, uint32_t handle, unsigned char color)
151 {
152         drm_intel_bo *dst = gem_handle_to_libdrm_bo(data->bufmgr,
153                                                     data->drm_fd,
154                                                     "", handle);
155         struct intel_batchbuffer *batch;
156
157         batch = intel_batchbuffer_alloc(data->bufmgr, data->devid);
158         igt_assert(batch);
159
160         BEGIN_BATCH(5);
161         OUT_BATCH(COLOR_BLT_CMD);
162         OUT_BATCH((1 << 24) | (0xf0 << 16) | 0);
163         OUT_BATCH(1 << 16 | 4);
164         OUT_RELOC(dst, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER, 0);
165         OUT_BATCH(color);
166         ADVANCE_BATCH();
167
168         intel_batchbuffer_flush(batch);
169         intel_batchbuffer_free(batch);
170
171         gem_bo_busy(data->drm_fd, handle);
172 }
173
174 static void scratch_buf_init(struct scratch_buf *buf, drm_intel_bo *bo)
175 {
176         buf->bo = bo;
177         buf->stride = 4096;
178         buf->tiling = I915_TILING_X;
179         buf->size = 4096;
180 }
181
182 static void exec_nop(data_t *data, uint32_t handle, drm_intel_context *context)
183 {
184         drm_intel_bo *dst;
185         struct intel_batchbuffer *batch;
186
187         dst = gem_handle_to_libdrm_bo(data->bufmgr, data->drm_fd, "", handle);
188         igt_assert(dst);
189
190         batch = intel_batchbuffer_alloc(data->bufmgr, data->devid);
191         igt_assert(batch);
192
193         /* add the reloc to make sure the kernel will think we write to dst */
194         BEGIN_BATCH(4);
195         OUT_BATCH(MI_BATCH_BUFFER_END);
196         OUT_BATCH(MI_NOOP);
197         OUT_RELOC(dst, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER, 0);
198         OUT_BATCH(MI_NOOP);
199         ADVANCE_BATCH();
200
201         intel_batchbuffer_flush_with_context(batch, context);
202         intel_batchbuffer_free(batch);
203 }
204
205 static void fill_render(data_t *data, uint32_t handle,
206                         drm_intel_context *context, unsigned char color)
207 {
208         drm_intel_bo *src, *dst;
209         struct intel_batchbuffer *batch;
210         struct scratch_buf src_buf, dst_buf;
211         const uint8_t buf[4] = { color, color, color, color };
212         render_copyfunc_t rendercopy = get_render_copyfunc(data->devid);
213
214         igt_skip_on(!rendercopy);
215
216         dst = gem_handle_to_libdrm_bo(data->bufmgr, data->drm_fd, "", handle);
217         igt_assert(dst);
218
219         src = drm_intel_bo_alloc(data->bufmgr, "", 4096, 4096);
220         igt_assert(src);
221
222         gem_write(data->drm_fd, src->handle, 0, buf, 4);
223
224         scratch_buf_init(&src_buf, src);
225         scratch_buf_init(&dst_buf, dst);
226
227         batch = intel_batchbuffer_alloc(data->bufmgr, data->devid);
228         igt_assert(batch);
229
230         rendercopy(batch, context,
231                    &src_buf, 0, 0, 1, 1,
232                    &dst_buf, 0, 0);
233
234         intel_batchbuffer_free(batch);
235
236         gem_bo_busy(data->drm_fd, handle);
237 }
238
239 static bool fbc_enabled(data_t *data)
240 {
241         FILE *status;
242         char str[64] = {};
243
244         status = igt_debugfs_fopen(&data->debugfs, "i915_fbc_status", "r");
245         fread(str, sizeof(str) - 1, 1, status);
246         fclose(status);
247         return strstr(str, "FBC enabled") != NULL;
248 }
249
250 static void test_crc(data_t *data, enum test_mode mode)
251 {
252         static const unsigned char buf[1] = { 0xcc };
253         igt_pipe_crc_t *pipe_crc = data->pipe_crc[data->crtc_idx];
254         igt_crc_t *crcs = NULL;
255         uint32_t handle = data->handle[0];
256
257         igt_assert(fbc_enabled(data));
258
259         if (mode >= TEST_PAGE_FLIP_AND_MMAP_CPU) {
260                 handle = data->handle[1];
261                 igt_assert(drmModePageFlip(data->drm_fd, data->crtc_id,
262                                            data->fb_id[1], 0, NULL) == 0);
263                 usleep(300000);
264
265                 igt_assert(fbc_enabled(data));
266         }
267
268         switch (mode) {
269                 void *ptr;
270         case TEST_PAGE_FLIP:
271                 igt_assert(drmModePageFlip(data->drm_fd, data->crtc_id,
272                                            data->fb_id[1], 0, NULL) == 0);
273                 break;
274         case TEST_MMAP_CPU:
275         case TEST_PAGE_FLIP_AND_MMAP_CPU:
276                 ptr = gem_mmap__cpu(data->drm_fd, handle, 4096, PROT_WRITE);
277                 gem_set_domain(data->drm_fd, handle, I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
278                 memset(ptr, 0xff, 4);
279                 munmap(ptr, 4096);
280                 gem_sw_finish(data->drm_fd, handle);
281                 break;
282         case TEST_MMAP_GTT:
283         case TEST_PAGE_FLIP_AND_MMAP_GTT:
284                 ptr = gem_mmap__gtt(data->drm_fd, handle, 4096, PROT_WRITE);
285                 gem_set_domain(data->drm_fd, handle, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
286                 memset(ptr, 0xff, 4);
287                 munmap(ptr, 4096);
288                 break;
289         case TEST_BLT:
290         case TEST_PAGE_FLIP_AND_BLT:
291                 fill_blt(data, handle, 0xff);
292                 break;
293         case TEST_RENDER:
294         case TEST_CONTEXT:
295         case TEST_PAGE_FLIP_AND_RENDER:
296         case TEST_PAGE_FLIP_AND_CONTEXT:
297                 fill_render(data, handle,
298                             (mode == TEST_CONTEXT || mode == TEST_PAGE_FLIP_AND_CONTEXT) ?
299                             data->ctx[1] : NULL, 0xff);
300                 break;
301         }
302
303         /*
304          * Make sure we're looking at new data (two vblanks
305          * to leave some leeway for the kernel if we ever do
306          * some kind of delayed FBC disable for GTT mmaps.
307          */
308         igt_wait_for_vblank(data->drm_fd, data->crtc_idx);
309         igt_wait_for_vblank(data->drm_fd, data->crtc_idx);
310
311         igt_pipe_crc_start(pipe_crc);
312         igt_pipe_crc_get_crcs(pipe_crc, 1, &crcs);
313         igt_pipe_crc_stop(pipe_crc);
314         igt_assert(!igt_crc_equal(&crcs[0], &data->ref_crc[0]));
315         if (mode == TEST_PAGE_FLIP)
316                 igt_assert(igt_crc_equal(&crcs[0], &data->ref_crc[1]));
317         else
318                 igt_assert(!igt_crc_equal(&crcs[0], &data->ref_crc[1]));
319         free(crcs);
320
321         /*
322          * Allow time for FBC to kick in again if it
323          * got disabled during dirtyfb or page flip.
324          */
325         usleep(300000);
326
327         igt_assert(fbc_enabled(data));
328
329         igt_pipe_crc_start(pipe_crc);
330         igt_pipe_crc_get_crcs(pipe_crc, 1, &crcs);
331         igt_pipe_crc_stop(pipe_crc);
332         igt_assert(!igt_crc_equal(&crcs[0], &data->ref_crc[0]));
333         if (mode == TEST_PAGE_FLIP)
334                 igt_assert(igt_crc_equal(&crcs[0], &data->ref_crc[1]));
335         else
336                 igt_assert(!igt_crc_equal(&crcs[0], &data->ref_crc[1]));
337         free(crcs);
338 }
339
340 static bool prepare_crtc(data_t *data, uint32_t connector_id, enum test_mode mode)
341 {
342         igt_pipe_crc_t *pipe_crc;
343         igt_crc_t *crcs = NULL;
344         connector_t connector;
345         int ret;
346
347         ret = kmstest_get_connector_config(data->drm_fd,
348                                            connector_id,
349                                            1 << data->crtc_idx,
350                                            &connector.config);
351         if (ret)
352                 return false;
353
354         igt_pipe_crc_free(data->pipe_crc[data->crtc_idx]);
355         data->pipe_crc[data->crtc_idx] = NULL;
356
357         pipe_crc = igt_pipe_crc_new(&data->debugfs,
358                                     data->drm_fd, data->crtc_idx,
359                                     INTEL_PIPE_CRC_SOURCE_AUTO);
360         if (!pipe_crc) {
361                 printf("auto crc not supported on this connector with crtc %i\n",
362                        data->crtc_idx);
363                 return false;
364         }
365
366         data->pipe_crc[data->crtc_idx] = pipe_crc;
367
368         data->fb_id[0] = create_fb(data,
369                                    connector.config.default_mode.hdisplay,
370                                    connector.config.default_mode.vdisplay,
371                                    0.0, 0.0, 0.0, &connector.fb[0]);
372         igt_assert(data->fb_id[0]);
373
374         data->fb_id[1] = create_fb(data,
375                                    connector.config.default_mode.hdisplay,
376                                    connector.config.default_mode.vdisplay,
377                                    0.1, 0.1, 0.1, &connector.fb[1]);
378         igt_assert(data->fb_id[1]);
379
380         data->handle[0] = connector.fb[0].gem_handle;
381         data->handle[1] = connector.fb[1].gem_handle;
382
383         /* scanout = fb[1] */
384         connector_set_mode(data, &connector, &connector.config.default_mode,
385                            data->fb_id[1]);
386         usleep(300000);
387
388         igt_skip_on(!fbc_enabled(data));
389
390         igt_wait_for_vblank(data->drm_fd, data->crtc_idx);
391
392         /* get reference crc for fb[1] */
393         igt_pipe_crc_start(pipe_crc);
394         igt_pipe_crc_get_crcs(pipe_crc, 1, &crcs);
395         data->ref_crc[1] = crcs[0];
396         igt_pipe_crc_stop(pipe_crc);
397         free(crcs);
398
399         if (mode == TEST_CONTEXT || mode == TEST_PAGE_FLIP_AND_CONTEXT) {
400                 data->ctx[0] = drm_intel_gem_context_create(data->bufmgr);
401                 igt_require(data->ctx[0]);
402                 data->ctx[1] = drm_intel_gem_context_create(data->bufmgr);
403                 igt_require(data->ctx[1]);
404
405                 /*
406                  * Disable FBC RT address for both contexts
407                  * (by "rendering" to a non-scanout buffer).
408                  */
409                 exec_nop(data, data->handle[0], data->ctx[1]);
410                 exec_nop(data, data->handle[0], data->ctx[0]);
411                 exec_nop(data, data->handle[0], data->ctx[1]);
412                 exec_nop(data, data->handle[0], data->ctx[0]);
413         }
414
415         /* scanout = fb[0] */
416         connector_set_mode(data, &connector, &connector.config.default_mode,
417                            data->fb_id[0]);
418         usleep(300000);
419
420         igt_skip_on(!fbc_enabled(data));
421
422         if (mode == TEST_CONTEXT || mode == TEST_PAGE_FLIP_AND_CONTEXT) {
423                 /*
424                  * make ctx[0] FBC RT address point to fb[0], ctx[1]
425                  * FBC RT address is left as disabled.
426                  */
427                 exec_nop(data, connector.fb[0].gem_handle, data->ctx[0]);
428         }
429
430         igt_wait_for_vblank(data->drm_fd, data->crtc_idx);
431
432         /* get reference crc for fb[0] */
433         igt_pipe_crc_start(pipe_crc);
434         igt_pipe_crc_get_crcs(pipe_crc, 1, &crcs);
435         data->ref_crc[0] = crcs[0];
436         igt_pipe_crc_stop(pipe_crc);
437         free(crcs);
438
439         kmstest_free_connector_config(&connector.config);
440
441         return true;
442 }
443
444 static void finish_crtc(data_t *data, enum test_mode mode)
445 {
446         igt_pipe_crc_free(data->pipe_crc[data->crtc_idx]);
447         data->pipe_crc[data->crtc_idx] = NULL;
448
449         if (mode == TEST_CONTEXT || mode == TEST_PAGE_FLIP_AND_CONTEXT) {
450                 drm_intel_gem_context_destroy(data->ctx[0]);
451                 drm_intel_gem_context_destroy(data->ctx[1]);
452         }
453 }
454
455 static void run_test(data_t *data, enum test_mode mode)
456 {
457         int i, n;
458
459         for (i = 0; i < data->resources->count_connectors; i++) {
460                 uint32_t connector_id = data->resources->connectors[i];
461
462                 for (n = 0; n < data->resources->count_crtcs; n++) {
463                         data->crtc_idx = n;
464                         data->crtc_id = data->resources->crtcs[n];
465
466                         if (!prepare_crtc(data, connector_id, mode))
467                                 continue;
468
469                         fprintf(stdout, "Beginning %s on crtc %d, connector %d\n",
470                                 igt_subtest_name(), data->crtc_id, connector_id);
471                         test_crc(data, mode);
472
473                         fprintf(stdout, "\n%s on crtc %d, connector %d: PASSED\n\n",
474                                 igt_subtest_name(), data->crtc_id, connector_id);
475
476                         finish_crtc(data, mode);
477                 }
478         }
479 }
480
481 igt_main
482 {
483         data_t data = {};
484         enum test_mode mode;
485
486         igt_skip_on_simulation();
487
488         igt_fixture {
489                 size_t written;
490                 int ret;
491                 const char *cmd = "pipe A none";
492                 char buf[64];
493                 FILE *status;
494
495                 data.drm_fd = drm_open_any();
496                 igt_require(data.drm_fd);
497                 igt_set_vt_graphics_mode();
498
499                 data.devid = intel_get_drm_devid(data.drm_fd);
500
501                 igt_debugfs_init(&data.debugfs);
502                 data.ctl = igt_debugfs_fopen(&data.debugfs,
503                                              "i915_display_crc_ctl", "r+");
504                 igt_require_f(data.ctl,
505                               "No display_crc_ctl found, kernel too old\n");
506                 written = fwrite(cmd, 1, strlen(cmd), data.ctl);
507                 ret = fflush(data.ctl);
508                 igt_require_f((written == strlen(cmd) && ret == 0) || errno != ENODEV,
509                               "CRCs not supported on this platform\n");
510
511                 status = igt_debugfs_fopen(&data.debugfs, "i915_fbc_status", "r");
512                 igt_require_f(status, "No i915_fbc_status found\n");
513                 fread(buf, sizeof(buf), 1, status);
514                 fclose(status);
515                 buf[sizeof(buf) - 1] = '\0';
516                 igt_require_f(!strstr(buf, "unsupported by this chipset") &&
517                               !strstr(buf, "disabled per module param") &&
518                               !strstr(buf, "disabled per chip default"),
519                               "FBC not supported/enabled\n");
520
521                 data.bufmgr = drm_intel_bufmgr_gem_init(data.drm_fd, 4096);
522                 igt_assert(data.bufmgr);
523                 drm_intel_bufmgr_gem_enable_reuse(data.bufmgr);
524
525                 display_init(&data);
526         }
527
528         for (mode = TEST_PAGE_FLIP; mode <= TEST_PAGE_FLIP_AND_CONTEXT; mode++) {
529                 igt_subtest_f("%s", test_mode_str(mode)) {
530                         run_test(&data, mode);
531                 }
532         }
533
534         igt_fixture {
535                 drm_intel_bufmgr_destroy(data.bufmgr);
536                 display_fini(&data);
537                 fclose(data.ctl);
538         }
539 }