sb_edac.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178
  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 <mchehab@redhat.com>
  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 DEFINE_PCI_DEVICE_TABLE(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. 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_INFO,
  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. mci = get_mci_for_node_id(mce->socketid);
  1578. if (!mci)
  1579. return NOTIFY_BAD;
  1580. pvt = mci->pvt_info;
  1581. /*
  1582. * Just let mcelog handle it if the error is
  1583. * outside the memory controller. A memory error
  1584. * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
  1585. * bit 12 has an special meaning.
  1586. */
  1587. if ((mce->status & 0xefff) >> 7 != 1)
  1588. return NOTIFY_DONE;
  1589. printk("sbridge: HANDLING MCE MEMORY ERROR\n");
  1590. printk("CPU %d: Machine Check Exception: %Lx Bank %d: %016Lx\n",
  1591. mce->extcpu, mce->mcgstatus, mce->bank, mce->status);
  1592. printk("TSC %llx ", mce->tsc);
  1593. printk("ADDR %llx ", mce->addr);
  1594. printk("MISC %llx ", mce->misc);
  1595. printk("PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x\n",
  1596. mce->cpuvendor, mce->cpuid, mce->time,
  1597. mce->socketid, mce->apicid);
  1598. /* Only handle if it is the right mc controller */
  1599. if (cpu_data(mce->cpu).phys_proc_id != pvt->sbridge_dev->mc)
  1600. return NOTIFY_DONE;
  1601. smp_rmb();
  1602. if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
  1603. smp_wmb();
  1604. pvt->mce_overrun++;
  1605. return NOTIFY_DONE;
  1606. }
  1607. /* Copy memory error at the ringbuffer */
  1608. memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
  1609. smp_wmb();
  1610. pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
  1611. /* Handle fatal errors immediately */
  1612. if (mce->mcgstatus & 1)
  1613. sbridge_check_error(mci);
  1614. /* Advice mcelog that the error were handled */
  1615. return NOTIFY_STOP;
  1616. }
  1617. static struct notifier_block sbridge_mce_dec = {
  1618. .notifier_call = sbridge_mce_check_error,
  1619. };
  1620. /****************************************************************************
  1621. EDAC register/unregister logic
  1622. ****************************************************************************/
  1623. static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
  1624. {
  1625. struct mem_ctl_info *mci = sbridge_dev->mci;
  1626. struct sbridge_pvt *pvt;
  1627. if (unlikely(!mci || !mci->pvt_info)) {
  1628. edac_dbg(0, "MC: dev = %p\n", &sbridge_dev->pdev[0]->dev);
  1629. sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
  1630. return;
  1631. }
  1632. pvt = mci->pvt_info;
  1633. edac_dbg(0, "MC: mci = %p, dev = %p\n",
  1634. mci, &sbridge_dev->pdev[0]->dev);
  1635. /* Remove MC sysfs nodes */
  1636. edac_mc_del_mc(mci->pdev);
  1637. edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
  1638. kfree(mci->ctl_name);
  1639. edac_mc_free(mci);
  1640. sbridge_dev->mci = NULL;
  1641. }
  1642. static int sbridge_register_mci(struct sbridge_dev *sbridge_dev, enum type type)
  1643. {
  1644. struct mem_ctl_info *mci;
  1645. struct edac_mc_layer layers[2];
  1646. struct sbridge_pvt *pvt;
  1647. struct pci_dev *pdev = sbridge_dev->pdev[0];
  1648. int rc;
  1649. /* Check the number of active and not disabled channels */
  1650. rc = check_if_ecc_is_active(sbridge_dev->bus);
  1651. if (unlikely(rc < 0))
  1652. return rc;
  1653. /* allocate a new MC control structure */
  1654. layers[0].type = EDAC_MC_LAYER_CHANNEL;
  1655. layers[0].size = NUM_CHANNELS;
  1656. layers[0].is_virt_csrow = false;
  1657. layers[1].type = EDAC_MC_LAYER_SLOT;
  1658. layers[1].size = MAX_DIMMS;
  1659. layers[1].is_virt_csrow = true;
  1660. mci = edac_mc_alloc(sbridge_dev->mc, ARRAY_SIZE(layers), layers,
  1661. sizeof(*pvt));
  1662. if (unlikely(!mci))
  1663. return -ENOMEM;
  1664. edac_dbg(0, "MC: mci = %p, dev = %p\n",
  1665. mci, &pdev->dev);
  1666. pvt = mci->pvt_info;
  1667. memset(pvt, 0, sizeof(*pvt));
  1668. /* Associate sbridge_dev and mci for future usage */
  1669. pvt->sbridge_dev = sbridge_dev;
  1670. sbridge_dev->mci = mci;
  1671. mci->mtype_cap = MEM_FLAG_DDR3;
  1672. mci->edac_ctl_cap = EDAC_FLAG_NONE;
  1673. mci->edac_cap = EDAC_FLAG_NONE;
  1674. mci->mod_name = "sbridge_edac.c";
  1675. mci->mod_ver = SBRIDGE_REVISION;
  1676. mci->dev_name = pci_name(pdev);
  1677. mci->ctl_page_to_phys = NULL;
  1678. /* Set the function pointer to an actual operation function */
  1679. mci->edac_check = sbridge_check_error;
  1680. pvt->info.type = type;
  1681. if (type == IVY_BRIDGE) {
  1682. pvt->info.rankcfgr = IB_RANK_CFG_A;
  1683. pvt->info.get_tolm = ibridge_get_tolm;
  1684. pvt->info.get_tohm = ibridge_get_tohm;
  1685. pvt->info.dram_rule = ibridge_dram_rule;
  1686. pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
  1687. pvt->info.interleave_list = ibridge_interleave_list;
  1688. pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
  1689. pvt->info.interleave_pkg = ibridge_interleave_pkg;
  1690. mci->ctl_name = kasprintf(GFP_KERNEL, "Ivy Bridge Socket#%d", mci->mc_idx);
  1691. /* Store pci devices at mci for faster access */
  1692. rc = ibridge_mci_bind_devs(mci, sbridge_dev);
  1693. if (unlikely(rc < 0))
  1694. goto fail0;
  1695. } else {
  1696. pvt->info.rankcfgr = SB_RANK_CFG_A;
  1697. pvt->info.get_tolm = sbridge_get_tolm;
  1698. pvt->info.get_tohm = sbridge_get_tohm;
  1699. pvt->info.dram_rule = sbridge_dram_rule;
  1700. pvt->info.max_sad = ARRAY_SIZE(sbridge_dram_rule);
  1701. pvt->info.interleave_list = sbridge_interleave_list;
  1702. pvt->info.max_interleave = ARRAY_SIZE(sbridge_interleave_list);
  1703. pvt->info.interleave_pkg = sbridge_interleave_pkg;
  1704. mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge Socket#%d", mci->mc_idx);
  1705. /* Store pci devices at mci for faster access */
  1706. rc = sbridge_mci_bind_devs(mci, sbridge_dev);
  1707. if (unlikely(rc < 0))
  1708. goto fail0;
  1709. }
  1710. /* Get dimm basic config and the memory layout */
  1711. get_dimm_config(mci);
  1712. get_memory_layout(mci);
  1713. /* record ptr to the generic device */
  1714. mci->pdev = &pdev->dev;
  1715. /* add this new MC control structure to EDAC's list of MCs */
  1716. if (unlikely(edac_mc_add_mc(mci))) {
  1717. edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
  1718. rc = -EINVAL;
  1719. goto fail0;
  1720. }
  1721. return 0;
  1722. fail0:
  1723. kfree(mci->ctl_name);
  1724. edac_mc_free(mci);
  1725. sbridge_dev->mci = NULL;
  1726. return rc;
  1727. }
  1728. /*
  1729. * sbridge_probe Probe for ONE instance of device to see if it is
  1730. * present.
  1731. * return:
  1732. * 0 for FOUND a device
  1733. * < 0 for error code
  1734. */
  1735. static int sbridge_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1736. {
  1737. int rc;
  1738. u8 mc, num_mc = 0;
  1739. struct sbridge_dev *sbridge_dev;
  1740. enum type type;
  1741. /* get the pci devices we want to reserve for our use */
  1742. mutex_lock(&sbridge_edac_lock);
  1743. /*
  1744. * All memory controllers are allocated at the first pass.
  1745. */
  1746. if (unlikely(probed >= 1)) {
  1747. mutex_unlock(&sbridge_edac_lock);
  1748. return -ENODEV;
  1749. }
  1750. probed++;
  1751. if (pdev->device == PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA) {
  1752. rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_ibridge_table);
  1753. type = IVY_BRIDGE;
  1754. } else {
  1755. rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_sbridge_table);
  1756. type = SANDY_BRIDGE;
  1757. }
  1758. if (unlikely(rc < 0))
  1759. goto fail0;
  1760. mc = 0;
  1761. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  1762. edac_dbg(0, "Registering MC#%d (%d of %d)\n",
  1763. mc, mc + 1, num_mc);
  1764. sbridge_dev->mc = mc++;
  1765. rc = sbridge_register_mci(sbridge_dev, type);
  1766. if (unlikely(rc < 0))
  1767. goto fail1;
  1768. }
  1769. sbridge_printk(KERN_INFO, "Driver loaded.\n");
  1770. mutex_unlock(&sbridge_edac_lock);
  1771. return 0;
  1772. fail1:
  1773. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
  1774. sbridge_unregister_mci(sbridge_dev);
  1775. sbridge_put_all_devices();
  1776. fail0:
  1777. mutex_unlock(&sbridge_edac_lock);
  1778. return rc;
  1779. }
  1780. /*
  1781. * sbridge_remove destructor for one instance of device
  1782. *
  1783. */
  1784. static void sbridge_remove(struct pci_dev *pdev)
  1785. {
  1786. struct sbridge_dev *sbridge_dev;
  1787. edac_dbg(0, "\n");
  1788. /*
  1789. * we have a trouble here: pdev value for removal will be wrong, since
  1790. * it will point to the X58 register used to detect that the machine
  1791. * is a Nehalem or upper design. However, due to the way several PCI
  1792. * devices are grouped together to provide MC functionality, we need
  1793. * to use a different method for releasing the devices
  1794. */
  1795. mutex_lock(&sbridge_edac_lock);
  1796. if (unlikely(!probed)) {
  1797. mutex_unlock(&sbridge_edac_lock);
  1798. return;
  1799. }
  1800. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
  1801. sbridge_unregister_mci(sbridge_dev);
  1802. /* Release PCI resources */
  1803. sbridge_put_all_devices();
  1804. probed--;
  1805. mutex_unlock(&sbridge_edac_lock);
  1806. }
  1807. MODULE_DEVICE_TABLE(pci, sbridge_pci_tbl);
  1808. /*
  1809. * sbridge_driver pci_driver structure for this module
  1810. *
  1811. */
  1812. static struct pci_driver sbridge_driver = {
  1813. .name = "sbridge_edac",
  1814. .probe = sbridge_probe,
  1815. .remove = sbridge_remove,
  1816. .id_table = sbridge_pci_tbl,
  1817. };
  1818. /*
  1819. * sbridge_init Module entry function
  1820. * Try to initialize this module for its devices
  1821. */
  1822. static int __init sbridge_init(void)
  1823. {
  1824. int pci_rc;
  1825. edac_dbg(2, "\n");
  1826. /* Ensure that the OPSTATE is set correctly for POLL or NMI */
  1827. opstate_init();
  1828. pci_rc = pci_register_driver(&sbridge_driver);
  1829. if (pci_rc >= 0) {
  1830. mce_register_decode_chain(&sbridge_mce_dec);
  1831. return 0;
  1832. }
  1833. sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
  1834. pci_rc);
  1835. return pci_rc;
  1836. }
  1837. /*
  1838. * sbridge_exit() Module exit function
  1839. * Unregister the driver
  1840. */
  1841. static void __exit sbridge_exit(void)
  1842. {
  1843. edac_dbg(2, "\n");
  1844. pci_unregister_driver(&sbridge_driver);
  1845. mce_unregister_decode_chain(&sbridge_mce_dec);
  1846. }
  1847. module_init(sbridge_init);
  1848. module_exit(sbridge_exit);
  1849. module_param(edac_op_state, int, 0444);
  1850. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
  1851. MODULE_LICENSE("GPL");
  1852. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  1853. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  1854. MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge and Ivy Bridge memory controllers - "
  1855. SBRIDGE_REVISION);