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