smp-cps.c 14 KB

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