From patchwork Tue Jul 24 10:08:52 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 10541781 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 283BE112E for ; Tue, 24 Jul 2018 10:09:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0FDFD28756 for ; Tue, 24 Jul 2018 10:09:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0439428762; Tue, 24 Jul 2018 10:09:03 +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=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, 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 8020328756 for ; Tue, 24 Jul 2018 10:09:02 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 904046E286; Tue, 24 Jul 2018 10:09:01 +0000 (UTC) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by gabe.freedesktop.org (Postfix) with ESMTPS id 79E916E282 for ; Tue, 24 Jul 2018 10:08:59 +0000 (UTC) Received: by mail-wr1-x441.google.com with SMTP id j5-v6so3528416wrr.8 for ; Tue, 24 Jul 2018 03:08:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=siaEq+rk4Ntf8FeRwJHZsYAnRnD+0BsfD6HixUbQd2E=; b=XAQykpHvdiSgMm2Zk2kGAdMDlHU1F/WpQJSbFD+/PsnVqiiPYWe67ff11z/lj85m4Y IBa9Ac3wbW/vAy++A2AGdqZOWVJkljE8LFTmmPCgnI4Ak/k9gJDE/d/w8IPbpJJz8tSq UepAyYSB9KImQN/ORcxJ/94p1tu0RlotdYVZbDEagFbcKw3rAphQWCFwkC9dn+M9p1oX fzQhYfbGPF+XVfa8yuaqTxIWiTXUsEAfRvbSpc0QedvV5XdynKVKAelksUGh9UluI9QN qBRYdhHuW/dZLLnIhJrrZPIoXVRJD/BnYte8YJw3I79ngwvfoDOTi2XTAjIDpIueZyW5 tNlQ== X-Gm-Message-State: AOUpUlEo+/Vm3sH6YytEETD2QYkSUk2y0q7KaBnq46LftnWBZzcClu11 ZU2nCMqVx8pQtGaQpcILn0SpLA== X-Google-Smtp-Source: AAOMgpeqT1kRzY19wds4Vamh6DcE4ggvRzRo5HoLcfDJDc6Bs888bXcKD4MtYUpmQKCDMfkaagPzbA== X-Received: by 2002:adf:e841:: with SMTP id d1-v6mr11086712wrn.269.1532426938175; Tue, 24 Jul 2018 03:08:58 -0700 (PDT) Received: from localhost.localdomain ([95.146.151.144]) by smtp.gmail.com with ESMTPSA id z11-v6sm21183895wrm.94.2018.07.24.03.08.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 24 Jul 2018 03:08:57 -0700 (PDT) From: Tvrtko Ursulin X-Google-Original-From: Tvrtko Ursulin To: igt-dev@lists.freedesktop.org Date: Tue, 24 Jul 2018 11:08:52 +0100 Message-Id: <20180724100852.24896-1-tvrtko.ursulin@linux.intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180724095932.24379-1-tvrtko.ursulin@linux.intel.com> References: <20180724095932.24379-1-tvrtko.ursulin@linux.intel.com> Subject: [Intel-gfx] [PATCH i-g-t v5 2/4] lib/igt_pm: Find HDA device when attempting to enable runtime PM X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: intel-gfx@lists.freedesktop.org MIME-Version: 1.0 Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" X-Virus-Scanned: ClamAV using ClamSMTP From: Tvrtko Ursulin 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. Signed-off-by: Tvrtko Ursulin --- lib/igt_pm.c | 93 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 77 insertions(+), 16 deletions(-) diff --git a/lib/igt_pm.c b/lib/igt_pm.c index 6f3b0a2d897d..21eb01e1903e 100644 --- a/lib/igt_pm.c +++ b/lib/igt_pm.c @@ -33,6 +33,7 @@ #include #include #include +#include #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,93 @@ 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); - close(fd); - } - 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); + 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; } + if (!path) + goto err; + + fd = open("/sys/module/snd_hda_intel/parameters/power_save", O_RDWR); + if (fd < 0) + 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); } /**