blob: 722211914936116bcdb33b58d3d1fc73e5a6e249 [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>
Tom Rini8c70baa2021-12-14 13:36:40 -05007#include <clock_legacy.h>
Simon Glass970b61e2019-11-14 12:57:09 -07008#include <cpu_func.h>
Simon Glass2dc9c342020-05-10 11:40:01 -06009#include <image.h>
Michael Walle24d116d2020-06-01 21:53:25 +020010#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060011#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
York Sun56cc3db2014-09-08 12:20:00 -070013#include <asm/io.h>
Jiafei Panded62e52021-04-21 12:12:49 +080014#include <asm/ptrace.h>
York Sun56cc3db2014-09-08 12:20:00 -070015#include <asm/system.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080016#include <asm/arch/mp.h>
17#include <asm/arch/soc.h>
Michael Walle7e3d6fd2021-10-31 23:21:56 +010018#include <linux/compat.h>
Simon Glassdbd79542020-05-10 11:40:11 -060019#include <linux/delay.h>
Jiafei Panded62e52021-04-21 12:12:49 +080020#include <linux/psci.h>
Michael Walle7e3d6fd2021-10-31 23:21:56 +010021#include <malloc.h>
Priyanka Jain9a276702016-11-17 12:29:56 +053022#include "cpu.h"
23#include <asm/arch-fsl-layerscape/soc.h>
York Sun56cc3db2014-09-08 12:20:00 -070024
25DECLARE_GLOBAL_DATA_PTR;
26
27void *get_spin_tbl_addr(void)
28{
Michael Wallec251f5b2020-06-01 21:53:34 +020029 /* the spin table is at the beginning */
30 return secondary_boot_code_start;
York Sun56cc3db2014-09-08 12:20:00 -070031}
32
Alison Wang876c7e12016-11-10 10:49:04 +080033void update_os_arch_secondary_cores(uint8_t os_arch)
34{
35 u64 *table = get_spin_tbl_addr();
36 int i;
37
Alison Wanga6231fe2017-06-08 16:15:14 +080038 for (i = 1; i < CONFIG_MAX_CPUS; i++) {
39 if (os_arch == IH_ARCH_DEFAULT)
40 table[i * WORDS_PER_SPIN_TABLE_ENTRY +
41 SPIN_TABLE_ELEM_ARCH_COMP_IDX] = OS_ARCH_SAME;
42 else
43 table[i * WORDS_PER_SPIN_TABLE_ENTRY +
44 SPIN_TABLE_ELEM_ARCH_COMP_IDX] = OS_ARCH_DIFF;
45 }
Alison Wang876c7e12016-11-10 10:49:04 +080046}
47
Priyanka Jain9a276702016-11-17 12:29:56 +053048#ifdef CONFIG_FSL_LSCH3
Michael Walle319f1a32020-06-01 21:53:32 +020049static void wake_secondary_core_n(int cluster, int core, int cluster_cores)
Priyanka Jain9a276702016-11-17 12:29:56 +053050{
51 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
52 struct ccsr_reset __iomem *rst = (void *)(CONFIG_SYS_FSL_RST_ADDR);
53 u32 mpidr = 0;
54
55 mpidr = ((cluster << 8) | core);
56 /*
57 * mpidr_el1 register value of core which needs to be released
58 * is written to scratchrw[6] register
59 */
60 gur_out32(&gur->scratchrw[6], mpidr);
61 asm volatile("dsb st" : : : "memory");
62 rst->brrl |= 1 << ((cluster * cluster_cores) + core);
63 asm volatile("dsb st" : : : "memory");
64 /*
65 * scratchrw[6] register value is polled
66 * when the value becomes zero, this means that this core is up
67 * and running, next core can be released now
68 */
69 while (gur_in32(&gur->scratchrw[6]) != 0)
70 ;
71}
72#endif
73
Mingkai Hu0e58b512015-10-26 19:47:50 +080074int fsl_layerscape_wake_seconday_cores(void)
York Sun56cc3db2014-09-08 12:20:00 -070075{
76 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
Hou Zhiqiangc7098fa2015-10-26 19:47:57 +080077#ifdef CONFIG_FSL_LSCH3
York Sun56cc3db2014-09-08 12:20:00 -070078 struct ccsr_reset __iomem *rst = (void *)(CONFIG_SYS_FSL_RST_ADDR);
Priyanka Jain9a276702016-11-17 12:29:56 +053079 u32 svr, ver, cluster, type;
80 int j = 0, cluster_cores = 0;
Hou Zhiqiangc7098fa2015-10-26 19:47:57 +080081#elif defined(CONFIG_FSL_LSCH2)
82 struct ccsr_scfg __iomem *scfg = (void *)(CONFIG_SYS_FSL_SCFG_ADDR);
83#endif
York Sun56cc3db2014-09-08 12:20:00 -070084 u32 cores, cpu_up_mask = 1;
85 int i, timeout = 10;
Michael Wallebd7e4ee2020-06-01 21:53:31 +020086 u64 *table;
Michael Walle661c6022020-06-01 21:53:35 +020087#ifdef CONFIG_EFI_LOADER
Michael Walle7e3d6fd2021-10-31 23:21:56 +010088 void *reloc_addr;
Michael Walle661c6022020-06-01 21:53:35 +020089#endif
York Sun56cc3db2014-09-08 12:20:00 -070090
York Sun77a10972015-03-20 19:28:08 -070091#ifdef COUNTER_FREQUENCY_REAL
92 /* update for secondary cores */
93 __real_cntfrq = COUNTER_FREQUENCY_REAL;
94 flush_dcache_range((unsigned long)&__real_cntfrq,
95 (unsigned long)&__real_cntfrq + 8);
96#endif
97
Michael Walle661c6022020-06-01 21:53:35 +020098#ifdef CONFIG_EFI_LOADER
99 /*
100 * EFI will reserve 64kb for its runtime services. This will probably
101 * overlap with our spin table code, which is why we have to relocate
102 * it.
103 * Keep this after the __real_cntfrq update, so we have it when we
104 * copy the complete section here.
105 */
Michael Walle7e3d6fd2021-10-31 23:21:56 +0100106 reloc_addr = memalign(PAGE_SIZE,
107 round_up(secondary_boot_code_size, PAGE_SIZE));
108 if (reloc_addr) {
109 debug("Relocating spin table from %p to %p (size %lx)\n",
110 secondary_boot_code_start, reloc_addr,
Michael Walle661c6022020-06-01 21:53:35 +0200111 secondary_boot_code_size);
Michael Walle7e3d6fd2021-10-31 23:21:56 +0100112 memcpy(reloc_addr, secondary_boot_code_start,
Michael Walle661c6022020-06-01 21:53:35 +0200113 secondary_boot_code_size);
Michael Walle7e3d6fd2021-10-31 23:21:56 +0100114 flush_dcache_range((unsigned long)reloc_addr,
115 (unsigned long)reloc_addr +
116 secondary_boot_code_size);
Michael Walle661c6022020-06-01 21:53:35 +0200117
118 /* set new entry point for secondary cores */
Michael Walle7e3d6fd2021-10-31 23:21:56 +0100119 secondary_boot_addr += reloc_addr -
Michael Walle661c6022020-06-01 21:53:35 +0200120 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 */
Michael Walle7e3d6fd2021-10-31 23:21:56 +0100125 secondary_boot_code_start = reloc_addr;
Michael Walle661c6022020-06-01 21:53:35 +0200126 }
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;
Michael Walle166ea482022-04-22 14:53:27 +0530305 int ret;
York Sun56cc3db2014-09-08 12:20:00 -0700306
York Sun56cc3db2014-09-08 12:20:00 -0700307 boot_addr = simple_strtoull(argv[0], NULL, 16);
Jiafei Panded62e52021-04-21 12:12:49 +0800308
309 if (check_psci()) {
310 /* SPIN Table is used */
311 pos = core_to_pos(nr);
312 if (pos <= 0)
313 return -1;
314
315 table += pos * WORDS_PER_SPIN_TABLE_ENTRY;
316 table[SPIN_TABLE_ELEM_ENTRY_ADDR_IDX] = boot_addr;
317 flush_dcache_range((unsigned long)table,
York Sun56cc3db2014-09-08 12:20:00 -0700318 (unsigned long)table + SPIN_TABLE_ELEM_SIZE);
Jiafei Panded62e52021-04-21 12:12:49 +0800319 asm volatile("dsb st");
Michael Walle576f68502020-06-01 21:53:24 +0200320
Jiafei Panded62e52021-04-21 12:12:49 +0800321 /*
322 * The secondary CPUs polling the spin-table above for a non-zero
323 * value. To save power "wfe" is called. Thus call "sev" here to
324 * wake the CPUs and let them check the spin-table again (see
325 * slave_cpu loop in lowlevel.S)
326 */
327 asm volatile("sev");
328 } else {
329 /* Use PSCI to kick the core */
Jiafei Panded62e52021-04-21 12:12:49 +0800330 printf("begin to kick cpu core #%d to address %llx\n",
331 nr, boot_addr);
Michael Walle166ea482022-04-22 14:53:27 +0530332 ret = invoke_psci_fn(PSCI_0_2_FN64_CPU_ON, nr, boot_addr, 0);
333 if (ret)
Jiafei Panded62e52021-04-21 12:12:49 +0800334 return -1;
335 }
York Sun56cc3db2014-09-08 12:20:00 -0700336
337 return 0;
338}