sb_edac.c 66 KB

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