smp-cps.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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/pm-cps.h>
  22. #include <asm/smp-cps.h>
  23. #include <asm/time.h>
  24. #include <asm/uasm.h>
  25. static DECLARE_BITMAP(core_power, NR_CPUS);
  26. struct core_boot_config *mips_cps_core_bootcfg;
  27. static unsigned core_vpe_count(unsigned core)
  28. {
  29. unsigned cfg;
  30. if (!config_enabled(CONFIG_MIPS_MT_SMP) || !cpu_has_mipsmt)
  31. return 1;
  32. write_gcr_cl_other(core << CM_GCR_Cx_OTHER_CORENUM_SHF);
  33. cfg = read_gcr_co_config() & CM_GCR_Cx_CONFIG_PVPE_MSK;
  34. return (cfg >> CM_GCR_Cx_CONFIG_PVPE_SHF) + 1;
  35. }
  36. static void __init cps_smp_setup(void)
  37. {
  38. unsigned int ncores, nvpes, core_vpes;
  39. int c, v;
  40. /* Detect & record VPE topology */
  41. ncores = mips_cm_numcores();
  42. pr_info("VPE topology ");
  43. for (c = nvpes = 0; c < ncores; c++) {
  44. core_vpes = core_vpe_count(c);
  45. pr_cont("%c%u", c ? ',' : '{', core_vpes);
  46. /* Use the number of VPEs in core 0 for smp_num_siblings */
  47. if (!c)
  48. smp_num_siblings = core_vpes;
  49. for (v = 0; v < min_t(int, core_vpes, NR_CPUS - nvpes); v++) {
  50. cpu_data[nvpes + v].core = c;
  51. #ifdef CONFIG_MIPS_MT_SMP
  52. cpu_data[nvpes + v].vpe_id = v;
  53. #endif
  54. }
  55. nvpes += core_vpes;
  56. }
  57. pr_cont("} total %u\n", nvpes);
  58. /* Indicate present CPUs (CPU being synonymous with VPE) */
  59. for (v = 0; v < min_t(unsigned, nvpes, NR_CPUS); v++) {
  60. set_cpu_possible(v, true);
  61. set_cpu_present(v, true);
  62. __cpu_number_map[v] = v;
  63. __cpu_logical_map[v] = v;
  64. }
  65. /* Set a coherent default CCA (CWB) */
  66. change_c0_config(CONF_CM_CMASK, 0x5);
  67. /* Core 0 is powered up (we're running on it) */
  68. bitmap_set(core_power, 0, 1);
  69. /* Initialise core 0 */
  70. mips_cps_core_init();
  71. /* Make core 0 coherent with everything */
  72. write_gcr_cl_coherence(0xff);
  73. }
  74. static void __init cps_prepare_cpus(unsigned int max_cpus)
  75. {
  76. unsigned ncores, core_vpes, c, cca;
  77. bool cca_unsuitable;
  78. u32 *entry_code;
  79. mips_mt_set_cpuoptions();
  80. /* Detect whether the CCA is unsuited to multi-core SMP */
  81. cca = read_c0_config() & CONF_CM_CMASK;
  82. switch (cca) {
  83. case 0x4: /* CWBE */
  84. case 0x5: /* CWB */
  85. /* The CCA is coherent, multi-core is fine */
  86. cca_unsuitable = false;
  87. break;
  88. default:
  89. /* CCA is not coherent, multi-core is not usable */
  90. cca_unsuitable = true;
  91. }
  92. /* Warn the user if the CCA prevents multi-core */
  93. ncores = mips_cm_numcores();
  94. if (cca_unsuitable && ncores > 1) {
  95. pr_warn("Using only one core due to unsuitable CCA 0x%x\n",
  96. cca);
  97. for_each_present_cpu(c) {
  98. if (cpu_data[c].core)
  99. set_cpu_present(c, false);
  100. }
  101. }
  102. /*
  103. * Patch the start of mips_cps_core_entry to provide:
  104. *
  105. * v0 = CM base address
  106. * s0 = kseg0 CCA
  107. */
  108. entry_code = (u32 *)&mips_cps_core_entry;
  109. UASM_i_LA(&entry_code, 3, (long)mips_cm_base);
  110. uasm_i_addiu(&entry_code, 16, 0, cca);
  111. dma_cache_wback_inv((unsigned long)&mips_cps_core_entry,
  112. (void *)entry_code - (void *)&mips_cps_core_entry);
  113. /* Allocate core boot configuration structs */
  114. mips_cps_core_bootcfg = kcalloc(ncores, sizeof(*mips_cps_core_bootcfg),
  115. GFP_KERNEL);
  116. if (!mips_cps_core_bootcfg) {
  117. pr_err("Failed to allocate boot config for %u cores\n", ncores);
  118. goto err_out;
  119. }
  120. /* Allocate VPE boot configuration structs */
  121. for (c = 0; c < ncores; c++) {
  122. core_vpes = core_vpe_count(c);
  123. mips_cps_core_bootcfg[c].vpe_config = kcalloc(core_vpes,
  124. sizeof(*mips_cps_core_bootcfg[c].vpe_config),
  125. GFP_KERNEL);
  126. if (!mips_cps_core_bootcfg[c].vpe_config) {
  127. pr_err("Failed to allocate %u VPE boot configs\n",
  128. core_vpes);
  129. goto err_out;
  130. }
  131. }
  132. /* Mark this CPU as booted */
  133. atomic_set(&mips_cps_core_bootcfg[current_cpu_data.core].vpe_mask,
  134. 1 << cpu_vpe_id(&current_cpu_data));
  135. return;
  136. err_out:
  137. /* Clean up allocations */
  138. if (mips_cps_core_bootcfg) {
  139. for (c = 0; c < ncores; c++)
  140. kfree(mips_cps_core_bootcfg[c].vpe_config);
  141. kfree(mips_cps_core_bootcfg);
  142. mips_cps_core_bootcfg = NULL;
  143. }
  144. /* Effectively disable SMP by declaring CPUs not present */
  145. for_each_possible_cpu(c) {
  146. if (c == 0)
  147. continue;
  148. set_cpu_present(c, false);
  149. }
  150. }
  151. static void boot_core(unsigned core)
  152. {
  153. u32 access;
  154. /* Select the appropriate core */
  155. write_gcr_cl_other(core << CM_GCR_Cx_OTHER_CORENUM_SHF);
  156. /* Set its reset vector */
  157. write_gcr_co_reset_base(CKSEG1ADDR((unsigned long)mips_cps_core_entry));
  158. /* Ensure its coherency is disabled */
  159. write_gcr_co_coherence(0);
  160. /* Ensure the core can access the GCRs */
  161. access = read_gcr_access();
  162. access |= 1 << (CM_GCR_ACCESS_ACCESSEN_SHF + core);
  163. write_gcr_access(access);
  164. if (mips_cpc_present()) {
  165. /* Reset the core */
  166. mips_cpc_lock_other(core);
  167. write_cpc_co_cmd(CPC_Cx_CMD_RESET);
  168. mips_cpc_unlock_other();
  169. } else {
  170. /* Take the core out of reset */
  171. write_gcr_co_reset_release(0);
  172. }
  173. /* The core is now powered up */
  174. bitmap_set(core_power, core, 1);
  175. }
  176. static void remote_vpe_boot(void *dummy)
  177. {
  178. mips_cps_boot_vpes();
  179. }
  180. static void cps_boot_secondary(int cpu, struct task_struct *idle)
  181. {
  182. unsigned core = cpu_data[cpu].core;
  183. unsigned vpe_id = cpu_vpe_id(&cpu_data[cpu]);
  184. struct core_boot_config *core_cfg = &mips_cps_core_bootcfg[core];
  185. struct vpe_boot_config *vpe_cfg = &core_cfg->vpe_config[vpe_id];
  186. unsigned int remote;
  187. int err;
  188. vpe_cfg->pc = (unsigned long)&smp_bootstrap;
  189. vpe_cfg->sp = __KSTK_TOS(idle);
  190. vpe_cfg->gp = (unsigned long)task_thread_info(idle);
  191. atomic_or(1 << cpu_vpe_id(&cpu_data[cpu]), &core_cfg->vpe_mask);
  192. preempt_disable();
  193. if (!test_bit(core, core_power)) {
  194. /* Boot a VPE on a powered down core */
  195. boot_core(core);
  196. goto out;
  197. }
  198. if (core != current_cpu_data.core) {
  199. /* Boot a VPE on another powered up core */
  200. for (remote = 0; remote < NR_CPUS; remote++) {
  201. if (cpu_data[remote].core != core)
  202. continue;
  203. if (cpu_online(remote))
  204. break;
  205. }
  206. BUG_ON(remote >= NR_CPUS);
  207. err = smp_call_function_single(remote, remote_vpe_boot,
  208. NULL, 1);
  209. if (err)
  210. panic("Failed to call remote CPU\n");
  211. goto out;
  212. }
  213. BUG_ON(!cpu_has_mipsmt);
  214. /* Boot a VPE on this core */
  215. mips_cps_boot_vpes();
  216. out:
  217. preempt_enable();
  218. }
  219. static void cps_init_secondary(void)
  220. {
  221. /* Disable MT - we only want to run 1 TC per VPE */
  222. if (cpu_has_mipsmt)
  223. dmt();
  224. change_c0_status(ST0_IM, STATUSF_IP3 | STATUSF_IP4 |
  225. STATUSF_IP6 | STATUSF_IP7);
  226. }
  227. static void cps_smp_finish(void)
  228. {
  229. write_c0_compare(read_c0_count() + (8 * mips_hpt_frequency / HZ));
  230. #ifdef CONFIG_MIPS_MT_FPAFF
  231. /* If we have an FPU, enroll ourselves in the FPU-full mask */
  232. if (cpu_has_fpu)
  233. cpu_set(smp_processor_id(), mt_fpu_cpumask);
  234. #endif /* CONFIG_MIPS_MT_FPAFF */
  235. local_irq_enable();
  236. }
  237. #ifdef CONFIG_HOTPLUG_CPU
  238. static int cps_cpu_disable(void)
  239. {
  240. unsigned cpu = smp_processor_id();
  241. struct core_boot_config *core_cfg;
  242. if (!cpu)
  243. return -EBUSY;
  244. if (!cps_pm_support_state(CPS_PM_POWER_GATED))
  245. return -EINVAL;
  246. core_cfg = &mips_cps_core_bootcfg[current_cpu_data.core];
  247. atomic_sub(1 << cpu_vpe_id(&current_cpu_data), &core_cfg->vpe_mask);
  248. smp_mb__after_atomic();
  249. set_cpu_online(cpu, false);
  250. cpu_clear(cpu, cpu_callin_map);
  251. return 0;
  252. }
  253. static DECLARE_COMPLETION(cpu_death_chosen);
  254. static unsigned cpu_death_sibling;
  255. static enum {
  256. CPU_DEATH_HALT,
  257. CPU_DEATH_POWER,
  258. } cpu_death;
  259. void play_dead(void)
  260. {
  261. unsigned cpu, core;
  262. local_irq_disable();
  263. idle_task_exit();
  264. cpu = smp_processor_id();
  265. cpu_death = CPU_DEATH_POWER;
  266. if (cpu_has_mipsmt) {
  267. core = cpu_data[cpu].core;
  268. /* Look for another online VPE within the core */
  269. for_each_online_cpu(cpu_death_sibling) {
  270. if (cpu_data[cpu_death_sibling].core != core)
  271. continue;
  272. /*
  273. * There is an online VPE within the core. Just halt
  274. * this TC and leave the core alone.
  275. */
  276. cpu_death = CPU_DEATH_HALT;
  277. break;
  278. }
  279. }
  280. /* This CPU has chosen its way out */
  281. complete(&cpu_death_chosen);
  282. if (cpu_death == CPU_DEATH_HALT) {
  283. /* Halt this TC */
  284. write_c0_tchalt(TCHALT_H);
  285. instruction_hazard();
  286. } else {
  287. /* Power down the core */
  288. cps_pm_enter_state(CPS_PM_POWER_GATED);
  289. }
  290. /* This should never be reached */
  291. panic("Failed to offline CPU %u", cpu);
  292. }
  293. static void wait_for_sibling_halt(void *ptr_cpu)
  294. {
  295. unsigned cpu = (unsigned)ptr_cpu;
  296. unsigned vpe_id = cpu_data[cpu].vpe_id;
  297. unsigned halted;
  298. unsigned long flags;
  299. do {
  300. local_irq_save(flags);
  301. settc(vpe_id);
  302. halted = read_tc_c0_tchalt();
  303. local_irq_restore(flags);
  304. } while (!(halted & TCHALT_H));
  305. }
  306. static void cps_cpu_die(unsigned int cpu)
  307. {
  308. unsigned core = cpu_data[cpu].core;
  309. unsigned stat;
  310. int err;
  311. /* Wait for the cpu to choose its way out */
  312. if (!wait_for_completion_timeout(&cpu_death_chosen,
  313. msecs_to_jiffies(5000))) {
  314. pr_err("CPU%u: didn't offline\n", cpu);
  315. return;
  316. }
  317. /*
  318. * Now wait for the CPU to actually offline. Without doing this that
  319. * offlining may race with one or more of:
  320. *
  321. * - Onlining the CPU again.
  322. * - Powering down the core if another VPE within it is offlined.
  323. * - A sibling VPE entering a non-coherent state.
  324. *
  325. * In the non-MT halt case (ie. infinite loop) the CPU is doing nothing
  326. * with which we could race, so do nothing.
  327. */
  328. if (cpu_death == CPU_DEATH_POWER) {
  329. /*
  330. * Wait for the core to enter a powered down or clock gated
  331. * state, the latter happening when a JTAG probe is connected
  332. * in which case the CPC will refuse to power down the core.
  333. */
  334. do {
  335. mips_cpc_lock_other(core);
  336. stat = read_cpc_co_stat_conf();
  337. stat &= CPC_Cx_STAT_CONF_SEQSTATE_MSK;
  338. mips_cpc_unlock_other();
  339. } while (stat != CPC_Cx_STAT_CONF_SEQSTATE_D0 &&
  340. stat != CPC_Cx_STAT_CONF_SEQSTATE_D2 &&
  341. stat != CPC_Cx_STAT_CONF_SEQSTATE_U2);
  342. /* Indicate the core is powered off */
  343. bitmap_clear(core_power, core, 1);
  344. } else if (cpu_has_mipsmt) {
  345. /*
  346. * Have a CPU with access to the offlined CPUs registers wait
  347. * for its TC to halt.
  348. */
  349. err = smp_call_function_single(cpu_death_sibling,
  350. wait_for_sibling_halt,
  351. (void *)cpu, 1);
  352. if (err)
  353. panic("Failed to call remote sibling CPU\n");
  354. }
  355. }
  356. #endif /* CONFIG_HOTPLUG_CPU */
  357. static struct plat_smp_ops cps_smp_ops = {
  358. .smp_setup = cps_smp_setup,
  359. .prepare_cpus = cps_prepare_cpus,
  360. .boot_secondary = cps_boot_secondary,
  361. .init_secondary = cps_init_secondary,
  362. .smp_finish = cps_smp_finish,
  363. .send_ipi_single = gic_send_ipi_single,
  364. .send_ipi_mask = gic_send_ipi_mask,
  365. #ifdef CONFIG_HOTPLUG_CPU
  366. .cpu_disable = cps_cpu_disable,
  367. .cpu_die = cps_cpu_die,
  368. #endif
  369. };
  370. bool mips_cps_smp_in_use(void)
  371. {
  372. extern struct plat_smp_ops *mp_ops;
  373. return mp_ops == &cps_smp_ops;
  374. }
  375. int register_cps_smp_ops(void)
  376. {
  377. if (!mips_cm_present()) {
  378. pr_warn("MIPS CPS SMP unable to proceed without a CM\n");
  379. return -ENODEV;
  380. }
  381. /* check we have a GIC - we need one for IPIs */
  382. if (!(read_gcr_gic_status() & CM_GCR_GIC_STATUS_EX_MSK)) {
  383. pr_warn("MIPS CPS SMP unable to proceed without a GIC\n");
  384. return -ENODEV;
  385. }
  386. register_smp_ops(&cps_smp_ops);
  387. return 0;
  388. }