All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tvrtko Ursulin <tursulin@ursulin.net>
To: igt-dev@lists.freedesktop.org
Cc: intel-gfx@lists.freedesktop.org
Subject: [PATCH i-g-t v6 2/4] lib/igt_pm: Find HDA device when attempting to enable runtime PM
Date: Tue, 24 Jul 2018 11:29:31 +0100	[thread overview]
Message-ID: <20180724102931.25352-1-tvrtko.ursulin@linux.intel.com> (raw)
In-Reply-To: <20180724100852.24896-1-tvrtko.ursulin@linux.intel.com>

From: Tvrtko Ursulin <tvrtko.ursulin@intel.com>

HDA audio device can be present at various PCI paths on different systems
which the existing code did not account for.

Furthermore the failure to enable runtime PM was silent leaving callers
in the dark.

Improve it by auto-locating the PCI path and logging a warning when
something is not as expected.

v2:
 * If there is no audio hw/driver there is no failure.

v3:
 * Comment.
 * Skip non-symlinks.
 * Free path on failure and restore.
 * Simplify with asprintf. (Chris Wilson)

v4:
 * Find snd_hda_intel instance tied with an Intel device.

v5:
 * Fix memory leak and silence Valgrind warning.

v6:
 * Fix error out logic.

Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
---
 lib/igt_pm.c | 93 ++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 79 insertions(+), 14 deletions(-)

diff --git a/lib/igt_pm.c b/lib/igt_pm.c
index 6f3b0a2d897d..94d4dd2d3c37 100644
--- a/lib/igt_pm.c
+++ b/lib/igt_pm.c
@@ -33,6 +33,7 @@
 #include <unistd.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <dirent.h>
 
 #include "drmtest.h"
 #include "igt_pm.h"
@@ -64,6 +65,7 @@ enum {
 #define MAX_POLICY_STRLEN	strlen(MAX_PERFORMANCE_STR)
 
 static char __igt_pm_audio_runtime_power_save[64];
+static char * __igt_pm_audio_runtime_control_path;
 static char __igt_pm_audio_runtime_control[64];
 
 static int __igt_pm_audio_restore_runtime_pm(void)
@@ -86,7 +88,7 @@ static int __igt_pm_audio_restore_runtime_pm(void)
 
 	close(fd);
 
-	fd = open("/sys/bus/pci/devices/0000:00:03.0/power/control", O_WRONLY);
+	fd = open(__igt_pm_audio_runtime_control_path, O_WRONLY);
 	if (fd < 0)
 		return errno;
 
@@ -100,6 +102,8 @@ static int __igt_pm_audio_restore_runtime_pm(void)
 	close(fd);
 
 	__igt_pm_audio_runtime_power_save[0] = 0;
+	free(__igt_pm_audio_runtime_control_path);
+	__igt_pm_audio_runtime_control_path = NULL;
 
 	return 0;
 }
@@ -130,36 +134,97 @@ static void strchomp(char *str)
  */
 void igt_pm_enable_audio_runtime_pm(void)
 {
+	char *path = NULL;
+	struct dirent *de;
+	DIR *dir;
 	int fd;
 
 	/* Check if already enabled. */
 	if (__igt_pm_audio_runtime_power_save[0])
 		return;
 
-	fd = open("/sys/module/snd_hda_intel/parameters/power_save", O_RDWR);
-	if (fd >= 0) {
-		igt_assert(read(fd, __igt_pm_audio_runtime_power_save,
-				sizeof(__igt_pm_audio_runtime_power_save)) > 0);
-		strchomp(__igt_pm_audio_runtime_power_save);
-		igt_install_exit_handler(__igt_pm_audio_runtime_exit_handler);
-		igt_assert_eq(write(fd, "1\n", 2), 2);
+	dir = opendir("/sys/class/sound");
+	if (!dir)
+		return;
+
+	/* Find PCI device claimed by snd_hda_intel and tied to i915. */
+	while ((de = readdir(dir))) {
+		const char *match = "hwC";
+		char buf[32] = { }; /* for Valgrind */
+		char *tmp;
+		int ret;
+
+		if (de->d_type != DT_LNK ||
+		    strncmp(de->d_name, match, strlen(match)))
+			continue;
+
+		igt_assert(asprintf(&tmp,
+				    "/sys/class/sound/%s/vendor_name",
+				    de->d_name));
+
+		fd = open(tmp, O_RDONLY);
+		free(tmp);
+		igt_assert_fd(fd);
+		ret = read(fd, buf, sizeof(buf));
 		close(fd);
+		igt_assert(ret > 0);
+		strchomp(buf);
+
+		/* Realtek and similar devices are not what we are after. */
+		if (strcmp(buf, "Intel"))
+			continue;
+
+		igt_assert(asprintf(&path,
+				    "/sys/class/sound/%s/device/device/power/control",
+				    de->d_name));
+
+		igt_debug("Audio device path is %s\n", path);
+
+		break;
 	}
-	fd = open("/sys/bus/pci/devices/0000:00:03.0/power/control", O_RDWR);
-	if (fd >= 0) {
-		igt_assert(read(fd, __igt_pm_audio_runtime_control,
-				sizeof(__igt_pm_audio_runtime_control)) > 0);
-		strchomp(__igt_pm_audio_runtime_control);
-		igt_assert_eq(write(fd, "auto\n", 5), 5);
+
+	fd = open("/sys/module/snd_hda_intel/parameters/power_save", O_RDWR);
+	if (fd < 0)
+		return;
+
+	/* snd_hda_intel loaded but no path found is an error. */
+	if (!path) {
 		close(fd);
+		errno = ESRCH;
+		goto err;
 	}
 
+	igt_assert(read(fd, __igt_pm_audio_runtime_power_save,
+			sizeof(__igt_pm_audio_runtime_power_save)) > 0);
+	strchomp(__igt_pm_audio_runtime_power_save);
+	igt_install_exit_handler(__igt_pm_audio_runtime_exit_handler);
+	igt_assert_eq(write(fd, "1\n", 2), 2);
+	close(fd);
+
+	fd = open(path, O_RDWR);
+	if (fd < 0)
+		goto err;
+
+	igt_assert(read(fd, __igt_pm_audio_runtime_control,
+			sizeof(__igt_pm_audio_runtime_control)) > 0);
+	strchomp(__igt_pm_audio_runtime_control);
+	igt_assert_eq(write(fd, "auto\n", 5), 5);
+	close(fd);
+
+	__igt_pm_audio_runtime_control_path = path;
+
 	igt_debug("Saved audio power management as '%s' and '%s'\n",
 		  __igt_pm_audio_runtime_power_save,
 		  __igt_pm_audio_runtime_control);
 
 	/* Give some time for it to react. */
 	sleep(1);
+
+	return;
+
+err:
+	igt_warn("Failed to enable audio runtime PM! (%d)", errno);
+	free(path);
 }
 
 /**
-- 
2.17.1

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

WARNING: multiple messages have this Message-ID (diff)
From: Tvrtko Ursulin <tursulin@ursulin.net>
To: igt-dev@lists.freedesktop.org
Cc: intel-gfx@lists.freedesktop.org
Subject: [Intel-gfx] [PATCH i-g-t v6 2/4] lib/igt_pm: Find HDA device when attempting to enable runtime PM
Date: Tue, 24 Jul 2018 11:29:31 +0100	[thread overview]
Message-ID: <20180724102931.25352-1-tvrtko.ursulin@linux.intel.com> (raw)
In-Reply-To: <20180724100852.24896-1-tvrtko.ursulin@linux.intel.com>

From: Tvrtko Ursulin <tvrtko.ursulin@intel.com>

HDA audio device can be present at various PCI paths on different systems
which the existing code did not account for.

Furthermore the failure to enable runtime PM was silent leaving callers
in the dark.

Improve it by auto-locating the PCI path and logging a warning when
something is not as expected.

v2:
 * If there is no audio hw/driver there is no failure.

v3:
 * Comment.
 * Skip non-symlinks.
 * Free path on failure and restore.
 * Simplify with asprintf. (Chris Wilson)

v4:
 * Find snd_hda_intel instance tied with an Intel device.

v5:
 * Fix memory leak and silence Valgrind warning.

v6:
 * Fix error out logic.

Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
---
 lib/igt_pm.c | 93 ++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 79 insertions(+), 14 deletions(-)

diff --git a/lib/igt_pm.c b/lib/igt_pm.c
index 6f3b0a2d897d..94d4dd2d3c37 100644
--- a/lib/igt_pm.c
+++ b/lib/igt_pm.c
@@ -33,6 +33,7 @@
 #include <unistd.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <dirent.h>
 
 #include "drmtest.h"
 #include "igt_pm.h"
@@ -64,6 +65,7 @@ enum {
 #define MAX_POLICY_STRLEN	strlen(MAX_PERFORMANCE_STR)
 
 static char __igt_pm_audio_runtime_power_save[64];
+static char * __igt_pm_audio_runtime_control_path;
 static char __igt_pm_audio_runtime_control[64];
 
 static int __igt_pm_audio_restore_runtime_pm(void)
@@ -86,7 +88,7 @@ static int __igt_pm_audio_restore_runtime_pm(void)
 
 	close(fd);
 
-	fd = open("/sys/bus/pci/devices/0000:00:03.0/power/control", O_WRONLY);
+	fd = open(__igt_pm_audio_runtime_control_path, O_WRONLY);
 	if (fd < 0)
 		return errno;
 
@@ -100,6 +102,8 @@ static int __igt_pm_audio_restore_runtime_pm(void)
 	close(fd);
 
 	__igt_pm_audio_runtime_power_save[0] = 0;
+	free(__igt_pm_audio_runtime_control_path);
+	__igt_pm_audio_runtime_control_path = NULL;
 
 	return 0;
 }
@@ -130,36 +134,97 @@ static void strchomp(char *str)
  */
 void igt_pm_enable_audio_runtime_pm(void)
 {
+	char *path = NULL;
+	struct dirent *de;
+	DIR *dir;
 	int fd;
 
 	/* Check if already enabled. */
 	if (__igt_pm_audio_runtime_power_save[0])
 		return;
 
-	fd = open("/sys/module/snd_hda_intel/parameters/power_save", O_RDWR);
-	if (fd >= 0) {
-		igt_assert(read(fd, __igt_pm_audio_runtime_power_save,
-				sizeof(__igt_pm_audio_runtime_power_save)) > 0);
-		strchomp(__igt_pm_audio_runtime_power_save);
-		igt_install_exit_handler(__igt_pm_audio_runtime_exit_handler);
-		igt_assert_eq(write(fd, "1\n", 2), 2);
+	dir = opendir("/sys/class/sound");
+	if (!dir)
+		return;
+
+	/* Find PCI device claimed by snd_hda_intel and tied to i915. */
+	while ((de = readdir(dir))) {
+		const char *match = "hwC";
+		char buf[32] = { }; /* for Valgrind */
+		char *tmp;
+		int ret;
+
+		if (de->d_type != DT_LNK ||
+		    strncmp(de->d_name, match, strlen(match)))
+			continue;
+
+		igt_assert(asprintf(&tmp,
+				    "/sys/class/sound/%s/vendor_name",
+				    de->d_name));
+
+		fd = open(tmp, O_RDONLY);
+		free(tmp);
+		igt_assert_fd(fd);
+		ret = read(fd, buf, sizeof(buf));
 		close(fd);
+		igt_assert(ret > 0);
+		strchomp(buf);
+
+		/* Realtek and similar devices are not what we are after. */
+		if (strcmp(buf, "Intel"))
+			continue;
+
+		igt_assert(asprintf(&path,
+				    "/sys/class/sound/%s/device/device/power/control",
+				    de->d_name));
+
+		igt_debug("Audio device path is %s\n", path);
+
+		break;
 	}
-	fd = open("/sys/bus/pci/devices/0000:00:03.0/power/control", O_RDWR);
-	if (fd >= 0) {
-		igt_assert(read(fd, __igt_pm_audio_runtime_control,
-				sizeof(__igt_pm_audio_runtime_control)) > 0);
-		strchomp(__igt_pm_audio_runtime_control);
-		igt_assert_eq(write(fd, "auto\n", 5), 5);
+
+	fd = open("/sys/module/snd_hda_intel/parameters/power_save", O_RDWR);
+	if (fd < 0)
+		return;
+
+	/* snd_hda_intel loaded but no path found is an error. */
+	if (!path) {
 		close(fd);
+		errno = ESRCH;
+		goto err;
 	}
 
+	igt_assert(read(fd, __igt_pm_audio_runtime_power_save,
+			sizeof(__igt_pm_audio_runtime_power_save)) > 0);
+	strchomp(__igt_pm_audio_runtime_power_save);
+	igt_install_exit_handler(__igt_pm_audio_runtime_exit_handler);
+	igt_assert_eq(write(fd, "1\n", 2), 2);
+	close(fd);
+
+	fd = open(path, O_RDWR);
+	if (fd < 0)
+		goto err;
+
+	igt_assert(read(fd, __igt_pm_audio_runtime_control,
+			sizeof(__igt_pm_audio_runtime_control)) > 0);
+	strchomp(__igt_pm_audio_runtime_control);
+	igt_assert_eq(write(fd, "auto\n", 5), 5);
+	close(fd);
+
+	__igt_pm_audio_runtime_control_path = path;
+
 	igt_debug("Saved audio power management as '%s' and '%s'\n",
 		  __igt_pm_audio_runtime_power_save,
 		  __igt_pm_audio_runtime_control);
 
 	/* Give some time for it to react. */
 	sleep(1);
+
+	return;
+
+err:
+	igt_warn("Failed to enable audio runtime PM! (%d)", errno);
+	free(path);
 }
 
 /**
-- 
2.17.1

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

  reply	other threads:[~2018-07-24 10:29 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-23 11:46 [PATCH i-g-t 1/4] lib/igt_pm: Make exit handlers signal safe Tvrtko Ursulin
2018-07-23 11:46 ` [igt-dev] " Tvrtko Ursulin
2018-07-23 11:46 ` [PATCH i-g-t 2/4] lib/igt_pm: Find HDA device when attempting to enable runtime PM Tvrtko Ursulin
2018-07-23 11:46   ` [Intel-gfx] " Tvrtko Ursulin
2018-07-23 12:46   ` [PATCH i-g-t v2 " Tvrtko Ursulin
2018-07-23 12:46     ` [Intel-gfx] " Tvrtko Ursulin
2018-07-23 12:54     ` [igt-dev] " Chris Wilson
2018-07-23 12:54       ` [Intel-gfx] " Chris Wilson
2018-07-23 15:31       ` [PATCH i-g-t v3 " Tvrtko Ursulin
2018-07-23 15:31         ` [igt-dev] " Tvrtko Ursulin
2018-07-24  9:59         ` [PATCH i-g-t v4 " Tvrtko Ursulin
2018-07-24  9:59           ` [igt-dev] " Tvrtko Ursulin
2018-07-24 10:08           ` [PATCH i-g-t v5 " Tvrtko Ursulin
2018-07-24 10:08             ` [igt-dev] " Tvrtko Ursulin
2018-07-24 10:29             ` Tvrtko Ursulin [this message]
2018-07-24 10:29               ` [Intel-gfx] [PATCH i-g-t v6 " Tvrtko Ursulin
2018-07-24 12:27               ` [igt-dev] " Chris Wilson
2018-07-24 12:27                 ` [Intel-gfx] " Chris Wilson
2018-07-23 11:46 ` [PATCH i-g-t 3/4] lib/igt_pm: Export function to restore runtime PM status Tvrtko Ursulin
2018-07-23 11:46   ` [igt-dev] " Tvrtko Ursulin
2018-07-23 11:46 ` [PATCH i-g-t 4/4] tests/perf_pmu: Restore runtime PM status at rc6 test exit Tvrtko Ursulin
2018-07-23 11:46   ` [igt-dev] " Tvrtko Ursulin
2018-07-23 12:37 ` [igt-dev] ✗ Fi.CI.BAT: failure for series starting with [i-g-t,1/4] lib/igt_pm: Make exit handlers signal safe Patchwork
2018-07-23 13:12 ` [igt-dev] ✓ Fi.CI.BAT: success for series starting with [i-g-t,1/4] lib/igt_pm: Make exit handlers signal safe (rev2) Patchwork
2018-07-23 13:58 ` [igt-dev] ✓ Fi.CI.IGT: success for series starting with [i-g-t,1/4] lib/igt_pm: Make exit handlers signal safe Patchwork
2018-07-23 15:20 ` [igt-dev] ✓ Fi.CI.IGT: success for series starting with [i-g-t,1/4] lib/igt_pm: Make exit handlers signal safe (rev2) Patchwork
2018-07-23 15:54 ` [igt-dev] ✗ Fi.CI.BAT: failure for series starting with [i-g-t,1/4] lib/igt_pm: Make exit handlers signal safe (rev3) Patchwork
2018-07-23 16:35   ` Tvrtko Ursulin
2018-07-24 10:34 ` [igt-dev] ✗ Fi.CI.BAT: failure for series starting with [i-g-t,1/4] lib/igt_pm: Make exit handlers signal safe (rev5) Patchwork
2018-07-24 10:50 ` [igt-dev] ✓ Fi.CI.BAT: success for series starting with [i-g-t,1/4] lib/igt_pm: Make exit handlers signal safe (rev6) Patchwork
2018-07-24 11:27 ` [igt-dev] ✓ Fi.CI.IGT: success for series starting with [i-g-t,1/4] lib/igt_pm: Make exit handlers signal safe (rev5) Patchwork
2018-07-24 12:07 ` [igt-dev] ✗ Fi.CI.IGT: failure for series starting with [i-g-t,1/4] lib/igt_pm: Make exit handlers signal safe (rev6) Patchwork
2018-07-26 17:08 ` [igt-dev] ✓ Fi.CI.BAT: success " 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=20180724102931.25352-1-tvrtko.ursulin@linux.intel.com \
    --to=tursulin@ursulin.net \
    --cc=igt-dev@lists.freedesktop.org \
    --cc=intel-gfx@lists.freedesktop.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.