igt/gem_userptr_blits: Fix forked access test
[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 "drmtest.h"
31 #include "igt_debugfs.h"
32 #include "igt_kms.h"
33 #include "igt_aux.h"
34 #include "ioctl_wrappers.h"
35
36 typedef struct {
37         int drm_fd;
38         igt_display_t display;
39         struct igt_fb fb;
40 } data_t;
41
42 static struct {
43         double r, g, b;
44         igt_crc_t crc;
45 } colors[2] = {
46         { .r = 0.0, .g = 1.0, .b = 0.0 },
47         { .r = 0.0, .g = 1.0, .b = 1.0 },
48 };
49
50 static uint64_t submit_batch(int fd, unsigned ring_id)
51 {
52         const uint32_t batch[] = { MI_NOOP,
53                                    MI_BATCH_BUFFER_END };
54         struct drm_i915_gem_execbuffer2 execbuf;
55         struct drm_i915_gem_exec_object2 exec;
56         uint64_t presumed_offset;
57
58         gem_require_ring(fd, ring_id);
59
60         exec.handle = gem_create(fd, 4096);
61         gem_write(fd, exec.handle, 0, batch, sizeof(batch));
62         exec.relocation_count = 0;
63         exec.relocs_ptr = 0;
64         exec.alignment = 0;
65         exec.offset = 0;
66         exec.flags = 0;
67         exec.rsvd1 = 0;
68         exec.rsvd2 = 0;
69
70         execbuf.buffers_ptr = (uintptr_t)&exec;
71         execbuf.buffer_count = 1;
72         execbuf.batch_start_offset = 0;
73         execbuf.batch_len = sizeof(batch);
74         execbuf.cliprects_ptr = 0;
75         execbuf.num_cliprects = 0;
76         execbuf.DR1 = 0;
77         execbuf.DR4 = 0;
78         execbuf.flags = ring_id;
79         i915_execbuffer2_set_context_id(execbuf, 0);
80         execbuf.rsvd2 = 0;
81
82         gem_execbuf(fd, &execbuf);
83         gem_sync(fd, exec.handle);
84         presumed_offset = exec.offset;
85
86         igt_set_stop_rings(igt_to_stop_ring_flag(ring_id));
87
88         gem_execbuf(fd, &execbuf);
89         gem_sync(fd, exec.handle);
90
91         igt_assert(igt_get_stop_rings() == STOP_RING_NONE);
92         igt_assert(presumed_offset == exec.offset);
93
94         gem_close(fd, exec.handle);
95
96         return exec.offset;
97 }
98
99 static void test_bad_command(data_t *data, const char *cmd)
100 {
101         FILE *ctl;
102         size_t written;
103
104         ctl = igt_debugfs_fopen("i915_display_crc_ctl", "r+");
105         written = fwrite(cmd, 1, strlen(cmd), ctl);
106         fflush(ctl);
107         igt_assert_cmpint(written, ==, (strlen(cmd)));
108         igt_assert(ferror(ctl));
109         igt_assert_cmpint(errno, ==, EINVAL);
110
111         fclose(ctl);
112 }
113
114 #define N_CRCS  3
115
116 #define TEST_SEQUENCE (1<<0)
117
118 static int
119 test_read_crc_for_output(data_t *data, int pipe, igt_output_t *output,
120                          unsigned flags)
121 {
122         igt_display_t *display = &data->display;
123         igt_plane_t *primary;
124         drmModeModeInfo *mode;
125         igt_pipe_crc_t *pipe_crc;
126         igt_crc_t *crcs = NULL;
127         int c, j;
128
129         for (c = 0; c < ARRAY_SIZE(colors); c++) {
130                 char *crc_str;
131
132                 igt_output_set_pipe(output, pipe);
133
134                 igt_debug("Clearing the fb with color (%.02lf,%.02lf,%.02lf)\n",
135                           colors[c].r, colors[c].g, colors[c].b);
136
137                 mode = igt_output_get_mode(output);
138                 igt_create_color_fb(data->drm_fd,
139                                         mode->hdisplay, mode->vdisplay,
140                                         DRM_FORMAT_XRGB8888,
141                                         false, /* tiled */
142                                         colors[c].r,
143                                         colors[c].g,
144                                         colors[c].b,
145                                         &data->fb);
146
147                 primary = igt_output_get_plane(output, 0);
148                 igt_plane_set_fb(primary, &data->fb);
149
150                 igt_display_commit(display);
151
152                 pipe_crc = igt_pipe_crc_new(pipe, INTEL_PIPE_CRC_SOURCE_AUTO);
153
154                 if (!pipe_crc)
155                         return 0;
156
157                 igt_pipe_crc_start(pipe_crc);
158
159                 /* wait for N_CRCS vblanks and the corresponding N_CRCS CRCs */
160                 igt_pipe_crc_get_crcs(pipe_crc, N_CRCS, &crcs);
161
162                 igt_pipe_crc_stop(pipe_crc);
163
164                 /*
165                  * save the CRC in colors so it can be compared to the CRC of
166                  * other fbs
167                  */
168                 colors[c].crc = crcs[0];
169
170                 crc_str = igt_crc_to_string(&crcs[0]);
171                 igt_debug("CRC for this fb: %s\n", crc_str);
172                 free(crc_str);
173
174                 /*
175                  * make sure the CRC of this fb is different from the ones of
176                  * previous fbs
177                  */
178                 for (j = 0; j < c; j++)
179                         igt_assert(!igt_crc_equal(&colors[j].crc,
180                                                   &colors[c].crc));
181
182                 /* ensure the CRCs are not all 0s */
183                 for (j = 0; j < N_CRCS; j++)
184                         igt_assert(!igt_crc_is_null(&crcs[j]));
185
186                 /* and ensure that they'are all equal, we haven't changed the fb */
187                 for (j = 0; j < (N_CRCS - 1); j++)
188                         igt_assert(igt_crc_equal(&crcs[j], &crcs[j + 1]));
189
190                 if (flags & TEST_SEQUENCE)
191                         for (j = 0; j < (N_CRCS - 1); j++)
192                                 igt_assert(crcs[j].frame + 1 == crcs[j + 1].frame);
193
194                 free(crcs);
195                 igt_pipe_crc_free(pipe_crc);
196                 igt_remove_fb(data->drm_fd, &data->fb);
197                 igt_plane_set_fb(primary, NULL);
198
199                 igt_output_set_pipe(output, PIPE_ANY);
200         }
201
202         return 1;
203 }
204
205 static void test_read_crc(data_t *data, int pipe, unsigned flags)
206 {
207         igt_display_t *display = &data->display;
208         int valid_connectors = 0;
209         igt_output_t *output;
210
211         igt_skip_on(pipe >= data->display.n_pipes);
212
213         for_each_connected_output(display, output) {
214
215                 igt_info("%s: Testing connector %s using pipe %c\n",
216                          igt_subtest_name(), igt_output_name(output),
217                          pipe_name(pipe));
218
219                 valid_connectors += test_read_crc_for_output(data, pipe, output, flags);
220         }
221
222         igt_require_f(valid_connectors, "No connector found for pipe %i\n", pipe);
223 }
224
225 data_t data = {0, };
226
227 igt_main
228 {
229         igt_skip_on_simulation();
230
231         igt_fixture {
232                 data.drm_fd = drm_open_any();
233
234                 igt_enable_connectors();
235
236                 igt_set_vt_graphics_mode();
237
238                 igt_require_pipe_crc();
239
240                 igt_display_init(&data.display, data.drm_fd);
241         }
242
243         igt_subtest("bad-pipe")
244                 test_bad_command(&data, "pipe D none");
245
246         igt_subtest("bad-source")
247                 test_bad_command(&data, "pipe A foo");
248
249         igt_subtest("bad-nb-words-1")
250                 test_bad_command(&data, "pipe foo");
251
252         igt_subtest("bad-nb-words-3")
253                 test_bad_command(&data, "pipe A none option");
254
255         for (int i = 0; i < 3; i++) {
256                 igt_subtest_f("read-crc-pipe-%c", 'A'+i)
257                         test_read_crc(&data, i, 0);
258
259                 igt_subtest_f("read-crc-pipe-%c-frame-sequence", 'A'+i)
260                         test_read_crc(&data, i, TEST_SEQUENCE);
261
262                 igt_subtest_f("suspend-read-crc-pipe-%c", 'A'+i) {
263                         igt_system_suspend_autoresume();
264
265                         test_read_crc(&data, i, 0);
266                 }
267
268                 igt_subtest_f("hang-read-crc-pipe-%c", 'A'+i) {
269                         submit_batch(data.drm_fd, I915_EXEC_RENDER);
270
271                         test_read_crc(&data, i, 0);
272                 }
273         }
274
275         igt_fixture {
276                 igt_display_fini(&data.display);
277         }
278 }