mp.c 6.8 KB

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