pm-cps.c 20 KB

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