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