smp-cps.c 11 KB

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