From patchwork Wed Jul 29 15:12:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lukasz Luba X-Patchwork-Id: 11691191 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3698513B6 for ; Wed, 29 Jul 2020 15:14:49 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id E5F9720829 for ; Wed, 29 Jul 2020 15:14:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="rp7MDN7M" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E5F9720829 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:MIME-Version:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:References:In-Reply-To:Message-Id:Date:Subject:To: From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=YtCBQEEHcsRp08jIAfSnJQS8fRsHEvCXVXO74hqJnRk=; b=rp7MDN7MbZrhpkhhaiWj4DcNQf kvwzsQMmaMUSu4lmGYRPYsgPV/7hY/WuYeqD+j+F5ygo4TR2tAfhY02oBZgpO5fjXeHBVL6kAz74/ J/3pvqT7QJlnbhrE0E7vOFjUIL+fSxCFID88hi1DWod995VOpkJ/Dd7VMcL9V10+mpRNS9EXgEi+E TT8yU66uBV/Ggvb0RyY6ZcK/MrblNgFCzFrPX73Dc3fG2qrhJWfDNlabll6Vj6+CGq9ZUqEMc1/N3 BwNVxAfHkwy1NOYEExYNX0d2SnnMkjsEh9gqtRdgJsQLsH8c37xwZ0jYDZ17Juw55/TJHk4/YZGJk Gj4bcZlg==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1k0nlZ-0006DU-Uq; Wed, 29 Jul 2020 15:13:13 +0000 Received: from foss.arm.com ([217.140.110.172]) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1k0nkt-000605-Az for linux-arm-kernel@lists.infradead.org; Wed, 29 Jul 2020 15:12:35 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id A7E931063; Wed, 29 Jul 2020 08:12:30 -0700 (PDT) Received: from e123648.arm.com (unknown [10.37.12.35]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 9F1BD3F66E; Wed, 29 Jul 2020 08:12:28 -0700 (PDT) From: Lukasz Luba To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-pm@vger.kernel.org Subject: [PATCH 4/4] cpufreq: scmi: Read statistics from FW shared memory Date: Wed, 29 Jul 2020 16:12:08 +0100 Message-Id: <20200729151208.27737-5-lukasz.luba@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200729151208.27737-1-lukasz.luba@arm.com> References: <20200729151208.27737-1-lukasz.luba@arm.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200729_111231_585520_3DEC3FE5 X-CRM114-Status: GOOD ( 19.99 ) X-Spam-Score: -2.3 (--) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-2.3 points) pts rule name description ---- ---------------------- -------------------------------------------------- -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at https://www.dnswl.org/, medium trust [217.140.110.172 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: viresh.kumar@linaro.org, rjw@rjwysocki.net, lukasz.luba@arm.com, cristian.marussi@arm.com, sudeep.holla@arm.com MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org Add support for reading CPUFreq statistics from the firmware. The setup code initializes needed structures per domain basis. The driver callback scmi_cpufreq_stats_get is called by the CPUFreq framework and invokes SCMI performance protocol in order to retrieve latest statistics. Signed-off-by: Lukasz Luba --- drivers/cpufreq/scmi-cpufreq.c | 80 ++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/drivers/cpufreq/scmi-cpufreq.c b/drivers/cpufreq/scmi-cpufreq.c index fe95350eb844..67ae63171dd0 100644 --- a/drivers/cpufreq/scmi-cpufreq.c +++ b/drivers/cpufreq/scmi-cpufreq.c @@ -22,6 +22,7 @@ struct scmi_data { int domain_id; struct device *cpu_dev; + struct scmi_perf_domain_stats *stats; }; static unsigned int scmi_cpufreq_get_rate(unsigned int cpu); @@ -48,6 +49,34 @@ static struct cpufreq_driver scmi_cpufreq_driver = { static const struct scmi_handle *handle; +static int scmi_cpufreq_stats_get(struct cpufreq_policy *policy) +{ + struct scmi_data *priv = policy->driver_data; + struct cpufreq_stats *stats = policy->stats; + int i, ret; + + /* + * Since the driver callback is global and can be set for all + * policy objects, there is a need to check if that policy has + * statistics in the shared memory. + */ + if (!policy->has_driver_stats) + return -EINVAL; + + ret = handle->perf_ops->statistics_get(handle, priv->domain_id, priv->stats); + if (ret) + return ret; + + for (i = 0; i < priv->stats->state_num; i++) + stats->time_in_state[i] = priv->stats->time_in_state[i]; + + stats->total_trans = priv->stats->total_trans; + stats->last_index = priv->stats->last_index; + stats->last_time = priv->stats->last_time; + + return ret; +} + static unsigned int scmi_cpufreq_get_rate(unsigned int cpu) { struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu); @@ -147,6 +176,45 @@ scmi_get_cpu_power(unsigned long *power, unsigned long *KHz, return 0; } +static int scmi_cpufreq_stats_init(struct cpufreq_policy *policy, + struct scmi_data *priv, int nr_opp) +{ + struct scmi_perf_domain_stats *stats; + int ret; + + stats = kzalloc(sizeof(struct scmi_perf_domain_stats), GFP_KERNEL); + if (!stats) + return -ENOMEM; + + stats->time_in_state = kcalloc(nr_opp, sizeof(u64), GFP_KERNEL); + if (!stats->time_in_state) { + kfree(stats); + return -ENOMEM; + } + + stats->freq_table = kcalloc(nr_opp, sizeof(unsigned int), GFP_KERNEL); + if (!stats->freq_table) { + kfree(stats->time_in_state); + kfree(stats); + return -ENOMEM; + } + + priv->stats = stats; + priv->stats->state_num = nr_opp; + policy->has_driver_stats = true; + scmi_cpufreq_driver.get_statistics = scmi_cpufreq_stats_get; + + ret = handle->perf_ops->statistics_get(handle, priv->domain_id, stats); + if (ret) { + kfree(stats->freq_table); + kfree(stats->time_in_state); + kfree(stats); + policy->has_driver_stats = false; + } + + return ret; +} + static int scmi_cpufreq_init(struct cpufreq_policy *policy) { int ret, nr_opp; @@ -209,6 +277,10 @@ static int scmi_cpufreq_init(struct cpufreq_policy *policy) /* SCMI allows DVFS request for any domain from any CPU */ policy->dvfs_possible_from_any_cpu = true; + ret = scmi_cpufreq_stats_init(policy, priv, nr_opp); + if (ret) + dev_warn(cpu_dev, "failed to init statistics: %d\n", ret); + latency = handle->perf_ops->transition_latency_get(handle, cpu_dev); if (!latency) latency = CPUFREQ_ETERNAL; @@ -236,6 +308,14 @@ static int scmi_cpufreq_exit(struct cpufreq_policy *policy) dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table); dev_pm_opp_remove_all_dynamic(priv->cpu_dev); + + if (priv->stats) { + policy->has_driver_stats = false; + kfree(priv->stats->time_in_state); + kfree(priv->stats->freq_table); + kfree(priv->stats); + } + kfree(priv); return 0;