i3200_edac.c 12 KB

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