pm-cps.c 21 KB

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