i7core_edac.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466
  1. /* Intel i7 core/Nehalem Memory Controller kernel module
  2. *
  3. * This driver supports the memory controllers found on the Intel
  4. * processor families i7core, i7core 7xx/8xx, i5core, Xeon 35xx,
  5. * Xeon 55xx and Xeon 56xx also known as Nehalem, Nehalem-EP, Lynnfield
  6. * and Westmere-EP.
  7. *
  8. * This file may be distributed under the terms of the
  9. * GNU General Public License version 2 only.
  10. *
  11. * Copyright (c) 2009-2010 by:
  12. * Mauro Carvalho Chehab
  13. *
  14. * Red Hat Inc. http://www.redhat.com
  15. *
  16. * Forked and adapted from the i5400_edac driver
  17. *
  18. * Based on the following public Intel datasheets:
  19. * Intel Core i7 Processor Extreme Edition and Intel Core i7 Processor
  20. * Datasheet, Volume 2:
  21. * http://download.intel.com/design/processor/datashts/320835.pdf
  22. * Intel Xeon Processor 5500 Series Datasheet Volume 2
  23. * http://www.intel.com/Assets/PDF/datasheet/321322.pdf
  24. * also available at:
  25. * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
  26. */
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/pci.h>
  30. #include <linux/pci_ids.h>
  31. #include <linux/slab.h>
  32. #include <linux/delay.h>
  33. #include <linux/dmi.h>
  34. #include <linux/edac.h>
  35. #include <linux/mmzone.h>
  36. #include <linux/smp.h>
  37. #include <asm/mce.h>
  38. #include <asm/processor.h>
  39. #include <asm/div64.h>
  40. #include "edac_core.h"
  41. /* Static vars */
  42. static LIST_HEAD(i7core_edac_list);
  43. static DEFINE_MUTEX(i7core_edac_lock);
  44. static int probed;
  45. static int use_pci_fixup;
  46. module_param(use_pci_fixup, int, 0444);
  47. MODULE_PARM_DESC(use_pci_fixup, "Enable PCI fixup to seek for hidden devices");
  48. /*
  49. * This is used for Nehalem-EP and Nehalem-EX devices, where the non-core
  50. * registers start at bus 255, and are not reported by BIOS.
  51. * We currently find devices with only 2 sockets. In order to support more QPI
  52. * Quick Path Interconnect, just increment this number.
  53. */
  54. #define MAX_SOCKET_BUSES 2
  55. /*
  56. * Alter this version for the module when modifications are made
  57. */
  58. #define I7CORE_REVISION " Ver: 1.0.0"
  59. #define EDAC_MOD_STR "i7core_edac"
  60. /*
  61. * Debug macros
  62. */
  63. #define i7core_printk(level, fmt, arg...) \
  64. edac_printk(level, "i7core", fmt, ##arg)
  65. #define i7core_mc_printk(mci, level, fmt, arg...) \
  66. edac_mc_chipset_printk(mci, level, "i7core", fmt, ##arg)
  67. /*
  68. * i7core Memory Controller Registers
  69. */
  70. /* OFFSETS for Device 0 Function 0 */
  71. #define MC_CFG_CONTROL 0x90
  72. #define MC_CFG_UNLOCK 0x02
  73. #define MC_CFG_LOCK 0x00
  74. /* OFFSETS for Device 3 Function 0 */
  75. #define MC_CONTROL 0x48
  76. #define MC_STATUS 0x4c
  77. #define MC_MAX_DOD 0x64
  78. /*
  79. * OFFSETS for Device 3 Function 4, as indicated on Xeon 5500 datasheet:
  80. * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
  81. */
  82. #define MC_TEST_ERR_RCV1 0x60
  83. #define DIMM2_COR_ERR(r) ((r) & 0x7fff)
  84. #define MC_TEST_ERR_RCV0 0x64
  85. #define DIMM1_COR_ERR(r) (((r) >> 16) & 0x7fff)
  86. #define DIMM0_COR_ERR(r) ((r) & 0x7fff)
  87. /* OFFSETS for Device 3 Function 2, as indicated on Xeon 5500 datasheet */
  88. #define MC_SSRCONTROL 0x48
  89. #define SSR_MODE_DISABLE 0x00
  90. #define SSR_MODE_ENABLE 0x01
  91. #define SSR_MODE_MASK 0x03
  92. #define MC_SCRUB_CONTROL 0x4c
  93. #define STARTSCRUB (1 << 24)
  94. #define SCRUBINTERVAL_MASK 0xffffff
  95. #define MC_COR_ECC_CNT_0 0x80
  96. #define MC_COR_ECC_CNT_1 0x84
  97. #define MC_COR_ECC_CNT_2 0x88
  98. #define MC_COR_ECC_CNT_3 0x8c
  99. #define MC_COR_ECC_CNT_4 0x90
  100. #define MC_COR_ECC_CNT_5 0x94
  101. #define DIMM_TOP_COR_ERR(r) (((r) >> 16) & 0x7fff)
  102. #define DIMM_BOT_COR_ERR(r) ((r) & 0x7fff)
  103. /* OFFSETS for Devices 4,5 and 6 Function 0 */
  104. #define MC_CHANNEL_DIMM_INIT_PARAMS 0x58
  105. #define THREE_DIMMS_PRESENT (1 << 24)
  106. #define SINGLE_QUAD_RANK_PRESENT (1 << 23)
  107. #define QUAD_RANK_PRESENT (1 << 22)
  108. #define REGISTERED_DIMM (1 << 15)
  109. #define MC_CHANNEL_MAPPER 0x60
  110. #define RDLCH(r, ch) ((((r) >> (3 + (ch * 6))) & 0x07) - 1)
  111. #define WRLCH(r, ch) ((((r) >> (ch * 6)) & 0x07) - 1)
  112. #define MC_CHANNEL_RANK_PRESENT 0x7c
  113. #define RANK_PRESENT_MASK 0xffff
  114. #define MC_CHANNEL_ADDR_MATCH 0xf0
  115. #define MC_CHANNEL_ERROR_MASK 0xf8
  116. #define MC_CHANNEL_ERROR_INJECT 0xfc
  117. #define INJECT_ADDR_PARITY 0x10
  118. #define INJECT_ECC 0x08
  119. #define MASK_CACHELINE 0x06
  120. #define MASK_FULL_CACHELINE 0x06
  121. #define MASK_MSB32_CACHELINE 0x04
  122. #define MASK_LSB32_CACHELINE 0x02
  123. #define NO_MASK_CACHELINE 0x00
  124. #define REPEAT_EN 0x01
  125. /* OFFSETS for Devices 4,5 and 6 Function 1 */
  126. #define MC_DOD_CH_DIMM0 0x48
  127. #define MC_DOD_CH_DIMM1 0x4c
  128. #define MC_DOD_CH_DIMM2 0x50
  129. #define RANKOFFSET_MASK ((1 << 12) | (1 << 11) | (1 << 10))
  130. #define RANKOFFSET(x) ((x & RANKOFFSET_MASK) >> 10)
  131. #define DIMM_PRESENT_MASK (1 << 9)
  132. #define DIMM_PRESENT(x) (((x) & DIMM_PRESENT_MASK) >> 9)
  133. #define MC_DOD_NUMBANK_MASK ((1 << 8) | (1 << 7))
  134. #define MC_DOD_NUMBANK(x) (((x) & MC_DOD_NUMBANK_MASK) >> 7)
  135. #define MC_DOD_NUMRANK_MASK ((1 << 6) | (1 << 5))
  136. #define MC_DOD_NUMRANK(x) (((x) & MC_DOD_NUMRANK_MASK) >> 5)
  137. #define MC_DOD_NUMROW_MASK ((1 << 4) | (1 << 3) | (1 << 2))
  138. #define MC_DOD_NUMROW(x) (((x) & MC_DOD_NUMROW_MASK) >> 2)
  139. #define MC_DOD_NUMCOL_MASK 3
  140. #define MC_DOD_NUMCOL(x) ((x) & MC_DOD_NUMCOL_MASK)
  141. #define MC_RANK_PRESENT 0x7c
  142. #define MC_SAG_CH_0 0x80
  143. #define MC_SAG_CH_1 0x84
  144. #define MC_SAG_CH_2 0x88
  145. #define MC_SAG_CH_3 0x8c
  146. #define MC_SAG_CH_4 0x90
  147. #define MC_SAG_CH_5 0x94
  148. #define MC_SAG_CH_6 0x98
  149. #define MC_SAG_CH_7 0x9c
  150. #define MC_RIR_LIMIT_CH_0 0x40
  151. #define MC_RIR_LIMIT_CH_1 0x44
  152. #define MC_RIR_LIMIT_CH_2 0x48
  153. #define MC_RIR_LIMIT_CH_3 0x4C
  154. #define MC_RIR_LIMIT_CH_4 0x50
  155. #define MC_RIR_LIMIT_CH_5 0x54
  156. #define MC_RIR_LIMIT_CH_6 0x58
  157. #define MC_RIR_LIMIT_CH_7 0x5C
  158. #define MC_RIR_LIMIT_MASK ((1 << 10) - 1)
  159. #define MC_RIR_WAY_CH 0x80
  160. #define MC_RIR_WAY_OFFSET_MASK (((1 << 14) - 1) & ~0x7)
  161. #define MC_RIR_WAY_RANK_MASK 0x7
  162. /*
  163. * i7core structs
  164. */
  165. #define NUM_CHANS 3
  166. #define MAX_DIMMS 3 /* Max DIMMS per channel */
  167. #define MAX_MCR_FUNC 4
  168. #define MAX_CHAN_FUNC 3
  169. struct i7core_info {
  170. u32 mc_control;
  171. u32 mc_status;
  172. u32 max_dod;
  173. u32 ch_map;
  174. };
  175. struct i7core_inject {
  176. int enable;
  177. u32 section;
  178. u32 type;
  179. u32 eccmask;
  180. /* Error address mask */
  181. int channel, dimm, rank, bank, page, col;
  182. };
  183. struct i7core_channel {
  184. bool is_3dimms_present;
  185. bool is_single_4rank;
  186. bool has_4rank;
  187. u32 dimms;
  188. };
  189. struct pci_id_descr {
  190. int dev;
  191. int func;
  192. int dev_id;
  193. int optional;
  194. };
  195. struct pci_id_table {
  196. const struct pci_id_descr *descr;
  197. int n_devs;
  198. };
  199. struct i7core_dev {
  200. struct list_head list;
  201. u8 socket;
  202. struct pci_dev **pdev;
  203. int n_devs;
  204. struct mem_ctl_info *mci;
  205. };
  206. struct i7core_pvt {
  207. struct device *addrmatch_dev, *chancounts_dev;
  208. struct pci_dev *pci_noncore;
  209. struct pci_dev *pci_mcr[MAX_MCR_FUNC + 1];
  210. struct pci_dev *pci_ch[NUM_CHANS][MAX_CHAN_FUNC + 1];
  211. struct i7core_dev *i7core_dev;
  212. struct i7core_info info;
  213. struct i7core_inject inject;
  214. struct i7core_channel channel[NUM_CHANS];
  215. int ce_count_available;
  216. /* ECC corrected errors counts per udimm */
  217. unsigned long udimm_ce_count[MAX_DIMMS];
  218. int udimm_last_ce_count[MAX_DIMMS];
  219. /* ECC corrected errors counts per rdimm */
  220. unsigned long rdimm_ce_count[NUM_CHANS][MAX_DIMMS];
  221. int rdimm_last_ce_count[NUM_CHANS][MAX_DIMMS];
  222. bool is_registered, enable_scrub;
  223. /* Fifo double buffers */
  224. struct mce mce_entry[MCE_LOG_LEN];
  225. struct mce mce_outentry[MCE_LOG_LEN];
  226. /* Fifo in/out counters */
  227. unsigned mce_in, mce_out;
  228. /* Count indicator to show errors not got */
  229. unsigned mce_overrun;
  230. /* DCLK Frequency used for computing scrub rate */
  231. int dclk_freq;
  232. /* Struct to control EDAC polling */
  233. struct edac_pci_ctl_info *i7core_pci;
  234. };
  235. #define PCI_DESCR(device, function, device_id) \
  236. .dev = (device), \
  237. .func = (function), \
  238. .dev_id = (device_id)
  239. static const struct pci_id_descr pci_dev_descr_i7core_nehalem[] = {
  240. /* Memory controller */
  241. { PCI_DESCR(3, 0, PCI_DEVICE_ID_INTEL_I7_MCR) },
  242. { PCI_DESCR(3, 1, PCI_DEVICE_ID_INTEL_I7_MC_TAD) },
  243. /* Exists only for RDIMM */
  244. { PCI_DESCR(3, 2, PCI_DEVICE_ID_INTEL_I7_MC_RAS), .optional = 1 },
  245. { PCI_DESCR(3, 4, PCI_DEVICE_ID_INTEL_I7_MC_TEST) },
  246. /* Channel 0 */
  247. { PCI_DESCR(4, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH0_CTRL) },
  248. { PCI_DESCR(4, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH0_ADDR) },
  249. { PCI_DESCR(4, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH0_RANK) },
  250. { PCI_DESCR(4, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH0_TC) },
  251. /* Channel 1 */
  252. { PCI_DESCR(5, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH1_CTRL) },
  253. { PCI_DESCR(5, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH1_ADDR) },
  254. { PCI_DESCR(5, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH1_RANK) },
  255. { PCI_DESCR(5, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH1_TC) },
  256. /* Channel 2 */
  257. { PCI_DESCR(6, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH2_CTRL) },
  258. { PCI_DESCR(6, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH2_ADDR) },
  259. { PCI_DESCR(6, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH2_RANK) },
  260. { PCI_DESCR(6, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH2_TC) },
  261. /* Generic Non-core registers */
  262. /*
  263. * This is the PCI device on i7core and on Xeon 35xx (8086:2c41)
  264. * On Xeon 55xx, however, it has a different id (8086:2c40). So,
  265. * the probing code needs to test for the other address in case of
  266. * failure of this one
  267. */
  268. { PCI_DESCR(0, 0, PCI_DEVICE_ID_INTEL_I7_NONCORE) },
  269. };
  270. static const struct pci_id_descr pci_dev_descr_lynnfield[] = {
  271. { PCI_DESCR( 3, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MCR) },
  272. { PCI_DESCR( 3, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TAD) },
  273. { PCI_DESCR( 3, 4, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TEST) },
  274. { PCI_DESCR( 4, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_CTRL) },
  275. { PCI_DESCR( 4, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_ADDR) },
  276. { PCI_DESCR( 4, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_RANK) },
  277. { PCI_DESCR( 4, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_TC) },
  278. { PCI_DESCR( 5, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_CTRL) },
  279. { PCI_DESCR( 5, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_ADDR) },
  280. { PCI_DESCR( 5, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_RANK) },
  281. { PCI_DESCR( 5, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_TC) },
  282. /*
  283. * This is the PCI device has an alternate address on some
  284. * processors like Core i7 860
  285. */
  286. { PCI_DESCR( 0, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE) },
  287. };
  288. static const struct pci_id_descr pci_dev_descr_i7core_westmere[] = {
  289. /* Memory controller */
  290. { PCI_DESCR(3, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MCR_REV2) },
  291. { PCI_DESCR(3, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TAD_REV2) },
  292. /* Exists only for RDIMM */
  293. { PCI_DESCR(3, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_RAS_REV2), .optional = 1 },
  294. { PCI_DESCR(3, 4, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TEST_REV2) },
  295. /* Channel 0 */
  296. { PCI_DESCR(4, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_CTRL_REV2) },
  297. { PCI_DESCR(4, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_ADDR_REV2) },
  298. { PCI_DESCR(4, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_RANK_REV2) },
  299. { PCI_DESCR(4, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_TC_REV2) },
  300. /* Channel 1 */
  301. { PCI_DESCR(5, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_CTRL_REV2) },
  302. { PCI_DESCR(5, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_ADDR_REV2) },
  303. { PCI_DESCR(5, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_RANK_REV2) },
  304. { PCI_DESCR(5, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_TC_REV2) },
  305. /* Channel 2 */
  306. { PCI_DESCR(6, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_CTRL_REV2) },
  307. { PCI_DESCR(6, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_ADDR_REV2) },
  308. { PCI_DESCR(6, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_RANK_REV2) },
  309. { PCI_DESCR(6, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_TC_REV2) },
  310. /* Generic Non-core registers */
  311. { PCI_DESCR(0, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE_REV2) },
  312. };
  313. #define PCI_ID_TABLE_ENTRY(A) { .descr=A, .n_devs = ARRAY_SIZE(A) }
  314. static const struct pci_id_table pci_dev_table[] = {
  315. PCI_ID_TABLE_ENTRY(pci_dev_descr_i7core_nehalem),
  316. PCI_ID_TABLE_ENTRY(pci_dev_descr_lynnfield),
  317. PCI_ID_TABLE_ENTRY(pci_dev_descr_i7core_westmere),
  318. {0,} /* 0 terminated list. */
  319. };
  320. /*
  321. * pci_device_id table for which devices we are looking for
  322. */
  323. static const struct pci_device_id i7core_pci_tbl[] = {
  324. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_X58_HUB_MGMT)},
  325. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNNFIELD_QPI_LINK0)},
  326. {0,} /* 0 terminated list. */
  327. };
  328. /****************************************************************************
  329. Ancillary status routines
  330. ****************************************************************************/
  331. /* MC_CONTROL bits */
  332. #define CH_ACTIVE(pvt, ch) ((pvt)->info.mc_control & (1 << (8 + ch)))
  333. #define ECCx8(pvt) ((pvt)->info.mc_control & (1 << 1))
  334. /* MC_STATUS bits */
  335. #define ECC_ENABLED(pvt) ((pvt)->info.mc_status & (1 << 4))
  336. #define CH_DISABLED(pvt, ch) ((pvt)->info.mc_status & (1 << ch))
  337. /* MC_MAX_DOD read functions */
  338. static inline int numdimms(u32 dimms)
  339. {
  340. return (dimms & 0x3) + 1;
  341. }
  342. static inline int numrank(u32 rank)
  343. {
  344. static const int ranks[] = { 1, 2, 4, -EINVAL };
  345. return ranks[rank & 0x3];
  346. }
  347. static inline int numbank(u32 bank)
  348. {
  349. static const int banks[] = { 4, 8, 16, -EINVAL };
  350. return banks[bank & 0x3];
  351. }
  352. static inline int numrow(u32 row)
  353. {
  354. static const int rows[] = {
  355. 1 << 12, 1 << 13, 1 << 14, 1 << 15,
  356. 1 << 16, -EINVAL, -EINVAL, -EINVAL,
  357. };
  358. return rows[row & 0x7];
  359. }
  360. static inline int numcol(u32 col)
  361. {
  362. static const int cols[] = {
  363. 1 << 10, 1 << 11, 1 << 12, -EINVAL,
  364. };
  365. return cols[col & 0x3];
  366. }
  367. static struct i7core_dev *get_i7core_dev(u8 socket)
  368. {
  369. struct i7core_dev *i7core_dev;
  370. list_for_each_entry(i7core_dev, &i7core_edac_list, list) {
  371. if (i7core_dev->socket == socket)
  372. return i7core_dev;
  373. }
  374. return NULL;
  375. }
  376. static struct i7core_dev *alloc_i7core_dev(u8 socket,
  377. const struct pci_id_table *table)
  378. {
  379. struct i7core_dev *i7core_dev;
  380. i7core_dev = kzalloc(sizeof(*i7core_dev), GFP_KERNEL);
  381. if (!i7core_dev)
  382. return NULL;
  383. i7core_dev->pdev = kzalloc(sizeof(*i7core_dev->pdev) * table->n_devs,
  384. GFP_KERNEL);
  385. if (!i7core_dev->pdev) {
  386. kfree(i7core_dev);
  387. return NULL;
  388. }
  389. i7core_dev->socket = socket;
  390. i7core_dev->n_devs = table->n_devs;
  391. list_add_tail(&i7core_dev->list, &i7core_edac_list);
  392. return i7core_dev;
  393. }
  394. static void free_i7core_dev(struct i7core_dev *i7core_dev)
  395. {
  396. list_del(&i7core_dev->list);
  397. kfree(i7core_dev->pdev);
  398. kfree(i7core_dev);
  399. }
  400. /****************************************************************************
  401. Memory check routines
  402. ****************************************************************************/
  403. static int get_dimm_config(struct mem_ctl_info *mci)
  404. {
  405. struct i7core_pvt *pvt = mci->pvt_info;
  406. struct pci_dev *pdev;
  407. int i, j;
  408. enum edac_type mode;
  409. enum mem_type mtype;
  410. struct dimm_info *dimm;
  411. /* Get data from the MC register, function 0 */
  412. pdev = pvt->pci_mcr[0];
  413. if (!pdev)
  414. return -ENODEV;
  415. /* Device 3 function 0 reads */
  416. pci_read_config_dword(pdev, MC_CONTROL, &pvt->info.mc_control);
  417. pci_read_config_dword(pdev, MC_STATUS, &pvt->info.mc_status);
  418. pci_read_config_dword(pdev, MC_MAX_DOD, &pvt->info.max_dod);
  419. pci_read_config_dword(pdev, MC_CHANNEL_MAPPER, &pvt->info.ch_map);
  420. edac_dbg(0, "QPI %d control=0x%08x status=0x%08x dod=0x%08x map=0x%08x\n",
  421. pvt->i7core_dev->socket, pvt->info.mc_control,
  422. pvt->info.mc_status, pvt->info.max_dod, pvt->info.ch_map);
  423. if (ECC_ENABLED(pvt)) {
  424. edac_dbg(0, "ECC enabled with x%d SDCC\n", ECCx8(pvt) ? 8 : 4);
  425. if (ECCx8(pvt))
  426. mode = EDAC_S8ECD8ED;
  427. else
  428. mode = EDAC_S4ECD4ED;
  429. } else {
  430. edac_dbg(0, "ECC disabled\n");
  431. mode = EDAC_NONE;
  432. }
  433. /* FIXME: need to handle the error codes */
  434. edac_dbg(0, "DOD Max limits: DIMMS: %d, %d-ranked, %d-banked x%x x 0x%x\n",
  435. numdimms(pvt->info.max_dod),
  436. numrank(pvt->info.max_dod >> 2),
  437. numbank(pvt->info.max_dod >> 4),
  438. numrow(pvt->info.max_dod >> 6),
  439. numcol(pvt->info.max_dod >> 9));
  440. for (i = 0; i < NUM_CHANS; i++) {
  441. u32 data, dimm_dod[3], value[8];
  442. if (!pvt->pci_ch[i][0])
  443. continue;
  444. if (!CH_ACTIVE(pvt, i)) {
  445. edac_dbg(0, "Channel %i is not active\n", i);
  446. continue;
  447. }
  448. if (CH_DISABLED(pvt, i)) {
  449. edac_dbg(0, "Channel %i is disabled\n", i);
  450. continue;
  451. }
  452. /* Devices 4-6 function 0 */
  453. pci_read_config_dword(pvt->pci_ch[i][0],
  454. MC_CHANNEL_DIMM_INIT_PARAMS, &data);
  455. if (data & THREE_DIMMS_PRESENT)
  456. pvt->channel[i].is_3dimms_present = true;
  457. if (data & SINGLE_QUAD_RANK_PRESENT)
  458. pvt->channel[i].is_single_4rank = true;
  459. if (data & QUAD_RANK_PRESENT)
  460. pvt->channel[i].has_4rank = true;
  461. if (data & REGISTERED_DIMM)
  462. mtype = MEM_RDDR3;
  463. else
  464. mtype = MEM_DDR3;
  465. /* Devices 4-6 function 1 */
  466. pci_read_config_dword(pvt->pci_ch[i][1],
  467. MC_DOD_CH_DIMM0, &dimm_dod[0]);
  468. pci_read_config_dword(pvt->pci_ch[i][1],
  469. MC_DOD_CH_DIMM1, &dimm_dod[1]);
  470. pci_read_config_dword(pvt->pci_ch[i][1],
  471. MC_DOD_CH_DIMM2, &dimm_dod[2]);
  472. edac_dbg(0, "Ch%d phy rd%d, wr%d (0x%08x): %s%s%s%cDIMMs\n",
  473. i,
  474. RDLCH(pvt->info.ch_map, i), WRLCH(pvt->info.ch_map, i),
  475. data,
  476. pvt->channel[i].is_3dimms_present ? "3DIMMS " : "",
  477. pvt->channel[i].is_3dimms_present ? "SINGLE_4R " : "",
  478. pvt->channel[i].has_4rank ? "HAS_4R " : "",
  479. (data & REGISTERED_DIMM) ? 'R' : 'U');
  480. for (j = 0; j < 3; j++) {
  481. u32 banks, ranks, rows, cols;
  482. u32 size, npages;
  483. if (!DIMM_PRESENT(dimm_dod[j]))
  484. continue;
  485. dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
  486. i, j, 0);
  487. banks = numbank(MC_DOD_NUMBANK(dimm_dod[j]));
  488. ranks = numrank(MC_DOD_NUMRANK(dimm_dod[j]));
  489. rows = numrow(MC_DOD_NUMROW(dimm_dod[j]));
  490. cols = numcol(MC_DOD_NUMCOL(dimm_dod[j]));
  491. /* DDR3 has 8 I/O banks */
  492. size = (rows * cols * banks * ranks) >> (20 - 3);
  493. edac_dbg(0, "\tdimm %d %d Mb offset: %x, bank: %d, rank: %d, row: %#x, col: %#x\n",
  494. j, size,
  495. RANKOFFSET(dimm_dod[j]),
  496. banks, ranks, rows, cols);
  497. npages = MiB_TO_PAGES(size);
  498. dimm->nr_pages = npages;
  499. switch (banks) {
  500. case 4:
  501. dimm->dtype = DEV_X4;
  502. break;
  503. case 8:
  504. dimm->dtype = DEV_X8;
  505. break;
  506. case 16:
  507. dimm->dtype = DEV_X16;
  508. break;
  509. default:
  510. dimm->dtype = DEV_UNKNOWN;
  511. }
  512. snprintf(dimm->label, sizeof(dimm->label),
  513. "CPU#%uChannel#%u_DIMM#%u",
  514. pvt->i7core_dev->socket, i, j);
  515. dimm->grain = 8;
  516. dimm->edac_mode = mode;
  517. dimm->mtype = mtype;
  518. }
  519. pci_read_config_dword(pdev, MC_SAG_CH_0, &value[0]);
  520. pci_read_config_dword(pdev, MC_SAG_CH_1, &value[1]);
  521. pci_read_config_dword(pdev, MC_SAG_CH_2, &value[2]);
  522. pci_read_config_dword(pdev, MC_SAG_CH_3, &value[3]);
  523. pci_read_config_dword(pdev, MC_SAG_CH_4, &value[4]);
  524. pci_read_config_dword(pdev, MC_SAG_CH_5, &value[5]);
  525. pci_read_config_dword(pdev, MC_SAG_CH_6, &value[6]);
  526. pci_read_config_dword(pdev, MC_SAG_CH_7, &value[7]);
  527. edac_dbg(1, "\t[%i] DIVBY3\tREMOVED\tOFFSET\n", i);
  528. for (j = 0; j < 8; j++)
  529. edac_dbg(1, "\t\t%#x\t%#x\t%#x\n",
  530. (value[j] >> 27) & 0x1,
  531. (value[j] >> 24) & 0x7,
  532. (value[j] & ((1 << 24) - 1)));
  533. }
  534. return 0;
  535. }
  536. /****************************************************************************
  537. Error insertion routines
  538. ****************************************************************************/
  539. #define to_mci(k) container_of(k, struct mem_ctl_info, dev)
  540. /* The i7core has independent error injection features per channel.
  541. However, to have a simpler code, we don't allow enabling error injection
  542. on more than one channel.
  543. Also, since a change at an inject parameter will be applied only at enable,
  544. we're disabling error injection on all write calls to the sysfs nodes that
  545. controls the error code injection.
  546. */
  547. static int disable_inject(const struct mem_ctl_info *mci)
  548. {
  549. struct i7core_pvt *pvt = mci->pvt_info;
  550. pvt->inject.enable = 0;
  551. if (!pvt->pci_ch[pvt->inject.channel][0])
  552. return -ENODEV;
  553. pci_write_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  554. MC_CHANNEL_ERROR_INJECT, 0);
  555. return 0;
  556. }
  557. /*
  558. * i7core inject inject.section
  559. *
  560. * accept and store error injection inject.section value
  561. * bit 0 - refers to the lower 32-byte half cacheline
  562. * bit 1 - refers to the upper 32-byte half cacheline
  563. */
  564. static ssize_t i7core_inject_section_store(struct device *dev,
  565. struct device_attribute *mattr,
  566. const char *data, size_t count)
  567. {
  568. struct mem_ctl_info *mci = to_mci(dev);
  569. struct i7core_pvt *pvt = mci->pvt_info;
  570. unsigned long value;
  571. int rc;
  572. if (pvt->inject.enable)
  573. disable_inject(mci);
  574. rc = kstrtoul(data, 10, &value);
  575. if ((rc < 0) || (value > 3))
  576. return -EIO;
  577. pvt->inject.section = (u32) value;
  578. return count;
  579. }
  580. static ssize_t i7core_inject_section_show(struct device *dev,
  581. struct device_attribute *mattr,
  582. char *data)
  583. {
  584. struct mem_ctl_info *mci = to_mci(dev);
  585. struct i7core_pvt *pvt = mci->pvt_info;
  586. return sprintf(data, "0x%08x\n", pvt->inject.section);
  587. }
  588. /*
  589. * i7core inject.type
  590. *
  591. * accept and store error injection inject.section value
  592. * bit 0 - repeat enable - Enable error repetition
  593. * bit 1 - inject ECC error
  594. * bit 2 - inject parity error
  595. */
  596. static ssize_t i7core_inject_type_store(struct device *dev,
  597. struct device_attribute *mattr,
  598. const char *data, size_t count)
  599. {
  600. struct mem_ctl_info *mci = to_mci(dev);
  601. struct i7core_pvt *pvt = mci->pvt_info;
  602. unsigned long value;
  603. int rc;
  604. if (pvt->inject.enable)
  605. disable_inject(mci);
  606. rc = kstrtoul(data, 10, &value);
  607. if ((rc < 0) || (value > 7))
  608. return -EIO;
  609. pvt->inject.type = (u32) value;
  610. return count;
  611. }
  612. static ssize_t i7core_inject_type_show(struct device *dev,
  613. struct device_attribute *mattr,
  614. char *data)
  615. {
  616. struct mem_ctl_info *mci = to_mci(dev);
  617. struct i7core_pvt *pvt = mci->pvt_info;
  618. return sprintf(data, "0x%08x\n", pvt->inject.type);
  619. }
  620. /*
  621. * i7core_inject_inject.eccmask_store
  622. *
  623. * The type of error (UE/CE) will depend on the inject.eccmask value:
  624. * Any bits set to a 1 will flip the corresponding ECC bit
  625. * Correctable errors can be injected by flipping 1 bit or the bits within
  626. * a symbol pair (2 consecutive aligned 8-bit pairs - i.e. 7:0 and 15:8 or
  627. * 23:16 and 31:24). Flipping bits in two symbol pairs will cause an
  628. * uncorrectable error to be injected.
  629. */
  630. static ssize_t i7core_inject_eccmask_store(struct device *dev,
  631. struct device_attribute *mattr,
  632. const char *data, size_t count)
  633. {
  634. struct mem_ctl_info *mci = to_mci(dev);
  635. struct i7core_pvt *pvt = mci->pvt_info;
  636. unsigned long value;
  637. int rc;
  638. if (pvt->inject.enable)
  639. disable_inject(mci);
  640. rc = kstrtoul(data, 10, &value);
  641. if (rc < 0)
  642. return -EIO;
  643. pvt->inject.eccmask = (u32) value;
  644. return count;
  645. }
  646. static ssize_t i7core_inject_eccmask_show(struct device *dev,
  647. struct device_attribute *mattr,
  648. char *data)
  649. {
  650. struct mem_ctl_info *mci = to_mci(dev);
  651. struct i7core_pvt *pvt = mci->pvt_info;
  652. return sprintf(data, "0x%08x\n", pvt->inject.eccmask);
  653. }
  654. /*
  655. * i7core_addrmatch
  656. *
  657. * The type of error (UE/CE) will depend on the inject.eccmask value:
  658. * Any bits set to a 1 will flip the corresponding ECC bit
  659. * Correctable errors can be injected by flipping 1 bit or the bits within
  660. * a symbol pair (2 consecutive aligned 8-bit pairs - i.e. 7:0 and 15:8 or
  661. * 23:16 and 31:24). Flipping bits in two symbol pairs will cause an
  662. * uncorrectable error to be injected.
  663. */
  664. #define DECLARE_ADDR_MATCH(param, limit) \
  665. static ssize_t i7core_inject_store_##param( \
  666. struct device *dev, \
  667. struct device_attribute *mattr, \
  668. const char *data, size_t count) \
  669. { \
  670. struct mem_ctl_info *mci = dev_get_drvdata(dev); \
  671. struct i7core_pvt *pvt; \
  672. long value; \
  673. int rc; \
  674. \
  675. edac_dbg(1, "\n"); \
  676. pvt = mci->pvt_info; \
  677. \
  678. if (pvt->inject.enable) \
  679. disable_inject(mci); \
  680. \
  681. if (!strcasecmp(data, "any") || !strcasecmp(data, "any\n"))\
  682. value = -1; \
  683. else { \
  684. rc = kstrtoul(data, 10, &value); \
  685. if ((rc < 0) || (value >= limit)) \
  686. return -EIO; \
  687. } \
  688. \
  689. pvt->inject.param = value; \
  690. \
  691. return count; \
  692. } \
  693. \
  694. static ssize_t i7core_inject_show_##param( \
  695. struct device *dev, \
  696. struct device_attribute *mattr, \
  697. char *data) \
  698. { \
  699. struct mem_ctl_info *mci = dev_get_drvdata(dev); \
  700. struct i7core_pvt *pvt; \
  701. \
  702. pvt = mci->pvt_info; \
  703. edac_dbg(1, "pvt=%p\n", pvt); \
  704. if (pvt->inject.param < 0) \
  705. return sprintf(data, "any\n"); \
  706. else \
  707. return sprintf(data, "%d\n", pvt->inject.param);\
  708. }
  709. #define ATTR_ADDR_MATCH(param) \
  710. static DEVICE_ATTR(param, S_IRUGO | S_IWUSR, \
  711. i7core_inject_show_##param, \
  712. i7core_inject_store_##param)
  713. DECLARE_ADDR_MATCH(channel, 3);
  714. DECLARE_ADDR_MATCH(dimm, 3);
  715. DECLARE_ADDR_MATCH(rank, 4);
  716. DECLARE_ADDR_MATCH(bank, 32);
  717. DECLARE_ADDR_MATCH(page, 0x10000);
  718. DECLARE_ADDR_MATCH(col, 0x4000);
  719. ATTR_ADDR_MATCH(channel);
  720. ATTR_ADDR_MATCH(dimm);
  721. ATTR_ADDR_MATCH(rank);
  722. ATTR_ADDR_MATCH(bank);
  723. ATTR_ADDR_MATCH(page);
  724. ATTR_ADDR_MATCH(col);
  725. static int write_and_test(struct pci_dev *dev, const int where, const u32 val)
  726. {
  727. u32 read;
  728. int count;
  729. edac_dbg(0, "setting pci %02x:%02x.%x reg=%02x value=%08x\n",
  730. dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
  731. where, val);
  732. for (count = 0; count < 10; count++) {
  733. if (count)
  734. msleep(100);
  735. pci_write_config_dword(dev, where, val);
  736. pci_read_config_dword(dev, where, &read);
  737. if (read == val)
  738. return 0;
  739. }
  740. i7core_printk(KERN_ERR, "Error during set pci %02x:%02x.%x reg=%02x "
  741. "write=%08x. Read=%08x\n",
  742. dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
  743. where, val, read);
  744. return -EINVAL;
  745. }
  746. /*
  747. * This routine prepares the Memory Controller for error injection.
  748. * The error will be injected when some process tries to write to the
  749. * memory that matches the given criteria.
  750. * The criteria can be set in terms of a mask where dimm, rank, bank, page
  751. * and col can be specified.
  752. * A -1 value for any of the mask items will make the MCU to ignore
  753. * that matching criteria for error injection.
  754. *
  755. * It should be noticed that the error will only happen after a write operation
  756. * on a memory that matches the condition. if REPEAT_EN is not enabled at
  757. * inject mask, then it will produce just one error. Otherwise, it will repeat
  758. * until the injectmask would be cleaned.
  759. *
  760. * FIXME: This routine assumes that MAXNUMDIMMS value of MC_MAX_DOD
  761. * is reliable enough to check if the MC is using the
  762. * three channels. However, this is not clear at the datasheet.
  763. */
  764. static ssize_t i7core_inject_enable_store(struct device *dev,
  765. struct device_attribute *mattr,
  766. const char *data, size_t count)
  767. {
  768. struct mem_ctl_info *mci = to_mci(dev);
  769. struct i7core_pvt *pvt = mci->pvt_info;
  770. u32 injectmask;
  771. u64 mask = 0;
  772. int rc;
  773. long enable;
  774. if (!pvt->pci_ch[pvt->inject.channel][0])
  775. return 0;
  776. rc = kstrtoul(data, 10, &enable);
  777. if ((rc < 0))
  778. return 0;
  779. if (enable) {
  780. pvt->inject.enable = 1;
  781. } else {
  782. disable_inject(mci);
  783. return count;
  784. }
  785. /* Sets pvt->inject.dimm mask */
  786. if (pvt->inject.dimm < 0)
  787. mask |= 1LL << 41;
  788. else {
  789. if (pvt->channel[pvt->inject.channel].dimms > 2)
  790. mask |= (pvt->inject.dimm & 0x3LL) << 35;
  791. else
  792. mask |= (pvt->inject.dimm & 0x1LL) << 36;
  793. }
  794. /* Sets pvt->inject.rank mask */
  795. if (pvt->inject.rank < 0)
  796. mask |= 1LL << 40;
  797. else {
  798. if (pvt->channel[pvt->inject.channel].dimms > 2)
  799. mask |= (pvt->inject.rank & 0x1LL) << 34;
  800. else
  801. mask |= (pvt->inject.rank & 0x3LL) << 34;
  802. }
  803. /* Sets pvt->inject.bank mask */
  804. if (pvt->inject.bank < 0)
  805. mask |= 1LL << 39;
  806. else
  807. mask |= (pvt->inject.bank & 0x15LL) << 30;
  808. /* Sets pvt->inject.page mask */
  809. if (pvt->inject.page < 0)
  810. mask |= 1LL << 38;
  811. else
  812. mask |= (pvt->inject.page & 0xffff) << 14;
  813. /* Sets pvt->inject.column mask */
  814. if (pvt->inject.col < 0)
  815. mask |= 1LL << 37;
  816. else
  817. mask |= (pvt->inject.col & 0x3fff);
  818. /*
  819. * bit 0: REPEAT_EN
  820. * bits 1-2: MASK_HALF_CACHELINE
  821. * bit 3: INJECT_ECC
  822. * bit 4: INJECT_ADDR_PARITY
  823. */
  824. injectmask = (pvt->inject.type & 1) |
  825. (pvt->inject.section & 0x3) << 1 |
  826. (pvt->inject.type & 0x6) << (3 - 1);
  827. /* Unlock writes to registers - this register is write only */
  828. pci_write_config_dword(pvt->pci_noncore,
  829. MC_CFG_CONTROL, 0x2);
  830. write_and_test(pvt->pci_ch[pvt->inject.channel][0],
  831. MC_CHANNEL_ADDR_MATCH, mask);
  832. write_and_test(pvt->pci_ch[pvt->inject.channel][0],
  833. MC_CHANNEL_ADDR_MATCH + 4, mask >> 32L);
  834. write_and_test(pvt->pci_ch[pvt->inject.channel][0],
  835. MC_CHANNEL_ERROR_MASK, pvt->inject.eccmask);
  836. write_and_test(pvt->pci_ch[pvt->inject.channel][0],
  837. MC_CHANNEL_ERROR_INJECT, injectmask);
  838. /*
  839. * This is something undocumented, based on my tests
  840. * Without writing 8 to this register, errors aren't injected. Not sure
  841. * why.
  842. */
  843. pci_write_config_dword(pvt->pci_noncore,
  844. MC_CFG_CONTROL, 8);
  845. edac_dbg(0, "Error inject addr match 0x%016llx, ecc 0x%08x, inject 0x%08x\n",
  846. mask, pvt->inject.eccmask, injectmask);
  847. return count;
  848. }
  849. static ssize_t i7core_inject_enable_show(struct device *dev,
  850. struct device_attribute *mattr,
  851. char *data)
  852. {
  853. struct mem_ctl_info *mci = to_mci(dev);
  854. struct i7core_pvt *pvt = mci->pvt_info;
  855. u32 injectmask;
  856. if (!pvt->pci_ch[pvt->inject.channel][0])
  857. return 0;
  858. pci_read_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  859. MC_CHANNEL_ERROR_INJECT, &injectmask);
  860. edac_dbg(0, "Inject error read: 0x%018x\n", injectmask);
  861. if (injectmask & 0x0c)
  862. pvt->inject.enable = 1;
  863. return sprintf(data, "%d\n", pvt->inject.enable);
  864. }
  865. #define DECLARE_COUNTER(param) \
  866. static ssize_t i7core_show_counter_##param( \
  867. struct device *dev, \
  868. struct device_attribute *mattr, \
  869. char *data) \
  870. { \
  871. struct mem_ctl_info *mci = dev_get_drvdata(dev); \
  872. struct i7core_pvt *pvt = mci->pvt_info; \
  873. \
  874. edac_dbg(1, "\n"); \
  875. if (!pvt->ce_count_available || (pvt->is_registered)) \
  876. return sprintf(data, "data unavailable\n"); \
  877. return sprintf(data, "%lu\n", \
  878. pvt->udimm_ce_count[param]); \
  879. }
  880. #define ATTR_COUNTER(param) \
  881. static DEVICE_ATTR(udimm##param, S_IRUGO | S_IWUSR, \
  882. i7core_show_counter_##param, \
  883. NULL)
  884. DECLARE_COUNTER(0);
  885. DECLARE_COUNTER(1);
  886. DECLARE_COUNTER(2);
  887. ATTR_COUNTER(0);
  888. ATTR_COUNTER(1);
  889. ATTR_COUNTER(2);
  890. /*
  891. * inject_addrmatch device sysfs struct
  892. */
  893. static struct attribute *i7core_addrmatch_attrs[] = {
  894. &dev_attr_channel.attr,
  895. &dev_attr_dimm.attr,
  896. &dev_attr_rank.attr,
  897. &dev_attr_bank.attr,
  898. &dev_attr_page.attr,
  899. &dev_attr_col.attr,
  900. NULL
  901. };
  902. static struct attribute_group addrmatch_grp = {
  903. .attrs = i7core_addrmatch_attrs,
  904. };
  905. static const struct attribute_group *addrmatch_groups[] = {
  906. &addrmatch_grp,
  907. NULL
  908. };
  909. static void addrmatch_release(struct device *device)
  910. {
  911. edac_dbg(1, "Releasing device %s\n", dev_name(device));
  912. kfree(device);
  913. }
  914. static struct device_type addrmatch_type = {
  915. .groups = addrmatch_groups,
  916. .release = addrmatch_release,
  917. };
  918. /*
  919. * all_channel_counts sysfs struct
  920. */
  921. static struct attribute *i7core_udimm_counters_attrs[] = {
  922. &dev_attr_udimm0.attr,
  923. &dev_attr_udimm1.attr,
  924. &dev_attr_udimm2.attr,
  925. NULL
  926. };
  927. static struct attribute_group all_channel_counts_grp = {
  928. .attrs = i7core_udimm_counters_attrs,
  929. };
  930. static const struct attribute_group *all_channel_counts_groups[] = {
  931. &all_channel_counts_grp,
  932. NULL
  933. };
  934. static void all_channel_counts_release(struct device *device)
  935. {
  936. edac_dbg(1, "Releasing device %s\n", dev_name(device));
  937. kfree(device);
  938. }
  939. static struct device_type all_channel_counts_type = {
  940. .groups = all_channel_counts_groups,
  941. .release = all_channel_counts_release,
  942. };
  943. /*
  944. * inject sysfs attributes
  945. */
  946. static DEVICE_ATTR(inject_section, S_IRUGO | S_IWUSR,
  947. i7core_inject_section_show, i7core_inject_section_store);
  948. static DEVICE_ATTR(inject_type, S_IRUGO | S_IWUSR,
  949. i7core_inject_type_show, i7core_inject_type_store);
  950. static DEVICE_ATTR(inject_eccmask, S_IRUGO | S_IWUSR,
  951. i7core_inject_eccmask_show, i7core_inject_eccmask_store);
  952. static DEVICE_ATTR(inject_enable, S_IRUGO | S_IWUSR,
  953. i7core_inject_enable_show, i7core_inject_enable_store);
  954. static struct attribute *i7core_dev_attrs[] = {
  955. &dev_attr_inject_section.attr,
  956. &dev_attr_inject_type.attr,
  957. &dev_attr_inject_eccmask.attr,
  958. &dev_attr_inject_enable.attr,
  959. NULL
  960. };
  961. ATTRIBUTE_GROUPS(i7core_dev);
  962. static int i7core_create_sysfs_devices(struct mem_ctl_info *mci)
  963. {
  964. struct i7core_pvt *pvt = mci->pvt_info;
  965. int rc;
  966. pvt->addrmatch_dev = kzalloc(sizeof(*pvt->addrmatch_dev), GFP_KERNEL);
  967. if (!pvt->addrmatch_dev)
  968. return -ENOMEM;
  969. pvt->addrmatch_dev->type = &addrmatch_type;
  970. pvt->addrmatch_dev->bus = mci->dev.bus;
  971. device_initialize(pvt->addrmatch_dev);
  972. pvt->addrmatch_dev->parent = &mci->dev;
  973. dev_set_name(pvt->addrmatch_dev, "inject_addrmatch");
  974. dev_set_drvdata(pvt->addrmatch_dev, mci);
  975. edac_dbg(1, "creating %s\n", dev_name(pvt->addrmatch_dev));
  976. rc = device_add(pvt->addrmatch_dev);
  977. if (rc < 0)
  978. goto err_put_addrmatch;
  979. if (!pvt->is_registered) {
  980. pvt->chancounts_dev = kzalloc(sizeof(*pvt->chancounts_dev),
  981. GFP_KERNEL);
  982. if (!pvt->chancounts_dev) {
  983. rc = -ENOMEM;
  984. goto err_del_addrmatch;
  985. }
  986. pvt->chancounts_dev->type = &all_channel_counts_type;
  987. pvt->chancounts_dev->bus = mci->dev.bus;
  988. device_initialize(pvt->chancounts_dev);
  989. pvt->chancounts_dev->parent = &mci->dev;
  990. dev_set_name(pvt->chancounts_dev, "all_channel_counts");
  991. dev_set_drvdata(pvt->chancounts_dev, mci);
  992. edac_dbg(1, "creating %s\n", dev_name(pvt->chancounts_dev));
  993. rc = device_add(pvt->chancounts_dev);
  994. if (rc < 0)
  995. goto err_put_chancounts;
  996. }
  997. return 0;
  998. err_put_chancounts:
  999. put_device(pvt->chancounts_dev);
  1000. err_del_addrmatch:
  1001. device_del(pvt->addrmatch_dev);
  1002. err_put_addrmatch:
  1003. put_device(pvt->addrmatch_dev);
  1004. return rc;
  1005. }
  1006. static void i7core_delete_sysfs_devices(struct mem_ctl_info *mci)
  1007. {
  1008. struct i7core_pvt *pvt = mci->pvt_info;
  1009. edac_dbg(1, "\n");
  1010. if (!pvt->is_registered) {
  1011. device_del(pvt->chancounts_dev);
  1012. put_device(pvt->chancounts_dev);
  1013. }
  1014. device_del(pvt->addrmatch_dev);
  1015. put_device(pvt->addrmatch_dev);
  1016. }
  1017. /****************************************************************************
  1018. Device initialization routines: put/get, init/exit
  1019. ****************************************************************************/
  1020. /*
  1021. * i7core_put_all_devices 'put' all the devices that we have
  1022. * reserved via 'get'
  1023. */
  1024. static void i7core_put_devices(struct i7core_dev *i7core_dev)
  1025. {
  1026. int i;
  1027. edac_dbg(0, "\n");
  1028. for (i = 0; i < i7core_dev->n_devs; i++) {
  1029. struct pci_dev *pdev = i7core_dev->pdev[i];
  1030. if (!pdev)
  1031. continue;
  1032. edac_dbg(0, "Removing dev %02x:%02x.%d\n",
  1033. pdev->bus->number,
  1034. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  1035. pci_dev_put(pdev);
  1036. }
  1037. }
  1038. static void i7core_put_all_devices(void)
  1039. {
  1040. struct i7core_dev *i7core_dev, *tmp;
  1041. list_for_each_entry_safe(i7core_dev, tmp, &i7core_edac_list, list) {
  1042. i7core_put_devices(i7core_dev);
  1043. free_i7core_dev(i7core_dev);
  1044. }
  1045. }
  1046. static void __init i7core_xeon_pci_fixup(const struct pci_id_table *table)
  1047. {
  1048. struct pci_dev *pdev = NULL;
  1049. int i;
  1050. /*
  1051. * On Xeon 55xx, the Intel Quick Path Arch Generic Non-core pci buses
  1052. * aren't announced by acpi. So, we need to use a legacy scan probing
  1053. * to detect them
  1054. */
  1055. while (table && table->descr) {
  1056. pdev = pci_get_device(PCI_VENDOR_ID_INTEL, table->descr[0].dev_id, NULL);
  1057. if (unlikely(!pdev)) {
  1058. for (i = 0; i < MAX_SOCKET_BUSES; i++)
  1059. pcibios_scan_specific_bus(255-i);
  1060. }
  1061. pci_dev_put(pdev);
  1062. table++;
  1063. }
  1064. }
  1065. static unsigned i7core_pci_lastbus(void)
  1066. {
  1067. int last_bus = 0, bus;
  1068. struct pci_bus *b = NULL;
  1069. while ((b = pci_find_next_bus(b)) != NULL) {
  1070. bus = b->number;
  1071. edac_dbg(0, "Found bus %d\n", bus);
  1072. if (bus > last_bus)
  1073. last_bus = bus;
  1074. }
  1075. edac_dbg(0, "Last bus %d\n", last_bus);
  1076. return last_bus;
  1077. }
  1078. /*
  1079. * i7core_get_all_devices Find and perform 'get' operation on the MCH's
  1080. * device/functions we want to reference for this driver
  1081. *
  1082. * Need to 'get' device 16 func 1 and func 2
  1083. */
  1084. static int i7core_get_onedevice(struct pci_dev **prev,
  1085. const struct pci_id_table *table,
  1086. const unsigned devno,
  1087. const unsigned last_bus)
  1088. {
  1089. struct i7core_dev *i7core_dev;
  1090. const struct pci_id_descr *dev_descr = &table->descr[devno];
  1091. struct pci_dev *pdev = NULL;
  1092. u8 bus = 0;
  1093. u8 socket = 0;
  1094. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1095. dev_descr->dev_id, *prev);
  1096. /*
  1097. * On Xeon 55xx, the Intel QuickPath Arch Generic Non-core regs
  1098. * is at addr 8086:2c40, instead of 8086:2c41. So, we need
  1099. * to probe for the alternate address in case of failure
  1100. */
  1101. if (dev_descr->dev_id == PCI_DEVICE_ID_INTEL_I7_NONCORE && !pdev) {
  1102. pci_dev_get(*prev); /* pci_get_device will put it */
  1103. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1104. PCI_DEVICE_ID_INTEL_I7_NONCORE_ALT, *prev);
  1105. }
  1106. if (dev_descr->dev_id == PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE &&
  1107. !pdev) {
  1108. pci_dev_get(*prev); /* pci_get_device will put it */
  1109. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1110. PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE_ALT,
  1111. *prev);
  1112. }
  1113. if (!pdev) {
  1114. if (*prev) {
  1115. *prev = pdev;
  1116. return 0;
  1117. }
  1118. if (dev_descr->optional)
  1119. return 0;
  1120. if (devno == 0)
  1121. return -ENODEV;
  1122. i7core_printk(KERN_INFO,
  1123. "Device not found: dev %02x.%d PCI ID %04x:%04x\n",
  1124. dev_descr->dev, dev_descr->func,
  1125. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1126. /* End of list, leave */
  1127. return -ENODEV;
  1128. }
  1129. bus = pdev->bus->number;
  1130. socket = last_bus - bus;
  1131. i7core_dev = get_i7core_dev(socket);
  1132. if (!i7core_dev) {
  1133. i7core_dev = alloc_i7core_dev(socket, table);
  1134. if (!i7core_dev) {
  1135. pci_dev_put(pdev);
  1136. return -ENOMEM;
  1137. }
  1138. }
  1139. if (i7core_dev->pdev[devno]) {
  1140. i7core_printk(KERN_ERR,
  1141. "Duplicated device for "
  1142. "dev %02x:%02x.%d PCI ID %04x:%04x\n",
  1143. bus, dev_descr->dev, dev_descr->func,
  1144. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1145. pci_dev_put(pdev);
  1146. return -ENODEV;
  1147. }
  1148. i7core_dev->pdev[devno] = pdev;
  1149. /* Sanity check */
  1150. if (unlikely(PCI_SLOT(pdev->devfn) != dev_descr->dev ||
  1151. PCI_FUNC(pdev->devfn) != dev_descr->func)) {
  1152. i7core_printk(KERN_ERR,
  1153. "Device PCI ID %04x:%04x "
  1154. "has dev %02x:%02x.%d instead of dev %02x:%02x.%d\n",
  1155. PCI_VENDOR_ID_INTEL, dev_descr->dev_id,
  1156. bus, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1157. bus, dev_descr->dev, dev_descr->func);
  1158. return -ENODEV;
  1159. }
  1160. /* Be sure that the device is enabled */
  1161. if (unlikely(pci_enable_device(pdev) < 0)) {
  1162. i7core_printk(KERN_ERR,
  1163. "Couldn't enable "
  1164. "dev %02x:%02x.%d PCI ID %04x:%04x\n",
  1165. bus, dev_descr->dev, dev_descr->func,
  1166. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1167. return -ENODEV;
  1168. }
  1169. edac_dbg(0, "Detected socket %d dev %02x:%02x.%d PCI ID %04x:%04x\n",
  1170. socket, bus, dev_descr->dev,
  1171. dev_descr->func,
  1172. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1173. /*
  1174. * As stated on drivers/pci/search.c, the reference count for
  1175. * @from is always decremented if it is not %NULL. So, as we need
  1176. * to get all devices up to null, we need to do a get for the device
  1177. */
  1178. pci_dev_get(pdev);
  1179. *prev = pdev;
  1180. return 0;
  1181. }
  1182. static int i7core_get_all_devices(void)
  1183. {
  1184. int i, rc, last_bus;
  1185. struct pci_dev *pdev = NULL;
  1186. const struct pci_id_table *table = pci_dev_table;
  1187. last_bus = i7core_pci_lastbus();
  1188. while (table && table->descr) {
  1189. for (i = 0; i < table->n_devs; i++) {
  1190. pdev = NULL;
  1191. do {
  1192. rc = i7core_get_onedevice(&pdev, table, i,
  1193. last_bus);
  1194. if (rc < 0) {
  1195. if (i == 0) {
  1196. i = table->n_devs;
  1197. break;
  1198. }
  1199. i7core_put_all_devices();
  1200. return -ENODEV;
  1201. }
  1202. } while (pdev);
  1203. }
  1204. table++;
  1205. }
  1206. return 0;
  1207. }
  1208. static int mci_bind_devs(struct mem_ctl_info *mci,
  1209. struct i7core_dev *i7core_dev)
  1210. {
  1211. struct i7core_pvt *pvt = mci->pvt_info;
  1212. struct pci_dev *pdev;
  1213. int i, func, slot;
  1214. char *family;
  1215. pvt->is_registered = false;
  1216. pvt->enable_scrub = false;
  1217. for (i = 0; i < i7core_dev->n_devs; i++) {
  1218. pdev = i7core_dev->pdev[i];
  1219. if (!pdev)
  1220. continue;
  1221. func = PCI_FUNC(pdev->devfn);
  1222. slot = PCI_SLOT(pdev->devfn);
  1223. if (slot == 3) {
  1224. if (unlikely(func > MAX_MCR_FUNC))
  1225. goto error;
  1226. pvt->pci_mcr[func] = pdev;
  1227. } else if (likely(slot >= 4 && slot < 4 + NUM_CHANS)) {
  1228. if (unlikely(func > MAX_CHAN_FUNC))
  1229. goto error;
  1230. pvt->pci_ch[slot - 4][func] = pdev;
  1231. } else if (!slot && !func) {
  1232. pvt->pci_noncore = pdev;
  1233. /* Detect the processor family */
  1234. switch (pdev->device) {
  1235. case PCI_DEVICE_ID_INTEL_I7_NONCORE:
  1236. family = "Xeon 35xx/ i7core";
  1237. pvt->enable_scrub = false;
  1238. break;
  1239. case PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE_ALT:
  1240. family = "i7-800/i5-700";
  1241. pvt->enable_scrub = false;
  1242. break;
  1243. case PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE:
  1244. family = "Xeon 34xx";
  1245. pvt->enable_scrub = false;
  1246. break;
  1247. case PCI_DEVICE_ID_INTEL_I7_NONCORE_ALT:
  1248. family = "Xeon 55xx";
  1249. pvt->enable_scrub = true;
  1250. break;
  1251. case PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE_REV2:
  1252. family = "Xeon 56xx / i7-900";
  1253. pvt->enable_scrub = true;
  1254. break;
  1255. default:
  1256. family = "unknown";
  1257. pvt->enable_scrub = false;
  1258. }
  1259. edac_dbg(0, "Detected a processor type %s\n", family);
  1260. } else
  1261. goto error;
  1262. edac_dbg(0, "Associated fn %d.%d, dev = %p, socket %d\n",
  1263. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1264. pdev, i7core_dev->socket);
  1265. if (PCI_SLOT(pdev->devfn) == 3 &&
  1266. PCI_FUNC(pdev->devfn) == 2)
  1267. pvt->is_registered = true;
  1268. }
  1269. return 0;
  1270. error:
  1271. i7core_printk(KERN_ERR, "Device %d, function %d "
  1272. "is out of the expected range\n",
  1273. slot, func);
  1274. return -EINVAL;
  1275. }
  1276. /****************************************************************************
  1277. Error check routines
  1278. ****************************************************************************/
  1279. static void i7core_rdimm_update_ce_count(struct mem_ctl_info *mci,
  1280. const int chan,
  1281. const int new0,
  1282. const int new1,
  1283. const int new2)
  1284. {
  1285. struct i7core_pvt *pvt = mci->pvt_info;
  1286. int add0 = 0, add1 = 0, add2 = 0;
  1287. /* Updates CE counters if it is not the first time here */
  1288. if (pvt->ce_count_available) {
  1289. /* Updates CE counters */
  1290. add2 = new2 - pvt->rdimm_last_ce_count[chan][2];
  1291. add1 = new1 - pvt->rdimm_last_ce_count[chan][1];
  1292. add0 = new0 - pvt->rdimm_last_ce_count[chan][0];
  1293. if (add2 < 0)
  1294. add2 += 0x7fff;
  1295. pvt->rdimm_ce_count[chan][2] += add2;
  1296. if (add1 < 0)
  1297. add1 += 0x7fff;
  1298. pvt->rdimm_ce_count[chan][1] += add1;
  1299. if (add0 < 0)
  1300. add0 += 0x7fff;
  1301. pvt->rdimm_ce_count[chan][0] += add0;
  1302. } else
  1303. pvt->ce_count_available = 1;
  1304. /* Store the new values */
  1305. pvt->rdimm_last_ce_count[chan][2] = new2;
  1306. pvt->rdimm_last_ce_count[chan][1] = new1;
  1307. pvt->rdimm_last_ce_count[chan][0] = new0;
  1308. /*updated the edac core */
  1309. if (add0 != 0)
  1310. edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, add0,
  1311. 0, 0, 0,
  1312. chan, 0, -1, "error", "");
  1313. if (add1 != 0)
  1314. edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, add1,
  1315. 0, 0, 0,
  1316. chan, 1, -1, "error", "");
  1317. if (add2 != 0)
  1318. edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, add2,
  1319. 0, 0, 0,
  1320. chan, 2, -1, "error", "");
  1321. }
  1322. static void i7core_rdimm_check_mc_ecc_err(struct mem_ctl_info *mci)
  1323. {
  1324. struct i7core_pvt *pvt = mci->pvt_info;
  1325. u32 rcv[3][2];
  1326. int i, new0, new1, new2;
  1327. /*Read DEV 3: FUN 2: MC_COR_ECC_CNT regs directly*/
  1328. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_0,
  1329. &rcv[0][0]);
  1330. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_1,
  1331. &rcv[0][1]);
  1332. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_2,
  1333. &rcv[1][0]);
  1334. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_3,
  1335. &rcv[1][1]);
  1336. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_4,
  1337. &rcv[2][0]);
  1338. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_5,
  1339. &rcv[2][1]);
  1340. for (i = 0 ; i < 3; i++) {
  1341. edac_dbg(3, "MC_COR_ECC_CNT%d = 0x%x; MC_COR_ECC_CNT%d = 0x%x\n",
  1342. (i * 2), rcv[i][0], (i * 2) + 1, rcv[i][1]);
  1343. /*if the channel has 3 dimms*/
  1344. if (pvt->channel[i].dimms > 2) {
  1345. new0 = DIMM_BOT_COR_ERR(rcv[i][0]);
  1346. new1 = DIMM_TOP_COR_ERR(rcv[i][0]);
  1347. new2 = DIMM_BOT_COR_ERR(rcv[i][1]);
  1348. } else {
  1349. new0 = DIMM_TOP_COR_ERR(rcv[i][0]) +
  1350. DIMM_BOT_COR_ERR(rcv[i][0]);
  1351. new1 = DIMM_TOP_COR_ERR(rcv[i][1]) +
  1352. DIMM_BOT_COR_ERR(rcv[i][1]);
  1353. new2 = 0;
  1354. }
  1355. i7core_rdimm_update_ce_count(mci, i, new0, new1, new2);
  1356. }
  1357. }
  1358. /* This function is based on the device 3 function 4 registers as described on:
  1359. * Intel Xeon Processor 5500 Series Datasheet Volume 2
  1360. * http://www.intel.com/Assets/PDF/datasheet/321322.pdf
  1361. * also available at:
  1362. * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
  1363. */
  1364. static void i7core_udimm_check_mc_ecc_err(struct mem_ctl_info *mci)
  1365. {
  1366. struct i7core_pvt *pvt = mci->pvt_info;
  1367. u32 rcv1, rcv0;
  1368. int new0, new1, new2;
  1369. if (!pvt->pci_mcr[4]) {
  1370. edac_dbg(0, "MCR registers not found\n");
  1371. return;
  1372. }
  1373. /* Corrected test errors */
  1374. pci_read_config_dword(pvt->pci_mcr[4], MC_TEST_ERR_RCV1, &rcv1);
  1375. pci_read_config_dword(pvt->pci_mcr[4], MC_TEST_ERR_RCV0, &rcv0);
  1376. /* Store the new values */
  1377. new2 = DIMM2_COR_ERR(rcv1);
  1378. new1 = DIMM1_COR_ERR(rcv0);
  1379. new0 = DIMM0_COR_ERR(rcv0);
  1380. /* Updates CE counters if it is not the first time here */
  1381. if (pvt->ce_count_available) {
  1382. /* Updates CE counters */
  1383. int add0, add1, add2;
  1384. add2 = new2 - pvt->udimm_last_ce_count[2];
  1385. add1 = new1 - pvt->udimm_last_ce_count[1];
  1386. add0 = new0 - pvt->udimm_last_ce_count[0];
  1387. if (add2 < 0)
  1388. add2 += 0x7fff;
  1389. pvt->udimm_ce_count[2] += add2;
  1390. if (add1 < 0)
  1391. add1 += 0x7fff;
  1392. pvt->udimm_ce_count[1] += add1;
  1393. if (add0 < 0)
  1394. add0 += 0x7fff;
  1395. pvt->udimm_ce_count[0] += add0;
  1396. if (add0 | add1 | add2)
  1397. i7core_printk(KERN_ERR, "New Corrected error(s): "
  1398. "dimm0: +%d, dimm1: +%d, dimm2 +%d\n",
  1399. add0, add1, add2);
  1400. } else
  1401. pvt->ce_count_available = 1;
  1402. /* Store the new values */
  1403. pvt->udimm_last_ce_count[2] = new2;
  1404. pvt->udimm_last_ce_count[1] = new1;
  1405. pvt->udimm_last_ce_count[0] = new0;
  1406. }
  1407. /*
  1408. * According with tables E-11 and E-12 of chapter E.3.3 of Intel 64 and IA-32
  1409. * Architectures Software Developer’s Manual Volume 3B.
  1410. * Nehalem are defined as family 0x06, model 0x1a
  1411. *
  1412. * The MCA registers used here are the following ones:
  1413. * struct mce field MCA Register
  1414. * m->status MSR_IA32_MC8_STATUS
  1415. * m->addr MSR_IA32_MC8_ADDR
  1416. * m->misc MSR_IA32_MC8_MISC
  1417. * In the case of Nehalem, the error information is masked at .status and .misc
  1418. * fields
  1419. */
  1420. static void i7core_mce_output_error(struct mem_ctl_info *mci,
  1421. const struct mce *m)
  1422. {
  1423. struct i7core_pvt *pvt = mci->pvt_info;
  1424. char *optype, *err;
  1425. enum hw_event_mc_err_type tp_event;
  1426. unsigned long error = m->status & 0x1ff0000l;
  1427. bool uncorrected_error = m->mcgstatus & 1ll << 61;
  1428. bool ripv = m->mcgstatus & 1;
  1429. u32 optypenum = (m->status >> 4) & 0x07;
  1430. u32 core_err_cnt = (m->status >> 38) & 0x7fff;
  1431. u32 dimm = (m->misc >> 16) & 0x3;
  1432. u32 channel = (m->misc >> 18) & 0x3;
  1433. u32 syndrome = m->misc >> 32;
  1434. u32 errnum = find_first_bit(&error, 32);
  1435. if (uncorrected_error) {
  1436. if (ripv)
  1437. tp_event = HW_EVENT_ERR_FATAL;
  1438. else
  1439. tp_event = HW_EVENT_ERR_UNCORRECTED;
  1440. } else {
  1441. tp_event = HW_EVENT_ERR_CORRECTED;
  1442. }
  1443. switch (optypenum) {
  1444. case 0:
  1445. optype = "generic undef request";
  1446. break;
  1447. case 1:
  1448. optype = "read error";
  1449. break;
  1450. case 2:
  1451. optype = "write error";
  1452. break;
  1453. case 3:
  1454. optype = "addr/cmd error";
  1455. break;
  1456. case 4:
  1457. optype = "scrubbing error";
  1458. break;
  1459. default:
  1460. optype = "reserved";
  1461. break;
  1462. }
  1463. switch (errnum) {
  1464. case 16:
  1465. err = "read ECC error";
  1466. break;
  1467. case 17:
  1468. err = "RAS ECC error";
  1469. break;
  1470. case 18:
  1471. err = "write parity error";
  1472. break;
  1473. case 19:
  1474. err = "redundacy loss";
  1475. break;
  1476. case 20:
  1477. err = "reserved";
  1478. break;
  1479. case 21:
  1480. err = "memory range error";
  1481. break;
  1482. case 22:
  1483. err = "RTID out of range";
  1484. break;
  1485. case 23:
  1486. err = "address parity error";
  1487. break;
  1488. case 24:
  1489. err = "byte enable parity error";
  1490. break;
  1491. default:
  1492. err = "unknown";
  1493. }
  1494. /*
  1495. * Call the helper to output message
  1496. * FIXME: what to do if core_err_cnt > 1? Currently, it generates
  1497. * only one event
  1498. */
  1499. if (uncorrected_error || !pvt->is_registered)
  1500. edac_mc_handle_error(tp_event, mci, core_err_cnt,
  1501. m->addr >> PAGE_SHIFT,
  1502. m->addr & ~PAGE_MASK,
  1503. syndrome,
  1504. channel, dimm, -1,
  1505. err, optype);
  1506. }
  1507. /*
  1508. * i7core_check_error Retrieve and process errors reported by the
  1509. * hardware. Called by the Core module.
  1510. */
  1511. static void i7core_check_error(struct mem_ctl_info *mci)
  1512. {
  1513. struct i7core_pvt *pvt = mci->pvt_info;
  1514. int i;
  1515. unsigned count = 0;
  1516. struct mce *m;
  1517. /*
  1518. * MCE first step: Copy all mce errors into a temporary buffer
  1519. * We use a double buffering here, to reduce the risk of
  1520. * losing an error.
  1521. */
  1522. smp_rmb();
  1523. count = (pvt->mce_out + MCE_LOG_LEN - pvt->mce_in)
  1524. % MCE_LOG_LEN;
  1525. if (!count)
  1526. goto check_ce_error;
  1527. m = pvt->mce_outentry;
  1528. if (pvt->mce_in + count > MCE_LOG_LEN) {
  1529. unsigned l = MCE_LOG_LEN - pvt->mce_in;
  1530. memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * l);
  1531. smp_wmb();
  1532. pvt->mce_in = 0;
  1533. count -= l;
  1534. m += l;
  1535. }
  1536. memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * count);
  1537. smp_wmb();
  1538. pvt->mce_in += count;
  1539. smp_rmb();
  1540. if (pvt->mce_overrun) {
  1541. i7core_printk(KERN_ERR, "Lost %d memory errors\n",
  1542. pvt->mce_overrun);
  1543. smp_wmb();
  1544. pvt->mce_overrun = 0;
  1545. }
  1546. /*
  1547. * MCE second step: parse errors and display
  1548. */
  1549. for (i = 0; i < count; i++)
  1550. i7core_mce_output_error(mci, &pvt->mce_outentry[i]);
  1551. /*
  1552. * Now, let's increment CE error counts
  1553. */
  1554. check_ce_error:
  1555. if (!pvt->is_registered)
  1556. i7core_udimm_check_mc_ecc_err(mci);
  1557. else
  1558. i7core_rdimm_check_mc_ecc_err(mci);
  1559. }
  1560. /*
  1561. * i7core_mce_check_error Replicates mcelog routine to get errors
  1562. * This routine simply queues mcelog errors, and
  1563. * return. The error itself should be handled later
  1564. * by i7core_check_error.
  1565. * WARNING: As this routine should be called at NMI time, extra care should
  1566. * be taken to avoid deadlocks, and to be as fast as possible.
  1567. */
  1568. static int i7core_mce_check_error(struct notifier_block *nb, unsigned long val,
  1569. void *data)
  1570. {
  1571. struct mce *mce = (struct mce *)data;
  1572. struct i7core_dev *i7_dev;
  1573. struct mem_ctl_info *mci;
  1574. struct i7core_pvt *pvt;
  1575. i7_dev = get_i7core_dev(mce->socketid);
  1576. if (!i7_dev)
  1577. return NOTIFY_DONE;
  1578. mci = i7_dev->mci;
  1579. pvt = mci->pvt_info;
  1580. /*
  1581. * Just let mcelog handle it if the error is
  1582. * outside the memory controller
  1583. */
  1584. if (((mce->status & 0xffff) >> 7) != 1)
  1585. return NOTIFY_DONE;
  1586. /* Bank 8 registers are the only ones that we know how to handle */
  1587. if (mce->bank != 8)
  1588. return NOTIFY_DONE;
  1589. smp_rmb();
  1590. if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
  1591. smp_wmb();
  1592. pvt->mce_overrun++;
  1593. return NOTIFY_DONE;
  1594. }
  1595. /* Copy memory error at the ringbuffer */
  1596. memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
  1597. smp_wmb();
  1598. pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
  1599. /* Handle fatal errors immediately */
  1600. if (mce->mcgstatus & 1)
  1601. i7core_check_error(mci);
  1602. /* Advise mcelog that the errors were handled */
  1603. return NOTIFY_STOP;
  1604. }
  1605. static struct notifier_block i7_mce_dec = {
  1606. .notifier_call = i7core_mce_check_error,
  1607. };
  1608. struct memdev_dmi_entry {
  1609. u8 type;
  1610. u8 length;
  1611. u16 handle;
  1612. u16 phys_mem_array_handle;
  1613. u16 mem_err_info_handle;
  1614. u16 total_width;
  1615. u16 data_width;
  1616. u16 size;
  1617. u8 form;
  1618. u8 device_set;
  1619. u8 device_locator;
  1620. u8 bank_locator;
  1621. u8 memory_type;
  1622. u16 type_detail;
  1623. u16 speed;
  1624. u8 manufacturer;
  1625. u8 serial_number;
  1626. u8 asset_tag;
  1627. u8 part_number;
  1628. u8 attributes;
  1629. u32 extended_size;
  1630. u16 conf_mem_clk_speed;
  1631. } __attribute__((__packed__));
  1632. /*
  1633. * Decode the DRAM Clock Frequency, be paranoid, make sure that all
  1634. * memory devices show the same speed, and if they don't then consider
  1635. * all speeds to be invalid.
  1636. */
  1637. static void decode_dclk(const struct dmi_header *dh, void *_dclk_freq)
  1638. {
  1639. int *dclk_freq = _dclk_freq;
  1640. u16 dmi_mem_clk_speed;
  1641. if (*dclk_freq == -1)
  1642. return;
  1643. if (dh->type == DMI_ENTRY_MEM_DEVICE) {
  1644. struct memdev_dmi_entry *memdev_dmi_entry =
  1645. (struct memdev_dmi_entry *)dh;
  1646. unsigned long conf_mem_clk_speed_offset =
  1647. (unsigned long)&memdev_dmi_entry->conf_mem_clk_speed -
  1648. (unsigned long)&memdev_dmi_entry->type;
  1649. unsigned long speed_offset =
  1650. (unsigned long)&memdev_dmi_entry->speed -
  1651. (unsigned long)&memdev_dmi_entry->type;
  1652. /* Check that a DIMM is present */
  1653. if (memdev_dmi_entry->size == 0)
  1654. return;
  1655. /*
  1656. * Pick the configured speed if it's available, otherwise
  1657. * pick the DIMM speed, or we don't have a speed.
  1658. */
  1659. if (memdev_dmi_entry->length > conf_mem_clk_speed_offset) {
  1660. dmi_mem_clk_speed =
  1661. memdev_dmi_entry->conf_mem_clk_speed;
  1662. } else if (memdev_dmi_entry->length > speed_offset) {
  1663. dmi_mem_clk_speed = memdev_dmi_entry->speed;
  1664. } else {
  1665. *dclk_freq = -1;
  1666. return;
  1667. }
  1668. if (*dclk_freq == 0) {
  1669. /* First pass, speed was 0 */
  1670. if (dmi_mem_clk_speed > 0) {
  1671. /* Set speed if a valid speed is read */
  1672. *dclk_freq = dmi_mem_clk_speed;
  1673. } else {
  1674. /* Otherwise we don't have a valid speed */
  1675. *dclk_freq = -1;
  1676. }
  1677. } else if (*dclk_freq > 0 &&
  1678. *dclk_freq != dmi_mem_clk_speed) {
  1679. /*
  1680. * If we have a speed, check that all DIMMS are the same
  1681. * speed, otherwise set the speed as invalid.
  1682. */
  1683. *dclk_freq = -1;
  1684. }
  1685. }
  1686. }
  1687. /*
  1688. * The default DCLK frequency is used as a fallback if we
  1689. * fail to find anything reliable in the DMI. The value
  1690. * is taken straight from the datasheet.
  1691. */
  1692. #define DEFAULT_DCLK_FREQ 800
  1693. static int get_dclk_freq(void)
  1694. {
  1695. int dclk_freq = 0;
  1696. dmi_walk(decode_dclk, (void *)&dclk_freq);
  1697. if (dclk_freq < 1)
  1698. return DEFAULT_DCLK_FREQ;
  1699. return dclk_freq;
  1700. }
  1701. /*
  1702. * set_sdram_scrub_rate This routine sets byte/sec bandwidth scrub rate
  1703. * to hardware according to SCRUBINTERVAL formula
  1704. * found in datasheet.
  1705. */
  1706. static int set_sdram_scrub_rate(struct mem_ctl_info *mci, u32 new_bw)
  1707. {
  1708. struct i7core_pvt *pvt = mci->pvt_info;
  1709. struct pci_dev *pdev;
  1710. u32 dw_scrub;
  1711. u32 dw_ssr;
  1712. /* Get data from the MC register, function 2 */
  1713. pdev = pvt->pci_mcr[2];
  1714. if (!pdev)
  1715. return -ENODEV;
  1716. pci_read_config_dword(pdev, MC_SCRUB_CONTROL, &dw_scrub);
  1717. if (new_bw == 0) {
  1718. /* Prepare to disable petrol scrub */
  1719. dw_scrub &= ~STARTSCRUB;
  1720. /* Stop the patrol scrub engine */
  1721. write_and_test(pdev, MC_SCRUB_CONTROL,
  1722. dw_scrub & ~SCRUBINTERVAL_MASK);
  1723. /* Get current status of scrub rate and set bit to disable */
  1724. pci_read_config_dword(pdev, MC_SSRCONTROL, &dw_ssr);
  1725. dw_ssr &= ~SSR_MODE_MASK;
  1726. dw_ssr |= SSR_MODE_DISABLE;
  1727. } else {
  1728. const int cache_line_size = 64;
  1729. const u32 freq_dclk_mhz = pvt->dclk_freq;
  1730. unsigned long long scrub_interval;
  1731. /*
  1732. * Translate the desired scrub rate to a register value and
  1733. * program the corresponding register value.
  1734. */
  1735. scrub_interval = (unsigned long long)freq_dclk_mhz *
  1736. cache_line_size * 1000000;
  1737. do_div(scrub_interval, new_bw);
  1738. if (!scrub_interval || scrub_interval > SCRUBINTERVAL_MASK)
  1739. return -EINVAL;
  1740. dw_scrub = SCRUBINTERVAL_MASK & scrub_interval;
  1741. /* Start the patrol scrub engine */
  1742. pci_write_config_dword(pdev, MC_SCRUB_CONTROL,
  1743. STARTSCRUB | dw_scrub);
  1744. /* Get current status of scrub rate and set bit to enable */
  1745. pci_read_config_dword(pdev, MC_SSRCONTROL, &dw_ssr);
  1746. dw_ssr &= ~SSR_MODE_MASK;
  1747. dw_ssr |= SSR_MODE_ENABLE;
  1748. }
  1749. /* Disable or enable scrubbing */
  1750. pci_write_config_dword(pdev, MC_SSRCONTROL, dw_ssr);
  1751. return new_bw;
  1752. }
  1753. /*
  1754. * get_sdram_scrub_rate This routine convert current scrub rate value
  1755. * into byte/sec bandwidth according to
  1756. * SCRUBINTERVAL formula found in datasheet.
  1757. */
  1758. static int get_sdram_scrub_rate(struct mem_ctl_info *mci)
  1759. {
  1760. struct i7core_pvt *pvt = mci->pvt_info;
  1761. struct pci_dev *pdev;
  1762. const u32 cache_line_size = 64;
  1763. const u32 freq_dclk_mhz = pvt->dclk_freq;
  1764. unsigned long long scrub_rate;
  1765. u32 scrubval;
  1766. /* Get data from the MC register, function 2 */
  1767. pdev = pvt->pci_mcr[2];
  1768. if (!pdev)
  1769. return -ENODEV;
  1770. /* Get current scrub control data */
  1771. pci_read_config_dword(pdev, MC_SCRUB_CONTROL, &scrubval);
  1772. /* Mask highest 8-bits to 0 */
  1773. scrubval &= SCRUBINTERVAL_MASK;
  1774. if (!scrubval)
  1775. return 0;
  1776. /* Calculate scrub rate value into byte/sec bandwidth */
  1777. scrub_rate = (unsigned long long)freq_dclk_mhz *
  1778. 1000000 * cache_line_size;
  1779. do_div(scrub_rate, scrubval);
  1780. return (int)scrub_rate;
  1781. }
  1782. static void enable_sdram_scrub_setting(struct mem_ctl_info *mci)
  1783. {
  1784. struct i7core_pvt *pvt = mci->pvt_info;
  1785. u32 pci_lock;
  1786. /* Unlock writes to pci registers */
  1787. pci_read_config_dword(pvt->pci_noncore, MC_CFG_CONTROL, &pci_lock);
  1788. pci_lock &= ~0x3;
  1789. pci_write_config_dword(pvt->pci_noncore, MC_CFG_CONTROL,
  1790. pci_lock | MC_CFG_UNLOCK);
  1791. mci->set_sdram_scrub_rate = set_sdram_scrub_rate;
  1792. mci->get_sdram_scrub_rate = get_sdram_scrub_rate;
  1793. }
  1794. static void disable_sdram_scrub_setting(struct mem_ctl_info *mci)
  1795. {
  1796. struct i7core_pvt *pvt = mci->pvt_info;
  1797. u32 pci_lock;
  1798. /* Lock writes to pci registers */
  1799. pci_read_config_dword(pvt->pci_noncore, MC_CFG_CONTROL, &pci_lock);
  1800. pci_lock &= ~0x3;
  1801. pci_write_config_dword(pvt->pci_noncore, MC_CFG_CONTROL,
  1802. pci_lock | MC_CFG_LOCK);
  1803. }
  1804. static void i7core_pci_ctl_create(struct i7core_pvt *pvt)
  1805. {
  1806. pvt->i7core_pci = edac_pci_create_generic_ctl(
  1807. &pvt->i7core_dev->pdev[0]->dev,
  1808. EDAC_MOD_STR);
  1809. if (unlikely(!pvt->i7core_pci))
  1810. i7core_printk(KERN_WARNING,
  1811. "Unable to setup PCI error report via EDAC\n");
  1812. }
  1813. static void i7core_pci_ctl_release(struct i7core_pvt *pvt)
  1814. {
  1815. if (likely(pvt->i7core_pci))
  1816. edac_pci_release_generic_ctl(pvt->i7core_pci);
  1817. else
  1818. i7core_printk(KERN_ERR,
  1819. "Couldn't find mem_ctl_info for socket %d\n",
  1820. pvt->i7core_dev->socket);
  1821. pvt->i7core_pci = NULL;
  1822. }
  1823. static void i7core_unregister_mci(struct i7core_dev *i7core_dev)
  1824. {
  1825. struct mem_ctl_info *mci = i7core_dev->mci;
  1826. struct i7core_pvt *pvt;
  1827. if (unlikely(!mci || !mci->pvt_info)) {
  1828. edac_dbg(0, "MC: dev = %p\n", &i7core_dev->pdev[0]->dev);
  1829. i7core_printk(KERN_ERR, "Couldn't find mci handler\n");
  1830. return;
  1831. }
  1832. pvt = mci->pvt_info;
  1833. edac_dbg(0, "MC: mci = %p, dev = %p\n", mci, &i7core_dev->pdev[0]->dev);
  1834. /* Disable scrubrate setting */
  1835. if (pvt->enable_scrub)
  1836. disable_sdram_scrub_setting(mci);
  1837. /* Disable EDAC polling */
  1838. i7core_pci_ctl_release(pvt);
  1839. /* Remove MC sysfs nodes */
  1840. i7core_delete_sysfs_devices(mci);
  1841. edac_mc_del_mc(mci->pdev);
  1842. edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
  1843. kfree(mci->ctl_name);
  1844. edac_mc_free(mci);
  1845. i7core_dev->mci = NULL;
  1846. }
  1847. static int i7core_register_mci(struct i7core_dev *i7core_dev)
  1848. {
  1849. struct mem_ctl_info *mci;
  1850. struct i7core_pvt *pvt;
  1851. int rc;
  1852. struct edac_mc_layer layers[2];
  1853. /* allocate a new MC control structure */
  1854. layers[0].type = EDAC_MC_LAYER_CHANNEL;
  1855. layers[0].size = NUM_CHANS;
  1856. layers[0].is_virt_csrow = false;
  1857. layers[1].type = EDAC_MC_LAYER_SLOT;
  1858. layers[1].size = MAX_DIMMS;
  1859. layers[1].is_virt_csrow = true;
  1860. mci = edac_mc_alloc(i7core_dev->socket, ARRAY_SIZE(layers), layers,
  1861. sizeof(*pvt));
  1862. if (unlikely(!mci))
  1863. return -ENOMEM;
  1864. edac_dbg(0, "MC: mci = %p, dev = %p\n", mci, &i7core_dev->pdev[0]->dev);
  1865. pvt = mci->pvt_info;
  1866. memset(pvt, 0, sizeof(*pvt));
  1867. /* Associates i7core_dev and mci for future usage */
  1868. pvt->i7core_dev = i7core_dev;
  1869. i7core_dev->mci = mci;
  1870. /*
  1871. * FIXME: how to handle RDDR3 at MCI level? It is possible to have
  1872. * Mixed RDDR3/UDDR3 with Nehalem, provided that they are on different
  1873. * memory channels
  1874. */
  1875. mci->mtype_cap = MEM_FLAG_DDR3;
  1876. mci->edac_ctl_cap = EDAC_FLAG_NONE;
  1877. mci->edac_cap = EDAC_FLAG_NONE;
  1878. mci->mod_name = "i7core_edac.c";
  1879. mci->mod_ver = I7CORE_REVISION;
  1880. mci->ctl_name = kasprintf(GFP_KERNEL, "i7 core #%d",
  1881. i7core_dev->socket);
  1882. mci->dev_name = pci_name(i7core_dev->pdev[0]);
  1883. mci->ctl_page_to_phys = NULL;
  1884. /* Store pci devices at mci for faster access */
  1885. rc = mci_bind_devs(mci, i7core_dev);
  1886. if (unlikely(rc < 0))
  1887. goto fail0;
  1888. /* Get dimm basic config */
  1889. get_dimm_config(mci);
  1890. /* record ptr to the generic device */
  1891. mci->pdev = &i7core_dev->pdev[0]->dev;
  1892. /* Set the function pointer to an actual operation function */
  1893. mci->edac_check = i7core_check_error;
  1894. /* Enable scrubrate setting */
  1895. if (pvt->enable_scrub)
  1896. enable_sdram_scrub_setting(mci);
  1897. /* add this new MC control structure to EDAC's list of MCs */
  1898. if (unlikely(edac_mc_add_mc_with_groups(mci, i7core_dev_groups))) {
  1899. edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
  1900. /* FIXME: perhaps some code should go here that disables error
  1901. * reporting if we just enabled it
  1902. */
  1903. rc = -EINVAL;
  1904. goto fail0;
  1905. }
  1906. if (i7core_create_sysfs_devices(mci)) {
  1907. edac_dbg(0, "MC: failed to create sysfs nodes\n");
  1908. edac_mc_del_mc(mci->pdev);
  1909. rc = -EINVAL;
  1910. goto fail0;
  1911. }
  1912. /* Default error mask is any memory */
  1913. pvt->inject.channel = 0;
  1914. pvt->inject.dimm = -1;
  1915. pvt->inject.rank = -1;
  1916. pvt->inject.bank = -1;
  1917. pvt->inject.page = -1;
  1918. pvt->inject.col = -1;
  1919. /* allocating generic PCI control info */
  1920. i7core_pci_ctl_create(pvt);
  1921. /* DCLK for scrub rate setting */
  1922. pvt->dclk_freq = get_dclk_freq();
  1923. return 0;
  1924. fail0:
  1925. kfree(mci->ctl_name);
  1926. edac_mc_free(mci);
  1927. i7core_dev->mci = NULL;
  1928. return rc;
  1929. }
  1930. /*
  1931. * i7core_probe Probe for ONE instance of device to see if it is
  1932. * present.
  1933. * return:
  1934. * 0 for FOUND a device
  1935. * < 0 for error code
  1936. */
  1937. static int i7core_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1938. {
  1939. int rc, count = 0;
  1940. struct i7core_dev *i7core_dev;
  1941. /* get the pci devices we want to reserve for our use */
  1942. mutex_lock(&i7core_edac_lock);
  1943. /*
  1944. * All memory controllers are allocated at the first pass.
  1945. */
  1946. if (unlikely(probed >= 1)) {
  1947. mutex_unlock(&i7core_edac_lock);
  1948. return -ENODEV;
  1949. }
  1950. probed++;
  1951. rc = i7core_get_all_devices();
  1952. if (unlikely(rc < 0))
  1953. goto fail0;
  1954. list_for_each_entry(i7core_dev, &i7core_edac_list, list) {
  1955. count++;
  1956. rc = i7core_register_mci(i7core_dev);
  1957. if (unlikely(rc < 0))
  1958. goto fail1;
  1959. }
  1960. /*
  1961. * Nehalem-EX uses a different memory controller. However, as the
  1962. * memory controller is not visible on some Nehalem/Nehalem-EP, we
  1963. * need to indirectly probe via a X58 PCI device. The same devices
  1964. * are found on (some) Nehalem-EX. So, on those machines, the
  1965. * probe routine needs to return -ENODEV, as the actual Memory
  1966. * Controller registers won't be detected.
  1967. */
  1968. if (!count) {
  1969. rc = -ENODEV;
  1970. goto fail1;
  1971. }
  1972. i7core_printk(KERN_INFO,
  1973. "Driver loaded, %d memory controller(s) found.\n",
  1974. count);
  1975. mutex_unlock(&i7core_edac_lock);
  1976. return 0;
  1977. fail1:
  1978. list_for_each_entry(i7core_dev, &i7core_edac_list, list)
  1979. i7core_unregister_mci(i7core_dev);
  1980. i7core_put_all_devices();
  1981. fail0:
  1982. mutex_unlock(&i7core_edac_lock);
  1983. return rc;
  1984. }
  1985. /*
  1986. * i7core_remove destructor for one instance of device
  1987. *
  1988. */
  1989. static void i7core_remove(struct pci_dev *pdev)
  1990. {
  1991. struct i7core_dev *i7core_dev;
  1992. edac_dbg(0, "\n");
  1993. /*
  1994. * we have a trouble here: pdev value for removal will be wrong, since
  1995. * it will point to the X58 register used to detect that the machine
  1996. * is a Nehalem or upper design. However, due to the way several PCI
  1997. * devices are grouped together to provide MC functionality, we need
  1998. * to use a different method for releasing the devices
  1999. */
  2000. mutex_lock(&i7core_edac_lock);
  2001. if (unlikely(!probed)) {
  2002. mutex_unlock(&i7core_edac_lock);
  2003. return;
  2004. }
  2005. list_for_each_entry(i7core_dev, &i7core_edac_list, list)
  2006. i7core_unregister_mci(i7core_dev);
  2007. /* Release PCI resources */
  2008. i7core_put_all_devices();
  2009. probed--;
  2010. mutex_unlock(&i7core_edac_lock);
  2011. }
  2012. MODULE_DEVICE_TABLE(pci, i7core_pci_tbl);
  2013. /*
  2014. * i7core_driver pci_driver structure for this module
  2015. *
  2016. */
  2017. static struct pci_driver i7core_driver = {
  2018. .name = "i7core_edac",
  2019. .probe = i7core_probe,
  2020. .remove = i7core_remove,
  2021. .id_table = i7core_pci_tbl,
  2022. };
  2023. /*
  2024. * i7core_init Module entry function
  2025. * Try to initialize this module for its devices
  2026. */
  2027. static int __init i7core_init(void)
  2028. {
  2029. int pci_rc;
  2030. edac_dbg(2, "\n");
  2031. /* Ensure that the OPSTATE is set correctly for POLL or NMI */
  2032. opstate_init();
  2033. if (use_pci_fixup)
  2034. i7core_xeon_pci_fixup(pci_dev_table);
  2035. pci_rc = pci_register_driver(&i7core_driver);
  2036. if (pci_rc >= 0) {
  2037. mce_register_decode_chain(&i7_mce_dec);
  2038. return 0;
  2039. }
  2040. i7core_printk(KERN_ERR, "Failed to register device with error %d.\n",
  2041. pci_rc);
  2042. return pci_rc;
  2043. }
  2044. /*
  2045. * i7core_exit() Module exit function
  2046. * Unregister the driver
  2047. */
  2048. static void __exit i7core_exit(void)
  2049. {
  2050. edac_dbg(2, "\n");
  2051. pci_unregister_driver(&i7core_driver);
  2052. mce_unregister_decode_chain(&i7_mce_dec);
  2053. }
  2054. module_init(i7core_init);
  2055. module_exit(i7core_exit);
  2056. MODULE_LICENSE("GPL");
  2057. MODULE_AUTHOR("Mauro Carvalho Chehab");
  2058. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  2059. MODULE_DESCRIPTION("MC Driver for Intel i7 Core memory controllers - "
  2060. I7CORE_REVISION);
  2061. module_param(edac_op_state, int, 0444);
  2062. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");