Skip to contentSkip to conversation

Cleanups for cpufreq

PATCH Superseded Nishanth Menon 3 participants 15 messages May 18, 2011 – May 25, 2011 1 message not in the archive

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [PM-WIP_CPUFREQ][PATCH 0/5] Cleanups for cpufreq

#1 lore raw

v2 cover Cleanups for cpufreq

Rev 2 of cleanups for cpufreq to include a couple of bugfixes we found as part of additional dvfs testing and code review.

Testing done: OMAP4SDP4430 +.39-rc7- I think the only other major platform would be omap2 - but I doubt it was functional previously - if someone has a chance to give it a whirl, please do, as I dont have access to an OMAP2 platform atm :(.

Applies on top of:
git://git.kernel.org/pub/scm/linux/kernel/git/khilman/linux-omap-pm.git
branch: pm-wip/cpufreq

Changes since V1: http://marc.info/?l=linux-omap&m=130532085617487&w=2
Patch 5 was reworked - return condition handling + lesser spam error msg Patch 6 was added - caught as part of kmemleak check

Nishanth Menon (6):
OMAP2+: cpufreq: free up table on exit
OMAP2+: cpufreq: handle invalid cpufreq table
OMAP2+: cpufreq: minor comment cleanup
OMAP2: cpufreq: use clk_init_cpufreq_table if OPPs not available OMAP2+: cpufreq: use cpufreq_frequency_table_target OMAP2+: cpufreq: fix freq_table leak

 arch/arm/mach-omap2/omap2plus-cpufreq.c |   81 ++++++++++++++++++++++++++-----
 1 files changed, 69 insertions(+), 12 deletions(-)

Regards,
Nishanth Menon

#2 lore raw

PATCH v2 1/6 OMAP2+: cpufreq: free up table on exit

In reply to a message not in the archive

freq_table allocated by opp_init_cpufreq_table in omap_cpu_init needs to be freed in omap_cpu_exit.

---
 arch/arm/mach-omap2/omap2plus-cpufreq.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
arch/arm/mach-omap2/omap2plus-cpufreq.c +3 -0
index d53ce23..e38ebb8 100644
@@ -26,6 +26,7 @@
 #include <linux/clk.h>
 #include <linux/io.h>
 #include <linux/opp.h>
+#include <linux/slab.h>
 #include <linux/cpu.h>
 
 #include <asm/system.h>
@@ -216,6 +217,8 @@ static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy)
 static int omap_cpu_exit(struct cpufreq_policy *policy)
 {
 	clk_exit_cpufreq_table(&freq_table);
+	kfree(freq_table);
+	freq_table = NULL;
 	clk_put(mpu_clk);
 	return 0;
 }
Signature · 1 line
1.7.1
#3 lore raw

Re: [PM-WIP_CPUFREQ][PATCH 1/6 v2] OMAP2+: cpufreq: free up table on exit

Quoted 2 lines · Nishanth Menon

freq_table allocated by opp_init_cpufreq_table in omap_cpu_init needs to be freed in omap_cpu_exit.

Actually it needs to be freed by a corresponding OPP layer function.

IOW, what happens if the OPP core code switches from using kmalloc to
static tables, or something else? The cleanup should be done by the
same layer that does the init/alloc.

Kevin

Quoted 26 lines
---
 arch/arm/mach-omap2/omap2plus-cpufreq.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
arch/arm/mach-omap2/omap2plus-cpufreq.c +3 -0
index d53ce23..e38ebb8 100644
@@ -26,6 +26,7 @@
 #include <linux/clk.h>
 #include <linux/io.h>
 #include <linux/opp.h>
+#include <linux/slab.h>
 #include <linux/cpu.h>
 
 #include <asm/system.h>
@@ -216,6 +217,8 @@ static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy)
 static int omap_cpu_exit(struct cpufreq_policy *policy)
 {
 	clk_exit_cpufreq_table(&freq_table);
+	kfree(freq_table);
+	freq_table = NULL;
 	clk_put(mpu_clk);
 	return 0;
 }
#4 lore raw

Re: [PM-WIP_CPUFREQ][PATCH 1/6 v2] OMAP2+: cpufreq: free up table on exit

Quoted 10 lines · Kevin Hilman
Quoted 2 lines · Nishanth Menon

freq_table allocated by opp_init_cpufreq_table in omap_cpu_init needs to be freed in omap_cpu_exit.

Actually it needs to be freed by a corresponding OPP layer function.

IOW, what happens if the OPP core code switches from using kmalloc to static tables, or something else?    The cleanup should be done by the same layer that does the init/alloc.

Agreed. We can introduce a new api in OPP layer to free up as well. I will post a patch for it.
Regards,
Nishanth Menon

Quoted 18 lines

Kevin

Quoted 16 lines

---
 arch/arm/mach-omap2/omap2plus-cpufreq.c |    3 +++

 1 files changed, 3 insertions(+), 0 deletions(-)
arch/arm/mach-omap2/omap2plus-cpufreq.c +1 -0
index d53ce23..e38ebb8 100644
@@ -26,6 +26,7 @@
  #include <linux/clk.h>
  #include <linux/io.h>
  #include <linux/opp.h>
+#include <linux/slab.h>
  #include <linux/cpu.h>
 
Quoted 11 lines

arch/arm/mach-omap2/omap2plus-cpufreq.c static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy) · line 224

Quoted 10 lines
arch/arm/mach-omap2/omap2plus-cpufreq.c +2 -0
  #include <asm/system.h>
@@ -216,6 +217,8 @@ static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy)
  static int omap_cpu_exit(struct cpufreq_policy *policy)
  {
       clk_exit_cpufreq_table(&freq_table);
+     kfree(freq_table);
+     freq_table = NULL;
       clk_put(mpu_clk);
       return 0;
  }

--
To unsubscribe from this list: send the line "unsubscribe linux-omap" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw

PATCH v2 2/6 OMAP2+: cpufreq: handle invalid cpufreq table

In reply to a message not in the archive

Handle the case when cpufreq_frequency_table_cpuinfo fails. freq_table that we passed failed the internal test of cpufreq generic driver, so we should'nt be using the freq_table as such. Instead, warn and fallback to clock functions for validation and operation.

---
 arch/arm/mach-omap2/omap2plus-cpufreq.c |   12 ++++++++++--
 1 files changed, 10 insertions(+), 2 deletions(-)
arch/arm/mach-omap2/omap2plus-cpufreq.c +10 -2
index e38ebb8..6e3666a 100644
@@ -182,10 +182,18 @@ static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy)
 
 	if (freq_table) {
 		result = cpufreq_frequency_table_cpuinfo(policy, freq_table);
-		if (!result)
+		if (!result) {
 			cpufreq_frequency_table_get_attr(freq_table,
 							policy->cpu);
-	} else {
+		} else {
+			WARN(true, "%s: fallback to clk_round(freq_table=%d)\n",
+					__func__, result);
+			kfree(freq_table);
+			freq_table = NULL;
+		}
+	}
+
+	if (!freq_table) {
 		policy->cpuinfo.min_freq = clk_round_rate(mpu_clk, 0) / 1000;
 		policy->cpuinfo.max_freq = clk_round_rate(mpu_clk,
 							VERY_HI_RATE) / 1000;
Signature · 1 line
1.7.1
#6 lore raw

PATCH v2 3/6 OMAP2+: cpufreq: minor comment cleanup

In reply to a message not in the archive

this should probably get squashed in..

---
 arch/arm/mach-omap2/omap2plus-cpufreq.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
arch/arm/mach-omap2/omap2plus-cpufreq.c +4 -2
index 6e3666a..45f1e9e 100644
@@ -84,8 +84,10 @@ static int omap_target(struct cpufreq_policy *policy,
 	if (is_smp() && (num_online_cpus() < NR_CPUS))
 		return ret;
 
-	/* Ensure desired rate is within allowed range.  Some govenors
-	 * (ondemand) will just pass target_freq=0 to get the minimum. */
+	/*
+	 * Ensure desired rate is within allowed range.  Some govenors
+	 * (ondemand) will just pass target_freq=0 to get the minimum.
+	 */
 	if (target_freq < policy->min)
 		target_freq = policy->min;
 	if (target_freq > policy->max)
Signature · 1 line
1.7.1
#7 lore raw

Re: [PM-WIP_CPUFREQ][PATCH 3/6 v2] OMAP2+: cpufreq: minor comment cleanup

Quoted 12 lines · Nishanth Menon

this should probably get squashed in..

---
 arch/arm/mach-omap2/omap2plus-cpufreq.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
arch/arm/mach-omap2/omap2plus-cpufreq.c +0 -0
index 6e3666a..45f1e9e 100644

arch/arm/mach-omap2/omap2plus-cpufreq.c static int omap_target(struct cpufreq_policy *policy, · line 89

Quoted 8 lines
arch/arm/mach-omap2/omap2plus-cpufreq.c +3 -2
@@ -84,8 +84,10 @@ static int omap_target(struct cpufreq_policy *policy,
 	if (is_smp() && (num_online_cpus() < NR_CPUS))
 		return ret;
 
-	/* Ensure desired rate is within allowed range.  Some govenors
-	 * (ondemand) will just pass target_freq=0 to get the minimum. */
+	/*
+	 * Ensure desired rate is within allowed range.  Some govenors
+	 * (ondemand) will just pass target_freq=0 to get the minimum.

So long as the comment is being touched up, could fixup the typo for "governor" (and also copied to a new block of code in another patch in the series).

Quoted 11 lines
arch/arm/mach-omap2/omap2plus-cpufreq.c +1 -0
+	 */
 	if (target_freq < policy->min)
 		target_freq = policy->min;
 	if (target_freq > policy->max)
Signature · 6 lines
1.7.1

--
To unsubscribe from this list: send the line "unsubscribe linux-omap" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#8 lore raw

Re: [PM-WIP_CPUFREQ][PATCH 3/6 v2] OMAP2+: cpufreq: minor comment cleanup

Quoted 16 lines · Todd Poynor
Quoted 15 lines · Nishanth Menon

this should probably get squashed in..

---
 arch/arm/mach-omap2/omap2plus-cpufreq.c |    6 ++++--

 1 files changed, 4 insertions(+), 2 deletions(-)
arch/arm/mach-omap2/omap2plus-cpufreq.c +0 -0
index 6e3666a..45f1e9e 100644
@@ -84,8 +84,10 @@ static int omap_target(struct cpufreq_policy *policy,
       if (is_smp() && (num_online_cpus() < NR_CPUS))
               return ret;
 
Quoted 9 lines

arch/arm/mach-omap2/omap2plus-cpufreq.c static int omap_target(struct cpufreq_policy *policy, · line 89

Quoted 5 lines
arch/arm/mach-omap2/omap2plus-cpufreq.c +3 -2
-     /* Ensure desired rate is within allowed range.  Some govenors
-      * (ondemand) will just pass target_freq=0 to get the minimum. */
+     /*
+      * Ensure desired rate is within allowed range.  Some govenors
+      * (ondemand) will just pass target_freq=0 to get the minimum.

So long as the comment is being touched up, could fixup the typo for "governor" (and also copied to a new block of code in another patch in the series).

Will do. thanks for catching it :).

Regards,
Nishanth Menon
--
To unsubscribe from this list: send the line "unsubscribe linux-omap" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#9 lore raw

PATCH v2 4/6 OMAP2: cpufreq: use clk_init_cpufreq_table if OPPs not available

In reply to a message not in the archive

OMAP2 does not use OPP tables at the moment for DVFS. Currently, we depend on opp table initialization to give us the freq_table, which makes sense for OMAP3+. for OMAP2, we should be using clk_init_cpufreq_table - so if the opp based frequency table initilization fails, fall back to clk_init_cpufreq_table to give us the table.

---
 arch/arm/mach-omap2/omap2plus-cpufreq.c |    9 ++++++++-
 1 files changed, 8 insertions(+), 1 deletions(-)
arch/arm/mach-omap2/omap2plus-cpufreq.c +8 -1
index 45f1e9e..854f4b3 100644
@@ -180,7 +180,13 @@ static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy)
 		pr_warning("%s: unable to get the mpu device\n", __func__);
 		return -EINVAL;
 	}
-	opp_init_cpufreq_table(mpu_dev, &freq_table);
+
+	/*
+	 * if we dont get cpufreq table using opp, use traditional omap2 lookup
+	 * as a fallback
+	 */
+	if (opp_init_cpufreq_table(mpu_dev, &freq_table))
+		clk_init_cpufreq_table(&freq_table);
 
 	if (freq_table) {
 		result = cpufreq_frequency_table_cpuinfo(policy, freq_table);
@@ -188,6 +194,7 @@ static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy)
 			cpufreq_frequency_table_get_attr(freq_table,
 							policy->cpu);
 		} else {
+			clk_exit_cpufreq_table(&freq_table);
 			WARN(true, "%s: fallback to clk_round(freq_table=%d)\n",
 					__func__, result);
 			kfree(freq_table);
Signature · 1 line
1.7.1
#10 lore raw

Re: [PM-WIP_CPUFREQ][PATCH 4/6 v2] OMAP2: cpufreq: use clk_init_cpufreq_table if OPPs not available

Quoted 8 lines · Nishanth Menon

OMAP2 does not use OPP tables at the moment for DVFS. Currently, we depend on opp table initialization to give us the freq_table, which makes sense for OMAP3+. for OMAP2, we should be using clk_init_cpufreq_table - so if the opp based frequency table initilization fails, fall back to clk_init_cpufreq_table to give us the table.

This is a good approach, but for readability, the OPP version and the clk version should probably be separated into separate functions, along with their error handling.

Minor: please capitalize acronyms: OPP, CPU, OMAP, etc...

Kevin

Quoted 31 lines
---
 arch/arm/mach-omap2/omap2plus-cpufreq.c |    9 ++++++++-
 1 files changed, 8 insertions(+), 1 deletions(-)
arch/arm/mach-omap2/omap2plus-cpufreq.c +8 -1
index 45f1e9e..854f4b3 100644
@@ -180,7 +180,13 @@ static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy)
 		pr_warning("%s: unable to get the mpu device\n", __func__);
 		return -EINVAL;
 	}
-	opp_init_cpufreq_table(mpu_dev, &freq_table);
+
+	/*
+	 * if we dont get cpufreq table using opp, use traditional omap2 lookup
+	 * as a fallback
+	 */
+	if (opp_init_cpufreq_table(mpu_dev, &freq_table))
+		clk_init_cpufreq_table(&freq_table);
 
 	if (freq_table) {
 		result = cpufreq_frequency_table_cpuinfo(policy, freq_table);
@@ -188,6 +194,7 @@ static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy)
 			cpufreq_frequency_table_get_attr(freq_table,
 							policy->cpu);
 		} else {
+			clk_exit_cpufreq_table(&freq_table);
 			WARN(true, "%s: fallback to clk_round(freq_table=%d)\n",
 					__func__, result);
 			kfree(freq_table);
#11 lore raw

Re: [PM-WIP_CPUFREQ][PATCH 4/6 v2] OMAP2: cpufreq: use clk_init_cpufreq_table if OPPs not available

Quoted 9 lines · Kevin Hilman
Quoted 7 lines · Nishanth Menon

OMAP2 does not use OPP tables at the moment for DVFS. Currently, we depend on opp table initialization to give us the freq_table, which makes sense for OMAP3+. for OMAP2, we should be using clk_init_cpufreq_table - so if the opp based frequency table initilization fails, fall back to clk_init_cpufreq_table to give us the table.

Quoted 5 lines
Quoted 1 line

This is a good approach, but for readability, the OPP version and the clk version should probably be separated into separate functions, along with their error handling.

Was thinking more of the lines of splitting the file up. OMAP3+ all have OPPs defined. only one pending is OMAP2
Either we introduce OPPs to OMAP2 OR we split it up and depend the OPP based stuff on ARCH_HAS_OPP and CPUFREQ

Quoted 2 lines

Minor: please capitalize acronyms: OPP, CPU, OMAP, etc...

will do.
Regards,
Nishanth Menon

Quoted 24 lines

Kevin

Quoted 21 lines

---
 arch/arm/mach-omap2/omap2plus-cpufreq.c |    9 ++++++++-

 1 files changed, 8 insertions(+), 1 deletions(-)
arch/arm/mach-omap2/omap2plus-cpufreq.c +7 -1
index 45f1e9e..854f4b3 100644
@@ -180,7 +180,13 @@ static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy)
               pr_warning("%s: unable to get the mpu device\n", __func__);
               return -EINVAL;
       }
-     opp_init_cpufreq_table(mpu_dev, &freq_table);
+
+     /*
+      * if we dont get cpufreq table using opp, use traditional omap2 lookup
+      * as a fallback
+      */
+     if (opp_init_cpufreq_table(mpu_dev, &freq_table))
+             clk_init_cpufreq_table(&freq_table);
 
Quoted 11 lines

arch/arm/mach-omap2/omap2plus-cpufreq.c static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy) · line 200

Quoted 10 lines
arch/arm/mach-omap2/omap2plus-cpufreq.c +1 -0
       if (freq_table) {
               result = cpufreq_frequency_table_cpuinfo(policy, freq_table);
@@ -188,6 +194,7 @@ static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy)
                       cpufreq_frequency_table_get_attr(freq_table,
                                                       policy->cpu);
               } else {
+                     clk_exit_cpufreq_table(&freq_table);
                       WARN(true, "%s: fallback to clk_round(freq_table=%d)\n",
                                       __func__, result);
                       kfree(freq_table);

--
To unsubscribe from this list: send the line "unsubscribe linux-omap" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#12 lore raw

Re: [PM-WIP_CPUFREQ][PATCH 4/6 v2] OMAP2: cpufreq: use clk_init_cpufreq_table if OPPs not available

Quoted 12 lines · Nishanth
Quoted 11 lines · Kevin Hilman
Quoted 8 lines · Nishanth Menon

OMAP2 does not use OPP tables at the moment for DVFS. Currently, we depend on opp table initialization to give us the freq_table, which makes sense for OMAP3+. for OMAP2, we should be using clk_init_cpufreq_table - so if the opp based frequency table initilization fails, fall back to clk_init_cpufreq_table to give us the table.

Quoted 8 lines
Quoted 3 lines

This is a good approach, but for readability, the OPP version and the clk version should probably be separated into separate functions, along with their error handling.

Was thinking more of the lines of splitting the file up. OMAP3+ all have OPPs defined. only one pending is OMAP2
Either we introduce OPPs to OMAP2 OR we split it up and depend the OPP based stuff on ARCH_HAS_OPP and CPUFREQ

Let's take the latter approach, and just focus on a single OPP-based driver.

When someone wants to add DVFS for OMAP2, all that's necessary is to add the OPPs.

Kevin

#13 lore raw

Re: [PM-WIP_CPUFREQ][PATCH 4/6 v2] OMAP2: cpufreq: use clk_init_cpufreq_table if OPPs not available

Quoted 23 lines · Kevin Hilman
Quoted 12 lines · Nishanth
Quoted 11 lines · Kevin Hilman
Quoted 8 lines · Nishanth Menon

OMAP2 does not use OPP tables at the moment for DVFS. Currently, we depend on opp table initialization to give us the freq_table, which makes sense for OMAP3+. for OMAP2, we should be using clk_init_cpufreq_table - so if the opp based frequency table initilization fails, fall back to clk_init_cpufreq_table to give us the table.

Quoted 8 lines
Quoted 3 lines

This is a good approach, but for readability, the OPP version and the clk version should probably be separated into separate functions, along with their error handling.

Was thinking more of the lines of splitting the file up. OMAP3+ all have OPPs defined. only one pending is OMAP2
Either we introduce OPPs to OMAP2 OR we split it up and depend the OPP based stuff on ARCH_HAS_OPP and CPUFREQ

Quoted 4 lines

Let's take the latter approach, and just focus on a single OPP-based driver.

When someone wants to add DVFS for OMAP2, all that's necessary is to add the OPPs.

yes, I have isolated the code to do that earlier today.. hopefully I should get time to post this out asap.

Regards,
Nishanth Menon

#14 lore raw

PATCH v2 5/6 OMAP2+: cpufreq: use cpufreq_frequency_table_target

In reply to a message not in the archive

Use cpufreq_frequency_table_target for finding the proper target instead of seeing if the frequency requested is divisible alone. if we have a frequency table, we should restrict ourselves to selecting the "approved" frequencies alone and only in the case where the frequency table is not available should we attempt at closest roundable clock frequency.

---
 arch/arm/mach-omap2/omap2plus-cpufreq.c |   38 ++++++++++++++++++++++--------
 1 files changed, 28 insertions(+), 10 deletions(-)
arch/arm/mach-omap2/omap2plus-cpufreq.c +28 -10
index 854f4b3..d0b4f97 100644
@@ -77,24 +77,42 @@ static int omap_target(struct cpufreq_policy *policy,
 		       unsigned int target_freq,
 		       unsigned int relation)
 {
-	int i, ret = 0;
+	unsigned int i;
+	int ret = 0;
 	struct cpufreq_freqs freqs;
 
 	/* Changes not allowed until all CPUs are online */
 	if (is_smp() && (num_online_cpus() < NR_CPUS))
 		return ret;
 
-	/*
-	 * Ensure desired rate is within allowed range.  Some govenors
-	 * (ondemand) will just pass target_freq=0 to get the minimum.
-	 */
-	if (target_freq < policy->min)
-		target_freq = policy->min;
-	if (target_freq > policy->max)
-		target_freq = policy->max;
+	if (freq_table) {
+		ret = cpufreq_frequency_table_target(policy, freq_table,
+				target_freq, relation, &i);
+		if (ret) {
+			pr_debug("%s: cpu%d: no freq match for %d(ret=%d)\n",
+				__func__, policy->cpu, target_freq, ret);
+			return ret;
+		}
+		freqs.new = freq_table[i].frequency;
+	} else {
+		/*
+		 * Ensure desired rate is within allowed range. Some govenors
+		 * (ondemand) will just pass target_freq=0 to get the minimum.
+		 */
+		if (target_freq < policy->min)
+			target_freq = policy->min;
+		if (target_freq > policy->max)
+			target_freq = policy->max;
+
+		freqs.new = clk_round_rate(mpu_clk, target_freq * 1000) / 1000;
+	}
+	if (!freqs.new) {
+		pr_err("%s: cpu%d: no match for freq %d\n", __func__,
+			policy->cpu, target_freq);
+		return -EINVAL;
+	}
 
 	freqs.old = omap_getspeed(policy->cpu);
-	freqs.new = clk_round_rate(mpu_clk, target_freq * 1000) / 1000;
 	freqs.cpu = policy->cpu;
 
 	if (freqs.old == freqs.new)
Signature · 1 line
1.7.1
#15 lore raw

PATCH v2 6/6 OMAP2+: cpufreq: fix freq_table leak

In reply to a message not in the archive

Since we have two cpus the cpuinit call for cpu1 causes freq_table of cpu0 to be overwritten. instead, we maintain a counter to keep track of cpus who use the cpufreq table allocate it once(one freq table for all CPUs) and free them once the last user is done with it.

---
 arch/arm/mach-omap2/omap2plus-cpufreq.c |   33 ++++++++++++++++++++++++------
 1 files changed, 26 insertions(+), 7 deletions(-)
arch/arm/mach-omap2/omap2plus-cpufreq.c +26 -7
index d0b4f97..fc3d0fb 100644
@@ -42,6 +42,9 @@
 #define VERY_HI_RATE	900000000
 
 static struct cpufreq_frequency_table *freq_table;
+static int freq_table_users;
+static DEFINE_MUTEX(freq_table_lock);
+
 static struct clk *mpu_clk;
 
 static int omap_verify_speed(struct cpufreq_policy *policy)
@@ -172,6 +175,18 @@ skip_lpj:
 	return ret;
 }
 
+static void freq_table_free(void)
+{
+	if (!freq_table_users)
+		return;
+	freq_table_users--;
+	if (freq_table_users)
+		return;
+	clk_exit_cpufreq_table(&freq_table);
+	kfree(freq_table);
+	freq_table = NULL;
+}
+
 static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy)
 {
 	int result = 0;
@@ -199,14 +214,18 @@ static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy)
 		return -EINVAL;
 	}
 
+	mutex_lock(&freq_table_lock);
 	/*
 	 * if we dont get cpufreq table using opp, use traditional omap2 lookup
 	 * as a fallback
 	 */
-	if (opp_init_cpufreq_table(mpu_dev, &freq_table))
-		clk_init_cpufreq_table(&freq_table);
+	if (!freq_table) {
+		if (opp_init_cpufreq_table(mpu_dev, &freq_table))
+			clk_init_cpufreq_table(&freq_table);
+	}
 
 	if (freq_table) {
+		freq_table_users++;
 		result = cpufreq_frequency_table_cpuinfo(policy, freq_table);
 		if (!result) {
 			cpufreq_frequency_table_get_attr(freq_table,
@@ -215,10 +234,10 @@ static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy)
 			clk_exit_cpufreq_table(&freq_table);
 			WARN(true, "%s: fallback to clk_round(freq_table=%d)\n",
 					__func__, result);
-			kfree(freq_table);
-			freq_table = NULL;
+			freq_table_free();
 		}
 	}
+	mutex_unlock(&freq_table_lock);
 
 	if (!freq_table) {
 		policy->cpuinfo.min_freq = clk_round_rate(mpu_clk, 0) / 1000;
@@ -251,9 +270,9 @@ static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy)
 
 static int omap_cpu_exit(struct cpufreq_policy *policy)
 {
-	clk_exit_cpufreq_table(&freq_table);
-	kfree(freq_table);
-	freq_table = NULL;
+	mutex_lock(&freq_table_lock);
+	freq_table_free();
+	mutex_unlock(&freq_table_lock);
 	clk_put(mpu_clk);
 	return 0;
 }
Signature · 1 line
1.7.1