scom_smp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. for (;;) {
  103. if (scom_read(scom, SCOM_RAMC, &val) != 0) {
  104. pr_err("SCOM error on instruction 0x%08x, thread %d\n",
  105. insn, thread);
  106. return -1;
  107. }
  108. if (val & mask)
  109. break;
  110. pr_devel("Waiting on RAMC = 0x%llx\n", val);
  111. if (++n == 3) {
  112. pr_err("RAMC timeout on instruction 0x%08x, thread %d\n",
  113. insn, thread);
  114. return -1;
  115. }
  116. }
  117. if (val & SCOM_RAMC_INTERRUPT) {
  118. pr_err("RAMC interrupt on instruction 0x%08x, thread %d\n",
  119. insn, thread);
  120. return -SCOM_RAMC_INTERRUPT;
  121. }
  122. if (val & SCOM_RAMC_ERROR) {
  123. pr_err("RAMC error on instruction 0x%08x, thread %d\n",
  124. insn, thread);
  125. return -SCOM_RAMC_ERROR;
  126. }
  127. return 0;
  128. }
  129. static int a2_scom_getgpr(scom_map_t scom, int thread, int gpr, int alt,
  130. u64 *out_gpr)
  131. {
  132. int rc;
  133. /* or rN, rN, rN */
  134. u32 insn = 0x7c000378 | (gpr << 21) | (gpr << 16) | (gpr << 11);
  135. rc = a2_scom_ram(scom, thread, insn, alt ? 0xf : 0x0);
  136. if (rc)
  137. return rc;
  138. return scom_read(scom, SCOM_RAMD, out_gpr);
  139. }
  140. static int a2_scom_getspr(scom_map_t scom, int thread, int spr, u64 *out_spr)
  141. {
  142. int rc, sprhi, sprlo;
  143. u32 insn;
  144. sprhi = spr >> 5;
  145. sprlo = spr & 0x1f;
  146. insn = 0x7c2002a6 | (sprlo << 16) | (sprhi << 11); /* mfspr r1,spr */
  147. if (spr == 0x0ff0)
  148. insn = 0x7c2000a6; /* mfmsr r1 */
  149. rc = a2_scom_ram(scom, thread, insn, 0xf);
  150. if (rc)
  151. return rc;
  152. return a2_scom_getgpr(scom, thread, 1, 1, out_spr);
  153. }
  154. static int a2_scom_setgpr(scom_map_t scom, int thread, int gpr,
  155. int alt, u64 val)
  156. {
  157. u32 lis = 0x3c000000 | (gpr << 21);
  158. u32 li = 0x38000000 | (gpr << 21);
  159. u32 oris = 0x64000000 | (gpr << 21) | (gpr << 16);
  160. u32 ori = 0x60000000 | (gpr << 21) | (gpr << 16);
  161. u32 rldicr32 = 0x780007c6 | (gpr << 21) | (gpr << 16);
  162. u32 highest = val >> 48;
  163. u32 higher = (val >> 32) & 0xffff;
  164. u32 high = (val >> 16) & 0xffff;
  165. u32 low = val & 0xffff;
  166. int lext = alt ? 0x8 : 0x0;
  167. int oext = alt ? 0xf : 0x0;
  168. int rc = 0;
  169. if (highest)
  170. rc |= a2_scom_ram(scom, thread, lis | highest, lext);
  171. if (higher) {
  172. if (highest)
  173. rc |= a2_scom_ram(scom, thread, oris | higher, oext);
  174. else
  175. rc |= a2_scom_ram(scom, thread, li | higher, lext);
  176. }
  177. if (highest || higher)
  178. rc |= a2_scom_ram(scom, thread, rldicr32, oext);
  179. if (high) {
  180. if (highest || higher)
  181. rc |= a2_scom_ram(scom, thread, oris | high, oext);
  182. else
  183. rc |= a2_scom_ram(scom, thread, lis | high, lext);
  184. }
  185. if (highest || higher || high)
  186. rc |= a2_scom_ram(scom, thread, ori | low, oext);
  187. else
  188. rc |= a2_scom_ram(scom, thread, li | low, lext);
  189. return rc;
  190. }
  191. static int a2_scom_setspr(scom_map_t scom, int thread, int spr, u64 val)
  192. {
  193. int sprhi = spr >> 5;
  194. int sprlo = spr & 0x1f;
  195. /* mtspr spr, r1 */
  196. u32 insn = 0x7c2003a6 | (sprlo << 16) | (sprhi << 11);
  197. if (spr == 0x0ff0)
  198. insn = 0x7c200124; /* mtmsr r1 */
  199. if (a2_scom_setgpr(scom, thread, 1, 1, val))
  200. return -1;
  201. return a2_scom_ram(scom, thread, insn, 0xf);
  202. }
  203. static int a2_scom_initial_tlb(scom_map_t scom, int thread)
  204. {
  205. extern u32 a2_tlbinit_code_start[], a2_tlbinit_code_end[];
  206. extern u32 a2_tlbinit_after_iprot_flush[];
  207. extern u32 a2_tlbinit_after_linear_map[];
  208. u32 assoc, entries, i;
  209. u64 epn, tlbcfg;
  210. u32 *p;
  211. int rc;
  212. /* Invalidate all entries (including iprot) */
  213. rc = a2_scom_getspr(scom, thread, SPRN_TLB0CFG, &tlbcfg);
  214. if (rc)
  215. goto scom_fail;
  216. entries = tlbcfg & TLBnCFG_N_ENTRY;
  217. assoc = (tlbcfg & TLBnCFG_ASSOC) >> 24;
  218. epn = 0;
  219. /* Set MMUCR2 to enable 4K, 64K, 1M, 16M and 1G pages */
  220. a2_scom_setspr(scom, thread, SPRN_MMUCR2, 0x000a7531);
  221. /* Set MMUCR3 to write all thids bit to the TLB */
  222. a2_scom_setspr(scom, thread, SPRN_MMUCR3, 0x0000000f);
  223. /* Set MAS1 for 1G page size, and MAS2 to our initial EPN */
  224. a2_scom_setspr(scom, thread, SPRN_MAS1, MAS1_TSIZE(BOOK3E_PAGESZ_1GB));
  225. a2_scom_setspr(scom, thread, SPRN_MAS2, epn);
  226. for (i = 0; i < entries; i++) {
  227. a2_scom_setspr(scom, thread, SPRN_MAS0, MAS0_ESEL(i % assoc));
  228. /* tlbwe */
  229. rc = a2_scom_ram(scom, thread, 0x7c0007a4, 0);
  230. if (rc)
  231. goto scom_fail;
  232. /* Next entry is new address? */
  233. if((i + 1) % assoc == 0) {
  234. epn += (1 << 30);
  235. a2_scom_setspr(scom, thread, SPRN_MAS2, epn);
  236. }
  237. }
  238. /* Setup args for linear mapping */
  239. rc = a2_scom_setgpr(scom, thread, 3, 0, MAS0_TLBSEL(0));
  240. if (rc)
  241. goto scom_fail;
  242. /* Linear mapping */
  243. for (p = a2_tlbinit_code_start; p < a2_tlbinit_after_linear_map; p++) {
  244. rc = a2_scom_ram(scom, thread, *p, 0);
  245. if (rc)
  246. goto scom_fail;
  247. }
  248. /*
  249. * For the boot thread, between the linear mapping and the debug
  250. * mappings there is a loop to flush iprot mappings. Ramming doesn't do
  251. * branches, but the secondary threads don't need to be nearly as smart
  252. * (i.e. we don't need to worry about invalidating the mapping we're
  253. * standing on).
  254. */
  255. /* Debug mappings. Expects r11 = MAS0 from linear map (set above) */
  256. for (p = a2_tlbinit_after_iprot_flush; p < a2_tlbinit_code_end; p++) {
  257. rc = a2_scom_ram(scom, thread, *p, 0);
  258. if (rc)
  259. goto scom_fail;
  260. }
  261. scom_fail:
  262. if (rc)
  263. pr_err("Setting up initial TLB failed, err %d\n", rc);
  264. if (rc == -SCOM_RAMC_INTERRUPT) {
  265. /* Interrupt, dump some status */
  266. int rc[10];
  267. u64 iar, srr0, srr1, esr, mas0, mas1, mas2, mas7_3, mas8, ccr2;
  268. rc[0] = a2_scom_getspr(scom, thread, SPRN_IAR, &iar);
  269. rc[1] = a2_scom_getspr(scom, thread, SPRN_SRR0, &srr0);
  270. rc[2] = a2_scom_getspr(scom, thread, SPRN_SRR1, &srr1);
  271. rc[3] = a2_scom_getspr(scom, thread, SPRN_ESR, &esr);
  272. rc[4] = a2_scom_getspr(scom, thread, SPRN_MAS0, &mas0);
  273. rc[5] = a2_scom_getspr(scom, thread, SPRN_MAS1, &mas1);
  274. rc[6] = a2_scom_getspr(scom, thread, SPRN_MAS2, &mas2);
  275. rc[7] = a2_scom_getspr(scom, thread, SPRN_MAS7_MAS3, &mas7_3);
  276. rc[8] = a2_scom_getspr(scom, thread, SPRN_MAS8, &mas8);
  277. rc[9] = a2_scom_getspr(scom, thread, SPRN_A2_CCR2, &ccr2);
  278. pr_err(" -> retreived IAR =0x%llx (err %d)\n", iar, rc[0]);
  279. pr_err(" retreived SRR0=0x%llx (err %d)\n", srr0, rc[1]);
  280. pr_err(" retreived SRR1=0x%llx (err %d)\n", srr1, rc[2]);
  281. pr_err(" retreived ESR =0x%llx (err %d)\n", esr, rc[3]);
  282. pr_err(" retreived MAS0=0x%llx (err %d)\n", mas0, rc[4]);
  283. pr_err(" retreived MAS1=0x%llx (err %d)\n", mas1, rc[5]);
  284. pr_err(" retreived MAS2=0x%llx (err %d)\n", mas2, rc[6]);
  285. pr_err(" retreived MS73=0x%llx (err %d)\n", mas7_3, rc[7]);
  286. pr_err(" retreived MAS8=0x%llx (err %d)\n", mas8, rc[8]);
  287. pr_err(" retreived CCR2=0x%llx (err %d)\n", ccr2, rc[9]);
  288. }
  289. return rc;
  290. }
  291. int a2_scom_startup_cpu(unsigned int lcpu, int thr_idx, struct device_node *np)
  292. {
  293. u64 init_iar, init_msr, init_ccr2;
  294. unsigned long start_here;
  295. int rc, core_setup;
  296. scom_map_t scom;
  297. u64 pccr0;
  298. scom = get_scom(lcpu, np, &core_setup);
  299. if (!scom) {
  300. printk(KERN_ERR "Couldn't map SCOM for CPU%d\n", lcpu);
  301. return -1;
  302. }
  303. pr_devel("Bringing up CPU%d using SCOM...\n", lcpu);
  304. if (scom_read(scom, SCOM_PCCR0, &pccr0) != 0) {
  305. printk(KERN_ERR "XSCOM failure readng PCCR0 on CPU%d\n", lcpu);
  306. return -1;
  307. }
  308. scom_write(scom, SCOM_PCCR0, pccr0 | SCOM_PCCR0_ENABLE_DEBUG |
  309. SCOM_PCCR0_ENABLE_RAM);
  310. /* Stop the thead with THRCTL. If we are setting up the TLB we stop all
  311. * threads. We also disable asynchronous interrupts while RAMing.
  312. */
  313. if (core_setup)
  314. scom_write(scom, SCOM_THRCTL_OR,
  315. SCOM_THRCTL_T0_STOP |
  316. SCOM_THRCTL_T1_STOP |
  317. SCOM_THRCTL_T2_STOP |
  318. SCOM_THRCTL_T3_STOP |
  319. SCOM_THRCTL_ASYNC_DIS);
  320. else
  321. scom_write(scom, SCOM_THRCTL_OR, SCOM_THRCTL_T0_STOP >> thr_idx);
  322. /* Flush its pipeline just in case */
  323. scom_write(scom, SCOM_RAMC, ((u64)thr_idx << 17) |
  324. SCOM_RAMC_FLUSH | SCOM_RAMC_ENABLE);
  325. a2_scom_getspr(scom, thr_idx, SPRN_IAR, &init_iar);
  326. a2_scom_getspr(scom, thr_idx, 0x0ff0, &init_msr);
  327. a2_scom_getspr(scom, thr_idx, SPRN_A2_CCR2, &init_ccr2);
  328. /* Set MSR to MSR_CM (0x0ff0 is magic value for MSR_CM) */
  329. rc = a2_scom_setspr(scom, thr_idx, 0x0ff0, MSR_CM);
  330. if (rc) {
  331. pr_err("Failed to set MSR ! err %d\n", rc);
  332. return rc;
  333. }
  334. /* RAM in an sync/isync for the sake of it */
  335. a2_scom_ram(scom, thr_idx, 0x7c0004ac, 0);
  336. a2_scom_ram(scom, thr_idx, 0x4c00012c, 0);
  337. if (core_setup) {
  338. pr_devel("CPU%d is first thread in core, initializing TLB...\n",
  339. lcpu);
  340. rc = a2_scom_initial_tlb(scom, thr_idx);
  341. if (rc)
  342. goto fail;
  343. }
  344. start_here = *(unsigned long *)(core_setup ? generic_secondary_smp_init
  345. : generic_secondary_thread_init);
  346. pr_devel("CPU%d entry point at 0x%lx...\n", lcpu, start_here);
  347. rc |= a2_scom_setspr(scom, thr_idx, SPRN_IAR, start_here);
  348. rc |= a2_scom_setgpr(scom, thr_idx, 3, 0,
  349. get_hard_smp_processor_id(lcpu));
  350. /*
  351. * Tell book3e_secondary_core_init not to set up the TLB, we've
  352. * already done that.
  353. */
  354. rc |= a2_scom_setgpr(scom, thr_idx, 4, 0, 1);
  355. rc |= a2_scom_setspr(scom, thr_idx, SPRN_TENS, 0x1 << thr_idx);
  356. scom_write(scom, SCOM_RAMC, 0);
  357. scom_write(scom, SCOM_THRCTL_AND, ~(SCOM_THRCTL_T0_STOP >> thr_idx));
  358. scom_write(scom, SCOM_PCCR0, pccr0);
  359. fail:
  360. pr_devel(" SCOM initialization %s\n", rc ? "failed" : "succeeded");
  361. if (rc) {
  362. pr_err("Old IAR=0x%08llx MSR=0x%08llx CCR2=0x%08llx\n",
  363. init_iar, init_msr, init_ccr2);
  364. }
  365. return rc;
  366. }