sb_edac.c 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612
  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. static const u32 knl_dram_rule[] = {
  59. 0x60, 0x68, 0x70, 0x78, 0x80, /* 0-4 */
  60. 0x88, 0x90, 0x98, 0xa0, 0xa8, /* 5-9 */
  61. 0xb0, 0xb8, 0xc0, 0xc8, 0xd0, /* 10-14 */
  62. 0xd8, 0xe0, 0xe8, 0xf0, 0xf8, /* 15-19 */
  63. 0x100, 0x108, 0x110, 0x118, /* 20-23 */
  64. };
  65. #define DRAM_RULE_ENABLE(reg) GET_BITFIELD(reg, 0, 0)
  66. #define A7MODE(reg) GET_BITFIELD(reg, 26, 26)
  67. static char *show_dram_attr(u32 attr)
  68. {
  69. switch (attr) {
  70. case 0:
  71. return "DRAM";
  72. case 1:
  73. return "MMCFG";
  74. case 2:
  75. return "NXM";
  76. default:
  77. return "unknown";
  78. }
  79. }
  80. static const u32 sbridge_interleave_list[] = {
  81. 0x84, 0x8c, 0x94, 0x9c, 0xa4,
  82. 0xac, 0xb4, 0xbc, 0xc4, 0xcc,
  83. };
  84. static const u32 ibridge_interleave_list[] = {
  85. 0x64, 0x6c, 0x74, 0x7c, 0x84,
  86. 0x8c, 0x94, 0x9c, 0xa4, 0xac,
  87. 0xb4, 0xbc, 0xc4, 0xcc, 0xd4,
  88. 0xdc, 0xe4, 0xec, 0xf4, 0xfc,
  89. };
  90. static const u32 knl_interleave_list[] = {
  91. 0x64, 0x6c, 0x74, 0x7c, 0x84, /* 0-4 */
  92. 0x8c, 0x94, 0x9c, 0xa4, 0xac, /* 5-9 */
  93. 0xb4, 0xbc, 0xc4, 0xcc, 0xd4, /* 10-14 */
  94. 0xdc, 0xe4, 0xec, 0xf4, 0xfc, /* 15-19 */
  95. 0x104, 0x10c, 0x114, 0x11c, /* 20-23 */
  96. };
  97. struct interleave_pkg {
  98. unsigned char start;
  99. unsigned char end;
  100. };
  101. static const struct interleave_pkg sbridge_interleave_pkg[] = {
  102. { 0, 2 },
  103. { 3, 5 },
  104. { 8, 10 },
  105. { 11, 13 },
  106. { 16, 18 },
  107. { 19, 21 },
  108. { 24, 26 },
  109. { 27, 29 },
  110. };
  111. static const struct interleave_pkg ibridge_interleave_pkg[] = {
  112. { 0, 3 },
  113. { 4, 7 },
  114. { 8, 11 },
  115. { 12, 15 },
  116. { 16, 19 },
  117. { 20, 23 },
  118. { 24, 27 },
  119. { 28, 31 },
  120. };
  121. static inline int sad_pkg(const struct interleave_pkg *table, u32 reg,
  122. int interleave)
  123. {
  124. return GET_BITFIELD(reg, table[interleave].start,
  125. table[interleave].end);
  126. }
  127. /* Devices 12 Function 7 */
  128. #define TOLM 0x80
  129. #define TOHM 0x84
  130. #define HASWELL_TOLM 0xd0
  131. #define HASWELL_TOHM_0 0xd4
  132. #define HASWELL_TOHM_1 0xd8
  133. #define KNL_TOLM 0xd0
  134. #define KNL_TOHM_0 0xd4
  135. #define KNL_TOHM_1 0xd8
  136. #define GET_TOLM(reg) ((GET_BITFIELD(reg, 0, 3) << 28) | 0x3ffffff)
  137. #define GET_TOHM(reg) ((GET_BITFIELD(reg, 0, 20) << 25) | 0x3ffffff)
  138. /* Device 13 Function 6 */
  139. #define SAD_TARGET 0xf0
  140. #define SOURCE_ID(reg) GET_BITFIELD(reg, 9, 11)
  141. #define SOURCE_ID_KNL(reg) GET_BITFIELD(reg, 12, 14)
  142. #define SAD_CONTROL 0xf4
  143. /* Device 14 function 0 */
  144. static const u32 tad_dram_rule[] = {
  145. 0x40, 0x44, 0x48, 0x4c,
  146. 0x50, 0x54, 0x58, 0x5c,
  147. 0x60, 0x64, 0x68, 0x6c,
  148. };
  149. #define MAX_TAD ARRAY_SIZE(tad_dram_rule)
  150. #define TAD_LIMIT(reg) ((GET_BITFIELD(reg, 12, 31) << 26) | 0x3ffffff)
  151. #define TAD_SOCK(reg) GET_BITFIELD(reg, 10, 11)
  152. #define TAD_CH(reg) GET_BITFIELD(reg, 8, 9)
  153. #define TAD_TGT3(reg) GET_BITFIELD(reg, 6, 7)
  154. #define TAD_TGT2(reg) GET_BITFIELD(reg, 4, 5)
  155. #define TAD_TGT1(reg) GET_BITFIELD(reg, 2, 3)
  156. #define TAD_TGT0(reg) GET_BITFIELD(reg, 0, 1)
  157. /* Device 15, function 0 */
  158. #define MCMTR 0x7c
  159. #define KNL_MCMTR 0x624
  160. #define IS_ECC_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 2, 2)
  161. #define IS_LOCKSTEP_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 1, 1)
  162. #define IS_CLOSE_PG(mcmtr) GET_BITFIELD(mcmtr, 0, 0)
  163. /* Device 15, function 1 */
  164. #define RASENABLES 0xac
  165. #define IS_MIRROR_ENABLED(reg) GET_BITFIELD(reg, 0, 0)
  166. /* Device 15, functions 2-5 */
  167. static const int mtr_regs[] = {
  168. 0x80, 0x84, 0x88,
  169. };
  170. static const int knl_mtr_reg = 0xb60;
  171. #define RANK_DISABLE(mtr) GET_BITFIELD(mtr, 16, 19)
  172. #define IS_DIMM_PRESENT(mtr) GET_BITFIELD(mtr, 14, 14)
  173. #define RANK_CNT_BITS(mtr) GET_BITFIELD(mtr, 12, 13)
  174. #define RANK_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 2, 4)
  175. #define COL_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 0, 1)
  176. static const u32 tad_ch_nilv_offset[] = {
  177. 0x90, 0x94, 0x98, 0x9c,
  178. 0xa0, 0xa4, 0xa8, 0xac,
  179. 0xb0, 0xb4, 0xb8, 0xbc,
  180. };
  181. #define CHN_IDX_OFFSET(reg) GET_BITFIELD(reg, 28, 29)
  182. #define TAD_OFFSET(reg) (GET_BITFIELD(reg, 6, 25) << 26)
  183. static const u32 rir_way_limit[] = {
  184. 0x108, 0x10c, 0x110, 0x114, 0x118,
  185. };
  186. #define MAX_RIR_RANGES ARRAY_SIZE(rir_way_limit)
  187. #define IS_RIR_VALID(reg) GET_BITFIELD(reg, 31, 31)
  188. #define RIR_WAY(reg) GET_BITFIELD(reg, 28, 29)
  189. #define MAX_RIR_WAY 8
  190. static const u32 rir_offset[MAX_RIR_RANGES][MAX_RIR_WAY] = {
  191. { 0x120, 0x124, 0x128, 0x12c, 0x130, 0x134, 0x138, 0x13c },
  192. { 0x140, 0x144, 0x148, 0x14c, 0x150, 0x154, 0x158, 0x15c },
  193. { 0x160, 0x164, 0x168, 0x16c, 0x170, 0x174, 0x178, 0x17c },
  194. { 0x180, 0x184, 0x188, 0x18c, 0x190, 0x194, 0x198, 0x19c },
  195. { 0x1a0, 0x1a4, 0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc },
  196. };
  197. #define RIR_RNK_TGT(reg) GET_BITFIELD(reg, 16, 19)
  198. #define RIR_OFFSET(reg) GET_BITFIELD(reg, 2, 14)
  199. /* Device 16, functions 2-7 */
  200. /*
  201. * FIXME: Implement the error count reads directly
  202. */
  203. static const u32 correrrcnt[] = {
  204. 0x104, 0x108, 0x10c, 0x110,
  205. };
  206. #define RANK_ODD_OV(reg) GET_BITFIELD(reg, 31, 31)
  207. #define RANK_ODD_ERR_CNT(reg) GET_BITFIELD(reg, 16, 30)
  208. #define RANK_EVEN_OV(reg) GET_BITFIELD(reg, 15, 15)
  209. #define RANK_EVEN_ERR_CNT(reg) GET_BITFIELD(reg, 0, 14)
  210. static const u32 correrrthrsld[] = {
  211. 0x11c, 0x120, 0x124, 0x128,
  212. };
  213. #define RANK_ODD_ERR_THRSLD(reg) GET_BITFIELD(reg, 16, 30)
  214. #define RANK_EVEN_ERR_THRSLD(reg) GET_BITFIELD(reg, 0, 14)
  215. /* Device 17, function 0 */
  216. #define SB_RANK_CFG_A 0x0328
  217. #define IB_RANK_CFG_A 0x0320
  218. /*
  219. * sbridge structs
  220. */
  221. #define NUM_CHANNELS 8 /* 2MC per socket, four chan per MC */
  222. #define MAX_DIMMS 3 /* Max DIMMS per channel */
  223. #define KNL_MAX_CHAS 38 /* KNL max num. of Cache Home Agents */
  224. #define KNL_MAX_CHANNELS 6 /* KNL max num. of PCI channels */
  225. #define KNL_MAX_EDCS 8 /* Embedded DRAM controllers */
  226. #define CHANNEL_UNSPECIFIED 0xf /* Intel IA32 SDM 15-14 */
  227. enum type {
  228. SANDY_BRIDGE,
  229. IVY_BRIDGE,
  230. HASWELL,
  231. BROADWELL,
  232. KNIGHTS_LANDING,
  233. };
  234. struct sbridge_pvt;
  235. struct sbridge_info {
  236. enum type type;
  237. u32 mcmtr;
  238. u32 rankcfgr;
  239. u64 (*get_tolm)(struct sbridge_pvt *pvt);
  240. u64 (*get_tohm)(struct sbridge_pvt *pvt);
  241. u64 (*rir_limit)(u32 reg);
  242. u64 (*sad_limit)(u32 reg);
  243. u32 (*interleave_mode)(u32 reg);
  244. char* (*show_interleave_mode)(u32 reg);
  245. u32 (*dram_attr)(u32 reg);
  246. const u32 *dram_rule;
  247. const u32 *interleave_list;
  248. const struct interleave_pkg *interleave_pkg;
  249. u8 max_sad;
  250. u8 max_interleave;
  251. u8 (*get_node_id)(struct sbridge_pvt *pvt);
  252. enum mem_type (*get_memory_type)(struct sbridge_pvt *pvt);
  253. enum dev_type (*get_width)(struct sbridge_pvt *pvt, u32 mtr);
  254. struct pci_dev *pci_vtd;
  255. };
  256. struct sbridge_channel {
  257. u32 ranks;
  258. u32 dimms;
  259. };
  260. struct pci_id_descr {
  261. int dev_id;
  262. int optional;
  263. };
  264. struct pci_id_table {
  265. const struct pci_id_descr *descr;
  266. int n_devs;
  267. };
  268. struct sbridge_dev {
  269. struct list_head list;
  270. u8 bus, mc;
  271. u8 node_id, source_id;
  272. struct pci_dev **pdev;
  273. int n_devs;
  274. struct mem_ctl_info *mci;
  275. };
  276. struct knl_pvt {
  277. struct pci_dev *pci_cha[KNL_MAX_CHAS];
  278. struct pci_dev *pci_channel[KNL_MAX_CHANNELS];
  279. struct pci_dev *pci_mc0;
  280. struct pci_dev *pci_mc1;
  281. struct pci_dev *pci_mc0_misc;
  282. struct pci_dev *pci_mc1_misc;
  283. struct pci_dev *pci_mc_info; /* tolm, tohm */
  284. };
  285. struct sbridge_pvt {
  286. struct pci_dev *pci_ta, *pci_ddrio, *pci_ras;
  287. struct pci_dev *pci_sad0, *pci_sad1;
  288. struct pci_dev *pci_ha0, *pci_ha1;
  289. struct pci_dev *pci_br0, *pci_br1;
  290. struct pci_dev *pci_ha1_ta;
  291. struct pci_dev *pci_tad[NUM_CHANNELS];
  292. struct sbridge_dev *sbridge_dev;
  293. struct sbridge_info info;
  294. struct sbridge_channel channel[NUM_CHANNELS];
  295. /* Memory type detection */
  296. bool is_mirrored, is_lockstep, is_close_pg;
  297. /* Fifo double buffers */
  298. struct mce mce_entry[MCE_LOG_LEN];
  299. struct mce mce_outentry[MCE_LOG_LEN];
  300. /* Fifo in/out counters */
  301. unsigned mce_in, mce_out;
  302. /* Count indicator to show errors not got */
  303. unsigned mce_overrun;
  304. /* Memory description */
  305. u64 tolm, tohm;
  306. struct knl_pvt knl;
  307. };
  308. #define PCI_DESCR(device_id, opt) \
  309. .dev_id = (device_id), \
  310. .optional = opt
  311. static const struct pci_id_descr pci_dev_descr_sbridge[] = {
  312. /* Processor Home Agent */
  313. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0, 0) },
  314. /* Memory controller */
  315. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA, 0) },
  316. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS, 0) },
  317. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0, 0) },
  318. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1, 0) },
  319. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2, 0) },
  320. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3, 0) },
  321. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO, 1) },
  322. /* System Address Decoder */
  323. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0, 0) },
  324. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1, 0) },
  325. /* Broadcast Registers */
  326. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_BR, 0) },
  327. };
  328. #define PCI_ID_TABLE_ENTRY(A) { .descr=A, .n_devs = ARRAY_SIZE(A) }
  329. static const struct pci_id_table pci_dev_descr_sbridge_table[] = {
  330. PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge),
  331. {0,} /* 0 terminated list. */
  332. };
  333. /* This changes depending if 1HA or 2HA:
  334. * 1HA:
  335. * 0x0eb8 (17.0) is DDRIO0
  336. * 2HA:
  337. * 0x0ebc (17.4) is DDRIO0
  338. */
  339. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0 0x0eb8
  340. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0 0x0ebc
  341. /* pci ids */
  342. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0 0x0ea0
  343. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA 0x0ea8
  344. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS 0x0e71
  345. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0 0x0eaa
  346. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1 0x0eab
  347. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2 0x0eac
  348. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3 0x0ead
  349. #define PCI_DEVICE_ID_INTEL_IBRIDGE_SAD 0x0ec8
  350. #define PCI_DEVICE_ID_INTEL_IBRIDGE_BR0 0x0ec9
  351. #define PCI_DEVICE_ID_INTEL_IBRIDGE_BR1 0x0eca
  352. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1 0x0e60
  353. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA 0x0e68
  354. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS 0x0e79
  355. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 0x0e6a
  356. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1 0x0e6b
  357. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2 0x0e6c
  358. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3 0x0e6d
  359. static const struct pci_id_descr pci_dev_descr_ibridge[] = {
  360. /* Processor Home Agent */
  361. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0, 0) },
  362. /* Memory controller */
  363. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA, 0) },
  364. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS, 0) },
  365. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0, 0) },
  366. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1, 0) },
  367. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2, 0) },
  368. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3, 0) },
  369. /* System Address Decoder */
  370. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_SAD, 0) },
  371. /* Broadcast Registers */
  372. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR0, 1) },
  373. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR1, 0) },
  374. /* Optional, mode 2HA */
  375. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, 1) },
  376. #if 0
  377. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA, 1) },
  378. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS, 1) },
  379. #endif
  380. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0, 1) },
  381. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1, 1) },
  382. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2, 1) },
  383. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3, 1) },
  384. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0, 1) },
  385. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0, 1) },
  386. };
  387. static const struct pci_id_table pci_dev_descr_ibridge_table[] = {
  388. PCI_ID_TABLE_ENTRY(pci_dev_descr_ibridge),
  389. {0,} /* 0 terminated list. */
  390. };
  391. /* Haswell support */
  392. /* EN processor:
  393. * - 1 IMC
  394. * - 3 DDR3 channels, 2 DPC per channel
  395. * EP processor:
  396. * - 1 or 2 IMC
  397. * - 4 DDR4 channels, 3 DPC per channel
  398. * EP 4S processor:
  399. * - 2 IMC
  400. * - 4 DDR4 channels, 3 DPC per channel
  401. * EX processor:
  402. * - 2 IMC
  403. * - each IMC interfaces with a SMI 2 channel
  404. * - each SMI channel interfaces with a scalable memory buffer
  405. * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
  406. */
  407. #define HASWELL_DDRCRCLKCONTROLS 0xa10 /* Ditto on Broadwell */
  408. #define HASWELL_HASYSDEFEATURE2 0x84
  409. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC 0x2f28
  410. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0 0x2fa0
  411. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1 0x2f60
  412. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA 0x2fa8
  413. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL 0x2f71
  414. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA 0x2f68
  415. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL 0x2f79
  416. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0 0x2ffc
  417. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1 0x2ffd
  418. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0 0x2faa
  419. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1 0x2fab
  420. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2 0x2fac
  421. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3 0x2fad
  422. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 0x2f6a
  423. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1 0x2f6b
  424. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2 0x2f6c
  425. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3 0x2f6d
  426. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0 0x2fbd
  427. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1 0x2fbf
  428. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2 0x2fb9
  429. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3 0x2fbb
  430. static const struct pci_id_descr pci_dev_descr_haswell[] = {
  431. /* first item must be the HA */
  432. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0, 0) },
  433. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0, 0) },
  434. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1, 0) },
  435. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1, 1) },
  436. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA, 0) },
  437. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL, 0) },
  438. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0, 0) },
  439. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1, 0) },
  440. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2, 1) },
  441. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3, 1) },
  442. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0, 1) },
  443. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1, 1) },
  444. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2, 1) },
  445. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3, 1) },
  446. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA, 1) },
  447. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL, 1) },
  448. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0, 1) },
  449. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1, 1) },
  450. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2, 1) },
  451. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3, 1) },
  452. };
  453. static const struct pci_id_table pci_dev_descr_haswell_table[] = {
  454. PCI_ID_TABLE_ENTRY(pci_dev_descr_haswell),
  455. {0,} /* 0 terminated list. */
  456. };
  457. /* Knight's Landing Support */
  458. /*
  459. * KNL's memory channels are swizzled between memory controllers.
  460. * MC0 is mapped to CH3,5,6 and MC1 is mapped to CH0,1,2
  461. */
  462. #define knl_channel_remap(channel) ((channel + 3) % 6)
  463. /* Memory controller, TAD tables, error injection - 2-8-0, 2-9-0 (2 of these) */
  464. #define PCI_DEVICE_ID_INTEL_KNL_IMC_MC 0x7840
  465. /* DRAM channel stuff; bank addrs, dimmmtr, etc.. 2-8-2 - 2-9-4 (6 of these) */
  466. #define PCI_DEVICE_ID_INTEL_KNL_IMC_CHANNEL 0x7843
  467. /* kdrwdbu TAD limits/offsets, MCMTR - 2-10-1, 2-11-1 (2 of these) */
  468. #define PCI_DEVICE_ID_INTEL_KNL_IMC_TA 0x7844
  469. /* CHA broadcast registers, dram rules - 1-29-0 (1 of these) */
  470. #define PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0 0x782a
  471. /* SAD target - 1-29-1 (1 of these) */
  472. #define PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1 0x782b
  473. /* Caching / Home Agent */
  474. #define PCI_DEVICE_ID_INTEL_KNL_IMC_CHA 0x782c
  475. /* Device with TOLM and TOHM, 0-5-0 (1 of these) */
  476. #define PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM 0x7810
  477. /*
  478. * KNL differs from SB, IB, and Haswell in that it has multiple
  479. * instances of the same device with the same device ID, so we handle that
  480. * by creating as many copies in the table as we expect to find.
  481. * (Like device ID must be grouped together.)
  482. */
  483. static const struct pci_id_descr pci_dev_descr_knl[] = {
  484. [0] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0, 0) },
  485. [1] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1, 0) },
  486. [2 ... 3] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_MC, 0)},
  487. [4 ... 41] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_CHA, 0) },
  488. [42 ... 47] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_CHANNEL, 0) },
  489. [48] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_TA, 0) },
  490. [49] = { PCI_DESCR(PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM, 0) },
  491. };
  492. static const struct pci_id_table pci_dev_descr_knl_table[] = {
  493. PCI_ID_TABLE_ENTRY(pci_dev_descr_knl),
  494. {0,}
  495. };
  496. /*
  497. * Broadwell support
  498. *
  499. * DE processor:
  500. * - 1 IMC
  501. * - 2 DDR3 channels, 2 DPC per channel
  502. * EP processor:
  503. * - 1 or 2 IMC
  504. * - 4 DDR4 channels, 3 DPC per channel
  505. * EP 4S processor:
  506. * - 2 IMC
  507. * - 4 DDR4 channels, 3 DPC per channel
  508. * EX processor:
  509. * - 2 IMC
  510. * - each IMC interfaces with a SMI 2 channel
  511. * - each SMI channel interfaces with a scalable memory buffer
  512. * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
  513. */
  514. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC 0x6f28
  515. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0 0x6fa0
  516. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1 0x6f60
  517. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA 0x6fa8
  518. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL 0x6f71
  519. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA 0x6f68
  520. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_THERMAL 0x6f79
  521. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0 0x6ffc
  522. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1 0x6ffd
  523. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0 0x6faa
  524. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1 0x6fab
  525. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2 0x6fac
  526. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3 0x6fad
  527. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0 0x6f6a
  528. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1 0x6f6b
  529. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2 0x6f6c
  530. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3 0x6f6d
  531. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0 0x6faf
  532. static const struct pci_id_descr pci_dev_descr_broadwell[] = {
  533. /* first item must be the HA */
  534. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0, 0) },
  535. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0, 0) },
  536. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1, 0) },
  537. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1, 1) },
  538. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA, 0) },
  539. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL, 0) },
  540. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0, 0) },
  541. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1, 0) },
  542. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2, 1) },
  543. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3, 1) },
  544. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0, 1) },
  545. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA, 1) },
  546. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_THERMAL, 1) },
  547. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0, 1) },
  548. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1, 1) },
  549. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2, 1) },
  550. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3, 1) },
  551. };
  552. static const struct pci_id_table pci_dev_descr_broadwell_table[] = {
  553. PCI_ID_TABLE_ENTRY(pci_dev_descr_broadwell),
  554. {0,} /* 0 terminated list. */
  555. };
  556. /*
  557. * pci_device_id table for which devices we are looking for
  558. */
  559. static const struct pci_device_id sbridge_pci_tbl[] = {
  560. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0)},
  561. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA)},
  562. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0)},
  563. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0)},
  564. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0)},
  565. {0,} /* 0 terminated list. */
  566. };
  567. /****************************************************************************
  568. Ancillary status routines
  569. ****************************************************************************/
  570. static inline int numrank(enum type type, u32 mtr)
  571. {
  572. int ranks = (1 << RANK_CNT_BITS(mtr));
  573. int max = 4;
  574. if (type == HASWELL || type == BROADWELL || type == KNIGHTS_LANDING)
  575. max = 8;
  576. if (ranks > max) {
  577. edac_dbg(0, "Invalid number of ranks: %d (max = %i) raw value = %x (%04x)\n",
  578. ranks, max, (unsigned int)RANK_CNT_BITS(mtr), mtr);
  579. return -EINVAL;
  580. }
  581. return ranks;
  582. }
  583. static inline int numrow(u32 mtr)
  584. {
  585. int rows = (RANK_WIDTH_BITS(mtr) + 12);
  586. if (rows < 13 || rows > 18) {
  587. edac_dbg(0, "Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)\n",
  588. rows, (unsigned int)RANK_WIDTH_BITS(mtr), mtr);
  589. return -EINVAL;
  590. }
  591. return 1 << rows;
  592. }
  593. static inline int numcol(u32 mtr)
  594. {
  595. int cols = (COL_WIDTH_BITS(mtr) + 10);
  596. if (cols > 12) {
  597. edac_dbg(0, "Invalid number of cols: %d (max = 4) raw value = %x (%04x)\n",
  598. cols, (unsigned int)COL_WIDTH_BITS(mtr), mtr);
  599. return -EINVAL;
  600. }
  601. return 1 << cols;
  602. }
  603. static struct sbridge_dev *get_sbridge_dev(u8 bus, int multi_bus)
  604. {
  605. struct sbridge_dev *sbridge_dev;
  606. /*
  607. * If we have devices scattered across several busses that pertain
  608. * to the same memory controller, we'll lump them all together.
  609. */
  610. if (multi_bus) {
  611. return list_first_entry_or_null(&sbridge_edac_list,
  612. struct sbridge_dev, list);
  613. }
  614. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  615. if (sbridge_dev->bus == bus)
  616. return sbridge_dev;
  617. }
  618. return NULL;
  619. }
  620. static struct sbridge_dev *alloc_sbridge_dev(u8 bus,
  621. const struct pci_id_table *table)
  622. {
  623. struct sbridge_dev *sbridge_dev;
  624. sbridge_dev = kzalloc(sizeof(*sbridge_dev), GFP_KERNEL);
  625. if (!sbridge_dev)
  626. return NULL;
  627. sbridge_dev->pdev = kzalloc(sizeof(*sbridge_dev->pdev) * table->n_devs,
  628. GFP_KERNEL);
  629. if (!sbridge_dev->pdev) {
  630. kfree(sbridge_dev);
  631. return NULL;
  632. }
  633. sbridge_dev->bus = bus;
  634. sbridge_dev->n_devs = table->n_devs;
  635. list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
  636. return sbridge_dev;
  637. }
  638. static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
  639. {
  640. list_del(&sbridge_dev->list);
  641. kfree(sbridge_dev->pdev);
  642. kfree(sbridge_dev);
  643. }
  644. static u64 sbridge_get_tolm(struct sbridge_pvt *pvt)
  645. {
  646. u32 reg;
  647. /* Address range is 32:28 */
  648. pci_read_config_dword(pvt->pci_sad1, TOLM, &reg);
  649. return GET_TOLM(reg);
  650. }
  651. static u64 sbridge_get_tohm(struct sbridge_pvt *pvt)
  652. {
  653. u32 reg;
  654. pci_read_config_dword(pvt->pci_sad1, TOHM, &reg);
  655. return GET_TOHM(reg);
  656. }
  657. static u64 ibridge_get_tolm(struct sbridge_pvt *pvt)
  658. {
  659. u32 reg;
  660. pci_read_config_dword(pvt->pci_br1, TOLM, &reg);
  661. return GET_TOLM(reg);
  662. }
  663. static u64 ibridge_get_tohm(struct sbridge_pvt *pvt)
  664. {
  665. u32 reg;
  666. pci_read_config_dword(pvt->pci_br1, TOHM, &reg);
  667. return GET_TOHM(reg);
  668. }
  669. static u64 rir_limit(u32 reg)
  670. {
  671. return ((u64)GET_BITFIELD(reg, 1, 10) << 29) | 0x1fffffff;
  672. }
  673. static u64 sad_limit(u32 reg)
  674. {
  675. return (GET_BITFIELD(reg, 6, 25) << 26) | 0x3ffffff;
  676. }
  677. static u32 interleave_mode(u32 reg)
  678. {
  679. return GET_BITFIELD(reg, 1, 1);
  680. }
  681. char *show_interleave_mode(u32 reg)
  682. {
  683. return interleave_mode(reg) ? "8:6" : "[8:6]XOR[18:16]";
  684. }
  685. static u32 dram_attr(u32 reg)
  686. {
  687. return GET_BITFIELD(reg, 2, 3);
  688. }
  689. static u64 knl_sad_limit(u32 reg)
  690. {
  691. return (GET_BITFIELD(reg, 7, 26) << 26) | 0x3ffffff;
  692. }
  693. static u32 knl_interleave_mode(u32 reg)
  694. {
  695. return GET_BITFIELD(reg, 1, 2);
  696. }
  697. static char *knl_show_interleave_mode(u32 reg)
  698. {
  699. char *s;
  700. switch (knl_interleave_mode(reg)) {
  701. case 0:
  702. s = "use address bits [8:6]";
  703. break;
  704. case 1:
  705. s = "use address bits [10:8]";
  706. break;
  707. case 2:
  708. s = "use address bits [14:12]";
  709. break;
  710. case 3:
  711. s = "use address bits [32:30]";
  712. break;
  713. default:
  714. WARN_ON(1);
  715. break;
  716. }
  717. return s;
  718. }
  719. static u32 dram_attr_knl(u32 reg)
  720. {
  721. return GET_BITFIELD(reg, 3, 4);
  722. }
  723. static enum mem_type get_memory_type(struct sbridge_pvt *pvt)
  724. {
  725. u32 reg;
  726. enum mem_type mtype;
  727. if (pvt->pci_ddrio) {
  728. pci_read_config_dword(pvt->pci_ddrio, pvt->info.rankcfgr,
  729. &reg);
  730. if (GET_BITFIELD(reg, 11, 11))
  731. /* FIXME: Can also be LRDIMM */
  732. mtype = MEM_RDDR3;
  733. else
  734. mtype = MEM_DDR3;
  735. } else
  736. mtype = MEM_UNKNOWN;
  737. return mtype;
  738. }
  739. static enum mem_type haswell_get_memory_type(struct sbridge_pvt *pvt)
  740. {
  741. u32 reg;
  742. bool registered = false;
  743. enum mem_type mtype = MEM_UNKNOWN;
  744. if (!pvt->pci_ddrio)
  745. goto out;
  746. pci_read_config_dword(pvt->pci_ddrio,
  747. HASWELL_DDRCRCLKCONTROLS, &reg);
  748. /* Is_Rdimm */
  749. if (GET_BITFIELD(reg, 16, 16))
  750. registered = true;
  751. pci_read_config_dword(pvt->pci_ta, MCMTR, &reg);
  752. if (GET_BITFIELD(reg, 14, 14)) {
  753. if (registered)
  754. mtype = MEM_RDDR4;
  755. else
  756. mtype = MEM_DDR4;
  757. } else {
  758. if (registered)
  759. mtype = MEM_RDDR3;
  760. else
  761. mtype = MEM_DDR3;
  762. }
  763. out:
  764. return mtype;
  765. }
  766. static enum dev_type knl_get_width(struct sbridge_pvt *pvt, u32 mtr)
  767. {
  768. /* for KNL value is fixed */
  769. return DEV_X16;
  770. }
  771. static enum dev_type sbridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
  772. {
  773. /* there's no way to figure out */
  774. return DEV_UNKNOWN;
  775. }
  776. static enum dev_type __ibridge_get_width(u32 mtr)
  777. {
  778. enum dev_type type;
  779. switch (mtr) {
  780. case 3:
  781. type = DEV_UNKNOWN;
  782. break;
  783. case 2:
  784. type = DEV_X16;
  785. break;
  786. case 1:
  787. type = DEV_X8;
  788. break;
  789. case 0:
  790. type = DEV_X4;
  791. break;
  792. }
  793. return type;
  794. }
  795. static enum dev_type ibridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
  796. {
  797. /*
  798. * ddr3_width on the documentation but also valid for DDR4 on
  799. * Haswell
  800. */
  801. return __ibridge_get_width(GET_BITFIELD(mtr, 7, 8));
  802. }
  803. static enum dev_type broadwell_get_width(struct sbridge_pvt *pvt, u32 mtr)
  804. {
  805. /* ddr3_width on the documentation but also valid for DDR4 */
  806. return __ibridge_get_width(GET_BITFIELD(mtr, 8, 9));
  807. }
  808. static enum mem_type knl_get_memory_type(struct sbridge_pvt *pvt)
  809. {
  810. /* DDR4 RDIMMS and LRDIMMS are supported */
  811. return MEM_RDDR4;
  812. }
  813. static u8 get_node_id(struct sbridge_pvt *pvt)
  814. {
  815. u32 reg;
  816. pci_read_config_dword(pvt->pci_br0, SAD_CONTROL, &reg);
  817. return GET_BITFIELD(reg, 0, 2);
  818. }
  819. static u8 haswell_get_node_id(struct sbridge_pvt *pvt)
  820. {
  821. u32 reg;
  822. pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, &reg);
  823. return GET_BITFIELD(reg, 0, 3);
  824. }
  825. static u8 knl_get_node_id(struct sbridge_pvt *pvt)
  826. {
  827. u32 reg;
  828. pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, &reg);
  829. return GET_BITFIELD(reg, 0, 2);
  830. }
  831. static u64 haswell_get_tolm(struct sbridge_pvt *pvt)
  832. {
  833. u32 reg;
  834. pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOLM, &reg);
  835. return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
  836. }
  837. static u64 haswell_get_tohm(struct sbridge_pvt *pvt)
  838. {
  839. u64 rc;
  840. u32 reg;
  841. pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_0, &reg);
  842. rc = GET_BITFIELD(reg, 26, 31);
  843. pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_1, &reg);
  844. rc = ((reg << 6) | rc) << 26;
  845. return rc | 0x1ffffff;
  846. }
  847. static u64 knl_get_tolm(struct sbridge_pvt *pvt)
  848. {
  849. u32 reg;
  850. pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOLM, &reg);
  851. return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
  852. }
  853. static u64 knl_get_tohm(struct sbridge_pvt *pvt)
  854. {
  855. u64 rc;
  856. u32 reg_lo, reg_hi;
  857. pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOHM_0, &reg_lo);
  858. pci_read_config_dword(pvt->knl.pci_mc_info, KNL_TOHM_1, &reg_hi);
  859. rc = ((u64)reg_hi << 32) | reg_lo;
  860. return rc | 0x3ffffff;
  861. }
  862. static u64 haswell_rir_limit(u32 reg)
  863. {
  864. return (((u64)GET_BITFIELD(reg, 1, 11) + 1) << 29) - 1;
  865. }
  866. static inline u8 sad_pkg_socket(u8 pkg)
  867. {
  868. /* on Ivy Bridge, nodeID is SASS, where A is HA and S is node id */
  869. return ((pkg >> 3) << 2) | (pkg & 0x3);
  870. }
  871. static inline u8 sad_pkg_ha(u8 pkg)
  872. {
  873. return (pkg >> 2) & 0x1;
  874. }
  875. /****************************************************************************
  876. Memory check routines
  877. ****************************************************************************/
  878. static struct pci_dev *get_pdev_same_bus(u8 bus, u32 id)
  879. {
  880. struct pci_dev *pdev = NULL;
  881. do {
  882. pdev = pci_get_device(PCI_VENDOR_ID_INTEL, id, pdev);
  883. if (pdev && pdev->bus->number == bus)
  884. break;
  885. } while (pdev);
  886. return pdev;
  887. }
  888. /**
  889. * check_if_ecc_is_active() - Checks if ECC is active
  890. * @bus: Device bus
  891. * @type: Memory controller type
  892. * returns: 0 in case ECC is active, -ENODEV if it can't be determined or
  893. * disabled
  894. */
  895. static int check_if_ecc_is_active(const u8 bus, enum type type)
  896. {
  897. struct pci_dev *pdev = NULL;
  898. u32 mcmtr, id;
  899. switch (type) {
  900. case IVY_BRIDGE:
  901. id = PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA;
  902. break;
  903. case HASWELL:
  904. id = PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA;
  905. break;
  906. case SANDY_BRIDGE:
  907. id = PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA;
  908. break;
  909. case BROADWELL:
  910. id = PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA;
  911. break;
  912. case KNIGHTS_LANDING:
  913. /*
  914. * KNL doesn't group things by bus the same way
  915. * SB/IB/Haswell does.
  916. */
  917. id = PCI_DEVICE_ID_INTEL_KNL_IMC_TA;
  918. break;
  919. default:
  920. return -ENODEV;
  921. }
  922. if (type != KNIGHTS_LANDING)
  923. pdev = get_pdev_same_bus(bus, id);
  924. else
  925. pdev = pci_get_device(PCI_VENDOR_ID_INTEL, id, 0);
  926. if (!pdev) {
  927. sbridge_printk(KERN_ERR, "Couldn't find PCI device "
  928. "%04x:%04x! on bus %02d\n",
  929. PCI_VENDOR_ID_INTEL, id, bus);
  930. return -ENODEV;
  931. }
  932. pci_read_config_dword(pdev,
  933. type == KNIGHTS_LANDING ? KNL_MCMTR : MCMTR, &mcmtr);
  934. if (!IS_ECC_ENABLED(mcmtr)) {
  935. sbridge_printk(KERN_ERR, "ECC is disabled. Aborting\n");
  936. return -ENODEV;
  937. }
  938. return 0;
  939. }
  940. /* Low bits of TAD limit, and some metadata. */
  941. static const u32 knl_tad_dram_limit_lo[] = {
  942. 0x400, 0x500, 0x600, 0x700,
  943. 0x800, 0x900, 0xa00, 0xb00,
  944. };
  945. /* Low bits of TAD offset. */
  946. static const u32 knl_tad_dram_offset_lo[] = {
  947. 0x404, 0x504, 0x604, 0x704,
  948. 0x804, 0x904, 0xa04, 0xb04,
  949. };
  950. /* High 16 bits of TAD limit and offset. */
  951. static const u32 knl_tad_dram_hi[] = {
  952. 0x408, 0x508, 0x608, 0x708,
  953. 0x808, 0x908, 0xa08, 0xb08,
  954. };
  955. /* Number of ways a tad entry is interleaved. */
  956. static const u32 knl_tad_ways[] = {
  957. 8, 6, 4, 3, 2, 1,
  958. };
  959. /*
  960. * Retrieve the n'th Target Address Decode table entry
  961. * from the memory controller's TAD table.
  962. *
  963. * @pvt: driver private data
  964. * @entry: which entry you want to retrieve
  965. * @mc: which memory controller (0 or 1)
  966. * @offset: output tad range offset
  967. * @limit: output address of first byte above tad range
  968. * @ways: output number of interleave ways
  969. *
  970. * The offset value has curious semantics. It's a sort of running total
  971. * of the sizes of all the memory regions that aren't mapped in this
  972. * tad table.
  973. */
  974. static int knl_get_tad(const struct sbridge_pvt *pvt,
  975. const int entry,
  976. const int mc,
  977. u64 *offset,
  978. u64 *limit,
  979. int *ways)
  980. {
  981. u32 reg_limit_lo, reg_offset_lo, reg_hi;
  982. struct pci_dev *pci_mc;
  983. int way_id;
  984. switch (mc) {
  985. case 0:
  986. pci_mc = pvt->knl.pci_mc0;
  987. break;
  988. case 1:
  989. pci_mc = pvt->knl.pci_mc1;
  990. break;
  991. default:
  992. WARN_ON(1);
  993. return -EINVAL;
  994. }
  995. pci_read_config_dword(pci_mc,
  996. knl_tad_dram_limit_lo[entry], &reg_limit_lo);
  997. pci_read_config_dword(pci_mc,
  998. knl_tad_dram_offset_lo[entry], &reg_offset_lo);
  999. pci_read_config_dword(pci_mc,
  1000. knl_tad_dram_hi[entry], &reg_hi);
  1001. /* Is this TAD entry enabled? */
  1002. if (!GET_BITFIELD(reg_limit_lo, 0, 0))
  1003. return -ENODEV;
  1004. way_id = GET_BITFIELD(reg_limit_lo, 3, 5);
  1005. if (way_id < ARRAY_SIZE(knl_tad_ways)) {
  1006. *ways = knl_tad_ways[way_id];
  1007. } else {
  1008. *ways = 0;
  1009. sbridge_printk(KERN_ERR,
  1010. "Unexpected value %d in mc_tad_limit_lo wayness field\n",
  1011. way_id);
  1012. return -ENODEV;
  1013. }
  1014. /*
  1015. * The least significant 6 bits of base and limit are truncated.
  1016. * For limit, we fill the missing bits with 1s.
  1017. */
  1018. *offset = ((u64) GET_BITFIELD(reg_offset_lo, 6, 31) << 6) |
  1019. ((u64) GET_BITFIELD(reg_hi, 0, 15) << 32);
  1020. *limit = ((u64) GET_BITFIELD(reg_limit_lo, 6, 31) << 6) | 63 |
  1021. ((u64) GET_BITFIELD(reg_hi, 16, 31) << 32);
  1022. return 0;
  1023. }
  1024. /* Determine which memory controller is responsible for a given channel. */
  1025. static int knl_channel_mc(int channel)
  1026. {
  1027. WARN_ON(channel < 0 || channel >= 6);
  1028. return channel < 3 ? 1 : 0;
  1029. }
  1030. /*
  1031. * Get the Nth entry from EDC_ROUTE_TABLE register.
  1032. * (This is the per-tile mapping of logical interleave targets to
  1033. * physical EDC modules.)
  1034. *
  1035. * entry 0: 0:2
  1036. * 1: 3:5
  1037. * 2: 6:8
  1038. * 3: 9:11
  1039. * 4: 12:14
  1040. * 5: 15:17
  1041. * 6: 18:20
  1042. * 7: 21:23
  1043. * reserved: 24:31
  1044. */
  1045. static u32 knl_get_edc_route(int entry, u32 reg)
  1046. {
  1047. WARN_ON(entry >= KNL_MAX_EDCS);
  1048. return GET_BITFIELD(reg, entry*3, (entry*3)+2);
  1049. }
  1050. /*
  1051. * Get the Nth entry from MC_ROUTE_TABLE register.
  1052. * (This is the per-tile mapping of logical interleave targets to
  1053. * physical DRAM channels modules.)
  1054. *
  1055. * entry 0: mc 0:2 channel 18:19
  1056. * 1: mc 3:5 channel 20:21
  1057. * 2: mc 6:8 channel 22:23
  1058. * 3: mc 9:11 channel 24:25
  1059. * 4: mc 12:14 channel 26:27
  1060. * 5: mc 15:17 channel 28:29
  1061. * reserved: 30:31
  1062. *
  1063. * Though we have 3 bits to identify the MC, we should only see
  1064. * the values 0 or 1.
  1065. */
  1066. static u32 knl_get_mc_route(int entry, u32 reg)
  1067. {
  1068. int mc, chan;
  1069. WARN_ON(entry >= KNL_MAX_CHANNELS);
  1070. mc = GET_BITFIELD(reg, entry*3, (entry*3)+2);
  1071. chan = GET_BITFIELD(reg, (entry*2) + 18, (entry*2) + 18 + 1);
  1072. return knl_channel_remap(mc*3 + chan);
  1073. }
  1074. /*
  1075. * Render the EDC_ROUTE register in human-readable form.
  1076. * Output string s should be at least KNL_MAX_EDCS*2 bytes.
  1077. */
  1078. static void knl_show_edc_route(u32 reg, char *s)
  1079. {
  1080. int i;
  1081. for (i = 0; i < KNL_MAX_EDCS; i++) {
  1082. s[i*2] = knl_get_edc_route(i, reg) + '0';
  1083. s[i*2+1] = '-';
  1084. }
  1085. s[KNL_MAX_EDCS*2 - 1] = '\0';
  1086. }
  1087. /*
  1088. * Render the MC_ROUTE register in human-readable form.
  1089. * Output string s should be at least KNL_MAX_CHANNELS*2 bytes.
  1090. */
  1091. static void knl_show_mc_route(u32 reg, char *s)
  1092. {
  1093. int i;
  1094. for (i = 0; i < KNL_MAX_CHANNELS; i++) {
  1095. s[i*2] = knl_get_mc_route(i, reg) + '0';
  1096. s[i*2+1] = '-';
  1097. }
  1098. s[KNL_MAX_CHANNELS*2 - 1] = '\0';
  1099. }
  1100. #define KNL_EDC_ROUTE 0xb8
  1101. #define KNL_MC_ROUTE 0xb4
  1102. /* Is this dram rule backed by regular DRAM in flat mode? */
  1103. #define KNL_EDRAM(reg) GET_BITFIELD(reg, 29, 29)
  1104. /* Is this dram rule cached? */
  1105. #define KNL_CACHEABLE(reg) GET_BITFIELD(reg, 28, 28)
  1106. /* Is this rule backed by edc ? */
  1107. #define KNL_EDRAM_ONLY(reg) GET_BITFIELD(reg, 29, 29)
  1108. /* Is this rule backed by DRAM, cacheable in EDRAM? */
  1109. #define KNL_CACHEABLE(reg) GET_BITFIELD(reg, 28, 28)
  1110. /* Is this rule mod3? */
  1111. #define KNL_MOD3(reg) GET_BITFIELD(reg, 27, 27)
  1112. /*
  1113. * Figure out how big our RAM modules are.
  1114. *
  1115. * The DIMMMTR register in KNL doesn't tell us the size of the DIMMs, so we
  1116. * have to figure this out from the SAD rules, interleave lists, route tables,
  1117. * and TAD rules.
  1118. *
  1119. * SAD rules can have holes in them (e.g. the 3G-4G hole), so we have to
  1120. * inspect the TAD rules to figure out how large the SAD regions really are.
  1121. *
  1122. * When we know the real size of a SAD region and how many ways it's
  1123. * interleaved, we know the individual contribution of each channel to
  1124. * TAD is size/ways.
  1125. *
  1126. * Finally, we have to check whether each channel participates in each SAD
  1127. * region.
  1128. *
  1129. * Fortunately, KNL only supports one DIMM per channel, so once we know how
  1130. * much memory the channel uses, we know the DIMM is at least that large.
  1131. * (The BIOS might possibly choose not to map all available memory, in which
  1132. * case we will underreport the size of the DIMM.)
  1133. *
  1134. * In theory, we could try to determine the EDC sizes as well, but that would
  1135. * only work in flat mode, not in cache mode.
  1136. *
  1137. * @mc_sizes: Output sizes of channels (must have space for KNL_MAX_CHANNELS
  1138. * elements)
  1139. */
  1140. static int knl_get_dimm_capacity(struct sbridge_pvt *pvt, u64 *mc_sizes)
  1141. {
  1142. u64 sad_base, sad_size, sad_limit = 0;
  1143. u64 tad_base, tad_size, tad_limit, tad_deadspace, tad_livespace;
  1144. int sad_rule = 0;
  1145. int tad_rule = 0;
  1146. int intrlv_ways, tad_ways;
  1147. u32 first_pkg, pkg;
  1148. int i;
  1149. u64 sad_actual_size[2]; /* sad size accounting for holes, per mc */
  1150. u32 dram_rule, interleave_reg;
  1151. u32 mc_route_reg[KNL_MAX_CHAS];
  1152. u32 edc_route_reg[KNL_MAX_CHAS];
  1153. int edram_only;
  1154. char edc_route_string[KNL_MAX_EDCS*2];
  1155. char mc_route_string[KNL_MAX_CHANNELS*2];
  1156. int cur_reg_start;
  1157. int mc;
  1158. int channel;
  1159. int way;
  1160. int participants[KNL_MAX_CHANNELS];
  1161. int participant_count = 0;
  1162. for (i = 0; i < KNL_MAX_CHANNELS; i++)
  1163. mc_sizes[i] = 0;
  1164. /* Read the EDC route table in each CHA. */
  1165. cur_reg_start = 0;
  1166. for (i = 0; i < KNL_MAX_CHAS; i++) {
  1167. pci_read_config_dword(pvt->knl.pci_cha[i],
  1168. KNL_EDC_ROUTE, &edc_route_reg[i]);
  1169. if (i > 0 && edc_route_reg[i] != edc_route_reg[i-1]) {
  1170. knl_show_edc_route(edc_route_reg[i-1],
  1171. edc_route_string);
  1172. if (cur_reg_start == i-1)
  1173. edac_dbg(0, "edc route table for CHA %d: %s\n",
  1174. cur_reg_start, edc_route_string);
  1175. else
  1176. edac_dbg(0, "edc route table for CHA %d-%d: %s\n",
  1177. cur_reg_start, i-1, edc_route_string);
  1178. cur_reg_start = i;
  1179. }
  1180. }
  1181. knl_show_edc_route(edc_route_reg[i-1], edc_route_string);
  1182. if (cur_reg_start == i-1)
  1183. edac_dbg(0, "edc route table for CHA %d: %s\n",
  1184. cur_reg_start, edc_route_string);
  1185. else
  1186. edac_dbg(0, "edc route table for CHA %d-%d: %s\n",
  1187. cur_reg_start, i-1, edc_route_string);
  1188. /* Read the MC route table in each CHA. */
  1189. cur_reg_start = 0;
  1190. for (i = 0; i < KNL_MAX_CHAS; i++) {
  1191. pci_read_config_dword(pvt->knl.pci_cha[i],
  1192. KNL_MC_ROUTE, &mc_route_reg[i]);
  1193. if (i > 0 && mc_route_reg[i] != mc_route_reg[i-1]) {
  1194. knl_show_mc_route(mc_route_reg[i-1], mc_route_string);
  1195. if (cur_reg_start == i-1)
  1196. edac_dbg(0, "mc route table for CHA %d: %s\n",
  1197. cur_reg_start, mc_route_string);
  1198. else
  1199. edac_dbg(0, "mc route table for CHA %d-%d: %s\n",
  1200. cur_reg_start, i-1, mc_route_string);
  1201. cur_reg_start = i;
  1202. }
  1203. }
  1204. knl_show_mc_route(mc_route_reg[i-1], mc_route_string);
  1205. if (cur_reg_start == i-1)
  1206. edac_dbg(0, "mc route table for CHA %d: %s\n",
  1207. cur_reg_start, mc_route_string);
  1208. else
  1209. edac_dbg(0, "mc route table for CHA %d-%d: %s\n",
  1210. cur_reg_start, i-1, mc_route_string);
  1211. /* Process DRAM rules */
  1212. for (sad_rule = 0; sad_rule < pvt->info.max_sad; sad_rule++) {
  1213. /* previous limit becomes the new base */
  1214. sad_base = sad_limit;
  1215. pci_read_config_dword(pvt->pci_sad0,
  1216. pvt->info.dram_rule[sad_rule], &dram_rule);
  1217. if (!DRAM_RULE_ENABLE(dram_rule))
  1218. break;
  1219. edram_only = KNL_EDRAM_ONLY(dram_rule);
  1220. sad_limit = pvt->info.sad_limit(dram_rule)+1;
  1221. sad_size = sad_limit - sad_base;
  1222. pci_read_config_dword(pvt->pci_sad0,
  1223. pvt->info.interleave_list[sad_rule], &interleave_reg);
  1224. /*
  1225. * Find out how many ways this dram rule is interleaved.
  1226. * We stop when we see the first channel again.
  1227. */
  1228. first_pkg = sad_pkg(pvt->info.interleave_pkg,
  1229. interleave_reg, 0);
  1230. for (intrlv_ways = 1; intrlv_ways < 8; intrlv_ways++) {
  1231. pkg = sad_pkg(pvt->info.interleave_pkg,
  1232. interleave_reg, intrlv_ways);
  1233. if ((pkg & 0x8) == 0) {
  1234. /*
  1235. * 0 bit means memory is non-local,
  1236. * which KNL doesn't support
  1237. */
  1238. edac_dbg(0, "Unexpected interleave target %d\n",
  1239. pkg);
  1240. return -1;
  1241. }
  1242. if (pkg == first_pkg)
  1243. break;
  1244. }
  1245. if (KNL_MOD3(dram_rule))
  1246. intrlv_ways *= 3;
  1247. edac_dbg(3, "dram rule %d (base 0x%llx, limit 0x%llx), %d way interleave%s\n",
  1248. sad_rule,
  1249. sad_base,
  1250. sad_limit,
  1251. intrlv_ways,
  1252. edram_only ? ", EDRAM" : "");
  1253. /*
  1254. * Find out how big the SAD region really is by iterating
  1255. * over TAD tables (SAD regions may contain holes).
  1256. * Each memory controller might have a different TAD table, so
  1257. * we have to look at both.
  1258. *
  1259. * Livespace is the memory that's mapped in this TAD table,
  1260. * deadspace is the holes (this could be the MMIO hole, or it
  1261. * could be memory that's mapped by the other TAD table but
  1262. * not this one).
  1263. */
  1264. for (mc = 0; mc < 2; mc++) {
  1265. sad_actual_size[mc] = 0;
  1266. tad_livespace = 0;
  1267. for (tad_rule = 0;
  1268. tad_rule < ARRAY_SIZE(
  1269. knl_tad_dram_limit_lo);
  1270. tad_rule++) {
  1271. if (knl_get_tad(pvt,
  1272. tad_rule,
  1273. mc,
  1274. &tad_deadspace,
  1275. &tad_limit,
  1276. &tad_ways))
  1277. break;
  1278. tad_size = (tad_limit+1) -
  1279. (tad_livespace + tad_deadspace);
  1280. tad_livespace += tad_size;
  1281. tad_base = (tad_limit+1) - tad_size;
  1282. if (tad_base < sad_base) {
  1283. if (tad_limit > sad_base)
  1284. edac_dbg(0, "TAD region overlaps lower SAD boundary -- TAD tables may be configured incorrectly.\n");
  1285. } else if (tad_base < sad_limit) {
  1286. if (tad_limit+1 > sad_limit) {
  1287. edac_dbg(0, "TAD region overlaps upper SAD boundary -- TAD tables may be configured incorrectly.\n");
  1288. } else {
  1289. /* TAD region is completely inside SAD region */
  1290. edac_dbg(3, "TAD region %d 0x%llx - 0x%llx (%lld bytes) table%d\n",
  1291. tad_rule, tad_base,
  1292. tad_limit, tad_size,
  1293. mc);
  1294. sad_actual_size[mc] += tad_size;
  1295. }
  1296. }
  1297. tad_base = tad_limit+1;
  1298. }
  1299. }
  1300. for (mc = 0; mc < 2; mc++) {
  1301. edac_dbg(3, " total TAD DRAM footprint in table%d : 0x%llx (%lld bytes)\n",
  1302. mc, sad_actual_size[mc], sad_actual_size[mc]);
  1303. }
  1304. /* Ignore EDRAM rule */
  1305. if (edram_only)
  1306. continue;
  1307. /* Figure out which channels participate in interleave. */
  1308. for (channel = 0; channel < KNL_MAX_CHANNELS; channel++)
  1309. participants[channel] = 0;
  1310. /* For each channel, does at least one CHA have
  1311. * this channel mapped to the given target?
  1312. */
  1313. for (channel = 0; channel < KNL_MAX_CHANNELS; channel++) {
  1314. for (way = 0; way < intrlv_ways; way++) {
  1315. int target;
  1316. int cha;
  1317. if (KNL_MOD3(dram_rule))
  1318. target = way;
  1319. else
  1320. target = 0x7 & sad_pkg(
  1321. pvt->info.interleave_pkg, interleave_reg, way);
  1322. for (cha = 0; cha < KNL_MAX_CHAS; cha++) {
  1323. if (knl_get_mc_route(target,
  1324. mc_route_reg[cha]) == channel
  1325. && !participants[channel]) {
  1326. participant_count++;
  1327. participants[channel] = 1;
  1328. break;
  1329. }
  1330. }
  1331. }
  1332. }
  1333. if (participant_count != intrlv_ways)
  1334. edac_dbg(0, "participant_count (%d) != interleave_ways (%d): DIMM size may be incorrect\n",
  1335. participant_count, intrlv_ways);
  1336. for (channel = 0; channel < KNL_MAX_CHANNELS; channel++) {
  1337. mc = knl_channel_mc(channel);
  1338. if (participants[channel]) {
  1339. edac_dbg(4, "mc channel %d contributes %lld bytes via sad entry %d\n",
  1340. channel,
  1341. sad_actual_size[mc]/intrlv_ways,
  1342. sad_rule);
  1343. mc_sizes[channel] +=
  1344. sad_actual_size[mc]/intrlv_ways;
  1345. }
  1346. }
  1347. }
  1348. return 0;
  1349. }
  1350. static int get_dimm_config(struct mem_ctl_info *mci)
  1351. {
  1352. struct sbridge_pvt *pvt = mci->pvt_info;
  1353. struct dimm_info *dimm;
  1354. unsigned i, j, banks, ranks, rows, cols, npages;
  1355. u64 size;
  1356. u32 reg;
  1357. enum edac_type mode;
  1358. enum mem_type mtype;
  1359. int channels = pvt->info.type == KNIGHTS_LANDING ?
  1360. KNL_MAX_CHANNELS : NUM_CHANNELS;
  1361. u64 knl_mc_sizes[KNL_MAX_CHANNELS];
  1362. if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL ||
  1363. pvt->info.type == KNIGHTS_LANDING)
  1364. pci_read_config_dword(pvt->pci_sad1, SAD_TARGET, &reg);
  1365. else
  1366. pci_read_config_dword(pvt->pci_br0, SAD_TARGET, &reg);
  1367. if (pvt->info.type == KNIGHTS_LANDING)
  1368. pvt->sbridge_dev->source_id = SOURCE_ID_KNL(reg);
  1369. else
  1370. pvt->sbridge_dev->source_id = SOURCE_ID(reg);
  1371. pvt->sbridge_dev->node_id = pvt->info.get_node_id(pvt);
  1372. edac_dbg(0, "mc#%d: Node ID: %d, source ID: %d\n",
  1373. pvt->sbridge_dev->mc,
  1374. pvt->sbridge_dev->node_id,
  1375. pvt->sbridge_dev->source_id);
  1376. /* KNL doesn't support mirroring or lockstep,
  1377. * and is always closed page
  1378. */
  1379. if (pvt->info.type == KNIGHTS_LANDING) {
  1380. mode = EDAC_S4ECD4ED;
  1381. pvt->is_mirrored = false;
  1382. if (knl_get_dimm_capacity(pvt, knl_mc_sizes) != 0)
  1383. return -1;
  1384. } else {
  1385. pci_read_config_dword(pvt->pci_ras, RASENABLES, &reg);
  1386. if (IS_MIRROR_ENABLED(reg)) {
  1387. edac_dbg(0, "Memory mirror is enabled\n");
  1388. pvt->is_mirrored = true;
  1389. } else {
  1390. edac_dbg(0, "Memory mirror is disabled\n");
  1391. pvt->is_mirrored = false;
  1392. }
  1393. pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
  1394. if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
  1395. edac_dbg(0, "Lockstep is enabled\n");
  1396. mode = EDAC_S8ECD8ED;
  1397. pvt->is_lockstep = true;
  1398. } else {
  1399. edac_dbg(0, "Lockstep is disabled\n");
  1400. mode = EDAC_S4ECD4ED;
  1401. pvt->is_lockstep = false;
  1402. }
  1403. if (IS_CLOSE_PG(pvt->info.mcmtr)) {
  1404. edac_dbg(0, "address map is on closed page mode\n");
  1405. pvt->is_close_pg = true;
  1406. } else {
  1407. edac_dbg(0, "address map is on open page mode\n");
  1408. pvt->is_close_pg = false;
  1409. }
  1410. }
  1411. mtype = pvt->info.get_memory_type(pvt);
  1412. if (mtype == MEM_RDDR3 || mtype == MEM_RDDR4)
  1413. edac_dbg(0, "Memory is registered\n");
  1414. else if (mtype == MEM_UNKNOWN)
  1415. edac_dbg(0, "Cannot determine memory type\n");
  1416. else
  1417. edac_dbg(0, "Memory is unregistered\n");
  1418. if (mtype == MEM_DDR4 || mtype == MEM_RDDR4)
  1419. banks = 16;
  1420. else
  1421. banks = 8;
  1422. for (i = 0; i < channels; i++) {
  1423. u32 mtr;
  1424. int max_dimms_per_channel;
  1425. if (pvt->info.type == KNIGHTS_LANDING) {
  1426. max_dimms_per_channel = 1;
  1427. if (!pvt->knl.pci_channel[i])
  1428. continue;
  1429. } else {
  1430. max_dimms_per_channel = ARRAY_SIZE(mtr_regs);
  1431. if (!pvt->pci_tad[i])
  1432. continue;
  1433. }
  1434. for (j = 0; j < max_dimms_per_channel; j++) {
  1435. dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
  1436. i, j, 0);
  1437. if (pvt->info.type == KNIGHTS_LANDING) {
  1438. pci_read_config_dword(pvt->knl.pci_channel[i],
  1439. knl_mtr_reg, &mtr);
  1440. } else {
  1441. pci_read_config_dword(pvt->pci_tad[i],
  1442. mtr_regs[j], &mtr);
  1443. }
  1444. edac_dbg(4, "Channel #%d MTR%d = %x\n", i, j, mtr);
  1445. if (IS_DIMM_PRESENT(mtr)) {
  1446. pvt->channel[i].dimms++;
  1447. ranks = numrank(pvt->info.type, mtr);
  1448. if (pvt->info.type == KNIGHTS_LANDING) {
  1449. /* For DDR4, this is fixed. */
  1450. cols = 1 << 10;
  1451. rows = knl_mc_sizes[i] /
  1452. ((u64) cols * ranks * banks * 8);
  1453. } else {
  1454. rows = numrow(mtr);
  1455. cols = numcol(mtr);
  1456. }
  1457. size = ((u64)rows * cols * banks * ranks) >> (20 - 3);
  1458. npages = MiB_TO_PAGES(size);
  1459. edac_dbg(0, "mc#%d: ha %d channel %d, dimm %d, %lld Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
  1460. pvt->sbridge_dev->mc, i/4, i%4, j,
  1461. size, npages,
  1462. banks, ranks, rows, cols);
  1463. dimm->nr_pages = npages;
  1464. dimm->grain = 32;
  1465. dimm->dtype = pvt->info.get_width(pvt, mtr);
  1466. dimm->mtype = mtype;
  1467. dimm->edac_mode = mode;
  1468. snprintf(dimm->label, sizeof(dimm->label),
  1469. "CPU_SrcID#%u_Ha#%u_Chan#%u_DIMM#%u",
  1470. pvt->sbridge_dev->source_id, i/4, i%4, j);
  1471. }
  1472. }
  1473. }
  1474. return 0;
  1475. }
  1476. static void get_memory_layout(const struct mem_ctl_info *mci)
  1477. {
  1478. struct sbridge_pvt *pvt = mci->pvt_info;
  1479. int i, j, k, n_sads, n_tads, sad_interl;
  1480. u32 reg;
  1481. u64 limit, prv = 0;
  1482. u64 tmp_mb;
  1483. u32 gb, mb;
  1484. u32 rir_way;
  1485. /*
  1486. * Step 1) Get TOLM/TOHM ranges
  1487. */
  1488. pvt->tolm = pvt->info.get_tolm(pvt);
  1489. tmp_mb = (1 + pvt->tolm) >> 20;
  1490. gb = div_u64_rem(tmp_mb, 1024, &mb);
  1491. edac_dbg(0, "TOLM: %u.%03u GB (0x%016Lx)\n",
  1492. gb, (mb*1000)/1024, (u64)pvt->tolm);
  1493. /* Address range is already 45:25 */
  1494. pvt->tohm = pvt->info.get_tohm(pvt);
  1495. tmp_mb = (1 + pvt->tohm) >> 20;
  1496. gb = div_u64_rem(tmp_mb, 1024, &mb);
  1497. edac_dbg(0, "TOHM: %u.%03u GB (0x%016Lx)\n",
  1498. gb, (mb*1000)/1024, (u64)pvt->tohm);
  1499. /*
  1500. * Step 2) Get SAD range and SAD Interleave list
  1501. * TAD registers contain the interleave wayness. However, it
  1502. * seems simpler to just discover it indirectly, with the
  1503. * algorithm bellow.
  1504. */
  1505. prv = 0;
  1506. for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
  1507. /* SAD_LIMIT Address range is 45:26 */
  1508. pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
  1509. &reg);
  1510. limit = pvt->info.sad_limit(reg);
  1511. if (!DRAM_RULE_ENABLE(reg))
  1512. continue;
  1513. if (limit <= prv)
  1514. break;
  1515. tmp_mb = (limit + 1) >> 20;
  1516. gb = div_u64_rem(tmp_mb, 1024, &mb);
  1517. edac_dbg(0, "SAD#%d %s up to %u.%03u GB (0x%016Lx) Interleave: %s reg=0x%08x\n",
  1518. n_sads,
  1519. show_dram_attr(pvt->info.dram_attr(reg)),
  1520. gb, (mb*1000)/1024,
  1521. ((u64)tmp_mb) << 20L,
  1522. pvt->info.show_interleave_mode(reg),
  1523. reg);
  1524. prv = limit;
  1525. pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
  1526. &reg);
  1527. sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
  1528. for (j = 0; j < 8; j++) {
  1529. u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, j);
  1530. if (j > 0 && sad_interl == pkg)
  1531. break;
  1532. edac_dbg(0, "SAD#%d, interleave #%d: %d\n",
  1533. n_sads, j, pkg);
  1534. }
  1535. }
  1536. if (pvt->info.type == KNIGHTS_LANDING)
  1537. return;
  1538. /*
  1539. * Step 3) Get TAD range
  1540. */
  1541. prv = 0;
  1542. for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
  1543. pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
  1544. &reg);
  1545. limit = TAD_LIMIT(reg);
  1546. if (limit <= prv)
  1547. break;
  1548. tmp_mb = (limit + 1) >> 20;
  1549. gb = div_u64_rem(tmp_mb, 1024, &mb);
  1550. 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",
  1551. n_tads, gb, (mb*1000)/1024,
  1552. ((u64)tmp_mb) << 20L,
  1553. (u32)(1 << TAD_SOCK(reg)),
  1554. (u32)TAD_CH(reg) + 1,
  1555. (u32)TAD_TGT0(reg),
  1556. (u32)TAD_TGT1(reg),
  1557. (u32)TAD_TGT2(reg),
  1558. (u32)TAD_TGT3(reg),
  1559. reg);
  1560. prv = limit;
  1561. }
  1562. /*
  1563. * Step 4) Get TAD offsets, per each channel
  1564. */
  1565. for (i = 0; i < NUM_CHANNELS; i++) {
  1566. if (!pvt->channel[i].dimms)
  1567. continue;
  1568. for (j = 0; j < n_tads; j++) {
  1569. pci_read_config_dword(pvt->pci_tad[i],
  1570. tad_ch_nilv_offset[j],
  1571. &reg);
  1572. tmp_mb = TAD_OFFSET(reg) >> 20;
  1573. gb = div_u64_rem(tmp_mb, 1024, &mb);
  1574. edac_dbg(0, "TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
  1575. i, j,
  1576. gb, (mb*1000)/1024,
  1577. ((u64)tmp_mb) << 20L,
  1578. reg);
  1579. }
  1580. }
  1581. /*
  1582. * Step 6) Get RIR Wayness/Limit, per each channel
  1583. */
  1584. for (i = 0; i < NUM_CHANNELS; i++) {
  1585. if (!pvt->channel[i].dimms)
  1586. continue;
  1587. for (j = 0; j < MAX_RIR_RANGES; j++) {
  1588. pci_read_config_dword(pvt->pci_tad[i],
  1589. rir_way_limit[j],
  1590. &reg);
  1591. if (!IS_RIR_VALID(reg))
  1592. continue;
  1593. tmp_mb = pvt->info.rir_limit(reg) >> 20;
  1594. rir_way = 1 << RIR_WAY(reg);
  1595. gb = div_u64_rem(tmp_mb, 1024, &mb);
  1596. edac_dbg(0, "CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
  1597. i, j,
  1598. gb, (mb*1000)/1024,
  1599. ((u64)tmp_mb) << 20L,
  1600. rir_way,
  1601. reg);
  1602. for (k = 0; k < rir_way; k++) {
  1603. pci_read_config_dword(pvt->pci_tad[i],
  1604. rir_offset[j][k],
  1605. &reg);
  1606. tmp_mb = RIR_OFFSET(reg) << 6;
  1607. gb = div_u64_rem(tmp_mb, 1024, &mb);
  1608. edac_dbg(0, "CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
  1609. i, j, k,
  1610. gb, (mb*1000)/1024,
  1611. ((u64)tmp_mb) << 20L,
  1612. (u32)RIR_RNK_TGT(reg),
  1613. reg);
  1614. }
  1615. }
  1616. }
  1617. }
  1618. static struct mem_ctl_info *get_mci_for_node_id(u8 node_id)
  1619. {
  1620. struct sbridge_dev *sbridge_dev;
  1621. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  1622. if (sbridge_dev->node_id == node_id)
  1623. return sbridge_dev->mci;
  1624. }
  1625. return NULL;
  1626. }
  1627. static int get_memory_error_data(struct mem_ctl_info *mci,
  1628. u64 addr,
  1629. u8 *socket, u8 *ha,
  1630. long *channel_mask,
  1631. u8 *rank,
  1632. char **area_type, char *msg)
  1633. {
  1634. struct mem_ctl_info *new_mci;
  1635. struct sbridge_pvt *pvt = mci->pvt_info;
  1636. struct pci_dev *pci_ha;
  1637. int n_rir, n_sads, n_tads, sad_way, sck_xch;
  1638. int sad_interl, idx, base_ch;
  1639. int interleave_mode, shiftup = 0;
  1640. unsigned sad_interleave[pvt->info.max_interleave];
  1641. u32 reg, dram_rule;
  1642. u8 ch_way, sck_way, pkg, sad_ha = 0, ch_add = 0;
  1643. u32 tad_offset;
  1644. u32 rir_way;
  1645. u32 mb, gb;
  1646. u64 ch_addr, offset, limit = 0, prv = 0;
  1647. /*
  1648. * Step 0) Check if the address is at special memory ranges
  1649. * The check bellow is probably enough to fill all cases where
  1650. * the error is not inside a memory, except for the legacy
  1651. * range (e. g. VGA addresses). It is unlikely, however, that the
  1652. * memory controller would generate an error on that range.
  1653. */
  1654. if ((addr > (u64) pvt->tolm) && (addr < (1LL << 32))) {
  1655. sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
  1656. return -EINVAL;
  1657. }
  1658. if (addr >= (u64)pvt->tohm) {
  1659. sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
  1660. return -EINVAL;
  1661. }
  1662. /*
  1663. * Step 1) Get socket
  1664. */
  1665. for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
  1666. pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
  1667. &reg);
  1668. if (!DRAM_RULE_ENABLE(reg))
  1669. continue;
  1670. limit = pvt->info.sad_limit(reg);
  1671. if (limit <= prv) {
  1672. sprintf(msg, "Can't discover the memory socket");
  1673. return -EINVAL;
  1674. }
  1675. if (addr <= limit)
  1676. break;
  1677. prv = limit;
  1678. }
  1679. if (n_sads == pvt->info.max_sad) {
  1680. sprintf(msg, "Can't discover the memory socket");
  1681. return -EINVAL;
  1682. }
  1683. dram_rule = reg;
  1684. *area_type = show_dram_attr(pvt->info.dram_attr(dram_rule));
  1685. interleave_mode = pvt->info.interleave_mode(dram_rule);
  1686. pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
  1687. &reg);
  1688. if (pvt->info.type == SANDY_BRIDGE) {
  1689. sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
  1690. for (sad_way = 0; sad_way < 8; sad_way++) {
  1691. u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, sad_way);
  1692. if (sad_way > 0 && sad_interl == pkg)
  1693. break;
  1694. sad_interleave[sad_way] = pkg;
  1695. edac_dbg(0, "SAD interleave #%d: %d\n",
  1696. sad_way, sad_interleave[sad_way]);
  1697. }
  1698. edac_dbg(0, "mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
  1699. pvt->sbridge_dev->mc,
  1700. n_sads,
  1701. addr,
  1702. limit,
  1703. sad_way + 7,
  1704. !interleave_mode ? "" : "XOR[18:16]");
  1705. if (interleave_mode)
  1706. idx = ((addr >> 6) ^ (addr >> 16)) & 7;
  1707. else
  1708. idx = (addr >> 6) & 7;
  1709. switch (sad_way) {
  1710. case 1:
  1711. idx = 0;
  1712. break;
  1713. case 2:
  1714. idx = idx & 1;
  1715. break;
  1716. case 4:
  1717. idx = idx & 3;
  1718. break;
  1719. case 8:
  1720. break;
  1721. default:
  1722. sprintf(msg, "Can't discover socket interleave");
  1723. return -EINVAL;
  1724. }
  1725. *socket = sad_interleave[idx];
  1726. edac_dbg(0, "SAD interleave index: %d (wayness %d) = CPU socket %d\n",
  1727. idx, sad_way, *socket);
  1728. } else if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL) {
  1729. int bits, a7mode = A7MODE(dram_rule);
  1730. if (a7mode) {
  1731. /* A7 mode swaps P9 with P6 */
  1732. bits = GET_BITFIELD(addr, 7, 8) << 1;
  1733. bits |= GET_BITFIELD(addr, 9, 9);
  1734. } else
  1735. bits = GET_BITFIELD(addr, 6, 8);
  1736. if (interleave_mode == 0) {
  1737. /* interleave mode will XOR {8,7,6} with {18,17,16} */
  1738. idx = GET_BITFIELD(addr, 16, 18);
  1739. idx ^= bits;
  1740. } else
  1741. idx = bits;
  1742. pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
  1743. *socket = sad_pkg_socket(pkg);
  1744. sad_ha = sad_pkg_ha(pkg);
  1745. if (sad_ha)
  1746. ch_add = 4;
  1747. if (a7mode) {
  1748. /* MCChanShiftUpEnable */
  1749. pci_read_config_dword(pvt->pci_ha0,
  1750. HASWELL_HASYSDEFEATURE2, &reg);
  1751. shiftup = GET_BITFIELD(reg, 22, 22);
  1752. }
  1753. edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %i, shiftup: %i\n",
  1754. idx, *socket, sad_ha, shiftup);
  1755. } else {
  1756. /* Ivy Bridge's SAD mode doesn't support XOR interleave mode */
  1757. idx = (addr >> 6) & 7;
  1758. pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
  1759. *socket = sad_pkg_socket(pkg);
  1760. sad_ha = sad_pkg_ha(pkg);
  1761. if (sad_ha)
  1762. ch_add = 4;
  1763. edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %d\n",
  1764. idx, *socket, sad_ha);
  1765. }
  1766. *ha = sad_ha;
  1767. /*
  1768. * Move to the proper node structure, in order to access the
  1769. * right PCI registers
  1770. */
  1771. new_mci = get_mci_for_node_id(*socket);
  1772. if (!new_mci) {
  1773. sprintf(msg, "Struct for socket #%u wasn't initialized",
  1774. *socket);
  1775. return -EINVAL;
  1776. }
  1777. mci = new_mci;
  1778. pvt = mci->pvt_info;
  1779. /*
  1780. * Step 2) Get memory channel
  1781. */
  1782. prv = 0;
  1783. if (pvt->info.type == SANDY_BRIDGE)
  1784. pci_ha = pvt->pci_ha0;
  1785. else {
  1786. if (sad_ha)
  1787. pci_ha = pvt->pci_ha1;
  1788. else
  1789. pci_ha = pvt->pci_ha0;
  1790. }
  1791. for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
  1792. pci_read_config_dword(pci_ha, tad_dram_rule[n_tads], &reg);
  1793. limit = TAD_LIMIT(reg);
  1794. if (limit <= prv) {
  1795. sprintf(msg, "Can't discover the memory channel");
  1796. return -EINVAL;
  1797. }
  1798. if (addr <= limit)
  1799. break;
  1800. prv = limit;
  1801. }
  1802. if (n_tads == MAX_TAD) {
  1803. sprintf(msg, "Can't discover the memory channel");
  1804. return -EINVAL;
  1805. }
  1806. ch_way = TAD_CH(reg) + 1;
  1807. sck_way = 1 << TAD_SOCK(reg);
  1808. if (ch_way == 3)
  1809. idx = addr >> 6;
  1810. else
  1811. idx = (addr >> (6 + sck_way + shiftup)) & 0x3;
  1812. idx = idx % ch_way;
  1813. /*
  1814. * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
  1815. */
  1816. switch (idx) {
  1817. case 0:
  1818. base_ch = TAD_TGT0(reg);
  1819. break;
  1820. case 1:
  1821. base_ch = TAD_TGT1(reg);
  1822. break;
  1823. case 2:
  1824. base_ch = TAD_TGT2(reg);
  1825. break;
  1826. case 3:
  1827. base_ch = TAD_TGT3(reg);
  1828. break;
  1829. default:
  1830. sprintf(msg, "Can't discover the TAD target");
  1831. return -EINVAL;
  1832. }
  1833. *channel_mask = 1 << base_ch;
  1834. pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
  1835. tad_ch_nilv_offset[n_tads],
  1836. &tad_offset);
  1837. if (pvt->is_mirrored) {
  1838. *channel_mask |= 1 << ((base_ch + 2) % 4);
  1839. switch(ch_way) {
  1840. case 2:
  1841. case 4:
  1842. sck_xch = 1 << sck_way * (ch_way >> 1);
  1843. break;
  1844. default:
  1845. sprintf(msg, "Invalid mirror set. Can't decode addr");
  1846. return -EINVAL;
  1847. }
  1848. } else
  1849. sck_xch = (1 << sck_way) * ch_way;
  1850. if (pvt->is_lockstep)
  1851. *channel_mask |= 1 << ((base_ch + 1) % 4);
  1852. offset = TAD_OFFSET(tad_offset);
  1853. 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",
  1854. n_tads,
  1855. addr,
  1856. limit,
  1857. sck_way,
  1858. ch_way,
  1859. offset,
  1860. idx,
  1861. base_ch,
  1862. *channel_mask);
  1863. /* Calculate channel address */
  1864. /* Remove the TAD offset */
  1865. if (offset > addr) {
  1866. sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
  1867. offset, addr);
  1868. return -EINVAL;
  1869. }
  1870. ch_addr = addr - offset;
  1871. ch_addr >>= (6 + shiftup);
  1872. ch_addr /= ch_way * sck_way;
  1873. ch_addr <<= (6 + shiftup);
  1874. ch_addr |= addr & ((1 << (6 + shiftup)) - 1);
  1875. /*
  1876. * Step 3) Decode rank
  1877. */
  1878. for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
  1879. pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
  1880. rir_way_limit[n_rir],
  1881. &reg);
  1882. if (!IS_RIR_VALID(reg))
  1883. continue;
  1884. limit = pvt->info.rir_limit(reg);
  1885. gb = div_u64_rem(limit >> 20, 1024, &mb);
  1886. edac_dbg(0, "RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
  1887. n_rir,
  1888. gb, (mb*1000)/1024,
  1889. limit,
  1890. 1 << RIR_WAY(reg));
  1891. if (ch_addr <= limit)
  1892. break;
  1893. }
  1894. if (n_rir == MAX_RIR_RANGES) {
  1895. sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
  1896. ch_addr);
  1897. return -EINVAL;
  1898. }
  1899. rir_way = RIR_WAY(reg);
  1900. if (pvt->is_close_pg)
  1901. idx = (ch_addr >> 6);
  1902. else
  1903. idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
  1904. idx %= 1 << rir_way;
  1905. pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
  1906. rir_offset[n_rir][idx],
  1907. &reg);
  1908. *rank = RIR_RNK_TGT(reg);
  1909. edac_dbg(0, "RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
  1910. n_rir,
  1911. ch_addr,
  1912. limit,
  1913. rir_way,
  1914. idx);
  1915. return 0;
  1916. }
  1917. /****************************************************************************
  1918. Device initialization routines: put/get, init/exit
  1919. ****************************************************************************/
  1920. /*
  1921. * sbridge_put_all_devices 'put' all the devices that we have
  1922. * reserved via 'get'
  1923. */
  1924. static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
  1925. {
  1926. int i;
  1927. edac_dbg(0, "\n");
  1928. for (i = 0; i < sbridge_dev->n_devs; i++) {
  1929. struct pci_dev *pdev = sbridge_dev->pdev[i];
  1930. if (!pdev)
  1931. continue;
  1932. edac_dbg(0, "Removing dev %02x:%02x.%d\n",
  1933. pdev->bus->number,
  1934. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  1935. pci_dev_put(pdev);
  1936. }
  1937. }
  1938. static void sbridge_put_all_devices(void)
  1939. {
  1940. struct sbridge_dev *sbridge_dev, *tmp;
  1941. list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
  1942. sbridge_put_devices(sbridge_dev);
  1943. free_sbridge_dev(sbridge_dev);
  1944. }
  1945. }
  1946. static int sbridge_get_onedevice(struct pci_dev **prev,
  1947. u8 *num_mc,
  1948. const struct pci_id_table *table,
  1949. const unsigned devno,
  1950. const int multi_bus)
  1951. {
  1952. struct sbridge_dev *sbridge_dev;
  1953. const struct pci_id_descr *dev_descr = &table->descr[devno];
  1954. struct pci_dev *pdev = NULL;
  1955. u8 bus = 0;
  1956. sbridge_printk(KERN_DEBUG,
  1957. "Seeking for: PCI ID %04x:%04x\n",
  1958. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1959. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1960. dev_descr->dev_id, *prev);
  1961. if (!pdev) {
  1962. if (*prev) {
  1963. *prev = pdev;
  1964. return 0;
  1965. }
  1966. if (dev_descr->optional)
  1967. return 0;
  1968. /* if the HA wasn't found */
  1969. if (devno == 0)
  1970. return -ENODEV;
  1971. sbridge_printk(KERN_INFO,
  1972. "Device not found: %04x:%04x\n",
  1973. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1974. /* End of list, leave */
  1975. return -ENODEV;
  1976. }
  1977. bus = pdev->bus->number;
  1978. sbridge_dev = get_sbridge_dev(bus, multi_bus);
  1979. if (!sbridge_dev) {
  1980. sbridge_dev = alloc_sbridge_dev(bus, table);
  1981. if (!sbridge_dev) {
  1982. pci_dev_put(pdev);
  1983. return -ENOMEM;
  1984. }
  1985. (*num_mc)++;
  1986. }
  1987. if (sbridge_dev->pdev[devno]) {
  1988. sbridge_printk(KERN_ERR,
  1989. "Duplicated device for %04x:%04x\n",
  1990. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1991. pci_dev_put(pdev);
  1992. return -ENODEV;
  1993. }
  1994. sbridge_dev->pdev[devno] = pdev;
  1995. /* Be sure that the device is enabled */
  1996. if (unlikely(pci_enable_device(pdev) < 0)) {
  1997. sbridge_printk(KERN_ERR,
  1998. "Couldn't enable %04x:%04x\n",
  1999. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  2000. return -ENODEV;
  2001. }
  2002. edac_dbg(0, "Detected %04x:%04x\n",
  2003. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  2004. /*
  2005. * As stated on drivers/pci/search.c, the reference count for
  2006. * @from is always decremented if it is not %NULL. So, as we need
  2007. * to get all devices up to null, we need to do a get for the device
  2008. */
  2009. pci_dev_get(pdev);
  2010. *prev = pdev;
  2011. return 0;
  2012. }
  2013. /*
  2014. * sbridge_get_all_devices - Find and perform 'get' operation on the MCH's
  2015. * devices we want to reference for this driver.
  2016. * @num_mc: pointer to the memory controllers count, to be incremented in case
  2017. * of success.
  2018. * @table: model specific table
  2019. * @allow_dups: allow for multiple devices to exist with the same device id
  2020. * (as implemented, this isn't expected to work correctly in the
  2021. * multi-socket case).
  2022. * @multi_bus: don't assume devices on different buses belong to different
  2023. * memory controllers.
  2024. *
  2025. * returns 0 in case of success or error code
  2026. */
  2027. static int sbridge_get_all_devices_full(u8 *num_mc,
  2028. const struct pci_id_table *table,
  2029. int allow_dups,
  2030. int multi_bus)
  2031. {
  2032. int i, rc;
  2033. struct pci_dev *pdev = NULL;
  2034. while (table && table->descr) {
  2035. for (i = 0; i < table->n_devs; i++) {
  2036. if (!allow_dups || i == 0 ||
  2037. table->descr[i].dev_id !=
  2038. table->descr[i-1].dev_id) {
  2039. pdev = NULL;
  2040. }
  2041. do {
  2042. rc = sbridge_get_onedevice(&pdev, num_mc,
  2043. table, i, multi_bus);
  2044. if (rc < 0) {
  2045. if (i == 0) {
  2046. i = table->n_devs;
  2047. break;
  2048. }
  2049. sbridge_put_all_devices();
  2050. return -ENODEV;
  2051. }
  2052. } while (pdev && !allow_dups);
  2053. }
  2054. table++;
  2055. }
  2056. return 0;
  2057. }
  2058. #define sbridge_get_all_devices(num_mc, table) \
  2059. sbridge_get_all_devices_full(num_mc, table, 0, 0)
  2060. #define sbridge_get_all_devices_knl(num_mc, table) \
  2061. sbridge_get_all_devices_full(num_mc, table, 1, 1)
  2062. static int sbridge_mci_bind_devs(struct mem_ctl_info *mci,
  2063. struct sbridge_dev *sbridge_dev)
  2064. {
  2065. struct sbridge_pvt *pvt = mci->pvt_info;
  2066. struct pci_dev *pdev;
  2067. u8 saw_chan_mask = 0;
  2068. int i;
  2069. for (i = 0; i < sbridge_dev->n_devs; i++) {
  2070. pdev = sbridge_dev->pdev[i];
  2071. if (!pdev)
  2072. continue;
  2073. switch (pdev->device) {
  2074. case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0:
  2075. pvt->pci_sad0 = pdev;
  2076. break;
  2077. case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1:
  2078. pvt->pci_sad1 = pdev;
  2079. break;
  2080. case PCI_DEVICE_ID_INTEL_SBRIDGE_BR:
  2081. pvt->pci_br0 = pdev;
  2082. break;
  2083. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0:
  2084. pvt->pci_ha0 = pdev;
  2085. break;
  2086. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA:
  2087. pvt->pci_ta = pdev;
  2088. break;
  2089. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS:
  2090. pvt->pci_ras = pdev;
  2091. break;
  2092. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0:
  2093. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1:
  2094. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2:
  2095. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3:
  2096. {
  2097. int id = pdev->device - PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0;
  2098. pvt->pci_tad[id] = pdev;
  2099. saw_chan_mask |= 1 << id;
  2100. }
  2101. break;
  2102. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO:
  2103. pvt->pci_ddrio = pdev;
  2104. break;
  2105. default:
  2106. goto error;
  2107. }
  2108. edac_dbg(0, "Associated PCI %02x:%02x, bus %d with dev = %p\n",
  2109. pdev->vendor, pdev->device,
  2110. sbridge_dev->bus,
  2111. pdev);
  2112. }
  2113. /* Check if everything were registered */
  2114. if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha0 ||
  2115. !pvt-> pci_tad || !pvt->pci_ras || !pvt->pci_ta)
  2116. goto enodev;
  2117. if (saw_chan_mask != 0x0f)
  2118. goto enodev;
  2119. return 0;
  2120. enodev:
  2121. sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
  2122. return -ENODEV;
  2123. error:
  2124. sbridge_printk(KERN_ERR, "Unexpected device %02x:%02x\n",
  2125. PCI_VENDOR_ID_INTEL, pdev->device);
  2126. return -EINVAL;
  2127. }
  2128. static int ibridge_mci_bind_devs(struct mem_ctl_info *mci,
  2129. struct sbridge_dev *sbridge_dev)
  2130. {
  2131. struct sbridge_pvt *pvt = mci->pvt_info;
  2132. struct pci_dev *pdev;
  2133. u8 saw_chan_mask = 0;
  2134. int i;
  2135. for (i = 0; i < sbridge_dev->n_devs; i++) {
  2136. pdev = sbridge_dev->pdev[i];
  2137. if (!pdev)
  2138. continue;
  2139. switch (pdev->device) {
  2140. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0:
  2141. pvt->pci_ha0 = pdev;
  2142. break;
  2143. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA:
  2144. pvt->pci_ta = pdev;
  2145. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS:
  2146. pvt->pci_ras = pdev;
  2147. break;
  2148. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0:
  2149. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1:
  2150. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2:
  2151. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3:
  2152. {
  2153. int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0;
  2154. pvt->pci_tad[id] = pdev;
  2155. saw_chan_mask |= 1 << id;
  2156. }
  2157. break;
  2158. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0:
  2159. pvt->pci_ddrio = pdev;
  2160. break;
  2161. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0:
  2162. pvt->pci_ddrio = pdev;
  2163. break;
  2164. case PCI_DEVICE_ID_INTEL_IBRIDGE_SAD:
  2165. pvt->pci_sad0 = pdev;
  2166. break;
  2167. case PCI_DEVICE_ID_INTEL_IBRIDGE_BR0:
  2168. pvt->pci_br0 = pdev;
  2169. break;
  2170. case PCI_DEVICE_ID_INTEL_IBRIDGE_BR1:
  2171. pvt->pci_br1 = pdev;
  2172. break;
  2173. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1:
  2174. pvt->pci_ha1 = pdev;
  2175. break;
  2176. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0:
  2177. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1:
  2178. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2:
  2179. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3:
  2180. {
  2181. int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 + 4;
  2182. pvt->pci_tad[id] = pdev;
  2183. saw_chan_mask |= 1 << id;
  2184. }
  2185. break;
  2186. default:
  2187. goto error;
  2188. }
  2189. edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
  2190. sbridge_dev->bus,
  2191. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  2192. pdev);
  2193. }
  2194. /* Check if everything were registered */
  2195. if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_br0 ||
  2196. !pvt->pci_br1 || !pvt->pci_tad || !pvt->pci_ras ||
  2197. !pvt->pci_ta)
  2198. goto enodev;
  2199. if (saw_chan_mask != 0x0f && /* -EN */
  2200. saw_chan_mask != 0x33 && /* -EP */
  2201. saw_chan_mask != 0xff) /* -EX */
  2202. goto enodev;
  2203. return 0;
  2204. enodev:
  2205. sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
  2206. return -ENODEV;
  2207. error:
  2208. sbridge_printk(KERN_ERR,
  2209. "Unexpected device %02x:%02x\n", PCI_VENDOR_ID_INTEL,
  2210. pdev->device);
  2211. return -EINVAL;
  2212. }
  2213. static int haswell_mci_bind_devs(struct mem_ctl_info *mci,
  2214. struct sbridge_dev *sbridge_dev)
  2215. {
  2216. struct sbridge_pvt *pvt = mci->pvt_info;
  2217. struct pci_dev *pdev;
  2218. u8 saw_chan_mask = 0;
  2219. int i;
  2220. /* there's only one device per system; not tied to any bus */
  2221. if (pvt->info.pci_vtd == NULL)
  2222. /* result will be checked later */
  2223. pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
  2224. PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC,
  2225. NULL);
  2226. for (i = 0; i < sbridge_dev->n_devs; i++) {
  2227. pdev = sbridge_dev->pdev[i];
  2228. if (!pdev)
  2229. continue;
  2230. switch (pdev->device) {
  2231. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0:
  2232. pvt->pci_sad0 = pdev;
  2233. break;
  2234. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1:
  2235. pvt->pci_sad1 = pdev;
  2236. break;
  2237. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0:
  2238. pvt->pci_ha0 = pdev;
  2239. break;
  2240. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA:
  2241. pvt->pci_ta = pdev;
  2242. break;
  2243. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL:
  2244. pvt->pci_ras = pdev;
  2245. break;
  2246. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0:
  2247. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1:
  2248. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2:
  2249. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3:
  2250. {
  2251. int id = pdev->device - PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0;
  2252. pvt->pci_tad[id] = pdev;
  2253. saw_chan_mask |= 1 << id;
  2254. }
  2255. break;
  2256. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0:
  2257. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1:
  2258. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2:
  2259. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3:
  2260. {
  2261. int id = pdev->device - PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 + 4;
  2262. pvt->pci_tad[id] = pdev;
  2263. saw_chan_mask |= 1 << id;
  2264. }
  2265. break;
  2266. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0:
  2267. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1:
  2268. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2:
  2269. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3:
  2270. if (!pvt->pci_ddrio)
  2271. pvt->pci_ddrio = pdev;
  2272. break;
  2273. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1:
  2274. pvt->pci_ha1 = pdev;
  2275. break;
  2276. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA:
  2277. pvt->pci_ha1_ta = pdev;
  2278. break;
  2279. default:
  2280. break;
  2281. }
  2282. edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
  2283. sbridge_dev->bus,
  2284. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  2285. pdev);
  2286. }
  2287. /* Check if everything were registered */
  2288. if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_sad1 ||
  2289. !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
  2290. goto enodev;
  2291. if (saw_chan_mask != 0x0f && /* -EN */
  2292. saw_chan_mask != 0x33 && /* -EP */
  2293. saw_chan_mask != 0xff) /* -EX */
  2294. goto enodev;
  2295. return 0;
  2296. enodev:
  2297. sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
  2298. return -ENODEV;
  2299. }
  2300. static int broadwell_mci_bind_devs(struct mem_ctl_info *mci,
  2301. struct sbridge_dev *sbridge_dev)
  2302. {
  2303. struct sbridge_pvt *pvt = mci->pvt_info;
  2304. struct pci_dev *pdev;
  2305. u8 saw_chan_mask = 0;
  2306. int i;
  2307. /* there's only one device per system; not tied to any bus */
  2308. if (pvt->info.pci_vtd == NULL)
  2309. /* result will be checked later */
  2310. pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
  2311. PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC,
  2312. NULL);
  2313. for (i = 0; i < sbridge_dev->n_devs; i++) {
  2314. pdev = sbridge_dev->pdev[i];
  2315. if (!pdev)
  2316. continue;
  2317. switch (pdev->device) {
  2318. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0:
  2319. pvt->pci_sad0 = pdev;
  2320. break;
  2321. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1:
  2322. pvt->pci_sad1 = pdev;
  2323. break;
  2324. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0:
  2325. pvt->pci_ha0 = pdev;
  2326. break;
  2327. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA:
  2328. pvt->pci_ta = pdev;
  2329. break;
  2330. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL:
  2331. pvt->pci_ras = pdev;
  2332. break;
  2333. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0:
  2334. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1:
  2335. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2:
  2336. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3:
  2337. {
  2338. int id = pdev->device - PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0;
  2339. pvt->pci_tad[id] = pdev;
  2340. saw_chan_mask |= 1 << id;
  2341. }
  2342. break;
  2343. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0:
  2344. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1:
  2345. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2:
  2346. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3:
  2347. {
  2348. int id = pdev->device - PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0 + 4;
  2349. pvt->pci_tad[id] = pdev;
  2350. saw_chan_mask |= 1 << id;
  2351. }
  2352. break;
  2353. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0:
  2354. pvt->pci_ddrio = pdev;
  2355. break;
  2356. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1:
  2357. pvt->pci_ha1 = pdev;
  2358. break;
  2359. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA:
  2360. pvt->pci_ha1_ta = pdev;
  2361. break;
  2362. default:
  2363. break;
  2364. }
  2365. edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
  2366. sbridge_dev->bus,
  2367. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  2368. pdev);
  2369. }
  2370. /* Check if everything were registered */
  2371. if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_sad1 ||
  2372. !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
  2373. goto enodev;
  2374. if (saw_chan_mask != 0x0f && /* -EN */
  2375. saw_chan_mask != 0x33 && /* -EP */
  2376. saw_chan_mask != 0xff) /* -EX */
  2377. goto enodev;
  2378. return 0;
  2379. enodev:
  2380. sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
  2381. return -ENODEV;
  2382. }
  2383. static int knl_mci_bind_devs(struct mem_ctl_info *mci,
  2384. struct sbridge_dev *sbridge_dev)
  2385. {
  2386. struct sbridge_pvt *pvt = mci->pvt_info;
  2387. struct pci_dev *pdev;
  2388. int dev, func;
  2389. int i;
  2390. int devidx;
  2391. for (i = 0; i < sbridge_dev->n_devs; i++) {
  2392. pdev = sbridge_dev->pdev[i];
  2393. if (!pdev)
  2394. continue;
  2395. /* Extract PCI device and function. */
  2396. dev = (pdev->devfn >> 3) & 0x1f;
  2397. func = pdev->devfn & 0x7;
  2398. switch (pdev->device) {
  2399. case PCI_DEVICE_ID_INTEL_KNL_IMC_MC:
  2400. if (dev == 8)
  2401. pvt->knl.pci_mc0 = pdev;
  2402. else if (dev == 9)
  2403. pvt->knl.pci_mc1 = pdev;
  2404. else {
  2405. sbridge_printk(KERN_ERR,
  2406. "Memory controller in unexpected place! (dev %d, fn %d)\n",
  2407. dev, func);
  2408. continue;
  2409. }
  2410. break;
  2411. case PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0:
  2412. pvt->pci_sad0 = pdev;
  2413. break;
  2414. case PCI_DEVICE_ID_INTEL_KNL_IMC_SAD1:
  2415. pvt->pci_sad1 = pdev;
  2416. break;
  2417. case PCI_DEVICE_ID_INTEL_KNL_IMC_CHA:
  2418. /* There are one of these per tile, and range from
  2419. * 1.14.0 to 1.18.5.
  2420. */
  2421. devidx = ((dev-14)*8)+func;
  2422. if (devidx < 0 || devidx >= KNL_MAX_CHAS) {
  2423. sbridge_printk(KERN_ERR,
  2424. "Caching and Home Agent in unexpected place! (dev %d, fn %d)\n",
  2425. dev, func);
  2426. continue;
  2427. }
  2428. WARN_ON(pvt->knl.pci_cha[devidx] != NULL);
  2429. pvt->knl.pci_cha[devidx] = pdev;
  2430. break;
  2431. case PCI_DEVICE_ID_INTEL_KNL_IMC_CHANNEL:
  2432. devidx = -1;
  2433. /*
  2434. * MC0 channels 0-2 are device 9 function 2-4,
  2435. * MC1 channels 3-5 are device 8 function 2-4.
  2436. */
  2437. if (dev == 9)
  2438. devidx = func-2;
  2439. else if (dev == 8)
  2440. devidx = 3 + (func-2);
  2441. if (devidx < 0 || devidx >= KNL_MAX_CHANNELS) {
  2442. sbridge_printk(KERN_ERR,
  2443. "DRAM Channel Registers in unexpected place! (dev %d, fn %d)\n",
  2444. dev, func);
  2445. continue;
  2446. }
  2447. WARN_ON(pvt->knl.pci_channel[devidx] != NULL);
  2448. pvt->knl.pci_channel[devidx] = pdev;
  2449. break;
  2450. case PCI_DEVICE_ID_INTEL_KNL_IMC_TOLHM:
  2451. pvt->knl.pci_mc_info = pdev;
  2452. break;
  2453. case PCI_DEVICE_ID_INTEL_KNL_IMC_TA:
  2454. pvt->pci_ta = pdev;
  2455. break;
  2456. default:
  2457. sbridge_printk(KERN_ERR, "Unexpected device %d\n",
  2458. pdev->device);
  2459. break;
  2460. }
  2461. }
  2462. if (!pvt->knl.pci_mc0 || !pvt->knl.pci_mc1 ||
  2463. !pvt->pci_sad0 || !pvt->pci_sad1 ||
  2464. !pvt->pci_ta) {
  2465. goto enodev;
  2466. }
  2467. for (i = 0; i < KNL_MAX_CHANNELS; i++) {
  2468. if (!pvt->knl.pci_channel[i]) {
  2469. sbridge_printk(KERN_ERR, "Missing channel %d\n", i);
  2470. goto enodev;
  2471. }
  2472. }
  2473. for (i = 0; i < KNL_MAX_CHAS; i++) {
  2474. if (!pvt->knl.pci_cha[i]) {
  2475. sbridge_printk(KERN_ERR, "Missing CHA %d\n", i);
  2476. goto enodev;
  2477. }
  2478. }
  2479. return 0;
  2480. enodev:
  2481. sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
  2482. return -ENODEV;
  2483. }
  2484. /****************************************************************************
  2485. Error check routines
  2486. ****************************************************************************/
  2487. /*
  2488. * While Sandy Bridge has error count registers, SMI BIOS read values from
  2489. * and resets the counters. So, they are not reliable for the OS to read
  2490. * from them. So, we have no option but to just trust on whatever MCE is
  2491. * telling us about the errors.
  2492. */
  2493. static void sbridge_mce_output_error(struct mem_ctl_info *mci,
  2494. const struct mce *m)
  2495. {
  2496. struct mem_ctl_info *new_mci;
  2497. struct sbridge_pvt *pvt = mci->pvt_info;
  2498. enum hw_event_mc_err_type tp_event;
  2499. char *type, *optype, msg[256];
  2500. bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
  2501. bool overflow = GET_BITFIELD(m->status, 62, 62);
  2502. bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
  2503. bool recoverable;
  2504. u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
  2505. u32 mscod = GET_BITFIELD(m->status, 16, 31);
  2506. u32 errcode = GET_BITFIELD(m->status, 0, 15);
  2507. u32 channel = GET_BITFIELD(m->status, 0, 3);
  2508. u32 optypenum = GET_BITFIELD(m->status, 4, 6);
  2509. long channel_mask, first_channel;
  2510. u8 rank, socket, ha;
  2511. int rc, dimm;
  2512. char *area_type = NULL;
  2513. if (pvt->info.type != SANDY_BRIDGE)
  2514. recoverable = true;
  2515. else
  2516. recoverable = GET_BITFIELD(m->status, 56, 56);
  2517. if (uncorrected_error) {
  2518. if (ripv) {
  2519. type = "FATAL";
  2520. tp_event = HW_EVENT_ERR_FATAL;
  2521. } else {
  2522. type = "NON_FATAL";
  2523. tp_event = HW_EVENT_ERR_UNCORRECTED;
  2524. }
  2525. } else {
  2526. type = "CORRECTED";
  2527. tp_event = HW_EVENT_ERR_CORRECTED;
  2528. }
  2529. /*
  2530. * According with Table 15-9 of the Intel Architecture spec vol 3A,
  2531. * memory errors should fit in this mask:
  2532. * 000f 0000 1mmm cccc (binary)
  2533. * where:
  2534. * f = Correction Report Filtering Bit. If 1, subsequent errors
  2535. * won't be shown
  2536. * mmm = error type
  2537. * cccc = channel
  2538. * If the mask doesn't match, report an error to the parsing logic
  2539. */
  2540. if (! ((errcode & 0xef80) == 0x80)) {
  2541. optype = "Can't parse: it is not a mem";
  2542. } else {
  2543. switch (optypenum) {
  2544. case 0:
  2545. optype = "generic undef request error";
  2546. break;
  2547. case 1:
  2548. optype = "memory read error";
  2549. break;
  2550. case 2:
  2551. optype = "memory write error";
  2552. break;
  2553. case 3:
  2554. optype = "addr/cmd error";
  2555. break;
  2556. case 4:
  2557. optype = "memory scrubbing error";
  2558. break;
  2559. default:
  2560. optype = "reserved";
  2561. break;
  2562. }
  2563. }
  2564. /* Only decode errors with an valid address (ADDRV) */
  2565. if (!GET_BITFIELD(m->status, 58, 58))
  2566. return;
  2567. if (pvt->info.type == KNIGHTS_LANDING) {
  2568. if (channel == 14) {
  2569. edac_dbg(0, "%s%s err_code:%04x:%04x EDRAM bank %d\n",
  2570. overflow ? " OVERFLOW" : "",
  2571. (uncorrected_error && recoverable)
  2572. ? " recoverable" : "",
  2573. mscod, errcode,
  2574. m->bank);
  2575. } else {
  2576. char A = *("A");
  2577. channel = knl_channel_remap(channel);
  2578. channel_mask = 1 << channel;
  2579. snprintf(msg, sizeof(msg),
  2580. "%s%s err_code:%04x:%04x channel:%d (DIMM_%c)",
  2581. overflow ? " OVERFLOW" : "",
  2582. (uncorrected_error && recoverable)
  2583. ? " recoverable" : " ",
  2584. mscod, errcode, channel, A + channel);
  2585. edac_mc_handle_error(tp_event, mci, core_err_cnt,
  2586. m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
  2587. channel, 0, -1,
  2588. optype, msg);
  2589. }
  2590. return;
  2591. } else {
  2592. rc = get_memory_error_data(mci, m->addr, &socket, &ha,
  2593. &channel_mask, &rank, &area_type, msg);
  2594. }
  2595. if (rc < 0)
  2596. goto err_parsing;
  2597. new_mci = get_mci_for_node_id(socket);
  2598. if (!new_mci) {
  2599. strcpy(msg, "Error: socket got corrupted!");
  2600. goto err_parsing;
  2601. }
  2602. mci = new_mci;
  2603. pvt = mci->pvt_info;
  2604. first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
  2605. if (rank < 4)
  2606. dimm = 0;
  2607. else if (rank < 8)
  2608. dimm = 1;
  2609. else
  2610. dimm = 2;
  2611. /*
  2612. * FIXME: On some memory configurations (mirror, lockstep), the
  2613. * Memory Controller can't point the error to a single DIMM. The
  2614. * EDAC core should be handling the channel mask, in order to point
  2615. * to the group of dimm's where the error may be happening.
  2616. */
  2617. if (!pvt->is_lockstep && !pvt->is_mirrored && !pvt->is_close_pg)
  2618. channel = first_channel;
  2619. snprintf(msg, sizeof(msg),
  2620. "%s%s area:%s err_code:%04x:%04x socket:%d ha:%d channel_mask:%ld rank:%d",
  2621. overflow ? " OVERFLOW" : "",
  2622. (uncorrected_error && recoverable) ? " recoverable" : "",
  2623. area_type,
  2624. mscod, errcode,
  2625. socket, ha,
  2626. channel_mask,
  2627. rank);
  2628. edac_dbg(0, "%s\n", msg);
  2629. /* FIXME: need support for channel mask */
  2630. if (channel == CHANNEL_UNSPECIFIED)
  2631. channel = -1;
  2632. /* Call the helper to output message */
  2633. edac_mc_handle_error(tp_event, mci, core_err_cnt,
  2634. m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
  2635. 4*ha+channel, dimm, -1,
  2636. optype, msg);
  2637. return;
  2638. err_parsing:
  2639. edac_mc_handle_error(tp_event, mci, core_err_cnt, 0, 0, 0,
  2640. -1, -1, -1,
  2641. msg, "");
  2642. }
  2643. /*
  2644. * sbridge_check_error Retrieve and process errors reported by the
  2645. * hardware. Called by the Core module.
  2646. */
  2647. static void sbridge_check_error(struct mem_ctl_info *mci)
  2648. {
  2649. struct sbridge_pvt *pvt = mci->pvt_info;
  2650. int i;
  2651. unsigned count = 0;
  2652. struct mce *m;
  2653. /*
  2654. * MCE first step: Copy all mce errors into a temporary buffer
  2655. * We use a double buffering here, to reduce the risk of
  2656. * loosing an error.
  2657. */
  2658. smp_rmb();
  2659. count = (pvt->mce_out + MCE_LOG_LEN - pvt->mce_in)
  2660. % MCE_LOG_LEN;
  2661. if (!count)
  2662. return;
  2663. m = pvt->mce_outentry;
  2664. if (pvt->mce_in + count > MCE_LOG_LEN) {
  2665. unsigned l = MCE_LOG_LEN - pvt->mce_in;
  2666. memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * l);
  2667. smp_wmb();
  2668. pvt->mce_in = 0;
  2669. count -= l;
  2670. m += l;
  2671. }
  2672. memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * count);
  2673. smp_wmb();
  2674. pvt->mce_in += count;
  2675. smp_rmb();
  2676. if (pvt->mce_overrun) {
  2677. sbridge_printk(KERN_ERR, "Lost %d memory errors\n",
  2678. pvt->mce_overrun);
  2679. smp_wmb();
  2680. pvt->mce_overrun = 0;
  2681. }
  2682. /*
  2683. * MCE second step: parse errors and display
  2684. */
  2685. for (i = 0; i < count; i++)
  2686. sbridge_mce_output_error(mci, &pvt->mce_outentry[i]);
  2687. }
  2688. /*
  2689. * sbridge_mce_check_error Replicates mcelog routine to get errors
  2690. * This routine simply queues mcelog errors, and
  2691. * return. The error itself should be handled later
  2692. * by sbridge_check_error.
  2693. * WARNING: As this routine should be called at NMI time, extra care should
  2694. * be taken to avoid deadlocks, and to be as fast as possible.
  2695. */
  2696. static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
  2697. void *data)
  2698. {
  2699. struct mce *mce = (struct mce *)data;
  2700. struct mem_ctl_info *mci;
  2701. struct sbridge_pvt *pvt;
  2702. char *type;
  2703. if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
  2704. return NOTIFY_DONE;
  2705. mci = get_mci_for_node_id(mce->socketid);
  2706. if (!mci)
  2707. return NOTIFY_BAD;
  2708. pvt = mci->pvt_info;
  2709. /*
  2710. * Just let mcelog handle it if the error is
  2711. * outside the memory controller. A memory error
  2712. * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
  2713. * bit 12 has an special meaning.
  2714. */
  2715. if ((mce->status & 0xefff) >> 7 != 1)
  2716. return NOTIFY_DONE;
  2717. if (mce->mcgstatus & MCG_STATUS_MCIP)
  2718. type = "Exception";
  2719. else
  2720. type = "Event";
  2721. sbridge_mc_printk(mci, KERN_DEBUG, "HANDLING MCE MEMORY ERROR\n");
  2722. sbridge_mc_printk(mci, KERN_DEBUG, "CPU %d: Machine Check %s: %Lx "
  2723. "Bank %d: %016Lx\n", mce->extcpu, type,
  2724. mce->mcgstatus, mce->bank, mce->status);
  2725. sbridge_mc_printk(mci, KERN_DEBUG, "TSC %llx ", mce->tsc);
  2726. sbridge_mc_printk(mci, KERN_DEBUG, "ADDR %llx ", mce->addr);
  2727. sbridge_mc_printk(mci, KERN_DEBUG, "MISC %llx ", mce->misc);
  2728. sbridge_mc_printk(mci, KERN_DEBUG, "PROCESSOR %u:%x TIME %llu SOCKET "
  2729. "%u APIC %x\n", mce->cpuvendor, mce->cpuid,
  2730. mce->time, mce->socketid, mce->apicid);
  2731. smp_rmb();
  2732. if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
  2733. smp_wmb();
  2734. pvt->mce_overrun++;
  2735. return NOTIFY_DONE;
  2736. }
  2737. /* Copy memory error at the ringbuffer */
  2738. memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
  2739. smp_wmb();
  2740. pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
  2741. /* Handle fatal errors immediately */
  2742. if (mce->mcgstatus & 1)
  2743. sbridge_check_error(mci);
  2744. /* Advice mcelog that the error were handled */
  2745. return NOTIFY_STOP;
  2746. }
  2747. static struct notifier_block sbridge_mce_dec = {
  2748. .notifier_call = sbridge_mce_check_error,
  2749. };
  2750. /****************************************************************************
  2751. EDAC register/unregister logic
  2752. ****************************************************************************/
  2753. static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
  2754. {
  2755. struct mem_ctl_info *mci = sbridge_dev->mci;
  2756. struct sbridge_pvt *pvt;
  2757. if (unlikely(!mci || !mci->pvt_info)) {
  2758. edac_dbg(0, "MC: dev = %p\n", &sbridge_dev->pdev[0]->dev);
  2759. sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
  2760. return;
  2761. }
  2762. pvt = mci->pvt_info;
  2763. edac_dbg(0, "MC: mci = %p, dev = %p\n",
  2764. mci, &sbridge_dev->pdev[0]->dev);
  2765. /* Remove MC sysfs nodes */
  2766. edac_mc_del_mc(mci->pdev);
  2767. edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
  2768. kfree(mci->ctl_name);
  2769. edac_mc_free(mci);
  2770. sbridge_dev->mci = NULL;
  2771. }
  2772. static int sbridge_register_mci(struct sbridge_dev *sbridge_dev, enum type type)
  2773. {
  2774. struct mem_ctl_info *mci;
  2775. struct edac_mc_layer layers[2];
  2776. struct sbridge_pvt *pvt;
  2777. struct pci_dev *pdev = sbridge_dev->pdev[0];
  2778. int rc;
  2779. /* Check the number of active and not disabled channels */
  2780. rc = check_if_ecc_is_active(sbridge_dev->bus, type);
  2781. if (unlikely(rc < 0))
  2782. return rc;
  2783. /* allocate a new MC control structure */
  2784. layers[0].type = EDAC_MC_LAYER_CHANNEL;
  2785. layers[0].size = type == KNIGHTS_LANDING ?
  2786. KNL_MAX_CHANNELS : NUM_CHANNELS;
  2787. layers[0].is_virt_csrow = false;
  2788. layers[1].type = EDAC_MC_LAYER_SLOT;
  2789. layers[1].size = type == KNIGHTS_LANDING ? 1 : MAX_DIMMS;
  2790. layers[1].is_virt_csrow = true;
  2791. mci = edac_mc_alloc(sbridge_dev->mc, ARRAY_SIZE(layers), layers,
  2792. sizeof(*pvt));
  2793. if (unlikely(!mci))
  2794. return -ENOMEM;
  2795. edac_dbg(0, "MC: mci = %p, dev = %p\n",
  2796. mci, &pdev->dev);
  2797. pvt = mci->pvt_info;
  2798. memset(pvt, 0, sizeof(*pvt));
  2799. /* Associate sbridge_dev and mci for future usage */
  2800. pvt->sbridge_dev = sbridge_dev;
  2801. sbridge_dev->mci = mci;
  2802. mci->mtype_cap = type == KNIGHTS_LANDING ?
  2803. MEM_FLAG_DDR4 : MEM_FLAG_DDR3;
  2804. mci->edac_ctl_cap = EDAC_FLAG_NONE;
  2805. mci->edac_cap = EDAC_FLAG_NONE;
  2806. mci->mod_name = "sbridge_edac.c";
  2807. mci->mod_ver = SBRIDGE_REVISION;
  2808. mci->dev_name = pci_name(pdev);
  2809. mci->ctl_page_to_phys = NULL;
  2810. /* Set the function pointer to an actual operation function */
  2811. mci->edac_check = sbridge_check_error;
  2812. pvt->info.type = type;
  2813. switch (type) {
  2814. case IVY_BRIDGE:
  2815. pvt->info.rankcfgr = IB_RANK_CFG_A;
  2816. pvt->info.get_tolm = ibridge_get_tolm;
  2817. pvt->info.get_tohm = ibridge_get_tohm;
  2818. pvt->info.dram_rule = ibridge_dram_rule;
  2819. pvt->info.get_memory_type = get_memory_type;
  2820. pvt->info.get_node_id = get_node_id;
  2821. pvt->info.rir_limit = rir_limit;
  2822. pvt->info.sad_limit = sad_limit;
  2823. pvt->info.interleave_mode = interleave_mode;
  2824. pvt->info.show_interleave_mode = show_interleave_mode;
  2825. pvt->info.dram_attr = dram_attr;
  2826. pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
  2827. pvt->info.interleave_list = ibridge_interleave_list;
  2828. pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
  2829. pvt->info.interleave_pkg = ibridge_interleave_pkg;
  2830. pvt->info.get_width = ibridge_get_width;
  2831. mci->ctl_name = kasprintf(GFP_KERNEL, "Ivy Bridge Socket#%d", mci->mc_idx);
  2832. /* Store pci devices at mci for faster access */
  2833. rc = ibridge_mci_bind_devs(mci, sbridge_dev);
  2834. if (unlikely(rc < 0))
  2835. goto fail0;
  2836. break;
  2837. case SANDY_BRIDGE:
  2838. pvt->info.rankcfgr = SB_RANK_CFG_A;
  2839. pvt->info.get_tolm = sbridge_get_tolm;
  2840. pvt->info.get_tohm = sbridge_get_tohm;
  2841. pvt->info.dram_rule = sbridge_dram_rule;
  2842. pvt->info.get_memory_type = get_memory_type;
  2843. pvt->info.get_node_id = get_node_id;
  2844. pvt->info.rir_limit = rir_limit;
  2845. pvt->info.sad_limit = sad_limit;
  2846. pvt->info.interleave_mode = interleave_mode;
  2847. pvt->info.show_interleave_mode = show_interleave_mode;
  2848. pvt->info.dram_attr = dram_attr;
  2849. pvt->info.max_sad = ARRAY_SIZE(sbridge_dram_rule);
  2850. pvt->info.interleave_list = sbridge_interleave_list;
  2851. pvt->info.max_interleave = ARRAY_SIZE(sbridge_interleave_list);
  2852. pvt->info.interleave_pkg = sbridge_interleave_pkg;
  2853. pvt->info.get_width = sbridge_get_width;
  2854. mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge Socket#%d", mci->mc_idx);
  2855. /* Store pci devices at mci for faster access */
  2856. rc = sbridge_mci_bind_devs(mci, sbridge_dev);
  2857. if (unlikely(rc < 0))
  2858. goto fail0;
  2859. break;
  2860. case HASWELL:
  2861. /* rankcfgr isn't used */
  2862. pvt->info.get_tolm = haswell_get_tolm;
  2863. pvt->info.get_tohm = haswell_get_tohm;
  2864. pvt->info.dram_rule = ibridge_dram_rule;
  2865. pvt->info.get_memory_type = haswell_get_memory_type;
  2866. pvt->info.get_node_id = haswell_get_node_id;
  2867. pvt->info.rir_limit = haswell_rir_limit;
  2868. pvt->info.sad_limit = sad_limit;
  2869. pvt->info.interleave_mode = interleave_mode;
  2870. pvt->info.show_interleave_mode = show_interleave_mode;
  2871. pvt->info.dram_attr = dram_attr;
  2872. pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
  2873. pvt->info.interleave_list = ibridge_interleave_list;
  2874. pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
  2875. pvt->info.interleave_pkg = ibridge_interleave_pkg;
  2876. pvt->info.get_width = ibridge_get_width;
  2877. mci->ctl_name = kasprintf(GFP_KERNEL, "Haswell Socket#%d", mci->mc_idx);
  2878. /* Store pci devices at mci for faster access */
  2879. rc = haswell_mci_bind_devs(mci, sbridge_dev);
  2880. if (unlikely(rc < 0))
  2881. goto fail0;
  2882. break;
  2883. case BROADWELL:
  2884. /* rankcfgr isn't used */
  2885. pvt->info.get_tolm = haswell_get_tolm;
  2886. pvt->info.get_tohm = haswell_get_tohm;
  2887. pvt->info.dram_rule = ibridge_dram_rule;
  2888. pvt->info.get_memory_type = haswell_get_memory_type;
  2889. pvt->info.get_node_id = haswell_get_node_id;
  2890. pvt->info.rir_limit = haswell_rir_limit;
  2891. pvt->info.sad_limit = sad_limit;
  2892. pvt->info.interleave_mode = interleave_mode;
  2893. pvt->info.show_interleave_mode = show_interleave_mode;
  2894. pvt->info.dram_attr = dram_attr;
  2895. pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
  2896. pvt->info.interleave_list = ibridge_interleave_list;
  2897. pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
  2898. pvt->info.interleave_pkg = ibridge_interleave_pkg;
  2899. pvt->info.get_width = broadwell_get_width;
  2900. mci->ctl_name = kasprintf(GFP_KERNEL, "Broadwell Socket#%d", mci->mc_idx);
  2901. /* Store pci devices at mci for faster access */
  2902. rc = broadwell_mci_bind_devs(mci, sbridge_dev);
  2903. if (unlikely(rc < 0))
  2904. goto fail0;
  2905. break;
  2906. case KNIGHTS_LANDING:
  2907. /* pvt->info.rankcfgr == ??? */
  2908. pvt->info.get_tolm = knl_get_tolm;
  2909. pvt->info.get_tohm = knl_get_tohm;
  2910. pvt->info.dram_rule = knl_dram_rule;
  2911. pvt->info.get_memory_type = knl_get_memory_type;
  2912. pvt->info.get_node_id = knl_get_node_id;
  2913. pvt->info.rir_limit = NULL;
  2914. pvt->info.sad_limit = knl_sad_limit;
  2915. pvt->info.interleave_mode = knl_interleave_mode;
  2916. pvt->info.show_interleave_mode = knl_show_interleave_mode;
  2917. pvt->info.dram_attr = dram_attr_knl;
  2918. pvt->info.max_sad = ARRAY_SIZE(knl_dram_rule);
  2919. pvt->info.interleave_list = knl_interleave_list;
  2920. pvt->info.max_interleave = ARRAY_SIZE(knl_interleave_list);
  2921. pvt->info.interleave_pkg = ibridge_interleave_pkg;
  2922. pvt->info.get_width = knl_get_width;
  2923. mci->ctl_name = kasprintf(GFP_KERNEL,
  2924. "Knights Landing Socket#%d", mci->mc_idx);
  2925. rc = knl_mci_bind_devs(mci, sbridge_dev);
  2926. if (unlikely(rc < 0))
  2927. goto fail0;
  2928. break;
  2929. }
  2930. /* Get dimm basic config and the memory layout */
  2931. get_dimm_config(mci);
  2932. get_memory_layout(mci);
  2933. /* record ptr to the generic device */
  2934. mci->pdev = &pdev->dev;
  2935. /* add this new MC control structure to EDAC's list of MCs */
  2936. if (unlikely(edac_mc_add_mc(mci))) {
  2937. edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
  2938. rc = -EINVAL;
  2939. goto fail0;
  2940. }
  2941. return 0;
  2942. fail0:
  2943. kfree(mci->ctl_name);
  2944. edac_mc_free(mci);
  2945. sbridge_dev->mci = NULL;
  2946. return rc;
  2947. }
  2948. /*
  2949. * sbridge_probe Probe for ONE instance of device to see if it is
  2950. * present.
  2951. * return:
  2952. * 0 for FOUND a device
  2953. * < 0 for error code
  2954. */
  2955. static int sbridge_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  2956. {
  2957. int rc = -ENODEV;
  2958. u8 mc, num_mc = 0;
  2959. struct sbridge_dev *sbridge_dev;
  2960. enum type type = SANDY_BRIDGE;
  2961. /* get the pci devices we want to reserve for our use */
  2962. mutex_lock(&sbridge_edac_lock);
  2963. /*
  2964. * All memory controllers are allocated at the first pass.
  2965. */
  2966. if (unlikely(probed >= 1)) {
  2967. mutex_unlock(&sbridge_edac_lock);
  2968. return -ENODEV;
  2969. }
  2970. probed++;
  2971. switch (pdev->device) {
  2972. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA:
  2973. rc = sbridge_get_all_devices(&num_mc,
  2974. pci_dev_descr_ibridge_table);
  2975. type = IVY_BRIDGE;
  2976. break;
  2977. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0:
  2978. rc = sbridge_get_all_devices(&num_mc,
  2979. pci_dev_descr_sbridge_table);
  2980. type = SANDY_BRIDGE;
  2981. break;
  2982. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0:
  2983. rc = sbridge_get_all_devices(&num_mc,
  2984. pci_dev_descr_haswell_table);
  2985. type = HASWELL;
  2986. break;
  2987. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0:
  2988. rc = sbridge_get_all_devices(&num_mc,
  2989. pci_dev_descr_broadwell_table);
  2990. type = BROADWELL;
  2991. break;
  2992. case PCI_DEVICE_ID_INTEL_KNL_IMC_SAD0:
  2993. rc = sbridge_get_all_devices_knl(&num_mc,
  2994. pci_dev_descr_knl_table);
  2995. type = KNIGHTS_LANDING;
  2996. break;
  2997. }
  2998. if (unlikely(rc < 0)) {
  2999. edac_dbg(0, "couldn't get all devices for 0x%x\n", pdev->device);
  3000. goto fail0;
  3001. }
  3002. mc = 0;
  3003. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  3004. edac_dbg(0, "Registering MC#%d (%d of %d)\n",
  3005. mc, mc + 1, num_mc);
  3006. sbridge_dev->mc = mc++;
  3007. rc = sbridge_register_mci(sbridge_dev, type);
  3008. if (unlikely(rc < 0))
  3009. goto fail1;
  3010. }
  3011. sbridge_printk(KERN_INFO, "%s\n", SBRIDGE_REVISION);
  3012. mutex_unlock(&sbridge_edac_lock);
  3013. return 0;
  3014. fail1:
  3015. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
  3016. sbridge_unregister_mci(sbridge_dev);
  3017. sbridge_put_all_devices();
  3018. fail0:
  3019. mutex_unlock(&sbridge_edac_lock);
  3020. return rc;
  3021. }
  3022. /*
  3023. * sbridge_remove destructor for one instance of device
  3024. *
  3025. */
  3026. static void sbridge_remove(struct pci_dev *pdev)
  3027. {
  3028. struct sbridge_dev *sbridge_dev;
  3029. edac_dbg(0, "\n");
  3030. /*
  3031. * we have a trouble here: pdev value for removal will be wrong, since
  3032. * it will point to the X58 register used to detect that the machine
  3033. * is a Nehalem or upper design. However, due to the way several PCI
  3034. * devices are grouped together to provide MC functionality, we need
  3035. * to use a different method for releasing the devices
  3036. */
  3037. mutex_lock(&sbridge_edac_lock);
  3038. if (unlikely(!probed)) {
  3039. mutex_unlock(&sbridge_edac_lock);
  3040. return;
  3041. }
  3042. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
  3043. sbridge_unregister_mci(sbridge_dev);
  3044. /* Release PCI resources */
  3045. sbridge_put_all_devices();
  3046. probed--;
  3047. mutex_unlock(&sbridge_edac_lock);
  3048. }
  3049. MODULE_DEVICE_TABLE(pci, sbridge_pci_tbl);
  3050. /*
  3051. * sbridge_driver pci_driver structure for this module
  3052. *
  3053. */
  3054. static struct pci_driver sbridge_driver = {
  3055. .name = "sbridge_edac",
  3056. .probe = sbridge_probe,
  3057. .remove = sbridge_remove,
  3058. .id_table = sbridge_pci_tbl,
  3059. };
  3060. /*
  3061. * sbridge_init Module entry function
  3062. * Try to initialize this module for its devices
  3063. */
  3064. static int __init sbridge_init(void)
  3065. {
  3066. int pci_rc;
  3067. edac_dbg(2, "\n");
  3068. /* Ensure that the OPSTATE is set correctly for POLL or NMI */
  3069. opstate_init();
  3070. pci_rc = pci_register_driver(&sbridge_driver);
  3071. if (pci_rc >= 0) {
  3072. mce_register_decode_chain(&sbridge_mce_dec);
  3073. if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
  3074. sbridge_printk(KERN_WARNING, "Loading driver, error reporting disabled.\n");
  3075. return 0;
  3076. }
  3077. sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
  3078. pci_rc);
  3079. return pci_rc;
  3080. }
  3081. /*
  3082. * sbridge_exit() Module exit function
  3083. * Unregister the driver
  3084. */
  3085. static void __exit sbridge_exit(void)
  3086. {
  3087. edac_dbg(2, "\n");
  3088. pci_unregister_driver(&sbridge_driver);
  3089. mce_unregister_decode_chain(&sbridge_mce_dec);
  3090. }
  3091. module_init(sbridge_init);
  3092. module_exit(sbridge_exit);
  3093. module_param(edac_op_state, int, 0444);
  3094. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
  3095. MODULE_LICENSE("GPL");
  3096. MODULE_AUTHOR("Mauro Carvalho Chehab");
  3097. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  3098. MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge and Ivy Bridge memory controllers - "
  3099. SBRIDGE_REVISION);