diff mbox

[v3,1/2] cpufreq/arm-bl-cpufreq: Add simple cpufreq big.LITTLE switcher frontend

Message ID 1333026934-21429-2-git-send-email-dave.martin@linaro.org
State Not Applicable
Headers show

Commit Message

Dave Martin March 29, 2012, 1:15 p.m. UTC
This patch adds a very simple cpufreq-based frontend to the ARM
big.LITTLE switcher.

This driver simply simulates two performance points corresponding
to the big and little clusters.

Note that this driver requires the ARM switcher implementation to
be loaded in order to work.  The switcher must have been built with
ASYNC = FALSE in big-little/Makefile in order for the synchronous
switching interface to be exposed.

Bugs and limitations:

  * Switching twice in quick succession currently tends to cause a
    deadlock inside the switcher.  I still need to identify exactly
    what is going wrong here.

  * There is currently no tracing interface, and no interface for
    reporting what the dummy frequencies exposed by the driver
    actually mean in terms of real cluster / performance point
    combinations.  For the very simple case supported,
    cpuinfo_max_freq corresponds to big and cpuinfo_min_freq
    corresponds to LITTLE.

  * cpufreq will trigger spurious extra cluster switches.  This is
    a "feature" since I didn't tell cpufreq that this matters, but
    it may not be desirable.  Setting policy->cpus can probably fix
    this.

Signed-off-by: Dave Martin <dave.martin@linaro.org>
---
 Documentation/cpu-freq/cpufreq-arm-bl.txt |   47 ++++++
 arch/arm/Kconfig                          |    1 +
 drivers/cpufreq/Kconfig.arm               |   18 +++
 drivers/cpufreq/Makefile                  |    4 +
 drivers/cpufreq/arm-bl-cpufreq.h          |   13 ++
 drivers/cpufreq/arm-bl-cpufreq_driver.c   |  216 +++++++++++++++++++++++++++++
 drivers/cpufreq/arm-bl-cpufreq_hvc.S      |   15 ++
 7 files changed, 314 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/cpu-freq/cpufreq-arm-bl.txt
 create mode 100644 drivers/cpufreq/arm-bl-cpufreq.h
 create mode 100644 drivers/cpufreq/arm-bl-cpufreq_driver.c
 create mode 100644 drivers/cpufreq/arm-bl-cpufreq_hvc.S

Comments

Grant Likely April 3, 2012, 3:12 p.m. UTC | #1
On Thu, 29 Mar 2012 14:15:33 +0100, Dave Martin <dave.martin@linaro.org> wrote:
> This patch adds a very simple cpufreq-based frontend to the ARM
> big.LITTLE switcher.
> 
> This driver simply simulates two performance points corresponding
> to the big and little clusters.
> 
> Note that this driver requires the ARM switcher implementation to
> be loaded in order to work.  The switcher must have been built with
> ASYNC = FALSE in big-little/Makefile in order for the synchronous
> switching interface to be exposed.
> 
> Bugs and limitations:
> 
>   * Switching twice in quick succession currently tends to cause a
>     deadlock inside the switcher.  I still need to identify exactly
>     what is going wrong here.
> 
>   * There is currently no tracing interface, and no interface for
>     reporting what the dummy frequencies exposed by the driver
>     actually mean in terms of real cluster / performance point
>     combinations.  For the very simple case supported,
>     cpuinfo_max_freq corresponds to big and cpuinfo_min_freq
>     corresponds to LITTLE.
> 
>   * cpufreq will trigger spurious extra cluster switches.  This is
>     a "feature" since I didn't tell cpufreq that this matters, but
>     it may not be desirable.  Setting policy->cpus can probably fix
>     this.
> 
> Signed-off-by: Dave Martin <dave.martin@linaro.org>

Hi Dave,

Comment below.

> +static int __init bl_cpufreq_module_init(void)
> +{
> +	int err;
> +
> +	err = cpufreq_register_driver(&bl_cpufreq_driver);
> +	if(err)
> +		pr_info("cpufreq backend driver registration failed (%d)\n",
> +			err);
> +	else
> +		pr_info("cpufreq backend driver registered.\n");
> +
> +	return err;
> +}
> +module_init(bl_cpufreq_module_init);

For completeness; can you verify in this function that the bL
hypervisor is loaded before registering the driver?

g.
Dave Martin April 4, 2012, 4:34 p.m. UTC | #2
On Tue, Apr 3, 2012 at 4:12 PM, Grant Likely <grant.likely@secretlab.ca> wrote:
> On Thu, 29 Mar 2012 14:15:33 +0100, Dave Martin <dave.martin@linaro.org> wrote:
>> This patch adds a very simple cpufreq-based frontend to the ARM
>> big.LITTLE switcher.
>>
>> This driver simply simulates two performance points corresponding
>> to the big and little clusters.
>>
>> Note that this driver requires the ARM switcher implementation to
>> be loaded in order to work.  The switcher must have been built with
>> ASYNC = FALSE in big-little/Makefile in order for the synchronous
>> switching interface to be exposed.
>>
>> Bugs and limitations:
>>
>>   * Switching twice in quick succession currently tends to cause a
>>     deadlock inside the switcher.  I still need to identify exactly
>>     what is going wrong here.
>>
>>   * There is currently no tracing interface, and no interface for
>>     reporting what the dummy frequencies exposed by the driver
>>     actually mean in terms of real cluster / performance point
>>     combinations.  For the very simple case supported,
>>     cpuinfo_max_freq corresponds to big and cpuinfo_min_freq
>>     corresponds to LITTLE.
>>
>>   * cpufreq will trigger spurious extra cluster switches.  This is
>>     a "feature" since I didn't tell cpufreq that this matters, but
>>     it may not be desirable.  Setting policy->cpus can probably fix
>>     this.
>>
>> Signed-off-by: Dave Martin <dave.martin@linaro.org>
>
> Hi Dave,
>
> Comment below.
>
>> +static int __init bl_cpufreq_module_init(void)
>> +{
>> +     int err;
>> +
>> +     err = cpufreq_register_driver(&bl_cpufreq_driver);
>> +     if(err)
>> +             pr_info("cpufreq backend driver registration failed (%d)\n",
>> +                     err);
>> +     else
>> +             pr_info("cpufreq backend driver registered.\n");
>> +
>> +     return err;
>> +}
>> +module_init(bl_cpufreq_module_init);
>
> For completeness; can you verify in this function that the bL
> hypervisor is loaded before registering the driver?

Agreed, we definitely want this, but for now it's not possible.

I need to propose a DT binding to flag the presence of absence of the
switcher HVC interface -- it's on my todo list.

Cheers
---Dave
diff mbox

Patch

diff --git a/Documentation/cpu-freq/cpufreq-arm-bl.txt b/Documentation/cpu-freq/cpufreq-arm-bl.txt
new file mode 100644
index 0000000..52e2f3a
--- /dev/null
+++ b/Documentation/cpu-freq/cpufreq-arm-bl.txt
@@ -0,0 +1,47 @@ 
+Synchronous cluster switching interface for the ARM big.LITTLE switcher
+-----------------------------------------------------------------------
+
+The arm-bl-cpufreq driver provides a simple interface which models two
+clusters as two performance points.
+
+Within each CPU's cpufreq directory in sysfs
+(/sys/devices/system/cpu/cpu?/cpufreq/):
+
+cpuinfo_max_freq:
+
+	reports the dummy frequency value which corresponds to the "big"
+	cluster.
+
+cpuinfo_min_freq:
+
+	reports the dummy frequency value which corresponds to the
+	"little" cluster.
+
+cpuinfo_cur_freq:
+
+	reports the dummy frequency corresponding to the currently
+	running cluster.
+
+
+To switch clusters, either the built-in "powersave" or "performance"
+governors can be used to force the "little" or "big" cluster
+respectively; or alternatively the "userspace" governor can be used,
+
+The following script fragment demonstrates how the userspace governor
+can be used to switch:
+
+
+for x in /sys/devices/system/cpu/cpu[0-9]*; do
+	echo userspace >$x/cpufreq/scaling_governor
+done
+
+big_freq=`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq`
+little_freq=`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq`
+
+switch_to_big () {
+	echo $big_freq >/sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed
+}
+
+switch_to_little () {
+	echo $little_freq >/sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed
+}
diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
index 455367d..907d44a 100644
--- a/arch/arm/Kconfig
+++ b/arch/arm/Kconfig
@@ -301,6 +301,7 @@  config ARCH_VERSATILE
 config ARCH_VEXPRESS
 	bool "ARM Ltd. Versatile Express family"
 	select ARCH_WANT_OPTIONAL_GPIOLIB
+	select ARCH_HAS_CPUFREQ
 	select ARM_AMBA
 	select ARM_TIMER_SP804
 	select CLKDEV_LOOKUP
diff --git a/drivers/cpufreq/Kconfig.arm b/drivers/cpufreq/Kconfig.arm
index 72a0044..0e4f6d0 100644
--- a/drivers/cpufreq/Kconfig.arm
+++ b/drivers/cpufreq/Kconfig.arm
@@ -30,3 +30,21 @@  config ARM_EXYNOS4210_CPUFREQ
 	  SoC (S5PV310 or S5PC210).
 
 	  If in doubt, say N.
+
+config ARM_BL_CPUFREQ
+	depends on EXPERIMENTAL
+	depends on ARCH_VEXPRESS_DT
+	tristate "Simple cpufreq interface for the ARM big.LITTLE switcher"
+	help
+	  Provides a simple cpufreq interface to control the ARM
+	  big.LITTLE switcher.
+
+	  Note that this code is not currently safe unless the
+	  big.LITTLE switcher binary has been loaded separately by an
+	  external bootloader or firmware before entering the kernel.
+	  Otherwise, you can still build this code as a module,
+	  providing that you don't load it.
+
+	  Refer to Documentation/cpufreq/cpufreq-arm-bl.txt for details.
+
+	  If unsure, say N.
diff --git a/drivers/cpufreq/Makefile b/drivers/cpufreq/Makefile
index a48bc02..ecf492d 100644
--- a/drivers/cpufreq/Makefile
+++ b/drivers/cpufreq/Makefile
@@ -40,6 +40,10 @@  obj-$(CONFIG_X86_CPUFREQ_NFORCE2)	+= cpufreq-nforce2.o
 ##################################################################################
 # ARM SoC drivers
 obj-$(CONFIG_UX500_SOC_DB8500)		+= db8500-cpufreq.o
+obj-$(CONFIG_ARM_BL_CPUFREQ)		+= arm-bl-cpufreq.o
+arm-bl-cpufreq-y			+= arm-bl-cpufreq_driver.o \
+					   arm-bl-cpufreq_hvc.o
+AFLAGS_arm-bl-cpufreq_hvc.o		:= -march=armv7-a
 obj-$(CONFIG_ARM_S3C64XX_CPUFREQ)	+= s3c64xx-cpufreq.o
 obj-$(CONFIG_ARM_S5PV210_CPUFREQ)	+= s5pv210-cpufreq.o
 obj-$(CONFIG_ARM_EXYNOS4210_CPUFREQ)	+= exynos4210-cpufreq.o
diff --git a/drivers/cpufreq/arm-bl-cpufreq.h b/drivers/cpufreq/arm-bl-cpufreq.h
new file mode 100644
index 0000000..2f9b0dc
--- /dev/null
+++ b/drivers/cpufreq/arm-bl-cpufreq.h
@@ -0,0 +1,13 @@ 
+#ifndef ARM_BL_CPUFREQ_HVC_H
+#define ARM_BL_CPUFREQ_HVC_H
+
+#ifndef __ASSEMBLY__
+int __arm_bl_get_cluster(void);
+void __arm_bl_switch_cluster(void);
+#endif /* ! __ASSEMBLY__ */
+
+/* Hypervisor call numbers for the ARM big.LITTLE switcher: */
+#define ARM_BL_HVC_SWITCH_CLUSTER 1
+#define ARM_BL_HVC_GET_MPIDR 2
+
+#endif /* ! ARM_BL_CPUFREQ_HVC_H */
diff --git a/drivers/cpufreq/arm-bl-cpufreq_driver.c b/drivers/cpufreq/arm-bl-cpufreq_driver.c
new file mode 100644
index 0000000..ffcd03b
--- /dev/null
+++ b/drivers/cpufreq/arm-bl-cpufreq_driver.c
@@ -0,0 +1,216 @@ 
+/*
+ * arm-bl-cpufreq.c: Simple cpufreq backend for the ARM big.LITTLE switcher
+ * Copyright (C) 2012  Linaro Limited
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+/* WARNING: This code is experimental and depends on external firmware */
+
+#define MODULE_NAME "arm-bl-cpufreq"
+#define pr_fmt(fmt) MODULE_NAME ": " fmt
+
+#include <linux/bug.h>
+#include <linux/cache.h>
+#include <linux/cpufreq.h>
+#include <linux/cpumask.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/printk.h>
+#include <linux/string.h>
+#include <linux/spinlock.h>
+
+#include "arm-bl-cpufreq.h"
+
+/* Dummy frequencies representing the big and little clusters: */
+#define FREQ_BIG	1000000
+#define FREQ_LITTLE	 100000
+
+/*  Cluster numbers */
+#define CLUSTER_BIG	0
+#define CLUSTER_LITTLE	1
+
+static DEFINE_SPINLOCK(switcher_lock);
+
+static struct cpufreq_frequency_table __read_mostly bl_freqs[] = {
+	{ CLUSTER_BIG,		FREQ_BIG		},
+	{ CLUSTER_LITTLE,	FREQ_LITTLE		},
+	{ 0,			CPUFREQ_TABLE_END	},
+};
+
+
+/* Miscellaneous helpers */
+
+static unsigned int cluster_to_freq(int cluster)
+{
+	unsigned int i;
+
+	for(i = 0; bl_freqs[i].frequency != CPUFREQ_TABLE_END; i++)
+		if(bl_freqs[i].index == cluster)
+			return bl_freqs[i].frequency;
+
+	WARN(1, pr_fmt("%s(): invalid cluster number %d, assuming 0\n"),
+		__func__, cluster);
+	return bl_freqs[0].frequency;
+}
+
+/*
+ * Functions to get the current status.
+ *
+ * If you intend to use the result (i.e., it's not just for diagnostic
+ * purposes) then you should be holding switcher_lock ... otherwise
+ * the current cluster may change unexpectedly.
+ */
+static int get_current_cluster(void)
+{
+	return (__arm_bl_get_cluster() >> 8) & 0xF;
+}
+
+static unsigned int get_current_freq(void)
+{
+	return cluster_to_freq(get_current_cluster());
+}
+
+/*
+ * Switch to the requested cluster.
+ * There is no "switch_to_frequency" function, because the cpufreq frequency
+ * table helpers can easily look up the appropriate cluster number for us.
+ */
+static void switch_to_cluster(int cluster)
+{
+	pr_info("Switching to cluster %d\n", cluster);
+
+	spin_lock(&switcher_lock);
+	if(cluster != get_current_cluster())
+		__arm_bl_switch_cluster();
+	spin_unlock(&switcher_lock);
+}
+
+
+/* Cpufreq methods and module code */
+
+static int bl_cpufreq_init(struct cpufreq_policy *policy)
+{
+	int err;
+
+	/*
+	 * Set CPU and policy min and max frequencies based on bl_freqs:
+	 */
+	err = cpufreq_frequency_table_cpuinfo(policy, bl_freqs);
+	if (err)
+		goto error;
+
+	/*
+	 * No need for locking here:
+	 * cpufreq is not active until initialisation has finished.
+	 * Ideally, transition_latency should be calibrated here.
+	 */
+	policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
+	policy->cur = get_current_freq();
+
+	/*
+	 * A b.L switch can be triggered from any CPU, but will affect them all.
+	 * The set of related CPUs should perhaps be determined from the
+	 * system CPU topology, rather than just the set of CPUs present...
+	 */
+	policy->shared_type = CPUFREQ_SHARED_TYPE_ANY;
+	cpumask_copy(policy->related_cpus, cpu_present_mask);
+	/*
+	 * We do not set ->cpus here, because it doesn't actually matter if
+	 * we try to switch on two CPUs at the same time.  Setting ->cpus
+	 * to cpu_present_mask might provide a way to avoid the need to take
+	 * switcher_lock when switching, though.
+	 */
+
+	pr_info("cpufreq initialised successfully\n");
+	return 0;
+
+error:
+	pr_warning("cpufreq initialisation failed (%d)\n", err);
+	return err;
+}
+
+static int bl_cpufreq_verify(struct cpufreq_policy *policy)
+{
+	return cpufreq_frequency_table_verify(policy, bl_freqs);
+}
+
+static int bl_cpufreq_target(struct cpufreq_policy *policy,
+			     unsigned int target_freq,
+			     unsigned int relation)
+{
+	int err;
+	int index;
+
+	err = cpufreq_frequency_table_target(policy, bl_freqs, target_freq,
+					     relation, &index);
+	if(err)
+		return err;
+
+	switch_to_cluster(bl_freqs[index].index);
+	return 0;
+}
+
+static unsigned int bl_cpufreq_get(unsigned int __always_unused cpu)
+{
+	/*
+	 * The cpu argument is ignored, because all CPUs have the same
+	 * performance point, by definition.
+	 */
+	return get_current_freq();
+}
+
+static struct cpufreq_driver __read_mostly bl_cpufreq_driver = {
+	.owner = THIS_MODULE,
+	.name = MODULE_NAME,
+
+	.init = bl_cpufreq_init,
+	.verify = bl_cpufreq_verify,
+	.target = bl_cpufreq_target,
+	.get = bl_cpufreq_get,
+	/* what else? */
+};
+
+static int __init bl_cpufreq_module_init(void)
+{
+	int err;
+
+	err = cpufreq_register_driver(&bl_cpufreq_driver);
+	if(err)
+		pr_info("cpufreq backend driver registration failed (%d)\n",
+			err);
+	else
+		pr_info("cpufreq backend driver registered.\n");
+
+	return err;
+}
+module_init(bl_cpufreq_module_init);
+
+static void __exit bl_cpufreq_module_exit(void)
+{
+	cpufreq_unregister_driver(&bl_cpufreq_driver);
+
+	/* Restore the "default" cluster: */
+	switch_to_cluster(CLUSTER_BIG);
+
+	pr_info("cpufreq backend driver unloaded.\n");
+}
+module_exit(bl_cpufreq_module_exit);
+
+
+MODULE_AUTHOR("Dave Martin");
+MODULE_DESCRIPTION("Simple cpufreq interface for the ARM big.LITTLE switcher");
+MODULE_LICENSE("GPL");
diff --git a/drivers/cpufreq/arm-bl-cpufreq_hvc.S b/drivers/cpufreq/arm-bl-cpufreq_hvc.S
new file mode 100644
index 0000000..6c7eb6d
--- /dev/null
+++ b/drivers/cpufreq/arm-bl-cpufreq_hvc.S
@@ -0,0 +1,15 @@ 
+#include <linux/linkage.h>
+
+#include "arm-bl-cpufreq.h"
+
+.arch_extension virt
+
+ENTRY(__arm_bl_get_cluster)
+	hvc	#ARM_BL_HVC_GET_MPIDR
+	bx	lr
+ENDPROC(__arm_bl_get_cluster)
+
+ENTRY(__arm_bl_switch_cluster)
+	hvc	#ARM_BL_HVC_SWITCH_CLUSTER
+	bx	lr
+ENDPROC(__arm_bl_switch_cluster)