amd76x_edac.c 8.3 KB

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