28e1580a437645ba3ba14824f350cdd601fa89f4
[oweals/u-boot.git] / arch / powerpc / cpu / mpc85xx / mp.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2008-2011 Freescale Semiconductor, Inc.
4  */
5
6 #include <common.h>
7 #include <cpu_func.h>
8 #include <env.h>
9 #include <log.h>
10 #include <asm/processor.h>
11 #include <env.h>
12 #include <ioports.h>
13 #include <lmb.h>
14 #include <asm/io.h>
15 #include <asm/mmu.h>
16 #include <asm/fsl_law.h>
17 #include <fsl_ddr_sdram.h>
18 #include "mp.h"
19
20 DECLARE_GLOBAL_DATA_PTR;
21 u32 fsl_ddr_get_intl3r(void);
22
23 extern u32 __spin_table[];
24
25 u32 get_my_id()
26 {
27         return mfspr(SPRN_PIR);
28 }
29
30 /*
31  * Determine if U-Boot should keep secondary cores in reset, or let them out
32  * of reset and hold them in a spinloop
33  */
34 int hold_cores_in_reset(int verbose)
35 {
36         /* Default to no, overridden by 'y', 'yes', 'Y', 'Yes', or '1' */
37         if (env_get_yesno("mp_holdoff") == 1) {
38                 if (verbose) {
39                         puts("Secondary cores are being held in reset.\n");
40                         puts("See 'mp_holdoff' environment variable\n");
41                 }
42
43                 return 1;
44         }
45
46         return 0;
47 }
48
49 int cpu_reset(u32 nr)
50 {
51         volatile ccsr_pic_t *pic = (void *)(CONFIG_SYS_MPC8xxx_PIC_ADDR);
52         out_be32(&pic->pir, 1 << nr);
53         /* the dummy read works around an errata on early 85xx MP PICs */
54         (void)in_be32(&pic->pir);
55         out_be32(&pic->pir, 0x0);
56
57         return 0;
58 }
59
60 int cpu_status(u32 nr)
61 {
62         u32 *table, id = get_my_id();
63
64         if (hold_cores_in_reset(1))
65                 return 0;
66
67         if (nr == id) {
68                 table = (u32 *)&__spin_table;
69                 printf("table base @ 0x%p\n", table);
70         } else if (is_core_disabled(nr)) {
71                 puts("Disabled\n");
72         } else {
73                 table = (u32 *)&__spin_table + nr * NUM_BOOT_ENTRY;
74                 printf("Running on cpu %d\n", id);
75                 printf("\n");
76                 printf("table @ 0x%p\n", table);
77                 printf("   addr - 0x%08x\n", table[BOOT_ENTRY_ADDR_LOWER]);
78                 printf("   r3   - 0x%08x\n", table[BOOT_ENTRY_R3_LOWER]);
79                 printf("   pir  - 0x%08x\n", table[BOOT_ENTRY_PIR]);
80         }
81
82         return 0;
83 }
84
85 #ifdef CONFIG_FSL_CORENET
86 int cpu_disable(u32 nr)
87 {
88         volatile ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
89
90         setbits_be32(&gur->coredisrl, 1 << nr);
91
92         return 0;
93 }
94
95 int is_core_disabled(int nr) {
96         ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
97         u32 coredisrl = in_be32(&gur->coredisrl);
98
99         return (coredisrl & (1 << nr));
100 }
101 #else
102 int cpu_disable(u32 nr)
103 {
104         volatile ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
105
106         switch (nr) {
107         case 0:
108                 setbits_be32(&gur->devdisr, MPC85xx_DEVDISR_CPU0);
109                 break;
110         case 1:
111                 setbits_be32(&gur->devdisr, MPC85xx_DEVDISR_CPU1);
112                 break;
113         default:
114                 printf("Invalid cpu number for disable %d\n", nr);
115                 return 1;
116         }
117
118         return 0;
119 }
120
121 int is_core_disabled(int nr) {
122         ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
123         u32 devdisr = in_be32(&gur->devdisr);
124
125         switch (nr) {
126         case 0:
127                 return (devdisr & MPC85xx_DEVDISR_CPU0);
128         case 1:
129                 return (devdisr & MPC85xx_DEVDISR_CPU1);
130         default:
131                 printf("Invalid cpu number for disable %d\n", nr);
132         }
133
134         return 0;
135 }
136 #endif
137
138 static u8 boot_entry_map[4] = {
139         0,
140         BOOT_ENTRY_PIR,
141         BOOT_ENTRY_R3_LOWER,
142 };
143
144 int cpu_release(u32 nr, int argc, char *const argv[])
145 {
146         u32 i, val, *table = (u32 *)&__spin_table + nr * NUM_BOOT_ENTRY;
147         u64 boot_addr;
148
149         if (hold_cores_in_reset(1))
150                 return 0;
151
152         if (nr == get_my_id()) {
153                 printf("Invalid to release the boot core.\n\n");
154                 return 1;
155         }
156
157         if (argc != 4) {
158                 printf("Invalid number of arguments to release.\n\n");
159                 return 1;
160         }
161
162         boot_addr = simple_strtoull(argv[0], NULL, 16);
163
164         /* handle pir, r3 */
165         for (i = 1; i < 3; i++) {
166                 if (argv[i][0] != '-') {
167                         u8 entry = boot_entry_map[i];
168                         val = simple_strtoul(argv[i], NULL, 16);
169                         table[entry] = val;
170                 }
171         }
172
173         table[BOOT_ENTRY_ADDR_UPPER] = (u32)(boot_addr >> 32);
174
175         /* ensure all table updates complete before final address write */
176         eieio();
177
178         table[BOOT_ENTRY_ADDR_LOWER] = (u32)(boot_addr & 0xffffffff);
179
180         return 0;
181 }
182
183 u32 determine_mp_bootpg(unsigned int *pagesize)
184 {
185         u32 bootpg;
186 #ifdef CONFIG_SYS_FSL_ERRATUM_A004468
187         u32 svr = get_svr();
188         u32 granule_size, check;
189         struct law_entry e;
190 #endif
191
192
193         /* use last 4K of mapped memory */
194         bootpg = ((gd->ram_size > CONFIG_MAX_MEM_MAPPED) ?
195                 CONFIG_MAX_MEM_MAPPED : gd->ram_size) +
196                 CONFIG_SYS_SDRAM_BASE - 4096;
197         if (pagesize)
198                 *pagesize = 4096;
199
200 #ifdef CONFIG_SYS_FSL_ERRATUM_A004468
201 /*
202  * Erratum A004468 has two parts. The 3-way interleaving applies to T4240,
203  * to be fixed in rev 2.0. The 2-way interleaving applies to many SoCs. But
204  * the way boot page chosen in u-boot avoids hitting this erratum. So only
205  * thw workaround for 3-way interleaving is needed.
206  *
207  * To make sure boot page translation works with 3-Way DDR interleaving
208  * enforce a check for the following constrains
209  * 8K granule size requires BRSIZE=8K and
210  *    bootpg >> log2(BRSIZE) %3 == 1
211  * 4K and 1K granule size requires BRSIZE=4K and
212  *    bootpg >> log2(BRSIZE) %3 == 0
213  */
214         if (SVR_SOC_VER(svr) == SVR_T4240 && SVR_MAJ(svr) < 2) {
215                 e = find_law(bootpg);
216                 switch (e.trgt_id) {
217                 case LAW_TRGT_IF_DDR_INTLV_123:
218                         granule_size = fsl_ddr_get_intl3r() & 0x1f;
219                         if (granule_size == FSL_DDR_3WAY_8KB_INTERLEAVING) {
220                                 if (pagesize)
221                                         *pagesize = 8192;
222                                 bootpg &= 0xffffe000;   /* align to 8KB */
223                                 check = bootpg >> 13;
224                                 while ((check % 3) != 1)
225                                         check--;
226                                 bootpg = check << 13;
227                                 debug("Boot page (8K) at 0x%08x\n", bootpg);
228                                 break;
229                         } else {
230                                 bootpg &= 0xfffff000;   /* align to 4KB */
231                                 check = bootpg >> 12;
232                                 while ((check % 3) != 0)
233                                         check--;
234                                 bootpg = check << 12;
235                                 debug("Boot page (4K) at 0x%08x\n", bootpg);
236                         }
237                                 break;
238                 default:
239                         break;
240                 }
241         }
242 #endif /* CONFIG_SYS_FSL_ERRATUM_A004468 */
243
244         return bootpg;
245 }
246
247 phys_addr_t get_spin_phys_addr(void)
248 {
249         return virt_to_phys(&__spin_table);
250 }
251
252 #ifdef CONFIG_FSL_CORENET
253 static void plat_mp_up(unsigned long bootpg, unsigned int pagesize)
254 {
255         u32 cpu_up_mask, whoami, brsize = LAW_SIZE_4K;
256         u32 *table = (u32 *)&__spin_table;
257         volatile ccsr_gur_t *gur;
258         volatile ccsr_local_t *ccm;
259         volatile ccsr_rcpm_t *rcpm;
260         volatile ccsr_pic_t *pic;
261         int timeout = 10;
262         u32 mask = cpu_mask();
263         struct law_entry e;
264
265         gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
266         ccm = (void *)(CONFIG_SYS_FSL_CORENET_CCM_ADDR);
267         rcpm = (void *)(CONFIG_SYS_FSL_CORENET_RCPM_ADDR);
268         pic = (void *)(CONFIG_SYS_MPC8xxx_PIC_ADDR);
269
270         whoami = in_be32(&pic->whoami);
271         cpu_up_mask = 1 << whoami;
272         out_be32(&ccm->bstrl, bootpg);
273
274         e = find_law(bootpg);
275         /* pagesize is only 4K or 8K */
276         if (pagesize == 8192)
277                 brsize = LAW_SIZE_8K;
278         out_be32(&ccm->bstrar, LAW_EN | e.trgt_id << 20 | brsize);
279         debug("BRSIZE is 0x%x\n", brsize);
280
281         /* readback to sync write */
282         in_be32(&ccm->bstrar);
283
284         /* disable time base at the platform */
285         out_be32(&rcpm->ctbenrl, cpu_up_mask);
286
287         out_be32(&gur->brrl, mask);
288
289         /* wait for everyone */
290         while (timeout) {
291                 unsigned int i, cpu, nr_cpus = cpu_numcores();
292
293                 for_each_cpu(i, cpu, nr_cpus, mask) {
294                         if (table[cpu * NUM_BOOT_ENTRY + BOOT_ENTRY_ADDR_LOWER])
295                                 cpu_up_mask |= (1 << cpu);
296                 }
297
298                 if ((cpu_up_mask & mask) == mask)
299                         break;
300
301                 udelay(100);
302                 timeout--;
303         }
304
305         if (timeout == 0)
306                 printf("CPU up timeout. CPU up mask is %x should be %x\n",
307                         cpu_up_mask, mask);
308
309         /* enable time base at the platform */
310         out_be32(&rcpm->ctbenrl, 0);
311
312         /* readback to sync write */
313         in_be32(&rcpm->ctbenrl);
314
315         mtspr(SPRN_TBWU, 0);
316         mtspr(SPRN_TBWL, 0);
317
318         out_be32(&rcpm->ctbenrl, mask);
319
320 #ifdef CONFIG_MPC8xxx_DISABLE_BPTR
321         /*
322          * Disabling Boot Page Translation allows the memory region 0xfffff000
323          * to 0xffffffff to be used normally.  Leaving Boot Page Translation
324          * enabled remaps 0xfffff000 to SDRAM which makes that memory region
325          * unusable for normal operation but it does allow OSes to easily
326          * reset a processor core to put it back into U-Boot's spinloop.
327          */
328         clrbits_be32(&ccm->bstrar, LAW_EN);
329 #endif
330 }
331 #else
332 static void plat_mp_up(unsigned long bootpg, unsigned int pagesize)
333 {
334         u32 up, cpu_up_mask, whoami;
335         u32 *table = (u32 *)&__spin_table;
336         volatile u32 bpcr;
337         volatile ccsr_local_ecm_t *ecm = (void *)(CONFIG_SYS_MPC85xx_ECM_ADDR);
338         volatile ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
339         volatile ccsr_pic_t *pic = (void *)(CONFIG_SYS_MPC8xxx_PIC_ADDR);
340         u32 devdisr;
341         int timeout = 10;
342
343         whoami = in_be32(&pic->whoami);
344         out_be32(&ecm->bptr, 0x80000000 | (bootpg >> 12));
345
346         /* disable time base at the platform */
347         devdisr = in_be32(&gur->devdisr);
348         if (whoami)
349                 devdisr |= MPC85xx_DEVDISR_TB0;
350         else
351                 devdisr |= MPC85xx_DEVDISR_TB1;
352         out_be32(&gur->devdisr, devdisr);
353
354         /* release the hounds */
355         up = ((1 << cpu_numcores()) - 1);
356         bpcr = in_be32(&ecm->eebpcr);
357         bpcr |= (up << 24);
358         out_be32(&ecm->eebpcr, bpcr);
359         asm("sync; isync; msync");
360
361         cpu_up_mask = 1 << whoami;
362         /* wait for everyone */
363         while (timeout) {
364                 int i;
365                 for (i = 0; i < cpu_numcores(); i++) {
366                         if (table[i * NUM_BOOT_ENTRY + BOOT_ENTRY_ADDR_LOWER])
367                                 cpu_up_mask |= (1 << i);
368                 };
369
370                 if ((cpu_up_mask & up) == up)
371                         break;
372
373                 udelay(100);
374                 timeout--;
375         }
376
377         if (timeout == 0)
378                 printf("CPU up timeout. CPU up mask is %x should be %x\n",
379                         cpu_up_mask, up);
380
381         /* enable time base at the platform */
382         if (whoami)
383                 devdisr |= MPC85xx_DEVDISR_TB1;
384         else
385                 devdisr |= MPC85xx_DEVDISR_TB0;
386         out_be32(&gur->devdisr, devdisr);
387
388         /* readback to sync write */
389         in_be32(&gur->devdisr);
390
391         mtspr(SPRN_TBWU, 0);
392         mtspr(SPRN_TBWL, 0);
393
394         devdisr &= ~(MPC85xx_DEVDISR_TB0 | MPC85xx_DEVDISR_TB1);
395         out_be32(&gur->devdisr, devdisr);
396
397 #ifdef CONFIG_MPC8xxx_DISABLE_BPTR
398         /*
399          * Disabling Boot Page Translation allows the memory region 0xfffff000
400          * to 0xffffffff to be used normally.  Leaving Boot Page Translation
401          * enabled remaps 0xfffff000 to SDRAM which makes that memory region
402          * unusable for normal operation but it does allow OSes to easily
403          * reset a processor core to put it back into U-Boot's spinloop.
404          */
405         clrbits_be32(&ecm->bptr, 0x80000000);
406 #endif
407 }
408 #endif
409
410 void cpu_mp_lmb_reserve(struct lmb *lmb)
411 {
412         u32 bootpg = determine_mp_bootpg(NULL);
413
414         lmb_reserve(lmb, bootpg, 4096);
415 }
416
417 void setup_mp(void)
418 {
419         extern u32 __secondary_start_page;
420         extern u32 __bootpg_addr, __spin_table_addr, __second_half_boot_page;
421
422         int i;
423         ulong fixup = (u32)&__secondary_start_page;
424         u32 bootpg, bootpg_map, pagesize;
425
426         bootpg = determine_mp_bootpg(&pagesize);
427
428         /*
429          * pagesize is only 4K or 8K
430          * we only use the last 4K of boot page
431          * bootpg_map saves the address for the boot page
432          * 8K is used for the workaround of 3-way DDR interleaving
433          */
434
435         bootpg_map = bootpg;
436
437         if (pagesize == 8192)
438                 bootpg += 4096; /* use 2nd half */
439
440         /* Some OSes expect secondary cores to be held in reset */
441         if (hold_cores_in_reset(0))
442                 return;
443
444         /*
445          * Store the bootpg's cache-able half address for use by secondary
446          * CPU cores to continue to boot
447          */
448         __bootpg_addr = (u32)virt_to_phys(&__second_half_boot_page);
449
450         /* Store spin table's physical address for use by secondary cores */
451         __spin_table_addr = (u32)get_spin_phys_addr();
452
453         /* flush bootpg it before copying invalidate any staled cacheline */
454         flush_cache(bootpg, 4096);
455
456         /* look for the tlb covering the reset page, there better be one */
457         i = find_tlb_idx((void *)CONFIG_BPTR_VIRT_ADDR, 1);
458
459         /* we found a match */
460         if (i != -1) {
461                 /* map reset page to bootpg so we can copy code there */
462                 disable_tlb(i);
463
464                 set_tlb(1, CONFIG_BPTR_VIRT_ADDR, bootpg, /* tlb, epn, rpn */
465                         MAS3_SX|MAS3_SW|MAS3_SR, MAS2_I|MAS2_G, /* perms, wimge */
466                         0, i, BOOKE_PAGESZ_4K, 1); /* ts, esel, tsize, iprot */
467
468                 memcpy((void *)CONFIG_BPTR_VIRT_ADDR, (void *)fixup, 4096);
469
470                 plat_mp_up(bootpg_map, pagesize);
471         } else {
472                 puts("WARNING: No reset page TLB. "
473                         "Skipping secondary core setup\n");
474         }
475 }