amd76x_edac.c 8.3 KB

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