lib/igt_kms: Unify pipe name helpers
[platform/upstream/intel-gpu-tools.git] / tests / kms_flip_event_leak.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
25 #include <errno.h>
26 #include <stdbool.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "drmtest.h"
31 #include "igt_debugfs.h"
32 #include "igt_kms.h"
33 #include "intel_chipset.h"
34 #include "intel_batchbuffer.h"
35 #include "ioctl_wrappers.h"
36
37 typedef struct {
38         int drm_fd;
39         igt_display_t display;
40 } data_t;
41
42 /*
43  * This test tries to provoke the kernel to leak a pending page flip event
44  * when the fd is closed before the flip has completed. The test itself won't
45  * fail even if the kernel leaks the event, but the resulting dmesg WARN
46  * will cause piglit to report a failure.
47  */
48 static bool test(data_t *data, enum pipe pipe, igt_output_t *output)
49 {
50         igt_plane_t *primary;
51         drmModeModeInfo *mode;
52         struct igt_fb fb[2];
53         int fd, ret;
54
55         /* select the pipe we want to use */
56         igt_output_set_pipe(output, pipe);
57         igt_display_commit(&data->display);
58
59         if (!output->valid) {
60                 igt_output_set_pipe(output, PIPE_ANY);
61                 igt_display_commit(&data->display);
62                 return false;
63         }
64
65         primary = igt_output_get_plane(output, IGT_PLANE_PRIMARY);
66         mode = igt_output_get_mode(output);
67
68         igt_create_color_fb(data->drm_fd, mode->hdisplay, mode->vdisplay,
69                             DRM_FORMAT_XRGB8888,
70                             true, /* tiled */
71                             0.0, 0.0, 0.0, &fb[0]);
72
73         igt_plane_set_fb(primary, &fb[0]);
74         igt_display_commit2(&data->display, COMMIT_LEGACY);
75
76         fd = drm_open_any();
77
78         ret = drmDropMaster(data->drm_fd);
79         igt_assert(ret == 0);
80
81         ret = drmSetMaster(fd);
82         igt_assert(ret == 0);
83
84         igt_create_color_fb(fd, mode->hdisplay, mode->vdisplay,
85                             DRM_FORMAT_XRGB8888,
86                             true, /* tiled */
87                             0.0, 0.0, 0.0, &fb[1]);
88         ret = drmModePageFlip(fd, output->config.crtc->crtc_id,
89                               fb[1].fb_id, DRM_MODE_PAGE_FLIP_EVENT,
90                               data);
91         igt_assert(ret == 0);
92
93         ret = close(fd);
94         igt_assert(ret == 0);
95
96         ret = drmSetMaster(data->drm_fd);
97         igt_assert(ret == 0);
98
99         igt_plane_set_fb(primary, NULL);
100         igt_output_set_pipe(output, PIPE_ANY);
101         igt_display_commit(&data->display);
102
103         igt_remove_fb(data->drm_fd, &fb[0]);
104
105         return true;
106 }
107
108 igt_simple_main
109 {
110         data_t data = {};
111         igt_output_t *output;
112         int valid_tests = 0;
113         enum pipe pipe;
114
115         igt_skip_on_simulation();
116
117         data.drm_fd = drm_open_any();
118         igt_set_vt_graphics_mode();
119
120         igt_display_init(&data.display, data.drm_fd);
121
122         for (pipe = 0; pipe < 3; pipe++) {
123                 for_each_connected_output(&data.display, output) {
124                         if (test(&data, pipe, output))
125                                 valid_tests++;
126                 }
127         }
128
129         igt_require_f(valid_tests, "no valid crtc/connector combinations found\n");
130
131         igt_display_fini(&data.display);
132 }