sb_edac.c 69 KB

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