mips-cm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Copyright (C) 2013 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. #include <linux/errno.h>
  11. #include <linux/percpu.h>
  12. #include <linux/spinlock.h>
  13. #include <asm/mips-cm.h>
  14. #include <asm/mipsregs.h>
  15. void __iomem *mips_cm_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 transation 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", "0x11", "0x12", "0x13",
  117. "0x14", "0x15", "0x16", "INTVN_WR_ERR",
  118. "INTVN_RD_ERR", "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_MSK)
  153. return base_reg & CM_GCR_L2_ONLY_SYNC_BASE_SYNCBASE_MSK;
  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_MSK) >>
  165. CM_GCR_REV_MAJOR_SHF;
  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_MSK) != 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_MSK);
  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_cm_base)
  188. return 0;
  189. addr = mips_cm_phys_base();
  190. BUG_ON((addr & CM_GCR_BASE_GCRBASE_MSK) != addr);
  191. if (!addr)
  192. return -ENODEV;
  193. mips_cm_base = ioremap_nocache(addr, MIPS_CM_GCR_SIZE);
  194. if (!mips_cm_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_MSK) != addr) {
  199. pr_err("GCRs appear to have been moved (expected them at 0x%08lx)!\n",
  200. (unsigned long)addr);
  201. mips_cm_base = NULL;
  202. return -ENODEV;
  203. }
  204. /* set default target to memory */
  205. base_reg &= ~CM_GCR_BASE_CMDEFTGT_MSK;
  206. base_reg |= CM_GCR_BASE_CMDEFTGT_MEM;
  207. write_gcr_base(base_reg);
  208. /* disable CM regions */
  209. write_gcr_reg0_base(CM_GCR_REGn_BASE_BASEADDR_MSK);
  210. write_gcr_reg0_mask(CM_GCR_REGn_MASK_ADDRMASK_MSK);
  211. write_gcr_reg1_base(CM_GCR_REGn_BASE_BASEADDR_MSK);
  212. write_gcr_reg1_mask(CM_GCR_REGn_MASK_ADDRMASK_MSK);
  213. write_gcr_reg2_base(CM_GCR_REGn_BASE_BASEADDR_MSK);
  214. write_gcr_reg2_mask(CM_GCR_REGn_MASK_ADDRMASK_MSK);
  215. write_gcr_reg3_base(CM_GCR_REGn_BASE_BASEADDR_MSK);
  216. write_gcr_reg3_mask(CM_GCR_REGn_MASK_ADDRMASK_MSK);
  217. /* probe for an L2-only sync region */
  218. mips_cm_probe_l2sync();
  219. /* determine register width for this CM */
  220. mips_cm_is64 = config_enabled(CONFIG_64BIT) && (mips_cm_revision() >= CM_REV_CM3);
  221. for_each_possible_cpu(cpu)
  222. spin_lock_init(&per_cpu(cm_core_lock, cpu));
  223. return 0;
  224. }
  225. void mips_cm_lock_other(unsigned int core, unsigned int vp)
  226. {
  227. unsigned curr_core;
  228. u32 val;
  229. preempt_disable();
  230. curr_core = current_cpu_data.core;
  231. spin_lock_irqsave(&per_cpu(cm_core_lock, curr_core),
  232. per_cpu(cm_core_lock_flags, curr_core));
  233. if (mips_cm_revision() >= CM_REV_CM3) {
  234. val = core << CM3_GCR_Cx_OTHER_CORE_SHF;
  235. val |= vp << CM3_GCR_Cx_OTHER_VP_SHF;
  236. } else {
  237. BUG_ON(vp != 0);
  238. val = core << CM_GCR_Cx_OTHER_CORENUM_SHF;
  239. }
  240. write_gcr_cl_other(val);
  241. /*
  242. * Ensure the core-other region reflects the appropriate core &
  243. * VP before any accesses to it occur.
  244. */
  245. mb();
  246. }
  247. void mips_cm_unlock_other(void)
  248. {
  249. unsigned curr_core = current_cpu_data.core;
  250. spin_unlock_irqrestore(&per_cpu(cm_core_lock, curr_core),
  251. per_cpu(cm_core_lock_flags, curr_core));
  252. preempt_enable();
  253. }
  254. void mips_cm_error_report(void)
  255. {
  256. u64 cm_error, cm_addr, cm_other;
  257. unsigned long revision;
  258. int ocause, cause;
  259. char buf[256];
  260. if (!mips_cm_present())
  261. return;
  262. revision = mips_cm_revision();
  263. if (revision < CM_REV_CM3) { /* CM2 */
  264. cm_error = read_gcr_error_cause();
  265. cm_addr = read_gcr_error_addr();
  266. cm_other = read_gcr_error_mult();
  267. cause = cm_error >> CM_GCR_ERROR_CAUSE_ERRTYPE_SHF;
  268. ocause = cm_other >> CM_GCR_ERROR_MULT_ERR2ND_SHF;
  269. if (!cause)
  270. return;
  271. if (cause < 16) {
  272. unsigned long cca_bits = (cm_error >> 15) & 7;
  273. unsigned long tr_bits = (cm_error >> 12) & 7;
  274. unsigned long cmd_bits = (cm_error >> 7) & 0x1f;
  275. unsigned long stag_bits = (cm_error >> 3) & 15;
  276. unsigned long sport_bits = (cm_error >> 0) & 7;
  277. snprintf(buf, sizeof(buf),
  278. "CCA=%lu TR=%s MCmd=%s STag=%lu "
  279. "SPort=%lu\n", cca_bits, cm2_tr[tr_bits],
  280. cm2_cmd[cmd_bits], stag_bits, sport_bits);
  281. } else {
  282. /* glob state & sresp together */
  283. unsigned long c3_bits = (cm_error >> 18) & 7;
  284. unsigned long c2_bits = (cm_error >> 15) & 7;
  285. unsigned long c1_bits = (cm_error >> 12) & 7;
  286. unsigned long c0_bits = (cm_error >> 9) & 7;
  287. unsigned long sc_bit = (cm_error >> 8) & 1;
  288. unsigned long cmd_bits = (cm_error >> 3) & 0x1f;
  289. unsigned long sport_bits = (cm_error >> 0) & 7;
  290. snprintf(buf, sizeof(buf),
  291. "C3=%s C2=%s C1=%s C0=%s SC=%s "
  292. "MCmd=%s SPort=%lu\n",
  293. cm2_core[c3_bits], cm2_core[c2_bits],
  294. cm2_core[c1_bits], cm2_core[c0_bits],
  295. sc_bit ? "True" : "False",
  296. cm2_cmd[cmd_bits], sport_bits);
  297. }
  298. pr_err("CM_ERROR=%08llx %s <%s>\n", cm_error,
  299. cm2_causes[cause], buf);
  300. pr_err("CM_ADDR =%08llx\n", cm_addr);
  301. pr_err("CM_OTHER=%08llx %s\n", cm_other, cm2_causes[ocause]);
  302. } else { /* CM3 */
  303. ulong core_id_bits, vp_id_bits, cmd_bits, cmd_group_bits;
  304. ulong cm3_cca_bits, mcp_bits, cm3_tr_bits, sched_bit;
  305. cm_error = read64_gcr_error_cause();
  306. cm_addr = read64_gcr_error_addr();
  307. cm_other = read64_gcr_error_mult();
  308. cause = cm_error >> CM3_GCR_ERROR_CAUSE_ERRTYPE_SHF;
  309. ocause = cm_other >> CM_GCR_ERROR_MULT_ERR2ND_SHF;
  310. if (!cause)
  311. return;
  312. /* Used by cause == {1,2,3} */
  313. core_id_bits = (cm_error >> 22) & 0xf;
  314. vp_id_bits = (cm_error >> 18) & 0xf;
  315. cmd_bits = (cm_error >> 14) & 0xf;
  316. cmd_group_bits = (cm_error >> 11) & 0xf;
  317. cm3_cca_bits = (cm_error >> 8) & 7;
  318. mcp_bits = (cm_error >> 5) & 0xf;
  319. cm3_tr_bits = (cm_error >> 1) & 0xf;
  320. sched_bit = cm_error & 0x1;
  321. if (cause == 1 || cause == 3) { /* Tag ECC */
  322. unsigned long tag_ecc = (cm_error >> 57) & 0x1;
  323. unsigned long tag_way_bits = (cm_error >> 29) & 0xffff;
  324. unsigned long dword_bits = (cm_error >> 49) & 0xff;
  325. unsigned long data_way_bits = (cm_error >> 45) & 0xf;
  326. unsigned long data_sets_bits = (cm_error >> 29) & 0xfff;
  327. unsigned long bank_bit = (cm_error >> 28) & 0x1;
  328. snprintf(buf, sizeof(buf),
  329. "%s ECC Error: Way=%lu (DWORD=%lu, Sets=%lu)"
  330. "Bank=%lu CoreID=%lu VPID=%lu Command=%s"
  331. "Command Group=%s CCA=%lu MCP=%d"
  332. "Transaction type=%s Scheduler=%lu\n",
  333. tag_ecc ? "TAG" : "DATA",
  334. tag_ecc ? (unsigned long)ffs(tag_way_bits) - 1 :
  335. data_way_bits, bank_bit, dword_bits,
  336. data_sets_bits,
  337. core_id_bits, vp_id_bits,
  338. cm3_cmd[cmd_bits],
  339. cm3_cmd_group[cmd_group_bits],
  340. cm3_cca_bits, 1 << mcp_bits,
  341. cm3_tr[cm3_tr_bits], sched_bit);
  342. } else if (cause == 2) {
  343. unsigned long data_error_type = (cm_error >> 41) & 0xfff;
  344. unsigned long data_decode_cmd = (cm_error >> 37) & 0xf;
  345. unsigned long data_decode_group = (cm_error >> 34) & 0x7;
  346. unsigned long data_decode_destination_id = (cm_error >> 28) & 0x3f;
  347. snprintf(buf, sizeof(buf),
  348. "Decode Request Error: Type=%lu, Command=%lu"
  349. "Command Group=%lu Destination ID=%lu"
  350. "CoreID=%lu VPID=%lu Command=%s"
  351. "Command Group=%s CCA=%lu MCP=%d"
  352. "Transaction type=%s Scheduler=%lu\n",
  353. data_error_type, data_decode_cmd,
  354. data_decode_group, data_decode_destination_id,
  355. core_id_bits, vp_id_bits,
  356. cm3_cmd[cmd_bits],
  357. cm3_cmd_group[cmd_group_bits],
  358. cm3_cca_bits, 1 << mcp_bits,
  359. cm3_tr[cm3_tr_bits], sched_bit);
  360. } else {
  361. buf[0] = 0;
  362. }
  363. pr_err("CM_ERROR=%llx %s <%s>\n", cm_error,
  364. cm3_causes[cause], buf);
  365. pr_err("CM_ADDR =%llx\n", cm_addr);
  366. pr_err("CM_OTHER=%llx %s\n", cm_other, cm3_causes[ocause]);
  367. }
  368. /* reprime cause register */
  369. write_gcr_error_cause(cm_error);
  370. }