x38_edac.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * Intel X38 Memory Controller kernel module
  3. * Copyright (C) 2008 Cluster Computing, Inc.
  4. *
  5. * This file may be distributed under the terms of the
  6. * GNU General Public License.
  7. *
  8. * This file is based on i3200_edac.c
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/pci.h>
  14. #include <linux/pci_ids.h>
  15. #include <linux/slab.h>
  16. #include <linux/edac.h>
  17. #include "edac_core.h"
  18. #define X38_REVISION "1.1"
  19. #define EDAC_MOD_STR "x38_edac"
  20. #define PCI_DEVICE_ID_INTEL_X38_HB 0x29e0
  21. #define X38_RANKS 8
  22. #define X38_RANKS_PER_CHANNEL 4
  23. #define X38_CHANNELS 2
  24. /* Intel X38 register addresses - device 0 function 0 - DRAM Controller */
  25. #define X38_MCHBAR_LOW 0x48 /* MCH Memory Mapped Register BAR */
  26. #define X38_MCHBAR_HIGH 0x4b
  27. #define X38_MCHBAR_MASK 0xfffffc000ULL /* bits 35:14 */
  28. #define X38_MMR_WINDOW_SIZE 16384
  29. #define X38_TOM 0xa0 /* Top of Memory (16b)
  30. *
  31. * 15:10 reserved
  32. * 9:0 total populated physical memory
  33. */
  34. #define X38_TOM_MASK 0x3ff /* bits 9:0 */
  35. #define X38_TOM_SHIFT 26 /* 64MiB grain */
  36. #define X38_ERRSTS 0xc8 /* Error Status Register (16b)
  37. *
  38. * 15 reserved
  39. * 14 Isochronous TBWRR Run Behind FIFO Full
  40. * (ITCV)
  41. * 13 Isochronous TBWRR Run Behind FIFO Put
  42. * (ITSTV)
  43. * 12 reserved
  44. * 11 MCH Thermal Sensor Event
  45. * for SMI/SCI/SERR (GTSE)
  46. * 10 reserved
  47. * 9 LOCK to non-DRAM Memory Flag (LCKF)
  48. * 8 reserved
  49. * 7 DRAM Throttle Flag (DTF)
  50. * 6:2 reserved
  51. * 1 Multi-bit DRAM ECC Error Flag (DMERR)
  52. * 0 Single-bit DRAM ECC Error Flag (DSERR)
  53. */
  54. #define X38_ERRSTS_UE 0x0002
  55. #define X38_ERRSTS_CE 0x0001
  56. #define X38_ERRSTS_BITS (X38_ERRSTS_UE | X38_ERRSTS_CE)
  57. /* Intel MMIO register space - device 0 function 0 - MMR space */
  58. #define X38_C0DRB 0x200 /* Channel 0 DRAM Rank Boundary (16b x 4)
  59. *
  60. * 15:10 reserved
  61. * 9:0 Channel 0 DRAM Rank Boundary Address
  62. */
  63. #define X38_C1DRB 0x600 /* Channel 1 DRAM Rank Boundary (16b x 4) */
  64. #define X38_DRB_MASK 0x3ff /* bits 9:0 */
  65. #define X38_DRB_SHIFT 26 /* 64MiB grain */
  66. #define X38_C0ECCERRLOG 0x280 /* Channel 0 ECC Error Log (64b)
  67. *
  68. * 63:48 Error Column Address (ERRCOL)
  69. * 47:32 Error Row Address (ERRROW)
  70. * 31:29 Error Bank Address (ERRBANK)
  71. * 28:27 Error Rank Address (ERRRANK)
  72. * 26:24 reserved
  73. * 23:16 Error Syndrome (ERRSYND)
  74. * 15: 2 reserved
  75. * 1 Multiple Bit Error Status (MERRSTS)
  76. * 0 Correctable Error Status (CERRSTS)
  77. */
  78. #define X38_C1ECCERRLOG 0x680 /* Channel 1 ECC Error Log (64b) */
  79. #define X38_ECCERRLOG_CE 0x1
  80. #define X38_ECCERRLOG_UE 0x2
  81. #define X38_ECCERRLOG_RANK_BITS 0x18000000
  82. #define X38_ECCERRLOG_SYNDROME_BITS 0xff0000
  83. #define X38_CAPID0 0xe0 /* see P.94 of spec for details */
  84. static int x38_channel_num;
  85. static int how_many_channel(struct pci_dev *pdev)
  86. {
  87. unsigned char capid0_8b; /* 8th byte of CAPID0 */
  88. pci_read_config_byte(pdev, X38_CAPID0 + 8, &capid0_8b);
  89. if (capid0_8b & 0x20) { /* check DCD: Dual Channel Disable */
  90. debugf0("In single channel mode.\n");
  91. x38_channel_num = 1;
  92. } else {
  93. debugf0("In dual channel mode.\n");
  94. x38_channel_num = 2;
  95. }
  96. return x38_channel_num;
  97. }
  98. static unsigned long eccerrlog_syndrome(u64 log)
  99. {
  100. return (log & X38_ECCERRLOG_SYNDROME_BITS) >> 16;
  101. }
  102. static int eccerrlog_row(int channel, u64 log)
  103. {
  104. return ((log & X38_ECCERRLOG_RANK_BITS) >> 27) |
  105. (channel * X38_RANKS_PER_CHANNEL);
  106. }
  107. enum x38_chips {
  108. X38 = 0,
  109. };
  110. struct x38_dev_info {
  111. const char *ctl_name;
  112. };
  113. struct x38_error_info {
  114. u16 errsts;
  115. u16 errsts2;
  116. u64 eccerrlog[X38_CHANNELS];
  117. };
  118. static const struct x38_dev_info x38_devs[] = {
  119. [X38] = {
  120. .ctl_name = "x38"},
  121. };
  122. static struct pci_dev *mci_pdev;
  123. static int x38_registered = 1;
  124. static void x38_clear_error_info(struct mem_ctl_info *mci)
  125. {
  126. struct pci_dev *pdev;
  127. pdev = to_pci_dev(mci->dev);
  128. /*
  129. * Clear any error bits.
  130. * (Yes, we really clear bits by writing 1 to them.)
  131. */
  132. pci_write_bits16(pdev, X38_ERRSTS, X38_ERRSTS_BITS,
  133. X38_ERRSTS_BITS);
  134. }
  135. static u64 x38_readq(const void __iomem *addr)
  136. {
  137. return readl(addr) | (((u64)readl(addr + 4)) << 32);
  138. }
  139. static void x38_get_and_clear_error_info(struct mem_ctl_info *mci,
  140. struct x38_error_info *info)
  141. {
  142. struct pci_dev *pdev;
  143. void __iomem *window = mci->pvt_info;
  144. pdev = to_pci_dev(mci->dev);
  145. /*
  146. * This is a mess because there is no atomic way to read all the
  147. * registers at once and the registers can transition from CE being
  148. * overwritten by UE.
  149. */
  150. pci_read_config_word(pdev, X38_ERRSTS, &info->errsts);
  151. if (!(info->errsts & X38_ERRSTS_BITS))
  152. return;
  153. info->eccerrlog[0] = x38_readq(window + X38_C0ECCERRLOG);
  154. if (x38_channel_num == 2)
  155. info->eccerrlog[1] = x38_readq(window + X38_C1ECCERRLOG);
  156. pci_read_config_word(pdev, X38_ERRSTS, &info->errsts2);
  157. /*
  158. * If the error is the same for both reads then the first set
  159. * of reads is valid. If there is a change then there is a CE
  160. * with no info and the second set of reads is valid and
  161. * should be UE info.
  162. */
  163. if ((info->errsts ^ info->errsts2) & X38_ERRSTS_BITS) {
  164. info->eccerrlog[0] = x38_readq(window + X38_C0ECCERRLOG);
  165. if (x38_channel_num == 2)
  166. info->eccerrlog[1] =
  167. x38_readq(window + X38_C1ECCERRLOG);
  168. }
  169. x38_clear_error_info(mci);
  170. }
  171. static void x38_process_error_info(struct mem_ctl_info *mci,
  172. struct x38_error_info *info)
  173. {
  174. int channel;
  175. u64 log;
  176. if (!(info->errsts & X38_ERRSTS_BITS))
  177. return;
  178. if ((info->errsts ^ info->errsts2) & X38_ERRSTS_BITS) {
  179. edac_mc_handle_ce_no_info(mci, "UE overwrote CE");
  180. info->errsts = info->errsts2;
  181. }
  182. for (channel = 0; channel < x38_channel_num; channel++) {
  183. log = info->eccerrlog[channel];
  184. if (log & X38_ECCERRLOG_UE) {
  185. edac_mc_handle_ue(mci, 0, 0,
  186. eccerrlog_row(channel, log), "x38 UE");
  187. } else if (log & X38_ECCERRLOG_CE) {
  188. edac_mc_handle_ce(mci, 0, 0,
  189. eccerrlog_syndrome(log),
  190. eccerrlog_row(channel, log), 0, "x38 CE");
  191. }
  192. }
  193. }
  194. static void x38_check(struct mem_ctl_info *mci)
  195. {
  196. struct x38_error_info info;
  197. debugf1("MC%d: %s()\n", mci->mc_idx, __func__);
  198. x38_get_and_clear_error_info(mci, &info);
  199. x38_process_error_info(mci, &info);
  200. }
  201. void __iomem *x38_map_mchbar(struct pci_dev *pdev)
  202. {
  203. union {
  204. u64 mchbar;
  205. struct {
  206. u32 mchbar_low;
  207. u32 mchbar_high;
  208. };
  209. } u;
  210. void __iomem *window;
  211. pci_read_config_dword(pdev, X38_MCHBAR_LOW, &u.mchbar_low);
  212. pci_write_config_dword(pdev, X38_MCHBAR_LOW, u.mchbar_low | 0x1);
  213. pci_read_config_dword(pdev, X38_MCHBAR_HIGH, &u.mchbar_high);
  214. u.mchbar &= X38_MCHBAR_MASK;
  215. if (u.mchbar != (resource_size_t)u.mchbar) {
  216. printk(KERN_ERR
  217. "x38: mmio space beyond accessible range (0x%llx)\n",
  218. (unsigned long long)u.mchbar);
  219. return NULL;
  220. }
  221. window = ioremap_nocache(u.mchbar, X38_MMR_WINDOW_SIZE);
  222. if (!window)
  223. printk(KERN_ERR "x38: cannot map mmio space at 0x%llx\n",
  224. (unsigned long long)u.mchbar);
  225. return window;
  226. }
  227. static void x38_get_drbs(void __iomem *window,
  228. u16 drbs[X38_CHANNELS][X38_RANKS_PER_CHANNEL])
  229. {
  230. int i;
  231. for (i = 0; i < X38_RANKS_PER_CHANNEL; i++) {
  232. drbs[0][i] = readw(window + X38_C0DRB + 2*i) & X38_DRB_MASK;
  233. drbs[1][i] = readw(window + X38_C1DRB + 2*i) & X38_DRB_MASK;
  234. }
  235. }
  236. static bool x38_is_stacked(struct pci_dev *pdev,
  237. u16 drbs[X38_CHANNELS][X38_RANKS_PER_CHANNEL])
  238. {
  239. u16 tom;
  240. pci_read_config_word(pdev, X38_TOM, &tom);
  241. tom &= X38_TOM_MASK;
  242. return drbs[X38_CHANNELS - 1][X38_RANKS_PER_CHANNEL - 1] == tom;
  243. }
  244. static unsigned long drb_to_nr_pages(
  245. u16 drbs[X38_CHANNELS][X38_RANKS_PER_CHANNEL],
  246. bool stacked, int channel, int rank)
  247. {
  248. int n;
  249. n = drbs[channel][rank];
  250. if (rank > 0)
  251. n -= drbs[channel][rank - 1];
  252. if (stacked && (channel == 1) && drbs[channel][rank] ==
  253. drbs[channel][X38_RANKS_PER_CHANNEL - 1]) {
  254. n -= drbs[0][X38_RANKS_PER_CHANNEL - 1];
  255. }
  256. n <<= (X38_DRB_SHIFT - PAGE_SHIFT);
  257. return n;
  258. }
  259. static int x38_probe1(struct pci_dev *pdev, int dev_idx)
  260. {
  261. int rc;
  262. int i;
  263. struct mem_ctl_info *mci = NULL;
  264. unsigned long last_page;
  265. u16 drbs[X38_CHANNELS][X38_RANKS_PER_CHANNEL];
  266. bool stacked;
  267. void __iomem *window;
  268. debugf0("MC: %s()\n", __func__);
  269. window = x38_map_mchbar(pdev);
  270. if (!window)
  271. return -ENODEV;
  272. x38_get_drbs(window, drbs);
  273. how_many_channel(pdev);
  274. /* FIXME: unconventional pvt_info usage */
  275. mci = edac_mc_alloc(0, X38_RANKS, x38_channel_num, 0);
  276. if (!mci)
  277. return -ENOMEM;
  278. debugf3("MC: %s(): init mci\n", __func__);
  279. mci->dev = &pdev->dev;
  280. mci->mtype_cap = MEM_FLAG_DDR2;
  281. mci->edac_ctl_cap = EDAC_FLAG_SECDED;
  282. mci->edac_cap = EDAC_FLAG_SECDED;
  283. mci->mod_name = EDAC_MOD_STR;
  284. mci->mod_ver = X38_REVISION;
  285. mci->ctl_name = x38_devs[dev_idx].ctl_name;
  286. mci->dev_name = pci_name(pdev);
  287. mci->edac_check = x38_check;
  288. mci->ctl_page_to_phys = NULL;
  289. mci->pvt_info = window;
  290. stacked = x38_is_stacked(pdev, drbs);
  291. /*
  292. * The dram rank boundary (DRB) reg values are boundary addresses
  293. * for each DRAM rank with a granularity of 64MB. DRB regs are
  294. * cumulative; the last one will contain the total memory
  295. * contained in all ranks.
  296. */
  297. last_page = -1UL;
  298. for (i = 0; i < mci->nr_csrows; i++) {
  299. unsigned long nr_pages;
  300. struct csrow_info *csrow = &mci->csrows[i];
  301. nr_pages = drb_to_nr_pages(drbs, stacked,
  302. i / X38_RANKS_PER_CHANNEL,
  303. i % X38_RANKS_PER_CHANNEL);
  304. if (nr_pages == 0) {
  305. csrow->mtype = MEM_EMPTY;
  306. continue;
  307. }
  308. csrow->first_page = last_page + 1;
  309. last_page += nr_pages;
  310. csrow->last_page = last_page;
  311. csrow->nr_pages = nr_pages;
  312. csrow->grain = nr_pages << PAGE_SHIFT;
  313. csrow->mtype = MEM_DDR2;
  314. csrow->dtype = DEV_UNKNOWN;
  315. csrow->edac_mode = EDAC_UNKNOWN;
  316. }
  317. x38_clear_error_info(mci);
  318. rc = -ENODEV;
  319. if (edac_mc_add_mc(mci)) {
  320. debugf3("MC: %s(): failed edac_mc_add_mc()\n", __func__);
  321. goto fail;
  322. }
  323. /* get this far and it's successful */
  324. debugf3("MC: %s(): success\n", __func__);
  325. return 0;
  326. fail:
  327. iounmap(window);
  328. if (mci)
  329. edac_mc_free(mci);
  330. return rc;
  331. }
  332. static int __devinit x38_init_one(struct pci_dev *pdev,
  333. const struct pci_device_id *ent)
  334. {
  335. int rc;
  336. debugf0("MC: %s()\n", __func__);
  337. if (pci_enable_device(pdev) < 0)
  338. return -EIO;
  339. rc = x38_probe1(pdev, ent->driver_data);
  340. if (!mci_pdev)
  341. mci_pdev = pci_dev_get(pdev);
  342. return rc;
  343. }
  344. static void __devexit x38_remove_one(struct pci_dev *pdev)
  345. {
  346. struct mem_ctl_info *mci;
  347. debugf0("%s()\n", __func__);
  348. mci = edac_mc_del_mc(&pdev->dev);
  349. if (!mci)
  350. return;
  351. iounmap(mci->pvt_info);
  352. edac_mc_free(mci);
  353. }
  354. static const struct pci_device_id x38_pci_tbl[] __devinitdata = {
  355. {
  356. PCI_VEND_DEV(INTEL, X38_HB), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  357. X38},
  358. {
  359. 0,
  360. } /* 0 terminated list. */
  361. };
  362. MODULE_DEVICE_TABLE(pci, x38_pci_tbl);
  363. static struct pci_driver x38_driver = {
  364. .name = EDAC_MOD_STR,
  365. .probe = x38_init_one,
  366. .remove = __devexit_p(x38_remove_one),
  367. .id_table = x38_pci_tbl,
  368. };
  369. static int __init x38_init(void)
  370. {
  371. int pci_rc;
  372. debugf3("MC: %s()\n", __func__);
  373. /* Ensure that the OPSTATE is set correctly for POLL or NMI */
  374. opstate_init();
  375. pci_rc = pci_register_driver(&x38_driver);
  376. if (pci_rc < 0)
  377. goto fail0;
  378. if (!mci_pdev) {
  379. x38_registered = 0;
  380. mci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  381. PCI_DEVICE_ID_INTEL_X38_HB, NULL);
  382. if (!mci_pdev) {
  383. debugf0("x38 pci_get_device fail\n");
  384. pci_rc = -ENODEV;
  385. goto fail1;
  386. }
  387. pci_rc = x38_init_one(mci_pdev, x38_pci_tbl);
  388. if (pci_rc < 0) {
  389. debugf0("x38 init fail\n");
  390. pci_rc = -ENODEV;
  391. goto fail1;
  392. }
  393. }
  394. return 0;
  395. fail1:
  396. pci_unregister_driver(&x38_driver);
  397. fail0:
  398. if (mci_pdev)
  399. pci_dev_put(mci_pdev);
  400. return pci_rc;
  401. }
  402. static void __exit x38_exit(void)
  403. {
  404. debugf3("MC: %s()\n", __func__);
  405. pci_unregister_driver(&x38_driver);
  406. if (!x38_registered) {
  407. x38_remove_one(mci_pdev);
  408. pci_dev_put(mci_pdev);
  409. }
  410. }
  411. module_init(x38_init);
  412. module_exit(x38_exit);
  413. MODULE_LICENSE("GPL");
  414. MODULE_AUTHOR("Cluster Computing, Inc. Hitoshi Mitake");
  415. MODULE_DESCRIPTION("MC support for Intel X38 memory hub controllers");
  416. module_param(edac_op_state, int, 0444);
  417. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");