i7core_edac.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175
  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 <mchehab@redhat.com>
  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/edac.h>
  34. #include <linux/mmzone.h>
  35. #include <linux/edac_mce.h>
  36. #include <linux/smp.h>
  37. #include <asm/processor.h>
  38. #include "edac_core.h"
  39. /* Static vars */
  40. static LIST_HEAD(i7core_edac_list);
  41. static DEFINE_MUTEX(i7core_edac_lock);
  42. static int probed;
  43. static int use_pci_fixup;
  44. module_param(use_pci_fixup, int, 0444);
  45. MODULE_PARM_DESC(use_pci_fixup, "Enable PCI fixup to seek for hidden devices");
  46. /*
  47. * This is used for Nehalem-EP and Nehalem-EX devices, where the non-core
  48. * registers start at bus 255, and are not reported by BIOS.
  49. * We currently find devices with only 2 sockets. In order to support more QPI
  50. * Quick Path Interconnect, just increment this number.
  51. */
  52. #define MAX_SOCKET_BUSES 2
  53. /*
  54. * Alter this version for the module when modifications are made
  55. */
  56. #define I7CORE_REVISION " Ver: 1.0.0"
  57. #define EDAC_MOD_STR "i7core_edac"
  58. /*
  59. * Debug macros
  60. */
  61. #define i7core_printk(level, fmt, arg...) \
  62. edac_printk(level, "i7core", fmt, ##arg)
  63. #define i7core_mc_printk(mci, level, fmt, arg...) \
  64. edac_mc_chipset_printk(mci, level, "i7core", fmt, ##arg)
  65. /*
  66. * i7core Memory Controller Registers
  67. */
  68. /* OFFSETS for Device 0 Function 0 */
  69. #define MC_CFG_CONTROL 0x90
  70. /* OFFSETS for Device 3 Function 0 */
  71. #define MC_CONTROL 0x48
  72. #define MC_STATUS 0x4c
  73. #define MC_MAX_DOD 0x64
  74. /*
  75. * OFFSETS for Device 3 Function 4, as inicated on Xeon 5500 datasheet:
  76. * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
  77. */
  78. #define MC_TEST_ERR_RCV1 0x60
  79. #define DIMM2_COR_ERR(r) ((r) & 0x7fff)
  80. #define MC_TEST_ERR_RCV0 0x64
  81. #define DIMM1_COR_ERR(r) (((r) >> 16) & 0x7fff)
  82. #define DIMM0_COR_ERR(r) ((r) & 0x7fff)
  83. /* OFFSETS for Device 3 Function 2, as inicated on Xeon 5500 datasheet */
  84. #define MC_COR_ECC_CNT_0 0x80
  85. #define MC_COR_ECC_CNT_1 0x84
  86. #define MC_COR_ECC_CNT_2 0x88
  87. #define MC_COR_ECC_CNT_3 0x8c
  88. #define MC_COR_ECC_CNT_4 0x90
  89. #define MC_COR_ECC_CNT_5 0x94
  90. #define DIMM_TOP_COR_ERR(r) (((r) >> 16) & 0x7fff)
  91. #define DIMM_BOT_COR_ERR(r) ((r) & 0x7fff)
  92. /* OFFSETS for Devices 4,5 and 6 Function 0 */
  93. #define MC_CHANNEL_DIMM_INIT_PARAMS 0x58
  94. #define THREE_DIMMS_PRESENT (1 << 24)
  95. #define SINGLE_QUAD_RANK_PRESENT (1 << 23)
  96. #define QUAD_RANK_PRESENT (1 << 22)
  97. #define REGISTERED_DIMM (1 << 15)
  98. #define MC_CHANNEL_MAPPER 0x60
  99. #define RDLCH(r, ch) ((((r) >> (3 + (ch * 6))) & 0x07) - 1)
  100. #define WRLCH(r, ch) ((((r) >> (ch * 6)) & 0x07) - 1)
  101. #define MC_CHANNEL_RANK_PRESENT 0x7c
  102. #define RANK_PRESENT_MASK 0xffff
  103. #define MC_CHANNEL_ADDR_MATCH 0xf0
  104. #define MC_CHANNEL_ERROR_MASK 0xf8
  105. #define MC_CHANNEL_ERROR_INJECT 0xfc
  106. #define INJECT_ADDR_PARITY 0x10
  107. #define INJECT_ECC 0x08
  108. #define MASK_CACHELINE 0x06
  109. #define MASK_FULL_CACHELINE 0x06
  110. #define MASK_MSB32_CACHELINE 0x04
  111. #define MASK_LSB32_CACHELINE 0x02
  112. #define NO_MASK_CACHELINE 0x00
  113. #define REPEAT_EN 0x01
  114. /* OFFSETS for Devices 4,5 and 6 Function 1 */
  115. #define MC_DOD_CH_DIMM0 0x48
  116. #define MC_DOD_CH_DIMM1 0x4c
  117. #define MC_DOD_CH_DIMM2 0x50
  118. #define RANKOFFSET_MASK ((1 << 12) | (1 << 11) | (1 << 10))
  119. #define RANKOFFSET(x) ((x & RANKOFFSET_MASK) >> 10)
  120. #define DIMM_PRESENT_MASK (1 << 9)
  121. #define DIMM_PRESENT(x) (((x) & DIMM_PRESENT_MASK) >> 9)
  122. #define MC_DOD_NUMBANK_MASK ((1 << 8) | (1 << 7))
  123. #define MC_DOD_NUMBANK(x) (((x) & MC_DOD_NUMBANK_MASK) >> 7)
  124. #define MC_DOD_NUMRANK_MASK ((1 << 6) | (1 << 5))
  125. #define MC_DOD_NUMRANK(x) (((x) & MC_DOD_NUMRANK_MASK) >> 5)
  126. #define MC_DOD_NUMROW_MASK ((1 << 4) | (1 << 3) | (1 << 2))
  127. #define MC_DOD_NUMROW(x) (((x) & MC_DOD_NUMROW_MASK) >> 2)
  128. #define MC_DOD_NUMCOL_MASK 3
  129. #define MC_DOD_NUMCOL(x) ((x) & MC_DOD_NUMCOL_MASK)
  130. #define MC_RANK_PRESENT 0x7c
  131. #define MC_SAG_CH_0 0x80
  132. #define MC_SAG_CH_1 0x84
  133. #define MC_SAG_CH_2 0x88
  134. #define MC_SAG_CH_3 0x8c
  135. #define MC_SAG_CH_4 0x90
  136. #define MC_SAG_CH_5 0x94
  137. #define MC_SAG_CH_6 0x98
  138. #define MC_SAG_CH_7 0x9c
  139. #define MC_RIR_LIMIT_CH_0 0x40
  140. #define MC_RIR_LIMIT_CH_1 0x44
  141. #define MC_RIR_LIMIT_CH_2 0x48
  142. #define MC_RIR_LIMIT_CH_3 0x4C
  143. #define MC_RIR_LIMIT_CH_4 0x50
  144. #define MC_RIR_LIMIT_CH_5 0x54
  145. #define MC_RIR_LIMIT_CH_6 0x58
  146. #define MC_RIR_LIMIT_CH_7 0x5C
  147. #define MC_RIR_LIMIT_MASK ((1 << 10) - 1)
  148. #define MC_RIR_WAY_CH 0x80
  149. #define MC_RIR_WAY_OFFSET_MASK (((1 << 14) - 1) & ~0x7)
  150. #define MC_RIR_WAY_RANK_MASK 0x7
  151. /*
  152. * i7core structs
  153. */
  154. #define NUM_CHANS 3
  155. #define MAX_DIMMS 3 /* Max DIMMS per channel */
  156. #define MAX_MCR_FUNC 4
  157. #define MAX_CHAN_FUNC 3
  158. struct i7core_info {
  159. u32 mc_control;
  160. u32 mc_status;
  161. u32 max_dod;
  162. u32 ch_map;
  163. };
  164. struct i7core_inject {
  165. int enable;
  166. u32 section;
  167. u32 type;
  168. u32 eccmask;
  169. /* Error address mask */
  170. int channel, dimm, rank, bank, page, col;
  171. };
  172. struct i7core_channel {
  173. u32 ranks;
  174. u32 dimms;
  175. };
  176. struct pci_id_descr {
  177. int dev;
  178. int func;
  179. int dev_id;
  180. int optional;
  181. };
  182. struct pci_id_table {
  183. const struct pci_id_descr *descr;
  184. int n_devs;
  185. };
  186. struct i7core_dev {
  187. struct list_head list;
  188. u8 socket;
  189. struct pci_dev **pdev;
  190. int n_devs;
  191. struct mem_ctl_info *mci;
  192. };
  193. struct i7core_pvt {
  194. struct pci_dev *pci_noncore;
  195. struct pci_dev *pci_mcr[MAX_MCR_FUNC + 1];
  196. struct pci_dev *pci_ch[NUM_CHANS][MAX_CHAN_FUNC + 1];
  197. struct i7core_dev *i7core_dev;
  198. struct i7core_info info;
  199. struct i7core_inject inject;
  200. struct i7core_channel channel[NUM_CHANS];
  201. int ce_count_available;
  202. int csrow_map[NUM_CHANS][MAX_DIMMS];
  203. /* ECC corrected errors counts per udimm */
  204. unsigned long udimm_ce_count[MAX_DIMMS];
  205. int udimm_last_ce_count[MAX_DIMMS];
  206. /* ECC corrected errors counts per rdimm */
  207. unsigned long rdimm_ce_count[NUM_CHANS][MAX_DIMMS];
  208. int rdimm_last_ce_count[NUM_CHANS][MAX_DIMMS];
  209. unsigned int is_registered;
  210. /* mcelog glue */
  211. struct edac_mce edac_mce;
  212. /* Fifo double buffers */
  213. struct mce mce_entry[MCE_LOG_LEN];
  214. struct mce mce_outentry[MCE_LOG_LEN];
  215. /* Fifo in/out counters */
  216. unsigned mce_in, mce_out;
  217. /* Count indicator to show errors not got */
  218. unsigned mce_overrun;
  219. /* Struct to control EDAC polling */
  220. struct edac_pci_ctl_info *i7core_pci;
  221. };
  222. #define PCI_DESCR(device, function, device_id) \
  223. .dev = (device), \
  224. .func = (function), \
  225. .dev_id = (device_id)
  226. static const struct pci_id_descr pci_dev_descr_i7core_nehalem[] = {
  227. /* Memory controller */
  228. { PCI_DESCR(3, 0, PCI_DEVICE_ID_INTEL_I7_MCR) },
  229. { PCI_DESCR(3, 1, PCI_DEVICE_ID_INTEL_I7_MC_TAD) },
  230. /* Exists only for RDIMM */
  231. { PCI_DESCR(3, 2, PCI_DEVICE_ID_INTEL_I7_MC_RAS), .optional = 1 },
  232. { PCI_DESCR(3, 4, PCI_DEVICE_ID_INTEL_I7_MC_TEST) },
  233. /* Channel 0 */
  234. { PCI_DESCR(4, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH0_CTRL) },
  235. { PCI_DESCR(4, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH0_ADDR) },
  236. { PCI_DESCR(4, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH0_RANK) },
  237. { PCI_DESCR(4, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH0_TC) },
  238. /* Channel 1 */
  239. { PCI_DESCR(5, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH1_CTRL) },
  240. { PCI_DESCR(5, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH1_ADDR) },
  241. { PCI_DESCR(5, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH1_RANK) },
  242. { PCI_DESCR(5, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH1_TC) },
  243. /* Channel 2 */
  244. { PCI_DESCR(6, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH2_CTRL) },
  245. { PCI_DESCR(6, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH2_ADDR) },
  246. { PCI_DESCR(6, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH2_RANK) },
  247. { PCI_DESCR(6, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH2_TC) },
  248. };
  249. static const struct pci_id_descr pci_dev_descr_lynnfield[] = {
  250. { PCI_DESCR( 3, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MCR) },
  251. { PCI_DESCR( 3, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TAD) },
  252. { PCI_DESCR( 3, 4, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TEST) },
  253. { PCI_DESCR( 4, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_CTRL) },
  254. { PCI_DESCR( 4, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_ADDR) },
  255. { PCI_DESCR( 4, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_RANK) },
  256. { PCI_DESCR( 4, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_TC) },
  257. { PCI_DESCR( 5, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_CTRL) },
  258. { PCI_DESCR( 5, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_ADDR) },
  259. { PCI_DESCR( 5, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_RANK) },
  260. { PCI_DESCR( 5, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_TC) },
  261. };
  262. static const struct pci_id_descr pci_dev_descr_i7core_westmere[] = {
  263. /* Memory controller */
  264. { PCI_DESCR(3, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MCR_REV2) },
  265. { PCI_DESCR(3, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TAD_REV2) },
  266. /* Exists only for RDIMM */
  267. { PCI_DESCR(3, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_RAS_REV2), .optional = 1 },
  268. { PCI_DESCR(3, 4, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TEST_REV2) },
  269. /* Channel 0 */
  270. { PCI_DESCR(4, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_CTRL_REV2) },
  271. { PCI_DESCR(4, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_ADDR_REV2) },
  272. { PCI_DESCR(4, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_RANK_REV2) },
  273. { PCI_DESCR(4, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_TC_REV2) },
  274. /* Channel 1 */
  275. { PCI_DESCR(5, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_CTRL_REV2) },
  276. { PCI_DESCR(5, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_ADDR_REV2) },
  277. { PCI_DESCR(5, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_RANK_REV2) },
  278. { PCI_DESCR(5, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_TC_REV2) },
  279. /* Channel 2 */
  280. { PCI_DESCR(6, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_CTRL_REV2) },
  281. { PCI_DESCR(6, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_ADDR_REV2) },
  282. { PCI_DESCR(6, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_RANK_REV2) },
  283. { PCI_DESCR(6, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_TC_REV2) },
  284. };
  285. #define PCI_ID_TABLE_ENTRY(A) { .descr=A, .n_devs = ARRAY_SIZE(A) }
  286. static const struct pci_id_table pci_dev_table[] = {
  287. PCI_ID_TABLE_ENTRY(pci_dev_descr_i7core_nehalem),
  288. PCI_ID_TABLE_ENTRY(pci_dev_descr_lynnfield),
  289. PCI_ID_TABLE_ENTRY(pci_dev_descr_i7core_westmere),
  290. {0,} /* 0 terminated list. */
  291. };
  292. /*
  293. * pci_device_id table for which devices we are looking for
  294. */
  295. static const struct pci_device_id i7core_pci_tbl[] __devinitdata = {
  296. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_X58_HUB_MGMT)},
  297. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNNFIELD_QPI_LINK0)},
  298. {0,} /* 0 terminated list. */
  299. };
  300. /****************************************************************************
  301. Anciliary status routines
  302. ****************************************************************************/
  303. /* MC_CONTROL bits */
  304. #define CH_ACTIVE(pvt, ch) ((pvt)->info.mc_control & (1 << (8 + ch)))
  305. #define ECCx8(pvt) ((pvt)->info.mc_control & (1 << 1))
  306. /* MC_STATUS bits */
  307. #define ECC_ENABLED(pvt) ((pvt)->info.mc_status & (1 << 4))
  308. #define CH_DISABLED(pvt, ch) ((pvt)->info.mc_status & (1 << ch))
  309. /* MC_MAX_DOD read functions */
  310. static inline int numdimms(u32 dimms)
  311. {
  312. return (dimms & 0x3) + 1;
  313. }
  314. static inline int numrank(u32 rank)
  315. {
  316. static int ranks[4] = { 1, 2, 4, -EINVAL };
  317. return ranks[rank & 0x3];
  318. }
  319. static inline int numbank(u32 bank)
  320. {
  321. static int banks[4] = { 4, 8, 16, -EINVAL };
  322. return banks[bank & 0x3];
  323. }
  324. static inline int numrow(u32 row)
  325. {
  326. static int rows[8] = {
  327. 1 << 12, 1 << 13, 1 << 14, 1 << 15,
  328. 1 << 16, -EINVAL, -EINVAL, -EINVAL,
  329. };
  330. return rows[row & 0x7];
  331. }
  332. static inline int numcol(u32 col)
  333. {
  334. static int cols[8] = {
  335. 1 << 10, 1 << 11, 1 << 12, -EINVAL,
  336. };
  337. return cols[col & 0x3];
  338. }
  339. static struct i7core_dev *get_i7core_dev(u8 socket)
  340. {
  341. struct i7core_dev *i7core_dev;
  342. list_for_each_entry(i7core_dev, &i7core_edac_list, list) {
  343. if (i7core_dev->socket == socket)
  344. return i7core_dev;
  345. }
  346. return NULL;
  347. }
  348. static struct i7core_dev *alloc_i7core_dev(u8 socket,
  349. const struct pci_id_table *table)
  350. {
  351. struct i7core_dev *i7core_dev;
  352. i7core_dev = kzalloc(sizeof(*i7core_dev), GFP_KERNEL);
  353. if (!i7core_dev)
  354. return NULL;
  355. i7core_dev->pdev = kzalloc(sizeof(*i7core_dev->pdev) * table->n_devs,
  356. GFP_KERNEL);
  357. if (!i7core_dev->pdev) {
  358. kfree(i7core_dev);
  359. return NULL;
  360. }
  361. i7core_dev->socket = socket;
  362. i7core_dev->n_devs = table->n_devs;
  363. list_add_tail(&i7core_dev->list, &i7core_edac_list);
  364. return i7core_dev;
  365. }
  366. static void free_i7core_dev(struct i7core_dev *i7core_dev)
  367. {
  368. list_del(&i7core_dev->list);
  369. kfree(i7core_dev->pdev);
  370. kfree(i7core_dev);
  371. }
  372. /****************************************************************************
  373. Memory check routines
  374. ****************************************************************************/
  375. static struct pci_dev *get_pdev_slot_func(u8 socket, unsigned slot,
  376. unsigned func)
  377. {
  378. struct i7core_dev *i7core_dev = get_i7core_dev(socket);
  379. int i;
  380. if (!i7core_dev)
  381. return NULL;
  382. for (i = 0; i < i7core_dev->n_devs; i++) {
  383. if (!i7core_dev->pdev[i])
  384. continue;
  385. if (PCI_SLOT(i7core_dev->pdev[i]->devfn) == slot &&
  386. PCI_FUNC(i7core_dev->pdev[i]->devfn) == func) {
  387. return i7core_dev->pdev[i];
  388. }
  389. }
  390. return NULL;
  391. }
  392. /**
  393. * i7core_get_active_channels() - gets the number of channels and csrows
  394. * @socket: Quick Path Interconnect socket
  395. * @channels: Number of channels that will be returned
  396. * @csrows: Number of csrows found
  397. *
  398. * Since EDAC core needs to know in advance the number of available channels
  399. * and csrows, in order to allocate memory for csrows/channels, it is needed
  400. * to run two similar steps. At the first step, implemented on this function,
  401. * it checks the number of csrows/channels present at one socket.
  402. * this is used in order to properly allocate the size of mci components.
  403. *
  404. * It should be noticed that none of the current available datasheets explain
  405. * or even mention how csrows are seen by the memory controller. So, we need
  406. * to add a fake description for csrows.
  407. * So, this driver is attributing one DIMM memory for one csrow.
  408. */
  409. static int i7core_get_active_channels(const u8 socket, unsigned *channels,
  410. unsigned *csrows)
  411. {
  412. struct pci_dev *pdev = NULL;
  413. int i, j;
  414. u32 status, control;
  415. *channels = 0;
  416. *csrows = 0;
  417. pdev = get_pdev_slot_func(socket, 3, 0);
  418. if (!pdev) {
  419. i7core_printk(KERN_ERR, "Couldn't find socket %d fn 3.0!!!\n",
  420. socket);
  421. return -ENODEV;
  422. }
  423. /* Device 3 function 0 reads */
  424. pci_read_config_dword(pdev, MC_STATUS, &status);
  425. pci_read_config_dword(pdev, MC_CONTROL, &control);
  426. for (i = 0; i < NUM_CHANS; i++) {
  427. u32 dimm_dod[3];
  428. /* Check if the channel is active */
  429. if (!(control & (1 << (8 + i))))
  430. continue;
  431. /* Check if the channel is disabled */
  432. if (status & (1 << i))
  433. continue;
  434. pdev = get_pdev_slot_func(socket, i + 4, 1);
  435. if (!pdev) {
  436. i7core_printk(KERN_ERR, "Couldn't find socket %d "
  437. "fn %d.%d!!!\n",
  438. socket, i + 4, 1);
  439. return -ENODEV;
  440. }
  441. /* Devices 4-6 function 1 */
  442. pci_read_config_dword(pdev,
  443. MC_DOD_CH_DIMM0, &dimm_dod[0]);
  444. pci_read_config_dword(pdev,
  445. MC_DOD_CH_DIMM1, &dimm_dod[1]);
  446. pci_read_config_dword(pdev,
  447. MC_DOD_CH_DIMM2, &dimm_dod[2]);
  448. (*channels)++;
  449. for (j = 0; j < 3; j++) {
  450. if (!DIMM_PRESENT(dimm_dod[j]))
  451. continue;
  452. (*csrows)++;
  453. }
  454. }
  455. debugf0("Number of active channels on socket %d: %d\n",
  456. socket, *channels);
  457. return 0;
  458. }
  459. static int get_dimm_config(const struct mem_ctl_info *mci)
  460. {
  461. struct i7core_pvt *pvt = mci->pvt_info;
  462. struct csrow_info *csr;
  463. struct pci_dev *pdev;
  464. int i, j;
  465. int csrow = 0;
  466. unsigned long last_page = 0;
  467. enum edac_type mode;
  468. enum mem_type mtype;
  469. /* Get data from the MC register, function 0 */
  470. pdev = pvt->pci_mcr[0];
  471. if (!pdev)
  472. return -ENODEV;
  473. /* Device 3 function 0 reads */
  474. pci_read_config_dword(pdev, MC_CONTROL, &pvt->info.mc_control);
  475. pci_read_config_dword(pdev, MC_STATUS, &pvt->info.mc_status);
  476. pci_read_config_dword(pdev, MC_MAX_DOD, &pvt->info.max_dod);
  477. pci_read_config_dword(pdev, MC_CHANNEL_MAPPER, &pvt->info.ch_map);
  478. debugf0("QPI %d control=0x%08x status=0x%08x dod=0x%08x map=0x%08x\n",
  479. pvt->i7core_dev->socket, pvt->info.mc_control, pvt->info.mc_status,
  480. pvt->info.max_dod, pvt->info.ch_map);
  481. if (ECC_ENABLED(pvt)) {
  482. debugf0("ECC enabled with x%d SDCC\n", ECCx8(pvt) ? 8 : 4);
  483. if (ECCx8(pvt))
  484. mode = EDAC_S8ECD8ED;
  485. else
  486. mode = EDAC_S4ECD4ED;
  487. } else {
  488. debugf0("ECC disabled\n");
  489. mode = EDAC_NONE;
  490. }
  491. /* FIXME: need to handle the error codes */
  492. debugf0("DOD Max limits: DIMMS: %d, %d-ranked, %d-banked "
  493. "x%x x 0x%x\n",
  494. numdimms(pvt->info.max_dod),
  495. numrank(pvt->info.max_dod >> 2),
  496. numbank(pvt->info.max_dod >> 4),
  497. numrow(pvt->info.max_dod >> 6),
  498. numcol(pvt->info.max_dod >> 9));
  499. for (i = 0; i < NUM_CHANS; i++) {
  500. u32 data, dimm_dod[3], value[8];
  501. if (!pvt->pci_ch[i][0])
  502. continue;
  503. if (!CH_ACTIVE(pvt, i)) {
  504. debugf0("Channel %i is not active\n", i);
  505. continue;
  506. }
  507. if (CH_DISABLED(pvt, i)) {
  508. debugf0("Channel %i is disabled\n", i);
  509. continue;
  510. }
  511. /* Devices 4-6 function 0 */
  512. pci_read_config_dword(pvt->pci_ch[i][0],
  513. MC_CHANNEL_DIMM_INIT_PARAMS, &data);
  514. pvt->channel[i].ranks = (data & QUAD_RANK_PRESENT) ?
  515. 4 : 2;
  516. if (data & REGISTERED_DIMM)
  517. mtype = MEM_RDDR3;
  518. else
  519. mtype = MEM_DDR3;
  520. #if 0
  521. if (data & THREE_DIMMS_PRESENT)
  522. pvt->channel[i].dimms = 3;
  523. else if (data & SINGLE_QUAD_RANK_PRESENT)
  524. pvt->channel[i].dimms = 1;
  525. else
  526. pvt->channel[i].dimms = 2;
  527. #endif
  528. /* Devices 4-6 function 1 */
  529. pci_read_config_dword(pvt->pci_ch[i][1],
  530. MC_DOD_CH_DIMM0, &dimm_dod[0]);
  531. pci_read_config_dword(pvt->pci_ch[i][1],
  532. MC_DOD_CH_DIMM1, &dimm_dod[1]);
  533. pci_read_config_dword(pvt->pci_ch[i][1],
  534. MC_DOD_CH_DIMM2, &dimm_dod[2]);
  535. debugf0("Ch%d phy rd%d, wr%d (0x%08x): "
  536. "%d ranks, %cDIMMs\n",
  537. i,
  538. RDLCH(pvt->info.ch_map, i), WRLCH(pvt->info.ch_map, i),
  539. data,
  540. pvt->channel[i].ranks,
  541. (data & REGISTERED_DIMM) ? 'R' : 'U');
  542. for (j = 0; j < 3; j++) {
  543. u32 banks, ranks, rows, cols;
  544. u32 size, npages;
  545. if (!DIMM_PRESENT(dimm_dod[j]))
  546. continue;
  547. banks = numbank(MC_DOD_NUMBANK(dimm_dod[j]));
  548. ranks = numrank(MC_DOD_NUMRANK(dimm_dod[j]));
  549. rows = numrow(MC_DOD_NUMROW(dimm_dod[j]));
  550. cols = numcol(MC_DOD_NUMCOL(dimm_dod[j]));
  551. /* DDR3 has 8 I/O banks */
  552. size = (rows * cols * banks * ranks) >> (20 - 3);
  553. pvt->channel[i].dimms++;
  554. debugf0("\tdimm %d %d Mb offset: %x, "
  555. "bank: %d, rank: %d, row: %#x, col: %#x\n",
  556. j, size,
  557. RANKOFFSET(dimm_dod[j]),
  558. banks, ranks, rows, cols);
  559. npages = MiB_TO_PAGES(size);
  560. csr = &mci->csrows[csrow];
  561. csr->first_page = last_page + 1;
  562. last_page += npages;
  563. csr->last_page = last_page;
  564. csr->nr_pages = npages;
  565. csr->page_mask = 0;
  566. csr->grain = 8;
  567. csr->csrow_idx = csrow;
  568. csr->nr_channels = 1;
  569. csr->channels[0].chan_idx = i;
  570. csr->channels[0].ce_count = 0;
  571. pvt->csrow_map[i][j] = csrow;
  572. switch (banks) {
  573. case 4:
  574. csr->dtype = DEV_X4;
  575. break;
  576. case 8:
  577. csr->dtype = DEV_X8;
  578. break;
  579. case 16:
  580. csr->dtype = DEV_X16;
  581. break;
  582. default:
  583. csr->dtype = DEV_UNKNOWN;
  584. }
  585. csr->edac_mode = mode;
  586. csr->mtype = mtype;
  587. csrow++;
  588. }
  589. pci_read_config_dword(pdev, MC_SAG_CH_0, &value[0]);
  590. pci_read_config_dword(pdev, MC_SAG_CH_1, &value[1]);
  591. pci_read_config_dword(pdev, MC_SAG_CH_2, &value[2]);
  592. pci_read_config_dword(pdev, MC_SAG_CH_3, &value[3]);
  593. pci_read_config_dword(pdev, MC_SAG_CH_4, &value[4]);
  594. pci_read_config_dword(pdev, MC_SAG_CH_5, &value[5]);
  595. pci_read_config_dword(pdev, MC_SAG_CH_6, &value[6]);
  596. pci_read_config_dword(pdev, MC_SAG_CH_7, &value[7]);
  597. debugf1("\t[%i] DIVBY3\tREMOVED\tOFFSET\n", i);
  598. for (j = 0; j < 8; j++)
  599. debugf1("\t\t%#x\t%#x\t%#x\n",
  600. (value[j] >> 27) & 0x1,
  601. (value[j] >> 24) & 0x7,
  602. (value[j] && ((1 << 24) - 1)));
  603. }
  604. return 0;
  605. }
  606. /****************************************************************************
  607. Error insertion routines
  608. ****************************************************************************/
  609. /* The i7core has independent error injection features per channel.
  610. However, to have a simpler code, we don't allow enabling error injection
  611. on more than one channel.
  612. Also, since a change at an inject parameter will be applied only at enable,
  613. we're disabling error injection on all write calls to the sysfs nodes that
  614. controls the error code injection.
  615. */
  616. static int disable_inject(const struct mem_ctl_info *mci)
  617. {
  618. struct i7core_pvt *pvt = mci->pvt_info;
  619. pvt->inject.enable = 0;
  620. if (!pvt->pci_ch[pvt->inject.channel][0])
  621. return -ENODEV;
  622. pci_write_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  623. MC_CHANNEL_ERROR_INJECT, 0);
  624. return 0;
  625. }
  626. /*
  627. * i7core inject inject.section
  628. *
  629. * accept and store error injection inject.section value
  630. * bit 0 - refers to the lower 32-byte half cacheline
  631. * bit 1 - refers to the upper 32-byte half cacheline
  632. */
  633. static ssize_t i7core_inject_section_store(struct mem_ctl_info *mci,
  634. const char *data, size_t count)
  635. {
  636. struct i7core_pvt *pvt = mci->pvt_info;
  637. unsigned long value;
  638. int rc;
  639. if (pvt->inject.enable)
  640. disable_inject(mci);
  641. rc = strict_strtoul(data, 10, &value);
  642. if ((rc < 0) || (value > 3))
  643. return -EIO;
  644. pvt->inject.section = (u32) value;
  645. return count;
  646. }
  647. static ssize_t i7core_inject_section_show(struct mem_ctl_info *mci,
  648. char *data)
  649. {
  650. struct i7core_pvt *pvt = mci->pvt_info;
  651. return sprintf(data, "0x%08x\n", pvt->inject.section);
  652. }
  653. /*
  654. * i7core inject.type
  655. *
  656. * accept and store error injection inject.section value
  657. * bit 0 - repeat enable - Enable error repetition
  658. * bit 1 - inject ECC error
  659. * bit 2 - inject parity error
  660. */
  661. static ssize_t i7core_inject_type_store(struct mem_ctl_info *mci,
  662. const char *data, size_t count)
  663. {
  664. struct i7core_pvt *pvt = mci->pvt_info;
  665. unsigned long value;
  666. int rc;
  667. if (pvt->inject.enable)
  668. disable_inject(mci);
  669. rc = strict_strtoul(data, 10, &value);
  670. if ((rc < 0) || (value > 7))
  671. return -EIO;
  672. pvt->inject.type = (u32) value;
  673. return count;
  674. }
  675. static ssize_t i7core_inject_type_show(struct mem_ctl_info *mci,
  676. char *data)
  677. {
  678. struct i7core_pvt *pvt = mci->pvt_info;
  679. return sprintf(data, "0x%08x\n", pvt->inject.type);
  680. }
  681. /*
  682. * i7core_inject_inject.eccmask_store
  683. *
  684. * The type of error (UE/CE) will depend on the inject.eccmask value:
  685. * Any bits set to a 1 will flip the corresponding ECC bit
  686. * Correctable errors can be injected by flipping 1 bit or the bits within
  687. * a symbol pair (2 consecutive aligned 8-bit pairs - i.e. 7:0 and 15:8 or
  688. * 23:16 and 31:24). Flipping bits in two symbol pairs will cause an
  689. * uncorrectable error to be injected.
  690. */
  691. static ssize_t i7core_inject_eccmask_store(struct mem_ctl_info *mci,
  692. const char *data, size_t count)
  693. {
  694. struct i7core_pvt *pvt = mci->pvt_info;
  695. unsigned long value;
  696. int rc;
  697. if (pvt->inject.enable)
  698. disable_inject(mci);
  699. rc = strict_strtoul(data, 10, &value);
  700. if (rc < 0)
  701. return -EIO;
  702. pvt->inject.eccmask = (u32) value;
  703. return count;
  704. }
  705. static ssize_t i7core_inject_eccmask_show(struct mem_ctl_info *mci,
  706. char *data)
  707. {
  708. struct i7core_pvt *pvt = mci->pvt_info;
  709. return sprintf(data, "0x%08x\n", pvt->inject.eccmask);
  710. }
  711. /*
  712. * i7core_addrmatch
  713. *
  714. * The type of error (UE/CE) will depend on the inject.eccmask value:
  715. * Any bits set to a 1 will flip the corresponding ECC bit
  716. * Correctable errors can be injected by flipping 1 bit or the bits within
  717. * a symbol pair (2 consecutive aligned 8-bit pairs - i.e. 7:0 and 15:8 or
  718. * 23:16 and 31:24). Flipping bits in two symbol pairs will cause an
  719. * uncorrectable error to be injected.
  720. */
  721. #define DECLARE_ADDR_MATCH(param, limit) \
  722. static ssize_t i7core_inject_store_##param( \
  723. struct mem_ctl_info *mci, \
  724. const char *data, size_t count) \
  725. { \
  726. struct i7core_pvt *pvt; \
  727. long value; \
  728. int rc; \
  729. \
  730. debugf1("%s()\n", __func__); \
  731. pvt = mci->pvt_info; \
  732. \
  733. if (pvt->inject.enable) \
  734. disable_inject(mci); \
  735. \
  736. if (!strcasecmp(data, "any") || !strcasecmp(data, "any\n"))\
  737. value = -1; \
  738. else { \
  739. rc = strict_strtoul(data, 10, &value); \
  740. if ((rc < 0) || (value >= limit)) \
  741. return -EIO; \
  742. } \
  743. \
  744. pvt->inject.param = value; \
  745. \
  746. return count; \
  747. } \
  748. \
  749. static ssize_t i7core_inject_show_##param( \
  750. struct mem_ctl_info *mci, \
  751. char *data) \
  752. { \
  753. struct i7core_pvt *pvt; \
  754. \
  755. pvt = mci->pvt_info; \
  756. debugf1("%s() pvt=%p\n", __func__, pvt); \
  757. if (pvt->inject.param < 0) \
  758. return sprintf(data, "any\n"); \
  759. else \
  760. return sprintf(data, "%d\n", pvt->inject.param);\
  761. }
  762. #define ATTR_ADDR_MATCH(param) \
  763. { \
  764. .attr = { \
  765. .name = #param, \
  766. .mode = (S_IRUGO | S_IWUSR) \
  767. }, \
  768. .show = i7core_inject_show_##param, \
  769. .store = i7core_inject_store_##param, \
  770. }
  771. DECLARE_ADDR_MATCH(channel, 3);
  772. DECLARE_ADDR_MATCH(dimm, 3);
  773. DECLARE_ADDR_MATCH(rank, 4);
  774. DECLARE_ADDR_MATCH(bank, 32);
  775. DECLARE_ADDR_MATCH(page, 0x10000);
  776. DECLARE_ADDR_MATCH(col, 0x4000);
  777. static int write_and_test(struct pci_dev *dev, const int where, const u32 val)
  778. {
  779. u32 read;
  780. int count;
  781. debugf0("setting pci %02x:%02x.%x reg=%02x value=%08x\n",
  782. dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
  783. where, val);
  784. for (count = 0; count < 10; count++) {
  785. if (count)
  786. msleep(100);
  787. pci_write_config_dword(dev, where, val);
  788. pci_read_config_dword(dev, where, &read);
  789. if (read == val)
  790. return 0;
  791. }
  792. i7core_printk(KERN_ERR, "Error during set pci %02x:%02x.%x reg=%02x "
  793. "write=%08x. Read=%08x\n",
  794. dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
  795. where, val, read);
  796. return -EINVAL;
  797. }
  798. /*
  799. * This routine prepares the Memory Controller for error injection.
  800. * The error will be injected when some process tries to write to the
  801. * memory that matches the given criteria.
  802. * The criteria can be set in terms of a mask where dimm, rank, bank, page
  803. * and col can be specified.
  804. * A -1 value for any of the mask items will make the MCU to ignore
  805. * that matching criteria for error injection.
  806. *
  807. * It should be noticed that the error will only happen after a write operation
  808. * on a memory that matches the condition. if REPEAT_EN is not enabled at
  809. * inject mask, then it will produce just one error. Otherwise, it will repeat
  810. * until the injectmask would be cleaned.
  811. *
  812. * FIXME: This routine assumes that MAXNUMDIMMS value of MC_MAX_DOD
  813. * is reliable enough to check if the MC is using the
  814. * three channels. However, this is not clear at the datasheet.
  815. */
  816. static ssize_t i7core_inject_enable_store(struct mem_ctl_info *mci,
  817. const char *data, size_t count)
  818. {
  819. struct i7core_pvt *pvt = mci->pvt_info;
  820. u32 injectmask;
  821. u64 mask = 0;
  822. int rc;
  823. long enable;
  824. if (!pvt->pci_ch[pvt->inject.channel][0])
  825. return 0;
  826. rc = strict_strtoul(data, 10, &enable);
  827. if ((rc < 0))
  828. return 0;
  829. if (enable) {
  830. pvt->inject.enable = 1;
  831. } else {
  832. disable_inject(mci);
  833. return count;
  834. }
  835. /* Sets pvt->inject.dimm mask */
  836. if (pvt->inject.dimm < 0)
  837. mask |= 1LL << 41;
  838. else {
  839. if (pvt->channel[pvt->inject.channel].dimms > 2)
  840. mask |= (pvt->inject.dimm & 0x3LL) << 35;
  841. else
  842. mask |= (pvt->inject.dimm & 0x1LL) << 36;
  843. }
  844. /* Sets pvt->inject.rank mask */
  845. if (pvt->inject.rank < 0)
  846. mask |= 1LL << 40;
  847. else {
  848. if (pvt->channel[pvt->inject.channel].dimms > 2)
  849. mask |= (pvt->inject.rank & 0x1LL) << 34;
  850. else
  851. mask |= (pvt->inject.rank & 0x3LL) << 34;
  852. }
  853. /* Sets pvt->inject.bank mask */
  854. if (pvt->inject.bank < 0)
  855. mask |= 1LL << 39;
  856. else
  857. mask |= (pvt->inject.bank & 0x15LL) << 30;
  858. /* Sets pvt->inject.page mask */
  859. if (pvt->inject.page < 0)
  860. mask |= 1LL << 38;
  861. else
  862. mask |= (pvt->inject.page & 0xffff) << 14;
  863. /* Sets pvt->inject.column mask */
  864. if (pvt->inject.col < 0)
  865. mask |= 1LL << 37;
  866. else
  867. mask |= (pvt->inject.col & 0x3fff);
  868. /*
  869. * bit 0: REPEAT_EN
  870. * bits 1-2: MASK_HALF_CACHELINE
  871. * bit 3: INJECT_ECC
  872. * bit 4: INJECT_ADDR_PARITY
  873. */
  874. injectmask = (pvt->inject.type & 1) |
  875. (pvt->inject.section & 0x3) << 1 |
  876. (pvt->inject.type & 0x6) << (3 - 1);
  877. /* Unlock writes to registers - this register is write only */
  878. pci_write_config_dword(pvt->pci_noncore,
  879. MC_CFG_CONTROL, 0x2);
  880. write_and_test(pvt->pci_ch[pvt->inject.channel][0],
  881. MC_CHANNEL_ADDR_MATCH, mask);
  882. write_and_test(pvt->pci_ch[pvt->inject.channel][0],
  883. MC_CHANNEL_ADDR_MATCH + 4, mask >> 32L);
  884. write_and_test(pvt->pci_ch[pvt->inject.channel][0],
  885. MC_CHANNEL_ERROR_MASK, pvt->inject.eccmask);
  886. write_and_test(pvt->pci_ch[pvt->inject.channel][0],
  887. MC_CHANNEL_ERROR_INJECT, injectmask);
  888. /*
  889. * This is something undocumented, based on my tests
  890. * Without writing 8 to this register, errors aren't injected. Not sure
  891. * why.
  892. */
  893. pci_write_config_dword(pvt->pci_noncore,
  894. MC_CFG_CONTROL, 8);
  895. debugf0("Error inject addr match 0x%016llx, ecc 0x%08x,"
  896. " inject 0x%08x\n",
  897. mask, pvt->inject.eccmask, injectmask);
  898. return count;
  899. }
  900. static ssize_t i7core_inject_enable_show(struct mem_ctl_info *mci,
  901. char *data)
  902. {
  903. struct i7core_pvt *pvt = mci->pvt_info;
  904. u32 injectmask;
  905. if (!pvt->pci_ch[pvt->inject.channel][0])
  906. return 0;
  907. pci_read_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  908. MC_CHANNEL_ERROR_INJECT, &injectmask);
  909. debugf0("Inject error read: 0x%018x\n", injectmask);
  910. if (injectmask & 0x0c)
  911. pvt->inject.enable = 1;
  912. return sprintf(data, "%d\n", pvt->inject.enable);
  913. }
  914. #define DECLARE_COUNTER(param) \
  915. static ssize_t i7core_show_counter_##param( \
  916. struct mem_ctl_info *mci, \
  917. char *data) \
  918. { \
  919. struct i7core_pvt *pvt = mci->pvt_info; \
  920. \
  921. debugf1("%s() \n", __func__); \
  922. if (!pvt->ce_count_available || (pvt->is_registered)) \
  923. return sprintf(data, "data unavailable\n"); \
  924. return sprintf(data, "%lu\n", \
  925. pvt->udimm_ce_count[param]); \
  926. }
  927. #define ATTR_COUNTER(param) \
  928. { \
  929. .attr = { \
  930. .name = __stringify(udimm##param), \
  931. .mode = (S_IRUGO | S_IWUSR) \
  932. }, \
  933. .show = i7core_show_counter_##param \
  934. }
  935. DECLARE_COUNTER(0);
  936. DECLARE_COUNTER(1);
  937. DECLARE_COUNTER(2);
  938. /*
  939. * Sysfs struct
  940. */
  941. static const struct mcidev_sysfs_attribute i7core_addrmatch_attrs[] = {
  942. ATTR_ADDR_MATCH(channel),
  943. ATTR_ADDR_MATCH(dimm),
  944. ATTR_ADDR_MATCH(rank),
  945. ATTR_ADDR_MATCH(bank),
  946. ATTR_ADDR_MATCH(page),
  947. ATTR_ADDR_MATCH(col),
  948. { } /* End of list */
  949. };
  950. static const struct mcidev_sysfs_group i7core_inject_addrmatch = {
  951. .name = "inject_addrmatch",
  952. .mcidev_attr = i7core_addrmatch_attrs,
  953. };
  954. static const struct mcidev_sysfs_attribute i7core_udimm_counters_attrs[] = {
  955. ATTR_COUNTER(0),
  956. ATTR_COUNTER(1),
  957. ATTR_COUNTER(2),
  958. { .attr = { .name = NULL } }
  959. };
  960. static const struct mcidev_sysfs_group i7core_udimm_counters = {
  961. .name = "all_channel_counts",
  962. .mcidev_attr = i7core_udimm_counters_attrs,
  963. };
  964. static const struct mcidev_sysfs_attribute i7core_sysfs_rdimm_attrs[] = {
  965. {
  966. .attr = {
  967. .name = "inject_section",
  968. .mode = (S_IRUGO | S_IWUSR)
  969. },
  970. .show = i7core_inject_section_show,
  971. .store = i7core_inject_section_store,
  972. }, {
  973. .attr = {
  974. .name = "inject_type",
  975. .mode = (S_IRUGO | S_IWUSR)
  976. },
  977. .show = i7core_inject_type_show,
  978. .store = i7core_inject_type_store,
  979. }, {
  980. .attr = {
  981. .name = "inject_eccmask",
  982. .mode = (S_IRUGO | S_IWUSR)
  983. },
  984. .show = i7core_inject_eccmask_show,
  985. .store = i7core_inject_eccmask_store,
  986. }, {
  987. .grp = &i7core_inject_addrmatch,
  988. }, {
  989. .attr = {
  990. .name = "inject_enable",
  991. .mode = (S_IRUGO | S_IWUSR)
  992. },
  993. .show = i7core_inject_enable_show,
  994. .store = i7core_inject_enable_store,
  995. },
  996. { } /* End of list */
  997. };
  998. static const struct mcidev_sysfs_attribute i7core_sysfs_udimm_attrs[] = {
  999. {
  1000. .attr = {
  1001. .name = "inject_section",
  1002. .mode = (S_IRUGO | S_IWUSR)
  1003. },
  1004. .show = i7core_inject_section_show,
  1005. .store = i7core_inject_section_store,
  1006. }, {
  1007. .attr = {
  1008. .name = "inject_type",
  1009. .mode = (S_IRUGO | S_IWUSR)
  1010. },
  1011. .show = i7core_inject_type_show,
  1012. .store = i7core_inject_type_store,
  1013. }, {
  1014. .attr = {
  1015. .name = "inject_eccmask",
  1016. .mode = (S_IRUGO | S_IWUSR)
  1017. },
  1018. .show = i7core_inject_eccmask_show,
  1019. .store = i7core_inject_eccmask_store,
  1020. }, {
  1021. .grp = &i7core_inject_addrmatch,
  1022. }, {
  1023. .attr = {
  1024. .name = "inject_enable",
  1025. .mode = (S_IRUGO | S_IWUSR)
  1026. },
  1027. .show = i7core_inject_enable_show,
  1028. .store = i7core_inject_enable_store,
  1029. }, {
  1030. .grp = &i7core_udimm_counters,
  1031. },
  1032. { } /* End of list */
  1033. };
  1034. /****************************************************************************
  1035. Device initialization routines: put/get, init/exit
  1036. ****************************************************************************/
  1037. /*
  1038. * i7core_put_all_devices 'put' all the devices that we have
  1039. * reserved via 'get'
  1040. */
  1041. static void i7core_put_devices(struct i7core_dev *i7core_dev)
  1042. {
  1043. int i;
  1044. debugf0(__FILE__ ": %s()\n", __func__);
  1045. for (i = 0; i < i7core_dev->n_devs; i++) {
  1046. struct pci_dev *pdev = i7core_dev->pdev[i];
  1047. if (!pdev)
  1048. continue;
  1049. debugf0("Removing dev %02x:%02x.%d\n",
  1050. pdev->bus->number,
  1051. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  1052. pci_dev_put(pdev);
  1053. }
  1054. }
  1055. static void i7core_put_all_devices(void)
  1056. {
  1057. struct i7core_dev *i7core_dev, *tmp;
  1058. list_for_each_entry_safe(i7core_dev, tmp, &i7core_edac_list, list) {
  1059. i7core_put_devices(i7core_dev);
  1060. free_i7core_dev(i7core_dev);
  1061. }
  1062. }
  1063. static void __init i7core_xeon_pci_fixup(const struct pci_id_table *table)
  1064. {
  1065. struct pci_dev *pdev = NULL;
  1066. int i;
  1067. /*
  1068. * On Xeon 55xx, the Intel Quick Path Arch Generic Non-core pci buses
  1069. * aren't announced by acpi. So, we need to use a legacy scan probing
  1070. * to detect them
  1071. */
  1072. while (table && table->descr) {
  1073. pdev = pci_get_device(PCI_VENDOR_ID_INTEL, table->descr[0].dev_id, NULL);
  1074. if (unlikely(!pdev)) {
  1075. for (i = 0; i < MAX_SOCKET_BUSES; i++)
  1076. pcibios_scan_specific_bus(255-i);
  1077. }
  1078. pci_dev_put(pdev);
  1079. table++;
  1080. }
  1081. }
  1082. static unsigned i7core_pci_lastbus(void)
  1083. {
  1084. int last_bus = 0, bus;
  1085. struct pci_bus *b = NULL;
  1086. while ((b = pci_find_next_bus(b)) != NULL) {
  1087. bus = b->number;
  1088. debugf0("Found bus %d\n", bus);
  1089. if (bus > last_bus)
  1090. last_bus = bus;
  1091. }
  1092. debugf0("Last bus %d\n", last_bus);
  1093. return last_bus;
  1094. }
  1095. /*
  1096. * i7core_get_all_devices Find and perform 'get' operation on the MCH's
  1097. * device/functions we want to reference for this driver
  1098. *
  1099. * Need to 'get' device 16 func 1 and func 2
  1100. */
  1101. static int i7core_get_onedevice(struct pci_dev **prev,
  1102. const struct pci_id_table *table,
  1103. const unsigned devno,
  1104. const unsigned last_bus)
  1105. {
  1106. struct i7core_dev *i7core_dev;
  1107. const struct pci_id_descr *dev_descr = &table->descr[devno];
  1108. struct pci_dev *pdev = NULL;
  1109. u8 bus = 0;
  1110. u8 socket = 0;
  1111. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1112. dev_descr->dev_id, *prev);
  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. debugf0("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. pvt->is_registered = 0;
  1215. for (i = 0; i < i7core_dev->n_devs; i++) {
  1216. pdev = i7core_dev->pdev[i];
  1217. if (!pdev)
  1218. continue;
  1219. func = PCI_FUNC(pdev->devfn);
  1220. slot = PCI_SLOT(pdev->devfn);
  1221. if (slot == 3) {
  1222. if (unlikely(func > MAX_MCR_FUNC))
  1223. goto error;
  1224. pvt->pci_mcr[func] = pdev;
  1225. } else if (likely(slot >= 4 && slot < 4 + NUM_CHANS)) {
  1226. if (unlikely(func > MAX_CHAN_FUNC))
  1227. goto error;
  1228. pvt->pci_ch[slot - 4][func] = pdev;
  1229. } else if (!slot && !func)
  1230. pvt->pci_noncore = pdev;
  1231. else
  1232. goto error;
  1233. debugf0("Associated fn %d.%d, dev = %p, socket %d\n",
  1234. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1235. pdev, i7core_dev->socket);
  1236. if (PCI_SLOT(pdev->devfn) == 3 &&
  1237. PCI_FUNC(pdev->devfn) == 2)
  1238. pvt->is_registered = 1;
  1239. }
  1240. return 0;
  1241. error:
  1242. i7core_printk(KERN_ERR, "Device %d, function %d "
  1243. "is out of the expected range\n",
  1244. slot, func);
  1245. return -EINVAL;
  1246. }
  1247. /****************************************************************************
  1248. Error check routines
  1249. ****************************************************************************/
  1250. static void i7core_rdimm_update_csrow(struct mem_ctl_info *mci,
  1251. const int chan,
  1252. const int dimm,
  1253. const int add)
  1254. {
  1255. char *msg;
  1256. struct i7core_pvt *pvt = mci->pvt_info;
  1257. int row = pvt->csrow_map[chan][dimm], i;
  1258. for (i = 0; i < add; i++) {
  1259. msg = kasprintf(GFP_KERNEL, "Corrected error "
  1260. "(Socket=%d channel=%d dimm=%d)",
  1261. pvt->i7core_dev->socket, chan, dimm);
  1262. edac_mc_handle_fbd_ce(mci, row, 0, msg);
  1263. kfree (msg);
  1264. }
  1265. }
  1266. static void i7core_rdimm_update_ce_count(struct mem_ctl_info *mci,
  1267. const int chan,
  1268. const int new0,
  1269. const int new1,
  1270. const int new2)
  1271. {
  1272. struct i7core_pvt *pvt = mci->pvt_info;
  1273. int add0 = 0, add1 = 0, add2 = 0;
  1274. /* Updates CE counters if it is not the first time here */
  1275. if (pvt->ce_count_available) {
  1276. /* Updates CE counters */
  1277. add2 = new2 - pvt->rdimm_last_ce_count[chan][2];
  1278. add1 = new1 - pvt->rdimm_last_ce_count[chan][1];
  1279. add0 = new0 - pvt->rdimm_last_ce_count[chan][0];
  1280. if (add2 < 0)
  1281. add2 += 0x7fff;
  1282. pvt->rdimm_ce_count[chan][2] += add2;
  1283. if (add1 < 0)
  1284. add1 += 0x7fff;
  1285. pvt->rdimm_ce_count[chan][1] += add1;
  1286. if (add0 < 0)
  1287. add0 += 0x7fff;
  1288. pvt->rdimm_ce_count[chan][0] += add0;
  1289. } else
  1290. pvt->ce_count_available = 1;
  1291. /* Store the new values */
  1292. pvt->rdimm_last_ce_count[chan][2] = new2;
  1293. pvt->rdimm_last_ce_count[chan][1] = new1;
  1294. pvt->rdimm_last_ce_count[chan][0] = new0;
  1295. /*updated the edac core */
  1296. if (add0 != 0)
  1297. i7core_rdimm_update_csrow(mci, chan, 0, add0);
  1298. if (add1 != 0)
  1299. i7core_rdimm_update_csrow(mci, chan, 1, add1);
  1300. if (add2 != 0)
  1301. i7core_rdimm_update_csrow(mci, chan, 2, add2);
  1302. }
  1303. static void i7core_rdimm_check_mc_ecc_err(struct mem_ctl_info *mci)
  1304. {
  1305. struct i7core_pvt *pvt = mci->pvt_info;
  1306. u32 rcv[3][2];
  1307. int i, new0, new1, new2;
  1308. /*Read DEV 3: FUN 2: MC_COR_ECC_CNT regs directly*/
  1309. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_0,
  1310. &rcv[0][0]);
  1311. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_1,
  1312. &rcv[0][1]);
  1313. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_2,
  1314. &rcv[1][0]);
  1315. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_3,
  1316. &rcv[1][1]);
  1317. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_4,
  1318. &rcv[2][0]);
  1319. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_5,
  1320. &rcv[2][1]);
  1321. for (i = 0 ; i < 3; i++) {
  1322. debugf3("MC_COR_ECC_CNT%d = 0x%x; MC_COR_ECC_CNT%d = 0x%x\n",
  1323. (i * 2), rcv[i][0], (i * 2) + 1, rcv[i][1]);
  1324. /*if the channel has 3 dimms*/
  1325. if (pvt->channel[i].dimms > 2) {
  1326. new0 = DIMM_BOT_COR_ERR(rcv[i][0]);
  1327. new1 = DIMM_TOP_COR_ERR(rcv[i][0]);
  1328. new2 = DIMM_BOT_COR_ERR(rcv[i][1]);
  1329. } else {
  1330. new0 = DIMM_TOP_COR_ERR(rcv[i][0]) +
  1331. DIMM_BOT_COR_ERR(rcv[i][0]);
  1332. new1 = DIMM_TOP_COR_ERR(rcv[i][1]) +
  1333. DIMM_BOT_COR_ERR(rcv[i][1]);
  1334. new2 = 0;
  1335. }
  1336. i7core_rdimm_update_ce_count(mci, i, new0, new1, new2);
  1337. }
  1338. }
  1339. /* This function is based on the device 3 function 4 registers as described on:
  1340. * Intel Xeon Processor 5500 Series Datasheet Volume 2
  1341. * http://www.intel.com/Assets/PDF/datasheet/321322.pdf
  1342. * also available at:
  1343. * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
  1344. */
  1345. static void i7core_udimm_check_mc_ecc_err(struct mem_ctl_info *mci)
  1346. {
  1347. struct i7core_pvt *pvt = mci->pvt_info;
  1348. u32 rcv1, rcv0;
  1349. int new0, new1, new2;
  1350. if (!pvt->pci_mcr[4]) {
  1351. debugf0("%s MCR registers not found\n", __func__);
  1352. return;
  1353. }
  1354. /* Corrected test errors */
  1355. pci_read_config_dword(pvt->pci_mcr[4], MC_TEST_ERR_RCV1, &rcv1);
  1356. pci_read_config_dword(pvt->pci_mcr[4], MC_TEST_ERR_RCV0, &rcv0);
  1357. /* Store the new values */
  1358. new2 = DIMM2_COR_ERR(rcv1);
  1359. new1 = DIMM1_COR_ERR(rcv0);
  1360. new0 = DIMM0_COR_ERR(rcv0);
  1361. /* Updates CE counters if it is not the first time here */
  1362. if (pvt->ce_count_available) {
  1363. /* Updates CE counters */
  1364. int add0, add1, add2;
  1365. add2 = new2 - pvt->udimm_last_ce_count[2];
  1366. add1 = new1 - pvt->udimm_last_ce_count[1];
  1367. add0 = new0 - pvt->udimm_last_ce_count[0];
  1368. if (add2 < 0)
  1369. add2 += 0x7fff;
  1370. pvt->udimm_ce_count[2] += add2;
  1371. if (add1 < 0)
  1372. add1 += 0x7fff;
  1373. pvt->udimm_ce_count[1] += add1;
  1374. if (add0 < 0)
  1375. add0 += 0x7fff;
  1376. pvt->udimm_ce_count[0] += add0;
  1377. if (add0 | add1 | add2)
  1378. i7core_printk(KERN_ERR, "New Corrected error(s): "
  1379. "dimm0: +%d, dimm1: +%d, dimm2 +%d\n",
  1380. add0, add1, add2);
  1381. } else
  1382. pvt->ce_count_available = 1;
  1383. /* Store the new values */
  1384. pvt->udimm_last_ce_count[2] = new2;
  1385. pvt->udimm_last_ce_count[1] = new1;
  1386. pvt->udimm_last_ce_count[0] = new0;
  1387. }
  1388. /*
  1389. * According with tables E-11 and E-12 of chapter E.3.3 of Intel 64 and IA-32
  1390. * Architectures Software Developer’s Manual Volume 3B.
  1391. * Nehalem are defined as family 0x06, model 0x1a
  1392. *
  1393. * The MCA registers used here are the following ones:
  1394. * struct mce field MCA Register
  1395. * m->status MSR_IA32_MC8_STATUS
  1396. * m->addr MSR_IA32_MC8_ADDR
  1397. * m->misc MSR_IA32_MC8_MISC
  1398. * In the case of Nehalem, the error information is masked at .status and .misc
  1399. * fields
  1400. */
  1401. static void i7core_mce_output_error(struct mem_ctl_info *mci,
  1402. const struct mce *m)
  1403. {
  1404. struct i7core_pvt *pvt = mci->pvt_info;
  1405. char *type, *optype, *err, *msg;
  1406. unsigned long error = m->status & 0x1ff0000l;
  1407. u32 optypenum = (m->status >> 4) & 0x07;
  1408. u32 core_err_cnt = (m->status >> 38) & 0x7fff;
  1409. u32 dimm = (m->misc >> 16) & 0x3;
  1410. u32 channel = (m->misc >> 18) & 0x3;
  1411. u32 syndrome = m->misc >> 32;
  1412. u32 errnum = find_first_bit(&error, 32);
  1413. int csrow;
  1414. if (m->mcgstatus & 1)
  1415. type = "FATAL";
  1416. else
  1417. type = "NON_FATAL";
  1418. switch (optypenum) {
  1419. case 0:
  1420. optype = "generic undef request";
  1421. break;
  1422. case 1:
  1423. optype = "read error";
  1424. break;
  1425. case 2:
  1426. optype = "write error";
  1427. break;
  1428. case 3:
  1429. optype = "addr/cmd error";
  1430. break;
  1431. case 4:
  1432. optype = "scrubbing error";
  1433. break;
  1434. default:
  1435. optype = "reserved";
  1436. break;
  1437. }
  1438. switch (errnum) {
  1439. case 16:
  1440. err = "read ECC error";
  1441. break;
  1442. case 17:
  1443. err = "RAS ECC error";
  1444. break;
  1445. case 18:
  1446. err = "write parity error";
  1447. break;
  1448. case 19:
  1449. err = "redundacy loss";
  1450. break;
  1451. case 20:
  1452. err = "reserved";
  1453. break;
  1454. case 21:
  1455. err = "memory range error";
  1456. break;
  1457. case 22:
  1458. err = "RTID out of range";
  1459. break;
  1460. case 23:
  1461. err = "address parity error";
  1462. break;
  1463. case 24:
  1464. err = "byte enable parity error";
  1465. break;
  1466. default:
  1467. err = "unknown";
  1468. }
  1469. /* FIXME: should convert addr into bank and rank information */
  1470. msg = kasprintf(GFP_ATOMIC,
  1471. "%s (addr = 0x%08llx, cpu=%d, Dimm=%d, Channel=%d, "
  1472. "syndrome=0x%08x, count=%d, Err=%08llx:%08llx (%s: %s))\n",
  1473. type, (long long) m->addr, m->cpu, dimm, channel,
  1474. syndrome, core_err_cnt, (long long)m->status,
  1475. (long long)m->misc, optype, err);
  1476. debugf0("%s", msg);
  1477. csrow = pvt->csrow_map[channel][dimm];
  1478. /* Call the helper to output message */
  1479. if (m->mcgstatus & 1)
  1480. edac_mc_handle_fbd_ue(mci, csrow, 0,
  1481. 0 /* FIXME: should be channel here */, msg);
  1482. else if (!pvt->is_registered)
  1483. edac_mc_handle_fbd_ce(mci, csrow,
  1484. 0 /* FIXME: should be channel here */, msg);
  1485. kfree(msg);
  1486. }
  1487. /*
  1488. * i7core_check_error Retrieve and process errors reported by the
  1489. * hardware. Called by the Core module.
  1490. */
  1491. static void i7core_check_error(struct mem_ctl_info *mci)
  1492. {
  1493. struct i7core_pvt *pvt = mci->pvt_info;
  1494. int i;
  1495. unsigned count = 0;
  1496. struct mce *m;
  1497. /*
  1498. * MCE first step: Copy all mce errors into a temporary buffer
  1499. * We use a double buffering here, to reduce the risk of
  1500. * losing an error.
  1501. */
  1502. smp_rmb();
  1503. count = (pvt->mce_out + MCE_LOG_LEN - pvt->mce_in)
  1504. % MCE_LOG_LEN;
  1505. if (!count)
  1506. goto check_ce_error;
  1507. m = pvt->mce_outentry;
  1508. if (pvt->mce_in + count > MCE_LOG_LEN) {
  1509. unsigned l = MCE_LOG_LEN - pvt->mce_in;
  1510. memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * l);
  1511. smp_wmb();
  1512. pvt->mce_in = 0;
  1513. count -= l;
  1514. m += l;
  1515. }
  1516. memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * count);
  1517. smp_wmb();
  1518. pvt->mce_in += count;
  1519. smp_rmb();
  1520. if (pvt->mce_overrun) {
  1521. i7core_printk(KERN_ERR, "Lost %d memory errors\n",
  1522. pvt->mce_overrun);
  1523. smp_wmb();
  1524. pvt->mce_overrun = 0;
  1525. }
  1526. /*
  1527. * MCE second step: parse errors and display
  1528. */
  1529. for (i = 0; i < count; i++)
  1530. i7core_mce_output_error(mci, &pvt->mce_outentry[i]);
  1531. /*
  1532. * Now, let's increment CE error counts
  1533. */
  1534. check_ce_error:
  1535. if (!pvt->is_registered)
  1536. i7core_udimm_check_mc_ecc_err(mci);
  1537. else
  1538. i7core_rdimm_check_mc_ecc_err(mci);
  1539. }
  1540. /*
  1541. * i7core_mce_check_error Replicates mcelog routine to get errors
  1542. * This routine simply queues mcelog errors, and
  1543. * return. The error itself should be handled later
  1544. * by i7core_check_error.
  1545. * WARNING: As this routine should be called at NMI time, extra care should
  1546. * be taken to avoid deadlocks, and to be as fast as possible.
  1547. */
  1548. static int i7core_mce_check_error(void *priv, struct mce *mce)
  1549. {
  1550. struct mem_ctl_info *mci = priv;
  1551. struct i7core_pvt *pvt = mci->pvt_info;
  1552. /*
  1553. * Just let mcelog handle it if the error is
  1554. * outside the memory controller
  1555. */
  1556. if (((mce->status & 0xffff) >> 7) != 1)
  1557. return 0;
  1558. /* Bank 8 registers are the only ones that we know how to handle */
  1559. if (mce->bank != 8)
  1560. return 0;
  1561. #ifdef CONFIG_SMP
  1562. /* Only handle if it is the right mc controller */
  1563. if (cpu_data(mce->cpu).phys_proc_id != pvt->i7core_dev->socket)
  1564. return 0;
  1565. #endif
  1566. smp_rmb();
  1567. if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
  1568. smp_wmb();
  1569. pvt->mce_overrun++;
  1570. return 0;
  1571. }
  1572. /* Copy memory error at the ringbuffer */
  1573. memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
  1574. smp_wmb();
  1575. pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
  1576. /* Handle fatal errors immediately */
  1577. if (mce->mcgstatus & 1)
  1578. i7core_check_error(mci);
  1579. /* Advise mcelog that the errors were handled */
  1580. return 1;
  1581. }
  1582. static void i7core_pci_ctl_create(struct i7core_pvt *pvt)
  1583. {
  1584. pvt->i7core_pci = edac_pci_create_generic_ctl(
  1585. &pvt->i7core_dev->pdev[0]->dev,
  1586. EDAC_MOD_STR);
  1587. if (unlikely(!pvt->i7core_pci))
  1588. pr_warn("Unable to setup PCI error report via EDAC\n");
  1589. }
  1590. static void i7core_pci_ctl_release(struct i7core_pvt *pvt)
  1591. {
  1592. if (likely(pvt->i7core_pci))
  1593. edac_pci_release_generic_ctl(pvt->i7core_pci);
  1594. else
  1595. i7core_printk(KERN_ERR,
  1596. "Couldn't find mem_ctl_info for socket %d\n",
  1597. pvt->i7core_dev->socket);
  1598. pvt->i7core_pci = NULL;
  1599. }
  1600. static void i7core_unregister_mci(struct i7core_dev *i7core_dev)
  1601. {
  1602. struct mem_ctl_info *mci = i7core_dev->mci;
  1603. struct i7core_pvt *pvt;
  1604. if (unlikely(!mci || !mci->pvt_info)) {
  1605. debugf0("MC: " __FILE__ ": %s(): dev = %p\n",
  1606. __func__, &i7core_dev->pdev[0]->dev);
  1607. i7core_printk(KERN_ERR, "Couldn't find mci handler\n");
  1608. return;
  1609. }
  1610. pvt = mci->pvt_info;
  1611. debugf0("MC: " __FILE__ ": %s(): mci = %p, dev = %p\n",
  1612. __func__, mci, &i7core_dev->pdev[0]->dev);
  1613. /* Disable MCE NMI handler */
  1614. edac_mce_unregister(&pvt->edac_mce);
  1615. /* Disable EDAC polling */
  1616. i7core_pci_ctl_release(pvt);
  1617. /* Remove MC sysfs nodes */
  1618. edac_mc_del_mc(mci->dev);
  1619. debugf1("%s: free mci struct\n", mci->ctl_name);
  1620. kfree(mci->ctl_name);
  1621. edac_mc_free(mci);
  1622. i7core_dev->mci = NULL;
  1623. }
  1624. static int i7core_register_mci(struct i7core_dev *i7core_dev)
  1625. {
  1626. struct mem_ctl_info *mci;
  1627. struct i7core_pvt *pvt;
  1628. int rc, channels, csrows;
  1629. /* Check the number of active and not disabled channels */
  1630. rc = i7core_get_active_channels(i7core_dev->socket, &channels, &csrows);
  1631. if (unlikely(rc < 0))
  1632. return rc;
  1633. /* allocate a new MC control structure */
  1634. mci = edac_mc_alloc(sizeof(*pvt), csrows, channels, i7core_dev->socket);
  1635. if (unlikely(!mci))
  1636. return -ENOMEM;
  1637. debugf0("MC: " __FILE__ ": %s(): mci = %p, dev = %p\n",
  1638. __func__, mci, &i7core_dev->pdev[0]->dev);
  1639. pvt = mci->pvt_info;
  1640. memset(pvt, 0, sizeof(*pvt));
  1641. /* Associates i7core_dev and mci for future usage */
  1642. pvt->i7core_dev = i7core_dev;
  1643. i7core_dev->mci = mci;
  1644. /*
  1645. * FIXME: how to handle RDDR3 at MCI level? It is possible to have
  1646. * Mixed RDDR3/UDDR3 with Nehalem, provided that they are on different
  1647. * memory channels
  1648. */
  1649. mci->mtype_cap = MEM_FLAG_DDR3;
  1650. mci->edac_ctl_cap = EDAC_FLAG_NONE;
  1651. mci->edac_cap = EDAC_FLAG_NONE;
  1652. mci->mod_name = "i7core_edac.c";
  1653. mci->mod_ver = I7CORE_REVISION;
  1654. mci->ctl_name = kasprintf(GFP_KERNEL, "i7 core #%d",
  1655. i7core_dev->socket);
  1656. mci->dev_name = pci_name(i7core_dev->pdev[0]);
  1657. mci->ctl_page_to_phys = NULL;
  1658. /* Store pci devices at mci for faster access */
  1659. rc = mci_bind_devs(mci, i7core_dev);
  1660. if (unlikely(rc < 0))
  1661. goto fail0;
  1662. if (pvt->is_registered)
  1663. mci->mc_driver_sysfs_attributes = i7core_sysfs_rdimm_attrs;
  1664. else
  1665. mci->mc_driver_sysfs_attributes = i7core_sysfs_udimm_attrs;
  1666. /* Get dimm basic config */
  1667. get_dimm_config(mci);
  1668. /* record ptr to the generic device */
  1669. mci->dev = &i7core_dev->pdev[0]->dev;
  1670. /* Set the function pointer to an actual operation function */
  1671. mci->edac_check = i7core_check_error;
  1672. /* add this new MC control structure to EDAC's list of MCs */
  1673. if (unlikely(edac_mc_add_mc(mci))) {
  1674. debugf0("MC: " __FILE__
  1675. ": %s(): failed edac_mc_add_mc()\n", __func__);
  1676. /* FIXME: perhaps some code should go here that disables error
  1677. * reporting if we just enabled it
  1678. */
  1679. rc = -EINVAL;
  1680. goto fail0;
  1681. }
  1682. /* Default error mask is any memory */
  1683. pvt->inject.channel = 0;
  1684. pvt->inject.dimm = -1;
  1685. pvt->inject.rank = -1;
  1686. pvt->inject.bank = -1;
  1687. pvt->inject.page = -1;
  1688. pvt->inject.col = -1;
  1689. /* allocating generic PCI control info */
  1690. i7core_pci_ctl_create(pvt);
  1691. /* Registers on edac_mce in order to receive memory errors */
  1692. pvt->edac_mce.priv = mci;
  1693. pvt->edac_mce.check_error = i7core_mce_check_error;
  1694. rc = edac_mce_register(&pvt->edac_mce);
  1695. if (unlikely(rc < 0)) {
  1696. debugf0("MC: " __FILE__
  1697. ": %s(): failed edac_mce_register()\n", __func__);
  1698. goto fail1;
  1699. }
  1700. return 0;
  1701. fail1:
  1702. i7core_pci_ctl_release(pvt);
  1703. edac_mc_del_mc(mci->dev);
  1704. fail0:
  1705. kfree(mci->ctl_name);
  1706. edac_mc_free(mci);
  1707. i7core_dev->mci = NULL;
  1708. return rc;
  1709. }
  1710. /*
  1711. * i7core_probe Probe for ONE instance of device to see if it is
  1712. * present.
  1713. * return:
  1714. * 0 for FOUND a device
  1715. * < 0 for error code
  1716. */
  1717. static int __devinit i7core_probe(struct pci_dev *pdev,
  1718. const struct pci_device_id *id)
  1719. {
  1720. int rc;
  1721. struct i7core_dev *i7core_dev;
  1722. /* get the pci devices we want to reserve for our use */
  1723. mutex_lock(&i7core_edac_lock);
  1724. /*
  1725. * All memory controllers are allocated at the first pass.
  1726. */
  1727. if (unlikely(probed >= 1)) {
  1728. mutex_unlock(&i7core_edac_lock);
  1729. return -ENODEV;
  1730. }
  1731. probed++;
  1732. rc = i7core_get_all_devices();
  1733. if (unlikely(rc < 0))
  1734. goto fail0;
  1735. list_for_each_entry(i7core_dev, &i7core_edac_list, list) {
  1736. rc = i7core_register_mci(i7core_dev);
  1737. if (unlikely(rc < 0))
  1738. goto fail1;
  1739. }
  1740. i7core_printk(KERN_INFO, "Driver loaded.\n");
  1741. mutex_unlock(&i7core_edac_lock);
  1742. return 0;
  1743. fail1:
  1744. list_for_each_entry(i7core_dev, &i7core_edac_list, list)
  1745. i7core_unregister_mci(i7core_dev);
  1746. i7core_put_all_devices();
  1747. fail0:
  1748. mutex_unlock(&i7core_edac_lock);
  1749. return rc;
  1750. }
  1751. /*
  1752. * i7core_remove destructor for one instance of device
  1753. *
  1754. */
  1755. static void __devexit i7core_remove(struct pci_dev *pdev)
  1756. {
  1757. struct i7core_dev *i7core_dev;
  1758. debugf0(__FILE__ ": %s()\n", __func__);
  1759. /*
  1760. * we have a trouble here: pdev value for removal will be wrong, since
  1761. * it will point to the X58 register used to detect that the machine
  1762. * is a Nehalem or upper design. However, due to the way several PCI
  1763. * devices are grouped together to provide MC functionality, we need
  1764. * to use a different method for releasing the devices
  1765. */
  1766. mutex_lock(&i7core_edac_lock);
  1767. if (unlikely(!probed)) {
  1768. mutex_unlock(&i7core_edac_lock);
  1769. return;
  1770. }
  1771. list_for_each_entry(i7core_dev, &i7core_edac_list, list)
  1772. i7core_unregister_mci(i7core_dev);
  1773. /* Release PCI resources */
  1774. i7core_put_all_devices();
  1775. probed--;
  1776. mutex_unlock(&i7core_edac_lock);
  1777. }
  1778. MODULE_DEVICE_TABLE(pci, i7core_pci_tbl);
  1779. /*
  1780. * i7core_driver pci_driver structure for this module
  1781. *
  1782. */
  1783. static struct pci_driver i7core_driver = {
  1784. .name = "i7core_edac",
  1785. .probe = i7core_probe,
  1786. .remove = __devexit_p(i7core_remove),
  1787. .id_table = i7core_pci_tbl,
  1788. };
  1789. /*
  1790. * i7core_init Module entry function
  1791. * Try to initialize this module for its devices
  1792. */
  1793. static int __init i7core_init(void)
  1794. {
  1795. int pci_rc;
  1796. debugf2("MC: " __FILE__ ": %s()\n", __func__);
  1797. /* Ensure that the OPSTATE is set correctly for POLL or NMI */
  1798. opstate_init();
  1799. if (use_pci_fixup)
  1800. i7core_xeon_pci_fixup(pci_dev_table);
  1801. pci_rc = pci_register_driver(&i7core_driver);
  1802. if (pci_rc >= 0)
  1803. return 0;
  1804. i7core_printk(KERN_ERR, "Failed to register device with error %d.\n",
  1805. pci_rc);
  1806. return pci_rc;
  1807. }
  1808. /*
  1809. * i7core_exit() Module exit function
  1810. * Unregister the driver
  1811. */
  1812. static void __exit i7core_exit(void)
  1813. {
  1814. debugf2("MC: " __FILE__ ": %s()\n", __func__);
  1815. pci_unregister_driver(&i7core_driver);
  1816. }
  1817. module_init(i7core_init);
  1818. module_exit(i7core_exit);
  1819. MODULE_LICENSE("GPL");
  1820. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  1821. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  1822. MODULE_DESCRIPTION("MC Driver for Intel i7 Core memory controllers - "
  1823. I7CORE_REVISION);
  1824. module_param(edac_op_state, int, 0444);
  1825. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");