All of lore.kernel.org
 help / color / mirror / Atom feed
From: Viresh Kumar <viresh.kumar@linaro.org>
To: linux-pm@vger.kernel.org, edubezval@gmail.com
Cc: linaro-kernel@lists.linaro.org, rui.zhang@intel.com,
	amit.daniel@samsung.com, javi.merino@arm.com,
	Viresh Kumar <viresh.kumar@linaro.org>
Subject: [PATCH V2 25/26] thermal: cpu_cooling: Use cpufreq_dev->freq_table for finding level/freq
Date: Thu,  4 Dec 2014 09:42:07 +0530	[thread overview]
Message-ID: <45a3ff79a10b0618783f0043efa44966c758b410.1417664938.git.viresh.kumar@linaro.org> (raw)
In-Reply-To: <cover.1417664938.git.viresh.kumar@linaro.org>
In-Reply-To: <cover.1417664938.git.viresh.kumar@linaro.org>

get_property() was an over complicated beast with BUGs. It used to believe that
cpufreq table is present in ascending or descending order, which might not
always be true.

Previous patch has created another freq table in descending order for us and we
better use it now. With that get_property() simply goes away and another helper
get_level() comes in.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
 drivers/thermal/cpu_cooling.c | 108 ++++++++----------------------------------
 1 file changed, 19 insertions(+), 89 deletions(-)

diff --git a/drivers/thermal/cpu_cooling.c b/drivers/thermal/cpu_cooling.c
index cb5a4b9..d97e14d 100644
--- a/drivers/thermal/cpu_cooling.c
+++ b/drivers/thermal/cpu_cooling.c
@@ -112,85 +112,27 @@ static void release_idr(struct idr *idr, int id)
 
 /* Below code defines functions to be used for cpufreq as cooling device */
 
-enum cpufreq_cooling_property {
-	GET_LEVEL,
-	GET_FREQ,
-};
-
 /**
- * get_property - fetch a property of interest for a given cpu.
+ * get_level: Find the level for a particular frequency
  * @cpufreq_dev: cpufreq_dev for which the property is required
- * @input: query parameter
- * @output: query return
- * @property: type of query (frequency, level)
- *
- * This is the common function to
- * 1. translate frequency to cooling state
- * 2. translate cooling state to frequency
+ * @freq: Frequency
  *
- * Note that the code may be not in good shape
- * but it is written in this way in order to:
- * a) reduce duplicate code as most of the code can be shared.
- * b) make sure the logic is consistent when translating between
- *    cooling states and frequencies.
- *
- * Return: 0 on success, -EINVAL when invalid parameters are passed.
+ * Returns: level on success, THERMAL_CSTATE_INVALID on error.
  */
-static int get_property(struct cpufreq_cooling_device *cpufreq_dev,
-			unsigned long input, unsigned int *output,
-			enum cpufreq_cooling_property property)
+static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_dev,
+			       unsigned int freq)
 {
-	int i;
-	unsigned long level = 0;
-	unsigned int freq = CPUFREQ_ENTRY_INVALID;
-	int descend = -1;
-	struct cpufreq_frequency_table *pos, *table;
-
-	if (!output)
-		return -EINVAL;
-
-	table = cpufreq_frequency_get_table(cpumask_first(&cpufreq_dev->allowed_cpus));
-	if (!table)
-		return -EINVAL;
-
-	cpufreq_for_each_valid_entry(pos, table) {
-		/* ignore duplicate entry */
-		if (freq == pos->frequency)
-			continue;
-
-		/* get the frequency order */
-		if (freq != CPUFREQ_ENTRY_INVALID && descend == -1)
-			descend = freq > pos->frequency;
-
-		freq = pos->frequency;
-	}
-
-	if (property == GET_FREQ)
-		level = descend ? input : (cpufreq_dev->max_level - input);
-
-	i = 0;
-	cpufreq_for_each_valid_entry(pos, table) {
-		/* ignore duplicate entry */
-		if (freq == pos->frequency)
-			continue;
+	unsigned long level;
 
-		/* now we have a valid frequency entry */
-		freq = pos->frequency;
+	for (level = 0; level <= cpufreq_dev->max_level; level++) {
+		if (freq == cpufreq_dev->freq_table[level])
+			return level;
 
-		if (property == GET_LEVEL && (unsigned int)input == freq) {
-			/* get level by frequency */
-			*output = descend ? i : (cpufreq_dev->max_level - i);
-			return 0;
-		}
-		if (property == GET_FREQ && level == i) {
-			/* get frequency by level */
-			*output = freq;
-			return 0;
-		}
-		i++;
+		if (freq > cpufreq_dev->freq_table[level])
+			break;
 	}
 
-	return -EINVAL;
+	return THERMAL_CSTATE_INVALID;
 }
 
 /**
@@ -211,14 +153,8 @@ unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
 	mutex_lock(&cooling_cpufreq_lock);
 	list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
 		if (cpumask_test_cpu(cpu, &cpufreq_dev->allowed_cpus)) {
-			unsigned int val;
-
 			mutex_unlock(&cooling_cpufreq_lock);
-			if (get_property(cpufreq_dev, (unsigned long)freq, &val,
-					 GET_LEVEL))
-				return THERMAL_CSTATE_INVALID;
-
-			return (unsigned long)val;
+			return get_level(cpufreq_dev, freq);
 		}
 	}
 	mutex_unlock(&cooling_cpufreq_lock);
@@ -323,16 +259,16 @@ static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
 	struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
 	unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
 	unsigned int clip_freq;
-	int ret;
+
+	/* Request state should be less than max_level */
+	if (WARN_ON(state > cpufreq_device->max_level))
+		return -EINVAL;
 
 	/* Check if the old cooling action is same as new cooling action */
 	if (cpufreq_device->cpufreq_state == state)
 		return 0;
 
-	ret = get_property(cpufreq_device, state, &clip_freq, GET_FREQ);
-	if (ret)
-		return ret;
-
+	clip_freq = cpufreq_device->freq_table[state];
 	cpufreq_device->cpufreq_state = state;
 	cpufreq_device->cpufreq_val = clip_freq;
 
@@ -402,13 +338,6 @@ __cpufreq_cooling_register(struct device_node *np,
 	if (!cpufreq_dev)
 		return ERR_PTR(-ENOMEM);
 
-	ret = get_property(cpufreq_dev, 0, &cpufreq_dev->cpufreq_val, GET_FREQ);
-	if (ret) {
-		pr_err("%s: Failed to get frequency: %d", __func__, ret);
-		cool_dev = ERR_PTR(ret);
-		goto free_cdev;
-	}
-
 	/* Find max levels */
 	cpufreq_for_each_valid_entry(pos, table)
 		cpufreq_dev->max_level++;
@@ -452,6 +381,7 @@ __cpufreq_cooling_register(struct device_node *np,
 			pr_debug("%s: freq:%u KHz\n", __func__, freq);
 	}
 
+	cpufreq_dev->cpufreq_val = cpufreq_dev->freq_table[0];
 	cpufreq_dev->cool_dev = cool_dev;
 
 	mutex_lock(&cooling_cpufreq_lock);
-- 
2.0.3.693.g996b0fd


  parent reply	other threads:[~2014-12-04  4:14 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-04  4:11 [PATCH V2 00/26] thermal: cpu_cooling: Fixes and cleanups Viresh Kumar
2014-12-04  4:11 ` [PATCH V2 01/26] thermal: cpu_cooling: check for the readiness of cpufreq layer Viresh Kumar
2014-12-04  4:11   ` Viresh Kumar
2014-12-04 14:08   ` Eduardo Valentin
2014-12-04 14:08     ` Eduardo Valentin
2014-12-04  4:11 ` [PATCH V2 02/26] thermal: db8500: pass cpu_present_mask to cpufreq_cooling_register() Viresh Kumar
2014-12-31  8:11   ` Linus Walleij
2014-12-04  4:11 ` [PATCH V2 03/26] thermal: imx: " Viresh Kumar
2014-12-04  4:11 ` [PATCH V2 04/26] thermal: exynos: " Viresh Kumar
2014-12-04  4:11 ` [PATCH V2 05/26] thermal: cpu_cooling: random comment fixups Viresh Kumar
2014-12-04  4:11 ` [PATCH V2 06/26] thermal: cpu_cooling: fix doc comment over struct cpufreq_cooling_device Viresh Kumar
2014-12-04 10:32   ` Javi Merino
2014-12-04 10:36     ` Viresh Kumar
2014-12-04  4:11 ` [PATCH V2 07/26] thermal: cpu_cooling: Add comment to clarify relation between cooling state and frequency Viresh Kumar
2014-12-04  4:11 ` [PATCH V2 08/26] thermal: cpu_cooling: Pass variable instead of its type to sizeof() Viresh Kumar
2014-12-04  4:11 ` [PATCH V2 09/26] thermal: cpu_cooling: no need to set cpufreq_state to zero Viresh Kumar
2014-12-04  4:11 ` [PATCH V2 10/26] thermal: cpu_cooling: no need to set cpufreq_dev to NULL Viresh Kumar
2014-12-04  4:11 ` [PATCH V2 11/26] thermal: cpu_cooling: no need to initialze 'ret' Viresh Kumar
2014-12-04  4:11 ` [PATCH V2 12/26] thermal: cpu_cooling: propagate error returned by idr_alloc() Viresh Kumar
2014-12-04  4:11 ` [PATCH V2 13/26] thermal: cpu_cooling: Don't match min/max frequencies for all CPUs on cooling register Viresh Kumar
2014-12-04  4:11 ` [PATCH V2 14/26] thermal: cpu_cooling: don't iterate over all allowed_cpus to update cpufreq policy Viresh Kumar
2014-12-04  4:11 ` [PATCH V2 15/26] thermal: cpu_cooling: Don't check is_cpufreq_valid() Viresh Kumar
2014-12-04  4:11 ` [PATCH V2 16/26] thermal: cpu_cooling: do error handling at the bottom in __cpufreq_cooling_register() Viresh Kumar
2014-12-04  4:11 ` [PATCH V2 17/26] thermal: cpu_cooling: initialize 'cpufreq_val' on registration Viresh Kumar
2014-12-04  4:12 ` [PATCH V2 18/26] thermal: cpu_cooling: Merge cpufreq_apply_cooling() into cpufreq_set_cur_state() Viresh Kumar
2014-12-04  4:12 ` [PATCH V2 19/26] thermal: cpu_cooling: remove unnecessary wrapper get_cpu_frequency() Viresh Kumar
2014-12-04  4:12 ` [PATCH V2 20/26] thermal: cpu_cooling: find max level during device registration Viresh Kumar
2014-12-04  4:12 ` [PATCH V2 21/26] thermal: cpu_cooling: get_property() doesn't need to support GET_MAXL anymore Viresh Kumar
2014-12-04  4:12 ` [PATCH V2 22/26] thermal: cpu_cooling: use cpufreq_dev_list instead of cpufreq_dev_count Viresh Kumar
2014-12-04  4:12 ` [PATCH V2 23/26] thermal: cpu_cooling: Pass 'cpufreq_dev' to get_property() Viresh Kumar
2014-12-04  4:12 ` [PATCH V2 24/26] thermal: cpu_cooling: Store frequencies in descending order Viresh Kumar
2014-12-04  4:12 ` Viresh Kumar [this message]
2014-12-04 10:52   ` [PATCH V2 25/26] thermal: cpu_cooling: Use cpufreq_dev->freq_table for finding level/freq Javi Merino
2014-12-04 10:56     ` Viresh Kumar
2014-12-04  4:12 ` [PATCH V2 26/26] thermal: cpu_cooling: update copyright tags Viresh Kumar
2014-12-08 20:01 ` [PATCH V2 00/26] thermal: cpu_cooling: Fixes and cleanups Eduardo Valentin
2014-12-09  1:47   ` Viresh Kumar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=45a3ff79a10b0618783f0043efa44966c758b410.1417664938.git.viresh.kumar@linaro.org \
    --to=viresh.kumar@linaro.org \
    --cc=amit.daniel@samsung.com \
    --cc=edubezval@gmail.com \
    --cc=javi.merino@arm.com \
    --cc=linaro-kernel@lists.linaro.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=rui.zhang@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.