ARM: vexpress: add shim layer for psci backend on TC2
authorAchin Gupta <achin.gupta@arm.com>
Tue, 26 Feb 2013 15:33:25 +0000 (15:33 +0000)
committerJon Medhurst <tixy@linaro.org>
Mon, 1 Jul 2013 10:05:15 +0000 (11:05 +0100)
This patch introduces a shim layer for the TC2 platform which converts
'bL_platform_power_ops' routines to their psci counterparts. The psci
counterparts are implemented by the secure firmware. The shim layer
is used only when Linux is running in non-secure world and the secure
firmware implements psci.

It also introduces the use of a reference count to allow a power up call
to go ahead of a power down call.

Signed-off-by: Achin Gupta <achin.gupta@arm.com>
Signed-off-by: Liviu Dudau <Liviu.Dudau@arm.com>
arch/arm/mach-vexpress/Makefile
arch/arm/mach-vexpress/tc2_pm_psci.c [new file with mode: 0644]

index fd9bcfd74316d1222a518001ea82dd9d0386d3b2..59cdec9cbd7e5f86f82649a9b84eab679e7975a7 100644 (file)
@@ -8,5 +8,9 @@ obj-y                                   := v2m.o
 obj-$(CONFIG_ARCH_VEXPRESS_CA9X4)      += ct-ca9x4.o
 obj-$(CONFIG_ARCH_VEXPRESS_TC2)                += tc2_pm.o tc2_pm_setup.o
 CFLAGS_REMOVE_tc2_pm.o                 = -pg
+ifeq ($(CONFIG_ARCH_VEXPRESS_TC2),y)
+obj-$(CONFIG_ARM_PSCI)                 += tc2_pm_psci.o
+CFLAGS_REMOVE_tc2_pm_psci.o            = -pg
+endif
 obj-$(CONFIG_SMP)                      += platsmp.o
 obj-$(CONFIG_HOTPLUG_CPU)              += hotplug.o
diff --git a/arch/arm/mach-vexpress/tc2_pm_psci.c b/arch/arm/mach-vexpress/tc2_pm_psci.c
new file mode 100644 (file)
index 0000000..c9715b8
--- /dev/null
@@ -0,0 +1,179 @@
+/*
+ * arch/arm/mach-vexpress/tc2_pm_psci.c - TC2 PSCI support
+ *
+ * Created by: Achin Gupta, December 2012
+ * Copyright:  (C) 2012  ARM Limited
+ *
+ * Some portions of this file were originally written by Nicolas Pitre
+ * 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 version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/spinlock.h>
+#include <linux/errno.h>
+
+#include <asm/bL_entry.h>
+#include <asm/proc-fns.h>
+#include <asm/cacheflush.h>
+#include <asm/psci.h>
+#include <asm/atomic.h>
+#include <asm/cputype.h>
+
+#include <mach/motherboard.h>
+#include <mach/tc2.h>
+
+#include <linux/vexpress.h>
+
+/*
+ * Platform specific state id understood by the firmware and used to
+ * program the power controller
+ */
+#define PSCI_POWER_STATE_ID           0
+
+static atomic_t tc2_pm_use_count[TC2_MAX_CPUS][TC2_MAX_CLUSTERS];
+
+static int tc2_pm_psci_power_up(unsigned int cpu, unsigned int cluster)
+{
+       unsigned int mpidr = (cluster << 8) | cpu;
+       int ret = 0;
+
+       BUG_ON(!psci_ops.cpu_on);
+
+       switch (atomic_inc_return(&tc2_pm_use_count[cpu][cluster])) {
+       case 1:
+               /*
+                * This is a request to power up a cpu that linux thinks has
+                * been powered down. Retries are needed if the firmware has
+                * seen the power down request as yet.
+                */
+               do
+                       ret = psci_ops.cpu_on(mpidr,
+                                             virt_to_phys(bL_entry_point));
+               while (ret == -EAGAIN);
+
+               return ret;
+       case 2:
+               /* This power up request has overtaken a power down request */
+               return ret;
+       default:
+               /* Any other value is a bug */
+               BUG();
+       }
+}
+
+static void tc2_pm_psci_power_down(void)
+{
+       struct psci_power_state power_state;
+       unsigned int mpidr, cpu, cluster;
+
+       mpidr = read_cpuid_mpidr();
+       cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
+       cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
+
+       BUG_ON(!psci_ops.cpu_off);
+
+       switch (atomic_dec_return(&tc2_pm_use_count[cpu][cluster])) {
+       case 1:
+               /*
+                * Overtaken by a power up. Flush caches, exit coherency,
+                * return & fake a reset
+                */
+               asm volatile (
+                       "mrc    p15, 0, ip, c1, c0, 0 \n\t"
+                       "bic    ip, ip, #(1 << 2) @ clear C bit \n\t"
+                       "mcr    p15, 0, ip, c1, c0, 0 \n\t"
+                       "dsb \n\t"
+                       "isb"
+                       : : : "ip" );
+
+               flush_cache_louis();
+
+               asm volatile (
+                       "clrex  \n\t"
+                       "mrc    p15, 0, ip, c1, c0, 1 \n\t"
+                       "bic    ip, ip, #(1 << 6) @ clear SMP bit \n\t"
+                       "mcr    p15, 0, ip, c1, c0, 1 \n\t"
+                       "isb \n\t"
+                       "dsb"
+                       : : : "ip" );
+
+               return;
+       case 0:
+               /* A normal request to possibly power down the cluster */
+               power_state.id = PSCI_POWER_STATE_ID;
+               power_state.type = PSCI_POWER_STATE_TYPE_POWER_DOWN;
+               power_state.affinity_level = PSCI_POWER_STATE_AFFINITY_LEVEL1;
+
+               psci_ops.cpu_off(power_state);
+
+               /* On success this function never returns */
+       default:
+               /* Any other value is a bug */
+               BUG();
+       }
+}
+
+static void tc2_pm_psci_suspend(u64 unused)
+{
+       struct psci_power_state power_state;
+
+       BUG_ON(!psci_ops.cpu_suspend);
+
+       /* On TC2 always attempt to power down the cluster */
+       power_state.id = PSCI_POWER_STATE_ID;
+       power_state.type = PSCI_POWER_STATE_TYPE_POWER_DOWN;
+       power_state.affinity_level = PSCI_POWER_STATE_AFFINITY_LEVEL1;
+
+       psci_ops.cpu_suspend(power_state, virt_to_phys(bL_entry_point));
+
+       /* On success this function never returns */
+       BUG();
+}
+
+static const struct bL_platform_power_ops tc2_pm_power_ops = {
+       .power_up      = tc2_pm_psci_power_up,
+       .power_down    = tc2_pm_psci_power_down,
+       .suspend       = tc2_pm_psci_suspend,
+};
+
+static void __init tc2_pm_usage_count_init(void)
+{
+       unsigned int mpidr, cpu, cluster;
+
+       mpidr = read_cpuid_mpidr();
+       cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
+       cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
+
+       pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
+       BUG_ON(cluster >= TC2_MAX_CLUSTERS ||
+              cpu >= vexpress_spc_get_nb_cpus(cluster));
+
+       atomic_set(&tc2_pm_use_count[cpu][cluster], 1);
+}
+
+static int __init tc2_pm_psci_init(void)
+{
+       int ret;
+
+       ret = psci_probe();
+       if (ret) {
+               pr_debug("psci not found. Aborting psci init\n");
+               return -ENODEV;
+       }
+
+       tc2_pm_usage_count_init();
+
+       ret = bL_platform_power_register(&tc2_pm_power_ops);
+       if (!ret)
+               ret = bL_cluster_sync_init(NULL);
+       if (!ret)
+               pr_info("TC2 power management initialized\n");
+       return ret;
+}
+
+early_initcall(tc2_pm_psci_init);