i5100_edac.c 26 KB

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