i7core_edac.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526
  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 <linux/edac_mce.h>
  30. #include <linux/spinlock.h>
  31. #include "edac_core.h"
  32. /* To use the new pci_[read/write]_config_qword instead of two dword */
  33. #define USE_QWORD 0
  34. /*
  35. * Alter this version for the module when modifications are made
  36. */
  37. #define I7CORE_REVISION " Ver: 1.0.0 " __DATE__
  38. #define EDAC_MOD_STR "i7core_edac"
  39. /* HACK: temporary, just to enable all logs, for now */
  40. #undef debugf0
  41. #define debugf0(fmt, arg...) edac_printk(KERN_INFO, "i7core", fmt, ##arg)
  42. /*
  43. * Debug macros
  44. */
  45. #define i7core_printk(level, fmt, arg...) \
  46. edac_printk(level, "i7core", fmt, ##arg)
  47. #define i7core_mc_printk(mci, level, fmt, arg...) \
  48. edac_mc_chipset_printk(mci, level, "i7core", fmt, ##arg)
  49. /*
  50. * i7core Memory Controller Registers
  51. */
  52. /* OFFSETS for Device 0 Function 0 */
  53. #define MC_CFG_CONTROL 0x90
  54. /* OFFSETS for Device 3 Function 0 */
  55. #define MC_CONTROL 0x48
  56. #define MC_STATUS 0x4c
  57. #define MC_MAX_DOD 0x64
  58. /*
  59. * OFFSETS for Device 3 Function 4, as inicated on Xeon 5500 datasheet:
  60. * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
  61. */
  62. #define MC_TEST_ERR_RCV1 0x60
  63. #define DIMM2_COR_ERR(r) ((r) & 0x7fff)
  64. #define MC_TEST_ERR_RCV0 0x64
  65. #define DIMM1_COR_ERR(r) (((r) >> 16) & 0x7fff)
  66. #define DIMM0_COR_ERR(r) ((r) & 0x7fff)
  67. /* OFFSETS for Devices 4,5 and 6 Function 0 */
  68. #define MC_CHANNEL_DIMM_INIT_PARAMS 0x58
  69. #define THREE_DIMMS_PRESENT (1 << 24)
  70. #define SINGLE_QUAD_RANK_PRESENT (1 << 23)
  71. #define QUAD_RANK_PRESENT (1 << 22)
  72. #define REGISTERED_DIMM (1 << 15)
  73. #define MC_CHANNEL_MAPPER 0x60
  74. #define RDLCH(r, ch) ((((r) >> (3 + (ch * 6))) & 0x07) - 1)
  75. #define WRLCH(r, ch) ((((r) >> (ch * 6)) & 0x07) - 1)
  76. #define MC_CHANNEL_RANK_PRESENT 0x7c
  77. #define RANK_PRESENT_MASK 0xffff
  78. #define MC_CHANNEL_ADDR_MATCH 0xf0
  79. #define MC_CHANNEL_ERROR_MASK 0xf8
  80. #define MC_CHANNEL_ERROR_INJECT 0xfc
  81. #define INJECT_ADDR_PARITY 0x10
  82. #define INJECT_ECC 0x08
  83. #define MASK_CACHELINE 0x06
  84. #define MASK_FULL_CACHELINE 0x06
  85. #define MASK_MSB32_CACHELINE 0x04
  86. #define MASK_LSB32_CACHELINE 0x02
  87. #define NO_MASK_CACHELINE 0x00
  88. #define REPEAT_EN 0x01
  89. /* OFFSETS for Devices 4,5 and 6 Function 1 */
  90. #define MC_DOD_CH_DIMM0 0x48
  91. #define MC_DOD_CH_DIMM1 0x4c
  92. #define MC_DOD_CH_DIMM2 0x50
  93. #define RANKOFFSET_MASK ((1 << 12) | (1 << 11) | (1 << 10))
  94. #define RANKOFFSET(x) ((x & RANKOFFSET_MASK) >> 10)
  95. #define DIMM_PRESENT_MASK (1 << 9)
  96. #define DIMM_PRESENT(x) (((x) & DIMM_PRESENT_MASK) >> 9)
  97. #define MC_DOD_NUMBANK_MASK ((1 << 8) | (1 << 7))
  98. #define MC_DOD_NUMBANK(x) (((x) & MC_DOD_NUMBANK_MASK) >> 7)
  99. #define MC_DOD_NUMRANK_MASK ((1 << 6) | (1 << 5))
  100. #define MC_DOD_NUMRANK(x) (((x) & MC_DOD_NUMRANK_MASK) >> 5)
  101. #define MC_DOD_NUMROW_MASK ((1 << 4) | (1 << 3) | (1 << 2))
  102. #define MC_DOD_NUMROW(x) (((x) & MC_DOD_NUMROW_MASK) >> 2)
  103. #define MC_DOD_NUMCOL_MASK 3
  104. #define MC_DOD_NUMCOL(x) ((x) & MC_DOD_NUMCOL_MASK)
  105. #define MC_RANK_PRESENT 0x7c
  106. #define MC_SAG_CH_0 0x80
  107. #define MC_SAG_CH_1 0x84
  108. #define MC_SAG_CH_2 0x88
  109. #define MC_SAG_CH_3 0x8c
  110. #define MC_SAG_CH_4 0x90
  111. #define MC_SAG_CH_5 0x94
  112. #define MC_SAG_CH_6 0x98
  113. #define MC_SAG_CH_7 0x9c
  114. #define MC_RIR_LIMIT_CH_0 0x40
  115. #define MC_RIR_LIMIT_CH_1 0x44
  116. #define MC_RIR_LIMIT_CH_2 0x48
  117. #define MC_RIR_LIMIT_CH_3 0x4C
  118. #define MC_RIR_LIMIT_CH_4 0x50
  119. #define MC_RIR_LIMIT_CH_5 0x54
  120. #define MC_RIR_LIMIT_CH_6 0x58
  121. #define MC_RIR_LIMIT_CH_7 0x5C
  122. #define MC_RIR_LIMIT_MASK ((1 << 10) - 1)
  123. #define MC_RIR_WAY_CH 0x80
  124. #define MC_RIR_WAY_OFFSET_MASK (((1 << 14) - 1) & ~0x7)
  125. #define MC_RIR_WAY_RANK_MASK 0x7
  126. /*
  127. * i7core structs
  128. */
  129. #define NUM_CHANS 3
  130. #define MAX_DIMMS 3 /* Max DIMMS per channel */
  131. #define MAX_MCR_FUNC 4
  132. #define MAX_CHAN_FUNC 3
  133. struct i7core_info {
  134. u32 mc_control;
  135. u32 mc_status;
  136. u32 max_dod;
  137. u32 ch_map;
  138. };
  139. struct i7core_inject {
  140. int enable;
  141. u32 section;
  142. u32 type;
  143. u32 eccmask;
  144. /* Error address mask */
  145. int channel, dimm, rank, bank, page, col;
  146. };
  147. struct i7core_channel {
  148. u32 ranks;
  149. u32 dimms;
  150. };
  151. struct pci_id_descr {
  152. int dev;
  153. int func;
  154. int dev_id;
  155. struct pci_dev *pdev;
  156. };
  157. struct i7core_pvt {
  158. struct pci_dev *pci_noncore;
  159. struct pci_dev *pci_mcr[MAX_MCR_FUNC + 1];
  160. struct pci_dev *pci_ch[NUM_CHANS][MAX_CHAN_FUNC + 1];
  161. struct i7core_info info;
  162. struct i7core_inject inject;
  163. struct i7core_channel channel[NUM_CHANS];
  164. int channels; /* Number of active channels */
  165. int ce_count_available;
  166. unsigned long ce_count[MAX_DIMMS]; /* ECC corrected errors counts per dimm */
  167. int last_ce_count[MAX_DIMMS];
  168. /* mcelog glue */
  169. struct edac_mce edac_mce;
  170. struct mce mce_entry[MCE_LOG_LEN];
  171. unsigned mce_count;
  172. spinlock_t mce_lock;
  173. };
  174. /* Device name and register DID (Device ID) */
  175. struct i7core_dev_info {
  176. const char *ctl_name; /* name for this device */
  177. u16 fsb_mapping_errors; /* DID for the branchmap,control */
  178. };
  179. #define PCI_DESCR(device, function, device_id) \
  180. .dev = (device), \
  181. .func = (function), \
  182. .dev_id = (device_id)
  183. struct pci_id_descr pci_devs[] = {
  184. /* Memory controller */
  185. { PCI_DESCR(3, 0, PCI_DEVICE_ID_INTEL_I7_MCR) },
  186. { PCI_DESCR(3, 1, PCI_DEVICE_ID_INTEL_I7_MC_TAD) },
  187. { PCI_DESCR(3, 2, PCI_DEVICE_ID_INTEL_I7_MC_RAS) }, /* if RDIMM is supported */
  188. { PCI_DESCR(3, 4, PCI_DEVICE_ID_INTEL_I7_MC_TEST) },
  189. /* Generic Non-core registers */
  190. { PCI_DESCR(0, 0, PCI_DEVICE_ID_INTEL_I7_NOCORE) },
  191. /* Channel 0 */
  192. { PCI_DESCR(4, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH0_CTRL) },
  193. { PCI_DESCR(4, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH0_ADDR) },
  194. { PCI_DESCR(4, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH0_RANK) },
  195. { PCI_DESCR(4, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH0_TC) },
  196. /* Channel 1 */
  197. { PCI_DESCR(5, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH1_CTRL) },
  198. { PCI_DESCR(5, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH1_ADDR) },
  199. { PCI_DESCR(5, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH1_RANK) },
  200. { PCI_DESCR(5, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH1_TC) },
  201. /* Channel 2 */
  202. { PCI_DESCR(6, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH2_CTRL) },
  203. { PCI_DESCR(6, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH2_ADDR) },
  204. { PCI_DESCR(6, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH2_RANK) },
  205. { PCI_DESCR(6, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH2_TC) },
  206. };
  207. #define N_DEVS ARRAY_SIZE(pci_devs)
  208. /*
  209. * pci_device_id table for which devices we are looking for
  210. * This should match the first device at pci_devs table
  211. */
  212. static const struct pci_device_id i7core_pci_tbl[] __devinitdata = {
  213. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I7_MCR)},
  214. {0,} /* 0 terminated list. */
  215. };
  216. /* Table of devices attributes supported by this driver */
  217. static const struct i7core_dev_info i7core_devs[] = {
  218. {
  219. .ctl_name = "i7 Core",
  220. .fsb_mapping_errors = PCI_DEVICE_ID_INTEL_I7_MCR,
  221. },
  222. };
  223. static struct edac_pci_ctl_info *i7core_pci;
  224. /****************************************************************************
  225. Anciliary status routines
  226. ****************************************************************************/
  227. /* MC_CONTROL bits */
  228. #define CH_ACTIVE(pvt, ch) ((pvt)->info.mc_control & (1 << (8 + ch)))
  229. #define ECCx8(pvt) ((pvt)->info.mc_control & (1 << 1))
  230. /* MC_STATUS bits */
  231. #define ECC_ENABLED(pvt) ((pvt)->info.mc_status & (1 << 3))
  232. #define CH_DISABLED(pvt, ch) ((pvt)->info.mc_status & (1 << ch))
  233. /* MC_MAX_DOD read functions */
  234. static inline int numdimms(u32 dimms)
  235. {
  236. return (dimms & 0x3) + 1;
  237. }
  238. static inline int numrank(u32 rank)
  239. {
  240. static int ranks[4] = { 1, 2, 4, -EINVAL };
  241. return ranks[rank & 0x3];
  242. }
  243. static inline int numbank(u32 bank)
  244. {
  245. static int banks[4] = { 4, 8, 16, -EINVAL };
  246. return banks[bank & 0x3];
  247. }
  248. static inline int numrow(u32 row)
  249. {
  250. static int rows[8] = {
  251. 1 << 12, 1 << 13, 1 << 14, 1 << 15,
  252. 1 << 16, -EINVAL, -EINVAL, -EINVAL,
  253. };
  254. return rows[row & 0x7];
  255. }
  256. static inline int numcol(u32 col)
  257. {
  258. static int cols[8] = {
  259. 1 << 10, 1 << 11, 1 << 12, -EINVAL,
  260. };
  261. return cols[col & 0x3];
  262. }
  263. /****************************************************************************
  264. Memory check routines
  265. ****************************************************************************/
  266. static struct pci_dev *get_pdev_slot_func(int slot, int func)
  267. {
  268. int i;
  269. for (i = 0; i < N_DEVS; i++) {
  270. if (!pci_devs[i].pdev)
  271. continue;
  272. if (PCI_SLOT(pci_devs[i].pdev->devfn) == slot &&
  273. PCI_FUNC(pci_devs[i].pdev->devfn) == func) {
  274. return pci_devs[i].pdev;
  275. }
  276. }
  277. return NULL;
  278. }
  279. static int i7core_get_active_channels(int *channels, int *csrows)
  280. {
  281. struct pci_dev *pdev = NULL;
  282. int i, j;
  283. u32 status, control;
  284. *channels = 0;
  285. *csrows = 0;
  286. pdev = get_pdev_slot_func(3, 0);
  287. if (!pdev) {
  288. i7core_printk(KERN_ERR, "Couldn't find fn 3.0!!!\n");
  289. return -ENODEV;
  290. }
  291. /* Device 3 function 0 reads */
  292. pci_read_config_dword(pdev, MC_STATUS, &status);
  293. pci_read_config_dword(pdev, MC_CONTROL, &control);
  294. for (i = 0; i < NUM_CHANS; i++) {
  295. u32 dimm_dod[3];
  296. /* Check if the channel is active */
  297. if (!(control & (1 << (8 + i))))
  298. continue;
  299. /* Check if the channel is disabled */
  300. if (status & (1 << i))
  301. continue;
  302. pdev = get_pdev_slot_func(i + 4, 1);
  303. if (!pdev) {
  304. i7core_printk(KERN_ERR, "Couldn't find fn %d.%d!!!\n",
  305. i + 4, 1);
  306. return -ENODEV;
  307. }
  308. /* Devices 4-6 function 1 */
  309. pci_read_config_dword(pdev,
  310. MC_DOD_CH_DIMM0, &dimm_dod[0]);
  311. pci_read_config_dword(pdev,
  312. MC_DOD_CH_DIMM1, &dimm_dod[1]);
  313. pci_read_config_dword(pdev,
  314. MC_DOD_CH_DIMM2, &dimm_dod[2]);
  315. (*channels)++;
  316. for (j = 0; j < 3; j++) {
  317. if (!DIMM_PRESENT(dimm_dod[j]))
  318. continue;
  319. (*csrows)++;
  320. }
  321. }
  322. debugf0("Number of active channels: %d\n", *channels);
  323. return 0;
  324. }
  325. static int get_dimm_config(struct mem_ctl_info *mci)
  326. {
  327. struct i7core_pvt *pvt = mci->pvt_info;
  328. struct csrow_info *csr;
  329. struct pci_dev *pdev;
  330. int i, j, csrow = 0;
  331. unsigned long last_page = 0;
  332. enum edac_type mode;
  333. enum mem_type mtype;
  334. /* Get data from the MC register, function 0 */
  335. pdev = pvt->pci_mcr[0];
  336. if (!pdev)
  337. return -ENODEV;
  338. /* Device 3 function 0 reads */
  339. pci_read_config_dword(pdev, MC_CONTROL, &pvt->info.mc_control);
  340. pci_read_config_dword(pdev, MC_STATUS, &pvt->info.mc_status);
  341. pci_read_config_dword(pdev, MC_MAX_DOD, &pvt->info.max_dod);
  342. pci_read_config_dword(pdev, MC_CHANNEL_MAPPER, &pvt->info.ch_map);
  343. debugf0("MC control=0x%08x status=0x%08x dod=0x%08x map=0x%08x\n",
  344. pvt->info.mc_control, pvt->info.mc_status,
  345. pvt->info.max_dod, pvt->info.ch_map);
  346. if (ECC_ENABLED(pvt)) {
  347. debugf0("ECC enabled with x%d SDCC\n", ECCx8(pvt) ? 8 : 4);
  348. if (ECCx8(pvt))
  349. mode = EDAC_S8ECD8ED;
  350. else
  351. mode = EDAC_S4ECD4ED;
  352. } else {
  353. debugf0("ECC disabled\n");
  354. mode = EDAC_NONE;
  355. }
  356. /* FIXME: need to handle the error codes */
  357. debugf0("DOD Max limits: DIMMS: %d, %d-ranked, %d-banked\n",
  358. numdimms(pvt->info.max_dod),
  359. numrank(pvt->info.max_dod >> 2),
  360. numbank(pvt->info.max_dod >> 4));
  361. debugf0("DOD Max rows x colums = 0x%x x 0x%x\n",
  362. numrow(pvt->info.max_dod >> 6),
  363. numcol(pvt->info.max_dod >> 9));
  364. debugf0("Memory channel configuration:\n");
  365. for (i = 0; i < NUM_CHANS; i++) {
  366. u32 data, dimm_dod[3], value[8];
  367. if (!CH_ACTIVE(pvt, i)) {
  368. debugf0("Channel %i is not active\n", i);
  369. continue;
  370. }
  371. if (CH_DISABLED(pvt, i)) {
  372. debugf0("Channel %i is disabled\n", i);
  373. continue;
  374. }
  375. /* Devices 4-6 function 0 */
  376. pci_read_config_dword(pvt->pci_ch[i][0],
  377. MC_CHANNEL_DIMM_INIT_PARAMS, &data);
  378. pvt->channel[i].ranks = (data & QUAD_RANK_PRESENT) ? 4 : 2;
  379. if (data & REGISTERED_DIMM)
  380. mtype = MEM_RDDR3;
  381. else
  382. mtype = MEM_DDR3;
  383. #if 0
  384. if (data & THREE_DIMMS_PRESENT)
  385. pvt->channel[i].dimms = 3;
  386. else if (data & SINGLE_QUAD_RANK_PRESENT)
  387. pvt->channel[i].dimms = 1;
  388. else
  389. pvt->channel[i].dimms = 2;
  390. #endif
  391. /* Devices 4-6 function 1 */
  392. pci_read_config_dword(pvt->pci_ch[i][1],
  393. MC_DOD_CH_DIMM0, &dimm_dod[0]);
  394. pci_read_config_dword(pvt->pci_ch[i][1],
  395. MC_DOD_CH_DIMM1, &dimm_dod[1]);
  396. pci_read_config_dword(pvt->pci_ch[i][1],
  397. MC_DOD_CH_DIMM2, &dimm_dod[2]);
  398. debugf0("Ch%d phy rd%d, wr%d (0x%08x): "
  399. "%d ranks, %cDIMMs\n",
  400. i,
  401. RDLCH(pvt->info.ch_map, i), WRLCH(pvt->info.ch_map, i),
  402. data,
  403. pvt->channel[i].ranks,
  404. (data & REGISTERED_DIMM) ? 'R' : 'U');
  405. for (j = 0; j < 3; j++) {
  406. u32 banks, ranks, rows, cols;
  407. u32 size, npages;
  408. if (!DIMM_PRESENT(dimm_dod[j]))
  409. continue;
  410. banks = numbank(MC_DOD_NUMBANK(dimm_dod[j]));
  411. ranks = numrank(MC_DOD_NUMRANK(dimm_dod[j]));
  412. rows = numrow(MC_DOD_NUMROW(dimm_dod[j]));
  413. cols = numcol(MC_DOD_NUMCOL(dimm_dod[j]));
  414. /* DDR3 has 8 I/O banks */
  415. size = (rows * cols * banks * ranks) >> (20 - 3);
  416. pvt->channel[i].dimms++;
  417. debugf0("\tdimm %d (0x%08x) %d Mb offset: %x, "
  418. "numbank: %d,\n\t\t"
  419. "numrank: %d, numrow: %#x, numcol: %#x\n",
  420. j, dimm_dod[j], size,
  421. RANKOFFSET(dimm_dod[j]),
  422. banks, ranks, rows, cols);
  423. #if PAGE_SHIFT > 20
  424. npages = size >> (PAGE_SHIFT - 20);
  425. #else
  426. npages = size << (20 - PAGE_SHIFT);
  427. #endif
  428. csr = &mci->csrows[csrow];
  429. csr->first_page = last_page + 1;
  430. last_page += npages;
  431. csr->last_page = last_page;
  432. csr->nr_pages = npages;
  433. csr->page_mask = 0;
  434. csr->grain = 8;
  435. csr->csrow_idx = csrow;
  436. csr->nr_channels = 1;
  437. csr->channels[0].chan_idx = i;
  438. csr->channels[0].ce_count = 0;
  439. switch (banks) {
  440. case 4:
  441. csr->dtype = DEV_X4;
  442. break;
  443. case 8:
  444. csr->dtype = DEV_X8;
  445. break;
  446. case 16:
  447. csr->dtype = DEV_X16;
  448. break;
  449. default:
  450. csr->dtype = DEV_UNKNOWN;
  451. }
  452. csr->edac_mode = mode;
  453. csr->mtype = mtype;
  454. csrow++;
  455. }
  456. pci_read_config_dword(pdev, MC_SAG_CH_0, &value[0]);
  457. pci_read_config_dword(pdev, MC_SAG_CH_1, &value[1]);
  458. pci_read_config_dword(pdev, MC_SAG_CH_2, &value[2]);
  459. pci_read_config_dword(pdev, MC_SAG_CH_3, &value[3]);
  460. pci_read_config_dword(pdev, MC_SAG_CH_4, &value[4]);
  461. pci_read_config_dword(pdev, MC_SAG_CH_5, &value[5]);
  462. pci_read_config_dword(pdev, MC_SAG_CH_6, &value[6]);
  463. pci_read_config_dword(pdev, MC_SAG_CH_7, &value[7]);
  464. debugf0("\t[%i] DIVBY3\tREMOVED\tOFFSET\n", i);
  465. for (j = 0; j < 8; j++)
  466. debugf0("\t\t%#x\t%#x\t%#x\n",
  467. (value[j] >> 27) & 0x1,
  468. (value[j] >> 24) & 0x7,
  469. (value[j] && ((1 << 24) - 1)));
  470. }
  471. return 0;
  472. }
  473. /****************************************************************************
  474. Error insertion routines
  475. ****************************************************************************/
  476. /* The i7core has independent error injection features per channel.
  477. However, to have a simpler code, we don't allow enabling error injection
  478. on more than one channel.
  479. Also, since a change at an inject parameter will be applied only at enable,
  480. we're disabling error injection on all write calls to the sysfs nodes that
  481. controls the error code injection.
  482. */
  483. static int disable_inject(struct mem_ctl_info *mci)
  484. {
  485. struct i7core_pvt *pvt = mci->pvt_info;
  486. pvt->inject.enable = 0;
  487. if (!pvt->pci_ch[pvt->inject.channel][0])
  488. return -ENODEV;
  489. pci_write_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  490. MC_CHANNEL_ERROR_MASK, 0);
  491. return 0;
  492. }
  493. /*
  494. * i7core inject inject.section
  495. *
  496. * accept and store error injection inject.section value
  497. * bit 0 - refers to the lower 32-byte half cacheline
  498. * bit 1 - refers to the upper 32-byte half cacheline
  499. */
  500. static ssize_t i7core_inject_section_store(struct mem_ctl_info *mci,
  501. const char *data, size_t count)
  502. {
  503. struct i7core_pvt *pvt = mci->pvt_info;
  504. unsigned long value;
  505. int rc;
  506. if (pvt->inject.enable)
  507. disable_inject(mci);
  508. rc = strict_strtoul(data, 10, &value);
  509. if ((rc < 0) || (value > 3))
  510. return 0;
  511. pvt->inject.section = (u32) value;
  512. return count;
  513. }
  514. static ssize_t i7core_inject_section_show(struct mem_ctl_info *mci,
  515. char *data)
  516. {
  517. struct i7core_pvt *pvt = mci->pvt_info;
  518. return sprintf(data, "0x%08x\n", pvt->inject.section);
  519. }
  520. /*
  521. * i7core inject.type
  522. *
  523. * accept and store error injection inject.section value
  524. * bit 0 - repeat enable - Enable error repetition
  525. * bit 1 - inject ECC error
  526. * bit 2 - inject parity error
  527. */
  528. static ssize_t i7core_inject_type_store(struct mem_ctl_info *mci,
  529. const char *data, size_t count)
  530. {
  531. struct i7core_pvt *pvt = mci->pvt_info;
  532. unsigned long value;
  533. int rc;
  534. if (pvt->inject.enable)
  535. disable_inject(mci);
  536. rc = strict_strtoul(data, 10, &value);
  537. if ((rc < 0) || (value > 7))
  538. return 0;
  539. pvt->inject.type = (u32) value;
  540. return count;
  541. }
  542. static ssize_t i7core_inject_type_show(struct mem_ctl_info *mci,
  543. char *data)
  544. {
  545. struct i7core_pvt *pvt = mci->pvt_info;
  546. return sprintf(data, "0x%08x\n", pvt->inject.type);
  547. }
  548. /*
  549. * i7core_inject_inject.eccmask_store
  550. *
  551. * The type of error (UE/CE) will depend on the inject.eccmask value:
  552. * Any bits set to a 1 will flip the corresponding ECC bit
  553. * Correctable errors can be injected by flipping 1 bit or the bits within
  554. * a symbol pair (2 consecutive aligned 8-bit pairs - i.e. 7:0 and 15:8 or
  555. * 23:16 and 31:24). Flipping bits in two symbol pairs will cause an
  556. * uncorrectable error to be injected.
  557. */
  558. static ssize_t i7core_inject_eccmask_store(struct mem_ctl_info *mci,
  559. const char *data, size_t count)
  560. {
  561. struct i7core_pvt *pvt = mci->pvt_info;
  562. unsigned long value;
  563. int rc;
  564. if (pvt->inject.enable)
  565. disable_inject(mci);
  566. rc = strict_strtoul(data, 10, &value);
  567. if (rc < 0)
  568. return 0;
  569. pvt->inject.eccmask = (u32) value;
  570. return count;
  571. }
  572. static ssize_t i7core_inject_eccmask_show(struct mem_ctl_info *mci,
  573. char *data)
  574. {
  575. struct i7core_pvt *pvt = mci->pvt_info;
  576. return sprintf(data, "0x%08x\n", pvt->inject.eccmask);
  577. }
  578. /*
  579. * i7core_addrmatch
  580. *
  581. * The type of error (UE/CE) will depend on the inject.eccmask value:
  582. * Any bits set to a 1 will flip the corresponding ECC bit
  583. * Correctable errors can be injected by flipping 1 bit or the bits within
  584. * a symbol pair (2 consecutive aligned 8-bit pairs - i.e. 7:0 and 15:8 or
  585. * 23:16 and 31:24). Flipping bits in two symbol pairs will cause an
  586. * uncorrectable error to be injected.
  587. */
  588. static ssize_t i7core_inject_addrmatch_store(struct mem_ctl_info *mci,
  589. const char *data, size_t count)
  590. {
  591. struct i7core_pvt *pvt = mci->pvt_info;
  592. char *cmd, *val;
  593. long value;
  594. int rc;
  595. if (pvt->inject.enable)
  596. disable_inject(mci);
  597. do {
  598. cmd = strsep((char **) &data, ":");
  599. if (!cmd)
  600. break;
  601. val = strsep((char **) &data, " \n\t");
  602. if (!val)
  603. return cmd - data;
  604. if (!strcasecmp(val, "any"))
  605. value = -1;
  606. else {
  607. rc = strict_strtol(val, 10, &value);
  608. if ((rc < 0) || (value < 0))
  609. return cmd - data;
  610. }
  611. if (!strcasecmp(cmd, "channel")) {
  612. if (value < 3)
  613. pvt->inject.channel = value;
  614. else
  615. return cmd - data;
  616. } else if (!strcasecmp(cmd, "dimm")) {
  617. if (value < 4)
  618. pvt->inject.dimm = value;
  619. else
  620. return cmd - data;
  621. } else if (!strcasecmp(cmd, "rank")) {
  622. if (value < 4)
  623. pvt->inject.rank = value;
  624. else
  625. return cmd - data;
  626. } else if (!strcasecmp(cmd, "bank")) {
  627. if (value < 4)
  628. pvt->inject.bank = value;
  629. else
  630. return cmd - data;
  631. } else if (!strcasecmp(cmd, "page")) {
  632. if (value <= 0xffff)
  633. pvt->inject.page = value;
  634. else
  635. return cmd - data;
  636. } else if (!strcasecmp(cmd, "col") ||
  637. !strcasecmp(cmd, "column")) {
  638. if (value <= 0x3fff)
  639. pvt->inject.col = value;
  640. else
  641. return cmd - data;
  642. }
  643. } while (1);
  644. return count;
  645. }
  646. static ssize_t i7core_inject_addrmatch_show(struct mem_ctl_info *mci,
  647. char *data)
  648. {
  649. struct i7core_pvt *pvt = mci->pvt_info;
  650. char channel[4], dimm[4], bank[4], rank[4], page[7], col[7];
  651. if (pvt->inject.channel < 0)
  652. sprintf(channel, "any");
  653. else
  654. sprintf(channel, "%d", pvt->inject.channel);
  655. if (pvt->inject.dimm < 0)
  656. sprintf(dimm, "any");
  657. else
  658. sprintf(dimm, "%d", pvt->inject.dimm);
  659. if (pvt->inject.bank < 0)
  660. sprintf(bank, "any");
  661. else
  662. sprintf(bank, "%d", pvt->inject.bank);
  663. if (pvt->inject.rank < 0)
  664. sprintf(rank, "any");
  665. else
  666. sprintf(rank, "%d", pvt->inject.rank);
  667. if (pvt->inject.page < 0)
  668. sprintf(page, "any");
  669. else
  670. sprintf(page, "0x%04x", pvt->inject.page);
  671. if (pvt->inject.col < 0)
  672. sprintf(col, "any");
  673. else
  674. sprintf(col, "0x%04x", pvt->inject.col);
  675. return sprintf(data, "channel: %s\ndimm: %s\nbank: %s\n"
  676. "rank: %s\npage: %s\ncolumn: %s\n",
  677. channel, dimm, bank, rank, page, col);
  678. }
  679. /*
  680. * This routine prepares the Memory Controller for error injection.
  681. * The error will be injected when some process tries to write to the
  682. * memory that matches the given criteria.
  683. * The criteria can be set in terms of a mask where dimm, rank, bank, page
  684. * and col can be specified.
  685. * A -1 value for any of the mask items will make the MCU to ignore
  686. * that matching criteria for error injection.
  687. *
  688. * It should be noticed that the error will only happen after a write operation
  689. * on a memory that matches the condition. if REPEAT_EN is not enabled at
  690. * inject mask, then it will produce just one error. Otherwise, it will repeat
  691. * until the injectmask would be cleaned.
  692. *
  693. * FIXME: This routine assumes that MAXNUMDIMMS value of MC_MAX_DOD
  694. * is reliable enough to check if the MC is using the
  695. * three channels. However, this is not clear at the datasheet.
  696. */
  697. static ssize_t i7core_inject_enable_store(struct mem_ctl_info *mci,
  698. const char *data, size_t count)
  699. {
  700. struct i7core_pvt *pvt = mci->pvt_info;
  701. u32 injectmask;
  702. u64 mask = 0;
  703. int rc;
  704. long enable;
  705. if (!pvt->pci_ch[pvt->inject.channel][0])
  706. return 0;
  707. rc = strict_strtoul(data, 10, &enable);
  708. if ((rc < 0))
  709. return 0;
  710. if (enable) {
  711. pvt->inject.enable = 1;
  712. } else {
  713. disable_inject(mci);
  714. return count;
  715. }
  716. /* Sets pvt->inject.dimm mask */
  717. if (pvt->inject.dimm < 0)
  718. mask |= 1L << 41;
  719. else {
  720. if (pvt->channel[pvt->inject.channel].dimms > 2)
  721. mask |= (pvt->inject.dimm & 0x3L) << 35;
  722. else
  723. mask |= (pvt->inject.dimm & 0x1L) << 36;
  724. }
  725. /* Sets pvt->inject.rank mask */
  726. if (pvt->inject.rank < 0)
  727. mask |= 1L << 40;
  728. else {
  729. if (pvt->channel[pvt->inject.channel].dimms > 2)
  730. mask |= (pvt->inject.rank & 0x1L) << 34;
  731. else
  732. mask |= (pvt->inject.rank & 0x3L) << 34;
  733. }
  734. /* Sets pvt->inject.bank mask */
  735. if (pvt->inject.bank < 0)
  736. mask |= 1L << 39;
  737. else
  738. mask |= (pvt->inject.bank & 0x15L) << 30;
  739. /* Sets pvt->inject.page mask */
  740. if (pvt->inject.page < 0)
  741. mask |= 1L << 38;
  742. else
  743. mask |= (pvt->inject.page & 0xffffL) << 14;
  744. /* Sets pvt->inject.column mask */
  745. if (pvt->inject.col < 0)
  746. mask |= 1L << 37;
  747. else
  748. mask |= (pvt->inject.col & 0x3fffL);
  749. /* Unlock writes to registers */
  750. pci_write_config_dword(pvt->pci_noncore, MC_CFG_CONTROL, 0x2);
  751. msleep(100);
  752. /* Zeroes error count registers */
  753. pci_write_config_dword(pvt->pci_mcr[4], MC_TEST_ERR_RCV1, 0);
  754. pci_write_config_dword(pvt->pci_mcr[4], MC_TEST_ERR_RCV0, 0);
  755. pvt->ce_count_available = 0;
  756. #if USE_QWORD
  757. pci_write_config_qword(pvt->pci_ch[pvt->inject.channel][0],
  758. MC_CHANNEL_ADDR_MATCH, mask);
  759. #else
  760. pci_write_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  761. MC_CHANNEL_ADDR_MATCH, mask);
  762. pci_write_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  763. MC_CHANNEL_ADDR_MATCH + 4, mask >> 32L);
  764. #endif
  765. #if 1
  766. #if USE_QWORD
  767. u64 rdmask;
  768. pci_read_config_qword(pvt->pci_ch[pvt->inject.channel][0],
  769. MC_CHANNEL_ADDR_MATCH, &rdmask);
  770. debugf0("Inject addr match write 0x%016llx, read: 0x%016llx\n",
  771. mask, rdmask);
  772. #else
  773. u32 rdmask1, rdmask2;
  774. pci_read_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  775. MC_CHANNEL_ADDR_MATCH, &rdmask1);
  776. pci_read_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  777. MC_CHANNEL_ADDR_MATCH + 4, &rdmask2);
  778. debugf0("Inject addr match write 0x%016llx, read: 0x%08x 0x%08x\n",
  779. mask, rdmask1, rdmask2);
  780. #endif
  781. #endif
  782. pci_write_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  783. MC_CHANNEL_ERROR_MASK, pvt->inject.eccmask);
  784. /*
  785. * bit 0: REPEAT_EN
  786. * bits 1-2: MASK_HALF_CACHELINE
  787. * bit 3: INJECT_ECC
  788. * bit 4: INJECT_ADDR_PARITY
  789. */
  790. injectmask = (pvt->inject.type & 1) |
  791. (pvt->inject.section & 0x3) << 1 |
  792. (pvt->inject.type & 0x6) << (3 - 1);
  793. pci_write_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  794. MC_CHANNEL_ERROR_MASK, injectmask);
  795. #if 0
  796. /* lock writes to registers */
  797. pci_write_config_dword(pvt->pci_noncore, MC_CFG_CONTROL, 0);
  798. #endif
  799. debugf0("Error inject addr match 0x%016llx, ecc 0x%08x,"
  800. " inject 0x%08x\n",
  801. mask, pvt->inject.eccmask, injectmask);
  802. return count;
  803. }
  804. static ssize_t i7core_inject_enable_show(struct mem_ctl_info *mci,
  805. char *data)
  806. {
  807. struct i7core_pvt *pvt = mci->pvt_info;
  808. u32 injectmask;
  809. pci_read_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  810. MC_CHANNEL_ERROR_MASK, &injectmask);
  811. debugf0("Inject error read: 0x%018x\n", injectmask);
  812. if (injectmask & 0x0c)
  813. pvt->inject.enable = 1;
  814. return sprintf(data, "%d\n", pvt->inject.enable);
  815. }
  816. static ssize_t i7core_ce_regs_show(struct mem_ctl_info *mci, char *data)
  817. {
  818. struct i7core_pvt *pvt = mci->pvt_info;
  819. if (!pvt->ce_count_available)
  820. return sprintf(data, "unavailable\n");
  821. return sprintf(data, "dimm0: %lu\ndimm1: %lu\ndimm2: %lu\n",
  822. pvt->ce_count[0],
  823. pvt->ce_count[1],
  824. pvt->ce_count[2]);
  825. }
  826. /*
  827. * Sysfs struct
  828. */
  829. static struct mcidev_sysfs_attribute i7core_inj_attrs[] = {
  830. {
  831. .attr = {
  832. .name = "inject_section",
  833. .mode = (S_IRUGO | S_IWUSR)
  834. },
  835. .show = i7core_inject_section_show,
  836. .store = i7core_inject_section_store,
  837. }, {
  838. .attr = {
  839. .name = "inject_type",
  840. .mode = (S_IRUGO | S_IWUSR)
  841. },
  842. .show = i7core_inject_type_show,
  843. .store = i7core_inject_type_store,
  844. }, {
  845. .attr = {
  846. .name = "inject_eccmask",
  847. .mode = (S_IRUGO | S_IWUSR)
  848. },
  849. .show = i7core_inject_eccmask_show,
  850. .store = i7core_inject_eccmask_store,
  851. }, {
  852. .attr = {
  853. .name = "inject_addrmatch",
  854. .mode = (S_IRUGO | S_IWUSR)
  855. },
  856. .show = i7core_inject_addrmatch_show,
  857. .store = i7core_inject_addrmatch_store,
  858. }, {
  859. .attr = {
  860. .name = "inject_enable",
  861. .mode = (S_IRUGO | S_IWUSR)
  862. },
  863. .show = i7core_inject_enable_show,
  864. .store = i7core_inject_enable_store,
  865. }, {
  866. .attr = {
  867. .name = "corrected_error_counts",
  868. .mode = (S_IRUGO | S_IWUSR)
  869. },
  870. .show = i7core_ce_regs_show,
  871. .store = NULL,
  872. },
  873. };
  874. /****************************************************************************
  875. Device initialization routines: put/get, init/exit
  876. ****************************************************************************/
  877. /*
  878. * i7core_put_devices 'put' all the devices that we have
  879. * reserved via 'get'
  880. */
  881. static void i7core_put_devices(void)
  882. {
  883. int i;
  884. for (i = 0; i < N_DEVS; i++)
  885. pci_dev_put(pci_devs[i].pdev);
  886. }
  887. /*
  888. * i7core_get_devices Find and perform 'get' operation on the MCH's
  889. * device/functions we want to reference for this driver
  890. *
  891. * Need to 'get' device 16 func 1 and func 2
  892. */
  893. static int i7core_get_devices(void)
  894. {
  895. int rc, i;
  896. struct pci_dev *pdev = NULL;
  897. for (i = 0; i < N_DEVS; i++) {
  898. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  899. pci_devs[i].dev_id, NULL);
  900. if (likely(pdev))
  901. pci_devs[i].pdev = pdev;
  902. else {
  903. i7core_printk(KERN_ERR,
  904. "Device not found: PCI ID %04x:%04x "
  905. "(dev %d, func %d)\n",
  906. PCI_VENDOR_ID_INTEL, pci_devs[i].dev_id,
  907. pci_devs[i].dev, pci_devs[i].func);
  908. /* Dev 3 function 2 only exists on chips with RDIMMs */
  909. if ((pci_devs[i].dev == 3) && (pci_devs[i].func == 2))
  910. continue;
  911. /* End of list, leave */
  912. rc = -ENODEV;
  913. goto error;
  914. }
  915. /* Sanity check */
  916. if (unlikely(PCI_SLOT(pdev->devfn) != pci_devs[i].dev ||
  917. PCI_FUNC(pdev->devfn) != pci_devs[i].func)) {
  918. i7core_printk(KERN_ERR,
  919. "Device PCI ID %04x:%04x "
  920. "has fn %d.%d instead of fn %d.%d\n",
  921. PCI_VENDOR_ID_INTEL, pci_devs[i].dev_id,
  922. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  923. pci_devs[i].dev, pci_devs[i].func);
  924. rc = -EINVAL;
  925. goto error;
  926. }
  927. /* Be sure that the device is enabled */
  928. rc = pci_enable_device(pdev);
  929. if (unlikely(rc < 0)) {
  930. i7core_printk(KERN_ERR,
  931. "Couldn't enable PCI ID %04x:%04x "
  932. "fn %d.%d\n",
  933. PCI_VENDOR_ID_INTEL, pci_devs[i].dev_id,
  934. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  935. goto error;
  936. }
  937. i7core_printk(KERN_INFO,
  938. "Registered device %0x:%0x fn %d.%d\n",
  939. PCI_VENDOR_ID_INTEL, pci_devs[i].dev_id,
  940. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  941. }
  942. return 0;
  943. error:
  944. i7core_put_devices();
  945. return -EINVAL;
  946. }
  947. static int mci_bind_devs(struct mem_ctl_info *mci)
  948. {
  949. struct i7core_pvt *pvt = mci->pvt_info;
  950. struct pci_dev *pdev;
  951. int i, func, slot;
  952. for (i = 0; i < N_DEVS; i++) {
  953. pdev = pci_devs[i].pdev;
  954. if (!pdev)
  955. continue;
  956. func = PCI_FUNC(pdev->devfn);
  957. slot = PCI_SLOT(pdev->devfn);
  958. if (slot == 3) {
  959. if (unlikely(func > MAX_MCR_FUNC))
  960. goto error;
  961. pvt->pci_mcr[func] = pdev;
  962. } else if (likely(slot >= 4 && slot < 4 + NUM_CHANS)) {
  963. if (unlikely(func > MAX_CHAN_FUNC))
  964. goto error;
  965. pvt->pci_ch[slot - 4][func] = pdev;
  966. } else if (!slot && !func)
  967. pvt->pci_noncore = pdev;
  968. else
  969. goto error;
  970. debugf0("Associated fn %d.%d, dev = %p\n",
  971. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn), pdev);
  972. }
  973. return 0;
  974. error:
  975. i7core_printk(KERN_ERR, "Device %d, function %d "
  976. "is out of the expected range\n",
  977. slot, func);
  978. return -EINVAL;
  979. }
  980. /****************************************************************************
  981. Error check routines
  982. ****************************************************************************/
  983. /* This function is based on the device 3 function 4 registers as described on:
  984. * Intel Xeon Processor 5500 Series Datasheet Volume 2
  985. * http://www.intel.com/Assets/PDF/datasheet/321322.pdf
  986. * also available at:
  987. * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
  988. */
  989. static void check_mc_test_err(struct mem_ctl_info *mci)
  990. {
  991. struct i7core_pvt *pvt = mci->pvt_info;
  992. u32 rcv1, rcv0;
  993. int new0, new1, new2;
  994. if (!pvt->pci_mcr[4]) {
  995. debugf0("%s MCR registers not found\n",__func__);
  996. return;
  997. }
  998. /* Corrected error reads */
  999. pci_read_config_dword(pvt->pci_mcr[4], MC_TEST_ERR_RCV1, &rcv1);
  1000. pci_read_config_dword(pvt->pci_mcr[4], MC_TEST_ERR_RCV0, &rcv0);
  1001. /* Store the new values */
  1002. new2 = DIMM2_COR_ERR(rcv1);
  1003. new1 = DIMM1_COR_ERR(rcv0);
  1004. new0 = DIMM0_COR_ERR(rcv0);
  1005. #if 0
  1006. debugf2("%s CE rcv1=0x%08x rcv0=0x%08x, %d %d %d\n",
  1007. (pvt->ce_count_available ? "UPDATE" : "READ"),
  1008. rcv1, rcv0, new0, new1, new2);
  1009. #endif
  1010. /* Updates CE counters if it is not the first time here */
  1011. if (pvt->ce_count_available) {
  1012. /* Updates CE counters */
  1013. int add0, add1, add2;
  1014. add2 = new2 - pvt->last_ce_count[2];
  1015. add1 = new1 - pvt->last_ce_count[1];
  1016. add0 = new0 - pvt->last_ce_count[0];
  1017. if (add2 < 0)
  1018. add2 += 0x7fff;
  1019. pvt->ce_count[2] += add2;
  1020. if (add1 < 0)
  1021. add1 += 0x7fff;
  1022. pvt->ce_count[1] += add1;
  1023. if (add0 < 0)
  1024. add0 += 0x7fff;
  1025. pvt->ce_count[0] += add0;
  1026. } else
  1027. pvt->ce_count_available = 1;
  1028. /* Store the new values */
  1029. pvt->last_ce_count[2] = new2;
  1030. pvt->last_ce_count[1] = new1;
  1031. pvt->last_ce_count[0] = new0;
  1032. }
  1033. static void i7core_mce_output_error(struct mem_ctl_info *mci,
  1034. struct mce *m)
  1035. {
  1036. debugf0("CPU %d: Machine Check Exception: %16Lx"
  1037. "Bank %d: %016Lx\n",
  1038. m->cpu, m->mcgstatus, m->bank, m->status);
  1039. if (m->ip) {
  1040. debugf0("RIP%s %02x:<%016Lx>\n",
  1041. !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
  1042. m->cs, m->ip);
  1043. }
  1044. printk(KERN_EMERG "TSC %llx ", m->tsc);
  1045. if (m->addr)
  1046. printk("ADDR %llx ", m->addr);
  1047. if (m->misc)
  1048. printk("MISC %llx ", m->misc);
  1049. #if 0
  1050. snprintf(msg, sizeof(msg),
  1051. "%s (Branch=%d DRAM-Bank=%d Buffer ID = %d RDWR=%s "
  1052. "RAS=%d CAS=%d %s Err=0x%lx (%s))",
  1053. type, branch >> 1, bank, buf_id, rdwr_str(rdwr), ras, cas,
  1054. type, allErrors, error_name[errnum]);
  1055. /* Call the helper to output message */
  1056. edac_mc_handle_fbd_ue(mci, rank, channel, channel + 1, msg);
  1057. #endif
  1058. }
  1059. /*
  1060. * i7core_check_error Retrieve and process errors reported by the
  1061. * hardware. Called by the Core module.
  1062. */
  1063. static void i7core_check_error(struct mem_ctl_info *mci)
  1064. {
  1065. struct i7core_pvt *pvt = mci->pvt_info;
  1066. int i;
  1067. unsigned count = 0;
  1068. struct mce *m = NULL;
  1069. unsigned long flags;
  1070. debugf0(__FILE__ ": %s()\n", __func__);
  1071. /* Copy all mce errors into a temporary buffer */
  1072. spin_lock_irqsave(&pvt->mce_lock, flags);
  1073. if (pvt->mce_count) {
  1074. m = kmalloc(sizeof(*m) * pvt->mce_count, GFP_ATOMIC);
  1075. if (m) {
  1076. count = pvt->mce_count;
  1077. memcpy(m, &pvt->mce_entry, sizeof(*m) * count);
  1078. }
  1079. pvt->mce_count = 0;
  1080. }
  1081. spin_unlock_irqrestore(&pvt->mce_lock, flags);
  1082. /* proccess mcelog errors */
  1083. for (i = 0; i < count; i++)
  1084. i7core_mce_output_error(mci, &m[i]);
  1085. kfree(m);
  1086. /* check memory count errors */
  1087. check_mc_test_err(mci);
  1088. }
  1089. /*
  1090. * i7core_mce_check_error Replicates mcelog routine to get errors
  1091. * This routine simply queues mcelog errors, and
  1092. * return. The error itself should be handled later
  1093. * by i7core_check_error.
  1094. */
  1095. static int i7core_mce_check_error(void *priv, struct mce *mce)
  1096. {
  1097. struct i7core_pvt *pvt = priv;
  1098. unsigned long flags;
  1099. debugf0(__FILE__ ": %s()\n", __func__);
  1100. spin_lock_irqsave(&pvt->mce_lock, flags);
  1101. if (pvt->mce_count < MCE_LOG_LEN) {
  1102. memcpy(&pvt->mce_entry[pvt->mce_count], mce, sizeof(*mce));
  1103. pvt->mce_count++;
  1104. }
  1105. spin_unlock_irqrestore(&pvt->mce_lock, flags);
  1106. /* Advice mcelog that the error were handled */
  1107. // return 1;
  1108. return 0; // Let's duplicate the log
  1109. }
  1110. /*
  1111. * i7core_probe Probe for ONE instance of device to see if it is
  1112. * present.
  1113. * return:
  1114. * 0 for FOUND a device
  1115. * < 0 for error code
  1116. */
  1117. static int __devinit i7core_probe(struct pci_dev *pdev,
  1118. const struct pci_device_id *id)
  1119. {
  1120. struct mem_ctl_info *mci;
  1121. struct i7core_pvt *pvt;
  1122. int num_channels;
  1123. int num_csrows;
  1124. int dev_idx = id->driver_data;
  1125. int rc;
  1126. if (unlikely(dev_idx >= ARRAY_SIZE(i7core_devs)))
  1127. return -EINVAL;
  1128. /* get the pci devices we want to reserve for our use */
  1129. rc = i7core_get_devices();
  1130. if (unlikely(rc < 0))
  1131. return rc;
  1132. /* Check the number of active and not disabled channels */
  1133. rc = i7core_get_active_channels(&num_channels, &num_csrows);
  1134. if (unlikely(rc < 0))
  1135. goto fail0;
  1136. /* allocate a new MC control structure */
  1137. mci = edac_mc_alloc(sizeof(*pvt), num_csrows, num_channels, 0);
  1138. if (unlikely(!mci)) {
  1139. rc = -ENOMEM;
  1140. goto fail0;
  1141. }
  1142. debugf0("MC: " __FILE__ ": %s(): mci = %p\n", __func__, mci);
  1143. mci->dev = &pdev->dev; /* record ptr to the generic device */
  1144. pvt = mci->pvt_info;
  1145. memset(pvt, 0, sizeof(*pvt));
  1146. mci->mc_idx = 0;
  1147. /*
  1148. * FIXME: how to handle RDDR3 at MCI level? It is possible to have
  1149. * Mixed RDDR3/UDDR3 with Nehalem, provided that they are on different
  1150. * memory channels
  1151. */
  1152. mci->mtype_cap = MEM_FLAG_DDR3;
  1153. mci->edac_ctl_cap = EDAC_FLAG_NONE;
  1154. mci->edac_cap = EDAC_FLAG_NONE;
  1155. mci->mod_name = "i7core_edac.c";
  1156. mci->mod_ver = I7CORE_REVISION;
  1157. mci->ctl_name = i7core_devs[dev_idx].ctl_name;
  1158. mci->dev_name = pci_name(pdev);
  1159. mci->ctl_page_to_phys = NULL;
  1160. mci->mc_driver_sysfs_attributes = i7core_inj_attrs;
  1161. /* Set the function pointer to an actual operation function */
  1162. mci->edac_check = i7core_check_error;
  1163. /* Store pci devices at mci for faster access */
  1164. rc = mci_bind_devs(mci);
  1165. if (unlikely(rc < 0))
  1166. goto fail1;
  1167. /* Get dimm basic config */
  1168. get_dimm_config(mci);
  1169. /* add this new MC control structure to EDAC's list of MCs */
  1170. if (unlikely(edac_mc_add_mc(mci))) {
  1171. debugf0("MC: " __FILE__
  1172. ": %s(): failed edac_mc_add_mc()\n", __func__);
  1173. /* FIXME: perhaps some code should go here that disables error
  1174. * reporting if we just enabled it
  1175. */
  1176. rc = -EINVAL;
  1177. goto fail1;
  1178. }
  1179. /* allocating generic PCI control info */
  1180. i7core_pci = edac_pci_create_generic_ctl(&pdev->dev, EDAC_MOD_STR);
  1181. if (unlikely(!i7core_pci)) {
  1182. printk(KERN_WARNING
  1183. "%s(): Unable to create PCI control\n",
  1184. __func__);
  1185. printk(KERN_WARNING
  1186. "%s(): PCI error report via EDAC not setup\n",
  1187. __func__);
  1188. }
  1189. /* Default error mask is any memory */
  1190. pvt->inject.channel = 0;
  1191. pvt->inject.dimm = -1;
  1192. pvt->inject.rank = -1;
  1193. pvt->inject.bank = -1;
  1194. pvt->inject.page = -1;
  1195. pvt->inject.col = -1;
  1196. /* Registers on edac_mce in order to receive memory errors */
  1197. pvt->edac_mce.priv = pvt;
  1198. pvt->edac_mce.check_error = i7core_mce_check_error;
  1199. spin_lock_init(&pvt->mce_lock);
  1200. rc = edac_mce_register(&pvt->edac_mce);
  1201. if (unlikely (rc < 0)) {
  1202. debugf0("MC: " __FILE__
  1203. ": %s(): failed edac_mce_register()\n", __func__);
  1204. goto fail1;
  1205. }
  1206. i7core_printk(KERN_INFO, "Driver loaded.\n");
  1207. return 0;
  1208. fail1:
  1209. edac_mc_free(mci);
  1210. fail0:
  1211. i7core_put_devices();
  1212. return rc;
  1213. }
  1214. /*
  1215. * i7core_remove destructor for one instance of device
  1216. *
  1217. */
  1218. static void __devexit i7core_remove(struct pci_dev *pdev)
  1219. {
  1220. struct mem_ctl_info *mci;
  1221. struct i7core_pvt *pvt;
  1222. debugf0(__FILE__ ": %s()\n", __func__);
  1223. if (i7core_pci)
  1224. edac_pci_release_generic_ctl(i7core_pci);
  1225. mci = edac_mc_del_mc(&pdev->dev);
  1226. if (!mci)
  1227. return;
  1228. /* Unregisters on edac_mce in order to receive memory errors */
  1229. pvt = mci->pvt_info;
  1230. edac_mce_unregister(&pvt->edac_mce);
  1231. /* retrieve references to resources, and free those resources */
  1232. i7core_put_devices();
  1233. edac_mc_free(mci);
  1234. }
  1235. MODULE_DEVICE_TABLE(pci, i7core_pci_tbl);
  1236. /*
  1237. * i7core_driver pci_driver structure for this module
  1238. *
  1239. */
  1240. static struct pci_driver i7core_driver = {
  1241. .name = "i7core_edac",
  1242. .probe = i7core_probe,
  1243. .remove = __devexit_p(i7core_remove),
  1244. .id_table = i7core_pci_tbl,
  1245. };
  1246. /*
  1247. * i7core_init Module entry function
  1248. * Try to initialize this module for its devices
  1249. */
  1250. static int __init i7core_init(void)
  1251. {
  1252. int pci_rc;
  1253. debugf2("MC: " __FILE__ ": %s()\n", __func__);
  1254. /* Ensure that the OPSTATE is set correctly for POLL or NMI */
  1255. opstate_init();
  1256. pci_rc = pci_register_driver(&i7core_driver);
  1257. return (pci_rc < 0) ? pci_rc : 0;
  1258. }
  1259. /*
  1260. * i7core_exit() Module exit function
  1261. * Unregister the driver
  1262. */
  1263. static void __exit i7core_exit(void)
  1264. {
  1265. debugf2("MC: " __FILE__ ": %s()\n", __func__);
  1266. pci_unregister_driver(&i7core_driver);
  1267. }
  1268. module_init(i7core_init);
  1269. module_exit(i7core_exit);
  1270. MODULE_LICENSE("GPL");
  1271. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  1272. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  1273. MODULE_DESCRIPTION("MC Driver for Intel i7 Core memory controllers - "
  1274. I7CORE_REVISION);
  1275. module_param(edac_op_state, int, 0444);
  1276. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");