2 * arch/arm/mach-vexpress/tc2_pm_psci.c - TC2 PSCI support
4 * Created by: Achin Gupta, December 2012
5 * Copyright: (C) 2012 ARM Limited
7 * Some portions of this file were originally written by Nicolas Pitre
8 * Copyright: (C) 2012 Linaro Limited
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/spinlock.h>
18 #include <linux/errno.h>
21 #include <asm/proc-fns.h>
22 #include <asm/cacheflush.h>
24 #include <asm/atomic.h>
25 #include <asm/cputype.h>
28 #include <mach/motherboard.h>
31 #include <linux/vexpress.h>
34 * Platform specific state id understood by the firmware and used to
35 * program the power controller
37 #define PSCI_POWER_STATE_ID 0
39 static atomic_t tc2_pm_use_count[TC2_MAX_CPUS][TC2_MAX_CLUSTERS];
41 static int tc2_pm_psci_power_up(unsigned int cpu, unsigned int cluster)
43 unsigned int mpidr = (cluster << 8) | cpu;
46 BUG_ON(!psci_ops.cpu_on);
48 switch (atomic_inc_return(&tc2_pm_use_count[cpu][cluster])) {
51 * This is a request to power up a cpu that linux thinks has
52 * been powered down. Retries are needed if the firmware has
53 * seen the power down request as yet.
56 ret = psci_ops.cpu_on(mpidr,
57 virt_to_phys(mcpm_entry_point));
58 while (ret == -EAGAIN);
62 /* This power up request has overtaken a power down request */
65 /* Any other value is a bug */
70 static void tc2_pm_psci_power_down(void)
72 struct psci_power_state power_state;
73 unsigned int mpidr, cpu, cluster;
75 mpidr = read_cpuid_mpidr();
76 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
77 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
79 BUG_ON(!psci_ops.cpu_off);
81 switch (atomic_dec_return(&tc2_pm_use_count[cpu][cluster])) {
84 * Overtaken by a power up. Flush caches, exit coherency,
85 * return & fake a reset
87 set_cr(get_cr() & ~CR_C);
91 asm volatile ("clrex");
92 set_auxcr(get_auxcr() & ~(1 << 6));
96 /* A normal request to possibly power down the cluster */
97 power_state.id = PSCI_POWER_STATE_ID;
98 power_state.type = PSCI_POWER_STATE_TYPE_POWER_DOWN;
99 power_state.affinity_level = PSCI_POWER_STATE_AFFINITY_LEVEL1;
101 psci_ops.cpu_off(power_state);
103 /* On success this function never returns */
105 /* Any other value is a bug */
110 static void tc2_pm_psci_suspend(u64 unused)
112 struct psci_power_state power_state;
114 BUG_ON(!psci_ops.cpu_suspend);
116 /* On TC2 always attempt to power down the cluster */
117 power_state.id = PSCI_POWER_STATE_ID;
118 power_state.type = PSCI_POWER_STATE_TYPE_POWER_DOWN;
119 power_state.affinity_level = PSCI_POWER_STATE_AFFINITY_LEVEL1;
121 psci_ops.cpu_suspend(power_state, virt_to_phys(mcpm_entry_point));
123 /* On success this function never returns */
127 static const struct mcpm_platform_ops tc2_pm_power_ops = {
128 .power_up = tc2_pm_psci_power_up,
129 .power_down = tc2_pm_psci_power_down,
130 .suspend = tc2_pm_psci_suspend,
133 static void __init tc2_pm_usage_count_init(void)
135 unsigned int mpidr, cpu, cluster;
137 mpidr = read_cpuid_mpidr();
138 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
139 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
141 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
142 BUG_ON(cluster >= TC2_MAX_CLUSTERS ||
143 cpu >= vexpress_spc_get_nb_cpus(cluster));
145 atomic_set(&tc2_pm_use_count[cpu][cluster], 1);
148 static int __init tc2_pm_psci_init(void)
154 pr_debug("psci not found. Aborting psci init\n");
158 if (!vexpress_spc_check_loaded()) {
159 pr_debug("spc not found. Aborting psci init\n");
163 tc2_pm_usage_count_init();
165 ret = mcpm_platform_register(&tc2_pm_power_ops);
167 ret = mcpm_sync_init(NULL);
169 pr_info("TC2 power management initialized\n");
173 early_initcall(tc2_pm_psci_init);