From patchwork Tue Jul 24 10:29:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 10541795 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 9931714E2 for ; Tue, 24 Jul 2018 10:29:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 80059286FB for ; Tue, 24 Jul 2018 10:29:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7216D28745; Tue, 24 Jul 2018 10:29:47 +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 0170A286FB for ; Tue, 24 Jul 2018 10:29:46 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 4FA3D6E523; Tue, 24 Jul 2018 10:29:46 +0000 (UTC) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) by gabe.freedesktop.org (Postfix) with ESMTPS id 3A12F6E523 for ; Tue, 24 Jul 2018 10:29:44 +0000 (UTC) Received: by mail-wr1-x444.google.com with SMTP id t13-v6so3574687wrv.12 for ; Tue, 24 Jul 2018 03:29:44 -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=VYqQOhWUC+Yg6gFaa++TCjj2u7h4uE2gDzTgRumSf5k=; b=Sfo5dEBgT61+1SNpPEj+lzSbr+SkXBmSfWc2Z1Jk44MosWZ370pi0Ga5LH6DLZ3MLF IVP3X5wf6fACZDBdQZjYg1GWAelDMJrSghaRZv8Or7JBAsE2aht/WPZmV4BwPTYUcEO+ par0e75WN2s4NNLE3gFIsSKqGDyNdxpy+khkIr9GOpODwvgPJgYLAibTF7V+2j/3Bs+I Yj2AGyjbgUamN26JVWLF795U/4Guo3nNJdwSc2HHGtKgdx2+aePrD+GrXaKiWCPWiRP7 u2TuUxsUPZRPJXV33lvTz00kcpDvzrlPUvCskeo+ugmXqBk2RCgR7gr1dOf1gQukmj9d 2L3A== X-Gm-Message-State: AOUpUlFai+4vTKIIZS9QiDkiHuBj4WdkibmmTqV72QFgDco7qYIbRDN4 2J/AmX5nWzPkSlnumfl9H/A6kw== X-Google-Smtp-Source: AAOMgpe+x7pXDvzUzOeTmL+7LEgBoSvtj3LMMI+uZuwMINvJfFOvj8SguFubYO7Aol+MFSgPxZhaRw== X-Received: by 2002:adf:b202:: with SMTP id u2-v6mr11156931wra.19.1532428182871; Tue, 24 Jul 2018 03:29:42 -0700 (PDT) Received: from localhost.localdomain ([95.146.151.144]) by smtp.gmail.com with ESMTPSA id s5-v6sm22092819wro.33.2018.07.24.03.29.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 24 Jul 2018 03:29:42 -0700 (PDT) From: Tvrtko Ursulin X-Google-Original-From: Tvrtko Ursulin To: igt-dev@lists.freedesktop.org Date: Tue, 24 Jul 2018 11:29:31 +0100 Message-Id: <20180724102931.25352-1-tvrtko.ursulin@linux.intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180724100852.24896-1-tvrtko.ursulin@linux.intel.com> References: <20180724100852.24896-1-tvrtko.ursulin@linux.intel.com> Subject: [Intel-gfx] [PATCH i-g-t v6 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. v6: * Fix error out logic. Signed-off-by: Tvrtko Ursulin Reviewed-by: Chris Wilson --- 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 #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,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); } /**