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