octeon_edac-lmc.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2009 Wind River Systems,
  7. * written by Ralf Baechle <ralf@linux-mips.org>
  8. *
  9. * Copyright (c) 2013 by Cisco Systems, Inc.
  10. * All rights reserved.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/io.h>
  16. #include <linux/edac.h>
  17. #include <linux/ctype.h>
  18. #include <asm/octeon/octeon.h>
  19. #include <asm/octeon/cvmx-lmcx-defs.h>
  20. #include "edac_core.h"
  21. #include "edac_module.h"
  22. #define OCTEON_MAX_MC 4
  23. #define to_mci(k) container_of(k, struct mem_ctl_info, dev)
  24. struct octeon_lmc_pvt {
  25. unsigned long inject;
  26. unsigned long error_type;
  27. unsigned long dimm;
  28. unsigned long rank;
  29. unsigned long bank;
  30. unsigned long row;
  31. unsigned long col;
  32. };
  33. static void octeon_lmc_edac_poll(struct mem_ctl_info *mci)
  34. {
  35. union cvmx_lmcx_mem_cfg0 cfg0;
  36. bool do_clear = false;
  37. char msg[64];
  38. cfg0.u64 = cvmx_read_csr(CVMX_LMCX_MEM_CFG0(mci->mc_idx));
  39. if (cfg0.s.sec_err || cfg0.s.ded_err) {
  40. union cvmx_lmcx_fadr fadr;
  41. fadr.u64 = cvmx_read_csr(CVMX_LMCX_FADR(mci->mc_idx));
  42. snprintf(msg, sizeof(msg),
  43. "DIMM %d rank %d bank %d row %d col %d",
  44. fadr.cn30xx.fdimm, fadr.cn30xx.fbunk,
  45. fadr.cn30xx.fbank, fadr.cn30xx.frow, fadr.cn30xx.fcol);
  46. }
  47. if (cfg0.s.sec_err) {
  48. edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, 1, 0, 0, 0,
  49. -1, -1, -1, msg, "");
  50. cfg0.s.sec_err = -1; /* Done, re-arm */
  51. do_clear = true;
  52. }
  53. if (cfg0.s.ded_err) {
  54. edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci, 1, 0, 0, 0,
  55. -1, -1, -1, msg, "");
  56. cfg0.s.ded_err = -1; /* Done, re-arm */
  57. do_clear = true;
  58. }
  59. if (do_clear)
  60. cvmx_write_csr(CVMX_LMCX_MEM_CFG0(mci->mc_idx), cfg0.u64);
  61. }
  62. static void octeon_lmc_edac_poll_o2(struct mem_ctl_info *mci)
  63. {
  64. struct octeon_lmc_pvt *pvt = mci->pvt_info;
  65. union cvmx_lmcx_int int_reg;
  66. bool do_clear = false;
  67. char msg[64];
  68. if (!pvt->inject)
  69. int_reg.u64 = cvmx_read_csr(CVMX_LMCX_INT(mci->mc_idx));
  70. else {
  71. int_reg.u64 = 0;
  72. if (pvt->error_type == 1)
  73. int_reg.s.sec_err = 1;
  74. if (pvt->error_type == 2)
  75. int_reg.s.ded_err = 1;
  76. }
  77. if (int_reg.s.sec_err || int_reg.s.ded_err) {
  78. union cvmx_lmcx_fadr fadr;
  79. if (likely(!pvt->inject))
  80. fadr.u64 = cvmx_read_csr(CVMX_LMCX_FADR(mci->mc_idx));
  81. else {
  82. fadr.cn61xx.fdimm = pvt->dimm;
  83. fadr.cn61xx.fbunk = pvt->rank;
  84. fadr.cn61xx.fbank = pvt->bank;
  85. fadr.cn61xx.frow = pvt->row;
  86. fadr.cn61xx.fcol = pvt->col;
  87. }
  88. snprintf(msg, sizeof(msg),
  89. "DIMM %d rank %d bank %d row %d col %d",
  90. fadr.cn61xx.fdimm, fadr.cn61xx.fbunk,
  91. fadr.cn61xx.fbank, fadr.cn61xx.frow, fadr.cn61xx.fcol);
  92. }
  93. if (int_reg.s.sec_err) {
  94. edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, 1, 0, 0, 0,
  95. -1, -1, -1, msg, "");
  96. int_reg.s.sec_err = -1; /* Done, re-arm */
  97. do_clear = true;
  98. }
  99. if (int_reg.s.ded_err) {
  100. edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci, 1, 0, 0, 0,
  101. -1, -1, -1, msg, "");
  102. int_reg.s.ded_err = -1; /* Done, re-arm */
  103. do_clear = true;
  104. }
  105. if (do_clear) {
  106. if (likely(!pvt->inject))
  107. cvmx_write_csr(CVMX_LMCX_INT(mci->mc_idx), int_reg.u64);
  108. else
  109. pvt->inject = 0;
  110. }
  111. }
  112. /************************ MC SYSFS parts ***********************************/
  113. /* Only a couple naming differences per template, so very similar */
  114. #define TEMPLATE_SHOW(reg) \
  115. static ssize_t octeon_mc_inject_##reg##_show(struct device *dev, \
  116. struct device_attribute *attr, \
  117. char *data) \
  118. { \
  119. struct mem_ctl_info *mci = to_mci(dev); \
  120. struct octeon_lmc_pvt *pvt = mci->pvt_info; \
  121. return sprintf(data, "%016llu\n", (u64)pvt->reg); \
  122. }
  123. #define TEMPLATE_STORE(reg) \
  124. static ssize_t octeon_mc_inject_##reg##_store(struct device *dev, \
  125. struct device_attribute *attr, \
  126. const char *data, size_t count) \
  127. { \
  128. struct mem_ctl_info *mci = to_mci(dev); \
  129. struct octeon_lmc_pvt *pvt = mci->pvt_info; \
  130. if (isdigit(*data)) { \
  131. if (!kstrtoul(data, 0, &pvt->reg)) \
  132. return count; \
  133. } \
  134. return 0; \
  135. }
  136. TEMPLATE_SHOW(inject);
  137. TEMPLATE_STORE(inject);
  138. TEMPLATE_SHOW(dimm);
  139. TEMPLATE_STORE(dimm);
  140. TEMPLATE_SHOW(bank);
  141. TEMPLATE_STORE(bank);
  142. TEMPLATE_SHOW(rank);
  143. TEMPLATE_STORE(rank);
  144. TEMPLATE_SHOW(row);
  145. TEMPLATE_STORE(row);
  146. TEMPLATE_SHOW(col);
  147. TEMPLATE_STORE(col);
  148. static ssize_t octeon_mc_inject_error_type_store(struct device *dev,
  149. struct device_attribute *attr,
  150. const char *data,
  151. size_t count)
  152. {
  153. struct mem_ctl_info *mci = to_mci(dev);
  154. struct octeon_lmc_pvt *pvt = mci->pvt_info;
  155. if (!strncmp(data, "single", 6))
  156. pvt->error_type = 1;
  157. else if (!strncmp(data, "double", 6))
  158. pvt->error_type = 2;
  159. return count;
  160. }
  161. static ssize_t octeon_mc_inject_error_type_show(struct device *dev,
  162. struct device_attribute *attr,
  163. char *data)
  164. {
  165. struct mem_ctl_info *mci = to_mci(dev);
  166. struct octeon_lmc_pvt *pvt = mci->pvt_info;
  167. if (pvt->error_type == 1)
  168. return sprintf(data, "single");
  169. else if (pvt->error_type == 2)
  170. return sprintf(data, "double");
  171. return 0;
  172. }
  173. static DEVICE_ATTR(inject, S_IRUGO | S_IWUSR,
  174. octeon_mc_inject_inject_show, octeon_mc_inject_inject_store);
  175. static DEVICE_ATTR(error_type, S_IRUGO | S_IWUSR,
  176. octeon_mc_inject_error_type_show, octeon_mc_inject_error_type_store);
  177. static DEVICE_ATTR(dimm, S_IRUGO | S_IWUSR,
  178. octeon_mc_inject_dimm_show, octeon_mc_inject_dimm_store);
  179. static DEVICE_ATTR(rank, S_IRUGO | S_IWUSR,
  180. octeon_mc_inject_rank_show, octeon_mc_inject_rank_store);
  181. static DEVICE_ATTR(bank, S_IRUGO | S_IWUSR,
  182. octeon_mc_inject_bank_show, octeon_mc_inject_bank_store);
  183. static DEVICE_ATTR(row, S_IRUGO | S_IWUSR,
  184. octeon_mc_inject_row_show, octeon_mc_inject_row_store);
  185. static DEVICE_ATTR(col, S_IRUGO | S_IWUSR,
  186. octeon_mc_inject_col_show, octeon_mc_inject_col_store);
  187. static struct attribute *octeon_dev_attrs[] = {
  188. &dev_attr_inject.attr,
  189. &dev_attr_error_type.attr,
  190. &dev_attr_dimm.attr,
  191. &dev_attr_rank.attr,
  192. &dev_attr_bank.attr,
  193. &dev_attr_row.attr,
  194. &dev_attr_col.attr,
  195. NULL
  196. };
  197. ATTRIBUTE_GROUPS(octeon_dev);
  198. static int octeon_lmc_edac_probe(struct platform_device *pdev)
  199. {
  200. struct mem_ctl_info *mci;
  201. struct edac_mc_layer layers[1];
  202. int mc = pdev->id;
  203. opstate_init();
  204. layers[0].type = EDAC_MC_LAYER_CHANNEL;
  205. layers[0].size = 1;
  206. layers[0].is_virt_csrow = false;
  207. if (OCTEON_IS_OCTEON1PLUS()) {
  208. union cvmx_lmcx_mem_cfg0 cfg0;
  209. cfg0.u64 = cvmx_read_csr(CVMX_LMCX_MEM_CFG0(0));
  210. if (!cfg0.s.ecc_ena) {
  211. dev_info(&pdev->dev, "Disabled (ECC not enabled)\n");
  212. return 0;
  213. }
  214. mci = edac_mc_alloc(mc, ARRAY_SIZE(layers), layers, sizeof(struct octeon_lmc_pvt));
  215. if (!mci)
  216. return -ENXIO;
  217. mci->pdev = &pdev->dev;
  218. mci->dev_name = dev_name(&pdev->dev);
  219. mci->mod_name = "octeon-lmc";
  220. mci->ctl_name = "octeon-lmc-err";
  221. mci->edac_check = octeon_lmc_edac_poll;
  222. if (edac_mc_add_mc_with_groups(mci, octeon_dev_groups)) {
  223. dev_err(&pdev->dev, "edac_mc_add_mc() failed\n");
  224. edac_mc_free(mci);
  225. return -ENXIO;
  226. }
  227. cfg0.u64 = cvmx_read_csr(CVMX_LMCX_MEM_CFG0(mc));
  228. cfg0.s.intr_ded_ena = 0; /* We poll */
  229. cfg0.s.intr_sec_ena = 0;
  230. cvmx_write_csr(CVMX_LMCX_MEM_CFG0(mc), cfg0.u64);
  231. } else {
  232. /* OCTEON II */
  233. union cvmx_lmcx_int_en en;
  234. union cvmx_lmcx_config config;
  235. config.u64 = cvmx_read_csr(CVMX_LMCX_CONFIG(0));
  236. if (!config.s.ecc_ena) {
  237. dev_info(&pdev->dev, "Disabled (ECC not enabled)\n");
  238. return 0;
  239. }
  240. mci = edac_mc_alloc(mc, ARRAY_SIZE(layers), layers, sizeof(struct octeon_lmc_pvt));
  241. if (!mci)
  242. return -ENXIO;
  243. mci->pdev = &pdev->dev;
  244. mci->dev_name = dev_name(&pdev->dev);
  245. mci->mod_name = "octeon-lmc";
  246. mci->ctl_name = "co_lmc_err";
  247. mci->edac_check = octeon_lmc_edac_poll_o2;
  248. if (edac_mc_add_mc_with_groups(mci, octeon_dev_groups)) {
  249. dev_err(&pdev->dev, "edac_mc_add_mc() failed\n");
  250. edac_mc_free(mci);
  251. return -ENXIO;
  252. }
  253. en.u64 = cvmx_read_csr(CVMX_LMCX_MEM_CFG0(mc));
  254. en.s.intr_ded_ena = 0; /* We poll */
  255. en.s.intr_sec_ena = 0;
  256. cvmx_write_csr(CVMX_LMCX_MEM_CFG0(mc), en.u64);
  257. }
  258. platform_set_drvdata(pdev, mci);
  259. return 0;
  260. }
  261. static int octeon_lmc_edac_remove(struct platform_device *pdev)
  262. {
  263. struct mem_ctl_info *mci = platform_get_drvdata(pdev);
  264. edac_mc_del_mc(&pdev->dev);
  265. edac_mc_free(mci);
  266. return 0;
  267. }
  268. static struct platform_driver octeon_lmc_edac_driver = {
  269. .probe = octeon_lmc_edac_probe,
  270. .remove = octeon_lmc_edac_remove,
  271. .driver = {
  272. .name = "octeon_lmc_edac",
  273. }
  274. };
  275. module_platform_driver(octeon_lmc_edac_driver);
  276. MODULE_LICENSE("GPL");
  277. MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");