Igt-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Anna Karas <anna.karas@intel.com>
To: igt-dev@lists.freedesktop.org
Cc: Joonas Lahtinen <joonas.lahtinen@intel.com>,
	Maarten Lankhorst <maarten.lankhorst@intel.com>
Subject: [igt-dev] [RFC 1/2] lib/xe_gpu: Introduce xe_gpu library
Date: Thu, 20 Apr 2023 21:14:53 +0200	[thread overview]
Message-ID: <20230420191454.497237-2-anna.karas@intel.com> (raw)
In-Reply-To: <20230420191454.497237-1-anna.karas@intel.com>

Add helpers for requiring the XE driver, reopening drm fd and restoring
engine properties between tests.
This lib is a direct port of gem.c from i915.

Reference: Jira VLK-46235
Signed-off-by: Anna Karas <anna.karas@intel.com>
---
 lib/meson.build |   1 +
 lib/xe/xe_gpu.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++++
 lib/xe/xe_gpu.h |  19 +++++++
 3 files changed, 156 insertions(+)
 create mode 100644 lib/xe/xe_gpu.c
 create mode 100644 lib/xe/xe_gpu.h

diff --git a/lib/meson.build b/lib/meson.build
index b21c252b..b33f8ae6 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -100,6 +100,7 @@ lib_sources = [
 	'igt_msm.c',
 	'igt_dsc.c',
 	'xe/xe_compute.c',
+	'xe/xe_gpu.c',
 	'xe/xe_compute_square_kernels.c',
 	'xe/xe_ioctl.c',
 	'xe/xe_query.c',
diff --git a/lib/xe/xe_gpu.c b/lib/xe/xe_gpu.c
new file mode 100644
index 00000000..7abd8057
--- /dev/null
+++ b/lib/xe/xe_gpu.c
@@ -0,0 +1,136 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ *
+ * Authors:
+ *    Anna Karas <anna.karas@intel.com>
+ */
+
+#include <dirent.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+
+#include "xe/xe_gpu.h"
+#include "igt_sysfs.h"
+#include "igt.h"
+
+
+/*
+ * Resets all engine properties to defaults prior to the start of a test.
+ */
+static void __restore_defaults(int engine)
+{
+	struct dirent *de;
+	int defaults;
+	DIR *dir;
+
+	defaults = openat(engine, ".defaults", O_RDONLY);
+	if (defaults < 0)
+		return;
+
+	dir = fdopendir(defaults);
+	if (!dir) {
+		close(defaults);
+		return;
+	}
+
+	while ((de = readdir(dir))) {
+		char buf[256];
+		int fd, len;
+
+		if (*de->d_name == '.')
+			continue;
+
+		fd = openat(defaults, de->d_name, O_RDONLY);
+		if (fd < 0)
+			continue;
+
+		len = read(fd, buf, sizeof(buf));
+		close(fd);
+		if (len < 0)
+			continue;
+
+		fd = openat(engine, de->d_name, O_WRONLY);
+		if (fd < 0)
+			continue;
+
+		write(fd, buf, len);
+		close(fd);
+	}
+
+	closedir(dir);
+}
+
+static void restore_defaults(int fd)
+{
+	struct dirent *de;
+	int engines;
+	DIR *dir;
+	int sys;
+
+	sys = igt_sysfs_open(fd);
+	if (sys < 0)
+		return;
+
+	engines = openat(sys, "engine", O_RDONLY);
+	if (engines < 0)
+		goto close_sys;
+
+	dir = fdopendir(engines);
+	if (!dir) {
+		close(engines);
+		goto close_sys;
+	}
+
+	while ((de = readdir(dir))) {
+		int engine;
+
+		if (*de->d_name == '.')
+			continue;
+
+		engine = openat(engines, de->d_name, O_RDONLY);
+		if (engine < 0)
+			continue;
+
+		__restore_defaults(engine);
+		close(engine);
+	}
+
+	closedir(dir);
+
+close_sys:
+	close(sys);
+}
+
+/**
+ * xe_reopen_driver:
+ * @fd: re-open the xe drm file descriptor
+ *
+ * Re-opens the drm fd which is useful in instances where a clean default
+ * context is needed.
+ */
+int xe_reopen_driver(int fd)
+{
+	char path[256];
+
+	snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
+	fd = open(path, O_RDWR);
+	igt_assert_fd(fd);
+
+	return fd;
+}
+
+/**
+ * xe_require_gpu:
+ * @fd: the xe drm file descriptor
+ *
+ * Helper to be used prior to the start of a tests, useful in instances
+ * where a clean default context is needed.
+ */
+void xe_require_gpu(int fd)
+{
+	igt_require_xe(fd);
+	fd = xe_reopen_driver(fd);
+	restore_defaults(fd);
+	close(fd);
+}
diff --git a/lib/xe/xe_gpu.h b/lib/xe/xe_gpu.h
new file mode 100644
index 00000000..020040ac
--- /dev/null
+++ b/lib/xe/xe_gpu.h
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ *
+ * Authors:
+ *    Anna Karas <anna.karas@intel.com>
+ */
+
+#ifndef XE_GPU_H
+#define XE_GPU_H
+
+#include <stdint.h>
+#include <xe_drm.h>
+
+int xe_reopen_driver(int fd);
+void xe_require_gpu(int fd);
+
+#endif	/* XE_GPU_H */
+
-- 
2.25.1

---------------------------------------------------------------------
Intel Technology Poland sp. z o.o.
ul. Slowackiego 173 | 80-298 Gdansk | Sad Rejonowy Gdansk Polnoc | VII Wydzial Gospodarczy Krajowego Rejestru Sadowego - KRS 101882 | NIP 957-07-52-316 | Kapital zakladowy 200.000 PLN.
Spolka oswiadcza, ze posiada status duzego przedsiebiorcy w rozumieniu ustawy z dnia 8 marca 2013 r. o przeciwdzialaniu nadmiernym opoznieniom w transakcjach handlowych.

Ta wiadomosc wraz z zalacznikami jest przeznaczona dla okreslonego adresata i moze zawierac informacje poufne. W razie przypadkowego otrzymania tej wiadomosci, prosimy o powiadomienie nadawcy oraz trwale jej usuniecie; jakiekolwiek przegladanie lub rozpowszechnianie jest zabronione.
This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). If you are not the intended recipient, please contact the sender and delete all copies; any review or distribution by others is strictly prohibited.

  reply	other threads:[~2023-04-20 19:15 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-20 19:14 [igt-dev] [RFC 0/2] lib/xe_gpu: Introduce xe_gpu library Anna Karas
2023-04-20 19:14 ` Anna Karas [this message]
2023-04-21  7:56   ` [igt-dev] [RFC 1/2] " Zbigniew Kempczyński
2023-05-15  9:51   ` Manszewski, Christoph
2023-05-15 18:01     ` Karas, Anna
2023-04-20 19:14 ` [igt-dev] [RFC 2/2] tests/xe: Use xe_require_gpu() in existing tests Anna Karas
2023-04-20 20:38 ` [igt-dev] [RFC 0/2] lib/xe_gpu: Introduce xe_gpu library Souza, Jose
2023-04-24 11:38   ` Karas, Anna
2023-04-25  3:41     ` Zbigniew Kempczyński
2023-04-20 20:58 ` [igt-dev] ✓ Fi.CI.BAT: success for " Patchwork
2023-04-21  5:28 ` [igt-dev] ✓ Fi.CI.IGT: " Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230420191454.497237-2-anna.karas@intel.com \
    --to=anna.karas@intel.com \
    --cc=igt-dev@lists.freedesktop.org \
    --cc=joonas.lahtinen@intel.com \
    --cc=maarten.lankhorst@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox