i5100_edac.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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, 1,
  366. 0, 0, syndrome,
  367. chan, rank, -1,
  368. msg, detail);
  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, 1,
  385. 0, 0, syndrome,
  386. chan, rank, -1,
  387. msg, detail);
  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 i5100_npages(struct mem_ctl_info *mci, int csrow)
  531. {
  532. struct i5100_priv *priv = mci->pvt_info;
  533. const unsigned chan_rank = i5100_csrow_to_rank(mci, csrow);
  534. const unsigned chan = i5100_csrow_to_chan(mci, csrow);
  535. unsigned addr_lines;
  536. /* dimm present? */
  537. if (!priv->mtr[chan][chan_rank].present)
  538. return 0ULL;
  539. addr_lines =
  540. I5100_DIMM_ADDR_LINES +
  541. priv->mtr[chan][chan_rank].numcol +
  542. priv->mtr[chan][chan_rank].numrow +
  543. priv->mtr[chan][chan_rank].numbank;
  544. return (unsigned long)
  545. ((unsigned long long) (1ULL << addr_lines) / PAGE_SIZE);
  546. }
  547. static void i5100_init_mtr(struct mem_ctl_info *mci)
  548. {
  549. struct i5100_priv *priv = mci->pvt_info;
  550. struct pci_dev *mms[2] = { priv->ch0mm, priv->ch1mm };
  551. int i;
  552. for (i = 0; i < I5100_CHANNELS; i++) {
  553. int j;
  554. struct pci_dev *pdev = mms[i];
  555. for (j = 0; j < I5100_MAX_RANKS_PER_CHAN; j++) {
  556. const unsigned addr =
  557. (j < 4) ? I5100_MTR_0 + j * 2 :
  558. I5100_MTR_4 + (j - 4) * 2;
  559. u16 w;
  560. pci_read_config_word(pdev, addr, &w);
  561. priv->mtr[i][j].present = i5100_mtr_present(w);
  562. priv->mtr[i][j].ethrottle = i5100_mtr_ethrottle(w);
  563. priv->mtr[i][j].width = 4 + 4 * i5100_mtr_width(w);
  564. priv->mtr[i][j].numbank = 2 + i5100_mtr_numbank(w);
  565. priv->mtr[i][j].numrow = 13 + i5100_mtr_numrow(w);
  566. priv->mtr[i][j].numcol = 10 + i5100_mtr_numcol(w);
  567. }
  568. }
  569. }
  570. /*
  571. * FIXME: make this into a real i2c adapter (so that dimm-decode
  572. * will work)?
  573. */
  574. static int i5100_read_spd_byte(const struct mem_ctl_info *mci,
  575. u8 ch, u8 slot, u8 addr, u8 *byte)
  576. {
  577. struct i5100_priv *priv = mci->pvt_info;
  578. u16 w;
  579. unsigned long et;
  580. pci_read_config_word(priv->mc, I5100_SPDDATA, &w);
  581. if (i5100_spddata_busy(w))
  582. return -1;
  583. pci_write_config_dword(priv->mc, I5100_SPDCMD,
  584. i5100_spdcmd_create(0xa, 1, ch * 4 + slot, addr,
  585. 0, 0));
  586. /* wait up to 100ms */
  587. et = jiffies + HZ / 10;
  588. udelay(100);
  589. while (1) {
  590. pci_read_config_word(priv->mc, I5100_SPDDATA, &w);
  591. if (!i5100_spddata_busy(w))
  592. break;
  593. udelay(100);
  594. }
  595. if (!i5100_spddata_rdo(w) || i5100_spddata_sbe(w))
  596. return -1;
  597. *byte = i5100_spddata_data(w);
  598. return 0;
  599. }
  600. /*
  601. * fill dimm chip select map
  602. *
  603. * FIXME:
  604. * o not the only way to may chip selects to dimm slots
  605. * o investigate if there is some way to obtain this map from the bios
  606. */
  607. static void i5100_init_dimm_csmap(struct mem_ctl_info *mci)
  608. {
  609. struct i5100_priv *priv = mci->pvt_info;
  610. int i;
  611. for (i = 0; i < I5100_MAX_DIMM_SLOTS_PER_CHAN; i++) {
  612. int j;
  613. for (j = 0; j < I5100_MAX_RANKS_PER_DIMM; j++)
  614. priv->dimm_csmap[i][j] = -1; /* default NC */
  615. }
  616. /* only 2 chip selects per slot... */
  617. if (priv->ranksperchan == 4) {
  618. priv->dimm_csmap[0][0] = 0;
  619. priv->dimm_csmap[0][1] = 3;
  620. priv->dimm_csmap[1][0] = 1;
  621. priv->dimm_csmap[1][1] = 2;
  622. priv->dimm_csmap[2][0] = 2;
  623. priv->dimm_csmap[3][0] = 3;
  624. } else {
  625. priv->dimm_csmap[0][0] = 0;
  626. priv->dimm_csmap[0][1] = 1;
  627. priv->dimm_csmap[1][0] = 2;
  628. priv->dimm_csmap[1][1] = 3;
  629. priv->dimm_csmap[2][0] = 4;
  630. priv->dimm_csmap[2][1] = 5;
  631. }
  632. }
  633. static void i5100_init_dimm_layout(struct pci_dev *pdev,
  634. struct mem_ctl_info *mci)
  635. {
  636. struct i5100_priv *priv = mci->pvt_info;
  637. int i;
  638. for (i = 0; i < I5100_CHANNELS; i++) {
  639. int j;
  640. for (j = 0; j < I5100_MAX_DIMM_SLOTS_PER_CHAN; j++) {
  641. u8 rank;
  642. if (i5100_read_spd_byte(mci, i, j, 5, &rank) < 0)
  643. priv->dimm_numrank[i][j] = 0;
  644. else
  645. priv->dimm_numrank[i][j] = (rank & 3) + 1;
  646. }
  647. }
  648. i5100_init_dimm_csmap(mci);
  649. }
  650. static void i5100_init_interleaving(struct pci_dev *pdev,
  651. struct mem_ctl_info *mci)
  652. {
  653. u16 w;
  654. u32 dw;
  655. struct i5100_priv *priv = mci->pvt_info;
  656. struct pci_dev *mms[2] = { priv->ch0mm, priv->ch1mm };
  657. int i;
  658. pci_read_config_word(pdev, I5100_TOLM, &w);
  659. priv->tolm = (u64) i5100_tolm_tolm(w) * 256 * 1024 * 1024;
  660. pci_read_config_word(pdev, I5100_MIR0, &w);
  661. priv->mir[0].limit = (u64) i5100_mir_limit(w) << 28;
  662. priv->mir[0].way[1] = i5100_mir_way1(w);
  663. priv->mir[0].way[0] = i5100_mir_way0(w);
  664. pci_read_config_word(pdev, I5100_MIR1, &w);
  665. priv->mir[1].limit = (u64) i5100_mir_limit(w) << 28;
  666. priv->mir[1].way[1] = i5100_mir_way1(w);
  667. priv->mir[1].way[0] = i5100_mir_way0(w);
  668. pci_read_config_word(pdev, I5100_AMIR_0, &w);
  669. priv->amir[0] = w;
  670. pci_read_config_word(pdev, I5100_AMIR_1, &w);
  671. priv->amir[1] = w;
  672. for (i = 0; i < I5100_CHANNELS; i++) {
  673. int j;
  674. for (j = 0; j < 5; j++) {
  675. int k;
  676. pci_read_config_dword(mms[i], I5100_DMIR + j * 4, &dw);
  677. priv->dmir[i][j].limit =
  678. (u64) i5100_dmir_limit(dw) << 28;
  679. for (k = 0; k < I5100_MAX_RANKS_PER_DIMM; k++)
  680. priv->dmir[i][j].rank[k] =
  681. i5100_dmir_rank(dw, k);
  682. }
  683. }
  684. i5100_init_mtr(mci);
  685. }
  686. static void i5100_init_csrows(struct mem_ctl_info *mci)
  687. {
  688. int i;
  689. struct i5100_priv *priv = mci->pvt_info;
  690. for (i = 0; i < mci->tot_dimms; i++) {
  691. struct dimm_info *dimm;
  692. const unsigned long npages = i5100_npages(mci, i);
  693. const unsigned chan = i5100_csrow_to_chan(mci, i);
  694. const unsigned rank = i5100_csrow_to_rank(mci, i);
  695. if (!npages)
  696. continue;
  697. dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
  698. chan, rank, 0);
  699. dimm->nr_pages = npages;
  700. if (npages) {
  701. dimm->grain = 32;
  702. dimm->dtype = (priv->mtr[chan][rank].width == 4) ?
  703. DEV_X4 : DEV_X8;
  704. dimm->mtype = MEM_RDDR2;
  705. dimm->edac_mode = EDAC_SECDED;
  706. snprintf(dimm->label, sizeof(dimm->label),
  707. "DIMM%u",
  708. i5100_rank_to_slot(mci, chan, rank));
  709. }
  710. edac_dbg(2, "dimm channel %d, rank %d, size %ld\n",
  711. chan, rank, (long)PAGES_TO_MiB(npages));
  712. }
  713. }
  714. static int i5100_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  715. {
  716. int rc;
  717. struct mem_ctl_info *mci;
  718. struct edac_mc_layer layers[2];
  719. struct i5100_priv *priv;
  720. struct pci_dev *ch0mm, *ch1mm;
  721. int ret = 0;
  722. u32 dw;
  723. int ranksperch;
  724. if (PCI_FUNC(pdev->devfn) != 1)
  725. return -ENODEV;
  726. rc = pci_enable_device(pdev);
  727. if (rc < 0) {
  728. ret = rc;
  729. goto bail;
  730. }
  731. /* ECC enabled? */
  732. pci_read_config_dword(pdev, I5100_MC, &dw);
  733. if (!i5100_mc_errdeten(dw)) {
  734. printk(KERN_INFO "i5100_edac: ECC not enabled.\n");
  735. ret = -ENODEV;
  736. goto bail_pdev;
  737. }
  738. /* figure out how many ranks, from strapped state of 48GB_Mode input */
  739. pci_read_config_dword(pdev, I5100_MS, &dw);
  740. ranksperch = !!(dw & (1 << 8)) * 2 + 4;
  741. /* enable error reporting... */
  742. pci_read_config_dword(pdev, I5100_EMASK_MEM, &dw);
  743. dw &= ~I5100_FERR_NF_MEM_ANY_MASK;
  744. pci_write_config_dword(pdev, I5100_EMASK_MEM, dw);
  745. /* device 21, func 0, Channel 0 Memory Map, Error Flag/Mask, etc... */
  746. ch0mm = pci_get_device_func(PCI_VENDOR_ID_INTEL,
  747. PCI_DEVICE_ID_INTEL_5100_21, 0);
  748. if (!ch0mm) {
  749. ret = -ENODEV;
  750. goto bail_pdev;
  751. }
  752. rc = pci_enable_device(ch0mm);
  753. if (rc < 0) {
  754. ret = rc;
  755. goto bail_ch0;
  756. }
  757. /* device 22, func 0, Channel 1 Memory Map, Error Flag/Mask, etc... */
  758. ch1mm = pci_get_device_func(PCI_VENDOR_ID_INTEL,
  759. PCI_DEVICE_ID_INTEL_5100_22, 0);
  760. if (!ch1mm) {
  761. ret = -ENODEV;
  762. goto bail_disable_ch0;
  763. }
  764. rc = pci_enable_device(ch1mm);
  765. if (rc < 0) {
  766. ret = rc;
  767. goto bail_ch1;
  768. }
  769. layers[0].type = EDAC_MC_LAYER_CHANNEL;
  770. layers[0].size = 2;
  771. layers[0].is_virt_csrow = false;
  772. layers[1].type = EDAC_MC_LAYER_SLOT;
  773. layers[1].size = ranksperch;
  774. layers[1].is_virt_csrow = true;
  775. mci = edac_mc_alloc(0, ARRAY_SIZE(layers), layers,
  776. sizeof(*priv));
  777. if (!mci) {
  778. ret = -ENOMEM;
  779. goto bail_disable_ch1;
  780. }
  781. mci->pdev = &pdev->dev;
  782. priv = mci->pvt_info;
  783. priv->ranksperchan = ranksperch;
  784. priv->mc = pdev;
  785. priv->ch0mm = ch0mm;
  786. priv->ch1mm = ch1mm;
  787. INIT_DELAYED_WORK(&(priv->i5100_scrubbing), i5100_refresh_scrubbing);
  788. /* If scrubbing was already enabled by the bios, start maintaining it */
  789. pci_read_config_dword(pdev, I5100_MC, &dw);
  790. if (i5100_mc_scrben(dw)) {
  791. priv->scrub_enable = 1;
  792. schedule_delayed_work(&(priv->i5100_scrubbing),
  793. I5100_SCRUB_REFRESH_RATE);
  794. }
  795. i5100_init_dimm_layout(pdev, mci);
  796. i5100_init_interleaving(pdev, mci);
  797. mci->mtype_cap = MEM_FLAG_FB_DDR2;
  798. mci->edac_ctl_cap = EDAC_FLAG_SECDED;
  799. mci->edac_cap = EDAC_FLAG_SECDED;
  800. mci->mod_name = "i5100_edac.c";
  801. mci->mod_ver = "not versioned";
  802. mci->ctl_name = "i5100";
  803. mci->dev_name = pci_name(pdev);
  804. mci->ctl_page_to_phys = NULL;
  805. mci->edac_check = i5100_check_error;
  806. mci->set_sdram_scrub_rate = i5100_set_scrub_rate;
  807. mci->get_sdram_scrub_rate = i5100_get_scrub_rate;
  808. i5100_init_csrows(mci);
  809. /* this strange construction seems to be in every driver, dunno why */
  810. switch (edac_op_state) {
  811. case EDAC_OPSTATE_POLL:
  812. case EDAC_OPSTATE_NMI:
  813. break;
  814. default:
  815. edac_op_state = EDAC_OPSTATE_POLL;
  816. break;
  817. }
  818. if (edac_mc_add_mc(mci)) {
  819. ret = -ENODEV;
  820. goto bail_scrub;
  821. }
  822. return ret;
  823. bail_scrub:
  824. priv->scrub_enable = 0;
  825. cancel_delayed_work_sync(&(priv->i5100_scrubbing));
  826. edac_mc_free(mci);
  827. bail_disable_ch1:
  828. pci_disable_device(ch1mm);
  829. bail_ch1:
  830. pci_dev_put(ch1mm);
  831. bail_disable_ch0:
  832. pci_disable_device(ch0mm);
  833. bail_ch0:
  834. pci_dev_put(ch0mm);
  835. bail_pdev:
  836. pci_disable_device(pdev);
  837. bail:
  838. return ret;
  839. }
  840. static void i5100_remove_one(struct pci_dev *pdev)
  841. {
  842. struct mem_ctl_info *mci;
  843. struct i5100_priv *priv;
  844. mci = edac_mc_del_mc(&pdev->dev);
  845. if (!mci)
  846. return;
  847. priv = mci->pvt_info;
  848. priv->scrub_enable = 0;
  849. cancel_delayed_work_sync(&(priv->i5100_scrubbing));
  850. pci_disable_device(pdev);
  851. pci_disable_device(priv->ch0mm);
  852. pci_disable_device(priv->ch1mm);
  853. pci_dev_put(priv->ch0mm);
  854. pci_dev_put(priv->ch1mm);
  855. edac_mc_free(mci);
  856. }
  857. static DEFINE_PCI_DEVICE_TABLE(i5100_pci_tbl) = {
  858. /* Device 16, Function 0, Channel 0 Memory Map, Error Flag/Mask, ... */
  859. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5100_16) },
  860. { 0, }
  861. };
  862. MODULE_DEVICE_TABLE(pci, i5100_pci_tbl);
  863. static struct pci_driver i5100_driver = {
  864. .name = KBUILD_BASENAME,
  865. .probe = i5100_init_one,
  866. .remove = i5100_remove_one,
  867. .id_table = i5100_pci_tbl,
  868. };
  869. static int __init i5100_init(void)
  870. {
  871. int pci_rc;
  872. pci_rc = pci_register_driver(&i5100_driver);
  873. return (pci_rc < 0) ? pci_rc : 0;
  874. }
  875. static void __exit i5100_exit(void)
  876. {
  877. pci_unregister_driver(&i5100_driver);
  878. }
  879. module_init(i5100_init);
  880. module_exit(i5100_exit);
  881. MODULE_LICENSE("GPL");
  882. MODULE_AUTHOR
  883. ("Arthur Jones <ajones@riverbed.com>");
  884. MODULE_DESCRIPTION("MC Driver for Intel I5100 memory controllers");