amd64_edac.c 78 KB

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