blob: d28ab265335f183ce56a459042165a835371a700 [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>
Michael Walle7e3d6fd2021-10-31 23:21:56 +010017#include <linux/compat.h>
Simon Glassdbd79542020-05-10 11:40:11 -060018#include <linux/delay.h>
Jiafei Panded62e52021-04-21 12:12:49 +080019#include <linux/psci.h>
Michael Walle7e3d6fd2021-10-31 23:21:56 +010020#include <malloc.h>
Priyanka Jain9a276702016-11-17 12:29:56 +053021#include "cpu.h"
22#include <asm/arch-fsl-layerscape/soc.h>
York Sun56cc3db2014-09-08 12:20:00 -070023
24DECLARE_GLOBAL_DATA_PTR;
25
26void *get_spin_tbl_addr(void)
27{
Michael Wallec251f5b2020-06-01 21:53:34 +020028 /* the spin table is at the beginning */
29 return secondary_boot_code_start;
York Sun56cc3db2014-09-08 12:20:00 -070030}
31
Alison Wang876c7e12016-11-10 10:49:04 +080032void update_os_arch_secondary_cores(uint8_t os_arch)
33{
34 u64 *table = get_spin_tbl_addr();
35 int i;
36
Alison Wanga6231fe2017-06-08 16:15:14 +080037 for (i = 1; i < CONFIG_MAX_CPUS; i++) {
38 if (os_arch == IH_ARCH_DEFAULT)
39 table[i * WORDS_PER_SPIN_TABLE_ENTRY +
40 SPIN_TABLE_ELEM_ARCH_COMP_IDX] = OS_ARCH_SAME;
41 else
42 table[i * WORDS_PER_SPIN_TABLE_ENTRY +
43 SPIN_TABLE_ELEM_ARCH_COMP_IDX] = OS_ARCH_DIFF;
44 }
Alison Wang876c7e12016-11-10 10:49:04 +080045}
46
Priyanka Jain9a276702016-11-17 12:29:56 +053047#ifdef CONFIG_FSL_LSCH3
Michael Walle319f1a32020-06-01 21:53:32 +020048static void wake_secondary_core_n(int cluster, int core, int cluster_cores)
Priyanka Jain9a276702016-11-17 12:29:56 +053049{
50 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
51 struct ccsr_reset __iomem *rst = (void *)(CONFIG_SYS_FSL_RST_ADDR);
52 u32 mpidr = 0;
53
54 mpidr = ((cluster << 8) | core);
55 /*
56 * mpidr_el1 register value of core which needs to be released
57 * is written to scratchrw[6] register
58 */
59 gur_out32(&gur->scratchrw[6], mpidr);
60 asm volatile("dsb st" : : : "memory");
61 rst->brrl |= 1 << ((cluster * cluster_cores) + core);
62 asm volatile("dsb st" : : : "memory");
63 /*
64 * scratchrw[6] register value is polled
65 * when the value becomes zero, this means that this core is up
66 * and running, next core can be released now
67 */
68 while (gur_in32(&gur->scratchrw[6]) != 0)
69 ;
70}
71#endif
72
Mingkai Hu0e58b512015-10-26 19:47:50 +080073int fsl_layerscape_wake_seconday_cores(void)
York Sun56cc3db2014-09-08 12:20:00 -070074{
75 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
Hou Zhiqiangc7098fa2015-10-26 19:47:57 +080076#ifdef CONFIG_FSL_LSCH3
York Sun56cc3db2014-09-08 12:20:00 -070077 struct ccsr_reset __iomem *rst = (void *)(CONFIG_SYS_FSL_RST_ADDR);
Priyanka Jain9a276702016-11-17 12:29:56 +053078 u32 svr, ver, cluster, type;
79 int j = 0, cluster_cores = 0;
Hou Zhiqiangc7098fa2015-10-26 19:47:57 +080080#elif defined(CONFIG_FSL_LSCH2)
81 struct ccsr_scfg __iomem *scfg = (void *)(CONFIG_SYS_FSL_SCFG_ADDR);
82#endif
York Sun56cc3db2014-09-08 12:20:00 -070083 u32 cores, cpu_up_mask = 1;
84 int i, timeout = 10;
Michael Wallebd7e4ee2020-06-01 21:53:31 +020085 u64 *table;
Michael Walle661c6022020-06-01 21:53:35 +020086#ifdef CONFIG_EFI_LOADER
Michael Walle7e3d6fd2021-10-31 23:21:56 +010087 void *reloc_addr;
Michael Walle661c6022020-06-01 21:53:35 +020088#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 */
Michael Walle7e3d6fd2021-10-31 23:21:56 +0100105 reloc_addr = memalign(PAGE_SIZE,
106 round_up(secondary_boot_code_size, PAGE_SIZE));
107 if (reloc_addr) {
108 debug("Relocating spin table from %p to %p (size %lx)\n",
109 secondary_boot_code_start, reloc_addr,
Michael Walle661c6022020-06-01 21:53:35 +0200110 secondary_boot_code_size);
Michael Walle7e3d6fd2021-10-31 23:21:56 +0100111 memcpy(reloc_addr, secondary_boot_code_start,
Michael Walle661c6022020-06-01 21:53:35 +0200112 secondary_boot_code_size);
Michael Walle7e3d6fd2021-10-31 23:21:56 +0100113 flush_dcache_range((unsigned long)reloc_addr,
114 (unsigned long)reloc_addr +
115 secondary_boot_code_size);
Michael Walle661c6022020-06-01 21:53:35 +0200116
117 /* set new entry point for secondary cores */
Michael Walle7e3d6fd2021-10-31 23:21:56 +0100118 secondary_boot_addr += reloc_addr -
Michael Walle661c6022020-06-01 21:53:35 +0200119 secondary_boot_code_start;
120 flush_dcache_range((unsigned long)&secondary_boot_addr,
121 (unsigned long)&secondary_boot_addr + 8);
122
123 /* this will be used to reserve the memory */
Michael Walle7e3d6fd2021-10-31 23:21:56 +0100124 secondary_boot_code_start = reloc_addr;
Michael Walle661c6022020-06-01 21:53:35 +0200125 }
126#endif
127
York Sun56cc3db2014-09-08 12:20:00 -0700128 cores = cpu_mask();
129 /* Clear spin table so that secondary processors
130 * observe the correct value after waking up from wfe.
131 */
Michael Wallebd7e4ee2020-06-01 21:53:31 +0200132 table = get_spin_tbl_addr();
York Sun56cc3db2014-09-08 12:20:00 -0700133 memset(table, 0, CONFIG_MAX_CPUS*SPIN_TABLE_ELEM_SIZE);
134 flush_dcache_range((unsigned long)table,
135 (unsigned long)table +
136 (CONFIG_MAX_CPUS*SPIN_TABLE_ELEM_SIZE));
137
Michael Walle24d116d2020-06-01 21:53:25 +0200138 debug("Waking secondary cores to start from %lx\n", gd->relocaddr);
Mingkai Hu0e58b512015-10-26 19:47:50 +0800139
Hou Zhiqiangc7098fa2015-10-26 19:47:57 +0800140#ifdef CONFIG_FSL_LSCH3
Mingkai Hu0e58b512015-10-26 19:47:50 +0800141 gur_out32(&gur->bootlocptrh, (u32)(gd->relocaddr >> 32));
142 gur_out32(&gur->bootlocptrl, (u32)gd->relocaddr);
Priyanka Jain9a276702016-11-17 12:29:56 +0530143
144 svr = gur_in32(&gur->svr);
145 ver = SVR_SOC_VER(svr);
146 if (ver == SVR_LS2080A || ver == SVR_LS2085A) {
147 gur_out32(&gur->scratchrw[6], 1);
148 asm volatile("dsb st" : : : "memory");
149 rst->brrl = cores;
150 asm volatile("dsb st" : : : "memory");
151 } else {
152 /*
153 * Release the cores out of reset one-at-a-time to avoid
154 * power spikes
155 */
156 i = 0;
157 cluster = in_le32(&gur->tp_cluster[i].lower);
158 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
159 type = initiator_type(cluster, j);
160 if (type &&
161 TP_ITYP_TYPE(type) == TP_ITYP_TYPE_ARM)
162 cluster_cores++;
163 }
164
165 do {
166 cluster = in_le32(&gur->tp_cluster[i].lower);
167 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
168 type = initiator_type(cluster, j);
169 if (type &&
170 TP_ITYP_TYPE(type) == TP_ITYP_TYPE_ARM)
171 wake_secondary_core_n(i, j,
172 cluster_cores);
173 }
174 i++;
175 } while ((cluster & TP_CLUSTER_EOC) != TP_CLUSTER_EOC);
176 }
Hou Zhiqiangc7098fa2015-10-26 19:47:57 +0800177#elif defined(CONFIG_FSL_LSCH2)
178 scfg_out32(&scfg->scratchrw[0], (u32)(gd->relocaddr >> 32));
179 scfg_out32(&scfg->scratchrw[1], (u32)gd->relocaddr);
180 asm volatile("dsb st" : : : "memory");
181 gur_out32(&gur->brrl, cores);
182 asm volatile("dsb st" : : : "memory");
York Sun56cc3db2014-09-08 12:20:00 -0700183
Hou Zhiqiangc7098fa2015-10-26 19:47:57 +0800184 /* Bootup online cores */
185 scfg_out32(&scfg->corebcr, cores);
186#endif
York Sun56cc3db2014-09-08 12:20:00 -0700187 /* This is needed as a precautionary measure.
188 * If some code before this has accidentally released the secondary
189 * cores then the pre-bootloader code will trap them in a "wfe" unless
190 * the scratchrw[6] is set. In this case we need a sev here to get these
191 * cores moving again.
192 */
193 asm volatile("sev");
194
195 while (timeout--) {
196 flush_dcache_range((unsigned long)table, (unsigned long)table +
197 CONFIG_MAX_CPUS * 64);
198 for (i = 1; i < CONFIG_MAX_CPUS; i++) {
199 if (table[i * WORDS_PER_SPIN_TABLE_ENTRY +
200 SPIN_TABLE_ELEM_STATUS_IDX])
201 cpu_up_mask |= 1 << i;
202 }
203 if (hweight32(cpu_up_mask) == hweight32(cores))
204 break;
205 udelay(10);
206 }
207 if (timeout <= 0) {
Michael Walle24d116d2020-06-01 21:53:25 +0200208 printf("CPU: Failed to bring up some cores (mask 0x%x)\n",
209 cores ^ cpu_up_mask);
York Sun56cc3db2014-09-08 12:20:00 -0700210 return 1;
211 }
Michael Walle24d116d2020-06-01 21:53:25 +0200212 printf("CPU: %d cores online\n", hweight32(cores));
York Sun56cc3db2014-09-08 12:20:00 -0700213
214 return 0;
215}
216
217int is_core_valid(unsigned int core)
218{
219 return !!((1 << core) & cpu_mask());
220}
221
York Suned7fbe32016-09-13 12:40:30 -0700222static int is_pos_valid(unsigned int pos)
223{
224 return !!((1 << pos) & cpu_pos_mask());
225}
226
Arnab Basu0cb19422015-01-06 13:18:41 -0800227int is_core_online(u64 cpu_id)
228{
Michael Wallebd7e4ee2020-06-01 21:53:31 +0200229 u64 *table = get_spin_tbl_addr();
Arnab Basu0cb19422015-01-06 13:18:41 -0800230 int pos = id_to_core(cpu_id);
Michael Wallebd7e4ee2020-06-01 21:53:31 +0200231 table += pos * WORDS_PER_SPIN_TABLE_ENTRY;
Arnab Basu0cb19422015-01-06 13:18:41 -0800232 return table[SPIN_TABLE_ELEM_STATUS_IDX] == 1;
233}
234
Michal Simek1669e182018-06-13 08:56:31 +0200235int cpu_reset(u32 nr)
York Sun56cc3db2014-09-08 12:20:00 -0700236{
237 puts("Feature is not implemented.\n");
238
239 return 0;
240}
241
Michal Simek1669e182018-06-13 08:56:31 +0200242int cpu_disable(u32 nr)
York Sun56cc3db2014-09-08 12:20:00 -0700243{
244 puts("Feature is not implemented.\n");
245
246 return 0;
247}
248
York Suned7fbe32016-09-13 12:40:30 -0700249static int core_to_pos(int nr)
York Sun56cc3db2014-09-08 12:20:00 -0700250{
York Suned7fbe32016-09-13 12:40:30 -0700251 u32 cores = cpu_pos_mask();
York Sun56cc3db2014-09-08 12:20:00 -0700252 int i, count = 0;
253
254 if (nr == 0) {
255 return 0;
256 } else if (nr >= hweight32(cores)) {
257 puts("Not a valid core number.\n");
258 return -1;
259 }
260
261 for (i = 1; i < 32; i++) {
York Suned7fbe32016-09-13 12:40:30 -0700262 if (is_pos_valid(i)) {
York Sun56cc3db2014-09-08 12:20:00 -0700263 count++;
264 if (count == nr)
265 break;
266 }
267 }
268
York Suned7fbe32016-09-13 12:40:30 -0700269 if (count != nr)
270 return -1;
271
272 return i;
York Sun56cc3db2014-09-08 12:20:00 -0700273}
274
Michal Simek1669e182018-06-13 08:56:31 +0200275int cpu_status(u32 nr)
York Sun56cc3db2014-09-08 12:20:00 -0700276{
Michael Wallebd7e4ee2020-06-01 21:53:31 +0200277 u64 *table = get_spin_tbl_addr();
York Sun56cc3db2014-09-08 12:20:00 -0700278 int pos;
279
280 if (nr == 0) {
York Sun56cc3db2014-09-08 12:20:00 -0700281 printf("table base @ 0x%p\n", table);
282 } else {
283 pos = core_to_pos(nr);
284 if (pos < 0)
285 return -1;
Michael Wallebd7e4ee2020-06-01 21:53:31 +0200286 table += pos * WORDS_PER_SPIN_TABLE_ENTRY;
York Sun56cc3db2014-09-08 12:20:00 -0700287 printf("table @ 0x%p\n", table);
288 printf(" addr - 0x%016llx\n",
289 table[SPIN_TABLE_ELEM_ENTRY_ADDR_IDX]);
290 printf(" status - 0x%016llx\n",
291 table[SPIN_TABLE_ELEM_STATUS_IDX]);
292 printf(" lpid - 0x%016llx\n",
293 table[SPIN_TABLE_ELEM_LPID_IDX]);
294 }
295
296 return 0;
297}
298
Simon Glassed38aef2020-05-10 11:40:03 -0600299int cpu_release(u32 nr, int argc, char *const argv[])
York Sun56cc3db2014-09-08 12:20:00 -0700300{
301 u64 boot_addr;
Michael Wallebd7e4ee2020-06-01 21:53:31 +0200302 u64 *table = get_spin_tbl_addr();
York Sun56cc3db2014-09-08 12:20:00 -0700303 int pos;
304
York Sun56cc3db2014-09-08 12:20:00 -0700305 boot_addr = simple_strtoull(argv[0], NULL, 16);
Jiafei Panded62e52021-04-21 12:12:49 +0800306
307 if (check_psci()) {
308 /* SPIN Table is used */
309 pos = core_to_pos(nr);
310 if (pos <= 0)
311 return -1;
312
313 table += pos * WORDS_PER_SPIN_TABLE_ENTRY;
314 table[SPIN_TABLE_ELEM_ENTRY_ADDR_IDX] = boot_addr;
315 flush_dcache_range((unsigned long)table,
York Sun56cc3db2014-09-08 12:20:00 -0700316 (unsigned long)table + SPIN_TABLE_ELEM_SIZE);
Jiafei Panded62e52021-04-21 12:12:49 +0800317 asm volatile("dsb st");
Michael Walle576f68502020-06-01 21:53:24 +0200318
Jiafei Panded62e52021-04-21 12:12:49 +0800319 /*
320 * The secondary CPUs polling the spin-table above for a non-zero
321 * value. To save power "wfe" is called. Thus call "sev" here to
322 * wake the CPUs and let them check the spin-table again (see
323 * slave_cpu loop in lowlevel.S)
324 */
325 asm volatile("sev");
326 } else {
327 /* Use PSCI to kick the core */
328 struct pt_regs regs;
329
330 printf("begin to kick cpu core #%d to address %llx\n",
331 nr, boot_addr);
332 regs.regs[0] = PSCI_0_2_FN64_CPU_ON;
333 regs.regs[1] = nr;
334 regs.regs[2] = boot_addr;
335 regs.regs[3] = 0;
336 smc_call(&regs);
337 if (regs.regs[0])
338 return -1;
339 }
York Sun56cc3db2014-09-08 12:20:00 -0700340
341 return 0;
342}