scom_smp.c 12 KB

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