igt-dev.lists.freedesktop.org archive mirror
 help / color / mirror / Atom feed
* [igt-dev] [PATCH i-g-t v2] lib/drmtest: Move open device to separate function
@ 2018-08-31 10:41 Katarzyna Dec
  2018-08-31 10:47 ` Chris Wilson
                   ` (7 more replies)
  0 siblings, 8 replies; 14+ messages in thread
From: Katarzyna Dec @ 2018-08-31 10:41 UTC (permalink / raw)
  To: igt-dev

While working on IGT code and during reviewes I've noticed that
it could be nice to have function that is opening particular device.
Let's move out conditions for opening device and rename __open_device
to __search_and_open() function.

v2: Refactored open_device even more by getting device name once and
returning fd for it. (Chris)

Signed-off-by: Katarzyna Dec <katarzyna.dec@intel.com>
Cc: Daniele Ceraolo Spurio <daniele.ceraolospurio@intel.com>
Cc: Chris Wilson <chris@chris-wilson.co.uk>
---
 lib/drmtest.c | 82 ++++++++++++++++++++++++++++-----------------------
 1 file changed, 45 insertions(+), 37 deletions(-)

diff --git a/lib/drmtest.c b/lib/drmtest.c
index fae6f86f..ca1669a1 100644
--- a/lib/drmtest.c
+++ b/lib/drmtest.c
@@ -218,38 +218,58 @@ static void modprobe_i915(const char *name)
 	igt_i915_driver_load(NULL);
 }
 
-static int __open_device(const char *base, int offset, unsigned int chipset)
+static const struct module {
+		unsigned int bit;
+		const char *module;
+		void (*modprobe)(const char *name);
+	} modules[] = {
+		{ DRIVER_AMDGPU, "amdgpu" },
+		{ DRIVER_INTEL, "i915", modprobe_i915 },
+		{ DRIVER_VC4, "vc4" },
+		{ DRIVER_VGEM, "vgem" },
+		{ DRIVER_VIRTIO, "virtio-gpu" },
+		{}
+	};
+
+static int open_device(const char *name, unsigned int chipset)
 {
-	for (int i = 0; i < 16; i++) {
-		char name[80];
-		int fd;
+	int fd;
+	char dev_name[16] = "";
+	int chip = DRIVER_ANY;
 
-		sprintf(name, "%s%u", base, i + offset);
-		fd = open(name, O_RDWR);
-		if (fd == -1)
-			continue;
+	fd = open(name, O_RDWR);
+	if (fd == -1)
+		return -1;
 
-		if (chipset & DRIVER_INTEL && is_i915_device(fd) &&
-		    has_known_intel_chipset(fd))
-			return fd;
+	if (__get_drm_device_name(fd, dev_name) == -1)
+		return -1;
 
-		if (chipset & DRIVER_VC4 && is_vc4_device(fd))
-			return fd;
+	for (const struct module *m = modules; m->module; m++) {
+		if (strcmp(m->module, dev_name) == 0) {
+			chip = m->bit;
+			break;
+		}
+	}
+	if (chipset & chip)
+		return fd;
 
-		if (chipset & DRIVER_VGEM && is_vgem_device(fd))
-			return fd;
+	close(fd);
 
-		if (chipset & DRIVER_VIRTIO && is_virtio_device(fd))
-			return fd;
+	return -1;
+}
 
-		if (chipset & DRIVER_AMDGPU && is_amd_device(fd))
-			return fd;
+static int __search_and_open(const char *base, int offset, unsigned int chipset)
+{
+	for (int i = 0; i < 16; i++) {
+		char name[80];
+		int fd;
 
-		/* Only VGEM-specific tests should be run on VGEM */
-		if (chipset == DRIVER_ANY && !is_vgem_device(fd))
+		sprintf(name, "%s%u", base, i + offset);
+		fd = open_device(name, chipset);
+		if (fd == -1)
+			continue;
+		else
 			return fd;
-
-		close(fd);
 	}
 
 	return -1;
@@ -258,21 +278,9 @@ static int __open_device(const char *base, int offset, unsigned int chipset)
 static int __open_driver(const char *base, int offset, unsigned int chipset)
 {
 	static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
-	static const struct module {
-		unsigned int bit;
-		const char *module;
-		void (*modprobe)(const char *name);
-	} modules[] = {
-		{ DRIVER_AMDGPU, "amdgpu" },
-		{ DRIVER_INTEL, "i915", modprobe_i915 },
-		{ DRIVER_VC4, "vc4" },
-		{ DRIVER_VGEM, "vgem" },
-		{ DRIVER_VIRTIO, "virtio-gpu" },
-		{}
-	};
 	int fd;
 
-	fd = __open_device(base, offset, chipset);
+	fd = __search_and_open(base, offset, chipset);
 	if (fd != -1)
 		return fd;
 
@@ -287,7 +295,7 @@ static int __open_driver(const char *base, int offset, unsigned int chipset)
 	}
 	pthread_mutex_unlock(&mutex);
 
-	return __open_device(base, offset, chipset);
+	return __search_and_open(base, offset, chipset);
 }
 
 /**
-- 
2.17.1

_______________________________________________
igt-dev mailing list
igt-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/igt-dev

^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2018-08-31 18:04 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-08-31 10:41 [igt-dev] [PATCH i-g-t v2] lib/drmtest: Move open device to separate function Katarzyna Dec
2018-08-31 10:47 ` Chris Wilson
2018-08-31 11:02   ` Katarzyna Dec
2018-08-31 11:00 ` [igt-dev] [PATCH i-g-t v3] " Katarzyna Dec
2018-08-31 11:51   ` Petri Latvala
2018-08-31 11:54     ` Katarzyna Dec
2018-08-31 11:59     ` Chris Wilson
2018-08-31 12:48   ` [igt-dev] [PATCH i-g-t v4] " Katarzyna Dec
2018-08-31 11:18 ` [igt-dev] ✗ Fi.CI.BAT: failure for lib/drmtest: Move open device to separate function (rev2) Patchwork
2018-08-31 12:03 ` [igt-dev] ✗ Fi.CI.BAT: failure for lib/drmtest: Move open device to separate function (rev3) Patchwork
2018-08-31 13:20 ` [igt-dev] ✗ Fi.CI.BAT: failure for lib/drmtest: Move open device to separate function (rev4) Patchwork
2018-08-31 14:19 ` [igt-dev] ✗ Fi.CI.IGT: failure for lib/drmtest: Move open device to separate function (rev2) Patchwork
2018-08-31 15:29 ` [igt-dev] ✗ Fi.CI.IGT: failure for lib/drmtest: Move open device to separate function (rev3) Patchwork
2018-08-31 18:04 ` [igt-dev] ✗ Fi.CI.IGT: failure for lib/drmtest: Move open device to separate function (rev4) Patchwork

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).