amd64_edac.c 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231
  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. /* Asymmetric dual-rank DIMM support. */
  1005. if (csrow_sec_enabled(2 * dimm + 1, ctrl, pvt))
  1006. cs_mode |= CS_ODD_SECONDARY;
  1007. /*
  1008. * 3 Rank inteleaving support.
  1009. * There should be only three bases enabled and their two masks should
  1010. * be equal.
  1011. */
  1012. for_each_chip_select(base, ctrl, pvt)
  1013. count += csrow_enabled(base, ctrl, pvt);
  1014. if (count == 3 &&
  1015. pvt->csels[ctrl].csmasks[0] == pvt->csels[ctrl].csmasks[1]) {
  1016. edac_dbg(1, "3R interleaving in use.\n");
  1017. cs_mode |= CS_3R_INTERLEAVE;
  1018. }
  1019. return cs_mode;
  1020. }
  1021. static int __addr_mask_to_cs_size(u32 addr_mask_orig, unsigned int cs_mode,
  1022. int csrow_nr, int dimm)
  1023. {
  1024. u32 msb, weight, num_zero_bits;
  1025. u32 addr_mask_deinterleaved;
  1026. int size = 0;
  1027. /*
  1028. * The number of zero bits in the mask is equal to the number of bits
  1029. * in a full mask minus the number of bits in the current mask.
  1030. *
  1031. * The MSB is the number of bits in the full mask because BIT[0] is
  1032. * always 0.
  1033. *
  1034. * In the special 3 Rank interleaving case, a single bit is flipped
  1035. * without swapping with the most significant bit. This can be handled
  1036. * by keeping the MSB where it is and ignoring the single zero bit.
  1037. */
  1038. msb = fls(addr_mask_orig) - 1;
  1039. weight = hweight_long(addr_mask_orig);
  1040. num_zero_bits = msb - weight - !!(cs_mode & CS_3R_INTERLEAVE);
  1041. /* Take the number of zero bits off from the top of the mask. */
  1042. addr_mask_deinterleaved = GENMASK_ULL(msb - num_zero_bits, 1);
  1043. edac_dbg(1, "CS%d DIMM%d AddrMasks:\n", csrow_nr, dimm);
  1044. edac_dbg(1, " Original AddrMask: 0x%x\n", addr_mask_orig);
  1045. edac_dbg(1, " Deinterleaved AddrMask: 0x%x\n", addr_mask_deinterleaved);
  1046. /* Register [31:1] = Address [39:9]. Size is in kBs here. */
  1047. size = (addr_mask_deinterleaved >> 2) + 1;
  1048. /* Return size in MBs. */
  1049. return size >> 10;
  1050. }
  1051. static int umc_addr_mask_to_cs_size(struct amd64_pvt *pvt, u8 umc,
  1052. unsigned int cs_mode, int csrow_nr)
  1053. {
  1054. int cs_mask_nr = csrow_nr;
  1055. u32 addr_mask_orig;
  1056. int dimm, size = 0;
  1057. /* No Chip Selects are enabled. */
  1058. if (!cs_mode)
  1059. return size;
  1060. /* Requested size of an even CS but none are enabled. */
  1061. if (!(cs_mode & CS_EVEN) && !(csrow_nr & 1))
  1062. return size;
  1063. /* Requested size of an odd CS but none are enabled. */
  1064. if (!(cs_mode & CS_ODD) && (csrow_nr & 1))
  1065. return size;
  1066. /*
  1067. * Family 17h introduced systems with one mask per DIMM,
  1068. * and two Chip Selects per DIMM.
  1069. *
  1070. * CS0 and CS1 -> MASK0 / DIMM0
  1071. * CS2 and CS3 -> MASK1 / DIMM1
  1072. *
  1073. * Family 19h Model 10h introduced systems with one mask per Chip Select,
  1074. * and two Chip Selects per DIMM.
  1075. *
  1076. * CS0 -> MASK0 -> DIMM0
  1077. * CS1 -> MASK1 -> DIMM0
  1078. * CS2 -> MASK2 -> DIMM1
  1079. * CS3 -> MASK3 -> DIMM1
  1080. *
  1081. * Keep the mask number equal to the Chip Select number for newer systems,
  1082. * and shift the mask number for older systems.
  1083. */
  1084. dimm = csrow_nr >> 1;
  1085. if (!pvt->flags.zn_regs_v2)
  1086. cs_mask_nr >>= 1;
  1087. /* Asymmetric dual-rank DIMM support. */
  1088. if ((csrow_nr & 1) && (cs_mode & CS_ODD_SECONDARY))
  1089. addr_mask_orig = pvt->csels[umc].csmasks_sec[cs_mask_nr];
  1090. else
  1091. addr_mask_orig = pvt->csels[umc].csmasks[cs_mask_nr];
  1092. return __addr_mask_to_cs_size(addr_mask_orig, cs_mode, csrow_nr, dimm);
  1093. }
  1094. static void umc_debug_display_dimm_sizes(struct amd64_pvt *pvt, u8 ctrl)
  1095. {
  1096. int dimm, size0, size1, cs0, cs1, cs_mode;
  1097. edac_printk(KERN_DEBUG, EDAC_MC, "UMC%d chip selects:\n", ctrl);
  1098. for (dimm = 0; dimm < 2; dimm++) {
  1099. cs0 = dimm * 2;
  1100. cs1 = dimm * 2 + 1;
  1101. cs_mode = umc_get_cs_mode(dimm, ctrl, pvt);
  1102. size0 = umc_addr_mask_to_cs_size(pvt, ctrl, cs_mode, cs0);
  1103. size1 = umc_addr_mask_to_cs_size(pvt, ctrl, cs_mode, cs1);
  1104. amd64_info(EDAC_MC ": %d: %5dMB %d: %5dMB\n",
  1105. cs0, size0,
  1106. cs1, size1);
  1107. }
  1108. }
  1109. static void umc_dump_misc_regs(struct amd64_pvt *pvt)
  1110. {
  1111. struct amd64_umc *umc;
  1112. u32 i;
  1113. for_each_umc(i) {
  1114. umc = &pvt->umc[i];
  1115. edac_dbg(1, "UMC%d DIMM cfg: 0x%x\n", i, umc->dimm_cfg);
  1116. edac_dbg(1, "UMC%d UMC cfg: 0x%x\n", i, umc->umc_cfg);
  1117. edac_dbg(1, "UMC%d SDP ctrl: 0x%x\n", i, umc->sdp_ctrl);
  1118. edac_dbg(1, "UMC%d ECC ctrl: 0x%x\n", i, umc->ecc_ctrl);
  1119. edac_dbg(1, "UMC%d UMC cap high: 0x%x\n", i, umc->umc_cap_hi);
  1120. edac_dbg(1, "UMC%d ECC capable: %s, ChipKill ECC capable: %s\n",
  1121. i, (umc->umc_cap_hi & BIT(30)) ? "yes" : "no",
  1122. (umc->umc_cap_hi & BIT(31)) ? "yes" : "no");
  1123. edac_dbg(1, "UMC%d All DIMMs support ECC: %s\n",
  1124. i, (umc->umc_cfg & BIT(12)) ? "yes" : "no");
  1125. edac_dbg(1, "UMC%d x4 DIMMs present: %s\n",
  1126. i, (umc->dimm_cfg & BIT(6)) ? "yes" : "no");
  1127. edac_dbg(1, "UMC%d x16 DIMMs present: %s\n",
  1128. i, (umc->dimm_cfg & BIT(7)) ? "yes" : "no");
  1129. umc_debug_display_dimm_sizes(pvt, i);
  1130. }
  1131. }
  1132. static void dct_dump_misc_regs(struct amd64_pvt *pvt)
  1133. {
  1134. edac_dbg(1, "F3xE8 (NB Cap): 0x%08x\n", pvt->nbcap);
  1135. edac_dbg(1, " NB two channel DRAM capable: %s\n",
  1136. (pvt->nbcap & NBCAP_DCT_DUAL) ? "yes" : "no");
  1137. edac_dbg(1, " ECC capable: %s, ChipKill ECC capable: %s\n",
  1138. (pvt->nbcap & NBCAP_SECDED) ? "yes" : "no",
  1139. (pvt->nbcap & NBCAP_CHIPKILL) ? "yes" : "no");
  1140. debug_dump_dramcfg_low(pvt, pvt->dclr0, 0);
  1141. edac_dbg(1, "F3xB0 (Online Spare): 0x%08x\n", pvt->online_spare);
  1142. edac_dbg(1, "F1xF0 (DRAM Hole Address): 0x%08x, base: 0x%08x, offset: 0x%08x\n",
  1143. pvt->dhar, dhar_base(pvt),
  1144. (pvt->fam == 0xf) ? k8_dhar_offset(pvt)
  1145. : f10_dhar_offset(pvt));
  1146. dct_debug_display_dimm_sizes(pvt, 0);
  1147. /* everything below this point is Fam10h and above */
  1148. if (pvt->fam == 0xf)
  1149. return;
  1150. dct_debug_display_dimm_sizes(pvt, 1);
  1151. /* Only if NOT ganged does dclr1 have valid info */
  1152. if (!dct_ganging_enabled(pvt))
  1153. debug_dump_dramcfg_low(pvt, pvt->dclr1, 1);
  1154. edac_dbg(1, " DramHoleValid: %s\n", dhar_valid(pvt) ? "yes" : "no");
  1155. amd64_info("using x%u syndromes.\n", pvt->ecc_sym_sz);
  1156. }
  1157. /*
  1158. * See BKDG, F2x[1,0][5C:40], F2[1,0][6C:60]
  1159. */
  1160. static void dct_prep_chip_selects(struct amd64_pvt *pvt)
  1161. {
  1162. if (pvt->fam == 0xf && pvt->ext_model < K8_REV_F) {
  1163. pvt->csels[0].b_cnt = pvt->csels[1].b_cnt = 8;
  1164. pvt->csels[0].m_cnt = pvt->csels[1].m_cnt = 8;
  1165. } else if (pvt->fam == 0x15 && pvt->model == 0x30) {
  1166. pvt->csels[0].b_cnt = pvt->csels[1].b_cnt = 4;
  1167. pvt->csels[0].m_cnt = pvt->csels[1].m_cnt = 2;
  1168. } else {
  1169. pvt->csels[0].b_cnt = pvt->csels[1].b_cnt = 8;
  1170. pvt->csels[0].m_cnt = pvt->csels[1].m_cnt = 4;
  1171. }
  1172. }
  1173. static void umc_prep_chip_selects(struct amd64_pvt *pvt)
  1174. {
  1175. int umc;
  1176. for_each_umc(umc) {
  1177. pvt->csels[umc].b_cnt = 4;
  1178. pvt->csels[umc].m_cnt = pvt->flags.zn_regs_v2 ? 4 : 2;
  1179. }
  1180. }
  1181. static void umc_read_base_mask(struct amd64_pvt *pvt)
  1182. {
  1183. u32 umc_base_reg, umc_base_reg_sec;
  1184. u32 umc_mask_reg, umc_mask_reg_sec;
  1185. u32 base_reg, base_reg_sec;
  1186. u32 mask_reg, mask_reg_sec;
  1187. u32 *base, *base_sec;
  1188. u32 *mask, *mask_sec;
  1189. int cs, umc;
  1190. u32 tmp;
  1191. for_each_umc(umc) {
  1192. umc_base_reg = get_umc_base(umc) + UMCCH_BASE_ADDR;
  1193. umc_base_reg_sec = get_umc_base(umc) + UMCCH_BASE_ADDR_SEC;
  1194. for_each_chip_select(cs, umc, pvt) {
  1195. base = &pvt->csels[umc].csbases[cs];
  1196. base_sec = &pvt->csels[umc].csbases_sec[cs];
  1197. base_reg = umc_base_reg + (cs * 4);
  1198. base_reg_sec = umc_base_reg_sec + (cs * 4);
  1199. if (!amd_smn_read(pvt->mc_node_id, base_reg, &tmp)) {
  1200. *base = tmp;
  1201. edac_dbg(0, " DCSB%d[%d]=0x%08x reg: 0x%x\n",
  1202. umc, cs, *base, base_reg);
  1203. }
  1204. if (!amd_smn_read(pvt->mc_node_id, base_reg_sec, &tmp)) {
  1205. *base_sec = tmp;
  1206. edac_dbg(0, " DCSB_SEC%d[%d]=0x%08x reg: 0x%x\n",
  1207. umc, cs, *base_sec, base_reg_sec);
  1208. }
  1209. }
  1210. umc_mask_reg = get_umc_base(umc) + UMCCH_ADDR_MASK;
  1211. umc_mask_reg_sec = get_umc_base(umc) + get_umc_reg(pvt, UMCCH_ADDR_MASK_SEC);
  1212. for_each_chip_select_mask(cs, umc, pvt) {
  1213. mask = &pvt->csels[umc].csmasks[cs];
  1214. mask_sec = &pvt->csels[umc].csmasks_sec[cs];
  1215. mask_reg = umc_mask_reg + (cs * 4);
  1216. mask_reg_sec = umc_mask_reg_sec + (cs * 4);
  1217. if (!amd_smn_read(pvt->mc_node_id, mask_reg, &tmp)) {
  1218. *mask = tmp;
  1219. edac_dbg(0, " DCSM%d[%d]=0x%08x reg: 0x%x\n",
  1220. umc, cs, *mask, mask_reg);
  1221. }
  1222. if (!amd_smn_read(pvt->mc_node_id, mask_reg_sec, &tmp)) {
  1223. *mask_sec = tmp;
  1224. edac_dbg(0, " DCSM_SEC%d[%d]=0x%08x reg: 0x%x\n",
  1225. umc, cs, *mask_sec, mask_reg_sec);
  1226. }
  1227. }
  1228. }
  1229. }
  1230. /*
  1231. * Function 2 Offset F10_DCSB0; read in the DCS Base and DCS Mask registers
  1232. */
  1233. static void dct_read_base_mask(struct amd64_pvt *pvt)
  1234. {
  1235. int cs;
  1236. for_each_chip_select(cs, 0, pvt) {
  1237. int reg0 = DCSB0 + (cs * 4);
  1238. int reg1 = DCSB1 + (cs * 4);
  1239. u32 *base0 = &pvt->csels[0].csbases[cs];
  1240. u32 *base1 = &pvt->csels[1].csbases[cs];
  1241. if (!amd64_read_dct_pci_cfg(pvt, 0, reg0, base0))
  1242. edac_dbg(0, " DCSB0[%d]=0x%08x reg: F2x%x\n",
  1243. cs, *base0, reg0);
  1244. if (pvt->fam == 0xf)
  1245. continue;
  1246. if (!amd64_read_dct_pci_cfg(pvt, 1, reg0, base1))
  1247. edac_dbg(0, " DCSB1[%d]=0x%08x reg: F2x%x\n",
  1248. cs, *base1, (pvt->fam == 0x10) ? reg1
  1249. : reg0);
  1250. }
  1251. for_each_chip_select_mask(cs, 0, pvt) {
  1252. int reg0 = DCSM0 + (cs * 4);
  1253. int reg1 = DCSM1 + (cs * 4);
  1254. u32 *mask0 = &pvt->csels[0].csmasks[cs];
  1255. u32 *mask1 = &pvt->csels[1].csmasks[cs];
  1256. if (!amd64_read_dct_pci_cfg(pvt, 0, reg0, mask0))
  1257. edac_dbg(0, " DCSM0[%d]=0x%08x reg: F2x%x\n",
  1258. cs, *mask0, reg0);
  1259. if (pvt->fam == 0xf)
  1260. continue;
  1261. if (!amd64_read_dct_pci_cfg(pvt, 1, reg0, mask1))
  1262. edac_dbg(0, " DCSM1[%d]=0x%08x reg: F2x%x\n",
  1263. cs, *mask1, (pvt->fam == 0x10) ? reg1
  1264. : reg0);
  1265. }
  1266. }
  1267. static void umc_determine_memory_type(struct amd64_pvt *pvt)
  1268. {
  1269. struct amd64_umc *umc;
  1270. u32 i;
  1271. for_each_umc(i) {
  1272. umc = &pvt->umc[i];
  1273. if (!(umc->sdp_ctrl & UMC_SDP_INIT)) {
  1274. umc->dram_type = MEM_EMPTY;
  1275. continue;
  1276. }
  1277. /*
  1278. * Check if the system supports the "DDR Type" field in UMC Config
  1279. * and has DDR5 DIMMs in use.
  1280. */
  1281. if (pvt->flags.zn_regs_v2 && ((umc->umc_cfg & GENMASK(2, 0)) == 0x1)) {
  1282. if (umc->dimm_cfg & BIT(5))
  1283. umc->dram_type = MEM_LRDDR5;
  1284. else if (umc->dimm_cfg & BIT(4))
  1285. umc->dram_type = MEM_RDDR5;
  1286. else
  1287. umc->dram_type = MEM_DDR5;
  1288. } else {
  1289. if (umc->dimm_cfg & BIT(5))
  1290. umc->dram_type = MEM_LRDDR4;
  1291. else if (umc->dimm_cfg & BIT(4))
  1292. umc->dram_type = MEM_RDDR4;
  1293. else
  1294. umc->dram_type = MEM_DDR4;
  1295. }
  1296. edac_dbg(1, " UMC%d DIMM type: %s\n", i, edac_mem_types[umc->dram_type]);
  1297. }
  1298. }
  1299. static void dct_determine_memory_type(struct amd64_pvt *pvt)
  1300. {
  1301. u32 dram_ctrl, dcsm;
  1302. switch (pvt->fam) {
  1303. case 0xf:
  1304. if (pvt->ext_model >= K8_REV_F)
  1305. goto ddr3;
  1306. pvt->dram_type = (pvt->dclr0 & BIT(18)) ? MEM_DDR : MEM_RDDR;
  1307. return;
  1308. case 0x10:
  1309. if (pvt->dchr0 & DDR3_MODE)
  1310. goto ddr3;
  1311. pvt->dram_type = (pvt->dclr0 & BIT(16)) ? MEM_DDR2 : MEM_RDDR2;
  1312. return;
  1313. case 0x15:
  1314. if (pvt->model < 0x60)
  1315. goto ddr3;
  1316. /*
  1317. * Model 0x60h needs special handling:
  1318. *
  1319. * We use a Chip Select value of '0' to obtain dcsm.
  1320. * Theoretically, it is possible to populate LRDIMMs of different
  1321. * 'Rank' value on a DCT. But this is not the common case. So,
  1322. * it's reasonable to assume all DIMMs are going to be of same
  1323. * 'type' until proven otherwise.
  1324. */
  1325. amd64_read_dct_pci_cfg(pvt, 0, DRAM_CONTROL, &dram_ctrl);
  1326. dcsm = pvt->csels[0].csmasks[0];
  1327. if (((dram_ctrl >> 8) & 0x7) == 0x2)
  1328. pvt->dram_type = MEM_DDR4;
  1329. else if (pvt->dclr0 & BIT(16))
  1330. pvt->dram_type = MEM_DDR3;
  1331. else if (dcsm & 0x3)
  1332. pvt->dram_type = MEM_LRDDR3;
  1333. else
  1334. pvt->dram_type = MEM_RDDR3;
  1335. return;
  1336. case 0x16:
  1337. goto ddr3;
  1338. default:
  1339. WARN(1, KERN_ERR "%s: Family??? 0x%x\n", __func__, pvt->fam);
  1340. pvt->dram_type = MEM_EMPTY;
  1341. }
  1342. edac_dbg(1, " DIMM type: %s\n", edac_mem_types[pvt->dram_type]);
  1343. return;
  1344. ddr3:
  1345. pvt->dram_type = (pvt->dclr0 & BIT(16)) ? MEM_DDR3 : MEM_RDDR3;
  1346. }
  1347. /* On F10h and later ErrAddr is MC4_ADDR[47:1] */
  1348. static u64 get_error_address(struct amd64_pvt *pvt, struct mce *m)
  1349. {
  1350. u16 mce_nid = topology_amd_node_id(m->extcpu);
  1351. struct mem_ctl_info *mci;
  1352. u8 start_bit = 1;
  1353. u8 end_bit = 47;
  1354. u64 addr;
  1355. mci = edac_mc_find(mce_nid);
  1356. if (!mci)
  1357. return 0;
  1358. pvt = mci->pvt_info;
  1359. if (pvt->fam == 0xf) {
  1360. start_bit = 3;
  1361. end_bit = 39;
  1362. }
  1363. addr = m->addr & GENMASK_ULL(end_bit, start_bit);
  1364. /*
  1365. * Erratum 637 workaround
  1366. */
  1367. if (pvt->fam == 0x15) {
  1368. u64 cc6_base, tmp_addr;
  1369. u32 tmp;
  1370. u8 intlv_en;
  1371. if ((addr & GENMASK_ULL(47, 24)) >> 24 != 0x00fdf7)
  1372. return addr;
  1373. amd64_read_pci_cfg(pvt->F1, DRAM_LOCAL_NODE_LIM, &tmp);
  1374. intlv_en = tmp >> 21 & 0x7;
  1375. /* add [47:27] + 3 trailing bits */
  1376. cc6_base = (tmp & GENMASK_ULL(20, 0)) << 3;
  1377. /* reverse and add DramIntlvEn */
  1378. cc6_base |= intlv_en ^ 0x7;
  1379. /* pin at [47:24] */
  1380. cc6_base <<= 24;
  1381. if (!intlv_en)
  1382. return cc6_base | (addr & GENMASK_ULL(23, 0));
  1383. amd64_read_pci_cfg(pvt->F1, DRAM_LOCAL_NODE_BASE, &tmp);
  1384. /* faster log2 */
  1385. tmp_addr = (addr & GENMASK_ULL(23, 12)) << __fls(intlv_en + 1);
  1386. /* OR DramIntlvSel into bits [14:12] */
  1387. tmp_addr |= (tmp & GENMASK_ULL(23, 21)) >> 9;
  1388. /* add remaining [11:0] bits from original MC4_ADDR */
  1389. tmp_addr |= addr & GENMASK_ULL(11, 0);
  1390. return cc6_base | tmp_addr;
  1391. }
  1392. return addr;
  1393. }
  1394. static struct pci_dev *pci_get_related_function(unsigned int vendor,
  1395. unsigned int device,
  1396. struct pci_dev *related)
  1397. {
  1398. struct pci_dev *dev = NULL;
  1399. while ((dev = pci_get_device(vendor, device, dev))) {
  1400. if (pci_domain_nr(dev->bus) == pci_domain_nr(related->bus) &&
  1401. (dev->bus->number == related->bus->number) &&
  1402. (PCI_SLOT(dev->devfn) == PCI_SLOT(related->devfn)))
  1403. break;
  1404. }
  1405. return dev;
  1406. }
  1407. static void read_dram_base_limit_regs(struct amd64_pvt *pvt, unsigned range)
  1408. {
  1409. struct amd_northbridge *nb;
  1410. struct pci_dev *f1 = NULL;
  1411. unsigned int pci_func;
  1412. int off = range << 3;
  1413. u32 llim;
  1414. amd64_read_pci_cfg(pvt->F1, DRAM_BASE_LO + off, &pvt->ranges[range].base.lo);
  1415. amd64_read_pci_cfg(pvt->F1, DRAM_LIMIT_LO + off, &pvt->ranges[range].lim.lo);
  1416. if (pvt->fam == 0xf)
  1417. return;
  1418. if (!dram_rw(pvt, range))
  1419. return;
  1420. amd64_read_pci_cfg(pvt->F1, DRAM_BASE_HI + off, &pvt->ranges[range].base.hi);
  1421. amd64_read_pci_cfg(pvt->F1, DRAM_LIMIT_HI + off, &pvt->ranges[range].lim.hi);
  1422. /* F15h: factor in CC6 save area by reading dst node's limit reg */
  1423. if (pvt->fam != 0x15)
  1424. return;
  1425. nb = node_to_amd_nb(dram_dst_node(pvt, range));
  1426. if (WARN_ON(!nb))
  1427. return;
  1428. if (pvt->model == 0x60)
  1429. pci_func = PCI_DEVICE_ID_AMD_15H_M60H_NB_F1;
  1430. else if (pvt->model == 0x30)
  1431. pci_func = PCI_DEVICE_ID_AMD_15H_M30H_NB_F1;
  1432. else
  1433. pci_func = PCI_DEVICE_ID_AMD_15H_NB_F1;
  1434. f1 = pci_get_related_function(nb->misc->vendor, pci_func, nb->misc);
  1435. if (WARN_ON(!f1))
  1436. return;
  1437. amd64_read_pci_cfg(f1, DRAM_LOCAL_NODE_LIM, &llim);
  1438. pvt->ranges[range].lim.lo &= GENMASK_ULL(15, 0);
  1439. /* {[39:27],111b} */
  1440. pvt->ranges[range].lim.lo |= ((llim & 0x1fff) << 3 | 0x7) << 16;
  1441. pvt->ranges[range].lim.hi &= GENMASK_ULL(7, 0);
  1442. /* [47:40] */
  1443. pvt->ranges[range].lim.hi |= llim >> 13;
  1444. pci_dev_put(f1);
  1445. }
  1446. static void k8_map_sysaddr_to_csrow(struct mem_ctl_info *mci, u64 sys_addr,
  1447. struct err_info *err)
  1448. {
  1449. struct amd64_pvt *pvt = mci->pvt_info;
  1450. error_address_to_page_and_offset(sys_addr, err);
  1451. /*
  1452. * Find out which node the error address belongs to. This may be
  1453. * different from the node that detected the error.
  1454. */
  1455. err->src_mci = find_mc_by_sys_addr(mci, sys_addr);
  1456. if (!err->src_mci) {
  1457. amd64_mc_err(mci, "failed to map error addr 0x%lx to a node\n",
  1458. (unsigned long)sys_addr);
  1459. err->err_code = ERR_NODE;
  1460. return;
  1461. }
  1462. /* Now map the sys_addr to a CSROW */
  1463. err->csrow = sys_addr_to_csrow(err->src_mci, sys_addr);
  1464. if (err->csrow < 0) {
  1465. err->err_code = ERR_CSROW;
  1466. return;
  1467. }
  1468. /* CHIPKILL enabled */
  1469. if (pvt->nbcfg & NBCFG_CHIPKILL) {
  1470. err->channel = get_channel_from_ecc_syndrome(mci, err->syndrome);
  1471. if (err->channel < 0) {
  1472. /*
  1473. * Syndrome didn't map, so we don't know which of the
  1474. * 2 DIMMs is in error. So we need to ID 'both' of them
  1475. * as suspect.
  1476. */
  1477. amd64_mc_warn(err->src_mci, "unknown syndrome 0x%04x - "
  1478. "possible error reporting race\n",
  1479. err->syndrome);
  1480. err->err_code = ERR_CHANNEL;
  1481. return;
  1482. }
  1483. } else {
  1484. /*
  1485. * non-chipkill ecc mode
  1486. *
  1487. * The k8 documentation is unclear about how to determine the
  1488. * channel number when using non-chipkill memory. This method
  1489. * was obtained from email communication with someone at AMD.
  1490. * (Wish the email was placed in this comment - norsk)
  1491. */
  1492. err->channel = ((sys_addr & BIT(3)) != 0);
  1493. }
  1494. }
  1495. static int ddr2_cs_size(unsigned i, bool dct_width)
  1496. {
  1497. unsigned shift = 0;
  1498. if (i <= 2)
  1499. shift = i;
  1500. else if (!(i & 0x1))
  1501. shift = i >> 1;
  1502. else
  1503. shift = (i + 1) >> 1;
  1504. return 128 << (shift + !!dct_width);
  1505. }
  1506. static int k8_dbam_to_chip_select(struct amd64_pvt *pvt, u8 dct,
  1507. unsigned cs_mode, int cs_mask_nr)
  1508. {
  1509. u32 dclr = dct ? pvt->dclr1 : pvt->dclr0;
  1510. if (pvt->ext_model >= K8_REV_F) {
  1511. WARN_ON(cs_mode > 11);
  1512. return ddr2_cs_size(cs_mode, dclr & WIDTH_128);
  1513. }
  1514. else if (pvt->ext_model >= K8_REV_D) {
  1515. unsigned diff;
  1516. WARN_ON(cs_mode > 10);
  1517. /*
  1518. * the below calculation, besides trying to win an obfuscated C
  1519. * contest, maps cs_mode values to DIMM chip select sizes. The
  1520. * mappings are:
  1521. *
  1522. * cs_mode CS size (mb)
  1523. * ======= ============
  1524. * 0 32
  1525. * 1 64
  1526. * 2 128
  1527. * 3 128
  1528. * 4 256
  1529. * 5 512
  1530. * 6 256
  1531. * 7 512
  1532. * 8 1024
  1533. * 9 1024
  1534. * 10 2048
  1535. *
  1536. * Basically, it calculates a value with which to shift the
  1537. * smallest CS size of 32MB.
  1538. *
  1539. * ddr[23]_cs_size have a similar purpose.
  1540. */
  1541. diff = cs_mode/3 + (unsigned)(cs_mode > 5);
  1542. return 32 << (cs_mode - diff);
  1543. }
  1544. else {
  1545. WARN_ON(cs_mode > 6);
  1546. return 32 << cs_mode;
  1547. }
  1548. }
  1549. static int ddr3_cs_size(unsigned i, bool dct_width)
  1550. {
  1551. unsigned shift = 0;
  1552. int cs_size = 0;
  1553. if (i == 0 || i == 3 || i == 4)
  1554. cs_size = -1;
  1555. else if (i <= 2)
  1556. shift = i;
  1557. else if (i == 12)
  1558. shift = 7;
  1559. else if (!(i & 0x1))
  1560. shift = i >> 1;
  1561. else
  1562. shift = (i + 1) >> 1;
  1563. if (cs_size != -1)
  1564. cs_size = (128 * (1 << !!dct_width)) << shift;
  1565. return cs_size;
  1566. }
  1567. static int ddr3_lrdimm_cs_size(unsigned i, unsigned rank_multiply)
  1568. {
  1569. unsigned shift = 0;
  1570. int cs_size = 0;
  1571. if (i < 4 || i == 6)
  1572. cs_size = -1;
  1573. else if (i == 12)
  1574. shift = 7;
  1575. else if (!(i & 0x1))
  1576. shift = i >> 1;
  1577. else
  1578. shift = (i + 1) >> 1;
  1579. if (cs_size != -1)
  1580. cs_size = rank_multiply * (128 << shift);
  1581. return cs_size;
  1582. }
  1583. static int ddr4_cs_size(unsigned i)
  1584. {
  1585. int cs_size = 0;
  1586. if (i == 0)
  1587. cs_size = -1;
  1588. else if (i == 1)
  1589. cs_size = 1024;
  1590. else
  1591. /* Min cs_size = 1G */
  1592. cs_size = 1024 * (1 << (i >> 1));
  1593. return cs_size;
  1594. }
  1595. static int f10_dbam_to_chip_select(struct amd64_pvt *pvt, u8 dct,
  1596. unsigned cs_mode, int cs_mask_nr)
  1597. {
  1598. u32 dclr = dct ? pvt->dclr1 : pvt->dclr0;
  1599. WARN_ON(cs_mode > 11);
  1600. if (pvt->dchr0 & DDR3_MODE || pvt->dchr1 & DDR3_MODE)
  1601. return ddr3_cs_size(cs_mode, dclr & WIDTH_128);
  1602. else
  1603. return ddr2_cs_size(cs_mode, dclr & WIDTH_128);
  1604. }
  1605. /*
  1606. * F15h supports only 64bit DCT interfaces
  1607. */
  1608. static int f15_dbam_to_chip_select(struct amd64_pvt *pvt, u8 dct,
  1609. unsigned cs_mode, int cs_mask_nr)
  1610. {
  1611. WARN_ON(cs_mode > 12);
  1612. return ddr3_cs_size(cs_mode, false);
  1613. }
  1614. /* F15h M60h supports DDR4 mapping as well.. */
  1615. static int f15_m60h_dbam_to_chip_select(struct amd64_pvt *pvt, u8 dct,
  1616. unsigned cs_mode, int cs_mask_nr)
  1617. {
  1618. int cs_size;
  1619. u32 dcsm = pvt->csels[dct].csmasks[cs_mask_nr];
  1620. WARN_ON(cs_mode > 12);
  1621. if (pvt->dram_type == MEM_DDR4) {
  1622. if (cs_mode > 9)
  1623. return -1;
  1624. cs_size = ddr4_cs_size(cs_mode);
  1625. } else if (pvt->dram_type == MEM_LRDDR3) {
  1626. unsigned rank_multiply = dcsm & 0xf;
  1627. if (rank_multiply == 3)
  1628. rank_multiply = 4;
  1629. cs_size = ddr3_lrdimm_cs_size(cs_mode, rank_multiply);
  1630. } else {
  1631. /* Minimum cs size is 512mb for F15hM60h*/
  1632. if (cs_mode == 0x1)
  1633. return -1;
  1634. cs_size = ddr3_cs_size(cs_mode, false);
  1635. }
  1636. return cs_size;
  1637. }
  1638. /*
  1639. * F16h and F15h model 30h have only limited cs_modes.
  1640. */
  1641. static int f16_dbam_to_chip_select(struct amd64_pvt *pvt, u8 dct,
  1642. unsigned cs_mode, int cs_mask_nr)
  1643. {
  1644. WARN_ON(cs_mode > 12);
  1645. if (cs_mode == 6 || cs_mode == 8 ||
  1646. cs_mode == 9 || cs_mode == 12)
  1647. return -1;
  1648. else
  1649. return ddr3_cs_size(cs_mode, false);
  1650. }
  1651. static void read_dram_ctl_register(struct amd64_pvt *pvt)
  1652. {
  1653. if (pvt->fam == 0xf)
  1654. return;
  1655. if (!amd64_read_pci_cfg(pvt->F2, DCT_SEL_LO, &pvt->dct_sel_lo)) {
  1656. edac_dbg(0, "F2x110 (DCTSelLow): 0x%08x, High range addrs at: 0x%x\n",
  1657. pvt->dct_sel_lo, dct_sel_baseaddr(pvt));
  1658. edac_dbg(0, " DCTs operate in %s mode\n",
  1659. (dct_ganging_enabled(pvt) ? "ganged" : "unganged"));
  1660. if (!dct_ganging_enabled(pvt))
  1661. edac_dbg(0, " Address range split per DCT: %s\n",
  1662. (dct_high_range_enabled(pvt) ? "yes" : "no"));
  1663. edac_dbg(0, " data interleave for ECC: %s, DRAM cleared since last warm reset: %s\n",
  1664. (dct_data_intlv_enabled(pvt) ? "enabled" : "disabled"),
  1665. (dct_memory_cleared(pvt) ? "yes" : "no"));
  1666. edac_dbg(0, " channel interleave: %s, "
  1667. "interleave bits selector: 0x%x\n",
  1668. (dct_interleave_enabled(pvt) ? "enabled" : "disabled"),
  1669. dct_sel_interleave_addr(pvt));
  1670. }
  1671. amd64_read_pci_cfg(pvt->F2, DCT_SEL_HI, &pvt->dct_sel_hi);
  1672. }
  1673. /*
  1674. * Determine channel (DCT) based on the interleaving mode (see F15h M30h BKDG,
  1675. * 2.10.12 Memory Interleaving Modes).
  1676. */
  1677. static u8 f15_m30h_determine_channel(struct amd64_pvt *pvt, u64 sys_addr,
  1678. u8 intlv_en, int num_dcts_intlv,
  1679. u32 dct_sel)
  1680. {
  1681. u8 channel = 0;
  1682. u8 select;
  1683. if (!(intlv_en))
  1684. return (u8)(dct_sel);
  1685. if (num_dcts_intlv == 2) {
  1686. select = (sys_addr >> 8) & 0x3;
  1687. channel = select ? 0x3 : 0;
  1688. } else if (num_dcts_intlv == 4) {
  1689. u8 intlv_addr = dct_sel_interleave_addr(pvt);
  1690. switch (intlv_addr) {
  1691. case 0x4:
  1692. channel = (sys_addr >> 8) & 0x3;
  1693. break;
  1694. case 0x5:
  1695. channel = (sys_addr >> 9) & 0x3;
  1696. break;
  1697. }
  1698. }
  1699. return channel;
  1700. }
  1701. /*
  1702. * Determine channel (DCT) based on the interleaving mode: F10h BKDG, 2.8.9 Memory
  1703. * Interleaving Modes.
  1704. */
  1705. static u8 f1x_determine_channel(struct amd64_pvt *pvt, u64 sys_addr,
  1706. bool hi_range_sel, u8 intlv_en)
  1707. {
  1708. u8 dct_sel_high = (pvt->dct_sel_lo >> 1) & 1;
  1709. if (dct_ganging_enabled(pvt))
  1710. return 0;
  1711. if (hi_range_sel)
  1712. return dct_sel_high;
  1713. /*
  1714. * see F2x110[DctSelIntLvAddr] - channel interleave mode
  1715. */
  1716. if (dct_interleave_enabled(pvt)) {
  1717. u8 intlv_addr = dct_sel_interleave_addr(pvt);
  1718. /* return DCT select function: 0=DCT0, 1=DCT1 */
  1719. if (!intlv_addr)
  1720. return sys_addr >> 6 & 1;
  1721. if (intlv_addr & 0x2) {
  1722. u8 shift = intlv_addr & 0x1 ? 9 : 6;
  1723. u32 temp = hweight_long((u32) ((sys_addr >> 16) & 0x1F)) & 1;
  1724. return ((sys_addr >> shift) & 1) ^ temp;
  1725. }
  1726. if (intlv_addr & 0x4) {
  1727. u8 shift = intlv_addr & 0x1 ? 9 : 8;
  1728. return (sys_addr >> shift) & 1;
  1729. }
  1730. return (sys_addr >> (12 + hweight8(intlv_en))) & 1;
  1731. }
  1732. if (dct_high_range_enabled(pvt))
  1733. return ~dct_sel_high & 1;
  1734. return 0;
  1735. }
  1736. /* Convert the sys_addr to the normalized DCT address */
  1737. static u64 f1x_get_norm_dct_addr(struct amd64_pvt *pvt, u8 range,
  1738. u64 sys_addr, bool hi_rng,
  1739. u32 dct_sel_base_addr)
  1740. {
  1741. u64 chan_off;
  1742. u64 dram_base = get_dram_base(pvt, range);
  1743. u64 hole_off = f10_dhar_offset(pvt);
  1744. u64 dct_sel_base_off = (u64)(pvt->dct_sel_hi & 0xFFFFFC00) << 16;
  1745. if (hi_rng) {
  1746. /*
  1747. * if
  1748. * base address of high range is below 4Gb
  1749. * (bits [47:27] at [31:11])
  1750. * DRAM address space on this DCT is hoisted above 4Gb &&
  1751. * sys_addr > 4Gb
  1752. *
  1753. * remove hole offset from sys_addr
  1754. * else
  1755. * remove high range offset from sys_addr
  1756. */
  1757. if ((!(dct_sel_base_addr >> 16) ||
  1758. dct_sel_base_addr < dhar_base(pvt)) &&
  1759. dhar_valid(pvt) &&
  1760. (sys_addr >= BIT_64(32)))
  1761. chan_off = hole_off;
  1762. else
  1763. chan_off = dct_sel_base_off;
  1764. } else {
  1765. /*
  1766. * if
  1767. * we have a valid hole &&
  1768. * sys_addr > 4Gb
  1769. *
  1770. * remove hole
  1771. * else
  1772. * remove dram base to normalize to DCT address
  1773. */
  1774. if (dhar_valid(pvt) && (sys_addr >= BIT_64(32)))
  1775. chan_off = hole_off;
  1776. else
  1777. chan_off = dram_base;
  1778. }
  1779. return (sys_addr & GENMASK_ULL(47,6)) - (chan_off & GENMASK_ULL(47,23));
  1780. }
  1781. /*
  1782. * checks if the csrow passed in is marked as SPARED, if so returns the new
  1783. * spare row
  1784. */
  1785. static int f10_process_possible_spare(struct amd64_pvt *pvt, u8 dct, int csrow)
  1786. {
  1787. int tmp_cs;
  1788. if (online_spare_swap_done(pvt, dct) &&
  1789. csrow == online_spare_bad_dramcs(pvt, dct)) {
  1790. for_each_chip_select(tmp_cs, dct, pvt) {
  1791. if (chip_select_base(tmp_cs, dct, pvt) & 0x2) {
  1792. csrow = tmp_cs;
  1793. break;
  1794. }
  1795. }
  1796. }
  1797. return csrow;
  1798. }
  1799. /*
  1800. * Iterate over the DRAM DCT "base" and "mask" registers looking for a
  1801. * SystemAddr match on the specified 'ChannelSelect' and 'NodeID'
  1802. *
  1803. * Return:
  1804. * -EINVAL: NOT FOUND
  1805. * 0..csrow = Chip-Select Row
  1806. */
  1807. static int f1x_lookup_addr_in_dct(u64 in_addr, u8 nid, u8 dct)
  1808. {
  1809. struct mem_ctl_info *mci;
  1810. struct amd64_pvt *pvt;
  1811. u64 cs_base, cs_mask;
  1812. int cs_found = -EINVAL;
  1813. int csrow;
  1814. mci = edac_mc_find(nid);
  1815. if (!mci)
  1816. return cs_found;
  1817. pvt = mci->pvt_info;
  1818. edac_dbg(1, "input addr: 0x%llx, DCT: %d\n", in_addr, dct);
  1819. for_each_chip_select(csrow, dct, pvt) {
  1820. if (!csrow_enabled(csrow, dct, pvt))
  1821. continue;
  1822. get_cs_base_and_mask(pvt, csrow, dct, &cs_base, &cs_mask);
  1823. edac_dbg(1, " CSROW=%d CSBase=0x%llx CSMask=0x%llx\n",
  1824. csrow, cs_base, cs_mask);
  1825. cs_mask = ~cs_mask;
  1826. edac_dbg(1, " (InputAddr & ~CSMask)=0x%llx (CSBase & ~CSMask)=0x%llx\n",
  1827. (in_addr & cs_mask), (cs_base & cs_mask));
  1828. if ((in_addr & cs_mask) == (cs_base & cs_mask)) {
  1829. if (pvt->fam == 0x15 && pvt->model >= 0x30) {
  1830. cs_found = csrow;
  1831. break;
  1832. }
  1833. cs_found = f10_process_possible_spare(pvt, dct, csrow);
  1834. edac_dbg(1, " MATCH csrow=%d\n", cs_found);
  1835. break;
  1836. }
  1837. }
  1838. return cs_found;
  1839. }
  1840. /*
  1841. * See F2x10C. Non-interleaved graphics framebuffer memory under the 16G is
  1842. * swapped with a region located at the bottom of memory so that the GPU can use
  1843. * the interleaved region and thus two channels.
  1844. */
  1845. static u64 f1x_swap_interleaved_region(struct amd64_pvt *pvt, u64 sys_addr)
  1846. {
  1847. u32 swap_reg, swap_base, swap_limit, rgn_size, tmp_addr;
  1848. if (pvt->fam == 0x10) {
  1849. /* only revC3 and revE have that feature */
  1850. if (pvt->model < 4 || (pvt->model < 0xa && pvt->stepping < 3))
  1851. return sys_addr;
  1852. }
  1853. amd64_read_pci_cfg(pvt->F2, SWAP_INTLV_REG, &swap_reg);
  1854. if (!(swap_reg & 0x1))
  1855. return sys_addr;
  1856. swap_base = (swap_reg >> 3) & 0x7f;
  1857. swap_limit = (swap_reg >> 11) & 0x7f;
  1858. rgn_size = (swap_reg >> 20) & 0x7f;
  1859. tmp_addr = sys_addr >> 27;
  1860. if (!(sys_addr >> 34) &&
  1861. (((tmp_addr >= swap_base) &&
  1862. (tmp_addr <= swap_limit)) ||
  1863. (tmp_addr < rgn_size)))
  1864. return sys_addr ^ (u64)swap_base << 27;
  1865. return sys_addr;
  1866. }
  1867. /* For a given @dram_range, check if @sys_addr falls within it. */
  1868. static int f1x_match_to_this_node(struct amd64_pvt *pvt, unsigned range,
  1869. u64 sys_addr, int *chan_sel)
  1870. {
  1871. int cs_found = -EINVAL;
  1872. u64 chan_addr;
  1873. u32 dct_sel_base;
  1874. u8 channel;
  1875. bool high_range = false;
  1876. u8 node_id = dram_dst_node(pvt, range);
  1877. u8 intlv_en = dram_intlv_en(pvt, range);
  1878. u32 intlv_sel = dram_intlv_sel(pvt, range);
  1879. edac_dbg(1, "(range %d) SystemAddr= 0x%llx Limit=0x%llx\n",
  1880. range, sys_addr, get_dram_limit(pvt, range));
  1881. if (dhar_valid(pvt) &&
  1882. dhar_base(pvt) <= sys_addr &&
  1883. sys_addr < BIT_64(32)) {
  1884. amd64_warn("Huh? Address is in the MMIO hole: 0x%016llx\n",
  1885. sys_addr);
  1886. return -EINVAL;
  1887. }
  1888. if (intlv_en && (intlv_sel != ((sys_addr >> 12) & intlv_en)))
  1889. return -EINVAL;
  1890. sys_addr = f1x_swap_interleaved_region(pvt, sys_addr);
  1891. dct_sel_base = dct_sel_baseaddr(pvt);
  1892. /*
  1893. * check whether addresses >= DctSelBaseAddr[47:27] are to be used to
  1894. * select between DCT0 and DCT1.
  1895. */
  1896. if (dct_high_range_enabled(pvt) &&
  1897. !dct_ganging_enabled(pvt) &&
  1898. ((sys_addr >> 27) >= (dct_sel_base >> 11)))
  1899. high_range = true;
  1900. channel = f1x_determine_channel(pvt, sys_addr, high_range, intlv_en);
  1901. chan_addr = f1x_get_norm_dct_addr(pvt, range, sys_addr,
  1902. high_range, dct_sel_base);
  1903. /* Remove node interleaving, see F1x120 */
  1904. if (intlv_en)
  1905. chan_addr = ((chan_addr >> (12 + hweight8(intlv_en))) << 12) |
  1906. (chan_addr & 0xfff);
  1907. /* remove channel interleave */
  1908. if (dct_interleave_enabled(pvt) &&
  1909. !dct_high_range_enabled(pvt) &&
  1910. !dct_ganging_enabled(pvt)) {
  1911. if (dct_sel_interleave_addr(pvt) != 1) {
  1912. if (dct_sel_interleave_addr(pvt) == 0x3)
  1913. /* hash 9 */
  1914. chan_addr = ((chan_addr >> 10) << 9) |
  1915. (chan_addr & 0x1ff);
  1916. else
  1917. /* A[6] or hash 6 */
  1918. chan_addr = ((chan_addr >> 7) << 6) |
  1919. (chan_addr & 0x3f);
  1920. } else
  1921. /* A[12] */
  1922. chan_addr = ((chan_addr >> 13) << 12) |
  1923. (chan_addr & 0xfff);
  1924. }
  1925. edac_dbg(1, " Normalized DCT addr: 0x%llx\n", chan_addr);
  1926. cs_found = f1x_lookup_addr_in_dct(chan_addr, node_id, channel);
  1927. if (cs_found >= 0)
  1928. *chan_sel = channel;
  1929. return cs_found;
  1930. }
  1931. static int f15_m30h_match_to_this_node(struct amd64_pvt *pvt, unsigned range,
  1932. u64 sys_addr, int *chan_sel)
  1933. {
  1934. int cs_found = -EINVAL;
  1935. int num_dcts_intlv = 0;
  1936. u64 chan_addr, chan_offset;
  1937. u64 dct_base, dct_limit;
  1938. u32 dct_cont_base_reg, dct_cont_limit_reg, tmp;
  1939. u8 channel, alias_channel, leg_mmio_hole, dct_sel, dct_offset_en;
  1940. u64 dhar_offset = f10_dhar_offset(pvt);
  1941. u8 intlv_addr = dct_sel_interleave_addr(pvt);
  1942. u8 node_id = dram_dst_node(pvt, range);
  1943. u8 intlv_en = dram_intlv_en(pvt, range);
  1944. amd64_read_pci_cfg(pvt->F1, DRAM_CONT_BASE, &dct_cont_base_reg);
  1945. amd64_read_pci_cfg(pvt->F1, DRAM_CONT_LIMIT, &dct_cont_limit_reg);
  1946. dct_offset_en = (u8) ((dct_cont_base_reg >> 3) & BIT(0));
  1947. dct_sel = (u8) ((dct_cont_base_reg >> 4) & 0x7);
  1948. edac_dbg(1, "(range %d) SystemAddr= 0x%llx Limit=0x%llx\n",
  1949. range, sys_addr, get_dram_limit(pvt, range));
  1950. if (!(get_dram_base(pvt, range) <= sys_addr) &&
  1951. !(get_dram_limit(pvt, range) >= sys_addr))
  1952. return -EINVAL;
  1953. if (dhar_valid(pvt) &&
  1954. dhar_base(pvt) <= sys_addr &&
  1955. sys_addr < BIT_64(32)) {
  1956. amd64_warn("Huh? Address is in the MMIO hole: 0x%016llx\n",
  1957. sys_addr);
  1958. return -EINVAL;
  1959. }
  1960. /* Verify sys_addr is within DCT Range. */
  1961. dct_base = (u64) dct_sel_baseaddr(pvt);
  1962. dct_limit = (dct_cont_limit_reg >> 11) & 0x1FFF;
  1963. if (!(dct_cont_base_reg & BIT(0)) &&
  1964. !(dct_base <= (sys_addr >> 27) &&
  1965. dct_limit >= (sys_addr >> 27)))
  1966. return -EINVAL;
  1967. /* Verify number of dct's that participate in channel interleaving. */
  1968. num_dcts_intlv = (int) hweight8(intlv_en);
  1969. if (!(num_dcts_intlv % 2 == 0) || (num_dcts_intlv > 4))
  1970. return -EINVAL;
  1971. if (pvt->model >= 0x60)
  1972. channel = f1x_determine_channel(pvt, sys_addr, false, intlv_en);
  1973. else
  1974. channel = f15_m30h_determine_channel(pvt, sys_addr, intlv_en,
  1975. num_dcts_intlv, dct_sel);
  1976. /* Verify we stay within the MAX number of channels allowed */
  1977. if (channel > 3)
  1978. return -EINVAL;
  1979. leg_mmio_hole = (u8) (dct_cont_base_reg >> 1 & BIT(0));
  1980. /* Get normalized DCT addr */
  1981. if (leg_mmio_hole && (sys_addr >= BIT_64(32)))
  1982. chan_offset = dhar_offset;
  1983. else
  1984. chan_offset = dct_base << 27;
  1985. chan_addr = sys_addr - chan_offset;
  1986. /* remove channel interleave */
  1987. if (num_dcts_intlv == 2) {
  1988. if (intlv_addr == 0x4)
  1989. chan_addr = ((chan_addr >> 9) << 8) |
  1990. (chan_addr & 0xff);
  1991. else if (intlv_addr == 0x5)
  1992. chan_addr = ((chan_addr >> 10) << 9) |
  1993. (chan_addr & 0x1ff);
  1994. else
  1995. return -EINVAL;
  1996. } else if (num_dcts_intlv == 4) {
  1997. if (intlv_addr == 0x4)
  1998. chan_addr = ((chan_addr >> 10) << 8) |
  1999. (chan_addr & 0xff);
  2000. else if (intlv_addr == 0x5)
  2001. chan_addr = ((chan_addr >> 11) << 9) |
  2002. (chan_addr & 0x1ff);
  2003. else
  2004. return -EINVAL;
  2005. }
  2006. if (dct_offset_en) {
  2007. amd64_read_pci_cfg(pvt->F1,
  2008. DRAM_CONT_HIGH_OFF + (int) channel * 4,
  2009. &tmp);
  2010. chan_addr += (u64) ((tmp >> 11) & 0xfff) << 27;
  2011. }
  2012. f15h_select_dct(pvt, channel);
  2013. edac_dbg(1, " Normalized DCT addr: 0x%llx\n", chan_addr);
  2014. /*
  2015. * Find Chip select:
  2016. * if channel = 3, then alias it to 1. This is because, in F15 M30h,
  2017. * there is support for 4 DCT's, but only 2 are currently functional.
  2018. * They are DCT0 and DCT3. But we have read all registers of DCT3 into
  2019. * pvt->csels[1]. So we need to use '1' here to get correct info.
  2020. * Refer F15 M30h BKDG Section 2.10 and 2.10.3 for clarifications.
  2021. */
  2022. alias_channel = (channel == 3) ? 1 : channel;
  2023. cs_found = f1x_lookup_addr_in_dct(chan_addr, node_id, alias_channel);
  2024. if (cs_found >= 0)
  2025. *chan_sel = alias_channel;
  2026. return cs_found;
  2027. }
  2028. static int f1x_translate_sysaddr_to_cs(struct amd64_pvt *pvt,
  2029. u64 sys_addr,
  2030. int *chan_sel)
  2031. {
  2032. int cs_found = -EINVAL;
  2033. unsigned range;
  2034. for (range = 0; range < DRAM_RANGES; range++) {
  2035. if (!dram_rw(pvt, range))
  2036. continue;
  2037. if (pvt->fam == 0x15 && pvt->model >= 0x30)
  2038. cs_found = f15_m30h_match_to_this_node(pvt, range,
  2039. sys_addr,
  2040. chan_sel);
  2041. else if ((get_dram_base(pvt, range) <= sys_addr) &&
  2042. (get_dram_limit(pvt, range) >= sys_addr)) {
  2043. cs_found = f1x_match_to_this_node(pvt, range,
  2044. sys_addr, chan_sel);
  2045. if (cs_found >= 0)
  2046. break;
  2047. }
  2048. }
  2049. return cs_found;
  2050. }
  2051. /*
  2052. * For reference see "2.8.5 Routing DRAM Requests" in F10 BKDG. This code maps
  2053. * a @sys_addr to NodeID, DCT (channel) and chip select (CSROW).
  2054. *
  2055. * The @sys_addr is usually an error address received from the hardware
  2056. * (MCX_ADDR).
  2057. */
  2058. static void f1x_map_sysaddr_to_csrow(struct mem_ctl_info *mci, u64 sys_addr,
  2059. struct err_info *err)
  2060. {
  2061. struct amd64_pvt *pvt = mci->pvt_info;
  2062. error_address_to_page_and_offset(sys_addr, err);
  2063. err->csrow = f1x_translate_sysaddr_to_cs(pvt, sys_addr, &err->channel);
  2064. if (err->csrow < 0) {
  2065. err->err_code = ERR_CSROW;
  2066. return;
  2067. }
  2068. /*
  2069. * We need the syndromes for channel detection only when we're
  2070. * ganged. Otherwise @chan should already contain the channel at
  2071. * this point.
  2072. */
  2073. if (dct_ganging_enabled(pvt))
  2074. err->channel = get_channel_from_ecc_syndrome(mci, err->syndrome);
  2075. }
  2076. /*
  2077. * These are tables of eigenvectors (one per line) which can be used for the
  2078. * construction of the syndrome tables. The modified syndrome search algorithm
  2079. * uses those to find the symbol in error and thus the DIMM.
  2080. *
  2081. * Algorithm courtesy of Ross LaFetra from AMD.
  2082. */
  2083. static const u16 x4_vectors[] = {
  2084. 0x2f57, 0x1afe, 0x66cc, 0xdd88,
  2085. 0x11eb, 0x3396, 0x7f4c, 0xeac8,
  2086. 0x0001, 0x0002, 0x0004, 0x0008,
  2087. 0x1013, 0x3032, 0x4044, 0x8088,
  2088. 0x106b, 0x30d6, 0x70fc, 0xe0a8,
  2089. 0x4857, 0xc4fe, 0x13cc, 0x3288,
  2090. 0x1ac5, 0x2f4a, 0x5394, 0xa1e8,
  2091. 0x1f39, 0x251e, 0xbd6c, 0x6bd8,
  2092. 0x15c1, 0x2a42, 0x89ac, 0x4758,
  2093. 0x2b03, 0x1602, 0x4f0c, 0xca08,
  2094. 0x1f07, 0x3a0e, 0x6b04, 0xbd08,
  2095. 0x8ba7, 0x465e, 0x244c, 0x1cc8,
  2096. 0x2b87, 0x164e, 0x642c, 0xdc18,
  2097. 0x40b9, 0x80de, 0x1094, 0x20e8,
  2098. 0x27db, 0x1eb6, 0x9dac, 0x7b58,
  2099. 0x11c1, 0x2242, 0x84ac, 0x4c58,
  2100. 0x1be5, 0x2d7a, 0x5e34, 0xa718,
  2101. 0x4b39, 0x8d1e, 0x14b4, 0x28d8,
  2102. 0x4c97, 0xc87e, 0x11fc, 0x33a8,
  2103. 0x8e97, 0x497e, 0x2ffc, 0x1aa8,
  2104. 0x16b3, 0x3d62, 0x4f34, 0x8518,
  2105. 0x1e2f, 0x391a, 0x5cac, 0xf858,
  2106. 0x1d9f, 0x3b7a, 0x572c, 0xfe18,
  2107. 0x15f5, 0x2a5a, 0x5264, 0xa3b8,
  2108. 0x1dbb, 0x3b66, 0x715c, 0xe3f8,
  2109. 0x4397, 0xc27e, 0x17fc, 0x3ea8,
  2110. 0x1617, 0x3d3e, 0x6464, 0xb8b8,
  2111. 0x23ff, 0x12aa, 0xab6c, 0x56d8,
  2112. 0x2dfb, 0x1ba6, 0x913c, 0x7328,
  2113. 0x185d, 0x2ca6, 0x7914, 0x9e28,
  2114. 0x171b, 0x3e36, 0x7d7c, 0xebe8,
  2115. 0x4199, 0x82ee, 0x19f4, 0x2e58,
  2116. 0x4807, 0xc40e, 0x130c, 0x3208,
  2117. 0x1905, 0x2e0a, 0x5804, 0xac08,
  2118. 0x213f, 0x132a, 0xadfc, 0x5ba8,
  2119. 0x19a9, 0x2efe, 0xb5cc, 0x6f88,
  2120. };
  2121. static const u16 x8_vectors[] = {
  2122. 0x0145, 0x028a, 0x2374, 0x43c8, 0xa1f0, 0x0520, 0x0a40, 0x1480,
  2123. 0x0211, 0x0422, 0x0844, 0x1088, 0x01b0, 0x44e0, 0x23c0, 0xed80,
  2124. 0x1011, 0x0116, 0x022c, 0x0458, 0x08b0, 0x8c60, 0x2740, 0x4e80,
  2125. 0x0411, 0x0822, 0x1044, 0x0158, 0x02b0, 0x2360, 0x46c0, 0xab80,
  2126. 0x0811, 0x1022, 0x012c, 0x0258, 0x04b0, 0x4660, 0x8cc0, 0x2780,
  2127. 0x2071, 0x40e2, 0xa0c4, 0x0108, 0x0210, 0x0420, 0x0840, 0x1080,
  2128. 0x4071, 0x80e2, 0x0104, 0x0208, 0x0410, 0x0820, 0x1040, 0x2080,
  2129. 0x8071, 0x0102, 0x0204, 0x0408, 0x0810, 0x1020, 0x2040, 0x4080,
  2130. 0x019d, 0x03d6, 0x136c, 0x2198, 0x50b0, 0xb2e0, 0x0740, 0x0e80,
  2131. 0x0189, 0x03ea, 0x072c, 0x0e58, 0x1cb0, 0x56e0, 0x37c0, 0xf580,
  2132. 0x01fd, 0x0376, 0x06ec, 0x0bb8, 0x1110, 0x2220, 0x4440, 0x8880,
  2133. 0x0163, 0x02c6, 0x1104, 0x0758, 0x0eb0, 0x2be0, 0x6140, 0xc280,
  2134. 0x02fd, 0x01c6, 0x0b5c, 0x1108, 0x07b0, 0x25a0, 0x8840, 0x6180,
  2135. 0x0801, 0x012e, 0x025c, 0x04b8, 0x1370, 0x26e0, 0x57c0, 0xb580,
  2136. 0x0401, 0x0802, 0x015c, 0x02b8, 0x22b0, 0x13e0, 0x7140, 0xe280,
  2137. 0x0201, 0x0402, 0x0804, 0x01b8, 0x11b0, 0x31a0, 0x8040, 0x7180,
  2138. 0x0101, 0x0202, 0x0404, 0x0808, 0x1010, 0x2020, 0x4040, 0x8080,
  2139. 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
  2140. 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000, 0x8000,
  2141. };
  2142. static int decode_syndrome(u16 syndrome, const u16 *vectors, unsigned num_vecs,
  2143. unsigned v_dim)
  2144. {
  2145. unsigned int i, err_sym;
  2146. for (err_sym = 0; err_sym < num_vecs / v_dim; err_sym++) {
  2147. u16 s = syndrome;
  2148. unsigned v_idx = err_sym * v_dim;
  2149. unsigned v_end = (err_sym + 1) * v_dim;
  2150. /* walk over all 16 bits of the syndrome */
  2151. for (i = 1; i < (1U << 16); i <<= 1) {
  2152. /* if bit is set in that eigenvector... */
  2153. if (v_idx < v_end && vectors[v_idx] & i) {
  2154. u16 ev_comp = vectors[v_idx++];
  2155. /* ... and bit set in the modified syndrome, */
  2156. if (s & i) {
  2157. /* remove it. */
  2158. s ^= ev_comp;
  2159. if (!s)
  2160. return err_sym;
  2161. }
  2162. } else if (s & i)
  2163. /* can't get to zero, move to next symbol */
  2164. break;
  2165. }
  2166. }
  2167. edac_dbg(0, "syndrome(%x) not found\n", syndrome);
  2168. return -1;
  2169. }
  2170. static int map_err_sym_to_channel(int err_sym, int sym_size)
  2171. {
  2172. if (sym_size == 4)
  2173. switch (err_sym) {
  2174. case 0x20:
  2175. case 0x21:
  2176. return 0;
  2177. case 0x22:
  2178. case 0x23:
  2179. return 1;
  2180. default:
  2181. return err_sym >> 4;
  2182. }
  2183. /* x8 symbols */
  2184. else
  2185. switch (err_sym) {
  2186. /* imaginary bits not in a DIMM */
  2187. case 0x10:
  2188. WARN(1, KERN_ERR "Invalid error symbol: 0x%x\n",
  2189. err_sym);
  2190. return -1;
  2191. case 0x11:
  2192. return 0;
  2193. case 0x12:
  2194. return 1;
  2195. default:
  2196. return err_sym >> 3;
  2197. }
  2198. return -1;
  2199. }
  2200. static int get_channel_from_ecc_syndrome(struct mem_ctl_info *mci, u16 syndrome)
  2201. {
  2202. struct amd64_pvt *pvt = mci->pvt_info;
  2203. int err_sym = -1;
  2204. if (pvt->ecc_sym_sz == 8)
  2205. err_sym = decode_syndrome(syndrome, x8_vectors,
  2206. ARRAY_SIZE(x8_vectors),
  2207. pvt->ecc_sym_sz);
  2208. else if (pvt->ecc_sym_sz == 4)
  2209. err_sym = decode_syndrome(syndrome, x4_vectors,
  2210. ARRAY_SIZE(x4_vectors),
  2211. pvt->ecc_sym_sz);
  2212. else {
  2213. amd64_warn("Illegal syndrome type: %u\n", pvt->ecc_sym_sz);
  2214. return err_sym;
  2215. }
  2216. return map_err_sym_to_channel(err_sym, pvt->ecc_sym_sz);
  2217. }
  2218. static void __log_ecc_error(struct mem_ctl_info *mci, struct err_info *err,
  2219. u8 ecc_type)
  2220. {
  2221. enum hw_event_mc_err_type err_type;
  2222. const char *string;
  2223. if (ecc_type == 2)
  2224. err_type = HW_EVENT_ERR_CORRECTED;
  2225. else if (ecc_type == 1)
  2226. err_type = HW_EVENT_ERR_UNCORRECTED;
  2227. else if (ecc_type == 3)
  2228. err_type = HW_EVENT_ERR_DEFERRED;
  2229. else {
  2230. WARN(1, "Something is rotten in the state of Denmark.\n");
  2231. return;
  2232. }
  2233. switch (err->err_code) {
  2234. case DECODE_OK:
  2235. string = "";
  2236. break;
  2237. case ERR_NODE:
  2238. string = "Failed to map error addr to a node";
  2239. break;
  2240. case ERR_CSROW:
  2241. string = "Failed to map error addr to a csrow";
  2242. break;
  2243. case ERR_CHANNEL:
  2244. string = "Unknown syndrome - possible error reporting race";
  2245. break;
  2246. case ERR_SYND:
  2247. string = "MCA_SYND not valid - unknown syndrome and csrow";
  2248. break;
  2249. case ERR_NORM_ADDR:
  2250. string = "Cannot decode normalized address";
  2251. break;
  2252. default:
  2253. string = "WTF error";
  2254. break;
  2255. }
  2256. edac_mc_handle_error(err_type, mci, 1,
  2257. err->page, err->offset, err->syndrome,
  2258. err->csrow, err->channel, -1,
  2259. string, "");
  2260. }
  2261. static inline void decode_bus_error(int node_id, struct mce *m)
  2262. {
  2263. struct mem_ctl_info *mci;
  2264. struct amd64_pvt *pvt;
  2265. u8 ecc_type = (m->status >> 45) & 0x3;
  2266. u8 xec = XEC(m->status, 0x1f);
  2267. u16 ec = EC(m->status);
  2268. u64 sys_addr;
  2269. struct err_info err;
  2270. mci = edac_mc_find(node_id);
  2271. if (!mci)
  2272. return;
  2273. pvt = mci->pvt_info;
  2274. /* Bail out early if this was an 'observed' error */
  2275. if (PP(ec) == NBSL_PP_OBS)
  2276. return;
  2277. /* Do only ECC errors */
  2278. if (xec && xec != F10_NBSL_EXT_ERR_ECC)
  2279. return;
  2280. memset(&err, 0, sizeof(err));
  2281. sys_addr = get_error_address(pvt, m);
  2282. if (ecc_type == 2)
  2283. err.syndrome = extract_syndrome(m->status);
  2284. pvt->ops->map_sysaddr_to_csrow(mci, sys_addr, &err);
  2285. __log_ecc_error(mci, &err, ecc_type);
  2286. }
  2287. /*
  2288. * To find the UMC channel represented by this bank we need to match on its
  2289. * instance_id. The instance_id of a bank is held in the lower 32 bits of its
  2290. * IPID.
  2291. *
  2292. * Currently, we can derive the channel number by looking at the 6th nibble in
  2293. * the instance_id. For example, instance_id=0xYXXXXX where Y is the channel
  2294. * number.
  2295. *
  2296. * For DRAM ECC errors, the Chip Select number is given in bits [2:0] of
  2297. * the MCA_SYND[ErrorInformation] field.
  2298. */
  2299. static void umc_get_err_info(struct mce *m, struct err_info *err)
  2300. {
  2301. err->channel = (m->ipid & GENMASK(31, 0)) >> 20;
  2302. err->csrow = m->synd & 0x7;
  2303. }
  2304. static void decode_umc_error(int node_id, struct mce *m)
  2305. {
  2306. u8 ecc_type = (m->status >> 45) & 0x3;
  2307. struct mem_ctl_info *mci;
  2308. unsigned long sys_addr;
  2309. struct amd64_pvt *pvt;
  2310. struct atl_err a_err;
  2311. struct err_info err;
  2312. node_id = fixup_node_id(node_id, m);
  2313. mci = edac_mc_find(node_id);
  2314. if (!mci)
  2315. return;
  2316. pvt = mci->pvt_info;
  2317. memset(&err, 0, sizeof(err));
  2318. if (m->status & MCI_STATUS_DEFERRED)
  2319. ecc_type = 3;
  2320. if (!(m->status & MCI_STATUS_SYNDV)) {
  2321. err.err_code = ERR_SYND;
  2322. goto log_error;
  2323. }
  2324. if (ecc_type == 2) {
  2325. u8 length = (m->synd >> 18) & 0x3f;
  2326. if (length)
  2327. err.syndrome = (m->synd >> 32) & GENMASK(length - 1, 0);
  2328. else
  2329. err.err_code = ERR_CHANNEL;
  2330. }
  2331. pvt->ops->get_err_info(m, &err);
  2332. a_err.addr = m->addr;
  2333. a_err.ipid = m->ipid;
  2334. a_err.cpu = m->extcpu;
  2335. sys_addr = amd_convert_umc_mca_addr_to_sys_addr(&a_err);
  2336. if (IS_ERR_VALUE(sys_addr)) {
  2337. err.err_code = ERR_NORM_ADDR;
  2338. goto log_error;
  2339. }
  2340. error_address_to_page_and_offset(sys_addr, &err);
  2341. log_error:
  2342. __log_ecc_error(mci, &err, ecc_type);
  2343. }
  2344. /*
  2345. * Use pvt->F3 which contains the F3 CPU PCI device to get the related
  2346. * F1 (AddrMap) and F2 (Dct) devices. Return negative value on error.
  2347. */
  2348. static int
  2349. reserve_mc_sibling_devs(struct amd64_pvt *pvt, u16 pci_id1, u16 pci_id2)
  2350. {
  2351. /* Reserve the ADDRESS MAP Device */
  2352. pvt->F1 = pci_get_related_function(pvt->F3->vendor, pci_id1, pvt->F3);
  2353. if (!pvt->F1) {
  2354. edac_dbg(1, "F1 not found: device 0x%x\n", pci_id1);
  2355. return -ENODEV;
  2356. }
  2357. /* Reserve the DCT Device */
  2358. pvt->F2 = pci_get_related_function(pvt->F3->vendor, pci_id2, pvt->F3);
  2359. if (!pvt->F2) {
  2360. pci_dev_put(pvt->F1);
  2361. pvt->F1 = NULL;
  2362. edac_dbg(1, "F2 not found: device 0x%x\n", pci_id2);
  2363. return -ENODEV;
  2364. }
  2365. if (!pci_ctl_dev)
  2366. pci_ctl_dev = &pvt->F2->dev;
  2367. edac_dbg(1, "F1: %s\n", pci_name(pvt->F1));
  2368. edac_dbg(1, "F2: %s\n", pci_name(pvt->F2));
  2369. edac_dbg(1, "F3: %s\n", pci_name(pvt->F3));
  2370. return 0;
  2371. }
  2372. static void determine_ecc_sym_sz(struct amd64_pvt *pvt)
  2373. {
  2374. pvt->ecc_sym_sz = 4;
  2375. if (pvt->fam >= 0x10) {
  2376. u32 tmp;
  2377. amd64_read_pci_cfg(pvt->F3, EXT_NB_MCA_CFG, &tmp);
  2378. /* F16h has only DCT0, so no need to read dbam1. */
  2379. if (pvt->fam != 0x16)
  2380. amd64_read_dct_pci_cfg(pvt, 1, DBAM0, &pvt->dbam1);
  2381. /* F10h, revD and later can do x8 ECC too. */
  2382. if ((pvt->fam > 0x10 || pvt->model > 7) && tmp & BIT(25))
  2383. pvt->ecc_sym_sz = 8;
  2384. }
  2385. }
  2386. /*
  2387. * Retrieve the hardware registers of the memory controller.
  2388. */
  2389. static void umc_read_mc_regs(struct amd64_pvt *pvt)
  2390. {
  2391. u8 nid = pvt->mc_node_id;
  2392. struct amd64_umc *umc;
  2393. u32 i, tmp, umc_base;
  2394. /* Read registers from each UMC */
  2395. for_each_umc(i) {
  2396. umc_base = get_umc_base(i);
  2397. umc = &pvt->umc[i];
  2398. if (!amd_smn_read(nid, umc_base + get_umc_reg(pvt, UMCCH_DIMM_CFG), &tmp))
  2399. umc->dimm_cfg = tmp;
  2400. if (!amd_smn_read(nid, umc_base + UMCCH_UMC_CFG, &tmp))
  2401. umc->umc_cfg = tmp;
  2402. if (!amd_smn_read(nid, umc_base + UMCCH_SDP_CTRL, &tmp))
  2403. umc->sdp_ctrl = tmp;
  2404. if (!amd_smn_read(nid, umc_base + UMCCH_ECC_CTRL, &tmp))
  2405. umc->ecc_ctrl = tmp;
  2406. if (!amd_smn_read(nid, umc_base + UMCCH_UMC_CAP_HI, &tmp))
  2407. umc->umc_cap_hi = tmp;
  2408. }
  2409. }
  2410. /*
  2411. * Retrieve the hardware registers of the memory controller (this includes the
  2412. * 'Address Map' and 'Misc' device regs)
  2413. */
  2414. static void dct_read_mc_regs(struct amd64_pvt *pvt)
  2415. {
  2416. unsigned int range;
  2417. u64 msr_val;
  2418. /*
  2419. * Retrieve TOP_MEM and TOP_MEM2; no masking off of reserved bits since
  2420. * those are Read-As-Zero.
  2421. */
  2422. rdmsrl(MSR_K8_TOP_MEM1, pvt->top_mem);
  2423. edac_dbg(0, " TOP_MEM: 0x%016llx\n", pvt->top_mem);
  2424. /* Check first whether TOP_MEM2 is enabled: */
  2425. rdmsrl(MSR_AMD64_SYSCFG, msr_val);
  2426. if (msr_val & BIT(21)) {
  2427. rdmsrl(MSR_K8_TOP_MEM2, pvt->top_mem2);
  2428. edac_dbg(0, " TOP_MEM2: 0x%016llx\n", pvt->top_mem2);
  2429. } else {
  2430. edac_dbg(0, " TOP_MEM2 disabled\n");
  2431. }
  2432. amd64_read_pci_cfg(pvt->F3, NBCAP, &pvt->nbcap);
  2433. read_dram_ctl_register(pvt);
  2434. for (range = 0; range < DRAM_RANGES; range++) {
  2435. u8 rw;
  2436. /* read settings for this DRAM range */
  2437. read_dram_base_limit_regs(pvt, range);
  2438. rw = dram_rw(pvt, range);
  2439. if (!rw)
  2440. continue;
  2441. edac_dbg(1, " DRAM range[%d], base: 0x%016llx; limit: 0x%016llx\n",
  2442. range,
  2443. get_dram_base(pvt, range),
  2444. get_dram_limit(pvt, range));
  2445. edac_dbg(1, " IntlvEn=%s; Range access: %s%s IntlvSel=%d DstNode=%d\n",
  2446. dram_intlv_en(pvt, range) ? "Enabled" : "Disabled",
  2447. (rw & 0x1) ? "R" : "-",
  2448. (rw & 0x2) ? "W" : "-",
  2449. dram_intlv_sel(pvt, range),
  2450. dram_dst_node(pvt, range));
  2451. }
  2452. amd64_read_pci_cfg(pvt->F1, DHAR, &pvt->dhar);
  2453. amd64_read_dct_pci_cfg(pvt, 0, DBAM0, &pvt->dbam0);
  2454. amd64_read_pci_cfg(pvt->F3, F10_ONLINE_SPARE, &pvt->online_spare);
  2455. amd64_read_dct_pci_cfg(pvt, 0, DCLR0, &pvt->dclr0);
  2456. amd64_read_dct_pci_cfg(pvt, 0, DCHR0, &pvt->dchr0);
  2457. if (!dct_ganging_enabled(pvt)) {
  2458. amd64_read_dct_pci_cfg(pvt, 1, DCLR0, &pvt->dclr1);
  2459. amd64_read_dct_pci_cfg(pvt, 1, DCHR0, &pvt->dchr1);
  2460. }
  2461. determine_ecc_sym_sz(pvt);
  2462. }
  2463. /*
  2464. * NOTE: CPU Revision Dependent code
  2465. *
  2466. * Input:
  2467. * @csrow_nr ChipSelect Row Number (0..NUM_CHIPSELECTS-1)
  2468. * k8 private pointer to -->
  2469. * DRAM Bank Address mapping register
  2470. * node_id
  2471. * DCL register where dual_channel_active is
  2472. *
  2473. * The DBAM register consists of 4 sets of 4 bits each definitions:
  2474. *
  2475. * Bits: CSROWs
  2476. * 0-3 CSROWs 0 and 1
  2477. * 4-7 CSROWs 2 and 3
  2478. * 8-11 CSROWs 4 and 5
  2479. * 12-15 CSROWs 6 and 7
  2480. *
  2481. * Values range from: 0 to 15
  2482. * The meaning of the values depends on CPU revision and dual-channel state,
  2483. * see relevant BKDG more info.
  2484. *
  2485. * The memory controller provides for total of only 8 CSROWs in its current
  2486. * architecture. Each "pair" of CSROWs normally represents just one DIMM in
  2487. * single channel or two (2) DIMMs in dual channel mode.
  2488. *
  2489. * The following code logic collapses the various tables for CSROW based on CPU
  2490. * revision.
  2491. *
  2492. * Returns:
  2493. * The number of PAGE_SIZE pages on the specified CSROW number it
  2494. * encompasses
  2495. *
  2496. */
  2497. static u32 dct_get_csrow_nr_pages(struct amd64_pvt *pvt, u8 dct, int csrow_nr)
  2498. {
  2499. u32 dbam = dct ? pvt->dbam1 : pvt->dbam0;
  2500. u32 cs_mode, nr_pages;
  2501. csrow_nr >>= 1;
  2502. cs_mode = DBAM_DIMM(csrow_nr, dbam);
  2503. nr_pages = pvt->ops->dbam_to_cs(pvt, dct, cs_mode, csrow_nr);
  2504. nr_pages <<= 20 - PAGE_SHIFT;
  2505. edac_dbg(0, "csrow: %d, channel: %d, DBAM idx: %d\n",
  2506. csrow_nr, dct, cs_mode);
  2507. edac_dbg(0, "nr_pages/channel: %u\n", nr_pages);
  2508. return nr_pages;
  2509. }
  2510. static u32 umc_get_csrow_nr_pages(struct amd64_pvt *pvt, u8 dct, int csrow_nr_orig)
  2511. {
  2512. int csrow_nr = csrow_nr_orig;
  2513. u32 cs_mode, nr_pages;
  2514. cs_mode = umc_get_cs_mode(csrow_nr >> 1, dct, pvt);
  2515. nr_pages = umc_addr_mask_to_cs_size(pvt, dct, cs_mode, csrow_nr);
  2516. nr_pages <<= 20 - PAGE_SHIFT;
  2517. edac_dbg(0, "csrow: %d, channel: %d, cs_mode %d\n",
  2518. csrow_nr_orig, dct, cs_mode);
  2519. edac_dbg(0, "nr_pages/channel: %u\n", nr_pages);
  2520. return nr_pages;
  2521. }
  2522. static void umc_init_csrows(struct mem_ctl_info *mci)
  2523. {
  2524. struct amd64_pvt *pvt = mci->pvt_info;
  2525. enum edac_type edac_mode = EDAC_NONE;
  2526. enum dev_type dev_type = DEV_UNKNOWN;
  2527. struct dimm_info *dimm;
  2528. u8 umc, cs;
  2529. if (mci->edac_ctl_cap & EDAC_FLAG_S16ECD16ED) {
  2530. edac_mode = EDAC_S16ECD16ED;
  2531. dev_type = DEV_X16;
  2532. } else if (mci->edac_ctl_cap & EDAC_FLAG_S8ECD8ED) {
  2533. edac_mode = EDAC_S8ECD8ED;
  2534. dev_type = DEV_X8;
  2535. } else if (mci->edac_ctl_cap & EDAC_FLAG_S4ECD4ED) {
  2536. edac_mode = EDAC_S4ECD4ED;
  2537. dev_type = DEV_X4;
  2538. } else if (mci->edac_ctl_cap & EDAC_FLAG_SECDED) {
  2539. edac_mode = EDAC_SECDED;
  2540. }
  2541. for_each_umc(umc) {
  2542. for_each_chip_select(cs, umc, pvt) {
  2543. if (!csrow_enabled(cs, umc, pvt))
  2544. continue;
  2545. dimm = mci->csrows[cs]->channels[umc]->dimm;
  2546. edac_dbg(1, "MC node: %d, csrow: %d\n",
  2547. pvt->mc_node_id, cs);
  2548. dimm->nr_pages = umc_get_csrow_nr_pages(pvt, umc, cs);
  2549. dimm->mtype = pvt->umc[umc].dram_type;
  2550. dimm->edac_mode = edac_mode;
  2551. dimm->dtype = dev_type;
  2552. dimm->grain = 64;
  2553. }
  2554. }
  2555. }
  2556. /*
  2557. * Initialize the array of csrow attribute instances, based on the values
  2558. * from pci config hardware registers.
  2559. */
  2560. static void dct_init_csrows(struct mem_ctl_info *mci)
  2561. {
  2562. struct amd64_pvt *pvt = mci->pvt_info;
  2563. enum edac_type edac_mode = EDAC_NONE;
  2564. struct csrow_info *csrow;
  2565. struct dimm_info *dimm;
  2566. int nr_pages = 0;
  2567. int i, j;
  2568. u32 val;
  2569. amd64_read_pci_cfg(pvt->F3, NBCFG, &val);
  2570. pvt->nbcfg = val;
  2571. edac_dbg(0, "node %d, NBCFG=0x%08x[ChipKillEccCap: %d|DramEccEn: %d]\n",
  2572. pvt->mc_node_id, val,
  2573. !!(val & NBCFG_CHIPKILL), !!(val & NBCFG_ECC_ENABLE));
  2574. /*
  2575. * We iterate over DCT0 here but we look at DCT1 in parallel, if needed.
  2576. */
  2577. for_each_chip_select(i, 0, pvt) {
  2578. bool row_dct0 = !!csrow_enabled(i, 0, pvt);
  2579. bool row_dct1 = false;
  2580. if (pvt->fam != 0xf)
  2581. row_dct1 = !!csrow_enabled(i, 1, pvt);
  2582. if (!row_dct0 && !row_dct1)
  2583. continue;
  2584. csrow = mci->csrows[i];
  2585. edac_dbg(1, "MC node: %d, csrow: %d\n",
  2586. pvt->mc_node_id, i);
  2587. if (row_dct0) {
  2588. nr_pages = dct_get_csrow_nr_pages(pvt, 0, i);
  2589. csrow->channels[0]->dimm->nr_pages = nr_pages;
  2590. }
  2591. /* K8 has only one DCT */
  2592. if (pvt->fam != 0xf && row_dct1) {
  2593. int row_dct1_pages = dct_get_csrow_nr_pages(pvt, 1, i);
  2594. csrow->channels[1]->dimm->nr_pages = row_dct1_pages;
  2595. nr_pages += row_dct1_pages;
  2596. }
  2597. edac_dbg(1, "Total csrow%d pages: %u\n", i, nr_pages);
  2598. /* Determine DIMM ECC mode: */
  2599. if (pvt->nbcfg & NBCFG_ECC_ENABLE) {
  2600. edac_mode = (pvt->nbcfg & NBCFG_CHIPKILL)
  2601. ? EDAC_S4ECD4ED
  2602. : EDAC_SECDED;
  2603. }
  2604. for (j = 0; j < pvt->max_mcs; j++) {
  2605. dimm = csrow->channels[j]->dimm;
  2606. dimm->mtype = pvt->dram_type;
  2607. dimm->edac_mode = edac_mode;
  2608. dimm->grain = 64;
  2609. }
  2610. }
  2611. }
  2612. /* get all cores on this DCT */
  2613. static void get_cpus_on_this_dct_cpumask(struct cpumask *mask, u16 nid)
  2614. {
  2615. int cpu;
  2616. for_each_online_cpu(cpu)
  2617. if (topology_amd_node_id(cpu) == nid)
  2618. cpumask_set_cpu(cpu, mask);
  2619. }
  2620. /* check MCG_CTL on all the cpus on this node */
  2621. static bool nb_mce_bank_enabled_on_node(u16 nid)
  2622. {
  2623. cpumask_var_t mask;
  2624. int cpu, nbe;
  2625. bool ret = false;
  2626. if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
  2627. amd64_warn("%s: Error allocating mask\n", __func__);
  2628. return false;
  2629. }
  2630. get_cpus_on_this_dct_cpumask(mask, nid);
  2631. rdmsr_on_cpus(mask, MSR_IA32_MCG_CTL, msrs);
  2632. for_each_cpu(cpu, mask) {
  2633. struct msr *reg = per_cpu_ptr(msrs, cpu);
  2634. nbe = reg->l & MSR_MCGCTL_NBE;
  2635. edac_dbg(0, "core: %u, MCG_CTL: 0x%llx, NB MSR is %s\n",
  2636. cpu, reg->q,
  2637. (nbe ? "enabled" : "disabled"));
  2638. if (!nbe)
  2639. goto out;
  2640. }
  2641. ret = true;
  2642. out:
  2643. free_cpumask_var(mask);
  2644. return ret;
  2645. }
  2646. static int toggle_ecc_err_reporting(struct ecc_settings *s, u16 nid, bool on)
  2647. {
  2648. cpumask_var_t cmask;
  2649. int cpu;
  2650. if (!zalloc_cpumask_var(&cmask, GFP_KERNEL)) {
  2651. amd64_warn("%s: error allocating mask\n", __func__);
  2652. return -ENOMEM;
  2653. }
  2654. get_cpus_on_this_dct_cpumask(cmask, nid);
  2655. rdmsr_on_cpus(cmask, MSR_IA32_MCG_CTL, msrs);
  2656. for_each_cpu(cpu, cmask) {
  2657. struct msr *reg = per_cpu_ptr(msrs, cpu);
  2658. if (on) {
  2659. if (reg->l & MSR_MCGCTL_NBE)
  2660. s->flags.nb_mce_enable = 1;
  2661. reg->l |= MSR_MCGCTL_NBE;
  2662. } else {
  2663. /*
  2664. * Turn off NB MCE reporting only when it was off before
  2665. */
  2666. if (!s->flags.nb_mce_enable)
  2667. reg->l &= ~MSR_MCGCTL_NBE;
  2668. }
  2669. }
  2670. wrmsr_on_cpus(cmask, MSR_IA32_MCG_CTL, msrs);
  2671. free_cpumask_var(cmask);
  2672. return 0;
  2673. }
  2674. static bool enable_ecc_error_reporting(struct ecc_settings *s, u16 nid,
  2675. struct pci_dev *F3)
  2676. {
  2677. bool ret = true;
  2678. u32 value, mask = 0x3; /* UECC/CECC enable */
  2679. if (toggle_ecc_err_reporting(s, nid, ON)) {
  2680. amd64_warn("Error enabling ECC reporting over MCGCTL!\n");
  2681. return false;
  2682. }
  2683. amd64_read_pci_cfg(F3, NBCTL, &value);
  2684. s->old_nbctl = value & mask;
  2685. s->nbctl_valid = true;
  2686. value |= mask;
  2687. amd64_write_pci_cfg(F3, NBCTL, value);
  2688. amd64_read_pci_cfg(F3, NBCFG, &value);
  2689. edac_dbg(0, "1: node %d, NBCFG=0x%08x[DramEccEn: %d]\n",
  2690. nid, value, !!(value & NBCFG_ECC_ENABLE));
  2691. if (!(value & NBCFG_ECC_ENABLE)) {
  2692. amd64_warn("DRAM ECC disabled on this node, enabling...\n");
  2693. s->flags.nb_ecc_prev = 0;
  2694. /* Attempt to turn on DRAM ECC Enable */
  2695. value |= NBCFG_ECC_ENABLE;
  2696. amd64_write_pci_cfg(F3, NBCFG, value);
  2697. amd64_read_pci_cfg(F3, NBCFG, &value);
  2698. if (!(value & NBCFG_ECC_ENABLE)) {
  2699. amd64_warn("Hardware rejected DRAM ECC enable,"
  2700. "check memory DIMM configuration.\n");
  2701. ret = false;
  2702. } else {
  2703. amd64_info("Hardware accepted DRAM ECC Enable\n");
  2704. }
  2705. } else {
  2706. s->flags.nb_ecc_prev = 1;
  2707. }
  2708. edac_dbg(0, "2: node %d, NBCFG=0x%08x[DramEccEn: %d]\n",
  2709. nid, value, !!(value & NBCFG_ECC_ENABLE));
  2710. return ret;
  2711. }
  2712. static void restore_ecc_error_reporting(struct ecc_settings *s, u16 nid,
  2713. struct pci_dev *F3)
  2714. {
  2715. u32 value, mask = 0x3; /* UECC/CECC enable */
  2716. if (!s->nbctl_valid)
  2717. return;
  2718. amd64_read_pci_cfg(F3, NBCTL, &value);
  2719. value &= ~mask;
  2720. value |= s->old_nbctl;
  2721. amd64_write_pci_cfg(F3, NBCTL, value);
  2722. /* restore previous BIOS DRAM ECC "off" setting we force-enabled */
  2723. if (!s->flags.nb_ecc_prev) {
  2724. amd64_read_pci_cfg(F3, NBCFG, &value);
  2725. value &= ~NBCFG_ECC_ENABLE;
  2726. amd64_write_pci_cfg(F3, NBCFG, value);
  2727. }
  2728. /* restore the NB Enable MCGCTL bit */
  2729. if (toggle_ecc_err_reporting(s, nid, OFF))
  2730. amd64_warn("Error restoring NB MCGCTL settings!\n");
  2731. }
  2732. static bool dct_ecc_enabled(struct amd64_pvt *pvt)
  2733. {
  2734. u16 nid = pvt->mc_node_id;
  2735. bool nb_mce_en = false;
  2736. u8 ecc_en = 0;
  2737. u32 value;
  2738. amd64_read_pci_cfg(pvt->F3, NBCFG, &value);
  2739. ecc_en = !!(value & NBCFG_ECC_ENABLE);
  2740. nb_mce_en = nb_mce_bank_enabled_on_node(nid);
  2741. if (!nb_mce_en)
  2742. edac_dbg(0, "NB MCE bank disabled, set MSR 0x%08x[4] on node %d to enable.\n",
  2743. MSR_IA32_MCG_CTL, nid);
  2744. edac_dbg(3, "Node %d: DRAM ECC %s.\n", nid, (ecc_en ? "enabled" : "disabled"));
  2745. if (!ecc_en || !nb_mce_en)
  2746. return false;
  2747. else
  2748. return true;
  2749. }
  2750. static bool umc_ecc_enabled(struct amd64_pvt *pvt)
  2751. {
  2752. struct amd64_umc *umc;
  2753. bool ecc_en = false;
  2754. int i;
  2755. /* Check whether at least one UMC is enabled: */
  2756. for_each_umc(i) {
  2757. umc = &pvt->umc[i];
  2758. if (umc->sdp_ctrl & UMC_SDP_INIT &&
  2759. umc->umc_cap_hi & UMC_ECC_ENABLED) {
  2760. ecc_en = true;
  2761. break;
  2762. }
  2763. }
  2764. edac_dbg(3, "Node %d: DRAM ECC %s.\n", pvt->mc_node_id, (ecc_en ? "enabled" : "disabled"));
  2765. return ecc_en;
  2766. }
  2767. static inline void
  2768. umc_determine_edac_ctl_cap(struct mem_ctl_info *mci, struct amd64_pvt *pvt)
  2769. {
  2770. u8 i, ecc_en = 1, cpk_en = 1, dev_x4 = 1, dev_x16 = 1;
  2771. for_each_umc(i) {
  2772. if (pvt->umc[i].sdp_ctrl & UMC_SDP_INIT) {
  2773. ecc_en &= !!(pvt->umc[i].umc_cap_hi & UMC_ECC_ENABLED);
  2774. cpk_en &= !!(pvt->umc[i].umc_cap_hi & UMC_ECC_CHIPKILL_CAP);
  2775. dev_x4 &= !!(pvt->umc[i].dimm_cfg & BIT(6));
  2776. dev_x16 &= !!(pvt->umc[i].dimm_cfg & BIT(7));
  2777. }
  2778. }
  2779. /* Set chipkill only if ECC is enabled: */
  2780. if (ecc_en) {
  2781. mci->edac_ctl_cap |= EDAC_FLAG_SECDED;
  2782. if (!cpk_en)
  2783. return;
  2784. if (dev_x4)
  2785. mci->edac_ctl_cap |= EDAC_FLAG_S4ECD4ED;
  2786. else if (dev_x16)
  2787. mci->edac_ctl_cap |= EDAC_FLAG_S16ECD16ED;
  2788. else
  2789. mci->edac_ctl_cap |= EDAC_FLAG_S8ECD8ED;
  2790. }
  2791. }
  2792. static void dct_setup_mci_misc_attrs(struct mem_ctl_info *mci)
  2793. {
  2794. struct amd64_pvt *pvt = mci->pvt_info;
  2795. mci->mtype_cap = MEM_FLAG_DDR2 | MEM_FLAG_RDDR2;
  2796. mci->edac_ctl_cap = EDAC_FLAG_NONE;
  2797. if (pvt->nbcap & NBCAP_SECDED)
  2798. mci->edac_ctl_cap |= EDAC_FLAG_SECDED;
  2799. if (pvt->nbcap & NBCAP_CHIPKILL)
  2800. mci->edac_ctl_cap |= EDAC_FLAG_S4ECD4ED;
  2801. mci->edac_cap = dct_determine_edac_cap(pvt);
  2802. mci->mod_name = EDAC_MOD_STR;
  2803. mci->ctl_name = pvt->ctl_name;
  2804. mci->dev_name = pci_name(pvt->F3);
  2805. mci->ctl_page_to_phys = NULL;
  2806. /* memory scrubber interface */
  2807. mci->set_sdram_scrub_rate = set_scrub_rate;
  2808. mci->get_sdram_scrub_rate = get_scrub_rate;
  2809. dct_init_csrows(mci);
  2810. }
  2811. static void umc_setup_mci_misc_attrs(struct mem_ctl_info *mci)
  2812. {
  2813. struct amd64_pvt *pvt = mci->pvt_info;
  2814. mci->mtype_cap = MEM_FLAG_DDR4 | MEM_FLAG_RDDR4;
  2815. mci->edac_ctl_cap = EDAC_FLAG_NONE;
  2816. umc_determine_edac_ctl_cap(mci, pvt);
  2817. mci->edac_cap = umc_determine_edac_cap(pvt);
  2818. mci->mod_name = EDAC_MOD_STR;
  2819. mci->ctl_name = pvt->ctl_name;
  2820. mci->dev_name = pci_name(pvt->F3);
  2821. mci->ctl_page_to_phys = NULL;
  2822. umc_init_csrows(mci);
  2823. }
  2824. static int dct_hw_info_get(struct amd64_pvt *pvt)
  2825. {
  2826. int ret = reserve_mc_sibling_devs(pvt, pvt->f1_id, pvt->f2_id);
  2827. if (ret)
  2828. return ret;
  2829. dct_prep_chip_selects(pvt);
  2830. dct_read_base_mask(pvt);
  2831. dct_read_mc_regs(pvt);
  2832. dct_determine_memory_type(pvt);
  2833. return 0;
  2834. }
  2835. static int umc_hw_info_get(struct amd64_pvt *pvt)
  2836. {
  2837. pvt->umc = kcalloc(pvt->max_mcs, sizeof(struct amd64_umc), GFP_KERNEL);
  2838. if (!pvt->umc)
  2839. return -ENOMEM;
  2840. umc_prep_chip_selects(pvt);
  2841. umc_read_base_mask(pvt);
  2842. umc_read_mc_regs(pvt);
  2843. umc_determine_memory_type(pvt);
  2844. return 0;
  2845. }
  2846. /*
  2847. * The CPUs have one channel per UMC, so UMC number is equivalent to a
  2848. * channel number. The GPUs have 8 channels per UMC, so the UMC number no
  2849. * longer works as a channel number.
  2850. *
  2851. * The channel number within a GPU UMC is given in MCA_IPID[15:12].
  2852. * However, the IDs are split such that two UMC values go to one UMC, and
  2853. * the channel numbers are split in two groups of four.
  2854. *
  2855. * Refer to comment on gpu_get_umc_base().
  2856. *
  2857. * For example,
  2858. * UMC0 CH[3:0] = 0x0005[3:0]000
  2859. * UMC0 CH[7:4] = 0x0015[3:0]000
  2860. * UMC1 CH[3:0] = 0x0025[3:0]000
  2861. * UMC1 CH[7:4] = 0x0035[3:0]000
  2862. */
  2863. static void gpu_get_err_info(struct mce *m, struct err_info *err)
  2864. {
  2865. u8 ch = (m->ipid & GENMASK(31, 0)) >> 20;
  2866. u8 phy = ((m->ipid >> 12) & 0xf);
  2867. err->channel = ch % 2 ? phy + 4 : phy;
  2868. err->csrow = phy;
  2869. }
  2870. static int gpu_addr_mask_to_cs_size(struct amd64_pvt *pvt, u8 umc,
  2871. unsigned int cs_mode, int csrow_nr)
  2872. {
  2873. u32 addr_mask_orig = pvt->csels[umc].csmasks[csrow_nr];
  2874. return __addr_mask_to_cs_size(addr_mask_orig, cs_mode, csrow_nr, csrow_nr >> 1);
  2875. }
  2876. static void gpu_debug_display_dimm_sizes(struct amd64_pvt *pvt, u8 ctrl)
  2877. {
  2878. int size, cs_mode, cs = 0;
  2879. edac_printk(KERN_DEBUG, EDAC_MC, "UMC%d chip selects:\n", ctrl);
  2880. cs_mode = CS_EVEN_PRIMARY | CS_ODD_PRIMARY;
  2881. for_each_chip_select(cs, ctrl, pvt) {
  2882. size = gpu_addr_mask_to_cs_size(pvt, ctrl, cs_mode, cs);
  2883. amd64_info(EDAC_MC ": %d: %5dMB\n", cs, size);
  2884. }
  2885. }
  2886. static void gpu_dump_misc_regs(struct amd64_pvt *pvt)
  2887. {
  2888. struct amd64_umc *umc;
  2889. u32 i;
  2890. for_each_umc(i) {
  2891. umc = &pvt->umc[i];
  2892. edac_dbg(1, "UMC%d UMC cfg: 0x%x\n", i, umc->umc_cfg);
  2893. edac_dbg(1, "UMC%d SDP ctrl: 0x%x\n", i, umc->sdp_ctrl);
  2894. edac_dbg(1, "UMC%d ECC ctrl: 0x%x\n", i, umc->ecc_ctrl);
  2895. edac_dbg(1, "UMC%d All HBMs support ECC: yes\n", i);
  2896. gpu_debug_display_dimm_sizes(pvt, i);
  2897. }
  2898. }
  2899. static u32 gpu_get_csrow_nr_pages(struct amd64_pvt *pvt, u8 dct, int csrow_nr)
  2900. {
  2901. u32 nr_pages;
  2902. int cs_mode = CS_EVEN_PRIMARY | CS_ODD_PRIMARY;
  2903. nr_pages = gpu_addr_mask_to_cs_size(pvt, dct, cs_mode, csrow_nr);
  2904. nr_pages <<= 20 - PAGE_SHIFT;
  2905. edac_dbg(0, "csrow: %d, channel: %d\n", csrow_nr, dct);
  2906. edac_dbg(0, "nr_pages/channel: %u\n", nr_pages);
  2907. return nr_pages;
  2908. }
  2909. static void gpu_init_csrows(struct mem_ctl_info *mci)
  2910. {
  2911. struct amd64_pvt *pvt = mci->pvt_info;
  2912. struct dimm_info *dimm;
  2913. u8 umc, cs;
  2914. for_each_umc(umc) {
  2915. for_each_chip_select(cs, umc, pvt) {
  2916. if (!csrow_enabled(cs, umc, pvt))
  2917. continue;
  2918. dimm = mci->csrows[umc]->channels[cs]->dimm;
  2919. edac_dbg(1, "MC node: %d, csrow: %d\n",
  2920. pvt->mc_node_id, cs);
  2921. dimm->nr_pages = gpu_get_csrow_nr_pages(pvt, umc, cs);
  2922. dimm->edac_mode = EDAC_SECDED;
  2923. dimm->mtype = pvt->dram_type;
  2924. dimm->dtype = DEV_X16;
  2925. dimm->grain = 64;
  2926. }
  2927. }
  2928. }
  2929. static void gpu_setup_mci_misc_attrs(struct mem_ctl_info *mci)
  2930. {
  2931. struct amd64_pvt *pvt = mci->pvt_info;
  2932. mci->mtype_cap = MEM_FLAG_HBM2;
  2933. mci->edac_ctl_cap = EDAC_FLAG_SECDED;
  2934. mci->edac_cap = EDAC_FLAG_EC;
  2935. mci->mod_name = EDAC_MOD_STR;
  2936. mci->ctl_name = pvt->ctl_name;
  2937. mci->dev_name = pci_name(pvt->F3);
  2938. mci->ctl_page_to_phys = NULL;
  2939. gpu_init_csrows(mci);
  2940. }
  2941. /* ECC is enabled by default on GPU nodes */
  2942. static bool gpu_ecc_enabled(struct amd64_pvt *pvt)
  2943. {
  2944. return true;
  2945. }
  2946. static inline u32 gpu_get_umc_base(struct amd64_pvt *pvt, u8 umc, u8 channel)
  2947. {
  2948. /*
  2949. * On CPUs, there is one channel per UMC, so UMC numbering equals
  2950. * channel numbering. On GPUs, there are eight channels per UMC,
  2951. * so the channel numbering is different from UMC numbering.
  2952. *
  2953. * On CPU nodes channels are selected in 6th nibble
  2954. * UMC chY[3:0]= [(chY*2 + 1) : (chY*2)]50000;
  2955. *
  2956. * On GPU nodes channels are selected in 3rd nibble
  2957. * HBM chX[3:0]= [Y ]5X[3:0]000;
  2958. * HBM chX[7:4]= [Y+1]5X[3:0]000
  2959. *
  2960. * On MI300 APU nodes, same as GPU nodes but channels are selected
  2961. * in the base address of 0x90000
  2962. */
  2963. umc *= 2;
  2964. if (channel >= 4)
  2965. umc++;
  2966. return pvt->gpu_umc_base + (umc << 20) + ((channel % 4) << 12);
  2967. }
  2968. static void gpu_read_mc_regs(struct amd64_pvt *pvt)
  2969. {
  2970. u8 nid = pvt->mc_node_id;
  2971. struct amd64_umc *umc;
  2972. u32 i, tmp, umc_base;
  2973. /* Read registers from each UMC */
  2974. for_each_umc(i) {
  2975. umc_base = gpu_get_umc_base(pvt, i, 0);
  2976. umc = &pvt->umc[i];
  2977. if (!amd_smn_read(nid, umc_base + UMCCH_UMC_CFG, &tmp))
  2978. umc->umc_cfg = tmp;
  2979. if (!amd_smn_read(nid, umc_base + UMCCH_SDP_CTRL, &tmp))
  2980. umc->sdp_ctrl = tmp;
  2981. if (!amd_smn_read(nid, umc_base + UMCCH_ECC_CTRL, &tmp))
  2982. umc->ecc_ctrl = tmp;
  2983. }
  2984. }
  2985. static void gpu_read_base_mask(struct amd64_pvt *pvt)
  2986. {
  2987. u32 base_reg, mask_reg;
  2988. u32 *base, *mask;
  2989. int umc, cs;
  2990. for_each_umc(umc) {
  2991. for_each_chip_select(cs, umc, pvt) {
  2992. base_reg = gpu_get_umc_base(pvt, umc, cs) + UMCCH_BASE_ADDR;
  2993. base = &pvt->csels[umc].csbases[cs];
  2994. if (!amd_smn_read(pvt->mc_node_id, base_reg, base)) {
  2995. edac_dbg(0, " DCSB%d[%d]=0x%08x reg: 0x%x\n",
  2996. umc, cs, *base, base_reg);
  2997. }
  2998. mask_reg = gpu_get_umc_base(pvt, umc, cs) + UMCCH_ADDR_MASK;
  2999. mask = &pvt->csels[umc].csmasks[cs];
  3000. if (!amd_smn_read(pvt->mc_node_id, mask_reg, mask)) {
  3001. edac_dbg(0, " DCSM%d[%d]=0x%08x reg: 0x%x\n",
  3002. umc, cs, *mask, mask_reg);
  3003. }
  3004. }
  3005. }
  3006. }
  3007. static void gpu_prep_chip_selects(struct amd64_pvt *pvt)
  3008. {
  3009. int umc;
  3010. for_each_umc(umc) {
  3011. pvt->csels[umc].b_cnt = 8;
  3012. pvt->csels[umc].m_cnt = 8;
  3013. }
  3014. }
  3015. static int gpu_hw_info_get(struct amd64_pvt *pvt)
  3016. {
  3017. int ret;
  3018. ret = gpu_get_node_map(pvt);
  3019. if (ret)
  3020. return ret;
  3021. pvt->umc = kcalloc(pvt->max_mcs, sizeof(struct amd64_umc), GFP_KERNEL);
  3022. if (!pvt->umc)
  3023. return -ENOMEM;
  3024. gpu_prep_chip_selects(pvt);
  3025. gpu_read_base_mask(pvt);
  3026. gpu_read_mc_regs(pvt);
  3027. return 0;
  3028. }
  3029. static void hw_info_put(struct amd64_pvt *pvt)
  3030. {
  3031. pci_dev_put(pvt->F1);
  3032. pci_dev_put(pvt->F2);
  3033. kfree(pvt->umc);
  3034. }
  3035. static struct low_ops umc_ops = {
  3036. .hw_info_get = umc_hw_info_get,
  3037. .ecc_enabled = umc_ecc_enabled,
  3038. .setup_mci_misc_attrs = umc_setup_mci_misc_attrs,
  3039. .dump_misc_regs = umc_dump_misc_regs,
  3040. .get_err_info = umc_get_err_info,
  3041. };
  3042. static struct low_ops gpu_ops = {
  3043. .hw_info_get = gpu_hw_info_get,
  3044. .ecc_enabled = gpu_ecc_enabled,
  3045. .setup_mci_misc_attrs = gpu_setup_mci_misc_attrs,
  3046. .dump_misc_regs = gpu_dump_misc_regs,
  3047. .get_err_info = gpu_get_err_info,
  3048. };
  3049. /* Use Family 16h versions for defaults and adjust as needed below. */
  3050. static struct low_ops dct_ops = {
  3051. .map_sysaddr_to_csrow = f1x_map_sysaddr_to_csrow,
  3052. .dbam_to_cs = f16_dbam_to_chip_select,
  3053. .hw_info_get = dct_hw_info_get,
  3054. .ecc_enabled = dct_ecc_enabled,
  3055. .setup_mci_misc_attrs = dct_setup_mci_misc_attrs,
  3056. .dump_misc_regs = dct_dump_misc_regs,
  3057. };
  3058. static int per_family_init(struct amd64_pvt *pvt)
  3059. {
  3060. pvt->ext_model = boot_cpu_data.x86_model >> 4;
  3061. pvt->stepping = boot_cpu_data.x86_stepping;
  3062. pvt->model = boot_cpu_data.x86_model;
  3063. pvt->fam = boot_cpu_data.x86;
  3064. pvt->max_mcs = 2;
  3065. /*
  3066. * Decide on which ops group to use here and do any family/model
  3067. * overrides below.
  3068. */
  3069. if (pvt->fam >= 0x17)
  3070. pvt->ops = &umc_ops;
  3071. else
  3072. pvt->ops = &dct_ops;
  3073. switch (pvt->fam) {
  3074. case 0xf:
  3075. pvt->ctl_name = (pvt->ext_model >= K8_REV_F) ?
  3076. "K8 revF or later" : "K8 revE or earlier";
  3077. pvt->f1_id = PCI_DEVICE_ID_AMD_K8_NB_ADDRMAP;
  3078. pvt->f2_id = PCI_DEVICE_ID_AMD_K8_NB_MEMCTL;
  3079. pvt->ops->map_sysaddr_to_csrow = k8_map_sysaddr_to_csrow;
  3080. pvt->ops->dbam_to_cs = k8_dbam_to_chip_select;
  3081. break;
  3082. case 0x10:
  3083. pvt->ctl_name = "F10h";
  3084. pvt->f1_id = PCI_DEVICE_ID_AMD_10H_NB_MAP;
  3085. pvt->f2_id = PCI_DEVICE_ID_AMD_10H_NB_DRAM;
  3086. pvt->ops->dbam_to_cs = f10_dbam_to_chip_select;
  3087. break;
  3088. case 0x15:
  3089. switch (pvt->model) {
  3090. case 0x30:
  3091. pvt->ctl_name = "F15h_M30h";
  3092. pvt->f1_id = PCI_DEVICE_ID_AMD_15H_M30H_NB_F1;
  3093. pvt->f2_id = PCI_DEVICE_ID_AMD_15H_M30H_NB_F2;
  3094. break;
  3095. case 0x60:
  3096. pvt->ctl_name = "F15h_M60h";
  3097. pvt->f1_id = PCI_DEVICE_ID_AMD_15H_M60H_NB_F1;
  3098. pvt->f2_id = PCI_DEVICE_ID_AMD_15H_M60H_NB_F2;
  3099. pvt->ops->dbam_to_cs = f15_m60h_dbam_to_chip_select;
  3100. break;
  3101. case 0x13:
  3102. /* Richland is only client */
  3103. return -ENODEV;
  3104. default:
  3105. pvt->ctl_name = "F15h";
  3106. pvt->f1_id = PCI_DEVICE_ID_AMD_15H_NB_F1;
  3107. pvt->f2_id = PCI_DEVICE_ID_AMD_15H_NB_F2;
  3108. pvt->ops->dbam_to_cs = f15_dbam_to_chip_select;
  3109. break;
  3110. }
  3111. break;
  3112. case 0x16:
  3113. switch (pvt->model) {
  3114. case 0x30:
  3115. pvt->ctl_name = "F16h_M30h";
  3116. pvt->f1_id = PCI_DEVICE_ID_AMD_16H_M30H_NB_F1;
  3117. pvt->f2_id = PCI_DEVICE_ID_AMD_16H_M30H_NB_F2;
  3118. break;
  3119. default:
  3120. pvt->ctl_name = "F16h";
  3121. pvt->f1_id = PCI_DEVICE_ID_AMD_16H_NB_F1;
  3122. pvt->f2_id = PCI_DEVICE_ID_AMD_16H_NB_F2;
  3123. break;
  3124. }
  3125. break;
  3126. case 0x17:
  3127. switch (pvt->model) {
  3128. case 0x10 ... 0x2f:
  3129. pvt->ctl_name = "F17h_M10h";
  3130. break;
  3131. case 0x30 ... 0x3f:
  3132. pvt->ctl_name = "F17h_M30h";
  3133. pvt->max_mcs = 8;
  3134. break;
  3135. case 0x60 ... 0x6f:
  3136. pvt->ctl_name = "F17h_M60h";
  3137. break;
  3138. case 0x70 ... 0x7f:
  3139. pvt->ctl_name = "F17h_M70h";
  3140. break;
  3141. default:
  3142. pvt->ctl_name = "F17h";
  3143. break;
  3144. }
  3145. break;
  3146. case 0x18:
  3147. pvt->ctl_name = "F18h";
  3148. break;
  3149. case 0x19:
  3150. switch (pvt->model) {
  3151. case 0x00 ... 0x0f:
  3152. pvt->ctl_name = "F19h";
  3153. pvt->max_mcs = 8;
  3154. break;
  3155. case 0x10 ... 0x1f:
  3156. pvt->ctl_name = "F19h_M10h";
  3157. pvt->max_mcs = 12;
  3158. pvt->flags.zn_regs_v2 = 1;
  3159. break;
  3160. case 0x20 ... 0x2f:
  3161. pvt->ctl_name = "F19h_M20h";
  3162. break;
  3163. case 0x30 ... 0x3f:
  3164. if (pvt->F3->device == PCI_DEVICE_ID_AMD_MI200_DF_F3) {
  3165. pvt->ctl_name = "MI200";
  3166. pvt->max_mcs = 4;
  3167. pvt->dram_type = MEM_HBM2;
  3168. pvt->gpu_umc_base = 0x50000;
  3169. pvt->ops = &gpu_ops;
  3170. } else {
  3171. pvt->ctl_name = "F19h_M30h";
  3172. pvt->max_mcs = 8;
  3173. }
  3174. break;
  3175. case 0x50 ... 0x5f:
  3176. pvt->ctl_name = "F19h_M50h";
  3177. break;
  3178. case 0x60 ... 0x6f:
  3179. pvt->ctl_name = "F19h_M60h";
  3180. pvt->flags.zn_regs_v2 = 1;
  3181. break;
  3182. case 0x70 ... 0x7f:
  3183. pvt->ctl_name = "F19h_M70h";
  3184. pvt->flags.zn_regs_v2 = 1;
  3185. break;
  3186. case 0x90 ... 0x9f:
  3187. pvt->ctl_name = "F19h_M90h";
  3188. pvt->max_mcs = 4;
  3189. pvt->dram_type = MEM_HBM3;
  3190. pvt->gpu_umc_base = 0x90000;
  3191. pvt->ops = &gpu_ops;
  3192. break;
  3193. case 0xa0 ... 0xaf:
  3194. pvt->ctl_name = "F19h_MA0h";
  3195. pvt->max_mcs = 12;
  3196. pvt->flags.zn_regs_v2 = 1;
  3197. break;
  3198. }
  3199. break;
  3200. case 0x1A:
  3201. switch (pvt->model) {
  3202. case 0x00 ... 0x1f:
  3203. pvt->ctl_name = "F1Ah";
  3204. pvt->max_mcs = 12;
  3205. pvt->flags.zn_regs_v2 = 1;
  3206. break;
  3207. case 0x40 ... 0x4f:
  3208. pvt->ctl_name = "F1Ah_M40h";
  3209. pvt->flags.zn_regs_v2 = 1;
  3210. break;
  3211. }
  3212. break;
  3213. default:
  3214. amd64_err("Unsupported family!\n");
  3215. return -ENODEV;
  3216. }
  3217. return 0;
  3218. }
  3219. static const struct attribute_group *amd64_edac_attr_groups[] = {
  3220. #ifdef CONFIG_EDAC_DEBUG
  3221. &dbg_group,
  3222. &inj_group,
  3223. #endif
  3224. NULL
  3225. };
  3226. /*
  3227. * For heterogeneous and APU models EDAC CHIP_SELECT and CHANNEL layers
  3228. * should be swapped to fit into the layers.
  3229. */
  3230. static unsigned int get_layer_size(struct amd64_pvt *pvt, u8 layer)
  3231. {
  3232. bool is_gpu = (pvt->ops == &gpu_ops);
  3233. if (!layer)
  3234. return is_gpu ? pvt->max_mcs
  3235. : pvt->csels[0].b_cnt;
  3236. else
  3237. return is_gpu ? pvt->csels[0].b_cnt
  3238. : pvt->max_mcs;
  3239. }
  3240. static int init_one_instance(struct amd64_pvt *pvt)
  3241. {
  3242. struct mem_ctl_info *mci = NULL;
  3243. struct edac_mc_layer layers[2];
  3244. int ret = -ENOMEM;
  3245. layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
  3246. layers[0].size = get_layer_size(pvt, 0);
  3247. layers[0].is_virt_csrow = true;
  3248. layers[1].type = EDAC_MC_LAYER_CHANNEL;
  3249. layers[1].size = get_layer_size(pvt, 1);
  3250. layers[1].is_virt_csrow = false;
  3251. mci = edac_mc_alloc(pvt->mc_node_id, ARRAY_SIZE(layers), layers, 0);
  3252. if (!mci)
  3253. return ret;
  3254. mci->pvt_info = pvt;
  3255. mci->pdev = &pvt->F3->dev;
  3256. pvt->ops->setup_mci_misc_attrs(mci);
  3257. ret = -ENODEV;
  3258. if (edac_mc_add_mc_with_groups(mci, amd64_edac_attr_groups)) {
  3259. edac_dbg(1, "failed edac_mc_add_mc()\n");
  3260. edac_mc_free(mci);
  3261. return ret;
  3262. }
  3263. return 0;
  3264. }
  3265. static bool instance_has_memory(struct amd64_pvt *pvt)
  3266. {
  3267. bool cs_enabled = false;
  3268. int cs = 0, dct = 0;
  3269. for (dct = 0; dct < pvt->max_mcs; dct++) {
  3270. for_each_chip_select(cs, dct, pvt)
  3271. cs_enabled |= csrow_enabled(cs, dct, pvt);
  3272. }
  3273. return cs_enabled;
  3274. }
  3275. static int probe_one_instance(unsigned int nid)
  3276. {
  3277. struct pci_dev *F3 = node_to_amd_nb(nid)->misc;
  3278. struct amd64_pvt *pvt = NULL;
  3279. struct ecc_settings *s;
  3280. int ret;
  3281. ret = -ENOMEM;
  3282. s = kzalloc(sizeof(struct ecc_settings), GFP_KERNEL);
  3283. if (!s)
  3284. goto err_out;
  3285. ecc_stngs[nid] = s;
  3286. pvt = kzalloc(sizeof(struct amd64_pvt), GFP_KERNEL);
  3287. if (!pvt)
  3288. goto err_settings;
  3289. pvt->mc_node_id = nid;
  3290. pvt->F3 = F3;
  3291. ret = per_family_init(pvt);
  3292. if (ret < 0)
  3293. goto err_enable;
  3294. ret = pvt->ops->hw_info_get(pvt);
  3295. if (ret < 0)
  3296. goto err_enable;
  3297. ret = 0;
  3298. if (!instance_has_memory(pvt)) {
  3299. amd64_info("Node %d: No DIMMs detected.\n", nid);
  3300. goto err_enable;
  3301. }
  3302. if (!pvt->ops->ecc_enabled(pvt)) {
  3303. ret = -ENODEV;
  3304. if (!ecc_enable_override)
  3305. goto err_enable;
  3306. if (boot_cpu_data.x86 >= 0x17) {
  3307. amd64_warn("Forcing ECC on is not recommended on newer systems. Please enable ECC in BIOS.");
  3308. goto err_enable;
  3309. } else
  3310. amd64_warn("Forcing ECC on!\n");
  3311. if (!enable_ecc_error_reporting(s, nid, F3))
  3312. goto err_enable;
  3313. }
  3314. ret = init_one_instance(pvt);
  3315. if (ret < 0) {
  3316. amd64_err("Error probing instance: %d\n", nid);
  3317. if (boot_cpu_data.x86 < 0x17)
  3318. restore_ecc_error_reporting(s, nid, F3);
  3319. goto err_enable;
  3320. }
  3321. amd64_info("%s detected (node %d).\n", pvt->ctl_name, pvt->mc_node_id);
  3322. /* Display and decode various registers for debug purposes. */
  3323. pvt->ops->dump_misc_regs(pvt);
  3324. return ret;
  3325. err_enable:
  3326. hw_info_put(pvt);
  3327. kfree(pvt);
  3328. err_settings:
  3329. kfree(s);
  3330. ecc_stngs[nid] = NULL;
  3331. err_out:
  3332. return ret;
  3333. }
  3334. static void remove_one_instance(unsigned int nid)
  3335. {
  3336. struct pci_dev *F3 = node_to_amd_nb(nid)->misc;
  3337. struct ecc_settings *s = ecc_stngs[nid];
  3338. struct mem_ctl_info *mci;
  3339. struct amd64_pvt *pvt;
  3340. /* Remove from EDAC CORE tracking list */
  3341. mci = edac_mc_del_mc(&F3->dev);
  3342. if (!mci)
  3343. return;
  3344. pvt = mci->pvt_info;
  3345. restore_ecc_error_reporting(s, nid, F3);
  3346. kfree(ecc_stngs[nid]);
  3347. ecc_stngs[nid] = NULL;
  3348. /* Free the EDAC CORE resources */
  3349. mci->pvt_info = NULL;
  3350. hw_info_put(pvt);
  3351. kfree(pvt);
  3352. edac_mc_free(mci);
  3353. }
  3354. static void setup_pci_device(void)
  3355. {
  3356. if (pci_ctl)
  3357. return;
  3358. pci_ctl = edac_pci_create_generic_ctl(pci_ctl_dev, EDAC_MOD_STR);
  3359. if (!pci_ctl) {
  3360. pr_warn("%s(): Unable to create PCI control\n", __func__);
  3361. pr_warn("%s(): PCI error report via EDAC not set\n", __func__);
  3362. }
  3363. }
  3364. static const struct x86_cpu_id amd64_cpuids[] = {
  3365. X86_MATCH_VENDOR_FAM(AMD, 0x0F, NULL),
  3366. X86_MATCH_VENDOR_FAM(AMD, 0x10, NULL),
  3367. X86_MATCH_VENDOR_FAM(AMD, 0x15, NULL),
  3368. X86_MATCH_VENDOR_FAM(AMD, 0x16, NULL),
  3369. X86_MATCH_VENDOR_FAM(AMD, 0x17, NULL),
  3370. X86_MATCH_VENDOR_FAM(HYGON, 0x18, NULL),
  3371. X86_MATCH_VENDOR_FAM(AMD, 0x19, NULL),
  3372. X86_MATCH_VENDOR_FAM(AMD, 0x1A, NULL),
  3373. { }
  3374. };
  3375. MODULE_DEVICE_TABLE(x86cpu, amd64_cpuids);
  3376. static int __init amd64_edac_init(void)
  3377. {
  3378. const char *owner;
  3379. int err = -ENODEV;
  3380. int i;
  3381. if (ghes_get_devices())
  3382. return -EBUSY;
  3383. owner = edac_get_owner();
  3384. if (owner && strncmp(owner, EDAC_MOD_STR, sizeof(EDAC_MOD_STR)))
  3385. return -EBUSY;
  3386. if (!x86_match_cpu(amd64_cpuids))
  3387. return -ENODEV;
  3388. if (!amd_nb_num())
  3389. return -ENODEV;
  3390. opstate_init();
  3391. err = -ENOMEM;
  3392. ecc_stngs = kcalloc(amd_nb_num(), sizeof(ecc_stngs[0]), GFP_KERNEL);
  3393. if (!ecc_stngs)
  3394. goto err_free;
  3395. msrs = msrs_alloc();
  3396. if (!msrs)
  3397. goto err_free;
  3398. for (i = 0; i < amd_nb_num(); i++) {
  3399. err = probe_one_instance(i);
  3400. if (err) {
  3401. /* unwind properly */
  3402. while (--i >= 0)
  3403. remove_one_instance(i);
  3404. goto err_pci;
  3405. }
  3406. }
  3407. if (!edac_has_mcs()) {
  3408. err = -ENODEV;
  3409. goto err_pci;
  3410. }
  3411. /* register stuff with EDAC MCE */
  3412. if (boot_cpu_data.x86 >= 0x17) {
  3413. amd_register_ecc_decoder(decode_umc_error);
  3414. } else {
  3415. amd_register_ecc_decoder(decode_bus_error);
  3416. setup_pci_device();
  3417. }
  3418. #ifdef CONFIG_X86_32
  3419. amd64_err("%s on 32-bit is unsupported. USE AT YOUR OWN RISK!\n", EDAC_MOD_STR);
  3420. #endif
  3421. return 0;
  3422. err_pci:
  3423. pci_ctl_dev = NULL;
  3424. msrs_free(msrs);
  3425. msrs = NULL;
  3426. err_free:
  3427. kfree(ecc_stngs);
  3428. ecc_stngs = NULL;
  3429. return err;
  3430. }
  3431. static void __exit amd64_edac_exit(void)
  3432. {
  3433. int i;
  3434. if (pci_ctl)
  3435. edac_pci_release_generic_ctl(pci_ctl);
  3436. /* unregister from EDAC MCE */
  3437. if (boot_cpu_data.x86 >= 0x17)
  3438. amd_unregister_ecc_decoder(decode_umc_error);
  3439. else
  3440. amd_unregister_ecc_decoder(decode_bus_error);
  3441. for (i = 0; i < amd_nb_num(); i++)
  3442. remove_one_instance(i);
  3443. kfree(ecc_stngs);
  3444. ecc_stngs = NULL;
  3445. pci_ctl_dev = NULL;
  3446. msrs_free(msrs);
  3447. msrs = NULL;
  3448. }
  3449. module_init(amd64_edac_init);
  3450. module_exit(amd64_edac_exit);
  3451. MODULE_LICENSE("GPL");
  3452. MODULE_AUTHOR("SoftwareBitMaker: Doug Thompson, Dave Peterson, Thayne Harbaugh; AMD");
  3453. MODULE_DESCRIPTION("MC support for AMD64 memory controllers");
  3454. module_param(edac_op_state, int, 0444);
  3455. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");