tile_edac.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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->dev, "ECC CE err on node %d\n", priv->node);
  62. priv->ce_count = mem_error.sbe_count;
  63. edac_mc_handle_ce(mci, 0, 0, 0, 0, 0, mci->ctl_name);
  64. }
  65. }
  66. /*
  67. * Initialize the 'csrows' table within the mci control structure with the
  68. * addressing of memory.
  69. */
  70. static int __devinit tile_edac_init_csrows(struct mem_ctl_info *mci)
  71. {
  72. struct csrow_info *csrow = &mci->csrows[0];
  73. struct tile_edac_priv *priv = mci->pvt_info;
  74. struct mshim_mem_info mem_info;
  75. struct dimm_info *dimm = csrow->channels[0].dimm;
  76. if (hv_dev_pread(priv->hv_devhdl, 0, (HV_VirtAddr)&mem_info,
  77. sizeof(struct mshim_mem_info), MSHIM_MEM_INFO_OFF) !=
  78. sizeof(struct mshim_mem_info)) {
  79. pr_err(DRV_NAME ": MSHIM_MEM_INFO_OFF pread failure.\n");
  80. return -1;
  81. }
  82. if (mem_info.mem_ecc)
  83. dimm->edac_mode = EDAC_SECDED;
  84. else
  85. dimm->edac_mode = EDAC_NONE;
  86. switch (mem_info.mem_type) {
  87. case DDR2:
  88. dimm->mtype = MEM_DDR2;
  89. break;
  90. case DDR3:
  91. dimm->mtype = MEM_DDR3;
  92. break;
  93. default:
  94. return -1;
  95. }
  96. csrow->nr_pages = mem_info.mem_size >> PAGE_SHIFT;
  97. dimm->grain = TILE_EDAC_ERROR_GRAIN;
  98. dimm->dtype = DEV_UNKNOWN;
  99. return 0;
  100. }
  101. static int __devinit tile_edac_mc_probe(struct platform_device *pdev)
  102. {
  103. char hv_file[32];
  104. int hv_devhdl;
  105. struct mem_ctl_info *mci;
  106. struct tile_edac_priv *priv;
  107. int rc;
  108. sprintf(hv_file, "mshim/%d", pdev->id);
  109. hv_devhdl = hv_dev_open((HV_VirtAddr)hv_file, 0);
  110. if (hv_devhdl < 0)
  111. return -EINVAL;
  112. /* A TILE MC has a single channel and one chip-select row. */
  113. mci = edac_mc_alloc(sizeof(struct tile_edac_priv),
  114. TILE_EDAC_NR_CSROWS, TILE_EDAC_NR_CHANS, pdev->id);
  115. if (mci == NULL)
  116. return -ENOMEM;
  117. priv = mci->pvt_info;
  118. priv->node = pdev->id;
  119. priv->hv_devhdl = hv_devhdl;
  120. mci->dev = &pdev->dev;
  121. mci->mtype_cap = MEM_FLAG_DDR2;
  122. mci->edac_ctl_cap = EDAC_FLAG_SECDED;
  123. mci->mod_name = DRV_NAME;
  124. #ifdef __tilegx__
  125. mci->ctl_name = "TILEGx_Memory_Controller";
  126. #else
  127. mci->ctl_name = "TILEPro_Memory_Controller";
  128. #endif
  129. mci->dev_name = dev_name(&pdev->dev);
  130. mci->edac_check = tile_edac_check;
  131. /*
  132. * Initialize the MC control structure 'csrows' table
  133. * with the mapping and control information.
  134. */
  135. if (tile_edac_init_csrows(mci)) {
  136. /* No csrows found. */
  137. mci->edac_cap = EDAC_FLAG_NONE;
  138. } else {
  139. mci->edac_cap = EDAC_FLAG_SECDED;
  140. }
  141. platform_set_drvdata(pdev, mci);
  142. /* Register with EDAC core */
  143. rc = edac_mc_add_mc(mci);
  144. if (rc) {
  145. dev_err(&pdev->dev, "failed to register with EDAC core\n");
  146. edac_mc_free(mci);
  147. return rc;
  148. }
  149. return 0;
  150. }
  151. static int __devexit tile_edac_mc_remove(struct platform_device *pdev)
  152. {
  153. struct mem_ctl_info *mci = platform_get_drvdata(pdev);
  154. edac_mc_del_mc(&pdev->dev);
  155. if (mci)
  156. edac_mc_free(mci);
  157. return 0;
  158. }
  159. static struct platform_driver tile_edac_mc_driver = {
  160. .driver = {
  161. .name = DRV_NAME,
  162. .owner = THIS_MODULE,
  163. },
  164. .probe = tile_edac_mc_probe,
  165. .remove = __devexit_p(tile_edac_mc_remove),
  166. };
  167. /*
  168. * Driver init routine.
  169. */
  170. static int __init tile_edac_init(void)
  171. {
  172. char hv_file[32];
  173. struct platform_device *pdev;
  174. int i, err, num = 0;
  175. /* Only support POLL mode. */
  176. edac_op_state = EDAC_OPSTATE_POLL;
  177. err = platform_driver_register(&tile_edac_mc_driver);
  178. if (err)
  179. return err;
  180. for (i = 0; i < TILE_MAX_MSHIMS; i++) {
  181. /*
  182. * Not all memory controllers are configured such as in the
  183. * case of a simulator. So we register only those mshims
  184. * that are configured by the hypervisor.
  185. */
  186. sprintf(hv_file, "mshim/%d", i);
  187. if (hv_dev_open((HV_VirtAddr)hv_file, 0) < 0)
  188. continue;
  189. pdev = platform_device_register_simple(DRV_NAME, i, NULL, 0);
  190. if (IS_ERR(pdev))
  191. continue;
  192. mshim_pdev[i] = pdev;
  193. num++;
  194. }
  195. if (num == 0) {
  196. platform_driver_unregister(&tile_edac_mc_driver);
  197. return -ENODEV;
  198. }
  199. return 0;
  200. }
  201. /*
  202. * Driver cleanup routine.
  203. */
  204. static void __exit tile_edac_exit(void)
  205. {
  206. int i;
  207. for (i = 0; i < TILE_MAX_MSHIMS; i++) {
  208. struct platform_device *pdev = mshim_pdev[i];
  209. if (!pdev)
  210. continue;
  211. platform_set_drvdata(pdev, NULL);
  212. platform_device_unregister(pdev);
  213. }
  214. platform_driver_unregister(&tile_edac_mc_driver);
  215. }
  216. module_init(tile_edac_init);
  217. module_exit(tile_edac_exit);