sb_edac.c 46 KB

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