tests: remove unused getopt header includes
[platform/upstream/intel-gpu-tools.git] / tests / drv_hangman.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  * Authors:
24  *    Mika Kuoppala <mika.kuoppala@intel.com>
25  *    Oscar Mateo <oscar.mateo@intel.com>
26  *
27  */
28
29 #include <limits.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33
34 #include "intel_chipset.h"
35 #include "drmtest.h"
36 #include "igt_debugfs.h"
37 #include "ioctl_wrappers.h"
38
39 static int _read_sysfs(void *dst, int maxlen,
40                       const char* path,
41                       const char *fname)
42 {
43         int fd;
44         char full[PATH_MAX];
45         int r, e;
46
47         igt_assert(snprintf(full, PATH_MAX, "%s/%s", path, fname) < PATH_MAX);
48
49         fd = open(full, O_RDONLY);
50         if (fd == -1)
51                 return -errno;
52
53         r = read(fd, dst, maxlen);
54         e = errno;
55         close(fd);
56
57         if (r < 0)
58                 return -e;
59
60         return r;
61 }
62
63 static int read_sysfs(void *dst, int maxlen, const char *fname)
64 {
65         char path[PATH_MAX];
66
67         igt_assert(snprintf(path, PATH_MAX, "/sys/class/drm/card%d",
68                             drm_get_card()) < PATH_MAX);
69
70         return _read_sysfs(dst, maxlen, path, fname);
71 }
72
73 static void test_sysfs_error_exists(void)
74 {
75         char tmp[1024];
76
77         igt_assert(read_sysfs(tmp, sizeof(tmp), "error") > 0);
78 }
79
80 static void test_debugfs_error_state_exists(void)
81 {
82         int fd;
83
84         igt_assert((fd = igt_debugfs_open("i915_error_state", O_RDONLY)) >= 0);
85
86         close (fd);
87 }
88
89 static void test_debugfs_ring_stop_exists(void)
90 {
91         int fd;
92
93         igt_assert((fd = igt_debugfs_open("i915_ring_stop", O_RDONLY)) >= 0);
94
95         close(fd);
96 }
97
98 static void read_dfs(const char *fname, char *d, int maxlen)
99 {
100         int fd;
101         int l;
102
103         igt_assert((fd = igt_debugfs_open(fname, O_RDONLY)) >= 0);
104
105         igt_assert((l = read(fd, d, maxlen-1)) > 0);
106         igt_assert(l < maxlen);
107         d[l] = 0;
108         close(fd);
109
110         igt_debug("dfs entry %s read '%s'\n", fname, d);
111 }
112
113 static void _assert_dfs_entry(const char *fname, const char *s, bool inverse)
114 {
115         char tmp[1024];
116         const int l = strlen(s) < sizeof(tmp) ?
117                 strlen(s) : sizeof(tmp);
118
119         read_dfs(fname, tmp, l + 1);
120         if (!inverse) {
121                 igt_fail_on_f(strncmp(tmp, s, l) != 0,
122                               "contents of %s: '%s' (expected '%s')\n", fname, tmp, s);
123         } else {
124                 igt_fail_on_f(strncmp(tmp, s, l) == 0,
125                               "contents of %s: '%s' (expected not '%s'\n", fname, tmp, s);
126         }
127 }
128
129 static void assert_dfs_entry(const char *fname, const char *s)
130 {
131         _assert_dfs_entry(fname, s, false);
132 }
133
134 static void assert_dfs_entry_not(const char *fname, const char *s)
135 {
136         _assert_dfs_entry(fname, s, true);
137 }
138
139 static void assert_error_state_clear(void)
140 {
141         assert_dfs_entry("i915_error_state", "no error state collected");
142 }
143
144 static void assert_error_state_collected(void)
145 {
146         assert_dfs_entry_not("i915_error_state", "no error state collected");
147 }
148
149 static int get_line_count(const char *s)
150 {
151         int count = 0;
152
153         while (*s) {
154                 if (*s == '\n')
155                         count++;
156                 s++;
157         }
158
159         return count;
160 }
161
162 static void check_other_clients(void)
163 {
164         char tmp[1024];
165         char *s;
166         int dev, pid, uid, magic;
167
168         read_dfs("clients", tmp, sizeof(tmp));
169         if (get_line_count(tmp) <= 2)
170                 return;
171
172         s = strstr(tmp, "y");
173         igt_assert(s != NULL);
174         igt_assert(sscanf(s, "y %d %d %d %d",
175                           &dev, &pid, &uid, &magic) == 4);
176
177         igt_debug("client %d %d %d %d\n", dev, pid, uid, magic);
178         igt_assert(pid == getpid());
179         igt_debug("found myself in client list\n");
180 }
181
182 #define MAGIC_NUMBER 0x10001
183 const uint32_t batch[] = { MI_NOOP,
184                            MI_BATCH_BUFFER_END,
185                            MAGIC_NUMBER,
186                            MAGIC_NUMBER };
187
188 static uint64_t submit_batch(int fd, unsigned ring_id, bool stop_ring)
189 {
190         struct drm_i915_gem_execbuffer2 execbuf;
191         struct drm_i915_gem_exec_object2 exec;
192         uint64_t presumed_offset;
193
194         gem_require_ring(fd, ring_id);
195
196         exec.handle = gem_create(fd, 4096);
197         gem_write(fd, exec.handle, 0, batch, sizeof(batch));
198         exec.relocation_count = 0;
199         exec.relocs_ptr = 0;
200         exec.alignment = 0;
201         exec.offset = 0;
202         exec.flags = 0;
203         exec.rsvd1 = 0;
204         exec.rsvd2 = 0;
205
206         execbuf.buffers_ptr = (uintptr_t)&exec;
207         execbuf.buffer_count = 1;
208         execbuf.batch_start_offset = 0;
209         execbuf.batch_len = sizeof(batch);
210         execbuf.cliprects_ptr = 0;
211         execbuf.num_cliprects = 0;
212         execbuf.DR1 = 0;
213         execbuf.DR4 = 0;
214         execbuf.flags = ring_id;
215         i915_execbuffer2_set_context_id(execbuf, 0);
216         execbuf.rsvd2 = 0;
217
218         gem_execbuf(fd, &execbuf);
219         gem_sync(fd, exec.handle);
220         presumed_offset = exec.offset;
221
222         if (stop_ring) {
223                 igt_set_stop_rings(igt_to_stop_ring_flag(ring_id));
224
225                 gem_execbuf(fd, &execbuf);
226                 gem_sync(fd, exec.handle);
227
228                 igt_assert(igt_get_stop_rings() == STOP_RING_NONE);
229                 igt_assert(presumed_offset == exec.offset);
230         }
231
232         gem_close(fd, exec.handle);
233
234         return exec.offset;
235 }
236
237 static void clear_error_state(void)
238 {
239         int fd;
240         const char *b = "1";
241
242         igt_assert((fd = igt_debugfs_open("i915_error_state", O_WRONLY)) >= 0);
243         igt_assert(write(fd, b, 1) == 1);
244         close(fd);
245 }
246
247 static void test_error_state_basic(void)
248 {
249         int fd;
250
251         check_other_clients();
252         clear_error_state();
253         assert_error_state_clear();
254
255         fd = drm_open_any();
256         submit_batch(fd, I915_EXEC_RENDER, true);
257         close(fd);
258
259         assert_error_state_collected();
260         clear_error_state();
261         assert_error_state_clear();
262 }
263
264 static void check_error_state(const int gen,
265                               const char *expected_ring_name,
266                               uint64_t expected_offset)
267 {
268         FILE *file;
269         int debug_fd;
270         char *line = NULL;
271         size_t line_size = 0;
272         char *ring_name = NULL;
273         bool bb_ok = false, req_ok = false, ringbuf_ok = false;
274
275         debug_fd = igt_debugfs_open("i915_error_state", O_RDONLY);
276         igt_assert(debug_fd >= 0);
277         file = fdopen(debug_fd, "r");
278
279         while (getline(&line, &line_size, file) > 0) {
280                 char *dashes = NULL;
281                 int bb_matched = 0;
282                 uint32_t gtt_offset;
283                 int req_matched = 0;
284                 int requests;
285                 uint32_t tail;
286                 int ringbuf_matched = 0;
287                 int i, items;
288
289                 dashes = strstr(line, "---");
290                 if (!dashes)
291                         continue;
292
293                 ring_name = realloc(ring_name, dashes - line);
294                 strncpy(ring_name, line, dashes - line);
295                 ring_name[dashes - line - 1] = '\0';
296
297                 bb_matched = sscanf(dashes, "--- gtt_offset = 0x%08x\n",
298                                     &gtt_offset);
299                 if (bb_matched == 1) {
300                         char expected_line[32];
301
302                         igt_assert(strstr(ring_name, expected_ring_name));
303                         igt_assert(gtt_offset == expected_offset);
304
305                         for (i = 0; i < sizeof(batch) / 4; i++) {
306                                 igt_assert(getline(&line, &line_size, file) > 0);
307                                 snprintf(expected_line, sizeof(expected_line), "%08x :  %08x",
308                                          4*i, batch[i]);
309                                 igt_assert(strstr(line, expected_line));
310                         }
311                         bb_ok = true;
312                         continue;
313                 }
314
315                 req_matched = sscanf(dashes, "--- %d requests\n", &requests);
316                 if (req_matched == 1) {
317                         igt_assert(strstr(ring_name, expected_ring_name));
318                         igt_assert(requests > 0);
319
320                         for (i = 0; i < requests; i++) {
321                                 uint32_t seqno;
322                                 long jiffies;
323
324                                 igt_assert(getline(&line, &line_size, file) > 0);
325                                 items = sscanf(line, "  seqno 0x%08x, emitted %ld, tail 0x%08x\n",
326                                                &seqno, &jiffies, &tail);
327                                 igt_assert(items == 3);
328                         }
329                         req_ok = true;
330                         continue;
331                 }
332
333                 ringbuf_matched = sscanf(dashes, "--- ringbuffer = 0x%08x\n",
334                                          &gtt_offset);
335                 if (ringbuf_matched == 1) {
336                         unsigned int offset, command, expected_addr = 0;
337
338                         if (!strstr(ring_name, expected_ring_name))
339                                 continue;
340                         igt_assert(req_ok);
341
342                         for (i = 0; i < tail / 4; i++) {
343                                 igt_assert(getline(&line, &line_size, file) > 0);
344                                 items = sscanf(line, "%08x :  %08x\n",
345                                                &offset, &command);
346                                 igt_assert(items == 2);
347                                 if ((command & 0x1F800000) == MI_BATCH_BUFFER_START) {
348                                         igt_assert(getline(&line, &line_size, file) > 0);
349                                         items = sscanf(line, "%08x :  %08x\n",
350                                                        &offset, &expected_addr);
351                                         igt_assert(items == 2);
352                                         i++;
353                                 }
354                         }
355                         if (gen >= 4)
356                                 igt_assert(expected_addr == expected_offset);
357                         else
358                                 igt_assert((expected_addr & ~0x1) == expected_offset);
359                         ringbuf_ok = true;
360                         continue;
361                 }
362
363                 if (bb_ok && req_ok && ringbuf_ok)
364                         break;
365         }
366         igt_assert(bb_ok && req_ok && ringbuf_ok);
367
368         free(line);
369         free(ring_name);
370         close(debug_fd);
371 }
372
373 static void test_error_state_capture(unsigned ring_id,
374                                      const char *ring_name)
375 {
376         int fd, gen;
377         uint64_t offset;
378
379         check_other_clients();
380         clear_error_state();
381
382         fd = drm_open_any();
383         gen = intel_gen(intel_get_drm_devid(fd));
384
385         offset = submit_batch(fd, ring_id, true);
386         close(fd);
387
388         check_error_state(gen, ring_name, offset);
389 }
390
391 static const struct target_ring {
392         const int id;
393         const char *short_name;
394         const char *full_name;
395 } rings[] = {
396         { I915_EXEC_RENDER, "render", "render ring" },
397         { I915_EXEC_BSD, "bsd", "bsd ring" },
398         { I915_EXEC_BLT, "blt", "blitter ring" },
399         { I915_EXEC_VEBOX, "vebox", "video enhancement ring" },
400 };
401
402 igt_main
403 {
404         igt_skip_on_simulation();
405
406         igt_subtest("error-state-debugfs-entry")
407                 test_debugfs_error_state_exists();
408
409         igt_subtest("error-state-sysfs-entry")
410                 test_sysfs_error_exists();
411
412         igt_subtest("ring-stop-sysfs-entry")
413                 test_debugfs_ring_stop_exists();
414
415         igt_subtest("error-state-basic")
416                 test_error_state_basic();
417
418         for (int i = 0; i < sizeof(rings)/sizeof(rings[0]); i++) {
419                 igt_subtest_f("error-state-capture-%s", rings[i].short_name)
420                         test_error_state_capture(rings[i].id, rings[i].full_name);
421         }
422 }