Merge tag 'v3.10.60' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-vexpress / tc2_pm_psci.c
1 /*
2  * arch/arm/mach-vexpress/tc2_pm_psci.c - TC2 PSCI support
3  *
4  * Created by: Achin Gupta, December 2012
5  * Copyright:  (C) 2012  ARM Limited
6  *
7  * Some portions of this file were originally written by Nicolas Pitre
8  * Copyright:   (C) 2012  Linaro Limited
9  *
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.
13  */
14
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/spinlock.h>
18 #include <linux/errno.h>
19
20 #include <asm/mcpm.h>
21 #include <asm/proc-fns.h>
22 #include <asm/cacheflush.h>
23 #include <asm/psci.h>
24 #include <asm/atomic.h>
25 #include <asm/cputype.h>
26 #include <asm/cp15.h>
27
28 #include <mach/motherboard.h>
29 #include <mach/tc2.h>
30
31 #include <linux/vexpress.h>
32
33 /*
34  * Platform specific state id understood by the firmware and used to
35  * program the power controller
36  */
37 #define PSCI_POWER_STATE_ID           0
38
39 static atomic_t tc2_pm_use_count[TC2_MAX_CPUS][TC2_MAX_CLUSTERS];
40
41 static int tc2_pm_psci_power_up(unsigned int cpu, unsigned int cluster)
42 {
43         unsigned int mpidr = (cluster << 8) | cpu;
44         int ret = 0;
45
46         BUG_ON(!psci_ops.cpu_on);
47
48         switch (atomic_inc_return(&tc2_pm_use_count[cpu][cluster])) {
49         case 1:
50                 /*
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.
54                  */
55                 do
56                         ret = psci_ops.cpu_on(mpidr,
57                                               virt_to_phys(mcpm_entry_point));
58                 while (ret == -EAGAIN);
59
60                 return ret;
61         case 2:
62                 /* This power up request has overtaken a power down request */
63                 return ret;
64         default:
65                 /* Any other value is a bug */
66                 BUG();
67         }
68 }
69
70 static void tc2_pm_psci_power_down(void)
71 {
72         struct psci_power_state power_state;
73         unsigned int mpidr, cpu, cluster;
74
75         mpidr = read_cpuid_mpidr();
76         cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
77         cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
78
79         BUG_ON(!psci_ops.cpu_off);
80
81         switch (atomic_dec_return(&tc2_pm_use_count[cpu][cluster])) {
82         case 1:
83                 /*
84                  * Overtaken by a power up. Flush caches, exit coherency,
85                  * return & fake a reset
86                  */
87                 set_cr(get_cr() & ~CR_C);
88
89                 flush_cache_louis();
90
91                 asm volatile ("clrex");
92                 set_auxcr(get_auxcr() & ~(1 << 6));
93
94                 return;
95         case 0:
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;
100
101                 psci_ops.cpu_off(power_state);
102
103                 /* On success this function never returns */
104         default:
105                 /* Any other value is a bug */
106                 BUG();
107         }
108 }
109
110 static void tc2_pm_psci_suspend(u64 unused)
111 {
112         struct psci_power_state power_state;
113
114         BUG_ON(!psci_ops.cpu_suspend);
115
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;
120
121         psci_ops.cpu_suspend(power_state, virt_to_phys(mcpm_entry_point));
122
123         /* On success this function never returns */
124         BUG();
125 }
126
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,
131 };
132
133 static void __init tc2_pm_usage_count_init(void)
134 {
135         unsigned int mpidr, cpu, cluster;
136
137         mpidr = read_cpuid_mpidr();
138         cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
139         cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
140
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));
144
145         atomic_set(&tc2_pm_use_count[cpu][cluster], 1);
146 }
147
148 static int __init tc2_pm_psci_init(void)
149 {
150         int ret;
151
152         ret = psci_probe();
153         if (ret) {
154                 pr_debug("psci not found. Aborting psci init\n");
155                 return -ENODEV;
156         }
157
158         if (!vexpress_spc_check_loaded()) {
159                 pr_debug("spc not found. Aborting psci init\n");
160                 return -ENODEV;
161         }
162
163         tc2_pm_usage_count_init();
164
165         ret = mcpm_platform_register(&tc2_pm_power_ops);
166         if (!ret)
167                 ret = mcpm_sync_init(NULL);
168         if (!ret)
169                 pr_info("TC2 power management initialized\n");
170         return ret;
171 }
172
173 early_initcall(tc2_pm_psci_init);