[igt-dev] [i-g-t 02/20] lib: Cache xe_device at driver open/close level

Bhanuprakash Modem bhanuprakash.modem at intel.com
Tue May 16 16:50:40 UTC 2023


Instead of caching the xe_device struct at subtest level, cache
just after opening the driver. And remove from the cache just
before closing the driver.

V2: - Place the logic in correct helpers (Zbigniew)

Signed-off-by: Bhanuprakash Modem <bhanuprakash.modem at intel.com>
Reviewed-by: Kamil Konieczny <kamil.konieczny at linux.intel.com>
---
 lib/drmtest.c   | 24 +++++++++++++++++++++---
 lib/igt_types.c |  6 ++++++
 2 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/lib/drmtest.c b/lib/drmtest.c
index 41ad2694c..cfcb630ed 100644
--- a/lib/drmtest.c
+++ b/lib/drmtest.c
@@ -64,6 +64,7 @@
 #include "intel_reg.h"
 #include "ioctl_wrappers.h"
 #include "igt_dummyload.h"
+#include "xe/xe_query.h"
 
 /**
  * SECTION:drmtest
@@ -465,9 +466,14 @@ int __drm_open_driver_another(int idx, int chipset)
 		fd = __open_driver("/dev/dri/card", 0, chipset, idx);
 	}
 
-	if (fd >= 0)
+	if (fd >= 0) {
 		_set_opened_fd(idx, fd);
 
+		/* Cache xe_device struct. */
+		if (is_xe_device(fd))
+			xe_device_get(fd);
+	}
+
 	return fd;
 }
 
@@ -491,6 +497,8 @@ int __drm_open_driver(int chipset)
 
 int __drm_open_driver_render(int chipset)
 {
+	int fd;
+
 	if (chipset != DRIVER_VGEM && igt_device_filter_count() > 0) {
 		struct igt_device_card card;
 		bool found;
@@ -500,10 +508,16 @@ int __drm_open_driver_render(int chipset)
 		if (!found || !strlen(card.render))
 			return -1;
 
-		return __open_driver_exact(card.render, chipset);
+		fd = __open_driver_exact(card.render, chipset);
+	} else {
+		fd = __open_driver("/dev/dri/renderD", 128, chipset, 0);
 	}
 
-	return __open_driver("/dev/dri/renderD", 128, chipset, 0);
+	/* Cache xe_device struct. */
+	if (fd >= 0 && is_xe_device(fd))
+		xe_device_get(fd);
+
+	return fd;
 }
 
 static int at_exit_drm_fd = -1;
@@ -622,6 +636,10 @@ int drm_close_driver(int fd)
 		return -1;
 	}
 
+	/* Remove xe_device from cache. */
+	if (is_xe_device(fd))
+		xe_device_put(fd);
+
 	return close(fd);
 }
 
diff --git a/lib/igt_types.c b/lib/igt_types.c
index 392f30fca..d5d444889 100644
--- a/lib/igt_types.c
+++ b/lib/igt_types.c
@@ -5,13 +5,19 @@
 
 #include <unistd.h>
 
+#include "drmtest.h"
 #include "igt_types.h"
+#include "xe/xe_query.h"
 
 void igt_cleanup_fd(volatile int *fd)
 {
 	if (!fd || *fd < 0)
 		return;
 
+	/* Remove xe_device from cache. */
+	if (is_xe_device(*fd))
+		xe_device_put(*fd);
+
 	close(*fd);
 	*fd = -1;
 }
-- 
2.40.0



More information about the igt-dev mailing list