mp.c 6.9 KB

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