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