amd8131_edac.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * amd8131_edac.c, AMD8131 hypertransport chip EDAC kernel module
  3. *
  4. * Copyright (c) 2008 Wind River Systems, Inc.
  5. *
  6. * Authors: Cao Qingtao <qingtao.cao@windriver.com>
  7. * Benjamin Walsh <benjamin.walsh@windriver.com>
  8. * Hu Yongqi <yongqi.hu@windriver.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. * See the GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/io.h>
  27. #include <linux/bitops.h>
  28. #include <linux/edac.h>
  29. #include <linux/pci_ids.h>
  30. #include "edac_core.h"
  31. #include "edac_module.h"
  32. #include "amd8131_edac.h"
  33. #define AMD8131_EDAC_REVISION " Ver: 1.0.0"
  34. #define AMD8131_EDAC_MOD_STR "amd8131_edac"
  35. /* Wrapper functions for accessing PCI configuration space */
  36. static void edac_pci_read_dword(struct pci_dev *dev, int reg, u32 *val32)
  37. {
  38. int ret;
  39. ret = pci_read_config_dword(dev, reg, val32);
  40. if (ret != 0)
  41. printk(KERN_ERR AMD8131_EDAC_MOD_STR
  42. " PCI Access Read Error at 0x%x\n", reg);
  43. }
  44. static void edac_pci_write_dword(struct pci_dev *dev, int reg, u32 val32)
  45. {
  46. int ret;
  47. ret = pci_write_config_dword(dev, reg, val32);
  48. if (ret != 0)
  49. printk(KERN_ERR AMD8131_EDAC_MOD_STR
  50. " PCI Access Write Error at 0x%x\n", reg);
  51. }
  52. static char * const bridge_str[] = {
  53. [NORTH_A] = "NORTH A",
  54. [NORTH_B] = "NORTH B",
  55. [SOUTH_A] = "SOUTH A",
  56. [SOUTH_B] = "SOUTH B",
  57. [NO_BRIDGE] = "NO BRIDGE",
  58. };
  59. /* Support up to two AMD8131 chipsets on a platform */
  60. static struct amd8131_dev_info amd8131_devices[] = {
  61. {
  62. .inst = NORTH_A,
  63. .devfn = DEVFN_PCIX_BRIDGE_NORTH_A,
  64. .ctl_name = "AMD8131_PCIX_NORTH_A",
  65. },
  66. {
  67. .inst = NORTH_B,
  68. .devfn = DEVFN_PCIX_BRIDGE_NORTH_B,
  69. .ctl_name = "AMD8131_PCIX_NORTH_B",
  70. },
  71. {
  72. .inst = SOUTH_A,
  73. .devfn = DEVFN_PCIX_BRIDGE_SOUTH_A,
  74. .ctl_name = "AMD8131_PCIX_SOUTH_A",
  75. },
  76. {
  77. .inst = SOUTH_B,
  78. .devfn = DEVFN_PCIX_BRIDGE_SOUTH_B,
  79. .ctl_name = "AMD8131_PCIX_SOUTH_B",
  80. },
  81. {.inst = NO_BRIDGE,},
  82. };
  83. static void amd8131_pcix_init(struct amd8131_dev_info *dev_info)
  84. {
  85. u32 val32;
  86. struct pci_dev *dev = dev_info->dev;
  87. /* First clear error detection flags */
  88. edac_pci_read_dword(dev, REG_MEM_LIM, &val32);
  89. if (val32 & MEM_LIMIT_MASK)
  90. edac_pci_write_dword(dev, REG_MEM_LIM, val32);
  91. /* Clear Discard Timer Timedout flag */
  92. edac_pci_read_dword(dev, REG_INT_CTLR, &val32);
  93. if (val32 & INT_CTLR_DTS)
  94. edac_pci_write_dword(dev, REG_INT_CTLR, val32);
  95. /* Clear CRC Error flag on link side A */
  96. edac_pci_read_dword(dev, REG_LNK_CTRL_A, &val32);
  97. if (val32 & LNK_CTRL_CRCERR_A)
  98. edac_pci_write_dword(dev, REG_LNK_CTRL_A, val32);
  99. /* Clear CRC Error flag on link side B */
  100. edac_pci_read_dword(dev, REG_LNK_CTRL_B, &val32);
  101. if (val32 & LNK_CTRL_CRCERR_B)
  102. edac_pci_write_dword(dev, REG_LNK_CTRL_B, val32);
  103. /*
  104. * Then enable all error detections.
  105. *
  106. * Setup Discard Timer Sync Flood Enable,
  107. * System Error Enable and Parity Error Enable.
  108. */
  109. edac_pci_read_dword(dev, REG_INT_CTLR, &val32);
  110. val32 |= INT_CTLR_PERR | INT_CTLR_SERR | INT_CTLR_DTSE;
  111. edac_pci_write_dword(dev, REG_INT_CTLR, val32);
  112. /* Enable overall SERR Error detection */
  113. edac_pci_read_dword(dev, REG_STS_CMD, &val32);
  114. val32 |= STS_CMD_SERREN;
  115. edac_pci_write_dword(dev, REG_STS_CMD, val32);
  116. /* Setup CRC Flood Enable for link side A */
  117. edac_pci_read_dword(dev, REG_LNK_CTRL_A, &val32);
  118. val32 |= LNK_CTRL_CRCFEN;
  119. edac_pci_write_dword(dev, REG_LNK_CTRL_A, val32);
  120. /* Setup CRC Flood Enable for link side B */
  121. edac_pci_read_dword(dev, REG_LNK_CTRL_B, &val32);
  122. val32 |= LNK_CTRL_CRCFEN;
  123. edac_pci_write_dword(dev, REG_LNK_CTRL_B, val32);
  124. }
  125. static void amd8131_pcix_exit(struct amd8131_dev_info *dev_info)
  126. {
  127. u32 val32;
  128. struct pci_dev *dev = dev_info->dev;
  129. /* Disable SERR, PERR and DTSE Error detection */
  130. edac_pci_read_dword(dev, REG_INT_CTLR, &val32);
  131. val32 &= ~(INT_CTLR_PERR | INT_CTLR_SERR | INT_CTLR_DTSE);
  132. edac_pci_write_dword(dev, REG_INT_CTLR, val32);
  133. /* Disable overall System Error detection */
  134. edac_pci_read_dword(dev, REG_STS_CMD, &val32);
  135. val32 &= ~STS_CMD_SERREN;
  136. edac_pci_write_dword(dev, REG_STS_CMD, val32);
  137. /* Disable CRC Sync Flood on link side A */
  138. edac_pci_read_dword(dev, REG_LNK_CTRL_A, &val32);
  139. val32 &= ~LNK_CTRL_CRCFEN;
  140. edac_pci_write_dword(dev, REG_LNK_CTRL_A, val32);
  141. /* Disable CRC Sync Flood on link side B */
  142. edac_pci_read_dword(dev, REG_LNK_CTRL_B, &val32);
  143. val32 &= ~LNK_CTRL_CRCFEN;
  144. edac_pci_write_dword(dev, REG_LNK_CTRL_B, val32);
  145. }
  146. static void amd8131_pcix_check(struct edac_pci_ctl_info *edac_dev)
  147. {
  148. struct amd8131_dev_info *dev_info = edac_dev->pvt_info;
  149. struct pci_dev *dev = dev_info->dev;
  150. u32 val32;
  151. /* Check PCI-X Bridge Memory Base-Limit Register for errors */
  152. edac_pci_read_dword(dev, REG_MEM_LIM, &val32);
  153. if (val32 & MEM_LIMIT_MASK) {
  154. printk(KERN_INFO "Error(s) in mem limit register "
  155. "on %s bridge\n", dev_info->ctl_name);
  156. printk(KERN_INFO "DPE: %d, RSE: %d, RMA: %d\n"
  157. "RTA: %d, STA: %d, MDPE: %d\n",
  158. val32 & MEM_LIMIT_DPE,
  159. val32 & MEM_LIMIT_RSE,
  160. val32 & MEM_LIMIT_RMA,
  161. val32 & MEM_LIMIT_RTA,
  162. val32 & MEM_LIMIT_STA,
  163. val32 & MEM_LIMIT_MDPE);
  164. val32 |= MEM_LIMIT_MASK;
  165. edac_pci_write_dword(dev, REG_MEM_LIM, val32);
  166. edac_pci_handle_npe(edac_dev, edac_dev->ctl_name);
  167. }
  168. /* Check if Discard Timer timed out */
  169. edac_pci_read_dword(dev, REG_INT_CTLR, &val32);
  170. if (val32 & INT_CTLR_DTS) {
  171. printk(KERN_INFO "Error(s) in interrupt and control register "
  172. "on %s bridge\n", dev_info->ctl_name);
  173. printk(KERN_INFO "DTS: %d\n", val32 & INT_CTLR_DTS);
  174. val32 |= INT_CTLR_DTS;
  175. edac_pci_write_dword(dev, REG_INT_CTLR, val32);
  176. edac_pci_handle_npe(edac_dev, edac_dev->ctl_name);
  177. }
  178. /* Check if CRC error happens on link side A */
  179. edac_pci_read_dword(dev, REG_LNK_CTRL_A, &val32);
  180. if (val32 & LNK_CTRL_CRCERR_A) {
  181. printk(KERN_INFO "Error(s) in link conf and control register "
  182. "on %s bridge\n", dev_info->ctl_name);
  183. printk(KERN_INFO "CRCERR: %d\n", val32 & LNK_CTRL_CRCERR_A);
  184. val32 |= LNK_CTRL_CRCERR_A;
  185. edac_pci_write_dword(dev, REG_LNK_CTRL_A, val32);
  186. edac_pci_handle_npe(edac_dev, edac_dev->ctl_name);
  187. }
  188. /* Check if CRC error happens on link side B */
  189. edac_pci_read_dword(dev, REG_LNK_CTRL_B, &val32);
  190. if (val32 & LNK_CTRL_CRCERR_B) {
  191. printk(KERN_INFO "Error(s) in link conf and control register "
  192. "on %s bridge\n", dev_info->ctl_name);
  193. printk(KERN_INFO "CRCERR: %d\n", val32 & LNK_CTRL_CRCERR_B);
  194. val32 |= LNK_CTRL_CRCERR_B;
  195. edac_pci_write_dword(dev, REG_LNK_CTRL_B, val32);
  196. edac_pci_handle_npe(edac_dev, edac_dev->ctl_name);
  197. }
  198. }
  199. static struct amd8131_info amd8131_chipset = {
  200. .err_dev = PCI_DEVICE_ID_AMD_8131_APIC,
  201. .devices = amd8131_devices,
  202. .init = amd8131_pcix_init,
  203. .exit = amd8131_pcix_exit,
  204. .check = amd8131_pcix_check,
  205. };
  206. /*
  207. * There are 4 PCIX Bridges on ATCA-6101 that share the same PCI Device ID,
  208. * so amd8131_probe() would be called by kernel 4 times, with different
  209. * address of pci_dev for each of them each time.
  210. */
  211. static int amd8131_probe(struct pci_dev *dev, const struct pci_device_id *id)
  212. {
  213. struct amd8131_dev_info *dev_info;
  214. for (dev_info = amd8131_chipset.devices; dev_info->inst != NO_BRIDGE;
  215. dev_info++)
  216. if (dev_info->devfn == dev->devfn)
  217. break;
  218. if (dev_info->inst == NO_BRIDGE) /* should never happen */
  219. return -ENODEV;
  220. /*
  221. * We can't call pci_get_device() as we are used to do because
  222. * there are 4 of them but pci_dev_get() instead.
  223. */
  224. dev_info->dev = pci_dev_get(dev);
  225. if (pci_enable_device(dev_info->dev)) {
  226. pci_dev_put(dev_info->dev);
  227. printk(KERN_ERR "failed to enable:"
  228. "vendor %x, device %x, devfn %x, name %s\n",
  229. PCI_VENDOR_ID_AMD, amd8131_chipset.err_dev,
  230. dev_info->devfn, dev_info->ctl_name);
  231. return -ENODEV;
  232. }
  233. /*
  234. * we do not allocate extra private structure for
  235. * edac_pci_ctl_info, but make use of existing
  236. * one instead.
  237. */
  238. dev_info->edac_idx = edac_pci_alloc_index();
  239. dev_info->edac_dev = edac_pci_alloc_ctl_info(0, dev_info->ctl_name);
  240. if (!dev_info->edac_dev)
  241. return -ENOMEM;
  242. dev_info->edac_dev->pvt_info = dev_info;
  243. dev_info->edac_dev->dev = &dev_info->dev->dev;
  244. dev_info->edac_dev->mod_name = AMD8131_EDAC_MOD_STR;
  245. dev_info->edac_dev->ctl_name = dev_info->ctl_name;
  246. dev_info->edac_dev->dev_name = dev_name(&dev_info->dev->dev);
  247. if (edac_op_state == EDAC_OPSTATE_POLL)
  248. dev_info->edac_dev->edac_check = amd8131_chipset.check;
  249. if (amd8131_chipset.init)
  250. amd8131_chipset.init(dev_info);
  251. if (edac_pci_add_device(dev_info->edac_dev, dev_info->edac_idx) > 0) {
  252. printk(KERN_ERR "failed edac_pci_add_device() for %s\n",
  253. dev_info->ctl_name);
  254. edac_pci_free_ctl_info(dev_info->edac_dev);
  255. return -ENODEV;
  256. }
  257. printk(KERN_INFO "added one device on AMD8131 "
  258. "vendor %x, device %x, devfn %x, name %s\n",
  259. PCI_VENDOR_ID_AMD, amd8131_chipset.err_dev,
  260. dev_info->devfn, dev_info->ctl_name);
  261. return 0;
  262. }
  263. static void amd8131_remove(struct pci_dev *dev)
  264. {
  265. struct amd8131_dev_info *dev_info;
  266. for (dev_info = amd8131_chipset.devices; dev_info->inst != NO_BRIDGE;
  267. dev_info++)
  268. if (dev_info->devfn == dev->devfn)
  269. break;
  270. if (dev_info->inst == NO_BRIDGE) /* should never happen */
  271. return;
  272. if (dev_info->edac_dev) {
  273. edac_pci_del_device(dev_info->edac_dev->dev);
  274. edac_pci_free_ctl_info(dev_info->edac_dev);
  275. }
  276. if (amd8131_chipset.exit)
  277. amd8131_chipset.exit(dev_info);
  278. pci_dev_put(dev_info->dev);
  279. }
  280. static const struct pci_device_id amd8131_edac_pci_tbl[] = {
  281. {
  282. PCI_VEND_DEV(AMD, 8131_BRIDGE),
  283. .subvendor = PCI_ANY_ID,
  284. .subdevice = PCI_ANY_ID,
  285. .class = 0,
  286. .class_mask = 0,
  287. .driver_data = 0,
  288. },
  289. {
  290. 0,
  291. } /* table is NULL-terminated */
  292. };
  293. MODULE_DEVICE_TABLE(pci, amd8131_edac_pci_tbl);
  294. static struct pci_driver amd8131_edac_driver = {
  295. .name = AMD8131_EDAC_MOD_STR,
  296. .probe = amd8131_probe,
  297. .remove = amd8131_remove,
  298. .id_table = amd8131_edac_pci_tbl,
  299. };
  300. static int __init amd8131_edac_init(void)
  301. {
  302. printk(KERN_INFO "AMD8131 EDAC driver " AMD8131_EDAC_REVISION "\n");
  303. printk(KERN_INFO "\t(c) 2008 Wind River Systems, Inc.\n");
  304. /* Only POLL mode supported so far */
  305. edac_op_state = EDAC_OPSTATE_POLL;
  306. return pci_register_driver(&amd8131_edac_driver);
  307. }
  308. static void __exit amd8131_edac_exit(void)
  309. {
  310. pci_unregister_driver(&amd8131_edac_driver);
  311. }
  312. module_init(amd8131_edac_init);
  313. module_exit(amd8131_edac_exit);
  314. MODULE_LICENSE("GPL");
  315. MODULE_AUTHOR("Cao Qingtao <qingtao.cao@windriver.com>\n");
  316. MODULE_DESCRIPTION("AMD8131 HyperTransport PCI-X Tunnel EDAC kernel module");