smp-cps.c 14 KB

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