smp-cps.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright (C) 2013 Imagination Technologies
  3. * Author: Paul Burton <paul.burton@imgtec.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. */
  10. #include <linux/io.h>
  11. #include <linux/sched.h>
  12. #include <linux/slab.h>
  13. #include <linux/smp.h>
  14. #include <linux/types.h>
  15. #include <asm/cacheflush.h>
  16. #include <asm/gic.h>
  17. #include <asm/mips-cm.h>
  18. #include <asm/mips-cpc.h>
  19. #include <asm/mips_mt.h>
  20. #include <asm/mipsregs.h>
  21. #include <asm/smp-cps.h>
  22. #include <asm/time.h>
  23. #include <asm/uasm.h>
  24. static DECLARE_BITMAP(core_power, NR_CPUS);
  25. struct boot_config mips_cps_bootcfg;
  26. static void init_core(void)
  27. {
  28. unsigned int nvpes, t;
  29. u32 mvpconf0, vpeconf0, vpecontrol, tcstatus, tcbind, status;
  30. if (!cpu_has_mipsmt)
  31. return;
  32. /* Enter VPE configuration state */
  33. dvpe();
  34. set_c0_mvpcontrol(MVPCONTROL_VPC);
  35. /* Retrieve the count of VPEs in this core */
  36. mvpconf0 = read_c0_mvpconf0();
  37. nvpes = ((mvpconf0 & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT) + 1;
  38. smp_num_siblings = nvpes;
  39. for (t = 1; t < nvpes; t++) {
  40. /* Use a 1:1 mapping of TC index to VPE index */
  41. settc(t);
  42. /* Bind 1 TC to this VPE */
  43. tcbind = read_tc_c0_tcbind();
  44. tcbind &= ~TCBIND_CURVPE;
  45. tcbind |= t << TCBIND_CURVPE_SHIFT;
  46. write_tc_c0_tcbind(tcbind);
  47. /* Set exclusive TC, non-active, master */
  48. vpeconf0 = read_vpe_c0_vpeconf0();
  49. vpeconf0 &= ~(VPECONF0_XTC | VPECONF0_VPA);
  50. vpeconf0 |= t << VPECONF0_XTC_SHIFT;
  51. vpeconf0 |= VPECONF0_MVP;
  52. write_vpe_c0_vpeconf0(vpeconf0);
  53. /* Declare TC non-active, non-allocatable & interrupt exempt */
  54. tcstatus = read_tc_c0_tcstatus();
  55. tcstatus &= ~(TCSTATUS_A | TCSTATUS_DA);
  56. tcstatus |= TCSTATUS_IXMT;
  57. write_tc_c0_tcstatus(tcstatus);
  58. /* Halt the TC */
  59. write_tc_c0_tchalt(TCHALT_H);
  60. /* Allow only 1 TC to execute */
  61. vpecontrol = read_vpe_c0_vpecontrol();
  62. vpecontrol &= ~VPECONTROL_TE;
  63. write_vpe_c0_vpecontrol(vpecontrol);
  64. /* Copy (most of) Status from VPE 0 */
  65. status = read_c0_status();
  66. status &= ~(ST0_IM | ST0_IE | ST0_KSU);
  67. status |= ST0_CU0;
  68. write_vpe_c0_status(status);
  69. /* Copy Config from VPE 0 */
  70. write_vpe_c0_config(read_c0_config());
  71. write_vpe_c0_config7(read_c0_config7());
  72. /* Ensure no software interrupts are pending */
  73. write_vpe_c0_cause(0);
  74. /* Sync Count */
  75. write_vpe_c0_count(read_c0_count());
  76. }
  77. /* Leave VPE configuration state */
  78. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  79. }
  80. static void __init cps_smp_setup(void)
  81. {
  82. unsigned int ncores, nvpes, core_vpes;
  83. int c, v;
  84. u32 core_cfg, *entry_code;
  85. /* Detect & record VPE topology */
  86. ncores = mips_cm_numcores();
  87. pr_info("VPE topology ");
  88. for (c = nvpes = 0; c < ncores; c++) {
  89. if (cpu_has_mipsmt && config_enabled(CONFIG_MIPS_MT_SMP)) {
  90. write_gcr_cl_other(c << CM_GCR_Cx_OTHER_CORENUM_SHF);
  91. core_cfg = read_gcr_co_config();
  92. core_vpes = ((core_cfg & CM_GCR_Cx_CONFIG_PVPE_MSK) >>
  93. CM_GCR_Cx_CONFIG_PVPE_SHF) + 1;
  94. } else {
  95. core_vpes = 1;
  96. }
  97. pr_cont("%c%u", c ? ',' : '{', core_vpes);
  98. for (v = 0; v < min_t(int, core_vpes, NR_CPUS - nvpes); v++) {
  99. cpu_data[nvpes + v].core = c;
  100. #ifdef CONFIG_MIPS_MT_SMP
  101. cpu_data[nvpes + v].vpe_id = v;
  102. #endif
  103. }
  104. nvpes += core_vpes;
  105. }
  106. pr_cont("} total %u\n", nvpes);
  107. /* Indicate present CPUs (CPU being synonymous with VPE) */
  108. for (v = 0; v < min_t(unsigned, nvpes, NR_CPUS); v++) {
  109. set_cpu_possible(v, true);
  110. set_cpu_present(v, true);
  111. __cpu_number_map[v] = v;
  112. __cpu_logical_map[v] = v;
  113. }
  114. /* Core 0 is powered up (we're running on it) */
  115. bitmap_set(core_power, 0, 1);
  116. /* Disable MT - we only want to run 1 TC per VPE */
  117. if (cpu_has_mipsmt)
  118. dmt();
  119. /* Initialise core 0 */
  120. init_core();
  121. /* Patch the start of mips_cps_core_entry to provide the CM base */
  122. entry_code = (u32 *)&mips_cps_core_entry;
  123. UASM_i_LA(&entry_code, 3, (long)mips_cm_base);
  124. /* Make core 0 coherent with everything */
  125. write_gcr_cl_coherence(0xff);
  126. }
  127. static void __init cps_prepare_cpus(unsigned int max_cpus)
  128. {
  129. mips_mt_set_cpuoptions();
  130. }
  131. static void boot_core(struct boot_config *cfg)
  132. {
  133. u32 access;
  134. /* Select the appropriate core */
  135. write_gcr_cl_other(cfg->core << CM_GCR_Cx_OTHER_CORENUM_SHF);
  136. /* Set its reset vector */
  137. write_gcr_co_reset_base(CKSEG1ADDR((unsigned long)mips_cps_core_entry));
  138. /* Ensure its coherency is disabled */
  139. write_gcr_co_coherence(0);
  140. /* Ensure the core can access the GCRs */
  141. access = read_gcr_access();
  142. access |= 1 << (CM_GCR_ACCESS_ACCESSEN_SHF + cfg->core);
  143. write_gcr_access(access);
  144. /* Copy cfg */
  145. mips_cps_bootcfg = *cfg;
  146. if (mips_cpc_present()) {
  147. /* Select the appropriate core */
  148. write_cpc_cl_other(cfg->core << CPC_Cx_OTHER_CORENUM_SHF);
  149. /* Reset the core */
  150. write_cpc_co_cmd(CPC_Cx_CMD_RESET);
  151. } else {
  152. /* Take the core out of reset */
  153. write_gcr_co_reset_release(0);
  154. }
  155. /* The core is now powered up */
  156. bitmap_set(core_power, cfg->core, 1);
  157. }
  158. static void boot_vpe(void *info)
  159. {
  160. struct boot_config *cfg = info;
  161. u32 tcstatus, vpeconf0;
  162. /* Enter VPE configuration state */
  163. dvpe();
  164. set_c0_mvpcontrol(MVPCONTROL_VPC);
  165. settc(cfg->vpe);
  166. /* Set the TC restart PC */
  167. write_tc_c0_tcrestart((unsigned long)&smp_bootstrap);
  168. /* Activate the TC, allow interrupts */
  169. tcstatus = read_tc_c0_tcstatus();
  170. tcstatus &= ~TCSTATUS_IXMT;
  171. tcstatus |= TCSTATUS_A;
  172. write_tc_c0_tcstatus(tcstatus);
  173. /* Clear the TC halt bit */
  174. write_tc_c0_tchalt(0);
  175. /* Activate the VPE */
  176. vpeconf0 = read_vpe_c0_vpeconf0();
  177. vpeconf0 |= VPECONF0_VPA;
  178. write_vpe_c0_vpeconf0(vpeconf0);
  179. /* Set the stack & global pointer registers */
  180. write_tc_gpr_sp(cfg->sp);
  181. write_tc_gpr_gp(cfg->gp);
  182. /* Leave VPE configuration state */
  183. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  184. /* Enable other VPEs to execute */
  185. evpe(EVPE_ENABLE);
  186. }
  187. static void cps_boot_secondary(int cpu, struct task_struct *idle)
  188. {
  189. struct boot_config cfg;
  190. unsigned int remote;
  191. int err;
  192. cfg.core = cpu_data[cpu].core;
  193. cfg.vpe = cpu_vpe_id(&cpu_data[cpu]);
  194. cfg.pc = (unsigned long)&smp_bootstrap;
  195. cfg.sp = __KSTK_TOS(idle);
  196. cfg.gp = (unsigned long)task_thread_info(idle);
  197. if (!test_bit(cfg.core, core_power)) {
  198. /* Boot a VPE on a powered down core */
  199. boot_core(&cfg);
  200. return;
  201. }
  202. if (cfg.core != current_cpu_data.core) {
  203. /* Boot a VPE on another powered up core */
  204. for (remote = 0; remote < NR_CPUS; remote++) {
  205. if (cpu_data[remote].core != cfg.core)
  206. continue;
  207. if (cpu_online(remote))
  208. break;
  209. }
  210. BUG_ON(remote >= NR_CPUS);
  211. err = smp_call_function_single(remote, boot_vpe, &cfg, 1);
  212. if (err)
  213. panic("Failed to call remote CPU\n");
  214. return;
  215. }
  216. BUG_ON(!cpu_has_mipsmt);
  217. /* Boot a VPE on this core */
  218. boot_vpe(&cfg);
  219. }
  220. static void cps_init_secondary(void)
  221. {
  222. /* Disable MT - we only want to run 1 TC per VPE */
  223. if (cpu_has_mipsmt)
  224. dmt();
  225. /* TODO: revisit this assumption once hotplug is implemented */
  226. if (cpu_vpe_id(&current_cpu_data) == 0)
  227. init_core();
  228. change_c0_status(ST0_IM, STATUSF_IP3 | STATUSF_IP4 |
  229. STATUSF_IP6 | STATUSF_IP7);
  230. }
  231. static void cps_smp_finish(void)
  232. {
  233. write_c0_compare(read_c0_count() + (8 * mips_hpt_frequency / HZ));
  234. #ifdef CONFIG_MIPS_MT_FPAFF
  235. /* If we have an FPU, enroll ourselves in the FPU-full mask */
  236. if (cpu_has_fpu)
  237. cpu_set(smp_processor_id(), mt_fpu_cpumask);
  238. #endif /* CONFIG_MIPS_MT_FPAFF */
  239. local_irq_enable();
  240. }
  241. static void cps_cpus_done(void)
  242. {
  243. }
  244. static struct plat_smp_ops cps_smp_ops = {
  245. .smp_setup = cps_smp_setup,
  246. .prepare_cpus = cps_prepare_cpus,
  247. .boot_secondary = cps_boot_secondary,
  248. .init_secondary = cps_init_secondary,
  249. .smp_finish = cps_smp_finish,
  250. .send_ipi_single = gic_send_ipi_single,
  251. .send_ipi_mask = gic_send_ipi_mask,
  252. .cpus_done = cps_cpus_done,
  253. };
  254. int register_cps_smp_ops(void)
  255. {
  256. if (!mips_cm_present()) {
  257. pr_warn("MIPS CPS SMP unable to proceed without a CM\n");
  258. return -ENODEV;
  259. }
  260. /* check we have a GIC - we need one for IPIs */
  261. if (!(read_gcr_gic_status() & CM_GCR_GIC_STATUS_EX_MSK)) {
  262. pr_warn("MIPS CPS SMP unable to proceed without a GIC\n");
  263. return -ENODEV;
  264. }
  265. register_smp_ops(&cps_smp_ops);
  266. return 0;
  267. }