Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp5182333imm; Wed, 12 Sep 2018 02:16:15 -0700 (PDT) X-Google-Smtp-Source: ANB0VdawmpO+B9heu4/LcJCTpVhyfN0uAhnAdThzlkI4WJjXbLREQ+QDsGVIXUxMJaHsgLrIAlkZ X-Received: by 2002:a17:902:8345:: with SMTP id z5-v6mr1066630pln.147.1536743775673; Wed, 12 Sep 2018 02:16:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536743775; cv=none; d=google.com; s=arc-20160816; b=qnX36bqc65c2BnLEqyl/03stK3mUDVxTOz8v2yH1QxrhCmh7juYshrKGyeeKFuP4WR IbmoBYRZk5KYGaYtQKYfmf16bA7M4xa6K6+fZeG8LFVh0rRrKkKFKngHnflwfLOmuF/q oWzU1C07uCV1hREhUn1GGowiTZliFiadCmNd+Dkbn3QEtY/nE+uz32PqHKfFpu4Ssw0/ AhiDPtJ2uUXlCw4IOJPVJOUa8e7TDy4epEZEzq3ILXWcSquDCLzMPrN9D+Mtk9b0AJ9q u5h/q2rnGjwCrOZCfH1CJGUZNB8MH6RghwK7WaI0VmvoboSxEPKs0NJw4X8XphV0FJJF dt5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=5aKT/Q/+Y7U23guq7RqtyLgCTmTMGRP6pynvpX9ztv8=; b=UKdxYtp2poEvCedcGLBTUnU7lV+DdausHMQYSt5HI0Yj2NzwEwsmlMXX/eBS+L+hXU U0t3vTs0p7saIRp6vlAmssz+tERbv70KE7vYVv9TYTFZymAkSrY224NmIgO5ptGViOLq merL1gFo8nYgobuXR8amhS0aJSUG1z3qPTZGJY7TCQjK1SXWfGQtpO9va86nHLinwqlG t8MQ2tbMvKiHBBeGc2QGQpiplPXopQt5/c40BVOlFKGXxGHUa5oorkZ9MlXHheosCXf6 1fQu5nrUZEKcrlwqzbxL5YfLpNR0DcweBYJZ2gfamZ5OzHxgrdbSNlcTaBL35fNX5gf0 kncw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f18-v6si481975pgi.300.2018.09.12.02.16.01; Wed, 12 Sep 2018 02:16:15 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728349AbeILOSA (ORCPT + 99 others); Wed, 12 Sep 2018 10:18:00 -0400 Received: from foss.arm.com ([217.140.101.70]:56130 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728338AbeILOR7 (ORCPT ); Wed, 12 Sep 2018 10:17:59 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 693377A9; Wed, 12 Sep 2018 02:14:22 -0700 (PDT) Received: from queper01-lin.local (queper01-lin.emea.arm.com [10.4.13.27]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 51B393F703; Wed, 12 Sep 2018 02:14:18 -0700 (PDT) From: Quentin Perret To: peterz@infradead.org, rjw@rjwysocki.net, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org Cc: gregkh@linuxfoundation.org, mingo@redhat.com, dietmar.eggemann@arm.com, morten.rasmussen@arm.com, chris.redpath@arm.com, patrick.bellasi@arm.com, valentin.schneider@arm.com, vincent.guittot@linaro.org, thara.gopinath@linaro.org, viresh.kumar@linaro.org, tkjos@google.com, joel@joelfernandes.org, smuckle@google.com, adharmap@codeaurora.org, skannan@codeaurora.org, pkondeti@codeaurora.org, juri.lelli@redhat.com, edubezval@gmail.com, srinivas.pandruvada@linux.intel.com, currojerez@riseup.net, javi.merino@kernel.org, quentin.perret@arm.com Subject: [PATCH v7 14/14] OPTIONAL: cpufreq: dt: Register an Energy Model Date: Wed, 12 Sep 2018 10:13:09 +0100 Message-Id: <20180912091309.7551-15-quentin.perret@arm.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180912091309.7551-1-quentin.perret@arm.com> References: <20180912091309.7551-1-quentin.perret@arm.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ******************************************************************* * This patch illustrates the usage of the newly introduced Energy * * Model framework and isn't supposed to be merged as-is. * ******************************************************************* The Energy Model framework provides an API to register the active power of CPUs. Call this API from the cpufreq-dt driver with an estimation of the power as P = C * V^2 * f with C, V, and f respectively the capacitance of the CPU and the voltage and frequency of the OPP. The CPU capacitance is read from the "dynamic-power-coefficient" DT binding (originally introduced for thermal/IPA), and the voltage and frequency values from PM_OPP. Cc: "Rafael J. Wysocki" Cc: Viresh Kumar Signed-off-by: Quentin Perret --- drivers/cpufreq/cpufreq-dt.c | 48 +++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/drivers/cpufreq/cpufreq-dt.c b/drivers/cpufreq/cpufreq-dt.c index 0a9ebf00be46..15ac9754afa2 100644 --- a/drivers/cpufreq/cpufreq-dt.c +++ b/drivers/cpufreq/cpufreq-dt.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -149,8 +150,50 @@ static int resources_available(void) return 0; } +static int __maybe_unused of_est_power(unsigned long *mW, unsigned long *KHz, + int cpu) +{ + unsigned long mV, Hz, MHz; + struct device *cpu_dev; + struct dev_pm_opp *opp; + struct device_node *np; + u32 cap; + u64 tmp; + + cpu_dev = get_cpu_device(cpu); + if (!cpu_dev) + return -ENODEV; + + np = of_node_get(cpu_dev->of_node); + if (!np) + return -EINVAL; + + if (of_property_read_u32(np, "dynamic-power-coefficient", &cap)) + return -EINVAL; + + Hz = *KHz * 1000; + opp = dev_pm_opp_find_freq_ceil(cpu_dev, &Hz); + if (IS_ERR(opp)) + return -EINVAL; + + mV = dev_pm_opp_get_voltage(opp) / 1000; + dev_pm_opp_put(opp); + if (!mV) + return -EINVAL; + + MHz = Hz / 1000000; + tmp = (u64)cap * mV * mV * MHz; + do_div(tmp, 1000000000); + + *mW = (unsigned long)tmp; + *KHz = Hz / 1000; + + return 0; +} + static int cpufreq_init(struct cpufreq_policy *policy) { + struct em_data_callback em_cb = EM_DATA_CB(of_est_power); struct cpufreq_frequency_table *freq_table; struct opp_table *opp_table = NULL; struct private_data *priv; @@ -159,7 +202,7 @@ static int cpufreq_init(struct cpufreq_policy *policy) unsigned int transition_latency; bool fallback = false; const char *name; - int ret; + int ret, nr_opp; cpu_dev = get_cpu_device(policy->cpu); if (!cpu_dev) { @@ -226,6 +269,7 @@ static int cpufreq_init(struct cpufreq_policy *policy) ret = -EPROBE_DEFER; goto out_free_opp; } + nr_opp = ret; if (fallback) { cpumask_setall(policy->cpus); @@ -278,6 +322,8 @@ static int cpufreq_init(struct cpufreq_policy *policy) policy->cpuinfo.transition_latency = transition_latency; policy->dvfs_possible_from_any_cpu = true; + em_register_perf_domain(policy->cpus, nr_opp, &em_cb); + return 0; out_free_cpufreq_table: -- 2.18.0