amd64_edac.c 77 KB

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