tile_edac.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. mci->ctl_name = "TILEPro_Memory_Controller";
  126. mci->dev_name = dev_name(&pdev->dev);
  127. mci->edac_check = tile_edac_check;
  128. /*
  129. * Initialize the MC control structure 'csrows' table
  130. * with the mapping and control information.
  131. */
  132. if (tile_edac_init_csrows(mci)) {
  133. /* No csrows found. */
  134. mci->edac_cap = EDAC_FLAG_NONE;
  135. } else {
  136. mci->edac_cap = EDAC_FLAG_SECDED;
  137. }
  138. platform_set_drvdata(pdev, mci);
  139. /* Register with EDAC core */
  140. rc = edac_mc_add_mc(mci);
  141. if (rc) {
  142. dev_err(&pdev->dev, "failed to register with EDAC core\n");
  143. edac_mc_free(mci);
  144. return rc;
  145. }
  146. return 0;
  147. }
  148. static int __devexit tile_edac_mc_remove(struct platform_device *pdev)
  149. {
  150. struct mem_ctl_info *mci = platform_get_drvdata(pdev);
  151. edac_mc_del_mc(&pdev->dev);
  152. if (mci)
  153. edac_mc_free(mci);
  154. return 0;
  155. }
  156. static struct platform_driver tile_edac_mc_driver = {
  157. .driver = {
  158. .name = DRV_NAME,
  159. .owner = THIS_MODULE,
  160. },
  161. .probe = tile_edac_mc_probe,
  162. .remove = __devexit_p(tile_edac_mc_remove),
  163. };
  164. /*
  165. * Driver init routine.
  166. */
  167. static int __init tile_edac_init(void)
  168. {
  169. char hv_file[32];
  170. struct platform_device *pdev;
  171. int i, err, num = 0;
  172. /* Only support POLL mode. */
  173. edac_op_state = EDAC_OPSTATE_POLL;
  174. err = platform_driver_register(&tile_edac_mc_driver);
  175. if (err)
  176. return err;
  177. for (i = 0; i < TILE_MAX_MSHIMS; i++) {
  178. /*
  179. * Not all memory controllers are configured such as in the
  180. * case of a simulator. So we register only those mshims
  181. * that are configured by the hypervisor.
  182. */
  183. sprintf(hv_file, "mshim/%d", i);
  184. if (hv_dev_open((HV_VirtAddr)hv_file, 0) < 0)
  185. continue;
  186. pdev = platform_device_register_simple(DRV_NAME, i, NULL, 0);
  187. if (IS_ERR(pdev))
  188. continue;
  189. mshim_pdev[i] = pdev;
  190. num++;
  191. }
  192. if (num == 0) {
  193. platform_driver_unregister(&tile_edac_mc_driver);
  194. return -ENODEV;
  195. }
  196. return 0;
  197. }
  198. /*
  199. * Driver cleanup routine.
  200. */
  201. static void __exit tile_edac_exit(void)
  202. {
  203. int i;
  204. for (i = 0; i < TILE_MAX_MSHIMS; i++) {
  205. struct platform_device *pdev = mshim_pdev[i];
  206. if (!pdev)
  207. continue;
  208. platform_set_drvdata(pdev, NULL);
  209. platform_device_unregister(pdev);
  210. }
  211. platform_driver_unregister(&tile_edac_mc_driver);
  212. }
  213. module_init(tile_edac_init);
  214. module_exit(tile_edac_exit);