smp-cps.c 12 KB

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