sb_edac.c 92 KB

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