[PATCH i-g-t 05/10] lib/igt_kmod: Add igt_kmod_bind()

Lucas De Marchi lucas.demarchi at intel.com
Thu Apr 10 22:48:34 UTC 2025


Like the unbind/rebind, this can be easily implemented by the
driver_set_device() helper.

Signed-off-by: Lucas De Marchi <lucas.demarchi at intel.com>
---
 lib/igt_kmod.c | 32 +++++++++++++++++++++++++++++---
 lib/igt_kmod.h |  2 ++
 2 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/lib/igt_kmod.c b/lib/igt_kmod.c
index 3f5f93f37..14b6b957a 100644
--- a/lib/igt_kmod.c
+++ b/lib/igt_kmod.c
@@ -598,9 +598,7 @@ int __igt_intel_driver_unload(char **who, const char *driver)
 }
 
 /**
- * igt_kmod_unbind:
- *
- * Unbind driver from devices. Currently supports only PCI bus
+ * igt_kmod_unbind: Unbind driver from devices. Currently supports only PCI bus
  * @mod_name: name of the module to unbind
  * @pci_device: if provided, unbind only this device, otherwise unbind all devices
  */
@@ -639,6 +637,34 @@ int igt_kmod_unbind(const char *mod_name, const char *pci_device)
 	return 0;
 }
 
+/**
+ * igt_kmod_bind: Bind driver to device
+ * @mod_name: name of the module to rebind
+ * @pci_device: device to bind
+ *
+ * Module should already be loaded
+ */
+int igt_kmod_bind(const char *mod_name, const char *pci_device)
+{
+	char path[PATH_MAX];
+	int dirlen, dirfd;
+	int ret;
+
+	dirlen = snprintf(path, sizeof(path), "/sys/module/%s/drivers/pci:%s/",
+			  mod_name, mod_name);
+	igt_assert(dirlen < sizeof(path));
+
+	dirfd = open(path, O_RDONLY | O_CLOEXEC);
+	if (dirfd < 0)
+		return dirfd;
+
+	ret = igt_sysfs_set(dirfd, "bind", pci_device);
+
+	close(dirfd);
+
+	return ret;
+}
+
 /**
  * igt_intel_driver_unload:
  *
diff --git a/lib/igt_kmod.h b/lib/igt_kmod.h
index 170a8df9d..e3a4ce671 100644
--- a/lib/igt_kmod.h
+++ b/lib/igt_kmod.h
@@ -37,6 +37,8 @@ int igt_kmod_load(const char *mod_name, const char *opts);
 int igt_kmod_unload(const char *mod_name);
 
 int igt_kmod_unbind(const char *mod_name, const char *pci_device);
+__attribute__((nonnull)) int igt_kmod_bind(const char *mod_name,
+					   const char *pci_device);
 
 int igt_audio_driver_unload(char **whom);
 

-- 
2.49.0



More information about the igt-dev mailing list