sb_edac.c 91 KB

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