i5100_edac.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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 *ch0mm; /* device 21 func 0 */
  288. struct pci_dev *ch1mm; /* device 22 func 0 */
  289. struct delayed_work i5100_scrubbing;
  290. int scrub_enable;
  291. };
  292. /* map a rank/chan to a slot number on the mainboard */
  293. static int i5100_rank_to_slot(const struct mem_ctl_info *mci,
  294. int chan, int rank)
  295. {
  296. const struct i5100_priv *priv = mci->pvt_info;
  297. int i;
  298. for (i = 0; i < I5100_MAX_DIMM_SLOTS_PER_CHAN; i++) {
  299. int j;
  300. const int numrank = priv->dimm_numrank[chan][i];
  301. for (j = 0; j < numrank; j++)
  302. if (priv->dimm_csmap[i][j] == rank)
  303. return i * 2 + chan;
  304. }
  305. return -1;
  306. }
  307. static const char *i5100_err_msg(unsigned err)
  308. {
  309. static const char *merrs[] = {
  310. "unknown", /* 0 */
  311. "uncorrectable data ECC on replay", /* 1 */
  312. "unknown", /* 2 */
  313. "unknown", /* 3 */
  314. "aliased uncorrectable demand data ECC", /* 4 */
  315. "aliased uncorrectable spare-copy data ECC", /* 5 */
  316. "aliased uncorrectable patrol data ECC", /* 6 */
  317. "unknown", /* 7 */
  318. "unknown", /* 8 */
  319. "unknown", /* 9 */
  320. "non-aliased uncorrectable demand data ECC", /* 10 */
  321. "non-aliased uncorrectable spare-copy data ECC", /* 11 */
  322. "non-aliased uncorrectable patrol data ECC", /* 12 */
  323. "unknown", /* 13 */
  324. "correctable demand data ECC", /* 14 */
  325. "correctable spare-copy data ECC", /* 15 */
  326. "correctable patrol data ECC", /* 16 */
  327. "unknown", /* 17 */
  328. "SPD protocol error", /* 18 */
  329. "unknown", /* 19 */
  330. "spare copy initiated", /* 20 */
  331. "spare copy completed", /* 21 */
  332. };
  333. unsigned i;
  334. for (i = 0; i < ARRAY_SIZE(merrs); i++)
  335. if (1 << i & err)
  336. return merrs[i];
  337. return "none";
  338. }
  339. /* convert csrow index into a rank (per channel -- 0..5) */
  340. static int i5100_csrow_to_rank(const struct mem_ctl_info *mci, int csrow)
  341. {
  342. const struct i5100_priv *priv = mci->pvt_info;
  343. return csrow % priv->ranksperchan;
  344. }
  345. /* convert csrow index into a channel (0..1) */
  346. static int i5100_csrow_to_chan(const struct mem_ctl_info *mci, int csrow)
  347. {
  348. const struct i5100_priv *priv = mci->pvt_info;
  349. return csrow / priv->ranksperchan;
  350. }
  351. static void i5100_handle_ce(struct mem_ctl_info *mci,
  352. int chan,
  353. unsigned bank,
  354. unsigned rank,
  355. unsigned long syndrome,
  356. unsigned cas,
  357. unsigned ras,
  358. const char *msg)
  359. {
  360. char detail[80];
  361. /* Form out message */
  362. snprintf(detail, sizeof(detail),
  363. "bank %u, cas %u, ras %u\n",
  364. bank, cas, ras);
  365. edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci,
  366. 0, 0, syndrome,
  367. chan, rank, -1,
  368. msg, detail, NULL);
  369. }
  370. static void i5100_handle_ue(struct mem_ctl_info *mci,
  371. int chan,
  372. unsigned bank,
  373. unsigned rank,
  374. unsigned long syndrome,
  375. unsigned cas,
  376. unsigned ras,
  377. const char *msg)
  378. {
  379. char detail[80];
  380. /* Form out message */
  381. snprintf(detail, sizeof(detail),
  382. "bank %u, cas %u, ras %u\n",
  383. bank, cas, ras);
  384. edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci,
  385. 0, 0, syndrome,
  386. chan, rank, -1,
  387. msg, detail, NULL);
  388. }
  389. static void i5100_read_log(struct mem_ctl_info *mci, int chan,
  390. u32 ferr, u32 nerr)
  391. {
  392. struct i5100_priv *priv = mci->pvt_info;
  393. struct pci_dev *pdev = (chan) ? priv->ch1mm : priv->ch0mm;
  394. u32 dw;
  395. u32 dw2;
  396. unsigned syndrome = 0;
  397. unsigned ecc_loc = 0;
  398. unsigned merr;
  399. unsigned bank;
  400. unsigned rank;
  401. unsigned cas;
  402. unsigned ras;
  403. pci_read_config_dword(pdev, I5100_VALIDLOG, &dw);
  404. if (i5100_validlog_redmemvalid(dw)) {
  405. pci_read_config_dword(pdev, I5100_REDMEMA, &dw2);
  406. syndrome = dw2;
  407. pci_read_config_dword(pdev, I5100_REDMEMB, &dw2);
  408. ecc_loc = i5100_redmemb_ecc_locator(dw2);
  409. }
  410. if (i5100_validlog_recmemvalid(dw)) {
  411. const char *msg;
  412. pci_read_config_dword(pdev, I5100_RECMEMA, &dw2);
  413. merr = i5100_recmema_merr(dw2);
  414. bank = i5100_recmema_bank(dw2);
  415. rank = i5100_recmema_rank(dw2);
  416. pci_read_config_dword(pdev, I5100_RECMEMB, &dw2);
  417. cas = i5100_recmemb_cas(dw2);
  418. ras = i5100_recmemb_ras(dw2);
  419. /* FIXME: not really sure if this is what merr is...
  420. */
  421. if (!merr)
  422. msg = i5100_err_msg(ferr);
  423. else
  424. msg = i5100_err_msg(nerr);
  425. i5100_handle_ce(mci, chan, bank, rank, syndrome, cas, ras, msg);
  426. }
  427. if (i5100_validlog_nrecmemvalid(dw)) {
  428. const char *msg;
  429. pci_read_config_dword(pdev, I5100_NRECMEMA, &dw2);
  430. merr = i5100_nrecmema_merr(dw2);
  431. bank = i5100_nrecmema_bank(dw2);
  432. rank = i5100_nrecmema_rank(dw2);
  433. pci_read_config_dword(pdev, I5100_NRECMEMB, &dw2);
  434. cas = i5100_nrecmemb_cas(dw2);
  435. ras = i5100_nrecmemb_ras(dw2);
  436. /* FIXME: not really sure if this is what merr is...
  437. */
  438. if (!merr)
  439. msg = i5100_err_msg(ferr);
  440. else
  441. msg = i5100_err_msg(nerr);
  442. i5100_handle_ue(mci, chan, bank, rank, syndrome, cas, ras, msg);
  443. }
  444. pci_write_config_dword(pdev, I5100_VALIDLOG, dw);
  445. }
  446. static void i5100_check_error(struct mem_ctl_info *mci)
  447. {
  448. struct i5100_priv *priv = mci->pvt_info;
  449. u32 dw, dw2;
  450. pci_read_config_dword(priv->mc, I5100_FERR_NF_MEM, &dw);
  451. if (i5100_ferr_nf_mem_any(dw)) {
  452. pci_read_config_dword(priv->mc, I5100_NERR_NF_MEM, &dw2);
  453. i5100_read_log(mci, i5100_ferr_nf_mem_chan_indx(dw),
  454. i5100_ferr_nf_mem_any(dw),
  455. i5100_nerr_nf_mem_any(dw2));
  456. pci_write_config_dword(priv->mc, I5100_NERR_NF_MEM, dw2);
  457. }
  458. pci_write_config_dword(priv->mc, I5100_FERR_NF_MEM, dw);
  459. }
  460. /* The i5100 chipset will scrub the entire memory once, then
  461. * set a done bit. Continuous scrubbing is achieved by enqueing
  462. * delayed work to a workqueue, checking every few minutes if
  463. * the scrubbing has completed and if so reinitiating it.
  464. */
  465. static void i5100_refresh_scrubbing(struct work_struct *work)
  466. {
  467. struct delayed_work *i5100_scrubbing = container_of(work,
  468. struct delayed_work,
  469. work);
  470. struct i5100_priv *priv = container_of(i5100_scrubbing,
  471. struct i5100_priv,
  472. i5100_scrubbing);
  473. u32 dw;
  474. pci_read_config_dword(priv->mc, I5100_MC, &dw);
  475. if (priv->scrub_enable) {
  476. pci_read_config_dword(priv->mc, I5100_MC, &dw);
  477. if (i5100_mc_scrbdone(dw)) {
  478. dw |= I5100_MC_SCRBEN_MASK;
  479. pci_write_config_dword(priv->mc, I5100_MC, dw);
  480. pci_read_config_dword(priv->mc, I5100_MC, &dw);
  481. }
  482. schedule_delayed_work(&(priv->i5100_scrubbing),
  483. I5100_SCRUB_REFRESH_RATE);
  484. }
  485. }
  486. /*
  487. * The bandwidth is based on experimentation, feel free to refine it.
  488. */
  489. static int i5100_set_scrub_rate(struct mem_ctl_info *mci, u32 bandwidth)
  490. {
  491. struct i5100_priv *priv = mci->pvt_info;
  492. u32 dw;
  493. pci_read_config_dword(priv->mc, I5100_MC, &dw);
  494. if (bandwidth) {
  495. priv->scrub_enable = 1;
  496. dw |= I5100_MC_SCRBEN_MASK;
  497. schedule_delayed_work(&(priv->i5100_scrubbing),
  498. I5100_SCRUB_REFRESH_RATE);
  499. } else {
  500. priv->scrub_enable = 0;
  501. dw &= ~I5100_MC_SCRBEN_MASK;
  502. cancel_delayed_work(&(priv->i5100_scrubbing));
  503. }
  504. pci_write_config_dword(priv->mc, I5100_MC, dw);
  505. pci_read_config_dword(priv->mc, I5100_MC, &dw);
  506. bandwidth = 5900000 * i5100_mc_scrben(dw);
  507. return bandwidth;
  508. }
  509. static int i5100_get_scrub_rate(struct mem_ctl_info *mci)
  510. {
  511. struct i5100_priv *priv = mci->pvt_info;
  512. u32 dw;
  513. pci_read_config_dword(priv->mc, I5100_MC, &dw);
  514. return 5900000 * i5100_mc_scrben(dw);
  515. }
  516. static struct pci_dev *pci_get_device_func(unsigned vendor,
  517. unsigned device,
  518. unsigned func)
  519. {
  520. struct pci_dev *ret = NULL;
  521. while (1) {
  522. ret = pci_get_device(vendor, device, ret);
  523. if (!ret)
  524. break;
  525. if (PCI_FUNC(ret->devfn) == func)
  526. break;
  527. }
  528. return ret;
  529. }
  530. static unsigned long __devinit i5100_npages(struct mem_ctl_info *mci,
  531. 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 __devinit 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 __devinit 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 __devinit 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 __devinit 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 __devinit 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. debugf2("dimm channel %d, rank %d, size %ld\n",
  712. chan, rank, (long)PAGES_TO_MiB(npages));
  713. }
  714. }
  715. static int __devinit i5100_init_one(struct pci_dev *pdev,
  716. const struct pci_device_id *id)
  717. {
  718. int rc;
  719. struct mem_ctl_info *mci;
  720. struct edac_mc_layer layers[2];
  721. struct i5100_priv *priv;
  722. struct pci_dev *ch0mm, *ch1mm;
  723. int ret = 0;
  724. u32 dw;
  725. int ranksperch;
  726. if (PCI_FUNC(pdev->devfn) != 1)
  727. return -ENODEV;
  728. rc = pci_enable_device(pdev);
  729. if (rc < 0) {
  730. ret = rc;
  731. goto bail;
  732. }
  733. /* ECC enabled? */
  734. pci_read_config_dword(pdev, I5100_MC, &dw);
  735. if (!i5100_mc_errdeten(dw)) {
  736. printk(KERN_INFO "i5100_edac: ECC not enabled.\n");
  737. ret = -ENODEV;
  738. goto bail_pdev;
  739. }
  740. /* figure out how many ranks, from strapped state of 48GB_Mode input */
  741. pci_read_config_dword(pdev, I5100_MS, &dw);
  742. ranksperch = !!(dw & (1 << 8)) * 2 + 4;
  743. /* enable error reporting... */
  744. pci_read_config_dword(pdev, I5100_EMASK_MEM, &dw);
  745. dw &= ~I5100_FERR_NF_MEM_ANY_MASK;
  746. pci_write_config_dword(pdev, I5100_EMASK_MEM, dw);
  747. /* device 21, func 0, Channel 0 Memory Map, Error Flag/Mask, etc... */
  748. ch0mm = pci_get_device_func(PCI_VENDOR_ID_INTEL,
  749. PCI_DEVICE_ID_INTEL_5100_21, 0);
  750. if (!ch0mm) {
  751. ret = -ENODEV;
  752. goto bail_pdev;
  753. }
  754. rc = pci_enable_device(ch0mm);
  755. if (rc < 0) {
  756. ret = rc;
  757. goto bail_ch0;
  758. }
  759. /* device 22, func 0, Channel 1 Memory Map, Error Flag/Mask, etc... */
  760. ch1mm = pci_get_device_func(PCI_VENDOR_ID_INTEL,
  761. PCI_DEVICE_ID_INTEL_5100_22, 0);
  762. if (!ch1mm) {
  763. ret = -ENODEV;
  764. goto bail_disable_ch0;
  765. }
  766. rc = pci_enable_device(ch1mm);
  767. if (rc < 0) {
  768. ret = rc;
  769. goto bail_ch1;
  770. }
  771. layers[0].type = EDAC_MC_LAYER_CHANNEL;
  772. layers[0].size = 2;
  773. layers[0].is_virt_csrow = false;
  774. layers[1].type = EDAC_MC_LAYER_SLOT;
  775. layers[1].size = ranksperch;
  776. layers[1].is_virt_csrow = true;
  777. mci = edac_mc_alloc(0, ARRAY_SIZE(layers), layers,
  778. sizeof(*priv));
  779. if (!mci) {
  780. ret = -ENOMEM;
  781. goto bail_disable_ch1;
  782. }
  783. mci->dev = &pdev->dev;
  784. priv = mci->pvt_info;
  785. priv->ranksperchan = ranksperch;
  786. priv->mc = pdev;
  787. priv->ch0mm = ch0mm;
  788. priv->ch1mm = ch1mm;
  789. INIT_DELAYED_WORK(&(priv->i5100_scrubbing), i5100_refresh_scrubbing);
  790. /* If scrubbing was already enabled by the bios, start maintaining it */
  791. pci_read_config_dword(pdev, I5100_MC, &dw);
  792. if (i5100_mc_scrben(dw)) {
  793. priv->scrub_enable = 1;
  794. schedule_delayed_work(&(priv->i5100_scrubbing),
  795. I5100_SCRUB_REFRESH_RATE);
  796. }
  797. i5100_init_dimm_layout(pdev, mci);
  798. i5100_init_interleaving(pdev, mci);
  799. mci->mtype_cap = MEM_FLAG_FB_DDR2;
  800. mci->edac_ctl_cap = EDAC_FLAG_SECDED;
  801. mci->edac_cap = EDAC_FLAG_SECDED;
  802. mci->mod_name = "i5100_edac.c";
  803. mci->mod_ver = "not versioned";
  804. mci->ctl_name = "i5100";
  805. mci->dev_name = pci_name(pdev);
  806. mci->ctl_page_to_phys = NULL;
  807. mci->edac_check = i5100_check_error;
  808. mci->set_sdram_scrub_rate = i5100_set_scrub_rate;
  809. mci->get_sdram_scrub_rate = i5100_get_scrub_rate;
  810. i5100_init_csrows(mci);
  811. /* this strange construction seems to be in every driver, dunno why */
  812. switch (edac_op_state) {
  813. case EDAC_OPSTATE_POLL:
  814. case EDAC_OPSTATE_NMI:
  815. break;
  816. default:
  817. edac_op_state = EDAC_OPSTATE_POLL;
  818. break;
  819. }
  820. if (edac_mc_add_mc(mci)) {
  821. ret = -ENODEV;
  822. goto bail_scrub;
  823. }
  824. return ret;
  825. bail_scrub:
  826. priv->scrub_enable = 0;
  827. cancel_delayed_work_sync(&(priv->i5100_scrubbing));
  828. edac_mc_free(mci);
  829. bail_disable_ch1:
  830. pci_disable_device(ch1mm);
  831. bail_ch1:
  832. pci_dev_put(ch1mm);
  833. bail_disable_ch0:
  834. pci_disable_device(ch0mm);
  835. bail_ch0:
  836. pci_dev_put(ch0mm);
  837. bail_pdev:
  838. pci_disable_device(pdev);
  839. bail:
  840. return ret;
  841. }
  842. static void __devexit i5100_remove_one(struct pci_dev *pdev)
  843. {
  844. struct mem_ctl_info *mci;
  845. struct i5100_priv *priv;
  846. mci = edac_mc_del_mc(&pdev->dev);
  847. if (!mci)
  848. return;
  849. priv = mci->pvt_info;
  850. priv->scrub_enable = 0;
  851. cancel_delayed_work_sync(&(priv->i5100_scrubbing));
  852. pci_disable_device(pdev);
  853. pci_disable_device(priv->ch0mm);
  854. pci_disable_device(priv->ch1mm);
  855. pci_dev_put(priv->ch0mm);
  856. pci_dev_put(priv->ch1mm);
  857. edac_mc_free(mci);
  858. }
  859. static DEFINE_PCI_DEVICE_TABLE(i5100_pci_tbl) = {
  860. /* Device 16, Function 0, Channel 0 Memory Map, Error Flag/Mask, ... */
  861. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5100_16) },
  862. { 0, }
  863. };
  864. MODULE_DEVICE_TABLE(pci, i5100_pci_tbl);
  865. static struct pci_driver i5100_driver = {
  866. .name = KBUILD_BASENAME,
  867. .probe = i5100_init_one,
  868. .remove = __devexit_p(i5100_remove_one),
  869. .id_table = i5100_pci_tbl,
  870. };
  871. static int __init i5100_init(void)
  872. {
  873. int pci_rc;
  874. pci_rc = pci_register_driver(&i5100_driver);
  875. return (pci_rc < 0) ? pci_rc : 0;
  876. }
  877. static void __exit i5100_exit(void)
  878. {
  879. pci_unregister_driver(&i5100_driver);
  880. }
  881. module_init(i5100_init);
  882. module_exit(i5100_exit);
  883. MODULE_LICENSE("GPL");
  884. MODULE_AUTHOR
  885. ("Arthur Jones <ajones@riverbed.com>");
  886. MODULE_DESCRIPTION("MC Driver for Intel I5100 memory controllers");