tile_edac.c 6.3 KB

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