i5100_edac.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. * Intel 5100 Memory Controllers kernel module
  3. *
  4. * This file may be distributed under the terms of the
  5. * GNU General Public License.
  6. *
  7. * This module is based on the following document:
  8. *
  9. * Intel 5100X Chipset Memory Controller Hub (MCH) - Datasheet
  10. * http://download.intel.com/design/chipsets/datashts/318378.pdf
  11. *
  12. * The intel 5100 has two independent channels. EDAC core currently
  13. * can not reflect this configuration so instead the chip-select
  14. * rows for each respective channel are laid out one after another,
  15. * the first half belonging to channel 0, the second half belonging
  16. * to channel 1.
  17. *
  18. * This driver is for DDR2 DIMMs, and it uses chip select to select among the
  19. * several ranks. However, instead of showing memories as ranks, it outputs
  20. * them as DIMM's. An internal table creates the association between ranks
  21. * and DIMM's.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/pci.h>
  26. #include <linux/pci_ids.h>
  27. #include <linux/edac.h>
  28. #include <linux/delay.h>
  29. #include <linux/mmzone.h>
  30. #include "edac_core.h"
  31. /* register addresses */
  32. /* device 16, func 1 */
  33. #define I5100_MC 0x40 /* Memory Control Register */
  34. #define I5100_MC_SCRBEN_MASK (1 << 7)
  35. #define I5100_MC_SCRBDONE_MASK (1 << 4)
  36. #define I5100_MS 0x44 /* Memory Status Register */
  37. #define I5100_SPDDATA 0x48 /* Serial Presence Detect Status Reg */
  38. #define I5100_SPDCMD 0x4c /* Serial Presence Detect Command Reg */
  39. #define I5100_TOLM 0x6c /* Top of Low Memory */
  40. #define I5100_MIR0 0x80 /* Memory Interleave Range 0 */
  41. #define I5100_MIR1 0x84 /* Memory Interleave Range 1 */
  42. #define I5100_AMIR_0 0x8c /* Adjusted Memory Interleave Range 0 */
  43. #define I5100_AMIR_1 0x90 /* Adjusted Memory Interleave Range 1 */
  44. #define I5100_FERR_NF_MEM 0xa0 /* MC First Non Fatal Errors */
  45. #define I5100_FERR_NF_MEM_M16ERR_MASK (1 << 16)
  46. #define I5100_FERR_NF_MEM_M15ERR_MASK (1 << 15)
  47. #define I5100_FERR_NF_MEM_M14ERR_MASK (1 << 14)
  48. #define I5100_FERR_NF_MEM_M12ERR_MASK (1 << 12)
  49. #define I5100_FERR_NF_MEM_M11ERR_MASK (1 << 11)
  50. #define I5100_FERR_NF_MEM_M10ERR_MASK (1 << 10)
  51. #define I5100_FERR_NF_MEM_M6ERR_MASK (1 << 6)
  52. #define I5100_FERR_NF_MEM_M5ERR_MASK (1 << 5)
  53. #define I5100_FERR_NF_MEM_M4ERR_MASK (1 << 4)
  54. #define I5100_FERR_NF_MEM_M1ERR_MASK (1 << 1)
  55. #define I5100_FERR_NF_MEM_ANY_MASK \
  56. (I5100_FERR_NF_MEM_M16ERR_MASK | \
  57. I5100_FERR_NF_MEM_M15ERR_MASK | \
  58. I5100_FERR_NF_MEM_M14ERR_MASK | \
  59. I5100_FERR_NF_MEM_M12ERR_MASK | \
  60. I5100_FERR_NF_MEM_M11ERR_MASK | \
  61. I5100_FERR_NF_MEM_M10ERR_MASK | \
  62. I5100_FERR_NF_MEM_M6ERR_MASK | \
  63. I5100_FERR_NF_MEM_M5ERR_MASK | \
  64. I5100_FERR_NF_MEM_M4ERR_MASK | \
  65. I5100_FERR_NF_MEM_M1ERR_MASK)
  66. #define I5100_NERR_NF_MEM 0xa4 /* MC Next Non-Fatal Errors */
  67. #define I5100_EMASK_MEM 0xa8 /* MC Error Mask Register */
  68. /* device 21 and 22, func 0 */
  69. #define I5100_MTR_0 0x154 /* Memory Technology Registers 0-3 */
  70. #define I5100_DMIR 0x15c /* DIMM Interleave Range */
  71. #define I5100_VALIDLOG 0x18c /* Valid Log Markers */
  72. #define I5100_NRECMEMA 0x190 /* Non-Recoverable Memory Error Log Reg A */
  73. #define I5100_NRECMEMB 0x194 /* Non-Recoverable Memory Error Log Reg B */
  74. #define I5100_REDMEMA 0x198 /* Recoverable Memory Data Error Log Reg A */
  75. #define I5100_REDMEMB 0x19c /* Recoverable Memory Data Error Log Reg B */
  76. #define I5100_RECMEMA 0x1a0 /* Recoverable Memory Error Log Reg A */
  77. #define I5100_RECMEMB 0x1a4 /* Recoverable Memory Error Log Reg B */
  78. #define I5100_MTR_4 0x1b0 /* Memory Technology Registers 4,5 */
  79. /* bit field accessors */
  80. static inline u32 i5100_mc_scrben(u32 mc)
  81. {
  82. return mc >> 7 & 1;
  83. }
  84. static inline u32 i5100_mc_errdeten(u32 mc)
  85. {
  86. return mc >> 5 & 1;
  87. }
  88. static inline u32 i5100_mc_scrbdone(u32 mc)
  89. {
  90. return mc >> 4 & 1;
  91. }
  92. static inline u16 i5100_spddata_rdo(u16 a)
  93. {
  94. return a >> 15 & 1;
  95. }
  96. static inline u16 i5100_spddata_sbe(u16 a)
  97. {
  98. return a >> 13 & 1;
  99. }
  100. static inline u16 i5100_spddata_busy(u16 a)
  101. {
  102. return a >> 12 & 1;
  103. }
  104. static inline u16 i5100_spddata_data(u16 a)
  105. {
  106. return a & ((1 << 8) - 1);
  107. }
  108. static inline u32 i5100_spdcmd_create(u32 dti, u32 ckovrd, u32 sa, u32 ba,
  109. u32 data, u32 cmd)
  110. {
  111. return ((dti & ((1 << 4) - 1)) << 28) |
  112. ((ckovrd & 1) << 27) |
  113. ((sa & ((1 << 3) - 1)) << 24) |
  114. ((ba & ((1 << 8) - 1)) << 16) |
  115. ((data & ((1 << 8) - 1)) << 8) |
  116. (cmd & 1);
  117. }
  118. static inline u16 i5100_tolm_tolm(u16 a)
  119. {
  120. return a >> 12 & ((1 << 4) - 1);
  121. }
  122. static inline u16 i5100_mir_limit(u16 a)
  123. {
  124. return a >> 4 & ((1 << 12) - 1);
  125. }
  126. static inline u16 i5100_mir_way1(u16 a)
  127. {
  128. return a >> 1 & 1;
  129. }
  130. static inline u16 i5100_mir_way0(u16 a)
  131. {
  132. return a & 1;
  133. }
  134. static inline u32 i5100_ferr_nf_mem_chan_indx(u32 a)
  135. {
  136. return a >> 28 & 1;
  137. }
  138. static inline u32 i5100_ferr_nf_mem_any(u32 a)
  139. {
  140. return a & I5100_FERR_NF_MEM_ANY_MASK;
  141. }
  142. static inline u32 i5100_nerr_nf_mem_any(u32 a)
  143. {
  144. return i5100_ferr_nf_mem_any(a);
  145. }
  146. static inline u32 i5100_dmir_limit(u32 a)
  147. {
  148. return a >> 16 & ((1 << 11) - 1);
  149. }
  150. static inline u32 i5100_dmir_rank(u32 a, u32 i)
  151. {
  152. return a >> (4 * i) & ((1 << 2) - 1);
  153. }
  154. static inline u16 i5100_mtr_present(u16 a)
  155. {
  156. return a >> 10 & 1;
  157. }
  158. static inline u16 i5100_mtr_ethrottle(u16 a)
  159. {
  160. return a >> 9 & 1;
  161. }
  162. static inline u16 i5100_mtr_width(u16 a)
  163. {
  164. return a >> 8 & 1;
  165. }
  166. static inline u16 i5100_mtr_numbank(u16 a)
  167. {
  168. return a >> 6 & 1;
  169. }
  170. static inline u16 i5100_mtr_numrow(u16 a)
  171. {
  172. return a >> 2 & ((1 << 2) - 1);
  173. }
  174. static inline u16 i5100_mtr_numcol(u16 a)
  175. {
  176. return a & ((1 << 2) - 1);
  177. }
  178. static inline u32 i5100_validlog_redmemvalid(u32 a)
  179. {
  180. return a >> 2 & 1;
  181. }
  182. static inline u32 i5100_validlog_recmemvalid(u32 a)
  183. {
  184. return a >> 1 & 1;
  185. }
  186. static inline u32 i5100_validlog_nrecmemvalid(u32 a)
  187. {
  188. return a & 1;
  189. }
  190. static inline u32 i5100_nrecmema_merr(u32 a)
  191. {
  192. return a >> 15 & ((1 << 5) - 1);
  193. }
  194. static inline u32 i5100_nrecmema_bank(u32 a)
  195. {
  196. return a >> 12 & ((1 << 3) - 1);
  197. }
  198. static inline u32 i5100_nrecmema_rank(u32 a)
  199. {
  200. return a >> 8 & ((1 << 3) - 1);
  201. }
  202. static inline u32 i5100_nrecmema_dm_buf_id(u32 a)
  203. {
  204. return a & ((1 << 8) - 1);
  205. }
  206. static inline u32 i5100_nrecmemb_cas(u32 a)
  207. {
  208. return a >> 16 & ((1 << 13) - 1);
  209. }
  210. static inline u32 i5100_nrecmemb_ras(u32 a)
  211. {
  212. return a & ((1 << 16) - 1);
  213. }
  214. static inline u32 i5100_redmemb_ecc_locator(u32 a)
  215. {
  216. return a & ((1 << 18) - 1);
  217. }
  218. static inline u32 i5100_recmema_merr(u32 a)
  219. {
  220. return i5100_nrecmema_merr(a);
  221. }
  222. static inline u32 i5100_recmema_bank(u32 a)
  223. {
  224. return i5100_nrecmema_bank(a);
  225. }
  226. static inline u32 i5100_recmema_rank(u32 a)
  227. {
  228. return i5100_nrecmema_rank(a);
  229. }
  230. static inline u32 i5100_recmema_dm_buf_id(u32 a)
  231. {
  232. return i5100_nrecmema_dm_buf_id(a);
  233. }
  234. static inline u32 i5100_recmemb_cas(u32 a)
  235. {
  236. return i5100_nrecmemb_cas(a);
  237. }
  238. static inline u32 i5100_recmemb_ras(u32 a)
  239. {
  240. return i5100_nrecmemb_ras(a);
  241. }
  242. /* some generic limits */
  243. #define I5100_MAX_RANKS_PER_CHAN 6
  244. #define I5100_CHANNELS 2
  245. #define I5100_MAX_RANKS_PER_DIMM 4
  246. #define I5100_DIMM_ADDR_LINES (6 - 3) /* 64 bits / 8 bits per byte */
  247. #define I5100_MAX_DIMM_SLOTS_PER_CHAN 4
  248. #define I5100_MAX_RANK_INTERLEAVE 4
  249. #define I5100_MAX_DMIRS 5
  250. #define I5100_SCRUB_REFRESH_RATE (5 * 60 * HZ)
  251. struct i5100_priv {
  252. /* ranks on each dimm -- 0 maps to not present -- obtained via SPD */
  253. int dimm_numrank[I5100_CHANNELS][I5100_MAX_DIMM_SLOTS_PER_CHAN];
  254. /*
  255. * mainboard chip select map -- maps i5100 chip selects to
  256. * DIMM slot chip selects. In the case of only 4 ranks per
  257. * channel, the mapping is fairly obvious but not unique.
  258. * we map -1 -> NC and assume both channels use the same
  259. * map...
  260. *
  261. */
  262. int dimm_csmap[I5100_MAX_DIMM_SLOTS_PER_CHAN][I5100_MAX_RANKS_PER_DIMM];
  263. /* memory interleave range */
  264. struct {
  265. u64 limit;
  266. unsigned way[2];
  267. } mir[I5100_CHANNELS];
  268. /* adjusted memory interleave range register */
  269. unsigned amir[I5100_CHANNELS];
  270. /* dimm interleave range */
  271. struct {
  272. unsigned rank[I5100_MAX_RANK_INTERLEAVE];
  273. u64 limit;
  274. } dmir[I5100_CHANNELS][I5100_MAX_DMIRS];
  275. /* memory technology registers... */
  276. struct {
  277. unsigned present; /* 0 or 1 */
  278. unsigned ethrottle; /* 0 or 1 */
  279. unsigned width; /* 4 or 8 bits */
  280. unsigned numbank; /* 2 or 3 lines */
  281. unsigned numrow; /* 13 .. 16 lines */
  282. unsigned numcol; /* 11 .. 12 lines */
  283. } mtr[I5100_CHANNELS][I5100_MAX_RANKS_PER_CHAN];
  284. u64 tolm; /* top of low memory in bytes */
  285. unsigned ranksperchan; /* number of ranks per channel */
  286. struct pci_dev *mc; /* device 16 func 1 */
  287. struct pci_dev *einj; /* device 19 func 0 */
  288. struct pci_dev *ch0mm; /* device 21 func 0 */
  289. struct pci_dev *ch1mm; /* device 22 func 0 */
  290. struct delayed_work i5100_scrubbing;
  291. int scrub_enable;
  292. };
  293. /* map a rank/chan to a slot number on the mainboard */
  294. static int i5100_rank_to_slot(const struct mem_ctl_info *mci,
  295. int chan, int rank)
  296. {
  297. const struct i5100_priv *priv = mci->pvt_info;
  298. int i;
  299. for (i = 0; i < I5100_MAX_DIMM_SLOTS_PER_CHAN; i++) {
  300. int j;
  301. const int numrank = priv->dimm_numrank[chan][i];
  302. for (j = 0; j < numrank; j++)
  303. if (priv->dimm_csmap[i][j] == rank)
  304. return i * 2 + chan;
  305. }
  306. return -1;
  307. }
  308. static const char *i5100_err_msg(unsigned err)
  309. {
  310. static const char *merrs[] = {
  311. "unknown", /* 0 */
  312. "uncorrectable data ECC on replay", /* 1 */
  313. "unknown", /* 2 */
  314. "unknown", /* 3 */
  315. "aliased uncorrectable demand data ECC", /* 4 */
  316. "aliased uncorrectable spare-copy data ECC", /* 5 */
  317. "aliased uncorrectable patrol data ECC", /* 6 */
  318. "unknown", /* 7 */
  319. "unknown", /* 8 */
  320. "unknown", /* 9 */
  321. "non-aliased uncorrectable demand data ECC", /* 10 */
  322. "non-aliased uncorrectable spare-copy data ECC", /* 11 */
  323. "non-aliased uncorrectable patrol data ECC", /* 12 */
  324. "unknown", /* 13 */
  325. "correctable demand data ECC", /* 14 */
  326. "correctable spare-copy data ECC", /* 15 */
  327. "correctable patrol data ECC", /* 16 */
  328. "unknown", /* 17 */
  329. "SPD protocol error", /* 18 */
  330. "unknown", /* 19 */
  331. "spare copy initiated", /* 20 */
  332. "spare copy completed", /* 21 */
  333. };
  334. unsigned i;
  335. for (i = 0; i < ARRAY_SIZE(merrs); i++)
  336. if (1 << i & err)
  337. return merrs[i];
  338. return "none";
  339. }
  340. /* convert csrow index into a rank (per channel -- 0..5) */
  341. static int i5100_csrow_to_rank(const struct mem_ctl_info *mci, int csrow)
  342. {
  343. const struct i5100_priv *priv = mci->pvt_info;
  344. return csrow % priv->ranksperchan;
  345. }
  346. /* convert csrow index into a channel (0..1) */
  347. static int i5100_csrow_to_chan(const struct mem_ctl_info *mci, int csrow)
  348. {
  349. const struct i5100_priv *priv = mci->pvt_info;
  350. return csrow / priv->ranksperchan;
  351. }
  352. static void i5100_handle_ce(struct mem_ctl_info *mci,
  353. int chan,
  354. unsigned bank,
  355. unsigned rank,
  356. unsigned long syndrome,
  357. unsigned cas,
  358. unsigned ras,
  359. const char *msg)
  360. {
  361. char detail[80];
  362. /* Form out message */
  363. snprintf(detail, sizeof(detail),
  364. "bank %u, cas %u, ras %u\n",
  365. bank, cas, ras);
  366. edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, 1,
  367. 0, 0, syndrome,
  368. chan, rank, -1,
  369. msg, detail);
  370. }
  371. static void i5100_handle_ue(struct mem_ctl_info *mci,
  372. int chan,
  373. unsigned bank,
  374. unsigned rank,
  375. unsigned long syndrome,
  376. unsigned cas,
  377. unsigned ras,
  378. const char *msg)
  379. {
  380. char detail[80];
  381. /* Form out message */
  382. snprintf(detail, sizeof(detail),
  383. "bank %u, cas %u, ras %u\n",
  384. bank, cas, ras);
  385. edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci, 1,
  386. 0, 0, syndrome,
  387. chan, rank, -1,
  388. msg, detail);
  389. }
  390. static void i5100_read_log(struct mem_ctl_info *mci, int chan,
  391. u32 ferr, u32 nerr)
  392. {
  393. struct i5100_priv *priv = mci->pvt_info;
  394. struct pci_dev *pdev = (chan) ? priv->ch1mm : priv->ch0mm;
  395. u32 dw;
  396. u32 dw2;
  397. unsigned syndrome = 0;
  398. unsigned ecc_loc = 0;
  399. unsigned merr;
  400. unsigned bank;
  401. unsigned rank;
  402. unsigned cas;
  403. unsigned ras;
  404. pci_read_config_dword(pdev, I5100_VALIDLOG, &dw);
  405. if (i5100_validlog_redmemvalid(dw)) {
  406. pci_read_config_dword(pdev, I5100_REDMEMA, &dw2);
  407. syndrome = dw2;
  408. pci_read_config_dword(pdev, I5100_REDMEMB, &dw2);
  409. ecc_loc = i5100_redmemb_ecc_locator(dw2);
  410. }
  411. if (i5100_validlog_recmemvalid(dw)) {
  412. const char *msg;
  413. pci_read_config_dword(pdev, I5100_RECMEMA, &dw2);
  414. merr = i5100_recmema_merr(dw2);
  415. bank = i5100_recmema_bank(dw2);
  416. rank = i5100_recmema_rank(dw2);
  417. pci_read_config_dword(pdev, I5100_RECMEMB, &dw2);
  418. cas = i5100_recmemb_cas(dw2);
  419. ras = i5100_recmemb_ras(dw2);
  420. /* FIXME: not really sure if this is what merr is...
  421. */
  422. if (!merr)
  423. msg = i5100_err_msg(ferr);
  424. else
  425. msg = i5100_err_msg(nerr);
  426. i5100_handle_ce(mci, chan, bank, rank, syndrome, cas, ras, msg);
  427. }
  428. if (i5100_validlog_nrecmemvalid(dw)) {
  429. const char *msg;
  430. pci_read_config_dword(pdev, I5100_NRECMEMA, &dw2);
  431. merr = i5100_nrecmema_merr(dw2);
  432. bank = i5100_nrecmema_bank(dw2);
  433. rank = i5100_nrecmema_rank(dw2);
  434. pci_read_config_dword(pdev, I5100_NRECMEMB, &dw2);
  435. cas = i5100_nrecmemb_cas(dw2);
  436. ras = i5100_nrecmemb_ras(dw2);
  437. /* FIXME: not really sure if this is what merr is...
  438. */
  439. if (!merr)
  440. msg = i5100_err_msg(ferr);
  441. else
  442. msg = i5100_err_msg(nerr);
  443. i5100_handle_ue(mci, chan, bank, rank, syndrome, cas, ras, msg);
  444. }
  445. pci_write_config_dword(pdev, I5100_VALIDLOG, dw);
  446. }
  447. static void i5100_check_error(struct mem_ctl_info *mci)
  448. {
  449. struct i5100_priv *priv = mci->pvt_info;
  450. u32 dw, dw2;
  451. pci_read_config_dword(priv->mc, I5100_FERR_NF_MEM, &dw);
  452. if (i5100_ferr_nf_mem_any(dw)) {
  453. pci_read_config_dword(priv->mc, I5100_NERR_NF_MEM, &dw2);
  454. i5100_read_log(mci, i5100_ferr_nf_mem_chan_indx(dw),
  455. i5100_ferr_nf_mem_any(dw),
  456. i5100_nerr_nf_mem_any(dw2));
  457. pci_write_config_dword(priv->mc, I5100_NERR_NF_MEM, dw2);
  458. }
  459. pci_write_config_dword(priv->mc, I5100_FERR_NF_MEM, dw);
  460. }
  461. /* The i5100 chipset will scrub the entire memory once, then
  462. * set a done bit. Continuous scrubbing is achieved by enqueing
  463. * delayed work to a workqueue, checking every few minutes if
  464. * the scrubbing has completed and if so reinitiating it.
  465. */
  466. static void i5100_refresh_scrubbing(struct work_struct *work)
  467. {
  468. struct delayed_work *i5100_scrubbing = container_of(work,
  469. struct delayed_work,
  470. work);
  471. struct i5100_priv *priv = container_of(i5100_scrubbing,
  472. struct i5100_priv,
  473. i5100_scrubbing);
  474. u32 dw;
  475. pci_read_config_dword(priv->mc, I5100_MC, &dw);
  476. if (priv->scrub_enable) {
  477. pci_read_config_dword(priv->mc, I5100_MC, &dw);
  478. if (i5100_mc_scrbdone(dw)) {
  479. dw |= I5100_MC_SCRBEN_MASK;
  480. pci_write_config_dword(priv->mc, I5100_MC, dw);
  481. pci_read_config_dword(priv->mc, I5100_MC, &dw);
  482. }
  483. schedule_delayed_work(&(priv->i5100_scrubbing),
  484. I5100_SCRUB_REFRESH_RATE);
  485. }
  486. }
  487. /*
  488. * The bandwidth is based on experimentation, feel free to refine it.
  489. */
  490. static int i5100_set_scrub_rate(struct mem_ctl_info *mci, u32 bandwidth)
  491. {
  492. struct i5100_priv *priv = mci->pvt_info;
  493. u32 dw;
  494. pci_read_config_dword(priv->mc, I5100_MC, &dw);
  495. if (bandwidth) {
  496. priv->scrub_enable = 1;
  497. dw |= I5100_MC_SCRBEN_MASK;
  498. schedule_delayed_work(&(priv->i5100_scrubbing),
  499. I5100_SCRUB_REFRESH_RATE);
  500. } else {
  501. priv->scrub_enable = 0;
  502. dw &= ~I5100_MC_SCRBEN_MASK;
  503. cancel_delayed_work(&(priv->i5100_scrubbing));
  504. }
  505. pci_write_config_dword(priv->mc, I5100_MC, dw);
  506. pci_read_config_dword(priv->mc, I5100_MC, &dw);
  507. bandwidth = 5900000 * i5100_mc_scrben(dw);
  508. return bandwidth;
  509. }
  510. static int i5100_get_scrub_rate(struct mem_ctl_info *mci)
  511. {
  512. struct i5100_priv *priv = mci->pvt_info;
  513. u32 dw;
  514. pci_read_config_dword(priv->mc, I5100_MC, &dw);
  515. return 5900000 * i5100_mc_scrben(dw);
  516. }
  517. static struct pci_dev *pci_get_device_func(unsigned vendor,
  518. unsigned device,
  519. unsigned func)
  520. {
  521. struct pci_dev *ret = NULL;
  522. while (1) {
  523. ret = pci_get_device(vendor, device, ret);
  524. if (!ret)
  525. break;
  526. if (PCI_FUNC(ret->devfn) == func)
  527. break;
  528. }
  529. return ret;
  530. }
  531. static unsigned long i5100_npages(struct mem_ctl_info *mci, int csrow)
  532. {
  533. struct i5100_priv *priv = mci->pvt_info;
  534. const unsigned chan_rank = i5100_csrow_to_rank(mci, csrow);
  535. const unsigned chan = i5100_csrow_to_chan(mci, csrow);
  536. unsigned addr_lines;
  537. /* dimm present? */
  538. if (!priv->mtr[chan][chan_rank].present)
  539. return 0ULL;
  540. addr_lines =
  541. I5100_DIMM_ADDR_LINES +
  542. priv->mtr[chan][chan_rank].numcol +
  543. priv->mtr[chan][chan_rank].numrow +
  544. priv->mtr[chan][chan_rank].numbank;
  545. return (unsigned long)
  546. ((unsigned long long) (1ULL << addr_lines) / PAGE_SIZE);
  547. }
  548. static void i5100_init_mtr(struct mem_ctl_info *mci)
  549. {
  550. struct i5100_priv *priv = mci->pvt_info;
  551. struct pci_dev *mms[2] = { priv->ch0mm, priv->ch1mm };
  552. int i;
  553. for (i = 0; i < I5100_CHANNELS; i++) {
  554. int j;
  555. struct pci_dev *pdev = mms[i];
  556. for (j = 0; j < I5100_MAX_RANKS_PER_CHAN; j++) {
  557. const unsigned addr =
  558. (j < 4) ? I5100_MTR_0 + j * 2 :
  559. I5100_MTR_4 + (j - 4) * 2;
  560. u16 w;
  561. pci_read_config_word(pdev, addr, &w);
  562. priv->mtr[i][j].present = i5100_mtr_present(w);
  563. priv->mtr[i][j].ethrottle = i5100_mtr_ethrottle(w);
  564. priv->mtr[i][j].width = 4 + 4 * i5100_mtr_width(w);
  565. priv->mtr[i][j].numbank = 2 + i5100_mtr_numbank(w);
  566. priv->mtr[i][j].numrow = 13 + i5100_mtr_numrow(w);
  567. priv->mtr[i][j].numcol = 10 + i5100_mtr_numcol(w);
  568. }
  569. }
  570. }
  571. /*
  572. * FIXME: make this into a real i2c adapter (so that dimm-decode
  573. * will work)?
  574. */
  575. static int i5100_read_spd_byte(const struct mem_ctl_info *mci,
  576. u8 ch, u8 slot, u8 addr, u8 *byte)
  577. {
  578. struct i5100_priv *priv = mci->pvt_info;
  579. u16 w;
  580. unsigned long et;
  581. pci_read_config_word(priv->mc, I5100_SPDDATA, &w);
  582. if (i5100_spddata_busy(w))
  583. return -1;
  584. pci_write_config_dword(priv->mc, I5100_SPDCMD,
  585. i5100_spdcmd_create(0xa, 1, ch * 4 + slot, addr,
  586. 0, 0));
  587. /* wait up to 100ms */
  588. et = jiffies + HZ / 10;
  589. udelay(100);
  590. while (1) {
  591. pci_read_config_word(priv->mc, I5100_SPDDATA, &w);
  592. if (!i5100_spddata_busy(w))
  593. break;
  594. udelay(100);
  595. }
  596. if (!i5100_spddata_rdo(w) || i5100_spddata_sbe(w))
  597. return -1;
  598. *byte = i5100_spddata_data(w);
  599. return 0;
  600. }
  601. /*
  602. * fill dimm chip select map
  603. *
  604. * FIXME:
  605. * o not the only way to may chip selects to dimm slots
  606. * o investigate if there is some way to obtain this map from the bios
  607. */
  608. static void i5100_init_dimm_csmap(struct mem_ctl_info *mci)
  609. {
  610. struct i5100_priv *priv = mci->pvt_info;
  611. int i;
  612. for (i = 0; i < I5100_MAX_DIMM_SLOTS_PER_CHAN; i++) {
  613. int j;
  614. for (j = 0; j < I5100_MAX_RANKS_PER_DIMM; j++)
  615. priv->dimm_csmap[i][j] = -1; /* default NC */
  616. }
  617. /* only 2 chip selects per slot... */
  618. if (priv->ranksperchan == 4) {
  619. priv->dimm_csmap[0][0] = 0;
  620. priv->dimm_csmap[0][1] = 3;
  621. priv->dimm_csmap[1][0] = 1;
  622. priv->dimm_csmap[1][1] = 2;
  623. priv->dimm_csmap[2][0] = 2;
  624. priv->dimm_csmap[3][0] = 3;
  625. } else {
  626. priv->dimm_csmap[0][0] = 0;
  627. priv->dimm_csmap[0][1] = 1;
  628. priv->dimm_csmap[1][0] = 2;
  629. priv->dimm_csmap[1][1] = 3;
  630. priv->dimm_csmap[2][0] = 4;
  631. priv->dimm_csmap[2][1] = 5;
  632. }
  633. }
  634. static void i5100_init_dimm_layout(struct pci_dev *pdev,
  635. struct mem_ctl_info *mci)
  636. {
  637. struct i5100_priv *priv = mci->pvt_info;
  638. int i;
  639. for (i = 0; i < I5100_CHANNELS; i++) {
  640. int j;
  641. for (j = 0; j < I5100_MAX_DIMM_SLOTS_PER_CHAN; j++) {
  642. u8 rank;
  643. if (i5100_read_spd_byte(mci, i, j, 5, &rank) < 0)
  644. priv->dimm_numrank[i][j] = 0;
  645. else
  646. priv->dimm_numrank[i][j] = (rank & 3) + 1;
  647. }
  648. }
  649. i5100_init_dimm_csmap(mci);
  650. }
  651. static void i5100_init_interleaving(struct pci_dev *pdev,
  652. struct mem_ctl_info *mci)
  653. {
  654. u16 w;
  655. u32 dw;
  656. struct i5100_priv *priv = mci->pvt_info;
  657. struct pci_dev *mms[2] = { priv->ch0mm, priv->ch1mm };
  658. int i;
  659. pci_read_config_word(pdev, I5100_TOLM, &w);
  660. priv->tolm = (u64) i5100_tolm_tolm(w) * 256 * 1024 * 1024;
  661. pci_read_config_word(pdev, I5100_MIR0, &w);
  662. priv->mir[0].limit = (u64) i5100_mir_limit(w) << 28;
  663. priv->mir[0].way[1] = i5100_mir_way1(w);
  664. priv->mir[0].way[0] = i5100_mir_way0(w);
  665. pci_read_config_word(pdev, I5100_MIR1, &w);
  666. priv->mir[1].limit = (u64) i5100_mir_limit(w) << 28;
  667. priv->mir[1].way[1] = i5100_mir_way1(w);
  668. priv->mir[1].way[0] = i5100_mir_way0(w);
  669. pci_read_config_word(pdev, I5100_AMIR_0, &w);
  670. priv->amir[0] = w;
  671. pci_read_config_word(pdev, I5100_AMIR_1, &w);
  672. priv->amir[1] = w;
  673. for (i = 0; i < I5100_CHANNELS; i++) {
  674. int j;
  675. for (j = 0; j < 5; j++) {
  676. int k;
  677. pci_read_config_dword(mms[i], I5100_DMIR + j * 4, &dw);
  678. priv->dmir[i][j].limit =
  679. (u64) i5100_dmir_limit(dw) << 28;
  680. for (k = 0; k < I5100_MAX_RANKS_PER_DIMM; k++)
  681. priv->dmir[i][j].rank[k] =
  682. i5100_dmir_rank(dw, k);
  683. }
  684. }
  685. i5100_init_mtr(mci);
  686. }
  687. static void i5100_init_csrows(struct mem_ctl_info *mci)
  688. {
  689. int i;
  690. struct i5100_priv *priv = mci->pvt_info;
  691. for (i = 0; i < mci->tot_dimms; i++) {
  692. struct dimm_info *dimm;
  693. const unsigned long npages = i5100_npages(mci, i);
  694. const unsigned chan = i5100_csrow_to_chan(mci, i);
  695. const unsigned rank = i5100_csrow_to_rank(mci, i);
  696. if (!npages)
  697. continue;
  698. dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
  699. chan, rank, 0);
  700. dimm->nr_pages = npages;
  701. if (npages) {
  702. dimm->grain = 32;
  703. dimm->dtype = (priv->mtr[chan][rank].width == 4) ?
  704. DEV_X4 : DEV_X8;
  705. dimm->mtype = MEM_RDDR2;
  706. dimm->edac_mode = EDAC_SECDED;
  707. snprintf(dimm->label, sizeof(dimm->label),
  708. "DIMM%u",
  709. i5100_rank_to_slot(mci, chan, rank));
  710. }
  711. edac_dbg(2, "dimm channel %d, rank %d, size %ld\n",
  712. chan, rank, (long)PAGES_TO_MiB(npages));
  713. }
  714. }
  715. static int i5100_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  716. {
  717. int rc;
  718. struct mem_ctl_info *mci;
  719. struct edac_mc_layer layers[2];
  720. struct i5100_priv *priv;
  721. struct pci_dev *ch0mm, *ch1mm, *einj;
  722. int ret = 0;
  723. u32 dw;
  724. int ranksperch;
  725. if (PCI_FUNC(pdev->devfn) != 1)
  726. return -ENODEV;
  727. rc = pci_enable_device(pdev);
  728. if (rc < 0) {
  729. ret = rc;
  730. goto bail;
  731. }
  732. /* ECC enabled? */
  733. pci_read_config_dword(pdev, I5100_MC, &dw);
  734. if (!i5100_mc_errdeten(dw)) {
  735. printk(KERN_INFO "i5100_edac: ECC not enabled.\n");
  736. ret = -ENODEV;
  737. goto bail_pdev;
  738. }
  739. /* figure out how many ranks, from strapped state of 48GB_Mode input */
  740. pci_read_config_dword(pdev, I5100_MS, &dw);
  741. ranksperch = !!(dw & (1 << 8)) * 2 + 4;
  742. /* enable error reporting... */
  743. pci_read_config_dword(pdev, I5100_EMASK_MEM, &dw);
  744. dw &= ~I5100_FERR_NF_MEM_ANY_MASK;
  745. pci_write_config_dword(pdev, I5100_EMASK_MEM, dw);
  746. /* device 21, func 0, Channel 0 Memory Map, Error Flag/Mask, etc... */
  747. ch0mm = pci_get_device_func(PCI_VENDOR_ID_INTEL,
  748. PCI_DEVICE_ID_INTEL_5100_21, 0);
  749. if (!ch0mm) {
  750. ret = -ENODEV;
  751. goto bail_pdev;
  752. }
  753. rc = pci_enable_device(ch0mm);
  754. if (rc < 0) {
  755. ret = rc;
  756. goto bail_ch0;
  757. }
  758. /* device 22, func 0, Channel 1 Memory Map, Error Flag/Mask, etc... */
  759. ch1mm = pci_get_device_func(PCI_VENDOR_ID_INTEL,
  760. PCI_DEVICE_ID_INTEL_5100_22, 0);
  761. if (!ch1mm) {
  762. ret = -ENODEV;
  763. goto bail_disable_ch0;
  764. }
  765. rc = pci_enable_device(ch1mm);
  766. if (rc < 0) {
  767. ret = rc;
  768. goto bail_ch1;
  769. }
  770. layers[0].type = EDAC_MC_LAYER_CHANNEL;
  771. layers[0].size = 2;
  772. layers[0].is_virt_csrow = false;
  773. layers[1].type = EDAC_MC_LAYER_SLOT;
  774. layers[1].size = ranksperch;
  775. layers[1].is_virt_csrow = true;
  776. mci = edac_mc_alloc(0, ARRAY_SIZE(layers), layers,
  777. sizeof(*priv));
  778. if (!mci) {
  779. ret = -ENOMEM;
  780. goto bail_disable_ch1;
  781. }
  782. /* device 19, func 0, Error injection */
  783. einj = pci_get_device_func(PCI_VENDOR_ID_INTEL,
  784. PCI_DEVICE_ID_INTEL_5100_19, 0);
  785. if (!einj) {
  786. ret = -ENODEV;
  787. goto bail_einj;
  788. }
  789. rc = pci_enable_device(einj);
  790. if (rc < 0) {
  791. ret = rc;
  792. goto bail_disable_einj;
  793. }
  794. mci->pdev = &pdev->dev;
  795. priv = mci->pvt_info;
  796. priv->ranksperchan = ranksperch;
  797. priv->mc = pdev;
  798. priv->ch0mm = ch0mm;
  799. priv->ch1mm = ch1mm;
  800. priv->einj = einj;
  801. INIT_DELAYED_WORK(&(priv->i5100_scrubbing), i5100_refresh_scrubbing);
  802. /* If scrubbing was already enabled by the bios, start maintaining it */
  803. pci_read_config_dword(pdev, I5100_MC, &dw);
  804. if (i5100_mc_scrben(dw)) {
  805. priv->scrub_enable = 1;
  806. schedule_delayed_work(&(priv->i5100_scrubbing),
  807. I5100_SCRUB_REFRESH_RATE);
  808. }
  809. i5100_init_dimm_layout(pdev, mci);
  810. i5100_init_interleaving(pdev, mci);
  811. mci->mtype_cap = MEM_FLAG_FB_DDR2;
  812. mci->edac_ctl_cap = EDAC_FLAG_SECDED;
  813. mci->edac_cap = EDAC_FLAG_SECDED;
  814. mci->mod_name = "i5100_edac.c";
  815. mci->mod_ver = "not versioned";
  816. mci->ctl_name = "i5100";
  817. mci->dev_name = pci_name(pdev);
  818. mci->ctl_page_to_phys = NULL;
  819. mci->edac_check = i5100_check_error;
  820. mci->set_sdram_scrub_rate = i5100_set_scrub_rate;
  821. mci->get_sdram_scrub_rate = i5100_get_scrub_rate;
  822. i5100_init_csrows(mci);
  823. /* this strange construction seems to be in every driver, dunno why */
  824. switch (edac_op_state) {
  825. case EDAC_OPSTATE_POLL:
  826. case EDAC_OPSTATE_NMI:
  827. break;
  828. default:
  829. edac_op_state = EDAC_OPSTATE_POLL;
  830. break;
  831. }
  832. if (edac_mc_add_mc(mci)) {
  833. ret = -ENODEV;
  834. goto bail_scrub;
  835. }
  836. return ret;
  837. bail_scrub:
  838. priv->scrub_enable = 0;
  839. cancel_delayed_work_sync(&(priv->i5100_scrubbing));
  840. edac_mc_free(mci);
  841. bail_disable_einj:
  842. pci_disable_device(einj);
  843. bail_einj:
  844. pci_dev_put(einj);
  845. bail_disable_ch1:
  846. pci_disable_device(ch1mm);
  847. bail_ch1:
  848. pci_dev_put(ch1mm);
  849. bail_disable_ch0:
  850. pci_disable_device(ch0mm);
  851. bail_ch0:
  852. pci_dev_put(ch0mm);
  853. bail_pdev:
  854. pci_disable_device(pdev);
  855. bail:
  856. return ret;
  857. }
  858. static void i5100_remove_one(struct pci_dev *pdev)
  859. {
  860. struct mem_ctl_info *mci;
  861. struct i5100_priv *priv;
  862. mci = edac_mc_del_mc(&pdev->dev);
  863. if (!mci)
  864. return;
  865. priv = mci->pvt_info;
  866. priv->scrub_enable = 0;
  867. cancel_delayed_work_sync(&(priv->i5100_scrubbing));
  868. pci_disable_device(pdev);
  869. pci_disable_device(priv->ch0mm);
  870. pci_disable_device(priv->ch1mm);
  871. pci_disable_device(priv->einj);
  872. pci_dev_put(priv->ch0mm);
  873. pci_dev_put(priv->ch1mm);
  874. pci_dev_put(priv->einj);
  875. edac_mc_free(mci);
  876. }
  877. static DEFINE_PCI_DEVICE_TABLE(i5100_pci_tbl) = {
  878. /* Device 16, Function 0, Channel 0 Memory Map, Error Flag/Mask, ... */
  879. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5100_16) },
  880. { 0, }
  881. };
  882. MODULE_DEVICE_TABLE(pci, i5100_pci_tbl);
  883. static struct pci_driver i5100_driver = {
  884. .name = KBUILD_BASENAME,
  885. .probe = i5100_init_one,
  886. .remove = i5100_remove_one,
  887. .id_table = i5100_pci_tbl,
  888. };
  889. static int __init i5100_init(void)
  890. {
  891. int pci_rc;
  892. pci_rc = pci_register_driver(&i5100_driver);
  893. return (pci_rc < 0) ? pci_rc : 0;
  894. }
  895. static void __exit i5100_exit(void)
  896. {
  897. pci_unregister_driver(&i5100_driver);
  898. }
  899. module_init(i5100_init);
  900. module_exit(i5100_exit);
  901. MODULE_LICENSE("GPL");
  902. MODULE_AUTHOR
  903. ("Arthur Jones <ajones@riverbed.com>");
  904. MODULE_DESCRIPTION("MC Driver for Intel I5100 memory controllers");