sb_edac.c 61 KB

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