bdw: Update obvious missing blit support
[platform/upstream/intel-gpu-tools.git] / tests / kms_pipe_crc_basic.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 "drmtest.h"
33 #include "igt_debugfs.h"
34
35 typedef struct {
36         struct kmstest_connector_config config;
37         struct kmstest_fb fb;
38         bool valid;
39 } connector_t;
40
41 typedef struct {
42         int drm_fd;
43         igt_debugfs_t debugfs;
44         drmModeRes *resources;
45         int n_connectors;
46         connector_t *connectors;
47         FILE *ctl;
48 } data_t;
49
50 static void test_bad_command(data_t *data, const char *cmd)
51 {
52         size_t written;
53
54         written = fwrite(cmd, 1, strlen(cmd), data->ctl);
55         fflush(data->ctl);
56         igt_assert_cmpint(written, ==, (strlen(cmd)));
57         igt_assert(ferror(data->ctl));
58         igt_assert_cmpint(errno, ==, EINVAL);
59 }
60
61 static void connector_init(data_t *data, connector_t *connector,
62                            uint32_t id, uint32_t crtc_id_mask)
63 {
64         int ret;
65
66         ret = kmstest_get_connector_config(data->drm_fd, id, crtc_id_mask,
67                                            &connector->config);
68         if (ret == 0)
69                 connector->valid = true;
70         else
71                 connector->valid = false;
72
73 }
74
75 static void connector_fini(connector_t *connector)
76 {
77         kmstest_free_connector_config(&connector->config);
78 }
79
80 static bool
81 connector_set_mode(data_t *data, connector_t *connector, drmModeModeInfo *mode)
82 {
83         struct kmstest_connector_config *config = &connector->config;
84         unsigned int fb_id;
85         cairo_t *cr;
86         int ret;
87
88         fb_id = kmstest_create_fb(data->drm_fd,
89                                   mode->hdisplay, mode->vdisplay,
90                                   32 /* bpp */, 24 /* depth */,
91                                   false /* tiling */,
92                                   &connector->fb);
93         igt_assert(fb_id);
94
95         cr = kmstest_get_cairo_ctx(data->drm_fd, &connector->fb);
96         kmstest_paint_color(cr, 0, 0, mode->hdisplay, mode->vdisplay,
97                             0.0, 1.0, 0.0);
98         igt_assert(cairo_status(cr) == 0);
99
100 #if 0
101         fprintf(stdout, "Using pipe %c, %dx%d\n", pipe_name(config->pipe),
102                 mode->hdisplay, mode->vdisplay);
103 #endif
104
105         ret = drmModeSetCrtc(data->drm_fd,
106                              config->crtc->crtc_id,
107                              connector->fb.fb_id,
108                              0, 0, /* x, y */
109                              &config->connector->connector_id,
110                              1,
111                              mode);
112         igt_assert(ret == 0);
113
114         return 0;
115 }
116
117 static void display_init(data_t *data)
118 {
119         data->resources = drmModeGetResources(data->drm_fd);
120         igt_assert(data->resources);
121
122         data->n_connectors = data->resources->count_connectors;
123         data->connectors = calloc(data->n_connectors, sizeof(connector_t));
124         igt_assert(data->connectors);
125 }
126
127 static void connectors_init(data_t *data, uint32_t crtc_id_mask)
128 {
129         int i;
130
131         for (i = 0; i < data->n_connectors; i++) {
132                 uint32_t id = data->resources->connectors[i];
133
134                 connector_init(data, &data->connectors[i], id, crtc_id_mask);
135         }
136 }
137
138 static void display_fini(data_t *data)
139 {
140         int i;
141
142         for (i = 0; i < data->n_connectors; i++)
143                 connector_fini(&data->connectors[i]);
144         free(data->connectors);
145
146         drmModeFreeResources(data->resources);
147 }
148
149 #define TEST_SEQUENCE (1<<0)
150
151 static void test_read_crc(data_t *data, int pipe, unsigned flags)
152 {
153         connector_t *connector;
154         igt_pipe_crc_t *pipe_crc;
155         igt_crc_t *crcs = NULL;
156         int valid_connectors = 0, i;
157
158         connectors_init(data, 1 << pipe);
159
160         for (i = 0;  i < data->n_connectors; i++) {
161                 connector = &data->connectors[i];
162
163                 if (!connector->valid)
164                         continue;
165
166                 fprintf(stdout, "%s: Testing connector %u\n",
167                         igt_subtest_name(), connector->config.connector->connector_id);
168
169                 connector_set_mode(data, connector, &connector->config.default_mode);
170
171                 pipe_crc = igt_pipe_crc_new(&data->debugfs, data->drm_fd,
172                                             connector->config.pipe,
173                                             INTEL_PIPE_CRC_SOURCE_AUTO);
174
175                 if (!pipe_crc)
176                         continue;
177                 valid_connectors++;
178
179                 igt_assert(igt_pipe_crc_start(pipe_crc));
180
181                 /* wait for 3 vblanks and the corresponding 3 CRCs */
182                 igt_pipe_crc_get_crcs(pipe_crc, 3, &crcs);
183
184                 igt_pipe_crc_stop(pipe_crc);
185
186                 /* ensure the CRCs are not all 0s */
187                 igt_assert(!igt_crc_is_null(&crcs[0]));
188                 igt_assert(!igt_crc_is_null(&crcs[1]));
189                 igt_assert(!igt_crc_is_null(&crcs[2]));
190
191                 /* and ensure that they'are all equal, we haven't changed the fb */
192                 igt_assert(igt_crc_equal(&crcs[0], &crcs[1]));
193                 igt_assert(igt_crc_equal(&crcs[1], &crcs[2]));
194
195                 if (flags & TEST_SEQUENCE) {
196                         igt_assert(crcs[0].frame + 1 == crcs[1].frame);
197                         igt_assert(crcs[1].frame + 1 == crcs[2].frame);
198                 }
199
200                 free(crcs);
201                 igt_pipe_crc_free(pipe_crc);
202                 kmstest_remove_fb(data->drm_fd, &connector->fb);
203         }
204
205         igt_require_f(valid_connectors, "No connector found for pipe %i\n", pipe);
206
207 }
208
209 igt_main
210 {
211         data_t data = {0, };
212
213         igt_skip_on_simulation();
214
215         igt_fixture {
216                 size_t written;
217                 int ret;
218                 const char *cmd = "pipe A none";
219
220                 data.drm_fd = drm_open_any();
221                 igt_require(data.drm_fd >= 0);
222
223                 igt_set_vt_graphics_mode();
224
225                 display_init(&data);
226
227                 igt_debugfs_init(&data.debugfs);
228                 data.ctl = igt_debugfs_fopen(&data.debugfs,
229                                              "i915_display_crc_ctl", "r+");
230                 igt_require_f(data.ctl,
231                               "No display_crc_ctl found, kernel too old\n");
232                 written = fwrite(cmd, 1, strlen(cmd), data.ctl);
233                 ret = fflush(data.ctl);
234                 igt_require_f((written == strlen(cmd) && ret == 0) || errno != ENODEV,
235                               "CRCs not supported on this platform\n");
236         }
237
238         igt_subtest("bad-pipe")
239                 test_bad_command(&data, "pipe D none");
240
241         igt_subtest("bad-source")
242                 test_bad_command(&data, "pipe A foo");
243
244         igt_subtest("bad-nb-words-1")
245                 test_bad_command(&data, "pipe foo");
246
247         igt_subtest("bad-nb-words-3")
248                 test_bad_command(&data, "pipe A none option");
249
250         for (int i = 0; i < 3; i++) {
251                 igt_subtest_f("read-crc-pipe-%c", 'A'+i)
252                         test_read_crc(&data, i, 0);
253
254                 igt_subtest_f("read-crc-pipe-%c-frame-sequence", 'A'+i)
255                         test_read_crc(&data, i, TEST_SEQUENCE);
256         }
257
258         igt_fixture {
259                 display_fini(&data);
260                 fclose(data.ctl);
261         }
262 }