sb_edac.c 92 KB

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