sb_edac.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892
  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. pci_read_config_dword(pvt->pci_br, SAD_TARGET, &reg);
  454. pvt->sbridge_dev->source_id = SOURCE_ID(reg);
  455. pci_read_config_dword(pvt->pci_br, SAD_CONTROL, &reg);
  456. pvt->sbridge_dev->node_id = NODE_ID(reg);
  457. debugf0("mc#%d: Node ID: %d, source ID: %d\n",
  458. pvt->sbridge_dev->mc,
  459. pvt->sbridge_dev->node_id,
  460. pvt->sbridge_dev->source_id);
  461. pci_read_config_dword(pvt->pci_ras, RASENABLES, &reg);
  462. if (IS_MIRROR_ENABLED(reg)) {
  463. debugf0("Memory mirror is enabled\n");
  464. pvt->is_mirrored = true;
  465. } else {
  466. debugf0("Memory mirror is disabled\n");
  467. pvt->is_mirrored = false;
  468. }
  469. pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
  470. if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
  471. debugf0("Lockstep is enabled\n");
  472. mode = EDAC_S8ECD8ED;
  473. pvt->is_lockstep = true;
  474. } else {
  475. debugf0("Lockstep is disabled\n");
  476. mode = EDAC_S4ECD4ED;
  477. pvt->is_lockstep = false;
  478. }
  479. if (IS_CLOSE_PG(pvt->info.mcmtr)) {
  480. debugf0("address map is on closed page mode\n");
  481. pvt->is_close_pg = true;
  482. } else {
  483. debugf0("address map is on open page mode\n");
  484. pvt->is_close_pg = false;
  485. }
  486. pci_read_config_dword(pvt->pci_ta, RANK_CFG_A, &reg);
  487. if (IS_RDIMM_ENABLED(reg)) {
  488. /* FIXME: Can also be LRDIMM */
  489. debugf0("Memory is registered\n");
  490. mode = MEM_RDDR3;
  491. } else {
  492. debugf0("Memory is unregistered\n");
  493. mode = MEM_DDR3;
  494. }
  495. /* On all supported DDR3 DIMM types, there are 8 banks available */
  496. banks = 8;
  497. for (i = 0; i < NUM_CHANNELS; i++) {
  498. u32 mtr;
  499. for (j = 0; j < ARRAY_SIZE(mtr_regs); j++) {
  500. pci_read_config_dword(pvt->pci_tad[i],
  501. mtr_regs[j], &mtr);
  502. debugf4("Channel #%d MTR%d = %x\n", i, j, mtr);
  503. if (IS_DIMM_PRESENT(mtr)) {
  504. pvt->channel[i].dimms++;
  505. ranks = numrank(mtr);
  506. rows = numrow(mtr);
  507. cols = numcol(mtr);
  508. /* DDR3 has 8 I/O banks */
  509. size = (rows * cols * banks * ranks) >> (20 - 3);
  510. npages = MiB_TO_PAGES(size);
  511. debugf0("mc#%d: channel %d, dimm %d, %d Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
  512. pvt->sbridge_dev->mc, i, j,
  513. size, npages,
  514. banks, ranks, rows, cols);
  515. csr = &mci->csrows[csrow];
  516. csr->first_page = last_page;
  517. csr->last_page = last_page + npages - 1;
  518. csr->page_mask = 0UL; /* Unused */
  519. csr->nr_pages = npages;
  520. csr->grain = 32;
  521. csr->csrow_idx = csrow;
  522. csr->dtype = (banks == 8) ? DEV_X8 : DEV_X4;
  523. csr->ce_count = 0;
  524. csr->ue_count = 0;
  525. csr->mtype = mode;
  526. csr->edac_mode = mode;
  527. csr->nr_channels = 1;
  528. csr->channels[0].chan_idx = i;
  529. csr->channels[0].ce_count = 0;
  530. pvt->csrow_map[i][j] = csrow;
  531. snprintf(csr->channels[0].label,
  532. sizeof(csr->channels[0].label),
  533. "CPU_SrcID#%u_Channel#%u_DIMM#%u",
  534. pvt->sbridge_dev->source_id, i, j);
  535. last_page += npages;
  536. csrow++;
  537. }
  538. }
  539. }
  540. return 0;
  541. }
  542. static void get_memory_layout(const struct mem_ctl_info *mci)
  543. {
  544. struct sbridge_pvt *pvt = mci->pvt_info;
  545. int i, j, k, n_sads, n_tads, sad_interl;
  546. u32 reg;
  547. u64 limit, prv = 0;
  548. u64 tmp_mb;
  549. u32 rir_way;
  550. /*
  551. * Step 1) Get TOLM/TOHM ranges
  552. */
  553. /* Address range is 32:28 */
  554. pci_read_config_dword(pvt->pci_sad1, TOLM,
  555. &reg);
  556. pvt->tolm = GET_TOLM(reg);
  557. tmp_mb = (1 + pvt->tolm) >> 20;
  558. debugf0("TOLM: %Lu.%03Lu GB (0x%016Lx)\n",
  559. tmp_mb / 1000, tmp_mb % 1000, (u64)pvt->tolm);
  560. /* Address range is already 45:25 */
  561. pci_read_config_dword(pvt->pci_sad1, TOHM,
  562. &reg);
  563. pvt->tohm = GET_TOHM(reg);
  564. tmp_mb = (1 + pvt->tohm) >> 20;
  565. debugf0("TOHM: %Lu.%03Lu GB (0x%016Lx)",
  566. tmp_mb / 1000, tmp_mb % 1000, (u64)pvt->tohm);
  567. /*
  568. * Step 2) Get SAD range and SAD Interleave list
  569. * TAD registers contain the interleave wayness. However, it
  570. * seems simpler to just discover it indirectly, with the
  571. * algorithm bellow.
  572. */
  573. prv = 0;
  574. for (n_sads = 0; n_sads < MAX_SAD; n_sads++) {
  575. /* SAD_LIMIT Address range is 45:26 */
  576. pci_read_config_dword(pvt->pci_sad0, dram_rule[n_sads],
  577. &reg);
  578. limit = SAD_LIMIT(reg);
  579. if (!DRAM_RULE_ENABLE(reg))
  580. continue;
  581. if (limit <= prv)
  582. break;
  583. tmp_mb = (limit + 1) >> 20;
  584. debugf0("SAD#%d %s up to %Lu.%03Lu GB (0x%016Lx) %s reg=0x%08x\n",
  585. n_sads,
  586. get_dram_attr(reg),
  587. tmp_mb / 1000, tmp_mb % 1000,
  588. ((u64)tmp_mb) << 20L,
  589. INTERLEAVE_MODE(reg) ? "Interleave: 8:6" : "Interleave: [8:6]XOR[18:16]",
  590. reg);
  591. prv = limit;
  592. pci_read_config_dword(pvt->pci_sad0, interleave_list[n_sads],
  593. &reg);
  594. sad_interl = sad_pkg(reg, 0);
  595. for (j = 0; j < 8; j++) {
  596. if (j > 0 && sad_interl == sad_pkg(reg, j))
  597. break;
  598. debugf0("SAD#%d, interleave #%d: %d\n",
  599. n_sads, j, sad_pkg(reg, j));
  600. }
  601. }
  602. /*
  603. * Step 3) Get TAD range
  604. */
  605. prv = 0;
  606. for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
  607. pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
  608. &reg);
  609. limit = TAD_LIMIT(reg);
  610. if (limit <= prv)
  611. break;
  612. tmp_mb = (limit + 1) >> 20;
  613. 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",
  614. n_tads, tmp_mb / 1000, tmp_mb % 1000,
  615. ((u64)tmp_mb) << 20L,
  616. (u32)TAD_SOCK(reg),
  617. (u32)TAD_CH(reg),
  618. (u32)TAD_TGT0(reg),
  619. (u32)TAD_TGT1(reg),
  620. (u32)TAD_TGT2(reg),
  621. (u32)TAD_TGT3(reg),
  622. reg);
  623. prv = tmp_mb;
  624. }
  625. /*
  626. * Step 4) Get TAD offsets, per each channel
  627. */
  628. for (i = 0; i < NUM_CHANNELS; i++) {
  629. if (!pvt->channel[i].dimms)
  630. continue;
  631. for (j = 0; j < n_tads; j++) {
  632. pci_read_config_dword(pvt->pci_tad[i],
  633. tad_ch_nilv_offset[j],
  634. &reg);
  635. tmp_mb = TAD_OFFSET(reg) >> 20;
  636. debugf0("TAD CH#%d, offset #%d: %Lu.%03Lu GB (0x%016Lx), reg=0x%08x\n",
  637. i, j,
  638. tmp_mb / 1000, tmp_mb % 1000,
  639. ((u64)tmp_mb) << 20L,
  640. reg);
  641. }
  642. }
  643. /*
  644. * Step 6) Get RIR Wayness/Limit, per each channel
  645. */
  646. for (i = 0; i < NUM_CHANNELS; i++) {
  647. if (!pvt->channel[i].dimms)
  648. continue;
  649. for (j = 0; j < MAX_RIR_RANGES; j++) {
  650. pci_read_config_dword(pvt->pci_tad[i],
  651. rir_way_limit[j],
  652. &reg);
  653. if (!IS_RIR_VALID(reg))
  654. continue;
  655. tmp_mb = RIR_LIMIT(reg) >> 20;
  656. rir_way = 1 << RIR_WAY(reg);
  657. debugf0("CH#%d RIR#%d, limit: %Lu.%03Lu GB (0x%016Lx), way: %d, reg=0x%08x\n",
  658. i, j,
  659. tmp_mb / 1000, tmp_mb % 1000,
  660. ((u64)tmp_mb) << 20L,
  661. rir_way,
  662. reg);
  663. for (k = 0; k < rir_way; k++) {
  664. pci_read_config_dword(pvt->pci_tad[i],
  665. rir_offset[j][k],
  666. &reg);
  667. tmp_mb = RIR_OFFSET(reg) << 6;
  668. debugf0("CH#%d RIR#%d INTL#%d, offset %Lu.%03Lu GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
  669. i, j, k,
  670. tmp_mb / 1000, tmp_mb % 1000,
  671. ((u64)tmp_mb) << 20L,
  672. (u32)RIR_RNK_TGT(reg),
  673. reg);
  674. }
  675. }
  676. }
  677. }
  678. struct mem_ctl_info *get_mci_for_node_id(u8 node_id)
  679. {
  680. struct sbridge_dev *sbridge_dev;
  681. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  682. if (sbridge_dev->node_id == node_id)
  683. return sbridge_dev->mci;
  684. }
  685. return NULL;
  686. }
  687. static int get_memory_error_data(struct mem_ctl_info *mci,
  688. u64 addr,
  689. u8 *socket,
  690. long *channel_mask,
  691. u8 *rank,
  692. char *area_type)
  693. {
  694. struct mem_ctl_info *new_mci;
  695. struct sbridge_pvt *pvt = mci->pvt_info;
  696. char msg[256];
  697. int n_rir, n_sads, n_tads, sad_way, sck_xch;
  698. int sad_interl, idx, base_ch;
  699. int interleave_mode;
  700. unsigned sad_interleave[MAX_INTERLEAVE];
  701. u32 reg;
  702. u8 ch_way,sck_way;
  703. u32 tad_offset;
  704. u32 rir_way;
  705. u64 ch_addr, offset, limit, prv = 0;
  706. /*
  707. * Step 0) Check if the address is at special memory ranges
  708. * The check bellow is probably enough to fill all cases where
  709. * the error is not inside a memory, except for the legacy
  710. * range (e. g. VGA addresses). It is unlikely, however, that the
  711. * memory controller would generate an error on that range.
  712. */
  713. if ((addr > (u64) pvt->tolm) && (addr < (1L << 32))) {
  714. sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
  715. edac_mc_handle_ce_no_info(mci, msg);
  716. return -EINVAL;
  717. }
  718. if (addr >= (u64)pvt->tohm) {
  719. sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
  720. edac_mc_handle_ce_no_info(mci, msg);
  721. return -EINVAL;
  722. }
  723. /*
  724. * Step 1) Get socket
  725. */
  726. for (n_sads = 0; n_sads < MAX_SAD; n_sads++) {
  727. pci_read_config_dword(pvt->pci_sad0, dram_rule[n_sads],
  728. &reg);
  729. if (!DRAM_RULE_ENABLE(reg))
  730. continue;
  731. limit = SAD_LIMIT(reg);
  732. if (limit <= prv) {
  733. sprintf(msg, "Can't discover the memory socket");
  734. edac_mc_handle_ce_no_info(mci, msg);
  735. return -EINVAL;
  736. }
  737. if (addr <= limit)
  738. break;
  739. prv = limit;
  740. }
  741. if (n_sads == MAX_SAD) {
  742. sprintf(msg, "Can't discover the memory socket");
  743. edac_mc_handle_ce_no_info(mci, msg);
  744. return -EINVAL;
  745. }
  746. area_type = get_dram_attr(reg);
  747. interleave_mode = INTERLEAVE_MODE(reg);
  748. pci_read_config_dword(pvt->pci_sad0, interleave_list[n_sads],
  749. &reg);
  750. sad_interl = sad_pkg(reg, 0);
  751. for (sad_way = 0; sad_way < 8; sad_way++) {
  752. if (sad_way > 0 && sad_interl == sad_pkg(reg, sad_way))
  753. break;
  754. sad_interleave[sad_way] = sad_pkg(reg, sad_way);
  755. debugf0("SAD interleave #%d: %d\n",
  756. sad_way, sad_interleave[sad_way]);
  757. }
  758. debugf0("mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
  759. pvt->sbridge_dev->mc,
  760. n_sads,
  761. addr,
  762. limit,
  763. sad_way + 7,
  764. INTERLEAVE_MODE(reg) ? "" : "XOR[18:16]");
  765. if (interleave_mode)
  766. idx = ((addr >> 6) ^ (addr >> 16)) & 7;
  767. else
  768. idx = (addr >> 6) & 7;
  769. switch (sad_way) {
  770. case 1:
  771. idx = 0;
  772. break;
  773. case 2:
  774. idx = idx & 1;
  775. break;
  776. case 4:
  777. idx = idx & 3;
  778. break;
  779. case 8:
  780. break;
  781. default:
  782. sprintf(msg, "Can't discover socket interleave");
  783. edac_mc_handle_ce_no_info(mci, msg);
  784. return -EINVAL;
  785. }
  786. *socket = sad_interleave[idx];
  787. debugf0("SAD interleave index: %d (wayness %d) = CPU socket %d\n",
  788. idx, sad_way, *socket);
  789. /*
  790. * Move to the proper node structure, in order to access the
  791. * right PCI registers
  792. */
  793. new_mci = get_mci_for_node_id(*socket);
  794. if (!new_mci) {
  795. sprintf(msg, "Struct for socket #%u wasn't initialized",
  796. *socket);
  797. edac_mc_handle_ce_no_info(mci, msg);
  798. return -EINVAL;
  799. }
  800. mci = new_mci;
  801. pvt = mci->pvt_info;
  802. /*
  803. * Step 2) Get memory channel
  804. */
  805. prv = 0;
  806. for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
  807. pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
  808. &reg);
  809. limit = TAD_LIMIT(reg);
  810. if (limit <= prv) {
  811. sprintf(msg, "Can't discover the memory channel");
  812. edac_mc_handle_ce_no_info(mci, msg);
  813. return -EINVAL;
  814. }
  815. if (addr <= limit)
  816. break;
  817. prv = limit;
  818. }
  819. ch_way = TAD_CH(reg) + 1;
  820. sck_way = TAD_SOCK(reg) + 1;
  821. /*
  822. * FIXME: Is it right to always use channel 0 for offsets?
  823. */
  824. pci_read_config_dword(pvt->pci_tad[0],
  825. tad_ch_nilv_offset[n_tads],
  826. &tad_offset);
  827. if (ch_way == 3)
  828. idx = addr >> 6;
  829. else
  830. idx = addr >> (6 + sck_way);
  831. idx = idx % ch_way;
  832. /*
  833. * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
  834. */
  835. switch (idx) {
  836. case 0:
  837. base_ch = TAD_TGT0(reg);
  838. break;
  839. case 1:
  840. base_ch = TAD_TGT1(reg);
  841. break;
  842. case 2:
  843. base_ch = TAD_TGT2(reg);
  844. break;
  845. case 3:
  846. base_ch = TAD_TGT3(reg);
  847. break;
  848. default:
  849. sprintf(msg, "Can't discover the TAD target");
  850. edac_mc_handle_ce_no_info(mci, msg);
  851. return -EINVAL;
  852. }
  853. *channel_mask = 1 << base_ch;
  854. if (pvt->is_mirrored) {
  855. *channel_mask |= 1 << ((base_ch + 2) % 4);
  856. switch(ch_way) {
  857. case 2:
  858. case 4:
  859. sck_xch = 1 << sck_way * (ch_way >> 1);
  860. break;
  861. default:
  862. sprintf(msg, "Invalid mirror set. Can't decode addr");
  863. edac_mc_handle_ce_no_info(mci, msg);
  864. return -EINVAL;
  865. }
  866. } else
  867. sck_xch = (1 << sck_way) * ch_way;
  868. if (pvt->is_lockstep)
  869. *channel_mask |= 1 << ((base_ch + 1) % 4);
  870. offset = TAD_OFFSET(tad_offset);
  871. 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",
  872. n_tads,
  873. addr,
  874. limit,
  875. (u32)TAD_SOCK(reg),
  876. ch_way,
  877. offset,
  878. idx,
  879. base_ch,
  880. *channel_mask);
  881. /* Calculate channel address */
  882. /* Remove the TAD offset */
  883. if (offset > addr) {
  884. sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
  885. offset, addr);
  886. edac_mc_handle_ce_no_info(mci, msg);
  887. return -EINVAL;
  888. }
  889. addr -= offset;
  890. /* Store the low bits [0:6] of the addr */
  891. ch_addr = addr & 0x7f;
  892. /* Remove socket wayness and remove 6 bits */
  893. addr >>= 6;
  894. addr /= sck_xch;
  895. #if 0
  896. /* Divide by channel way */
  897. addr = addr / ch_way;
  898. #endif
  899. /* Recover the last 6 bits */
  900. ch_addr |= addr << 6;
  901. /*
  902. * Step 3) Decode rank
  903. */
  904. for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
  905. pci_read_config_dword(pvt->pci_tad[base_ch],
  906. rir_way_limit[n_rir],
  907. &reg);
  908. if (!IS_RIR_VALID(reg))
  909. continue;
  910. limit = RIR_LIMIT(reg);
  911. debugf0("RIR#%d, limit: %Lu.%03Lu GB (0x%016Lx), way: %d\n",
  912. n_rir,
  913. (limit >> 20) / 1000, (limit >> 20) % 1000,
  914. limit,
  915. 1 << RIR_WAY(reg));
  916. if (ch_addr <= limit)
  917. break;
  918. }
  919. if (n_rir == MAX_RIR_RANGES) {
  920. sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
  921. ch_addr);
  922. edac_mc_handle_ce_no_info(mci, msg);
  923. return -EINVAL;
  924. }
  925. rir_way = RIR_WAY(reg);
  926. if (pvt->is_close_pg)
  927. idx = (ch_addr >> 6);
  928. else
  929. idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
  930. idx %= 1 << rir_way;
  931. pci_read_config_dword(pvt->pci_tad[base_ch],
  932. rir_offset[n_rir][idx],
  933. &reg);
  934. *rank = RIR_RNK_TGT(reg);
  935. debugf0("RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
  936. n_rir,
  937. ch_addr,
  938. limit,
  939. rir_way,
  940. idx);
  941. return 0;
  942. }
  943. /****************************************************************************
  944. Device initialization routines: put/get, init/exit
  945. ****************************************************************************/
  946. /*
  947. * sbridge_put_all_devices 'put' all the devices that we have
  948. * reserved via 'get'
  949. */
  950. static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
  951. {
  952. int i;
  953. debugf0(__FILE__ ": %s()\n", __func__);
  954. for (i = 0; i < sbridge_dev->n_devs; i++) {
  955. struct pci_dev *pdev = sbridge_dev->pdev[i];
  956. if (!pdev)
  957. continue;
  958. debugf0("Removing dev %02x:%02x.%d\n",
  959. pdev->bus->number,
  960. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  961. pci_dev_put(pdev);
  962. }
  963. }
  964. static void sbridge_put_all_devices(void)
  965. {
  966. struct sbridge_dev *sbridge_dev, *tmp;
  967. list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
  968. sbridge_put_devices(sbridge_dev);
  969. free_sbridge_dev(sbridge_dev);
  970. }
  971. }
  972. /*
  973. * sbridge_get_all_devices Find and perform 'get' operation on the MCH's
  974. * device/functions we want to reference for this driver
  975. *
  976. * Need to 'get' device 16 func 1 and func 2
  977. */
  978. static int sbridge_get_onedevice(struct pci_dev **prev,
  979. u8 *num_mc,
  980. const struct pci_id_table *table,
  981. const unsigned devno)
  982. {
  983. struct sbridge_dev *sbridge_dev;
  984. const struct pci_id_descr *dev_descr = &table->descr[devno];
  985. struct pci_dev *pdev = NULL;
  986. u8 bus = 0;
  987. sbridge_printk(KERN_INFO,
  988. "Seeking for: dev %02x.%d PCI ID %04x:%04x\n",
  989. dev_descr->dev, dev_descr->func,
  990. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  991. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  992. dev_descr->dev_id, *prev);
  993. if (!pdev) {
  994. if (*prev) {
  995. *prev = pdev;
  996. return 0;
  997. }
  998. if (dev_descr->optional)
  999. return 0;
  1000. if (devno == 0)
  1001. return -ENODEV;
  1002. sbridge_printk(KERN_INFO,
  1003. "Device not found: dev %02x.%d PCI ID %04x:%04x\n",
  1004. dev_descr->dev, dev_descr->func,
  1005. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1006. /* End of list, leave */
  1007. return -ENODEV;
  1008. }
  1009. bus = pdev->bus->number;
  1010. sbridge_dev = get_sbridge_dev(bus);
  1011. if (!sbridge_dev) {
  1012. sbridge_dev = alloc_sbridge_dev(bus, table);
  1013. if (!sbridge_dev) {
  1014. pci_dev_put(pdev);
  1015. return -ENOMEM;
  1016. }
  1017. (*num_mc)++;
  1018. }
  1019. if (sbridge_dev->pdev[devno]) {
  1020. sbridge_printk(KERN_ERR,
  1021. "Duplicated device for "
  1022. "dev %02x:%d.%d PCI ID %04x:%04x\n",
  1023. bus, dev_descr->dev, dev_descr->func,
  1024. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1025. pci_dev_put(pdev);
  1026. return -ENODEV;
  1027. }
  1028. sbridge_dev->pdev[devno] = pdev;
  1029. /* Sanity check */
  1030. if (unlikely(PCI_SLOT(pdev->devfn) != dev_descr->dev ||
  1031. PCI_FUNC(pdev->devfn) != dev_descr->func)) {
  1032. sbridge_printk(KERN_ERR,
  1033. "Device PCI ID %04x:%04x "
  1034. "has dev %02x:%d.%d instead of dev %02x:%02x.%d\n",
  1035. PCI_VENDOR_ID_INTEL, dev_descr->dev_id,
  1036. bus, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1037. bus, dev_descr->dev, dev_descr->func);
  1038. return -ENODEV;
  1039. }
  1040. /* Be sure that the device is enabled */
  1041. if (unlikely(pci_enable_device(pdev) < 0)) {
  1042. sbridge_printk(KERN_ERR,
  1043. "Couldn't enable "
  1044. "dev %02x:%d.%d PCI ID %04x:%04x\n",
  1045. bus, dev_descr->dev, dev_descr->func,
  1046. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1047. return -ENODEV;
  1048. }
  1049. debugf0("Detected dev %02x:%d.%d PCI ID %04x:%04x\n",
  1050. bus, dev_descr->dev,
  1051. dev_descr->func,
  1052. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1053. /*
  1054. * As stated on drivers/pci/search.c, the reference count for
  1055. * @from is always decremented if it is not %NULL. So, as we need
  1056. * to get all devices up to null, we need to do a get for the device
  1057. */
  1058. pci_dev_get(pdev);
  1059. *prev = pdev;
  1060. return 0;
  1061. }
  1062. static int sbridge_get_all_devices(u8 *num_mc)
  1063. {
  1064. int i, rc;
  1065. struct pci_dev *pdev = NULL;
  1066. const struct pci_id_table *table = pci_dev_descr_sbridge_table;
  1067. while (table && table->descr) {
  1068. for (i = 0; i < table->n_devs; i++) {
  1069. pdev = NULL;
  1070. do {
  1071. rc = sbridge_get_onedevice(&pdev, num_mc,
  1072. table, i);
  1073. if (rc < 0) {
  1074. if (i == 0) {
  1075. i = table->n_devs;
  1076. break;
  1077. }
  1078. sbridge_put_all_devices();
  1079. return -ENODEV;
  1080. }
  1081. } while (pdev);
  1082. }
  1083. table++;
  1084. }
  1085. return 0;
  1086. }
  1087. static int mci_bind_devs(struct mem_ctl_info *mci,
  1088. struct sbridge_dev *sbridge_dev)
  1089. {
  1090. struct sbridge_pvt *pvt = mci->pvt_info;
  1091. struct pci_dev *pdev;
  1092. int i, func, slot;
  1093. for (i = 0; i < sbridge_dev->n_devs; i++) {
  1094. pdev = sbridge_dev->pdev[i];
  1095. if (!pdev)
  1096. continue;
  1097. slot = PCI_SLOT(pdev->devfn);
  1098. func = PCI_FUNC(pdev->devfn);
  1099. switch (slot) {
  1100. case 12:
  1101. switch (func) {
  1102. case 6:
  1103. pvt->pci_sad0 = pdev;
  1104. break;
  1105. case 7:
  1106. pvt->pci_sad1 = pdev;
  1107. break;
  1108. default:
  1109. goto error;
  1110. }
  1111. break;
  1112. case 13:
  1113. switch (func) {
  1114. case 6:
  1115. pvt->pci_br = pdev;
  1116. break;
  1117. default:
  1118. goto error;
  1119. }
  1120. break;
  1121. case 14:
  1122. switch (func) {
  1123. case 0:
  1124. pvt->pci_ha0 = pdev;
  1125. break;
  1126. default:
  1127. goto error;
  1128. }
  1129. break;
  1130. case 15:
  1131. switch (func) {
  1132. case 0:
  1133. pvt->pci_ta = pdev;
  1134. break;
  1135. case 1:
  1136. pvt->pci_ras = pdev;
  1137. break;
  1138. case 2:
  1139. case 3:
  1140. case 4:
  1141. case 5:
  1142. pvt->pci_tad[func - 2] = pdev;
  1143. break;
  1144. default:
  1145. goto error;
  1146. }
  1147. break;
  1148. case 17:
  1149. switch (func) {
  1150. case 0:
  1151. pvt->pci_ddrio = pdev;
  1152. break;
  1153. default:
  1154. goto error;
  1155. }
  1156. break;
  1157. default:
  1158. goto error;
  1159. }
  1160. debugf0("Associated PCI %02x.%02d.%d with dev = %p\n",
  1161. sbridge_dev->bus,
  1162. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1163. pdev);
  1164. }
  1165. /* Check if everything were registered */
  1166. if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha0 ||
  1167. !pvt-> pci_tad || !pvt->pci_ras || !pvt->pci_ta ||
  1168. !pvt->pci_ddrio)
  1169. goto enodev;
  1170. for (i = 0; i < NUM_CHANNELS; i++) {
  1171. if (!pvt->pci_tad[i])
  1172. goto enodev;
  1173. }
  1174. return 0;
  1175. enodev:
  1176. sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
  1177. return -ENODEV;
  1178. error:
  1179. sbridge_printk(KERN_ERR, "Device %d, function %d "
  1180. "is out of the expected range\n",
  1181. slot, func);
  1182. return -EINVAL;
  1183. }
  1184. /****************************************************************************
  1185. Error check routines
  1186. ****************************************************************************/
  1187. /*
  1188. * While Sandy Bridge has error count registers, SMI BIOS read values from
  1189. * and resets the counters. So, they are not reliable for the OS to read
  1190. * from them. So, we have no option but to just trust on whatever MCE is
  1191. * telling us about the errors.
  1192. */
  1193. static void sbridge_mce_output_error(struct mem_ctl_info *mci,
  1194. const struct mce *m)
  1195. {
  1196. struct mem_ctl_info *new_mci;
  1197. struct sbridge_pvt *pvt = mci->pvt_info;
  1198. char *type, *optype, *msg, *recoverable_msg;
  1199. bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
  1200. bool overflow = GET_BITFIELD(m->status, 62, 62);
  1201. bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
  1202. bool recoverable = GET_BITFIELD(m->status, 56, 56);
  1203. u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
  1204. u32 mscod = GET_BITFIELD(m->status, 16, 31);
  1205. u32 errcode = GET_BITFIELD(m->status, 0, 15);
  1206. u32 channel = GET_BITFIELD(m->status, 0, 3);
  1207. u32 optypenum = GET_BITFIELD(m->status, 4, 6);
  1208. long channel_mask, first_channel;
  1209. u8 rank, socket;
  1210. int csrow, rc, dimm;
  1211. char *area_type = "Unknown";
  1212. if (ripv)
  1213. type = "NON_FATAL";
  1214. else
  1215. type = "FATAL";
  1216. /*
  1217. * According with Table 15-9 of the Intel Archictecture spec vol 3A,
  1218. * memory errors should fit in this mask:
  1219. * 000f 0000 1mmm cccc (binary)
  1220. * where:
  1221. * f = Correction Report Filtering Bit. If 1, subsequent errors
  1222. * won't be shown
  1223. * mmm = error type
  1224. * cccc = channel
  1225. * If the mask doesn't match, report an error to the parsing logic
  1226. */
  1227. if (! ((errcode & 0xef80) == 0x80)) {
  1228. optype = "Can't parse: it is not a mem";
  1229. } else {
  1230. switch (optypenum) {
  1231. case 0:
  1232. optype = "generic undef request";
  1233. break;
  1234. case 1:
  1235. optype = "memory read";
  1236. break;
  1237. case 2:
  1238. optype = "memory write";
  1239. break;
  1240. case 3:
  1241. optype = "addr/cmd";
  1242. break;
  1243. case 4:
  1244. optype = "memory scrubbing";
  1245. break;
  1246. default:
  1247. optype = "reserved";
  1248. break;
  1249. }
  1250. }
  1251. rc = get_memory_error_data(mci, m->addr, &socket,
  1252. &channel_mask, &rank, area_type);
  1253. if (rc < 0)
  1254. return;
  1255. new_mci = get_mci_for_node_id(socket);
  1256. if (!new_mci) {
  1257. edac_mc_handle_ce_no_info(mci, "Error: socket got corrupted!");
  1258. return;
  1259. }
  1260. mci = new_mci;
  1261. pvt = mci->pvt_info;
  1262. first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
  1263. if (rank < 4)
  1264. dimm = 0;
  1265. else if (rank < 8)
  1266. dimm = 1;
  1267. else
  1268. dimm = 2;
  1269. csrow = pvt->csrow_map[first_channel][dimm];
  1270. if (uncorrected_error && recoverable)
  1271. recoverable_msg = " recoverable";
  1272. else
  1273. recoverable_msg = "";
  1274. /*
  1275. * FIXME: What should we do with "channel" information on mcelog?
  1276. * Probably, we can just discard it, as the channel information
  1277. * comes from the get_memory_error_data() address decoding
  1278. */
  1279. msg = kasprintf(GFP_ATOMIC,
  1280. "%d %s error(s): %s on %s area %s%s: cpu=%d Err=%04x:%04x (ch=%d), "
  1281. "addr = 0x%08llx => socket=%d, Channel=%ld(mask=%ld), rank=%d\n",
  1282. core_err_cnt,
  1283. area_type,
  1284. optype,
  1285. type,
  1286. recoverable_msg,
  1287. overflow ? "OVERFLOW" : "",
  1288. m->cpu,
  1289. mscod, errcode,
  1290. channel, /* 1111b means not specified */
  1291. (long long) m->addr,
  1292. socket,
  1293. first_channel, /* This is the real channel on SB */
  1294. channel_mask,
  1295. rank);
  1296. debugf0("%s", msg);
  1297. /* Call the helper to output message */
  1298. if (uncorrected_error)
  1299. edac_mc_handle_fbd_ue(mci, csrow, 0, 0, msg);
  1300. else
  1301. edac_mc_handle_fbd_ce(mci, csrow, 0, msg);
  1302. kfree(msg);
  1303. }
  1304. /*
  1305. * sbridge_check_error Retrieve and process errors reported by the
  1306. * hardware. Called by the Core module.
  1307. */
  1308. static void sbridge_check_error(struct mem_ctl_info *mci)
  1309. {
  1310. struct sbridge_pvt *pvt = mci->pvt_info;
  1311. int i;
  1312. unsigned count = 0;
  1313. struct mce *m;
  1314. /*
  1315. * MCE first step: Copy all mce errors into a temporary buffer
  1316. * We use a double buffering here, to reduce the risk of
  1317. * loosing an error.
  1318. */
  1319. smp_rmb();
  1320. count = (pvt->mce_out + MCE_LOG_LEN - pvt->mce_in)
  1321. % MCE_LOG_LEN;
  1322. if (!count)
  1323. return;
  1324. m = pvt->mce_outentry;
  1325. if (pvt->mce_in + count > MCE_LOG_LEN) {
  1326. unsigned l = MCE_LOG_LEN - pvt->mce_in;
  1327. memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * l);
  1328. smp_wmb();
  1329. pvt->mce_in = 0;
  1330. count -= l;
  1331. m += l;
  1332. }
  1333. memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * count);
  1334. smp_wmb();
  1335. pvt->mce_in += count;
  1336. smp_rmb();
  1337. if (pvt->mce_overrun) {
  1338. sbridge_printk(KERN_ERR, "Lost %d memory errors\n",
  1339. pvt->mce_overrun);
  1340. smp_wmb();
  1341. pvt->mce_overrun = 0;
  1342. }
  1343. /*
  1344. * MCE second step: parse errors and display
  1345. */
  1346. for (i = 0; i < count; i++)
  1347. sbridge_mce_output_error(mci, &pvt->mce_outentry[i]);
  1348. }
  1349. /*
  1350. * sbridge_mce_check_error Replicates mcelog routine to get errors
  1351. * This routine simply queues mcelog errors, and
  1352. * return. The error itself should be handled later
  1353. * by sbridge_check_error.
  1354. * WARNING: As this routine should be called at NMI time, extra care should
  1355. * be taken to avoid deadlocks, and to be as fast as possible.
  1356. */
  1357. static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
  1358. void *data)
  1359. {
  1360. struct mce *mce = (struct mce *)data;
  1361. struct mem_ctl_info *mci;
  1362. struct sbridge_pvt *pvt;
  1363. mci = get_mci_for_node_id(mce->socketid);
  1364. if (!mci)
  1365. return NOTIFY_BAD;
  1366. pvt = mci->pvt_info;
  1367. /*
  1368. * Just let mcelog handle it if the error is
  1369. * outside the memory controller. A memory error
  1370. * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
  1371. * bit 12 has an special meaning.
  1372. */
  1373. if ((mce->status & 0xefff) >> 7 != 1)
  1374. return NOTIFY_DONE;
  1375. printk("sbridge: HANDLING MCE MEMORY ERROR\n");
  1376. printk("CPU %d: Machine Check Exception: %Lx Bank %d: %016Lx\n",
  1377. mce->extcpu, mce->mcgstatus, mce->bank, mce->status);
  1378. printk("TSC %llx ", mce->tsc);
  1379. printk("ADDR %llx ", mce->addr);
  1380. printk("MISC %llx ", mce->misc);
  1381. printk("PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x\n",
  1382. mce->cpuvendor, mce->cpuid, mce->time,
  1383. mce->socketid, mce->apicid);
  1384. #ifdef CONFIG_SMP
  1385. /* Only handle if it is the right mc controller */
  1386. if (cpu_data(mce->cpu).phys_proc_id != pvt->sbridge_dev->mc)
  1387. return NOTIFY_DONE;
  1388. #endif
  1389. smp_rmb();
  1390. if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
  1391. smp_wmb();
  1392. pvt->mce_overrun++;
  1393. return NOTIFY_DONE;
  1394. }
  1395. /* Copy memory error at the ringbuffer */
  1396. memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
  1397. smp_wmb();
  1398. pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
  1399. /* Handle fatal errors immediately */
  1400. if (mce->mcgstatus & 1)
  1401. sbridge_check_error(mci);
  1402. /* Advice mcelog that the error were handled */
  1403. return NOTIFY_STOP;
  1404. }
  1405. static struct notifier_block sbridge_mce_dec = {
  1406. .notifier_call = sbridge_mce_check_error,
  1407. };
  1408. /****************************************************************************
  1409. EDAC register/unregister logic
  1410. ****************************************************************************/
  1411. static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
  1412. {
  1413. struct mem_ctl_info *mci = sbridge_dev->mci;
  1414. struct sbridge_pvt *pvt;
  1415. if (unlikely(!mci || !mci->pvt_info)) {
  1416. debugf0("MC: " __FILE__ ": %s(): dev = %p\n",
  1417. __func__, &sbridge_dev->pdev[0]->dev);
  1418. sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
  1419. return;
  1420. }
  1421. pvt = mci->pvt_info;
  1422. debugf0("MC: " __FILE__ ": %s(): mci = %p, dev = %p\n",
  1423. __func__, mci, &sbridge_dev->pdev[0]->dev);
  1424. atomic_notifier_chain_unregister(&x86_mce_decoder_chain,
  1425. &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. atomic_notifier_chain_register(&x86_mce_decoder_chain,
  1480. &sbridge_mce_dec);
  1481. return 0;
  1482. fail0:
  1483. kfree(mci->ctl_name);
  1484. edac_mc_free(mci);
  1485. sbridge_dev->mci = NULL;
  1486. return rc;
  1487. }
  1488. /*
  1489. * sbridge_probe Probe for ONE instance of device to see if it is
  1490. * present.
  1491. * return:
  1492. * 0 for FOUND a device
  1493. * < 0 for error code
  1494. */
  1495. static int __devinit sbridge_probe(struct pci_dev *pdev,
  1496. const struct pci_device_id *id)
  1497. {
  1498. int rc;
  1499. u8 mc, num_mc = 0;
  1500. struct sbridge_dev *sbridge_dev;
  1501. /* get the pci devices we want to reserve for our use */
  1502. mutex_lock(&sbridge_edac_lock);
  1503. /*
  1504. * All memory controllers are allocated at the first pass.
  1505. */
  1506. if (unlikely(probed >= 1)) {
  1507. mutex_unlock(&sbridge_edac_lock);
  1508. return -ENODEV;
  1509. }
  1510. probed++;
  1511. rc = sbridge_get_all_devices(&num_mc);
  1512. if (unlikely(rc < 0))
  1513. goto fail0;
  1514. mc = 0;
  1515. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  1516. debugf0("Registering MC#%d (%d of %d)\n", mc, mc + 1, num_mc);
  1517. sbridge_dev->mc = mc++;
  1518. rc = sbridge_register_mci(sbridge_dev);
  1519. if (unlikely(rc < 0))
  1520. goto fail1;
  1521. }
  1522. sbridge_printk(KERN_INFO, "Driver loaded.\n");
  1523. mutex_unlock(&sbridge_edac_lock);
  1524. return 0;
  1525. fail1:
  1526. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
  1527. sbridge_unregister_mci(sbridge_dev);
  1528. sbridge_put_all_devices();
  1529. fail0:
  1530. mutex_unlock(&sbridge_edac_lock);
  1531. return rc;
  1532. }
  1533. /*
  1534. * sbridge_remove destructor for one instance of device
  1535. *
  1536. */
  1537. static void __devexit sbridge_remove(struct pci_dev *pdev)
  1538. {
  1539. struct sbridge_dev *sbridge_dev;
  1540. debugf0(__FILE__ ": %s()\n", __func__);
  1541. /*
  1542. * we have a trouble here: pdev value for removal will be wrong, since
  1543. * it will point to the X58 register used to detect that the machine
  1544. * is a Nehalem or upper design. However, due to the way several PCI
  1545. * devices are grouped together to provide MC functionality, we need
  1546. * to use a different method for releasing the devices
  1547. */
  1548. mutex_lock(&sbridge_edac_lock);
  1549. if (unlikely(!probed)) {
  1550. mutex_unlock(&sbridge_edac_lock);
  1551. return;
  1552. }
  1553. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
  1554. sbridge_unregister_mci(sbridge_dev);
  1555. /* Release PCI resources */
  1556. sbridge_put_all_devices();
  1557. probed--;
  1558. mutex_unlock(&sbridge_edac_lock);
  1559. }
  1560. MODULE_DEVICE_TABLE(pci, sbridge_pci_tbl);
  1561. /*
  1562. * sbridge_driver pci_driver structure for this module
  1563. *
  1564. */
  1565. static struct pci_driver sbridge_driver = {
  1566. .name = "sbridge_edac",
  1567. .probe = sbridge_probe,
  1568. .remove = __devexit_p(sbridge_remove),
  1569. .id_table = sbridge_pci_tbl,
  1570. };
  1571. /*
  1572. * sbridge_init Module entry function
  1573. * Try to initialize this module for its devices
  1574. */
  1575. static int __init sbridge_init(void)
  1576. {
  1577. int pci_rc;
  1578. debugf2("MC: " __FILE__ ": %s()\n", __func__);
  1579. /* Ensure that the OPSTATE is set correctly for POLL or NMI */
  1580. opstate_init();
  1581. pci_rc = pci_register_driver(&sbridge_driver);
  1582. if (pci_rc >= 0)
  1583. return 0;
  1584. sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
  1585. pci_rc);
  1586. return pci_rc;
  1587. }
  1588. /*
  1589. * sbridge_exit() Module exit function
  1590. * Unregister the driver
  1591. */
  1592. static void __exit sbridge_exit(void)
  1593. {
  1594. debugf2("MC: " __FILE__ ": %s()\n", __func__);
  1595. pci_unregister_driver(&sbridge_driver);
  1596. }
  1597. module_init(sbridge_init);
  1598. module_exit(sbridge_exit);
  1599. module_param(edac_op_state, int, 0444);
  1600. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
  1601. MODULE_LICENSE("GPL");
  1602. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  1603. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  1604. MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge memory controllers - "
  1605. SBRIDGE_REVISION);