2 * Copyright © 2014 Intel Corporation
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:
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
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
24 * Mika Kuoppala <mika.kuoppala@intel.com>
25 * Oscar Mateo <oscar.mateo@intel.com>
30 #include <sys/types.h>
34 #include "intel_chipset.h"
36 #include "igt_debugfs.h"
37 #include "ioctl_wrappers.h"
39 static int _read_sysfs(void *dst, int maxlen,
47 igt_assert(snprintf(full, PATH_MAX, "%s/%s", path, fname) < PATH_MAX);
49 fd = open(full, O_RDONLY);
53 r = read(fd, dst, maxlen);
63 static int read_sysfs(void *dst, int maxlen, const char *fname)
67 igt_assert(snprintf(path, PATH_MAX, "/sys/class/drm/card%d",
68 drm_get_card()) < PATH_MAX);
70 return _read_sysfs(dst, maxlen, path, fname);
73 static void test_sysfs_error_exists(void)
77 igt_assert(read_sysfs(tmp, sizeof(tmp), "error") > 0);
80 static void test_debugfs_error_state_exists(void)
84 igt_assert((fd = igt_debugfs_open("i915_error_state", O_RDONLY)) >= 0);
89 static void test_debugfs_ring_stop_exists(void)
93 igt_assert((fd = igt_debugfs_open("i915_ring_stop", O_RDONLY)) >= 0);
98 static void read_dfs(const char *fname, char *d, int maxlen)
103 igt_assert((fd = igt_debugfs_open(fname, O_RDONLY)) >= 0);
105 igt_assert((l = read(fd, d, maxlen-1)) > 0);
106 igt_assert(l < maxlen);
110 igt_debug("dfs entry %s read '%s'\n", fname, d);
113 static void _assert_dfs_entry(const char *fname, const char *s, bool inverse)
116 const int l = strlen(s) < sizeof(tmp) ?
117 strlen(s) : sizeof(tmp);
119 read_dfs(fname, tmp, l + 1);
121 igt_fail_on_f(strncmp(tmp, s, l) != 0,
122 "contents of %s: '%s' (expected '%s')\n", fname, tmp, s);
124 igt_fail_on_f(strncmp(tmp, s, l) == 0,
125 "contents of %s: '%s' (expected not '%s'\n", fname, tmp, s);
129 static void assert_dfs_entry(const char *fname, const char *s)
131 _assert_dfs_entry(fname, s, false);
134 static void assert_dfs_entry_not(const char *fname, const char *s)
136 _assert_dfs_entry(fname, s, true);
139 static void assert_error_state_clear(void)
141 assert_dfs_entry("i915_error_state", "no error state collected");
144 static void assert_error_state_collected(void)
146 assert_dfs_entry_not("i915_error_state", "no error state collected");
149 static int get_line_count(const char *s)
162 static void check_other_clients(void)
166 int dev, pid, uid, magic;
168 read_dfs("clients", tmp, sizeof(tmp));
169 if (get_line_count(tmp) <= 2)
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);
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");
182 #define MAGIC_NUMBER 0x10001
183 const uint32_t batch[] = { MI_NOOP,
188 static uint64_t submit_batch(int fd, unsigned ring_id, bool stop_ring)
190 struct drm_i915_gem_execbuffer2 execbuf;
191 struct drm_i915_gem_exec_object2 exec;
192 uint64_t presumed_offset;
194 gem_require_ring(fd, ring_id);
196 exec.handle = gem_create(fd, 4096);
197 gem_write(fd, exec.handle, 0, batch, sizeof(batch));
198 exec.relocation_count = 0;
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;
214 execbuf.flags = ring_id;
215 i915_execbuffer2_set_context_id(execbuf, 0);
218 gem_execbuf(fd, &execbuf);
219 gem_sync(fd, exec.handle);
220 presumed_offset = exec.offset;
223 igt_set_stop_rings(igt_to_stop_ring_flag(ring_id));
225 gem_execbuf(fd, &execbuf);
226 gem_sync(fd, exec.handle);
228 igt_assert(igt_get_stop_rings() == STOP_RING_NONE);
229 igt_assert(presumed_offset == exec.offset);
232 gem_close(fd, exec.handle);
237 static void clear_error_state(void)
242 igt_assert((fd = igt_debugfs_open("i915_error_state", O_WRONLY)) >= 0);
243 igt_assert(write(fd, b, 1) == 1);
247 static void test_error_state_basic(void)
251 check_other_clients();
253 assert_error_state_clear();
256 submit_batch(fd, I915_EXEC_RENDER, true);
259 assert_error_state_collected();
261 assert_error_state_clear();
264 static void check_error_state(const int gen,
265 const char *expected_ring_name,
266 uint64_t expected_offset)
271 size_t line_size = 0;
272 char *ring_name = NULL;
273 bool bb_ok = false, req_ok = false, ringbuf_ok = false;
275 debug_fd = igt_debugfs_open("i915_error_state", O_RDONLY);
276 igt_assert(debug_fd >= 0);
277 file = fdopen(debug_fd, "r");
279 while (getline(&line, &line_size, file) > 0) {
286 int ringbuf_matched = 0;
289 dashes = strstr(line, "---");
293 ring_name = realloc(ring_name, dashes - line);
294 strncpy(ring_name, line, dashes - line);
295 ring_name[dashes - line - 1] = '\0';
297 bb_matched = sscanf(dashes, "--- gtt_offset = 0x%08x\n",
299 if (bb_matched == 1) {
300 char expected_line[32];
302 igt_assert(strstr(ring_name, expected_ring_name));
303 igt_assert(gtt_offset == expected_offset);
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",
309 igt_assert(strstr(line, expected_line));
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);
320 for (i = 0; i < requests; i++) {
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);
333 ringbuf_matched = sscanf(dashes, "--- ringbuffer = 0x%08x\n",
335 if (ringbuf_matched == 1) {
336 unsigned int offset, command, expected_addr = 0;
338 if (!strstr(ring_name, expected_ring_name))
342 for (i = 0; i < tail / 4; i++) {
343 igt_assert(getline(&line, &line_size, file) > 0);
344 items = sscanf(line, "%08x : %08x\n",
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);
356 igt_assert(expected_addr == expected_offset);
358 igt_assert((expected_addr & ~0x1) == expected_offset);
363 if (bb_ok && req_ok && ringbuf_ok)
366 igt_assert(bb_ok && req_ok && ringbuf_ok);
373 static void test_error_state_capture(unsigned ring_id,
374 const char *ring_name)
379 check_other_clients();
383 gen = intel_gen(intel_get_drm_devid(fd));
385 offset = submit_batch(fd, ring_id, true);
388 check_error_state(gen, ring_name, offset);
391 static const struct target_ring {
393 const char *short_name;
394 const char *full_name;
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" },
404 igt_skip_on_simulation();
406 igt_subtest("error-state-debugfs-entry")
407 test_debugfs_error_state_exists();
409 igt_subtest("error-state-sysfs-entry")
410 test_sysfs_error_exists();
412 igt_subtest("ring-stop-sysfs-entry")
413 test_debugfs_ring_stop_exists();
415 igt_subtest("error-state-basic")
416 test_error_state_basic();
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);