blob: 730d7663d0fbe0bdc712f4fa656bfbbfb30c5975 [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>
Simon Glass970b61e2019-11-14 12:57:09 -07007#include <cpu_func.h>
Simon Glass2dc9c342020-05-10 11:40:01 -06008#include <image.h>
Michael Walle24d116d2020-06-01 21:53:25 +02009#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060010#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
York Sun56cc3db2014-09-08 12:20:00 -070012#include <asm/io.h>
Jiafei Panded62e52021-04-21 12:12:49 +080013#include <asm/ptrace.h>
York Sun56cc3db2014-09-08 12:20:00 -070014#include <asm/system.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080015#include <asm/arch/mp.h>
16#include <asm/arch/soc.h>
Simon Glassdbd79542020-05-10 11:40:11 -060017#include <linux/delay.h>
Jiafei Panded62e52021-04-21 12:12:49 +080018#include <linux/psci.h>
Priyanka Jain9a276702016-11-17 12:29:56 +053019#include "cpu.h"
20#include <asm/arch-fsl-layerscape/soc.h>
Michael Wallec251f5b2020-06-01 21:53:34 +020021#include <efi_loader.h>
York Sun56cc3db2014-09-08 12:20:00 -070022
23DECLARE_GLOBAL_DATA_PTR;
24
25void *get_spin_tbl_addr(void)
26{
Michael Wallec251f5b2020-06-01 21:53:34 +020027 /* the spin table is at the beginning */
28 return secondary_boot_code_start;
York Sun56cc3db2014-09-08 12:20:00 -070029}
30
Alison Wang876c7e12016-11-10 10:49:04 +080031void update_os_arch_secondary_cores(uint8_t os_arch)
32{
33 u64 *table = get_spin_tbl_addr();
34 int i;
35
Alison Wanga6231fe2017-06-08 16:15:14 +080036 for (i = 1; i < CONFIG_MAX_CPUS; i++) {
37 if (os_arch == IH_ARCH_DEFAULT)
38 table[i * WORDS_PER_SPIN_TABLE_ENTRY +
39 SPIN_TABLE_ELEM_ARCH_COMP_IDX] = OS_ARCH_SAME;
40 else
41 table[i * WORDS_PER_SPIN_TABLE_ENTRY +
42 SPIN_TABLE_ELEM_ARCH_COMP_IDX] = OS_ARCH_DIFF;
43 }
Alison Wang876c7e12016-11-10 10:49:04 +080044}
45
Priyanka Jain9a276702016-11-17 12:29:56 +053046#ifdef CONFIG_FSL_LSCH3
Michael Walle319f1a32020-06-01 21:53:32 +020047static void wake_secondary_core_n(int cluster, int core, int cluster_cores)
Priyanka Jain9a276702016-11-17 12:29:56 +053048{
49 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
50 struct ccsr_reset __iomem *rst = (void *)(CONFIG_SYS_FSL_RST_ADDR);
51 u32 mpidr = 0;
52
53 mpidr = ((cluster << 8) | core);
54 /*
55 * mpidr_el1 register value of core which needs to be released
56 * is written to scratchrw[6] register
57 */
58 gur_out32(&gur->scratchrw[6], mpidr);
59 asm volatile("dsb st" : : : "memory");
60 rst->brrl |= 1 << ((cluster * cluster_cores) + core);
61 asm volatile("dsb st" : : : "memory");
62 /*
63 * scratchrw[6] register value is polled
64 * when the value becomes zero, this means that this core is up
65 * and running, next core can be released now
66 */
67 while (gur_in32(&gur->scratchrw[6]) != 0)
68 ;
69}
70#endif
71
Mingkai Hu0e58b512015-10-26 19:47:50 +080072int fsl_layerscape_wake_seconday_cores(void)
York Sun56cc3db2014-09-08 12:20:00 -070073{
74 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
Hou Zhiqiangc7098fa2015-10-26 19:47:57 +080075#ifdef CONFIG_FSL_LSCH3
York Sun56cc3db2014-09-08 12:20:00 -070076 struct ccsr_reset __iomem *rst = (void *)(CONFIG_SYS_FSL_RST_ADDR);
Priyanka Jain9a276702016-11-17 12:29:56 +053077 u32 svr, ver, cluster, type;
78 int j = 0, cluster_cores = 0;
Hou Zhiqiangc7098fa2015-10-26 19:47:57 +080079#elif defined(CONFIG_FSL_LSCH2)
80 struct ccsr_scfg __iomem *scfg = (void *)(CONFIG_SYS_FSL_SCFG_ADDR);
81#endif
York Sun56cc3db2014-09-08 12:20:00 -070082 u32 cores, cpu_up_mask = 1;
83 int i, timeout = 10;
Michael Wallebd7e4ee2020-06-01 21:53:31 +020084 u64 *table;
Michael Walle661c6022020-06-01 21:53:35 +020085#ifdef CONFIG_EFI_LOADER
86 u64 reloc_addr = U32_MAX;
87 efi_status_t ret;
88#endif
York Sun56cc3db2014-09-08 12:20:00 -070089
York Sun77a10972015-03-20 19:28:08 -070090#ifdef COUNTER_FREQUENCY_REAL
91 /* update for secondary cores */
92 __real_cntfrq = COUNTER_FREQUENCY_REAL;
93 flush_dcache_range((unsigned long)&__real_cntfrq,
94 (unsigned long)&__real_cntfrq + 8);
95#endif
96
Michael Walle661c6022020-06-01 21:53:35 +020097#ifdef CONFIG_EFI_LOADER
98 /*
99 * EFI will reserve 64kb for its runtime services. This will probably
100 * overlap with our spin table code, which is why we have to relocate
101 * it.
102 * Keep this after the __real_cntfrq update, so we have it when we
103 * copy the complete section here.
104 */
105 ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
106 EFI_RESERVED_MEMORY_TYPE,
107 efi_size_in_pages(secondary_boot_code_size),
108 &reloc_addr);
109 if (ret == EFI_SUCCESS) {
110 debug("Relocating spin table from %llx to %llx (size %lx)\n",
111 (u64)secondary_boot_code_start, reloc_addr,
112 secondary_boot_code_size);
113 memcpy((void *)reloc_addr, secondary_boot_code_start,
114 secondary_boot_code_size);
115 flush_dcache_range(reloc_addr,
116 reloc_addr + secondary_boot_code_size);
117
118 /* set new entry point for secondary cores */
119 secondary_boot_addr += (void *)reloc_addr -
120 secondary_boot_code_start;
121 flush_dcache_range((unsigned long)&secondary_boot_addr,
122 (unsigned long)&secondary_boot_addr + 8);
123
124 /* this will be used to reserve the memory */
125 secondary_boot_code_start = (void *)reloc_addr;
126 }
127#endif
128
York Sun56cc3db2014-09-08 12:20:00 -0700129 cores = cpu_mask();
130 /* Clear spin table so that secondary processors
131 * observe the correct value after waking up from wfe.
132 */
Michael Wallebd7e4ee2020-06-01 21:53:31 +0200133 table = get_spin_tbl_addr();
York Sun56cc3db2014-09-08 12:20:00 -0700134 memset(table, 0, CONFIG_MAX_CPUS*SPIN_TABLE_ELEM_SIZE);
135 flush_dcache_range((unsigned long)table,
136 (unsigned long)table +
137 (CONFIG_MAX_CPUS*SPIN_TABLE_ELEM_SIZE));
138
Michael Walle24d116d2020-06-01 21:53:25 +0200139 debug("Waking secondary cores to start from %lx\n", gd->relocaddr);
Mingkai Hu0e58b512015-10-26 19:47:50 +0800140
Hou Zhiqiangc7098fa2015-10-26 19:47:57 +0800141#ifdef CONFIG_FSL_LSCH3
Mingkai Hu0e58b512015-10-26 19:47:50 +0800142 gur_out32(&gur->bootlocptrh, (u32)(gd->relocaddr >> 32));
143 gur_out32(&gur->bootlocptrl, (u32)gd->relocaddr);
Priyanka Jain9a276702016-11-17 12:29:56 +0530144
145 svr = gur_in32(&gur->svr);
146 ver = SVR_SOC_VER(svr);
147 if (ver == SVR_LS2080A || ver == SVR_LS2085A) {
148 gur_out32(&gur->scratchrw[6], 1);
149 asm volatile("dsb st" : : : "memory");
150 rst->brrl = cores;
151 asm volatile("dsb st" : : : "memory");
152 } else {
153 /*
154 * Release the cores out of reset one-at-a-time to avoid
155 * power spikes
156 */
157 i = 0;
158 cluster = in_le32(&gur->tp_cluster[i].lower);
159 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
160 type = initiator_type(cluster, j);
161 if (type &&
162 TP_ITYP_TYPE(type) == TP_ITYP_TYPE_ARM)
163 cluster_cores++;
164 }
165
166 do {
167 cluster = in_le32(&gur->tp_cluster[i].lower);
168 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
169 type = initiator_type(cluster, j);
170 if (type &&
171 TP_ITYP_TYPE(type) == TP_ITYP_TYPE_ARM)
172 wake_secondary_core_n(i, j,
173 cluster_cores);
174 }
175 i++;
176 } while ((cluster & TP_CLUSTER_EOC) != TP_CLUSTER_EOC);
177 }
Hou Zhiqiangc7098fa2015-10-26 19:47:57 +0800178#elif defined(CONFIG_FSL_LSCH2)
179 scfg_out32(&scfg->scratchrw[0], (u32)(gd->relocaddr >> 32));
180 scfg_out32(&scfg->scratchrw[1], (u32)gd->relocaddr);
181 asm volatile("dsb st" : : : "memory");
182 gur_out32(&gur->brrl, cores);
183 asm volatile("dsb st" : : : "memory");
York Sun56cc3db2014-09-08 12:20:00 -0700184
Hou Zhiqiangc7098fa2015-10-26 19:47:57 +0800185 /* Bootup online cores */
186 scfg_out32(&scfg->corebcr, cores);
187#endif
York Sun56cc3db2014-09-08 12:20:00 -0700188 /* This is needed as a precautionary measure.
189 * If some code before this has accidentally released the secondary
190 * cores then the pre-bootloader code will trap them in a "wfe" unless
191 * the scratchrw[6] is set. In this case we need a sev here to get these
192 * cores moving again.
193 */
194 asm volatile("sev");
195
196 while (timeout--) {
197 flush_dcache_range((unsigned long)table, (unsigned long)table +
198 CONFIG_MAX_CPUS * 64);
199 for (i = 1; i < CONFIG_MAX_CPUS; i++) {
200 if (table[i * WORDS_PER_SPIN_TABLE_ENTRY +
201 SPIN_TABLE_ELEM_STATUS_IDX])
202 cpu_up_mask |= 1 << i;
203 }
204 if (hweight32(cpu_up_mask) == hweight32(cores))
205 break;
206 udelay(10);
207 }
208 if (timeout <= 0) {
Michael Walle24d116d2020-06-01 21:53:25 +0200209 printf("CPU: Failed to bring up some cores (mask 0x%x)\n",
210 cores ^ cpu_up_mask);
York Sun56cc3db2014-09-08 12:20:00 -0700211 return 1;
212 }
Michael Walle24d116d2020-06-01 21:53:25 +0200213 printf("CPU: %d cores online\n", hweight32(cores));
York Sun56cc3db2014-09-08 12:20:00 -0700214
215 return 0;
216}
217
218int is_core_valid(unsigned int core)
219{
220 return !!((1 << core) & cpu_mask());
221}
222
York Suned7fbe32016-09-13 12:40:30 -0700223static int is_pos_valid(unsigned int pos)
224{
225 return !!((1 << pos) & cpu_pos_mask());
226}
227
Arnab Basu0cb19422015-01-06 13:18:41 -0800228int is_core_online(u64 cpu_id)
229{
Michael Wallebd7e4ee2020-06-01 21:53:31 +0200230 u64 *table = get_spin_tbl_addr();
Arnab Basu0cb19422015-01-06 13:18:41 -0800231 int pos = id_to_core(cpu_id);
Michael Wallebd7e4ee2020-06-01 21:53:31 +0200232 table += pos * WORDS_PER_SPIN_TABLE_ENTRY;
Arnab Basu0cb19422015-01-06 13:18:41 -0800233 return table[SPIN_TABLE_ELEM_STATUS_IDX] == 1;
234}
235
Michal Simek1669e182018-06-13 08:56:31 +0200236int cpu_reset(u32 nr)
York Sun56cc3db2014-09-08 12:20:00 -0700237{
238 puts("Feature is not implemented.\n");
239
240 return 0;
241}
242
Michal Simek1669e182018-06-13 08:56:31 +0200243int cpu_disable(u32 nr)
York Sun56cc3db2014-09-08 12:20:00 -0700244{
245 puts("Feature is not implemented.\n");
246
247 return 0;
248}
249
York Suned7fbe32016-09-13 12:40:30 -0700250static int core_to_pos(int nr)
York Sun56cc3db2014-09-08 12:20:00 -0700251{
York Suned7fbe32016-09-13 12:40:30 -0700252 u32 cores = cpu_pos_mask();
York Sun56cc3db2014-09-08 12:20:00 -0700253 int i, count = 0;
254
255 if (nr == 0) {
256 return 0;
257 } else if (nr >= hweight32(cores)) {
258 puts("Not a valid core number.\n");
259 return -1;
260 }
261
262 for (i = 1; i < 32; i++) {
York Suned7fbe32016-09-13 12:40:30 -0700263 if (is_pos_valid(i)) {
York Sun56cc3db2014-09-08 12:20:00 -0700264 count++;
265 if (count == nr)
266 break;
267 }
268 }
269
York Suned7fbe32016-09-13 12:40:30 -0700270 if (count != nr)
271 return -1;
272
273 return i;
York Sun56cc3db2014-09-08 12:20:00 -0700274}
275
Michal Simek1669e182018-06-13 08:56:31 +0200276int cpu_status(u32 nr)
York Sun56cc3db2014-09-08 12:20:00 -0700277{
Michael Wallebd7e4ee2020-06-01 21:53:31 +0200278 u64 *table = get_spin_tbl_addr();
York Sun56cc3db2014-09-08 12:20:00 -0700279 int pos;
280
281 if (nr == 0) {
York Sun56cc3db2014-09-08 12:20:00 -0700282 printf("table base @ 0x%p\n", table);
283 } else {
284 pos = core_to_pos(nr);
285 if (pos < 0)
286 return -1;
Michael Wallebd7e4ee2020-06-01 21:53:31 +0200287 table += pos * WORDS_PER_SPIN_TABLE_ENTRY;
York Sun56cc3db2014-09-08 12:20:00 -0700288 printf("table @ 0x%p\n", table);
289 printf(" addr - 0x%016llx\n",
290 table[SPIN_TABLE_ELEM_ENTRY_ADDR_IDX]);
291 printf(" status - 0x%016llx\n",
292 table[SPIN_TABLE_ELEM_STATUS_IDX]);
293 printf(" lpid - 0x%016llx\n",
294 table[SPIN_TABLE_ELEM_LPID_IDX]);
295 }
296
297 return 0;
298}
299
Simon Glassed38aef2020-05-10 11:40:03 -0600300int cpu_release(u32 nr, int argc, char *const argv[])
York Sun56cc3db2014-09-08 12:20:00 -0700301{
302 u64 boot_addr;
Michael Wallebd7e4ee2020-06-01 21:53:31 +0200303 u64 *table = get_spin_tbl_addr();
York Sun56cc3db2014-09-08 12:20:00 -0700304 int pos;
305
York Sun56cc3db2014-09-08 12:20:00 -0700306 boot_addr = simple_strtoull(argv[0], NULL, 16);
Jiafei Panded62e52021-04-21 12:12:49 +0800307
308 if (check_psci()) {
309 /* SPIN Table is used */
310 pos = core_to_pos(nr);
311 if (pos <= 0)
312 return -1;
313
314 table += pos * WORDS_PER_SPIN_TABLE_ENTRY;
315 table[SPIN_TABLE_ELEM_ENTRY_ADDR_IDX] = boot_addr;
316 flush_dcache_range((unsigned long)table,
York Sun56cc3db2014-09-08 12:20:00 -0700317 (unsigned long)table + SPIN_TABLE_ELEM_SIZE);
Jiafei Panded62e52021-04-21 12:12:49 +0800318 asm volatile("dsb st");
Michael Walle576f68502020-06-01 21:53:24 +0200319
Jiafei Panded62e52021-04-21 12:12:49 +0800320 /*
321 * The secondary CPUs polling the spin-table above for a non-zero
322 * value. To save power "wfe" is called. Thus call "sev" here to
323 * wake the CPUs and let them check the spin-table again (see
324 * slave_cpu loop in lowlevel.S)
325 */
326 asm volatile("sev");
327 } else {
328 /* Use PSCI to kick the core */
329 struct pt_regs regs;
330
331 printf("begin to kick cpu core #%d to address %llx\n",
332 nr, boot_addr);
333 regs.regs[0] = PSCI_0_2_FN64_CPU_ON;
334 regs.regs[1] = nr;
335 regs.regs[2] = boot_addr;
336 regs.regs[3] = 0;
337 smc_call(&regs);
338 if (regs.regs[0])
339 return -1;
340 }
York Sun56cc3db2014-09-08 12:20:00 -0700341
342 return 0;
343}