]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/powerpc/platforms/wsp/scom_smp.c
268bc899c1f7168ca5827920010af0b53595c228
[karo-tx-linux.git] / arch / powerpc / platforms / wsp / scom_smp.c
1 /*
2  * SCOM support for A2 platforms
3  *
4  * Copyright 2007-2011 Benjamin Herrenschmidt, David Gibson,
5  *                     Michael Ellerman, IBM Corp.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version
10  * 2 of the License, or (at your option) any later version.
11  */
12
13 #include <linux/cpumask.h>
14 #include <linux/io.h>
15 #include <linux/of.h>
16 #include <linux/spinlock.h>
17 #include <linux/types.h>
18
19 #include <asm/cputhreads.h>
20 #include <asm/reg_a2.h>
21 #include <asm/scom.h>
22 #include <asm/udbg.h>
23
24 #include "wsp.h"
25
26 #define SCOM_RAMC               0x2a            /* Ram Command */
27 #define SCOM_RAMC_TGT1_EXT      0x80000000
28 #define SCOM_RAMC_SRC1_EXT      0x40000000
29 #define SCOM_RAMC_SRC2_EXT      0x20000000
30 #define SCOM_RAMC_SRC3_EXT      0x10000000
31 #define SCOM_RAMC_ENABLE        0x00080000
32 #define SCOM_RAMC_THREADSEL     0x00060000
33 #define SCOM_RAMC_EXECUTE       0x00010000
34 #define SCOM_RAMC_MSR_OVERRIDE  0x00008000
35 #define SCOM_RAMC_MSR_PR        0x00004000
36 #define SCOM_RAMC_MSR_GS        0x00002000
37 #define SCOM_RAMC_FORCE         0x00001000
38 #define SCOM_RAMC_FLUSH         0x00000800
39 #define SCOM_RAMC_INTERRUPT     0x00000004
40 #define SCOM_RAMC_ERROR         0x00000002
41 #define SCOM_RAMC_DONE          0x00000001
42 #define SCOM_RAMI               0x29            /* Ram Instruction */
43 #define SCOM_RAMIC              0x28            /* Ram Instruction and Command */
44 #define SCOM_RAMIC_INSN         0xffffffff00000000
45 #define SCOM_RAMD               0x2d            /* Ram Data */
46 #define SCOM_RAMDH              0x2e            /* Ram Data High */
47 #define SCOM_RAMDL              0x2f            /* Ram Data Low */
48 #define SCOM_PCCR0              0x33            /* PC Configuration Register 0 */
49 #define SCOM_PCCR0_ENABLE_DEBUG 0x80000000
50 #define SCOM_PCCR0_ENABLE_RAM   0x40000000
51 #define SCOM_THRCTL             0x30            /* Thread Control and Status */
52 #define SCOM_THRCTL_T0_STOP     0x80000000
53 #define SCOM_THRCTL_T1_STOP     0x40000000
54 #define SCOM_THRCTL_T2_STOP     0x20000000
55 #define SCOM_THRCTL_T3_STOP     0x10000000
56 #define SCOM_THRCTL_T0_STEP     0x08000000
57 #define SCOM_THRCTL_T1_STEP     0x04000000
58 #define SCOM_THRCTL_T2_STEP     0x02000000
59 #define SCOM_THRCTL_T3_STEP     0x01000000
60 #define SCOM_THRCTL_T0_RUN      0x00800000
61 #define SCOM_THRCTL_T1_RUN      0x00400000
62 #define SCOM_THRCTL_T2_RUN      0x00200000
63 #define SCOM_THRCTL_T3_RUN      0x00100000
64 #define SCOM_THRCTL_T0_PM       0x00080000
65 #define SCOM_THRCTL_T1_PM       0x00040000
66 #define SCOM_THRCTL_T2_PM       0x00020000
67 #define SCOM_THRCTL_T3_PM       0x00010000
68 #define SCOM_THRCTL_T0_UDE      0x00008000
69 #define SCOM_THRCTL_T1_UDE      0x00004000
70 #define SCOM_THRCTL_T2_UDE      0x00002000
71 #define SCOM_THRCTL_T3_UDE      0x00001000
72 #define SCOM_THRCTL_ASYNC_DIS   0x00000800
73 #define SCOM_THRCTL_TB_DIS      0x00000400
74 #define SCOM_THRCTL_DEC_DIS     0x00000200
75 #define SCOM_THRCTL_AND         0x31            /* Thread Control and Status */
76 #define SCOM_THRCTL_OR          0x32            /* Thread Control and Status */
77
78
79 static DEFINE_PER_CPU(scom_map_t, scom_ptrs);
80
81 static scom_map_t get_scom(int cpu, struct device_node *np, int *first_thread)
82 {
83         scom_map_t scom = per_cpu(scom_ptrs, cpu);
84         int tcpu;
85
86         if (scom_map_ok(scom)) {
87                 *first_thread = 0;
88                 return scom;
89         }
90
91         *first_thread = 1;
92
93         scom = scom_map_device(np, 0);
94
95         for (tcpu = cpu_first_thread_sibling(cpu);
96              tcpu <= cpu_last_thread_sibling(cpu); tcpu++)
97                 per_cpu(scom_ptrs, tcpu) = scom;
98
99         /* Hack: for the boot core, this will actually get called on
100          * the second thread up, not the first so our test above will
101          * set first_thread incorrectly. */
102         if (cpu_first_thread_sibling(cpu) == 0)
103                 *first_thread = 0;
104
105         return scom;
106 }
107
108 static int a2_scom_ram(scom_map_t scom, int thread, u32 insn, int extmask)
109 {
110         u64 cmd, mask, val;
111         int n = 0;
112
113         cmd = ((u64)insn << 32) | (((u64)extmask & 0xf) << 28)
114                 | ((u64)thread << 17) | SCOM_RAMC_ENABLE | SCOM_RAMC_EXECUTE;
115         mask = SCOM_RAMC_DONE | SCOM_RAMC_INTERRUPT | SCOM_RAMC_ERROR;
116
117         scom_write(scom, SCOM_RAMIC, cmd);
118
119         for (;;) {
120                 if (scom_read(scom, SCOM_RAMC, &val) != 0) {
121                         pr_err("SCOM error on instruction 0x%08x, thread %d\n",
122                                insn, thread);
123                         return -1;
124                 }
125                 if (val & mask)
126                         break;
127                 pr_devel("Waiting on RAMC = 0x%llx\n", val);
128                 if (++n == 3) {
129                         pr_err("RAMC timeout on instruction 0x%08x, thread %d\n",
130                                insn, thread);
131                         return -1;
132                 }
133         }
134
135         if (val & SCOM_RAMC_INTERRUPT) {
136                 pr_err("RAMC interrupt on instruction 0x%08x, thread %d\n",
137                        insn, thread);
138                 return -SCOM_RAMC_INTERRUPT;
139         }
140
141         if (val & SCOM_RAMC_ERROR) {
142                 pr_err("RAMC error on instruction 0x%08x, thread %d\n",
143                        insn, thread);
144                 return -SCOM_RAMC_ERROR;
145         }
146
147         return 0;
148 }
149
150 static int a2_scom_getgpr(scom_map_t scom, int thread, int gpr, int alt,
151                           u64 *out_gpr)
152 {
153         int rc;
154
155         /* or rN, rN, rN */
156         u32 insn = 0x7c000378 | (gpr << 21) | (gpr << 16) | (gpr << 11);
157         rc = a2_scom_ram(scom, thread, insn, alt ? 0xf : 0x0);
158         if (rc)
159                 return rc;
160
161         return scom_read(scom, SCOM_RAMD, out_gpr);
162 }
163
164 static int a2_scom_getspr(scom_map_t scom, int thread, int spr, u64 *out_spr)
165 {
166         int rc, sprhi, sprlo;
167         u32 insn;
168
169         sprhi = spr >> 5;
170         sprlo = spr & 0x1f;
171         insn = 0x7c2002a6 | (sprlo << 16) | (sprhi << 11); /* mfspr r1,spr */
172
173         if (spr == 0x0ff0)
174                 insn = 0x7c2000a6; /* mfmsr r1 */
175
176         rc = a2_scom_ram(scom, thread, insn, 0xf);
177         if (rc)
178                 return rc;
179         return a2_scom_getgpr(scom, thread, 1, 1, out_spr);
180 }
181
182 static int a2_scom_setgpr(scom_map_t scom, int thread, int gpr,
183                           int alt, u64 val)
184 {
185         u32 lis = 0x3c000000 | (gpr << 21);
186         u32 li = 0x38000000 | (gpr << 21);
187         u32 oris = 0x64000000 | (gpr << 21) | (gpr << 16);
188         u32 ori = 0x60000000 | (gpr << 21) | (gpr << 16);
189         u32 rldicr32 = 0x780007c6 | (gpr << 21) | (gpr << 16);
190         u32 highest = val >> 48;
191         u32 higher = (val >> 32) & 0xffff;
192         u32 high = (val >> 16) & 0xffff;
193         u32 low = val & 0xffff;
194         int lext = alt ? 0x8 : 0x0;
195         int oext = alt ? 0xf : 0x0;
196         int rc = 0;
197
198         if (highest)
199                 rc |= a2_scom_ram(scom, thread, lis | highest, lext);
200
201         if (higher) {
202                 if (highest)
203                         rc |= a2_scom_ram(scom, thread, oris | higher, oext);
204                 else
205                         rc |= a2_scom_ram(scom, thread, li | higher, lext);
206         }
207
208         if (highest || higher)
209                 rc |= a2_scom_ram(scom, thread, rldicr32, oext);
210
211         if (high) {
212                 if (highest || higher)
213                         rc |= a2_scom_ram(scom, thread, oris | high, oext);
214                 else
215                         rc |= a2_scom_ram(scom, thread, lis | high, lext);
216         }
217
218         if (highest || higher || high)
219                 rc |= a2_scom_ram(scom, thread, ori | low, oext);
220         else
221                 rc |= a2_scom_ram(scom, thread, li | low, lext);
222
223         return rc;
224 }
225
226 static int a2_scom_setspr(scom_map_t scom, int thread, int spr, u64 val)
227 {
228         int sprhi = spr >> 5;
229         int sprlo = spr & 0x1f;
230         /* mtspr spr, r1 */
231         u32 insn = 0x7c2003a6 | (sprlo << 16) | (sprhi << 11);
232
233         if (spr == 0x0ff0)
234                 insn = 0x7c200124; /* mtmsr r1 */
235
236         if (a2_scom_setgpr(scom, thread, 1, 1, val))
237                 return -1;
238
239         return a2_scom_ram(scom, thread, insn, 0xf);
240 }
241
242 static int a2_scom_initial_tlb(scom_map_t scom, int thread)
243 {
244         extern u32 a2_tlbinit_code_start[], a2_tlbinit_code_end[];
245         extern u32 a2_tlbinit_after_iprot_flush[];
246         extern u32 a2_tlbinit_after_linear_map[];
247         u32 assoc, entries, i;
248         u64 epn, tlbcfg;
249         u32 *p;
250         int rc;
251
252         /* Invalidate all entries (including iprot) */
253
254         rc = a2_scom_getspr(scom, thread, SPRN_TLB0CFG, &tlbcfg);
255         if (rc)
256                 goto scom_fail;
257         entries = tlbcfg & TLBnCFG_N_ENTRY;
258         assoc = (tlbcfg & TLBnCFG_ASSOC) >> 24;
259         epn = 0;
260
261         /* Set MMUCR2 to enable 4K, 64K, 1M, 16M and 1G pages */
262         a2_scom_setspr(scom, thread, SPRN_MMUCR2, 0x000a7531);
263         /* Set MMUCR3 to write all thids bit to the TLB */
264         a2_scom_setspr(scom, thread, SPRN_MMUCR3, 0x0000000f);
265
266         /* Set MAS1 for 1G page size, and MAS2 to our initial EPN */
267         a2_scom_setspr(scom, thread, SPRN_MAS1, MAS1_TSIZE(BOOK3E_PAGESZ_1GB));
268         a2_scom_setspr(scom, thread, SPRN_MAS2, epn);
269         for (i = 0; i < entries; i++) {
270
271                 a2_scom_setspr(scom, thread, SPRN_MAS0, MAS0_ESEL(i % assoc));
272
273                 /* tlbwe */
274                 rc = a2_scom_ram(scom, thread, 0x7c0007a4, 0);
275                 if (rc)
276                         goto scom_fail;
277
278                 /* Next entry is new address? */
279                 if((i + 1) % assoc == 0) {
280                         epn += (1 << 30);
281                         a2_scom_setspr(scom, thread, SPRN_MAS2, epn);
282                 }
283         }
284
285         /* Setup args for linear mapping */
286         rc = a2_scom_setgpr(scom, thread, 3, 0, MAS0_TLBSEL(0));
287         if (rc)
288                 goto scom_fail;
289
290         /* Linear mapping */
291         for (p = a2_tlbinit_code_start; p < a2_tlbinit_after_linear_map; p++) {
292                 rc = a2_scom_ram(scom, thread, *p, 0);
293                 if (rc)
294                         goto scom_fail;
295         }
296
297         /*
298          * For the boot thread, between the linear mapping and the debug
299          * mappings there is a loop to flush iprot mappings. Ramming doesn't do
300          * branches, but the secondary threads don't need to be nearly as smart
301          * (i.e. we don't need to worry about invalidating the mapping we're
302          * standing on).
303          */
304
305         /* Debug mappings. Expects r11 = MAS0 from linear map (set above) */
306         for (p = a2_tlbinit_after_iprot_flush; p < a2_tlbinit_code_end; p++) {
307                 rc = a2_scom_ram(scom, thread, *p, 0);
308                 if (rc)
309                         goto scom_fail;
310         }
311
312 scom_fail:
313         if (rc)
314                 pr_err("Setting up initial TLB failed, err %d\n", rc);
315
316         if (rc == -SCOM_RAMC_INTERRUPT) {
317                 /* Interrupt, dump some status */
318                 int rc[10];
319                 u64 iar, srr0, srr1, esr, mas0, mas1, mas2, mas7_3, mas8, ccr2;
320                 rc[0] = a2_scom_getspr(scom, thread, SPRN_IAR, &iar);
321                 rc[1] = a2_scom_getspr(scom, thread, SPRN_SRR0, &srr0);
322                 rc[2] = a2_scom_getspr(scom, thread, SPRN_SRR1, &srr1);
323                 rc[3] = a2_scom_getspr(scom, thread, SPRN_ESR, &esr);
324                 rc[4] = a2_scom_getspr(scom, thread, SPRN_MAS0, &mas0);
325                 rc[5] = a2_scom_getspr(scom, thread, SPRN_MAS1, &mas1);
326                 rc[6] = a2_scom_getspr(scom, thread, SPRN_MAS2, &mas2);
327                 rc[7] = a2_scom_getspr(scom, thread, SPRN_MAS7_MAS3, &mas7_3);
328                 rc[8] = a2_scom_getspr(scom, thread, SPRN_MAS8, &mas8);
329                 rc[9] = a2_scom_getspr(scom, thread, SPRN_A2_CCR2, &ccr2);
330                 pr_err(" -> retreived IAR =0x%llx (err %d)\n", iar, rc[0]);
331                 pr_err("    retreived SRR0=0x%llx (err %d)\n", srr0, rc[1]);
332                 pr_err("    retreived SRR1=0x%llx (err %d)\n", srr1, rc[2]);
333                 pr_err("    retreived ESR =0x%llx (err %d)\n", esr, rc[3]);
334                 pr_err("    retreived MAS0=0x%llx (err %d)\n", mas0, rc[4]);
335                 pr_err("    retreived MAS1=0x%llx (err %d)\n", mas1, rc[5]);
336                 pr_err("    retreived MAS2=0x%llx (err %d)\n", mas2, rc[6]);
337                 pr_err("    retreived MS73=0x%llx (err %d)\n", mas7_3, rc[7]);
338                 pr_err("    retreived MAS8=0x%llx (err %d)\n", mas8, rc[8]);
339                 pr_err("    retreived CCR2=0x%llx (err %d)\n", ccr2, rc[9]);
340         }
341
342         return rc;
343 }
344
345 int a2_scom_startup_cpu(unsigned int lcpu, int thr_idx, struct device_node *np)
346 {
347         u64 init_iar, init_msr, init_ccr2;
348         unsigned long start_here;
349         int rc, core_setup;
350         scom_map_t scom;
351         u64 pccr0;
352
353         scom = get_scom(lcpu, np, &core_setup);
354         if (!scom) {
355                 printk(KERN_ERR "Couldn't map SCOM for CPU%d\n", lcpu);
356                 return -1;
357         }
358
359         pr_devel("Bringing up CPU%d using SCOM...\n", lcpu);
360
361         if (scom_read(scom, SCOM_PCCR0, &pccr0) != 0) {
362                 printk(KERN_ERR "XSCOM failure readng PCCR0 on CPU%d\n", lcpu);
363                 return -1;
364         }
365         scom_write(scom, SCOM_PCCR0, pccr0 | SCOM_PCCR0_ENABLE_DEBUG |
366                                      SCOM_PCCR0_ENABLE_RAM);
367
368         /* Stop the thead with THRCTL. If we are setting up the TLB we stop all
369          * threads. We also disable asynchronous interrupts while RAMing.
370          */
371         if (core_setup)
372                 scom_write(scom, SCOM_THRCTL_OR,
373                               SCOM_THRCTL_T0_STOP |
374                               SCOM_THRCTL_T1_STOP |
375                               SCOM_THRCTL_T2_STOP |
376                               SCOM_THRCTL_T3_STOP |
377                               SCOM_THRCTL_ASYNC_DIS);
378         else
379                 scom_write(scom, SCOM_THRCTL_OR, SCOM_THRCTL_T0_STOP >> thr_idx);
380
381         /* Flush its pipeline just in case */
382         scom_write(scom, SCOM_RAMC, ((u64)thr_idx << 17) |
383                       SCOM_RAMC_FLUSH | SCOM_RAMC_ENABLE);
384
385         a2_scom_getspr(scom, thr_idx, SPRN_IAR, &init_iar);
386         a2_scom_getspr(scom, thr_idx, 0x0ff0, &init_msr);
387         a2_scom_getspr(scom, thr_idx, SPRN_A2_CCR2, &init_ccr2);
388
389         /* Set MSR to MSR_CM (0x0ff0 is magic value for MSR_CM) */
390         rc = a2_scom_setspr(scom, thr_idx, 0x0ff0, MSR_CM);
391         if (rc) {
392                 pr_err("Failed to set MSR ! err %d\n", rc);
393                 return rc;
394         }
395
396         /* RAM in an sync/isync for the sake of it */
397         a2_scom_ram(scom, thr_idx, 0x7c0004ac, 0);
398         a2_scom_ram(scom, thr_idx, 0x4c00012c, 0);
399
400         if (core_setup) {
401                 pr_devel("CPU%d is first thread in core, initializing TLB...\n",
402                          lcpu);
403                 rc = a2_scom_initial_tlb(scom, thr_idx);
404                 if (rc)
405                         goto fail;
406         }
407
408         start_here = *(unsigned long *)(core_setup ? generic_secondary_smp_init
409                                         : generic_secondary_thread_init);
410         pr_devel("CPU%d entry point at 0x%lx...\n", lcpu, start_here);
411
412         rc |= a2_scom_setspr(scom, thr_idx, SPRN_IAR, start_here);
413         rc |= a2_scom_setgpr(scom, thr_idx, 3, 0,
414                              get_hard_smp_processor_id(lcpu));
415         /*
416          * Tell book3e_secondary_core_init not to set up the TLB, we've
417          * already done that.
418          */
419         rc |= a2_scom_setgpr(scom, thr_idx, 4, 0, 1);
420
421         rc |= a2_scom_setspr(scom, thr_idx, SPRN_TENS, 0x1 << thr_idx);
422
423         scom_write(scom, SCOM_RAMC, 0);
424         scom_write(scom, SCOM_THRCTL_AND, ~(SCOM_THRCTL_T0_STOP >> thr_idx));
425         scom_write(scom, SCOM_PCCR0, pccr0);
426 fail:
427         pr_devel("  SCOM initialization %s\n", rc ? "failed" : "succeeded");
428         if (rc) {
429                 pr_err("Old IAR=0x%08llx MSR=0x%08llx CCR2=0x%08llx\n",
430                        init_iar, init_msr, init_ccr2);
431         }
432
433         return rc;
434 }