i7core_edac.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. /* Intel 7 core Memory Controller kernel module (Nehalem)
  2. *
  3. * This file may be distributed under the terms of the
  4. * GNU General Public License version 2 only.
  5. *
  6. * Copyright (c) 2009 by:
  7. * Mauro Carvalho Chehab <mchehab@redhat.com>
  8. *
  9. * Red Hat Inc. http://www.redhat.com
  10. *
  11. * Forked and adapted from the i5400_edac driver
  12. *
  13. * Based on the following public Intel datasheets:
  14. * Intel Core i7 Processor Extreme Edition and Intel Core i7 Processor
  15. * Datasheet, Volume 2:
  16. * http://download.intel.com/design/processor/datashts/320835.pdf
  17. * Intel Xeon Processor 5500 Series Datasheet Volume 2
  18. * http://www.intel.com/Assets/PDF/datasheet/321322.pdf
  19. * also available at:
  20. * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
  21. */
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/pci.h>
  25. #include <linux/pci_ids.h>
  26. #include <linux/slab.h>
  27. #include <linux/edac.h>
  28. #include <linux/mmzone.h>
  29. #include "edac_core.h"
  30. /* To use the new pci_[read/write]_config_qword instead of two dword */
  31. #define USE_QWORD 1
  32. /*
  33. * Alter this version for the module when modifications are made
  34. */
  35. #define I7CORE_REVISION " Ver: 1.0.0 " __DATE__
  36. #define EDAC_MOD_STR "i7core_edac"
  37. /* HACK: temporary, just to enable all logs, for now */
  38. #undef debugf0
  39. #define debugf0(fmt, arg...) edac_printk(KERN_INFO, "i7core", fmt, ##arg)
  40. /*
  41. * Debug macros
  42. */
  43. #define i7core_printk(level, fmt, arg...) \
  44. edac_printk(level, "i7core", fmt, ##arg)
  45. #define i7core_mc_printk(mci, level, fmt, arg...) \
  46. edac_mc_chipset_printk(mci, level, "i7core", fmt, ##arg)
  47. /*
  48. * i7core Memory Controller Registers
  49. */
  50. /* OFFSETS for Device 3 Function 0 */
  51. #define MC_CONTROL 0x48
  52. #define MC_STATUS 0x4c
  53. #define MC_MAX_DOD 0x64
  54. /* OFFSETS for Devices 4,5 and 6 Function 0 */
  55. #define MC_CHANNEL_DIMM_INIT_PARAMS 0x58
  56. #define THREE_DIMMS_PRESENT (1 << 24)
  57. #define SINGLE_QUAD_RANK_PRESENT (1 << 23)
  58. #define QUAD_RANK_PRESENT (1 << 22)
  59. #define REGISTERED_DIMM (1 << 15)
  60. #define MC_CHANNEL_MAPPER 0x60
  61. #define RDLCH(r, ch) ((((r) >> (3 + (ch * 6))) & 0x07) - 1)
  62. #define WRLCH(r, ch) ((((r) >> (ch * 6)) & 0x07) - 1)
  63. #define MC_CHANNEL_RANK_PRESENT 0x7c
  64. #define RANK_PRESENT_MASK 0xffff
  65. #define MC_CHANNEL_ADDR_MATCH 0xf0
  66. #define MC_CHANNEL_ERROR_MASK 0xf8
  67. #define MC_CHANNEL_ERROR_INJECT 0xfc
  68. #define INJECT_ADDR_PARITY 0x10
  69. #define INJECT_ECC 0x08
  70. #define MASK_CACHELINE 0x06
  71. #define MASK_FULL_CACHELINE 0x06
  72. #define MASK_MSB32_CACHELINE 0x04
  73. #define MASK_LSB32_CACHELINE 0x02
  74. #define NO_MASK_CACHELINE 0x00
  75. #define REPEAT_EN 0x01
  76. /* OFFSETS for Devices 4,5 and 6 Function 1 */
  77. #define MC_DOD_CH_DIMM0 0x48
  78. #define MC_DOD_CH_DIMM1 0x4c
  79. #define MC_DOD_CH_DIMM2 0x50
  80. #define RANKOFFSET_MASK ((1 << 12) | (1 << 11) | (1 << 10))
  81. #define RANKOFFSET(x) ((x & RANKOFFSET_MASK) >> 10)
  82. #define DIMM_PRESENT_MASK (1 << 9)
  83. #define DIMM_PRESENT(x) (((x) & DIMM_PRESENT_MASK) >> 9)
  84. #define NUMBANK_MASK ((1 << 8) | (1 << 7))
  85. #define NUMBANK(x) (((x) & NUMBANK_MASK) >> 7)
  86. #define NUMRANK_MASK ((1 << 6) | (1 << 5))
  87. #define NUMRANK(x) (((x) & NUMRANK_MASK) >> 5)
  88. #define NUMROW_MASK ((1 << 4) | (1 << 3))
  89. #define NUMROW(x) (((x) & NUMROW_MASK) >> 3)
  90. #define NUMCOL_MASK 3
  91. #define NUMCOL(x) ((x) & NUMCOL_MASK)
  92. #define MC_RANK_PRESENT 0x7c
  93. #define MC_SAG_CH_0 0x80
  94. #define MC_SAG_CH_1 0x84
  95. #define MC_SAG_CH_2 0x88
  96. #define MC_SAG_CH_3 0x8c
  97. #define MC_SAG_CH_4 0x90
  98. #define MC_SAG_CH_5 0x94
  99. #define MC_SAG_CH_6 0x98
  100. #define MC_SAG_CH_7 0x9c
  101. #define MC_RIR_LIMIT_CH_0 0x40
  102. #define MC_RIR_LIMIT_CH_1 0x44
  103. #define MC_RIR_LIMIT_CH_2 0x48
  104. #define MC_RIR_LIMIT_CH_3 0x4C
  105. #define MC_RIR_LIMIT_CH_4 0x50
  106. #define MC_RIR_LIMIT_CH_5 0x54
  107. #define MC_RIR_LIMIT_CH_6 0x58
  108. #define MC_RIR_LIMIT_CH_7 0x5C
  109. #define MC_RIR_LIMIT_MASK ((1 << 10) - 1)
  110. #define MC_RIR_WAY_CH 0x80
  111. #define MC_RIR_WAY_OFFSET_MASK (((1 << 14) - 1) & ~0x7)
  112. #define MC_RIR_WAY_RANK_MASK 0x7
  113. /*
  114. * i7core structs
  115. */
  116. #define NUM_CHANS 3
  117. #define NUM_MCR_FUNCS 4
  118. #define NUM_CHAN_FUNCS 3
  119. struct i7core_info {
  120. u32 mc_control;
  121. u32 mc_status;
  122. u32 max_dod;
  123. u32 ch_map;
  124. };
  125. struct i7core_inject {
  126. int enable;
  127. u32 section;
  128. u32 type;
  129. u32 eccmask;
  130. /* Error address mask */
  131. int channel, dimm, rank, bank, page, col;
  132. };
  133. struct i7core_channel {
  134. u32 ranks;
  135. u32 dimms;
  136. };
  137. struct pci_id_descr {
  138. int dev;
  139. int func;
  140. int dev_id;
  141. struct pci_dev *pdev;
  142. };
  143. struct i7core_pvt {
  144. struct pci_dev *pci_mcr[NUM_MCR_FUNCS];
  145. struct pci_dev *pci_ch[NUM_CHANS][NUM_CHAN_FUNCS];
  146. struct i7core_info info;
  147. struct i7core_inject inject;
  148. struct i7core_channel channel[NUM_CHANS];
  149. };
  150. /* Device name and register DID (Device ID) */
  151. struct i7core_dev_info {
  152. const char *ctl_name; /* name for this device */
  153. u16 fsb_mapping_errors; /* DID for the branchmap,control */
  154. };
  155. #define PCI_DESCR(device, function, device_id) \
  156. .dev = (device), \
  157. .func = (function), \
  158. .dev_id = (device_id)
  159. struct pci_id_descr pci_devs[] = {
  160. /* Memory controller */
  161. { PCI_DESCR(3, 0, PCI_DEVICE_ID_INTEL_I7_MCR) },
  162. { PCI_DESCR(3, 1, PCI_DEVICE_ID_INTEL_I7_MC_TAD) },
  163. { PCI_DESCR(3, 2, PCI_DEVICE_ID_INTEL_I7_MC_RAS) }, /* if RDIMM is supported */
  164. { PCI_DESCR(3, 4, PCI_DEVICE_ID_INTEL_I7_MC_TEST) },
  165. /* Channel 0 */
  166. { PCI_DESCR(4, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH0_CTRL) },
  167. { PCI_DESCR(4, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH0_ADDR) },
  168. { PCI_DESCR(4, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH0_RANK) },
  169. { PCI_DESCR(4, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH0_TC) },
  170. /* Channel 1 */
  171. { PCI_DESCR(5, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH1_CTRL) },
  172. { PCI_DESCR(5, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH1_ADDR) },
  173. { PCI_DESCR(5, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH1_RANK) },
  174. { PCI_DESCR(5, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH1_TC) },
  175. /* Channel 2 */
  176. { PCI_DESCR(6, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH2_CTRL) },
  177. { PCI_DESCR(6, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH2_ADDR) },
  178. { PCI_DESCR(6, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH2_RANK) },
  179. { PCI_DESCR(6, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH2_TC) },
  180. };
  181. #define N_DEVS ARRAY_SIZE(pci_devs)
  182. /*
  183. * pci_device_id table for which devices we are looking for
  184. * This should match the first device at pci_devs table
  185. */
  186. static const struct pci_device_id i7core_pci_tbl[] __devinitdata = {
  187. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I7_MCR)},
  188. {0,} /* 0 terminated list. */
  189. };
  190. /* Table of devices attributes supported by this driver */
  191. static const struct i7core_dev_info i7core_devs[] = {
  192. {
  193. .ctl_name = "i7 Core",
  194. .fsb_mapping_errors = PCI_DEVICE_ID_INTEL_I7_MCR,
  195. },
  196. };
  197. static struct edac_pci_ctl_info *i7core_pci;
  198. /****************************************************************************
  199. Anciliary status routines
  200. ****************************************************************************/
  201. /* MC_CONTROL bits */
  202. #define CH_ACTIVE(pvt, ch) ((pvt)->info.mc_control & 1 << (8 + ch))
  203. #define ECCx8(pvt) ((pvt)->info.mc_control & 1 << 1)
  204. /* MC_STATUS bits */
  205. #define ECC_ENABLED(pvt) ((pvt)->info.mc_status & 1 << 3)
  206. #define CH_DISABLED(pvt, ch) ((pvt)->info.mc_status & 1 << ch)
  207. /* MC_MAX_DOD read functions */
  208. static inline int maxnumdimms(struct i7core_pvt *pvt)
  209. {
  210. return (pvt->info.max_dod & 0x3) + 1;
  211. }
  212. static inline int maxnumrank(struct i7core_pvt *pvt)
  213. {
  214. static int ranks[4] = { 1, 2, 4, -EINVAL };
  215. return ranks[(pvt->info.max_dod >> 2) & 0x3];
  216. }
  217. static inline int maxnumbank(struct i7core_pvt *pvt)
  218. {
  219. static int banks[4] = { 4, 8, 16, -EINVAL };
  220. return banks[(pvt->info.max_dod >> 4) & 0x3];
  221. }
  222. static inline int maxnumrow(struct i7core_pvt *pvt)
  223. {
  224. static int rows[8] = {
  225. 1 << 12, 1 << 13, 1 << 14, 1 << 15,
  226. 1 << 16, -EINVAL, -EINVAL, -EINVAL,
  227. };
  228. return rows[((pvt->info.max_dod >> 6) & 0x7)];
  229. }
  230. static inline int maxnumcol(struct i7core_pvt *pvt)
  231. {
  232. static int cols[8] = {
  233. 1 << 10, 1 << 11, 1 << 12, -EINVAL,
  234. };
  235. return cols[((pvt->info.max_dod >> 9) & 0x3) << 12];
  236. }
  237. /****************************************************************************
  238. Memory check routines
  239. ****************************************************************************/
  240. static int get_dimm_config(struct mem_ctl_info *mci)
  241. {
  242. struct i7core_pvt *pvt = mci->pvt_info;
  243. int i;
  244. if (!pvt->pci_mcr[0])
  245. return -ENODEV;
  246. /* Device 3 function 0 reads */
  247. pci_read_config_dword(pvt->pci_mcr[0], MC_CONTROL,
  248. &pvt->info.mc_control);
  249. pci_read_config_dword(pvt->pci_mcr[0], MC_STATUS,
  250. &pvt->info.mc_status);
  251. pci_read_config_dword(pvt->pci_mcr[0], MC_MAX_DOD,
  252. &pvt->info.max_dod);
  253. pci_read_config_dword(pvt->pci_mcr[0], MC_CHANNEL_MAPPER,
  254. &pvt->info.ch_map);
  255. debugf0("MC control=0x%08x status=0x%08x dod=0x%08x map=0x%08x\n",
  256. pvt->info.mc_control, pvt->info.mc_status,
  257. pvt->info.max_dod, pvt->info.ch_map);
  258. if (ECC_ENABLED(pvt))
  259. debugf0("ECC enabled with x%d SDCC\n", ECCx8(pvt)?8:4);
  260. else
  261. debugf0("ECC disabled\n");
  262. /* FIXME: need to handle the error codes */
  263. debugf0("DOD Maximum limits: DIMMS: %d, %d-ranked, %d-banked\n",
  264. maxnumdimms(pvt), maxnumrank(pvt), maxnumbank(pvt));
  265. debugf0("DOD Maximum rows x colums = 0x%x x 0x%x\n",
  266. maxnumrow(pvt), maxnumcol(pvt));
  267. debugf0("Memory channel configuration:\n");
  268. for (i = 0; i < NUM_CHANS; i++) {
  269. u32 data;
  270. if (!CH_ACTIVE(pvt, i)) {
  271. debugf0("Channel %i is not active\n", i);
  272. continue;
  273. }
  274. if (CH_DISABLED(pvt, i)) {
  275. debugf0("Channel %i is disabled\n", i);
  276. continue;
  277. }
  278. /* Devices 4-6 function 0 */
  279. pci_read_config_dword(pvt->pci_ch[i][0],
  280. MC_CHANNEL_DIMM_INIT_PARAMS, &data);
  281. pvt->channel[i].ranks = (data & QUAD_RANK_PRESENT)? 4 : 2;
  282. if (data & THREE_DIMMS_PRESENT)
  283. pvt->channel[i].dimms = 3;
  284. else if (data & SINGLE_QUAD_RANK_PRESENT)
  285. pvt->channel[i].dimms = 1;
  286. else
  287. pvt->channel[i].dimms = 2;
  288. debugf0("Ch%d (0x%08x): rd ch %d, wr ch %d, "
  289. "%d ranks, %d %cDIMMs\n",
  290. i, data,
  291. RDLCH(pvt->info.ch_map, i),
  292. WRLCH(pvt->info.ch_map, i),
  293. pvt->channel[i].ranks, pvt->channel[i].dimms,
  294. (data & REGISTERED_DIMM)? 'R' : 'U' );
  295. }
  296. return 0;
  297. }
  298. /****************************************************************************
  299. Error insertion routines
  300. ****************************************************************************/
  301. /* The i7core has independent error injection features per channel.
  302. However, to have a simpler code, we don't allow enabling error injection
  303. on more than one channel.
  304. Also, since a change at an inject parameter will be applied only at enable,
  305. we're disabling error injection on all write calls to the sysfs nodes that
  306. controls the error code injection.
  307. */
  308. static int disable_inject(struct mem_ctl_info *mci)
  309. {
  310. struct i7core_pvt *pvt = mci->pvt_info;
  311. pvt->inject.enable = 0;
  312. if (!pvt->pci_ch[pvt->inject.channel][0])
  313. return -ENODEV;
  314. pci_write_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  315. MC_CHANNEL_ERROR_MASK, 0);
  316. return 0;
  317. }
  318. /*
  319. * i7core inject inject.section
  320. *
  321. * accept and store error injection inject.section value
  322. * bit 0 - refers to the lower 32-byte half cacheline
  323. * bit 1 - refers to the upper 32-byte half cacheline
  324. */
  325. static ssize_t i7core_inject_section_store(struct mem_ctl_info *mci,
  326. const char *data, size_t count)
  327. {
  328. struct i7core_pvt *pvt = mci->pvt_info;
  329. unsigned long value;
  330. int rc;
  331. if (pvt->inject.enable)
  332. disable_inject(mci);
  333. rc = strict_strtoul(data, 10, &value);
  334. if ((rc < 0) || (value > 3))
  335. return 0;
  336. pvt->inject.section = (u32) value;
  337. return count;
  338. }
  339. static ssize_t i7core_inject_section_show(struct mem_ctl_info *mci,
  340. char *data)
  341. {
  342. struct i7core_pvt *pvt = mci->pvt_info;
  343. return sprintf(data, "0x%08x\n", pvt->inject.section);
  344. }
  345. /*
  346. * i7core inject.type
  347. *
  348. * accept and store error injection inject.section value
  349. * bit 0 - repeat enable - Enable error repetition
  350. * bit 1 - inject ECC error
  351. * bit 2 - inject parity error
  352. */
  353. static ssize_t i7core_inject_type_store(struct mem_ctl_info *mci,
  354. const char *data, size_t count)
  355. {
  356. struct i7core_pvt *pvt = mci->pvt_info;
  357. unsigned long value;
  358. int rc;
  359. if (pvt->inject.enable)
  360. disable_inject(mci);
  361. rc = strict_strtoul(data, 10, &value);
  362. if ((rc < 0) || (value > 7))
  363. return 0;
  364. pvt->inject.type = (u32) value;
  365. return count;
  366. }
  367. static ssize_t i7core_inject_type_show(struct mem_ctl_info *mci,
  368. char *data)
  369. {
  370. struct i7core_pvt *pvt = mci->pvt_info;
  371. return sprintf(data, "0x%08x\n", pvt->inject.type);
  372. }
  373. /*
  374. * i7core_inject_inject.eccmask_store
  375. *
  376. * The type of error (UE/CE) will depend on the inject.eccmask value:
  377. * Any bits set to a 1 will flip the corresponding ECC bit
  378. * Correctable errors can be injected by flipping 1 bit or the bits within
  379. * a symbol pair (2 consecutive aligned 8-bit pairs - i.e. 7:0 and 15:8 or
  380. * 23:16 and 31:24). Flipping bits in two symbol pairs will cause an
  381. * uncorrectable error to be injected.
  382. */
  383. static ssize_t i7core_inject_eccmask_store(struct mem_ctl_info *mci,
  384. const char *data, size_t count)
  385. {
  386. struct i7core_pvt *pvt = mci->pvt_info;
  387. unsigned long value;
  388. int rc;
  389. if (pvt->inject.enable)
  390. disable_inject(mci);
  391. rc = strict_strtoul(data, 10, &value);
  392. if (rc < 0)
  393. return 0;
  394. pvt->inject.eccmask = (u32) value;
  395. return count;
  396. }
  397. static ssize_t i7core_inject_eccmask_show(struct mem_ctl_info *mci,
  398. char *data)
  399. {
  400. struct i7core_pvt *pvt = mci->pvt_info;
  401. return sprintf(data, "0x%08x\n", pvt->inject.eccmask);
  402. }
  403. /*
  404. * i7core_addrmatch
  405. *
  406. * The type of error (UE/CE) will depend on the inject.eccmask value:
  407. * Any bits set to a 1 will flip the corresponding ECC bit
  408. * Correctable errors can be injected by flipping 1 bit or the bits within
  409. * a symbol pair (2 consecutive aligned 8-bit pairs - i.e. 7:0 and 15:8 or
  410. * 23:16 and 31:24). Flipping bits in two symbol pairs will cause an
  411. * uncorrectable error to be injected.
  412. */
  413. static ssize_t i7core_inject_addrmatch_store(struct mem_ctl_info *mci,
  414. const char *data, size_t count)
  415. {
  416. struct i7core_pvt *pvt = mci->pvt_info;
  417. char *cmd, *val;
  418. long value;
  419. int rc;
  420. if (pvt->inject.enable)
  421. disable_inject(mci);
  422. do {
  423. cmd = strsep((char **) &data, ":");
  424. if (!cmd)
  425. break;
  426. val = strsep((char **) &data, " \n\t");
  427. if (!val)
  428. return cmd - data;
  429. if (!strcasecmp(val,"any"))
  430. value = -1;
  431. else {
  432. rc = strict_strtol(val, 10, &value);
  433. if ((rc < 0) || (value < 0))
  434. return cmd - data;
  435. }
  436. if (!strcasecmp(cmd,"channel")) {
  437. if (value < 3)
  438. pvt->inject.channel = value;
  439. else
  440. return cmd - data;
  441. } else if (!strcasecmp(cmd,"dimm")) {
  442. if (value < 4)
  443. pvt->inject.dimm = value;
  444. else
  445. return cmd - data;
  446. } else if (!strcasecmp(cmd,"rank")) {
  447. if (value < 4)
  448. pvt->inject.rank = value;
  449. else
  450. return cmd - data;
  451. } else if (!strcasecmp(cmd,"bank")) {
  452. if (value < 4)
  453. pvt->inject.bank = value;
  454. else
  455. return cmd - data;
  456. } else if (!strcasecmp(cmd,"page")) {
  457. if (value <= 0xffff)
  458. pvt->inject.page = value;
  459. else
  460. return cmd - data;
  461. } else if (!strcasecmp(cmd,"col") ||
  462. !strcasecmp(cmd,"column")) {
  463. if (value <= 0x3fff)
  464. pvt->inject.col = value;
  465. else
  466. return cmd - data;
  467. }
  468. } while (1);
  469. return count;
  470. }
  471. static ssize_t i7core_inject_addrmatch_show(struct mem_ctl_info *mci,
  472. char *data)
  473. {
  474. struct i7core_pvt *pvt = mci->pvt_info;
  475. char channel[4], dimm[4], bank[4], rank[4], page[7], col[7];
  476. if (pvt->inject.channel < 0)
  477. sprintf(channel, "any");
  478. else
  479. sprintf(channel, "%d", pvt->inject.channel);
  480. if (pvt->inject.dimm < 0)
  481. sprintf(dimm, "any");
  482. else
  483. sprintf(dimm, "%d", pvt->inject.dimm);
  484. if (pvt->inject.bank < 0)
  485. sprintf(bank, "any");
  486. else
  487. sprintf(bank, "%d", pvt->inject.bank);
  488. if (pvt->inject.rank < 0)
  489. sprintf(rank, "any");
  490. else
  491. sprintf(rank, "%d", pvt->inject.rank);
  492. if (pvt->inject.page < 0)
  493. sprintf(page, "any");
  494. else
  495. sprintf(page, "0x%04x", pvt->inject.page);
  496. if (pvt->inject.col < 0)
  497. sprintf(col, "any");
  498. else
  499. sprintf(col, "0x%04x", pvt->inject.col);
  500. return sprintf(data, "channel: %s\ndimm: %s\nbank: %s\n"
  501. "rank: %s\npage: %s\ncolumn: %s\n",
  502. channel, dimm, bank, rank, page, col);
  503. }
  504. /*
  505. * This routine prepares the Memory Controller for error injection.
  506. * The error will be injected when some process tries to write to the
  507. * memory that matches the given criteria.
  508. * The criteria can be set in terms of a mask where dimm, rank, bank, page
  509. * and col can be specified.
  510. * A -1 value for any of the mask items will make the MCU to ignore
  511. * that matching criteria for error injection.
  512. *
  513. * It should be noticed that the error will only happen after a write operation
  514. * on a memory that matches the condition. if REPEAT_EN is not enabled at
  515. * inject mask, then it will produce just one error. Otherwise, it will repeat
  516. * until the injectmask would be cleaned.
  517. *
  518. * FIXME: This routine assumes that MAXNUMDIMMS value of MC_MAX_DOD
  519. * is reliable enough to check if the MC is using the
  520. * three channels. However, this is not clear at the datasheet.
  521. */
  522. static ssize_t i7core_inject_enable_store(struct mem_ctl_info *mci,
  523. const char *data, size_t count)
  524. {
  525. struct i7core_pvt *pvt = mci->pvt_info;
  526. u32 injectmask;
  527. u64 mask = 0;
  528. int rc;
  529. long enable;
  530. if (!pvt->pci_ch[pvt->inject.channel][0])
  531. return 0;
  532. rc = strict_strtoul(data, 10, &enable);
  533. if ((rc < 0))
  534. return 0;
  535. if (enable) {
  536. pvt->inject.enable = 1;
  537. } else {
  538. disable_inject(mci);
  539. return count;
  540. }
  541. /* Sets pvt->inject.dimm mask */
  542. if (pvt->inject.dimm < 0)
  543. mask |= 1L << 41;
  544. else {
  545. if (pvt->channel[pvt->inject.channel].dimms > 2)
  546. mask |= (pvt->inject.dimm & 0x3L) << 35;
  547. else
  548. mask |= (pvt->inject.dimm & 0x1L) << 36;
  549. }
  550. /* Sets pvt->inject.rank mask */
  551. if (pvt->inject.rank < 0)
  552. mask |= 1L << 40;
  553. else {
  554. if (pvt->channel[pvt->inject.channel].dimms > 2)
  555. mask |= (pvt->inject.rank & 0x1L) << 34;
  556. else
  557. mask |= (pvt->inject.rank & 0x3L) << 34;
  558. }
  559. /* Sets pvt->inject.bank mask */
  560. if (pvt->inject.bank < 0)
  561. mask |= 1L << 39;
  562. else
  563. mask |= (pvt->inject.bank & 0x15L) << 30;
  564. /* Sets pvt->inject.page mask */
  565. if (pvt->inject.page < 0)
  566. mask |= 1L << 38;
  567. else
  568. mask |= (pvt->inject.page & 0xffffL) << 14;
  569. /* Sets pvt->inject.column mask */
  570. if (pvt->inject.col < 0)
  571. mask |= 1L << 37;
  572. else
  573. mask |= (pvt->inject.col & 0x3fffL);
  574. #if USE_QWORD
  575. pci_write_config_qword(pvt->pci_ch[pvt->inject.channel][0],
  576. MC_CHANNEL_ADDR_MATCH, mask);
  577. #else
  578. pci_write_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  579. MC_CHANNEL_ADDR_MATCH, mask);
  580. pci_write_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  581. MC_CHANNEL_ADDR_MATCH + 4, mask >> 32L);
  582. #endif
  583. #if 1
  584. #if USE_QWORD
  585. u64 rdmask;
  586. pci_read_config_qword(pvt->pci_ch[pvt->inject.channel][0],
  587. MC_CHANNEL_ADDR_MATCH, &rdmask);
  588. debugf0("Inject addr match write 0x%016llx, read: 0x%016llx\n",
  589. mask, rdmask);
  590. #else
  591. u32 rdmask1, rdmask2;
  592. pci_read_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  593. MC_CHANNEL_ADDR_MATCH, &rdmask1);
  594. pci_read_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  595. MC_CHANNEL_ADDR_MATCH + 4, &rdmask2);
  596. debugf0("Inject addr match write 0x%016llx, read: 0x%08x%08x\n",
  597. mask, rdmask1, rdmask2);
  598. #endif
  599. #endif
  600. pci_write_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  601. MC_CHANNEL_ERROR_MASK, pvt->inject.eccmask);
  602. /*
  603. * bit 0: REPEAT_EN
  604. * bits 1-2: MASK_HALF_CACHELINE
  605. * bit 3: INJECT_ECC
  606. * bit 4: INJECT_ADDR_PARITY
  607. */
  608. injectmask = (pvt->inject.type & 1) |
  609. (pvt->inject.section & 0x3) << 1 |
  610. (pvt->inject.type & 0x6) << (3 - 1);
  611. pci_write_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  612. MC_CHANNEL_ERROR_MASK, injectmask);
  613. debugf0("Error inject addr match 0x%016llx, ecc 0x%08x, inject 0x%08x\n",
  614. mask, pvt->inject.eccmask, injectmask);
  615. return count;
  616. }
  617. static ssize_t i7core_inject_enable_show(struct mem_ctl_info *mci,
  618. char *data)
  619. {
  620. struct i7core_pvt *pvt = mci->pvt_info;
  621. u32 injectmask;
  622. pci_read_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  623. MC_CHANNEL_ERROR_MASK, &injectmask);
  624. debugf0("Inject error read: 0x%018x\n", injectmask);
  625. if (injectmask & 0x0c)
  626. pvt->inject.enable = 1;
  627. return sprintf(data, "%d\n", pvt->inject.enable);
  628. }
  629. /*
  630. * Sysfs struct
  631. */
  632. static struct mcidev_sysfs_attribute i7core_inj_attrs[] = {
  633. {
  634. .attr = {
  635. .name = "inject_section",
  636. .mode = (S_IRUGO | S_IWUSR)
  637. },
  638. .show = i7core_inject_section_show,
  639. .store = i7core_inject_section_store,
  640. }, {
  641. .attr = {
  642. .name = "inject_type",
  643. .mode = (S_IRUGO | S_IWUSR)
  644. },
  645. .show = i7core_inject_type_show,
  646. .store = i7core_inject_type_store,
  647. }, {
  648. .attr = {
  649. .name = "inject_eccmask",
  650. .mode = (S_IRUGO | S_IWUSR)
  651. },
  652. .show = i7core_inject_eccmask_show,
  653. .store = i7core_inject_eccmask_store,
  654. }, {
  655. .attr = {
  656. .name = "inject_addrmatch",
  657. .mode = (S_IRUGO | S_IWUSR)
  658. },
  659. .show = i7core_inject_addrmatch_show,
  660. .store = i7core_inject_addrmatch_store,
  661. }, {
  662. .attr = {
  663. .name = "inject_enable",
  664. .mode = (S_IRUGO | S_IWUSR)
  665. },
  666. .show = i7core_inject_enable_show,
  667. .store = i7core_inject_enable_store,
  668. },
  669. };
  670. /****************************************************************************
  671. Device initialization routines: put/get, init/exit
  672. ****************************************************************************/
  673. /*
  674. * i7core_put_devices 'put' all the devices that we have
  675. * reserved via 'get'
  676. */
  677. static void i7core_put_devices(void)
  678. {
  679. int i;
  680. for (i = 0; i < N_DEVS; i++)
  681. pci_dev_put(pci_devs[i].pdev);
  682. }
  683. /*
  684. * i7core_get_devices Find and perform 'get' operation on the MCH's
  685. * device/functions we want to reference for this driver
  686. *
  687. * Need to 'get' device 16 func 1 and func 2
  688. */
  689. static int i7core_get_devices(struct mem_ctl_info *mci, struct pci_dev *mcidev)
  690. {
  691. struct i7core_pvt *pvt = mci->pvt_info;
  692. int rc, i,func;
  693. struct pci_dev *pdev = NULL;
  694. pvt = mci->pvt_info;
  695. memset(pvt, 0, sizeof(*pvt));
  696. for (i = 0; i < N_DEVS; i++) {
  697. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  698. pci_devs[i].dev_id, NULL);
  699. if (!pdev) {
  700. /* End of list, leave */
  701. i7core_printk(KERN_ERR,
  702. "Device not found: PCI ID %04x:%04x "
  703. "(dev %d, func %d)\n",
  704. PCI_VENDOR_ID_INTEL, pci_devs[i].dev_id,
  705. pci_devs[i].dev,pci_devs[i].func);
  706. if ((pci_devs[i].dev == 3) && (pci_devs[i].func == 2))
  707. continue; /* Only on chips with RDIMMs */
  708. else
  709. i7core_put_devices();
  710. }
  711. pci_devs[i].pdev = pdev;
  712. rc = pci_enable_device(pdev);
  713. if (rc < 0) {
  714. i7core_printk(KERN_ERR,
  715. "Couldn't enable PCI ID %04x:%04x "
  716. "(dev %d, func %d)\n",
  717. PCI_VENDOR_ID_INTEL, pci_devs[i].dev_id,
  718. pci_devs[i].dev, pci_devs[i].func);
  719. i7core_put_devices();
  720. return rc;
  721. }
  722. /* Sanity check */
  723. if (PCI_FUNC(pdev->devfn) != pci_devs[i].func) {
  724. i7core_printk(KERN_ERR,
  725. "Device PCI ID %04x:%04x "
  726. "has function %d instead of %d\n",
  727. PCI_VENDOR_ID_INTEL, pci_devs[i].dev_id,
  728. PCI_FUNC(pdev->devfn), pci_devs[i].func);
  729. i7core_put_devices();
  730. return -EINVAL;
  731. }
  732. i7core_printk(KERN_INFO,
  733. "Registered device %0x:%0x fn=%0x %0x\n",
  734. PCI_VENDOR_ID_INTEL, pci_devs[i].dev_id,
  735. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  736. func = PCI_FUNC(pdev->devfn);
  737. if (pci_devs[i].dev < 4) {
  738. pvt->pci_mcr[func] = pdev;
  739. } else {
  740. pvt->pci_ch[pci_devs[i].dev - 4][func] = pdev;
  741. }
  742. }
  743. i7core_printk(KERN_INFO, "Driver loaded.\n");
  744. return 0;
  745. }
  746. /*
  747. * i7core_check_error Retrieve and process errors reported by the
  748. * hardware. Called by the Core module.
  749. */
  750. static void i7core_check_error(struct mem_ctl_info *mci)
  751. {
  752. /* FIXME: need a real code here */
  753. }
  754. /*
  755. * i7core_probe Probe for ONE instance of device to see if it is
  756. * present.
  757. * return:
  758. * 0 for FOUND a device
  759. * < 0 for error code
  760. */
  761. static int __devinit i7core_probe(struct pci_dev *pdev,
  762. const struct pci_device_id *id)
  763. {
  764. struct mem_ctl_info *mci;
  765. struct i7core_pvt *pvt;
  766. int num_channels;
  767. int num_csrows;
  768. int num_dimms_per_channel;
  769. int dev_idx = id->driver_data;
  770. if (dev_idx >= ARRAY_SIZE(i7core_devs))
  771. return -EINVAL;
  772. num_channels = NUM_CHANS;
  773. /* FIXME: FAKE data, since we currently don't now how to get this */
  774. num_dimms_per_channel = 4;
  775. num_csrows = num_dimms_per_channel;
  776. /* allocate a new MC control structure */
  777. mci = edac_mc_alloc(sizeof(*pvt), num_csrows, num_channels, 0);
  778. if (mci == NULL)
  779. return -ENOMEM;
  780. debugf0("MC: " __FILE__ ": %s(): mci = %p\n", __func__, mci);
  781. /* 'get' the pci devices we want to reserve for our use */
  782. if (i7core_get_devices(mci, pdev))
  783. goto fail0;
  784. mci->dev = &pdev->dev; /* record ptr to the generic device */
  785. pvt = mci->pvt_info;
  786. // pvt->system_address = pdev; /* Record this device in our private */
  787. // pvt->maxch = num_channels;
  788. // pvt->maxdimmperch = num_dimms_per_channel;
  789. mci->mc_idx = 0;
  790. mci->mtype_cap = MEM_FLAG_FB_DDR2; /* FIXME: it uses DDR3 */
  791. mci->edac_ctl_cap = EDAC_FLAG_NONE;
  792. mci->edac_cap = EDAC_FLAG_NONE;
  793. mci->mod_name = "i7core_edac.c";
  794. mci->mod_ver = I7CORE_REVISION;
  795. mci->ctl_name = i7core_devs[dev_idx].ctl_name;
  796. mci->dev_name = pci_name(pdev);
  797. mci->ctl_page_to_phys = NULL;
  798. mci->mc_driver_sysfs_attributes = i7core_inj_attrs;
  799. /* Set the function pointer to an actual operation function */
  800. mci->edac_check = i7core_check_error;
  801. /* add this new MC control structure to EDAC's list of MCs */
  802. if (edac_mc_add_mc(mci)) {
  803. debugf0("MC: " __FILE__
  804. ": %s(): failed edac_mc_add_mc()\n", __func__);
  805. /* FIXME: perhaps some code should go here that disables error
  806. * reporting if we just enabled it
  807. */
  808. goto fail1;
  809. }
  810. /* allocating generic PCI control info */
  811. i7core_pci = edac_pci_create_generic_ctl(&pdev->dev, EDAC_MOD_STR);
  812. if (!i7core_pci) {
  813. printk(KERN_WARNING
  814. "%s(): Unable to create PCI control\n",
  815. __func__);
  816. printk(KERN_WARNING
  817. "%s(): PCI error report via EDAC not setup\n",
  818. __func__);
  819. }
  820. /* Default error mask is any memory */
  821. pvt->inject.channel = -1;
  822. pvt->inject.dimm = -1;
  823. pvt->inject.rank = -1;
  824. pvt->inject.bank = -1;
  825. pvt->inject.page = -1;
  826. pvt->inject.col = -1;
  827. /* Get dimm basic config */
  828. get_dimm_config(mci);
  829. return 0;
  830. fail1:
  831. i7core_put_devices();
  832. fail0:
  833. edac_mc_free(mci);
  834. return -ENODEV;
  835. }
  836. /*
  837. * i7core_remove destructor for one instance of device
  838. *
  839. */
  840. static void __devexit i7core_remove(struct pci_dev *pdev)
  841. {
  842. struct mem_ctl_info *mci;
  843. debugf0(__FILE__ ": %s()\n", __func__);
  844. if (i7core_pci)
  845. edac_pci_release_generic_ctl(i7core_pci);
  846. mci = edac_mc_del_mc(&pdev->dev);
  847. if (!mci)
  848. return;
  849. /* retrieve references to resources, and free those resources */
  850. i7core_put_devices();
  851. edac_mc_free(mci);
  852. }
  853. MODULE_DEVICE_TABLE(pci, i7core_pci_tbl);
  854. /*
  855. * i7core_driver pci_driver structure for this module
  856. *
  857. */
  858. static struct pci_driver i7core_driver = {
  859. .name = "i7core_edac",
  860. .probe = i7core_probe,
  861. .remove = __devexit_p(i7core_remove),
  862. .id_table = i7core_pci_tbl,
  863. };
  864. /*
  865. * i7core_init Module entry function
  866. * Try to initialize this module for its devices
  867. */
  868. static int __init i7core_init(void)
  869. {
  870. int pci_rc;
  871. debugf2("MC: " __FILE__ ": %s()\n", __func__);
  872. /* Ensure that the OPSTATE is set correctly for POLL or NMI */
  873. opstate_init();
  874. pci_rc = pci_register_driver(&i7core_driver);
  875. return (pci_rc < 0) ? pci_rc : 0;
  876. }
  877. /*
  878. * i7core_exit() Module exit function
  879. * Unregister the driver
  880. */
  881. static void __exit i7core_exit(void)
  882. {
  883. debugf2("MC: " __FILE__ ": %s()\n", __func__);
  884. pci_unregister_driver(&i7core_driver);
  885. }
  886. module_init(i7core_init);
  887. module_exit(i7core_exit);
  888. MODULE_LICENSE("GPL");
  889. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  890. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  891. MODULE_DESCRIPTION("MC Driver for Intel i7 Core memory controllers - "
  892. I7CORE_REVISION);
  893. module_param(edac_op_state, int, 0444);
  894. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");