mips-cm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * Copyright (C) 2013 Imagination Technologies
  3. * Author: Paul Burton <paul.burton@mips.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. #include <linux/errno.h>
  11. #include <linux/percpu.h>
  12. #include <linux/spinlock.h>
  13. #include <asm/mips-cps.h>
  14. #include <asm/mipsregs.h>
  15. void __iomem *mips_gcr_base;
  16. void __iomem *mips_cm_l2sync_base;
  17. int mips_cm_is64;
  18. static char *cm2_tr[8] = {
  19. "mem", "gcr", "gic", "mmio",
  20. "0x04", "cpc", "0x06", "0x07"
  21. };
  22. /* CM3 Tag ECC transaction type */
  23. static char *cm3_tr[16] = {
  24. [0x0] = "ReqNoData",
  25. [0x1] = "0x1",
  26. [0x2] = "ReqWData",
  27. [0x3] = "0x3",
  28. [0x4] = "IReqNoResp",
  29. [0x5] = "IReqWResp",
  30. [0x6] = "IReqNoRespDat",
  31. [0x7] = "IReqWRespDat",
  32. [0x8] = "RespNoData",
  33. [0x9] = "RespDataFol",
  34. [0xa] = "RespWData",
  35. [0xb] = "RespDataOnly",
  36. [0xc] = "IRespNoData",
  37. [0xd] = "IRespDataFol",
  38. [0xe] = "IRespWData",
  39. [0xf] = "IRespDataOnly"
  40. };
  41. static char *cm2_cmd[32] = {
  42. [0x00] = "0x00",
  43. [0x01] = "Legacy Write",
  44. [0x02] = "Legacy Read",
  45. [0x03] = "0x03",
  46. [0x04] = "0x04",
  47. [0x05] = "0x05",
  48. [0x06] = "0x06",
  49. [0x07] = "0x07",
  50. [0x08] = "Coherent Read Own",
  51. [0x09] = "Coherent Read Share",
  52. [0x0a] = "Coherent Read Discard",
  53. [0x0b] = "Coherent Ready Share Always",
  54. [0x0c] = "Coherent Upgrade",
  55. [0x0d] = "Coherent Writeback",
  56. [0x0e] = "0x0e",
  57. [0x0f] = "0x0f",
  58. [0x10] = "Coherent Copyback",
  59. [0x11] = "Coherent Copyback Invalidate",
  60. [0x12] = "Coherent Invalidate",
  61. [0x13] = "Coherent Write Invalidate",
  62. [0x14] = "Coherent Completion Sync",
  63. [0x15] = "0x15",
  64. [0x16] = "0x16",
  65. [0x17] = "0x17",
  66. [0x18] = "0x18",
  67. [0x19] = "0x19",
  68. [0x1a] = "0x1a",
  69. [0x1b] = "0x1b",
  70. [0x1c] = "0x1c",
  71. [0x1d] = "0x1d",
  72. [0x1e] = "0x1e",
  73. [0x1f] = "0x1f"
  74. };
  75. /* CM3 Tag ECC command type */
  76. static char *cm3_cmd[16] = {
  77. [0x0] = "Legacy Read",
  78. [0x1] = "Legacy Write",
  79. [0x2] = "Coherent Read Own",
  80. [0x3] = "Coherent Read Share",
  81. [0x4] = "Coherent Read Discard",
  82. [0x5] = "Coherent Evicted",
  83. [0x6] = "Coherent Upgrade",
  84. [0x7] = "Coherent Upgrade for Store Conditional",
  85. [0x8] = "Coherent Writeback",
  86. [0x9] = "Coherent Write Invalidate",
  87. [0xa] = "0xa",
  88. [0xb] = "0xb",
  89. [0xc] = "0xc",
  90. [0xd] = "0xd",
  91. [0xe] = "0xe",
  92. [0xf] = "0xf"
  93. };
  94. /* CM3 Tag ECC command group */
  95. static char *cm3_cmd_group[8] = {
  96. [0x0] = "Normal",
  97. [0x1] = "Registers",
  98. [0x2] = "TLB",
  99. [0x3] = "0x3",
  100. [0x4] = "L1I",
  101. [0x5] = "L1D",
  102. [0x6] = "L3",
  103. [0x7] = "L2"
  104. };
  105. static char *cm2_core[8] = {
  106. "Invalid/OK", "Invalid/Data",
  107. "Shared/OK", "Shared/Data",
  108. "Modified/OK", "Modified/Data",
  109. "Exclusive/OK", "Exclusive/Data"
  110. };
  111. static char *cm2_causes[32] = {
  112. "None", "GC_WR_ERR", "GC_RD_ERR", "COH_WR_ERR",
  113. "COH_RD_ERR", "MMIO_WR_ERR", "MMIO_RD_ERR", "0x07",
  114. "0x08", "0x09", "0x0a", "0x0b",
  115. "0x0c", "0x0d", "0x0e", "0x0f",
  116. "0x10", "INTVN_WR_ERR", "INTVN_RD_ERR", "0x13",
  117. "0x14", "0x15", "0x16", "0x17",
  118. "0x18", "0x19", "0x1a", "0x1b",
  119. "0x1c", "0x1d", "0x1e", "0x1f"
  120. };
  121. static char *cm3_causes[32] = {
  122. "0x0", "MP_CORRECTABLE_ECC_ERR", "MP_REQUEST_DECODE_ERR",
  123. "MP_UNCORRECTABLE_ECC_ERR", "MP_PARITY_ERR", "MP_COHERENCE_ERR",
  124. "CMBIU_REQUEST_DECODE_ERR", "CMBIU_PARITY_ERR", "CMBIU_AXI_RESP_ERR",
  125. "0x9", "RBI_BUS_ERR", "0xb", "0xc", "0xd", "0xe", "0xf", "0x10",
  126. "0x11", "0x12", "0x13", "0x14", "0x15", "0x16", "0x17", "0x18",
  127. "0x19", "0x1a", "0x1b", "0x1c", "0x1d", "0x1e", "0x1f"
  128. };
  129. static DEFINE_PER_CPU_ALIGNED(spinlock_t, cm_core_lock);
  130. static DEFINE_PER_CPU_ALIGNED(unsigned long, cm_core_lock_flags);
  131. phys_addr_t __mips_cm_phys_base(void)
  132. {
  133. u32 config3 = read_c0_config3();
  134. unsigned long cmgcr;
  135. /* Check the CMGCRBase register is implemented */
  136. if (!(config3 & MIPS_CONF3_CMGCR))
  137. return 0;
  138. /* Read the address from CMGCRBase */
  139. cmgcr = read_c0_cmgcrbase();
  140. return (cmgcr & MIPS_CMGCRF_BASE) << (36 - 32);
  141. }
  142. phys_addr_t mips_cm_phys_base(void)
  143. __attribute__((weak, alias("__mips_cm_phys_base")));
  144. phys_addr_t __mips_cm_l2sync_phys_base(void)
  145. {
  146. u32 base_reg;
  147. /*
  148. * If the L2-only sync region is already enabled then leave it at it's
  149. * current location.
  150. */
  151. base_reg = read_gcr_l2_only_sync_base();
  152. if (base_reg & CM_GCR_L2_ONLY_SYNC_BASE_SYNCEN)
  153. return base_reg & CM_GCR_L2_ONLY_SYNC_BASE_SYNCBASE;
  154. /* Default to following the CM */
  155. return mips_cm_phys_base() + MIPS_CM_GCR_SIZE;
  156. }
  157. phys_addr_t mips_cm_l2sync_phys_base(void)
  158. __attribute__((weak, alias("__mips_cm_l2sync_phys_base")));
  159. static void mips_cm_probe_l2sync(void)
  160. {
  161. unsigned major_rev;
  162. phys_addr_t addr;
  163. /* L2-only sync was introduced with CM major revision 6 */
  164. major_rev = (read_gcr_rev() & CM_GCR_REV_MAJOR) >>
  165. __ffs(CM_GCR_REV_MAJOR);
  166. if (major_rev < 6)
  167. return;
  168. /* Find a location for the L2 sync region */
  169. addr = mips_cm_l2sync_phys_base();
  170. BUG_ON((addr & CM_GCR_L2_ONLY_SYNC_BASE_SYNCBASE) != addr);
  171. if (!addr)
  172. return;
  173. /* Set the region base address & enable it */
  174. write_gcr_l2_only_sync_base(addr | CM_GCR_L2_ONLY_SYNC_BASE_SYNCEN);
  175. /* Map the region */
  176. mips_cm_l2sync_base = ioremap_nocache(addr, MIPS_CM_L2SYNC_SIZE);
  177. }
  178. int mips_cm_probe(void)
  179. {
  180. phys_addr_t addr;
  181. u32 base_reg;
  182. unsigned cpu;
  183. /*
  184. * No need to probe again if we have already been
  185. * here before.
  186. */
  187. if (mips_gcr_base)
  188. return 0;
  189. addr = mips_cm_phys_base();
  190. BUG_ON((addr & CM_GCR_BASE_GCRBASE) != addr);
  191. if (!addr)
  192. return -ENODEV;
  193. mips_gcr_base = ioremap_nocache(addr, MIPS_CM_GCR_SIZE);
  194. if (!mips_gcr_base)
  195. return -ENXIO;
  196. /* sanity check that we're looking at a CM */
  197. base_reg = read_gcr_base();
  198. if ((base_reg & CM_GCR_BASE_GCRBASE) != addr) {
  199. pr_err("GCRs appear to have been moved (expected them at 0x%08lx)!\n",
  200. (unsigned long)addr);
  201. mips_gcr_base = NULL;
  202. return -ENODEV;
  203. }
  204. /* set default target to memory */
  205. change_gcr_base(CM_GCR_BASE_CMDEFTGT, CM_GCR_BASE_CMDEFTGT_MEM);
  206. /* disable CM regions */
  207. write_gcr_reg0_base(CM_GCR_REGn_BASE_BASEADDR);
  208. write_gcr_reg0_mask(CM_GCR_REGn_MASK_ADDRMASK);
  209. write_gcr_reg1_base(CM_GCR_REGn_BASE_BASEADDR);
  210. write_gcr_reg1_mask(CM_GCR_REGn_MASK_ADDRMASK);
  211. write_gcr_reg2_base(CM_GCR_REGn_BASE_BASEADDR);
  212. write_gcr_reg2_mask(CM_GCR_REGn_MASK_ADDRMASK);
  213. write_gcr_reg3_base(CM_GCR_REGn_BASE_BASEADDR);
  214. write_gcr_reg3_mask(CM_GCR_REGn_MASK_ADDRMASK);
  215. /* probe for an L2-only sync region */
  216. mips_cm_probe_l2sync();
  217. /* determine register width for this CM */
  218. mips_cm_is64 = IS_ENABLED(CONFIG_64BIT) && (mips_cm_revision() >= CM_REV_CM3);
  219. for_each_possible_cpu(cpu)
  220. spin_lock_init(&per_cpu(cm_core_lock, cpu));
  221. return 0;
  222. }
  223. void mips_cm_lock_other(unsigned int cluster, unsigned int core,
  224. unsigned int vp, unsigned int block)
  225. {
  226. unsigned int curr_core, cm_rev;
  227. u32 val;
  228. cm_rev = mips_cm_revision();
  229. preempt_disable();
  230. if (cm_rev >= CM_REV_CM3) {
  231. val = core << __ffs(CM3_GCR_Cx_OTHER_CORE);
  232. val |= vp << __ffs(CM3_GCR_Cx_OTHER_VP);
  233. if (cm_rev >= CM_REV_CM3_5) {
  234. val |= CM_GCR_Cx_OTHER_CLUSTER_EN;
  235. val |= cluster << __ffs(CM_GCR_Cx_OTHER_CLUSTER);
  236. val |= block << __ffs(CM_GCR_Cx_OTHER_BLOCK);
  237. } else {
  238. WARN_ON(cluster != 0);
  239. WARN_ON(block != CM_GCR_Cx_OTHER_BLOCK_LOCAL);
  240. }
  241. /*
  242. * We need to disable interrupts in SMP systems in order to
  243. * ensure that we don't interrupt the caller with code which
  244. * may modify the redirect register. We do so here in a
  245. * slightly obscure way by using a spin lock, since this has
  246. * the neat property of also catching any nested uses of
  247. * mips_cm_lock_other() leading to a deadlock or a nice warning
  248. * with lockdep enabled.
  249. */
  250. spin_lock_irqsave(this_cpu_ptr(&cm_core_lock),
  251. *this_cpu_ptr(&cm_core_lock_flags));
  252. } else {
  253. WARN_ON(cluster != 0);
  254. WARN_ON(block != CM_GCR_Cx_OTHER_BLOCK_LOCAL);
  255. /*
  256. * We only have a GCR_CL_OTHER per core in systems with
  257. * CM 2.5 & older, so have to ensure other VP(E)s don't
  258. * race with us.
  259. */
  260. curr_core = cpu_core(&current_cpu_data);
  261. spin_lock_irqsave(&per_cpu(cm_core_lock, curr_core),
  262. per_cpu(cm_core_lock_flags, curr_core));
  263. val = core << __ffs(CM_GCR_Cx_OTHER_CORENUM);
  264. }
  265. write_gcr_cl_other(val);
  266. /*
  267. * Ensure the core-other region reflects the appropriate core &
  268. * VP before any accesses to it occur.
  269. */
  270. mb();
  271. }
  272. void mips_cm_unlock_other(void)
  273. {
  274. unsigned int curr_core;
  275. if (mips_cm_revision() < CM_REV_CM3) {
  276. curr_core = cpu_core(&current_cpu_data);
  277. spin_unlock_irqrestore(&per_cpu(cm_core_lock, curr_core),
  278. per_cpu(cm_core_lock_flags, curr_core));
  279. } else {
  280. spin_unlock_irqrestore(this_cpu_ptr(&cm_core_lock),
  281. *this_cpu_ptr(&cm_core_lock_flags));
  282. }
  283. preempt_enable();
  284. }
  285. void mips_cm_error_report(void)
  286. {
  287. u64 cm_error, cm_addr, cm_other;
  288. unsigned long revision;
  289. int ocause, cause;
  290. char buf[256];
  291. if (!mips_cm_present())
  292. return;
  293. revision = mips_cm_revision();
  294. cm_error = read_gcr_error_cause();
  295. cm_addr = read_gcr_error_addr();
  296. cm_other = read_gcr_error_mult();
  297. if (revision < CM_REV_CM3) { /* CM2 */
  298. cause = cm_error >> __ffs(CM_GCR_ERROR_CAUSE_ERRTYPE);
  299. ocause = cm_other >> __ffs(CM_GCR_ERROR_MULT_ERR2ND);
  300. if (!cause)
  301. return;
  302. if (cause < 16) {
  303. unsigned long cca_bits = (cm_error >> 15) & 7;
  304. unsigned long tr_bits = (cm_error >> 12) & 7;
  305. unsigned long cmd_bits = (cm_error >> 7) & 0x1f;
  306. unsigned long stag_bits = (cm_error >> 3) & 15;
  307. unsigned long sport_bits = (cm_error >> 0) & 7;
  308. snprintf(buf, sizeof(buf),
  309. "CCA=%lu TR=%s MCmd=%s STag=%lu "
  310. "SPort=%lu\n", cca_bits, cm2_tr[tr_bits],
  311. cm2_cmd[cmd_bits], stag_bits, sport_bits);
  312. } else {
  313. /* glob state & sresp together */
  314. unsigned long c3_bits = (cm_error >> 18) & 7;
  315. unsigned long c2_bits = (cm_error >> 15) & 7;
  316. unsigned long c1_bits = (cm_error >> 12) & 7;
  317. unsigned long c0_bits = (cm_error >> 9) & 7;
  318. unsigned long sc_bit = (cm_error >> 8) & 1;
  319. unsigned long cmd_bits = (cm_error >> 3) & 0x1f;
  320. unsigned long sport_bits = (cm_error >> 0) & 7;
  321. snprintf(buf, sizeof(buf),
  322. "C3=%s C2=%s C1=%s C0=%s SC=%s "
  323. "MCmd=%s SPort=%lu\n",
  324. cm2_core[c3_bits], cm2_core[c2_bits],
  325. cm2_core[c1_bits], cm2_core[c0_bits],
  326. sc_bit ? "True" : "False",
  327. cm2_cmd[cmd_bits], sport_bits);
  328. }
  329. pr_err("CM_ERROR=%08llx %s <%s>\n", cm_error,
  330. cm2_causes[cause], buf);
  331. pr_err("CM_ADDR =%08llx\n", cm_addr);
  332. pr_err("CM_OTHER=%08llx %s\n", cm_other, cm2_causes[ocause]);
  333. } else { /* CM3 */
  334. ulong core_id_bits, vp_id_bits, cmd_bits, cmd_group_bits;
  335. ulong cm3_cca_bits, mcp_bits, cm3_tr_bits, sched_bit;
  336. cause = cm_error >> __ffs64(CM3_GCR_ERROR_CAUSE_ERRTYPE);
  337. ocause = cm_other >> __ffs(CM_GCR_ERROR_MULT_ERR2ND);
  338. if (!cause)
  339. return;
  340. /* Used by cause == {1,2,3} */
  341. core_id_bits = (cm_error >> 22) & 0xf;
  342. vp_id_bits = (cm_error >> 18) & 0xf;
  343. cmd_bits = (cm_error >> 14) & 0xf;
  344. cmd_group_bits = (cm_error >> 11) & 0xf;
  345. cm3_cca_bits = (cm_error >> 8) & 7;
  346. mcp_bits = (cm_error >> 5) & 0xf;
  347. cm3_tr_bits = (cm_error >> 1) & 0xf;
  348. sched_bit = cm_error & 0x1;
  349. if (cause == 1 || cause == 3) { /* Tag ECC */
  350. unsigned long tag_ecc = (cm_error >> 57) & 0x1;
  351. unsigned long tag_way_bits = (cm_error >> 29) & 0xffff;
  352. unsigned long dword_bits = (cm_error >> 49) & 0xff;
  353. unsigned long data_way_bits = (cm_error >> 45) & 0xf;
  354. unsigned long data_sets_bits = (cm_error >> 29) & 0xfff;
  355. unsigned long bank_bit = (cm_error >> 28) & 0x1;
  356. snprintf(buf, sizeof(buf),
  357. "%s ECC Error: Way=%lu (DWORD=%lu, Sets=%lu)"
  358. "Bank=%lu CoreID=%lu VPID=%lu Command=%s"
  359. "Command Group=%s CCA=%lu MCP=%d"
  360. "Transaction type=%s Scheduler=%lu\n",
  361. tag_ecc ? "TAG" : "DATA",
  362. tag_ecc ? (unsigned long)ffs(tag_way_bits) - 1 :
  363. data_way_bits, bank_bit, dword_bits,
  364. data_sets_bits,
  365. core_id_bits, vp_id_bits,
  366. cm3_cmd[cmd_bits],
  367. cm3_cmd_group[cmd_group_bits],
  368. cm3_cca_bits, 1 << mcp_bits,
  369. cm3_tr[cm3_tr_bits], sched_bit);
  370. } else if (cause == 2) {
  371. unsigned long data_error_type = (cm_error >> 41) & 0xfff;
  372. unsigned long data_decode_cmd = (cm_error >> 37) & 0xf;
  373. unsigned long data_decode_group = (cm_error >> 34) & 0x7;
  374. unsigned long data_decode_destination_id = (cm_error >> 28) & 0x3f;
  375. snprintf(buf, sizeof(buf),
  376. "Decode Request Error: Type=%lu, Command=%lu"
  377. "Command Group=%lu Destination ID=%lu"
  378. "CoreID=%lu VPID=%lu Command=%s"
  379. "Command Group=%s CCA=%lu MCP=%d"
  380. "Transaction type=%s Scheduler=%lu\n",
  381. data_error_type, data_decode_cmd,
  382. data_decode_group, data_decode_destination_id,
  383. core_id_bits, vp_id_bits,
  384. cm3_cmd[cmd_bits],
  385. cm3_cmd_group[cmd_group_bits],
  386. cm3_cca_bits, 1 << mcp_bits,
  387. cm3_tr[cm3_tr_bits], sched_bit);
  388. } else {
  389. buf[0] = 0;
  390. }
  391. pr_err("CM_ERROR=%llx %s <%s>\n", cm_error,
  392. cm3_causes[cause], buf);
  393. pr_err("CM_ADDR =%llx\n", cm_addr);
  394. pr_err("CM_OTHER=%llx %s\n", cm_other, cm3_causes[ocause]);
  395. }
  396. /* reprime cause register */
  397. write_gcr_error_cause(cm_error);
  398. }