blob: d50c5a437bcaf4f37c03c9a08512d9a40e9b1912 [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>
York Sun56cc3db2014-09-08 12:20:00 -070011#include <asm/io.h>
12#include <asm/system.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080013#include <asm/arch/mp.h>
14#include <asm/arch/soc.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
Priyanka Jain9a276702016-11-17 12:29:56 +053016#include "cpu.h"
17#include <asm/arch-fsl-layerscape/soc.h>
Michael Wallec251f5b2020-06-01 21:53:34 +020018#include <efi_loader.h>
York Sun56cc3db2014-09-08 12:20:00 -070019
20DECLARE_GLOBAL_DATA_PTR;
21
22void *get_spin_tbl_addr(void)
23{
Michael Wallec251f5b2020-06-01 21:53:34 +020024 /* the spin table is at the beginning */
25 return secondary_boot_code_start;
York Sun56cc3db2014-09-08 12:20:00 -070026}
27
Alison Wang876c7e12016-11-10 10:49:04 +080028void update_os_arch_secondary_cores(uint8_t os_arch)
29{
30 u64 *table = get_spin_tbl_addr();
31 int i;
32
Alison Wanga6231fe2017-06-08 16:15:14 +080033 for (i = 1; i < CONFIG_MAX_CPUS; i++) {
34 if (os_arch == IH_ARCH_DEFAULT)
35 table[i * WORDS_PER_SPIN_TABLE_ENTRY +
36 SPIN_TABLE_ELEM_ARCH_COMP_IDX] = OS_ARCH_SAME;
37 else
38 table[i * WORDS_PER_SPIN_TABLE_ENTRY +
39 SPIN_TABLE_ELEM_ARCH_COMP_IDX] = OS_ARCH_DIFF;
40 }
Alison Wang876c7e12016-11-10 10:49:04 +080041}
42
Priyanka Jain9a276702016-11-17 12:29:56 +053043#ifdef CONFIG_FSL_LSCH3
Michael Walle319f1a32020-06-01 21:53:32 +020044static void wake_secondary_core_n(int cluster, int core, int cluster_cores)
Priyanka Jain9a276702016-11-17 12:29:56 +053045{
46 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
47 struct ccsr_reset __iomem *rst = (void *)(CONFIG_SYS_FSL_RST_ADDR);
48 u32 mpidr = 0;
49
50 mpidr = ((cluster << 8) | core);
51 /*
52 * mpidr_el1 register value of core which needs to be released
53 * is written to scratchrw[6] register
54 */
55 gur_out32(&gur->scratchrw[6], mpidr);
56 asm volatile("dsb st" : : : "memory");
57 rst->brrl |= 1 << ((cluster * cluster_cores) + core);
58 asm volatile("dsb st" : : : "memory");
59 /*
60 * scratchrw[6] register value is polled
61 * when the value becomes zero, this means that this core is up
62 * and running, next core can be released now
63 */
64 while (gur_in32(&gur->scratchrw[6]) != 0)
65 ;
66}
67#endif
68
Mingkai Hu0e58b512015-10-26 19:47:50 +080069int fsl_layerscape_wake_seconday_cores(void)
York Sun56cc3db2014-09-08 12:20:00 -070070{
71 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
Hou Zhiqiangc7098fa2015-10-26 19:47:57 +080072#ifdef CONFIG_FSL_LSCH3
York Sun56cc3db2014-09-08 12:20:00 -070073 struct ccsr_reset __iomem *rst = (void *)(CONFIG_SYS_FSL_RST_ADDR);
Priyanka Jain9a276702016-11-17 12:29:56 +053074 u32 svr, ver, cluster, type;
75 int j = 0, cluster_cores = 0;
Hou Zhiqiangc7098fa2015-10-26 19:47:57 +080076#elif defined(CONFIG_FSL_LSCH2)
77 struct ccsr_scfg __iomem *scfg = (void *)(CONFIG_SYS_FSL_SCFG_ADDR);
78#endif
York Sun56cc3db2014-09-08 12:20:00 -070079 u32 cores, cpu_up_mask = 1;
80 int i, timeout = 10;
Michael Wallebd7e4ee2020-06-01 21:53:31 +020081 u64 *table;
York Sun56cc3db2014-09-08 12:20:00 -070082
York Sun77a10972015-03-20 19:28:08 -070083#ifdef COUNTER_FREQUENCY_REAL
84 /* update for secondary cores */
85 __real_cntfrq = COUNTER_FREQUENCY_REAL;
86 flush_dcache_range((unsigned long)&__real_cntfrq,
87 (unsigned long)&__real_cntfrq + 8);
88#endif
89
York Sun56cc3db2014-09-08 12:20:00 -070090 cores = cpu_mask();
91 /* Clear spin table so that secondary processors
92 * observe the correct value after waking up from wfe.
93 */
Michael Wallebd7e4ee2020-06-01 21:53:31 +020094 table = get_spin_tbl_addr();
York Sun56cc3db2014-09-08 12:20:00 -070095 memset(table, 0, CONFIG_MAX_CPUS*SPIN_TABLE_ELEM_SIZE);
96 flush_dcache_range((unsigned long)table,
97 (unsigned long)table +
98 (CONFIG_MAX_CPUS*SPIN_TABLE_ELEM_SIZE));
99
Michael Walle24d116d2020-06-01 21:53:25 +0200100 debug("Waking secondary cores to start from %lx\n", gd->relocaddr);
Mingkai Hu0e58b512015-10-26 19:47:50 +0800101
Hou Zhiqiangc7098fa2015-10-26 19:47:57 +0800102#ifdef CONFIG_FSL_LSCH3
Mingkai Hu0e58b512015-10-26 19:47:50 +0800103 gur_out32(&gur->bootlocptrh, (u32)(gd->relocaddr >> 32));
104 gur_out32(&gur->bootlocptrl, (u32)gd->relocaddr);
Priyanka Jain9a276702016-11-17 12:29:56 +0530105
106 svr = gur_in32(&gur->svr);
107 ver = SVR_SOC_VER(svr);
108 if (ver == SVR_LS2080A || ver == SVR_LS2085A) {
109 gur_out32(&gur->scratchrw[6], 1);
110 asm volatile("dsb st" : : : "memory");
111 rst->brrl = cores;
112 asm volatile("dsb st" : : : "memory");
113 } else {
114 /*
115 * Release the cores out of reset one-at-a-time to avoid
116 * power spikes
117 */
118 i = 0;
119 cluster = in_le32(&gur->tp_cluster[i].lower);
120 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
121 type = initiator_type(cluster, j);
122 if (type &&
123 TP_ITYP_TYPE(type) == TP_ITYP_TYPE_ARM)
124 cluster_cores++;
125 }
126
127 do {
128 cluster = in_le32(&gur->tp_cluster[i].lower);
129 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
130 type = initiator_type(cluster, j);
131 if (type &&
132 TP_ITYP_TYPE(type) == TP_ITYP_TYPE_ARM)
133 wake_secondary_core_n(i, j,
134 cluster_cores);
135 }
136 i++;
137 } while ((cluster & TP_CLUSTER_EOC) != TP_CLUSTER_EOC);
138 }
Hou Zhiqiangc7098fa2015-10-26 19:47:57 +0800139#elif defined(CONFIG_FSL_LSCH2)
140 scfg_out32(&scfg->scratchrw[0], (u32)(gd->relocaddr >> 32));
141 scfg_out32(&scfg->scratchrw[1], (u32)gd->relocaddr);
142 asm volatile("dsb st" : : : "memory");
143 gur_out32(&gur->brrl, cores);
144 asm volatile("dsb st" : : : "memory");
York Sun56cc3db2014-09-08 12:20:00 -0700145
Hou Zhiqiangc7098fa2015-10-26 19:47:57 +0800146 /* Bootup online cores */
147 scfg_out32(&scfg->corebcr, cores);
148#endif
York Sun56cc3db2014-09-08 12:20:00 -0700149 /* This is needed as a precautionary measure.
150 * If some code before this has accidentally released the secondary
151 * cores then the pre-bootloader code will trap them in a "wfe" unless
152 * the scratchrw[6] is set. In this case we need a sev here to get these
153 * cores moving again.
154 */
155 asm volatile("sev");
156
157 while (timeout--) {
158 flush_dcache_range((unsigned long)table, (unsigned long)table +
159 CONFIG_MAX_CPUS * 64);
160 for (i = 1; i < CONFIG_MAX_CPUS; i++) {
161 if (table[i * WORDS_PER_SPIN_TABLE_ENTRY +
162 SPIN_TABLE_ELEM_STATUS_IDX])
163 cpu_up_mask |= 1 << i;
164 }
165 if (hweight32(cpu_up_mask) == hweight32(cores))
166 break;
167 udelay(10);
168 }
169 if (timeout <= 0) {
Michael Walle24d116d2020-06-01 21:53:25 +0200170 printf("CPU: Failed to bring up some cores (mask 0x%x)\n",
171 cores ^ cpu_up_mask);
York Sun56cc3db2014-09-08 12:20:00 -0700172 return 1;
173 }
Michael Walle24d116d2020-06-01 21:53:25 +0200174 printf("CPU: %d cores online\n", hweight32(cores));
York Sun56cc3db2014-09-08 12:20:00 -0700175
176 return 0;
177}
178
179int is_core_valid(unsigned int core)
180{
181 return !!((1 << core) & cpu_mask());
182}
183
York Suned7fbe32016-09-13 12:40:30 -0700184static int is_pos_valid(unsigned int pos)
185{
186 return !!((1 << pos) & cpu_pos_mask());
187}
188
Arnab Basu0cb19422015-01-06 13:18:41 -0800189int is_core_online(u64 cpu_id)
190{
Michael Wallebd7e4ee2020-06-01 21:53:31 +0200191 u64 *table = get_spin_tbl_addr();
Arnab Basu0cb19422015-01-06 13:18:41 -0800192 int pos = id_to_core(cpu_id);
Michael Wallebd7e4ee2020-06-01 21:53:31 +0200193 table += pos * WORDS_PER_SPIN_TABLE_ENTRY;
Arnab Basu0cb19422015-01-06 13:18:41 -0800194 return table[SPIN_TABLE_ELEM_STATUS_IDX] == 1;
195}
196
Michal Simek1669e182018-06-13 08:56:31 +0200197int cpu_reset(u32 nr)
York Sun56cc3db2014-09-08 12:20:00 -0700198{
199 puts("Feature is not implemented.\n");
200
201 return 0;
202}
203
Michal Simek1669e182018-06-13 08:56:31 +0200204int cpu_disable(u32 nr)
York Sun56cc3db2014-09-08 12:20:00 -0700205{
206 puts("Feature is not implemented.\n");
207
208 return 0;
209}
210
York Suned7fbe32016-09-13 12:40:30 -0700211static int core_to_pos(int nr)
York Sun56cc3db2014-09-08 12:20:00 -0700212{
York Suned7fbe32016-09-13 12:40:30 -0700213 u32 cores = cpu_pos_mask();
York Sun56cc3db2014-09-08 12:20:00 -0700214 int i, count = 0;
215
216 if (nr == 0) {
217 return 0;
218 } else if (nr >= hweight32(cores)) {
219 puts("Not a valid core number.\n");
220 return -1;
221 }
222
223 for (i = 1; i < 32; i++) {
York Suned7fbe32016-09-13 12:40:30 -0700224 if (is_pos_valid(i)) {
York Sun56cc3db2014-09-08 12:20:00 -0700225 count++;
226 if (count == nr)
227 break;
228 }
229 }
230
York Suned7fbe32016-09-13 12:40:30 -0700231 if (count != nr)
232 return -1;
233
234 return i;
York Sun56cc3db2014-09-08 12:20:00 -0700235}
236
Michal Simek1669e182018-06-13 08:56:31 +0200237int cpu_status(u32 nr)
York Sun56cc3db2014-09-08 12:20:00 -0700238{
Michael Wallebd7e4ee2020-06-01 21:53:31 +0200239 u64 *table = get_spin_tbl_addr();
York Sun56cc3db2014-09-08 12:20:00 -0700240 int pos;
241
242 if (nr == 0) {
York Sun56cc3db2014-09-08 12:20:00 -0700243 printf("table base @ 0x%p\n", table);
244 } else {
245 pos = core_to_pos(nr);
246 if (pos < 0)
247 return -1;
Michael Wallebd7e4ee2020-06-01 21:53:31 +0200248 table += pos * WORDS_PER_SPIN_TABLE_ENTRY;
York Sun56cc3db2014-09-08 12:20:00 -0700249 printf("table @ 0x%p\n", table);
250 printf(" addr - 0x%016llx\n",
251 table[SPIN_TABLE_ELEM_ENTRY_ADDR_IDX]);
252 printf(" status - 0x%016llx\n",
253 table[SPIN_TABLE_ELEM_STATUS_IDX]);
254 printf(" lpid - 0x%016llx\n",
255 table[SPIN_TABLE_ELEM_LPID_IDX]);
256 }
257
258 return 0;
259}
260
Simon Glassed38aef2020-05-10 11:40:03 -0600261int cpu_release(u32 nr, int argc, char *const argv[])
York Sun56cc3db2014-09-08 12:20:00 -0700262{
263 u64 boot_addr;
Michael Wallebd7e4ee2020-06-01 21:53:31 +0200264 u64 *table = get_spin_tbl_addr();
York Sun56cc3db2014-09-08 12:20:00 -0700265 int pos;
266
267 pos = core_to_pos(nr);
268 if (pos <= 0)
269 return -1;
270
271 table += pos * WORDS_PER_SPIN_TABLE_ENTRY;
272 boot_addr = simple_strtoull(argv[0], NULL, 16);
273 table[SPIN_TABLE_ELEM_ENTRY_ADDR_IDX] = boot_addr;
274 flush_dcache_range((unsigned long)table,
275 (unsigned long)table + SPIN_TABLE_ELEM_SIZE);
276 asm volatile("dsb st");
Michael Walle576f68502020-06-01 21:53:24 +0200277
York Sun89c717c2015-11-12 12:38:21 -0800278 /*
Michael Walle576f68502020-06-01 21:53:24 +0200279 * The secondary CPUs polling the spin-table above for a non-zero
280 * value. To save power "wfe" is called. Thus call "sev" here to
281 * wake the CPUs and let them check the spin-table again (see
282 * slave_cpu loop in lowlevel.S)
York Sun89c717c2015-11-12 12:38:21 -0800283 */
284 asm volatile("sev");
York Sun56cc3db2014-09-08 12:20:00 -0700285
286 return 0;
287}