• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <inttypes.h>
26 #include <sys/stat.h>
27 #include <sys/mount.h>
28 #include <sys/sysmacros.h>
29 #include <dirent.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <limits.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <i915_drm.h>
38 #include <poll.h>
39 
40 #include "drmtest.h"
41 #include "igt_aux.h"
42 #include "igt_kms.h"
43 #include "igt_debugfs.h"
44 #include "igt_sysfs.h"
45 
46 /**
47  * SECTION:igt_debugfs
48  * @short_description: Support code for debugfs features
49  * @title: debugfs
50  * @include: igt.h
51  *
52  * This library provides helpers to access debugfs features. On top of some
53  * basic functions to access debugfs files with e.g. igt_debugfs_open() it also
54  * provides higher-level wrappers for some debugfs features.
55  *
56  * # Pipe CRC Support
57  *
58  * This library wraps up the kernel's support for capturing pipe CRCs into a
59  * neat and tidy package. For the detailed usage see all the functions which
60  * work on #igt_pipe_crc_t. This is supported on all platforms and outputs.
61  *
62  * Actually using pipe CRCs to write modeset tests is a bit tricky though, so
63  * there is no way to directly check a CRC: Both the details of the plane
64  * blending, color correction and other hardware and how exactly the CRC is
65  * computed at each tap point vary by hardware generation and are not disclosed.
66  *
67  * The only way to use #igt_crc_t CRCs therefore is to compare CRCs among each
68  * another either for equality or difference. Otherwise CRCs must be treated as
69  * completely opaque values. Note that not even CRCs from different pipes or tap
70  * points on the same platform can be compared. Hence only use
71  * igt_assert_crc_equal() to inspect CRC values captured by the same
72  * #igt_pipe_crc_t object.
73  *
74  * # Other debugfs interface wrappers
75  *
76  * This covers the miscellaneous debugfs interface wrappers:
77  *
78  * - drm/i915 supports interfaces to evict certain classes of gem buffer
79  *   objects, see igt_drop_caches_set().
80  *
81  * - drm/i915 supports an interface to disable prefaulting, useful to test
82  *   slow paths in ioctls. See igt_disable_prefault().
83  */
84 
85 /*
86  * General debugfs helpers
87  */
88 
is_mountpoint(const char * path)89 static bool is_mountpoint(const char *path)
90 {
91 	char buf[strlen(path) + 4];
92 	struct stat st;
93 	dev_t dev;
94 
95 	igt_assert_lt(snprintf(buf, sizeof(buf), "%s/.", path), sizeof(buf));
96 	if (stat(buf, &st))
97 		return false;
98 
99 	if (!S_ISDIR(st.st_mode))
100 		return false;
101 
102 	dev = st.st_dev;
103 
104 	igt_assert_lt(snprintf(buf, sizeof(buf), "%s/..", path), sizeof(buf));
105 	if (stat(buf, &st))
106 		return false;
107 
108 	if (!S_ISDIR(st.st_mode))
109 		return false;
110 
111 	return dev != st.st_dev;
112 }
113 
__igt_debugfs_mount(void)114 static const char *__igt_debugfs_mount(void)
115 {
116 	if (is_mountpoint("/sys/kernel/debug"))
117 		return "/sys/kernel/debug";
118 
119 	if (is_mountpoint("/debug"))
120 		return "/debug";
121 
122 	if (mount("debug", "/sys/kernel/debug", "debugfs", 0, 0))
123 		return NULL;
124 
125 	return "/sys/kernel/debug";
126 }
127 
128 /**
129  * igt_debugfs_mount:
130  *
131  * This attempts to locate where debugfs is mounted on the filesystem,
132  * and if not found, will then try to mount debugfs at /sys/kernel/debug.
133  *
134  * Returns:
135  * The path to the debugfs mount point (e.g. /sys/kernel/debug)
136  */
igt_debugfs_mount(void)137 const char *igt_debugfs_mount(void)
138 {
139 	static const char *path;
140 
141 	if (!path)
142 		path = __igt_debugfs_mount();
143 
144 	return path;
145 }
146 
147 /**
148  * igt_debugfs_path:
149  * @device: fd of the device
150  * @path: buffer to store path
151  * @pathlen: len of @path buffer.
152  *
153  * This finds the debugfs directory corresponding to @device.
154  *
155  * Returns:
156  * The directory path, or NULL on failure.
157  */
igt_debugfs_path(int device,char * path,int pathlen)158 char *igt_debugfs_path(int device, char *path, int pathlen)
159 {
160 	struct stat st;
161 	const char *debugfs_root;
162 	int idx;
163 
164 	debugfs_root = igt_debugfs_mount();
165 	igt_assert(debugfs_root);
166 
167 	memset(&st, 0, sizeof(st));
168 	if (device != -1) { /* if no fd, we presume we want dri/0 */
169 		if (fstat(device, &st)) {
170 			igt_debug("Couldn't stat FD for DRM device: %m\n");
171 			return NULL;
172 		}
173 
174 		if (!S_ISCHR(st.st_mode)) {
175 			igt_debug("FD for DRM device not a char device!\n");
176 			return NULL;
177 		}
178 	}
179 
180 	idx = minor(st.st_rdev);
181 	snprintf(path, pathlen, "%s/dri/%d/name", debugfs_root, idx);
182 	if (stat(path, &st))
183 		return NULL;
184 
185 	if (idx >= 64) {
186 		int file, name_len, cmp_len;
187 		char name[100], cmp[100];
188 
189 		file = open(path, O_RDONLY);
190 		if (file < 0)
191 			return NULL;
192 
193 		name_len = read(file, name, sizeof(name));
194 		close(file);
195 
196 		for (idx = 0; idx < 16; idx++) {
197 			snprintf(path, pathlen, "%s/dri/%d/name",
198 				 debugfs_root, idx);
199 			file = open(path, O_RDONLY);
200 			if (file < 0)
201 				return NULL;
202 
203 			cmp_len = read(file, cmp, sizeof(cmp));
204 			close(file);
205 
206 			if (cmp_len == name_len && !memcmp(cmp, name, name_len))
207 				break;
208 		}
209 
210 		if (idx == 16)
211 			return NULL;
212 	}
213 
214 	snprintf(path, pathlen, "%s/dri/%d", debugfs_root, idx);
215 	return path;
216 }
217 
218 /**
219  * igt_debugfs_dir:
220  * @device: fd of the device
221  *
222  * This opens the debugfs directory corresponding to device for use
223  * with igt_sysfs_get() and related functions.
224  *
225  * Returns:
226  * The directory fd, or -1 on failure.
227  */
igt_debugfs_dir(int device)228 int igt_debugfs_dir(int device)
229 {
230 	char path[200];
231 
232 	if (!igt_debugfs_path(device, path, sizeof(path)))
233 		return -1;
234 
235 	igt_debug("Opening debugfs directory '%s'\n", path);
236 	return open(path, O_RDONLY);
237 }
238 
239 /**
240  * igt_debugfs_connector_dir:
241  * @device: fd of the device
242  * @conn_name: conenctor name
243  * @mode: mode bits as used by open()
244  *
245  * This opens the debugfs directory corresponding to connector on the device
246  * for use with igt_sysfs_get() and related functions.
247  *
248  * Returns:
249  * The directory fd, or -1 on failure.
250  */
igt_debugfs_connector_dir(int device,char * conn_name,int mode)251 int igt_debugfs_connector_dir(int device, char *conn_name, int mode)
252 {
253 	int dir, ret;
254 
255 	dir = igt_debugfs_dir(device);
256 	if (dir < 0)
257 		return dir;
258 
259 	ret = openat(dir, conn_name, mode);
260 
261 	close(dir);
262 
263 	return ret;
264 }
265 
266 /**
267  * igt_debugfs_open:
268  * @filename: name of the debugfs node to open
269  * @mode: mode bits as used by open()
270  *
271  * This opens a debugfs file as a Unix file descriptor. The filename should be
272  * relative to the drm device's root, i.e. without "drm/$minor".
273  *
274  * Returns:
275  * The Unix file descriptor for the debugfs file or -1 if that didn't work out.
276  */
igt_debugfs_open(int device,const char * filename,int mode)277 int igt_debugfs_open(int device, const char *filename, int mode)
278 {
279 	int dir, ret;
280 
281 	dir = igt_debugfs_dir(device);
282 	if (dir < 0)
283 		return dir;
284 
285 	ret = openat(dir, filename, mode);
286 
287 	close(dir);
288 
289 	return ret;
290 }
291 
292 /**
293  * igt_debugfs_simple_read:
294  * @filename: file name
295  * @buf: buffer where the contents will be stored, allocated by the caller
296  * @size: size of the buffer
297  *
298  * This function is similar to __igt_debugfs_read, the difference is that it
299  * expects the debugfs directory to be open and it's descriptor passed as the
300  * first argument.
301  *
302  * Returns:
303  * -errorno on failure or bytes read on success
304  */
igt_debugfs_simple_read(int dir,const char * filename,char * buf,int size)305 int igt_debugfs_simple_read(int dir, const char *filename, char *buf, int size)
306 {
307 	int len;
308 
309 	len = igt_sysfs_read(dir, filename, buf, size - 1);
310 	if (len < 0)
311 		buf[0] = '\0';
312 	else
313 		buf[len] = '\0';
314 
315 	return len;
316 }
317 
318 /**
319  * __igt_debugfs_read:
320  * @filename: file name
321  * @buf: buffer where the contents will be stored, allocated by the caller
322  * @size: size of the buffer
323  *
324  * This function opens the debugfs file, reads it, stores the content in the
325  * provided buffer, then closes the file. Users should make sure that the buffer
326  * provided is big enough to fit the whole file, plus one byte.
327  */
__igt_debugfs_read(int fd,const char * filename,char * buf,int size)328 void __igt_debugfs_read(int fd, const char *filename, char *buf, int size)
329 {
330 	int dir = igt_debugfs_dir(fd);
331 
332 	igt_debugfs_simple_read(dir, filename, buf, size);
333 	close(dir);
334 }
335 
336 /**
337  * igt_debugfs_search:
338  * @filename: file name
339  * @substring: string to search for in @filename
340  *
341  * Searches each line in @filename for the substring specified in @substring.
342  *
343  * Returns: True if the @substring is found to occur in @filename
344  */
igt_debugfs_search(int device,const char * filename,const char * substring)345 bool igt_debugfs_search(int device, const char *filename, const char *substring)
346 {
347 	FILE *file;
348 	size_t n = 0;
349 	char *line = NULL;
350 	bool matched = false;
351 	int fd;
352 
353 	fd = igt_debugfs_open(device, filename, O_RDONLY);
354 	file = fdopen(fd, "r");
355 	igt_assert(file);
356 
357 	while (getline(&line, &n, file) >= 0) {
358 		matched = strstr(line, substring) != NULL;
359 		if (matched)
360 			break;
361 	}
362 
363 	free(line);
364 	fclose(file);
365 	close(fd);
366 
367 	return matched;
368 }
369 
370 /*
371  * Pipe CRC
372  */
373 
igt_find_crc_mismatch(const igt_crc_t * a,const igt_crc_t * b,int * index)374 static bool igt_find_crc_mismatch(const igt_crc_t *a, const igt_crc_t *b,
375 				  int *index)
376 {
377 	int nwords = min(a->n_words, b->n_words);
378 	int i;
379 
380 	for (i = 0; i < nwords; i++) {
381 		if (a->crc[i] != b->crc[i]) {
382 			if (index)
383 				*index = i;
384 
385 			return true;
386 		}
387 	}
388 
389 	if (a->n_words != b->n_words) {
390 		if (index)
391 			*index = i;
392 		return true;
393 	}
394 
395 	return false;
396 }
397 
398 /**
399  * igt_assert_crc_equal:
400  * @a: first pipe CRC value
401  * @b: second pipe CRC value
402  *
403  * Compares two CRC values and fails the testcase if they don't match with
404  * igt_fail(). Note that due to CRC collisions CRC based testcase can only
405  * assert that CRCs match, never that they are different. Otherwise there might
406  * be random testcase failures when different screen contents end up with the
407  * same CRC by chance.
408  *
409  * Passing --skip-crc-compare on the command line will force this function
410  * to always pass, which can be useful in interactive debugging where you
411  * might know the test will fail, but still want the test to keep going as if
412  * it had succeeded so that you can see the on-screen behavior.
413  *
414  */
igt_assert_crc_equal(const igt_crc_t * a,const igt_crc_t * b)415 void igt_assert_crc_equal(const igt_crc_t *a, const igt_crc_t *b)
416 {
417 	int index;
418 	bool mismatch;
419 
420 	mismatch = igt_find_crc_mismatch(a, b, &index);
421 	if (mismatch)
422 		igt_debug("CRC mismatch%s at index %d: 0x%x != 0x%x\n",
423 			  igt_skip_crc_compare ? " (ignored)" : "",
424 			  index, a->crc[index], b->crc[index]);
425 
426 	igt_assert(!mismatch || igt_skip_crc_compare);
427 }
428 
429 /**
430  * igt_check_crc_equal:
431  * @a: first pipe CRC value
432  * @b: second pipe CRC value
433  *
434  * Compares two CRC values and return whether they match.
435  *
436  * Returns: A boolean indicating whether the CRC values match
437  */
igt_check_crc_equal(const igt_crc_t * a,const igt_crc_t * b)438 bool igt_check_crc_equal(const igt_crc_t *a, const igt_crc_t *b)
439 {
440 	int index;
441 	bool mismatch;
442 
443 	mismatch = igt_find_crc_mismatch(a, b, &index);
444 	if (mismatch)
445 		igt_debug("CRC mismatch at index %d: 0x%x != 0x%x\n", index,
446 			  a->crc[index], b->crc[index]);
447 
448 	return !mismatch;
449 }
450 
451 /**
452  * igt_crc_to_string_extended:
453  * @crc: pipe CRC value to print
454  * @delimiter: The delimiter to use between crc words
455  * @crc_size: the number of bytes to print per crc word (between 1 and 4)
456  *
457  * This function allocates a string and formats @crc into it, depending on
458  * @delimiter and @crc_size.
459  * The caller is responsible for freeing the string.
460  *
461  * This should only ever be used for diagnostic debug output.
462  */
igt_crc_to_string_extended(igt_crc_t * crc,char delimiter,int crc_size)463 char *igt_crc_to_string_extended(igt_crc_t *crc, char delimiter, int crc_size)
464 {
465 	int i;
466 	int len = 0;
467 	int field_width = 2 * crc_size; /* Two chars per byte. */
468 	char *buf = malloc((field_width+1) * crc->n_words);
469 
470 	if (!buf)
471 		return NULL;
472 
473 	for (i = 0; i < crc->n_words - 1; i++)
474 		len += sprintf(buf + len, "%0*x%c", field_width,
475 			       crc->crc[i], delimiter);
476 
477 	sprintf(buf + len, "%0*x", field_width, crc->crc[i]);
478 
479 	return buf;
480 }
481 
482 /**
483  * igt_crc_to_string:
484  * @crc: pipe CRC value to print
485  *
486  * This function allocates a string and formats @crc into it.
487  * The caller is responsible for freeing the string.
488  *
489  * This should only ever be used for diagnostic debug output.
490  */
igt_crc_to_string(igt_crc_t * crc)491 char *igt_crc_to_string(igt_crc_t *crc)
492 {
493 	return igt_crc_to_string_extended(crc, ' ', 4);
494 }
495 
496 #define MAX_CRC_ENTRIES 10
497 #define MAX_LINE_LEN (10 + 11 * MAX_CRC_ENTRIES + 1)
498 
499 struct _igt_pipe_crc {
500 	int fd;
501 	int dir;
502 	int ctl_fd;
503 	int crc_fd;
504 	int flags;
505 
506 	enum pipe pipe;
507 	char *source;
508 };
509 
510 /**
511  * igt_require_pipe_crc:
512  *
513  * Convenience helper to check whether pipe CRC capturing is supported by the
514  * kernel. Uses igt_skip to automatically skip the test/subtest if this isn't
515  * the case.
516  */
igt_require_pipe_crc(int fd)517 void igt_require_pipe_crc(int fd)
518 {
519 	int dir;
520 	struct stat stat;
521 
522 	dir = igt_debugfs_dir(fd);
523 	igt_require_f(dir >= 0, "Could not open debugfs directory\n");
524 	igt_require_f(fstatat(dir, "crtc-0/crc/control", &stat, 0) == 0,
525 		      "CRCs not supported on this platform\n");
526 
527 	close(dir);
528 }
529 
igt_hpd_storm_exit_handler(int sig)530 static void igt_hpd_storm_exit_handler(int sig)
531 {
532 	int fd = drm_open_driver(DRIVER_INTEL);
533 
534 	/* Here we assume that only one i915 device will be ever present */
535 	igt_hpd_storm_reset(fd);
536 
537 	close(fd);
538 }
539 
540 /**
541  * igt_hpd_storm_set_threshold:
542  * @threshold: How many hotplugs per second required to trigger an HPD storm,
543  * or 0 to disable storm detection.
544  *
545  * Convienence helper to configure the HPD storm detection threshold for i915
546  * through debugfs. Useful for hotplugging tests where HPD storm detection
547  * might get in the way and slow things down.
548  *
549  * If the system does not support HPD storm detection, this function does
550  * nothing.
551  *
552  * See: https://01.org/linuxgraphics/gfx-docs/drm/gpu/i915.html#hotplug
553  */
igt_hpd_storm_set_threshold(int drm_fd,unsigned int threshold)554 void igt_hpd_storm_set_threshold(int drm_fd, unsigned int threshold)
555 {
556 	int fd = igt_debugfs_open(drm_fd, "i915_hpd_storm_ctl", O_WRONLY);
557 	char buf[16];
558 
559 	if (fd < 0)
560 		return;
561 
562 	igt_debug("Setting HPD storm threshold to %d\n", threshold);
563 	snprintf(buf, sizeof(buf), "%d", threshold);
564 	igt_assert_eq(write(fd, buf, strlen(buf)), strlen(buf));
565 
566 	close(fd);
567 	igt_install_exit_handler(igt_hpd_storm_exit_handler);
568 }
569 
570 /**
571  * igt_hpd_storm_reset:
572  *
573  * Convienence helper to reset HPD storm detection to it's default settings.
574  * If hotplug detection was disabled on any ports due to an HPD storm, it will
575  * be immediately re-enabled. Always called on exit if the HPD storm detection
576  * threshold was modified during any tests.
577  *
578  * If the system does not support HPD storm detection, this function does
579  * nothing.
580  *
581  * See: https://01.org/linuxgraphics/gfx-docs/drm/gpu/i915.html#hotplug
582  */
igt_hpd_storm_reset(int drm_fd)583 void igt_hpd_storm_reset(int drm_fd)
584 {
585 	int fd = igt_debugfs_open(drm_fd, "i915_hpd_storm_ctl", O_WRONLY);
586 	const char *buf = "reset";
587 
588 	if (fd < 0)
589 		return;
590 
591 	igt_debug("Resetting HPD storm threshold\n");
592 	igt_assert_eq(write(fd, buf, strlen(buf)), strlen(buf));
593 
594 	close(fd);
595 }
596 
597 /**
598  * igt_hpd_storm_detected:
599  *
600  * Checks whether or not i915 has detected an HPD interrupt storm on any of the
601  * system's ports.
602  *
603  * This function always returns false on systems that do not support HPD storm
604  * detection.
605  *
606  * See: https://01.org/linuxgraphics/gfx-docs/drm/gpu/i915.html#hotplug
607  *
608  * Returns: Whether or not an HPD storm has been detected.
609  */
igt_hpd_storm_detected(int drm_fd)610 bool igt_hpd_storm_detected(int drm_fd)
611 {
612 	int fd = igt_debugfs_open(drm_fd, "i915_hpd_storm_ctl", O_RDONLY);
613 	char *start_loc;
614 	char buf[32] = {0}, detected_str[4];
615 	bool ret;
616 
617 	if (fd < 0)
618 		return false;
619 
620 	igt_assert_lt(0, read(fd, buf, sizeof(buf) - 1));
621 	igt_assert(start_loc = strstr(buf, "Detected: "));
622 	igt_assert_eq(sscanf(start_loc, "Detected: %s\n", detected_str), 1);
623 
624 	if (strcmp(detected_str, "yes") == 0)
625 		ret = true;
626 	else if (strcmp(detected_str, "no") == 0)
627 		ret = false;
628 	else
629 		igt_fail_on_f(true, "Unknown hpd storm detection status '%s'\n",
630 			      detected_str);
631 
632 	close(fd);
633 	return ret;
634 }
635 
636 /**
637  * igt_require_hpd_storm_ctl:
638  *
639  * Skips the current test if the system does not have HPD storm detection.
640  *
641  * See: https://01.org/linuxgraphics/gfx-docs/drm/gpu/i915.html#hotplug
642  */
igt_require_hpd_storm_ctl(int drm_fd)643 void igt_require_hpd_storm_ctl(int drm_fd)
644 {
645 	int fd = igt_debugfs_open(drm_fd, "i915_hpd_storm_ctl", O_RDONLY);
646 
647 	igt_require_f(fd > 0, "No i915_hpd_storm_ctl found in debugfs\n");
648 	close(fd);
649 }
650 
651 static igt_pipe_crc_t *
pipe_crc_new(int fd,enum pipe pipe,const char * source,int flags)652 pipe_crc_new(int fd, enum pipe pipe, const char *source, int flags)
653 {
654 	igt_pipe_crc_t *pipe_crc;
655 	char buf[128];
656 	int debugfs;
657 
658 	igt_assert(source);
659 
660 	debugfs = igt_debugfs_dir(fd);
661 	igt_assert(debugfs != -1);
662 
663 	pipe_crc = calloc(1, sizeof(struct _igt_pipe_crc));
664 
665 	sprintf(buf, "crtc-%d/crc/control", pipe);
666 	pipe_crc->ctl_fd = openat(debugfs, buf, O_WRONLY);
667 	igt_assert(pipe_crc->ctl_fd != -1);
668 
669 	pipe_crc->crc_fd = -1;
670 	pipe_crc->fd = fd;
671 	pipe_crc->dir = debugfs;
672 	pipe_crc->pipe = pipe;
673 	pipe_crc->source = strdup(source);
674 	igt_assert(pipe_crc->source);
675 	pipe_crc->flags = flags;
676 
677 	return pipe_crc;
678 }
679 
680 /**
681  * igt_pipe_crc_new:
682  * @pipe: display pipe to use as source
683  * @source: CRC tap point to use as source
684  *
685  * This sets up a new pipe CRC capture object for the given @pipe and @source
686  * in blocking mode.
687  *
688  * Returns: A pipe CRC object for the given @pipe and @source. The library
689  * assumes that the source is always available since recent kernels support at
690  * least INTEL_PIPE_CRC_SOURCE_AUTO everywhere.
691  */
692 igt_pipe_crc_t *
igt_pipe_crc_new(int fd,enum pipe pipe,const char * source)693 igt_pipe_crc_new(int fd, enum pipe pipe, const char *source)
694 {
695 	return pipe_crc_new(fd, pipe, source, O_RDONLY);
696 }
697 
698 /**
699  * igt_pipe_crc_new_nonblock:
700  * @pipe: display pipe to use as source
701  * @source: CRC tap point to use as source
702  *
703  * This sets up a new pipe CRC capture object for the given @pipe and @source
704  * in nonblocking mode.
705  *
706  * Returns: A pipe CRC object for the given @pipe and @source. The library
707  * assumes that the source is always available since recent kernels support at
708  * least INTEL_PIPE_CRC_SOURCE_AUTO everywhere.
709  */
710 igt_pipe_crc_t *
igt_pipe_crc_new_nonblock(int fd,enum pipe pipe,const char * source)711 igt_pipe_crc_new_nonblock(int fd, enum pipe pipe, const char *source)
712 {
713 	return pipe_crc_new(fd, pipe, source, O_RDONLY | O_NONBLOCK);
714 }
715 
716 /**
717  * igt_pipe_crc_free:
718  * @pipe_crc: pipe CRC object
719  *
720  * Frees all resources associated with @pipe_crc.
721  */
igt_pipe_crc_free(igt_pipe_crc_t * pipe_crc)722 void igt_pipe_crc_free(igt_pipe_crc_t *pipe_crc)
723 {
724 	if (!pipe_crc)
725 		return;
726 
727 	close(pipe_crc->ctl_fd);
728 	close(pipe_crc->crc_fd);
729 	close(pipe_crc->dir);
730 	free(pipe_crc->source);
731 	free(pipe_crc);
732 }
733 
pipe_crc_init_from_string(igt_pipe_crc_t * pipe_crc,igt_crc_t * crc,const char * line)734 static bool pipe_crc_init_from_string(igt_pipe_crc_t *pipe_crc, igt_crc_t *crc,
735 				      const char *line)
736 {
737 	int i;
738 	const char *buf;
739 
740 	if (strncmp(line, "XXXXXXXXXX", 10) == 0)
741 		crc->has_valid_frame = false;
742 	else {
743 		crc->has_valid_frame = true;
744 		crc->frame = strtoul(line, NULL, 16);
745 	}
746 
747 	buf = line + 10;
748 	for (i = 0; *buf != '\n'; i++, buf += 11)
749 		crc->crc[i] = strtoul(buf, NULL, 16);
750 
751 	crc->n_words = i;
752 
753 	return true;
754 }
755 
read_crc(igt_pipe_crc_t * pipe_crc,igt_crc_t * out)756 static int read_crc(igt_pipe_crc_t *pipe_crc, igt_crc_t *out)
757 {
758 	ssize_t bytes_read;
759 	char buf[MAX_LINE_LEN + 1];
760 
761 	igt_set_timeout(5, "CRC reading");
762 	bytes_read = read(pipe_crc->crc_fd, &buf, MAX_LINE_LEN);
763 	igt_reset_timeout();
764 
765 	if (bytes_read < 0)
766 		bytes_read = -errno;
767 	else
768 		buf[bytes_read] = '\0';
769 
770 	if (bytes_read > 0 && !pipe_crc_init_from_string(pipe_crc, out, buf))
771 		return -EINVAL;
772 
773 	return bytes_read;
774 }
775 
read_one_crc(igt_pipe_crc_t * pipe_crc,igt_crc_t * out)776 static void read_one_crc(igt_pipe_crc_t *pipe_crc, igt_crc_t *out)
777 {
778 	int ret;
779 
780 	fcntl(pipe_crc->crc_fd, F_SETFL, pipe_crc->flags & ~O_NONBLOCK);
781 
782 	do {
783 		ret = read_crc(pipe_crc, out);
784 	} while (ret == -EINTR);
785 
786 	fcntl(pipe_crc->crc_fd, F_SETFL, pipe_crc->flags);
787 }
788 
789 /**
790  * igt_pipe_crc_start:
791  * @pipe_crc: pipe CRC object
792  *
793  * Starts the CRC capture process on @pipe_crc.
794  */
igt_pipe_crc_start(igt_pipe_crc_t * pipe_crc)795 void igt_pipe_crc_start(igt_pipe_crc_t *pipe_crc)
796 {
797 	const char *src = pipe_crc->source;
798 	struct pollfd pfd;
799 	char buf[32];
800 
801 	/* Stop first just to make sure we don't have lingering state left. */
802 	igt_pipe_crc_stop(pipe_crc);
803 
804 	igt_reset_fifo_underrun_reporting(pipe_crc->fd);
805 
806 	igt_assert_eq(write(pipe_crc->ctl_fd, src, strlen(src)), strlen(src));
807 
808 	sprintf(buf, "crtc-%d/crc/data", pipe_crc->pipe);
809 
810 	igt_set_timeout(10, "Opening crc fd, and poll for first CRC.");
811 	pipe_crc->crc_fd = openat(pipe_crc->dir, buf, pipe_crc->flags);
812 	igt_assert(pipe_crc->crc_fd != -1);
813 
814 	pfd.fd = pipe_crc->crc_fd;
815 	pfd.events = POLLIN;
816 	poll(&pfd, 1, -1);
817 
818 	igt_reset_timeout();
819 
820 	errno = 0;
821 }
822 
823 /**
824  * igt_pipe_crc_stop:
825  * @pipe_crc: pipe CRC object
826  *
827  * Stops the CRC capture process on @pipe_crc.
828  */
igt_pipe_crc_stop(igt_pipe_crc_t * pipe_crc)829 void igt_pipe_crc_stop(igt_pipe_crc_t *pipe_crc)
830 {
831 	close(pipe_crc->crc_fd);
832 	pipe_crc->crc_fd = -1;
833 }
834 
835 /**
836  * igt_pipe_crc_get_crcs:
837  * @pipe_crc: pipe CRC object
838  * @n_crcs: number of CRCs to capture
839  * @out_crcs: buffer pointer for the captured CRC values
840  *
841  * Read up to @n_crcs from @pipe_crc. This function does not block, and will
842  * return early if not enough CRCs can be captured, if @pipe_crc has been
843  * opened using igt_pipe_crc_new_nonblock(). It will block until @n_crcs are
844  * retrieved if @pipe_crc has been opened using igt_pipe_crc_new(). @out_crcs is
845  * alloced by this function and must be released with free() by the caller.
846  *
847  * Callers must start and stop the capturing themselves by calling
848  * igt_pipe_crc_start() and igt_pipe_crc_stop(). For one-shot CRC collecting
849  * look at igt_pipe_crc_collect_crc().
850  *
851  * Returns:
852  * The number of CRCs captured. Should be equal to @n_crcs in blocking mode, but
853  * can be less (even zero) in non-blocking mode.
854  */
855 int
igt_pipe_crc_get_crcs(igt_pipe_crc_t * pipe_crc,int n_crcs,igt_crc_t ** out_crcs)856 igt_pipe_crc_get_crcs(igt_pipe_crc_t *pipe_crc, int n_crcs,
857 		      igt_crc_t **out_crcs)
858 {
859 	igt_crc_t *crcs;
860 	int n = 0;
861 
862 	crcs = calloc(n_crcs, sizeof(igt_crc_t));
863 
864 	do {
865 		igt_crc_t *crc = &crcs[n];
866 		int ret;
867 
868 		ret = read_crc(pipe_crc, crc);
869 		if (ret == -EAGAIN)
870 			break;
871 
872 		if (ret < 0)
873 			continue;
874 
875 		n++;
876 	} while (n < n_crcs);
877 
878 	*out_crcs = crcs;
879 	return n;
880 }
881 
crc_sanity_checks(igt_pipe_crc_t * pipe_crc,igt_crc_t * crc)882 static void crc_sanity_checks(igt_pipe_crc_t *pipe_crc, igt_crc_t *crc)
883 {
884 	int i;
885 	bool all_zero = true;
886 
887 	/* Any CRC value can be considered valid on amdgpu hardware. */
888 	if (is_amdgpu_device(pipe_crc->fd))
889 		return;
890 
891 	for (i = 0; i < crc->n_words; i++) {
892 		igt_warn_on_f(crc->crc[i] == 0xffffffff,
893 			      "Suspicious CRC: it looks like the CRC "
894 			      "read back was from a register in a powered "
895 			      "down well\n");
896 		if (crc->crc[i])
897 			all_zero = false;
898 	}
899 
900 	igt_warn_on_f(all_zero, "Suspicious CRC: All values are 0.\n");
901 }
902 
903 /**
904  * igt_pipe_crc_drain:
905  * @pipe_crc: pipe CRC object
906  *
907  * Discards all currently queued CRC values from @pipe_crc. This function does
908  * not block, and is useful to flush @pipe_crc. Afterwards you can get a fresh
909  * CRC with igt_pipe_crc_get_single().
910  */
igt_pipe_crc_drain(igt_pipe_crc_t * pipe_crc)911 void igt_pipe_crc_drain(igt_pipe_crc_t *pipe_crc)
912 {
913 	int ret;
914 	igt_crc_t crc;
915 
916 	fcntl(pipe_crc->crc_fd, F_SETFL, pipe_crc->flags | O_NONBLOCK);
917 
918 	do {
919 		ret = read_crc(pipe_crc, &crc);
920 	} while (ret > 0 || ret == -EINVAL);
921 
922 	fcntl(pipe_crc->crc_fd, F_SETFL, pipe_crc->flags);
923 }
924 
925 /**
926  * igt_pipe_crc_get_single:
927  * @pipe_crc: pipe CRC object
928  * @crc: buffer pointer for the captured CRC value
929  *
930  * Read a single @crc from @pipe_crc. This function blocks even
931  * when nonblocking CRC is requested.
932  *
933  * Callers must start and stop the capturing themselves by calling
934  * igt_pipe_crc_start() and igt_pipe_crc_stop(). For one-shot CRC collecting
935  * look at igt_pipe_crc_collect_crc().
936  *
937  * If capturing has been going on for a while and a fresh crc is required,
938  * you should use igt_pipe_crc_get_current() instead.
939  */
igt_pipe_crc_get_single(igt_pipe_crc_t * pipe_crc,igt_crc_t * crc)940 void igt_pipe_crc_get_single(igt_pipe_crc_t *pipe_crc, igt_crc_t *crc)
941 {
942 	read_one_crc(pipe_crc, crc);
943 
944 	crc_sanity_checks(pipe_crc, crc);
945 }
946 
947 /**
948  * igt_pipe_crc_get_current:
949  * @drm_fd: Pointer to drm fd for vblank counter
950  * @pipe_crc: pipe CRC object
951  * @crc: buffer pointer for the captured CRC value
952  *
953  * Same as igt_pipe_crc_get_single(), but will wait until a new CRC can be captured.
954  * This is useful for retrieving the current CRC in a more race free way than
955  * igt_pipe_crc_drain() + igt_pipe_crc_get_single().
956  */
957 void
igt_pipe_crc_get_current(int drm_fd,igt_pipe_crc_t * pipe_crc,igt_crc_t * crc)958 igt_pipe_crc_get_current(int drm_fd, igt_pipe_crc_t *pipe_crc, igt_crc_t *crc)
959 {
960 	unsigned vblank = kmstest_get_vblank(drm_fd, pipe_crc->pipe, 0);
961 
962 	do {
963 		read_one_crc(pipe_crc, crc);
964 
965 		/* Only works with valid frame counter */
966 		if (!crc->has_valid_frame) {
967 			igt_pipe_crc_drain(pipe_crc);
968 			igt_pipe_crc_get_single(pipe_crc, crc);
969 			return;
970 		}
971 	} while (igt_vblank_before_eq(crc->frame, vblank));
972 
973 	crc_sanity_checks(pipe_crc, crc);
974 }
975 
976 /**
977  * igt_pipe_crc_collect_crc:
978  * @pipe_crc: pipe CRC object
979  * @out_crc: buffer for the captured CRC values
980  *
981  * Read a single CRC from @pipe_crc. This function blocks until the CRC is
982  * retrieved, irrespective of whether @pipe_crc has been opened with
983  * igt_pipe_crc_new() or igt_pipe_crc_new_nonblock().  @out_crc must be
984  * allocated by the caller.
985  *
986  * This function takes care of the pipe_crc book-keeping, it will start/stop
987  * the collection of the CRC.
988  *
989  * This function also calls the interactive debug with the "crc" domain, so you
990  * can make use of this feature to actually see the screen that is being CRC'd.
991  *
992  * For continuous CRC collection look at igt_pipe_crc_start(),
993  * igt_pipe_crc_get_crcs() and igt_pipe_crc_stop().
994  */
igt_pipe_crc_collect_crc(igt_pipe_crc_t * pipe_crc,igt_crc_t * out_crc)995 void igt_pipe_crc_collect_crc(igt_pipe_crc_t *pipe_crc, igt_crc_t *out_crc)
996 {
997 	igt_debug_wait_for_keypress("crc");
998 
999 	igt_pipe_crc_start(pipe_crc);
1000 	igt_pipe_crc_get_single(pipe_crc, out_crc);
1001 	igt_pipe_crc_stop(pipe_crc);
1002 }
1003 
1004 /**
1005  * igt_reset_fifo_underrun_reporting:
1006  * @drm_fd: drm device file descriptor
1007  *
1008  * Resets fifo underrun reporting, if supported by the device. Useful since fifo
1009  * underrun reporting tends to be one-shot, so good to reset it before the
1010  * actual functional test again in case there's been a separate issue happening
1011  * while preparing the test setup.
1012  */
igt_reset_fifo_underrun_reporting(int drm_fd)1013 void igt_reset_fifo_underrun_reporting(int drm_fd)
1014 {
1015 	int fd = igt_debugfs_open(drm_fd, "i915_fifo_underrun_reset", O_WRONLY);
1016 	if (fd >= 0) {
1017 		igt_assert_eq(write(fd, "y", 1), 1);
1018 
1019 		close(fd);
1020 	}
1021 }
1022 
1023 /*
1024  * Drop caches
1025  */
1026 
1027 /**
1028  * igt_drop_caches_has:
1029  * @val: bitmask for DROP_* values
1030  *
1031  * This queries the debugfs to see if it supports the full set of desired
1032  * operations.
1033  */
igt_drop_caches_has(int drm_fd,uint64_t val)1034 bool igt_drop_caches_has(int drm_fd, uint64_t val)
1035 {
1036 	uint64_t mask;
1037 	int dir;
1038 
1039 	mask = 0;
1040 	dir = igt_debugfs_dir(drm_fd);
1041 	igt_sysfs_scanf(dir, "i915_gem_drop_caches", "0x%" PRIx64, &mask);
1042 	close(dir);
1043 
1044 	return (val & mask) == val;
1045 }
1046 
1047 /**
1048  * igt_drop_caches_set:
1049  * @val: bitmask for DROP_* values
1050  *
1051  * This calls the debugfs interface the drm/i915 GEM driver exposes to drop or
1052  * evict certain classes of gem buffer objects.
1053  */
igt_drop_caches_set(int drm_fd,uint64_t val)1054 void igt_drop_caches_set(int drm_fd, uint64_t val)
1055 {
1056 	int dir;
1057 
1058 	dir = igt_debugfs_dir(drm_fd);
1059 	igt_assert(igt_sysfs_printf(dir, "i915_gem_drop_caches",
1060 				    "0x%" PRIx64, val) > 0);
1061 	close(dir);
1062 }
1063 
1064 /*
1065  * Prefault control
1066  */
1067 
1068 #define PREFAULT_DEBUGFS "/sys/module/i915/parameters/prefault_disable"
igt_prefault_control(bool enable)1069 static void igt_prefault_control(bool enable)
1070 {
1071 	const char *name = PREFAULT_DEBUGFS;
1072 	int fd;
1073 	char buf[2] = {'Y', 'N'};
1074 	int index;
1075 
1076 	fd = open(name, O_RDWR);
1077 	igt_require(fd >= 0);
1078 
1079 	if (enable)
1080 		index = 1;
1081 	else
1082 		index = 0;
1083 
1084 	igt_require(write(fd, &buf[index], 1) == 1);
1085 
1086 	close(fd);
1087 }
1088 
enable_prefault_at_exit(int sig)1089 static void enable_prefault_at_exit(int sig)
1090 {
1091 	igt_enable_prefault();
1092 }
1093 
1094 /**
1095  * igt_disable_prefault:
1096  *
1097  * Disable prefaulting in certain gem ioctls through the debugfs interface. As
1098  * usual this installs an exit handler to clean up and re-enable prefaulting
1099  * even when the test exited abnormally.
1100  *
1101  * igt_enable_prefault() will enable normale operation again.
1102  */
igt_disable_prefault(void)1103 void igt_disable_prefault(void)
1104 {
1105 	igt_prefault_control(false);
1106 
1107 	igt_install_exit_handler(enable_prefault_at_exit);
1108 }
1109 
1110 /**
1111  * igt_enable_prefault:
1112  *
1113  * Enable prefault (again) through the debugfs interface.
1114  */
igt_enable_prefault(void)1115 void igt_enable_prefault(void)
1116 {
1117 	igt_prefault_control(true);
1118 }
1119 
get_object_count(int fd)1120 static int get_object_count(int fd)
1121 {
1122 	int dir, ret, scanned;
1123 
1124 	igt_drop_caches_set(fd,
1125 			    DROP_RETIRE | DROP_ACTIVE | DROP_IDLE | DROP_FREED);
1126 
1127 	dir = igt_debugfs_dir(fd);
1128 	scanned = igt_sysfs_scanf(dir, "i915_gem_objects",
1129 				  "%i objects", &ret);
1130 	igt_assert_eq(scanned, 1);
1131 	close(dir);
1132 
1133 	return ret;
1134 }
1135 
1136 /**
1137  * igt_get_stable_obj_count:
1138  * @driver: fd to drm/i915 GEM driver
1139  *
1140  * This puts the driver into a stable (quiescent) state and then returns the
1141  * current number of gem buffer objects as reported in the i915_gem_objects
1142  * debugFS interface.
1143  */
igt_get_stable_obj_count(int driver)1144 int igt_get_stable_obj_count(int driver)
1145 {
1146 	int obj_count;
1147 	gem_quiescent_gpu(driver);
1148 	obj_count = get_object_count(driver);
1149 	/* The test relies on the system being in the same state before and
1150 	 * after the test so any difference in the object count is a result of
1151 	 * leaks during the test. */
1152 	return obj_count;
1153 }
1154 
__igt_debugfs_dump(int device,const char * filename,int level)1155 void __igt_debugfs_dump(int device, const char *filename, int level)
1156 {
1157 	char *contents;
1158 	int dir;
1159 
1160 	dir = igt_debugfs_dir(device);
1161 	contents = igt_sysfs_get(dir, filename);
1162 	close(dir);
1163 
1164 	igt_log(IGT_LOG_DOMAIN, level, "%s:\n%s\n", filename, contents);
1165 	free(contents);
1166 }
1167