smp-cps.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2013 Imagination Technologies
  4. * Author: Paul Burton <paul.burton@mips.com>
  5. */
  6. #include <linux/cpu.h>
  7. #include <linux/delay.h>
  8. #include <linux/io.h>
  9. #include <linux/memblock.h>
  10. #include <linux/sched/task_stack.h>
  11. #include <linux/sched/hotplug.h>
  12. #include <linux/slab.h>
  13. #include <linux/smp.h>
  14. #include <linux/types.h>
  15. #include <linux/irq.h>
  16. #include <asm/bcache.h>
  17. #include <asm/mips-cps.h>
  18. #include <asm/mips_mt.h>
  19. #include <asm/mipsregs.h>
  20. #include <asm/pm-cps.h>
  21. #include <asm/r4kcache.h>
  22. #include <asm/regdef.h>
  23. #include <asm/smp.h>
  24. #include <asm/smp-cps.h>
  25. #include <asm/time.h>
  26. #include <asm/uasm.h>
  27. #define BEV_VEC_SIZE 0x500
  28. #define BEV_VEC_ALIGN 0x1000
  29. enum label_id {
  30. label_not_nmi = 1,
  31. };
  32. UASM_L_LA(_not_nmi)
  33. static DECLARE_BITMAP(core_power, NR_CPUS);
  34. static uint32_t core_entry_reg;
  35. static phys_addr_t cps_vec_pa;
  36. struct core_boot_config *mips_cps_core_bootcfg;
  37. static unsigned __init core_vpe_count(unsigned int cluster, unsigned core)
  38. {
  39. return min(smp_max_threads, mips_cps_numvps(cluster, core));
  40. }
  41. static void __init *mips_cps_build_core_entry(void *addr)
  42. {
  43. extern void (*nmi_handler)(void);
  44. u32 *p = addr;
  45. u32 val;
  46. struct uasm_label labels[2];
  47. struct uasm_reloc relocs[2];
  48. struct uasm_label *l = labels;
  49. struct uasm_reloc *r = relocs;
  50. memset(labels, 0, sizeof(labels));
  51. memset(relocs, 0, sizeof(relocs));
  52. uasm_i_mfc0(&p, GPR_K0, C0_STATUS);
  53. UASM_i_LA(&p, GPR_T9, ST0_NMI);
  54. uasm_i_and(&p, GPR_K0, GPR_K0, GPR_T9);
  55. uasm_il_bnez(&p, &r, GPR_K0, label_not_nmi);
  56. uasm_i_nop(&p);
  57. UASM_i_LA(&p, GPR_K0, (long)&nmi_handler);
  58. uasm_l_not_nmi(&l, p);
  59. val = CAUSEF_IV;
  60. uasm_i_lui(&p, GPR_K0, val >> 16);
  61. uasm_i_ori(&p, GPR_K0, GPR_K0, val & 0xffff);
  62. uasm_i_mtc0(&p, GPR_K0, C0_CAUSE);
  63. val = ST0_CU1 | ST0_CU0 | ST0_BEV | ST0_KX_IF_64;
  64. uasm_i_lui(&p, GPR_K0, val >> 16);
  65. uasm_i_ori(&p, GPR_K0, GPR_K0, val & 0xffff);
  66. uasm_i_mtc0(&p, GPR_K0, C0_STATUS);
  67. uasm_i_ehb(&p);
  68. uasm_i_ori(&p, GPR_A0, 0, read_c0_config() & CONF_CM_CMASK);
  69. UASM_i_LA(&p, GPR_A1, (long)mips_gcr_base);
  70. #if defined(KBUILD_64BIT_SYM32) || defined(CONFIG_32BIT)
  71. UASM_i_LA(&p, GPR_T9, CKSEG1ADDR(__pa_symbol(mips_cps_core_boot)));
  72. #else
  73. UASM_i_LA(&p, GPR_T9, TO_UNCAC(__pa_symbol(mips_cps_core_boot)));
  74. #endif
  75. uasm_i_jr(&p, GPR_T9);
  76. uasm_i_nop(&p);
  77. uasm_resolve_relocs(relocs, labels);
  78. return p;
  79. }
  80. static int __init allocate_cps_vecs(void)
  81. {
  82. /* Try to allocate in KSEG1 first */
  83. cps_vec_pa = memblock_phys_alloc_range(BEV_VEC_SIZE, BEV_VEC_ALIGN,
  84. 0x0, CSEGX_SIZE - 1);
  85. if (cps_vec_pa)
  86. core_entry_reg = CKSEG1ADDR(cps_vec_pa) &
  87. CM_GCR_Cx_RESET_BASE_BEVEXCBASE;
  88. if (!cps_vec_pa && mips_cm_is64) {
  89. cps_vec_pa = memblock_phys_alloc_range(BEV_VEC_SIZE, BEV_VEC_ALIGN,
  90. 0x0, SZ_4G - 1);
  91. if (cps_vec_pa)
  92. core_entry_reg = (cps_vec_pa & CM_GCR_Cx_RESET_BASE_BEVEXCBASE) |
  93. CM_GCR_Cx_RESET_BASE_MODE;
  94. }
  95. if (!cps_vec_pa)
  96. return -ENOMEM;
  97. return 0;
  98. }
  99. static void __init setup_cps_vecs(void)
  100. {
  101. void *cps_vec;
  102. cps_vec = (void *)CKSEG1ADDR_OR_64BIT(cps_vec_pa);
  103. mips_cps_build_core_entry(cps_vec);
  104. memcpy(cps_vec + 0x200, &excep_tlbfill, 0x80);
  105. memcpy(cps_vec + 0x280, &excep_xtlbfill, 0x80);
  106. memcpy(cps_vec + 0x300, &excep_cache, 0x80);
  107. memcpy(cps_vec + 0x380, &excep_genex, 0x80);
  108. memcpy(cps_vec + 0x400, &excep_intex, 0x80);
  109. memcpy(cps_vec + 0x480, &excep_ejtag, 0x80);
  110. /* Make sure no prefetched data in cache */
  111. blast_inv_dcache_range(CKSEG0ADDR_OR_64BIT(cps_vec_pa), CKSEG0ADDR_OR_64BIT(cps_vec_pa) + BEV_VEC_SIZE);
  112. bc_inv(CKSEG0ADDR_OR_64BIT(cps_vec_pa), BEV_VEC_SIZE);
  113. __sync();
  114. }
  115. static void __init cps_smp_setup(void)
  116. {
  117. unsigned int nclusters, ncores, nvpes, core_vpes;
  118. int cl, c, v;
  119. /* Detect & record VPE topology */
  120. nvpes = 0;
  121. nclusters = mips_cps_numclusters();
  122. pr_info("%s topology ", cpu_has_mips_r6 ? "VP" : "VPE");
  123. for (cl = 0; cl < nclusters; cl++) {
  124. if (cl > 0)
  125. pr_cont(",");
  126. pr_cont("{");
  127. ncores = mips_cps_numcores(cl);
  128. for (c = 0; c < ncores; c++) {
  129. core_vpes = core_vpe_count(cl, c);
  130. if (c > 0)
  131. pr_cont(",");
  132. pr_cont("%u", core_vpes);
  133. /* Use the number of VPEs in cluster 0 core 0 for smp_num_siblings */
  134. if (!cl && !c)
  135. smp_num_siblings = core_vpes;
  136. for (v = 0; v < min_t(int, core_vpes, NR_CPUS - nvpes); v++) {
  137. cpu_set_cluster(&cpu_data[nvpes + v], cl);
  138. cpu_set_core(&cpu_data[nvpes + v], c);
  139. cpu_set_vpe_id(&cpu_data[nvpes + v], v);
  140. }
  141. nvpes += core_vpes;
  142. }
  143. pr_cont("}");
  144. }
  145. pr_cont(" total %u\n", nvpes);
  146. /* Indicate present CPUs (CPU being synonymous with VPE) */
  147. for (v = 0; v < min_t(unsigned, nvpes, NR_CPUS); v++) {
  148. set_cpu_possible(v, cpu_cluster(&cpu_data[v]) == 0);
  149. set_cpu_present(v, cpu_cluster(&cpu_data[v]) == 0);
  150. __cpu_number_map[v] = v;
  151. __cpu_logical_map[v] = v;
  152. }
  153. /* Set a coherent default CCA (CWB) */
  154. change_c0_config(CONF_CM_CMASK, 0x5);
  155. /* Core 0 is powered up (we're running on it) */
  156. bitmap_set(core_power, 0, 1);
  157. /* Initialise core 0 */
  158. mips_cps_core_init();
  159. /* Make core 0 coherent with everything */
  160. write_gcr_cl_coherence(0xff);
  161. if (allocate_cps_vecs())
  162. pr_err("Failed to allocate CPS vectors\n");
  163. if (core_entry_reg && mips_cm_revision() >= CM_REV_CM3)
  164. write_gcr_bev_base(core_entry_reg);
  165. #ifdef CONFIG_MIPS_MT_FPAFF
  166. /* If we have an FPU, enroll ourselves in the FPU-full mask */
  167. if (cpu_has_fpu)
  168. cpumask_set_cpu(0, &mt_fpu_cpumask);
  169. #endif /* CONFIG_MIPS_MT_FPAFF */
  170. }
  171. static void __init cps_prepare_cpus(unsigned int max_cpus)
  172. {
  173. unsigned ncores, core_vpes, c, cca;
  174. bool cca_unsuitable, cores_limited;
  175. mips_mt_set_cpuoptions();
  176. if (!core_entry_reg) {
  177. pr_err("core_entry address unsuitable, disabling smp-cps\n");
  178. goto err_out;
  179. }
  180. /* Detect whether the CCA is unsuited to multi-core SMP */
  181. cca = read_c0_config() & CONF_CM_CMASK;
  182. switch (cca) {
  183. case 0x4: /* CWBE */
  184. case 0x5: /* CWB */
  185. /* The CCA is coherent, multi-core is fine */
  186. cca_unsuitable = false;
  187. break;
  188. default:
  189. /* CCA is not coherent, multi-core is not usable */
  190. cca_unsuitable = true;
  191. }
  192. /* Warn the user if the CCA prevents multi-core */
  193. cores_limited = false;
  194. if (cca_unsuitable || cpu_has_dc_aliases) {
  195. for_each_present_cpu(c) {
  196. if (cpus_are_siblings(smp_processor_id(), c))
  197. continue;
  198. set_cpu_present(c, false);
  199. cores_limited = true;
  200. }
  201. }
  202. if (cores_limited)
  203. pr_warn("Using only one core due to %s%s%s\n",
  204. cca_unsuitable ? "unsuitable CCA" : "",
  205. (cca_unsuitable && cpu_has_dc_aliases) ? " & " : "",
  206. cpu_has_dc_aliases ? "dcache aliasing" : "");
  207. setup_cps_vecs();
  208. /* Allocate core boot configuration structs */
  209. ncores = mips_cps_numcores(0);
  210. mips_cps_core_bootcfg = kcalloc(ncores, sizeof(*mips_cps_core_bootcfg),
  211. GFP_KERNEL);
  212. if (!mips_cps_core_bootcfg) {
  213. pr_err("Failed to allocate boot config for %u cores\n", ncores);
  214. goto err_out;
  215. }
  216. /* Allocate VPE boot configuration structs */
  217. for (c = 0; c < ncores; c++) {
  218. core_vpes = core_vpe_count(0, c);
  219. mips_cps_core_bootcfg[c].vpe_config = kcalloc(core_vpes,
  220. sizeof(*mips_cps_core_bootcfg[c].vpe_config),
  221. GFP_KERNEL);
  222. if (!mips_cps_core_bootcfg[c].vpe_config) {
  223. pr_err("Failed to allocate %u VPE boot configs\n",
  224. core_vpes);
  225. goto err_out;
  226. }
  227. }
  228. /* Mark this CPU as booted */
  229. atomic_set(&mips_cps_core_bootcfg[cpu_core(&current_cpu_data)].vpe_mask,
  230. 1 << cpu_vpe_id(&current_cpu_data));
  231. return;
  232. err_out:
  233. /* Clean up allocations */
  234. if (mips_cps_core_bootcfg) {
  235. for (c = 0; c < ncores; c++)
  236. kfree(mips_cps_core_bootcfg[c].vpe_config);
  237. kfree(mips_cps_core_bootcfg);
  238. mips_cps_core_bootcfg = NULL;
  239. }
  240. /* Effectively disable SMP by declaring CPUs not present */
  241. for_each_possible_cpu(c) {
  242. if (c == 0)
  243. continue;
  244. set_cpu_present(c, false);
  245. }
  246. }
  247. static void boot_core(unsigned int core, unsigned int vpe_id)
  248. {
  249. u32 stat, seq_state;
  250. unsigned timeout;
  251. /* Select the appropriate core */
  252. mips_cm_lock_other(0, core, 0, CM_GCR_Cx_OTHER_BLOCK_LOCAL);
  253. /* Set its reset vector */
  254. write_gcr_co_reset_base(core_entry_reg);
  255. /* Ensure its coherency is disabled */
  256. write_gcr_co_coherence(0);
  257. /* Start it with the legacy memory map and exception base */
  258. write_gcr_co_reset_ext_base(CM_GCR_Cx_RESET_EXT_BASE_UEB);
  259. /* Ensure the core can access the GCRs */
  260. if (mips_cm_revision() < CM_REV_CM3)
  261. set_gcr_access(1 << core);
  262. else
  263. set_gcr_access_cm3(1 << core);
  264. if (mips_cpc_present()) {
  265. /* Reset the core */
  266. mips_cpc_lock_other(core);
  267. if (mips_cm_revision() >= CM_REV_CM3) {
  268. /* Run only the requested VP following the reset */
  269. write_cpc_co_vp_stop(0xf);
  270. write_cpc_co_vp_run(1 << vpe_id);
  271. /*
  272. * Ensure that the VP_RUN register is written before the
  273. * core leaves reset.
  274. */
  275. wmb();
  276. }
  277. write_cpc_co_cmd(CPC_Cx_CMD_RESET);
  278. timeout = 100;
  279. while (true) {
  280. stat = read_cpc_co_stat_conf();
  281. seq_state = stat & CPC_Cx_STAT_CONF_SEQSTATE;
  282. seq_state >>= __ffs(CPC_Cx_STAT_CONF_SEQSTATE);
  283. /* U6 == coherent execution, ie. the core is up */
  284. if (seq_state == CPC_Cx_STAT_CONF_SEQSTATE_U6)
  285. break;
  286. /* Delay a little while before we start warning */
  287. if (timeout) {
  288. timeout--;
  289. mdelay(10);
  290. continue;
  291. }
  292. pr_warn("Waiting for core %u to start... STAT_CONF=0x%x\n",
  293. core, stat);
  294. mdelay(1000);
  295. }
  296. mips_cpc_unlock_other();
  297. } else {
  298. /* Take the core out of reset */
  299. write_gcr_co_reset_release(0);
  300. }
  301. mips_cm_unlock_other();
  302. /* The core is now powered up */
  303. bitmap_set(core_power, core, 1);
  304. }
  305. static void remote_vpe_boot(void *dummy)
  306. {
  307. unsigned core = cpu_core(&current_cpu_data);
  308. struct core_boot_config *core_cfg = &mips_cps_core_bootcfg[core];
  309. mips_cps_boot_vpes(core_cfg, cpu_vpe_id(&current_cpu_data));
  310. }
  311. static int cps_boot_secondary(int cpu, struct task_struct *idle)
  312. {
  313. unsigned core = cpu_core(&cpu_data[cpu]);
  314. unsigned vpe_id = cpu_vpe_id(&cpu_data[cpu]);
  315. struct core_boot_config *core_cfg = &mips_cps_core_bootcfg[core];
  316. struct vpe_boot_config *vpe_cfg = &core_cfg->vpe_config[vpe_id];
  317. unsigned int remote;
  318. int err;
  319. /* We don't yet support booting CPUs in other clusters */
  320. if (cpu_cluster(&cpu_data[cpu]) != cpu_cluster(&raw_current_cpu_data))
  321. return -ENOSYS;
  322. vpe_cfg->pc = (unsigned long)&smp_bootstrap;
  323. vpe_cfg->sp = __KSTK_TOS(idle);
  324. vpe_cfg->gp = (unsigned long)task_thread_info(idle);
  325. atomic_or(1 << cpu_vpe_id(&cpu_data[cpu]), &core_cfg->vpe_mask);
  326. preempt_disable();
  327. if (!test_bit(core, core_power)) {
  328. /* Boot a VPE on a powered down core */
  329. boot_core(core, vpe_id);
  330. goto out;
  331. }
  332. if (cpu_has_vp) {
  333. mips_cm_lock_other(0, core, vpe_id, CM_GCR_Cx_OTHER_BLOCK_LOCAL);
  334. write_gcr_co_reset_base(core_entry_reg);
  335. mips_cm_unlock_other();
  336. }
  337. if (!cpus_are_siblings(cpu, smp_processor_id())) {
  338. /* Boot a VPE on another powered up core */
  339. for (remote = 0; remote < NR_CPUS; remote++) {
  340. if (!cpus_are_siblings(cpu, remote))
  341. continue;
  342. if (cpu_online(remote))
  343. break;
  344. }
  345. if (remote >= NR_CPUS) {
  346. pr_crit("No online CPU in core %u to start CPU%d\n",
  347. core, cpu);
  348. goto out;
  349. }
  350. err = smp_call_function_single(remote, remote_vpe_boot,
  351. NULL, 1);
  352. if (err)
  353. panic("Failed to call remote CPU\n");
  354. goto out;
  355. }
  356. BUG_ON(!cpu_has_mipsmt && !cpu_has_vp);
  357. /* Boot a VPE on this core */
  358. mips_cps_boot_vpes(core_cfg, vpe_id);
  359. out:
  360. preempt_enable();
  361. return 0;
  362. }
  363. static void cps_init_secondary(void)
  364. {
  365. int core = cpu_core(&current_cpu_data);
  366. /* Disable MT - we only want to run 1 TC per VPE */
  367. if (cpu_has_mipsmt)
  368. dmt();
  369. if (mips_cm_revision() >= CM_REV_CM3) {
  370. unsigned int ident = read_gic_vl_ident();
  371. /*
  372. * Ensure that our calculation of the VP ID matches up with
  373. * what the GIC reports, otherwise we'll have configured
  374. * interrupts incorrectly.
  375. */
  376. BUG_ON(ident != mips_cm_vp_id(smp_processor_id()));
  377. }
  378. if (core > 0 && !read_gcr_cl_coherence())
  379. pr_warn("Core %u is not in coherent domain\n", core);
  380. if (cpu_has_veic)
  381. clear_c0_status(ST0_IM);
  382. else
  383. change_c0_status(ST0_IM, STATUSF_IP2 | STATUSF_IP3 |
  384. STATUSF_IP4 | STATUSF_IP5 |
  385. STATUSF_IP6 | STATUSF_IP7);
  386. }
  387. static void cps_smp_finish(void)
  388. {
  389. write_c0_compare(read_c0_count() + (8 * mips_hpt_frequency / HZ));
  390. #ifdef CONFIG_MIPS_MT_FPAFF
  391. /* If we have an FPU, enroll ourselves in the FPU-full mask */
  392. if (cpu_has_fpu)
  393. cpumask_set_cpu(smp_processor_id(), &mt_fpu_cpumask);
  394. #endif /* CONFIG_MIPS_MT_FPAFF */
  395. local_irq_enable();
  396. }
  397. #if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_KEXEC_CORE)
  398. enum cpu_death {
  399. CPU_DEATH_HALT,
  400. CPU_DEATH_POWER,
  401. };
  402. static void cps_shutdown_this_cpu(enum cpu_death death)
  403. {
  404. unsigned int cpu, core, vpe_id;
  405. cpu = smp_processor_id();
  406. core = cpu_core(&cpu_data[cpu]);
  407. if (death == CPU_DEATH_HALT) {
  408. vpe_id = cpu_vpe_id(&cpu_data[cpu]);
  409. pr_debug("Halting core %d VP%d\n", core, vpe_id);
  410. if (cpu_has_mipsmt) {
  411. /* Halt this TC */
  412. write_c0_tchalt(TCHALT_H);
  413. instruction_hazard();
  414. } else if (cpu_has_vp) {
  415. write_cpc_cl_vp_stop(1 << vpe_id);
  416. /* Ensure that the VP_STOP register is written */
  417. wmb();
  418. }
  419. } else {
  420. if (IS_ENABLED(CONFIG_HOTPLUG_CPU)) {
  421. pr_debug("Gating power to core %d\n", core);
  422. /* Power down the core */
  423. cps_pm_enter_state(CPS_PM_POWER_GATED);
  424. }
  425. }
  426. }
  427. #ifdef CONFIG_KEXEC_CORE
  428. static void cps_kexec_nonboot_cpu(void)
  429. {
  430. if (cpu_has_mipsmt || cpu_has_vp)
  431. cps_shutdown_this_cpu(CPU_DEATH_HALT);
  432. else
  433. cps_shutdown_this_cpu(CPU_DEATH_POWER);
  434. }
  435. #endif /* CONFIG_KEXEC_CORE */
  436. #endif /* CONFIG_HOTPLUG_CPU || CONFIG_KEXEC_CORE */
  437. #ifdef CONFIG_HOTPLUG_CPU
  438. static int cps_cpu_disable(void)
  439. {
  440. unsigned cpu = smp_processor_id();
  441. struct core_boot_config *core_cfg;
  442. if (!cps_pm_support_state(CPS_PM_POWER_GATED))
  443. return -EINVAL;
  444. core_cfg = &mips_cps_core_bootcfg[cpu_core(&current_cpu_data)];
  445. atomic_sub(1 << cpu_vpe_id(&current_cpu_data), &core_cfg->vpe_mask);
  446. smp_mb__after_atomic();
  447. set_cpu_online(cpu, false);
  448. calculate_cpu_foreign_map();
  449. irq_migrate_all_off_this_cpu();
  450. return 0;
  451. }
  452. static unsigned cpu_death_sibling;
  453. static enum cpu_death cpu_death;
  454. void play_dead(void)
  455. {
  456. unsigned int cpu;
  457. local_irq_disable();
  458. idle_task_exit();
  459. cpu = smp_processor_id();
  460. cpu_death = CPU_DEATH_POWER;
  461. pr_debug("CPU%d going offline\n", cpu);
  462. if (cpu_has_mipsmt || cpu_has_vp) {
  463. /* Look for another online VPE within the core */
  464. for_each_online_cpu(cpu_death_sibling) {
  465. if (!cpus_are_siblings(cpu, cpu_death_sibling))
  466. continue;
  467. /*
  468. * There is an online VPE within the core. Just halt
  469. * this TC and leave the core alone.
  470. */
  471. cpu_death = CPU_DEATH_HALT;
  472. break;
  473. }
  474. }
  475. cpuhp_ap_report_dead();
  476. cps_shutdown_this_cpu(cpu_death);
  477. /* This should never be reached */
  478. panic("Failed to offline CPU %u", cpu);
  479. }
  480. static void wait_for_sibling_halt(void *ptr_cpu)
  481. {
  482. unsigned cpu = (unsigned long)ptr_cpu;
  483. unsigned vpe_id = cpu_vpe_id(&cpu_data[cpu]);
  484. unsigned halted;
  485. unsigned long flags;
  486. do {
  487. local_irq_save(flags);
  488. settc(vpe_id);
  489. halted = read_tc_c0_tchalt();
  490. local_irq_restore(flags);
  491. } while (!(halted & TCHALT_H));
  492. }
  493. static void cps_cpu_die(unsigned int cpu) { }
  494. static void cps_cleanup_dead_cpu(unsigned cpu)
  495. {
  496. unsigned core = cpu_core(&cpu_data[cpu]);
  497. unsigned int vpe_id = cpu_vpe_id(&cpu_data[cpu]);
  498. ktime_t fail_time;
  499. unsigned stat;
  500. int err;
  501. /*
  502. * Now wait for the CPU to actually offline. Without doing this that
  503. * offlining may race with one or more of:
  504. *
  505. * - Onlining the CPU again.
  506. * - Powering down the core if another VPE within it is offlined.
  507. * - A sibling VPE entering a non-coherent state.
  508. *
  509. * In the non-MT halt case (ie. infinite loop) the CPU is doing nothing
  510. * with which we could race, so do nothing.
  511. */
  512. if (cpu_death == CPU_DEATH_POWER) {
  513. /*
  514. * Wait for the core to enter a powered down or clock gated
  515. * state, the latter happening when a JTAG probe is connected
  516. * in which case the CPC will refuse to power down the core.
  517. */
  518. fail_time = ktime_add_ms(ktime_get(), 2000);
  519. do {
  520. mips_cm_lock_other(0, core, 0, CM_GCR_Cx_OTHER_BLOCK_LOCAL);
  521. mips_cpc_lock_other(core);
  522. stat = read_cpc_co_stat_conf();
  523. stat &= CPC_Cx_STAT_CONF_SEQSTATE;
  524. stat >>= __ffs(CPC_Cx_STAT_CONF_SEQSTATE);
  525. mips_cpc_unlock_other();
  526. mips_cm_unlock_other();
  527. if (stat == CPC_Cx_STAT_CONF_SEQSTATE_D0 ||
  528. stat == CPC_Cx_STAT_CONF_SEQSTATE_D2 ||
  529. stat == CPC_Cx_STAT_CONF_SEQSTATE_U2)
  530. break;
  531. /*
  532. * The core ought to have powered down, but didn't &
  533. * now we don't really know what state it's in. It's
  534. * likely that its _pwr_up pin has been wired to logic
  535. * 1 & it powered back up as soon as we powered it
  536. * down...
  537. *
  538. * The best we can do is warn the user & continue in
  539. * the hope that the core is doing nothing harmful &
  540. * might behave properly if we online it later.
  541. */
  542. if (WARN(ktime_after(ktime_get(), fail_time),
  543. "CPU%u hasn't powered down, seq. state %u\n",
  544. cpu, stat))
  545. break;
  546. } while (1);
  547. /* Indicate the core is powered off */
  548. bitmap_clear(core_power, core, 1);
  549. } else if (cpu_has_mipsmt) {
  550. /*
  551. * Have a CPU with access to the offlined CPUs registers wait
  552. * for its TC to halt.
  553. */
  554. err = smp_call_function_single(cpu_death_sibling,
  555. wait_for_sibling_halt,
  556. (void *)(unsigned long)cpu, 1);
  557. if (err)
  558. panic("Failed to call remote sibling CPU\n");
  559. } else if (cpu_has_vp) {
  560. do {
  561. mips_cm_lock_other(0, core, vpe_id, CM_GCR_Cx_OTHER_BLOCK_LOCAL);
  562. stat = read_cpc_co_vp_running();
  563. mips_cm_unlock_other();
  564. } while (stat & (1 << vpe_id));
  565. }
  566. }
  567. #endif /* CONFIG_HOTPLUG_CPU */
  568. static const struct plat_smp_ops cps_smp_ops = {
  569. .smp_setup = cps_smp_setup,
  570. .prepare_cpus = cps_prepare_cpus,
  571. .boot_secondary = cps_boot_secondary,
  572. .init_secondary = cps_init_secondary,
  573. .smp_finish = cps_smp_finish,
  574. .send_ipi_single = mips_smp_send_ipi_single,
  575. .send_ipi_mask = mips_smp_send_ipi_mask,
  576. #ifdef CONFIG_HOTPLUG_CPU
  577. .cpu_disable = cps_cpu_disable,
  578. .cpu_die = cps_cpu_die,
  579. .cleanup_dead_cpu = cps_cleanup_dead_cpu,
  580. #endif
  581. #ifdef CONFIG_KEXEC_CORE
  582. .kexec_nonboot_cpu = cps_kexec_nonboot_cpu,
  583. #endif
  584. };
  585. bool mips_cps_smp_in_use(void)
  586. {
  587. extern const struct plat_smp_ops *mp_ops;
  588. return mp_ops == &cps_smp_ops;
  589. }
  590. int register_cps_smp_ops(void)
  591. {
  592. if (!mips_cm_present()) {
  593. pr_warn("MIPS CPS SMP unable to proceed without a CM\n");
  594. return -ENODEV;
  595. }
  596. /* check we have a GIC - we need one for IPIs */
  597. if (!(read_gcr_gic_status() & CM_GCR_GIC_STATUS_EX)) {
  598. pr_warn("MIPS CPS SMP unable to proceed without a GIC\n");
  599. return -ENODEV;
  600. }
  601. register_smp_ops(&cps_smp_ops);
  602. return 0;
  603. }