amd64_edac.c 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/ras.h>
  3. #include "amd64_edac.h"
  4. #include <asm/amd_nb.h>
  5. static struct edac_pci_ctl_info *pci_ctl;
  6. /*
  7. * Set by command line parameter. If BIOS has enabled the ECC, this override is
  8. * cleared to prevent re-enabling the hardware by this driver.
  9. */
  10. static int ecc_enable_override;
  11. module_param(ecc_enable_override, int, 0644);
  12. static struct msr __percpu *msrs;
  13. static inline u32 get_umc_reg(struct amd64_pvt *pvt, u32 reg)
  14. {
  15. if (!pvt->flags.zn_regs_v2)
  16. return reg;
  17. switch (reg) {
  18. case UMCCH_ADDR_MASK_SEC: return UMCCH_ADDR_MASK_SEC_DDR5;
  19. case UMCCH_DIMM_CFG: return UMCCH_DIMM_CFG_DDR5;
  20. }
  21. WARN_ONCE(1, "%s: unknown register 0x%x", __func__, reg);
  22. return 0;
  23. }
  24. /* Per-node stuff */
  25. static struct ecc_settings **ecc_stngs;
  26. /* Device for the PCI component */
  27. static struct device *pci_ctl_dev;
  28. /*
  29. * Valid scrub rates for the K8 hardware memory scrubber. We map the scrubbing
  30. * bandwidth to a valid bit pattern. The 'set' operation finds the 'matching-
  31. * or higher value'.
  32. *
  33. *FIXME: Produce a better mapping/linearisation.
  34. */
  35. static const struct scrubrate {
  36. u32 scrubval; /* bit pattern for scrub rate */
  37. u32 bandwidth; /* bandwidth consumed (bytes/sec) */
  38. } scrubrates[] = {
  39. { 0x01, 1600000000UL},
  40. { 0x02, 800000000UL},
  41. { 0x03, 400000000UL},
  42. { 0x04, 200000000UL},
  43. { 0x05, 100000000UL},
  44. { 0x06, 50000000UL},
  45. { 0x07, 25000000UL},
  46. { 0x08, 12284069UL},
  47. { 0x09, 6274509UL},
  48. { 0x0A, 3121951UL},
  49. { 0x0B, 1560975UL},
  50. { 0x0C, 781440UL},
  51. { 0x0D, 390720UL},
  52. { 0x0E, 195300UL},
  53. { 0x0F, 97650UL},
  54. { 0x10, 48854UL},
  55. { 0x11, 24427UL},
  56. { 0x12, 12213UL},
  57. { 0x13, 6101UL},
  58. { 0x14, 3051UL},
  59. { 0x15, 1523UL},
  60. { 0x16, 761UL},
  61. { 0x00, 0UL}, /* scrubbing off */
  62. };
  63. int __amd64_read_pci_cfg_dword(struct pci_dev *pdev, int offset,
  64. u32 *val, const char *func)
  65. {
  66. int err = 0;
  67. err = pci_read_config_dword(pdev, offset, val);
  68. if (err)
  69. amd64_warn("%s: error reading F%dx%03x.\n",
  70. func, PCI_FUNC(pdev->devfn), offset);
  71. return pcibios_err_to_errno(err);
  72. }
  73. int __amd64_write_pci_cfg_dword(struct pci_dev *pdev, int offset,
  74. u32 val, const char *func)
  75. {
  76. int err = 0;
  77. err = pci_write_config_dword(pdev, offset, val);
  78. if (err)
  79. amd64_warn("%s: error writing to F%dx%03x.\n",
  80. func, PCI_FUNC(pdev->devfn), offset);
  81. return pcibios_err_to_errno(err);
  82. }
  83. /*
  84. * Select DCT to which PCI cfg accesses are routed
  85. */
  86. static void f15h_select_dct(struct amd64_pvt *pvt, u8 dct)
  87. {
  88. u32 reg = 0;
  89. amd64_read_pci_cfg(pvt->F1, DCT_CFG_SEL, &reg);
  90. reg &= (pvt->model == 0x30) ? ~3 : ~1;
  91. reg |= dct;
  92. amd64_write_pci_cfg(pvt->F1, DCT_CFG_SEL, reg);
  93. }
  94. /*
  95. *
  96. * Depending on the family, F2 DCT reads need special handling:
  97. *
  98. * K8: has a single DCT only and no address offsets >= 0x100
  99. *
  100. * F10h: each DCT has its own set of regs
  101. * DCT0 -> F2x040..
  102. * DCT1 -> F2x140..
  103. *
  104. * F16h: has only 1 DCT
  105. *
  106. * F15h: we select which DCT we access using F1x10C[DctCfgSel]
  107. */
  108. static inline int amd64_read_dct_pci_cfg(struct amd64_pvt *pvt, u8 dct,
  109. int offset, u32 *val)
  110. {
  111. switch (pvt->fam) {
  112. case 0xf:
  113. if (dct || offset >= 0x100)
  114. return -EINVAL;
  115. break;
  116. case 0x10:
  117. if (dct) {
  118. /*
  119. * Note: If ganging is enabled, barring the regs
  120. * F2x[1,0]98 and F2x[1,0]9C; reads reads to F2x1xx
  121. * return 0. (cf. Section 2.8.1 F10h BKDG)
  122. */
  123. if (dct_ganging_enabled(pvt))
  124. return 0;
  125. offset += 0x100;
  126. }
  127. break;
  128. case 0x15:
  129. /*
  130. * F15h: F2x1xx addresses do not map explicitly to DCT1.
  131. * We should select which DCT we access using F1x10C[DctCfgSel]
  132. */
  133. dct = (dct && pvt->model == 0x30) ? 3 : dct;
  134. f15h_select_dct(pvt, dct);
  135. break;
  136. case 0x16:
  137. if (dct)
  138. return -EINVAL;
  139. break;
  140. default:
  141. break;
  142. }
  143. return amd64_read_pci_cfg(pvt->F2, offset, val);
  144. }
  145. /*
  146. * Memory scrubber control interface. For K8, memory scrubbing is handled by
  147. * hardware and can involve L2 cache, dcache as well as the main memory. With
  148. * F10, this is extended to L3 cache scrubbing on CPU models sporting that
  149. * functionality.
  150. *
  151. * This causes the "units" for the scrubbing speed to vary from 64 byte blocks
  152. * (dram) over to cache lines. This is nasty, so we will use bandwidth in
  153. * bytes/sec for the setting.
  154. *
  155. * Currently, we only do dram scrubbing. If the scrubbing is done in software on
  156. * other archs, we might not have access to the caches directly.
  157. */
  158. /*
  159. * Scan the scrub rate mapping table for a close or matching bandwidth value to
  160. * issue. If requested is too big, then use last maximum value found.
  161. */
  162. static int __set_scrub_rate(struct amd64_pvt *pvt, u32 new_bw, u32 min_rate)
  163. {
  164. u32 scrubval;
  165. int i;
  166. /*
  167. * map the configured rate (new_bw) to a value specific to the AMD64
  168. * memory controller and apply to register. Search for the first
  169. * bandwidth entry that is greater or equal than the setting requested
  170. * and program that. If at last entry, turn off DRAM scrubbing.
  171. *
  172. * If no suitable bandwidth is found, turn off DRAM scrubbing entirely
  173. * by falling back to the last element in scrubrates[].
  174. */
  175. for (i = 0; i < ARRAY_SIZE(scrubrates) - 1; i++) {
  176. /*
  177. * skip scrub rates which aren't recommended
  178. * (see F10 BKDG, F3x58)
  179. */
  180. if (scrubrates[i].scrubval < min_rate)
  181. continue;
  182. if (scrubrates[i].bandwidth <= new_bw)
  183. break;
  184. }
  185. scrubval = scrubrates[i].scrubval;
  186. if (pvt->fam == 0x15 && pvt->model == 0x60) {
  187. f15h_select_dct(pvt, 0);
  188. pci_write_bits32(pvt->F2, F15H_M60H_SCRCTRL, scrubval, 0x001F);
  189. f15h_select_dct(pvt, 1);
  190. pci_write_bits32(pvt->F2, F15H_M60H_SCRCTRL, scrubval, 0x001F);
  191. } else {
  192. pci_write_bits32(pvt->F3, SCRCTRL, scrubval, 0x001F);
  193. }
  194. if (scrubval)
  195. return scrubrates[i].bandwidth;
  196. return 0;
  197. }
  198. static int set_scrub_rate(struct mem_ctl_info *mci, u32 bw)
  199. {
  200. struct amd64_pvt *pvt = mci->pvt_info;
  201. u32 min_scrubrate = 0x5;
  202. if (pvt->fam == 0xf)
  203. min_scrubrate = 0x0;
  204. if (pvt->fam == 0x15) {
  205. /* Erratum #505 */
  206. if (pvt->model < 0x10)
  207. f15h_select_dct(pvt, 0);
  208. if (pvt->model == 0x60)
  209. min_scrubrate = 0x6;
  210. }
  211. return __set_scrub_rate(pvt, bw, min_scrubrate);
  212. }
  213. static int get_scrub_rate(struct mem_ctl_info *mci)
  214. {
  215. struct amd64_pvt *pvt = mci->pvt_info;
  216. int i, retval = -EINVAL;
  217. u32 scrubval = 0;
  218. if (pvt->fam == 0x15) {
  219. /* Erratum #505 */
  220. if (pvt->model < 0x10)
  221. f15h_select_dct(pvt, 0);
  222. if (pvt->model == 0x60)
  223. amd64_read_pci_cfg(pvt->F2, F15H_M60H_SCRCTRL, &scrubval);
  224. else
  225. amd64_read_pci_cfg(pvt->F3, SCRCTRL, &scrubval);
  226. } else {
  227. amd64_read_pci_cfg(pvt->F3, SCRCTRL, &scrubval);
  228. }
  229. scrubval = scrubval & 0x001F;
  230. for (i = 0; i < ARRAY_SIZE(scrubrates); i++) {
  231. if (scrubrates[i].scrubval == scrubval) {
  232. retval = scrubrates[i].bandwidth;
  233. break;
  234. }
  235. }
  236. return retval;
  237. }
  238. /*
  239. * returns true if the SysAddr given by sys_addr matches the
  240. * DRAM base/limit associated with node_id
  241. */
  242. static bool base_limit_match(struct amd64_pvt *pvt, u64 sys_addr, u8 nid)
  243. {
  244. u64 addr;
  245. /* The K8 treats this as a 40-bit value. However, bits 63-40 will be
  246. * all ones if the most significant implemented address bit is 1.
  247. * Here we discard bits 63-40. See section 3.4.2 of AMD publication
  248. * 24592: AMD x86-64 Architecture Programmer's Manual Volume 1
  249. * Application Programming.
  250. */
  251. addr = sys_addr & 0x000000ffffffffffull;
  252. return ((addr >= get_dram_base(pvt, nid)) &&
  253. (addr <= get_dram_limit(pvt, nid)));
  254. }
  255. /*
  256. * Attempt to map a SysAddr to a node. On success, return a pointer to the
  257. * mem_ctl_info structure for the node that the SysAddr maps to.
  258. *
  259. * On failure, return NULL.
  260. */
  261. static struct mem_ctl_info *find_mc_by_sys_addr(struct mem_ctl_info *mci,
  262. u64 sys_addr)
  263. {
  264. struct amd64_pvt *pvt;
  265. u8 node_id;
  266. u32 intlv_en, bits;
  267. /*
  268. * Here we use the DRAM Base (section 3.4.4.1) and DRAM Limit (section
  269. * 3.4.4.2) registers to map the SysAddr to a node ID.
  270. */
  271. pvt = mci->pvt_info;
  272. /*
  273. * The value of this field should be the same for all DRAM Base
  274. * registers. Therefore we arbitrarily choose to read it from the
  275. * register for node 0.
  276. */
  277. intlv_en = dram_intlv_en(pvt, 0);
  278. if (intlv_en == 0) {
  279. for (node_id = 0; node_id < DRAM_RANGES; node_id++) {
  280. if (base_limit_match(pvt, sys_addr, node_id))
  281. goto found;
  282. }
  283. goto err_no_match;
  284. }
  285. if (unlikely((intlv_en != 0x01) &&
  286. (intlv_en != 0x03) &&
  287. (intlv_en != 0x07))) {
  288. amd64_warn("DRAM Base[IntlvEn] junk value: 0x%x, BIOS bug?\n", intlv_en);
  289. return NULL;
  290. }
  291. bits = (((u32) sys_addr) >> 12) & intlv_en;
  292. for (node_id = 0; ; ) {
  293. if ((dram_intlv_sel(pvt, node_id) & intlv_en) == bits)
  294. break; /* intlv_sel field matches */
  295. if (++node_id >= DRAM_RANGES)
  296. goto err_no_match;
  297. }
  298. /* sanity test for sys_addr */
  299. if (unlikely(!base_limit_match(pvt, sys_addr, node_id))) {
  300. amd64_warn("%s: sys_addr 0x%llx falls outside base/limit address"
  301. "range for node %d with node interleaving enabled.\n",
  302. __func__, sys_addr, node_id);
  303. return NULL;
  304. }
  305. found:
  306. return edac_mc_find((int)node_id);
  307. err_no_match:
  308. edac_dbg(2, "sys_addr 0x%lx doesn't match any node\n",
  309. (unsigned long)sys_addr);
  310. return NULL;
  311. }
  312. /*
  313. * compute the CS base address of the @csrow on the DRAM controller @dct.
  314. * For details see F2x[5C:40] in the processor's BKDG
  315. */
  316. static void get_cs_base_and_mask(struct amd64_pvt *pvt, int csrow, u8 dct,
  317. u64 *base, u64 *mask)
  318. {
  319. u64 csbase, csmask, base_bits, mask_bits;
  320. u8 addr_shift;
  321. if (pvt->fam == 0xf && pvt->ext_model < K8_REV_F) {
  322. csbase = pvt->csels[dct].csbases[csrow];
  323. csmask = pvt->csels[dct].csmasks[csrow];
  324. base_bits = GENMASK_ULL(31, 21) | GENMASK_ULL(15, 9);
  325. mask_bits = GENMASK_ULL(29, 21) | GENMASK_ULL(15, 9);
  326. addr_shift = 4;
  327. /*
  328. * F16h and F15h, models 30h and later need two addr_shift values:
  329. * 8 for high and 6 for low (cf. F16h BKDG).
  330. */
  331. } else if (pvt->fam == 0x16 ||
  332. (pvt->fam == 0x15 && pvt->model >= 0x30)) {
  333. csbase = pvt->csels[dct].csbases[csrow];
  334. csmask = pvt->csels[dct].csmasks[csrow >> 1];
  335. *base = (csbase & GENMASK_ULL(15, 5)) << 6;
  336. *base |= (csbase & GENMASK_ULL(30, 19)) << 8;
  337. *mask = ~0ULL;
  338. /* poke holes for the csmask */
  339. *mask &= ~((GENMASK_ULL(15, 5) << 6) |
  340. (GENMASK_ULL(30, 19) << 8));
  341. *mask |= (csmask & GENMASK_ULL(15, 5)) << 6;
  342. *mask |= (csmask & GENMASK_ULL(30, 19)) << 8;
  343. return;
  344. } else {
  345. csbase = pvt->csels[dct].csbases[csrow];
  346. csmask = pvt->csels[dct].csmasks[csrow >> 1];
  347. addr_shift = 8;
  348. if (pvt->fam == 0x15)
  349. base_bits = mask_bits =
  350. GENMASK_ULL(30,19) | GENMASK_ULL(13,5);
  351. else
  352. base_bits = mask_bits =
  353. GENMASK_ULL(28,19) | GENMASK_ULL(13,5);
  354. }
  355. *base = (csbase & base_bits) << addr_shift;
  356. *mask = ~0ULL;
  357. /* poke holes for the csmask */
  358. *mask &= ~(mask_bits << addr_shift);
  359. /* OR them in */
  360. *mask |= (csmask & mask_bits) << addr_shift;
  361. }
  362. #define for_each_chip_select(i, dct, pvt) \
  363. for (i = 0; i < pvt->csels[dct].b_cnt; i++)
  364. #define chip_select_base(i, dct, pvt) \
  365. pvt->csels[dct].csbases[i]
  366. #define for_each_chip_select_mask(i, dct, pvt) \
  367. for (i = 0; i < pvt->csels[dct].m_cnt; i++)
  368. #define for_each_umc(i) \
  369. for (i = 0; i < pvt->max_mcs; i++)
  370. /*
  371. * @input_addr is an InputAddr associated with the node given by mci. Return the
  372. * csrow that input_addr maps to, or -1 on failure (no csrow claims input_addr).
  373. */
  374. static int input_addr_to_csrow(struct mem_ctl_info *mci, u64 input_addr)
  375. {
  376. struct amd64_pvt *pvt;
  377. int csrow;
  378. u64 base, mask;
  379. pvt = mci->pvt_info;
  380. for_each_chip_select(csrow, 0, pvt) {
  381. if (!csrow_enabled(csrow, 0, pvt))
  382. continue;
  383. get_cs_base_and_mask(pvt, csrow, 0, &base, &mask);
  384. mask = ~mask;
  385. if ((input_addr & mask) == (base & mask)) {
  386. edac_dbg(2, "InputAddr 0x%lx matches csrow %d (node %d)\n",
  387. (unsigned long)input_addr, csrow,
  388. pvt->mc_node_id);
  389. return csrow;
  390. }
  391. }
  392. edac_dbg(2, "no matching csrow for InputAddr 0x%lx (MC node %d)\n",
  393. (unsigned long)input_addr, pvt->mc_node_id);
  394. return -1;
  395. }
  396. /*
  397. * Obtain info from the DRAM Hole Address Register (section 3.4.8, pub #26094)
  398. * for the node represented by mci. Info is passed back in *hole_base,
  399. * *hole_offset, and *hole_size. Function returns 0 if info is valid or 1 if
  400. * info is invalid. Info may be invalid for either of the following reasons:
  401. *
  402. * - The revision of the node is not E or greater. In this case, the DRAM Hole
  403. * Address Register does not exist.
  404. *
  405. * - The DramHoleValid bit is cleared in the DRAM Hole Address Register,
  406. * indicating that its contents are not valid.
  407. *
  408. * The values passed back in *hole_base, *hole_offset, and *hole_size are
  409. * complete 32-bit values despite the fact that the bitfields in the DHAR
  410. * only represent bits 31-24 of the base and offset values.
  411. */
  412. static int get_dram_hole_info(struct mem_ctl_info *mci, u64 *hole_base,
  413. u64 *hole_offset, u64 *hole_size)
  414. {
  415. struct amd64_pvt *pvt = mci->pvt_info;
  416. /* only revE and later have the DRAM Hole Address Register */
  417. if (pvt->fam == 0xf && pvt->ext_model < K8_REV_E) {
  418. edac_dbg(1, " revision %d for node %d does not support DHAR\n",
  419. pvt->ext_model, pvt->mc_node_id);
  420. return 1;
  421. }
  422. /* valid for Fam10h and above */
  423. if (pvt->fam >= 0x10 && !dhar_mem_hoist_valid(pvt)) {
  424. edac_dbg(1, " Dram Memory Hoisting is DISABLED on this system\n");
  425. return 1;
  426. }
  427. if (!dhar_valid(pvt)) {
  428. edac_dbg(1, " Dram Memory Hoisting is DISABLED on this node %d\n",
  429. pvt->mc_node_id);
  430. return 1;
  431. }
  432. /* This node has Memory Hoisting */
  433. /* +------------------+--------------------+--------------------+-----
  434. * | memory | DRAM hole | relocated |
  435. * | [0, (x - 1)] | [x, 0xffffffff] | addresses from |
  436. * | | | DRAM hole |
  437. * | | | [0x100000000, |
  438. * | | | (0x100000000+ |
  439. * | | | (0xffffffff-x))] |
  440. * +------------------+--------------------+--------------------+-----
  441. *
  442. * Above is a diagram of physical memory showing the DRAM hole and the
  443. * relocated addresses from the DRAM hole. As shown, the DRAM hole
  444. * starts at address x (the base address) and extends through address
  445. * 0xffffffff. The DRAM Hole Address Register (DHAR) relocates the
  446. * addresses in the hole so that they start at 0x100000000.
  447. */
  448. *hole_base = dhar_base(pvt);
  449. *hole_size = (1ULL << 32) - *hole_base;
  450. *hole_offset = (pvt->fam > 0xf) ? f10_dhar_offset(pvt)
  451. : k8_dhar_offset(pvt);
  452. edac_dbg(1, " DHAR info for node %d base 0x%lx offset 0x%lx size 0x%lx\n",
  453. pvt->mc_node_id, (unsigned long)*hole_base,
  454. (unsigned long)*hole_offset, (unsigned long)*hole_size);
  455. return 0;
  456. }
  457. #ifdef CONFIG_EDAC_DEBUG
  458. #define EDAC_DCT_ATTR_SHOW(reg) \
  459. static ssize_t reg##_show(struct device *dev, \
  460. struct device_attribute *mattr, char *data) \
  461. { \
  462. struct mem_ctl_info *mci = to_mci(dev); \
  463. struct amd64_pvt *pvt = mci->pvt_info; \
  464. \
  465. return sprintf(data, "0x%016llx\n", (u64)pvt->reg); \
  466. }
  467. EDAC_DCT_ATTR_SHOW(dhar);
  468. EDAC_DCT_ATTR_SHOW(dbam0);
  469. EDAC_DCT_ATTR_SHOW(top_mem);
  470. EDAC_DCT_ATTR_SHOW(top_mem2);
  471. static ssize_t dram_hole_show(struct device *dev, struct device_attribute *mattr,
  472. char *data)
  473. {
  474. struct mem_ctl_info *mci = to_mci(dev);
  475. u64 hole_base = 0;
  476. u64 hole_offset = 0;
  477. u64 hole_size = 0;
  478. get_dram_hole_info(mci, &hole_base, &hole_offset, &hole_size);
  479. return sprintf(data, "%llx %llx %llx\n", hole_base, hole_offset,
  480. hole_size);
  481. }
  482. /*
  483. * update NUM_DBG_ATTRS in case you add new members
  484. */
  485. static DEVICE_ATTR(dhar, S_IRUGO, dhar_show, NULL);
  486. static DEVICE_ATTR(dbam, S_IRUGO, dbam0_show, NULL);
  487. static DEVICE_ATTR(topmem, S_IRUGO, top_mem_show, NULL);
  488. static DEVICE_ATTR(topmem2, S_IRUGO, top_mem2_show, NULL);
  489. static DEVICE_ATTR_RO(dram_hole);
  490. static struct attribute *dbg_attrs[] = {
  491. &dev_attr_dhar.attr,
  492. &dev_attr_dbam.attr,
  493. &dev_attr_topmem.attr,
  494. &dev_attr_topmem2.attr,
  495. &dev_attr_dram_hole.attr,
  496. NULL
  497. };
  498. static const struct attribute_group dbg_group = {
  499. .attrs = dbg_attrs,
  500. };
  501. static ssize_t inject_section_show(struct device *dev,
  502. struct device_attribute *mattr, char *buf)
  503. {
  504. struct mem_ctl_info *mci = to_mci(dev);
  505. struct amd64_pvt *pvt = mci->pvt_info;
  506. return sprintf(buf, "0x%x\n", pvt->injection.section);
  507. }
  508. /*
  509. * store error injection section value which refers to one of 4 16-byte sections
  510. * within a 64-byte cacheline
  511. *
  512. * range: 0..3
  513. */
  514. static ssize_t inject_section_store(struct device *dev,
  515. struct device_attribute *mattr,
  516. const char *data, size_t count)
  517. {
  518. struct mem_ctl_info *mci = to_mci(dev);
  519. struct amd64_pvt *pvt = mci->pvt_info;
  520. unsigned long value;
  521. int ret;
  522. ret = kstrtoul(data, 10, &value);
  523. if (ret < 0)
  524. return ret;
  525. if (value > 3) {
  526. amd64_warn("%s: invalid section 0x%lx\n", __func__, value);
  527. return -EINVAL;
  528. }
  529. pvt->injection.section = (u32) value;
  530. return count;
  531. }
  532. static ssize_t inject_word_show(struct device *dev,
  533. struct device_attribute *mattr, char *buf)
  534. {
  535. struct mem_ctl_info *mci = to_mci(dev);
  536. struct amd64_pvt *pvt = mci->pvt_info;
  537. return sprintf(buf, "0x%x\n", pvt->injection.word);
  538. }
  539. /*
  540. * store error injection word value which refers to one of 9 16-bit word of the
  541. * 16-byte (128-bit + ECC bits) section
  542. *
  543. * range: 0..8
  544. */
  545. static ssize_t inject_word_store(struct device *dev,
  546. struct device_attribute *mattr,
  547. const char *data, size_t count)
  548. {
  549. struct mem_ctl_info *mci = to_mci(dev);
  550. struct amd64_pvt *pvt = mci->pvt_info;
  551. unsigned long value;
  552. int ret;
  553. ret = kstrtoul(data, 10, &value);
  554. if (ret < 0)
  555. return ret;
  556. if (value > 8) {
  557. amd64_warn("%s: invalid word 0x%lx\n", __func__, value);
  558. return -EINVAL;
  559. }
  560. pvt->injection.word = (u32) value;
  561. return count;
  562. }
  563. static ssize_t inject_ecc_vector_show(struct device *dev,
  564. struct device_attribute *mattr,
  565. char *buf)
  566. {
  567. struct mem_ctl_info *mci = to_mci(dev);
  568. struct amd64_pvt *pvt = mci->pvt_info;
  569. return sprintf(buf, "0x%x\n", pvt->injection.bit_map);
  570. }
  571. /*
  572. * store 16 bit error injection vector which enables injecting errors to the
  573. * corresponding bit within the error injection word above. When used during a
  574. * DRAM ECC read, it holds the contents of the of the DRAM ECC bits.
  575. */
  576. static ssize_t inject_ecc_vector_store(struct device *dev,
  577. struct device_attribute *mattr,
  578. const char *data, size_t count)
  579. {
  580. struct mem_ctl_info *mci = to_mci(dev);
  581. struct amd64_pvt *pvt = mci->pvt_info;
  582. unsigned long value;
  583. int ret;
  584. ret = kstrtoul(data, 16, &value);
  585. if (ret < 0)
  586. return ret;
  587. if (value & 0xFFFF0000) {
  588. amd64_warn("%s: invalid EccVector: 0x%lx\n", __func__, value);
  589. return -EINVAL;
  590. }
  591. pvt->injection.bit_map = (u32) value;
  592. return count;
  593. }
  594. /*
  595. * Do a DRAM ECC read. Assemble staged values in the pvt area, format into
  596. * fields needed by the injection registers and read the NB Array Data Port.
  597. */
  598. static ssize_t inject_read_store(struct device *dev,
  599. struct device_attribute *mattr,
  600. const char *data, size_t count)
  601. {
  602. struct mem_ctl_info *mci = to_mci(dev);
  603. struct amd64_pvt *pvt = mci->pvt_info;
  604. unsigned long value;
  605. u32 section, word_bits;
  606. int ret;
  607. ret = kstrtoul(data, 10, &value);
  608. if (ret < 0)
  609. return ret;
  610. /* Form value to choose 16-byte section of cacheline */
  611. section = F10_NB_ARRAY_DRAM | SET_NB_ARRAY_ADDR(pvt->injection.section);
  612. amd64_write_pci_cfg(pvt->F3, F10_NB_ARRAY_ADDR, section);
  613. word_bits = SET_NB_DRAM_INJECTION_READ(pvt->injection);
  614. /* Issue 'word' and 'bit' along with the READ request */
  615. amd64_write_pci_cfg(pvt->F3, F10_NB_ARRAY_DATA, word_bits);
  616. edac_dbg(0, "section=0x%x word_bits=0x%x\n", section, word_bits);
  617. return count;
  618. }
  619. /*
  620. * Do a DRAM ECC write. Assemble staged values in the pvt area and format into
  621. * fields needed by the injection registers.
  622. */
  623. static ssize_t inject_write_store(struct device *dev,
  624. struct device_attribute *mattr,
  625. const char *data, size_t count)
  626. {
  627. struct mem_ctl_info *mci = to_mci(dev);
  628. struct amd64_pvt *pvt = mci->pvt_info;
  629. u32 section, word_bits, tmp;
  630. unsigned long value;
  631. int ret;
  632. ret = kstrtoul(data, 10, &value);
  633. if (ret < 0)
  634. return ret;
  635. /* Form value to choose 16-byte section of cacheline */
  636. section = F10_NB_ARRAY_DRAM | SET_NB_ARRAY_ADDR(pvt->injection.section);
  637. amd64_write_pci_cfg(pvt->F3, F10_NB_ARRAY_ADDR, section);
  638. word_bits = SET_NB_DRAM_INJECTION_WRITE(pvt->injection);
  639. pr_notice_once("Don't forget to decrease MCE polling interval in\n"
  640. "/sys/bus/machinecheck/devices/machinecheck<CPUNUM>/check_interval\n"
  641. "so that you can get the error report faster.\n");
  642. on_each_cpu(disable_caches, NULL, 1);
  643. /* Issue 'word' and 'bit' along with the READ request */
  644. amd64_write_pci_cfg(pvt->F3, F10_NB_ARRAY_DATA, word_bits);
  645. retry:
  646. /* wait until injection happens */
  647. amd64_read_pci_cfg(pvt->F3, F10_NB_ARRAY_DATA, &tmp);
  648. if (tmp & F10_NB_ARR_ECC_WR_REQ) {
  649. cpu_relax();
  650. goto retry;
  651. }
  652. on_each_cpu(enable_caches, NULL, 1);
  653. edac_dbg(0, "section=0x%x word_bits=0x%x\n", section, word_bits);
  654. return count;
  655. }
  656. /*
  657. * update NUM_INJ_ATTRS in case you add new members
  658. */
  659. static DEVICE_ATTR_RW(inject_section);
  660. static DEVICE_ATTR_RW(inject_word);
  661. static DEVICE_ATTR_RW(inject_ecc_vector);
  662. static DEVICE_ATTR_WO(inject_write);
  663. static DEVICE_ATTR_WO(inject_read);
  664. static struct attribute *inj_attrs[] = {
  665. &dev_attr_inject_section.attr,
  666. &dev_attr_inject_word.attr,
  667. &dev_attr_inject_ecc_vector.attr,
  668. &dev_attr_inject_write.attr,
  669. &dev_attr_inject_read.attr,
  670. NULL
  671. };
  672. static umode_t inj_is_visible(struct kobject *kobj, struct attribute *attr, int idx)
  673. {
  674. struct device *dev = kobj_to_dev(kobj);
  675. struct mem_ctl_info *mci = container_of(dev, struct mem_ctl_info, dev);
  676. struct amd64_pvt *pvt = mci->pvt_info;
  677. /* Families which have that injection hw */
  678. if (pvt->fam >= 0x10 && pvt->fam <= 0x16)
  679. return attr->mode;
  680. return 0;
  681. }
  682. static const struct attribute_group inj_group = {
  683. .attrs = inj_attrs,
  684. .is_visible = inj_is_visible,
  685. };
  686. #endif /* CONFIG_EDAC_DEBUG */
  687. /*
  688. * Return the DramAddr that the SysAddr given by @sys_addr maps to. It is
  689. * assumed that sys_addr maps to the node given by mci.
  690. *
  691. * The first part of section 3.4.4 (p. 70) shows how the DRAM Base (section
  692. * 3.4.4.1) and DRAM Limit (section 3.4.4.2) registers are used to translate a
  693. * SysAddr to a DramAddr. If the DRAM Hole Address Register (DHAR) is enabled,
  694. * then it is also involved in translating a SysAddr to a DramAddr. Sections
  695. * 3.4.8 and 3.5.8.2 describe the DHAR and how it is used for memory hoisting.
  696. * These parts of the documentation are unclear. I interpret them as follows:
  697. *
  698. * When node n receives a SysAddr, it processes the SysAddr as follows:
  699. *
  700. * 1. It extracts the DRAMBase and DRAMLimit values from the DRAM Base and DRAM
  701. * Limit registers for node n. If the SysAddr is not within the range
  702. * specified by the base and limit values, then node n ignores the Sysaddr
  703. * (since it does not map to node n). Otherwise continue to step 2 below.
  704. *
  705. * 2. If the DramHoleValid bit of the DHAR for node n is clear, the DHAR is
  706. * disabled so skip to step 3 below. Otherwise see if the SysAddr is within
  707. * the range of relocated addresses (starting at 0x100000000) from the DRAM
  708. * hole. If not, skip to step 3 below. Else get the value of the
  709. * DramHoleOffset field from the DHAR. To obtain the DramAddr, subtract the
  710. * offset defined by this value from the SysAddr.
  711. *
  712. * 3. Obtain the base address for node n from the DRAMBase field of the DRAM
  713. * Base register for node n. To obtain the DramAddr, subtract the base
  714. * address from the SysAddr, as shown near the start of section 3.4.4 (p.70).
  715. */
  716. static u64 sys_addr_to_dram_addr(struct mem_ctl_info *mci, u64 sys_addr)
  717. {
  718. struct amd64_pvt *pvt = mci->pvt_info;
  719. u64 dram_base, hole_base, hole_offset, hole_size, dram_addr;
  720. int ret;
  721. dram_base = get_dram_base(pvt, pvt->mc_node_id);
  722. ret = get_dram_hole_info(mci, &hole_base, &hole_offset, &hole_size);
  723. if (!ret) {
  724. if ((sys_addr >= (1ULL << 32)) &&
  725. (sys_addr < ((1ULL << 32) + hole_size))) {
  726. /* use DHAR to translate SysAddr to DramAddr */
  727. dram_addr = sys_addr - hole_offset;
  728. edac_dbg(2, "using DHAR to translate SysAddr 0x%lx to DramAddr 0x%lx\n",
  729. (unsigned long)sys_addr,
  730. (unsigned long)dram_addr);
  731. return dram_addr;
  732. }
  733. }
  734. /*
  735. * Translate the SysAddr to a DramAddr as shown near the start of
  736. * section 3.4.4 (p. 70). Although sys_addr is a 64-bit value, the k8
  737. * only deals with 40-bit values. Therefore we discard bits 63-40 of
  738. * sys_addr below. If bit 39 of sys_addr is 1 then the bits we
  739. * discard are all 1s. Otherwise the bits we discard are all 0s. See
  740. * section 3.4.2 of AMD publication 24592: AMD x86-64 Architecture
  741. * Programmer's Manual Volume 1 Application Programming.
  742. */
  743. dram_addr = (sys_addr & GENMASK_ULL(39, 0)) - dram_base;
  744. edac_dbg(2, "using DRAM Base register to translate SysAddr 0x%lx to DramAddr 0x%lx\n",
  745. (unsigned long)sys_addr, (unsigned long)dram_addr);
  746. return dram_addr;
  747. }
  748. /*
  749. * @intlv_en is the value of the IntlvEn field from a DRAM Base register
  750. * (section 3.4.4.1). Return the number of bits from a SysAddr that are used
  751. * for node interleaving.
  752. */
  753. static int num_node_interleave_bits(unsigned intlv_en)
  754. {
  755. static const int intlv_shift_table[] = { 0, 1, 0, 2, 0, 0, 0, 3 };
  756. int n;
  757. BUG_ON(intlv_en > 7);
  758. n = intlv_shift_table[intlv_en];
  759. return n;
  760. }
  761. /* Translate the DramAddr given by @dram_addr to an InputAddr. */
  762. static u64 dram_addr_to_input_addr(struct mem_ctl_info *mci, u64 dram_addr)
  763. {
  764. struct amd64_pvt *pvt;
  765. int intlv_shift;
  766. u64 input_addr;
  767. pvt = mci->pvt_info;
  768. /*
  769. * See the start of section 3.4.4 (p. 70, BKDG #26094, K8, revA-E)
  770. * concerning translating a DramAddr to an InputAddr.
  771. */
  772. intlv_shift = num_node_interleave_bits(dram_intlv_en(pvt, 0));
  773. input_addr = ((dram_addr >> intlv_shift) & GENMASK_ULL(35, 12)) +
  774. (dram_addr & 0xfff);
  775. edac_dbg(2, " Intlv Shift=%d DramAddr=0x%lx maps to InputAddr=0x%lx\n",
  776. intlv_shift, (unsigned long)dram_addr,
  777. (unsigned long)input_addr);
  778. return input_addr;
  779. }
  780. /*
  781. * Translate the SysAddr represented by @sys_addr to an InputAddr. It is
  782. * assumed that @sys_addr maps to the node given by mci.
  783. */
  784. static u64 sys_addr_to_input_addr(struct mem_ctl_info *mci, u64 sys_addr)
  785. {
  786. u64 input_addr;
  787. input_addr =
  788. dram_addr_to_input_addr(mci, sys_addr_to_dram_addr(mci, sys_addr));
  789. edac_dbg(2, "SysAddr 0x%lx translates to InputAddr 0x%lx\n",
  790. (unsigned long)sys_addr, (unsigned long)input_addr);
  791. return input_addr;
  792. }
  793. /* Map the Error address to a PAGE and PAGE OFFSET. */
  794. static inline void error_address_to_page_and_offset(u64 error_address,
  795. struct err_info *err)
  796. {
  797. err->page = (u32) (error_address >> PAGE_SHIFT);
  798. err->offset = ((u32) error_address) & ~PAGE_MASK;
  799. }
  800. /*
  801. * @sys_addr is an error address (a SysAddr) extracted from the MCA NB Address
  802. * Low (section 3.6.4.5) and MCA NB Address High (section 3.6.4.6) registers
  803. * of a node that detected an ECC memory error. mci represents the node that
  804. * the error address maps to (possibly different from the node that detected
  805. * the error). Return the number of the csrow that sys_addr maps to, or -1 on
  806. * error.
  807. */
  808. static int sys_addr_to_csrow(struct mem_ctl_info *mci, u64 sys_addr)
  809. {
  810. int csrow;
  811. csrow = input_addr_to_csrow(mci, sys_addr_to_input_addr(mci, sys_addr));
  812. if (csrow == -1)
  813. amd64_mc_err(mci, "Failed to translate InputAddr to csrow for "
  814. "address 0x%lx\n", (unsigned long)sys_addr);
  815. return csrow;
  816. }
  817. /*
  818. * See AMD PPR DF::LclNodeTypeMap
  819. *
  820. * This register gives information for nodes of the same type within a system.
  821. *
  822. * Reading this register from a GPU node will tell how many GPU nodes are in the
  823. * system and what the lowest AMD Node ID value is for the GPU nodes. Use this
  824. * info to fixup the Linux logical "Node ID" value set in the AMD NB code and EDAC.
  825. */
  826. static struct local_node_map {
  827. u16 node_count;
  828. u16 base_node_id;
  829. } gpu_node_map;
  830. #define PCI_DEVICE_ID_AMD_MI200_DF_F1 0x14d1
  831. #define REG_LOCAL_NODE_TYPE_MAP 0x144
  832. /* Local Node Type Map (LNTM) fields */
  833. #define LNTM_NODE_COUNT GENMASK(27, 16)
  834. #define LNTM_BASE_NODE_ID GENMASK(11, 0)
  835. static int gpu_get_node_map(struct amd64_pvt *pvt)
  836. {
  837. struct pci_dev *pdev;
  838. int ret;
  839. u32 tmp;
  840. /*
  841. * Mapping of nodes from hardware-provided AMD Node ID to a
  842. * Linux logical one is applicable for MI200 models. Therefore,
  843. * return early for other heterogeneous systems.
  844. */
  845. if (pvt->F3->device != PCI_DEVICE_ID_AMD_MI200_DF_F3)
  846. return 0;
  847. /*
  848. * Node ID 0 is reserved for CPUs. Therefore, a non-zero Node ID
  849. * means the values have been already cached.
  850. */
  851. if (gpu_node_map.base_node_id)
  852. return 0;
  853. pdev = pci_get_device(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_MI200_DF_F1, NULL);
  854. if (!pdev) {
  855. ret = -ENODEV;
  856. goto out;
  857. }
  858. ret = pci_read_config_dword(pdev, REG_LOCAL_NODE_TYPE_MAP, &tmp);
  859. if (ret) {
  860. ret = pcibios_err_to_errno(ret);
  861. goto out;
  862. }
  863. gpu_node_map.node_count = FIELD_GET(LNTM_NODE_COUNT, tmp);
  864. gpu_node_map.base_node_id = FIELD_GET(LNTM_BASE_NODE_ID, tmp);
  865. out:
  866. pci_dev_put(pdev);
  867. return ret;
  868. }
  869. static int fixup_node_id(int node_id, struct mce *m)
  870. {
  871. /* MCA_IPID[InstanceIdHi] give the AMD Node ID for the bank. */
  872. u8 nid = (m->ipid >> 44) & 0xF;
  873. if (smca_get_bank_type(m->extcpu, m->bank) != SMCA_UMC_V2)
  874. return node_id;
  875. /* Nodes below the GPU base node are CPU nodes and don't need a fixup. */
  876. if (nid < gpu_node_map.base_node_id)
  877. return node_id;
  878. /* Convert the hardware-provided AMD Node ID to a Linux logical one. */
  879. return nid - gpu_node_map.base_node_id + 1;
  880. }
  881. static int get_channel_from_ecc_syndrome(struct mem_ctl_info *, u16);
  882. /*
  883. * Determine if the DIMMs have ECC enabled. ECC is enabled ONLY if all the DIMMs
  884. * are ECC capable.
  885. */
  886. static unsigned long dct_determine_edac_cap(struct amd64_pvt *pvt)
  887. {
  888. unsigned long edac_cap = EDAC_FLAG_NONE;
  889. u8 bit;
  890. bit = (pvt->fam > 0xf || pvt->ext_model >= K8_REV_F)
  891. ? 19
  892. : 17;
  893. if (pvt->dclr0 & BIT(bit))
  894. edac_cap = EDAC_FLAG_SECDED;
  895. return edac_cap;
  896. }
  897. static unsigned long umc_determine_edac_cap(struct amd64_pvt *pvt)
  898. {
  899. u8 i, umc_en_mask = 0, dimm_ecc_en_mask = 0;
  900. unsigned long edac_cap = EDAC_FLAG_NONE;
  901. for_each_umc(i) {
  902. if (!(pvt->umc[i].sdp_ctrl & UMC_SDP_INIT))
  903. continue;
  904. umc_en_mask |= BIT(i);
  905. /* UMC Configuration bit 12 (DimmEccEn) */
  906. if (pvt->umc[i].umc_cfg & BIT(12))
  907. dimm_ecc_en_mask |= BIT(i);
  908. }
  909. if (umc_en_mask == dimm_ecc_en_mask)
  910. edac_cap = EDAC_FLAG_SECDED;
  911. return edac_cap;
  912. }
  913. /*
  914. * debug routine to display the memory sizes of all logical DIMMs and its
  915. * CSROWs
  916. */
  917. static void dct_debug_display_dimm_sizes(struct amd64_pvt *pvt, u8 ctrl)
  918. {
  919. u32 *dcsb = ctrl ? pvt->csels[1].csbases : pvt->csels[0].csbases;
  920. u32 dbam = ctrl ? pvt->dbam1 : pvt->dbam0;
  921. int dimm, size0, size1;
  922. if (pvt->fam == 0xf) {
  923. /* K8 families < revF not supported yet */
  924. if (pvt->ext_model < K8_REV_F)
  925. return;
  926. WARN_ON(ctrl != 0);
  927. }
  928. if (pvt->fam == 0x10) {
  929. dbam = (ctrl && !dct_ganging_enabled(pvt)) ? pvt->dbam1
  930. : pvt->dbam0;
  931. dcsb = (ctrl && !dct_ganging_enabled(pvt)) ?
  932. pvt->csels[1].csbases :
  933. pvt->csels[0].csbases;
  934. } else if (ctrl) {
  935. dbam = pvt->dbam0;
  936. dcsb = pvt->csels[1].csbases;
  937. }
  938. edac_dbg(1, "F2x%d80 (DRAM Bank Address Mapping): 0x%08x\n",
  939. ctrl, dbam);
  940. edac_printk(KERN_DEBUG, EDAC_MC, "DCT%d chip selects:\n", ctrl);
  941. /* Dump memory sizes for DIMM and its CSROWs */
  942. for (dimm = 0; dimm < 4; dimm++) {
  943. size0 = 0;
  944. if (dcsb[dimm * 2] & DCSB_CS_ENABLE)
  945. /*
  946. * For F15m60h, we need multiplier for LRDIMM cs_size
  947. * calculation. We pass dimm value to the dbam_to_cs
  948. * mapper so we can find the multiplier from the
  949. * corresponding DCSM.
  950. */
  951. size0 = pvt->ops->dbam_to_cs(pvt, ctrl,
  952. DBAM_DIMM(dimm, dbam),
  953. dimm);
  954. size1 = 0;
  955. if (dcsb[dimm * 2 + 1] & DCSB_CS_ENABLE)
  956. size1 = pvt->ops->dbam_to_cs(pvt, ctrl,
  957. DBAM_DIMM(dimm, dbam),
  958. dimm);
  959. amd64_info(EDAC_MC ": %d: %5dMB %d: %5dMB\n",
  960. dimm * 2, size0,
  961. dimm * 2 + 1, size1);
  962. }
  963. }
  964. static void debug_dump_dramcfg_low(struct amd64_pvt *pvt, u32 dclr, int chan)
  965. {
  966. edac_dbg(1, "F2x%d90 (DRAM Cfg Low): 0x%08x\n", chan, dclr);
  967. if (pvt->dram_type == MEM_LRDDR3) {
  968. u32 dcsm = pvt->csels[chan].csmasks[0];
  969. /*
  970. * It's assumed all LRDIMMs in a DCT are going to be of
  971. * same 'type' until proven otherwise. So, use a cs
  972. * value of '0' here to get dcsm value.
  973. */
  974. edac_dbg(1, " LRDIMM %dx rank multiply\n", (dcsm & 0x3));
  975. }
  976. edac_dbg(1, "All DIMMs support ECC:%s\n",
  977. (dclr & BIT(19)) ? "yes" : "no");
  978. edac_dbg(1, " PAR/ERR parity: %s\n",
  979. (dclr & BIT(8)) ? "enabled" : "disabled");
  980. if (pvt->fam == 0x10)
  981. edac_dbg(1, " DCT 128bit mode width: %s\n",
  982. (dclr & BIT(11)) ? "128b" : "64b");
  983. edac_dbg(1, " x4 logical DIMMs present: L0: %s L1: %s L2: %s L3: %s\n",
  984. (dclr & BIT(12)) ? "yes" : "no",
  985. (dclr & BIT(13)) ? "yes" : "no",
  986. (dclr & BIT(14)) ? "yes" : "no",
  987. (dclr & BIT(15)) ? "yes" : "no");
  988. }
  989. #define CS_EVEN_PRIMARY BIT(0)
  990. #define CS_ODD_PRIMARY BIT(1)
  991. #define CS_EVEN_SECONDARY BIT(2)
  992. #define CS_ODD_SECONDARY BIT(3)
  993. #define CS_3R_INTERLEAVE BIT(4)
  994. #define CS_EVEN (CS_EVEN_PRIMARY | CS_EVEN_SECONDARY)
  995. #define CS_ODD (CS_ODD_PRIMARY | CS_ODD_SECONDARY)
  996. static int umc_get_cs_mode(int dimm, u8 ctrl, struct amd64_pvt *pvt)
  997. {
  998. u8 base, count = 0;
  999. int cs_mode = 0;
  1000. if (csrow_enabled(2 * dimm, ctrl, pvt))
  1001. cs_mode |= CS_EVEN_PRIMARY;
  1002. if (csrow_enabled(2 * dimm + 1, ctrl, pvt))
  1003. cs_mode |= CS_ODD_PRIMARY;
  1004. if (csrow_sec_enabled(2 * dimm, ctrl, pvt))
  1005. cs_mode |= CS_EVEN_SECONDARY;
  1006. if (csrow_sec_enabled(2 * dimm + 1, ctrl, pvt))
  1007. cs_mode |= CS_ODD_SECONDARY;
  1008. /*
  1009. * 3 Rank inteleaving support.
  1010. * There should be only three bases enabled and their two masks should
  1011. * be equal.
  1012. */
  1013. for_each_chip_select(base, ctrl, pvt)
  1014. count += csrow_enabled(base, ctrl, pvt);
  1015. if (count == 3 &&
  1016. pvt->csels[ctrl].csmasks[0] == pvt->csels[ctrl].csmasks[1]) {
  1017. edac_dbg(1, "3R interleaving in use.\n");
  1018. cs_mode |= CS_3R_INTERLEAVE;
  1019. }
  1020. return cs_mode;
  1021. }
  1022. static int calculate_cs_size(u32 mask, unsigned int cs_mode)
  1023. {
  1024. int msb, weight, num_zero_bits;
  1025. u32 deinterleaved_mask;
  1026. if (!mask)
  1027. return 0;
  1028. /*
  1029. * The number of zero bits in the mask is equal to the number of bits
  1030. * in a full mask minus the number of bits in the current mask.
  1031. *
  1032. * The MSB is the number of bits in the full mask because BIT[0] is
  1033. * always 0.
  1034. *
  1035. * In the special 3 Rank interleaving case, a single bit is flipped
  1036. * without swapping with the most significant bit. This can be handled
  1037. * by keeping the MSB where it is and ignoring the single zero bit.
  1038. */
  1039. msb = fls(mask) - 1;
  1040. weight = hweight_long(mask);
  1041. num_zero_bits = msb - weight - !!(cs_mode & CS_3R_INTERLEAVE);
  1042. /* Take the number of zero bits off from the top of the mask. */
  1043. deinterleaved_mask = GENMASK(msb - num_zero_bits, 1);
  1044. edac_dbg(1, " Deinterleaved AddrMask: 0x%x\n", deinterleaved_mask);
  1045. return (deinterleaved_mask >> 2) + 1;
  1046. }
  1047. static int __addr_mask_to_cs_size(u32 addr_mask, u32 addr_mask_sec,
  1048. unsigned int cs_mode, int csrow_nr, int dimm)
  1049. {
  1050. int size;
  1051. edac_dbg(1, "CS%d DIMM%d AddrMasks:\n", csrow_nr, dimm);
  1052. edac_dbg(1, " Primary AddrMask: 0x%x\n", addr_mask);
  1053. /* Register [31:1] = Address [39:9]. Size is in kBs here. */
  1054. size = calculate_cs_size(addr_mask, cs_mode);
  1055. edac_dbg(1, " Secondary AddrMask: 0x%x\n", addr_mask_sec);
  1056. size += calculate_cs_size(addr_mask_sec, cs_mode);
  1057. /* Return size in MBs. */
  1058. return size >> 10;
  1059. }
  1060. static int umc_addr_mask_to_cs_size(struct amd64_pvt *pvt, u8 umc,
  1061. unsigned int cs_mode, int csrow_nr)
  1062. {
  1063. u32 addr_mask = 0, addr_mask_sec = 0;
  1064. int cs_mask_nr = csrow_nr;
  1065. int dimm, size = 0;
  1066. /* No Chip Selects are enabled. */
  1067. if (!cs_mode)
  1068. return size;
  1069. /* Requested size of an even CS but none are enabled. */
  1070. if (!(cs_mode & CS_EVEN) && !(csrow_nr & 1))
  1071. return size;
  1072. /* Requested size of an odd CS but none are enabled. */
  1073. if (!(cs_mode & CS_ODD) && (csrow_nr & 1))
  1074. return size;
  1075. /*
  1076. * Family 17h introduced systems with one mask per DIMM,
  1077. * and two Chip Selects per DIMM.
  1078. *
  1079. * CS0 and CS1 -> MASK0 / DIMM0
  1080. * CS2 and CS3 -> MASK1 / DIMM1
  1081. *
  1082. * Family 19h Model 10h introduced systems with one mask per Chip Select,
  1083. * and two Chip Selects per DIMM.
  1084. *
  1085. * CS0 -> MASK0 -> DIMM0
  1086. * CS1 -> MASK1 -> DIMM0
  1087. * CS2 -> MASK2 -> DIMM1
  1088. * CS3 -> MASK3 -> DIMM1
  1089. *
  1090. * Keep the mask number equal to the Chip Select number for newer systems,
  1091. * and shift the mask number for older systems.
  1092. */
  1093. dimm = csrow_nr >> 1;
  1094. if (!pvt->flags.zn_regs_v2)
  1095. cs_mask_nr >>= 1;
  1096. if (cs_mode & (CS_EVEN_PRIMARY | CS_ODD_PRIMARY))
  1097. addr_mask = pvt->csels[umc].csmasks[cs_mask_nr];
  1098. if (cs_mode & (CS_EVEN_SECONDARY | CS_ODD_SECONDARY))
  1099. addr_mask_sec = pvt->csels[umc].csmasks_sec[cs_mask_nr];
  1100. return __addr_mask_to_cs_size(addr_mask, addr_mask_sec, cs_mode, csrow_nr, dimm);
  1101. }
  1102. static void umc_debug_display_dimm_sizes(struct amd64_pvt *pvt, u8 ctrl)
  1103. {
  1104. int dimm, size0, size1, cs0, cs1, cs_mode;
  1105. edac_printk(KERN_DEBUG, EDAC_MC, "UMC%d chip selects:\n", ctrl);
  1106. for (dimm = 0; dimm < 2; dimm++) {
  1107. cs0 = dimm * 2;
  1108. cs1 = dimm * 2 + 1;
  1109. cs_mode = umc_get_cs_mode(dimm, ctrl, pvt);
  1110. size0 = umc_addr_mask_to_cs_size(pvt, ctrl, cs_mode, cs0);
  1111. size1 = umc_addr_mask_to_cs_size(pvt, ctrl, cs_mode, cs1);
  1112. amd64_info(EDAC_MC ": %d: %5dMB %d: %5dMB\n",
  1113. cs0, size0,
  1114. cs1, size1);
  1115. }
  1116. }
  1117. static void umc_dump_misc_regs(struct amd64_pvt *pvt)
  1118. {
  1119. struct amd64_umc *umc;
  1120. u32 i;
  1121. for_each_umc(i) {
  1122. umc = &pvt->umc[i];
  1123. edac_dbg(1, "UMC%d DIMM cfg: 0x%x\n", i, umc->dimm_cfg);
  1124. edac_dbg(1, "UMC%d UMC cfg: 0x%x\n", i, umc->umc_cfg);
  1125. edac_dbg(1, "UMC%d SDP ctrl: 0x%x\n", i, umc->sdp_ctrl);
  1126. edac_dbg(1, "UMC%d ECC ctrl: 0x%x\n", i, umc->ecc_ctrl);
  1127. edac_dbg(1, "UMC%d UMC cap high: 0x%x\n", i, umc->umc_cap_hi);
  1128. edac_dbg(1, "UMC%d ECC capable: %s, ChipKill ECC capable: %s\n",
  1129. i, (umc->umc_cap_hi & BIT(30)) ? "yes" : "no",
  1130. (umc->umc_cap_hi & BIT(31)) ? "yes" : "no");
  1131. edac_dbg(1, "UMC%d All DIMMs support ECC: %s\n",
  1132. i, (umc->umc_cfg & BIT(12)) ? "yes" : "no");
  1133. edac_dbg(1, "UMC%d x4 DIMMs present: %s\n",
  1134. i, (umc->dimm_cfg & BIT(6)) ? "yes" : "no");
  1135. edac_dbg(1, "UMC%d x16 DIMMs present: %s\n",
  1136. i, (umc->dimm_cfg & BIT(7)) ? "yes" : "no");
  1137. umc_debug_display_dimm_sizes(pvt, i);
  1138. }
  1139. }
  1140. static void dct_dump_misc_regs(struct amd64_pvt *pvt)
  1141. {
  1142. edac_dbg(1, "F3xE8 (NB Cap): 0x%08x\n", pvt->nbcap);
  1143. edac_dbg(1, " NB two channel DRAM capable: %s\n",
  1144. (pvt->nbcap & NBCAP_DCT_DUAL) ? "yes" : "no");
  1145. edac_dbg(1, " ECC capable: %s, ChipKill ECC capable: %s\n",
  1146. (pvt->nbcap & NBCAP_SECDED) ? "yes" : "no",
  1147. (pvt->nbcap & NBCAP_CHIPKILL) ? "yes" : "no");
  1148. debug_dump_dramcfg_low(pvt, pvt->dclr0, 0);
  1149. edac_dbg(1, "F3xB0 (Online Spare): 0x%08x\n", pvt->online_spare);
  1150. edac_dbg(1, "F1xF0 (DRAM Hole Address): 0x%08x, base: 0x%08x, offset: 0x%08x\n",
  1151. pvt->dhar, dhar_base(pvt),
  1152. (pvt->fam == 0xf) ? k8_dhar_offset(pvt)
  1153. : f10_dhar_offset(pvt));
  1154. dct_debug_display_dimm_sizes(pvt, 0);
  1155. /* everything below this point is Fam10h and above */
  1156. if (pvt->fam == 0xf)
  1157. return;
  1158. dct_debug_display_dimm_sizes(pvt, 1);
  1159. /* Only if NOT ganged does dclr1 have valid info */
  1160. if (!dct_ganging_enabled(pvt))
  1161. debug_dump_dramcfg_low(pvt, pvt->dclr1, 1);
  1162. edac_dbg(1, " DramHoleValid: %s\n", dhar_valid(pvt) ? "yes" : "no");
  1163. amd64_info("using x%u syndromes.\n", pvt->ecc_sym_sz);
  1164. }
  1165. /*
  1166. * See BKDG, F2x[1,0][5C:40], F2[1,0][6C:60]
  1167. */
  1168. static void dct_prep_chip_selects(struct amd64_pvt *pvt)
  1169. {
  1170. if (pvt->fam == 0xf && pvt->ext_model < K8_REV_F) {
  1171. pvt->csels[0].b_cnt = pvt->csels[1].b_cnt = 8;
  1172. pvt->csels[0].m_cnt = pvt->csels[1].m_cnt = 8;
  1173. } else if (pvt->fam == 0x15 && pvt->model == 0x30) {
  1174. pvt->csels[0].b_cnt = pvt->csels[1].b_cnt = 4;
  1175. pvt->csels[0].m_cnt = pvt->csels[1].m_cnt = 2;
  1176. } else {
  1177. pvt->csels[0].b_cnt = pvt->csels[1].b_cnt = 8;
  1178. pvt->csels[0].m_cnt = pvt->csels[1].m_cnt = 4;
  1179. }
  1180. }
  1181. static void umc_prep_chip_selects(struct amd64_pvt *pvt)
  1182. {
  1183. int umc;
  1184. for_each_umc(umc) {
  1185. pvt->csels[umc].b_cnt = 4;
  1186. pvt->csels[umc].m_cnt = pvt->flags.zn_regs_v2 ? 4 : 2;
  1187. }
  1188. }
  1189. static void umc_read_base_mask(struct amd64_pvt *pvt)
  1190. {
  1191. u32 umc_base_reg, umc_base_reg_sec;
  1192. u32 umc_mask_reg, umc_mask_reg_sec;
  1193. u32 base_reg, base_reg_sec;
  1194. u32 mask_reg, mask_reg_sec;
  1195. u32 *base, *base_sec;
  1196. u32 *mask, *mask_sec;
  1197. int cs, umc;
  1198. u32 tmp;
  1199. for_each_umc(umc) {
  1200. umc_base_reg = get_umc_base(umc) + UMCCH_BASE_ADDR;
  1201. umc_base_reg_sec = get_umc_base(umc) + UMCCH_BASE_ADDR_SEC;
  1202. for_each_chip_select(cs, umc, pvt) {
  1203. base = &pvt->csels[umc].csbases[cs];
  1204. base_sec = &pvt->csels[umc].csbases_sec[cs];
  1205. base_reg = umc_base_reg + (cs * 4);
  1206. base_reg_sec = umc_base_reg_sec + (cs * 4);
  1207. if (!amd_smn_read(pvt->mc_node_id, base_reg, &tmp)) {
  1208. *base = tmp;
  1209. edac_dbg(0, " DCSB%d[%d]=0x%08x reg: 0x%x\n",
  1210. umc, cs, *base, base_reg);
  1211. }
  1212. if (!amd_smn_read(pvt->mc_node_id, base_reg_sec, &tmp)) {
  1213. *base_sec = tmp;
  1214. edac_dbg(0, " DCSB_SEC%d[%d]=0x%08x reg: 0x%x\n",
  1215. umc, cs, *base_sec, base_reg_sec);
  1216. }
  1217. }
  1218. umc_mask_reg = get_umc_base(umc) + UMCCH_ADDR_MASK;
  1219. umc_mask_reg_sec = get_umc_base(umc) + get_umc_reg(pvt, UMCCH_ADDR_MASK_SEC);
  1220. for_each_chip_select_mask(cs, umc, pvt) {
  1221. mask = &pvt->csels[umc].csmasks[cs];
  1222. mask_sec = &pvt->csels[umc].csmasks_sec[cs];
  1223. mask_reg = umc_mask_reg + (cs * 4);
  1224. mask_reg_sec = umc_mask_reg_sec + (cs * 4);
  1225. if (!amd_smn_read(pvt->mc_node_id, mask_reg, &tmp)) {
  1226. *mask = tmp;
  1227. edac_dbg(0, " DCSM%d[%d]=0x%08x reg: 0x%x\n",
  1228. umc, cs, *mask, mask_reg);
  1229. }
  1230. if (!amd_smn_read(pvt->mc_node_id, mask_reg_sec, &tmp)) {
  1231. *mask_sec = tmp;
  1232. edac_dbg(0, " DCSM_SEC%d[%d]=0x%08x reg: 0x%x\n",
  1233. umc, cs, *mask_sec, mask_reg_sec);
  1234. }
  1235. }
  1236. }
  1237. }
  1238. /*
  1239. * Function 2 Offset F10_DCSB0; read in the DCS Base and DCS Mask registers
  1240. */
  1241. static void dct_read_base_mask(struct amd64_pvt *pvt)
  1242. {
  1243. int cs;
  1244. for_each_chip_select(cs, 0, pvt) {
  1245. int reg0 = DCSB0 + (cs * 4);
  1246. int reg1 = DCSB1 + (cs * 4);
  1247. u32 *base0 = &pvt->csels[0].csbases[cs];
  1248. u32 *base1 = &pvt->csels[1].csbases[cs];
  1249. if (!amd64_read_dct_pci_cfg(pvt, 0, reg0, base0))
  1250. edac_dbg(0, " DCSB0[%d]=0x%08x reg: F2x%x\n",
  1251. cs, *base0, reg0);
  1252. if (pvt->fam == 0xf)
  1253. continue;
  1254. if (!amd64_read_dct_pci_cfg(pvt, 1, reg0, base1))
  1255. edac_dbg(0, " DCSB1[%d]=0x%08x reg: F2x%x\n",
  1256. cs, *base1, (pvt->fam == 0x10) ? reg1
  1257. : reg0);
  1258. }
  1259. for_each_chip_select_mask(cs, 0, pvt) {
  1260. int reg0 = DCSM0 + (cs * 4);
  1261. int reg1 = DCSM1 + (cs * 4);
  1262. u32 *mask0 = &pvt->csels[0].csmasks[cs];
  1263. u32 *mask1 = &pvt->csels[1].csmasks[cs];
  1264. if (!amd64_read_dct_pci_cfg(pvt, 0, reg0, mask0))
  1265. edac_dbg(0, " DCSM0[%d]=0x%08x reg: F2x%x\n",
  1266. cs, *mask0, reg0);
  1267. if (pvt->fam == 0xf)
  1268. continue;
  1269. if (!amd64_read_dct_pci_cfg(pvt, 1, reg0, mask1))
  1270. edac_dbg(0, " DCSM1[%d]=0x%08x reg: F2x%x\n",
  1271. cs, *mask1, (pvt->fam == 0x10) ? reg1
  1272. : reg0);
  1273. }
  1274. }
  1275. static void umc_determine_memory_type(struct amd64_pvt *pvt)
  1276. {
  1277. struct amd64_umc *umc;
  1278. u32 i;
  1279. for_each_umc(i) {
  1280. umc = &pvt->umc[i];
  1281. if (!(umc->sdp_ctrl & UMC_SDP_INIT)) {
  1282. umc->dram_type = MEM_EMPTY;
  1283. continue;
  1284. }
  1285. /*
  1286. * Check if the system supports the "DDR Type" field in UMC Config
  1287. * and has DDR5 DIMMs in use.
  1288. */
  1289. if (pvt->flags.zn_regs_v2 && ((umc->umc_cfg & GENMASK(2, 0)) == 0x1)) {
  1290. if (umc->dimm_cfg & BIT(5))
  1291. umc->dram_type = MEM_LRDDR5;
  1292. else if (umc->dimm_cfg & BIT(4))
  1293. umc->dram_type = MEM_RDDR5;
  1294. else
  1295. umc->dram_type = MEM_DDR5;
  1296. } else {
  1297. if (umc->dimm_cfg & BIT(5))
  1298. umc->dram_type = MEM_LRDDR4;
  1299. else if (umc->dimm_cfg & BIT(4))
  1300. umc->dram_type = MEM_RDDR4;
  1301. else
  1302. umc->dram_type = MEM_DDR4;
  1303. }
  1304. edac_dbg(1, " UMC%d DIMM type: %s\n", i, edac_mem_types[umc->dram_type]);
  1305. }
  1306. }
  1307. static void dct_determine_memory_type(struct amd64_pvt *pvt)
  1308. {
  1309. u32 dram_ctrl, dcsm;
  1310. switch (pvt->fam) {
  1311. case 0xf:
  1312. if (pvt->ext_model >= K8_REV_F)
  1313. goto ddr3;
  1314. pvt->dram_type = (pvt->dclr0 & BIT(18)) ? MEM_DDR : MEM_RDDR;
  1315. return;
  1316. case 0x10:
  1317. if (pvt->dchr0 & DDR3_MODE)
  1318. goto ddr3;
  1319. pvt->dram_type = (pvt->dclr0 & BIT(16)) ? MEM_DDR2 : MEM_RDDR2;
  1320. return;
  1321. case 0x15:
  1322. if (pvt->model < 0x60)
  1323. goto ddr3;
  1324. /*
  1325. * Model 0x60h needs special handling:
  1326. *
  1327. * We use a Chip Select value of '0' to obtain dcsm.
  1328. * Theoretically, it is possible to populate LRDIMMs of different
  1329. * 'Rank' value on a DCT. But this is not the common case. So,
  1330. * it's reasonable to assume all DIMMs are going to be of same
  1331. * 'type' until proven otherwise.
  1332. */
  1333. amd64_read_dct_pci_cfg(pvt, 0, DRAM_CONTROL, &dram_ctrl);
  1334. dcsm = pvt->csels[0].csmasks[0];
  1335. if (((dram_ctrl >> 8) & 0x7) == 0x2)
  1336. pvt->dram_type = MEM_DDR4;
  1337. else if (pvt->dclr0 & BIT(16))
  1338. pvt->dram_type = MEM_DDR3;
  1339. else if (dcsm & 0x3)
  1340. pvt->dram_type = MEM_LRDDR3;
  1341. else
  1342. pvt->dram_type = MEM_RDDR3;
  1343. return;
  1344. case 0x16:
  1345. goto ddr3;
  1346. default:
  1347. WARN(1, KERN_ERR "%s: Family??? 0x%x\n", __func__, pvt->fam);
  1348. pvt->dram_type = MEM_EMPTY;
  1349. }
  1350. edac_dbg(1, " DIMM type: %s\n", edac_mem_types[pvt->dram_type]);
  1351. return;
  1352. ddr3:
  1353. pvt->dram_type = (pvt->dclr0 & BIT(16)) ? MEM_DDR3 : MEM_RDDR3;
  1354. }
  1355. /* On F10h and later ErrAddr is MC4_ADDR[47:1] */
  1356. static u64 get_error_address(struct amd64_pvt *pvt, struct mce *m)
  1357. {
  1358. u16 mce_nid = topology_amd_node_id(m->extcpu);
  1359. struct mem_ctl_info *mci;
  1360. u8 start_bit = 1;
  1361. u8 end_bit = 47;
  1362. u64 addr;
  1363. mci = edac_mc_find(mce_nid);
  1364. if (!mci)
  1365. return 0;
  1366. pvt = mci->pvt_info;
  1367. if (pvt->fam == 0xf) {
  1368. start_bit = 3;
  1369. end_bit = 39;
  1370. }
  1371. addr = m->addr & GENMASK_ULL(end_bit, start_bit);
  1372. /*
  1373. * Erratum 637 workaround
  1374. */
  1375. if (pvt->fam == 0x15) {
  1376. u64 cc6_base, tmp_addr;
  1377. u32 tmp;
  1378. u8 intlv_en;
  1379. if ((addr & GENMASK_ULL(47, 24)) >> 24 != 0x00fdf7)
  1380. return addr;
  1381. amd64_read_pci_cfg(pvt->F1, DRAM_LOCAL_NODE_LIM, &tmp);
  1382. intlv_en = tmp >> 21 & 0x7;
  1383. /* add [47:27] + 3 trailing bits */
  1384. cc6_base = (tmp & GENMASK_ULL(20, 0)) << 3;
  1385. /* reverse and add DramIntlvEn */
  1386. cc6_base |= intlv_en ^ 0x7;
  1387. /* pin at [47:24] */
  1388. cc6_base <<= 24;
  1389. if (!intlv_en)
  1390. return cc6_base | (addr & GENMASK_ULL(23, 0));
  1391. amd64_read_pci_cfg(pvt->F1, DRAM_LOCAL_NODE_BASE, &tmp);
  1392. /* faster log2 */
  1393. tmp_addr = (addr & GENMASK_ULL(23, 12)) << __fls(intlv_en + 1);
  1394. /* OR DramIntlvSel into bits [14:12] */
  1395. tmp_addr |= (tmp & GENMASK_ULL(23, 21)) >> 9;
  1396. /* add remaining [11:0] bits from original MC4_ADDR */
  1397. tmp_addr |= addr & GENMASK_ULL(11, 0);
  1398. return cc6_base | tmp_addr;
  1399. }
  1400. return addr;
  1401. }
  1402. static struct pci_dev *pci_get_related_function(unsigned int vendor,
  1403. unsigned int device,
  1404. struct pci_dev *related)
  1405. {
  1406. struct pci_dev *dev = NULL;
  1407. while ((dev = pci_get_device(vendor, device, dev))) {
  1408. if (pci_domain_nr(dev->bus) == pci_domain_nr(related->bus) &&
  1409. (dev->bus->number == related->bus->number) &&
  1410. (PCI_SLOT(dev->devfn) == PCI_SLOT(related->devfn)))
  1411. break;
  1412. }
  1413. return dev;
  1414. }
  1415. static void read_dram_base_limit_regs(struct amd64_pvt *pvt, unsigned range)
  1416. {
  1417. struct amd_northbridge *nb;
  1418. struct pci_dev *f1 = NULL;
  1419. unsigned int pci_func;
  1420. int off = range << 3;
  1421. u32 llim;
  1422. amd64_read_pci_cfg(pvt->F1, DRAM_BASE_LO + off, &pvt->ranges[range].base.lo);
  1423. amd64_read_pci_cfg(pvt->F1, DRAM_LIMIT_LO + off, &pvt->ranges[range].lim.lo);
  1424. if (pvt->fam == 0xf)
  1425. return;
  1426. if (!dram_rw(pvt, range))
  1427. return;
  1428. amd64_read_pci_cfg(pvt->F1, DRAM_BASE_HI + off, &pvt->ranges[range].base.hi);
  1429. amd64_read_pci_cfg(pvt->F1, DRAM_LIMIT_HI + off, &pvt->ranges[range].lim.hi);
  1430. /* F15h: factor in CC6 save area by reading dst node's limit reg */
  1431. if (pvt->fam != 0x15)
  1432. return;
  1433. nb = node_to_amd_nb(dram_dst_node(pvt, range));
  1434. if (WARN_ON(!nb))
  1435. return;
  1436. if (pvt->model == 0x60)
  1437. pci_func = PCI_DEVICE_ID_AMD_15H_M60H_NB_F1;
  1438. else if (pvt->model == 0x30)
  1439. pci_func = PCI_DEVICE_ID_AMD_15H_M30H_NB_F1;
  1440. else
  1441. pci_func = PCI_DEVICE_ID_AMD_15H_NB_F1;
  1442. f1 = pci_get_related_function(nb->misc->vendor, pci_func, nb->misc);
  1443. if (WARN_ON(!f1))
  1444. return;
  1445. amd64_read_pci_cfg(f1, DRAM_LOCAL_NODE_LIM, &llim);
  1446. pvt->ranges[range].lim.lo &= GENMASK_ULL(15, 0);
  1447. /* {[39:27],111b} */
  1448. pvt->ranges[range].lim.lo |= ((llim & 0x1fff) << 3 | 0x7) << 16;
  1449. pvt->ranges[range].lim.hi &= GENMASK_ULL(7, 0);
  1450. /* [47:40] */
  1451. pvt->ranges[range].lim.hi |= llim >> 13;
  1452. pci_dev_put(f1);
  1453. }
  1454. static void k8_map_sysaddr_to_csrow(struct mem_ctl_info *mci, u64 sys_addr,
  1455. struct err_info *err)
  1456. {
  1457. struct amd64_pvt *pvt = mci->pvt_info;
  1458. error_address_to_page_and_offset(sys_addr, err);
  1459. /*
  1460. * Find out which node the error address belongs to. This may be
  1461. * different from the node that detected the error.
  1462. */
  1463. err->src_mci = find_mc_by_sys_addr(mci, sys_addr);
  1464. if (!err->src_mci) {
  1465. amd64_mc_err(mci, "failed to map error addr 0x%lx to a node\n",
  1466. (unsigned long)sys_addr);
  1467. err->err_code = ERR_NODE;
  1468. return;
  1469. }
  1470. /* Now map the sys_addr to a CSROW */
  1471. err->csrow = sys_addr_to_csrow(err->src_mci, sys_addr);
  1472. if (err->csrow < 0) {
  1473. err->err_code = ERR_CSROW;
  1474. return;
  1475. }
  1476. /* CHIPKILL enabled */
  1477. if (pvt->nbcfg & NBCFG_CHIPKILL) {
  1478. err->channel = get_channel_from_ecc_syndrome(mci, err->syndrome);
  1479. if (err->channel < 0) {
  1480. /*
  1481. * Syndrome didn't map, so we don't know which of the
  1482. * 2 DIMMs is in error. So we need to ID 'both' of them
  1483. * as suspect.
  1484. */
  1485. amd64_mc_warn(err->src_mci, "unknown syndrome 0x%04x - "
  1486. "possible error reporting race\n",
  1487. err->syndrome);
  1488. err->err_code = ERR_CHANNEL;
  1489. return;
  1490. }
  1491. } else {
  1492. /*
  1493. * non-chipkill ecc mode
  1494. *
  1495. * The k8 documentation is unclear about how to determine the
  1496. * channel number when using non-chipkill memory. This method
  1497. * was obtained from email communication with someone at AMD.
  1498. * (Wish the email was placed in this comment - norsk)
  1499. */
  1500. err->channel = ((sys_addr & BIT(3)) != 0);
  1501. }
  1502. }
  1503. static int ddr2_cs_size(unsigned i, bool dct_width)
  1504. {
  1505. unsigned shift = 0;
  1506. if (i <= 2)
  1507. shift = i;
  1508. else if (!(i & 0x1))
  1509. shift = i >> 1;
  1510. else
  1511. shift = (i + 1) >> 1;
  1512. return 128 << (shift + !!dct_width);
  1513. }
  1514. static int k8_dbam_to_chip_select(struct amd64_pvt *pvt, u8 dct,
  1515. unsigned cs_mode, int cs_mask_nr)
  1516. {
  1517. u32 dclr = dct ? pvt->dclr1 : pvt->dclr0;
  1518. if (pvt->ext_model >= K8_REV_F) {
  1519. WARN_ON(cs_mode > 11);
  1520. return ddr2_cs_size(cs_mode, dclr & WIDTH_128);
  1521. }
  1522. else if (pvt->ext_model >= K8_REV_D) {
  1523. unsigned diff;
  1524. WARN_ON(cs_mode > 10);
  1525. /*
  1526. * the below calculation, besides trying to win an obfuscated C
  1527. * contest, maps cs_mode values to DIMM chip select sizes. The
  1528. * mappings are:
  1529. *
  1530. * cs_mode CS size (mb)
  1531. * ======= ============
  1532. * 0 32
  1533. * 1 64
  1534. * 2 128
  1535. * 3 128
  1536. * 4 256
  1537. * 5 512
  1538. * 6 256
  1539. * 7 512
  1540. * 8 1024
  1541. * 9 1024
  1542. * 10 2048
  1543. *
  1544. * Basically, it calculates a value with which to shift the
  1545. * smallest CS size of 32MB.
  1546. *
  1547. * ddr[23]_cs_size have a similar purpose.
  1548. */
  1549. diff = cs_mode/3 + (unsigned)(cs_mode > 5);
  1550. return 32 << (cs_mode - diff);
  1551. }
  1552. else {
  1553. WARN_ON(cs_mode > 6);
  1554. return 32 << cs_mode;
  1555. }
  1556. }
  1557. static int ddr3_cs_size(unsigned i, bool dct_width)
  1558. {
  1559. unsigned shift = 0;
  1560. int cs_size = 0;
  1561. if (i == 0 || i == 3 || i == 4)
  1562. cs_size = -1;
  1563. else if (i <= 2)
  1564. shift = i;
  1565. else if (i == 12)
  1566. shift = 7;
  1567. else if (!(i & 0x1))
  1568. shift = i >> 1;
  1569. else
  1570. shift = (i + 1) >> 1;
  1571. if (cs_size != -1)
  1572. cs_size = (128 * (1 << !!dct_width)) << shift;
  1573. return cs_size;
  1574. }
  1575. static int ddr3_lrdimm_cs_size(unsigned i, unsigned rank_multiply)
  1576. {
  1577. unsigned shift = 0;
  1578. int cs_size = 0;
  1579. if (i < 4 || i == 6)
  1580. cs_size = -1;
  1581. else if (i == 12)
  1582. shift = 7;
  1583. else if (!(i & 0x1))
  1584. shift = i >> 1;
  1585. else
  1586. shift = (i + 1) >> 1;
  1587. if (cs_size != -1)
  1588. cs_size = rank_multiply * (128 << shift);
  1589. return cs_size;
  1590. }
  1591. static int ddr4_cs_size(unsigned i)
  1592. {
  1593. int cs_size = 0;
  1594. if (i == 0)
  1595. cs_size = -1;
  1596. else if (i == 1)
  1597. cs_size = 1024;
  1598. else
  1599. /* Min cs_size = 1G */
  1600. cs_size = 1024 * (1 << (i >> 1));
  1601. return cs_size;
  1602. }
  1603. static int f10_dbam_to_chip_select(struct amd64_pvt *pvt, u8 dct,
  1604. unsigned cs_mode, int cs_mask_nr)
  1605. {
  1606. u32 dclr = dct ? pvt->dclr1 : pvt->dclr0;
  1607. WARN_ON(cs_mode > 11);
  1608. if (pvt->dchr0 & DDR3_MODE || pvt->dchr1 & DDR3_MODE)
  1609. return ddr3_cs_size(cs_mode, dclr & WIDTH_128);
  1610. else
  1611. return ddr2_cs_size(cs_mode, dclr & WIDTH_128);
  1612. }
  1613. /*
  1614. * F15h supports only 64bit DCT interfaces
  1615. */
  1616. static int f15_dbam_to_chip_select(struct amd64_pvt *pvt, u8 dct,
  1617. unsigned cs_mode, int cs_mask_nr)
  1618. {
  1619. WARN_ON(cs_mode > 12);
  1620. return ddr3_cs_size(cs_mode, false);
  1621. }
  1622. /* F15h M60h supports DDR4 mapping as well.. */
  1623. static int f15_m60h_dbam_to_chip_select(struct amd64_pvt *pvt, u8 dct,
  1624. unsigned cs_mode, int cs_mask_nr)
  1625. {
  1626. int cs_size;
  1627. u32 dcsm = pvt->csels[dct].csmasks[cs_mask_nr];
  1628. WARN_ON(cs_mode > 12);
  1629. if (pvt->dram_type == MEM_DDR4) {
  1630. if (cs_mode > 9)
  1631. return -1;
  1632. cs_size = ddr4_cs_size(cs_mode);
  1633. } else if (pvt->dram_type == MEM_LRDDR3) {
  1634. unsigned rank_multiply = dcsm & 0xf;
  1635. if (rank_multiply == 3)
  1636. rank_multiply = 4;
  1637. cs_size = ddr3_lrdimm_cs_size(cs_mode, rank_multiply);
  1638. } else {
  1639. /* Minimum cs size is 512mb for F15hM60h*/
  1640. if (cs_mode == 0x1)
  1641. return -1;
  1642. cs_size = ddr3_cs_size(cs_mode, false);
  1643. }
  1644. return cs_size;
  1645. }
  1646. /*
  1647. * F16h and F15h model 30h have only limited cs_modes.
  1648. */
  1649. static int f16_dbam_to_chip_select(struct amd64_pvt *pvt, u8 dct,
  1650. unsigned cs_mode, int cs_mask_nr)
  1651. {
  1652. WARN_ON(cs_mode > 12);
  1653. if (cs_mode == 6 || cs_mode == 8 ||
  1654. cs_mode == 9 || cs_mode == 12)
  1655. return -1;
  1656. else
  1657. return ddr3_cs_size(cs_mode, false);
  1658. }
  1659. static void read_dram_ctl_register(struct amd64_pvt *pvt)
  1660. {
  1661. if (pvt->fam == 0xf)
  1662. return;
  1663. if (!amd64_read_pci_cfg(pvt->F2, DCT_SEL_LO, &pvt->dct_sel_lo)) {
  1664. edac_dbg(0, "F2x110 (DCTSelLow): 0x%08x, High range addrs at: 0x%x\n",
  1665. pvt->dct_sel_lo, dct_sel_baseaddr(pvt));
  1666. edac_dbg(0, " DCTs operate in %s mode\n",
  1667. (dct_ganging_enabled(pvt) ? "ganged" : "unganged"));
  1668. if (!dct_ganging_enabled(pvt))
  1669. edac_dbg(0, " Address range split per DCT: %s\n",
  1670. (dct_high_range_enabled(pvt) ? "yes" : "no"));
  1671. edac_dbg(0, " data interleave for ECC: %s, DRAM cleared since last warm reset: %s\n",
  1672. (dct_data_intlv_enabled(pvt) ? "enabled" : "disabled"),
  1673. (dct_memory_cleared(pvt) ? "yes" : "no"));
  1674. edac_dbg(0, " channel interleave: %s, "
  1675. "interleave bits selector: 0x%x\n",
  1676. (dct_interleave_enabled(pvt) ? "enabled" : "disabled"),
  1677. dct_sel_interleave_addr(pvt));
  1678. }
  1679. amd64_read_pci_cfg(pvt->F2, DCT_SEL_HI, &pvt->dct_sel_hi);
  1680. }
  1681. /*
  1682. * Determine channel (DCT) based on the interleaving mode (see F15h M30h BKDG,
  1683. * 2.10.12 Memory Interleaving Modes).
  1684. */
  1685. static u8 f15_m30h_determine_channel(struct amd64_pvt *pvt, u64 sys_addr,
  1686. u8 intlv_en, int num_dcts_intlv,
  1687. u32 dct_sel)
  1688. {
  1689. u8 channel = 0;
  1690. u8 select;
  1691. if (!(intlv_en))
  1692. return (u8)(dct_sel);
  1693. if (num_dcts_intlv == 2) {
  1694. select = (sys_addr >> 8) & 0x3;
  1695. channel = select ? 0x3 : 0;
  1696. } else if (num_dcts_intlv == 4) {
  1697. u8 intlv_addr = dct_sel_interleave_addr(pvt);
  1698. switch (intlv_addr) {
  1699. case 0x4:
  1700. channel = (sys_addr >> 8) & 0x3;
  1701. break;
  1702. case 0x5:
  1703. channel = (sys_addr >> 9) & 0x3;
  1704. break;
  1705. }
  1706. }
  1707. return channel;
  1708. }
  1709. /*
  1710. * Determine channel (DCT) based on the interleaving mode: F10h BKDG, 2.8.9 Memory
  1711. * Interleaving Modes.
  1712. */
  1713. static u8 f1x_determine_channel(struct amd64_pvt *pvt, u64 sys_addr,
  1714. bool hi_range_sel, u8 intlv_en)
  1715. {
  1716. u8 dct_sel_high = (pvt->dct_sel_lo >> 1) & 1;
  1717. if (dct_ganging_enabled(pvt))
  1718. return 0;
  1719. if (hi_range_sel)
  1720. return dct_sel_high;
  1721. /*
  1722. * see F2x110[DctSelIntLvAddr] - channel interleave mode
  1723. */
  1724. if (dct_interleave_enabled(pvt)) {
  1725. u8 intlv_addr = dct_sel_interleave_addr(pvt);
  1726. /* return DCT select function: 0=DCT0, 1=DCT1 */
  1727. if (!intlv_addr)
  1728. return sys_addr >> 6 & 1;
  1729. if (intlv_addr & 0x2) {
  1730. u8 shift = intlv_addr & 0x1 ? 9 : 6;
  1731. u32 temp = hweight_long((u32) ((sys_addr >> 16) & 0x1F)) & 1;
  1732. return ((sys_addr >> shift) & 1) ^ temp;
  1733. }
  1734. if (intlv_addr & 0x4) {
  1735. u8 shift = intlv_addr & 0x1 ? 9 : 8;
  1736. return (sys_addr >> shift) & 1;
  1737. }
  1738. return (sys_addr >> (12 + hweight8(intlv_en))) & 1;
  1739. }
  1740. if (dct_high_range_enabled(pvt))
  1741. return ~dct_sel_high & 1;
  1742. return 0;
  1743. }
  1744. /* Convert the sys_addr to the normalized DCT address */
  1745. static u64 f1x_get_norm_dct_addr(struct amd64_pvt *pvt, u8 range,
  1746. u64 sys_addr, bool hi_rng,
  1747. u32 dct_sel_base_addr)
  1748. {
  1749. u64 chan_off;
  1750. u64 dram_base = get_dram_base(pvt, range);
  1751. u64 hole_off = f10_dhar_offset(pvt);
  1752. u64 dct_sel_base_off = (u64)(pvt->dct_sel_hi & 0xFFFFFC00) << 16;
  1753. if (hi_rng) {
  1754. /*
  1755. * if
  1756. * base address of high range is below 4Gb
  1757. * (bits [47:27] at [31:11])
  1758. * DRAM address space on this DCT is hoisted above 4Gb &&
  1759. * sys_addr > 4Gb
  1760. *
  1761. * remove hole offset from sys_addr
  1762. * else
  1763. * remove high range offset from sys_addr
  1764. */
  1765. if ((!(dct_sel_base_addr >> 16) ||
  1766. dct_sel_base_addr < dhar_base(pvt)) &&
  1767. dhar_valid(pvt) &&
  1768. (sys_addr >= BIT_64(32)))
  1769. chan_off = hole_off;
  1770. else
  1771. chan_off = dct_sel_base_off;
  1772. } else {
  1773. /*
  1774. * if
  1775. * we have a valid hole &&
  1776. * sys_addr > 4Gb
  1777. *
  1778. * remove hole
  1779. * else
  1780. * remove dram base to normalize to DCT address
  1781. */
  1782. if (dhar_valid(pvt) && (sys_addr >= BIT_64(32)))
  1783. chan_off = hole_off;
  1784. else
  1785. chan_off = dram_base;
  1786. }
  1787. return (sys_addr & GENMASK_ULL(47,6)) - (chan_off & GENMASK_ULL(47,23));
  1788. }
  1789. /*
  1790. * checks if the csrow passed in is marked as SPARED, if so returns the new
  1791. * spare row
  1792. */
  1793. static int f10_process_possible_spare(struct amd64_pvt *pvt, u8 dct, int csrow)
  1794. {
  1795. int tmp_cs;
  1796. if (online_spare_swap_done(pvt, dct) &&
  1797. csrow == online_spare_bad_dramcs(pvt, dct)) {
  1798. for_each_chip_select(tmp_cs, dct, pvt) {
  1799. if (chip_select_base(tmp_cs, dct, pvt) & 0x2) {
  1800. csrow = tmp_cs;
  1801. break;
  1802. }
  1803. }
  1804. }
  1805. return csrow;
  1806. }
  1807. /*
  1808. * Iterate over the DRAM DCT "base" and "mask" registers looking for a
  1809. * SystemAddr match on the specified 'ChannelSelect' and 'NodeID'
  1810. *
  1811. * Return:
  1812. * -EINVAL: NOT FOUND
  1813. * 0..csrow = Chip-Select Row
  1814. */
  1815. static int f1x_lookup_addr_in_dct(u64 in_addr, u8 nid, u8 dct)
  1816. {
  1817. struct mem_ctl_info *mci;
  1818. struct amd64_pvt *pvt;
  1819. u64 cs_base, cs_mask;
  1820. int cs_found = -EINVAL;
  1821. int csrow;
  1822. mci = edac_mc_find(nid);
  1823. if (!mci)
  1824. return cs_found;
  1825. pvt = mci->pvt_info;
  1826. edac_dbg(1, "input addr: 0x%llx, DCT: %d\n", in_addr, dct);
  1827. for_each_chip_select(csrow, dct, pvt) {
  1828. if (!csrow_enabled(csrow, dct, pvt))
  1829. continue;
  1830. get_cs_base_and_mask(pvt, csrow, dct, &cs_base, &cs_mask);
  1831. edac_dbg(1, " CSROW=%d CSBase=0x%llx CSMask=0x%llx\n",
  1832. csrow, cs_base, cs_mask);
  1833. cs_mask = ~cs_mask;
  1834. edac_dbg(1, " (InputAddr & ~CSMask)=0x%llx (CSBase & ~CSMask)=0x%llx\n",
  1835. (in_addr & cs_mask), (cs_base & cs_mask));
  1836. if ((in_addr & cs_mask) == (cs_base & cs_mask)) {
  1837. if (pvt->fam == 0x15 && pvt->model >= 0x30) {
  1838. cs_found = csrow;
  1839. break;
  1840. }
  1841. cs_found = f10_process_possible_spare(pvt, dct, csrow);
  1842. edac_dbg(1, " MATCH csrow=%d\n", cs_found);
  1843. break;
  1844. }
  1845. }
  1846. return cs_found;
  1847. }
  1848. /*
  1849. * See F2x10C. Non-interleaved graphics framebuffer memory under the 16G is
  1850. * swapped with a region located at the bottom of memory so that the GPU can use
  1851. * the interleaved region and thus two channels.
  1852. */
  1853. static u64 f1x_swap_interleaved_region(struct amd64_pvt *pvt, u64 sys_addr)
  1854. {
  1855. u32 swap_reg, swap_base, swap_limit, rgn_size, tmp_addr;
  1856. if (pvt->fam == 0x10) {
  1857. /* only revC3 and revE have that feature */
  1858. if (pvt->model < 4 || (pvt->model < 0xa && pvt->stepping < 3))
  1859. return sys_addr;
  1860. }
  1861. amd64_read_pci_cfg(pvt->F2, SWAP_INTLV_REG, &swap_reg);
  1862. if (!(swap_reg & 0x1))
  1863. return sys_addr;
  1864. swap_base = (swap_reg >> 3) & 0x7f;
  1865. swap_limit = (swap_reg >> 11) & 0x7f;
  1866. rgn_size = (swap_reg >> 20) & 0x7f;
  1867. tmp_addr = sys_addr >> 27;
  1868. if (!(sys_addr >> 34) &&
  1869. (((tmp_addr >= swap_base) &&
  1870. (tmp_addr <= swap_limit)) ||
  1871. (tmp_addr < rgn_size)))
  1872. return sys_addr ^ (u64)swap_base << 27;
  1873. return sys_addr;
  1874. }
  1875. /* For a given @dram_range, check if @sys_addr falls within it. */
  1876. static int f1x_match_to_this_node(struct amd64_pvt *pvt, unsigned range,
  1877. u64 sys_addr, int *chan_sel)
  1878. {
  1879. int cs_found = -EINVAL;
  1880. u64 chan_addr;
  1881. u32 dct_sel_base;
  1882. u8 channel;
  1883. bool high_range = false;
  1884. u8 node_id = dram_dst_node(pvt, range);
  1885. u8 intlv_en = dram_intlv_en(pvt, range);
  1886. u32 intlv_sel = dram_intlv_sel(pvt, range);
  1887. edac_dbg(1, "(range %d) SystemAddr= 0x%llx Limit=0x%llx\n",
  1888. range, sys_addr, get_dram_limit(pvt, range));
  1889. if (dhar_valid(pvt) &&
  1890. dhar_base(pvt) <= sys_addr &&
  1891. sys_addr < BIT_64(32)) {
  1892. amd64_warn("Huh? Address is in the MMIO hole: 0x%016llx\n",
  1893. sys_addr);
  1894. return -EINVAL;
  1895. }
  1896. if (intlv_en && (intlv_sel != ((sys_addr >> 12) & intlv_en)))
  1897. return -EINVAL;
  1898. sys_addr = f1x_swap_interleaved_region(pvt, sys_addr);
  1899. dct_sel_base = dct_sel_baseaddr(pvt);
  1900. /*
  1901. * check whether addresses >= DctSelBaseAddr[47:27] are to be used to
  1902. * select between DCT0 and DCT1.
  1903. */
  1904. if (dct_high_range_enabled(pvt) &&
  1905. !dct_ganging_enabled(pvt) &&
  1906. ((sys_addr >> 27) >= (dct_sel_base >> 11)))
  1907. high_range = true;
  1908. channel = f1x_determine_channel(pvt, sys_addr, high_range, intlv_en);
  1909. chan_addr = f1x_get_norm_dct_addr(pvt, range, sys_addr,
  1910. high_range, dct_sel_base);
  1911. /* Remove node interleaving, see F1x120 */
  1912. if (intlv_en)
  1913. chan_addr = ((chan_addr >> (12 + hweight8(intlv_en))) << 12) |
  1914. (chan_addr & 0xfff);
  1915. /* remove channel interleave */
  1916. if (dct_interleave_enabled(pvt) &&
  1917. !dct_high_range_enabled(pvt) &&
  1918. !dct_ganging_enabled(pvt)) {
  1919. if (dct_sel_interleave_addr(pvt) != 1) {
  1920. if (dct_sel_interleave_addr(pvt) == 0x3)
  1921. /* hash 9 */
  1922. chan_addr = ((chan_addr >> 10) << 9) |
  1923. (chan_addr & 0x1ff);
  1924. else
  1925. /* A[6] or hash 6 */
  1926. chan_addr = ((chan_addr >> 7) << 6) |
  1927. (chan_addr & 0x3f);
  1928. } else
  1929. /* A[12] */
  1930. chan_addr = ((chan_addr >> 13) << 12) |
  1931. (chan_addr & 0xfff);
  1932. }
  1933. edac_dbg(1, " Normalized DCT addr: 0x%llx\n", chan_addr);
  1934. cs_found = f1x_lookup_addr_in_dct(chan_addr, node_id, channel);
  1935. if (cs_found >= 0)
  1936. *chan_sel = channel;
  1937. return cs_found;
  1938. }
  1939. static int f15_m30h_match_to_this_node(struct amd64_pvt *pvt, unsigned range,
  1940. u64 sys_addr, int *chan_sel)
  1941. {
  1942. int cs_found = -EINVAL;
  1943. int num_dcts_intlv = 0;
  1944. u64 chan_addr, chan_offset;
  1945. u64 dct_base, dct_limit;
  1946. u32 dct_cont_base_reg, dct_cont_limit_reg, tmp;
  1947. u8 channel, alias_channel, leg_mmio_hole, dct_sel, dct_offset_en;
  1948. u64 dhar_offset = f10_dhar_offset(pvt);
  1949. u8 intlv_addr = dct_sel_interleave_addr(pvt);
  1950. u8 node_id = dram_dst_node(pvt, range);
  1951. u8 intlv_en = dram_intlv_en(pvt, range);
  1952. amd64_read_pci_cfg(pvt->F1, DRAM_CONT_BASE, &dct_cont_base_reg);
  1953. amd64_read_pci_cfg(pvt->F1, DRAM_CONT_LIMIT, &dct_cont_limit_reg);
  1954. dct_offset_en = (u8) ((dct_cont_base_reg >> 3) & BIT(0));
  1955. dct_sel = (u8) ((dct_cont_base_reg >> 4) & 0x7);
  1956. edac_dbg(1, "(range %d) SystemAddr= 0x%llx Limit=0x%llx\n",
  1957. range, sys_addr, get_dram_limit(pvt, range));
  1958. if (!(get_dram_base(pvt, range) <= sys_addr) &&
  1959. !(get_dram_limit(pvt, range) >= sys_addr))
  1960. return -EINVAL;
  1961. if (dhar_valid(pvt) &&
  1962. dhar_base(pvt) <= sys_addr &&
  1963. sys_addr < BIT_64(32)) {
  1964. amd64_warn("Huh? Address is in the MMIO hole: 0x%016llx\n",
  1965. sys_addr);
  1966. return -EINVAL;
  1967. }
  1968. /* Verify sys_addr is within DCT Range. */
  1969. dct_base = (u64) dct_sel_baseaddr(pvt);
  1970. dct_limit = (dct_cont_limit_reg >> 11) & 0x1FFF;
  1971. if (!(dct_cont_base_reg & BIT(0)) &&
  1972. !(dct_base <= (sys_addr >> 27) &&
  1973. dct_limit >= (sys_addr >> 27)))
  1974. return -EINVAL;
  1975. /* Verify number of dct's that participate in channel interleaving. */
  1976. num_dcts_intlv = (int) hweight8(intlv_en);
  1977. if (!(num_dcts_intlv % 2 == 0) || (num_dcts_intlv > 4))
  1978. return -EINVAL;
  1979. if (pvt->model >= 0x60)
  1980. channel = f1x_determine_channel(pvt, sys_addr, false, intlv_en);
  1981. else
  1982. channel = f15_m30h_determine_channel(pvt, sys_addr, intlv_en,
  1983. num_dcts_intlv, dct_sel);
  1984. /* Verify we stay within the MAX number of channels allowed */
  1985. if (channel > 3)
  1986. return -EINVAL;
  1987. leg_mmio_hole = (u8) (dct_cont_base_reg >> 1 & BIT(0));
  1988. /* Get normalized DCT addr */
  1989. if (leg_mmio_hole && (sys_addr >= BIT_64(32)))
  1990. chan_offset = dhar_offset;
  1991. else
  1992. chan_offset = dct_base << 27;
  1993. chan_addr = sys_addr - chan_offset;
  1994. /* remove channel interleave */
  1995. if (num_dcts_intlv == 2) {
  1996. if (intlv_addr == 0x4)
  1997. chan_addr = ((chan_addr >> 9) << 8) |
  1998. (chan_addr & 0xff);
  1999. else if (intlv_addr == 0x5)
  2000. chan_addr = ((chan_addr >> 10) << 9) |
  2001. (chan_addr & 0x1ff);
  2002. else
  2003. return -EINVAL;
  2004. } else if (num_dcts_intlv == 4) {
  2005. if (intlv_addr == 0x4)
  2006. chan_addr = ((chan_addr >> 10) << 8) |
  2007. (chan_addr & 0xff);
  2008. else if (intlv_addr == 0x5)
  2009. chan_addr = ((chan_addr >> 11) << 9) |
  2010. (chan_addr & 0x1ff);
  2011. else
  2012. return -EINVAL;
  2013. }
  2014. if (dct_offset_en) {
  2015. amd64_read_pci_cfg(pvt->F1,
  2016. DRAM_CONT_HIGH_OFF + (int) channel * 4,
  2017. &tmp);
  2018. chan_addr += (u64) ((tmp >> 11) & 0xfff) << 27;
  2019. }
  2020. f15h_select_dct(pvt, channel);
  2021. edac_dbg(1, " Normalized DCT addr: 0x%llx\n", chan_addr);
  2022. /*
  2023. * Find Chip select:
  2024. * if channel = 3, then alias it to 1. This is because, in F15 M30h,
  2025. * there is support for 4 DCT's, but only 2 are currently functional.
  2026. * They are DCT0 and DCT3. But we have read all registers of DCT3 into
  2027. * pvt->csels[1]. So we need to use '1' here to get correct info.
  2028. * Refer F15 M30h BKDG Section 2.10 and 2.10.3 for clarifications.
  2029. */
  2030. alias_channel = (channel == 3) ? 1 : channel;
  2031. cs_found = f1x_lookup_addr_in_dct(chan_addr, node_id, alias_channel);
  2032. if (cs_found >= 0)
  2033. *chan_sel = alias_channel;
  2034. return cs_found;
  2035. }
  2036. static int f1x_translate_sysaddr_to_cs(struct amd64_pvt *pvt,
  2037. u64 sys_addr,
  2038. int *chan_sel)
  2039. {
  2040. int cs_found = -EINVAL;
  2041. unsigned range;
  2042. for (range = 0; range < DRAM_RANGES; range++) {
  2043. if (!dram_rw(pvt, range))
  2044. continue;
  2045. if (pvt->fam == 0x15 && pvt->model >= 0x30)
  2046. cs_found = f15_m30h_match_to_this_node(pvt, range,
  2047. sys_addr,
  2048. chan_sel);
  2049. else if ((get_dram_base(pvt, range) <= sys_addr) &&
  2050. (get_dram_limit(pvt, range) >= sys_addr)) {
  2051. cs_found = f1x_match_to_this_node(pvt, range,
  2052. sys_addr, chan_sel);
  2053. if (cs_found >= 0)
  2054. break;
  2055. }
  2056. }
  2057. return cs_found;
  2058. }
  2059. /*
  2060. * For reference see "2.8.5 Routing DRAM Requests" in F10 BKDG. This code maps
  2061. * a @sys_addr to NodeID, DCT (channel) and chip select (CSROW).
  2062. *
  2063. * The @sys_addr is usually an error address received from the hardware
  2064. * (MCX_ADDR).
  2065. */
  2066. static void f1x_map_sysaddr_to_csrow(struct mem_ctl_info *mci, u64 sys_addr,
  2067. struct err_info *err)
  2068. {
  2069. struct amd64_pvt *pvt = mci->pvt_info;
  2070. error_address_to_page_and_offset(sys_addr, err);
  2071. err->csrow = f1x_translate_sysaddr_to_cs(pvt, sys_addr, &err->channel);
  2072. if (err->csrow < 0) {
  2073. err->err_code = ERR_CSROW;
  2074. return;
  2075. }
  2076. /*
  2077. * We need the syndromes for channel detection only when we're
  2078. * ganged. Otherwise @chan should already contain the channel at
  2079. * this point.
  2080. */
  2081. if (dct_ganging_enabled(pvt))
  2082. err->channel = get_channel_from_ecc_syndrome(mci, err->syndrome);
  2083. }
  2084. /*
  2085. * These are tables of eigenvectors (one per line) which can be used for the
  2086. * construction of the syndrome tables. The modified syndrome search algorithm
  2087. * uses those to find the symbol in error and thus the DIMM.
  2088. *
  2089. * Algorithm courtesy of Ross LaFetra from AMD.
  2090. */
  2091. static const u16 x4_vectors[] = {
  2092. 0x2f57, 0x1afe, 0x66cc, 0xdd88,
  2093. 0x11eb, 0x3396, 0x7f4c, 0xeac8,
  2094. 0x0001, 0x0002, 0x0004, 0x0008,
  2095. 0x1013, 0x3032, 0x4044, 0x8088,
  2096. 0x106b, 0x30d6, 0x70fc, 0xe0a8,
  2097. 0x4857, 0xc4fe, 0x13cc, 0x3288,
  2098. 0x1ac5, 0x2f4a, 0x5394, 0xa1e8,
  2099. 0x1f39, 0x251e, 0xbd6c, 0x6bd8,
  2100. 0x15c1, 0x2a42, 0x89ac, 0x4758,
  2101. 0x2b03, 0x1602, 0x4f0c, 0xca08,
  2102. 0x1f07, 0x3a0e, 0x6b04, 0xbd08,
  2103. 0x8ba7, 0x465e, 0x244c, 0x1cc8,
  2104. 0x2b87, 0x164e, 0x642c, 0xdc18,
  2105. 0x40b9, 0x80de, 0x1094, 0x20e8,
  2106. 0x27db, 0x1eb6, 0x9dac, 0x7b58,
  2107. 0x11c1, 0x2242, 0x84ac, 0x4c58,
  2108. 0x1be5, 0x2d7a, 0x5e34, 0xa718,
  2109. 0x4b39, 0x8d1e, 0x14b4, 0x28d8,
  2110. 0x4c97, 0xc87e, 0x11fc, 0x33a8,
  2111. 0x8e97, 0x497e, 0x2ffc, 0x1aa8,
  2112. 0x16b3, 0x3d62, 0x4f34, 0x8518,
  2113. 0x1e2f, 0x391a, 0x5cac, 0xf858,
  2114. 0x1d9f, 0x3b7a, 0x572c, 0xfe18,
  2115. 0x15f5, 0x2a5a, 0x5264, 0xa3b8,
  2116. 0x1dbb, 0x3b66, 0x715c, 0xe3f8,
  2117. 0x4397, 0xc27e, 0x17fc, 0x3ea8,
  2118. 0x1617, 0x3d3e, 0x6464, 0xb8b8,
  2119. 0x23ff, 0x12aa, 0xab6c, 0x56d8,
  2120. 0x2dfb, 0x1ba6, 0x913c, 0x7328,
  2121. 0x185d, 0x2ca6, 0x7914, 0x9e28,
  2122. 0x171b, 0x3e36, 0x7d7c, 0xebe8,
  2123. 0x4199, 0x82ee, 0x19f4, 0x2e58,
  2124. 0x4807, 0xc40e, 0x130c, 0x3208,
  2125. 0x1905, 0x2e0a, 0x5804, 0xac08,
  2126. 0x213f, 0x132a, 0xadfc, 0x5ba8,
  2127. 0x19a9, 0x2efe, 0xb5cc, 0x6f88,
  2128. };
  2129. static const u16 x8_vectors[] = {
  2130. 0x0145, 0x028a, 0x2374, 0x43c8, 0xa1f0, 0x0520, 0x0a40, 0x1480,
  2131. 0x0211, 0x0422, 0x0844, 0x1088, 0x01b0, 0x44e0, 0x23c0, 0xed80,
  2132. 0x1011, 0x0116, 0x022c, 0x0458, 0x08b0, 0x8c60, 0x2740, 0x4e80,
  2133. 0x0411, 0x0822, 0x1044, 0x0158, 0x02b0, 0x2360, 0x46c0, 0xab80,
  2134. 0x0811, 0x1022, 0x012c, 0x0258, 0x04b0, 0x4660, 0x8cc0, 0x2780,
  2135. 0x2071, 0x40e2, 0xa0c4, 0x0108, 0x0210, 0x0420, 0x0840, 0x1080,
  2136. 0x4071, 0x80e2, 0x0104, 0x0208, 0x0410, 0x0820, 0x1040, 0x2080,
  2137. 0x8071, 0x0102, 0x0204, 0x0408, 0x0810, 0x1020, 0x2040, 0x4080,
  2138. 0x019d, 0x03d6, 0x136c, 0x2198, 0x50b0, 0xb2e0, 0x0740, 0x0e80,
  2139. 0x0189, 0x03ea, 0x072c, 0x0e58, 0x1cb0, 0x56e0, 0x37c0, 0xf580,
  2140. 0x01fd, 0x0376, 0x06ec, 0x0bb8, 0x1110, 0x2220, 0x4440, 0x8880,
  2141. 0x0163, 0x02c6, 0x1104, 0x0758, 0x0eb0, 0x2be0, 0x6140, 0xc280,
  2142. 0x02fd, 0x01c6, 0x0b5c, 0x1108, 0x07b0, 0x25a0, 0x8840, 0x6180,
  2143. 0x0801, 0x012e, 0x025c, 0x04b8, 0x1370, 0x26e0, 0x57c0, 0xb580,
  2144. 0x0401, 0x0802, 0x015c, 0x02b8, 0x22b0, 0x13e0, 0x7140, 0xe280,
  2145. 0x0201, 0x0402, 0x0804, 0x01b8, 0x11b0, 0x31a0, 0x8040, 0x7180,
  2146. 0x0101, 0x0202, 0x0404, 0x0808, 0x1010, 0x2020, 0x4040, 0x8080,
  2147. 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
  2148. 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000, 0x8000,
  2149. };
  2150. static int decode_syndrome(u16 syndrome, const u16 *vectors, unsigned num_vecs,
  2151. unsigned v_dim)
  2152. {
  2153. unsigned int i, err_sym;
  2154. for (err_sym = 0; err_sym < num_vecs / v_dim; err_sym++) {
  2155. u16 s = syndrome;
  2156. unsigned v_idx = err_sym * v_dim;
  2157. unsigned v_end = (err_sym + 1) * v_dim;
  2158. /* walk over all 16 bits of the syndrome */
  2159. for (i = 1; i < (1U << 16); i <<= 1) {
  2160. /* if bit is set in that eigenvector... */
  2161. if (v_idx < v_end && vectors[v_idx] & i) {
  2162. u16 ev_comp = vectors[v_idx++];
  2163. /* ... and bit set in the modified syndrome, */
  2164. if (s & i) {
  2165. /* remove it. */
  2166. s ^= ev_comp;
  2167. if (!s)
  2168. return err_sym;
  2169. }
  2170. } else if (s & i)
  2171. /* can't get to zero, move to next symbol */
  2172. break;
  2173. }
  2174. }
  2175. edac_dbg(0, "syndrome(%x) not found\n", syndrome);
  2176. return -1;
  2177. }
  2178. static int map_err_sym_to_channel(int err_sym, int sym_size)
  2179. {
  2180. if (sym_size == 4)
  2181. switch (err_sym) {
  2182. case 0x20:
  2183. case 0x21:
  2184. return 0;
  2185. case 0x22:
  2186. case 0x23:
  2187. return 1;
  2188. default:
  2189. return err_sym >> 4;
  2190. }
  2191. /* x8 symbols */
  2192. else
  2193. switch (err_sym) {
  2194. /* imaginary bits not in a DIMM */
  2195. case 0x10:
  2196. WARN(1, KERN_ERR "Invalid error symbol: 0x%x\n",
  2197. err_sym);
  2198. return -1;
  2199. case 0x11:
  2200. return 0;
  2201. case 0x12:
  2202. return 1;
  2203. default:
  2204. return err_sym >> 3;
  2205. }
  2206. return -1;
  2207. }
  2208. static int get_channel_from_ecc_syndrome(struct mem_ctl_info *mci, u16 syndrome)
  2209. {
  2210. struct amd64_pvt *pvt = mci->pvt_info;
  2211. int err_sym = -1;
  2212. if (pvt->ecc_sym_sz == 8)
  2213. err_sym = decode_syndrome(syndrome, x8_vectors,
  2214. ARRAY_SIZE(x8_vectors),
  2215. pvt->ecc_sym_sz);
  2216. else if (pvt->ecc_sym_sz == 4)
  2217. err_sym = decode_syndrome(syndrome, x4_vectors,
  2218. ARRAY_SIZE(x4_vectors),
  2219. pvt->ecc_sym_sz);
  2220. else {
  2221. amd64_warn("Illegal syndrome type: %u\n", pvt->ecc_sym_sz);
  2222. return err_sym;
  2223. }
  2224. return map_err_sym_to_channel(err_sym, pvt->ecc_sym_sz);
  2225. }
  2226. static void __log_ecc_error(struct mem_ctl_info *mci, struct err_info *err,
  2227. u8 ecc_type)
  2228. {
  2229. enum hw_event_mc_err_type err_type;
  2230. const char *string;
  2231. if (ecc_type == 2)
  2232. err_type = HW_EVENT_ERR_CORRECTED;
  2233. else if (ecc_type == 1)
  2234. err_type = HW_EVENT_ERR_UNCORRECTED;
  2235. else if (ecc_type == 3)
  2236. err_type = HW_EVENT_ERR_DEFERRED;
  2237. else {
  2238. WARN(1, "Something is rotten in the state of Denmark.\n");
  2239. return;
  2240. }
  2241. switch (err->err_code) {
  2242. case DECODE_OK:
  2243. string = "";
  2244. break;
  2245. case ERR_NODE:
  2246. string = "Failed to map error addr to a node";
  2247. break;
  2248. case ERR_CSROW:
  2249. string = "Failed to map error addr to a csrow";
  2250. break;
  2251. case ERR_CHANNEL:
  2252. string = "Unknown syndrome - possible error reporting race";
  2253. break;
  2254. case ERR_SYND:
  2255. string = "MCA_SYND not valid - unknown syndrome and csrow";
  2256. break;
  2257. case ERR_NORM_ADDR:
  2258. string = "Cannot decode normalized address";
  2259. break;
  2260. default:
  2261. string = "WTF error";
  2262. break;
  2263. }
  2264. edac_mc_handle_error(err_type, mci, 1,
  2265. err->page, err->offset, err->syndrome,
  2266. err->csrow, err->channel, -1,
  2267. string, "");
  2268. }
  2269. static inline void decode_bus_error(int node_id, struct mce *m)
  2270. {
  2271. struct mem_ctl_info *mci;
  2272. struct amd64_pvt *pvt;
  2273. u8 ecc_type = (m->status >> 45) & 0x3;
  2274. u8 xec = XEC(m->status, 0x1f);
  2275. u16 ec = EC(m->status);
  2276. u64 sys_addr;
  2277. struct err_info err;
  2278. mci = edac_mc_find(node_id);
  2279. if (!mci)
  2280. return;
  2281. pvt = mci->pvt_info;
  2282. /* Bail out early if this was an 'observed' error */
  2283. if (PP(ec) == NBSL_PP_OBS)
  2284. return;
  2285. /* Do only ECC errors */
  2286. if (xec && xec != F10_NBSL_EXT_ERR_ECC)
  2287. return;
  2288. memset(&err, 0, sizeof(err));
  2289. sys_addr = get_error_address(pvt, m);
  2290. if (ecc_type == 2)
  2291. err.syndrome = extract_syndrome(m->status);
  2292. pvt->ops->map_sysaddr_to_csrow(mci, sys_addr, &err);
  2293. __log_ecc_error(mci, &err, ecc_type);
  2294. }
  2295. /*
  2296. * To find the UMC channel represented by this bank we need to match on its
  2297. * instance_id. The instance_id of a bank is held in the lower 32 bits of its
  2298. * IPID.
  2299. *
  2300. * Currently, we can derive the channel number by looking at the 6th nibble in
  2301. * the instance_id. For example, instance_id=0xYXXXXX where Y is the channel
  2302. * number.
  2303. *
  2304. * For DRAM ECC errors, the Chip Select number is given in bits [2:0] of
  2305. * the MCA_SYND[ErrorInformation] field.
  2306. */
  2307. static void umc_get_err_info(struct mce *m, struct err_info *err)
  2308. {
  2309. err->channel = (m->ipid & GENMASK(31, 0)) >> 20;
  2310. err->csrow = m->synd & 0x7;
  2311. }
  2312. static void decode_umc_error(int node_id, struct mce *m)
  2313. {
  2314. u8 ecc_type = (m->status >> 45) & 0x3;
  2315. struct mem_ctl_info *mci;
  2316. unsigned long sys_addr;
  2317. struct amd64_pvt *pvt;
  2318. struct atl_err a_err;
  2319. struct err_info err;
  2320. node_id = fixup_node_id(node_id, m);
  2321. mci = edac_mc_find(node_id);
  2322. if (!mci)
  2323. return;
  2324. pvt = mci->pvt_info;
  2325. memset(&err, 0, sizeof(err));
  2326. if (m->status & MCI_STATUS_DEFERRED)
  2327. ecc_type = 3;
  2328. if (!(m->status & MCI_STATUS_SYNDV)) {
  2329. err.err_code = ERR_SYND;
  2330. goto log_error;
  2331. }
  2332. if (ecc_type == 2) {
  2333. u8 length = (m->synd >> 18) & 0x3f;
  2334. if (length)
  2335. err.syndrome = (m->synd >> 32) & GENMASK(length - 1, 0);
  2336. else
  2337. err.err_code = ERR_CHANNEL;
  2338. }
  2339. pvt->ops->get_err_info(m, &err);
  2340. a_err.addr = m->addr;
  2341. a_err.ipid = m->ipid;
  2342. a_err.cpu = m->extcpu;
  2343. sys_addr = amd_convert_umc_mca_addr_to_sys_addr(&a_err);
  2344. if (IS_ERR_VALUE(sys_addr)) {
  2345. err.err_code = ERR_NORM_ADDR;
  2346. goto log_error;
  2347. }
  2348. error_address_to_page_and_offset(sys_addr, &err);
  2349. log_error:
  2350. __log_ecc_error(mci, &err, ecc_type);
  2351. }
  2352. /*
  2353. * Use pvt->F3 which contains the F3 CPU PCI device to get the related
  2354. * F1 (AddrMap) and F2 (Dct) devices. Return negative value on error.
  2355. */
  2356. static int
  2357. reserve_mc_sibling_devs(struct amd64_pvt *pvt, u16 pci_id1, u16 pci_id2)
  2358. {
  2359. /* Reserve the ADDRESS MAP Device */
  2360. pvt->F1 = pci_get_related_function(pvt->F3->vendor, pci_id1, pvt->F3);
  2361. if (!pvt->F1) {
  2362. edac_dbg(1, "F1 not found: device 0x%x\n", pci_id1);
  2363. return -ENODEV;
  2364. }
  2365. /* Reserve the DCT Device */
  2366. pvt->F2 = pci_get_related_function(pvt->F3->vendor, pci_id2, pvt->F3);
  2367. if (!pvt->F2) {
  2368. pci_dev_put(pvt->F1);
  2369. pvt->F1 = NULL;
  2370. edac_dbg(1, "F2 not found: device 0x%x\n", pci_id2);
  2371. return -ENODEV;
  2372. }
  2373. if (!pci_ctl_dev)
  2374. pci_ctl_dev = &pvt->F2->dev;
  2375. edac_dbg(1, "F1: %s\n", pci_name(pvt->F1));
  2376. edac_dbg(1, "F2: %s\n", pci_name(pvt->F2));
  2377. edac_dbg(1, "F3: %s\n", pci_name(pvt->F3));
  2378. return 0;
  2379. }
  2380. static void determine_ecc_sym_sz(struct amd64_pvt *pvt)
  2381. {
  2382. pvt->ecc_sym_sz = 4;
  2383. if (pvt->fam >= 0x10) {
  2384. u32 tmp;
  2385. amd64_read_pci_cfg(pvt->F3, EXT_NB_MCA_CFG, &tmp);
  2386. /* F16h has only DCT0, so no need to read dbam1. */
  2387. if (pvt->fam != 0x16)
  2388. amd64_read_dct_pci_cfg(pvt, 1, DBAM0, &pvt->dbam1);
  2389. /* F10h, revD and later can do x8 ECC too. */
  2390. if ((pvt->fam > 0x10 || pvt->model > 7) && tmp & BIT(25))
  2391. pvt->ecc_sym_sz = 8;
  2392. }
  2393. }
  2394. /*
  2395. * Retrieve the hardware registers of the memory controller.
  2396. */
  2397. static void umc_read_mc_regs(struct amd64_pvt *pvt)
  2398. {
  2399. u8 nid = pvt->mc_node_id;
  2400. struct amd64_umc *umc;
  2401. u32 i, tmp, umc_base;
  2402. /* Read registers from each UMC */
  2403. for_each_umc(i) {
  2404. umc_base = get_umc_base(i);
  2405. umc = &pvt->umc[i];
  2406. if (!amd_smn_read(nid, umc_base + get_umc_reg(pvt, UMCCH_DIMM_CFG), &tmp))
  2407. umc->dimm_cfg = tmp;
  2408. if (!amd_smn_read(nid, umc_base + UMCCH_UMC_CFG, &tmp))
  2409. umc->umc_cfg = tmp;
  2410. if (!amd_smn_read(nid, umc_base + UMCCH_SDP_CTRL, &tmp))
  2411. umc->sdp_ctrl = tmp;
  2412. if (!amd_smn_read(nid, umc_base + UMCCH_ECC_CTRL, &tmp))
  2413. umc->ecc_ctrl = tmp;
  2414. if (!amd_smn_read(nid, umc_base + UMCCH_UMC_CAP_HI, &tmp))
  2415. umc->umc_cap_hi = tmp;
  2416. }
  2417. }
  2418. /*
  2419. * Retrieve the hardware registers of the memory controller (this includes the
  2420. * 'Address Map' and 'Misc' device regs)
  2421. */
  2422. static void dct_read_mc_regs(struct amd64_pvt *pvt)
  2423. {
  2424. unsigned int range;
  2425. u64 msr_val;
  2426. /*
  2427. * Retrieve TOP_MEM and TOP_MEM2; no masking off of reserved bits since
  2428. * those are Read-As-Zero.
  2429. */
  2430. rdmsrl(MSR_K8_TOP_MEM1, pvt->top_mem);
  2431. edac_dbg(0, " TOP_MEM: 0x%016llx\n", pvt->top_mem);
  2432. /* Check first whether TOP_MEM2 is enabled: */
  2433. rdmsrl(MSR_AMD64_SYSCFG, msr_val);
  2434. if (msr_val & BIT(21)) {
  2435. rdmsrl(MSR_K8_TOP_MEM2, pvt->top_mem2);
  2436. edac_dbg(0, " TOP_MEM2: 0x%016llx\n", pvt->top_mem2);
  2437. } else {
  2438. edac_dbg(0, " TOP_MEM2 disabled\n");
  2439. }
  2440. amd64_read_pci_cfg(pvt->F3, NBCAP, &pvt->nbcap);
  2441. read_dram_ctl_register(pvt);
  2442. for (range = 0; range < DRAM_RANGES; range++) {
  2443. u8 rw;
  2444. /* read settings for this DRAM range */
  2445. read_dram_base_limit_regs(pvt, range);
  2446. rw = dram_rw(pvt, range);
  2447. if (!rw)
  2448. continue;
  2449. edac_dbg(1, " DRAM range[%d], base: 0x%016llx; limit: 0x%016llx\n",
  2450. range,
  2451. get_dram_base(pvt, range),
  2452. get_dram_limit(pvt, range));
  2453. edac_dbg(1, " IntlvEn=%s; Range access: %s%s IntlvSel=%d DstNode=%d\n",
  2454. dram_intlv_en(pvt, range) ? "Enabled" : "Disabled",
  2455. (rw & 0x1) ? "R" : "-",
  2456. (rw & 0x2) ? "W" : "-",
  2457. dram_intlv_sel(pvt, range),
  2458. dram_dst_node(pvt, range));
  2459. }
  2460. amd64_read_pci_cfg(pvt->F1, DHAR, &pvt->dhar);
  2461. amd64_read_dct_pci_cfg(pvt, 0, DBAM0, &pvt->dbam0);
  2462. amd64_read_pci_cfg(pvt->F3, F10_ONLINE_SPARE, &pvt->online_spare);
  2463. amd64_read_dct_pci_cfg(pvt, 0, DCLR0, &pvt->dclr0);
  2464. amd64_read_dct_pci_cfg(pvt, 0, DCHR0, &pvt->dchr0);
  2465. if (!dct_ganging_enabled(pvt)) {
  2466. amd64_read_dct_pci_cfg(pvt, 1, DCLR0, &pvt->dclr1);
  2467. amd64_read_dct_pci_cfg(pvt, 1, DCHR0, &pvt->dchr1);
  2468. }
  2469. determine_ecc_sym_sz(pvt);
  2470. }
  2471. /*
  2472. * NOTE: CPU Revision Dependent code
  2473. *
  2474. * Input:
  2475. * @csrow_nr ChipSelect Row Number (0..NUM_CHIPSELECTS-1)
  2476. * k8 private pointer to -->
  2477. * DRAM Bank Address mapping register
  2478. * node_id
  2479. * DCL register where dual_channel_active is
  2480. *
  2481. * The DBAM register consists of 4 sets of 4 bits each definitions:
  2482. *
  2483. * Bits: CSROWs
  2484. * 0-3 CSROWs 0 and 1
  2485. * 4-7 CSROWs 2 and 3
  2486. * 8-11 CSROWs 4 and 5
  2487. * 12-15 CSROWs 6 and 7
  2488. *
  2489. * Values range from: 0 to 15
  2490. * The meaning of the values depends on CPU revision and dual-channel state,
  2491. * see relevant BKDG more info.
  2492. *
  2493. * The memory controller provides for total of only 8 CSROWs in its current
  2494. * architecture. Each "pair" of CSROWs normally represents just one DIMM in
  2495. * single channel or two (2) DIMMs in dual channel mode.
  2496. *
  2497. * The following code logic collapses the various tables for CSROW based on CPU
  2498. * revision.
  2499. *
  2500. * Returns:
  2501. * The number of PAGE_SIZE pages on the specified CSROW number it
  2502. * encompasses
  2503. *
  2504. */
  2505. static u32 dct_get_csrow_nr_pages(struct amd64_pvt *pvt, u8 dct, int csrow_nr)
  2506. {
  2507. u32 dbam = dct ? pvt->dbam1 : pvt->dbam0;
  2508. u32 cs_mode, nr_pages;
  2509. csrow_nr >>= 1;
  2510. cs_mode = DBAM_DIMM(csrow_nr, dbam);
  2511. nr_pages = pvt->ops->dbam_to_cs(pvt, dct, cs_mode, csrow_nr);
  2512. nr_pages <<= 20 - PAGE_SHIFT;
  2513. edac_dbg(0, "csrow: %d, channel: %d, DBAM idx: %d\n",
  2514. csrow_nr, dct, cs_mode);
  2515. edac_dbg(0, "nr_pages/channel: %u\n", nr_pages);
  2516. return nr_pages;
  2517. }
  2518. static u32 umc_get_csrow_nr_pages(struct amd64_pvt *pvt, u8 dct, int csrow_nr_orig)
  2519. {
  2520. int csrow_nr = csrow_nr_orig;
  2521. u32 cs_mode, nr_pages;
  2522. cs_mode = umc_get_cs_mode(csrow_nr >> 1, dct, pvt);
  2523. nr_pages = umc_addr_mask_to_cs_size(pvt, dct, cs_mode, csrow_nr);
  2524. nr_pages <<= 20 - PAGE_SHIFT;
  2525. edac_dbg(0, "csrow: %d, channel: %d, cs_mode %d\n",
  2526. csrow_nr_orig, dct, cs_mode);
  2527. edac_dbg(0, "nr_pages/channel: %u\n", nr_pages);
  2528. return nr_pages;
  2529. }
  2530. static void umc_init_csrows(struct mem_ctl_info *mci)
  2531. {
  2532. struct amd64_pvt *pvt = mci->pvt_info;
  2533. enum edac_type edac_mode = EDAC_NONE;
  2534. enum dev_type dev_type = DEV_UNKNOWN;
  2535. struct dimm_info *dimm;
  2536. u8 umc, cs;
  2537. if (mci->edac_ctl_cap & EDAC_FLAG_S16ECD16ED) {
  2538. edac_mode = EDAC_S16ECD16ED;
  2539. dev_type = DEV_X16;
  2540. } else if (mci->edac_ctl_cap & EDAC_FLAG_S8ECD8ED) {
  2541. edac_mode = EDAC_S8ECD8ED;
  2542. dev_type = DEV_X8;
  2543. } else if (mci->edac_ctl_cap & EDAC_FLAG_S4ECD4ED) {
  2544. edac_mode = EDAC_S4ECD4ED;
  2545. dev_type = DEV_X4;
  2546. } else if (mci->edac_ctl_cap & EDAC_FLAG_SECDED) {
  2547. edac_mode = EDAC_SECDED;
  2548. }
  2549. for_each_umc(umc) {
  2550. for_each_chip_select(cs, umc, pvt) {
  2551. if (!csrow_enabled(cs, umc, pvt))
  2552. continue;
  2553. dimm = mci->csrows[cs]->channels[umc]->dimm;
  2554. edac_dbg(1, "MC node: %d, csrow: %d\n",
  2555. pvt->mc_node_id, cs);
  2556. dimm->nr_pages = umc_get_csrow_nr_pages(pvt, umc, cs);
  2557. dimm->mtype = pvt->umc[umc].dram_type;
  2558. dimm->edac_mode = edac_mode;
  2559. dimm->dtype = dev_type;
  2560. dimm->grain = 64;
  2561. }
  2562. }
  2563. }
  2564. /*
  2565. * Initialize the array of csrow attribute instances, based on the values
  2566. * from pci config hardware registers.
  2567. */
  2568. static void dct_init_csrows(struct mem_ctl_info *mci)
  2569. {
  2570. struct amd64_pvt *pvt = mci->pvt_info;
  2571. enum edac_type edac_mode = EDAC_NONE;
  2572. struct csrow_info *csrow;
  2573. struct dimm_info *dimm;
  2574. int nr_pages = 0;
  2575. int i, j;
  2576. u32 val;
  2577. amd64_read_pci_cfg(pvt->F3, NBCFG, &val);
  2578. pvt->nbcfg = val;
  2579. edac_dbg(0, "node %d, NBCFG=0x%08x[ChipKillEccCap: %d|DramEccEn: %d]\n",
  2580. pvt->mc_node_id, val,
  2581. !!(val & NBCFG_CHIPKILL), !!(val & NBCFG_ECC_ENABLE));
  2582. /*
  2583. * We iterate over DCT0 here but we look at DCT1 in parallel, if needed.
  2584. */
  2585. for_each_chip_select(i, 0, pvt) {
  2586. bool row_dct0 = !!csrow_enabled(i, 0, pvt);
  2587. bool row_dct1 = false;
  2588. if (pvt->fam != 0xf)
  2589. row_dct1 = !!csrow_enabled(i, 1, pvt);
  2590. if (!row_dct0 && !row_dct1)
  2591. continue;
  2592. csrow = mci->csrows[i];
  2593. edac_dbg(1, "MC node: %d, csrow: %d\n",
  2594. pvt->mc_node_id, i);
  2595. if (row_dct0) {
  2596. nr_pages = dct_get_csrow_nr_pages(pvt, 0, i);
  2597. csrow->channels[0]->dimm->nr_pages = nr_pages;
  2598. }
  2599. /* K8 has only one DCT */
  2600. if (pvt->fam != 0xf && row_dct1) {
  2601. int row_dct1_pages = dct_get_csrow_nr_pages(pvt, 1, i);
  2602. csrow->channels[1]->dimm->nr_pages = row_dct1_pages;
  2603. nr_pages += row_dct1_pages;
  2604. }
  2605. edac_dbg(1, "Total csrow%d pages: %u\n", i, nr_pages);
  2606. /* Determine DIMM ECC mode: */
  2607. if (pvt->nbcfg & NBCFG_ECC_ENABLE) {
  2608. edac_mode = (pvt->nbcfg & NBCFG_CHIPKILL)
  2609. ? EDAC_S4ECD4ED
  2610. : EDAC_SECDED;
  2611. }
  2612. for (j = 0; j < pvt->max_mcs; j++) {
  2613. dimm = csrow->channels[j]->dimm;
  2614. dimm->mtype = pvt->dram_type;
  2615. dimm->edac_mode = edac_mode;
  2616. dimm->grain = 64;
  2617. }
  2618. }
  2619. }
  2620. /* get all cores on this DCT */
  2621. static void get_cpus_on_this_dct_cpumask(struct cpumask *mask, u16 nid)
  2622. {
  2623. int cpu;
  2624. for_each_online_cpu(cpu)
  2625. if (topology_amd_node_id(cpu) == nid)
  2626. cpumask_set_cpu(cpu, mask);
  2627. }
  2628. /* check MCG_CTL on all the cpus on this node */
  2629. static bool nb_mce_bank_enabled_on_node(u16 nid)
  2630. {
  2631. cpumask_var_t mask;
  2632. int cpu, nbe;
  2633. bool ret = false;
  2634. if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
  2635. amd64_warn("%s: Error allocating mask\n", __func__);
  2636. return false;
  2637. }
  2638. get_cpus_on_this_dct_cpumask(mask, nid);
  2639. rdmsr_on_cpus(mask, MSR_IA32_MCG_CTL, msrs);
  2640. for_each_cpu(cpu, mask) {
  2641. struct msr *reg = per_cpu_ptr(msrs, cpu);
  2642. nbe = reg->l & MSR_MCGCTL_NBE;
  2643. edac_dbg(0, "core: %u, MCG_CTL: 0x%llx, NB MSR is %s\n",
  2644. cpu, reg->q,
  2645. (nbe ? "enabled" : "disabled"));
  2646. if (!nbe)
  2647. goto out;
  2648. }
  2649. ret = true;
  2650. out:
  2651. free_cpumask_var(mask);
  2652. return ret;
  2653. }
  2654. static int toggle_ecc_err_reporting(struct ecc_settings *s, u16 nid, bool on)
  2655. {
  2656. cpumask_var_t cmask;
  2657. int cpu;
  2658. if (!zalloc_cpumask_var(&cmask, GFP_KERNEL)) {
  2659. amd64_warn("%s: error allocating mask\n", __func__);
  2660. return -ENOMEM;
  2661. }
  2662. get_cpus_on_this_dct_cpumask(cmask, nid);
  2663. rdmsr_on_cpus(cmask, MSR_IA32_MCG_CTL, msrs);
  2664. for_each_cpu(cpu, cmask) {
  2665. struct msr *reg = per_cpu_ptr(msrs, cpu);
  2666. if (on) {
  2667. if (reg->l & MSR_MCGCTL_NBE)
  2668. s->flags.nb_mce_enable = 1;
  2669. reg->l |= MSR_MCGCTL_NBE;
  2670. } else {
  2671. /*
  2672. * Turn off NB MCE reporting only when it was off before
  2673. */
  2674. if (!s->flags.nb_mce_enable)
  2675. reg->l &= ~MSR_MCGCTL_NBE;
  2676. }
  2677. }
  2678. wrmsr_on_cpus(cmask, MSR_IA32_MCG_CTL, msrs);
  2679. free_cpumask_var(cmask);
  2680. return 0;
  2681. }
  2682. static bool enable_ecc_error_reporting(struct ecc_settings *s, u16 nid,
  2683. struct pci_dev *F3)
  2684. {
  2685. bool ret = true;
  2686. u32 value, mask = 0x3; /* UECC/CECC enable */
  2687. if (toggle_ecc_err_reporting(s, nid, ON)) {
  2688. amd64_warn("Error enabling ECC reporting over MCGCTL!\n");
  2689. return false;
  2690. }
  2691. amd64_read_pci_cfg(F3, NBCTL, &value);
  2692. s->old_nbctl = value & mask;
  2693. s->nbctl_valid = true;
  2694. value |= mask;
  2695. amd64_write_pci_cfg(F3, NBCTL, value);
  2696. amd64_read_pci_cfg(F3, NBCFG, &value);
  2697. edac_dbg(0, "1: node %d, NBCFG=0x%08x[DramEccEn: %d]\n",
  2698. nid, value, !!(value & NBCFG_ECC_ENABLE));
  2699. if (!(value & NBCFG_ECC_ENABLE)) {
  2700. amd64_warn("DRAM ECC disabled on this node, enabling...\n");
  2701. s->flags.nb_ecc_prev = 0;
  2702. /* Attempt to turn on DRAM ECC Enable */
  2703. value |= NBCFG_ECC_ENABLE;
  2704. amd64_write_pci_cfg(F3, NBCFG, value);
  2705. amd64_read_pci_cfg(F3, NBCFG, &value);
  2706. if (!(value & NBCFG_ECC_ENABLE)) {
  2707. amd64_warn("Hardware rejected DRAM ECC enable,"
  2708. "check memory DIMM configuration.\n");
  2709. ret = false;
  2710. } else {
  2711. amd64_info("Hardware accepted DRAM ECC Enable\n");
  2712. }
  2713. } else {
  2714. s->flags.nb_ecc_prev = 1;
  2715. }
  2716. edac_dbg(0, "2: node %d, NBCFG=0x%08x[DramEccEn: %d]\n",
  2717. nid, value, !!(value & NBCFG_ECC_ENABLE));
  2718. return ret;
  2719. }
  2720. static void restore_ecc_error_reporting(struct ecc_settings *s, u16 nid,
  2721. struct pci_dev *F3)
  2722. {
  2723. u32 value, mask = 0x3; /* UECC/CECC enable */
  2724. if (!s->nbctl_valid)
  2725. return;
  2726. amd64_read_pci_cfg(F3, NBCTL, &value);
  2727. value &= ~mask;
  2728. value |= s->old_nbctl;
  2729. amd64_write_pci_cfg(F3, NBCTL, value);
  2730. /* restore previous BIOS DRAM ECC "off" setting we force-enabled */
  2731. if (!s->flags.nb_ecc_prev) {
  2732. amd64_read_pci_cfg(F3, NBCFG, &value);
  2733. value &= ~NBCFG_ECC_ENABLE;
  2734. amd64_write_pci_cfg(F3, NBCFG, value);
  2735. }
  2736. /* restore the NB Enable MCGCTL bit */
  2737. if (toggle_ecc_err_reporting(s, nid, OFF))
  2738. amd64_warn("Error restoring NB MCGCTL settings!\n");
  2739. }
  2740. static bool dct_ecc_enabled(struct amd64_pvt *pvt)
  2741. {
  2742. u16 nid = pvt->mc_node_id;
  2743. bool nb_mce_en = false;
  2744. u8 ecc_en = 0;
  2745. u32 value;
  2746. amd64_read_pci_cfg(pvt->F3, NBCFG, &value);
  2747. ecc_en = !!(value & NBCFG_ECC_ENABLE);
  2748. nb_mce_en = nb_mce_bank_enabled_on_node(nid);
  2749. if (!nb_mce_en)
  2750. edac_dbg(0, "NB MCE bank disabled, set MSR 0x%08x[4] on node %d to enable.\n",
  2751. MSR_IA32_MCG_CTL, nid);
  2752. edac_dbg(3, "Node %d: DRAM ECC %s.\n", nid, (ecc_en ? "enabled" : "disabled"));
  2753. if (!ecc_en || !nb_mce_en)
  2754. return false;
  2755. else
  2756. return true;
  2757. }
  2758. static bool umc_ecc_enabled(struct amd64_pvt *pvt)
  2759. {
  2760. struct amd64_umc *umc;
  2761. bool ecc_en = false;
  2762. int i;
  2763. /* Check whether at least one UMC is enabled: */
  2764. for_each_umc(i) {
  2765. umc = &pvt->umc[i];
  2766. if (umc->sdp_ctrl & UMC_SDP_INIT &&
  2767. umc->umc_cap_hi & UMC_ECC_ENABLED) {
  2768. ecc_en = true;
  2769. break;
  2770. }
  2771. }
  2772. edac_dbg(3, "Node %d: DRAM ECC %s.\n", pvt->mc_node_id, (ecc_en ? "enabled" : "disabled"));
  2773. return ecc_en;
  2774. }
  2775. static inline void
  2776. umc_determine_edac_ctl_cap(struct mem_ctl_info *mci, struct amd64_pvt *pvt)
  2777. {
  2778. u8 i, ecc_en = 1, cpk_en = 1, dev_x4 = 1, dev_x16 = 1;
  2779. for_each_umc(i) {
  2780. if (pvt->umc[i].sdp_ctrl & UMC_SDP_INIT) {
  2781. ecc_en &= !!(pvt->umc[i].umc_cap_hi & UMC_ECC_ENABLED);
  2782. cpk_en &= !!(pvt->umc[i].umc_cap_hi & UMC_ECC_CHIPKILL_CAP);
  2783. dev_x4 &= !!(pvt->umc[i].dimm_cfg & BIT(6));
  2784. dev_x16 &= !!(pvt->umc[i].dimm_cfg & BIT(7));
  2785. }
  2786. }
  2787. /* Set chipkill only if ECC is enabled: */
  2788. if (ecc_en) {
  2789. mci->edac_ctl_cap |= EDAC_FLAG_SECDED;
  2790. if (!cpk_en)
  2791. return;
  2792. if (dev_x4)
  2793. mci->edac_ctl_cap |= EDAC_FLAG_S4ECD4ED;
  2794. else if (dev_x16)
  2795. mci->edac_ctl_cap |= EDAC_FLAG_S16ECD16ED;
  2796. else
  2797. mci->edac_ctl_cap |= EDAC_FLAG_S8ECD8ED;
  2798. }
  2799. }
  2800. static void dct_setup_mci_misc_attrs(struct mem_ctl_info *mci)
  2801. {
  2802. struct amd64_pvt *pvt = mci->pvt_info;
  2803. mci->mtype_cap = MEM_FLAG_DDR2 | MEM_FLAG_RDDR2;
  2804. mci->edac_ctl_cap = EDAC_FLAG_NONE;
  2805. if (pvt->nbcap & NBCAP_SECDED)
  2806. mci->edac_ctl_cap |= EDAC_FLAG_SECDED;
  2807. if (pvt->nbcap & NBCAP_CHIPKILL)
  2808. mci->edac_ctl_cap |= EDAC_FLAG_S4ECD4ED;
  2809. mci->edac_cap = dct_determine_edac_cap(pvt);
  2810. mci->mod_name = EDAC_MOD_STR;
  2811. mci->ctl_name = pvt->ctl_name;
  2812. mci->dev_name = pci_name(pvt->F3);
  2813. mci->ctl_page_to_phys = NULL;
  2814. /* memory scrubber interface */
  2815. mci->set_sdram_scrub_rate = set_scrub_rate;
  2816. mci->get_sdram_scrub_rate = get_scrub_rate;
  2817. dct_init_csrows(mci);
  2818. }
  2819. static void umc_setup_mci_misc_attrs(struct mem_ctl_info *mci)
  2820. {
  2821. struct amd64_pvt *pvt = mci->pvt_info;
  2822. mci->mtype_cap = MEM_FLAG_DDR4 | MEM_FLAG_RDDR4;
  2823. mci->edac_ctl_cap = EDAC_FLAG_NONE;
  2824. umc_determine_edac_ctl_cap(mci, pvt);
  2825. mci->edac_cap = umc_determine_edac_cap(pvt);
  2826. mci->mod_name = EDAC_MOD_STR;
  2827. mci->ctl_name = pvt->ctl_name;
  2828. mci->dev_name = pci_name(pvt->F3);
  2829. mci->ctl_page_to_phys = NULL;
  2830. umc_init_csrows(mci);
  2831. }
  2832. static int dct_hw_info_get(struct amd64_pvt *pvt)
  2833. {
  2834. int ret = reserve_mc_sibling_devs(pvt, pvt->f1_id, pvt->f2_id);
  2835. if (ret)
  2836. return ret;
  2837. dct_prep_chip_selects(pvt);
  2838. dct_read_base_mask(pvt);
  2839. dct_read_mc_regs(pvt);
  2840. dct_determine_memory_type(pvt);
  2841. return 0;
  2842. }
  2843. static int umc_hw_info_get(struct amd64_pvt *pvt)
  2844. {
  2845. pvt->umc = kcalloc(pvt->max_mcs, sizeof(struct amd64_umc), GFP_KERNEL);
  2846. if (!pvt->umc)
  2847. return -ENOMEM;
  2848. umc_prep_chip_selects(pvt);
  2849. umc_read_base_mask(pvt);
  2850. umc_read_mc_regs(pvt);
  2851. umc_determine_memory_type(pvt);
  2852. return 0;
  2853. }
  2854. /*
  2855. * The CPUs have one channel per UMC, so UMC number is equivalent to a
  2856. * channel number. The GPUs have 8 channels per UMC, so the UMC number no
  2857. * longer works as a channel number.
  2858. *
  2859. * The channel number within a GPU UMC is given in MCA_IPID[15:12].
  2860. * However, the IDs are split such that two UMC values go to one UMC, and
  2861. * the channel numbers are split in two groups of four.
  2862. *
  2863. * Refer to comment on gpu_get_umc_base().
  2864. *
  2865. * For example,
  2866. * UMC0 CH[3:0] = 0x0005[3:0]000
  2867. * UMC0 CH[7:4] = 0x0015[3:0]000
  2868. * UMC1 CH[3:0] = 0x0025[3:0]000
  2869. * UMC1 CH[7:4] = 0x0035[3:0]000
  2870. */
  2871. static void gpu_get_err_info(struct mce *m, struct err_info *err)
  2872. {
  2873. u8 ch = (m->ipid & GENMASK(31, 0)) >> 20;
  2874. u8 phy = ((m->ipid >> 12) & 0xf);
  2875. err->channel = ch % 2 ? phy + 4 : phy;
  2876. err->csrow = phy;
  2877. }
  2878. static int gpu_addr_mask_to_cs_size(struct amd64_pvt *pvt, u8 umc,
  2879. unsigned int cs_mode, int csrow_nr)
  2880. {
  2881. u32 addr_mask = pvt->csels[umc].csmasks[csrow_nr];
  2882. u32 addr_mask_sec = pvt->csels[umc].csmasks_sec[csrow_nr];
  2883. return __addr_mask_to_cs_size(addr_mask, addr_mask_sec, cs_mode, csrow_nr, csrow_nr >> 1);
  2884. }
  2885. static void gpu_debug_display_dimm_sizes(struct amd64_pvt *pvt, u8 ctrl)
  2886. {
  2887. int size, cs_mode, cs = 0;
  2888. edac_printk(KERN_DEBUG, EDAC_MC, "UMC%d chip selects:\n", ctrl);
  2889. cs_mode = CS_EVEN_PRIMARY | CS_ODD_PRIMARY;
  2890. for_each_chip_select(cs, ctrl, pvt) {
  2891. size = gpu_addr_mask_to_cs_size(pvt, ctrl, cs_mode, cs);
  2892. amd64_info(EDAC_MC ": %d: %5dMB\n", cs, size);
  2893. }
  2894. }
  2895. static void gpu_dump_misc_regs(struct amd64_pvt *pvt)
  2896. {
  2897. struct amd64_umc *umc;
  2898. u32 i;
  2899. for_each_umc(i) {
  2900. umc = &pvt->umc[i];
  2901. edac_dbg(1, "UMC%d UMC cfg: 0x%x\n", i, umc->umc_cfg);
  2902. edac_dbg(1, "UMC%d SDP ctrl: 0x%x\n", i, umc->sdp_ctrl);
  2903. edac_dbg(1, "UMC%d ECC ctrl: 0x%x\n", i, umc->ecc_ctrl);
  2904. edac_dbg(1, "UMC%d All HBMs support ECC: yes\n", i);
  2905. gpu_debug_display_dimm_sizes(pvt, i);
  2906. }
  2907. }
  2908. static u32 gpu_get_csrow_nr_pages(struct amd64_pvt *pvt, u8 dct, int csrow_nr)
  2909. {
  2910. u32 nr_pages;
  2911. int cs_mode = CS_EVEN_PRIMARY | CS_ODD_PRIMARY;
  2912. nr_pages = gpu_addr_mask_to_cs_size(pvt, dct, cs_mode, csrow_nr);
  2913. nr_pages <<= 20 - PAGE_SHIFT;
  2914. edac_dbg(0, "csrow: %d, channel: %d\n", csrow_nr, dct);
  2915. edac_dbg(0, "nr_pages/channel: %u\n", nr_pages);
  2916. return nr_pages;
  2917. }
  2918. static void gpu_init_csrows(struct mem_ctl_info *mci)
  2919. {
  2920. struct amd64_pvt *pvt = mci->pvt_info;
  2921. struct dimm_info *dimm;
  2922. u8 umc, cs;
  2923. for_each_umc(umc) {
  2924. for_each_chip_select(cs, umc, pvt) {
  2925. if (!csrow_enabled(cs, umc, pvt))
  2926. continue;
  2927. dimm = mci->csrows[umc]->channels[cs]->dimm;
  2928. edac_dbg(1, "MC node: %d, csrow: %d\n",
  2929. pvt->mc_node_id, cs);
  2930. dimm->nr_pages = gpu_get_csrow_nr_pages(pvt, umc, cs);
  2931. dimm->edac_mode = EDAC_SECDED;
  2932. dimm->mtype = pvt->dram_type;
  2933. dimm->dtype = DEV_X16;
  2934. dimm->grain = 64;
  2935. }
  2936. }
  2937. }
  2938. static void gpu_setup_mci_misc_attrs(struct mem_ctl_info *mci)
  2939. {
  2940. struct amd64_pvt *pvt = mci->pvt_info;
  2941. mci->mtype_cap = MEM_FLAG_HBM2;
  2942. mci->edac_ctl_cap = EDAC_FLAG_SECDED;
  2943. mci->edac_cap = EDAC_FLAG_EC;
  2944. mci->mod_name = EDAC_MOD_STR;
  2945. mci->ctl_name = pvt->ctl_name;
  2946. mci->dev_name = pci_name(pvt->F3);
  2947. mci->ctl_page_to_phys = NULL;
  2948. gpu_init_csrows(mci);
  2949. }
  2950. /* ECC is enabled by default on GPU nodes */
  2951. static bool gpu_ecc_enabled(struct amd64_pvt *pvt)
  2952. {
  2953. return true;
  2954. }
  2955. static inline u32 gpu_get_umc_base(struct amd64_pvt *pvt, u8 umc, u8 channel)
  2956. {
  2957. /*
  2958. * On CPUs, there is one channel per UMC, so UMC numbering equals
  2959. * channel numbering. On GPUs, there are eight channels per UMC,
  2960. * so the channel numbering is different from UMC numbering.
  2961. *
  2962. * On CPU nodes channels are selected in 6th nibble
  2963. * UMC chY[3:0]= [(chY*2 + 1) : (chY*2)]50000;
  2964. *
  2965. * On GPU nodes channels are selected in 3rd nibble
  2966. * HBM chX[3:0]= [Y ]5X[3:0]000;
  2967. * HBM chX[7:4]= [Y+1]5X[3:0]000
  2968. *
  2969. * On MI300 APU nodes, same as GPU nodes but channels are selected
  2970. * in the base address of 0x90000
  2971. */
  2972. umc *= 2;
  2973. if (channel >= 4)
  2974. umc++;
  2975. return pvt->gpu_umc_base + (umc << 20) + ((channel % 4) << 12);
  2976. }
  2977. static void gpu_read_mc_regs(struct amd64_pvt *pvt)
  2978. {
  2979. u8 nid = pvt->mc_node_id;
  2980. struct amd64_umc *umc;
  2981. u32 i, tmp, umc_base;
  2982. /* Read registers from each UMC */
  2983. for_each_umc(i) {
  2984. umc_base = gpu_get_umc_base(pvt, i, 0);
  2985. umc = &pvt->umc[i];
  2986. if (!amd_smn_read(nid, umc_base + UMCCH_UMC_CFG, &tmp))
  2987. umc->umc_cfg = tmp;
  2988. if (!amd_smn_read(nid, umc_base + UMCCH_SDP_CTRL, &tmp))
  2989. umc->sdp_ctrl = tmp;
  2990. if (!amd_smn_read(nid, umc_base + UMCCH_ECC_CTRL, &tmp))
  2991. umc->ecc_ctrl = tmp;
  2992. }
  2993. }
  2994. static void gpu_read_base_mask(struct amd64_pvt *pvt)
  2995. {
  2996. u32 base_reg, mask_reg;
  2997. u32 *base, *mask;
  2998. int umc, cs;
  2999. for_each_umc(umc) {
  3000. for_each_chip_select(cs, umc, pvt) {
  3001. base_reg = gpu_get_umc_base(pvt, umc, cs) + UMCCH_BASE_ADDR;
  3002. base = &pvt->csels[umc].csbases[cs];
  3003. if (!amd_smn_read(pvt->mc_node_id, base_reg, base)) {
  3004. edac_dbg(0, " DCSB%d[%d]=0x%08x reg: 0x%x\n",
  3005. umc, cs, *base, base_reg);
  3006. }
  3007. mask_reg = gpu_get_umc_base(pvt, umc, cs) + UMCCH_ADDR_MASK;
  3008. mask = &pvt->csels[umc].csmasks[cs];
  3009. if (!amd_smn_read(pvt->mc_node_id, mask_reg, mask)) {
  3010. edac_dbg(0, " DCSM%d[%d]=0x%08x reg: 0x%x\n",
  3011. umc, cs, *mask, mask_reg);
  3012. }
  3013. }
  3014. }
  3015. }
  3016. static void gpu_prep_chip_selects(struct amd64_pvt *pvt)
  3017. {
  3018. int umc;
  3019. for_each_umc(umc) {
  3020. pvt->csels[umc].b_cnt = 8;
  3021. pvt->csels[umc].m_cnt = 8;
  3022. }
  3023. }
  3024. static int gpu_hw_info_get(struct amd64_pvt *pvt)
  3025. {
  3026. int ret;
  3027. ret = gpu_get_node_map(pvt);
  3028. if (ret)
  3029. return ret;
  3030. pvt->umc = kcalloc(pvt->max_mcs, sizeof(struct amd64_umc), GFP_KERNEL);
  3031. if (!pvt->umc)
  3032. return -ENOMEM;
  3033. gpu_prep_chip_selects(pvt);
  3034. gpu_read_base_mask(pvt);
  3035. gpu_read_mc_regs(pvt);
  3036. return 0;
  3037. }
  3038. static void hw_info_put(struct amd64_pvt *pvt)
  3039. {
  3040. pci_dev_put(pvt->F1);
  3041. pci_dev_put(pvt->F2);
  3042. kfree(pvt->umc);
  3043. }
  3044. static struct low_ops umc_ops = {
  3045. .hw_info_get = umc_hw_info_get,
  3046. .ecc_enabled = umc_ecc_enabled,
  3047. .setup_mci_misc_attrs = umc_setup_mci_misc_attrs,
  3048. .dump_misc_regs = umc_dump_misc_regs,
  3049. .get_err_info = umc_get_err_info,
  3050. };
  3051. static struct low_ops gpu_ops = {
  3052. .hw_info_get = gpu_hw_info_get,
  3053. .ecc_enabled = gpu_ecc_enabled,
  3054. .setup_mci_misc_attrs = gpu_setup_mci_misc_attrs,
  3055. .dump_misc_regs = gpu_dump_misc_regs,
  3056. .get_err_info = gpu_get_err_info,
  3057. };
  3058. /* Use Family 16h versions for defaults and adjust as needed below. */
  3059. static struct low_ops dct_ops = {
  3060. .map_sysaddr_to_csrow = f1x_map_sysaddr_to_csrow,
  3061. .dbam_to_cs = f16_dbam_to_chip_select,
  3062. .hw_info_get = dct_hw_info_get,
  3063. .ecc_enabled = dct_ecc_enabled,
  3064. .setup_mci_misc_attrs = dct_setup_mci_misc_attrs,
  3065. .dump_misc_regs = dct_dump_misc_regs,
  3066. };
  3067. static int per_family_init(struct amd64_pvt *pvt)
  3068. {
  3069. pvt->ext_model = boot_cpu_data.x86_model >> 4;
  3070. pvt->stepping = boot_cpu_data.x86_stepping;
  3071. pvt->model = boot_cpu_data.x86_model;
  3072. pvt->fam = boot_cpu_data.x86;
  3073. pvt->max_mcs = 2;
  3074. /*
  3075. * Decide on which ops group to use here and do any family/model
  3076. * overrides below.
  3077. */
  3078. if (pvt->fam >= 0x17)
  3079. pvt->ops = &umc_ops;
  3080. else
  3081. pvt->ops = &dct_ops;
  3082. switch (pvt->fam) {
  3083. case 0xf:
  3084. pvt->ctl_name = (pvt->ext_model >= K8_REV_F) ?
  3085. "K8 revF or later" : "K8 revE or earlier";
  3086. pvt->f1_id = PCI_DEVICE_ID_AMD_K8_NB_ADDRMAP;
  3087. pvt->f2_id = PCI_DEVICE_ID_AMD_K8_NB_MEMCTL;
  3088. pvt->ops->map_sysaddr_to_csrow = k8_map_sysaddr_to_csrow;
  3089. pvt->ops->dbam_to_cs = k8_dbam_to_chip_select;
  3090. break;
  3091. case 0x10:
  3092. pvt->ctl_name = "F10h";
  3093. pvt->f1_id = PCI_DEVICE_ID_AMD_10H_NB_MAP;
  3094. pvt->f2_id = PCI_DEVICE_ID_AMD_10H_NB_DRAM;
  3095. pvt->ops->dbam_to_cs = f10_dbam_to_chip_select;
  3096. break;
  3097. case 0x15:
  3098. switch (pvt->model) {
  3099. case 0x30:
  3100. pvt->ctl_name = "F15h_M30h";
  3101. pvt->f1_id = PCI_DEVICE_ID_AMD_15H_M30H_NB_F1;
  3102. pvt->f2_id = PCI_DEVICE_ID_AMD_15H_M30H_NB_F2;
  3103. break;
  3104. case 0x60:
  3105. pvt->ctl_name = "F15h_M60h";
  3106. pvt->f1_id = PCI_DEVICE_ID_AMD_15H_M60H_NB_F1;
  3107. pvt->f2_id = PCI_DEVICE_ID_AMD_15H_M60H_NB_F2;
  3108. pvt->ops->dbam_to_cs = f15_m60h_dbam_to_chip_select;
  3109. break;
  3110. case 0x13:
  3111. /* Richland is only client */
  3112. return -ENODEV;
  3113. default:
  3114. pvt->ctl_name = "F15h";
  3115. pvt->f1_id = PCI_DEVICE_ID_AMD_15H_NB_F1;
  3116. pvt->f2_id = PCI_DEVICE_ID_AMD_15H_NB_F2;
  3117. pvt->ops->dbam_to_cs = f15_dbam_to_chip_select;
  3118. break;
  3119. }
  3120. break;
  3121. case 0x16:
  3122. switch (pvt->model) {
  3123. case 0x30:
  3124. pvt->ctl_name = "F16h_M30h";
  3125. pvt->f1_id = PCI_DEVICE_ID_AMD_16H_M30H_NB_F1;
  3126. pvt->f2_id = PCI_DEVICE_ID_AMD_16H_M30H_NB_F2;
  3127. break;
  3128. default:
  3129. pvt->ctl_name = "F16h";
  3130. pvt->f1_id = PCI_DEVICE_ID_AMD_16H_NB_F1;
  3131. pvt->f2_id = PCI_DEVICE_ID_AMD_16H_NB_F2;
  3132. break;
  3133. }
  3134. break;
  3135. case 0x17:
  3136. switch (pvt->model) {
  3137. case 0x10 ... 0x2f:
  3138. pvt->ctl_name = "F17h_M10h";
  3139. break;
  3140. case 0x30 ... 0x3f:
  3141. pvt->ctl_name = "F17h_M30h";
  3142. pvt->max_mcs = 8;
  3143. break;
  3144. case 0x60 ... 0x6f:
  3145. pvt->ctl_name = "F17h_M60h";
  3146. break;
  3147. case 0x70 ... 0x7f:
  3148. pvt->ctl_name = "F17h_M70h";
  3149. break;
  3150. default:
  3151. pvt->ctl_name = "F17h";
  3152. break;
  3153. }
  3154. break;
  3155. case 0x18:
  3156. pvt->ctl_name = "F18h";
  3157. break;
  3158. case 0x19:
  3159. switch (pvt->model) {
  3160. case 0x00 ... 0x0f:
  3161. pvt->ctl_name = "F19h";
  3162. pvt->max_mcs = 8;
  3163. break;
  3164. case 0x10 ... 0x1f:
  3165. pvt->ctl_name = "F19h_M10h";
  3166. pvt->max_mcs = 12;
  3167. pvt->flags.zn_regs_v2 = 1;
  3168. break;
  3169. case 0x20 ... 0x2f:
  3170. pvt->ctl_name = "F19h_M20h";
  3171. break;
  3172. case 0x30 ... 0x3f:
  3173. if (pvt->F3->device == PCI_DEVICE_ID_AMD_MI200_DF_F3) {
  3174. pvt->ctl_name = "MI200";
  3175. pvt->max_mcs = 4;
  3176. pvt->dram_type = MEM_HBM2;
  3177. pvt->gpu_umc_base = 0x50000;
  3178. pvt->ops = &gpu_ops;
  3179. } else {
  3180. pvt->ctl_name = "F19h_M30h";
  3181. pvt->max_mcs = 8;
  3182. }
  3183. break;
  3184. case 0x50 ... 0x5f:
  3185. pvt->ctl_name = "F19h_M50h";
  3186. break;
  3187. case 0x60 ... 0x6f:
  3188. pvt->ctl_name = "F19h_M60h";
  3189. pvt->flags.zn_regs_v2 = 1;
  3190. break;
  3191. case 0x70 ... 0x7f:
  3192. pvt->ctl_name = "F19h_M70h";
  3193. pvt->max_mcs = 4;
  3194. pvt->flags.zn_regs_v2 = 1;
  3195. break;
  3196. case 0x90 ... 0x9f:
  3197. pvt->ctl_name = "F19h_M90h";
  3198. pvt->max_mcs = 4;
  3199. pvt->dram_type = MEM_HBM3;
  3200. pvt->gpu_umc_base = 0x90000;
  3201. pvt->ops = &gpu_ops;
  3202. break;
  3203. case 0xa0 ... 0xaf:
  3204. pvt->ctl_name = "F19h_MA0h";
  3205. pvt->max_mcs = 12;
  3206. pvt->flags.zn_regs_v2 = 1;
  3207. break;
  3208. }
  3209. break;
  3210. case 0x1A:
  3211. switch (pvt->model) {
  3212. case 0x00 ... 0x1f:
  3213. pvt->ctl_name = "F1Ah";
  3214. pvt->max_mcs = 12;
  3215. pvt->flags.zn_regs_v2 = 1;
  3216. break;
  3217. case 0x40 ... 0x4f:
  3218. pvt->ctl_name = "F1Ah_M40h";
  3219. pvt->flags.zn_regs_v2 = 1;
  3220. break;
  3221. }
  3222. break;
  3223. default:
  3224. amd64_err("Unsupported family!\n");
  3225. return -ENODEV;
  3226. }
  3227. return 0;
  3228. }
  3229. static const struct attribute_group *amd64_edac_attr_groups[] = {
  3230. #ifdef CONFIG_EDAC_DEBUG
  3231. &dbg_group,
  3232. &inj_group,
  3233. #endif
  3234. NULL
  3235. };
  3236. /*
  3237. * For heterogeneous and APU models EDAC CHIP_SELECT and CHANNEL layers
  3238. * should be swapped to fit into the layers.
  3239. */
  3240. static unsigned int get_layer_size(struct amd64_pvt *pvt, u8 layer)
  3241. {
  3242. bool is_gpu = (pvt->ops == &gpu_ops);
  3243. if (!layer)
  3244. return is_gpu ? pvt->max_mcs
  3245. : pvt->csels[0].b_cnt;
  3246. else
  3247. return is_gpu ? pvt->csels[0].b_cnt
  3248. : pvt->max_mcs;
  3249. }
  3250. static int init_one_instance(struct amd64_pvt *pvt)
  3251. {
  3252. struct mem_ctl_info *mci = NULL;
  3253. struct edac_mc_layer layers[2];
  3254. int ret = -ENOMEM;
  3255. layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
  3256. layers[0].size = get_layer_size(pvt, 0);
  3257. layers[0].is_virt_csrow = true;
  3258. layers[1].type = EDAC_MC_LAYER_CHANNEL;
  3259. layers[1].size = get_layer_size(pvt, 1);
  3260. layers[1].is_virt_csrow = false;
  3261. mci = edac_mc_alloc(pvt->mc_node_id, ARRAY_SIZE(layers), layers, 0);
  3262. if (!mci)
  3263. return ret;
  3264. mci->pvt_info = pvt;
  3265. mci->pdev = &pvt->F3->dev;
  3266. pvt->ops->setup_mci_misc_attrs(mci);
  3267. ret = -ENODEV;
  3268. if (edac_mc_add_mc_with_groups(mci, amd64_edac_attr_groups)) {
  3269. edac_dbg(1, "failed edac_mc_add_mc()\n");
  3270. edac_mc_free(mci);
  3271. return ret;
  3272. }
  3273. return 0;
  3274. }
  3275. static bool instance_has_memory(struct amd64_pvt *pvt)
  3276. {
  3277. bool cs_enabled = false;
  3278. int cs = 0, dct = 0;
  3279. for (dct = 0; dct < pvt->max_mcs; dct++) {
  3280. for_each_chip_select(cs, dct, pvt)
  3281. cs_enabled |= csrow_enabled(cs, dct, pvt);
  3282. }
  3283. return cs_enabled;
  3284. }
  3285. static int probe_one_instance(unsigned int nid)
  3286. {
  3287. struct pci_dev *F3 = node_to_amd_nb(nid)->misc;
  3288. struct amd64_pvt *pvt = NULL;
  3289. struct ecc_settings *s;
  3290. int ret;
  3291. ret = -ENOMEM;
  3292. s = kzalloc(sizeof(struct ecc_settings), GFP_KERNEL);
  3293. if (!s)
  3294. goto err_out;
  3295. ecc_stngs[nid] = s;
  3296. pvt = kzalloc(sizeof(struct amd64_pvt), GFP_KERNEL);
  3297. if (!pvt)
  3298. goto err_settings;
  3299. pvt->mc_node_id = nid;
  3300. pvt->F3 = F3;
  3301. ret = per_family_init(pvt);
  3302. if (ret < 0)
  3303. goto err_enable;
  3304. ret = pvt->ops->hw_info_get(pvt);
  3305. if (ret < 0)
  3306. goto err_enable;
  3307. ret = 0;
  3308. if (!instance_has_memory(pvt)) {
  3309. amd64_info("Node %d: No DIMMs detected.\n", nid);
  3310. goto err_enable;
  3311. }
  3312. if (!pvt->ops->ecc_enabled(pvt)) {
  3313. ret = -ENODEV;
  3314. if (!ecc_enable_override)
  3315. goto err_enable;
  3316. if (boot_cpu_data.x86 >= 0x17) {
  3317. amd64_warn("Forcing ECC on is not recommended on newer systems. Please enable ECC in BIOS.");
  3318. goto err_enable;
  3319. } else
  3320. amd64_warn("Forcing ECC on!\n");
  3321. if (!enable_ecc_error_reporting(s, nid, F3))
  3322. goto err_enable;
  3323. }
  3324. ret = init_one_instance(pvt);
  3325. if (ret < 0) {
  3326. amd64_err("Error probing instance: %d\n", nid);
  3327. if (boot_cpu_data.x86 < 0x17)
  3328. restore_ecc_error_reporting(s, nid, F3);
  3329. goto err_enable;
  3330. }
  3331. amd64_info("%s detected (node %d).\n", pvt->ctl_name, pvt->mc_node_id);
  3332. /* Display and decode various registers for debug purposes. */
  3333. pvt->ops->dump_misc_regs(pvt);
  3334. return ret;
  3335. err_enable:
  3336. hw_info_put(pvt);
  3337. kfree(pvt);
  3338. err_settings:
  3339. kfree(s);
  3340. ecc_stngs[nid] = NULL;
  3341. err_out:
  3342. return ret;
  3343. }
  3344. static void remove_one_instance(unsigned int nid)
  3345. {
  3346. struct pci_dev *F3 = node_to_amd_nb(nid)->misc;
  3347. struct ecc_settings *s = ecc_stngs[nid];
  3348. struct mem_ctl_info *mci;
  3349. struct amd64_pvt *pvt;
  3350. /* Remove from EDAC CORE tracking list */
  3351. mci = edac_mc_del_mc(&F3->dev);
  3352. if (!mci)
  3353. return;
  3354. pvt = mci->pvt_info;
  3355. restore_ecc_error_reporting(s, nid, F3);
  3356. kfree(ecc_stngs[nid]);
  3357. ecc_stngs[nid] = NULL;
  3358. /* Free the EDAC CORE resources */
  3359. mci->pvt_info = NULL;
  3360. hw_info_put(pvt);
  3361. kfree(pvt);
  3362. edac_mc_free(mci);
  3363. }
  3364. static void setup_pci_device(void)
  3365. {
  3366. if (pci_ctl)
  3367. return;
  3368. pci_ctl = edac_pci_create_generic_ctl(pci_ctl_dev, EDAC_MOD_STR);
  3369. if (!pci_ctl) {
  3370. pr_warn("%s(): Unable to create PCI control\n", __func__);
  3371. pr_warn("%s(): PCI error report via EDAC not set\n", __func__);
  3372. }
  3373. }
  3374. static const struct x86_cpu_id amd64_cpuids[] = {
  3375. X86_MATCH_VENDOR_FAM(AMD, 0x0F, NULL),
  3376. X86_MATCH_VENDOR_FAM(AMD, 0x10, NULL),
  3377. X86_MATCH_VENDOR_FAM(AMD, 0x15, NULL),
  3378. X86_MATCH_VENDOR_FAM(AMD, 0x16, NULL),
  3379. X86_MATCH_VENDOR_FAM(AMD, 0x17, NULL),
  3380. X86_MATCH_VENDOR_FAM(HYGON, 0x18, NULL),
  3381. X86_MATCH_VENDOR_FAM(AMD, 0x19, NULL),
  3382. X86_MATCH_VENDOR_FAM(AMD, 0x1A, NULL),
  3383. { }
  3384. };
  3385. MODULE_DEVICE_TABLE(x86cpu, amd64_cpuids);
  3386. static int __init amd64_edac_init(void)
  3387. {
  3388. const char *owner;
  3389. int err = -ENODEV;
  3390. int i;
  3391. if (ghes_get_devices())
  3392. return -EBUSY;
  3393. owner = edac_get_owner();
  3394. if (owner && strncmp(owner, EDAC_MOD_STR, sizeof(EDAC_MOD_STR)))
  3395. return -EBUSY;
  3396. if (!x86_match_cpu(amd64_cpuids))
  3397. return -ENODEV;
  3398. if (!amd_nb_num())
  3399. return -ENODEV;
  3400. opstate_init();
  3401. err = -ENOMEM;
  3402. ecc_stngs = kcalloc(amd_nb_num(), sizeof(ecc_stngs[0]), GFP_KERNEL);
  3403. if (!ecc_stngs)
  3404. goto err_free;
  3405. msrs = msrs_alloc();
  3406. if (!msrs)
  3407. goto err_free;
  3408. for (i = 0; i < amd_nb_num(); i++) {
  3409. err = probe_one_instance(i);
  3410. if (err) {
  3411. /* unwind properly */
  3412. while (--i >= 0)
  3413. remove_one_instance(i);
  3414. goto err_pci;
  3415. }
  3416. }
  3417. if (!edac_has_mcs()) {
  3418. err = -ENODEV;
  3419. goto err_pci;
  3420. }
  3421. /* register stuff with EDAC MCE */
  3422. if (boot_cpu_data.x86 >= 0x17) {
  3423. amd_register_ecc_decoder(decode_umc_error);
  3424. } else {
  3425. amd_register_ecc_decoder(decode_bus_error);
  3426. setup_pci_device();
  3427. }
  3428. #ifdef CONFIG_X86_32
  3429. amd64_err("%s on 32-bit is unsupported. USE AT YOUR OWN RISK!\n", EDAC_MOD_STR);
  3430. #endif
  3431. return 0;
  3432. err_pci:
  3433. pci_ctl_dev = NULL;
  3434. msrs_free(msrs);
  3435. msrs = NULL;
  3436. err_free:
  3437. kfree(ecc_stngs);
  3438. ecc_stngs = NULL;
  3439. return err;
  3440. }
  3441. static void __exit amd64_edac_exit(void)
  3442. {
  3443. int i;
  3444. if (pci_ctl)
  3445. edac_pci_release_generic_ctl(pci_ctl);
  3446. /* unregister from EDAC MCE */
  3447. if (boot_cpu_data.x86 >= 0x17)
  3448. amd_unregister_ecc_decoder(decode_umc_error);
  3449. else
  3450. amd_unregister_ecc_decoder(decode_bus_error);
  3451. for (i = 0; i < amd_nb_num(); i++)
  3452. remove_one_instance(i);
  3453. kfree(ecc_stngs);
  3454. ecc_stngs = NULL;
  3455. pci_ctl_dev = NULL;
  3456. msrs_free(msrs);
  3457. msrs = NULL;
  3458. }
  3459. module_init(amd64_edac_init);
  3460. module_exit(amd64_edac_exit);
  3461. MODULE_LICENSE("GPL");
  3462. MODULE_AUTHOR("SoftwareBitMaker: Doug Thompson, Dave Peterson, Thayne Harbaugh; AMD");
  3463. MODULE_DESCRIPTION("MC support for AMD64 memory controllers");
  3464. module_param(edac_op_state, int, 0444);
  3465. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");