sb_edac.c 91 KB

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