pm-cps.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2014 Imagination Technologies
  4. * Author: Paul Burton <paul.burton@mips.com>
  5. */
  6. #include <linux/cpuhotplug.h>
  7. #include <linux/init.h>
  8. #include <linux/percpu.h>
  9. #include <linux/slab.h>
  10. #include <linux/suspend.h>
  11. #include <asm/asm-offsets.h>
  12. #include <asm/cacheflush.h>
  13. #include <asm/cacheops.h>
  14. #include <asm/idle.h>
  15. #include <asm/mips-cps.h>
  16. #include <asm/mipsmtregs.h>
  17. #include <asm/pm.h>
  18. #include <asm/pm-cps.h>
  19. #include <asm/regdef.h>
  20. #include <asm/smp-cps.h>
  21. #include <asm/uasm.h>
  22. /*
  23. * cps_nc_entry_fn - type of a generated non-coherent state entry function
  24. * @online: the count of online coupled VPEs
  25. * @nc_ready_count: pointer to a non-coherent mapping of the core ready_count
  26. *
  27. * The code entering & exiting non-coherent states is generated at runtime
  28. * using uasm, in order to ensure that the compiler cannot insert a stray
  29. * memory access at an unfortunate time and to allow the generation of optimal
  30. * core-specific code particularly for cache routines. If coupled_coherence
  31. * is non-zero and this is the entry function for the CPS_PM_NC_WAIT state,
  32. * returns the number of VPEs that were in the wait state at the point this
  33. * VPE left it. Returns garbage if coupled_coherence is zero or this is not
  34. * the entry function for CPS_PM_NC_WAIT.
  35. */
  36. typedef unsigned (*cps_nc_entry_fn)(unsigned online, u32 *nc_ready_count);
  37. /*
  38. * The entry point of the generated non-coherent idle state entry/exit
  39. * functions. Actually per-core rather than per-CPU.
  40. */
  41. static DEFINE_PER_CPU_READ_MOSTLY(cps_nc_entry_fn[CPS_PM_STATE_COUNT],
  42. nc_asm_enter);
  43. /* Bitmap indicating which states are supported by the system */
  44. static DECLARE_BITMAP(state_support, CPS_PM_STATE_COUNT);
  45. /*
  46. * Indicates the number of coupled VPEs ready to operate in a non-coherent
  47. * state. Actually per-core rather than per-CPU.
  48. */
  49. static DEFINE_PER_CPU_ALIGNED(u32*, ready_count);
  50. /* Indicates online CPUs coupled with the current CPU */
  51. static DEFINE_PER_CPU_ALIGNED(cpumask_t, online_coupled);
  52. /* Used to synchronize entry to deep idle states */
  53. static DEFINE_PER_CPU_ALIGNED(atomic_t, pm_barrier);
  54. /* Saved CPU state across the CPS_PM_POWER_GATED state */
  55. DEFINE_PER_CPU_ALIGNED(struct mips_static_suspend_state, cps_cpu_state);
  56. /* A somewhat arbitrary number of labels & relocs for uasm */
  57. static struct uasm_label labels[32];
  58. static struct uasm_reloc relocs[32];
  59. bool cps_pm_support_state(enum cps_pm_state state)
  60. {
  61. return test_bit(state, state_support);
  62. }
  63. static void coupled_barrier(atomic_t *a, unsigned online)
  64. {
  65. /*
  66. * This function is effectively the same as
  67. * cpuidle_coupled_parallel_barrier, which can't be used here since
  68. * there's no cpuidle device.
  69. */
  70. if (!coupled_coherence)
  71. return;
  72. smp_mb__before_atomic();
  73. atomic_inc(a);
  74. while (atomic_read(a) < online)
  75. cpu_relax();
  76. if (atomic_inc_return(a) == online * 2) {
  77. atomic_set(a, 0);
  78. return;
  79. }
  80. while (atomic_read(a) > online)
  81. cpu_relax();
  82. }
  83. int cps_pm_enter_state(enum cps_pm_state state)
  84. {
  85. unsigned cpu = smp_processor_id();
  86. unsigned core = cpu_core(&current_cpu_data);
  87. unsigned online, left;
  88. cpumask_t *coupled_mask = this_cpu_ptr(&online_coupled);
  89. u32 *core_ready_count, *nc_core_ready_count;
  90. void *nc_addr;
  91. cps_nc_entry_fn entry;
  92. struct core_boot_config *core_cfg;
  93. struct vpe_boot_config *vpe_cfg;
  94. atomic_t *barrier;
  95. /* Check that there is an entry function for this state */
  96. entry = per_cpu(nc_asm_enter, cpu)[state];
  97. if (!entry)
  98. return -EINVAL;
  99. /* Calculate which coupled CPUs (VPEs) are online */
  100. #if defined(CONFIG_MIPS_MT) || defined(CONFIG_CPU_MIPSR6)
  101. if (cpu_online(cpu)) {
  102. cpumask_and(coupled_mask, cpu_online_mask,
  103. &cpu_sibling_map[cpu]);
  104. online = cpumask_weight(coupled_mask);
  105. cpumask_clear_cpu(cpu, coupled_mask);
  106. } else
  107. #endif
  108. {
  109. cpumask_clear(coupled_mask);
  110. online = 1;
  111. }
  112. /* Setup the VPE to run mips_cps_pm_restore when started again */
  113. if (IS_ENABLED(CONFIG_CPU_PM) && state == CPS_PM_POWER_GATED) {
  114. /* Power gating relies upon CPS SMP */
  115. if (!mips_cps_smp_in_use())
  116. return -EINVAL;
  117. core_cfg = &mips_cps_core_bootcfg[core];
  118. vpe_cfg = &core_cfg->vpe_config[cpu_vpe_id(&current_cpu_data)];
  119. vpe_cfg->pc = (unsigned long)mips_cps_pm_restore;
  120. vpe_cfg->gp = (unsigned long)current_thread_info();
  121. vpe_cfg->sp = 0;
  122. }
  123. /* Indicate that this CPU might not be coherent */
  124. cpumask_clear_cpu(cpu, &cpu_coherent_mask);
  125. smp_mb__after_atomic();
  126. /* Create a non-coherent mapping of the core ready_count */
  127. core_ready_count = per_cpu(ready_count, cpu);
  128. nc_addr = kmap_noncoherent(virt_to_page(core_ready_count),
  129. (unsigned long)core_ready_count);
  130. nc_addr += ((unsigned long)core_ready_count & ~PAGE_MASK);
  131. nc_core_ready_count = nc_addr;
  132. /* Ensure ready_count is zero-initialised before the assembly runs */
  133. WRITE_ONCE(*nc_core_ready_count, 0);
  134. barrier = &per_cpu(pm_barrier, cpumask_first(&cpu_sibling_map[cpu]));
  135. coupled_barrier(barrier, online);
  136. /* Run the generated entry code */
  137. left = entry(online, nc_core_ready_count);
  138. /* Remove the non-coherent mapping of ready_count */
  139. kunmap_noncoherent();
  140. /* Indicate that this CPU is definitely coherent */
  141. cpumask_set_cpu(cpu, &cpu_coherent_mask);
  142. /*
  143. * If this VPE is the first to leave the non-coherent wait state then
  144. * it needs to wake up any coupled VPEs still running their wait
  145. * instruction so that they return to cpuidle, which can then complete
  146. * coordination between the coupled VPEs & provide the governor with
  147. * a chance to reflect on the length of time the VPEs were in the
  148. * idle state.
  149. */
  150. if (coupled_coherence && (state == CPS_PM_NC_WAIT) && (left == online))
  151. arch_send_call_function_ipi_mask(coupled_mask);
  152. return 0;
  153. }
  154. static void cps_gen_cache_routine(u32 **pp, struct uasm_label **pl,
  155. struct uasm_reloc **pr,
  156. const struct cache_desc *cache,
  157. unsigned op, int lbl)
  158. {
  159. unsigned cache_size = cache->ways << cache->waybit;
  160. unsigned i;
  161. const unsigned unroll_lines = 32;
  162. /* If the cache isn't present this function has it easy */
  163. if (cache->flags & MIPS_CACHE_NOT_PRESENT)
  164. return;
  165. /* Load base address */
  166. UASM_i_LA(pp, GPR_T0, (long)CKSEG0);
  167. /* Calculate end address */
  168. if (cache_size < 0x8000)
  169. uasm_i_addiu(pp, GPR_T1, GPR_T0, cache_size);
  170. else
  171. UASM_i_LA(pp, GPR_T1, (long)(CKSEG0 + cache_size));
  172. /* Start of cache op loop */
  173. uasm_build_label(pl, *pp, lbl);
  174. /* Generate the cache ops */
  175. for (i = 0; i < unroll_lines; i++) {
  176. if (cpu_has_mips_r6) {
  177. uasm_i_cache(pp, op, 0, GPR_T0);
  178. uasm_i_addiu(pp, GPR_T0, GPR_T0, cache->linesz);
  179. } else {
  180. uasm_i_cache(pp, op, i * cache->linesz, GPR_T0);
  181. }
  182. }
  183. if (!cpu_has_mips_r6)
  184. /* Update the base address */
  185. uasm_i_addiu(pp, GPR_T0, GPR_T0, unroll_lines * cache->linesz);
  186. /* Loop if we haven't reached the end address yet */
  187. uasm_il_bne(pp, pr, GPR_T0, GPR_T1, lbl);
  188. uasm_i_nop(pp);
  189. }
  190. static int cps_gen_flush_fsb(u32 **pp, struct uasm_label **pl,
  191. struct uasm_reloc **pr,
  192. const struct cpuinfo_mips *cpu_info,
  193. int lbl)
  194. {
  195. unsigned i, fsb_size = 8;
  196. unsigned num_loads = (fsb_size * 3) / 2;
  197. unsigned line_stride = 2;
  198. unsigned line_size = cpu_info->dcache.linesz;
  199. unsigned perf_counter, perf_event;
  200. unsigned revision = cpu_info->processor_id & PRID_REV_MASK;
  201. /*
  202. * Determine whether this CPU requires an FSB flush, and if so which
  203. * performance counter/event reflect stalls due to a full FSB.
  204. */
  205. switch (__get_cpu_type(cpu_info->cputype)) {
  206. case CPU_INTERAPTIV:
  207. perf_counter = 1;
  208. perf_event = 51;
  209. break;
  210. case CPU_PROAPTIV:
  211. /* Newer proAptiv cores don't require this workaround */
  212. if (revision >= PRID_REV_ENCODE_332(1, 1, 0))
  213. return 0;
  214. /* On older ones it's unavailable */
  215. return -1;
  216. default:
  217. /* Assume that the CPU does not need this workaround */
  218. return 0;
  219. }
  220. /*
  221. * Ensure that the fill/store buffer (FSB) is not holding the results
  222. * of a prefetch, since if it is then the CPC sequencer may become
  223. * stuck in the D3 (ClrBus) state whilst entering a low power state.
  224. */
  225. /* Preserve perf counter setup */
  226. uasm_i_mfc0(pp, GPR_T2, 25, (perf_counter * 2) + 0); /* PerfCtlN */
  227. uasm_i_mfc0(pp, GPR_T3, 25, (perf_counter * 2) + 1); /* PerfCntN */
  228. /* Setup perf counter to count FSB full pipeline stalls */
  229. uasm_i_addiu(pp, GPR_T0, GPR_ZERO, (perf_event << 5) | 0xf);
  230. uasm_i_mtc0(pp, GPR_T0, 25, (perf_counter * 2) + 0); /* PerfCtlN */
  231. uasm_i_ehb(pp);
  232. uasm_i_mtc0(pp, GPR_ZERO, 25, (perf_counter * 2) + 1); /* PerfCntN */
  233. uasm_i_ehb(pp);
  234. /* Base address for loads */
  235. UASM_i_LA(pp, GPR_T0, (long)CKSEG0);
  236. /* Start of clear loop */
  237. uasm_build_label(pl, *pp, lbl);
  238. /* Perform some loads to fill the FSB */
  239. for (i = 0; i < num_loads; i++)
  240. uasm_i_lw(pp, GPR_ZERO, i * line_size * line_stride, GPR_T0);
  241. /*
  242. * Invalidate the new D-cache entries so that the cache will need
  243. * refilling (via the FSB) if the loop is executed again.
  244. */
  245. for (i = 0; i < num_loads; i++) {
  246. uasm_i_cache(pp, Hit_Invalidate_D,
  247. i * line_size * line_stride, GPR_T0);
  248. uasm_i_cache(pp, Hit_Writeback_Inv_SD,
  249. i * line_size * line_stride, GPR_T0);
  250. }
  251. /* Barrier ensuring previous cache invalidates are complete */
  252. uasm_i_sync(pp, __SYNC_full);
  253. uasm_i_ehb(pp);
  254. /* Check whether the pipeline stalled due to the FSB being full */
  255. uasm_i_mfc0(pp, GPR_T1, 25, (perf_counter * 2) + 1); /* PerfCntN */
  256. /* Loop if it didn't */
  257. uasm_il_beqz(pp, pr, GPR_T1, lbl);
  258. uasm_i_nop(pp);
  259. /* Restore perf counter 1. The count may well now be wrong... */
  260. uasm_i_mtc0(pp, GPR_T2, 25, (perf_counter * 2) + 0); /* PerfCtlN */
  261. uasm_i_ehb(pp);
  262. uasm_i_mtc0(pp, GPR_T3, 25, (perf_counter * 2) + 1); /* PerfCntN */
  263. uasm_i_ehb(pp);
  264. return 0;
  265. }
  266. static void cps_gen_set_top_bit(u32 **pp, struct uasm_label **pl,
  267. struct uasm_reloc **pr,
  268. unsigned r_addr, int lbl)
  269. {
  270. uasm_i_lui(pp, GPR_T0, uasm_rel_hi(0x80000000));
  271. uasm_build_label(pl, *pp, lbl);
  272. uasm_i_ll(pp, GPR_T1, 0, r_addr);
  273. uasm_i_or(pp, GPR_T1, GPR_T1, GPR_T0);
  274. uasm_i_sc(pp, GPR_T1, 0, r_addr);
  275. uasm_il_beqz(pp, pr, GPR_T1, lbl);
  276. uasm_i_nop(pp);
  277. }
  278. static void *cps_gen_entry_code(unsigned cpu, enum cps_pm_state state)
  279. {
  280. struct uasm_label *l = labels;
  281. struct uasm_reloc *r = relocs;
  282. u32 *buf, *p;
  283. const unsigned r_online = GPR_A0;
  284. const unsigned r_nc_count = GPR_A1;
  285. const unsigned r_pcohctl = GPR_T8;
  286. const unsigned max_instrs = 256;
  287. unsigned cpc_cmd;
  288. int err;
  289. enum {
  290. lbl_incready = 1,
  291. lbl_poll_cont,
  292. lbl_secondary_hang,
  293. lbl_disable_coherence,
  294. lbl_flush_fsb,
  295. lbl_invicache,
  296. lbl_flushdcache,
  297. lbl_hang,
  298. lbl_set_cont,
  299. lbl_secondary_cont,
  300. lbl_decready,
  301. };
  302. /* Allocate a buffer to hold the generated code */
  303. p = buf = kcalloc(max_instrs, sizeof(u32), GFP_KERNEL);
  304. if (!buf)
  305. return NULL;
  306. /* Clear labels & relocs ready for (re)use */
  307. memset(labels, 0, sizeof(labels));
  308. memset(relocs, 0, sizeof(relocs));
  309. if (IS_ENABLED(CONFIG_CPU_PM) && state == CPS_PM_POWER_GATED) {
  310. /* Power gating relies upon CPS SMP */
  311. if (!mips_cps_smp_in_use())
  312. goto out_err;
  313. /*
  314. * Save CPU state. Note the non-standard calling convention
  315. * with the return address placed in v0 to avoid clobbering
  316. * the ra register before it is saved.
  317. */
  318. UASM_i_LA(&p, GPR_T0, (long)mips_cps_pm_save);
  319. uasm_i_jalr(&p, GPR_V0, GPR_T0);
  320. uasm_i_nop(&p);
  321. }
  322. /*
  323. * Load addresses of required CM & CPC registers. This is done early
  324. * because they're needed in both the enable & disable coherence steps
  325. * but in the coupled case the enable step will only run on one VPE.
  326. */
  327. UASM_i_LA(&p, r_pcohctl, (long)addr_gcr_cl_coherence());
  328. if (coupled_coherence) {
  329. /* Increment ready_count */
  330. uasm_i_sync(&p, __SYNC_mb);
  331. uasm_build_label(&l, p, lbl_incready);
  332. uasm_i_ll(&p, GPR_T1, 0, r_nc_count);
  333. uasm_i_addiu(&p, GPR_T2, GPR_T1, 1);
  334. uasm_i_sc(&p, GPR_T2, 0, r_nc_count);
  335. uasm_il_beqz(&p, &r, GPR_T2, lbl_incready);
  336. uasm_i_addiu(&p, GPR_T1, GPR_T1, 1);
  337. /* Barrier ensuring all CPUs see the updated r_nc_count value */
  338. uasm_i_sync(&p, __SYNC_mb);
  339. /*
  340. * If this is the last VPE to become ready for non-coherence
  341. * then it should branch below.
  342. */
  343. uasm_il_beq(&p, &r, GPR_T1, r_online, lbl_disable_coherence);
  344. uasm_i_nop(&p);
  345. if (state < CPS_PM_POWER_GATED) {
  346. /*
  347. * Otherwise this is not the last VPE to become ready
  348. * for non-coherence. It needs to wait until coherence
  349. * has been disabled before proceeding, which it will do
  350. * by polling for the top bit of ready_count being set.
  351. */
  352. uasm_i_addiu(&p, GPR_T1, GPR_ZERO, -1);
  353. uasm_build_label(&l, p, lbl_poll_cont);
  354. uasm_i_lw(&p, GPR_T0, 0, r_nc_count);
  355. uasm_il_bltz(&p, &r, GPR_T0, lbl_secondary_cont);
  356. uasm_i_ehb(&p);
  357. if (cpu_has_mipsmt)
  358. uasm_i_yield(&p, GPR_ZERO, GPR_T1);
  359. uasm_il_b(&p, &r, lbl_poll_cont);
  360. uasm_i_nop(&p);
  361. } else {
  362. /*
  363. * The core will lose power & this VPE will not continue
  364. * so it can simply halt here.
  365. */
  366. if (cpu_has_mipsmt) {
  367. /* Halt the VPE via C0 tchalt register */
  368. uasm_i_addiu(&p, GPR_T0, GPR_ZERO, TCHALT_H);
  369. uasm_i_mtc0(&p, GPR_T0, 2, 4);
  370. } else if (cpu_has_vp) {
  371. /* Halt the VP via the CPC VP_STOP register */
  372. unsigned int vpe_id;
  373. vpe_id = cpu_vpe_id(&cpu_data[cpu]);
  374. uasm_i_addiu(&p, GPR_T0, GPR_ZERO, 1 << vpe_id);
  375. UASM_i_LA(&p, GPR_T1, (long)addr_cpc_cl_vp_stop());
  376. uasm_i_sw(&p, GPR_T0, 0, GPR_T1);
  377. } else {
  378. BUG();
  379. }
  380. uasm_build_label(&l, p, lbl_secondary_hang);
  381. uasm_il_b(&p, &r, lbl_secondary_hang);
  382. uasm_i_nop(&p);
  383. }
  384. }
  385. /*
  386. * This is the point of no return - this VPE will now proceed to
  387. * disable coherence. At this point we *must* be sure that no other
  388. * VPE within the core will interfere with the L1 dcache.
  389. */
  390. uasm_build_label(&l, p, lbl_disable_coherence);
  391. /* Invalidate the L1 icache */
  392. cps_gen_cache_routine(&p, &l, &r, &cpu_data[cpu].icache,
  393. Index_Invalidate_I, lbl_invicache);
  394. /* Writeback & invalidate the L1 dcache */
  395. cps_gen_cache_routine(&p, &l, &r, &cpu_data[cpu].dcache,
  396. Index_Writeback_Inv_D, lbl_flushdcache);
  397. /* Barrier ensuring previous cache invalidates are complete */
  398. uasm_i_sync(&p, __SYNC_full);
  399. uasm_i_ehb(&p);
  400. if (mips_cm_revision() < CM_REV_CM3) {
  401. /*
  402. * Disable all but self interventions. The load from COHCTL is
  403. * defined by the interAptiv & proAptiv SUMs as ensuring that the
  404. * operation resulting from the preceding store is complete.
  405. */
  406. uasm_i_addiu(&p, GPR_T0, GPR_ZERO, 1 << cpu_core(&cpu_data[cpu]));
  407. uasm_i_sw(&p, GPR_T0, 0, r_pcohctl);
  408. uasm_i_lw(&p, GPR_T0, 0, r_pcohctl);
  409. /* Barrier to ensure write to coherence control is complete */
  410. uasm_i_sync(&p, __SYNC_full);
  411. uasm_i_ehb(&p);
  412. }
  413. /* Disable coherence */
  414. uasm_i_sw(&p, GPR_ZERO, 0, r_pcohctl);
  415. uasm_i_lw(&p, GPR_T0, 0, r_pcohctl);
  416. if (state >= CPS_PM_CLOCK_GATED) {
  417. err = cps_gen_flush_fsb(&p, &l, &r, &cpu_data[cpu],
  418. lbl_flush_fsb);
  419. if (err)
  420. goto out_err;
  421. /* Determine the CPC command to issue */
  422. switch (state) {
  423. case CPS_PM_CLOCK_GATED:
  424. cpc_cmd = CPC_Cx_CMD_CLOCKOFF;
  425. break;
  426. case CPS_PM_POWER_GATED:
  427. cpc_cmd = CPC_Cx_CMD_PWRDOWN;
  428. break;
  429. default:
  430. BUG();
  431. goto out_err;
  432. }
  433. /* Issue the CPC command */
  434. UASM_i_LA(&p, GPR_T0, (long)addr_cpc_cl_cmd());
  435. uasm_i_addiu(&p, GPR_T1, GPR_ZERO, cpc_cmd);
  436. uasm_i_sw(&p, GPR_T1, 0, GPR_T0);
  437. if (state == CPS_PM_POWER_GATED) {
  438. /* If anything goes wrong just hang */
  439. uasm_build_label(&l, p, lbl_hang);
  440. uasm_il_b(&p, &r, lbl_hang);
  441. uasm_i_nop(&p);
  442. /*
  443. * There's no point generating more code, the core is
  444. * powered down & if powered back up will run from the
  445. * reset vector not from here.
  446. */
  447. goto gen_done;
  448. }
  449. /* Barrier to ensure write to CPC command is complete */
  450. uasm_i_sync(&p, __SYNC_full);
  451. uasm_i_ehb(&p);
  452. }
  453. if (state == CPS_PM_NC_WAIT) {
  454. /*
  455. * At this point it is safe for all VPEs to proceed with
  456. * execution. This VPE will set the top bit of ready_count
  457. * to indicate to the other VPEs that they may continue.
  458. */
  459. if (coupled_coherence)
  460. cps_gen_set_top_bit(&p, &l, &r, r_nc_count,
  461. lbl_set_cont);
  462. /*
  463. * VPEs which did not disable coherence will continue
  464. * executing, after coherence has been disabled, from this
  465. * point.
  466. */
  467. uasm_build_label(&l, p, lbl_secondary_cont);
  468. /* Now perform our wait */
  469. uasm_i_wait(&p, 0);
  470. }
  471. /*
  472. * Re-enable coherence. Note that for CPS_PM_NC_WAIT all coupled VPEs
  473. * will run this. The first will actually re-enable coherence & the
  474. * rest will just be performing a rather unusual nop.
  475. */
  476. uasm_i_addiu(&p, GPR_T0, GPR_ZERO, mips_cm_revision() < CM_REV_CM3
  477. ? CM_GCR_Cx_COHERENCE_COHDOMAINEN
  478. : CM3_GCR_Cx_COHERENCE_COHEN);
  479. uasm_i_sw(&p, GPR_T0, 0, r_pcohctl);
  480. uasm_i_lw(&p, GPR_T0, 0, r_pcohctl);
  481. /* Barrier to ensure write to coherence control is complete */
  482. uasm_i_sync(&p, __SYNC_full);
  483. uasm_i_ehb(&p);
  484. if (coupled_coherence && (state == CPS_PM_NC_WAIT)) {
  485. /* Decrement ready_count */
  486. uasm_build_label(&l, p, lbl_decready);
  487. uasm_i_sync(&p, __SYNC_mb);
  488. uasm_i_ll(&p, GPR_T1, 0, r_nc_count);
  489. uasm_i_addiu(&p, GPR_T2, GPR_T1, -1);
  490. uasm_i_sc(&p, GPR_T2, 0, r_nc_count);
  491. uasm_il_beqz(&p, &r, GPR_T2, lbl_decready);
  492. uasm_i_andi(&p, GPR_V0, GPR_T1, (1 << fls(smp_num_siblings)) - 1);
  493. /* Barrier ensuring all CPUs see the updated r_nc_count value */
  494. uasm_i_sync(&p, __SYNC_mb);
  495. }
  496. if (coupled_coherence && (state == CPS_PM_CLOCK_GATED)) {
  497. /*
  498. * At this point it is safe for all VPEs to proceed with
  499. * execution. This VPE will set the top bit of ready_count
  500. * to indicate to the other VPEs that they may continue.
  501. */
  502. cps_gen_set_top_bit(&p, &l, &r, r_nc_count, lbl_set_cont);
  503. /*
  504. * This core will be reliant upon another core sending a
  505. * power-up command to the CPC in order to resume operation.
  506. * Thus an arbitrary VPE can't trigger the core leaving the
  507. * idle state and the one that disables coherence might as well
  508. * be the one to re-enable it. The rest will continue from here
  509. * after that has been done.
  510. */
  511. uasm_build_label(&l, p, lbl_secondary_cont);
  512. /* Barrier ensuring all CPUs see the updated r_nc_count value */
  513. uasm_i_sync(&p, __SYNC_mb);
  514. }
  515. /* The core is coherent, time to return to C code */
  516. uasm_i_jr(&p, GPR_RA);
  517. uasm_i_nop(&p);
  518. gen_done:
  519. /* Ensure the code didn't exceed the resources allocated for it */
  520. BUG_ON((p - buf) > max_instrs);
  521. BUG_ON((l - labels) > ARRAY_SIZE(labels));
  522. BUG_ON((r - relocs) > ARRAY_SIZE(relocs));
  523. /* Patch branch offsets */
  524. uasm_resolve_relocs(relocs, labels);
  525. /* Flush the icache */
  526. local_flush_icache_range((unsigned long)buf, (unsigned long)p);
  527. return buf;
  528. out_err:
  529. kfree(buf);
  530. return NULL;
  531. }
  532. static int cps_pm_online_cpu(unsigned int cpu)
  533. {
  534. unsigned int sibling, core;
  535. void *entry_fn, *core_rc;
  536. enum cps_pm_state state;
  537. core = cpu_core(&cpu_data[cpu]);
  538. for (state = CPS_PM_NC_WAIT; state < CPS_PM_STATE_COUNT; state++) {
  539. if (per_cpu(nc_asm_enter, cpu)[state])
  540. continue;
  541. if (!test_bit(state, state_support))
  542. continue;
  543. entry_fn = cps_gen_entry_code(cpu, state);
  544. if (!entry_fn) {
  545. pr_err("Failed to generate core %u state %u entry\n",
  546. core, state);
  547. clear_bit(state, state_support);
  548. }
  549. for_each_cpu(sibling, &cpu_sibling_map[cpu])
  550. per_cpu(nc_asm_enter, sibling)[state] = entry_fn;
  551. }
  552. if (!per_cpu(ready_count, cpu)) {
  553. core_rc = kmalloc(sizeof(u32), GFP_KERNEL);
  554. if (!core_rc) {
  555. pr_err("Failed allocate core %u ready_count\n", core);
  556. return -ENOMEM;
  557. }
  558. for_each_cpu(sibling, &cpu_sibling_map[cpu])
  559. per_cpu(ready_count, sibling) = core_rc;
  560. }
  561. return 0;
  562. }
  563. static int cps_pm_power_notifier(struct notifier_block *this,
  564. unsigned long event, void *ptr)
  565. {
  566. unsigned int stat;
  567. switch (event) {
  568. case PM_SUSPEND_PREPARE:
  569. stat = read_cpc_cl_stat_conf();
  570. /*
  571. * If we're attempting to suspend the system and power down all
  572. * of the cores, the JTAG detect bit indicates that the CPC will
  573. * instead put the cores into clock-off state. In this state
  574. * a connected debugger can cause the CPU to attempt
  575. * interactions with the powered down system. At best this will
  576. * fail. At worst, it can hang the NoC, requiring a hard reset.
  577. * To avoid this, just block system suspend if a JTAG probe
  578. * is detected.
  579. */
  580. if (stat & CPC_Cx_STAT_CONF_EJTAG_PROBE) {
  581. pr_warn("JTAG probe is connected - abort suspend\n");
  582. return NOTIFY_BAD;
  583. }
  584. return NOTIFY_DONE;
  585. default:
  586. return NOTIFY_DONE;
  587. }
  588. }
  589. static int __init cps_pm_init(void)
  590. {
  591. /* A CM is required for all non-coherent states */
  592. if (!mips_cm_present()) {
  593. pr_warn("pm-cps: no CM, non-coherent states unavailable\n");
  594. return 0;
  595. }
  596. /*
  597. * If interrupts were enabled whilst running a wait instruction on a
  598. * non-coherent core then the VPE may end up processing interrupts
  599. * whilst non-coherent. That would be bad.
  600. */
  601. if (cpu_wait == r4k_wait_irqoff)
  602. set_bit(CPS_PM_NC_WAIT, state_support);
  603. else
  604. pr_warn("pm-cps: non-coherent wait unavailable\n");
  605. /* Detect whether a CPC is present */
  606. if (mips_cpc_present()) {
  607. /* Detect whether clock gating is implemented */
  608. if (read_cpc_cl_stat_conf() & CPC_Cx_STAT_CONF_CLKGAT_IMPL)
  609. set_bit(CPS_PM_CLOCK_GATED, state_support);
  610. else
  611. pr_warn("pm-cps: CPC does not support clock gating\n");
  612. /* Power gating is available with CPS SMP & any CPC */
  613. if (mips_cps_smp_in_use())
  614. set_bit(CPS_PM_POWER_GATED, state_support);
  615. else
  616. pr_warn("pm-cps: CPS SMP not in use, power gating unavailable\n");
  617. } else {
  618. pr_warn("pm-cps: no CPC, clock & power gating unavailable\n");
  619. }
  620. pm_notifier(cps_pm_power_notifier, 0);
  621. return cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "mips/cps_pm:online",
  622. cps_pm_online_cpu, NULL);
  623. }
  624. arch_initcall(cps_pm_init);