blob: 7627fd13e7d87995c2d19ae2ed4a6c38a279b439 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
York Sun56cc3db2014-09-08 12:20:00 -07002/*
Mingkai Hu0e58b512015-10-26 19:47:50 +08003 * Copyright 2014-2015 Freescale Semiconductor, Inc.
York Sun56cc3db2014-09-08 12:20:00 -07004 */
5
6#include <common.h>
7#include <asm/io.h>
8#include <asm/system.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +08009#include <asm/arch/mp.h>
10#include <asm/arch/soc.h>
Priyanka Jain9a276702016-11-17 12:29:56 +053011#include "cpu.h"
12#include <asm/arch-fsl-layerscape/soc.h>
York Sun56cc3db2014-09-08 12:20:00 -070013
14DECLARE_GLOBAL_DATA_PTR;
15
16void *get_spin_tbl_addr(void)
17{
18 return &__spin_table;
19}
20
21phys_addr_t determine_mp_bootpg(void)
22{
23 return (phys_addr_t)&secondary_boot_code;
24}
25
Alison Wang876c7e12016-11-10 10:49:04 +080026void update_os_arch_secondary_cores(uint8_t os_arch)
27{
28 u64 *table = get_spin_tbl_addr();
29 int i;
30
Alison Wanga6231fe2017-06-08 16:15:14 +080031 for (i = 1; i < CONFIG_MAX_CPUS; i++) {
32 if (os_arch == IH_ARCH_DEFAULT)
33 table[i * WORDS_PER_SPIN_TABLE_ENTRY +
34 SPIN_TABLE_ELEM_ARCH_COMP_IDX] = OS_ARCH_SAME;
35 else
36 table[i * WORDS_PER_SPIN_TABLE_ENTRY +
37 SPIN_TABLE_ELEM_ARCH_COMP_IDX] = OS_ARCH_DIFF;
38 }
Alison Wang876c7e12016-11-10 10:49:04 +080039}
40
Priyanka Jain9a276702016-11-17 12:29:56 +053041#ifdef CONFIG_FSL_LSCH3
42void wake_secondary_core_n(int cluster, int core, int cluster_cores)
43{
44 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
45 struct ccsr_reset __iomem *rst = (void *)(CONFIG_SYS_FSL_RST_ADDR);
46 u32 mpidr = 0;
47
48 mpidr = ((cluster << 8) | core);
49 /*
50 * mpidr_el1 register value of core which needs to be released
51 * is written to scratchrw[6] register
52 */
53 gur_out32(&gur->scratchrw[6], mpidr);
54 asm volatile("dsb st" : : : "memory");
55 rst->brrl |= 1 << ((cluster * cluster_cores) + core);
56 asm volatile("dsb st" : : : "memory");
57 /*
58 * scratchrw[6] register value is polled
59 * when the value becomes zero, this means that this core is up
60 * and running, next core can be released now
61 */
62 while (gur_in32(&gur->scratchrw[6]) != 0)
63 ;
64}
65#endif
66
Mingkai Hu0e58b512015-10-26 19:47:50 +080067int fsl_layerscape_wake_seconday_cores(void)
York Sun56cc3db2014-09-08 12:20:00 -070068{
69 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
Hou Zhiqiangc7098fa2015-10-26 19:47:57 +080070#ifdef CONFIG_FSL_LSCH3
York Sun56cc3db2014-09-08 12:20:00 -070071 struct ccsr_reset __iomem *rst = (void *)(CONFIG_SYS_FSL_RST_ADDR);
Priyanka Jain9a276702016-11-17 12:29:56 +053072 u32 svr, ver, cluster, type;
73 int j = 0, cluster_cores = 0;
Hou Zhiqiangc7098fa2015-10-26 19:47:57 +080074#elif defined(CONFIG_FSL_LSCH2)
75 struct ccsr_scfg __iomem *scfg = (void *)(CONFIG_SYS_FSL_SCFG_ADDR);
76#endif
York Sun56cc3db2014-09-08 12:20:00 -070077 u32 cores, cpu_up_mask = 1;
78 int i, timeout = 10;
79 u64 *table = get_spin_tbl_addr();
80
York Sun77a10972015-03-20 19:28:08 -070081#ifdef COUNTER_FREQUENCY_REAL
82 /* update for secondary cores */
83 __real_cntfrq = COUNTER_FREQUENCY_REAL;
84 flush_dcache_range((unsigned long)&__real_cntfrq,
85 (unsigned long)&__real_cntfrq + 8);
86#endif
87
York Sun56cc3db2014-09-08 12:20:00 -070088 cores = cpu_mask();
89 /* Clear spin table so that secondary processors
90 * observe the correct value after waking up from wfe.
91 */
92 memset(table, 0, CONFIG_MAX_CPUS*SPIN_TABLE_ELEM_SIZE);
93 flush_dcache_range((unsigned long)table,
94 (unsigned long)table +
95 (CONFIG_MAX_CPUS*SPIN_TABLE_ELEM_SIZE));
96
97 printf("Waking secondary cores to start from %lx\n", gd->relocaddr);
Mingkai Hu0e58b512015-10-26 19:47:50 +080098
Hou Zhiqiangc7098fa2015-10-26 19:47:57 +080099#ifdef CONFIG_FSL_LSCH3
Mingkai Hu0e58b512015-10-26 19:47:50 +0800100 gur_out32(&gur->bootlocptrh, (u32)(gd->relocaddr >> 32));
101 gur_out32(&gur->bootlocptrl, (u32)gd->relocaddr);
Priyanka Jain9a276702016-11-17 12:29:56 +0530102
103 svr = gur_in32(&gur->svr);
104 ver = SVR_SOC_VER(svr);
105 if (ver == SVR_LS2080A || ver == SVR_LS2085A) {
106 gur_out32(&gur->scratchrw[6], 1);
107 asm volatile("dsb st" : : : "memory");
108 rst->brrl = cores;
109 asm volatile("dsb st" : : : "memory");
110 } else {
111 /*
112 * Release the cores out of reset one-at-a-time to avoid
113 * power spikes
114 */
115 i = 0;
116 cluster = in_le32(&gur->tp_cluster[i].lower);
117 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
118 type = initiator_type(cluster, j);
119 if (type &&
120 TP_ITYP_TYPE(type) == TP_ITYP_TYPE_ARM)
121 cluster_cores++;
122 }
123
124 do {
125 cluster = in_le32(&gur->tp_cluster[i].lower);
126 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
127 type = initiator_type(cluster, j);
128 if (type &&
129 TP_ITYP_TYPE(type) == TP_ITYP_TYPE_ARM)
130 wake_secondary_core_n(i, j,
131 cluster_cores);
132 }
133 i++;
134 } while ((cluster & TP_CLUSTER_EOC) != TP_CLUSTER_EOC);
135 }
Hou Zhiqiangc7098fa2015-10-26 19:47:57 +0800136#elif defined(CONFIG_FSL_LSCH2)
137 scfg_out32(&scfg->scratchrw[0], (u32)(gd->relocaddr >> 32));
138 scfg_out32(&scfg->scratchrw[1], (u32)gd->relocaddr);
139 asm volatile("dsb st" : : : "memory");
140 gur_out32(&gur->brrl, cores);
141 asm volatile("dsb st" : : : "memory");
York Sun56cc3db2014-09-08 12:20:00 -0700142
Hou Zhiqiangc7098fa2015-10-26 19:47:57 +0800143 /* Bootup online cores */
144 scfg_out32(&scfg->corebcr, cores);
145#endif
York Sun56cc3db2014-09-08 12:20:00 -0700146 /* This is needed as a precautionary measure.
147 * If some code before this has accidentally released the secondary
148 * cores then the pre-bootloader code will trap them in a "wfe" unless
149 * the scratchrw[6] is set. In this case we need a sev here to get these
150 * cores moving again.
151 */
152 asm volatile("sev");
153
154 while (timeout--) {
155 flush_dcache_range((unsigned long)table, (unsigned long)table +
156 CONFIG_MAX_CPUS * 64);
157 for (i = 1; i < CONFIG_MAX_CPUS; i++) {
158 if (table[i * WORDS_PER_SPIN_TABLE_ENTRY +
159 SPIN_TABLE_ELEM_STATUS_IDX])
160 cpu_up_mask |= 1 << i;
161 }
162 if (hweight32(cpu_up_mask) == hweight32(cores))
163 break;
164 udelay(10);
165 }
166 if (timeout <= 0) {
167 printf("Not all cores (0x%x) are up (0x%x)\n",
168 cores, cpu_up_mask);
169 return 1;
170 }
171 printf("All (%d) cores are up.\n", hweight32(cores));
172
173 return 0;
174}
175
176int is_core_valid(unsigned int core)
177{
178 return !!((1 << core) & cpu_mask());
179}
180
York Suned7fbe32016-09-13 12:40:30 -0700181static int is_pos_valid(unsigned int pos)
182{
183 return !!((1 << pos) & cpu_pos_mask());
184}
185
Arnab Basu0cb19422015-01-06 13:18:41 -0800186int is_core_online(u64 cpu_id)
187{
188 u64 *table;
189 int pos = id_to_core(cpu_id);
190 table = (u64 *)get_spin_tbl_addr() + pos * WORDS_PER_SPIN_TABLE_ENTRY;
191 return table[SPIN_TABLE_ELEM_STATUS_IDX] == 1;
192}
193
Michal Simek1669e182018-06-13 08:56:31 +0200194int cpu_reset(u32 nr)
York Sun56cc3db2014-09-08 12:20:00 -0700195{
196 puts("Feature is not implemented.\n");
197
198 return 0;
199}
200
Michal Simek1669e182018-06-13 08:56:31 +0200201int cpu_disable(u32 nr)
York Sun56cc3db2014-09-08 12:20:00 -0700202{
203 puts("Feature is not implemented.\n");
204
205 return 0;
206}
207
York Suned7fbe32016-09-13 12:40:30 -0700208static int core_to_pos(int nr)
York Sun56cc3db2014-09-08 12:20:00 -0700209{
York Suned7fbe32016-09-13 12:40:30 -0700210 u32 cores = cpu_pos_mask();
York Sun56cc3db2014-09-08 12:20:00 -0700211 int i, count = 0;
212
213 if (nr == 0) {
214 return 0;
215 } else if (nr >= hweight32(cores)) {
216 puts("Not a valid core number.\n");
217 return -1;
218 }
219
220 for (i = 1; i < 32; i++) {
York Suned7fbe32016-09-13 12:40:30 -0700221 if (is_pos_valid(i)) {
York Sun56cc3db2014-09-08 12:20:00 -0700222 count++;
223 if (count == nr)
224 break;
225 }
226 }
227
York Suned7fbe32016-09-13 12:40:30 -0700228 if (count != nr)
229 return -1;
230
231 return i;
York Sun56cc3db2014-09-08 12:20:00 -0700232}
233
Michal Simek1669e182018-06-13 08:56:31 +0200234int cpu_status(u32 nr)
York Sun56cc3db2014-09-08 12:20:00 -0700235{
236 u64 *table;
237 int pos;
238
239 if (nr == 0) {
240 table = (u64 *)get_spin_tbl_addr();
241 printf("table base @ 0x%p\n", table);
242 } else {
243 pos = core_to_pos(nr);
244 if (pos < 0)
245 return -1;
246 table = (u64 *)get_spin_tbl_addr() + pos *
247 WORDS_PER_SPIN_TABLE_ENTRY;
248 printf("table @ 0x%p\n", table);
249 printf(" addr - 0x%016llx\n",
250 table[SPIN_TABLE_ELEM_ENTRY_ADDR_IDX]);
251 printf(" status - 0x%016llx\n",
252 table[SPIN_TABLE_ELEM_STATUS_IDX]);
253 printf(" lpid - 0x%016llx\n",
254 table[SPIN_TABLE_ELEM_LPID_IDX]);
255 }
256
257 return 0;
258}
259
Michal Simek1669e182018-06-13 08:56:31 +0200260int cpu_release(u32 nr, int argc, char * const argv[])
York Sun56cc3db2014-09-08 12:20:00 -0700261{
262 u64 boot_addr;
263 u64 *table = (u64 *)get_spin_tbl_addr();
264 int pos;
265
266 pos = core_to_pos(nr);
267 if (pos <= 0)
268 return -1;
269
270 table += pos * WORDS_PER_SPIN_TABLE_ENTRY;
271 boot_addr = simple_strtoull(argv[0], NULL, 16);
272 table[SPIN_TABLE_ELEM_ENTRY_ADDR_IDX] = boot_addr;
273 flush_dcache_range((unsigned long)table,
274 (unsigned long)table + SPIN_TABLE_ELEM_SIZE);
275 asm volatile("dsb st");
276 smp_kick_all_cpus(); /* only those with entry addr set will run */
York Sun89c717c2015-11-12 12:38:21 -0800277 /*
278 * When the first release command runs, all cores are set to go. Those
279 * without a valid entry address will be trapped by "wfe". "sev" kicks
280 * them off to check the address again. When set, they continue to run.
281 */
282 asm volatile("sev");
York Sun56cc3db2014-09-08 12:20:00 -0700283
284 return 0;
285}