tile_edac.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright 2011 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. * Tilera-specific EDAC driver.
  14. *
  15. * This source code is derived from the following driver:
  16. *
  17. * Cell MIC driver for ECC counting
  18. *
  19. * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
  20. * <benh@kernel.crashing.org>
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/io.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/edac.h>
  29. #include <hv/hypervisor.h>
  30. #include <hv/drv_mshim_intf.h>
  31. #include "edac_core.h"
  32. #define DRV_NAME "tile-edac"
  33. /* Number of cs_rows needed per memory controller on TILEPro. */
  34. #define TILE_EDAC_NR_CSROWS 1
  35. /* Number of channels per memory controller on TILEPro. */
  36. #define TILE_EDAC_NR_CHANS 1
  37. /* Granularity of reported error in bytes on TILEPro. */
  38. #define TILE_EDAC_ERROR_GRAIN 8
  39. /* TILE processor has multiple independent memory controllers. */
  40. struct platform_device *mshim_pdev[TILE_MAX_MSHIMS];
  41. struct tile_edac_priv {
  42. int hv_devhdl; /* Hypervisor device handle. */
  43. int node; /* Memory controller instance #. */
  44. unsigned int ce_count; /*
  45. * Correctable-error counter
  46. * kept by the driver.
  47. */
  48. };
  49. static void tile_edac_check(struct mem_ctl_info *mci)
  50. {
  51. struct tile_edac_priv *priv = mci->pvt_info;
  52. struct mshim_mem_error mem_error;
  53. if (hv_dev_pread(priv->hv_devhdl, 0, (HV_VirtAddr)&mem_error,
  54. sizeof(struct mshim_mem_error), MSHIM_MEM_ERROR_OFF) !=
  55. sizeof(struct mshim_mem_error)) {
  56. pr_err(DRV_NAME ": MSHIM_MEM_ERROR_OFF pread failure.\n");
  57. return;
  58. }
  59. /* Check if the current error count is different from the saved one. */
  60. if (mem_error.sbe_count != priv->ce_count) {
  61. dev_dbg(mci->pdev, "ECC CE err on node %d\n", priv->node);
  62. priv->ce_count = mem_error.sbe_count;
  63. edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, 1,
  64. 0, 0, 0,
  65. 0, 0, -1,
  66. mci->ctl_name, "");
  67. }
  68. }
  69. /*
  70. * Initialize the 'csrows' table within the mci control structure with the
  71. * addressing of memory.
  72. */
  73. static int tile_edac_init_csrows(struct mem_ctl_info *mci)
  74. {
  75. struct csrow_info *csrow = mci->csrows[0];
  76. struct tile_edac_priv *priv = mci->pvt_info;
  77. struct mshim_mem_info mem_info;
  78. struct dimm_info *dimm = csrow->channels[0]->dimm;
  79. if (hv_dev_pread(priv->hv_devhdl, 0, (HV_VirtAddr)&mem_info,
  80. sizeof(struct mshim_mem_info), MSHIM_MEM_INFO_OFF) !=
  81. sizeof(struct mshim_mem_info)) {
  82. pr_err(DRV_NAME ": MSHIM_MEM_INFO_OFF pread failure.\n");
  83. return -1;
  84. }
  85. if (mem_info.mem_ecc)
  86. dimm->edac_mode = EDAC_SECDED;
  87. else
  88. dimm->edac_mode = EDAC_NONE;
  89. switch (mem_info.mem_type) {
  90. case DDR2:
  91. dimm->mtype = MEM_DDR2;
  92. break;
  93. case DDR3:
  94. dimm->mtype = MEM_DDR3;
  95. break;
  96. default:
  97. return -1;
  98. }
  99. dimm->nr_pages = mem_info.mem_size >> PAGE_SHIFT;
  100. dimm->grain = TILE_EDAC_ERROR_GRAIN;
  101. dimm->dtype = DEV_UNKNOWN;
  102. return 0;
  103. }
  104. static int tile_edac_mc_probe(struct platform_device *pdev)
  105. {
  106. char hv_file[32];
  107. int hv_devhdl;
  108. struct mem_ctl_info *mci;
  109. struct edac_mc_layer layers[2];
  110. struct tile_edac_priv *priv;
  111. int rc;
  112. sprintf(hv_file, "mshim/%d", pdev->id);
  113. hv_devhdl = hv_dev_open((HV_VirtAddr)hv_file, 0);
  114. if (hv_devhdl < 0)
  115. return -EINVAL;
  116. /* A TILE MC has a single channel and one chip-select row. */
  117. layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
  118. layers[0].size = TILE_EDAC_NR_CSROWS;
  119. layers[0].is_virt_csrow = true;
  120. layers[1].type = EDAC_MC_LAYER_CHANNEL;
  121. layers[1].size = TILE_EDAC_NR_CHANS;
  122. layers[1].is_virt_csrow = false;
  123. mci = edac_mc_alloc(pdev->id, ARRAY_SIZE(layers), layers,
  124. sizeof(struct tile_edac_priv));
  125. if (mci == NULL)
  126. return -ENOMEM;
  127. priv = mci->pvt_info;
  128. priv->node = pdev->id;
  129. priv->hv_devhdl = hv_devhdl;
  130. mci->pdev = &pdev->dev;
  131. mci->mtype_cap = MEM_FLAG_DDR2;
  132. mci->edac_ctl_cap = EDAC_FLAG_SECDED;
  133. mci->mod_name = DRV_NAME;
  134. #ifdef __tilegx__
  135. mci->ctl_name = "TILEGx_Memory_Controller";
  136. #else
  137. mci->ctl_name = "TILEPro_Memory_Controller";
  138. #endif
  139. mci->dev_name = dev_name(&pdev->dev);
  140. mci->edac_check = tile_edac_check;
  141. /*
  142. * Initialize the MC control structure 'csrows' table
  143. * with the mapping and control information.
  144. */
  145. if (tile_edac_init_csrows(mci)) {
  146. /* No csrows found. */
  147. mci->edac_cap = EDAC_FLAG_NONE;
  148. } else {
  149. mci->edac_cap = EDAC_FLAG_SECDED;
  150. }
  151. platform_set_drvdata(pdev, mci);
  152. /* Register with EDAC core */
  153. rc = edac_mc_add_mc(mci);
  154. if (rc) {
  155. dev_err(&pdev->dev, "failed to register with EDAC core\n");
  156. edac_mc_free(mci);
  157. return rc;
  158. }
  159. return 0;
  160. }
  161. static int tile_edac_mc_remove(struct platform_device *pdev)
  162. {
  163. struct mem_ctl_info *mci = platform_get_drvdata(pdev);
  164. edac_mc_del_mc(&pdev->dev);
  165. if (mci)
  166. edac_mc_free(mci);
  167. return 0;
  168. }
  169. static struct platform_driver tile_edac_mc_driver = {
  170. .driver = {
  171. .name = DRV_NAME,
  172. },
  173. .probe = tile_edac_mc_probe,
  174. .remove = tile_edac_mc_remove,
  175. };
  176. /*
  177. * Driver init routine.
  178. */
  179. static int __init tile_edac_init(void)
  180. {
  181. char hv_file[32];
  182. struct platform_device *pdev;
  183. int i, err, num = 0;
  184. /* Only support POLL mode. */
  185. edac_op_state = EDAC_OPSTATE_POLL;
  186. err = platform_driver_register(&tile_edac_mc_driver);
  187. if (err)
  188. return err;
  189. for (i = 0; i < TILE_MAX_MSHIMS; i++) {
  190. /*
  191. * Not all memory controllers are configured such as in the
  192. * case of a simulator. So we register only those mshims
  193. * that are configured by the hypervisor.
  194. */
  195. sprintf(hv_file, "mshim/%d", i);
  196. if (hv_dev_open((HV_VirtAddr)hv_file, 0) < 0)
  197. continue;
  198. pdev = platform_device_register_simple(DRV_NAME, i, NULL, 0);
  199. if (IS_ERR(pdev))
  200. continue;
  201. mshim_pdev[i] = pdev;
  202. num++;
  203. }
  204. if (num == 0) {
  205. platform_driver_unregister(&tile_edac_mc_driver);
  206. return -ENODEV;
  207. }
  208. return 0;
  209. }
  210. /*
  211. * Driver cleanup routine.
  212. */
  213. static void __exit tile_edac_exit(void)
  214. {
  215. int i;
  216. for (i = 0; i < TILE_MAX_MSHIMS; i++) {
  217. struct platform_device *pdev = mshim_pdev[i];
  218. if (!pdev)
  219. continue;
  220. platform_device_unregister(pdev);
  221. }
  222. platform_driver_unregister(&tile_edac_mc_driver);
  223. }
  224. module_init(tile_edac_init);
  225. module_exit(tile_edac_exit);