sb_edac.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893
  1. /* Intel Sandy Bridge -EN/-EP/-EX Memory Controller kernel module
  2. *
  3. * This driver supports the memory controllers found on the Intel
  4. * processor family Sandy Bridge.
  5. *
  6. * This file may be distributed under the terms of the
  7. * GNU General Public License version 2 only.
  8. *
  9. * Copyright (c) 2011 by:
  10. * Mauro Carvalho Chehab <mchehab@redhat.com>
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/pci.h>
  15. #include <linux/pci_ids.h>
  16. #include <linux/slab.h>
  17. #include <linux/delay.h>
  18. #include <linux/edac.h>
  19. #include <linux/mmzone.h>
  20. #include <linux/smp.h>
  21. #include <linux/bitmap.h>
  22. #include <asm/processor.h>
  23. #include <asm/mce.h>
  24. #include "edac_core.h"
  25. /* Static vars */
  26. static LIST_HEAD(sbridge_edac_list);
  27. static DEFINE_MUTEX(sbridge_edac_lock);
  28. static int probed;
  29. /*
  30. * Alter this version for the module when modifications are made
  31. */
  32. #define SBRIDGE_REVISION " Ver: 1.0.0 "
  33. #define EDAC_MOD_STR "sbridge_edac"
  34. /*
  35. * Debug macros
  36. */
  37. #define sbridge_printk(level, fmt, arg...) \
  38. edac_printk(level, "sbridge", fmt, ##arg)
  39. #define sbridge_mc_printk(mci, level, fmt, arg...) \
  40. edac_mc_chipset_printk(mci, level, "sbridge", fmt, ##arg)
  41. /*
  42. * Get a bit field at register value <v>, from bit <lo> to bit <hi>
  43. */
  44. #define GET_BITFIELD(v, lo, hi) \
  45. (((v) & ((1ULL << ((hi) - (lo) + 1)) - 1) << (lo)) >> (lo))
  46. /*
  47. * sbridge Memory Controller Registers
  48. */
  49. /*
  50. * FIXME: For now, let's order by device function, as it makes
  51. * easier for driver's development proccess. This table should be
  52. * moved to pci_id.h when submitted upstream
  53. */
  54. #define PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0 0x3cf4 /* 12.6 */
  55. #define PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1 0x3cf6 /* 12.7 */
  56. #define PCI_DEVICE_ID_INTEL_SBRIDGE_BR 0x3cf5 /* 13.6 */
  57. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0 0x3ca0 /* 14.0 */
  58. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA 0x3ca8 /* 15.0 */
  59. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS 0x3c71 /* 15.1 */
  60. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0 0x3caa /* 15.2 */
  61. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1 0x3cab /* 15.3 */
  62. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2 0x3cac /* 15.4 */
  63. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3 0x3cad /* 15.5 */
  64. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO 0x3cb8 /* 17.0 */
  65. /*
  66. * Currently, unused, but will be needed in the future
  67. * implementations, as they hold the error counters
  68. */
  69. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_ERR0 0x3c72 /* 16.2 */
  70. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_ERR1 0x3c73 /* 16.3 */
  71. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_ERR2 0x3c76 /* 16.6 */
  72. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_ERR3 0x3c77 /* 16.7 */
  73. /* Devices 12 Function 6, Offsets 0x80 to 0xcc */
  74. static const u32 dram_rule[] = {
  75. 0x80, 0x88, 0x90, 0x98, 0xa0,
  76. 0xa8, 0xb0, 0xb8, 0xc0, 0xc8,
  77. };
  78. #define MAX_SAD ARRAY_SIZE(dram_rule)
  79. #define SAD_LIMIT(reg) ((GET_BITFIELD(reg, 6, 25) << 26) | 0x3ffffff)
  80. #define DRAM_ATTR(reg) GET_BITFIELD(reg, 2, 3)
  81. #define INTERLEAVE_MODE(reg) GET_BITFIELD(reg, 1, 1)
  82. #define DRAM_RULE_ENABLE(reg) GET_BITFIELD(reg, 0, 0)
  83. static char *get_dram_attr(u32 reg)
  84. {
  85. switch(DRAM_ATTR(reg)) {
  86. case 0:
  87. return "DRAM";
  88. case 1:
  89. return "MMCFG";
  90. case 2:
  91. return "NXM";
  92. default:
  93. return "unknown";
  94. }
  95. }
  96. static const u32 interleave_list[] = {
  97. 0x84, 0x8c, 0x94, 0x9c, 0xa4,
  98. 0xac, 0xb4, 0xbc, 0xc4, 0xcc,
  99. };
  100. #define MAX_INTERLEAVE ARRAY_SIZE(interleave_list)
  101. #define SAD_PKG0(reg) GET_BITFIELD(reg, 0, 2)
  102. #define SAD_PKG1(reg) GET_BITFIELD(reg, 3, 5)
  103. #define SAD_PKG2(reg) GET_BITFIELD(reg, 8, 10)
  104. #define SAD_PKG3(reg) GET_BITFIELD(reg, 11, 13)
  105. #define SAD_PKG4(reg) GET_BITFIELD(reg, 16, 18)
  106. #define SAD_PKG5(reg) GET_BITFIELD(reg, 19, 21)
  107. #define SAD_PKG6(reg) GET_BITFIELD(reg, 24, 26)
  108. #define SAD_PKG7(reg) GET_BITFIELD(reg, 27, 29)
  109. static inline int sad_pkg(u32 reg, int interleave)
  110. {
  111. switch (interleave) {
  112. case 0:
  113. return SAD_PKG0(reg);
  114. case 1:
  115. return SAD_PKG1(reg);
  116. case 2:
  117. return SAD_PKG2(reg);
  118. case 3:
  119. return SAD_PKG3(reg);
  120. case 4:
  121. return SAD_PKG4(reg);
  122. case 5:
  123. return SAD_PKG5(reg);
  124. case 6:
  125. return SAD_PKG6(reg);
  126. case 7:
  127. return SAD_PKG7(reg);
  128. default:
  129. return -EINVAL;
  130. }
  131. }
  132. /* Devices 12 Function 7 */
  133. #define TOLM 0x80
  134. #define TOHM 0x84
  135. #define GET_TOLM(reg) ((GET_BITFIELD(reg, 0, 3) << 28) | 0x3ffffff)
  136. #define GET_TOHM(reg) ((GET_BITFIELD(reg, 0, 20) << 25) | 0x3ffffff)
  137. /* Device 13 Function 6 */
  138. #define SAD_TARGET 0xf0
  139. #define SOURCE_ID(reg) GET_BITFIELD(reg, 9, 11)
  140. #define SAD_CONTROL 0xf4
  141. #define NODE_ID(reg) GET_BITFIELD(reg, 0, 2)
  142. /* Device 14 function 0 */
  143. static const u32 tad_dram_rule[] = {
  144. 0x40, 0x44, 0x48, 0x4c,
  145. 0x50, 0x54, 0x58, 0x5c,
  146. 0x60, 0x64, 0x68, 0x6c,
  147. };
  148. #define MAX_TAD ARRAY_SIZE(tad_dram_rule)
  149. #define TAD_LIMIT(reg) ((GET_BITFIELD(reg, 12, 31) << 26) | 0x3ffffff)
  150. #define TAD_SOCK(reg) GET_BITFIELD(reg, 10, 11)
  151. #define TAD_CH(reg) GET_BITFIELD(reg, 8, 9)
  152. #define TAD_TGT3(reg) GET_BITFIELD(reg, 6, 7)
  153. #define TAD_TGT2(reg) GET_BITFIELD(reg, 4, 5)
  154. #define TAD_TGT1(reg) GET_BITFIELD(reg, 2, 3)
  155. #define TAD_TGT0(reg) GET_BITFIELD(reg, 0, 1)
  156. /* Device 15, function 0 */
  157. #define MCMTR 0x7c
  158. #define IS_ECC_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 2, 2)
  159. #define IS_LOCKSTEP_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 1, 1)
  160. #define IS_CLOSE_PG(mcmtr) GET_BITFIELD(mcmtr, 0, 0)
  161. /* Device 15, function 1 */
  162. #define RASENABLES 0xac
  163. #define IS_MIRROR_ENABLED(reg) GET_BITFIELD(reg, 0, 0)
  164. /* Device 15, functions 2-5 */
  165. static const int mtr_regs[] = {
  166. 0x80, 0x84, 0x88,
  167. };
  168. #define RANK_DISABLE(mtr) GET_BITFIELD(mtr, 16, 19)
  169. #define IS_DIMM_PRESENT(mtr) GET_BITFIELD(mtr, 14, 14)
  170. #define RANK_CNT_BITS(mtr) GET_BITFIELD(mtr, 12, 13)
  171. #define RANK_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 2, 4)
  172. #define COL_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 0, 1)
  173. static const u32 tad_ch_nilv_offset[] = {
  174. 0x90, 0x94, 0x98, 0x9c,
  175. 0xa0, 0xa4, 0xa8, 0xac,
  176. 0xb0, 0xb4, 0xb8, 0xbc,
  177. };
  178. #define CHN_IDX_OFFSET(reg) GET_BITFIELD(reg, 28, 29)
  179. #define TAD_OFFSET(reg) (GET_BITFIELD(reg, 6, 25) << 26)
  180. static const u32 rir_way_limit[] = {
  181. 0x108, 0x10c, 0x110, 0x114, 0x118,
  182. };
  183. #define MAX_RIR_RANGES ARRAY_SIZE(rir_way_limit)
  184. #define IS_RIR_VALID(reg) GET_BITFIELD(reg, 31, 31)
  185. #define RIR_WAY(reg) GET_BITFIELD(reg, 28, 29)
  186. #define RIR_LIMIT(reg) ((GET_BITFIELD(reg, 1, 10) << 29)| 0x1fffffff)
  187. #define MAX_RIR_WAY 8
  188. static const u32 rir_offset[MAX_RIR_RANGES][MAX_RIR_WAY] = {
  189. { 0x120, 0x124, 0x128, 0x12c, 0x130, 0x134, 0x138, 0x13c },
  190. { 0x140, 0x144, 0x148, 0x14c, 0x150, 0x154, 0x158, 0x15c },
  191. { 0x160, 0x164, 0x168, 0x16c, 0x170, 0x174, 0x178, 0x17c },
  192. { 0x180, 0x184, 0x188, 0x18c, 0x190, 0x194, 0x198, 0x19c },
  193. { 0x1a0, 0x1a4, 0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc },
  194. };
  195. #define RIR_RNK_TGT(reg) GET_BITFIELD(reg, 16, 19)
  196. #define RIR_OFFSET(reg) GET_BITFIELD(reg, 2, 14)
  197. /* Device 16, functions 2-7 */
  198. /*
  199. * FIXME: Implement the error count reads directly
  200. */
  201. static const u32 correrrcnt[] = {
  202. 0x104, 0x108, 0x10c, 0x110,
  203. };
  204. #define RANK_ODD_OV(reg) GET_BITFIELD(reg, 31, 31)
  205. #define RANK_ODD_ERR_CNT(reg) GET_BITFIELD(reg, 16, 30)
  206. #define RANK_EVEN_OV(reg) GET_BITFIELD(reg, 15, 15)
  207. #define RANK_EVEN_ERR_CNT(reg) GET_BITFIELD(reg, 0, 14)
  208. static const u32 correrrthrsld[] = {
  209. 0x11c, 0x120, 0x124, 0x128,
  210. };
  211. #define RANK_ODD_ERR_THRSLD(reg) GET_BITFIELD(reg, 16, 30)
  212. #define RANK_EVEN_ERR_THRSLD(reg) GET_BITFIELD(reg, 0, 14)
  213. /* Device 17, function 0 */
  214. #define RANK_CFG_A 0x0328
  215. #define IS_RDIMM_ENABLED(reg) GET_BITFIELD(reg, 11, 11)
  216. /*
  217. * sbridge structs
  218. */
  219. #define NUM_CHANNELS 4
  220. #define MAX_DIMMS 3 /* Max DIMMS per channel */
  221. struct sbridge_info {
  222. u32 mcmtr;
  223. };
  224. struct sbridge_channel {
  225. u32 ranks;
  226. u32 dimms;
  227. };
  228. struct pci_id_descr {
  229. int dev;
  230. int func;
  231. int dev_id;
  232. int optional;
  233. };
  234. struct pci_id_table {
  235. const struct pci_id_descr *descr;
  236. int n_devs;
  237. };
  238. struct sbridge_dev {
  239. struct list_head list;
  240. u8 bus, mc;
  241. u8 node_id, source_id;
  242. struct pci_dev **pdev;
  243. int n_devs;
  244. struct mem_ctl_info *mci;
  245. };
  246. struct sbridge_pvt {
  247. struct pci_dev *pci_ta, *pci_ddrio, *pci_ras;
  248. struct pci_dev *pci_sad0, *pci_sad1, *pci_ha0;
  249. struct pci_dev *pci_br;
  250. struct pci_dev *pci_tad[NUM_CHANNELS];
  251. struct sbridge_dev *sbridge_dev;
  252. struct sbridge_info info;
  253. struct sbridge_channel channel[NUM_CHANNELS];
  254. int csrow_map[NUM_CHANNELS][MAX_DIMMS];
  255. /* Memory type detection */
  256. bool is_mirrored, is_lockstep, is_close_pg;
  257. /* Fifo double buffers */
  258. struct mce mce_entry[MCE_LOG_LEN];
  259. struct mce mce_outentry[MCE_LOG_LEN];
  260. /* Fifo in/out counters */
  261. unsigned mce_in, mce_out;
  262. /* Count indicator to show errors not got */
  263. unsigned mce_overrun;
  264. /* Memory description */
  265. u64 tolm, tohm;
  266. };
  267. #define PCI_DESCR(device, function, device_id) \
  268. .dev = (device), \
  269. .func = (function), \
  270. .dev_id = (device_id)
  271. static const struct pci_id_descr pci_dev_descr_sbridge[] = {
  272. /* Processor Home Agent */
  273. { PCI_DESCR(14, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0) },
  274. /* Memory controller */
  275. { PCI_DESCR(15, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA) },
  276. { PCI_DESCR(15, 1, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS) },
  277. { PCI_DESCR(15, 2, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0) },
  278. { PCI_DESCR(15, 3, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1) },
  279. { PCI_DESCR(15, 4, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2) },
  280. { PCI_DESCR(15, 5, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3) },
  281. { PCI_DESCR(17, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO) },
  282. /* System Address Decoder */
  283. { PCI_DESCR(12, 6, PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0) },
  284. { PCI_DESCR(12, 7, PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1) },
  285. /* Broadcast Registers */
  286. { PCI_DESCR(13, 6, PCI_DEVICE_ID_INTEL_SBRIDGE_BR) },
  287. };
  288. #define PCI_ID_TABLE_ENTRY(A) { .descr=A, .n_devs = ARRAY_SIZE(A) }
  289. static const struct pci_id_table pci_dev_descr_sbridge_table[] = {
  290. PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge),
  291. {0,} /* 0 terminated list. */
  292. };
  293. /*
  294. * pci_device_id table for which devices we are looking for
  295. */
  296. static const struct pci_device_id sbridge_pci_tbl[] __devinitdata = {
  297. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA)},
  298. {0,} /* 0 terminated list. */
  299. };
  300. /****************************************************************************
  301. Anciliary status routines
  302. ****************************************************************************/
  303. static inline int numrank(u32 mtr)
  304. {
  305. int ranks = (1 << RANK_CNT_BITS(mtr));
  306. if (ranks > 4) {
  307. debugf0("Invalid number of ranks: %d (max = 4) raw value = %x (%04x)",
  308. ranks, (unsigned int)RANK_CNT_BITS(mtr), mtr);
  309. return -EINVAL;
  310. }
  311. return ranks;
  312. }
  313. static inline int numrow(u32 mtr)
  314. {
  315. int rows = (RANK_WIDTH_BITS(mtr) + 12);
  316. if (rows < 13 || rows > 18) {
  317. debugf0("Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)",
  318. rows, (unsigned int)RANK_WIDTH_BITS(mtr), mtr);
  319. return -EINVAL;
  320. }
  321. return 1 << rows;
  322. }
  323. static inline int numcol(u32 mtr)
  324. {
  325. int cols = (COL_WIDTH_BITS(mtr) + 10);
  326. if (cols > 12) {
  327. debugf0("Invalid number of cols: %d (max = 4) raw value = %x (%04x)",
  328. cols, (unsigned int)COL_WIDTH_BITS(mtr), mtr);
  329. return -EINVAL;
  330. }
  331. return 1 << cols;
  332. }
  333. static struct sbridge_dev *get_sbridge_dev(u8 bus)
  334. {
  335. struct sbridge_dev *sbridge_dev;
  336. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  337. if (sbridge_dev->bus == bus)
  338. return sbridge_dev;
  339. }
  340. return NULL;
  341. }
  342. static struct sbridge_dev *alloc_sbridge_dev(u8 bus,
  343. const struct pci_id_table *table)
  344. {
  345. struct sbridge_dev *sbridge_dev;
  346. sbridge_dev = kzalloc(sizeof(*sbridge_dev), GFP_KERNEL);
  347. if (!sbridge_dev)
  348. return NULL;
  349. sbridge_dev->pdev = kzalloc(sizeof(*sbridge_dev->pdev) * table->n_devs,
  350. GFP_KERNEL);
  351. if (!sbridge_dev->pdev) {
  352. kfree(sbridge_dev);
  353. return NULL;
  354. }
  355. sbridge_dev->bus = bus;
  356. sbridge_dev->n_devs = table->n_devs;
  357. list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
  358. return sbridge_dev;
  359. }
  360. static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
  361. {
  362. list_del(&sbridge_dev->list);
  363. kfree(sbridge_dev->pdev);
  364. kfree(sbridge_dev);
  365. }
  366. /****************************************************************************
  367. Memory check routines
  368. ****************************************************************************/
  369. static struct pci_dev *get_pdev_slot_func(u8 bus, unsigned slot,
  370. unsigned func)
  371. {
  372. struct sbridge_dev *sbridge_dev = get_sbridge_dev(bus);
  373. int i;
  374. if (!sbridge_dev)
  375. return NULL;
  376. for (i = 0; i < sbridge_dev->n_devs; i++) {
  377. if (!sbridge_dev->pdev[i])
  378. continue;
  379. if (PCI_SLOT(sbridge_dev->pdev[i]->devfn) == slot &&
  380. PCI_FUNC(sbridge_dev->pdev[i]->devfn) == func) {
  381. debugf1("Associated %02x.%02x.%d with %p\n",
  382. bus, slot, func, sbridge_dev->pdev[i]);
  383. return sbridge_dev->pdev[i];
  384. }
  385. }
  386. return NULL;
  387. }
  388. /**
  389. * sbridge_get_active_channels() - gets the number of channels and csrows
  390. * bus: Device bus
  391. * @channels: Number of channels that will be returned
  392. * @csrows: Number of csrows found
  393. *
  394. * Since EDAC core needs to know in advance the number of available channels
  395. * and csrows, in order to allocate memory for csrows/channels, it is needed
  396. * to run two similar steps. At the first step, implemented on this function,
  397. * it checks the number of csrows/channels present at one socket, identified
  398. * by the associated PCI bus.
  399. * this is used in order to properly allocate the size of mci components.
  400. * Note: one csrow is one dimm.
  401. */
  402. static int sbridge_get_active_channels(const u8 bus, unsigned *channels,
  403. unsigned *csrows)
  404. {
  405. struct pci_dev *pdev = NULL;
  406. int i, j;
  407. u32 mcmtr;
  408. *channels = 0;
  409. *csrows = 0;
  410. pdev = get_pdev_slot_func(bus, 15, 0);
  411. if (!pdev) {
  412. sbridge_printk(KERN_ERR, "Couldn't find PCI device "
  413. "%2x.%02d.%d!!!\n",
  414. bus, 15, 0);
  415. return -ENODEV;
  416. }
  417. pci_read_config_dword(pdev, MCMTR, &mcmtr);
  418. if (!IS_ECC_ENABLED(mcmtr)) {
  419. sbridge_printk(KERN_ERR, "ECC is disabled. Aborting\n");
  420. return -ENODEV;
  421. }
  422. for (i = 0; i < NUM_CHANNELS; i++) {
  423. u32 mtr;
  424. /* Device 15 functions 2 - 5 */
  425. pdev = get_pdev_slot_func(bus, 15, 2 + i);
  426. if (!pdev) {
  427. sbridge_printk(KERN_ERR, "Couldn't find PCI device "
  428. "%2x.%02d.%d!!!\n",
  429. bus, 15, 2 + i);
  430. return -ENODEV;
  431. }
  432. (*channels)++;
  433. for (j = 0; j < ARRAY_SIZE(mtr_regs); j++) {
  434. pci_read_config_dword(pdev, mtr_regs[j], &mtr);
  435. debugf1("Bus#%02x channel #%d MTR%d = %x\n", bus, i, j, mtr);
  436. if (IS_DIMM_PRESENT(mtr))
  437. (*csrows)++;
  438. }
  439. }
  440. debugf0("Number of active channels: %d, number of active dimms: %d\n",
  441. *channels, *csrows);
  442. return 0;
  443. }
  444. static int get_dimm_config(const struct mem_ctl_info *mci)
  445. {
  446. struct sbridge_pvt *pvt = mci->pvt_info;
  447. struct csrow_info *csr;
  448. int i, j, banks, ranks, rows, cols, size, npages;
  449. int csrow = 0;
  450. unsigned long last_page = 0;
  451. u32 reg;
  452. enum edac_type mode;
  453. enum mem_type mtype;
  454. pci_read_config_dword(pvt->pci_br, SAD_TARGET, &reg);
  455. pvt->sbridge_dev->source_id = SOURCE_ID(reg);
  456. pci_read_config_dword(pvt->pci_br, SAD_CONTROL, &reg);
  457. pvt->sbridge_dev->node_id = NODE_ID(reg);
  458. debugf0("mc#%d: Node ID: %d, source ID: %d\n",
  459. pvt->sbridge_dev->mc,
  460. pvt->sbridge_dev->node_id,
  461. pvt->sbridge_dev->source_id);
  462. pci_read_config_dword(pvt->pci_ras, RASENABLES, &reg);
  463. if (IS_MIRROR_ENABLED(reg)) {
  464. debugf0("Memory mirror is enabled\n");
  465. pvt->is_mirrored = true;
  466. } else {
  467. debugf0("Memory mirror is disabled\n");
  468. pvt->is_mirrored = false;
  469. }
  470. pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
  471. if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
  472. debugf0("Lockstep is enabled\n");
  473. mode = EDAC_S8ECD8ED;
  474. pvt->is_lockstep = true;
  475. } else {
  476. debugf0("Lockstep is disabled\n");
  477. mode = EDAC_S4ECD4ED;
  478. pvt->is_lockstep = false;
  479. }
  480. if (IS_CLOSE_PG(pvt->info.mcmtr)) {
  481. debugf0("address map is on closed page mode\n");
  482. pvt->is_close_pg = true;
  483. } else {
  484. debugf0("address map is on open page mode\n");
  485. pvt->is_close_pg = false;
  486. }
  487. pci_read_config_dword(pvt->pci_ta, RANK_CFG_A, &reg);
  488. if (IS_RDIMM_ENABLED(reg)) {
  489. /* FIXME: Can also be LRDIMM */
  490. debugf0("Memory is registered\n");
  491. mtype = MEM_RDDR3;
  492. } else {
  493. debugf0("Memory is unregistered\n");
  494. mtype = MEM_DDR3;
  495. }
  496. /* On all supported DDR3 DIMM types, there are 8 banks available */
  497. banks = 8;
  498. for (i = 0; i < NUM_CHANNELS; i++) {
  499. u32 mtr;
  500. for (j = 0; j < ARRAY_SIZE(mtr_regs); j++) {
  501. pci_read_config_dword(pvt->pci_tad[i],
  502. mtr_regs[j], &mtr);
  503. debugf4("Channel #%d MTR%d = %x\n", i, j, mtr);
  504. if (IS_DIMM_PRESENT(mtr)) {
  505. pvt->channel[i].dimms++;
  506. ranks = numrank(mtr);
  507. rows = numrow(mtr);
  508. cols = numcol(mtr);
  509. /* DDR3 has 8 I/O banks */
  510. size = (rows * cols * banks * ranks) >> (20 - 3);
  511. npages = MiB_TO_PAGES(size);
  512. debugf0("mc#%d: channel %d, dimm %d, %d Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
  513. pvt->sbridge_dev->mc, i, j,
  514. size, npages,
  515. banks, ranks, rows, cols);
  516. csr = &mci->csrows[csrow];
  517. csr->first_page = last_page;
  518. csr->last_page = last_page + npages - 1;
  519. csr->page_mask = 0UL; /* Unused */
  520. csr->nr_pages = npages;
  521. csr->grain = 32;
  522. csr->csrow_idx = csrow;
  523. csr->dtype = (banks == 8) ? DEV_X8 : DEV_X4;
  524. csr->ce_count = 0;
  525. csr->ue_count = 0;
  526. csr->mtype = mtype;
  527. csr->edac_mode = mode;
  528. csr->nr_channels = 1;
  529. csr->channels[0].chan_idx = i;
  530. csr->channels[0].ce_count = 0;
  531. pvt->csrow_map[i][j] = csrow;
  532. snprintf(csr->channels[0].label,
  533. sizeof(csr->channels[0].label),
  534. "CPU_SrcID#%u_Channel#%u_DIMM#%u",
  535. pvt->sbridge_dev->source_id, i, j);
  536. last_page += npages;
  537. csrow++;
  538. }
  539. }
  540. }
  541. return 0;
  542. }
  543. static void get_memory_layout(const struct mem_ctl_info *mci)
  544. {
  545. struct sbridge_pvt *pvt = mci->pvt_info;
  546. int i, j, k, n_sads, n_tads, sad_interl;
  547. u32 reg;
  548. u64 limit, prv = 0;
  549. u64 tmp_mb;
  550. u32 rir_way;
  551. /*
  552. * Step 1) Get TOLM/TOHM ranges
  553. */
  554. /* Address range is 32:28 */
  555. pci_read_config_dword(pvt->pci_sad1, TOLM,
  556. &reg);
  557. pvt->tolm = GET_TOLM(reg);
  558. tmp_mb = (1 + pvt->tolm) >> 20;
  559. debugf0("TOLM: %Lu.%03Lu GB (0x%016Lx)\n",
  560. tmp_mb / 1000, tmp_mb % 1000, (u64)pvt->tolm);
  561. /* Address range is already 45:25 */
  562. pci_read_config_dword(pvt->pci_sad1, TOHM,
  563. &reg);
  564. pvt->tohm = GET_TOHM(reg);
  565. tmp_mb = (1 + pvt->tohm) >> 20;
  566. debugf0("TOHM: %Lu.%03Lu GB (0x%016Lx)",
  567. tmp_mb / 1000, tmp_mb % 1000, (u64)pvt->tohm);
  568. /*
  569. * Step 2) Get SAD range and SAD Interleave list
  570. * TAD registers contain the interleave wayness. However, it
  571. * seems simpler to just discover it indirectly, with the
  572. * algorithm bellow.
  573. */
  574. prv = 0;
  575. for (n_sads = 0; n_sads < MAX_SAD; n_sads++) {
  576. /* SAD_LIMIT Address range is 45:26 */
  577. pci_read_config_dword(pvt->pci_sad0, dram_rule[n_sads],
  578. &reg);
  579. limit = SAD_LIMIT(reg);
  580. if (!DRAM_RULE_ENABLE(reg))
  581. continue;
  582. if (limit <= prv)
  583. break;
  584. tmp_mb = (limit + 1) >> 20;
  585. debugf0("SAD#%d %s up to %Lu.%03Lu GB (0x%016Lx) %s reg=0x%08x\n",
  586. n_sads,
  587. get_dram_attr(reg),
  588. tmp_mb / 1000, tmp_mb % 1000,
  589. ((u64)tmp_mb) << 20L,
  590. INTERLEAVE_MODE(reg) ? "Interleave: 8:6" : "Interleave: [8:6]XOR[18:16]",
  591. reg);
  592. prv = limit;
  593. pci_read_config_dword(pvt->pci_sad0, interleave_list[n_sads],
  594. &reg);
  595. sad_interl = sad_pkg(reg, 0);
  596. for (j = 0; j < 8; j++) {
  597. if (j > 0 && sad_interl == sad_pkg(reg, j))
  598. break;
  599. debugf0("SAD#%d, interleave #%d: %d\n",
  600. n_sads, j, sad_pkg(reg, j));
  601. }
  602. }
  603. /*
  604. * Step 3) Get TAD range
  605. */
  606. prv = 0;
  607. for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
  608. pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
  609. &reg);
  610. limit = TAD_LIMIT(reg);
  611. if (limit <= prv)
  612. break;
  613. tmp_mb = (limit + 1) >> 20;
  614. debugf0("TAD#%d: up to %Lu.%03Lu GB (0x%016Lx), socket interleave %d, memory interleave %d, TGT: %d, %d, %d, %d, reg=0x%08x\n",
  615. n_tads, tmp_mb / 1000, tmp_mb % 1000,
  616. ((u64)tmp_mb) << 20L,
  617. (u32)TAD_SOCK(reg),
  618. (u32)TAD_CH(reg),
  619. (u32)TAD_TGT0(reg),
  620. (u32)TAD_TGT1(reg),
  621. (u32)TAD_TGT2(reg),
  622. (u32)TAD_TGT3(reg),
  623. reg);
  624. prv = tmp_mb;
  625. }
  626. /*
  627. * Step 4) Get TAD offsets, per each channel
  628. */
  629. for (i = 0; i < NUM_CHANNELS; i++) {
  630. if (!pvt->channel[i].dimms)
  631. continue;
  632. for (j = 0; j < n_tads; j++) {
  633. pci_read_config_dword(pvt->pci_tad[i],
  634. tad_ch_nilv_offset[j],
  635. &reg);
  636. tmp_mb = TAD_OFFSET(reg) >> 20;
  637. debugf0("TAD CH#%d, offset #%d: %Lu.%03Lu GB (0x%016Lx), reg=0x%08x\n",
  638. i, j,
  639. tmp_mb / 1000, tmp_mb % 1000,
  640. ((u64)tmp_mb) << 20L,
  641. reg);
  642. }
  643. }
  644. /*
  645. * Step 6) Get RIR Wayness/Limit, per each channel
  646. */
  647. for (i = 0; i < NUM_CHANNELS; i++) {
  648. if (!pvt->channel[i].dimms)
  649. continue;
  650. for (j = 0; j < MAX_RIR_RANGES; j++) {
  651. pci_read_config_dword(pvt->pci_tad[i],
  652. rir_way_limit[j],
  653. &reg);
  654. if (!IS_RIR_VALID(reg))
  655. continue;
  656. tmp_mb = RIR_LIMIT(reg) >> 20;
  657. rir_way = 1 << RIR_WAY(reg);
  658. debugf0("CH#%d RIR#%d, limit: %Lu.%03Lu GB (0x%016Lx), way: %d, reg=0x%08x\n",
  659. i, j,
  660. tmp_mb / 1000, tmp_mb % 1000,
  661. ((u64)tmp_mb) << 20L,
  662. rir_way,
  663. reg);
  664. for (k = 0; k < rir_way; k++) {
  665. pci_read_config_dword(pvt->pci_tad[i],
  666. rir_offset[j][k],
  667. &reg);
  668. tmp_mb = RIR_OFFSET(reg) << 6;
  669. debugf0("CH#%d RIR#%d INTL#%d, offset %Lu.%03Lu GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
  670. i, j, k,
  671. tmp_mb / 1000, tmp_mb % 1000,
  672. ((u64)tmp_mb) << 20L,
  673. (u32)RIR_RNK_TGT(reg),
  674. reg);
  675. }
  676. }
  677. }
  678. }
  679. struct mem_ctl_info *get_mci_for_node_id(u8 node_id)
  680. {
  681. struct sbridge_dev *sbridge_dev;
  682. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  683. if (sbridge_dev->node_id == node_id)
  684. return sbridge_dev->mci;
  685. }
  686. return NULL;
  687. }
  688. static int get_memory_error_data(struct mem_ctl_info *mci,
  689. u64 addr,
  690. u8 *socket,
  691. long *channel_mask,
  692. u8 *rank,
  693. char *area_type)
  694. {
  695. struct mem_ctl_info *new_mci;
  696. struct sbridge_pvt *pvt = mci->pvt_info;
  697. char msg[256];
  698. int n_rir, n_sads, n_tads, sad_way, sck_xch;
  699. int sad_interl, idx, base_ch;
  700. int interleave_mode;
  701. unsigned sad_interleave[MAX_INTERLEAVE];
  702. u32 reg;
  703. u8 ch_way,sck_way;
  704. u32 tad_offset;
  705. u32 rir_way;
  706. u64 ch_addr, offset, limit, prv = 0;
  707. /*
  708. * Step 0) Check if the address is at special memory ranges
  709. * The check bellow is probably enough to fill all cases where
  710. * the error is not inside a memory, except for the legacy
  711. * range (e. g. VGA addresses). It is unlikely, however, that the
  712. * memory controller would generate an error on that range.
  713. */
  714. if ((addr > (u64) pvt->tolm) && (addr < (1L << 32))) {
  715. sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
  716. edac_mc_handle_ce_no_info(mci, msg);
  717. return -EINVAL;
  718. }
  719. if (addr >= (u64)pvt->tohm) {
  720. sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
  721. edac_mc_handle_ce_no_info(mci, msg);
  722. return -EINVAL;
  723. }
  724. /*
  725. * Step 1) Get socket
  726. */
  727. for (n_sads = 0; n_sads < MAX_SAD; n_sads++) {
  728. pci_read_config_dword(pvt->pci_sad0, dram_rule[n_sads],
  729. &reg);
  730. if (!DRAM_RULE_ENABLE(reg))
  731. continue;
  732. limit = SAD_LIMIT(reg);
  733. if (limit <= prv) {
  734. sprintf(msg, "Can't discover the memory socket");
  735. edac_mc_handle_ce_no_info(mci, msg);
  736. return -EINVAL;
  737. }
  738. if (addr <= limit)
  739. break;
  740. prv = limit;
  741. }
  742. if (n_sads == MAX_SAD) {
  743. sprintf(msg, "Can't discover the memory socket");
  744. edac_mc_handle_ce_no_info(mci, msg);
  745. return -EINVAL;
  746. }
  747. area_type = get_dram_attr(reg);
  748. interleave_mode = INTERLEAVE_MODE(reg);
  749. pci_read_config_dword(pvt->pci_sad0, interleave_list[n_sads],
  750. &reg);
  751. sad_interl = sad_pkg(reg, 0);
  752. for (sad_way = 0; sad_way < 8; sad_way++) {
  753. if (sad_way > 0 && sad_interl == sad_pkg(reg, sad_way))
  754. break;
  755. sad_interleave[sad_way] = sad_pkg(reg, sad_way);
  756. debugf0("SAD interleave #%d: %d\n",
  757. sad_way, sad_interleave[sad_way]);
  758. }
  759. debugf0("mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
  760. pvt->sbridge_dev->mc,
  761. n_sads,
  762. addr,
  763. limit,
  764. sad_way + 7,
  765. INTERLEAVE_MODE(reg) ? "" : "XOR[18:16]");
  766. if (interleave_mode)
  767. idx = ((addr >> 6) ^ (addr >> 16)) & 7;
  768. else
  769. idx = (addr >> 6) & 7;
  770. switch (sad_way) {
  771. case 1:
  772. idx = 0;
  773. break;
  774. case 2:
  775. idx = idx & 1;
  776. break;
  777. case 4:
  778. idx = idx & 3;
  779. break;
  780. case 8:
  781. break;
  782. default:
  783. sprintf(msg, "Can't discover socket interleave");
  784. edac_mc_handle_ce_no_info(mci, msg);
  785. return -EINVAL;
  786. }
  787. *socket = sad_interleave[idx];
  788. debugf0("SAD interleave index: %d (wayness %d) = CPU socket %d\n",
  789. idx, sad_way, *socket);
  790. /*
  791. * Move to the proper node structure, in order to access the
  792. * right PCI registers
  793. */
  794. new_mci = get_mci_for_node_id(*socket);
  795. if (!new_mci) {
  796. sprintf(msg, "Struct for socket #%u wasn't initialized",
  797. *socket);
  798. edac_mc_handle_ce_no_info(mci, msg);
  799. return -EINVAL;
  800. }
  801. mci = new_mci;
  802. pvt = mci->pvt_info;
  803. /*
  804. * Step 2) Get memory channel
  805. */
  806. prv = 0;
  807. for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
  808. pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
  809. &reg);
  810. limit = TAD_LIMIT(reg);
  811. if (limit <= prv) {
  812. sprintf(msg, "Can't discover the memory channel");
  813. edac_mc_handle_ce_no_info(mci, msg);
  814. return -EINVAL;
  815. }
  816. if (addr <= limit)
  817. break;
  818. prv = limit;
  819. }
  820. ch_way = TAD_CH(reg) + 1;
  821. sck_way = TAD_SOCK(reg) + 1;
  822. /*
  823. * FIXME: Is it right to always use channel 0 for offsets?
  824. */
  825. pci_read_config_dword(pvt->pci_tad[0],
  826. tad_ch_nilv_offset[n_tads],
  827. &tad_offset);
  828. if (ch_way == 3)
  829. idx = addr >> 6;
  830. else
  831. idx = addr >> (6 + sck_way);
  832. idx = idx % ch_way;
  833. /*
  834. * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
  835. */
  836. switch (idx) {
  837. case 0:
  838. base_ch = TAD_TGT0(reg);
  839. break;
  840. case 1:
  841. base_ch = TAD_TGT1(reg);
  842. break;
  843. case 2:
  844. base_ch = TAD_TGT2(reg);
  845. break;
  846. case 3:
  847. base_ch = TAD_TGT3(reg);
  848. break;
  849. default:
  850. sprintf(msg, "Can't discover the TAD target");
  851. edac_mc_handle_ce_no_info(mci, msg);
  852. return -EINVAL;
  853. }
  854. *channel_mask = 1 << base_ch;
  855. if (pvt->is_mirrored) {
  856. *channel_mask |= 1 << ((base_ch + 2) % 4);
  857. switch(ch_way) {
  858. case 2:
  859. case 4:
  860. sck_xch = 1 << sck_way * (ch_way >> 1);
  861. break;
  862. default:
  863. sprintf(msg, "Invalid mirror set. Can't decode addr");
  864. edac_mc_handle_ce_no_info(mci, msg);
  865. return -EINVAL;
  866. }
  867. } else
  868. sck_xch = (1 << sck_way) * ch_way;
  869. if (pvt->is_lockstep)
  870. *channel_mask |= 1 << ((base_ch + 1) % 4);
  871. offset = TAD_OFFSET(tad_offset);
  872. debugf0("TAD#%d: address 0x%016Lx < 0x%016Lx, socket interleave %d, channel interleave %d (offset 0x%08Lx), index %d, base ch: %d, ch mask: 0x%02lx\n",
  873. n_tads,
  874. addr,
  875. limit,
  876. (u32)TAD_SOCK(reg),
  877. ch_way,
  878. offset,
  879. idx,
  880. base_ch,
  881. *channel_mask);
  882. /* Calculate channel address */
  883. /* Remove the TAD offset */
  884. if (offset > addr) {
  885. sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
  886. offset, addr);
  887. edac_mc_handle_ce_no_info(mci, msg);
  888. return -EINVAL;
  889. }
  890. addr -= offset;
  891. /* Store the low bits [0:6] of the addr */
  892. ch_addr = addr & 0x7f;
  893. /* Remove socket wayness and remove 6 bits */
  894. addr >>= 6;
  895. addr /= sck_xch;
  896. #if 0
  897. /* Divide by channel way */
  898. addr = addr / ch_way;
  899. #endif
  900. /* Recover the last 6 bits */
  901. ch_addr |= addr << 6;
  902. /*
  903. * Step 3) Decode rank
  904. */
  905. for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
  906. pci_read_config_dword(pvt->pci_tad[base_ch],
  907. rir_way_limit[n_rir],
  908. &reg);
  909. if (!IS_RIR_VALID(reg))
  910. continue;
  911. limit = RIR_LIMIT(reg);
  912. debugf0("RIR#%d, limit: %Lu.%03Lu GB (0x%016Lx), way: %d\n",
  913. n_rir,
  914. (limit >> 20) / 1000, (limit >> 20) % 1000,
  915. limit,
  916. 1 << RIR_WAY(reg));
  917. if (ch_addr <= limit)
  918. break;
  919. }
  920. if (n_rir == MAX_RIR_RANGES) {
  921. sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
  922. ch_addr);
  923. edac_mc_handle_ce_no_info(mci, msg);
  924. return -EINVAL;
  925. }
  926. rir_way = RIR_WAY(reg);
  927. if (pvt->is_close_pg)
  928. idx = (ch_addr >> 6);
  929. else
  930. idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
  931. idx %= 1 << rir_way;
  932. pci_read_config_dword(pvt->pci_tad[base_ch],
  933. rir_offset[n_rir][idx],
  934. &reg);
  935. *rank = RIR_RNK_TGT(reg);
  936. debugf0("RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
  937. n_rir,
  938. ch_addr,
  939. limit,
  940. rir_way,
  941. idx);
  942. return 0;
  943. }
  944. /****************************************************************************
  945. Device initialization routines: put/get, init/exit
  946. ****************************************************************************/
  947. /*
  948. * sbridge_put_all_devices 'put' all the devices that we have
  949. * reserved via 'get'
  950. */
  951. static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
  952. {
  953. int i;
  954. debugf0(__FILE__ ": %s()\n", __func__);
  955. for (i = 0; i < sbridge_dev->n_devs; i++) {
  956. struct pci_dev *pdev = sbridge_dev->pdev[i];
  957. if (!pdev)
  958. continue;
  959. debugf0("Removing dev %02x:%02x.%d\n",
  960. pdev->bus->number,
  961. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  962. pci_dev_put(pdev);
  963. }
  964. }
  965. static void sbridge_put_all_devices(void)
  966. {
  967. struct sbridge_dev *sbridge_dev, *tmp;
  968. list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
  969. sbridge_put_devices(sbridge_dev);
  970. free_sbridge_dev(sbridge_dev);
  971. }
  972. }
  973. /*
  974. * sbridge_get_all_devices Find and perform 'get' operation on the MCH's
  975. * device/functions we want to reference for this driver
  976. *
  977. * Need to 'get' device 16 func 1 and func 2
  978. */
  979. static int sbridge_get_onedevice(struct pci_dev **prev,
  980. u8 *num_mc,
  981. const struct pci_id_table *table,
  982. const unsigned devno)
  983. {
  984. struct sbridge_dev *sbridge_dev;
  985. const struct pci_id_descr *dev_descr = &table->descr[devno];
  986. struct pci_dev *pdev = NULL;
  987. u8 bus = 0;
  988. sbridge_printk(KERN_INFO,
  989. "Seeking for: dev %02x.%d PCI ID %04x:%04x\n",
  990. dev_descr->dev, dev_descr->func,
  991. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  992. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  993. dev_descr->dev_id, *prev);
  994. if (!pdev) {
  995. if (*prev) {
  996. *prev = pdev;
  997. return 0;
  998. }
  999. if (dev_descr->optional)
  1000. return 0;
  1001. if (devno == 0)
  1002. return -ENODEV;
  1003. sbridge_printk(KERN_INFO,
  1004. "Device not found: dev %02x.%d PCI ID %04x:%04x\n",
  1005. dev_descr->dev, dev_descr->func,
  1006. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1007. /* End of list, leave */
  1008. return -ENODEV;
  1009. }
  1010. bus = pdev->bus->number;
  1011. sbridge_dev = get_sbridge_dev(bus);
  1012. if (!sbridge_dev) {
  1013. sbridge_dev = alloc_sbridge_dev(bus, table);
  1014. if (!sbridge_dev) {
  1015. pci_dev_put(pdev);
  1016. return -ENOMEM;
  1017. }
  1018. (*num_mc)++;
  1019. }
  1020. if (sbridge_dev->pdev[devno]) {
  1021. sbridge_printk(KERN_ERR,
  1022. "Duplicated device for "
  1023. "dev %02x:%d.%d PCI ID %04x:%04x\n",
  1024. bus, dev_descr->dev, dev_descr->func,
  1025. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1026. pci_dev_put(pdev);
  1027. return -ENODEV;
  1028. }
  1029. sbridge_dev->pdev[devno] = pdev;
  1030. /* Sanity check */
  1031. if (unlikely(PCI_SLOT(pdev->devfn) != dev_descr->dev ||
  1032. PCI_FUNC(pdev->devfn) != dev_descr->func)) {
  1033. sbridge_printk(KERN_ERR,
  1034. "Device PCI ID %04x:%04x "
  1035. "has dev %02x:%d.%d instead of dev %02x:%02x.%d\n",
  1036. PCI_VENDOR_ID_INTEL, dev_descr->dev_id,
  1037. bus, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1038. bus, dev_descr->dev, dev_descr->func);
  1039. return -ENODEV;
  1040. }
  1041. /* Be sure that the device is enabled */
  1042. if (unlikely(pci_enable_device(pdev) < 0)) {
  1043. sbridge_printk(KERN_ERR,
  1044. "Couldn't enable "
  1045. "dev %02x:%d.%d PCI ID %04x:%04x\n",
  1046. bus, dev_descr->dev, dev_descr->func,
  1047. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1048. return -ENODEV;
  1049. }
  1050. debugf0("Detected dev %02x:%d.%d PCI ID %04x:%04x\n",
  1051. bus, dev_descr->dev,
  1052. dev_descr->func,
  1053. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1054. /*
  1055. * As stated on drivers/pci/search.c, the reference count for
  1056. * @from is always decremented if it is not %NULL. So, as we need
  1057. * to get all devices up to null, we need to do a get for the device
  1058. */
  1059. pci_dev_get(pdev);
  1060. *prev = pdev;
  1061. return 0;
  1062. }
  1063. static int sbridge_get_all_devices(u8 *num_mc)
  1064. {
  1065. int i, rc;
  1066. struct pci_dev *pdev = NULL;
  1067. const struct pci_id_table *table = pci_dev_descr_sbridge_table;
  1068. while (table && table->descr) {
  1069. for (i = 0; i < table->n_devs; i++) {
  1070. pdev = NULL;
  1071. do {
  1072. rc = sbridge_get_onedevice(&pdev, num_mc,
  1073. table, i);
  1074. if (rc < 0) {
  1075. if (i == 0) {
  1076. i = table->n_devs;
  1077. break;
  1078. }
  1079. sbridge_put_all_devices();
  1080. return -ENODEV;
  1081. }
  1082. } while (pdev);
  1083. }
  1084. table++;
  1085. }
  1086. return 0;
  1087. }
  1088. static int mci_bind_devs(struct mem_ctl_info *mci,
  1089. struct sbridge_dev *sbridge_dev)
  1090. {
  1091. struct sbridge_pvt *pvt = mci->pvt_info;
  1092. struct pci_dev *pdev;
  1093. int i, func, slot;
  1094. for (i = 0; i < sbridge_dev->n_devs; i++) {
  1095. pdev = sbridge_dev->pdev[i];
  1096. if (!pdev)
  1097. continue;
  1098. slot = PCI_SLOT(pdev->devfn);
  1099. func = PCI_FUNC(pdev->devfn);
  1100. switch (slot) {
  1101. case 12:
  1102. switch (func) {
  1103. case 6:
  1104. pvt->pci_sad0 = pdev;
  1105. break;
  1106. case 7:
  1107. pvt->pci_sad1 = pdev;
  1108. break;
  1109. default:
  1110. goto error;
  1111. }
  1112. break;
  1113. case 13:
  1114. switch (func) {
  1115. case 6:
  1116. pvt->pci_br = pdev;
  1117. break;
  1118. default:
  1119. goto error;
  1120. }
  1121. break;
  1122. case 14:
  1123. switch (func) {
  1124. case 0:
  1125. pvt->pci_ha0 = pdev;
  1126. break;
  1127. default:
  1128. goto error;
  1129. }
  1130. break;
  1131. case 15:
  1132. switch (func) {
  1133. case 0:
  1134. pvt->pci_ta = pdev;
  1135. break;
  1136. case 1:
  1137. pvt->pci_ras = pdev;
  1138. break;
  1139. case 2:
  1140. case 3:
  1141. case 4:
  1142. case 5:
  1143. pvt->pci_tad[func - 2] = pdev;
  1144. break;
  1145. default:
  1146. goto error;
  1147. }
  1148. break;
  1149. case 17:
  1150. switch (func) {
  1151. case 0:
  1152. pvt->pci_ddrio = pdev;
  1153. break;
  1154. default:
  1155. goto error;
  1156. }
  1157. break;
  1158. default:
  1159. goto error;
  1160. }
  1161. debugf0("Associated PCI %02x.%02d.%d with dev = %p\n",
  1162. sbridge_dev->bus,
  1163. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1164. pdev);
  1165. }
  1166. /* Check if everything were registered */
  1167. if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha0 ||
  1168. !pvt-> pci_tad || !pvt->pci_ras || !pvt->pci_ta ||
  1169. !pvt->pci_ddrio)
  1170. goto enodev;
  1171. for (i = 0; i < NUM_CHANNELS; i++) {
  1172. if (!pvt->pci_tad[i])
  1173. goto enodev;
  1174. }
  1175. return 0;
  1176. enodev:
  1177. sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
  1178. return -ENODEV;
  1179. error:
  1180. sbridge_printk(KERN_ERR, "Device %d, function %d "
  1181. "is out of the expected range\n",
  1182. slot, func);
  1183. return -EINVAL;
  1184. }
  1185. /****************************************************************************
  1186. Error check routines
  1187. ****************************************************************************/
  1188. /*
  1189. * While Sandy Bridge has error count registers, SMI BIOS read values from
  1190. * and resets the counters. So, they are not reliable for the OS to read
  1191. * from them. So, we have no option but to just trust on whatever MCE is
  1192. * telling us about the errors.
  1193. */
  1194. static void sbridge_mce_output_error(struct mem_ctl_info *mci,
  1195. const struct mce *m)
  1196. {
  1197. struct mem_ctl_info *new_mci;
  1198. struct sbridge_pvt *pvt = mci->pvt_info;
  1199. char *type, *optype, *msg, *recoverable_msg;
  1200. bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
  1201. bool overflow = GET_BITFIELD(m->status, 62, 62);
  1202. bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
  1203. bool recoverable = GET_BITFIELD(m->status, 56, 56);
  1204. u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
  1205. u32 mscod = GET_BITFIELD(m->status, 16, 31);
  1206. u32 errcode = GET_BITFIELD(m->status, 0, 15);
  1207. u32 channel = GET_BITFIELD(m->status, 0, 3);
  1208. u32 optypenum = GET_BITFIELD(m->status, 4, 6);
  1209. long channel_mask, first_channel;
  1210. u8 rank, socket;
  1211. int csrow, rc, dimm;
  1212. char *area_type = "Unknown";
  1213. if (ripv)
  1214. type = "NON_FATAL";
  1215. else
  1216. type = "FATAL";
  1217. /*
  1218. * According with Table 15-9 of the Intel Archictecture spec vol 3A,
  1219. * memory errors should fit in this mask:
  1220. * 000f 0000 1mmm cccc (binary)
  1221. * where:
  1222. * f = Correction Report Filtering Bit. If 1, subsequent errors
  1223. * won't be shown
  1224. * mmm = error type
  1225. * cccc = channel
  1226. * If the mask doesn't match, report an error to the parsing logic
  1227. */
  1228. if (! ((errcode & 0xef80) == 0x80)) {
  1229. optype = "Can't parse: it is not a mem";
  1230. } else {
  1231. switch (optypenum) {
  1232. case 0:
  1233. optype = "generic undef request";
  1234. break;
  1235. case 1:
  1236. optype = "memory read";
  1237. break;
  1238. case 2:
  1239. optype = "memory write";
  1240. break;
  1241. case 3:
  1242. optype = "addr/cmd";
  1243. break;
  1244. case 4:
  1245. optype = "memory scrubbing";
  1246. break;
  1247. default:
  1248. optype = "reserved";
  1249. break;
  1250. }
  1251. }
  1252. rc = get_memory_error_data(mci, m->addr, &socket,
  1253. &channel_mask, &rank, area_type);
  1254. if (rc < 0)
  1255. return;
  1256. new_mci = get_mci_for_node_id(socket);
  1257. if (!new_mci) {
  1258. edac_mc_handle_ce_no_info(mci, "Error: socket got corrupted!");
  1259. return;
  1260. }
  1261. mci = new_mci;
  1262. pvt = mci->pvt_info;
  1263. first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
  1264. if (rank < 4)
  1265. dimm = 0;
  1266. else if (rank < 8)
  1267. dimm = 1;
  1268. else
  1269. dimm = 2;
  1270. csrow = pvt->csrow_map[first_channel][dimm];
  1271. if (uncorrected_error && recoverable)
  1272. recoverable_msg = " recoverable";
  1273. else
  1274. recoverable_msg = "";
  1275. /*
  1276. * FIXME: What should we do with "channel" information on mcelog?
  1277. * Probably, we can just discard it, as the channel information
  1278. * comes from the get_memory_error_data() address decoding
  1279. */
  1280. msg = kasprintf(GFP_ATOMIC,
  1281. "%d %s error(s): %s on %s area %s%s: cpu=%d Err=%04x:%04x (ch=%d), "
  1282. "addr = 0x%08llx => socket=%d, Channel=%ld(mask=%ld), rank=%d\n",
  1283. core_err_cnt,
  1284. area_type,
  1285. optype,
  1286. type,
  1287. recoverable_msg,
  1288. overflow ? "OVERFLOW" : "",
  1289. m->cpu,
  1290. mscod, errcode,
  1291. channel, /* 1111b means not specified */
  1292. (long long) m->addr,
  1293. socket,
  1294. first_channel, /* This is the real channel on SB */
  1295. channel_mask,
  1296. rank);
  1297. debugf0("%s", msg);
  1298. /* Call the helper to output message */
  1299. if (uncorrected_error)
  1300. edac_mc_handle_fbd_ue(mci, csrow, 0, 0, msg);
  1301. else
  1302. edac_mc_handle_fbd_ce(mci, csrow, 0, msg);
  1303. kfree(msg);
  1304. }
  1305. /*
  1306. * sbridge_check_error Retrieve and process errors reported by the
  1307. * hardware. Called by the Core module.
  1308. */
  1309. static void sbridge_check_error(struct mem_ctl_info *mci)
  1310. {
  1311. struct sbridge_pvt *pvt = mci->pvt_info;
  1312. int i;
  1313. unsigned count = 0;
  1314. struct mce *m;
  1315. /*
  1316. * MCE first step: Copy all mce errors into a temporary buffer
  1317. * We use a double buffering here, to reduce the risk of
  1318. * loosing an error.
  1319. */
  1320. smp_rmb();
  1321. count = (pvt->mce_out + MCE_LOG_LEN - pvt->mce_in)
  1322. % MCE_LOG_LEN;
  1323. if (!count)
  1324. return;
  1325. m = pvt->mce_outentry;
  1326. if (pvt->mce_in + count > MCE_LOG_LEN) {
  1327. unsigned l = MCE_LOG_LEN - pvt->mce_in;
  1328. memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * l);
  1329. smp_wmb();
  1330. pvt->mce_in = 0;
  1331. count -= l;
  1332. m += l;
  1333. }
  1334. memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * count);
  1335. smp_wmb();
  1336. pvt->mce_in += count;
  1337. smp_rmb();
  1338. if (pvt->mce_overrun) {
  1339. sbridge_printk(KERN_ERR, "Lost %d memory errors\n",
  1340. pvt->mce_overrun);
  1341. smp_wmb();
  1342. pvt->mce_overrun = 0;
  1343. }
  1344. /*
  1345. * MCE second step: parse errors and display
  1346. */
  1347. for (i = 0; i < count; i++)
  1348. sbridge_mce_output_error(mci, &pvt->mce_outentry[i]);
  1349. }
  1350. /*
  1351. * sbridge_mce_check_error Replicates mcelog routine to get errors
  1352. * This routine simply queues mcelog errors, and
  1353. * return. The error itself should be handled later
  1354. * by sbridge_check_error.
  1355. * WARNING: As this routine should be called at NMI time, extra care should
  1356. * be taken to avoid deadlocks, and to be as fast as possible.
  1357. */
  1358. static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
  1359. void *data)
  1360. {
  1361. struct mce *mce = (struct mce *)data;
  1362. struct mem_ctl_info *mci;
  1363. struct sbridge_pvt *pvt;
  1364. mci = get_mci_for_node_id(mce->socketid);
  1365. if (!mci)
  1366. return NOTIFY_BAD;
  1367. pvt = mci->pvt_info;
  1368. /*
  1369. * Just let mcelog handle it if the error is
  1370. * outside the memory controller. A memory error
  1371. * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
  1372. * bit 12 has an special meaning.
  1373. */
  1374. if ((mce->status & 0xefff) >> 7 != 1)
  1375. return NOTIFY_DONE;
  1376. printk("sbridge: HANDLING MCE MEMORY ERROR\n");
  1377. printk("CPU %d: Machine Check Exception: %Lx Bank %d: %016Lx\n",
  1378. mce->extcpu, mce->mcgstatus, mce->bank, mce->status);
  1379. printk("TSC %llx ", mce->tsc);
  1380. printk("ADDR %llx ", mce->addr);
  1381. printk("MISC %llx ", mce->misc);
  1382. printk("PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x\n",
  1383. mce->cpuvendor, mce->cpuid, mce->time,
  1384. mce->socketid, mce->apicid);
  1385. #ifdef CONFIG_SMP
  1386. /* Only handle if it is the right mc controller */
  1387. if (cpu_data(mce->cpu).phys_proc_id != pvt->sbridge_dev->mc)
  1388. return NOTIFY_DONE;
  1389. #endif
  1390. smp_rmb();
  1391. if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
  1392. smp_wmb();
  1393. pvt->mce_overrun++;
  1394. return NOTIFY_DONE;
  1395. }
  1396. /* Copy memory error at the ringbuffer */
  1397. memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
  1398. smp_wmb();
  1399. pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
  1400. /* Handle fatal errors immediately */
  1401. if (mce->mcgstatus & 1)
  1402. sbridge_check_error(mci);
  1403. /* Advice mcelog that the error were handled */
  1404. return NOTIFY_STOP;
  1405. }
  1406. static struct notifier_block sbridge_mce_dec = {
  1407. .notifier_call = sbridge_mce_check_error,
  1408. };
  1409. /****************************************************************************
  1410. EDAC register/unregister logic
  1411. ****************************************************************************/
  1412. static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
  1413. {
  1414. struct mem_ctl_info *mci = sbridge_dev->mci;
  1415. struct sbridge_pvt *pvt;
  1416. if (unlikely(!mci || !mci->pvt_info)) {
  1417. debugf0("MC: " __FILE__ ": %s(): dev = %p\n",
  1418. __func__, &sbridge_dev->pdev[0]->dev);
  1419. sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
  1420. return;
  1421. }
  1422. pvt = mci->pvt_info;
  1423. debugf0("MC: " __FILE__ ": %s(): mci = %p, dev = %p\n",
  1424. __func__, mci, &sbridge_dev->pdev[0]->dev);
  1425. atomic_notifier_chain_unregister(&x86_mce_decoder_chain,
  1426. &sbridge_mce_dec);
  1427. /* Remove MC sysfs nodes */
  1428. edac_mc_del_mc(mci->dev);
  1429. debugf1("%s: free mci struct\n", mci->ctl_name);
  1430. kfree(mci->ctl_name);
  1431. edac_mc_free(mci);
  1432. sbridge_dev->mci = NULL;
  1433. }
  1434. static int sbridge_register_mci(struct sbridge_dev *sbridge_dev)
  1435. {
  1436. struct mem_ctl_info *mci;
  1437. struct sbridge_pvt *pvt;
  1438. int rc, channels, csrows;
  1439. /* Check the number of active and not disabled channels */
  1440. rc = sbridge_get_active_channels(sbridge_dev->bus, &channels, &csrows);
  1441. if (unlikely(rc < 0))
  1442. return rc;
  1443. /* allocate a new MC control structure */
  1444. mci = edac_mc_alloc(sizeof(*pvt), csrows, channels, sbridge_dev->mc);
  1445. if (unlikely(!mci))
  1446. return -ENOMEM;
  1447. debugf0("MC: " __FILE__ ": %s(): mci = %p, dev = %p\n",
  1448. __func__, mci, &sbridge_dev->pdev[0]->dev);
  1449. pvt = mci->pvt_info;
  1450. memset(pvt, 0, sizeof(*pvt));
  1451. /* Associate sbridge_dev and mci for future usage */
  1452. pvt->sbridge_dev = sbridge_dev;
  1453. sbridge_dev->mci = mci;
  1454. mci->mtype_cap = MEM_FLAG_DDR3;
  1455. mci->edac_ctl_cap = EDAC_FLAG_NONE;
  1456. mci->edac_cap = EDAC_FLAG_NONE;
  1457. mci->mod_name = "sbridge_edac.c";
  1458. mci->mod_ver = SBRIDGE_REVISION;
  1459. mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge Socket#%d", mci->mc_idx);
  1460. mci->dev_name = pci_name(sbridge_dev->pdev[0]);
  1461. mci->ctl_page_to_phys = NULL;
  1462. /* Set the function pointer to an actual operation function */
  1463. mci->edac_check = sbridge_check_error;
  1464. /* Store pci devices at mci for faster access */
  1465. rc = mci_bind_devs(mci, sbridge_dev);
  1466. if (unlikely(rc < 0))
  1467. goto fail0;
  1468. /* Get dimm basic config and the memory layout */
  1469. get_dimm_config(mci);
  1470. get_memory_layout(mci);
  1471. /* record ptr to the generic device */
  1472. mci->dev = &sbridge_dev->pdev[0]->dev;
  1473. /* add this new MC control structure to EDAC's list of MCs */
  1474. if (unlikely(edac_mc_add_mc(mci))) {
  1475. debugf0("MC: " __FILE__
  1476. ": %s(): failed edac_mc_add_mc()\n", __func__);
  1477. rc = -EINVAL;
  1478. goto fail0;
  1479. }
  1480. atomic_notifier_chain_register(&x86_mce_decoder_chain,
  1481. &sbridge_mce_dec);
  1482. return 0;
  1483. fail0:
  1484. kfree(mci->ctl_name);
  1485. edac_mc_free(mci);
  1486. sbridge_dev->mci = NULL;
  1487. return rc;
  1488. }
  1489. /*
  1490. * sbridge_probe Probe for ONE instance of device to see if it is
  1491. * present.
  1492. * return:
  1493. * 0 for FOUND a device
  1494. * < 0 for error code
  1495. */
  1496. static int __devinit sbridge_probe(struct pci_dev *pdev,
  1497. const struct pci_device_id *id)
  1498. {
  1499. int rc;
  1500. u8 mc, num_mc = 0;
  1501. struct sbridge_dev *sbridge_dev;
  1502. /* get the pci devices we want to reserve for our use */
  1503. mutex_lock(&sbridge_edac_lock);
  1504. /*
  1505. * All memory controllers are allocated at the first pass.
  1506. */
  1507. if (unlikely(probed >= 1)) {
  1508. mutex_unlock(&sbridge_edac_lock);
  1509. return -ENODEV;
  1510. }
  1511. probed++;
  1512. rc = sbridge_get_all_devices(&num_mc);
  1513. if (unlikely(rc < 0))
  1514. goto fail0;
  1515. mc = 0;
  1516. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  1517. debugf0("Registering MC#%d (%d of %d)\n", mc, mc + 1, num_mc);
  1518. sbridge_dev->mc = mc++;
  1519. rc = sbridge_register_mci(sbridge_dev);
  1520. if (unlikely(rc < 0))
  1521. goto fail1;
  1522. }
  1523. sbridge_printk(KERN_INFO, "Driver loaded.\n");
  1524. mutex_unlock(&sbridge_edac_lock);
  1525. return 0;
  1526. fail1:
  1527. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
  1528. sbridge_unregister_mci(sbridge_dev);
  1529. sbridge_put_all_devices();
  1530. fail0:
  1531. mutex_unlock(&sbridge_edac_lock);
  1532. return rc;
  1533. }
  1534. /*
  1535. * sbridge_remove destructor for one instance of device
  1536. *
  1537. */
  1538. static void __devexit sbridge_remove(struct pci_dev *pdev)
  1539. {
  1540. struct sbridge_dev *sbridge_dev;
  1541. debugf0(__FILE__ ": %s()\n", __func__);
  1542. /*
  1543. * we have a trouble here: pdev value for removal will be wrong, since
  1544. * it will point to the X58 register used to detect that the machine
  1545. * is a Nehalem or upper design. However, due to the way several PCI
  1546. * devices are grouped together to provide MC functionality, we need
  1547. * to use a different method for releasing the devices
  1548. */
  1549. mutex_lock(&sbridge_edac_lock);
  1550. if (unlikely(!probed)) {
  1551. mutex_unlock(&sbridge_edac_lock);
  1552. return;
  1553. }
  1554. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
  1555. sbridge_unregister_mci(sbridge_dev);
  1556. /* Release PCI resources */
  1557. sbridge_put_all_devices();
  1558. probed--;
  1559. mutex_unlock(&sbridge_edac_lock);
  1560. }
  1561. MODULE_DEVICE_TABLE(pci, sbridge_pci_tbl);
  1562. /*
  1563. * sbridge_driver pci_driver structure for this module
  1564. *
  1565. */
  1566. static struct pci_driver sbridge_driver = {
  1567. .name = "sbridge_edac",
  1568. .probe = sbridge_probe,
  1569. .remove = __devexit_p(sbridge_remove),
  1570. .id_table = sbridge_pci_tbl,
  1571. };
  1572. /*
  1573. * sbridge_init Module entry function
  1574. * Try to initialize this module for its devices
  1575. */
  1576. static int __init sbridge_init(void)
  1577. {
  1578. int pci_rc;
  1579. debugf2("MC: " __FILE__ ": %s()\n", __func__);
  1580. /* Ensure that the OPSTATE is set correctly for POLL or NMI */
  1581. opstate_init();
  1582. pci_rc = pci_register_driver(&sbridge_driver);
  1583. if (pci_rc >= 0)
  1584. return 0;
  1585. sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
  1586. pci_rc);
  1587. return pci_rc;
  1588. }
  1589. /*
  1590. * sbridge_exit() Module exit function
  1591. * Unregister the driver
  1592. */
  1593. static void __exit sbridge_exit(void)
  1594. {
  1595. debugf2("MC: " __FILE__ ": %s()\n", __func__);
  1596. pci_unregister_driver(&sbridge_driver);
  1597. }
  1598. module_init(sbridge_init);
  1599. module_exit(sbridge_exit);
  1600. module_param(edac_op_state, int, 0444);
  1601. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
  1602. MODULE_LICENSE("GPL");
  1603. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  1604. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  1605. MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge memory controllers - "
  1606. SBRIDGE_REVISION);