sb_edac.c 44 KB

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