From patchwork Mon Apr 17 06:01:57 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 97501 Delivered-To: patch@linaro.org Received: by 10.140.109.52 with SMTP id k49csp1212755qgf; Sun, 16 Apr 2017 23:03:55 -0700 (PDT) X-Received: by 10.99.160.85 with SMTP id u21mr8567508pgn.157.1492409035213; Sun, 16 Apr 2017 23:03:55 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g26si10020227pge.96.2017.04.16.23.03.54; Sun, 16 Apr 2017 23:03:55 -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; dkim=pass header.i=@linaro.org; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932991AbdDQGDc (ORCPT + 14 others); Mon, 17 Apr 2017 02:03:32 -0400 Received: from mail-pg0-f51.google.com ([74.125.83.51]:34714 "EHLO mail-pg0-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932967AbdDQGDZ (ORCPT ); Mon, 17 Apr 2017 02:03:25 -0400 Received: by mail-pg0-f51.google.com with SMTP id z127so3843820pgb.1 for ; Sun, 16 Apr 2017 23:03:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=GKE5srZzSSC3rX4vUbO/8WFttQSD6iqqoCAl2mL6vLA=; b=A1+K4GZ7KzuhyKUL0NRs8yC67DJPN+4DXcHQ4qnrhAF2BsUQujEnOTHQy0EQbWj9xb Z7yAb5B2sqqOAadrjxpZaUNzdu6+MMR8EcIxdZKePwCaqVyNaQ6NFNBsWS0Wxlhl/QOM 0xFGxt7Qw2j4YuiP8SLgLsYJ33OeaBIwq7VlA= 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:in-reply-to:references; bh=GKE5srZzSSC3rX4vUbO/8WFttQSD6iqqoCAl2mL6vLA=; b=mjvEXGmr8dGgr0sjmKNDvo/7hIRo6orUdN99CKzs6cXWgVbS1SsOCl/RZ8FoX3JDb0 PeIDBVw1dlFiMK3XID+MEob3q1AZo07BRVrNuiSTu2kGmcEbC69JLXDW8xcX9713iGdx T9QHruFWGs0VwnJBl3Ay8h7LteM4TfB2CkS94K+bXvBhPqhaxy5uGG8cUPWdVtonE7bu t5zewRjp9DSmuW7l8Ij3/Ih6wgU8I4bFg3Y5RpznfjQUWe3yKJOvuoERyz+L/DjKBXcK uTRwBiATIwogHniLO3qpVsj18OkheifVD7YQzPd9e1sd3NbyPt5lyiPiz4VXh1T5seYd DaTA== X-Gm-Message-State: AN3rC/4yccNqJDpFKxe4ytG1DeNluZPpblwPR0+4mhrnRDYTk5+ipFXW 5xxw91aZpQFaawe2 X-Received: by 10.99.144.65 with SMTP id a62mr10431139pge.12.1492409004603; Sun, 16 Apr 2017 23:03:24 -0700 (PDT) Received: from localhost ([122.172.121.5]) by smtp.gmail.com with ESMTPSA id d130sm15395745pga.17.2017.04.16.23.03.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 16 Apr 2017 23:03:24 -0700 (PDT) From: Viresh Kumar To: Javi Merino , Zhang Rui , Eduardo Valentin , Amit Daniel Kachhap , Viresh Kumar Cc: linaro-kernel@lists.linaro.org, Rafael Wysocki , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Vincent Guittot Subject: [PATCH V2 12/17] thermal: cpu_cooling: merge frequency and power tables Date: Mon, 17 Apr 2017 11:31:57 +0530 Message-Id: <0bdf2c1125f4e0cd768e5972b8374ac5535b0323.1492408342.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.12.0.432.g71c3a4f4ba37 In-Reply-To: References: In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The cpu_cooling driver keeps two tables: - freq_table: table of frequencies in descending order, built from policy->freq_table. - power_table: table of frequencies and power in ascending order, built from OPP table. If the OPPs are used for the CPU device then both these tables are actually built using the OPP core and should have the same frequency entries. And there is no need to keep separate tables for this. Lets merge them both. Note that the new table is in descending order of frequencies and so the 'for' loops were required to be fixed at few places to make it work. Signed-off-by: Viresh Kumar --- drivers/thermal/cpu_cooling.c | 123 ++++++++++++++++++------------------------ 1 file changed, 52 insertions(+), 71 deletions(-) -- 2.12.0.432.g71c3a4f4ba37 diff --git a/drivers/thermal/cpu_cooling.c b/drivers/thermal/cpu_cooling.c index 37a1605a4d7a..4702a9821c1e 100644 --- a/drivers/thermal/cpu_cooling.c +++ b/drivers/thermal/cpu_cooling.c @@ -49,14 +49,14 @@ */ /** - * struct power_table - frequency to power conversion + * struct freq_table - frequency table along with power entries * @frequency: frequency in KHz * @power: power in mW * * This structure is built when the cooling device registers and helps - * in translating frequency to power and viceversa. + * in translating frequency to power and vice versa. */ -struct power_table { +struct freq_table { u32 frequency; u32 power; }; @@ -79,9 +79,6 @@ struct power_table { * @time_in_idle: previous reading of the absolute time that this cpu was idle * @time_in_idle_timestamp: wall time of the last invocation of * get_cpu_idle_time_us() - * @dyn_power_table: array of struct power_table for frequency to power - * conversion, sorted in ascending order. - * @dyn_power_table_entries: number of entries in the @dyn_power_table array * @cpu_dev: the cpu_device of policy->cpu. * @plat_get_static_power: callback to calculate the static power * @@ -95,13 +92,11 @@ struct cpufreq_cooling_device { unsigned int cpufreq_state; unsigned int clipped_freq; unsigned int max_level; - unsigned int *freq_table; /* In descending order */ + struct freq_table *freq_table; /* In descending order */ struct list_head node; u32 last_load; u64 *time_in_idle; u64 *time_in_idle_timestamp; - struct power_table *dyn_power_table; - int dyn_power_table_entries; struct device *cpu_dev; get_static_t plat_get_static_power; }; @@ -125,10 +120,10 @@ static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev, unsigned long level; for (level = 0; level <= cpufreq_cdev->max_level; level++) { - if (freq == cpufreq_cdev->freq_table[level]) + if (freq == cpufreq_cdev->freq_table[level].frequency) return level; - if (freq > cpufreq_cdev->freq_table[level]) + if (freq > cpufreq_cdev->freq_table[level].frequency) break; } @@ -185,28 +180,25 @@ static int cpufreq_thermal_notifier(struct notifier_block *nb, } /** - * build_dyn_power_table() - create a dynamic power to frequency table - * @cpufreq_cdev: the cpufreq cooling device in which to store the table + * update_freq_table() - Update the freq table with power numbers + * @cpufreq_cdev: the cpufreq cooling device in which to update the table * @capacitance: dynamic power coefficient for these cpus * - * Build a dynamic power to frequency table for this cpu and store it - * in @cpufreq_cdev. This table will be used in cpu_power_to_freq() and - * cpu_freq_to_power() to convert between power and frequency - * efficiently. Power is stored in mW, frequency in KHz. The - * resulting table is in ascending order. + * Update the freq table with power numbers. This table will be used in + * cpu_power_to_freq() and cpu_freq_to_power() to convert between power and + * frequency efficiently. Power is stored in mW, frequency in KHz. The + * resulting table is in descending order. * * Return: 0 on success, -EINVAL if there are no OPPs for any CPUs, - * -ENOMEM if we run out of memory or -EAGAIN if an OPP was - * added/enabled while the function was executing. + * or -ENOMEM if we run out of memory. */ -static int build_dyn_power_table(struct cpufreq_cooling_device *cpufreq_cdev, - u32 capacitance) +static int update_freq_table(struct cpufreq_cooling_device *cpufreq_cdev, + u32 capacitance) { - struct power_table *power_table; + struct freq_table *freq_table = cpufreq_cdev->freq_table; struct dev_pm_opp *opp; struct device *dev = NULL; - int num_opps = 0, cpu = cpufreq_cdev->policy->cpu, i, ret = 0; - unsigned long freq; + int num_opps = 0, cpu = cpufreq_cdev->policy->cpu, i; dev = get_cpu_device(cpu); if (unlikely(!dev)) { @@ -219,25 +211,32 @@ static int build_dyn_power_table(struct cpufreq_cooling_device *cpufreq_cdev, if (num_opps < 0) return num_opps; - if (num_opps == 0) + /* + * The cpufreq table is also built from the OPP table and so the count + * should match. + */ + if (num_opps != cpufreq_cdev->max_level + 1) { + dev_warn(dev, "Number of OPPs not matching with max_levels\n"); return -EINVAL; + } - power_table = kcalloc(num_opps, sizeof(*power_table), GFP_KERNEL); - if (!power_table) - return -ENOMEM; - - for (freq = 0, i = 0; - opp = dev_pm_opp_find_freq_ceil(dev, &freq), !IS_ERR(opp); - freq++, i++) { - u32 freq_mhz, voltage_mv; + for (i = 0; i < cpufreq_cdev->max_level; i++) { + unsigned long freq = freq_table[i].frequency * 1000; + u32 freq_mhz = freq_table[i].frequency / 1000; u64 power; + u32 voltage_mv; - if (i >= num_opps) { - ret = -EAGAIN; - goto free_power_table; + /* + * Find ceil frequency as 'freq' may be slightly lower than OPP + * freq due to truncation while converting to kHz. + */ + opp = dev_pm_opp_find_freq_ceil(dev, &freq); + if (IS_ERR(opp)) { + dev_err(dev, "failed to get opp for %lu frequency\n", + freq); + return -EINVAL; } - freq_mhz = freq / 1000000; voltage_mv = dev_pm_opp_get_voltage(opp) / 1000; dev_pm_opp_put(opp); @@ -248,54 +247,39 @@ static int build_dyn_power_table(struct cpufreq_cooling_device *cpufreq_cdev, power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv; do_div(power, 1000000000); - /* frequency is stored in power_table in KHz */ - power_table[i].frequency = freq / 1000; - /* power is stored in mW */ - power_table[i].power = power; - } - - if (i != num_opps) { - ret = PTR_ERR(opp); - goto free_power_table; + freq_table[i].power = power; } cpufreq_cdev->cpu_dev = dev; - cpufreq_cdev->dyn_power_table = power_table; - cpufreq_cdev->dyn_power_table_entries = i; return 0; - -free_power_table: - kfree(power_table); - - return ret; } static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev, u32 freq) { int i; - struct power_table *pt = cpufreq_cdev->dyn_power_table; + struct freq_table *freq_table = cpufreq_cdev->freq_table; - for (i = 1; i < cpufreq_cdev->dyn_power_table_entries; i++) - if (freq < pt[i].frequency) + for (i = 1; i < cpufreq_cdev->max_level; i++) + if (freq > freq_table[i].frequency) break; - return pt[i - 1].power; + return freq_table[i - 1].power; } static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev, u32 power) { int i; - struct power_table *pt = cpufreq_cdev->dyn_power_table; + struct freq_table *freq_table = cpufreq_cdev->freq_table; - for (i = 1; i < cpufreq_cdev->dyn_power_table_entries; i++) - if (power < pt[i].power) + for (i = 1; i < cpufreq_cdev->max_level; i++) + if (power > freq_table[i].power) break; - return pt[i - 1].frequency; + return freq_table[i - 1].frequency; } /** @@ -462,7 +446,7 @@ static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev, if (cpufreq_cdev->cpufreq_state == state) return 0; - clip_freq = cpufreq_cdev->freq_table[state]; + clip_freq = cpufreq_cdev->freq_table[state].frequency; cpufreq_cdev->cpufreq_state = state; cpufreq_cdev->clipped_freq = clip_freq; @@ -575,7 +559,7 @@ static int cpufreq_state2power(struct thermal_cooling_device *cdev, num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus); - freq = cpufreq_cdev->freq_table[state]; + freq = cpufreq_cdev->freq_table[state].frequency; if (!freq) return -EINVAL; @@ -750,7 +734,7 @@ __cpufreq_cooling_register(struct device_node *np, if (capacitance) { cpufreq_cdev->plat_get_static_power = plat_static_func; - ret = build_dyn_power_table(cpufreq_cdev, capacitance); + ret = update_freq_table(cpufreq_cdev, capacitance); if (ret) { cdev = ERR_PTR(ret); goto free_table; @@ -764,14 +748,14 @@ __cpufreq_cooling_register(struct device_node *np, ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL); if (ret < 0) { cdev = ERR_PTR(ret); - goto free_power_table; + goto free_table; } cpufreq_cdev->id = ret; /* Fill freq-table in descending order of frequencies */ for (i = 0, freq = -1; i <= cpufreq_cdev->max_level; i++) { freq = find_next_max(policy->freq_table, freq); - cpufreq_cdev->freq_table[i] = freq; + cpufreq_cdev->freq_table[i].frequency = freq; /* Warn for duplicate entries */ if (!freq) @@ -788,7 +772,7 @@ __cpufreq_cooling_register(struct device_node *np, if (IS_ERR(cdev)) goto remove_ida; - cpufreq_cdev->clipped_freq = cpufreq_cdev->freq_table[0]; + cpufreq_cdev->clipped_freq = cpufreq_cdev->freq_table[0].frequency; cpufreq_cdev->cdev = cdev; cpufreq_cdev->policy = policy; @@ -806,8 +790,6 @@ __cpufreq_cooling_register(struct device_node *np, remove_ida: ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id); -free_power_table: - kfree(cpufreq_cdev->dyn_power_table); free_table: kfree(cpufreq_cdev->freq_table); free_time_in_idle_timestamp: @@ -956,7 +938,6 @@ void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev) thermal_cooling_device_unregister(cpufreq_cdev->cdev); ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id); - kfree(cpufreq_cdev->dyn_power_table); kfree(cpufreq_cdev->time_in_idle_timestamp); kfree(cpufreq_cdev->time_in_idle); kfree(cpufreq_cdev->freq_table);