i3200_edac.c 13 KB

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