From patchwork Tue Jul 24 09:59:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 10541755 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 C9773112B for ; Tue, 24 Jul 2018 09:59:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B5E2A286D7 for ; Tue, 24 Jul 2018 09:59:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A8B9F28711; Tue, 24 Jul 2018 09:59:42 +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 3697B286E1 for ; Tue, 24 Jul 2018 09:59:42 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 055D46E1DD; Tue, 24 Jul 2018 09:59:41 +0000 (UTC) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by gabe.freedesktop.org (Postfix) with ESMTPS id 0DEE16E1C5 for ; Tue, 24 Jul 2018 09:59:38 +0000 (UTC) Received: by mail-wr1-x443.google.com with SMTP id h9-v6so3514503wro.3 for ; Tue, 24 Jul 2018 02:59:38 -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=B1YsJsbyOhb2jhgV1EsyYT08mqsjt2vTfCP108mJC88=; b=dgVw+Ash1thYOUvkv4jyexPKSUv0MRACHlxnYdgjnL8qoHhHLh5LFfdE5zxHnPrBYd IUvjFCMDpMVpOo0qNB/io4a6Wgi3DGSSLleM9Iat5Spbw/5mpm2MaqonO1NYTRYsI0oA GefR6yqwNUSf8FTJnpi/pcL2+J5Px7QFp8r3K2SPSmCH76Y9uEcEwbCG1F7RJbL5cua9 /qvUfsJCRaC2SP9XH3A+LsJdF0t3IkOxJWxaWwgsBwyGkF7kqh+3s9Awpm9M+c4vIjnX RadZlVNDNjwJxLeY2E7Zdah0/lKOxTSfnh5HQQ3WODHzz9m2noukdou1iEkJxdxz8uNV LadA== X-Gm-Message-State: AOUpUlHBcWdTtrEaxeTF37B1PtfbeFYNDkIF1iKVSjw3Y0Lu8EBkdAzE LmF6yd6C2VXeSIQ7QlcljKH96wCFUug= X-Google-Smtp-Source: AAOMgpfqcD1Ts153m4YiPteZpjus82z6ezhCAwYSBko36BZf8PkVyjNTY2Or8kP5jFiOY0DDVyO6ag== X-Received: by 2002:adf:de84:: with SMTP id w4-v6mr11657194wrl.270.1532426377662; Tue, 24 Jul 2018 02:59:37 -0700 (PDT) Received: from localhost.localdomain ([95.146.151.144]) by smtp.gmail.com with ESMTPSA id e133-v6sm2657394wma.33.2018.07.24.02.59.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 24 Jul 2018 02:59:37 -0700 (PDT) From: Tvrtko Ursulin X-Google-Original-From: Tvrtko Ursulin To: igt-dev@lists.freedesktop.org Date: Tue, 24 Jul 2018 10:59:32 +0100 Message-Id: <20180724095932.24379-1-tvrtko.ursulin@linux.intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180723153136.15531-1-tvrtko.ursulin@linux.intel.com> References: <20180723153136.15531-1-tvrtko.ursulin@linux.intel.com> Subject: [Intel-gfx] [PATCH i-g-t v4 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. Signed-off-by: Tvrtko Ursulin --- lib/igt_pm.c | 92 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 76 insertions(+), 16 deletions(-) diff --git a/lib/igt_pm.c b/lib/igt_pm.c index 6f3b0a2d897d..22d8d420c5d7 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,92 @@ 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]; + 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); + 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); } /**