sb_edac.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189
  1. /* Intel Sandy Bridge -EN/-EP/-EX Memory Controller kernel module
  2. *
  3. * This driver supports the memory controllers found on the Intel
  4. * processor family Sandy Bridge.
  5. *
  6. * This file may be distributed under the terms of the
  7. * GNU General Public License version 2 only.
  8. *
  9. * Copyright (c) 2011 by:
  10. * Mauro Carvalho Chehab
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/pci.h>
  15. #include <linux/pci_ids.h>
  16. #include <linux/slab.h>
  17. #include <linux/delay.h>
  18. #include <linux/edac.h>
  19. #include <linux/mmzone.h>
  20. #include <linux/smp.h>
  21. #include <linux/bitmap.h>
  22. #include <linux/math64.h>
  23. #include <asm/processor.h>
  24. #include <asm/mce.h>
  25. #include "edac_core.h"
  26. /* Static vars */
  27. static LIST_HEAD(sbridge_edac_list);
  28. static DEFINE_MUTEX(sbridge_edac_lock);
  29. static int probed;
  30. /*
  31. * Alter this version for the module when modifications are made
  32. */
  33. #define SBRIDGE_REVISION " Ver: 1.1.0 "
  34. #define EDAC_MOD_STR "sbridge_edac"
  35. /*
  36. * Debug macros
  37. */
  38. #define sbridge_printk(level, fmt, arg...) \
  39. edac_printk(level, "sbridge", fmt, ##arg)
  40. #define sbridge_mc_printk(mci, level, fmt, arg...) \
  41. edac_mc_chipset_printk(mci, level, "sbridge", fmt, ##arg)
  42. /*
  43. * Get a bit field at register value <v>, from bit <lo> to bit <hi>
  44. */
  45. #define GET_BITFIELD(v, lo, hi) \
  46. (((v) & GENMASK_ULL(hi, lo)) >> (lo))
  47. /*
  48. * sbridge Memory Controller Registers
  49. */
  50. /*
  51. * FIXME: For now, let's order by device function, as it makes
  52. * easier for driver's development process. This table should be
  53. * moved to pci_id.h when submitted upstream
  54. */
  55. #define PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0 0x3cf4 /* 12.6 */
  56. #define PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1 0x3cf6 /* 12.7 */
  57. #define PCI_DEVICE_ID_INTEL_SBRIDGE_BR 0x3cf5 /* 13.6 */
  58. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0 0x3ca0 /* 14.0 */
  59. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA 0x3ca8 /* 15.0 */
  60. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS 0x3c71 /* 15.1 */
  61. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0 0x3caa /* 15.2 */
  62. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1 0x3cab /* 15.3 */
  63. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2 0x3cac /* 15.4 */
  64. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3 0x3cad /* 15.5 */
  65. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO 0x3cb8 /* 17.0 */
  66. /*
  67. * Currently, unused, but will be needed in the future
  68. * implementations, as they hold the error counters
  69. */
  70. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_ERR0 0x3c72 /* 16.2 */
  71. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_ERR1 0x3c73 /* 16.3 */
  72. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_ERR2 0x3c76 /* 16.6 */
  73. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_ERR3 0x3c77 /* 16.7 */
  74. /* Devices 12 Function 6, Offsets 0x80 to 0xcc */
  75. static const u32 sbridge_dram_rule[] = {
  76. 0x80, 0x88, 0x90, 0x98, 0xa0,
  77. 0xa8, 0xb0, 0xb8, 0xc0, 0xc8,
  78. };
  79. static const u32 ibridge_dram_rule[] = {
  80. 0x60, 0x68, 0x70, 0x78, 0x80,
  81. 0x88, 0x90, 0x98, 0xa0, 0xa8,
  82. 0xb0, 0xb8, 0xc0, 0xc8, 0xd0,
  83. 0xd8, 0xe0, 0xe8, 0xf0, 0xf8,
  84. };
  85. #define SAD_LIMIT(reg) ((GET_BITFIELD(reg, 6, 25) << 26) | 0x3ffffff)
  86. #define DRAM_ATTR(reg) GET_BITFIELD(reg, 2, 3)
  87. #define INTERLEAVE_MODE(reg) GET_BITFIELD(reg, 1, 1)
  88. #define DRAM_RULE_ENABLE(reg) GET_BITFIELD(reg, 0, 0)
  89. static char *get_dram_attr(u32 reg)
  90. {
  91. switch(DRAM_ATTR(reg)) {
  92. case 0:
  93. return "DRAM";
  94. case 1:
  95. return "MMCFG";
  96. case 2:
  97. return "NXM";
  98. default:
  99. return "unknown";
  100. }
  101. }
  102. static const u32 sbridge_interleave_list[] = {
  103. 0x84, 0x8c, 0x94, 0x9c, 0xa4,
  104. 0xac, 0xb4, 0xbc, 0xc4, 0xcc,
  105. };
  106. static const u32 ibridge_interleave_list[] = {
  107. 0x64, 0x6c, 0x74, 0x7c, 0x84,
  108. 0x8c, 0x94, 0x9c, 0xa4, 0xac,
  109. 0xb4, 0xbc, 0xc4, 0xcc, 0xd4,
  110. 0xdc, 0xe4, 0xec, 0xf4, 0xfc,
  111. };
  112. struct interleave_pkg {
  113. unsigned char start;
  114. unsigned char end;
  115. };
  116. static const struct interleave_pkg sbridge_interleave_pkg[] = {
  117. { 0, 2 },
  118. { 3, 5 },
  119. { 8, 10 },
  120. { 11, 13 },
  121. { 16, 18 },
  122. { 19, 21 },
  123. { 24, 26 },
  124. { 27, 29 },
  125. };
  126. static const struct interleave_pkg ibridge_interleave_pkg[] = {
  127. { 0, 3 },
  128. { 4, 7 },
  129. { 8, 11 },
  130. { 12, 15 },
  131. { 16, 19 },
  132. { 20, 23 },
  133. { 24, 27 },
  134. { 28, 31 },
  135. };
  136. static inline int sad_pkg(const struct interleave_pkg *table, u32 reg,
  137. int interleave)
  138. {
  139. return GET_BITFIELD(reg, table[interleave].start,
  140. table[interleave].end);
  141. }
  142. /* Devices 12 Function 7 */
  143. #define TOLM 0x80
  144. #define TOHM 0x84
  145. #define GET_TOLM(reg) ((GET_BITFIELD(reg, 0, 3) << 28) | 0x3ffffff)
  146. #define GET_TOHM(reg) ((GET_BITFIELD(reg, 0, 20) << 25) | 0x3ffffff)
  147. /* Device 13 Function 6 */
  148. #define SAD_TARGET 0xf0
  149. #define SOURCE_ID(reg) GET_BITFIELD(reg, 9, 11)
  150. #define SAD_CONTROL 0xf4
  151. #define NODE_ID(reg) GET_BITFIELD(reg, 0, 2)
  152. /* Device 14 function 0 */
  153. static const u32 tad_dram_rule[] = {
  154. 0x40, 0x44, 0x48, 0x4c,
  155. 0x50, 0x54, 0x58, 0x5c,
  156. 0x60, 0x64, 0x68, 0x6c,
  157. };
  158. #define MAX_TAD ARRAY_SIZE(tad_dram_rule)
  159. #define TAD_LIMIT(reg) ((GET_BITFIELD(reg, 12, 31) << 26) | 0x3ffffff)
  160. #define TAD_SOCK(reg) GET_BITFIELD(reg, 10, 11)
  161. #define TAD_CH(reg) GET_BITFIELD(reg, 8, 9)
  162. #define TAD_TGT3(reg) GET_BITFIELD(reg, 6, 7)
  163. #define TAD_TGT2(reg) GET_BITFIELD(reg, 4, 5)
  164. #define TAD_TGT1(reg) GET_BITFIELD(reg, 2, 3)
  165. #define TAD_TGT0(reg) GET_BITFIELD(reg, 0, 1)
  166. /* Device 15, function 0 */
  167. #define MCMTR 0x7c
  168. #define IS_ECC_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 2, 2)
  169. #define IS_LOCKSTEP_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 1, 1)
  170. #define IS_CLOSE_PG(mcmtr) GET_BITFIELD(mcmtr, 0, 0)
  171. /* Device 15, function 1 */
  172. #define RASENABLES 0xac
  173. #define IS_MIRROR_ENABLED(reg) GET_BITFIELD(reg, 0, 0)
  174. /* Device 15, functions 2-5 */
  175. static const int mtr_regs[] = {
  176. 0x80, 0x84, 0x88,
  177. };
  178. #define RANK_DISABLE(mtr) GET_BITFIELD(mtr, 16, 19)
  179. #define IS_DIMM_PRESENT(mtr) GET_BITFIELD(mtr, 14, 14)
  180. #define RANK_CNT_BITS(mtr) GET_BITFIELD(mtr, 12, 13)
  181. #define RANK_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 2, 4)
  182. #define COL_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 0, 1)
  183. static const u32 tad_ch_nilv_offset[] = {
  184. 0x90, 0x94, 0x98, 0x9c,
  185. 0xa0, 0xa4, 0xa8, 0xac,
  186. 0xb0, 0xb4, 0xb8, 0xbc,
  187. };
  188. #define CHN_IDX_OFFSET(reg) GET_BITFIELD(reg, 28, 29)
  189. #define TAD_OFFSET(reg) (GET_BITFIELD(reg, 6, 25) << 26)
  190. static const u32 rir_way_limit[] = {
  191. 0x108, 0x10c, 0x110, 0x114, 0x118,
  192. };
  193. #define MAX_RIR_RANGES ARRAY_SIZE(rir_way_limit)
  194. #define IS_RIR_VALID(reg) GET_BITFIELD(reg, 31, 31)
  195. #define RIR_WAY(reg) GET_BITFIELD(reg, 28, 29)
  196. #define RIR_LIMIT(reg) ((GET_BITFIELD(reg, 1, 10) << 29)| 0x1fffffff)
  197. #define MAX_RIR_WAY 8
  198. static const u32 rir_offset[MAX_RIR_RANGES][MAX_RIR_WAY] = {
  199. { 0x120, 0x124, 0x128, 0x12c, 0x130, 0x134, 0x138, 0x13c },
  200. { 0x140, 0x144, 0x148, 0x14c, 0x150, 0x154, 0x158, 0x15c },
  201. { 0x160, 0x164, 0x168, 0x16c, 0x170, 0x174, 0x178, 0x17c },
  202. { 0x180, 0x184, 0x188, 0x18c, 0x190, 0x194, 0x198, 0x19c },
  203. { 0x1a0, 0x1a4, 0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc },
  204. };
  205. #define RIR_RNK_TGT(reg) GET_BITFIELD(reg, 16, 19)
  206. #define RIR_OFFSET(reg) GET_BITFIELD(reg, 2, 14)
  207. /* Device 16, functions 2-7 */
  208. /*
  209. * FIXME: Implement the error count reads directly
  210. */
  211. static const u32 correrrcnt[] = {
  212. 0x104, 0x108, 0x10c, 0x110,
  213. };
  214. #define RANK_ODD_OV(reg) GET_BITFIELD(reg, 31, 31)
  215. #define RANK_ODD_ERR_CNT(reg) GET_BITFIELD(reg, 16, 30)
  216. #define RANK_EVEN_OV(reg) GET_BITFIELD(reg, 15, 15)
  217. #define RANK_EVEN_ERR_CNT(reg) GET_BITFIELD(reg, 0, 14)
  218. static const u32 correrrthrsld[] = {
  219. 0x11c, 0x120, 0x124, 0x128,
  220. };
  221. #define RANK_ODD_ERR_THRSLD(reg) GET_BITFIELD(reg, 16, 30)
  222. #define RANK_EVEN_ERR_THRSLD(reg) GET_BITFIELD(reg, 0, 14)
  223. /* Device 17, function 0 */
  224. #define SB_RANK_CFG_A 0x0328
  225. #define IB_RANK_CFG_A 0x0320
  226. #define IS_RDIMM_ENABLED(reg) GET_BITFIELD(reg, 11, 11)
  227. /*
  228. * sbridge structs
  229. */
  230. #define NUM_CHANNELS 4
  231. #define MAX_DIMMS 3 /* Max DIMMS per channel */
  232. enum type {
  233. SANDY_BRIDGE,
  234. IVY_BRIDGE,
  235. };
  236. struct sbridge_pvt;
  237. struct sbridge_info {
  238. enum type type;
  239. u32 mcmtr;
  240. u32 rankcfgr;
  241. u64 (*get_tolm)(struct sbridge_pvt *pvt);
  242. u64 (*get_tohm)(struct sbridge_pvt *pvt);
  243. const u32 *dram_rule;
  244. const u32 *interleave_list;
  245. const struct interleave_pkg *interleave_pkg;
  246. u8 max_sad;
  247. u8 max_interleave;
  248. };
  249. struct sbridge_channel {
  250. u32 ranks;
  251. u32 dimms;
  252. };
  253. struct pci_id_descr {
  254. int dev;
  255. int func;
  256. int dev_id;
  257. int optional;
  258. };
  259. struct pci_id_table {
  260. const struct pci_id_descr *descr;
  261. int n_devs;
  262. };
  263. struct sbridge_dev {
  264. struct list_head list;
  265. u8 bus, mc;
  266. u8 node_id, source_id;
  267. struct pci_dev **pdev;
  268. int n_devs;
  269. struct mem_ctl_info *mci;
  270. };
  271. struct sbridge_pvt {
  272. struct pci_dev *pci_ta, *pci_ddrio, *pci_ras;
  273. struct pci_dev *pci_sad0, *pci_sad1;
  274. struct pci_dev *pci_ha0, *pci_ha1;
  275. struct pci_dev *pci_br0, *pci_br1;
  276. struct pci_dev *pci_tad[NUM_CHANNELS];
  277. struct sbridge_dev *sbridge_dev;
  278. struct sbridge_info info;
  279. struct sbridge_channel channel[NUM_CHANNELS];
  280. /* Memory type detection */
  281. bool is_mirrored, is_lockstep, is_close_pg;
  282. /* Fifo double buffers */
  283. struct mce mce_entry[MCE_LOG_LEN];
  284. struct mce mce_outentry[MCE_LOG_LEN];
  285. /* Fifo in/out counters */
  286. unsigned mce_in, mce_out;
  287. /* Count indicator to show errors not got */
  288. unsigned mce_overrun;
  289. /* Memory description */
  290. u64 tolm, tohm;
  291. };
  292. #define PCI_DESCR(device, function, device_id, opt) \
  293. .dev = (device), \
  294. .func = (function), \
  295. .dev_id = (device_id), \
  296. .optional = opt
  297. static const struct pci_id_descr pci_dev_descr_sbridge[] = {
  298. /* Processor Home Agent */
  299. { PCI_DESCR(14, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0, 0) },
  300. /* Memory controller */
  301. { PCI_DESCR(15, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA, 0) },
  302. { PCI_DESCR(15, 1, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS, 0) },
  303. { PCI_DESCR(15, 2, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0, 0) },
  304. { PCI_DESCR(15, 3, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1, 0) },
  305. { PCI_DESCR(15, 4, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2, 0) },
  306. { PCI_DESCR(15, 5, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3, 0) },
  307. { PCI_DESCR(17, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO, 1) },
  308. /* System Address Decoder */
  309. { PCI_DESCR(12, 6, PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0, 0) },
  310. { PCI_DESCR(12, 7, PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1, 0) },
  311. /* Broadcast Registers */
  312. { PCI_DESCR(13, 6, PCI_DEVICE_ID_INTEL_SBRIDGE_BR, 0) },
  313. };
  314. #define PCI_ID_TABLE_ENTRY(A) { .descr=A, .n_devs = ARRAY_SIZE(A) }
  315. static const struct pci_id_table pci_dev_descr_sbridge_table[] = {
  316. PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge),
  317. {0,} /* 0 terminated list. */
  318. };
  319. /* This changes depending if 1HA or 2HA:
  320. * 1HA:
  321. * 0x0eb8 (17.0) is DDRIO0
  322. * 2HA:
  323. * 0x0ebc (17.4) is DDRIO0
  324. */
  325. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0 0x0eb8
  326. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0 0x0ebc
  327. /* pci ids */
  328. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0 0x0ea0
  329. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA 0x0ea8
  330. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS 0x0e71
  331. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0 0x0eaa
  332. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1 0x0eab
  333. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2 0x0eac
  334. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3 0x0ead
  335. #define PCI_DEVICE_ID_INTEL_IBRIDGE_SAD 0x0ec8
  336. #define PCI_DEVICE_ID_INTEL_IBRIDGE_BR0 0x0ec9
  337. #define PCI_DEVICE_ID_INTEL_IBRIDGE_BR1 0x0eca
  338. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1 0x0e60
  339. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA 0x0e68
  340. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS 0x0e79
  341. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 0x0e6a
  342. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1 0x0e6b
  343. static const struct pci_id_descr pci_dev_descr_ibridge[] = {
  344. /* Processor Home Agent */
  345. { PCI_DESCR(14, 0, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0, 0) },
  346. /* Memory controller */
  347. { PCI_DESCR(15, 0, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA, 0) },
  348. { PCI_DESCR(15, 1, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS, 0) },
  349. { PCI_DESCR(15, 2, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0, 0) },
  350. { PCI_DESCR(15, 3, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1, 0) },
  351. { PCI_DESCR(15, 4, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2, 0) },
  352. { PCI_DESCR(15, 5, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3, 0) },
  353. /* System Address Decoder */
  354. { PCI_DESCR(22, 0, PCI_DEVICE_ID_INTEL_IBRIDGE_SAD, 0) },
  355. /* Broadcast Registers */
  356. { PCI_DESCR(22, 1, PCI_DEVICE_ID_INTEL_IBRIDGE_BR0, 1) },
  357. { PCI_DESCR(22, 2, PCI_DEVICE_ID_INTEL_IBRIDGE_BR1, 0) },
  358. /* Optional, mode 2HA */
  359. { PCI_DESCR(28, 0, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, 1) },
  360. #if 0
  361. { PCI_DESCR(29, 0, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA, 1) },
  362. { PCI_DESCR(29, 1, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS, 1) },
  363. #endif
  364. { PCI_DESCR(29, 2, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0, 1) },
  365. { PCI_DESCR(29, 3, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1, 1) },
  366. { PCI_DESCR(17, 0, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0, 1) },
  367. { PCI_DESCR(17, 4, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0, 1) },
  368. };
  369. static const struct pci_id_table pci_dev_descr_ibridge_table[] = {
  370. PCI_ID_TABLE_ENTRY(pci_dev_descr_ibridge),
  371. {0,} /* 0 terminated list. */
  372. };
  373. /*
  374. * pci_device_id table for which devices we are looking for
  375. */
  376. static const struct pci_device_id sbridge_pci_tbl[] = {
  377. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA)},
  378. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA)},
  379. {0,} /* 0 terminated list. */
  380. };
  381. /****************************************************************************
  382. Ancillary status routines
  383. ****************************************************************************/
  384. static inline int numrank(u32 mtr)
  385. {
  386. int ranks = (1 << RANK_CNT_BITS(mtr));
  387. if (ranks > 4) {
  388. edac_dbg(0, "Invalid number of ranks: %d (max = 4) raw value = %x (%04x)\n",
  389. ranks, (unsigned int)RANK_CNT_BITS(mtr), mtr);
  390. return -EINVAL;
  391. }
  392. return ranks;
  393. }
  394. static inline int numrow(u32 mtr)
  395. {
  396. int rows = (RANK_WIDTH_BITS(mtr) + 12);
  397. if (rows < 13 || rows > 18) {
  398. edac_dbg(0, "Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)\n",
  399. rows, (unsigned int)RANK_WIDTH_BITS(mtr), mtr);
  400. return -EINVAL;
  401. }
  402. return 1 << rows;
  403. }
  404. static inline int numcol(u32 mtr)
  405. {
  406. int cols = (COL_WIDTH_BITS(mtr) + 10);
  407. if (cols > 12) {
  408. edac_dbg(0, "Invalid number of cols: %d (max = 4) raw value = %x (%04x)\n",
  409. cols, (unsigned int)COL_WIDTH_BITS(mtr), mtr);
  410. return -EINVAL;
  411. }
  412. return 1 << cols;
  413. }
  414. static struct sbridge_dev *get_sbridge_dev(u8 bus)
  415. {
  416. struct sbridge_dev *sbridge_dev;
  417. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  418. if (sbridge_dev->bus == bus)
  419. return sbridge_dev;
  420. }
  421. return NULL;
  422. }
  423. static struct sbridge_dev *alloc_sbridge_dev(u8 bus,
  424. const struct pci_id_table *table)
  425. {
  426. struct sbridge_dev *sbridge_dev;
  427. sbridge_dev = kzalloc(sizeof(*sbridge_dev), GFP_KERNEL);
  428. if (!sbridge_dev)
  429. return NULL;
  430. sbridge_dev->pdev = kzalloc(sizeof(*sbridge_dev->pdev) * table->n_devs,
  431. GFP_KERNEL);
  432. if (!sbridge_dev->pdev) {
  433. kfree(sbridge_dev);
  434. return NULL;
  435. }
  436. sbridge_dev->bus = bus;
  437. sbridge_dev->n_devs = table->n_devs;
  438. list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
  439. return sbridge_dev;
  440. }
  441. static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
  442. {
  443. list_del(&sbridge_dev->list);
  444. kfree(sbridge_dev->pdev);
  445. kfree(sbridge_dev);
  446. }
  447. static u64 sbridge_get_tolm(struct sbridge_pvt *pvt)
  448. {
  449. u32 reg;
  450. /* Address range is 32:28 */
  451. pci_read_config_dword(pvt->pci_sad1, TOLM, &reg);
  452. return GET_TOLM(reg);
  453. }
  454. static u64 sbridge_get_tohm(struct sbridge_pvt *pvt)
  455. {
  456. u32 reg;
  457. pci_read_config_dword(pvt->pci_sad1, TOHM, &reg);
  458. return GET_TOHM(reg);
  459. }
  460. static u64 ibridge_get_tolm(struct sbridge_pvt *pvt)
  461. {
  462. u32 reg;
  463. pci_read_config_dword(pvt->pci_br1, TOLM, &reg);
  464. return GET_TOLM(reg);
  465. }
  466. static u64 ibridge_get_tohm(struct sbridge_pvt *pvt)
  467. {
  468. u32 reg;
  469. pci_read_config_dword(pvt->pci_br1, TOHM, &reg);
  470. return GET_TOHM(reg);
  471. }
  472. static inline u8 sad_pkg_socket(u8 pkg)
  473. {
  474. /* on Ivy Bridge, nodeID is SASS, where A is HA and S is node id */
  475. return (pkg >> 3) | (pkg & 0x3);
  476. }
  477. static inline u8 sad_pkg_ha(u8 pkg)
  478. {
  479. return (pkg >> 2) & 0x1;
  480. }
  481. /****************************************************************************
  482. Memory check routines
  483. ****************************************************************************/
  484. static struct pci_dev *get_pdev_slot_func(u8 bus, unsigned slot,
  485. unsigned func)
  486. {
  487. struct sbridge_dev *sbridge_dev = get_sbridge_dev(bus);
  488. int i;
  489. if (!sbridge_dev)
  490. return NULL;
  491. for (i = 0; i < sbridge_dev->n_devs; i++) {
  492. if (!sbridge_dev->pdev[i])
  493. continue;
  494. if (PCI_SLOT(sbridge_dev->pdev[i]->devfn) == slot &&
  495. PCI_FUNC(sbridge_dev->pdev[i]->devfn) == func) {
  496. edac_dbg(1, "Associated %02x.%02x.%d with %p\n",
  497. bus, slot, func, sbridge_dev->pdev[i]);
  498. return sbridge_dev->pdev[i];
  499. }
  500. }
  501. return NULL;
  502. }
  503. /**
  504. * check_if_ecc_is_active() - Checks if ECC is active
  505. * bus: Device bus
  506. */
  507. static int check_if_ecc_is_active(const u8 bus)
  508. {
  509. struct pci_dev *pdev = NULL;
  510. u32 mcmtr;
  511. pdev = get_pdev_slot_func(bus, 15, 0);
  512. if (!pdev) {
  513. sbridge_printk(KERN_ERR, "Couldn't find PCI device "
  514. "%2x.%02d.%d!!!\n",
  515. bus, 15, 0);
  516. return -ENODEV;
  517. }
  518. pci_read_config_dword(pdev, MCMTR, &mcmtr);
  519. if (!IS_ECC_ENABLED(mcmtr)) {
  520. sbridge_printk(KERN_ERR, "ECC is disabled. Aborting\n");
  521. return -ENODEV;
  522. }
  523. return 0;
  524. }
  525. static int get_dimm_config(struct mem_ctl_info *mci)
  526. {
  527. struct sbridge_pvt *pvt = mci->pvt_info;
  528. struct dimm_info *dimm;
  529. unsigned i, j, banks, ranks, rows, cols, npages;
  530. u64 size;
  531. u32 reg;
  532. enum edac_type mode;
  533. enum mem_type mtype;
  534. pci_read_config_dword(pvt->pci_br0, SAD_TARGET, &reg);
  535. pvt->sbridge_dev->source_id = SOURCE_ID(reg);
  536. pci_read_config_dword(pvt->pci_br0, SAD_CONTROL, &reg);
  537. pvt->sbridge_dev->node_id = NODE_ID(reg);
  538. edac_dbg(0, "mc#%d: Node ID: %d, source ID: %d\n",
  539. pvt->sbridge_dev->mc,
  540. pvt->sbridge_dev->node_id,
  541. pvt->sbridge_dev->source_id);
  542. pci_read_config_dword(pvt->pci_ras, RASENABLES, &reg);
  543. if (IS_MIRROR_ENABLED(reg)) {
  544. edac_dbg(0, "Memory mirror is enabled\n");
  545. pvt->is_mirrored = true;
  546. } else {
  547. edac_dbg(0, "Memory mirror is disabled\n");
  548. pvt->is_mirrored = false;
  549. }
  550. pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
  551. if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
  552. edac_dbg(0, "Lockstep is enabled\n");
  553. mode = EDAC_S8ECD8ED;
  554. pvt->is_lockstep = true;
  555. } else {
  556. edac_dbg(0, "Lockstep is disabled\n");
  557. mode = EDAC_S4ECD4ED;
  558. pvt->is_lockstep = false;
  559. }
  560. if (IS_CLOSE_PG(pvt->info.mcmtr)) {
  561. edac_dbg(0, "address map is on closed page mode\n");
  562. pvt->is_close_pg = true;
  563. } else {
  564. edac_dbg(0, "address map is on open page mode\n");
  565. pvt->is_close_pg = false;
  566. }
  567. if (pvt->pci_ddrio) {
  568. pci_read_config_dword(pvt->pci_ddrio, pvt->info.rankcfgr,
  569. &reg);
  570. if (IS_RDIMM_ENABLED(reg)) {
  571. /* FIXME: Can also be LRDIMM */
  572. edac_dbg(0, "Memory is registered\n");
  573. mtype = MEM_RDDR3;
  574. } else {
  575. edac_dbg(0, "Memory is unregistered\n");
  576. mtype = MEM_DDR3;
  577. }
  578. } else {
  579. edac_dbg(0, "Cannot determine memory type\n");
  580. mtype = MEM_UNKNOWN;
  581. }
  582. /* On all supported DDR3 DIMM types, there are 8 banks available */
  583. banks = 8;
  584. for (i = 0; i < NUM_CHANNELS; i++) {
  585. u32 mtr;
  586. for (j = 0; j < ARRAY_SIZE(mtr_regs); j++) {
  587. dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
  588. i, j, 0);
  589. pci_read_config_dword(pvt->pci_tad[i],
  590. mtr_regs[j], &mtr);
  591. edac_dbg(4, "Channel #%d MTR%d = %x\n", i, j, mtr);
  592. if (IS_DIMM_PRESENT(mtr)) {
  593. pvt->channel[i].dimms++;
  594. ranks = numrank(mtr);
  595. rows = numrow(mtr);
  596. cols = numcol(mtr);
  597. /* DDR3 has 8 I/O banks */
  598. size = ((u64)rows * cols * banks * ranks) >> (20 - 3);
  599. npages = MiB_TO_PAGES(size);
  600. edac_dbg(0, "mc#%d: channel %d, dimm %d, %Ld Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
  601. pvt->sbridge_dev->mc, i, j,
  602. size, npages,
  603. banks, ranks, rows, cols);
  604. dimm->nr_pages = npages;
  605. dimm->grain = 32;
  606. dimm->dtype = (banks == 8) ? DEV_X8 : DEV_X4;
  607. dimm->mtype = mtype;
  608. dimm->edac_mode = mode;
  609. snprintf(dimm->label, sizeof(dimm->label),
  610. "CPU_SrcID#%u_Channel#%u_DIMM#%u",
  611. pvt->sbridge_dev->source_id, i, j);
  612. }
  613. }
  614. }
  615. return 0;
  616. }
  617. static void get_memory_layout(const struct mem_ctl_info *mci)
  618. {
  619. struct sbridge_pvt *pvt = mci->pvt_info;
  620. int i, j, k, n_sads, n_tads, sad_interl;
  621. u32 reg;
  622. u64 limit, prv = 0;
  623. u64 tmp_mb;
  624. u32 mb, kb;
  625. u32 rir_way;
  626. /*
  627. * Step 1) Get TOLM/TOHM ranges
  628. */
  629. pvt->tolm = pvt->info.get_tolm(pvt);
  630. tmp_mb = (1 + pvt->tolm) >> 20;
  631. mb = div_u64_rem(tmp_mb, 1000, &kb);
  632. edac_dbg(0, "TOLM: %u.%03u GB (0x%016Lx)\n", mb, kb, (u64)pvt->tolm);
  633. /* Address range is already 45:25 */
  634. pvt->tohm = pvt->info.get_tohm(pvt);
  635. tmp_mb = (1 + pvt->tohm) >> 20;
  636. mb = div_u64_rem(tmp_mb, 1000, &kb);
  637. edac_dbg(0, "TOHM: %u.%03u GB (0x%016Lx)\n", mb, kb, (u64)pvt->tohm);
  638. /*
  639. * Step 2) Get SAD range and SAD Interleave list
  640. * TAD registers contain the interleave wayness. However, it
  641. * seems simpler to just discover it indirectly, with the
  642. * algorithm bellow.
  643. */
  644. prv = 0;
  645. for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
  646. /* SAD_LIMIT Address range is 45:26 */
  647. pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
  648. &reg);
  649. limit = SAD_LIMIT(reg);
  650. if (!DRAM_RULE_ENABLE(reg))
  651. continue;
  652. if (limit <= prv)
  653. break;
  654. tmp_mb = (limit + 1) >> 20;
  655. mb = div_u64_rem(tmp_mb, 1000, &kb);
  656. edac_dbg(0, "SAD#%d %s up to %u.%03u GB (0x%016Lx) Interleave: %s reg=0x%08x\n",
  657. n_sads,
  658. get_dram_attr(reg),
  659. mb, kb,
  660. ((u64)tmp_mb) << 20L,
  661. INTERLEAVE_MODE(reg) ? "8:6" : "[8:6]XOR[18:16]",
  662. reg);
  663. prv = limit;
  664. pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
  665. &reg);
  666. sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
  667. for (j = 0; j < 8; j++) {
  668. u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, j);
  669. if (j > 0 && sad_interl == pkg)
  670. break;
  671. edac_dbg(0, "SAD#%d, interleave #%d: %d\n",
  672. n_sads, j, pkg);
  673. }
  674. }
  675. /*
  676. * Step 3) Get TAD range
  677. */
  678. prv = 0;
  679. for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
  680. pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
  681. &reg);
  682. limit = TAD_LIMIT(reg);
  683. if (limit <= prv)
  684. break;
  685. tmp_mb = (limit + 1) >> 20;
  686. mb = div_u64_rem(tmp_mb, 1000, &kb);
  687. edac_dbg(0, "TAD#%d: up to %u.%03u GB (0x%016Lx), socket interleave %d, memory interleave %d, TGT: %d, %d, %d, %d, reg=0x%08x\n",
  688. n_tads, mb, kb,
  689. ((u64)tmp_mb) << 20L,
  690. (u32)TAD_SOCK(reg),
  691. (u32)TAD_CH(reg),
  692. (u32)TAD_TGT0(reg),
  693. (u32)TAD_TGT1(reg),
  694. (u32)TAD_TGT2(reg),
  695. (u32)TAD_TGT3(reg),
  696. reg);
  697. prv = limit;
  698. }
  699. /*
  700. * Step 4) Get TAD offsets, per each channel
  701. */
  702. for (i = 0; i < NUM_CHANNELS; i++) {
  703. if (!pvt->channel[i].dimms)
  704. continue;
  705. for (j = 0; j < n_tads; j++) {
  706. pci_read_config_dword(pvt->pci_tad[i],
  707. tad_ch_nilv_offset[j],
  708. &reg);
  709. tmp_mb = TAD_OFFSET(reg) >> 20;
  710. mb = div_u64_rem(tmp_mb, 1000, &kb);
  711. edac_dbg(0, "TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
  712. i, j,
  713. mb, kb,
  714. ((u64)tmp_mb) << 20L,
  715. reg);
  716. }
  717. }
  718. /*
  719. * Step 6) Get RIR Wayness/Limit, per each channel
  720. */
  721. for (i = 0; i < NUM_CHANNELS; i++) {
  722. if (!pvt->channel[i].dimms)
  723. continue;
  724. for (j = 0; j < MAX_RIR_RANGES; j++) {
  725. pci_read_config_dword(pvt->pci_tad[i],
  726. rir_way_limit[j],
  727. &reg);
  728. if (!IS_RIR_VALID(reg))
  729. continue;
  730. tmp_mb = RIR_LIMIT(reg) >> 20;
  731. rir_way = 1 << RIR_WAY(reg);
  732. mb = div_u64_rem(tmp_mb, 1000, &kb);
  733. edac_dbg(0, "CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
  734. i, j,
  735. mb, kb,
  736. ((u64)tmp_mb) << 20L,
  737. rir_way,
  738. reg);
  739. for (k = 0; k < rir_way; k++) {
  740. pci_read_config_dword(pvt->pci_tad[i],
  741. rir_offset[j][k],
  742. &reg);
  743. tmp_mb = RIR_OFFSET(reg) << 6;
  744. mb = div_u64_rem(tmp_mb, 1000, &kb);
  745. edac_dbg(0, "CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
  746. i, j, k,
  747. mb, kb,
  748. ((u64)tmp_mb) << 20L,
  749. (u32)RIR_RNK_TGT(reg),
  750. reg);
  751. }
  752. }
  753. }
  754. }
  755. static struct mem_ctl_info *get_mci_for_node_id(u8 node_id)
  756. {
  757. struct sbridge_dev *sbridge_dev;
  758. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  759. if (sbridge_dev->node_id == node_id)
  760. return sbridge_dev->mci;
  761. }
  762. return NULL;
  763. }
  764. static int get_memory_error_data(struct mem_ctl_info *mci,
  765. u64 addr,
  766. u8 *socket,
  767. long *channel_mask,
  768. u8 *rank,
  769. char **area_type, char *msg)
  770. {
  771. struct mem_ctl_info *new_mci;
  772. struct sbridge_pvt *pvt = mci->pvt_info;
  773. struct pci_dev *pci_ha;
  774. int n_rir, n_sads, n_tads, sad_way, sck_xch;
  775. int sad_interl, idx, base_ch;
  776. int interleave_mode;
  777. unsigned sad_interleave[pvt->info.max_interleave];
  778. u32 reg;
  779. u8 ch_way, sck_way, pkg, sad_ha = 0;
  780. u32 tad_offset;
  781. u32 rir_way;
  782. u32 mb, kb;
  783. u64 ch_addr, offset, limit = 0, prv = 0;
  784. /*
  785. * Step 0) Check if the address is at special memory ranges
  786. * The check bellow is probably enough to fill all cases where
  787. * the error is not inside a memory, except for the legacy
  788. * range (e. g. VGA addresses). It is unlikely, however, that the
  789. * memory controller would generate an error on that range.
  790. */
  791. if ((addr > (u64) pvt->tolm) && (addr < (1LL << 32))) {
  792. sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
  793. return -EINVAL;
  794. }
  795. if (addr >= (u64)pvt->tohm) {
  796. sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
  797. return -EINVAL;
  798. }
  799. /*
  800. * Step 1) Get socket
  801. */
  802. for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
  803. pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
  804. &reg);
  805. if (!DRAM_RULE_ENABLE(reg))
  806. continue;
  807. limit = SAD_LIMIT(reg);
  808. if (limit <= prv) {
  809. sprintf(msg, "Can't discover the memory socket");
  810. return -EINVAL;
  811. }
  812. if (addr <= limit)
  813. break;
  814. prv = limit;
  815. }
  816. if (n_sads == pvt->info.max_sad) {
  817. sprintf(msg, "Can't discover the memory socket");
  818. return -EINVAL;
  819. }
  820. *area_type = get_dram_attr(reg);
  821. interleave_mode = INTERLEAVE_MODE(reg);
  822. pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
  823. &reg);
  824. if (pvt->info.type == SANDY_BRIDGE) {
  825. sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
  826. for (sad_way = 0; sad_way < 8; sad_way++) {
  827. u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, sad_way);
  828. if (sad_way > 0 && sad_interl == pkg)
  829. break;
  830. sad_interleave[sad_way] = pkg;
  831. edac_dbg(0, "SAD interleave #%d: %d\n",
  832. sad_way, sad_interleave[sad_way]);
  833. }
  834. edac_dbg(0, "mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
  835. pvt->sbridge_dev->mc,
  836. n_sads,
  837. addr,
  838. limit,
  839. sad_way + 7,
  840. !interleave_mode ? "" : "XOR[18:16]");
  841. if (interleave_mode)
  842. idx = ((addr >> 6) ^ (addr >> 16)) & 7;
  843. else
  844. idx = (addr >> 6) & 7;
  845. switch (sad_way) {
  846. case 1:
  847. idx = 0;
  848. break;
  849. case 2:
  850. idx = idx & 1;
  851. break;
  852. case 4:
  853. idx = idx & 3;
  854. break;
  855. case 8:
  856. break;
  857. default:
  858. sprintf(msg, "Can't discover socket interleave");
  859. return -EINVAL;
  860. }
  861. *socket = sad_interleave[idx];
  862. edac_dbg(0, "SAD interleave index: %d (wayness %d) = CPU socket %d\n",
  863. idx, sad_way, *socket);
  864. } else {
  865. /* Ivy Bridge's SAD mode doesn't support XOR interleave mode */
  866. idx = (addr >> 6) & 7;
  867. pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
  868. *socket = sad_pkg_socket(pkg);
  869. sad_ha = sad_pkg_ha(pkg);
  870. edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %d\n",
  871. idx, *socket, sad_ha);
  872. }
  873. /*
  874. * Move to the proper node structure, in order to access the
  875. * right PCI registers
  876. */
  877. new_mci = get_mci_for_node_id(*socket);
  878. if (!new_mci) {
  879. sprintf(msg, "Struct for socket #%u wasn't initialized",
  880. *socket);
  881. return -EINVAL;
  882. }
  883. mci = new_mci;
  884. pvt = mci->pvt_info;
  885. /*
  886. * Step 2) Get memory channel
  887. */
  888. prv = 0;
  889. if (pvt->info.type == SANDY_BRIDGE)
  890. pci_ha = pvt->pci_ha0;
  891. else {
  892. if (sad_ha)
  893. pci_ha = pvt->pci_ha1;
  894. else
  895. pci_ha = pvt->pci_ha0;
  896. }
  897. for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
  898. pci_read_config_dword(pci_ha, tad_dram_rule[n_tads], &reg);
  899. limit = TAD_LIMIT(reg);
  900. if (limit <= prv) {
  901. sprintf(msg, "Can't discover the memory channel");
  902. return -EINVAL;
  903. }
  904. if (addr <= limit)
  905. break;
  906. prv = limit;
  907. }
  908. if (n_tads == MAX_TAD) {
  909. sprintf(msg, "Can't discover the memory channel");
  910. return -EINVAL;
  911. }
  912. ch_way = TAD_CH(reg) + 1;
  913. sck_way = TAD_SOCK(reg) + 1;
  914. if (ch_way == 3)
  915. idx = addr >> 6;
  916. else
  917. idx = addr >> (6 + sck_way);
  918. idx = idx % ch_way;
  919. /*
  920. * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
  921. */
  922. switch (idx) {
  923. case 0:
  924. base_ch = TAD_TGT0(reg);
  925. break;
  926. case 1:
  927. base_ch = TAD_TGT1(reg);
  928. break;
  929. case 2:
  930. base_ch = TAD_TGT2(reg);
  931. break;
  932. case 3:
  933. base_ch = TAD_TGT3(reg);
  934. break;
  935. default:
  936. sprintf(msg, "Can't discover the TAD target");
  937. return -EINVAL;
  938. }
  939. *channel_mask = 1 << base_ch;
  940. pci_read_config_dword(pvt->pci_tad[base_ch],
  941. tad_ch_nilv_offset[n_tads],
  942. &tad_offset);
  943. if (pvt->is_mirrored) {
  944. *channel_mask |= 1 << ((base_ch + 2) % 4);
  945. switch(ch_way) {
  946. case 2:
  947. case 4:
  948. sck_xch = 1 << sck_way * (ch_way >> 1);
  949. break;
  950. default:
  951. sprintf(msg, "Invalid mirror set. Can't decode addr");
  952. return -EINVAL;
  953. }
  954. } else
  955. sck_xch = (1 << sck_way) * ch_way;
  956. if (pvt->is_lockstep)
  957. *channel_mask |= 1 << ((base_ch + 1) % 4);
  958. offset = TAD_OFFSET(tad_offset);
  959. edac_dbg(0, "TAD#%d: address 0x%016Lx < 0x%016Lx, socket interleave %d, channel interleave %d (offset 0x%08Lx), index %d, base ch: %d, ch mask: 0x%02lx\n",
  960. n_tads,
  961. addr,
  962. limit,
  963. (u32)TAD_SOCK(reg),
  964. ch_way,
  965. offset,
  966. idx,
  967. base_ch,
  968. *channel_mask);
  969. /* Calculate channel address */
  970. /* Remove the TAD offset */
  971. if (offset > addr) {
  972. sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
  973. offset, addr);
  974. return -EINVAL;
  975. }
  976. addr -= offset;
  977. /* Store the low bits [0:6] of the addr */
  978. ch_addr = addr & 0x7f;
  979. /* Remove socket wayness and remove 6 bits */
  980. addr >>= 6;
  981. addr = div_u64(addr, sck_xch);
  982. #if 0
  983. /* Divide by channel way */
  984. addr = addr / ch_way;
  985. #endif
  986. /* Recover the last 6 bits */
  987. ch_addr |= addr << 6;
  988. /*
  989. * Step 3) Decode rank
  990. */
  991. for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
  992. pci_read_config_dword(pvt->pci_tad[base_ch],
  993. rir_way_limit[n_rir],
  994. &reg);
  995. if (!IS_RIR_VALID(reg))
  996. continue;
  997. limit = RIR_LIMIT(reg);
  998. mb = div_u64_rem(limit >> 20, 1000, &kb);
  999. edac_dbg(0, "RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
  1000. n_rir,
  1001. mb, kb,
  1002. limit,
  1003. 1 << RIR_WAY(reg));
  1004. if (ch_addr <= limit)
  1005. break;
  1006. }
  1007. if (n_rir == MAX_RIR_RANGES) {
  1008. sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
  1009. ch_addr);
  1010. return -EINVAL;
  1011. }
  1012. rir_way = RIR_WAY(reg);
  1013. if (pvt->is_close_pg)
  1014. idx = (ch_addr >> 6);
  1015. else
  1016. idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
  1017. idx %= 1 << rir_way;
  1018. pci_read_config_dword(pvt->pci_tad[base_ch],
  1019. rir_offset[n_rir][idx],
  1020. &reg);
  1021. *rank = RIR_RNK_TGT(reg);
  1022. edac_dbg(0, "RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
  1023. n_rir,
  1024. ch_addr,
  1025. limit,
  1026. rir_way,
  1027. idx);
  1028. return 0;
  1029. }
  1030. /****************************************************************************
  1031. Device initialization routines: put/get, init/exit
  1032. ****************************************************************************/
  1033. /*
  1034. * sbridge_put_all_devices 'put' all the devices that we have
  1035. * reserved via 'get'
  1036. */
  1037. static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
  1038. {
  1039. int i;
  1040. edac_dbg(0, "\n");
  1041. for (i = 0; i < sbridge_dev->n_devs; i++) {
  1042. struct pci_dev *pdev = sbridge_dev->pdev[i];
  1043. if (!pdev)
  1044. continue;
  1045. edac_dbg(0, "Removing dev %02x:%02x.%d\n",
  1046. pdev->bus->number,
  1047. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  1048. pci_dev_put(pdev);
  1049. }
  1050. }
  1051. static void sbridge_put_all_devices(void)
  1052. {
  1053. struct sbridge_dev *sbridge_dev, *tmp;
  1054. list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
  1055. sbridge_put_devices(sbridge_dev);
  1056. free_sbridge_dev(sbridge_dev);
  1057. }
  1058. }
  1059. static int sbridge_get_onedevice(struct pci_dev **prev,
  1060. u8 *num_mc,
  1061. const struct pci_id_table *table,
  1062. const unsigned devno)
  1063. {
  1064. struct sbridge_dev *sbridge_dev;
  1065. const struct pci_id_descr *dev_descr = &table->descr[devno];
  1066. struct pci_dev *pdev = NULL;
  1067. u8 bus = 0;
  1068. sbridge_printk(KERN_DEBUG,
  1069. "Seeking for: dev %02x.%d PCI ID %04x:%04x\n",
  1070. dev_descr->dev, dev_descr->func,
  1071. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1072. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1073. dev_descr->dev_id, *prev);
  1074. if (!pdev) {
  1075. if (*prev) {
  1076. *prev = pdev;
  1077. return 0;
  1078. }
  1079. if (dev_descr->optional)
  1080. return 0;
  1081. if (devno == 0)
  1082. return -ENODEV;
  1083. sbridge_printk(KERN_INFO,
  1084. "Device not found: dev %02x.%d PCI ID %04x:%04x\n",
  1085. dev_descr->dev, dev_descr->func,
  1086. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1087. /* End of list, leave */
  1088. return -ENODEV;
  1089. }
  1090. bus = pdev->bus->number;
  1091. sbridge_dev = get_sbridge_dev(bus);
  1092. if (!sbridge_dev) {
  1093. sbridge_dev = alloc_sbridge_dev(bus, table);
  1094. if (!sbridge_dev) {
  1095. pci_dev_put(pdev);
  1096. return -ENOMEM;
  1097. }
  1098. (*num_mc)++;
  1099. }
  1100. if (sbridge_dev->pdev[devno]) {
  1101. sbridge_printk(KERN_ERR,
  1102. "Duplicated device for "
  1103. "dev %02x:%d.%d PCI ID %04x:%04x\n",
  1104. bus, dev_descr->dev, dev_descr->func,
  1105. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1106. pci_dev_put(pdev);
  1107. return -ENODEV;
  1108. }
  1109. sbridge_dev->pdev[devno] = pdev;
  1110. /* Sanity check */
  1111. if (unlikely(PCI_SLOT(pdev->devfn) != dev_descr->dev ||
  1112. PCI_FUNC(pdev->devfn) != dev_descr->func)) {
  1113. sbridge_printk(KERN_ERR,
  1114. "Device PCI ID %04x:%04x "
  1115. "has dev %02x:%d.%d instead of dev %02x:%02x.%d\n",
  1116. PCI_VENDOR_ID_INTEL, dev_descr->dev_id,
  1117. bus, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1118. bus, dev_descr->dev, dev_descr->func);
  1119. return -ENODEV;
  1120. }
  1121. /* Be sure that the device is enabled */
  1122. if (unlikely(pci_enable_device(pdev) < 0)) {
  1123. sbridge_printk(KERN_ERR,
  1124. "Couldn't enable "
  1125. "dev %02x:%d.%d PCI ID %04x:%04x\n",
  1126. bus, dev_descr->dev, dev_descr->func,
  1127. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1128. return -ENODEV;
  1129. }
  1130. edac_dbg(0, "Detected dev %02x:%d.%d PCI ID %04x:%04x\n",
  1131. bus, dev_descr->dev, dev_descr->func,
  1132. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1133. /*
  1134. * As stated on drivers/pci/search.c, the reference count for
  1135. * @from is always decremented if it is not %NULL. So, as we need
  1136. * to get all devices up to null, we need to do a get for the device
  1137. */
  1138. pci_dev_get(pdev);
  1139. *prev = pdev;
  1140. return 0;
  1141. }
  1142. /*
  1143. * sbridge_get_all_devices - Find and perform 'get' operation on the MCH's
  1144. * device/functions we want to reference for this driver.
  1145. * Need to 'get' device 16 func 1 and func 2.
  1146. * @num_mc: pointer to the memory controllers count, to be incremented in case
  1147. * of success.
  1148. * @table: model specific table
  1149. *
  1150. * returns 0 in case of success or error code
  1151. */
  1152. static int sbridge_get_all_devices(u8 *num_mc,
  1153. const struct pci_id_table *table)
  1154. {
  1155. int i, rc;
  1156. struct pci_dev *pdev = NULL;
  1157. while (table && table->descr) {
  1158. for (i = 0; i < table->n_devs; i++) {
  1159. pdev = NULL;
  1160. do {
  1161. rc = sbridge_get_onedevice(&pdev, num_mc,
  1162. table, i);
  1163. if (rc < 0) {
  1164. if (i == 0) {
  1165. i = table->n_devs;
  1166. break;
  1167. }
  1168. sbridge_put_all_devices();
  1169. return -ENODEV;
  1170. }
  1171. } while (pdev);
  1172. }
  1173. table++;
  1174. }
  1175. return 0;
  1176. }
  1177. static int sbridge_mci_bind_devs(struct mem_ctl_info *mci,
  1178. struct sbridge_dev *sbridge_dev)
  1179. {
  1180. struct sbridge_pvt *pvt = mci->pvt_info;
  1181. struct pci_dev *pdev;
  1182. int i, func, slot;
  1183. for (i = 0; i < sbridge_dev->n_devs; i++) {
  1184. pdev = sbridge_dev->pdev[i];
  1185. if (!pdev)
  1186. continue;
  1187. slot = PCI_SLOT(pdev->devfn);
  1188. func = PCI_FUNC(pdev->devfn);
  1189. switch (slot) {
  1190. case 12:
  1191. switch (func) {
  1192. case 6:
  1193. pvt->pci_sad0 = pdev;
  1194. break;
  1195. case 7:
  1196. pvt->pci_sad1 = pdev;
  1197. break;
  1198. default:
  1199. goto error;
  1200. }
  1201. break;
  1202. case 13:
  1203. switch (func) {
  1204. case 6:
  1205. pvt->pci_br0 = pdev;
  1206. break;
  1207. default:
  1208. goto error;
  1209. }
  1210. break;
  1211. case 14:
  1212. switch (func) {
  1213. case 0:
  1214. pvt->pci_ha0 = pdev;
  1215. break;
  1216. default:
  1217. goto error;
  1218. }
  1219. break;
  1220. case 15:
  1221. switch (func) {
  1222. case 0:
  1223. pvt->pci_ta = pdev;
  1224. break;
  1225. case 1:
  1226. pvt->pci_ras = pdev;
  1227. break;
  1228. case 2:
  1229. case 3:
  1230. case 4:
  1231. case 5:
  1232. pvt->pci_tad[func - 2] = pdev;
  1233. break;
  1234. default:
  1235. goto error;
  1236. }
  1237. break;
  1238. case 17:
  1239. switch (func) {
  1240. case 0:
  1241. pvt->pci_ddrio = pdev;
  1242. break;
  1243. default:
  1244. goto error;
  1245. }
  1246. break;
  1247. default:
  1248. goto error;
  1249. }
  1250. edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
  1251. sbridge_dev->bus,
  1252. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1253. pdev);
  1254. }
  1255. /* Check if everything were registered */
  1256. if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha0 ||
  1257. !pvt-> pci_tad || !pvt->pci_ras || !pvt->pci_ta)
  1258. goto enodev;
  1259. for (i = 0; i < NUM_CHANNELS; i++) {
  1260. if (!pvt->pci_tad[i])
  1261. goto enodev;
  1262. }
  1263. return 0;
  1264. enodev:
  1265. sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
  1266. return -ENODEV;
  1267. error:
  1268. sbridge_printk(KERN_ERR, "Device %d, function %d "
  1269. "is out of the expected range\n",
  1270. slot, func);
  1271. return -EINVAL;
  1272. }
  1273. static int ibridge_mci_bind_devs(struct mem_ctl_info *mci,
  1274. struct sbridge_dev *sbridge_dev)
  1275. {
  1276. struct sbridge_pvt *pvt = mci->pvt_info;
  1277. struct pci_dev *pdev, *tmp;
  1278. int i, func, slot;
  1279. bool mode_2ha = false;
  1280. tmp = pci_get_device(PCI_VENDOR_ID_INTEL,
  1281. PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, NULL);
  1282. if (tmp) {
  1283. mode_2ha = true;
  1284. pci_dev_put(tmp);
  1285. }
  1286. for (i = 0; i < sbridge_dev->n_devs; i++) {
  1287. pdev = sbridge_dev->pdev[i];
  1288. if (!pdev)
  1289. continue;
  1290. slot = PCI_SLOT(pdev->devfn);
  1291. func = PCI_FUNC(pdev->devfn);
  1292. switch (slot) {
  1293. case 14:
  1294. if (func == 0) {
  1295. pvt->pci_ha0 = pdev;
  1296. break;
  1297. }
  1298. goto error;
  1299. case 15:
  1300. switch (func) {
  1301. case 0:
  1302. pvt->pci_ta = pdev;
  1303. break;
  1304. case 1:
  1305. pvt->pci_ras = pdev;
  1306. break;
  1307. case 4:
  1308. case 5:
  1309. /* if we have 2 HAs active, channels 2 and 3
  1310. * are in other device */
  1311. if (mode_2ha)
  1312. break;
  1313. /* fall through */
  1314. case 2:
  1315. case 3:
  1316. pvt->pci_tad[func - 2] = pdev;
  1317. break;
  1318. default:
  1319. goto error;
  1320. }
  1321. break;
  1322. case 17:
  1323. if (func == 4) {
  1324. pvt->pci_ddrio = pdev;
  1325. break;
  1326. } else if (func == 0) {
  1327. if (!mode_2ha)
  1328. pvt->pci_ddrio = pdev;
  1329. break;
  1330. }
  1331. goto error;
  1332. case 22:
  1333. switch (func) {
  1334. case 0:
  1335. pvt->pci_sad0 = pdev;
  1336. break;
  1337. case 1:
  1338. pvt->pci_br0 = pdev;
  1339. break;
  1340. case 2:
  1341. pvt->pci_br1 = pdev;
  1342. break;
  1343. default:
  1344. goto error;
  1345. }
  1346. break;
  1347. case 28:
  1348. if (func == 0) {
  1349. pvt->pci_ha1 = pdev;
  1350. break;
  1351. }
  1352. goto error;
  1353. case 29:
  1354. /* we shouldn't have this device if we have just one
  1355. * HA present */
  1356. WARN_ON(!mode_2ha);
  1357. if (func == 2 || func == 3) {
  1358. pvt->pci_tad[func] = pdev;
  1359. break;
  1360. }
  1361. goto error;
  1362. default:
  1363. goto error;
  1364. }
  1365. edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
  1366. sbridge_dev->bus,
  1367. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1368. pdev);
  1369. }
  1370. /* Check if everything were registered */
  1371. if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_br0 ||
  1372. !pvt->pci_br1 || !pvt->pci_tad || !pvt->pci_ras ||
  1373. !pvt->pci_ta)
  1374. goto enodev;
  1375. for (i = 0; i < NUM_CHANNELS; i++) {
  1376. if (!pvt->pci_tad[i])
  1377. goto enodev;
  1378. }
  1379. return 0;
  1380. enodev:
  1381. sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
  1382. return -ENODEV;
  1383. error:
  1384. sbridge_printk(KERN_ERR,
  1385. "Device %d, function %d is out of the expected range\n",
  1386. slot, func);
  1387. return -EINVAL;
  1388. }
  1389. /****************************************************************************
  1390. Error check routines
  1391. ****************************************************************************/
  1392. /*
  1393. * While Sandy Bridge has error count registers, SMI BIOS read values from
  1394. * and resets the counters. So, they are not reliable for the OS to read
  1395. * from them. So, we have no option but to just trust on whatever MCE is
  1396. * telling us about the errors.
  1397. */
  1398. static void sbridge_mce_output_error(struct mem_ctl_info *mci,
  1399. const struct mce *m)
  1400. {
  1401. struct mem_ctl_info *new_mci;
  1402. struct sbridge_pvt *pvt = mci->pvt_info;
  1403. enum hw_event_mc_err_type tp_event;
  1404. char *type, *optype, msg[256];
  1405. bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
  1406. bool overflow = GET_BITFIELD(m->status, 62, 62);
  1407. bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
  1408. bool recoverable;
  1409. u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
  1410. u32 mscod = GET_BITFIELD(m->status, 16, 31);
  1411. u32 errcode = GET_BITFIELD(m->status, 0, 15);
  1412. u32 channel = GET_BITFIELD(m->status, 0, 3);
  1413. u32 optypenum = GET_BITFIELD(m->status, 4, 6);
  1414. long channel_mask, first_channel;
  1415. u8 rank, socket;
  1416. int rc, dimm;
  1417. char *area_type = NULL;
  1418. if (pvt->info.type == IVY_BRIDGE)
  1419. recoverable = true;
  1420. else
  1421. recoverable = GET_BITFIELD(m->status, 56, 56);
  1422. if (uncorrected_error) {
  1423. if (ripv) {
  1424. type = "FATAL";
  1425. tp_event = HW_EVENT_ERR_FATAL;
  1426. } else {
  1427. type = "NON_FATAL";
  1428. tp_event = HW_EVENT_ERR_UNCORRECTED;
  1429. }
  1430. } else {
  1431. type = "CORRECTED";
  1432. tp_event = HW_EVENT_ERR_CORRECTED;
  1433. }
  1434. /*
  1435. * According with Table 15-9 of the Intel Architecture spec vol 3A,
  1436. * memory errors should fit in this mask:
  1437. * 000f 0000 1mmm cccc (binary)
  1438. * where:
  1439. * f = Correction Report Filtering Bit. If 1, subsequent errors
  1440. * won't be shown
  1441. * mmm = error type
  1442. * cccc = channel
  1443. * If the mask doesn't match, report an error to the parsing logic
  1444. */
  1445. if (! ((errcode & 0xef80) == 0x80)) {
  1446. optype = "Can't parse: it is not a mem";
  1447. } else {
  1448. switch (optypenum) {
  1449. case 0:
  1450. optype = "generic undef request error";
  1451. break;
  1452. case 1:
  1453. optype = "memory read error";
  1454. break;
  1455. case 2:
  1456. optype = "memory write error";
  1457. break;
  1458. case 3:
  1459. optype = "addr/cmd error";
  1460. break;
  1461. case 4:
  1462. optype = "memory scrubbing error";
  1463. break;
  1464. default:
  1465. optype = "reserved";
  1466. break;
  1467. }
  1468. }
  1469. /* Only decode errors with an valid address (ADDRV) */
  1470. if (!GET_BITFIELD(m->status, 58, 58))
  1471. return;
  1472. rc = get_memory_error_data(mci, m->addr, &socket,
  1473. &channel_mask, &rank, &area_type, msg);
  1474. if (rc < 0)
  1475. goto err_parsing;
  1476. new_mci = get_mci_for_node_id(socket);
  1477. if (!new_mci) {
  1478. strcpy(msg, "Error: socket got corrupted!");
  1479. goto err_parsing;
  1480. }
  1481. mci = new_mci;
  1482. pvt = mci->pvt_info;
  1483. first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
  1484. if (rank < 4)
  1485. dimm = 0;
  1486. else if (rank < 8)
  1487. dimm = 1;
  1488. else
  1489. dimm = 2;
  1490. /*
  1491. * FIXME: On some memory configurations (mirror, lockstep), the
  1492. * Memory Controller can't point the error to a single DIMM. The
  1493. * EDAC core should be handling the channel mask, in order to point
  1494. * to the group of dimm's where the error may be happening.
  1495. */
  1496. snprintf(msg, sizeof(msg),
  1497. "%s%s area:%s err_code:%04x:%04x socket:%d channel_mask:%ld rank:%d",
  1498. overflow ? " OVERFLOW" : "",
  1499. (uncorrected_error && recoverable) ? " recoverable" : "",
  1500. area_type,
  1501. mscod, errcode,
  1502. socket,
  1503. channel_mask,
  1504. rank);
  1505. edac_dbg(0, "%s\n", msg);
  1506. /* FIXME: need support for channel mask */
  1507. /* Call the helper to output message */
  1508. edac_mc_handle_error(tp_event, mci, core_err_cnt,
  1509. m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
  1510. channel, dimm, -1,
  1511. optype, msg);
  1512. return;
  1513. err_parsing:
  1514. edac_mc_handle_error(tp_event, mci, core_err_cnt, 0, 0, 0,
  1515. -1, -1, -1,
  1516. msg, "");
  1517. }
  1518. /*
  1519. * sbridge_check_error Retrieve and process errors reported by the
  1520. * hardware. Called by the Core module.
  1521. */
  1522. static void sbridge_check_error(struct mem_ctl_info *mci)
  1523. {
  1524. struct sbridge_pvt *pvt = mci->pvt_info;
  1525. int i;
  1526. unsigned count = 0;
  1527. struct mce *m;
  1528. /*
  1529. * MCE first step: Copy all mce errors into a temporary buffer
  1530. * We use a double buffering here, to reduce the risk of
  1531. * loosing an error.
  1532. */
  1533. smp_rmb();
  1534. count = (pvt->mce_out + MCE_LOG_LEN - pvt->mce_in)
  1535. % MCE_LOG_LEN;
  1536. if (!count)
  1537. return;
  1538. m = pvt->mce_outentry;
  1539. if (pvt->mce_in + count > MCE_LOG_LEN) {
  1540. unsigned l = MCE_LOG_LEN - pvt->mce_in;
  1541. memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * l);
  1542. smp_wmb();
  1543. pvt->mce_in = 0;
  1544. count -= l;
  1545. m += l;
  1546. }
  1547. memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * count);
  1548. smp_wmb();
  1549. pvt->mce_in += count;
  1550. smp_rmb();
  1551. if (pvt->mce_overrun) {
  1552. sbridge_printk(KERN_ERR, "Lost %d memory errors\n",
  1553. pvt->mce_overrun);
  1554. smp_wmb();
  1555. pvt->mce_overrun = 0;
  1556. }
  1557. /*
  1558. * MCE second step: parse errors and display
  1559. */
  1560. for (i = 0; i < count; i++)
  1561. sbridge_mce_output_error(mci, &pvt->mce_outentry[i]);
  1562. }
  1563. /*
  1564. * sbridge_mce_check_error Replicates mcelog routine to get errors
  1565. * This routine simply queues mcelog errors, and
  1566. * return. The error itself should be handled later
  1567. * by sbridge_check_error.
  1568. * WARNING: As this routine should be called at NMI time, extra care should
  1569. * be taken to avoid deadlocks, and to be as fast as possible.
  1570. */
  1571. static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
  1572. void *data)
  1573. {
  1574. struct mce *mce = (struct mce *)data;
  1575. struct mem_ctl_info *mci;
  1576. struct sbridge_pvt *pvt;
  1577. char *type;
  1578. if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
  1579. return NOTIFY_DONE;
  1580. mci = get_mci_for_node_id(mce->socketid);
  1581. if (!mci)
  1582. return NOTIFY_BAD;
  1583. pvt = mci->pvt_info;
  1584. /*
  1585. * Just let mcelog handle it if the error is
  1586. * outside the memory controller. A memory error
  1587. * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
  1588. * bit 12 has an special meaning.
  1589. */
  1590. if ((mce->status & 0xefff) >> 7 != 1)
  1591. return NOTIFY_DONE;
  1592. if (mce->mcgstatus & MCG_STATUS_MCIP)
  1593. type = "Exception";
  1594. else
  1595. type = "Event";
  1596. sbridge_mc_printk(mci, KERN_DEBUG, "HANDLING MCE MEMORY ERROR\n");
  1597. sbridge_mc_printk(mci, KERN_DEBUG, "CPU %d: Machine Check %s: %Lx "
  1598. "Bank %d: %016Lx\n", mce->extcpu, type,
  1599. mce->mcgstatus, mce->bank, mce->status);
  1600. sbridge_mc_printk(mci, KERN_DEBUG, "TSC %llx ", mce->tsc);
  1601. sbridge_mc_printk(mci, KERN_DEBUG, "ADDR %llx ", mce->addr);
  1602. sbridge_mc_printk(mci, KERN_DEBUG, "MISC %llx ", mce->misc);
  1603. sbridge_mc_printk(mci, KERN_DEBUG, "PROCESSOR %u:%x TIME %llu SOCKET "
  1604. "%u APIC %x\n", mce->cpuvendor, mce->cpuid,
  1605. mce->time, mce->socketid, mce->apicid);
  1606. /* Only handle if it is the right mc controller */
  1607. if (cpu_data(mce->cpu).phys_proc_id != pvt->sbridge_dev->mc)
  1608. return NOTIFY_DONE;
  1609. smp_rmb();
  1610. if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
  1611. smp_wmb();
  1612. pvt->mce_overrun++;
  1613. return NOTIFY_DONE;
  1614. }
  1615. /* Copy memory error at the ringbuffer */
  1616. memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
  1617. smp_wmb();
  1618. pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
  1619. /* Handle fatal errors immediately */
  1620. if (mce->mcgstatus & 1)
  1621. sbridge_check_error(mci);
  1622. /* Advice mcelog that the error were handled */
  1623. return NOTIFY_STOP;
  1624. }
  1625. static struct notifier_block sbridge_mce_dec = {
  1626. .notifier_call = sbridge_mce_check_error,
  1627. };
  1628. /****************************************************************************
  1629. EDAC register/unregister logic
  1630. ****************************************************************************/
  1631. static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
  1632. {
  1633. struct mem_ctl_info *mci = sbridge_dev->mci;
  1634. struct sbridge_pvt *pvt;
  1635. if (unlikely(!mci || !mci->pvt_info)) {
  1636. edac_dbg(0, "MC: dev = %p\n", &sbridge_dev->pdev[0]->dev);
  1637. sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
  1638. return;
  1639. }
  1640. pvt = mci->pvt_info;
  1641. edac_dbg(0, "MC: mci = %p, dev = %p\n",
  1642. mci, &sbridge_dev->pdev[0]->dev);
  1643. /* Remove MC sysfs nodes */
  1644. edac_mc_del_mc(mci->pdev);
  1645. edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
  1646. kfree(mci->ctl_name);
  1647. edac_mc_free(mci);
  1648. sbridge_dev->mci = NULL;
  1649. }
  1650. static int sbridge_register_mci(struct sbridge_dev *sbridge_dev, enum type type)
  1651. {
  1652. struct mem_ctl_info *mci;
  1653. struct edac_mc_layer layers[2];
  1654. struct sbridge_pvt *pvt;
  1655. struct pci_dev *pdev = sbridge_dev->pdev[0];
  1656. int rc;
  1657. /* Check the number of active and not disabled channels */
  1658. rc = check_if_ecc_is_active(sbridge_dev->bus);
  1659. if (unlikely(rc < 0))
  1660. return rc;
  1661. /* allocate a new MC control structure */
  1662. layers[0].type = EDAC_MC_LAYER_CHANNEL;
  1663. layers[0].size = NUM_CHANNELS;
  1664. layers[0].is_virt_csrow = false;
  1665. layers[1].type = EDAC_MC_LAYER_SLOT;
  1666. layers[1].size = MAX_DIMMS;
  1667. layers[1].is_virt_csrow = true;
  1668. mci = edac_mc_alloc(sbridge_dev->mc, ARRAY_SIZE(layers), layers,
  1669. sizeof(*pvt));
  1670. if (unlikely(!mci))
  1671. return -ENOMEM;
  1672. edac_dbg(0, "MC: mci = %p, dev = %p\n",
  1673. mci, &pdev->dev);
  1674. pvt = mci->pvt_info;
  1675. memset(pvt, 0, sizeof(*pvt));
  1676. /* Associate sbridge_dev and mci for future usage */
  1677. pvt->sbridge_dev = sbridge_dev;
  1678. sbridge_dev->mci = mci;
  1679. mci->mtype_cap = MEM_FLAG_DDR3;
  1680. mci->edac_ctl_cap = EDAC_FLAG_NONE;
  1681. mci->edac_cap = EDAC_FLAG_NONE;
  1682. mci->mod_name = "sbridge_edac.c";
  1683. mci->mod_ver = SBRIDGE_REVISION;
  1684. mci->dev_name = pci_name(pdev);
  1685. mci->ctl_page_to_phys = NULL;
  1686. /* Set the function pointer to an actual operation function */
  1687. mci->edac_check = sbridge_check_error;
  1688. pvt->info.type = type;
  1689. if (type == IVY_BRIDGE) {
  1690. pvt->info.rankcfgr = IB_RANK_CFG_A;
  1691. pvt->info.get_tolm = ibridge_get_tolm;
  1692. pvt->info.get_tohm = ibridge_get_tohm;
  1693. pvt->info.dram_rule = ibridge_dram_rule;
  1694. pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
  1695. pvt->info.interleave_list = ibridge_interleave_list;
  1696. pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
  1697. pvt->info.interleave_pkg = ibridge_interleave_pkg;
  1698. mci->ctl_name = kasprintf(GFP_KERNEL, "Ivy Bridge Socket#%d", mci->mc_idx);
  1699. /* Store pci devices at mci for faster access */
  1700. rc = ibridge_mci_bind_devs(mci, sbridge_dev);
  1701. if (unlikely(rc < 0))
  1702. goto fail0;
  1703. } else {
  1704. pvt->info.rankcfgr = SB_RANK_CFG_A;
  1705. pvt->info.get_tolm = sbridge_get_tolm;
  1706. pvt->info.get_tohm = sbridge_get_tohm;
  1707. pvt->info.dram_rule = sbridge_dram_rule;
  1708. pvt->info.max_sad = ARRAY_SIZE(sbridge_dram_rule);
  1709. pvt->info.interleave_list = sbridge_interleave_list;
  1710. pvt->info.max_interleave = ARRAY_SIZE(sbridge_interleave_list);
  1711. pvt->info.interleave_pkg = sbridge_interleave_pkg;
  1712. mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge Socket#%d", mci->mc_idx);
  1713. /* Store pci devices at mci for faster access */
  1714. rc = sbridge_mci_bind_devs(mci, sbridge_dev);
  1715. if (unlikely(rc < 0))
  1716. goto fail0;
  1717. }
  1718. /* Get dimm basic config and the memory layout */
  1719. get_dimm_config(mci);
  1720. get_memory_layout(mci);
  1721. /* record ptr to the generic device */
  1722. mci->pdev = &pdev->dev;
  1723. /* add this new MC control structure to EDAC's list of MCs */
  1724. if (unlikely(edac_mc_add_mc(mci))) {
  1725. edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
  1726. rc = -EINVAL;
  1727. goto fail0;
  1728. }
  1729. return 0;
  1730. fail0:
  1731. kfree(mci->ctl_name);
  1732. edac_mc_free(mci);
  1733. sbridge_dev->mci = NULL;
  1734. return rc;
  1735. }
  1736. /*
  1737. * sbridge_probe Probe for ONE instance of device to see if it is
  1738. * present.
  1739. * return:
  1740. * 0 for FOUND a device
  1741. * < 0 for error code
  1742. */
  1743. static int sbridge_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1744. {
  1745. int rc;
  1746. u8 mc, num_mc = 0;
  1747. struct sbridge_dev *sbridge_dev;
  1748. enum type type;
  1749. /* get the pci devices we want to reserve for our use */
  1750. mutex_lock(&sbridge_edac_lock);
  1751. /*
  1752. * All memory controllers are allocated at the first pass.
  1753. */
  1754. if (unlikely(probed >= 1)) {
  1755. mutex_unlock(&sbridge_edac_lock);
  1756. return -ENODEV;
  1757. }
  1758. probed++;
  1759. if (pdev->device == PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA) {
  1760. rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_ibridge_table);
  1761. type = IVY_BRIDGE;
  1762. } else {
  1763. rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_sbridge_table);
  1764. type = SANDY_BRIDGE;
  1765. }
  1766. if (unlikely(rc < 0))
  1767. goto fail0;
  1768. mc = 0;
  1769. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  1770. edac_dbg(0, "Registering MC#%d (%d of %d)\n",
  1771. mc, mc + 1, num_mc);
  1772. sbridge_dev->mc = mc++;
  1773. rc = sbridge_register_mci(sbridge_dev, type);
  1774. if (unlikely(rc < 0))
  1775. goto fail1;
  1776. }
  1777. sbridge_printk(KERN_INFO, "Driver loaded.\n");
  1778. mutex_unlock(&sbridge_edac_lock);
  1779. return 0;
  1780. fail1:
  1781. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
  1782. sbridge_unregister_mci(sbridge_dev);
  1783. sbridge_put_all_devices();
  1784. fail0:
  1785. mutex_unlock(&sbridge_edac_lock);
  1786. return rc;
  1787. }
  1788. /*
  1789. * sbridge_remove destructor for one instance of device
  1790. *
  1791. */
  1792. static void sbridge_remove(struct pci_dev *pdev)
  1793. {
  1794. struct sbridge_dev *sbridge_dev;
  1795. edac_dbg(0, "\n");
  1796. /*
  1797. * we have a trouble here: pdev value for removal will be wrong, since
  1798. * it will point to the X58 register used to detect that the machine
  1799. * is a Nehalem or upper design. However, due to the way several PCI
  1800. * devices are grouped together to provide MC functionality, we need
  1801. * to use a different method for releasing the devices
  1802. */
  1803. mutex_lock(&sbridge_edac_lock);
  1804. if (unlikely(!probed)) {
  1805. mutex_unlock(&sbridge_edac_lock);
  1806. return;
  1807. }
  1808. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
  1809. sbridge_unregister_mci(sbridge_dev);
  1810. /* Release PCI resources */
  1811. sbridge_put_all_devices();
  1812. probed--;
  1813. mutex_unlock(&sbridge_edac_lock);
  1814. }
  1815. MODULE_DEVICE_TABLE(pci, sbridge_pci_tbl);
  1816. /*
  1817. * sbridge_driver pci_driver structure for this module
  1818. *
  1819. */
  1820. static struct pci_driver sbridge_driver = {
  1821. .name = "sbridge_edac",
  1822. .probe = sbridge_probe,
  1823. .remove = sbridge_remove,
  1824. .id_table = sbridge_pci_tbl,
  1825. };
  1826. /*
  1827. * sbridge_init Module entry function
  1828. * Try to initialize this module for its devices
  1829. */
  1830. static int __init sbridge_init(void)
  1831. {
  1832. int pci_rc;
  1833. edac_dbg(2, "\n");
  1834. /* Ensure that the OPSTATE is set correctly for POLL or NMI */
  1835. opstate_init();
  1836. pci_rc = pci_register_driver(&sbridge_driver);
  1837. if (pci_rc >= 0) {
  1838. mce_register_decode_chain(&sbridge_mce_dec);
  1839. if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
  1840. sbridge_printk(KERN_WARNING, "Loading driver, error reporting disabled.\n");
  1841. return 0;
  1842. }
  1843. sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
  1844. pci_rc);
  1845. return pci_rc;
  1846. }
  1847. /*
  1848. * sbridge_exit() Module exit function
  1849. * Unregister the driver
  1850. */
  1851. static void __exit sbridge_exit(void)
  1852. {
  1853. edac_dbg(2, "\n");
  1854. pci_unregister_driver(&sbridge_driver);
  1855. mce_unregister_decode_chain(&sbridge_mce_dec);
  1856. }
  1857. module_init(sbridge_init);
  1858. module_exit(sbridge_exit);
  1859. module_param(edac_op_state, int, 0444);
  1860. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
  1861. MODULE_LICENSE("GPL");
  1862. MODULE_AUTHOR("Mauro Carvalho Chehab");
  1863. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  1864. MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge and Ivy Bridge memory controllers - "
  1865. SBRIDGE_REVISION);