amd76x_edac.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * AMD 76x Memory Controller kernel module
  3. * (C) 2003 Linux Networx (http://lnxi.com)
  4. * This file may be distributed under the terms of the
  5. * GNU General Public License.
  6. *
  7. * Written by Thayne Harbaugh
  8. * Based on work by Dan Hollis <goemon at anime dot net> and others.
  9. * http://www.anime.net/~goemon/linux-ecc/
  10. *
  11. * $Id: edac_amd76x.c,v 1.4.2.5 2005/10/05 00:43:44 dsp_llnl Exp $
  12. *
  13. */
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/pci.h>
  17. #include <linux/pci_ids.h>
  18. #include <linux/slab.h>
  19. #include <linux/edac.h>
  20. #include "edac_core.h"
  21. #define AMD76X_REVISION " Ver: 2.0.2 " __DATE__
  22. #define EDAC_MOD_STR "amd76x_edac"
  23. #define amd76x_printk(level, fmt, arg...) \
  24. edac_printk(level, "amd76x", fmt, ##arg)
  25. #define amd76x_mc_printk(mci, level, fmt, arg...) \
  26. edac_mc_chipset_printk(mci, level, "amd76x", fmt, ##arg)
  27. #define AMD76X_NR_CSROWS 8
  28. #define AMD76X_NR_CHANS 1
  29. #define AMD76X_NR_DIMMS 4
  30. /* AMD 76x register addresses - device 0 function 0 - PCI bridge */
  31. #define AMD76X_ECC_MODE_STATUS 0x48 /* Mode and status of ECC (32b)
  32. *
  33. * 31:16 reserved
  34. * 15:14 SERR enabled: x1=ue 1x=ce
  35. * 13 reserved
  36. * 12 diag: disabled, enabled
  37. * 11:10 mode: dis, EC, ECC, ECC+scrub
  38. * 9:8 status: x1=ue 1x=ce
  39. * 7:4 UE cs row
  40. * 3:0 CE cs row
  41. */
  42. #define AMD76X_DRAM_MODE_STATUS 0x58 /* DRAM Mode and status (32b)
  43. *
  44. * 31:26 clock disable 5 - 0
  45. * 25 SDRAM init
  46. * 24 reserved
  47. * 23 mode register service
  48. * 22:21 suspend to RAM
  49. * 20 burst refresh enable
  50. * 19 refresh disable
  51. * 18 reserved
  52. * 17:16 cycles-per-refresh
  53. * 15:8 reserved
  54. * 7:0 x4 mode enable 7 - 0
  55. */
  56. #define AMD76X_MEM_BASE_ADDR 0xC0 /* Memory base address (8 x 32b)
  57. *
  58. * 31:23 chip-select base
  59. * 22:16 reserved
  60. * 15:7 chip-select mask
  61. * 6:3 reserved
  62. * 2:1 address mode
  63. * 0 chip-select enable
  64. */
  65. struct amd76x_error_info {
  66. u32 ecc_mode_status;
  67. };
  68. enum amd76x_chips {
  69. AMD761 = 0,
  70. AMD762
  71. };
  72. struct amd76x_dev_info {
  73. const char *ctl_name;
  74. };
  75. static const struct amd76x_dev_info amd76x_devs[] = {
  76. [AMD761] = {
  77. .ctl_name = "AMD761"},
  78. [AMD762] = {
  79. .ctl_name = "AMD762"},
  80. };
  81. static struct edac_pci_ctl_info *amd76x_pci;
  82. /**
  83. * amd76x_get_error_info - fetch error information
  84. * @mci: Memory controller
  85. * @info: Info to fill in
  86. *
  87. * Fetch and store the AMD76x ECC status. Clear pending status
  88. * on the chip so that further errors will be reported
  89. */
  90. static void amd76x_get_error_info(struct mem_ctl_info *mci,
  91. struct amd76x_error_info *info)
  92. {
  93. struct pci_dev *pdev;
  94. pdev = to_pci_dev(mci->dev);
  95. pci_read_config_dword(pdev, AMD76X_ECC_MODE_STATUS,
  96. &info->ecc_mode_status);
  97. if (info->ecc_mode_status & BIT(8))
  98. pci_write_bits32(pdev, AMD76X_ECC_MODE_STATUS,
  99. (u32) BIT(8), (u32) BIT(8));
  100. if (info->ecc_mode_status & BIT(9))
  101. pci_write_bits32(pdev, AMD76X_ECC_MODE_STATUS,
  102. (u32) BIT(9), (u32) BIT(9));
  103. }
  104. /**
  105. * amd76x_process_error_info - Error check
  106. * @mci: Memory controller
  107. * @info: Previously fetched information from chip
  108. * @handle_errors: 1 if we should do recovery
  109. *
  110. * Process the chip state and decide if an error has occurred.
  111. * A return of 1 indicates an error. Also if handle_errors is true
  112. * then attempt to handle and clean up after the error
  113. */
  114. static int amd76x_process_error_info(struct mem_ctl_info *mci,
  115. struct amd76x_error_info *info,
  116. int handle_errors)
  117. {
  118. int error_found;
  119. u32 row;
  120. error_found = 0;
  121. /*
  122. * Check for an uncorrectable error
  123. */
  124. if (info->ecc_mode_status & BIT(8)) {
  125. error_found = 1;
  126. if (handle_errors) {
  127. row = (info->ecc_mode_status >> 4) & 0xf;
  128. edac_mc_handle_ue(mci, mci->csrows[row].first_page, 0,
  129. row, mci->ctl_name);
  130. }
  131. }
  132. /*
  133. * Check for a correctable error
  134. */
  135. if (info->ecc_mode_status & BIT(9)) {
  136. error_found = 1;
  137. if (handle_errors) {
  138. row = info->ecc_mode_status & 0xf;
  139. edac_mc_handle_ce(mci, mci->csrows[row].first_page, 0,
  140. 0, row, 0, mci->ctl_name);
  141. }
  142. }
  143. return error_found;
  144. }
  145. /**
  146. * amd76x_check - Poll the controller
  147. * @mci: Memory controller
  148. *
  149. * Called by the poll handlers this function reads the status
  150. * from the controller and checks for errors.
  151. */
  152. static void amd76x_check(struct mem_ctl_info *mci)
  153. {
  154. struct amd76x_error_info info;
  155. debugf3("%s()\n", __func__);
  156. amd76x_get_error_info(mci, &info);
  157. amd76x_process_error_info(mci, &info, 1);
  158. }
  159. static void amd76x_init_csrows(struct mem_ctl_info *mci, struct pci_dev *pdev,
  160. enum edac_type edac_mode)
  161. {
  162. struct csrow_info *csrow;
  163. u32 mba, mba_base, mba_mask, dms;
  164. int index;
  165. for (index = 0; index < mci->nr_csrows; index++) {
  166. csrow = &mci->csrows[index];
  167. /* find the DRAM Chip Select Base address and mask */
  168. pci_read_config_dword(pdev,
  169. AMD76X_MEM_BASE_ADDR + (index * 4), &mba);
  170. if (!(mba & BIT(0)))
  171. continue;
  172. mba_base = mba & 0xff800000UL;
  173. mba_mask = ((mba & 0xff80) << 16) | 0x7fffffUL;
  174. pci_read_config_dword(pdev, AMD76X_DRAM_MODE_STATUS, &dms);
  175. csrow->first_page = mba_base >> PAGE_SHIFT;
  176. csrow->nr_pages = (mba_mask + 1) >> PAGE_SHIFT;
  177. csrow->last_page = csrow->first_page + csrow->nr_pages - 1;
  178. csrow->page_mask = mba_mask >> PAGE_SHIFT;
  179. csrow->grain = csrow->nr_pages << PAGE_SHIFT;
  180. csrow->mtype = MEM_RDDR;
  181. csrow->dtype = ((dms >> index) & 0x1) ? DEV_X4 : DEV_UNKNOWN;
  182. csrow->edac_mode = edac_mode;
  183. }
  184. }
  185. /**
  186. * amd76x_probe1 - Perform set up for detected device
  187. * @pdev; PCI device detected
  188. * @dev_idx: Device type index
  189. *
  190. * We have found an AMD76x and now need to set up the memory
  191. * controller status reporting. We configure and set up the
  192. * memory controller reporting and claim the device.
  193. */
  194. static int amd76x_probe1(struct pci_dev *pdev, int dev_idx)
  195. {
  196. static const enum edac_type ems_modes[] = {
  197. EDAC_NONE,
  198. EDAC_EC,
  199. EDAC_SECDED,
  200. EDAC_SECDED
  201. };
  202. struct mem_ctl_info *mci = NULL;
  203. u32 ems;
  204. u32 ems_mode;
  205. struct amd76x_error_info discard;
  206. debugf0("%s()\n", __func__);
  207. pci_read_config_dword(pdev, AMD76X_ECC_MODE_STATUS, &ems);
  208. ems_mode = (ems >> 10) & 0x3;
  209. mci = edac_mc_alloc(0, AMD76X_NR_CSROWS, AMD76X_NR_CHANS, 0);
  210. if (mci == NULL) {
  211. return -ENOMEM;
  212. }
  213. debugf0("%s(): mci = %p\n", __func__, mci);
  214. mci->dev = &pdev->dev;
  215. mci->mtype_cap = MEM_FLAG_RDDR;
  216. mci->edac_ctl_cap = EDAC_FLAG_NONE | EDAC_FLAG_EC | EDAC_FLAG_SECDED;
  217. mci->edac_cap = ems_mode ?
  218. (EDAC_FLAG_EC | EDAC_FLAG_SECDED) : EDAC_FLAG_NONE;
  219. mci->mod_name = EDAC_MOD_STR;
  220. mci->mod_ver = AMD76X_REVISION;
  221. mci->ctl_name = amd76x_devs[dev_idx].ctl_name;
  222. mci->dev_name = pci_name(pdev);
  223. mci->edac_check = amd76x_check;
  224. mci->ctl_page_to_phys = NULL;
  225. amd76x_init_csrows(mci, pdev, ems_modes[ems_mode]);
  226. amd76x_get_error_info(mci, &discard); /* clear counters */
  227. /* Here we assume that we will never see multiple instances of this
  228. * type of memory controller. The ID is therefore hardcoded to 0.
  229. */
  230. if (edac_mc_add_mc(mci)) {
  231. debugf3("%s(): failed edac_mc_add_mc()\n", __func__);
  232. goto fail;
  233. }
  234. /* allocating generic PCI control info */
  235. amd76x_pci = edac_pci_create_generic_ctl(&pdev->dev, EDAC_MOD_STR);
  236. if (!amd76x_pci) {
  237. printk(KERN_WARNING
  238. "%s(): Unable to create PCI control\n",
  239. __func__);
  240. printk(KERN_WARNING
  241. "%s(): PCI error report via EDAC not setup\n",
  242. __func__);
  243. }
  244. /* get this far and it's successful */
  245. debugf3("%s(): success\n", __func__);
  246. return 0;
  247. fail:
  248. edac_mc_free(mci);
  249. return -ENODEV;
  250. }
  251. /* returns count (>= 0), or negative on error */
  252. static int __devinit amd76x_init_one(struct pci_dev *pdev,
  253. const struct pci_device_id *ent)
  254. {
  255. debugf0("%s()\n", __func__);
  256. /* don't need to call pci_device_enable() */
  257. return amd76x_probe1(pdev, ent->driver_data);
  258. }
  259. /**
  260. * amd76x_remove_one - driver shutdown
  261. * @pdev: PCI device being handed back
  262. *
  263. * Called when the driver is unloaded. Find the matching mci
  264. * structure for the device then delete the mci and free the
  265. * resources.
  266. */
  267. static void __devexit amd76x_remove_one(struct pci_dev *pdev)
  268. {
  269. struct mem_ctl_info *mci;
  270. debugf0("%s()\n", __func__);
  271. if (amd76x_pci)
  272. edac_pci_release_generic_ctl(amd76x_pci);
  273. if ((mci = edac_mc_del_mc(&pdev->dev)) == NULL)
  274. return;
  275. edac_mc_free(mci);
  276. }
  277. static const struct pci_device_id amd76x_pci_tbl[] __devinitdata = {
  278. {
  279. PCI_VEND_DEV(AMD, FE_GATE_700C), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  280. AMD762},
  281. {
  282. PCI_VEND_DEV(AMD, FE_GATE_700E), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  283. AMD761},
  284. {
  285. 0,
  286. } /* 0 terminated list. */
  287. };
  288. MODULE_DEVICE_TABLE(pci, amd76x_pci_tbl);
  289. static struct pci_driver amd76x_driver = {
  290. .name = EDAC_MOD_STR,
  291. .probe = amd76x_init_one,
  292. .remove = __devexit_p(amd76x_remove_one),
  293. .id_table = amd76x_pci_tbl,
  294. };
  295. static int __init amd76x_init(void)
  296. {
  297. /* Ensure that the OPSTATE is set correctly for POLL or NMI */
  298. opstate_init();
  299. return pci_register_driver(&amd76x_driver);
  300. }
  301. static void __exit amd76x_exit(void)
  302. {
  303. pci_unregister_driver(&amd76x_driver);
  304. }
  305. module_init(amd76x_init);
  306. module_exit(amd76x_exit);
  307. MODULE_LICENSE("GPL");
  308. MODULE_AUTHOR("Linux Networx (http://lnxi.com) Thayne Harbaugh");
  309. MODULE_DESCRIPTION("MC support for AMD 76x memory controllers");
  310. module_param(edac_op_state, int, 0444);
  311. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");