lib/igt.cocci: Add s/assert/igt_assert/
[platform/upstream/intel-gpu-tools.git] / tests / kms_universal_plane.c
1 /*
2  * Copyright © 2014 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 #include <errno.h>
25 #include <stdbool.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #include "drmtest.h"
30 #include "igt_debugfs.h"
31 #include "igt_kms.h"
32
33 typedef struct {
34         int drm_fd;
35         igt_display_t display;
36 } data_t;
37
38 typedef struct {
39         data_t *data;
40         igt_pipe_crc_t *pipe_crc;
41         igt_crc_t crc_1, crc_2, crc_3, crc_4, crc_5, crc_6, crc_7, crc_8,
42                   crc_9, crc_10;
43         struct igt_fb red_fb, blue_fb, black_fb, yellow_fb;
44         drmModeModeInfo *mode;
45 } functional_test_t;
46
47 typedef struct {
48         data_t *data;
49         drmModeResPtr moderes;
50         struct igt_fb blue_fb, oversized_fb, undersized_fb;
51 } sanity_test_t;
52
53 typedef struct {
54         data_t *data;
55         struct igt_fb red_fb, blue_fb;
56 } pageflip_test_t;
57
58 static void
59 functional_test_init(functional_test_t *test, igt_output_t *output, enum pipe pipe)
60 {
61         data_t *data = test->data;
62         drmModeModeInfo *mode;
63
64         test->pipe_crc = igt_pipe_crc_new(pipe, INTEL_PIPE_CRC_SOURCE_AUTO);
65         igt_skip_on_f(!test->pipe_crc,
66                       "auto crc not supported on this connector with pipe %i\n", pipe);
67
68
69         igt_output_set_pipe(output, pipe);
70
71         mode = igt_output_get_mode(output);
72         igt_create_color_fb(data->drm_fd, mode->hdisplay, mode->vdisplay,
73                                 DRM_FORMAT_XRGB8888,
74                                 false, /* tiled */
75                                 0.0, 0.0, 0.0,
76                                 &test->black_fb);
77         igt_create_color_fb(data->drm_fd, mode->hdisplay, mode->vdisplay,
78                                 DRM_FORMAT_XRGB8888,
79                                 false, /* tiled */
80                                 0.0, 0.0, 1.0,
81                                 &test->blue_fb);
82         igt_create_color_fb(data->drm_fd, mode->hdisplay, mode->vdisplay,
83                                 DRM_FORMAT_XRGB8888,
84                                 false, /* tiled */
85                                 1.0, 1.0, 0.0,
86                                 &test->yellow_fb);
87         igt_create_color_fb(data->drm_fd, 100, 100,
88                                 DRM_FORMAT_XRGB8888,
89                                 false, /* tiled */
90                                 1.0, 0.0, 0.0,
91                                 &test->red_fb);
92
93         test->mode = mode;
94 }
95
96 static void
97 functional_test_fini(functional_test_t *test, igt_output_t *output)
98 {
99         igt_pipe_crc_free(test->pipe_crc);
100
101         igt_remove_fb(test->data->drm_fd, &test->black_fb);
102         igt_remove_fb(test->data->drm_fd, &test->blue_fb);
103         igt_remove_fb(test->data->drm_fd, &test->red_fb);
104         igt_remove_fb(test->data->drm_fd, &test->yellow_fb);
105
106         igt_output_set_pipe(output, PIPE_ANY);
107         igt_display_commit2(&test->data->display, COMMIT_LEGACY);
108 }
109
110 /*
111  * Universal plane functional testing.
112  *   - Black primary plane via traditional interfaces, red sprite, grab CRC:1.
113  *   - Blue primary plane via traditional interfaces, red sprite, grab CRC:2.
114  *   - Yellow primary via traditional interfaces
115  *   - Blue primary plane, red sprite via universal planes, grab CRC:3 and compare
116  *     with CRC:2 (should be the same)
117  *   - Disable primary plane, grab CRC:4 (should be same as CRC:1)
118  *   - Reenable primary, grab CRC:5 (should be same as CRC:2 and CRC:3)
119  *   - Yellow primary, no sprite
120  *   - Disable CRTC
121  *   - Program red sprite (while CRTC off)
122  *   - Program blue primary (while CRTC off)
123  *   - Enable CRTC, grab CRC:6 (should be same as CRC:2)
124  */
125 static void
126 functional_test_pipe(data_t *data, enum pipe pipe, igt_output_t *output)
127 {
128         functional_test_t test = { .data = data };
129         igt_display_t *display = &data->display;
130         igt_plane_t *primary, *sprite;
131         int ret;
132         int num_primary = 0, num_cursor = 0;
133         int i;
134
135         igt_assert(data->display.has_universal_planes);
136         igt_skip_on(pipe >= display->n_pipes);
137
138         igt_info("Testing connector %s using pipe %c\n", igt_output_name(output), pipe_name(pipe));
139
140         functional_test_init(&test, output, pipe);
141
142         /*
143          * Make sure we have no more than one primary or cursor plane per crtc.
144          * If the kernel accidentally calls drm_plane_init() rather than
145          * drm_universal_plane_init(), the type enum can get interpreted as a
146          * boolean and show up in userspace as the wrong type.
147          */
148         for (i = 0; i < display->pipes[pipe].n_planes; i++)
149                 if (display->pipes[pipe].planes[i].is_primary)
150                         num_primary++;
151                 else if (display->pipes[pipe].planes[i].is_cursor)
152                         num_cursor++;
153
154         igt_assert(num_primary == 1);
155         igt_assert(num_cursor <= 1);
156
157         primary = igt_output_get_plane(output, IGT_PLANE_PRIMARY);
158         sprite = igt_output_get_plane(output, IGT_PLANE_2);
159         if (!sprite) {
160                 functional_test_fini(&test, output);
161                 igt_skip("No sprite plane available\n");
162         }
163
164         igt_plane_set_position(sprite, 100, 100);
165
166         /* Step 1: Legacy API's, black primary, red sprite (CRC 1) */
167         igt_plane_set_fb(primary, &test.black_fb);
168         igt_plane_set_fb(sprite, &test.red_fb);
169         igt_display_commit2(display, COMMIT_LEGACY);
170         igt_pipe_crc_collect_crc(test.pipe_crc, &test.crc_1);
171
172         /* Step 2: Legacy API', blue primary, red sprite (CRC 2) */
173         igt_plane_set_fb(primary, &test.blue_fb);
174         igt_plane_set_fb(sprite, &test.red_fb);
175         igt_display_commit2(display, COMMIT_LEGACY);
176         igt_pipe_crc_collect_crc(test.pipe_crc, &test.crc_2);
177
178         /* Step 3: Legacy API's, yellow primary (CRC 3) */
179         igt_plane_set_fb(primary, &test.yellow_fb);
180         igt_display_commit2(display, COMMIT_LEGACY);
181         igt_pipe_crc_collect_crc(test.pipe_crc, &test.crc_3);
182
183         /* Step 4: Universal API's, blue primary, red sprite (CRC 4) */
184         igt_plane_set_fb(primary, &test.blue_fb);
185         igt_plane_set_fb(sprite, &test.red_fb);
186         igt_display_commit2(display, COMMIT_UNIVERSAL);
187         igt_pipe_crc_collect_crc(test.pipe_crc, &test.crc_4);
188
189         /* Step 5: Universal API's, disable primary plane (CRC 5) */
190         igt_plane_set_fb(primary, NULL);
191         igt_display_commit2(display, COMMIT_UNIVERSAL);
192         igt_pipe_crc_collect_crc(test.pipe_crc, &test.crc_5);
193
194         /* Step 6: Universal API's, re-enable primary with blue (CRC 6) */
195         igt_plane_set_fb(primary, &test.blue_fb);
196         igt_display_commit2(display, COMMIT_UNIVERSAL);
197         igt_pipe_crc_collect_crc(test.pipe_crc, &test.crc_6);
198
199         /* Step 7: Legacy API's, yellow primary, no sprite */
200         igt_plane_set_fb(primary, &test.yellow_fb);
201         igt_plane_set_fb(sprite, NULL);
202         igt_display_commit2(display, COMMIT_LEGACY);
203
204         /* Step 8: Disable CRTC */
205         igt_plane_set_fb(primary, NULL);
206         igt_display_commit2(display, COMMIT_LEGACY);
207
208         /* Step 9: Universal API's with crtc off:
209          *  - red sprite
210          *  - multiple primary fb's, ending in blue
211          */
212         igt_plane_set_fb(sprite, &test.red_fb);
213         igt_display_commit2(display, COMMIT_UNIVERSAL);
214         igt_plane_set_fb(primary, &test.yellow_fb);
215         igt_display_commit2(display, COMMIT_UNIVERSAL);
216         igt_plane_set_fb(primary, &test.black_fb);
217         igt_display_commit2(display, COMMIT_UNIVERSAL);
218         igt_plane_set_fb(primary, &test.blue_fb);
219         igt_display_commit2(display, COMMIT_UNIVERSAL);
220
221         /* Step 10: Enable crtc (fb = -1), take CRC (CRC 7) */
222         ret = drmModeSetCrtc(data->drm_fd, output->config.crtc->crtc_id, -1,
223                              0, 0, &output->config.connector->connector_id,
224                              1, test.mode);
225         igt_assert(ret == 0);
226         igt_pipe_crc_collect_crc(test.pipe_crc, &test.crc_7);
227
228         /* Step 11: Disable primary plane */
229         igt_plane_set_fb(primary, NULL);
230         igt_display_commit2(display, COMMIT_UNIVERSAL);
231
232         /* Step 12: Legacy modeset to yellow FB (CRC 8) */
233         igt_plane_set_fb(primary, &test.yellow_fb);
234         igt_display_commit2(display, COMMIT_LEGACY);
235         igt_pipe_crc_collect_crc(test.pipe_crc, &test.crc_8);
236
237         /* Step 13: Legacy API', blue primary, red sprite */
238         igt_plane_set_fb(primary, &test.blue_fb);
239         igt_plane_set_fb(sprite, &test.red_fb);
240         igt_display_commit2(display, COMMIT_LEGACY);
241
242         /* Step 14: Universal API, set primary completely offscreen (CRC 9) */
243         ret = drmModeSetPlane(data->drm_fd, primary->drm_plane->plane_id,
244                               output->config.crtc->crtc_id,
245                               test.blue_fb.fb_id, 0,
246                               9000, 9000,
247                               test.mode->hdisplay,
248                               test.mode->vdisplay,
249                               IGT_FIXED(0,0), IGT_FIXED(0,0),
250                               IGT_FIXED(test.mode->hdisplay,0),
251                               IGT_FIXED(test.mode->vdisplay,0));
252         igt_assert(ret == 0);
253         igt_pipe_crc_collect_crc(test.pipe_crc, &test.crc_9);
254
255         /*
256          * Step 15: Explicitly disable primary after it's already been
257          * implicitly disabled (CRC 10).
258          */
259         igt_plane_set_fb(primary, NULL);
260         igt_display_commit2(display, COMMIT_UNIVERSAL);
261         igt_pipe_crc_collect_crc(test.pipe_crc, &test.crc_10);
262
263         /* Step 16: Legacy API's, blue primary, red sprite */
264         igt_plane_set_fb(primary, &test.blue_fb);
265         igt_plane_set_fb(sprite, &test.red_fb);
266         igt_display_commit2(display, COMMIT_LEGACY);
267
268         /* Blue bg + red sprite should be same under both types of API's */
269         igt_assert(igt_crc_equal(&test.crc_2, &test.crc_4));
270
271         /* Disabling primary plane should be same as black primary */
272         igt_assert(igt_crc_equal(&test.crc_1, &test.crc_5));
273
274         /* Re-enabling primary should return to blue properly */
275         igt_assert(igt_crc_equal(&test.crc_2, &test.crc_6));
276
277         /*
278          * We should be able to setup plane FB's while CRTC is disabled and
279          * then have them pop up correctly when the CRTC is re-enabled.
280          */
281         igt_assert(igt_crc_equal(&test.crc_2, &test.crc_7));
282
283         /*
284          * We should be able to modeset with the primary plane off
285          * successfully
286          */
287         igt_assert(igt_crc_equal(&test.crc_3, &test.crc_8));
288
289         /*
290          * We should be able to move the primary plane completely offscreen
291          * and have it disable successfully.
292          */
293         igt_assert(igt_crc_equal(&test.crc_5, &test.crc_9));
294
295         /*
296          * We should be able to explicitly disable an already
297          * implicitly-disabled primary plane
298          */
299         igt_assert(igt_crc_equal(&test.crc_5, &test.crc_10));
300
301         igt_plane_set_fb(primary, NULL);
302         igt_plane_set_fb(sprite, NULL);
303
304         functional_test_fini(&test, output);
305 }
306
307 static void
308 sanity_test_init(sanity_test_t *test, igt_output_t *output, enum pipe pipe)
309 {
310         data_t *data = test->data;
311         drmModeModeInfo *mode;
312
313         igt_output_set_pipe(output, pipe);
314
315         mode = igt_output_get_mode(output);
316         igt_create_color_fb(data->drm_fd, mode->hdisplay, mode->vdisplay,
317                             DRM_FORMAT_XRGB8888,
318                             false, /* tiled */
319                             0.0, 0.0, 1.0,
320                             &test->blue_fb);
321         igt_create_color_fb(data->drm_fd,
322                             mode->hdisplay + 100, mode->vdisplay + 100,
323                             DRM_FORMAT_XRGB8888,
324                             false, /* tiled */
325                             0.0, 0.0, 1.0,
326                             &test->oversized_fb);
327         igt_create_color_fb(data->drm_fd,
328                             mode->hdisplay - 100, mode->vdisplay - 100,
329                             DRM_FORMAT_XRGB8888,
330                             false, /* tiled */
331                             0.0, 0.0, 1.0,
332                             &test->undersized_fb);
333
334         test->moderes = drmModeGetResources(data->drm_fd);
335 }
336
337 static void
338 sanity_test_fini(sanity_test_t *test, igt_output_t *output)
339 {
340         drmModeFreeResources(test->moderes);
341
342         igt_remove_fb(test->data->drm_fd, &test->oversized_fb);
343         igt_remove_fb(test->data->drm_fd, &test->undersized_fb);
344         igt_remove_fb(test->data->drm_fd, &test->blue_fb);
345
346         igt_output_set_pipe(output, PIPE_ANY);
347         igt_display_commit2(&test->data->display, COMMIT_LEGACY);
348 }
349
350 /*
351  * Universal plane sanity testing.
352  *   - Primary doesn't cover CRTC
353  *   - Primary plane tries to scale down
354  *   - Primary plane tries to scale up
355  */
356 static void
357 sanity_test_pipe(data_t *data, enum pipe pipe, igt_output_t *output)
358 {
359         sanity_test_t test = { .data = data };
360         igt_plane_t *primary;
361         drmModeModeInfo *mode;
362         int i, ret = 0;
363
364         igt_output_set_pipe(output, pipe);
365         mode = igt_output_get_mode(output);
366
367         sanity_test_init(&test, output, pipe);
368
369         primary = igt_output_get_plane(output, IGT_PLANE_PRIMARY);
370
371         /* Use legacy API to set a mode with a blue FB */
372         igt_plane_set_fb(primary, &test.blue_fb);
373         igt_display_commit2(&data->display, COMMIT_LEGACY);
374
375         /*
376          * Try to use universal plane API to set primary plane that
377          * doesn't cover CRTC (should fail).
378          */
379         igt_plane_set_fb(primary, &test.undersized_fb);
380         ret = igt_display_try_commit2(&data->display, COMMIT_UNIVERSAL);
381         igt_assert(ret == -EINVAL);
382
383         /* Same as above, but different plane positioning. */
384         igt_plane_set_position(primary, 100, 100);
385         ret = igt_display_try_commit2(&data->display, COMMIT_UNIVERSAL);
386         igt_assert(ret == -EINVAL);
387
388         igt_plane_set_position(primary, 0, 0);
389
390         /* Try to use universal plane API to scale down (should fail) */
391         ret = drmModeSetPlane(data->drm_fd, primary->drm_plane->plane_id,
392                               output->config.crtc->crtc_id,
393                               test.oversized_fb.fb_id, 0,
394                               0, 0,
395                               mode->hdisplay + 100,
396                               mode->vdisplay + 100,
397                               IGT_FIXED(0,0), IGT_FIXED(0,0),
398                               IGT_FIXED(mode->hdisplay,0),
399                               IGT_FIXED(mode->vdisplay,0));
400         igt_assert(ret == -ERANGE);
401
402         /* Try to use universal plane API to scale up (should fail) */
403         ret = drmModeSetPlane(data->drm_fd, primary->drm_plane->plane_id,
404                               output->config.crtc->crtc_id,
405                               test.oversized_fb.fb_id, 0,
406                               0, 0,
407                               mode->hdisplay,
408                               mode->vdisplay,
409                               IGT_FIXED(0,0), IGT_FIXED(0,0),
410                               IGT_FIXED(mode->hdisplay - 100,0),
411                               IGT_FIXED(mode->vdisplay - 100,0));
412         igt_assert(ret == -ERANGE);
413
414         /* Find other crtcs and try to program our primary plane on them */
415         for (i = 0; i < test.moderes->count_crtcs; i++)
416                 if (test.moderes->crtcs[i] != output->config.crtc->crtc_id) {
417                         ret = drmModeSetPlane(data->drm_fd,
418                                               primary->drm_plane->plane_id,
419                                               test.moderes->crtcs[i],
420                                               test.blue_fb.fb_id, 0,
421                                               0, 0,
422                                               mode->hdisplay,
423                                               mode->vdisplay,
424                                               IGT_FIXED(0,0), IGT_FIXED(0,0),
425                                               IGT_FIXED(mode->hdisplay,0),
426                                               IGT_FIXED(mode->vdisplay,0));
427                         igt_assert(ret == -EINVAL);
428                 }
429
430         igt_plane_set_fb(primary, NULL);
431         sanity_test_fini(&test, output);
432 }
433
434 static void
435 pageflip_test_init(pageflip_test_t *test, igt_output_t *output, enum pipe pipe)
436 {
437         data_t *data = test->data;
438         drmModeModeInfo *mode;
439
440         igt_output_set_pipe(output, pipe);
441
442         mode = igt_output_get_mode(output);
443         igt_create_color_fb(data->drm_fd, mode->hdisplay, mode->vdisplay,
444                             DRM_FORMAT_XRGB8888,
445                             false, /* tiled */
446                             1.0, 0.0, 0.0,
447                             &test->red_fb);
448         igt_create_color_fb(data->drm_fd, mode->hdisplay, mode->vdisplay,
449                             DRM_FORMAT_XRGB8888,
450                             false, /* tiled */
451                             0.0, 0.0, 1.0,
452                             &test->blue_fb);
453 }
454
455 static void
456 pageflip_test_fini(pageflip_test_t *test, igt_output_t *output)
457 {
458         igt_remove_fb(test->data->drm_fd, &test->red_fb);
459         igt_remove_fb(test->data->drm_fd, &test->blue_fb);
460
461         igt_output_set_pipe(output, PIPE_ANY);
462         igt_display_commit2(&test->data->display, COMMIT_LEGACY);
463 }
464
465 static void
466 pageflip_test_pipe(data_t *data, enum pipe pipe, igt_output_t *output)
467 {
468         pageflip_test_t test = { .data = data };
469         igt_plane_t *primary;
470         struct timeval timeout = { .tv_sec = 0, .tv_usec = 500 };
471         drmEventContext evctx = { .version = DRM_EVENT_CONTEXT_VERSION };
472
473         fd_set fds;
474         int ret = 0;
475
476         igt_output_set_pipe(output, pipe);
477
478         pageflip_test_init(&test, output, pipe);
479
480         primary = igt_output_get_plane(output, IGT_PLANE_PRIMARY);
481
482         /* Use legacy API to set a mode with a blue FB */
483         igt_plane_set_fb(primary, &test.blue_fb);
484         igt_display_commit2(&data->display, COMMIT_LEGACY);
485
486         /* Disable the primary plane */
487         igt_plane_set_fb(primary, NULL);
488         igt_display_commit2(&data->display, COMMIT_UNIVERSAL);
489
490         /*
491          * Issue a pageflip to red FB
492          *
493          * Note that crtc->primary->fb = NULL causes flip to return EBUSY for
494          * historical reasons...
495          */
496         ret = drmModePageFlip(data->drm_fd, output->config.crtc->crtc_id,
497                               test.red_fb.fb_id, 0, NULL);
498         igt_assert(ret == -EBUSY);
499
500         /* Turn primary plane back on */
501         igt_plane_set_fb(primary, &test.blue_fb);
502         igt_display_commit2(&data->display, COMMIT_UNIVERSAL);
503
504         /*
505          * Issue a pageflip to red, then immediately try to disable the primary
506          * plane, hopefully before the pageflip has a chance to complete.  The
507          * plane disable operation should wind up blocking while the pageflip
508          * completes, which we don't have a good way to specifically test for,
509          * but at least we can make sure that nothing blows up.
510          */
511         ret = drmModePageFlip(data->drm_fd, output->config.crtc->crtc_id,
512                               test.red_fb.fb_id, DRM_MODE_PAGE_FLIP_EVENT,
513                               &test);
514         igt_assert(ret == 0);
515         igt_plane_set_fb(primary, NULL);
516         igt_display_commit2(&data->display, COMMIT_UNIVERSAL);
517
518         /* Wait for pageflip completion, then consume event on fd */
519         FD_ZERO(&fds);
520         FD_SET(data->drm_fd, &fds);
521         do {
522                 ret = select(data->drm_fd + 1, &fds, NULL, NULL, &timeout);
523         } while (ret < 0 && errno == EINTR);
524         igt_assert(ret == 1);
525         ret = drmHandleEvent(data->drm_fd, &evctx);
526         igt_assert(ret == 0);
527
528         igt_plane_set_fb(primary, NULL);
529         pageflip_test_fini(&test, output);
530 }
531
532 static void
533 run_tests_for_pipe(data_t *data, enum pipe pipe)
534 {
535         igt_output_t *output;
536
537         igt_subtest_f("universal-plane-pipe-%c-functional", pipe_name(pipe))
538                 for_each_connected_output(&data->display, output)
539                         functional_test_pipe(data, pipe, output);
540
541         igt_subtest_f("universal-plane-pipe-%c-sanity", pipe_name(pipe))
542                 for_each_connected_output(&data->display, output)
543                         sanity_test_pipe(data, pipe, output);
544
545         igt_subtest_f("disable-primary-vs-flip-pipe-%c", pipe_name(pipe))
546                 for_each_connected_output(&data->display, output)
547                         pageflip_test_pipe(data, pipe, output);
548 }
549
550 static data_t data;
551
552 igt_main
553 {
554
555         igt_skip_on_simulation();
556
557         igt_fixture {
558                 data.drm_fd = drm_open_any();
559
560                 igt_set_vt_graphics_mode();
561
562                 igt_require_pipe_crc();
563                 igt_display_init(&data.display, data.drm_fd);
564
565                 igt_require(data.display.has_universal_planes);
566         }
567
568         for (int pipe = 0; pipe < 3; pipe++)
569                 run_tests_for_pipe(&data, pipe);
570
571         igt_fixture {
572                 igt_display_fini(&data.display);
573         }
574 }