From patchwork Wed Oct 26 13:54:39 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marius Vlad X-Patchwork-Id: 9397331 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id ADB52600BA for ; Wed, 26 Oct 2016 13:48:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8DC9929BA6 for ; Wed, 26 Oct 2016 13:48:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 829BE29BAF; Wed, 26 Oct 2016 13:48:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id BCD4329BAD for ; Wed, 26 Oct 2016 13:48:32 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 0C3FF6E8A1; Wed, 26 Oct 2016 13:48:32 +0000 (UTC) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by gabe.freedesktop.org (Postfix) with ESMTPS id 9D9FE6E8A1 for ; Wed, 26 Oct 2016 13:48:30 +0000 (UTC) Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga102.jf.intel.com with ESMTP; 26 Oct 2016 06:48:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.31,550,1473145200"; d="scan'208";a="184061452" Received: from mcvlad-wk.rb.intel.com ([10.237.105.57]) by fmsmga004.fm.intel.com with ESMTP; 26 Oct 2016 06:48:28 -0700 From: Marius Vlad To: intel-gfx@lists.freedesktop.org Date: Wed, 26 Oct 2016 16:54:39 +0300 Message-Id: <20161026135441.7349-2-marius.c.vlad@intel.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20161026135441.7349-1-marius.c.vlad@intel.com> References: <20161026135441.7349-1-marius.c.vlad@intel.com> Subject: [Intel-gfx] [PATCH i-g-t 1/3 v3] lib/{igt_sysfs, igt_aux}: Make available to other users kick_fbcon() (unbind_fbcon()), and added helpers to igt_aux. X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" X-Virus-Scanned: ClamAV using ClamSMTP Previously under unbind_fbcon(), to disable/enable framebuffer console. lib/igt_aux: Added helpers to help convert sh scripts to C version. libkmod and procps interface. v3: - return -errno (igt_pkill()) in case of failure (Cris Wilson) - return bool for igt_kmod_is_loaded(), replaced strncasecmp with strncmp (Chris Wilson) - use igt_debug() instead of igt_info() for igt_kmod_load()/ igt_kmod_unload() and return -err directly from libkmod (Chris Wilson) v2: - Renamed libkmod helpers (Chris Wilson) - Removed SIGTERM/SIGKILL case where we repeatedly tried to terminate the process: just call kill(2) once (Chris Wilson) - Removed redundant check in igt_kmod_unload(), igt_module_in_use() (Chris Wilson) - Pass flags to igt_kmod_unload() from the caller (Chris Wilson) - Removed useless function igt_kill() which acts just as kill(2) (Chris Wilson) Signed-off-by: Marius Vlad --- configure.ac | 2 + lib/Makefile.am | 2 + lib/igt_aux.c | 177 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/igt_aux.h | 6 ++ lib/igt_gvt.c | 43 +------------- lib/igt_sysfs.c | 54 +++++++++++++++++ lib/igt_sysfs.h | 2 + 7 files changed, 245 insertions(+), 41 deletions(-) diff --git a/configure.ac b/configure.ac index 735cfd5..2c6e49d 100644 --- a/configure.ac +++ b/configure.ac @@ -121,6 +121,8 @@ AC_SUBST(ASSEMBLER_WARN_CFLAGS) PKG_CHECK_MODULES(DRM, [libdrm]) PKG_CHECK_MODULES(PCIACCESS, [pciaccess >= 0.10]) +PKG_CHECK_MODULES(KMOD, [libkmod]) +PKG_CHECK_MODULES(PROCPS, [libprocps]) case "$target_cpu" in x86*|i?86) diff --git a/lib/Makefile.am b/lib/Makefile.am index 4c0893d..e1737bd 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -34,6 +34,8 @@ AM_CFLAGS += $(CAIRO_CFLAGS) libintel_tools_la_LIBADD = \ $(DRM_LIBS) \ $(PCIACCESS_LIBS) \ + $(PROCPS_LIBS) \ + $(KMOD_LIBS) \ $(CAIRO_LIBS) \ $(LIBUDEV_LIBS) \ $(LIBUNWIND_LIBS) \ diff --git a/lib/igt_aux.c b/lib/igt_aux.c index 421f6d4..d43ec8c 100644 --- a/lib/igt_aux.c +++ b/lib/igt_aux.c @@ -51,6 +51,9 @@ #include #include +#include +#include + #include "drmtest.h" #include "i915_drm.h" #include "intel_chipset.h" @@ -1193,6 +1196,180 @@ void igt_set_module_param_int(const char *name, int val) igt_set_module_param(name, str); } +/** + * igt_pkill: + * @sig: Signal to send + * @name: Name of process in the form found in /proc/pid/comm (limited to 15 + * chars) + * @returns: 0 in case the process is not found running or the signal has been + * sent successfully or -errno otherwise. + * + * This function sends the signal @sig for a process found in process table + * with name @comm. + */ +int +igt_pkill(int sig, const char *comm) +{ + PROCTAB *proc; + proc_t *proc_info; + int err = 0; + + proc = openproc(PROC_FILLCOM | PROC_FILLSTAT | PROC_FILLARG); + igt_assert(proc != NULL); + + while ((proc_info = readproc(proc, NULL))) { + if (!strncasecmp(proc_info->cmd, comm, sizeof(proc_info->cmd))) { + + if (kill(proc_info->tid, sig) < 0) + err = -errno; + + freeproc(proc_info); + break; + } + freeproc(proc_info); + } + + closeproc(proc); + return err; +} + +/** + * igt_kmod_is_loaded: + * @mod_name: The name of the module. + * @returns: True in case the module has been found or false otherwise. + * + * Function to check the existance of module @mod_name in list of loaded kernel + * modules. + * + */ +bool +igt_kmod_is_loaded(const char *mod_name) +{ + struct kmod_list *mod, *list; + struct kmod_ctx *ctx; + bool ret = false; + + ctx = kmod_new(NULL, NULL); + igt_assert(ctx != NULL); + + if (kmod_module_new_from_loaded(ctx, &list) < 0) { + goto out; + } + + kmod_list_foreach(mod, list) { + struct kmod_module *kmod = kmod_module_get_module(mod); + const char *kmod_name = kmod_module_get_name(kmod); + + if (!strncmp(kmod_name, mod_name, strlen(kmod_name))) { + kmod_module_unref(kmod); + ret = true; + break; + } + kmod_module_unref(kmod); + } + kmod_module_unref_list(list); +out: + kmod_unref(ctx); + + return ret; +} + +/** + * igt_kmod_load: + * @mod_name: The name of the module + * @opts: Parameters for the module. NULL in case no parameters + * are to be passed, or a '\0' terminated string otherwise. + * @returns: 0 in case of success or -errno in case the module could not + * be loaded. + * + * This function loads a kernel module using the name specified in @mod_name. + * + * @Note: This functions doesn't automatically resolve other module + * dependencies so make make sure you load the dependencies module(s) before + * this one. + */ +int +igt_kmod_load(const char *mod_name, const char *opts) +{ + struct kmod_ctx *ctx; + struct kmod_module *kmod; + int err = 0; + + ctx = kmod_new(NULL, NULL); + igt_assert(ctx != NULL); + + err = kmod_module_new_from_name(ctx, mod_name, &kmod); + if (err < 0) { + goto out; + } + + err = kmod_module_insert_module(kmod, 0, opts); + if (err < 0) { + switch (err) { + case -EEXIST: + igt_debug("Module %s already inserted\n", + kmod_module_get_name(kmod)); + break; + case -ENOENT: + igt_debug("Unknown symbol in module %s or " + "unknown parameter\n", + kmod_module_get_name(kmod)); + break; + default: + igt_debug("Could not insert %s (%s)\n", + kmod_module_get_name(kmod), strerror(-err)); + break; + } + } +out: + kmod_module_unref(kmod); + kmod_unref(ctx); + + return -err ? err < 0 : err; +} + + +/** + * igt_kmod_unload: + * @mod_name: Module name. + * @flags: flags are passed directly to libkmod and can be: + * KMOD_REMOVE_FORCE or KMOD_REMOVE_NOWAIT. + * @returns: 0 in case of success or -errno otherwise. + * + * Removes the module @mod_name. + * + */ +int +igt_kmod_unload(const char *mod_name, unsigned int flags) +{ + struct kmod_ctx *ctx; + struct kmod_module *kmod; + int err; + + ctx = kmod_new(NULL, NULL); + igt_assert(ctx != NULL); + + err = kmod_module_new_from_name(ctx, mod_name, &kmod); + if (err < 0) { + igt_debug("Could not use module %s (%s)\n", mod_name, + strerror(-err)); + goto out; + } + + err = kmod_module_remove_module(kmod, flags); + if (err < 0) { + igt_debug("Could not remove module %s (%s)\n", mod_name, + strerror(-err)); + } + +out: + kmod_module_unref(kmod); + kmod_unref(ctx); + + return -err ? err < 0 : err; +} + + static struct igt_siglatency { timer_t timer; struct timespec target; diff --git a/lib/igt_aux.h b/lib/igt_aux.h index d30196b..df825e0 100644 --- a/lib/igt_aux.h +++ b/lib/igt_aux.h @@ -264,4 +264,10 @@ double igt_stop_siglatency(struct igt_mean *result); void igt_set_module_param(const char *name, const char *val); void igt_set_module_param_int(const char *name, int val); +int igt_pkill(int sig, const char *comm); + +bool igt_kmod_is_loaded(const char *mod_name); +int igt_kmod_load(const char *mod_name, const char *opts); +int igt_kmod_unload(const char *mod_name, unsigned int flags); + #endif /* IGT_AUX_H */ diff --git a/lib/igt_gvt.c b/lib/igt_gvt.c index 0f332d1..8bbf9bd 100644 --- a/lib/igt_gvt.c +++ b/lib/igt_gvt.c @@ -23,6 +23,7 @@ #include "igt.h" #include "igt_gvt.h" +#include "igt_sysfs.h" #include #include @@ -46,49 +47,9 @@ static bool is_gvt_enabled(void) return enabled; } -static void unbind_fbcon(void) -{ - char buf[128]; - const char *path = "/sys/class/vtconsole"; - DIR *dir; - struct dirent *vtcon; - - dir = opendir(path); - if (!dir) - return; - - while ((vtcon = readdir(dir))) { - int fd, len; - - if (strncmp(vtcon->d_name, "vtcon", 5)) - continue; - - sprintf(buf, "%s/%s/name", path, vtcon->d_name); - fd = open(buf, O_RDONLY); - if (fd < 0) - continue; - - len = read(fd, buf, sizeof(buf) - 1); - close(fd); - if (len >= 0) - buf[len] = '\0'; - - if (strstr(buf, "frame buffer device")) { - sprintf(buf, "%s/%s/bind", path, vtcon->d_name); - fd = open(buf, O_WRONLY); - if (fd != -1) { - igt_ignore_warn(write(fd, "1\n", 2)); - close(fd); - } - break; - } - } - closedir(dir); -} - static void unload_i915(void) { - unbind_fbcon(); + kick_fbcon(false); /* pkill alsact */ igt_ignore_warn(system("/sbin/modprobe -s -r i915")); diff --git a/lib/igt_sysfs.c b/lib/igt_sysfs.c index c19821d..64a95de 100644 --- a/lib/igt_sysfs.c +++ b/lib/igt_sysfs.c @@ -34,7 +34,11 @@ #include #include #include +#include +#include +#include +#include "igt_core.h" #include "igt_sysfs.h" /** @@ -392,3 +396,53 @@ bool igt_sysfs_set_boolean(int dir, const char *attr, bool value) { return igt_sysfs_printf(dir, attr, "%d", value) == 1; } + +/** + * kick_fbcon: + * @enable: boolean value + * + * This functions enables/disables the text console running on top of the + * framebuffer device. + */ +void kick_fbcon(bool enable) +{ + char buf[128]; + const char *path = "/sys/class/vtconsole"; + DIR *dir; + struct dirent *vtcon; + + dir = opendir(path); + if (!dir) + return; + + while ((vtcon = readdir(dir))) { + int fd, len; + + if (strncmp(vtcon->d_name, "vtcon", 5)) + continue; + + sprintf(buf, "%s/%s/name", path, vtcon->d_name); + fd = open(buf, O_RDONLY); + if (fd < 0) + continue; + + len = read(fd, buf, sizeof(buf) - 1); + close(fd); + if (len >= 0) + buf[len] = '\0'; + + if (strstr(buf, "frame buffer device")) { + sprintf(buf, "%s/%s/bind", path, vtcon->d_name); + fd = open(buf, O_WRONLY); + if (fd != -1) { + if (enable) + igt_ignore_warn(write(fd, "1\n", 2)); + else + igt_ignore_warn(write(fd, "0\n", 2)); + close(fd); + } + break; + } + } + closedir(dir); +} diff --git a/lib/igt_sysfs.h b/lib/igt_sysfs.h index 4820066..a141894 100644 --- a/lib/igt_sysfs.h +++ b/lib/igt_sysfs.h @@ -43,4 +43,6 @@ bool igt_sysfs_set_u32(int dir, const char *attr, uint32_t value); bool igt_sysfs_get_boolean(int dir, const char *attr); bool igt_sysfs_set_boolean(int dir, const char *attr, bool value); +void kick_fbcon(bool enable); + #endif /* __IGT_SYSFS_H__ */