2 * Copyright (C) 2014 Imagination Technologies
3 * Author: Paul Burton <paul.burton@imgtec.com>
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.
11 #include <linux/init.h>
12 #include <linux/percpu.h>
13 #include <linux/slab.h>
15 #include <asm/asm-offsets.h>
16 #include <asm/cacheflush.h>
17 #include <asm/cacheops.h>
19 #include <asm/mips-cm.h>
20 #include <asm/mips-cpc.h>
21 #include <asm/mipsmtregs.h>
23 #include <asm/pm-cps.h>
24 #include <asm/smp-cps.h>
28 * cps_nc_entry_fn - type of a generated non-coherent state entry function
29 * @online: the count of online coupled VPEs
30 * @nc_ready_count: pointer to a non-coherent mapping of the core ready_count
32 * The code entering & exiting non-coherent states is generated at runtime
33 * using uasm, in order to ensure that the compiler cannot insert a stray
34 * memory access at an unfortunate time and to allow the generation of optimal
35 * core-specific code particularly for cache routines. If coupled_coherence
36 * is non-zero and this is the entry function for the CPS_PM_NC_WAIT state,
37 * returns the number of VPEs that were in the wait state at the point this
38 * VPE left it. Returns garbage if coupled_coherence is zero or this is not
39 * the entry function for CPS_PM_NC_WAIT.
41 typedef unsigned (*cps_nc_entry_fn)(unsigned online, u32 *nc_ready_count);
44 * The entry point of the generated non-coherent idle state entry/exit
45 * functions. Actually per-core rather than per-CPU.
47 static DEFINE_PER_CPU_READ_MOSTLY(cps_nc_entry_fn[CPS_PM_STATE_COUNT],
50 /* Bitmap indicating which states are supported by the system */
51 DECLARE_BITMAP(state_support, CPS_PM_STATE_COUNT);
54 * Indicates the number of coupled VPEs ready to operate in a non-coherent
55 * state. Actually per-core rather than per-CPU.
57 static DEFINE_PER_CPU_ALIGNED(u32*, ready_count);
58 static DEFINE_PER_CPU_ALIGNED(void*, ready_count_alloc);
60 /* Indicates online CPUs coupled with the current CPU */
61 static DEFINE_PER_CPU_ALIGNED(cpumask_t, online_coupled);
64 * Used to synchronize entry to deep idle states. Actually per-core rather
67 static DEFINE_PER_CPU_ALIGNED(atomic_t, pm_barrier);
69 /* Saved CPU state across the CPS_PM_POWER_GATED state */
70 DEFINE_PER_CPU_ALIGNED(struct mips_static_suspend_state, cps_cpu_state);
72 /* A somewhat arbitrary number of labels & relocs for uasm */
73 static struct uasm_label labels[32] __initdata;
74 static struct uasm_reloc relocs[32] __initdata;
76 /* CPU dependant sync types */
77 static unsigned stype_intervention;
78 static unsigned stype_memory;
81 zero, at, v0, v1, a0, a1, a2, a3,
82 t0, t1, t2, t3, t4, t5, t6, t7,
83 s0, s1, s2, s3, s4, s5, s6, s7,
84 t8, t9, k0, k1, gp, sp, fp, ra,
87 bool cps_pm_support_state(enum cps_pm_state state)
89 return test_bit(state, state_support);
92 static void coupled_barrier(atomic_t *a, unsigned online)
95 * This function is effectively the same as
96 * cpuidle_coupled_parallel_barrier, which can't be used here since
97 * there's no cpuidle device.
100 if (!coupled_coherence)
103 smp_mb__before_atomic();
106 while (atomic_read(a) < online)
109 if (atomic_inc_return(a) == online * 2) {
114 while (atomic_read(a) > online)
118 int cps_pm_enter_state(enum cps_pm_state state)
120 unsigned cpu = smp_processor_id();
121 unsigned core = current_cpu_data.core;
122 unsigned online, left;
123 cpumask_t *coupled_mask = this_cpu_ptr(&online_coupled);
124 u32 *core_ready_count, *nc_core_ready_count;
126 cps_nc_entry_fn entry;
127 struct core_boot_config *core_cfg;
128 struct vpe_boot_config *vpe_cfg;
130 /* Check that there is an entry function for this state */
131 entry = per_cpu(nc_asm_enter, core)[state];
135 /* Calculate which coupled CPUs (VPEs) are online */
136 #ifdef CONFIG_MIPS_MT
137 if (cpu_online(cpu)) {
138 cpumask_and(coupled_mask, cpu_online_mask,
139 &cpu_sibling_map[cpu]);
140 online = cpumask_weight(coupled_mask);
141 cpumask_clear_cpu(cpu, coupled_mask);
145 cpumask_clear(coupled_mask);
149 /* Setup the VPE to run mips_cps_pm_restore when started again */
150 if (IS_ENABLED(CONFIG_CPU_PM) && state == CPS_PM_POWER_GATED) {
151 /* Power gating relies upon CPS SMP */
152 if (!mips_cps_smp_in_use())
155 core_cfg = &mips_cps_core_bootcfg[core];
156 vpe_cfg = &core_cfg->vpe_config[cpu_vpe_id(¤t_cpu_data)];
157 vpe_cfg->pc = (unsigned long)mips_cps_pm_restore;
158 vpe_cfg->gp = (unsigned long)current_thread_info();
162 /* Indicate that this CPU might not be coherent */
163 cpumask_clear_cpu(cpu, &cpu_coherent_mask);
164 smp_mb__after_atomic();
166 /* Create a non-coherent mapping of the core ready_count */
167 core_ready_count = per_cpu(ready_count, core);
168 nc_addr = kmap_noncoherent(virt_to_page(core_ready_count),
169 (unsigned long)core_ready_count);
170 nc_addr += ((unsigned long)core_ready_count & ~PAGE_MASK);
171 nc_core_ready_count = nc_addr;
173 /* Ensure ready_count is zero-initialised before the assembly runs */
174 ACCESS_ONCE(*nc_core_ready_count) = 0;
175 coupled_barrier(&per_cpu(pm_barrier, core), online);
177 /* Run the generated entry code */
178 left = entry(online, nc_core_ready_count);
180 /* Remove the non-coherent mapping of ready_count */
181 kunmap_noncoherent();
183 /* Indicate that this CPU is definitely coherent */
184 cpumask_set_cpu(cpu, &cpu_coherent_mask);
187 * If this VPE is the first to leave the non-coherent wait state then
188 * it needs to wake up any coupled VPEs still running their wait
189 * instruction so that they return to cpuidle, which can then complete
190 * coordination between the coupled VPEs & provide the governor with
191 * a chance to reflect on the length of time the VPEs were in the
194 if (coupled_coherence && (state == CPS_PM_NC_WAIT) && (left == online))
195 arch_send_call_function_ipi_mask(coupled_mask);
200 static void __init cps_gen_cache_routine(u32 **pp, struct uasm_label **pl,
201 struct uasm_reloc **pr,
202 const struct cache_desc *cache,
203 unsigned op, int lbl)
205 unsigned cache_size = cache->ways << cache->waybit;
207 const unsigned unroll_lines = 32;
209 /* If the cache isn't present this function has it easy */
210 if (cache->flags & MIPS_CACHE_NOT_PRESENT)
213 /* Load base address */
214 UASM_i_LA(pp, t0, (long)CKSEG0);
216 /* Calculate end address */
217 if (cache_size < 0x8000)
218 uasm_i_addiu(pp, t1, t0, cache_size);
220 UASM_i_LA(pp, t1, (long)(CKSEG0 + cache_size));
222 /* Start of cache op loop */
223 uasm_build_label(pl, *pp, lbl);
225 /* Generate the cache ops */
226 for (i = 0; i < unroll_lines; i++) {
227 if (cpu_has_mips_r6) {
228 uasm_i_cache(pp, op, 0, t0);
229 uasm_i_addiu(pp, t0, t0, cache->linesz);
231 uasm_i_cache(pp, op, i * cache->linesz, t0);
235 if (!cpu_has_mips_r6)
236 /* Update the base address */
237 uasm_i_addiu(pp, t0, t0, unroll_lines * cache->linesz);
239 /* Loop if we haven't reached the end address yet */
240 uasm_il_bne(pp, pr, t0, t1, lbl);
244 static int __init cps_gen_flush_fsb(u32 **pp, struct uasm_label **pl,
245 struct uasm_reloc **pr,
246 const struct cpuinfo_mips *cpu_info,
249 unsigned i, fsb_size = 8;
250 unsigned num_loads = (fsb_size * 3) / 2;
251 unsigned line_stride = 2;
252 unsigned line_size = cpu_info->dcache.linesz;
253 unsigned perf_counter, perf_event;
254 unsigned revision = cpu_info->processor_id & PRID_REV_MASK;
257 * Determine whether this CPU requires an FSB flush, and if so which
258 * performance counter/event reflect stalls due to a full FSB.
260 switch (__get_cpu_type(cpu_info->cputype)) {
267 /* Newer proAptiv cores don't require this workaround */
268 if (revision >= PRID_REV_ENCODE_332(1, 1, 0))
271 /* On older ones it's unavailable */
275 /* Assume that the CPU does not need this workaround */
280 * Ensure that the fill/store buffer (FSB) is not holding the results
281 * of a prefetch, since if it is then the CPC sequencer may become
282 * stuck in the D3 (ClrBus) state whilst entering a low power state.
285 /* Preserve perf counter setup */
286 uasm_i_mfc0(pp, t2, 25, (perf_counter * 2) + 0); /* PerfCtlN */
287 uasm_i_mfc0(pp, t3, 25, (perf_counter * 2) + 1); /* PerfCntN */
289 /* Setup perf counter to count FSB full pipeline stalls */
290 uasm_i_addiu(pp, t0, zero, (perf_event << 5) | 0xf);
291 uasm_i_mtc0(pp, t0, 25, (perf_counter * 2) + 0); /* PerfCtlN */
293 uasm_i_mtc0(pp, zero, 25, (perf_counter * 2) + 1); /* PerfCntN */
296 /* Base address for loads */
297 UASM_i_LA(pp, t0, (long)CKSEG0);
299 /* Start of clear loop */
300 uasm_build_label(pl, *pp, lbl);
302 /* Perform some loads to fill the FSB */
303 for (i = 0; i < num_loads; i++)
304 uasm_i_lw(pp, zero, i * line_size * line_stride, t0);
307 * Invalidate the new D-cache entries so that the cache will need
308 * refilling (via the FSB) if the loop is executed again.
310 for (i = 0; i < num_loads; i++) {
311 uasm_i_cache(pp, Hit_Invalidate_D,
312 i * line_size * line_stride, t0);
313 uasm_i_cache(pp, Hit_Writeback_Inv_SD,
314 i * line_size * line_stride, t0);
317 /* Barrier ensuring previous cache invalidates are complete */
318 uasm_i_sync(pp, STYPE_SYNC);
321 /* Check whether the pipeline stalled due to the FSB being full */
322 uasm_i_mfc0(pp, t1, 25, (perf_counter * 2) + 1); /* PerfCntN */
324 /* Loop if it didn't */
325 uasm_il_beqz(pp, pr, t1, lbl);
328 /* Restore perf counter 1. The count may well now be wrong... */
329 uasm_i_mtc0(pp, t2, 25, (perf_counter * 2) + 0); /* PerfCtlN */
331 uasm_i_mtc0(pp, t3, 25, (perf_counter * 2) + 1); /* PerfCntN */
337 static void __init cps_gen_set_top_bit(u32 **pp, struct uasm_label **pl,
338 struct uasm_reloc **pr,
339 unsigned r_addr, int lbl)
341 uasm_i_lui(pp, t0, uasm_rel_hi(0x80000000));
342 uasm_build_label(pl, *pp, lbl);
343 uasm_i_ll(pp, t1, 0, r_addr);
344 uasm_i_or(pp, t1, t1, t0);
345 uasm_i_sc(pp, t1, 0, r_addr);
346 uasm_il_beqz(pp, pr, t1, lbl);
350 static void * __init cps_gen_entry_code(unsigned cpu, enum cps_pm_state state)
352 struct uasm_label *l = labels;
353 struct uasm_reloc *r = relocs;
355 const unsigned r_online = a0;
356 const unsigned r_nc_count = a1;
357 const unsigned r_pcohctl = t7;
358 const unsigned max_instrs = 256;
365 lbl_disable_coherence,
375 /* Allocate a buffer to hold the generated code */
376 p = buf = kcalloc(max_instrs, sizeof(u32), GFP_KERNEL);
380 /* Clear labels & relocs ready for (re)use */
381 memset(labels, 0, sizeof(labels));
382 memset(relocs, 0, sizeof(relocs));
384 if (IS_ENABLED(CONFIG_CPU_PM) && state == CPS_PM_POWER_GATED) {
385 /* Power gating relies upon CPS SMP */
386 if (!mips_cps_smp_in_use())
390 * Save CPU state. Note the non-standard calling convention
391 * with the return address placed in v0 to avoid clobbering
392 * the ra register before it is saved.
394 UASM_i_LA(&p, t0, (long)mips_cps_pm_save);
395 uasm_i_jalr(&p, v0, t0);
400 * Load addresses of required CM & CPC registers. This is done early
401 * because they're needed in both the enable & disable coherence steps
402 * but in the coupled case the enable step will only run on one VPE.
404 UASM_i_LA(&p, r_pcohctl, (long)addr_gcr_cl_coherence());
406 if (coupled_coherence) {
407 /* Increment ready_count */
408 uasm_i_sync(&p, STYPE_SYNC_MB);
409 uasm_build_label(&l, p, lbl_incready);
410 uasm_i_ll(&p, t1, 0, r_nc_count);
411 uasm_i_addiu(&p, t2, t1, 1);
412 uasm_i_sc(&p, t2, 0, r_nc_count);
413 uasm_il_beqz(&p, &r, t2, lbl_incready);
414 uasm_i_addiu(&p, t1, t1, 1);
416 /* Barrier ensuring all CPUs see the updated r_nc_count value */
417 uasm_i_sync(&p, STYPE_SYNC_MB);
420 * If this is the last VPE to become ready for non-coherence
421 * then it should branch below.
423 uasm_il_beq(&p, &r, t1, r_online, lbl_disable_coherence);
426 if (state < CPS_PM_POWER_GATED) {
428 * Otherwise this is not the last VPE to become ready
429 * for non-coherence. It needs to wait until coherence
430 * has been disabled before proceeding, which it will do
431 * by polling for the top bit of ready_count being set.
433 uasm_i_addiu(&p, t1, zero, -1);
434 uasm_build_label(&l, p, lbl_poll_cont);
435 uasm_i_lw(&p, t0, 0, r_nc_count);
436 uasm_il_bltz(&p, &r, t0, lbl_secondary_cont);
438 uasm_i_yield(&p, zero, t1);
439 uasm_il_b(&p, &r, lbl_poll_cont);
443 * The core will lose power & this VPE will not continue
444 * so it can simply halt here.
446 uasm_i_addiu(&p, t0, zero, TCHALT_H);
447 uasm_i_mtc0(&p, t0, 2, 4);
448 uasm_build_label(&l, p, lbl_secondary_hang);
449 uasm_il_b(&p, &r, lbl_secondary_hang);
455 * This is the point of no return - this VPE will now proceed to
456 * disable coherence. At this point we *must* be sure that no other
457 * VPE within the core will interfere with the L1 dcache.
459 uasm_build_label(&l, p, lbl_disable_coherence);
461 /* Invalidate the L1 icache */
462 cps_gen_cache_routine(&p, &l, &r, &cpu_data[cpu].icache,
463 Index_Invalidate_I, lbl_invicache);
465 /* Writeback & invalidate the L1 dcache */
466 cps_gen_cache_routine(&p, &l, &r, &cpu_data[cpu].dcache,
467 Index_Writeback_Inv_D, lbl_flushdcache);
469 /* Barrier ensuring previous cache invalidates are complete */
470 uasm_i_sync(&p, STYPE_SYNC);
474 * Disable all but self interventions. The load from COHCTL is defined
475 * by the interAptiv & proAptiv SUMs as ensuring that the operation
476 * resulting from the preceding store is complete.
478 uasm_i_addiu(&p, t0, zero, 1 << cpu_data[cpu].core);
479 uasm_i_sw(&p, t0, 0, r_pcohctl);
480 uasm_i_lw(&p, t0, 0, r_pcohctl);
482 /* Barrier to ensure write to coherence control is complete */
483 uasm_i_sync(&p, STYPE_SYNC);
486 /* Disable coherence */
487 uasm_i_sw(&p, zero, 0, r_pcohctl);
488 uasm_i_lw(&p, t0, 0, r_pcohctl);
490 if (state >= CPS_PM_CLOCK_GATED) {
491 err = cps_gen_flush_fsb(&p, &l, &r, &cpu_data[cpu],
496 /* Determine the CPC command to issue */
498 case CPS_PM_CLOCK_GATED:
499 cpc_cmd = CPC_Cx_CMD_CLOCKOFF;
501 case CPS_PM_POWER_GATED:
502 cpc_cmd = CPC_Cx_CMD_PWRDOWN;
509 /* Issue the CPC command */
510 UASM_i_LA(&p, t0, (long)addr_cpc_cl_cmd());
511 uasm_i_addiu(&p, t1, zero, cpc_cmd);
512 uasm_i_sw(&p, t1, 0, t0);
514 if (state == CPS_PM_POWER_GATED) {
515 /* If anything goes wrong just hang */
516 uasm_build_label(&l, p, lbl_hang);
517 uasm_il_b(&p, &r, lbl_hang);
521 * There's no point generating more code, the core is
522 * powered down & if powered back up will run from the
523 * reset vector not from here.
528 /* Barrier to ensure write to CPC command is complete */
529 uasm_i_sync(&p, STYPE_SYNC);
533 if (state == CPS_PM_NC_WAIT) {
535 * At this point it is safe for all VPEs to proceed with
536 * execution. This VPE will set the top bit of ready_count
537 * to indicate to the other VPEs that they may continue.
539 if (coupled_coherence)
540 cps_gen_set_top_bit(&p, &l, &r, r_nc_count,
544 * VPEs which did not disable coherence will continue
545 * executing, after coherence has been disabled, from this
548 uasm_build_label(&l, p, lbl_secondary_cont);
550 /* Now perform our wait */
555 * Re-enable coherence. Note that for CPS_PM_NC_WAIT all coupled VPEs
556 * will run this. The first will actually re-enable coherence & the
557 * rest will just be performing a rather unusual nop.
559 uasm_i_addiu(&p, t0, zero, CM_GCR_Cx_COHERENCE_COHDOMAINEN_MSK);
560 uasm_i_sw(&p, t0, 0, r_pcohctl);
561 uasm_i_lw(&p, t0, 0, r_pcohctl);
563 /* Barrier to ensure write to coherence control is complete */
564 uasm_i_sync(&p, STYPE_SYNC);
567 if (coupled_coherence && (state == CPS_PM_NC_WAIT)) {
568 /* Decrement ready_count */
569 uasm_build_label(&l, p, lbl_decready);
570 uasm_i_sync(&p, STYPE_SYNC_MB);
571 uasm_i_ll(&p, t1, 0, r_nc_count);
572 uasm_i_addiu(&p, t2, t1, -1);
573 uasm_i_sc(&p, t2, 0, r_nc_count);
574 uasm_il_beqz(&p, &r, t2, lbl_decready);
575 uasm_i_andi(&p, v0, t1, (1 << fls(smp_num_siblings)) - 1);
577 /* Barrier ensuring all CPUs see the updated r_nc_count value */
578 uasm_i_sync(&p, STYPE_SYNC_MB);
581 if (coupled_coherence && (state == CPS_PM_CLOCK_GATED)) {
583 * At this point it is safe for all VPEs to proceed with
584 * execution. This VPE will set the top bit of ready_count
585 * to indicate to the other VPEs that they may continue.
587 cps_gen_set_top_bit(&p, &l, &r, r_nc_count, lbl_set_cont);
590 * This core will be reliant upon another core sending a
591 * power-up command to the CPC in order to resume operation.
592 * Thus an arbitrary VPE can't trigger the core leaving the
593 * idle state and the one that disables coherence might as well
594 * be the one to re-enable it. The rest will continue from here
595 * after that has been done.
597 uasm_build_label(&l, p, lbl_secondary_cont);
599 /* Barrier ensuring all CPUs see the updated r_nc_count value */
600 uasm_i_sync(&p, STYPE_SYNC_MB);
603 /* The core is coherent, time to return to C code */
608 /* Ensure the code didn't exceed the resources allocated for it */
609 BUG_ON((p - buf) > max_instrs);
610 BUG_ON((l - labels) > ARRAY_SIZE(labels));
611 BUG_ON((r - relocs) > ARRAY_SIZE(relocs));
613 /* Patch branch offsets */
614 uasm_resolve_relocs(relocs, labels);
616 /* Flush the icache */
617 local_flush_icache_range((unsigned long)buf, (unsigned long)p);
625 static int __init cps_gen_core_entries(unsigned cpu)
627 enum cps_pm_state state;
628 unsigned core = cpu_data[cpu].core;
629 unsigned dlinesz = cpu_data[cpu].dcache.linesz;
630 void *entry_fn, *core_rc;
632 for (state = CPS_PM_NC_WAIT; state < CPS_PM_STATE_COUNT; state++) {
633 if (per_cpu(nc_asm_enter, core)[state])
635 if (!test_bit(state, state_support))
638 entry_fn = cps_gen_entry_code(cpu, state);
640 pr_err("Failed to generate core %u state %u entry\n",
642 clear_bit(state, state_support);
645 per_cpu(nc_asm_enter, core)[state] = entry_fn;
648 if (!per_cpu(ready_count, core)) {
649 core_rc = kmalloc(dlinesz * 2, GFP_KERNEL);
651 pr_err("Failed allocate core %u ready_count\n", core);
654 per_cpu(ready_count_alloc, core) = core_rc;
656 /* Ensure ready_count is aligned to a cacheline boundary */
657 core_rc += dlinesz - 1;
658 core_rc = (void *)((unsigned long)core_rc & ~(dlinesz - 1));
659 per_cpu(ready_count, core) = core_rc;
665 static int __init cps_pm_init(void)
670 /* Detect appropriate sync types for the system */
671 switch (current_cpu_data.cputype) {
677 stype_intervention = 0x2;
682 pr_warn("Power management is using heavyweight sync 0\n");
685 /* A CM is required for all non-coherent states */
686 if (!mips_cm_present()) {
687 pr_warn("pm-cps: no CM, non-coherent states unavailable\n");
692 * If interrupts were enabled whilst running a wait instruction on a
693 * non-coherent core then the VPE may end up processing interrupts
694 * whilst non-coherent. That would be bad.
696 if (cpu_wait == r4k_wait_irqoff)
697 set_bit(CPS_PM_NC_WAIT, state_support);
699 pr_warn("pm-cps: non-coherent wait unavailable\n");
701 /* Detect whether a CPC is present */
702 if (mips_cpc_present()) {
703 /* Detect whether clock gating is implemented */
704 if (read_cpc_cl_stat_conf() & CPC_Cx_STAT_CONF_CLKGAT_IMPL_MSK)
705 set_bit(CPS_PM_CLOCK_GATED, state_support);
707 pr_warn("pm-cps: CPC does not support clock gating\n");
709 /* Power gating is available with CPS SMP & any CPC */
710 if (mips_cps_smp_in_use())
711 set_bit(CPS_PM_POWER_GATED, state_support);
713 pr_warn("pm-cps: CPS SMP not in use, power gating unavailable\n");
715 pr_warn("pm-cps: no CPC, clock & power gating unavailable\n");
718 for_each_present_cpu(cpu) {
719 err = cps_gen_core_entries(cpu);
726 arch_initcall(cps_pm_init);