amd76x_edac.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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/edac.h>
  19. #include "edac_core.h"
  20. #define AMD76X_REVISION " Ver: 2.0.2"
  21. #define EDAC_MOD_STR "amd76x_edac"
  22. #define amd76x_printk(level, fmt, arg...) \
  23. edac_printk(level, "amd76x", fmt, ##arg)
  24. #define amd76x_mc_printk(mci, level, fmt, arg...) \
  25. edac_mc_chipset_printk(mci, level, "amd76x", fmt, ##arg)
  26. #define AMD76X_NR_CSROWS 8
  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] = {
  75. .ctl_name = "AMD761"},
  76. [AMD762] = {
  77. .ctl_name = "AMD762"},
  78. };
  79. static struct edac_pci_ctl_info *amd76x_pci;
  80. /**
  81. * amd76x_get_error_info - fetch error information
  82. * @mci: Memory controller
  83. * @info: Info to fill in
  84. *
  85. * Fetch and store the AMD76x ECC status. Clear pending status
  86. * on the chip so that further errors will be reported
  87. */
  88. static void amd76x_get_error_info(struct mem_ctl_info *mci,
  89. struct amd76x_error_info *info)
  90. {
  91. struct pci_dev *pdev;
  92. pdev = to_pci_dev(mci->pdev);
  93. pci_read_config_dword(pdev, AMD76X_ECC_MODE_STATUS,
  94. &info->ecc_mode_status);
  95. if (info->ecc_mode_status & BIT(8))
  96. pci_write_bits32(pdev, AMD76X_ECC_MODE_STATUS,
  97. (u32) BIT(8), (u32) BIT(8));
  98. if (info->ecc_mode_status & BIT(9))
  99. pci_write_bits32(pdev, AMD76X_ECC_MODE_STATUS,
  100. (u32) BIT(9), (u32) BIT(9));
  101. }
  102. /**
  103. * amd76x_process_error_info - Error check
  104. * @mci: Memory controller
  105. * @info: Previously fetched information from chip
  106. * @handle_errors: 1 if we should do recovery
  107. *
  108. * Process the chip state and decide if an error has occurred.
  109. * A return of 1 indicates an error. Also if handle_errors is true
  110. * then attempt to handle and clean up after the error
  111. */
  112. static int amd76x_process_error_info(struct mem_ctl_info *mci,
  113. struct amd76x_error_info *info,
  114. int handle_errors)
  115. {
  116. int error_found;
  117. u32 row;
  118. error_found = 0;
  119. /*
  120. * Check for an uncorrectable error
  121. */
  122. if (info->ecc_mode_status & BIT(8)) {
  123. error_found = 1;
  124. if (handle_errors) {
  125. row = (info->ecc_mode_status >> 4) & 0xf;
  126. edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci, 1,
  127. mci->csrows[row]->first_page, 0, 0,
  128. row, 0, -1,
  129. 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_error(HW_EVENT_ERR_CORRECTED, mci, 1,
  140. mci->csrows[row]->first_page, 0, 0,
  141. row, 0, -1,
  142. mci->ctl_name, "");
  143. }
  144. }
  145. return error_found;
  146. }
  147. /**
  148. * amd76x_check - Poll the controller
  149. * @mci: Memory controller
  150. *
  151. * Called by the poll handlers this function reads the status
  152. * from the controller and checks for errors.
  153. */
  154. static void amd76x_check(struct mem_ctl_info *mci)
  155. {
  156. struct amd76x_error_info info;
  157. edac_dbg(3, "\n");
  158. amd76x_get_error_info(mci, &info);
  159. amd76x_process_error_info(mci, &info, 1);
  160. }
  161. static void amd76x_init_csrows(struct mem_ctl_info *mci, struct pci_dev *pdev,
  162. enum edac_type edac_mode)
  163. {
  164. struct csrow_info *csrow;
  165. struct dimm_info *dimm;
  166. u32 mba, mba_base, mba_mask, dms;
  167. int index;
  168. for (index = 0; index < mci->nr_csrows; index++) {
  169. csrow = mci->csrows[index];
  170. dimm = csrow->channels[0]->dimm;
  171. /* find the DRAM Chip Select Base address and mask */
  172. pci_read_config_dword(pdev,
  173. AMD76X_MEM_BASE_ADDR + (index * 4), &mba);
  174. if (!(mba & BIT(0)))
  175. continue;
  176. mba_base = mba & 0xff800000UL;
  177. mba_mask = ((mba & 0xff80) << 16) | 0x7fffffUL;
  178. pci_read_config_dword(pdev, AMD76X_DRAM_MODE_STATUS, &dms);
  179. csrow->first_page = mba_base >> PAGE_SHIFT;
  180. dimm->nr_pages = (mba_mask + 1) >> PAGE_SHIFT;
  181. csrow->last_page = csrow->first_page + dimm->nr_pages - 1;
  182. csrow->page_mask = mba_mask >> PAGE_SHIFT;
  183. dimm->grain = dimm->nr_pages << PAGE_SHIFT;
  184. dimm->mtype = MEM_RDDR;
  185. dimm->dtype = ((dms >> index) & 0x1) ? DEV_X4 : DEV_UNKNOWN;
  186. dimm->edac_mode = edac_mode;
  187. }
  188. }
  189. /**
  190. * amd76x_probe1 - Perform set up for detected device
  191. * @pdev; PCI device detected
  192. * @dev_idx: Device type index
  193. *
  194. * We have found an AMD76x and now need to set up the memory
  195. * controller status reporting. We configure and set up the
  196. * memory controller reporting and claim the device.
  197. */
  198. static int amd76x_probe1(struct pci_dev *pdev, int dev_idx)
  199. {
  200. static const enum edac_type ems_modes[] = {
  201. EDAC_NONE,
  202. EDAC_EC,
  203. EDAC_SECDED,
  204. EDAC_SECDED
  205. };
  206. struct mem_ctl_info *mci;
  207. struct edac_mc_layer layers[2];
  208. u32 ems;
  209. u32 ems_mode;
  210. struct amd76x_error_info discard;
  211. edac_dbg(0, "\n");
  212. pci_read_config_dword(pdev, AMD76X_ECC_MODE_STATUS, &ems);
  213. ems_mode = (ems >> 10) & 0x3;
  214. layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
  215. layers[0].size = AMD76X_NR_CSROWS;
  216. layers[0].is_virt_csrow = true;
  217. layers[1].type = EDAC_MC_LAYER_CHANNEL;
  218. layers[1].size = 1;
  219. layers[1].is_virt_csrow = false;
  220. mci = edac_mc_alloc(0, ARRAY_SIZE(layers), layers, 0);
  221. if (mci == NULL)
  222. return -ENOMEM;
  223. edac_dbg(0, "mci = %p\n", mci);
  224. mci->pdev = &pdev->dev;
  225. mci->mtype_cap = MEM_FLAG_RDDR;
  226. mci->edac_ctl_cap = EDAC_FLAG_NONE | EDAC_FLAG_EC | EDAC_FLAG_SECDED;
  227. mci->edac_cap = ems_mode ?
  228. (EDAC_FLAG_EC | EDAC_FLAG_SECDED) : EDAC_FLAG_NONE;
  229. mci->mod_name = EDAC_MOD_STR;
  230. mci->mod_ver = AMD76X_REVISION;
  231. mci->ctl_name = amd76x_devs[dev_idx].ctl_name;
  232. mci->dev_name = pci_name(pdev);
  233. mci->edac_check = amd76x_check;
  234. mci->ctl_page_to_phys = NULL;
  235. amd76x_init_csrows(mci, pdev, ems_modes[ems_mode]);
  236. amd76x_get_error_info(mci, &discard); /* clear counters */
  237. /* Here we assume that we will never see multiple instances of this
  238. * type of memory controller. The ID is therefore hardcoded to 0.
  239. */
  240. if (edac_mc_add_mc(mci)) {
  241. edac_dbg(3, "failed edac_mc_add_mc()\n");
  242. goto fail;
  243. }
  244. /* allocating generic PCI control info */
  245. amd76x_pci = edac_pci_create_generic_ctl(&pdev->dev, EDAC_MOD_STR);
  246. if (!amd76x_pci) {
  247. printk(KERN_WARNING
  248. "%s(): Unable to create PCI control\n",
  249. __func__);
  250. printk(KERN_WARNING
  251. "%s(): PCI error report via EDAC not setup\n",
  252. __func__);
  253. }
  254. /* get this far and it's successful */
  255. edac_dbg(3, "success\n");
  256. return 0;
  257. fail:
  258. edac_mc_free(mci);
  259. return -ENODEV;
  260. }
  261. /* returns count (>= 0), or negative on error */
  262. static int amd76x_init_one(struct pci_dev *pdev,
  263. const struct pci_device_id *ent)
  264. {
  265. edac_dbg(0, "\n");
  266. /* don't need to call pci_enable_device() */
  267. return amd76x_probe1(pdev, ent->driver_data);
  268. }
  269. /**
  270. * amd76x_remove_one - driver shutdown
  271. * @pdev: PCI device being handed back
  272. *
  273. * Called when the driver is unloaded. Find the matching mci
  274. * structure for the device then delete the mci and free the
  275. * resources.
  276. */
  277. static void amd76x_remove_one(struct pci_dev *pdev)
  278. {
  279. struct mem_ctl_info *mci;
  280. edac_dbg(0, "\n");
  281. if (amd76x_pci)
  282. edac_pci_release_generic_ctl(amd76x_pci);
  283. if ((mci = edac_mc_del_mc(&pdev->dev)) == NULL)
  284. return;
  285. edac_mc_free(mci);
  286. }
  287. static const struct pci_device_id amd76x_pci_tbl[] = {
  288. {
  289. PCI_VEND_DEV(AMD, FE_GATE_700C), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  290. AMD762},
  291. {
  292. PCI_VEND_DEV(AMD, FE_GATE_700E), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  293. AMD761},
  294. {
  295. 0,
  296. } /* 0 terminated list. */
  297. };
  298. MODULE_DEVICE_TABLE(pci, amd76x_pci_tbl);
  299. static struct pci_driver amd76x_driver = {
  300. .name = EDAC_MOD_STR,
  301. .probe = amd76x_init_one,
  302. .remove = amd76x_remove_one,
  303. .id_table = amd76x_pci_tbl,
  304. };
  305. static int __init amd76x_init(void)
  306. {
  307. /* Ensure that the OPSTATE is set correctly for POLL or NMI */
  308. opstate_init();
  309. return pci_register_driver(&amd76x_driver);
  310. }
  311. static void __exit amd76x_exit(void)
  312. {
  313. pci_unregister_driver(&amd76x_driver);
  314. }
  315. module_init(amd76x_init);
  316. module_exit(amd76x_exit);
  317. MODULE_LICENSE("GPL");
  318. MODULE_AUTHOR("Linux Networx (http://lnxi.com) Thayne Harbaugh");
  319. MODULE_DESCRIPTION("MC support for AMD 76x memory controllers");
  320. module_param(edac_op_state, int, 0444);
  321. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");