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