i3000_edac.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * Intel 3000/3010 Memory Controller kernel module
  3. * Copyright (C) 2007 Akamai Technologies, Inc.
  4. * Shamelessly copied from:
  5. * Intel D82875P Memory Controller kernel module
  6. * (C) 2003 Linux Networx (http://lnxi.com)
  7. *
  8. * This file may be distributed under the terms of the
  9. * GNU General Public License.
  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 "edac_core.h"
  17. #define I3000_REVISION "1.1"
  18. #define EDAC_MOD_STR "i3000_edac"
  19. #define I3000_RANKS 8
  20. #define I3000_RANKS_PER_CHANNEL 4
  21. #define I3000_CHANNELS 2
  22. /* Intel 3000 register addresses - device 0 function 0 - DRAM Controller */
  23. #define I3000_MCHBAR 0x44 /* MCH Memory Mapped Register BAR */
  24. #define I3000_MCHBAR_MASK 0xffffc000
  25. #define I3000_MMR_WINDOW_SIZE 16384
  26. #define I3000_EDEAP 0x70 /* Extended DRAM Error Address Pointer (8b)
  27. *
  28. * 7:1 reserved
  29. * 0 bit 32 of address
  30. */
  31. #define I3000_DEAP 0x58 /* DRAM Error Address Pointer (32b)
  32. *
  33. * 31:7 address
  34. * 6:1 reserved
  35. * 0 Error channel 0/1
  36. */
  37. #define I3000_DEAP_GRAIN (1 << 7)
  38. #define I3000_DEAP_PFN(edeap, deap) ((((edeap) & 1) << (32 - PAGE_SHIFT)) | \
  39. ((deap) >> PAGE_SHIFT))
  40. #define I3000_DEAP_OFFSET(deap) ((deap) & ~(I3000_DEAP_GRAIN-1) & ~PAGE_MASK)
  41. #define I3000_DEAP_CHANNEL(deap) ((deap) & 1)
  42. #define I3000_DERRSYN 0x5c /* DRAM Error Syndrome (8b)
  43. *
  44. * 7:0 DRAM ECC Syndrome
  45. */
  46. #define I3000_ERRSTS 0xc8 /* Error Status Register (16b)
  47. *
  48. * 15:12 reserved
  49. * 11 MCH Thermal Sensor Event for SMI/SCI/SERR
  50. * 10 reserved
  51. * 9 LOCK to non-DRAM Memory Flag (LCKF)
  52. * 8 Received Refresh Timeout Flag (RRTOF)
  53. * 7:2 reserved
  54. * 1 Multiple-bit DRAM ECC Error Flag (DMERR)
  55. * 0 Single-bit DRAM ECC Error Flag (DSERR)
  56. */
  57. #define I3000_ERRSTS_BITS 0x0b03 /* bits which indicate errors */
  58. #define I3000_ERRSTS_UE 0x0002
  59. #define I3000_ERRSTS_CE 0x0001
  60. #define I3000_ERRCMD 0xca /* Error Command (16b)
  61. *
  62. * 15:12 reserved
  63. * 11 SERR on MCH Thermal Sensor Event (TSESERR)
  64. * 10 reserved
  65. * 9 SERR on LOCK to non-DRAM Memory (LCKERR)
  66. * 8 SERR on DRAM Refresh Timeout (DRTOERR)
  67. * 7:2 reserved
  68. * 1 SERR Multiple-Bit DRAM ECC Error (DMERR)
  69. * 0 SERR on Single-Bit ECC Error (DSERR)
  70. */
  71. /* Intel MMIO register space - device 0 function 0 - MMR space */
  72. #define I3000_DRB_SHIFT 25 /* 32MiB grain */
  73. #define I3000_C0DRB 0x100 /* Channel 0 DRAM Rank Boundary (8b x 4)
  74. *
  75. * 7:0 Channel 0 DRAM Rank Boundary Address
  76. */
  77. #define I3000_C1DRB 0x180 /* Channel 1 DRAM Rank Boundary (8b x 4)
  78. *
  79. * 7:0 Channel 1 DRAM Rank Boundary Address
  80. */
  81. #define I3000_C0DRA 0x108 /* Channel 0 DRAM Rank Attribute (8b x 2)
  82. *
  83. * 7 reserved
  84. * 6:4 DRAM odd Rank Attribute
  85. * 3 reserved
  86. * 2:0 DRAM even Rank Attribute
  87. *
  88. * Each attribute defines the page
  89. * size of the corresponding rank:
  90. * 000: unpopulated
  91. * 001: reserved
  92. * 010: 4 KB
  93. * 011: 8 KB
  94. * 100: 16 KB
  95. * Others: reserved
  96. */
  97. #define I3000_C1DRA 0x188 /* Channel 1 DRAM Rank Attribute (8b x 2) */
  98. #define ODD_RANK_ATTRIB(dra) (((dra) & 0x70) >> 4)
  99. #define EVEN_RANK_ATTRIB(dra) ((dra) & 0x07)
  100. #define I3000_C0DRC0 0x120 /* DRAM Controller Mode 0 (32b)
  101. *
  102. * 31:30 reserved
  103. * 29 Initialization Complete (IC)
  104. * 28:11 reserved
  105. * 10:8 Refresh Mode Select (RMS)
  106. * 7 reserved
  107. * 6:4 Mode Select (SMS)
  108. * 3:2 reserved
  109. * 1:0 DRAM Type (DT)
  110. */
  111. #define I3000_C0DRC1 0x124 /* DRAM Controller Mode 1 (32b)
  112. *
  113. * 31 Enhanced Addressing Enable (ENHADE)
  114. * 30:0 reserved
  115. */
  116. enum i3000p_chips {
  117. I3000 = 0,
  118. };
  119. struct i3000_dev_info {
  120. const char *ctl_name;
  121. };
  122. struct i3000_error_info {
  123. u16 errsts;
  124. u8 derrsyn;
  125. u8 edeap;
  126. u32 deap;
  127. u16 errsts2;
  128. };
  129. static const struct i3000_dev_info i3000_devs[] = {
  130. [I3000] = {
  131. .ctl_name = "i3000"},
  132. };
  133. static struct pci_dev *mci_pdev;
  134. static int i3000_registered = 1;
  135. static struct edac_pci_ctl_info *i3000_pci;
  136. static void i3000_get_error_info(struct mem_ctl_info *mci,
  137. struct i3000_error_info *info)
  138. {
  139. struct pci_dev *pdev;
  140. pdev = to_pci_dev(mci->dev);
  141. /*
  142. * This is a mess because there is no atomic way to read all the
  143. * registers at once and the registers can transition from CE being
  144. * overwritten by UE.
  145. */
  146. pci_read_config_word(pdev, I3000_ERRSTS, &info->errsts);
  147. if (!(info->errsts & I3000_ERRSTS_BITS))
  148. return;
  149. pci_read_config_byte(pdev, I3000_EDEAP, &info->edeap);
  150. pci_read_config_dword(pdev, I3000_DEAP, &info->deap);
  151. pci_read_config_byte(pdev, I3000_DERRSYN, &info->derrsyn);
  152. pci_read_config_word(pdev, I3000_ERRSTS, &info->errsts2);
  153. /*
  154. * If the error is the same for both reads then the first set
  155. * of reads is valid. If there is a change then there is a CE
  156. * with no info and the second set of reads is valid and
  157. * should be UE info.
  158. */
  159. if ((info->errsts ^ info->errsts2) & I3000_ERRSTS_BITS) {
  160. pci_read_config_byte(pdev, I3000_EDEAP, &info->edeap);
  161. pci_read_config_dword(pdev, I3000_DEAP, &info->deap);
  162. pci_read_config_byte(pdev, I3000_DERRSYN, &info->derrsyn);
  163. }
  164. /* Clear any error bits.
  165. * (Yes, we really clear bits by writing 1 to them.)
  166. */
  167. pci_write_bits16(pdev, I3000_ERRSTS, I3000_ERRSTS_BITS,
  168. I3000_ERRSTS_BITS);
  169. }
  170. static int i3000_process_error_info(struct mem_ctl_info *mci,
  171. struct i3000_error_info *info,
  172. int handle_errors)
  173. {
  174. int row, multi_chan;
  175. int pfn, offset, channel;
  176. multi_chan = mci->csrows[0].nr_channels - 1;
  177. if (!(info->errsts & I3000_ERRSTS_BITS))
  178. return 0;
  179. if (!handle_errors)
  180. return 1;
  181. if ((info->errsts ^ info->errsts2) & I3000_ERRSTS_BITS) {
  182. edac_mc_handle_ce_no_info(mci, "UE overwrote CE");
  183. info->errsts = info->errsts2;
  184. }
  185. pfn = I3000_DEAP_PFN(info->edeap, info->deap);
  186. offset = I3000_DEAP_OFFSET(info->deap);
  187. channel = I3000_DEAP_CHANNEL(info->deap);
  188. row = edac_mc_find_csrow_by_page(mci, pfn);
  189. if (info->errsts & I3000_ERRSTS_UE)
  190. edac_mc_handle_ue(mci, pfn, offset, row, "i3000 UE");
  191. else
  192. edac_mc_handle_ce(mci, pfn, offset, info->derrsyn, row,
  193. multi_chan ? channel : 0, "i3000 CE");
  194. return 1;
  195. }
  196. static void i3000_check(struct mem_ctl_info *mci)
  197. {
  198. struct i3000_error_info info;
  199. debugf1("MC%d: %s()\n", mci->mc_idx, __func__);
  200. i3000_get_error_info(mci, &info);
  201. i3000_process_error_info(mci, &info, 1);
  202. }
  203. static int i3000_is_interleaved(const unsigned char *c0dra,
  204. const unsigned char *c1dra,
  205. const unsigned char *c0drb,
  206. const unsigned char *c1drb)
  207. {
  208. int i;
  209. /* If the channels aren't populated identically then
  210. * we're not interleaved.
  211. */
  212. for (i = 0; i < I3000_RANKS_PER_CHANNEL / 2; i++)
  213. if (ODD_RANK_ATTRIB(c0dra[i]) != ODD_RANK_ATTRIB(c1dra[i]) ||
  214. EVEN_RANK_ATTRIB(c0dra[i]) !=
  215. EVEN_RANK_ATTRIB(c1dra[i]))
  216. return 0;
  217. /* If the rank boundaries for the two channels are different
  218. * then we're not interleaved.
  219. */
  220. for (i = 0; i < I3000_RANKS_PER_CHANNEL; i++)
  221. if (c0drb[i] != c1drb[i])
  222. return 0;
  223. return 1;
  224. }
  225. static int i3000_probe1(struct pci_dev *pdev, int dev_idx)
  226. {
  227. int rc;
  228. int i;
  229. struct mem_ctl_info *mci = NULL;
  230. unsigned long last_cumul_size;
  231. int interleaved, nr_channels;
  232. unsigned char dra[I3000_RANKS / 2], drb[I3000_RANKS];
  233. unsigned char *c0dra = dra, *c1dra = &dra[I3000_RANKS_PER_CHANNEL / 2];
  234. unsigned char *c0drb = drb, *c1drb = &drb[I3000_RANKS_PER_CHANNEL];
  235. unsigned long mchbar;
  236. void __iomem *window;
  237. debugf0("MC: %s()\n", __func__);
  238. pci_read_config_dword(pdev, I3000_MCHBAR, (u32 *) & mchbar);
  239. mchbar &= I3000_MCHBAR_MASK;
  240. window = ioremap_nocache(mchbar, I3000_MMR_WINDOW_SIZE);
  241. if (!window) {
  242. printk(KERN_ERR "i3000: cannot map mmio space at 0x%lx\n",
  243. mchbar);
  244. return -ENODEV;
  245. }
  246. c0dra[0] = readb(window + I3000_C0DRA + 0); /* ranks 0,1 */
  247. c0dra[1] = readb(window + I3000_C0DRA + 1); /* ranks 2,3 */
  248. c1dra[0] = readb(window + I3000_C1DRA + 0); /* ranks 0,1 */
  249. c1dra[1] = readb(window + I3000_C1DRA + 1); /* ranks 2,3 */
  250. for (i = 0; i < I3000_RANKS_PER_CHANNEL; i++) {
  251. c0drb[i] = readb(window + I3000_C0DRB + i);
  252. c1drb[i] = readb(window + I3000_C1DRB + i);
  253. }
  254. iounmap(window);
  255. /* Figure out how many channels we have.
  256. *
  257. * If we have what the datasheet calls "asymmetric channels"
  258. * (essentially the same as what was called "virtual single
  259. * channel mode" in the i82875) then it's a single channel as
  260. * far as EDAC is concerned.
  261. */
  262. interleaved = i3000_is_interleaved(c0dra, c1dra, c0drb, c1drb);
  263. nr_channels = interleaved ? 2 : 1;
  264. mci = edac_mc_alloc(0, I3000_RANKS / nr_channels, nr_channels, 0);
  265. if (!mci)
  266. return -ENOMEM;
  267. debugf3("MC: %s(): init mci\n", __func__);
  268. mci->dev = &pdev->dev;
  269. mci->mtype_cap = MEM_FLAG_DDR2;
  270. mci->edac_ctl_cap = EDAC_FLAG_SECDED;
  271. mci->edac_cap = EDAC_FLAG_SECDED;
  272. mci->mod_name = EDAC_MOD_STR;
  273. mci->mod_ver = I3000_REVISION;
  274. mci->ctl_name = i3000_devs[dev_idx].ctl_name;
  275. mci->dev_name = pci_name(pdev);
  276. mci->edac_check = i3000_check;
  277. mci->ctl_page_to_phys = NULL;
  278. /*
  279. * The dram rank boundary (DRB) reg values are boundary addresses
  280. * for each DRAM rank with a granularity of 32MB. DRB regs are
  281. * cumulative; the last one will contain the total memory
  282. * contained in all ranks.
  283. *
  284. * If we're in interleaved mode then we're only walking through
  285. * the ranks of controller 0, so we double all the values we see.
  286. */
  287. for (last_cumul_size = i = 0; i < mci->nr_csrows; i++) {
  288. u8 value;
  289. u32 cumul_size;
  290. struct csrow_info *csrow = &mci->csrows[i];
  291. value = drb[i];
  292. cumul_size = value << (I3000_DRB_SHIFT - PAGE_SHIFT);
  293. if (interleaved)
  294. cumul_size <<= 1;
  295. debugf3("MC: %s(): (%d) cumul_size 0x%x\n",
  296. __func__, i, cumul_size);
  297. if (cumul_size == last_cumul_size) {
  298. csrow->mtype = MEM_EMPTY;
  299. continue;
  300. }
  301. csrow->first_page = last_cumul_size;
  302. csrow->last_page = cumul_size - 1;
  303. csrow->nr_pages = cumul_size - last_cumul_size;
  304. last_cumul_size = cumul_size;
  305. csrow->grain = I3000_DEAP_GRAIN;
  306. csrow->mtype = MEM_DDR2;
  307. csrow->dtype = DEV_UNKNOWN;
  308. csrow->edac_mode = EDAC_UNKNOWN;
  309. }
  310. /* Clear any error bits.
  311. * (Yes, we really clear bits by writing 1 to them.)
  312. */
  313. pci_write_bits16(pdev, I3000_ERRSTS, I3000_ERRSTS_BITS,
  314. I3000_ERRSTS_BITS);
  315. rc = -ENODEV;
  316. if (edac_mc_add_mc(mci)) {
  317. debugf3("MC: %s(): failed edac_mc_add_mc()\n", __func__);
  318. goto fail;
  319. }
  320. /* allocating generic PCI control info */
  321. i3000_pci = edac_pci_create_generic_ctl(&pdev->dev, EDAC_MOD_STR);
  322. if (!i3000_pci) {
  323. printk(KERN_WARNING
  324. "%s(): Unable to create PCI control\n",
  325. __func__);
  326. printk(KERN_WARNING
  327. "%s(): PCI error report via EDAC not setup\n",
  328. __func__);
  329. }
  330. /* get this far and it's successful */
  331. debugf3("MC: %s(): success\n", __func__);
  332. return 0;
  333. fail:
  334. if (mci)
  335. edac_mc_free(mci);
  336. return rc;
  337. }
  338. /* returns count (>= 0), or negative on error */
  339. static int __devinit i3000_init_one(struct pci_dev *pdev,
  340. const struct pci_device_id *ent)
  341. {
  342. int rc;
  343. debugf0("MC: %s()\n", __func__);
  344. if (pci_enable_device(pdev) < 0)
  345. return -EIO;
  346. rc = i3000_probe1(pdev, ent->driver_data);
  347. if (mci_pdev == NULL)
  348. mci_pdev = pci_dev_get(pdev);
  349. return rc;
  350. }
  351. static void __devexit i3000_remove_one(struct pci_dev *pdev)
  352. {
  353. struct mem_ctl_info *mci;
  354. debugf0("%s()\n", __func__);
  355. if (i3000_pci)
  356. edac_pci_release_generic_ctl(i3000_pci);
  357. if ((mci = edac_mc_del_mc(&pdev->dev)) == NULL)
  358. return;
  359. edac_mc_free(mci);
  360. }
  361. static const struct pci_device_id i3000_pci_tbl[] __devinitdata = {
  362. {
  363. PCI_VEND_DEV(INTEL, 3000_HB), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  364. I3000},
  365. {
  366. 0,
  367. } /* 0 terminated list. */
  368. };
  369. MODULE_DEVICE_TABLE(pci, i3000_pci_tbl);
  370. static struct pci_driver i3000_driver = {
  371. .name = EDAC_MOD_STR,
  372. .probe = i3000_init_one,
  373. .remove = __devexit_p(i3000_remove_one),
  374. .id_table = i3000_pci_tbl,
  375. };
  376. static int __init i3000_init(void)
  377. {
  378. int pci_rc;
  379. debugf3("MC: %s()\n", __func__);
  380. pci_rc = pci_register_driver(&i3000_driver);
  381. if (pci_rc < 0)
  382. goto fail0;
  383. if (mci_pdev == NULL) {
  384. i3000_registered = 0;
  385. mci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  386. PCI_DEVICE_ID_INTEL_3000_HB, NULL);
  387. if (!mci_pdev) {
  388. debugf0("i3000 pci_get_device fail\n");
  389. pci_rc = -ENODEV;
  390. goto fail1;
  391. }
  392. pci_rc = i3000_init_one(mci_pdev, i3000_pci_tbl);
  393. if (pci_rc < 0) {
  394. debugf0("i3000 init fail\n");
  395. pci_rc = -ENODEV;
  396. goto fail1;
  397. }
  398. }
  399. return 0;
  400. fail1:
  401. pci_unregister_driver(&i3000_driver);
  402. fail0:
  403. if (mci_pdev)
  404. pci_dev_put(mci_pdev);
  405. return pci_rc;
  406. }
  407. static void __exit i3000_exit(void)
  408. {
  409. debugf3("MC: %s()\n", __func__);
  410. pci_unregister_driver(&i3000_driver);
  411. if (!i3000_registered) {
  412. i3000_remove_one(mci_pdev);
  413. pci_dev_put(mci_pdev);
  414. }
  415. }
  416. module_init(i3000_init);
  417. module_exit(i3000_exit);
  418. MODULE_LICENSE("GPL");
  419. MODULE_AUTHOR("Akamai Technologies Arthur Ulfeldt/Jason Uhlenkott");
  420. MODULE_DESCRIPTION("MC support for Intel 3000 memory hub controllers");