mp.c 11 KB

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