smp-cps.c 12 KB

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