sb_edac.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891
  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. mce_unregister_decode_chain(&sbridge_mce_dec);
  1426. /* Remove MC sysfs nodes */
  1427. edac_mc_del_mc(mci->dev);
  1428. debugf1("%s: free mci struct\n", mci->ctl_name);
  1429. kfree(mci->ctl_name);
  1430. edac_mc_free(mci);
  1431. sbridge_dev->mci = NULL;
  1432. }
  1433. static int sbridge_register_mci(struct sbridge_dev *sbridge_dev)
  1434. {
  1435. struct mem_ctl_info *mci;
  1436. struct sbridge_pvt *pvt;
  1437. int rc, channels, csrows;
  1438. /* Check the number of active and not disabled channels */
  1439. rc = sbridge_get_active_channels(sbridge_dev->bus, &channels, &csrows);
  1440. if (unlikely(rc < 0))
  1441. return rc;
  1442. /* allocate a new MC control structure */
  1443. mci = edac_mc_alloc(sizeof(*pvt), csrows, channels, sbridge_dev->mc);
  1444. if (unlikely(!mci))
  1445. return -ENOMEM;
  1446. debugf0("MC: " __FILE__ ": %s(): mci = %p, dev = %p\n",
  1447. __func__, mci, &sbridge_dev->pdev[0]->dev);
  1448. pvt = mci->pvt_info;
  1449. memset(pvt, 0, sizeof(*pvt));
  1450. /* Associate sbridge_dev and mci for future usage */
  1451. pvt->sbridge_dev = sbridge_dev;
  1452. sbridge_dev->mci = mci;
  1453. mci->mtype_cap = MEM_FLAG_DDR3;
  1454. mci->edac_ctl_cap = EDAC_FLAG_NONE;
  1455. mci->edac_cap = EDAC_FLAG_NONE;
  1456. mci->mod_name = "sbridge_edac.c";
  1457. mci->mod_ver = SBRIDGE_REVISION;
  1458. mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge Socket#%d", mci->mc_idx);
  1459. mci->dev_name = pci_name(sbridge_dev->pdev[0]);
  1460. mci->ctl_page_to_phys = NULL;
  1461. /* Set the function pointer to an actual operation function */
  1462. mci->edac_check = sbridge_check_error;
  1463. /* Store pci devices at mci for faster access */
  1464. rc = mci_bind_devs(mci, sbridge_dev);
  1465. if (unlikely(rc < 0))
  1466. goto fail0;
  1467. /* Get dimm basic config and the memory layout */
  1468. get_dimm_config(mci);
  1469. get_memory_layout(mci);
  1470. /* record ptr to the generic device */
  1471. mci->dev = &sbridge_dev->pdev[0]->dev;
  1472. /* add this new MC control structure to EDAC's list of MCs */
  1473. if (unlikely(edac_mc_add_mc(mci))) {
  1474. debugf0("MC: " __FILE__
  1475. ": %s(): failed edac_mc_add_mc()\n", __func__);
  1476. rc = -EINVAL;
  1477. goto fail0;
  1478. }
  1479. mce_register_decode_chain(&sbridge_mce_dec);
  1480. return 0;
  1481. fail0:
  1482. kfree(mci->ctl_name);
  1483. edac_mc_free(mci);
  1484. sbridge_dev->mci = NULL;
  1485. return rc;
  1486. }
  1487. /*
  1488. * sbridge_probe Probe for ONE instance of device to see if it is
  1489. * present.
  1490. * return:
  1491. * 0 for FOUND a device
  1492. * < 0 for error code
  1493. */
  1494. static int __devinit sbridge_probe(struct pci_dev *pdev,
  1495. const struct pci_device_id *id)
  1496. {
  1497. int rc;
  1498. u8 mc, num_mc = 0;
  1499. struct sbridge_dev *sbridge_dev;
  1500. /* get the pci devices we want to reserve for our use */
  1501. mutex_lock(&sbridge_edac_lock);
  1502. /*
  1503. * All memory controllers are allocated at the first pass.
  1504. */
  1505. if (unlikely(probed >= 1)) {
  1506. mutex_unlock(&sbridge_edac_lock);
  1507. return -ENODEV;
  1508. }
  1509. probed++;
  1510. rc = sbridge_get_all_devices(&num_mc);
  1511. if (unlikely(rc < 0))
  1512. goto fail0;
  1513. mc = 0;
  1514. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  1515. debugf0("Registering MC#%d (%d of %d)\n", mc, mc + 1, num_mc);
  1516. sbridge_dev->mc = mc++;
  1517. rc = sbridge_register_mci(sbridge_dev);
  1518. if (unlikely(rc < 0))
  1519. goto fail1;
  1520. }
  1521. sbridge_printk(KERN_INFO, "Driver loaded.\n");
  1522. mutex_unlock(&sbridge_edac_lock);
  1523. return 0;
  1524. fail1:
  1525. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
  1526. sbridge_unregister_mci(sbridge_dev);
  1527. sbridge_put_all_devices();
  1528. fail0:
  1529. mutex_unlock(&sbridge_edac_lock);
  1530. return rc;
  1531. }
  1532. /*
  1533. * sbridge_remove destructor for one instance of device
  1534. *
  1535. */
  1536. static void __devexit sbridge_remove(struct pci_dev *pdev)
  1537. {
  1538. struct sbridge_dev *sbridge_dev;
  1539. debugf0(__FILE__ ": %s()\n", __func__);
  1540. /*
  1541. * we have a trouble here: pdev value for removal will be wrong, since
  1542. * it will point to the X58 register used to detect that the machine
  1543. * is a Nehalem or upper design. However, due to the way several PCI
  1544. * devices are grouped together to provide MC functionality, we need
  1545. * to use a different method for releasing the devices
  1546. */
  1547. mutex_lock(&sbridge_edac_lock);
  1548. if (unlikely(!probed)) {
  1549. mutex_unlock(&sbridge_edac_lock);
  1550. return;
  1551. }
  1552. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
  1553. sbridge_unregister_mci(sbridge_dev);
  1554. /* Release PCI resources */
  1555. sbridge_put_all_devices();
  1556. probed--;
  1557. mutex_unlock(&sbridge_edac_lock);
  1558. }
  1559. MODULE_DEVICE_TABLE(pci, sbridge_pci_tbl);
  1560. /*
  1561. * sbridge_driver pci_driver structure for this module
  1562. *
  1563. */
  1564. static struct pci_driver sbridge_driver = {
  1565. .name = "sbridge_edac",
  1566. .probe = sbridge_probe,
  1567. .remove = __devexit_p(sbridge_remove),
  1568. .id_table = sbridge_pci_tbl,
  1569. };
  1570. /*
  1571. * sbridge_init Module entry function
  1572. * Try to initialize this module for its devices
  1573. */
  1574. static int __init sbridge_init(void)
  1575. {
  1576. int pci_rc;
  1577. debugf2("MC: " __FILE__ ": %s()\n", __func__);
  1578. /* Ensure that the OPSTATE is set correctly for POLL or NMI */
  1579. opstate_init();
  1580. pci_rc = pci_register_driver(&sbridge_driver);
  1581. if (pci_rc >= 0)
  1582. return 0;
  1583. sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
  1584. pci_rc);
  1585. return pci_rc;
  1586. }
  1587. /*
  1588. * sbridge_exit() Module exit function
  1589. * Unregister the driver
  1590. */
  1591. static void __exit sbridge_exit(void)
  1592. {
  1593. debugf2("MC: " __FILE__ ": %s()\n", __func__);
  1594. pci_unregister_driver(&sbridge_driver);
  1595. }
  1596. module_init(sbridge_init);
  1597. module_exit(sbridge_exit);
  1598. module_param(edac_op_state, int, 0444);
  1599. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
  1600. MODULE_LICENSE("GPL");
  1601. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  1602. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  1603. MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge memory controllers - "
  1604. SBRIDGE_REVISION);