amd64_edac.c 89 KB

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