sb_edac.c 45 KB

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